1 /* Title: MethodLiveVarInfo.cpp
2 Author: Ruchira Sasanka
6 This is the interface for live variable info of a method that is required
7 by any other part of the compiler.
12 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
13 #include "llvm/CodeGen/MachineInstr.h"
14 #include "Support/PostOrderIterator.h"
17 //************************** Constructor/Destructor ***************************
20 MethodLiveVarInfo::MethodLiveVarInfo(const Method *const M) : Meth(M),
23 assert(! M->isExternal() ); // cannot be a prototype decleration
24 HasAnalyzed = false; // still we haven't called analyze()
29 MethodLiveVarInfo:: ~MethodLiveVarInfo()
31 // First delete all BBLiveVar objects created in constructBBs(). A new object
32 // of type BBLiveVa is created for every BasicBlock in the method
34 // hash map iterator for BB2BBLVMap
36 BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin();
38 for( ; HMI != BB2BBLVMap.end() ; HMI ++ ) {
39 if( (*HMI).first ) // delete all BBLiveVar in BB2BBLVMap
44 // Then delete all objects of type LiveVarSet created in calcLiveVarSetsForBB
45 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
46 // to return LiveVarSet's before/after a machine instruction quickly). It
47 // is sufficient to free up all LiveVarSet using only one cache since
48 // both caches refer to the same sets
50 // hash map iterator for MInst2LVSetBI
52 MInstToLiveVarSetMapType::iterator MI = MInst2LVSetBI.begin();
54 for( ; MI != MInst2LVSetBI.end() ; MI ++ ) {
55 if( (*MI).first ) // delete all LiveVarSets in MInst2LVSetBI
63 // ************************* support functions ********************************
66 //-----------------------------------------------------------------------------
67 // constructs BBLiveVars and init Def and In sets
68 //-----------------------------------------------------------------------------
70 void MethodLiveVarInfo::constructBBs()
72 unsigned int POId = 0; // Reverse Depth-first Order ID
74 po_iterator<const Method*> BBI = po_begin(Meth);
76 for( ; BBI != po_end(Meth) ; ++BBI, ++POId)
79 const BasicBlock *BB = *BBI; // get the current BB
81 if(DEBUG_LV) { cout << " For BB "; printValue(BB); cout << ":" << endl; }
83 // create a new BBLiveVar
84 BBLiveVar * LVBB = new BBLiveVar( BB, POId );
86 BB2BBLVMap[ BB ] = LVBB; // insert the pair to Map
88 LVBB->calcDefUseSets(); // calculates the def and in set
94 // Since the PO iterator does not discover unreachable blocks,
95 // go over the random iterator and init those blocks as well.
96 // However, LV info is not correct for those blocks (they are not
99 Method::const_iterator BBRI = Meth->begin(); // random iterator for BBs
101 for( ; BBRI != Meth->end(); ++BBRI, ++POId) {
103 if( ! BB2BBLVMap[ *BBRI ] )
104 BB2BBLVMap[ *BBRI ] = new BBLiveVar( *BBRI, POId );
112 //-----------------------------------------------------------------------------
113 // do one backward pass over the CFG (for iterative analysis)
114 //-----------------------------------------------------------------------------
115 bool MethodLiveVarInfo::doSingleBackwardPass()
117 bool ResultFlow, NeedAnotherIteration = false;
120 cout << endl << " After Backward Pass ..." << endl;
122 po_iterator<const Method*> BBI = po_begin(Meth);
124 for( ; BBI != po_end(Meth) ; ++BBI)
127 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
130 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
131 // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
135 if( LVBB->isOutSetChanged() )
136 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
138 if( LVBB->isInSetChanged() ) // to calc Outsets of preds
139 ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
141 if(DEBUG_LV) LVBB->printInOutSets();
144 if( ResultFlow ) NeedAnotherIteration = true;
148 // true if we need to reiterate over the CFG
149 return NeedAnotherIteration;
155 //-----------------------------------------------------------------------------
156 // performs live var anal for a method
157 //-----------------------------------------------------------------------------
158 void MethodLiveVarInfo::analyze()
160 // Don't analyze the same method twice!
161 // Later, we need to add change notification here.
168 if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
170 // create and initialize all the BBLiveVars of the CFG
173 bool NeedAnotherIteration = false;
174 do { // do one pass over CFG
175 NeedAnotherIteration = doSingleBackwardPass( );
176 } while (NeedAnotherIteration ); // repeat until we need more iterations
179 HasAnalyzed = true; // finished analysing
181 if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
186 //-----------------------------------------------------------------------------
187 /* Following functions will give the LiveVar info for any machine instr in
188 a method. It should be called after a call to analyze().
190 Thsese functions calucluates live var info for all the machine instrs in a
191 BB when LVInfo for one inst is requested. Hence, this function is useful
192 when live var info is required for many (or all) instructions in a basic
193 block. Also, the arguments to this method does not require specific
196 //-----------------------------------------------------------------------------
198 //-----------------------------------------------------------------------------
199 // Gives live variable information before a machine instruction
200 //-----------------------------------------------------------------------------
202 MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
203 const BasicBlock *const CurBB)
205 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
207 if( LVSet ) return LVSet; // if found, just return the set
209 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
211 /*if( ! MInst2LVSetBI[ MInst ] ) {
212 cerr << "\nFor BB "; printValue( CurBB);
213 cerr << "\nRequested LVSet for inst: " << *MInst;
216 assert( MInst2LVSetBI[ MInst ] );
217 return MInst2LVSetBI[ MInst ];
222 //-----------------------------------------------------------------------------
223 // Gives live variable information after a machine instruction
224 //-----------------------------------------------------------------------------
226 MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
227 const BasicBlock *const CurBB)
229 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
231 if( LVSet ) return LVSet; // if found, just return the set
233 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
234 assert( MInst2LVSetAI[ MInst ] );
235 return MInst2LVSetAI[ MInst ];
241 //-----------------------------------------------------------------------------
242 // This method calculates the live variable information for all the
243 // instructions in a basic block and enter the newly constructed live
244 // variable sets into a the caches ( MInst2LVSetAI, MInst2LVSetBI)
245 //-----------------------------------------------------------------------------
246 void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
248 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
249 MachineCodeForBasicBlock::const_reverse_iterator
250 MInstIterator = MIVec.rbegin();
252 LiveVarSet *CurSet = new LiveVarSet();
253 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
254 CurSet->setUnion(SetAI); // CurSet now contains OutSet
256 // iterate over all the machine instructions in BB
257 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
259 // MInst is cur machine inst
260 const MachineInstr * MInst = *MInstIterator;
262 MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
264 CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
265 LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
266 NewSet->setUnion( CurSet ); // copy the set after T/F to it
268 MInst2LVSetBI[MInst] = NewSet; // record in Before Inst map
270 // SetAI will be used in the next iteration