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.
15 // 3. Cost thresholds for alternative implementations of certain operations.
17 // In addition it has a few other components, like information about FP
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_TARGET_TARGETLOWERING_H
23 #define LLVM_TARGET_TARGETLOWERING_H
25 #include "llvm/Type.h"
26 #include "llvm/CodeGen/ValueTypes.h"
34 class TargetRegisterClass;
39 //===----------------------------------------------------------------------===//
40 /// TargetLowering - This class defines information used to lower LLVM code to
41 /// legal SelectionDAG operators that the target instruction selector can accept
44 /// This class also defines callbacks that targets must implement to lower
45 /// target-specific constructs to SelectionDAG operators.
47 class TargetLowering {
49 /// LegalizeAction - This enum indicates whether operations are valid for a
50 /// target, and if not, what action should be used to make them valid.
52 Legal, // The target natively supports this operation.
53 Promote, // This operation should be executed in a larger type.
54 Expand, // Try to expand this to other ops, otherwise use a libcall.
55 Custom, // Use the LowerOperation hook to implement custom lowering.
58 enum OutOfRangeShiftAmount {
59 Undefined, // Oversized shift amounts are undefined (default).
60 Mask, // Shift amounts are auto masked (anded) to value size.
61 Extend, // Oversized shift pulls in zeros or sign bits.
64 enum SetCCResultValue {
65 UndefinedSetCCResult, // SetCC returns a garbage/unknown extend.
66 ZeroOrOneSetCCResult, // SetCC returns a zero extended result.
67 ZeroOrNegativeOneSetCCResult, // SetCC returns a sign extended result.
70 TargetLowering(TargetMachine &TM);
71 virtual ~TargetLowering();
73 TargetMachine &getTargetMachine() const { return TM; }
74 const TargetData &getTargetData() const { return TD; }
76 bool isLittleEndian() const { return IsLittleEndian; }
77 MVT::ValueType getPointerTy() const { return PointerTy; }
78 MVT::ValueType getShiftAmountTy() const { return ShiftAmountTy; }
79 OutOfRangeShiftAmount getShiftAmountFlavor() const {return ShiftAmtHandling; }
81 /// isSetCCExpensive - Return true if the setcc operation is expensive for
83 bool isSetCCExpensive() const { return SetCCIsExpensive; }
85 /// getSetCCResultTy - Return the ValueType of the result of setcc operations.
87 MVT::ValueType getSetCCResultTy() const { return SetCCResultTy; }
89 /// getSetCCResultContents - For targets without boolean registers, this flag
90 /// returns information about the contents of the high-bits in the setcc
92 SetCCResultValue getSetCCResultContents() const { return SetCCResultContents;}
94 /// getRegClassFor - Return the register class that should be used for the
95 /// specified value type. This may only be called on legal types.
96 TargetRegisterClass *getRegClassFor(MVT::ValueType VT) const {
97 TargetRegisterClass *RC = RegClassForVT[VT];
98 assert(RC && "This value type is not natively supported!");
102 /// hasNativeSupportFor - Return true if the target has native support for the
103 /// specified value type. This means that it has a register that directly
104 /// holds it without promotions or expansions.
105 bool hasNativeSupportFor(MVT::ValueType VT) const {
106 return RegClassForVT[VT] != 0;
109 /// getTypeAction - Return how we should legalize values of this type, either
110 /// it is already legal (return 'Legal') or we need to promote it to a larger
111 /// type (return 'Promote'), or we need to expand it into multiple registers
112 /// of smaller integer type (return 'Expand'). 'Custom' is not an option.
113 LegalizeAction getTypeAction(MVT::ValueType VT) const {
114 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
116 unsigned getValueTypeActions() const { return ValueTypeActions; }
118 /// getTypeToTransformTo - For types supported by the target, this is an
119 /// identity function. For types that must be promoted to larger types, this
120 /// returns the larger type to promote to. For types that are larger than the
121 /// largest integer register, this contains one step in the expansion to get
122 /// to the smaller register.
123 MVT::ValueType getTypeToTransformTo(MVT::ValueType VT) const {
124 return TransformToType[VT];
127 typedef std::vector<double>::const_iterator legal_fpimm_iterator;
128 legal_fpimm_iterator legal_fpimm_begin() const {
129 return LegalFPImmediates.begin();
131 legal_fpimm_iterator legal_fpimm_end() const {
132 return LegalFPImmediates.end();
135 /// getOperationAction - Return how this operation should be
136 LegalizeAction getOperationAction(unsigned Op, MVT::ValueType VT) const {
137 return (LegalizeAction)((OpActions[Op] >> (2*VT)) & 3);
140 /// hasNativeSupportForOperation - Return true if this operation is legal for
143 bool hasNativeSupportForOperation(unsigned Op, MVT::ValueType VT) const {
144 return getOperationAction(Op, VT) == Legal;
147 /// getTypeToPromoteTo - If the action for this operation is to promote, this
148 /// method returns the ValueType to promote to.
149 MVT::ValueType getTypeToPromoteTo(unsigned Op, MVT::ValueType VT) const {
150 assert(getOperationAction(Op, VT) == Promote &&
151 "This operation isn't promoted!");
152 MVT::ValueType NVT = VT;
154 NVT = (MVT::ValueType)(NVT+1);
155 assert(MVT::isInteger(NVT) == MVT::isInteger(VT) && NVT != MVT::isVoid &&
156 "Didn't find type to promote to!");
157 } while (!hasNativeSupportFor(NVT) ||
158 getOperationAction(Op, NVT) == Promote);
162 /// getValueType - Return the MVT::ValueType corresponding to this LLVM type.
163 /// This is fixed by the LLVM operations except for the pointer size.
164 MVT::ValueType getValueType(const Type *Ty) const {
165 switch (Ty->getTypeID()) {
166 default: assert(0 && "Unknown type!");
167 case Type::VoidTyID: return MVT::isVoid;
168 case Type::BoolTyID: return MVT::i1;
169 case Type::UByteTyID:
170 case Type::SByteTyID: return MVT::i8;
171 case Type::ShortTyID:
172 case Type::UShortTyID: return MVT::i16;
174 case Type::UIntTyID: return MVT::i32;
176 case Type::ULongTyID: return MVT::i64;
177 case Type::FloatTyID: return MVT::f32;
178 case Type::DoubleTyID: return MVT::f64;
179 case Type::PointerTyID: return PointerTy;
183 /// getNumElements - Return the number of registers that this ValueType will
184 /// eventually require. This is always one for all non-integer types, is
185 /// one for any types promoted to live in larger registers, but may be more
186 /// than one for types (like i64) that are split into pieces.
187 unsigned getNumElements(MVT::ValueType VT) const {
188 return NumElementsForVT[VT];
191 /// This function returns the maximum number of store operations permitted
192 /// to replace a call to llvm.memset. The value is set by the target at the
193 /// performance threshold for such a replacement.
194 /// @brief Get maximum # of store operations permitted for llvm.memset
195 unsigned getMaxStoresPerMemSet() const { return maxStoresPerMemSet; }
197 /// This function returns the maximum number of store operations permitted
198 /// to replace a call to llvm.memcpy. The value is set by the target at the
199 /// performance threshold for such a replacement.
200 /// @brief Get maximum # of store operations permitted for llvm.memcpy
201 unsigned getMaxStoresPerMemCpy() const { return maxStoresPerMemCpy; }
203 /// This function returns the maximum number of store operations permitted
204 /// to replace a call to llvm.memmove. The value is set by the target at the
205 /// performance threshold for such a replacement.
206 /// @brief Get maximum # of store operations permitted for llvm.memmove
207 unsigned getMaxStoresPerMemMove() const { return maxStoresPerMemMove; }
209 /// This function returns true if the target allows unaligned stores. This is
210 /// used in situations where an array copy/move/set is converted to a sequence
211 /// of store operations. It ensures that such replacements don't generate
212 /// code that causes an alignment error (trap) on the target machine.
213 /// @brief Determine if the target supports unaligned stores.
214 bool allowsUnalignedStores() const { return allowUnalignedStores; }
216 //===--------------------------------------------------------------------===//
217 // TargetLowering Configuration Methods - These methods should be invoked by
218 // the derived class constructor to configure this object for the target.
223 /// setShiftAmountType - Describe the type that should be used for shift
224 /// amounts. This type defaults to the pointer type.
225 void setShiftAmountType(MVT::ValueType VT) { ShiftAmountTy = VT; }
227 /// setSetCCResultType - Describe the type that shoudl be used as the result
228 /// of a setcc operation. This defaults to the pointer type.
229 void setSetCCResultType(MVT::ValueType VT) { SetCCResultTy = VT; }
231 /// setSetCCResultContents - Specify how the target extends the result of a
232 /// setcc operation in a register.
233 void setSetCCResultContents(SetCCResultValue Ty) { SetCCResultContents = Ty; }
235 /// setShiftAmountFlavor - Describe how the target handles out of range shift
237 void setShiftAmountFlavor(OutOfRangeShiftAmount OORSA) {
238 ShiftAmtHandling = OORSA;
241 /// setSetCCIxExpensive - This is a short term hack for targets that codegen
242 /// setcc as a conditional branch. This encourages the code generator to fold
243 /// setcc operations into other operations if possible.
244 void setSetCCIsExpensive() { SetCCIsExpensive = true; }
246 /// addRegisterClass - Add the specified register class as an available
247 /// regclass for the specified value type. This indicates the selector can
248 /// handle values of that class natively.
249 void addRegisterClass(MVT::ValueType VT, TargetRegisterClass *RC) {
250 AvailableRegClasses.push_back(std::make_pair(VT, RC));
251 RegClassForVT[VT] = RC;
254 /// computeRegisterProperties - Once all of the register classes are added,
255 /// this allows us to compute derived properties we expose.
256 void computeRegisterProperties();
258 /// setOperationAction - Indicate that the specified operation does not work
259 /// with the specified type and indicate what to do about it.
260 void setOperationAction(unsigned Op, MVT::ValueType VT,
261 LegalizeAction Action) {
262 assert(VT < 16 && Op < sizeof(OpActions)/sizeof(OpActions[0]) &&
263 "Table isn't big enough!");
264 OpActions[Op] |= Action << VT*2;
267 /// addLegalFPImmediate - Indicate that this target can instruction select
268 /// the specified FP immediate natively.
269 void addLegalFPImmediate(double Imm) {
270 LegalFPImmediates.push_back(Imm);
275 //===--------------------------------------------------------------------===//
276 // Lowering methods - These methods must be implemented by targets so that
277 // the SelectionDAGLowering code knows how to lower these.
280 /// LowerArguments - This hook must be implemented to indicate how we should
281 /// lower the arguments for the specified function, into the specified DAG.
282 virtual std::vector<SDOperand>
283 LowerArguments(Function &F, SelectionDAG &DAG) = 0;
285 /// LowerCallTo - This hook lowers an abstract call to a function into an
286 /// actual call. This returns a pair of operands. The first element is the
287 /// return value for the function (if RetTy is not VoidTy). The second
288 /// element is the outgoing token chain.
289 typedef std::vector<std::pair<SDOperand, const Type*> > ArgListTy;
290 virtual std::pair<SDOperand, SDOperand>
291 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
292 unsigned CallingConv, bool isTailCall, SDOperand Callee,
293 ArgListTy &Args, SelectionDAG &DAG) = 0;
295 /// LowerVAStart - This lowers the llvm.va_start intrinsic. If not
296 /// implemented, this method prints a message and aborts. This method should
297 /// return the modified chain value. Note that VAListPtr* correspond to the
298 /// llvm.va_start operand.
299 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
300 Value *VAListV, SelectionDAG &DAG);
302 /// LowerVAEnd - This lowers llvm.va_end and returns the resultant chain. If
303 /// not implemented, this defaults to a noop.
304 virtual SDOperand LowerVAEnd(SDOperand Chain, SDOperand LP, Value *LV,
307 /// LowerVACopy - This lowers llvm.va_copy and returns the resultant chain.
308 /// If not implemented, this defaults to loading a pointer from the input and
309 /// storing it to the output.
310 virtual SDOperand LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV,
311 SDOperand DestP, Value *DestV,
314 /// LowerVAArg - This lowers the vaarg instruction. If not implemented, this
315 /// prints a message and aborts.
316 virtual std::pair<SDOperand,SDOperand>
317 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
318 const Type *ArgTy, SelectionDAG &DAG);
320 /// LowerFrameReturnAddress - This hook lowers a call to llvm.returnaddress or
321 /// llvm.frameaddress (depending on the value of the first argument). The
322 /// return values are the result pointer and the resultant token chain. If
323 /// not implemented, both of these intrinsics will return null.
324 virtual std::pair<SDOperand, SDOperand>
325 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
328 /// LowerOperation - For operations that are unsupported by the target, and
329 /// which are registered to use 'custom' lowering. This callback is invoked.
330 /// If the target has no operations that require custom lowering, it need not
331 /// implement this. The default implementation of this aborts.
332 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
337 const TargetData &TD;
339 /// IsLittleEndian - True if this is a little endian target.
343 /// PointerTy - The type to use for pointers, usually i32 or i64.
345 MVT::ValueType PointerTy;
347 /// ShiftAmountTy - The type to use for shift amounts, usually i8 or whatever
349 MVT::ValueType ShiftAmountTy;
351 OutOfRangeShiftAmount ShiftAmtHandling;
353 /// SetCCIsExpensive - This is a short term hack for targets that codegen
354 /// setcc as a conditional branch. This encourages the code generator to fold
355 /// setcc operations into other operations if possible.
356 bool SetCCIsExpensive;
358 /// SetCCResultTy - The type that SetCC operations use. This defaults to the
360 MVT::ValueType SetCCResultTy;
362 /// SetCCResultContents - Information about the contents of the high-bits in
363 /// the result of a setcc comparison operation.
364 SetCCResultValue SetCCResultContents;
366 /// RegClassForVT - This indicates the default register class to use for
367 /// each ValueType the target supports natively.
368 TargetRegisterClass *RegClassForVT[MVT::LAST_VALUETYPE];
369 unsigned char NumElementsForVT[MVT::LAST_VALUETYPE];
371 /// ValueTypeActions - This is a bitvector that contains two bits for each
372 /// value type, where the two bits correspond to the LegalizeAction enum.
373 /// This can be queried with "getTypeAction(VT)".
374 unsigned ValueTypeActions;
376 /// TransformToType - For any value types we are promoting or expanding, this
377 /// contains the value type that we are changing to. For Expanded types, this
378 /// contains one step of the expand (e.g. i64 -> i32), even if there are
379 /// multiple steps required (e.g. i64 -> i16). For types natively supported
380 /// by the system, this holds the same type (e.g. i32 -> i32).
381 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
383 /// OpActions - For each operation and each value type, keep a LegalizeAction
384 /// that indicates how instruction selection should deal with the operation.
385 /// Most operations are Legal (aka, supported natively by the target), but
386 /// operations that are not should be described. Note that operations on
387 /// non-legal value types are not described here.
388 unsigned OpActions[128];
390 std::vector<double> LegalFPImmediates;
392 std::vector<std::pair<MVT::ValueType,
393 TargetRegisterClass*> > AvailableRegClasses;
396 /// When lowering %llvm.memset this field specifies the maximum number of
397 /// store operations that may be substituted for the call to memset. Targets
398 /// must set this value based on the cost threshold for that target. Targets
399 /// should assume that the memset will be done using as many of the largest
400 /// store operations first, followed by smaller ones, if necessary, per
401 /// alignment restrictions. For example, storing 9 bytes on a 32-bit machine
402 /// with 16-bit alignment would result in four 2-byte stores and one 1-byte
403 /// store. This only applies to setting a constant array of a constant size.
404 /// @brief Specify maximum number of store instructions per memset call.
405 unsigned maxStoresPerMemSet;
407 /// When lowering %llvm.memcpy this field specifies the maximum number of
408 /// store operations that may be substituted for a call to memcpy. Targets
409 /// must set this value based on the cost threshold for that target. Targets
410 /// should assume that the memcpy will be done using as many of the largest
411 /// store operations first, followed by smaller ones, if necessary, per
412 /// alignment restrictions. For example, storing 7 bytes on a 32-bit machine
413 /// with 32-bit alignment would result in one 4-byte store, a one 2-byte store
414 /// and one 1-byte store. This only applies to copying a constant array of
416 /// @brief Specify maximum bytes of store instructions per memcpy call.
417 unsigned maxStoresPerMemCpy;
419 /// When lowering %llvm.memmove this field specifies the maximum number of
420 /// store instructions that may be substituted for a call to memmove. Targets
421 /// must set this value based on the cost threshold for that target. Targets
422 /// should assume that the memmove will be done using as many of the largest
423 /// store operations first, followed by smaller ones, if necessary, per
424 /// alignment restrictions. For example, moving 9 bytes on a 32-bit machine
425 /// with 8-bit alignment would result in nine 1-byte stores. This only
426 /// applies to copying a constant array of constant size.
427 /// @brief Specify maximum bytes of store instructions per memmove call.
428 unsigned maxStoresPerMemMove;
430 /// This field specifies whether the target machine permits unaligned stores.
431 /// This is used to determine the size of store operations for copying
432 /// small arrays and other similar tasks.
433 /// @brief Indicate whether the target machine permits unaligned stores.
434 bool allowUnalignedStores;
436 } // end llvm namespace