b3911ec80b30da94c4efae5ceed9370744fa9a09
[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) const {
131     typedef std::multimap<MachineInstr*, unsigned>::const_iterator cki;
132     std::pair<cki, cki> KIP = RegistersKilled.equal_range(MI);
133     for (; KIP.first != KIP.second; ++KIP.first)
134       if (KIP.first->second == Reg)
135         return true;
136     return false;
137   }
138
139   killed_iterator dead_begin(MachineInstr *MI) {
140     return RegistersDead.lower_bound(MI);
141   }
142   killed_iterator dead_end(MachineInstr *MI) {
143     return RegistersDead.upper_bound(MI);
144   }
145   std::pair<killed_iterator, killed_iterator>
146   dead_range(MachineInstr *MI) {
147     return RegistersDead.equal_range(MI);
148   }
149   
150   /// RegisterDefIsDead - Return true if the specified instruction defines the
151   /// specified register, but that definition is dead.
152   bool RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const {
153     typedef std::multimap<MachineInstr*, unsigned>::const_iterator cki;
154     std::pair<cki, cki> KIP = RegistersDead.equal_range(MI);
155     for (; KIP.first != KIP.second; ++KIP.first)
156       if (KIP.first->second == Reg)
157         return true;
158     return false;
159   }
160
161   //===--------------------------------------------------------------------===//
162   //  API to update live variable information
163
164   /// instructionChanged - When the address of an instruction changes, this
165   /// method should be called so that live variables can update its internal
166   /// data structures.  This removes the records for OldMI, transfering them to
167   /// the records for NewMI.
168   void instructionChanged(MachineInstr *OldMI, MachineInstr *NewMI);
169
170   /// addVirtualRegisterKilled - Add information about the fact that the
171   /// specified register is killed after being used by the specified
172   /// instruction.
173   ///
174   void addVirtualRegisterKilled(unsigned IncomingReg, MachineInstr *MI) {
175     RegistersKilled.insert(std::make_pair(MI, IncomingReg));
176     getVarInfo(IncomingReg).Kills.push_back(MI);
177   }
178
179   /// removeVirtualRegisterKilled - Remove the specified virtual
180   /// register from the live variable information. Returns true if the
181   /// variable was marked as killed by the specified instruction,
182   /// false otherwise.
183   bool removeVirtualRegisterKilled(unsigned reg,
184                                    MachineBasicBlock *MBB,
185                                    MachineInstr *MI) {
186     if (!getVarInfo(reg).removeKill(MI))
187       return false;
188     for (killed_iterator i = killed_begin(MI), e = killed_end(MI); i != e; ) {
189       if (i->second == reg)
190         RegistersKilled.erase(i++);
191       else
192         ++i;
193     }
194     return true;
195   }
196
197   /// removeVirtualRegistersKilled - Remove all of the specified killed
198   /// registers from the live variable information.
199   void removeVirtualRegistersKilled(killed_iterator B, killed_iterator E) {
200     for (killed_iterator I = B; I != E; ++I) { // Remove VarInfo entries...
201       bool removed = getVarInfo(I->second).removeKill(I->first);
202       assert(removed && "kill not in register's VarInfo?");
203     }
204     RegistersKilled.erase(B, E);
205   }
206
207   /// addVirtualRegisterDead - Add information about the fact that the specified
208   /// register is dead after being used by the specified instruction.
209   ///
210   void addVirtualRegisterDead(unsigned IncomingReg, MachineInstr *MI) {
211     RegistersDead.insert(std::make_pair(MI, IncomingReg));
212     getVarInfo(IncomingReg).Kills.push_back(MI);
213   }
214
215   /// removeVirtualRegisterDead - Remove the specified virtual
216   /// register from the live variable information. Returns true if the
217   /// variable was marked dead at the specified instruction, false
218   /// otherwise.
219   bool removeVirtualRegisterDead(unsigned reg,
220                                  MachineBasicBlock *MBB,
221                                  MachineInstr *MI) {
222     if (!getVarInfo(reg).removeKill(MI))
223       return false;
224
225     for (killed_iterator i = killed_begin(MI), e = killed_end(MI); i != e; ) {
226       if (i->second == reg)
227         RegistersKilled.erase(i++);
228       else
229         ++i;
230     }
231     return true;
232   }
233
234   /// removeVirtualRegistersDead - Remove all of the specified dead
235   /// registers from the live variable information.
236   void removeVirtualRegistersDead(killed_iterator B, killed_iterator E) {
237     for (killed_iterator I = B; I != E; ++I)  // Remove VarInfo entries...
238       getVarInfo(I->second).removeKill(I->first);
239     RegistersDead.erase(B, E);
240   }
241
242   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
243     AU.setPreservesAll();
244   }
245
246   virtual void releaseMemory() {
247     VirtRegInfo.clear();
248     RegistersKilled.clear();
249     RegistersDead.clear();
250   }
251
252   /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
253   /// register.
254   VarInfo &getVarInfo(unsigned RegIdx);
255
256   void MarkVirtRegAliveInBlock(VarInfo &VRInfo, MachineBasicBlock *BB);
257   void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
258                         MachineInstr *MI);
259 };
260
261 } // End llvm namespace
262
263 #endif