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;
38 class MachineBasicBlock;
41 //===----------------------------------------------------------------------===//
42 /// TargetLowering - This class defines information used to lower LLVM code to
43 /// legal SelectionDAG operators that the target instruction selector can accept
46 /// This class also defines callbacks that targets must implement to lower
47 /// target-specific constructs to SelectionDAG operators.
49 class TargetLowering {
51 /// LegalizeAction - This enum indicates whether operations are valid for a
52 /// target, and if not, what action should be used to make them valid.
54 Legal, // The target natively supports this operation.
55 Promote, // This operation should be executed in a larger type.
56 Expand, // Try to expand this to other ops, otherwise use a libcall.
57 Custom, // Use the LowerOperation hook to implement custom lowering.
60 enum OutOfRangeShiftAmount {
61 Undefined, // Oversized shift amounts are undefined (default).
62 Mask, // Shift amounts are auto masked (anded) to value size.
63 Extend, // Oversized shift pulls in zeros or sign bits.
66 enum SetCCResultValue {
67 UndefinedSetCCResult, // SetCC returns a garbage/unknown extend.
68 ZeroOrOneSetCCResult, // SetCC returns a zero extended result.
69 ZeroOrNegativeOneSetCCResult, // SetCC returns a sign extended result.
72 TargetLowering(TargetMachine &TM);
73 virtual ~TargetLowering();
75 TargetMachine &getTargetMachine() const { return TM; }
76 const TargetData &getTargetData() const { return TD; }
78 bool isLittleEndian() const { return IsLittleEndian; }
79 MVT::ValueType getPointerTy() const { return PointerTy; }
80 MVT::ValueType getShiftAmountTy() const { return ShiftAmountTy; }
81 OutOfRangeShiftAmount getShiftAmountFlavor() const {return ShiftAmtHandling; }
83 /// isSetCCExpensive - Return true if the setcc operation is expensive for
85 bool isSetCCExpensive() const { return SetCCIsExpensive; }
87 /// isIntDivCheap() - Return true if integer divide is usually cheaper than
88 /// a sequence of several shifts, adds, and multiplies for this target.
89 bool isIntDivCheap() const { return IntDivIsCheap; }
91 /// isPow2DivCheap() - Return true if pow2 div is cheaper than a chain of
93 bool isPow2DivCheap() const { return Pow2DivIsCheap; }
95 /// getSetCCResultTy - Return the ValueType of the result of setcc operations.
97 MVT::ValueType getSetCCResultTy() const { return SetCCResultTy; }
99 /// getSetCCResultContents - For targets without boolean registers, this flag
100 /// returns information about the contents of the high-bits in the setcc
102 SetCCResultValue getSetCCResultContents() const { return SetCCResultContents;}
104 /// getRegClassFor - Return the register class that should be used for the
105 /// specified value type. This may only be called on legal types.
106 TargetRegisterClass *getRegClassFor(MVT::ValueType VT) const {
107 TargetRegisterClass *RC = RegClassForVT[VT];
108 assert(RC && "This value type is not natively supported!");
112 /// isTypeLegal - Return true if the target has native support for the
113 /// specified value type. This means that it has a register that directly
114 /// holds it without promotions or expansions.
115 bool isTypeLegal(MVT::ValueType VT) const {
116 return RegClassForVT[VT] != 0;
119 /// getTypeAction - Return how we should legalize values of this type, either
120 /// it is already legal (return 'Legal') or we need to promote it to a larger
121 /// type (return 'Promote'), or we need to expand it into multiple registers
122 /// of smaller integer type (return 'Expand'). 'Custom' is not an option.
123 LegalizeAction getTypeAction(MVT::ValueType VT) const {
124 return (LegalizeAction)((ValueTypeActions >> (2*VT)) & 3);
126 unsigned getValueTypeActions() const { return ValueTypeActions; }
128 /// getTypeToTransformTo - For types supported by the target, this is an
129 /// identity function. For types that must be promoted to larger types, this
130 /// returns the larger type to promote to. For types that are larger than the
131 /// largest integer register, this contains one step in the expansion to get
132 /// to the smaller register.
133 MVT::ValueType getTypeToTransformTo(MVT::ValueType VT) const {
134 return TransformToType[VT];
137 typedef std::vector<double>::const_iterator legal_fpimm_iterator;
138 legal_fpimm_iterator legal_fpimm_begin() const {
139 return LegalFPImmediates.begin();
141 legal_fpimm_iterator legal_fpimm_end() const {
142 return LegalFPImmediates.end();
145 /// getOperationAction - Return how this operation should be treated: either
146 /// it is legal, needs to be promoted to a larger size, needs to be
147 /// expanded to some other code sequence, or the target has a custom expander
149 LegalizeAction getOperationAction(unsigned Op, MVT::ValueType VT) const {
150 return (LegalizeAction)((OpActions[Op] >> (2*VT)) & 3);
153 /// isOperationLegal - Return true if the specified operation is legal on this
155 bool isOperationLegal(unsigned Op, MVT::ValueType VT) const {
156 return getOperationAction(Op, VT) == Legal;
159 /// getTypeToPromoteTo - If the action for this operation is to promote, this
160 /// method returns the ValueType to promote to.
161 MVT::ValueType getTypeToPromoteTo(unsigned Op, MVT::ValueType VT) const {
162 assert(getOperationAction(Op, VT) == Promote &&
163 "This operation isn't promoted!");
164 MVT::ValueType NVT = VT;
166 NVT = (MVT::ValueType)(NVT+1);
167 assert(MVT::isInteger(NVT) == MVT::isInteger(VT) && NVT != MVT::isVoid &&
168 "Didn't find type to promote to!");
169 } while (!isTypeLegal(NVT) ||
170 getOperationAction(Op, NVT) == Promote);
174 /// getValueType - Return the MVT::ValueType corresponding to this LLVM type.
175 /// This is fixed by the LLVM operations except for the pointer size.
176 MVT::ValueType getValueType(const Type *Ty) const {
177 switch (Ty->getTypeID()) {
178 default: assert(0 && "Unknown type!");
179 case Type::VoidTyID: return MVT::isVoid;
180 case Type::BoolTyID: return MVT::i1;
181 case Type::UByteTyID:
182 case Type::SByteTyID: return MVT::i8;
183 case Type::ShortTyID:
184 case Type::UShortTyID: return MVT::i16;
186 case Type::UIntTyID: return MVT::i32;
188 case Type::ULongTyID: return MVT::i64;
189 case Type::FloatTyID: return MVT::f32;
190 case Type::DoubleTyID: return MVT::f64;
191 case Type::PointerTyID: return PointerTy;
195 /// getNumElements - Return the number of registers that this ValueType will
196 /// eventually require. This is always one for all non-integer types, is
197 /// one for any types promoted to live in larger registers, but may be more
198 /// than one for types (like i64) that are split into pieces.
199 unsigned getNumElements(MVT::ValueType VT) const {
200 return NumElementsForVT[VT];
203 /// This function returns the maximum number of store operations permitted
204 /// to replace a call to llvm.memset. 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.memset
207 unsigned getMaxStoresPerMemSet() const { return maxStoresPerMemSet; }
209 /// This function returns the maximum number of store operations permitted
210 /// to replace a call to llvm.memcpy. The value is set by the target at the
211 /// performance threshold for such a replacement.
212 /// @brief Get maximum # of store operations permitted for llvm.memcpy
213 unsigned getMaxStoresPerMemCpy() const { return maxStoresPerMemCpy; }
215 /// This function returns the maximum number of store operations permitted
216 /// to replace a call to llvm.memmove. The value is set by the target at the
217 /// performance threshold for such a replacement.
218 /// @brief Get maximum # of store operations permitted for llvm.memmove
219 unsigned getMaxStoresPerMemMove() const { return maxStoresPerMemMove; }
221 /// This function returns true if the target allows unaligned memory accesses.
222 /// This is used, for example, in situations where an array copy/move/set is
223 /// converted to a sequence of store operations. It's use helps to ensure that
224 /// such replacements don't generate code that causes an alignment error
225 /// (trap) on the target machine.
226 /// @brief Determine if the target supports unaligned memory accesses.
227 bool allowsUnalignedMemoryAccesses() const
228 { return allowUnalignedMemoryAccesses; }
230 /// usesUnderscoreSetJmpLongJmp - Determine if we should use _setjmp or setjmp
231 /// to implement llvm.setjmp.
232 bool usesUnderscoreSetJmpLongJmp() const {
233 return UseUnderscoreSetJmpLongJmp;
236 //===--------------------------------------------------------------------===//
237 // TargetLowering Configuration Methods - These methods should be invoked by
238 // the derived class constructor to configure this object for the target.
243 /// setShiftAmountType - Describe the type that should be used for shift
244 /// amounts. This type defaults to the pointer type.
245 void setShiftAmountType(MVT::ValueType VT) { ShiftAmountTy = VT; }
247 /// setSetCCResultType - Describe the type that shoudl be used as the result
248 /// of a setcc operation. This defaults to the pointer type.
249 void setSetCCResultType(MVT::ValueType VT) { SetCCResultTy = VT; }
251 /// setSetCCResultContents - Specify how the target extends the result of a
252 /// setcc operation in a register.
253 void setSetCCResultContents(SetCCResultValue Ty) { SetCCResultContents = Ty; }
255 /// setShiftAmountFlavor - Describe how the target handles out of range shift
257 void setShiftAmountFlavor(OutOfRangeShiftAmount OORSA) {
258 ShiftAmtHandling = OORSA;
261 /// setUseUnderscoreSetJmpLongJmp - Indicate whether this target prefers to
262 /// use _setjmp and _longjmp to or implement llvm.setjmp/llvm.longjmp or
263 /// the non _ versions. Defaults to false.
264 void setUseUnderscoreSetJmpLongJmp(bool Val) {
265 UseUnderscoreSetJmpLongJmp = Val;
268 /// setSetCCIxExpensive - This is a short term hack for targets that codegen
269 /// setcc as a conditional branch. This encourages the code generator to fold
270 /// setcc operations into other operations if possible.
271 void setSetCCIsExpensive() { SetCCIsExpensive = true; }
273 /// setIntDivIsCheap - Tells the code generator that integer divide is
274 /// expensive, and if possible, should be replaced by an alternate sequence
275 /// of instructions not containing an integer divide.
276 void setIntDivIsCheap(bool isCheap = true) { IntDivIsCheap = isCheap; }
278 /// setPow2DivIsCheap - Tells the code generator that it shouldn't generate
279 /// srl/add/sra for a signed divide by power of two, and let the target handle
281 void setPow2DivIsCheap(bool isCheap = true) { Pow2DivIsCheap = isCheap; }
283 /// addRegisterClass - Add the specified register class as an available
284 /// regclass for the specified value type. This indicates the selector can
285 /// handle values of that class natively.
286 void addRegisterClass(MVT::ValueType VT, TargetRegisterClass *RC) {
287 AvailableRegClasses.push_back(std::make_pair(VT, RC));
288 RegClassForVT[VT] = RC;
291 /// computeRegisterProperties - Once all of the register classes are added,
292 /// this allows us to compute derived properties we expose.
293 void computeRegisterProperties();
295 /// setOperationAction - Indicate that the specified operation does not work
296 /// with the specified type and indicate what to do about it.
297 void setOperationAction(unsigned Op, MVT::ValueType VT,
298 LegalizeAction Action) {
299 assert(VT < 16 && Op < sizeof(OpActions)/sizeof(OpActions[0]) &&
300 "Table isn't big enough!");
301 OpActions[Op] |= Action << VT*2;
304 /// addLegalFPImmediate - Indicate that this target can instruction select
305 /// the specified FP immediate natively.
306 void addLegalFPImmediate(double Imm) {
307 LegalFPImmediates.push_back(Imm);
312 //===--------------------------------------------------------------------===//
313 // Lowering methods - These methods must be implemented by targets so that
314 // the SelectionDAGLowering code knows how to lower these.
317 /// LowerArguments - This hook must be implemented to indicate how we should
318 /// lower the arguments for the specified function, into the specified DAG.
319 virtual std::vector<SDOperand>
320 LowerArguments(Function &F, SelectionDAG &DAG) = 0;
322 /// LowerCallTo - This hook lowers an abstract call to a function into an
323 /// actual call. This returns a pair of operands. The first element is the
324 /// return value for the function (if RetTy is not VoidTy). The second
325 /// element is the outgoing token chain.
326 typedef std::vector<std::pair<SDOperand, const Type*> > ArgListTy;
327 virtual std::pair<SDOperand, SDOperand>
328 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
329 unsigned CallingConv, bool isTailCall, SDOperand Callee,
330 ArgListTy &Args, SelectionDAG &DAG) = 0;
332 /// LowerReturnTo - This hook lowers a return instruction into the appropriate
333 /// legal ISD::RET node for the target's current ABI. This method is optional
334 /// and is intended for targets that need non-standard behavior.
335 virtual SDOperand LowerReturnTo(SDOperand Chain, SDOperand Op,
338 /// LowerVAStart - This lowers the llvm.va_start intrinsic. If not
339 /// implemented, this method prints a message and aborts. This method should
340 /// return the modified chain value. Note that VAListPtr* correspond to the
341 /// llvm.va_start operand.
342 virtual SDOperand LowerVAStart(SDOperand Chain, SDOperand VAListP,
343 Value *VAListV, SelectionDAG &DAG);
345 /// LowerVAEnd - This lowers llvm.va_end and returns the resultant chain. If
346 /// not implemented, this defaults to a noop.
347 virtual SDOperand LowerVAEnd(SDOperand Chain, SDOperand LP, Value *LV,
350 /// LowerVACopy - This lowers llvm.va_copy and returns the resultant chain.
351 /// If not implemented, this defaults to loading a pointer from the input and
352 /// storing it to the output.
353 virtual SDOperand LowerVACopy(SDOperand Chain, SDOperand SrcP, Value *SrcV,
354 SDOperand DestP, Value *DestV,
357 /// LowerVAArg - This lowers the vaarg instruction. If not implemented, this
358 /// prints a message and aborts.
359 virtual std::pair<SDOperand,SDOperand>
360 LowerVAArg(SDOperand Chain, SDOperand VAListP, Value *VAListV,
361 const Type *ArgTy, SelectionDAG &DAG);
363 /// LowerFrameReturnAddress - This hook lowers a call to llvm.returnaddress or
364 /// llvm.frameaddress (depending on the value of the first argument). The
365 /// return values are the result pointer and the resultant token chain. If
366 /// not implemented, both of these intrinsics will return null.
367 virtual std::pair<SDOperand, SDOperand>
368 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
371 /// LowerOperation - For operations that are unsupported by the target, and
372 /// which are registered to use 'custom' lowering. This callback is invoked.
373 /// If the target has no operations that require custom lowering, it need not
374 /// implement this. The default implementation of this aborts.
375 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
377 //===--------------------------------------------------------------------===//
381 // InsertAtEndOfBasicBlock - This method should be implemented by targets that
382 // mark instructions with the 'usesCustomDAGSchedInserter' flag. These
383 // instructions are special in various ways, which require special support to
384 // insert. The specified MachineInstr is created but not inserted into any
385 // basic blocks, and the scheduler passes ownership of it to this method.
386 virtual MachineBasicBlock *InsertAtEndOfBasicBlock(MachineInstr *MI,
387 MachineBasicBlock *MBB);
391 const TargetData &TD;
393 /// IsLittleEndian - True if this is a little endian target.
397 /// PointerTy - The type to use for pointers, usually i32 or i64.
399 MVT::ValueType PointerTy;
401 /// ShiftAmountTy - The type to use for shift amounts, usually i8 or whatever
403 MVT::ValueType ShiftAmountTy;
405 OutOfRangeShiftAmount ShiftAmtHandling;
407 /// SetCCIsExpensive - This is a short term hack for targets that codegen
408 /// setcc as a conditional branch. This encourages the code generator to fold
409 /// setcc operations into other operations if possible.
410 bool SetCCIsExpensive;
412 /// IntDivIsCheap - Tells the code generator not to expand integer divides by
413 /// constants into a sequence of muls, adds, and shifts. This is a hack until
414 /// a real cost model is in place. If we ever optimize for size, this will be
415 /// set to true unconditionally.
418 /// Pow2DivIsCheap - Tells the code generator that it shouldn't generate
419 /// srl/add/sra for a signed divide by power of two, and let the target handle
423 /// SetCCResultTy - The type that SetCC operations use. This defaults to the
425 MVT::ValueType SetCCResultTy;
427 /// SetCCResultContents - Information about the contents of the high-bits in
428 /// the result of a setcc comparison operation.
429 SetCCResultValue SetCCResultContents;
431 /// UseUnderscoreSetJmpLongJmp - This target prefers to use _setjmp and
432 /// _longjmp to implement llvm.setjmp/llvm.longjmp. Defaults to false.
433 bool UseUnderscoreSetJmpLongJmp;
435 /// RegClassForVT - This indicates the default register class to use for
436 /// each ValueType the target supports natively.
437 TargetRegisterClass *RegClassForVT[MVT::LAST_VALUETYPE];
438 unsigned char NumElementsForVT[MVT::LAST_VALUETYPE];
440 /// ValueTypeActions - This is a bitvector that contains two bits for each
441 /// value type, where the two bits correspond to the LegalizeAction enum.
442 /// This can be queried with "getTypeAction(VT)".
443 unsigned ValueTypeActions;
445 /// TransformToType - For any value types we are promoting or expanding, this
446 /// contains the value type that we are changing to. For Expanded types, this
447 /// contains one step of the expand (e.g. i64 -> i32), even if there are
448 /// multiple steps required (e.g. i64 -> i16). For types natively supported
449 /// by the system, this holds the same type (e.g. i32 -> i32).
450 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
452 /// OpActions - For each operation and each value type, keep a LegalizeAction
453 /// that indicates how instruction selection should deal with the operation.
454 /// Most operations are Legal (aka, supported natively by the target), but
455 /// operations that are not should be described. Note that operations on
456 /// non-legal value types are not described here.
457 unsigned OpActions[128];
459 std::vector<double> LegalFPImmediates;
461 std::vector<std::pair<MVT::ValueType,
462 TargetRegisterClass*> > AvailableRegClasses;
465 /// When lowering %llvm.memset this field specifies the maximum number of
466 /// store operations that may be substituted for the call to memset. Targets
467 /// must set this value based on the cost threshold for that target. Targets
468 /// should assume that the memset will be done using as many of the largest
469 /// store operations first, followed by smaller ones, if necessary, per
470 /// alignment restrictions. For example, storing 9 bytes on a 32-bit machine
471 /// with 16-bit alignment would result in four 2-byte stores and one 1-byte
472 /// store. This only applies to setting a constant array of a constant size.
473 /// @brief Specify maximum number of store instructions per memset call.
474 unsigned maxStoresPerMemSet;
476 /// When lowering %llvm.memcpy this field specifies the maximum number of
477 /// store operations that may be substituted for a call to memcpy. Targets
478 /// must set this value based on the cost threshold for that target. Targets
479 /// should assume that the memcpy will be done using as many of the largest
480 /// store operations first, followed by smaller ones, if necessary, per
481 /// alignment restrictions. For example, storing 7 bytes on a 32-bit machine
482 /// with 32-bit alignment would result in one 4-byte store, a one 2-byte store
483 /// and one 1-byte store. This only applies to copying a constant array of
485 /// @brief Specify maximum bytes of store instructions per memcpy call.
486 unsigned maxStoresPerMemCpy;
488 /// When lowering %llvm.memmove this field specifies the maximum number of
489 /// store instructions that may be substituted for a call to memmove. Targets
490 /// must set this value based on the cost threshold for that target. Targets
491 /// should assume that the memmove will be done using as many of the largest
492 /// store operations first, followed by smaller ones, if necessary, per
493 /// alignment restrictions. For example, moving 9 bytes on a 32-bit machine
494 /// with 8-bit alignment would result in nine 1-byte stores. This only
495 /// applies to copying a constant array of constant size.
496 /// @brief Specify maximum bytes of store instructions per memmove call.
497 unsigned maxStoresPerMemMove;
499 /// This field specifies whether the target machine permits unaligned memory
500 /// accesses. This is used, for example, to determine the size of store
501 /// operations when copying small arrays and other similar tasks.
502 /// @brief Indicate whether the target permits unaligned memory accesses.
503 bool allowUnalignedMemoryAccesses;
505 } // end llvm namespace