1 //===-- SIISelLowering.h - SI DAG Lowering Interface ------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 /// \file 10 /// SI DAG Lowering interface definition 11 // 12 //===----------------------------------------------------------------------===// 13 14 #ifndef LLVM_LIB_TARGET_AMDGPU_SIISELLOWERING_H 15 #define LLVM_LIB_TARGET_AMDGPU_SIISELLOWERING_H 16 17 #include "AMDGPUISelLowering.h" 18 #include "AMDGPUArgumentUsageInfo.h" 19 #include "SIInstrInfo.h" 20 21 namespace llvm { 22 23 class SITargetLowering final : public AMDGPUTargetLowering { 24 private: 25 const GCNSubtarget *Subtarget; 26 27 public: 28 MVT getRegisterTypeForCallingConv(LLVMContext &Context, 29 CallingConv::ID CC, 30 EVT VT) const override; 31 unsigned getNumRegistersForCallingConv(LLVMContext &Context, 32 CallingConv::ID CC, 33 EVT VT) const override; 34 35 unsigned getVectorTypeBreakdownForCallingConv( 36 LLVMContext &Context, CallingConv::ID CC, EVT VT, EVT &IntermediateVT, 37 unsigned &NumIntermediates, MVT &RegisterVT) const override; 38 39 private: 40 SDValue lowerKernArgParameterPtr(SelectionDAG &DAG, const SDLoc &SL, 41 SDValue Chain, uint64_t Offset) const; 42 SDValue getImplicitArgPtr(SelectionDAG &DAG, const SDLoc &SL) const; 43 SDValue lowerKernargMemParameter(SelectionDAG &DAG, EVT VT, EVT MemVT, 44 const SDLoc &SL, SDValue Chain, 45 uint64_t Offset, unsigned Align, bool Signed, 46 const ISD::InputArg *Arg = nullptr) const; 47 48 SDValue lowerStackParameter(SelectionDAG &DAG, CCValAssign &VA, 49 const SDLoc &SL, SDValue Chain, 50 const ISD::InputArg &Arg) const; 51 SDValue getPreloadedValue(SelectionDAG &DAG, 52 const SIMachineFunctionInfo &MFI, 53 EVT VT, 54 AMDGPUFunctionArgInfo::PreloadedValue) const; 55 56 SDValue LowerGlobalAddress(AMDGPUMachineFunction *MFI, SDValue Op, 57 SelectionDAG &DAG) const override; 58 SDValue lowerImplicitZextParam(SelectionDAG &DAG, SDValue Op, 59 MVT VT, unsigned Offset) const; 60 SDValue lowerImage(SDValue Op, const AMDGPU::ImageDimIntrinsicInfo *Intr, 61 SelectionDAG &DAG) const; 62 SDValue lowerSBuffer(EVT VT, SDLoc DL, SDValue Rsrc, SDValue Offset, 63 SDValue CachePolicy, SelectionDAG &DAG) const; 64 65 SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const; 66 SDValue LowerINTRINSIC_W_CHAIN(SDValue Op, SelectionDAG &DAG) const; 67 SDValue LowerINTRINSIC_VOID(SDValue Op, SelectionDAG &DAG) const; 68 69 // The raw.tbuffer and struct.tbuffer intrinsics have two offset args: offset 70 // (the offset that is included in bounds checking and swizzling, to be split 71 // between the instruction's voffset and immoffset fields) and soffset (the 72 // offset that is excluded from bounds checking and swizzling, to go in the 73 // instruction's soffset field). This function takes the first kind of 74 // offset and figures out how to split it between voffset and immoffset. 75 std::pair<SDValue, SDValue> splitBufferOffsets(SDValue Offset, 76 SelectionDAG &DAG) const; 77 78 SDValue widenLoad(LoadSDNode *Ld, DAGCombinerInfo &DCI) const; 79 SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG) const; 80 SDValue LowerSELECT(SDValue Op, SelectionDAG &DAG) const; 81 SDValue lowerFastUnsafeFDIV(SDValue Op, SelectionDAG &DAG) const; 82 SDValue lowerFDIV_FAST(SDValue Op, SelectionDAG &DAG) const; 83 SDValue LowerFDIV16(SDValue Op, SelectionDAG &DAG) const; 84 SDValue LowerFDIV32(SDValue Op, SelectionDAG &DAG) const; 85 SDValue LowerFDIV64(SDValue Op, SelectionDAG &DAG) const; 86 SDValue LowerFDIV(SDValue Op, SelectionDAG &DAG) const; 87 SDValue LowerINT_TO_FP(SDValue Op, SelectionDAG &DAG, bool Signed) const; 88 SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const; 89 SDValue LowerTrig(SDValue Op, SelectionDAG &DAG) const; 90 SDValue LowerATOMIC_CMP_SWAP(SDValue Op, SelectionDAG &DAG) const; 91 SDValue LowerBRCOND(SDValue Op, SelectionDAG &DAG) const; 92 SDValue LowerRETURNADDR(SDValue Op, SelectionDAG &DAG) const; 93 SDValue adjustLoadValueType(unsigned Opcode, MemSDNode *M, 94 SelectionDAG &DAG, ArrayRef<SDValue> Ops, 95 bool IsIntrinsic = false) const; 96 97 SDValue lowerIntrinsicLoad(MemSDNode *M, bool IsFormat, SelectionDAG &DAG, 98 ArrayRef<SDValue> Ops) const; 99 100 // Call DAG.getMemIntrinsicNode for a load, but first widen a dwordx3 type to 101 // dwordx4 if on SI. 102 SDValue getMemIntrinsicNode(unsigned Opcode, const SDLoc &DL, SDVTList VTList, 103 ArrayRef<SDValue> Ops, EVT MemVT, 104 MachineMemOperand *MMO, SelectionDAG &DAG) const; 105 106 SDValue handleD16VData(SDValue VData, SelectionDAG &DAG) const; 107 108 /// Converts \p Op, which must be of floating point type, to the 109 /// floating point type \p VT, by either extending or truncating it. 110 SDValue getFPExtOrFPRound(SelectionDAG &DAG, 111 SDValue Op, 112 const SDLoc &DL, 113 EVT VT) const; 114 115 SDValue convertArgType( 116 SelectionDAG &DAG, EVT VT, EVT MemVT, const SDLoc &SL, SDValue Val, 117 bool Signed, const ISD::InputArg *Arg = nullptr) const; 118 119 /// Custom lowering for ISD::FP_ROUND for MVT::f16. 120 SDValue lowerFP_ROUND(SDValue Op, SelectionDAG &DAG) const; 121 SDValue lowerFMINNUM_FMAXNUM(SDValue Op, SelectionDAG &DAG) const; 122 123 SDValue getSegmentAperture(unsigned AS, const SDLoc &DL, 124 SelectionDAG &DAG) const; 125 126 SDValue lowerADDRSPACECAST(SDValue Op, SelectionDAG &DAG) const; 127 SDValue lowerINSERT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const; 128 SDValue lowerINSERT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const; 129 SDValue lowerEXTRACT_VECTOR_ELT(SDValue Op, SelectionDAG &DAG) const; 130 SDValue lowerVECTOR_SHUFFLE(SDValue Op, SelectionDAG &DAG) const; 131 SDValue lowerBUILD_VECTOR(SDValue Op, SelectionDAG &DAG) const; 132 SDValue lowerTRAP(SDValue Op, SelectionDAG &DAG) const; 133 SDValue lowerDEBUGTRAP(SDValue Op, SelectionDAG &DAG) const; 134 135 SDNode *adjustWritemask(MachineSDNode *&N, SelectionDAG &DAG) const; 136 137 SDValue performUCharToFloatCombine(SDNode *N, 138 DAGCombinerInfo &DCI) const; 139 SDValue performSHLPtrCombine(SDNode *N, 140 unsigned AS, 141 EVT MemVT, 142 DAGCombinerInfo &DCI) const; 143 144 SDValue performMemSDNodeCombine(MemSDNode *N, DAGCombinerInfo &DCI) const; 145 146 SDValue splitBinaryBitConstantOp(DAGCombinerInfo &DCI, const SDLoc &SL, 147 unsigned Opc, SDValue LHS, 148 const ConstantSDNode *CRHS) const; 149 150 SDValue performAndCombine(SDNode *N, DAGCombinerInfo &DCI) const; 151 SDValue performOrCombine(SDNode *N, DAGCombinerInfo &DCI) const; 152 SDValue performXorCombine(SDNode *N, DAGCombinerInfo &DCI) const; 153 SDValue performZeroExtendCombine(SDNode *N, DAGCombinerInfo &DCI) const; 154 SDValue performSignExtendInRegCombine(SDNode *N, DAGCombinerInfo &DCI) const; 155 SDValue performClassCombine(SDNode *N, DAGCombinerInfo &DCI) const; 156 SDValue getCanonicalConstantFP(SelectionDAG &DAG, const SDLoc &SL, EVT VT, 157 const APFloat &C) const; 158 SDValue performFCanonicalizeCombine(SDNode *N, DAGCombinerInfo &DCI) const; 159 160 SDValue performFPMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL, 161 SDValue Op0, SDValue Op1) const; 162 SDValue performIntMed3ImmCombine(SelectionDAG &DAG, const SDLoc &SL, 163 SDValue Op0, SDValue Op1, bool Signed) const; 164 SDValue performMinMaxCombine(SDNode *N, DAGCombinerInfo &DCI) const; 165 SDValue performFMed3Combine(SDNode *N, DAGCombinerInfo &DCI) const; 166 SDValue performCvtPkRTZCombine(SDNode *N, DAGCombinerInfo &DCI) const; 167 SDValue performExtractVectorEltCombine(SDNode *N, DAGCombinerInfo &DCI) const; 168 SDValue performInsertVectorEltCombine(SDNode *N, DAGCombinerInfo &DCI) const; 169 170 SDValue reassociateScalarOps(SDNode *N, SelectionDAG &DAG) const; 171 unsigned getFusedOpcode(const SelectionDAG &DAG, 172 const SDNode *N0, const SDNode *N1) const; 173 SDValue performAddCombine(SDNode *N, DAGCombinerInfo &DCI) const; 174 SDValue performAddCarrySubCarryCombine(SDNode *N, DAGCombinerInfo &DCI) const; 175 SDValue performSubCombine(SDNode *N, DAGCombinerInfo &DCI) const; 176 SDValue performFAddCombine(SDNode *N, DAGCombinerInfo &DCI) const; 177 SDValue performFSubCombine(SDNode *N, DAGCombinerInfo &DCI) const; 178 SDValue performFMACombine(SDNode *N, DAGCombinerInfo &DCI) const; 179 SDValue performSetCCCombine(SDNode *N, DAGCombinerInfo &DCI) const; 180 SDValue performCvtF32UByteNCombine(SDNode *N, DAGCombinerInfo &DCI) const; 181 SDValue performClampCombine(SDNode *N, DAGCombinerInfo &DCI) const; 182 SDValue performRcpCombine(SDNode *N, DAGCombinerInfo &DCI) const; 183 184 bool isLegalFlatAddressingMode(const AddrMode &AM) const; 185 bool isLegalMUBUFAddressingMode(const AddrMode &AM) const; 186 187 unsigned isCFIntrinsic(const SDNode *Intr) const; 188 189 public: 190 /// \returns True if fixup needs to be emitted for given global value \p GV, 191 /// false otherwise. 192 bool shouldEmitFixup(const GlobalValue *GV) const; 193 194 /// \returns True if GOT relocation needs to be emitted for given global value 195 /// \p GV, false otherwise. 196 bool shouldEmitGOTReloc(const GlobalValue *GV) const; 197 198 /// \returns True if PC-relative relocation needs to be emitted for given 199 /// global value \p GV, false otherwise. 200 bool shouldEmitPCReloc(const GlobalValue *GV) const; 201 202 /// \returns true if this should use a literal constant for an LDS address, 203 /// and not emit a relocation for an LDS global. 204 bool shouldUseLDSConstAddress(const GlobalValue *GV) const; 205 206 private: 207 // Analyze a combined offset from an amdgcn_buffer_ intrinsic and store the 208 // three offsets (voffset, soffset and instoffset) into the SDValue[3] array 209 // pointed to by Offsets. 210 /// \returns 0 If there is a non-constant offset or if the offset is 0. 211 /// Otherwise returns the constant offset. 212 unsigned setBufferOffsets(SDValue CombinedOffset, SelectionDAG &DAG, 213 SDValue *Offsets, unsigned Align = 4) const; 214 215 // Handle 8 bit and 16 bit buffer loads 216 SDValue handleByteShortBufferLoads(SelectionDAG &DAG, EVT LoadVT, SDLoc DL, 217 ArrayRef<SDValue> Ops, MemSDNode *M) const; 218 219 // Handle 8 bit and 16 bit buffer stores 220 SDValue handleByteShortBufferStores(SelectionDAG &DAG, EVT VDataType, 221 SDLoc DL, SDValue Ops[], 222 MemSDNode *M) const; 223 224 public: 225 SITargetLowering(const TargetMachine &tm, const GCNSubtarget &STI); 226 227 const GCNSubtarget *getSubtarget() const; 228 229 bool isFPExtFoldable(const SelectionDAG &DAG, unsigned Opcode, EVT DestVT, 230 EVT SrcVT) const override; 231 232 bool isShuffleMaskLegal(ArrayRef<int> /*Mask*/, EVT /*VT*/) const override; 233 234 bool getTgtMemIntrinsic(IntrinsicInfo &, const CallInst &, 235 MachineFunction &MF, 236 unsigned IntrinsicID) const override; 237 238 bool getAddrModeArguments(IntrinsicInst * /*I*/, 239 SmallVectorImpl<Value*> &/*Ops*/, 240 Type *&/*AccessTy*/) const override; 241 242 bool isLegalGlobalAddressingMode(const AddrMode &AM) const; 243 bool isLegalAddressingMode(const DataLayout &DL, const AddrMode &AM, Type *Ty, 244 unsigned AS, 245 Instruction *I = nullptr) const override; 246 247 bool canMergeStoresTo(unsigned AS, EVT MemVT, 248 const SelectionDAG &DAG) const override; 249 250 bool allowsMisalignedMemoryAccessesImpl( 251 unsigned Size, unsigned AS, unsigned Align, 252 MachineMemOperand::Flags Flags = MachineMemOperand::MONone, 253 bool *IsFast = nullptr) const; 254 255 bool allowsMisalignedMemoryAccesses( 256 EVT VT, unsigned AS, unsigned Align, 257 MachineMemOperand::Flags Flags = MachineMemOperand::MONone, 258 bool *IsFast = nullptr) const override; 259 260 EVT getOptimalMemOpType(const MemOp &Op, 261 const AttributeList &FuncAttributes) const override; 262 263 bool isMemOpUniform(const SDNode *N) const; 264 bool isMemOpHasNoClobberedMemOperand(const SDNode *N) const; 265 266 static bool isNonGlobalAddrSpace(unsigned AS) { 267 return AS == AMDGPUAS::LOCAL_ADDRESS || AS == AMDGPUAS::REGION_ADDRESS || 268 AS == AMDGPUAS::PRIVATE_ADDRESS; 269 } 270 271 // FIXME: Missing constant_32bit 272 static bool isFlatGlobalAddrSpace(unsigned AS) { 273 return AS == AMDGPUAS::GLOBAL_ADDRESS || 274 AS == AMDGPUAS::FLAT_ADDRESS || 275 AS == AMDGPUAS::CONSTANT_ADDRESS || 276 AS > AMDGPUAS::MAX_AMDGPU_ADDRESS; 277 } 278 279 bool isNoopAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override; 280 bool isFreeAddrSpaceCast(unsigned SrcAS, unsigned DestAS) const override; 281 282 TargetLoweringBase::LegalizeTypeAction 283 getPreferredVectorAction(MVT VT) const override; 284 285 bool shouldConvertConstantLoadToIntImm(const APInt &Imm, 286 Type *Ty) const override; 287 288 bool isTypeDesirableForOp(unsigned Op, EVT VT) const override; 289 290 bool isOffsetFoldingLegal(const GlobalAddressSDNode *GA) const override; 291 292 bool supportSplitCSR(MachineFunction *MF) const override; 293 void initializeSplitCSR(MachineBasicBlock *Entry) const override; 294 void insertCopiesSplitCSR( 295 MachineBasicBlock *Entry, 296 const SmallVectorImpl<MachineBasicBlock *> &Exits) const override; 297 298 SDValue LowerFormalArguments(SDValue Chain, CallingConv::ID CallConv, 299 bool isVarArg, 300 const SmallVectorImpl<ISD::InputArg> &Ins, 301 const SDLoc &DL, SelectionDAG &DAG, 302 SmallVectorImpl<SDValue> &InVals) const override; 303 304 bool CanLowerReturn(CallingConv::ID CallConv, 305 MachineFunction &MF, bool isVarArg, 306 const SmallVectorImpl<ISD::OutputArg> &Outs, 307 LLVMContext &Context) const override; 308 309 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool IsVarArg, 310 const SmallVectorImpl<ISD::OutputArg> &Outs, 311 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL, 312 SelectionDAG &DAG) const override; 313 314 void passSpecialInputs( 315 CallLoweringInfo &CLI, 316 CCState &CCInfo, 317 const SIMachineFunctionInfo &Info, 318 SmallVectorImpl<std::pair<unsigned, SDValue>> &RegsToPass, 319 SmallVectorImpl<SDValue> &MemOpChains, 320 SDValue Chain) const; 321 322 SDValue LowerCallResult(SDValue Chain, SDValue InFlag, 323 CallingConv::ID CallConv, bool isVarArg, 324 const SmallVectorImpl<ISD::InputArg> &Ins, 325 const SDLoc &DL, SelectionDAG &DAG, 326 SmallVectorImpl<SDValue> &InVals, bool isThisReturn, 327 SDValue ThisVal) const; 328 329 bool mayBeEmittedAsTailCall(const CallInst *) const override; 330 331 bool isEligibleForTailCallOptimization( 332 SDValue Callee, CallingConv::ID CalleeCC, bool isVarArg, 333 const SmallVectorImpl<ISD::OutputArg> &Outs, 334 const SmallVectorImpl<SDValue> &OutVals, 335 const SmallVectorImpl<ISD::InputArg> &Ins, SelectionDAG &DAG) const; 336 337 SDValue LowerCall(CallLoweringInfo &CLI, 338 SmallVectorImpl<SDValue> &InVals) const override; 339 340 Register getRegisterByName(const char* RegName, LLT VT, 341 const MachineFunction &MF) const override; 342 343 MachineBasicBlock *splitKillBlock(MachineInstr &MI, 344 MachineBasicBlock *BB) const; 345 346 void bundleInstWithWaitcnt(MachineInstr &MI) const; 347 MachineBasicBlock *emitGWSMemViolTestLoop(MachineInstr &MI, 348 MachineBasicBlock *BB) const; 349 350 MachineBasicBlock * 351 EmitInstrWithCustomInserter(MachineInstr &MI, 352 MachineBasicBlock *BB) const override; 353 354 bool hasBitPreservingFPLogic(EVT VT) const override; 355 bool enableAggressiveFMAFusion(EVT VT) const override; 356 EVT getSetCCResultType(const DataLayout &DL, LLVMContext &Context, 357 EVT VT) const override; 358 MVT getScalarShiftAmountTy(const DataLayout &, EVT) const override; 359 bool isFMAFasterThanFMulAndFAdd(const MachineFunction &MF, 360 EVT VT) const override; 361 bool isFMADLegal(const SelectionDAG &DAG, const SDNode *N) const override; 362 363 SDValue splitUnaryVectorOp(SDValue Op, SelectionDAG &DAG) const; 364 SDValue splitBinaryVectorOp(SDValue Op, SelectionDAG &DAG) const; 365 SDValue splitTernaryVectorOp(SDValue Op, SelectionDAG &DAG) const; 366 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override; 367 368 void ReplaceNodeResults(SDNode *N, SmallVectorImpl<SDValue> &Results, 369 SelectionDAG &DAG) const override; 370 371 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override; 372 SDNode *PostISelFolding(MachineSDNode *N, SelectionDAG &DAG) const override; 373 void AdjustInstrPostInstrSelection(MachineInstr &MI, 374 SDNode *Node) const override; 375 376 SDNode *legalizeTargetIndependentNode(SDNode *Node, SelectionDAG &DAG) const; 377 378 MachineSDNode *wrapAddr64Rsrc(SelectionDAG &DAG, const SDLoc &DL, 379 SDValue Ptr) const; 380 MachineSDNode *buildRSRC(SelectionDAG &DAG, const SDLoc &DL, SDValue Ptr, 381 uint32_t RsrcDword1, uint64_t RsrcDword2And3) const; 382 std::pair<unsigned, const TargetRegisterClass *> 383 getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, 384 StringRef Constraint, MVT VT) const override; 385 ConstraintType getConstraintType(StringRef Constraint) const override; 386 SDValue copyToM0(SelectionDAG &DAG, SDValue Chain, const SDLoc &DL, 387 SDValue V) const; 388 389 void finalizeLowering(MachineFunction &MF) const override; 390 391 void computeKnownBitsForFrameIndex(const SDValue Op, 392 KnownBits &Known, 393 const APInt &DemandedElts, 394 const SelectionDAG &DAG, 395 unsigned Depth = 0) const override; 396 397 bool isSDNodeSourceOfDivergence(const SDNode *N, 398 FunctionLoweringInfo *FLI, LegacyDivergenceAnalysis *DA) const override; 399 400 bool isCanonicalized(SelectionDAG &DAG, SDValue Op, 401 unsigned MaxDepth = 5) const; 402 bool denormalsEnabledForType(const SelectionDAG &DAG, EVT VT) const; 403 404 bool isKnownNeverNaNForTargetNode(SDValue Op, 405 const SelectionDAG &DAG, 406 bool SNaN = false, 407 unsigned Depth = 0) const override; 408 AtomicExpansionKind shouldExpandAtomicRMWInIR(AtomicRMWInst *) const override; 409 410 virtual const TargetRegisterClass * 411 getRegClassFor(MVT VT, bool isDivergent) const override; 412 virtual bool requiresUniformRegister(MachineFunction &MF, 413 const Value *V) const override; 414 Align getPrefLoopAlignment(MachineLoop *ML) const override; 415 416 void allocateHSAUserSGPRs(CCState &CCInfo, 417 MachineFunction &MF, 418 const SIRegisterInfo &TRI, 419 SIMachineFunctionInfo &Info) const; 420 421 void allocateSystemSGPRs(CCState &CCInfo, 422 MachineFunction &MF, 423 SIMachineFunctionInfo &Info, 424 CallingConv::ID CallConv, 425 bool IsShader) const; 426 427 void allocateSpecialEntryInputVGPRs(CCState &CCInfo, 428 MachineFunction &MF, 429 const SIRegisterInfo &TRI, 430 SIMachineFunctionInfo &Info) const; 431 void allocateSpecialInputSGPRs( 432 CCState &CCInfo, 433 MachineFunction &MF, 434 const SIRegisterInfo &TRI, 435 SIMachineFunctionInfo &Info) const; 436 437 void allocateSpecialInputVGPRs(CCState &CCInfo, 438 MachineFunction &MF, 439 const SIRegisterInfo &TRI, 440 SIMachineFunctionInfo &Info) const; 441 void allocateSpecialInputVGPRsFixed(CCState &CCInfo, 442 MachineFunction &MF, 443 const SIRegisterInfo &TRI, 444 SIMachineFunctionInfo &Info) const; 445 }; 446 447 } // End namespace llvm 448 449 #endif 450