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