2001-07-24 17:14:13 +00:00
|
|
|
#include "llvm/Analysis/LiveVar/BBLiveVar.h"
|
2001-08-20 21:12:49 +00:00
|
|
|
#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2001-09-14 03:55:11 +00:00
|
|
|
#include "../../Target/Sparc/SparcInternals.h" // TODO: FIXME!! Only for PHI defn
|
2001-07-24 17:14:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
/********************* Implementation **************************************/
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
BBLiveVar::BBLiveVar( const BasicBlock *const baseBB, unsigned int RdfoId)
|
|
|
|
: BaseBB(baseBB), DefSet(), InSet(),
|
|
|
|
OutSet(), PhiArgMap() {
|
2001-07-24 17:14:13 +00:00
|
|
|
BaseBB = baseBB;
|
|
|
|
InSetChanged = OutSetChanged = false;
|
|
|
|
POId = RdfoId;
|
|
|
|
}
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
// caluculates def and use sets for each BB
|
|
|
|
// There are two passes over operands of a machine instruction. This is
|
|
|
|
// because, we can have instructions like V = V + 1, since we no longer
|
|
|
|
// assume single definition.
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
void BBLiveVar::calcDefUseSets()
|
2001-07-24 17:14:13 +00:00
|
|
|
{
|
2001-08-20 21:12:49 +00:00
|
|
|
// get the iterator for machine instructions
|
|
|
|
const MachineCodeForBasicBlock& MIVec = BaseBB->getMachineInstrVec();
|
|
|
|
MachineCodeForBasicBlock::const_reverse_iterator
|
|
|
|
MInstIterator = MIVec.rbegin();
|
|
|
|
|
|
|
|
// iterate over all the machine instructions in BB
|
|
|
|
for( ; MInstIterator != MIVec.rend(); ++MInstIterator) {
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
const MachineInstr * MInst = *MInstIterator; // MInst is the machine inst
|
|
|
|
assert(MInst);
|
|
|
|
|
|
|
|
if( DEBUG_LV > 1) { // debug msg
|
2001-10-15 18:30:06 +00:00
|
|
|
cerr << " *Iterating over machine instr ";
|
2001-08-20 21:12:49 +00:00
|
|
|
MInst->dump();
|
2001-10-15 18:30:06 +00:00
|
|
|
cerr << endl;
|
2001-08-20 21:12:49 +00:00
|
|
|
}
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
// iterate over MI operands to find defs
|
|
|
|
for( MachineInstr::val_op_const_iterator OpI(MInst); !OpI.done() ; ++OpI) {
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2001-10-12 17:47:23 +00:00
|
|
|
if( OpI.isDef() ) // add to Defs only if this operand is a def
|
|
|
|
addDef( *OpI );
|
2001-07-24 17:14:13 +00:00
|
|
|
}
|
|
|
|
|
2001-10-12 17:47:23 +00:00
|
|
|
// do for implicit operands as well
|
|
|
|
for( unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
|
|
|
|
if( MInst->implicitRefIsDefined(i) )
|
|
|
|
addDef( MInst->getImplicitRef(i) );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
bool IsPhi = ( MInst->getOpCode() == PHI );
|
|
|
|
|
|
|
|
|
|
|
|
// iterate over MI operands to find uses
|
|
|
|
for(MachineInstr::val_op_const_iterator OpI(MInst); !OpI.done() ; ++OpI) {
|
|
|
|
const Value *Op = *OpI;
|
|
|
|
|
|
|
|
if ( ((Op)->getType())->isLabelType() )
|
|
|
|
continue; // don't process labels
|
|
|
|
|
2001-10-12 17:47:23 +00:00
|
|
|
if(! OpI.isDef() ) { // add to Defs only if this operand is a use
|
|
|
|
addUse( Op );
|
2001-08-20 21:12:49 +00:00
|
|
|
|
|
|
|
if( IsPhi ) { // for a phi node
|
2001-10-12 17:47:23 +00:00
|
|
|
// put args into the PhiArgMap (Val -> BB)
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
const Value * ArgVal = Op;
|
|
|
|
++OpI; // increment to point to BB of value
|
|
|
|
const Value * BBVal = *OpI;
|
2001-10-12 17:47:23 +00:00
|
|
|
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
assert( (BBVal)->getValueType() == Value::BasicBlockVal );
|
|
|
|
|
|
|
|
PhiArgMap[ ArgVal ] = (const BasicBlock *) (BBVal);
|
|
|
|
assert( PhiArgMap[ ArgVal ] );
|
2001-10-12 17:47:23 +00:00
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
if( DEBUG_LV > 1) { // debug msg of level 2
|
2001-10-15 18:30:06 +00:00
|
|
|
cerr << " - phi operand ";
|
2001-08-20 21:12:49 +00:00
|
|
|
printValue( ArgVal );
|
2001-10-15 18:30:06 +00:00
|
|
|
cerr << " came from BB ";
|
2001-08-20 21:12:49 +00:00
|
|
|
printValue( PhiArgMap[ ArgVal ]);
|
2001-10-15 18:30:06 +00:00
|
|
|
cerr<<endl;
|
2001-08-20 21:12:49 +00:00
|
|
|
}
|
|
|
|
|
2001-10-12 17:47:23 +00:00
|
|
|
} // if( IsPhi )
|
|
|
|
|
|
|
|
} // if a use
|
|
|
|
|
|
|
|
} // for all operands
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2001-10-12 17:47:23 +00:00
|
|
|
// do for implicit operands as well
|
|
|
|
for( unsigned i=0; i < MInst->getNumImplicitRefs(); ++i) {
|
|
|
|
|
|
|
|
assert( !IsPhi && "Phi cannot have implicit opeands");
|
|
|
|
const Value *Op = MInst->getImplicitRef(i);
|
|
|
|
|
|
|
|
if ( ((Op)->getType())->isLabelType() )
|
|
|
|
continue; // don't process labels
|
|
|
|
if( ! MInst->implicitRefIsDefined(i) )
|
|
|
|
addUse( Op );
|
|
|
|
}
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
} // for all machine instructions
|
2001-07-24 17:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-10-12 17:47:23 +00:00
|
|
|
// To add an operand wichi is a def
|
|
|
|
|
|
|
|
void BBLiveVar::addDef(const Value *Op)
|
|
|
|
{
|
|
|
|
DefSet.add( Op ); // operand is a def - so add to def set
|
|
|
|
InSet.remove( Op); // this definition kills any uses
|
|
|
|
InSetChanged = true;
|
|
|
|
|
|
|
|
if( DEBUG_LV > 1) {
|
2001-10-15 18:30:06 +00:00
|
|
|
cerr << " +Def: "; printValue( Op ); cerr << endl;
|
2001-10-12 17:47:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// To add an operand which is a use
|
|
|
|
|
|
|
|
void BBLiveVar::addUse(const Value *Op)
|
|
|
|
{
|
|
|
|
InSet.add( Op ); // An operand is a use - so add to use set
|
|
|
|
OutSet.remove( Op ); // remove if there is a def below this use
|
|
|
|
InSetChanged = true;
|
|
|
|
|
|
|
|
if( DEBUG_LV > 1) { // debug msg of level 2
|
2001-10-15 18:30:06 +00:00
|
|
|
cerr << " Use: "; printValue( Op ); cerr << endl;
|
2001-10-12 17:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-07-24 17:14:13 +00:00
|
|
|
|
|
|
|
bool BBLiveVar::applyTransferFunc() // calculates the InSet in terms of OutSet
|
|
|
|
{
|
|
|
|
|
|
|
|
// IMPORTANT: caller should check whether the OutSet changed
|
|
|
|
// (else no point in calling)
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
LiveVarSet OutMinusDef; // set to hold (Out[B] - Def[B])
|
2001-07-24 17:14:13 +00:00
|
|
|
OutMinusDef.setDifference( &OutSet, &DefSet);
|
|
|
|
InSetChanged = InSet.setUnion( &OutMinusDef );
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
OutSetChanged = false; // no change to OutSet since transf func applied
|
2001-07-24 17:14:13 +00:00
|
|
|
|
|
|
|
return InSetChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
// calculates Out set using In sets of the predecessors
|
2001-07-24 17:14:13 +00:00
|
|
|
bool BBLiveVar::setPropagate( LiveVarSet *const OutSet,
|
|
|
|
const LiveVarSet *const InSet,
|
|
|
|
const BasicBlock *const PredBB) {
|
|
|
|
|
|
|
|
LiveVarSet::const_iterator InIt;
|
|
|
|
pair<LiveVarSet::iterator, bool> result;
|
|
|
|
bool changed = false;
|
|
|
|
const BasicBlock *PredBBOfPhiArg;
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
// for all all elements in InSet
|
2001-07-24 17:14:13 +00:00
|
|
|
for( InIt = InSet->begin() ; InIt != InSet->end(); InIt++) {
|
|
|
|
PredBBOfPhiArg = PhiArgMap[ *InIt ];
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
// if this var is not a phi arg OR
|
|
|
|
// it's a phi arg and the var went down from this BB
|
2001-07-24 17:14:13 +00:00
|
|
|
if( !PredBBOfPhiArg || PredBBOfPhiArg == PredBB) {
|
|
|
|
result = OutSet->insert( *InIt ); // insert to this set
|
|
|
|
if( result.second == true) changed = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return changed;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
// propogates in set to OutSets of PREDECESSORs
|
|
|
|
|
2001-07-24 17:14:13 +00:00
|
|
|
bool BBLiveVar::applyFlowFunc(BBToBBLiveVarMapType LVMap)
|
|
|
|
{
|
|
|
|
|
|
|
|
// IMPORTANT: caller should check whether inset changed
|
|
|
|
// (else no point in calling)
|
|
|
|
|
|
|
|
bool needAnotherIt= false; // did this BB change any OutSets of pred.s
|
|
|
|
// whose POId is lower
|
|
|
|
|
|
|
|
|
2001-10-01 13:19:53 +00:00
|
|
|
BasicBlock::pred_const_iterator PredBBI = BaseBB->pred_begin();
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2001-10-01 13:19:53 +00:00
|
|
|
for( ; PredBBI != BaseBB->pred_end() ; PredBBI++) {
|
2001-08-20 21:12:49 +00:00
|
|
|
assert( *PredBBI ); // assert that the predecessor is valid
|
2001-07-24 17:14:13 +00:00
|
|
|
BBLiveVar *PredLVBB = LVMap[*PredBBI];
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
// do set union
|
2001-07-24 17:14:13 +00:00
|
|
|
if( setPropagate( &(PredLVBB->OutSet), &InSet, *PredBBI ) == true) {
|
|
|
|
PredLVBB->OutSetChanged = true;
|
|
|
|
|
2001-08-20 21:12:49 +00:00
|
|
|
// if the predec POId is lower than mine
|
|
|
|
if( PredLVBB->getPOId() <= POId)
|
2001-07-24 17:14:13 +00:00
|
|
|
needAnotherIt = true;
|
|
|
|
}
|
2001-08-20 21:12:49 +00:00
|
|
|
|
|
|
|
} // for
|
2001-07-24 17:14:13 +00:00
|
|
|
|
|
|
|
return needAnotherIt;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ----------------- Methods For Debugging (Printing) ----------------- */
|
|
|
|
|
|
|
|
void BBLiveVar::printAllSets() const
|
|
|
|
{
|
2001-10-15 18:30:06 +00:00
|
|
|
cerr << " Defs: "; DefSet.printSet(); cerr << endl;
|
|
|
|
cerr << " In: "; InSet.printSet(); cerr << endl;
|
|
|
|
cerr << " Out: "; OutSet.printSet(); cerr << endl;
|
2001-07-24 17:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void BBLiveVar::printInOutSets() const
|
|
|
|
{
|
2001-10-15 18:30:06 +00:00
|
|
|
cerr << " In: "; InSet.printSet(); cerr << endl;
|
|
|
|
cerr << " Out: "; OutSet.printSet(); cerr << endl;
|
2001-07-24 17:14:13 +00:00
|
|
|
}
|
2001-08-20 21:12:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|