3a0d044c59c29d5a47fabc2d2123c2e5ea1f21a8
[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 machine instr
61     void removeKill(MachineInstr *MI) {
62       for (unsigned i = 0; ; ++i) {
63         assert(i < Kills.size() && "Machine instr is not a kill!");
64         if (Kills[i].second == MI) {
65           Kills.erase(Kills.begin()+i);
66           return;
67         }
68       }
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
100   /// BBMap - Maps LLVM basic blocks to their corresponding machine basic block.
101   /// This also provides a numbering of the basic blocks in the function.
102   std::map<const BasicBlock*, std::pair<MachineBasicBlock*, unsigned> > BBMap;
103   
104   const MRegisterInfo *RegInfo;
105
106   MachineInstr **PhysRegInfo;
107   bool          *PhysRegUsed;
108
109 public:
110
111   virtual bool runOnMachineFunction(MachineFunction &MF);
112
113   /// getMachineBasicBlockIndex - Turn a MachineBasicBlock into an index number
114   /// suitable for use with VarInfo's.
115   ///
116   const std::pair<MachineBasicBlock*, unsigned>
117       &getMachineBasicBlockInfo(MachineBasicBlock *MBB) const;
118   const std::pair<MachineBasicBlock*, unsigned>
119       &getBasicBlockInfo(const BasicBlock *BB) const {
120     return BBMap.find(BB)->second;
121   }
122
123
124   /// killed_iterator - Iterate over registers killed by a machine instruction
125   ///
126   typedef std::multimap<MachineInstr*, unsigned>::iterator killed_iterator;
127   
128   /// killed_begin/end - Get access to the range of registers killed by a
129   /// machine instruction.
130   killed_iterator killed_begin(MachineInstr *MI) {
131     return RegistersKilled.lower_bound(MI);
132   }
133   killed_iterator killed_end(MachineInstr *MI) {
134     return RegistersKilled.upper_bound(MI);
135   }
136   std::pair<killed_iterator, killed_iterator>
137   killed_range(MachineInstr *MI) {
138     return RegistersKilled.equal_range(MI);
139   }
140
141   killed_iterator dead_begin(MachineInstr *MI) {
142     return RegistersDead.lower_bound(MI);
143   }
144   killed_iterator dead_end(MachineInstr *MI) {
145     return RegistersDead.upper_bound(MI);
146   }
147   std::pair<killed_iterator, killed_iterator>
148   dead_range(MachineInstr *MI) {
149     return RegistersDead.equal_range(MI);
150   }
151
152   //===--------------------------------------------------------------------===//
153   //  API to update live variable information
154
155   /// addVirtualRegisterKilled - Add information about the fact that the
156   /// specified register is killed after being used by the specified
157   /// instruction.
158   ///
159   void addVirtualRegisterKilled(unsigned IncomingReg, MachineBasicBlock *MBB,
160                                 MachineInstr *MI) {
161     RegistersKilled.insert(std::make_pair(MI, IncomingReg));
162     getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
163   }
164
165   /// removeVirtualRegistersKilled - Remove all of the specified killed
166   /// registers from the live variable information.
167   void removeVirtualRegistersKilled(killed_iterator B, killed_iterator E) {
168     for (killed_iterator I = B; I != E; ++I)  // Remove VarInfo entries...
169       getVarInfo(I->second).removeKill(I->first);
170     RegistersKilled.erase(B, E);
171   }
172
173   /// addVirtualRegisterDead - Add information about the fact that the specified
174   /// register is dead after being used by the specified instruction.
175   ///
176   void addVirtualRegisterDead(unsigned IncomingReg, MachineBasicBlock *MBB,
177                               MachineInstr *MI) {
178     RegistersDead.insert(std::make_pair(MI, IncomingReg));
179     getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
180   }
181
182   /// removeVirtualRegistersKilled - Remove all of the specified killed
183   /// registers from the live variable information.
184   void removeVirtualRegistersDead(killed_iterator B, killed_iterator E) {
185     for (killed_iterator I = B; I != E; ++I)  // Remove VarInfo entries...
186       getVarInfo(I->second).removeKill(I->first);
187     RegistersDead.erase(B, E);
188   }
189
190   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
191     AU.setPreservesAll();
192   }
193
194   virtual void releaseMemory() {
195     VirtRegInfo.clear();
196     RegistersKilled.clear();
197     RegistersDead.clear();
198     BBMap.clear();
199   }
200
201   /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
202   /// register.
203   VarInfo &getVarInfo(unsigned RegIdx);
204
205   void MarkVirtRegAliveInBlock(VarInfo &VRInfo, const BasicBlock *BB);
206   void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
207                         MachineInstr *MI);
208   void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
209   void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);
210 };
211
212 } // End llvm namespace
213
214 #endif