Added LLVM copyright header (for lack of a better term).
[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 class MRegisterInfo;
36
37 class LiveVariables : public MachineFunctionPass {
38 public:
39   struct VarInfo {
40     /// DefBlock - The basic block which defines this value...
41     MachineBasicBlock *DefBlock;
42     MachineInstr      *DefInst;
43
44     /// AliveBlocks - Set of blocks of which this value is alive completely
45     /// through.  This is a bit set which uses the basic block number as an
46     /// index.
47     ///
48     std::vector<bool> AliveBlocks;
49
50     /// Kills - List of MachineBasicblock's which contain the last use of this
51     /// virtual register (kill it).  This also includes the specific instruction
52     /// which kills the value.
53     ///
54     std::vector<std::pair<MachineBasicBlock*, MachineInstr*> > Kills;
55
56     VarInfo() : DefBlock(0), DefInst(0) {}
57
58     /// removeKill - Delete a kill corresponding to the specified machine instr
59     void removeKill(MachineInstr *MI) {
60       for (unsigned i = 0; ; ++i) {
61         assert(i < Kills.size() && "Machine instr is not a kill!");
62         if (Kills[i].second == MI) {
63           Kills.erase(Kills.begin()+i);
64           return;
65         }
66       }
67     }
68   };
69
70 private:
71   /// VirtRegInfo - This list is a mapping from virtual register number to
72   /// variable information.  FirstVirtualRegister is subtracted from the virtual
73   /// register number before indexing into this list.
74   ///
75   std::vector<VarInfo> VirtRegInfo;
76
77   /// RegistersKilled - This multimap keeps track of all of the registers that
78   /// are dead immediately after an instruction reads its operands.  If an
79   /// instruction does not have an entry in this map, it kills no registers.
80   ///
81   std::multimap<MachineInstr*, unsigned> RegistersKilled;
82
83   /// RegistersDead - This multimap keeps track of all of the registers that are
84   /// dead immediately after an instruction executes, which are not dead after
85   /// the operands are evaluated.  In practice, this only contains registers
86   /// which are defined by an instruction, but never used.
87   ///
88   std::multimap<MachineInstr*, unsigned> RegistersDead;
89
90   /// AllocatablePhysicalRegisters - This vector keeps track of which registers
91   /// are actually register allocatable by the target machine.  We can not track
92   /// liveness for values that are not in this set.
93   ///
94   std::vector<bool> AllocatablePhysicalRegisters;
95
96 private:   // Intermediate data structures
97
98   /// BBMap - Maps LLVM basic blocks to their corresponding machine basic block.
99   /// This also provides a numbering of the basic blocks in the function.
100   std::map<const BasicBlock*, std::pair<MachineBasicBlock*, unsigned> > BBMap;
101   
102   const MRegisterInfo *RegInfo;
103
104   MachineInstr **PhysRegInfo;
105   bool          *PhysRegUsed;
106
107 public:
108
109   virtual bool runOnMachineFunction(MachineFunction &MF);
110
111   /// getMachineBasicBlockIndex - Turn a MachineBasicBlock into an index number
112   /// suitable for use with VarInfo's.
113   ///
114   const std::pair<MachineBasicBlock*, unsigned>
115       &getMachineBasicBlockInfo(MachineBasicBlock *MBB) const;
116   const std::pair<MachineBasicBlock*, unsigned>
117       &getBasicBlockInfo(const BasicBlock *BB) const {
118     return BBMap.find(BB)->second;
119   }
120
121
122   /// killed_iterator - Iterate over registers killed by a machine instruction
123   ///
124   typedef std::multimap<MachineInstr*, unsigned>::iterator killed_iterator;
125   
126   /// killed_begin/end - Get access to the range of registers killed by a
127   /// machine instruction.
128   killed_iterator killed_begin(MachineInstr *MI) {
129     return RegistersKilled.lower_bound(MI);
130   }
131   killed_iterator killed_end(MachineInstr *MI) {
132     return RegistersKilled.upper_bound(MI);
133   }
134   std::pair<killed_iterator, killed_iterator>
135   killed_range(MachineInstr *MI) {
136     return RegistersKilled.equal_range(MI);
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   //===--------------------------------------------------------------------===//
151   //  API to update live variable information
152
153   /// addVirtualRegisterKilled - Add information about the fact that the
154   /// specified register is killed after being used by the specified
155   /// instruction.
156   ///
157   void addVirtualRegisterKilled(unsigned IncomingReg, MachineBasicBlock *MBB,
158                                 MachineInstr *MI) {
159     RegistersKilled.insert(std::make_pair(MI, IncomingReg));
160     getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
161   }
162
163   /// removeVirtualRegistersKilled - Remove all of the specified killed
164   /// registers from the live variable information.
165   void removeVirtualRegistersKilled(killed_iterator B, killed_iterator E) {
166     for (killed_iterator I = B; I != E; ++I)  // Remove VarInfo entries...
167       getVarInfo(I->second).removeKill(I->first);
168     RegistersKilled.erase(B, E);
169   }
170
171   /// addVirtualRegisterDead - Add information about the fact that the specified
172   /// register is dead after being used by the specified instruction.
173   ///
174   void addVirtualRegisterDead(unsigned IncomingReg, MachineBasicBlock *MBB,
175                               MachineInstr *MI) {
176     RegistersDead.insert(std::make_pair(MI, IncomingReg));
177     getVarInfo(IncomingReg).Kills.push_back(std::make_pair(MBB, MI));
178   }
179
180   /// removeVirtualRegistersKilled - Remove all of the specified killed
181   /// registers from the live variable information.
182   void removeVirtualRegistersDead(killed_iterator B, killed_iterator E) {
183     for (killed_iterator I = B; I != E; ++I)  // Remove VarInfo entries...
184       getVarInfo(I->second).removeKill(I->first);
185     RegistersDead.erase(B, E);
186   }
187
188   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
189     AU.setPreservesAll();
190   }
191
192   virtual void releaseMemory() {
193     VirtRegInfo.clear();
194     RegistersKilled.clear();
195     RegistersDead.clear();
196     BBMap.clear();
197   }
198
199   /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
200   /// register.
201   VarInfo &getVarInfo(unsigned RegIdx);
202
203   void MarkVirtRegAliveInBlock(VarInfo &VRInfo, const BasicBlock *BB);
204   void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
205                         MachineInstr *MI);
206   void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
207   void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);
208 };
209
210 #endif