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