1 //===-- AMDGPUISelDAGToDAG.cpp - A dag to dag inst selector for AMDGPU ----===// 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 /// \file 11 /// \brief Defines an instruction selector for the AMDGPU target. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "AMDGPUInstrInfo.h" 16 #include "AMDGPUIntrinsicInfo.h" 17 #include "AMDGPUISelLowering.h" // For AMDGPUISD 18 #include "AMDGPUSubtarget.h" 19 #include "SIISelLowering.h" 20 #include "SIMachineFunctionInfo.h" 21 #include "llvm/Analysis/ValueTracking.h" 22 #include "llvm/CodeGen/FunctionLoweringInfo.h" 23 #include "llvm/CodeGen/MachineFrameInfo.h" 24 #include "llvm/CodeGen/PseudoSourceValue.h" 25 #include "llvm/CodeGen/SelectionDAG.h" 26 #include "llvm/CodeGen/SelectionDAGISel.h" 27 #include "llvm/IR/DiagnosticInfo.h" 28 29 using namespace llvm; 30 31 namespace llvm { 32 class R600InstrInfo; 33 } 34 35 //===----------------------------------------------------------------------===// 36 // Instruction Selector Implementation 37 //===----------------------------------------------------------------------===// 38 39 namespace { 40 41 static bool isCBranchSCC(const SDNode *N) { 42 assert(N->getOpcode() == ISD::BRCOND); 43 if (!N->hasOneUse()) 44 return false; 45 46 SDValue Cond = N->getOperand(1); 47 if (Cond.getOpcode() == ISD::CopyToReg) 48 Cond = Cond.getOperand(2); 49 return Cond.getOpcode() == ISD::SETCC && 50 Cond.getOperand(0).getValueType() == MVT::i32 && Cond.hasOneUse(); 51 } 52 53 /// AMDGPU specific code to select AMDGPU machine instructions for 54 /// SelectionDAG operations. 55 class AMDGPUDAGToDAGISel : public SelectionDAGISel { 56 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can 57 // make the right decision when generating code for different targets. 58 const AMDGPUSubtarget *Subtarget; 59 60 public: 61 AMDGPUDAGToDAGISel(TargetMachine &TM); 62 virtual ~AMDGPUDAGToDAGISel(); 63 bool runOnMachineFunction(MachineFunction &MF) override; 64 void Select(SDNode *N) override; 65 const char *getPassName() const override; 66 void PreprocessISelDAG() override; 67 void PostprocessISelDAG() override; 68 69 private: 70 bool isInlineImmediate(SDNode *N) const; 71 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs, 72 const R600InstrInfo *TII); 73 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &); 74 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &); 75 76 static bool checkType(const Value *ptr, unsigned int addrspace); 77 78 static bool isGlobalStore(const MemSDNode *N); 79 static bool isFlatStore(const MemSDNode *N); 80 static bool isLocalStore(const StoreSDNode *N); 81 82 bool isConstantLoad(const MemSDNode *N, int cbID) const; 83 bool isGlobalLoad(const MemSDNode *N) const; 84 bool isFlatLoad(const MemSDNode *N) const; 85 bool isLocalLoad(const LoadSDNode *N) const; 86 87 bool isUniformBr(const SDNode *N) const; 88 89 SDNode *glueCopyToM0(SDNode *N) const; 90 91 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const; 92 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr); 93 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg, 94 SDValue& Offset); 95 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset); 96 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset); 97 bool isDSOffsetLegal(const SDValue &Base, unsigned Offset, 98 unsigned OffsetBits) const; 99 bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const; 100 bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0, 101 SDValue &Offset1) const; 102 bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, 103 SDValue &SOffset, SDValue &Offset, SDValue &Offen, 104 SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC, 105 SDValue &TFE) const; 106 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, 107 SDValue &SOffset, SDValue &Offset, SDValue &GLC, 108 SDValue &SLC, SDValue &TFE) const; 109 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 110 SDValue &VAddr, SDValue &SOffset, SDValue &Offset, 111 SDValue &SLC) const; 112 bool SelectMUBUFScratch(SDValue Addr, SDValue &RSrc, SDValue &VAddr, 113 SDValue &SOffset, SDValue &ImmOffset) const; 114 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset, 115 SDValue &Offset, SDValue &GLC, SDValue &SLC, 116 SDValue &TFE) const; 117 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset, 118 SDValue &Offset, SDValue &SLC) const; 119 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset, 120 SDValue &Offset) const; 121 bool SelectMUBUFConstant(SDValue Constant, 122 SDValue &SOffset, 123 SDValue &ImmOffset) const; 124 bool SelectMUBUFIntrinsicOffset(SDValue Offset, SDValue &SOffset, 125 SDValue &ImmOffset) const; 126 bool SelectMUBUFIntrinsicVOffset(SDValue Offset, SDValue &SOffset, 127 SDValue &ImmOffset, SDValue &VOffset) const; 128 129 bool SelectFlat(SDValue Addr, SDValue &VAddr, 130 SDValue &SLC, SDValue &TFE) const; 131 132 bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset, 133 bool &Imm) const; 134 bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset, 135 bool &Imm) const; 136 bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 137 bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 138 bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 139 bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const; 140 bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const; 141 bool SelectSMRDBufferSgpr(SDValue Addr, SDValue &Offset) const; 142 bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 143 bool SelectVOP3NoMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 144 bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods, 145 SDValue &Clamp, SDValue &Omod) const; 146 bool SelectVOP3NoMods0(SDValue In, SDValue &Src, SDValue &SrcMods, 147 SDValue &Clamp, SDValue &Omod) const; 148 149 bool SelectVOP3Mods0Clamp(SDValue In, SDValue &Src, SDValue &SrcMods, 150 SDValue &Omod) const; 151 bool SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, SDValue &SrcMods, 152 SDValue &Clamp, 153 SDValue &Omod) const; 154 155 void SelectADD_SUB_I64(SDNode *N); 156 void SelectDIV_SCALE(SDNode *N); 157 158 SDNode *getS_BFE(unsigned Opcode, const SDLoc &DL, SDValue Val, 159 uint32_t Offset, uint32_t Width); 160 void SelectS_BFEFromShifts(SDNode *N); 161 void SelectS_BFE(SDNode *N); 162 void SelectBRCOND(SDNode *N); 163 void SelectATOMIC_CMP_SWAP(SDNode *N); 164 165 // Include the pieces autogenerated from the target description. 166 #include "AMDGPUGenDAGISel.inc" 167 }; 168 } // end anonymous namespace 169 170 /// \brief This pass converts a legalized DAG into a AMDGPU-specific 171 // DAG, ready for instruction scheduling. 172 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine &TM) { 173 return new AMDGPUDAGToDAGISel(TM); 174 } 175 176 AMDGPUDAGToDAGISel::AMDGPUDAGToDAGISel(TargetMachine &TM) 177 : SelectionDAGISel(TM) {} 178 179 bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) { 180 Subtarget = &MF.getSubtarget<AMDGPUSubtarget>(); 181 return SelectionDAGISel::runOnMachineFunction(MF); 182 } 183 184 AMDGPUDAGToDAGISel::~AMDGPUDAGToDAGISel() { 185 } 186 187 bool AMDGPUDAGToDAGISel::isInlineImmediate(SDNode *N) const { 188 const SITargetLowering *TL 189 = static_cast<const SITargetLowering *>(getTargetLowering()); 190 return TL->analyzeImmediate(N) == 0; 191 } 192 193 /// \brief Determine the register class for \p OpNo 194 /// \returns The register class of the virtual register that will be used for 195 /// the given operand number \OpNo or NULL if the register class cannot be 196 /// determined. 197 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N, 198 unsigned OpNo) const { 199 if (!N->isMachineOpcode()) 200 return nullptr; 201 202 switch (N->getMachineOpcode()) { 203 default: { 204 const MCInstrDesc &Desc = 205 Subtarget->getInstrInfo()->get(N->getMachineOpcode()); 206 unsigned OpIdx = Desc.getNumDefs() + OpNo; 207 if (OpIdx >= Desc.getNumOperands()) 208 return nullptr; 209 int RegClass = Desc.OpInfo[OpIdx].RegClass; 210 if (RegClass == -1) 211 return nullptr; 212 213 return Subtarget->getRegisterInfo()->getRegClass(RegClass); 214 } 215 case AMDGPU::REG_SEQUENCE: { 216 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 217 const TargetRegisterClass *SuperRC = 218 Subtarget->getRegisterInfo()->getRegClass(RCID); 219 220 SDValue SubRegOp = N->getOperand(OpNo + 1); 221 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue(); 222 return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC, 223 SubRegIdx); 224 } 225 } 226 } 227 228 SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N) const { 229 if (Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS || 230 !checkType(cast<MemSDNode>(N)->getMemOperand()->getValue(), 231 AMDGPUAS::LOCAL_ADDRESS)) 232 return N; 233 234 const SITargetLowering& Lowering = 235 *static_cast<const SITargetLowering*>(getTargetLowering()); 236 237 // Write max value to m0 before each load operation 238 239 SDValue M0 = Lowering.copyToM0(*CurDAG, CurDAG->getEntryNode(), SDLoc(N), 240 CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32)); 241 242 SDValue Glue = M0.getValue(1); 243 244 SmallVector <SDValue, 8> Ops; 245 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 246 Ops.push_back(N->getOperand(i)); 247 } 248 Ops.push_back(Glue); 249 CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops); 250 251 return N; 252 } 253 254 static unsigned selectSGPRVectorRegClassID(unsigned NumVectorElts) { 255 switch (NumVectorElts) { 256 case 1: 257 return AMDGPU::SReg_32RegClassID; 258 case 2: 259 return AMDGPU::SReg_64RegClassID; 260 case 4: 261 return AMDGPU::SReg_128RegClassID; 262 case 8: 263 return AMDGPU::SReg_256RegClassID; 264 case 16: 265 return AMDGPU::SReg_512RegClassID; 266 } 267 268 llvm_unreachable("invalid vector size"); 269 } 270 271 void AMDGPUDAGToDAGISel::Select(SDNode *N) { 272 unsigned int Opc = N->getOpcode(); 273 if (N->isMachineOpcode()) { 274 N->setNodeId(-1); 275 return; // Already selected. 276 } 277 278 if (isa<AtomicSDNode>(N) || 279 (Opc == AMDGPUISD::ATOMIC_INC || Opc == AMDGPUISD::ATOMIC_DEC)) 280 N = glueCopyToM0(N); 281 282 switch (Opc) { 283 default: break; 284 // We are selecting i64 ADD here instead of custom lower it during 285 // DAG legalization, so we can fold some i64 ADDs used for address 286 // calculation into the LOAD and STORE instructions. 287 case ISD::ADD: 288 case ISD::SUB: { 289 if (N->getValueType(0) != MVT::i64 || 290 Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) 291 break; 292 293 SelectADD_SUB_I64(N); 294 return; 295 } 296 case ISD::SCALAR_TO_VECTOR: 297 case AMDGPUISD::BUILD_VERTICAL_VECTOR: 298 case ISD::BUILD_VECTOR: { 299 unsigned RegClassID; 300 const AMDGPURegisterInfo *TRI = Subtarget->getRegisterInfo(); 301 EVT VT = N->getValueType(0); 302 unsigned NumVectorElts = VT.getVectorNumElements(); 303 EVT EltVT = VT.getVectorElementType(); 304 assert(EltVT.bitsEq(MVT::i32)); 305 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SOUTHERN_ISLANDS) { 306 RegClassID = selectSGPRVectorRegClassID(NumVectorElts); 307 } else { 308 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG 309 // that adds a 128 bits reg copy when going through TwoAddressInstructions 310 // pass. We want to avoid 128 bits copies as much as possible because they 311 // can't be bundled by our scheduler. 312 switch(NumVectorElts) { 313 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break; 314 case 4: 315 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR) 316 RegClassID = AMDGPU::R600_Reg128VerticalRegClassID; 317 else 318 RegClassID = AMDGPU::R600_Reg128RegClassID; 319 break; 320 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR"); 321 } 322 } 323 324 SDLoc DL(N); 325 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); 326 327 if (NumVectorElts == 1) { 328 CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, N->getOperand(0), 329 RegClass); 330 return; 331 } 332 333 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not " 334 "supported yet"); 335 // 16 = Max Num Vector Elements 336 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index) 337 // 1 = Vector Register Class 338 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1); 339 340 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); 341 bool IsRegSeq = true; 342 unsigned NOps = N->getNumOperands(); 343 for (unsigned i = 0; i < NOps; i++) { 344 // XXX: Why is this here? 345 if (isa<RegisterSDNode>(N->getOperand(i))) { 346 IsRegSeq = false; 347 break; 348 } 349 RegSeqArgs[1 + (2 * i)] = N->getOperand(i); 350 RegSeqArgs[1 + (2 * i) + 1] = 351 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL, 352 MVT::i32); 353 } 354 355 if (NOps != NumVectorElts) { 356 // Fill in the missing undef elements if this was a scalar_to_vector. 357 assert(Opc == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts); 358 359 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, 360 DL, EltVT); 361 for (unsigned i = NOps; i < NumVectorElts; ++i) { 362 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0); 363 RegSeqArgs[1 + (2 * i) + 1] = 364 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL, MVT::i32); 365 } 366 } 367 368 if (!IsRegSeq) 369 break; 370 CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), RegSeqArgs); 371 return; 372 } 373 case ISD::BUILD_PAIR: { 374 SDValue RC, SubReg0, SubReg1; 375 if (Subtarget->getGeneration() <= AMDGPUSubtarget::NORTHERN_ISLANDS) { 376 break; 377 } 378 SDLoc DL(N); 379 if (N->getValueType(0) == MVT::i128) { 380 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32); 381 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32); 382 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32); 383 } else if (N->getValueType(0) == MVT::i64) { 384 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32); 385 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); 386 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); 387 } else { 388 llvm_unreachable("Unhandled value type for BUILD_PAIR"); 389 } 390 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0, 391 N->getOperand(1), SubReg1 }; 392 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, 393 N->getValueType(0), Ops)); 394 return; 395 } 396 397 case ISD::Constant: 398 case ISD::ConstantFP: { 399 if (Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS || 400 N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N)) 401 break; 402 403 uint64_t Imm; 404 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N)) 405 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue(); 406 else { 407 ConstantSDNode *C = cast<ConstantSDNode>(N); 408 Imm = C->getZExtValue(); 409 } 410 411 SDLoc DL(N); 412 SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 413 CurDAG->getConstant(Imm & 0xFFFFFFFF, DL, 414 MVT::i32)); 415 SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 416 CurDAG->getConstant(Imm >> 32, DL, MVT::i32)); 417 const SDValue Ops[] = { 418 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), 419 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 420 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32) 421 }; 422 423 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, 424 N->getValueType(0), Ops)); 425 return; 426 } 427 case ISD::LOAD: 428 case ISD::STORE: { 429 N = glueCopyToM0(N); 430 break; 431 } 432 433 case AMDGPUISD::BFE_I32: 434 case AMDGPUISD::BFE_U32: { 435 if (Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) 436 break; 437 438 // There is a scalar version available, but unlike the vector version which 439 // has a separate operand for the offset and width, the scalar version packs 440 // the width and offset into a single operand. Try to move to the scalar 441 // version if the offsets are constant, so that we can try to keep extended 442 // loads of kernel arguments in SGPRs. 443 444 // TODO: Technically we could try to pattern match scalar bitshifts of 445 // dynamic values, but it's probably not useful. 446 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 447 if (!Offset) 448 break; 449 450 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); 451 if (!Width) 452 break; 453 454 bool Signed = Opc == AMDGPUISD::BFE_I32; 455 456 uint32_t OffsetVal = Offset->getZExtValue(); 457 uint32_t WidthVal = Width->getZExtValue(); 458 459 ReplaceNode(N, getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32, 460 SDLoc(N), N->getOperand(0), OffsetVal, WidthVal)); 461 return; 462 } 463 case AMDGPUISD::DIV_SCALE: { 464 SelectDIV_SCALE(N); 465 return; 466 } 467 case ISD::CopyToReg: { 468 const SITargetLowering& Lowering = 469 *static_cast<const SITargetLowering*>(getTargetLowering()); 470 Lowering.legalizeTargetIndependentNode(N, *CurDAG); 471 break; 472 } 473 case ISD::AND: 474 case ISD::SRL: 475 case ISD::SRA: 476 case ISD::SIGN_EXTEND_INREG: 477 if (N->getValueType(0) != MVT::i32 || 478 Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) 479 break; 480 481 SelectS_BFE(N); 482 return; 483 case ISD::BRCOND: 484 SelectBRCOND(N); 485 return; 486 487 case AMDGPUISD::ATOMIC_CMP_SWAP: 488 SelectATOMIC_CMP_SWAP(N); 489 return; 490 } 491 492 SelectCode(N); 493 } 494 495 bool AMDGPUDAGToDAGISel::checkType(const Value *Ptr, unsigned AS) { 496 assert(AS != 0 && "Use checkPrivateAddress instead."); 497 if (!Ptr) 498 return false; 499 500 return Ptr->getType()->getPointerAddressSpace() == AS; 501 } 502 503 bool AMDGPUDAGToDAGISel::isGlobalStore(const MemSDNode *N) { 504 if (!N->writeMem()) 505 return false; 506 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS); 507 } 508 509 bool AMDGPUDAGToDAGISel::isLocalStore(const StoreSDNode *N) { 510 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS); 511 } 512 513 bool AMDGPUDAGToDAGISel::isFlatStore(const MemSDNode *N) { 514 if (!N->writeMem()) 515 return false; 516 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::FLAT_ADDRESS); 517 } 518 519 bool AMDGPUDAGToDAGISel::isConstantLoad(const MemSDNode *N, int CbId) const { 520 if (!N->readMem()) 521 return false; 522 const Value *MemVal = N->getMemOperand()->getValue(); 523 if (CbId == -1) 524 return checkType(MemVal, AMDGPUAS::CONSTANT_ADDRESS); 525 526 return checkType(MemVal, AMDGPUAS::CONSTANT_BUFFER_0 + CbId); 527 } 528 529 bool AMDGPUDAGToDAGISel::isGlobalLoad(const MemSDNode *N) const { 530 if (!N->readMem()) 531 return false; 532 if (N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS) { 533 if (Subtarget->getGeneration() < AMDGPUSubtarget::SOUTHERN_ISLANDS) 534 return !isa<GlobalValue>(GetUnderlyingObject( 535 N->getMemOperand()->getValue(), CurDAG->getDataLayout())); 536 537 //TODO: Why do we need this? 538 if (N->getMemoryVT().bitsLT(MVT::i32)) 539 return true; 540 } 541 542 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::GLOBAL_ADDRESS); 543 } 544 545 bool AMDGPUDAGToDAGISel::isLocalLoad(const LoadSDNode *N) const { 546 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::LOCAL_ADDRESS); 547 } 548 549 bool AMDGPUDAGToDAGISel::isFlatLoad(const MemSDNode *N) const { 550 if (!N->readMem()) 551 return false; 552 return checkType(N->getMemOperand()->getValue(), AMDGPUAS::FLAT_ADDRESS); 553 } 554 555 bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const { 556 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock(); 557 const Instruction *Term = BB->getTerminator(); 558 return Term->getMetadata("amdgpu.uniform") || 559 Term->getMetadata("structurizecfg.uniform"); 560 } 561 562 const char *AMDGPUDAGToDAGISel::getPassName() const { 563 return "AMDGPU DAG->DAG Pattern Instruction Selection"; 564 } 565 566 //===----------------------------------------------------------------------===// 567 // Complex Patterns 568 //===----------------------------------------------------------------------===// 569 570 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr, 571 SDValue& IntPtr) { 572 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) { 573 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr), 574 true); 575 return true; 576 } 577 return false; 578 } 579 580 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr, 581 SDValue& BaseReg, SDValue &Offset) { 582 if (!isa<ConstantSDNode>(Addr)) { 583 BaseReg = Addr; 584 Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true); 585 return true; 586 } 587 return false; 588 } 589 590 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base, 591 SDValue &Offset) { 592 ConstantSDNode *IMMOffset; 593 594 if (Addr.getOpcode() == ISD::ADD 595 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) 596 && isInt<16>(IMMOffset->getZExtValue())) { 597 598 Base = Addr.getOperand(0); 599 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), 600 MVT::i32); 601 return true; 602 // If the pointer address is constant, we can move it to the offset field. 603 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr)) 604 && isInt<16>(IMMOffset->getZExtValue())) { 605 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), 606 SDLoc(CurDAG->getEntryNode()), 607 AMDGPU::ZERO, MVT::i32); 608 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), 609 MVT::i32); 610 return true; 611 } 612 613 // Default case, no offset 614 Base = Addr; 615 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32); 616 return true; 617 } 618 619 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base, 620 SDValue &Offset) { 621 ConstantSDNode *C; 622 SDLoc DL(Addr); 623 624 if ((C = dyn_cast<ConstantSDNode>(Addr))) { 625 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); 626 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 627 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) && 628 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) { 629 Base = Addr.getOperand(0); 630 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 631 } else { 632 Base = Addr; 633 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); 634 } 635 636 return true; 637 } 638 639 void AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) { 640 SDLoc DL(N); 641 SDValue LHS = N->getOperand(0); 642 SDValue RHS = N->getOperand(1); 643 644 bool IsAdd = (N->getOpcode() == ISD::ADD); 645 646 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); 647 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); 648 649 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 650 DL, MVT::i32, LHS, Sub0); 651 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 652 DL, MVT::i32, LHS, Sub1); 653 654 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 655 DL, MVT::i32, RHS, Sub0); 656 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 657 DL, MVT::i32, RHS, Sub1); 658 659 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue); 660 SDValue AddLoArgs[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) }; 661 662 unsigned Opc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32; 663 unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32; 664 665 SDNode *AddLo = CurDAG->getMachineNode( Opc, DL, VTList, AddLoArgs); 666 SDValue Carry(AddLo, 1); 667 SDNode *AddHi 668 = CurDAG->getMachineNode(CarryOpc, DL, MVT::i32, 669 SDValue(Hi0, 0), SDValue(Hi1, 0), Carry); 670 671 SDValue Args[5] = { 672 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), 673 SDValue(AddLo,0), 674 Sub0, 675 SDValue(AddHi,0), 676 Sub1, 677 }; 678 CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, MVT::i64, Args); 679 } 680 681 // We need to handle this here because tablegen doesn't support matching 682 // instructions with multiple outputs. 683 void AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) { 684 SDLoc SL(N); 685 EVT VT = N->getValueType(0); 686 687 assert(VT == MVT::f32 || VT == MVT::f64); 688 689 unsigned Opc 690 = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32; 691 692 // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, 693 // omod 694 SDValue Ops[8]; 695 696 SelectVOP3Mods0(N->getOperand(0), Ops[1], Ops[0], Ops[6], Ops[7]); 697 SelectVOP3Mods(N->getOperand(1), Ops[3], Ops[2]); 698 SelectVOP3Mods(N->getOperand(2), Ops[5], Ops[4]); 699 CurDAG->SelectNodeTo(N, Opc, VT, MVT::i1, Ops); 700 } 701 702 bool AMDGPUDAGToDAGISel::isDSOffsetLegal(const SDValue &Base, unsigned Offset, 703 unsigned OffsetBits) const { 704 if ((OffsetBits == 16 && !isUInt<16>(Offset)) || 705 (OffsetBits == 8 && !isUInt<8>(Offset))) 706 return false; 707 708 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS || 709 Subtarget->unsafeDSOffsetFoldingEnabled()) 710 return true; 711 712 // On Southern Islands instruction with a negative base value and an offset 713 // don't seem to work. 714 return CurDAG->SignBitIsZero(Base); 715 } 716 717 bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base, 718 SDValue &Offset) const { 719 SDLoc DL(Addr); 720 if (CurDAG->isBaseWithConstantOffset(Addr)) { 721 SDValue N0 = Addr.getOperand(0); 722 SDValue N1 = Addr.getOperand(1); 723 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 724 if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) { 725 // (add n0, c0) 726 Base = N0; 727 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 728 return true; 729 } 730 } else if (Addr.getOpcode() == ISD::SUB) { 731 // sub C, x -> add (sub 0, x), C 732 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { 733 int64_t ByteOffset = C->getSExtValue(); 734 if (isUInt<16>(ByteOffset)) { 735 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 736 737 // XXX - This is kind of hacky. Create a dummy sub node so we can check 738 // the known bits in isDSOffsetLegal. We need to emit the selected node 739 // here, so this is thrown away. 740 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32, 741 Zero, Addr.getOperand(1)); 742 743 if (isDSOffsetLegal(Sub, ByteOffset, 16)) { 744 MachineSDNode *MachineSub 745 = CurDAG->getMachineNode(AMDGPU::V_SUB_I32_e32, DL, MVT::i32, 746 Zero, Addr.getOperand(1)); 747 748 Base = SDValue(MachineSub, 0); 749 Offset = CurDAG->getTargetConstant(ByteOffset, DL, MVT::i16); 750 return true; 751 } 752 } 753 } 754 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { 755 // If we have a constant address, prefer to put the constant into the 756 // offset. This can save moves to load the constant address since multiple 757 // operations can share the zero base address register, and enables merging 758 // into read2 / write2 instructions. 759 760 SDLoc DL(Addr); 761 762 if (isUInt<16>(CAddr->getZExtValue())) { 763 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 764 MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 765 DL, MVT::i32, Zero); 766 Base = SDValue(MovZero, 0); 767 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16); 768 return true; 769 } 770 } 771 772 // default case 773 Base = Addr; 774 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16); 775 return true; 776 } 777 778 // TODO: If offset is too big, put low 16-bit into offset. 779 bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base, 780 SDValue &Offset0, 781 SDValue &Offset1) const { 782 SDLoc DL(Addr); 783 784 if (CurDAG->isBaseWithConstantOffset(Addr)) { 785 SDValue N0 = Addr.getOperand(0); 786 SDValue N1 = Addr.getOperand(1); 787 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 788 unsigned DWordOffset0 = C1->getZExtValue() / 4; 789 unsigned DWordOffset1 = DWordOffset0 + 1; 790 // (add n0, c0) 791 if (isDSOffsetLegal(N0, DWordOffset1, 8)) { 792 Base = N0; 793 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); 794 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); 795 return true; 796 } 797 } else if (Addr.getOpcode() == ISD::SUB) { 798 // sub C, x -> add (sub 0, x), C 799 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { 800 unsigned DWordOffset0 = C->getZExtValue() / 4; 801 unsigned DWordOffset1 = DWordOffset0 + 1; 802 803 if (isUInt<8>(DWordOffset0)) { 804 SDLoc DL(Addr); 805 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 806 807 // XXX - This is kind of hacky. Create a dummy sub node so we can check 808 // the known bits in isDSOffsetLegal. We need to emit the selected node 809 // here, so this is thrown away. 810 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32, 811 Zero, Addr.getOperand(1)); 812 813 if (isDSOffsetLegal(Sub, DWordOffset1, 8)) { 814 MachineSDNode *MachineSub 815 = CurDAG->getMachineNode(AMDGPU::V_SUB_I32_e32, DL, MVT::i32, 816 Zero, Addr.getOperand(1)); 817 818 Base = SDValue(MachineSub, 0); 819 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); 820 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); 821 return true; 822 } 823 } 824 } 825 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { 826 unsigned DWordOffset0 = CAddr->getZExtValue() / 4; 827 unsigned DWordOffset1 = DWordOffset0 + 1; 828 assert(4 * DWordOffset0 == CAddr->getZExtValue()); 829 830 if (isUInt<8>(DWordOffset0) && isUInt<8>(DWordOffset1)) { 831 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 832 MachineSDNode *MovZero 833 = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 834 DL, MVT::i32, Zero); 835 Base = SDValue(MovZero, 0); 836 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); 837 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); 838 return true; 839 } 840 } 841 842 // default case 843 Base = Addr; 844 Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8); 845 Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8); 846 return true; 847 } 848 849 static bool isLegalMUBUFImmOffset(const ConstantSDNode *Imm) { 850 return isUInt<12>(Imm->getZExtValue()); 851 } 852 853 bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr, 854 SDValue &VAddr, SDValue &SOffset, 855 SDValue &Offset, SDValue &Offen, 856 SDValue &Idxen, SDValue &Addr64, 857 SDValue &GLC, SDValue &SLC, 858 SDValue &TFE) const { 859 // Subtarget prefers to use flat instruction 860 if (Subtarget->useFlatForGlobal()) 861 return false; 862 863 SDLoc DL(Addr); 864 865 if (!GLC.getNode()) 866 GLC = CurDAG->getTargetConstant(0, DL, MVT::i1); 867 if (!SLC.getNode()) 868 SLC = CurDAG->getTargetConstant(0, DL, MVT::i1); 869 TFE = CurDAG->getTargetConstant(0, DL, MVT::i1); 870 871 Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1); 872 Offen = CurDAG->getTargetConstant(0, DL, MVT::i1); 873 Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1); 874 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32); 875 876 if (CurDAG->isBaseWithConstantOffset(Addr)) { 877 SDValue N0 = Addr.getOperand(0); 878 SDValue N1 = Addr.getOperand(1); 879 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 880 881 if (N0.getOpcode() == ISD::ADD) { 882 // (add (add N2, N3), C1) -> addr64 883 SDValue N2 = N0.getOperand(0); 884 SDValue N3 = N0.getOperand(1); 885 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); 886 Ptr = N2; 887 VAddr = N3; 888 } else { 889 890 // (add N0, C1) -> offset 891 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32); 892 Ptr = N0; 893 } 894 895 if (isLegalMUBUFImmOffset(C1)) { 896 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 897 return true; 898 } 899 900 if (isUInt<32>(C1->getZExtValue())) { 901 // Illegal offset, store it in soffset. 902 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 903 SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 904 CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)), 905 0); 906 return true; 907 } 908 } 909 910 if (Addr.getOpcode() == ISD::ADD) { 911 // (add N0, N1) -> addr64 912 SDValue N0 = Addr.getOperand(0); 913 SDValue N1 = Addr.getOperand(1); 914 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); 915 Ptr = N0; 916 VAddr = N1; 917 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 918 return true; 919 } 920 921 // default case -> offset 922 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32); 923 Ptr = Addr; 924 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 925 926 return true; 927 } 928 929 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 930 SDValue &VAddr, SDValue &SOffset, 931 SDValue &Offset, SDValue &GLC, 932 SDValue &SLC, SDValue &TFE) const { 933 SDValue Ptr, Offen, Idxen, Addr64; 934 935 // addr64 bit was removed for volcanic islands. 936 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 937 return false; 938 939 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, 940 GLC, SLC, TFE)) 941 return false; 942 943 ConstantSDNode *C = cast<ConstantSDNode>(Addr64); 944 if (C->getSExtValue()) { 945 SDLoc DL(Addr); 946 947 const SITargetLowering& Lowering = 948 *static_cast<const SITargetLowering*>(getTargetLowering()); 949 950 SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0); 951 return true; 952 } 953 954 return false; 955 } 956 957 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 958 SDValue &VAddr, SDValue &SOffset, 959 SDValue &Offset, 960 SDValue &SLC) const { 961 SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1); 962 SDValue GLC, TFE; 963 964 return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE); 965 } 966 967 bool AMDGPUDAGToDAGISel::SelectMUBUFScratch(SDValue Addr, SDValue &Rsrc, 968 SDValue &VAddr, SDValue &SOffset, 969 SDValue &ImmOffset) const { 970 971 SDLoc DL(Addr); 972 MachineFunction &MF = CurDAG->getMachineFunction(); 973 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 974 975 Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32); 976 SOffset = CurDAG->getRegister(Info->getScratchWaveOffsetReg(), MVT::i32); 977 978 // (add n0, c1) 979 if (CurDAG->isBaseWithConstantOffset(Addr)) { 980 SDValue N0 = Addr.getOperand(0); 981 SDValue N1 = Addr.getOperand(1); 982 983 // Offsets in vaddr must be positive. 984 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 985 if (isLegalMUBUFImmOffset(C1)) { 986 VAddr = N0; 987 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 988 return true; 989 } 990 } 991 992 // (node) 993 VAddr = Addr; 994 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16); 995 return true; 996 } 997 998 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 999 SDValue &SOffset, SDValue &Offset, 1000 SDValue &GLC, SDValue &SLC, 1001 SDValue &TFE) const { 1002 SDValue Ptr, VAddr, Offen, Idxen, Addr64; 1003 const SIInstrInfo *TII = 1004 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); 1005 1006 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, 1007 GLC, SLC, TFE)) 1008 return false; 1009 1010 if (!cast<ConstantSDNode>(Offen)->getSExtValue() && 1011 !cast<ConstantSDNode>(Idxen)->getSExtValue() && 1012 !cast<ConstantSDNode>(Addr64)->getSExtValue()) { 1013 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | 1014 APInt::getAllOnesValue(32).getZExtValue(); // Size 1015 SDLoc DL(Addr); 1016 1017 const SITargetLowering& Lowering = 1018 *static_cast<const SITargetLowering*>(getTargetLowering()); 1019 1020 SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0); 1021 return true; 1022 } 1023 return false; 1024 } 1025 1026 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 1027 SDValue &Soffset, SDValue &Offset 1028 ) const { 1029 SDValue GLC, SLC, TFE; 1030 1031 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE); 1032 } 1033 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 1034 SDValue &Soffset, SDValue &Offset, 1035 SDValue &SLC) const { 1036 SDValue GLC, TFE; 1037 1038 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE); 1039 } 1040 1041 bool AMDGPUDAGToDAGISel::SelectMUBUFConstant(SDValue Constant, 1042 SDValue &SOffset, 1043 SDValue &ImmOffset) const { 1044 SDLoc DL(Constant); 1045 uint32_t Imm = cast<ConstantSDNode>(Constant)->getZExtValue(); 1046 uint32_t Overflow = 0; 1047 1048 if (Imm >= 4096) { 1049 if (Imm <= 4095 + 64) { 1050 // Use an SOffset inline constant for 1..64 1051 Overflow = Imm - 4095; 1052 Imm = 4095; 1053 } else { 1054 // Try to keep the same value in SOffset for adjacent loads, so that 1055 // the corresponding register contents can be re-used. 1056 // 1057 // Load values with all low-bits set into SOffset, so that a larger 1058 // range of values can be covered using s_movk_i32 1059 uint32_t High = (Imm + 1) & ~4095; 1060 uint32_t Low = (Imm + 1) & 4095; 1061 Imm = Low; 1062 Overflow = High - 1; 1063 } 1064 } 1065 1066 // There is a hardware bug in SI and CI which prevents address clamping in 1067 // MUBUF instructions from working correctly with SOffsets. The immediate 1068 // offset is unaffected. 1069 if (Overflow > 0 && 1070 Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS) 1071 return false; 1072 1073 ImmOffset = CurDAG->getTargetConstant(Imm, DL, MVT::i16); 1074 1075 if (Overflow <= 64) 1076 SOffset = CurDAG->getTargetConstant(Overflow, DL, MVT::i32); 1077 else 1078 SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 1079 CurDAG->getTargetConstant(Overflow, DL, MVT::i32)), 1080 0); 1081 1082 return true; 1083 } 1084 1085 bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicOffset(SDValue Offset, 1086 SDValue &SOffset, 1087 SDValue &ImmOffset) const { 1088 SDLoc DL(Offset); 1089 1090 if (!isa<ConstantSDNode>(Offset)) 1091 return false; 1092 1093 return SelectMUBUFConstant(Offset, SOffset, ImmOffset); 1094 } 1095 1096 bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicVOffset(SDValue Offset, 1097 SDValue &SOffset, 1098 SDValue &ImmOffset, 1099 SDValue &VOffset) const { 1100 SDLoc DL(Offset); 1101 1102 // Don't generate an unnecessary voffset for constant offsets. 1103 if (isa<ConstantSDNode>(Offset)) { 1104 SDValue Tmp1, Tmp2; 1105 1106 // When necessary, use a voffset in <= CI anyway to work around a hardware 1107 // bug. 1108 if (Subtarget->getGeneration() > AMDGPUSubtarget::SEA_ISLANDS || 1109 SelectMUBUFConstant(Offset, Tmp1, Tmp2)) 1110 return false; 1111 } 1112 1113 if (CurDAG->isBaseWithConstantOffset(Offset)) { 1114 SDValue N0 = Offset.getOperand(0); 1115 SDValue N1 = Offset.getOperand(1); 1116 if (cast<ConstantSDNode>(N1)->getSExtValue() >= 0 && 1117 SelectMUBUFConstant(N1, SOffset, ImmOffset)) { 1118 VOffset = N0; 1119 return true; 1120 } 1121 } 1122 1123 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32); 1124 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1125 VOffset = Offset; 1126 1127 return true; 1128 } 1129 1130 bool AMDGPUDAGToDAGISel::SelectFlat(SDValue Addr, 1131 SDValue &VAddr, 1132 SDValue &SLC, 1133 SDValue &TFE) const { 1134 VAddr = Addr; 1135 TFE = SLC = CurDAG->getTargetConstant(0, SDLoc(), MVT::i1); 1136 return true; 1137 } 1138 1139 /// 1140 /// \param EncodedOffset This is the immediate value that will be encoded 1141 /// directly into the instruction. On SI/CI the \p EncodedOffset 1142 /// will be in units of dwords and on VI+ it will be units of bytes. 1143 static bool isLegalSMRDImmOffset(const AMDGPUSubtarget *ST, 1144 int64_t EncodedOffset) { 1145 return ST->getGeneration() < AMDGPUSubtarget::VOLCANIC_ISLANDS ? 1146 isUInt<8>(EncodedOffset) : isUInt<20>(EncodedOffset); 1147 } 1148 1149 bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode, 1150 SDValue &Offset, bool &Imm) const { 1151 1152 // FIXME: Handle non-constant offsets. 1153 ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode); 1154 if (!C) 1155 return false; 1156 1157 SDLoc SL(ByteOffsetNode); 1158 AMDGPUSubtarget::Generation Gen = Subtarget->getGeneration(); 1159 int64_t ByteOffset = C->getSExtValue(); 1160 int64_t EncodedOffset = Gen < AMDGPUSubtarget::VOLCANIC_ISLANDS ? 1161 ByteOffset >> 2 : ByteOffset; 1162 1163 if (isLegalSMRDImmOffset(Subtarget, EncodedOffset)) { 1164 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32); 1165 Imm = true; 1166 return true; 1167 } 1168 1169 if (!isUInt<32>(EncodedOffset) || !isUInt<32>(ByteOffset)) 1170 return false; 1171 1172 if (Gen == AMDGPUSubtarget::SEA_ISLANDS && isUInt<32>(EncodedOffset)) { 1173 // 32-bit Immediates are supported on Sea Islands. 1174 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32); 1175 } else { 1176 SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32); 1177 Offset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, 1178 C32Bit), 0); 1179 } 1180 Imm = false; 1181 return true; 1182 } 1183 1184 bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase, 1185 SDValue &Offset, bool &Imm) const { 1186 1187 SDLoc SL(Addr); 1188 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1189 SDValue N0 = Addr.getOperand(0); 1190 SDValue N1 = Addr.getOperand(1); 1191 1192 if (SelectSMRDOffset(N1, Offset, Imm)) { 1193 SBase = N0; 1194 return true; 1195 } 1196 } 1197 SBase = Addr; 1198 Offset = CurDAG->getTargetConstant(0, SL, MVT::i32); 1199 Imm = true; 1200 return true; 1201 } 1202 1203 bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase, 1204 SDValue &Offset) const { 1205 bool Imm; 1206 return SelectSMRD(Addr, SBase, Offset, Imm) && Imm; 1207 } 1208 1209 bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase, 1210 SDValue &Offset) const { 1211 1212 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS) 1213 return false; 1214 1215 bool Imm; 1216 if (!SelectSMRD(Addr, SBase, Offset, Imm)) 1217 return false; 1218 1219 return !Imm && isa<ConstantSDNode>(Offset); 1220 } 1221 1222 bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase, 1223 SDValue &Offset) const { 1224 bool Imm; 1225 return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm && 1226 !isa<ConstantSDNode>(Offset); 1227 } 1228 1229 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr, 1230 SDValue &Offset) const { 1231 bool Imm; 1232 return SelectSMRDOffset(Addr, Offset, Imm) && Imm; 1233 } 1234 1235 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr, 1236 SDValue &Offset) const { 1237 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS) 1238 return false; 1239 1240 bool Imm; 1241 if (!SelectSMRDOffset(Addr, Offset, Imm)) 1242 return false; 1243 1244 return !Imm && isa<ConstantSDNode>(Offset); 1245 } 1246 1247 bool AMDGPUDAGToDAGISel::SelectSMRDBufferSgpr(SDValue Addr, 1248 SDValue &Offset) const { 1249 bool Imm; 1250 return SelectSMRDOffset(Addr, Offset, Imm) && !Imm && 1251 !isa<ConstantSDNode>(Offset); 1252 } 1253 1254 SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, const SDLoc &DL, 1255 SDValue Val, uint32_t Offset, 1256 uint32_t Width) { 1257 // Transformation function, pack the offset and width of a BFE into 1258 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second 1259 // source, bits [5:0] contain the offset and bits [22:16] the width. 1260 uint32_t PackedVal = Offset | (Width << 16); 1261 SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32); 1262 1263 return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst); 1264 } 1265 1266 void AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) { 1267 // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c) 1268 // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c) 1269 // Predicate: 0 < b <= c < 32 1270 1271 const SDValue &Shl = N->getOperand(0); 1272 ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1)); 1273 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 1274 1275 if (B && C) { 1276 uint32_t BVal = B->getZExtValue(); 1277 uint32_t CVal = C->getZExtValue(); 1278 1279 if (0 < BVal && BVal <= CVal && CVal < 32) { 1280 bool Signed = N->getOpcode() == ISD::SRA; 1281 unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32; 1282 1283 ReplaceNode(N, getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), CVal - BVal, 1284 32 - CVal)); 1285 return; 1286 } 1287 } 1288 SelectCode(N); 1289 } 1290 1291 void AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) { 1292 switch (N->getOpcode()) { 1293 case ISD::AND: 1294 if (N->getOperand(0).getOpcode() == ISD::SRL) { 1295 // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)" 1296 // Predicate: isMask(mask) 1297 const SDValue &Srl = N->getOperand(0); 1298 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1)); 1299 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1)); 1300 1301 if (Shift && Mask) { 1302 uint32_t ShiftVal = Shift->getZExtValue(); 1303 uint32_t MaskVal = Mask->getZExtValue(); 1304 1305 if (isMask_32(MaskVal)) { 1306 uint32_t WidthVal = countPopulation(MaskVal); 1307 1308 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), 1309 Srl.getOperand(0), ShiftVal, WidthVal)); 1310 return; 1311 } 1312 } 1313 } 1314 break; 1315 case ISD::SRL: 1316 if (N->getOperand(0).getOpcode() == ISD::AND) { 1317 // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)" 1318 // Predicate: isMask(mask >> b) 1319 const SDValue &And = N->getOperand(0); 1320 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1)); 1321 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1)); 1322 1323 if (Shift && Mask) { 1324 uint32_t ShiftVal = Shift->getZExtValue(); 1325 uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal; 1326 1327 if (isMask_32(MaskVal)) { 1328 uint32_t WidthVal = countPopulation(MaskVal); 1329 1330 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), 1331 And.getOperand(0), ShiftVal, WidthVal)); 1332 return; 1333 } 1334 } 1335 } else if (N->getOperand(0).getOpcode() == ISD::SHL) { 1336 SelectS_BFEFromShifts(N); 1337 return; 1338 } 1339 break; 1340 case ISD::SRA: 1341 if (N->getOperand(0).getOpcode() == ISD::SHL) { 1342 SelectS_BFEFromShifts(N); 1343 return; 1344 } 1345 break; 1346 1347 case ISD::SIGN_EXTEND_INREG: { 1348 // sext_inreg (srl x, 16), i8 -> bfe_i32 x, 16, 8 1349 SDValue Src = N->getOperand(0); 1350 if (Src.getOpcode() != ISD::SRL) 1351 break; 1352 1353 const ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(Src.getOperand(1)); 1354 if (!Amt) 1355 break; 1356 1357 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits(); 1358 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_I32, SDLoc(N), Src.getOperand(0), 1359 Amt->getZExtValue(), Width)); 1360 return; 1361 } 1362 } 1363 1364 SelectCode(N); 1365 } 1366 1367 void AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) { 1368 SDValue Cond = N->getOperand(1); 1369 1370 if (isCBranchSCC(N)) { 1371 // This brcond will use S_CBRANCH_SCC*, so let tablegen handle it. 1372 SelectCode(N); 1373 return; 1374 } 1375 1376 // The result of VOPC instructions is or'd against ~EXEC before it is 1377 // written to vcc or another SGPR. This means that the value '1' is always 1378 // written to the corresponding bit for results that are masked. In order 1379 // to correctly check against vccz, we need to and VCC with the EXEC 1380 // register in order to clear the value from the masked bits. 1381 1382 SDLoc SL(N); 1383 1384 SDNode *MaskedCond = 1385 CurDAG->getMachineNode(AMDGPU::S_AND_B64, SL, MVT::i1, 1386 CurDAG->getRegister(AMDGPU::EXEC, MVT::i1), 1387 Cond); 1388 SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, AMDGPU::VCC, 1389 SDValue(MaskedCond, 0), 1390 SDValue()); // Passing SDValue() adds a 1391 // glue output. 1392 CurDAG->SelectNodeTo(N, AMDGPU::S_CBRANCH_VCCNZ, MVT::Other, 1393 N->getOperand(2), // Basic Block 1394 VCC.getValue(0), // Chain 1395 VCC.getValue(1)); // Glue 1396 return; 1397 } 1398 1399 // This is here because there isn't a way to use the generated sub0_sub1 as the 1400 // subreg index to EXTRACT_SUBREG in tablegen. 1401 void AMDGPUDAGToDAGISel::SelectATOMIC_CMP_SWAP(SDNode *N) { 1402 MemSDNode *Mem = cast<MemSDNode>(N); 1403 unsigned AS = Mem->getAddressSpace(); 1404 if (AS == AMDGPUAS::FLAT_ADDRESS) { 1405 SelectCode(N); 1406 return; 1407 } 1408 1409 MVT VT = N->getSimpleValueType(0); 1410 bool Is32 = (VT == MVT::i32); 1411 SDLoc SL(N); 1412 1413 MachineSDNode *CmpSwap = nullptr; 1414 if (Subtarget->hasAddr64()) { 1415 SDValue SRsrc, VAddr, SOffset, Offset, GLC, SLC; 1416 1417 if (SelectMUBUFAddr64(Mem->getBasePtr(), SRsrc, VAddr, SOffset, Offset, SLC)) { 1418 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_RTN_ADDR64 : 1419 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_RTN_ADDR64; 1420 SDValue CmpVal = Mem->getOperand(2); 1421 1422 // XXX - Do we care about glue operands? 1423 1424 SDValue Ops[] = { 1425 CmpVal, VAddr, SRsrc, SOffset, Offset, SLC, Mem->getChain() 1426 }; 1427 1428 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops); 1429 } 1430 } 1431 1432 if (!CmpSwap) { 1433 SDValue SRsrc, SOffset, Offset, SLC; 1434 if (SelectMUBUFOffset(Mem->getBasePtr(), SRsrc, SOffset, Offset, SLC)) { 1435 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_RTN_OFFSET : 1436 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_RTN_OFFSET; 1437 1438 SDValue CmpVal = Mem->getOperand(2); 1439 SDValue Ops[] = { 1440 CmpVal, SRsrc, SOffset, Offset, SLC, Mem->getChain() 1441 }; 1442 1443 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops); 1444 } 1445 } 1446 1447 if (!CmpSwap) { 1448 SelectCode(N); 1449 return; 1450 } 1451 1452 MachineSDNode::mmo_iterator MMOs = MF->allocateMemRefsArray(1); 1453 *MMOs = Mem->getMemOperand(); 1454 CmpSwap->setMemRefs(MMOs, MMOs + 1); 1455 1456 unsigned SubReg = Is32 ? AMDGPU::sub0 : AMDGPU::sub0_sub1; 1457 SDValue Extract 1458 = CurDAG->getTargetExtractSubreg(SubReg, SL, VT, SDValue(CmpSwap, 0)); 1459 1460 ReplaceUses(SDValue(N, 0), Extract); 1461 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 1)); 1462 CurDAG->RemoveDeadNode(N); 1463 } 1464 1465 bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src, 1466 SDValue &SrcMods) const { 1467 1468 unsigned Mods = 0; 1469 1470 Src = In; 1471 1472 if (Src.getOpcode() == ISD::FNEG) { 1473 Mods |= SISrcMods::NEG; 1474 Src = Src.getOperand(0); 1475 } 1476 1477 if (Src.getOpcode() == ISD::FABS) { 1478 Mods |= SISrcMods::ABS; 1479 Src = Src.getOperand(0); 1480 } 1481 1482 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 1483 1484 return true; 1485 } 1486 1487 bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src, 1488 SDValue &SrcMods) const { 1489 bool Res = SelectVOP3Mods(In, Src, SrcMods); 1490 return Res && cast<ConstantSDNode>(SrcMods)->isNullValue(); 1491 } 1492 1493 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src, 1494 SDValue &SrcMods, SDValue &Clamp, 1495 SDValue &Omod) const { 1496 SDLoc DL(In); 1497 // FIXME: Handle Clamp and Omod 1498 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i32); 1499 Omod = CurDAG->getTargetConstant(0, DL, MVT::i32); 1500 1501 return SelectVOP3Mods(In, Src, SrcMods); 1502 } 1503 1504 bool AMDGPUDAGToDAGISel::SelectVOP3NoMods0(SDValue In, SDValue &Src, 1505 SDValue &SrcMods, SDValue &Clamp, 1506 SDValue &Omod) const { 1507 bool Res = SelectVOP3Mods0(In, Src, SrcMods, Clamp, Omod); 1508 1509 return Res && cast<ConstantSDNode>(SrcMods)->isNullValue() && 1510 cast<ConstantSDNode>(Clamp)->isNullValue() && 1511 cast<ConstantSDNode>(Omod)->isNullValue(); 1512 } 1513 1514 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp(SDValue In, SDValue &Src, 1515 SDValue &SrcMods, 1516 SDValue &Omod) const { 1517 // FIXME: Handle Omod 1518 Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32); 1519 1520 return SelectVOP3Mods(In, Src, SrcMods); 1521 } 1522 1523 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, 1524 SDValue &SrcMods, 1525 SDValue &Clamp, 1526 SDValue &Omod) const { 1527 Clamp = Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32); 1528 return SelectVOP3Mods(In, Src, SrcMods); 1529 } 1530 1531 void AMDGPUDAGToDAGISel::PreprocessISelDAG() { 1532 MachineFrameInfo *MFI = CurDAG->getMachineFunction().getFrameInfo(); 1533 1534 // Handle the perverse case where a frame index is being stored. We don't 1535 // want to see multiple frame index operands on the same instruction since 1536 // it complicates things and violates some assumptions about frame index 1537 // lowering. 1538 for (int I = MFI->getObjectIndexBegin(), E = MFI->getObjectIndexEnd(); 1539 I != E; ++I) { 1540 SDValue FI = CurDAG->getTargetFrameIndex(I, MVT::i32); 1541 1542 // It's possible that we have a frame index defined in the function that 1543 // isn't used in this block. 1544 if (FI.use_empty()) 1545 continue; 1546 1547 // Skip over the AssertZext inserted during lowering. 1548 SDValue EffectiveFI = FI; 1549 auto It = FI->use_begin(); 1550 if (It->getOpcode() == ISD::AssertZext && FI->hasOneUse()) { 1551 EffectiveFI = SDValue(*It, 0); 1552 It = EffectiveFI->use_begin(); 1553 } 1554 1555 for (auto It = EffectiveFI->use_begin(); !It.atEnd(); ) { 1556 SDUse &Use = It.getUse(); 1557 SDNode *User = Use.getUser(); 1558 unsigned OpIdx = It.getOperandNo(); 1559 ++It; 1560 1561 if (MemSDNode *M = dyn_cast<MemSDNode>(User)) { 1562 unsigned PtrIdx = M->getOpcode() == ISD::STORE ? 2 : 1; 1563 if (OpIdx == PtrIdx) 1564 continue; 1565 1566 unsigned OpN = M->getNumOperands(); 1567 SDValue NewOps[8]; 1568 1569 assert(OpN < array_lengthof(NewOps)); 1570 for (unsigned Op = 0; Op != OpN; ++Op) { 1571 if (Op != OpIdx) { 1572 NewOps[Op] = M->getOperand(Op); 1573 continue; 1574 } 1575 1576 MachineSDNode *Mov = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 1577 SDLoc(M), MVT::i32, FI); 1578 NewOps[Op] = SDValue(Mov, 0); 1579 } 1580 1581 CurDAG->UpdateNodeOperands(M, makeArrayRef(NewOps, OpN)); 1582 } 1583 } 1584 } 1585 } 1586 1587 void AMDGPUDAGToDAGISel::PostprocessISelDAG() { 1588 const AMDGPUTargetLowering& Lowering = 1589 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering()); 1590 bool IsModified = false; 1591 do { 1592 IsModified = false; 1593 // Go over all selected nodes and try to fold them a bit more 1594 for (SDNode &Node : CurDAG->allnodes()) { 1595 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node); 1596 if (!MachineNode) 1597 continue; 1598 1599 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG); 1600 if (ResNode != &Node) { 1601 ReplaceUses(&Node, ResNode); 1602 IsModified = true; 1603 } 1604 } 1605 CurDAG->RemoveDeadNodes(); 1606 } while (IsModified); 1607 } 1608