blob: 77c8c54e89c5ae17490b692b995f76856854cd8f [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 Lattner11646322002-02-04 16:35:12 +000014#include "llvm/BasicBlock.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000015#include "Support/PostOrderIterator.h"
Chris Lattner697954c2002-01-20 22:54:45 +000016#include <iostream>
17using std::cout;
18using std::endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +000019
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000020//************************** Constructor/Destructor ***************************
Ruchira Sasanka683847f2001-07-24 17:14:13 +000021
22
Chris Lattner697954c2002-01-20 22:54:45 +000023MethodLiveVarInfo::MethodLiveVarInfo(const Method *const M) : Meth(M) {
24 assert(!M->isExternal() && "Cannot be a prototype declaration");
Ruchira Sasankae27c3442001-08-20 21:12:49 +000025 HasAnalyzed = false; // still we haven't called analyze()
Ruchira Sasanka683847f2001-07-24 17:14:13 +000026}
27
28
29
30MethodLiveVarInfo:: ~MethodLiveVarInfo()
31{
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000032 // First delete all BBLiveVar objects created in constructBBs(). A new object
33 // of type BBLiveVa is created for every BasicBlock in the method
34
35 // hash map iterator for BB2BBLVMap
36 //
Ruchira Sasankae27c3442001-08-20 21:12:49 +000037 BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000038
39 for( ; HMI != BB2BBLVMap.end() ; HMI ++ ) {
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000040 if( (*HMI).first ) // delete all BBLiveVar in BB2BBLVMap
Ruchira Sasanka683847f2001-07-24 17:14:13 +000041 delete (*HMI).second;
42 }
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000043
44
45 // Then delete all objects of type LiveVarSet created in calcLiveVarSetsForBB
46 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
47 // to return LiveVarSet's before/after a machine instruction quickly). It
48 // is sufficient to free up all LiveVarSet using only one cache since
49 // both caches refer to the same sets
50
51 // hash map iterator for MInst2LVSetBI
52 //
53 MInstToLiveVarSetMapType::iterator MI = MInst2LVSetBI.begin();
54
55 for( ; MI != MInst2LVSetBI.end() ; MI ++ ) {
56 if( (*MI).first ) // delete all LiveVarSets in MInst2LVSetBI
57 delete (*MI).second;
58 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +000059}
60
61
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000062// ************************* support functions ********************************
Ruchira Sasanka683847f2001-07-24 17:14:13 +000063
64
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000065//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +000066// constructs BBLiveVars and init Def and In sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000067//-----------------------------------------------------------------------------
68
Ruchira Sasanka683847f2001-07-24 17:14:13 +000069void MethodLiveVarInfo::constructBBs()
70{
Ruchira Sasankae27c3442001-08-20 21:12:49 +000071 unsigned int POId = 0; // Reverse Depth-first Order ID
Ruchira Sasanka683847f2001-07-24 17:14:13 +000072
Chris Lattner3ff43872001-09-28 22:56:31 +000073 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000074
Chris Lattner3ff43872001-09-28 22:56:31 +000075 for( ; BBI != po_end(Meth) ; ++BBI, ++POId)
Ruchira Sasanka683847f2001-07-24 17:14:13 +000076 {
77
Ruchira Sasankae27c3442001-08-20 21:12:49 +000078 const BasicBlock *BB = *BBI; // get the current BB
Ruchira Sasankaaca997c2001-09-30 23:28:04 +000079
Ruchira Sasanka92e251c2001-10-16 01:25:05 +000080 if(DEBUG_LV) { cout << " For BB "; printValue(BB); cout << ":" << endl; }
Ruchira Sasankaaca997c2001-09-30 23:28:04 +000081
Ruchira Sasankae27c3442001-08-20 21:12:49 +000082 // create a new BBLiveVar
83 BBLiveVar * LVBB = new BBLiveVar( BB, POId );
Ruchira Sasanka683847f2001-07-24 17:14:13 +000084
Ruchira Sasankae27c3442001-08-20 21:12:49 +000085 BB2BBLVMap[ BB ] = LVBB; // insert the pair to Map
Ruchira Sasanka683847f2001-07-24 17:14:13 +000086
Ruchira Sasankae27c3442001-08-20 21:12:49 +000087 LVBB->calcDefUseSets(); // calculates the def and in set
Ruchira Sasanka683847f2001-07-24 17:14:13 +000088
Ruchira Sasankae27c3442001-08-20 21:12:49 +000089 if(DEBUG_LV)
90 LVBB->printAllSets();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000091 }
Ruchira Sasankac1daae82001-10-12 17:47:23 +000092
93 // Since the PO iterator does not discover unreachable blocks,
94 // go over the random iterator and init those blocks as well.
95 // However, LV info is not correct for those blocks (they are not
96 // analyzed)
97
98 Method::const_iterator BBRI = Meth->begin(); // random iterator for BBs
99
100 for( ; BBRI != Meth->end(); ++BBRI, ++POId) {
101
102 if( ! BB2BBLVMap[ *BBRI ] )
103 BB2BBLVMap[ *BBRI ] = new BBLiveVar( *BBRI, POId );
104
105 }
106
107
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000108}
109
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000110
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000111//-----------------------------------------------------------------------------
112// do one backward pass over the CFG (for iterative analysis)
113//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000114bool MethodLiveVarInfo::doSingleBackwardPass()
115{
116 bool ResultFlow, NeedAnotherIteration = false;
117
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000118 if(DEBUG_LV)
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000119 cout << endl << " After Backward Pass ..." << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000120
Chris Lattner3ff43872001-09-28 22:56:31 +0000121 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000122
Chris Lattner3ff43872001-09-28 22:56:31 +0000123 for( ; BBI != po_end(Meth) ; ++BBI)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000124 {
125
126 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
127 assert( LVBB );
128
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000129 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
130 // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000131
132 ResultFlow = false;
133
134 if( LVBB->isOutSetChanged() )
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000135 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
136
137 if( LVBB->isInSetChanged() ) // to calc Outsets of preds
138 ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000139
140 if(DEBUG_LV) LVBB->printInOutSets();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000141
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000142
143 if( ResultFlow ) NeedAnotherIteration = true;
144
145 }
146
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000147 // true if we need to reiterate over the CFG
148 return NeedAnotherIteration;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000149}
150
151
152
153
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000154//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000155// performs live var anal for a method
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000156//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000157void MethodLiveVarInfo::analyze()
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000158{
Vikram S. Adve8b5f6cc2001-08-28 22:36:35 +0000159 // Don't analyze the same method twice!
160 // Later, we need to add change notification here.
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000161
162
Vikram S. Adve8b5f6cc2001-08-28 22:36:35 +0000163 if (HasAnalyzed)
164 return;
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000165
166
167 if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000168
169 // create and initialize all the BBLiveVars of the CFG
170 constructBBs();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000171
172 bool NeedAnotherIteration = false;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000173 do { // do one pass over CFG
174 NeedAnotherIteration = doSingleBackwardPass( );
175 } while (NeedAnotherIteration ); // repeat until we need more iterations
176
177
178 HasAnalyzed = true; // finished analysing
179
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000180 if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000181}
182
183
184
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000185//-----------------------------------------------------------------------------
186/* Following functions will give the LiveVar info for any machine instr in
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000187 a method. It should be called after a call to analyze().
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000188
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000189 Thsese functions calucluates live var info for all the machine instrs in a
190 BB when LVInfo for one inst is requested. Hence, this function is useful
191 when live var info is required for many (or all) instructions in a basic
192 block. Also, the arguments to this method does not require specific
193 iterators.
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000194*/
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000195//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000196
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000197//-----------------------------------------------------------------------------
198// Gives live variable information before a machine instruction
199//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000200const LiveVarSet *
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000201MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
202 const BasicBlock *const CurBB)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000203{
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000204 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000205
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000206 if( LVSet ) return LVSet; // if found, just return the set
207 else {
208 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000209
210 /*if( ! MInst2LVSetBI[ MInst ] ) {
211 cerr << "\nFor BB "; printValue( CurBB);
212 cerr << "\nRequested LVSet for inst: " << *MInst;
213 }*/
214
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000215 assert( MInst2LVSetBI[ MInst ] );
216 return MInst2LVSetBI[ MInst ];
217 }
218}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000219
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000220
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000221//-----------------------------------------------------------------------------
222// Gives live variable information after a machine instruction
223//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000224const LiveVarSet *
225MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
226 const BasicBlock *const CurBB)
227{
228 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000229
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000230 if( LVSet ) return LVSet; // if found, just return the set
231 else {
232 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
233 assert( MInst2LVSetAI[ MInst ] );
234 return MInst2LVSetAI[ MInst ];
235 }
236}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000237
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000238
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000239
240//-----------------------------------------------------------------------------
241// This method calculates the live variable information for all the
242// instructions in a basic block and enter the newly constructed live
243// variable sets into a the caches ( MInst2LVSetAI, MInst2LVSetBI)
244//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000245void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
246{
247 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
248 MachineCodeForBasicBlock::const_reverse_iterator
249 MInstIterator = MIVec.rbegin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000250
251 LiveVarSet *CurSet = new LiveVarSet();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000252 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
253 CurSet->setUnion(SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000254
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000255 // iterate over all the machine instructions in BB
256 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000257
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000258 // MInst is cur machine inst
259 const MachineInstr * MInst = *MInstIterator;
260
261 MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
262
263 CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
264 LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
265 NewSet->setUnion( CurSet ); // copy the set after T/F to it
266
267 MInst2LVSetBI[MInst] = NewSet; // record in Before Inst map
268
269 // SetAI will be used in the next iteration
270 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000271 }
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000272
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000273}
274
275
276
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000277
278
279
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000280
281
282
283
284
285
286
287
288
289
290