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/SelectionDAGNodes.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 enum SchedPreference {
73 SchedulingForLatency, // Scheduling for shortest total latency.
74 SchedulingForRegPressure // Scheduling for lowest register pressure.
77 TargetLowering(TargetMachine &TM);
78 virtual ~TargetLowering();
80 TargetMachine &getTargetMachine() const { return TM; }
81 const TargetData *getTargetData() const { return TD; }
83 bool isLittleEndian() const { return IsLittleEndian; }
84 MVT::ValueType getPointerTy() const { return PointerTy; }
85 MVT::ValueType getShiftAmountTy() const { return ShiftAmountTy; }
86 OutOfRangeShiftAmount getShiftAmountFlavor() const {return ShiftAmtHandling; }
88 /// isSetCCExpensive - Return true if the setcc operation is expensive for
90 bool isSetCCExpensive() const { return SetCCIsExpensive; }
92 /// isIntDivCheap() - Return true if integer divide is usually cheaper than
93 /// a sequence of several shifts, adds, and multiplies for this target.
94 bool isIntDivCheap() const { return IntDivIsCheap; }
96 /// isPow2DivCheap() - Return true if pow2 div is cheaper than a chain of
98 bool isPow2DivCheap() const { return Pow2DivIsCheap; }
100 /// getSetCCResultTy - Return the ValueType of the result of setcc operations.
102 MVT::ValueType getSetCCResultTy() const { return SetCCResultTy; }
104 /// getSetCCResultContents - For targets without boolean registers, this flag
105 /// returns information about the contents of the high-bits in the setcc
107 SetCCResultValue getSetCCResultContents() const { return SetCCResultContents;}
109 /// getSchedulingPreference - Return target scheduling preference.
110 SchedPreference getSchedulingPreference() const {
111 return SchedPreferenceInfo;
114 /// getRegClassFor - Return the register class that should be used for the
115 /// specified value type. This may only be called on legal types.
116 TargetRegisterClass *getRegClassFor(MVT::ValueType VT) const {
117 TargetRegisterClass *RC = RegClassForVT[VT];
118 assert(RC && "This value type is not natively supported!");
122 /// isTypeLegal - Return true if the target has native support for the
123 /// specified value type. This means that it has a register that directly
124 /// holds it without promotions or expansions.
125 bool isTypeLegal(MVT::ValueType VT) const {
126 return RegClassForVT[VT] != 0;
129 class ValueTypeActionImpl {
130 /// ValueTypeActions - This is a bitvector that contains two bits for each
131 /// value type, where the two bits correspond to the LegalizeAction enum.
132 /// This can be queried with "getTypeAction(VT)".
133 uint32_t ValueTypeActions[2];
135 ValueTypeActionImpl() {
136 ValueTypeActions[0] = ValueTypeActions[1] = 0;
138 ValueTypeActionImpl(const ValueTypeActionImpl &RHS) {
139 ValueTypeActions[0] = RHS.ValueTypeActions[0];
140 ValueTypeActions[1] = RHS.ValueTypeActions[1];
143 LegalizeAction getTypeAction(MVT::ValueType VT) const {
144 return (LegalizeAction)((ValueTypeActions[VT>>4] >> ((2*VT) & 31)) & 3);
146 void setTypeAction(MVT::ValueType VT, LegalizeAction Action) {
147 assert(unsigned(VT >> 4) <
148 sizeof(ValueTypeActions)/sizeof(ValueTypeActions[0]));
149 ValueTypeActions[VT>>4] |= Action << ((VT*2) & 31);
153 const ValueTypeActionImpl &getValueTypeActions() const {
154 return ValueTypeActions;
157 /// getTypeAction - Return how we should legalize values of this type, either
158 /// it is already legal (return 'Legal') or we need to promote it to a larger
159 /// type (return 'Promote'), or we need to expand it into multiple registers
160 /// of smaller integer type (return 'Expand'). 'Custom' is not an option.
161 LegalizeAction getTypeAction(MVT::ValueType VT) const {
162 return ValueTypeActions.getTypeAction(VT);
165 /// getTypeToTransformTo - For types supported by the target, this is an
166 /// identity function. For types that must be promoted to larger types, this
167 /// returns the larger type to promote to. For types that are larger than the
168 /// largest integer register, this contains one step in the expansion to get
169 /// to the smaller register.
170 MVT::ValueType getTypeToTransformTo(MVT::ValueType VT) const {
171 return TransformToType[VT];
174 /// getPackedTypeBreakdown - Packed types are broken down into some number of
175 /// legal first class types. For example, <8 x float> maps to 2 MVT::v4f32
176 /// with Altivec or SSE1, or 8 promoted MVT::f64 values with the X86 FP stack.
177 /// Similarly, <2 x long> turns into 4 MVT::i32 values with both PPC and X86.
179 /// This method returns the number of registers needed, and the VT for each
180 /// register. It also returns the VT of the PackedType elements before they
181 /// are promoted/expanded.
183 unsigned getPackedTypeBreakdown(const PackedType *PTy,
184 MVT::ValueType &PTyElementVT,
185 MVT::ValueType &PTyLegalElementVT) const;
187 typedef std::vector<double>::const_iterator legal_fpimm_iterator;
188 legal_fpimm_iterator legal_fpimm_begin() const {
189 return LegalFPImmediates.begin();
191 legal_fpimm_iterator legal_fpimm_end() const {
192 return LegalFPImmediates.end();
195 /// isShuffleMaskLegal - Targets can use this to indicate that they only
196 /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
197 /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
198 /// are assumed to be legal.
199 virtual bool isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
203 /// isVectorClearMaskLegal - Similar to isShuffleMaskLegal. This is
204 /// used by Targets can use this to indicate if there is a suitable
205 /// VECTOR_SHUFFLE that can be used to replace a VAND with a constant
207 virtual bool isVectorClearMaskLegal(std::vector<SDOperand> &BVOps,
209 SelectionDAG &DAG) const {
213 /// getOperationAction - Return how this operation should be treated: either
214 /// it is legal, needs to be promoted to a larger size, needs to be
215 /// expanded to some other code sequence, or the target has a custom expander
217 LegalizeAction getOperationAction(unsigned Op, MVT::ValueType VT) const {
218 return (LegalizeAction)((OpActions[Op] >> (2*VT)) & 3);
221 /// isOperationLegal - Return true if the specified operation is legal on this
223 bool isOperationLegal(unsigned Op, MVT::ValueType VT) const {
224 return getOperationAction(Op, VT) == Legal ||
225 getOperationAction(Op, VT) == Custom;
228 /// getTypeToPromoteTo - If the action for this operation is to promote, this
229 /// method returns the ValueType to promote to.
230 MVT::ValueType getTypeToPromoteTo(unsigned Op, MVT::ValueType VT) const {
231 assert(getOperationAction(Op, VT) == Promote &&
232 "This operation isn't promoted!");
234 // See if this has an explicit type specified.
235 std::map<std::pair<unsigned, MVT::ValueType>,
236 MVT::ValueType>::const_iterator PTTI =
237 PromoteToType.find(std::make_pair(Op, VT));
238 if (PTTI != PromoteToType.end()) return PTTI->second;
240 assert((MVT::isInteger(VT) || MVT::isFloatingPoint(VT)) &&
241 "Cannot autopromote this type, add it with AddPromotedToType.");
243 MVT::ValueType NVT = VT;
245 NVT = (MVT::ValueType)(NVT+1);
246 assert(MVT::isInteger(NVT) == MVT::isInteger(VT) && NVT != MVT::isVoid &&
247 "Didn't find type to promote to!");
248 } while (!isTypeLegal(NVT) ||
249 getOperationAction(Op, NVT) == Promote);
253 /// getValueType - Return the MVT::ValueType corresponding to this LLVM type.
254 /// This is fixed by the LLVM operations except for the pointer size.
255 MVT::ValueType getValueType(const Type *Ty) const {
256 switch (Ty->getTypeID()) {
257 default: assert(0 && "Unknown type!");
258 case Type::VoidTyID: return MVT::isVoid;
259 case Type::BoolTyID: return MVT::i1;
260 case Type::UByteTyID:
261 case Type::SByteTyID: return MVT::i8;
262 case Type::ShortTyID:
263 case Type::UShortTyID: return MVT::i16;
265 case Type::UIntTyID: return MVT::i32;
267 case Type::ULongTyID: return MVT::i64;
268 case Type::FloatTyID: return MVT::f32;
269 case Type::DoubleTyID: return MVT::f64;
270 case Type::PointerTyID: return PointerTy;
271 case Type::PackedTyID: return MVT::Vector;
275 /// getNumElements - Return the number of registers that this ValueType will
276 /// eventually require. This is always one for all non-integer types, is
277 /// one for any types promoted to live in larger registers, but may be more
278 /// than one for types (like i64) that are split into pieces.
279 unsigned getNumElements(MVT::ValueType VT) const {
280 return NumElementsForVT[VT];
283 /// hasTargetDAGCombine - If true, the target has custom DAG combine
284 /// transformations that it can perform for the specified node.
285 bool hasTargetDAGCombine(ISD::NodeType NT) const {
286 return TargetDAGCombineArray[NT >> 3] & (1 << (NT&7));
289 /// This function returns the maximum number of store operations permitted
290 /// to replace a call to llvm.memset. The value is set by the target at the
291 /// performance threshold for such a replacement.
292 /// @brief Get maximum # of store operations permitted for llvm.memset
293 unsigned getMaxStoresPerMemset() const { return maxStoresPerMemset; }
295 /// This function returns the maximum number of store operations permitted
296 /// to replace a call to llvm.memcpy. The value is set by the target at the
297 /// performance threshold for such a replacement.
298 /// @brief Get maximum # of store operations permitted for llvm.memcpy
299 unsigned getMaxStoresPerMemcpy() const { return maxStoresPerMemcpy; }
301 /// This function returns the maximum number of store operations permitted
302 /// to replace a call to llvm.memmove. The value is set by the target at the
303 /// performance threshold for such a replacement.
304 /// @brief Get maximum # of store operations permitted for llvm.memmove
305 unsigned getMaxStoresPerMemmove() const { return maxStoresPerMemmove; }
307 /// This function returns true if the target allows unaligned memory accesses.
308 /// This is used, for example, in situations where an array copy/move/set is
309 /// converted to a sequence of store operations. It's use helps to ensure that
310 /// such replacements don't generate code that causes an alignment error
311 /// (trap) on the target machine.
312 /// @brief Determine if the target supports unaligned memory accesses.
313 bool allowsUnalignedMemoryAccesses() const {
314 return allowUnalignedMemoryAccesses;
317 /// usesUnderscoreSetJmpLongJmp - Determine if we should use _setjmp or setjmp
318 /// to implement llvm.setjmp.
319 bool usesUnderscoreSetJmpLongJmp() const {
320 return UseUnderscoreSetJmpLongJmp;
323 /// getStackPointerRegisterToSaveRestore - If a physical register, this
324 /// specifies the register that llvm.savestack/llvm.restorestack should save
326 unsigned getStackPointerRegisterToSaveRestore() const {
327 return StackPointerRegisterToSaveRestore;
330 /// getJumpBufSize - returns the target's jmp_buf size in bytes (if never
331 /// set, the default is 200)
332 unsigned getJumpBufSize() const {
336 /// getJumpBufAlignment - returns the target's jmp_buf alignment in bytes
337 /// (if never set, the default is 0)
338 unsigned getJumpBufAlignment() const {
339 return JumpBufAlignment;
342 //===--------------------------------------------------------------------===//
343 // TargetLowering Optimization Methods
346 /// TargetLoweringOpt - A convenience struct that encapsulates a DAG, and two
347 /// SDOperands for returning information from TargetLowering to its clients
348 /// that want to combine
349 struct TargetLoweringOpt {
354 TargetLoweringOpt(SelectionDAG &InDAG) : DAG(InDAG) {}
356 bool CombineTo(SDOperand O, SDOperand N) {
362 /// ShrinkDemandedConstant - Check to see if the specified operand of the
363 /// specified instruction is a constant integer. If so, check to see if there
364 /// are any bits set in the constant that are not demanded. If so, shrink the
365 /// constant and return true.
366 bool ShrinkDemandedConstant(SDOperand Op, uint64_t Demanded);
369 /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero. We
370 /// use this predicate to simplify operations downstream. Op and Mask are
371 /// known to be the same type.
372 bool MaskedValueIsZero(SDOperand Op, uint64_t Mask, unsigned Depth = 0)
375 /// ComputeMaskedBits - Determine which of the bits specified in Mask are
376 /// known to be either zero or one and return them in the KnownZero/KnownOne
377 /// bitsets. This code only analyzes bits in Mask, in order to short-circuit
378 /// processing. Targets can implement the computeMaskedBitsForTargetNode
379 /// method, to allow target nodes to be understood.
380 void ComputeMaskedBits(SDOperand Op, uint64_t Mask, uint64_t &KnownZero,
381 uint64_t &KnownOne, unsigned Depth = 0) const;
383 /// SimplifyDemandedBits - Look at Op. At this point, we know that only the
384 /// DemandedMask bits of the result of Op are ever used downstream. If we can
385 /// use this information to simplify Op, create a new simplified DAG node and
386 /// return true, returning the original and new nodes in Old and New.
387 /// Otherwise, analyze the expression and return a mask of KnownOne and
388 /// KnownZero bits for the expression (used to simplify the caller).
389 /// The KnownZero/One bits may only be accurate for those bits in the
391 bool SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask,
392 uint64_t &KnownZero, uint64_t &KnownOne,
393 TargetLoweringOpt &TLO, unsigned Depth = 0) const;
395 /// computeMaskedBitsForTargetNode - Determine which of the bits specified in
396 /// Mask are known to be either zero or one and return them in the
397 /// KnownZero/KnownOne bitsets.
398 virtual void computeMaskedBitsForTargetNode(const SDOperand Op,
402 unsigned Depth = 0) const;
404 /// ComputeNumSignBits - Return the number of times the sign bit of the
405 /// register is replicated into the other bits. We know that at least 1 bit
406 /// is always equal to the sign bit (itself), but other cases can give us
407 /// information. For example, immediately after an "SRA X, 2", we know that
408 /// the top 3 bits are all equal to each other, so we return 3.
409 unsigned ComputeNumSignBits(SDOperand Op, unsigned Depth = 0) const;
411 /// ComputeNumSignBitsForTargetNode - This method can be implemented by
412 /// targets that want to expose additional information about sign bits to the
414 virtual unsigned ComputeNumSignBitsForTargetNode(SDOperand Op,
415 unsigned Depth = 0) const;
417 struct DAGCombinerInfo {
418 void *DC; // The DAG Combiner object.
423 DAGCombinerInfo(SelectionDAG &dag, bool bl, void *dc)
424 : DC(dc), BeforeLegalize(bl), DAG(dag) {}
426 bool isBeforeLegalize() const { return BeforeLegalize; }
428 void AddToWorklist(SDNode *N);
429 SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To);
430 SDOperand CombineTo(SDNode *N, SDOperand Res);
431 SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1);
434 /// PerformDAGCombine - This method will be invoked for all target nodes and
435 /// for any target-independent nodes that the target has registered with
438 /// The semantics are as follows:
440 /// SDOperand.Val == 0 - No change was made
441 /// SDOperand.Val == N - N was replaced, is dead, and is already handled.
442 /// otherwise - N should be replaced by the returned Operand.
444 /// In addition, methods provided by DAGCombinerInfo may be used to perform
445 /// more complex transformations.
447 virtual SDOperand PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
449 //===--------------------------------------------------------------------===//
450 // TargetLowering Configuration Methods - These methods should be invoked by
451 // the derived class constructor to configure this object for the target.
456 /// setShiftAmountType - Describe the type that should be used for shift
457 /// amounts. This type defaults to the pointer type.
458 void setShiftAmountType(MVT::ValueType VT) { ShiftAmountTy = VT; }
460 /// setSetCCResultType - Describe the type that shoudl be used as the result
461 /// of a setcc operation. This defaults to the pointer type.
462 void setSetCCResultType(MVT::ValueType VT) { SetCCResultTy = VT; }
464 /// setSetCCResultContents - Specify how the target extends the result of a
465 /// setcc operation in a register.
466 void setSetCCResultContents(SetCCResultValue Ty) { SetCCResultContents = Ty; }
468 /// setSchedulingPreference - Specify the target scheduling preference.
469 void setSchedulingPreference(SchedPreference Pref) {
470 SchedPreferenceInfo = Pref;
473 /// setShiftAmountFlavor - Describe how the target handles out of range shift
475 void setShiftAmountFlavor(OutOfRangeShiftAmount OORSA) {
476 ShiftAmtHandling = OORSA;
479 /// setUseUnderscoreSetJmpLongJmp - Indicate whether this target prefers to
480 /// use _setjmp and _longjmp to or implement llvm.setjmp/llvm.longjmp or
481 /// the non _ versions. Defaults to false.
482 void setUseUnderscoreSetJmpLongJmp(bool Val) {
483 UseUnderscoreSetJmpLongJmp = Val;
486 /// setStackPointerRegisterToSaveRestore - If set to a physical register, this
487 /// specifies the register that llvm.savestack/llvm.restorestack should save
489 void setStackPointerRegisterToSaveRestore(unsigned R) {
490 StackPointerRegisterToSaveRestore = R;
493 /// setSetCCIxExpensive - This is a short term hack for targets that codegen
494 /// setcc as a conditional branch. This encourages the code generator to fold
495 /// setcc operations into other operations if possible.
496 void setSetCCIsExpensive() { SetCCIsExpensive = true; }
498 /// setIntDivIsCheap - Tells the code generator that integer divide is
499 /// expensive, and if possible, should be replaced by an alternate sequence
500 /// of instructions not containing an integer divide.
501 void setIntDivIsCheap(bool isCheap = true) { IntDivIsCheap = isCheap; }
503 /// setPow2DivIsCheap - Tells the code generator that it shouldn't generate
504 /// srl/add/sra for a signed divide by power of two, and let the target handle
506 void setPow2DivIsCheap(bool isCheap = true) { Pow2DivIsCheap = isCheap; }
508 /// addRegisterClass - Add the specified register class as an available
509 /// regclass for the specified value type. This indicates the selector can
510 /// handle values of that class natively.
511 void addRegisterClass(MVT::ValueType VT, TargetRegisterClass *RC) {
512 AvailableRegClasses.push_back(std::make_pair(VT, RC));
513 RegClassForVT[VT] = RC;
516 /// computeRegisterProperties - Once all of the register classes are added,
517 /// this allows us to compute derived properties we expose.
518 void computeRegisterProperties();
520 /// setOperationAction - Indicate that the specified operation does not work
521 /// with the specified type and indicate what to do about it.
522 void setOperationAction(unsigned Op, MVT::ValueType VT,
523 LegalizeAction Action) {
524 assert(VT < 32 && Op < sizeof(OpActions)/sizeof(OpActions[0]) &&
525 "Table isn't big enough!");
526 OpActions[Op] &= ~(uint64_t(3UL) << VT*2);
527 OpActions[Op] |= (uint64_t)Action << VT*2;
530 /// AddPromotedToType - If Opc/OrigVT is specified as being promoted, the
531 /// promotion code defaults to trying a larger integer/fp until it can find
532 /// one that works. If that default is insufficient, this method can be used
533 /// by the target to override the default.
534 void AddPromotedToType(unsigned Opc, MVT::ValueType OrigVT,
535 MVT::ValueType DestVT) {
536 PromoteToType[std::make_pair(Opc, OrigVT)] = DestVT;
539 /// addLegalFPImmediate - Indicate that this target can instruction select
540 /// the specified FP immediate natively.
541 void addLegalFPImmediate(double Imm) {
542 LegalFPImmediates.push_back(Imm);
545 /// setTargetDAGCombine - Targets should invoke this method for each target
546 /// independent node that they want to provide a custom DAG combiner for by
547 /// implementing the PerformDAGCombine virtual method.
548 void setTargetDAGCombine(ISD::NodeType NT) {
549 TargetDAGCombineArray[NT >> 3] |= 1 << (NT&7);
552 /// setJumpBufSize - Set the target's required jmp_buf buffer size (in
553 /// bytes); default is 200
554 void setJumpBufSize(unsigned Size) {
558 /// setJumpBufAlignment - Set the target's required jmp_buf buffer
559 /// alignment (in bytes); default is 0
560 void setJumpBufAlignment(unsigned Align) {
561 JumpBufAlignment = Align;
566 //===--------------------------------------------------------------------===//
567 // Lowering methods - These methods must be implemented by targets so that
568 // the SelectionDAGLowering code knows how to lower these.
571 /// LowerArguments - This hook must be implemented to indicate how we should
572 /// lower the arguments for the specified function, into the specified DAG.
573 virtual std::vector<SDOperand>
574 LowerArguments(Function &F, SelectionDAG &DAG);
576 /// LowerCallTo - This hook lowers an abstract call to a function into an
577 /// actual call. This returns a pair of operands. The first element is the
578 /// return value for the function (if RetTy is not VoidTy). The second
579 /// element is the outgoing token chain.
580 typedef std::vector<std::pair<SDOperand, const Type*> > ArgListTy;
581 virtual std::pair<SDOperand, SDOperand>
582 LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
583 unsigned CallingConv, bool isTailCall, SDOperand Callee,
584 ArgListTy &Args, SelectionDAG &DAG);
586 /// LowerFrameReturnAddress - This hook lowers a call to llvm.returnaddress or
587 /// llvm.frameaddress (depending on the value of the first argument). The
588 /// return values are the result pointer and the resultant token chain. If
589 /// not implemented, both of these intrinsics will return null.
590 virtual std::pair<SDOperand, SDOperand>
591 LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
594 /// LowerOperation - This callback is invoked for operations that are
595 /// unsupported by the target, which are registered to use 'custom' lowering,
596 /// and whose defined values are all legal.
597 /// If the target has no operations that require custom lowering, it need not
598 /// implement this. The default implementation of this aborts.
599 virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
601 /// CustomPromoteOperation - This callback is invoked for operations that are
602 /// unsupported by the target, are registered to use 'custom' lowering, and
603 /// whose type needs to be promoted.
604 virtual SDOperand CustomPromoteOperation(SDOperand Op, SelectionDAG &DAG);
606 /// getTargetNodeName() - This method returns the name of a target specific
608 virtual const char *getTargetNodeName(unsigned Opcode) const;
610 //===--------------------------------------------------------------------===//
611 // Inline Asm Support hooks
614 enum ConstraintType {
615 C_Register, // Constraint represents a single register.
616 C_RegisterClass, // Constraint represents one or more registers.
617 C_Memory, // Memory constraint.
618 C_Other, // Something else.
619 C_Unknown // Unsupported constraint.
622 /// getConstraintType - Given a constraint letter, return the type of
623 /// constraint it is for this target.
624 virtual ConstraintType getConstraintType(char ConstraintLetter) const;
627 /// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
628 /// return a list of registers that can be used to satisfy the constraint.
629 /// This should only be used for C_RegisterClass constraints.
630 virtual std::vector<unsigned>
631 getRegClassForInlineAsmConstraint(const std::string &Constraint,
632 MVT::ValueType VT) const;
634 /// getRegForInlineAsmConstraint - Given a physical register constraint (e.g.
635 /// {edx}), return the register number and the register class for the
636 /// register. This should only be used for C_Register constraints. On error,
637 /// this returns a register number of 0.
638 virtual std::pair<unsigned, const TargetRegisterClass*>
639 getRegForInlineAsmConstraint(const std::string &Constraint,
640 MVT::ValueType VT) const;
643 /// isOperandValidForConstraint - Return true if the specified SDOperand is
644 /// valid for the specified target constraint letter.
645 virtual bool isOperandValidForConstraint(SDOperand Op, char ConstraintLetter);
647 //===--------------------------------------------------------------------===//
651 // InsertAtEndOfBasicBlock - This method should be implemented by targets that
652 // mark instructions with the 'usesCustomDAGSchedInserter' flag. These
653 // instructions are special in various ways, which require special support to
654 // insert. The specified MachineInstr is created but not inserted into any
655 // basic blocks, and the scheduler passes ownership of it to this method.
656 virtual MachineBasicBlock *InsertAtEndOfBasicBlock(MachineInstr *MI,
657 MachineBasicBlock *MBB);
659 //===--------------------------------------------------------------------===//
660 // Loop Strength Reduction hooks
663 /// isLegalAddressImmediate - Return true if the integer value or GlobalValue
664 /// can be used as the offset of the target addressing mode.
665 virtual bool isLegalAddressImmediate(int64_t V) const;
666 virtual bool isLegalAddressImmediate(GlobalValue *GV) const;
668 typedef std::vector<unsigned>::const_iterator legal_am_scale_iterator;
669 legal_am_scale_iterator legal_am_scale_begin() const {
670 return LegalAddressScales.begin();
672 legal_am_scale_iterator legal_am_scale_end() const {
673 return LegalAddressScales.end();
676 //===--------------------------------------------------------------------===//
677 // Div utility functions
679 SDOperand BuildSDIV(SDNode *N, SelectionDAG &DAG,
680 std::vector<SDNode*>* Created) const;
681 SDOperand BuildUDIV(SDNode *N, SelectionDAG &DAG,
682 std::vector<SDNode*>* Created) const;
686 /// addLegalAddressScale - Add a integer (> 1) value which can be used as
687 /// scale in the target addressing mode. Note: the ordering matters so the
688 /// least efficient ones should be entered first.
689 void addLegalAddressScale(unsigned Scale) {
690 LegalAddressScales.push_back(Scale);
694 std::vector<unsigned> LegalAddressScales;
697 const TargetData *TD;
699 /// IsLittleEndian - True if this is a little endian target.
703 /// PointerTy - The type to use for pointers, usually i32 or i64.
705 MVT::ValueType PointerTy;
707 /// ShiftAmountTy - The type to use for shift amounts, usually i8 or whatever
709 MVT::ValueType ShiftAmountTy;
711 OutOfRangeShiftAmount ShiftAmtHandling;
713 /// SetCCIsExpensive - This is a short term hack for targets that codegen
714 /// setcc as a conditional branch. This encourages the code generator to fold
715 /// setcc operations into other operations if possible.
716 bool SetCCIsExpensive;
718 /// IntDivIsCheap - Tells the code generator not to expand integer divides by
719 /// constants into a sequence of muls, adds, and shifts. This is a hack until
720 /// a real cost model is in place. If we ever optimize for size, this will be
721 /// set to true unconditionally.
724 /// Pow2DivIsCheap - Tells the code generator that it shouldn't generate
725 /// srl/add/sra for a signed divide by power of two, and let the target handle
729 /// SetCCResultTy - The type that SetCC operations use. This defaults to the
731 MVT::ValueType SetCCResultTy;
733 /// SetCCResultContents - Information about the contents of the high-bits in
734 /// the result of a setcc comparison operation.
735 SetCCResultValue SetCCResultContents;
737 /// SchedPreferenceInfo - The target scheduling preference: shortest possible
738 /// total cycles or lowest register usage.
739 SchedPreference SchedPreferenceInfo;
741 /// UseUnderscoreSetJmpLongJmp - This target prefers to use _setjmp and
742 /// _longjmp to implement llvm.setjmp/llvm.longjmp. Defaults to false.
743 bool UseUnderscoreSetJmpLongJmp;
745 /// JumpBufSize - The size, in bytes, of the target's jmp_buf buffers
746 unsigned JumpBufSize;
748 /// JumpBufAlignment - The alignment, in bytes, of the target's jmp_buf
750 unsigned JumpBufAlignment;
752 /// StackPointerRegisterToSaveRestore - If set to a physical register, this
753 /// specifies the register that llvm.savestack/llvm.restorestack should save
755 unsigned StackPointerRegisterToSaveRestore;
757 /// RegClassForVT - This indicates the default register class to use for
758 /// each ValueType the target supports natively.
759 TargetRegisterClass *RegClassForVT[MVT::LAST_VALUETYPE];
760 unsigned char NumElementsForVT[MVT::LAST_VALUETYPE];
762 /// TransformToType - For any value types we are promoting or expanding, this
763 /// contains the value type that we are changing to. For Expanded types, this
764 /// contains one step of the expand (e.g. i64 -> i32), even if there are
765 /// multiple steps required (e.g. i64 -> i16). For types natively supported
766 /// by the system, this holds the same type (e.g. i32 -> i32).
767 MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
769 /// OpActions - For each operation and each value type, keep a LegalizeAction
770 /// that indicates how instruction selection should deal with the operation.
771 /// Most operations are Legal (aka, supported natively by the target), but
772 /// operations that are not should be described. Note that operations on
773 /// non-legal value types are not described here.
774 uint64_t OpActions[156];
776 ValueTypeActionImpl ValueTypeActions;
778 std::vector<double> LegalFPImmediates;
780 std::vector<std::pair<MVT::ValueType,
781 TargetRegisterClass*> > AvailableRegClasses;
783 /// TargetDAGCombineArray - Targets can specify ISD nodes that they would
784 /// like PerformDAGCombine callbacks for by calling setTargetDAGCombine(),
785 /// which sets a bit in this array.
786 unsigned char TargetDAGCombineArray[156/(sizeof(unsigned char)*8)];
788 /// PromoteToType - For operations that must be promoted to a specific type,
789 /// this holds the destination type. This map should be sparse, so don't hold
792 /// Targets add entries to this map with AddPromotedToType(..), clients access
793 /// this with getTypeToPromoteTo(..).
794 std::map<std::pair<unsigned, MVT::ValueType>, MVT::ValueType> PromoteToType;
797 /// When lowering %llvm.memset this field specifies the maximum number of
798 /// store operations that may be substituted for the call to memset. Targets
799 /// must set this value based on the cost threshold for that target. Targets
800 /// should assume that the memset will be done using as many of the largest
801 /// store operations first, followed by smaller ones, if necessary, per
802 /// alignment restrictions. For example, storing 9 bytes on a 32-bit machine
803 /// with 16-bit alignment would result in four 2-byte stores and one 1-byte
804 /// store. This only applies to setting a constant array of a constant size.
805 /// @brief Specify maximum number of store instructions per memset call.
806 unsigned maxStoresPerMemset;
808 /// When lowering %llvm.memcpy this field specifies the maximum number of
809 /// store operations that may be substituted for a call to memcpy. Targets
810 /// must set this value based on the cost threshold for that target. Targets
811 /// should assume that the memcpy will be done using as many of the largest
812 /// store operations first, followed by smaller ones, if necessary, per
813 /// alignment restrictions. For example, storing 7 bytes on a 32-bit machine
814 /// with 32-bit alignment would result in one 4-byte store, a one 2-byte store
815 /// and one 1-byte store. This only applies to copying a constant array of
817 /// @brief Specify maximum bytes of store instructions per memcpy call.
818 unsigned maxStoresPerMemcpy;
820 /// When lowering %llvm.memmove this field specifies the maximum number of
821 /// store instructions that may be substituted for a call to memmove. Targets
822 /// must set this value based on the cost threshold for that target. Targets
823 /// should assume that the memmove will be done using as many of the largest
824 /// store operations first, followed by smaller ones, if necessary, per
825 /// alignment restrictions. For example, moving 9 bytes on a 32-bit machine
826 /// with 8-bit alignment would result in nine 1-byte stores. This only
827 /// applies to copying a constant array of constant size.
828 /// @brief Specify maximum bytes of store instructions per memmove call.
829 unsigned maxStoresPerMemmove;
831 /// This field specifies whether the target machine permits unaligned memory
832 /// accesses. This is used, for example, to determine the size of store
833 /// operations when copying small arrays and other similar tasks.
834 /// @brief Indicate whether the target permits unaligned memory accesses.
835 bool allowUnalignedMemoryAccesses;
837 } // end llvm namespace