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 "AMDGPU.h" 16 #include "AMDGPUArgumentUsageInfo.h" 17 #include "AMDGPUISelLowering.h" // For AMDGPUISD 18 #include "AMDGPUInstrInfo.h" 19 #include "AMDGPURegisterInfo.h" 20 #include "AMDGPUSubtarget.h" 21 #include "AMDGPUTargetMachine.h" 22 #include "SIDefines.h" 23 #include "SIISelLowering.h" 24 #include "SIInstrInfo.h" 25 #include "SIMachineFunctionInfo.h" 26 #include "SIRegisterInfo.h" 27 #include "llvm/ADT/APInt.h" 28 #include "llvm/ADT/SmallVector.h" 29 #include "llvm/ADT/StringRef.h" 30 #include "llvm/Analysis/ValueTracking.h" 31 #include "llvm/CodeGen/FunctionLoweringInfo.h" 32 #include "llvm/CodeGen/ISDOpcodes.h" 33 #include "llvm/CodeGen/MachineFunction.h" 34 #include "llvm/CodeGen/MachineRegisterInfo.h" 35 #include "llvm/CodeGen/MachineValueType.h" 36 #include "llvm/CodeGen/SelectionDAG.h" 37 #include "llvm/CodeGen/SelectionDAGISel.h" 38 #include "llvm/CodeGen/SelectionDAGNodes.h" 39 #include "llvm/CodeGen/ValueTypes.h" 40 #include "llvm/IR/BasicBlock.h" 41 #include "llvm/IR/Instruction.h" 42 #include "llvm/MC/MCInstrDesc.h" 43 #include "llvm/Support/Casting.h" 44 #include "llvm/Support/CodeGen.h" 45 #include "llvm/Support/ErrorHandling.h" 46 #include "llvm/Support/MathExtras.h" 47 #include <cassert> 48 #include <cstdint> 49 #include <new> 50 #include <vector> 51 52 using namespace llvm; 53 54 namespace llvm { 55 56 class R600InstrInfo; 57 58 } // end namespace llvm 59 60 //===----------------------------------------------------------------------===// 61 // Instruction Selector Implementation 62 //===----------------------------------------------------------------------===// 63 64 namespace { 65 66 /// AMDGPU specific code to select AMDGPU machine instructions for 67 /// SelectionDAG operations. 68 class AMDGPUDAGToDAGISel : public SelectionDAGISel { 69 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can 70 // make the right decision when generating code for different targets. 71 const AMDGPUSubtarget *Subtarget; 72 AMDGPUAS AMDGPUASI; 73 bool EnableLateStructurizeCFG; 74 75 public: 76 explicit AMDGPUDAGToDAGISel(TargetMachine *TM = nullptr, 77 CodeGenOpt::Level OptLevel = CodeGenOpt::Default) 78 : SelectionDAGISel(*TM, OptLevel) { 79 AMDGPUASI = AMDGPU::getAMDGPUAS(*TM); 80 EnableLateStructurizeCFG = AMDGPUTargetMachine::EnableLateStructurizeCFG; 81 } 82 ~AMDGPUDAGToDAGISel() override = default; 83 84 void getAnalysisUsage(AnalysisUsage &AU) const override { 85 AU.addRequired<AMDGPUArgumentUsageInfo>(); 86 SelectionDAGISel::getAnalysisUsage(AU); 87 } 88 89 bool runOnMachineFunction(MachineFunction &MF) override; 90 void Select(SDNode *N) override; 91 StringRef getPassName() const override; 92 void PostprocessISelDAG() override; 93 94 protected: 95 void SelectBuildVector(SDNode *N, unsigned RegClassID); 96 97 private: 98 std::pair<SDValue, SDValue> foldFrameIndex(SDValue N) const; 99 bool isNoNanSrc(SDValue N) const; 100 bool isInlineImmediate(const SDNode *N) const; 101 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs, 102 const R600InstrInfo *TII); 103 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &); 104 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &); 105 106 bool isConstantLoad(const MemSDNode *N, int cbID) const; 107 bool isUniformBr(const SDNode *N) const; 108 109 SDNode *glueCopyToM0(SDNode *N) const; 110 111 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const; 112 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr); 113 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg, 114 SDValue& Offset); 115 virtual bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset); 116 virtual bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset); 117 bool isDSOffsetLegal(const SDValue &Base, unsigned Offset, 118 unsigned OffsetBits) const; 119 bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const; 120 bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0, 121 SDValue &Offset1) const; 122 bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, 123 SDValue &SOffset, SDValue &Offset, SDValue &Offen, 124 SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC, 125 SDValue &TFE) const; 126 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, 127 SDValue &SOffset, SDValue &Offset, SDValue &GLC, 128 SDValue &SLC, SDValue &TFE) const; 129 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 130 SDValue &VAddr, SDValue &SOffset, SDValue &Offset, 131 SDValue &SLC) const; 132 bool SelectMUBUFScratchOffen(SDNode *Parent, 133 SDValue Addr, SDValue &RSrc, SDValue &VAddr, 134 SDValue &SOffset, SDValue &ImmOffset) const; 135 bool SelectMUBUFScratchOffset(SDNode *Parent, 136 SDValue Addr, SDValue &SRsrc, SDValue &Soffset, 137 SDValue &Offset) const; 138 139 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset, 140 SDValue &Offset, SDValue &GLC, SDValue &SLC, 141 SDValue &TFE) const; 142 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset, 143 SDValue &Offset, SDValue &SLC) const; 144 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset, 145 SDValue &Offset) const; 146 bool SelectMUBUFConstant(SDValue Constant, 147 SDValue &SOffset, 148 SDValue &ImmOffset) const; 149 bool SelectMUBUFIntrinsicOffset(SDValue Offset, SDValue &SOffset, 150 SDValue &ImmOffset) const; 151 bool SelectMUBUFIntrinsicVOffset(SDValue Offset, SDValue &SOffset, 152 SDValue &ImmOffset, SDValue &VOffset) const; 153 154 bool SelectFlatAtomic(SDValue Addr, SDValue &VAddr, 155 SDValue &Offset, SDValue &SLC) const; 156 bool SelectFlatAtomicSigned(SDValue Addr, SDValue &VAddr, 157 SDValue &Offset, SDValue &SLC) const; 158 159 template <bool IsSigned> 160 bool SelectFlatOffset(SDValue Addr, SDValue &VAddr, 161 SDValue &Offset, SDValue &SLC) const; 162 163 bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset, 164 bool &Imm) const; 165 bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset, 166 bool &Imm) const; 167 bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 168 bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 169 bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 170 bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const; 171 bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const; 172 bool SelectMOVRELOffset(SDValue Index, SDValue &Base, SDValue &Offset) const; 173 174 bool SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, SDValue &SrcMods) const; 175 bool SelectVOP3ModsImpl(SDValue In, SDValue &Src, unsigned &SrcMods) const; 176 bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 177 bool SelectVOP3NoMods(SDValue In, SDValue &Src) const; 178 bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods, 179 SDValue &Clamp, SDValue &Omod) const; 180 bool SelectVOP3NoMods0(SDValue In, SDValue &Src, SDValue &SrcMods, 181 SDValue &Clamp, SDValue &Omod) const; 182 183 bool SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, SDValue &SrcMods, 184 SDValue &Clamp, 185 SDValue &Omod) const; 186 187 bool SelectVOP3OMods(SDValue In, SDValue &Src, 188 SDValue &Clamp, SDValue &Omod) const; 189 190 bool SelectVOP3PMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 191 bool SelectVOP3PMods0(SDValue In, SDValue &Src, SDValue &SrcMods, 192 SDValue &Clamp) const; 193 194 bool SelectVOP3OpSel(SDValue In, SDValue &Src, SDValue &SrcMods) const; 195 bool SelectVOP3OpSel0(SDValue In, SDValue &Src, SDValue &SrcMods, 196 SDValue &Clamp) const; 197 198 bool SelectVOP3OpSelMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 199 bool SelectVOP3OpSelMods0(SDValue In, SDValue &Src, SDValue &SrcMods, 200 SDValue &Clamp) const; 201 bool SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src, unsigned &Mods) const; 202 bool SelectVOP3PMadMixMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 203 204 void SelectADD_SUB_I64(SDNode *N); 205 void SelectUADDO_USUBO(SDNode *N); 206 void SelectDIV_SCALE(SDNode *N); 207 void SelectMAD_64_32(SDNode *N); 208 void SelectFMA_W_CHAIN(SDNode *N); 209 void SelectFMUL_W_CHAIN(SDNode *N); 210 211 SDNode *getS_BFE(unsigned Opcode, const SDLoc &DL, SDValue Val, 212 uint32_t Offset, uint32_t Width); 213 void SelectS_BFEFromShifts(SDNode *N); 214 void SelectS_BFE(SDNode *N); 215 bool isCBranchSCC(const SDNode *N) const; 216 void SelectBRCOND(SDNode *N); 217 void SelectFMAD(SDNode *N); 218 void SelectATOMIC_CMP_SWAP(SDNode *N); 219 220 protected: 221 // Include the pieces autogenerated from the target description. 222 #include "AMDGPUGenDAGISel.inc" 223 }; 224 225 class R600DAGToDAGISel : public AMDGPUDAGToDAGISel { 226 public: 227 explicit R600DAGToDAGISel(TargetMachine *TM, CodeGenOpt::Level OptLevel) : 228 AMDGPUDAGToDAGISel(TM, OptLevel) {} 229 230 void Select(SDNode *N) override; 231 232 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, 233 SDValue &Offset) override; 234 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, 235 SDValue &Offset) override; 236 }; 237 238 } // end anonymous namespace 239 240 INITIALIZE_PASS_BEGIN(AMDGPUDAGToDAGISel, "isel", 241 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false) 242 INITIALIZE_PASS_DEPENDENCY(AMDGPUArgumentUsageInfo) 243 INITIALIZE_PASS_END(AMDGPUDAGToDAGISel, "isel", 244 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false) 245 246 /// \brief This pass converts a legalized DAG into a AMDGPU-specific 247 // DAG, ready for instruction scheduling. 248 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine *TM, 249 CodeGenOpt::Level OptLevel) { 250 return new AMDGPUDAGToDAGISel(TM, OptLevel); 251 } 252 253 /// \brief This pass converts a legalized DAG into a R600-specific 254 // DAG, ready for instruction scheduling. 255 FunctionPass *llvm::createR600ISelDag(TargetMachine *TM, 256 CodeGenOpt::Level OptLevel) { 257 return new R600DAGToDAGISel(TM, OptLevel); 258 } 259 260 bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) { 261 Subtarget = &MF.getSubtarget<AMDGPUSubtarget>(); 262 return SelectionDAGISel::runOnMachineFunction(MF); 263 } 264 265 bool AMDGPUDAGToDAGISel::isNoNanSrc(SDValue N) const { 266 if (TM.Options.NoNaNsFPMath) 267 return true; 268 269 // TODO: Move into isKnownNeverNaN 270 if (N->getFlags().isDefined()) 271 return N->getFlags().hasNoNaNs(); 272 273 return CurDAG->isKnownNeverNaN(N); 274 } 275 276 bool AMDGPUDAGToDAGISel::isInlineImmediate(const SDNode *N) const { 277 const SIInstrInfo *TII 278 = static_cast<const SISubtarget *>(Subtarget)->getInstrInfo(); 279 280 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) 281 return TII->isInlineConstant(C->getAPIntValue()); 282 283 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) 284 return TII->isInlineConstant(C->getValueAPF().bitcastToAPInt()); 285 286 return false; 287 } 288 289 /// \brief Determine the register class for \p OpNo 290 /// \returns The register class of the virtual register that will be used for 291 /// the given operand number \OpNo or NULL if the register class cannot be 292 /// determined. 293 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N, 294 unsigned OpNo) const { 295 if (!N->isMachineOpcode()) { 296 if (N->getOpcode() == ISD::CopyToReg) { 297 unsigned Reg = cast<RegisterSDNode>(N->getOperand(1))->getReg(); 298 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 299 MachineRegisterInfo &MRI = CurDAG->getMachineFunction().getRegInfo(); 300 return MRI.getRegClass(Reg); 301 } 302 303 const SIRegisterInfo *TRI 304 = static_cast<const SISubtarget *>(Subtarget)->getRegisterInfo(); 305 return TRI->getPhysRegClass(Reg); 306 } 307 308 return nullptr; 309 } 310 311 switch (N->getMachineOpcode()) { 312 default: { 313 const MCInstrDesc &Desc = 314 Subtarget->getInstrInfo()->get(N->getMachineOpcode()); 315 unsigned OpIdx = Desc.getNumDefs() + OpNo; 316 if (OpIdx >= Desc.getNumOperands()) 317 return nullptr; 318 int RegClass = Desc.OpInfo[OpIdx].RegClass; 319 if (RegClass == -1) 320 return nullptr; 321 322 return Subtarget->getRegisterInfo()->getRegClass(RegClass); 323 } 324 case AMDGPU::REG_SEQUENCE: { 325 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 326 const TargetRegisterClass *SuperRC = 327 Subtarget->getRegisterInfo()->getRegClass(RCID); 328 329 SDValue SubRegOp = N->getOperand(OpNo + 1); 330 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue(); 331 return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC, 332 SubRegIdx); 333 } 334 } 335 } 336 337 SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N) const { 338 if (cast<MemSDNode>(N)->getAddressSpace() != AMDGPUASI.LOCAL_ADDRESS) 339 return N; 340 341 const SITargetLowering& Lowering = 342 *static_cast<const SITargetLowering*>(getTargetLowering()); 343 344 // Write max value to m0 before each load operation 345 346 SDValue M0 = Lowering.copyToM0(*CurDAG, CurDAG->getEntryNode(), SDLoc(N), 347 CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32)); 348 349 SDValue Glue = M0.getValue(1); 350 351 SmallVector <SDValue, 8> Ops; 352 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 353 Ops.push_back(N->getOperand(i)); 354 } 355 Ops.push_back(Glue); 356 CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops); 357 358 return N; 359 } 360 361 static unsigned selectSGPRVectorRegClassID(unsigned NumVectorElts) { 362 switch (NumVectorElts) { 363 case 1: 364 return AMDGPU::SReg_32_XM0RegClassID; 365 case 2: 366 return AMDGPU::SReg_64RegClassID; 367 case 4: 368 return AMDGPU::SReg_128RegClassID; 369 case 8: 370 return AMDGPU::SReg_256RegClassID; 371 case 16: 372 return AMDGPU::SReg_512RegClassID; 373 } 374 375 llvm_unreachable("invalid vector size"); 376 } 377 378 static bool getConstantValue(SDValue N, uint32_t &Out) { 379 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) { 380 Out = C->getAPIntValue().getZExtValue(); 381 return true; 382 } 383 384 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) { 385 Out = C->getValueAPF().bitcastToAPInt().getZExtValue(); 386 return true; 387 } 388 389 return false; 390 } 391 392 void AMDGPUDAGToDAGISel::SelectBuildVector(SDNode *N, unsigned RegClassID) { 393 EVT VT = N->getValueType(0); 394 unsigned NumVectorElts = VT.getVectorNumElements(); 395 EVT EltVT = VT.getVectorElementType(); 396 const AMDGPURegisterInfo *TRI = Subtarget->getRegisterInfo(); 397 SDLoc DL(N); 398 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); 399 400 if (NumVectorElts == 1) { 401 CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, N->getOperand(0), 402 RegClass); 403 return; 404 } 405 406 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not " 407 "supported yet"); 408 // 16 = Max Num Vector Elements 409 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index) 410 // 1 = Vector Register Class 411 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1); 412 413 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); 414 bool IsRegSeq = true; 415 unsigned NOps = N->getNumOperands(); 416 for (unsigned i = 0; i < NOps; i++) { 417 // XXX: Why is this here? 418 if (isa<RegisterSDNode>(N->getOperand(i))) { 419 IsRegSeq = false; 420 break; 421 } 422 RegSeqArgs[1 + (2 * i)] = N->getOperand(i); 423 RegSeqArgs[1 + (2 * i) + 1] = 424 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL, 425 MVT::i32); 426 } 427 if (NOps != NumVectorElts) { 428 // Fill in the missing undef elements if this was a scalar_to_vector. 429 assert(N->getOpcode() == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts); 430 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, 431 DL, EltVT); 432 for (unsigned i = NOps; i < NumVectorElts; ++i) { 433 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0); 434 RegSeqArgs[1 + (2 * i) + 1] = 435 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL, MVT::i32); 436 } 437 } 438 439 if (!IsRegSeq) 440 SelectCode(N); 441 CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), RegSeqArgs); 442 } 443 444 void AMDGPUDAGToDAGISel::Select(SDNode *N) { 445 unsigned int Opc = N->getOpcode(); 446 if (N->isMachineOpcode()) { 447 N->setNodeId(-1); 448 return; // Already selected. 449 } 450 451 if (isa<AtomicSDNode>(N) || 452 (Opc == AMDGPUISD::ATOMIC_INC || Opc == AMDGPUISD::ATOMIC_DEC)) 453 N = glueCopyToM0(N); 454 455 switch (Opc) { 456 default: break; 457 // We are selecting i64 ADD here instead of custom lower it during 458 // DAG legalization, so we can fold some i64 ADDs used for address 459 // calculation into the LOAD and STORE instructions. 460 case ISD::ADD: 461 case ISD::ADDC: 462 case ISD::ADDE: 463 case ISD::SUB: 464 case ISD::SUBC: 465 case ISD::SUBE: { 466 if (N->getValueType(0) != MVT::i64) 467 break; 468 469 SelectADD_SUB_I64(N); 470 return; 471 } 472 case ISD::UADDO: 473 case ISD::USUBO: { 474 SelectUADDO_USUBO(N); 475 return; 476 } 477 case AMDGPUISD::FMUL_W_CHAIN: { 478 SelectFMUL_W_CHAIN(N); 479 return; 480 } 481 case AMDGPUISD::FMA_W_CHAIN: { 482 SelectFMA_W_CHAIN(N); 483 return; 484 } 485 486 case ISD::SCALAR_TO_VECTOR: 487 case ISD::BUILD_VECTOR: { 488 EVT VT = N->getValueType(0); 489 unsigned NumVectorElts = VT.getVectorNumElements(); 490 491 if (VT == MVT::v2i16 || VT == MVT::v2f16) { 492 if (Opc == ISD::BUILD_VECTOR) { 493 uint32_t LHSVal, RHSVal; 494 if (getConstantValue(N->getOperand(0), LHSVal) && 495 getConstantValue(N->getOperand(1), RHSVal)) { 496 uint32_t K = LHSVal | (RHSVal << 16); 497 CurDAG->SelectNodeTo(N, AMDGPU::S_MOV_B32, VT, 498 CurDAG->getTargetConstant(K, SDLoc(N), MVT::i32)); 499 return; 500 } 501 } 502 503 break; 504 } 505 506 assert(VT.getVectorElementType().bitsEq(MVT::i32)); 507 unsigned RegClassID = selectSGPRVectorRegClassID(NumVectorElts); 508 SelectBuildVector(N, RegClassID); 509 return; 510 } 511 case ISD::BUILD_PAIR: { 512 SDValue RC, SubReg0, SubReg1; 513 SDLoc DL(N); 514 if (N->getValueType(0) == MVT::i128) { 515 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32); 516 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32); 517 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32); 518 } else if (N->getValueType(0) == MVT::i64) { 519 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32); 520 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); 521 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); 522 } else { 523 llvm_unreachable("Unhandled value type for BUILD_PAIR"); 524 } 525 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0, 526 N->getOperand(1), SubReg1 }; 527 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, 528 N->getValueType(0), Ops)); 529 return; 530 } 531 532 case ISD::Constant: 533 case ISD::ConstantFP: { 534 if (N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N)) 535 break; 536 537 uint64_t Imm; 538 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N)) 539 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue(); 540 else { 541 ConstantSDNode *C = cast<ConstantSDNode>(N); 542 Imm = C->getZExtValue(); 543 } 544 545 SDLoc DL(N); 546 SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 547 CurDAG->getConstant(Imm & 0xFFFFFFFF, DL, 548 MVT::i32)); 549 SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 550 CurDAG->getConstant(Imm >> 32, DL, MVT::i32)); 551 const SDValue Ops[] = { 552 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), 553 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 554 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32) 555 }; 556 557 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, 558 N->getValueType(0), Ops)); 559 return; 560 } 561 case ISD::LOAD: 562 case ISD::STORE: { 563 N = glueCopyToM0(N); 564 break; 565 } 566 567 case AMDGPUISD::BFE_I32: 568 case AMDGPUISD::BFE_U32: { 569 // There is a scalar version available, but unlike the vector version which 570 // has a separate operand for the offset and width, the scalar version packs 571 // the width and offset into a single operand. Try to move to the scalar 572 // version if the offsets are constant, so that we can try to keep extended 573 // loads of kernel arguments in SGPRs. 574 575 // TODO: Technically we could try to pattern match scalar bitshifts of 576 // dynamic values, but it's probably not useful. 577 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 578 if (!Offset) 579 break; 580 581 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); 582 if (!Width) 583 break; 584 585 bool Signed = Opc == AMDGPUISD::BFE_I32; 586 587 uint32_t OffsetVal = Offset->getZExtValue(); 588 uint32_t WidthVal = Width->getZExtValue(); 589 590 ReplaceNode(N, getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32, 591 SDLoc(N), N->getOperand(0), OffsetVal, WidthVal)); 592 return; 593 } 594 case AMDGPUISD::DIV_SCALE: { 595 SelectDIV_SCALE(N); 596 return; 597 } 598 case AMDGPUISD::MAD_I64_I32: 599 case AMDGPUISD::MAD_U64_U32: { 600 SelectMAD_64_32(N); 601 return; 602 } 603 case ISD::CopyToReg: { 604 const SITargetLowering& Lowering = 605 *static_cast<const SITargetLowering*>(getTargetLowering()); 606 N = Lowering.legalizeTargetIndependentNode(N, *CurDAG); 607 break; 608 } 609 case ISD::AND: 610 case ISD::SRL: 611 case ISD::SRA: 612 case ISD::SIGN_EXTEND_INREG: 613 if (N->getValueType(0) != MVT::i32) 614 break; 615 616 SelectS_BFE(N); 617 return; 618 case ISD::BRCOND: 619 SelectBRCOND(N); 620 return; 621 case ISD::FMAD: 622 SelectFMAD(N); 623 return; 624 case AMDGPUISD::ATOMIC_CMP_SWAP: 625 SelectATOMIC_CMP_SWAP(N); 626 return; 627 } 628 629 SelectCode(N); 630 } 631 632 bool AMDGPUDAGToDAGISel::isConstantLoad(const MemSDNode *N, int CbId) const { 633 if (!N->readMem()) 634 return false; 635 if (CbId == -1) 636 return N->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS; 637 638 return N->getAddressSpace() == AMDGPUASI.CONSTANT_BUFFER_0 + CbId; 639 } 640 641 bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const { 642 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock(); 643 const Instruction *Term = BB->getTerminator(); 644 return Term->getMetadata("amdgpu.uniform") || 645 Term->getMetadata("structurizecfg.uniform"); 646 } 647 648 StringRef AMDGPUDAGToDAGISel::getPassName() const { 649 return "AMDGPU DAG->DAG Pattern Instruction Selection"; 650 } 651 652 //===----------------------------------------------------------------------===// 653 // Complex Patterns 654 //===----------------------------------------------------------------------===// 655 656 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr, 657 SDValue& IntPtr) { 658 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) { 659 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr), 660 true); 661 return true; 662 } 663 return false; 664 } 665 666 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr, 667 SDValue& BaseReg, SDValue &Offset) { 668 if (!isa<ConstantSDNode>(Addr)) { 669 BaseReg = Addr; 670 Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true); 671 return true; 672 } 673 return false; 674 } 675 676 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base, 677 SDValue &Offset) { 678 return false; 679 } 680 681 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base, 682 SDValue &Offset) { 683 ConstantSDNode *C; 684 SDLoc DL(Addr); 685 686 if ((C = dyn_cast<ConstantSDNode>(Addr))) { 687 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); 688 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 689 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) && 690 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) { 691 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); 692 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 693 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) && 694 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) { 695 Base = Addr.getOperand(0); 696 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 697 } else { 698 Base = Addr; 699 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); 700 } 701 702 return true; 703 } 704 705 void AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) { 706 SDLoc DL(N); 707 SDValue LHS = N->getOperand(0); 708 SDValue RHS = N->getOperand(1); 709 710 unsigned Opcode = N->getOpcode(); 711 bool ConsumeCarry = (Opcode == ISD::ADDE || Opcode == ISD::SUBE); 712 bool ProduceCarry = 713 ConsumeCarry || Opcode == ISD::ADDC || Opcode == ISD::SUBC; 714 bool IsAdd = 715 (Opcode == ISD::ADD || Opcode == ISD::ADDC || Opcode == ISD::ADDE); 716 717 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); 718 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); 719 720 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 721 DL, MVT::i32, LHS, Sub0); 722 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 723 DL, MVT::i32, LHS, Sub1); 724 725 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 726 DL, MVT::i32, RHS, Sub0); 727 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 728 DL, MVT::i32, RHS, Sub1); 729 730 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue); 731 732 unsigned Opc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32; 733 unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32; 734 735 SDNode *AddLo; 736 if (!ConsumeCarry) { 737 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) }; 738 AddLo = CurDAG->getMachineNode(Opc, DL, VTList, Args); 739 } else { 740 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0), N->getOperand(2) }; 741 AddLo = CurDAG->getMachineNode(CarryOpc, DL, VTList, Args); 742 } 743 SDValue AddHiArgs[] = { 744 SDValue(Hi0, 0), 745 SDValue(Hi1, 0), 746 SDValue(AddLo, 1) 747 }; 748 SDNode *AddHi = CurDAG->getMachineNode(CarryOpc, DL, VTList, AddHiArgs); 749 750 SDValue RegSequenceArgs[] = { 751 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), 752 SDValue(AddLo,0), 753 Sub0, 754 SDValue(AddHi,0), 755 Sub1, 756 }; 757 SDNode *RegSequence = CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL, 758 MVT::i64, RegSequenceArgs); 759 760 if (ProduceCarry) { 761 // Replace the carry-use 762 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 1), SDValue(AddHi, 1)); 763 } 764 765 // Replace the remaining uses. 766 CurDAG->ReplaceAllUsesWith(N, RegSequence); 767 CurDAG->RemoveDeadNode(N); 768 } 769 770 void AMDGPUDAGToDAGISel::SelectUADDO_USUBO(SDNode *N) { 771 // The name of the opcodes are misleading. v_add_i32/v_sub_i32 have unsigned 772 // carry out despite the _i32 name. These were renamed in VI to _U32. 773 // FIXME: We should probably rename the opcodes here. 774 unsigned Opc = N->getOpcode() == ISD::UADDO ? 775 AMDGPU::V_ADD_I32_e64 : AMDGPU::V_SUB_I32_e64; 776 777 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), 778 { N->getOperand(0), N->getOperand(1) }); 779 } 780 781 void AMDGPUDAGToDAGISel::SelectFMA_W_CHAIN(SDNode *N) { 782 SDLoc SL(N); 783 // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, omod 784 SDValue Ops[10]; 785 786 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[6], Ops[7]); 787 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]); 788 SelectVOP3Mods(N->getOperand(3), Ops[5], Ops[4]); 789 Ops[8] = N->getOperand(0); 790 Ops[9] = N->getOperand(4); 791 792 CurDAG->SelectNodeTo(N, AMDGPU::V_FMA_F32, N->getVTList(), Ops); 793 } 794 795 void AMDGPUDAGToDAGISel::SelectFMUL_W_CHAIN(SDNode *N) { 796 SDLoc SL(N); 797 // src0_modifiers, src0, src1_modifiers, src1, clamp, omod 798 SDValue Ops[8]; 799 800 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[4], Ops[5]); 801 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]); 802 Ops[6] = N->getOperand(0); 803 Ops[7] = N->getOperand(3); 804 805 CurDAG->SelectNodeTo(N, AMDGPU::V_MUL_F32_e64, N->getVTList(), Ops); 806 } 807 808 // We need to handle this here because tablegen doesn't support matching 809 // instructions with multiple outputs. 810 void AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) { 811 SDLoc SL(N); 812 EVT VT = N->getValueType(0); 813 814 assert(VT == MVT::f32 || VT == MVT::f64); 815 816 unsigned Opc 817 = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32; 818 819 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) }; 820 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops); 821 } 822 823 // We need to handle this here because tablegen doesn't support matching 824 // instructions with multiple outputs. 825 void AMDGPUDAGToDAGISel::SelectMAD_64_32(SDNode *N) { 826 SDLoc SL(N); 827 bool Signed = N->getOpcode() == AMDGPUISD::MAD_I64_I32; 828 unsigned Opc = Signed ? AMDGPU::V_MAD_I64_I32 : AMDGPU::V_MAD_U64_U32; 829 830 SDValue Clamp = CurDAG->getTargetConstant(0, SL, MVT::i1); 831 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2), 832 Clamp }; 833 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops); 834 } 835 836 bool AMDGPUDAGToDAGISel::isDSOffsetLegal(const SDValue &Base, unsigned Offset, 837 unsigned OffsetBits) const { 838 if ((OffsetBits == 16 && !isUInt<16>(Offset)) || 839 (OffsetBits == 8 && !isUInt<8>(Offset))) 840 return false; 841 842 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS || 843 Subtarget->unsafeDSOffsetFoldingEnabled()) 844 return true; 845 846 // On Southern Islands instruction with a negative base value and an offset 847 // don't seem to work. 848 return CurDAG->SignBitIsZero(Base); 849 } 850 851 bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base, 852 SDValue &Offset) const { 853 SDLoc DL(Addr); 854 if (CurDAG->isBaseWithConstantOffset(Addr)) { 855 SDValue N0 = Addr.getOperand(0); 856 SDValue N1 = Addr.getOperand(1); 857 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 858 if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) { 859 // (add n0, c0) 860 Base = N0; 861 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 862 return true; 863 } 864 } else if (Addr.getOpcode() == ISD::SUB) { 865 // sub C, x -> add (sub 0, x), C 866 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { 867 int64_t ByteOffset = C->getSExtValue(); 868 if (isUInt<16>(ByteOffset)) { 869 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 870 871 // XXX - This is kind of hacky. Create a dummy sub node so we can check 872 // the known bits in isDSOffsetLegal. We need to emit the selected node 873 // here, so this is thrown away. 874 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32, 875 Zero, Addr.getOperand(1)); 876 877 if (isDSOffsetLegal(Sub, ByteOffset, 16)) { 878 MachineSDNode *MachineSub 879 = CurDAG->getMachineNode(AMDGPU::V_SUB_I32_e32, DL, MVT::i32, 880 Zero, Addr.getOperand(1)); 881 882 Base = SDValue(MachineSub, 0); 883 Offset = CurDAG->getTargetConstant(ByteOffset, DL, MVT::i16); 884 return true; 885 } 886 } 887 } 888 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { 889 // If we have a constant address, prefer to put the constant into the 890 // offset. This can save moves to load the constant address since multiple 891 // operations can share the zero base address register, and enables merging 892 // into read2 / write2 instructions. 893 894 SDLoc DL(Addr); 895 896 if (isUInt<16>(CAddr->getZExtValue())) { 897 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 898 MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 899 DL, MVT::i32, Zero); 900 Base = SDValue(MovZero, 0); 901 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16); 902 return true; 903 } 904 } 905 906 // default case 907 Base = Addr; 908 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16); 909 return true; 910 } 911 912 // TODO: If offset is too big, put low 16-bit into offset. 913 bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base, 914 SDValue &Offset0, 915 SDValue &Offset1) const { 916 SDLoc DL(Addr); 917 918 if (CurDAG->isBaseWithConstantOffset(Addr)) { 919 SDValue N0 = Addr.getOperand(0); 920 SDValue N1 = Addr.getOperand(1); 921 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 922 unsigned DWordOffset0 = C1->getZExtValue() / 4; 923 unsigned DWordOffset1 = DWordOffset0 + 1; 924 // (add n0, c0) 925 if (isDSOffsetLegal(N0, DWordOffset1, 8)) { 926 Base = N0; 927 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); 928 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); 929 return true; 930 } 931 } else if (Addr.getOpcode() == ISD::SUB) { 932 // sub C, x -> add (sub 0, x), C 933 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { 934 unsigned DWordOffset0 = C->getZExtValue() / 4; 935 unsigned DWordOffset1 = DWordOffset0 + 1; 936 937 if (isUInt<8>(DWordOffset0)) { 938 SDLoc DL(Addr); 939 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 940 941 // XXX - This is kind of hacky. Create a dummy sub node so we can check 942 // the known bits in isDSOffsetLegal. We need to emit the selected node 943 // here, so this is thrown away. 944 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32, 945 Zero, Addr.getOperand(1)); 946 947 if (isDSOffsetLegal(Sub, DWordOffset1, 8)) { 948 MachineSDNode *MachineSub 949 = CurDAG->getMachineNode(AMDGPU::V_SUB_I32_e32, DL, MVT::i32, 950 Zero, Addr.getOperand(1)); 951 952 Base = SDValue(MachineSub, 0); 953 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); 954 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); 955 return true; 956 } 957 } 958 } 959 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { 960 unsigned DWordOffset0 = CAddr->getZExtValue() / 4; 961 unsigned DWordOffset1 = DWordOffset0 + 1; 962 assert(4 * DWordOffset0 == CAddr->getZExtValue()); 963 964 if (isUInt<8>(DWordOffset0) && isUInt<8>(DWordOffset1)) { 965 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 966 MachineSDNode *MovZero 967 = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 968 DL, MVT::i32, Zero); 969 Base = SDValue(MovZero, 0); 970 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); 971 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); 972 return true; 973 } 974 } 975 976 // default case 977 978 // FIXME: This is broken on SI where we still need to check if the base 979 // pointer is positive here. 980 Base = Addr; 981 Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8); 982 Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8); 983 return true; 984 } 985 986 static bool isLegalMUBUFImmOffset(unsigned Imm) { 987 return isUInt<12>(Imm); 988 } 989 990 static bool isLegalMUBUFImmOffset(const ConstantSDNode *Imm) { 991 return isLegalMUBUFImmOffset(Imm->getZExtValue()); 992 } 993 994 bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr, 995 SDValue &VAddr, SDValue &SOffset, 996 SDValue &Offset, SDValue &Offen, 997 SDValue &Idxen, SDValue &Addr64, 998 SDValue &GLC, SDValue &SLC, 999 SDValue &TFE) const { 1000 // Subtarget prefers to use flat instruction 1001 if (Subtarget->useFlatForGlobal()) 1002 return false; 1003 1004 SDLoc DL(Addr); 1005 1006 if (!GLC.getNode()) 1007 GLC = CurDAG->getTargetConstant(0, DL, MVT::i1); 1008 if (!SLC.getNode()) 1009 SLC = CurDAG->getTargetConstant(0, DL, MVT::i1); 1010 TFE = CurDAG->getTargetConstant(0, DL, MVT::i1); 1011 1012 Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1); 1013 Offen = CurDAG->getTargetConstant(0, DL, MVT::i1); 1014 Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1); 1015 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32); 1016 1017 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1018 SDValue N0 = Addr.getOperand(0); 1019 SDValue N1 = Addr.getOperand(1); 1020 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 1021 1022 if (N0.getOpcode() == ISD::ADD) { 1023 // (add (add N2, N3), C1) -> addr64 1024 SDValue N2 = N0.getOperand(0); 1025 SDValue N3 = N0.getOperand(1); 1026 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); 1027 Ptr = N2; 1028 VAddr = N3; 1029 } else { 1030 // (add N0, C1) -> offset 1031 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32); 1032 Ptr = N0; 1033 } 1034 1035 if (isLegalMUBUFImmOffset(C1)) { 1036 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 1037 return true; 1038 } 1039 1040 if (isUInt<32>(C1->getZExtValue())) { 1041 // Illegal offset, store it in soffset. 1042 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1043 SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 1044 CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)), 1045 0); 1046 return true; 1047 } 1048 } 1049 1050 if (Addr.getOpcode() == ISD::ADD) { 1051 // (add N0, N1) -> addr64 1052 SDValue N0 = Addr.getOperand(0); 1053 SDValue N1 = Addr.getOperand(1); 1054 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); 1055 Ptr = N0; 1056 VAddr = N1; 1057 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1058 return true; 1059 } 1060 1061 // default case -> offset 1062 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32); 1063 Ptr = Addr; 1064 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1065 1066 return true; 1067 } 1068 1069 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 1070 SDValue &VAddr, SDValue &SOffset, 1071 SDValue &Offset, SDValue &GLC, 1072 SDValue &SLC, SDValue &TFE) const { 1073 SDValue Ptr, Offen, Idxen, Addr64; 1074 1075 // addr64 bit was removed for volcanic islands. 1076 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 1077 return false; 1078 1079 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, 1080 GLC, SLC, TFE)) 1081 return false; 1082 1083 ConstantSDNode *C = cast<ConstantSDNode>(Addr64); 1084 if (C->getSExtValue()) { 1085 SDLoc DL(Addr); 1086 1087 const SITargetLowering& Lowering = 1088 *static_cast<const SITargetLowering*>(getTargetLowering()); 1089 1090 SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0); 1091 return true; 1092 } 1093 1094 return false; 1095 } 1096 1097 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 1098 SDValue &VAddr, SDValue &SOffset, 1099 SDValue &Offset, 1100 SDValue &SLC) const { 1101 SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1); 1102 SDValue GLC, TFE; 1103 1104 return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE); 1105 } 1106 1107 static bool isStackPtrRelative(const MachinePointerInfo &PtrInfo) { 1108 auto PSV = PtrInfo.V.dyn_cast<const PseudoSourceValue *>(); 1109 return PSV && PSV->isStack(); 1110 } 1111 1112 std::pair<SDValue, SDValue> AMDGPUDAGToDAGISel::foldFrameIndex(SDValue N) const { 1113 const MachineFunction &MF = CurDAG->getMachineFunction(); 1114 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1115 1116 if (auto FI = dyn_cast<FrameIndexSDNode>(N)) { 1117 SDValue TFI = CurDAG->getTargetFrameIndex(FI->getIndex(), 1118 FI->getValueType(0)); 1119 1120 // If we can resolve this to a frame index access, this is relative to the 1121 // frame pointer SGPR. 1122 return std::make_pair(TFI, CurDAG->getRegister(Info->getFrameOffsetReg(), 1123 MVT::i32)); 1124 } 1125 1126 // If we don't know this private access is a local stack object, it needs to 1127 // be relative to the entry point's scratch wave offset register. 1128 return std::make_pair(N, CurDAG->getRegister(Info->getScratchWaveOffsetReg(), 1129 MVT::i32)); 1130 } 1131 1132 bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffen(SDNode *Parent, 1133 SDValue Addr, SDValue &Rsrc, 1134 SDValue &VAddr, SDValue &SOffset, 1135 SDValue &ImmOffset) const { 1136 1137 SDLoc DL(Addr); 1138 MachineFunction &MF = CurDAG->getMachineFunction(); 1139 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1140 1141 Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32); 1142 1143 if (ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { 1144 unsigned Imm = CAddr->getZExtValue(); 1145 assert(!isLegalMUBUFImmOffset(Imm) && 1146 "should have been selected by other pattern"); 1147 1148 SDValue HighBits = CurDAG->getTargetConstant(Imm & ~4095, DL, MVT::i32); 1149 MachineSDNode *MovHighBits = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 1150 DL, MVT::i32, HighBits); 1151 VAddr = SDValue(MovHighBits, 0); 1152 1153 // In a call sequence, stores to the argument stack area are relative to the 1154 // stack pointer. 1155 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo(); 1156 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ? 1157 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg(); 1158 1159 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32); 1160 ImmOffset = CurDAG->getTargetConstant(Imm & 4095, DL, MVT::i16); 1161 return true; 1162 } 1163 1164 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1165 // (add n0, c1) 1166 1167 SDValue N0 = Addr.getOperand(0); 1168 SDValue N1 = Addr.getOperand(1); 1169 1170 // Offsets in vaddr must be positive. 1171 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 1172 if (isLegalMUBUFImmOffset(C1)) { 1173 std::tie(VAddr, SOffset) = foldFrameIndex(N0); 1174 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 1175 return true; 1176 } 1177 } 1178 1179 // (node) 1180 std::tie(VAddr, SOffset) = foldFrameIndex(Addr); 1181 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1182 return true; 1183 } 1184 1185 bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffset(SDNode *Parent, 1186 SDValue Addr, 1187 SDValue &SRsrc, 1188 SDValue &SOffset, 1189 SDValue &Offset) const { 1190 ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr); 1191 if (!CAddr || !isLegalMUBUFImmOffset(CAddr)) 1192 return false; 1193 1194 SDLoc DL(Addr); 1195 MachineFunction &MF = CurDAG->getMachineFunction(); 1196 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1197 1198 SRsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32); 1199 1200 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo(); 1201 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ? 1202 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg(); 1203 1204 // FIXME: Get from MachinePointerInfo? We should only be using the frame 1205 // offset if we know this is in a call sequence. 1206 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32); 1207 1208 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16); 1209 return true; 1210 } 1211 1212 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 1213 SDValue &SOffset, SDValue &Offset, 1214 SDValue &GLC, SDValue &SLC, 1215 SDValue &TFE) const { 1216 SDValue Ptr, VAddr, Offen, Idxen, Addr64; 1217 const SIInstrInfo *TII = 1218 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); 1219 1220 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, 1221 GLC, SLC, TFE)) 1222 return false; 1223 1224 if (!cast<ConstantSDNode>(Offen)->getSExtValue() && 1225 !cast<ConstantSDNode>(Idxen)->getSExtValue() && 1226 !cast<ConstantSDNode>(Addr64)->getSExtValue()) { 1227 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | 1228 APInt::getAllOnesValue(32).getZExtValue(); // Size 1229 SDLoc DL(Addr); 1230 1231 const SITargetLowering& Lowering = 1232 *static_cast<const SITargetLowering*>(getTargetLowering()); 1233 1234 SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0); 1235 return true; 1236 } 1237 return false; 1238 } 1239 1240 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 1241 SDValue &Soffset, SDValue &Offset 1242 ) const { 1243 SDValue GLC, SLC, TFE; 1244 1245 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE); 1246 } 1247 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 1248 SDValue &Soffset, SDValue &Offset, 1249 SDValue &SLC) const { 1250 SDValue GLC, TFE; 1251 1252 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE); 1253 } 1254 1255 bool AMDGPUDAGToDAGISel::SelectMUBUFConstant(SDValue Constant, 1256 SDValue &SOffset, 1257 SDValue &ImmOffset) const { 1258 SDLoc DL(Constant); 1259 const uint32_t Align = 4; 1260 const uint32_t MaxImm = alignDown(4095, Align); 1261 uint32_t Imm = cast<ConstantSDNode>(Constant)->getZExtValue(); 1262 uint32_t Overflow = 0; 1263 1264 if (Imm > MaxImm) { 1265 if (Imm <= MaxImm + 64) { 1266 // Use an SOffset inline constant for 4..64 1267 Overflow = Imm - MaxImm; 1268 Imm = MaxImm; 1269 } else { 1270 // Try to keep the same value in SOffset for adjacent loads, so that 1271 // the corresponding register contents can be re-used. 1272 // 1273 // Load values with all low-bits (except for alignment bits) set into 1274 // SOffset, so that a larger range of values can be covered using 1275 // s_movk_i32. 1276 // 1277 // Atomic operations fail to work correctly when individual address 1278 // components are unaligned, even if their sum is aligned. 1279 uint32_t High = (Imm + Align) & ~4095; 1280 uint32_t Low = (Imm + Align) & 4095; 1281 Imm = Low; 1282 Overflow = High - Align; 1283 } 1284 } 1285 1286 // There is a hardware bug in SI and CI which prevents address clamping in 1287 // MUBUF instructions from working correctly with SOffsets. The immediate 1288 // offset is unaffected. 1289 if (Overflow > 0 && 1290 Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS) 1291 return false; 1292 1293 ImmOffset = CurDAG->getTargetConstant(Imm, DL, MVT::i16); 1294 1295 if (Overflow <= 64) 1296 SOffset = CurDAG->getTargetConstant(Overflow, DL, MVT::i32); 1297 else 1298 SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 1299 CurDAG->getTargetConstant(Overflow, DL, MVT::i32)), 1300 0); 1301 1302 return true; 1303 } 1304 1305 bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicOffset(SDValue Offset, 1306 SDValue &SOffset, 1307 SDValue &ImmOffset) const { 1308 SDLoc DL(Offset); 1309 1310 if (!isa<ConstantSDNode>(Offset)) 1311 return false; 1312 1313 return SelectMUBUFConstant(Offset, SOffset, ImmOffset); 1314 } 1315 1316 bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicVOffset(SDValue Offset, 1317 SDValue &SOffset, 1318 SDValue &ImmOffset, 1319 SDValue &VOffset) const { 1320 SDLoc DL(Offset); 1321 1322 // Don't generate an unnecessary voffset for constant offsets. 1323 if (isa<ConstantSDNode>(Offset)) { 1324 SDValue Tmp1, Tmp2; 1325 1326 // When necessary, use a voffset in <= CI anyway to work around a hardware 1327 // bug. 1328 if (Subtarget->getGeneration() > AMDGPUSubtarget::SEA_ISLANDS || 1329 SelectMUBUFConstant(Offset, Tmp1, Tmp2)) 1330 return false; 1331 } 1332 1333 if (CurDAG->isBaseWithConstantOffset(Offset)) { 1334 SDValue N0 = Offset.getOperand(0); 1335 SDValue N1 = Offset.getOperand(1); 1336 if (cast<ConstantSDNode>(N1)->getSExtValue() >= 0 && 1337 SelectMUBUFConstant(N1, SOffset, ImmOffset)) { 1338 VOffset = N0; 1339 return true; 1340 } 1341 } 1342 1343 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32); 1344 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1345 VOffset = Offset; 1346 1347 return true; 1348 } 1349 1350 template <bool IsSigned> 1351 bool AMDGPUDAGToDAGISel::SelectFlatOffset(SDValue Addr, 1352 SDValue &VAddr, 1353 SDValue &Offset, 1354 SDValue &SLC) const { 1355 int64_t OffsetVal = 0; 1356 1357 if (Subtarget->hasFlatInstOffsets() && 1358 CurDAG->isBaseWithConstantOffset(Addr)) { 1359 SDValue N0 = Addr.getOperand(0); 1360 SDValue N1 = Addr.getOperand(1); 1361 int64_t COffsetVal = cast<ConstantSDNode>(N1)->getSExtValue(); 1362 1363 if ((IsSigned && isInt<13>(COffsetVal)) || 1364 (!IsSigned && isUInt<12>(COffsetVal))) { 1365 Addr = N0; 1366 OffsetVal = COffsetVal; 1367 } 1368 } 1369 1370 VAddr = Addr; 1371 Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i16); 1372 SLC = CurDAG->getTargetConstant(0, SDLoc(), MVT::i1); 1373 1374 return true; 1375 } 1376 1377 bool AMDGPUDAGToDAGISel::SelectFlatAtomic(SDValue Addr, 1378 SDValue &VAddr, 1379 SDValue &Offset, 1380 SDValue &SLC) const { 1381 return SelectFlatOffset<false>(Addr, VAddr, Offset, SLC); 1382 } 1383 1384 bool AMDGPUDAGToDAGISel::SelectFlatAtomicSigned(SDValue Addr, 1385 SDValue &VAddr, 1386 SDValue &Offset, 1387 SDValue &SLC) const { 1388 return SelectFlatOffset<true>(Addr, VAddr, Offset, SLC); 1389 } 1390 1391 bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode, 1392 SDValue &Offset, bool &Imm) const { 1393 1394 // FIXME: Handle non-constant offsets. 1395 ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode); 1396 if (!C) 1397 return false; 1398 1399 SDLoc SL(ByteOffsetNode); 1400 AMDGPUSubtarget::Generation Gen = Subtarget->getGeneration(); 1401 int64_t ByteOffset = C->getSExtValue(); 1402 int64_t EncodedOffset = AMDGPU::getSMRDEncodedOffset(*Subtarget, ByteOffset); 1403 1404 if (AMDGPU::isLegalSMRDImmOffset(*Subtarget, ByteOffset)) { 1405 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32); 1406 Imm = true; 1407 return true; 1408 } 1409 1410 if (!isUInt<32>(EncodedOffset) || !isUInt<32>(ByteOffset)) 1411 return false; 1412 1413 if (Gen == AMDGPUSubtarget::SEA_ISLANDS && isUInt<32>(EncodedOffset)) { 1414 // 32-bit Immediates are supported on Sea Islands. 1415 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32); 1416 } else { 1417 SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32); 1418 Offset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, 1419 C32Bit), 0); 1420 } 1421 Imm = false; 1422 return true; 1423 } 1424 1425 bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase, 1426 SDValue &Offset, bool &Imm) const { 1427 SDLoc SL(Addr); 1428 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1429 SDValue N0 = Addr.getOperand(0); 1430 SDValue N1 = Addr.getOperand(1); 1431 1432 if (SelectSMRDOffset(N1, Offset, Imm)) { 1433 SBase = N0; 1434 return true; 1435 } 1436 } 1437 SBase = Addr; 1438 Offset = CurDAG->getTargetConstant(0, SL, MVT::i32); 1439 Imm = true; 1440 return true; 1441 } 1442 1443 bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase, 1444 SDValue &Offset) const { 1445 bool Imm; 1446 return SelectSMRD(Addr, SBase, Offset, Imm) && Imm; 1447 } 1448 1449 bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase, 1450 SDValue &Offset) const { 1451 1452 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS) 1453 return false; 1454 1455 bool Imm; 1456 if (!SelectSMRD(Addr, SBase, Offset, Imm)) 1457 return false; 1458 1459 return !Imm && isa<ConstantSDNode>(Offset); 1460 } 1461 1462 bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase, 1463 SDValue &Offset) const { 1464 bool Imm; 1465 return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm && 1466 !isa<ConstantSDNode>(Offset); 1467 } 1468 1469 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr, 1470 SDValue &Offset) const { 1471 bool Imm; 1472 return SelectSMRDOffset(Addr, Offset, Imm) && Imm; 1473 } 1474 1475 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr, 1476 SDValue &Offset) const { 1477 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS) 1478 return false; 1479 1480 bool Imm; 1481 if (!SelectSMRDOffset(Addr, Offset, Imm)) 1482 return false; 1483 1484 return !Imm && isa<ConstantSDNode>(Offset); 1485 } 1486 1487 bool AMDGPUDAGToDAGISel::SelectMOVRELOffset(SDValue Index, 1488 SDValue &Base, 1489 SDValue &Offset) const { 1490 SDLoc DL(Index); 1491 1492 if (CurDAG->isBaseWithConstantOffset(Index)) { 1493 SDValue N0 = Index.getOperand(0); 1494 SDValue N1 = Index.getOperand(1); 1495 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 1496 1497 // (add n0, c0) 1498 Base = N0; 1499 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32); 1500 return true; 1501 } 1502 1503 if (isa<ConstantSDNode>(Index)) 1504 return false; 1505 1506 Base = Index; 1507 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); 1508 return true; 1509 } 1510 1511 SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, const SDLoc &DL, 1512 SDValue Val, uint32_t Offset, 1513 uint32_t Width) { 1514 // Transformation function, pack the offset and width of a BFE into 1515 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second 1516 // source, bits [5:0] contain the offset and bits [22:16] the width. 1517 uint32_t PackedVal = Offset | (Width << 16); 1518 SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32); 1519 1520 return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst); 1521 } 1522 1523 void AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) { 1524 // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c) 1525 // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c) 1526 // Predicate: 0 < b <= c < 32 1527 1528 const SDValue &Shl = N->getOperand(0); 1529 ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1)); 1530 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 1531 1532 if (B && C) { 1533 uint32_t BVal = B->getZExtValue(); 1534 uint32_t CVal = C->getZExtValue(); 1535 1536 if (0 < BVal && BVal <= CVal && CVal < 32) { 1537 bool Signed = N->getOpcode() == ISD::SRA; 1538 unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32; 1539 1540 ReplaceNode(N, getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), CVal - BVal, 1541 32 - CVal)); 1542 return; 1543 } 1544 } 1545 SelectCode(N); 1546 } 1547 1548 void AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) { 1549 switch (N->getOpcode()) { 1550 case ISD::AND: 1551 if (N->getOperand(0).getOpcode() == ISD::SRL) { 1552 // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)" 1553 // Predicate: isMask(mask) 1554 const SDValue &Srl = N->getOperand(0); 1555 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1)); 1556 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1)); 1557 1558 if (Shift && Mask) { 1559 uint32_t ShiftVal = Shift->getZExtValue(); 1560 uint32_t MaskVal = Mask->getZExtValue(); 1561 1562 if (isMask_32(MaskVal)) { 1563 uint32_t WidthVal = countPopulation(MaskVal); 1564 1565 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), 1566 Srl.getOperand(0), ShiftVal, WidthVal)); 1567 return; 1568 } 1569 } 1570 } 1571 break; 1572 case ISD::SRL: 1573 if (N->getOperand(0).getOpcode() == ISD::AND) { 1574 // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)" 1575 // Predicate: isMask(mask >> b) 1576 const SDValue &And = N->getOperand(0); 1577 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1)); 1578 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1)); 1579 1580 if (Shift && Mask) { 1581 uint32_t ShiftVal = Shift->getZExtValue(); 1582 uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal; 1583 1584 if (isMask_32(MaskVal)) { 1585 uint32_t WidthVal = countPopulation(MaskVal); 1586 1587 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), 1588 And.getOperand(0), ShiftVal, WidthVal)); 1589 return; 1590 } 1591 } 1592 } else if (N->getOperand(0).getOpcode() == ISD::SHL) { 1593 SelectS_BFEFromShifts(N); 1594 return; 1595 } 1596 break; 1597 case ISD::SRA: 1598 if (N->getOperand(0).getOpcode() == ISD::SHL) { 1599 SelectS_BFEFromShifts(N); 1600 return; 1601 } 1602 break; 1603 1604 case ISD::SIGN_EXTEND_INREG: { 1605 // sext_inreg (srl x, 16), i8 -> bfe_i32 x, 16, 8 1606 SDValue Src = N->getOperand(0); 1607 if (Src.getOpcode() != ISD::SRL) 1608 break; 1609 1610 const ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(Src.getOperand(1)); 1611 if (!Amt) 1612 break; 1613 1614 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits(); 1615 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_I32, SDLoc(N), Src.getOperand(0), 1616 Amt->getZExtValue(), Width)); 1617 return; 1618 } 1619 } 1620 1621 SelectCode(N); 1622 } 1623 1624 bool AMDGPUDAGToDAGISel::isCBranchSCC(const SDNode *N) const { 1625 assert(N->getOpcode() == ISD::BRCOND); 1626 if (!N->hasOneUse()) 1627 return false; 1628 1629 SDValue Cond = N->getOperand(1); 1630 if (Cond.getOpcode() == ISD::CopyToReg) 1631 Cond = Cond.getOperand(2); 1632 1633 if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse()) 1634 return false; 1635 1636 MVT VT = Cond.getOperand(0).getSimpleValueType(); 1637 if (VT == MVT::i32) 1638 return true; 1639 1640 if (VT == MVT::i64) { 1641 auto ST = static_cast<const SISubtarget *>(Subtarget); 1642 1643 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); 1644 return (CC == ISD::SETEQ || CC == ISD::SETNE) && ST->hasScalarCompareEq64(); 1645 } 1646 1647 return false; 1648 } 1649 1650 void AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) { 1651 SDValue Cond = N->getOperand(1); 1652 1653 if (Cond.isUndef()) { 1654 CurDAG->SelectNodeTo(N, AMDGPU::SI_BR_UNDEF, MVT::Other, 1655 N->getOperand(2), N->getOperand(0)); 1656 return; 1657 } 1658 1659 bool UseSCCBr = isCBranchSCC(N) && isUniformBr(N); 1660 unsigned BrOp = UseSCCBr ? AMDGPU::S_CBRANCH_SCC1 : AMDGPU::S_CBRANCH_VCCNZ; 1661 unsigned CondReg = UseSCCBr ? AMDGPU::SCC : AMDGPU::VCC; 1662 SDLoc SL(N); 1663 1664 SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, CondReg, Cond); 1665 CurDAG->SelectNodeTo(N, BrOp, MVT::Other, 1666 N->getOperand(2), // Basic Block 1667 VCC.getValue(0)); 1668 } 1669 1670 void AMDGPUDAGToDAGISel::SelectFMAD(SDNode *N) { 1671 MVT VT = N->getSimpleValueType(0); 1672 if (VT != MVT::f32 || !Subtarget->hasMadMixInsts()) { 1673 SelectCode(N); 1674 return; 1675 } 1676 1677 SDValue Src0 = N->getOperand(0); 1678 SDValue Src1 = N->getOperand(1); 1679 SDValue Src2 = N->getOperand(2); 1680 unsigned Src0Mods, Src1Mods, Src2Mods; 1681 1682 // Avoid using v_mad_mix_f32 unless there is actually an operand using the 1683 // conversion from f16. 1684 bool Sel0 = SelectVOP3PMadMixModsImpl(Src0, Src0, Src0Mods); 1685 bool Sel1 = SelectVOP3PMadMixModsImpl(Src1, Src1, Src1Mods); 1686 bool Sel2 = SelectVOP3PMadMixModsImpl(Src2, Src2, Src2Mods); 1687 1688 assert(!Subtarget->hasFP32Denormals() && 1689 "fmad selected with denormals enabled"); 1690 // TODO: We can select this with f32 denormals enabled if all the sources are 1691 // converted from f16 (in which case fmad isn't legal). 1692 1693 if (Sel0 || Sel1 || Sel2) { 1694 // For dummy operands. 1695 SDValue Zero = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32); 1696 SDValue Ops[] = { 1697 CurDAG->getTargetConstant(Src0Mods, SDLoc(), MVT::i32), Src0, 1698 CurDAG->getTargetConstant(Src1Mods, SDLoc(), MVT::i32), Src1, 1699 CurDAG->getTargetConstant(Src2Mods, SDLoc(), MVT::i32), Src2, 1700 CurDAG->getTargetConstant(0, SDLoc(), MVT::i1), 1701 Zero, Zero 1702 }; 1703 1704 CurDAG->SelectNodeTo(N, AMDGPU::V_MAD_MIX_F32, MVT::f32, Ops); 1705 } else { 1706 SelectCode(N); 1707 } 1708 } 1709 1710 // This is here because there isn't a way to use the generated sub0_sub1 as the 1711 // subreg index to EXTRACT_SUBREG in tablegen. 1712 void AMDGPUDAGToDAGISel::SelectATOMIC_CMP_SWAP(SDNode *N) { 1713 MemSDNode *Mem = cast<MemSDNode>(N); 1714 unsigned AS = Mem->getAddressSpace(); 1715 if (AS == AMDGPUASI.FLAT_ADDRESS) { 1716 SelectCode(N); 1717 return; 1718 } 1719 1720 MVT VT = N->getSimpleValueType(0); 1721 bool Is32 = (VT == MVT::i32); 1722 SDLoc SL(N); 1723 1724 MachineSDNode *CmpSwap = nullptr; 1725 if (Subtarget->hasAddr64()) { 1726 SDValue SRsrc, VAddr, SOffset, Offset, SLC; 1727 1728 if (SelectMUBUFAddr64(Mem->getBasePtr(), SRsrc, VAddr, SOffset, Offset, SLC)) { 1729 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_ADDR64_RTN : 1730 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_ADDR64_RTN; 1731 SDValue CmpVal = Mem->getOperand(2); 1732 1733 // XXX - Do we care about glue operands? 1734 1735 SDValue Ops[] = { 1736 CmpVal, VAddr, SRsrc, SOffset, Offset, SLC, Mem->getChain() 1737 }; 1738 1739 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops); 1740 } 1741 } 1742 1743 if (!CmpSwap) { 1744 SDValue SRsrc, SOffset, Offset, SLC; 1745 if (SelectMUBUFOffset(Mem->getBasePtr(), SRsrc, SOffset, Offset, SLC)) { 1746 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_OFFSET_RTN : 1747 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_OFFSET_RTN; 1748 1749 SDValue CmpVal = Mem->getOperand(2); 1750 SDValue Ops[] = { 1751 CmpVal, SRsrc, SOffset, Offset, SLC, Mem->getChain() 1752 }; 1753 1754 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops); 1755 } 1756 } 1757 1758 if (!CmpSwap) { 1759 SelectCode(N); 1760 return; 1761 } 1762 1763 MachineSDNode::mmo_iterator MMOs = MF->allocateMemRefsArray(1); 1764 *MMOs = Mem->getMemOperand(); 1765 CmpSwap->setMemRefs(MMOs, MMOs + 1); 1766 1767 unsigned SubReg = Is32 ? AMDGPU::sub0 : AMDGPU::sub0_sub1; 1768 SDValue Extract 1769 = CurDAG->getTargetExtractSubreg(SubReg, SL, VT, SDValue(CmpSwap, 0)); 1770 1771 ReplaceUses(SDValue(N, 0), Extract); 1772 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 1)); 1773 CurDAG->RemoveDeadNode(N); 1774 } 1775 1776 bool AMDGPUDAGToDAGISel::SelectVOP3ModsImpl(SDValue In, SDValue &Src, 1777 unsigned &Mods) const { 1778 Mods = 0; 1779 Src = In; 1780 1781 if (Src.getOpcode() == ISD::FNEG) { 1782 Mods |= SISrcMods::NEG; 1783 Src = Src.getOperand(0); 1784 } 1785 1786 if (Src.getOpcode() == ISD::FABS) { 1787 Mods |= SISrcMods::ABS; 1788 Src = Src.getOperand(0); 1789 } 1790 1791 return true; 1792 } 1793 1794 bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src, 1795 SDValue &SrcMods) const { 1796 unsigned Mods; 1797 if (SelectVOP3ModsImpl(In, Src, Mods)) { 1798 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 1799 return true; 1800 } 1801 1802 return false; 1803 } 1804 1805 bool AMDGPUDAGToDAGISel::SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, 1806 SDValue &SrcMods) const { 1807 SelectVOP3Mods(In, Src, SrcMods); 1808 return isNoNanSrc(Src); 1809 } 1810 1811 bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src) const { 1812 if (In.getOpcode() == ISD::FABS || In.getOpcode() == ISD::FNEG) 1813 return false; 1814 1815 Src = In; 1816 return true; 1817 } 1818 1819 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src, 1820 SDValue &SrcMods, SDValue &Clamp, 1821 SDValue &Omod) const { 1822 SDLoc DL(In); 1823 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1); 1824 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1); 1825 1826 return SelectVOP3Mods(In, Src, SrcMods); 1827 } 1828 1829 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, 1830 SDValue &SrcMods, 1831 SDValue &Clamp, 1832 SDValue &Omod) const { 1833 Clamp = Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32); 1834 return SelectVOP3Mods(In, Src, SrcMods); 1835 } 1836 1837 bool AMDGPUDAGToDAGISel::SelectVOP3OMods(SDValue In, SDValue &Src, 1838 SDValue &Clamp, SDValue &Omod) const { 1839 Src = In; 1840 1841 SDLoc DL(In); 1842 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1); 1843 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1); 1844 1845 return true; 1846 } 1847 1848 static SDValue stripBitcast(SDValue Val) { 1849 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val; 1850 } 1851 1852 // Figure out if this is really an extract of the high 16-bits of a dword. 1853 static bool isExtractHiElt(SDValue In, SDValue &Out) { 1854 In = stripBitcast(In); 1855 if (In.getOpcode() != ISD::TRUNCATE) 1856 return false; 1857 1858 SDValue Srl = In.getOperand(0); 1859 if (Srl.getOpcode() == ISD::SRL) { 1860 if (ConstantSDNode *ShiftAmt = dyn_cast<ConstantSDNode>(Srl.getOperand(1))) { 1861 if (ShiftAmt->getZExtValue() == 16) { 1862 Out = stripBitcast(Srl.getOperand(0)); 1863 return true; 1864 } 1865 } 1866 } 1867 1868 return false; 1869 } 1870 1871 // Look through operations that obscure just looking at the low 16-bits of the 1872 // same register. 1873 static SDValue stripExtractLoElt(SDValue In) { 1874 if (In.getOpcode() == ISD::TRUNCATE) { 1875 SDValue Src = In.getOperand(0); 1876 if (Src.getValueType().getSizeInBits() == 32) 1877 return stripBitcast(Src); 1878 } 1879 1880 return In; 1881 } 1882 1883 bool AMDGPUDAGToDAGISel::SelectVOP3PMods(SDValue In, SDValue &Src, 1884 SDValue &SrcMods) const { 1885 unsigned Mods = 0; 1886 Src = In; 1887 1888 if (Src.getOpcode() == ISD::FNEG) { 1889 Mods ^= (SISrcMods::NEG | SISrcMods::NEG_HI); 1890 Src = Src.getOperand(0); 1891 } 1892 1893 if (Src.getOpcode() == ISD::BUILD_VECTOR) { 1894 unsigned VecMods = Mods; 1895 1896 SDValue Lo = stripBitcast(Src.getOperand(0)); 1897 SDValue Hi = stripBitcast(Src.getOperand(1)); 1898 1899 if (Lo.getOpcode() == ISD::FNEG) { 1900 Lo = stripBitcast(Lo.getOperand(0)); 1901 Mods ^= SISrcMods::NEG; 1902 } 1903 1904 if (Hi.getOpcode() == ISD::FNEG) { 1905 Hi = stripBitcast(Hi.getOperand(0)); 1906 Mods ^= SISrcMods::NEG_HI; 1907 } 1908 1909 if (isExtractHiElt(Lo, Lo)) 1910 Mods |= SISrcMods::OP_SEL_0; 1911 1912 if (isExtractHiElt(Hi, Hi)) 1913 Mods |= SISrcMods::OP_SEL_1; 1914 1915 Lo = stripExtractLoElt(Lo); 1916 Hi = stripExtractLoElt(Hi); 1917 1918 if (Lo == Hi && !isInlineImmediate(Lo.getNode())) { 1919 // Really a scalar input. Just select from the low half of the register to 1920 // avoid packing. 1921 1922 Src = Lo; 1923 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 1924 return true; 1925 } 1926 1927 Mods = VecMods; 1928 } 1929 1930 // Packed instructions do not have abs modifiers. 1931 Mods |= SISrcMods::OP_SEL_1; 1932 1933 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 1934 return true; 1935 } 1936 1937 bool AMDGPUDAGToDAGISel::SelectVOP3PMods0(SDValue In, SDValue &Src, 1938 SDValue &SrcMods, 1939 SDValue &Clamp) const { 1940 SDLoc SL(In); 1941 1942 // FIXME: Handle clamp and op_sel 1943 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32); 1944 1945 return SelectVOP3PMods(In, Src, SrcMods); 1946 } 1947 1948 bool AMDGPUDAGToDAGISel::SelectVOP3OpSel(SDValue In, SDValue &Src, 1949 SDValue &SrcMods) const { 1950 Src = In; 1951 // FIXME: Handle op_sel 1952 SrcMods = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32); 1953 return true; 1954 } 1955 1956 bool AMDGPUDAGToDAGISel::SelectVOP3OpSel0(SDValue In, SDValue &Src, 1957 SDValue &SrcMods, 1958 SDValue &Clamp) const { 1959 SDLoc SL(In); 1960 1961 // FIXME: Handle clamp 1962 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32); 1963 1964 return SelectVOP3OpSel(In, Src, SrcMods); 1965 } 1966 1967 bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods(SDValue In, SDValue &Src, 1968 SDValue &SrcMods) const { 1969 // FIXME: Handle op_sel 1970 return SelectVOP3Mods(In, Src, SrcMods); 1971 } 1972 1973 bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods0(SDValue In, SDValue &Src, 1974 SDValue &SrcMods, 1975 SDValue &Clamp) const { 1976 SDLoc SL(In); 1977 1978 // FIXME: Handle clamp 1979 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32); 1980 1981 return SelectVOP3OpSelMods(In, Src, SrcMods); 1982 } 1983 1984 // The return value is not whether the match is possible (which it always is), 1985 // but whether or not it a conversion is really used. 1986 bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src, 1987 unsigned &Mods) const { 1988 Mods = 0; 1989 SelectVOP3ModsImpl(In, Src, Mods); 1990 1991 if (Src.getOpcode() == ISD::FP_EXTEND) { 1992 Src = Src.getOperand(0); 1993 assert(Src.getValueType() == MVT::f16); 1994 Src = stripBitcast(Src); 1995 1996 // Be careful about folding modifiers if we already have an abs. fneg is 1997 // applied last, so we don't want to apply an earlier fneg. 1998 if ((Mods & SISrcMods::ABS) == 0) { 1999 unsigned ModsTmp; 2000 SelectVOP3ModsImpl(Src, Src, ModsTmp); 2001 2002 if ((ModsTmp & SISrcMods::NEG) != 0) 2003 Mods ^= SISrcMods::NEG; 2004 2005 if ((ModsTmp & SISrcMods::ABS) != 0) 2006 Mods |= SISrcMods::ABS; 2007 } 2008 2009 // op_sel/op_sel_hi decide the source type and source. 2010 // If the source's op_sel_hi is set, it indicates to do a conversion from fp16. 2011 // If the sources's op_sel is set, it picks the high half of the source 2012 // register. 2013 2014 Mods |= SISrcMods::OP_SEL_1; 2015 if (isExtractHiElt(Src, Src)) { 2016 Mods |= SISrcMods::OP_SEL_0; 2017 2018 // TODO: Should we try to look for neg/abs here? 2019 } 2020 2021 return true; 2022 } 2023 2024 return false; 2025 } 2026 2027 bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixMods(SDValue In, SDValue &Src, 2028 SDValue &SrcMods) const { 2029 unsigned Mods = 0; 2030 SelectVOP3PMadMixModsImpl(In, Src, Mods); 2031 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 2032 return true; 2033 } 2034 2035 void AMDGPUDAGToDAGISel::PostprocessISelDAG() { 2036 const AMDGPUTargetLowering& Lowering = 2037 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering()); 2038 bool IsModified = false; 2039 do { 2040 IsModified = false; 2041 // Go over all selected nodes and try to fold them a bit more 2042 for (SDNode &Node : CurDAG->allnodes()) { 2043 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node); 2044 if (!MachineNode) 2045 continue; 2046 2047 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG); 2048 if (ResNode != &Node) { 2049 ReplaceUses(&Node, ResNode); 2050 IsModified = true; 2051 } 2052 } 2053 CurDAG->RemoveDeadNodes(); 2054 } while (IsModified); 2055 } 2056 2057 void R600DAGToDAGISel::Select(SDNode *N) { 2058 unsigned int Opc = N->getOpcode(); 2059 if (N->isMachineOpcode()) { 2060 N->setNodeId(-1); 2061 return; // Already selected. 2062 } 2063 2064 switch (Opc) { 2065 default: break; 2066 case AMDGPUISD::BUILD_VERTICAL_VECTOR: 2067 case ISD::SCALAR_TO_VECTOR: 2068 case ISD::BUILD_VECTOR: { 2069 EVT VT = N->getValueType(0); 2070 unsigned NumVectorElts = VT.getVectorNumElements(); 2071 unsigned RegClassID; 2072 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG 2073 // that adds a 128 bits reg copy when going through TwoAddressInstructions 2074 // pass. We want to avoid 128 bits copies as much as possible because they 2075 // can't be bundled by our scheduler. 2076 switch(NumVectorElts) { 2077 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break; 2078 case 4: 2079 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR) 2080 RegClassID = AMDGPU::R600_Reg128VerticalRegClassID; 2081 else 2082 RegClassID = AMDGPU::R600_Reg128RegClassID; 2083 break; 2084 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR"); 2085 } 2086 SelectBuildVector(N, RegClassID); 2087 return; 2088 } 2089 } 2090 2091 SelectCode(N); 2092 } 2093 2094 bool R600DAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base, 2095 SDValue &Offset) { 2096 ConstantSDNode *C; 2097 SDLoc DL(Addr); 2098 2099 if ((C = dyn_cast<ConstantSDNode>(Addr))) { 2100 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); 2101 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 2102 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) && 2103 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) { 2104 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); 2105 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 2106 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) && 2107 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) { 2108 Base = Addr.getOperand(0); 2109 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 2110 } else { 2111 Base = Addr; 2112 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); 2113 } 2114 2115 return true; 2116 } 2117 2118 bool R600DAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base, 2119 SDValue &Offset) { 2120 ConstantSDNode *IMMOffset; 2121 2122 if (Addr.getOpcode() == ISD::ADD 2123 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) 2124 && isInt<16>(IMMOffset->getZExtValue())) { 2125 2126 Base = Addr.getOperand(0); 2127 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), 2128 MVT::i32); 2129 return true; 2130 // If the pointer address is constant, we can move it to the offset field. 2131 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr)) 2132 && isInt<16>(IMMOffset->getZExtValue())) { 2133 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), 2134 SDLoc(CurDAG->getEntryNode()), 2135 AMDGPU::ZERO, MVT::i32); 2136 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), 2137 MVT::i32); 2138 return true; 2139 } 2140 2141 // Default case, no offset 2142 Base = Addr; 2143 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32); 2144 return true; 2145 } 2146