LLVM API Documentation

MachineRegisterInfo.h

Go to the documentation of this file.
00001 //===-- llvm/CodeGen/MachineRegisterInfo.h ----------------------*- 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 defines the MachineRegisterInfo class.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #ifndef LLVM_CODEGEN_MACHINEREGISTERINFO_H
00015 #define LLVM_CODEGEN_MACHINEREGISTERINFO_H
00016 
00017 #include "llvm/Target/TargetRegisterInfo.h"
00018 #include "llvm/ADT/BitVector.h"
00019 #include "llvm/ADT/iterator.h"
00020 #include <vector>
00021 
00022 namespace llvm {
00023   
00024 /// MachineRegisterInfo - Keep track of information for virtual and physical
00025 /// registers, including vreg register classes, use/def chains for registers,
00026 /// etc.
00027 class MachineRegisterInfo {
00028   /// VRegInfo - Information we keep for each virtual register.  The entries in
00029   /// this vector are actually converted to vreg numbers by adding the 
00030   /// TargetRegisterInfo::FirstVirtualRegister delta to their index.
00031   ///
00032   /// Each element in this list contains the register class of the vreg and the
00033   /// start of the use/def list for the register.
00034   std::vector<std::pair<const TargetRegisterClass*, MachineOperand*> > VRegInfo;
00035 
00036   /// RegClassVRegMap - This vector acts as a map from TargetRegisterClass to
00037   /// virtual registers. For each target register class, it keeps a list of
00038   /// virtual registers belonging to the class.
00039   std::vector<std::vector<unsigned> > RegClass2VRegMap;
00040   
00041   /// PhysRegUseDefLists - This is an array of the head of the use/def list for
00042   /// physical registers.
00043   MachineOperand **PhysRegUseDefLists; 
00044   
00045   /// UsedPhysRegs - This is a bit vector that is computed and set by the
00046   /// register allocator, and must be kept up to date by passes that run after
00047   /// register allocation (though most don't modify this).  This is used
00048   /// so that the code generator knows which callee save registers to save and
00049   /// for other target specific uses.
00050   BitVector UsedPhysRegs;
00051   
00052   /// LiveIns/LiveOuts - Keep track of the physical registers that are
00053   /// livein/liveout of the function.  Live in values are typically arguments in
00054   /// registers, live out values are typically return values in registers.
00055   /// LiveIn values are allowed to have virtual registers associated with them,
00056   /// stored in the second element.
00057   std::vector<std::pair<unsigned, unsigned> > LiveIns;
00058   std::vector<unsigned> LiveOuts;
00059   
00060   MachineRegisterInfo(const MachineRegisterInfo&); // DO NOT IMPLEMENT
00061   void operator=(const MachineRegisterInfo&);      // DO NOT IMPLEMENT
00062 public:
00063   explicit MachineRegisterInfo(const TargetRegisterInfo &TRI);
00064   ~MachineRegisterInfo();
00065   
00066   //===--------------------------------------------------------------------===//
00067   // Register Info
00068   //===--------------------------------------------------------------------===//
00069 
00070   /// reg_begin/reg_end - Provide iteration support to walk over all definitions
00071   /// and uses of a register within the MachineFunction that corresponds to this
00072   /// MachineRegisterInfo object.
00073   template<bool Uses, bool Defs>
00074   class defusechain_iterator;
00075 
00076   /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
00077   /// register.
00078   typedef defusechain_iterator<true,true> reg_iterator;
00079   reg_iterator reg_begin(unsigned RegNo) const {
00080     return reg_iterator(getRegUseDefListHead(RegNo));
00081   }
00082   static reg_iterator reg_end() { return reg_iterator(0); }
00083 
00084   /// reg_empty - Return true if there are no instructions using or defining the
00085   /// specified register (it may be live-in).
00086   bool reg_empty(unsigned RegNo) const { return reg_begin(RegNo) == reg_end(); }
00087 
00088   /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
00089   typedef defusechain_iterator<false,true> def_iterator;
00090   def_iterator def_begin(unsigned RegNo) const {
00091     return def_iterator(getRegUseDefListHead(RegNo));
00092   }
00093   static def_iterator def_end() { return def_iterator(0); }
00094 
00095   /// def_empty - Return true if there are no instructions defining the
00096   /// specified register (it may be live-in).
00097   bool def_empty(unsigned RegNo) const { return def_begin(RegNo) == def_end(); }
00098 
00099   /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
00100   typedef defusechain_iterator<true,false> use_iterator;
00101   use_iterator use_begin(unsigned RegNo) const {
00102     return use_iterator(getRegUseDefListHead(RegNo));
00103   }
00104   static use_iterator use_end() { return use_iterator(0); }
00105   
00106   /// use_empty - Return true if there are no instructions using the specified
00107   /// register.
00108   bool use_empty(unsigned RegNo) const { return use_begin(RegNo) == use_end(); }
00109 
00110   
00111   /// replaceRegWith - Replace all instances of FromReg with ToReg in the
00112   /// machine function.  This is like llvm-level X->replaceAllUsesWith(Y),
00113   /// except that it also changes any definitions of the register as well.
00114   void replaceRegWith(unsigned FromReg, unsigned ToReg);
00115   
00116   /// getRegUseDefListHead - Return the head pointer for the register use/def
00117   /// list for the specified virtual or physical register.
00118   MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
00119     if (RegNo < TargetRegisterInfo::FirstVirtualRegister)
00120       return PhysRegUseDefLists[RegNo];
00121     RegNo -= TargetRegisterInfo::FirstVirtualRegister;
00122     return VRegInfo[RegNo].second;
00123   }
00124   
00125   MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
00126     if (RegNo < TargetRegisterInfo::FirstVirtualRegister)
00127       return PhysRegUseDefLists[RegNo];
00128     RegNo -= TargetRegisterInfo::FirstVirtualRegister;
00129     return VRegInfo[RegNo].second;
00130   }
00131 
00132   /// getVRegDef - Return the machine instr that defines the specified virtual
00133   /// register or null if none is found.  This assumes that the code is in SSA
00134   /// form, so there should only be one definition.
00135   MachineInstr *getVRegDef(unsigned Reg) const;
00136   
00137 #ifndef NDEBUG
00138   void dumpUses(unsigned RegNo) const;
00139 #endif
00140   
00141   //===--------------------------------------------------------------------===//
00142   // Virtual Register Info
00143   //===--------------------------------------------------------------------===//
00144   
00145   /// getRegClass - Return the register class of the specified virtual register.
00146   ///
00147   const TargetRegisterClass *getRegClass(unsigned Reg) const {
00148     Reg -= TargetRegisterInfo::FirstVirtualRegister;
00149     assert(Reg < VRegInfo.size() && "Invalid vreg!");
00150     return VRegInfo[Reg].first;
00151   }
00152 
00153   /// setRegClass - Set the register class of the specified virtual register.
00154   ///
00155   void setRegClass(unsigned Reg, const TargetRegisterClass *RC) {
00156     unsigned VR = Reg;
00157     Reg -= TargetRegisterInfo::FirstVirtualRegister;
00158     assert(Reg < VRegInfo.size() && "Invalid vreg!");
00159     const TargetRegisterClass *OldRC = VRegInfo[Reg].first;
00160     VRegInfo[Reg].first = RC;
00161 
00162     // Remove from old register class's vregs list. This may be slow but
00163     // fortunately this operation is rarely needed.
00164     std::vector<unsigned> &VRegs = RegClass2VRegMap[OldRC->getID()];
00165     std::vector<unsigned>::iterator I=std::find(VRegs.begin(), VRegs.end(), VR);
00166     VRegs.erase(I);
00167 
00168     // Add to new register class's vregs list.
00169     RegClass2VRegMap[RC->getID()].push_back(VR);
00170   }
00171   
00172   /// createVirtualRegister - Create and return a new virtual register in the
00173   /// function with the specified register class.
00174   ///
00175   unsigned createVirtualRegister(const TargetRegisterClass *RegClass);
00176 
00177   /// getLastVirtReg - Return the highest currently assigned virtual register.
00178   ///
00179   unsigned getLastVirtReg() const {
00180     return (unsigned)VRegInfo.size()+TargetRegisterInfo::FirstVirtualRegister-1;
00181   }
00182 
00183   /// getRegClassVirtRegs - Return the list of virtual registers of the given
00184   /// target register class.
00185   std::vector<unsigned> &getRegClassVirtRegs(const TargetRegisterClass *RC) {
00186     return RegClass2VRegMap[RC->getID()];
00187   }
00188   
00189   //===--------------------------------------------------------------------===//
00190   // Physical Register Use Info
00191   //===--------------------------------------------------------------------===//
00192   
00193   /// isPhysRegUsed - Return true if the specified register is used in this
00194   /// function.  This only works after register allocation.
00195   bool isPhysRegUsed(unsigned Reg) const { return UsedPhysRegs[Reg]; }
00196   
00197   /// setPhysRegUsed - Mark the specified register used in this function.
00198   /// This should only be called during and after register allocation.
00199   void setPhysRegUsed(unsigned Reg) { UsedPhysRegs[Reg] = true; }
00200   
00201   /// setPhysRegUnused - Mark the specified register unused in this function.
00202   /// This should only be called during and after register allocation.
00203   void setPhysRegUnused(unsigned Reg) { UsedPhysRegs[Reg] = false; }
00204   
00205 
00206   //===--------------------------------------------------------------------===//
00207   // LiveIn/LiveOut Management
00208   //===--------------------------------------------------------------------===//
00209   
00210   /// addLiveIn/Out - Add the specified register as a live in/out.  Note that it
00211   /// is an error to add the same register to the same set more than once.
00212   void addLiveIn(unsigned Reg, unsigned vreg = 0) {
00213     LiveIns.push_back(std::make_pair(Reg, vreg));
00214   }
00215   void addLiveOut(unsigned Reg) { LiveOuts.push_back(Reg); }
00216   
00217   // Iteration support for live in/out sets.  These sets are kept in sorted
00218   // order by their register number.
00219   typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
00220   livein_iterator;
00221   typedef std::vector<unsigned>::const_iterator liveout_iterator;
00222   livein_iterator livein_begin() const { return LiveIns.begin(); }
00223   livein_iterator livein_end()   const { return LiveIns.end(); }
00224   bool            livein_empty() const { return LiveIns.empty(); }
00225   liveout_iterator liveout_begin() const { return LiveOuts.begin(); }
00226   liveout_iterator liveout_end()   const { return LiveOuts.end(); }
00227   bool             liveout_empty() const { return LiveOuts.empty(); }
00228 
00229   bool isLiveIn(unsigned Reg) const {
00230     for (livein_iterator I = livein_begin(), E = livein_end(); I != E; ++I)
00231       if (I->first == Reg || I->second == Reg)
00232         return true;
00233     return false;
00234   }
00235 
00236 private:
00237   void HandleVRegListReallocation();
00238   
00239 public:
00240   /// defusechain_iterator - This class provides iterator support for machine
00241   /// operands in the function that use or define a specific register.  If
00242   /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
00243   /// returns defs.  If neither are true then you are silly and it always
00244   /// returns end().
00245   template<bool ReturnUses, bool ReturnDefs>
00246   class defusechain_iterator
00247     : public forward_iterator<MachineInstr, ptrdiff_t> {
00248     MachineOperand *Op;
00249     explicit defusechain_iterator(MachineOperand *op) : Op(op) {
00250       // If the first node isn't one we're interested in, advance to one that
00251       // we are interested in.
00252       if (op) {
00253         if ((!ReturnUses && op->isUse()) ||
00254             (!ReturnDefs && op->isDef()))
00255           ++*this;
00256       }
00257     }
00258     friend class MachineRegisterInfo;
00259   public:
00260     typedef forward_iterator<MachineInstr, ptrdiff_t>::reference reference;
00261     typedef forward_iterator<MachineInstr, ptrdiff_t>::pointer pointer;
00262     
00263     defusechain_iterator(const defusechain_iterator &I) : Op(I.Op) {}
00264     defusechain_iterator() : Op(0) {}
00265     
00266     bool operator==(const defusechain_iterator &x) const {
00267       return Op == x.Op;
00268     }
00269     bool operator!=(const defusechain_iterator &x) const {
00270       return !operator==(x);
00271     }
00272     
00273     /// atEnd - return true if this iterator is equal to reg_end() on the value.
00274     bool atEnd() const { return Op == 0; }
00275     
00276     // Iterator traversal: forward iteration only
00277     defusechain_iterator &operator++() {          // Preincrement
00278       assert(Op && "Cannot increment end iterator!");
00279       Op = Op->getNextOperandForReg();
00280       
00281       // If this is an operand we don't care about, skip it.
00282       while (Op && ((!ReturnUses && Op->isUse()) || 
00283                     (!ReturnDefs && Op->isDef())))
00284         Op = Op->getNextOperandForReg();
00285       
00286       return *this;
00287     }
00288     defusechain_iterator operator++(int) {        // Postincrement
00289       defusechain_iterator tmp = *this; ++*this; return tmp;
00290     }
00291     
00292     MachineOperand &getOperand() const {
00293       assert(Op && "Cannot dereference end iterator!");
00294       return *Op;
00295     }
00296     
00297     /// getOperandNo - Return the operand # of this MachineOperand in its
00298     /// MachineInstr.
00299     unsigned getOperandNo() const {
00300       assert(Op && "Cannot dereference end iterator!");
00301       return Op - &Op->getParent()->getOperand(0);
00302     }
00303     
00304     // Retrieve a reference to the current operand.
00305     MachineInstr &operator*() const {
00306       assert(Op && "Cannot dereference end iterator!");
00307       return *Op->getParent();
00308     }
00309     
00310     MachineInstr *operator->() const {
00311       assert(Op && "Cannot dereference end iterator!");
00312       return Op->getParent();
00313     }
00314   };
00315   
00316 };
00317 
00318 } // End llvm namespace
00319 
00320 #endif