blob: 898dd28b72d886a4b4679815fec2f8da3ad6a5db [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 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl ;
Ruchira Sasanka683847f2001-07-24 17:14:13 +000056
Ruchira Sasankae27c3442001-08-20 21:12:49 +000057 const BasicBlock *BB = *BBI; // get the current BB
58 // create a new BBLiveVar
59 BBLiveVar * LVBB = new BBLiveVar( BB, POId );
Ruchira Sasanka683847f2001-07-24 17:14:13 +000060
Ruchira Sasankae27c3442001-08-20 21:12:49 +000061 BB2BBLVMap[ BB ] = LVBB; // insert the pair to Map
Ruchira Sasanka683847f2001-07-24 17:14:13 +000062
Ruchira Sasankae27c3442001-08-20 21:12:49 +000063 LVBB->calcDefUseSets(); // calculates the def and in set
Ruchira Sasanka683847f2001-07-24 17:14:13 +000064
Ruchira Sasankae27c3442001-08-20 21:12:49 +000065 if(DEBUG_LV)
66 LVBB->printAllSets();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000067 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +000068}
69
Ruchira Sasankae27c3442001-08-20 21:12:49 +000070
71
72// do one backward pass over the CFG
Ruchira Sasanka683847f2001-07-24 17:14:13 +000073bool MethodLiveVarInfo::doSingleBackwardPass()
74{
75 bool ResultFlow, NeedAnotherIteration = false;
76
Ruchira Sasankae27c3442001-08-20 21:12:49 +000077 if(DEBUG_LV)
78 cout << endl << " After Backward Pass ..." << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +000079
Chris Lattner3ff43872001-09-28 22:56:31 +000080 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000081
Chris Lattner3ff43872001-09-28 22:56:31 +000082 for( ; BBI != po_end(Meth) ; ++BBI)
Ruchira Sasanka683847f2001-07-24 17:14:13 +000083 {
84
85 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
86 assert( LVBB );
87
Ruchira Sasankae27c3442001-08-20 21:12:49 +000088 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +000089 // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
90
91 ResultFlow = false;
92
93 if( LVBB->isOutSetChanged() )
Ruchira Sasankae27c3442001-08-20 21:12:49 +000094 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
95
96 if( LVBB->isInSetChanged() ) // to calc Outsets of preds
97 ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000098
99 if(DEBUG_LV) LVBB->printInOutSets();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000100
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000101
102 if( ResultFlow ) NeedAnotherIteration = true;
103
104 }
105
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000106 // true if we need to reiterate over the CFG
107 return NeedAnotherIteration;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000108}
109
110
111
112
113
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000114// performs live var anal for a method
115void MethodLiveVarInfo::analyze()
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000116{
Vikram S. Adve8b5f6cc2001-08-28 22:36:35 +0000117 // Don't analyze the same method twice!
118 // Later, we need to add change notification here.
119 if (HasAnalyzed)
120 return;
121
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000122 if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
123
124 // create and initialize all the BBLiveVars of the CFG
125 constructBBs();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000126
127 bool NeedAnotherIteration = false;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000128 do { // do one pass over CFG
129 NeedAnotherIteration = doSingleBackwardPass( );
130 } while (NeedAnotherIteration ); // repeat until we need more iterations
131
132
133 HasAnalyzed = true; // finished analysing
134
135 if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000136}
137
138
139
140
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000141/* Thsese functions will give the LiveVar info for any machine instruction in
142 a method. It should be called after a call to analyze().
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000143
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000144 Thsese functions calucluates live var info for all the machine instrs in a
145 BB when LVInfo for one inst is requested. Hence, this function is useful
146 when live var info is required for many (or all) instructions in a basic
147 block. Also, the arguments to this method does not require specific
148 iterators.
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000149*/
150
151
152const LiveVarSet *
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000153MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
154 const BasicBlock *const CurBB)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000155{
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000156 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000157
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000158 if( LVSet ) return LVSet; // if found, just return the set
159 else {
160 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
161 assert( MInst2LVSetBI[ MInst ] );
162 return MInst2LVSetBI[ MInst ];
163 }
164}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000165
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000166
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000167const LiveVarSet *
168MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
169 const BasicBlock *const CurBB)
170{
171 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000172
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000173 if( LVSet ) return LVSet; // if found, just return the set
174 else {
175 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
176 assert( MInst2LVSetAI[ MInst ] );
177 return MInst2LVSetAI[ MInst ];
178 }
179}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000180
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000181
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000182void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
183{
184 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
185 MachineCodeForBasicBlock::const_reverse_iterator
186 MInstIterator = MIVec.rbegin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000187
188 LiveVarSet *CurSet = new LiveVarSet();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000189 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
190 CurSet->setUnion(SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000191
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000192 // iterate over all the machine instructions in BB
193 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000194
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000195 // MInst is cur machine inst
196 const MachineInstr * MInst = *MInstIterator;
197
198 MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
199
200 CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
201 LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
202 NewSet->setUnion( CurSet ); // copy the set after T/F to it
203
204 MInst2LVSetBI[MInst] = NewSet; // record in Before Inst map
205
206 // SetAI will be used in the next iteration
207 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000208 }
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000209
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000210}
211
212
213
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000214
215
216
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000217
218
219
220
221
222
223
224
225
226
227