3debf347fc8c63321d5466fe1c3a9b6d6dd204dd
[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     std::map<MachineInstr*, std::vector<unsigned> >::const_iterator I = 
150       RegistersKilled.find(MI);
151     if (I != RegistersKilled.end())
152       for (std::vector<unsigned>::const_iterator CI = I->second.begin(),
153            E = I->second.end(); CI != E; ++CI)
154         if (*CI == Reg) return true;
155     return false;
156   }
157
158   killed_iterator dead_begin(MachineInstr *MI) {
159     return getDeadDefsVector(MI).begin();
160   }
161   killed_iterator dead_end(MachineInstr *MI) {
162     return getDeadDefsVector(MI).end();
163   }
164   std::pair<killed_iterator, killed_iterator>
165   dead_range(MachineInstr *MI) {
166     std::vector<unsigned> &V = getDeadDefsVector(MI);
167     return std::make_pair(V.begin(), V.end());
168   }
169   
170   /// RegisterDefIsDead - Return true if the specified instruction defines the
171   /// specified register, but that definition is dead.
172   bool RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const {
173     std::map<MachineInstr*, std::vector<unsigned> >::const_iterator I = 
174       RegistersDead.find(MI);
175     if (I != RegistersDead.end())
176       for (std::vector<unsigned>::const_iterator CI = I->second.begin(),
177            E = I->second.end(); CI != E; ++CI)
178         if (*CI == Reg) return true;
179     return false;
180   }
181
182   //===--------------------------------------------------------------------===//
183   //  API to update live variable information
184
185   /// instructionChanged - When the address of an instruction changes, this
186   /// method should be called so that live variables can update its internal
187   /// data structures.  This removes the records for OldMI, transfering them to
188   /// the records for NewMI.
189   void instructionChanged(MachineInstr *OldMI, MachineInstr *NewMI);
190
191   /// addVirtualRegisterKilled - Add information about the fact that the
192   /// specified register is killed after being used by the specified
193   /// instruction.
194   ///
195   void addVirtualRegisterKilled(unsigned IncomingReg, MachineInstr *MI) {
196     RegistersKilled.insert(std::make_pair(MI, IncomingReg));
197     getVarInfo(IncomingReg).Kills.push_back(MI);
198   }
199
200   /// removeVirtualRegisterKilled - Remove the specified virtual
201   /// register from the live variable information. Returns true if the
202   /// variable was marked as killed by the specified instruction,
203   /// false otherwise.
204   bool removeVirtualRegisterKilled(unsigned reg,
205                                    MachineBasicBlock *MBB,
206                                    MachineInstr *MI) {
207     if (!getVarInfo(reg).removeKill(MI))
208       return false;
209
210     std::vector<unsigned> &V = getKillsVector(MI);
211     for (unsigned i = 0, e = V.size(); i != e; ++i)
212       if (V[i] == reg) {
213         V.erase(V.begin()+i);
214         return true;
215       }
216     return true;
217   }
218
219   /// removeVirtualRegistersKilled - Remove all killed info for the specified
220   /// instruction.
221   void removeVirtualRegistersKilled(MachineInstr *MI) {
222     RegistersKilled.erase(MI);
223   }
224
225   /// addVirtualRegisterDead - Add information about the fact that the specified
226   /// register is dead after being used by the specified instruction.
227   ///
228   void addVirtualRegisterDead(unsigned IncomingReg, MachineInstr *MI) {
229     RegistersDead.insert(std::make_pair(MI, IncomingReg));
230     getVarInfo(IncomingReg).Kills.push_back(MI);
231   }
232
233   /// removeVirtualRegisterDead - Remove the specified virtual
234   /// register from the live variable information. Returns true if the
235   /// variable was marked dead at the specified instruction, false
236   /// otherwise.
237   bool removeVirtualRegisterDead(unsigned reg,
238                                  MachineBasicBlock *MBB,
239                                  MachineInstr *MI) {
240     if (!getVarInfo(reg).removeKill(MI))
241       return false;
242
243     std::vector<unsigned> &V = getDeadDefsVector(MI);
244     for (unsigned i = 0, e = V.size(); i != e; ++i)
245       if (V[i] == reg) {
246         V.erase(V.begin()+i);
247         return true;
248       }
249     return true;
250   }
251
252   /// removeVirtualRegistersDead - Remove all of the specified dead
253   /// registers from the live variable information.
254   void removeVirtualRegistersDead(MachineInstr *MI) {
255     RegistersDead.erase(MI);
256   }
257
258   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
259     AU.setPreservesAll();
260   }
261
262   virtual void releaseMemory() {
263     VirtRegInfo.clear();
264     RegistersKilled.clear();
265     RegistersDead.clear();
266   }
267
268   /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
269   /// register.
270   VarInfo &getVarInfo(unsigned RegIdx);
271
272   void MarkVirtRegAliveInBlock(VarInfo &VRInfo, MachineBasicBlock *BB);
273   void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
274                         MachineInstr *MI);
275 };
276
277 } // End llvm namespace
278
279 #endif