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"
37 class LiveVariables : public MachineFunctionPass {
40 /// DefBlock - The basic block which defines this value...
41 MachineBasicBlock *DefBlock;
42 MachineInstr *DefInst;
44 /// AliveBlocks - Set of blocks of which this value is alive completely
45 /// through. This is a bit set which uses the basic block number as an
48 std::vector<bool> AliveBlocks;
50 /// Kills - List of MachineBasicblock's which contain the last use of this
51 /// virtual register (kill it). This also includes the specific instruction
52 /// which kills the value.
54 std::vector<std::pair<MachineBasicBlock*, MachineInstr*> > Kills;
56 VarInfo() : DefBlock(0), DefInst(0) {}
58 /// removeKill - Delete a kill corresponding to the specified machine instr
59 void removeKill(MachineInstr *MI) {
60 for (unsigned i = 0; ; ++i) {
61 assert(i < Kills.size() && "Machine instr is not a kill!");
62 if (Kills[i].second == MI) {
63 Kills.erase(Kills.begin()+i);
71 /// VirtRegInfo - This list is a mapping from virtual register number to
72 /// variable information. FirstVirtualRegister is subtracted from the virtual
73 /// register number before indexing into this list.
75 std::vector<VarInfo> VirtRegInfo;
77 /// RegistersKilled - This multimap keeps track of all of the registers that
78 /// are dead immediately after an instruction reads its operands. If an
79 /// instruction does not have an entry in this map, it kills no registers.
81 std::multimap<MachineInstr*, unsigned> RegistersKilled;
83 /// RegistersDead - This multimap keeps track of all of the registers that are
84 /// dead immediately after an instruction executes, which are not dead after
85 /// the operands are evaluated. In practice, this only contains registers
86 /// which are defined by an instruction, but never used.
88 std::multimap<MachineInstr*, unsigned> RegistersDead;
90 /// AllocatablePhysicalRegisters - This vector keeps track of which registers
91 /// are actually register allocatable by the target machine. We can not track
92 /// liveness for values that are not in this set.
94 std::vector<bool> AllocatablePhysicalRegisters;
96 private: // Intermediate data structures
98 /// BBMap - Maps LLVM basic blocks to their corresponding machine basic block.
99 /// This also provides a numbering of the basic blocks in the function.
100 std::map<const BasicBlock*, std::pair<MachineBasicBlock*, unsigned> > BBMap;
102 const MRegisterInfo *RegInfo;
104 MachineInstr **PhysRegInfo;
109 virtual bool runOnMachineFunction(MachineFunction &MF);
111 /// getMachineBasicBlockIndex - Turn a MachineBasicBlock into an index number
112 /// suitable for use with VarInfo's.
114 const std::pair<MachineBasicBlock*, unsigned>
115 &getMachineBasicBlockInfo(MachineBasicBlock *MBB) const;
116 const std::pair<MachineBasicBlock*, unsigned>
117 &getBasicBlockInfo(const BasicBlock *BB) const {
118 return BBMap.find(BB)->second;
122 /// killed_iterator - Iterate over registers killed by a machine instruction
124 typedef std::multimap<MachineInstr*, unsigned>::iterator killed_iterator;
126 /// killed_begin/end - Get access to the range of registers killed by a
127 /// machine instruction.
128 killed_iterator killed_begin(MachineInstr *MI) {
129 return RegistersKilled.lower_bound(MI);
131 killed_iterator killed_end(MachineInstr *MI) {
132 return RegistersKilled.upper_bound(MI);
134 std::pair<killed_iterator, killed_iterator>
135 killed_range(MachineInstr *MI) {
136 return RegistersKilled.equal_range(MI);
139 killed_iterator dead_begin(MachineInstr *MI) {
140 return RegistersDead.lower_bound(MI);
142 killed_iterator dead_end(MachineInstr *MI) {
143 return RegistersDead.upper_bound(MI);
145 std::pair<killed_iterator, killed_iterator>
146 dead_range(MachineInstr *MI) {
147 return RegistersDead.equal_range(MI);
150 //===--------------------------------------------------------------------===//
151 // API to update live variable information
153 /// addVirtualRegisterKilled - Add information about the fact that the
154 /// specified register is killed after being used by the specified
157 void addVirtualRegisterKilled(unsigned IncomingReg, MachineBasicBlock *MBB,
159 RegistersKilled.insert(std::make_pair(MI, IncomingReg));
160 getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
163 /// removeVirtualRegistersKilled - Remove all of the specified killed
164 /// registers from the live variable information.
165 void removeVirtualRegistersKilled(killed_iterator B, killed_iterator E) {
166 for (killed_iterator I = B; I != E; ++I) // Remove VarInfo entries...
167 getVarInfo(I->second).removeKill(I->first);
168 RegistersKilled.erase(B, E);
171 /// addVirtualRegisterDead - Add information about the fact that the specified
172 /// register is dead after being used by the specified instruction.
174 void addVirtualRegisterDead(unsigned IncomingReg, MachineBasicBlock *MBB,
176 RegistersDead.insert(std::make_pair(MI, IncomingReg));
177 getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
180 /// removeVirtualRegistersKilled - Remove all of the specified killed
181 /// registers from the live variable information.
182 void removeVirtualRegistersDead(killed_iterator B, killed_iterator E) {
183 for (killed_iterator I = B; I != E; ++I) // Remove VarInfo entries...
184 getVarInfo(I->second).removeKill(I->first);
185 RegistersDead.erase(B, E);
188 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
189 AU.setPreservesAll();
192 virtual void releaseMemory() {
194 RegistersKilled.clear();
195 RegistersDead.clear();
199 /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
201 VarInfo &getVarInfo(unsigned RegIdx);
203 void MarkVirtRegAliveInBlock(VarInfo &VRInfo, const BasicBlock *BB);
204 void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
206 void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
207 void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);