2002-02-05 01:34:50 +01:00
|
|
|
//===-- BBLiveVar.cpp - Live Variable Analysis for a BasicBlock -----------===//
|
|
|
|
//
|
|
|
|
// This is a wrapper class for BasicBlock which is used by live var analysis.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2002-02-05 01:43:37 +01:00
|
|
|
#include "BBLiveVar.h"
|
2001-08-20 23:12:49 +02:00
|
|
|
#include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
|
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-02-04 17:35:12 +01:00
|
|
|
#include "llvm/BasicBlock.h"
|
2002-01-20 23:54:45 +01:00
|
|
|
|
|
|
|
/// BROKEN: Should not include sparc stuff directly into here
|
2001-12-08 22:05:27 +01:00
|
|
|
#include "../../Target/Sparc/SparcInternals.h" // Only for PHI defn
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-01-20 23:54:45 +01:00
|
|
|
using std::cerr;
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
BBLiveVar::BBLiveVar(const BasicBlock *bb, unsigned id)
|
|
|
|
: BB(bb), POID(id) {
|
|
|
|
InSetChanged = OutSetChanged = false;
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2002-02-05 01:34:50 +01:00
|
|
|
// calculates def and use sets for each BB
|
2001-08-20 23:12:49 +02:00
|
|
|
// 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-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
void BBLiveVar::calcDefUseSets() {
|
2001-08-20 23:12:49 +02:00
|
|
|
// get the iterator for machine instructions
|
2002-02-05 01:34:50 +01:00
|
|
|
const MachineCodeForBasicBlock &MIVec = BB->getMachineInstrVec();
|
2001-08-20 23:12:49 +02:00
|
|
|
|
|
|
|
// iterate over all the machine instructions in BB
|
2002-02-05 01:34:50 +01:00
|
|
|
for (MachineCodeForBasicBlock::const_reverse_iterator MII = MIVec.rbegin(),
|
|
|
|
MIE = MIVec.rend(); MII != MIE; ++MII) {
|
|
|
|
const MachineInstr *MI = *MII;
|
2001-08-20 23:12:49 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
if (DEBUG_LV > 1) { // debug msg
|
2001-10-15 20:30:06 +02:00
|
|
|
cerr << " *Iterating over machine instr ";
|
2002-02-05 01:34:50 +01:00
|
|
|
MI->dump();
|
2002-01-20 23:54:45 +01:00
|
|
|
cerr << "\n";
|
2001-08-20 23:12:49 +02:00
|
|
|
}
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
// iterate over MI operands to find defs
|
2002-02-05 01:34:50 +01:00
|
|
|
for (MachineInstr::val_const_op_iterator OpI(MI); !OpI.done(); ++OpI)
|
|
|
|
if (OpI.isDef()) // add to Defs only if this operand is a def
|
|
|
|
addDef(*OpI);
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-10-12 19:47:23 +02:00
|
|
|
// do for implicit operands as well
|
2002-02-05 01:34:50 +01:00
|
|
|
for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i)
|
|
|
|
if (MI->implicitRefIsDefined(i))
|
|
|
|
addDef(MI->getImplicitRef(i));
|
2001-10-12 19:47:23 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
bool IsPhi = MI->getOpCode() == PHI;
|
2001-08-20 23:12:49 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
// iterate over MI operands to find uses
|
|
|
|
for (MachineInstr::val_const_op_iterator OpI(MI); !OpI.done(); ++OpI) {
|
2001-08-20 23:12:49 +02:00
|
|
|
const Value *Op = *OpI;
|
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
if (Op->getType()->isLabelType())
|
2001-08-20 23:12:49 +02:00
|
|
|
continue; // don't process labels
|
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
if (!OpI.isDef()) { // add to Defs only if this operand is a use
|
|
|
|
addUse(Op);
|
2001-08-20 23:12:49 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
if (IsPhi) { // for a phi node
|
2001-10-12 19:47:23 +02:00
|
|
|
// put args into the PhiArgMap (Val -> BB)
|
2002-02-05 01:34:50 +01:00
|
|
|
const Value *ArgVal = Op;
|
|
|
|
const Value *BBVal = *++OpI; // increment to point to BB of value
|
2001-10-12 19:47:23 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
PhiArgMap[ArgVal] = cast<BasicBlock>(BBVal);
|
2001-10-12 19:47:23 +02:00
|
|
|
|
2002-02-05 02:43:49 +01:00
|
|
|
if (DEBUG_LV > 1)
|
|
|
|
cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
|
|
|
|
<< RAV(PhiArgMap[ArgVal]) << "\n";
|
2001-10-12 19:47:23 +02:00
|
|
|
} // if( IsPhi )
|
|
|
|
} // if a use
|
|
|
|
} // for all operands
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-10-12 19:47:23 +02:00
|
|
|
// do for implicit operands as well
|
2002-02-05 01:34:50 +01:00
|
|
|
for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
|
|
|
|
assert(!IsPhi && "Phi cannot have implicit opeands");
|
|
|
|
const Value *Op = MI->getImplicitRef(i);
|
2001-10-12 19:47:23 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
if (Op->getType()->isLabelType()) // don't process labels
|
|
|
|
continue;
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
if (!MI->implicitRefIsDefined(i))
|
|
|
|
addUse(Op);
|
|
|
|
}
|
2001-08-20 23:12:49 +02:00
|
|
|
} // for all machine instructions
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
2001-10-12 19:47:23 +02:00
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// To add an operand which is a def
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-02-05 01:34:50 +01:00
|
|
|
void BBLiveVar::addDef(const Value *Op) {
|
2002-02-04 17:35:12 +01:00
|
|
|
DefSet.insert(Op); // operand is a def - so add to def set
|
2002-02-05 01:34:50 +01:00
|
|
|
InSet.erase(Op); // this definition kills any uses
|
2001-10-12 19:47:23 +02:00
|
|
|
InSetChanged = true;
|
|
|
|
|
2002-02-05 02:43:49 +01:00
|
|
|
if (DEBUG_LV > 1) cerr << " +Def: " << RAV(Op) << "\n";
|
2001-10-12 19:47:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// To add an operand which is a use
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-02-05 01:34:50 +01:00
|
|
|
void BBLiveVar::addUse(const Value *Op) {
|
2002-02-04 17:35:12 +01:00
|
|
|
InSet.insert(Op); // An operand is a use - so add to use set
|
|
|
|
OutSet.erase(Op); // remove if there is a def below this use
|
2001-10-12 19:47:23 +02:00
|
|
|
InSetChanged = true;
|
|
|
|
|
2002-02-05 02:43:49 +01:00
|
|
|
if (DEBUG_LV > 1) cerr << " Use: " << RAV(Op) << "\n";
|
2001-10-12 19:47:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Applies the transfer function to a basic block to produce the InSet using
|
|
|
|
// the outset.
|
|
|
|
//-----------------------------------------------------------------------------
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
bool BBLiveVar::applyTransferFunc() {
|
2001-07-24 19:14:13 +02:00
|
|
|
// IMPORTANT: caller should check whether the OutSet changed
|
|
|
|
// (else no point in calling)
|
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
LiveVarSet OutMinusDef; // set to hold (Out[B] - Def[B])
|
2002-02-05 01:34:50 +01:00
|
|
|
OutMinusDef.setDifference(&OutSet, &DefSet);
|
|
|
|
InSetChanged = InSet.setUnion(&OutMinusDef);
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
OutSetChanged = false; // no change to OutSet since transf func applied
|
2001-07-24 19:14:13 +02:00
|
|
|
return InSetChanged;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-08-20 23:12:49 +02:00
|
|
|
// calculates Out set using In sets of the predecessors
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
bool BBLiveVar::setPropagate(LiveVarSet *OutSet, const LiveVarSet *InSet,
|
|
|
|
const BasicBlock *PredBB) {
|
|
|
|
bool Changed = false;
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
// for all all elements in InSet
|
2002-02-05 01:34:50 +01:00
|
|
|
for (LiveVarSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
|
|
|
|
InIt != InE; ++InIt) {
|
|
|
|
const BasicBlock *PredBBOfPhiArg = PhiArgMap[*InIt];
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2001-08-20 23:12:49 +02:00
|
|
|
// if this var is not a phi arg OR
|
|
|
|
// it's a phi arg and the var went down from this BB
|
2002-02-05 01:34:50 +01:00
|
|
|
if (!PredBBOfPhiArg || PredBBOfPhiArg == PredBB)
|
|
|
|
if (OutSet->insert(*InIt).second)
|
|
|
|
Changed = true;
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
return Changed;
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-08-20 23:12:49 +02:00
|
|
|
// propogates in set to OutSets of PREDECESSORs
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
bool BBLiveVar::applyFlowFunc(std::map<const BasicBlock *, BBLiveVar *> &LVMap){
|
2001-07-24 19:14:13 +02:00
|
|
|
// IMPORTANT: caller should check whether inset changed
|
|
|
|
// (else no point in calling)
|
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
// If this BB changed any OutSets of preds whose POID is lower, than we need
|
|
|
|
// another iteration...
|
|
|
|
//
|
|
|
|
bool needAnotherIt = false;
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
for (BasicBlock::pred_const_iterator PI = BB->pred_begin(),
|
|
|
|
PE = BB->pred_begin(); PI != PE ; ++PI) {
|
|
|
|
BBLiveVar *PredLVBB = LVMap[*PI];
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
// do set union
|
|
|
|
if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {
|
2001-07-24 19:14:13 +02:00
|
|
|
PredLVBB->OutSetChanged = true;
|
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
// if the predec POID is lower than mine
|
|
|
|
if (PredLVBB->getPOId() <= POID)
|
2001-07-24 19:14:13 +02:00
|
|
|
needAnotherIt = true;
|
|
|
|
}
|
2001-08-20 23:12:49 +02:00
|
|
|
} // for
|
2001-07-24 19:14:13 +02:00
|
|
|
|
|
|
|
return needAnotherIt;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
// ----------------- Methods For Debugging (Printing) -----------------
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
void BBLiveVar::printAllSets() const {
|
|
|
|
cerr << " Defs: "; DefSet.printSet(); cerr << "\n";
|
|
|
|
cerr << " In: "; InSet.printSet(); cerr << "\n";
|
|
|
|
cerr << " Out: "; OutSet.printSet(); cerr << "\n";
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
void BBLiveVar::printInOutSets() const {
|
|
|
|
cerr << " In: "; InSet.printSet(); cerr << "\n";
|
|
|
|
cerr << " Out: "; OutSet.printSet(); cerr << "\n";
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
2001-08-20 23:12:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|