2002-02-05 03:51:01 +01:00
|
|
|
//===-- MethodLiveVarInfo.cpp - Live Variable Analysis for a Method -------===//
|
|
|
|
//
|
|
|
|
// This is the interface to method level live variable information that is
|
|
|
|
// provided by live variable analysis.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
|
2002-02-05 01:43:37 +01:00
|
|
|
#include "BBLiveVar.h"
|
2001-08-20 23:12:49 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-02-04 17:35:12 +01:00
|
|
|
#include "llvm/BasicBlock.h"
|
2001-11-27 01:03:19 +01:00
|
|
|
#include "Support/PostOrderIterator.h"
|
2002-02-05 04:35:53 +01:00
|
|
|
#include "Support/SetOperations.h"
|
2002-01-20 23:54:45 +01:00
|
|
|
#include <iostream>
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-04 21:00:08 +01:00
|
|
|
AnalysisID MethodLiveVarInfo::ID(AnalysisID::create<MethodLiveVarInfo>());
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Accessor Functions
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// gets OutSet of a BB
|
2002-02-05 03:51:01 +01:00
|
|
|
const ValueSet *MethodLiveVarInfo::getOutSetOfBB(const BasicBlock *BB) const {
|
2002-02-05 01:34:50 +01:00
|
|
|
return BB2BBLVMap.find(BB)->second->getOutSet();
|
|
|
|
}
|
|
|
|
|
|
|
|
// gets InSet of a BB
|
2002-02-05 03:51:01 +01:00
|
|
|
const ValueSet *MethodLiveVarInfo::getInSetOfBB(const BasicBlock *BB) const {
|
2002-02-05 01:34:50 +01:00
|
|
|
return BB2BBLVMap.find(BB)->second->getInSet();
|
|
|
|
}
|
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Performs live var analysis for a method
|
|
|
|
//-----------------------------------------------------------------------------
|
2001-12-08 22:05:27 +01:00
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
bool MethodLiveVarInfo::runOnMethod(Method *M) {
|
2002-02-05 00:31:16 +01:00
|
|
|
if (DEBUG_LV) std::cerr << "Analysing live variables ...\n";
|
2001-12-08 22:05:27 +01:00
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
// create and initialize all the BBLiveVars of the CFG
|
|
|
|
constructBBs(M);
|
2001-12-08 22:05:27 +01:00
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
while (doSingleBackwardPass(M))
|
|
|
|
; // Iterate until we are done.
|
|
|
|
|
2002-02-05 00:31:16 +01:00
|
|
|
if (DEBUG_LV) std::cerr << "Live Variable Analysis complete!\n";
|
2002-02-04 21:49:04 +01:00
|
|
|
return false;
|
2002-02-04 21:00:08 +01:00
|
|
|
}
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-08-20 23:12:49 +02:00
|
|
|
// constructs BBLiveVars and init Def and In sets
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
void MethodLiveVarInfo::constructBBs(const Method *M) {
|
2001-08-20 23:12:49 +02:00
|
|
|
unsigned int POId = 0; // Reverse Depth-first Order ID
|
2002-02-04 21:49:04 +01:00
|
|
|
|
|
|
|
for(po_iterator<const Method*> BBI = po_begin(M), BBE = po_end(M);
|
|
|
|
BBI != BBE; ++BBI, ++POId) {
|
2001-08-20 23:12:49 +02:00
|
|
|
const BasicBlock *BB = *BBI; // get the current BB
|
2001-10-01 01:28:04 +02:00
|
|
|
|
2002-02-05 02:43:49 +01:00
|
|
|
if (DEBUG_LV) std::cerr << " For BB " << RAV(BB) << ":\n";
|
2001-10-01 01:28:04 +02:00
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
// create a new BBLiveVar
|
|
|
|
BBLiveVar *LVBB = new BBLiveVar(BB, POId);
|
|
|
|
BB2BBLVMap[BB] = LVBB; // insert the pair to Map
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
LVBB->calcDefUseSets(); // calculates the def and in set
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 00:31:16 +01:00
|
|
|
if (DEBUG_LV)
|
2001-08-20 23:12:49 +02:00
|
|
|
LVBB->printAllSets();
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
2001-10-12 19:47:23 +02:00
|
|
|
|
|
|
|
// Since the PO iterator does not discover unreachable blocks,
|
|
|
|
// go over the random iterator and init those blocks as well.
|
|
|
|
// However, LV info is not correct for those blocks (they are not
|
|
|
|
// analyzed)
|
2002-02-04 21:49:04 +01:00
|
|
|
//
|
|
|
|
for (Method::const_iterator BBRI = M->begin(), BBRE = M->end();
|
|
|
|
BBRI != BBRE; ++BBRI, ++POId)
|
2002-02-05 00:31:16 +01:00
|
|
|
if (!BB2BBLVMap[*BBRI]) // Not yet processed?
|
2002-02-04 21:49:04 +01:00
|
|
|
BB2BBLVMap[*BBRI] = new BBLiveVar(*BBRI, POId);
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// do one backward pass over the CFG (for iterative analysis)
|
|
|
|
//-----------------------------------------------------------------------------
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 00:31:16 +01:00
|
|
|
bool MethodLiveVarInfo::doSingleBackwardPass(const Method *M) {
|
|
|
|
if (DEBUG_LV) std::cerr << "\n After Backward Pass ...\n";
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 00:31:16 +01:00
|
|
|
bool NeedAnotherIteration = false;
|
|
|
|
for (po_iterator<const Method*> BBI = po_begin(M); BBI != po_end(M) ; ++BBI) {
|
2002-02-04 21:49:04 +01:00
|
|
|
BBLiveVar *LVBB = BB2BBLVMap[*BBI];
|
2002-02-05 00:31:16 +01:00
|
|
|
assert(LVBB && "BasicBlock information not set for block!");
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 00:31:16 +01:00
|
|
|
if (DEBUG_LV) std::cerr << " For BB " << (*BBI)->getName() << ":\n";
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
if(LVBB->isOutSetChanged())
|
2001-08-20 23:12:49 +02:00
|
|
|
LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
|
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
if (LVBB->isInSetChanged()) // to calc Outsets of preds
|
|
|
|
NeedAnotherIteration |= LVBB->applyFlowFunc(BB2BBLVMap);
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 00:31:16 +01:00
|
|
|
if (DEBUG_LV) LVBB->printInOutSets();
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
// true if we need to reiterate over the CFG
|
|
|
|
return NeedAnotherIteration;
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
void MethodLiveVarInfo::releaseMemory() {
|
|
|
|
// First delete all BBLiveVar objects created in constructBBs(). A new object
|
2002-02-05 00:31:16 +01:00
|
|
|
// of type BBLiveVar is created for every BasicBlock in the method
|
2002-02-04 21:49:04 +01:00
|
|
|
//
|
2002-02-05 01:34:50 +01:00
|
|
|
for (std::map<const BasicBlock *, BBLiveVar *>::iterator
|
|
|
|
HMI = BB2BBLVMap.begin(),
|
2002-02-05 00:31:16 +01:00
|
|
|
HME = BB2BBLVMap.end(); HMI != HME; ++HMI)
|
2002-02-04 21:49:04 +01:00
|
|
|
delete HMI->second; // delete all BBLiveVar in BB2BBLVMap
|
2001-10-16 03:25:05 +02:00
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
BB2BBLVMap.clear();
|
2001-10-16 03:25:05 +02:00
|
|
|
|
2002-02-05 03:51:01 +01:00
|
|
|
// Then delete all objects of type ValueSet created in calcLiveVarSetsForBB
|
2002-02-04 21:49:04 +01:00
|
|
|
// and entered into MInst2LVSetBI and MInst2LVSetAI (these are caches
|
2002-02-05 03:51:01 +01:00
|
|
|
// to return ValueSet's before/after a machine instruction quickly). It
|
|
|
|
// is sufficient to free up all ValueSet using only one cache since
|
2002-02-04 21:49:04 +01:00
|
|
|
// both caches refer to the same sets
|
|
|
|
//
|
2002-02-05 03:51:01 +01:00
|
|
|
for (std::map<const MachineInstr*, const ValueSet*>::iterator
|
2002-02-05 01:34:50 +01:00
|
|
|
MI = MInst2LVSetBI.begin(),
|
2002-02-05 00:31:16 +01:00
|
|
|
ME = MInst2LVSetBI.end(); MI != ME; ++MI)
|
2002-02-05 03:51:01 +01:00
|
|
|
delete MI->second; // delete all ValueSets in MInst2LVSetBI
|
2001-08-20 23:12:49 +02:00
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
MInst2LVSetBI.clear();
|
|
|
|
MInst2LVSetAI.clear();
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-04 21:49:04 +01:00
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2002-02-05 00:31:16 +01:00
|
|
|
// Following functions will give the LiveVar info for any machine instr in
|
|
|
|
// a method. It should be called after a call to analyze().
|
|
|
|
//
|
|
|
|
// Thsese functions calucluates live var info for all the machine instrs in a
|
|
|
|
// BB when LVInfo for one inst is requested. Hence, this function is useful
|
|
|
|
// when live var info is required for many (or all) instructions in a basic
|
|
|
|
// block. Also, the arguments to this method does not require specific
|
|
|
|
// iterators.
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Gives live variable information before a machine instruction
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-02-05 00:31:16 +01:00
|
|
|
|
2002-02-05 03:51:01 +01:00
|
|
|
const ValueSet *
|
2002-02-04 21:49:04 +01:00
|
|
|
MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *MInst,
|
2002-02-05 00:31:16 +01:00
|
|
|
const BasicBlock *BB) {
|
2002-02-05 03:51:01 +01:00
|
|
|
if (const ValueSet *LVSet = MInst2LVSetBI[MInst]) {
|
2002-02-05 00:31:16 +01:00
|
|
|
return LVSet; // if found, just return the set
|
2002-02-04 21:49:04 +01:00
|
|
|
} else {
|
2002-02-05 00:31:16 +01:00
|
|
|
calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
|
2002-02-04 21:49:04 +01:00
|
|
|
return MInst2LVSetBI[MInst];
|
2001-08-20 23:12:49 +02:00
|
|
|
}
|
|
|
|
}
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Gives live variable information after a machine instruction
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-02-05 03:51:01 +01:00
|
|
|
const ValueSet *
|
2002-02-05 00:31:16 +01:00
|
|
|
MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *MI,
|
|
|
|
const BasicBlock *BB) {
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 03:51:01 +01:00
|
|
|
if (const ValueSet *LVSet = MInst2LVSetAI[MI]) {
|
2002-02-04 21:49:04 +01:00
|
|
|
return LVSet; // if found, just return the set
|
|
|
|
} else {
|
2002-02-05 00:31:16 +01:00
|
|
|
calcLiveVarSetsForBB(BB); // else, calc for all instrs in BB
|
|
|
|
return MInst2LVSetAI[MI];
|
2001-08-20 23:12:49 +02:00
|
|
|
}
|
|
|
|
}
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 03:51:01 +01:00
|
|
|
// This function applies a machine instr to a live var set (accepts OutSet) and
|
|
|
|
// makes necessary changes to it (produces InSet). Note that two for loops are
|
|
|
|
// used to first kill all defs and then to add all uses. This is because there
|
|
|
|
// can be instructions like Val = Val + 1 since we allow multipe defs to a
|
|
|
|
// machine instruction operand.
|
|
|
|
//
|
|
|
|
static void applyTranferFuncForMInst(ValueSet &LVS, const MachineInstr *MInst) {
|
|
|
|
for (MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done(); ++OpI) {
|
|
|
|
if (OpI.isDef()) // kill only if this operand is a def
|
|
|
|
LVS.insert(*OpI); // this definition kills any uses
|
|
|
|
}
|
|
|
|
|
|
|
|
// do for implicit operands as well
|
|
|
|
for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
|
|
|
|
if (MInst->implicitRefIsDefined(i))
|
|
|
|
LVS.erase(MInst->getImplicitRef(i));
|
|
|
|
}
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 03:51:01 +01:00
|
|
|
for (MachineInstr::val_const_op_iterator OpI(MInst); !OpI.done(); ++OpI) {
|
|
|
|
if (isa<BasicBlock>(*OpI)) continue; // don't process labels
|
|
|
|
|
|
|
|
if (!OpI.isDef()) // add only if this operand is a use
|
|
|
|
LVS.insert(*OpI); // An operand is a use - so add to use set
|
|
|
|
}
|
|
|
|
|
|
|
|
// do for implicit operands as well
|
|
|
|
for (unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
|
|
|
|
if (!MInst->implicitRefIsDefined(i))
|
|
|
|
LVS.insert(MInst->getImplicitRef(i));
|
|
|
|
}
|
|
|
|
}
|
2001-12-08 22:05:27 +01:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// This method calculates the live variable information for all the
|
|
|
|
// instructions in a basic block and enter the newly constructed live
|
2002-02-05 00:31:16 +01:00
|
|
|
// variable sets into a the caches (MInst2LVSetAI, MInst2LVSetBI)
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2002-02-05 00:31:16 +01:00
|
|
|
|
|
|
|
void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *BB) {
|
|
|
|
const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 03:51:01 +01:00
|
|
|
ValueSet *CurSet = new ValueSet();
|
|
|
|
const ValueSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
|
|
|
|
set_union(*CurSet, *SetAI); // CurSet now contains OutSet
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
// iterate over all the machine instructions in BB
|
2002-02-05 00:31:16 +01:00
|
|
|
for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
|
|
|
|
MIE = MIVec.rend(); MII != MIE; ++MII) {
|
|
|
|
// MI is cur machine inst
|
|
|
|
const MachineInstr *MI = *MII;
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 00:31:16 +01:00
|
|
|
MInst2LVSetAI[MI] = SetAI; // record in After Inst map
|
2001-08-20 23:12:49 +02:00
|
|
|
|
2002-02-05 03:51:01 +01:00
|
|
|
applyTranferFuncForMInst(*CurSet, MI); // apply the transfer Func
|
|
|
|
ValueSet *NewSet = new ValueSet(); // create a new set and
|
|
|
|
set_union(*NewSet, *CurSet); // copy the set after T/F to it
|
2001-08-20 23:12:49 +02:00
|
|
|
|
2002-02-05 00:31:16 +01:00
|
|
|
MInst2LVSetBI[MI] = NewSet; // record in Before Inst map
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
// SetAI will be used in the next iteration
|
|
|
|
SetAI = NewSet;
|
|
|
|
}
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|