#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);
}
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);
}
};
void setMemDep(MemoryDependenceAnalysis* M) { MD = M; }
void setDomTree(DominatorTree* D) { DT = D; }
uint32_t getNextUnusedValueNumber() { return nextValueNumber; }
+ void verifyRemoved(const Value *) const;
};
}
valueNumbering.erase(V);
}
+/// verifyRemoved - Verify that the value is removed from all internal data
+/// structures.
+void ValueTable::verifyRemoved(const Value *V) const {
+ for (DenseMap<Value*, uint32_t>::iterator
+ I = valueNumbering.begin(), E = valueNumbering.end(); I != E; ++I) {
+ assert(I->first != V && "Inst still occurs in value numbering map!");
+ }
+}
+
//===----------------------------------------------------------------------===//
-// GVN Pass
+// GVN Pass
//===----------------------------------------------------------------------===//
namespace {
bool mergeBlockIntoPredecessor(BasicBlock* BB);
Value* AttemptRedundancyElimination(Instruction* orig, unsigned valno);
void cleanupGlobalSets();
+ void verifyRemoved(const Instruction *I) const;
};
char GVN::ID = 0;
DEBUG(cerr << "GVN removed: " << *PN);
MD->removeInstruction(PN);
PN->eraseFromParent();
+ DEBUG(verifyRemoved(PN));
Phis[BB] = v;
return v;
// it will be too expensive.
if (Deps.size() > 100)
return false;
+
+ // If we had a phi translation failure, we'll have a single entry which is a
+ // clobber in the current block. Reject this early.
+ if (Deps.size() == 1 && Deps[0].second.isClobber())
+ return false;
// Filter out useless results (non-locals, etc). Keep track of the blocks
// where we have a value available in repl, also keep track of whether we see
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);
// 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);
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.
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>();
DEBUG(cerr << "GVN removed: " << **I);
MD->removeInstruction(*I);
(*I)->eraseFromParent();
+ DEBUG(verifyRemoved(*I));
}
toErase.clear();
// are not value numbered precisely.
if (!success) {
delete PREInstr;
+ DEBUG(verifyRemoved(PREInstr));
continue;
}
DEBUG(cerr << "GVN PRE removed: " << *CurInst);
MD->removeInstruction(CurInst);
CurInst->eraseFromParent();
+ DEBUG(verifyRemoved(CurInst));
Changed = true;
}
}
return Changed || toSplit.size();
}
-// iterateOnFunction - Executes one iteration of GVN
+/// iterateOnFunction - Executes one iteration of GVN
bool GVN::iterateOnFunction(Function &F) {
cleanupGlobalSets();
delete I->second;
localAvail.clear();
}
+
+/// verifyRemoved - Verify that the specified instruction does not occur in our
+/// internal data structures.
+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;
+ }
+ }
+}