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