2001-08-20 23:12:49 +02:00
|
|
|
/* Title: MethodLiveVarInfo.cpp
|
2001-07-24 19:14:13 +02:00
|
|
|
Author: Ruchira Sasanka
|
|
|
|
Date: Jun 30, 01
|
|
|
|
Purpose:
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
This is the interface for live variable info of a method that is required
|
|
|
|
by any other part of the compiler.
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
|
2001-08-20 23:12:49 +02:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2001-09-29 00:56:31 +02:00
|
|
|
#include "llvm/Support/PostOrderIterator.h"
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
/************************** Constructor/Destructor ***************************/
|
|
|
|
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
MethodLiveVarInfo::MethodLiveVarInfo(const Method *const M) : Meth(M),
|
|
|
|
BB2BBLVMap()
|
|
|
|
{
|
|
|
|
assert(! M->isExternal() ); // cannot be a prototype decleration
|
|
|
|
HasAnalyzed = false; // still we haven't called analyze()
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
MethodLiveVarInfo:: ~MethodLiveVarInfo()
|
|
|
|
{
|
2001-08-20 23:12:49 +02:00
|
|
|
// hash map iterator
|
|
|
|
BBToBBLiveVarMapType::iterator HMI = BB2BBLVMap.begin();
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
for( ; HMI != BB2BBLVMap.end() ; HMI ++ ) {
|
2001-08-20 23:12:49 +02:00
|
|
|
if( (*HMI).first ) // delete all LiveVarSets in BB2BBLVMap
|
2001-07-24 19:14:13 +02:00
|
|
|
delete (*HMI).second;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// -------------------------- support functions -------------------------------
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
// constructs BBLiveVars and init Def and In sets
|
2001-07-24 19:14:13 +02:00
|
|
|
void MethodLiveVarInfo::constructBBs()
|
|
|
|
{
|
2001-08-20 23:12:49 +02:00
|
|
|
unsigned int POId = 0; // Reverse Depth-first Order ID
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-09-29 00:56:31 +02:00
|
|
|
po_iterator<const Method*> BBI = po_begin(Meth);
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-09-29 00:56:31 +02:00
|
|
|
for( ; BBI != po_end(Meth) ; ++BBI, ++POId)
|
2001-07-24 19:14:13 +02:00
|
|
|
{
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
const BasicBlock *BB = *BBI; // get the current BB
|
2001-10-01 01:28:04 +02:00
|
|
|
|
2001-10-16 03:25:05 +02:00
|
|
|
if(DEBUG_LV) { cout << " For BB "; printValue(BB); cout << ":" << endl; }
|
2001-10-01 01:28:04 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
// create a new BBLiveVar
|
|
|
|
BBLiveVar * LVBB = new BBLiveVar( BB, POId );
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
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
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
if(DEBUG_LV)
|
|
|
|
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)
|
|
|
|
|
|
|
|
Method::const_iterator BBRI = Meth->begin(); // random iterator for BBs
|
|
|
|
|
|
|
|
for( ; BBRI != Meth->end(); ++BBRI, ++POId) {
|
|
|
|
|
|
|
|
if( ! BB2BBLVMap[ *BBRI ] )
|
|
|
|
BB2BBLVMap[ *BBRI ] = new BBLiveVar( *BBRI, POId );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
// do one backward pass over the CFG
|
2001-07-24 19:14:13 +02:00
|
|
|
bool MethodLiveVarInfo::doSingleBackwardPass()
|
|
|
|
{
|
|
|
|
bool ResultFlow, NeedAnotherIteration = false;
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
if(DEBUG_LV)
|
2001-10-16 03:25:05 +02:00
|
|
|
cout << endl << " After Backward Pass ..." << endl;
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-09-29 00:56:31 +02:00
|
|
|
po_iterator<const Method*> BBI = po_begin(Meth);
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-09-29 00:56:31 +02:00
|
|
|
for( ; BBI != po_end(Meth) ; ++BBI)
|
2001-07-24 19:14:13 +02:00
|
|
|
{
|
|
|
|
|
|
|
|
BBLiveVar* LVBB = BB2BBLVMap[*BBI];
|
|
|
|
assert( LVBB );
|
|
|
|
|
2001-10-16 03:25:05 +02:00
|
|
|
if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl;
|
|
|
|
// cout << " (POId=" << LVBB->getPOId() << ")" << endl ;
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
ResultFlow = false;
|
|
|
|
|
|
|
|
if( LVBB->isOutSetChanged() )
|
2001-08-20 23:12:49 +02:00
|
|
|
LVBB->applyTransferFunc(); // apply the Tran Func to calc InSet
|
|
|
|
|
|
|
|
if( LVBB->isInSetChanged() ) // to calc Outsets of preds
|
|
|
|
ResultFlow = LVBB->applyFlowFunc(BB2BBLVMap);
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
if(DEBUG_LV) LVBB->printInOutSets();
|
2001-08-20 23:12:49 +02:00
|
|
|
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
if( ResultFlow ) NeedAnotherIteration = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
// performs live var anal for a method
|
|
|
|
void MethodLiveVarInfo::analyze()
|
2001-07-24 19:14:13 +02:00
|
|
|
{
|
2001-08-29 00:36:35 +02:00
|
|
|
// Don't analyze the same method twice!
|
|
|
|
// Later, we need to add change notification here.
|
2001-10-16 03:25:05 +02:00
|
|
|
|
|
|
|
|
2001-08-29 00:36:35 +02:00
|
|
|
if (HasAnalyzed)
|
|
|
|
return;
|
2001-10-16 03:25:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
if( DEBUG_LV) cout << "Analysing live variables ..." << endl;
|
2001-08-20 23:12:49 +02:00
|
|
|
|
|
|
|
// create and initialize all the BBLiveVars of the CFG
|
|
|
|
constructBBs();
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
bool NeedAnotherIteration = false;
|
2001-08-20 23:12:49 +02:00
|
|
|
do { // do one pass over CFG
|
|
|
|
NeedAnotherIteration = doSingleBackwardPass( );
|
|
|
|
} while (NeedAnotherIteration ); // repeat until we need more iterations
|
|
|
|
|
|
|
|
|
|
|
|
HasAnalyzed = true; // finished analysing
|
|
|
|
|
2001-10-16 03:25:05 +02:00
|
|
|
if( DEBUG_LV) cout << "Live Variable Analysis complete!" << endl;
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
/* Thsese functions will give the LiveVar info for any machine instruction in
|
|
|
|
a method. It should be called after a call to analyze().
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
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-07-24 19:14:13 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
const LiveVarSet *
|
2001-08-20 23:12:49 +02:00
|
|
|
MethodLiveVarInfo::getLiveVarSetBeforeMInst(const MachineInstr *const MInst,
|
|
|
|
const BasicBlock *const CurBB)
|
2001-07-24 19:14:13 +02:00
|
|
|
{
|
2001-08-20 23:12:49 +02:00
|
|
|
const LiveVarSet *LVSet = MInst2LVSetBI[MInst];
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
if( LVSet ) return LVSet; // if found, just return the set
|
|
|
|
else {
|
|
|
|
calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
|
|
|
|
assert( MInst2LVSetBI[ MInst ] );
|
|
|
|
return MInst2LVSetBI[ MInst ];
|
|
|
|
}
|
|
|
|
}
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
const LiveVarSet *
|
|
|
|
MethodLiveVarInfo::getLiveVarSetAfterMInst(const MachineInstr *const MInst,
|
|
|
|
const BasicBlock *const CurBB)
|
|
|
|
{
|
|
|
|
const LiveVarSet *LVSet = MInst2LVSetAI[MInst];
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
if( LVSet ) return LVSet; // if found, just return the set
|
|
|
|
else {
|
|
|
|
calcLiveVarSetsForBB( CurBB ); // else, calc for all instrs in BB
|
|
|
|
assert( MInst2LVSetAI[ MInst ] );
|
|
|
|
return MInst2LVSetAI[ MInst ];
|
|
|
|
}
|
|
|
|
}
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
void MethodLiveVarInfo::calcLiveVarSetsForBB(const BasicBlock *const BB)
|
|
|
|
{
|
|
|
|
const MachineCodeForBasicBlock& MIVec = BB->getMachineInstrVec();
|
|
|
|
MachineCodeForBasicBlock::const_reverse_iterator
|
|
|
|
MInstIterator = MIVec.rbegin();
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
LiveVarSet *CurSet = new LiveVarSet();
|
2001-08-20 23:12:49 +02:00
|
|
|
const LiveVarSet *SetAI = getOutSetOfBB(BB); // init SetAI with OutSet
|
|
|
|
CurSet->setUnion(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
|
|
|
|
for( ; MInstIterator != MIVec.rend(); MInstIterator++) {
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
// MInst is cur machine inst
|
|
|
|
const MachineInstr * MInst = *MInstIterator;
|
|
|
|
|
|
|
|
MInst2LVSetAI[MInst] = SetAI; // record in After Inst map
|
|
|
|
|
|
|
|
CurSet->applyTranferFuncForMInst( MInst ); // apply the transfer Func
|
|
|
|
LiveVarSet *NewSet = new LiveVarSet(); // create a new set and
|
|
|
|
NewSet->setUnion( CurSet ); // copy the set after T/F to it
|
|
|
|
|
|
|
|
MInst2LVSetBI[MInst] = 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
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|