2002-02-05 00:34:50 +00:00
|
|
|
//===-- BBLiveVar.cpp - Live Variable Analysis for a BasicBlock -----------===//
|
2003-10-20 19:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by the LLVM research group and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
2002-02-05 00:34:50 +00:00
|
|
|
//
|
|
|
|
// 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"
|
2004-02-24 19:46:00 +00:00
|
|
|
#include "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"
|
2004-01-09 18:15:24 +00:00
|
|
|
#include "../SparcInternals.h"
|
2001-07-24 17:14:13 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
namespace llvm {
|
2002-02-05 06:52:25 +00:00
|
|
|
|
2004-02-11 17:55:09 +00:00
|
|
|
BBLiveVar::BBLiveVar(const BasicBlock &bb,
|
|
|
|
const MachineBasicBlock &mbb,
|
|
|
|
unsigned id)
|
2003-10-20 20:52:23 +00:00
|
|
|
: 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) {
|
2004-02-12 02:27:10 +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) {
|
2003-10-20 20:38:17 +00:00
|
|
|
std::cerr << " *Iterating over machine instr ";
|
2002-02-05 00:34:50 +00:00
|
|
|
MI->dump();
|
2003-10-20 20:38:17 +00:00
|
|
|
std::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-12-14 13:24:17 +00:00
|
|
|
if (OpI.isDef()) // 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-12-14 13:24:17 +00:00
|
|
|
if (MI->getImplicitOp(i).isDef())
|
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-12-14 13:24:17 +00:00
|
|
|
if (OpI.isUse()) { // 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.
|
2004-02-11 20:47:34 +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)
|
2003-10-20 20:38:17 +00:00
|
|
|
std::cerr << " - phi operand " << RAV(ArgVal) << " came from BB "
|
|
|
|
<< 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) {
|
2004-02-11 20:47:34 +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-12-14 13:24:17 +00:00
|
|
|
if (MI->getImplicitOp(i).isUse())
|
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;
|
|
|
|
|
2003-10-20 20:38:17 +00:00
|
|
|
if (DEBUG_LV >= LV_DEBUG_Verbose) std::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;
|
|
|
|
|
2003-10-20 20:38:17 +00:00
|
|
|
if (DEBUG_LV >= LV_DEBUG_Verbose) std::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
|
|
|
|
2003-10-20 20:52:23 +00:00
|
|
|
bool BBLiveVar::applyFlowFunc(hash_map<const BasicBlock*,
|
|
|
|
BBLiveVar*> &BBLiveVarInfo) {
|
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) {
|
2003-10-20 20:52:23 +00:00
|
|
|
BBLiveVar *PredLVBB = BBLiveVarInfo[*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 {
|
2003-10-20 20:38:17 +00:00
|
|
|
std::cerr << " Defs: "; printSet(DefSet); std::cerr << "\n";
|
|
|
|
std::cerr << " In: "; printSet(InSet); std::cerr << "\n";
|
|
|
|
std::cerr << " Out: "; printSet(OutSet); std::cerr << "\n";
|
2001-07-24 17:14:13 +00:00
|
|
|
}
|
|
|
|
|
2002-02-05 00:34:50 +00:00
|
|
|
void BBLiveVar::printInOutSets() const {
|
2003-10-20 20:38:17 +00:00
|
|
|
std::cerr << " In: "; printSet(InSet); std::cerr << "\n";
|
|
|
|
std::cerr << " Out: "; printSet(OutSet); std::cerr << "\n";
|
2001-07-24 17:14:13 +00:00
|
|
|
}
|
2001-08-20 21:12:49 +00:00
|
|
|
|
2003-11-11 22:41:34 +00:00
|
|
|
} // End llvm namespace
|