R600: Custom lower frem
[oota-llvm.git] / lib / Target / R600 / AMDGPUISelLowering.h
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_R600_AMDGPUISELLOWERING_H
17 #define LLVM_LIB_TARGET_R600_AMDGPUISELLOWERING_H
18
19 #include "llvm/Target/TargetLowering.h"
20
21 namespace llvm {
22
23 class AMDGPUMachineFunction;
24 class AMDGPUSubtarget;
25 class MachineRegisterInfo;
26
27 class AMDGPUTargetLowering : public TargetLowering {
28 protected:
29   const AMDGPUSubtarget *Subtarget;
30
31 private:
32   SDValue LowerConstantInitializer(const Constant* Init, const GlobalValue *GV,
33                                    const SDValue &InitPtr,
34                                    SDValue Chain,
35                                    SelectionDAG &DAG) const;
36   SDValue LowerFrameIndex(SDValue Op, SelectionDAG &DAG) const;
37   SDValue LowerEXTRACT_SUBVECTOR(SDValue Op, SelectionDAG &DAG) const;
38   SDValue LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const;
39   SDValue LowerINTRINSIC_WO_CHAIN(SDValue Op, SelectionDAG &DAG) const;
40   /// \brief Lower vector stores by merging the vector elements into an integer
41   /// of the same bitwidth.
42   SDValue MergeVectorStore(const SDValue &Op, SelectionDAG &DAG) const;
43   /// \brief Split a vector store into multiple scalar stores.
44   /// \returns The resulting chain.
45
46   SDValue LowerUDIVREM(SDValue Op, SelectionDAG &DAG) const;
47   SDValue LowerFREM(SDValue Op, SelectionDAG &DAG) const;
48   SDValue LowerFCEIL(SDValue Op, SelectionDAG &DAG) const;
49   SDValue LowerFTRUNC(SDValue Op, SelectionDAG &DAG) const;
50   SDValue LowerFRINT(SDValue Op, SelectionDAG &DAG) const;
51   SDValue LowerFNEARBYINT(SDValue Op, SelectionDAG &DAG) const;
52   SDValue LowerFFLOOR(SDValue Op, SelectionDAG &DAG) const;
53
54   SDValue LowerUINT_TO_FP(SDValue Op, SelectionDAG &DAG) const;
55
56   SDValue ExpandSIGN_EXTEND_INREG(SDValue Op,
57                                   unsigned BitsDiff,
58                                   SelectionDAG &DAG) const;
59   SDValue LowerSIGN_EXTEND_INREG(SDValue Op, SelectionDAG &DAG) const;
60
61   SDValue performStoreCombine(SDNode *N, DAGCombinerInfo &DCI) const;
62   SDValue performMulCombine(SDNode *N, DAGCombinerInfo &DCI) const;
63
64 protected:
65   static EVT getEquivalentMemType(LLVMContext &Context, EVT VT);
66   static EVT getEquivalentLoadRegType(LLVMContext &Context, EVT VT);
67
68   virtual SDValue LowerGlobalAddress(AMDGPUMachineFunction *MFI, SDValue Op,
69                                      SelectionDAG &DAG) const;
70
71   /// \brief Split a vector load into a scalar load of each component.
72   SDValue ScalarizeVectorLoad(SDValue Op, SelectionDAG &DAG) const;
73
74   /// \brief Split a vector load into 2 loads of half the vector.
75   SDValue SplitVectorLoad(SDValue Op, SelectionDAG &DAG) const;
76
77   /// \brief Split a vector store into a scalar store of each component.
78   SDValue ScalarizeVectorStore(SDValue Op, SelectionDAG &DAG) const;
79
80   /// \brief Split a vector store into 2 stores of half the vector.
81   SDValue SplitVectorStore(SDValue Op, SelectionDAG &DAG) const;
82
83   SDValue LowerLOAD(SDValue Op, SelectionDAG &DAG) const;
84   SDValue LowerSTORE(SDValue Op, SelectionDAG &DAG) const;
85   SDValue LowerSDIVREM(SDValue Op, SelectionDAG &DAG) const;
86   SDValue LowerDIVREM24(SDValue Op, SelectionDAG &DAG, bool sign) const;
87   bool isHWTrueValue(SDValue Op) const;
88   bool isHWFalseValue(SDValue Op) const;
89
90   /// The SelectionDAGBuilder will automatically promote function arguments
91   /// with illegal types.  However, this does not work for the AMDGPU targets
92   /// since the function arguments are stored in memory as these illegal types.
93   /// In order to handle this properly we need to get the origianl types sizes
94   /// from the LLVM IR Function and fixup the ISD:InputArg values before
95   /// passing them to AnalyzeFormalArguments()
96   void getOriginalFunctionArgs(SelectionDAG &DAG,
97                                const Function *F,
98                                const SmallVectorImpl<ISD::InputArg> &Ins,
99                                SmallVectorImpl<ISD::InputArg> &OrigIns) const;
100   void AnalyzeFormalArguments(CCState &State,
101                               const SmallVectorImpl<ISD::InputArg> &Ins) const;
102
103 public:
104   AMDGPUTargetLowering(TargetMachine &TM);
105
106   bool isFAbsFree(EVT VT) const override;
107   bool isFNegFree(EVT VT) const override;
108   bool isTruncateFree(EVT Src, EVT Dest) const override;
109   bool isTruncateFree(Type *Src, Type *Dest) const override;
110
111   bool isZExtFree(Type *Src, Type *Dest) const override;
112   bool isZExtFree(EVT Src, EVT Dest) const override;
113   bool isZExtFree(SDValue Val, EVT VT2) const override;
114
115   bool isNarrowingProfitable(EVT VT1, EVT VT2) const override;
116
117   MVT getVectorIdxTy() const override;
118   bool isSelectSupported(SelectSupportKind) const override;
119
120   bool isFPImmLegal(const APFloat &Imm, EVT VT) const override;
121   bool ShouldShrinkFPConstant(EVT VT) const override;
122
123   bool isLoadBitCastBeneficial(EVT, EVT) const override;
124   SDValue LowerReturn(SDValue Chain, CallingConv::ID CallConv,
125                       bool isVarArg,
126                       const SmallVectorImpl<ISD::OutputArg> &Outs,
127                       const SmallVectorImpl<SDValue> &OutVals,
128                       SDLoc DL, SelectionDAG &DAG) const override;
129   SDValue LowerCall(CallLoweringInfo &CLI,
130                     SmallVectorImpl<SDValue> &InVals) const override;
131
132   SDValue LowerOperation(SDValue Op, SelectionDAG &DAG) const override;
133   SDValue PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const override;
134   void ReplaceNodeResults(SDNode * N,
135                           SmallVectorImpl<SDValue> &Results,
136                           SelectionDAG &DAG) const override;
137
138   SDValue LowerIntrinsicIABS(SDValue Op, SelectionDAG &DAG) const;
139   SDValue LowerIntrinsicLRP(SDValue Op, SelectionDAG &DAG) const;
140   SDValue CombineMinMax(SDNode *N, SelectionDAG &DAG) const;
141   const char* getTargetNodeName(unsigned Opcode) const override;
142
143   virtual SDNode *PostISelFolding(MachineSDNode *N,
144                                   SelectionDAG &DAG) const {
145     return N;
146   }
147
148   /// \brief Determine which of the bits specified in \p Mask are known to be
149   /// either zero or one and return them in the \p KnownZero and \p KnownOne
150   /// bitsets.
151   void computeKnownBitsForTargetNode(const SDValue Op,
152                                      APInt &KnownZero,
153                                      APInt &KnownOne,
154                                      const SelectionDAG &DAG,
155                                      unsigned Depth = 0) const override;
156
157   unsigned ComputeNumSignBitsForTargetNode(SDValue Op, const SelectionDAG &DAG,
158                                            unsigned Depth = 0) const override;
159
160   /// \brief Helper function that adds Reg to the LiveIn list of the DAG's
161   /// MachineFunction.
162   ///
163   /// \returns a RegisterSDNode representing Reg.
164   virtual SDValue CreateLiveInRegister(SelectionDAG &DAG,
165                                        const TargetRegisterClass *RC,
166                                        unsigned Reg, EVT VT) const;
167 };
168
169 namespace AMDGPUISD {
170
171 enum {
172   // AMDIL ISD Opcodes
173   FIRST_NUMBER = ISD::BUILTIN_OP_END,
174   CALL,        // Function call based on a single integer
175   UMUL,        // 32bit unsigned multiplication
176   RET_FLAG,
177   BRANCH_COND,
178   // End AMDIL ISD Opcodes
179   DWORDADDR,
180   FRACT,
181   CLAMP,
182   MAD, // Multiply + add with same result as the separate operations.
183
184   // SIN_HW, COS_HW - f32 for SI, 1 ULP max error, valid from -100 pi to 100 pi.
185   // Denormals handled on some parts.
186   COS_HW,
187   SIN_HW,
188   FMAX,
189   SMAX,
190   UMAX,
191   FMIN,
192   SMIN,
193   UMIN,
194   URECIP,
195   DIV_SCALE,
196   DIV_FMAS,
197   DIV_FIXUP,
198   TRIG_PREOP, // 1 ULP max error for f64
199
200   // RCP, RSQ - For f32, 1 ULP max error, no denormal handling.
201   //            For f64, max error 2^29 ULP, handles denormals.
202   RCP,
203   RSQ,
204   RSQ_LEGACY,
205   RSQ_CLAMPED,
206   LDEXP,
207   DOT4,
208   BFE_U32, // Extract range of bits with zero extension to 32-bits.
209   BFE_I32, // Extract range of bits with sign extension to 32-bits.
210   BFI, // (src0 & src1) | (~src0 & src2)
211   BFM, // Insert a range of bits into a 32-bit word.
212   BREV, // Reverse bits.
213   MUL_U24,
214   MUL_I24,
215   MAD_U24,
216   MAD_I24,
217   TEXTURE_FETCH,
218   EXPORT,
219   CONST_ADDRESS,
220   REGISTER_LOAD,
221   REGISTER_STORE,
222   LOAD_INPUT,
223   SAMPLE,
224   SAMPLEB,
225   SAMPLED,
226   SAMPLEL,
227
228   // These cvt_f32_ubyte* nodes need to remain consecutive and in order.
229   CVT_F32_UBYTE0,
230   CVT_F32_UBYTE1,
231   CVT_F32_UBYTE2,
232   CVT_F32_UBYTE3,
233   /// This node is for VLIW targets and it is used to represent a vector
234   /// that is stored in consecutive registers with the same channel.
235   /// For example:
236   ///   |X  |Y|Z|W|
237   /// T0|v.x| | | |
238   /// T1|v.y| | | |
239   /// T2|v.z| | | |
240   /// T3|v.w| | | |
241   BUILD_VERTICAL_VECTOR,
242   /// Pointer to the start of the shader's constant data.
243   CONST_DATA_PTR,
244   FIRST_MEM_OPCODE_NUMBER = ISD::FIRST_TARGET_MEMORY_OPCODE,
245   STORE_MSKOR,
246   LOAD_CONSTANT,
247   TBUFFER_STORE_FORMAT,
248   LAST_AMDGPU_ISD_NUMBER
249 };
250
251
252 } // End namespace AMDGPUISD
253
254 } // End namespace llvm
255
256 #endif