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 AnalysisID MethodLiveVarInfo::ID(AnalysisID::create<MethodLiveVarInfo>());
22 void MethodLiveVarInfo::releaseMemory() {
23 // First delete all BBLiveVar objects created in constructBBs(). A new object
24 // of type BBLiveVa is created for every BasicBlock in the method
26 // hash map iterator for BB2BBLVMap
28 BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin();
30 for( ; HMI != BB2BBLVMap.end(); ++HMI)
31 delete HMI->second; // delete all BBLiveVar in BB2BBLVMap
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
41 // hash map iterator for MInst2LVSetBI
43 MInstToLiveVarSetMapType::iterator MI = MInst2LVSetBI.begin();
45 for( ; MI != MInst2LVSetBI.end(); ++MI)
46 delete MI->second; // delete all LiveVarSets in MInst2LVSetBI
48 MInst2LVSetBI.clear();
49 MInst2LVSetAI.clear();
53 //-----------------------------------------------------------------------------
54 // constructs BBLiveVars and init Def and In sets
55 //-----------------------------------------------------------------------------
57 void MethodLiveVarInfo::constructBBs()
59 unsigned int POId = 0; // Reverse Depth-first Order ID
61 po_iterator<const Method*> BBI = po_begin(Meth);
63 for( ; BBI != po_end(Meth) ; ++BBI, ++POId)
66 const BasicBlock *BB = *BBI; // get the current BB
68 if(DEBUG_LV) { cout << " For BB "; printValue(BB); cout << ":" << endl; }
70 // create a new BBLiveVar
71 BBLiveVar * LVBB = new BBLiveVar( BB, POId );
73 BB2BBLVMap[ BB ] = LVBB; // insert the pair to Map
75 LVBB->calcDefUseSets(); // calculates the def and in set
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
86 Method::const_iterator BBRI = Meth->begin(); // random iterator for BBs
88 for( ; BBRI != Meth->end(); ++BBRI, ++POId) {
90 if( ! BB2BBLVMap[ *BBRI ] )
91 BB2BBLVMap[ *BBRI ] = new BBLiveVar( *BBRI, POId );
99 //-----------------------------------------------------------------------------
100 // do one backward pass over the CFG (for iterative analysis)
101 //-----------------------------------------------------------------------------
102 bool MethodLiveVarInfo::doSingleBackwardPass()
104 bool ResultFlow, NeedAnotherIteration = false;
107 cout << endl << " After Backward Pass ..." << endl;
109 po_iterator<const Method*> BBI = po_begin(Meth);
111 for( ; BBI != po_end(Meth) ; ++BBI)
114 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
117 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
118 // cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
122 if( LVBB->isOutSetChanged() )
123 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
125 if( LVBB->isInSetChanged() ) // to calc Outsets of preds
126 ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
128 if(DEBUG_LV) LVBB->printInOutSets();
131 if( ResultFlow ) NeedAnotherIteration = true;
135 // true if we need to reiterate over the CFG
136 return NeedAnotherIteration;
142 //-----------------------------------------------------------------------------
143 // performs live var anal for a method
144 //-----------------------------------------------------------------------------
146 bool MethodLiveVarInfo::runOnMethod(Method *M) {
149 if( DEBUG_LV) cout << "Analysing live variables ...\n";
151 // create and initialize all the BBLiveVars of the CFG
154 bool NeedAnotherIteration = false;
155 do { // do one pass over CFG
156 NeedAnotherIteration = doSingleBackwardPass( );
157 } while (NeedAnotherIteration ); // repeat until we need more iterations
160 if( DEBUG_LV) cout << "Live Variable Analysis complete!\n";
166 //-----------------------------------------------------------------------------
167 /* Following functions will give the LiveVar info for any machine instr in
168 a method. It should be called after a call to analyze().
170 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
176 //-----------------------------------------------------------------------------
178 //-----------------------------------------------------------------------------
179 // Gives live variable information before a machine instruction
180 //-----------------------------------------------------------------------------
182 MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
183 const BasicBlock *const CurBB)
185 const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
187 if( LVSet ) return LVSet; // if found, just return the set
189 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
191 /*if( ! MInst2LVSetBI[ MInst ] ) {
192 cerr << "\nFor BB "; printValue( CurBB);
193 cerr << "\nRequested LVSet for inst: " << *MInst;
196 assert( MInst2LVSetBI[ MInst ] );
197 return MInst2LVSetBI[ MInst ];
202 //-----------------------------------------------------------------------------
203 // Gives live variable information after a machine instruction
204 //-----------------------------------------------------------------------------
206 MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
207 const BasicBlock *const CurBB)
209 const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
211 if( LVSet ) return LVSet; // if found, just return the set
213 calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
214 assert( MInst2LVSetAI[ MInst ] );
215 return MInst2LVSetAI[ MInst ];
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 //-----------------------------------------------------------------------------
226 void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
228 const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
229 MachineCodeForBasicBlock::const_reverse_iterator
230 MInstIterator = MIVec.rbegin();
232 LiveVarSet *CurSet = new LiveVarSet();
233 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
234 CurSet->setUnion(SetAI); // CurSet now contains OutSet
236 // iterate over all the machine instructions in BB
237 for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
239 // MInst is cur machine inst
240 const MachineInstr * MInst = *MInstIterator;
242 MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
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
248 MInst2LVSetBI[MInst] = NewSet; // record in Before Inst map
250 // SetAI will be used in the next iteration