6c58815e27326e23ab0986ad0ba1e57f323fdc9f
[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 "llvm/ADT/BitVector.h"
34 #include <map>
35
36 namespace llvm {
37
38 class MRegisterInfo;
39 class BitVector;
40
41 class LiveVariables : public MachineFunctionPass {
42 public:
43   /// VarInfo - This represents the regions where a virtual register is live in
44   /// the program.  We represent this with three different pieces of
45   /// information: the instruction that uniquely defines the value, the set of
46   /// blocks the instruction is live into and live out of, and the set of 
47   /// non-phi instructions that are the last users of the value.
48   ///
49   /// In the common case where a value is defined and killed in the same block,
50   /// DefInst is the defining inst, there is one killing instruction, and 
51   /// AliveBlocks is empty.
52   ///
53   /// Otherwise, the value is live out of the block.  If the value is live
54   /// across any blocks, these blocks are listed in AliveBlocks.  Blocks where
55   /// the liveness range ends are not included in AliveBlocks, instead being
56   /// captured by the Kills set.  In these blocks, the value is live into the
57   /// block (unless the value is defined and killed in the same block) and lives
58   /// until the specified instruction.  Note that there cannot ever be a value
59   /// whose Kills set contains two instructions from the same basic block.
60   ///
61   /// PHI nodes complicate things a bit.  If a PHI node is the last user of a
62   /// value in one of its predecessor blocks, it is not listed in the kills set,
63   /// but does include the predecessor block in the AliveBlocks set (unless that
64   /// block also defines the value).  This leads to the (perfectly sensical)
65   /// situation where a value is defined in a block, and the last use is a phi
66   /// node in the successor.  In this case, DefInst will be the defining
67   /// instruction, AliveBlocks is empty (the value is not live across any 
68   /// blocks) and Kills is empty (phi nodes are not included).  This is sensical
69   /// because the value must be live to the end of the block, but is not live in
70   /// any successor blocks.
71   struct VarInfo {
72     /// DefInst - The machine instruction that defines this register.
73     ///
74     MachineInstr *DefInst;
75
76     /// AliveBlocks - Set of blocks of which this value is alive completely
77     /// through.  This is a bit set which uses the basic block number as an
78     /// index.
79     ///
80     BitVector AliveBlocks;
81
82     /// UsedBlocks - Set of blocks of which this value is actually used. This
83     /// is a bit set which uses the basic block number as an index.
84     BitVector UsedBlocks;
85
86     /// NumUses - Number of uses of this register across the entire function.
87     ///
88     unsigned NumUses;
89
90     /// Kills - List of MachineInstruction's which are the last use of this
91     /// virtual register (kill it) in their basic block.
92     ///
93     std::vector<MachineInstr*> Kills;
94
95     VarInfo() : DefInst(0), NumUses(0) {}
96
97     /// removeKill - Delete a kill corresponding to the specified
98     /// machine instruction. Returns true if there was a kill
99     /// corresponding to this instruction, false otherwise.
100     bool removeKill(MachineInstr *MI) {
101       for (std::vector<MachineInstr*>::iterator i = Kills.begin(),
102              e = Kills.end(); i != e; ++i)
103         if (*i == MI) {
104           Kills.erase(i);
105           return true;
106         }
107       return false;
108     }
109     
110     void dump() const;
111   };
112
113 private:
114   /// VirtRegInfo - This list is a mapping from virtual register number to
115   /// variable information.  FirstVirtualRegister is subtracted from the virtual
116   /// register number before indexing into this list.
117   ///
118   std::vector<VarInfo> VirtRegInfo;
119
120   /// ReservedRegisters - This vector keeps track of which registers
121   /// are reserved register which are not allocatable by the target machine.
122   /// We can not track liveness for values that are in this set.
123   ///
124   BitVector ReservedRegisters;
125
126 private:   // Intermediate data structures
127   MachineFunction *MF;
128
129   const MRegisterInfo *RegInfo;
130
131   MachineInstr **PhysRegInfo;
132   bool          *PhysRegUsed;
133
134   typedef std::map<const MachineBasicBlock*,
135                    std::vector<unsigned> > PHIVarInfoMap;
136
137   PHIVarInfoMap PHIVarInfo;
138
139
140   /// addRegisterKilled - We have determined MI kills a register. Look for the
141   /// operand that uses it and mark it as IsKill.
142   void addRegisterKilled(unsigned IncomingReg, MachineInstr *MI);
143
144   /// addRegisterDead - We have determined MI defined a register without a use.
145   /// Look for the operand that defines it and mark it as IsDead. 
146   void addRegisterDead(unsigned IncomingReg, MachineInstr *MI);
147
148   void HandlePhysRegUse(unsigned Reg, MachineInstr *MI);
149   void HandlePhysRegDef(unsigned Reg, MachineInstr *MI);
150
151   /// analyzePHINodes - Gather information about the PHI nodes in here. In
152   /// particular, we want to map the variable information of a virtual
153   /// register which is used in a PHI node. We map that to the BB the vreg
154   /// is coming from.
155   void analyzePHINodes(const MachineFunction& Fn);
156 public:
157
158   virtual bool runOnMachineFunction(MachineFunction &MF);
159
160   /// KillsRegister - Return true if the specified instruction kills the
161   /// specified register.
162   bool KillsRegister(MachineInstr *MI, unsigned Reg) const;
163   
164   /// RegisterDefIsDead - Return true if the specified instruction defines the
165   /// specified register, but that definition is dead.
166   bool RegisterDefIsDead(MachineInstr *MI, unsigned Reg) const;
167
168   /// ModifiesRegister - Return true if the specified instruction modifies the
169   /// specified register.
170   bool ModifiesRegister(MachineInstr *MI, unsigned Reg) const;
171   
172   //===--------------------------------------------------------------------===//
173   //  API to update live variable information
174
175   /// instructionChanged - When the address of an instruction changes, this
176   /// method should be called so that live variables can update its internal
177   /// data structures.  This removes the records for OldMI, transfering them to
178   /// the records for NewMI.
179   void instructionChanged(MachineInstr *OldMI, MachineInstr *NewMI);
180
181   /// addVirtualRegisterKilled - Add information about the fact that the
182   /// specified register is killed after being used by the specified
183   /// instruction.
184   ///
185   void addVirtualRegisterKilled(unsigned IncomingReg, MachineInstr *MI) {
186     addRegisterKilled(IncomingReg, MI);
187     getVarInfo(IncomingReg).Kills.push_back(MI); 
188  }
189
190   /// removeVirtualRegisterKilled - Remove the specified virtual
191   /// register from the live variable information. Returns true if the
192   /// variable was marked as killed by the specified instruction,
193   /// false otherwise.
194   bool removeVirtualRegisterKilled(unsigned reg,
195                                    MachineBasicBlock *MBB,
196                                    MachineInstr *MI) {
197     if (!getVarInfo(reg).removeKill(MI))
198       return false;
199
200     bool Removed = false;
201     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
202       MachineOperand &MO = MI->getOperand(i);
203       if (MO.isReg() && MO.isUse() && MO.getReg() == reg) {
204         MO.unsetIsKill();
205         Removed = true;
206         break;
207       }
208     }
209
210     assert(Removed && "Register is not used by this instruction!");
211     return true;
212   }
213
214   /// removeVirtualRegistersKilled - Remove all killed info for the specified
215   /// instruction.
216   void removeVirtualRegistersKilled(MachineInstr *MI);
217   
218   /// addVirtualRegisterDead - Add information about the fact that the specified
219   /// register is dead after being used by the specified instruction.
220   ///
221   void addVirtualRegisterDead(unsigned IncomingReg, MachineInstr *MI) {
222     addRegisterDead(IncomingReg, MI);
223     getVarInfo(IncomingReg).Kills.push_back(MI);
224   }
225
226   /// removeVirtualRegisterDead - Remove the specified virtual
227   /// register from the live variable information. Returns true if the
228   /// variable was marked dead at the specified instruction, false
229   /// otherwise.
230   bool removeVirtualRegisterDead(unsigned reg,
231                                  MachineBasicBlock *MBB,
232                                  MachineInstr *MI) {
233     if (!getVarInfo(reg).removeKill(MI))
234       return false;
235
236     bool Removed = false;
237     for (unsigned i = 0, e = MI->getNumOperands(); i != e; ++i) {
238       MachineOperand &MO = MI->getOperand(i);
239       if (MO.isReg() && MO.isDef() && MO.getReg() == reg) {
240         MO.unsetIsDead();
241         Removed = true;
242         break;
243       }
244     }
245     assert(Removed && "Register is not defined by this instruction!");
246     return true;
247   }
248
249   /// removeVirtualRegistersDead - Remove all of the dead registers for the
250   /// specified instruction from the live variable information.
251   void removeVirtualRegistersDead(MachineInstr *MI);
252   
253   virtual void getAnalysisUsage(AnalysisUsage &AU) const {
254     AU.setPreservesAll();
255   }
256
257   virtual void releaseMemory() {
258     VirtRegInfo.clear();
259   }
260
261   /// getVarInfo - Return the VarInfo structure for the specified VIRTUAL
262   /// register.
263   VarInfo &getVarInfo(unsigned RegIdx);
264
265   void MarkVirtRegAliveInBlock(VarInfo &VRInfo, MachineBasicBlock *BB);
266   void HandleVirtRegUse(VarInfo &VRInfo, MachineBasicBlock *MBB,
267                         MachineInstr *MI);
268 };
269
270 } // End llvm namespace
271
272 #endif