blob: d06485dc4dfec30eebe16fd004bcd824f1a90a7b [file] [log] [blame]
Chris Lattner5e5dfa32002-02-05 02:51:01 +00001//===-- MethodLiveVarInfo.cpp - Live Variable Analysis for a Method -------===//
2//
3// This is the interface to method level live variable information that is
4// provided by live variable analysis.
5//
6//===----------------------------------------------------------------------===//
Ruchira Sasanka683847f2001-07-24 17:14:13 +00007
8#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.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 Lattner11646322002-02-04 16:35:12 +000011#include "llvm/BasicBlock.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 Lattner697954c2002-01-20 22:54:45 +000014#include <iostream>
Ruchira Sasanka683847f2001-07-24 17:14:13 +000015
Chris Lattner4fd2dbb2002-02-04 20:00:08 +000016AnalysisID MethodLiveVarInfo::ID(AnalysisID::create<MethodLiveVarInfo>());
Ruchira Sasanka683847f2001-07-24 17:14:13 +000017
Chris Lattnerab584112002-02-05 00:34:50 +000018//-----------------------------------------------------------------------------
19// Accessor Functions
20//-----------------------------------------------------------------------------
21
22// gets OutSet of a BB
Chris Lattner748697d2002-02-05 04:20:12 +000023const ValueSet &MethodLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
Chris Lattnerab584112002-02-05 00:34:50 +000024 return BB2BBLVMap.find(BB)->second->getOutSet();
25}
26
27// gets InSet of a BB
Chris Lattner748697d2002-02-05 04:20:12 +000028const ValueSet &MethodLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
Chris Lattnerab584112002-02-05 00:34:50 +000029 return BB2BBLVMap.find(BB)->second->getInSet();
30}
31
Chris Lattnerbdfd3282002-02-04 20:49:04 +000032
33//-----------------------------------------------------------------------------
34// Performs live var analysis for a method
35//-----------------------------------------------------------------------------
36
37bool MethodLiveVarInfo::runOnMethod(Method *M) {
Chris Lattnera51c7a82002-02-04 23:31:16 +000038 if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000039
40 // create and initialize all the BBLiveVars of the CFG
41 constructBBs(M);
42
43 while (doSingleBackwardPass(M))
44 ; // Iterate until we are done.
45
Chris Lattnera51c7a82002-02-04 23:31:16 +000046 if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000047 return false;
48}
49
50
51//-----------------------------------------------------------------------------
52// constructs BBLiveVars and init Def and In sets
53//-----------------------------------------------------------------------------
54
55void MethodLiveVarInfo::constructBBs(const Method *M) {
56 unsigned int POId = 0; // Reverse Depth-first Order ID
57
58 for(po_iterator<const Method*> BBI = po_begin(M), BBE = po_end(M);
59 BBI != BBE; ++BBI, ++POId) {
60 const BasicBlock *BB = *BBI; // get the current BB
61
Chris Lattner0665a5f2002-02-05 01:43:49 +000062 if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000063
64 // create a new BBLiveVar
65 BBLiveVar *LVBB = new BBLiveVar(BB, POId);
66 BB2BBLVMap[BB] = LVBB; // insert the pair to Map
67
Chris Lattnera51c7a82002-02-04 23:31:16 +000068 if (DEBUG_LV)
Chris Lattnerbdfd3282002-02-04 20:49:04 +000069 LVBB->printAllSets();
70 }
71
72 // Since the PO iterator does not discover unreachable blocks,
73 // go over the random iterator and init those blocks as well.
74 // However, LV info is not correct for those blocks (they are not
75 // analyzed)
76 //
77 for (Method::const_iterator BBRI = M->begin(), BBRE = M->end();
78 BBRI != BBRE; ++BBRI, ++POId)
Chris Lattnera51c7a82002-02-04 23:31:16 +000079 if (!BB2BBLVMap[*BBRI]) // Not yet processed?
Chris Lattnerbdfd3282002-02-04 20:49:04 +000080 BB2BBLVMap[*BBRI] = new BBLiveVar(*BBRI, POId);
81}
82
83
84//-----------------------------------------------------------------------------
85// do one backward pass over the CFG (for iterative analysis)
86//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +000087
Chris Lattnerbdfd3282002-02-04 20:49:04 +000088bool MethodLiveVarInfo::doSingleBackwardPass(const Method *M) {
Chris Lattnera51c7a82002-02-04 23:31:16 +000089 if (DEBUG_LV) std::cerr << "\n After Backward Pass ...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000090
Chris Lattnera51c7a82002-02-04 23:31:16 +000091 bool NeedAnotherIteration = false;
92 for (po_iterator<const Method*> BBI = po_begin(M); BBI != po_end(M) ; ++BBI) {
Chris Lattnerbdfd3282002-02-04 20:49:04 +000093 BBLiveVar *LVBB = BB2BBLVMap[*BBI];
Chris Lattnera51c7a82002-02-04 23:31:16 +000094 assert(LVBB && "BasicBlock information not set for block!");
Chris Lattnerbdfd3282002-02-04 20:49:04 +000095
Chris Lattnera51c7a82002-02-04 23:31:16 +000096 if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000097
98 if(LVBB->isOutSetChanged())
99 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
100
101 if (LVBB->isInSetChanged()) // to calc Outsets of preds
102 NeedAnotherIteration |= LVBB->applyFlowFunc(BB2BBLVMap);
103
Chris Lattnera51c7a82002-02-04 23:31:16 +0000104 if (DEBUG_LV) LVBB->printInOutSets();
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000105 }
106
107 // true if we need to reiterate over the CFG
108 return NeedAnotherIteration;
109}
110
111
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000112void MethodLiveVarInfo::releaseMemory() {
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000113 // First delete all BBLiveVar objects created in constructBBs(). A new object
Chris Lattnera51c7a82002-02-04 23:31:16 +0000114 // of type BBLiveVar is created for every BasicBlock in the method
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000115 //
Chris Lattnerab584112002-02-05 00:34:50 +0000116 for (std::map<const BasicBlock *, BBLiveVar *>::iterator
117 HMI = BB2BBLVMap.begin(),
Chris Lattnera51c7a82002-02-04 23:31:16 +0000118 HME = BB2BBLVMap.end(); HMI != HME; ++HMI)
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000119 delete HMI->second; // delete all BBLiveVar in BB2BBLVMap
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000120
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000121 BB2BBLVMap.clear();
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000122
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000123 // Then delete all objects of type ValueSet created in calcLiveVarSetsForBB
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000124 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000125 // to return ValueSet's before/after a machine instruction quickly). It
126 // is sufficient to free up all ValueSet using only one cache since
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000127 // both caches refer to the same sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000128 //
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000129 for (std::map<const MachineInstr*, const ValueSet*>::iterator
Chris Lattnerab584112002-02-05 00:34:50 +0000130 MI = MInst2LVSetBI.begin(),
Chris Lattnera51c7a82002-02-04 23:31:16 +0000131 ME = MInst2LVSetBI.end(); MI != ME; ++MI)
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000132 delete MI->second; // delete all ValueSets in MInst2LVSetBI
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000133
134 MInst2LVSetBI.clear();
135 MInst2LVSetAI.clear();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000136}
137
138
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000139
140
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000141//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000142// Following functions will give the LiveVar info for any machine instr in
143// a method. It should be called after a call to analyze().
144//
145// Thsese functions calucluates live var info for all the machine instrs in a
146// BB when LVInfo for one inst is requested. Hence, this function is useful
147// when live var info is required for many (or all) instructions in a basic
148// block. Also, the arguments to this method does not require specific
149// iterators.
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000150//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000151
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000152//-----------------------------------------------------------------------------
153// Gives live variable information before a machine instruction
154//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000155
Chris Lattner748697d2002-02-05 04:20:12 +0000156const ValueSet &
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000157MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
Chris Lattnera51c7a82002-02-04 23:31:16 +0000158 const BasicBlock *BB) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000159 if (const ValueSet *LVSet = MInst2LVSetBI[MInst]) {
Chris Lattner748697d2002-02-05 04:20:12 +0000160 return *LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000161 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000162 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattner748697d2002-02-05 04:20:12 +0000163 return *MInst2LVSetBI[MInst];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000164 }
165}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000166
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000167
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000168//-----------------------------------------------------------------------------
169// Gives live variable information after a machine instruction
170//-----------------------------------------------------------------------------
Chris Lattner748697d2002-02-05 04:20:12 +0000171const ValueSet &
Chris Lattnera51c7a82002-02-04 23:31:16 +0000172MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
173 const BasicBlock *BB) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000174
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000175 if (const ValueSet *LVSet = MInst2LVSetAI[MI]) {
Chris Lattner748697d2002-02-05 04:20:12 +0000176 return *LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000177 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000178 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattner748697d2002-02-05 04:20:12 +0000179 return *MInst2LVSetAI[MI];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000180 }
181}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000182
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000183// This function applies a machine instr to a live var set (accepts OutSet) and
184// makes necessary changes to it (produces InSet). Note that two for loops are
185// used to first kill all defs and then to add all uses. This is because there
186// can be instructions like Val = Val + 1 since we allow multipe defs to a
187// machine instruction operand.
188//
189static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
Chris Lattner2f898d22002-02-05 06:02:59 +0000190 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
191 OpE = MInst->end(); OpI != OpE; ++OpI) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000192 if (OpI.isDef()) // kill only if this operand is a def
193 LVS.insert(*OpI); // this definition kills any uses
194 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000195
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000196 // do for implicit operands as well
197 for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
198 if (MInst->implicitRefIsDefined(i))
199 LVS.erase(MInst->getImplicitRef(i));
200 }
201
Chris Lattner2f898d22002-02-05 06:02:59 +0000202 for (MachineInstr::const_val_op_iterator OpI = MInst->begin(),
203 OpE = MInst->end(); OpI != OpE; ++OpI) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000204 if (isa<BasicBlock>(*OpI)) continue; // don't process labels
205
206 if (!OpI.isDef()) // add only if this operand is a use
207 LVS.insert(*OpI); // An operand is a use - so add to use set
208 }
209
210 // do for implicit operands as well
Chris Lattner2f898d22002-02-05 06:02:59 +0000211 for (unsigned i = 0; i < MInst->getNumImplicitRefs(); ++i) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000212 if (!MInst->implicitRefIsDefined(i))
213 LVS.insert(MInst->getImplicitRef(i));
214 }
215}
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000216
217//-----------------------------------------------------------------------------
218// This method calculates the live variable information for all the
219// instructions in a basic block and enter the newly constructed live
Chris Lattnera51c7a82002-02-04 23:31:16 +0000220// variable sets into a the caches (MInst2LVSetAI, MInst2LVSetBI)
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000221//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000222
223void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
224 const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000225
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000226 ValueSet *CurSet = new ValueSet();
Chris Lattner748697d2002-02-05 04:20:12 +0000227 const ValueSet *SetAI = &getOutSetOfBB(BB); // init SetAI with OutSet
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000228 set_union(*CurSet, *SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000229
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000230 // iterate over all the machine instructions in BB
Chris Lattnera51c7a82002-02-04 23:31:16 +0000231 for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
232 MIE = MIVec.rend(); MII != MIE; ++MII) {
233 // MI is cur machine inst
234 const MachineInstr *MI = *MII;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000235
Chris Lattnera51c7a82002-02-04 23:31:16 +0000236 MInst2LVSetAI[MI] = SetAI; // record in After Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000237
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000238 applyTranferFuncForMInst(*CurSet, MI); // apply the transfer Func
239 ValueSet *NewSet = new ValueSet(); // create a new set and
240 set_union(*NewSet, *CurSet); // copy the set after T/F to it
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000241
Chris Lattnera51c7a82002-02-04 23:31:16 +0000242 MInst2LVSetBI[MI] = NewSet; // record in Before Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000243
244 // SetAI will be used in the next iteration
245 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000246 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000247}