LLVM API Documentation
00001 //===-- llvm/CodeGen/LiveVariables.h - Live Variable Analysis ---*- 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 LiveVariables analysis pass. For each machine 00011 // instruction in the function, this pass calculates the set of registers that 00012 // are immediately dead after the instruction (i.e., the instruction calculates 00013 // the value, but it is never used) and the set of registers that are used by 00014 // the instruction, but are never used after the instruction (i.e., they are 00015 // killed). 00016 // 00017 // This class computes live variables using a sparse implementation based on 00018 // the machine code SSA form. This class computes live variable information for 00019 // each virtual and _register allocatable_ physical register in a function. It 00020 // uses the dominance properties of SSA form to efficiently compute live 00021 // variables for virtual registers, and assumes that physical registers are only 00022 // live within a single basic block (allowing it to do a single local analysis 00023 // to resolve physical register lifetimes in each basic block). If a physical 00024 // register is not register allocatable, it is not tracked. This is useful for 00025 // things like the stack pointer and condition codes. 00026 // 00027 //===----------------------------------------------------------------------===// 00028 00029 #ifndef LLVM_CODEGEN_LIVEVARIABLES_H 00030 #define LLVM_CODEGEN_LIVEVARIABLES_H 00031 00032 #include "llvm/CodeGen/MachineFunctionPass.h" 00033 #include "llvm/ADT/BitVector.h" 00034 #include "llvm/ADT/DenseMap.h" 00035 #include "llvm/ADT/SmallVector.h" 00036 00037 namespace llvm { 00038 00039 class MachineRegisterInfo; 00040 class TargetRegisterInfo; 00041 00042 class LiveVariables : public MachineFunctionPass { 00043 public: 00044 static char ID; // Pass identification, replacement for typeid 00045 LiveVariables() : MachineFunctionPass(&ID) {} 00046 00047 /// VarInfo - This represents the regions where a virtual register is live in 00048 /// the program. We represent this with three different pieces of 00049 /// information: the set of blocks in which the instruction is live 00050 /// throughout, the set of blocks in which the instruction is actually used, 00051 /// and the set of non-phi instructions that are the last users of the value. 00052 /// 00053 /// In the common case where a value is defined and killed in the same block, 00054 /// There is one killing instruction, and AliveBlocks is empty. 00055 /// 00056 /// Otherwise, the value is live out of the block. If the value is live 00057 /// throughout any blocks, these blocks are listed in AliveBlocks. Blocks 00058 /// where the liveness range ends are not included in AliveBlocks, instead 00059 /// being captured by the Kills set. In these blocks, the value is live into 00060 /// the block (unless the value is defined and killed in the same block) and 00061 /// lives until the specified instruction. Note that there cannot ever be a 00062 /// value whose Kills set contains two instructions from the same basic block. 00063 /// 00064 /// PHI nodes complicate things a bit. If a PHI node is the last user of a 00065 /// value in one of its predecessor blocks, it is not listed in the kills set, 00066 /// but does include the predecessor block in the AliveBlocks set (unless that 00067 /// block also defines the value). This leads to the (perfectly sensical) 00068 /// situation where a value is defined in a block, and the last use is a phi 00069 /// node in the successor. In this case, AliveBlocks is empty (the value is 00070 /// not live across any blocks) and Kills is empty (phi nodes are not 00071 /// included). This is sensical because the value must be live to the end of 00072 /// the block, but is not live in any successor blocks. 00073 struct VarInfo { 00074 /// AliveBlocks - Set of blocks in which this value is alive completely 00075 /// through. This is a bit set which uses the basic block number as an 00076 /// index. 00077 /// 00078 BitVector AliveBlocks; 00079 00080 /// UsedBlocks - Set of blocks in which this value is actually used. This 00081 /// is a bit set which uses the basic block number as an index. 00082 BitVector UsedBlocks; 00083 00084 /// NumUses - Number of uses of this register across the entire function. 00085 /// 00086 unsigned NumUses; 00087 00088 /// Kills - List of MachineInstruction's which are the last use of this 00089 /// virtual register (kill it) in their basic block. 00090 /// 00091 std::vector<MachineInstr*> Kills; 00092 00093 VarInfo() : NumUses(0) {} 00094 00095 /// removeKill - Delete a kill corresponding to the specified 00096 /// machine instruction. Returns true if there was a kill 00097 /// corresponding to this instruction, false otherwise. 00098 bool removeKill(MachineInstr *MI) { 00099 std::vector<MachineInstr*>::iterator 00100 I = std::find(Kills.begin(), Kills.end(), MI); 00101 if (I == Kills.end()) 00102 return false; 00103 Kills.erase(I); 00104 return true; 00105 } 00106 00107 void dump() const; 00108 }; 00109 00110 private: 00111 /// VirtRegInfo - This list is a mapping from virtual register number to 00112 /// variable information. FirstVirtualRegister is subtracted from the virtual 00113 /// register number before indexing into this list. 00114 /// 00115 std::vector<VarInfo> VirtRegInfo; 00116 00117 /// ReservedRegisters - This vector keeps track of which registers 00118 /// are reserved register which are not allocatable by the target machine. 00119 /// We can not track liveness for values that are in this set. 00120 /// 00121 BitVector ReservedRegisters; 00122 00123 private: // Intermediate data structures 00124 MachineFunction *MF; 00125 00126 MachineRegisterInfo* MRI; 00127 00128 const TargetRegisterInfo *TRI; 00129 00130 // PhysRegInfo - Keep track of which instruction was the last def of a 00131 // physical register. This is a purely local property, because all physical 00132 // register references are presumed dead across basic blocks. 00133 MachineInstr **PhysRegDef; 00134 00135 // PhysRegInfo - Keep track of which instruction was the last use of a 00136 // physical register. This is a purely local property, because all physical 00137 // register references are presumed dead across basic blocks. 00138 MachineInstr **PhysRegUse; 00139 00140 SmallVector<unsigned, 4> *PHIVarInfo; 00141 00142 // DistanceMap - Keep track the distance of a MI from the start of the 00143 // current basic block. 00144 DenseMap<MachineInstr*, unsigned> DistanceMap; 00145 00146 /// HandlePhysRegKill - Add kills of Reg and its sub-registers to the 00147 /// uses. Pay special attention to the sub-register uses which may come below 00148 /// the last use of the whole register. 00149 bool HandlePhysRegKill(unsigned Reg, MachineInstr *MI); 00150 00151 void HandlePhysRegUse(unsigned Reg, MachineInstr *MI); 00152 void HandlePhysRegDef(unsigned Reg, MachineInstr *MI); 00153 00154 /// FindLastPartialDef - Return the last partial def of the specified register. 00155 /// Also returns the sub-register that's defined. 00156 MachineInstr *FindLastPartialDef(unsigned Reg, unsigned &PartDefReg); 00157 00158 /// hasRegisterUseBelow - Return true if the specified register is used after 00159 /// the current instruction and before it's next definition. 00160 bool hasRegisterUseBelow(unsigned Reg, MachineBasicBlock::iterator I, 00161 MachineBasicBlock *MBB); 00162 00163 /// analyzePHINodes - Gather information about the PHI nodes in here. In 00164 /// particular, we want to map the variable information of a virtual 00165 /// register which is used in a PHI node. We map that to the BB the vreg 00166 /// is coming from. 00167 void analyzePHINodes(const MachineFunction& Fn); 00168 public: 00169 00170 virtual bool runOnMachineFunction(MachineFunction &MF); 00171 00172 /// RegisterDefIsDead - Return true if the specified instruction defines the 00173 /// specified register, but that definition is dead. 00174 bool RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const; 00175 00176 //===--------------------------------------------------------------------===// 00177 // API to update live variable information 00178 00179 /// replaceKillInstruction - Update register kill info by replacing a kill 00180 /// instruction with a new one. 00181 void replaceKillInstruction(unsigned Reg, MachineInstr *OldMI, 00182 MachineInstr *NewMI); 00183 00184 /// addVirtualRegisterKilled - Add information about the fact that the 00185 /// specified register is killed after being used by the specified 00186 /// instruction. If AddIfNotFound is true, add a implicit operand if it's 00187 /// not found. 00188 void addVirtualRegisterKilled(unsigned IncomingReg, MachineInstr *MI, 00189 bool AddIfNotFound = false) { 00190 if (MI->addRegisterKilled(IncomingReg, TRI, AddIfNotFound)) 00191 getVarInfo(IncomingReg).Kills.push_back(MI); 00192 } 00193 00194 /// removeVirtualRegisterKilled - Remove the specified kill of the virtual 00195 /// register from the live variable information. Returns true if the 00196 /// variable was marked as killed by the specified instruction, 00197 /// false otherwise. 00198 bool removeVirtualRegisterKilled(unsigned reg, MachineInstr *MI) { 00199 if (!getVarInfo(reg).removeKill(MI)) 00200 return false; 00201 00202 bool Removed = false; 00203 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00204 MachineOperand &MO = MI->getOperand(i); 00205 if (MO.isReg() && MO.isKill() && MO.getReg() == reg) { 00206 MO.setIsKill(false); 00207 Removed = true; 00208 break; 00209 } 00210 } 00211 00212 assert(Removed && "Register is not used by this instruction!"); 00213 return true; 00214 } 00215 00216 /// removeVirtualRegistersKilled - Remove all killed info for the specified 00217 /// instruction. 00218 void removeVirtualRegistersKilled(MachineInstr *MI); 00219 00220 /// addVirtualRegisterDead - Add information about the fact that the specified 00221 /// register is dead after being used by the specified instruction. If 00222 /// AddIfNotFound is true, add a implicit operand if it's not found. 00223 void addVirtualRegisterDead(unsigned IncomingReg, MachineInstr *MI, 00224 bool AddIfNotFound = false) { 00225 if (MI->addRegisterDead(IncomingReg, TRI, AddIfNotFound)) 00226 getVarInfo(IncomingReg).Kills.push_back(MI); 00227 } 00228 00229 /// removeVirtualRegisterDead - Remove the specified kill of the virtual 00230 /// register from the live variable information. Returns true if the 00231 /// variable was marked dead at the specified instruction, false 00232 /// otherwise. 00233 bool removeVirtualRegisterDead(unsigned reg, MachineInstr *MI) { 00234 if (!getVarInfo(reg).removeKill(MI)) 00235 return false; 00236 00237 bool Removed = false; 00238 for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) { 00239 MachineOperand &MO = MI->getOperand(i); 00240 if (MO.isReg() && MO.isDef() && MO.getReg() == reg) { 00241 MO.setIsDead(false); 00242 Removed = true; 00243 break; 00244 } 00245 } 00246 assert(Removed && "Register is not defined by this instruction!"); 00247 return true; 00248 } 00249 00250 void getAnalysisUsage(AnalysisUsage &AU) const; 00251 00252 virtual void releaseMemory() { 00253 VirtRegInfo.clear(); 00254 } 00255 00256 /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL 00257 /// register. 00258 VarInfo &getVarInfo(unsigned RegIdx); 00259 00260 void MarkVirtRegAliveInBlock(VarInfo& VRInfo, MachineBasicBlock* DefBlock, 00261 MachineBasicBlock *BB); 00262 void MarkVirtRegAliveInBlock(VarInfo& VRInfo, MachineBasicBlock* DefBlock, 00263 MachineBasicBlock *BB, 00264 std::vector<MachineBasicBlock*> &WorkList); 00265 void HandleVirtRegDef(unsigned reg, MachineInstr *MI); 00266 void HandleVirtRegUse(unsigned reg, MachineBasicBlock *MBB, 00267 MachineInstr *MI); 00268 }; 00269 00270 } // End llvm namespace 00271 00272 #endif