blob: 40ebf1e6bd38656267555ce563f163230ec9c1bf [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 Sasankac1daae82001-10-12 17:47:23 +000069
70 // Since the PO iterator does not discover unreachable blocks,
71 // go over the random iterator and init those blocks as well.
72 // However, LV info is not correct for those blocks (they are not
73 // analyzed)
74
75 Method::const_iterator BBRI = Meth->begin(); // random iterator for BBs
76
77 for( ; BBRI != Meth->end(); ++BBRI, ++POId) {
78
79 if( ! BB2BBLVMap[ *BBRI ] )
80 BB2BBLVMap[ *BBRI ] = new BBLiveVar( *BBRI, POId );
81
82 }
83
84
Ruchira Sasanka683847f2001-07-24 17:14:13 +000085}
86
Ruchira Sasankae27c3442001-08-20 21:12:49 +000087
88
89// do one backward pass over the CFG
Ruchira Sasanka683847f2001-07-24 17:14:13 +000090bool MethodLiveVarInfo::doSingleBackwardPass()
91{
92 bool ResultFlow, NeedAnotherIteration = false;
93
Ruchira Sasankae27c3442001-08-20 21:12:49 +000094 if(DEBUG_LV)
95 cout << endl << " After Backward Pass ..." << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +000096
Chris Lattner3ff43872001-09-28 22:56:31 +000097 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000098
Chris Lattner3ff43872001-09-28 22:56:31 +000099 for( ; BBI != po_end(Meth) ; ++BBI)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000100 {
101
102 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
103 assert( LVBB );
104
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000105 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000106 // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
107
108 ResultFlow = false;
109
110 if( LVBB->isOutSetChanged() )
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000111 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
112
113 if( LVBB->isInSetChanged() ) // to calc Outsets of preds
114 ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000115
116 if(DEBUG_LV) LVBB->printInOutSets();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000117
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000118
119 if( ResultFlow ) NeedAnotherIteration = true;
120
121 }
122
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000123 // true if we need to reiterate over the CFG
124 return NeedAnotherIteration;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000125}
126
127
128
129
130
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000131// performs live var anal for a method
132void MethodLiveVarInfo::analyze()
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000133{
Vikram S. Adve8b5f6cc2001-08-28 22:36:35 +0000134 // Don't analyze the same method twice!
135 // Later, we need to add change notification here.
136 if (HasAnalyzed)
137 return;
138
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000139 if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
140
141 // create and initialize all the BBLiveVars of the CFG
142 constructBBs();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000143
144 bool NeedAnotherIteration = false;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000145 do { // do one pass over CFG
146 NeedAnotherIteration = doSingleBackwardPass( );
147 } while (NeedAnotherIteration ); // repeat until we need more iterations
148
149
150 HasAnalyzed = true; // finished analysing
151
152 if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000153}
154
155
156
157
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000158/* Thsese functions will give the LiveVar info for any machine instruction in
159 a method. It should be called after a call to analyze().
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000160
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000161 Thsese functions calucluates live var info for all the machine instrs in a
162 BB when LVInfo for one inst is requested. Hence, this function is useful
163 when live var info is required for many (or all) instructions in a basic
164 block. Also, the arguments to this method does not require specific
165 iterators.
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000166*/
167
168
169const LiveVarSet *
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000170MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
171 const BasicBlock *const CurBB)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000172{
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000173 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000174
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000175 if( LVSet ) return LVSet; // if found, just return the set
176 else {
177 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
178 assert( MInst2LVSetBI[ MInst ] );
179 return MInst2LVSetBI[ MInst ];
180 }
181}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000182
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000183
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000184const LiveVarSet *
185MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
186 const BasicBlock *const CurBB)
187{
188 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000189
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000190 if( LVSet ) return LVSet; // if found, just return the set
191 else {
192 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
193 assert( MInst2LVSetAI[ MInst ] );
194 return MInst2LVSetAI[ MInst ];
195 }
196}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000197
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000198
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000199void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
200{
201 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
202 MachineCodeForBasicBlock::const_reverse_iterator
203 MInstIterator = MIVec.rbegin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000204
205 LiveVarSet *CurSet = new LiveVarSet();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000206 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
207 CurSet->setUnion(SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000208
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000209 // iterate over all the machine instructions in BB
210 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000211
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000212 // MInst is cur machine inst
213 const MachineInstr * MInst = *MInstIterator;
214
215 MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
216
217 CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
218 LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
219 NewSet->setUnion( CurSet ); // copy the set after T/F to it
220
221 MInst2LVSetBI[MInst] = NewSet; // record in Before Inst map
222
223 // SetAI will be used in the next iteration
224 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000225 }
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000226
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000227}
228
229
230
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000231
232
233
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000234
235
236
237
238
239
240
241
242
243
244