2002-02-05 00:34:50 +00: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 00:43:37 +00:00
|
|
|
#include "BBLiveVar.h"
|
2003-01-14 23:05:08 +00:00
|
|
|
#include "llvm/CodeGen/FunctionLiveVarInfo.h"
|
2001-08-20 21:12:49 +00:00
|
|
|
#include "llvm/CodeGen/MachineInstr.h"
|
2002-10-28 01:41:47 +00:00
|
|
|
#include "llvm/CodeGen/MachineBasicBlock.h"
|
2002-02-12 22:39:50 +00:00
|
|
|
#include "llvm/Support/CFG.h"
|
2002-02-05 03:35:53 +00:00
|
|
|
#include "Support/SetOperations.h"
|
2002-01-20 22:54:45 +00:00
|
|
|
|
|
|
|
/// BROKEN: Should not include sparc stuff directly into here
|
2001-12-08 21:05:27 +00:00
|
|
|
#include "../../Target/Sparc/SparcInternals.h" // Only for PHI defn
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2002-01-20 22:54:45 +00:00
|
|
|
using std::cerr;
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2002-02-05 06:52:25 +00:00
|
|
|
static AnnotationID AID(AnnotationManager::getID("Analysis::BBLiveVar"));
|
|
|
|
|
2002-10-28 18:01:21 +00:00
|
|
|
BBLiveVar *BBLiveVar::CreateOnBB(const BasicBlock &BB, MachineBasicBlock &MBB,
|
|
|
|
unsigned POID) {
|
|
|
|
BBLiveVar *Result = new BBLiveVar(BB, MBB, POID);
|
2002-06-25 16:12:52 +00:00
|
|
|
BB.addAnnotation(Result);
|
2002-02-05 06:52:25 +00:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2002-06-25 16:12:52 +00:00
|
|
|
BBLiveVar *BBLiveVar::GetFromBB(const BasicBlock &BB) {
|
|
|
|
return (BBLiveVar*)BB.getAnnotation(AID);
|
2002-02-05 06:52:25 +00:00
|
|
|
}
|
|
|
|
|
2002-06-25 16:12:52 +00:00
|
|
|
void BBLiveVar::RemoveFromBB(const BasicBlock &BB) {
|
|
|
|
bool Deleted = BB.deleteAnnotation(AID);
|
2002-02-05 06:52:25 +00:00
|
|
|
assert(Deleted && "BBLiveVar annotation did not exist!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-10-28 18:01:21 +00:00
|
|
|
BBLiveVar::BBLiveVar(const BasicBlock &bb, MachineBasicBlock &mbb, unsigned id)
|
|
|
|
: Annotation(AID), BB(bb), MBB(mbb), POID(id) {
|
2002-02-05 00:34:50 +00:00
|
|
|
InSetChanged = OutSetChanged = false;
|
2002-02-05 04:20:12 +00:00
|
|
|
|
|
|
|
calcDefUseSets();
|
2001-07-24 17:14:13 +00:00
|
|
|
}
|
|
|
|
|
2001-12-08 21:05:27 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2002-02-05 00:34:50 +00:00
|
|
|
// calculates def and use sets for each BB
|
2001-08-20 21:12:49 +00: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 21:05:27 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2002-02-05 00:34:50 +00:00
|
|
|
void BBLiveVar::calcDefUseSets() {
|
2001-08-20 21:12:49 +00:00
|
|
|
// iterate over all the machine instructions in BB
|
2002-10-28 18:01:21 +00:00
|
|
|
for (MachineBasicBlock::const_reverse_iterator MII = MBB.rbegin(),
|
|
|
|
MIE = MBB.rend(); MII != MIE; ++MII) {
|
2002-02-05 00:34:50 +00:00
|
|
|
const MachineInstr *MI = *MII;
|
2001-08-20 21:12:49 +00:00
|
|
|
|
2002-10-28 18:01:21 +00:00
|
|
|
if (DEBUG_LV >= LV_DEBUG_Verbose) {
|
2001-10-15 18:30:06 +00:00
|
|
|
cerr << " *Iterating over machine instr ";
|
2002-02-05 00:34:50 +00:00
|
|
|
MI->dump();
|
2002-01-20 22:54:45 +00:00
|
|
|
cerr << "\n";
|
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
|
2002-02-05 06:02:59 +00:00
|
|
|
for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
|
|
|
|
OpI != OpE; ++OpI)
|
2003-05-27 00:06:48 +00:00
|
|
|
if (OpI.isDefOnly() || OpI.isDefAndUse()) // add to Defs if this operand is a def
|
2002-02-05 00:34:50 +00:00
|
|
|
addDef(*OpI);
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2001-10-12 17:47:23 +00:00
|
|
|
// do for implicit operands as well
|
2002-02-05 00:34:50 +00:00
|
|
|
for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i)
|
2003-05-27 00:06:48 +00:00
|
|
|
if (MI->getImplicitOp(i).opIsDefOnly() || MI->getImplicitOp(i).opIsDefAndUse())
|
2002-02-05 00:34:50 +00:00
|
|
|
addDef(MI->getImplicitRef(i));
|
2001-10-12 17:47:23 +00:00
|
|
|
|
2002-02-05 00:34:50 +00:00
|
|
|
// iterate over MI operands to find uses
|
2002-02-05 06:02:59 +00:00
|
|
|
for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
|
|
|
|
OpI != OpE; ++OpI) {
|
2001-08-20 21:12:49 +00:00
|
|
|
const Value *Op = *OpI;
|
|
|
|
|
2002-02-05 06:02:59 +00:00
|
|
|
if (isa<BasicBlock>(Op))
|
2001-08-20 21:12:49 +00:00
|
|
|
continue; // don't process labels
|
|
|
|
|
2003-05-27 00:06:48 +00:00
|
|
|
if (OpI.isUseOnly() || OpI.isDefAndUse()) {
|
2002-07-08 22:56:34 +00:00
|
|
|
// add to Uses only if this operand is a use
|
2002-03-18 03:47:26 +00:00
|
|
|
//
|
|
|
|
// *** WARNING: The following code for handling dummy PHI machine
|
|
|
|
// instructions is untested. The previous code was broken and I
|
2002-07-08 22:56:34 +00:00
|
|
|
// fixed it, but it turned out to be unused as long as Phi
|
|
|
|
// elimination is performed during instruction selection.
|
2002-03-18 03:47:26 +00:00
|
|
|
//
|
|
|
|
// Put Phi operands in UseSet for the incoming edge, not node.
|
|
|
|
// They must not "hide" later defs, and must be handled specially
|
|
|
|
// during set propagation over the CFG.
|
2003-05-20 20:32:24 +00:00
|
|
|
if (MI->getOpCode() == V9::PHI) { // for a phi node
|
2002-02-05 00:34:50 +00:00
|
|
|
const Value *ArgVal = Op;
|
2002-03-18 03:47:26 +00:00
|
|
|
const BasicBlock *PredBB = cast<BasicBlock>(*++OpI); // next ptr is BB
|
2001-10-12 17:47:23 +00:00
|
|
|
|
2002-03-18 03:47:26 +00:00
|
|
|
PredToEdgeInSetMap[PredBB].insert(ArgVal);
|
2001-10-12 17:47:23 +00:00
|
|
|
|
2002-03-18 03:47:26 +00:00
|
|
|
if (DEBUG_LV >= LV_DEBUG_Verbose)
|
2002-02-05 01:43:49 +00:00
|
|
|
cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
|
2002-06-25 20:34:54 +00:00
|
|
|
<< RAV(PredBB) << "\n";
|
2001-10-12 17:47:23 +00:00
|
|
|
} // if( IsPhi )
|
2002-03-18 03:47:26 +00:00
|
|
|
else {
|
|
|
|
// It is not a Phi use: add to regular use set and remove later defs.
|
|
|
|
addUse(Op);
|
|
|
|
}
|
2001-10-12 17:47:23 +00:00
|
|
|
} // 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
|
2002-02-05 00:34:50 +00:00
|
|
|
for (unsigned i = 0; i < MI->getNumImplicitRefs(); ++i) {
|
2003-05-20 20:36:39 +00:00
|
|
|
assert(MI->getOpCode() != V9::PHI && "Phi cannot have implicit operands");
|
2002-02-05 00:34:50 +00:00
|
|
|
const Value *Op = MI->getImplicitRef(i);
|
2001-10-12 17:47:23 +00:00
|
|
|
|
2002-04-08 21:59:08 +00:00
|
|
|
if (Op->getType() == Type::LabelTy) // don't process labels
|
2002-02-05 00:34:50 +00:00
|
|
|
continue;
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2003-05-27 00:06:48 +00:00
|
|
|
if (MI->getImplicitOp(i).opIsUse() || MI->getImplicitOp(i).opIsDefAndUse())
|
2002-02-05 00:34:50 +00:00
|
|
|
addUse(Op);
|
|
|
|
}
|
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
|
|
|
|
2001-12-08 21:05:27 +00:00
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// To add an operand which is a def
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-06-25 16:12:52 +00:00
|
|
|
void BBLiveVar::addDef(const Value *Op) {
|
2002-02-04 16:35:12 +00:00
|
|
|
DefSet.insert(Op); // operand is a def - so add to def set
|
2002-03-18 03:47:26 +00:00
|
|
|
InSet.erase(Op); // this definition kills any later uses
|
2001-10-12 17:47:23 +00:00
|
|
|
InSetChanged = true;
|
|
|
|
|
2002-03-18 03:47:26 +00:00
|
|
|
if (DEBUG_LV >= LV_DEBUG_Verbose) cerr << " +Def: " << RAV(Op) << "\n";
|
2001-10-12 17:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-08 21:05:27 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// To add an operand which is a use
|
|
|
|
//-----------------------------------------------------------------------------
|
2002-02-05 00:34:50 +00:00
|
|
|
void BBLiveVar::addUse(const Value *Op) {
|
2002-02-04 16:35:12 +00:00
|
|
|
InSet.insert(Op); // An operand is a use - so add to use set
|
2002-03-18 03:47:26 +00:00
|
|
|
DefSet.erase(Op); // remove if there is a def below this use
|
2001-10-12 17:47:23 +00:00
|
|
|
InSetChanged = true;
|
|
|
|
|
2002-03-18 03:47:26 +00:00
|
|
|
if (DEBUG_LV >= LV_DEBUG_Verbose) cerr << " Use: " << RAV(Op) << "\n";
|
2001-10-12 17:47:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-08 21:05:27 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// Applies the transfer function to a basic block to produce the InSet using
|
2002-03-18 03:47:26 +00:00
|
|
|
// the OutSet.
|
2001-12-08 21:05:27 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2002-02-05 00:34:50 +00:00
|
|
|
bool BBLiveVar::applyTransferFunc() {
|
2001-07-24 17:14:13 +00:00
|
|
|
// IMPORTANT: caller should check whether the OutSet changed
|
|
|
|
// (else no point in calling)
|
|
|
|
|
2002-02-05 02:51:01 +00:00
|
|
|
ValueSet OutMinusDef = set_difference(OutSet, DefSet);
|
|
|
|
InSetChanged = set_union(InSet, OutMinusDef);
|
2001-07-24 17:14:13 +00:00
|
|
|
|
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-12-08 21:05:27 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2002-03-18 03:47:26 +00:00
|
|
|
// calculates Out set using In sets of the successors
|
2001-12-08 21:05:27 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2002-02-05 02:51:01 +00:00
|
|
|
bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet,
|
2002-02-05 00:34:50 +00:00
|
|
|
const BasicBlock *PredBB) {
|
|
|
|
bool Changed = false;
|
2002-03-18 03:47:26 +00:00
|
|
|
|
|
|
|
// merge all members of InSet into OutSet of the predecessor
|
2002-02-05 02:51:01 +00:00
|
|
|
for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
|
2002-03-18 03:47:26 +00:00
|
|
|
InIt != InE; ++InIt)
|
|
|
|
if ((OutSet->insert(*InIt)).second)
|
|
|
|
Changed = true;
|
|
|
|
|
|
|
|
//
|
|
|
|
//**** WARNING: The following code for handling dummy PHI machine
|
|
|
|
// instructions is untested. See explanation above.
|
|
|
|
//
|
|
|
|
// then merge all members of the EdgeInSet for the predecessor into the OutSet
|
|
|
|
const ValueSet& EdgeInSet = PredToEdgeInSetMap[PredBB];
|
|
|
|
for (ValueSet::const_iterator InIt = EdgeInSet.begin(), InE = EdgeInSet.end();
|
|
|
|
InIt != InE; ++InIt)
|
|
|
|
if ((OutSet->insert(*InIt)).second)
|
|
|
|
Changed = true;
|
|
|
|
//
|
|
|
|
//****
|
|
|
|
|
2002-02-05 00:34:50 +00:00
|
|
|
return Changed;
|
2001-07-24 17:14:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-12-08 21:05:27 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2002-10-29 23:06:16 +00:00
|
|
|
// propagates in set to OutSets of PREDECESSORs
|
2001-12-08 21:05:27 +00:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2002-02-05 06:52:25 +00:00
|
|
|
bool BBLiveVar::applyFlowFunc() {
|
2001-07-24 17:14:13 +00:00
|
|
|
// IMPORTANT: caller should check whether inset changed
|
|
|
|
// (else no point in calling)
|
2002-03-18 03:47:26 +00:00
|
|
|
|
2002-02-05 00:34:50 +00:00
|
|
|
// If this BB changed any OutSets of preds whose POID is lower, than we need
|
|
|
|
// another iteration...
|
|
|
|
//
|
|
|
|
bool needAnotherIt = false;
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2002-06-25 16:12:52 +00:00
|
|
|
for (pred_const_iterator PI = pred_begin(&BB), PE = pred_end(&BB);
|
2002-02-12 22:39:50 +00:00
|
|
|
PI != PE ; ++PI) {
|
2002-06-25 16:12:52 +00:00
|
|
|
BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(**PI);
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2002-02-05 00:34:50 +00:00
|
|
|
// do set union
|
|
|
|
if (setPropagate(&PredLVBB->OutSet, &InSet, *PI)) {
|
2001-07-24 17:14:13 +00:00
|
|
|
PredLVBB->OutSetChanged = true;
|
|
|
|
|
2002-02-05 00:34:50 +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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2002-02-05 00:34:50 +00:00
|
|
|
// ----------------- Methods For Debugging (Printing) -----------------
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2002-02-05 00:34:50 +00:00
|
|
|
void BBLiveVar::printAllSets() const {
|
2002-02-05 02:51:01 +00:00
|
|
|
cerr << " Defs: "; printSet(DefSet); cerr << "\n";
|
|
|
|
cerr << " In: "; printSet(InSet); cerr << "\n";
|
|
|
|
cerr << " Out: "; printSet(OutSet); cerr << "\n";
|
2001-07-24 17:14:13 +00:00
|
|
|
}
|
|
|
|
|
2002-02-05 00:34:50 +00:00
|
|
|
void BBLiveVar::printInOutSets() const {
|
2002-02-05 02:51:01 +00:00
|
|
|
cerr << " In: "; printSet(InSet); cerr << "\n";
|
|
|
|
cerr << " Out: "; printSet(OutSet); cerr << "\n";
|
2001-07-24 17:14:13 +00:00
|
|
|
}
|
2001-08-20 21:12:49 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|