1 //===-- SystemZISelDAGToDAG.cpp - A dag to dag inst selector for SystemZ --===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines an instruction selector for the SystemZ target. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "SystemZTargetMachine.h" 15 #include "llvm/Analysis/AliasAnalysis.h" 16 #include "llvm/CodeGen/SelectionDAGISel.h" 17 #include "llvm/Support/Debug.h" 18 #include "llvm/Support/raw_ostream.h" 19 20 using namespace llvm; 21 22 namespace { 23 // Used to build addressing modes. 24 struct SystemZAddressingMode { 25 // The shape of the address. 26 enum AddrForm { 27 // base+displacement 28 FormBD, 29 30 // base+displacement+index for load and store operands 31 FormBDXNormal, 32 33 // base+displacement+index for load address operands 34 FormBDXLA, 35 36 // base+displacement+index+ADJDYNALLOC 37 FormBDXDynAlloc 38 }; 39 AddrForm Form; 40 41 // The type of displacement. The enum names here correspond directly 42 // to the definitions in SystemZOperand.td. We could split them into 43 // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it. 44 enum DispRange { 45 Disp12Only, 46 Disp12Pair, 47 Disp20Only, 48 Disp20Only128, 49 Disp20Pair 50 }; 51 DispRange DR; 52 53 // The parts of the address. The address is equivalent to: 54 // 55 // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0) 56 SDValue Base; 57 int64_t Disp; 58 SDValue Index; 59 bool IncludesDynAlloc; 60 61 SystemZAddressingMode(AddrForm form, DispRange dr) 62 : Form(form), DR(dr), Base(), Disp(0), Index(), 63 IncludesDynAlloc(false) {} 64 65 // True if the address can have an index register. 66 bool hasIndexField() { return Form != FormBD; } 67 68 // True if the address can (and must) include ADJDYNALLOC. 69 bool isDynAlloc() { return Form == FormBDXDynAlloc; } 70 71 void dump() { 72 errs() << "SystemZAddressingMode " << this << '\n'; 73 74 errs() << " Base "; 75 if (Base.getNode() != 0) 76 Base.getNode()->dump(); 77 else 78 errs() << "null\n"; 79 80 if (hasIndexField()) { 81 errs() << " Index "; 82 if (Index.getNode() != 0) 83 Index.getNode()->dump(); 84 else 85 errs() << "null\n"; 86 } 87 88 errs() << " Disp " << Disp; 89 if (IncludesDynAlloc) 90 errs() << " + ADJDYNALLOC"; 91 errs() << '\n'; 92 } 93 }; 94 95 // Return a mask with Count low bits set. 96 static uint64_t allOnes(unsigned int Count) { 97 return Count == 0 ? 0 : (uint64_t(1) << (Count - 1) << 1) - 1; 98 } 99 100 // Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation 101 // given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and 102 // Rotate (I5). The combined operand value is effectively: 103 // 104 // (or (rotl Input, Rotate), ~Mask) 105 // 106 // for RNSBG and: 107 // 108 // (and (rotl Input, Rotate), Mask) 109 // 110 // otherwise. The value has BitSize bits. 111 struct RxSBGOperands { 112 RxSBGOperands(unsigned Op, SDValue N) 113 : Opcode(Op), BitSize(N.getValueType().getSizeInBits()), 114 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63), 115 Rotate(0) {} 116 117 unsigned Opcode; 118 unsigned BitSize; 119 uint64_t Mask; 120 SDValue Input; 121 unsigned Start; 122 unsigned End; 123 unsigned Rotate; 124 }; 125 126 class SystemZDAGToDAGISel : public SelectionDAGISel { 127 const SystemZTargetLowering &Lowering; 128 const SystemZSubtarget &Subtarget; 129 130 // Used by SystemZOperands.td to create integer constants. 131 inline SDValue getImm(const SDNode *Node, uint64_t Imm) { 132 return CurDAG->getTargetConstant(Imm, Node->getValueType(0)); 133 } 134 135 const SystemZTargetMachine &getTargetMachine() const { 136 return static_cast<const SystemZTargetMachine &>(TM); 137 } 138 139 const SystemZInstrInfo *getInstrInfo() const { 140 return getTargetMachine().getInstrInfo(); 141 } 142 143 // Try to fold more of the base or index of AM into AM, where IsBase 144 // selects between the base and index. 145 bool expandAddress(SystemZAddressingMode &AM, bool IsBase); 146 147 // Try to describe N in AM, returning true on success. 148 bool selectAddress(SDValue N, SystemZAddressingMode &AM); 149 150 // Extract individual target operands from matched address AM. 151 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT, 152 SDValue &Base, SDValue &Disp); 153 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT, 154 SDValue &Base, SDValue &Disp, SDValue &Index); 155 156 // Try to match Addr as a FormBD address with displacement type DR. 157 // Return true on success, storing the base and displacement in 158 // Base and Disp respectively. 159 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr, 160 SDValue &Base, SDValue &Disp); 161 162 // Try to match Addr as a FormBDX* address of form Form with 163 // displacement type DR. Return true on success, storing the base, 164 // displacement and index in Base, Disp and Index respectively. 165 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form, 166 SystemZAddressingMode::DispRange DR, SDValue Addr, 167 SDValue &Base, SDValue &Disp, SDValue &Index); 168 169 // PC-relative address matching routines used by SystemZOperands.td. 170 bool selectPCRelAddress(SDValue Addr, SDValue &Target) { 171 if (Addr.getOpcode() == SystemZISD::PCREL_WRAPPER) { 172 Target = Addr.getOperand(0); 173 return true; 174 } 175 return false; 176 } 177 178 // BD matching routines used by SystemZOperands.td. 179 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) { 180 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp); 181 } 182 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) { 183 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp); 184 } 185 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) { 186 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp); 187 } 188 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) { 189 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp); 190 } 191 192 // BDX matching routines used by SystemZOperands.td. 193 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp, 194 SDValue &Index) { 195 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, 196 SystemZAddressingMode::Disp12Only, 197 Addr, Base, Disp, Index); 198 } 199 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp, 200 SDValue &Index) { 201 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, 202 SystemZAddressingMode::Disp12Pair, 203 Addr, Base, Disp, Index); 204 } 205 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp, 206 SDValue &Index) { 207 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc, 208 SystemZAddressingMode::Disp12Only, 209 Addr, Base, Disp, Index); 210 } 211 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp, 212 SDValue &Index) { 213 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, 214 SystemZAddressingMode::Disp20Only, 215 Addr, Base, Disp, Index); 216 } 217 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp, 218 SDValue &Index) { 219 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, 220 SystemZAddressingMode::Disp20Only128, 221 Addr, Base, Disp, Index); 222 } 223 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp, 224 SDValue &Index) { 225 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, 226 SystemZAddressingMode::Disp20Pair, 227 Addr, Base, Disp, Index); 228 } 229 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp, 230 SDValue &Index) { 231 return selectBDXAddr(SystemZAddressingMode::FormBDXLA, 232 SystemZAddressingMode::Disp12Pair, 233 Addr, Base, Disp, Index); 234 } 235 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp, 236 SDValue &Index) { 237 return selectBDXAddr(SystemZAddressingMode::FormBDXLA, 238 SystemZAddressingMode::Disp20Pair, 239 Addr, Base, Disp, Index); 240 } 241 242 // Check whether (or Op (and X InsertMask)) is effectively an insertion 243 // of X into bits InsertMask of some Y != Op. Return true if so and 244 // set Op to that Y. 245 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask); 246 247 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used. 248 // Return true on success. 249 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask); 250 251 // Try to fold some of RxSBG.Input into other fields of RxSBG. 252 // Return true on success. 253 bool expandRxSBG(RxSBGOperands &RxSBG); 254 255 // Return an undefined i64 value. 256 SDValue getUNDEF64(SDLoc DL); 257 258 // Convert N to VT, if it isn't already. 259 SDValue convertTo(SDLoc DL, EVT VT, SDValue N); 260 261 // Try to implement AND or shift node N using RISBG with the zero flag set. 262 // Return the selected node on success, otherwise return null. 263 SDNode *tryRISBGZero(SDNode *N); 264 265 // Try to use RISBG or Opcode to implement OR or XOR node N. 266 // Return the selected node on success, otherwise return null. 267 SDNode *tryRxSBG(SDNode *N, unsigned Opcode); 268 269 // If Op0 is null, then Node is a constant that can be loaded using: 270 // 271 // (Opcode UpperVal LowerVal) 272 // 273 // If Op0 is nonnull, then Node can be implemented using: 274 // 275 // (Opcode (Opcode Op0 UpperVal) LowerVal) 276 SDNode *splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0, 277 uint64_t UpperVal, uint64_t LowerVal); 278 279 bool storeLoadCanUseMVC(SDNode *N) const; 280 281 public: 282 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel) 283 : SelectionDAGISel(TM, OptLevel), 284 Lowering(*TM.getTargetLowering()), 285 Subtarget(*TM.getSubtargetImpl()) { } 286 287 // Override MachineFunctionPass. 288 virtual const char *getPassName() const LLVM_OVERRIDE { 289 return "SystemZ DAG->DAG Pattern Instruction Selection"; 290 } 291 292 // Override SelectionDAGISel. 293 virtual SDNode *Select(SDNode *Node) LLVM_OVERRIDE; 294 virtual bool SelectInlineAsmMemoryOperand(const SDValue &Op, 295 char ConstraintCode, 296 std::vector<SDValue> &OutOps) 297 LLVM_OVERRIDE; 298 299 // Include the pieces autogenerated from the target description. 300 #include "SystemZGenDAGISel.inc" 301 }; 302 } // end anonymous namespace 303 304 FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM, 305 CodeGenOpt::Level OptLevel) { 306 return new SystemZDAGToDAGISel(TM, OptLevel); 307 } 308 309 // Return true if Val should be selected as a displacement for an address 310 // with range DR. Here we're interested in the range of both the instruction 311 // described by DR and of any pairing instruction. 312 static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { 313 switch (DR) { 314 case SystemZAddressingMode::Disp12Only: 315 return isUInt<12>(Val); 316 317 case SystemZAddressingMode::Disp12Pair: 318 case SystemZAddressingMode::Disp20Only: 319 case SystemZAddressingMode::Disp20Pair: 320 return isInt<20>(Val); 321 322 case SystemZAddressingMode::Disp20Only128: 323 return isInt<20>(Val) && isInt<20>(Val + 8); 324 } 325 llvm_unreachable("Unhandled displacement range"); 326 } 327 328 // Change the base or index in AM to Value, where IsBase selects 329 // between the base and index. 330 static void changeComponent(SystemZAddressingMode &AM, bool IsBase, 331 SDValue Value) { 332 if (IsBase) 333 AM.Base = Value; 334 else 335 AM.Index = Value; 336 } 337 338 // The base or index of AM is equivalent to Value + ADJDYNALLOC, 339 // where IsBase selects between the base and index. Try to fold the 340 // ADJDYNALLOC into AM. 341 static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase, 342 SDValue Value) { 343 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) { 344 changeComponent(AM, IsBase, Value); 345 AM.IncludesDynAlloc = true; 346 return true; 347 } 348 return false; 349 } 350 351 // The base of AM is equivalent to Base + Index. Try to use Index as 352 // the index register. 353 static bool expandIndex(SystemZAddressingMode &AM, SDValue Base, 354 SDValue Index) { 355 if (AM.hasIndexField() && !AM.Index.getNode()) { 356 AM.Base = Base; 357 AM.Index = Index; 358 return true; 359 } 360 return false; 361 } 362 363 // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects 364 // between the base and index. Try to fold Op1 into AM's displacement. 365 static bool expandDisp(SystemZAddressingMode &AM, bool IsBase, 366 SDValue Op0, ConstantSDNode *Op1) { 367 // First try adjusting the displacement. 368 int64_t TestDisp = AM.Disp + Op1->getSExtValue(); 369 if (selectDisp(AM.DR, TestDisp)) { 370 changeComponent(AM, IsBase, Op0); 371 AM.Disp = TestDisp; 372 return true; 373 } 374 375 // We could consider forcing the displacement into a register and 376 // using it as an index, but it would need to be carefully tuned. 377 return false; 378 } 379 380 bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM, 381 bool IsBase) { 382 SDValue N = IsBase ? AM.Base : AM.Index; 383 unsigned Opcode = N.getOpcode(); 384 if (Opcode == ISD::TRUNCATE) { 385 N = N.getOperand(0); 386 Opcode = N.getOpcode(); 387 } 388 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) { 389 SDValue Op0 = N.getOperand(0); 390 SDValue Op1 = N.getOperand(1); 391 392 unsigned Op0Code = Op0->getOpcode(); 393 unsigned Op1Code = Op1->getOpcode(); 394 395 if (Op0Code == SystemZISD::ADJDYNALLOC) 396 return expandAdjDynAlloc(AM, IsBase, Op1); 397 if (Op1Code == SystemZISD::ADJDYNALLOC) 398 return expandAdjDynAlloc(AM, IsBase, Op0); 399 400 if (Op0Code == ISD::Constant) 401 return expandDisp(AM, IsBase, Op1, cast<ConstantSDNode>(Op0)); 402 if (Op1Code == ISD::Constant) 403 return expandDisp(AM, IsBase, Op0, cast<ConstantSDNode>(Op1)); 404 405 if (IsBase && expandIndex(AM, Op0, Op1)) 406 return true; 407 } 408 return false; 409 } 410 411 // Return true if an instruction with displacement range DR should be 412 // used for displacement value Val. selectDisp(DR, Val) must already hold. 413 static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { 414 assert(selectDisp(DR, Val) && "Invalid displacement"); 415 switch (DR) { 416 case SystemZAddressingMode::Disp12Only: 417 case SystemZAddressingMode::Disp20Only: 418 case SystemZAddressingMode::Disp20Only128: 419 return true; 420 421 case SystemZAddressingMode::Disp12Pair: 422 // Use the other instruction if the displacement is too large. 423 return isUInt<12>(Val); 424 425 case SystemZAddressingMode::Disp20Pair: 426 // Use the other instruction if the displacement is small enough. 427 return !isUInt<12>(Val); 428 } 429 llvm_unreachable("Unhandled displacement range"); 430 } 431 432 // Return true if Base + Disp + Index should be performed by LA(Y). 433 static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) { 434 // Don't use LA(Y) for constants. 435 if (!Base) 436 return false; 437 438 // Always use LA(Y) for frame addresses, since we know that the destination 439 // register is almost always (perhaps always) going to be different from 440 // the frame register. 441 if (Base->getOpcode() == ISD::FrameIndex) 442 return true; 443 444 if (Disp) { 445 // Always use LA(Y) if there is a base, displacement and index. 446 if (Index) 447 return true; 448 449 // Always use LA if the displacement is small enough. It should always 450 // be no worse than AGHI (and better if it avoids a move). 451 if (isUInt<12>(Disp)) 452 return true; 453 454 // For similar reasons, always use LAY if the constant is too big for AGHI. 455 // LAY should be no worse than AGFI. 456 if (!isInt<16>(Disp)) 457 return true; 458 } else { 459 // Don't use LA for plain registers. 460 if (!Index) 461 return false; 462 463 // Don't use LA for plain addition if the index operand is only used 464 // once. It should be a natural two-operand addition in that case. 465 if (Index->hasOneUse()) 466 return false; 467 468 // Prefer addition if the second operation is sign-extended, in the 469 // hope of using AGF. 470 unsigned IndexOpcode = Index->getOpcode(); 471 if (IndexOpcode == ISD::SIGN_EXTEND || 472 IndexOpcode == ISD::SIGN_EXTEND_INREG) 473 return false; 474 } 475 476 // Don't use LA for two-operand addition if either operand is only 477 // used once. The addition instructions are better in that case. 478 if (Base->hasOneUse()) 479 return false; 480 481 return true; 482 } 483 484 // Return true if Addr is suitable for AM, updating AM if so. 485 bool SystemZDAGToDAGISel::selectAddress(SDValue Addr, 486 SystemZAddressingMode &AM) { 487 // Start out assuming that the address will need to be loaded separately, 488 // then try to extend it as much as we can. 489 AM.Base = Addr; 490 491 // First try treating the address as a constant. 492 if (Addr.getOpcode() == ISD::Constant && 493 expandDisp(AM, true, SDValue(), cast<ConstantSDNode>(Addr))) 494 ; 495 else 496 // Otherwise try expanding each component. 497 while (expandAddress(AM, true) || 498 (AM.Index.getNode() && expandAddress(AM, false))) 499 continue; 500 501 // Reject cases where it isn't profitable to use LA(Y). 502 if (AM.Form == SystemZAddressingMode::FormBDXLA && 503 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode())) 504 return false; 505 506 // Reject cases where the other instruction in a pair should be used. 507 if (!isValidDisp(AM.DR, AM.Disp)) 508 return false; 509 510 // Make sure that ADJDYNALLOC is included where necessary. 511 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) 512 return false; 513 514 DEBUG(AM.dump()); 515 return true; 516 } 517 518 // Insert a node into the DAG at least before Pos. This will reposition 519 // the node as needed, and will assign it a node ID that is <= Pos's ID. 520 // Note that this does *not* preserve the uniqueness of node IDs! 521 // The selection DAG must no longer depend on their uniqueness when this 522 // function is used. 523 static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) { 524 if (N.getNode()->getNodeId() == -1 || 525 N.getNode()->getNodeId() > Pos->getNodeId()) { 526 DAG->RepositionNode(Pos, N.getNode()); 527 N.getNode()->setNodeId(Pos->getNodeId()); 528 } 529 } 530 531 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, 532 EVT VT, SDValue &Base, 533 SDValue &Disp) { 534 Base = AM.Base; 535 if (!Base.getNode()) 536 // Register 0 means "no base". This is mostly useful for shifts. 537 Base = CurDAG->getRegister(0, VT); 538 else if (Base.getOpcode() == ISD::FrameIndex) { 539 // Lower a FrameIndex to a TargetFrameIndex. 540 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex(); 541 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT); 542 } else if (Base.getValueType() != VT) { 543 // Truncate values from i64 to i32, for shifts. 544 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 && 545 "Unexpected truncation"); 546 SDLoc DL(Base); 547 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base); 548 insertDAGNode(CurDAG, Base.getNode(), Trunc); 549 Base = Trunc; 550 } 551 552 // Lower the displacement to a TargetConstant. 553 Disp = CurDAG->getTargetConstant(AM.Disp, VT); 554 } 555 556 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, 557 EVT VT, SDValue &Base, 558 SDValue &Disp, SDValue &Index) { 559 getAddressOperands(AM, VT, Base, Disp); 560 561 Index = AM.Index; 562 if (!Index.getNode()) 563 // Register 0 means "no index". 564 Index = CurDAG->getRegister(0, VT); 565 } 566 567 bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR, 568 SDValue Addr, SDValue &Base, 569 SDValue &Disp) { 570 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR); 571 if (!selectAddress(Addr, AM)) 572 return false; 573 574 getAddressOperands(AM, Addr.getValueType(), Base, Disp); 575 return true; 576 } 577 578 bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form, 579 SystemZAddressingMode::DispRange DR, 580 SDValue Addr, SDValue &Base, 581 SDValue &Disp, SDValue &Index) { 582 SystemZAddressingMode AM(Form, DR); 583 if (!selectAddress(Addr, AM)) 584 return false; 585 586 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index); 587 return true; 588 } 589 590 bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op, 591 uint64_t InsertMask) { 592 // We're only interested in cases where the insertion is into some operand 593 // of Op, rather than into Op itself. The only useful case is an AND. 594 if (Op.getOpcode() != ISD::AND) 595 return false; 596 597 // We need a constant mask. 598 ConstantSDNode *MaskNode = 599 dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode()); 600 if (!MaskNode) 601 return false; 602 603 // It's not an insertion of Op.getOperand(0) if the two masks overlap. 604 uint64_t AndMask = MaskNode->getZExtValue(); 605 if (InsertMask & AndMask) 606 return false; 607 608 // It's only an insertion if all bits are covered or are known to be zero. 609 // The inner check covers all cases but is more expensive. 610 uint64_t Used = allOnes(Op.getValueType().getSizeInBits()); 611 if (Used != (AndMask | InsertMask)) { 612 APInt KnownZero, KnownOne; 613 CurDAG->ComputeMaskedBits(Op.getOperand(0), KnownZero, KnownOne); 614 if (Used != (AndMask | InsertMask | KnownZero.getZExtValue())) 615 return false; 616 } 617 618 Op = Op.getOperand(0); 619 return true; 620 } 621 622 bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) { 623 const SystemZInstrInfo *TII = getInstrInfo(); 624 if (RxSBG.Rotate != 0) 625 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)); 626 Mask &= RxSBG.Mask; 627 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) { 628 RxSBG.Mask = Mask; 629 return true; 630 } 631 return false; 632 } 633 634 // RxSBG.Input is a shift of Count bits in the direction given by IsLeft. 635 // Return true if the result depends on the signs or zeros that are 636 // shifted in. 637 static bool shiftedInBitsMatter(RxSBGOperands &RxSBG, uint64_t Count, 638 bool IsLeft) { 639 // Work out which bits of the shift result are zeros or sign copies. 640 uint64_t ShiftedIn = allOnes(Count); 641 if (!IsLeft) 642 ShiftedIn <<= RxSBG.BitSize - Count; 643 644 // Rotate that mask in the same way as RxSBG.Input is rotated. 645 if (RxSBG.Rotate != 0) 646 ShiftedIn = ((ShiftedIn << RxSBG.Rotate) | 647 (ShiftedIn >> (64 - RxSBG.Rotate))); 648 649 // Fail if any of the zero or sign bits are used. 650 return (ShiftedIn & RxSBG.Mask) != 0; 651 } 652 653 bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) { 654 SDValue N = RxSBG.Input; 655 unsigned Opcode = N.getOpcode(); 656 switch (Opcode) { 657 case ISD::AND: { 658 if (RxSBG.Opcode == SystemZ::RNSBG) 659 return false; 660 661 ConstantSDNode *MaskNode = 662 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 663 if (!MaskNode) 664 return false; 665 666 SDValue Input = N.getOperand(0); 667 uint64_t Mask = MaskNode->getZExtValue(); 668 if (!refineRxSBGMask(RxSBG, Mask)) { 669 // If some bits of Input are already known zeros, those bits will have 670 // been removed from the mask. See if adding them back in makes the 671 // mask suitable. 672 APInt KnownZero, KnownOne; 673 CurDAG->ComputeMaskedBits(Input, KnownZero, KnownOne); 674 Mask |= KnownZero.getZExtValue(); 675 if (!refineRxSBGMask(RxSBG, Mask)) 676 return false; 677 } 678 RxSBG.Input = Input; 679 return true; 680 } 681 682 case ISD::OR: { 683 if (RxSBG.Opcode != SystemZ::RNSBG) 684 return false; 685 686 ConstantSDNode *MaskNode = 687 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 688 if (!MaskNode) 689 return false; 690 691 SDValue Input = N.getOperand(0); 692 uint64_t Mask = ~MaskNode->getZExtValue(); 693 if (!refineRxSBGMask(RxSBG, Mask)) { 694 // If some bits of Input are already known ones, those bits will have 695 // been removed from the mask. See if adding them back in makes the 696 // mask suitable. 697 APInt KnownZero, KnownOne; 698 CurDAG->ComputeMaskedBits(Input, KnownZero, KnownOne); 699 Mask &= ~KnownOne.getZExtValue(); 700 if (!refineRxSBGMask(RxSBG, Mask)) 701 return false; 702 } 703 RxSBG.Input = Input; 704 return true; 705 } 706 707 case ISD::ROTL: { 708 // Any 64-bit rotate left can be merged into the RxSBG. 709 if (RxSBG.BitSize != 64) 710 return false; 711 ConstantSDNode *CountNode 712 = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 713 if (!CountNode) 714 return false; 715 716 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63; 717 RxSBG.Input = N.getOperand(0); 718 return true; 719 } 720 721 case ISD::SHL: { 722 ConstantSDNode *CountNode = 723 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 724 if (!CountNode) 725 return false; 726 727 uint64_t Count = CountNode->getZExtValue(); 728 if (Count < 1 || Count >= RxSBG.BitSize) 729 return false; 730 731 if (RxSBG.Opcode == SystemZ::RNSBG) { 732 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom 733 // count bits from RxSBG.Input are ignored. 734 if (shiftedInBitsMatter(RxSBG, Count, true)) 735 return false; 736 } else { 737 // Treat (shl X, count) as (and (rotl X, count), ~0<<count). 738 if (!refineRxSBGMask(RxSBG, allOnes(RxSBG.BitSize - Count) << Count)) 739 return false; 740 } 741 742 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63; 743 RxSBG.Input = N.getOperand(0); 744 return true; 745 } 746 747 case ISD::SRL: 748 case ISD::SRA: { 749 ConstantSDNode *CountNode = 750 dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 751 if (!CountNode) 752 return false; 753 754 uint64_t Count = CountNode->getZExtValue(); 755 if (Count < 1 || Count >= RxSBG.BitSize) 756 return false; 757 758 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) { 759 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top 760 // count bits from RxSBG.Input are ignored. 761 if (shiftedInBitsMatter(RxSBG, Count, false)) 762 return false; 763 } else { 764 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count), 765 // which is similar to SLL above. 766 if (!refineRxSBGMask(RxSBG, allOnes(RxSBG.BitSize - Count))) 767 return false; 768 } 769 770 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63; 771 RxSBG.Input = N.getOperand(0); 772 return true; 773 } 774 default: 775 return false; 776 } 777 } 778 779 SDValue SystemZDAGToDAGISel::getUNDEF64(SDLoc DL) { 780 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, MVT::i64); 781 return SDValue(N, 0); 782 } 783 784 SDValue SystemZDAGToDAGISel::convertTo(SDLoc DL, EVT VT, SDValue N) { 785 if (N.getValueType() == MVT::i32 && VT == MVT::i64) { 786 SDValue Index = CurDAG->getTargetConstant(SystemZ::subreg_32bit, MVT::i64); 787 SDNode *Insert = CurDAG->getMachineNode(TargetOpcode::INSERT_SUBREG, 788 DL, VT, getUNDEF64(DL), N, Index); 789 return SDValue(Insert, 0); 790 } 791 if (N.getValueType() == MVT::i64 && VT == MVT::i32) { 792 SDValue Index = CurDAG->getTargetConstant(SystemZ::subreg_32bit, MVT::i64); 793 SDNode *Extract = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 794 DL, VT, N, Index); 795 return SDValue(Extract, 0); 796 } 797 assert(N.getValueType() == VT && "Unexpected value types"); 798 return N; 799 } 800 801 SDNode *SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) { 802 EVT VT = N->getValueType(0); 803 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0)); 804 unsigned Count = 0; 805 while (expandRxSBG(RISBG)) 806 Count += 1; 807 if (Count == 0) 808 return 0; 809 if (Count == 1) { 810 // Prefer to use normal shift instructions over RISBG, since they can handle 811 // all cases and are sometimes shorter. 812 if (N->getOpcode() != ISD::AND) 813 return 0; 814 815 // Prefer register extensions like LLC over RISBG. Also prefer to start 816 // out with normal ANDs if one instruction would be enough. We can convert 817 // these ANDs into an RISBG later if a three-address instruction is useful. 818 if (VT == MVT::i32 || 819 RISBG.Mask == 0xff || 820 RISBG.Mask == 0xffff || 821 SystemZ::isImmLF(~RISBG.Mask) || 822 SystemZ::isImmHF(~RISBG.Mask)) { 823 // Force the new mask into the DAG, since it may include known-one bits. 824 ConstantSDNode *MaskN = cast<ConstantSDNode>(N->getOperand(1).getNode()); 825 if (MaskN->getZExtValue() != RISBG.Mask) { 826 SDValue NewMask = CurDAG->getConstant(RISBG.Mask, VT); 827 N = CurDAG->UpdateNodeOperands(N, N->getOperand(0), NewMask); 828 return SelectCode(N); 829 } 830 return 0; 831 } 832 } 833 834 SDValue Ops[5] = { 835 getUNDEF64(SDLoc(N)), 836 convertTo(SDLoc(N), MVT::i64, RISBG.Input), 837 CurDAG->getTargetConstant(RISBG.Start, MVT::i32), 838 CurDAG->getTargetConstant(RISBG.End | 128, MVT::i32), 839 CurDAG->getTargetConstant(RISBG.Rotate, MVT::i32) 840 }; 841 N = CurDAG->getMachineNode(SystemZ::RISBG, SDLoc(N), MVT::i64, Ops); 842 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode(); 843 } 844 845 SDNode *SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) { 846 // Try treating each operand of N as the second operand of the RxSBG 847 // and see which goes deepest. 848 RxSBGOperands RxSBG[] = { 849 RxSBGOperands(Opcode, N->getOperand(0)), 850 RxSBGOperands(Opcode, N->getOperand(1)) 851 }; 852 unsigned Count[] = { 0, 0 }; 853 for (unsigned I = 0; I < 2; ++I) 854 while (expandRxSBG(RxSBG[I])) 855 Count[I] += 1; 856 857 // Do nothing if neither operand is suitable. 858 if (Count[0] == 0 && Count[1] == 0) 859 return 0; 860 861 // Pick the deepest second operand. 862 unsigned I = Count[0] > Count[1] ? 0 : 1; 863 SDValue Op0 = N->getOperand(I ^ 1); 864 865 // Prefer IC for character insertions from memory. 866 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0) 867 if (LoadSDNode *Load = dyn_cast<LoadSDNode>(Op0.getNode())) 868 if (Load->getMemoryVT() == MVT::i8) 869 return 0; 870 871 // See whether we can avoid an AND in the first operand by converting 872 // ROSBG to RISBG. 873 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) 874 Opcode = SystemZ::RISBG; 875 876 EVT VT = N->getValueType(0); 877 SDValue Ops[5] = { 878 convertTo(SDLoc(N), MVT::i64, Op0), 879 convertTo(SDLoc(N), MVT::i64, RxSBG[I].Input), 880 CurDAG->getTargetConstant(RxSBG[I].Start, MVT::i32), 881 CurDAG->getTargetConstant(RxSBG[I].End, MVT::i32), 882 CurDAG->getTargetConstant(RxSBG[I].Rotate, MVT::i32) 883 }; 884 N = CurDAG->getMachineNode(Opcode, SDLoc(N), MVT::i64, Ops); 885 return convertTo(SDLoc(N), VT, SDValue(N, 0)).getNode(); 886 } 887 888 SDNode *SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node, 889 SDValue Op0, uint64_t UpperVal, 890 uint64_t LowerVal) { 891 EVT VT = Node->getValueType(0); 892 SDLoc DL(Node); 893 SDValue Upper = CurDAG->getConstant(UpperVal, VT); 894 if (Op0.getNode()) 895 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper); 896 Upper = SDValue(Select(Upper.getNode()), 0); 897 898 SDValue Lower = CurDAG->getConstant(LowerVal, VT); 899 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower); 900 return Or.getNode(); 901 } 902 903 // N is a (store (load ...), ...) pattern. Return true if it can use MVC. 904 bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const { 905 StoreSDNode *Store = cast<StoreSDNode>(N); 906 LoadSDNode *Load = cast<LoadSDNode>(Store->getValue().getNode()); 907 908 // MVC is logically a bytewise copy, so can't be used for volatile accesses. 909 if (Load->isVolatile() || Store->isVolatile()) 910 return false; 911 912 // Prefer not to use MVC if either address can use ... RELATIVE LONG 913 // instructions. 914 assert(Load->getMemoryVT() == Store->getMemoryVT() && 915 "Should already have checked that the types match"); 916 uint64_t Size = Load->getMemoryVT().getStoreSize(); 917 if (Size > 1 && Size <= 8) { 918 // Prefer LHRL, LRL and LGRL. 919 if (Load->getBasePtr().getOpcode() == SystemZISD::PCREL_WRAPPER) 920 return false; 921 // Prefer STHRL, STRL and STGRL. 922 if (Store->getBasePtr().getOpcode() == SystemZISD::PCREL_WRAPPER) 923 return false; 924 } 925 926 // There's no chance of overlap if the load is invariant. 927 if (Load->isInvariant()) 928 return true; 929 930 // If both operands are aligned, they must be equal or not overlap. 931 if (Load->getAlignment() >= Size && Store->getAlignment() >= Size) 932 return true; 933 934 // Otherwise we need to check whether there's an alias. 935 const Value *V1 = Load->getSrcValue(); 936 const Value *V2 = Store->getSrcValue(); 937 if (!V1 || !V2) 938 return false; 939 940 int64_t End1 = Load->getSrcValueOffset() + Size; 941 int64_t End2 = Store->getSrcValueOffset() + Size; 942 return !AA->alias(AliasAnalysis::Location(V1, End1, Load->getTBAAInfo()), 943 AliasAnalysis::Location(V2, End2, Store->getTBAAInfo())); 944 } 945 946 SDNode *SystemZDAGToDAGISel::Select(SDNode *Node) { 947 // Dump information about the Node being selected 948 DEBUG(errs() << "Selecting: "; Node->dump(CurDAG); errs() << "\n"); 949 950 // If we have a custom node, we already have selected! 951 if (Node->isMachineOpcode()) { 952 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); 953 return 0; 954 } 955 956 unsigned Opcode = Node->getOpcode(); 957 SDNode *ResNode = 0; 958 switch (Opcode) { 959 case ISD::OR: 960 if (Node->getOperand(1).getOpcode() != ISD::Constant) 961 ResNode = tryRxSBG(Node, SystemZ::ROSBG); 962 goto or_xor; 963 964 case ISD::XOR: 965 if (Node->getOperand(1).getOpcode() != ISD::Constant) 966 ResNode = tryRxSBG(Node, SystemZ::RXSBG); 967 // Fall through. 968 or_xor: 969 // If this is a 64-bit operation in which both 32-bit halves are nonzero, 970 // split the operation into two. 971 if (!ResNode && Node->getValueType(0) == MVT::i64) 972 if (ConstantSDNode *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) { 973 uint64_t Val = Op1->getZExtValue(); 974 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) 975 Node = splitLargeImmediate(Opcode, Node, Node->getOperand(0), 976 Val - uint32_t(Val), uint32_t(Val)); 977 } 978 break; 979 980 case ISD::AND: 981 if (Node->getOperand(1).getOpcode() != ISD::Constant) 982 ResNode = tryRxSBG(Node, SystemZ::RNSBG); 983 // Fall through. 984 case ISD::ROTL: 985 case ISD::SHL: 986 case ISD::SRL: 987 if (!ResNode) 988 ResNode = tryRISBGZero(Node); 989 break; 990 991 case ISD::Constant: 992 // If this is a 64-bit constant that is out of the range of LLILF, 993 // LLIHF and LGFI, split it into two 32-bit pieces. 994 if (Node->getValueType(0) == MVT::i64) { 995 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue(); 996 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) 997 Node = splitLargeImmediate(ISD::OR, Node, SDValue(), 998 Val - uint32_t(Val), uint32_t(Val)); 999 } 1000 break; 1001 1002 case ISD::ATOMIC_LOAD_SUB: 1003 // Try to convert subtractions of constants to additions. 1004 if (ConstantSDNode *Op2 = dyn_cast<ConstantSDNode>(Node->getOperand(2))) { 1005 uint64_t Value = -Op2->getZExtValue(); 1006 EVT VT = Node->getValueType(0); 1007 if (VT == MVT::i32 || isInt<32>(Value)) { 1008 SDValue Ops[] = { Node->getOperand(0), Node->getOperand(1), 1009 CurDAG->getConstant(int32_t(Value), VT) }; 1010 Node = CurDAG->MorphNodeTo(Node, ISD::ATOMIC_LOAD_ADD, 1011 Node->getVTList(), Ops, array_lengthof(Ops)); 1012 } 1013 } 1014 break; 1015 1016 case SystemZISD::SELECT_CCMASK: { 1017 SDValue Op0 = Node->getOperand(0); 1018 SDValue Op1 = Node->getOperand(1); 1019 // Prefer to put any load first, so that it can be matched as a 1020 // conditional load. 1021 if (Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) { 1022 SDValue CCValid = Node->getOperand(2); 1023 SDValue CCMask = Node->getOperand(3); 1024 uint64_t ConstCCValid = 1025 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue(); 1026 uint64_t ConstCCMask = 1027 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue(); 1028 // Invert the condition. 1029 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, 1030 CCMask.getValueType()); 1031 SDValue Op4 = Node->getOperand(4); 1032 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4); 1033 } 1034 break; 1035 } 1036 } 1037 1038 // Select the default instruction 1039 if (!ResNode) 1040 ResNode = SelectCode(Node); 1041 1042 DEBUG(errs() << "=> "; 1043 if (ResNode == NULL || ResNode == Node) 1044 Node->dump(CurDAG); 1045 else 1046 ResNode->dump(CurDAG); 1047 errs() << "\n"; 1048 ); 1049 return ResNode; 1050 } 1051 1052 bool SystemZDAGToDAGISel:: 1053 SelectInlineAsmMemoryOperand(const SDValue &Op, 1054 char ConstraintCode, 1055 std::vector<SDValue> &OutOps) { 1056 assert(ConstraintCode == 'm' && "Unexpected constraint code"); 1057 // Accept addresses with short displacements, which are compatible 1058 // with Q, R, S and T. But keep the index operand for future expansion. 1059 SDValue Base, Disp, Index; 1060 if (!selectBDXAddr(SystemZAddressingMode::FormBD, 1061 SystemZAddressingMode::Disp12Only, 1062 Op, Base, Disp, Index)) 1063 return true; 1064 OutOps.push_back(Base); 1065 OutOps.push_back(Disp); 1066 OutOps.push_back(Index); 1067 return false; 1068 } 1069