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"
33 class TargetRegisterClass;
38 //===----------------------------------------------------------------------===//
39 /// TargetLowering - This class defines information used to lower LLVM code to
40 /// legal SelectionDAG operators that the target instruction selector can accept
43 /// This class also defines callbacks that targets must implement to lower
44 /// target-specific constructs to SelectionDAG operators.
46 class TargetLowering {
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.
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.
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.
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.
69 TargetLowering(TargetMachine &TM);
70 virtual ~TargetLowering();
72 TargetMachine &getTargetMachine() const { return TM; }
73 const TargetData &getTargetData() const { return TD; }
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; }
80 /// isSetCCExpensive - Return true if the setcc operation is expensive for
82 bool isSetCCExpensive() const { return SetCCIsExpensive; }
84 /// getSetCCResultTy - Return the ValueType of the result of setcc operations.
86 MVT::ValueType getSetCCResultTy() const { return SetCCResultTy; }
88 /// getSetCCResultContents - For targets without boolean registers, this flag
89 /// returns information about the contents of the high-bits in the setcc
91 SetCCResultValue getSetCCResultContents() const { return SetCCResultContents;}
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!");
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;
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);
115 unsigned getValueTypeActions() const { return ValueTypeActions; }
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];
126 typedef std::vector<double>::const_iterator legal_fpimm_iterator;
127 legal_fpimm_iterator legal_fpimm_begin() const {
128 return LegalFPImmediates.begin();
130 legal_fpimm_iterator legal_fpimm_end() const {
131 return LegalFPImmediates.end();
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);
139 /// hasNativeSupportForOperation - Return true if this operation is legal for
142 bool hasNativeSupportForOperation(unsigned Op, MVT::ValueType VT) const {
143 return getOperationAction(Op, VT) == Legal;
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;
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);
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;
173 case Type::UIntTyID: return MVT::i32;
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;
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];
190 //===--------------------------------------------------------------------===//
191 // TargetLowering Configuration Methods - These methods should be invoked by
192 // the derived class constructor to configure this object for the target.
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; }
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; }
205 /// setSetCCResultContents - Specify how the target extends the result of a
206 /// setcc operation in a register.
207 void setSetCCResultContents(SetCCResultValue Ty) { SetCCResultContents = Ty; }
209 /// setShiftAmountFlavor - Describe how the target handles out of range shift
211 void setShiftAmountFlavor(OutOfRangeShiftAmount OORSA) {
212 ShiftAmtHandling = OORSA;
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; }
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;
228 /// computeRegisterProperties - Once all of the register classes are added,
229 /// this allows us to compute derived properties we expose.
230 void computeRegisterProperties();
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;
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);
249 //===--------------------------------------------------------------------===//
250 // Lowering methods - These methods must be implemented by targets so that
251 // the SelectionDAGLowering code knows how to lower these.
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;
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;
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);
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,
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,
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);
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,
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);
311 const TargetData &TD;
313 /// IsLittleEndian - True if this is a little endian target.
317 /// PointerTy - The type to use for pointers, usually i32 or i64.
319 MVT::ValueType PointerTy;
321 /// ShiftAmountTy - The type to use for shift amounts, usually i8 or whatever
323 MVT::ValueType ShiftAmountTy;
325 OutOfRangeShiftAmount ShiftAmtHandling;
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;
332 /// SetCCResultTy - The type that SetCC operations use. This defaults to the
334 MVT::ValueType SetCCResultTy;
336 /// SetCCResultContents - Information about the contents of the high-bits in
337 /// the result of a setcc comparison operation.
338 SetCCResultValue SetCCResultContents;
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];
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;
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];
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];
364 std::vector<double> LegalFPImmediates;
366 std::vector<std::pair<MVT::ValueType,
367 TargetRegisterClass*> > AvailableRegClasses;
369 } // end llvm namespace