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 "llvm/BasicBlock.h"
15 #include "Support/PostOrderIterator.h"
20 //************************** Constructor/Destructor ***************************
23 MethodLiveVarInfo::MethodLiveVarInfo(const Method *const M) : Meth(M) {
24 assert(!M->isExternal() && "Cannot be a prototype declaration");
25 HasAnalyzed = false; // still we haven't called analyze()
30 MethodLiveVarInfo:: ~MethodLiveVarInfo()
32 // First delete all BBLiveVar objects created in constructBBs(). A new object
33 // of type BBLiveVa is created for every BasicBlock in the method
35 // hash map iterator for BB2BBLVMap
37 BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin();
39 for( ; HMI != BB2BBLVMap.end() ; HMI ++ ) {
40 if( (*HMI).first ) // delete all BBLiveVar in BB2BBLVMap
45 // Then delete all objects of type LiveVarSet created in calcLiveVarSetsForBB
46 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
47 // to return LiveVarSet's before/after a machine instruction quickly). It
48 // is sufficient to free up all LiveVarSet using only one cache since
49 // both caches refer to the same sets
51 // hash map iterator for MInst2LVSetBI
53 MInstToLiveVarSetMapType::iterator MI = MInst2LVSetBI.begin();
55 for( ; MI != MInst2LVSetBI.end() ; MI ++ ) {
56 if( (*MI).first ) // delete all LiveVarSets in MInst2LVSetBI
62 // ************************* support functions ********************************
65 //-----------------------------------------------------------------------------
66 // constructs BBLiveVars and init Def and In sets
67 //-----------------------------------------------------------------------------
69 void MethodLiveVarInfo::constructBBs()
71 unsigned int POId = 0; // Reverse Depth-first Order ID
73 po_iterator<const Method*> BBI = po_begin(Meth);
75 for( ; BBI != po_end(Meth) ; ++BBI, ++POId)
78 const BasicBlock *BB = *BBI; // get the current BB
80 if(DEBUG_LV) { cout << " For BB "; printValue(BB); cout << ":" << endl; }
82 // create a new BBLiveVar
83 BBLiveVar * LVBB = new BBLiveVar( BB, POId );
85 BB2BBLVMap[ BB ] = LVBB; // insert the pair to Map
87 LVBB->calcDefUseSets(); // calculates the def and in set
93 // Since the PO iterator does not discover unreachable blocks,
94 // go over the random iterator and init those blocks as well.
95 // However, LV info is not correct for those blocks (they are not
98 Method::const_iterator BBRI = Meth->begin(); // random iterator for BBs
100 for( ; BBRI != Meth->end(); ++BBRI, ++POId) {
102 if( ! BB2BBLVMap[ *BBRI ] )
103 BB2BBLVMap[ *BBRI ] = new BBLiveVar( *BBRI, POId );
111 //-----------------------------------------------------------------------------
112 // do one backward pass over the CFG (for iterative analysis)
113 //-----------------------------------------------------------------------------
114 bool MethodLiveVarInfo::doSingleBackwardPass()
116 bool ResultFlow, NeedAnotherIteration = false;
119 cout << endl << " After Backward Pass ..." << endl;
121 po_iterator<const Method*> BBI = po_begin(Meth);
123 for( ; BBI != po_end(Meth) ; ++BBI)
126 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
129 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
130 // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
134 if( LVBB->isOutSetChanged() )
135 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
137 if( LVBB->isInSetChanged() ) // to calc Outsets of preds
138 ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
140 if(DEBUG_LV) LVBB->printInOutSets();
143 if( ResultFlow ) NeedAnotherIteration = true;
147 // true if we need to reiterate over the CFG
148 return NeedAnotherIteration;
154 //-----------------------------------------------------------------------------
155 // performs live var anal for a method
156 //-----------------------------------------------------------------------------
157 void MethodLiveVarInfo::analyze()
159 // Don't analyze the same method twice!
160 // Later, we need to add change notification here.
167 if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
169 // create and initialize all the BBLiveVars of the CFG
172 bool NeedAnotherIteration = false;
173 do { // do one pass over CFG
174 NeedAnotherIteration = doSingleBackwardPass( );
175 } while (NeedAnotherIteration ); // repeat until we need more iterations
178 HasAnalyzed = true; // finished analysing
180 if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
185 //-----------------------------------------------------------------------------
186 /* Following functions will give the LiveVar info for any machine instr in
187 a method. It should be called after a call to analyze().
189 Thsese functions calucluates live var info for all the machine instrs in a
190 BB when LVInfo for one inst is requested. Hence, this function is useful
191 when live var info is required for many (or all) instructions in a basic
192 block. Also, the arguments to this method does not require specific
195 //-----------------------------------------------------------------------------
197 //-----------------------------------------------------------------------------
198 // Gives live variable information before a machine instruction
199 //-----------------------------------------------------------------------------
201 MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
202 const BasicBlock *const CurBB)
204 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
206 if( LVSet ) return LVSet; // if found, just return the set
208 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
210 /*if( ! MInst2LVSetBI[ MInst ] ) {
211 cerr << "\nFor BB "; printValue( CurBB);
212 cerr << "\nRequested LVSet for inst: " << *MInst;
215 assert( MInst2LVSetBI[ MInst ] );
216 return MInst2LVSetBI[ MInst ];
221 //-----------------------------------------------------------------------------
222 // Gives live variable information after a machine instruction
223 //-----------------------------------------------------------------------------
225 MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
226 const BasicBlock *const CurBB)
228 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
230 if( LVSet ) return LVSet; // if found, just return the set
232 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
233 assert( MInst2LVSetAI[ MInst ] );
234 return MInst2LVSetAI[ MInst ];
240 //-----------------------------------------------------------------------------
241 // This method calculates the live variable information for all the
242 // instructions in a basic block and enter the newly constructed live
243 // variable sets into a the caches ( MInst2LVSetAI, MInst2LVSetBI)
244 //-----------------------------------------------------------------------------
245 void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
247 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
248 MachineCodeForBasicBlock::const_reverse_iterator
249 MInstIterator = MIVec.rbegin();
251 LiveVarSet *CurSet = new LiveVarSet();
252 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
253 CurSet->setUnion(SetAI); // CurSet now contains OutSet
255 // iterate over all the machine instructions in BB
256 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
258 // MInst is cur machine inst
259 const MachineInstr * MInst = *MInstIterator;
261 MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
263 CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
264 LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
265 NewSet->setUnion( CurSet ); // copy the set after T/F to it
267 MInst2LVSetBI[MInst] = NewSet; // record in Before Inst map
269 // SetAI will be used in the next iteration