28cfa2a0b2bdb8aa2cf480c43964ed415ac5685b
[oota-llvm.git] / include / llvm / CodeGen / LiveVariables.h
1 //===-- llvm/CodeGen/LiveVariables.h - Live Variable Analysis ---*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 //
8 //===----------------------------------------------------------------------===//
9 //
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
15 // killed).
16 //
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.
26 //
27 //===----------------------------------------------------------------------===//
28
29 #ifndef LLVM_CODEGEN_LIVEVARIABLES_H
30 #define LLVM_CODEGEN_LIVEVARIABLES_H
31
32 #include "llvm/CodeGen/MachineFunctionPass.h"
33 #include <map>
34
35 namespace llvm {
36
37 class MRegisterInfo;
38
39 class LiveVariables : public MachineFunctionPass {
40 public:
41   struct VarInfo {
42     /// DefInst - The machine instruction that defines this register.
43     ///
44     MachineInstr *DefInst;
45
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
48     /// index.
49     ///
50     std::vector<bool> AliveBlocks;
51
52     /// Kills - List of MachineInstruction's which are the last use of this
53     /// virtual register (kill it) in their basic block.
54     ///
55     std::vector<MachineInstr*> Kills;
56
57     VarInfo() : DefInst(0) {}
58
59     /// removeKill - Delete a kill corresponding to the specified
60     /// machine instruction. Returns true if there was a kill
61     /// corresponding to this instruction, false otherwise.
62     bool removeKill(MachineInstr *MI) {
63       for (std::vector<MachineInstr*>::iterator i = Kills.begin(),
64              e = Kills.end(); i != e; ++i)
65         if (*i == MI) {
66           Kills.erase(i);
67           return true;
68         }
69       return false;
70     }
71   };
72
73 private:
74   /// VirtRegInfo - This list is a mapping from virtual register number to
75   /// variable information.  FirstVirtualRegister is subtracted from the virtual
76   /// register number before indexing into this list.
77   ///
78   std::vector<VarInfo> VirtRegInfo;
79
80   /// RegistersKilled - This map keeps track of all of the registers that
81   /// are dead immediately after an instruction reads its operands.  If an
82   /// instruction does not have an entry in this map, it kills no registers.
83   ///
84   std::map<MachineInstr*, std::vector<unsigned> > RegistersKilled;
85
86   /// RegistersDead - This map keeps track of all of the registers that are
87   /// dead immediately after an instruction executes, which are not dead after
88   /// the operands are evaluated.  In practice, this only contains registers
89   /// which are defined by an instruction, but never used.
90   ///
91   std::map<MachineInstr*, std::vector<unsigned> > RegistersDead;
92   
93   /// Dummy - An always empty vector used for instructions without dead or
94   /// killed operands.
95   std::vector<unsigned> Dummy;
96
97   /// AllocatablePhysicalRegisters - This vector keeps track of which registers
98   /// are actually register allocatable by the target machine.  We can not track
99   /// liveness for values that are not in this set.
100   ///
101   std::vector<bool> AllocatablePhysicalRegisters;
102
103 private:   // Intermediate data structures
104   const MRegisterInfo *RegInfo;
105
106   MachineInstr **PhysRegInfo;
107   bool          *PhysRegUsed;
108
109   void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
110   void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);
111
112 public:
113
114   virtual bool runOnMachineFunction(MachineFunction &MF);
115
116   /// killed_iterator - Iterate over registers killed by a machine instruction
117   ///
118   typedef std::vector<unsigned>::iterator killed_iterator;
119
120   std::vector<unsigned> &getKillsVector(MachineInstr *MI) {
121     std::map<MachineInstr*, std::vector<unsigned> >::iterator I = 
122       RegistersKilled.find(MI);
123     return I != RegistersKilled.end() ? I->second : Dummy;
124   }
125   std::vector<unsigned> &getDeadDefsVector(MachineInstr *MI) {
126     std::map<MachineInstr*, std::vector<unsigned> >::iterator I = 
127       RegistersDead.find(MI);
128     return I != RegistersDead.end() ? I->second : Dummy;
129   }
130   
131     
132   /// killed_begin/end - Get access to the range of registers killed by a
133   /// machine instruction.
134   killed_iterator killed_begin(MachineInstr *MI) {
135     return getKillsVector(MI).begin();
136   }
137   killed_iterator killed_end(MachineInstr *MI) {
138     return getKillsVector(MI).end();
139   }
140   std::pair<killed_iterator, killed_iterator>
141   killed_range(MachineInstr *MI) {
142     std::vector<unsigned> &V = getKillsVector(MI);
143     return std::make_pair(V.begin(), V.end());
144   }
145
146   /// KillsRegister - Return true if the specified instruction kills the
147   /// specified register.
148   bool KillsRegister(MachineInstr *MI, unsigned Reg) const;
149   
150   killed_iterator dead_begin(MachineInstr *MI) {
151     return getDeadDefsVector(MI).begin();
152   }
153   killed_iterator dead_end(MachineInstr *MI) {
154     return getDeadDefsVector(MI).end();
155   }
156   std::pair<killed_iterator, killed_iterator>
157   dead_range(MachineInstr *MI) {
158     std::vector<unsigned> &V = getDeadDefsVector(MI);
159     return std::make_pair(V.begin(), V.end());
160   }
161   
162   /// RegisterDefIsDead - Return true if the specified instruction defines the
163   /// specified register, but that definition is dead.
164   bool RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const;
165   
166   //===--------------------------------------------------------------------===//
167   //  API to update live variable information
168
169   /// instructionChanged - When the address of an instruction changes, this
170   /// method should be called so that live variables can update its internal
171   /// data structures.  This removes the records for OldMI, transfering them to
172   /// the records for NewMI.
173   void instructionChanged(MachineInstr *OldMI, MachineInstr *NewMI);
174
175   /// addVirtualRegisterKilled - Add information about the fact that the
176   /// specified register is killed after being used by the specified
177   /// instruction.
178   ///
179   void addVirtualRegisterKilled(unsigned IncomingReg, MachineInstr *MI) {
180     std::vector<unsigned> &V = RegistersKilled[MI];
181     // Insert in a sorted order.
182     if (V.empty() || IncomingReg > V.back()) {
183       V.push_back(IncomingReg);
184     } else {
185       std::vector<unsigned>::iterator I = V.begin();
186       for (; *I < IncomingReg; ++I)
187         /*empty*/;
188       if (*I != IncomingReg)   // Don't insert duplicates.
189         V.insert(I, IncomingReg);
190     }
191     getVarInfo(IncomingReg).Kills.push_back(MI);
192   }
193
194   /// removeVirtualRegisterKilled - Remove the specified virtual
195   /// register from the live variable information. Returns true if the
196   /// variable was marked as killed by the specified instruction,
197   /// false otherwise.
198   bool removeVirtualRegisterKilled(unsigned reg,
199                                    MachineBasicBlock *MBB,
200                                    MachineInstr *MI) {
201     if (!getVarInfo(reg).removeKill(MI))
202       return false;
203
204     std::vector<unsigned> &V = getKillsVector(MI);
205     for (unsigned i = 0, e = V.size(); i != e; ++i)
206       if (V[i] == reg) {
207         V.erase(V.begin()+i);
208         return true;
209       }
210     return true;
211   }
212
213   /// removeVirtualRegistersKilled - Remove all killed info for the specified
214   /// instruction.
215   void removeVirtualRegistersKilled(MachineInstr *MI) {
216     std::map<MachineInstr*, std::vector<unsigned> >::iterator I = 
217       RegistersKilled.find(MI);
218     if (I != RegistersKilled.end()) {
219       std::vector<unsigned> &Regs = I->second;
220       for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
221         bool removed = getVarInfo(Regs[i]).removeKill(MI);
222         assert(removed && "kill not in register's VarInfo?");
223       }
224       RegistersKilled.erase(I);
225     }
226   }
227
228   /// addVirtualRegisterDead - Add information about the fact that the specified
229   /// register is dead after being used by the specified instruction.
230   ///
231   void addVirtualRegisterDead(unsigned IncomingReg, MachineInstr *MI) {
232     std::vector<unsigned> &V = RegistersDead[MI];
233     // Insert in a sorted order.
234     if (V.empty() || IncomingReg > V.back()) {
235       V.push_back(IncomingReg);
236     } else {
237       std::vector<unsigned>::iterator I = V.begin();
238       for (; *I < IncomingReg; ++I)
239         /*empty*/;
240       if (*I != IncomingReg)   // Don't insert duplicates.
241         V.insert(I, IncomingReg);
242     }
243     getVarInfo(IncomingReg).Kills.push_back(MI);
244   }
245
246   /// removeVirtualRegisterDead - Remove the specified virtual
247   /// register from the live variable information. Returns true if the
248   /// variable was marked dead at the specified instruction, false
249   /// otherwise.
250   bool removeVirtualRegisterDead(unsigned reg,
251                                  MachineBasicBlock *MBB,
252                                  MachineInstr *MI) {
253     if (!getVarInfo(reg).removeKill(MI))
254       return false;
255
256     std::vector<unsigned> &V = getDeadDefsVector(MI);
257     for (unsigned i = 0, e = V.size(); i != e; ++i)
258       if (V[i] == reg) {
259         V.erase(V.begin()+i);
260         return true;
261       }
262     return true;
263   }
264
265   /// removeVirtualRegistersDead - Remove all of the specified dead
266   /// registers from the live variable information.
267   void removeVirtualRegistersDead(MachineInstr *MI) {
268     std::map<MachineInstr*, std::vector<unsigned> >::iterator I = 
269       RegistersDead.find(MI);
270     if (I != RegistersDead.end()) {
271       std::vector<unsigned> &Regs = I->second;
272       for (unsigned i = 0, e = Regs.size(); i != e; ++i) {
273         bool removed = getVarInfo(Regs[i]).removeKill(MI);
274         assert(removed && "kill not in register's VarInfo?");
275       }
276       RegistersDead.erase(I);
277     }
278   }
279
280   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
281     AU.setPreservesAll();
282   }
283
284   virtual void releaseMemory() {
285     VirtRegInfo.clear();
286     RegistersKilled.clear();
287     RegistersDead.clear();
288   }
289
290   /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
291   /// register.
292   VarInfo &getVarInfo(unsigned RegIdx);
293
294   void MarkVirtRegAliveInBlock(VarInfo &VRInfo, MachineBasicBlock *BB);
295   void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
296                         MachineInstr *MI);
297 };
298
299 } // End llvm namespace
300
301 #endif