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