Drop the SuperregHashTable. It is essentially the same as SubregHashTable.
[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/CodeGen/MachineBasicBlock.h"
20 #include "llvm/CodeGen/ValueTypes.h"
21 #include "llvm/ADT/DenseSet.h"
22 #include <cassert>
23 #include <functional>
24
25 namespace llvm {
26
27 class BitVector;
28 class MachineFunction;
29 class MachineMove;
30 class RegScavenger;
31 template<class T> class SmallVectorImpl;
32
33 /// TargetRegisterDesc - This record contains all of the information known about
34 /// a particular register.  The AliasSet field (if not null) contains a pointer
35 /// to a Zero terminated array of registers that this register aliases.  This is
36 /// needed for architectures like X86 which have AL alias AX alias EAX.
37 /// Registers that this does not apply to simply should set this to null.
38 /// The SubRegs field is a zero terminated array of registers that are
39 /// sub-registers of the specific register, e.g. AL, AH are sub-registers of AX.
40 /// The SuperRegs field is a zero terminated array of registers that are
41 /// super-registers of the specific register, e.g. RAX, EAX, are super-registers
42 /// of AX.
43 ///
44 struct TargetRegisterDesc {
45   const char     *Name;         // Printable name for the reg (for debugging)
46   const unsigned *AliasSet;     // Register Alias Set, described above
47   const unsigned *SubRegs;      // Sub-register set, described above
48   const unsigned *SuperRegs;    // Super-register set, described above
49 };
50
51 class TargetRegisterClass {
52 public:
53   typedef const unsigned* iterator;
54   typedef const unsigned* const_iterator;
55
56   typedef const EVT* vt_iterator;
57   typedef const TargetRegisterClass* const * sc_iterator;
58 private:
59   unsigned ID;
60   const char *Name;
61   const vt_iterator VTs;
62   const sc_iterator SubClasses;
63   const sc_iterator SuperClasses;
64   const sc_iterator SubRegClasses;
65   const sc_iterator SuperRegClasses;
66   const unsigned RegSize, Alignment;    // Size & Alignment of register in bytes
67   const int CopyCost;
68   const iterator RegsBegin, RegsEnd;
69   DenseSet<unsigned> RegSet;
70 public:
71   TargetRegisterClass(unsigned id,
72                       const char *name,
73                       const EVT *vts,
74                       const TargetRegisterClass * const *subcs,
75                       const TargetRegisterClass * const *supcs,
76                       const TargetRegisterClass * const *subregcs,
77                       const TargetRegisterClass * const *superregcs,
78                       unsigned RS, unsigned Al, int CC,
79                       iterator RB, iterator RE)
80     : ID(id), Name(name), VTs(vts), SubClasses(subcs), SuperClasses(supcs),
81     SubRegClasses(subregcs), SuperRegClasses(superregcs),
82     RegSize(RS), Alignment(Al), CopyCost(CC), RegsBegin(RB), RegsEnd(RE) {
83       for (iterator I = RegsBegin, E = RegsEnd; I != E; ++I)
84         RegSet.insert(*I);
85     }
86   virtual ~TargetRegisterClass() {}     // Allow subclasses
87
88   /// getID() - Return the register class ID number.
89   ///
90   unsigned getID() const { return ID; }
91
92   /// getName() - Return the register class name for debugging.
93   ///
94   const char *getName() const { return Name; }
95
96   /// begin/end - Return all of the registers in this class.
97   ///
98   iterator       begin() const { return RegsBegin; }
99   iterator         end() const { return RegsEnd; }
100
101   /// getNumRegs - Return the number of registers in this class.
102   ///
103   unsigned getNumRegs() const { return (unsigned)(RegsEnd-RegsBegin); }
104
105   /// getRegister - Return the specified register in the class.
106   ///
107   unsigned getRegister(unsigned i) const {
108     assert(i < getNumRegs() && "Register number out of range!");
109     return RegsBegin[i];
110   }
111
112   /// contains - Return true if the specified register is included in this
113   /// register class.  This does not include virtual registers.
114   bool contains(unsigned Reg) const {
115     return RegSet.count(Reg);
116   }
117
118   /// hasType - return true if this TargetRegisterClass has the ValueType vt.
119   ///
120   bool hasType(EVT vt) const {
121     for(int i = 0; VTs[i].getSimpleVT().SimpleTy != MVT::Other; ++i)
122       if (VTs[i] == vt)
123         return true;
124     return false;
125   }
126
127   /// vt_begin / vt_end - Loop over all of the value types that can be
128   /// represented by values in this register class.
129   vt_iterator vt_begin() const {
130     return VTs;
131   }
132
133   vt_iterator vt_end() const {
134     vt_iterator I = VTs;
135     while (I->getSimpleVT().SimpleTy != MVT::Other) ++I;
136     return I;
137   }
138
139   /// subregclasses_begin / subregclasses_end - Loop over all of
140   /// the subreg register classes of this register class.
141   sc_iterator subregclasses_begin() const {
142     return SubRegClasses;
143   }
144
145   sc_iterator subregclasses_end() const {
146     sc_iterator I = SubRegClasses;
147     while (*I != NULL) ++I;
148     return I;
149   }
150
151   /// getSubRegisterRegClass - Return the register class of subregisters with
152   /// index SubIdx, or NULL if no such class exists.
153   const TargetRegisterClass* getSubRegisterRegClass(unsigned SubIdx) const {
154     assert(SubIdx>0 && "Invalid subregister index");
155     return SubRegClasses[SubIdx-1];
156   }
157
158   /// superregclasses_begin / superregclasses_end - Loop over all of
159   /// the superreg register classes of this register class.
160   sc_iterator superregclasses_begin() const {
161     return SuperRegClasses;
162   }
163
164   sc_iterator superregclasses_end() const {
165     sc_iterator I = SuperRegClasses;
166     while (*I != NULL) ++I;
167     return I;
168   }
169
170   /// hasSubClass - return true if the specified TargetRegisterClass
171   /// is a proper subset of this TargetRegisterClass.
172   bool hasSubClass(const TargetRegisterClass *cs) const {
173     for (int i = 0; SubClasses[i] != NULL; ++i)
174       if (SubClasses[i] == cs)
175         return true;
176     return false;
177   }
178
179   /// subclasses_begin / subclasses_end - Loop over all of the classes
180   /// that are proper subsets of this register class.
181   sc_iterator subclasses_begin() const {
182     return SubClasses;
183   }
184
185   sc_iterator subclasses_end() const {
186     sc_iterator I = SubClasses;
187     while (*I != NULL) ++I;
188     return I;
189   }
190
191   /// hasSuperClass - return true if the specified TargetRegisterClass is a
192   /// proper superset of this TargetRegisterClass.
193   bool hasSuperClass(const TargetRegisterClass *cs) const {
194     for (int i = 0; SuperClasses[i] != NULL; ++i)
195       if (SuperClasses[i] == cs)
196         return true;
197     return false;
198   }
199
200   /// superclasses_begin / superclasses_end - Loop over all of the classes
201   /// that are proper supersets of this register class.
202   sc_iterator superclasses_begin() const {
203     return SuperClasses;
204   }
205
206   sc_iterator superclasses_end() const {
207     sc_iterator I = SuperClasses;
208     while (*I != NULL) ++I;
209     return I;
210   }
211
212   /// isASubClass - return true if this TargetRegisterClass is a subset
213   /// class of at least one other TargetRegisterClass.
214   bool isASubClass() const {
215     return SuperClasses[0] != 0;
216   }
217
218   /// allocation_order_begin/end - These methods define a range of registers
219   /// which specify the registers in this class that are valid to register
220   /// allocate, and the preferred order to allocate them in.  For example,
221   /// callee saved registers should be at the end of the list, because it is
222   /// cheaper to allocate caller saved registers.
223   ///
224   /// These methods take a MachineFunction argument, which can be used to tune
225   /// the allocatable registers based on the characteristics of the function.
226   /// One simple example is that the frame pointer register can be used if
227   /// frame-pointer-elimination is performed.
228   ///
229   /// By default, these methods return all registers in the class.
230   ///
231   virtual iterator allocation_order_begin(const MachineFunction &MF) const {
232     return begin();
233   }
234   virtual iterator allocation_order_end(const MachineFunction &MF)   const {
235     return end();
236   }
237
238   /// getSize - Return the size of the register in bytes, which is also the size
239   /// of a stack slot allocated to hold a spilled copy of this register.
240   unsigned getSize() const { return RegSize; }
241
242   /// getAlignment - Return the minimum required alignment for a register of
243   /// this class.
244   unsigned getAlignment() const { return Alignment; }
245
246   /// getCopyCost - Return the cost of copying a value between two registers in
247   /// this class. A negative number means the register class is very expensive
248   /// to copy e.g. status flag register classes.
249   int getCopyCost() const { return CopyCost; }
250 };
251
252
253 /// TargetRegisterInfo base class - We assume that the target defines a static
254 /// array of TargetRegisterDesc objects that represent all of the machine
255 /// registers that the target has.  As such, we simply have to track a pointer
256 /// to this array so that we can turn register number into a register
257 /// descriptor.
258 ///
259 class TargetRegisterInfo {
260 protected:
261   const unsigned* SubregHash;
262   const unsigned SubregHashSize;
263   const unsigned* AliasesHash;
264   const unsigned AliasesHashSize;
265 public:
266   typedef const TargetRegisterClass * const * regclass_iterator;
267 private:
268   const TargetRegisterDesc *Desc;             // Pointer to the descriptor array
269   const char *const *SubRegIndexNames;        // Names of subreg indexes.
270   unsigned NumRegs;                           // Number of entries in the array
271
272   regclass_iterator RegClassBegin, RegClassEnd;   // List of regclasses
273
274   int CallFrameSetupOpcode, CallFrameDestroyOpcode;
275
276 protected:
277   TargetRegisterInfo(const TargetRegisterDesc *D, unsigned NR,
278                      regclass_iterator RegClassBegin,
279                      regclass_iterator RegClassEnd,
280                      const char *const *subregindexnames,
281                      int CallFrameSetupOpcode = -1,
282                      int CallFrameDestroyOpcode = -1,
283                      const unsigned* subregs = 0,
284                      const unsigned subregsize = 0,
285                      const unsigned* aliases = 0,
286                      const unsigned aliasessize = 0);
287   virtual ~TargetRegisterInfo();
288 public:
289
290   enum {                        // Define some target independent constants
291     /// NoRegister - This physical register is not a real target register.  It
292     /// is useful as a sentinal.
293     NoRegister = 0,
294
295     /// FirstVirtualRegister - This is the first register number that is
296     /// considered to be a 'virtual' register, which is part of the SSA
297     /// namespace.  This must be the same for all targets, which means that each
298     /// target is limited to this fixed number of registers.
299     FirstVirtualRegister = 1024
300   };
301
302   /// isPhysicalRegister - Return true if the specified register number is in
303   /// the physical register namespace.
304   static bool isPhysicalRegister(unsigned Reg) {
305     assert(Reg && "this is not a register!");
306     return Reg < FirstVirtualRegister;
307   }
308
309   /// isVirtualRegister - Return true if the specified register number is in
310   /// the virtual register namespace.
311   static bool isVirtualRegister(unsigned Reg) {
312     assert(Reg && "this is not a register!");
313     return Reg >= FirstVirtualRegister;
314   }
315
316   /// getPhysicalRegisterRegClass - Returns the Register Class of a physical
317   /// register of the given type. If type is EVT::Other, then just return any
318   /// register class the register belongs to.
319   virtual const TargetRegisterClass *
320     getPhysicalRegisterRegClass(unsigned Reg, EVT VT = MVT::Other) const;
321
322   /// getAllocatableSet - Returns a bitset indexed by register number
323   /// indicating if a register is allocatable or not. If a register class is
324   /// specified, returns the subset for the class.
325   BitVector getAllocatableSet(const MachineFunction &MF,
326                               const TargetRegisterClass *RC = NULL) const;
327
328   const TargetRegisterDesc &operator[](unsigned RegNo) const {
329     assert(RegNo < NumRegs &&
330            "Attempting to access record for invalid register number!");
331     return Desc[RegNo];
332   }
333
334   /// Provide a get method, equivalent to [], but more useful if we have a
335   /// pointer to this object.
336   ///
337   const TargetRegisterDesc &get(unsigned RegNo) const {
338     return operator[](RegNo);
339   }
340
341   /// getAliasSet - Return the set of registers aliased by the specified
342   /// register, or a null list of there are none.  The list returned is zero
343   /// terminated.
344   ///
345   const unsigned *getAliasSet(unsigned RegNo) const {
346     return get(RegNo).AliasSet;
347   }
348
349   /// getSubRegisters - Return the list of registers that are sub-registers of
350   /// the specified register, or a null list of there are none. The list
351   /// returned is zero terminated and sorted according to super-sub register
352   /// relations. e.g. X86::RAX's sub-register list is EAX, AX, AL, AH.
353   ///
354   const unsigned *getSubRegisters(unsigned RegNo) const {
355     return get(RegNo).SubRegs;
356   }
357
358   /// getSuperRegisters - Return the list of registers that are super-registers
359   /// of the specified register, or a null list of there are none. The list
360   /// returned is zero terminated and sorted according to super-sub register
361   /// relations. e.g. X86::AL's super-register list is RAX, EAX, AX.
362   ///
363   const unsigned *getSuperRegisters(unsigned RegNo) const {
364     return get(RegNo).SuperRegs;
365   }
366
367   /// getName - Return the human-readable symbolic target-specific name for the
368   /// specified physical register.
369   const char *getName(unsigned RegNo) const {
370     return get(RegNo).Name;
371   }
372
373   /// getNumRegs - Return the number of registers this target has (useful for
374   /// sizing arrays holding per register information)
375   unsigned getNumRegs() const {
376     return NumRegs;
377   }
378
379   /// getSubRegIndexName - Return the human-readable symbolic target-specific
380   /// name for the specified SubRegIndex.
381   const char *getSubRegIndexName(unsigned SubIdx) const {
382     assert(SubIdx && "This is not a subregister index");
383     return SubRegIndexNames[SubIdx-1];
384   }
385
386   /// regsOverlap - Returns true if the two registers are equal or alias each
387   /// other. The registers may be virtual register.
388   bool regsOverlap(unsigned regA, unsigned regB) const {
389     if (regA == regB)
390       return true;
391
392     if (isVirtualRegister(regA) || isVirtualRegister(regB))
393       return false;
394
395     // regA and regB are distinct physical registers. Do they alias?
396     size_t index = (regA + regB * 37) & (AliasesHashSize-1);
397     unsigned ProbeAmt = 0;
398     while (AliasesHash[index*2] != 0 &&
399            AliasesHash[index*2+1] != 0) {
400       if (AliasesHash[index*2] == regA && AliasesHash[index*2+1] == regB)
401         return true;
402
403       index = (index + ProbeAmt) & (AliasesHashSize-1);
404       ProbeAmt += 2;
405     }
406
407     return false;
408   }
409
410   /// isSubRegister - Returns true if regB is a sub-register of regA.
411   ///
412   bool isSubRegister(unsigned regA, unsigned regB) const {
413     // SubregHash is a simple quadratically probed hash table.
414     size_t index = (regA + regB * 37) & (SubregHashSize-1);
415     unsigned ProbeAmt = 2;
416     while (SubregHash[index*2] != 0 &&
417            SubregHash[index*2+1] != 0) {
418       if (SubregHash[index*2] == regA && SubregHash[index*2+1] == regB)
419         return true;
420
421       index = (index + ProbeAmt) & (SubregHashSize-1);
422       ProbeAmt += 2;
423     }
424
425     return false;
426   }
427
428   /// isSuperRegister - Returns true if regB is a super-register of regA.
429   ///
430   bool isSuperRegister(unsigned regA, unsigned regB) const {
431     return isSubRegister(regB, regA);
432   }
433
434   /// getCalleeSavedRegs - Return a null-terminated list of all of the
435   /// callee saved registers on this target. The register should be in the
436   /// order of desired callee-save stack frame offset. The first register is
437   /// closed to the incoming stack pointer if stack grows down, and vice versa.
438   virtual const unsigned* getCalleeSavedRegs(const MachineFunction *MF = 0)
439                                                                       const = 0;
440
441   /// getCalleeSavedRegClasses - Return a null-terminated list of the preferred
442   /// register classes to spill each callee saved register with.  The order and
443   /// length of this list match the getCalleeSaveRegs() list.
444   virtual const TargetRegisterClass* const *getCalleeSavedRegClasses(
445                                             const MachineFunction *MF) const =0;
446
447   /// getReservedRegs - Returns a bitset indexed by physical register number
448   /// indicating if a register is a special register that has particular uses
449   /// and should be considered unavailable at all times, e.g. SP, RA. This is
450   /// used by register scavenger to determine what registers are free.
451   virtual BitVector getReservedRegs(const MachineFunction &MF) const = 0;
452
453   /// getSubReg - Returns the physical register number of sub-register "Index"
454   /// for physical register RegNo. Return zero if the sub-register does not
455   /// exist.
456   virtual unsigned getSubReg(unsigned RegNo, unsigned Index) const = 0;
457
458   /// getSubRegIndex - For a given register pair, return the sub-register index
459   /// if the are second register is a sub-register of the first. Return zero
460   /// otherwise.
461   virtual unsigned getSubRegIndex(unsigned RegNo, unsigned SubRegNo) const = 0;
462
463   /// getMatchingSuperReg - Return a super-register of the specified register
464   /// Reg so its sub-register of index SubIdx is Reg.
465   unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
466                                const TargetRegisterClass *RC) const {
467     for (const unsigned *SRs = getSuperRegisters(Reg); unsigned SR = *SRs;++SRs)
468       if (Reg == getSubReg(SR, SubIdx) && RC->contains(SR))
469         return SR;
470     return 0;
471   }
472
473   /// canCombinedSubRegIndex - Given a register class and a list of sub-register
474   /// indices, return true if it's possible to combine the sub-register indices
475   /// into one that corresponds to a larger sub-register. Return the new sub-
476   /// register index by reference. Note the new index by be zero if the given
477   /// sub-registers combined to form the whole register.
478   virtual bool canCombinedSubRegIndex(const TargetRegisterClass *RC,
479                                       SmallVectorImpl<unsigned> &SubIndices,
480                                       unsigned &NewSubIdx) const {
481     return 0;
482   }
483
484   /// getMatchingSuperRegClass - Return a subclass of the specified register
485   /// class A so that each register in it has a sub-register of the
486   /// specified sub-register index which is in the specified register class B.
487   virtual const TargetRegisterClass *
488   getMatchingSuperRegClass(const TargetRegisterClass *A,
489                            const TargetRegisterClass *B, unsigned Idx) const {
490     return 0;
491   }
492
493   //===--------------------------------------------------------------------===//
494   // Register Class Information
495   //
496
497   /// Register class iterators
498   ///
499   regclass_iterator regclass_begin() const { return RegClassBegin; }
500   regclass_iterator regclass_end() const { return RegClassEnd; }
501
502   unsigned getNumRegClasses() const {
503     return (unsigned)(regclass_end()-regclass_begin());
504   }
505
506   /// getRegClass - Returns the register class associated with the enumeration
507   /// value.  See class TargetOperandInfo.
508   const TargetRegisterClass *getRegClass(unsigned i) const {
509     assert(i <= getNumRegClasses() && "Register Class ID out of range");
510     return i ? RegClassBegin[i - 1] : NULL;
511   }
512
513   /// getPointerRegClass - Returns a TargetRegisterClass used for pointer
514   /// values.  If a target supports multiple different pointer register classes,
515   /// kind specifies which one is indicated.
516   virtual const TargetRegisterClass *getPointerRegClass(unsigned Kind=0) const {
517     assert(0 && "Target didn't implement getPointerRegClass!");
518     return 0; // Must return a value in order to compile with VS 2005
519   }
520
521   /// getCrossCopyRegClass - Returns a legal register class to copy a register
522   /// in the specified class to or from. Returns NULL if it is possible to copy
523   /// between a two registers of the specified class.
524   virtual const TargetRegisterClass *
525   getCrossCopyRegClass(const TargetRegisterClass *RC) const {
526     return NULL;
527   }
528
529   /// getAllocationOrder - Returns the register allocation order for a specified
530   /// register class in the form of a pair of TargetRegisterClass iterators.
531   virtual std::pair<TargetRegisterClass::iterator,TargetRegisterClass::iterator>
532   getAllocationOrder(const TargetRegisterClass *RC,
533                      unsigned HintType, unsigned HintReg,
534                      const MachineFunction &MF) const {
535     return std::make_pair(RC->allocation_order_begin(MF),
536                           RC->allocation_order_end(MF));
537   }
538
539   /// ResolveRegAllocHint - Resolves the specified register allocation hint
540   /// to a physical register. Returns the physical register if it is successful.
541   virtual unsigned ResolveRegAllocHint(unsigned Type, unsigned Reg,
542                                        const MachineFunction &MF) const {
543     if (Type == 0 && Reg && isPhysicalRegister(Reg))
544       return Reg;
545     return 0;
546   }
547
548   /// UpdateRegAllocHint - A callback to allow target a chance to update
549   /// register allocation hints when a register is "changed" (e.g. coalesced)
550   /// to another register. e.g. On ARM, some virtual registers should target
551   /// register pairs, if one of pair is coalesced to another register, the
552   /// allocation hint of the other half of the pair should be changed to point
553   /// to the new register.
554   virtual void UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
555                                   MachineFunction &MF) const {
556     // Do nothing.
557   }
558
559   /// targetHandlesStackFrameRounding - Returns true if the target is
560   /// responsible for rounding up the stack frame (probably at emitPrologue
561   /// time).
562   virtual bool targetHandlesStackFrameRounding() const {
563     return false;
564   }
565
566   /// requiresRegisterScavenging - returns true if the target requires (and can
567   /// make use of) the register scavenger.
568   virtual bool requiresRegisterScavenging(const MachineFunction &MF) const {
569     return false;
570   }
571
572   /// requiresFrameIndexScavenging - returns true if the target requires post
573   /// PEI scavenging of registers for materializing frame index constants.
574   virtual bool requiresFrameIndexScavenging(const MachineFunction &MF) const {
575     return false;
576   }
577
578   /// hasFP - Return true if the specified function should have a dedicated
579   /// frame pointer register. For most targets this is true only if the function
580   /// has variable sized allocas or if frame pointer elimination is disabled.
581   virtual bool hasFP(const MachineFunction &MF) const = 0;
582
583   /// hasReservedCallFrame - Under normal circumstances, when a frame pointer is
584   /// not required, we reserve argument space for call sites in the function
585   /// immediately on entry to the current function. This eliminates the need for
586   /// add/sub sp brackets around call sites. Returns true if the call frame is
587   /// included as part of the stack frame.
588   virtual bool hasReservedCallFrame(MachineFunction &MF) const {
589     return !hasFP(MF);
590   }
591
592   /// canSimplifyCallFramePseudos - When possible, it's best to simplify the
593   /// call frame pseudo ops before doing frame index elimination. This is
594   /// possible only when frame index references between the pseudos won't
595   /// need adjusted for the call frame adjustments. Normally, that's true
596   /// if the function has a reserved call frame or a frame pointer. Some
597   /// targets (Thumb2, for example) may have more complicated criteria,
598   /// however, and can override this behavior.
599   virtual bool canSimplifyCallFramePseudos(MachineFunction &MF) const {
600     return hasReservedCallFrame(MF) || hasFP(MF);
601   }
602
603   /// hasReservedSpillSlot - Return true if target has reserved a spill slot in
604   /// the stack frame of the given function for the specified register. e.g. On
605   /// x86, if the frame register is required, the first fixed stack object is
606   /// reserved as its spill slot. This tells PEI not to create a new stack frame
607   /// object for the given register. It should be called only after
608   /// processFunctionBeforeCalleeSavedScan().
609   virtual bool hasReservedSpillSlot(MachineFunction &MF, unsigned Reg,
610                                     int &FrameIdx) const {
611     return false;
612   }
613
614   /// needsStackRealignment - true if storage within the function requires the
615   /// stack pointer to be aligned more than the normal calling convention calls
616   /// for.
617   virtual bool needsStackRealignment(const MachineFunction &MF) const {
618     return false;
619   }
620
621   /// getCallFrameSetup/DestroyOpcode - These methods return the opcode of the
622   /// frame setup/destroy instructions if they exist (-1 otherwise).  Some
623   /// targets use pseudo instructions in order to abstract away the difference
624   /// between operating with a frame pointer and operating without, through the
625   /// use of these two instructions.
626   ///
627   int getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
628   int getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
629
630   /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
631   /// code insertion to eliminate call frame setup and destroy pseudo
632   /// instructions (but only if the Target is using them).  It is responsible
633   /// for eliminating these instructions, replacing them with concrete
634   /// instructions.  This method need only be implemented if using call frame
635   /// setup/destroy pseudo instructions.
636   ///
637   virtual void
638   eliminateCallFramePseudoInstr(MachineFunction &MF,
639                                 MachineBasicBlock &MBB,
640                                 MachineBasicBlock::iterator MI) const {
641     assert(getCallFrameSetupOpcode()== -1 && getCallFrameDestroyOpcode()== -1 &&
642            "eliminateCallFramePseudoInstr must be implemented if using"
643            " call frame setup/destroy pseudo instructions!");
644     assert(0 && "Call Frame Pseudo Instructions do not exist on this target!");
645   }
646
647   /// processFunctionBeforeCalleeSavedScan - This method is called immediately
648   /// before PrologEpilogInserter scans the physical registers used to determine
649   /// what callee saved registers should be spilled. This method is optional.
650   virtual void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
651                                                 RegScavenger *RS = NULL) const {
652
653   }
654
655   /// processFunctionBeforeFrameFinalized - This method is called immediately
656   /// before the specified functions frame layout (MF.getFrameInfo()) is
657   /// finalized.  Once the frame is finalized, MO_FrameIndex operands are
658   /// replaced with direct constants.  This method is optional.
659   ///
660   virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
661   }
662
663   /// saveScavengerRegister - Spill the register so it can be used by the
664   /// register scavenger. Return true if the register was spilled, false
665   /// otherwise. If this function does not spill the register, the scavenger
666   /// will instead spill it to the emergency spill slot.
667   ///
668   virtual bool saveScavengerRegister(MachineBasicBlock &MBB,
669                                      MachineBasicBlock::iterator I,
670                                      MachineBasicBlock::iterator &UseMI,
671                                      const TargetRegisterClass *RC,
672                                      unsigned Reg) const {
673     return false;
674   }
675
676   /// eliminateFrameIndex - This method must be overriden to eliminate abstract
677   /// frame indices from instructions which may use them.  The instruction
678   /// referenced by the iterator contains an MO_FrameIndex operand which must be
679   /// eliminated by this method.  This method may modify or replace the
680   /// specified instruction, as long as it keeps the iterator pointing at the
681   /// finished product. SPAdj is the SP adjustment due to call frame setup
682   /// instruction.
683   ///
684   /// When -enable-frame-index-scavenging is enabled, the virtual register
685   /// allocated for this frame index is returned and its value is stored in
686   /// *Value.
687   typedef std::pair<unsigned, int> FrameIndexValue;
688   virtual unsigned eliminateFrameIndex(MachineBasicBlock::iterator MI,
689                                        int SPAdj, FrameIndexValue *Value = NULL,
690                                        RegScavenger *RS=NULL) const = 0;
691
692   /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
693   /// the function.
694   virtual void emitPrologue(MachineFunction &MF) const = 0;
695   virtual void emitEpilogue(MachineFunction &MF,
696                             MachineBasicBlock &MBB) const = 0;
697
698   //===--------------------------------------------------------------------===//
699   /// Debug information queries.
700
701   /// getDwarfRegNum - Map a target register to an equivalent dwarf register
702   /// number.  Returns -1 if there is no equivalent value.  The second
703   /// parameter allows targets to use different numberings for EH info and
704   /// debugging info.
705   virtual int getDwarfRegNum(unsigned RegNum, bool isEH) const = 0;
706
707   /// getFrameRegister - This method should return the register used as a base
708   /// for values allocated in the current stack frame.
709   virtual unsigned getFrameRegister(const MachineFunction &MF) const = 0;
710
711   /// getFrameIndexOffset - Returns the displacement from the frame register to
712   /// the stack frame of the specified index.
713   virtual int getFrameIndexOffset(const MachineFunction &MF, int FI) const;
714
715   /// getFrameIndexReference - This method should return the base register
716   /// and offset used to reference a frame index location. The offset is
717   /// returned directly, and the base register is returned via FrameReg.
718   virtual int getFrameIndexReference(const MachineFunction &MF, int FI,
719                                      unsigned &FrameReg) const {
720     // By default, assume all frame indices are referenced via whatever
721     // getFrameRegister() says. The target can override this if it's doing
722     // something different.
723     FrameReg = getFrameRegister(MF);
724     return getFrameIndexOffset(MF, FI);
725   }
726
727   /// getRARegister - This method should return the register where the return
728   /// address can be found.
729   virtual unsigned getRARegister() const = 0;
730
731   /// getInitialFrameState - Returns a list of machine moves that are assumed
732   /// on entry to all functions.  Note that LabelID is ignored (assumed to be
733   /// the beginning of the function.)
734   virtual void getInitialFrameState(std::vector<MachineMove> &Moves) const;
735 };
736
737
738 // This is useful when building IndexedMaps keyed on virtual registers
739 struct VirtReg2IndexFunctor : public std::unary_function<unsigned, unsigned> {
740   unsigned operator()(unsigned Reg) const {
741     return Reg - TargetRegisterInfo::FirstVirtualRegister;
742   }
743 };
744
745 /// getCommonSubClass - find the largest common subclass of A and B. Return NULL
746 /// if there is no common subclass.
747 const TargetRegisterClass *getCommonSubClass(const TargetRegisterClass *A,
748                                              const TargetRegisterClass *B);
749
750 } // End llvm namespace
751
752 #endif