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