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 /// \returns The minimum number of bits needed to store the value of \Op as an
39 /// unsigned integer. Truncating to this size and then zero-extending to the
40 /// original size will not change the value.
41 static unsigned numBitsUnsigned(SDValue Op
, SelectionDAG
&DAG
);
43 /// \returns The minimum number of bits needed to store the value of \Op as a
44 /// signed integer. Truncating to this size and then sign-extending to the
45 /// original size will not change the value.
46 static unsigned numBitsSigned(SDValue Op
, SelectionDAG
&DAG
);
49 SDValue
LowerEXTRACT_SUBVECTOR(SDValue Op
, SelectionDAG
&DAG
) const;
50 SDValue
LowerCONCAT_VECTORS(SDValue Op
, SelectionDAG
&DAG
) const;
51 /// Split a vector store into multiple scalar stores.
52 /// \returns The resulting chain.
54 SDValue
LowerFREM(SDValue Op
, SelectionDAG
&DAG
) const;
55 SDValue
LowerFCEIL(SDValue Op
, SelectionDAG
&DAG
) const;
56 SDValue
LowerFTRUNC(SDValue Op
, SelectionDAG
&DAG
) const;
57 SDValue
LowerFRINT(SDValue Op
, SelectionDAG
&DAG
) const;
58 SDValue
LowerFNEARBYINT(SDValue Op
, SelectionDAG
&DAG
) const;
60 SDValue
LowerFROUND(SDValue Op
, SelectionDAG
&DAG
) const;
61 SDValue
LowerFFLOOR(SDValue Op
, SelectionDAG
&DAG
) const;
62 SDValue
LowerFLOG(SDValue Op
, SelectionDAG
&DAG
,
63 double Log2BaseInverted
) const;
64 SDValue
lowerFEXP(SDValue Op
, SelectionDAG
&DAG
) const;
66 SDValue
LowerCTLZ_CTTZ(SDValue Op
, SelectionDAG
&DAG
) const;
68 SDValue
LowerINT_TO_FP32(SDValue Op
, SelectionDAG
&DAG
, bool Signed
) const;
69 SDValue
LowerINT_TO_FP64(SDValue Op
, SelectionDAG
&DAG
, bool Signed
) const;
70 SDValue
LowerUINT_TO_FP(SDValue Op
, SelectionDAG
&DAG
) const;
71 SDValue
LowerSINT_TO_FP(SDValue Op
, SelectionDAG
&DAG
) const;
73 SDValue
LowerFP_TO_INT64(SDValue Op
, SelectionDAG
&DAG
, bool Signed
) const;
74 SDValue
LowerFP_TO_FP16(SDValue Op
, SelectionDAG
&DAG
) const;
75 SDValue
LowerFP_TO_INT(SDValue Op
, SelectionDAG
&DAG
) const;
77 SDValue
LowerSIGN_EXTEND_INREG(SDValue Op
, SelectionDAG
&DAG
) const;
80 bool shouldCombineMemoryType(EVT VT
) const;
81 SDValue
performLoadCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
82 SDValue
performStoreCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
83 SDValue
performAssertSZExtCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
84 SDValue
performIntrinsicWOChainCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
86 SDValue
splitBinaryBitConstantOpImpl(DAGCombinerInfo
&DCI
, const SDLoc
&SL
,
87 unsigned Opc
, SDValue LHS
,
88 uint32_t ValLo
, uint32_t ValHi
) const;
89 SDValue
performShlCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
90 SDValue
performSraCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
91 SDValue
performSrlCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
92 SDValue
performTruncateCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
93 SDValue
performMulCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
94 SDValue
performMulLoHiCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
95 SDValue
performMulhsCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
96 SDValue
performMulhuCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
97 SDValue
performCtlz_CttzCombine(const SDLoc
&SL
, SDValue Cond
, SDValue LHS
,
98 SDValue RHS
, DAGCombinerInfo
&DCI
) const;
99 SDValue
performSelectCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
101 bool isConstantCostlierToNegate(SDValue N
) const;
102 SDValue
performFNegCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
103 SDValue
performFAbsCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
104 SDValue
performRcpCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const;
106 static EVT
getEquivalentMemType(LLVMContext
&Context
, EVT VT
);
108 virtual SDValue
LowerGlobalAddress(AMDGPUMachineFunction
*MFI
, SDValue Op
,
109 SelectionDAG
&DAG
) const;
111 /// Return 64-bit value Op as two 32-bit integers.
112 std::pair
<SDValue
, SDValue
> split64BitValue(SDValue Op
,
113 SelectionDAG
&DAG
) const;
114 SDValue
getLoHalf64(SDValue Op
, SelectionDAG
&DAG
) const;
115 SDValue
getHiHalf64(SDValue Op
, SelectionDAG
&DAG
) const;
117 /// Split a vector type into two parts. The first part is a power of two
118 /// vector. The second part is whatever is left over, and is a scalar if it
119 /// would otherwise be a 1-vector.
120 std::pair
<EVT
, EVT
> getSplitDestVTs(const EVT
&VT
, SelectionDAG
&DAG
) const;
122 /// Split a vector value into two parts of types LoVT and HiVT. HiVT could be
124 std::pair
<SDValue
, SDValue
> splitVector(const SDValue
&N
, const SDLoc
&DL
,
125 const EVT
&LoVT
, const EVT
&HighVT
,
126 SelectionDAG
&DAG
) const;
128 /// Split a vector load into 2 loads of half the vector.
129 SDValue
SplitVectorLoad(SDValue Op
, SelectionDAG
&DAG
) const;
131 /// Widen a suitably aligned v3 load. For all other cases, split the input
133 SDValue
WidenOrSplitVectorLoad(SDValue Op
, SelectionDAG
&DAG
) const;
135 /// Split a vector store into 2 stores of half the vector.
136 SDValue
SplitVectorStore(SDValue Op
, SelectionDAG
&DAG
) const;
138 SDValue
LowerSTORE(SDValue Op
, SelectionDAG
&DAG
) const;
139 SDValue
LowerSDIVREM(SDValue Op
, SelectionDAG
&DAG
) const;
140 SDValue
LowerUDIVREM(SDValue Op
, SelectionDAG
&DAG
) const;
141 SDValue
LowerDIVREM24(SDValue Op
, SelectionDAG
&DAG
, bool sign
) const;
142 void LowerUDIVREM64(SDValue Op
, SelectionDAG
&DAG
,
143 SmallVectorImpl
<SDValue
> &Results
) const;
145 void analyzeFormalArgumentsCompute(
147 const SmallVectorImpl
<ISD::InputArg
> &Ins
) const;
150 AMDGPUTargetLowering(const TargetMachine
&TM
, const AMDGPUSubtarget
&STI
);
152 bool mayIgnoreSignedZero(SDValue Op
) const;
154 static inline SDValue
stripBitcast(SDValue Val
) {
155 return Val
.getOpcode() == ISD::BITCAST
? Val
.getOperand(0) : Val
;
158 static bool allUsesHaveSourceMods(const SDNode
*N
,
159 unsigned CostThreshold
= 4);
160 bool isFAbsFree(EVT VT
) const override
;
161 bool isFNegFree(EVT VT
) const override
;
162 bool isTruncateFree(EVT Src
, EVT Dest
) const override
;
163 bool isTruncateFree(Type
*Src
, Type
*Dest
) const override
;
165 bool isZExtFree(Type
*Src
, Type
*Dest
) const override
;
166 bool isZExtFree(EVT Src
, EVT Dest
) const override
;
167 bool isZExtFree(SDValue Val
, EVT VT2
) const override
;
169 SDValue
getNegatedExpression(SDValue Op
, SelectionDAG
&DAG
,
170 bool LegalOperations
, bool ForCodeSize
,
172 unsigned Depth
) const override
;
174 bool isNarrowingProfitable(EVT VT1
, EVT VT2
) const override
;
176 EVT
getTypeForExtReturn(LLVMContext
&Context
, EVT VT
,
177 ISD::NodeType ExtendKind
) const override
;
179 MVT
getVectorIdxTy(const DataLayout
&) const override
;
180 bool isSelectSupported(SelectSupportKind
) const override
;
182 bool isFPImmLegal(const APFloat
&Imm
, EVT VT
,
183 bool ForCodeSize
) const override
;
184 bool ShouldShrinkFPConstant(EVT VT
) const override
;
185 bool shouldReduceLoadWidth(SDNode
*Load
,
186 ISD::LoadExtType ExtType
,
187 EVT ExtVT
) const override
;
189 bool isLoadBitCastBeneficial(EVT
, EVT
, const SelectionDAG
&DAG
,
190 const MachineMemOperand
&MMO
) const final
;
192 bool storeOfVectorConstantIsCheap(EVT MemVT
,
194 unsigned AS
) const override
;
195 bool aggressivelyPreferBuildVectorSources(EVT VecVT
) const override
;
196 bool isCheapToSpeculateCttz() const override
;
197 bool isCheapToSpeculateCtlz() const override
;
199 bool isSDNodeAlwaysUniform(const SDNode
*N
) const override
;
200 static CCAssignFn
*CCAssignFnForCall(CallingConv::ID CC
, bool IsVarArg
);
201 static CCAssignFn
*CCAssignFnForReturn(CallingConv::ID CC
, bool IsVarArg
);
203 SDValue
LowerReturn(SDValue Chain
, CallingConv::ID CallConv
, bool isVarArg
,
204 const SmallVectorImpl
<ISD::OutputArg
> &Outs
,
205 const SmallVectorImpl
<SDValue
> &OutVals
, const SDLoc
&DL
,
206 SelectionDAG
&DAG
) const override
;
208 SDValue
addTokenForArgument(SDValue Chain
,
210 MachineFrameInfo
&MFI
,
211 int ClobberedFI
) const;
213 SDValue
lowerUnhandledCall(CallLoweringInfo
&CLI
,
214 SmallVectorImpl
<SDValue
> &InVals
,
215 StringRef Reason
) const;
216 SDValue
LowerCall(CallLoweringInfo
&CLI
,
217 SmallVectorImpl
<SDValue
> &InVals
) const override
;
219 SDValue
LowerDYNAMIC_STACKALLOC(SDValue Op
,
220 SelectionDAG
&DAG
) const;
222 SDValue
LowerOperation(SDValue Op
, SelectionDAG
&DAG
) const override
;
223 SDValue
PerformDAGCombine(SDNode
*N
, DAGCombinerInfo
&DCI
) const override
;
224 void ReplaceNodeResults(SDNode
* N
,
225 SmallVectorImpl
<SDValue
> &Results
,
226 SelectionDAG
&DAG
) const override
;
228 SDValue
combineFMinMaxLegacy(const SDLoc
&DL
, EVT VT
, SDValue LHS
,
229 SDValue RHS
, SDValue True
, SDValue False
,
230 SDValue CC
, DAGCombinerInfo
&DCI
) const;
232 const char* getTargetNodeName(unsigned Opcode
) const override
;
234 // FIXME: Turn off MergeConsecutiveStores() before Instruction Selection for
235 // AMDGPU. Commit r319036,
236 // (https://github.com/llvm/llvm-project/commit/db77e57ea86d941a4262ef60261692f4cb6893e6)
237 // turned on MergeConsecutiveStores() before Instruction Selection for all
238 // targets. Enough AMDGPU compiles go into an infinite loop (
239 // MergeConsecutiveStores() merges two stores; LegalizeStoreOps() un-merges;
240 // MergeConsecutiveStores() re-merges, etc. ) to warrant turning it off for
242 bool mergeStoresAfterLegalization(EVT
) const override
{ return false; }
244 bool isFsqrtCheap(SDValue Operand
, SelectionDAG
&DAG
) const override
{
247 SDValue
getSqrtEstimate(SDValue Operand
, SelectionDAG
&DAG
, int Enabled
,
248 int &RefinementSteps
, bool &UseOneConstNR
,
249 bool Reciprocal
) const override
;
250 SDValue
getRecipEstimate(SDValue Operand
, SelectionDAG
&DAG
, int Enabled
,
251 int &RefinementSteps
) const override
;
253 virtual SDNode
*PostISelFolding(MachineSDNode
*N
,
254 SelectionDAG
&DAG
) const = 0;
256 /// Determine which of the bits specified in \p Mask are known to be
257 /// either zero or one and return them in the \p KnownZero and \p KnownOne
259 void computeKnownBitsForTargetNode(const SDValue Op
,
261 const APInt
&DemandedElts
,
262 const SelectionDAG
&DAG
,
263 unsigned Depth
= 0) const override
;
265 unsigned ComputeNumSignBitsForTargetNode(SDValue Op
, const APInt
&DemandedElts
,
266 const SelectionDAG
&DAG
,
267 unsigned Depth
= 0) const override
;
269 unsigned computeNumSignBitsForTargetInstr(GISelKnownBits
&Analysis
,
271 const APInt
&DemandedElts
,
272 const MachineRegisterInfo
&MRI
,
273 unsigned Depth
= 0) const override
;
275 bool isKnownNeverNaNForTargetNode(SDValue Op
,
276 const SelectionDAG
&DAG
,
278 unsigned Depth
= 0) const override
;
280 /// Helper function that adds Reg to the LiveIn list of the DAG's
283 /// \returns a RegisterSDNode representing Reg if \p RawReg is true, otherwise
284 /// a copy from the register.
285 SDValue
CreateLiveInRegister(SelectionDAG
&DAG
,
286 const TargetRegisterClass
*RC
,
287 Register Reg
, EVT VT
,
289 bool RawReg
= false) const;
290 SDValue
CreateLiveInRegister(SelectionDAG
&DAG
,
291 const TargetRegisterClass
*RC
,
292 Register Reg
, EVT VT
) const {
293 return CreateLiveInRegister(DAG
, RC
, Reg
, VT
, SDLoc(DAG
.getEntryNode()));
296 // Returns the raw live in register rather than a copy from it.
297 SDValue
CreateLiveInRegisterRaw(SelectionDAG
&DAG
,
298 const TargetRegisterClass
*RC
,
299 Register Reg
, EVT VT
) const {
300 return CreateLiveInRegister(DAG
, RC
, Reg
, VT
, SDLoc(DAG
.getEntryNode()), true);
303 /// Similar to CreateLiveInRegister, except value maybe loaded from a stack
304 /// slot rather than passed in a register.
305 SDValue
loadStackInputValue(SelectionDAG
&DAG
,
308 int64_t Offset
) const;
310 SDValue
storeStackInputValue(SelectionDAG
&DAG
,
314 int64_t Offset
) const;
316 SDValue
loadInputValue(SelectionDAG
&DAG
,
317 const TargetRegisterClass
*RC
,
318 EVT VT
, const SDLoc
&SL
,
319 const ArgDescriptor
&Arg
) const;
321 enum ImplicitParameter
{
328 /// Helper function that returns the byte offset of the given
329 /// type of implicit parameter.
330 uint32_t getImplicitParameterOffset(const MachineFunction
&MF
,
331 const ImplicitParameter Param
) const;
333 MVT
getFenceOperandTy(const DataLayout
&DL
) const override
{
337 AtomicExpansionKind
shouldExpandAtomicRMWInIR(AtomicRMWInst
*) const override
;
339 bool isConstantUnsignedBitfieldExtractLegal(unsigned Opc
, LLT Ty1
,
340 LLT Ty2
) const override
;
343 namespace AMDGPUISD
{
345 enum NodeType
: unsigned {
347 FIRST_NUMBER
= ISD::BUILTIN_OP_END
,
348 UMUL
, // 32bit unsigned multiplication
350 // End AMDIL ISD Opcodes
357 // Masked control flow nodes.
362 // A uniform kernel return that terminates the wavefront.
365 // Return to a shader part's epilog code.
368 // Return with values from a non-entry function.
374 /// CLAMP value between 0.0 and 1.0. NaN clamped to 0, following clamp output
375 /// modifier behavior with dx10_enable.
378 // This is SETCC with the full mask result which is used for a compare with a
379 // result bit per item in the wavefront.
385 // FP ops with input and output chain.
389 // SIN_HW, COS_HW - f32 for SI, 1 ULP max error, valid from -100 pi to 100 pi.
390 // Denormals handled on some parts.
410 // For emitting ISD::FMAD when f32 denormals are enabled because mac/mad is
411 // treated as an illegal operation.
414 // RCP, RSQ - For f32, 1 ULP max error, no denormal handling.
415 // For f64, max error 2^29 ULP, handles denormals.
427 BFE_U32
, // Extract range of bits with zero extension to 32-bits.
428 BFE_I32
, // Extract range of bits with sign extension to 32-bits.
429 BFI
, // (src0 & src1) | (~src0 & src2)
430 BFM
, // Insert a range of bits into a 32-bit word.
431 FFBH_U32
, // ctlz with -1 if input is zero.
433 FFBL_B32
, // cttz with -1 if input is zero.
453 // These cvt_f32_ubyte* nodes need to remain consecutive and in order.
459 // Convert two float 32 numbers into a single register holding two packed f16
460 // with round to zero.
467 // Same as the standard node, except the high bits of the resulting integer
471 /// This node is for VLIW targets and it is used to represent a vector
472 /// that is stored in consecutive registers with the same channel.
479 BUILD_VERTICAL_VECTOR
,
480 /// Pointer to the start of the shader's constant data.
484 FPTRUNC_ROUND_UPWARD
,
485 FPTRUNC_ROUND_DOWNWARD
,
488 FIRST_MEM_OPCODE_NUMBER
= ISD::FIRST_TARGET_MEMORY_OPCODE
,
498 TBUFFER_STORE_FORMAT
,
499 TBUFFER_STORE_FORMAT_D16
,
501 TBUFFER_LOAD_FORMAT_D16
,
514 BUFFER_LOAD_FORMAT_D16
,
520 BUFFER_STORE_FORMAT_D16
,
533 BUFFER_ATOMIC_CMPSWAP
,
539 LAST_AMDGPU_ISD_NUMBER
542 } // End namespace AMDGPUISD
544 } // End namespace llvm