blob: 5de35ff1be58da22dc614d7cc7ff54dff658f1ec [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"
Chris Lattner697954c2002-01-20 22:54:45 +000015#include <iostream>
16using std::cout;
17using std::endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +000018
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000019//************************** Constructor/Destructor ***************************
Ruchira Sasanka683847f2001-07-24 17:14:13 +000020
21
Chris Lattner697954c2002-01-20 22:54:45 +000022MethodLiveVarInfo::MethodLiveVarInfo(const Method *const M) : Meth(M) {
23 assert(!M->isExternal() && "Cannot be a prototype declaration");
Ruchira Sasankae27c3442001-08-20 21:12:49 +000024 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 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +000058}
59
60
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000061// ************************* support functions ********************************
Ruchira Sasanka683847f2001-07-24 17:14:13 +000062
63
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000064//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +000065// constructs BBLiveVars and init Def and In sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000066//-----------------------------------------------------------------------------
67
Ruchira Sasanka683847f2001-07-24 17:14:13 +000068void MethodLiveVarInfo::constructBBs()
69{
Ruchira Sasankae27c3442001-08-20 21:12:49 +000070 unsigned int POId = 0; // Reverse Depth-first Order ID
Ruchira Sasanka683847f2001-07-24 17:14:13 +000071
Chris Lattner3ff43872001-09-28 22:56:31 +000072 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000073
Chris Lattner3ff43872001-09-28 22:56:31 +000074 for( ; BBI != po_end(Meth) ; ++BBI, ++POId)
Ruchira Sasanka683847f2001-07-24 17:14:13 +000075 {
76
Ruchira Sasankae27c3442001-08-20 21:12:49 +000077 const BasicBlock *BB = *BBI; // get the current BB
Ruchira Sasankaaca997c2001-09-30 23:28:04 +000078
Ruchira Sasanka92e251c2001-10-16 01:25:05 +000079 if(DEBUG_LV) { cout << " For BB "; printValue(BB); cout << ":" << endl; }
Ruchira Sasankaaca997c2001-09-30 23:28:04 +000080
Ruchira Sasankae27c3442001-08-20 21:12:49 +000081 // create a new BBLiveVar
82 BBLiveVar * LVBB = new BBLiveVar( BB, POId );
Ruchira Sasanka683847f2001-07-24 17:14:13 +000083
Ruchira Sasankae27c3442001-08-20 21:12:49 +000084 BB2BBLVMap[ BB ] = LVBB; // insert the pair to Map
Ruchira Sasanka683847f2001-07-24 17:14:13 +000085
Ruchira Sasankae27c3442001-08-20 21:12:49 +000086 LVBB->calcDefUseSets(); // calculates the def and in set
Ruchira Sasanka683847f2001-07-24 17:14:13 +000087
Ruchira Sasankae27c3442001-08-20 21:12:49 +000088 if(DEBUG_LV)
89 LVBB->printAllSets();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000090 }
Ruchira Sasankac1daae82001-10-12 17:47:23 +000091
92 // Since the PO iterator does not discover unreachable blocks,
93 // go over the random iterator and init those blocks as well.
94 // However, LV info is not correct for those blocks (they are not
95 // analyzed)
96
97 Method::const_iterator BBRI = Meth->begin(); // random iterator for BBs
98
99 for( ; BBRI != Meth->end(); ++BBRI, ++POId) {
100
101 if( ! BB2BBLVMap[ *BBRI ] )
102 BB2BBLVMap[ *BBRI ] = new BBLiveVar( *BBRI, POId );
103
104 }
105
106
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000107}
108
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000109
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000110//-----------------------------------------------------------------------------
111// do one backward pass over the CFG (for iterative analysis)
112//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000113bool MethodLiveVarInfo::doSingleBackwardPass()
114{
115 bool ResultFlow, NeedAnotherIteration = false;
116
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000117 if(DEBUG_LV)
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000118 cout << endl << " After Backward Pass ..." << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000119
Chris Lattner3ff43872001-09-28 22:56:31 +0000120 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000121
Chris Lattner3ff43872001-09-28 22:56:31 +0000122 for( ; BBI != po_end(Meth) ; ++BBI)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000123 {
124
125 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
126 assert( LVBB );
127
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000128 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
129 // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000130
131 ResultFlow = false;
132
133 if( LVBB->isOutSetChanged() )
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000134 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
135
136 if( LVBB->isInSetChanged() ) // to calc Outsets of preds
137 ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000138
139 if(DEBUG_LV) LVBB->printInOutSets();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000140
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000141
142 if( ResultFlow ) NeedAnotherIteration = true;
143
144 }
145
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000146 // true if we need to reiterate over the CFG
147 return NeedAnotherIteration;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000148}
149
150
151
152
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000153//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000154// performs live var anal for a method
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000155//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000156void MethodLiveVarInfo::analyze()
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000157{
Vikram S. Adve8b5f6cc2001-08-28 22:36:35 +0000158 // Don't analyze the same method twice!
159 // Later, we need to add change notification here.
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000160
161
Vikram S. Adve8b5f6cc2001-08-28 22:36:35 +0000162 if (HasAnalyzed)
163 return;
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000164
165
166 if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000167
168 // create and initialize all the BBLiveVars of the CFG
169 constructBBs();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000170
171 bool NeedAnotherIteration = false;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000172 do { // do one pass over CFG
173 NeedAnotherIteration = doSingleBackwardPass( );
174 } while (NeedAnotherIteration ); // repeat until we need more iterations
175
176
177 HasAnalyzed = true; // finished analysing
178
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000179 if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000180}
181
182
183
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000184//-----------------------------------------------------------------------------
185/* Following functions will give the LiveVar info for any machine instr in
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000186 a method. It should be called after a call to analyze().
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000187
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000188 Thsese functions calucluates live var info for all the machine instrs in a
189 BB when LVInfo for one inst is requested. Hence, this function is useful
190 when live var info is required for many (or all) instructions in a basic
191 block. Also, the arguments to this method does not require specific
192 iterators.
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000193*/
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000194//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000195
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000196//-----------------------------------------------------------------------------
197// Gives live variable information before a machine instruction
198//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000199const LiveVarSet *
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000200MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
201 const BasicBlock *const CurBB)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000202{
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000203 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000204
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000205 if( LVSet ) return LVSet; // if found, just return the set
206 else {
207 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000208
209 /*if( ! MInst2LVSetBI[ MInst ] ) {
210 cerr << "\nFor BB "; printValue( CurBB);
211 cerr << "\nRequested LVSet for inst: " << *MInst;
212 }*/
213
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000214 assert( MInst2LVSetBI[ MInst ] );
215 return MInst2LVSetBI[ MInst ];
216 }
217}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000218
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000219
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000220//-----------------------------------------------------------------------------
221// Gives live variable information after a machine instruction
222//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000223const LiveVarSet *
224MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
225 const BasicBlock *const CurBB)
226{
227 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000228
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000229 if( LVSet ) return LVSet; // if found, just return the set
230 else {
231 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
232 assert( MInst2LVSetAI[ MInst ] );
233 return MInst2LVSetAI[ MInst ];
234 }
235}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000236
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000237
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000238
239//-----------------------------------------------------------------------------
240// This method calculates the live variable information for all the
241// instructions in a basic block and enter the newly constructed live
242// variable sets into a the caches ( MInst2LVSetAI, MInst2LVSetBI)
243//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000244void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
245{
246 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
247 MachineCodeForBasicBlock::const_reverse_iterator
248 MInstIterator = MIVec.rbegin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000249
250 LiveVarSet *CurSet = new LiveVarSet();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000251 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
252 CurSet->setUnion(SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000253
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000254 // iterate over all the machine instructions in BB
255 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000256
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000257 // MInst is cur machine inst
258 const MachineInstr * MInst = *MInstIterator;
259
260 MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
261
262 CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
263 LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
264 NewSet->setUnion( CurSet ); // copy the set after T/F to it
265
266 MInst2LVSetBI[MInst] = NewSet; // record in Before Inst map
267
268 // SetAI will be used in the next iteration
269 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000270 }
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000271
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000272}
273
274
275
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000276
277
278
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000279
280
281
282
283
284
285
286
287
288
289