Revert "Emit the SubRegTable with the smallest possible integer type."
[oota-llvm.git] / include / llvm / Target / TargetRegisterInfo.h
1 //=== Target/TargetRegisterInfo.h - Target Register Information -*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes an abstract interface used to get information about a
11 // target machines register file.  This information is used for a variety of
12 // purposed, especially register allocation.
13 //
14 //===----------------------------------------------------------------------===//
15
16 #ifndef LLVM_TARGET_TARGETREGISTERINFO_H
17 #define LLVM_TARGET_TARGETREGISTERINFO_H
18
19 #include "llvm/MC/MCRegisterInfo.h"
20 #include "llvm/CodeGen/MachineBasicBlock.h"
21 #include "llvm/CodeGen/ValueTypes.h"
22 #include "llvm/ADT/ArrayRef.h"
23 #include "llvm/CallingConv.h"
24 #include <cassert>
25 #include <functional>
26
27 namespace llvm {
28
29 class BitVector;
30 class MachineFunction;
31 class RegScavenger;
32 template<class T> class SmallVectorImpl;
33 class raw_ostream;
34
35 class TargetRegisterClass {
36 public:
37   typedef const unsigned* iterator;
38   typedef const unsigned* const_iterator;
39   typedef const MVT::SimpleValueType* vt_iterator;
40   typedef const TargetRegisterClass* const * sc_iterator;
41
42   // Instance variables filled by tablegen, do not use!
43   const MCRegisterClass *MC;
44   const vt_iterator VTs;
45   const unsigned *SubClassMask;
46   const sc_iterator SuperClasses;
47   const sc_iterator SuperRegClasses;
48   ArrayRef<unsigned> (*OrderFunc)(const MachineFunction&);
49
50   /// getID() - Return the register class ID number.
51   ///
52   unsigned getID() const { return MC->getID(); }
53
54   /// getName() - Return the register class name for debugging.
55   ///
56   const char *getName() const { return MC->getName(); }
57
58   /// begin/end - Return all of the registers in this class.
59   ///
60   iterator       begin() const { return MC->begin(); }
61   iterator         end() const { return MC->end(); }
62
63   /// getNumRegs - Return the number of registers in this class.
64   ///
65   unsigned getNumRegs() const { return MC->getNumRegs(); }
66
67   /// getRegister - Return the specified register in the class.
68   ///
69   unsigned getRegister(unsigned i) const {
70     return MC->getRegister(i);
71   }
72
73   /// contains - Return true if the specified register is included in this
74   /// register class.  This does not include virtual registers.
75   bool contains(unsigned Reg) const {
76     return MC->contains(Reg);
77   }
78
79   /// contains - Return true if both registers are in this class.
80   bool contains(unsigned Reg1, unsigned Reg2) const {
81     return MC->contains(Reg1, Reg2);
82   }
83
84   /// getSize - Return the size of the register in bytes, which is also the size
85   /// of a stack slot allocated to hold a spilled copy of this register.
86   unsigned getSize() const { return MC->getSize(); }
87
88   /// getAlignment - Return the minimum required alignment for a register of
89   /// this class.
90   unsigned getAlignment() const { return MC->getAlignment(); }
91
92   /// getCopyCost - Return the cost of copying a value between two registers in
93   /// this class. A negative number means the register class is very expensive
94   /// to copy e.g. status flag register classes.
95   int getCopyCost() const { return MC->getCopyCost(); }
96
97   /// isAllocatable - Return true if this register class may be used to create
98   /// virtual registers.
99   bool isAllocatable() const { return MC->isAllocatable(); }
100
101   /// hasType - return true if this TargetRegisterClass has the ValueType vt.
102   ///
103   bool hasType(EVT vt) const {
104     for(int i = 0; VTs[i] != MVT::Other; ++i)
105       if (EVT(VTs[i]) == vt)
106         return true;
107     return false;
108   }
109
110   /// vt_begin / vt_end - Loop over all of the value types that can be
111   /// represented by values in this register class.
112   vt_iterator vt_begin() const {
113     return VTs;
114   }
115
116   vt_iterator vt_end() const {
117     vt_iterator I = VTs;
118     while (*I != MVT::Other) ++I;
119     return I;
120   }
121
122   /// superregclasses_begin / superregclasses_end - Loop over all of
123   /// the superreg register classes of this register class.
124   sc_iterator superregclasses_begin() const {
125     return SuperRegClasses;
126   }
127
128   sc_iterator superregclasses_end() const {
129     sc_iterator I = SuperRegClasses;
130     while (*I != NULL) ++I;
131     return I;
132   }
133
134   /// hasSubClass - return true if the specified TargetRegisterClass
135   /// is a proper sub-class of this TargetRegisterClass.
136   bool hasSubClass(const TargetRegisterClass *RC) const {
137     return RC != this && hasSubClassEq(RC);
138   }
139
140   /// hasSubClassEq - Returns true if RC is a sub-class of or equal to this
141   /// class.
142   bool hasSubClassEq(const TargetRegisterClass *RC) const {
143     unsigned ID = RC->getID();
144     return (SubClassMask[ID / 32] >> (ID % 32)) & 1;
145   }
146
147   /// hasSuperClass - return true if the specified TargetRegisterClass is a
148   /// proper super-class of this TargetRegisterClass.
149   bool hasSuperClass(const TargetRegisterClass *RC) const {
150     return RC->hasSubClass(this);
151   }
152
153   /// hasSuperClassEq - Returns true if RC is a super-class of or equal to this
154   /// class.
155   bool hasSuperClassEq(const TargetRegisterClass *RC) const {
156     return RC->hasSubClassEq(this);
157   }
158
159   /// getSubClassMask - Returns a bit vector of subclasses, including this one.
160   /// The vector is indexed by class IDs, see hasSubClassEq() above for how to
161   /// use it.
162   const unsigned *getSubClassMask() const {
163     return SubClassMask;
164   }
165
166   /// getSuperClasses - Returns a NULL terminated list of super-classes.  The
167   /// classes are ordered by ID which is also a topological ordering from large
168   /// to small classes.  The list does NOT include the current class.
169   sc_iterator getSuperClasses() const {
170     return SuperClasses;
171   }
172
173   /// isASubClass - return true if this TargetRegisterClass is a subset
174   /// class of at least one other TargetRegisterClass.
175   bool isASubClass() const {
176     return SuperClasses[0] != 0;
177   }
178
179   /// getRawAllocationOrder - Returns the preferred order for allocating
180   /// registers from this register class in MF. The raw order comes directly
181   /// from the .td file and may include reserved registers that are not
182   /// allocatable. Register allocators should also make sure to allocate
183   /// callee-saved registers only after all the volatiles are used. The
184   /// RegisterClassInfo class provides filtered allocation orders with
185   /// callee-saved registers moved to the end.
186   ///
187   /// The MachineFunction argument can be used to tune the allocatable
188   /// registers based on the characteristics of the function, subtarget, or
189   /// other criteria.
190   ///
191   /// By default, this method returns all registers in the class.
192   ///
193   ArrayRef<unsigned> getRawAllocationOrder(const MachineFunction &MF) const {
194     return OrderFunc ? OrderFunc(MF) : makeArrayRef(begin(), getNumRegs());
195   }
196 };
197
198 /// TargetRegisterInfoDesc - Extra information, not in MCRegisterDesc, about
199 /// registers. These are used by codegen, not by MC.
200 struct TargetRegisterInfoDesc {
201   unsigned CostPerUse;          // Extra cost of instructions using register.
202   bool inAllocatableClass;      // Register belongs to an allocatable regclass.
203 };
204
205 /// TargetRegisterInfo base class - We assume that the target defines a static
206 /// array of TargetRegisterDesc objects that represent all of the machine
207 /// registers that the target has.  As such, we simply have to track a pointer
208 /// to this array so that we can turn register number into a register
209 /// descriptor.
210 ///
211 class TargetRegisterInfo : public MCRegisterInfo {
212 public:
213   typedef const TargetRegisterClass * const * regclass_iterator;
214 private:
215   const TargetRegisterInfoDesc *InfoDesc;     // Extra desc array for codegen
216   const char *const *SubRegIndexNames;        // Names of subreg indexes.
217   regclass_iterator RegClassBegin, RegClassEnd;   // List of regclasses
218
219 protected:
220   TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
221                      regclass_iterator RegClassBegin,
222                      regclass_iterator RegClassEnd,
223                      const char *const *subregindexnames);
224   virtual ~TargetRegisterInfo();
225 public:
226
227   // Register numbers can represent physical registers, virtual registers, and
228   // sometimes stack slots. The unsigned values are divided into these ranges:
229   //
230   //   0           Not a register, can be used as a sentinel.
231   //   [1;2^30)    Physical registers assigned by TableGen.
232   //   [2^30;2^31) Stack slots. (Rarely used.)
233   //   [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
234   //
235   // Further sentinels can be allocated from the small negative integers.
236   // DenseMapInfo<unsigned> uses -1u and -2u.
237
238   /// isStackSlot - Sometimes it is useful the be able to store a non-negative
239   /// frame index in a variable that normally holds a register. isStackSlot()
240   /// returns true if Reg is in the range used for stack slots.
241   ///
242   /// Note that isVirtualRegister() and isPhysicalRegister() cannot handle stack
243   /// slots, so if a variable may contains a stack slot, always check
244   /// isStackSlot() first.
245   ///
246   static bool isStackSlot(unsigned Reg) {
247     return int(Reg) >= (1 << 30);
248   }
249
250   /// stackSlot2Index - Compute the frame index from a register value
251   /// representing a stack slot.
252   static int stackSlot2Index(unsigned Reg) {
253     assert(isStackSlot(Reg) && "Not a stack slot");
254     return int(Reg - (1u << 30));
255   }
256
257   /// index2StackSlot - Convert a non-negative frame index to a stack slot
258   /// register value.
259   static unsigned index2StackSlot(int FI) {
260     assert(FI >= 0 && "Cannot hold a negative frame index.");
261     return FI + (1u << 30);
262   }
263
264   /// isPhysicalRegister - Return true if the specified register number is in
265   /// the physical register namespace.
266   static bool isPhysicalRegister(unsigned Reg) {
267     assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
268     return int(Reg) > 0;
269   }
270
271   /// isVirtualRegister - Return true if the specified register number is in
272   /// the virtual register namespace.
273   static bool isVirtualRegister(unsigned Reg) {
274     assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
275     return int(Reg) < 0;
276   }
277
278   /// virtReg2Index - Convert a virtual register number to a 0-based index.
279   /// The first virtual register in a function will get the index 0.
280   static unsigned virtReg2Index(unsigned Reg) {
281     assert(isVirtualRegister(Reg) && "Not a virtual register");
282     return Reg & ~(1u << 31);
283   }
284
285   /// index2VirtReg - Convert a 0-based index to a virtual register number.
286   /// This is the inverse operation of VirtReg2IndexFunctor below.
287   static unsigned index2VirtReg(unsigned Index) {
288     return Index | (1u << 31);
289   }
290
291   /// getMinimalPhysRegClass - Returns the Register Class of a physical
292   /// register of the given type, picking the most sub register class of
293   /// the right type that contains this physreg.
294   const TargetRegisterClass *
295     getMinimalPhysRegClass(unsigned Reg, EVT VT = MVT::Other) const;
296
297   /// getAllocatableSet - Returns a bitset indexed by register number
298   /// indicating if a register is allocatable or not. If a register class is
299   /// specified, returns the subset for the class.
300   BitVector getAllocatableSet(const MachineFunction &MF,
301                               const TargetRegisterClass *RC = NULL) const;
302
303   /// getCostPerUse - Return the additional cost of using this register instead
304   /// of other registers in its class.
305   unsigned getCostPerUse(unsigned RegNo) const {
306     return InfoDesc[RegNo].CostPerUse;
307   }
308
309   /// isInAllocatableClass - Return true if the register is in the allocation
310   /// of any register class.
311   bool isInAllocatableClass(unsigned RegNo) const {
312     return InfoDesc[RegNo].inAllocatableClass;
313   }
314
315   /// getSubRegIndexName - Return the human-readable symbolic target-specific
316   /// name for the specified SubRegIndex.
317   const char *getSubRegIndexName(unsigned SubIdx) const {
318     assert(SubIdx && "This is not a subregister index");
319     return SubRegIndexNames[SubIdx-1];
320   }
321
322   /// regsOverlap - Returns true if the two registers are equal or alias each
323   /// other. The registers may be virtual register.
324   bool regsOverlap(unsigned regA, unsigned regB) const {
325     if (regA == regB) return true;
326     if (isVirtualRegister(regA) || isVirtualRegister(regB))
327       return false;
328     for (const unsigned *regList = getOverlaps(regA)+1; *regList; ++regList) {
329       if (*regList == regB) return true;
330     }
331     return false;
332   }
333
334   /// isSubRegister - Returns true if regB is a sub-register of regA.
335   ///
336   bool isSubRegister(unsigned regA, unsigned regB) const {
337     return isSuperRegister(regB, regA);
338   }
339
340   /// isSuperRegister - Returns true if regB is a super-register of regA.
341   ///
342   bool isSuperRegister(unsigned regA, unsigned regB) const {
343     for (const unsigned *regList = getSuperRegisters(regA); *regList;++regList){
344       if (*regList == regB) return true;
345     }
346     return false;
347   }
348
349   /// getCalleeSavedRegs - Return a null-terminated list of all of the
350   /// callee saved registers on this target. The register should be in the
351   /// order of desired callee-save stack frame offset. The first register is
352   /// closest to the incoming stack pointer if stack grows down, and vice versa.
353   ///
354   virtual const unsigned* getCalleeSavedRegs(const MachineFunction *MF = 0)
355                                                                       const = 0;
356
357   /// getCallPreservedMask - Return a mask of call-preserved registers for the
358   /// given calling convention on the current sub-target.  The mask should
359   /// include all call-preserved aliases.  This is used by the register
360   /// allocator to determine which registers can be live across a call.
361   ///
362   /// The mask is an array containing (TRI::getNumRegs()+31)/32 entries.
363   /// A set bit indicates that all bits of the corresponding register are
364   /// preserved across the function call.  The bit mask is expected to be
365   /// sub-register complete, i.e. if A is preserved, so are all its
366   /// sub-registers.
367   ///
368   /// Bits are numbered from the LSB, so the bit for physical register Reg can
369   /// be found as (Mask[Reg / 32] >> Reg % 32) & 1.
370   ///
371   /// A NULL pointer means that no register mask will be used, and call
372   /// instructions should use implicit-def operands to indicate call clobbered
373   /// registers.
374   ///
375   virtual const uint32_t *getCallPreservedMask(CallingConv::ID) const {
376     // The default mask clobbers everything.  All targets should override.
377     return 0;
378   }
379
380   /// getReservedRegs - Returns a bitset indexed by physical register number
381   /// indicating if a register is a special register that has particular uses
382   /// and should be considered unavailable at all times, e.g. SP, RA. This is
383   /// used by register scavenger to determine what registers are free.
384   virtual BitVector getReservedRegs(const MachineFunction &MF) const = 0;
385
386   /// getSubReg - Returns the physical register number of sub-register "Index"
387   /// for physical register RegNo. Return zero if the sub-register does not
388   /// exist.
389   virtual unsigned getSubReg(unsigned RegNo, unsigned Index) const = 0;
390
391   /// getSubRegIndex - For a given register pair, return the sub-register index
392   /// if the second register is a sub-register of the first. Return zero
393   /// otherwise.
394   virtual unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const = 0;
395
396   /// getMatchingSuperReg - Return a super-register of the specified register
397   /// Reg so its sub-register of index SubIdx is Reg.
398   unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
399                                const TargetRegisterClass *RC) const {
400     for (const unsigned *SRs = getSuperRegisters(Reg); unsigned SR = *SRs;++SRs)
401       if (Reg == getSubReg(SR, SubIdx) && RC->contains(SR))
402         return SR;
403     return 0;
404   }
405
406   /// canCombineSubRegIndices - Given a register class and a list of
407   /// subregister indices, return true if it's possible to combine the
408   /// subregister indices into one that corresponds to a larger
409   /// subregister. Return the new subregister index by reference. Note the
410   /// new index may be zero if the given subregisters can be combined to
411   /// form the whole register.
412   virtual bool canCombineSubRegIndices(const TargetRegisterClass *RC,
413                                        SmallVectorImpl<unsigned> &SubIndices,
414                                        unsigned &NewSubIdx) const {
415     return 0;
416   }
417
418   /// getMatchingSuperRegClass - Return a subclass of the specified register
419   /// class A so that each register in it has a sub-register of the
420   /// specified sub-register index which is in the specified register class B.
421   ///
422   /// TableGen will synthesize missing A sub-classes.
423   virtual const TargetRegisterClass *
424   getMatchingSuperRegClass(const TargetRegisterClass *A,
425                            const TargetRegisterClass *B, unsigned Idx) const =0;
426
427   /// getSubClassWithSubReg - Returns the largest legal sub-class of RC that
428   /// supports the sub-register index Idx.
429   /// If no such sub-class exists, return NULL.
430   /// If all registers in RC already have an Idx sub-register, return RC.
431   ///
432   /// TableGen generates a version of this function that is good enough in most
433   /// cases.  Targets can override if they have constraints that TableGen
434   /// doesn't understand.  For example, the x86 sub_8bit sub-register index is
435   /// supported by the full GR32 register class in 64-bit mode, but only by the
436   /// GR32_ABCD regiister class in 32-bit mode.
437   ///
438   /// TableGen will synthesize missing RC sub-classes.
439   virtual const TargetRegisterClass *
440   getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx) const =0;
441
442   /// composeSubRegIndices - Return the subregister index you get from composing
443   /// two subregister indices.
444   ///
445   /// If R:a:b is the same register as R:c, then composeSubRegIndices(a, b)
446   /// returns c. Note that composeSubRegIndices does not tell you about illegal
447   /// compositions. If R does not have a subreg a, or R:a does not have a subreg
448   /// b, composeSubRegIndices doesn't tell you.
449   ///
450   /// The ARM register Q0 has two D subregs dsub_0:D0 and dsub_1:D1. It also has
451   /// ssub_0:S0 - ssub_3:S3 subregs.
452   /// If you compose subreg indices dsub_1, ssub_0 you get ssub_2.
453   ///
454   virtual unsigned composeSubRegIndices(unsigned a, unsigned b) const {
455     // This default implementation is correct for most targets.
456     return b;
457   }
458
459   //===--------------------------------------------------------------------===//
460   // Register Class Information
461   //
462
463   /// Register class iterators
464   ///
465   regclass_iterator regclass_begin() const { return RegClassBegin; }
466   regclass_iterator regclass_end() const { return RegClassEnd; }
467
468   unsigned getNumRegClasses() const {
469     return (unsigned)(regclass_end()-regclass_begin());
470   }
471
472   /// getRegClass - Returns the register class associated with the enumeration
473   /// value.  See class MCOperandInfo.
474   const TargetRegisterClass *getRegClass(unsigned i) const {
475     assert(i < getNumRegClasses() && "Register Class ID out of range");
476     return RegClassBegin[i];
477   }
478
479   /// getCommonSubClass - find the largest common subclass of A and B. Return
480   /// NULL if there is no common subclass.
481   const TargetRegisterClass *
482   getCommonSubClass(const TargetRegisterClass *A,
483                     const TargetRegisterClass *B) const;
484
485   /// getPointerRegClass - Returns a TargetRegisterClass used for pointer
486   /// values.  If a target supports multiple different pointer register classes,
487   /// kind specifies which one is indicated.
488   virtual const TargetRegisterClass *getPointerRegClass(unsigned Kind=0) const {
489     llvm_unreachable("Target didn't implement getPointerRegClass!");
490   }
491
492   /// getCrossCopyRegClass - Returns a legal register class to copy a register
493   /// in the specified class to or from. If it is possible to copy the register
494   /// directly without using a cross register class copy, return the specified
495   /// RC. Returns NULL if it is not possible to copy between a two registers of
496   /// the specified class.
497   virtual const TargetRegisterClass *
498   getCrossCopyRegClass(const TargetRegisterClass *RC) const {
499     return RC;
500   }
501
502   /// getLargestLegalSuperClass - Returns the largest super class of RC that is
503   /// legal to use in the current sub-target and has the same spill size.
504   /// The returned register class can be used to create virtual registers which
505   /// means that all its registers can be copied and spilled.
506   virtual const TargetRegisterClass*
507   getLargestLegalSuperClass(const TargetRegisterClass *RC) const {
508     /// The default implementation is very conservative and doesn't allow the
509     /// register allocator to inflate register classes.
510     return RC;
511   }
512
513   /// getRegPressureLimit - Return the register pressure "high water mark" for
514   /// the specific register class. The scheduler is in high register pressure
515   /// mode (for the specific register class) if it goes over the limit.
516   virtual unsigned getRegPressureLimit(const TargetRegisterClass *RC,
517                                        MachineFunction &MF) const {
518     return 0;
519   }
520
521   /// getRawAllocationOrder - Returns the register allocation order for a
522   /// specified register class with a target-dependent hint. The returned list
523   /// may contain reserved registers that cannot be allocated.
524   ///
525   /// Register allocators need only call this function to resolve
526   /// target-dependent hints, but it should work without hinting as well.
527   virtual ArrayRef<unsigned>
528   getRawAllocationOrder(const TargetRegisterClass *RC,
529                         unsigned HintType, unsigned HintReg,
530                         const MachineFunction &MF) const {
531     return RC->getRawAllocationOrder(MF);
532   }
533
534   /// ResolveRegAllocHint - Resolves the specified register allocation hint
535   /// to a physical register. Returns the physical register if it is successful.
536   virtual unsigned ResolveRegAllocHint(unsigned Type, unsigned Reg,
537                                        const MachineFunction &MF) const {
538     if (Type == 0 && Reg && isPhysicalRegister(Reg))
539       return Reg;
540     return 0;
541   }
542
543   /// avoidWriteAfterWrite - Return true if the register allocator should avoid
544   /// writing a register from RC in two consecutive instructions.
545   /// This can avoid pipeline stalls on certain architectures.
546   /// It does cause increased register pressure, though.
547   virtual bool avoidWriteAfterWrite(const TargetRegisterClass *RC) const {
548     return false;
549   }
550
551   /// UpdateRegAllocHint - A callback to allow target a chance to update
552   /// register allocation hints when a register is "changed" (e.g. coalesced)
553   /// to another register. e.g. On ARM, some virtual registers should target
554   /// register pairs, if one of pair is coalesced to another register, the
555   /// allocation hint of the other half of the pair should be changed to point
556   /// to the new register.
557   virtual void UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
558                                   MachineFunction &MF) const {
559     // Do nothing.
560   }
561
562   /// requiresRegisterScavenging - returns true if the target requires (and can
563   /// make use of) the register scavenger.
564   virtual bool requiresRegisterScavenging(const MachineFunction &MF) const {
565     return false;
566   }
567
568   /// useFPForScavengingIndex - returns true if the target wants to use
569   /// frame pointer based accesses to spill to the scavenger emergency spill
570   /// slot.
571   virtual bool useFPForScavengingIndex(const MachineFunction &MF) const {
572     return true;
573   }
574
575   /// requiresFrameIndexScavenging - returns true if the target requires post
576   /// PEI scavenging of registers for materializing frame index constants.
577   virtual bool requiresFrameIndexScavenging(const MachineFunction &MF) const {
578     return false;
579   }
580
581   /// requiresVirtualBaseRegisters - Returns true if the target wants the
582   /// LocalStackAllocation pass to be run and virtual base registers
583   /// used for more efficient stack access.
584   virtual bool requiresVirtualBaseRegisters(const MachineFunction &MF) const {
585     return false;
586   }
587
588   /// hasReservedSpillSlot - Return true if target has reserved a spill slot in
589   /// the stack frame of the given function for the specified register. e.g. On
590   /// x86, if the frame register is required, the first fixed stack object is
591   /// reserved as its spill slot. This tells PEI not to create a new stack frame
592   /// object for the given register. It should be called only after
593   /// processFunctionBeforeCalleeSavedScan().
594   virtual bool hasReservedSpillSlot(const MachineFunction &MF, unsigned Reg,
595                                     int &FrameIdx) const {
596     return false;
597   }
598
599   /// needsStackRealignment - true if storage within the function requires the
600   /// stack pointer to be aligned more than the normal calling convention calls
601   /// for.
602   virtual bool needsStackRealignment(const MachineFunction &MF) const {
603     return false;
604   }
605
606   /// getFrameIndexInstrOffset - Get the offset from the referenced frame
607   /// index in the instruction, if there is one.
608   virtual int64_t getFrameIndexInstrOffset(const MachineInstr *MI,
609                                            int Idx) const {
610     return 0;
611   }
612
613   /// needsFrameBaseReg - Returns true if the instruction's frame index
614   /// reference would be better served by a base register other than FP
615   /// or SP. Used by LocalStackFrameAllocation to determine which frame index
616   /// references it should create new base registers for.
617   virtual bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
618     return false;
619   }
620
621   /// materializeFrameBaseRegister - Insert defining instruction(s) for
622   /// BaseReg to be a pointer to FrameIdx before insertion point I.
623   virtual void materializeFrameBaseRegister(MachineBasicBlock *MBB,
624                                             unsigned BaseReg, int FrameIdx,
625                                             int64_t Offset) const {
626     llvm_unreachable("materializeFrameBaseRegister does not exist on this "
627                      "target");
628   }
629
630   /// resolveFrameIndex - Resolve a frame index operand of an instruction
631   /// to reference the indicated base register plus offset instead.
632   virtual void resolveFrameIndex(MachineBasicBlock::iterator I,
633                                  unsigned BaseReg, int64_t Offset) const {
634     llvm_unreachable("resolveFrameIndex does not exist on this target");
635   }
636
637   /// isFrameOffsetLegal - Determine whether a given offset immediate is
638   /// encodable to resolve a frame index.
639   virtual bool isFrameOffsetLegal(const MachineInstr *MI,
640                                   int64_t Offset) const {
641     llvm_unreachable("isFrameOffsetLegal does not exist on this target");
642   }
643
644   /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
645   /// code insertion to eliminate call frame setup and destroy pseudo
646   /// instructions (but only if the Target is using them).  It is responsible
647   /// for eliminating these instructions, replacing them with concrete
648   /// instructions.  This method need only be implemented if using call frame
649   /// setup/destroy pseudo instructions.
650   ///
651   virtual void
652   eliminateCallFramePseudoInstr(MachineFunction &MF,
653                                 MachineBasicBlock &MBB,
654                                 MachineBasicBlock::iterator MI) const {
655     llvm_unreachable("Call Frame Pseudo Instructions do not exist on this "
656                      "target!");
657   }
658
659
660   /// saveScavengerRegister - Spill the register so it can be used by the
661   /// register scavenger. Return true if the register was spilled, false
662   /// otherwise. If this function does not spill the register, the scavenger
663   /// will instead spill it to the emergency spill slot.
664   ///
665   virtual bool saveScavengerRegister(MachineBasicBlock &MBB,
666                                      MachineBasicBlock::iterator I,
667                                      MachineBasicBlock::iterator &UseMI,
668                                      const TargetRegisterClass *RC,
669                                      unsigned Reg) const {
670     return false;
671   }
672
673   /// eliminateFrameIndex - This method must be overriden to eliminate abstract
674   /// frame indices from instructions which may use them.  The instruction
675   /// referenced by the iterator contains an MO_FrameIndex operand which must be
676   /// eliminated by this method.  This method may modify or replace the
677   /// specified instruction, as long as it keeps the iterator pointing at the
678   /// finished product. SPAdj is the SP adjustment due to call frame setup
679   /// instruction.
680   virtual void eliminateFrameIndex(MachineBasicBlock::iterator MI,
681                                    int SPAdj, RegScavenger *RS=NULL) const = 0;
682
683   //===--------------------------------------------------------------------===//
684   /// Debug information queries.
685
686   /// getFrameRegister - This method should return the register used as a base
687   /// for values allocated in the current stack frame.
688   virtual unsigned getFrameRegister(const MachineFunction &MF) const = 0;
689
690   /// getCompactUnwindRegNum - This function maps the register to the number for
691   /// compact unwind encoding. Return -1 if the register isn't valid.
692   virtual int getCompactUnwindRegNum(unsigned, bool) const {
693     return -1;
694   }
695 };
696
697
698 // This is useful when building IndexedMaps keyed on virtual registers
699 struct VirtReg2IndexFunctor : public std::unary_function<unsigned, unsigned> {
700   unsigned operator()(unsigned Reg) const {
701     return TargetRegisterInfo::virtReg2Index(Reg);
702   }
703 };
704
705 /// PrintReg - Helper class for printing registers on a raw_ostream.
706 /// Prints virtual and physical registers with or without a TRI instance.
707 ///
708 /// The format is:
709 ///   %noreg          - NoRegister
710 ///   %vreg5          - a virtual register.
711 ///   %vreg5:sub_8bit - a virtual register with sub-register index (with TRI).
712 ///   %EAX            - a physical register
713 ///   %physreg17      - a physical register when no TRI instance given.
714 ///
715 /// Usage: OS << PrintReg(Reg, TRI) << '\n';
716 ///
717 class PrintReg {
718   const TargetRegisterInfo *TRI;
719   unsigned Reg;
720   unsigned SubIdx;
721 public:
722   PrintReg(unsigned reg, const TargetRegisterInfo *tri = 0, unsigned subidx = 0)
723     : TRI(tri), Reg(reg), SubIdx(subidx) {}
724   void print(raw_ostream&) const;
725 };
726
727 static inline raw_ostream &operator<<(raw_ostream &OS, const PrintReg &PR) {
728   PR.print(OS);
729   return OS;
730 }
731
732 } // End llvm namespace
733
734 #endif