1 //===-- llvm/CodeGen/LiveVariables.h - Live Variable Analysis ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements the LiveVariable analysis pass. For each machine
11 // instruction in the function, this pass calculates the set of registers that
12 // are immediately dead after the instruction (i.e., the instruction calculates
13 // the value, but it is never used) and the set of registers that are used by
14 // the instruction, but are never used after the instruction (i.e., they are
17 // This class computes live variables using are sparse implementation based on
18 // the machine code SSA form. This class computes live variable information for
19 // each virtual and _register allocatable_ physical register in a function. It
20 // uses the dominance properties of SSA form to efficiently compute live
21 // variables for virtual registers, and assumes that physical registers are only
22 // live within a single basic block (allowing it to do a single local analysis
23 // to resolve physical register lifetimes in each basic block). If a physical
24 // register is not register allocatable, it is not tracked. This is useful for
25 // things like the stack pointer and condition codes.
27 //===----------------------------------------------------------------------===//
29 #ifndef LLVM_CODEGEN_LIVEVARIABLES_H
30 #define LLVM_CODEGEN_LIVEVARIABLES_H
32 #include "llvm/CodeGen/MachineFunctionPass.h"
39 class LiveVariables : public MachineFunctionPass {
42 /// DefBlock - The basic block which defines this value...
43 MachineBasicBlock *DefBlock;
44 MachineInstr *DefInst;
46 /// AliveBlocks - Set of blocks of which this value is alive completely
47 /// through. This is a bit set which uses the basic block number as an
50 std::vector<bool> AliveBlocks;
52 /// Kills - List of MachineBasicblock's which contain the last use of this
53 /// virtual register (kill it). This also includes the specific instruction
54 /// which kills the value.
56 std::vector<std::pair<MachineBasicBlock*, MachineInstr*> > Kills;
58 VarInfo() : DefBlock(0), DefInst(0) {}
60 /// removeKill - Delete a kill corresponding to the specified
61 /// machine instruction. Returns true if there was a kill
62 /// corresponding to this instruction, false otherwise.
63 bool removeKill(MachineInstr *MI) {
64 for (std::vector<std::pair<MachineBasicBlock*, MachineInstr*> >::iterator
65 i = Kills.begin(); i != Kills.end(); ++i) {
66 if (i->second == MI) {
76 /// VirtRegInfo - This list is a mapping from virtual register number to
77 /// variable information. FirstVirtualRegister is subtracted from the virtual
78 /// register number before indexing into this list.
80 std::vector<VarInfo> VirtRegInfo;
82 /// RegistersKilled - This multimap keeps track of all of the registers that
83 /// are dead immediately after an instruction reads its operands. If an
84 /// instruction does not have an entry in this map, it kills no registers.
86 std::multimap<MachineInstr*, unsigned> RegistersKilled;
88 /// RegistersDead - This multimap keeps track of all of the registers that are
89 /// dead immediately after an instruction executes, which are not dead after
90 /// the operands are evaluated. In practice, this only contains registers
91 /// which are defined by an instruction, but never used.
93 std::multimap<MachineInstr*, unsigned> RegistersDead;
95 /// AllocatablePhysicalRegisters - This vector keeps track of which registers
96 /// are actually register allocatable by the target machine. We can not track
97 /// liveness for values that are not in this set.
99 std::vector<bool> AllocatablePhysicalRegisters;
101 private: // Intermediate data structures
103 /// BBMap - Maps LLVM basic blocks to their corresponding machine basic block.
104 /// This also provides a numbering of the basic blocks in the function.
105 std::map<const BasicBlock*, std::pair<MachineBasicBlock*, unsigned> > BBMap;
108 /// BBIdxMap - This contains the inverse mapping of BBMap, going from block ID
109 /// numbers to the corresponding MachineBasicBlock. This is lazily computed
110 /// when the getIndexMachineBasicBlock() method is called.
111 std::vector<MachineBasicBlock*> BBIdxMap;
113 const MRegisterInfo *RegInfo;
115 MachineInstr **PhysRegInfo;
118 void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
119 void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);
123 virtual bool runOnMachineFunction(MachineFunction &MF);
125 /// getMachineBasicBlockIndex - Turn a MachineBasicBlock into an index number
126 /// suitable for use with VarInfo's.
128 const std::pair<MachineBasicBlock*, unsigned>
129 &getMachineBasicBlockInfo(MachineBasicBlock *MBB) const;
130 const std::pair<MachineBasicBlock*, unsigned>
131 &getBasicBlockInfo(const BasicBlock *BB) const {
132 return BBMap.find(BB)->second;
135 /// getIndexMachineBasicBlock() - Given a block index, return the
136 /// MachineBasicBlock corresponding to it.
137 MachineBasicBlock *getIndexMachineBasicBlock(unsigned Idx);
139 /// killed_iterator - Iterate over registers killed by a machine instruction
141 typedef std::multimap<MachineInstr*, unsigned>::iterator killed_iterator;
143 /// killed_begin/end - Get access to the range of registers killed by a
144 /// machine instruction.
145 killed_iterator killed_begin(MachineInstr *MI) {
146 return RegistersKilled.lower_bound(MI);
148 killed_iterator killed_end(MachineInstr *MI) {
149 return RegistersKilled.upper_bound(MI);
151 std::pair<killed_iterator, killed_iterator>
152 killed_range(MachineInstr *MI) {
153 return RegistersKilled.equal_range(MI);
156 killed_iterator dead_begin(MachineInstr *MI) {
157 return RegistersDead.lower_bound(MI);
159 killed_iterator dead_end(MachineInstr *MI) {
160 return RegistersDead.upper_bound(MI);
162 std::pair<killed_iterator, killed_iterator>
163 dead_range(MachineInstr *MI) {
164 return RegistersDead.equal_range(MI);
167 //===--------------------------------------------------------------------===//
168 // API to update live variable information
170 /// instructionChanged - When the address of an instruction changes, this
171 /// method should be called so that live variables can update its internal
172 /// data structures. This removes the records for OldMI, transfering them to
173 /// the records for NewMI.
174 void instructionChanged(MachineInstr *OldMI, MachineInstr *NewMI);
176 /// addVirtualRegisterKilled - Add information about the fact that the
177 /// specified register is killed after being used by the specified
180 void addVirtualRegisterKilled(unsigned IncomingReg,
181 MachineBasicBlock *MBB,
183 RegistersKilled.insert(std::make_pair(MI, IncomingReg));
184 getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
187 /// removeVirtualRegisterKilled - Remove the specified virtual
188 /// register from the live variable information. Returns true if the
189 /// variable was marked as killed by the specified instruction,
191 bool removeVirtualRegisterKilled(unsigned reg,
192 MachineBasicBlock *MBB,
194 if (!getVarInfo(reg).removeKill(MI))
196 for (killed_iterator i = killed_begin(MI), e = killed_end(MI); i != e; ) {
197 if (i->second == reg)
198 RegistersKilled.erase(i++);
205 /// removeVirtualRegistersKilled - Remove all of the specified killed
206 /// registers from the live variable information.
207 void removeVirtualRegistersKilled(killed_iterator B, killed_iterator E) {
208 for (killed_iterator I = B; I != E; ++I) { // Remove VarInfo entries...
209 bool removed = getVarInfo(I->second).removeKill(I->first);
210 assert(removed && "kill not in register's VarInfo?");
212 RegistersKilled.erase(B, E);
215 /// addVirtualRegisterDead - Add information about the fact that the specified
216 /// register is dead after being used by the specified instruction.
218 void addVirtualRegisterDead(unsigned IncomingReg,
219 MachineBasicBlock *MBB,
221 RegistersDead.insert(std::make_pair(MI, IncomingReg));
222 getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
225 /// removeVirtualRegisterDead - Remove the specified virtual
226 /// register from the live variable information. Returns true if the
227 /// variable was marked dead at the specified instruction, false
229 bool removeVirtualRegisterDead(unsigned reg,
230 MachineBasicBlock *MBB,
232 if (!getVarInfo(reg).removeKill(MI))
235 for (killed_iterator i = killed_begin(MI), e = killed_end(MI); i != e; ) {
236 if (i->second == reg)
237 RegistersKilled.erase(i++);
244 /// removeVirtualRegistersDead - Remove all of the specified dead
245 /// registers from the live variable information.
246 void removeVirtualRegistersDead(killed_iterator B, killed_iterator E) {
247 for (killed_iterator I = B; I != E; ++I) // Remove VarInfo entries...
248 getVarInfo(I->second).removeKill(I->first);
249 RegistersDead.erase(B, E);
252 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
253 AU.setPreservesAll();
256 virtual void releaseMemory() {
258 RegistersKilled.clear();
259 RegistersDead.clear();
264 /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
266 VarInfo &getVarInfo(unsigned RegIdx);
268 const std::vector<bool>& getAllocatablePhysicalRegisters() const {
269 return AllocatablePhysicalRegisters;
272 void MarkVirtRegAliveInBlock(VarInfo &VRInfo, const BasicBlock *BB);
273 void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
277 } // End llvm namespace