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