1 //===-- AMDGPUISelDAGToDAG.cpp - A dag to dag inst selector for AMDGPU ----===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //==-----------------------------------------------------------------------===// 9 // 10 /// \file 11 /// \brief Defines an instruction selector for the AMDGPU target. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #include "AMDGPU.h" 16 #include "AMDGPUArgumentUsageInfo.h" 17 #include "AMDGPUISelLowering.h" // For AMDGPUISD 18 #include "AMDGPUInstrInfo.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 "llvm/ADT/APInt.h" 28 #include "llvm/ADT/SmallVector.h" 29 #include "llvm/ADT/StringRef.h" 30 #include "llvm/Analysis/ValueTracking.h" 31 #include "llvm/CodeGen/FunctionLoweringInfo.h" 32 #include "llvm/CodeGen/ISDOpcodes.h" 33 #include "llvm/CodeGen/MachineFunction.h" 34 #include "llvm/CodeGen/MachineRegisterInfo.h" 35 #include "llvm/CodeGen/MachineValueType.h" 36 #include "llvm/CodeGen/SelectionDAG.h" 37 #include "llvm/CodeGen/SelectionDAGISel.h" 38 #include "llvm/CodeGen/SelectionDAGNodes.h" 39 #include "llvm/CodeGen/ValueTypes.h" 40 #include "llvm/IR/BasicBlock.h" 41 #include "llvm/IR/Instruction.h" 42 #include "llvm/MC/MCInstrDesc.h" 43 #include "llvm/Support/Casting.h" 44 #include "llvm/Support/CodeGen.h" 45 #include "llvm/Support/ErrorHandling.h" 46 #include "llvm/Support/MathExtras.h" 47 #include <cassert> 48 #include <cstdint> 49 #include <new> 50 #include <vector> 51 52 using namespace llvm; 53 54 namespace llvm { 55 56 class R600InstrInfo; 57 58 } // end namespace llvm 59 60 //===----------------------------------------------------------------------===// 61 // Instruction Selector Implementation 62 //===----------------------------------------------------------------------===// 63 64 namespace { 65 66 /// AMDGPU specific code to select AMDGPU machine instructions for 67 /// SelectionDAG operations. 68 class AMDGPUDAGToDAGISel : public SelectionDAGISel { 69 // Subtarget - Keep a pointer to the AMDGPU Subtarget around so that we can 70 // make the right decision when generating code for different targets. 71 const AMDGPUSubtarget *Subtarget; 72 AMDGPUAS AMDGPUASI; 73 bool EnableLateStructurizeCFG; 74 75 public: 76 explicit AMDGPUDAGToDAGISel(TargetMachine *TM = nullptr, 77 CodeGenOpt::Level OptLevel = CodeGenOpt::Default) 78 : SelectionDAGISel(*TM, OptLevel) { 79 AMDGPUASI = AMDGPU::getAMDGPUAS(*TM); 80 EnableLateStructurizeCFG = AMDGPUTargetMachine::EnableLateStructurizeCFG; 81 } 82 ~AMDGPUDAGToDAGISel() override = default; 83 84 void getAnalysisUsage(AnalysisUsage &AU) const override { 85 AU.addRequired<AMDGPUArgumentUsageInfo>(); 86 SelectionDAGISel::getAnalysisUsage(AU); 87 } 88 89 bool runOnMachineFunction(MachineFunction &MF) override; 90 void Select(SDNode *N) override; 91 StringRef getPassName() const override; 92 void PostprocessISelDAG() override; 93 94 protected: 95 void SelectBuildVector(SDNode *N, unsigned RegClassID); 96 97 private: 98 std::pair<SDValue, SDValue> foldFrameIndex(SDValue N) const; 99 bool isNoNanSrc(SDValue N) const; 100 bool isInlineImmediate(const SDNode *N) const; 101 bool FoldOperand(SDValue &Src, SDValue &Sel, SDValue &Neg, SDValue &Abs, 102 const R600InstrInfo *TII); 103 bool FoldOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &); 104 bool FoldDotOperands(unsigned, const R600InstrInfo *, std::vector<SDValue> &); 105 106 bool isConstantLoad(const MemSDNode *N, int cbID) const; 107 bool isUniformBr(const SDNode *N) const; 108 109 SDNode *glueCopyToM0(SDNode *N) const; 110 111 const TargetRegisterClass *getOperandRegClass(SDNode *N, unsigned OpNo) const; 112 bool SelectGlobalValueConstantOffset(SDValue Addr, SDValue& IntPtr); 113 bool SelectGlobalValueVariableOffset(SDValue Addr, SDValue &BaseReg, 114 SDValue& Offset); 115 virtual bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, SDValue &Offset); 116 virtual bool SelectADDRIndirect(SDValue Addr, SDValue &Base, SDValue &Offset); 117 bool isDSOffsetLegal(const SDValue &Base, unsigned Offset, 118 unsigned OffsetBits) const; 119 bool SelectDS1Addr1Offset(SDValue Ptr, SDValue &Base, SDValue &Offset) const; 120 bool SelectDS64Bit4ByteAligned(SDValue Ptr, SDValue &Base, SDValue &Offset0, 121 SDValue &Offset1) const; 122 bool SelectMUBUF(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, 123 SDValue &SOffset, SDValue &Offset, SDValue &Offen, 124 SDValue &Idxen, SDValue &Addr64, SDValue &GLC, SDValue &SLC, 125 SDValue &TFE) const; 126 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, SDValue &VAddr, 127 SDValue &SOffset, SDValue &Offset, SDValue &GLC, 128 SDValue &SLC, SDValue &TFE) const; 129 bool SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 130 SDValue &VAddr, SDValue &SOffset, SDValue &Offset, 131 SDValue &SLC) const; 132 bool SelectMUBUFScratchOffen(SDNode *Parent, 133 SDValue Addr, SDValue &RSrc, SDValue &VAddr, 134 SDValue &SOffset, SDValue &ImmOffset) const; 135 bool SelectMUBUFScratchOffset(SDNode *Parent, 136 SDValue Addr, SDValue &SRsrc, SDValue &Soffset, 137 SDValue &Offset) const; 138 139 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &SOffset, 140 SDValue &Offset, SDValue &GLC, SDValue &SLC, 141 SDValue &TFE) const; 142 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset, 143 SDValue &Offset, SDValue &SLC) const; 144 bool SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, SDValue &Soffset, 145 SDValue &Offset) const; 146 bool SelectMUBUFConstant(SDValue Constant, 147 SDValue &SOffset, 148 SDValue &ImmOffset) const; 149 bool SelectMUBUFIntrinsicOffset(SDValue Offset, SDValue &SOffset, 150 SDValue &ImmOffset) const; 151 bool SelectMUBUFIntrinsicVOffset(SDValue Offset, SDValue &SOffset, 152 SDValue &ImmOffset, SDValue &VOffset) const; 153 154 bool SelectFlatAtomic(SDValue Addr, SDValue &VAddr, 155 SDValue &Offset, SDValue &SLC) const; 156 bool SelectFlatAtomicSigned(SDValue Addr, SDValue &VAddr, 157 SDValue &Offset, SDValue &SLC) const; 158 159 template <bool IsSigned> 160 bool SelectFlatOffset(SDValue Addr, SDValue &VAddr, 161 SDValue &Offset, SDValue &SLC) const; 162 163 bool SelectSMRDOffset(SDValue ByteOffsetNode, SDValue &Offset, 164 bool &Imm) const; 165 bool SelectSMRD(SDValue Addr, SDValue &SBase, SDValue &Offset, 166 bool &Imm) const; 167 bool SelectSMRDImm(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 168 bool SelectSMRDImm32(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 169 bool SelectSMRDSgpr(SDValue Addr, SDValue &SBase, SDValue &Offset) const; 170 bool SelectSMRDBufferImm(SDValue Addr, SDValue &Offset) const; 171 bool SelectSMRDBufferImm32(SDValue Addr, SDValue &Offset) const; 172 bool SelectMOVRELOffset(SDValue Index, SDValue &Base, SDValue &Offset) const; 173 174 bool SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, SDValue &SrcMods) const; 175 bool SelectVOP3ModsImpl(SDValue In, SDValue &Src, unsigned &SrcMods) const; 176 bool SelectVOP3Mods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 177 bool SelectVOP3NoMods(SDValue In, SDValue &Src) const; 178 bool SelectVOP3Mods0(SDValue In, SDValue &Src, SDValue &SrcMods, 179 SDValue &Clamp, SDValue &Omod) const; 180 bool SelectVOP3NoMods0(SDValue In, SDValue &Src, SDValue &SrcMods, 181 SDValue &Clamp, SDValue &Omod) const; 182 183 bool SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, SDValue &SrcMods, 184 SDValue &Clamp, 185 SDValue &Omod) const; 186 187 bool SelectVOP3OMods(SDValue In, SDValue &Src, 188 SDValue &Clamp, SDValue &Omod) const; 189 190 bool SelectVOP3PMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 191 bool SelectVOP3PMods0(SDValue In, SDValue &Src, SDValue &SrcMods, 192 SDValue &Clamp) const; 193 194 bool SelectVOP3OpSel(SDValue In, SDValue &Src, SDValue &SrcMods) const; 195 bool SelectVOP3OpSel0(SDValue In, SDValue &Src, SDValue &SrcMods, 196 SDValue &Clamp) const; 197 198 bool SelectVOP3OpSelMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 199 bool SelectVOP3OpSelMods0(SDValue In, SDValue &Src, SDValue &SrcMods, 200 SDValue &Clamp) const; 201 bool SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src, unsigned &Mods) const; 202 bool SelectVOP3PMadMixMods(SDValue In, SDValue &Src, SDValue &SrcMods) const; 203 204 bool SelectHi16Elt(SDValue In, SDValue &Src) const; 205 206 void SelectADD_SUB_I64(SDNode *N); 207 void SelectUADDO_USUBO(SDNode *N); 208 void SelectDIV_SCALE(SDNode *N); 209 void SelectMAD_64_32(SDNode *N); 210 void SelectFMA_W_CHAIN(SDNode *N); 211 void SelectFMUL_W_CHAIN(SDNode *N); 212 213 SDNode *getS_BFE(unsigned Opcode, const SDLoc &DL, SDValue Val, 214 uint32_t Offset, uint32_t Width); 215 void SelectS_BFEFromShifts(SDNode *N); 216 void SelectS_BFE(SDNode *N); 217 bool isCBranchSCC(const SDNode *N) const; 218 void SelectBRCOND(SDNode *N); 219 void SelectFMAD(SDNode *N); 220 void SelectATOMIC_CMP_SWAP(SDNode *N); 221 222 protected: 223 // Include the pieces autogenerated from the target description. 224 #include "AMDGPUGenDAGISel.inc" 225 }; 226 227 class R600DAGToDAGISel : public AMDGPUDAGToDAGISel { 228 public: 229 explicit R600DAGToDAGISel(TargetMachine *TM, CodeGenOpt::Level OptLevel) : 230 AMDGPUDAGToDAGISel(TM, OptLevel) {} 231 232 void Select(SDNode *N) override; 233 234 bool SelectADDRIndirect(SDValue Addr, SDValue &Base, 235 SDValue &Offset) override; 236 bool SelectADDRVTX_READ(SDValue Addr, SDValue &Base, 237 SDValue &Offset) override; 238 }; 239 240 } // end anonymous namespace 241 242 INITIALIZE_PASS_BEGIN(AMDGPUDAGToDAGISel, "isel", 243 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false) 244 INITIALIZE_PASS_DEPENDENCY(AMDGPUArgumentUsageInfo) 245 INITIALIZE_PASS_END(AMDGPUDAGToDAGISel, "isel", 246 "AMDGPU DAG->DAG Pattern Instruction Selection", false, false) 247 248 /// \brief This pass converts a legalized DAG into a AMDGPU-specific 249 // DAG, ready for instruction scheduling. 250 FunctionPass *llvm::createAMDGPUISelDag(TargetMachine *TM, 251 CodeGenOpt::Level OptLevel) { 252 return new AMDGPUDAGToDAGISel(TM, OptLevel); 253 } 254 255 /// \brief This pass converts a legalized DAG into a R600-specific 256 // DAG, ready for instruction scheduling. 257 FunctionPass *llvm::createR600ISelDag(TargetMachine *TM, 258 CodeGenOpt::Level OptLevel) { 259 return new R600DAGToDAGISel(TM, OptLevel); 260 } 261 262 bool AMDGPUDAGToDAGISel::runOnMachineFunction(MachineFunction &MF) { 263 Subtarget = &MF.getSubtarget<AMDGPUSubtarget>(); 264 return SelectionDAGISel::runOnMachineFunction(MF); 265 } 266 267 bool AMDGPUDAGToDAGISel::isNoNanSrc(SDValue N) const { 268 if (TM.Options.NoNaNsFPMath) 269 return true; 270 271 // TODO: Move into isKnownNeverNaN 272 if (N->getFlags().isDefined()) 273 return N->getFlags().hasNoNaNs(); 274 275 return CurDAG->isKnownNeverNaN(N); 276 } 277 278 bool AMDGPUDAGToDAGISel::isInlineImmediate(const SDNode *N) const { 279 const SIInstrInfo *TII 280 = static_cast<const SISubtarget *>(Subtarget)->getInstrInfo(); 281 282 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) 283 return TII->isInlineConstant(C->getAPIntValue()); 284 285 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) 286 return TII->isInlineConstant(C->getValueAPF().bitcastToAPInt()); 287 288 return false; 289 } 290 291 /// \brief Determine the register class for \p OpNo 292 /// \returns The register class of the virtual register that will be used for 293 /// the given operand number \OpNo or NULL if the register class cannot be 294 /// determined. 295 const TargetRegisterClass *AMDGPUDAGToDAGISel::getOperandRegClass(SDNode *N, 296 unsigned OpNo) const { 297 if (!N->isMachineOpcode()) { 298 if (N->getOpcode() == ISD::CopyToReg) { 299 unsigned Reg = cast<RegisterSDNode>(N->getOperand(1))->getReg(); 300 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 301 MachineRegisterInfo &MRI = CurDAG->getMachineFunction().getRegInfo(); 302 return MRI.getRegClass(Reg); 303 } 304 305 const SIRegisterInfo *TRI 306 = static_cast<const SISubtarget *>(Subtarget)->getRegisterInfo(); 307 return TRI->getPhysRegClass(Reg); 308 } 309 310 return nullptr; 311 } 312 313 switch (N->getMachineOpcode()) { 314 default: { 315 const MCInstrDesc &Desc = 316 Subtarget->getInstrInfo()->get(N->getMachineOpcode()); 317 unsigned OpIdx = Desc.getNumDefs() + OpNo; 318 if (OpIdx >= Desc.getNumOperands()) 319 return nullptr; 320 int RegClass = Desc.OpInfo[OpIdx].RegClass; 321 if (RegClass == -1) 322 return nullptr; 323 324 return Subtarget->getRegisterInfo()->getRegClass(RegClass); 325 } 326 case AMDGPU::REG_SEQUENCE: { 327 unsigned RCID = cast<ConstantSDNode>(N->getOperand(0))->getZExtValue(); 328 const TargetRegisterClass *SuperRC = 329 Subtarget->getRegisterInfo()->getRegClass(RCID); 330 331 SDValue SubRegOp = N->getOperand(OpNo + 1); 332 unsigned SubRegIdx = cast<ConstantSDNode>(SubRegOp)->getZExtValue(); 333 return Subtarget->getRegisterInfo()->getSubClassWithSubReg(SuperRC, 334 SubRegIdx); 335 } 336 } 337 } 338 339 SDNode *AMDGPUDAGToDAGISel::glueCopyToM0(SDNode *N) const { 340 if (cast<MemSDNode>(N)->getAddressSpace() != AMDGPUASI.LOCAL_ADDRESS || 341 !Subtarget->ldsRequiresM0Init()) 342 return N; 343 344 const SITargetLowering& Lowering = 345 *static_cast<const SITargetLowering*>(getTargetLowering()); 346 347 // Write max value to m0 before each load operation 348 349 SDValue M0 = Lowering.copyToM0(*CurDAG, CurDAG->getEntryNode(), SDLoc(N), 350 CurDAG->getTargetConstant(-1, SDLoc(N), MVT::i32)); 351 352 SDValue Glue = M0.getValue(1); 353 354 SmallVector <SDValue, 8> Ops; 355 for (unsigned i = 0, e = N->getNumOperands(); i != e; ++i) { 356 Ops.push_back(N->getOperand(i)); 357 } 358 Ops.push_back(Glue); 359 CurDAG->MorphNodeTo(N, N->getOpcode(), N->getVTList(), Ops); 360 361 return N; 362 } 363 364 static unsigned selectSGPRVectorRegClassID(unsigned NumVectorElts) { 365 switch (NumVectorElts) { 366 case 1: 367 return AMDGPU::SReg_32_XM0RegClassID; 368 case 2: 369 return AMDGPU::SReg_64RegClassID; 370 case 4: 371 return AMDGPU::SReg_128RegClassID; 372 case 8: 373 return AMDGPU::SReg_256RegClassID; 374 case 16: 375 return AMDGPU::SReg_512RegClassID; 376 } 377 378 llvm_unreachable("invalid vector size"); 379 } 380 381 static bool getConstantValue(SDValue N, uint32_t &Out) { 382 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(N)) { 383 Out = C->getAPIntValue().getZExtValue(); 384 return true; 385 } 386 387 if (const ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(N)) { 388 Out = C->getValueAPF().bitcastToAPInt().getZExtValue(); 389 return true; 390 } 391 392 return false; 393 } 394 395 void AMDGPUDAGToDAGISel::SelectBuildVector(SDNode *N, unsigned RegClassID) { 396 EVT VT = N->getValueType(0); 397 unsigned NumVectorElts = VT.getVectorNumElements(); 398 EVT EltVT = VT.getVectorElementType(); 399 const AMDGPURegisterInfo *TRI = Subtarget->getRegisterInfo(); 400 SDLoc DL(N); 401 SDValue RegClass = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); 402 403 if (NumVectorElts == 1) { 404 CurDAG->SelectNodeTo(N, AMDGPU::COPY_TO_REGCLASS, EltVT, N->getOperand(0), 405 RegClass); 406 return; 407 } 408 409 assert(NumVectorElts <= 16 && "Vectors with more than 16 elements not " 410 "supported yet"); 411 // 16 = Max Num Vector Elements 412 // 2 = 2 REG_SEQUENCE operands per element (value, subreg index) 413 // 1 = Vector Register Class 414 SmallVector<SDValue, 16 * 2 + 1> RegSeqArgs(NumVectorElts * 2 + 1); 415 416 RegSeqArgs[0] = CurDAG->getTargetConstant(RegClassID, DL, MVT::i32); 417 bool IsRegSeq = true; 418 unsigned NOps = N->getNumOperands(); 419 for (unsigned i = 0; i < NOps; i++) { 420 // XXX: Why is this here? 421 if (isa<RegisterSDNode>(N->getOperand(i))) { 422 IsRegSeq = false; 423 break; 424 } 425 RegSeqArgs[1 + (2 * i)] = N->getOperand(i); 426 RegSeqArgs[1 + (2 * i) + 1] = 427 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL, 428 MVT::i32); 429 } 430 if (NOps != NumVectorElts) { 431 // Fill in the missing undef elements if this was a scalar_to_vector. 432 assert(N->getOpcode() == ISD::SCALAR_TO_VECTOR && NOps < NumVectorElts); 433 MachineSDNode *ImpDef = CurDAG->getMachineNode(TargetOpcode::IMPLICIT_DEF, 434 DL, EltVT); 435 for (unsigned i = NOps; i < NumVectorElts; ++i) { 436 RegSeqArgs[1 + (2 * i)] = SDValue(ImpDef, 0); 437 RegSeqArgs[1 + (2 * i) + 1] = 438 CurDAG->getTargetConstant(TRI->getSubRegFromChannel(i), DL, MVT::i32); 439 } 440 } 441 442 if (!IsRegSeq) 443 SelectCode(N); 444 CurDAG->SelectNodeTo(N, AMDGPU::REG_SEQUENCE, N->getVTList(), RegSeqArgs); 445 } 446 447 void AMDGPUDAGToDAGISel::Select(SDNode *N) { 448 unsigned int Opc = N->getOpcode(); 449 if (N->isMachineOpcode()) { 450 N->setNodeId(-1); 451 return; // Already selected. 452 } 453 454 if (isa<AtomicSDNode>(N) || 455 (Opc == AMDGPUISD::ATOMIC_INC || Opc == AMDGPUISD::ATOMIC_DEC)) 456 N = glueCopyToM0(N); 457 458 switch (Opc) { 459 default: 460 break; 461 // We are selecting i64 ADD here instead of custom lower it during 462 // DAG legalization, so we can fold some i64 ADDs used for address 463 // calculation into the LOAD and STORE instructions. 464 case ISD::ADDC: 465 case ISD::ADDE: 466 case ISD::SUBC: 467 case ISD::SUBE: { 468 if (N->getValueType(0) != MVT::i64) 469 break; 470 471 SelectADD_SUB_I64(N); 472 return; 473 } 474 case ISD::UADDO: 475 case ISD::USUBO: { 476 SelectUADDO_USUBO(N); 477 return; 478 } 479 case AMDGPUISD::FMUL_W_CHAIN: { 480 SelectFMUL_W_CHAIN(N); 481 return; 482 } 483 case AMDGPUISD::FMA_W_CHAIN: { 484 SelectFMA_W_CHAIN(N); 485 return; 486 } 487 488 case ISD::SCALAR_TO_VECTOR: 489 case ISD::BUILD_VECTOR: { 490 EVT VT = N->getValueType(0); 491 unsigned NumVectorElts = VT.getVectorNumElements(); 492 493 if (VT == MVT::v2i16 || VT == MVT::v2f16) { 494 if (Opc == ISD::BUILD_VECTOR) { 495 uint32_t LHSVal, RHSVal; 496 if (getConstantValue(N->getOperand(0), LHSVal) && 497 getConstantValue(N->getOperand(1), RHSVal)) { 498 uint32_t K = LHSVal | (RHSVal << 16); 499 CurDAG->SelectNodeTo(N, AMDGPU::S_MOV_B32, VT, 500 CurDAG->getTargetConstant(K, SDLoc(N), MVT::i32)); 501 return; 502 } 503 } 504 505 break; 506 } 507 508 assert(VT.getVectorElementType().bitsEq(MVT::i32)); 509 unsigned RegClassID = selectSGPRVectorRegClassID(NumVectorElts); 510 SelectBuildVector(N, RegClassID); 511 return; 512 } 513 case ISD::BUILD_PAIR: { 514 SDValue RC, SubReg0, SubReg1; 515 SDLoc DL(N); 516 if (N->getValueType(0) == MVT::i128) { 517 RC = CurDAG->getTargetConstant(AMDGPU::SReg_128RegClassID, DL, MVT::i32); 518 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0_sub1, DL, MVT::i32); 519 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub2_sub3, DL, MVT::i32); 520 } else if (N->getValueType(0) == MVT::i64) { 521 RC = CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32); 522 SubReg0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); 523 SubReg1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); 524 } else { 525 llvm_unreachable("Unhandled value type for BUILD_PAIR"); 526 } 527 const SDValue Ops[] = { RC, N->getOperand(0), SubReg0, 528 N->getOperand(1), SubReg1 }; 529 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, 530 N->getValueType(0), Ops)); 531 return; 532 } 533 534 case ISD::Constant: 535 case ISD::ConstantFP: { 536 if (N->getValueType(0).getSizeInBits() != 64 || isInlineImmediate(N)) 537 break; 538 539 uint64_t Imm; 540 if (ConstantFPSDNode *FP = dyn_cast<ConstantFPSDNode>(N)) 541 Imm = FP->getValueAPF().bitcastToAPInt().getZExtValue(); 542 else { 543 ConstantSDNode *C = cast<ConstantSDNode>(N); 544 Imm = C->getZExtValue(); 545 } 546 547 SDLoc DL(N); 548 SDNode *Lo = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 549 CurDAG->getConstant(Imm & 0xFFFFFFFF, DL, 550 MVT::i32)); 551 SDNode *Hi = CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 552 CurDAG->getConstant(Imm >> 32, DL, MVT::i32)); 553 const SDValue Ops[] = { 554 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), 555 SDValue(Lo, 0), CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32), 556 SDValue(Hi, 0), CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32) 557 }; 558 559 ReplaceNode(N, CurDAG->getMachineNode(TargetOpcode::REG_SEQUENCE, DL, 560 N->getValueType(0), Ops)); 561 return; 562 } 563 case ISD::LOAD: 564 case ISD::STORE: { 565 N = glueCopyToM0(N); 566 break; 567 } 568 569 case AMDGPUISD::BFE_I32: 570 case AMDGPUISD::BFE_U32: { 571 // There is a scalar version available, but unlike the vector version which 572 // has a separate operand for the offset and width, the scalar version packs 573 // the width and offset into a single operand. Try to move to the scalar 574 // version if the offsets are constant, so that we can try to keep extended 575 // loads of kernel arguments in SGPRs. 576 577 // TODO: Technically we could try to pattern match scalar bitshifts of 578 // dynamic values, but it's probably not useful. 579 ConstantSDNode *Offset = dyn_cast<ConstantSDNode>(N->getOperand(1)); 580 if (!Offset) 581 break; 582 583 ConstantSDNode *Width = dyn_cast<ConstantSDNode>(N->getOperand(2)); 584 if (!Width) 585 break; 586 587 bool Signed = Opc == AMDGPUISD::BFE_I32; 588 589 uint32_t OffsetVal = Offset->getZExtValue(); 590 uint32_t WidthVal = Width->getZExtValue(); 591 592 ReplaceNode(N, getS_BFE(Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32, 593 SDLoc(N), N->getOperand(0), OffsetVal, WidthVal)); 594 return; 595 } 596 case AMDGPUISD::DIV_SCALE: { 597 SelectDIV_SCALE(N); 598 return; 599 } 600 case AMDGPUISD::MAD_I64_I32: 601 case AMDGPUISD::MAD_U64_U32: { 602 SelectMAD_64_32(N); 603 return; 604 } 605 case ISD::CopyToReg: { 606 const SITargetLowering& Lowering = 607 *static_cast<const SITargetLowering*>(getTargetLowering()); 608 N = Lowering.legalizeTargetIndependentNode(N, *CurDAG); 609 break; 610 } 611 case ISD::AND: 612 case ISD::SRL: 613 case ISD::SRA: 614 case ISD::SIGN_EXTEND_INREG: 615 if (N->getValueType(0) != MVT::i32) 616 break; 617 618 SelectS_BFE(N); 619 return; 620 case ISD::BRCOND: 621 SelectBRCOND(N); 622 return; 623 case ISD::FMAD: 624 SelectFMAD(N); 625 return; 626 case AMDGPUISD::ATOMIC_CMP_SWAP: 627 SelectATOMIC_CMP_SWAP(N); 628 return; 629 } 630 631 SelectCode(N); 632 } 633 634 bool AMDGPUDAGToDAGISel::isConstantLoad(const MemSDNode *N, int CbId) const { 635 if (!N->readMem()) 636 return false; 637 if (CbId == -1) 638 return N->getAddressSpace() == AMDGPUASI.CONSTANT_ADDRESS; 639 640 return N->getAddressSpace() == AMDGPUASI.CONSTANT_BUFFER_0 + CbId; 641 } 642 643 bool AMDGPUDAGToDAGISel::isUniformBr(const SDNode *N) const { 644 const BasicBlock *BB = FuncInfo->MBB->getBasicBlock(); 645 const Instruction *Term = BB->getTerminator(); 646 return Term->getMetadata("amdgpu.uniform") || 647 Term->getMetadata("structurizecfg.uniform"); 648 } 649 650 StringRef AMDGPUDAGToDAGISel::getPassName() const { 651 return "AMDGPU DAG->DAG Pattern Instruction Selection"; 652 } 653 654 //===----------------------------------------------------------------------===// 655 // Complex Patterns 656 //===----------------------------------------------------------------------===// 657 658 bool AMDGPUDAGToDAGISel::SelectGlobalValueConstantOffset(SDValue Addr, 659 SDValue& IntPtr) { 660 if (ConstantSDNode *Cst = dyn_cast<ConstantSDNode>(Addr)) { 661 IntPtr = CurDAG->getIntPtrConstant(Cst->getZExtValue() / 4, SDLoc(Addr), 662 true); 663 return true; 664 } 665 return false; 666 } 667 668 bool AMDGPUDAGToDAGISel::SelectGlobalValueVariableOffset(SDValue Addr, 669 SDValue& BaseReg, SDValue &Offset) { 670 if (!isa<ConstantSDNode>(Addr)) { 671 BaseReg = Addr; 672 Offset = CurDAG->getIntPtrConstant(0, SDLoc(Addr), true); 673 return true; 674 } 675 return false; 676 } 677 678 bool AMDGPUDAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base, 679 SDValue &Offset) { 680 return false; 681 } 682 683 bool AMDGPUDAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base, 684 SDValue &Offset) { 685 ConstantSDNode *C; 686 SDLoc DL(Addr); 687 688 if ((C = dyn_cast<ConstantSDNode>(Addr))) { 689 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); 690 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 691 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) && 692 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) { 693 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); 694 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 695 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) && 696 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) { 697 Base = Addr.getOperand(0); 698 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 699 } else { 700 Base = Addr; 701 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); 702 } 703 704 return true; 705 } 706 707 // FIXME: Should only handle addcarry/subcarry 708 void AMDGPUDAGToDAGISel::SelectADD_SUB_I64(SDNode *N) { 709 SDLoc DL(N); 710 SDValue LHS = N->getOperand(0); 711 SDValue RHS = N->getOperand(1); 712 713 unsigned Opcode = N->getOpcode(); 714 bool ConsumeCarry = (Opcode == ISD::ADDE || Opcode == ISD::SUBE); 715 bool ProduceCarry = 716 ConsumeCarry || Opcode == ISD::ADDC || Opcode == ISD::SUBC; 717 bool IsAdd = Opcode == ISD::ADD || Opcode == ISD::ADDC || Opcode == ISD::ADDE; 718 719 SDValue Sub0 = CurDAG->getTargetConstant(AMDGPU::sub0, DL, MVT::i32); 720 SDValue Sub1 = CurDAG->getTargetConstant(AMDGPU::sub1, DL, MVT::i32); 721 722 SDNode *Lo0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 723 DL, MVT::i32, LHS, Sub0); 724 SDNode *Hi0 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 725 DL, MVT::i32, LHS, Sub1); 726 727 SDNode *Lo1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 728 DL, MVT::i32, RHS, Sub0); 729 SDNode *Hi1 = CurDAG->getMachineNode(TargetOpcode::EXTRACT_SUBREG, 730 DL, MVT::i32, RHS, Sub1); 731 732 SDVTList VTList = CurDAG->getVTList(MVT::i32, MVT::Glue); 733 734 unsigned Opc = IsAdd ? AMDGPU::S_ADD_U32 : AMDGPU::S_SUB_U32; 735 unsigned CarryOpc = IsAdd ? AMDGPU::S_ADDC_U32 : AMDGPU::S_SUBB_U32; 736 737 SDNode *AddLo; 738 if (!ConsumeCarry) { 739 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0) }; 740 AddLo = CurDAG->getMachineNode(Opc, DL, VTList, Args); 741 } else { 742 SDValue Args[] = { SDValue(Lo0, 0), SDValue(Lo1, 0), N->getOperand(2) }; 743 AddLo = CurDAG->getMachineNode(CarryOpc, DL, VTList, Args); 744 } 745 SDValue AddHiArgs[] = { 746 SDValue(Hi0, 0), 747 SDValue(Hi1, 0), 748 SDValue(AddLo, 1) 749 }; 750 SDNode *AddHi = CurDAG->getMachineNode(CarryOpc, DL, VTList, AddHiArgs); 751 752 SDValue RegSequenceArgs[] = { 753 CurDAG->getTargetConstant(AMDGPU::SReg_64RegClassID, DL, MVT::i32), 754 SDValue(AddLo,0), 755 Sub0, 756 SDValue(AddHi,0), 757 Sub1, 758 }; 759 SDNode *RegSequence = CurDAG->getMachineNode(AMDGPU::REG_SEQUENCE, DL, 760 MVT::i64, RegSequenceArgs); 761 762 if (ProduceCarry) { 763 // Replace the carry-use 764 CurDAG->ReplaceAllUsesOfValueWith(SDValue(N, 1), SDValue(AddHi, 1)); 765 } 766 767 // Replace the remaining uses. 768 CurDAG->ReplaceAllUsesWith(N, RegSequence); 769 CurDAG->RemoveDeadNode(N); 770 } 771 772 void AMDGPUDAGToDAGISel::SelectUADDO_USUBO(SDNode *N) { 773 // The name of the opcodes are misleading. v_add_i32/v_sub_i32 have unsigned 774 // carry out despite the _i32 name. These were renamed in VI to _U32. 775 // FIXME: We should probably rename the opcodes here. 776 unsigned Opc = N->getOpcode() == ISD::UADDO ? 777 AMDGPU::V_ADD_I32_e64 : AMDGPU::V_SUB_I32_e64; 778 779 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), 780 { N->getOperand(0), N->getOperand(1) }); 781 } 782 783 void AMDGPUDAGToDAGISel::SelectFMA_W_CHAIN(SDNode *N) { 784 SDLoc SL(N); 785 // src0_modifiers, src0, src1_modifiers, src1, src2_modifiers, src2, clamp, omod 786 SDValue Ops[10]; 787 788 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[6], Ops[7]); 789 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]); 790 SelectVOP3Mods(N->getOperand(3), Ops[5], Ops[4]); 791 Ops[8] = N->getOperand(0); 792 Ops[9] = N->getOperand(4); 793 794 CurDAG->SelectNodeTo(N, AMDGPU::V_FMA_F32, N->getVTList(), Ops); 795 } 796 797 void AMDGPUDAGToDAGISel::SelectFMUL_W_CHAIN(SDNode *N) { 798 SDLoc SL(N); 799 // src0_modifiers, src0, src1_modifiers, src1, clamp, omod 800 SDValue Ops[8]; 801 802 SelectVOP3Mods0(N->getOperand(1), Ops[1], Ops[0], Ops[4], Ops[5]); 803 SelectVOP3Mods(N->getOperand(2), Ops[3], Ops[2]); 804 Ops[6] = N->getOperand(0); 805 Ops[7] = N->getOperand(3); 806 807 CurDAG->SelectNodeTo(N, AMDGPU::V_MUL_F32_e64, N->getVTList(), Ops); 808 } 809 810 // We need to handle this here because tablegen doesn't support matching 811 // instructions with multiple outputs. 812 void AMDGPUDAGToDAGISel::SelectDIV_SCALE(SDNode *N) { 813 SDLoc SL(N); 814 EVT VT = N->getValueType(0); 815 816 assert(VT == MVT::f32 || VT == MVT::f64); 817 818 unsigned Opc 819 = (VT == MVT::f64) ? AMDGPU::V_DIV_SCALE_F64 : AMDGPU::V_DIV_SCALE_F32; 820 821 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2) }; 822 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops); 823 } 824 825 // We need to handle this here because tablegen doesn't support matching 826 // instructions with multiple outputs. 827 void AMDGPUDAGToDAGISel::SelectMAD_64_32(SDNode *N) { 828 SDLoc SL(N); 829 bool Signed = N->getOpcode() == AMDGPUISD::MAD_I64_I32; 830 unsigned Opc = Signed ? AMDGPU::V_MAD_I64_I32 : AMDGPU::V_MAD_U64_U32; 831 832 SDValue Clamp = CurDAG->getTargetConstant(0, SL, MVT::i1); 833 SDValue Ops[] = { N->getOperand(0), N->getOperand(1), N->getOperand(2), 834 Clamp }; 835 CurDAG->SelectNodeTo(N, Opc, N->getVTList(), Ops); 836 } 837 838 bool AMDGPUDAGToDAGISel::isDSOffsetLegal(const SDValue &Base, unsigned Offset, 839 unsigned OffsetBits) const { 840 if ((OffsetBits == 16 && !isUInt<16>(Offset)) || 841 (OffsetBits == 8 && !isUInt<8>(Offset))) 842 return false; 843 844 if (Subtarget->getGeneration() >= AMDGPUSubtarget::SEA_ISLANDS || 845 Subtarget->unsafeDSOffsetFoldingEnabled()) 846 return true; 847 848 // On Southern Islands instruction with a negative base value and an offset 849 // don't seem to work. 850 return CurDAG->SignBitIsZero(Base); 851 } 852 853 bool AMDGPUDAGToDAGISel::SelectDS1Addr1Offset(SDValue Addr, SDValue &Base, 854 SDValue &Offset) const { 855 SDLoc DL(Addr); 856 if (CurDAG->isBaseWithConstantOffset(Addr)) { 857 SDValue N0 = Addr.getOperand(0); 858 SDValue N1 = Addr.getOperand(1); 859 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 860 if (isDSOffsetLegal(N0, C1->getSExtValue(), 16)) { 861 // (add n0, c0) 862 Base = N0; 863 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 864 return true; 865 } 866 } else if (Addr.getOpcode() == ISD::SUB) { 867 // sub C, x -> add (sub 0, x), C 868 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { 869 int64_t ByteOffset = C->getSExtValue(); 870 if (isUInt<16>(ByteOffset)) { 871 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 872 873 // XXX - This is kind of hacky. Create a dummy sub node so we can check 874 // the known bits in isDSOffsetLegal. We need to emit the selected node 875 // here, so this is thrown away. 876 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32, 877 Zero, Addr.getOperand(1)); 878 879 if (isDSOffsetLegal(Sub, ByteOffset, 16)) { 880 // FIXME: Select to VOP3 version for with-carry. 881 unsigned SubOp = Subtarget->hasAddNoCarry() ? 882 AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_I32_e32; 883 884 MachineSDNode *MachineSub 885 = CurDAG->getMachineNode(SubOp, DL, MVT::i32, 886 Zero, Addr.getOperand(1)); 887 888 Base = SDValue(MachineSub, 0); 889 Offset = CurDAG->getTargetConstant(ByteOffset, DL, MVT::i16); 890 return true; 891 } 892 } 893 } 894 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { 895 // If we have a constant address, prefer to put the constant into the 896 // offset. This can save moves to load the constant address since multiple 897 // operations can share the zero base address register, and enables merging 898 // into read2 / write2 instructions. 899 900 SDLoc DL(Addr); 901 902 if (isUInt<16>(CAddr->getZExtValue())) { 903 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 904 MachineSDNode *MovZero = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 905 DL, MVT::i32, Zero); 906 Base = SDValue(MovZero, 0); 907 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16); 908 return true; 909 } 910 } 911 912 // default case 913 Base = Addr; 914 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i16); 915 return true; 916 } 917 918 // TODO: If offset is too big, put low 16-bit into offset. 919 bool AMDGPUDAGToDAGISel::SelectDS64Bit4ByteAligned(SDValue Addr, SDValue &Base, 920 SDValue &Offset0, 921 SDValue &Offset1) const { 922 SDLoc DL(Addr); 923 924 if (CurDAG->isBaseWithConstantOffset(Addr)) { 925 SDValue N0 = Addr.getOperand(0); 926 SDValue N1 = Addr.getOperand(1); 927 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 928 unsigned DWordOffset0 = C1->getZExtValue() / 4; 929 unsigned DWordOffset1 = DWordOffset0 + 1; 930 // (add n0, c0) 931 if (isDSOffsetLegal(N0, DWordOffset1, 8)) { 932 Base = N0; 933 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); 934 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); 935 return true; 936 } 937 } else if (Addr.getOpcode() == ISD::SUB) { 938 // sub C, x -> add (sub 0, x), C 939 if (const ConstantSDNode *C = dyn_cast<ConstantSDNode>(Addr.getOperand(0))) { 940 unsigned DWordOffset0 = C->getZExtValue() / 4; 941 unsigned DWordOffset1 = DWordOffset0 + 1; 942 943 if (isUInt<8>(DWordOffset0)) { 944 SDLoc DL(Addr); 945 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 946 947 // XXX - This is kind of hacky. Create a dummy sub node so we can check 948 // the known bits in isDSOffsetLegal. We need to emit the selected node 949 // here, so this is thrown away. 950 SDValue Sub = CurDAG->getNode(ISD::SUB, DL, MVT::i32, 951 Zero, Addr.getOperand(1)); 952 953 if (isDSOffsetLegal(Sub, DWordOffset1, 8)) { 954 unsigned SubOp = Subtarget->hasAddNoCarry() ? 955 AMDGPU::V_SUB_U32_e64 : AMDGPU::V_SUB_I32_e32; 956 957 MachineSDNode *MachineSub 958 = CurDAG->getMachineNode(SubOp, DL, MVT::i32, 959 Zero, Addr.getOperand(1)); 960 961 Base = SDValue(MachineSub, 0); 962 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); 963 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); 964 return true; 965 } 966 } 967 } 968 } else if (const ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { 969 unsigned DWordOffset0 = CAddr->getZExtValue() / 4; 970 unsigned DWordOffset1 = DWordOffset0 + 1; 971 assert(4 * DWordOffset0 == CAddr->getZExtValue()); 972 973 if (isUInt<8>(DWordOffset0) && isUInt<8>(DWordOffset1)) { 974 SDValue Zero = CurDAG->getTargetConstant(0, DL, MVT::i32); 975 MachineSDNode *MovZero 976 = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 977 DL, MVT::i32, Zero); 978 Base = SDValue(MovZero, 0); 979 Offset0 = CurDAG->getTargetConstant(DWordOffset0, DL, MVT::i8); 980 Offset1 = CurDAG->getTargetConstant(DWordOffset1, DL, MVT::i8); 981 return true; 982 } 983 } 984 985 // default case 986 987 // FIXME: This is broken on SI where we still need to check if the base 988 // pointer is positive here. 989 Base = Addr; 990 Offset0 = CurDAG->getTargetConstant(0, DL, MVT::i8); 991 Offset1 = CurDAG->getTargetConstant(1, DL, MVT::i8); 992 return true; 993 } 994 995 bool AMDGPUDAGToDAGISel::SelectMUBUF(SDValue Addr, SDValue &Ptr, 996 SDValue &VAddr, SDValue &SOffset, 997 SDValue &Offset, SDValue &Offen, 998 SDValue &Idxen, SDValue &Addr64, 999 SDValue &GLC, SDValue &SLC, 1000 SDValue &TFE) const { 1001 // Subtarget prefers to use flat instruction 1002 if (Subtarget->useFlatForGlobal()) 1003 return false; 1004 1005 SDLoc DL(Addr); 1006 1007 if (!GLC.getNode()) 1008 GLC = CurDAG->getTargetConstant(0, DL, MVT::i1); 1009 if (!SLC.getNode()) 1010 SLC = CurDAG->getTargetConstant(0, DL, MVT::i1); 1011 TFE = CurDAG->getTargetConstant(0, DL, MVT::i1); 1012 1013 Idxen = CurDAG->getTargetConstant(0, DL, MVT::i1); 1014 Offen = CurDAG->getTargetConstant(0, DL, MVT::i1); 1015 Addr64 = CurDAG->getTargetConstant(0, DL, MVT::i1); 1016 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32); 1017 1018 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1019 SDValue N0 = Addr.getOperand(0); 1020 SDValue N1 = Addr.getOperand(1); 1021 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 1022 1023 if (N0.getOpcode() == ISD::ADD) { 1024 // (add (add N2, N3), C1) -> addr64 1025 SDValue N2 = N0.getOperand(0); 1026 SDValue N3 = N0.getOperand(1); 1027 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); 1028 Ptr = N2; 1029 VAddr = N3; 1030 } else { 1031 // (add N0, C1) -> offset 1032 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32); 1033 Ptr = N0; 1034 } 1035 1036 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue())) { 1037 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 1038 return true; 1039 } 1040 1041 if (isUInt<32>(C1->getZExtValue())) { 1042 // Illegal offset, store it in soffset. 1043 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1044 SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 1045 CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32)), 1046 0); 1047 return true; 1048 } 1049 } 1050 1051 if (Addr.getOpcode() == ISD::ADD) { 1052 // (add N0, N1) -> addr64 1053 SDValue N0 = Addr.getOperand(0); 1054 SDValue N1 = Addr.getOperand(1); 1055 Addr64 = CurDAG->getTargetConstant(1, DL, MVT::i1); 1056 Ptr = N0; 1057 VAddr = N1; 1058 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1059 return true; 1060 } 1061 1062 // default case -> offset 1063 VAddr = CurDAG->getTargetConstant(0, DL, MVT::i32); 1064 Ptr = Addr; 1065 Offset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1066 1067 return true; 1068 } 1069 1070 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 1071 SDValue &VAddr, SDValue &SOffset, 1072 SDValue &Offset, SDValue &GLC, 1073 SDValue &SLC, SDValue &TFE) const { 1074 SDValue Ptr, Offen, Idxen, Addr64; 1075 1076 // addr64 bit was removed for volcanic islands. 1077 if (Subtarget->getGeneration() >= AMDGPUSubtarget::VOLCANIC_ISLANDS) 1078 return false; 1079 1080 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, 1081 GLC, SLC, TFE)) 1082 return false; 1083 1084 ConstantSDNode *C = cast<ConstantSDNode>(Addr64); 1085 if (C->getSExtValue()) { 1086 SDLoc DL(Addr); 1087 1088 const SITargetLowering& Lowering = 1089 *static_cast<const SITargetLowering*>(getTargetLowering()); 1090 1091 SRsrc = SDValue(Lowering.wrapAddr64Rsrc(*CurDAG, DL, Ptr), 0); 1092 return true; 1093 } 1094 1095 return false; 1096 } 1097 1098 bool AMDGPUDAGToDAGISel::SelectMUBUFAddr64(SDValue Addr, SDValue &SRsrc, 1099 SDValue &VAddr, SDValue &SOffset, 1100 SDValue &Offset, 1101 SDValue &SLC) const { 1102 SLC = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i1); 1103 SDValue GLC, TFE; 1104 1105 return SelectMUBUFAddr64(Addr, SRsrc, VAddr, SOffset, Offset, GLC, SLC, TFE); 1106 } 1107 1108 static bool isStackPtrRelative(const MachinePointerInfo &PtrInfo) { 1109 auto PSV = PtrInfo.V.dyn_cast<const PseudoSourceValue *>(); 1110 return PSV && PSV->isStack(); 1111 } 1112 1113 std::pair<SDValue, SDValue> AMDGPUDAGToDAGISel::foldFrameIndex(SDValue N) const { 1114 const MachineFunction &MF = CurDAG->getMachineFunction(); 1115 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1116 1117 if (auto FI = dyn_cast<FrameIndexSDNode>(N)) { 1118 SDValue TFI = CurDAG->getTargetFrameIndex(FI->getIndex(), 1119 FI->getValueType(0)); 1120 1121 // If we can resolve this to a frame index access, this is relative to the 1122 // frame pointer SGPR. 1123 return std::make_pair(TFI, CurDAG->getRegister(Info->getFrameOffsetReg(), 1124 MVT::i32)); 1125 } 1126 1127 // If we don't know this private access is a local stack object, it needs to 1128 // be relative to the entry point's scratch wave offset register. 1129 return std::make_pair(N, CurDAG->getRegister(Info->getScratchWaveOffsetReg(), 1130 MVT::i32)); 1131 } 1132 1133 bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffen(SDNode *Parent, 1134 SDValue Addr, SDValue &Rsrc, 1135 SDValue &VAddr, SDValue &SOffset, 1136 SDValue &ImmOffset) const { 1137 1138 SDLoc DL(Addr); 1139 MachineFunction &MF = CurDAG->getMachineFunction(); 1140 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1141 1142 Rsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32); 1143 1144 if (ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr)) { 1145 unsigned Imm = CAddr->getZExtValue(); 1146 1147 SDValue HighBits = CurDAG->getTargetConstant(Imm & ~4095, DL, MVT::i32); 1148 MachineSDNode *MovHighBits = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 1149 DL, MVT::i32, HighBits); 1150 VAddr = SDValue(MovHighBits, 0); 1151 1152 // In a call sequence, stores to the argument stack area are relative to the 1153 // stack pointer. 1154 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo(); 1155 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ? 1156 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg(); 1157 1158 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32); 1159 ImmOffset = CurDAG->getTargetConstant(Imm & 4095, DL, MVT::i16); 1160 return true; 1161 } 1162 1163 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1164 // (add n0, c1) 1165 1166 SDValue N0 = Addr.getOperand(0); 1167 SDValue N1 = Addr.getOperand(1); 1168 1169 // Offsets in vaddr must be positive if range checking is enabled. 1170 // 1171 // The total computation of vaddr + soffset + offset must not overflow. If 1172 // vaddr is negative, even if offset is 0 the sgpr offset add will end up 1173 // overflowing. 1174 // 1175 // Prior to gfx9, MUBUF instructions with the vaddr offset enabled would 1176 // always perform a range check. If a negative vaddr base index was used, 1177 // this would fail the range check. The overall address computation would 1178 // compute a valid address, but this doesn't happen due to the range 1179 // check. For out-of-bounds MUBUF loads, a 0 is returned. 1180 // 1181 // Therefore it should be safe to fold any VGPR offset on gfx9 into the 1182 // MUBUF vaddr, but not on older subtargets which can only do this if the 1183 // sign bit is known 0. 1184 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 1185 if (SIInstrInfo::isLegalMUBUFImmOffset(C1->getZExtValue()) && 1186 (!Subtarget->privateMemoryResourceIsRangeChecked() || 1187 CurDAG->SignBitIsZero(N0))) { 1188 std::tie(VAddr, SOffset) = foldFrameIndex(N0); 1189 ImmOffset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i16); 1190 return true; 1191 } 1192 } 1193 1194 // (node) 1195 std::tie(VAddr, SOffset) = foldFrameIndex(Addr); 1196 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1197 return true; 1198 } 1199 1200 bool AMDGPUDAGToDAGISel::SelectMUBUFScratchOffset(SDNode *Parent, 1201 SDValue Addr, 1202 SDValue &SRsrc, 1203 SDValue &SOffset, 1204 SDValue &Offset) const { 1205 ConstantSDNode *CAddr = dyn_cast<ConstantSDNode>(Addr); 1206 if (!CAddr || !SIInstrInfo::isLegalMUBUFImmOffset(CAddr->getZExtValue())) 1207 return false; 1208 1209 SDLoc DL(Addr); 1210 MachineFunction &MF = CurDAG->getMachineFunction(); 1211 const SIMachineFunctionInfo *Info = MF.getInfo<SIMachineFunctionInfo>(); 1212 1213 SRsrc = CurDAG->getRegister(Info->getScratchRSrcReg(), MVT::v4i32); 1214 1215 const MachinePointerInfo &PtrInfo = cast<MemSDNode>(Parent)->getPointerInfo(); 1216 unsigned SOffsetReg = isStackPtrRelative(PtrInfo) ? 1217 Info->getStackPtrOffsetReg() : Info->getScratchWaveOffsetReg(); 1218 1219 // FIXME: Get from MachinePointerInfo? We should only be using the frame 1220 // offset if we know this is in a call sequence. 1221 SOffset = CurDAG->getRegister(SOffsetReg, MVT::i32); 1222 1223 Offset = CurDAG->getTargetConstant(CAddr->getZExtValue(), DL, MVT::i16); 1224 return true; 1225 } 1226 1227 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 1228 SDValue &SOffset, SDValue &Offset, 1229 SDValue &GLC, SDValue &SLC, 1230 SDValue &TFE) const { 1231 SDValue Ptr, VAddr, Offen, Idxen, Addr64; 1232 const SIInstrInfo *TII = 1233 static_cast<const SIInstrInfo *>(Subtarget->getInstrInfo()); 1234 1235 if (!SelectMUBUF(Addr, Ptr, VAddr, SOffset, Offset, Offen, Idxen, Addr64, 1236 GLC, SLC, TFE)) 1237 return false; 1238 1239 if (!cast<ConstantSDNode>(Offen)->getSExtValue() && 1240 !cast<ConstantSDNode>(Idxen)->getSExtValue() && 1241 !cast<ConstantSDNode>(Addr64)->getSExtValue()) { 1242 uint64_t Rsrc = TII->getDefaultRsrcDataFormat() | 1243 APInt::getAllOnesValue(32).getZExtValue(); // Size 1244 SDLoc DL(Addr); 1245 1246 const SITargetLowering& Lowering = 1247 *static_cast<const SITargetLowering*>(getTargetLowering()); 1248 1249 SRsrc = SDValue(Lowering.buildRSRC(*CurDAG, DL, Ptr, 0, Rsrc), 0); 1250 return true; 1251 } 1252 return false; 1253 } 1254 1255 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 1256 SDValue &Soffset, SDValue &Offset 1257 ) const { 1258 SDValue GLC, SLC, TFE; 1259 1260 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE); 1261 } 1262 bool AMDGPUDAGToDAGISel::SelectMUBUFOffset(SDValue Addr, SDValue &SRsrc, 1263 SDValue &Soffset, SDValue &Offset, 1264 SDValue &SLC) const { 1265 SDValue GLC, TFE; 1266 1267 return SelectMUBUFOffset(Addr, SRsrc, Soffset, Offset, GLC, SLC, TFE); 1268 } 1269 1270 bool AMDGPUDAGToDAGISel::SelectMUBUFConstant(SDValue Constant, 1271 SDValue &SOffset, 1272 SDValue &ImmOffset) const { 1273 SDLoc DL(Constant); 1274 const uint32_t Align = 4; 1275 const uint32_t MaxImm = alignDown(4095, Align); 1276 uint32_t Imm = cast<ConstantSDNode>(Constant)->getZExtValue(); 1277 uint32_t Overflow = 0; 1278 1279 if (Imm > MaxImm) { 1280 if (Imm <= MaxImm + 64) { 1281 // Use an SOffset inline constant for 4..64 1282 Overflow = Imm - MaxImm; 1283 Imm = MaxImm; 1284 } else { 1285 // Try to keep the same value in SOffset for adjacent loads, so that 1286 // the corresponding register contents can be re-used. 1287 // 1288 // Load values with all low-bits (except for alignment bits) set into 1289 // SOffset, so that a larger range of values can be covered using 1290 // s_movk_i32. 1291 // 1292 // Atomic operations fail to work correctly when individual address 1293 // components are unaligned, even if their sum is aligned. 1294 uint32_t High = (Imm + Align) & ~4095; 1295 uint32_t Low = (Imm + Align) & 4095; 1296 Imm = Low; 1297 Overflow = High - Align; 1298 } 1299 } 1300 1301 // There is a hardware bug in SI and CI which prevents address clamping in 1302 // MUBUF instructions from working correctly with SOffsets. The immediate 1303 // offset is unaffected. 1304 if (Overflow > 0 && 1305 Subtarget->getGeneration() <= AMDGPUSubtarget::SEA_ISLANDS) 1306 return false; 1307 1308 ImmOffset = CurDAG->getTargetConstant(Imm, DL, MVT::i16); 1309 1310 if (Overflow <= 64) 1311 SOffset = CurDAG->getTargetConstant(Overflow, DL, MVT::i32); 1312 else 1313 SOffset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, DL, MVT::i32, 1314 CurDAG->getTargetConstant(Overflow, DL, MVT::i32)), 1315 0); 1316 1317 return true; 1318 } 1319 1320 bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicOffset(SDValue Offset, 1321 SDValue &SOffset, 1322 SDValue &ImmOffset) const { 1323 SDLoc DL(Offset); 1324 1325 if (!isa<ConstantSDNode>(Offset)) 1326 return false; 1327 1328 return SelectMUBUFConstant(Offset, SOffset, ImmOffset); 1329 } 1330 1331 bool AMDGPUDAGToDAGISel::SelectMUBUFIntrinsicVOffset(SDValue Offset, 1332 SDValue &SOffset, 1333 SDValue &ImmOffset, 1334 SDValue &VOffset) const { 1335 SDLoc DL(Offset); 1336 1337 // Don't generate an unnecessary voffset for constant offsets. 1338 if (isa<ConstantSDNode>(Offset)) { 1339 SDValue Tmp1, Tmp2; 1340 1341 // When necessary, use a voffset in <= CI anyway to work around a hardware 1342 // bug. 1343 if (Subtarget->getGeneration() > AMDGPUSubtarget::SEA_ISLANDS || 1344 SelectMUBUFConstant(Offset, Tmp1, Tmp2)) 1345 return false; 1346 } 1347 1348 if (CurDAG->isBaseWithConstantOffset(Offset)) { 1349 SDValue N0 = Offset.getOperand(0); 1350 SDValue N1 = Offset.getOperand(1); 1351 if (cast<ConstantSDNode>(N1)->getSExtValue() >= 0 && 1352 SelectMUBUFConstant(N1, SOffset, ImmOffset)) { 1353 VOffset = N0; 1354 return true; 1355 } 1356 } 1357 1358 SOffset = CurDAG->getTargetConstant(0, DL, MVT::i32); 1359 ImmOffset = CurDAG->getTargetConstant(0, DL, MVT::i16); 1360 VOffset = Offset; 1361 1362 return true; 1363 } 1364 1365 template <bool IsSigned> 1366 bool AMDGPUDAGToDAGISel::SelectFlatOffset(SDValue Addr, 1367 SDValue &VAddr, 1368 SDValue &Offset, 1369 SDValue &SLC) const { 1370 int64_t OffsetVal = 0; 1371 1372 if (Subtarget->hasFlatInstOffsets() && 1373 CurDAG->isBaseWithConstantOffset(Addr)) { 1374 SDValue N0 = Addr.getOperand(0); 1375 SDValue N1 = Addr.getOperand(1); 1376 int64_t COffsetVal = cast<ConstantSDNode>(N1)->getSExtValue(); 1377 1378 if ((IsSigned && isInt<13>(COffsetVal)) || 1379 (!IsSigned && isUInt<12>(COffsetVal))) { 1380 Addr = N0; 1381 OffsetVal = COffsetVal; 1382 } 1383 } 1384 1385 VAddr = Addr; 1386 Offset = CurDAG->getTargetConstant(OffsetVal, SDLoc(), MVT::i16); 1387 SLC = CurDAG->getTargetConstant(0, SDLoc(), MVT::i1); 1388 1389 return true; 1390 } 1391 1392 bool AMDGPUDAGToDAGISel::SelectFlatAtomic(SDValue Addr, 1393 SDValue &VAddr, 1394 SDValue &Offset, 1395 SDValue &SLC) const { 1396 return SelectFlatOffset<false>(Addr, VAddr, Offset, SLC); 1397 } 1398 1399 bool AMDGPUDAGToDAGISel::SelectFlatAtomicSigned(SDValue Addr, 1400 SDValue &VAddr, 1401 SDValue &Offset, 1402 SDValue &SLC) const { 1403 return SelectFlatOffset<true>(Addr, VAddr, Offset, SLC); 1404 } 1405 1406 bool AMDGPUDAGToDAGISel::SelectSMRDOffset(SDValue ByteOffsetNode, 1407 SDValue &Offset, bool &Imm) const { 1408 1409 // FIXME: Handle non-constant offsets. 1410 ConstantSDNode *C = dyn_cast<ConstantSDNode>(ByteOffsetNode); 1411 if (!C) 1412 return false; 1413 1414 SDLoc SL(ByteOffsetNode); 1415 AMDGPUSubtarget::Generation Gen = Subtarget->getGeneration(); 1416 int64_t ByteOffset = C->getSExtValue(); 1417 int64_t EncodedOffset = AMDGPU::getSMRDEncodedOffset(*Subtarget, ByteOffset); 1418 1419 if (AMDGPU::isLegalSMRDImmOffset(*Subtarget, ByteOffset)) { 1420 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32); 1421 Imm = true; 1422 return true; 1423 } 1424 1425 if (!isUInt<32>(EncodedOffset) || !isUInt<32>(ByteOffset)) 1426 return false; 1427 1428 if (Gen == AMDGPUSubtarget::SEA_ISLANDS && isUInt<32>(EncodedOffset)) { 1429 // 32-bit Immediates are supported on Sea Islands. 1430 Offset = CurDAG->getTargetConstant(EncodedOffset, SL, MVT::i32); 1431 } else { 1432 SDValue C32Bit = CurDAG->getTargetConstant(ByteOffset, SL, MVT::i32); 1433 Offset = SDValue(CurDAG->getMachineNode(AMDGPU::S_MOV_B32, SL, MVT::i32, 1434 C32Bit), 0); 1435 } 1436 Imm = false; 1437 return true; 1438 } 1439 1440 bool AMDGPUDAGToDAGISel::SelectSMRD(SDValue Addr, SDValue &SBase, 1441 SDValue &Offset, bool &Imm) const { 1442 SDLoc SL(Addr); 1443 if (CurDAG->isBaseWithConstantOffset(Addr)) { 1444 SDValue N0 = Addr.getOperand(0); 1445 SDValue N1 = Addr.getOperand(1); 1446 1447 if (SelectSMRDOffset(N1, Offset, Imm)) { 1448 SBase = N0; 1449 return true; 1450 } 1451 } 1452 SBase = Addr; 1453 Offset = CurDAG->getTargetConstant(0, SL, MVT::i32); 1454 Imm = true; 1455 return true; 1456 } 1457 1458 bool AMDGPUDAGToDAGISel::SelectSMRDImm(SDValue Addr, SDValue &SBase, 1459 SDValue &Offset) const { 1460 bool Imm; 1461 return SelectSMRD(Addr, SBase, Offset, Imm) && Imm; 1462 } 1463 1464 bool AMDGPUDAGToDAGISel::SelectSMRDImm32(SDValue Addr, SDValue &SBase, 1465 SDValue &Offset) const { 1466 1467 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS) 1468 return false; 1469 1470 bool Imm; 1471 if (!SelectSMRD(Addr, SBase, Offset, Imm)) 1472 return false; 1473 1474 return !Imm && isa<ConstantSDNode>(Offset); 1475 } 1476 1477 bool AMDGPUDAGToDAGISel::SelectSMRDSgpr(SDValue Addr, SDValue &SBase, 1478 SDValue &Offset) const { 1479 bool Imm; 1480 return SelectSMRD(Addr, SBase, Offset, Imm) && !Imm && 1481 !isa<ConstantSDNode>(Offset); 1482 } 1483 1484 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm(SDValue Addr, 1485 SDValue &Offset) const { 1486 bool Imm; 1487 return SelectSMRDOffset(Addr, Offset, Imm) && Imm; 1488 } 1489 1490 bool AMDGPUDAGToDAGISel::SelectSMRDBufferImm32(SDValue Addr, 1491 SDValue &Offset) const { 1492 if (Subtarget->getGeneration() != AMDGPUSubtarget::SEA_ISLANDS) 1493 return false; 1494 1495 bool Imm; 1496 if (!SelectSMRDOffset(Addr, Offset, Imm)) 1497 return false; 1498 1499 return !Imm && isa<ConstantSDNode>(Offset); 1500 } 1501 1502 bool AMDGPUDAGToDAGISel::SelectMOVRELOffset(SDValue Index, 1503 SDValue &Base, 1504 SDValue &Offset) const { 1505 SDLoc DL(Index); 1506 1507 if (CurDAG->isBaseWithConstantOffset(Index)) { 1508 SDValue N0 = Index.getOperand(0); 1509 SDValue N1 = Index.getOperand(1); 1510 ConstantSDNode *C1 = cast<ConstantSDNode>(N1); 1511 1512 // (add n0, c0) 1513 Base = N0; 1514 Offset = CurDAG->getTargetConstant(C1->getZExtValue(), DL, MVT::i32); 1515 return true; 1516 } 1517 1518 if (isa<ConstantSDNode>(Index)) 1519 return false; 1520 1521 Base = Index; 1522 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); 1523 return true; 1524 } 1525 1526 SDNode *AMDGPUDAGToDAGISel::getS_BFE(unsigned Opcode, const SDLoc &DL, 1527 SDValue Val, uint32_t Offset, 1528 uint32_t Width) { 1529 // Transformation function, pack the offset and width of a BFE into 1530 // the format expected by the S_BFE_I32 / S_BFE_U32. In the second 1531 // source, bits [5:0] contain the offset and bits [22:16] the width. 1532 uint32_t PackedVal = Offset | (Width << 16); 1533 SDValue PackedConst = CurDAG->getTargetConstant(PackedVal, DL, MVT::i32); 1534 1535 return CurDAG->getMachineNode(Opcode, DL, MVT::i32, Val, PackedConst); 1536 } 1537 1538 void AMDGPUDAGToDAGISel::SelectS_BFEFromShifts(SDNode *N) { 1539 // "(a << b) srl c)" ---> "BFE_U32 a, (c-b), (32-c) 1540 // "(a << b) sra c)" ---> "BFE_I32 a, (c-b), (32-c) 1541 // Predicate: 0 < b <= c < 32 1542 1543 const SDValue &Shl = N->getOperand(0); 1544 ConstantSDNode *B = dyn_cast<ConstantSDNode>(Shl->getOperand(1)); 1545 ConstantSDNode *C = dyn_cast<ConstantSDNode>(N->getOperand(1)); 1546 1547 if (B && C) { 1548 uint32_t BVal = B->getZExtValue(); 1549 uint32_t CVal = C->getZExtValue(); 1550 1551 if (0 < BVal && BVal <= CVal && CVal < 32) { 1552 bool Signed = N->getOpcode() == ISD::SRA; 1553 unsigned Opcode = Signed ? AMDGPU::S_BFE_I32 : AMDGPU::S_BFE_U32; 1554 1555 ReplaceNode(N, getS_BFE(Opcode, SDLoc(N), Shl.getOperand(0), CVal - BVal, 1556 32 - CVal)); 1557 return; 1558 } 1559 } 1560 SelectCode(N); 1561 } 1562 1563 void AMDGPUDAGToDAGISel::SelectS_BFE(SDNode *N) { 1564 switch (N->getOpcode()) { 1565 case ISD::AND: 1566 if (N->getOperand(0).getOpcode() == ISD::SRL) { 1567 // "(a srl b) & mask" ---> "BFE_U32 a, b, popcount(mask)" 1568 // Predicate: isMask(mask) 1569 const SDValue &Srl = N->getOperand(0); 1570 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(Srl.getOperand(1)); 1571 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(N->getOperand(1)); 1572 1573 if (Shift && Mask) { 1574 uint32_t ShiftVal = Shift->getZExtValue(); 1575 uint32_t MaskVal = Mask->getZExtValue(); 1576 1577 if (isMask_32(MaskVal)) { 1578 uint32_t WidthVal = countPopulation(MaskVal); 1579 1580 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), 1581 Srl.getOperand(0), ShiftVal, WidthVal)); 1582 return; 1583 } 1584 } 1585 } 1586 break; 1587 case ISD::SRL: 1588 if (N->getOperand(0).getOpcode() == ISD::AND) { 1589 // "(a & mask) srl b)" ---> "BFE_U32 a, b, popcount(mask >> b)" 1590 // Predicate: isMask(mask >> b) 1591 const SDValue &And = N->getOperand(0); 1592 ConstantSDNode *Shift = dyn_cast<ConstantSDNode>(N->getOperand(1)); 1593 ConstantSDNode *Mask = dyn_cast<ConstantSDNode>(And->getOperand(1)); 1594 1595 if (Shift && Mask) { 1596 uint32_t ShiftVal = Shift->getZExtValue(); 1597 uint32_t MaskVal = Mask->getZExtValue() >> ShiftVal; 1598 1599 if (isMask_32(MaskVal)) { 1600 uint32_t WidthVal = countPopulation(MaskVal); 1601 1602 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_U32, SDLoc(N), 1603 And.getOperand(0), ShiftVal, WidthVal)); 1604 return; 1605 } 1606 } 1607 } else if (N->getOperand(0).getOpcode() == ISD::SHL) { 1608 SelectS_BFEFromShifts(N); 1609 return; 1610 } 1611 break; 1612 case ISD::SRA: 1613 if (N->getOperand(0).getOpcode() == ISD::SHL) { 1614 SelectS_BFEFromShifts(N); 1615 return; 1616 } 1617 break; 1618 1619 case ISD::SIGN_EXTEND_INREG: { 1620 // sext_inreg (srl x, 16), i8 -> bfe_i32 x, 16, 8 1621 SDValue Src = N->getOperand(0); 1622 if (Src.getOpcode() != ISD::SRL) 1623 break; 1624 1625 const ConstantSDNode *Amt = dyn_cast<ConstantSDNode>(Src.getOperand(1)); 1626 if (!Amt) 1627 break; 1628 1629 unsigned Width = cast<VTSDNode>(N->getOperand(1))->getVT().getSizeInBits(); 1630 ReplaceNode(N, getS_BFE(AMDGPU::S_BFE_I32, SDLoc(N), Src.getOperand(0), 1631 Amt->getZExtValue(), Width)); 1632 return; 1633 } 1634 } 1635 1636 SelectCode(N); 1637 } 1638 1639 bool AMDGPUDAGToDAGISel::isCBranchSCC(const SDNode *N) const { 1640 assert(N->getOpcode() == ISD::BRCOND); 1641 if (!N->hasOneUse()) 1642 return false; 1643 1644 SDValue Cond = N->getOperand(1); 1645 if (Cond.getOpcode() == ISD::CopyToReg) 1646 Cond = Cond.getOperand(2); 1647 1648 if (Cond.getOpcode() != ISD::SETCC || !Cond.hasOneUse()) 1649 return false; 1650 1651 MVT VT = Cond.getOperand(0).getSimpleValueType(); 1652 if (VT == MVT::i32) 1653 return true; 1654 1655 if (VT == MVT::i64) { 1656 auto ST = static_cast<const SISubtarget *>(Subtarget); 1657 1658 ISD::CondCode CC = cast<CondCodeSDNode>(Cond.getOperand(2))->get(); 1659 return (CC == ISD::SETEQ || CC == ISD::SETNE) && ST->hasScalarCompareEq64(); 1660 } 1661 1662 return false; 1663 } 1664 1665 void AMDGPUDAGToDAGISel::SelectBRCOND(SDNode *N) { 1666 SDValue Cond = N->getOperand(1); 1667 1668 if (Cond.isUndef()) { 1669 CurDAG->SelectNodeTo(N, AMDGPU::SI_BR_UNDEF, MVT::Other, 1670 N->getOperand(2), N->getOperand(0)); 1671 return; 1672 } 1673 1674 bool UseSCCBr = isCBranchSCC(N) && isUniformBr(N); 1675 unsigned BrOp = UseSCCBr ? AMDGPU::S_CBRANCH_SCC1 : AMDGPU::S_CBRANCH_VCCNZ; 1676 unsigned CondReg = UseSCCBr ? AMDGPU::SCC : AMDGPU::VCC; 1677 SDLoc SL(N); 1678 1679 SDValue VCC = CurDAG->getCopyToReg(N->getOperand(0), SL, CondReg, Cond); 1680 CurDAG->SelectNodeTo(N, BrOp, MVT::Other, 1681 N->getOperand(2), // Basic Block 1682 VCC.getValue(0)); 1683 } 1684 1685 void AMDGPUDAGToDAGISel::SelectFMAD(SDNode *N) { 1686 MVT VT = N->getSimpleValueType(0); 1687 if (VT != MVT::f32 || !Subtarget->hasMadMixInsts()) { 1688 SelectCode(N); 1689 return; 1690 } 1691 1692 SDValue Src0 = N->getOperand(0); 1693 SDValue Src1 = N->getOperand(1); 1694 SDValue Src2 = N->getOperand(2); 1695 unsigned Src0Mods, Src1Mods, Src2Mods; 1696 1697 // Avoid using v_mad_mix_f32 unless there is actually an operand using the 1698 // conversion from f16. 1699 bool Sel0 = SelectVOP3PMadMixModsImpl(Src0, Src0, Src0Mods); 1700 bool Sel1 = SelectVOP3PMadMixModsImpl(Src1, Src1, Src1Mods); 1701 bool Sel2 = SelectVOP3PMadMixModsImpl(Src2, Src2, Src2Mods); 1702 1703 assert(!Subtarget->hasFP32Denormals() && 1704 "fmad selected with denormals enabled"); 1705 // TODO: We can select this with f32 denormals enabled if all the sources are 1706 // converted from f16 (in which case fmad isn't legal). 1707 1708 if (Sel0 || Sel1 || Sel2) { 1709 // For dummy operands. 1710 SDValue Zero = CurDAG->getTargetConstant(0, SDLoc(), MVT::i32); 1711 SDValue Ops[] = { 1712 CurDAG->getTargetConstant(Src0Mods, SDLoc(), MVT::i32), Src0, 1713 CurDAG->getTargetConstant(Src1Mods, SDLoc(), MVT::i32), Src1, 1714 CurDAG->getTargetConstant(Src2Mods, SDLoc(), MVT::i32), Src2, 1715 CurDAG->getTargetConstant(0, SDLoc(), MVT::i1), 1716 Zero, Zero 1717 }; 1718 1719 CurDAG->SelectNodeTo(N, AMDGPU::V_MAD_MIX_F32, MVT::f32, Ops); 1720 } else { 1721 SelectCode(N); 1722 } 1723 } 1724 1725 // This is here because there isn't a way to use the generated sub0_sub1 as the 1726 // subreg index to EXTRACT_SUBREG in tablegen. 1727 void AMDGPUDAGToDAGISel::SelectATOMIC_CMP_SWAP(SDNode *N) { 1728 MemSDNode *Mem = cast<MemSDNode>(N); 1729 unsigned AS = Mem->getAddressSpace(); 1730 if (AS == AMDGPUASI.FLAT_ADDRESS) { 1731 SelectCode(N); 1732 return; 1733 } 1734 1735 MVT VT = N->getSimpleValueType(0); 1736 bool Is32 = (VT == MVT::i32); 1737 SDLoc SL(N); 1738 1739 MachineSDNode *CmpSwap = nullptr; 1740 if (Subtarget->hasAddr64()) { 1741 SDValue SRsrc, VAddr, SOffset, Offset, SLC; 1742 1743 if (SelectMUBUFAddr64(Mem->getBasePtr(), SRsrc, VAddr, SOffset, Offset, SLC)) { 1744 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_ADDR64_RTN : 1745 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_ADDR64_RTN; 1746 SDValue CmpVal = Mem->getOperand(2); 1747 1748 // XXX - Do we care about glue operands? 1749 1750 SDValue Ops[] = { 1751 CmpVal, VAddr, SRsrc, SOffset, Offset, SLC, Mem->getChain() 1752 }; 1753 1754 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops); 1755 } 1756 } 1757 1758 if (!CmpSwap) { 1759 SDValue SRsrc, SOffset, Offset, SLC; 1760 if (SelectMUBUFOffset(Mem->getBasePtr(), SRsrc, SOffset, Offset, SLC)) { 1761 unsigned Opcode = Is32 ? AMDGPU::BUFFER_ATOMIC_CMPSWAP_OFFSET_RTN : 1762 AMDGPU::BUFFER_ATOMIC_CMPSWAP_X2_OFFSET_RTN; 1763 1764 SDValue CmpVal = Mem->getOperand(2); 1765 SDValue Ops[] = { 1766 CmpVal, SRsrc, SOffset, Offset, SLC, Mem->getChain() 1767 }; 1768 1769 CmpSwap = CurDAG->getMachineNode(Opcode, SL, Mem->getVTList(), Ops); 1770 } 1771 } 1772 1773 if (!CmpSwap) { 1774 SelectCode(N); 1775 return; 1776 } 1777 1778 MachineSDNode::mmo_iterator MMOs = MF->allocateMemRefsArray(1); 1779 *MMOs = Mem->getMemOperand(); 1780 CmpSwap->setMemRefs(MMOs, MMOs + 1); 1781 1782 unsigned SubReg = Is32 ? AMDGPU::sub0 : AMDGPU::sub0_sub1; 1783 SDValue Extract 1784 = CurDAG->getTargetExtractSubreg(SubReg, SL, VT, SDValue(CmpSwap, 0)); 1785 1786 ReplaceUses(SDValue(N, 0), Extract); 1787 ReplaceUses(SDValue(N, 1), SDValue(CmpSwap, 1)); 1788 CurDAG->RemoveDeadNode(N); 1789 } 1790 1791 bool AMDGPUDAGToDAGISel::SelectVOP3ModsImpl(SDValue In, SDValue &Src, 1792 unsigned &Mods) const { 1793 Mods = 0; 1794 Src = In; 1795 1796 if (Src.getOpcode() == ISD::FNEG) { 1797 Mods |= SISrcMods::NEG; 1798 Src = Src.getOperand(0); 1799 } 1800 1801 if (Src.getOpcode() == ISD::FABS) { 1802 Mods |= SISrcMods::ABS; 1803 Src = Src.getOperand(0); 1804 } 1805 1806 return true; 1807 } 1808 1809 bool AMDGPUDAGToDAGISel::SelectVOP3Mods(SDValue In, SDValue &Src, 1810 SDValue &SrcMods) const { 1811 unsigned Mods; 1812 if (SelectVOP3ModsImpl(In, Src, Mods)) { 1813 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 1814 return true; 1815 } 1816 1817 return false; 1818 } 1819 1820 bool AMDGPUDAGToDAGISel::SelectVOP3Mods_NNaN(SDValue In, SDValue &Src, 1821 SDValue &SrcMods) const { 1822 SelectVOP3Mods(In, Src, SrcMods); 1823 return isNoNanSrc(Src); 1824 } 1825 1826 bool AMDGPUDAGToDAGISel::SelectVOP3NoMods(SDValue In, SDValue &Src) const { 1827 if (In.getOpcode() == ISD::FABS || In.getOpcode() == ISD::FNEG) 1828 return false; 1829 1830 Src = In; 1831 return true; 1832 } 1833 1834 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0(SDValue In, SDValue &Src, 1835 SDValue &SrcMods, SDValue &Clamp, 1836 SDValue &Omod) const { 1837 SDLoc DL(In); 1838 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1); 1839 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1); 1840 1841 return SelectVOP3Mods(In, Src, SrcMods); 1842 } 1843 1844 bool AMDGPUDAGToDAGISel::SelectVOP3Mods0Clamp0OMod(SDValue In, SDValue &Src, 1845 SDValue &SrcMods, 1846 SDValue &Clamp, 1847 SDValue &Omod) const { 1848 Clamp = Omod = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32); 1849 return SelectVOP3Mods(In, Src, SrcMods); 1850 } 1851 1852 bool AMDGPUDAGToDAGISel::SelectVOP3OMods(SDValue In, SDValue &Src, 1853 SDValue &Clamp, SDValue &Omod) const { 1854 Src = In; 1855 1856 SDLoc DL(In); 1857 Clamp = CurDAG->getTargetConstant(0, DL, MVT::i1); 1858 Omod = CurDAG->getTargetConstant(0, DL, MVT::i1); 1859 1860 return true; 1861 } 1862 1863 static SDValue stripBitcast(SDValue Val) { 1864 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val; 1865 } 1866 1867 // Figure out if this is really an extract of the high 16-bits of a dword. 1868 static bool isExtractHiElt(SDValue In, SDValue &Out) { 1869 In = stripBitcast(In); 1870 if (In.getOpcode() != ISD::TRUNCATE) 1871 return false; 1872 1873 SDValue Srl = In.getOperand(0); 1874 if (Srl.getOpcode() == ISD::SRL) { 1875 if (ConstantSDNode *ShiftAmt = dyn_cast<ConstantSDNode>(Srl.getOperand(1))) { 1876 if (ShiftAmt->getZExtValue() == 16) { 1877 Out = stripBitcast(Srl.getOperand(0)); 1878 return true; 1879 } 1880 } 1881 } 1882 1883 return false; 1884 } 1885 1886 // Look through operations that obscure just looking at the low 16-bits of the 1887 // same register. 1888 static SDValue stripExtractLoElt(SDValue In) { 1889 if (In.getOpcode() == ISD::TRUNCATE) { 1890 SDValue Src = In.getOperand(0); 1891 if (Src.getValueType().getSizeInBits() == 32) 1892 return stripBitcast(Src); 1893 } 1894 1895 return In; 1896 } 1897 1898 bool AMDGPUDAGToDAGISel::SelectVOP3PMods(SDValue In, SDValue &Src, 1899 SDValue &SrcMods) const { 1900 unsigned Mods = 0; 1901 Src = In; 1902 1903 if (Src.getOpcode() == ISD::FNEG) { 1904 Mods ^= (SISrcMods::NEG | SISrcMods::NEG_HI); 1905 Src = Src.getOperand(0); 1906 } 1907 1908 if (Src.getOpcode() == ISD::BUILD_VECTOR) { 1909 unsigned VecMods = Mods; 1910 1911 SDValue Lo = stripBitcast(Src.getOperand(0)); 1912 SDValue Hi = stripBitcast(Src.getOperand(1)); 1913 1914 if (Lo.getOpcode() == ISD::FNEG) { 1915 Lo = stripBitcast(Lo.getOperand(0)); 1916 Mods ^= SISrcMods::NEG; 1917 } 1918 1919 if (Hi.getOpcode() == ISD::FNEG) { 1920 Hi = stripBitcast(Hi.getOperand(0)); 1921 Mods ^= SISrcMods::NEG_HI; 1922 } 1923 1924 if (isExtractHiElt(Lo, Lo)) 1925 Mods |= SISrcMods::OP_SEL_0; 1926 1927 if (isExtractHiElt(Hi, Hi)) 1928 Mods |= SISrcMods::OP_SEL_1; 1929 1930 Lo = stripExtractLoElt(Lo); 1931 Hi = stripExtractLoElt(Hi); 1932 1933 if (Lo == Hi && !isInlineImmediate(Lo.getNode())) { 1934 // Really a scalar input. Just select from the low half of the register to 1935 // avoid packing. 1936 1937 Src = Lo; 1938 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 1939 return true; 1940 } 1941 1942 Mods = VecMods; 1943 } 1944 1945 // Packed instructions do not have abs modifiers. 1946 Mods |= SISrcMods::OP_SEL_1; 1947 1948 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 1949 return true; 1950 } 1951 1952 bool AMDGPUDAGToDAGISel::SelectVOP3PMods0(SDValue In, SDValue &Src, 1953 SDValue &SrcMods, 1954 SDValue &Clamp) const { 1955 SDLoc SL(In); 1956 1957 // FIXME: Handle clamp and op_sel 1958 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32); 1959 1960 return SelectVOP3PMods(In, Src, SrcMods); 1961 } 1962 1963 bool AMDGPUDAGToDAGISel::SelectVOP3OpSel(SDValue In, SDValue &Src, 1964 SDValue &SrcMods) const { 1965 Src = In; 1966 // FIXME: Handle op_sel 1967 SrcMods = CurDAG->getTargetConstant(0, SDLoc(In), MVT::i32); 1968 return true; 1969 } 1970 1971 bool AMDGPUDAGToDAGISel::SelectVOP3OpSel0(SDValue In, SDValue &Src, 1972 SDValue &SrcMods, 1973 SDValue &Clamp) const { 1974 SDLoc SL(In); 1975 1976 // FIXME: Handle clamp 1977 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32); 1978 1979 return SelectVOP3OpSel(In, Src, SrcMods); 1980 } 1981 1982 bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods(SDValue In, SDValue &Src, 1983 SDValue &SrcMods) const { 1984 // FIXME: Handle op_sel 1985 return SelectVOP3Mods(In, Src, SrcMods); 1986 } 1987 1988 bool AMDGPUDAGToDAGISel::SelectVOP3OpSelMods0(SDValue In, SDValue &Src, 1989 SDValue &SrcMods, 1990 SDValue &Clamp) const { 1991 SDLoc SL(In); 1992 1993 // FIXME: Handle clamp 1994 Clamp = CurDAG->getTargetConstant(0, SL, MVT::i32); 1995 1996 return SelectVOP3OpSelMods(In, Src, SrcMods); 1997 } 1998 1999 // The return value is not whether the match is possible (which it always is), 2000 // but whether or not it a conversion is really used. 2001 bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixModsImpl(SDValue In, SDValue &Src, 2002 unsigned &Mods) const { 2003 Mods = 0; 2004 SelectVOP3ModsImpl(In, Src, Mods); 2005 2006 if (Src.getOpcode() == ISD::FP_EXTEND) { 2007 Src = Src.getOperand(0); 2008 assert(Src.getValueType() == MVT::f16); 2009 Src = stripBitcast(Src); 2010 2011 // Be careful about folding modifiers if we already have an abs. fneg is 2012 // applied last, so we don't want to apply an earlier fneg. 2013 if ((Mods & SISrcMods::ABS) == 0) { 2014 unsigned ModsTmp; 2015 SelectVOP3ModsImpl(Src, Src, ModsTmp); 2016 2017 if ((ModsTmp & SISrcMods::NEG) != 0) 2018 Mods ^= SISrcMods::NEG; 2019 2020 if ((ModsTmp & SISrcMods::ABS) != 0) 2021 Mods |= SISrcMods::ABS; 2022 } 2023 2024 // op_sel/op_sel_hi decide the source type and source. 2025 // If the source's op_sel_hi is set, it indicates to do a conversion from fp16. 2026 // If the sources's op_sel is set, it picks the high half of the source 2027 // register. 2028 2029 Mods |= SISrcMods::OP_SEL_1; 2030 if (isExtractHiElt(Src, Src)) { 2031 Mods |= SISrcMods::OP_SEL_0; 2032 2033 // TODO: Should we try to look for neg/abs here? 2034 } 2035 2036 return true; 2037 } 2038 2039 return false; 2040 } 2041 2042 bool AMDGPUDAGToDAGISel::SelectVOP3PMadMixMods(SDValue In, SDValue &Src, 2043 SDValue &SrcMods) const { 2044 unsigned Mods = 0; 2045 SelectVOP3PMadMixModsImpl(In, Src, Mods); 2046 SrcMods = CurDAG->getTargetConstant(Mods, SDLoc(In), MVT::i32); 2047 return true; 2048 } 2049 2050 // TODO: Can we identify things like v_mad_mixhi_f16? 2051 bool AMDGPUDAGToDAGISel::SelectHi16Elt(SDValue In, SDValue &Src) const { 2052 if (In.isUndef()) { 2053 Src = In; 2054 return true; 2055 } 2056 2057 if (ConstantSDNode *C = dyn_cast<ConstantSDNode>(In)) { 2058 SDLoc SL(In); 2059 SDValue K = CurDAG->getTargetConstant(C->getZExtValue() << 16, SL, MVT::i32); 2060 MachineSDNode *MovK = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 2061 SL, MVT::i32, K); 2062 Src = SDValue(MovK, 0); 2063 return true; 2064 } 2065 2066 if (ConstantFPSDNode *C = dyn_cast<ConstantFPSDNode>(In)) { 2067 SDLoc SL(In); 2068 SDValue K = CurDAG->getTargetConstant( 2069 C->getValueAPF().bitcastToAPInt().getZExtValue() << 16, SL, MVT::i32); 2070 MachineSDNode *MovK = CurDAG->getMachineNode(AMDGPU::V_MOV_B32_e32, 2071 SL, MVT::i32, K); 2072 Src = SDValue(MovK, 0); 2073 return true; 2074 } 2075 2076 return isExtractHiElt(In, Src); 2077 } 2078 2079 void AMDGPUDAGToDAGISel::PostprocessISelDAG() { 2080 const AMDGPUTargetLowering& Lowering = 2081 *static_cast<const AMDGPUTargetLowering*>(getTargetLowering()); 2082 bool IsModified = false; 2083 do { 2084 IsModified = false; 2085 // Go over all selected nodes and try to fold them a bit more 2086 for (SDNode &Node : CurDAG->allnodes()) { 2087 MachineSDNode *MachineNode = dyn_cast<MachineSDNode>(&Node); 2088 if (!MachineNode) 2089 continue; 2090 2091 SDNode *ResNode = Lowering.PostISelFolding(MachineNode, *CurDAG); 2092 if (ResNode != &Node) { 2093 ReplaceUses(&Node, ResNode); 2094 IsModified = true; 2095 } 2096 } 2097 CurDAG->RemoveDeadNodes(); 2098 } while (IsModified); 2099 } 2100 2101 void R600DAGToDAGISel::Select(SDNode *N) { 2102 unsigned int Opc = N->getOpcode(); 2103 if (N->isMachineOpcode()) { 2104 N->setNodeId(-1); 2105 return; // Already selected. 2106 } 2107 2108 switch (Opc) { 2109 default: break; 2110 case AMDGPUISD::BUILD_VERTICAL_VECTOR: 2111 case ISD::SCALAR_TO_VECTOR: 2112 case ISD::BUILD_VECTOR: { 2113 EVT VT = N->getValueType(0); 2114 unsigned NumVectorElts = VT.getVectorNumElements(); 2115 unsigned RegClassID; 2116 // BUILD_VECTOR was lowered into an IMPLICIT_DEF + 4 INSERT_SUBREG 2117 // that adds a 128 bits reg copy when going through TwoAddressInstructions 2118 // pass. We want to avoid 128 bits copies as much as possible because they 2119 // can't be bundled by our scheduler. 2120 switch(NumVectorElts) { 2121 case 2: RegClassID = AMDGPU::R600_Reg64RegClassID; break; 2122 case 4: 2123 if (Opc == AMDGPUISD::BUILD_VERTICAL_VECTOR) 2124 RegClassID = AMDGPU::R600_Reg128VerticalRegClassID; 2125 else 2126 RegClassID = AMDGPU::R600_Reg128RegClassID; 2127 break; 2128 default: llvm_unreachable("Do not know how to lower this BUILD_VECTOR"); 2129 } 2130 SelectBuildVector(N, RegClassID); 2131 return; 2132 } 2133 } 2134 2135 SelectCode(N); 2136 } 2137 2138 bool R600DAGToDAGISel::SelectADDRIndirect(SDValue Addr, SDValue &Base, 2139 SDValue &Offset) { 2140 ConstantSDNode *C; 2141 SDLoc DL(Addr); 2142 2143 if ((C = dyn_cast<ConstantSDNode>(Addr))) { 2144 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); 2145 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 2146 } else if ((Addr.getOpcode() == AMDGPUISD::DWORDADDR) && 2147 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(0)))) { 2148 Base = CurDAG->getRegister(AMDGPU::INDIRECT_BASE_ADDR, MVT::i32); 2149 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 2150 } else if ((Addr.getOpcode() == ISD::ADD || Addr.getOpcode() == ISD::OR) && 2151 (C = dyn_cast<ConstantSDNode>(Addr.getOperand(1)))) { 2152 Base = Addr.getOperand(0); 2153 Offset = CurDAG->getTargetConstant(C->getZExtValue(), DL, MVT::i32); 2154 } else { 2155 Base = Addr; 2156 Offset = CurDAG->getTargetConstant(0, DL, MVT::i32); 2157 } 2158 2159 return true; 2160 } 2161 2162 bool R600DAGToDAGISel::SelectADDRVTX_READ(SDValue Addr, SDValue &Base, 2163 SDValue &Offset) { 2164 ConstantSDNode *IMMOffset; 2165 2166 if (Addr.getOpcode() == ISD::ADD 2167 && (IMMOffset = dyn_cast<ConstantSDNode>(Addr.getOperand(1))) 2168 && isInt<16>(IMMOffset->getZExtValue())) { 2169 2170 Base = Addr.getOperand(0); 2171 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), 2172 MVT::i32); 2173 return true; 2174 // If the pointer address is constant, we can move it to the offset field. 2175 } else if ((IMMOffset = dyn_cast<ConstantSDNode>(Addr)) 2176 && isInt<16>(IMMOffset->getZExtValue())) { 2177 Base = CurDAG->getCopyFromReg(CurDAG->getEntryNode(), 2178 SDLoc(CurDAG->getEntryNode()), 2179 AMDGPU::ZERO, MVT::i32); 2180 Offset = CurDAG->getTargetConstant(IMMOffset->getZExtValue(), SDLoc(Addr), 2181 MVT::i32); 2182 return true; 2183 } 2184 2185 // Default case, no offset 2186 Base = Addr; 2187 Offset = CurDAG->getTargetConstant(0, SDLoc(Addr), MVT::i32); 2188 return true; 2189 } 2190