blob: e730f49e692d3121711f0fe46002eb3108bb5dec [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"
Vikram S. Adve9afa88c2002-07-08 22:56:34 +000011#include "llvm/CodeGen/MachineCodeForBasicBlock.h"
Chris Lattner221d6882002-02-12 21:07:25 +000012#include "llvm/Support/CFG.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000013#include "Support/PostOrderIterator.h"
Chris Lattner7471a7b2002-02-05 03:35:53 +000014#include "Support/SetOperations.h"
Chris Lattner70e60cb2002-05-22 17:08:27 +000015#include "Support/CommandLine.h"
Chris Lattner697954c2002-01-20 22:54:45 +000016#include <iostream>
Ruchira Sasanka683847f2001-07-24 17:14:13 +000017
Chris Lattner1e435162002-07-26 21:12:44 +000018static RegisterAnalysis<FunctionLiveVarInfo>
19X("livevar", "Live Variable Analysis");
Chris Lattner483e14e2002-04-27 07:27:19 +000020AnalysisID FunctionLiveVarInfo::ID(AnalysisID::create<FunctionLiveVarInfo>());
Ruchira Sasanka683847f2001-07-24 17:14:13 +000021
Chris Lattner70e60cb2002-05-22 17:08:27 +000022LiveVarDebugLevel_t DEBUG_LV;
23
Chris Lattner5ff62e92002-07-22 02:10:13 +000024static cl::opt<LiveVarDebugLevel_t, true>
25DEBUG_LV_opt("dlivevar", cl::Hidden, cl::location(DEBUG_LV),
26 cl::desc("enable live-variable debugging information"),
27 cl::values(
28clEnumValN(LV_DEBUG_None , "n", "disable debug output"),
29clEnumValN(LV_DEBUG_Normal , "y", "enable debug output"),
30clEnumValN(LV_DEBUG_Instr, "i", "print live-var sets before/after "
31 "every machine instrn"),
32clEnumValN(LV_DEBUG_Verbose, "v", "print def, use sets for every instrn also"),
33 0));
Vikram S. Adve9cf85a72002-03-18 03:45:41 +000034
Chris Lattner70e60cb2002-05-22 17:08:27 +000035
36
Chris Lattnerab584112002-02-05 00:34:50 +000037//-----------------------------------------------------------------------------
38// Accessor Functions
39//-----------------------------------------------------------------------------
40
41// gets OutSet of a BB
Chris Lattner483e14e2002-04-27 07:27:19 +000042const ValueSet &FunctionLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
Chris Lattner18961502002-06-25 16:12:52 +000043 return BBLiveVar::GetFromBB(*BB)->getOutSet();
Chris Lattnerab584112002-02-05 00:34:50 +000044}
45
46// gets InSet of a BB
Chris Lattner483e14e2002-04-27 07:27:19 +000047const ValueSet &FunctionLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
Chris Lattner18961502002-06-25 16:12:52 +000048 return BBLiveVar::GetFromBB(*BB)->getInSet();
Chris Lattnerab584112002-02-05 00:34:50 +000049}
50
Chris Lattnerbdfd3282002-02-04 20:49:04 +000051
52//-----------------------------------------------------------------------------
Chris Lattnerf57b8452002-04-27 06:56:12 +000053// Performs live var analysis for a function
Chris Lattnerbdfd3282002-02-04 20:49:04 +000054//-----------------------------------------------------------------------------
55
Chris Lattner18961502002-06-25 16:12:52 +000056bool FunctionLiveVarInfo::runOnFunction(Function &F) {
57 M = &F;
Chris Lattnera51c7a82002-02-04 23:31:16 +000058 if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000059
60 // create and initialize all the BBLiveVars of the CFG
Chris Lattner18961502002-06-25 16:12:52 +000061 constructBBs(M);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000062
Vikram S. Adve9cf85a72002-03-18 03:45:41 +000063 unsigned int iter=0;
Chris Lattner18961502002-06-25 16:12:52 +000064 while (doSingleBackwardPass(M, iter++))
Chris Lattnerbdfd3282002-02-04 20:49:04 +000065 ; // Iterate until we are done.
66
Chris Lattnera51c7a82002-02-04 23:31:16 +000067 if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000068 return false;
69}
70
71
72//-----------------------------------------------------------------------------
73// constructs BBLiveVars and init Def and In sets
74//-----------------------------------------------------------------------------
75
Chris Lattner483e14e2002-04-27 07:27:19 +000076void FunctionLiveVarInfo::constructBBs(const Function *M) {
Chris Lattnerbdfd3282002-02-04 20:49:04 +000077 unsigned int POId = 0; // Reverse Depth-first Order ID
78
Chris Lattnerb7653df2002-04-08 22:03:57 +000079 for(po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000080 BBI != BBE; ++BBI, ++POId) {
Chris Lattner18961502002-06-25 16:12:52 +000081 const BasicBlock &BB = **BBI; // get the current BB
Chris Lattnerbdfd3282002-02-04 20:49:04 +000082
Chris Lattner0665a5f2002-02-05 01:43:49 +000083 if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000084
85 // create a new BBLiveVar
Chris Lattner6357a3f2002-02-05 06:52:25 +000086 BBLiveVar *LVBB = BBLiveVar::CreateOnBB(BB, POId);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000087
Chris Lattnera51c7a82002-02-04 23:31:16 +000088 if (DEBUG_LV)
Chris Lattnerbdfd3282002-02-04 20:49:04 +000089 LVBB->printAllSets();
90 }
91
92 // Since the PO iterator does not discover unreachable blocks,
93 // go over the random iterator and init those blocks as well.
94 // However, LV info is not correct for those blocks (they are not
95 // analyzed)
96 //
Chris Lattnerb7653df2002-04-08 22:03:57 +000097 for (Function::const_iterator BBRI = M->begin(), BBRE = M->end();
Chris Lattnerbdfd3282002-02-04 20:49:04 +000098 BBRI != BBRE; ++BBRI, ++POId)
Chris Lattner6357a3f2002-02-05 06:52:25 +000099 if (!BBLiveVar::GetFromBB(*BBRI)) // Not yet processed?
100 BBLiveVar::CreateOnBB(*BBRI, POId);
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000101}
102
103
104//-----------------------------------------------------------------------------
105// do one backward pass over the CFG (for iterative analysis)
106//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000107
Chris Lattner483e14e2002-04-27 07:27:19 +0000108bool FunctionLiveVarInfo::doSingleBackwardPass(const Function *M,
109 unsigned iter) {
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000110 if (DEBUG_LV) std::cerr << "\n After Backward Pass " << iter << "...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000111
Chris Lattnera51c7a82002-02-04 23:31:16 +0000112 bool NeedAnotherIteration = false;
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000113 for (po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
114 BBI != BBE; ++BBI) {
Chris Lattner18961502002-06-25 16:12:52 +0000115 BBLiveVar *LVBB = BBLiveVar::GetFromBB(**BBI);
Chris Lattnera51c7a82002-02-04 23:31:16 +0000116 assert(LVBB && "BasicBlock information not set for block!");
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000117
Chris Lattnera51c7a82002-02-04 23:31:16 +0000118 if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000119
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000120 // InSets are initialized to "GenSet". Recompute only if OutSet changed.
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000121 if(LVBB->isOutSetChanged())
122 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000123
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000124 // OutSets are initialized to EMPTY. Recompute on first iter or if InSet
125 // changed.
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000126 if (iter == 0 || LVBB->isInSetChanged()) // to calc Outsets of preds
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000127 NeedAnotherIteration |= LVBB->applyFlowFunc();
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000128
Chris Lattnera51c7a82002-02-04 23:31:16 +0000129 if (DEBUG_LV) LVBB->printInOutSets();
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000130 }
131
132 // true if we need to reiterate over the CFG
133 return NeedAnotherIteration;
134}
135
136
Chris Lattner483e14e2002-04-27 07:27:19 +0000137void FunctionLiveVarInfo::releaseMemory() {
Chris Lattner6357a3f2002-02-05 06:52:25 +0000138 // First remove all BBLiveVar annotations created in constructBBs().
139 if (M)
Chris Lattnerb7653df2002-04-08 22:03:57 +0000140 for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner6357a3f2002-02-05 06:52:25 +0000141 BBLiveVar::RemoveFromBB(*I);
142 M = 0;
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000143
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000144 // Then delete all objects of type ValueSet created in calcLiveVarSetsForBB
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000145 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000146 // to return ValueSet's before/after a machine instruction quickly). It
147 // is sufficient to free up all ValueSet using only one cache since
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000148 // both caches refer to the same sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000149 //
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000150 for (std::map<const MachineInstr*, const ValueSet*>::iterator
Chris Lattnerab584112002-02-05 00:34:50 +0000151 MI = MInst2LVSetBI.begin(),
Chris Lattnera51c7a82002-02-04 23:31:16 +0000152 ME = MInst2LVSetBI.end(); MI != ME; ++MI)
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000153 delete MI->second; // delete all ValueSets in MInst2LVSetBI
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000154
155 MInst2LVSetBI.clear();
156 MInst2LVSetAI.clear();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000157}
158
159
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000160
161
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000162//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000163// Following functions will give the LiveVar info for any machine instr in
Chris Lattnerf57b8452002-04-27 06:56:12 +0000164// a function. It should be called after a call to analyze().
Chris Lattnera51c7a82002-02-04 23:31:16 +0000165//
166// Thsese functions calucluates live var info for all the machine instrs in a
167// BB when LVInfo for one inst is requested. Hence, this function is useful
168// when live var info is required for many (or all) instructions in a basic
Chris Lattnerf57b8452002-04-27 06:56:12 +0000169// block. Also, the arguments to this function does not require specific
Chris Lattnera51c7a82002-02-04 23:31:16 +0000170// iterators.
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000171//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000172
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000173//-----------------------------------------------------------------------------
174// Gives live variable information before a machine instruction
175//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000176
Chris Lattner748697d2002-02-05 04:20:12 +0000177const ValueSet &
Chris Lattner483e14e2002-04-27 07:27:19 +0000178FunctionLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
179 const BasicBlock *BB) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000180 if (const ValueSet *LVSet = MInst2LVSetBI[MInst]) {
Chris Lattner748697d2002-02-05 04:20:12 +0000181 return *LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000182 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000183 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattner748697d2002-02-05 04:20:12 +0000184 return *MInst2LVSetBI[MInst];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000185 }
186}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000187
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000188
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000189//-----------------------------------------------------------------------------
190// Gives live variable information after a machine instruction
191//-----------------------------------------------------------------------------
Chris Lattner748697d2002-02-05 04:20:12 +0000192const ValueSet &
Chris Lattner483e14e2002-04-27 07:27:19 +0000193FunctionLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
194 const BasicBlock *BB) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000195
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000196 if (const ValueSet *LVSet = MInst2LVSetAI[MI]) {
Chris Lattner748697d2002-02-05 04:20:12 +0000197 return *LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000198 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000199 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattner748697d2002-02-05 04:20:12 +0000200 return *MInst2LVSetAI[MI];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000201 }
202}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000203
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000204// This function applies a machine instr to a live var set (accepts OutSet) and
205// makes necessary changes to it (produces InSet). Note that two for loops are
206// used to first kill all defs and then to add all uses. This is because there
207// can be instructions like Val = Val + 1 since we allow multipe defs to a
208// machine instruction operand.
209//
210static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000211 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
212 OpE = MInst->end(); OpI != OpE; ++OpI) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000213 if (OpI.isDef()) // kill only if this operand is a def
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000214 LVS.erase(*OpI); // this definition kills any uses
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000215 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000216
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000217 // do for implicit operands as well
218 for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
219 if (MInst->implicitRefIsDefined(i))
220 LVS.erase(MInst->getImplicitRef(i));
221 }
222
Chris Lattner2f898d22002-02-05 06:02:59 +0000223 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
224 OpE = MInst->end(); OpI != OpE; ++OpI) {
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000225 if (!isa<BasicBlock>(*OpI)) // don't process labels
Vikram S. Adve9afa88c2002-07-08 22:56:34 +0000226 // add only if this operand is a use
227 if (!OpI.isDef() || OpI.isDefAndUse() )
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000228 LVS.insert(*OpI); // An operand is a use - so add to use set
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000229 }
230
231 // do for implicit operands as well
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000232 for (unsigned i = 0, e = MInst->getNumImplicitRefs(); i != e; ++i)
Vikram S. Adve9afa88c2002-07-08 22:56:34 +0000233 if (!MInst->implicitRefIsDefined(i) ||
234 MInst->implicitRefIsDefinedAndUsed(i))
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000235 LVS.insert(MInst->getImplicitRef(i));
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000236}
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000237
238//-----------------------------------------------------------------------------
239// This method calculates the live variable information for all the
240// instructions in a basic block and enter the newly constructed live
Chris Lattnera51c7a82002-02-04 23:31:16 +0000241// variable sets into a the caches (MInst2LVSetAI, MInst2LVSetBI)
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000242//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000243
Chris Lattner483e14e2002-04-27 07:27:19 +0000244void FunctionLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
Vikram S. Adve9afa88c2002-07-08 22:56:34 +0000245 const MachineCodeForBasicBlock &MIVec = MachineCodeForBasicBlock::get(BB);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000246
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000247 if (DEBUG_LV >= LV_DEBUG_Instr)
248 std::cerr << "\n======For BB " << BB->getName()
249 << ": Live var sets for instructions======\n";
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000250
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000251 ValueSet CurSet;
Chris Lattner748697d2002-02-05 04:20:12 +0000252 const ValueSet *SetAI = &getOutSetOfBB(BB); // init SetAI with OutSet
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000253 set_union(CurSet, *SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000254
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000255 // iterate over all the machine instructions in BB
Chris Lattnera51c7a82002-02-04 23:31:16 +0000256 for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
257 MIE = MIVec.rend(); MII != MIE; ++MII) {
258 // MI is cur machine inst
259 const MachineInstr *MI = *MII;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000260
Chris Lattnera51c7a82002-02-04 23:31:16 +0000261 MInst2LVSetAI[MI] = SetAI; // record in After Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000262
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000263 applyTranferFuncForMInst(CurSet, MI); // apply the transfer Func
264 ValueSet *NewSet = new ValueSet(); // create a new set and
265 set_union(*NewSet, CurSet); // copy the set after T/F to it
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000266
Chris Lattnera51c7a82002-02-04 23:31:16 +0000267 MInst2LVSetBI[MI] = NewSet; // record in Before Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000268
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000269 if (DEBUG_LV >= LV_DEBUG_Instr) {
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000270 std::cerr << "\nLive var sets before/after instruction " << *MI;
Anand Shuklaa9284032002-06-25 20:35:19 +0000271 std::cerr << " Before: "; printSet(*NewSet); std::cerr << "\n";
272 std::cerr << " After : "; printSet(*SetAI); std::cerr << "\n";
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000273 }
274
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000275 // SetAI will be used in the next iteration
276 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000277 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000278}