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