LLVM API Documentation

TargetRegisterInfo.h

Go to the documentation of this file.
00001 //=== Target/TargetRegisterInfo.h - Target Register Information -*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file describes an abstract interface used to get information about a
00011 // target machines register file.  This information is used for a variety of
00012 // purposed, especially register allocation.
00013 //
00014 //===----------------------------------------------------------------------===//
00015 
00016 #ifndef LLVM_TARGET_TARGETREGISTERINFO_H
00017 #define LLVM_TARGET_TARGETREGISTERINFO_H
00018 
00019 #include "llvm/CodeGen/MachineBasicBlock.h"
00020 #include "llvm/CodeGen/ValueTypes.h"
00021 #include <cassert>
00022 #include <functional>
00023 
00024 namespace llvm {
00025 
00026 class BitVector;
00027 class MachineFunction;
00028 class MachineMove;
00029 class RegScavenger;
00030 
00031 /// TargetRegisterDesc - This record contains all of the information known about
00032 /// a particular register.  The AliasSet field (if not null) contains a pointer
00033 /// to a Zero terminated array of registers that this register aliases.  This is
00034 /// needed for architectures like X86 which have AL alias AX alias EAX.
00035 /// Registers that this does not apply to simply should set this to null.
00036 /// The SubRegs field is a zero terminated array of registers that are
00037 /// sub-registers of the specific register, e.g. AL, AH are sub-registers of AX.
00038 /// The SuperRegs field is a zero terminated array of registers that are
00039 /// super-registers of the specific register, e.g. RAX, EAX, are super-registers
00040 /// of AX.
00041 ///
00042 struct TargetRegisterDesc {
00043   const char     *AsmName;      // Assembly language name for the register
00044   const char     *Name;         // Printable name for the reg (for debugging)
00045   const unsigned *AliasSet;     // Register Alias Set, described above
00046   const unsigned *SubRegs;      // Sub-register set, described above
00047   const unsigned *SuperRegs;    // Super-register set, described above
00048 };
00049 
00050 class TargetRegisterClass {
00051 public:
00052   typedef const unsigned* iterator;
00053   typedef const unsigned* const_iterator;
00054 
00055   typedef const MVT* vt_iterator;
00056   typedef const TargetRegisterClass* const * sc_iterator;
00057 private:
00058   unsigned ID;
00059   bool  isSubClass;
00060   const vt_iterator VTs;
00061   const sc_iterator SubClasses;
00062   const sc_iterator SuperClasses;
00063   const unsigned RegSize, Alignment;    // Size & Alignment of register in bytes
00064   const int CopyCost;
00065   const iterator RegsBegin, RegsEnd;
00066 public:
00067   TargetRegisterClass(unsigned id,
00068                       const MVT *vts,
00069                       const TargetRegisterClass * const *subcs,
00070                       const TargetRegisterClass * const *supcs,
00071                       unsigned RS, unsigned Al, int CC,
00072                       iterator RB, iterator RE)
00073     : ID(id), VTs(vts), SubClasses(subcs), SuperClasses(supcs),
00074     RegSize(RS), Alignment(Al), CopyCost(CC), RegsBegin(RB), RegsEnd(RE) {}
00075   virtual ~TargetRegisterClass() {}     // Allow subclasses
00076   
00077   /// getID() - Return the register class ID number.
00078   ///
00079   unsigned getID() const { return ID; }
00080   
00081   /// begin/end - Return all of the registers in this class.
00082   ///
00083   iterator       begin() const { return RegsBegin; }
00084   iterator         end() const { return RegsEnd; }
00085 
00086   /// getNumRegs - Return the number of registers in this class.
00087   ///
00088   unsigned getNumRegs() const { return (unsigned)(RegsEnd-RegsBegin); }
00089 
00090   /// getRegister - Return the specified register in the class.
00091   ///
00092   unsigned getRegister(unsigned i) const {
00093     assert(i < getNumRegs() && "Register number out of range!");
00094     return RegsBegin[i];
00095   }
00096 
00097   /// contains - Return true if the specified register is included in this
00098   /// register class.
00099   bool contains(unsigned Reg) const {
00100     for (iterator I = begin(), E = end(); I != E; ++I)
00101       if (*I == Reg) return true;
00102     return false;
00103   }
00104 
00105   /// hasType - return true if this TargetRegisterClass has the ValueType vt.
00106   ///
00107   bool hasType(MVT vt) const {
00108     for(int i = 0; VTs[i] != MVT::Other; ++i)
00109       if (VTs[i] == vt)
00110         return true;
00111     return false;
00112   }
00113   
00114   /// vt_begin / vt_end - Loop over all of the value types that can be
00115   /// represented by values in this register class.
00116   vt_iterator vt_begin() const {
00117     return VTs;
00118   }
00119 
00120   vt_iterator vt_end() const {
00121     vt_iterator I = VTs;
00122     while (*I != MVT::Other) ++I;
00123     return I;
00124   }
00125 
00126   /// hasSubClass - return true if the specified TargetRegisterClass is a
00127   /// sub-register class of this TargetRegisterClass.
00128   bool hasSubClass(const TargetRegisterClass *cs) const {
00129     for (int i = 0; SubClasses[i] != NULL; ++i) 
00130       if (SubClasses[i] == cs)
00131         return true;
00132     return false;
00133   }
00134 
00135   /// subclasses_begin / subclasses_end - Loop over all of the sub-classes of
00136   /// this register class.
00137   sc_iterator subclasses_begin() const {
00138     return SubClasses;
00139   }
00140   
00141   sc_iterator subclasses_end() const {
00142     sc_iterator I = SubClasses;
00143     while (*I != NULL) ++I;
00144     return I;
00145   }
00146   
00147   /// hasSuperClass - return true if the specified TargetRegisterClass is a
00148   /// super-register class of this TargetRegisterClass.
00149   bool hasSuperClass(const TargetRegisterClass *cs) const {
00150     for (int i = 0; SuperClasses[i] != NULL; ++i) 
00151       if (SuperClasses[i] == cs)
00152         return true;
00153     return false;
00154   }
00155 
00156   /// superclasses_begin / superclasses_end - Loop over all of the super-classes
00157   /// of this register class.
00158   sc_iterator superclasses_begin() const {
00159     return SuperClasses;
00160   }
00161   
00162   sc_iterator superclasses_end() const {
00163     sc_iterator I = SuperClasses;
00164     while (*I != NULL) ++I;
00165     return I;
00166   }
00167 
00168   /// isASubClass - return true if this TargetRegisterClass is a sub-class of at
00169   /// least one other TargetRegisterClass.
00170   bool isASubClass() const {
00171     return SuperClasses[0] != 0;
00172   }
00173   
00174   /// allocation_order_begin/end - These methods define a range of registers
00175   /// which specify the registers in this class that are valid to register
00176   /// allocate, and the preferred order to allocate them in.  For example,
00177   /// callee saved registers should be at the end of the list, because it is
00178   /// cheaper to allocate caller saved registers.
00179   ///
00180   /// These methods take a MachineFunction argument, which can be used to tune
00181   /// the allocatable registers based on the characteristics of the function.
00182   /// One simple example is that the frame pointer register can be used if
00183   /// frame-pointer-elimination is performed.
00184   ///
00185   /// By default, these methods return all registers in the class.
00186   ///
00187   virtual iterator allocation_order_begin(const MachineFunction &MF) const {
00188     return begin();
00189   }
00190   virtual iterator allocation_order_end(const MachineFunction &MF)   const {
00191     return end();
00192   }
00193 
00194 
00195 
00196   /// getSize - Return the size of the register in bytes, which is also the size
00197   /// of a stack slot allocated to hold a spilled copy of this register.
00198   unsigned getSize() const { return RegSize; }
00199 
00200   /// getAlignment - Return the minimum required alignment for a register of
00201   /// this class.
00202   unsigned getAlignment() const { return Alignment; }
00203 
00204   /// getCopyCost - Return the cost of copying a value between two registers in
00205   /// this class.
00206   int getCopyCost() const { return CopyCost; }
00207 };
00208 
00209 
00210 /// TargetRegisterInfo base class - We assume that the target defines a static
00211 /// array of TargetRegisterDesc objects that represent all of the machine
00212 /// registers that the target has.  As such, we simply have to track a pointer
00213 /// to this array so that we can turn register number into a register
00214 /// descriptor.
00215 ///
00216 class TargetRegisterInfo {
00217 protected:
00218   const unsigned* SubregHash;
00219   const unsigned SubregHashSize;
00220 public:
00221   typedef const TargetRegisterClass * const * regclass_iterator;
00222 private:
00223   const TargetRegisterDesc *Desc;             // Pointer to the descriptor array
00224   unsigned NumRegs;                           // Number of entries in the array
00225 
00226   regclass_iterator RegClassBegin, RegClassEnd;   // List of regclasses
00227 
00228   int CallFrameSetupOpcode, CallFrameDestroyOpcode;
00229 protected:
00230   TargetRegisterInfo(const TargetRegisterDesc *D, unsigned NR,
00231                      regclass_iterator RegClassBegin,
00232                      regclass_iterator RegClassEnd,
00233                      int CallFrameSetupOpcode = -1,
00234                      int CallFrameDestroyOpcode = -1,
00235                      const unsigned* subregs = 0,
00236                      const unsigned subregsize = 0);
00237   virtual ~TargetRegisterInfo();
00238 public:
00239 
00240   enum {                        // Define some target independent constants
00241     /// NoRegister - This physical register is not a real target register.  It
00242     /// is useful as a sentinal.
00243     NoRegister = 0,
00244 
00245     /// FirstVirtualRegister - This is the first register number that is
00246     /// considered to be a 'virtual' register, which is part of the SSA
00247     /// namespace.  This must be the same for all targets, which means that each
00248     /// target is limited to 1024 registers.
00249     FirstVirtualRegister = 1024
00250   };
00251 
00252   /// isPhysicalRegister - Return true if the specified register number is in
00253   /// the physical register namespace.
00254   static bool isPhysicalRegister(unsigned Reg) {
00255     assert(Reg && "this is not a register!");
00256     return Reg < FirstVirtualRegister;
00257   }
00258 
00259   /// isVirtualRegister - Return true if the specified register number is in
00260   /// the virtual register namespace.
00261   static bool isVirtualRegister(unsigned Reg) {
00262     assert(Reg && "this is not a register!");
00263     return Reg >= FirstVirtualRegister;
00264   }
00265 
00266   /// getPhysicalRegisterRegClass - Returns the Register Class of a physical
00267   /// register of the given type. If type is MVT::Other, then just return any
00268   /// register class the register belongs to.
00269   const TargetRegisterClass *getPhysicalRegisterRegClass(unsigned Reg,
00270                                           MVT VT = MVT::Other) const;
00271 
00272   /// getAllocatableSet - Returns a bitset indexed by register number
00273   /// indicating if a register is allocatable or not. If a register class is
00274   /// specified, returns the subset for the class.
00275   BitVector getAllocatableSet(MachineFunction &MF,
00276                               const TargetRegisterClass *RC = NULL) const;
00277 
00278   const TargetRegisterDesc &operator[](unsigned RegNo) const {
00279     assert(RegNo < NumRegs &&
00280            "Attempting to access record for invalid register number!");
00281     return Desc[RegNo];
00282   }
00283 
00284   /// Provide a get method, equivalent to [], but more useful if we have a
00285   /// pointer to this object.
00286   ///
00287   const TargetRegisterDesc &get(unsigned RegNo) const {
00288     return operator[](RegNo);
00289   }
00290 
00291   /// getAliasSet - Return the set of registers aliased by the specified
00292   /// register, or a null list of there are none.  The list returned is zero
00293   /// terminated.
00294   ///
00295   const unsigned *getAliasSet(unsigned RegNo) const {
00296     return get(RegNo).AliasSet;
00297   }
00298 
00299   /// getSubRegisters - Return the list of registers that are sub-registers of
00300   /// the specified register, or a null list of there are none. The list
00301   /// returned is zero terminated and sorted according to super-sub register
00302   /// relations. e.g. X86::RAX's sub-register list is EAX, AX, AL, AH.
00303   ///
00304   const unsigned *getSubRegisters(unsigned RegNo) const {
00305     return get(RegNo).SubRegs;
00306   }
00307 
00308   /// getSuperRegisters - Return the list of registers that are super-registers
00309   /// of the specified register, or a null list of there are none. The list
00310   /// returned is zero terminated and sorted according to super-sub register
00311   /// relations. e.g. X86::AL's super-register list is RAX, EAX, AX.
00312   ///
00313   const unsigned *getSuperRegisters(unsigned RegNo) const {
00314     return get(RegNo).SuperRegs;
00315   }
00316 
00317   /// getAsmName - Return the symbolic target-specific name for the
00318   /// specified physical register.
00319   const char *getAsmName(unsigned RegNo) const {
00320     return get(RegNo).AsmName;
00321   }
00322 
00323   /// getName - Return the human-readable symbolic target-specific name for the
00324   /// specified physical register.
00325   const char *getName(unsigned RegNo) const {
00326     return get(RegNo).Name;
00327   }
00328 
00329   /// getNumRegs - Return the number of registers this target has (useful for
00330   /// sizing arrays holding per register information)
00331   unsigned getNumRegs() const {
00332     return NumRegs;
00333   }
00334 
00335   /// areAliases - Returns true if the two registers alias each other, false
00336   /// otherwise
00337   bool areAliases(unsigned regA, unsigned regB) const {
00338     for (const unsigned *Alias = getAliasSet(regA); *Alias; ++Alias)
00339       if (*Alias == regB) return true;
00340     return false;
00341   }
00342 
00343   /// regsOverlap - Returns true if the two registers are equal or alias each
00344   /// other. The registers may be virtual register.
00345   bool regsOverlap(unsigned regA, unsigned regB) const {
00346     if (regA == regB)
00347       return true;
00348 
00349     if (isVirtualRegister(regA) || isVirtualRegister(regB))
00350       return false;
00351     return areAliases(regA, regB);
00352   }
00353 
00354   /// isSubRegister - Returns true if regB is a sub-register of regA.
00355   ///
00356   bool isSubRegister(unsigned regA, unsigned regB) const {
00357     // SubregHash is a simple quadratically probed hash table.
00358     size_t index = (regA + regB * 37) & (SubregHashSize-1);
00359     unsigned ProbeAmt = 2;
00360     while (SubregHash[index*2] != 0 &&
00361            SubregHash[index*2+1] != 0) {
00362       if (SubregHash[index*2] == regA && SubregHash[index*2+1] == regB)
00363         return true;
00364       
00365       index = (index + ProbeAmt) & (SubregHashSize-1);
00366       ProbeAmt += 2;
00367     }
00368     
00369     return false;
00370   }
00371 
00372   /// isSuperRegister - Returns true if regB is a super-register of regA.
00373   ///
00374   bool isSuperRegister(unsigned regA, unsigned regB) const {
00375     for (const unsigned *SR = getSuperRegisters(regA); *SR; ++SR)
00376       if (*SR == regB) return true;
00377     return false;
00378   }
00379 
00380   /// getCalleeSavedRegs - Return a null-terminated list of all of the
00381   /// callee saved registers on this target. The register should be in the
00382   /// order of desired callee-save stack frame offset. The first register is
00383   /// closed to the incoming stack pointer if stack grows down, and vice versa.
00384   virtual const unsigned* getCalleeSavedRegs(const MachineFunction *MF = 0)
00385                                                                       const = 0;
00386 
00387   /// getCalleeSavedRegClasses - Return a null-terminated list of the preferred
00388   /// register classes to spill each callee saved register with.  The order and
00389   /// length of this list match the getCalleeSaveRegs() list.
00390   virtual const TargetRegisterClass* const *getCalleeSavedRegClasses(
00391                                             const MachineFunction *MF) const =0;
00392 
00393   /// getReservedRegs - Returns a bitset indexed by physical register number
00394   /// indicating if a register is a special register that has particular uses
00395   /// and should be considered unavailable at all times, e.g. SP, RA. This is
00396   /// used by register scavenger to determine what registers are free.
00397   virtual BitVector getReservedRegs(const MachineFunction &MF) const = 0;
00398 
00399   /// getSubReg - Returns the physical register number of sub-register "Index"
00400   /// for physical register RegNo. Return zero if the sub-register does not
00401   /// exist.
00402   virtual unsigned getSubReg(unsigned RegNo, unsigned Index) const = 0;
00403 
00404   //===--------------------------------------------------------------------===//
00405   // Register Class Information
00406   //
00407 
00408   /// Register class iterators
00409   ///
00410   regclass_iterator regclass_begin() const { return RegClassBegin; }
00411   regclass_iterator regclass_end() const { return RegClassEnd; }
00412 
00413   unsigned getNumRegClasses() const {
00414     return (unsigned)(regclass_end()-regclass_begin());
00415   }
00416   
00417   /// getRegClass - Returns the register class associated with the enumeration
00418   /// value.  See class TargetOperandInfo.
00419   const TargetRegisterClass *getRegClass(unsigned i) const {
00420     assert(i <= getNumRegClasses() && "Register Class ID out of range");
00421     return i ? RegClassBegin[i - 1] : NULL;
00422   }
00423 
00424   //===--------------------------------------------------------------------===//
00425   // Interfaces used by the register allocator and stack frame
00426   // manipulation passes to move data around between registers,
00427   // immediates and memory.  FIXME: Move these to TargetInstrInfo.h.
00428   //
00429 
00430   /// getCrossCopyRegClass - Returns a legal register class to copy a register
00431   /// in the specified class to or from. Returns NULL if it is possible to copy
00432   /// between a two registers of the specified class.
00433   virtual const TargetRegisterClass *
00434   getCrossCopyRegClass(const TargetRegisterClass *RC) const {
00435     return NULL;
00436   }
00437 
00438   /// targetHandlesStackFrameRounding - Returns true if the target is
00439   /// responsible for rounding up the stack frame (probably at emitPrologue
00440   /// time).
00441   virtual bool targetHandlesStackFrameRounding() const {
00442     return false;
00443   }
00444 
00445   /// requiresRegisterScavenging - returns true if the target requires (and can
00446   /// make use of) the register scavenger.
00447   virtual bool requiresRegisterScavenging(const MachineFunction &MF) const {
00448     return false;
00449   }
00450   
00451   /// hasFP - Return true if the specified function should have a dedicated
00452   /// frame pointer register. For most targets this is true only if the function
00453   /// has variable sized allocas or if frame pointer elimination is disabled.
00454   virtual bool hasFP(const MachineFunction &MF) const = 0;
00455 
00456   // hasReservedCallFrame - Under normal circumstances, when a frame pointer is
00457   // not required, we reserve argument space for call sites in the function
00458   // immediately on entry to the current function. This eliminates the need for
00459   // add/sub sp brackets around call sites. Returns true if the call frame is
00460   // included as part of the stack frame.
00461   virtual bool hasReservedCallFrame(MachineFunction &MF) const {
00462     return !hasFP(MF);
00463   }
00464 
00465   // needsStackRealignment - true if storage within the function requires the
00466   // stack pointer to be aligned more than the normal calling convention calls
00467   // for.
00468   virtual bool needsStackRealignment(const MachineFunction &MF) const {
00469     return false;
00470   }
00471 
00472   /// getCallFrameSetup/DestroyOpcode - These methods return the opcode of the
00473   /// frame setup/destroy instructions if they exist (-1 otherwise).  Some
00474   /// targets use pseudo instructions in order to abstract away the difference
00475   /// between operating with a frame pointer and operating without, through the
00476   /// use of these two instructions.
00477   ///
00478   int getCallFrameSetupOpcode() const { return CallFrameSetupOpcode; }
00479   int getCallFrameDestroyOpcode() const { return CallFrameDestroyOpcode; }
00480 
00481 
00482   /// eliminateCallFramePseudoInstr - This method is called during prolog/epilog
00483   /// code insertion to eliminate call frame setup and destroy pseudo
00484   /// instructions (but only if the Target is using them).  It is responsible
00485   /// for eliminating these instructions, replacing them with concrete
00486   /// instructions.  This method need only be implemented if using call frame
00487   /// setup/destroy pseudo instructions.
00488   ///
00489   virtual void
00490   eliminateCallFramePseudoInstr(MachineFunction &MF,
00491                                 MachineBasicBlock &MBB,
00492                                 MachineBasicBlock::iterator MI) const {
00493     assert(getCallFrameSetupOpcode()== -1 && getCallFrameDestroyOpcode()== -1 &&
00494            "eliminateCallFramePseudoInstr must be implemented if using"
00495            " call frame setup/destroy pseudo instructions!");
00496     assert(0 && "Call Frame Pseudo Instructions do not exist on this target!");
00497   }
00498 
00499   /// processFunctionBeforeCalleeSavedScan - This method is called immediately
00500   /// before PrologEpilogInserter scans the physical registers used to determine
00501   /// what callee saved registers should be spilled. This method is optional.
00502   virtual void processFunctionBeforeCalleeSavedScan(MachineFunction &MF,
00503                                                 RegScavenger *RS = NULL) const {
00504 
00505   }
00506 
00507   /// processFunctionBeforeFrameFinalized - This method is called immediately
00508   /// before the specified functions frame layout (MF.getFrameInfo()) is
00509   /// finalized.  Once the frame is finalized, MO_FrameIndex operands are
00510   /// replaced with direct constants.  This method is optional.
00511   ///
00512   virtual void processFunctionBeforeFrameFinalized(MachineFunction &MF) const {
00513   }
00514 
00515   /// eliminateFrameIndex - This method must be overriden to eliminate abstract
00516   /// frame indices from instructions which may use them.  The instruction
00517   /// referenced by the iterator contains an MO_FrameIndex operand which must be
00518   /// eliminated by this method.  This method may modify or replace the
00519   /// specified instruction, as long as it keeps the iterator pointing the the
00520   /// finished product. SPAdj is the SP adjustment due to call frame setup
00521   /// instruction.
00522   virtual void eliminateFrameIndex(MachineBasicBlock::iterator MI,
00523                                    int SPAdj, RegScavenger *RS=NULL) const = 0;
00524 
00525   /// emitProlog/emitEpilog - These methods insert prolog and epilog code into
00526   /// the function.
00527   virtual void emitPrologue(MachineFunction &MF) const = 0;
00528   virtual void emitEpilogue(MachineFunction &MF,
00529                             MachineBasicBlock &MBB) const = 0;
00530                             
00531   //===--------------------------------------------------------------------===//
00532   /// Debug information queries.
00533   
00534   /// getDwarfRegNum - Map a target register to an equivalent dwarf register
00535   /// number.  Returns -1 if there is no equivalent value.  The second
00536   /// parameter allows targets to use different numberings for EH info and
00537   /// debugging info.
00538   virtual int getDwarfRegNum(unsigned RegNum, bool isEH) const = 0;
00539 
00540   /// getFrameRegister - This method should return the register used as a base
00541   /// for values allocated in the current stack frame.
00542   virtual unsigned getFrameRegister(MachineFunction &MF) const = 0;
00543 
00544   /// getFrameIndexOffset - Returns the displacement from the frame register to
00545   /// the stack frame of the specified index.
00546   virtual int getFrameIndexOffset(MachineFunction &MF, int FI) const;
00547                            
00548   /// getRARegister - This method should return the register where the return
00549   /// address can be found.
00550   virtual unsigned getRARegister() const = 0;
00551   
00552   /// getInitialFrameState - Returns a list of machine moves that are assumed
00553   /// on entry to all functions.  Note that LabelID is ignored (assumed to be
00554   /// the beginning of the function.)
00555   virtual void getInitialFrameState(std::vector<MachineMove> &Moves) const;
00556 };
00557 
00558 // This is useful when building IndexedMaps keyed on virtual registers
00559 struct VirtReg2IndexFunctor : std::unary_function<unsigned, unsigned> {
00560   unsigned operator()(unsigned Reg) const {
00561     return Reg - TargetRegisterInfo::FirstVirtualRegister;
00562   }
00563 };
00564 
00565 } // End llvm namespace
00566 
00567 #endif