blob: a4dbef1cf0728d4bf40ab92fd12e1e008dd4d407 [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 Lattner5e5dfa32002-02-05 02:51:01 +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 Lattner5e5dfa32002-02-05 02:51:01 +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
68 LVBB->calcDefUseSets(); // calculates the def and in set
69
Chris Lattnera51c7a82002-02-04 23:31:16 +000070 if (DEBUG_LV)
Chris Lattnerbdfd3282002-02-04 20:49:04 +000071 LVBB->printAllSets();
72 }
73
74 // Since the PO iterator does not discover unreachable blocks,
75 // go over the random iterator and init those blocks as well.
76 // However, LV info is not correct for those blocks (they are not
77 // analyzed)
78 //
79 for (Method::const_iterator BBRI = M->begin(), BBRE = M->end();
80 BBRI != BBRE; ++BBRI, ++POId)
Chris Lattnera51c7a82002-02-04 23:31:16 +000081 if (!BB2BBLVMap[*BBRI]) // Not yet processed?
Chris Lattnerbdfd3282002-02-04 20:49:04 +000082 BB2BBLVMap[*BBRI] = new BBLiveVar(*BBRI, POId);
83}
84
85
86//-----------------------------------------------------------------------------
87// do one backward pass over the CFG (for iterative analysis)
88//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +000089
Chris Lattnerbdfd3282002-02-04 20:49:04 +000090bool MethodLiveVarInfo::doSingleBackwardPass(const Method *M) {
Chris Lattnera51c7a82002-02-04 23:31:16 +000091 if (DEBUG_LV) std::cerr << "\n After Backward Pass ...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000092
Chris Lattnera51c7a82002-02-04 23:31:16 +000093 bool NeedAnotherIteration = false;
94 for (po_iterator<const Method*> BBI = po_begin(M); BBI != po_end(M) ; ++BBI) {
Chris Lattnerbdfd3282002-02-04 20:49:04 +000095 BBLiveVar *LVBB = BB2BBLVMap[*BBI];
Chris Lattnera51c7a82002-02-04 23:31:16 +000096 assert(LVBB && "BasicBlock information not set for block!");
Chris Lattnerbdfd3282002-02-04 20:49:04 +000097
Chris Lattnera51c7a82002-02-04 23:31:16 +000098 if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000099
100 if(LVBB->isOutSetChanged())
101 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
102
103 if (LVBB->isInSetChanged()) // to calc Outsets of preds
104 NeedAnotherIteration |= LVBB->applyFlowFunc(BB2BBLVMap);
105
Chris Lattnera51c7a82002-02-04 23:31:16 +0000106 if (DEBUG_LV) LVBB->printInOutSets();
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000107 }
108
109 // true if we need to reiterate over the CFG
110 return NeedAnotherIteration;
111}
112
113
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000114void MethodLiveVarInfo::releaseMemory() {
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000115 // First delete all BBLiveVar objects created in constructBBs(). A new object
Chris Lattnera51c7a82002-02-04 23:31:16 +0000116 // of type BBLiveVar is created for every BasicBlock in the method
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000117 //
Chris Lattnerab584112002-02-05 00:34:50 +0000118 for (std::map<const BasicBlock *, BBLiveVar *>::iterator
119 HMI = BB2BBLVMap.begin(),
Chris Lattnera51c7a82002-02-04 23:31:16 +0000120 HME = BB2BBLVMap.end(); HMI != HME; ++HMI)
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000121 delete HMI->second; // delete all BBLiveVar in BB2BBLVMap
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000122
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000123 BB2BBLVMap.clear();
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000124
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000125 // Then delete all objects of type ValueSet created in calcLiveVarSetsForBB
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000126 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000127 // to return ValueSet's before/after a machine instruction quickly). It
128 // is sufficient to free up all ValueSet using only one cache since
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000129 // both caches refer to the same sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000130 //
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000131 for (std::map<const MachineInstr*, const ValueSet*>::iterator
Chris Lattnerab584112002-02-05 00:34:50 +0000132 MI = MInst2LVSetBI.begin(),
Chris Lattnera51c7a82002-02-04 23:31:16 +0000133 ME = MInst2LVSetBI.end(); MI != ME; ++MI)
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000134 delete MI->second; // delete all ValueSets in MInst2LVSetBI
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000135
136 MInst2LVSetBI.clear();
137 MInst2LVSetAI.clear();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000138}
139
140
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000141
142
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000143//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000144// Following functions will give the LiveVar info for any machine instr in
145// a method. It should be called after a call to analyze().
146//
147// Thsese functions calucluates live var info for all the machine instrs in a
148// BB when LVInfo for one inst is requested. Hence, this function is useful
149// when live var info is required for many (or all) instructions in a basic
150// block. Also, the arguments to this method does not require specific
151// iterators.
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000152//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000153
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000154//-----------------------------------------------------------------------------
155// Gives live variable information before a machine instruction
156//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000157
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000158const ValueSet *
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000159MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
Chris Lattnera51c7a82002-02-04 23:31:16 +0000160 const BasicBlock *BB) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000161 if (const ValueSet *LVSet = MInst2LVSetBI[MInst]) {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000162 return LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000163 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000164 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000165 return MInst2LVSetBI[MInst];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000166 }
167}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000168
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000169
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000170//-----------------------------------------------------------------------------
171// Gives live variable information after a machine instruction
172//-----------------------------------------------------------------------------
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000173const ValueSet *
Chris Lattnera51c7a82002-02-04 23:31:16 +0000174MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
175 const BasicBlock *BB) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000176
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000177 if (const ValueSet *LVSet = MInst2LVSetAI[MI]) {
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000178 return LVSet; // if found, just return the set
179 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000180 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
181 return MInst2LVSetAI[MI];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000182 }
183}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000184
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000185// This function applies a machine instr to a live var set (accepts OutSet) and
186// makes necessary changes to it (produces InSet). Note that two for loops are
187// used to first kill all defs and then to add all uses. This is because there
188// can be instructions like Val = Val + 1 since we allow multipe defs to a
189// machine instruction operand.
190//
191static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
192 for (MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done(); ++OpI) {
193 if (OpI.isDef()) // kill only if this operand is a def
194 LVS.insert(*OpI); // this definition kills any uses
195 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000196
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000197 // do for implicit operands as well
198 for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
199 if (MInst->implicitRefIsDefined(i))
200 LVS.erase(MInst->getImplicitRef(i));
201 }
202
203 for (MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done(); ++OpI) {
204 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
211 for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
212 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();
227 const ValueSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
228 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}