* Add #includes that were yanked out of header files
[oota-llvm.git] / lib / Analysis / LiveVar / FunctionLiveVarInfo.cpp
1 /* Title:   MethodLiveVarInfo.cpp
2    Author:  Ruchira Sasanka
3    Date:    Jun 30, 01
4    Purpose: 
5
6    This is the interface for live variable info of a method that is required 
7    by any other part of the compiler.
8
9 */
10
11
12 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
13 #include "llvm/CodeGen/MachineInstr.h"
14 #include "llvm/BasicBlock.h"
15 #include "Support/PostOrderIterator.h"
16 #include <iostream>
17 using std::cout;
18 using std::endl;
19
20 //************************** Constructor/Destructor ***************************
21
22
23 MethodLiveVarInfo::MethodLiveVarInfo(const Method *const M) : Meth(M) {
24   assert(!M->isExternal() && "Cannot be a prototype declaration");
25   HasAnalyzed = false;                  // still we haven't called analyze()
26 }
27
28
29
30 MethodLiveVarInfo:: ~MethodLiveVarInfo()
31 {
32   // First delete all BBLiveVar objects created in constructBBs(). A new object
33   // of type  BBLiveVa is created for every BasicBlock in the method
34
35   // hash map iterator for BB2BBLVMap
36   //
37   BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin(); 
38
39   for( ; HMI != BB2BBLVMap.end() ; HMI ++ ) {  
40     if( (*HMI).first )                // delete all BBLiveVar in BB2BBLVMap
41       delete (*HMI).second;
42    }
43
44
45   // Then delete all objects of type LiveVarSet created in calcLiveVarSetsForBB
46   // and entered into  MInst2LVSetBI and  MInst2LVSetAI (these are caches
47   // to return LiveVarSet's before/after a machine instruction quickly). It
48   // is sufficient to free up all LiveVarSet using only one cache since 
49   // both caches refer to the same sets
50
51   // hash map iterator for MInst2LVSetBI
52   //
53   MInstToLiveVarSetMapType::iterator MI =  MInst2LVSetBI.begin(); 
54
55   for( ; MI !=  MInst2LVSetBI.end() ; MI ++ ) {  
56     if( (*MI).first )              // delete all LiveVarSets in  MInst2LVSetBI
57       delete (*MI).second;
58    }
59 }
60
61
62 // ************************* support functions ********************************
63
64
65 //-----------------------------------------------------------------------------
66 // constructs BBLiveVars and init Def and In sets
67 //-----------------------------------------------------------------------------
68
69 void MethodLiveVarInfo::constructBBs()   
70 {
71   unsigned int POId = 0;                // Reverse Depth-first Order ID
72
73   po_iterator<const Method*> BBI = po_begin(Meth);
74
75   for(  ; BBI != po_end(Meth) ; ++BBI, ++POId) 
76   { 
77
78     const BasicBlock *BB = *BBI;        // get the current BB 
79
80     if(DEBUG_LV) { cout << " For BB "; printValue(BB); cout << ":" << endl; }
81
82                                         // create a new BBLiveVar
83     BBLiveVar * LVBB = new BBLiveVar( BB, POId );  
84     
85     BB2BBLVMap[ BB ] = LVBB;            // insert the pair to Map
86     
87     LVBB->calcDefUseSets();             // calculates the def and in set
88
89     if(DEBUG_LV) 
90       LVBB->printAllSets();
91   }
92
93   // Since the PO iterator does not discover unreachable blocks,
94   // go over the random iterator and init those blocks as well.
95   // However, LV info is not correct for those blocks (they are not
96   // analyzed)
97
98   Method::const_iterator BBRI = Meth->begin();  // random iterator for BBs   
99
100   for( ; BBRI != Meth->end(); ++BBRI, ++POId) {     
101
102     if(   ! BB2BBLVMap[ *BBRI ] )
103       BB2BBLVMap[ *BBRI ] = new BBLiveVar( *BBRI, POId );
104
105   }
106
107
108 }
109
110
111 //-----------------------------------------------------------------------------
112 // do one backward pass over the CFG (for iterative analysis)
113 //-----------------------------------------------------------------------------
114 bool MethodLiveVarInfo::doSingleBackwardPass()  
115 {
116   bool ResultFlow, NeedAnotherIteration = false;
117
118   if(DEBUG_LV) 
119     cout << endl <<  " After Backward Pass ..." << endl;
120
121   po_iterator<const Method*> BBI = po_begin(Meth);
122
123   for( ; BBI != po_end(Meth) ; ++BBI) 
124   { 
125
126     BBLiveVar* LVBB = BB2BBLVMap[*BBI];
127     assert( LVBB );
128
129     if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":"  << endl;
130     // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
131
132     ResultFlow = false;
133
134     if( LVBB->isOutSetChanged() ) 
135       LVBB->applyTransferFunc();        // apply the Tran Func to calc InSet
136
137     if( LVBB->isInSetChanged() )        // to calc Outsets of preds
138       ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap); 
139
140     if(DEBUG_LV) LVBB->printInOutSets();
141
142
143     if( ResultFlow ) NeedAnotherIteration = true;
144
145   }
146
147   // true if we need to reiterate over the CFG
148   return NeedAnotherIteration;         
149 }
150
151
152
153
154 //-----------------------------------------------------------------------------
155 // performs live var anal for a method
156 //-----------------------------------------------------------------------------
157 void MethodLiveVarInfo::analyze()        
158 {
159   // Don't analyze the same method twice!
160   // Later, we need to add change notification here.
161
162   
163   if (HasAnalyzed)
164     return;
165     
166
167   if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
168
169   // create and initialize all the BBLiveVars of the CFG
170   constructBBs();        
171
172   bool NeedAnotherIteration = false;
173   do {                                // do one  pass over  CFG
174     NeedAnotherIteration = doSingleBackwardPass( );   
175   } while (NeedAnotherIteration );    // repeat until we need more iterations
176
177   
178   HasAnalyzed  = true;                // finished analysing
179
180   if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
181 }
182
183
184
185 //-----------------------------------------------------------------------------
186 /* Following functions will give the LiveVar info for any machine instr in
187    a method. It should be called after a call to analyze().
188
189    Thsese functions calucluates live var info for all the machine instrs in a 
190    BB when LVInfo for one inst is requested. Hence, this function is useful 
191    when live var info is required for many (or all) instructions in a basic 
192    block. Also, the arguments to this method does not require specific 
193    iterators.
194 */
195 //-----------------------------------------------------------------------------
196
197 //-----------------------------------------------------------------------------
198 // Gives live variable information before a machine instruction
199 //-----------------------------------------------------------------------------
200 const LiveVarSet * 
201 MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
202                                             const BasicBlock *const CurBB) 
203 {
204   const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
205
206   if( LVSet  ) return LVSet;              // if found, just return the set
207   else { 
208     calcLiveVarSetsForBB( CurBB );        // else, calc for all instrs in BB
209
210     /*if(  ! MInst2LVSetBI[ MInst ] ) {
211       cerr << "\nFor BB "; printValue( CurBB);
212       cerr << "\nRequested LVSet for inst: " << *MInst;
213       }*/
214
215     assert( MInst2LVSetBI[ MInst ] );
216     return  MInst2LVSetBI[ MInst ];
217   }
218 }
219
220
221 //-----------------------------------------------------------------------------
222 // Gives live variable information after a machine instruction
223 //-----------------------------------------------------------------------------
224 const LiveVarSet * 
225 MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
226                                             const BasicBlock *const CurBB) 
227 {
228   const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
229
230   if( LVSet  ) return LVSet;              // if found, just return the set
231   else { 
232     calcLiveVarSetsForBB( CurBB );        // else, calc for all instrs in BB
233     assert( MInst2LVSetAI[ MInst ] );
234     return  MInst2LVSetAI[ MInst ];
235   }
236 }
237
238
239
240 //-----------------------------------------------------------------------------
241 // This method calculates the live variable information for all the 
242 // instructions in a basic block and enter the newly constructed live
243 // variable sets into a the caches ( MInst2LVSetAI,  MInst2LVSetBI)
244 //-----------------------------------------------------------------------------
245 void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
246 {
247   const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
248   MachineCodeForBasicBlock::const_reverse_iterator 
249     MInstIterator = MIVec.rbegin();
250
251   LiveVarSet *CurSet = new LiveVarSet();
252   const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
253   CurSet->setUnion(SetAI);                     // CurSet now contains OutSet
254
255   // iterate over all the machine instructions in BB
256   for( ; MInstIterator != MIVec.rend(); MInstIterator++) {  
257
258     // MInst is cur machine inst
259     const MachineInstr * MInst  = *MInstIterator;  
260
261     MInst2LVSetAI[MInst] = SetAI;              // record in After Inst map
262     
263     CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
264     LiveVarSet *NewSet = new LiveVarSet();     // create a new set and
265     NewSet->setUnion( CurSet );                // copy the set after T/F to it
266  
267     MInst2LVSetBI[MInst] = NewSet;             // record in Before Inst map
268
269     // SetAI will be used in the next iteration
270     SetAI = NewSet;                 
271   }
272   
273 }
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290