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