blob: 63c20194df02b034a7ddbc9540574254b3e65b03 [file] [log] [blame]
Chris Lattnerf57b8452002-04-27 06:56:12 +00001//===-- FunctionLiveVarInfo.cpp - Live Variable Analysis for a Function ---===//
Chris Lattner5e5dfa32002-02-05 02:51:01 +00002//
Chris Lattnerf57b8452002-04-27 06:56:12 +00003// This is the interface to function level live variable information that is
Chris Lattner5e5dfa32002-02-05 02:51:01 +00004// provided by live variable analysis.
5//
6//===----------------------------------------------------------------------===//
Ruchira Sasanka683847f2001-07-24 17:14:13 +00007
Chris Lattner92ba2aa2003-01-14 23:05:08 +00008#include "llvm/CodeGen/FunctionLiveVarInfo.h"
Ruchira Sasankae27c3442001-08-20 21:12:49 +00009#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner601fc7c2002-10-28 18:01:21 +000010#include "llvm/CodeGen/MachineFunction.h"
Chris Lattner221d6882002-02-12 21:07:25 +000011#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000012#include "Support/PostOrderIterator.h"
Chris Lattner7471a7b2002-02-05 03:35:53 +000013#include "Support/SetOperations.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000014#include "Support/CommandLine.h"
Chris Lattner92ba2aa2003-01-14 23:05:08 +000015#include "BBLiveVar.h"
Ruchira Sasanka683847f2001-07-24 17:14:13 +000016
Chris Lattner1e435162002-07-26 21:12:44 +000017static RegisterAnalysis<FunctionLiveVarInfo>
18X("livevar", "Live Variable Analysis");
Ruchira Sasanka683847f2001-07-24 17:14:13 +000019
Chris Lattner70e60cb2002-05-22 17:08:27 +000020LiveVarDebugLevel_t DEBUG_LV;
21
Chris Lattner5ff62e92002-07-22 02:10:13 +000022static cl::opt<LiveVarDebugLevel_t, true>
23DEBUG_LV_opt("dlivevar", cl::Hidden, cl::location(DEBUG_LV),
24 cl::desc("enable live-variable debugging information"),
25 cl::values(
26clEnumValN(LV_DEBUG_None , "n", "disable debug output"),
27clEnumValN(LV_DEBUG_Normal , "y", "enable debug output"),
28clEnumValN(LV_DEBUG_Instr, "i", "print live-var sets before/after "
29 "every machine instrn"),
30clEnumValN(LV_DEBUG_Verbose, "v", "print def, use sets for every instrn also"),
31 0));
Vikram S. Adve9cf85a72002-03-18 03:45:41 +000032
Chris Lattner70e60cb2002-05-22 17:08:27 +000033
34
Chris Lattnerab584112002-02-05 00:34:50 +000035//-----------------------------------------------------------------------------
36// Accessor Functions
37//-----------------------------------------------------------------------------
38
39// gets OutSet of a BB
Chris Lattner483e14e2002-04-27 07:27:19 +000040const ValueSet &FunctionLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
Chris Lattner18961502002-06-25 16:12:52 +000041 return BBLiveVar::GetFromBB(*BB)->getOutSet();
Chris Lattnerab584112002-02-05 00:34:50 +000042}
43
44// gets InSet of a BB
Chris Lattner483e14e2002-04-27 07:27:19 +000045const ValueSet &FunctionLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
Chris Lattner18961502002-06-25 16:12:52 +000046 return BBLiveVar::GetFromBB(*BB)->getInSet();
Chris Lattnerab584112002-02-05 00:34:50 +000047}
48
Chris Lattnerbdfd3282002-02-04 20:49:04 +000049
50//-----------------------------------------------------------------------------
Chris Lattnerf57b8452002-04-27 06:56:12 +000051// Performs live var analysis for a function
Chris Lattnerbdfd3282002-02-04 20:49:04 +000052//-----------------------------------------------------------------------------
53
Chris Lattner18961502002-06-25 16:12:52 +000054bool FunctionLiveVarInfo::runOnFunction(Function &F) {
55 M = &F;
Chris Lattnera51c7a82002-02-04 23:31:16 +000056 if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000057
58 // create and initialize all the BBLiveVars of the CFG
Chris Lattner18961502002-06-25 16:12:52 +000059 constructBBs(M);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000060
Vikram S. Adve9cf85a72002-03-18 03:45:41 +000061 unsigned int iter=0;
Chris Lattner18961502002-06-25 16:12:52 +000062 while (doSingleBackwardPass(M, iter++))
Chris Lattnerbdfd3282002-02-04 20:49:04 +000063 ; // Iterate until we are done.
64
Chris Lattnera51c7a82002-02-04 23:31:16 +000065 if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000066 return false;
67}
68
69
70//-----------------------------------------------------------------------------
71// constructs BBLiveVars and init Def and In sets
72//-----------------------------------------------------------------------------
73
Chris Lattner601fc7c2002-10-28 18:01:21 +000074void FunctionLiveVarInfo::constructBBs(const Function *F) {
75 unsigned POId = 0; // Reverse Depth-first Order ID
76 std::map<const BasicBlock*, unsigned> PONumbering;
Chris Lattnerbdfd3282002-02-04 20:49:04 +000077
Chris Lattner601fc7c2002-10-28 18:01:21 +000078 for (po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
79 BBI != BBE; ++BBI)
80 PONumbering[*BBI] = POId++;
81
82 MachineFunction &MF = MachineFunction::get(F);
83 for (MachineFunction::iterator I = MF.begin(), E = MF.end(); I != E; ++I) {
84 const BasicBlock &BB = *I->getBasicBlock(); // get the current BB
Chris Lattner0665a5f2002-02-05 01:43:49 +000085 if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000086
Chris Lattner601fc7c2002-10-28 18:01:21 +000087 BBLiveVar *LVBB;
88 std::map<const BasicBlock*, unsigned>::iterator POI = PONumbering.find(&BB);
89 if (POI != PONumbering.end()) {
90 // create a new BBLiveVar
91 LVBB = BBLiveVar::CreateOnBB(BB, *I, POId);
92 } else {
93 // The PO iterator does not discover unreachable blocks, but the random
94 // iterator later may access these blocks. We must make sure to
95 // initialize unreachable blocks as well. However, LV info is not correct
96 // for those blocks (they are not analyzed)
97 //
98 LVBB = BBLiveVar::CreateOnBB(BB, *I, ++POId);
99 }
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000100
Chris Lattnera51c7a82002-02-04 23:31:16 +0000101 if (DEBUG_LV)
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000102 LVBB->printAllSets();
103 }
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000104}
105
106
107//-----------------------------------------------------------------------------
108// do one backward pass over the CFG (for iterative analysis)
109//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000110
Chris Lattner483e14e2002-04-27 07:27:19 +0000111bool FunctionLiveVarInfo::doSingleBackwardPass(const Function *M,
112 unsigned iter) {
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000113 if (DEBUG_LV) std::cerr << "\n After Backward Pass " << iter << "...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000114
Chris Lattnera51c7a82002-02-04 23:31:16 +0000115 bool NeedAnotherIteration = false;
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000116 for (po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
117 BBI != BBE; ++BBI) {
Chris Lattner18961502002-06-25 16:12:52 +0000118 BBLiveVar *LVBB = BBLiveVar::GetFromBB(**BBI);
Chris Lattnera51c7a82002-02-04 23:31:16 +0000119 assert(LVBB && "BasicBlock information not set for block!");
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000120
Chris Lattnera51c7a82002-02-04 23:31:16 +0000121 if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000122
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000123 // InSets are initialized to "GenSet". Recompute only if OutSet changed.
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000124 if(LVBB->isOutSetChanged())
125 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000126
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000127 // OutSets are initialized to EMPTY. Recompute on first iter or if InSet
128 // changed.
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000129 if (iter == 0 || LVBB->isInSetChanged()) // to calc Outsets of preds
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000130 NeedAnotherIteration |= LVBB->applyFlowFunc();
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000131
Chris Lattnera51c7a82002-02-04 23:31:16 +0000132 if (DEBUG_LV) LVBB->printInOutSets();
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000133 }
134
135 // true if we need to reiterate over the CFG
136 return NeedAnotherIteration;
137}
138
139
Chris Lattner483e14e2002-04-27 07:27:19 +0000140void FunctionLiveVarInfo::releaseMemory() {
Chris Lattner6357a3f2002-02-05 06:52:25 +0000141 // First remove all BBLiveVar annotations created in constructBBs().
142 if (M)
Chris Lattnerb7653df2002-04-08 22:03:57 +0000143 for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner6357a3f2002-02-05 06:52:25 +0000144 BBLiveVar::RemoveFromBB(*I);
145 M = 0;
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000146
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000147 // Then delete all objects of type ValueSet created in calcLiveVarSetsForBB
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000148 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000149 // to return ValueSet's before/after a machine instruction quickly). It
150 // is sufficient to free up all ValueSet using only one cache since
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000151 // both caches refer to the same sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000152 //
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000153 for (std::map<const MachineInstr*, const ValueSet*>::iterator
Chris Lattnerab584112002-02-05 00:34:50 +0000154 MI = MInst2LVSetBI.begin(),
Chris Lattnera51c7a82002-02-04 23:31:16 +0000155 ME = MInst2LVSetBI.end(); MI != ME; ++MI)
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000156 delete MI->second; // delete all ValueSets in MInst2LVSetBI
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000157
158 MInst2LVSetBI.clear();
159 MInst2LVSetAI.clear();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000160}
161
162
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000163
164
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000165//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000166// Following functions will give the LiveVar info for any machine instr in
Chris Lattnerf57b8452002-04-27 06:56:12 +0000167// a function. It should be called after a call to analyze().
Chris Lattnera51c7a82002-02-04 23:31:16 +0000168//
169// Thsese functions calucluates live var info for all the machine instrs in a
170// BB when LVInfo for one inst is requested. Hence, this function is useful
171// when live var info is required for many (or all) instructions in a basic
Chris Lattnerf57b8452002-04-27 06:56:12 +0000172// block. Also, the arguments to this function does not require specific
Chris Lattnera51c7a82002-02-04 23:31:16 +0000173// iterators.
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000174//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000175
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000176//-----------------------------------------------------------------------------
177// Gives live variable information before a machine instruction
178//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000179
Chris Lattner748697d2002-02-05 04:20:12 +0000180const ValueSet &
Chris Lattner483e14e2002-04-27 07:27:19 +0000181FunctionLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
182 const BasicBlock *BB) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000183 if (const ValueSet *LVSet = MInst2LVSetBI[MInst]) {
Chris Lattner748697d2002-02-05 04:20:12 +0000184 return *LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000185 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000186 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattner748697d2002-02-05 04:20:12 +0000187 return *MInst2LVSetBI[MInst];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000188 }
189}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000190
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000191
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000192//-----------------------------------------------------------------------------
193// Gives live variable information after a machine instruction
194//-----------------------------------------------------------------------------
Chris Lattner748697d2002-02-05 04:20:12 +0000195const ValueSet &
Chris Lattner483e14e2002-04-27 07:27:19 +0000196FunctionLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
197 const BasicBlock *BB) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000198
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000199 if (const ValueSet *LVSet = MInst2LVSetAI[MI]) {
Chris Lattner748697d2002-02-05 04:20:12 +0000200 return *LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000201 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000202 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattner748697d2002-02-05 04:20:12 +0000203 return *MInst2LVSetAI[MI];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000204 }
205}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000206
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000207// This function applies a machine instr to a live var set (accepts OutSet) and
208// makes necessary changes to it (produces InSet). Note that two for loops are
209// used to first kill all defs and then to add all uses. This is because there
210// can be instructions like Val = Val + 1 since we allow multipe defs to a
211// machine instruction operand.
212//
213static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000214 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
215 OpE = MInst->end(); OpI != OpE; ++OpI) {
Vikram S. Advea22eace2003-05-27 00:06:48 +0000216 if (OpI.isDefOnly() || OpI.isDefAndUse()) // kill if this operand is a def
217 LVS.erase(*OpI); // this definition kills any uses
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000218 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000219
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000220 // do for implicit operands as well
221 for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
Vikram S. Advea22eace2003-05-27 00:06:48 +0000222 if (MInst->getImplicitOp(i).opIsDefOnly() ||
223 MInst->getImplicitOp(i).opIsDefAndUse())
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000224 LVS.erase(MInst->getImplicitRef(i));
225 }
226
Chris Lattner2f898d22002-02-05 06:02:59 +0000227 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
228 OpE = MInst->end(); OpI != OpE; ++OpI) {
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000229 if (!isa<BasicBlock>(*OpI)) // don't process labels
Vikram S. Adve9afa88c2002-07-08 22:56:34 +0000230 // add only if this operand is a use
Vikram S. Advea22eace2003-05-27 00:06:48 +0000231 if (!OpI.isDefOnly() || OpI.isDefAndUse() )
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000232 LVS.insert(*OpI); // An operand is a use - so add to use set
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000233 }
234
235 // do for implicit operands as well
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000236 for (unsigned i = 0, e = MInst->getNumImplicitRefs(); i != e; ++i)
Vikram S. Advea22eace2003-05-27 00:06:48 +0000237 if (MInst->getImplicitOp(i).opIsUse() ||
238 MInst->getImplicitOp(i).opIsDefAndUse())
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000239 LVS.insert(MInst->getImplicitRef(i));
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000240}
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000241
242//-----------------------------------------------------------------------------
243// This method calculates the live variable information for all the
244// instructions in a basic block and enter the newly constructed live
Chris Lattnera51c7a82002-02-04 23:31:16 +0000245// variable sets into a the caches (MInst2LVSetAI, MInst2LVSetBI)
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000246//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000247
Chris Lattner483e14e2002-04-27 07:27:19 +0000248void FunctionLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
Chris Lattner601fc7c2002-10-28 18:01:21 +0000249 BBLiveVar *BBLV = BBLiveVar::GetFromBB(*BB);
250 assert(BBLV && "BBLiveVar annotation doesn't exist?");
251 const MachineBasicBlock &MIVec = BBLV->getMachineBasicBlock();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000252
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000253 if (DEBUG_LV >= LV_DEBUG_Instr)
254 std::cerr << "\n======For BB " << BB->getName()
255 << ": Live var sets for instructions======\n";
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000256
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000257 ValueSet CurSet;
Chris Lattner748697d2002-02-05 04:20:12 +0000258 const ValueSet *SetAI = &getOutSetOfBB(BB); // init SetAI with OutSet
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000259 set_union(CurSet, *SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000260
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000261 // iterate over all the machine instructions in BB
Chris Lattner55291ea2002-10-28 01:41:47 +0000262 for (MachineBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
Chris Lattnera51c7a82002-02-04 23:31:16 +0000263 MIE = MIVec.rend(); MII != MIE; ++MII) {
264 // MI is cur machine inst
265 const MachineInstr *MI = *MII;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000266
Chris Lattnera51c7a82002-02-04 23:31:16 +0000267 MInst2LVSetAI[MI] = SetAI; // record in After Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000268
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000269 applyTranferFuncForMInst(CurSet, MI); // apply the transfer Func
270 ValueSet *NewSet = new ValueSet(); // create a new set and
271 set_union(*NewSet, CurSet); // copy the set after T/F to it
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000272
Chris Lattnera51c7a82002-02-04 23:31:16 +0000273 MInst2LVSetBI[MI] = NewSet; // record in Before Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000274
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000275 if (DEBUG_LV >= LV_DEBUG_Instr) {
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000276 std::cerr << "\nLive var sets before/after instruction " << *MI;
Anand Shuklaa9284032002-06-25 20:35:19 +0000277 std::cerr << " Before: "; printSet(*NewSet); std::cerr << "\n";
278 std::cerr << " After : "; printSet(*SetAI); std::cerr << "\n";
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000279 }
280
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000281 // SetAI will be used in the next iteration
282 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000283 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000284}