Add a useful accessor
[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 MachineInstruction's which are the last use of this
52     /// virtual register (kill it) in their basic block.
53     ///
54     std::vector<MachineInstr*> Kills;
55
56     VarInfo() : DefInst(0) {}
57
58     /// removeKill - Delete a kill corresponding to the specified
59     /// machine instruction. Returns true if there was a kill
60     /// corresponding to this instruction, false otherwise.
61     bool removeKill(MachineInstr *MI) {
62       for (std::vector<MachineInstr*>::iterator i = Kills.begin(),
63              e = Kills.end(); i != e; ++i)
64         if (*i == MI) {
65           Kills.erase(i);
66           return true;
67         }
68       return false;
69     }
70   };
71
72 private:
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.
76   ///
77   std::vector<VarInfo> VirtRegInfo;
78
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.
82   ///
83   std::multimap<MachineInstr*, unsigned> RegistersKilled;
84
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.
89   ///
90   std::multimap<MachineInstr*, unsigned> RegistersDead;
91
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.
95   ///
96   std::vector<bool> AllocatablePhysicalRegisters;
97
98 private:   // Intermediate data structures
99   const MRegisterInfo *RegInfo;
100
101   MachineInstr **PhysRegInfo;
102   bool          *PhysRegUsed;
103
104   void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
105   void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);
106
107 public:
108
109   virtual bool runOnMachineFunction(MachineFunction &MF);
110
111   /// killed_iterator - Iterate over registers killed by a machine instruction
112   ///
113   typedef std::multimap<MachineInstr*, unsigned>::iterator killed_iterator;
114   
115   /// killed_begin/end - Get access to the range of registers killed by a
116   /// machine instruction.
117   killed_iterator killed_begin(MachineInstr *MI) {
118     return RegistersKilled.lower_bound(MI);
119   }
120   killed_iterator killed_end(MachineInstr *MI) {
121     return RegistersKilled.upper_bound(MI);
122   }
123   std::pair<killed_iterator, killed_iterator>
124   killed_range(MachineInstr *MI) {
125     return RegistersKilled.equal_range(MI);
126   }
127
128   /// KillsRegister - Return true if the specified instruction kills the
129   /// specified register.
130   bool KillsRegister(MachineInstr *MI, unsigned Reg) {
131     std::pair<killed_iterator, killed_iterator> KIP = killed_range(MI);
132     for (; KIP.first != KIP.second; ++KIP.first)
133       if (KIP.first->second == Reg)
134         return true;
135     return false;
136   }
137
138   killed_iterator dead_begin(MachineInstr *MI) {
139     return RegistersDead.lower_bound(MI);
140   }
141   killed_iterator dead_end(MachineInstr *MI) {
142     return RegistersDead.upper_bound(MI);
143   }
144   std::pair<killed_iterator, killed_iterator>
145   dead_range(MachineInstr *MI) {
146     return RegistersDead.equal_range(MI);
147   }
148
149   //===--------------------------------------------------------------------===//
150   //  API to update live variable information
151
152   /// instructionChanged - When the address of an instruction changes, this
153   /// method should be called so that live variables can update its internal
154   /// data structures.  This removes the records for OldMI, transfering them to
155   /// the records for NewMI.
156   void instructionChanged(MachineInstr *OldMI, MachineInstr *NewMI);
157
158   /// addVirtualRegisterKilled - Add information about the fact that the
159   /// specified register is killed after being used by the specified
160   /// instruction.
161   ///
162   void addVirtualRegisterKilled(unsigned IncomingReg, MachineInstr *MI) {
163     RegistersKilled.insert(std::make_pair(MI, IncomingReg));
164     getVarInfo(IncomingReg).Kills.push_back(MI);
165   }
166
167   /// removeVirtualRegisterKilled - Remove the specified virtual
168   /// register from the live variable information. Returns true if the
169   /// variable was marked as killed by the specified instruction,
170   /// false otherwise.
171   bool removeVirtualRegisterKilled(unsigned reg,
172                                    MachineBasicBlock *MBB,
173                                    MachineInstr *MI) {
174     if (!getVarInfo(reg).removeKill(MI))
175       return false;
176     for (killed_iterator i = killed_begin(MI), e = killed_end(MI); i != e; ) {
177       if (i->second == reg)
178         RegistersKilled.erase(i++);
179       else
180         ++i;
181     }
182     return true;
183   }
184
185   /// removeVirtualRegistersKilled - Remove all of the specified killed
186   /// registers from the live variable information.
187   void removeVirtualRegistersKilled(killed_iterator B, killed_iterator E) {
188     for (killed_iterator I = B; I != E; ++I) { // Remove VarInfo entries...
189       bool removed = getVarInfo(I->second).removeKill(I->first);
190       assert(removed && "kill not in register's VarInfo?");
191     }
192     RegistersKilled.erase(B, E);
193   }
194
195   /// addVirtualRegisterDead - Add information about the fact that the specified
196   /// register is dead after being used by the specified instruction.
197   ///
198   void addVirtualRegisterDead(unsigned IncomingReg, MachineInstr *MI) {
199     RegistersDead.insert(std::make_pair(MI, IncomingReg));
200     getVarInfo(IncomingReg).Kills.push_back(MI);
201   }
202
203   /// removeVirtualRegisterDead - Remove the specified virtual
204   /// register from the live variable information. Returns true if the
205   /// variable was marked dead at the specified instruction, false
206   /// otherwise.
207   bool removeVirtualRegisterDead(unsigned reg,
208                                  MachineBasicBlock *MBB,
209                                  MachineInstr *MI) {
210     if (!getVarInfo(reg).removeKill(MI))
211       return false;
212
213     for (killed_iterator i = killed_begin(MI), e = killed_end(MI); i != e; ) {
214       if (i->second == reg)
215         RegistersKilled.erase(i++);
216       else
217         ++i;
218     }
219     return true;
220   }
221
222   /// removeVirtualRegistersDead - Remove all of the specified dead
223   /// registers from the live variable information.
224   void removeVirtualRegistersDead(killed_iterator B, killed_iterator E) {
225     for (killed_iterator I = B; I != E; ++I)  // Remove VarInfo entries...
226       getVarInfo(I->second).removeKill(I->first);
227     RegistersDead.erase(B, E);
228   }
229
230   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
231     AU.setPreservesAll();
232   }
233
234   virtual void releaseMemory() {
235     VirtRegInfo.clear();
236     RegistersKilled.clear();
237     RegistersDead.clear();
238   }
239
240   /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
241   /// register.
242   VarInfo &getVarInfo(unsigned RegIdx);
243
244   void MarkVirtRegAliveInBlock(VarInfo &VRInfo, MachineBasicBlock *BB);
245   void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
246                         MachineInstr *MI);
247 };
248
249 } // End llvm namespace
250
251 #endif