Allow targets to have fine grained control over which types various ops get
[oota-llvm.git] / include / llvm / Target / TargetLowering.h
1 //===-- llvm/Target/TargetLowering.h - Target Lowering Info -----*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes how to lower LLVM code to machine code.  This has two
11 // main components:
12 //
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.
16 //
17 // In addition it has a few other components, like information about FP
18 // immediates.
19 //
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_TARGET_TARGETLOWERING_H
23 #define LLVM_TARGET_TARGETLOWERING_H
24
25 #include "llvm/Type.h"
26 #include "llvm/CodeGen/SelectionDAGNodes.h"
27 #include <map>
28
29 namespace llvm {
30   class Value;
31   class Function;
32   class TargetMachine;
33   class TargetData;
34   class TargetRegisterClass;
35   class SDNode;
36   class SDOperand;
37   class SelectionDAG;
38   class MachineBasicBlock;
39   class MachineInstr;
40
41 //===----------------------------------------------------------------------===//
42 /// TargetLowering - This class defines information used to lower LLVM code to
43 /// legal SelectionDAG operators that the target instruction selector can accept
44 /// natively.
45 ///
46 /// This class also defines callbacks that targets must implement to lower
47 /// target-specific constructs to SelectionDAG operators.
48 ///
49 class TargetLowering {
50 public:
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.
53   enum LegalizeAction {
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.
58   };
59
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.
64   };
65
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.
70   };
71
72   enum SchedPreference {
73     SchedulingForLatency,          // Scheduling for shortest total latency.
74     SchedulingForRegPressure       // Scheduling for lowest register pressure.
75   };
76
77   TargetLowering(TargetMachine &TM);
78   virtual ~TargetLowering();
79
80   TargetMachine &getTargetMachine() const { return TM; }
81   const TargetData &getTargetData() const { return TD; }
82
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; }
87
88   /// isSetCCExpensive - Return true if the setcc operation is expensive for
89   /// this target.
90   bool isSetCCExpensive() const { return SetCCIsExpensive; }
91   
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; }
95
96   /// isPow2DivCheap() - Return true if pow2 div is cheaper than a chain of
97   /// srl/add/sra.
98   bool isPow2DivCheap() const { return Pow2DivIsCheap; }
99   
100   /// getSetCCResultTy - Return the ValueType of the result of setcc operations.
101   ///
102   MVT::ValueType getSetCCResultTy() const { return SetCCResultTy; }
103
104   /// getSetCCResultContents - For targets without boolean registers, this flag
105   /// returns information about the contents of the high-bits in the setcc
106   /// result register.
107   SetCCResultValue getSetCCResultContents() const { return SetCCResultContents;}
108
109   /// getSchedulingPreference - Return target scheduling preference.
110   SchedPreference getSchedulingPreference() const {
111     return SchedPreferenceInfo;
112   }
113
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!");
119     return RC;
120   }
121   
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;
127   }
128
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];
134   public:
135     ValueTypeActionImpl() {
136       ValueTypeActions[0] = ValueTypeActions[1] = 0;
137     }
138     ValueTypeActionImpl(const ValueTypeActionImpl &RHS) {
139       ValueTypeActions[0] = RHS.ValueTypeActions[0];
140       ValueTypeActions[1] = RHS.ValueTypeActions[1];
141     }
142     
143     LegalizeAction getTypeAction(MVT::ValueType VT) const {
144       return (LegalizeAction)((ValueTypeActions[VT>>4] >> ((2*VT) & 31)) & 3);
145     }
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);
150     }
151   };
152   
153   const ValueTypeActionImpl &getValueTypeActions() const {
154     return ValueTypeActions;
155   }
156   
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);
163   }
164
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];
172   }
173   
174   /// getPackedTypeBreakdown - Packed types are broken down into some number of
175   /// legal scalar types.  For example, <8 x float> maps to 2 MVT::v2f32 values
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.
178   ///
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.
182   ///
183   unsigned getPackedTypeBreakdown(const PackedType *PTy, 
184                                   MVT::ValueType &PTyElementVT,
185                                   MVT::ValueType &PTyLegalElementVT) const;
186   
187   typedef std::vector<double>::const_iterator legal_fpimm_iterator;
188   legal_fpimm_iterator legal_fpimm_begin() const {
189     return LegalFPImmediates.begin();
190   }
191   legal_fpimm_iterator legal_fpimm_end() const {
192     return LegalFPImmediates.end();
193   }
194
195   /// getOperationAction - Return how this operation should be treated: either
196   /// it is legal, needs to be promoted to a larger size, needs to be
197   /// expanded to some other code sequence, or the target has a custom expander
198   /// for it.
199   LegalizeAction getOperationAction(unsigned Op, MVT::ValueType VT) const {
200     return (LegalizeAction)((OpActions[Op] >> (2*VT)) & 3);
201   }
202   
203   /// isOperationLegal - Return true if the specified operation is legal on this
204   /// target.
205   bool isOperationLegal(unsigned Op, MVT::ValueType VT) const {
206     return getOperationAction(Op, VT) == Legal ||
207            getOperationAction(Op, VT) == Custom;
208   }
209   
210   
211   /// isVectorShuffleLegal - Return true if a vector shuffle is legal with the
212   /// specified mask and type.  Targets can specify exactly which masks they
213   /// support and the code generator is tasked with not creating illegal masks.
214   bool isShuffleLegal(MVT::ValueType VT, SDOperand Mask) const {
215     return isOperationLegal(ISD::VECTOR_SHUFFLE, VT) && 
216            isShuffleMaskLegal(Mask, VT);
217   }
218
219   /// getTypeToPromoteTo - If the action for this operation is to promote, this
220   /// method returns the ValueType to promote to.
221   MVT::ValueType getTypeToPromoteTo(unsigned Op, MVT::ValueType VT) const {
222     assert(getOperationAction(Op, VT) == Promote &&
223            "This operation isn't promoted!");
224
225     // See if this has an explicit type specified.
226     std::map<std::pair<unsigned, MVT::ValueType>, 
227              MVT::ValueType>::const_iterator PTTI =
228       PromoteToType.find(std::make_pair(Op, VT));
229     if (PTTI != PromoteToType.end()) return PTTI->second;
230     
231     assert((MVT::isInteger(VT) || MVT::isFloatingPoint(VT)) &&
232            "Cannot autopromote this type, add it with AddPromotedToType.");
233     
234     MVT::ValueType NVT = VT;
235     do {
236       NVT = (MVT::ValueType)(NVT+1);
237       assert(MVT::isInteger(NVT) == MVT::isInteger(VT) && NVT != MVT::isVoid &&
238              "Didn't find type to promote to!");
239     } while (!isTypeLegal(NVT) ||
240               getOperationAction(Op, NVT) == Promote);
241     return NVT;
242   }
243
244   /// getValueType - Return the MVT::ValueType corresponding to this LLVM type.
245   /// This is fixed by the LLVM operations except for the pointer size.
246   MVT::ValueType getValueType(const Type *Ty) const {
247     switch (Ty->getTypeID()) {
248     default: assert(0 && "Unknown type!");
249     case Type::VoidTyID:    return MVT::isVoid;
250     case Type::BoolTyID:    return MVT::i1;
251     case Type::UByteTyID:
252     case Type::SByteTyID:   return MVT::i8;
253     case Type::ShortTyID:
254     case Type::UShortTyID:  return MVT::i16;
255     case Type::IntTyID:
256     case Type::UIntTyID:    return MVT::i32;
257     case Type::LongTyID:
258     case Type::ULongTyID:   return MVT::i64;
259     case Type::FloatTyID:   return MVT::f32;
260     case Type::DoubleTyID:  return MVT::f64;
261     case Type::PointerTyID: return PointerTy;
262     case Type::PackedTyID:  return MVT::Vector;
263     }
264   }
265
266   /// getNumElements - Return the number of registers that this ValueType will
267   /// eventually require.  This is always one for all non-integer types, is
268   /// one for any types promoted to live in larger registers, but may be more
269   /// than one for types (like i64) that are split into pieces.
270   unsigned getNumElements(MVT::ValueType VT) const {
271     return NumElementsForVT[VT];
272   }
273   
274   /// hasTargetDAGCombine - If true, the target has custom DAG combine
275   /// transformations that it can perform for the specified node.
276   bool hasTargetDAGCombine(ISD::NodeType NT) const {
277     return TargetDAGCombineArray[NT >> 3] & (1 << (NT&7));
278   }
279
280   /// This function returns the maximum number of store operations permitted
281   /// to replace a call to llvm.memset. The value is set by the target at the
282   /// performance threshold for such a replacement.
283   /// @brief Get maximum # of store operations permitted for llvm.memset
284   unsigned getMaxStoresPerMemset() const { return maxStoresPerMemset; }
285
286   /// This function returns the maximum number of store operations permitted
287   /// to replace a call to llvm.memcpy. The value is set by the target at the
288   /// performance threshold for such a replacement.
289   /// @brief Get maximum # of store operations permitted for llvm.memcpy
290   unsigned getMaxStoresPerMemcpy() const { return maxStoresPerMemcpy; }
291
292   /// This function returns the maximum number of store operations permitted
293   /// to replace a call to llvm.memmove. The value is set by the target at the
294   /// performance threshold for such a replacement.
295   /// @brief Get maximum # of store operations permitted for llvm.memmove
296   unsigned getMaxStoresPerMemmove() const { return maxStoresPerMemmove; }
297
298   /// This function returns true if the target allows unaligned memory accesses.
299   /// This is used, for example, in situations where an array copy/move/set is 
300   /// converted to a sequence of store operations. It's use helps to ensure that
301   /// such replacements don't generate code that causes an alignment error 
302   /// (trap) on the target machine. 
303   /// @brief Determine if the target supports unaligned memory accesses.
304   bool allowsUnalignedMemoryAccesses() const {
305     return allowUnalignedMemoryAccesses;
306   }
307   
308   /// usesUnderscoreSetJmpLongJmp - Determine if we should use _setjmp or setjmp
309   /// to implement llvm.setjmp.
310   bool usesUnderscoreSetJmpLongJmp() const {
311     return UseUnderscoreSetJmpLongJmp;
312   }
313   
314   /// getStackPointerRegisterToSaveRestore - If a physical register, this
315   /// specifies the register that llvm.savestack/llvm.restorestack should save
316   /// and restore.
317   unsigned getStackPointerRegisterToSaveRestore() const {
318     return StackPointerRegisterToSaveRestore;
319   }
320
321   //===--------------------------------------------------------------------===//
322   // TargetLowering Optimization Methods
323   //
324   
325   /// TargetLoweringOpt - A convenience struct that encapsulates a DAG, and two
326   /// SDOperands for returning information from TargetLowering to its clients
327   /// that want to combine 
328   struct TargetLoweringOpt {
329     SelectionDAG &DAG;
330     SDOperand Old;
331     SDOperand New;
332
333     TargetLoweringOpt(SelectionDAG &InDAG) : DAG(InDAG) {}
334     
335     bool CombineTo(SDOperand O, SDOperand N) { 
336       Old = O; 
337       New = N; 
338       return true;
339     }
340     
341     /// ShrinkDemandedConstant - Check to see if the specified operand of the 
342     /// specified instruction is a constant integer.  If so, check to see if there
343     /// are any bits set in the constant that are not demanded.  If so, shrink the
344     /// constant and return true.
345     bool ShrinkDemandedConstant(SDOperand Op, uint64_t Demanded);
346   };
347                                                 
348   /// MaskedValueIsZero - Return true if 'Op & Mask' is known to be zero.  We
349   /// use this predicate to simplify operations downstream.  Op and Mask are
350   /// known to be the same type.
351   bool MaskedValueIsZero(SDOperand Op, uint64_t Mask, unsigned Depth = 0)
352     const;
353   
354   /// ComputeMaskedBits - Determine which of the bits specified in Mask are
355   /// known to be either zero or one and return them in the KnownZero/KnownOne
356   /// bitsets.  This code only analyzes bits in Mask, in order to short-circuit
357   /// processing.  Targets can implement the computeMaskedBitsForTargetNode 
358   /// method, to allow target nodes to be understood.
359   void ComputeMaskedBits(SDOperand Op, uint64_t Mask, uint64_t &KnownZero,
360                          uint64_t &KnownOne, unsigned Depth = 0) const;
361     
362   /// SimplifyDemandedBits - Look at Op.  At this point, we know that only the
363   /// DemandedMask bits of the result of Op are ever used downstream.  If we can
364   /// use this information to simplify Op, create a new simplified DAG node and
365   /// return true, returning the original and new nodes in Old and New. 
366   /// Otherwise, analyze the expression and return a mask of KnownOne and 
367   /// KnownZero bits for the expression (used to simplify the caller).  
368   /// The KnownZero/One bits may only be accurate for those bits in the 
369   /// DemandedMask.
370   bool SimplifyDemandedBits(SDOperand Op, uint64_t DemandedMask, 
371                             uint64_t &KnownZero, uint64_t &KnownOne,
372                             TargetLoweringOpt &TLO, unsigned Depth = 0) const;
373   
374   /// computeMaskedBitsForTargetNode - Determine which of the bits specified in
375   /// Mask are known to be either zero or one and return them in the 
376   /// KnownZero/KnownOne bitsets.
377   virtual void computeMaskedBitsForTargetNode(const SDOperand Op,
378                                               uint64_t Mask,
379                                               uint64_t &KnownZero, 
380                                               uint64_t &KnownOne,
381                                               unsigned Depth = 0) const;
382
383   struct DAGCombinerInfo {
384     void *DC;  // The DAG Combiner object.
385     bool BeforeLegalize;
386   public:
387     SelectionDAG &DAG;
388     
389     DAGCombinerInfo(SelectionDAG &dag, bool bl, void *dc)
390       : DC(dc), BeforeLegalize(bl), DAG(dag) {}
391     
392     bool isBeforeLegalize() const { return BeforeLegalize; }
393     
394     void AddToWorklist(SDNode *N);
395     SDOperand CombineTo(SDNode *N, const std::vector<SDOperand> &To);
396     SDOperand CombineTo(SDNode *N, SDOperand Res);
397     SDOperand CombineTo(SDNode *N, SDOperand Res0, SDOperand Res1);
398   };
399
400   /// PerformDAGCombine - This method will be invoked for all target nodes and
401   /// for any target-independent nodes that the target has registered with
402   /// invoke it for.
403   ///
404   /// The semantics are as follows:
405   /// Return Value:
406   ///   SDOperand.Val == 0   - No change was made
407   ///   SDOperand.Val == N   - N was replaced, is dead, and is already handled.
408   ///   otherwise            - N should be replaced by the returned Operand.
409   ///
410   /// In addition, methods provided by DAGCombinerInfo may be used to perform
411   /// more complex transformations.
412   ///
413   virtual SDOperand PerformDAGCombine(SDNode *N, DAGCombinerInfo &DCI) const;
414   
415   //===--------------------------------------------------------------------===//
416   // TargetLowering Configuration Methods - These methods should be invoked by
417   // the derived class constructor to configure this object for the target.
418   //
419
420 protected:
421
422   /// setShiftAmountType - Describe the type that should be used for shift
423   /// amounts.  This type defaults to the pointer type.
424   void setShiftAmountType(MVT::ValueType VT) { ShiftAmountTy = VT; }
425
426   /// setSetCCResultType - Describe the type that shoudl be used as the result
427   /// of a setcc operation.  This defaults to the pointer type.
428   void setSetCCResultType(MVT::ValueType VT) { SetCCResultTy = VT; }
429
430   /// setSetCCResultContents - Specify how the target extends the result of a
431   /// setcc operation in a register.
432   void setSetCCResultContents(SetCCResultValue Ty) { SetCCResultContents = Ty; }
433
434   /// setSchedulingPreference - Specify the target scheduling preference.
435   void setSchedulingPreference(SchedPreference Pref) {
436     SchedPreferenceInfo = Pref;
437   }
438
439   /// setShiftAmountFlavor - Describe how the target handles out of range shift
440   /// amounts.
441   void setShiftAmountFlavor(OutOfRangeShiftAmount OORSA) {
442     ShiftAmtHandling = OORSA;
443   }
444
445   /// setUseUnderscoreSetJmpLongJmp - Indicate whether this target prefers to
446   /// use _setjmp and _longjmp to or implement llvm.setjmp/llvm.longjmp or
447   /// the non _ versions.  Defaults to false.
448   void setUseUnderscoreSetJmpLongJmp(bool Val) {
449     UseUnderscoreSetJmpLongJmp = Val;
450   }
451   
452   /// setStackPointerRegisterToSaveRestore - If set to a physical register, this
453   /// specifies the register that llvm.savestack/llvm.restorestack should save
454   /// and restore.
455   void setStackPointerRegisterToSaveRestore(unsigned R) {
456     StackPointerRegisterToSaveRestore = R;
457   }
458   
459   /// setSetCCIxExpensive - This is a short term hack for targets that codegen
460   /// setcc as a conditional branch.  This encourages the code generator to fold
461   /// setcc operations into other operations if possible.
462   void setSetCCIsExpensive() { SetCCIsExpensive = true; }
463
464   /// setIntDivIsCheap - Tells the code generator that integer divide is
465   /// expensive, and if possible, should be replaced by an alternate sequence
466   /// of instructions not containing an integer divide.
467   void setIntDivIsCheap(bool isCheap = true) { IntDivIsCheap = isCheap; }
468   
469   /// setPow2DivIsCheap - Tells the code generator that it shouldn't generate
470   /// srl/add/sra for a signed divide by power of two, and let the target handle
471   /// it.
472   void setPow2DivIsCheap(bool isCheap = true) { Pow2DivIsCheap = isCheap; }
473   
474   /// addRegisterClass - Add the specified register class as an available
475   /// regclass for the specified value type.  This indicates the selector can
476   /// handle values of that class natively.
477   void addRegisterClass(MVT::ValueType VT, TargetRegisterClass *RC) {
478     AvailableRegClasses.push_back(std::make_pair(VT, RC));
479     RegClassForVT[VT] = RC;
480   }
481
482   /// computeRegisterProperties - Once all of the register classes are added,
483   /// this allows us to compute derived properties we expose.
484   void computeRegisterProperties();
485
486   /// setOperationAction - Indicate that the specified operation does not work
487   /// with the specified type and indicate what to do about it.
488   void setOperationAction(unsigned Op, MVT::ValueType VT,
489                           LegalizeAction Action) {
490     assert(VT < 32 && Op < sizeof(OpActions)/sizeof(OpActions[0]) &&
491            "Table isn't big enough!");
492     OpActions[Op] &= ~(3ULL << VT*2);
493     OpActions[Op] |= (uint64_t)Action << VT*2;
494   }
495   
496   /// AddPromotedToType - If Opc/OrigVT is specified as being promoted, the
497   /// promotion code defaults to trying a larger integer/fp until it can find
498   /// one that works.  If that default is insufficient, this method can be used
499   /// by the target to override the default.
500   void AddPromotedToType(unsigned Opc, MVT::ValueType OrigVT, 
501                          MVT::ValueType DestVT) {
502     PromoteToType[std::make_pair(Opc, OrigVT)] = DestVT;
503   }
504
505   /// addLegalFPImmediate - Indicate that this target can instruction select
506   /// the specified FP immediate natively.
507   void addLegalFPImmediate(double Imm) {
508     LegalFPImmediates.push_back(Imm);
509   }
510
511   /// setTargetDAGCombine - Targets should invoke this method for each target
512   /// independent node that they want to provide a custom DAG combiner for by
513   /// implementing the PerformDAGCombine virtual method.
514   void setTargetDAGCombine(ISD::NodeType NT) {
515     TargetDAGCombineArray[NT >> 3] |= 1 << (NT&7);
516   }
517   
518   /// isShuffleMaskLegal - Targets can use this to indicate that they only
519   /// support *some* VECTOR_SHUFFLE operations, those with specific masks.
520   /// By default, if a target supports the VECTOR_SHUFFLE node, all mask values
521   /// are assumed to be legal.
522   virtual bool isShuffleMaskLegal(SDOperand Mask, MVT::ValueType VT) const {
523     return true;
524   }
525   
526 public:
527
528   //===--------------------------------------------------------------------===//
529   // Lowering methods - These methods must be implemented by targets so that
530   // the SelectionDAGLowering code knows how to lower these.
531   //
532
533   /// LowerArguments - This hook must be implemented to indicate how we should
534   /// lower the arguments for the specified function, into the specified DAG.
535   virtual std::vector<SDOperand>
536   LowerArguments(Function &F, SelectionDAG &DAG) = 0;
537
538   /// LowerCallTo - This hook lowers an abstract call to a function into an
539   /// actual call.  This returns a pair of operands.  The first element is the
540   /// return value for the function (if RetTy is not VoidTy).  The second
541   /// element is the outgoing token chain.
542   typedef std::vector<std::pair<SDOperand, const Type*> > ArgListTy;
543   virtual std::pair<SDOperand, SDOperand>
544   LowerCallTo(SDOperand Chain, const Type *RetTy, bool isVarArg,
545               unsigned CallingConv, bool isTailCall, SDOperand Callee,
546               ArgListTy &Args, SelectionDAG &DAG) = 0;
547
548   /// LowerFrameReturnAddress - This hook lowers a call to llvm.returnaddress or
549   /// llvm.frameaddress (depending on the value of the first argument).  The
550   /// return values are the result pointer and the resultant token chain.  If
551   /// not implemented, both of these intrinsics will return null.
552   virtual std::pair<SDOperand, SDOperand>
553   LowerFrameReturnAddress(bool isFrameAddr, SDOperand Chain, unsigned Depth,
554                           SelectionDAG &DAG);
555
556   /// LowerOperation - This callback is invoked for operations that are 
557   /// unsupported by the target, which are registered to use 'custom' lowering,
558   /// and whose defined values are all legal.
559   /// If the target has no operations that require custom lowering, it need not
560   /// implement this.  The default implementation of this aborts.
561   virtual SDOperand LowerOperation(SDOperand Op, SelectionDAG &DAG);
562
563   /// CustomPromoteOperation - This callback is invoked for operations that are
564   /// unsupported by the target, are registered to use 'custom' lowering, and
565   /// whose type needs to be promoted.
566   virtual SDOperand CustomPromoteOperation(SDOperand Op, SelectionDAG &DAG);
567   
568   /// getTargetNodeName() - This method returns the name of a target specific
569   /// DAG node.
570   virtual const char *getTargetNodeName(unsigned Opcode) const;
571
572   //===--------------------------------------------------------------------===//
573   // Inline Asm Support hooks
574   //
575   
576   enum ConstraintType {
577     C_Register,            // Constraint represents a single register.
578     C_RegisterClass,       // Constraint represents one or more registers.
579     C_Memory,              // Memory constraint.
580     C_Other,               // Something else.
581     C_Unknown              // Unsupported constraint.
582   };
583   
584   /// getConstraintType - Given a constraint letter, return the type of
585   /// constraint it is for this target.
586   virtual ConstraintType getConstraintType(char ConstraintLetter) const;
587   
588   
589   /// getRegClassForInlineAsmConstraint - Given a constraint letter (e.g. "r"),
590   /// return a list of registers that can be used to satisfy the constraint.
591   /// This should only be used for C_RegisterClass constraints.
592   virtual std::vector<unsigned> 
593   getRegClassForInlineAsmConstraint(const std::string &Constraint,
594                                     MVT::ValueType VT) const;
595
596   /// getRegForInlineAsmConstraint - Given a physical register constraint (e.g.
597   /// {edx}), return the register number and the register class for the
598   /// register.  This should only be used for C_Register constraints.  On error,
599   /// this returns a register number of 0.
600   virtual std::pair<unsigned, const TargetRegisterClass*> 
601     getRegForInlineAsmConstraint(const std::string &Constraint,
602                                  MVT::ValueType VT) const;
603   
604   
605   /// isOperandValidForConstraint - Return true if the specified SDOperand is
606   /// valid for the specified target constraint letter.
607   virtual bool isOperandValidForConstraint(SDOperand Op, char ConstraintLetter);
608   
609   //===--------------------------------------------------------------------===//
610   // Scheduler hooks
611   //
612   
613   // InsertAtEndOfBasicBlock - This method should be implemented by targets that
614   // mark instructions with the 'usesCustomDAGSchedInserter' flag.  These
615   // instructions are special in various ways, which require special support to
616   // insert.  The specified MachineInstr is created but not inserted into any
617   // basic blocks, and the scheduler passes ownership of it to this method.
618   virtual MachineBasicBlock *InsertAtEndOfBasicBlock(MachineInstr *MI,
619                                                      MachineBasicBlock *MBB);
620
621   //===--------------------------------------------------------------------===//
622   // Loop Strength Reduction hooks
623   //
624   
625   /// isLegalAddressImmediate - Return true if the integer value or GlobalValue
626   /// can be used as the offset of the target addressing mode.
627   virtual bool isLegalAddressImmediate(int64_t V) const;
628   virtual bool isLegalAddressImmediate(GlobalValue *GV) const;
629
630   typedef std::vector<unsigned>::const_iterator legal_am_scale_iterator;
631   legal_am_scale_iterator legal_am_scale_begin() const {
632     return LegalAddressScales.begin();
633   }
634   legal_am_scale_iterator legal_am_scale_end() const {
635     return LegalAddressScales.end();
636   }
637
638 protected:
639   /// addLegalAddressScale - Add a integer (> 1) value which can be used as
640   /// scale in the target addressing mode. Note: the ordering matters so the
641   /// least efficient ones should be entered first.
642   void addLegalAddressScale(unsigned Scale) {
643     LegalAddressScales.push_back(Scale);
644   }
645
646 private:
647   std::vector<unsigned> LegalAddressScales;
648   
649   TargetMachine &TM;
650   const TargetData &TD;
651
652   /// IsLittleEndian - True if this is a little endian target.
653   ///
654   bool IsLittleEndian;
655
656   /// PointerTy - The type to use for pointers, usually i32 or i64.
657   ///
658   MVT::ValueType PointerTy;
659
660   /// ShiftAmountTy - The type to use for shift amounts, usually i8 or whatever
661   /// PointerTy is.
662   MVT::ValueType ShiftAmountTy;
663
664   OutOfRangeShiftAmount ShiftAmtHandling;
665
666   /// SetCCIsExpensive - This is a short term hack for targets that codegen
667   /// setcc as a conditional branch.  This encourages the code generator to fold
668   /// setcc operations into other operations if possible.
669   bool SetCCIsExpensive;
670
671   /// IntDivIsCheap - Tells the code generator not to expand integer divides by
672   /// constants into a sequence of muls, adds, and shifts.  This is a hack until
673   /// a real cost model is in place.  If we ever optimize for size, this will be
674   /// set to true unconditionally.
675   bool IntDivIsCheap;
676   
677   /// Pow2DivIsCheap - Tells the code generator that it shouldn't generate
678   /// srl/add/sra for a signed divide by power of two, and let the target handle
679   /// it.
680   bool Pow2DivIsCheap;
681   
682   /// SetCCResultTy - The type that SetCC operations use.  This defaults to the
683   /// PointerTy.
684   MVT::ValueType SetCCResultTy;
685
686   /// SetCCResultContents - Information about the contents of the high-bits in
687   /// the result of a setcc comparison operation.
688   SetCCResultValue SetCCResultContents;
689
690   /// SchedPreferenceInfo - The target scheduling preference: shortest possible
691   /// total cycles or lowest register usage.
692   SchedPreference SchedPreferenceInfo;
693   
694   /// UseUnderscoreSetJmpLongJmp - This target prefers to use _setjmp and
695   /// _longjmp to implement llvm.setjmp/llvm.longjmp.  Defaults to false.
696   bool UseUnderscoreSetJmpLongJmp;
697   
698   /// StackPointerRegisterToSaveRestore - If set to a physical register, this
699   /// specifies the register that llvm.savestack/llvm.restorestack should save
700   /// and restore.
701   unsigned StackPointerRegisterToSaveRestore;
702
703   /// RegClassForVT - This indicates the default register class to use for
704   /// each ValueType the target supports natively.
705   TargetRegisterClass *RegClassForVT[MVT::LAST_VALUETYPE];
706   unsigned char NumElementsForVT[MVT::LAST_VALUETYPE];
707
708   /// TransformToType - For any value types we are promoting or expanding, this
709   /// contains the value type that we are changing to.  For Expanded types, this
710   /// contains one step of the expand (e.g. i64 -> i32), even if there are
711   /// multiple steps required (e.g. i64 -> i16).  For types natively supported
712   /// by the system, this holds the same type (e.g. i32 -> i32).
713   MVT::ValueType TransformToType[MVT::LAST_VALUETYPE];
714
715   /// OpActions - For each operation and each value type, keep a LegalizeAction
716   /// that indicates how instruction selection should deal with the operation.
717   /// Most operations are Legal (aka, supported natively by the target), but
718   /// operations that are not should be described.  Note that operations on
719   /// non-legal value types are not described here.
720   uint64_t OpActions[156];
721   
722   ValueTypeActionImpl ValueTypeActions;
723
724   std::vector<double> LegalFPImmediates;
725
726   std::vector<std::pair<MVT::ValueType,
727                         TargetRegisterClass*> > AvailableRegClasses;
728
729   /// TargetDAGCombineArray - Targets can specify ISD nodes that they would
730   /// like PerformDAGCombine callbacks for by calling setTargetDAGCombine(),
731   /// which sets a bit in this array.
732   unsigned char TargetDAGCombineArray[156/(sizeof(unsigned char)*8)];
733   
734   /// PromoteToType - For operations that must be promoted to a specific type,
735   /// this holds the destination type.  This map should be sparse, so don't hold
736   /// it as an array.
737   ///
738   /// Targets add entries to this map with AddPromotedToType(..), clients access
739   /// this with getTypeToPromoteTo(..).
740   std::map<std::pair<unsigned, MVT::ValueType>, MVT::ValueType> PromoteToType;
741   
742 protected:
743   /// When lowering %llvm.memset this field specifies the maximum number of
744   /// store operations that may be substituted for the call to memset. Targets
745   /// must set this value based on the cost threshold for that target. Targets
746   /// should assume that the memset will be done using as many of the largest
747   /// store operations first, followed by smaller ones, if necessary, per
748   /// alignment restrictions. For example, storing 9 bytes on a 32-bit machine
749   /// with 16-bit alignment would result in four 2-byte stores and one 1-byte
750   /// store.  This only applies to setting a constant array of a constant size.
751   /// @brief Specify maximum number of store instructions per memset call.
752   unsigned maxStoresPerMemset;
753
754   /// When lowering %llvm.memcpy this field specifies the maximum number of
755   /// store operations that may be substituted for a call to memcpy. Targets
756   /// must set this value based on the cost threshold for that target. Targets
757   /// should assume that the memcpy will be done using as many of the largest
758   /// store operations first, followed by smaller ones, if necessary, per
759   /// alignment restrictions. For example, storing 7 bytes on a 32-bit machine
760   /// with 32-bit alignment would result in one 4-byte store, a one 2-byte store
761   /// and one 1-byte store. This only applies to copying a constant array of
762   /// constant size.
763   /// @brief Specify maximum bytes of store instructions per memcpy call.
764   unsigned maxStoresPerMemcpy;
765
766   /// When lowering %llvm.memmove this field specifies the maximum number of
767   /// store instructions that may be substituted for a call to memmove. Targets
768   /// must set this value based on the cost threshold for that target. Targets
769   /// should assume that the memmove will be done using as many of the largest
770   /// store operations first, followed by smaller ones, if necessary, per
771   /// alignment restrictions. For example, moving 9 bytes on a 32-bit machine
772   /// with 8-bit alignment would result in nine 1-byte stores.  This only
773   /// applies to copying a constant array of constant size.
774   /// @brief Specify maximum bytes of store instructions per memmove call.
775   unsigned maxStoresPerMemmove;
776
777   /// This field specifies whether the target machine permits unaligned memory
778   /// accesses.  This is used, for example, to determine the size of store 
779   /// operations when copying small arrays and other similar tasks.
780   /// @brief Indicate whether the target permits unaligned memory accesses.
781   bool allowUnalignedMemoryAccesses;
782 };
783 } // end llvm namespace
784
785 #endif