1 //===-- llvm/CodeGen/LiveVariables.h - Live Variable Analysis ---*- C++ -*-===//
3 // This file implements the LiveVariable analysis pass. For each machine
4 // instruction in the function, this pass calculates the set of registers that
5 // are immediately dead after the instruction (i.e., the instruction calculates
6 // the value, but it is never used) and the set of registers that are used by
7 // the instruction, but are never used after the instruction (i.e., they are
10 // This class computes live variables using are sparse implementation based on
11 // the machine code SSA form. This class computes live variable information for
12 // each virtual and _register allocatable_ physical register in a function. It
13 // uses the dominance properties of SSA form to efficiently compute live
14 // variables for virtual registers, and assumes that physical registers are only
15 // live within a single basic block (allowing it to do a single local analysis
16 // to resolve physical register lifetimes in each basic block). If a physical
17 // register is not register allocatable, it is not tracked. This is useful for
18 // things like the stack pointer and condition codes.
20 //===----------------------------------------------------------------------===//
22 #ifndef LLVM_CODEGEN_LIVEVARIABLES_H
23 #define LLVM_CODEGEN_LIVEVARIABLES_H
25 #include "llvm/CodeGen/MachineFunctionPass.h"
31 class LiveVariables : public MachineFunctionPass {
34 /// DefBlock - The basic block which defines this value...
35 MachineBasicBlock *DefBlock;
36 MachineInstr *DefInst;
38 /// AliveBlocks - Set of blocks of which this value is alive completely
39 /// through. This is a bit set which uses the basic block number as an
42 std::vector<bool> AliveBlocks;
44 /// Kills - List of MachineBasicblock's which contain the last use of this
45 /// virtual register (kill it). This also includes the specific instruction
46 /// which kills the value.
48 std::vector<std::pair<MachineBasicBlock*, MachineInstr*> > Kills;
50 VarInfo() : DefBlock(0), DefInst(0) {}
52 /// removeKill - Delete a kill corresponding to the specified machine instr
53 void removeKill(MachineInstr *MI) {
54 for (unsigned i = 0; ; ++i) {
55 assert(i < Kills.size() && "Machine instr is not a kill!");
56 if (Kills[i].second == MI) {
57 Kills.erase(Kills.begin()+i);
65 /// VirtRegInfo - This list is a mapping from virtual register number to
66 /// variable information. FirstVirtualRegister is subtracted from the virtual
67 /// register number before indexing into this list.
69 std::vector<VarInfo> VirtRegInfo;
71 /// RegistersKilled - This multimap keeps track of all of the registers that
72 /// are dead immediately after an instruction reads its operands. If an
73 /// instruction does not have an entry in this map, it kills no registers.
75 std::multimap<MachineInstr*, unsigned> RegistersKilled;
77 /// RegistersDead - This multimap keeps track of all of the registers that are
78 /// dead immediately after an instruction executes, which are not dead after
79 /// the operands are evaluated. In practice, this only contains registers
80 /// which are defined by an instruction, but never used.
82 std::multimap<MachineInstr*, unsigned> RegistersDead;
84 /// AllocatablePhysicalRegisters - This vector keeps track of which registers
85 /// are actually register allocatable by the target machine. We can not track
86 /// liveness for values that are not in this set.
88 std::vector<bool> AllocatablePhysicalRegisters;
90 private: // Intermediate data structures
92 /// BBMap - Maps LLVM basic blocks to their corresponding machine basic block.
93 /// This also provides a numbering of the basic blocks in the function.
94 std::map<const BasicBlock*, std::pair<MachineBasicBlock*, unsigned> > BBMap;
96 const MRegisterInfo *RegInfo;
98 MachineInstr **PhysRegInfo;
103 virtual bool runOnMachineFunction(MachineFunction &MF);
105 /// getMachineBasicBlockIndex - Turn a MachineBasicBlock into an index number
106 /// suitable for use with VarInfo's.
108 const std::pair<MachineBasicBlock*, unsigned>
109 &getMachineBasicBlockInfo(MachineBasicBlock *MBB) const;
110 const std::pair<MachineBasicBlock*, unsigned>
111 &getBasicBlockInfo(const BasicBlock *BB) const {
112 return BBMap.find(BB)->second;
116 /// killed_iterator - Iterate over registers killed by a machine instruction
118 typedef std::multimap<MachineInstr*, unsigned>::iterator killed_iterator;
120 /// killed_begin/end - Get access to the range of registers killed by a
121 /// machine instruction.
122 killed_iterator killed_begin(MachineInstr *MI) {
123 return RegistersKilled.lower_bound(MI);
125 killed_iterator killed_end(MachineInstr *MI) {
126 return RegistersKilled.upper_bound(MI);
128 std::pair<killed_iterator, killed_iterator>
129 killed_range(MachineInstr *MI) {
130 return RegistersKilled.equal_range(MI);
133 killed_iterator dead_begin(MachineInstr *MI) {
134 return RegistersDead.lower_bound(MI);
136 killed_iterator dead_end(MachineInstr *MI) {
137 return RegistersDead.upper_bound(MI);
139 std::pair<killed_iterator, killed_iterator>
140 dead_range(MachineInstr *MI) {
141 return RegistersDead.equal_range(MI);
144 //===--------------------------------------------------------------------===//
145 // API to update live variable information
147 /// addVirtualRegisterKilled - Add information about the fact that the
148 /// specified register is killed after being used by the specified
151 void addVirtualRegisterKilled(unsigned IncomingReg, MachineBasicBlock *MBB,
153 RegistersKilled.insert(std::make_pair(MI, IncomingReg));
154 getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
157 /// removeVirtualRegistersKilled - Remove all of the specified killed
158 /// registers from the live variable information.
159 void removeVirtualRegistersKilled(killed_iterator B, killed_iterator E) {
160 for (killed_iterator I = B; I != E; ++I) // Remove VarInfo entries...
161 getVarInfo(I->second).removeKill(I->first);
162 RegistersKilled.erase(B, E);
165 /// addVirtualRegisterDead - Add information about the fact that the specified
166 /// register is dead after being used by the specified instruction.
168 void addVirtualRegisterDead(unsigned IncomingReg, MachineBasicBlock *MBB,
170 RegistersDead.insert(std::make_pair(MI, IncomingReg));
171 getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
174 /// removeVirtualRegistersKilled - Remove all of the specified killed
175 /// registers from the live variable information.
176 void removeVirtualRegistersDead(killed_iterator B, killed_iterator E) {
177 for (killed_iterator I = B; I != E; ++I) // Remove VarInfo entries...
178 getVarInfo(I->second).removeKill(I->first);
179 RegistersDead.erase(B, E);
182 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
183 AU.setPreservesAll();
186 virtual void releaseMemory() {
188 RegistersKilled.clear();
189 RegistersDead.clear();
193 /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
195 VarInfo &getVarInfo(unsigned RegIdx);
197 void MarkVirtRegAliveInBlock(VarInfo &VRInfo, const BasicBlock *BB);
198 void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
200 void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
201 void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);