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