SparsePropagation.cpp [plain text]
#define DEBUG_TYPE "sparseprop"
#include "llvm/Analysis/SparsePropagation.h"
#include "llvm/Constants.h"
#include "llvm/Function.h"
#include "llvm/Instructions.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/raw_ostream.h"
using namespace llvm;
AbstractLatticeFunction::~AbstractLatticeFunction() {}
void AbstractLatticeFunction::PrintValue(LatticeVal V, raw_ostream &OS) {
if (V == UndefVal)
OS << "undefined";
else if (V == OverdefinedVal)
OS << "overdefined";
else if (V == UntrackedVal)
OS << "untracked";
else
OS << "unknown lattice value";
}
SparseSolver::LatticeVal SparseSolver::getOrInitValueState(Value *V) {
DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(V);
if (I != ValueState.end()) return I->second;
LatticeVal LV;
if (LatticeFunc->IsUntrackedValue(V))
return LatticeFunc->getUntrackedVal();
else if (Constant *C = dyn_cast<Constant>(V))
LV = LatticeFunc->ComputeConstant(C);
else if (Argument *A = dyn_cast<Argument>(V))
LV = LatticeFunc->ComputeArgument(A);
else if (!isa<Instruction>(V))
LV = LatticeFunc->getOverdefinedVal();
else
LV = LatticeFunc->getUndefVal();
if (LV == LatticeFunc->getUntrackedVal())
return LV;
return ValueState[V] = LV;
}
void SparseSolver::UpdateState(Instruction &Inst, LatticeVal V) {
DenseMap<Value*, LatticeVal>::iterator I = ValueState.find(&Inst);
if (I != ValueState.end() && I->second == V)
return;
ValueState[&Inst] = V;
InstWorkList.push_back(&Inst);
}
void SparseSolver::MarkBlockExecutable(BasicBlock *BB) {
DEBUG(dbgs() << "Marking Block Executable: " << BB->getName() << "\n");
BBExecutable.insert(BB); BBWorkList.push_back(BB); }
void SparseSolver::markEdgeExecutable(BasicBlock *Source, BasicBlock *Dest) {
if (!KnownFeasibleEdges.insert(Edge(Source, Dest)).second)
return;
DEBUG(dbgs() << "Marking Edge Executable: " << Source->getName()
<< " -> " << Dest->getName() << "\n");
if (BBExecutable.count(Dest)) {
for (BasicBlock::iterator I = Dest->begin(); isa<PHINode>(I); ++I)
visitPHINode(*cast<PHINode>(I));
} else {
MarkBlockExecutable(Dest);
}
}
void SparseSolver::getFeasibleSuccessors(TerminatorInst &TI,
SmallVectorImpl<bool> &Succs,
bool AggressiveUndef) {
Succs.resize(TI.getNumSuccessors());
if (TI.getNumSuccessors() == 0) return;
if (BranchInst *BI = dyn_cast<BranchInst>(&TI)) {
if (BI->isUnconditional()) {
Succs[0] = true;
return;
}
LatticeVal BCValue;
if (AggressiveUndef)
BCValue = getOrInitValueState(BI->getCondition());
else
BCValue = getLatticeState(BI->getCondition());
if (BCValue == LatticeFunc->getOverdefinedVal() ||
BCValue == LatticeFunc->getUntrackedVal()) {
Succs[0] = Succs[1] = true;
return;
}
if (BCValue == LatticeFunc->getUndefVal())
return;
Constant *C = LatticeFunc->GetConstant(BCValue, BI->getCondition(), *this);
if (C == 0 || !isa<ConstantInt>(C)) {
Succs[0] = Succs[1] = true;
return;
}
Succs[C->isNullValue()] = true;
return;
}
if (isa<InvokeInst>(TI)) {
Succs[0] = Succs[1] = true;
return;
}
if (isa<IndirectBrInst>(TI)) {
Succs.assign(Succs.size(), true);
return;
}
SwitchInst &SI = cast<SwitchInst>(TI);
LatticeVal SCValue;
if (AggressiveUndef)
SCValue = getOrInitValueState(SI.getCondition());
else
SCValue = getLatticeState(SI.getCondition());
if (SCValue == LatticeFunc->getOverdefinedVal() ||
SCValue == LatticeFunc->getUntrackedVal()) {
Succs.assign(TI.getNumSuccessors(), true);
return;
}
if (SCValue == LatticeFunc->getUndefVal())
return;
Constant *C = LatticeFunc->GetConstant(SCValue, SI.getCondition(), *this);
if (C == 0 || !isa<ConstantInt>(C)) {
Succs.assign(TI.getNumSuccessors(), true);
return;
}
Succs[SI.findCaseValue(cast<ConstantInt>(C))] = true;
}
bool SparseSolver::isEdgeFeasible(BasicBlock *From, BasicBlock *To,
bool AggressiveUndef) {
SmallVector<bool, 16> SuccFeasible;
TerminatorInst *TI = From->getTerminator();
getFeasibleSuccessors(*TI, SuccFeasible, AggressiveUndef);
for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
if (TI->getSuccessor(i) == To && SuccFeasible[i])
return true;
return false;
}
void SparseSolver::visitTerminatorInst(TerminatorInst &TI) {
SmallVector<bool, 16> SuccFeasible;
getFeasibleSuccessors(TI, SuccFeasible, true);
BasicBlock *BB = TI.getParent();
for (unsigned i = 0, e = SuccFeasible.size(); i != e; ++i)
if (SuccFeasible[i])
markEdgeExecutable(BB, TI.getSuccessor(i));
}
void SparseSolver::visitPHINode(PHINode &PN) {
if (LatticeFunc->IsSpecialCasedPHI(&PN)) {
LatticeVal IV = LatticeFunc->ComputeInstructionState(PN, *this);
if (IV != LatticeFunc->getUntrackedVal())
UpdateState(PN, IV);
return;
}
LatticeVal PNIV = getOrInitValueState(&PN);
LatticeVal Overdefined = LatticeFunc->getOverdefinedVal();
if (PNIV == Overdefined || PNIV == LatticeFunc->getUntrackedVal())
return;
if (PN.getNumIncomingValues() > 64) {
UpdateState(PN, Overdefined);
return;
}
for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
if (!isEdgeFeasible(PN.getIncomingBlock(i), PN.getParent(), true))
continue;
LatticeVal OpVal = getOrInitValueState(PN.getIncomingValue(i));
if (OpVal != PNIV)
PNIV = LatticeFunc->MergeValues(PNIV, OpVal);
if (PNIV == Overdefined)
break; }
UpdateState(PN, PNIV);
}
void SparseSolver::visitInst(Instruction &I) {
if (PHINode *PN = dyn_cast<PHINode>(&I))
return visitPHINode(*PN);
LatticeVal IV = LatticeFunc->ComputeInstructionState(I, *this);
if (IV != LatticeFunc->getUntrackedVal())
UpdateState(I, IV);
if (TerminatorInst *TI = dyn_cast<TerminatorInst>(&I))
visitTerminatorInst(*TI);
}
void SparseSolver::Solve(Function &F) {
MarkBlockExecutable(&F.getEntryBlock());
while (!BBWorkList.empty() || !InstWorkList.empty()) {
while (!InstWorkList.empty()) {
Instruction *I = InstWorkList.back();
InstWorkList.pop_back();
DEBUG(dbgs() << "\nPopped off I-WL: " << *I << "\n");
for (Value::use_iterator UI = I->use_begin(), E = I->use_end();
UI != E; ++UI) {
Instruction *U = cast<Instruction>(*UI);
if (BBExecutable.count(U->getParent())) visitInst(*U);
}
}
while (!BBWorkList.empty()) {
BasicBlock *BB = BBWorkList.back();
BBWorkList.pop_back();
DEBUG(dbgs() << "\nPopped off BBWL: " << *BB);
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I)
visitInst(*I);
}
}
}
void SparseSolver::Print(Function &F, raw_ostream &OS) const {
OS << "\nFUNCTION: " << F.getNameStr() << "\n";
for (Function::iterator BB = F.begin(), E = F.end(); BB != E; ++BB) {
if (!BBExecutable.count(BB))
OS << "INFEASIBLE: ";
OS << "\t";
if (BB->hasName())
OS << BB->getNameStr() << ":\n";
else
OS << "; anon bb\n";
for (BasicBlock::iterator I = BB->begin(), E = BB->end(); I != E; ++I) {
LatticeFunc->PrintValue(getLatticeState(I), OS);
OS << *I << "\n";
}
OS << "\n";
}
}