80cbb41950b57c7eda9d3ec61d2371857cea830e
[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     MachineInstr      *DefInst;
44
45     /// AliveBlocks - Set of blocks of which this value is alive completely
46     /// through.  This is a bit set which uses the basic block number as an
47     /// index.
48     ///
49     std::vector<bool> AliveBlocks;
50
51     /// Kills - List of MachineBasicblock's which contain the last use of this
52     /// virtual register (kill it).  This also includes the specific instruction
53     /// which kills the value.
54     ///
55     std::vector<std::pair<MachineBasicBlock*, 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<std::pair<MachineBasicBlock*, MachineInstr*> >::iterator
64              i = Kills.begin(); i != Kills.end(); ++i) {
65         if (i->second == MI) {
66           Kills.erase(i);
67           return true;
68         }
69       }
70       return false;
71     }
72   };
73
74 private:
75   /// VirtRegInfo - This list is a mapping from virtual register number to
76   /// variable information.  FirstVirtualRegister is subtracted from the virtual
77   /// register number before indexing into this list.
78   ///
79   std::vector<VarInfo> VirtRegInfo;
80
81   /// RegistersKilled - This multimap keeps track of all of the registers that
82   /// are dead immediately after an instruction reads its operands.  If an
83   /// instruction does not have an entry in this map, it kills no registers.
84   ///
85   std::multimap<MachineInstr*, unsigned> RegistersKilled;
86
87   /// RegistersDead - This multimap keeps track of all of the registers that are
88   /// dead immediately after an instruction executes, which are not dead after
89   /// the operands are evaluated.  In practice, this only contains registers
90   /// which are defined by an instruction, but never used.
91   ///
92   std::multimap<MachineInstr*, unsigned> RegistersDead;
93
94   /// AllocatablePhysicalRegisters - This vector keeps track of which registers
95   /// are actually register allocatable by the target machine.  We can not track
96   /// liveness for values that are not in this set.
97   ///
98   std::vector<bool> AllocatablePhysicalRegisters;
99
100 private:   // Intermediate data structures
101   const MRegisterInfo *RegInfo;
102
103   MachineInstr **PhysRegInfo;
104   bool          *PhysRegUsed;
105
106   void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
107   void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);
108
109 public:
110
111   virtual bool runOnMachineFunction(MachineFunction &MF);
112
113   /// killed_iterator - Iterate over registers killed by a machine instruction
114   ///
115   typedef std::multimap<MachineInstr*, unsigned>::iterator killed_iterator;
116   
117   /// killed_begin/end - Get access to the range of registers killed by a
118   /// machine instruction.
119   killed_iterator killed_begin(MachineInstr *MI) {
120     return RegistersKilled.lower_bound(MI);
121   }
122   killed_iterator killed_end(MachineInstr *MI) {
123     return RegistersKilled.upper_bound(MI);
124   }
125   std::pair<killed_iterator, killed_iterator>
126   killed_range(MachineInstr *MI) {
127     return RegistersKilled.equal_range(MI);
128   }
129
130   killed_iterator dead_begin(MachineInstr *MI) {
131     return RegistersDead.lower_bound(MI);
132   }
133   killed_iterator dead_end(MachineInstr *MI) {
134     return RegistersDead.upper_bound(MI);
135   }
136   std::pair<killed_iterator, killed_iterator>
137   dead_range(MachineInstr *MI) {
138     return RegistersDead.equal_range(MI);
139   }
140
141   //===--------------------------------------------------------------------===//
142   //  API to update live variable information
143
144   /// instructionChanged - When the address of an instruction changes, this
145   /// method should be called so that live variables can update its internal
146   /// data structures.  This removes the records for OldMI, transfering them to
147   /// the records for NewMI.
148   void instructionChanged(MachineInstr *OldMI, MachineInstr *NewMI);
149
150   /// addVirtualRegisterKilled - Add information about the fact that the
151   /// specified register is killed after being used by the specified
152   /// instruction.
153   ///
154   void addVirtualRegisterKilled(unsigned IncomingReg, MachineInstr *MI) {
155     RegistersKilled.insert(std::make_pair(MI, IncomingReg));
156     getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MI->getParent(),MI));
157   }
158
159   /// removeVirtualRegisterKilled - Remove the specified virtual
160   /// register from the live variable information. Returns true if the
161   /// variable was marked as killed by the specified instruction,
162   /// false otherwise.
163   bool removeVirtualRegisterKilled(unsigned reg,
164                                    MachineBasicBlock *MBB,
165                                    MachineInstr *MI) {
166     if (!getVarInfo(reg).removeKill(MI))
167       return false;
168     for (killed_iterator i = killed_begin(MI), e = killed_end(MI); i != e; ) {
169       if (i->second == reg)
170         RegistersKilled.erase(i++);
171       else
172         ++i;
173     }
174     return true;
175   }
176
177   /// removeVirtualRegistersKilled - Remove all of the specified killed
178   /// registers from the live variable information.
179   void removeVirtualRegistersKilled(killed_iterator B, killed_iterator E) {
180     for (killed_iterator I = B; I != E; ++I) { // Remove VarInfo entries...
181       bool removed = getVarInfo(I->second).removeKill(I->first);
182       assert(removed && "kill not in register's VarInfo?");
183     }
184     RegistersKilled.erase(B, E);
185   }
186
187   /// addVirtualRegisterDead - Add information about the fact that the specified
188   /// register is dead after being used by the specified instruction.
189   ///
190   void addVirtualRegisterDead(unsigned IncomingReg, MachineInstr *MI) {
191     RegistersDead.insert(std::make_pair(MI, IncomingReg));
192     getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MI->getParent(),MI));
193   }
194
195   /// removeVirtualRegisterDead - Remove the specified virtual
196   /// register from the live variable information. Returns true if the
197   /// variable was marked dead at the specified instruction, false
198   /// otherwise.
199   bool removeVirtualRegisterDead(unsigned reg,
200                                  MachineBasicBlock *MBB,
201                                  MachineInstr *MI) {
202     if (!getVarInfo(reg).removeKill(MI))
203       return false;
204
205     for (killed_iterator i = killed_begin(MI), e = killed_end(MI); i != e; ) {
206       if (i->second == reg)
207         RegistersKilled.erase(i++);
208       else
209         ++i;
210     }
211     return true;
212   }
213
214   /// removeVirtualRegistersDead - Remove all of the specified dead
215   /// registers from the live variable information.
216   void removeVirtualRegistersDead(killed_iterator B, killed_iterator E) {
217     for (killed_iterator I = B; I != E; ++I)  // Remove VarInfo entries...
218       getVarInfo(I->second).removeKill(I->first);
219     RegistersDead.erase(B, E);
220   }
221
222   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
223     AU.setPreservesAll();
224   }
225
226   virtual void releaseMemory() {
227     VirtRegInfo.clear();
228     RegistersKilled.clear();
229     RegistersDead.clear();
230   }
231
232   /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
233   /// register.
234   VarInfo &getVarInfo(unsigned RegIdx);
235
236   const std::vector<bool>& getAllocatablePhysicalRegisters() const {
237     return AllocatablePhysicalRegisters;
238   }
239
240   void MarkVirtRegAliveInBlock(VarInfo &VRInfo, MachineBasicBlock *BB);
241   void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
242                         MachineInstr *MI);
243 };
244
245 } // End llvm namespace
246
247 #endif