1 //===-- AMDGPUISelLowering.h - AMDGPU Lowering Interface --------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 /// \file
11 /// \brief Interface definition of the TargetLowering class that is common
12 /// to all AMD GPUs.
13 //
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H
17 #define LLVM_LIB_TARGET_AMDGPU_AMDGPUISELLOWERING_H
18 
19 #include "AMDGPU.h"
20 #include "llvm/CodeGen/CallingConvLower.h"
21 #include "llvm/Target/TargetLowering.h"
22 
23 namespace llvm {
24 
25 class AMDGPUMachineFunction;
26 class AMDGPUSubtarget;
27 struct ArgDescriptor;
28 
29 class AMDGPUTargetLowering : public TargetLowering {
30 private:
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
34   /// compare.
35   SDValue getFFBX_U32(SelectionDAG &DAG, SDValue Op, const SDLoc &DL, unsigned Opc) const;
36 
37 public:
38   static bool isOrEquivalentToAdd(SelectionDAG &DAG, SDValue Op);
39 
40 protected:
41   const AMDGPUSubtarget *Subtarget;
42   AMDGPUAS AMDGPUASI;
43 
44   SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const;
45   SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const;
46   /// \brief Split a vector store into multiple scalar stores.
47   /// \returns The resulting chain.
48 
49   SDValue LowerFREM(SDValue Op, SelectionDAG &DAG) const;
50   SDValue LowerFCEIL(SDValue Op, SelectionDAG &DAG) const;
51   SDValue LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const;
52   SDValue LowerFRINT(SDValue Op, SelectionDAG &DAG) const;
53   SDValue LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const;
54 
55   SDValue LowerFROUND32_16(SDValue Op, SelectionDAG &DAG) const;
56   SDValue LowerFROUND64(SDValue Op, SelectionDAG &DAG) const;
57   SDValue LowerFROUND(SDValue Op, SelectionDAG &DAG) const;
58   SDValue LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const;
59 
60   SDValue LowerCTLZ_CTTZ(SDValue Op, SelectionDAG &DAG) const;
61 
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;
66 
67   SDValue LowerFP64_TO_INT(SDValue Op, SelectionDAG &DAG, bool Signed) const;
68   SDValue LowerFP_TO_FP16(SDValue Op, SelectionDAG &DAG) const;
69   SDValue LowerFP_TO_UINT(SDValue Op, SelectionDAG &DAG) const;
70   SDValue LowerFP_TO_SINT(SDValue Op, SelectionDAG &DAG) const;
71 
72   SDValue LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const;
73 
74 protected:
75   bool shouldCombineMemoryType(EVT VT) const;
76   SDValue performLoadCombine(SDNode *N, DAGCombinerInfo &DCI) const;
77   SDValue performStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const;
78   SDValue performClampCombine(SDNode *N, DAGCombinerInfo &DCI) const;
79   SDValue performAssertSZExtCombine(SDNode *N, DAGCombinerInfo &DCI) const;
80 
81   SDValue splitBinaryBitConstantOpImpl(DAGCombinerInfo &DCI, const SDLoc &SL,
82                                        unsigned Opc, SDValue LHS,
83                                        uint32_t ValLo, uint32_t ValHi) const;
84   SDValue performShlCombine(SDNode *N, DAGCombinerInfo &DCI) const;
85   SDValue performSraCombine(SDNode *N, DAGCombinerInfo &DCI) const;
86   SDValue performSrlCombine(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 performMulLoHi24Combine(SDNode *N, DAGCombinerInfo &DCI) const;
91   SDValue performCtlz_CttzCombine(const SDLoc &SL, SDValue Cond, SDValue LHS,
92                              SDValue RHS, DAGCombinerInfo &DCI) const;
93   SDValue performSelectCombine(SDNode *N, DAGCombinerInfo &DCI) const;
94   SDValue performFNegCombine(SDNode *N, DAGCombinerInfo &DCI) const;
95   SDValue performFAbsCombine(SDNode *N, DAGCombinerInfo &DCI) const;
96 
97   static EVT getEquivalentMemType(LLVMContext &Context, EVT VT);
98 
99   virtual SDValue LowerGlobalAddress(AMDGPUMachineFunction *MFI, SDValue Op,
100                                      SelectionDAG &DAG) const;
101 
102   /// Return 64-bit value Op as two 32-bit integers.
103   std::pair<SDValue, SDValue> split64BitValue(SDValue Op,
104                                               SelectionDAG &DAG) const;
105   SDValue getLoHalf64(SDValue Op, SelectionDAG &DAG) const;
106   SDValue getHiHalf64(SDValue Op, SelectionDAG &DAG) const;
107 
108   /// \brief Split a vector load into 2 loads of half the vector.
109   SDValue SplitVectorLoad(SDValue Op, SelectionDAG &DAG) const;
110 
111   /// \brief Split a vector store into 2 stores of half the vector.
112   SDValue SplitVectorStore(SDValue Op, SelectionDAG &DAG) const;
113 
114   SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const;
115   SDValue LowerSDIVREM(SDValue Op, SelectionDAG &DAG) const;
116   SDValue LowerUDIVREM(SDValue Op, SelectionDAG &DAG) const;
117   SDValue LowerDIVREM24(SDValue Op, SelectionDAG &DAG, bool sign) const;
118   void LowerUDIVREM64(SDValue Op, SelectionDAG &DAG,
119                                     SmallVectorImpl<SDValue> &Results) const;
120   void analyzeFormalArgumentsCompute(CCState &State,
121                               const SmallVectorImpl<ISD::InputArg> &Ins) const;
122 public:
123   AMDGPUTargetLowering(const TargetMachine &TM, const AMDGPUSubtarget &STI);
124 
125   bool mayIgnoreSignedZero(SDValue Op) const {
126     if (getTargetMachine().Options.NoSignedZerosFPMath)
127       return true;
128 
129     const auto Flags = Op.getNode()->getFlags();
130     if (Flags.isDefined())
131       return Flags.hasNoSignedZeros();
132 
133     return false;
134   }
135 
136   static bool allUsesHaveSourceMods(const SDNode *N,
137                                     unsigned CostThreshold = 4);
138   bool isFAbsFree(EVT VT) const override;
139   bool isFNegFree(EVT VT) const override;
140   bool isTruncateFree(EVT Src, EVT Dest) const override;
141   bool isTruncateFree(Type *Src, Type *Dest) const override;
142 
143   bool isZExtFree(Type *Src, Type *Dest) const override;
144   bool isZExtFree(EVT Src, EVT Dest) const override;
145   bool isZExtFree(SDValue Val, EVT VT2) const override;
146   bool isFPExtFoldable(unsigned Opcode, EVT DestVT, EVT SrcVT) const override;
147 
148   bool isNarrowingProfitable(EVT VT1, EVT VT2) const override;
149 
150   MVT getVectorIdxTy(const DataLayout &) const override;
151   bool isSelectSupported(SelectSupportKind) const override;
152 
153   bool isFPImmLegal(const APFloat &Imm, EVT VT) const override;
154   bool ShouldShrinkFPConstant(EVT VT) const override;
155   bool shouldReduceLoadWidth(SDNode *Load,
156                              ISD::LoadExtType ExtType,
157                              EVT ExtVT) const override;
158 
159   bool isLoadBitCastBeneficial(EVT, EVT) const final;
160 
161   bool storeOfVectorConstantIsCheap(EVT MemVT,
162                                     unsigned NumElem,
163                                     unsigned AS) const override;
164   bool aggressivelyPreferBuildVectorSources(EVT VecVT) const override;
165   bool isCheapToSpeculateCttz() const override;
166   bool isCheapToSpeculateCtlz() const override;
167 
168   static CCAssignFn *CCAssignFnForCall(CallingConv::ID CC, bool IsVarArg);
169   static CCAssignFn *CCAssignFnForReturn(CallingConv::ID CC, bool IsVarArg);
170 
171   SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv, bool isVarArg,
172                       const SmallVectorImpl<ISD::OutputArg> &Outs,
173                       const SmallVectorImpl<SDValue> &OutVals, const SDLoc &DL,
174                       SelectionDAG &DAG) const override;
175 
176   SDValue addTokenForArgument(SDValue Chain,
177                               SelectionDAG &DAG,
178                               MachineFrameInfo &MFI,
179                               int ClobberedFI) const;
180 
181   SDValue lowerUnhandledCall(CallLoweringInfo &CLI,
182                              SmallVectorImpl<SDValue> &InVals,
183                              StringRef Reason) const;
184   SDValue LowerCall(CallLoweringInfo &CLI,
185                     SmallVectorImpl<SDValue> &InVals) const override;
186 
187   SDValue LowerDYNAMIC_STACKALLOC(SDValue Op,
188                                   SelectionDAG &DAG) const;
189 
190   SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
191   SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
192   void ReplaceNodeResults(SDNode * N,
193                           SmallVectorImpl<SDValue> &Results,
194                           SelectionDAG &DAG) const override;
195 
196   SDValue combineFMinMaxLegacy(const SDLoc &DL, EVT VT, SDValue LHS,
197                                SDValue RHS, SDValue True, SDValue False,
198                                SDValue CC, DAGCombinerInfo &DCI) const;
199 
200   const char* getTargetNodeName(unsigned Opcode) const override;
201 
202   bool isFsqrtCheap(SDValue Operand, SelectionDAG &DAG) const override {
203     return true;
204   }
205   SDValue getSqrtEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled,
206                            int &RefinementSteps, bool &UseOneConstNR,
207                            bool Reciprocal) const override;
208   SDValue getRecipEstimate(SDValue Operand, SelectionDAG &DAG, int Enabled,
209                            int &RefinementSteps) const override;
210 
211   virtual SDNode *PostISelFolding(MachineSDNode *N,
212                                   SelectionDAG &DAG) const = 0;
213 
214   /// \brief Determine which of the bits specified in \p Mask are known to be
215   /// either zero or one and return them in the \p KnownZero and \p KnownOne
216   /// bitsets.
217   void computeKnownBitsForTargetNode(const SDValue Op,
218                                      KnownBits &Known,
219                                      const APInt &DemandedElts,
220                                      const SelectionDAG &DAG,
221                                      unsigned Depth = 0) const override;
222 
223   unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const APInt &DemandedElts,
224                                            const SelectionDAG &DAG,
225                                            unsigned Depth = 0) const override;
226 
227   /// \brief Helper function that adds Reg to the LiveIn list of the DAG's
228   /// MachineFunction.
229   ///
230   /// \returns a RegisterSDNode representing Reg if \p RawReg is true, otherwise
231   /// a copy from the register.
232   SDValue CreateLiveInRegister(SelectionDAG &DAG,
233                                const TargetRegisterClass *RC,
234                                unsigned Reg, EVT VT,
235                                const SDLoc &SL,
236                                bool RawReg = false) const;
237   SDValue CreateLiveInRegister(SelectionDAG &DAG,
238                                const TargetRegisterClass *RC,
239                                unsigned Reg, EVT VT) const {
240     return CreateLiveInRegister(DAG, RC, Reg, VT, SDLoc(DAG.getEntryNode()));
241   }
242 
243   // Returns the raw live in register rather than a copy from it.
244   SDValue CreateLiveInRegisterRaw(SelectionDAG &DAG,
245                                   const TargetRegisterClass *RC,
246                                   unsigned Reg, EVT VT) const {
247     return CreateLiveInRegister(DAG, RC, Reg, VT, SDLoc(DAG.getEntryNode()), true);
248   }
249 
250   /// Similar to CreateLiveInRegister, except value maybe loaded from a stack
251   /// slot rather than passed in a register.
252   SDValue loadStackInputValue(SelectionDAG &DAG,
253                               EVT VT,
254                               const SDLoc &SL,
255                               int64_t Offset) const;
256 
257   SDValue storeStackInputValue(SelectionDAG &DAG,
258                                const SDLoc &SL,
259                                SDValue Chain,
260                                SDValue StackPtr,
261                                SDValue ArgVal,
262                                int64_t Offset) const;
263 
264   SDValue loadInputValue(SelectionDAG &DAG,
265                          const TargetRegisterClass *RC,
266                          EVT VT, const SDLoc &SL,
267                          const ArgDescriptor &Arg) const;
268 
269   enum ImplicitParameter {
270     FIRST_IMPLICIT,
271     GRID_DIM = FIRST_IMPLICIT,
272     GRID_OFFSET,
273   };
274 
275   /// \brief Helper function that returns the byte offset of the given
276   /// type of implicit parameter.
277   uint32_t getImplicitParameterOffset(const AMDGPUMachineFunction *MFI,
278                                       const ImplicitParameter Param) const;
279 
280   AMDGPUAS getAMDGPUAS() const {
281     return AMDGPUASI;
282   }
283 
284   MVT getFenceOperandTy(const DataLayout &DL) const override {
285     return MVT::i32;
286   }
287 };
288 
289 namespace AMDGPUISD {
290 
291 enum NodeType : unsigned {
292   // AMDIL ISD Opcodes
293   FIRST_NUMBER = ISD::BUILTIN_OP_END,
294   UMUL,        // 32bit unsigned multiplication
295   BRANCH_COND,
296   // End AMDIL ISD Opcodes
297 
298   // Function call.
299   CALL,
300   TC_RETURN,
301   TRAP,
302 
303   // Masked control flow nodes.
304   IF,
305   ELSE,
306   LOOP,
307 
308   // A uniform kernel return that terminates the wavefront.
309   ENDPGM,
310 
311   // Return to a shader part's epilog code.
312   RETURN_TO_EPILOG,
313 
314   // Return with values from a non-entry function.
315   RET_FLAG,
316 
317   DWORDADDR,
318   FRACT,
319 
320   /// CLAMP value between 0.0 and 1.0. NaN clamped to 0, following clamp output
321   /// modifier behavior with dx10_enable.
322   CLAMP,
323 
324   // This is SETCC with the full mask result which is used for a compare with a
325   // result bit per item in the wavefront.
326   SETCC,
327   SETREG,
328   // FP ops with input and output chain.
329   FMA_W_CHAIN,
330   FMUL_W_CHAIN,
331 
332   // SIN_HW, COS_HW - f32 for SI, 1 ULP max error, valid from -100 pi to 100 pi.
333   // Denormals handled on some parts.
334   COS_HW,
335   SIN_HW,
336   FMAX_LEGACY,
337   FMIN_LEGACY,
338   FMAX3,
339   SMAX3,
340   UMAX3,
341   FMIN3,
342   SMIN3,
343   UMIN3,
344   FMED3,
345   SMED3,
346   UMED3,
347   URECIP,
348   DIV_SCALE,
349   DIV_FMAS,
350   DIV_FIXUP,
351   // For emitting ISD::FMAD when f32 denormals are enabled because mac/mad is
352   // treated as an illegal operation.
353   FMAD_FTZ,
354   TRIG_PREOP, // 1 ULP max error for f64
355 
356   // RCP, RSQ - For f32, 1 ULP max error, no denormal handling.
357   //            For f64, max error 2^29 ULP, handles denormals.
358   RCP,
359   RSQ,
360   RCP_LEGACY,
361   RSQ_LEGACY,
362   FMUL_LEGACY,
363   RSQ_CLAMP,
364   LDEXP,
365   FP_CLASS,
366   DOT4,
367   CARRY,
368   BORROW,
369   BFE_U32, // Extract range of bits with zero extension to 32-bits.
370   BFE_I32, // Extract range of bits with sign extension to 32-bits.
371   BFI, // (src0 & src1) | (~src0 & src2)
372   BFM, // Insert a range of bits into a 32-bit word.
373   FFBH_U32, // ctlz with -1 if input is zero.
374   FFBH_I32,
375   FFBL_B32, // cttz with -1 if input is zero.
376   MUL_U24,
377   MUL_I24,
378   MULHI_U24,
379   MULHI_I24,
380   MAD_U24,
381   MAD_I24,
382   MUL_LOHI_I24,
383   MUL_LOHI_U24,
384   TEXTURE_FETCH,
385   EXPORT, // exp on SI+
386   EXPORT_DONE, // exp on SI+ with done bit set
387   R600_EXPORT,
388   CONST_ADDRESS,
389   REGISTER_LOAD,
390   REGISTER_STORE,
391   SAMPLE,
392   SAMPLEB,
393   SAMPLED,
394   SAMPLEL,
395 
396   // These cvt_f32_ubyte* nodes need to remain consecutive and in order.
397   CVT_F32_UBYTE0,
398   CVT_F32_UBYTE1,
399   CVT_F32_UBYTE2,
400   CVT_F32_UBYTE3,
401 
402   // Convert two float 32 numbers into a single register holding two packed f16
403   // with round to zero.
404   CVT_PKRTZ_F16_F32,
405 
406   // Same as the standard node, except the high bits of the resulting integer
407   // are known 0.
408   FP_TO_FP16,
409 
410   // Wrapper around fp16 results that are known to zero the high bits.
411   FP16_ZEXT,
412 
413   /// This node is for VLIW targets and it is used to represent a vector
414   /// that is stored in consecutive registers with the same channel.
415   /// For example:
416   ///   |X  |Y|Z|W|
417   /// T0|v.x| | | |
418   /// T1|v.y| | | |
419   /// T2|v.z| | | |
420   /// T3|v.w| | | |
421   BUILD_VERTICAL_VECTOR,
422   /// Pointer to the start of the shader's constant data.
423   CONST_DATA_PTR,
424   INIT_EXEC,
425   INIT_EXEC_FROM_INPUT,
426   SENDMSG,
427   SENDMSGHALT,
428   INTERP_MOV,
429   INTERP_P1,
430   INTERP_P2,
431   PC_ADD_REL_OFFSET,
432   KILL,
433   DUMMY_CHAIN,
434   FIRST_MEM_OPCODE_NUMBER = ISD::FIRST_TARGET_MEMORY_OPCODE,
435   STORE_MSKOR,
436   LOAD_CONSTANT,
437   TBUFFER_STORE_FORMAT,
438   TBUFFER_STORE_FORMAT_X3,
439   TBUFFER_LOAD_FORMAT,
440   ATOMIC_CMP_SWAP,
441   ATOMIC_INC,
442   ATOMIC_DEC,
443   BUFFER_LOAD,
444   BUFFER_LOAD_FORMAT,
445   LAST_AMDGPU_ISD_NUMBER
446 };
447 
448 
449 } // End namespace AMDGPUISD
450 
451 } // End namespace llvm
452 
453 #endif
454