// This pass performs global value numbering to eliminate fully redundant
// instructions. It also performs simple dead load elimination.
//
-// Note that this pass does the value numbering itself, it does not use the
+// Note that this pass does the value numbering itself; it does not use the
// ValueNumbering analysis passes.
//
//===----------------------------------------------------------------------===//
#include "llvm/Constants.h"
#include "llvm/DerivedTypes.h"
#include "llvm/Function.h"
-#include "llvm/Instructions.h"
+#include "llvm/IntrinsicInst.h"
#include "llvm/Value.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include <cstdio>
using namespace llvm;
-STATISTIC(NumGVNInstr, "Number of instructions deleted");
-STATISTIC(NumGVNLoad, "Number of loads deleted");
-STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
+STATISTIC(NumGVNInstr, "Number of instructions deleted");
+STATISTIC(NumGVNLoad, "Number of loads deleted");
+STATISTIC(NumGVNPRE, "Number of instructions PRE'd");
STATISTIC(NumGVNBlocks, "Number of blocks merged");
-STATISTIC(NumPRELoad, "Number of loads PRE'd");
+STATISTIC(NumPRELoad, "Number of loads PRE'd");
static cl::opt<bool> EnablePRE("enable-pre",
cl::init(true), cl::Hidden);
-cl::opt<bool> EnableLoadPRE("enable-load-pre"/*, cl::init(true)*/);
+cl::opt<bool> EnableLoadPRE("enable-load-pre", cl::init(true));
//===----------------------------------------------------------------------===//
// ValueTable Class
}
bool operator!=(const Expression &other) const {
- if (opcode != other.opcode)
- return true;
- else if (opcode == EMPTY || opcode == TOMBSTONE)
- return false;
- else if (type != other.type)
- return true;
- else if (function != other.function)
- return true;
- else if (firstVN != other.firstVN)
- return true;
- else if (secondVN != other.secondVN)
- return true;
- else if (thirdVN != other.thirdVN)
- return true;
- else {
- if (varargs.size() != other.varargs.size())
- return true;
-
- for (size_t i = 0; i < varargs.size(); ++i)
- if (varargs[i] != other.varargs[i])
- return true;
-
- return false;
- }
+ return !(*this == other);
}
};
}
//===----------------------------------------------------------------------===//
-// GVN Pass
+// GVN Pass
//===----------------------------------------------------------------------===//
namespace {
Value* v = GetValueForBlock(LI->getParent(), LI, BlockReplValues, true);
LI->replaceAllUsesWith(v);
- if (!isa<GlobalValue>(v))
+ if (isa<PHINode>(v))
v->takeName(LI);
if (isa<PointerType>(v->getType()))
MD->invalidateCachedPointerInfo(v);
LI->getAlignment(),
UnavailablePred->getTerminator());
+ SmallPtrSet<Instruction*, 4> &p = phiMap[LI->getPointerOperand()];
+ for (SmallPtrSet<Instruction*, 4>::iterator I = p.begin(), E = p.end();
+ I != E; ++I)
+ ValuesPerBlock.push_back(std::make_pair((*I)->getParent(), *I));
+
DenseMap<BasicBlock*, Value*> BlockReplValues;
BlockReplValues.insert(ValuesPerBlock.begin(), ValuesPerBlock.end());
BlockReplValues[UnavailablePred] = NewLoad;
// Perform PHI construction.
Value* v = GetValueForBlock(LI->getParent(), LI, BlockReplValues, true);
LI->replaceAllUsesWith(v);
- if (!isa<GlobalValue>(v))
+ if (isa<PHINode>(v))
v->takeName(LI);
if (isa<PointerType>(v->getType()))
MD->invalidateCachedPointerInfo(v);
MemDepResult dep = MD->getDependency(L);
// If the value isn't available, don't do anything!
- if (dep.isClobber())
+ if (dep.isClobber()) {
+ DEBUG(
+ // fast print dep, using operator<< on instruction would be too slow
+ DOUT << "GVN: load ";
+ WriteAsOperand(*DOUT.stream(), L);
+ Instruction *I = dep.getInst();
+ DOUT << " is clobbered by " << *I;
+ );
return false;
+ }
// If it is defined in another block, try harder.
if (dep.isNonLocal())
DenseMap<BasicBlock*, ValueNumberScope*>::iterator LA =
localAvail.find(Current);
if (LA == localAvail.end()) return 0;
- DenseMap<unsigned, Value*>::iterator V = LA->second->table.find(valno);
+ DenseMap<uint32_t, Value*>::iterator V = LA->second->table.find(valno);
if (V != LA->second->table.end()) {
// Found an instance, record it.
uint32_t nextNum = VN.getNextUnusedValueNumber();
unsigned num = VN.lookup_or_add(I);
+ if (BranchInst* BI = dyn_cast<BranchInst>(I)) {
+ localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
+
+ if (!BI->isConditional() || isa<Constant>(BI->getCondition()))
+ return false;
+
+ Value* branchCond = BI->getCondition();
+ uint32_t condVN = VN.lookup_or_add(branchCond);
+
+ BasicBlock* trueSucc = BI->getSuccessor(0);
+ BasicBlock* falseSucc = BI->getSuccessor(1);
+
+ if (trueSucc->getSinglePredecessor())
+ localAvail[trueSucc]->table[condVN] = ConstantInt::getTrue();
+ if (falseSucc->getSinglePredecessor())
+ localAvail[falseSucc]->table[condVN] = ConstantInt::getFalse();
+
+ return false;
+
// Allocations are always uniquely numbered, so we can save time and memory
- // by fast failing them.
- if (isa<AllocationInst>(I) || isa<TerminatorInst>(I)) {
+ // by fast failing them.
+ } else if (isa<AllocationInst>(I) || isa<TerminatorInst>(I)) {
localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
return false;
}
p->replaceAllUsesWith(constVal);
if (isa<PointerType>(constVal->getType()))
MD->invalidateCachedPointerInfo(constVal);
+ VN.erase(p);
+
toErase.push_back(p);
} else {
localAvail[I->getParent()]->table.insert(std::make_pair(num, I));
return false;
}
-// GVN::runOnFunction - This is the main transformation entry point for a
-// function.
-//
+/// runOnFunction - This is the main transformation entry point for a function.
bool GVN::runOnFunction(Function& F) {
MD = &getAnalysis<MemoryDependenceAnalysis>();
DT = &getAnalysis<DominatorTree>();
bool GVN::processBlock(BasicBlock* BB) {
- DomTreeNode* DTN = DT->getNode(BB);
// FIXME: Kill off toErase by doing erasing eagerly in a helper function (and
// incrementing BI before processing an instruction).
SmallVector<Instruction*, 8> toErase;
bool changed_function = false;
- if (DTN->getIDom())
- localAvail[BB] =
- new ValueNumberScope(localAvail[DTN->getIDom()->getBlock()]);
- else
- localAvail[BB] = new ValueNumberScope(0);
-
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
BI != BE;) {
changed_function |= processInstruction(BI, toErase);
for (BasicBlock::iterator BI = CurrentBlock->begin(),
BE = CurrentBlock->end(); BI != BE; ) {
Instruction *CurInst = BI++;
-
+
if (isa<AllocationInst>(CurInst) || isa<TerminatorInst>(CurInst) ||
- isa<PHINode>(CurInst) || CurInst->mayReadFromMemory() ||
- CurInst->mayWriteToMemory())
+ isa<PHINode>(CurInst) || (CurInst->getType() == Type::VoidTy) ||
+ CurInst->mayReadFromMemory() || CurInst->mayHaveSideEffects() ||
+ isa<DbgInfoIntrinsic>(CurInst))
continue;
-
+
uint32_t valno = VN.lookup(CurInst);
// Look for the predecessors for PRE opportunities. We're
// are not value numbered precisely.
if (!success) {
delete PREInstr;
+ DEBUG(verifyRemoved(PREInstr));
continue;
}
return Changed || toSplit.size();
}
-// iterateOnFunction - Executes one iteration of GVN
+/// iterateOnFunction - Executes one iteration of GVN
bool GVN::iterateOnFunction(Function &F) {
cleanupGlobalSets();
+ for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()),
+ DE = df_end(DT->getRootNode()); DI != DE; ++DI) {
+ if (DI->getIDom())
+ localAvail[DI->getBlock()] =
+ new ValueNumberScope(localAvail[DI->getIDom()->getBlock()]);
+ else
+ localAvail[DI->getBlock()] = new ValueNumberScope(0);
+ }
+
// Top-down walk of the dominator tree
bool changed = false;
#if 0
/// verifyRemoved - Verify that the specified instruction does not occur in our
/// internal data structures.
-void GVN::verifyRemoved(const Instruction *I) const {
- VN.verifyRemoved(I);
+void GVN::verifyRemoved(const Instruction *Inst) const {
+ VN.verifyRemoved(Inst);
+
+ // Walk through the PHI map to make sure the instruction isn't hiding in there
+ // somewhere.
+ for (PhiMapType::iterator
+ I = phiMap.begin(), E = phiMap.end(); I != E; ++I) {
+ assert(I->first != Inst && "Inst is still a key in PHI map!");
+
+ for (SmallPtrSet<Instruction*, 4>::iterator
+ II = I->second.begin(), IE = I->second.end(); II != IE; ++II) {
+ assert(*II != Inst && "Inst is still a value in PHI map!");
+ }
+ }
+
+ // Walk through the value number scope to make sure the instruction isn't
+ // ferreted away in it.
+ for (DenseMap<BasicBlock*, ValueNumberScope*>::iterator
+ I = localAvail.begin(), E = localAvail.end(); I != E; ++I) {
+ const ValueNumberScope *VNS = I->second;
+
+ while (VNS) {
+ for (DenseMap<uint32_t, Value*>::iterator
+ II = VNS->table.begin(), IE = VNS->table.end(); II != IE; ++II) {
+ assert(II->second != Inst && "Inst still in value numbering scope!");
+ }
+
+ VNS = VNS->parent;
+ }
+ }
}