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/ADT/ArrayRef.h"
00020 #include "llvm/CodeGen/MachineBasicBlock.h"
00021 #include "llvm/CodeGen/ValueTypes.h"
00022 #include "llvm/IR/CallingConv.h"
00023 #include "llvm/MC/MCRegisterInfo.h"
00024 #include <cassert>
00025 #include <functional>
00026 
00027 namespace llvm {
00028 
00029 class BitVector;
00030 class MachineFunction;
00031 class RegScavenger;
00032 template<class T> class SmallVectorImpl;
00033 class VirtRegMap;
00034 class raw_ostream;
00035 
00036 class TargetRegisterClass {
00037 public:
00038   typedef const MCPhysReg* iterator;
00039   typedef const MCPhysReg* const_iterator;
00040   typedef const MVT::SimpleValueType* vt_iterator;
00041   typedef const TargetRegisterClass* const * sc_iterator;
00042 
00043   // Instance variables filled by tablegen, do not use!
00044   const MCRegisterClass *MC;
00045   const vt_iterator VTs;
00046   const uint32_t *SubClassMask;
00047   const uint16_t *SuperRegIndices;
00048   const sc_iterator SuperClasses;
00049   ArrayRef<MCPhysReg> (*OrderFunc)(const MachineFunction&);
00050 
00051   /// getID() - Return the register class ID number.
00052   ///
00053   unsigned getID() const { return MC->getID(); }
00054 
00055   /// getName() - Return the register class name for debugging.
00056   ///
00057   const char *getName() const { return MC->getName(); }
00058 
00059   /// begin/end - Return all of the registers in this class.
00060   ///
00061   iterator       begin() const { return MC->begin(); }
00062   iterator         end() const { return MC->end(); }
00063 
00064   /// getNumRegs - Return the number of registers in this class.
00065   ///
00066   unsigned getNumRegs() const { return MC->getNumRegs(); }
00067 
00068   /// getRegister - Return the specified register in the class.
00069   ///
00070   unsigned getRegister(unsigned i) const {
00071     return MC->getRegister(i);
00072   }
00073 
00074   /// contains - Return true if the specified register is included in this
00075   /// register class.  This does not include virtual registers.
00076   bool contains(unsigned Reg) const {
00077     return MC->contains(Reg);
00078   }
00079 
00080   /// contains - Return true if both registers are in this class.
00081   bool contains(unsigned Reg1, unsigned Reg2) const {
00082     return MC->contains(Reg1, Reg2);
00083   }
00084 
00085   /// getSize - Return the size of the register in bytes, which is also the size
00086   /// of a stack slot allocated to hold a spilled copy of this register.
00087   unsigned getSize() const { return MC->getSize(); }
00088 
00089   /// getAlignment - Return the minimum required alignment for a register of
00090   /// this class.
00091   unsigned getAlignment() const { return MC->getAlignment(); }
00092 
00093   /// getCopyCost - Return the cost of copying a value between two registers in
00094   /// this class. A negative number means the register class is very expensive
00095   /// to copy e.g. status flag register classes.
00096   int getCopyCost() const { return MC->getCopyCost(); }
00097 
00098   /// isAllocatable - Return true if this register class may be used to create
00099   /// virtual registers.
00100   bool isAllocatable() const { return MC->isAllocatable(); }
00101 
00102   /// hasType - return true if this TargetRegisterClass has the ValueType vt.
00103   ///
00104   bool hasType(EVT vt) const {
00105     for(int i = 0; VTs[i] != MVT::Other; ++i)
00106       if (EVT(VTs[i]) == vt)
00107         return true;
00108     return false;
00109   }
00110 
00111   /// vt_begin / vt_end - Loop over all of the value types that can be
00112   /// represented by values in this register class.
00113   vt_iterator vt_begin() const {
00114     return VTs;
00115   }
00116 
00117   vt_iterator vt_end() const {
00118     vt_iterator I = VTs;
00119     while (*I != MVT::Other) ++I;
00120     return I;
00121   }
00122 
00123   /// hasSubClass - return true if the specified TargetRegisterClass
00124   /// is a proper sub-class of this TargetRegisterClass.
00125   bool hasSubClass(const TargetRegisterClass *RC) const {
00126     return RC != this && hasSubClassEq(RC);
00127   }
00128 
00129   /// hasSubClassEq - Returns true if RC is a sub-class of or equal to this
00130   /// class.
00131   bool hasSubClassEq(const TargetRegisterClass *RC) const {
00132     unsigned ID = RC->getID();
00133     return (SubClassMask[ID / 32] >> (ID % 32)) & 1;
00134   }
00135 
00136   /// hasSuperClass - return true if the specified TargetRegisterClass is a
00137   /// proper super-class of this TargetRegisterClass.
00138   bool hasSuperClass(const TargetRegisterClass *RC) const {
00139     return RC->hasSubClass(this);
00140   }
00141 
00142   /// hasSuperClassEq - Returns true if RC is a super-class of or equal to this
00143   /// class.
00144   bool hasSuperClassEq(const TargetRegisterClass *RC) const {
00145     return RC->hasSubClassEq(this);
00146   }
00147 
00148   /// getSubClassMask - Returns a bit vector of subclasses, including this one.
00149   /// The vector is indexed by class IDs, see hasSubClassEq() above for how to
00150   /// use it.
00151   const uint32_t *getSubClassMask() const {
00152     return SubClassMask;
00153   }
00154 
00155   /// getSuperRegIndices - Returns a 0-terminated list of sub-register indices
00156   /// that project some super-register class into this register class. The list
00157   /// has an entry for each Idx such that:
00158   ///
00159   ///   There exists SuperRC where:
00160   ///     For all Reg in SuperRC:
00161   ///       this->contains(Reg:Idx)
00162   ///
00163   const uint16_t *getSuperRegIndices() const {
00164     return SuperRegIndices;
00165   }
00166 
00167   /// getSuperClasses - Returns a NULL terminated list of super-classes.  The
00168   /// classes are ordered by ID which is also a topological ordering from large
00169   /// to small classes.  The list does NOT include the current class.
00170   sc_iterator getSuperClasses() const {
00171     return SuperClasses;
00172   }
00173 
00174   /// isASubClass - return true if this TargetRegisterClass is a subset
00175   /// class of at least one other TargetRegisterClass.
00176   bool isASubClass() const {
00177     return SuperClasses[0] != nullptr;
00178   }
00179 
00180   /// getRawAllocationOrder - Returns the preferred order for allocating
00181   /// registers from this register class in MF. The raw order comes directly
00182   /// from the .td file and may include reserved registers that are not
00183   /// allocatable. Register allocators should also make sure to allocate
00184   /// callee-saved registers only after all the volatiles are used. The
00185   /// RegisterClassInfo class provides filtered allocation orders with
00186   /// callee-saved registers moved to the end.
00187   ///
00188   /// The MachineFunction argument can be used to tune the allocatable
00189   /// registers based on the characteristics of the function, subtarget, or
00190   /// other criteria.
00191   ///
00192   /// By default, this method returns all registers in the class.
00193   ///
00194   ArrayRef<MCPhysReg> getRawAllocationOrder(const MachineFunction &MF) const {
00195     return OrderFunc ? OrderFunc(MF) : makeArrayRef(begin(), getNumRegs());
00196   }
00197 };
00198 
00199 /// TargetRegisterInfoDesc - Extra information, not in MCRegisterDesc, about
00200 /// registers. These are used by codegen, not by MC.
00201 struct TargetRegisterInfoDesc {
00202   unsigned CostPerUse;          // Extra cost of instructions using register.
00203   bool inAllocatableClass;      // Register belongs to an allocatable regclass.
00204 };
00205 
00206 /// Each TargetRegisterClass has a per register weight, and weight
00207 /// limit which must be less than the limits of its pressure sets.
00208 struct RegClassWeight {
00209   unsigned RegWeight;
00210   unsigned WeightLimit;
00211 };
00212 
00213 /// TargetRegisterInfo base class - We assume that the target defines a static
00214 /// array of TargetRegisterDesc objects that represent all of the machine
00215 /// registers that the target has.  As such, we simply have to track a pointer
00216 /// to this array so that we can turn register number into a register
00217 /// descriptor.
00218 ///
00219 class TargetRegisterInfo : public MCRegisterInfo {
00220 public:
00221   typedef const TargetRegisterClass * const * regclass_iterator;
00222 private:
00223   const TargetRegisterInfoDesc *InfoDesc;     // Extra desc array for codegen
00224   const char *const *SubRegIndexNames;        // Names of subreg indexes.
00225   // Pointer to array of lane masks, one per sub-reg index.
00226   const unsigned *SubRegIndexLaneMasks;
00227 
00228   regclass_iterator RegClassBegin, RegClassEnd;   // List of regclasses
00229   unsigned CoveringLanes;
00230 
00231 protected:
00232   TargetRegisterInfo(const TargetRegisterInfoDesc *ID,
00233                      regclass_iterator RegClassBegin,
00234                      regclass_iterator RegClassEnd,
00235                      const char *const *SRINames,
00236                      const unsigned *SRILaneMasks,
00237                      unsigned CoveringLanes);
00238   virtual ~TargetRegisterInfo();
00239 public:
00240 
00241   // Register numbers can represent physical registers, virtual registers, and
00242   // sometimes stack slots. The unsigned values are divided into these ranges:
00243   //
00244   //   0           Not a register, can be used as a sentinel.
00245   //   [1;2^30)    Physical registers assigned by TableGen.
00246   //   [2^30;2^31) Stack slots. (Rarely used.)
00247   //   [2^31;2^32) Virtual registers assigned by MachineRegisterInfo.
00248   //
00249   // Further sentinels can be allocated from the small negative integers.
00250   // DenseMapInfo<unsigned> uses -1u and -2u.
00251 
00252   /// isStackSlot - Sometimes it is useful the be able to store a non-negative
00253   /// frame index in a variable that normally holds a register. isStackSlot()
00254   /// returns true if Reg is in the range used for stack slots.
00255   ///
00256   /// Note that isVirtualRegister() and isPhysicalRegister() cannot handle stack
00257   /// slots, so if a variable may contains a stack slot, always check
00258   /// isStackSlot() first.
00259   ///
00260   static bool isStackSlot(unsigned Reg) {
00261     return int(Reg) >= (1 << 30);
00262   }
00263 
00264   /// stackSlot2Index - Compute the frame index from a register value
00265   /// representing a stack slot.
00266   static int stackSlot2Index(unsigned Reg) {
00267     assert(isStackSlot(Reg) && "Not a stack slot");
00268     return int(Reg - (1u << 30));
00269   }
00270 
00271   /// index2StackSlot - Convert a non-negative frame index to a stack slot
00272   /// register value.
00273   static unsigned index2StackSlot(int FI) {
00274     assert(FI >= 0 && "Cannot hold a negative frame index.");
00275     return FI + (1u << 30);
00276   }
00277 
00278   /// isPhysicalRegister - Return true if the specified register number is in
00279   /// the physical register namespace.
00280   static bool isPhysicalRegister(unsigned Reg) {
00281     assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
00282     return int(Reg) > 0;
00283   }
00284 
00285   /// isVirtualRegister - Return true if the specified register number is in
00286   /// the virtual register namespace.
00287   static bool isVirtualRegister(unsigned Reg) {
00288     assert(!isStackSlot(Reg) && "Not a register! Check isStackSlot() first.");
00289     return int(Reg) < 0;
00290   }
00291 
00292   /// virtReg2Index - Convert a virtual register number to a 0-based index.
00293   /// The first virtual register in a function will get the index 0.
00294   static unsigned virtReg2Index(unsigned Reg) {
00295     assert(isVirtualRegister(Reg) && "Not a virtual register");
00296     return Reg & ~(1u << 31);
00297   }
00298 
00299   /// index2VirtReg - Convert a 0-based index to a virtual register number.
00300   /// This is the inverse operation of VirtReg2IndexFunctor below.
00301   static unsigned index2VirtReg(unsigned Index) {
00302     return Index | (1u << 31);
00303   }
00304 
00305   /// getMinimalPhysRegClass - Returns the Register Class of a physical
00306   /// register of the given type, picking the most sub register class of
00307   /// the right type that contains this physreg.
00308   const TargetRegisterClass *
00309     getMinimalPhysRegClass(unsigned Reg, EVT VT = MVT::Other) const;
00310 
00311   /// getAllocatableClass - Return the maximal subclass of the given register
00312   /// class that is alloctable, or NULL.
00313   const TargetRegisterClass *
00314     getAllocatableClass(const TargetRegisterClass *RC) const;
00315 
00316   /// getAllocatableSet - Returns a bitset indexed by register number
00317   /// indicating if a register is allocatable or not. If a register class is
00318   /// specified, returns the subset for the class.
00319   BitVector getAllocatableSet(const MachineFunction &MF,
00320                               const TargetRegisterClass *RC = nullptr) const;
00321 
00322   /// getCostPerUse - Return the additional cost of using this register instead
00323   /// of other registers in its class.
00324   unsigned getCostPerUse(unsigned RegNo) const {
00325     return InfoDesc[RegNo].CostPerUse;
00326   }
00327 
00328   /// isInAllocatableClass - Return true if the register is in the allocation
00329   /// of any register class.
00330   bool isInAllocatableClass(unsigned RegNo) const {
00331     return InfoDesc[RegNo].inAllocatableClass;
00332   }
00333 
00334   /// getSubRegIndexName - Return the human-readable symbolic target-specific
00335   /// name for the specified SubRegIndex.
00336   const char *getSubRegIndexName(unsigned SubIdx) const {
00337     assert(SubIdx && SubIdx < getNumSubRegIndices() &&
00338            "This is not a subregister index");
00339     return SubRegIndexNames[SubIdx-1];
00340   }
00341 
00342   /// getSubRegIndexLaneMask - Return a bitmask representing the parts of a
00343   /// register that are covered by SubIdx.
00344   ///
00345   /// Lane masks for sub-register indices are similar to register units for
00346   /// physical registers. The individual bits in a lane mask can't be assigned
00347   /// any specific meaning. They can be used to check if two sub-register
00348   /// indices overlap.
00349   ///
00350   /// If the target has a register such that:
00351   ///
00352   ///   getSubReg(Reg, A) overlaps getSubReg(Reg, B)
00353   ///
00354   /// then:
00355   ///
00356   ///   getSubRegIndexLaneMask(A) & getSubRegIndexLaneMask(B) != 0
00357   ///
00358   /// The converse is not necessarily true. If two lane masks have a common
00359   /// bit, the corresponding sub-registers may not overlap, but it can be
00360   /// assumed that they usually will.
00361   unsigned getSubRegIndexLaneMask(unsigned SubIdx) const {
00362     // SubIdx == 0 is allowed, it has the lane mask ~0u.
00363     assert(SubIdx < getNumSubRegIndices() && "This is not a subregister index");
00364     return SubRegIndexLaneMasks[SubIdx];
00365   }
00366 
00367   /// The lane masks returned by getSubRegIndexLaneMask() above can only be
00368   /// used to determine if sub-registers overlap - they can't be used to
00369   /// determine if a set of sub-registers completely cover another
00370   /// sub-register.
00371   ///
00372   /// The X86 general purpose registers have two lanes corresponding to the
00373   /// sub_8bit and sub_8bit_hi sub-registers. Both sub_32bit and sub_16bit have
00374   /// lane masks '3', but the sub_16bit sub-register doesn't fully cover the
00375   /// sub_32bit sub-register.
00376   ///
00377   /// On the other hand, the ARM NEON lanes fully cover their registers: The
00378   /// dsub_0 sub-register is completely covered by the ssub_0 and ssub_1 lanes.
00379   /// This is related to the CoveredBySubRegs property on register definitions.
00380   ///
00381   /// This function returns a bit mask of lanes that completely cover their
00382   /// sub-registers. More precisely, given:
00383   ///
00384   ///   Covering = getCoveringLanes();
00385   ///   MaskA = getSubRegIndexLaneMask(SubA);
00386   ///   MaskB = getSubRegIndexLaneMask(SubB);
00387   ///
00388   /// If (MaskA & ~(MaskB & Covering)) == 0, then SubA is completely covered by
00389   /// SubB.
00390   unsigned getCoveringLanes() const { return CoveringLanes; }
00391 
00392   /// regsOverlap - Returns true if the two registers are equal or alias each
00393   /// other. The registers may be virtual register.
00394   bool regsOverlap(unsigned regA, unsigned regB) const {
00395     if (regA == regB) return true;
00396     if (isVirtualRegister(regA) || isVirtualRegister(regB))
00397       return false;
00398 
00399     // Regunits are numerically ordered. Find a common unit.
00400     MCRegUnitIterator RUA(regA, this);
00401     MCRegUnitIterator RUB(regB, this);
00402     do {
00403       if (*RUA == *RUB) return true;
00404       if (*RUA < *RUB) ++RUA;
00405       else             ++RUB;
00406     } while (RUA.isValid() && RUB.isValid());
00407     return false;
00408   }
00409 
00410   /// hasRegUnit - Returns true if Reg contains RegUnit.
00411   bool hasRegUnit(unsigned Reg, unsigned RegUnit) const {
00412     for (MCRegUnitIterator Units(Reg, this); Units.isValid(); ++Units)
00413       if (*Units == RegUnit)
00414         return true;
00415     return false;
00416   }
00417 
00418   /// getCalleeSavedRegs - Return a null-terminated list of all of the
00419   /// callee saved registers on this target. The register should be in the
00420   /// order of desired callee-save stack frame offset. The first register is
00421   /// closest to the incoming stack pointer if stack grows down, and vice versa.
00422   ///
00423   virtual const MCPhysReg*
00424   getCalleeSavedRegs(const MachineFunction *MF = nullptr) const = 0;
00425 
00426   /// getCallPreservedMask - Return a mask of call-preserved registers for the
00427   /// given calling convention on the current sub-target.  The mask should
00428   /// include all call-preserved aliases.  This is used by the register
00429   /// allocator to determine which registers can be live across a call.
00430   ///
00431   /// The mask is an array containing (TRI::getNumRegs()+31)/32 entries.
00432   /// A set bit indicates that all bits of the corresponding register are
00433   /// preserved across the function call.  The bit mask is expected to be
00434   /// sub-register complete, i.e. if A is preserved, so are all its
00435   /// sub-registers.
00436   ///
00437   /// Bits are numbered from the LSB, so the bit for physical register Reg can
00438   /// be found as (Mask[Reg / 32] >> Reg % 32) & 1.
00439   ///
00440   /// A NULL pointer means that no register mask will be used, and call
00441   /// instructions should use implicit-def operands to indicate call clobbered
00442   /// registers.
00443   ///
00444   virtual const uint32_t *getCallPreservedMask(CallingConv::ID) const {
00445     // The default mask clobbers everything.  All targets should override.
00446     return nullptr;
00447   }
00448 
00449   /// getReservedRegs - Returns a bitset indexed by physical register number
00450   /// indicating if a register is a special register that has particular uses
00451   /// and should be considered unavailable at all times, e.g. SP, RA. This is
00452   /// used by register scavenger to determine what registers are free.
00453   virtual BitVector getReservedRegs(const MachineFunction &MF) const = 0;
00454 
00455   /// getMatchingSuperReg - Return a super-register of the specified register
00456   /// Reg so its sub-register of index SubIdx is Reg.
00457   unsigned getMatchingSuperReg(unsigned Reg, unsigned SubIdx,
00458                                const TargetRegisterClass *RC) const {
00459     return MCRegisterInfo::getMatchingSuperReg(Reg, SubIdx, RC->MC);
00460   }
00461 
00462   /// getMatchingSuperRegClass - Return a subclass of the specified register
00463   /// class A so that each register in it has a sub-register of the
00464   /// specified sub-register index which is in the specified register class B.
00465   ///
00466   /// TableGen will synthesize missing A sub-classes.
00467   virtual const TargetRegisterClass *
00468   getMatchingSuperRegClass(const TargetRegisterClass *A,
00469                            const TargetRegisterClass *B, unsigned Idx) const;
00470 
00471   /// getSubClassWithSubReg - Returns the largest legal sub-class of RC that
00472   /// supports the sub-register index Idx.
00473   /// If no such sub-class exists, return NULL.
00474   /// If all registers in RC already have an Idx sub-register, return RC.
00475   ///
00476   /// TableGen generates a version of this function that is good enough in most
00477   /// cases.  Targets can override if they have constraints that TableGen
00478   /// doesn't understand.  For example, the x86 sub_8bit sub-register index is
00479   /// supported by the full GR32 register class in 64-bit mode, but only by the
00480   /// GR32_ABCD regiister class in 32-bit mode.
00481   ///
00482   /// TableGen will synthesize missing RC sub-classes.
00483   virtual const TargetRegisterClass *
00484   getSubClassWithSubReg(const TargetRegisterClass *RC, unsigned Idx) const {
00485     assert(Idx == 0 && "Target has no sub-registers");
00486     return RC;
00487   }
00488 
00489   /// composeSubRegIndices - Return the subregister index you get from composing
00490   /// two subregister indices.
00491   ///
00492   /// The special null sub-register index composes as the identity.
00493   ///
00494   /// If R:a:b is the same register as R:c, then composeSubRegIndices(a, b)
00495   /// returns c. Note that composeSubRegIndices does not tell you about illegal
00496   /// compositions. If R does not have a subreg a, or R:a does not have a subreg
00497   /// b, composeSubRegIndices doesn't tell you.
00498   ///
00499   /// The ARM register Q0 has two D subregs dsub_0:D0 and dsub_1:D1. It also has
00500   /// ssub_0:S0 - ssub_3:S3 subregs.
00501   /// If you compose subreg indices dsub_1, ssub_0 you get ssub_2.
00502   ///
00503   unsigned composeSubRegIndices(unsigned a, unsigned b) const {
00504     if (!a) return b;
00505     if (!b) return a;
00506     return composeSubRegIndicesImpl(a, b);
00507   }
00508 
00509 protected:
00510   /// Overridden by TableGen in targets that have sub-registers.
00511   virtual unsigned composeSubRegIndicesImpl(unsigned, unsigned) const {
00512     llvm_unreachable("Target has no sub-registers");
00513   }
00514 
00515 public:
00516   /// getCommonSuperRegClass - Find a common super-register class if it exists.
00517   ///
00518   /// Find a register class, SuperRC and two sub-register indices, PreA and
00519   /// PreB, such that:
00520   ///
00521   ///   1. PreA + SubA == PreB + SubB  (using composeSubRegIndices()), and
00522   ///
00523   ///   2. For all Reg in SuperRC: Reg:PreA in RCA and Reg:PreB in RCB, and
00524   ///
00525   ///   3. SuperRC->getSize() >= max(RCA->getSize(), RCB->getSize()).
00526   ///
00527   /// SuperRC will be chosen such that no super-class of SuperRC satisfies the
00528   /// requirements, and there is no register class with a smaller spill size
00529   /// that satisfies the requirements.
00530   ///
00531   /// SubA and SubB must not be 0. Use getMatchingSuperRegClass() instead.
00532   ///
00533   /// Either of the PreA and PreB sub-register indices may be returned as 0. In
00534   /// that case, the returned register class will be a sub-class of the
00535   /// corresponding argument register class.
00536   ///
00537   /// The function returns NULL if no register class can be found.
00538   ///
00539   const TargetRegisterClass*
00540   getCommonSuperRegClass(const TargetRegisterClass *RCA, unsigned SubA,
00541                          const TargetRegisterClass *RCB, unsigned SubB,
00542                          unsigned &PreA, unsigned &PreB) const;
00543 
00544   //===--------------------------------------------------------------------===//
00545   // Register Class Information
00546   //
00547 
00548   /// Register class iterators
00549   ///
00550   regclass_iterator regclass_begin() const { return RegClassBegin; }
00551   regclass_iterator regclass_end() const { return RegClassEnd; }
00552 
00553   unsigned getNumRegClasses() const {
00554     return (unsigned)(regclass_end()-regclass_begin());
00555   }
00556 
00557   /// getRegClass - Returns the register class associated with the enumeration
00558   /// value.  See class MCOperandInfo.
00559   const TargetRegisterClass *getRegClass(unsigned i) const {
00560     assert(i < getNumRegClasses() && "Register Class ID out of range");
00561     return RegClassBegin[i];
00562   }
00563 
00564   /// getCommonSubClass - find the largest common subclass of A and B. Return
00565   /// NULL if there is no common subclass.
00566   const TargetRegisterClass *
00567   getCommonSubClass(const TargetRegisterClass *A,
00568                     const TargetRegisterClass *B) const;
00569 
00570   /// getPointerRegClass - Returns a TargetRegisterClass used for pointer
00571   /// values.  If a target supports multiple different pointer register classes,
00572   /// kind specifies which one is indicated.
00573   virtual const TargetRegisterClass *
00574   getPointerRegClass(const MachineFunction &MF, unsigned Kind=0) const {
00575     llvm_unreachable("Target didn't implement getPointerRegClass!");
00576   }
00577 
00578   /// getCrossCopyRegClass - Returns a legal register class to copy a register
00579   /// in the specified class to or from. If it is possible to copy the register
00580   /// directly without using a cross register class copy, return the specified
00581   /// RC. Returns NULL if it is not possible to copy between a two registers of
00582   /// the specified class.
00583   virtual const TargetRegisterClass *
00584   getCrossCopyRegClass(const TargetRegisterClass *RC) const {
00585     return RC;
00586   }
00587 
00588   /// getLargestLegalSuperClass - Returns the largest super class of RC that is
00589   /// legal to use in the current sub-target and has the same spill size.
00590   /// The returned register class can be used to create virtual registers which
00591   /// means that all its registers can be copied and spilled.
00592   virtual const TargetRegisterClass*
00593   getLargestLegalSuperClass(const TargetRegisterClass *RC) const {
00594     /// The default implementation is very conservative and doesn't allow the
00595     /// register allocator to inflate register classes.
00596     return RC;
00597   }
00598 
00599   /// getRegPressureLimit - Return the register pressure "high water mark" for
00600   /// the specific register class. The scheduler is in high register pressure
00601   /// mode (for the specific register class) if it goes over the limit.
00602   ///
00603   /// Note: this is the old register pressure model that relies on a manually
00604   /// specified representative register class per value type.
00605   virtual unsigned getRegPressureLimit(const TargetRegisterClass *RC,
00606                                        MachineFunction &MF) const {
00607     return 0;
00608   }
00609 
00610   /// Get the weight in units of pressure for this register class.
00611   virtual const RegClassWeight &getRegClassWeight(
00612     const TargetRegisterClass *RC) const = 0;
00613 
00614   /// Get the weight in units of pressure for this register unit.
00615   virtual unsigned getRegUnitWeight(unsigned RegUnit) const = 0;
00616 
00617   /// Get the number of dimensions of register pressure.
00618   virtual unsigned getNumRegPressureSets() const = 0;
00619 
00620   /// Get the name of this register unit pressure set.
00621   virtual const char *getRegPressureSetName(unsigned Idx) const = 0;
00622 
00623   /// Get the register unit pressure limit for this dimension.
00624   /// This limit must be adjusted dynamically for reserved registers.
00625   virtual unsigned getRegPressureSetLimit(unsigned Idx) const = 0;
00626 
00627   /// Get the dimensions of register pressure impacted by this register class.
00628   /// Returns a -1 terminated array of pressure set IDs.
00629   virtual const int *getRegClassPressureSets(
00630     const TargetRegisterClass *RC) const = 0;
00631 
00632   /// Get the dimensions of register pressure impacted by this register unit.
00633   /// Returns a -1 terminated array of pressure set IDs.
00634   virtual const int *getRegUnitPressureSets(unsigned RegUnit) const = 0;
00635 
00636   /// Get a list of 'hint' registers that the register allocator should try
00637   /// first when allocating a physical register for the virtual register
00638   /// VirtReg. These registers are effectively moved to the front of the
00639   /// allocation order.
00640   ///
00641   /// The Order argument is the allocation order for VirtReg's register class
00642   /// as returned from RegisterClassInfo::getOrder(). The hint registers must
00643   /// come from Order, and they must not be reserved.
00644   ///
00645   /// The default implementation of this function can resolve
00646   /// target-independent hints provided to MRI::setRegAllocationHint with
00647   /// HintType == 0. Targets that override this function should defer to the
00648   /// default implementation if they have no reason to change the allocation
00649   /// order for VirtReg. There may be target-independent hints.
00650   virtual void getRegAllocationHints(unsigned VirtReg,
00651                                      ArrayRef<MCPhysReg> Order,
00652                                      SmallVectorImpl<MCPhysReg> &Hints,
00653                                      const MachineFunction &MF,
00654                                      const VirtRegMap *VRM = nullptr) const;
00655 
00656   /// avoidWriteAfterWrite - Return true if the register allocator should avoid
00657   /// writing a register from RC in two consecutive instructions.
00658   /// This can avoid pipeline stalls on certain architectures.
00659   /// It does cause increased register pressure, though.
00660   virtual bool avoidWriteAfterWrite(const TargetRegisterClass *RC) const {
00661     return false;
00662   }
00663 
00664   /// UpdateRegAllocHint - A callback to allow target a chance to update
00665   /// register allocation hints when a register is "changed" (e.g. coalesced)
00666   /// to another register. e.g. On ARM, some virtual registers should target
00667   /// register pairs, if one of pair is coalesced to another register, the
00668   /// allocation hint of the other half of the pair should be changed to point
00669   /// to the new register.
00670   virtual void UpdateRegAllocHint(unsigned Reg, unsigned NewReg,
00671                                   MachineFunction &MF) const {
00672     // Do nothing.
00673   }
00674 
00675   /// Allow the target to reverse allocation order of local live ranges. This
00676   /// will generally allocate shorter local live ranges first. For targets with
00677   /// many registers, this could reduce regalloc compile time by a large
00678   /// factor. It is disabled by default for three reasons:
00679   /// (1) Top-down allocation is simpler and easier to debug for targets that
00680   /// don't benefit from reversing the order.
00681   /// (2) Bottom-up allocation could result in poor evicition decisions on some
00682   /// targets affecting the performance of compiled code.
00683   /// (3) Bottom-up allocation is no longer guaranteed to optimally color.
00684   virtual bool reverseLocalAssignment() const { return false; }
00685 
00686   /// Allow the target to override register assignment heuristics based on the
00687   /// live range size. If this returns false, then local live ranges are always
00688   /// assigned in order regardless of their size. This is a temporary hook for
00689   /// debugging downstream codegen failures exposed by regalloc.
00690   virtual bool mayOverrideLocalAssignment() const { return true; }
00691 
00692   /// Allow the target to override the cost of using a callee-saved register for
00693   /// the first time. Default value of 0 means we will use a callee-saved
00694   /// register if it is available.
00695   virtual unsigned getCSRFirstUseCost() const { return 0; }
00696 
00697   /// requiresRegisterScavenging - returns true if the target requires (and can
00698   /// make use of) the register scavenger.
00699   virtual bool requiresRegisterScavenging(const MachineFunction &MF) const {
00700     return false;
00701   }
00702 
00703   /// useFPForScavengingIndex - returns true if the target wants to use
00704   /// frame pointer based accesses to spill to the scavenger emergency spill
00705   /// slot.
00706   virtual bool useFPForScavengingIndex(const MachineFunction &MF) const {
00707     return true;
00708   }
00709 
00710   /// requiresFrameIndexScavenging - returns true if the target requires post
00711   /// PEI scavenging of registers for materializing frame index constants.
00712   virtual bool requiresFrameIndexScavenging(const MachineFunction &MF) const {
00713     return false;
00714   }
00715 
00716   /// requiresVirtualBaseRegisters - Returns true if the target wants the
00717   /// LocalStackAllocation pass to be run and virtual base registers
00718   /// used for more efficient stack access.
00719   virtual bool requiresVirtualBaseRegisters(const MachineFunction &MF) const {
00720     return false;
00721   }
00722 
00723   /// hasReservedSpillSlot - Return true if target has reserved a spill slot in
00724   /// the stack frame of the given function for the specified register. e.g. On
00725   /// x86, if the frame register is required, the first fixed stack object is
00726   /// reserved as its spill slot. This tells PEI not to create a new stack frame
00727   /// object for the given register. It should be called only after
00728   /// processFunctionBeforeCalleeSavedScan().
00729   virtual bool hasReservedSpillSlot(const MachineFunction &MF, unsigned Reg,
00730                                     int &FrameIdx) const {
00731     return false;
00732   }
00733 
00734   /// trackLivenessAfterRegAlloc - returns true if the live-ins should be tracked
00735   /// after register allocation.
00736   virtual bool trackLivenessAfterRegAlloc(const MachineFunction &MF) const {
00737     return false;
00738   }
00739 
00740   /// needsStackRealignment - true if storage within the function requires the
00741   /// stack pointer to be aligned more than the normal calling convention calls
00742   /// for.
00743   virtual bool needsStackRealignment(const MachineFunction &MF) const {
00744     return false;
00745   }
00746 
00747   /// getFrameIndexInstrOffset - Get the offset from the referenced frame
00748   /// index in the instruction, if there is one.
00749   virtual int64_t getFrameIndexInstrOffset(const MachineInstr *MI,
00750                                            int Idx) const {
00751     return 0;
00752   }
00753 
00754   /// needsFrameBaseReg - Returns true if the instruction's frame index
00755   /// reference would be better served by a base register other than FP
00756   /// or SP. Used by LocalStackFrameAllocation to determine which frame index
00757   /// references it should create new base registers for.
00758   virtual bool needsFrameBaseReg(MachineInstr *MI, int64_t Offset) const {
00759     return false;
00760   }
00761 
00762   /// materializeFrameBaseRegister - Insert defining instruction(s) for
00763   /// BaseReg to be a pointer to FrameIdx before insertion point I.
00764   virtual void materializeFrameBaseRegister(MachineBasicBlock *MBB,
00765                                             unsigned BaseReg, int FrameIdx,
00766                                             int64_t Offset) const {
00767     llvm_unreachable("materializeFrameBaseRegister does not exist on this "
00768                      "target");
00769   }
00770 
00771   /// resolveFrameIndex - Resolve a frame index operand of an instruction
00772   /// to reference the indicated base register plus offset instead.
00773   virtual void resolveFrameIndex(MachineInstr &MI, unsigned BaseReg,
00774                                  int64_t Offset) const {
00775     llvm_unreachable("resolveFrameIndex does not exist on this target");
00776   }
00777 
00778   /// isFrameOffsetLegal - Determine whether a given offset immediate is
00779   /// encodable to resolve a frame index.
00780   virtual bool isFrameOffsetLegal(const MachineInstr *MI,
00781                                   int64_t Offset) const {
00782     llvm_unreachable("isFrameOffsetLegal does not exist on this target");
00783   }
00784 
00785 
00786   /// saveScavengerRegister - Spill the register so it can be used by the
00787   /// register scavenger. Return true if the register was spilled, false
00788   /// otherwise. If this function does not spill the register, the scavenger
00789   /// will instead spill it to the emergency spill slot.
00790   ///
00791   virtual bool saveScavengerRegister(MachineBasicBlock &MBB,
00792                                      MachineBasicBlock::iterator I,
00793                                      MachineBasicBlock::iterator &UseMI,
00794                                      const TargetRegisterClass *RC,
00795                                      unsigned Reg) const {
00796     return false;
00797   }
00798 
00799   /// eliminateFrameIndex - This method must be overriden to eliminate abstract
00800   /// frame indices from instructions which may use them.  The instruction
00801   /// referenced by the iterator contains an MO_FrameIndex operand which must be
00802   /// eliminated by this method.  This method may modify or replace the
00803   /// specified instruction, as long as it keeps the iterator pointing at the
00804   /// finished product.  SPAdj is the SP adjustment due to call frame setup
00805   /// instruction.  FIOperandNum is the FI operand number.
00806   virtual void eliminateFrameIndex(MachineBasicBlock::iterator MI,
00807                                    int SPAdj, unsigned FIOperandNum,
00808                                    RegScavenger *RS = nullptr) const = 0;
00809 
00810   //===--------------------------------------------------------------------===//
00811   /// Subtarget Hooks
00812 
00813   /// \brief SrcRC and DstRC will be morphed into NewRC if this returns true.
00814   virtual bool shouldCoalesce(MachineInstr *MI,
00815                               const TargetRegisterClass *SrcRC,
00816                               unsigned SubReg,
00817                               const TargetRegisterClass *DstRC,
00818                               unsigned DstSubReg,
00819                               const TargetRegisterClass *NewRC) const
00820   { return true; }
00821 
00822   //===--------------------------------------------------------------------===//
00823   /// Debug information queries.
00824 
00825   /// getFrameRegister - This method should return the register used as a base
00826   /// for values allocated in the current stack frame.
00827   virtual unsigned getFrameRegister(const MachineFunction &MF) const = 0;
00828 };
00829 
00830 
00831 //===----------------------------------------------------------------------===//
00832 //                           SuperRegClassIterator
00833 //===----------------------------------------------------------------------===//
00834 //
00835 // Iterate over the possible super-registers for a given register class. The
00836 // iterator will visit a list of pairs (Idx, Mask) corresponding to the
00837 // possible classes of super-registers.
00838 //
00839 // Each bit mask will have at least one set bit, and each set bit in Mask
00840 // corresponds to a SuperRC such that:
00841 //
00842 //   For all Reg in SuperRC: Reg:Idx is in RC.
00843 //
00844 // The iterator can include (O, RC->getSubClassMask()) as the first entry which
00845 // also satisfies the above requirement, assuming Reg:0 == Reg.
00846 //
00847 class SuperRegClassIterator {
00848   const unsigned RCMaskWords;
00849   unsigned SubReg;
00850   const uint16_t *Idx;
00851   const uint32_t *Mask;
00852 
00853 public:
00854   /// Create a SuperRegClassIterator that visits all the super-register classes
00855   /// of RC. When IncludeSelf is set, also include the (0, sub-classes) entry.
00856   SuperRegClassIterator(const TargetRegisterClass *RC,
00857                         const TargetRegisterInfo *TRI,
00858                         bool IncludeSelf = false)
00859     : RCMaskWords((TRI->getNumRegClasses() + 31) / 32),
00860       SubReg(0),
00861       Idx(RC->getSuperRegIndices()),
00862       Mask(RC->getSubClassMask()) {
00863     if (!IncludeSelf)
00864       ++*this;
00865   }
00866 
00867   /// Returns true if this iterator is still pointing at a valid entry.
00868   bool isValid() const { return Idx; }
00869 
00870   /// Returns the current sub-register index.
00871   unsigned getSubReg() const { return SubReg; }
00872 
00873   /// Returns the bit mask if register classes that getSubReg() projects into
00874   /// RC.
00875   const uint32_t *getMask() const { return Mask; }
00876 
00877   /// Advance iterator to the next entry.
00878   void operator++() {
00879     assert(isValid() && "Cannot move iterator past end.");
00880     Mask += RCMaskWords;
00881     SubReg = *Idx++;
00882     if (!SubReg)
00883       Idx = nullptr;
00884   }
00885 };
00886 
00887 // This is useful when building IndexedMaps keyed on virtual registers
00888 struct VirtReg2IndexFunctor : public std::unary_function<unsigned, unsigned> {
00889   unsigned operator()(unsigned Reg) const {
00890     return TargetRegisterInfo::virtReg2Index(Reg);
00891   }
00892 };
00893 
00894 /// PrintReg - Helper class for printing registers on a raw_ostream.
00895 /// Prints virtual and physical registers with or without a TRI instance.
00896 ///
00897 /// The format is:
00898 ///   %noreg          - NoRegister
00899 ///   %vreg5          - a virtual register.
00900 ///   %vreg5:sub_8bit - a virtual register with sub-register index (with TRI).
00901 ///   %EAX            - a physical register
00902 ///   %physreg17      - a physical register when no TRI instance given.
00903 ///
00904 /// Usage: OS << PrintReg(Reg, TRI) << '\n';
00905 ///
00906 class PrintReg {
00907   const TargetRegisterInfo *TRI;
00908   unsigned Reg;
00909   unsigned SubIdx;
00910 public:
00911   explicit PrintReg(unsigned reg, const TargetRegisterInfo *tri = nullptr,
00912                     unsigned subidx = 0)
00913     : TRI(tri), Reg(reg), SubIdx(subidx) {}
00914   void print(raw_ostream&) const;
00915 };
00916 
00917 static inline raw_ostream &operator<<(raw_ostream &OS, const PrintReg &PR) {
00918   PR.print(OS);
00919   return OS;
00920 }
00921 
00922 /// PrintRegUnit - Helper class for printing register units on a raw_ostream.
00923 ///
00924 /// Register units are named after their root registers:
00925 ///
00926 ///   AL      - Single root.
00927 ///   FP0~ST7 - Dual roots.
00928 ///
00929 /// Usage: OS << PrintRegUnit(Unit, TRI) << '\n';
00930 ///
00931 class PrintRegUnit {
00932 protected:
00933   const TargetRegisterInfo *TRI;
00934   unsigned Unit;
00935 public:
00936   PrintRegUnit(unsigned unit, const TargetRegisterInfo *tri)
00937     : TRI(tri), Unit(unit) {}
00938   void print(raw_ostream&) const;
00939 };
00940 
00941 static inline raw_ostream &operator<<(raw_ostream &OS, const PrintRegUnit &PR) {
00942   PR.print(OS);
00943   return OS;
00944 }
00945 
00946 /// PrintVRegOrUnit - It is often convenient to track virtual registers and
00947 /// physical register units in the same list.
00948 class PrintVRegOrUnit : protected PrintRegUnit {
00949 public:
00950   PrintVRegOrUnit(unsigned VRegOrUnit, const TargetRegisterInfo *tri)
00951     : PrintRegUnit(VRegOrUnit, tri) {}
00952   void print(raw_ostream&) const;
00953 };
00954 
00955 static inline raw_ostream &operator<<(raw_ostream &OS,
00956                                       const PrintVRegOrUnit &PR) {
00957   PR.print(OS);
00958   return OS;
00959 }
00960 
00961 } // End llvm namespace
00962 
00963 #endif