blob: 297b64a93c7e29660fbea5c9b2fce337b84e78a9 [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 Lattner483e14e2002-04-27 07:27:19 +00008#include "llvm/Analysis/LiveVar/FunctionLiveVarInfo.h"
Chris Lattnerf39f3792002-02-05 00:43:37 +00009#include "BBLiveVar.h"
Ruchira Sasankae27c3442001-08-20 21:12:49 +000010#include "llvm/CodeGen/MachineInstr.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 Lattner697954c2002-01-20 22:54:45 +000015#include <iostream>
Ruchira Sasanka683847f2001-07-24 17:14:13 +000016
Chris Lattner483e14e2002-04-27 07:27:19 +000017AnalysisID FunctionLiveVarInfo::ID(AnalysisID::create<FunctionLiveVarInfo>());
Ruchira Sasanka683847f2001-07-24 17:14:13 +000018
Chris Lattner70e60cb2002-05-22 17:08:27 +000019LiveVarDebugLevel_t DEBUG_LV;
20
21static cl::Enum<LiveVarDebugLevel_t> DEBUG_LV_opt(DEBUG_LV, "dlivevar", cl::Hidden,
Vikram S. Adve9cf85a72002-03-18 03:45:41 +000022 "enable live-variable debugging information",
23 clEnumValN(LV_DEBUG_None , "n", "disable debug output"),
24 clEnumValN(LV_DEBUG_Normal , "y", "enable debug output"),
25 clEnumValN(LV_DEBUG_Instr, "i", "print live-var sets before/after every machine instrn"),
26 clEnumValN(LV_DEBUG_Verbose, "v", "print def, use sets for every instrn also"), 0);
27
Chris Lattner70e60cb2002-05-22 17:08:27 +000028
29
Chris Lattnerab584112002-02-05 00:34:50 +000030//-----------------------------------------------------------------------------
31// Accessor Functions
32//-----------------------------------------------------------------------------
33
34// gets OutSet of a BB
Chris Lattner483e14e2002-04-27 07:27:19 +000035const ValueSet &FunctionLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
Chris Lattner18961502002-06-25 16:12:52 +000036 return BBLiveVar::GetFromBB(*BB)->getOutSet();
Chris Lattnerab584112002-02-05 00:34:50 +000037}
38
39// gets InSet of a BB
Chris Lattner483e14e2002-04-27 07:27:19 +000040const ValueSet &FunctionLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
Chris Lattner18961502002-06-25 16:12:52 +000041 return BBLiveVar::GetFromBB(*BB)->getInSet();
Chris Lattnerab584112002-02-05 00:34:50 +000042}
43
Chris Lattnerbdfd3282002-02-04 20:49:04 +000044
45//-----------------------------------------------------------------------------
Chris Lattnerf57b8452002-04-27 06:56:12 +000046// Performs live var analysis for a function
Chris Lattnerbdfd3282002-02-04 20:49:04 +000047//-----------------------------------------------------------------------------
48
Chris Lattner18961502002-06-25 16:12:52 +000049bool FunctionLiveVarInfo::runOnFunction(Function &F) {
50 M = &F;
Chris Lattnera51c7a82002-02-04 23:31:16 +000051 if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000052
53 // create and initialize all the BBLiveVars of the CFG
Chris Lattner18961502002-06-25 16:12:52 +000054 constructBBs(M);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000055
Vikram S. Adve9cf85a72002-03-18 03:45:41 +000056 unsigned int iter=0;
Chris Lattner18961502002-06-25 16:12:52 +000057 while (doSingleBackwardPass(M, iter++))
Chris Lattnerbdfd3282002-02-04 20:49:04 +000058 ; // Iterate until we are done.
59
Chris Lattnera51c7a82002-02-04 23:31:16 +000060 if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000061 return false;
62}
63
64
65//-----------------------------------------------------------------------------
66// constructs BBLiveVars and init Def and In sets
67//-----------------------------------------------------------------------------
68
Chris Lattner483e14e2002-04-27 07:27:19 +000069void FunctionLiveVarInfo::constructBBs(const Function *M) {
Chris Lattnerbdfd3282002-02-04 20:49:04 +000070 unsigned int POId = 0; // Reverse Depth-first Order ID
71
Chris Lattnerb7653df2002-04-08 22:03:57 +000072 for(po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000073 BBI != BBE; ++BBI, ++POId) {
Chris Lattner18961502002-06-25 16:12:52 +000074 const BasicBlock &BB = **BBI; // get the current BB
Chris Lattnerbdfd3282002-02-04 20:49:04 +000075
Chris Lattner0665a5f2002-02-05 01:43:49 +000076 if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000077
78 // create a new BBLiveVar
Chris Lattner6357a3f2002-02-05 06:52:25 +000079 BBLiveVar *LVBB = BBLiveVar::CreateOnBB(BB, POId);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000080
Chris Lattnera51c7a82002-02-04 23:31:16 +000081 if (DEBUG_LV)
Chris Lattnerbdfd3282002-02-04 20:49:04 +000082 LVBB->printAllSets();
83 }
84
85 // Since the PO iterator does not discover unreachable blocks,
86 // go over the random iterator and init those blocks as well.
87 // However, LV info is not correct for those blocks (they are not
88 // analyzed)
89 //
Chris Lattnerb7653df2002-04-08 22:03:57 +000090 for (Function::const_iterator BBRI = M->begin(), BBRE = M->end();
Chris Lattnerbdfd3282002-02-04 20:49:04 +000091 BBRI != BBRE; ++BBRI, ++POId)
Chris Lattner6357a3f2002-02-05 06:52:25 +000092 if (!BBLiveVar::GetFromBB(*BBRI)) // Not yet processed?
93 BBLiveVar::CreateOnBB(*BBRI, POId);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000094}
95
96
97//-----------------------------------------------------------------------------
98// do one backward pass over the CFG (for iterative analysis)
99//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000100
Chris Lattner483e14e2002-04-27 07:27:19 +0000101bool FunctionLiveVarInfo::doSingleBackwardPass(const Function *M,
102 unsigned iter) {
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000103 if (DEBUG_LV) std::cerr << "\n After Backward Pass " << iter << "...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000104
Chris Lattnera51c7a82002-02-04 23:31:16 +0000105 bool NeedAnotherIteration = false;
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000106 for (po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
107 BBI != BBE; ++BBI) {
Chris Lattner18961502002-06-25 16:12:52 +0000108 BBLiveVar *LVBB = BBLiveVar::GetFromBB(**BBI);
Chris Lattnera51c7a82002-02-04 23:31:16 +0000109 assert(LVBB && "BasicBlock information not set for block!");
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000110
Chris Lattnera51c7a82002-02-04 23:31:16 +0000111 if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000112
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000113 // InSets are initialized to "GenSet". Recompute only if OutSet changed.
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000114 if(LVBB->isOutSetChanged())
115 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000116
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000117 // OutSets are initialized to EMPTY. Recompute on first iter or if InSet
118 // changed.
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000119 if (iter == 0 || LVBB->isInSetChanged()) // to calc Outsets of preds
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000120 NeedAnotherIteration |= LVBB->applyFlowFunc();
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000121
Chris Lattnera51c7a82002-02-04 23:31:16 +0000122 if (DEBUG_LV) LVBB->printInOutSets();
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000123 }
124
125 // true if we need to reiterate over the CFG
126 return NeedAnotherIteration;
127}
128
129
Chris Lattner483e14e2002-04-27 07:27:19 +0000130void FunctionLiveVarInfo::releaseMemory() {
Chris Lattner6357a3f2002-02-05 06:52:25 +0000131 // First remove all BBLiveVar annotations created in constructBBs().
132 if (M)
Chris Lattnerb7653df2002-04-08 22:03:57 +0000133 for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner6357a3f2002-02-05 06:52:25 +0000134 BBLiveVar::RemoveFromBB(*I);
135 M = 0;
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000136
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000137 // Then delete all objects of type ValueSet created in calcLiveVarSetsForBB
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000138 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000139 // to return ValueSet's before/after a machine instruction quickly). It
140 // is sufficient to free up all ValueSet using only one cache since
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000141 // both caches refer to the same sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000142 //
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000143 for (std::map<const MachineInstr*, const ValueSet*>::iterator
Chris Lattnerab584112002-02-05 00:34:50 +0000144 MI = MInst2LVSetBI.begin(),
Chris Lattnera51c7a82002-02-04 23:31:16 +0000145 ME = MInst2LVSetBI.end(); MI != ME; ++MI)
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000146 delete MI->second; // delete all ValueSets in MInst2LVSetBI
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000147
148 MInst2LVSetBI.clear();
149 MInst2LVSetAI.clear();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000150}
151
152
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000153
154
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000155//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000156// Following functions will give the LiveVar info for any machine instr in
Chris Lattnerf57b8452002-04-27 06:56:12 +0000157// a function. It should be called after a call to analyze().
Chris Lattnera51c7a82002-02-04 23:31:16 +0000158//
159// Thsese functions calucluates live var info for all the machine instrs in a
160// BB when LVInfo for one inst is requested. Hence, this function is useful
161// when live var info is required for many (or all) instructions in a basic
Chris Lattnerf57b8452002-04-27 06:56:12 +0000162// block. Also, the arguments to this function does not require specific
Chris Lattnera51c7a82002-02-04 23:31:16 +0000163// iterators.
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000164//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000165
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000166//-----------------------------------------------------------------------------
167// Gives live variable information before a machine instruction
168//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000169
Chris Lattner748697d2002-02-05 04:20:12 +0000170const ValueSet &
Chris Lattner483e14e2002-04-27 07:27:19 +0000171FunctionLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
172 const BasicBlock *BB) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000173 if (const ValueSet *LVSet = MInst2LVSetBI[MInst]) {
Chris Lattner748697d2002-02-05 04:20:12 +0000174 return *LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000175 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000176 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattner748697d2002-02-05 04:20:12 +0000177 return *MInst2LVSetBI[MInst];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000178 }
179}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000180
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000181
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000182//-----------------------------------------------------------------------------
183// Gives live variable information after a machine instruction
184//-----------------------------------------------------------------------------
Chris Lattner748697d2002-02-05 04:20:12 +0000185const ValueSet &
Chris Lattner483e14e2002-04-27 07:27:19 +0000186FunctionLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
187 const BasicBlock *BB) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000188
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000189 if (const ValueSet *LVSet = MInst2LVSetAI[MI]) {
Chris Lattner748697d2002-02-05 04:20:12 +0000190 return *LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000191 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000192 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattner748697d2002-02-05 04:20:12 +0000193 return *MInst2LVSetAI[MI];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000194 }
195}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000196
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000197// This function applies a machine instr to a live var set (accepts OutSet) and
198// makes necessary changes to it (produces InSet). Note that two for loops are
199// used to first kill all defs and then to add all uses. This is because there
200// can be instructions like Val = Val + 1 since we allow multipe defs to a
201// machine instruction operand.
202//
203static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000204 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
205 OpE = MInst->end(); OpI != OpE; ++OpI) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000206 if (OpI.isDef()) // kill only if this operand is a def
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000207 LVS.erase(*OpI); // this definition kills any uses
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000208 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000209
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000210 // do for implicit operands as well
211 for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
212 if (MInst->implicitRefIsDefined(i))
213 LVS.erase(MInst->getImplicitRef(i));
214 }
215
Chris Lattner2f898d22002-02-05 06:02:59 +0000216 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
217 OpE = MInst->end(); OpI != OpE; ++OpI) {
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000218 if (!isa<BasicBlock>(*OpI)) // don't process labels
219 if (!OpI.isDef()) // add only if this operand is a use
220 LVS.insert(*OpI); // An operand is a use - so add to use set
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000221 }
222
223 // do for implicit operands as well
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000224 for (unsigned i = 0, e = MInst->getNumImplicitRefs(); i != e; ++i)
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000225 if (!MInst->implicitRefIsDefined(i))
226 LVS.insert(MInst->getImplicitRef(i));
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000227}
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000228
229//-----------------------------------------------------------------------------
230// This method calculates the live variable information for all the
231// instructions in a basic block and enter the newly constructed live
Chris Lattnera51c7a82002-02-04 23:31:16 +0000232// variable sets into a the caches (MInst2LVSetAI, MInst2LVSetBI)
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000233//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000234
Chris Lattner483e14e2002-04-27 07:27:19 +0000235void FunctionLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000236 const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000237
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000238 if (DEBUG_LV >= LV_DEBUG_Instr)
239 std::cerr << "\n======For BB " << BB->getName()
240 << ": Live var sets for instructions======\n";
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000241
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000242 ValueSet CurSet;
Chris Lattner748697d2002-02-05 04:20:12 +0000243 const ValueSet *SetAI = &getOutSetOfBB(BB); // init SetAI with OutSet
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000244 set_union(CurSet, *SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000245
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000246 // iterate over all the machine instructions in BB
Chris Lattnera51c7a82002-02-04 23:31:16 +0000247 for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
248 MIE = MIVec.rend(); MII != MIE; ++MII) {
249 // MI is cur machine inst
250 const MachineInstr *MI = *MII;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000251
Chris Lattnera51c7a82002-02-04 23:31:16 +0000252 MInst2LVSetAI[MI] = SetAI; // record in After Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000253
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000254 applyTranferFuncForMInst(CurSet, MI); // apply the transfer Func
255 ValueSet *NewSet = new ValueSet(); // create a new set and
256 set_union(*NewSet, CurSet); // copy the set after T/F to it
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000257
Chris Lattnera51c7a82002-02-04 23:31:16 +0000258 MInst2LVSetBI[MI] = NewSet; // record in Before Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000259
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000260 if (DEBUG_LV >= LV_DEBUG_Instr) {
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000261 std::cerr << "\nLive var sets before/after instruction " << *MI;
Anand Shuklaa9284032002-06-25 20:35:19 +0000262 std::cerr << " Before: "; printSet(*NewSet); std::cerr << "\n";
263 std::cerr << " After : "; printSet(*SetAI); std::cerr << "\n";
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000264 }
265
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000266 // SetAI will be used in the next iteration
267 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000268 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000269}