blob: 645735c2cc712720a45bf7e68e7186c9ea45d2e8 [file] [log] [blame]
Ruchira Sasankae27c3442001-08-20 21:12:49 +00001/* Title: MethodLiveVarInfo.cpp
Ruchira Sasanka683847f2001-07-24 17:14:13 +00002 Author: Ruchira Sasanka
3 Date: Jun 30, 01
4 Purpose:
5
Ruchira Sasankae27c3442001-08-20 21:12:49 +00006 This is the interface for live variable info of a method that is required
7 by any other part of the compiler.
Ruchira Sasanka683847f2001-07-24 17:14:13 +00008
9*/
10
11
12#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
Ruchira Sasankae27c3442001-08-20 21:12:49 +000013#include "llvm/CodeGen/MachineInstr.h"
Chris Lattner11646322002-02-04 16:35:12 +000014#include "llvm/BasicBlock.h"
Chris Lattnercee8f9a2001-11-27 00:03:19 +000015#include "Support/PostOrderIterator.h"
Chris Lattner697954c2002-01-20 22:54:45 +000016#include <iostream>
Ruchira Sasanka683847f2001-07-24 17:14:13 +000017
Chris Lattner4fd2dbb2002-02-04 20:00:08 +000018AnalysisID MethodLiveVarInfo::ID(AnalysisID::create<MethodLiveVarInfo>());
Ruchira Sasanka683847f2001-07-24 17:14:13 +000019
Chris Lattnerbdfd3282002-02-04 20:49:04 +000020
21//-----------------------------------------------------------------------------
22// Performs live var analysis for a method
23//-----------------------------------------------------------------------------
24
25bool MethodLiveVarInfo::runOnMethod(Method *M) {
Chris Lattnera51c7a82002-02-04 23:31:16 +000026 if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000027
28 // create and initialize all the BBLiveVars of the CFG
29 constructBBs(M);
30
31 while (doSingleBackwardPass(M))
32 ; // Iterate until we are done.
33
Chris Lattnera51c7a82002-02-04 23:31:16 +000034 if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000035 return false;
36}
37
38
39//-----------------------------------------------------------------------------
40// constructs BBLiveVars and init Def and In sets
41//-----------------------------------------------------------------------------
42
43void MethodLiveVarInfo::constructBBs(const Method *M) {
44 unsigned int POId = 0; // Reverse Depth-first Order ID
45
46 for(po_iterator<const Method*> BBI = po_begin(M), BBE = po_end(M);
47 BBI != BBE; ++BBI, ++POId) {
48 const BasicBlock *BB = *BBI; // get the current BB
49
Chris Lattnera51c7a82002-02-04 23:31:16 +000050 if (DEBUG_LV) { std::cerr << " For BB "; printValue(BB); cerr << ":\n"; }
Chris Lattnerbdfd3282002-02-04 20:49:04 +000051
52 // create a new BBLiveVar
53 BBLiveVar *LVBB = new BBLiveVar(BB, POId);
54 BB2BBLVMap[BB] = LVBB; // insert the pair to Map
55
56 LVBB->calcDefUseSets(); // calculates the def and in set
57
Chris Lattnera51c7a82002-02-04 23:31:16 +000058 if (DEBUG_LV)
Chris Lattnerbdfd3282002-02-04 20:49:04 +000059 LVBB->printAllSets();
60 }
61
62 // Since the PO iterator does not discover unreachable blocks,
63 // go over the random iterator and init those blocks as well.
64 // However, LV info is not correct for those blocks (they are not
65 // analyzed)
66 //
67 for (Method::const_iterator BBRI = M->begin(), BBRE = M->end();
68 BBRI != BBRE; ++BBRI, ++POId)
Chris Lattnera51c7a82002-02-04 23:31:16 +000069 if (!BB2BBLVMap[*BBRI]) // Not yet processed?
Chris Lattnerbdfd3282002-02-04 20:49:04 +000070 BB2BBLVMap[*BBRI] = new BBLiveVar(*BBRI, POId);
71}
72
73
74//-----------------------------------------------------------------------------
75// do one backward pass over the CFG (for iterative analysis)
76//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +000077
Chris Lattnerbdfd3282002-02-04 20:49:04 +000078bool MethodLiveVarInfo::doSingleBackwardPass(const Method *M) {
Chris Lattnera51c7a82002-02-04 23:31:16 +000079 if (DEBUG_LV) std::cerr << "\n After Backward Pass ...\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000080
Chris Lattnera51c7a82002-02-04 23:31:16 +000081 bool NeedAnotherIteration = false;
82 for (po_iterator<const Method*> BBI = po_begin(M); BBI != po_end(M) ; ++BBI) {
Chris Lattnerbdfd3282002-02-04 20:49:04 +000083 BBLiveVar *LVBB = BB2BBLVMap[*BBI];
Chris Lattnera51c7a82002-02-04 23:31:16 +000084 assert(LVBB && "BasicBlock information not set for block!");
Chris Lattnerbdfd3282002-02-04 20:49:04 +000085
Chris Lattnera51c7a82002-02-04 23:31:16 +000086 if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
Chris Lattnerbdfd3282002-02-04 20:49:04 +000087
88 if(LVBB->isOutSetChanged())
89 LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
90
91 if (LVBB->isInSetChanged()) // to calc Outsets of preds
92 NeedAnotherIteration |= LVBB->applyFlowFunc(BB2BBLVMap);
93
Chris Lattnera51c7a82002-02-04 23:31:16 +000094 if (DEBUG_LV) LVBB->printInOutSets();
Chris Lattnerbdfd3282002-02-04 20:49:04 +000095 }
96
97 // true if we need to reiterate over the CFG
98 return NeedAnotherIteration;
99}
100
101
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000102void MethodLiveVarInfo::releaseMemory() {
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000103 // First delete all BBLiveVar objects created in constructBBs(). A new object
Chris Lattnera51c7a82002-02-04 23:31:16 +0000104 // of type BBLiveVar is created for every BasicBlock in the method
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000105 //
Chris Lattnera51c7a82002-02-04 23:31:16 +0000106 for (BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin(),
107 HME = BB2BBLVMap.end(); HMI != HME; ++HMI)
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000108 delete HMI->second; // delete all BBLiveVar in BB2BBLVMap
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000109
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000110 BB2BBLVMap.clear();
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000111
112 // Then delete all objects of type LiveVarSet created in calcLiveVarSetsForBB
113 // and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
114 // to return LiveVarSet's before/after a machine instruction quickly). It
115 // is sufficient to free up all LiveVarSet using only one cache since
116 // both caches refer to the same sets
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000117 //
Chris Lattnera51c7a82002-02-04 23:31:16 +0000118 for (MInstToLiveVarSetMapType::iterator MI = MInst2LVSetBI.begin(),
119 ME = MInst2LVSetBI.end(); MI != ME; ++MI)
Chris Lattner4fd2dbb2002-02-04 20:00:08 +0000120 delete MI->second; // delete all LiveVarSets in MInst2LVSetBI
121
122 MInst2LVSetBI.clear();
123 MInst2LVSetAI.clear();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000124}
125
126
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000127
128
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000129//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000130// Following functions will give the LiveVar info for any machine instr in
131// a method. It should be called after a call to analyze().
132//
133// Thsese functions calucluates live var info for all the machine instrs in a
134// BB when LVInfo for one inst is requested. Hence, this function is useful
135// when live var info is required for many (or all) instructions in a basic
136// block. Also, the arguments to this method does not require specific
137// iterators.
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000138//-----------------------------------------------------------------------------
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000139
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000140//-----------------------------------------------------------------------------
141// Gives live variable information before a machine instruction
142//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000143
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000144const LiveVarSet *
145MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
Chris Lattnera51c7a82002-02-04 23:31:16 +0000146 const BasicBlock *BB) {
147 if (const LiveVarSet *LVSet = MInst2LVSetBI[MInst]) {
148 return LVSet; // if found, just return the set
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000149 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000150 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000151 return MInst2LVSetBI[MInst];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000152 }
153}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000154
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000155
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000156//-----------------------------------------------------------------------------
157// Gives live variable information after a machine instruction
158//-----------------------------------------------------------------------------
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000159const LiveVarSet *
Chris Lattnera51c7a82002-02-04 23:31:16 +0000160MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
161 const BasicBlock *BB) {
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000162
Chris Lattnera51c7a82002-02-04 23:31:16 +0000163 if (const LiveVarSet *LVSet = MInst2LVSetAI[MI]) {
Chris Lattnerbdfd3282002-02-04 20:49:04 +0000164 return LVSet; // if found, just return the set
165 } else {
Chris Lattnera51c7a82002-02-04 23:31:16 +0000166 calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
167 return MInst2LVSetAI[MI];
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000168 }
169}
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000170
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000171
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000172
173//-----------------------------------------------------------------------------
174// This method calculates the live variable information for all the
175// instructions in a basic block and enter the newly constructed live
Chris Lattnera51c7a82002-02-04 23:31:16 +0000176// variable sets into a the caches (MInst2LVSetAI, MInst2LVSetBI)
Ruchira Sasanka789cebb2001-12-08 21:05:27 +0000177//-----------------------------------------------------------------------------
Chris Lattnera51c7a82002-02-04 23:31:16 +0000178
179void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
180 const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000181
182 LiveVarSet *CurSet = new LiveVarSet();
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000183 const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
184 CurSet->setUnion(SetAI); // CurSet now contains OutSet
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000185
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000186 // iterate over all the machine instructions in BB
Chris Lattnera51c7a82002-02-04 23:31:16 +0000187 for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
188 MIE = MIVec.rend(); MII != MIE; ++MII) {
189 // MI is cur machine inst
190 const MachineInstr *MI = *MII;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000191
Chris Lattnera51c7a82002-02-04 23:31:16 +0000192 MInst2LVSetAI[MI] = SetAI; // record in After Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000193
Chris Lattnera51c7a82002-02-04 23:31:16 +0000194 CurSet->applyTranferFuncForMInst(MI); // apply the transfer Func
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000195 LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
Chris Lattnera51c7a82002-02-04 23:31:16 +0000196 NewSet->setUnion(CurSet); // copy the set after T/F to it
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000197
Chris Lattnera51c7a82002-02-04 23:31:16 +0000198 MInst2LVSetBI[MI] = NewSet; // record in Before Inst map
Ruchira Sasankae27c3442001-08-20 21:12:49 +0000199
200 // SetAI will be used in the next iteration
201 SetAI = NewSet;
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000202 }
Ruchira Sasanka683847f2001-07-24 17:14:13 +0000203}