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
18 #include "llvm/CodeGen/CallingConvLower.h"
19 #include "llvm/CodeGen/TargetLowering.h"
23 class AMDGPUMachineFunction
;
24 class AMDGPUSubtarget
;
27 class AMDGPUTargetLowering
: public TargetLowering
{
29 const AMDGPUSubtarget
*Subtarget
;
31 /// \returns AMDGPUISD::FFBH_U32 node if the incoming \p Op may have been
32 /// legalized from a smaller type VT. Need to match pre-legalized type because
33 /// the generic legalization inserts the add/sub between the select and
35 SDValue
getFFBX_U32(SelectionDAG
&DAG
, SDValue Op
, const SDLoc
&DL
, unsigned Opc
) const;
38 static unsigned numBitsUnsigned(SDValue Op
, SelectionDAG
&DAG
);
39 static unsigned numBitsSigned(SDValue Op
, SelectionDAG
&DAG
);
40 static bool hasDefinedInitializer(const GlobalValue
*GV
);
43 SDValue
LowerEXTRACT_SUBVECTOR(SDValue Op
, SelectionDAG
&DAG
) const;
44 SDValue
LowerCONCAT_VECTORS(SDValue Op
, SelectionDAG
&DAG
) const;
45 /// Split a vector store into multiple scalar stores.
46 /// \returns The resulting chain.
48 SDValue
LowerFREM(SDValue Op
, SelectionDAG
&DAG
) const;
49 SDValue
LowerFCEIL(SDValue Op
, SelectionDAG
&DAG
) const;
50 SDValue
LowerFTRUNC(SDValue Op
, SelectionDAG
&DAG
) const;
51 SDValue
LowerFRINT(SDValue Op
, SelectionDAG
&DAG
) const;
52 SDValue
LowerFNEARBYINT(SDValue Op
, SelectionDAG
&DAG
) const;
54 SDValue
LowerFROUND(SDValue Op
, SelectionDAG
&DAG
) const;
55 SDValue
LowerFFLOOR(SDValue Op
, SelectionDAG
&DAG
) const;
56 SDValue
LowerFLOG(SDValue Op
, SelectionDAG
&DAG
,
57 double Log2BaseInverted
) const;
58 SDValue
lowerFEXP(SDValue Op
, SelectionDAG
&DAG
) const;
60 SDValue
LowerCTLZ_CTTZ(SDValue Op
, SelectionDAG
&DAG
) const;
62 SDValue
LowerINT_TO_FP32(SDValue Op
, SelectionDAG
&DAG
, bool Signed
) const;
63 SDValue
LowerINT_TO_FP64(SDValue Op
, SelectionDAG
&DAG
, bool Signed
) const;
64 SDValue
LowerUINT_TO_FP(SDValue Op
, SelectionDAG
&DAG
) const;
65 SDValue
LowerSINT_TO_FP(SDValue Op
, SelectionDAG
&DAG
) const;
67 SDValue
LowerFP_TO_INT64(SDValue Op
, SelectionDAG
&DAG
, bool Signed
) const;
68 SDValue
LowerFP_TO_FP16(SDValue Op
, SelectionDAG
&DAG
) const;
69 SDValue
LowerFP_TO_INT(SDValue Op
, SelectionDAG
&DAG
) const;
71 SDValue
LowerSIGN_EXTEND_INREG(SDValue Op
, SelectionDAG
&DAG
) const;
74 bool shouldCombineMemoryType(EVT VT
) const;
75 SDValue
performLoadCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
76 SDValue
performStoreCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
77 SDValue
performAssertSZExtCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
78 SDValue
performIntrinsicWOChainCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
80 SDValue
splitBinaryBitConstantOpImpl(DAGCombinerInfo
&DCI
, const SDLoc
&SL
,
81 unsigned Opc
, SDValue LHS
,
82 uint32_t ValLo
, uint32_t ValHi
) const;
83 SDValue
performShlCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
84 SDValue
performSraCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
85 SDValue
performSrlCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
86 SDValue
performTruncateCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
87 SDValue
performMulCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
88 SDValue
performMulhsCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
89 SDValue
performMulhuCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
90 SDValue
performCtlz_CttzCombine(const SDLoc
&SL
, SDValue Cond
, SDValue LHS
,
91 SDValue RHS
, DAGCombinerInfo
&DCI
) const;
92 SDValue
performSelectCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
94 bool isConstantCostlierToNegate(SDValue N
) const;
95 SDValue
performFNegCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
96 SDValue
performFAbsCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
97 SDValue
performRcpCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
99 static EVT
getEquivalentMemType(LLVMContext
&Context
, EVT VT
);
101 virtual SDValue
LowerGlobalAddress(AMDGPUMachineFunction
*MFI
, SDValue Op
,
102 SelectionDAG
&DAG
) const;
104 /// Return 64-bit value Op as two 32-bit integers.
105 std::pair
<SDValue
, SDValue
> split64BitValue(SDValue Op
,
106 SelectionDAG
&DAG
) const;
107 SDValue
getLoHalf64(SDValue Op
, SelectionDAG
&DAG
) const;
108 SDValue
getHiHalf64(SDValue Op
, SelectionDAG
&DAG
) const;
110 /// Split a vector type into two parts. The first part is a power of two
111 /// vector. The second part is whatever is left over, and is a scalar if it
112 /// would otherwise be a 1-vector.
113 std::pair
<EVT
, EVT
> getSplitDestVTs(const EVT
&VT
, SelectionDAG
&DAG
) const;
115 /// Split a vector value into two parts of types LoVT and HiVT. HiVT could be
117 std::pair
<SDValue
, SDValue
> splitVector(const SDValue
&N
, const SDLoc
&DL
,
118 const EVT
&LoVT
, const EVT
&HighVT
,
119 SelectionDAG
&DAG
) const;
121 /// Split a vector load into 2 loads of half the vector.
122 SDValue
SplitVectorLoad(SDValue Op
, SelectionDAG
&DAG
) const;
124 /// Widen a suitably aligned v3 load. For all other cases, split the input
126 SDValue
WidenOrSplitVectorLoad(SDValue Op
, SelectionDAG
&DAG
) const;
128 /// Split a vector store into 2 stores of half the vector.
129 SDValue
SplitVectorStore(SDValue Op
, SelectionDAG
&DAG
) const;
131 SDValue
LowerSTORE(SDValue Op
, SelectionDAG
&DAG
) const;
132 SDValue
LowerSDIVREM(SDValue Op
, SelectionDAG
&DAG
) const;
133 SDValue
LowerUDIVREM(SDValue Op
, SelectionDAG
&DAG
) const;
134 SDValue
LowerDIVREM24(SDValue Op
, SelectionDAG
&DAG
, bool sign
) const;
135 void LowerUDIVREM64(SDValue Op
, SelectionDAG
&DAG
,
136 SmallVectorImpl
<SDValue
> &Results
) const;
138 void analyzeFormalArgumentsCompute(
140 const SmallVectorImpl
<ISD::InputArg
> &Ins
) const;
143 AMDGPUTargetLowering(const TargetMachine
&TM
, const AMDGPUSubtarget
&STI
);
145 bool mayIgnoreSignedZero(SDValue Op
) const;
147 static inline SDValue
stripBitcast(SDValue Val
) {
148 return Val
.getOpcode() == ISD::BITCAST
? Val
.getOperand(0) : Val
;
151 static bool allUsesHaveSourceMods(const SDNode
*N
,
152 unsigned CostThreshold
= 4);
153 bool isFAbsFree(EVT VT
) const override
;
154 bool isFNegFree(EVT VT
) const override
;
155 bool isTruncateFree(EVT Src
, EVT Dest
) const override
;
156 bool isTruncateFree(Type
*Src
, Type
*Dest
) const override
;
158 bool isZExtFree(Type
*Src
, Type
*Dest
) const override
;
159 bool isZExtFree(EVT Src
, EVT Dest
) const override
;
160 bool isZExtFree(SDValue Val
, EVT VT2
) const override
;
162 SDValue
getNegatedExpression(SDValue Op
, SelectionDAG
&DAG
,
163 bool LegalOperations
, bool ForCodeSize
,
165 unsigned Depth
) const override
;
167 bool isNarrowingProfitable(EVT VT1
, EVT VT2
) const override
;
169 EVT
getTypeForExtReturn(LLVMContext
&Context
, EVT VT
,
170 ISD::NodeType ExtendKind
) const override
;
172 MVT
getVectorIdxTy(const DataLayout
&) const override
;
173 bool isSelectSupported(SelectSupportKind
) const override
;
175 bool isFPImmLegal(const APFloat
&Imm
, EVT VT
,
176 bool ForCodeSize
) const override
;
177 bool ShouldShrinkFPConstant(EVT VT
) const override
;
178 bool shouldReduceLoadWidth(SDNode
*Load
,
179 ISD::LoadExtType ExtType
,
180 EVT ExtVT
) const override
;
182 bool isLoadBitCastBeneficial(EVT
, EVT
, const SelectionDAG
&DAG
,
183 const MachineMemOperand
&MMO
) const final
;
185 bool storeOfVectorConstantIsCheap(EVT MemVT
,
187 unsigned AS
) const override
;
188 bool aggressivelyPreferBuildVectorSources(EVT VecVT
) const override
;
189 bool isCheapToSpeculateCttz() const override
;
190 bool isCheapToSpeculateCtlz() const override
;
192 bool isSDNodeAlwaysUniform(const SDNode
*N
) const override
;
193 static CCAssignFn
*CCAssignFnForCall(CallingConv::ID CC
, bool IsVarArg
);
194 static CCAssignFn
*CCAssignFnForReturn(CallingConv::ID CC
, bool IsVarArg
);
196 SDValue
LowerReturn(SDValue Chain
, CallingConv::ID CallConv
, bool isVarArg
,
197 const SmallVectorImpl
<ISD::OutputArg
> &Outs
,
198 const SmallVectorImpl
<SDValue
> &OutVals
, const SDLoc
&DL
,
199 SelectionDAG
&DAG
) const override
;
201 SDValue
addTokenForArgument(SDValue Chain
,
203 MachineFrameInfo
&MFI
,
204 int ClobberedFI
) const;
206 SDValue
lowerUnhandledCall(CallLoweringInfo
&CLI
,
207 SmallVectorImpl
<SDValue
> &InVals
,
208 StringRef Reason
) const;
209 SDValue
LowerCall(CallLoweringInfo
&CLI
,
210 SmallVectorImpl
<SDValue
> &InVals
) const override
;
212 SDValue
LowerDYNAMIC_STACKALLOC(SDValue Op
,
213 SelectionDAG
&DAG
) const;
215 SDValue
LowerOperation(SDValue Op
, SelectionDAG
&DAG
) const override
;
216 SDValue
PerformDAGCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const override
;
217 void ReplaceNodeResults(SDNode
* N
,
218 SmallVectorImpl
<SDValue
> &Results
,
219 SelectionDAG
&DAG
) const override
;
221 SDValue
combineFMinMaxLegacy(const SDLoc
&DL
, EVT VT
, SDValue LHS
,
222 SDValue RHS
, SDValue True
, SDValue False
,
223 SDValue CC
, DAGCombinerInfo
&DCI
) const;
225 const char* getTargetNodeName(unsigned Opcode
) const override
;
227 // FIXME: Turn off MergeConsecutiveStores() before Instruction Selection for
228 // AMDGPU. Commit r319036,
229 // (https://github.com/llvm/llvm-project/commit/db77e57ea86d941a4262ef60261692f4cb6893e6)
230 // turned on MergeConsecutiveStores() before Instruction Selection for all
231 // targets. Enough AMDGPU compiles go into an infinite loop (
232 // MergeConsecutiveStores() merges two stores; LegalizeStoreOps() un-merges;
233 // MergeConsecutiveStores() re-merges, etc. ) to warrant turning it off for
235 bool mergeStoresAfterLegalization(EVT
) const override
{ return false; }
237 bool isFsqrtCheap(SDValue Operand
, SelectionDAG
&DAG
) const override
{
240 SDValue
getSqrtEstimate(SDValue Operand
, SelectionDAG
&DAG
, int Enabled
,
241 int &RefinementSteps
, bool &UseOneConstNR
,
242 bool Reciprocal
) const override
;
243 SDValue
getRecipEstimate(SDValue Operand
, SelectionDAG
&DAG
, int Enabled
,
244 int &RefinementSteps
) const override
;
246 virtual SDNode
*PostISelFolding(MachineSDNode
*N
,
247 SelectionDAG
&DAG
) const = 0;
249 /// Determine which of the bits specified in \p Mask are known to be
250 /// either zero or one and return them in the \p KnownZero and \p KnownOne
252 void computeKnownBitsForTargetNode(const SDValue Op
,
254 const APInt
&DemandedElts
,
255 const SelectionDAG
&DAG
,
256 unsigned Depth
= 0) const override
;
258 unsigned ComputeNumSignBitsForTargetNode(SDValue Op
, const APInt
&DemandedElts
,
259 const SelectionDAG
&DAG
,
260 unsigned Depth
= 0) const override
;
262 unsigned computeNumSignBitsForTargetInstr(GISelKnownBits
&Analysis
,
264 const APInt
&DemandedElts
,
265 const MachineRegisterInfo
&MRI
,
266 unsigned Depth
= 0) const override
;
268 bool isKnownNeverNaNForTargetNode(SDValue Op
,
269 const SelectionDAG
&DAG
,
271 unsigned Depth
= 0) const override
;
273 /// Helper function that adds Reg to the LiveIn list of the DAG's
276 /// \returns a RegisterSDNode representing Reg if \p RawReg is true, otherwise
277 /// a copy from the register.
278 SDValue
CreateLiveInRegister(SelectionDAG
&DAG
,
279 const TargetRegisterClass
*RC
,
280 Register Reg
, EVT VT
,
282 bool RawReg
= false) const;
283 SDValue
CreateLiveInRegister(SelectionDAG
&DAG
,
284 const TargetRegisterClass
*RC
,
285 Register Reg
, EVT VT
) const {
286 return CreateLiveInRegister(DAG
, RC
, Reg
, VT
, SDLoc(DAG
.getEntryNode()));
289 // Returns the raw live in register rather than a copy from it.
290 SDValue
CreateLiveInRegisterRaw(SelectionDAG
&DAG
,
291 const TargetRegisterClass
*RC
,
292 Register Reg
, EVT VT
) const {
293 return CreateLiveInRegister(DAG
, RC
, Reg
, VT
, SDLoc(DAG
.getEntryNode()), true);
296 /// Similar to CreateLiveInRegister, except value maybe loaded from a stack
297 /// slot rather than passed in a register.
298 SDValue
loadStackInputValue(SelectionDAG
&DAG
,
301 int64_t Offset
) const;
303 SDValue
storeStackInputValue(SelectionDAG
&DAG
,
307 int64_t Offset
) const;
309 SDValue
loadInputValue(SelectionDAG
&DAG
,
310 const TargetRegisterClass
*RC
,
311 EVT VT
, const SDLoc
&SL
,
312 const ArgDescriptor
&Arg
) const;
314 enum ImplicitParameter
{
316 GRID_DIM
= FIRST_IMPLICIT
,
320 /// Helper function that returns the byte offset of the given
321 /// type of implicit parameter.
322 uint32_t getImplicitParameterOffset(const MachineFunction
&MF
,
323 const ImplicitParameter Param
) const;
325 MVT
getFenceOperandTy(const DataLayout
&DL
) const override
{
329 AtomicExpansionKind
shouldExpandAtomicRMWInIR(AtomicRMWInst
*) const override
;
331 bool isConstantUnsignedBitfieldExtactLegal(unsigned Opc
, LLT Ty1
,
332 LLT Ty2
) const override
;
335 namespace AMDGPUISD
{
337 enum NodeType
: unsigned {
339 FIRST_NUMBER
= ISD::BUILTIN_OP_END
,
340 UMUL
, // 32bit unsigned multiplication
342 // End AMDIL ISD Opcodes
349 // Masked control flow nodes.
354 // A uniform kernel return that terminates the wavefront.
357 // Return to a shader part's epilog code.
360 // Return with values from a non-entry function.
366 /// CLAMP value between 0.0 and 1.0. NaN clamped to 0, following clamp output
367 /// modifier behavior with dx10_enable.
370 // This is SETCC with the full mask result which is used for a compare with a
371 // result bit per item in the wavefront.
377 // FP ops with input and output chain.
381 // SIN_HW, COS_HW - f32 for SI, 1 ULP max error, valid from -100 pi to 100 pi.
382 // Denormals handled on some parts.
402 // For emitting ISD::FMAD when f32 denormals are enabled because mac/mad is
403 // treated as an illegal operation.
406 // RCP, RSQ - For f32, 1 ULP max error, no denormal handling.
407 // For f64, max error 2^29 ULP, handles denormals.
419 BFE_U32
, // Extract range of bits with zero extension to 32-bits.
420 BFE_I32
, // Extract range of bits with sign extension to 32-bits.
421 BFI
, // (src0 & src1) | (~src0 & src2)
422 BFM
, // Insert a range of bits into a 32-bit word.
423 FFBH_U32
, // ctlz with -1 if input is zero.
425 FFBL_B32
, // cttz with -1 if input is zero.
445 // These cvt_f32_ubyte* nodes need to remain consecutive and in order.
451 // Convert two float 32 numbers into a single register holding two packed f16
452 // with round to zero.
459 // Same as the standard node, except the high bits of the resulting integer
463 /// This node is for VLIW targets and it is used to represent a vector
464 /// that is stored in consecutive registers with the same channel.
471 BUILD_VERTICAL_VECTOR
,
472 /// Pointer to the start of the shader's constant data.
477 FIRST_MEM_OPCODE_NUMBER
= ISD::FIRST_TARGET_MEMORY_OPCODE
,
487 TBUFFER_STORE_FORMAT
,
488 TBUFFER_STORE_FORMAT_D16
,
490 TBUFFER_LOAD_FORMAT_D16
,
503 BUFFER_LOAD_FORMAT_D16
,
509 BUFFER_STORE_FORMAT_D16
,
522 BUFFER_ATOMIC_CMPSWAP
,
528 LAST_AMDGPU_ISD_NUMBER
532 } // End namespace AMDGPUISD
534 } // End namespace llvm