1 //===-- AMDGPUISelLowering.h - AMDGPU Lowering Interface --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
10 /// Interface definition of the TargetLowering class that is common
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H
16 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H
19 #include "llvm/CodeGen/CallingConvLower.h"
20 #include "llvm/CodeGen/TargetLowering.h"
21 #include "llvm/Target/TargetMachine.h"
25 class AMDGPUMachineFunction;
26 class AMDGPUSubtarget;
29 class AMDGPUTargetLowering : public TargetLowering {
31 const AMDGPUSubtarget *Subtarget;
33 /// \returns AMDGPUISD::FFBH_U32 node if the incoming \p Op may have been
34 /// legalized from a smaller type VT. Need to match pre-legalized type because
35 /// the generic legalization inserts the add/sub between the select and
37 SDValue getFFBX_U32(SelectionDAG &DAG, SDValue Op, const SDLoc &DL, unsigned Opc) const;
40 static unsigned numBitsUnsigned(SDValue Op, SelectionDAG &DAG);
41 static unsigned numBitsSigned(SDValue Op, SelectionDAG &DAG);
42 static bool hasDefinedInitializer(const GlobalValue *GV);
45 SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const;
46 SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const;
47 /// Split a vector store into multiple scalar stores.
48 /// \returns The resulting chain.
50 SDValue LowerFREM(SDValue Op, SelectionDAG &DAG) const;
51 SDValue LowerFCEIL(SDValue Op, SelectionDAG &DAG) const;
52 SDValue LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const;
53 SDValue LowerFRINT(SDValue Op, SelectionDAG &DAG) const;
54 SDValue LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const;
56 SDValue LowerFROUND(SDValue Op, SelectionDAG &DAG) const;
57 SDValue LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const;
58 SDValue LowerFLOG(SDValue Op, SelectionDAG &DAG,
59 double Log2BaseInverted) const;
60 SDValue lowerFEXP(SDValue Op, SelectionDAG &DAG) const;
62 SDValue LowerCTLZ_CTTZ(SDValue Op, SelectionDAG &DAG) const;
64 SDValue LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG, bool Signed) const;
65 SDValue LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG, bool Signed) const;
66 SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
67 SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
69 SDValue LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG, bool Signed) const;
70 SDValue LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const;
71 SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const;
72 SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const;
74 SDValue LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const;
77 bool shouldCombineMemoryType(EVT VT) const;
78 SDValue performLoadCombine(SDNode *N, DAGCombinerInfo &DCI) const;
79 SDValue performStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const;
80 SDValue performAssertSZExtCombine(SDNode *N, DAGCombinerInfo &DCI) const;
81 SDValue performIntrinsicWOChainCombine(SDNode *N, DAGCombinerInfo &DCI) const;
83 SDValue splitBinaryBitConstantOpImpl(DAGCombinerInfo &DCI, const SDLoc &SL,
84 unsigned Opc, SDValue LHS,
85 uint32_t ValLo, uint32_t ValHi) const;
86 SDValue performShlCombine(SDNode *N, DAGCombinerInfo &DCI) const;
87 SDValue performSraCombine(SDNode *N, DAGCombinerInfo &DCI) const;
88 SDValue performSrlCombine(SDNode *N, DAGCombinerInfo &DCI) const;
89 SDValue performTruncateCombine(SDNode *N, DAGCombinerInfo &DCI) const;
90 SDValue performMulCombine(SDNode *N, DAGCombinerInfo &DCI) const;
91 SDValue performMulhsCombine(SDNode *N, DAGCombinerInfo &DCI) const;
92 SDValue performMulhuCombine(SDNode *N, DAGCombinerInfo &DCI) const;
93 SDValue performMulLoHi24Combine(SDNode *N, DAGCombinerInfo &DCI) const;
94 SDValue performCtlz_CttzCombine(const SDLoc &SL, SDValue Cond, SDValue LHS,
95 SDValue RHS, DAGCombinerInfo &DCI) const;
96 SDValue performSelectCombine(SDNode *N, DAGCombinerInfo &DCI) const;
98 bool isConstantCostlierToNegate(SDValue N) const;
99 SDValue performFNegCombine(SDNode *N, DAGCombinerInfo &DCI) const;
100 SDValue performFAbsCombine(SDNode *N, DAGCombinerInfo &DCI) const;
101 SDValue performRcpCombine(SDNode *N, DAGCombinerInfo &DCI) const;
103 static EVT getEquivalentMemType(LLVMContext &Context, EVT VT);
105 virtual SDValue LowerGlobalAddress(AMDGPUMachineFunction *MFI, SDValue Op,
106 SelectionDAG &DAG) const;
108 /// Return 64-bit value Op as two 32-bit integers.
109 std::pair<SDValue, SDValue> split64BitValue(SDValue Op,
110 SelectionDAG &DAG) const;
111 SDValue getLoHalf64(SDValue Op, SelectionDAG &DAG) const;
112 SDValue getHiHalf64(SDValue Op, SelectionDAG &DAG) const;
114 /// Split a vector type into two parts. The first part is a power of two
115 /// vector. The second part is whatever is left over, and is a scalar if it
116 /// would otherwise be a 1-vector.
117 std::pair<EVT, EVT> getSplitDestVTs(const EVT &VT, SelectionDAG &DAG) const;
119 /// Split a vector value into two parts of types LoVT and HiVT. HiVT could be
121 std::pair<SDValue, SDValue> splitVector(const SDValue &N, const SDLoc &DL,
122 const EVT &LoVT, const EVT &HighVT,
123 SelectionDAG &DAG) const;
125 /// Split a vector load into 2 loads of half the vector.
126 SDValue SplitVectorLoad(SDValue Op, SelectionDAG &DAG) const;
128 /// Widen a vector load from vec3 to vec4.
129 SDValue WidenVectorLoad(SDValue Op, SelectionDAG &DAG) const;
131 /// Split a vector store into 2 stores of half the vector.
132 SDValue SplitVectorStore(SDValue Op, SelectionDAG &DAG) const;
134 SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const;
135 SDValue LowerSDIVREM(SDValue Op, SelectionDAG &DAG) const;
136 SDValue LowerUDIVREM(SDValue Op, SelectionDAG &DAG) const;
137 SDValue LowerDIVREM24(SDValue Op, SelectionDAG &DAG, bool sign) const;
138 void LowerUDIVREM64(SDValue Op, SelectionDAG &DAG,
139 SmallVectorImpl<SDValue> &Results) const;
141 void analyzeFormalArgumentsCompute(
143 const SmallVectorImpl<ISD::InputArg> &Ins) const;
146 AMDGPUTargetLowering(const TargetMachine &TM, const AMDGPUSubtarget &STI);
148 bool mayIgnoreSignedZero(SDValue Op) const {
149 if (getTargetMachine().Options.NoSignedZerosFPMath)
152 const auto Flags = Op.getNode()->getFlags();
153 if (Flags.isDefined())
154 return Flags.hasNoSignedZeros();
159 static inline SDValue stripBitcast(SDValue Val) {
160 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val;
163 static bool allUsesHaveSourceMods(const SDNode *N,
164 unsigned CostThreshold = 4);
165 bool isFAbsFree(EVT VT) const override;
166 bool isFNegFree(EVT VT) const override;
167 bool isTruncateFree(EVT Src, EVT Dest) const override;
168 bool isTruncateFree(Type *Src, Type *Dest) const override;
170 bool isZExtFree(Type *Src, Type *Dest) const override;
171 bool isZExtFree(EVT Src, EVT Dest) const override;
172 bool isZExtFree(SDValue Val, EVT VT2) const override;
174 SDValue getNegatedExpression(SDValue Op, SelectionDAG &DAG,
175 bool LegalOperations, bool ForCodeSize,
177 unsigned Depth) const override;
179 bool isNarrowingProfitable(EVT VT1, EVT VT2) const override;
181 EVT getTypeForExtReturn(LLVMContext &Context, EVT VT,
182 ISD::NodeType ExtendKind) const override;
184 MVT getVectorIdxTy(const DataLayout &) const override;
185 bool isSelectSupported(SelectSupportKind) const override;
187 bool isFPImmLegal(const APFloat &Imm, EVT VT,
188 bool ForCodeSize) const override;
189 bool ShouldShrinkFPConstant(EVT VT) const override;
190 bool shouldReduceLoadWidth(SDNode *Load,
191 ISD::LoadExtType ExtType,
192 EVT ExtVT) const override;
194 bool isLoadBitCastBeneficial(EVT, EVT, const SelectionDAG &DAG,
195 const MachineMemOperand &MMO) const final;
197 bool storeOfVectorConstantIsCheap(EVT MemVT,
199 unsigned AS) const override;
200 bool aggressivelyPreferBuildVectorSources(EVT VecVT) const override;
201 bool isCheapToSpeculateCttz() const override;
202 bool isCheapToSpeculateCtlz() const override;
204 bool isSDNodeAlwaysUniform(const SDNode *N) const override;
205 static CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg);
206 static CCAssignFn *CCAssignFnForReturn(CallingConv::ID CC, bool IsVarArg);
208 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
209 const SmallVectorImpl<ISD::OutputArg> &Outs,
210 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
211 SelectionDAG &DAG) const override;
213 SDValue addTokenForArgument(SDValue Chain,
215 MachineFrameInfo &MFI,
216 int ClobberedFI) const;
218 SDValue lowerUnhandledCall(CallLoweringInfo &CLI,
219 SmallVectorImpl<SDValue> &InVals,
220 StringRef Reason) const;
221 SDValue LowerCall(CallLoweringInfo &CLI,
222 SmallVectorImpl<SDValue> &InVals) const override;
224 SDValue LowerDYNAMIC_STACKALLOC(SDValue Op,
225 SelectionDAG &DAG) const;
227 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
228 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
229 void ReplaceNodeResults(SDNode * N,
230 SmallVectorImpl<SDValue> &Results,
231 SelectionDAG &DAG) const override;
233 SDValue combineFMinMaxLegacy(const SDLoc &DL, EVT VT, SDValue LHS,
234 SDValue RHS, SDValue True, SDValue False,
235 SDValue CC, DAGCombinerInfo &DCI) const;
237 const char* getTargetNodeName(unsigned Opcode) const override;
239 // FIXME: Turn off MergeConsecutiveStores() before Instruction Selection for
240 // AMDGPU. Commit r319036,
241 // (https://github.com/llvm/llvm-project/commit/db77e57ea86d941a4262ef60261692f4cb6893e6)
242 // turned on MergeConsecutiveStores() before Instruction Selection for all
243 // targets. Enough AMDGPU compiles go into an infinite loop (
244 // MergeConsecutiveStores() merges two stores; LegalizeStoreOps() un-merges;
245 // MergeConsecutiveStores() re-merges, etc. ) to warrant turning it off for
247 bool mergeStoresAfterLegalization(EVT) const override { return false; }
249 bool isFsqrtCheap(SDValue Operand, SelectionDAG &DAG) const override {
252 SDValue getSqrtEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled,
253 int &RefinementSteps, bool &UseOneConstNR,
254 bool Reciprocal) const override;
255 SDValue getRecipEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled,
256 int &RefinementSteps) const override;
258 virtual SDNode *PostISelFolding(MachineSDNode *N,
259 SelectionDAG &DAG) const = 0;
261 /// Determine which of the bits specified in \p Mask are known to be
262 /// either zero or one and return them in the \p KnownZero and \p KnownOne
264 void computeKnownBitsForTargetNode(const SDValue Op,
266 const APInt &DemandedElts,
267 const SelectionDAG &DAG,
268 unsigned Depth = 0) const override;
270 unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts,
271 const SelectionDAG &DAG,
272 unsigned Depth = 0) const override;
274 unsigned computeNumSignBitsForTargetInstr(GISelKnownBits &Analysis,
276 const APInt &DemandedElts,
277 const MachineRegisterInfo &MRI,
278 unsigned Depth = 0) const override;
280 bool isKnownNeverNaNForTargetNode(SDValue Op,
281 const SelectionDAG &DAG,
283 unsigned Depth = 0) const override;
285 /// Helper function that adds Reg to the LiveIn list of the DAG's
288 /// \returns a RegisterSDNode representing Reg if \p RawReg is true, otherwise
289 /// a copy from the register.
290 SDValue CreateLiveInRegister(SelectionDAG &DAG,
291 const TargetRegisterClass *RC,
292 Register Reg, EVT VT,
294 bool RawReg = false) const;
295 SDValue CreateLiveInRegister(SelectionDAG &DAG,
296 const TargetRegisterClass *RC,
297 Register Reg, EVT VT) const {
298 return CreateLiveInRegister(DAG, RC, Reg, VT, SDLoc(DAG.getEntryNode()));
301 // Returns the raw live in register rather than a copy from it.
302 SDValue CreateLiveInRegisterRaw(SelectionDAG &DAG,
303 const TargetRegisterClass *RC,
304 Register Reg, EVT VT) const {
305 return CreateLiveInRegister(DAG, RC, Reg, VT, SDLoc(DAG.getEntryNode()), true);
308 /// Similar to CreateLiveInRegister, except value maybe loaded from a stack
309 /// slot rather than passed in a register.
310 SDValue loadStackInputValue(SelectionDAG &DAG,
313 int64_t Offset) const;
315 SDValue storeStackInputValue(SelectionDAG &DAG,
319 int64_t Offset) const;
321 SDValue loadInputValue(SelectionDAG &DAG,
322 const TargetRegisterClass *RC,
323 EVT VT, const SDLoc &SL,
324 const ArgDescriptor &Arg) const;
326 enum ImplicitParameter {
328 GRID_DIM = FIRST_IMPLICIT,
332 /// Helper function that returns the byte offset of the given
333 /// type of implicit parameter.
334 uint32_t getImplicitParameterOffset(const MachineFunction &MF,
335 const ImplicitParameter Param) const;
337 MVT getFenceOperandTy(const DataLayout &DL) const override {
341 AtomicExpansionKind shouldExpandAtomicRMWInIR(AtomicRMWInst *) const override;
344 namespace AMDGPUISD {
346 enum NodeType : unsigned {
348 FIRST_NUMBER = ISD::BUILTIN_OP_END,
349 UMUL, // 32bit unsigned multiplication
351 // End AMDIL ISD Opcodes
358 // Masked control flow nodes.
363 // A uniform kernel return that terminates the wavefront.
366 // Return to a shader part's epilog code.
369 // Return with values from a non-entry function.
375 /// CLAMP value between 0.0 and 1.0. NaN clamped to 0, following clamp output
376 /// modifier behavior with dx10_enable.
379 // This is SETCC with the full mask result which is used for a compare with a
380 // result bit per item in the wavefront.
386 // FP ops with input and output chain.
390 // SIN_HW, COS_HW - f32 for SI, 1 ULP max error, valid from -100 pi to 100 pi.
391 // Denormals handled on some parts.
411 // For emitting ISD::FMAD when f32 denormals are enabled because mac/mad is
412 // treated as an illegal operation.
415 // RCP, RSQ - For f32, 1 ULP max error, no denormal handling.
416 // For f64, max error 2^29 ULP, handles denormals.
428 BFE_U32, // Extract range of bits with zero extension to 32-bits.
429 BFE_I32, // Extract range of bits with sign extension to 32-bits.
430 BFI, // (src0 & src1) | (~src0 & src2)
431 BFM, // Insert a range of bits into a 32-bit word.
432 FFBH_U32, // ctlz with -1 if input is zero.
434 FFBL_B32, // cttz with -1 if input is zero.
456 // These cvt_f32_ubyte* nodes need to remain consecutive and in order.
462 // Convert two float 32 numbers into a single register holding two packed f16
463 // with round to zero.
470 // Same as the standard node, except the high bits of the resulting integer
474 // Wrapper around fp16 results that are known to zero the high bits.
477 /// This node is for VLIW targets and it is used to represent a vector
478 /// that is stored in consecutive registers with the same channel.
485 BUILD_VERTICAL_VECTOR,
486 /// Pointer to the start of the shader's constant data.
491 FIRST_MEM_OPCODE_NUMBER = ISD::FIRST_TARGET_MEMORY_OPCODE,
501 TBUFFER_STORE_FORMAT,
502 TBUFFER_STORE_FORMAT_D16,
504 TBUFFER_LOAD_FORMAT_D16,
518 BUFFER_LOAD_FORMAT_D16,
524 BUFFER_STORE_FORMAT_D16,
537 BUFFER_ATOMIC_CMPSWAP,
540 BUFFER_ATOMIC_PK_FADD,
543 LAST_AMDGPU_ISD_NUMBER
547 } // End namespace AMDGPUISD
549 } // End namespace llvm