blob: 91c2498e9750c46cee02264642fcefc6d54ecf9c [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"
Ruchira Sasanka683847f2001-07-24 17:14:13 +000014
15
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
50 cfg::po_const_iterator BBI = cfg::po_begin(Meth);
51
52 for( ; BBI != cfg::po_end(Meth) ; ++BBI, ++POId)
53 {
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
80 cfg::po_const_iterator BBI = cfg::po_begin(Meth);
81
82 for( ; BBI != cfg::po_end(Meth) ; ++BBI)
83 {
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{
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000117
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000118 if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
119
120 // create and initialize all the BBLiveVars of the CFG
121 constructBBs();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000122
123 bool NeedAnotherIteration = false;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000124 do { // do one pass over CFG
125 NeedAnotherIteration = doSingleBackwardPass( );
126 } while (NeedAnotherIteration ); // repeat until we need more iterations
127
128
129 HasAnalyzed = true; // finished analysing
130
131 if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000132}
133
134
135
136
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000137/* Thsese functions will give the LiveVar info for any machine instruction in
138 a method. It should be called after a call to analyze().
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000139
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000140 Thsese functions calucluates live var info for all the machine instrs in a
141 BB when LVInfo for one inst is requested. Hence, this function is useful
142 when live var info is required for many (or all) instructions in a basic
143 block. Also, the arguments to this method does not require specific
144 iterators.
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000145*/
146
147
148const LiveVarSet *
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000149MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
150 const BasicBlock *const CurBB)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000151{
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000152 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000153
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000154 if( LVSet ) return LVSet; // if found, just return the set
155 else {
156 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
157 assert( MInst2LVSetBI[ MInst ] );
158 return MInst2LVSetBI[ MInst ];
159 }
160}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000161
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000162
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000163const LiveVarSet *
164MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
165 const BasicBlock *const CurBB)
166{
167 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000168
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000169 if( LVSet ) return LVSet; // if found, just return the set
170 else {
171 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
172 assert( MInst2LVSetAI[ MInst ] );
173 return MInst2LVSetAI[ MInst ];
174 }
175}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000176
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000177
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000178void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
179{
180 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
181 MachineCodeForBasicBlock::const_reverse_iterator
182 MInstIterator = MIVec.rbegin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000183
184 LiveVarSet *CurSet = new LiveVarSet();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000185 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
186 CurSet->setUnion(SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000187
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000188 // iterate over all the machine instructions in BB
189 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000190
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000191 // MInst is cur machine inst
192 const MachineInstr * MInst = *MInstIterator;
193
194 MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
195
196 CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
197 LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
198 NewSet->setUnion( CurSet ); // copy the set after T/F to it
199
200 MInst2LVSetBI[MInst] = NewSet; // record in Before Inst map
201
202 // SetAI will be used in the next iteration
203 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000204 }
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000205
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000206}
207
208
209
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000210
211
212
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000213
214
215
216
217
218
219
220
221
222
223