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