blob: 636359d1d08eefa9d5846b222c0fa3ec1e458e6c [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
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000017//************************** Constructor/Destructor ***************************
Ruchira Sasanka683847f2001-07-24 17:14:13 +000018
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 Sasanka789cebb2001-12-08 21:05:27 +000031 // 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 //
Ruchira Sasankae27c3442001-08-20 21:12:49 +000036 BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000037
38 for( ; HMI != BB2BBLVMap.end() ; HMI ++ ) {
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000039 if( (*HMI).first ) // delete all BBLiveVar in BB2BBLVMap
Ruchira Sasanka683847f2001-07-24 17:14:13 +000040 delete (*HMI).second;
41 }
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000042
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
Ruchira Sasanka683847f2001-07-24 17:14:13 +000060}
61
62
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000063// ************************* support functions ********************************
Ruchira Sasanka683847f2001-07-24 17:14:13 +000064
65
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000066//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +000067// constructs BBLiveVars and init Def and In sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000068//-----------------------------------------------------------------------------
69
Ruchira Sasanka683847f2001-07-24 17:14:13 +000070void MethodLiveVarInfo::constructBBs()
71{
Ruchira Sasankae27c3442001-08-20 21:12:49 +000072 unsigned int POId = 0; // Reverse Depth-first Order ID
Ruchira Sasanka683847f2001-07-24 17:14:13 +000073
Chris Lattner3ff43872001-09-28 22:56:31 +000074 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000075
Chris Lattner3ff43872001-09-28 22:56:31 +000076 for( ; BBI != po_end(Meth) ; ++BBI, ++POId)
Ruchira Sasanka683847f2001-07-24 17:14:13 +000077 {
78
Ruchira Sasankae27c3442001-08-20 21:12:49 +000079 const BasicBlock *BB = *BBI; // get the current BB
Ruchira Sasankaaca997c2001-09-30 23:28:04 +000080
Ruchira Sasanka92e251c2001-10-16 01:25:05 +000081 if(DEBUG_LV) { cout << " For BB "; printValue(BB); cout << ":" << endl; }
Ruchira Sasankaaca997c2001-09-30 23:28:04 +000082
Ruchira Sasankae27c3442001-08-20 21:12:49 +000083 // create a new BBLiveVar
84 BBLiveVar * LVBB = new BBLiveVar( BB, POId );
Ruchira Sasanka683847f2001-07-24 17:14:13 +000085
Ruchira Sasankae27c3442001-08-20 21:12:49 +000086 BB2BBLVMap[ BB ] = LVBB; // insert the pair to Map
Ruchira Sasanka683847f2001-07-24 17:14:13 +000087
Ruchira Sasankae27c3442001-08-20 21:12:49 +000088 LVBB->calcDefUseSets(); // calculates the def and in set
Ruchira Sasanka683847f2001-07-24 17:14:13 +000089
Ruchira Sasankae27c3442001-08-20 21:12:49 +000090 if(DEBUG_LV)
91 LVBB->printAllSets();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000092 }
Ruchira Sasankac1daae82001-10-12 17:47:23 +000093
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
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000109}
110
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000111
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000112//-----------------------------------------------------------------------------
113// do one backward pass over the CFG (for iterative analysis)
114//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000115bool MethodLiveVarInfo::doSingleBackwardPass()
116{
117 bool ResultFlow, NeedAnotherIteration = false;
118
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000119 if(DEBUG_LV)
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000120 cout << endl << " After Backward Pass ..." << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000121
Chris Lattner3ff43872001-09-28 22:56:31 +0000122 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000123
Chris Lattner3ff43872001-09-28 22:56:31 +0000124 for( ; BBI != po_end(Meth) ; ++BBI)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000125 {
126
127 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
128 assert( LVBB );
129
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000130 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
131 // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000132
133 ResultFlow = false;
134
135 if( LVBB->isOutSetChanged() )
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000136 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
137
138 if( LVBB->isInSetChanged() ) // to calc Outsets of preds
139 ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000140
141 if(DEBUG_LV) LVBB->printInOutSets();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000142
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000143
144 if( ResultFlow ) NeedAnotherIteration = true;
145
146 }
147
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000148 // true if we need to reiterate over the CFG
149 return NeedAnotherIteration;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000150}
151
152
153
154
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000155//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000156// performs live var anal for a method
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000157//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000158void MethodLiveVarInfo::analyze()
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000159{
Vikram S. Adve8b5f6cc2001-08-28 22:36:35 +0000160 // Don't analyze the same method twice!
161 // Later, we need to add change notification here.
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000162
163
Vikram S. Adve8b5f6cc2001-08-28 22:36:35 +0000164 if (HasAnalyzed)
165 return;
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000166
167
168 if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000169
170 // create and initialize all the BBLiveVars of the CFG
171 constructBBs();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000172
173 bool NeedAnotherIteration = false;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000174 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
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000181 if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000182}
183
184
185
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000186//-----------------------------------------------------------------------------
187/* Following functions will give the LiveVar info for any machine instr in
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000188 a method. It should be called after a call to analyze().
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000189
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000190 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.
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000195*/
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000196//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000197
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000198//-----------------------------------------------------------------------------
199// Gives live variable information before a machine instruction
200//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000201const LiveVarSet *
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000202MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
203 const BasicBlock *const CurBB)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000204{
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000205 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000206
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000207 if( LVSet ) return LVSet; // if found, just return the set
208 else {
209 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000210
211 /*if( ! MInst2LVSetBI[ MInst ] ) {
212 cerr << "\nFor BB "; printValue( CurBB);
213 cerr << "\nRequested LVSet for inst: " << *MInst;
214 }*/
215
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000216 assert( MInst2LVSetBI[ MInst ] );
217 return MInst2LVSetBI[ MInst ];
218 }
219}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000220
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000221
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000222//-----------------------------------------------------------------------------
223// Gives live variable information after a machine instruction
224//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000225const LiveVarSet *
226MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
227 const BasicBlock *const CurBB)
228{
229 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000230
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000231 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}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000238
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000239
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000240
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//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000246void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
247{
248 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
249 MachineCodeForBasicBlock::const_reverse_iterator
250 MInstIterator = MIVec.rbegin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000251
252 LiveVarSet *CurSet = new LiveVarSet();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000253 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
254 CurSet->setUnion(SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000255
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000256 // iterate over all the machine instructions in BB
257 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000258
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000259 // 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;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000272 }
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000273
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000274}
275
276
277
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000278
279
280
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000281
282
283
284
285
286
287
288
289
290
291