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