1 //===-- AMDGPUISelDAGToDAG.cpp - A dag to dag inst selector for AMDGPU ----===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //==-----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// Defines an instruction selector for the AMDGPU target. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "AMDGPU.h" 15 #include "AMDGPUSubtarget.h" 16 #include "AMDGPUTargetMachine.h" 17 #include "SIMachineFunctionInfo.h" 18 #include "llvm/Analysis/LegacyDivergenceAnalysis.h" 19 #include "llvm/Analysis/ValueTracking.h" 20 #include "llvm/CodeGen/FunctionLoweringInfo.h" 21 #include "llvm/CodeGen/SelectionDAG.h" 22 #include "llvm/CodeGen/SelectionDAGISel.h" 23 #include "llvm/CodeGen/SelectionDAGNodes.h" 24 #include "llvm/IR/IntrinsicsAMDGPU.h" 25 #include "llvm/InitializePasses.h" 26 27 #ifdef EXPENSIVE_CHECKS 28 #include "llvm/Analysis/LoopInfo.h" 29 #include "llvm/IR/Dominators.h" 30 #endif 31 32 #define DEBUG_TYPE "isel" 33 34 using namespace llvm; 35 36 namespace llvm { 37 38 class R600InstrInfo; 39 40 } // end namespace llvm 41 42 //===----------------------------------------------------------------------===// 43 // Instruction Selector Implementation 44 //===----------------------------------------------------------------------===// 45 46 namespace { 47 48 static bool isNullConstantOrUndef(SDValue V) { 49 if (V.isUndef()) 50 return true; 51 52 ConstantSDNode *Const = dyn_cast<ConstantSDNode>(V); 53 return Const != nullptr && Const->isNullValue(); 54 } 55 56 static bool getConstantValue(SDValue N, uint32_t &Out) { 57 // This is only used for packed vectors, where ussing 0 for undef should 58 // always be good. 59 if (N.isUndef()) { 60 Out = 0; 61 return true; 62 } 63 64 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) { 65 Out = C->getAPIntValue().getSExtValue(); 66 return true; 67 } 68 69 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) { 70 Out = C->getValueAPF().bitcastToAPInt().getSExtValue(); 71 return true; 72 } 73 74 return false; 75 } 76 77 // TODO: Handle undef as zero 78 static SDNode *packConstantV2I16(const SDNode *N, SelectionDAG &DAG, 79 bool Negate = false) { 80 assert(N->getOpcode() == ISD::BUILD_VECTOR && N->getNumOperands() == 2); 81 uint32_t LHSVal, RHSVal; 82 if (getConstantValue(N->getOperand(0), LHSVal) && 83 getConstantValue(N->getOperand(1), RHSVal)) { 84 SDLoc SL(N); 85 uint32_t K = Negate ? 86 (-LHSVal & 0xffff) | (-RHSVal << 16) : 87 (LHSVal & 0xffff) | (RHSVal << 16); 88 return DAG.getMachineNode(AMDGPU::S_MOV_B32, SL, N->getValueType(0), 89 DAG.getTargetConstant(K, SL, MVT::i32)); 90 } 91 92 return nullptr; 93 } 94 95 static SDNode *packNegConstantV2I16(const SDNode *N, SelectionDAG &DAG) { 96 return packConstantV2I16(N, DAG, true); 97 } 98 99 /// AMDGPU specific code to select AMDGPU machine instructions for 100 /// SelectionDAG operations. 101 class AMDGPUDAGToDAGISel : public SelectionDAGISel { 102 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can 103 // make the right decision when generating code for different targets. 104 const GCNSubtarget *Subtarget; 105 106 // Default FP mode for the current function. 107 AMDGPU::SIModeRegisterDefaults Mode; 108 109 bool EnableLateStructurizeCFG; 110 111 public: 112 explicit AMDGPUDAGToDAGISel(TargetMachine *TM = nullptr, 113 CodeGenOpt::Level OptLevel = CodeGenOpt::Default) 114 : SelectionDAGISel(*TM, OptLevel) { 115 EnableLateStructurizeCFG = AMDGPUTargetMachine::EnableLateStructurizeCFG; 116 } 117 ~AMDGPUDAGToDAGISel() override = default; 118 119 void getAnalysisUsage(AnalysisUsage &AU) const override { 120 AU.addRequired<AMDGPUArgumentUsageInfo>(); 121 AU.addRequired<LegacyDivergenceAnalysis>(); 122 #ifdef EXPENSIVE_CHECKS 123 AU.addRequired<DominatorTreeWrapperPass>(); 124 AU.addRequired<LoopInfoWrapperPass>(); 125 #endif 126 SelectionDAGISel::getAnalysisUsage(AU); 127 } 128 129 bool matchLoadD16FromBuildVector(SDNode *N) const; 130 131 bool runOnMachineFunction(MachineFunction &MF) override; 132 void PreprocessISelDAG() override; 133 void Select(SDNode *N) override; 134 StringRef getPassName() const override; 135 void PostprocessISelDAG() override; 136 137 protected: 138 void SelectBuildVector(SDNode *N, unsigned RegClassID); 139 140 private: 141 std::pair<SDValue, SDValue> foldFrameIndex(SDValue N) const; 142 bool isNoNanSrc(SDValue N) const; 143 bool isInlineImmediate(const SDNode *N, bool Negated = false) const; 144 bool isNegInlineImmediate(const SDNode *N) const { 145 return isInlineImmediate(N, true); 146 } 147 148 bool isInlineImmediate16(int64_t Imm) const { 149 return AMDGPU::isInlinableLiteral16(Imm, Subtarget->hasInv2PiInlineImm()); 150 } 151 152 bool isInlineImmediate32(int64_t Imm) const { 153 return AMDGPU::isInlinableLiteral32(Imm, Subtarget->hasInv2PiInlineImm()); 154 } 155 156 bool isInlineImmediate64(int64_t Imm) const { 157 return AMDGPU::isInlinableLiteral64(Imm, Subtarget->hasInv2PiInlineImm()); 158 } 159 160 bool isInlineImmediate(const APFloat &Imm) const { 161 return Subtarget->getInstrInfo()->isInlineConstant(Imm); 162 } 163 164 bool isVGPRImm(const SDNode *N) const; 165 bool isUniformLoad(const SDNode *N) const; 166 bool isUniformBr(const SDNode *N) const; 167 168 bool isBaseWithConstantOffset64(SDValue Addr, SDValue &LHS, 169 SDValue &RHS) const; 170 171 MachineSDNode *buildSMovImm64(SDLoc &DL, uint64_t Val, EVT VT) const; 172 173 SDNode *glueCopyToOp(SDNode *N, SDValue NewChain, SDValue Glue) const; 174 SDNode *glueCopyToM0(SDNode *N, SDValue Val) const; 175 SDNode *glueCopyToM0LDSInit(SDNode *N) const; 176 177 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const; 178 virtual bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset); 179 virtual bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset); 180 bool isDSOffsetLegal(SDValue Base, unsigned Offset) const; 181 bool isDSOffset2Legal(SDValue Base, unsigned Offset0, unsigned Offset1, 182 unsigned Size) const; 183 bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const; 184 bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0, 185 SDValue &Offset1) const; 186 bool SelectDS128Bit8ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0, 187 SDValue &Offset1) const; 188 bool SelectDSReadWrite2(SDValue Ptr, SDValue &Base, SDValue &Offset0, 189 SDValue &Offset1, unsigned Size) const; 190 bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, 191 SDValue &SOffset, SDValue &Offset, SDValue &Offen, 192 SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC, 193 SDValue &TFE, SDValue &DLC, SDValue &SWZ) const; 194 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, 195 SDValue &SOffset, SDValue &Offset, SDValue &GLC, 196 SDValue &SLC, SDValue &TFE, SDValue &DLC, 197 SDValue &SWZ) const; 198 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 199 SDValue &VAddr, SDValue &SOffset, SDValue &Offset, 200 SDValue &SLC) const; 201 bool SelectMUBUFScratchOffen(SDNode *Parent, 202 SDValue Addr, SDValue &RSrc, SDValue &VAddr, 203 SDValue &SOffset, SDValue &ImmOffset) const; 204 bool SelectMUBUFScratchOffset(SDNode *Parent, 205 SDValue Addr, SDValue &SRsrc, SDValue &Soffset, 206 SDValue &Offset) const; 207 208 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset, 209 SDValue &Offset, SDValue &GLC, SDValue &SLC, 210 SDValue &TFE, SDValue &DLC, SDValue &SWZ) const; 211 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset, 212 SDValue &Offset, SDValue &SLC) const; 213 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset, 214 SDValue &Offset) const; 215 216 template <bool IsSigned> 217 bool SelectFlatOffset(SDNode *N, SDValue Addr, SDValue &VAddr, 218 SDValue &Offset) const; 219 bool SelectGlobalSAddr(SDNode *N, SDValue Addr, SDValue &SAddr, 220 SDValue &VOffset, SDValue &Offset) const; 221 bool SelectScratchSAddr(SDNode *N, SDValue Addr, SDValue &SAddr, 222 SDValue &Offset) const; 223 224 bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset, 225 bool &Imm) const; 226 SDValue Expand32BitAddress(SDValue Addr) const; 227 bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset, 228 bool &Imm) const; 229 bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 230 bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 231 bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 232 bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const; 233 bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const; 234 bool SelectMOVRELOffset(SDValue Index, SDValue &Base, SDValue &Offset) const; 235 236 bool SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, SDValue &SrcMods) const; 237 bool SelectVOP3ModsImpl(SDValue In, SDValue &Src, unsigned &SrcMods, 238 bool AllowAbs = true) const; 239 bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 240 bool SelectVOP3BMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 241 bool SelectVOP3NoMods(SDValue In, SDValue &Src) const; 242 bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods, 243 SDValue &Clamp, SDValue &Omod) const; 244 bool SelectVOP3BMods0(SDValue In, SDValue &Src, SDValue &SrcMods, 245 SDValue &Clamp, SDValue &Omod) const; 246 bool SelectVOP3NoMods0(SDValue In, SDValue &Src, SDValue &SrcMods, 247 SDValue &Clamp, SDValue &Omod) const; 248 249 bool SelectVOP3OMods(SDValue In, SDValue &Src, 250 SDValue &Clamp, SDValue &Omod) const; 251 252 bool SelectVOP3PMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 253 254 bool SelectVOP3OpSel(SDValue In, SDValue &Src, SDValue &SrcMods) const; 255 256 bool SelectVOP3OpSelMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 257 bool SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src, unsigned &Mods) const; 258 bool SelectVOP3PMadMixMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 259 260 SDValue getHi16Elt(SDValue In) const; 261 262 SDValue getMaterializedScalarImm32(int64_t Val, const SDLoc &DL) const; 263 264 void SelectADD_SUB_I64(SDNode *N); 265 void SelectAddcSubb(SDNode *N); 266 void SelectUADDO_USUBO(SDNode *N); 267 void SelectDIV_SCALE(SDNode *N); 268 void SelectMAD_64_32(SDNode *N); 269 void SelectFMA_W_CHAIN(SDNode *N); 270 void SelectFMUL_W_CHAIN(SDNode *N); 271 272 SDNode *getS_BFE(unsigned Opcode, const SDLoc &DL, SDValue Val, 273 uint32_t Offset, uint32_t Width); 274 void SelectS_BFEFromShifts(SDNode *N); 275 void SelectS_BFE(SDNode *N); 276 bool isCBranchSCC(const SDNode *N) const; 277 void SelectBRCOND(SDNode *N); 278 void SelectFMAD_FMA(SDNode *N); 279 void SelectATOMIC_CMP_SWAP(SDNode *N); 280 void SelectDSAppendConsume(SDNode *N, unsigned IntrID); 281 void SelectDS_GWS(SDNode *N, unsigned IntrID); 282 void SelectInterpP1F16(SDNode *N); 283 void SelectINTRINSIC_W_CHAIN(SDNode *N); 284 void SelectINTRINSIC_WO_CHAIN(SDNode *N); 285 void SelectINTRINSIC_VOID(SDNode *N); 286 287 protected: 288 // Include the pieces autogenerated from the target description. 289 #include "AMDGPUGenDAGISel.inc" 290 }; 291 292 class R600DAGToDAGISel : public AMDGPUDAGToDAGISel { 293 const R600Subtarget *Subtarget; 294 295 bool isConstantLoad(const MemSDNode *N, int cbID) const; 296 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr); 297 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg, 298 SDValue& Offset); 299 public: 300 explicit R600DAGToDAGISel(TargetMachine *TM, CodeGenOpt::Level OptLevel) : 301 AMDGPUDAGToDAGISel(TM, OptLevel) {} 302 303 void Select(SDNode *N) override; 304 305 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, 306 SDValue &Offset) override; 307 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, 308 SDValue &Offset) override; 309 310 bool runOnMachineFunction(MachineFunction &MF) override; 311 312 void PreprocessISelDAG() override {} 313 314 protected: 315 // Include the pieces autogenerated from the target description. 316 #include "R600GenDAGISel.inc" 317 }; 318 319 static SDValue stripBitcast(SDValue Val) { 320 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val; 321 } 322 323 // Figure out if this is really an extract of the high 16-bits of a dword. 324 static bool isExtractHiElt(SDValue In, SDValue &Out) { 325 In = stripBitcast(In); 326 if (In.getOpcode() != ISD::TRUNCATE) 327 return false; 328 329 SDValue Srl = In.getOperand(0); 330 if (Srl.getOpcode() == ISD::SRL) { 331 if (ConstantSDNode *ShiftAmt = dyn_cast<ConstantSDNode>(Srl.getOperand(1))) { 332 if (ShiftAmt->getZExtValue() == 16) { 333 Out = stripBitcast(Srl.getOperand(0)); 334 return true; 335 } 336 } 337 } 338 339 return false; 340 } 341 342 // Look through operations that obscure just looking at the low 16-bits of the 343 // same register. 344 static SDValue stripExtractLoElt(SDValue In) { 345 if (In.getOpcode() == ISD::TRUNCATE) { 346 SDValue Src = In.getOperand(0); 347 if (Src.getValueType().getSizeInBits() == 32) 348 return stripBitcast(Src); 349 } 350 351 return In; 352 } 353 354 } // end anonymous namespace 355 356 INITIALIZE_PASS_BEGIN(AMDGPUDAGToDAGISel, "amdgpu-isel", 357 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false) 358 INITIALIZE_PASS_DEPENDENCY(AMDGPUArgumentUsageInfo) 359 INITIALIZE_PASS_DEPENDENCY(AMDGPUPerfHintAnalysis) 360 INITIALIZE_PASS_DEPENDENCY(LegacyDivergenceAnalysis) 361 #ifdef EXPENSIVE_CHECKS 362 INITIALIZE_PASS_DEPENDENCY(DominatorTreeWrapperPass) 363 INITIALIZE_PASS_DEPENDENCY(LoopInfoWrapperPass) 364 #endif 365 INITIALIZE_PASS_END(AMDGPUDAGToDAGISel, "amdgpu-isel", 366 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false) 367 368 /// This pass converts a legalized DAG into a AMDGPU-specific 369 // DAG, ready for instruction scheduling. 370 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine *TM, 371 CodeGenOpt::Level OptLevel) { 372 return new AMDGPUDAGToDAGISel(TM, OptLevel); 373 } 374 375 /// This pass converts a legalized DAG into a R600-specific 376 // DAG, ready for instruction scheduling. 377 FunctionPass *llvm::createR600ISelDag(TargetMachine *TM, 378 CodeGenOpt::Level OptLevel) { 379 return new R600DAGToDAGISel(TM, OptLevel); 380 } 381 382 bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) { 383 #ifdef EXPENSIVE_CHECKS 384 DominatorTree & DT = getAnalysis<DominatorTreeWrapperPass>().getDomTree(); 385 LoopInfo * LI = &getAnalysis<LoopInfoWrapperPass>().getLoopInfo(); 386 for (auto &L : LI->getLoopsInPreorder()) { 387 assert(L->isLCSSAForm(DT)); 388 } 389 #endif 390 Subtarget = &MF.getSubtarget<GCNSubtarget>(); 391 Mode = AMDGPU::SIModeRegisterDefaults(MF.getFunction()); 392 return SelectionDAGISel::runOnMachineFunction(MF); 393 } 394 395 bool AMDGPUDAGToDAGISel::matchLoadD16FromBuildVector(SDNode *N) const { 396 assert(Subtarget->d16PreservesUnusedBits()); 397 MVT VT = N->getValueType(0).getSimpleVT(); 398 if (VT != MVT::v2i16 && VT != MVT::v2f16) 399 return false; 400 401 SDValue Lo = N->getOperand(0); 402 SDValue Hi = N->getOperand(1); 403 404 LoadSDNode *LdHi = dyn_cast<LoadSDNode>(stripBitcast(Hi)); 405 406 // build_vector lo, (load ptr) -> load_d16_hi ptr, lo 407 // build_vector lo, (zextload ptr from i8) -> load_d16_hi_u8 ptr, lo 408 // build_vector lo, (sextload ptr from i8) -> load_d16_hi_i8 ptr, lo 409 410 // Need to check for possible indirect dependencies on the other half of the 411 // vector to avoid introducing a cycle. 412 if (LdHi && Hi.hasOneUse() && !LdHi->isPredecessorOf(Lo.getNode())) { 413 SDVTList VTList = CurDAG->getVTList(VT, MVT::Other); 414 415 SDValue TiedIn = CurDAG->getNode(ISD::SCALAR_TO_VECTOR, SDLoc(N), VT, Lo); 416 SDValue Ops[] = { 417 LdHi->getChain(), LdHi->getBasePtr(), TiedIn 418 }; 419 420 unsigned LoadOp = AMDGPUISD::LOAD_D16_HI; 421 if (LdHi->getMemoryVT() == MVT::i8) { 422 LoadOp = LdHi->getExtensionType() == ISD::SEXTLOAD ? 423 AMDGPUISD::LOAD_D16_HI_I8 : AMDGPUISD::LOAD_D16_HI_U8; 424 } else { 425 assert(LdHi->getMemoryVT() == MVT::i16); 426 } 427 428 SDValue NewLoadHi = 429 CurDAG->getMemIntrinsicNode(LoadOp, SDLoc(LdHi), VTList, 430 Ops, LdHi->getMemoryVT(), 431 LdHi->getMemOperand()); 432 433 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), NewLoadHi); 434 CurDAG->ReplaceAllUsesOfValueWith(SDValue(LdHi, 1), NewLoadHi.getValue(1)); 435 return true; 436 } 437 438 // build_vector (load ptr), hi -> load_d16_lo ptr, hi 439 // build_vector (zextload ptr from i8), hi -> load_d16_lo_u8 ptr, hi 440 // build_vector (sextload ptr from i8), hi -> load_d16_lo_i8 ptr, hi 441 LoadSDNode *LdLo = dyn_cast<LoadSDNode>(stripBitcast(Lo)); 442 if (LdLo && Lo.hasOneUse()) { 443 SDValue TiedIn = getHi16Elt(Hi); 444 if (!TiedIn || LdLo->isPredecessorOf(TiedIn.getNode())) 445 return false; 446 447 SDVTList VTList = CurDAG->getVTList(VT, MVT::Other); 448 unsigned LoadOp = AMDGPUISD::LOAD_D16_LO; 449 if (LdLo->getMemoryVT() == MVT::i8) { 450 LoadOp = LdLo->getExtensionType() == ISD::SEXTLOAD ? 451 AMDGPUISD::LOAD_D16_LO_I8 : AMDGPUISD::LOAD_D16_LO_U8; 452 } else { 453 assert(LdLo->getMemoryVT() == MVT::i16); 454 } 455 456 TiedIn = CurDAG->getNode(ISD::BITCAST, SDLoc(N), VT, TiedIn); 457 458 SDValue Ops[] = { 459 LdLo->getChain(), LdLo->getBasePtr(), TiedIn 460 }; 461 462 SDValue NewLoadLo = 463 CurDAG->getMemIntrinsicNode(LoadOp, SDLoc(LdLo), VTList, 464 Ops, LdLo->getMemoryVT(), 465 LdLo->getMemOperand()); 466 467 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), NewLoadLo); 468 CurDAG->ReplaceAllUsesOfValueWith(SDValue(LdLo, 1), NewLoadLo.getValue(1)); 469 return true; 470 } 471 472 return false; 473 } 474 475 void AMDGPUDAGToDAGISel::PreprocessISelDAG() { 476 if (!Subtarget->d16PreservesUnusedBits()) 477 return; 478 479 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_end(); 480 481 bool MadeChange = false; 482 while (Position != CurDAG->allnodes_begin()) { 483 SDNode *N = &*--Position; 484 if (N->use_empty()) 485 continue; 486 487 switch (N->getOpcode()) { 488 case ISD::BUILD_VECTOR: 489 MadeChange |= matchLoadD16FromBuildVector(N); 490 break; 491 default: 492 break; 493 } 494 } 495 496 if (MadeChange) { 497 CurDAG->RemoveDeadNodes(); 498 LLVM_DEBUG(dbgs() << "After PreProcess:\n"; 499 CurDAG->dump();); 500 } 501 } 502 503 bool AMDGPUDAGToDAGISel::isNoNanSrc(SDValue N) const { 504 if (TM.Options.NoNaNsFPMath) 505 return true; 506 507 // TODO: Move into isKnownNeverNaN 508 if (N->getFlags().hasNoNaNs()) 509 return true; 510 511 return CurDAG->isKnownNeverNaN(N); 512 } 513 514 bool AMDGPUDAGToDAGISel::isInlineImmediate(const SDNode *N, 515 bool Negated) const { 516 if (N->isUndef()) 517 return true; 518 519 const SIInstrInfo *TII = Subtarget->getInstrInfo(); 520 if (Negated) { 521 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) 522 return TII->isInlineConstant(-C->getAPIntValue()); 523 524 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) 525 return TII->isInlineConstant(-C->getValueAPF().bitcastToAPInt()); 526 527 } else { 528 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) 529 return TII->isInlineConstant(C->getAPIntValue()); 530 531 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) 532 return TII->isInlineConstant(C->getValueAPF().bitcastToAPInt()); 533 } 534 535 return false; 536 } 537 538 /// Determine the register class for \p OpNo 539 /// \returns The register class of the virtual register that will be used for 540 /// the given operand number \OpNo or NULL if the register class cannot be 541 /// determined. 542 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N, 543 unsigned OpNo) const { 544 if (!N->isMachineOpcode()) { 545 if (N->getOpcode() == ISD::CopyToReg) { 546 Register Reg = cast<RegisterSDNode>(N->getOperand(1))->getReg(); 547 if (Reg.isVirtual()) { 548 MachineRegisterInfo &MRI = CurDAG->getMachineFunction().getRegInfo(); 549 return MRI.getRegClass(Reg); 550 } 551 552 const SIRegisterInfo *TRI 553 = static_cast<const GCNSubtarget *>(Subtarget)->getRegisterInfo(); 554 return TRI->getPhysRegClass(Reg); 555 } 556 557 return nullptr; 558 } 559 560 switch (N->getMachineOpcode()) { 561 default: { 562 const MCInstrDesc &Desc = 563 Subtarget->getInstrInfo()->get(N->getMachineOpcode()); 564 unsigned OpIdx = Desc.getNumDefs() + OpNo; 565 if (OpIdx >= Desc.getNumOperands()) 566 return nullptr; 567 int RegClass = Desc.OpInfo[OpIdx].RegClass; 568 if (RegClass == -1) 569 return nullptr; 570 571 return Subtarget->getRegisterInfo()->getRegClass(RegClass); 572 } 573 case AMDGPU::REG_SEQUENCE: { 574 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 575 const TargetRegisterClass *SuperRC = 576 Subtarget->getRegisterInfo()->getRegClass(RCID); 577 578 SDValue SubRegOp = N->getOperand(OpNo + 1); 579 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue(); 580 return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC, 581 SubRegIdx); 582 } 583 } 584 } 585 586 SDNode *AMDGPUDAGToDAGISel::glueCopyToOp(SDNode *N, SDValue NewChain, 587 SDValue Glue) const { 588 SmallVector <SDValue, 8> Ops; 589 Ops.push_back(NewChain); // Replace the chain. 590 for (unsigned i = 1, e = N->getNumOperands(); i != e; ++i) 591 Ops.push_back(N->getOperand(i)); 592 593 Ops.push_back(Glue); 594 return CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops); 595 } 596 597 SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N, SDValue Val) const { 598 const SITargetLowering& Lowering = 599 *static_cast<const SITargetLowering*>(getTargetLowering()); 600 601 assert(N->getOperand(0).getValueType() == MVT::Other && "Expected chain"); 602 603 SDValue M0 = Lowering.copyToM0(*CurDAG, N->getOperand(0), SDLoc(N), Val); 604 return glueCopyToOp(N, M0, M0.getValue(1)); 605 } 606 607 SDNode *AMDGPUDAGToDAGISel::glueCopyToM0LDSInit(SDNode *N) const { 608 unsigned AS = cast<MemSDNode>(N)->getAddressSpace(); 609 if (AS == AMDGPUAS::LOCAL_ADDRESS) { 610 if (Subtarget->ldsRequiresM0Init()) 611 return glueCopyToM0(N, CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32)); 612 } else if (AS == AMDGPUAS::REGION_ADDRESS) { 613 MachineFunction &MF = CurDAG->getMachineFunction(); 614 unsigned Value = MF.getInfo<SIMachineFunctionInfo>()->getGDSSize(); 615 return 616 glueCopyToM0(N, CurDAG->getTargetConstant(Value, SDLoc(N), MVT::i32)); 617 } 618 return N; 619 } 620 621 MachineSDNode *AMDGPUDAGToDAGISel::buildSMovImm64(SDLoc &DL, uint64_t Imm, 622 EVT VT) const { 623 SDNode *Lo = CurDAG->getMachineNode( 624 AMDGPU::S_MOV_B32, DL, MVT::i32, 625 CurDAG->getTargetConstant(Imm & 0xFFFFFFFF, DL, MVT::i32)); 626 SDNode *Hi = 627 CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 628 CurDAG->getTargetConstant(Imm >> 32, DL, MVT::i32)); 629 const SDValue Ops[] = { 630 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), 631 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 632 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32)}; 633 634 return CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, VT, Ops); 635 } 636 637 void AMDGPUDAGToDAGISel::SelectBuildVector(SDNode *N, unsigned RegClassID) { 638 EVT VT = N->getValueType(0); 639 unsigned NumVectorElts = VT.getVectorNumElements(); 640 EVT EltVT = VT.getVectorElementType(); 641 SDLoc DL(N); 642 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); 643 644 if (NumVectorElts == 1) { 645 CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, N->getOperand(0), 646 RegClass); 647 return; 648 } 649 650 assert(NumVectorElts <= 32 && "Vectors with more than 32 elements not " 651 "supported yet"); 652 // 32 = Max Num Vector Elements 653 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index) 654 // 1 = Vector Register Class 655 SmallVector<SDValue, 32 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1); 656 657 bool IsGCN = CurDAG->getSubtarget().getTargetTriple().getArch() == 658 Triple::amdgcn; 659 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); 660 bool IsRegSeq = true; 661 unsigned NOps = N->getNumOperands(); 662 for (unsigned i = 0; i < NOps; i++) { 663 // XXX: Why is this here? 664 if (isa<RegisterSDNode>(N->getOperand(i))) { 665 IsRegSeq = false; 666 break; 667 } 668 unsigned Sub = IsGCN ? SIRegisterInfo::getSubRegFromChannel(i) 669 : R600RegisterInfo::getSubRegFromChannel(i); 670 RegSeqArgs[1 + (2 * i)] = N->getOperand(i); 671 RegSeqArgs[1 + (2 * i) + 1] = CurDAG->getTargetConstant(Sub, DL, MVT::i32); 672 } 673 if (NOps != NumVectorElts) { 674 // Fill in the missing undef elements if this was a scalar_to_vector. 675 assert(N->getOpcode() == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts); 676 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, 677 DL, EltVT); 678 for (unsigned i = NOps; i < NumVectorElts; ++i) { 679 unsigned Sub = IsGCN ? SIRegisterInfo::getSubRegFromChannel(i) 680 : R600RegisterInfo::getSubRegFromChannel(i); 681 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0); 682 RegSeqArgs[1 + (2 * i) + 1] = 683 CurDAG->getTargetConstant(Sub, DL, MVT::i32); 684 } 685 } 686 687 if (!IsRegSeq) 688 SelectCode(N); 689 CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), RegSeqArgs); 690 } 691 692 void AMDGPUDAGToDAGISel::Select(SDNode *N) { 693 unsigned int Opc = N->getOpcode(); 694 if (N->isMachineOpcode()) { 695 N->setNodeId(-1); 696 return; // Already selected. 697 } 698 699 // isa<MemSDNode> almost works but is slightly too permissive for some DS 700 // intrinsics. 701 if (Opc == ISD::LOAD || Opc == ISD::STORE || isa<AtomicSDNode>(N) || 702 (Opc == AMDGPUISD::ATOMIC_INC || Opc == AMDGPUISD::ATOMIC_DEC || 703 Opc == ISD::ATOMIC_LOAD_FADD || 704 Opc == AMDGPUISD::ATOMIC_LOAD_FMIN || 705 Opc == AMDGPUISD::ATOMIC_LOAD_FMAX)) { 706 N = glueCopyToM0LDSInit(N); 707 SelectCode(N); 708 return; 709 } 710 711 switch (Opc) { 712 default: 713 break; 714 // We are selecting i64 ADD here instead of custom lower it during 715 // DAG legalization, so we can fold some i64 ADDs used for address 716 // calculation into the LOAD and STORE instructions. 717 case ISD::ADDC: 718 case ISD::ADDE: 719 case ISD::SUBC: 720 case ISD::SUBE: { 721 if (N->getValueType(0) != MVT::i64) 722 break; 723 724 SelectADD_SUB_I64(N); 725 return; 726 } 727 case ISD::ADDCARRY: 728 case ISD::SUBCARRY: 729 if (N->getValueType(0) != MVT::i32) 730 break; 731 732 SelectAddcSubb(N); 733 return; 734 case ISD::UADDO: 735 case ISD::USUBO: { 736 SelectUADDO_USUBO(N); 737 return; 738 } 739 case AMDGPUISD::FMUL_W_CHAIN: { 740 SelectFMUL_W_CHAIN(N); 741 return; 742 } 743 case AMDGPUISD::FMA_W_CHAIN: { 744 SelectFMA_W_CHAIN(N); 745 return; 746 } 747 748 case ISD::SCALAR_TO_VECTOR: 749 case ISD::BUILD_VECTOR: { 750 EVT VT = N->getValueType(0); 751 unsigned NumVectorElts = VT.getVectorNumElements(); 752 if (VT.getScalarSizeInBits() == 16) { 753 if (Opc == ISD::BUILD_VECTOR && NumVectorElts == 2) { 754 if (SDNode *Packed = packConstantV2I16(N, *CurDAG)) { 755 ReplaceNode(N, Packed); 756 return; 757 } 758 } 759 760 break; 761 } 762 763 assert(VT.getVectorElementType().bitsEq(MVT::i32)); 764 unsigned RegClassID = 765 SIRegisterInfo::getSGPRClassForBitWidth(NumVectorElts * 32)->getID(); 766 SelectBuildVector(N, RegClassID); 767 return; 768 } 769 case ISD::BUILD_PAIR: { 770 SDValue RC, SubReg0, SubReg1; 771 SDLoc DL(N); 772 if (N->getValueType(0) == MVT::i128) { 773 RC = CurDAG->getTargetConstant(AMDGPU::SGPR_128RegClassID, DL, MVT::i32); 774 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32); 775 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32); 776 } else if (N->getValueType(0) == MVT::i64) { 777 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32); 778 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); 779 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); 780 } else { 781 llvm_unreachable("Unhandled value type for BUILD_PAIR"); 782 } 783 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0, 784 N->getOperand(1), SubReg1 }; 785 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, 786 N->getValueType(0), Ops)); 787 return; 788 } 789 790 case ISD::Constant: 791 case ISD::ConstantFP: { 792 if (N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N)) 793 break; 794 795 uint64_t Imm; 796 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N)) 797 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue(); 798 else { 799 ConstantSDNode *C = cast<ConstantSDNode>(N); 800 Imm = C->getZExtValue(); 801 } 802 803 SDLoc DL(N); 804 ReplaceNode(N, buildSMovImm64(DL, Imm, N->getValueType(0))); 805 return; 806 } 807 case AMDGPUISD::BFE_I32: 808 case AMDGPUISD::BFE_U32: { 809 // There is a scalar version available, but unlike the vector version which 810 // has a separate operand for the offset and width, the scalar version packs 811 // the width and offset into a single operand. Try to move to the scalar 812 // version if the offsets are constant, so that we can try to keep extended 813 // loads of kernel arguments in SGPRs. 814 815 // TODO: Technically we could try to pattern match scalar bitshifts of 816 // dynamic values, but it's probably not useful. 817 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 818 if (!Offset) 819 break; 820 821 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); 822 if (!Width) 823 break; 824 825 bool Signed = Opc == AMDGPUISD::BFE_I32; 826 827 uint32_t OffsetVal = Offset->getZExtValue(); 828 uint32_t WidthVal = Width->getZExtValue(); 829 830 ReplaceNode(N, getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32, 831 SDLoc(N), N->getOperand(0), OffsetVal, WidthVal)); 832 return; 833 } 834 case AMDGPUISD::DIV_SCALE: { 835 SelectDIV_SCALE(N); 836 return; 837 } 838 case AMDGPUISD::MAD_I64_I32: 839 case AMDGPUISD::MAD_U64_U32: { 840 SelectMAD_64_32(N); 841 return; 842 } 843 case ISD::CopyToReg: { 844 const SITargetLowering& Lowering = 845 *static_cast<const SITargetLowering*>(getTargetLowering()); 846 N = Lowering.legalizeTargetIndependentNode(N, *CurDAG); 847 break; 848 } 849 case ISD::AND: 850 case ISD::SRL: 851 case ISD::SRA: 852 case ISD::SIGN_EXTEND_INREG: 853 if (N->getValueType(0) != MVT::i32) 854 break; 855 856 SelectS_BFE(N); 857 return; 858 case ISD::BRCOND: 859 SelectBRCOND(N); 860 return; 861 case ISD::FMAD: 862 case ISD::FMA: 863 SelectFMAD_FMA(N); 864 return; 865 case AMDGPUISD::ATOMIC_CMP_SWAP: 866 SelectATOMIC_CMP_SWAP(N); 867 return; 868 case AMDGPUISD::CVT_PKRTZ_F16_F32: 869 case AMDGPUISD::CVT_PKNORM_I16_F32: 870 case AMDGPUISD::CVT_PKNORM_U16_F32: 871 case AMDGPUISD::CVT_PK_U16_U32: 872 case AMDGPUISD::CVT_PK_I16_I32: { 873 // Hack around using a legal type if f16 is illegal. 874 if (N->getValueType(0) == MVT::i32) { 875 MVT NewVT = Opc == AMDGPUISD::CVT_PKRTZ_F16_F32 ? MVT::v2f16 : MVT::v2i16; 876 N = CurDAG->MorphNodeTo(N, N->getOpcode(), CurDAG->getVTList(NewVT), 877 { N->getOperand(0), N->getOperand(1) }); 878 SelectCode(N); 879 return; 880 } 881 882 break; 883 } 884 case ISD::INTRINSIC_W_CHAIN: { 885 SelectINTRINSIC_W_CHAIN(N); 886 return; 887 } 888 case ISD::INTRINSIC_WO_CHAIN: { 889 SelectINTRINSIC_WO_CHAIN(N); 890 return; 891 } 892 case ISD::INTRINSIC_VOID: { 893 SelectINTRINSIC_VOID(N); 894 return; 895 } 896 } 897 898 SelectCode(N); 899 } 900 901 bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const { 902 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock(); 903 const Instruction *Term = BB->getTerminator(); 904 return Term->getMetadata("amdgpu.uniform") || 905 Term->getMetadata("structurizecfg.uniform"); 906 } 907 908 static bool getBaseWithOffsetUsingSplitOR(SelectionDAG &DAG, SDValue Addr, 909 SDValue &N0, SDValue &N1) { 910 if (Addr.getValueType() == MVT::i64 && Addr.getOpcode() == ISD::BITCAST && 911 Addr.getOperand(0).getOpcode() == ISD::BUILD_VECTOR) { 912 // As we split 64-bit `or` earlier, it's complicated pattern to match, i.e. 913 // (i64 (bitcast (v2i32 (build_vector 914 // (or (extract_vector_elt V, 0), OFFSET), 915 // (extract_vector_elt V, 1))))) 916 SDValue Lo = Addr.getOperand(0).getOperand(0); 917 if (Lo.getOpcode() == ISD::OR && DAG.isBaseWithConstantOffset(Lo)) { 918 SDValue BaseLo = Lo.getOperand(0); 919 SDValue BaseHi = Addr.getOperand(0).getOperand(1); 920 // Check that split base (Lo and Hi) are extracted from the same one. 921 if (BaseLo.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 922 BaseHi.getOpcode() == ISD::EXTRACT_VECTOR_ELT && 923 BaseLo.getOperand(0) == BaseHi.getOperand(0) && 924 // Lo is statically extracted from index 0. 925 isa<ConstantSDNode>(BaseLo.getOperand(1)) && 926 BaseLo.getConstantOperandVal(1) == 0 && 927 // Hi is statically extracted from index 0. 928 isa<ConstantSDNode>(BaseHi.getOperand(1)) && 929 BaseHi.getConstantOperandVal(1) == 1) { 930 N0 = BaseLo.getOperand(0).getOperand(0); 931 N1 = Lo.getOperand(1); 932 return true; 933 } 934 } 935 } 936 return false; 937 } 938 939 bool AMDGPUDAGToDAGISel::isBaseWithConstantOffset64(SDValue Addr, SDValue &LHS, 940 SDValue &RHS) const { 941 if (CurDAG->isBaseWithConstantOffset(Addr)) { 942 LHS = Addr.getOperand(0); 943 RHS = Addr.getOperand(1); 944 return true; 945 } 946 947 if (getBaseWithOffsetUsingSplitOR(*CurDAG, Addr, LHS, RHS)) { 948 assert(LHS && RHS && isa<ConstantSDNode>(RHS)); 949 return true; 950 } 951 952 return false; 953 } 954 955 StringRef AMDGPUDAGToDAGISel::getPassName() const { 956 return "AMDGPU DAG->DAG Pattern Instruction Selection"; 957 } 958 959 //===----------------------------------------------------------------------===// 960 // Complex Patterns 961 //===----------------------------------------------------------------------===// 962 963 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base, 964 SDValue &Offset) { 965 return false; 966 } 967 968 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base, 969 SDValue &Offset) { 970 ConstantSDNode *C; 971 SDLoc DL(Addr); 972 973 if ((C = dyn_cast<ConstantSDNode>(Addr))) { 974 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32); 975 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 976 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) && 977 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) { 978 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32); 979 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 980 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) && 981 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) { 982 Base = Addr.getOperand(0); 983 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 984 } else { 985 Base = Addr; 986 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); 987 } 988 989 return true; 990 } 991 992 SDValue AMDGPUDAGToDAGISel::getMaterializedScalarImm32(int64_t Val, 993 const SDLoc &DL) const { 994 SDNode *Mov = CurDAG->getMachineNode( 995 AMDGPU::S_MOV_B32, DL, MVT::i32, 996 CurDAG->getTargetConstant(Val, DL, MVT::i32)); 997 return SDValue(Mov, 0); 998 } 999 1000 // FIXME: Should only handle addcarry/subcarry 1001 void AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) { 1002 SDLoc DL(N); 1003 SDValue LHS = N->getOperand(0); 1004 SDValue RHS = N->getOperand(1); 1005 1006 unsigned Opcode = N->getOpcode(); 1007 bool ConsumeCarry = (Opcode == ISD::ADDE || Opcode == ISD::SUBE); 1008 bool ProduceCarry = 1009 ConsumeCarry || Opcode == ISD::ADDC || Opcode == ISD::SUBC; 1010 bool IsAdd = Opcode == ISD::ADD || Opcode == ISD::ADDC || Opcode == ISD::ADDE; 1011 1012 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); 1013 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); 1014 1015 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 1016 DL, MVT::i32, LHS, Sub0); 1017 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 1018 DL, MVT::i32, LHS, Sub1); 1019 1020 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 1021 DL, MVT::i32, RHS, Sub0); 1022 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 1023 DL, MVT::i32, RHS, Sub1); 1024 1025 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue); 1026 1027 static const unsigned OpcMap[2][2][2] = { 1028 {{AMDGPU::S_SUB_U32, AMDGPU::S_ADD_U32}, 1029 {AMDGPU::V_SUB_CO_U32_e32, AMDGPU::V_ADD_CO_U32_e32}}, 1030 {{AMDGPU::S_SUBB_U32, AMDGPU::S_ADDC_U32}, 1031 {AMDGPU::V_SUBB_U32_e32, AMDGPU::V_ADDC_U32_e32}}}; 1032 1033 unsigned Opc = OpcMap[0][N->isDivergent()][IsAdd]; 1034 unsigned CarryOpc = OpcMap[1][N->isDivergent()][IsAdd]; 1035 1036 SDNode *AddLo; 1037 if (!ConsumeCarry) { 1038 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) }; 1039 AddLo = CurDAG->getMachineNode(Opc, DL, VTList, Args); 1040 } else { 1041 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0), N->getOperand(2) }; 1042 AddLo = CurDAG->getMachineNode(CarryOpc, DL, VTList, Args); 1043 } 1044 SDValue AddHiArgs[] = { 1045 SDValue(Hi0, 0), 1046 SDValue(Hi1, 0), 1047 SDValue(AddLo, 1) 1048 }; 1049 SDNode *AddHi = CurDAG->getMachineNode(CarryOpc, DL, VTList, AddHiArgs); 1050 1051 SDValue RegSequenceArgs[] = { 1052 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), 1053 SDValue(AddLo,0), 1054 Sub0, 1055 SDValue(AddHi,0), 1056 Sub1, 1057 }; 1058 SDNode *RegSequence = CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL, 1059 MVT::i64, RegSequenceArgs); 1060 1061 if (ProduceCarry) { 1062 // Replace the carry-use 1063 ReplaceUses(SDValue(N, 1), SDValue(AddHi, 1)); 1064 } 1065 1066 // Replace the remaining uses. 1067 ReplaceNode(N, RegSequence); 1068 } 1069 1070 void AMDGPUDAGToDAGISel::SelectAddcSubb(SDNode *N) { 1071 SDLoc DL(N); 1072 SDValue LHS = N->getOperand(0); 1073 SDValue RHS = N->getOperand(1); 1074 SDValue CI = N->getOperand(2); 1075 1076 if (N->isDivergent()) { 1077 unsigned Opc = N->getOpcode() == ISD::ADDCARRY ? AMDGPU::V_ADDC_U32_e64 1078 : AMDGPU::V_SUBB_U32_e64; 1079 CurDAG->SelectNodeTo( 1080 N, Opc, N->getVTList(), 1081 {LHS, RHS, CI, 1082 CurDAG->getTargetConstant(0, {}, MVT::i1) /*clamp bit*/}); 1083 } else { 1084 unsigned Opc = N->getOpcode() == ISD::ADDCARRY ? AMDGPU::S_ADD_CO_PSEUDO 1085 : AMDGPU::S_SUB_CO_PSEUDO; 1086 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), {LHS, RHS, CI}); 1087 } 1088 } 1089 1090 void AMDGPUDAGToDAGISel::SelectUADDO_USUBO(SDNode *N) { 1091 // The name of the opcodes are misleading. v_add_i32/v_sub_i32 have unsigned 1092 // carry out despite the _i32 name. These were renamed in VI to _U32. 1093 // FIXME: We should probably rename the opcodes here. 1094 bool IsAdd = N->getOpcode() == ISD::UADDO; 1095 bool IsVALU = N->isDivergent(); 1096 1097 for (SDNode::use_iterator UI = N->use_begin(), E = N->use_end(); UI != E; 1098 ++UI) 1099 if (UI.getUse().getResNo() == 1) { 1100 if ((IsAdd && (UI->getOpcode() != ISD::ADDCARRY)) || 1101 (!IsAdd && (UI->getOpcode() != ISD::SUBCARRY))) { 1102 IsVALU = true; 1103 break; 1104 } 1105 } 1106 1107 if (IsVALU) { 1108 unsigned Opc = IsAdd ? AMDGPU::V_ADD_CO_U32_e64 : AMDGPU::V_SUB_CO_U32_e64; 1109 1110 CurDAG->SelectNodeTo( 1111 N, Opc, N->getVTList(), 1112 {N->getOperand(0), N->getOperand(1), 1113 CurDAG->getTargetConstant(0, {}, MVT::i1) /*clamp bit*/}); 1114 } else { 1115 unsigned Opc = N->getOpcode() == ISD::UADDO ? AMDGPU::S_UADDO_PSEUDO 1116 : AMDGPU::S_USUBO_PSEUDO; 1117 1118 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), 1119 {N->getOperand(0), N->getOperand(1)}); 1120 } 1121 } 1122 1123 void AMDGPUDAGToDAGISel::SelectFMA_W_CHAIN(SDNode *N) { 1124 SDLoc SL(N); 1125 // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, omod 1126 SDValue Ops[10]; 1127 1128 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[6], Ops[7]); 1129 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]); 1130 SelectVOP3Mods(N->getOperand(3), Ops[5], Ops[4]); 1131 Ops[8] = N->getOperand(0); 1132 Ops[9] = N->getOperand(4); 1133 1134 CurDAG->SelectNodeTo(N, AMDGPU::V_FMA_F32_e64, N->getVTList(), Ops); 1135 } 1136 1137 void AMDGPUDAGToDAGISel::SelectFMUL_W_CHAIN(SDNode *N) { 1138 SDLoc SL(N); 1139 // src0_modifiers, src0, src1_modifiers, src1, clamp, omod 1140 SDValue Ops[8]; 1141 1142 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[4], Ops[5]); 1143 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]); 1144 Ops[6] = N->getOperand(0); 1145 Ops[7] = N->getOperand(3); 1146 1147 CurDAG->SelectNodeTo(N, AMDGPU::V_MUL_F32_e64, N->getVTList(), Ops); 1148 } 1149 1150 // We need to handle this here because tablegen doesn't support matching 1151 // instructions with multiple outputs. 1152 void AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) { 1153 SDLoc SL(N); 1154 EVT VT = N->getValueType(0); 1155 1156 assert(VT == MVT::f32 || VT == MVT::f64); 1157 1158 unsigned Opc 1159 = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64_e64 : AMDGPU::V_DIV_SCALE_F32_e64; 1160 1161 // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, 1162 // omod 1163 SDValue Ops[8]; 1164 SelectVOP3BMods0(N->getOperand(0), Ops[1], Ops[0], Ops[6], Ops[7]); 1165 SelectVOP3BMods(N->getOperand(1), Ops[3], Ops[2]); 1166 SelectVOP3BMods(N->getOperand(2), Ops[5], Ops[4]); 1167 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops); 1168 } 1169 1170 // We need to handle this here because tablegen doesn't support matching 1171 // instructions with multiple outputs. 1172 void AMDGPUDAGToDAGISel::SelectMAD_64_32(SDNode *N) { 1173 SDLoc SL(N); 1174 bool Signed = N->getOpcode() == AMDGPUISD::MAD_I64_I32; 1175 unsigned Opc = Signed ? AMDGPU::V_MAD_I64_I32_e64 : AMDGPU::V_MAD_U64_U32_e64; 1176 1177 SDValue Clamp = CurDAG->getTargetConstant(0, SL, MVT::i1); 1178 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2), 1179 Clamp }; 1180 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops); 1181 } 1182 1183 bool AMDGPUDAGToDAGISel::isDSOffsetLegal(SDValue Base, unsigned Offset) const { 1184 if (!isUInt<16>(Offset)) 1185 return false; 1186 1187 if (!Base || Subtarget->hasUsableDSOffset() || 1188 Subtarget->unsafeDSOffsetFoldingEnabled()) 1189 return true; 1190 1191 // On Southern Islands instruction with a negative base value and an offset 1192 // don't seem to work. 1193 return CurDAG->SignBitIsZero(Base); 1194 } 1195 1196 bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base, 1197 SDValue &Offset) const { 1198 SDLoc DL(Addr); 1199 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1200 SDValue N0 = Addr.getOperand(0); 1201 SDValue N1 = Addr.getOperand(1); 1202 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 1203 if (isDSOffsetLegal(N0, C1->getSExtValue())) { 1204 // (add n0, c0) 1205 Base = N0; 1206 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 1207 return true; 1208 } 1209 } else if (Addr.getOpcode() == ISD::SUB) { 1210 // sub C, x -> add (sub 0, x), C 1211 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { 1212 int64_t ByteOffset = C->getSExtValue(); 1213 if (isDSOffsetLegal(SDValue(), ByteOffset)) { 1214 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 1215 1216 // XXX - This is kind of hacky. Create a dummy sub node so we can check 1217 // the known bits in isDSOffsetLegal. We need to emit the selected node 1218 // here, so this is thrown away. 1219 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32, 1220 Zero, Addr.getOperand(1)); 1221 1222 if (isDSOffsetLegal(Sub, ByteOffset)) { 1223 SmallVector<SDValue, 3> Opnds; 1224 Opnds.push_back(Zero); 1225 Opnds.push_back(Addr.getOperand(1)); 1226 1227 // FIXME: Select to VOP3 version for with-carry. 1228 unsigned SubOp = AMDGPU::V_SUB_CO_U32_e32; 1229 if (Subtarget->hasAddNoCarry()) { 1230 SubOp = AMDGPU::V_SUB_U32_e64; 1231 Opnds.push_back( 1232 CurDAG->getTargetConstant(0, {}, MVT::i1)); // clamp bit 1233 } 1234 1235 MachineSDNode *MachineSub = 1236 CurDAG->getMachineNode(SubOp, DL, MVT::i32, Opnds); 1237 1238 Base = SDValue(MachineSub, 0); 1239 Offset = CurDAG->getTargetConstant(ByteOffset, DL, MVT::i16); 1240 return true; 1241 } 1242 } 1243 } 1244 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { 1245 // If we have a constant address, prefer to put the constant into the 1246 // offset. This can save moves to load the constant address since multiple 1247 // operations can share the zero base address register, and enables merging 1248 // into read2 / write2 instructions. 1249 1250 SDLoc DL(Addr); 1251 1252 if (isDSOffsetLegal(SDValue(), CAddr->getZExtValue())) { 1253 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 1254 MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 1255 DL, MVT::i32, Zero); 1256 Base = SDValue(MovZero, 0); 1257 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16); 1258 return true; 1259 } 1260 } 1261 1262 // default case 1263 Base = Addr; 1264 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16); 1265 return true; 1266 } 1267 1268 bool AMDGPUDAGToDAGISel::isDSOffset2Legal(SDValue Base, unsigned Offset0, 1269 unsigned Offset1, 1270 unsigned Size) const { 1271 if (Offset0 % Size != 0 || Offset1 % Size != 0) 1272 return false; 1273 if (!isUInt<8>(Offset0 / Size) || !isUInt<8>(Offset1 / Size)) 1274 return false; 1275 1276 if (!Base || Subtarget->hasUsableDSOffset() || 1277 Subtarget->unsafeDSOffsetFoldingEnabled()) 1278 return true; 1279 1280 // On Southern Islands instruction with a negative base value and an offset 1281 // don't seem to work. 1282 return CurDAG->SignBitIsZero(Base); 1283 } 1284 1285 // TODO: If offset is too big, put low 16-bit into offset. 1286 bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base, 1287 SDValue &Offset0, 1288 SDValue &Offset1) const { 1289 return SelectDSReadWrite2(Addr, Base, Offset0, Offset1, 4); 1290 } 1291 1292 bool AMDGPUDAGToDAGISel::SelectDS128Bit8ByteAligned(SDValue Addr, SDValue &Base, 1293 SDValue &Offset0, 1294 SDValue &Offset1) const { 1295 return SelectDSReadWrite2(Addr, Base, Offset0, Offset1, 8); 1296 } 1297 1298 bool AMDGPUDAGToDAGISel::SelectDSReadWrite2(SDValue Addr, SDValue &Base, 1299 SDValue &Offset0, SDValue &Offset1, 1300 unsigned Size) const { 1301 SDLoc DL(Addr); 1302 1303 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1304 SDValue N0 = Addr.getOperand(0); 1305 SDValue N1 = Addr.getOperand(1); 1306 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 1307 unsigned OffsetValue0 = C1->getZExtValue(); 1308 unsigned OffsetValue1 = OffsetValue0 + Size; 1309 1310 // (add n0, c0) 1311 if (isDSOffset2Legal(N0, OffsetValue0, OffsetValue1, Size)) { 1312 Base = N0; 1313 Offset0 = CurDAG->getTargetConstant(OffsetValue0 / Size, DL, MVT::i8); 1314 Offset1 = CurDAG->getTargetConstant(OffsetValue1 / Size, DL, MVT::i8); 1315 return true; 1316 } 1317 } else if (Addr.getOpcode() == ISD::SUB) { 1318 // sub C, x -> add (sub 0, x), C 1319 if (const ConstantSDNode *C = 1320 dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { 1321 unsigned OffsetValue0 = C->getZExtValue(); 1322 unsigned OffsetValue1 = OffsetValue0 + Size; 1323 1324 if (isDSOffset2Legal(SDValue(), OffsetValue0, OffsetValue1, Size)) { 1325 SDLoc DL(Addr); 1326 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 1327 1328 // XXX - This is kind of hacky. Create a dummy sub node so we can check 1329 // the known bits in isDSOffsetLegal. We need to emit the selected node 1330 // here, so this is thrown away. 1331 SDValue Sub = 1332 CurDAG->getNode(ISD::SUB, DL, MVT::i32, Zero, Addr.getOperand(1)); 1333 1334 if (isDSOffset2Legal(Sub, OffsetValue0, OffsetValue1, Size)) { 1335 SmallVector<SDValue, 3> Opnds; 1336 Opnds.push_back(Zero); 1337 Opnds.push_back(Addr.getOperand(1)); 1338 unsigned SubOp = AMDGPU::V_SUB_CO_U32_e32; 1339 if (Subtarget->hasAddNoCarry()) { 1340 SubOp = AMDGPU::V_SUB_U32_e64; 1341 Opnds.push_back( 1342 CurDAG->getTargetConstant(0, {}, MVT::i1)); // clamp bit 1343 } 1344 1345 MachineSDNode *MachineSub = CurDAG->getMachineNode( 1346 SubOp, DL, MVT::getIntegerVT(Size * 8), Opnds); 1347 1348 Base = SDValue(MachineSub, 0); 1349 Offset0 = CurDAG->getTargetConstant(OffsetValue0 / Size, DL, MVT::i8); 1350 Offset1 = CurDAG->getTargetConstant(OffsetValue1 / Size, DL, MVT::i8); 1351 return true; 1352 } 1353 } 1354 } 1355 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { 1356 unsigned OffsetValue0 = CAddr->getZExtValue(); 1357 unsigned OffsetValue1 = OffsetValue0 + Size; 1358 1359 if (isDSOffset2Legal(SDValue(), OffsetValue0, OffsetValue1, Size)) { 1360 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 1361 MachineSDNode *MovZero = 1362 CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, DL, MVT::i32, Zero); 1363 Base = SDValue(MovZero, 0); 1364 Offset0 = CurDAG->getTargetConstant(OffsetValue0 / Size, DL, MVT::i8); 1365 Offset1 = CurDAG->getTargetConstant(OffsetValue1 / Size, DL, MVT::i8); 1366 return true; 1367 } 1368 } 1369 1370 // default case 1371 1372 Base = Addr; 1373 Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8); 1374 Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8); 1375 return true; 1376 } 1377 1378 bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr, 1379 SDValue &VAddr, SDValue &SOffset, 1380 SDValue &Offset, SDValue &Offen, 1381 SDValue &Idxen, SDValue &Addr64, 1382 SDValue &GLC, SDValue &SLC, 1383 SDValue &TFE, SDValue &DLC, 1384 SDValue &SWZ) const { 1385 // Subtarget prefers to use flat instruction 1386 // FIXME: This should be a pattern predicate and not reach here 1387 if (Subtarget->useFlatForGlobal()) 1388 return false; 1389 1390 SDLoc DL(Addr); 1391 1392 if (!GLC.getNode()) 1393 GLC = CurDAG->getTargetConstant(0, DL, MVT::i1); 1394 if (!SLC.getNode()) 1395 SLC = CurDAG->getTargetConstant(0, DL, MVT::i1); 1396 TFE = CurDAG->getTargetConstant(0, DL, MVT::i1); 1397 DLC = CurDAG->getTargetConstant(0, DL, MVT::i1); 1398 SWZ = CurDAG->getTargetConstant(0, DL, MVT::i1); 1399 1400 Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1); 1401 Offen = CurDAG->getTargetConstant(0, DL, MVT::i1); 1402 Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1); 1403 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32); 1404 1405 ConstantSDNode *C1 = nullptr; 1406 SDValue N0 = Addr; 1407 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1408 C1 = cast<ConstantSDNode>(Addr.getOperand(1)); 1409 if (isUInt<32>(C1->getZExtValue())) 1410 N0 = Addr.getOperand(0); 1411 else 1412 C1 = nullptr; 1413 } 1414 1415 if (N0.getOpcode() == ISD::ADD) { 1416 // (add N2, N3) -> addr64, or 1417 // (add (add N2, N3), C1) -> addr64 1418 SDValue N2 = N0.getOperand(0); 1419 SDValue N3 = N0.getOperand(1); 1420 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); 1421 1422 if (N2->isDivergent()) { 1423 if (N3->isDivergent()) { 1424 // Both N2 and N3 are divergent. Use N0 (the result of the add) as the 1425 // addr64, and construct the resource from a 0 address. 1426 Ptr = SDValue(buildSMovImm64(DL, 0, MVT::v2i32), 0); 1427 VAddr = N0; 1428 } else { 1429 // N2 is divergent, N3 is not. 1430 Ptr = N3; 1431 VAddr = N2; 1432 } 1433 } else { 1434 // N2 is not divergent. 1435 Ptr = N2; 1436 VAddr = N3; 1437 } 1438 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1439 } else if (N0->isDivergent()) { 1440 // N0 is divergent. Use it as the addr64, and construct the resource from a 1441 // 0 address. 1442 Ptr = SDValue(buildSMovImm64(DL, 0, MVT::v2i32), 0); 1443 VAddr = N0; 1444 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); 1445 } else { 1446 // N0 -> offset, or 1447 // (N0 + C1) -> offset 1448 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32); 1449 Ptr = N0; 1450 } 1451 1452 if (!C1) { 1453 // No offset. 1454 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1455 return true; 1456 } 1457 1458 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue())) { 1459 // Legal offset for instruction. 1460 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 1461 return true; 1462 } 1463 1464 // Illegal offset, store it in soffset. 1465 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1466 SOffset = 1467 SDValue(CurDAG->getMachineNode( 1468 AMDGPU::S_MOV_B32, DL, MVT::i32, 1469 CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)), 1470 0); 1471 return true; 1472 } 1473 1474 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 1475 SDValue &VAddr, SDValue &SOffset, 1476 SDValue &Offset, SDValue &GLC, 1477 SDValue &SLC, SDValue &TFE, 1478 SDValue &DLC, SDValue &SWZ) const { 1479 SDValue Ptr, Offen, Idxen, Addr64; 1480 1481 // addr64 bit was removed for volcanic islands. 1482 // FIXME: This should be a pattern predicate and not reach here 1483 if (!Subtarget->hasAddr64()) 1484 return false; 1485 1486 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, 1487 GLC, SLC, TFE, DLC, SWZ)) 1488 return false; 1489 1490 ConstantSDNode *C = cast<ConstantSDNode>(Addr64); 1491 if (C->getSExtValue()) { 1492 SDLoc DL(Addr); 1493 1494 const SITargetLowering& Lowering = 1495 *static_cast<const SITargetLowering*>(getTargetLowering()); 1496 1497 SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0); 1498 return true; 1499 } 1500 1501 return false; 1502 } 1503 1504 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 1505 SDValue &VAddr, SDValue &SOffset, 1506 SDValue &Offset, 1507 SDValue &SLC) const { 1508 SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1); 1509 SDValue GLC, TFE, DLC, SWZ; 1510 1511 return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE, DLC, SWZ); 1512 } 1513 1514 static bool isStackPtrRelative(const MachinePointerInfo &PtrInfo) { 1515 auto PSV = PtrInfo.V.dyn_cast<const PseudoSourceValue *>(); 1516 return PSV && PSV->isStack(); 1517 } 1518 1519 std::pair<SDValue, SDValue> AMDGPUDAGToDAGISel::foldFrameIndex(SDValue N) const { 1520 SDLoc DL(N); 1521 1522 auto *FI = dyn_cast<FrameIndexSDNode>(N); 1523 SDValue TFI = 1524 FI ? CurDAG->getTargetFrameIndex(FI->getIndex(), FI->getValueType(0)) : N; 1525 1526 // We rebase the base address into an absolute stack address and hence 1527 // use constant 0 for soffset. 1528 return std::make_pair(TFI, CurDAG->getTargetConstant(0, DL, MVT::i32)); 1529 } 1530 1531 bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffen(SDNode *Parent, 1532 SDValue Addr, SDValue &Rsrc, 1533 SDValue &VAddr, SDValue &SOffset, 1534 SDValue &ImmOffset) const { 1535 1536 SDLoc DL(Addr); 1537 MachineFunction &MF = CurDAG->getMachineFunction(); 1538 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1539 1540 Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32); 1541 1542 if (ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { 1543 int64_t Imm = CAddr->getSExtValue(); 1544 const int64_t NullPtr = 1545 AMDGPUTargetMachine::getNullPointerValue(AMDGPUAS::PRIVATE_ADDRESS); 1546 // Don't fold null pointer. 1547 if (Imm != NullPtr) { 1548 SDValue HighBits = CurDAG->getTargetConstant(Imm & ~4095, DL, MVT::i32); 1549 MachineSDNode *MovHighBits = CurDAG->getMachineNode( 1550 AMDGPU::V_MOV_B32_e32, DL, MVT::i32, HighBits); 1551 VAddr = SDValue(MovHighBits, 0); 1552 1553 // In a call sequence, stores to the argument stack area are relative to the 1554 // stack pointer. 1555 const MachinePointerInfo &PtrInfo 1556 = cast<MemSDNode>(Parent)->getPointerInfo(); 1557 SOffset = isStackPtrRelative(PtrInfo) 1558 ? CurDAG->getRegister(Info->getStackPtrOffsetReg(), MVT::i32) 1559 : CurDAG->getTargetConstant(0, DL, MVT::i32); 1560 ImmOffset = CurDAG->getTargetConstant(Imm & 4095, DL, MVT::i16); 1561 return true; 1562 } 1563 } 1564 1565 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1566 // (add n0, c1) 1567 1568 SDValue N0 = Addr.getOperand(0); 1569 SDValue N1 = Addr.getOperand(1); 1570 1571 // Offsets in vaddr must be positive if range checking is enabled. 1572 // 1573 // The total computation of vaddr + soffset + offset must not overflow. If 1574 // vaddr is negative, even if offset is 0 the sgpr offset add will end up 1575 // overflowing. 1576 // 1577 // Prior to gfx9, MUBUF instructions with the vaddr offset enabled would 1578 // always perform a range check. If a negative vaddr base index was used, 1579 // this would fail the range check. The overall address computation would 1580 // compute a valid address, but this doesn't happen due to the range 1581 // check. For out-of-bounds MUBUF loads, a 0 is returned. 1582 // 1583 // Therefore it should be safe to fold any VGPR offset on gfx9 into the 1584 // MUBUF vaddr, but not on older subtargets which can only do this if the 1585 // sign bit is known 0. 1586 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 1587 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue()) && 1588 (!Subtarget->privateMemoryResourceIsRangeChecked() || 1589 CurDAG->SignBitIsZero(N0))) { 1590 std::tie(VAddr, SOffset) = foldFrameIndex(N0); 1591 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 1592 return true; 1593 } 1594 } 1595 1596 // (node) 1597 std::tie(VAddr, SOffset) = foldFrameIndex(Addr); 1598 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1599 return true; 1600 } 1601 1602 bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffset(SDNode *Parent, 1603 SDValue Addr, 1604 SDValue &SRsrc, 1605 SDValue &SOffset, 1606 SDValue &Offset) const { 1607 ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr); 1608 if (!CAddr || !SIInstrInfo::isLegalMUBUFImmOffset(CAddr->getZExtValue())) 1609 return false; 1610 1611 SDLoc DL(Addr); 1612 MachineFunction &MF = CurDAG->getMachineFunction(); 1613 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1614 1615 SRsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32); 1616 1617 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo(); 1618 1619 // FIXME: Get from MachinePointerInfo? We should only be using the frame 1620 // offset if we know this is in a call sequence. 1621 SOffset = isStackPtrRelative(PtrInfo) 1622 ? CurDAG->getRegister(Info->getStackPtrOffsetReg(), MVT::i32) 1623 : CurDAG->getTargetConstant(0, DL, MVT::i32); 1624 1625 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16); 1626 return true; 1627 } 1628 1629 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 1630 SDValue &SOffset, SDValue &Offset, 1631 SDValue &GLC, SDValue &SLC, 1632 SDValue &TFE, SDValue &DLC, 1633 SDValue &SWZ) const { 1634 SDValue Ptr, VAddr, Offen, Idxen, Addr64; 1635 const SIInstrInfo *TII = 1636 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); 1637 1638 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, 1639 GLC, SLC, TFE, DLC, SWZ)) 1640 return false; 1641 1642 if (!cast<ConstantSDNode>(Offen)->getSExtValue() && 1643 !cast<ConstantSDNode>(Idxen)->getSExtValue() && 1644 !cast<ConstantSDNode>(Addr64)->getSExtValue()) { 1645 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | 1646 APInt::getAllOnesValue(32).getZExtValue(); // Size 1647 SDLoc DL(Addr); 1648 1649 const SITargetLowering& Lowering = 1650 *static_cast<const SITargetLowering*>(getTargetLowering()); 1651 1652 SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0); 1653 return true; 1654 } 1655 return false; 1656 } 1657 1658 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 1659 SDValue &Soffset, SDValue &Offset 1660 ) const { 1661 SDValue GLC, SLC, TFE, DLC, SWZ; 1662 1663 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE, DLC, SWZ); 1664 } 1665 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 1666 SDValue &Soffset, SDValue &Offset, 1667 SDValue &SLC) const { 1668 SDValue GLC, TFE, DLC, SWZ; 1669 1670 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE, DLC, SWZ); 1671 } 1672 1673 // Find a load or store from corresponding pattern root. 1674 // Roots may be build_vector, bitconvert or their combinations. 1675 static MemSDNode* findMemSDNode(SDNode *N) { 1676 N = AMDGPUTargetLowering::stripBitcast(SDValue(N,0)).getNode(); 1677 if (MemSDNode *MN = dyn_cast<MemSDNode>(N)) 1678 return MN; 1679 assert(isa<BuildVectorSDNode>(N)); 1680 for (SDValue V : N->op_values()) 1681 if (MemSDNode *MN = 1682 dyn_cast<MemSDNode>(AMDGPUTargetLowering::stripBitcast(V))) 1683 return MN; 1684 llvm_unreachable("cannot find MemSDNode in the pattern!"); 1685 } 1686 1687 template <bool IsSigned> 1688 bool AMDGPUDAGToDAGISel::SelectFlatOffset(SDNode *N, 1689 SDValue Addr, 1690 SDValue &VAddr, 1691 SDValue &Offset) const { 1692 int64_t OffsetVal = 0; 1693 1694 unsigned AS = findMemSDNode(N)->getAddressSpace(); 1695 1696 if (Subtarget->hasFlatInstOffsets() && 1697 (!Subtarget->hasFlatSegmentOffsetBug() || 1698 AS != AMDGPUAS::FLAT_ADDRESS)) { 1699 SDValue N0, N1; 1700 if (isBaseWithConstantOffset64(Addr, N0, N1)) { 1701 uint64_t COffsetVal = cast<ConstantSDNode>(N1)->getSExtValue(); 1702 1703 const SIInstrInfo *TII = Subtarget->getInstrInfo(); 1704 if (TII->isLegalFLATOffset(COffsetVal, AS, IsSigned)) { 1705 Addr = N0; 1706 OffsetVal = COffsetVal; 1707 } else { 1708 // If the offset doesn't fit, put the low bits into the offset field and 1709 // add the rest. 1710 // 1711 // For a FLAT instruction the hardware decides whether to access 1712 // global/scratch/shared memory based on the high bits of vaddr, 1713 // ignoring the offset field, so we have to ensure that when we add 1714 // remainder to vaddr it still points into the same underlying object. 1715 // The easiest way to do that is to make sure that we split the offset 1716 // into two pieces that are both >= 0 or both <= 0. 1717 1718 SDLoc DL(N); 1719 uint64_t RemainderOffset; 1720 1721 std::tie(OffsetVal, RemainderOffset) 1722 = TII->splitFlatOffset(COffsetVal, AS, IsSigned); 1723 1724 SDValue AddOffsetLo = 1725 getMaterializedScalarImm32(Lo_32(RemainderOffset), DL); 1726 SDValue Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1); 1727 1728 if (Addr.getValueType().getSizeInBits() == 32) { 1729 SmallVector<SDValue, 3> Opnds; 1730 Opnds.push_back(N0); 1731 Opnds.push_back(AddOffsetLo); 1732 unsigned AddOp = AMDGPU::V_ADD_CO_U32_e32; 1733 if (Subtarget->hasAddNoCarry()) { 1734 AddOp = AMDGPU::V_ADD_U32_e64; 1735 Opnds.push_back(Clamp); 1736 } 1737 Addr = SDValue(CurDAG->getMachineNode(AddOp, DL, MVT::i32, Opnds), 0); 1738 } else { 1739 // TODO: Should this try to use a scalar add pseudo if the base address 1740 // is uniform and saddr is usable? 1741 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); 1742 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); 1743 1744 SDNode *N0Lo = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 1745 DL, MVT::i32, N0, Sub0); 1746 SDNode *N0Hi = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 1747 DL, MVT::i32, N0, Sub1); 1748 1749 SDValue AddOffsetHi = 1750 getMaterializedScalarImm32(Hi_32(RemainderOffset), DL); 1751 1752 SDVTList VTs = CurDAG->getVTList(MVT::i32, MVT::i1); 1753 1754 SDNode *Add = 1755 CurDAG->getMachineNode(AMDGPU::V_ADD_CO_U32_e64, DL, VTs, 1756 {AddOffsetLo, SDValue(N0Lo, 0), Clamp}); 1757 1758 SDNode *Addc = CurDAG->getMachineNode( 1759 AMDGPU::V_ADDC_U32_e64, DL, VTs, 1760 {AddOffsetHi, SDValue(N0Hi, 0), SDValue(Add, 1), Clamp}); 1761 1762 SDValue RegSequenceArgs[] = { 1763 CurDAG->getTargetConstant(AMDGPU::VReg_64RegClassID, DL, MVT::i32), 1764 SDValue(Add, 0), Sub0, SDValue(Addc, 0), Sub1}; 1765 1766 Addr = SDValue(CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL, 1767 MVT::i64, RegSequenceArgs), 1768 0); 1769 } 1770 } 1771 } 1772 } 1773 1774 VAddr = Addr; 1775 Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i16); 1776 return true; 1777 } 1778 1779 // If this matches zero_extend i32:x, return x 1780 static SDValue matchZExtFromI32(SDValue Op) { 1781 if (Op.getOpcode() != ISD::ZERO_EXTEND) 1782 return SDValue(); 1783 1784 SDValue ExtSrc = Op.getOperand(0); 1785 return (ExtSrc.getValueType() == MVT::i32) ? ExtSrc : SDValue(); 1786 } 1787 1788 // Match (64-bit SGPR base) + (zext vgpr offset) + sext(imm offset) 1789 bool AMDGPUDAGToDAGISel::SelectGlobalSAddr(SDNode *N, 1790 SDValue Addr, 1791 SDValue &SAddr, 1792 SDValue &VOffset, 1793 SDValue &Offset) const { 1794 int64_t ImmOffset = 0; 1795 1796 // Match the immediate offset first, which canonically is moved as low as 1797 // possible. 1798 1799 SDValue LHS, RHS; 1800 if (isBaseWithConstantOffset64(Addr, LHS, RHS)) { 1801 int64_t COffsetVal = cast<ConstantSDNode>(RHS)->getSExtValue(); 1802 const SIInstrInfo *TII = Subtarget->getInstrInfo(); 1803 1804 if (TII->isLegalFLATOffset(COffsetVal, AMDGPUAS::GLOBAL_ADDRESS, true)) { 1805 Addr = LHS; 1806 ImmOffset = COffsetVal; 1807 } else if (!LHS->isDivergent() && COffsetVal > 0) { 1808 SDLoc SL(N); 1809 // saddr + large_offset -> saddr + (voffset = large_offset & ~MaxOffset) + 1810 // (large_offset & MaxOffset); 1811 int64_t SplitImmOffset, RemainderOffset; 1812 std::tie(SplitImmOffset, RemainderOffset) 1813 = TII->splitFlatOffset(COffsetVal, AMDGPUAS::GLOBAL_ADDRESS, true); 1814 1815 if (isUInt<32>(RemainderOffset)) { 1816 SDNode *VMov = CurDAG->getMachineNode( 1817 AMDGPU::V_MOV_B32_e32, SL, MVT::i32, 1818 CurDAG->getTargetConstant(RemainderOffset, SDLoc(), MVT::i32)); 1819 VOffset = SDValue(VMov, 0); 1820 SAddr = LHS; 1821 Offset = CurDAG->getTargetConstant(SplitImmOffset, SDLoc(), MVT::i16); 1822 return true; 1823 } 1824 } 1825 } 1826 1827 // Match the variable offset. 1828 if (Addr.getOpcode() != ISD::ADD) { 1829 if (Addr->isDivergent() || Addr.getOpcode() == ISD::UNDEF || 1830 isa<ConstantSDNode>(Addr)) 1831 return false; 1832 1833 // It's cheaper to materialize a single 32-bit zero for vaddr than the two 1834 // moves required to copy a 64-bit SGPR to VGPR. 1835 SAddr = Addr; 1836 SDNode *VMov = CurDAG->getMachineNode( 1837 AMDGPU::V_MOV_B32_e32, SDLoc(Addr), MVT::i32, 1838 CurDAG->getTargetConstant(0, SDLoc(), MVT::i32)); 1839 VOffset = SDValue(VMov, 0); 1840 Offset = CurDAG->getTargetConstant(ImmOffset, SDLoc(), MVT::i16); 1841 return true; 1842 } 1843 1844 LHS = Addr.getOperand(0); 1845 RHS = Addr.getOperand(1); 1846 1847 if (!LHS->isDivergent()) { 1848 // add (i64 sgpr), (zero_extend (i32 vgpr)) 1849 if (SDValue ZextRHS = matchZExtFromI32(RHS)) { 1850 SAddr = LHS; 1851 VOffset = ZextRHS; 1852 } 1853 } 1854 1855 if (!SAddr && !RHS->isDivergent()) { 1856 // add (zero_extend (i32 vgpr)), (i64 sgpr) 1857 if (SDValue ZextLHS = matchZExtFromI32(LHS)) { 1858 SAddr = RHS; 1859 VOffset = ZextLHS; 1860 } 1861 } 1862 1863 if (!SAddr) 1864 return false; 1865 1866 Offset = CurDAG->getTargetConstant(ImmOffset, SDLoc(), MVT::i16); 1867 return true; 1868 } 1869 1870 // Match (32-bit SGPR base) + sext(imm offset) 1871 bool AMDGPUDAGToDAGISel::SelectScratchSAddr(SDNode *N, 1872 SDValue Addr, 1873 SDValue &SAddr, 1874 SDValue &Offset) const { 1875 if (Addr->isDivergent()) 1876 return false; 1877 1878 SAddr = Addr; 1879 int64_t COffsetVal = 0; 1880 1881 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1882 COffsetVal = cast<ConstantSDNode>(Addr.getOperand(1))->getSExtValue(); 1883 SAddr = Addr.getOperand(0); 1884 } 1885 1886 if (auto FI = dyn_cast<FrameIndexSDNode>(SAddr)) { 1887 SAddr = CurDAG->getTargetFrameIndex(FI->getIndex(), FI->getValueType(0)); 1888 } else if (SAddr.getOpcode() == ISD::ADD && 1889 isa<FrameIndexSDNode>(SAddr.getOperand(0))) { 1890 // Materialize this into a scalar move for scalar address to avoid 1891 // readfirstlane. 1892 auto FI = cast<FrameIndexSDNode>(SAddr.getOperand(0)); 1893 SDValue TFI = CurDAG->getTargetFrameIndex(FI->getIndex(), 1894 FI->getValueType(0)); 1895 SAddr = SDValue(CurDAG->getMachineNode(AMDGPU::S_ADD_U32, SDLoc(SAddr), 1896 MVT::i32, TFI, SAddr.getOperand(1)), 1897 0); 1898 } 1899 1900 const SIInstrInfo *TII = Subtarget->getInstrInfo(); 1901 1902 if (!TII->isLegalFLATOffset(COffsetVal, AMDGPUAS::PRIVATE_ADDRESS, true)) { 1903 int64_t RemainderOffset = COffsetVal; 1904 int64_t ImmField = 0; 1905 const unsigned NumBits = AMDGPU::getNumFlatOffsetBits(*Subtarget, true); 1906 // Use signed division by a power of two to truncate towards 0. 1907 int64_t D = 1LL << (NumBits - 1); 1908 RemainderOffset = (COffsetVal / D) * D; 1909 ImmField = COffsetVal - RemainderOffset; 1910 1911 assert(TII->isLegalFLATOffset(ImmField, AMDGPUAS::PRIVATE_ADDRESS, true)); 1912 assert(RemainderOffset + ImmField == COffsetVal); 1913 1914 COffsetVal = ImmField; 1915 1916 SDLoc DL(N); 1917 SDValue AddOffset = 1918 getMaterializedScalarImm32(Lo_32(RemainderOffset), DL); 1919 SAddr = SDValue(CurDAG->getMachineNode(AMDGPU::S_ADD_U32, DL, MVT::i32, 1920 SAddr, AddOffset), 0); 1921 } 1922 1923 Offset = CurDAG->getTargetConstant(COffsetVal, SDLoc(), MVT::i16); 1924 1925 return true; 1926 } 1927 1928 bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode, 1929 SDValue &Offset, bool &Imm) const { 1930 ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode); 1931 if (!C) { 1932 if (ByteOffsetNode.getValueType().isScalarInteger() && 1933 ByteOffsetNode.getValueType().getSizeInBits() == 32) { 1934 Offset = ByteOffsetNode; 1935 Imm = false; 1936 return true; 1937 } 1938 if (ByteOffsetNode.getOpcode() == ISD::ZERO_EXTEND) { 1939 if (ByteOffsetNode.getOperand(0).getValueType().getSizeInBits() == 32) { 1940 Offset = ByteOffsetNode.getOperand(0); 1941 Imm = false; 1942 return true; 1943 } 1944 } 1945 return false; 1946 } 1947 1948 SDLoc SL(ByteOffsetNode); 1949 // GFX9 and GFX10 have signed byte immediate offsets. 1950 int64_t ByteOffset = C->getSExtValue(); 1951 Optional<int64_t> EncodedOffset = 1952 AMDGPU::getSMRDEncodedOffset(*Subtarget, ByteOffset, false); 1953 if (EncodedOffset) { 1954 Offset = CurDAG->getTargetConstant(*EncodedOffset, SL, MVT::i32); 1955 Imm = true; 1956 return true; 1957 } 1958 1959 // SGPR and literal offsets are unsigned. 1960 if (ByteOffset < 0) 1961 return false; 1962 1963 EncodedOffset = AMDGPU::getSMRDEncodedLiteralOffset32(*Subtarget, ByteOffset); 1964 if (EncodedOffset) { 1965 Offset = CurDAG->getTargetConstant(*EncodedOffset, SL, MVT::i32); 1966 return true; 1967 } 1968 1969 if (!isUInt<32>(ByteOffset) && !isInt<32>(ByteOffset)) 1970 return false; 1971 1972 SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32); 1973 Offset = SDValue( 1974 CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, C32Bit), 0); 1975 1976 return true; 1977 } 1978 1979 SDValue AMDGPUDAGToDAGISel::Expand32BitAddress(SDValue Addr) const { 1980 if (Addr.getValueType() != MVT::i32) 1981 return Addr; 1982 1983 // Zero-extend a 32-bit address. 1984 SDLoc SL(Addr); 1985 1986 const MachineFunction &MF = CurDAG->getMachineFunction(); 1987 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1988 unsigned AddrHiVal = Info->get32BitAddressHighBits(); 1989 SDValue AddrHi = CurDAG->getTargetConstant(AddrHiVal, SL, MVT::i32); 1990 1991 const SDValue Ops[] = { 1992 CurDAG->getTargetConstant(AMDGPU::SReg_64_XEXECRegClassID, SL, MVT::i32), 1993 Addr, 1994 CurDAG->getTargetConstant(AMDGPU::sub0, SL, MVT::i32), 1995 SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, AddrHi), 1996 0), 1997 CurDAG->getTargetConstant(AMDGPU::sub1, SL, MVT::i32), 1998 }; 1999 2000 return SDValue(CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, SL, MVT::i64, 2001 Ops), 0); 2002 } 2003 2004 bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase, 2005 SDValue &Offset, bool &Imm) const { 2006 SDLoc SL(Addr); 2007 2008 // A 32-bit (address + offset) should not cause unsigned 32-bit integer 2009 // wraparound, because s_load instructions perform the addition in 64 bits. 2010 if ((Addr.getValueType() != MVT::i32 || 2011 Addr->getFlags().hasNoUnsignedWrap())) { 2012 SDValue N0, N1; 2013 // Extract the base and offset if possible. 2014 if (CurDAG->isBaseWithConstantOffset(Addr) || 2015 Addr.getOpcode() == ISD::ADD) { 2016 N0 = Addr.getOperand(0); 2017 N1 = Addr.getOperand(1); 2018 } else if (getBaseWithOffsetUsingSplitOR(*CurDAG, Addr, N0, N1)) { 2019 assert(N0 && N1 && isa<ConstantSDNode>(N1)); 2020 } 2021 if (N0 && N1) { 2022 if (SelectSMRDOffset(N1, Offset, Imm)) { 2023 SBase = Expand32BitAddress(N0); 2024 return true; 2025 } 2026 } 2027 } 2028 SBase = Expand32BitAddress(Addr); 2029 Offset = CurDAG->getTargetConstant(0, SL, MVT::i32); 2030 Imm = true; 2031 return true; 2032 } 2033 2034 bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase, 2035 SDValue &Offset) const { 2036 bool Imm = false; 2037 return SelectSMRD(Addr, SBase, Offset, Imm) && Imm; 2038 } 2039 2040 bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase, 2041 SDValue &Offset) const { 2042 2043 assert(Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS); 2044 2045 bool Imm = false; 2046 if (!SelectSMRD(Addr, SBase, Offset, Imm)) 2047 return false; 2048 2049 return !Imm && isa<ConstantSDNode>(Offset); 2050 } 2051 2052 bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase, 2053 SDValue &Offset) const { 2054 bool Imm = false; 2055 return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm && 2056 !isa<ConstantSDNode>(Offset); 2057 } 2058 2059 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr, 2060 SDValue &Offset) const { 2061 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr)) { 2062 // The immediate offset for S_BUFFER instructions is unsigned. 2063 if (auto Imm = 2064 AMDGPU::getSMRDEncodedOffset(*Subtarget, C->getZExtValue(), true)) { 2065 Offset = CurDAG->getTargetConstant(*Imm, SDLoc(Addr), MVT::i32); 2066 return true; 2067 } 2068 } 2069 2070 return false; 2071 } 2072 2073 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr, 2074 SDValue &Offset) const { 2075 assert(Subtarget->getGeneration() == AMDGPUSubtarget::SEA_ISLANDS); 2076 2077 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr)) { 2078 if (auto Imm = AMDGPU::getSMRDEncodedLiteralOffset32(*Subtarget, 2079 C->getZExtValue())) { 2080 Offset = CurDAG->getTargetConstant(*Imm, SDLoc(Addr), MVT::i32); 2081 return true; 2082 } 2083 } 2084 2085 return false; 2086 } 2087 2088 bool AMDGPUDAGToDAGISel::SelectMOVRELOffset(SDValue Index, 2089 SDValue &Base, 2090 SDValue &Offset) const { 2091 SDLoc DL(Index); 2092 2093 if (CurDAG->isBaseWithConstantOffset(Index)) { 2094 SDValue N0 = Index.getOperand(0); 2095 SDValue N1 = Index.getOperand(1); 2096 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 2097 2098 // (add n0, c0) 2099 // Don't peel off the offset (c0) if doing so could possibly lead 2100 // the base (n0) to be negative. 2101 // (or n0, |c0|) can never change a sign given isBaseWithConstantOffset. 2102 if (C1->getSExtValue() <= 0 || CurDAG->SignBitIsZero(N0) || 2103 (Index->getOpcode() == ISD::OR && C1->getSExtValue() >= 0)) { 2104 Base = N0; 2105 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32); 2106 return true; 2107 } 2108 } 2109 2110 if (isa<ConstantSDNode>(Index)) 2111 return false; 2112 2113 Base = Index; 2114 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); 2115 return true; 2116 } 2117 2118 SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, const SDLoc &DL, 2119 SDValue Val, uint32_t Offset, 2120 uint32_t Width) { 2121 // Transformation function, pack the offset and width of a BFE into 2122 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second 2123 // source, bits [5:0] contain the offset and bits [22:16] the width. 2124 uint32_t PackedVal = Offset | (Width << 16); 2125 SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32); 2126 2127 return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst); 2128 } 2129 2130 void AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) { 2131 // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c) 2132 // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c) 2133 // Predicate: 0 < b <= c < 32 2134 2135 const SDValue &Shl = N->getOperand(0); 2136 ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1)); 2137 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2138 2139 if (B && C) { 2140 uint32_t BVal = B->getZExtValue(); 2141 uint32_t CVal = C->getZExtValue(); 2142 2143 if (0 < BVal && BVal <= CVal && CVal < 32) { 2144 bool Signed = N->getOpcode() == ISD::SRA; 2145 unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32; 2146 2147 ReplaceNode(N, getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), CVal - BVal, 2148 32 - CVal)); 2149 return; 2150 } 2151 } 2152 SelectCode(N); 2153 } 2154 2155 void AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) { 2156 switch (N->getOpcode()) { 2157 case ISD::AND: 2158 if (N->getOperand(0).getOpcode() == ISD::SRL) { 2159 // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)" 2160 // Predicate: isMask(mask) 2161 const SDValue &Srl = N->getOperand(0); 2162 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1)); 2163 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2164 2165 if (Shift && Mask) { 2166 uint32_t ShiftVal = Shift->getZExtValue(); 2167 uint32_t MaskVal = Mask->getZExtValue(); 2168 2169 if (isMask_32(MaskVal)) { 2170 uint32_t WidthVal = countPopulation(MaskVal); 2171 2172 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), 2173 Srl.getOperand(0), ShiftVal, WidthVal)); 2174 return; 2175 } 2176 } 2177 } 2178 break; 2179 case ISD::SRL: 2180 if (N->getOperand(0).getOpcode() == ISD::AND) { 2181 // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)" 2182 // Predicate: isMask(mask >> b) 2183 const SDValue &And = N->getOperand(0); 2184 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1)); 2185 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1)); 2186 2187 if (Shift && Mask) { 2188 uint32_t ShiftVal = Shift->getZExtValue(); 2189 uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal; 2190 2191 if (isMask_32(MaskVal)) { 2192 uint32_t WidthVal = countPopulation(MaskVal); 2193 2194 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), 2195 And.getOperand(0), ShiftVal, WidthVal)); 2196 return; 2197 } 2198 } 2199 } else if (N->getOperand(0).getOpcode() == ISD::SHL) { 2200 SelectS_BFEFromShifts(N); 2201 return; 2202 } 2203 break; 2204 case ISD::SRA: 2205 if (N->getOperand(0).getOpcode() == ISD::SHL) { 2206 SelectS_BFEFromShifts(N); 2207 return; 2208 } 2209 break; 2210 2211 case ISD::SIGN_EXTEND_INREG: { 2212 // sext_inreg (srl x, 16), i8 -> bfe_i32 x, 16, 8 2213 SDValue Src = N->getOperand(0); 2214 if (Src.getOpcode() != ISD::SRL) 2215 break; 2216 2217 const ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(Src.getOperand(1)); 2218 if (!Amt) 2219 break; 2220 2221 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits(); 2222 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_I32, SDLoc(N), Src.getOperand(0), 2223 Amt->getZExtValue(), Width)); 2224 return; 2225 } 2226 } 2227 2228 SelectCode(N); 2229 } 2230 2231 bool AMDGPUDAGToDAGISel::isCBranchSCC(const SDNode *N) const { 2232 assert(N->getOpcode() == ISD::BRCOND); 2233 if (!N->hasOneUse()) 2234 return false; 2235 2236 SDValue Cond = N->getOperand(1); 2237 if (Cond.getOpcode() == ISD::CopyToReg) 2238 Cond = Cond.getOperand(2); 2239 2240 if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse()) 2241 return false; 2242 2243 MVT VT = Cond.getOperand(0).getSimpleValueType(); 2244 if (VT == MVT::i32) 2245 return true; 2246 2247 if (VT == MVT::i64) { 2248 auto ST = static_cast<const GCNSubtarget *>(Subtarget); 2249 2250 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); 2251 return (CC == ISD::SETEQ || CC == ISD::SETNE) && ST->hasScalarCompareEq64(); 2252 } 2253 2254 return false; 2255 } 2256 2257 void AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) { 2258 SDValue Cond = N->getOperand(1); 2259 2260 if (Cond.isUndef()) { 2261 CurDAG->SelectNodeTo(N, AMDGPU::SI_BR_UNDEF, MVT::Other, 2262 N->getOperand(2), N->getOperand(0)); 2263 return; 2264 } 2265 2266 const GCNSubtarget *ST = static_cast<const GCNSubtarget *>(Subtarget); 2267 const SIRegisterInfo *TRI = ST->getRegisterInfo(); 2268 2269 bool UseSCCBr = isCBranchSCC(N) && isUniformBr(N); 2270 unsigned BrOp = UseSCCBr ? AMDGPU::S_CBRANCH_SCC1 : AMDGPU::S_CBRANCH_VCCNZ; 2271 Register CondReg = UseSCCBr ? AMDGPU::SCC : TRI->getVCC(); 2272 SDLoc SL(N); 2273 2274 if (!UseSCCBr) { 2275 // This is the case that we are selecting to S_CBRANCH_VCCNZ. We have not 2276 // analyzed what generates the vcc value, so we do not know whether vcc 2277 // bits for disabled lanes are 0. Thus we need to mask out bits for 2278 // disabled lanes. 2279 // 2280 // For the case that we select S_CBRANCH_SCC1 and it gets 2281 // changed to S_CBRANCH_VCCNZ in SIFixSGPRCopies, SIFixSGPRCopies calls 2282 // SIInstrInfo::moveToVALU which inserts the S_AND). 2283 // 2284 // We could add an analysis of what generates the vcc value here and omit 2285 // the S_AND when is unnecessary. But it would be better to add a separate 2286 // pass after SIFixSGPRCopies to do the unnecessary S_AND removal, so it 2287 // catches both cases. 2288 Cond = SDValue(CurDAG->getMachineNode(ST->isWave32() ? AMDGPU::S_AND_B32 2289 : AMDGPU::S_AND_B64, 2290 SL, MVT::i1, 2291 CurDAG->getRegister(ST->isWave32() ? AMDGPU::EXEC_LO 2292 : AMDGPU::EXEC, 2293 MVT::i1), 2294 Cond), 2295 0); 2296 } 2297 2298 SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, CondReg, Cond); 2299 CurDAG->SelectNodeTo(N, BrOp, MVT::Other, 2300 N->getOperand(2), // Basic Block 2301 VCC.getValue(0)); 2302 } 2303 2304 void AMDGPUDAGToDAGISel::SelectFMAD_FMA(SDNode *N) { 2305 MVT VT = N->getSimpleValueType(0); 2306 bool IsFMA = N->getOpcode() == ISD::FMA; 2307 if (VT != MVT::f32 || (!Subtarget->hasMadMixInsts() && 2308 !Subtarget->hasFmaMixInsts()) || 2309 ((IsFMA && Subtarget->hasMadMixInsts()) || 2310 (!IsFMA && Subtarget->hasFmaMixInsts()))) { 2311 SelectCode(N); 2312 return; 2313 } 2314 2315 SDValue Src0 = N->getOperand(0); 2316 SDValue Src1 = N->getOperand(1); 2317 SDValue Src2 = N->getOperand(2); 2318 unsigned Src0Mods, Src1Mods, Src2Mods; 2319 2320 // Avoid using v_mad_mix_f32/v_fma_mix_f32 unless there is actually an operand 2321 // using the conversion from f16. 2322 bool Sel0 = SelectVOP3PMadMixModsImpl(Src0, Src0, Src0Mods); 2323 bool Sel1 = SelectVOP3PMadMixModsImpl(Src1, Src1, Src1Mods); 2324 bool Sel2 = SelectVOP3PMadMixModsImpl(Src2, Src2, Src2Mods); 2325 2326 assert((IsFMA || !Mode.allFP32Denormals()) && 2327 "fmad selected with denormals enabled"); 2328 // TODO: We can select this with f32 denormals enabled if all the sources are 2329 // converted from f16 (in which case fmad isn't legal). 2330 2331 if (Sel0 || Sel1 || Sel2) { 2332 // For dummy operands. 2333 SDValue Zero = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32); 2334 SDValue Ops[] = { 2335 CurDAG->getTargetConstant(Src0Mods, SDLoc(), MVT::i32), Src0, 2336 CurDAG->getTargetConstant(Src1Mods, SDLoc(), MVT::i32), Src1, 2337 CurDAG->getTargetConstant(Src2Mods, SDLoc(), MVT::i32), Src2, 2338 CurDAG->getTargetConstant(0, SDLoc(), MVT::i1), 2339 Zero, Zero 2340 }; 2341 2342 CurDAG->SelectNodeTo(N, 2343 IsFMA ? AMDGPU::V_FMA_MIX_F32 : AMDGPU::V_MAD_MIX_F32, 2344 MVT::f32, Ops); 2345 } else { 2346 SelectCode(N); 2347 } 2348 } 2349 2350 // This is here because there isn't a way to use the generated sub0_sub1 as the 2351 // subreg index to EXTRACT_SUBREG in tablegen. 2352 void AMDGPUDAGToDAGISel::SelectATOMIC_CMP_SWAP(SDNode *N) { 2353 MemSDNode *Mem = cast<MemSDNode>(N); 2354 unsigned AS = Mem->getAddressSpace(); 2355 if (AS == AMDGPUAS::FLAT_ADDRESS) { 2356 SelectCode(N); 2357 return; 2358 } 2359 2360 MVT VT = N->getSimpleValueType(0); 2361 bool Is32 = (VT == MVT::i32); 2362 SDLoc SL(N); 2363 2364 MachineSDNode *CmpSwap = nullptr; 2365 if (Subtarget->hasAddr64()) { 2366 SDValue SRsrc, VAddr, SOffset, Offset, SLC; 2367 2368 if (SelectMUBUFAddr64(Mem->getBasePtr(), SRsrc, VAddr, SOffset, Offset, SLC)) { 2369 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_ADDR64_RTN : 2370 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_ADDR64_RTN; 2371 SDValue CmpVal = Mem->getOperand(2); 2372 SDValue GLC = CurDAG->getTargetConstant(1, SL, MVT::i1); 2373 2374 // XXX - Do we care about glue operands? 2375 2376 SDValue Ops[] = { 2377 CmpVal, VAddr, SRsrc, SOffset, Offset, GLC, SLC, Mem->getChain() 2378 }; 2379 2380 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops); 2381 } 2382 } 2383 2384 if (!CmpSwap) { 2385 SDValue SRsrc, SOffset, Offset, SLC; 2386 if (SelectMUBUFOffset(Mem->getBasePtr(), SRsrc, SOffset, Offset, SLC)) { 2387 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_OFFSET_RTN : 2388 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_OFFSET_RTN; 2389 2390 SDValue CmpVal = Mem->getOperand(2); 2391 SDValue GLC = CurDAG->getTargetConstant(1, SL, MVT::i1); 2392 SDValue Ops[] = { 2393 CmpVal, SRsrc, SOffset, Offset, GLC, SLC, Mem->getChain() 2394 }; 2395 2396 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops); 2397 } 2398 } 2399 2400 if (!CmpSwap) { 2401 SelectCode(N); 2402 return; 2403 } 2404 2405 MachineMemOperand *MMO = Mem->getMemOperand(); 2406 CurDAG->setNodeMemRefs(CmpSwap, {MMO}); 2407 2408 unsigned SubReg = Is32 ? AMDGPU::sub0 : AMDGPU::sub0_sub1; 2409 SDValue Extract 2410 = CurDAG->getTargetExtractSubreg(SubReg, SL, VT, SDValue(CmpSwap, 0)); 2411 2412 ReplaceUses(SDValue(N, 0), Extract); 2413 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 1)); 2414 CurDAG->RemoveDeadNode(N); 2415 } 2416 2417 void AMDGPUDAGToDAGISel::SelectDSAppendConsume(SDNode *N, unsigned IntrID) { 2418 // The address is assumed to be uniform, so if it ends up in a VGPR, it will 2419 // be copied to an SGPR with readfirstlane. 2420 unsigned Opc = IntrID == Intrinsic::amdgcn_ds_append ? 2421 AMDGPU::DS_APPEND : AMDGPU::DS_CONSUME; 2422 2423 SDValue Chain = N->getOperand(0); 2424 SDValue Ptr = N->getOperand(2); 2425 MemIntrinsicSDNode *M = cast<MemIntrinsicSDNode>(N); 2426 MachineMemOperand *MMO = M->getMemOperand(); 2427 bool IsGDS = M->getAddressSpace() == AMDGPUAS::REGION_ADDRESS; 2428 2429 SDValue Offset; 2430 if (CurDAG->isBaseWithConstantOffset(Ptr)) { 2431 SDValue PtrBase = Ptr.getOperand(0); 2432 SDValue PtrOffset = Ptr.getOperand(1); 2433 2434 const APInt &OffsetVal = cast<ConstantSDNode>(PtrOffset)->getAPIntValue(); 2435 if (isDSOffsetLegal(PtrBase, OffsetVal.getZExtValue())) { 2436 N = glueCopyToM0(N, PtrBase); 2437 Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i32); 2438 } 2439 } 2440 2441 if (!Offset) { 2442 N = glueCopyToM0(N, Ptr); 2443 Offset = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32); 2444 } 2445 2446 SDValue Ops[] = { 2447 Offset, 2448 CurDAG->getTargetConstant(IsGDS, SDLoc(), MVT::i32), 2449 Chain, 2450 N->getOperand(N->getNumOperands() - 1) // New glue 2451 }; 2452 2453 SDNode *Selected = CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops); 2454 CurDAG->setNodeMemRefs(cast<MachineSDNode>(Selected), {MMO}); 2455 } 2456 2457 static unsigned gwsIntrinToOpcode(unsigned IntrID) { 2458 switch (IntrID) { 2459 case Intrinsic::amdgcn_ds_gws_init: 2460 return AMDGPU::DS_GWS_INIT; 2461 case Intrinsic::amdgcn_ds_gws_barrier: 2462 return AMDGPU::DS_GWS_BARRIER; 2463 case Intrinsic::amdgcn_ds_gws_sema_v: 2464 return AMDGPU::DS_GWS_SEMA_V; 2465 case Intrinsic::amdgcn_ds_gws_sema_br: 2466 return AMDGPU::DS_GWS_SEMA_BR; 2467 case Intrinsic::amdgcn_ds_gws_sema_p: 2468 return AMDGPU::DS_GWS_SEMA_P; 2469 case Intrinsic::amdgcn_ds_gws_sema_release_all: 2470 return AMDGPU::DS_GWS_SEMA_RELEASE_ALL; 2471 default: 2472 llvm_unreachable("not a gws intrinsic"); 2473 } 2474 } 2475 2476 void AMDGPUDAGToDAGISel::SelectDS_GWS(SDNode *N, unsigned IntrID) { 2477 if (IntrID == Intrinsic::amdgcn_ds_gws_sema_release_all && 2478 !Subtarget->hasGWSSemaReleaseAll()) { 2479 // Let this error. 2480 SelectCode(N); 2481 return; 2482 } 2483 2484 // Chain, intrinsic ID, vsrc, offset 2485 const bool HasVSrc = N->getNumOperands() == 4; 2486 assert(HasVSrc || N->getNumOperands() == 3); 2487 2488 SDLoc SL(N); 2489 SDValue BaseOffset = N->getOperand(HasVSrc ? 3 : 2); 2490 int ImmOffset = 0; 2491 MemIntrinsicSDNode *M = cast<MemIntrinsicSDNode>(N); 2492 MachineMemOperand *MMO = M->getMemOperand(); 2493 2494 // Don't worry if the offset ends up in a VGPR. Only one lane will have 2495 // effect, so SIFixSGPRCopies will validly insert readfirstlane. 2496 2497 // The resource id offset is computed as (<isa opaque base> + M0[21:16] + 2498 // offset field) % 64. Some versions of the programming guide omit the m0 2499 // part, or claim it's from offset 0. 2500 if (ConstantSDNode *ConstOffset = dyn_cast<ConstantSDNode>(BaseOffset)) { 2501 // If we have a constant offset, try to use the 0 in m0 as the base. 2502 // TODO: Look into changing the default m0 initialization value. If the 2503 // default -1 only set the low 16-bits, we could leave it as-is and add 1 to 2504 // the immediate offset. 2505 glueCopyToM0(N, CurDAG->getTargetConstant(0, SL, MVT::i32)); 2506 ImmOffset = ConstOffset->getZExtValue(); 2507 } else { 2508 if (CurDAG->isBaseWithConstantOffset(BaseOffset)) { 2509 ImmOffset = BaseOffset.getConstantOperandVal(1); 2510 BaseOffset = BaseOffset.getOperand(0); 2511 } 2512 2513 // Prefer to do the shift in an SGPR since it should be possible to use m0 2514 // as the result directly. If it's already an SGPR, it will be eliminated 2515 // later. 2516 SDNode *SGPROffset 2517 = CurDAG->getMachineNode(AMDGPU::V_READFIRSTLANE_B32, SL, MVT::i32, 2518 BaseOffset); 2519 // Shift to offset in m0 2520 SDNode *M0Base 2521 = CurDAG->getMachineNode(AMDGPU::S_LSHL_B32, SL, MVT::i32, 2522 SDValue(SGPROffset, 0), 2523 CurDAG->getTargetConstant(16, SL, MVT::i32)); 2524 glueCopyToM0(N, SDValue(M0Base, 0)); 2525 } 2526 2527 SDValue Chain = N->getOperand(0); 2528 SDValue OffsetField = CurDAG->getTargetConstant(ImmOffset, SL, MVT::i32); 2529 2530 const unsigned Opc = gwsIntrinToOpcode(IntrID); 2531 SmallVector<SDValue, 5> Ops; 2532 if (HasVSrc) 2533 Ops.push_back(N->getOperand(2)); 2534 Ops.push_back(OffsetField); 2535 Ops.push_back(Chain); 2536 2537 SDNode *Selected = CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops); 2538 CurDAG->setNodeMemRefs(cast<MachineSDNode>(Selected), {MMO}); 2539 } 2540 2541 void AMDGPUDAGToDAGISel::SelectInterpP1F16(SDNode *N) { 2542 if (Subtarget->getLDSBankCount() != 16) { 2543 // This is a single instruction with a pattern. 2544 SelectCode(N); 2545 return; 2546 } 2547 2548 SDLoc DL(N); 2549 2550 // This requires 2 instructions. It is possible to write a pattern to support 2551 // this, but the generated isel emitter doesn't correctly deal with multiple 2552 // output instructions using the same physical register input. The copy to m0 2553 // is incorrectly placed before the second instruction. 2554 // 2555 // TODO: Match source modifiers. 2556 // 2557 // def : Pat < 2558 // (int_amdgcn_interp_p1_f16 2559 // (VOP3Mods f32:$src0, i32:$src0_modifiers), 2560 // (i32 timm:$attrchan), (i32 timm:$attr), 2561 // (i1 timm:$high), M0), 2562 // (V_INTERP_P1LV_F16 $src0_modifiers, VGPR_32:$src0, timm:$attr, 2563 // timm:$attrchan, 0, 2564 // (V_INTERP_MOV_F32 2, timm:$attr, timm:$attrchan), timm:$high)> { 2565 // let Predicates = [has16BankLDS]; 2566 // } 2567 2568 // 16 bank LDS 2569 SDValue ToM0 = CurDAG->getCopyToReg(CurDAG->getEntryNode(), DL, AMDGPU::M0, 2570 N->getOperand(5), SDValue()); 2571 2572 SDVTList VTs = CurDAG->getVTList(MVT::f32, MVT::Other); 2573 2574 SDNode *InterpMov = 2575 CurDAG->getMachineNode(AMDGPU::V_INTERP_MOV_F32, DL, VTs, { 2576 CurDAG->getTargetConstant(2, DL, MVT::i32), // P0 2577 N->getOperand(3), // Attr 2578 N->getOperand(2), // Attrchan 2579 ToM0.getValue(1) // In glue 2580 }); 2581 2582 SDNode *InterpP1LV = 2583 CurDAG->getMachineNode(AMDGPU::V_INTERP_P1LV_F16, DL, MVT::f32, { 2584 CurDAG->getTargetConstant(0, DL, MVT::i32), // $src0_modifiers 2585 N->getOperand(1), // Src0 2586 N->getOperand(3), // Attr 2587 N->getOperand(2), // Attrchan 2588 CurDAG->getTargetConstant(0, DL, MVT::i32), // $src2_modifiers 2589 SDValue(InterpMov, 0), // Src2 - holds two f16 values selected by high 2590 N->getOperand(4), // high 2591 CurDAG->getTargetConstant(0, DL, MVT::i1), // $clamp 2592 CurDAG->getTargetConstant(0, DL, MVT::i32), // $omod 2593 SDValue(InterpMov, 1) 2594 }); 2595 2596 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 0), SDValue(InterpP1LV, 0)); 2597 } 2598 2599 void AMDGPUDAGToDAGISel::SelectINTRINSIC_W_CHAIN(SDNode *N) { 2600 unsigned IntrID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 2601 switch (IntrID) { 2602 case Intrinsic::amdgcn_ds_append: 2603 case Intrinsic::amdgcn_ds_consume: { 2604 if (N->getValueType(0) != MVT::i32) 2605 break; 2606 SelectDSAppendConsume(N, IntrID); 2607 return; 2608 } 2609 } 2610 2611 SelectCode(N); 2612 } 2613 2614 void AMDGPUDAGToDAGISel::SelectINTRINSIC_WO_CHAIN(SDNode *N) { 2615 unsigned IntrID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 2616 unsigned Opcode; 2617 switch (IntrID) { 2618 case Intrinsic::amdgcn_wqm: 2619 Opcode = AMDGPU::WQM; 2620 break; 2621 case Intrinsic::amdgcn_softwqm: 2622 Opcode = AMDGPU::SOFT_WQM; 2623 break; 2624 case Intrinsic::amdgcn_wwm: 2625 Opcode = AMDGPU::WWM; 2626 break; 2627 case Intrinsic::amdgcn_interp_p1_f16: 2628 SelectInterpP1F16(N); 2629 return; 2630 default: 2631 SelectCode(N); 2632 return; 2633 } 2634 2635 SDValue Src = N->getOperand(1); 2636 CurDAG->SelectNodeTo(N, Opcode, N->getVTList(), {Src}); 2637 } 2638 2639 void AMDGPUDAGToDAGISel::SelectINTRINSIC_VOID(SDNode *N) { 2640 unsigned IntrID = cast<ConstantSDNode>(N->getOperand(1))->getZExtValue(); 2641 switch (IntrID) { 2642 case Intrinsic::amdgcn_ds_gws_init: 2643 case Intrinsic::amdgcn_ds_gws_barrier: 2644 case Intrinsic::amdgcn_ds_gws_sema_v: 2645 case Intrinsic::amdgcn_ds_gws_sema_br: 2646 case Intrinsic::amdgcn_ds_gws_sema_p: 2647 case Intrinsic::amdgcn_ds_gws_sema_release_all: 2648 SelectDS_GWS(N, IntrID); 2649 return; 2650 default: 2651 break; 2652 } 2653 2654 SelectCode(N); 2655 } 2656 2657 bool AMDGPUDAGToDAGISel::SelectVOP3ModsImpl(SDValue In, SDValue &Src, 2658 unsigned &Mods, 2659 bool AllowAbs) const { 2660 Mods = 0; 2661 Src = In; 2662 2663 if (Src.getOpcode() == ISD::FNEG) { 2664 Mods |= SISrcMods::NEG; 2665 Src = Src.getOperand(0); 2666 } 2667 2668 if (AllowAbs && Src.getOpcode() == ISD::FABS) { 2669 Mods |= SISrcMods::ABS; 2670 Src = Src.getOperand(0); 2671 } 2672 2673 return true; 2674 } 2675 2676 bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src, 2677 SDValue &SrcMods) const { 2678 unsigned Mods; 2679 if (SelectVOP3ModsImpl(In, Src, Mods)) { 2680 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 2681 return true; 2682 } 2683 2684 return false; 2685 } 2686 2687 bool AMDGPUDAGToDAGISel::SelectVOP3BMods(SDValue In, SDValue &Src, 2688 SDValue &SrcMods) const { 2689 unsigned Mods; 2690 if (SelectVOP3ModsImpl(In, Src, Mods, /* AllowAbs */ false)) { 2691 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 2692 return true; 2693 } 2694 2695 return false; 2696 } 2697 2698 bool AMDGPUDAGToDAGISel::SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, 2699 SDValue &SrcMods) const { 2700 SelectVOP3Mods(In, Src, SrcMods); 2701 return isNoNanSrc(Src); 2702 } 2703 2704 bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src) const { 2705 if (In.getOpcode() == ISD::FABS || In.getOpcode() == ISD::FNEG) 2706 return false; 2707 2708 Src = In; 2709 return true; 2710 } 2711 2712 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src, 2713 SDValue &SrcMods, SDValue &Clamp, 2714 SDValue &Omod) const { 2715 SDLoc DL(In); 2716 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1); 2717 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1); 2718 2719 return SelectVOP3Mods(In, Src, SrcMods); 2720 } 2721 2722 bool AMDGPUDAGToDAGISel::SelectVOP3BMods0(SDValue In, SDValue &Src, 2723 SDValue &SrcMods, SDValue &Clamp, 2724 SDValue &Omod) const { 2725 SDLoc DL(In); 2726 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1); 2727 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1); 2728 2729 return SelectVOP3BMods(In, Src, SrcMods); 2730 } 2731 2732 bool AMDGPUDAGToDAGISel::SelectVOP3OMods(SDValue In, SDValue &Src, 2733 SDValue &Clamp, SDValue &Omod) const { 2734 Src = In; 2735 2736 SDLoc DL(In); 2737 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1); 2738 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1); 2739 2740 return true; 2741 } 2742 2743 bool AMDGPUDAGToDAGISel::SelectVOP3PMods(SDValue In, SDValue &Src, 2744 SDValue &SrcMods) const { 2745 unsigned Mods = 0; 2746 Src = In; 2747 2748 if (Src.getOpcode() == ISD::FNEG) { 2749 Mods ^= (SISrcMods::NEG | SISrcMods::NEG_HI); 2750 Src = Src.getOperand(0); 2751 } 2752 2753 if (Src.getOpcode() == ISD::BUILD_VECTOR) { 2754 unsigned VecMods = Mods; 2755 2756 SDValue Lo = stripBitcast(Src.getOperand(0)); 2757 SDValue Hi = stripBitcast(Src.getOperand(1)); 2758 2759 if (Lo.getOpcode() == ISD::FNEG) { 2760 Lo = stripBitcast(Lo.getOperand(0)); 2761 Mods ^= SISrcMods::NEG; 2762 } 2763 2764 if (Hi.getOpcode() == ISD::FNEG) { 2765 Hi = stripBitcast(Hi.getOperand(0)); 2766 Mods ^= SISrcMods::NEG_HI; 2767 } 2768 2769 if (isExtractHiElt(Lo, Lo)) 2770 Mods |= SISrcMods::OP_SEL_0; 2771 2772 if (isExtractHiElt(Hi, Hi)) 2773 Mods |= SISrcMods::OP_SEL_1; 2774 2775 Lo = stripExtractLoElt(Lo); 2776 Hi = stripExtractLoElt(Hi); 2777 2778 if (Lo == Hi && !isInlineImmediate(Lo.getNode())) { 2779 // Really a scalar input. Just select from the low half of the register to 2780 // avoid packing. 2781 2782 Src = Lo; 2783 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 2784 return true; 2785 } 2786 2787 Mods = VecMods; 2788 } 2789 2790 // Packed instructions do not have abs modifiers. 2791 Mods |= SISrcMods::OP_SEL_1; 2792 2793 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 2794 return true; 2795 } 2796 2797 bool AMDGPUDAGToDAGISel::SelectVOP3OpSel(SDValue In, SDValue &Src, 2798 SDValue &SrcMods) const { 2799 Src = In; 2800 // FIXME: Handle op_sel 2801 SrcMods = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32); 2802 return true; 2803 } 2804 2805 bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods(SDValue In, SDValue &Src, 2806 SDValue &SrcMods) const { 2807 // FIXME: Handle op_sel 2808 return SelectVOP3Mods(In, Src, SrcMods); 2809 } 2810 2811 // The return value is not whether the match is possible (which it always is), 2812 // but whether or not it a conversion is really used. 2813 bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src, 2814 unsigned &Mods) const { 2815 Mods = 0; 2816 SelectVOP3ModsImpl(In, Src, Mods); 2817 2818 if (Src.getOpcode() == ISD::FP_EXTEND) { 2819 Src = Src.getOperand(0); 2820 assert(Src.getValueType() == MVT::f16); 2821 Src = stripBitcast(Src); 2822 2823 // Be careful about folding modifiers if we already have an abs. fneg is 2824 // applied last, so we don't want to apply an earlier fneg. 2825 if ((Mods & SISrcMods::ABS) == 0) { 2826 unsigned ModsTmp; 2827 SelectVOP3ModsImpl(Src, Src, ModsTmp); 2828 2829 if ((ModsTmp & SISrcMods::NEG) != 0) 2830 Mods ^= SISrcMods::NEG; 2831 2832 if ((ModsTmp & SISrcMods::ABS) != 0) 2833 Mods |= SISrcMods::ABS; 2834 } 2835 2836 // op_sel/op_sel_hi decide the source type and source. 2837 // If the source's op_sel_hi is set, it indicates to do a conversion from fp16. 2838 // If the sources's op_sel is set, it picks the high half of the source 2839 // register. 2840 2841 Mods |= SISrcMods::OP_SEL_1; 2842 if (isExtractHiElt(Src, Src)) { 2843 Mods |= SISrcMods::OP_SEL_0; 2844 2845 // TODO: Should we try to look for neg/abs here? 2846 } 2847 2848 return true; 2849 } 2850 2851 return false; 2852 } 2853 2854 bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixMods(SDValue In, SDValue &Src, 2855 SDValue &SrcMods) const { 2856 unsigned Mods = 0; 2857 SelectVOP3PMadMixModsImpl(In, Src, Mods); 2858 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 2859 return true; 2860 } 2861 2862 SDValue AMDGPUDAGToDAGISel::getHi16Elt(SDValue In) const { 2863 if (In.isUndef()) 2864 return CurDAG->getUNDEF(MVT::i32); 2865 2866 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(In)) { 2867 SDLoc SL(In); 2868 return CurDAG->getConstant(C->getZExtValue() << 16, SL, MVT::i32); 2869 } 2870 2871 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(In)) { 2872 SDLoc SL(In); 2873 return CurDAG->getConstant( 2874 C->getValueAPF().bitcastToAPInt().getZExtValue() << 16, SL, MVT::i32); 2875 } 2876 2877 SDValue Src; 2878 if (isExtractHiElt(In, Src)) 2879 return Src; 2880 2881 return SDValue(); 2882 } 2883 2884 bool AMDGPUDAGToDAGISel::isVGPRImm(const SDNode * N) const { 2885 assert(CurDAG->getTarget().getTargetTriple().getArch() == Triple::amdgcn); 2886 2887 const SIRegisterInfo *SIRI = 2888 static_cast<const SIRegisterInfo *>(Subtarget->getRegisterInfo()); 2889 const SIInstrInfo * SII = 2890 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); 2891 2892 unsigned Limit = 0; 2893 bool AllUsesAcceptSReg = true; 2894 for (SDNode::use_iterator U = N->use_begin(), E = SDNode::use_end(); 2895 Limit < 10 && U != E; ++U, ++Limit) { 2896 const TargetRegisterClass *RC = getOperandRegClass(*U, U.getOperandNo()); 2897 2898 // If the register class is unknown, it could be an unknown 2899 // register class that needs to be an SGPR, e.g. an inline asm 2900 // constraint 2901 if (!RC || SIRI->isSGPRClass(RC)) 2902 return false; 2903 2904 if (RC != &AMDGPU::VS_32RegClass) { 2905 AllUsesAcceptSReg = false; 2906 SDNode * User = *U; 2907 if (User->isMachineOpcode()) { 2908 unsigned Opc = User->getMachineOpcode(); 2909 MCInstrDesc Desc = SII->get(Opc); 2910 if (Desc.isCommutable()) { 2911 unsigned OpIdx = Desc.getNumDefs() + U.getOperandNo(); 2912 unsigned CommuteIdx1 = TargetInstrInfo::CommuteAnyOperandIndex; 2913 if (SII->findCommutedOpIndices(Desc, OpIdx, CommuteIdx1)) { 2914 unsigned CommutedOpNo = CommuteIdx1 - Desc.getNumDefs(); 2915 const TargetRegisterClass *CommutedRC = getOperandRegClass(*U, CommutedOpNo); 2916 if (CommutedRC == &AMDGPU::VS_32RegClass) 2917 AllUsesAcceptSReg = true; 2918 } 2919 } 2920 } 2921 // If "AllUsesAcceptSReg == false" so far we haven't suceeded 2922 // commuting current user. This means have at least one use 2923 // that strictly require VGPR. Thus, we will not attempt to commute 2924 // other user instructions. 2925 if (!AllUsesAcceptSReg) 2926 break; 2927 } 2928 } 2929 return !AllUsesAcceptSReg && (Limit < 10); 2930 } 2931 2932 bool AMDGPUDAGToDAGISel::isUniformLoad(const SDNode * N) const { 2933 auto Ld = cast<LoadSDNode>(N); 2934 2935 return Ld->getAlignment() >= 4 && 2936 ( 2937 ( 2938 ( 2939 Ld->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS || 2940 Ld->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT 2941 ) 2942 && 2943 !N->isDivergent() 2944 ) 2945 || 2946 ( 2947 Subtarget->getScalarizeGlobalBehavior() && 2948 Ld->getAddressSpace() == AMDGPUAS::GLOBAL_ADDRESS && 2949 Ld->isSimple() && 2950 !N->isDivergent() && 2951 static_cast<const SITargetLowering *>( 2952 getTargetLowering())->isMemOpHasNoClobberedMemOperand(N) 2953 ) 2954 ); 2955 } 2956 2957 void AMDGPUDAGToDAGISel::PostprocessISelDAG() { 2958 const AMDGPUTargetLowering& Lowering = 2959 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering()); 2960 bool IsModified = false; 2961 do { 2962 IsModified = false; 2963 2964 // Go over all selected nodes and try to fold them a bit more 2965 SelectionDAG::allnodes_iterator Position = CurDAG->allnodes_begin(); 2966 while (Position != CurDAG->allnodes_end()) { 2967 SDNode *Node = &*Position++; 2968 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(Node); 2969 if (!MachineNode) 2970 continue; 2971 2972 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG); 2973 if (ResNode != Node) { 2974 if (ResNode) 2975 ReplaceUses(Node, ResNode); 2976 IsModified = true; 2977 } 2978 } 2979 CurDAG->RemoveDeadNodes(); 2980 } while (IsModified); 2981 } 2982 2983 bool R600DAGToDAGISel::runOnMachineFunction(MachineFunction &MF) { 2984 Subtarget = &MF.getSubtarget<R600Subtarget>(); 2985 return SelectionDAGISel::runOnMachineFunction(MF); 2986 } 2987 2988 bool R600DAGToDAGISel::isConstantLoad(const MemSDNode *N, int CbId) const { 2989 if (!N->readMem()) 2990 return false; 2991 if (CbId == -1) 2992 return N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS || 2993 N->getAddressSpace() == AMDGPUAS::CONSTANT_ADDRESS_32BIT; 2994 2995 return N->getAddressSpace() == AMDGPUAS::CONSTANT_BUFFER_0 + CbId; 2996 } 2997 2998 bool R600DAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr, 2999 SDValue& IntPtr) { 3000 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) { 3001 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr), 3002 true); 3003 return true; 3004 } 3005 return false; 3006 } 3007 3008 bool R600DAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr, 3009 SDValue& BaseReg, SDValue &Offset) { 3010 if (!isa<ConstantSDNode>(Addr)) { 3011 BaseReg = Addr; 3012 Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true); 3013 return true; 3014 } 3015 return false; 3016 } 3017 3018 void R600DAGToDAGISel::Select(SDNode *N) { 3019 unsigned int Opc = N->getOpcode(); 3020 if (N->isMachineOpcode()) { 3021 N->setNodeId(-1); 3022 return; // Already selected. 3023 } 3024 3025 switch (Opc) { 3026 default: break; 3027 case AMDGPUISD::BUILD_VERTICAL_VECTOR: 3028 case ISD::SCALAR_TO_VECTOR: 3029 case ISD::BUILD_VECTOR: { 3030 EVT VT = N->getValueType(0); 3031 unsigned NumVectorElts = VT.getVectorNumElements(); 3032 unsigned RegClassID; 3033 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG 3034 // that adds a 128 bits reg copy when going through TwoAddressInstructions 3035 // pass. We want to avoid 128 bits copies as much as possible because they 3036 // can't be bundled by our scheduler. 3037 switch(NumVectorElts) { 3038 case 2: RegClassID = R600::R600_Reg64RegClassID; break; 3039 case 4: 3040 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR) 3041 RegClassID = R600::R600_Reg128VerticalRegClassID; 3042 else 3043 RegClassID = R600::R600_Reg128RegClassID; 3044 break; 3045 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR"); 3046 } 3047 SelectBuildVector(N, RegClassID); 3048 return; 3049 } 3050 } 3051 3052 SelectCode(N); 3053 } 3054 3055 bool R600DAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base, 3056 SDValue &Offset) { 3057 ConstantSDNode *C; 3058 SDLoc DL(Addr); 3059 3060 if ((C = dyn_cast<ConstantSDNode>(Addr))) { 3061 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32); 3062 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 3063 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) && 3064 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) { 3065 Base = CurDAG->getRegister(R600::INDIRECT_BASE_ADDR, MVT::i32); 3066 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 3067 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) && 3068 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) { 3069 Base = Addr.getOperand(0); 3070 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 3071 } else { 3072 Base = Addr; 3073 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); 3074 } 3075 3076 return true; 3077 } 3078 3079 bool R600DAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base, 3080 SDValue &Offset) { 3081 ConstantSDNode *IMMOffset; 3082 3083 if (Addr.getOpcode() == ISD::ADD 3084 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) 3085 && isInt<16>(IMMOffset->getZExtValue())) { 3086 3087 Base = Addr.getOperand(0); 3088 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), 3089 MVT::i32); 3090 return true; 3091 // If the pointer address is constant, we can move it to the offset field. 3092 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr)) 3093 && isInt<16>(IMMOffset->getZExtValue())) { 3094 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), 3095 SDLoc(CurDAG->getEntryNode()), 3096 R600::ZERO, MVT::i32); 3097 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), 3098 MVT::i32); 3099 return true; 3100 } 3101 3102 // Default case, no offset 3103 Base = Addr; 3104 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32); 3105 return true; 3106 } 3107