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/ADT/BitVector.h"
00018 #include "llvm/ADT/IndexedMap.h"
00019 #include "llvm/ADT/iterator_range.h"
00020 #include "llvm/CodeGen/MachineFunction.h"
00021 #include "llvm/CodeGen/MachineInstrBundle.h"
00022 #include "llvm/Target/TargetRegisterInfo.h"
00023 #include "llvm/Target/TargetSubtargetInfo.h"
00024 #include <vector>
00025 
00026 namespace llvm {
00027 class PSetIterator;
00028 
00029 /// MachineRegisterInfo - Keep track of information for virtual and physical
00030 /// registers, including vreg register classes, use/def chains for registers,
00031 /// etc.
00032 class MachineRegisterInfo {
00033 public:
00034   class Delegate {
00035     virtual void anchor();
00036   public:
00037     virtual void MRI_NoteNewVirtualRegister(unsigned Reg) = 0;
00038 
00039     virtual ~Delegate() {}
00040   };
00041 
00042 private:
00043   const MachineFunction *MF;
00044   Delegate *TheDelegate;
00045 
00046   /// IsSSA - True when the machine function is in SSA form and virtual
00047   /// registers have a single def.
00048   bool IsSSA;
00049 
00050   /// TracksLiveness - True while register liveness is being tracked accurately.
00051   /// Basic block live-in lists, kill flags, and implicit defs may not be
00052   /// accurate when after this flag is cleared.
00053   bool TracksLiveness;
00054 
00055   /// VRegInfo - Information we keep for each virtual register.
00056   ///
00057   /// Each element in this list contains the register class of the vreg and the
00058   /// start of the use/def list for the register.
00059   IndexedMap<std::pair<const TargetRegisterClass*, MachineOperand*>,
00060              VirtReg2IndexFunctor> VRegInfo;
00061 
00062   /// RegAllocHints - This vector records register allocation hints for virtual
00063   /// registers. For each virtual register, it keeps a register and hint type
00064   /// pair making up the allocation hint. Hint type is target specific except
00065   /// for the value 0 which means the second value of the pair is the preferred
00066   /// register for allocation. For example, if the hint is <0, 1024>, it means
00067   /// the allocator should prefer the physical register allocated to the virtual
00068   /// register of the hint.
00069   IndexedMap<std::pair<unsigned, unsigned>, VirtReg2IndexFunctor> RegAllocHints;
00070 
00071   /// PhysRegUseDefLists - This is an array of the head of the use/def list for
00072   /// physical registers.
00073   std::vector<MachineOperand *> PhysRegUseDefLists;
00074 
00075   /// getRegUseDefListHead - Return the head pointer for the register use/def
00076   /// list for the specified virtual or physical register.
00077   MachineOperand *&getRegUseDefListHead(unsigned RegNo) {
00078     if (TargetRegisterInfo::isVirtualRegister(RegNo))
00079       return VRegInfo[RegNo].second;
00080     return PhysRegUseDefLists[RegNo];
00081   }
00082 
00083   MachineOperand *getRegUseDefListHead(unsigned RegNo) const {
00084     if (TargetRegisterInfo::isVirtualRegister(RegNo))
00085       return VRegInfo[RegNo].second;
00086     return PhysRegUseDefLists[RegNo];
00087   }
00088 
00089   /// Get the next element in the use-def chain.
00090   static MachineOperand *getNextOperandForReg(const MachineOperand *MO) {
00091     assert(MO && MO->isReg() && "This is not a register operand!");
00092     return MO->Contents.Reg.Next;
00093   }
00094 
00095   /// UsedRegUnits - This is a bit vector that is computed and set by the
00096   /// register allocator, and must be kept up to date by passes that run after
00097   /// register allocation (though most don't modify this).  This is used
00098   /// so that the code generator knows which callee save registers to save and
00099   /// for other target specific uses.
00100   /// This vector has bits set for register units that are modified in the
00101   /// current function. It doesn't include registers clobbered by function
00102   /// calls with register mask operands.
00103   BitVector UsedRegUnits;
00104 
00105   /// UsedPhysRegMask - Additional used physregs including aliases.
00106   /// This bit vector represents all the registers clobbered by function calls.
00107   /// It can model things that UsedRegUnits can't, such as function calls that
00108   /// clobber ymm7 but preserve the low half in xmm7.
00109   BitVector UsedPhysRegMask;
00110 
00111   /// ReservedRegs - This is a bit vector of reserved registers.  The target
00112   /// may change its mind about which registers should be reserved.  This
00113   /// vector is the frozen set of reserved registers when register allocation
00114   /// started.
00115   BitVector ReservedRegs;
00116 
00117   /// Keep track of the physical registers that are live in to the function.
00118   /// Live in values are typically arguments in registers.  LiveIn values are
00119   /// allowed to have virtual registers associated with them, stored in the
00120   /// second element.
00121   std::vector<std::pair<unsigned, unsigned> > LiveIns;
00122 
00123   MachineRegisterInfo(const MachineRegisterInfo&) LLVM_DELETED_FUNCTION;
00124   void operator=(const MachineRegisterInfo&) LLVM_DELETED_FUNCTION;
00125 public:
00126   explicit MachineRegisterInfo(const MachineFunction *MF);
00127 
00128   const TargetRegisterInfo *getTargetRegisterInfo() const {
00129     return MF->getSubtarget().getRegisterInfo();
00130   }
00131 
00132   void resetDelegate(Delegate *delegate) {
00133     // Ensure another delegate does not take over unless the current
00134     // delegate first unattaches itself. If we ever need to multicast
00135     // notifications, we will need to change to using a list.
00136     assert(TheDelegate == delegate &&
00137            "Only the current delegate can perform reset!");
00138     TheDelegate = nullptr;
00139   }
00140 
00141   void setDelegate(Delegate *delegate) {
00142     assert(delegate && !TheDelegate &&
00143            "Attempted to set delegate to null, or to change it without "
00144            "first resetting it!");
00145 
00146     TheDelegate = delegate;
00147   }
00148 
00149   //===--------------------------------------------------------------------===//
00150   // Function State
00151   //===--------------------------------------------------------------------===//
00152 
00153   // isSSA - Returns true when the machine function is in SSA form. Early
00154   // passes require the machine function to be in SSA form where every virtual
00155   // register has a single defining instruction.
00156   //
00157   // The TwoAddressInstructionPass and PHIElimination passes take the machine
00158   // function out of SSA form when they introduce multiple defs per virtual
00159   // register.
00160   bool isSSA() const { return IsSSA; }
00161 
00162   // leaveSSA - Indicates that the machine function is no longer in SSA form.
00163   void leaveSSA() { IsSSA = false; }
00164 
00165   /// tracksLiveness - Returns true when tracking register liveness accurately.
00166   ///
00167   /// While this flag is true, register liveness information in basic block
00168   /// live-in lists and machine instruction operands is accurate. This means it
00169   /// can be used to change the code in ways that affect the values in
00170   /// registers, for example by the register scavenger.
00171   ///
00172   /// When this flag is false, liveness is no longer reliable.
00173   bool tracksLiveness() const { return TracksLiveness; }
00174 
00175   /// invalidateLiveness - Indicates that register liveness is no longer being
00176   /// tracked accurately.
00177   ///
00178   /// This should be called by late passes that invalidate the liveness
00179   /// information.
00180   void invalidateLiveness() { TracksLiveness = false; }
00181 
00182   //===--------------------------------------------------------------------===//
00183   // Register Info
00184   //===--------------------------------------------------------------------===//
00185 
00186   // Strictly for use by MachineInstr.cpp.
00187   void addRegOperandToUseList(MachineOperand *MO);
00188 
00189   // Strictly for use by MachineInstr.cpp.
00190   void removeRegOperandFromUseList(MachineOperand *MO);
00191 
00192   // Strictly for use by MachineInstr.cpp.
00193   void moveOperands(MachineOperand *Dst, MachineOperand *Src, unsigned NumOps);
00194 
00195   /// Verify the sanity of the use list for Reg.
00196   void verifyUseList(unsigned Reg) const;
00197 
00198   /// Verify the use list of all registers.
00199   void verifyUseLists() const;
00200 
00201   /// reg_begin/reg_end - Provide iteration support to walk over all definitions
00202   /// and uses of a register within the MachineFunction that corresponds to this
00203   /// MachineRegisterInfo object.
00204   template<bool Uses, bool Defs, bool SkipDebug,
00205            bool ByOperand, bool ByInstr, bool ByBundle>
00206   class defusechain_iterator;
00207   template<bool Uses, bool Defs, bool SkipDebug,
00208            bool ByOperand, bool ByInstr, bool ByBundle>
00209   class defusechain_instr_iterator;
00210 
00211   // Make it a friend so it can access getNextOperandForReg().
00212   template<bool, bool, bool, bool, bool, bool>
00213     friend class defusechain_iterator;
00214   template<bool, bool, bool, bool, bool, bool>
00215     friend class defusechain_instr_iterator;
00216 
00217 
00218 
00219   /// reg_iterator/reg_begin/reg_end - Walk all defs and uses of the specified
00220   /// register.
00221   typedef defusechain_iterator<true,true,false,true,false,false>
00222           reg_iterator;
00223   reg_iterator reg_begin(unsigned RegNo) const {
00224     return reg_iterator(getRegUseDefListHead(RegNo));
00225   }
00226   static reg_iterator reg_end() { return reg_iterator(nullptr); }
00227 
00228   inline iterator_range<reg_iterator>  reg_operands(unsigned Reg) const {
00229     return iterator_range<reg_iterator>(reg_begin(Reg), reg_end());
00230   }
00231 
00232   /// reg_instr_iterator/reg_instr_begin/reg_instr_end - Walk all defs and uses
00233   /// of the specified register, stepping by MachineInstr.
00234   typedef defusechain_instr_iterator<true,true,false,false,true,false>
00235           reg_instr_iterator;
00236   reg_instr_iterator reg_instr_begin(unsigned RegNo) const {
00237     return reg_instr_iterator(getRegUseDefListHead(RegNo));
00238   }
00239   static reg_instr_iterator reg_instr_end() {
00240     return reg_instr_iterator(nullptr);
00241   }
00242 
00243   inline iterator_range<reg_instr_iterator>
00244   reg_instructions(unsigned Reg) const {
00245     return iterator_range<reg_instr_iterator>(reg_instr_begin(Reg),
00246                                               reg_instr_end());
00247   }
00248 
00249   /// reg_bundle_iterator/reg_bundle_begin/reg_bundle_end - Walk all defs and uses
00250   /// of the specified register, stepping by bundle.
00251   typedef defusechain_instr_iterator<true,true,false,false,false,true>
00252           reg_bundle_iterator;
00253   reg_bundle_iterator reg_bundle_begin(unsigned RegNo) const {
00254     return reg_bundle_iterator(getRegUseDefListHead(RegNo));
00255   }
00256   static reg_bundle_iterator reg_bundle_end() {
00257     return reg_bundle_iterator(nullptr);
00258   }
00259 
00260   inline iterator_range<reg_bundle_iterator> reg_bundles(unsigned Reg) const {
00261     return iterator_range<reg_bundle_iterator>(reg_bundle_begin(Reg),
00262                                                reg_bundle_end());
00263   }
00264 
00265   /// reg_empty - Return true if there are no instructions using or defining the
00266   /// specified register (it may be live-in).
00267   bool reg_empty(unsigned RegNo) const { return reg_begin(RegNo) == reg_end(); }
00268 
00269   /// reg_nodbg_iterator/reg_nodbg_begin/reg_nodbg_end - Walk all defs and uses
00270   /// of the specified register, skipping those marked as Debug.
00271   typedef defusechain_iterator<true,true,true,true,false,false>
00272           reg_nodbg_iterator;
00273   reg_nodbg_iterator reg_nodbg_begin(unsigned RegNo) const {
00274     return reg_nodbg_iterator(getRegUseDefListHead(RegNo));
00275   }
00276   static reg_nodbg_iterator reg_nodbg_end() {
00277     return reg_nodbg_iterator(nullptr);
00278   }
00279 
00280   inline iterator_range<reg_nodbg_iterator>
00281   reg_nodbg_operands(unsigned Reg) const {
00282     return iterator_range<reg_nodbg_iterator>(reg_nodbg_begin(Reg),
00283                                               reg_nodbg_end());
00284   }
00285 
00286   /// reg_instr_nodbg_iterator/reg_instr_nodbg_begin/reg_instr_nodbg_end - Walk
00287   /// all defs and uses of the specified register, stepping by MachineInstr,
00288   /// skipping those marked as Debug.
00289   typedef defusechain_instr_iterator<true,true,true,false,true,false>
00290           reg_instr_nodbg_iterator;
00291   reg_instr_nodbg_iterator reg_instr_nodbg_begin(unsigned RegNo) const {
00292     return reg_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
00293   }
00294   static reg_instr_nodbg_iterator reg_instr_nodbg_end() {
00295     return reg_instr_nodbg_iterator(nullptr);
00296   }
00297 
00298   inline iterator_range<reg_instr_nodbg_iterator>
00299   reg_nodbg_instructions(unsigned Reg) const {
00300     return iterator_range<reg_instr_nodbg_iterator>(reg_instr_nodbg_begin(Reg),
00301                                                     reg_instr_nodbg_end());
00302   }
00303 
00304   /// reg_bundle_nodbg_iterator/reg_bundle_nodbg_begin/reg_bundle_nodbg_end - Walk
00305   /// all defs and uses of the specified register, stepping by bundle,
00306   /// skipping those marked as Debug.
00307   typedef defusechain_instr_iterator<true,true,true,false,false,true>
00308           reg_bundle_nodbg_iterator;
00309   reg_bundle_nodbg_iterator reg_bundle_nodbg_begin(unsigned RegNo) const {
00310     return reg_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
00311   }
00312   static reg_bundle_nodbg_iterator reg_bundle_nodbg_end() {
00313     return reg_bundle_nodbg_iterator(nullptr);
00314   }
00315 
00316   inline iterator_range<reg_bundle_nodbg_iterator> 
00317   reg_nodbg_bundles(unsigned Reg) const {
00318     return iterator_range<reg_bundle_nodbg_iterator>(reg_bundle_nodbg_begin(Reg),
00319                                                      reg_bundle_nodbg_end());
00320   }
00321 
00322   /// reg_nodbg_empty - Return true if the only instructions using or defining
00323   /// Reg are Debug instructions.
00324   bool reg_nodbg_empty(unsigned RegNo) const {
00325     return reg_nodbg_begin(RegNo) == reg_nodbg_end();
00326   }
00327 
00328   /// def_iterator/def_begin/def_end - Walk all defs of the specified register.
00329   typedef defusechain_iterator<false,true,false,true,false,false>
00330           def_iterator;
00331   def_iterator def_begin(unsigned RegNo) const {
00332     return def_iterator(getRegUseDefListHead(RegNo));
00333   }
00334   static def_iterator def_end() { return def_iterator(nullptr); }
00335 
00336   inline iterator_range<def_iterator> def_operands(unsigned Reg) const {
00337     return iterator_range<def_iterator>(def_begin(Reg), def_end());
00338   }
00339 
00340   /// def_instr_iterator/def_instr_begin/def_instr_end - Walk all defs of the
00341   /// specified register, stepping by MachineInst.
00342   typedef defusechain_instr_iterator<false,true,false,false,true,false>
00343           def_instr_iterator;
00344   def_instr_iterator def_instr_begin(unsigned RegNo) const {
00345     return def_instr_iterator(getRegUseDefListHead(RegNo));
00346   }
00347   static def_instr_iterator def_instr_end() {
00348     return def_instr_iterator(nullptr);
00349   }
00350 
00351   inline iterator_range<def_instr_iterator>
00352   def_instructions(unsigned Reg) const {
00353     return iterator_range<def_instr_iterator>(def_instr_begin(Reg),
00354                                               def_instr_end());
00355   }
00356 
00357   /// def_bundle_iterator/def_bundle_begin/def_bundle_end - Walk all defs of the
00358   /// specified register, stepping by bundle.
00359   typedef defusechain_instr_iterator<false,true,false,false,false,true>
00360           def_bundle_iterator;
00361   def_bundle_iterator def_bundle_begin(unsigned RegNo) const {
00362     return def_bundle_iterator(getRegUseDefListHead(RegNo));
00363   }
00364   static def_bundle_iterator def_bundle_end() {
00365     return def_bundle_iterator(nullptr);
00366   }
00367 
00368   inline iterator_range<def_bundle_iterator> def_bundles(unsigned Reg) const {
00369     return iterator_range<def_bundle_iterator>(def_bundle_begin(Reg),
00370                                                def_bundle_end());
00371   }
00372 
00373   /// def_empty - Return true if there are no instructions defining the
00374   /// specified register (it may be live-in).
00375   bool def_empty(unsigned RegNo) const { return def_begin(RegNo) == def_end(); }
00376 
00377   /// hasOneDef - Return true if there is exactly one instruction defining the
00378   /// specified register.
00379   bool hasOneDef(unsigned RegNo) const {
00380     def_iterator DI = def_begin(RegNo);
00381     if (DI == def_end())
00382       return false;
00383     return ++DI == def_end();
00384   }
00385 
00386   /// use_iterator/use_begin/use_end - Walk all uses of the specified register.
00387   typedef defusechain_iterator<true,false,false,true,false,false>
00388           use_iterator;
00389   use_iterator use_begin(unsigned RegNo) const {
00390     return use_iterator(getRegUseDefListHead(RegNo));
00391   }
00392   static use_iterator use_end() { return use_iterator(nullptr); }
00393 
00394   inline iterator_range<use_iterator> use_operands(unsigned Reg) const {
00395     return iterator_range<use_iterator>(use_begin(Reg), use_end());
00396   }
00397 
00398   /// use_instr_iterator/use_instr_begin/use_instr_end - Walk all uses of the
00399   /// specified register, stepping by MachineInstr.
00400   typedef defusechain_instr_iterator<true,false,false,false,true,false>
00401           use_instr_iterator;
00402   use_instr_iterator use_instr_begin(unsigned RegNo) const {
00403     return use_instr_iterator(getRegUseDefListHead(RegNo));
00404   }
00405   static use_instr_iterator use_instr_end() {
00406     return use_instr_iterator(nullptr);
00407   }
00408 
00409   inline iterator_range<use_instr_iterator>
00410   use_instructions(unsigned Reg) const {
00411     return iterator_range<use_instr_iterator>(use_instr_begin(Reg),
00412                                               use_instr_end());
00413   }
00414 
00415   /// use_bundle_iterator/use_bundle_begin/use_bundle_end - Walk all uses of the
00416   /// specified register, stepping by bundle.
00417   typedef defusechain_instr_iterator<true,false,false,false,false,true>
00418           use_bundle_iterator;
00419   use_bundle_iterator use_bundle_begin(unsigned RegNo) const {
00420     return use_bundle_iterator(getRegUseDefListHead(RegNo));
00421   }
00422   static use_bundle_iterator use_bundle_end() {
00423     return use_bundle_iterator(nullptr);
00424   }
00425 
00426   inline iterator_range<use_bundle_iterator> use_bundles(unsigned Reg) const {
00427     return iterator_range<use_bundle_iterator>(use_bundle_begin(Reg),
00428                                                use_bundle_end());
00429   }
00430 
00431   /// use_empty - Return true if there are no instructions using the specified
00432   /// register.
00433   bool use_empty(unsigned RegNo) const { return use_begin(RegNo) == use_end(); }
00434 
00435   /// hasOneUse - Return true if there is exactly one instruction using the
00436   /// specified register.
00437   bool hasOneUse(unsigned RegNo) const {
00438     use_iterator UI = use_begin(RegNo);
00439     if (UI == use_end())
00440       return false;
00441     return ++UI == use_end();
00442   }
00443 
00444   /// use_nodbg_iterator/use_nodbg_begin/use_nodbg_end - Walk all uses of the
00445   /// specified register, skipping those marked as Debug.
00446   typedef defusechain_iterator<true,false,true,true,false,false>
00447           use_nodbg_iterator;
00448   use_nodbg_iterator use_nodbg_begin(unsigned RegNo) const {
00449     return use_nodbg_iterator(getRegUseDefListHead(RegNo));
00450   }
00451   static use_nodbg_iterator use_nodbg_end() {
00452     return use_nodbg_iterator(nullptr);
00453   }
00454 
00455   inline iterator_range<use_nodbg_iterator>
00456   use_nodbg_operands(unsigned Reg) const {
00457     return iterator_range<use_nodbg_iterator>(use_nodbg_begin(Reg),
00458                                               use_nodbg_end());
00459   }
00460 
00461   /// use_instr_nodbg_iterator/use_instr_nodbg_begin/use_instr_nodbg_end - Walk
00462   /// all uses of the specified register, stepping by MachineInstr, skipping
00463   /// those marked as Debug.
00464   typedef defusechain_instr_iterator<true,false,true,false,true,false>
00465           use_instr_nodbg_iterator;
00466   use_instr_nodbg_iterator use_instr_nodbg_begin(unsigned RegNo) const {
00467     return use_instr_nodbg_iterator(getRegUseDefListHead(RegNo));
00468   }
00469   static use_instr_nodbg_iterator use_instr_nodbg_end() {
00470     return use_instr_nodbg_iterator(nullptr);
00471   }
00472 
00473   inline iterator_range<use_instr_nodbg_iterator>
00474   use_nodbg_instructions(unsigned Reg) const {
00475     return iterator_range<use_instr_nodbg_iterator>(use_instr_nodbg_begin(Reg),
00476                                                     use_instr_nodbg_end());
00477   }
00478 
00479   /// use_bundle_nodbg_iterator/use_bundle_nodbg_begin/use_bundle_nodbg_end - Walk
00480   /// all uses of the specified register, stepping by bundle, skipping
00481   /// those marked as Debug.
00482   typedef defusechain_instr_iterator<true,false,true,false,false,true>
00483           use_bundle_nodbg_iterator;
00484   use_bundle_nodbg_iterator use_bundle_nodbg_begin(unsigned RegNo) const {
00485     return use_bundle_nodbg_iterator(getRegUseDefListHead(RegNo));
00486   }
00487   static use_bundle_nodbg_iterator use_bundle_nodbg_end() {
00488     return use_bundle_nodbg_iterator(nullptr);
00489   }
00490 
00491   inline iterator_range<use_bundle_nodbg_iterator>
00492   use_nodbg_bundles(unsigned Reg) const {
00493     return iterator_range<use_bundle_nodbg_iterator>(use_bundle_nodbg_begin(Reg),
00494                                                      use_bundle_nodbg_end());
00495   }
00496 
00497   /// use_nodbg_empty - Return true if there are no non-Debug instructions
00498   /// using the specified register.
00499   bool use_nodbg_empty(unsigned RegNo) const {
00500     return use_nodbg_begin(RegNo) == use_nodbg_end();
00501   }
00502 
00503   /// hasOneNonDBGUse - Return true if there is exactly one non-Debug
00504   /// instruction using the specified register.
00505   bool hasOneNonDBGUse(unsigned RegNo) const;
00506 
00507   /// replaceRegWith - Replace all instances of FromReg with ToReg in the
00508   /// machine function.  This is like llvm-level X->replaceAllUsesWith(Y),
00509   /// except that it also changes any definitions of the register as well.
00510   ///
00511   /// Note that it is usually necessary to first constrain ToReg's register
00512   /// class to match the FromReg constraints using:
00513   ///
00514   ///   constrainRegClass(ToReg, getRegClass(FromReg))
00515   ///
00516   /// That function will return NULL if the virtual registers have incompatible
00517   /// constraints.
00518   ///
00519   /// Note that if ToReg is a physical register the function will replace and
00520   /// apply sub registers to ToReg in order to obtain a final/proper physical
00521   /// register.
00522   void replaceRegWith(unsigned FromReg, unsigned ToReg);
00523   
00524   /// getVRegDef - Return the machine instr that defines the specified virtual
00525   /// register or null if none is found.  This assumes that the code is in SSA
00526   /// form, so there should only be one definition.
00527   MachineInstr *getVRegDef(unsigned Reg) const;
00528 
00529   /// getUniqueVRegDef - Return the unique machine instr that defines the
00530   /// specified virtual register or null if none is found.  If there are
00531   /// multiple definitions or no definition, return null.
00532   MachineInstr *getUniqueVRegDef(unsigned Reg) const;
00533 
00534   /// clearKillFlags - Iterate over all the uses of the given register and
00535   /// clear the kill flag from the MachineOperand. This function is used by
00536   /// optimization passes which extend register lifetimes and need only
00537   /// preserve conservative kill flag information.
00538   void clearKillFlags(unsigned Reg) const;
00539 
00540 #ifndef NDEBUG
00541   void dumpUses(unsigned RegNo) const;
00542 #endif
00543 
00544   /// isConstantPhysReg - Returns true if PhysReg is unallocatable and constant
00545   /// throughout the function.  It is safe to move instructions that read such
00546   /// a physreg.
00547   bool isConstantPhysReg(unsigned PhysReg, const MachineFunction &MF) const;
00548 
00549   /// Get an iterator over the pressure sets affected by the given physical or
00550   /// virtual register. If RegUnit is physical, it must be a register unit (from
00551   /// MCRegUnitIterator).
00552   PSetIterator getPressureSets(unsigned RegUnit) const;
00553 
00554   //===--------------------------------------------------------------------===//
00555   // Virtual Register Info
00556   //===--------------------------------------------------------------------===//
00557 
00558   /// getRegClass - Return the register class of the specified virtual register.
00559   ///
00560   const TargetRegisterClass *getRegClass(unsigned Reg) const {
00561     return VRegInfo[Reg].first;
00562   }
00563 
00564   /// setRegClass - Set the register class of the specified virtual register.
00565   ///
00566   void setRegClass(unsigned Reg, const TargetRegisterClass *RC);
00567 
00568   /// constrainRegClass - Constrain the register class of the specified virtual
00569   /// register to be a common subclass of RC and the current register class,
00570   /// but only if the new class has at least MinNumRegs registers.  Return the
00571   /// new register class, or NULL if no such class exists.
00572   /// This should only be used when the constraint is known to be trivial, like
00573   /// GR32 -> GR32_NOSP. Beware of increasing register pressure.
00574   ///
00575   const TargetRegisterClass *constrainRegClass(unsigned Reg,
00576                                                const TargetRegisterClass *RC,
00577                                                unsigned MinNumRegs = 0);
00578 
00579   /// recomputeRegClass - Try to find a legal super-class of Reg's register
00580   /// class that still satisfies the constraints from the instructions using
00581   /// Reg.  Returns true if Reg was upgraded.
00582   ///
00583   /// This method can be used after constraints have been removed from a
00584   /// virtual register, for example after removing instructions or splitting
00585   /// the live range.
00586   ///
00587   bool recomputeRegClass(unsigned Reg, const TargetMachine&);
00588 
00589   /// createVirtualRegister - Create and return a new virtual register in the
00590   /// function with the specified register class.
00591   ///
00592   unsigned createVirtualRegister(const TargetRegisterClass *RegClass);
00593 
00594   /// getNumVirtRegs - Return the number of virtual registers created.
00595   ///
00596   unsigned getNumVirtRegs() const { return VRegInfo.size(); }
00597 
00598   /// clearVirtRegs - Remove all virtual registers (after physreg assignment).
00599   void clearVirtRegs();
00600 
00601   /// setRegAllocationHint - Specify a register allocation hint for the
00602   /// specified virtual register.
00603   void setRegAllocationHint(unsigned Reg, unsigned Type, unsigned PrefReg) {
00604     RegAllocHints[Reg].first  = Type;
00605     RegAllocHints[Reg].second = PrefReg;
00606   }
00607 
00608   /// getRegAllocationHint - Return the register allocation hint for the
00609   /// specified virtual register.
00610   std::pair<unsigned, unsigned>
00611   getRegAllocationHint(unsigned Reg) const {
00612     return RegAllocHints[Reg];
00613   }
00614 
00615   /// getSimpleHint - Return the preferred register allocation hint, or 0 if a
00616   /// standard simple hint (Type == 0) is not set.
00617   unsigned getSimpleHint(unsigned Reg) const {
00618     std::pair<unsigned, unsigned> Hint = getRegAllocationHint(Reg);
00619     return Hint.first ? 0 : Hint.second;
00620   }
00621 
00622   /// markUsesInDebugValueAsUndef - Mark every DBG_VALUE referencing the
00623   /// specified register as undefined which causes the DBG_VALUE to be
00624   /// deleted during LiveDebugVariables analysis.
00625   void markUsesInDebugValueAsUndef(unsigned Reg) const;
00626 
00627   //===--------------------------------------------------------------------===//
00628   // Physical Register Use Info
00629   //===--------------------------------------------------------------------===//
00630 
00631   /// isPhysRegUsed - Return true if the specified register is used in this
00632   /// function. Also check for clobbered aliases and registers clobbered by
00633   /// function calls with register mask operands.
00634   ///
00635   /// This only works after register allocation. It is primarily used by
00636   /// PrologEpilogInserter to determine which callee-saved registers need
00637   /// spilling.
00638   bool isPhysRegUsed(unsigned Reg) const {
00639     if (UsedPhysRegMask.test(Reg))
00640       return true;
00641     for (MCRegUnitIterator Units(Reg, getTargetRegisterInfo());
00642          Units.isValid(); ++Units)
00643       if (UsedRegUnits.test(*Units))
00644         return true;
00645     return false;
00646   }
00647 
00648   /// Mark the specified register unit as used in this function.
00649   /// This should only be called during and after register allocation.
00650   void setRegUnitUsed(unsigned RegUnit) {
00651     UsedRegUnits.set(RegUnit);
00652   }
00653 
00654   /// setPhysRegUsed - Mark the specified register used in this function.
00655   /// This should only be called during and after register allocation.
00656   void setPhysRegUsed(unsigned Reg) {
00657     for (MCRegUnitIterator Units(Reg, getTargetRegisterInfo());
00658          Units.isValid(); ++Units)
00659       UsedRegUnits.set(*Units);
00660   }
00661 
00662   /// addPhysRegsUsedFromRegMask - Mark any registers not in RegMask as used.
00663   /// This corresponds to the bit mask attached to register mask operands.
00664   void addPhysRegsUsedFromRegMask(const uint32_t *RegMask) {
00665     UsedPhysRegMask.setBitsNotInMask(RegMask);
00666   }
00667 
00668   /// setPhysRegUnused - Mark the specified register unused in this function.
00669   /// This should only be called during and after register allocation.
00670   void setPhysRegUnused(unsigned Reg) {
00671     UsedPhysRegMask.reset(Reg);
00672     for (MCRegUnitIterator Units(Reg, getTargetRegisterInfo());
00673          Units.isValid(); ++Units)
00674       UsedRegUnits.reset(*Units);
00675   }
00676 
00677 
00678   //===--------------------------------------------------------------------===//
00679   // Reserved Register Info
00680   //===--------------------------------------------------------------------===//
00681   //
00682   // The set of reserved registers must be invariant during register
00683   // allocation.  For example, the target cannot suddenly decide it needs a
00684   // frame pointer when the register allocator has already used the frame
00685   // pointer register for something else.
00686   //
00687   // These methods can be used by target hooks like hasFP() to avoid changing
00688   // the reserved register set during register allocation.
00689 
00690   /// freezeReservedRegs - Called by the register allocator to freeze the set
00691   /// of reserved registers before allocation begins.
00692   void freezeReservedRegs(const MachineFunction&);
00693 
00694   /// reservedRegsFrozen - Returns true after freezeReservedRegs() was called
00695   /// to ensure the set of reserved registers stays constant.
00696   bool reservedRegsFrozen() const {
00697     return !ReservedRegs.empty();
00698   }
00699 
00700   /// canReserveReg - Returns true if PhysReg can be used as a reserved
00701   /// register.  Any register can be reserved before freezeReservedRegs() is
00702   /// called.
00703   bool canReserveReg(unsigned PhysReg) const {
00704     return !reservedRegsFrozen() || ReservedRegs.test(PhysReg);
00705   }
00706 
00707   /// getReservedRegs - Returns a reference to the frozen set of reserved
00708   /// registers. This method should always be preferred to calling
00709   /// TRI::getReservedRegs() when possible.
00710   const BitVector &getReservedRegs() const {
00711     assert(reservedRegsFrozen() &&
00712            "Reserved registers haven't been frozen yet. "
00713            "Use TRI::getReservedRegs().");
00714     return ReservedRegs;
00715   }
00716 
00717   /// isReserved - Returns true when PhysReg is a reserved register.
00718   ///
00719   /// Reserved registers may belong to an allocatable register class, but the
00720   /// target has explicitly requested that they are not used.
00721   ///
00722   bool isReserved(unsigned PhysReg) const {
00723     return getReservedRegs().test(PhysReg);
00724   }
00725 
00726   /// isAllocatable - Returns true when PhysReg belongs to an allocatable
00727   /// register class and it hasn't been reserved.
00728   ///
00729   /// Allocatable registers may show up in the allocation order of some virtual
00730   /// register, so a register allocator needs to track its liveness and
00731   /// availability.
00732   bool isAllocatable(unsigned PhysReg) const {
00733     return getTargetRegisterInfo()->isInAllocatableClass(PhysReg) &&
00734       !isReserved(PhysReg);
00735   }
00736 
00737   //===--------------------------------------------------------------------===//
00738   // LiveIn Management
00739   //===--------------------------------------------------------------------===//
00740 
00741   /// addLiveIn - Add the specified register as a live-in.  Note that it
00742   /// is an error to add the same register to the same set more than once.
00743   void addLiveIn(unsigned Reg, unsigned vreg = 0) {
00744     LiveIns.push_back(std::make_pair(Reg, vreg));
00745   }
00746 
00747   // Iteration support for the live-ins set.  It's kept in sorted order
00748   // by register number.
00749   typedef std::vector<std::pair<unsigned,unsigned> >::const_iterator
00750   livein_iterator;
00751   livein_iterator livein_begin() const { return LiveIns.begin(); }
00752   livein_iterator livein_end()   const { return LiveIns.end(); }
00753   bool            livein_empty() const { return LiveIns.empty(); }
00754 
00755   bool isLiveIn(unsigned Reg) const;
00756 
00757   /// getLiveInPhysReg - If VReg is a live-in virtual register, return the
00758   /// corresponding live-in physical register.
00759   unsigned getLiveInPhysReg(unsigned VReg) const;
00760 
00761   /// getLiveInVirtReg - If PReg is a live-in physical register, return the
00762   /// corresponding live-in physical register.
00763   unsigned getLiveInVirtReg(unsigned PReg) const;
00764 
00765   /// EmitLiveInCopies - Emit copies to initialize livein virtual registers
00766   /// into the given entry block.
00767   void EmitLiveInCopies(MachineBasicBlock *EntryMBB,
00768                         const TargetRegisterInfo &TRI,
00769                         const TargetInstrInfo &TII);
00770 
00771   /// defusechain_iterator - This class provides iterator support for machine
00772   /// operands in the function that use or define a specific register.  If
00773   /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
00774   /// returns defs.  If neither are true then you are silly and it always
00775   /// returns end().  If SkipDebug is true it skips uses marked Debug
00776   /// when incrementing.
00777   template<bool ReturnUses, bool ReturnDefs, bool SkipDebug,
00778            bool ByOperand, bool ByInstr, bool ByBundle>
00779   class defusechain_iterator
00780     : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
00781     MachineOperand *Op;
00782     explicit defusechain_iterator(MachineOperand *op) : Op(op) {
00783       // If the first node isn't one we're interested in, advance to one that
00784       // we are interested in.
00785       if (op) {
00786         if ((!ReturnUses && op->isUse()) ||
00787             (!ReturnDefs && op->isDef()) ||
00788             (SkipDebug && op->isDebug()))
00789           advance();
00790       }
00791     }
00792     friend class MachineRegisterInfo;
00793 
00794     void advance() {
00795       assert(Op && "Cannot increment end iterator!");
00796       Op = getNextOperandForReg(Op);
00797 
00798       // All defs come before the uses, so stop def_iterator early.
00799       if (!ReturnUses) {
00800         if (Op) {
00801           if (Op->isUse())
00802             Op = nullptr;
00803           else
00804             assert(!Op->isDebug() && "Can't have debug defs");
00805         }
00806       } else {
00807         // If this is an operand we don't care about, skip it.
00808         while (Op && ((!ReturnDefs && Op->isDef()) ||
00809                       (SkipDebug && Op->isDebug())))
00810           Op = getNextOperandForReg(Op);
00811       }
00812     }
00813   public:
00814     typedef std::iterator<std::forward_iterator_tag,
00815                           MachineInstr, ptrdiff_t>::reference reference;
00816     typedef std::iterator<std::forward_iterator_tag,
00817                           MachineInstr, ptrdiff_t>::pointer pointer;
00818 
00819     defusechain_iterator(const defusechain_iterator &I) : Op(I.Op) {}
00820     defusechain_iterator() : Op(nullptr) {}
00821 
00822     bool operator==(const defusechain_iterator &x) const {
00823       return Op == x.Op;
00824     }
00825     bool operator!=(const defusechain_iterator &x) const {
00826       return !operator==(x);
00827     }
00828 
00829     /// atEnd - return true if this iterator is equal to reg_end() on the value.
00830     bool atEnd() const { return Op == nullptr; }
00831 
00832     // Iterator traversal: forward iteration only
00833     defusechain_iterator &operator++() {          // Preincrement
00834       assert(Op && "Cannot increment end iterator!");
00835       if (ByOperand)
00836         advance();
00837       else if (ByInstr) {
00838         MachineInstr *P = Op->getParent();
00839         do {
00840           advance();
00841         } while (Op && Op->getParent() == P);
00842       } else if (ByBundle) {
00843         MachineInstr *P = getBundleStart(Op->getParent());
00844         do {
00845           advance();
00846         } while (Op && getBundleStart(Op->getParent()) == P);
00847       }
00848 
00849       return *this;
00850     }
00851     defusechain_iterator operator++(int) {        // Postincrement
00852       defusechain_iterator tmp = *this; ++*this; return tmp;
00853     }
00854 
00855     /// getOperandNo - Return the operand # of this MachineOperand in its
00856     /// MachineInstr.
00857     unsigned getOperandNo() const {
00858       assert(Op && "Cannot dereference end iterator!");
00859       return Op - &Op->getParent()->getOperand(0);
00860     }
00861 
00862     // Retrieve a reference to the current operand.
00863     MachineOperand &operator*() const {
00864       assert(Op && "Cannot dereference end iterator!");
00865       return *Op;
00866     }
00867 
00868     MachineOperand *operator->() const {
00869       assert(Op && "Cannot dereference end iterator!");
00870       return Op;
00871     }
00872   };
00873 
00874   /// defusechain_iterator - This class provides iterator support for machine
00875   /// operands in the function that use or define a specific register.  If
00876   /// ReturnUses is true it returns uses of registers, if ReturnDefs is true it
00877   /// returns defs.  If neither are true then you are silly and it always
00878   /// returns end().  If SkipDebug is true it skips uses marked Debug
00879   /// when incrementing.
00880   template<bool ReturnUses, bool ReturnDefs, bool SkipDebug,
00881            bool ByOperand, bool ByInstr, bool ByBundle>
00882   class defusechain_instr_iterator
00883     : public std::iterator<std::forward_iterator_tag, MachineInstr, ptrdiff_t> {
00884     MachineOperand *Op;
00885     explicit defusechain_instr_iterator(MachineOperand *op) : Op(op) {
00886       // If the first node isn't one we're interested in, advance to one that
00887       // we are interested in.
00888       if (op) {
00889         if ((!ReturnUses && op->isUse()) ||
00890             (!ReturnDefs && op->isDef()) ||
00891             (SkipDebug && op->isDebug()))
00892           advance();
00893       }
00894     }
00895     friend class MachineRegisterInfo;
00896 
00897     void advance() {
00898       assert(Op && "Cannot increment end iterator!");
00899       Op = getNextOperandForReg(Op);
00900 
00901       // All defs come before the uses, so stop def_iterator early.
00902       if (!ReturnUses) {
00903         if (Op) {
00904           if (Op->isUse())
00905             Op = nullptr;
00906           else
00907             assert(!Op->isDebug() && "Can't have debug defs");
00908         }
00909       } else {
00910         // If this is an operand we don't care about, skip it.
00911         while (Op && ((!ReturnDefs && Op->isDef()) ||
00912                       (SkipDebug && Op->isDebug())))
00913           Op = getNextOperandForReg(Op);
00914       }
00915     }
00916   public:
00917     typedef std::iterator<std::forward_iterator_tag,
00918                           MachineInstr, ptrdiff_t>::reference reference;
00919     typedef std::iterator<std::forward_iterator_tag,
00920                           MachineInstr, ptrdiff_t>::pointer pointer;
00921 
00922     defusechain_instr_iterator(const defusechain_instr_iterator &I) : Op(I.Op){}
00923     defusechain_instr_iterator() : Op(nullptr) {}
00924 
00925     bool operator==(const defusechain_instr_iterator &x) const {
00926       return Op == x.Op;
00927     }
00928     bool operator!=(const defusechain_instr_iterator &x) const {
00929       return !operator==(x);
00930     }
00931 
00932     /// atEnd - return true if this iterator is equal to reg_end() on the value.
00933     bool atEnd() const { return Op == nullptr; }
00934 
00935     // Iterator traversal: forward iteration only
00936     defusechain_instr_iterator &operator++() {          // Preincrement
00937       assert(Op && "Cannot increment end iterator!");
00938       if (ByOperand)
00939         advance();
00940       else if (ByInstr) {
00941         MachineInstr *P = Op->getParent();
00942         do {
00943           advance();
00944         } while (Op && Op->getParent() == P);
00945       } else if (ByBundle) {
00946         MachineInstr *P = getBundleStart(Op->getParent());
00947         do {
00948           advance();
00949         } while (Op && getBundleStart(Op->getParent()) == P);
00950       }
00951 
00952       return *this;
00953     }
00954     defusechain_instr_iterator operator++(int) {        // Postincrement
00955       defusechain_instr_iterator tmp = *this; ++*this; return tmp;
00956     }
00957 
00958     // Retrieve a reference to the current operand.
00959     MachineInstr &operator*() const {
00960       assert(Op && "Cannot dereference end iterator!");
00961       if (ByBundle) return *(getBundleStart(Op->getParent()));
00962       return *Op->getParent();
00963     }
00964 
00965     MachineInstr *operator->() const {
00966       assert(Op && "Cannot dereference end iterator!");
00967       if (ByBundle) return getBundleStart(Op->getParent());
00968       return Op->getParent();
00969     }
00970   };
00971 };
00972 
00973 /// Iterate over the pressure sets affected by the given physical or virtual
00974 /// register. If Reg is physical, it must be a register unit (from
00975 /// MCRegUnitIterator).
00976 class PSetIterator {
00977   const int *PSet;
00978   unsigned Weight;
00979 public:
00980   PSetIterator(): PSet(nullptr), Weight(0) {}
00981   PSetIterator(unsigned RegUnit, const MachineRegisterInfo *MRI) {
00982     const TargetRegisterInfo *TRI = MRI->getTargetRegisterInfo();
00983     if (TargetRegisterInfo::isVirtualRegister(RegUnit)) {
00984       const TargetRegisterClass *RC = MRI->getRegClass(RegUnit);
00985       PSet = TRI->getRegClassPressureSets(RC);
00986       Weight = TRI->getRegClassWeight(RC).RegWeight;
00987     }
00988     else {
00989       PSet = TRI->getRegUnitPressureSets(RegUnit);
00990       Weight = TRI->getRegUnitWeight(RegUnit);
00991     }
00992     if (*PSet == -1)
00993       PSet = nullptr;
00994   }
00995   bool isValid() const { return PSet; }
00996 
00997   unsigned getWeight() const { return Weight; }
00998 
00999   unsigned operator*() const { return *PSet; }
01000 
01001   void operator++() {
01002     assert(isValid() && "Invalid PSetIterator.");
01003     ++PSet;
01004     if (*PSet == -1)
01005       PSet = nullptr;
01006   }
01007 };
01008 
01009 inline PSetIterator MachineRegisterInfo::
01010 getPressureSets(unsigned RegUnit) const {
01011   return PSetIterator(RegUnit, this);
01012 }
01013 
01014 } // End llvm namespace
01015 
01016 #endif