1 //===-- MethodLiveVarInfo.cpp - Live Variable Analysis for a Function -----===//
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 "llvm/Support/CFG.h"
13 #include "Support/PostOrderIterator.h"
14 #include "Support/SetOperations.h"
17 AnalysisID MethodLiveVarInfo::ID(AnalysisID::create<MethodLiveVarInfo>());
19 cl::Enum<LiveVarDebugLevel_t> DEBUG_LV("dlivevar", cl::NoFlags,
20 "enable live-variable debugging information",
21 clEnumValN(LV_DEBUG_None , "n", "disable debug output"),
22 clEnumValN(LV_DEBUG_Normal , "y", "enable debug output"),
23 clEnumValN(LV_DEBUG_Instr, "i", "print live-var sets before/after every machine instrn"),
24 clEnumValN(LV_DEBUG_Verbose, "v", "print def, use sets for every instrn also"), 0);
26 //-----------------------------------------------------------------------------
28 //-----------------------------------------------------------------------------
30 // gets OutSet of a BB
31 const ValueSet &MethodLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
32 return BBLiveVar::GetFromBB(BB)->getOutSet();
36 const ValueSet &MethodLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
37 return BBLiveVar::GetFromBB(BB)->getInSet();
41 //-----------------------------------------------------------------------------
42 // Performs live var analysis for a method
43 //-----------------------------------------------------------------------------
45 bool MethodLiveVarInfo::runOnMethod(Function *Meth) {
47 if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
49 // create and initialize all the BBLiveVars of the CFG
53 while (doSingleBackwardPass(Meth, iter++))
54 ; // Iterate until we are done.
56 if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
61 //-----------------------------------------------------------------------------
62 // constructs BBLiveVars and init Def and In sets
63 //-----------------------------------------------------------------------------
65 void MethodLiveVarInfo::constructBBs(const Function *M) {
66 unsigned int POId = 0; // Reverse Depth-first Order ID
68 for(po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
69 BBI != BBE; ++BBI, ++POId) {
70 const BasicBlock *BB = *BBI; // get the current BB
72 if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
74 // create a new BBLiveVar
75 BBLiveVar *LVBB = BBLiveVar::CreateOnBB(BB, POId);
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 for (Function::const_iterator BBRI = M->begin(), BBRE = M->end();
87 BBRI != BBRE; ++BBRI, ++POId)
88 if (!BBLiveVar::GetFromBB(*BBRI)) // Not yet processed?
89 BBLiveVar::CreateOnBB(*BBRI, POId);
93 //-----------------------------------------------------------------------------
94 // do one backward pass over the CFG (for iterative analysis)
95 //-----------------------------------------------------------------------------
97 bool MethodLiveVarInfo::doSingleBackwardPass(const Function *M, unsigned int iter) {
98 if (DEBUG_LV) std::cerr << "\n After Backward Pass " << iter << "...\n";
100 bool NeedAnotherIteration = false;
101 for (po_iterator<const Function*> BBI = po_begin(M); BBI != po_end(M) ; ++BBI) {
102 BBLiveVar *LVBB = BBLiveVar::GetFromBB(*BBI);
103 assert(LVBB && "BasicBlock information not set for block!");
105 if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
107 // InSets are initialized to "GenSet". Recompute only if OutSet changed.
108 if(LVBB->isOutSetChanged())
109 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
111 // OutSets are initialized to EMPTY. Recompute on first iter or if InSet changed.
112 if (iter == 0 || LVBB->isInSetChanged()) // to calc Outsets of preds
113 NeedAnotherIteration = LVBB->applyFlowFunc() || NeedAnotherIteration;
115 if (DEBUG_LV) LVBB->printInOutSets();
118 // true if we need to reiterate over the CFG
119 return NeedAnotherIteration;
123 void MethodLiveVarInfo::releaseMemory() {
124 // First remove all BBLiveVar annotations created in constructBBs().
126 for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
127 BBLiveVar::RemoveFromBB(*I);
130 // Then delete all objects of type ValueSet created in calcLiveVarSetsForBB
131 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
132 // to return ValueSet's before/after a machine instruction quickly). It
133 // is sufficient to free up all ValueSet using only one cache since
134 // both caches refer to the same sets
136 for (std::map<const MachineInstr*, const ValueSet*>::iterator
137 MI = MInst2LVSetBI.begin(),
138 ME = MInst2LVSetBI.end(); MI != ME; ++MI)
139 delete MI->second; // delete all ValueSets in MInst2LVSetBI
141 MInst2LVSetBI.clear();
142 MInst2LVSetAI.clear();
148 //-----------------------------------------------------------------------------
149 // Following functions will give the LiveVar info for any machine instr in
150 // a method. It should be called after a call to analyze().
152 // Thsese functions calucluates live var info for all the machine instrs in a
153 // BB when LVInfo for one inst is requested. Hence, this function is useful
154 // when live var info is required for many (or all) instructions in a basic
155 // block. Also, the arguments to this method does not require specific
157 //-----------------------------------------------------------------------------
159 //-----------------------------------------------------------------------------
160 // Gives live variable information before a machine instruction
161 //-----------------------------------------------------------------------------
164 MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
165 const BasicBlock *BB) {
166 if (const ValueSet *LVSet = MInst2LVSetBI[MInst]) {
167 return *LVSet; // if found, just return the set
169 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
170 return *MInst2LVSetBI[MInst];
175 //-----------------------------------------------------------------------------
176 // Gives live variable information after a machine instruction
177 //-----------------------------------------------------------------------------
179 MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
180 const BasicBlock *BB) {
182 if (const ValueSet *LVSet = MInst2LVSetAI[MI]) {
183 return *LVSet; // if found, just return the set
185 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
186 return *MInst2LVSetAI[MI];
190 // This function applies a machine instr to a live var set (accepts OutSet) and
191 // makes necessary changes to it (produces InSet). Note that two for loops are
192 // used to first kill all defs and then to add all uses. This is because there
193 // can be instructions like Val = Val + 1 since we allow multipe defs to a
194 // machine instruction operand.
196 static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
197 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
198 OpE = MInst->end(); OpI != OpE; ++OpI) {
199 if (OpI.isDef()) // kill only if this operand is a def
200 LVS.erase(*OpI); // this definition kills any uses
203 // do for implicit operands as well
204 for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
205 if (MInst->implicitRefIsDefined(i))
206 LVS.erase(MInst->getImplicitRef(i));
209 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
210 OpE = MInst->end(); OpI != OpE; ++OpI) {
211 if (isa<BasicBlock>(*OpI)) continue; // don't process labels
213 if (!OpI.isDef()) // add only if this operand is a use
214 LVS.insert(*OpI); // An operand is a use - so add to use set
217 // do for implicit operands as well
218 for (unsigned i = 0; i < MInst->getNumImplicitRefs(); ++i) {
219 if (!MInst->implicitRefIsDefined(i))
220 LVS.insert(MInst->getImplicitRef(i));
224 //-----------------------------------------------------------------------------
225 // This method calculates the live variable information for all the
226 // instructions in a basic block and enter the newly constructed live
227 // variable sets into a the caches (MInst2LVSetAI, MInst2LVSetBI)
228 //-----------------------------------------------------------------------------
230 void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
231 const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
233 if (DEBUG_LV >= LV_DEBUG_Instr) {
234 std::cerr << endl << "======For BB " << BB->getName() << ": Live var sets for instructions======" << endl;
237 ValueSet *CurSet = new ValueSet();
238 const ValueSet *SetAI = &getOutSetOfBB(BB); // init SetAI with OutSet
239 set_union(*CurSet, *SetAI); // CurSet now contains OutSet
241 // iterate over all the machine instructions in BB
242 for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
243 MIE = MIVec.rend(); MII != MIE; ++MII) {
244 // MI is cur machine inst
245 const MachineInstr *MI = *MII;
247 MInst2LVSetAI[MI] = SetAI; // record in After Inst map
249 applyTranferFuncForMInst(*CurSet, MI); // apply the transfer Func
250 ValueSet *NewSet = new ValueSet(); // create a new set and
251 set_union(*NewSet, *CurSet); // copy the set after T/F to it
253 MInst2LVSetBI[MI] = NewSet; // record in Before Inst map
255 if (DEBUG_LV >= LV_DEBUG_Instr) {
256 std::cerr << endl << "Live var sets before/after instruction " << *MI;
257 cerr << " Before: "; printSet(*NewSet); cerr << endl;
258 cerr << " After : "; printSet(*SetAI); cerr << endl;
261 // SetAI will be used in the next iteration