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