blob: 54980d796030c17b237dfc7fdbbf82ab9586d93d [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
Chris Lattner4fd2dbb2002-02-04 20:00:08 +000020AnalysisID MethodLiveVarInfo::ID(AnalysisID::create<MethodLiveVarInfo>());
Ruchira Sasanka683847f2001-07-24 17:14:13 +000021
Chris Lattner4fd2dbb2002-02-04 20:00:08 +000022void MethodLiveVarInfo::releaseMemory() {
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000023 // First delete all BBLiveVar objects created in constructBBs(). A new object
24 // of type BBLiveVa is created for every BasicBlock in the method
25
26 // hash map iterator for BB2BBLVMap
27 //
Ruchira Sasankae27c3442001-08-20 21:12:49 +000028 BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000029
Chris Lattner4fd2dbb2002-02-04 20:00:08 +000030 for( ; HMI != BB2BBLVMap.end(); ++HMI)
31 delete HMI->second; // delete all BBLiveVar in BB2BBLVMap
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000032
Chris Lattner4fd2dbb2002-02-04 20:00:08 +000033 BB2BBLVMap.clear();
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000034
35 // Then delete all objects of type LiveVarSet created in calcLiveVarSetsForBB
36 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
37 // to return LiveVarSet's before/after a machine instruction quickly). It
38 // is sufficient to free up all LiveVarSet using only one cache since
39 // both caches refer to the same sets
40
41 // hash map iterator for MInst2LVSetBI
42 //
Chris Lattner4fd2dbb2002-02-04 20:00:08 +000043 MInstToLiveVarSetMapType::iterator MI = MInst2LVSetBI.begin();
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000044
Chris Lattner4fd2dbb2002-02-04 20:00:08 +000045 for( ; MI != MInst2LVSetBI.end(); ++MI)
46 delete MI->second; // delete all LiveVarSets in MInst2LVSetBI
47
48 MInst2LVSetBI.clear();
49 MInst2LVSetAI.clear();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000050}
51
52
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000053//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +000054// constructs BBLiveVars and init Def and In sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000055//-----------------------------------------------------------------------------
56
Ruchira Sasanka683847f2001-07-24 17:14:13 +000057void MethodLiveVarInfo::constructBBs()
58{
Ruchira Sasankae27c3442001-08-20 21:12:49 +000059 unsigned int POId = 0; // Reverse Depth-first Order ID
Ruchira Sasanka683847f2001-07-24 17:14:13 +000060
Chris Lattner3ff43872001-09-28 22:56:31 +000061 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +000062
Chris Lattner3ff43872001-09-28 22:56:31 +000063 for( ; BBI != po_end(Meth) ; ++BBI, ++POId)
Ruchira Sasanka683847f2001-07-24 17:14:13 +000064 {
65
Ruchira Sasankae27c3442001-08-20 21:12:49 +000066 const BasicBlock *BB = *BBI; // get the current BB
Ruchira Sasankaaca997c2001-09-30 23:28:04 +000067
Ruchira Sasanka92e251c2001-10-16 01:25:05 +000068 if(DEBUG_LV) { cout << " For BB "; printValue(BB); cout << ":" << endl; }
Ruchira Sasankaaca997c2001-09-30 23:28:04 +000069
Ruchira Sasankae27c3442001-08-20 21:12:49 +000070 // create a new BBLiveVar
71 BBLiveVar * LVBB = new BBLiveVar( BB, POId );
Ruchira Sasanka683847f2001-07-24 17:14:13 +000072
Ruchira Sasankae27c3442001-08-20 21:12:49 +000073 BB2BBLVMap[ BB ] = LVBB; // insert the pair to Map
Ruchira Sasanka683847f2001-07-24 17:14:13 +000074
Ruchira Sasankae27c3442001-08-20 21:12:49 +000075 LVBB->calcDefUseSets(); // calculates the def and in set
Ruchira Sasanka683847f2001-07-24 17:14:13 +000076
Ruchira Sasankae27c3442001-08-20 21:12:49 +000077 if(DEBUG_LV)
78 LVBB->printAllSets();
Ruchira Sasanka683847f2001-07-24 17:14:13 +000079 }
Ruchira Sasankac1daae82001-10-12 17:47:23 +000080
81 // Since the PO iterator does not discover unreachable blocks,
82 // go over the random iterator and init those blocks as well.
83 // However, LV info is not correct for those blocks (they are not
84 // analyzed)
85
86 Method::const_iterator BBRI = Meth->begin(); // random iterator for BBs
87
88 for( ; BBRI != Meth->end(); ++BBRI, ++POId) {
89
90 if( ! BB2BBLVMap[ *BBRI ] )
91 BB2BBLVMap[ *BBRI ] = new BBLiveVar( *BBRI, POId );
92
93 }
94
95
Ruchira Sasanka683847f2001-07-24 17:14:13 +000096}
97
Ruchira Sasankae27c3442001-08-20 21:12:49 +000098
Ruchira Sasanka789cebb2001-12-08 21:05:27 +000099//-----------------------------------------------------------------------------
100// do one backward pass over the CFG (for iterative analysis)
101//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000102bool MethodLiveVarInfo::doSingleBackwardPass()
103{
104 bool ResultFlow, NeedAnotherIteration = false;
105
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000106 if(DEBUG_LV)
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000107 cout << endl << " After Backward Pass ..." << endl;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000108
Chris Lattner3ff43872001-09-28 22:56:31 +0000109 po_iterator<const Method*> BBI = po_begin(Meth);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000110
Chris Lattner3ff43872001-09-28 22:56:31 +0000111 for( ; BBI != po_end(Meth) ; ++BBI)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000112 {
113
114 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
115 assert( LVBB );
116
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000117 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
118 // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000119
120 ResultFlow = false;
121
122 if( LVBB->isOutSetChanged() )
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000123 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
124
125 if( LVBB->isInSetChanged() ) // to calc Outsets of preds
126 ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000127
128 if(DEBUG_LV) LVBB->printInOutSets();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000129
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000130
131 if( ResultFlow ) NeedAnotherIteration = true;
132
133 }
134
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000135 // true if we need to reiterate over the CFG
136 return NeedAnotherIteration;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000137}
138
139
140
141
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000142//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000143// performs live var anal for a method
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000144//-----------------------------------------------------------------------------
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000145
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000146bool MethodLiveVarInfo::runOnMethod(Method *M) {
147 Meth = M;
Ruchira Sasanka92e251c2001-10-16 01:25:05 +0000148
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000149 if( DEBUG_LV) cout << "Analysing live variables ...\n";
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000150
151 // create and initialize all the BBLiveVars of the CFG
152 constructBBs();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000153
154 bool NeedAnotherIteration = false;
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000155 do { // do one pass over CFG
156 NeedAnotherIteration = doSingleBackwardPass( );
157 } while (NeedAnotherIteration ); // repeat until we need more iterations
158
159
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000160 if( DEBUG_LV) cout << "Live Variable Analysis complete!\n";
161 return false;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000162}
163
164
165
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000166//-----------------------------------------------------------------------------
167/* Following functions will give the LiveVar info for any machine instr in
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000168 a method. It should be called after a call to analyze().
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000169
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000170 Thsese functions calucluates live var info for all the machine instrs in a
171 BB when LVInfo for one inst is requested. Hence, this function is useful
172 when live var info is required for many (or all) instructions in a basic
173 block. Also, the arguments to this method does not require specific
174 iterators.
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000175*/
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000176//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000177
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000178//-----------------------------------------------------------------------------
179// Gives live variable information before a machine instruction
180//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000181const LiveVarSet *
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000182MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
183 const BasicBlock *const CurBB)
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000184{
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000185 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000186
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000187 if( LVSet ) return LVSet; // if found, just return the set
188 else {
189 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000190
191 /*if( ! MInst2LVSetBI[ MInst ] ) {
192 cerr << "\nFor BB "; printValue( CurBB);
193 cerr << "\nRequested LVSet for inst: " << *MInst;
194 }*/
195
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000196 assert( MInst2LVSetBI[ MInst ] );
197 return MInst2LVSetBI[ MInst ];
198 }
199}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000200
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000201
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000202//-----------------------------------------------------------------------------
203// Gives live variable information after a machine instruction
204//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000205const LiveVarSet *
206MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
207 const BasicBlock *const CurBB)
208{
209 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000210
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000211 if( LVSet ) return LVSet; // if found, just return the set
212 else {
213 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
214 assert( MInst2LVSetAI[ MInst ] );
215 return MInst2LVSetAI[ MInst ];
216 }
217}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000218
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000219
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000220
221//-----------------------------------------------------------------------------
222// This method calculates the live variable information for all the
223// instructions in a basic block and enter the newly constructed live
224// variable sets into a the caches ( MInst2LVSetAI, MInst2LVSetBI)
225//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000226void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
227{
228 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
229 MachineCodeForBasicBlock::const_reverse_iterator
230 MInstIterator = MIVec.rbegin();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000231
232 LiveVarSet *CurSet = new LiveVarSet();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000233 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
234 CurSet->setUnion(SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000235
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000236 // iterate over all the machine instructions in BB
237 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000238
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000239 // MInst is cur machine inst
240 const MachineInstr * MInst = *MInstIterator;
241
242 MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
243
244 CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
245 LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
246 NewSet->setUnion( CurSet ); // copy the set after T/F to it
247
248 MInst2LVSetBI[MInst] = NewSet; // record in Before Inst map
249
250 // SetAI will be used in the next iteration
251 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000252 }
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000253
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000254}
255
256
257
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000258
259
260
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000261
262
263
264
265
266
267
268
269
270
271