blob: e981a86642086204c872e2056e0b6a42da4282ff [file] [log] [blame]
Ruchira Sasankae27c3442001-08-20 21:12:49 +00001/* Title: MethodLiveVarInfo.cpp
Ruchira Sasanka683847f2001-07-24 17:14:13 +00002 Author: Ruchira Sasanka
3 Date: Jun 30, 01
4 Purpose:
5
Ruchira Sasankae27c3442001-08-20 21:12:49 +00006 This is the interface for live variable info of a method that is required
7 by any other part of the compiler.
Ruchira Sasanka683847f2001-07-24 17:14:13 +00008
9*/
10
11
12#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
Ruchira Sasankae27c3442001-08-20 21:12:49 +000013#include "llvm/CodeGen/MachineInstr.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000014#include "Support/PostOrderIterator.h"
Ruchira Sasanka683847f2001-07-24 17:14:13 +000015
16
17/************************** Constructor/Destructor ***************************/
18
19
Ruchira Sasankae27c3442001-08-20 21:12:49 +000020MethodLiveVarInfo::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()
Ruchira Sasanka683847f2001-07-24 17:14:13 +000025}
26
27
28
29MethodLiveVarInfo:: ~MethodLiveVarInfo()
30{
Ruchira Sasankae27c3442001-08-20 21:12:49 +000031 // hash map iterator
32 BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000033
34 for( ; HMI != BB2BBLVMap.end() ; HMI ++ ) {
Ruchira Sasankae27c3442001-08-20 21:12:49 +000035 if( (*HMI).first ) // delete all LiveVarSets in BB2BBLVMap
Ruchira Sasanka683847f2001-07-24 17:14:13 +000036 delete (*HMI).second;
37 }
38}
39
40
41// -------------------------- support functions -------------------------------
42
43
44
Ruchira Sasankae27c3442001-08-20 21:12:49 +000045// constructs BBLiveVars and init Def and In sets
Ruchira Sasanka683847f2001-07-24 17:14:13 +000046void MethodLiveVarInfo::constructBBs()
47{
Ruchira Sasankae27c3442001-08-20 21:12:49 +000048 unsigned int POId = 0; // Reverse Depth-first Order ID
Ruchira Sasanka683847f2001-07-24 17:14:13 +000049
Chris Lattner3ff43872001-09-28 22:56:31 +000050 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000051
Chris Lattner3ff43872001-09-28 22:56:31 +000052 for( ; BBI != po_end(Meth) ; ++BBI, ++POId)
Ruchira Sasanka683847f2001-07-24 17:14:13 +000053 {
54
Ruchira Sasankae27c3442001-08-20 21:12:49 +000055 const BasicBlock *BB = *BBI; // get the current BB
Ruchira Sasankaaca997c2001-09-30 23:28:04 +000056
Ruchira Sasanka92e251c2001-10-16 01:25:05 +000057 if(DEBUG_LV) { cout << " For BB "; printValue(BB); cout << ":" << endl; }
Ruchira Sasankaaca997c2001-09-30 23:28:04 +000058
Ruchira Sasankae27c3442001-08-20 21:12:49 +000059 // create a new BBLiveVar
60 BBLiveVar * LVBB = new BBLiveVar( BB, POId );
Ruchira Sasanka683847f2001-07-24 17:14:13 +000061
Ruchira Sasankae27c3442001-08-20 21:12:49 +000062 BB2BBLVMap[ BB ] = LVBB; // insert the pair to Map
Ruchira Sasanka683847f2001-07-24 17:14:13 +000063
Ruchira Sasankae27c3442001-08-20 21:12:49 +000064 LVBB->calcDefUseSets(); // calculates the def and in set
Ruchira Sasanka683847f2001-07-24 17:14:13 +000065
Ruchira Sasankae27c3442001-08-20 21:12:49 +000066 if(DEBUG_LV)
67 LVBB->printAllSets();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000068 }
Ruchira Sasankac1daae82001-10-12 17:47:23 +000069
70 // Since the PO iterator does not discover unreachable blocks,
71 // go over the random iterator and init those blocks as well.
72 // However, LV info is not correct for those blocks (they are not
73 // analyzed)
74
75 Method::const_iterator BBRI = Meth->begin(); // random iterator for BBs
76
77 for( ; BBRI != Meth->end(); ++BBRI, ++POId) {
78
79 if( ! BB2BBLVMap[ *BBRI ] )
80 BB2BBLVMap[ *BBRI ] = new BBLiveVar( *BBRI, POId );
81
82 }
83
84
Ruchira Sasanka683847f2001-07-24 17:14:13 +000085}
86
Ruchira Sasankae27c3442001-08-20 21:12:49 +000087
88
89// do one backward pass over the CFG
Ruchira Sasanka683847f2001-07-24 17:14:13 +000090bool MethodLiveVarInfo::doSingleBackwardPass()
91{
92 bool ResultFlow, NeedAnotherIteration = false;
93
Ruchira Sasankae27c3442001-08-20 21:12:49 +000094 if(DEBUG_LV)
Ruchira Sasanka92e251c2001-10-16 01:25:05 +000095 cout << endl << " After Backward Pass ..." << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +000096
Chris Lattner3ff43872001-09-28 22:56:31 +000097 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000098
Chris Lattner3ff43872001-09-28 22:56:31 +000099 for( ; BBI != po_end(Meth) ; ++BBI)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000100 {
101
102 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
103 assert( LVBB );
104
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000105 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
106 // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000107
108 ResultFlow = false;
109
110 if( LVBB->isOutSetChanged() )
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000111 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
112
113 if( LVBB->isInSetChanged() ) // to calc Outsets of preds
114 ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000115
116 if(DEBUG_LV) LVBB->printInOutSets();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000117
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000118
119 if( ResultFlow ) NeedAnotherIteration = true;
120
121 }
122
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000123 // true if we need to reiterate over the CFG
124 return NeedAnotherIteration;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000125}
126
127
128
129
130
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000131// performs live var anal for a method
132void MethodLiveVarInfo::analyze()
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000133{
Vikram S. Adve8b5f6cc2001-08-28 22:36:35 +0000134 // Don't analyze the same method twice!
135 // Later, we need to add change notification here.
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000136
137
Vikram S. Adve8b5f6cc2001-08-28 22:36:35 +0000138 if (HasAnalyzed)
139 return;
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000140
141
142 if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000143
144 // create and initialize all the BBLiveVars of the CFG
145 constructBBs();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000146
147 bool NeedAnotherIteration = false;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000148 do { // do one pass over CFG
149 NeedAnotherIteration = doSingleBackwardPass( );
150 } while (NeedAnotherIteration ); // repeat until we need more iterations
151
152
153 HasAnalyzed = true; // finished analysing
154
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000155 if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000156}
157
158
159
160
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000161/* Thsese functions will give the LiveVar info for any machine instruction in
162 a method. It should be called after a call to analyze().
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000163
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000164 Thsese functions calucluates live var info for all the machine instrs in a
165 BB when LVInfo for one inst is requested. Hence, this function is useful
166 when live var info is required for many (or all) instructions in a basic
167 block. Also, the arguments to this method does not require specific
168 iterators.
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000169*/
170
171
172const LiveVarSet *
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000173MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
174 const BasicBlock *const CurBB)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000175{
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000176 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000177
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000178 if( LVSet ) return LVSet; // if found, just return the set
179 else {
180 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
181 assert( MInst2LVSetBI[ MInst ] );
182 return MInst2LVSetBI[ MInst ];
183 }
184}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000185
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000186
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000187const LiveVarSet *
188MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
189 const BasicBlock *const CurBB)
190{
191 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000192
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000193 if( LVSet ) return LVSet; // if found, just return the set
194 else {
195 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
196 assert( MInst2LVSetAI[ MInst ] );
197 return MInst2LVSetAI[ MInst ];
198 }
199}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000200
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000201
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000202void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
203{
204 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
205 MachineCodeForBasicBlock::const_reverse_iterator
206 MInstIterator = MIVec.rbegin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000207
208 LiveVarSet *CurSet = new LiveVarSet();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000209 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
210 CurSet->setUnion(SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000211
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000212 // iterate over all the machine instructions in BB
213 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000214
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000215 // MInst is cur machine inst
216 const MachineInstr * MInst = *MInstIterator;
217
218 MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
219
220 CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
221 LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
222 NewSet->setUnion( CurSet ); // copy the set after T/F to it
223
224 MInst2LVSetBI[MInst] = NewSet; // record in Before Inst map
225
226 // SetAI will be used in the next iteration
227 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000228 }
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000229
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000230}
231
232
233
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000234
235
236
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000237
238
239
240
241
242
243
244
245
246
247