LLVM API Documentation

RegisterClassInfo.h
Go to the documentation of this file.
00001 //===-- RegisterClassInfo.h - Dynamic Register Class Info -*- 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 implements the RegisterClassInfo class which provides dynamic
00011 // information about target register classes. Callee saved and reserved
00012 // registers depends on calling conventions and other dynamic information, so
00013 // some things cannot be determined statically.
00014 //
00015 //===----------------------------------------------------------------------===//
00016 
00017 #ifndef LLVM_CODEGEN_REGISTERCLASSINFO_H
00018 #define LLVM_CODEGEN_REGISTERCLASSINFO_H
00019 
00020 #include "llvm/ADT/ArrayRef.h"
00021 #include "llvm/ADT/BitVector.h"
00022 #include "llvm/Target/TargetRegisterInfo.h"
00023 
00024 namespace llvm {
00025 
00026 class RegisterClassInfo {
00027   struct RCInfo {
00028     unsigned Tag;
00029     unsigned NumRegs;
00030     bool ProperSubClass;
00031     uint8_t MinCost;
00032     uint16_t LastCostChange;
00033     std::unique_ptr<MCPhysReg[]> Order;
00034 
00035     RCInfo()
00036       : Tag(0), NumRegs(0), ProperSubClass(false), MinCost(0),
00037         LastCostChange(0) {}
00038 
00039     operator ArrayRef<MCPhysReg>() const {
00040       return makeArrayRef(Order.get(), NumRegs);
00041     }
00042   };
00043 
00044   // Brief cached information for each register class.
00045   std::unique_ptr<RCInfo[]> RegClass;
00046 
00047   // Tag changes whenever cached information needs to be recomputed. An RCInfo
00048   // entry is valid when its tag matches.
00049   unsigned Tag;
00050 
00051   const MachineFunction *MF;
00052   const TargetRegisterInfo *TRI;
00053 
00054   // Callee saved registers of last MF. Assumed to be valid until the next
00055   // runOnFunction() call.
00056   const MCPhysReg *CalleeSaved;
00057 
00058   // Map register number to CalleeSaved index + 1;
00059   SmallVector<uint8_t, 4> CSRNum;
00060 
00061   // Reserved registers in the current MF.
00062   BitVector Reserved;
00063 
00064   std::unique_ptr<unsigned[]> PSetLimits;
00065 
00066   // Compute all information about RC.
00067   void compute(const TargetRegisterClass *RC) const;
00068 
00069   // Return an up-to-date RCInfo for RC.
00070   const RCInfo &get(const TargetRegisterClass *RC) const {
00071     const RCInfo &RCI = RegClass[RC->getID()];
00072     if (Tag != RCI.Tag)
00073       compute(RC);
00074     return RCI;
00075   }
00076 
00077 public:
00078   RegisterClassInfo();
00079 
00080   /// runOnFunction - Prepare to answer questions about MF. This must be called
00081   /// before any other methods are used.
00082   void runOnMachineFunction(const MachineFunction &MF);
00083 
00084   /// getNumAllocatableRegs - Returns the number of actually allocatable
00085   /// registers in RC in the current function.
00086   unsigned getNumAllocatableRegs(const TargetRegisterClass *RC) const {
00087     return get(RC).NumRegs;
00088   }
00089 
00090   /// getOrder - Returns the preferred allocation order for RC. The order
00091   /// contains no reserved registers, and registers that alias callee saved
00092   /// registers come last.
00093   ArrayRef<MCPhysReg> getOrder(const TargetRegisterClass *RC) const {
00094     return get(RC);
00095   }
00096 
00097   /// isProperSubClass - Returns true if RC has a legal super-class with more
00098   /// allocatable registers.
00099   ///
00100   /// Register classes like GR32_NOSP are not proper sub-classes because %esp
00101   /// is not allocatable.  Similarly, tGPR is not a proper sub-class in Thumb
00102   /// mode because the GPR super-class is not legal.
00103   bool isProperSubClass(const TargetRegisterClass *RC) const {
00104     return get(RC).ProperSubClass;
00105   }
00106 
00107   /// getLastCalleeSavedAlias - Returns the last callee saved register that
00108   /// overlaps PhysReg, or 0 if Reg doesn't overlap a CSR.
00109   unsigned getLastCalleeSavedAlias(unsigned PhysReg) const {
00110     assert(TargetRegisterInfo::isPhysicalRegister(PhysReg));
00111     if (unsigned N = CSRNum[PhysReg])
00112       return CalleeSaved[N-1];
00113     return 0;
00114   }
00115 
00116   /// Get the minimum register cost in RC's allocation order.
00117   /// This is the smallest value returned by TRI->getCostPerUse(Reg) for all
00118   /// the registers in getOrder(RC).
00119   unsigned getMinCost(const TargetRegisterClass *RC) {
00120     return get(RC).MinCost;
00121   }
00122 
00123   /// Get the position of the last cost change in getOrder(RC).
00124   ///
00125   /// All registers in getOrder(RC).slice(getLastCostChange(RC)) will have the
00126   /// same cost according to TRI->getCostPerUse().
00127   unsigned getLastCostChange(const TargetRegisterClass *RC) {
00128     return get(RC).LastCostChange;
00129   }
00130 
00131   /// Get the register unit limit for the given pressure set index.
00132   ///
00133   /// RegisterClassInfo adjusts this limit for reserved registers.
00134   unsigned getRegPressureSetLimit(unsigned Idx) const {
00135     if (!PSetLimits[Idx])
00136       PSetLimits[Idx] = computePSetLimit(Idx);
00137     return PSetLimits[Idx];
00138   }
00139 
00140 protected:
00141   unsigned computePSetLimit(unsigned Idx) const;
00142 };
00143 } // end namespace llvm
00144 
00145 #endif