881fce4b0dceef45d40d4fded6fba096f6567ab5
[oota-llvm.git] / include / llvm / CodeGen / LiveVariables.h
1 //===-- llvm/CodeGen/LiveVariables.h - Live Variable Analysis ---*- C++ -*-===//
2 // 
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
8 // killed).
9 //
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.
19 //   
20 //===----------------------------------------------------------------------===//
21
22 #ifndef LLVM_CODEGEN_LIVEVARIABLES_H
23 #define LLVM_CODEGEN_LIVEVARIABLES_H
24
25 #include "llvm/CodeGen/MachineFunctionPass.h"
26 #include <map>
27 #include <assert.h>
28
29 class MRegisterInfo;
30
31 class LiveVariables : public MachineFunctionPass {
32 public:
33   struct VarInfo {
34     /// DefBlock - The basic block which defines this value...
35     MachineBasicBlock *DefBlock;
36     MachineInstr      *DefInst;
37
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
40     /// index.
41     ///
42     std::vector<bool> AliveBlocks;
43
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.
47     ///
48     std::vector<std::pair<MachineBasicBlock*, MachineInstr*> > Kills;
49
50     VarInfo() : DefBlock(0), DefInst(0) {}
51
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);
58           return;
59         }
60       }
61     }
62   };
63
64 private:
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.
68   ///
69   std::vector<VarInfo> VirtRegInfo;
70
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.
74   ///
75   std::multimap<MachineInstr*, unsigned> RegistersKilled;
76
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.
81   ///
82   std::multimap<MachineInstr*, unsigned> RegistersDead;
83
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.
87   ///
88   std::vector<bool> AllocatablePhysicalRegisters;
89
90 private:   // Intermediate data structures
91
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;
95   
96   const MRegisterInfo *RegInfo;
97
98   MachineInstr **PhysRegInfo;
99   bool          *PhysRegUsed;
100
101 public:
102
103   virtual bool runOnMachineFunction(MachineFunction &MF);
104
105   /// getMachineBasicBlockIndex - Turn a MachineBasicBlock into an index number
106   /// suitable for use with VarInfo's.
107   ///
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;
113   }
114
115
116   /// killed_iterator - Iterate over registers killed by a machine instruction
117   ///
118   typedef std::multimap<MachineInstr*, unsigned>::iterator killed_iterator;
119   
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);
124   }
125   killed_iterator killed_end(MachineInstr *MI) {
126     return RegistersKilled.upper_bound(MI);
127   }
128   std::pair<killed_iterator, killed_iterator>
129   killed_range(MachineInstr *MI) {
130     return RegistersKilled.equal_range(MI);
131   }
132
133   killed_iterator dead_begin(MachineInstr *MI) {
134     return RegistersDead.lower_bound(MI);
135   }
136   killed_iterator dead_end(MachineInstr *MI) {
137     return RegistersDead.upper_bound(MI);
138   }
139   std::pair<killed_iterator, killed_iterator>
140   dead_range(MachineInstr *MI) {
141     return RegistersDead.equal_range(MI);
142   }
143
144   //===--------------------------------------------------------------------===//
145   //  API to update live variable information
146
147   /// addVirtualRegisterKilled - Add information about the fact that the
148   /// specified register is killed after being used by the specified
149   /// instruction.
150   ///
151   void addVirtualRegisterKilled(unsigned IncomingReg, MachineBasicBlock *MBB,
152                                 MachineInstr *MI) {
153     RegistersKilled.insert(std::make_pair(MI, IncomingReg));
154     getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
155   }
156
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);
163   }
164
165   /// addVirtualRegisterDead - Add information about the fact that the specified
166   /// register is dead after being used by the specified instruction.
167   ///
168   void addVirtualRegisterDead(unsigned IncomingReg, MachineBasicBlock *MBB,
169                               MachineInstr *MI) {
170     RegistersDead.insert(std::make_pair(MI, IncomingReg));
171     getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
172   }
173
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);
180   }
181
182   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
183     AU.setPreservesAll();
184   }
185
186   virtual void releaseMemory() {
187     VirtRegInfo.clear();
188     RegistersKilled.clear();
189     RegistersDead.clear();
190     BBMap.clear();
191   }
192
193   /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
194   /// register.
195   VarInfo &getVarInfo(unsigned RegIdx);
196
197   void MarkVirtRegAliveInBlock(VarInfo &VRInfo, const BasicBlock *BB);
198   void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
199                         MachineInstr *MI);
200   void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
201   void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);
202 };
203
204 #endif