1 //===-- MethodLiveVarInfo.cpp - Live Variable Analysis for a Method -------===//
3 // This is the interface to method level live variable information that is
4 // provided by live variable analysis.
6 //===----------------------------------------------------------------------===//
8 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
10 #include "llvm/CodeGen/MachineInstr.h"
11 #include "llvm/BasicBlock.h"
12 #include "Support/PostOrderIterator.h"
13 #include "Support/SetOperations.h"
16 AnalysisID MethodLiveVarInfo::ID(AnalysisID::create<MethodLiveVarInfo>());
18 //-----------------------------------------------------------------------------
20 //-----------------------------------------------------------------------------
22 // gets OutSet of a BB
23 const ValueSet &MethodLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
24 return BB2BBLVMap.find(BB)->second->getOutSet();
28 const ValueSet &MethodLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
29 return BB2BBLVMap.find(BB)->second->getInSet();
33 //-----------------------------------------------------------------------------
34 // Performs live var analysis for a method
35 //-----------------------------------------------------------------------------
37 bool MethodLiveVarInfo::runOnMethod(Method *M) {
38 if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
40 // create and initialize all the BBLiveVars of the CFG
43 while (doSingleBackwardPass(M))
44 ; // Iterate until we are done.
46 if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
51 //-----------------------------------------------------------------------------
52 // constructs BBLiveVars and init Def and In sets
53 //-----------------------------------------------------------------------------
55 void MethodLiveVarInfo::constructBBs(const Method *M) {
56 unsigned int POId = 0; // Reverse Depth-first Order ID
58 for(po_iterator<const Method*> BBI = po_begin(M), BBE = po_end(M);
59 BBI != BBE; ++BBI, ++POId) {
60 const BasicBlock *BB = *BBI; // get the current BB
62 if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
64 // create a new BBLiveVar
65 BBLiveVar *LVBB = new BBLiveVar(BB, POId);
66 BB2BBLVMap[BB] = LVBB; // insert the pair to Map
72 // Since the PO iterator does not discover unreachable blocks,
73 // go over the random iterator and init those blocks as well.
74 // However, LV info is not correct for those blocks (they are not
77 for (Method::const_iterator BBRI = M->begin(), BBRE = M->end();
78 BBRI != BBRE; ++BBRI, ++POId)
79 if (!BB2BBLVMap[*BBRI]) // Not yet processed?
80 BB2BBLVMap[*BBRI] = new BBLiveVar(*BBRI, POId);
84 //-----------------------------------------------------------------------------
85 // do one backward pass over the CFG (for iterative analysis)
86 //-----------------------------------------------------------------------------
88 bool MethodLiveVarInfo::doSingleBackwardPass(const Method *M) {
89 if (DEBUG_LV) std::cerr << "\n After Backward Pass ...\n";
91 bool NeedAnotherIteration = false;
92 for (po_iterator<const Method*> BBI = po_begin(M); BBI != po_end(M) ; ++BBI) {
93 BBLiveVar *LVBB = BB2BBLVMap[*BBI];
94 assert(LVBB && "BasicBlock information not set for block!");
96 if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
98 if(LVBB->isOutSetChanged())
99 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
101 if (LVBB->isInSetChanged()) // to calc Outsets of preds
102 NeedAnotherIteration |= LVBB->applyFlowFunc(BB2BBLVMap);
104 if (DEBUG_LV) LVBB->printInOutSets();
107 // true if we need to reiterate over the CFG
108 return NeedAnotherIteration;
112 void MethodLiveVarInfo::releaseMemory() {
113 // First delete all BBLiveVar objects created in constructBBs(). A new object
114 // of type BBLiveVar is created for every BasicBlock in the method
116 for (std::map<const BasicBlock *, BBLiveVar *>::iterator
117 HMI = BB2BBLVMap.begin(),
118 HME = BB2BBLVMap.end(); HMI != HME; ++HMI)
119 delete HMI->second; // delete all BBLiveVar in BB2BBLVMap
123 // Then delete all objects of type ValueSet created in calcLiveVarSetsForBB
124 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
125 // to return ValueSet's before/after a machine instruction quickly). It
126 // is sufficient to free up all ValueSet using only one cache since
127 // both caches refer to the same sets
129 for (std::map<const MachineInstr*, const ValueSet*>::iterator
130 MI = MInst2LVSetBI.begin(),
131 ME = MInst2LVSetBI.end(); MI != ME; ++MI)
132 delete MI->second; // delete all ValueSets in MInst2LVSetBI
134 MInst2LVSetBI.clear();
135 MInst2LVSetAI.clear();
141 //-----------------------------------------------------------------------------
142 // Following functions will give the LiveVar info for any machine instr in
143 // a method. It should be called after a call to analyze().
145 // Thsese functions calucluates live var info for all the machine instrs in a
146 // BB when LVInfo for one inst is requested. Hence, this function is useful
147 // when live var info is required for many (or all) instructions in a basic
148 // block. Also, the arguments to this method does not require specific
150 //-----------------------------------------------------------------------------
152 //-----------------------------------------------------------------------------
153 // Gives live variable information before a machine instruction
154 //-----------------------------------------------------------------------------
157 MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
158 const BasicBlock *BB) {
159 if (const ValueSet *LVSet = MInst2LVSetBI[MInst]) {
160 return *LVSet; // if found, just return the set
162 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
163 return *MInst2LVSetBI[MInst];
168 //-----------------------------------------------------------------------------
169 // Gives live variable information after a machine instruction
170 //-----------------------------------------------------------------------------
172 MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
173 const BasicBlock *BB) {
175 if (const ValueSet *LVSet = MInst2LVSetAI[MI]) {
176 return *LVSet; // if found, just return the set
178 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
179 return *MInst2LVSetAI[MI];
183 // This function applies a machine instr to a live var set (accepts OutSet) and
184 // makes necessary changes to it (produces InSet). Note that two for loops are
185 // used to first kill all defs and then to add all uses. This is because there
186 // can be instructions like Val = Val + 1 since we allow multipe defs to a
187 // machine instruction operand.
189 static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
190 for (MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done(); ++OpI) {
191 if (OpI.isDef()) // kill only if this operand is a def
192 LVS.insert(*OpI); // this definition kills any uses
195 // do for implicit operands as well
196 for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
197 if (MInst->implicitRefIsDefined(i))
198 LVS.erase(MInst->getImplicitRef(i));
201 for (MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done(); ++OpI) {
202 if (isa<BasicBlock>(*OpI)) continue; // don't process labels
204 if (!OpI.isDef()) // add only if this operand is a use
205 LVS.insert(*OpI); // An operand is a use - so add to use set
208 // do for implicit operands as well
209 for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
210 if (!MInst->implicitRefIsDefined(i))
211 LVS.insert(MInst->getImplicitRef(i));
215 //-----------------------------------------------------------------------------
216 // This method calculates the live variable information for all the
217 // instructions in a basic block and enter the newly constructed live
218 // variable sets into a the caches (MInst2LVSetAI, MInst2LVSetBI)
219 //-----------------------------------------------------------------------------
221 void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
222 const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
224 ValueSet *CurSet = new ValueSet();
225 const ValueSet *SetAI = &getOutSetOfBB(BB); // init SetAI with OutSet
226 set_union(*CurSet, *SetAI); // CurSet now contains OutSet
228 // iterate over all the machine instructions in BB
229 for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
230 MIE = MIVec.rend(); MII != MIE; ++MII) {
231 // MI is cur machine inst
232 const MachineInstr *MI = *MII;
234 MInst2LVSetAI[MI] = SetAI; // record in After Inst map
236 applyTranferFuncForMInst(*CurSet, MI); // apply the transfer Func
237 ValueSet *NewSet = new ValueSet(); // create a new set and
238 set_union(*NewSet, *CurSet); // copy the set after T/F to it
240 MInst2LVSetBI[MI] = NewSet; // record in Before Inst map
242 // SetAI will be used in the next iteration