blob: 774bdc7cd7bcfb93124f08bfd74d33d06447a758 [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 Lattner483e14e2002-04-27 07:27:19 +000018AnalysisID FunctionLiveVarInfo::ID(AnalysisID::create<FunctionLiveVarInfo>());
Ruchira Sasanka683847f2001-07-24 17:14:13 +000019
Chris Lattner70e60cb2002-05-22 17:08:27 +000020LiveVarDebugLevel_t DEBUG_LV;
21
22static cl::Enum<LiveVarDebugLevel_t> DEBUG_LV_opt(DEBUG_LV, "dlivevar", cl::Hidden,
Vikram S. Adve9cf85a72002-03-18 03:45:41 +000023 "enable live-variable debugging information",
24 clEnumValN(LV_DEBUG_None , "n", "disable debug output"),
25 clEnumValN(LV_DEBUG_Normal , "y", "enable debug output"),
26 clEnumValN(LV_DEBUG_Instr, "i", "print live-var sets before/after every machine instrn"),
27 clEnumValN(LV_DEBUG_Verbose, "v", "print def, use sets for every instrn also"), 0);
28
Chris Lattner70e60cb2002-05-22 17:08:27 +000029
30
Chris Lattnerab584112002-02-05 00:34:50 +000031//-----------------------------------------------------------------------------
32// Accessor Functions
33//-----------------------------------------------------------------------------
34
35// gets OutSet of a BB
Chris Lattner483e14e2002-04-27 07:27:19 +000036const ValueSet &FunctionLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
Chris Lattner18961502002-06-25 16:12:52 +000037 return BBLiveVar::GetFromBB(*BB)->getOutSet();
Chris Lattnerab584112002-02-05 00:34:50 +000038}
39
40// gets InSet of a BB
Chris Lattner483e14e2002-04-27 07:27:19 +000041const ValueSet &FunctionLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
Chris Lattner18961502002-06-25 16:12:52 +000042 return BBLiveVar::GetFromBB(*BB)->getInSet();
Chris Lattnerab584112002-02-05 00:34:50 +000043}
44
Chris Lattnerbdfd3282002-02-04 20:49:04 +000045
46//-----------------------------------------------------------------------------
Chris Lattnerf57b8452002-04-27 06:56:12 +000047// Performs live var analysis for a function
Chris Lattnerbdfd3282002-02-04 20:49:04 +000048//-----------------------------------------------------------------------------
49
Chris Lattner18961502002-06-25 16:12:52 +000050bool FunctionLiveVarInfo::runOnFunction(Function &F) {
51 M = &F;
Chris Lattnera51c7a82002-02-04 23:31:16 +000052 if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000053
54 // create and initialize all the BBLiveVars of the CFG
Chris Lattner18961502002-06-25 16:12:52 +000055 constructBBs(M);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000056
Vikram S. Adve9cf85a72002-03-18 03:45:41 +000057 unsigned int iter=0;
Chris Lattner18961502002-06-25 16:12:52 +000058 while (doSingleBackwardPass(M, iter++))
Chris Lattnerbdfd3282002-02-04 20:49:04 +000059 ; // Iterate until we are done.
60
Chris Lattnera51c7a82002-02-04 23:31:16 +000061 if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000062 return false;
63}
64
65
66//-----------------------------------------------------------------------------
67// constructs BBLiveVars and init Def and In sets
68//-----------------------------------------------------------------------------
69
Chris Lattner483e14e2002-04-27 07:27:19 +000070void FunctionLiveVarInfo::constructBBs(const Function *M) {
Chris Lattnerbdfd3282002-02-04 20:49:04 +000071 unsigned int POId = 0; // Reverse Depth-first Order ID
72
Chris Lattnerb7653df2002-04-08 22:03:57 +000073 for(po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000074 BBI != BBE; ++BBI, ++POId) {
Chris Lattner18961502002-06-25 16:12:52 +000075 const BasicBlock &BB = **BBI; // get the current BB
Chris Lattnerbdfd3282002-02-04 20:49:04 +000076
Chris Lattner0665a5f2002-02-05 01:43:49 +000077 if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000078
79 // create a new BBLiveVar
Chris Lattner6357a3f2002-02-05 06:52:25 +000080 BBLiveVar *LVBB = BBLiveVar::CreateOnBB(BB, POId);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000081
Chris Lattnera51c7a82002-02-04 23:31:16 +000082 if (DEBUG_LV)
Chris Lattnerbdfd3282002-02-04 20:49:04 +000083 LVBB->printAllSets();
84 }
85
86 // Since the PO iterator does not discover unreachable blocks,
87 // go over the random iterator and init those blocks as well.
88 // However, LV info is not correct for those blocks (they are not
89 // analyzed)
90 //
Chris Lattnerb7653df2002-04-08 22:03:57 +000091 for (Function::const_iterator BBRI = M->begin(), BBRE = M->end();
Chris Lattnerbdfd3282002-02-04 20:49:04 +000092 BBRI != BBRE; ++BBRI, ++POId)
Chris Lattner6357a3f2002-02-05 06:52:25 +000093 if (!BBLiveVar::GetFromBB(*BBRI)) // Not yet processed?
94 BBLiveVar::CreateOnBB(*BBRI, POId);
Chris Lattnerbdfd3282002-02-04 20:49:04 +000095}
96
97
98//-----------------------------------------------------------------------------
99// do one backward pass over the CFG (for iterative analysis)
100//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000101
Chris Lattner483e14e2002-04-27 07:27:19 +0000102bool FunctionLiveVarInfo::doSingleBackwardPass(const Function *M,
103 unsigned iter) {
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000104 if (DEBUG_LV) std::cerr << "\n After Backward Pass " << iter << "...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000105
Chris Lattnera51c7a82002-02-04 23:31:16 +0000106 bool NeedAnotherIteration = false;
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000107 for (po_iterator<const Function*> BBI = po_begin(M), BBE = po_end(M);
108 BBI != BBE; ++BBI) {
Chris Lattner18961502002-06-25 16:12:52 +0000109 BBLiveVar *LVBB = BBLiveVar::GetFromBB(**BBI);
Chris Lattnera51c7a82002-02-04 23:31:16 +0000110 assert(LVBB && "BasicBlock information not set for block!");
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000111
Chris Lattnera51c7a82002-02-04 23:31:16 +0000112 if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000113
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000114 // InSets are initialized to "GenSet". Recompute only if OutSet changed.
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000115 if(LVBB->isOutSetChanged())
116 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000117
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000118 // OutSets are initialized to EMPTY. Recompute on first iter or if InSet
119 // changed.
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000120 if (iter == 0 || LVBB->isInSetChanged()) // to calc Outsets of preds
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000121 NeedAnotherIteration |= LVBB->applyFlowFunc();
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000122
Chris Lattnera51c7a82002-02-04 23:31:16 +0000123 if (DEBUG_LV) LVBB->printInOutSets();
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000124 }
125
126 // true if we need to reiterate over the CFG
127 return NeedAnotherIteration;
128}
129
130
Chris Lattner483e14e2002-04-27 07:27:19 +0000131void FunctionLiveVarInfo::releaseMemory() {
Chris Lattner6357a3f2002-02-05 06:52:25 +0000132 // First remove all BBLiveVar annotations created in constructBBs().
133 if (M)
Chris Lattnerb7653df2002-04-08 22:03:57 +0000134 for (Function::const_iterator I = M->begin(), E = M->end(); I != E; ++I)
Chris Lattner6357a3f2002-02-05 06:52:25 +0000135 BBLiveVar::RemoveFromBB(*I);
136 M = 0;
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000137
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000138 // Then delete all objects of type ValueSet created in calcLiveVarSetsForBB
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000139 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000140 // to return ValueSet's before/after a machine instruction quickly). It
141 // is sufficient to free up all ValueSet using only one cache since
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000142 // both caches refer to the same sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000143 //
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000144 for (std::map<const MachineInstr*, const ValueSet*>::iterator
Chris Lattnerab584112002-02-05 00:34:50 +0000145 MI = MInst2LVSetBI.begin(),
Chris Lattnera51c7a82002-02-04 23:31:16 +0000146 ME = MInst2LVSetBI.end(); MI != ME; ++MI)
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000147 delete MI->second; // delete all ValueSets in MInst2LVSetBI
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000148
149 MInst2LVSetBI.clear();
150 MInst2LVSetAI.clear();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000151}
152
153
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000154
155
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000156//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000157// Following functions will give the LiveVar info for any machine instr in
Chris Lattnerf57b8452002-04-27 06:56:12 +0000158// a function. It should be called after a call to analyze().
Chris Lattnera51c7a82002-02-04 23:31:16 +0000159//
160// Thsese functions calucluates live var info for all the machine instrs in a
161// BB when LVInfo for one inst is requested. Hence, this function is useful
162// when live var info is required for many (or all) instructions in a basic
Chris Lattnerf57b8452002-04-27 06:56:12 +0000163// block. Also, the arguments to this function does not require specific
Chris Lattnera51c7a82002-02-04 23:31:16 +0000164// iterators.
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000165//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000166
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000167//-----------------------------------------------------------------------------
168// Gives live variable information before a machine instruction
169//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000170
Chris Lattner748697d2002-02-05 04:20:12 +0000171const ValueSet &
Chris Lattner483e14e2002-04-27 07:27:19 +0000172FunctionLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
173 const BasicBlock *BB) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000174 if (const ValueSet *LVSet = MInst2LVSetBI[MInst]) {
Chris Lattner748697d2002-02-05 04:20:12 +0000175 return *LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000176 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000177 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattner748697d2002-02-05 04:20:12 +0000178 return *MInst2LVSetBI[MInst];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000179 }
180}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000181
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000182
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000183//-----------------------------------------------------------------------------
184// Gives live variable information after a machine instruction
185//-----------------------------------------------------------------------------
Chris Lattner748697d2002-02-05 04:20:12 +0000186const ValueSet &
Chris Lattner483e14e2002-04-27 07:27:19 +0000187FunctionLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
188 const BasicBlock *BB) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000189
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000190 if (const ValueSet *LVSet = MInst2LVSetAI[MI]) {
Chris Lattner748697d2002-02-05 04:20:12 +0000191 return *LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000192 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000193 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattner748697d2002-02-05 04:20:12 +0000194 return *MInst2LVSetAI[MI];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000195 }
196}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000197
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000198// This function applies a machine instr to a live var set (accepts OutSet) and
199// makes necessary changes to it (produces InSet). Note that two for loops are
200// used to first kill all defs and then to add all uses. This is because there
201// can be instructions like Val = Val + 1 since we allow multipe defs to a
202// machine instruction operand.
203//
204static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000205 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
206 OpE = MInst->end(); OpI != OpE; ++OpI) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000207 if (OpI.isDef()) // kill only if this operand is a def
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000208 LVS.erase(*OpI); // this definition kills any uses
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000209 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000210
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000211 // do for implicit operands as well
212 for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
213 if (MInst->implicitRefIsDefined(i))
214 LVS.erase(MInst->getImplicitRef(i));
215 }
216
Chris Lattner2f898d22002-02-05 06:02:59 +0000217 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
218 OpE = MInst->end(); OpI != OpE; ++OpI) {
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000219 if (!isa<BasicBlock>(*OpI)) // don't process labels
Vikram S. Adve9afa88c2002-07-08 22:56:34 +0000220 // add only if this operand is a use
221 if (!OpI.isDef() || OpI.isDefAndUse() )
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000222 LVS.insert(*OpI); // An operand is a use - so add to use set
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000223 }
224
225 // do for implicit operands as well
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000226 for (unsigned i = 0, e = MInst->getNumImplicitRefs(); i != e; ++i)
Vikram S. Adve9afa88c2002-07-08 22:56:34 +0000227 if (!MInst->implicitRefIsDefined(i) ||
228 MInst->implicitRefIsDefinedAndUsed(i))
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000229 LVS.insert(MInst->getImplicitRef(i));
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000230}
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000231
232//-----------------------------------------------------------------------------
233// This method calculates the live variable information for all the
234// instructions in a basic block and enter the newly constructed live
Chris Lattnera51c7a82002-02-04 23:31:16 +0000235// variable sets into a the caches (MInst2LVSetAI, MInst2LVSetBI)
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000236//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000237
Chris Lattner483e14e2002-04-27 07:27:19 +0000238void FunctionLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
Vikram S. Adve9afa88c2002-07-08 22:56:34 +0000239 const MachineCodeForBasicBlock &MIVec = MachineCodeForBasicBlock::get(BB);
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000240
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000241 if (DEBUG_LV >= LV_DEBUG_Instr)
242 std::cerr << "\n======For BB " << BB->getName()
243 << ": Live var sets for instructions======\n";
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000244
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000245 ValueSet CurSet;
Chris Lattner748697d2002-02-05 04:20:12 +0000246 const ValueSet *SetAI = &getOutSetOfBB(BB); // init SetAI with OutSet
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000247 set_union(CurSet, *SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000248
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000249 // iterate over all the machine instructions in BB
Chris Lattnera51c7a82002-02-04 23:31:16 +0000250 for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
251 MIE = MIVec.rend(); MII != MIE; ++MII) {
252 // MI is cur machine inst
253 const MachineInstr *MI = *MII;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000254
Chris Lattnera51c7a82002-02-04 23:31:16 +0000255 MInst2LVSetAI[MI] = SetAI; // record in After Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000256
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000257 applyTranferFuncForMInst(CurSet, MI); // apply the transfer Func
258 ValueSet *NewSet = new ValueSet(); // create a new set and
259 set_union(*NewSet, CurSet); // copy the set after T/F to it
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000260
Chris Lattnera51c7a82002-02-04 23:31:16 +0000261 MInst2LVSetBI[MI] = NewSet; // record in Before Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000262
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000263 if (DEBUG_LV >= LV_DEBUG_Instr) {
Chris Lattnerfb005fe2002-04-09 05:14:14 +0000264 std::cerr << "\nLive var sets before/after instruction " << *MI;
Anand Shuklaa9284032002-06-25 20:35:19 +0000265 std::cerr << " Before: "; printSet(*NewSet); std::cerr << "\n";
266 std::cerr << " After : "; printSet(*SetAI); std::cerr << "\n";
Vikram S. Adve9cf85a72002-03-18 03:45:41 +0000267 }
268
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000269 // SetAI will be used in the next iteration
270 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000271 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000272}