Make several cleanups to Andrews varargs change:
[oota-llvm.git] / include / llvm / Target / TargetLowering.h
1 //===-- llvm/Target/TargetLowering.h - Target Lowering Info -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes how to lower LLVM code to machine code.  This has two
11 // main components:
12 //
13 //  1. Which ValueTypes are natively supported by the target.
14 //  2. Which operations are supported for supported ValueTypes.
15 //
16 // In addition it has a few other components, like information about FP
17 // immediates.
18 //
19 //===----------------------------------------------------------------------===//
20
21 #ifndef LLVM_TARGET_TARGETLOWERING_H
22 #define LLVM_TARGET_TARGETLOWERING_H
23
24 #include "llvm/Type.h"
25 #include "llvm/CodeGen/ValueTypes.h"
26 #include <vector>
27
28 namespace llvm {
29   class Value;
30   class Function;
31   class TargetMachine;
32   class TargetData;
33   class TargetRegisterClass;
34   class SDNode;
35   class SDOperand;
36   class SelectionDAG;
37
38 //===----------------------------------------------------------------------===//
39 /// TargetLowering - This class defines information used to lower LLVM code to
40 /// legal SelectionDAG operators that the target instruction selector can accept
41 /// natively.
42 ///
43 /// This class also defines callbacks that targets must implement to lower
44 /// target-specific constructs to SelectionDAG operators.
45 ///
46 class TargetLowering {
47 public:
48   /// LegalizeAction - This enum indicates whether operations are valid for a
49   /// target, and if not, what action should be used to make them valid.
50   enum LegalizeAction {
51     Legal,      // The target natively supports this operation.
52     Promote,    // This operation should be executed in a larger type.
53     Expand,     // Try to expand this to other ops, otherwise use a libcall.
54     Custom,     // Use the LowerOperation hook to implement custom lowering.
55   };
56
57   enum OutOfRangeShiftAmount {
58     Undefined,  // Oversized shift amounts are undefined (default).
59     Mask,       // Shift amounts are auto masked (anded) to value size.
60     Extend,     // Oversized shift pulls in zeros or sign bits.
61   };
62
63   enum SetCCResultValue {
64     UndefinedSetCCResult,          // SetCC returns a garbage/unknown extend.
65     ZeroOrOneSetCCResult,          // SetCC returns a zero extended result.
66     ZeroOrNegativeOneSetCCResult,  // SetCC returns a sign extended result.
67   };
68
69   TargetLowering(TargetMachine &TM);
70   virtual ~TargetLowering();
71
72   TargetMachine &getTargetMachine() const { return TM; }
73   const TargetData &getTargetData() const { return TD; }
74
75   bool isLittleEndian() const { return IsLittleEndian; }
76   MVT::ValueType getPointerTy() const { return PointerTy; }
77   MVT::ValueType getShiftAmountTy() const { return ShiftAmountTy; }
78   OutOfRangeShiftAmount getShiftAmountFlavor() const {return ShiftAmtHandling; }
79
80   /// isSetCCExpensive - Return true if the setcc operation is expensive for
81   /// this target.
82   bool isSetCCExpensive() const { return SetCCIsExpensive; }
83
84   /// getSetCCResultTy - Return the ValueType of the result of setcc operations.
85   ///
86   MVT::ValueType getSetCCResultTy() const { return SetCCResultTy; }
87
88   /// getSetCCResultContents - For targets without boolean registers, this flag
89   /// returns information about the contents of the high-bits in the setcc
90   /// result register.
91   SetCCResultValue getSetCCResultContents() const { return SetCCResultContents;}
92
93   /// getRegClassFor - Return the register class that should be used for the
94   /// specified value type.  This may only be called on legal types.
95   TargetRegisterClass *getRegClassFor(MVT::ValueType VT) const {
96     TargetRegisterClass *RC = RegClassForVT[VT];
97     assert(RC && "This value type is not natively supported!");
98     return RC;
99   }
100
101   /// hasNativeSupportFor - Return true if the target has native support for the
102   /// specified value type.  This means that it has a register that directly
103   /// holds it without promotions or expansions.
104   bool hasNativeSupportFor(MVT::ValueType VT) const {
105     return RegClassForVT[VT] != 0;
106   }
107
108   /// getTypeAction - Return how we should legalize values of this type, either
109   /// it is already legal (return 'Legal') or we need to promote it to a larger
110   /// type (return 'Promote'), or we need to expand it into multiple registers
111   /// of smaller integer type (return 'Expand').  'Custom' is not an option.
112   LegalizeAction getTypeAction(MVT::ValueType VT) const {
113     return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
114   }
115   unsigned getValueTypeActions() const { return ValueTypeActions; }
116
117   /// getTypeToTransformTo - For types supported by the target, this is an
118   /// identity function.  For types that must be promoted to larger types, this
119   /// returns the larger type to promote to.  For types that are larger than the
120   /// largest integer register, this contains one step in the expansion to get
121   /// to the smaller register.
122   MVT::ValueType getTypeToTransformTo(MVT::ValueType VT) const {
123     return TransformToType[VT];
124   }
125
126   typedef std::vector<double>::const_iterator legal_fpimm_iterator;
127   legal_fpimm_iterator legal_fpimm_begin() const {
128     return LegalFPImmediates.begin();
129   }
130   legal_fpimm_iterator legal_fpimm_end() const {
131     return LegalFPImmediates.end();
132   }
133
134   /// getOperationAction - Return how this operation should be
135   LegalizeAction getOperationAction(unsigned Op, MVT::ValueType VT) const {
136     return (LegalizeAction)((OpActions[Op] >> (2*VT)) & 3);
137   }
138
139   /// hasNativeSupportForOperation - Return true if this operation is legal for
140   /// this type.
141   ///
142   bool hasNativeSupportForOperation(unsigned Op, MVT::ValueType VT) const {
143     return getOperationAction(Op, VT) == Legal;
144   }
145
146   /// getTypeToPromoteTo - If the action for this operation is to promote, this
147   /// method returns the ValueType to promote to.
148   MVT::ValueType getTypeToPromoteTo(unsigned Op, MVT::ValueType VT) const {
149     assert(getOperationAction(Op, VT) == Promote &&
150            "This operation isn't promoted!");
151     MVT::ValueType NVT = VT;
152     do {
153       NVT = (MVT::ValueType)(NVT+1);
154       assert(MVT::isInteger(NVT) == MVT::isInteger(VT) && NVT != MVT::isVoid &&
155              "Didn't find type to promote to!");
156     } while (!hasNativeSupportFor(NVT) ||
157              getOperationAction(Op, NVT) == Promote);
158     return NVT;
159   }
160
161   /// getValueType - Return the MVT::ValueType corresponding to this LLVM type.
162   /// This is fixed by the LLVM operations except for the pointer size.
163   MVT::ValueType getValueType(const Type *Ty) const {
164     switch (Ty->getTypeID()) {
165     default: assert(0 && "Unknown type!");
166     case Type::VoidTyID:    return MVT::isVoid;
167     case Type::BoolTyID:    return MVT::i1;
168     case Type::UByteTyID:
169     case Type::SByteTyID:   return MVT::i8;
170     case Type::ShortTyID:
171     case Type::UShortTyID:  return MVT::i16;
172     case Type::IntTyID:
173     case Type::UIntTyID:    return MVT::i32;
174     case Type::LongTyID:
175     case Type::ULongTyID:   return MVT::i64;
176     case Type::FloatTyID:   return MVT::f32;
177     case Type::DoubleTyID:  return MVT::f64;
178     case Type::PointerTyID: return PointerTy;
179     }
180   }
181
182   /// getNumElements - Return the number of registers that this ValueType will
183   /// eventually require.  This is always one for all non-integer types, is
184   /// one for any types promoted to live in larger registers, but may be more
185   /// than one for types (like i64) that are split into pieces.
186   unsigned getNumElements(MVT::ValueType VT) const {
187     return NumElementsForVT[VT];
188   }
189
190   //===--------------------------------------------------------------------===//
191   // TargetLowering Configuration Methods - These methods should be invoked by
192   // the derived class constructor to configure this object for the target.
193   //
194
195 protected:
196
197   /// setShiftAmountType - Describe the type that should be used for shift
198   /// amounts.  This type defaults to the pointer type.
199   void setShiftAmountType(MVT::ValueType VT) { ShiftAmountTy = VT; }
200
201   /// setSetCCResultType - Describe the type that shoudl be used as the result
202   /// of a setcc operation.  This defaults to the pointer type.
203   void setSetCCResultType(MVT::ValueType VT) { SetCCResultTy = VT; }
204
205   /// setSetCCResultContents - Specify how the target extends the result of a
206   /// setcc operation in a register.
207   void setSetCCResultContents(SetCCResultValue Ty) { SetCCResultContents = Ty; }
208
209   /// setShiftAmountFlavor - Describe how the target handles out of range shift
210   /// amounts.
211   void setShiftAmountFlavor(OutOfRangeShiftAmount OORSA) {
212     ShiftAmtHandling = OORSA;
213   }
214
215   /// setSetCCIxExpensive - This is a short term hack for targets that codegen
216   /// setcc as a conditional branch.  This encourages the code generator to fold
217   /// setcc operations into other operations if possible.
218   void setSetCCIsExpensive() { SetCCIsExpensive = true; }
219
220   /// addRegisterClass - Add the specified register class as an available
221   /// regclass for the specified value type.  This indicates the selector can
222   /// handle values of that class natively.
223   void addRegisterClass(MVT::ValueType VT, TargetRegisterClass *RC) {
224     AvailableRegClasses.push_back(std::make_pair(VT, RC));
225     RegClassForVT[VT] = RC;
226   }
227
228   /// computeRegisterProperties - Once all of the register classes are added,
229   /// this allows us to compute derived properties we expose.
230   void computeRegisterProperties();
231
232   /// setOperationAction - Indicate that the specified operation does not work
233   /// with the specified type and indicate what to do about it.
234   void setOperationAction(unsigned Op, MVT::ValueType VT,
235                           LegalizeAction Action) {
236     assert(VT < 16 && Op < sizeof(OpActions)/sizeof(OpActions[0]) &&
237            "Table isn't big enough!");
238     OpActions[Op] |= Action << VT*2;
239   }
240
241   /// addLegalFPImmediate - Indicate that this target can instruction select
242   /// the specified FP immediate natively.
243   void addLegalFPImmediate(double Imm) {
244     LegalFPImmediates.push_back(Imm);
245   }
246
247 public:
248
249   //===--------------------------------------------------------------------===//
250   // Lowering methods - These methods must be implemented by targets so that
251   // the SelectionDAGLowering code knows how to lower these.
252   //
253
254   /// LowerArguments - This hook must be implemented to indicate how we should
255   /// lower the arguments for the specified function, into the specified DAG.
256   virtual std::vector<SDOperand>
257   LowerArguments(Function &F, SelectionDAG &DAG) = 0;
258
259   /// LowerCallTo - This hook lowers an abstract call to a function into an
260   /// actual call.  This returns a pair of operands.  The first element is the
261   /// return value for the function (if RetTy is not VoidTy).  The second
262   /// element is the outgoing token chain.
263   typedef std::vector<std::pair<SDOperand, const Type*> > ArgListTy;
264   virtual std::pair<SDOperand, SDOperand>
265   LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
266               unsigned CallingConv, bool isTailCall, SDOperand Callee,
267               ArgListTy &Args, SelectionDAG &DAG) = 0;
268
269   /// LowerVAStart - This lowers the llvm.va_start intrinsic.  If not
270   /// implemented, this method prints a message and aborts.  This method should
271   /// return the modified chain value.  Note that VAListPtr* correspond to the
272   /// llvm.va_start operand.
273   virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
274                                  Value *VAListV, SelectionDAG &DAG);
275
276   /// LowerVAEnd - This lowers llvm.va_end and returns the resultant chain.  If
277   /// not implemented, this defaults to a noop.
278   virtual SDOperand LowerVAEnd(SDOperand Chain, SDOperand LP, Value *LV,
279                                SelectionDAG &DAG);
280
281   /// LowerVACopy - This lowers llvm.va_copy and returns the resultant chain.
282   /// If not implemented, this defaults to loading a pointer from the input and
283   /// storing it to the output.
284   virtual SDOperand LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV,
285                                 SDOperand DestP, Value *DestV,
286                                 SelectionDAG &DAG);
287
288   /// LowerVAArg - This lowers the vaarg instruction.  If not implemented, this
289   /// prints a message and aborts.
290   virtual std::pair<SDOperand,SDOperand>
291   LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
292              const Type *ArgTy, SelectionDAG &DAG);
293
294   /// LowerFrameReturnAddress - This hook lowers a call to llvm.returnaddress or
295   /// llvm.frameaddress (depending on the value of the first argument).  The
296   /// return values are the result pointer and the resultant token chain.  If
297   /// not implemented, both of these intrinsics will return null.
298   virtual std::pair<SDOperand, SDOperand>
299   LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
300                           SelectionDAG &DAG);
301
302   /// LowerOperation - For operations that are unsupported by the target, and
303   /// which are registered to use 'custom' lowering.  This callback is invoked.
304   /// If the target has no operations that require custom lowering, it need not
305   /// implement this.  The default implementation of this aborts.
306   virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
307
308
309 private:
310   TargetMachine &TM;
311   const TargetData &TD;
312
313   /// IsLittleEndian - True if this is a little endian target.
314   ///
315   bool IsLittleEndian;
316
317   /// PointerTy - The type to use for pointers, usually i32 or i64.
318   ///
319   MVT::ValueType PointerTy;
320
321   /// ShiftAmountTy - The type to use for shift amounts, usually i8 or whatever
322   /// PointerTy is.
323   MVT::ValueType ShiftAmountTy;
324
325   OutOfRangeShiftAmount ShiftAmtHandling;
326
327   /// SetCCIsExpensive - This is a short term hack for targets that codegen
328   /// setcc as a conditional branch.  This encourages the code generator to fold
329   /// setcc operations into other operations if possible.
330   bool SetCCIsExpensive;
331
332   /// SetCCResultTy - The type that SetCC operations use.  This defaults to the
333   /// PointerTy.
334   MVT::ValueType SetCCResultTy;
335
336   /// SetCCResultContents - Information about the contents of the high-bits in
337   /// the result of a setcc comparison operation.
338   SetCCResultValue SetCCResultContents;
339
340   /// RegClassForVT - This indicates the default register class to use for
341   /// each ValueType the target supports natively.
342   TargetRegisterClass *RegClassForVT[MVT::LAST_VALUETYPE];
343   unsigned char NumElementsForVT[MVT::LAST_VALUETYPE];
344
345   /// ValueTypeActions - This is a bitvector that contains two bits for each
346   /// value type, where the two bits correspond to the LegalizeAction enum.
347   /// This can be queried with "getTypeAction(VT)".
348   unsigned ValueTypeActions;
349
350   /// TransformToType - For any value types we are promoting or expanding, this
351   /// contains the value type that we are changing to.  For Expanded types, this
352   /// contains one step of the expand (e.g. i64 -> i32), even if there are
353   /// multiple steps required (e.g. i64 -> i16).  For types natively supported
354   /// by the system, this holds the same type (e.g. i32 -> i32).
355   MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
356
357   /// OpActions - For each operation and each value type, keep a LegalizeAction
358   /// that indicates how instruction selection should deal with the operation.
359   /// Most operations are Legal (aka, supported natively by the target), but
360   /// operations that are not should be described.  Note that operations on
361   /// non-legal value types are not described here.
362   unsigned OpActions[128];
363
364   std::vector<double> LegalFPImmediates;
365
366   std::vector<std::pair<MVT::ValueType,
367                         TargetRegisterClass*> > AvailableRegClasses;
368 };
369 } // end llvm namespace
370
371 #endif