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/CodeGen/SelectionDAGNodes.h"
26 #include "llvm/CodeGen/RuntimeLibcalls.h"
35 class TargetRegisterClass;
39 class MachineBasicBlock;
43 //===----------------------------------------------------------------------===//
44 /// TargetLowering - This class defines information used to lower LLVM code to
45 /// legal SelectionDAG operators that the target instruction selector can accept
48 /// This class also defines callbacks that targets must implement to lower
49 /// target-specific constructs to SelectionDAG operators.
51 class TargetLowering {
53 /// LegalizeAction - This enum indicates whether operations are valid for a
54 /// target, and if not, what action should be used to make them valid.
56 Legal, // The target natively supports this operation.
57 Promote, // This operation should be executed in a larger type.
58 Expand, // Try to expand this to other ops, otherwise use a libcall.
59 Custom // Use the LowerOperation hook to implement custom lowering.
62 enum OutOfRangeShiftAmount {
63 Undefined, // Oversized shift amounts are undefined (default).
64 Mask, // Shift amounts are auto masked (anded) to value size.
65 Extend // Oversized shift pulls in zeros or sign bits.
68 enum SetCCResultValue {
69 UndefinedSetCCResult, // SetCC returns a garbage/unknown extend.
70 ZeroOrOneSetCCResult, // SetCC returns a zero extended result.
71 ZeroOrNegativeOneSetCCResult // SetCC returns a sign extended result.
74 enum SchedPreference {
75 SchedulingForLatency, // Scheduling for shortest total latency.
76 SchedulingForRegPressure // Scheduling for lowest register pressure.
79 TargetLowering(TargetMachine &TM);
80 virtual ~TargetLowering();
82 TargetMachine &getTargetMachine() const { return TM; }
83 const TargetData *getTargetData() const { return TD; }
85 bool isLittleEndian() const { return IsLittleEndian; }
86 MVT::ValueType getPointerTy() const { return PointerTy; }
87 MVT::ValueType getShiftAmountTy() const { return ShiftAmountTy; }
88 OutOfRangeShiftAmount getShiftAmountFlavor() const {return ShiftAmtHandling; }
90 /// usesGlobalOffsetTable - Return true if this target uses a GOT for PIC
92 bool usesGlobalOffsetTable() const { return UsesGlobalOffsetTable; }
94 /// isSelectExpensive - Return true if the select operation is expensive for
96 bool isSelectExpensive() const { return SelectIsExpensive; }
98 /// isIntDivCheap() - Return true if integer divide is usually cheaper than
99 /// a sequence of several shifts, adds, and multiplies for this target.
100 bool isIntDivCheap() const { return IntDivIsCheap; }
102 /// isPow2DivCheap() - Return true if pow2 div is cheaper than a chain of
104 bool isPow2DivCheap() const { return Pow2DivIsCheap; }
106 /// getSetCCResultTy - Return the ValueType of the result of setcc operations.
108 MVT::ValueType getSetCCResultTy() const { return SetCCResultTy; }
110 /// getSetCCResultContents - For targets without boolean registers, this flag
111 /// returns information about the contents of the high-bits in the setcc
113 SetCCResultValue getSetCCResultContents() const { return SetCCResultContents;}
115 /// getSchedulingPreference - Return target scheduling preference.
116 SchedPreference getSchedulingPreference() const {
117 return SchedPreferenceInfo;
120 /// getRegClassFor - Return the register class that should be used for the
121 /// specified value type. This may only be called on legal types.
122 TargetRegisterClass *getRegClassFor(MVT::ValueType VT) const {
123 TargetRegisterClass *RC = RegClassForVT[VT];
124 assert(RC && "This value type is not natively supported!");
128 /// isTypeLegal - Return true if the target has native support for the
129 /// specified value type. This means that it has a register that directly
130 /// holds it without promotions or expansions.
131 bool isTypeLegal(MVT::ValueType VT) const {
132 return RegClassForVT[VT] != 0;
135 class ValueTypeActionImpl {
136 /// ValueTypeActions - This is a bitvector that contains two bits for each
137 /// value type, where the two bits correspond to the LegalizeAction enum.
138 /// This can be queried with "getTypeAction(VT)".
139 uint32_t ValueTypeActions[2];
141 ValueTypeActionImpl() {
142 ValueTypeActions[0] = ValueTypeActions[1] = 0;
144 ValueTypeActionImpl(const ValueTypeActionImpl &RHS) {
145 ValueTypeActions[0] = RHS.ValueTypeActions[0];
146 ValueTypeActions[1] = RHS.ValueTypeActions[1];
149 LegalizeAction getTypeAction(MVT::ValueType VT) const {
150 return (LegalizeAction)((ValueTypeActions[VT>>4] >> ((2*VT) & 31)) & 3);
152 void setTypeAction(MVT::ValueType VT, LegalizeAction Action) {
153 assert(unsigned(VT >> 4) <
154 sizeof(ValueTypeActions)/sizeof(ValueTypeActions[0]));
155 ValueTypeActions[VT>>4] |= Action << ((VT*2) & 31);
159 const ValueTypeActionImpl &getValueTypeActions() const {
160 return ValueTypeActions;
163 /// getTypeAction - Return how we should legalize values of this type, either
164 /// it is already legal (return 'Legal') or we need to promote it to a larger
165 /// type (return 'Promote'), or we need to expand it into multiple registers
166 /// of smaller integer type (return 'Expand'). 'Custom' is not an option.
167 LegalizeAction getTypeAction(MVT::ValueType VT) const {
168 return ValueTypeActions.getTypeAction(VT);
171 /// getTypeToTransformTo - For types supported by the target, this is an
172 /// identity function. For types that must be promoted to larger types, this
173 /// returns the larger type to promote to. For integer types that are larger
174 /// than the largest integer register, this contains one step in the expansion
175 /// to get to the smaller register. For illegal floating point types, this
176 /// returns the integer type to transform to.
177 MVT::ValueType getTypeToTransformTo(MVT::ValueType VT) const {
178 return TransformToType[VT];
181 /// getTypeToExpandTo - For types supported by the target, this is an
182 /// identity function. For types that must be expanded (i.e. integer types
183 /// that are larger than the largest integer register or illegal floating
184 /// point types), this returns the largest legal type it will be expanded to.
185 MVT::ValueType getTypeToExpandTo(MVT::ValueType VT) const {
187 switch (getTypeAction(VT)) {
191 VT = TransformToType[VT];
194 assert(false && "Type is not legal nor is it to be expanded!");
201 /// getVectorTypeBreakdown - Vector types are broken down into some number of
202 /// legal first class types. For example, <8 x float> maps to 2 MVT::v4f32
203 /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
204 /// Similarly, <2 x long> turns into 4 MVT::i32 values with both PPC and X86.
206 /// This method returns the number of registers needed, and the VT for each
207 /// register. It also returns the VT of the VectorType elements before they
208 /// are promoted/expanded.
210 unsigned getVectorTypeBreakdown(const VectorType *PTy,
211 MVT::ValueType &PTyElementVT,
212 MVT::ValueType &PTyLegalElementVT) const;
214 typedef std::vector<double>::const_iterator legal_fpimm_iterator;
215 legal_fpimm_iterator legal_fpimm_begin() const {
216 return LegalFPImmediates.begin();
218 legal_fpimm_iterator legal_fpimm_end() const {
219 return LegalFPImmediates.end();
222 /// isShuffleMaskLegal - Targets can use this to indicate that they only
223 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
224 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
225 /// are assumed to be legal.
226 virtual bool isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
230 /// isVectorClearMaskLegal - Similar to isShuffleMaskLegal. This is
231 /// used by Targets can use this to indicate if there is a suitable
232 /// VECTOR_SHUFFLE that can be used to replace a VAND with a constant
234 virtual bool isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
236 SelectionDAG &DAG) const {
240 /// getOperationAction - Return how this operation should be treated: either
241 /// it is legal, needs to be promoted to a larger size, needs to be
242 /// expanded to some other code sequence, or the target has a custom expander
244 LegalizeAction getOperationAction(unsigned Op, MVT::ValueType VT) const {
245 return (LegalizeAction)((OpActions[Op] >> (2*VT)) & 3);
248 /// isOperationLegal - Return true if the specified operation is legal on this
250 bool isOperationLegal(unsigned Op, MVT::ValueType VT) const {
251 return getOperationAction(Op, VT) == Legal ||
252 getOperationAction(Op, VT) == Custom;
255 /// getLoadXAction - Return how this load with extension should be treated:
256 /// either it is legal, needs to be promoted to a larger size, needs to be
257 /// expanded to some other code sequence, or the target has a custom expander
259 LegalizeAction getLoadXAction(unsigned LType, MVT::ValueType VT) const {
260 return (LegalizeAction)((LoadXActions[LType] >> (2*VT)) & 3);
263 /// isLoadXLegal - Return true if the specified load with extension is legal
265 bool isLoadXLegal(unsigned LType, MVT::ValueType VT) const {
266 return getLoadXAction(LType, VT) == Legal ||
267 getLoadXAction(LType, VT) == Custom;
270 /// getStoreXAction - Return how this store with truncation should be treated:
271 /// either it is legal, needs to be promoted to a larger size, needs to be
272 /// expanded to some other code sequence, or the target has a custom expander
274 LegalizeAction getStoreXAction(MVT::ValueType VT) const {
275 return (LegalizeAction)((StoreXActions >> (2*VT)) & 3);
278 /// isStoreXLegal - Return true if the specified store with truncation is
279 /// legal on this target.
280 bool isStoreXLegal(MVT::ValueType VT) const {
281 return getStoreXAction(VT) == Legal || getStoreXAction(VT) == Custom;
284 /// getIndexedLoadAction - Return how the indexed load should be treated:
285 /// either it is legal, needs to be promoted to a larger size, needs to be
286 /// expanded to some other code sequence, or the target has a custom expander
289 getIndexedLoadAction(unsigned IdxMode, MVT::ValueType VT) const {
290 return (LegalizeAction)((IndexedModeActions[0][IdxMode] >> (2*VT)) & 3);
293 /// isIndexedLoadLegal - Return true if the specified indexed load is legal
295 bool isIndexedLoadLegal(unsigned IdxMode, MVT::ValueType VT) const {
296 return getIndexedLoadAction(IdxMode, VT) == Legal ||
297 getIndexedLoadAction(IdxMode, VT) == Custom;
300 /// getIndexedStoreAction - Return how the indexed store should be treated:
301 /// either it is legal, needs to be promoted to a larger size, needs to be
302 /// expanded to some other code sequence, or the target has a custom expander
305 getIndexedStoreAction(unsigned IdxMode, MVT::ValueType VT) const {
306 return (LegalizeAction)((IndexedModeActions[1][IdxMode] >> (2*VT)) & 3);
309 /// isIndexedStoreLegal - Return true if the specified indexed load is legal
311 bool isIndexedStoreLegal(unsigned IdxMode, MVT::ValueType VT) const {
312 return getIndexedStoreAction(IdxMode, VT) == Legal ||
313 getIndexedStoreAction(IdxMode, VT) == Custom;
316 /// getTypeToPromoteTo - If the action for this operation is to promote, this
317 /// method returns the ValueType to promote to.
318 MVT::ValueType getTypeToPromoteTo(unsigned Op, MVT::ValueType VT) const {
319 assert(getOperationAction(Op, VT) == Promote &&
320 "This operation isn't promoted!");
322 // See if this has an explicit type specified.
323 std::map<std::pair<unsigned, MVT::ValueType>,
324 MVT::ValueType>::const_iterator PTTI =
325 PromoteToType.find(std::make_pair(Op, VT));
326 if (PTTI != PromoteToType.end()) return PTTI->second;
328 assert((MVT::isInteger(VT) || MVT::isFloatingPoint(VT)) &&
329 "Cannot autopromote this type, add it with AddPromotedToType.");
331 MVT::ValueType NVT = VT;
333 NVT = (MVT::ValueType)(NVT+1);
334 assert(MVT::isInteger(NVT) == MVT::isInteger(VT) && NVT != MVT::isVoid &&
335 "Didn't find type to promote to!");
336 } while (!isTypeLegal(NVT) ||
337 getOperationAction(Op, NVT) == Promote);
341 /// getValueType - Return the MVT::ValueType corresponding to this LLVM type.
342 /// This is fixed by the LLVM operations except for the pointer size. If
343 /// AllowUnknown is true, this will return MVT::Other for types with no MVT
344 /// counterpart (e.g. structs), otherwise it will assert.
345 MVT::ValueType getValueType(const Type *Ty, bool AllowUnknown = false) const {
346 MVT::ValueType VT = MVT::getValueType(Ty, AllowUnknown);
347 return VT == MVT::iPTR ? PointerTy : VT;
350 /// getNumElements - Return the number of registers that this ValueType will
351 /// eventually require. This is one for any types promoted to live in larger
352 /// registers, but may be more than one for types (like i64) that are split
354 unsigned getNumElements(MVT::ValueType VT) const {
355 return NumElementsForVT[VT];
358 /// hasTargetDAGCombine - If true, the target has custom DAG combine
359 /// transformations that it can perform for the specified node.
360 bool hasTargetDAGCombine(ISD::NodeType NT) const {
361 return TargetDAGCombineArray[NT >> 3] & (1 << (NT&7));
364 /// This function returns the maximum number of store operations permitted
365 /// to replace a call to llvm.memset. The value is set by the target at the
366 /// performance threshold for such a replacement.
367 /// @brief Get maximum # of store operations permitted for llvm.memset
368 unsigned getMaxStoresPerMemset() const { return maxStoresPerMemset; }
370 /// This function returns the maximum number of store operations permitted
371 /// to replace a call to llvm.memcpy. The value is set by the target at the
372 /// performance threshold for such a replacement.
373 /// @brief Get maximum # of store operations permitted for llvm.memcpy
374 unsigned getMaxStoresPerMemcpy() const { return maxStoresPerMemcpy; }
376 /// This function returns the maximum number of store operations permitted
377 /// to replace a call to llvm.memmove. The value is set by the target at the
378 /// performance threshold for such a replacement.
379 /// @brief Get maximum # of store operations permitted for llvm.memmove
380 unsigned getMaxStoresPerMemmove() const { return maxStoresPerMemmove; }
382 /// This function returns true if the target allows unaligned memory accesses.
383 /// This is used, for example, in situations where an array copy/move/set is
384 /// converted to a sequence of store operations. It's use helps to ensure that
385 /// such replacements don't generate code that causes an alignment error
386 /// (trap) on the target machine.
387 /// @brief Determine if the target supports unaligned memory accesses.
388 bool allowsUnalignedMemoryAccesses() const {
389 return allowUnalignedMemoryAccesses;
392 /// usesUnderscoreSetJmp - Determine if we should use _setjmp or setjmp
393 /// to implement llvm.setjmp.
394 bool usesUnderscoreSetJmp() const {
395 return UseUnderscoreSetJmp;
398 /// usesUnderscoreLongJmp - Determine if we should use _longjmp or longjmp
399 /// to implement llvm.longjmp.
400 bool usesUnderscoreLongJmp() const {
401 return UseUnderscoreLongJmp;
404 /// getStackPointerRegisterToSaveRestore - If a physical register, this
405 /// specifies the register that llvm.savestack/llvm.restorestack should save
407 unsigned getStackPointerRegisterToSaveRestore() const {
408 return StackPointerRegisterToSaveRestore;
411 /// getExceptionAddressRegister - If a physical register, this returns
412 /// the register that receives the exception address on entry to a landing
414 unsigned getExceptionAddressRegister() const {
415 return ExceptionPointerRegister;
418 /// getExceptionSelectorRegister - If a physical register, this returns
419 /// the register that receives the exception typeid on entry to a landing
421 unsigned getExceptionSelectorRegister() const {
422 return ExceptionSelectorRegister;
425 /// getJumpBufSize - returns the target's jmp_buf size in bytes (if never
426 /// set, the default is 200)
427 unsigned getJumpBufSize() const {
431 /// getJumpBufAlignment - returns the target's jmp_buf alignment in bytes
432 /// (if never set, the default is 0)
433 unsigned getJumpBufAlignment() const {
434 return JumpBufAlignment;
437 /// getPreIndexedAddressParts - returns true by value, base pointer and
438 /// offset pointer and addressing mode by reference if the node's address
439 /// can be legally represented as pre-indexed load / store address.
440 virtual bool getPreIndexedAddressParts(SDNode *N, SDOperand &Base,
442 ISD::MemIndexedMode &AM,
447 /// getPostIndexedAddressParts - returns true by value, base pointer and
448 /// offset pointer and addressing mode by reference if this node can be
449 /// combined with a load / store to form a post-indexed load / store.
450 virtual bool getPostIndexedAddressParts(SDNode *N, SDNode *Op,
451 SDOperand &Base, SDOperand &Offset,
452 ISD::MemIndexedMode &AM,
457 //===--------------------------------------------------------------------===//
458 // TargetLowering Optimization Methods
461 /// TargetLoweringOpt - A convenience struct that encapsulates a DAG, and two
462 /// SDOperands for returning information from TargetLowering to its clients
463 /// that want to combine
464 struct TargetLoweringOpt {
469 TargetLoweringOpt(SelectionDAG &InDAG) : DAG(InDAG) {}
471 bool CombineTo(SDOperand O, SDOperand N) {
477 /// ShrinkDemandedConstant - Check to see if the specified operand of the
478 /// specified instruction is a constant integer. If so, check to see if there
479 /// are any bits set in the constant that are not demanded. If so, shrink the
480 /// constant and return true.
481 bool ShrinkDemandedConstant(SDOperand Op, uint64_t Demanded);
484 /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We
485 /// use this predicate to simplify operations downstream. Op and Mask are
486 /// known to be the same type.
487 bool MaskedValueIsZero(SDOperand Op, uint64_t Mask, unsigned Depth = 0)
490 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
491 /// known to be either zero or one and return them in the KnownZero/KnownOne
492 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
493 /// processing. Targets can implement the computeMaskedBitsForTargetNode
494 /// method, to allow target nodes to be understood.
495 void ComputeMaskedBits(SDOperand Op, uint64_t Mask, uint64_t &KnownZero,
496 uint64_t &KnownOne, unsigned Depth = 0) const;
498 /// SimplifyDemandedBits - Look at Op. At this point, we know that only the
499 /// DemandedMask bits of the result of Op are ever used downstream. If we can
500 /// use this information to simplify Op, create a new simplified DAG node and
501 /// return true, returning the original and new nodes in Old and New.
502 /// Otherwise, analyze the expression and return a mask of KnownOne and
503 /// KnownZero bits for the expression (used to simplify the caller).
504 /// The KnownZero/One bits may only be accurate for those bits in the
506 bool SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask,
507 uint64_t &KnownZero, uint64_t &KnownOne,
508 TargetLoweringOpt &TLO, unsigned Depth = 0) const;
510 /// computeMaskedBitsForTargetNode - Determine which of the bits specified in
511 /// Mask are known to be either zero or one and return them in the
512 /// KnownZero/KnownOne bitsets.
513 virtual void computeMaskedBitsForTargetNode(const SDOperand Op,
517 unsigned Depth = 0) const;
519 /// ComputeNumSignBits - Return the number of times the sign bit of the
520 /// register is replicated into the other bits. We know that at least 1 bit
521 /// is always equal to the sign bit (itself), but other cases can give us
522 /// information. For example, immediately after an "SRA X, 2", we know that
523 /// the top 3 bits are all equal to each other, so we return 3.
524 unsigned ComputeNumSignBits(SDOperand Op, unsigned Depth = 0) const;
526 /// ComputeNumSignBitsForTargetNode - This method can be implemented by
527 /// targets that want to expose additional information about sign bits to the
529 virtual unsigned ComputeNumSignBitsForTargetNode(SDOperand Op,
530 unsigned Depth = 0) const;
532 struct DAGCombinerInfo {
533 void *DC; // The DAG Combiner object.
535 bool CalledByLegalizer;
539 DAGCombinerInfo(SelectionDAG &dag, bool bl, bool cl, void *dc)
540 : DC(dc), BeforeLegalize(bl), CalledByLegalizer(cl), DAG(dag) {}
542 bool isBeforeLegalize() const { return BeforeLegalize; }
543 bool isCalledByLegalizer() const { return CalledByLegalizer; }
545 void AddToWorklist(SDNode *N);
546 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To);
547 SDOperand CombineTo(SDNode *N, SDOperand Res);
548 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1);
551 /// SimplifySetCC - Try to simplify a setcc built with the specified operands
552 /// and cc. If it is unable to simplify it, return a null SDOperand.
553 SDOperand SimplifySetCC(MVT::ValueType VT, SDOperand N0, SDOperand N1,
554 ISD::CondCode Cond, bool foldBooleans,
555 DAGCombinerInfo &DCI) const;
557 /// PerformDAGCombine - This method will be invoked for all target nodes and
558 /// for any target-independent nodes that the target has registered with
561 /// The semantics are as follows:
563 /// SDOperand.Val == 0 - No change was made
564 /// SDOperand.Val == N - N was replaced, is dead, and is already handled.
565 /// otherwise - N should be replaced by the returned Operand.
567 /// In addition, methods provided by DAGCombinerInfo may be used to perform
568 /// more complex transformations.
570 virtual SDOperand PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
572 //===--------------------------------------------------------------------===//
573 // TargetLowering Configuration Methods - These methods should be invoked by
574 // the derived class constructor to configure this object for the target.
578 /// setUsesGlobalOffsetTable - Specify that this target does or doesn't use a
579 /// GOT for PC-relative code.
580 void setUsesGlobalOffsetTable(bool V) { UsesGlobalOffsetTable = V; }
582 /// setShiftAmountType - Describe the type that should be used for shift
583 /// amounts. This type defaults to the pointer type.
584 void setShiftAmountType(MVT::ValueType VT) { ShiftAmountTy = VT; }
586 /// setSetCCResultType - Describe the type that shoudl be used as the result
587 /// of a setcc operation. This defaults to the pointer type.
588 void setSetCCResultType(MVT::ValueType VT) { SetCCResultTy = VT; }
590 /// setSetCCResultContents - Specify how the target extends the result of a
591 /// setcc operation in a register.
592 void setSetCCResultContents(SetCCResultValue Ty) { SetCCResultContents = Ty; }
594 /// setSchedulingPreference - Specify the target scheduling preference.
595 void setSchedulingPreference(SchedPreference Pref) {
596 SchedPreferenceInfo = Pref;
599 /// setShiftAmountFlavor - Describe how the target handles out of range shift
601 void setShiftAmountFlavor(OutOfRangeShiftAmount OORSA) {
602 ShiftAmtHandling = OORSA;
605 /// setUseUnderscoreSetJmp - Indicate whether this target prefers to
606 /// use _setjmp to implement llvm.setjmp or the non _ version.
607 /// Defaults to false.
608 void setUseUnderscoreSetJmp(bool Val) {
609 UseUnderscoreSetJmp = Val;
612 /// setUseUnderscoreLongJmp - Indicate whether this target prefers to
613 /// use _longjmp to implement llvm.longjmp or the non _ version.
614 /// Defaults to false.
615 void setUseUnderscoreLongJmp(bool Val) {
616 UseUnderscoreLongJmp = Val;
619 /// setStackPointerRegisterToSaveRestore - If set to a physical register, this
620 /// specifies the register that llvm.savestack/llvm.restorestack should save
622 void setStackPointerRegisterToSaveRestore(unsigned R) {
623 StackPointerRegisterToSaveRestore = R;
626 /// setExceptionPointerRegister - If set to a physical register, this sets
627 /// the register that receives the exception address on entry to a landing
629 void setExceptionPointerRegister(unsigned R) {
630 ExceptionPointerRegister = R;
633 /// setExceptionSelectorRegister - If set to a physical register, this sets
634 /// the register that receives the exception typeid on entry to a landing
636 void setExceptionSelectorRegister(unsigned R) {
637 ExceptionSelectorRegister = R;
640 /// SelectIsExpensive - Tells the code generator not to expand operations
641 /// into sequences that use the select operations if possible.
642 void setSelectIsExpensive() { SelectIsExpensive = true; }
644 /// setIntDivIsCheap - Tells the code generator that integer divide is
645 /// expensive, and if possible, should be replaced by an alternate sequence
646 /// of instructions not containing an integer divide.
647 void setIntDivIsCheap(bool isCheap = true) { IntDivIsCheap = isCheap; }
649 /// setPow2DivIsCheap - Tells the code generator that it shouldn't generate
650 /// srl/add/sra for a signed divide by power of two, and let the target handle
652 void setPow2DivIsCheap(bool isCheap = true) { Pow2DivIsCheap = isCheap; }
654 /// addRegisterClass - Add the specified register class as an available
655 /// regclass for the specified value type. This indicates the selector can
656 /// handle values of that class natively.
657 void addRegisterClass(MVT::ValueType VT, TargetRegisterClass *RC) {
658 AvailableRegClasses.push_back(std::make_pair(VT, RC));
659 RegClassForVT[VT] = RC;
662 /// computeRegisterProperties - Once all of the register classes are added,
663 /// this allows us to compute derived properties we expose.
664 void computeRegisterProperties();
666 /// setOperationAction - Indicate that the specified operation does not work
667 /// with the specified type and indicate what to do about it.
668 void setOperationAction(unsigned Op, MVT::ValueType VT,
669 LegalizeAction Action) {
670 assert(VT < 32 && Op < sizeof(OpActions)/sizeof(OpActions[0]) &&
671 "Table isn't big enough!");
672 OpActions[Op] &= ~(uint64_t(3UL) << VT*2);
673 OpActions[Op] |= (uint64_t)Action << VT*2;
676 /// setLoadXAction - Indicate that the specified load with extension does not
677 /// work with the with specified type and indicate what to do about it.
678 void setLoadXAction(unsigned ExtType, MVT::ValueType VT,
679 LegalizeAction Action) {
680 assert(VT < 32 && ExtType < sizeof(LoadXActions)/sizeof(LoadXActions[0]) &&
681 "Table isn't big enough!");
682 LoadXActions[ExtType] &= ~(uint64_t(3UL) << VT*2);
683 LoadXActions[ExtType] |= (uint64_t)Action << VT*2;
686 /// setStoreXAction - Indicate that the specified store with truncation does
687 /// not work with the with specified type and indicate what to do about it.
688 void setStoreXAction(MVT::ValueType VT, LegalizeAction Action) {
689 assert(VT < 32 && "Table isn't big enough!");
690 StoreXActions &= ~(uint64_t(3UL) << VT*2);
691 StoreXActions |= (uint64_t)Action << VT*2;
694 /// setIndexedLoadAction - Indicate that the specified indexed load does or
695 /// does not work with the with specified type and indicate what to do abort
696 /// it. NOTE: All indexed mode loads are initialized to Expand in
697 /// TargetLowering.cpp
698 void setIndexedLoadAction(unsigned IdxMode, MVT::ValueType VT,
699 LegalizeAction Action) {
700 assert(VT < 32 && IdxMode <
701 sizeof(IndexedModeActions[0]) / sizeof(IndexedModeActions[0][0]) &&
702 "Table isn't big enough!");
703 IndexedModeActions[0][IdxMode] &= ~(uint64_t(3UL) << VT*2);
704 IndexedModeActions[0][IdxMode] |= (uint64_t)Action << VT*2;
707 /// setIndexedStoreAction - Indicate that the specified indexed store does or
708 /// does not work with the with specified type and indicate what to do about
709 /// it. NOTE: All indexed mode stores are initialized to Expand in
710 /// TargetLowering.cpp
711 void setIndexedStoreAction(unsigned IdxMode, MVT::ValueType VT,
712 LegalizeAction Action) {
713 assert(VT < 32 && IdxMode <
714 sizeof(IndexedModeActions[1]) / sizeof(IndexedModeActions[1][0]) &&
715 "Table isn't big enough!");
716 IndexedModeActions[1][IdxMode] &= ~(uint64_t(3UL) << VT*2);
717 IndexedModeActions[1][IdxMode] |= (uint64_t)Action << VT*2;
720 /// AddPromotedToType - If Opc/OrigVT is specified as being promoted, the
721 /// promotion code defaults to trying a larger integer/fp until it can find
722 /// one that works. If that default is insufficient, this method can be used
723 /// by the target to override the default.
724 void AddPromotedToType(unsigned Opc, MVT::ValueType OrigVT,
725 MVT::ValueType DestVT) {
726 PromoteToType[std::make_pair(Opc, OrigVT)] = DestVT;
729 /// addLegalFPImmediate - Indicate that this target can instruction select
730 /// the specified FP immediate natively.
731 void addLegalFPImmediate(double Imm) {
732 LegalFPImmediates.push_back(Imm);
735 /// setTargetDAGCombine - Targets should invoke this method for each target
736 /// independent node that they want to provide a custom DAG combiner for by
737 /// implementing the PerformDAGCombine virtual method.
738 void setTargetDAGCombine(ISD::NodeType NT) {
739 TargetDAGCombineArray[NT >> 3] |= 1 << (NT&7);
742 /// setJumpBufSize - Set the target's required jmp_buf buffer size (in
743 /// bytes); default is 200
744 void setJumpBufSize(unsigned Size) {
748 /// setJumpBufAlignment - Set the target's required jmp_buf buffer
749 /// alignment (in bytes); default is 0
750 void setJumpBufAlignment(unsigned Align) {
751 JumpBufAlignment = Align;
756 //===--------------------------------------------------------------------===//
757 // Lowering methods - These methods must be implemented by targets so that
758 // the SelectionDAGLowering code knows how to lower these.
761 /// LowerArguments - This hook must be implemented to indicate how we should
762 /// lower the arguments for the specified function, into the specified DAG.
763 virtual std::vector<SDOperand>
764 LowerArguments(Function &F, SelectionDAG &DAG);
766 /// LowerCallTo - This hook lowers an abstract call to a function into an
767 /// actual call. This returns a pair of operands. The first element is the
768 /// return value for the function (if RetTy is not VoidTy). The second
769 /// element is the outgoing token chain.
770 struct ArgListEntry {
778 ArgListEntry():isSExt(false), isZExt(false), isInReg(false), isSRet(false) { };
780 typedef std::vector<ArgListEntry> ArgListTy;
781 virtual std::pair<SDOperand, SDOperand>
782 LowerCallTo(SDOperand Chain, const Type *RetTy, bool RetTyIsSigned,
783 bool isVarArg, unsigned CallingConv, bool isTailCall,
784 SDOperand Callee, ArgListTy &Args, SelectionDAG &DAG);
786 /// LowerOperation - This callback is invoked for operations that are
787 /// unsupported by the target, which are registered to use 'custom' lowering,
788 /// and whose defined values are all legal.
789 /// If the target has no operations that require custom lowering, it need not
790 /// implement this. The default implementation of this aborts.
791 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
793 /// CustomPromoteOperation - This callback is invoked for operations that are
794 /// unsupported by the target, are registered to use 'custom' lowering, and
795 /// whose type needs to be promoted.
796 virtual SDOperand CustomPromoteOperation(SDOperand Op, SelectionDAG &DAG);
798 /// getTargetNodeName() - This method returns the name of a target specific
800 virtual const char *getTargetNodeName(unsigned Opcode) const;
802 //===--------------------------------------------------------------------===//
803 // Inline Asm Support hooks
806 enum ConstraintType {
807 C_Register, // Constraint represents a single register.
808 C_RegisterClass, // Constraint represents one or more registers.
809 C_Memory, // Memory constraint.
810 C_Other, // Something else.
811 C_Unknown // Unsupported constraint.
814 /// getConstraintType - Given a constraint, return the type of constraint it
815 /// is for this target.
816 virtual ConstraintType getConstraintType(const std::string &Constraint) const;
819 /// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
820 /// return a list of registers that can be used to satisfy the constraint.
821 /// This should only be used for C_RegisterClass constraints.
822 virtual std::vector<unsigned>
823 getRegClassForInlineAsmConstraint(const std::string &Constraint,
824 MVT::ValueType VT) const;
826 /// getRegForInlineAsmConstraint - Given a physical register constraint (e.g.
827 /// {edx}), return the register number and the register class for the
830 /// Given a register class constraint, like 'r', if this corresponds directly
831 /// to an LLVM register class, return a register of 0 and the register class
834 /// This should only be used for C_Register constraints. On error,
835 /// this returns a register number of 0 and a null register class pointer..
836 virtual std::pair<unsigned, const TargetRegisterClass*>
837 getRegForInlineAsmConstraint(const std::string &Constraint,
838 MVT::ValueType VT) const;
841 /// isOperandValidForConstraint - Return the specified operand (possibly
842 /// modified) if the specified SDOperand is valid for the specified target
843 /// constraint letter, otherwise return null.
845 isOperandValidForConstraint(SDOperand Op, char ConstraintLetter,
848 //===--------------------------------------------------------------------===//
852 // InsertAtEndOfBasicBlock - This method should be implemented by targets that
853 // mark instructions with the 'usesCustomDAGSchedInserter' flag. These
854 // instructions are special in various ways, which require special support to
855 // insert. The specified MachineInstr is created but not inserted into any
856 // basic blocks, and the scheduler passes ownership of it to this method.
857 virtual MachineBasicBlock *InsertAtEndOfBasicBlock(MachineInstr *MI,
858 MachineBasicBlock *MBB);
860 //===--------------------------------------------------------------------===//
861 // Addressing mode description hooks (used by LSR etc).
864 /// AddrMode - This represents an addressing mode of:
865 /// BaseGV + BaseOffs + BaseReg + Scale*ScaleReg
866 /// If BaseGV is null, there is no BaseGV.
867 /// If BaseOffs is zero, there is no base offset.
868 /// If HasBaseReg is false, there is no base register.
869 /// If Scale is zero, there is no ScaleReg. Scale of 1 indicates a reg with
877 AddrMode() : BaseGV(0), BaseOffs(0), HasBaseReg(false), Scale(0) {}
880 /// isLegalAddressingMode - Return true if the addressing mode represented by
881 /// AM is legal for this target, for a load/store of the specified type.
882 /// TODO: Handle pre/postinc as well.
883 virtual bool isLegalAddressingMode(const AddrMode &AM, const Type *Ty) const;
885 //===--------------------------------------------------------------------===//
886 // Div utility functions
888 SDOperand BuildSDIV(SDNode *N, SelectionDAG &DAG,
889 std::vector<SDNode*>* Created) const;
890 SDOperand BuildUDIV(SDNode *N, SelectionDAG &DAG,
891 std::vector<SDNode*>* Created) const;
894 //===--------------------------------------------------------------------===//
895 // Runtime Library hooks
898 /// setLibcallName - Rename the default libcall routine name for the specified
900 void setLibcallName(RTLIB::Libcall Call, const char *Name) {
901 LibcallRoutineNames[Call] = Name;
904 /// getLibcallName - Get the libcall routine name for the specified libcall.
906 const char *getLibcallName(RTLIB::Libcall Call) const {
907 return LibcallRoutineNames[Call];
910 /// setCmpLibcallCC - Override the default CondCode to be used to test the
911 /// result of the comparison libcall against zero.
912 void setCmpLibcallCC(RTLIB::Libcall Call, ISD::CondCode CC) {
913 CmpLibcallCCs[Call] = CC;
916 /// getCmpLibcallCC - Get the CondCode that's to be used to test the result of
917 /// the comparison libcall against zero.
918 ISD::CondCode getCmpLibcallCC(RTLIB::Libcall Call) const {
919 return CmpLibcallCCs[Call];
924 const TargetData *TD;
926 /// IsLittleEndian - True if this is a little endian target.
930 /// PointerTy - The type to use for pointers, usually i32 or i64.
932 MVT::ValueType PointerTy;
934 /// UsesGlobalOffsetTable - True if this target uses a GOT for PIC codegen.
936 bool UsesGlobalOffsetTable;
938 /// ShiftAmountTy - The type to use for shift amounts, usually i8 or whatever
940 MVT::ValueType ShiftAmountTy;
942 OutOfRangeShiftAmount ShiftAmtHandling;
944 /// SelectIsExpensive - Tells the code generator not to expand operations
945 /// into sequences that use the select operations if possible.
946 bool SelectIsExpensive;
948 /// IntDivIsCheap - Tells the code generator not to expand integer divides by
949 /// constants into a sequence of muls, adds, and shifts. This is a hack until
950 /// a real cost model is in place. If we ever optimize for size, this will be
951 /// set to true unconditionally.
954 /// Pow2DivIsCheap - Tells the code generator that it shouldn't generate
955 /// srl/add/sra for a signed divide by power of two, and let the target handle
959 /// SetCCResultTy - The type that SetCC operations use. This defaults to the
961 MVT::ValueType SetCCResultTy;
963 /// SetCCResultContents - Information about the contents of the high-bits in
964 /// the result of a setcc comparison operation.
965 SetCCResultValue SetCCResultContents;
967 /// SchedPreferenceInfo - The target scheduling preference: shortest possible
968 /// total cycles or lowest register usage.
969 SchedPreference SchedPreferenceInfo;
971 /// UseUnderscoreSetJmp - This target prefers to use _setjmp to implement
972 /// llvm.setjmp. Defaults to false.
973 bool UseUnderscoreSetJmp;
975 /// UseUnderscoreLongJmp - This target prefers to use _longjmp to implement
976 /// llvm.longjmp. Defaults to false.
977 bool UseUnderscoreLongJmp;
979 /// JumpBufSize - The size, in bytes, of the target's jmp_buf buffers
980 unsigned JumpBufSize;
982 /// JumpBufAlignment - The alignment, in bytes, of the target's jmp_buf
984 unsigned JumpBufAlignment;
986 /// StackPointerRegisterToSaveRestore - If set to a physical register, this
987 /// specifies the register that llvm.savestack/llvm.restorestack should save
989 unsigned StackPointerRegisterToSaveRestore;
991 /// ExceptionPointerRegister - If set to a physical register, this specifies
992 /// the register that receives the exception address on entry to a landing
994 unsigned ExceptionPointerRegister;
996 /// ExceptionSelectorRegister - If set to a physical register, this specifies
997 /// the register that receives the exception typeid on entry to a landing
999 unsigned ExceptionSelectorRegister;
1001 /// RegClassForVT - This indicates the default register class to use for
1002 /// each ValueType the target supports natively.
1003 TargetRegisterClass *RegClassForVT[MVT::LAST_VALUETYPE];
1004 unsigned char NumElementsForVT[MVT::LAST_VALUETYPE];
1006 /// TransformToType - For any value types we are promoting or expanding, this
1007 /// contains the value type that we are changing to. For Expanded types, this
1008 /// contains one step of the expand (e.g. i64 -> i32), even if there are
1009 /// multiple steps required (e.g. i64 -> i16). For types natively supported
1010 /// by the system, this holds the same type (e.g. i32 -> i32).
1011 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
1013 /// OpActions - For each operation and each value type, keep a LegalizeAction
1014 /// that indicates how instruction selection should deal with the operation.
1015 /// Most operations are Legal (aka, supported natively by the target), but
1016 /// operations that are not should be described. Note that operations on
1017 /// non-legal value types are not described here.
1018 uint64_t OpActions[156];
1020 /// LoadXActions - For each load of load extension type and each value type,
1021 /// keep a LegalizeAction that indicates how instruction selection should deal
1023 uint64_t LoadXActions[ISD::LAST_LOADX_TYPE];
1025 /// StoreXActions - For each store with truncation of each value type, keep a
1026 /// LegalizeAction that indicates how instruction selection should deal with
1028 uint64_t StoreXActions;
1030 /// IndexedModeActions - For each indexed mode and each value type, keep a
1031 /// pair of LegalizeAction that indicates how instruction selection should
1032 /// deal with the load / store.
1033 uint64_t IndexedModeActions[2][ISD::LAST_INDEXED_MODE];
1035 ValueTypeActionImpl ValueTypeActions;
1037 std::vector<double> LegalFPImmediates;
1039 std::vector<std::pair<MVT::ValueType,
1040 TargetRegisterClass*> > AvailableRegClasses;
1042 /// TargetDAGCombineArray - Targets can specify ISD nodes that they would
1043 /// like PerformDAGCombine callbacks for by calling setTargetDAGCombine(),
1044 /// which sets a bit in this array.
1045 unsigned char TargetDAGCombineArray[156/(sizeof(unsigned char)*8)];
1047 /// PromoteToType - For operations that must be promoted to a specific type,
1048 /// this holds the destination type. This map should be sparse, so don't hold
1051 /// Targets add entries to this map with AddPromotedToType(..), clients access
1052 /// this with getTypeToPromoteTo(..).
1053 std::map<std::pair<unsigned, MVT::ValueType>, MVT::ValueType> PromoteToType;
1055 /// LibcallRoutineNames - Stores the name each libcall.
1057 const char *LibcallRoutineNames[RTLIB::UNKNOWN_LIBCALL];
1059 /// CmpLibcallCCs - The ISD::CondCode that should be used to test the result
1060 /// of each of the comparison libcall against zero.
1061 ISD::CondCode CmpLibcallCCs[RTLIB::UNKNOWN_LIBCALL];
1064 /// When lowering %llvm.memset this field specifies the maximum number of
1065 /// store operations that may be substituted for the call to memset. Targets
1066 /// must set this value based on the cost threshold for that target. Targets
1067 /// should assume that the memset will be done using as many of the largest
1068 /// store operations first, followed by smaller ones, if necessary, per
1069 /// alignment restrictions. For example, storing 9 bytes on a 32-bit machine
1070 /// with 16-bit alignment would result in four 2-byte stores and one 1-byte
1071 /// store. This only applies to setting a constant array of a constant size.
1072 /// @brief Specify maximum number of store instructions per memset call.
1073 unsigned maxStoresPerMemset;
1075 /// When lowering %llvm.memcpy this field specifies the maximum number of
1076 /// store operations that may be substituted for a call to memcpy. Targets
1077 /// must set this value based on the cost threshold for that target. Targets
1078 /// should assume that the memcpy will be done using as many of the largest
1079 /// store operations first, followed by smaller ones, if necessary, per
1080 /// alignment restrictions. For example, storing 7 bytes on a 32-bit machine
1081 /// with 32-bit alignment would result in one 4-byte store, a one 2-byte store
1082 /// and one 1-byte store. This only applies to copying a constant array of
1084 /// @brief Specify maximum bytes of store instructions per memcpy call.
1085 unsigned maxStoresPerMemcpy;
1087 /// When lowering %llvm.memmove this field specifies the maximum number of
1088 /// store instructions that may be substituted for a call to memmove. Targets
1089 /// must set this value based on the cost threshold for that target. Targets
1090 /// should assume that the memmove will be done using as many of the largest
1091 /// store operations first, followed by smaller ones, if necessary, per
1092 /// alignment restrictions. For example, moving 9 bytes on a 32-bit machine
1093 /// with 8-bit alignment would result in nine 1-byte stores. This only
1094 /// applies to copying a constant array of constant size.
1095 /// @brief Specify maximum bytes of store instructions per memmove call.
1096 unsigned maxStoresPerMemmove;
1098 /// This field specifies whether the target machine permits unaligned memory
1099 /// accesses. This is used, for example, to determine the size of store
1100 /// operations when copying small arrays and other similar tasks.
1101 /// @brief Indicate whether the target permits unaligned memory accesses.
1102 bool allowUnalignedMemoryAccesses;
1104 } // end llvm namespace