blob: 98d39ec2a6e28cdd6b1656ad3fc8e5ed5fd723ae [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 Lattner697954c2002-01-20 22:54:45 +000013#include <iostream>
Ruchira Sasanka683847f2001-07-24 17:14:13 +000014
Chris Lattner4fd2dbb2002-02-04 20:00:08 +000015AnalysisID MethodLiveVarInfo::ID(AnalysisID::create<MethodLiveVarInfo>());
Ruchira Sasanka683847f2001-07-24 17:14:13 +000016
Chris Lattnerab584112002-02-05 00:34:50 +000017//-----------------------------------------------------------------------------
18// Accessor Functions
19//-----------------------------------------------------------------------------
20
21// gets OutSet of a BB
Chris Lattner5e5dfa32002-02-05 02:51:01 +000022const ValueSet *MethodLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
Chris Lattnerab584112002-02-05 00:34:50 +000023 return BB2BBLVMap.find(BB)->second->getOutSet();
24}
25
26// gets InSet of a BB
Chris Lattner5e5dfa32002-02-05 02:51:01 +000027const ValueSet *MethodLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
Chris Lattnerab584112002-02-05 00:34:50 +000028 return BB2BBLVMap.find(BB)->second->getInSet();
29}
30
Chris Lattnerbdfd3282002-02-04 20:49:04 +000031
32//-----------------------------------------------------------------------------
33// Performs live var analysis for a method
34//-----------------------------------------------------------------------------
35
36bool MethodLiveVarInfo::runOnMethod(Method *M) {
Chris Lattnera51c7a82002-02-04 23:31:16 +000037 if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000038
39 // create and initialize all the BBLiveVars of the CFG
40 constructBBs(M);
41
42 while (doSingleBackwardPass(M))
43 ; // Iterate until we are done.
44
Chris Lattnera51c7a82002-02-04 23:31:16 +000045 if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000046 return false;
47}
48
49
50//-----------------------------------------------------------------------------
51// constructs BBLiveVars and init Def and In sets
52//-----------------------------------------------------------------------------
53
54void MethodLiveVarInfo::constructBBs(const Method *M) {
55 unsigned int POId = 0; // Reverse Depth-first Order ID
56
57 for(po_iterator<const Method*> BBI = po_begin(M), BBE = po_end(M);
58 BBI != BBE; ++BBI, ++POId) {
59 const BasicBlock *BB = *BBI; // get the current BB
60
Chris Lattner0665a5f2002-02-05 01:43:49 +000061 if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000062
63 // create a new BBLiveVar
64 BBLiveVar *LVBB = new BBLiveVar(BB, POId);
65 BB2BBLVMap[BB] = LVBB; // insert the pair to Map
66
67 LVBB->calcDefUseSets(); // calculates the def and in set
68
Chris Lattnera51c7a82002-02-04 23:31:16 +000069 if (DEBUG_LV)
Chris Lattnerbdfd3282002-02-04 20:49:04 +000070 LVBB->printAllSets();
71 }
72
73 // Since the PO iterator does not discover unreachable blocks,
74 // go over the random iterator and init those blocks as well.
75 // However, LV info is not correct for those blocks (they are not
76 // analyzed)
77 //
78 for (Method::const_iterator BBRI = M->begin(), BBRE = M->end();
79 BBRI != BBRE; ++BBRI, ++POId)
Chris Lattnera51c7a82002-02-04 23:31:16 +000080 if (!BB2BBLVMap[*BBRI]) // Not yet processed?
Chris Lattnerbdfd3282002-02-04 20:49:04 +000081 BB2BBLVMap[*BBRI] = new BBLiveVar(*BBRI, POId);
82}
83
84
85//-----------------------------------------------------------------------------
86// do one backward pass over the CFG (for iterative analysis)
87//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +000088
Chris Lattnerbdfd3282002-02-04 20:49:04 +000089bool MethodLiveVarInfo::doSingleBackwardPass(const Method *M) {
Chris Lattnera51c7a82002-02-04 23:31:16 +000090 if (DEBUG_LV) std::cerr << "\n After Backward Pass ...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000091
Chris Lattnera51c7a82002-02-04 23:31:16 +000092 bool NeedAnotherIteration = false;
93 for (po_iterator<const Method*> BBI = po_begin(M); BBI != po_end(M) ; ++BBI) {
Chris Lattnerbdfd3282002-02-04 20:49:04 +000094 BBLiveVar *LVBB = BB2BBLVMap[*BBI];
Chris Lattnera51c7a82002-02-04 23:31:16 +000095 assert(LVBB && "BasicBlock information not set for block!");
Chris Lattnerbdfd3282002-02-04 20:49:04 +000096
Chris Lattnera51c7a82002-02-04 23:31:16 +000097 if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000098
99 if(LVBB->isOutSetChanged())
100 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
101
102 if (LVBB->isInSetChanged()) // to calc Outsets of preds
103 NeedAnotherIteration |= LVBB->applyFlowFunc(BB2BBLVMap);
104
Chris Lattnera51c7a82002-02-04 23:31:16 +0000105 if (DEBUG_LV) LVBB->printInOutSets();
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000106 }
107
108 // true if we need to reiterate over the CFG
109 return NeedAnotherIteration;
110}
111
112
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000113void MethodLiveVarInfo::releaseMemory() {
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000114 // First delete all BBLiveVar objects created in constructBBs(). A new object
Chris Lattnera51c7a82002-02-04 23:31:16 +0000115 // of type BBLiveVar is created for every BasicBlock in the method
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000116 //
Chris Lattnerab584112002-02-05 00:34:50 +0000117 for (std::map<const BasicBlock *, BBLiveVar *>::iterator
118 HMI = BB2BBLVMap.begin(),
Chris Lattnera51c7a82002-02-04 23:31:16 +0000119 HME = BB2BBLVMap.end(); HMI != HME; ++HMI)
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000120 delete HMI->second; // delete all BBLiveVar in BB2BBLVMap
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000121
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000122 BB2BBLVMap.clear();
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000123
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000124 // Then delete all objects of type ValueSet created in calcLiveVarSetsForBB
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000125 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000126 // to return ValueSet's before/after a machine instruction quickly). It
127 // is sufficient to free up all ValueSet using only one cache since
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000128 // both caches refer to the same sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000129 //
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000130 for (std::map<const MachineInstr*, const ValueSet*>::iterator
Chris Lattnerab584112002-02-05 00:34:50 +0000131 MI = MInst2LVSetBI.begin(),
Chris Lattnera51c7a82002-02-04 23:31:16 +0000132 ME = MInst2LVSetBI.end(); MI != ME; ++MI)
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000133 delete MI->second; // delete all ValueSets in MInst2LVSetBI
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000134
135 MInst2LVSetBI.clear();
136 MInst2LVSetAI.clear();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000137}
138
139
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000140
141
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000142//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000143// Following functions will give the LiveVar info for any machine instr in
144// a method. It should be called after a call to analyze().
145//
146// Thsese functions calucluates live var info for all the machine instrs in a
147// BB when LVInfo for one inst is requested. Hence, this function is useful
148// when live var info is required for many (or all) instructions in a basic
149// block. Also, the arguments to this method does not require specific
150// iterators.
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000151//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000152
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000153//-----------------------------------------------------------------------------
154// Gives live variable information before a machine instruction
155//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000156
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000157const ValueSet *
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000158MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
Chris Lattnera51c7a82002-02-04 23:31:16 +0000159 const BasicBlock *BB) {
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000160 if (const ValueSet *LVSet = MInst2LVSetBI[MInst]) {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000161 return LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000162 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000163 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000164 return MInst2LVSetBI[MInst];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000165 }
166}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000167
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000168
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000169//-----------------------------------------------------------------------------
170// Gives live variable information after a machine instruction
171//-----------------------------------------------------------------------------
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000172const ValueSet *
Chris Lattnera51c7a82002-02-04 23:31:16 +0000173MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
174 const BasicBlock *BB) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000175
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000176 if (const ValueSet *LVSet = MInst2LVSetAI[MI]) {
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000177 return LVSet; // if found, just return the set
178 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000179 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
180 return MInst2LVSetAI[MI];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000181 }
182}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000183
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000184// This function applies a machine instr to a live var set (accepts OutSet) and
185// makes necessary changes to it (produces InSet). Note that two for loops are
186// used to first kill all defs and then to add all uses. This is because there
187// can be instructions like Val = Val + 1 since we allow multipe defs to a
188// machine instruction operand.
189//
190static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
191 for (MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done(); ++OpI) {
192 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
202 for (MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done(); ++OpI) {
203 if (isa<BasicBlock>(*OpI)) continue; // don't process labels
204
205 if (!OpI.isDef()) // add only if this operand is a use
206 LVS.insert(*OpI); // An operand is a use - so add to use set
207 }
208
209 // do for implicit operands as well
210 for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
211 if (!MInst->implicitRefIsDefined(i))
212 LVS.insert(MInst->getImplicitRef(i));
213 }
214}
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000215
216//-----------------------------------------------------------------------------
217// This method calculates the live variable information for all the
218// instructions in a basic block and enter the newly constructed live
Chris Lattnera51c7a82002-02-04 23:31:16 +0000219// variable sets into a the caches (MInst2LVSetAI, MInst2LVSetBI)
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000220//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000221
222void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
223 const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000224
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000225 ValueSet *CurSet = new ValueSet();
226 const ValueSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
227 set_union(*CurSet, *SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000228
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000229 // iterate over all the machine instructions in BB
Chris Lattnera51c7a82002-02-04 23:31:16 +0000230 for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
231 MIE = MIVec.rend(); MII != MIE; ++MII) {
232 // MI is cur machine inst
233 const MachineInstr *MI = *MII;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000234
Chris Lattnera51c7a82002-02-04 23:31:16 +0000235 MInst2LVSetAI[MI] = SetAI; // record in After Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000236
Chris Lattner5e5dfa32002-02-05 02:51:01 +0000237 applyTranferFuncForMInst(*CurSet, MI); // apply the transfer Func
238 ValueSet *NewSet = new ValueSet(); // create a new set and
239 set_union(*NewSet, *CurSet); // copy the set after T/F to it
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000240
Chris Lattnera51c7a82002-02-04 23:31:16 +0000241 MInst2LVSetBI[MI] = NewSet; // record in Before Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000242
243 // SetAI will be used in the next iteration
244 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000245 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000246}