1 //===-- llvm/Target/TargetLowering.h - Target Lowering Info -----*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file describes how to lower LLVM code to machine code. This has two
13 // 1. Which ValueTypes are natively supported by the target.
14 // 2. Which operations are supported for supported ValueTypes.
16 // In addition it has a few other components, like information about FP
19 //===----------------------------------------------------------------------===//
21 #ifndef LLVM_TARGET_TARGETLOWERING_H
22 #define LLVM_TARGET_TARGETLOWERING_H
24 #include "llvm/Type.h"
25 #include "llvm/CodeGen/ValueTypes.h"
32 class TargetRegisterClass;
37 //===----------------------------------------------------------------------===//
38 /// TargetLowering - This class defines information used to lower LLVM code to
39 /// legal SelectionDAG operators that the target instruction selector can accept
42 /// This class also defines callbacks that targets must implement to lower
43 /// target-specific constructs to SelectionDAG operators.
45 class TargetLowering {
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.
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.
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.
62 TargetLowering(TargetMachine &TM);
63 virtual ~TargetLowering();
65 TargetMachine &getTargetMachine() const { return TM; }
66 const TargetData &getTargetData() const { return TD; }
68 bool isLittleEndian() const { return IsLittleEndian; }
69 MVT::ValueType getPointerTy() const { return PointerTy; }
70 MVT::ValueType getShiftAmountTy() const { return ShiftAmountTy; }
71 MVT::ValueType getSetCCResultTy() const { return SetCCResultTy; }
72 OutOfRangeShiftAmount getShiftAmountFlavor() const {return ShiftAmtHandling; }
74 /// getRegClassFor - Return the register class that should be used for the
75 /// specified value type. This may only be called on legal types.
76 TargetRegisterClass *getRegClassFor(MVT::ValueType VT) const {
77 TargetRegisterClass *RC = RegClassForVT[VT];
78 assert(RC && "This value type is not natively supported!");
82 /// hasNativeSupportFor - Return true if the target has native support for the
83 /// specified value type. This means that it has a register that directly
84 /// holds it without promotions or expansions.
85 bool hasNativeSupportFor(MVT::ValueType VT) const {
86 return RegClassForVT[VT] != 0;
89 /// getTypeAction - Return how we should legalize values of this type, either
90 /// it is already legal (return 'Legal') or we need to promote it to a larger
91 /// type (return 'Promote'), or we need to expand it into multiple registers
92 /// of smaller integer type (return 'Expand'). 'Custom' is not an option.
93 LegalizeAction getTypeAction(MVT::ValueType VT) const {
94 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
96 unsigned getValueTypeActions() const { return ValueTypeActions; }
98 /// getTypeToTransformTo - For types supported by the target, this is an
99 /// identity function. For types that must be promoted to larger types, this
100 /// returns the larger type to promote to. For types that are larger than the
101 /// largest integer register, this contains one step in the expansion to get
102 /// to the smaller register.
103 MVT::ValueType getTypeToTransformTo(MVT::ValueType VT) const {
104 return TransformToType[VT];
107 typedef std::vector<double>::const_iterator legal_fpimm_iterator;
108 legal_fpimm_iterator legal_fpimm_begin() const {
109 return LegalFPImmediates.begin();
111 legal_fpimm_iterator legal_fpimm_end() const {
112 return LegalFPImmediates.end();
115 /// getOperationAction - Return how this operation should be
116 LegalizeAction getOperationAction(unsigned Op, MVT::ValueType VT) const {
117 return (LegalizeAction)((OpActions[Op] >> (2*VT)) & 3);
120 /// hasNativeSupportForOperation - Return true if this operation is legal for
123 bool hasNativeSupportForOperation(unsigned Op, MVT::ValueType VT) const {
124 return getOperationAction(Op, VT) == Legal;
127 /// getTypeToPromoteTo - If the action for this operation is to promote, this
128 /// method returns the ValueType to promote to.
129 MVT::ValueType getTypeToPromoteTo(unsigned Op, MVT::ValueType VT) const {
130 assert(getOperationAction(Op, VT) == Promote &&
131 "This operation isn't promoted!");
132 MVT::ValueType NVT = VT;
134 NVT = (MVT::ValueType)(NVT+1);
135 assert(MVT::isInteger(NVT) == MVT::isInteger(VT) && NVT != MVT::isVoid &&
136 "Didn't find type to promote to!");
137 } while (!hasNativeSupportFor(NVT) ||
138 getOperationAction(Op, NVT) == Promote);
142 /// getValueType - Return the MVT::ValueType corresponding to this LLVM type.
143 /// This is fixed by the LLVM operations except for the pointer size.
144 MVT::ValueType getValueType(const Type *Ty) const {
145 switch (Ty->getTypeID()) {
146 default: assert(0 && "Unknown type!");
147 case Type::VoidTyID: return MVT::isVoid;
148 case Type::BoolTyID: return MVT::i1;
149 case Type::UByteTyID:
150 case Type::SByteTyID: return MVT::i8;
151 case Type::ShortTyID:
152 case Type::UShortTyID: return MVT::i16;
154 case Type::UIntTyID: return MVT::i32;
156 case Type::ULongTyID: return MVT::i64;
157 case Type::FloatTyID: return MVT::f32;
158 case Type::DoubleTyID: return MVT::f64;
159 case Type::PointerTyID: return PointerTy;
163 /// getNumElements - Return the number of registers that this ValueType will
164 /// eventually require. This is always one for all non-integer types, is
165 /// one for any types promoted to live in larger registers, but may be more
166 /// than one for types (like i64) that are split into pieces.
167 unsigned getNumElements(MVT::ValueType VT) const {
168 return NumElementsForVT[VT];
171 //===--------------------------------------------------------------------===//
172 // TargetLowering Configuration Methods - These methods should be invoked by
173 // the derived class constructor to configure this object for the target.
178 /// setShiftAmountType - Describe the type that should be used for shift
179 /// amounts. This type defaults to the pointer type.
180 void setShiftAmountType(MVT::ValueType VT) { ShiftAmountTy = VT; }
182 /// setSetCCResultType - Describe the type that shoudl be used as the result
183 /// of a setcc operation. This defaults to the pointer type.
184 void setSetCCResultType(MVT::ValueType VT) { SetCCResultTy = VT; }
186 /// setShiftAmountFlavor - Describe how the target handles out of range shift
188 void setShiftAmountFlavor(OutOfRangeShiftAmount OORSA) {
189 ShiftAmtHandling = OORSA;
192 /// addRegisterClass - Add the specified register class as an available
193 /// regclass for the specified value type. This indicates the selector can
194 /// handle values of that class natively.
195 void addRegisterClass(MVT::ValueType VT, TargetRegisterClass *RC) {
196 AvailableRegClasses.push_back(std::make_pair(VT, RC));
197 RegClassForVT[VT] = RC;
200 /// computeRegisterProperties - Once all of the register classes are added,
201 /// this allows us to compute derived properties we expose.
202 void computeRegisterProperties();
204 /// setOperationAction - Indicate that the specified operation does not work
205 /// with the specified type and indicate what to do about it.
206 void setOperationAction(unsigned Op, MVT::ValueType VT,
207 LegalizeAction Action) {
208 assert(VT < 16 && Op < sizeof(OpActions)/sizeof(OpActions[0]) &&
209 "Table isn't big enough!");
210 OpActions[Op] |= Action << VT*2;
213 /// addLegalFPImmediate - Indicate that this target can instruction select
214 /// the specified FP immediate natively.
215 void addLegalFPImmediate(double Imm) {
216 LegalFPImmediates.push_back(Imm);
221 //===--------------------------------------------------------------------===//
222 // Lowering methods - These methods must be implemented by targets so that
223 // the SelectionDAGLowering code knows how to lower these.
226 /// LowerArguments - This hook must be implemented to indicate how we should
227 /// lower the arguments for the specified function, into the specified DAG.
228 virtual std::vector<SDOperand>
229 LowerArguments(Function &F, SelectionDAG &DAG) = 0;
231 /// LowerCallTo - This hook lowers an abstract call to a function into an
232 /// actual call. This returns a pair of operands. The first element is the
233 /// return value for the function (if RetTy is not VoidTy). The second
234 /// element is the outgoing token chain.
235 typedef std::vector<std::pair<SDOperand, const Type*> > ArgListTy;
236 virtual std::pair<SDOperand, SDOperand>
237 LowerCallTo(SDOperand Chain, const Type *RetTy, SDOperand Callee,
238 ArgListTy &Args, SelectionDAG &DAG) = 0;
241 /// LowerVAStart - This lowers the llvm.va_start intrinsic. If not
242 /// implemented, this method prints a message and aborts.
243 virtual std::pair<SDOperand, SDOperand>
244 LowerVAStart(SDOperand Chain, SelectionDAG &DAG);
246 /// LowerVAEnd - This lowers llvm.va_end and returns the resultant chain. If
247 /// not implemented, this defaults to a noop.
248 virtual SDOperand LowerVAEnd(SDOperand Chain, SDOperand L, SelectionDAG &DAG);
250 /// LowerVACopy - This lowers llvm.va_copy and returns the resultant
251 /// value/chain pair. If not implemented, this defaults to returning the
253 virtual std::pair<SDOperand,SDOperand>
254 LowerVACopy(SDOperand Chain, SDOperand L, SelectionDAG &DAG);
256 /// LowerVAArgNext - This lowers the vaarg and vanext instructions (depending
257 /// on whether the first argument is true). If not implemented, this prints a
258 /// message and aborts.
259 virtual std::pair<SDOperand,SDOperand>
260 LowerVAArgNext(bool isVANext, SDOperand Chain, SDOperand VAList,
261 const Type *ArgTy, SelectionDAG &DAG);
263 /// LowerFrameReturnAddress - This hook lowers a call to llvm.returnaddress or
264 /// llvm.frameaddress (depending on the value of the first argument). The
265 /// return values are the result pointer and the resultant token chain. If
266 /// not implemented, both of these intrinsics will return null.
267 virtual std::pair<SDOperand, SDOperand>
268 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
271 /// LowerOperation - For operations that are unsupported by the target, and
272 /// which are registered to use 'custom' lowering. This callback is invoked.
273 /// If the target has no operations that require custom lowering, it need not
274 /// implement this. The default implementation of this aborts.
275 virtual SDOperand LowerOperation(SDOperand Op);
280 const TargetData &TD;
282 /// IsLittleEndian - True if this is a little endian target.
286 /// PointerTy - The type to use for pointers, usually i32 or i64.
288 MVT::ValueType PointerTy;
290 /// ShiftAmountTy - The type to use for shift amounts, usually i8 or whatever
292 MVT::ValueType ShiftAmountTy;
294 OutOfRangeShiftAmount ShiftAmtHandling;
296 /// SetCCResultTy - The type that SetCC operations use. This defaults to the
298 MVT::ValueType SetCCResultTy;
300 /// RegClassForVT - This indicates the default register class to use for
301 /// each ValueType the target supports natively.
302 TargetRegisterClass *RegClassForVT[MVT::LAST_VALUETYPE];
303 unsigned char NumElementsForVT[MVT::LAST_VALUETYPE];
305 /// ValueTypeActions - This is a bitvector that contains two bits for each
306 /// value type, where the two bits correspond to the LegalizeAction enum.
307 /// This can be queried with "getTypeAction(VT)".
308 unsigned ValueTypeActions;
310 /// TransformToType - For any value types we are promoting or expanding, this
311 /// contains the value type that we are changing to. For Expanded types, this
312 /// contains one step of the expand (e.g. i64 -> i32), even if there are
313 /// multiple steps required (e.g. i64 -> i16). For types natively supported
314 /// by the system, this holds the same type (e.g. i32 -> i32).
315 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
317 /// OpActions - For each operation and each value type, keep a LegalizeAction
318 /// that indicates how instruction selection should deal with the operation.
319 /// Most operations are Legal (aka, supported natively by the target), but
320 /// operations that are not should be described. Note that operations on
321 /// non-legal value types are not described here.
322 unsigned OpActions[128];
324 std::vector<double> LegalFPImmediates;
326 std::vector<std::pair<MVT::ValueType,
327 TargetRegisterClass*> > AvailableRegClasses;
329 } // end llvm namespace