1 //===-- AMDGPUISelLowering.h - AMDGPU Lowering Interface --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
11 /// \brief Interface definition of the TargetLowering class that is common
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H
17 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/CodeGen/TargetLowering.h"
25 class AMDGPUMachineFunction;
26 class AMDGPUSubtarget;
29 class AMDGPUTargetLowering : public TargetLowering {
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);
42 const AMDGPUSubtarget *Subtarget;
45 SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const;
46 SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const;
47 /// \brief 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 LowerFROUND32_16(SDValue Op, SelectionDAG &DAG) const;
57 SDValue LowerFROUND64(SDValue Op, SelectionDAG &DAG) const;
58 SDValue LowerFROUND(SDValue Op, SelectionDAG &DAG) const;
59 SDValue LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const;
60 SDValue LowerFLOG(SDValue Op, SelectionDAG &Dag,
61 double Log2BaseInverted) const;
63 SDValue LowerCTLZ_CTTZ(SDValue Op, SelectionDAG &DAG) const;
65 SDValue LowerINT_TO_FP32(SDValue Op, SelectionDAG &DAG, bool Signed) const;
66 SDValue LowerINT_TO_FP64(SDValue Op, SelectionDAG &DAG, bool Signed) const;
67 SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
68 SDValue LowerSINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
70 SDValue LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG, bool Signed) const;
71 SDValue LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const;
72 SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const;
73 SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const;
75 SDValue LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const;
78 bool shouldCombineMemoryType(EVT VT) const;
79 SDValue performLoadCombine(SDNode *N, DAGCombinerInfo &DCI) const;
80 SDValue performStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const;
81 SDValue performClampCombine(SDNode *N, DAGCombinerInfo &DCI) const;
82 SDValue performAssertSZExtCombine(SDNode *N, DAGCombinerInfo &DCI) const;
84 SDValue splitBinaryBitConstantOpImpl(DAGCombinerInfo &DCI, const SDLoc &SL,
85 unsigned Opc, SDValue LHS,
86 uint32_t ValLo, uint32_t ValHi) const;
87 SDValue performShlCombine(SDNode *N, DAGCombinerInfo &DCI) const;
88 SDValue performSraCombine(SDNode *N, DAGCombinerInfo &DCI) const;
89 SDValue performSrlCombine(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;
97 SDValue performFNegCombine(SDNode *N, DAGCombinerInfo &DCI) const;
98 SDValue performFAbsCombine(SDNode *N, DAGCombinerInfo &DCI) const;
100 static EVT getEquivalentMemType(LLVMContext &Context, EVT VT);
102 virtual SDValue LowerGlobalAddress(AMDGPUMachineFunction *MFI, SDValue Op,
103 SelectionDAG &DAG) const;
105 /// Return 64-bit value Op as two 32-bit integers.
106 std::pair<SDValue, SDValue> split64BitValue(SDValue Op,
107 SelectionDAG &DAG) const;
108 SDValue getLoHalf64(SDValue Op, SelectionDAG &DAG) const;
109 SDValue getHiHalf64(SDValue Op, SelectionDAG &DAG) const;
111 /// \brief Split a vector load into 2 loads of half the vector.
112 SDValue SplitVectorLoad(SDValue Op, SelectionDAG &DAG) const;
114 /// \brief Split a vector store into 2 stores of half the vector.
115 SDValue SplitVectorStore(SDValue Op, SelectionDAG &DAG) const;
117 SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const;
118 SDValue LowerSDIVREM(SDValue Op, SelectionDAG &DAG) const;
119 SDValue LowerUDIVREM(SDValue Op, SelectionDAG &DAG) const;
120 SDValue LowerDIVREM24(SDValue Op, SelectionDAG &DAG, bool sign) const;
121 void LowerUDIVREM64(SDValue Op, SelectionDAG &DAG,
122 SmallVectorImpl<SDValue> &Results) const;
123 void analyzeFormalArgumentsCompute(CCState &State,
124 const SmallVectorImpl<ISD::InputArg> &Ins) const;
126 AMDGPUTargetLowering(const TargetMachine &TM, const AMDGPUSubtarget &STI);
128 bool mayIgnoreSignedZero(SDValue Op) const {
129 if (getTargetMachine().Options.NoSignedZerosFPMath)
132 const auto Flags = Op.getNode()->getFlags();
133 if (Flags.isDefined())
134 return Flags.hasNoSignedZeros();
139 static bool allUsesHaveSourceMods(const SDNode *N,
140 unsigned CostThreshold = 4);
141 bool isFAbsFree(EVT VT) const override;
142 bool isFNegFree(EVT VT) const override;
143 bool isTruncateFree(EVT Src, EVT Dest) const override;
144 bool isTruncateFree(Type *Src, Type *Dest) const override;
146 bool isZExtFree(Type *Src, Type *Dest) const override;
147 bool isZExtFree(EVT Src, EVT Dest) const override;
148 bool isZExtFree(SDValue Val, EVT VT2) const override;
149 bool isFPExtFoldable(unsigned Opcode, EVT DestVT, EVT SrcVT) const override;
151 bool isNarrowingProfitable(EVT VT1, EVT VT2) const override;
153 MVT getVectorIdxTy(const DataLayout &) const override;
154 bool isSelectSupported(SelectSupportKind) const override;
156 bool isFPImmLegal(const APFloat &Imm, EVT VT) const override;
157 bool ShouldShrinkFPConstant(EVT VT) const override;
158 bool shouldReduceLoadWidth(SDNode *Load,
159 ISD::LoadExtType ExtType,
160 EVT ExtVT) const override;
162 bool isLoadBitCastBeneficial(EVT, EVT) const final;
164 bool storeOfVectorConstantIsCheap(EVT MemVT,
166 unsigned AS) const override;
167 bool aggressivelyPreferBuildVectorSources(EVT VecVT) const override;
168 bool isCheapToSpeculateCttz() const override;
169 bool isCheapToSpeculateCtlz() const override;
171 static CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg);
172 static CCAssignFn *CCAssignFnForReturn(CallingConv::ID CC, bool IsVarArg);
174 SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
175 const SmallVectorImpl<ISD::OutputArg> &Outs,
176 const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
177 SelectionDAG &DAG) const override;
179 SDValue addTokenForArgument(SDValue Chain,
181 MachineFrameInfo &MFI,
182 int ClobberedFI) const;
184 SDValue lowerUnhandledCall(CallLoweringInfo &CLI,
185 SmallVectorImpl<SDValue> &InVals,
186 StringRef Reason) const;
187 SDValue LowerCall(CallLoweringInfo &CLI,
188 SmallVectorImpl<SDValue> &InVals) const override;
190 SDValue LowerDYNAMIC_STACKALLOC(SDValue Op,
191 SelectionDAG &DAG) const;
193 SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
194 SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
195 void ReplaceNodeResults(SDNode * N,
196 SmallVectorImpl<SDValue> &Results,
197 SelectionDAG &DAG) const override;
199 SDValue combineFMinMaxLegacy(const SDLoc &DL, EVT VT, SDValue LHS,
200 SDValue RHS, SDValue True, SDValue False,
201 SDValue CC, DAGCombinerInfo &DCI) const;
203 const char* getTargetNodeName(unsigned Opcode) const override;
205 // FIXME: Turn off MergeConsecutiveStores() before Instruction Selection
207 // A commit ( git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@319036
208 // 91177308-0d34-0410-b5e6-96231b3b80d8 ) turned on
209 // MergeConsecutiveStores() before Instruction Selection for all targets.
210 // Enough AMDGPU compiles go into an infinite loop ( MergeConsecutiveStores()
211 // merges two stores; LegalizeStoreOps() un-merges; MergeConsecutiveStores()
212 // re-merges, etc. ) to warrant turning it off for now.
213 bool mergeStoresAfterLegalization() const override { return false; }
215 bool isFsqrtCheap(SDValue Operand, SelectionDAG &DAG) const override {
218 SDValue getSqrtEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled,
219 int &RefinementSteps, bool &UseOneConstNR,
220 bool Reciprocal) const override;
221 SDValue getRecipEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled,
222 int &RefinementSteps) const override;
224 virtual SDNode *PostISelFolding(MachineSDNode *N,
225 SelectionDAG &DAG) const = 0;
227 /// \brief Determine which of the bits specified in \p Mask are known to be
228 /// either zero or one and return them in the \p KnownZero and \p KnownOne
230 void computeKnownBitsForTargetNode(const SDValue Op,
232 const APInt &DemandedElts,
233 const SelectionDAG &DAG,
234 unsigned Depth = 0) const override;
236 unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts,
237 const SelectionDAG &DAG,
238 unsigned Depth = 0) const override;
240 /// \brief Helper function that adds Reg to the LiveIn list of the DAG's
243 /// \returns a RegisterSDNode representing Reg if \p RawReg is true, otherwise
244 /// a copy from the register.
245 SDValue CreateLiveInRegister(SelectionDAG &DAG,
246 const TargetRegisterClass *RC,
247 unsigned Reg, EVT VT,
249 bool RawReg = false) const;
250 SDValue CreateLiveInRegister(SelectionDAG &DAG,
251 const TargetRegisterClass *RC,
252 unsigned Reg, EVT VT) const {
253 return CreateLiveInRegister(DAG, RC, Reg, VT, SDLoc(DAG.getEntryNode()));
256 // Returns the raw live in register rather than a copy from it.
257 SDValue CreateLiveInRegisterRaw(SelectionDAG &DAG,
258 const TargetRegisterClass *RC,
259 unsigned Reg, EVT VT) const {
260 return CreateLiveInRegister(DAG, RC, Reg, VT, SDLoc(DAG.getEntryNode()), true);
263 /// Similar to CreateLiveInRegister, except value maybe loaded from a stack
264 /// slot rather than passed in a register.
265 SDValue loadStackInputValue(SelectionDAG &DAG,
268 int64_t Offset) const;
270 SDValue storeStackInputValue(SelectionDAG &DAG,
275 int64_t Offset) const;
277 SDValue loadInputValue(SelectionDAG &DAG,
278 const TargetRegisterClass *RC,
279 EVT VT, const SDLoc &SL,
280 const ArgDescriptor &Arg) const;
282 enum ImplicitParameter {
284 GRID_DIM = FIRST_IMPLICIT,
288 /// \brief Helper function that returns the byte offset of the given
289 /// type of implicit parameter.
290 uint32_t getImplicitParameterOffset(const AMDGPUMachineFunction *MFI,
291 const ImplicitParameter Param) const;
293 AMDGPUAS getAMDGPUAS() const {
297 MVT getFenceOperandTy(const DataLayout &DL) const override {
302 namespace AMDGPUISD {
304 enum NodeType : unsigned {
306 FIRST_NUMBER = ISD::BUILTIN_OP_END,
307 UMUL, // 32bit unsigned multiplication
309 // End AMDIL ISD Opcodes
316 // Masked control flow nodes.
321 // A uniform kernel return that terminates the wavefront.
324 // Return to a shader part's epilog code.
327 // Return with values from a non-entry function.
333 /// CLAMP value between 0.0 and 1.0. NaN clamped to 0, following clamp output
334 /// modifier behavior with dx10_enable.
337 // This is SETCC with the full mask result which is used for a compare with a
338 // result bit per item in the wavefront.
341 // FP ops with input and output chain.
345 // SIN_HW, COS_HW - f32 for SI, 1 ULP max error, valid from -100 pi to 100 pi.
346 // Denormals handled on some parts.
364 // For emitting ISD::FMAD when f32 denormals are enabled because mac/mad is
365 // treated as an illegal operation.
367 TRIG_PREOP, // 1 ULP max error for f64
369 // RCP, RSQ - For f32, 1 ULP max error, no denormal handling.
370 // For f64, max error 2^29 ULP, handles denormals.
382 BFE_U32, // Extract range of bits with zero extension to 32-bits.
383 BFE_I32, // Extract range of bits with sign extension to 32-bits.
384 BFI, // (src0 & src1) | (~src0 & src2)
385 BFM, // Insert a range of bits into a 32-bit word.
386 FFBH_U32, // ctlz with -1 if input is zero.
388 FFBL_B32, // cttz with -1 if input is zero.
400 EXPORT, // exp on SI+
401 EXPORT_DONE, // exp on SI+ with done bit set
411 // These cvt_f32_ubyte* nodes need to remain consecutive and in order.
417 // Convert two float 32 numbers into a single register holding two packed f16
418 // with round to zero.
421 // Same as the standard node, except the high bits of the resulting integer
425 // Wrapper around fp16 results that are known to zero the high bits.
428 /// This node is for VLIW targets and it is used to represent a vector
429 /// that is stored in consecutive registers with the same channel.
436 BUILD_VERTICAL_VECTOR,
437 /// Pointer to the start of the shader's constant data.
440 INIT_EXEC_FROM_INPUT,
449 FIRST_MEM_OPCODE_NUMBER = ISD::FIRST_TARGET_MEMORY_OPCODE,
452 TBUFFER_STORE_FORMAT,
453 TBUFFER_STORE_FORMAT_X3,
472 BUFFER_ATOMIC_CMPSWAP,
473 LAST_AMDGPU_ISD_NUMBER
477 } // End namespace AMDGPUISD
479 } // End namespace llvm