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/KnownBits.h" 19 #include "llvm/Support/raw_ostream.h" 20 21 using namespace llvm; 22 23 #define DEBUG_TYPE "systemz-isel" 24 25 namespace { 26 // Used to build addressing modes. 27 struct SystemZAddressingMode { 28 // The shape of the address. 29 enum AddrForm { 30 // base+displacement 31 FormBD, 32 33 // base+displacement+index for load and store operands 34 FormBDXNormal, 35 36 // base+displacement+index for load address operands 37 FormBDXLA, 38 39 // base+displacement+index+ADJDYNALLOC 40 FormBDXDynAlloc 41 }; 42 AddrForm Form; 43 44 // The type of displacement. The enum names here correspond directly 45 // to the definitions in SystemZOperand.td. We could split them into 46 // flags -- single/pair, 128-bit, etc. -- but it hardly seems worth it. 47 enum DispRange { 48 Disp12Only, 49 Disp12Pair, 50 Disp20Only, 51 Disp20Only128, 52 Disp20Pair 53 }; 54 DispRange DR; 55 56 // The parts of the address. The address is equivalent to: 57 // 58 // Base + Disp + Index + (IncludesDynAlloc ? ADJDYNALLOC : 0) 59 SDValue Base; 60 int64_t Disp; 61 SDValue Index; 62 bool IncludesDynAlloc; 63 64 SystemZAddressingMode(AddrForm form, DispRange dr) 65 : Form(form), DR(dr), Base(), Disp(0), Index(), 66 IncludesDynAlloc(false) {} 67 68 // True if the address can have an index register. 69 bool hasIndexField() { return Form != FormBD; } 70 71 // True if the address can (and must) include ADJDYNALLOC. 72 bool isDynAlloc() { return Form == FormBDXDynAlloc; } 73 74 void dump() { 75 errs() << "SystemZAddressingMode " << this << '\n'; 76 77 errs() << " Base "; 78 if (Base.getNode()) 79 Base.getNode()->dump(); 80 else 81 errs() << "null\n"; 82 83 if (hasIndexField()) { 84 errs() << " Index "; 85 if (Index.getNode()) 86 Index.getNode()->dump(); 87 else 88 errs() << "null\n"; 89 } 90 91 errs() << " Disp " << Disp; 92 if (IncludesDynAlloc) 93 errs() << " + ADJDYNALLOC"; 94 errs() << '\n'; 95 } 96 }; 97 98 // Return a mask with Count low bits set. 99 static uint64_t allOnes(unsigned int Count) { 100 assert(Count <= 64); 101 if (Count > 63) 102 return UINT64_MAX; 103 return (uint64_t(1) << Count) - 1; 104 } 105 106 // Represents operands 2 to 5 of the ROTATE AND ... SELECTED BITS operation 107 // given by Opcode. The operands are: Input (R2), Start (I3), End (I4) and 108 // Rotate (I5). The combined operand value is effectively: 109 // 110 // (or (rotl Input, Rotate), ~Mask) 111 // 112 // for RNSBG and: 113 // 114 // (and (rotl Input, Rotate), Mask) 115 // 116 // otherwise. The output value has BitSize bits, although Input may be 117 // narrower (in which case the upper bits are don't care), or wider (in which 118 // case the result will be truncated as part of the operation). 119 struct RxSBGOperands { 120 RxSBGOperands(unsigned Op, SDValue N) 121 : Opcode(Op), BitSize(N.getValueSizeInBits()), 122 Mask(allOnes(BitSize)), Input(N), Start(64 - BitSize), End(63), 123 Rotate(0) {} 124 125 unsigned Opcode; 126 unsigned BitSize; 127 uint64_t Mask; 128 SDValue Input; 129 unsigned Start; 130 unsigned End; 131 unsigned Rotate; 132 }; 133 134 class SystemZDAGToDAGISel : public SelectionDAGISel { 135 const SystemZSubtarget *Subtarget; 136 137 // Used by SystemZOperands.td to create integer constants. 138 inline SDValue getImm(const SDNode *Node, uint64_t Imm) const { 139 return CurDAG->getTargetConstant(Imm, SDLoc(Node), Node->getValueType(0)); 140 } 141 142 const SystemZTargetMachine &getTargetMachine() const { 143 return static_cast<const SystemZTargetMachine &>(TM); 144 } 145 146 const SystemZInstrInfo *getInstrInfo() const { 147 return Subtarget->getInstrInfo(); 148 } 149 150 // Try to fold more of the base or index of AM into AM, where IsBase 151 // selects between the base and index. 152 bool expandAddress(SystemZAddressingMode &AM, bool IsBase) const; 153 154 // Try to describe N in AM, returning true on success. 155 bool selectAddress(SDValue N, SystemZAddressingMode &AM) const; 156 157 // Extract individual target operands from matched address AM. 158 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT, 159 SDValue &Base, SDValue &Disp) const; 160 void getAddressOperands(const SystemZAddressingMode &AM, EVT VT, 161 SDValue &Base, SDValue &Disp, SDValue &Index) const; 162 163 // Try to match Addr as a FormBD address with displacement type DR. 164 // Return true on success, storing the base and displacement in 165 // Base and Disp respectively. 166 bool selectBDAddr(SystemZAddressingMode::DispRange DR, SDValue Addr, 167 SDValue &Base, SDValue &Disp) const; 168 169 // Try to match Addr as a FormBDX address with displacement type DR. 170 // Return true on success and if the result had no index. Store the 171 // base and displacement in Base and Disp respectively. 172 bool selectMVIAddr(SystemZAddressingMode::DispRange DR, SDValue Addr, 173 SDValue &Base, SDValue &Disp) const; 174 175 // Try to match Addr as a FormBDX* address of form Form with 176 // displacement type DR. Return true on success, storing the base, 177 // displacement and index in Base, Disp and Index respectively. 178 bool selectBDXAddr(SystemZAddressingMode::AddrForm Form, 179 SystemZAddressingMode::DispRange DR, SDValue Addr, 180 SDValue &Base, SDValue &Disp, SDValue &Index) const; 181 182 // PC-relative address matching routines used by SystemZOperands.td. 183 bool selectPCRelAddress(SDValue Addr, SDValue &Target) const { 184 if (SystemZISD::isPCREL(Addr.getOpcode())) { 185 Target = Addr.getOperand(0); 186 return true; 187 } 188 return false; 189 } 190 191 // BD matching routines used by SystemZOperands.td. 192 bool selectBDAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp) const { 193 return selectBDAddr(SystemZAddressingMode::Disp12Only, Addr, Base, Disp); 194 } 195 bool selectBDAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { 196 return selectBDAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp); 197 } 198 bool selectBDAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp) const { 199 return selectBDAddr(SystemZAddressingMode::Disp20Only, Addr, Base, Disp); 200 } 201 bool selectBDAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { 202 return selectBDAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp); 203 } 204 205 // MVI matching routines used by SystemZOperands.td. 206 bool selectMVIAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { 207 return selectMVIAddr(SystemZAddressingMode::Disp12Pair, Addr, Base, Disp); 208 } 209 bool selectMVIAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp) const { 210 return selectMVIAddr(SystemZAddressingMode::Disp20Pair, Addr, Base, Disp); 211 } 212 213 // BDX matching routines used by SystemZOperands.td. 214 bool selectBDXAddr12Only(SDValue Addr, SDValue &Base, SDValue &Disp, 215 SDValue &Index) const { 216 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, 217 SystemZAddressingMode::Disp12Only, 218 Addr, Base, Disp, Index); 219 } 220 bool selectBDXAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp, 221 SDValue &Index) const { 222 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, 223 SystemZAddressingMode::Disp12Pair, 224 Addr, Base, Disp, Index); 225 } 226 bool selectDynAlloc12Only(SDValue Addr, SDValue &Base, SDValue &Disp, 227 SDValue &Index) const { 228 return selectBDXAddr(SystemZAddressingMode::FormBDXDynAlloc, 229 SystemZAddressingMode::Disp12Only, 230 Addr, Base, Disp, Index); 231 } 232 bool selectBDXAddr20Only(SDValue Addr, SDValue &Base, SDValue &Disp, 233 SDValue &Index) const { 234 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, 235 SystemZAddressingMode::Disp20Only, 236 Addr, Base, Disp, Index); 237 } 238 bool selectBDXAddr20Only128(SDValue Addr, SDValue &Base, SDValue &Disp, 239 SDValue &Index) const { 240 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, 241 SystemZAddressingMode::Disp20Only128, 242 Addr, Base, Disp, Index); 243 } 244 bool selectBDXAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp, 245 SDValue &Index) const { 246 return selectBDXAddr(SystemZAddressingMode::FormBDXNormal, 247 SystemZAddressingMode::Disp20Pair, 248 Addr, Base, Disp, Index); 249 } 250 bool selectLAAddr12Pair(SDValue Addr, SDValue &Base, SDValue &Disp, 251 SDValue &Index) const { 252 return selectBDXAddr(SystemZAddressingMode::FormBDXLA, 253 SystemZAddressingMode::Disp12Pair, 254 Addr, Base, Disp, Index); 255 } 256 bool selectLAAddr20Pair(SDValue Addr, SDValue &Base, SDValue &Disp, 257 SDValue &Index) const { 258 return selectBDXAddr(SystemZAddressingMode::FormBDXLA, 259 SystemZAddressingMode::Disp20Pair, 260 Addr, Base, Disp, Index); 261 } 262 263 // Try to match Addr as an address with a base, 12-bit displacement 264 // and index, where the index is element Elem of a vector. 265 // Return true on success, storing the base, displacement and vector 266 // in Base, Disp and Index respectively. 267 bool selectBDVAddr12Only(SDValue Addr, SDValue Elem, SDValue &Base, 268 SDValue &Disp, SDValue &Index) const; 269 270 // Check whether (or Op (and X InsertMask)) is effectively an insertion 271 // of X into bits InsertMask of some Y != Op. Return true if so and 272 // set Op to that Y. 273 bool detectOrAndInsertion(SDValue &Op, uint64_t InsertMask) const; 274 275 // Try to update RxSBG so that only the bits of RxSBG.Input in Mask are used. 276 // Return true on success. 277 bool refineRxSBGMask(RxSBGOperands &RxSBG, uint64_t Mask) const; 278 279 // Try to fold some of RxSBG.Input into other fields of RxSBG. 280 // Return true on success. 281 bool expandRxSBG(RxSBGOperands &RxSBG) const; 282 283 // Return an undefined value of type VT. 284 SDValue getUNDEF(const SDLoc &DL, EVT VT) const; 285 286 // Convert N to VT, if it isn't already. 287 SDValue convertTo(const SDLoc &DL, EVT VT, SDValue N) const; 288 289 // Try to implement AND or shift node N using RISBG with the zero flag set. 290 // Return the selected node on success, otherwise return null. 291 bool tryRISBGZero(SDNode *N); 292 293 // Try to use RISBG or Opcode to implement OR or XOR node N. 294 // Return the selected node on success, otherwise return null. 295 bool tryRxSBG(SDNode *N, unsigned Opcode); 296 297 // If Op0 is null, then Node is a constant that can be loaded using: 298 // 299 // (Opcode UpperVal LowerVal) 300 // 301 // If Op0 is nonnull, then Node can be implemented using: 302 // 303 // (Opcode (Opcode Op0 UpperVal) LowerVal) 304 void splitLargeImmediate(unsigned Opcode, SDNode *Node, SDValue Op0, 305 uint64_t UpperVal, uint64_t LowerVal); 306 307 // Try to use gather instruction Opcode to implement vector insertion N. 308 bool tryGather(SDNode *N, unsigned Opcode); 309 310 // Try to use scatter instruction Opcode to implement store Store. 311 bool tryScatter(StoreSDNode *Store, unsigned Opcode); 312 313 // Return true if Load and Store are loads and stores of the same size 314 // and are guaranteed not to overlap. Such operations can be implemented 315 // using block (SS-format) instructions. 316 // 317 // Partial overlap would lead to incorrect code, since the block operations 318 // are logically bytewise, even though they have a fast path for the 319 // non-overlapping case. We also need to avoid full overlap (i.e. two 320 // addresses that might be equal at run time) because although that case 321 // would be handled correctly, it might be implemented by millicode. 322 bool canUseBlockOperation(StoreSDNode *Store, LoadSDNode *Load) const; 323 324 // N is a (store (load Y), X) pattern. Return true if it can use an MVC 325 // from Y to X. 326 bool storeLoadCanUseMVC(SDNode *N) const; 327 328 // N is a (store (op (load A[0]), (load A[1])), X) pattern. Return true 329 // if A[1 - I] == X and if N can use a block operation like NC from A[I] 330 // to X. 331 bool storeLoadCanUseBlockBinary(SDNode *N, unsigned I) const; 332 333 // Try to expand a boolean SELECT_CCMASK using an IPM sequence. 334 SDValue expandSelectBoolean(SDNode *Node); 335 336 public: 337 SystemZDAGToDAGISel(SystemZTargetMachine &TM, CodeGenOpt::Level OptLevel) 338 : SelectionDAGISel(TM, OptLevel) {} 339 340 bool runOnMachineFunction(MachineFunction &MF) override { 341 Subtarget = &MF.getSubtarget<SystemZSubtarget>(); 342 return SelectionDAGISel::runOnMachineFunction(MF); 343 } 344 345 // Override MachineFunctionPass. 346 StringRef getPassName() const override { 347 return "SystemZ DAG->DAG Pattern Instruction Selection"; 348 } 349 350 // Override SelectionDAGISel. 351 void Select(SDNode *Node) override; 352 bool SelectInlineAsmMemoryOperand(const SDValue &Op, unsigned ConstraintID, 353 std::vector<SDValue> &OutOps) override; 354 void PreprocessISelDAG() override; 355 356 // Include the pieces autogenerated from the target description. 357 #include "SystemZGenDAGISel.inc" 358 }; 359 } // end anonymous namespace 360 361 FunctionPass *llvm::createSystemZISelDag(SystemZTargetMachine &TM, 362 CodeGenOpt::Level OptLevel) { 363 return new SystemZDAGToDAGISel(TM, OptLevel); 364 } 365 366 // Return true if Val should be selected as a displacement for an address 367 // with range DR. Here we're interested in the range of both the instruction 368 // described by DR and of any pairing instruction. 369 static bool selectDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { 370 switch (DR) { 371 case SystemZAddressingMode::Disp12Only: 372 return isUInt<12>(Val); 373 374 case SystemZAddressingMode::Disp12Pair: 375 case SystemZAddressingMode::Disp20Only: 376 case SystemZAddressingMode::Disp20Pair: 377 return isInt<20>(Val); 378 379 case SystemZAddressingMode::Disp20Only128: 380 return isInt<20>(Val) && isInt<20>(Val + 8); 381 } 382 llvm_unreachable("Unhandled displacement range"); 383 } 384 385 // Change the base or index in AM to Value, where IsBase selects 386 // between the base and index. 387 static void changeComponent(SystemZAddressingMode &AM, bool IsBase, 388 SDValue Value) { 389 if (IsBase) 390 AM.Base = Value; 391 else 392 AM.Index = Value; 393 } 394 395 // The base or index of AM is equivalent to Value + ADJDYNALLOC, 396 // where IsBase selects between the base and index. Try to fold the 397 // ADJDYNALLOC into AM. 398 static bool expandAdjDynAlloc(SystemZAddressingMode &AM, bool IsBase, 399 SDValue Value) { 400 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) { 401 changeComponent(AM, IsBase, Value); 402 AM.IncludesDynAlloc = true; 403 return true; 404 } 405 return false; 406 } 407 408 // The base of AM is equivalent to Base + Index. Try to use Index as 409 // the index register. 410 static bool expandIndex(SystemZAddressingMode &AM, SDValue Base, 411 SDValue Index) { 412 if (AM.hasIndexField() && !AM.Index.getNode()) { 413 AM.Base = Base; 414 AM.Index = Index; 415 return true; 416 } 417 return false; 418 } 419 420 // The base or index of AM is equivalent to Op0 + Op1, where IsBase selects 421 // between the base and index. Try to fold Op1 into AM's displacement. 422 static bool expandDisp(SystemZAddressingMode &AM, bool IsBase, 423 SDValue Op0, uint64_t Op1) { 424 // First try adjusting the displacement. 425 int64_t TestDisp = AM.Disp + Op1; 426 if (selectDisp(AM.DR, TestDisp)) { 427 changeComponent(AM, IsBase, Op0); 428 AM.Disp = TestDisp; 429 return true; 430 } 431 432 // We could consider forcing the displacement into a register and 433 // using it as an index, but it would need to be carefully tuned. 434 return false; 435 } 436 437 bool SystemZDAGToDAGISel::expandAddress(SystemZAddressingMode &AM, 438 bool IsBase) const { 439 SDValue N = IsBase ? AM.Base : AM.Index; 440 unsigned Opcode = N.getOpcode(); 441 if (Opcode == ISD::TRUNCATE) { 442 N = N.getOperand(0); 443 Opcode = N.getOpcode(); 444 } 445 if (Opcode == ISD::ADD || CurDAG->isBaseWithConstantOffset(N)) { 446 SDValue Op0 = N.getOperand(0); 447 SDValue Op1 = N.getOperand(1); 448 449 unsigned Op0Code = Op0->getOpcode(); 450 unsigned Op1Code = Op1->getOpcode(); 451 452 if (Op0Code == SystemZISD::ADJDYNALLOC) 453 return expandAdjDynAlloc(AM, IsBase, Op1); 454 if (Op1Code == SystemZISD::ADJDYNALLOC) 455 return expandAdjDynAlloc(AM, IsBase, Op0); 456 457 if (Op0Code == ISD::Constant) 458 return expandDisp(AM, IsBase, Op1, 459 cast<ConstantSDNode>(Op0)->getSExtValue()); 460 if (Op1Code == ISD::Constant) 461 return expandDisp(AM, IsBase, Op0, 462 cast<ConstantSDNode>(Op1)->getSExtValue()); 463 464 if (IsBase && expandIndex(AM, Op0, Op1)) 465 return true; 466 } 467 if (Opcode == SystemZISD::PCREL_OFFSET) { 468 SDValue Full = N.getOperand(0); 469 SDValue Base = N.getOperand(1); 470 SDValue Anchor = Base.getOperand(0); 471 uint64_t Offset = (cast<GlobalAddressSDNode>(Full)->getOffset() - 472 cast<GlobalAddressSDNode>(Anchor)->getOffset()); 473 return expandDisp(AM, IsBase, Base, Offset); 474 } 475 return false; 476 } 477 478 // Return true if an instruction with displacement range DR should be 479 // used for displacement value Val. selectDisp(DR, Val) must already hold. 480 static bool isValidDisp(SystemZAddressingMode::DispRange DR, int64_t Val) { 481 assert(selectDisp(DR, Val) && "Invalid displacement"); 482 switch (DR) { 483 case SystemZAddressingMode::Disp12Only: 484 case SystemZAddressingMode::Disp20Only: 485 case SystemZAddressingMode::Disp20Only128: 486 return true; 487 488 case SystemZAddressingMode::Disp12Pair: 489 // Use the other instruction if the displacement is too large. 490 return isUInt<12>(Val); 491 492 case SystemZAddressingMode::Disp20Pair: 493 // Use the other instruction if the displacement is small enough. 494 return !isUInt<12>(Val); 495 } 496 llvm_unreachable("Unhandled displacement range"); 497 } 498 499 // Return true if Base + Disp + Index should be performed by LA(Y). 500 static bool shouldUseLA(SDNode *Base, int64_t Disp, SDNode *Index) { 501 // Don't use LA(Y) for constants. 502 if (!Base) 503 return false; 504 505 // Always use LA(Y) for frame addresses, since we know that the destination 506 // register is almost always (perhaps always) going to be different from 507 // the frame register. 508 if (Base->getOpcode() == ISD::FrameIndex) 509 return true; 510 511 if (Disp) { 512 // Always use LA(Y) if there is a base, displacement and index. 513 if (Index) 514 return true; 515 516 // Always use LA if the displacement is small enough. It should always 517 // be no worse than AGHI (and better if it avoids a move). 518 if (isUInt<12>(Disp)) 519 return true; 520 521 // For similar reasons, always use LAY if the constant is too big for AGHI. 522 // LAY should be no worse than AGFI. 523 if (!isInt<16>(Disp)) 524 return true; 525 } else { 526 // Don't use LA for plain registers. 527 if (!Index) 528 return false; 529 530 // Don't use LA for plain addition if the index operand is only used 531 // once. It should be a natural two-operand addition in that case. 532 if (Index->hasOneUse()) 533 return false; 534 535 // Prefer addition if the second operation is sign-extended, in the 536 // hope of using AGF. 537 unsigned IndexOpcode = Index->getOpcode(); 538 if (IndexOpcode == ISD::SIGN_EXTEND || 539 IndexOpcode == ISD::SIGN_EXTEND_INREG) 540 return false; 541 } 542 543 // Don't use LA for two-operand addition if either operand is only 544 // used once. The addition instructions are better in that case. 545 if (Base->hasOneUse()) 546 return false; 547 548 return true; 549 } 550 551 // Return true if Addr is suitable for AM, updating AM if so. 552 bool SystemZDAGToDAGISel::selectAddress(SDValue Addr, 553 SystemZAddressingMode &AM) const { 554 // Start out assuming that the address will need to be loaded separately, 555 // then try to extend it as much as we can. 556 AM.Base = Addr; 557 558 // First try treating the address as a constant. 559 if (Addr.getOpcode() == ISD::Constant && 560 expandDisp(AM, true, SDValue(), 561 cast<ConstantSDNode>(Addr)->getSExtValue())) 562 ; 563 // Also see if it's a bare ADJDYNALLOC. 564 else if (Addr.getOpcode() == SystemZISD::ADJDYNALLOC && 565 expandAdjDynAlloc(AM, true, SDValue())) 566 ; 567 else 568 // Otherwise try expanding each component. 569 while (expandAddress(AM, true) || 570 (AM.Index.getNode() && expandAddress(AM, false))) 571 continue; 572 573 // Reject cases where it isn't profitable to use LA(Y). 574 if (AM.Form == SystemZAddressingMode::FormBDXLA && 575 !shouldUseLA(AM.Base.getNode(), AM.Disp, AM.Index.getNode())) 576 return false; 577 578 // Reject cases where the other instruction in a pair should be used. 579 if (!isValidDisp(AM.DR, AM.Disp)) 580 return false; 581 582 // Make sure that ADJDYNALLOC is included where necessary. 583 if (AM.isDynAlloc() && !AM.IncludesDynAlloc) 584 return false; 585 586 DEBUG(AM.dump()); 587 return true; 588 } 589 590 // Insert a node into the DAG at least before Pos. This will reposition 591 // the node as needed, and will assign it a node ID that is <= Pos's ID. 592 // Note that this does *not* preserve the uniqueness of node IDs! 593 // The selection DAG must no longer depend on their uniqueness when this 594 // function is used. 595 static void insertDAGNode(SelectionDAG *DAG, SDNode *Pos, SDValue N) { 596 if (N->getNodeId() == -1 || 597 (SelectionDAGISel::getUninvalidatedNodeId(N.getNode()) > 598 SelectionDAGISel::getUninvalidatedNodeId(Pos))) { 599 DAG->RepositionNode(Pos->getIterator(), N.getNode()); 600 // Mark Node as invalid for pruning as after this it may be a successor to a 601 // selected node but otherwise be in the same position of Pos. 602 // Conservatively mark it with the same -abs(Id) to assure node id 603 // invariant is preserved. 604 N->setNodeId(Pos->getNodeId()); 605 SelectionDAGISel::InvalidateNodeId(N.getNode()); 606 } 607 } 608 609 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, 610 EVT VT, SDValue &Base, 611 SDValue &Disp) const { 612 Base = AM.Base; 613 if (!Base.getNode()) 614 // Register 0 means "no base". This is mostly useful for shifts. 615 Base = CurDAG->getRegister(0, VT); 616 else if (Base.getOpcode() == ISD::FrameIndex) { 617 // Lower a FrameIndex to a TargetFrameIndex. 618 int64_t FrameIndex = cast<FrameIndexSDNode>(Base)->getIndex(); 619 Base = CurDAG->getTargetFrameIndex(FrameIndex, VT); 620 } else if (Base.getValueType() != VT) { 621 // Truncate values from i64 to i32, for shifts. 622 assert(VT == MVT::i32 && Base.getValueType() == MVT::i64 && 623 "Unexpected truncation"); 624 SDLoc DL(Base); 625 SDValue Trunc = CurDAG->getNode(ISD::TRUNCATE, DL, VT, Base); 626 insertDAGNode(CurDAG, Base.getNode(), Trunc); 627 Base = Trunc; 628 } 629 630 // Lower the displacement to a TargetConstant. 631 Disp = CurDAG->getTargetConstant(AM.Disp, SDLoc(Base), VT); 632 } 633 634 void SystemZDAGToDAGISel::getAddressOperands(const SystemZAddressingMode &AM, 635 EVT VT, SDValue &Base, 636 SDValue &Disp, 637 SDValue &Index) const { 638 getAddressOperands(AM, VT, Base, Disp); 639 640 Index = AM.Index; 641 if (!Index.getNode()) 642 // Register 0 means "no index". 643 Index = CurDAG->getRegister(0, VT); 644 } 645 646 bool SystemZDAGToDAGISel::selectBDAddr(SystemZAddressingMode::DispRange DR, 647 SDValue Addr, SDValue &Base, 648 SDValue &Disp) const { 649 SystemZAddressingMode AM(SystemZAddressingMode::FormBD, DR); 650 if (!selectAddress(Addr, AM)) 651 return false; 652 653 getAddressOperands(AM, Addr.getValueType(), Base, Disp); 654 return true; 655 } 656 657 bool SystemZDAGToDAGISel::selectMVIAddr(SystemZAddressingMode::DispRange DR, 658 SDValue Addr, SDValue &Base, 659 SDValue &Disp) const { 660 SystemZAddressingMode AM(SystemZAddressingMode::FormBDXNormal, DR); 661 if (!selectAddress(Addr, AM) || AM.Index.getNode()) 662 return false; 663 664 getAddressOperands(AM, Addr.getValueType(), Base, Disp); 665 return true; 666 } 667 668 bool SystemZDAGToDAGISel::selectBDXAddr(SystemZAddressingMode::AddrForm Form, 669 SystemZAddressingMode::DispRange DR, 670 SDValue Addr, SDValue &Base, 671 SDValue &Disp, SDValue &Index) const { 672 SystemZAddressingMode AM(Form, DR); 673 if (!selectAddress(Addr, AM)) 674 return false; 675 676 getAddressOperands(AM, Addr.getValueType(), Base, Disp, Index); 677 return true; 678 } 679 680 bool SystemZDAGToDAGISel::selectBDVAddr12Only(SDValue Addr, SDValue Elem, 681 SDValue &Base, 682 SDValue &Disp, 683 SDValue &Index) const { 684 SDValue Regs[2]; 685 if (selectBDXAddr12Only(Addr, Regs[0], Disp, Regs[1]) && 686 Regs[0].getNode() && Regs[1].getNode()) { 687 for (unsigned int I = 0; I < 2; ++I) { 688 Base = Regs[I]; 689 Index = Regs[1 - I]; 690 // We can't tell here whether the index vector has the right type 691 // for the access; the caller needs to do that instead. 692 if (Index.getOpcode() == ISD::ZERO_EXTEND) 693 Index = Index.getOperand(0); 694 if (Index.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 695 Index.getOperand(1) == Elem) { 696 Index = Index.getOperand(0); 697 return true; 698 } 699 } 700 } 701 return false; 702 } 703 704 bool SystemZDAGToDAGISel::detectOrAndInsertion(SDValue &Op, 705 uint64_t InsertMask) const { 706 // We're only interested in cases where the insertion is into some operand 707 // of Op, rather than into Op itself. The only useful case is an AND. 708 if (Op.getOpcode() != ISD::AND) 709 return false; 710 711 // We need a constant mask. 712 auto *MaskNode = dyn_cast<ConstantSDNode>(Op.getOperand(1).getNode()); 713 if (!MaskNode) 714 return false; 715 716 // It's not an insertion of Op.getOperand(0) if the two masks overlap. 717 uint64_t AndMask = MaskNode->getZExtValue(); 718 if (InsertMask & AndMask) 719 return false; 720 721 // It's only an insertion if all bits are covered or are known to be zero. 722 // The inner check covers all cases but is more expensive. 723 uint64_t Used = allOnes(Op.getValueSizeInBits()); 724 if (Used != (AndMask | InsertMask)) { 725 KnownBits Known; 726 CurDAG->computeKnownBits(Op.getOperand(0), Known); 727 if (Used != (AndMask | InsertMask | Known.Zero.getZExtValue())) 728 return false; 729 } 730 731 Op = Op.getOperand(0); 732 return true; 733 } 734 735 bool SystemZDAGToDAGISel::refineRxSBGMask(RxSBGOperands &RxSBG, 736 uint64_t Mask) const { 737 const SystemZInstrInfo *TII = getInstrInfo(); 738 if (RxSBG.Rotate != 0) 739 Mask = (Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate)); 740 Mask &= RxSBG.Mask; 741 if (TII->isRxSBGMask(Mask, RxSBG.BitSize, RxSBG.Start, RxSBG.End)) { 742 RxSBG.Mask = Mask; 743 return true; 744 } 745 return false; 746 } 747 748 // Return true if any bits of (RxSBG.Input & Mask) are significant. 749 static bool maskMatters(RxSBGOperands &RxSBG, uint64_t Mask) { 750 // Rotate the mask in the same way as RxSBG.Input is rotated. 751 if (RxSBG.Rotate != 0) 752 Mask = ((Mask << RxSBG.Rotate) | (Mask >> (64 - RxSBG.Rotate))); 753 return (Mask & RxSBG.Mask) != 0; 754 } 755 756 bool SystemZDAGToDAGISel::expandRxSBG(RxSBGOperands &RxSBG) const { 757 SDValue N = RxSBG.Input; 758 unsigned Opcode = N.getOpcode(); 759 switch (Opcode) { 760 case ISD::TRUNCATE: { 761 if (RxSBG.Opcode == SystemZ::RNSBG) 762 return false; 763 uint64_t BitSize = N.getValueSizeInBits(); 764 uint64_t Mask = allOnes(BitSize); 765 if (!refineRxSBGMask(RxSBG, Mask)) 766 return false; 767 RxSBG.Input = N.getOperand(0); 768 return true; 769 } 770 case ISD::AND: { 771 if (RxSBG.Opcode == SystemZ::RNSBG) 772 return false; 773 774 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 775 if (!MaskNode) 776 return false; 777 778 SDValue Input = N.getOperand(0); 779 uint64_t Mask = MaskNode->getZExtValue(); 780 if (!refineRxSBGMask(RxSBG, Mask)) { 781 // If some bits of Input are already known zeros, those bits will have 782 // been removed from the mask. See if adding them back in makes the 783 // mask suitable. 784 KnownBits Known; 785 CurDAG->computeKnownBits(Input, Known); 786 Mask |= Known.Zero.getZExtValue(); 787 if (!refineRxSBGMask(RxSBG, Mask)) 788 return false; 789 } 790 RxSBG.Input = Input; 791 return true; 792 } 793 794 case ISD::OR: { 795 if (RxSBG.Opcode != SystemZ::RNSBG) 796 return false; 797 798 auto *MaskNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 799 if (!MaskNode) 800 return false; 801 802 SDValue Input = N.getOperand(0); 803 uint64_t Mask = ~MaskNode->getZExtValue(); 804 if (!refineRxSBGMask(RxSBG, Mask)) { 805 // If some bits of Input are already known ones, those bits will have 806 // been removed from the mask. See if adding them back in makes the 807 // mask suitable. 808 KnownBits Known; 809 CurDAG->computeKnownBits(Input, Known); 810 Mask &= ~Known.One.getZExtValue(); 811 if (!refineRxSBGMask(RxSBG, Mask)) 812 return false; 813 } 814 RxSBG.Input = Input; 815 return true; 816 } 817 818 case ISD::ROTL: { 819 // Any 64-bit rotate left can be merged into the RxSBG. 820 if (RxSBG.BitSize != 64 || N.getValueType() != MVT::i64) 821 return false; 822 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 823 if (!CountNode) 824 return false; 825 826 RxSBG.Rotate = (RxSBG.Rotate + CountNode->getZExtValue()) & 63; 827 RxSBG.Input = N.getOperand(0); 828 return true; 829 } 830 831 case ISD::ANY_EXTEND: 832 // Bits above the extended operand are don't-care. 833 RxSBG.Input = N.getOperand(0); 834 return true; 835 836 case ISD::ZERO_EXTEND: 837 if (RxSBG.Opcode != SystemZ::RNSBG) { 838 // Restrict the mask to the extended operand. 839 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits(); 840 if (!refineRxSBGMask(RxSBG, allOnes(InnerBitSize))) 841 return false; 842 843 RxSBG.Input = N.getOperand(0); 844 return true; 845 } 846 LLVM_FALLTHROUGH; 847 848 case ISD::SIGN_EXTEND: { 849 // Check that the extension bits are don't-care (i.e. are masked out 850 // by the final mask). 851 unsigned BitSize = N.getValueSizeInBits(); 852 unsigned InnerBitSize = N.getOperand(0).getValueSizeInBits(); 853 if (maskMatters(RxSBG, allOnes(BitSize) - allOnes(InnerBitSize))) { 854 // In the case where only the sign bit is active, increase Rotate with 855 // the extension width. 856 if (RxSBG.Mask == 1 && RxSBG.Rotate == 1) 857 RxSBG.Rotate += (BitSize - InnerBitSize); 858 else 859 return false; 860 } 861 862 RxSBG.Input = N.getOperand(0); 863 return true; 864 } 865 866 case ISD::SHL: { 867 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 868 if (!CountNode) 869 return false; 870 871 uint64_t Count = CountNode->getZExtValue(); 872 unsigned BitSize = N.getValueSizeInBits(); 873 if (Count < 1 || Count >= BitSize) 874 return false; 875 876 if (RxSBG.Opcode == SystemZ::RNSBG) { 877 // Treat (shl X, count) as (rotl X, size-count) as long as the bottom 878 // count bits from RxSBG.Input are ignored. 879 if (maskMatters(RxSBG, allOnes(Count))) 880 return false; 881 } else { 882 // Treat (shl X, count) as (and (rotl X, count), ~0<<count). 883 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count) << Count)) 884 return false; 885 } 886 887 RxSBG.Rotate = (RxSBG.Rotate + Count) & 63; 888 RxSBG.Input = N.getOperand(0); 889 return true; 890 } 891 892 case ISD::SRL: 893 case ISD::SRA: { 894 auto *CountNode = dyn_cast<ConstantSDNode>(N.getOperand(1).getNode()); 895 if (!CountNode) 896 return false; 897 898 uint64_t Count = CountNode->getZExtValue(); 899 unsigned BitSize = N.getValueSizeInBits(); 900 if (Count < 1 || Count >= BitSize) 901 return false; 902 903 if (RxSBG.Opcode == SystemZ::RNSBG || Opcode == ISD::SRA) { 904 // Treat (srl|sra X, count) as (rotl X, size-count) as long as the top 905 // count bits from RxSBG.Input are ignored. 906 if (maskMatters(RxSBG, allOnes(Count) << (BitSize - Count))) 907 return false; 908 } else { 909 // Treat (srl X, count), mask) as (and (rotl X, size-count), ~0>>count), 910 // which is similar to SLL above. 911 if (!refineRxSBGMask(RxSBG, allOnes(BitSize - Count))) 912 return false; 913 } 914 915 RxSBG.Rotate = (RxSBG.Rotate - Count) & 63; 916 RxSBG.Input = N.getOperand(0); 917 return true; 918 } 919 default: 920 return false; 921 } 922 } 923 924 SDValue SystemZDAGToDAGISel::getUNDEF(const SDLoc &DL, EVT VT) const { 925 SDNode *N = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, DL, VT); 926 return SDValue(N, 0); 927 } 928 929 SDValue SystemZDAGToDAGISel::convertTo(const SDLoc &DL, EVT VT, 930 SDValue N) const { 931 if (N.getValueType() == MVT::i32 && VT == MVT::i64) 932 return CurDAG->getTargetInsertSubreg(SystemZ::subreg_l32, 933 DL, VT, getUNDEF(DL, MVT::i64), N); 934 if (N.getValueType() == MVT::i64 && VT == MVT::i32) 935 return CurDAG->getTargetExtractSubreg(SystemZ::subreg_l32, DL, VT, N); 936 assert(N.getValueType() == VT && "Unexpected value types"); 937 return N; 938 } 939 940 bool SystemZDAGToDAGISel::tryRISBGZero(SDNode *N) { 941 SDLoc DL(N); 942 EVT VT = N->getValueType(0); 943 if (!VT.isInteger() || VT.getSizeInBits() > 64) 944 return false; 945 RxSBGOperands RISBG(SystemZ::RISBG, SDValue(N, 0)); 946 unsigned Count = 0; 947 while (expandRxSBG(RISBG)) 948 // The widening or narrowing is expected to be free. 949 // Counting widening or narrowing as a saved operation will result in 950 // preferring an R*SBG over a simple shift/logical instruction. 951 if (RISBG.Input.getOpcode() != ISD::ANY_EXTEND && 952 RISBG.Input.getOpcode() != ISD::TRUNCATE) 953 Count += 1; 954 if (Count == 0) 955 return false; 956 957 // Prefer to use normal shift instructions over RISBG, since they can handle 958 // all cases and are sometimes shorter. 959 if (Count == 1 && N->getOpcode() != ISD::AND) 960 return false; 961 962 // Prefer register extensions like LLC over RISBG. Also prefer to start 963 // out with normal ANDs if one instruction would be enough. We can convert 964 // these ANDs into an RISBG later if a three-address instruction is useful. 965 if (RISBG.Rotate == 0) { 966 bool PreferAnd = false; 967 // Prefer AND for any 32-bit and-immediate operation. 968 if (VT == MVT::i32) 969 PreferAnd = true; 970 // As well as for any 64-bit operation that can be implemented via LLC(R), 971 // LLH(R), LLGT(R), or one of the and-immediate instructions. 972 else if (RISBG.Mask == 0xff || 973 RISBG.Mask == 0xffff || 974 RISBG.Mask == 0x7fffffff || 975 SystemZ::isImmLF(~RISBG.Mask) || 976 SystemZ::isImmHF(~RISBG.Mask)) 977 PreferAnd = true; 978 // And likewise for the LLZRGF instruction, which doesn't have a register 979 // to register version. 980 else if (auto *Load = dyn_cast<LoadSDNode>(RISBG.Input)) { 981 if (Load->getMemoryVT() == MVT::i32 && 982 (Load->getExtensionType() == ISD::EXTLOAD || 983 Load->getExtensionType() == ISD::ZEXTLOAD) && 984 RISBG.Mask == 0xffffff00 && 985 Subtarget->hasLoadAndZeroRightmostByte()) 986 PreferAnd = true; 987 } 988 if (PreferAnd) { 989 // Replace the current node with an AND. Note that the current node 990 // might already be that same AND, in which case it is already CSE'd 991 // with it, and we must not call ReplaceNode. 992 SDValue In = convertTo(DL, VT, RISBG.Input); 993 SDValue Mask = CurDAG->getConstant(RISBG.Mask, DL, VT); 994 SDValue New = CurDAG->getNode(ISD::AND, DL, VT, In, Mask); 995 if (N != New.getNode()) { 996 insertDAGNode(CurDAG, N, Mask); 997 insertDAGNode(CurDAG, N, New); 998 ReplaceNode(N, New.getNode()); 999 N = New.getNode(); 1000 } 1001 // Now, select the machine opcode to implement this operation. 1002 if (!N->isMachineOpcode()) 1003 SelectCode(N); 1004 return true; 1005 } 1006 } 1007 1008 unsigned Opcode = SystemZ::RISBG; 1009 // Prefer RISBGN if available, since it does not clobber CC. 1010 if (Subtarget->hasMiscellaneousExtensions()) 1011 Opcode = SystemZ::RISBGN; 1012 EVT OpcodeVT = MVT::i64; 1013 if (VT == MVT::i32 && Subtarget->hasHighWord() && 1014 // We can only use the 32-bit instructions if all source bits are 1015 // in the low 32 bits without wrapping, both after rotation (because 1016 // of the smaller range for Start and End) and before rotation 1017 // (because the input value is truncated). 1018 RISBG.Start >= 32 && RISBG.End >= RISBG.Start && 1019 ((RISBG.Start + RISBG.Rotate) & 63) >= 32 && 1020 ((RISBG.End + RISBG.Rotate) & 63) >= 1021 ((RISBG.Start + RISBG.Rotate) & 63)) { 1022 Opcode = SystemZ::RISBMux; 1023 OpcodeVT = MVT::i32; 1024 RISBG.Start &= 31; 1025 RISBG.End &= 31; 1026 } 1027 SDValue Ops[5] = { 1028 getUNDEF(DL, OpcodeVT), 1029 convertTo(DL, OpcodeVT, RISBG.Input), 1030 CurDAG->getTargetConstant(RISBG.Start, DL, MVT::i32), 1031 CurDAG->getTargetConstant(RISBG.End | 128, DL, MVT::i32), 1032 CurDAG->getTargetConstant(RISBG.Rotate, DL, MVT::i32) 1033 }; 1034 SDValue New = convertTo( 1035 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, OpcodeVT, Ops), 0)); 1036 ReplaceNode(N, New.getNode()); 1037 return true; 1038 } 1039 1040 bool SystemZDAGToDAGISel::tryRxSBG(SDNode *N, unsigned Opcode) { 1041 SDLoc DL(N); 1042 EVT VT = N->getValueType(0); 1043 if (!VT.isInteger() || VT.getSizeInBits() > 64) 1044 return false; 1045 // Try treating each operand of N as the second operand of the RxSBG 1046 // and see which goes deepest. 1047 RxSBGOperands RxSBG[] = { 1048 RxSBGOperands(Opcode, N->getOperand(0)), 1049 RxSBGOperands(Opcode, N->getOperand(1)) 1050 }; 1051 unsigned Count[] = { 0, 0 }; 1052 for (unsigned I = 0; I < 2; ++I) 1053 while (expandRxSBG(RxSBG[I])) 1054 // The widening or narrowing is expected to be free. 1055 // Counting widening or narrowing as a saved operation will result in 1056 // preferring an R*SBG over a simple shift/logical instruction. 1057 if (RxSBG[I].Input.getOpcode() != ISD::ANY_EXTEND && 1058 RxSBG[I].Input.getOpcode() != ISD::TRUNCATE) 1059 Count[I] += 1; 1060 1061 // Do nothing if neither operand is suitable. 1062 if (Count[0] == 0 && Count[1] == 0) 1063 return false; 1064 1065 // Pick the deepest second operand. 1066 unsigned I = Count[0] > Count[1] ? 0 : 1; 1067 SDValue Op0 = N->getOperand(I ^ 1); 1068 1069 // Prefer IC for character insertions from memory. 1070 if (Opcode == SystemZ::ROSBG && (RxSBG[I].Mask & 0xff) == 0) 1071 if (auto *Load = dyn_cast<LoadSDNode>(Op0.getNode())) 1072 if (Load->getMemoryVT() == MVT::i8) 1073 return false; 1074 1075 // See whether we can avoid an AND in the first operand by converting 1076 // ROSBG to RISBG. 1077 if (Opcode == SystemZ::ROSBG && detectOrAndInsertion(Op0, RxSBG[I].Mask)) { 1078 Opcode = SystemZ::RISBG; 1079 // Prefer RISBGN if available, since it does not clobber CC. 1080 if (Subtarget->hasMiscellaneousExtensions()) 1081 Opcode = SystemZ::RISBGN; 1082 } 1083 1084 SDValue Ops[5] = { 1085 convertTo(DL, MVT::i64, Op0), 1086 convertTo(DL, MVT::i64, RxSBG[I].Input), 1087 CurDAG->getTargetConstant(RxSBG[I].Start, DL, MVT::i32), 1088 CurDAG->getTargetConstant(RxSBG[I].End, DL, MVT::i32), 1089 CurDAG->getTargetConstant(RxSBG[I].Rotate, DL, MVT::i32) 1090 }; 1091 SDValue New = convertTo( 1092 DL, VT, SDValue(CurDAG->getMachineNode(Opcode, DL, MVT::i64, Ops), 0)); 1093 ReplaceNode(N, New.getNode()); 1094 return true; 1095 } 1096 1097 void SystemZDAGToDAGISel::splitLargeImmediate(unsigned Opcode, SDNode *Node, 1098 SDValue Op0, uint64_t UpperVal, 1099 uint64_t LowerVal) { 1100 EVT VT = Node->getValueType(0); 1101 SDLoc DL(Node); 1102 SDValue Upper = CurDAG->getConstant(UpperVal, DL, VT); 1103 if (Op0.getNode()) 1104 Upper = CurDAG->getNode(Opcode, DL, VT, Op0, Upper); 1105 1106 { 1107 // When we haven't passed in Op0, Upper will be a constant. In order to 1108 // prevent folding back to the large immediate in `Or = getNode(...)` we run 1109 // SelectCode first and end up with an opaque machine node. This means that 1110 // we need to use a handle to keep track of Upper in case it gets CSE'd by 1111 // SelectCode. 1112 // 1113 // Note that in the case where Op0 is passed in we could just call 1114 // SelectCode(Upper) later, along with the SelectCode(Or), and avoid needing 1115 // the handle at all, but it's fine to do it here. 1116 // 1117 // TODO: This is a pretty hacky way to do this. Can we do something that 1118 // doesn't require a two paragraph explanation? 1119 HandleSDNode Handle(Upper); 1120 SelectCode(Upper.getNode()); 1121 Upper = Handle.getValue(); 1122 } 1123 1124 SDValue Lower = CurDAG->getConstant(LowerVal, DL, VT); 1125 SDValue Or = CurDAG->getNode(Opcode, DL, VT, Upper, Lower); 1126 1127 ReplaceNode(Node, Or.getNode()); 1128 1129 SelectCode(Or.getNode()); 1130 } 1131 1132 bool SystemZDAGToDAGISel::tryGather(SDNode *N, unsigned Opcode) { 1133 SDValue ElemV = N->getOperand(2); 1134 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV); 1135 if (!ElemN) 1136 return false; 1137 1138 unsigned Elem = ElemN->getZExtValue(); 1139 EVT VT = N->getValueType(0); 1140 if (Elem >= VT.getVectorNumElements()) 1141 return false; 1142 1143 auto *Load = dyn_cast<LoadSDNode>(N->getOperand(1)); 1144 if (!Load || !Load->hasOneUse()) 1145 return false; 1146 if (Load->getMemoryVT().getSizeInBits() != 1147 Load->getValueType(0).getSizeInBits()) 1148 return false; 1149 1150 SDValue Base, Disp, Index; 1151 if (!selectBDVAddr12Only(Load->getBasePtr(), ElemV, Base, Disp, Index) || 1152 Index.getValueType() != VT.changeVectorElementTypeToInteger()) 1153 return false; 1154 1155 SDLoc DL(Load); 1156 SDValue Ops[] = { 1157 N->getOperand(0), Base, Disp, Index, 1158 CurDAG->getTargetConstant(Elem, DL, MVT::i32), Load->getChain() 1159 }; 1160 SDNode *Res = CurDAG->getMachineNode(Opcode, DL, VT, MVT::Other, Ops); 1161 ReplaceUses(SDValue(Load, 1), SDValue(Res, 1)); 1162 ReplaceNode(N, Res); 1163 return true; 1164 } 1165 1166 bool SystemZDAGToDAGISel::tryScatter(StoreSDNode *Store, unsigned Opcode) { 1167 SDValue Value = Store->getValue(); 1168 if (Value.getOpcode() != ISD::EXTRACT_VECTOR_ELT) 1169 return false; 1170 if (Store->getMemoryVT().getSizeInBits() != Value.getValueSizeInBits()) 1171 return false; 1172 1173 SDValue ElemV = Value.getOperand(1); 1174 auto *ElemN = dyn_cast<ConstantSDNode>(ElemV); 1175 if (!ElemN) 1176 return false; 1177 1178 SDValue Vec = Value.getOperand(0); 1179 EVT VT = Vec.getValueType(); 1180 unsigned Elem = ElemN->getZExtValue(); 1181 if (Elem >= VT.getVectorNumElements()) 1182 return false; 1183 1184 SDValue Base, Disp, Index; 1185 if (!selectBDVAddr12Only(Store->getBasePtr(), ElemV, Base, Disp, Index) || 1186 Index.getValueType() != VT.changeVectorElementTypeToInteger()) 1187 return false; 1188 1189 SDLoc DL(Store); 1190 SDValue Ops[] = { 1191 Vec, Base, Disp, Index, CurDAG->getTargetConstant(Elem, DL, MVT::i32), 1192 Store->getChain() 1193 }; 1194 ReplaceNode(Store, CurDAG->getMachineNode(Opcode, DL, MVT::Other, Ops)); 1195 return true; 1196 } 1197 1198 bool SystemZDAGToDAGISel::canUseBlockOperation(StoreSDNode *Store, 1199 LoadSDNode *Load) const { 1200 // Check that the two memory operands have the same size. 1201 if (Load->getMemoryVT() != Store->getMemoryVT()) 1202 return false; 1203 1204 // Volatility stops an access from being decomposed. 1205 if (Load->isVolatile() || Store->isVolatile()) 1206 return false; 1207 1208 // There's no chance of overlap if the load is invariant. 1209 if (Load->isInvariant() && Load->isDereferenceable()) 1210 return true; 1211 1212 // Otherwise we need to check whether there's an alias. 1213 const Value *V1 = Load->getMemOperand()->getValue(); 1214 const Value *V2 = Store->getMemOperand()->getValue(); 1215 if (!V1 || !V2) 1216 return false; 1217 1218 // Reject equality. 1219 uint64_t Size = Load->getMemoryVT().getStoreSize(); 1220 int64_t End1 = Load->getSrcValueOffset() + Size; 1221 int64_t End2 = Store->getSrcValueOffset() + Size; 1222 if (V1 == V2 && End1 == End2) 1223 return false; 1224 1225 return !AA->alias(MemoryLocation(V1, End1, Load->getAAInfo()), 1226 MemoryLocation(V2, End2, Store->getAAInfo())); 1227 } 1228 1229 bool SystemZDAGToDAGISel::storeLoadCanUseMVC(SDNode *N) const { 1230 auto *Store = cast<StoreSDNode>(N); 1231 auto *Load = cast<LoadSDNode>(Store->getValue()); 1232 1233 // Prefer not to use MVC if either address can use ... RELATIVE LONG 1234 // instructions. 1235 uint64_t Size = Load->getMemoryVT().getStoreSize(); 1236 if (Size > 1 && Size <= 8) { 1237 // Prefer LHRL, LRL and LGRL. 1238 if (SystemZISD::isPCREL(Load->getBasePtr().getOpcode())) 1239 return false; 1240 // Prefer STHRL, STRL and STGRL. 1241 if (SystemZISD::isPCREL(Store->getBasePtr().getOpcode())) 1242 return false; 1243 } 1244 1245 return canUseBlockOperation(Store, Load); 1246 } 1247 1248 bool SystemZDAGToDAGISel::storeLoadCanUseBlockBinary(SDNode *N, 1249 unsigned I) const { 1250 auto *StoreA = cast<StoreSDNode>(N); 1251 auto *LoadA = cast<LoadSDNode>(StoreA->getValue().getOperand(1 - I)); 1252 auto *LoadB = cast<LoadSDNode>(StoreA->getValue().getOperand(I)); 1253 return !LoadA->isVolatile() && canUseBlockOperation(StoreA, LoadB); 1254 } 1255 1256 void SystemZDAGToDAGISel::Select(SDNode *Node) { 1257 // If we have a custom node, we already have selected! 1258 if (Node->isMachineOpcode()) { 1259 DEBUG(errs() << "== "; Node->dump(CurDAG); errs() << "\n"); 1260 Node->setNodeId(-1); 1261 return; 1262 } 1263 1264 unsigned Opcode = Node->getOpcode(); 1265 switch (Opcode) { 1266 case ISD::OR: 1267 if (Node->getOperand(1).getOpcode() != ISD::Constant) 1268 if (tryRxSBG(Node, SystemZ::ROSBG)) 1269 return; 1270 goto or_xor; 1271 1272 case ISD::XOR: 1273 if (Node->getOperand(1).getOpcode() != ISD::Constant) 1274 if (tryRxSBG(Node, SystemZ::RXSBG)) 1275 return; 1276 // Fall through. 1277 or_xor: 1278 // If this is a 64-bit operation in which both 32-bit halves are nonzero, 1279 // split the operation into two. If both operands here happen to be 1280 // constant, leave this to common code to optimize. 1281 if (Node->getValueType(0) == MVT::i64 && 1282 Node->getOperand(0).getOpcode() != ISD::Constant) 1283 if (auto *Op1 = dyn_cast<ConstantSDNode>(Node->getOperand(1))) { 1284 uint64_t Val = Op1->getZExtValue(); 1285 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val)) { 1286 splitLargeImmediate(Opcode, Node, Node->getOperand(0), 1287 Val - uint32_t(Val), uint32_t(Val)); 1288 return; 1289 } 1290 } 1291 break; 1292 1293 case ISD::AND: 1294 if (Node->getOperand(1).getOpcode() != ISD::Constant) 1295 if (tryRxSBG(Node, SystemZ::RNSBG)) 1296 return; 1297 LLVM_FALLTHROUGH; 1298 case ISD::ROTL: 1299 case ISD::SHL: 1300 case ISD::SRL: 1301 case ISD::ZERO_EXTEND: 1302 if (tryRISBGZero(Node)) 1303 return; 1304 break; 1305 1306 case ISD::Constant: 1307 // If this is a 64-bit constant that is out of the range of LLILF, 1308 // LLIHF and LGFI, split it into two 32-bit pieces. 1309 if (Node->getValueType(0) == MVT::i64) { 1310 uint64_t Val = cast<ConstantSDNode>(Node)->getZExtValue(); 1311 if (!SystemZ::isImmLF(Val) && !SystemZ::isImmHF(Val) && !isInt<32>(Val)) { 1312 splitLargeImmediate(ISD::OR, Node, SDValue(), Val - uint32_t(Val), 1313 uint32_t(Val)); 1314 return; 1315 } 1316 } 1317 break; 1318 1319 case SystemZISD::SELECT_CCMASK: { 1320 SDValue Op0 = Node->getOperand(0); 1321 SDValue Op1 = Node->getOperand(1); 1322 // Prefer to put any load first, so that it can be matched as a 1323 // conditional load. Likewise for constants in range for LOCHI. 1324 if ((Op1.getOpcode() == ISD::LOAD && Op0.getOpcode() != ISD::LOAD) || 1325 (Subtarget->hasLoadStoreOnCond2() && 1326 Node->getValueType(0).isInteger() && 1327 Op1.getOpcode() == ISD::Constant && 1328 isInt<16>(cast<ConstantSDNode>(Op1)->getSExtValue()) && 1329 !(Op0.getOpcode() == ISD::Constant && 1330 isInt<16>(cast<ConstantSDNode>(Op0)->getSExtValue())))) { 1331 SDValue CCValid = Node->getOperand(2); 1332 SDValue CCMask = Node->getOperand(3); 1333 uint64_t ConstCCValid = 1334 cast<ConstantSDNode>(CCValid.getNode())->getZExtValue(); 1335 uint64_t ConstCCMask = 1336 cast<ConstantSDNode>(CCMask.getNode())->getZExtValue(); 1337 // Invert the condition. 1338 CCMask = CurDAG->getConstant(ConstCCValid ^ ConstCCMask, SDLoc(Node), 1339 CCMask.getValueType()); 1340 SDValue Op4 = Node->getOperand(4); 1341 Node = CurDAG->UpdateNodeOperands(Node, Op1, Op0, CCValid, CCMask, Op4); 1342 } 1343 break; 1344 } 1345 1346 case ISD::INSERT_VECTOR_ELT: { 1347 EVT VT = Node->getValueType(0); 1348 unsigned ElemBitSize = VT.getScalarSizeInBits(); 1349 if (ElemBitSize == 32) { 1350 if (tryGather(Node, SystemZ::VGEF)) 1351 return; 1352 } else if (ElemBitSize == 64) { 1353 if (tryGather(Node, SystemZ::VGEG)) 1354 return; 1355 } 1356 break; 1357 } 1358 1359 case ISD::STORE: { 1360 auto *Store = cast<StoreSDNode>(Node); 1361 unsigned ElemBitSize = Store->getValue().getValueSizeInBits(); 1362 if (ElemBitSize == 32) { 1363 if (tryScatter(Store, SystemZ::VSCEF)) 1364 return; 1365 } else if (ElemBitSize == 64) { 1366 if (tryScatter(Store, SystemZ::VSCEG)) 1367 return; 1368 } 1369 break; 1370 } 1371 } 1372 1373 SelectCode(Node); 1374 } 1375 1376 bool SystemZDAGToDAGISel:: 1377 SelectInlineAsmMemoryOperand(const SDValue &Op, 1378 unsigned ConstraintID, 1379 std::vector<SDValue> &OutOps) { 1380 SystemZAddressingMode::AddrForm Form; 1381 SystemZAddressingMode::DispRange DispRange; 1382 SDValue Base, Disp, Index; 1383 1384 switch(ConstraintID) { 1385 default: 1386 llvm_unreachable("Unexpected asm memory constraint"); 1387 case InlineAsm::Constraint_i: 1388 case InlineAsm::Constraint_Q: 1389 // Accept an address with a short displacement, but no index. 1390 Form = SystemZAddressingMode::FormBD; 1391 DispRange = SystemZAddressingMode::Disp12Only; 1392 break; 1393 case InlineAsm::Constraint_R: 1394 // Accept an address with a short displacement and an index. 1395 Form = SystemZAddressingMode::FormBDXNormal; 1396 DispRange = SystemZAddressingMode::Disp12Only; 1397 break; 1398 case InlineAsm::Constraint_S: 1399 // Accept an address with a long displacement, but no index. 1400 Form = SystemZAddressingMode::FormBD; 1401 DispRange = SystemZAddressingMode::Disp20Only; 1402 break; 1403 case InlineAsm::Constraint_T: 1404 case InlineAsm::Constraint_m: 1405 case InlineAsm::Constraint_o: 1406 // Accept an address with a long displacement and an index. 1407 // m works the same as T, as this is the most general case. 1408 // We don't really have any special handling of "offsettable" 1409 // memory addresses, so just treat o the same as m. 1410 Form = SystemZAddressingMode::FormBDXNormal; 1411 DispRange = SystemZAddressingMode::Disp20Only; 1412 break; 1413 } 1414 1415 if (selectBDXAddr(Form, DispRange, Op, Base, Disp, Index)) { 1416 const TargetRegisterClass *TRC = 1417 Subtarget->getRegisterInfo()->getPointerRegClass(*MF); 1418 SDLoc DL(Base); 1419 SDValue RC = CurDAG->getTargetConstant(TRC->getID(), DL, MVT::i32); 1420 1421 // Make sure that the base address doesn't go into %r0. 1422 // If it's a TargetFrameIndex or a fixed register, we shouldn't do anything. 1423 if (Base.getOpcode() != ISD::TargetFrameIndex && 1424 Base.getOpcode() != ISD::Register) { 1425 Base = 1426 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, 1427 DL, Base.getValueType(), 1428 Base, RC), 0); 1429 } 1430 1431 // Make sure that the index register isn't assigned to %r0 either. 1432 if (Index.getOpcode() != ISD::Register) { 1433 Index = 1434 SDValue(CurDAG->getMachineNode(TargetOpcode::COPY_TO_REGCLASS, 1435 DL, Index.getValueType(), 1436 Index, RC), 0); 1437 } 1438 1439 OutOps.push_back(Base); 1440 OutOps.push_back(Disp); 1441 OutOps.push_back(Index); 1442 return false; 1443 } 1444 1445 return true; 1446 } 1447 1448 namespace { 1449 // Represents a sequence for extracting a 0/1 value from an IPM result: 1450 // (((X ^ XORValue) + AddValue) >> Bit) 1451 struct IPMConversion { 1452 IPMConversion(unsigned xorValue, int64_t addValue, unsigned bit) 1453 : XORValue(xorValue), AddValue(addValue), Bit(bit) {} 1454 1455 int64_t XORValue; 1456 int64_t AddValue; 1457 unsigned Bit; 1458 }; 1459 } // end anonymous namespace 1460 1461 // Return a sequence for getting a 1 from an IPM result when CC has a 1462 // value in CCMask and a 0 when CC has a value in CCValid & ~CCMask. 1463 // The handling of CC values outside CCValid doesn't matter. 1464 static IPMConversion getIPMConversion(unsigned CCValid, unsigned CCMask) { 1465 // Deal with cases where the result can be taken directly from a bit 1466 // of the IPM result. 1467 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_3))) 1468 return IPMConversion(0, 0, SystemZ::IPM_CC); 1469 if (CCMask == (CCValid & (SystemZ::CCMASK_2 | SystemZ::CCMASK_3))) 1470 return IPMConversion(0, 0, SystemZ::IPM_CC + 1); 1471 1472 // Deal with cases where we can add a value to force the sign bit 1473 // to contain the right value. Putting the bit in 31 means we can 1474 // use SRL rather than RISBG(L), and also makes it easier to get a 1475 // 0/-1 value, so it has priority over the other tests below. 1476 // 1477 // These sequences rely on the fact that the upper two bits of the 1478 // IPM result are zero. 1479 uint64_t TopBit = uint64_t(1) << 31; 1480 if (CCMask == (CCValid & SystemZ::CCMASK_0)) 1481 return IPMConversion(0, -(1 << SystemZ::IPM_CC), 31); 1482 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_1))) 1483 return IPMConversion(0, -(2 << SystemZ::IPM_CC), 31); 1484 if (CCMask == (CCValid & (SystemZ::CCMASK_0 1485 | SystemZ::CCMASK_1 1486 | SystemZ::CCMASK_2))) 1487 return IPMConversion(0, -(3 << SystemZ::IPM_CC), 31); 1488 if (CCMask == (CCValid & SystemZ::CCMASK_3)) 1489 return IPMConversion(0, TopBit - (3 << SystemZ::IPM_CC), 31); 1490 if (CCMask == (CCValid & (SystemZ::CCMASK_1 1491 | SystemZ::CCMASK_2 1492 | SystemZ::CCMASK_3))) 1493 return IPMConversion(0, TopBit - (1 << SystemZ::IPM_CC), 31); 1494 1495 // Next try inverting the value and testing a bit. 0/1 could be 1496 // handled this way too, but we dealt with that case above. 1497 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_2))) 1498 return IPMConversion(-1, 0, SystemZ::IPM_CC); 1499 1500 // Handle cases where adding a value forces a non-sign bit to contain 1501 // the right value. 1502 if (CCMask == (CCValid & (SystemZ::CCMASK_1 | SystemZ::CCMASK_2))) 1503 return IPMConversion(0, 1 << SystemZ::IPM_CC, SystemZ::IPM_CC + 1); 1504 if (CCMask == (CCValid & (SystemZ::CCMASK_0 | SystemZ::CCMASK_3))) 1505 return IPMConversion(0, -(1 << SystemZ::IPM_CC), SystemZ::IPM_CC + 1); 1506 1507 // The remaining cases are 1, 2, 0/1/3 and 0/2/3. All these are 1508 // can be done by inverting the low CC bit and applying one of the 1509 // sign-based extractions above. 1510 if (CCMask == (CCValid & SystemZ::CCMASK_1)) 1511 return IPMConversion(1 << SystemZ::IPM_CC, -(1 << SystemZ::IPM_CC), 31); 1512 if (CCMask == (CCValid & SystemZ::CCMASK_2)) 1513 return IPMConversion(1 << SystemZ::IPM_CC, 1514 TopBit - (3 << SystemZ::IPM_CC), 31); 1515 if (CCMask == (CCValid & (SystemZ::CCMASK_0 1516 | SystemZ::CCMASK_1 1517 | SystemZ::CCMASK_3))) 1518 return IPMConversion(1 << SystemZ::IPM_CC, -(3 << SystemZ::IPM_CC), 31); 1519 if (CCMask == (CCValid & (SystemZ::CCMASK_0 1520 | SystemZ::CCMASK_2 1521 | SystemZ::CCMASK_3))) 1522 return IPMConversion(1 << SystemZ::IPM_CC, 1523 TopBit - (1 << SystemZ::IPM_CC), 31); 1524 1525 llvm_unreachable("Unexpected CC combination"); 1526 } 1527 1528 SDValue SystemZDAGToDAGISel::expandSelectBoolean(SDNode *Node) { 1529 auto *TrueOp = dyn_cast<ConstantSDNode>(Node->getOperand(0)); 1530 auto *FalseOp = dyn_cast<ConstantSDNode>(Node->getOperand(1)); 1531 if (!TrueOp || !FalseOp) 1532 return SDValue(); 1533 if (FalseOp->getZExtValue() != 0) 1534 return SDValue(); 1535 if (TrueOp->getSExtValue() != 1 && TrueOp->getSExtValue() != -1) 1536 return SDValue(); 1537 1538 auto *CCValidOp = dyn_cast<ConstantSDNode>(Node->getOperand(2)); 1539 auto *CCMaskOp = dyn_cast<ConstantSDNode>(Node->getOperand(3)); 1540 if (!CCValidOp || !CCMaskOp) 1541 return SDValue(); 1542 int CCValid = CCValidOp->getZExtValue(); 1543 int CCMask = CCMaskOp->getZExtValue(); 1544 1545 SDLoc DL(Node); 1546 SDValue Glue = Node->getOperand(4); 1547 IPMConversion IPM = getIPMConversion(CCValid, CCMask); 1548 SDValue Result = CurDAG->getNode(SystemZISD::IPM, DL, MVT::i32, Glue); 1549 1550 if (IPM.XORValue) 1551 Result = CurDAG->getNode(ISD::XOR, DL, MVT::i32, Result, 1552 CurDAG->getConstant(IPM.XORValue, DL, MVT::i32)); 1553 1554 if (IPM.AddValue) 1555 Result = CurDAG->getNode(ISD::ADD, DL, MVT::i32, Result, 1556 CurDAG->getConstant(IPM.AddValue, DL, MVT::i32)); 1557 1558 EVT VT = Node->getValueType(0); 1559 if (VT == MVT::i32 && IPM.Bit == 31) { 1560 unsigned ShiftOp = TrueOp->getSExtValue() == 1 ? ISD::SRL : ISD::SRA; 1561 Result = CurDAG->getNode(ShiftOp, DL, MVT::i32, Result, 1562 CurDAG->getConstant(IPM.Bit, DL, MVT::i32)); 1563 } else { 1564 if (VT != MVT::i32) 1565 Result = CurDAG->getNode(ISD::ANY_EXTEND, DL, VT, Result); 1566 1567 if (TrueOp->getSExtValue() == 1) { 1568 // The SHR/AND sequence should get optimized to an RISBG. 1569 Result = CurDAG->getNode(ISD::SRL, DL, VT, Result, 1570 CurDAG->getConstant(IPM.Bit, DL, MVT::i32)); 1571 Result = CurDAG->getNode(ISD::AND, DL, VT, Result, 1572 CurDAG->getConstant(1, DL, VT)); 1573 } else { 1574 // Sign-extend from IPM.Bit using a pair of shifts. 1575 int ShlAmt = VT.getSizeInBits() - 1 - IPM.Bit; 1576 int SraAmt = VT.getSizeInBits() - 1; 1577 Result = CurDAG->getNode(ISD::SHL, DL, VT, Result, 1578 CurDAG->getConstant(ShlAmt, DL, MVT::i32)); 1579 Result = CurDAG->getNode(ISD::SRA, DL, VT, Result, 1580 CurDAG->getConstant(SraAmt, DL, MVT::i32)); 1581 } 1582 } 1583 1584 return Result; 1585 } 1586 1587 void SystemZDAGToDAGISel::PreprocessISelDAG() { 1588 // If we have conditional immediate loads, we always prefer 1589 // using those over an IPM sequence. 1590 if (Subtarget->hasLoadStoreOnCond2()) 1591 return; 1592 1593 bool MadeChange = false; 1594 1595 for (SelectionDAG::allnodes_iterator I = CurDAG->allnodes_begin(), 1596 E = CurDAG->allnodes_end(); 1597 I != E;) { 1598 SDNode *N = &*I++; 1599 if (N->use_empty()) 1600 continue; 1601 1602 SDValue Res; 1603 switch (N->getOpcode()) { 1604 default: break; 1605 case SystemZISD::SELECT_CCMASK: 1606 Res = expandSelectBoolean(N); 1607 break; 1608 } 1609 1610 if (Res) { 1611 DEBUG(dbgs() << "SystemZ DAG preprocessing replacing:\nOld: "); 1612 DEBUG(N->dump(CurDAG)); 1613 DEBUG(dbgs() << "\nNew: "); 1614 DEBUG(Res.getNode()->dump(CurDAG)); 1615 DEBUG(dbgs() << "\n"); 1616 1617 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), Res); 1618 MadeChange = true; 1619 } 1620 } 1621 1622 if (MadeChange) 1623 CurDAG->RemoveDeadNodes(); 1624 } 1625