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"
24 class AMDGPUMachineFunction;
25 class AMDGPUSubtarget;
28 class AMDGPUTargetLowering : public TargetLowering {
30 const AMDGPUSubtarget *Subtarget;
32 /// \returns AMDGPUISD::FFBH_U32 node if the incoming \p Op may have been
33 /// legalized from a smaller type VT. Need to match pre-legalized type because
34 /// the generic legalization inserts the add/sub between the select and
36 SDValue getFFBX_U32(SelectionDAG &DAG, SDValue Op, const SDLoc &DL, unsigned Opc) const;
39 static unsigned numBitsUnsigned(SDValue Op, SelectionDAG &DAG);
40 static unsigned numBitsSigned(SDValue Op, SelectionDAG &DAG);
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 LowerFROUND32_16(SDValue Op, SelectionDAG &DAG) const;
55 SDValue LowerFROUND64(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;
82 SDValue splitBinaryBitConstantOpImpl(DAGCombinerInfo &DCI, const SDLoc &SL,
83 unsigned Opc, SDValue LHS,
84 uint32_t ValLo, uint32_t ValHi) const;
85 SDValue performShlCombine(SDNode *N, DAGCombinerInfo &DCI) const;
86 SDValue performSraCombine(SDNode *N, DAGCombinerInfo &DCI) const;
87 SDValue performSrlCombine(SDNode *N, DAGCombinerInfo &DCI) const;
88 SDValue performTruncateCombine(SDNode *N, DAGCombinerInfo &DCI) const;
89 SDValue performMulCombine(SDNode *N, DAGCombinerInfo &DCI) const;
90 SDValue performMulhsCombine(SDNode *N, DAGCombinerInfo &DCI) const;
91 SDValue performMulhuCombine(SDNode *N, DAGCombinerInfo &DCI) const;
92 SDValue performMulLoHi24Combine(SDNode *N, DAGCombinerInfo &DCI) const;
93 SDValue performCtlz_CttzCombine(const SDLoc &SL, SDValue Cond, SDValue LHS,
94 SDValue RHS, DAGCombinerInfo &DCI) const;
95 SDValue performSelectCombine(SDNode *N, DAGCombinerInfo &DCI) const;
97 bool isConstantCostlierToNegate(SDValue N) const;
98 SDValue performFNegCombine(SDNode *N, DAGCombinerInfo &DCI) const;
99 SDValue performFAbsCombine(SDNode *N, DAGCombinerInfo &DCI) const;
100 SDValue performRcpCombine(SDNode *N, DAGCombinerInfo &DCI) const;
102 static EVT getEquivalentMemType(LLVMContext &Context, EVT VT);
104 virtual SDValue LowerGlobalAddress(AMDGPUMachineFunction *MFI, SDValue Op,
105 SelectionDAG &DAG) const;
107 /// Return 64-bit value Op as two 32-bit integers.
108 std::pair<SDValue, SDValue> split64BitValue(SDValue Op,
109 SelectionDAG &DAG) const;
110 SDValue getLoHalf64(SDValue Op, SelectionDAG &DAG) const;
111 SDValue getHiHalf64(SDValue Op, SelectionDAG &DAG) const;
113 /// Split a vector type into two parts. The first part is a power of two
114 /// vector. The second part is whatever is left over, and is a scalar if it
115 /// would otherwise be a 1-vector.
116 std::pair<EVT, EVT> getSplitDestVTs(const EVT &VT, SelectionDAG &DAG) const;
118 /// Split a vector value into two parts of types LoVT and HiVT. HiVT could be
120 std::pair<SDValue, SDValue> splitVector(const SDValue &N, const SDLoc &DL,
121 const EVT &LoVT, const EVT &HighVT,
122 SelectionDAG &DAG) const;
124 /// Split a vector load into 2 loads of half the vector.
125 SDValue SplitVectorLoad(SDValue Op, SelectionDAG &DAG) const;
127 /// Widen a vector load from vec3 to vec4.
128 SDValue WidenVectorLoad(SDValue Op, SelectionDAG &DAG) const;
130 /// Split a vector store into 2 stores of half the vector.
131 SDValue SplitVectorStore(SDValue Op, SelectionDAG &DAG) const;
133 SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const;
134 SDValue LowerSDIVREM(SDValue Op, SelectionDAG &DAG) const;
135 SDValue LowerUDIVREM(SDValue Op, SelectionDAG &DAG) const;
136 SDValue LowerDIVREM24(SDValue Op, SelectionDAG &DAG, bool sign) const;
137 void LowerUDIVREM64(SDValue Op, SelectionDAG &DAG,
138 SmallVectorImpl<SDValue> &Results) const;
140 void analyzeFormalArgumentsCompute(
142 const SmallVectorImpl<ISD::InputArg> &Ins) const;
145 AMDGPUTargetLowering(const TargetMachine &TM, const AMDGPUSubtarget &STI);
147 bool mayIgnoreSignedZero(SDValue Op) const {
148 if (getTargetMachine().Options.NoSignedZerosFPMath)
151 const auto Flags = Op.getNode()->getFlags();
152 if (Flags.isDefined())
153 return Flags.hasNoSignedZeros();
158 static inline SDValue stripBitcast(SDValue Val) {
159 return Val.getOpcode() == ISD::BITCAST ? Val.getOperand(0) : Val;
162 static bool allUsesHaveSourceMods(const SDNode *N,
163 unsigned CostThreshold = 4);
164 bool isFAbsFree(EVT VT) const override;
165 bool isFNegFree(EVT VT) const override;
166 bool isTruncateFree(EVT Src, EVT Dest) const override;
167 bool isTruncateFree(Type *Src, Type *Dest) const override;
169 bool isZExtFree(Type *Src, Type *Dest) const override;
170 bool isZExtFree(EVT Src, EVT Dest) const override;
171 bool isZExtFree(SDValue Val, EVT VT2) const override;
173 bool isNarrowingProfitable(EVT VT1, EVT VT2) const override;
175 MVT getVectorIdxTy(const DataLayout &) const override;
176 bool isSelectSupported(SelectSupportKind) const override;
178 bool isFPImmLegal(const APFloat &Imm, EVT VT,
179 bool ForCodeSize) const override;
180 bool ShouldShrinkFPConstant(EVT VT) const override;
181 bool shouldReduceLoadWidth(SDNode *Load,
182 ISD::LoadExtType ExtType,
183 EVT ExtVT) const override;
185 bool isLoadBitCastBeneficial(EVT, EVT, const SelectionDAG &DAG,
186 const MachineMemOperand &MMO) const final;
188 bool storeOfVectorConstantIsCheap(EVT MemVT,
190 unsigned AS) const override;
191 bool aggressivelyPreferBuildVectorSources(EVT VecVT) const override;
192 bool isCheapToSpeculateCttz() const override;
193 bool isCheapToSpeculateCtlz() const override;
195 bool isSDNodeAlwaysUniform(const SDNode *N) const override;
196 static CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg);
197 static CCAssignFn *CCAssignFnForReturn(CallingConv::ID CC, bool IsVarArg);
199 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
200 const SmallVectorImpl<ISD::OutputArg> &Outs,
201 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
202 SelectionDAG &DAG) const override;
204 SDValue addTokenForArgument(SDValue Chain,
206 MachineFrameInfo &MFI,
207 int ClobberedFI) const;
209 SDValue lowerUnhandledCall(CallLoweringInfo &CLI,
210 SmallVectorImpl<SDValue> &InVals,
211 StringRef Reason) const;
212 SDValue LowerCall(CallLoweringInfo &CLI,
213 SmallVectorImpl<SDValue> &InVals) const override;
215 SDValue LowerDYNAMIC_STACKALLOC(SDValue Op,
216 SelectionDAG &DAG) const;
218 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
219 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
220 void ReplaceNodeResults(SDNode * N,
221 SmallVectorImpl<SDValue> &Results,
222 SelectionDAG &DAG) const override;
224 SDValue combineFMinMaxLegacy(const SDLoc &DL, EVT VT, SDValue LHS,
225 SDValue RHS, SDValue True, SDValue False,
226 SDValue CC, DAGCombinerInfo &DCI) const;
228 const char* getTargetNodeName(unsigned Opcode) const override;
230 // FIXME: Turn off MergeConsecutiveStores() before Instruction Selection for
231 // AMDGPU. Commit r319036,
232 // (https://github.com/llvm/llvm-project/commit/db77e57ea86d941a4262ef60261692f4cb6893e6)
233 // turned on MergeConsecutiveStores() before Instruction Selection for all
234 // targets. Enough AMDGPU compiles go into an infinite loop (
235 // MergeConsecutiveStores() merges two stores; LegalizeStoreOps() un-merges;
236 // MergeConsecutiveStores() re-merges, etc. ) to warrant turning it off for
238 bool mergeStoresAfterLegalization(EVT) const override { return false; }
240 bool isFsqrtCheap(SDValue Operand, SelectionDAG &DAG) const override {
243 SDValue getSqrtEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled,
244 int &RefinementSteps, bool &UseOneConstNR,
245 bool Reciprocal) const override;
246 SDValue getRecipEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled,
247 int &RefinementSteps) const override;
249 virtual SDNode *PostISelFolding(MachineSDNode *N,
250 SelectionDAG &DAG) const = 0;
252 /// Determine which of the bits specified in \p Mask are known to be
253 /// either zero or one and return them in the \p KnownZero and \p KnownOne
255 void computeKnownBitsForTargetNode(const SDValue Op,
257 const APInt &DemandedElts,
258 const SelectionDAG &DAG,
259 unsigned Depth = 0) const override;
261 unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts,
262 const SelectionDAG &DAG,
263 unsigned Depth = 0) const override;
265 bool isKnownNeverNaNForTargetNode(SDValue Op,
266 const SelectionDAG &DAG,
268 unsigned Depth = 0) const override;
270 /// Helper function that adds Reg to the LiveIn list of the DAG's
273 /// \returns a RegisterSDNode representing Reg if \p RawReg is true, otherwise
274 /// a copy from the register.
275 SDValue CreateLiveInRegister(SelectionDAG &DAG,
276 const TargetRegisterClass *RC,
277 unsigned Reg, EVT VT,
279 bool RawReg = false) const;
280 SDValue CreateLiveInRegister(SelectionDAG &DAG,
281 const TargetRegisterClass *RC,
282 unsigned Reg, EVT VT) const {
283 return CreateLiveInRegister(DAG, RC, Reg, VT, SDLoc(DAG.getEntryNode()));
286 // Returns the raw live in register rather than a copy from it.
287 SDValue CreateLiveInRegisterRaw(SelectionDAG &DAG,
288 const TargetRegisterClass *RC,
289 unsigned Reg, EVT VT) const {
290 return CreateLiveInRegister(DAG, RC, Reg, VT, SDLoc(DAG.getEntryNode()), true);
293 /// Similar to CreateLiveInRegister, except value maybe loaded from a stack
294 /// slot rather than passed in a register.
295 SDValue loadStackInputValue(SelectionDAG &DAG,
298 int64_t Offset) const;
300 SDValue storeStackInputValue(SelectionDAG &DAG,
304 int64_t Offset) const;
306 SDValue loadInputValue(SelectionDAG &DAG,
307 const TargetRegisterClass *RC,
308 EVT VT, const SDLoc &SL,
309 const ArgDescriptor &Arg) const;
311 enum ImplicitParameter {
313 GRID_DIM = FIRST_IMPLICIT,
317 /// Helper function that returns the byte offset of the given
318 /// type of implicit parameter.
319 uint32_t getImplicitParameterOffset(const MachineFunction &MF,
320 const ImplicitParameter Param) const;
322 MVT getFenceOperandTy(const DataLayout &DL) const override {
326 AtomicExpansionKind shouldExpandAtomicRMWInIR(AtomicRMWInst *) const override;
328 bool SelectFlatOffset(bool IsSigned, SelectionDAG &DAG, SDNode *N,
329 SDValue Addr, SDValue &VAddr, SDValue &Offset,
333 namespace AMDGPUISD {
335 enum NodeType : unsigned {
337 FIRST_NUMBER = ISD::BUILTIN_OP_END,
338 UMUL, // 32bit unsigned multiplication
340 // End AMDIL ISD Opcodes
347 // Masked control flow nodes.
352 // A uniform kernel return that terminates the wavefront.
355 // Return to a shader part's epilog code.
358 // Return with values from a non-entry function.
364 /// CLAMP value between 0.0 and 1.0. NaN clamped to 0, following clamp output
365 /// modifier behavior with dx10_enable.
368 // This is SETCC with the full mask result which is used for a compare with a
369 // result bit per item in the wavefront.
372 // FP ops with input and output chain.
376 // SIN_HW, COS_HW - f32 for SI, 1 ULP max error, valid from -100 pi to 100 pi.
377 // Denormals handled on some parts.
397 // For emitting ISD::FMAD when f32 denormals are enabled because mac/mad is
398 // treated as an illegal operation.
400 TRIG_PREOP, // 1 ULP max error for f64
402 // RCP, RSQ - For f32, 1 ULP max error, no denormal handling.
403 // For f64, max error 2^29 ULP, handles denormals.
416 BFE_U32, // Extract range of bits with zero extension to 32-bits.
417 BFE_I32, // Extract range of bits with sign extension to 32-bits.
418 BFI, // (src0 & src1) | (~src0 & src2)
419 BFM, // Insert a range of bits into a 32-bit word.
420 FFBH_U32, // ctlz with -1 if input is zero.
422 FFBL_B32, // cttz with -1 if input is zero.
435 EXPORT, // exp on SI+
436 EXPORT_DONE, // exp on SI+ with done bit set
446 // These cvt_f32_ubyte* nodes need to remain consecutive and in order.
452 // Convert two float 32 numbers into a single register holding two packed f16
453 // with round to zero.
460 // Same as the standard node, except the high bits of the resulting integer
464 // Wrapper around fp16 results that are known to zero the high bits.
467 /// This node is for VLIW targets and it is used to represent a vector
468 /// that is stored in consecutive registers with the same channel.
475 BUILD_VERTICAL_VECTOR,
476 /// Pointer to the start of the shader's constant data.
479 INIT_EXEC_FROM_INPUT,
492 FIRST_MEM_OPCODE_NUMBER = ISD::FIRST_TARGET_MEMORY_OPCODE,
502 TBUFFER_STORE_FORMAT,
503 TBUFFER_STORE_FORMAT_D16,
505 TBUFFER_LOAD_FORMAT_D16,
518 BUFFER_LOAD_FORMAT_D16,
524 BUFFER_STORE_FORMAT_D16,
535 BUFFER_ATOMIC_CMPSWAP,
537 BUFFER_ATOMIC_PK_FADD,
541 LAST_AMDGPU_ISD_NUMBER
545 } // End namespace AMDGPUISD
547 } // End namespace llvm