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