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-02-12 23:39:50 +01:00
|
|
|
#include "llvm/Support/CFG.h"
|
2002-02-05 04:35:53 +01:00
|
|
|
#include "Support/SetOperations.h"
|
2002-02-25 00:01:21 +01:00
|
|
|
#include <iostream>
|
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 07:52:25 +01:00
|
|
|
static AnnotationID AID(AnnotationManager::getID("Analysis::BBLiveVar"));
|
|
|
|
|
|
|
|
BBLiveVar *BBLiveVar::CreateOnBB(const BasicBlock *BB, unsigned POID) {
|
|
|
|
BBLiveVar *Result = new BBLiveVar(BB, POID);
|
|
|
|
BB->addAnnotation(Result);
|
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
|
|
|
BBLiveVar *BBLiveVar::GetFromBB(const BasicBlock *BB) {
|
|
|
|
return (BBLiveVar*)BB->getAnnotation(AID);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BBLiveVar::RemoveFromBB(const BasicBlock *BB) {
|
|
|
|
bool Deleted = BB->deleteAnnotation(AID);
|
|
|
|
assert(Deleted && "BBLiveVar annotation did not exist!");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
BBLiveVar::BBLiveVar(const BasicBlock *bb, unsigned id)
|
2002-02-05 07:52:25 +01:00
|
|
|
: Annotation(AID), BB(bb), POID(id) {
|
2002-02-05 01:34:50 +01:00
|
|
|
InSetChanged = OutSetChanged = false;
|
2002-02-05 05:20:12 +01:00
|
|
|
|
|
|
|
calcDefUseSets();
|
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-03-18 04:47:26 +01:00
|
|
|
if (DEBUG_LV >= LV_DEBUG_Verbose) { // 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 07:02:59 +01:00
|
|
|
for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
|
|
|
|
OpI != OpE; ++OpI)
|
2002-02-05 01:34:50 +01:00
|
|
|
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
|
|
|
// iterate over MI operands to find uses
|
2002-02-05 07:02:59 +01:00
|
|
|
for (MachineInstr::const_val_op_iterator OpI = MI->begin(), OpE = MI->end();
|
|
|
|
OpI != OpE; ++OpI) {
|
2001-08-20 23:12:49 +02:00
|
|
|
const Value *Op = *OpI;
|
|
|
|
|
2002-02-05 07:02:59 +01:00
|
|
|
if (isa<BasicBlock>(Op))
|
2001-08-20 23:12:49 +02:00
|
|
|
continue; // don't process labels
|
|
|
|
|
2002-03-18 04:47:26 +01:00
|
|
|
if (!OpI.isDef()) { // add to Uses only if this operand is a use
|
|
|
|
|
|
|
|
//
|
|
|
|
// *** WARNING: The following code for handling dummy PHI machine
|
|
|
|
// instructions is untested. The previous code was broken and I
|
|
|
|
// fixed it, but it turned out to be unused as long as Phi elimination
|
|
|
|
// is performed during instruction selection.
|
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
if (MI->getOpCode() == PHI) { // for a phi node
|
2002-02-05 01:34:50 +01:00
|
|
|
const Value *ArgVal = Op;
|
2002-03-18 04:47:26 +01:00
|
|
|
const BasicBlock *PredBB = cast<BasicBlock>(*++OpI); // next ptr is BB
|
2001-10-12 19:47:23 +02:00
|
|
|
|
2002-03-18 04:47:26 +01:00
|
|
|
PredToEdgeInSetMap[PredBB].insert(ArgVal);
|
2001-10-12 19:47:23 +02:00
|
|
|
|
2002-03-18 04:47:26 +01:00
|
|
|
if (DEBUG_LV >= LV_DEBUG_Verbose)
|
2002-02-05 02:43:49 +01:00
|
|
|
cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
|
2002-03-18 04:47:26 +01:00
|
|
|
<< RAV(PredBB) << endl;
|
2001-10-12 19:47:23 +02:00
|
|
|
} // if( IsPhi )
|
2002-03-18 04:47:26 +01:00
|
|
|
else {
|
|
|
|
// It is not a Phi use: add to regular use set and remove later defs.
|
|
|
|
addUse(Op);
|
|
|
|
}
|
2001-10-12 19:47:23 +02:00
|
|
|
} // 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) {
|
2002-03-18 04:47:26 +01:00
|
|
|
assert(MI->getOpCode() != PHI && "Phi cannot have implicit opeands");
|
2002-02-05 01:34:50 +01:00
|
|
|
const Value *Op = MI->getImplicitRef(i);
|
2001-10-12 19:47:23 +02:00
|
|
|
|
2002-04-08 23:59:08 +02:00
|
|
|
if (Op->getType() == Type::LabelTy) // don't process labels
|
2002-02-05 01:34:50 +01:00
|
|
|
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-03-18 04:47:26 +01:00
|
|
|
InSet.erase(Op); // this definition kills any later uses
|
2001-10-12 19:47:23 +02:00
|
|
|
InSetChanged = true;
|
|
|
|
|
2002-03-18 04:47:26 +01:00
|
|
|
if (DEBUG_LV >= LV_DEBUG_Verbose) 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
|
2002-03-18 04:47:26 +01:00
|
|
|
DefSet.erase(Op); // remove if there is a def below this use
|
2001-10-12 19:47:23 +02:00
|
|
|
InSetChanged = true;
|
|
|
|
|
2002-03-18 04:47:26 +01:00
|
|
|
if (DEBUG_LV >= LV_DEBUG_Verbose) 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
|
2002-03-18 04:47:26 +01:00
|
|
|
// the OutSet.
|
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::applyTransferFunc() {
|
2001-07-24 19:14:13 +02:00
|
|
|
// IMPORTANT: caller should check whether the OutSet changed
|
|
|
|
// (else no point in calling)
|
|
|
|
|
2002-02-05 03:51:01 +01:00
|
|
|
ValueSet OutMinusDef = set_difference(OutSet, DefSet);
|
|
|
|
InSetChanged = set_union(InSet, 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
|
|
|
//-----------------------------------------------------------------------------
|
2002-03-18 04:47:26 +01:00
|
|
|
// calculates Out set using In sets of the successors
|
2001-12-08 22:05:27 +01:00
|
|
|
//-----------------------------------------------------------------------------
|
2001-07-24 19:14:13 +02:00
|
|
|
|
2002-02-05 03:51:01 +01:00
|
|
|
bool BBLiveVar::setPropagate(ValueSet *OutSet, const ValueSet *InSet,
|
2002-02-05 01:34:50 +01:00
|
|
|
const BasicBlock *PredBB) {
|
|
|
|
bool Changed = false;
|
2002-03-18 04:47:26 +01:00
|
|
|
|
|
|
|
// merge all members of InSet into OutSet of the predecessor
|
2002-02-05 03:51:01 +01:00
|
|
|
for (ValueSet::const_iterator InIt = InSet->begin(), InE = InSet->end();
|
2002-03-18 04:47:26 +01: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 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 07:52:25 +01:00
|
|
|
bool BBLiveVar::applyFlowFunc() {
|
2001-07-24 19:14:13 +02:00
|
|
|
// IMPORTANT: caller should check whether inset changed
|
|
|
|
// (else no point in calling)
|
2002-03-18 04:47:26 +01:00
|
|
|
|
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-03-18 04:47:26 +01:00
|
|
|
for (pred_const_iterator PI = pred_begin(BB), PE = pred_end(BB);
|
2002-02-12 23:39:50 +01:00
|
|
|
PI != PE ; ++PI) {
|
2002-02-05 07:52:25 +01:00
|
|
|
BBLiveVar *PredLVBB = BBLiveVar::GetFromBB(*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 {
|
2002-02-05 03:51:01 +01:00
|
|
|
cerr << " Defs: "; printSet(DefSet); cerr << "\n";
|
|
|
|
cerr << " In: "; printSet(InSet); cerr << "\n";
|
|
|
|
cerr << " Out: "; printSet(OutSet); cerr << "\n";
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
|
|
|
|
2002-02-05 01:34:50 +01:00
|
|
|
void BBLiveVar::printInOutSets() const {
|
2002-02-05 03:51:01 +01:00
|
|
|
cerr << " In: "; printSet(InSet); cerr << "\n";
|
|
|
|
cerr << " Out: "; printSet(OutSet); cerr << "\n";
|
2001-07-24 19:14:13 +02:00
|
|
|
}
|
2001-08-20 23:12:49 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|