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