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 machine instr
61 void removeKill(MachineInstr *MI) {
62 for (unsigned i = 0; ; ++i) {
63 assert(i < Kills.size() && "Machine instr is not a kill!");
64 if (Kills[i].second == MI) {
65 Kills.erase(Kills.begin()+i);
73 /// VirtRegInfo - This list is a mapping from virtual register number to
74 /// variable information. FirstVirtualRegister is subtracted from the virtual
75 /// register number before indexing into this list.
77 std::vector<VarInfo> VirtRegInfo;
79 /// RegistersKilled - This multimap keeps track of all of the registers that
80 /// are dead immediately after an instruction reads its operands. If an
81 /// instruction does not have an entry in this map, it kills no registers.
83 std::multimap<MachineInstr*, unsigned> RegistersKilled;
85 /// RegistersDead - This multimap keeps track of all of the registers that are
86 /// dead immediately after an instruction executes, which are not dead after
87 /// the operands are evaluated. In practice, this only contains registers
88 /// which are defined by an instruction, but never used.
90 std::multimap<MachineInstr*, unsigned> RegistersDead;
92 /// AllocatablePhysicalRegisters - This vector keeps track of which registers
93 /// are actually register allocatable by the target machine. We can not track
94 /// liveness for values that are not in this set.
96 std::vector<bool> AllocatablePhysicalRegisters;
98 private: // Intermediate data structures
100 /// BBMap - Maps LLVM basic blocks to their corresponding machine basic block.
101 /// This also provides a numbering of the basic blocks in the function.
102 std::map<const BasicBlock*, std::pair<MachineBasicBlock*, unsigned> > BBMap;
104 const MRegisterInfo *RegInfo;
106 MachineInstr **PhysRegInfo;
111 virtual bool runOnMachineFunction(MachineFunction &MF);
113 /// getMachineBasicBlockIndex - Turn a MachineBasicBlock into an index number
114 /// suitable for use with VarInfo's.
116 const std::pair<MachineBasicBlock*, unsigned>
117 &getMachineBasicBlockInfo(MachineBasicBlock *MBB) const;
118 const std::pair<MachineBasicBlock*, unsigned>
119 &getBasicBlockInfo(const BasicBlock *BB) const {
120 return BBMap.find(BB)->second;
124 /// killed_iterator - Iterate over registers killed by a machine instruction
126 typedef std::multimap<MachineInstr*, unsigned>::iterator killed_iterator;
128 /// killed_begin/end - Get access to the range of registers killed by a
129 /// machine instruction.
130 killed_iterator killed_begin(MachineInstr *MI) {
131 return RegistersKilled.lower_bound(MI);
133 killed_iterator killed_end(MachineInstr *MI) {
134 return RegistersKilled.upper_bound(MI);
136 std::pair<killed_iterator, killed_iterator>
137 killed_range(MachineInstr *MI) {
138 return RegistersKilled.equal_range(MI);
141 killed_iterator dead_begin(MachineInstr *MI) {
142 return RegistersDead.lower_bound(MI);
144 killed_iterator dead_end(MachineInstr *MI) {
145 return RegistersDead.upper_bound(MI);
147 std::pair<killed_iterator, killed_iterator>
148 dead_range(MachineInstr *MI) {
149 return RegistersDead.equal_range(MI);
152 //===--------------------------------------------------------------------===//
153 // API to update live variable information
155 /// addVirtualRegisterKilled - Add information about the fact that the
156 /// specified register is killed after being used by the specified
159 void addVirtualRegisterKilled(unsigned IncomingReg, MachineBasicBlock *MBB,
161 RegistersKilled.insert(std::make_pair(MI, IncomingReg));
162 getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
165 /// removeVirtualRegistersKilled - Remove all of the specified killed
166 /// registers from the live variable information.
167 void removeVirtualRegistersKilled(killed_iterator B, killed_iterator E) {
168 for (killed_iterator I = B; I != E; ++I) // Remove VarInfo entries...
169 getVarInfo(I->second).removeKill(I->first);
170 RegistersKilled.erase(B, E);
173 /// addVirtualRegisterDead - Add information about the fact that the specified
174 /// register is dead after being used by the specified instruction.
176 void addVirtualRegisterDead(unsigned IncomingReg, MachineBasicBlock *MBB,
178 RegistersDead.insert(std::make_pair(MI, IncomingReg));
179 getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
182 /// removeVirtualRegistersKilled - Remove all of the specified killed
183 /// registers from the live variable information.
184 void removeVirtualRegistersDead(killed_iterator B, killed_iterator E) {
185 for (killed_iterator I = B; I != E; ++I) // Remove VarInfo entries...
186 getVarInfo(I->second).removeKill(I->first);
187 RegistersDead.erase(B, E);
190 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
191 AU.setPreservesAll();
194 virtual void releaseMemory() {
196 RegistersKilled.clear();
197 RegistersDead.clear();
201 /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
203 VarInfo &getVarInfo(unsigned RegIdx);
205 void MarkVirtRegAliveInBlock(VarInfo &VRInfo, const BasicBlock *BB);
206 void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
208 void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
209 void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);
212 } // End llvm namespace