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