blob: b9fd0ffbe86c1963610a6bdf51a71dec4865aa17 [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 Lattner3ff43872001-09-28 22:56:31 +000014#include "llvm/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
57 if(DEBUG_LV) { cout << " For BB "; printValue(BB); cout << ":" << endl; }
58
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 Sasanka683847f2001-07-24 17:14:13 +000069}
70
Ruchira Sasankae27c3442001-08-20 21:12:49 +000071
72
73// do one backward pass over the CFG
Ruchira Sasanka683847f2001-07-24 17:14:13 +000074bool MethodLiveVarInfo::doSingleBackwardPass()
75{
76 bool ResultFlow, NeedAnotherIteration = false;
77
Ruchira Sasankae27c3442001-08-20 21:12:49 +000078 if(DEBUG_LV)
79 cout << endl << " After Backward Pass ..." << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +000080
Chris Lattner3ff43872001-09-28 22:56:31 +000081 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000082
Chris Lattner3ff43872001-09-28 22:56:31 +000083 for( ; BBI != po_end(Meth) ; ++BBI)
Ruchira Sasanka683847f2001-07-24 17:14:13 +000084 {
85
86 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
87 assert( LVBB );
88
Ruchira Sasankae27c3442001-08-20 21:12:49 +000089 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +000090 // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
91
92 ResultFlow = false;
93
94 if( LVBB->isOutSetChanged() )
Ruchira Sasankae27c3442001-08-20 21:12:49 +000095 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
96
97 if( LVBB->isInSetChanged() ) // to calc Outsets of preds
98 ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000099
100 if(DEBUG_LV) LVBB->printInOutSets();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000101
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000102
103 if( ResultFlow ) NeedAnotherIteration = true;
104
105 }
106
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000107 // true if we need to reiterate over the CFG
108 return NeedAnotherIteration;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000109}
110
111
112
113
114
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000115// performs live var anal for a method
116void MethodLiveVarInfo::analyze()
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000117{
Vikram S. Adve8b5f6cc2001-08-28 22:36:35 +0000118 // Don't analyze the same method twice!
119 // Later, we need to add change notification here.
120 if (HasAnalyzed)
121 return;
122
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000123 if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
124
125 // create and initialize all the BBLiveVars of the CFG
126 constructBBs();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000127
128 bool NeedAnotherIteration = false;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000129 do { // do one pass over CFG
130 NeedAnotherIteration = doSingleBackwardPass( );
131 } while (NeedAnotherIteration ); // repeat until we need more iterations
132
133
134 HasAnalyzed = true; // finished analysing
135
136 if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000137}
138
139
140
141
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000142/* Thsese functions will give the LiveVar info for any machine instruction in
143 a method. It should be called after a call to analyze().
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000144
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000145 Thsese functions calucluates live var info for all the machine instrs in a
146 BB when LVInfo for one inst is requested. Hence, this function is useful
147 when live var info is required for many (or all) instructions in a basic
148 block. Also, the arguments to this method does not require specific
149 iterators.
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000150*/
151
152
153const LiveVarSet *
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000154MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
155 const BasicBlock *const CurBB)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000156{
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000157 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000158
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000159 if( LVSet ) return LVSet; // if found, just return the set
160 else {
161 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
162 assert( MInst2LVSetBI[ MInst ] );
163 return MInst2LVSetBI[ MInst ];
164 }
165}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000166
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000167
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000168const LiveVarSet *
169MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
170 const BasicBlock *const CurBB)
171{
172 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000173
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000174 if( LVSet ) return LVSet; // if found, just return the set
175 else {
176 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
177 assert( MInst2LVSetAI[ MInst ] );
178 return MInst2LVSetAI[ MInst ];
179 }
180}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000181
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000182
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000183void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
184{
185 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
186 MachineCodeForBasicBlock::const_reverse_iterator
187 MInstIterator = MIVec.rbegin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000188
189 LiveVarSet *CurSet = new LiveVarSet();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000190 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
191 CurSet->setUnion(SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000192
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000193 // iterate over all the machine instructions in BB
194 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000195
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000196 // MInst is cur machine inst
197 const MachineInstr * MInst = *MInstIterator;
198
199 MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
200
201 CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
202 LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
203 NewSet->setUnion( CurSet ); // copy the set after T/F to it
204
205 MInst2LVSetBI[MInst] = NewSet; // record in Before Inst map
206
207 // SetAI will be used in the next iteration
208 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000209 }
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000210
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000211}
212
213
214
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000215
216
217
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000218
219
220
221
222
223
224
225
226
227
228