Start using a set representation that remembers the set of value numbers represented

in the set.  For the moment, this results in a slight performance decrease, but
it lays the groundwork for future improvements.

llvm-svn: 38439
This commit is contained in:
Owen Anderson 2007-07-09 06:50:06 +00:00
parent 74119e0e7e
commit 1c83b5d999
1 changed files with 176 additions and 141 deletions

View File

@ -546,6 +546,52 @@ unsigned ValueTable::size() {
return nextValueNumber; return nextValueNumber;
} }
//===----------------------------------------------------------------------===//
// ValueTable Class
//===----------------------------------------------------------------------===//
class ValueNumberedSet {
private:
SmallPtrSet<Value*, 8> contents;
BitVector numbers;
public:
ValueNumberedSet() { numbers.resize(1); }
typedef SmallPtrSet<Value*, 8>::iterator iterator;
iterator begin() { return contents.begin(); }
iterator end() { return contents.end(); }
bool insert(Value* v) { return contents.insert(v); }
void insert(iterator I, iterator E) { contents.insert(I, E); }
void erase(Value* v) { contents.erase(v); }
size_t size() { return contents.size(); }
void set(unsigned i) {
if (i >= numbers.size())
numbers.resize(i+1);
numbers.set(i);
}
void reset(unsigned i) {
if (i < numbers.size())
numbers.reset(i);
}
bool test(unsigned i) {
if (i >= numbers.size())
return false;
return numbers.test(i);
}
void clear() {
contents.clear();
numbers.clear();
}
};
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// GVNPRE Pass // GVNPRE Pass
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
@ -562,9 +608,9 @@ namespace {
ValueTable VN; ValueTable VN;
std::vector<Instruction*> createdExpressions; std::vector<Instruction*> createdExpressions;
std::map<BasicBlock*, SmallPtrSet<Value*, 16> > availableOut; std::map<BasicBlock*, ValueNumberedSet> availableOut;
std::map<BasicBlock*, SmallPtrSet<Value*, 16> > anticipatedIn; std::map<BasicBlock*, ValueNumberedSet> anticipatedIn;
std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedPhis; std::map<BasicBlock*, ValueNumberedSet> generatedPhis;
// This transformation requires dominator postdominator info // This transformation requires dominator postdominator info
virtual void getAnalysisUsage(AnalysisUsage &AU) const { virtual void getAnalysisUsage(AnalysisUsage &AU) const {
@ -576,46 +622,43 @@ namespace {
// Helper fuctions // Helper fuctions
// FIXME: eliminate or document these better // FIXME: eliminate or document these better
void dump(const SmallPtrSet<Value*, 16>& s) const; void dump(ValueNumberedSet& s) const;
void clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet); void clean(ValueNumberedSet& set);
Value* find_leader(SmallPtrSet<Value*, 16>& vals, Value* find_leader(ValueNumberedSet& vals, uint32_t v);
uint32_t v);
Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ); Value* phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ);
void phi_translate_set(SmallPtrSet<Value*, 16>& anticIn, BasicBlock* pred, void phi_translate_set(ValueNumberedSet& anticIn, BasicBlock* pred,
BasicBlock* succ, SmallPtrSet<Value*, 16>& out); BasicBlock* succ, ValueNumberedSet& out);
void topo_sort(SmallPtrSet<Value*, 16>& set, void topo_sort(ValueNumberedSet& set,
std::vector<Value*>& vec); std::vector<Value*>& vec);
void cleanup(); void cleanup();
bool elimination(); bool elimination();
void val_insert(SmallPtrSet<Value*, 16>& s, Value* v); void val_insert(ValueNumberedSet& s, Value* v);
void val_replace(SmallPtrSet<Value*, 16>& s, Value* v); void val_replace(ValueNumberedSet& s, Value* v);
bool dependsOnInvoke(Value* V); bool dependsOnInvoke(Value* V);
void buildsets_availout(BasicBlock::iterator I, void buildsets_availout(BasicBlock::iterator I,
SmallPtrSet<Value*, 16>& currAvail, ValueNumberedSet& currAvail,
SmallPtrSet<Value*, 16>& currPhis, ValueNumberedSet& currPhis,
SmallPtrSet<Value*, 16>& currExps, ValueNumberedSet& currExps,
SmallPtrSet<Value*, 16>& currTemps, SmallPtrSet<Value*, 16>& currTemps);
BitVector& availNumbers,
BitVector& expNumbers);
bool buildsets_anticout(BasicBlock* BB, bool buildsets_anticout(BasicBlock* BB,
SmallPtrSet<Value*, 16>& anticOut, ValueNumberedSet& anticOut,
std::set<BasicBlock*>& visited); std::set<BasicBlock*>& visited);
unsigned buildsets_anticin(BasicBlock* BB, unsigned buildsets_anticin(BasicBlock* BB,
SmallPtrSet<Value*, 16>& anticOut, ValueNumberedSet& anticOut,
SmallPtrSet<Value*, 16>& currExps, ValueNumberedSet& currExps,
SmallPtrSet<Value*, 16>& currTemps, SmallPtrSet<Value*, 16>& currTemps,
std::set<BasicBlock*>& visited); std::set<BasicBlock*>& visited);
void buildsets(Function& F); void buildsets(Function& F);
void insertion_pre(Value* e, BasicBlock* BB, void insertion_pre(Value* e, BasicBlock* BB,
std::map<BasicBlock*, Value*>& avail, std::map<BasicBlock*, Value*>& avail,
std::map<BasicBlock*, SmallPtrSet<Value*, 16> >& new_set); std::map<BasicBlock*,ValueNumberedSet>& new_set);
unsigned insertion_mergepoint(std::vector<Value*>& workList, unsigned insertion_mergepoint(std::vector<Value*>& workList,
df_iterator<DomTreeNode*>& D, df_iterator<DomTreeNode*>& D,
std::map<BasicBlock*, SmallPtrSet<Value*, 16> >& new_set); std::map<BasicBlock*, ValueNumberedSet>& new_set);
bool insertion(Function& F); bool insertion(Function& F);
}; };
@ -638,8 +681,11 @@ STATISTIC(NumEliminated, "Number of redundant instructions eliminated");
/// find_leader - Given a set and a value number, return the first /// find_leader - Given a set and a value number, return the first
/// element of the set with that value number, or 0 if no such element /// element of the set with that value number, or 0 if no such element
/// is present /// is present
Value* GVNPRE::find_leader(SmallPtrSet<Value*, 16>& vals, uint32_t v) { Value* GVNPRE::find_leader(ValueNumberedSet& vals, uint32_t v) {
for (SmallPtrSet<Value*, 16>::iterator I = vals.begin(), E = vals.end(); if (!vals.test(v))
return 0;
for (ValueNumberedSet::iterator I = vals.begin(), E = vals.end();
I != E; ++I) I != E; ++I)
if (v == VN.lookup(*I)) if (v == VN.lookup(*I))
return *I; return *I;
@ -649,16 +695,15 @@ Value* GVNPRE::find_leader(SmallPtrSet<Value*, 16>& vals, uint32_t v) {
/// val_insert - Insert a value into a set only if there is not a value /// val_insert - Insert a value into a set only if there is not a value
/// with the same value number already in the set /// with the same value number already in the set
void GVNPRE::val_insert(SmallPtrSet<Value*, 16>& s, Value* v) { void GVNPRE::val_insert(ValueNumberedSet& s, Value* v) {
uint32_t num = VN.lookup(v); uint32_t num = VN.lookup(v);
Value* leader = find_leader(s, num); if (!s.test(num))
if (leader == 0)
s.insert(v); s.insert(v);
} }
/// val_replace - Insert a value into a set, replacing any values already in /// val_replace - Insert a value into a set, replacing any values already in
/// the set that have the same value number /// the set that have the same value number
void GVNPRE::val_replace(SmallPtrSet<Value*, 16>& s, Value* v) { void GVNPRE::val_replace(ValueNumberedSet& s, Value* v) {
uint32_t num = VN.lookup(v); uint32_t num = VN.lookup(v);
Value* leader = find_leader(s, num); Value* leader = find_leader(s, num);
while (leader != 0) { while (leader != 0) {
@ -867,14 +912,16 @@ Value* GVNPRE::phi_translate(Value* V, BasicBlock* pred, BasicBlock* succ) {
} }
/// phi_translate_set - Perform phi translation on every element of a set /// phi_translate_set - Perform phi translation on every element of a set
void GVNPRE::phi_translate_set(SmallPtrSet<Value*, 16>& anticIn, void GVNPRE::phi_translate_set(ValueNumberedSet& anticIn,
BasicBlock* pred, BasicBlock* succ, BasicBlock* pred, BasicBlock* succ,
SmallPtrSet<Value*, 16>& out) { ValueNumberedSet& out) {
for (SmallPtrSet<Value*, 16>::iterator I = anticIn.begin(), for (ValueNumberedSet::iterator I = anticIn.begin(),
E = anticIn.end(); I != E; ++I) { E = anticIn.end(); I != E; ++I) {
Value* V = phi_translate(*I, pred, succ); Value* V = phi_translate(*I, pred, succ);
if (V != 0) if (V != 0 && !out.test(VN.lookup_or_add(V))) {
out.insert(V); out.insert(V);
out.set(VN.lookup(V));
}
} }
} }
@ -895,7 +942,7 @@ bool GVNPRE::dependsOnInvoke(Value* V) {
/// clean - Remove all non-opaque values from the set whose operands are not /// clean - Remove all non-opaque values from the set whose operands are not
/// themselves in the set, as well as all values that depend on invokes (see /// themselves in the set, as well as all values that depend on invokes (see
/// above) /// above)
void GVNPRE::clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet) { void GVNPRE::clean(ValueNumberedSet& set) {
std::vector<Value*> worklist; std::vector<Value*> worklist;
worklist.reserve(set.size()); worklist.reserve(set.size());
topo_sort(set, worklist); topo_sort(set, worklist);
@ -906,13 +953,13 @@ void GVNPRE::clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet) {
// Handle unary ops // Handle unary ops
if (CastInst* U = dyn_cast<CastInst>(v)) { if (CastInst* U = dyn_cast<CastInst>(v)) {
bool lhsValid = !isa<Instruction>(U->getOperand(0)); bool lhsValid = !isa<Instruction>(U->getOperand(0));
lhsValid |= presentInSet.test(VN.lookup(U->getOperand(0))); lhsValid |= set.test(VN.lookup(U->getOperand(0)));
if (lhsValid) if (lhsValid)
lhsValid = !dependsOnInvoke(U->getOperand(0)); lhsValid = !dependsOnInvoke(U->getOperand(0));
if (!lhsValid) { if (!lhsValid) {
set.erase(U); set.erase(U);
presentInSet.flip(VN.lookup(U)); set.reset(VN.lookup(U));
} }
// Handle binary ops // Handle binary ops
@ -921,18 +968,18 @@ void GVNPRE::clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet) {
User* U = cast<User>(v); User* U = cast<User>(v);
bool lhsValid = !isa<Instruction>(U->getOperand(0)); bool lhsValid = !isa<Instruction>(U->getOperand(0));
lhsValid |= presentInSet.test(VN.lookup(U->getOperand(0))); lhsValid |= set.test(VN.lookup(U->getOperand(0)));
if (lhsValid) if (lhsValid)
lhsValid = !dependsOnInvoke(U->getOperand(0)); lhsValid = !dependsOnInvoke(U->getOperand(0));
bool rhsValid = !isa<Instruction>(U->getOperand(1)); bool rhsValid = !isa<Instruction>(U->getOperand(1));
rhsValid |= presentInSet.test(VN.lookup(U->getOperand(1))); rhsValid |= set.test(VN.lookup(U->getOperand(1)));
if (rhsValid) if (rhsValid)
rhsValid = !dependsOnInvoke(U->getOperand(1)); rhsValid = !dependsOnInvoke(U->getOperand(1));
if (!lhsValid || !rhsValid) { if (!lhsValid || !rhsValid) {
set.erase(U); set.erase(U);
presentInSet.flip(VN.lookup(U)); set.reset(VN.lookup(U));
} }
// Handle ternary ops // Handle ternary ops
@ -941,29 +988,29 @@ void GVNPRE::clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet) {
User* U = cast<User>(v); User* U = cast<User>(v);
bool lhsValid = !isa<Instruction>(U->getOperand(0)); bool lhsValid = !isa<Instruction>(U->getOperand(0));
lhsValid |= presentInSet.test(VN.lookup(U->getOperand(0))); lhsValid |= set.test(VN.lookup(U->getOperand(0)));
if (lhsValid) if (lhsValid)
lhsValid = !dependsOnInvoke(U->getOperand(0)); lhsValid = !dependsOnInvoke(U->getOperand(0));
bool rhsValid = !isa<Instruction>(U->getOperand(1)); bool rhsValid = !isa<Instruction>(U->getOperand(1));
rhsValid |= presentInSet.test(VN.lookup(U->getOperand(1))); rhsValid |= set.test(VN.lookup(U->getOperand(1)));
if (rhsValid) if (rhsValid)
rhsValid = !dependsOnInvoke(U->getOperand(1)); rhsValid = !dependsOnInvoke(U->getOperand(1));
bool thirdValid = !isa<Instruction>(U->getOperand(2)); bool thirdValid = !isa<Instruction>(U->getOperand(2));
thirdValid |= presentInSet.test(VN.lookup(U->getOperand(2))); thirdValid |= set.test(VN.lookup(U->getOperand(2)));
if (thirdValid) if (thirdValid)
thirdValid = !dependsOnInvoke(U->getOperand(2)); thirdValid = !dependsOnInvoke(U->getOperand(2));
if (!lhsValid || !rhsValid || !thirdValid) { if (!lhsValid || !rhsValid || !thirdValid) {
set.erase(U); set.erase(U);
presentInSet.flip(VN.lookup(U)); set.reset(VN.lookup(U));
} }
// Handle varargs ops // Handle varargs ops
} else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(v)) { } else if (GetElementPtrInst* U = dyn_cast<GetElementPtrInst>(v)) {
bool ptrValid = !isa<Instruction>(U->getPointerOperand()); bool ptrValid = !isa<Instruction>(U->getPointerOperand());
ptrValid |= presentInSet.test(VN.lookup(U->getPointerOperand())); ptrValid |= set.test(VN.lookup(U->getPointerOperand()));
if (ptrValid) if (ptrValid)
ptrValid = !dependsOnInvoke(U->getPointerOperand()); ptrValid = !dependsOnInvoke(U->getPointerOperand());
@ -971,13 +1018,13 @@ void GVNPRE::clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet) {
for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end(); for (GetElementPtrInst::op_iterator I = U->idx_begin(), E = U->idx_end();
I != E; ++I) I != E; ++I)
if (varValid) { if (varValid) {
varValid &= !isa<Instruction>(*I) || presentInSet.test(VN.lookup(*I)); varValid &= !isa<Instruction>(*I) || set.test(VN.lookup(*I));
varValid &= !dependsOnInvoke(*I); varValid &= !dependsOnInvoke(*I);
} }
if (!ptrValid || !varValid) { if (!ptrValid || !varValid) {
set.erase(U); set.erase(U);
presentInSet.flip(VN.lookup(U)); set.reset(VN.lookup(U));
} }
} }
} }
@ -985,10 +1032,10 @@ void GVNPRE::clean(SmallPtrSet<Value*, 16>& set, BitVector& presentInSet) {
/// topo_sort - Given a set of values, sort them by topological /// topo_sort - Given a set of values, sort them by topological
/// order into the provided vector. /// order into the provided vector.
void GVNPRE::topo_sort(SmallPtrSet<Value*, 16>& set, std::vector<Value*>& vec) { void GVNPRE::topo_sort(ValueNumberedSet& set, std::vector<Value*>& vec) {
SmallPtrSet<Value*, 16> visited; SmallPtrSet<Value*, 16> visited;
std::vector<Value*> stack; std::vector<Value*> stack;
for (SmallPtrSet<Value*, 16>::iterator I = set.begin(), E = set.end(); for (ValueNumberedSet::iterator I = set.begin(), E = set.end();
I != E; ++I) { I != E; ++I) {
if (visited.count(*I) == 0) if (visited.count(*I) == 0)
stack.push_back(*I); stack.push_back(*I);
@ -1089,9 +1136,9 @@ void GVNPRE::topo_sort(SmallPtrSet<Value*, 16>& set, std::vector<Value*>& vec) {
} }
/// dump - Dump a set of values to standard error /// dump - Dump a set of values to standard error
void GVNPRE::dump(const SmallPtrSet<Value*, 16>& s) const { void GVNPRE::dump(ValueNumberedSet& s) const {
DOUT << "{ "; DOUT << "{ ";
for (SmallPtrSet<Value*, 16>::iterator I = s.begin(), E = s.end(); for (ValueNumberedSet::iterator I = s.begin(), E = s.end();
I != E; ++I) { I != E; ++I) {
DOUT << "" << VN.lookup(*I) << ": "; DOUT << "" << VN.lookup(*I) << ": ";
DEBUG((*I)->dump()); DEBUG((*I)->dump());
@ -1168,37 +1215,32 @@ void GVNPRE::cleanup() {
/// buildsets_availout - When calculating availability, handle an instruction /// buildsets_availout - When calculating availability, handle an instruction
/// by inserting it into the appropriate sets /// by inserting it into the appropriate sets
void GVNPRE::buildsets_availout(BasicBlock::iterator I, void GVNPRE::buildsets_availout(BasicBlock::iterator I,
SmallPtrSet<Value*, 16>& currAvail, ValueNumberedSet& currAvail,
SmallPtrSet<Value*, 16>& currPhis, ValueNumberedSet& currPhis,
SmallPtrSet<Value*, 16>& currExps, ValueNumberedSet& currExps,
SmallPtrSet<Value*, 16>& currTemps, SmallPtrSet<Value*, 16>& currTemps) {
BitVector& availNumbers,
BitVector& expNumbers) {
// Handle PHI nodes // Handle PHI nodes
if (PHINode* p = dyn_cast<PHINode>(I)) { if (PHINode* p = dyn_cast<PHINode>(I)) {
VN.lookup_or_add(p); unsigned num = VN.lookup_or_add(p);
expNumbers.resize(VN.size());
availNumbers.resize(VN.size());
currPhis.insert(p); currPhis.insert(p);
currPhis.set(num);
// Handle unary ops // Handle unary ops
} else if (CastInst* U = dyn_cast<CastInst>(I)) { } else if (CastInst* U = dyn_cast<CastInst>(I)) {
Value* leftValue = U->getOperand(0); Value* leftValue = U->getOperand(0);
unsigned num = VN.lookup_or_add(U); unsigned num = VN.lookup_or_add(U);
expNumbers.resize(VN.size());
availNumbers.resize(VN.size());
if (isa<Instruction>(leftValue)) if (isa<Instruction>(leftValue))
if (!expNumbers.test(VN.lookup(leftValue))) { if (!currExps.test(VN.lookup(leftValue))) {
currExps.insert(leftValue); currExps.insert(leftValue);
expNumbers.set(VN.lookup(leftValue)); currExps.set(VN.lookup(leftValue));
} }
if (!expNumbers.test(VN.lookup(U))) { if (!currExps.test(num)) {
currExps.insert(U); currExps.insert(U);
expNumbers.set(num); currExps.set(num);
} }
// Handle binary ops // Handle binary ops
@ -1209,24 +1251,22 @@ void GVNPRE::buildsets_availout(BasicBlock::iterator I,
Value* rightValue = U->getOperand(1); Value* rightValue = U->getOperand(1);
unsigned num = VN.lookup_or_add(U); unsigned num = VN.lookup_or_add(U);
expNumbers.resize(VN.size());
availNumbers.resize(VN.size());
if (isa<Instruction>(leftValue)) if (isa<Instruction>(leftValue))
if (!expNumbers.test(VN.lookup(leftValue))) { if (!currExps.test(VN.lookup(leftValue))) {
currExps.insert(leftValue); currExps.insert(leftValue);
expNumbers.set(VN.lookup(leftValue)); currExps.set(VN.lookup(leftValue));
} }
if (isa<Instruction>(rightValue)) if (isa<Instruction>(rightValue))
if (!expNumbers.test(VN.lookup(rightValue))) { if (!currExps.test(VN.lookup(rightValue))) {
currExps.insert(rightValue); currExps.insert(rightValue);
expNumbers.set(VN.lookup(rightValue)); currExps.set(VN.lookup(rightValue));
} }
if (!expNumbers.test(VN.lookup(U))) { if (!currExps.test(num)) {
currExps.insert(U); currExps.insert(U);
expNumbers.set(num); currExps.set(num);
} }
// Handle ternary ops // Handle ternary ops
@ -1240,28 +1280,26 @@ void GVNPRE::buildsets_availout(BasicBlock::iterator I,
VN.lookup_or_add(U); VN.lookup_or_add(U);
unsigned num = VN.lookup_or_add(U); unsigned num = VN.lookup_or_add(U);
expNumbers.resize(VN.size());
availNumbers.resize(VN.size());
if (isa<Instruction>(leftValue)) if (isa<Instruction>(leftValue))
if (!expNumbers.test(VN.lookup(leftValue))) { if (!currExps.test(VN.lookup(leftValue))) {
currExps.insert(leftValue); currExps.insert(leftValue);
expNumbers.set(VN.lookup(leftValue)); currExps.set(VN.lookup(leftValue));
} }
if (isa<Instruction>(rightValue)) if (isa<Instruction>(rightValue))
if (!expNumbers.test(VN.lookup(rightValue))) { if (!currExps.test(VN.lookup(rightValue))) {
currExps.insert(rightValue); currExps.insert(rightValue);
expNumbers.set(VN.lookup(rightValue)); currExps.set(VN.lookup(rightValue));
} }
if (isa<Instruction>(thirdValue)) if (isa<Instruction>(thirdValue))
if (!expNumbers.test(VN.lookup(thirdValue))) { if (!currExps.test(VN.lookup(thirdValue))) {
currExps.insert(thirdValue); currExps.insert(thirdValue);
expNumbers.set(VN.lookup(thirdValue)); currExps.set(VN.lookup(thirdValue));
} }
if (!expNumbers.test(VN.lookup(U))) { if (!currExps.test(num)) {
currExps.insert(U); currExps.insert(U);
expNumbers.set(num); currExps.set(num);
} }
// Handle vararg ops // Handle vararg ops
@ -1271,47 +1309,43 @@ void GVNPRE::buildsets_availout(BasicBlock::iterator I,
VN.lookup_or_add(U); VN.lookup_or_add(U);
unsigned num = VN.lookup_or_add(U); unsigned num = VN.lookup_or_add(U);
expNumbers.resize(VN.size());
availNumbers.resize(VN.size());
if (isa<Instruction>(ptrValue)) if (isa<Instruction>(ptrValue))
if (!expNumbers.test(VN.lookup(ptrValue))) { if (!currExps.test(VN.lookup(ptrValue))) {
currExps.insert(ptrValue); currExps.insert(ptrValue);
expNumbers.set(VN.lookup(ptrValue)); currExps.set(VN.lookup(ptrValue));
} }
for (GetElementPtrInst::op_iterator OI = U->idx_begin(), OE = U->idx_end(); for (GetElementPtrInst::op_iterator OI = U->idx_begin(), OE = U->idx_end();
OI != OE; ++OI) OI != OE; ++OI)
if (isa<Instruction>(*OI) && !expNumbers.test(VN.lookup(*OI))) { if (isa<Instruction>(*OI) && !currExps.test(VN.lookup(*OI))) {
currExps.insert(*OI); currExps.insert(*OI);
expNumbers.set(VN.lookup(*OI)); currExps.set(VN.lookup(*OI));
} }
if (!expNumbers.test(VN.lookup(U))) { if (!currExps.test(VN.lookup(U))) {
currExps.insert(U); currExps.insert(U);
expNumbers.set(num); currExps.set(num);
} }
// Handle opaque ops // Handle opaque ops
} else if (!I->isTerminator()){ } else if (!I->isTerminator()){
VN.lookup_or_add(I); VN.lookup_or_add(I);
expNumbers.resize(VN.size());
availNumbers.resize(VN.size());
currTemps.insert(I); currTemps.insert(I);
} }
if (!I->isTerminator()) if (!I->isTerminator())
if (!availNumbers.test(VN.lookup(I))) { if (!currAvail.test(VN.lookup(I))) {
currAvail.insert(I); currAvail.insert(I);
availNumbers.set(VN.lookup(I)); currAvail.set(VN.lookup(I));
} }
} }
/// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT /// buildsets_anticout - When walking the postdom tree, calculate the ANTIC_OUT
/// set as a function of the ANTIC_IN set of the block's predecessors /// set as a function of the ANTIC_IN set of the block's predecessors
bool GVNPRE::buildsets_anticout(BasicBlock* BB, bool GVNPRE::buildsets_anticout(BasicBlock* BB,
SmallPtrSet<Value*, 16>& anticOut, ValueNumberedSet& anticOut,
std::set<BasicBlock*>& visited) { std::set<BasicBlock*>& visited) {
if (BB->getTerminator()->getNumSuccessors() == 1) { if (BB->getTerminator()->getNumSuccessors() == 1) {
if (BB->getTerminator()->getSuccessor(0) != BB && if (BB->getTerminator()->getSuccessor(0) != BB &&
@ -1325,22 +1359,28 @@ bool GVNPRE::buildsets_anticout(BasicBlock* BB,
} }
} else if (BB->getTerminator()->getNumSuccessors() > 1) { } else if (BB->getTerminator()->getNumSuccessors() > 1) {
BasicBlock* first = BB->getTerminator()->getSuccessor(0); BasicBlock* first = BB->getTerminator()->getSuccessor(0);
anticOut.insert(anticipatedIn[first].begin(), anticipatedIn[first].end()); for (ValueNumberedSet::iterator I = anticipatedIn[first].begin(),
E = anticipatedIn[first].end(); I != E; ++I) {
anticOut.insert(*I);
anticOut.set(VN.lookup(*I));
}
for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) { for (unsigned i = 1; i < BB->getTerminator()->getNumSuccessors(); ++i) {
BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i); BasicBlock* currSucc = BB->getTerminator()->getSuccessor(i);
SmallPtrSet<Value*, 16>& succAnticIn = anticipatedIn[currSucc]; ValueNumberedSet& succAnticIn = anticipatedIn[currSucc];
std::vector<Value*> temp; std::vector<Value*> temp;
for (SmallPtrSet<Value*, 16>::iterator I = anticOut.begin(), for (ValueNumberedSet::iterator I = anticOut.begin(),
E = anticOut.end(); I != E; ++I) E = anticOut.end(); I != E; ++I)
if (find_leader(succAnticIn, VN.lookup(*I)) == 0) if (!succAnticIn.test(VN.lookup(*I)))
temp.push_back(*I); temp.push_back(*I);
for (std::vector<Value*>::iterator I = temp.begin(), E = temp.end(); for (std::vector<Value*>::iterator I = temp.begin(), E = temp.end();
I != E; ++I) I != E; ++I) {
anticOut.erase(*I); anticOut.erase(*I);
anticOut.reset(VN.lookup(*I));
}
} }
} }
@ -1351,11 +1391,11 @@ bool GVNPRE::buildsets_anticout(BasicBlock* BB,
/// each block. ANTIC_IN is then a function of ANTIC_OUT and the GEN /// each block. ANTIC_IN is then a function of ANTIC_OUT and the GEN
/// sets populated in buildsets_availout /// sets populated in buildsets_availout
unsigned GVNPRE::buildsets_anticin(BasicBlock* BB, unsigned GVNPRE::buildsets_anticin(BasicBlock* BB,
SmallPtrSet<Value*, 16>& anticOut, ValueNumberedSet& anticOut,
SmallPtrSet<Value*, 16>& currExps, ValueNumberedSet& currExps,
SmallPtrSet<Value*, 16>& currTemps, SmallPtrSet<Value*, 16>& currTemps,
std::set<BasicBlock*>& visited) { std::set<BasicBlock*>& visited) {
SmallPtrSet<Value*, 16>& anticIn = anticipatedIn[BB]; ValueNumberedSet& anticIn = anticipatedIn[BB];
unsigned old = anticIn.size(); unsigned old = anticIn.size();
bool defer = buildsets_anticout(BB, anticOut, visited); bool defer = buildsets_anticout(BB, anticOut, visited);
@ -1364,32 +1404,26 @@ unsigned GVNPRE::buildsets_anticin(BasicBlock* BB,
anticIn.clear(); anticIn.clear();
BitVector numbers(VN.size()); for (ValueNumberedSet::iterator I = anticOut.begin(),
for (SmallPtrSet<Value*, 16>::iterator I = anticOut.begin(),
E = anticOut.end(); I != E; ++I) { E = anticOut.end(); I != E; ++I) {
unsigned num = VN.lookup_or_add(*I);
numbers.resize(VN.size());
if (isa<Instruction>(*I) && !numbers.test(num)) {
anticIn.insert(*I); anticIn.insert(*I);
numbers.set(num); anticIn.set(VN.lookup(*I));
} }
} for (ValueNumberedSet::iterator I = currExps.begin(),
for (SmallPtrSet<Value*, 16>::iterator I = currExps.begin(),
E = currExps.end(); I != E; ++I) { E = currExps.end(); I != E; ++I) {
if (!numbers.test(VN.lookup_or_add(*I))) { if (!anticIn.test(VN.lookup(*I))) {
anticIn.insert(*I); anticIn.insert(*I);
numbers.set(VN.lookup(*I)); anticIn.set(VN.lookup(*I));
} }
} }
for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(), for (SmallPtrSet<Value*, 16>::iterator I = currTemps.begin(),
E = currTemps.end(); I != E; ++I) { E = currTemps.end(); I != E; ++I) {
anticIn.erase(*I); anticIn.erase(*I);
numbers.flip(VN.lookup(*I)); anticIn.reset(VN.lookup(*I));
} }
clean(anticIn, numbers); clean(anticIn);
anticOut.clear(); anticOut.clear();
if (old != anticIn.size()) if (old != anticIn.size())
@ -1401,7 +1435,7 @@ unsigned GVNPRE::buildsets_anticin(BasicBlock* BB,
/// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT /// buildsets - Phase 1 of the main algorithm. Construct the AVAIL_OUT
/// and the ANTIC_IN sets. /// and the ANTIC_IN sets.
void GVNPRE::buildsets(Function& F) { void GVNPRE::buildsets(Function& F) {
std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedExpressions; std::map<BasicBlock*, ValueNumberedSet> generatedExpressions;
std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedTemporaries; std::map<BasicBlock*, SmallPtrSet<Value*, 16> > generatedTemporaries;
DominatorTree &DT = getAnalysis<DominatorTree>(); DominatorTree &DT = getAnalysis<DominatorTree>();
@ -1413,10 +1447,10 @@ void GVNPRE::buildsets(Function& F) {
E = df_end(DT.getRootNode()); DI != E; ++DI) { E = df_end(DT.getRootNode()); DI != E; ++DI) {
// Get the sets to update for this block // Get the sets to update for this block
SmallPtrSet<Value*, 16>& currExps = generatedExpressions[DI->getBlock()]; ValueNumberedSet& currExps = generatedExpressions[DI->getBlock()];
SmallPtrSet<Value*, 16>& currPhis = generatedPhis[DI->getBlock()]; ValueNumberedSet& currPhis = generatedPhis[DI->getBlock()];
SmallPtrSet<Value*, 16>& currTemps = generatedTemporaries[DI->getBlock()]; SmallPtrSet<Value*, 16>& currTemps = generatedTemporaries[DI->getBlock()];
SmallPtrSet<Value*, 16>& currAvail = availableOut[DI->getBlock()]; ValueNumberedSet& currAvail = availableOut[DI->getBlock()];
BasicBlock* BB = DI->getBlock(); BasicBlock* BB = DI->getBlock();
@ -1425,16 +1459,14 @@ void GVNPRE::buildsets(Function& F) {
currAvail.insert(availableOut[DI->getIDom()->getBlock()].begin(), currAvail.insert(availableOut[DI->getIDom()->getBlock()].begin(),
availableOut[DI->getIDom()->getBlock()].end()); availableOut[DI->getIDom()->getBlock()].end());
BitVector availNumbers(VN.size()); for (ValueNumberedSet::iterator I = currAvail.begin(),
for (SmallPtrSet<Value*, 16>::iterator I = currAvail.begin(),
E = currAvail.end(); I != E; ++I) E = currAvail.end(); I != E; ++I)
availNumbers.set(VN.lookup(*I)); currAvail.set(VN.lookup(*I));
BitVector expNumbers(VN.size());
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
BI != BE; ++BI) BI != BE; ++BI)
buildsets_availout(BI, currAvail, currPhis, currExps, buildsets_availout(BI, currAvail, currPhis, currExps,
currTemps, availNumbers, expNumbers); currTemps);
} }
@ -1450,7 +1482,7 @@ void GVNPRE::buildsets(Function& F) {
while (changed) { while (changed) {
changed = false; changed = false;
SmallPtrSet<Value*, 16> anticOut; ValueNumberedSet anticOut;
// Postorder walk of the CFG // Postorder walk of the CFG
for (po_iterator<BasicBlock*> BBI = po_begin(&F.getEntryBlock()), for (po_iterator<BasicBlock*> BBI = po_begin(&F.getEntryBlock()),
@ -1491,11 +1523,11 @@ void GVNPRE::buildsets(Function& F) {
/// the main block /// the main block
void GVNPRE::insertion_pre(Value* e, BasicBlock* BB, void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
std::map<BasicBlock*, Value*>& avail, std::map<BasicBlock*, Value*>& avail,
std::map<BasicBlock*, SmallPtrSet<Value*, 16> >& new_sets) { std::map<BasicBlock*, ValueNumberedSet>& new_sets) {
for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) { for (pred_iterator PI = pred_begin(BB), PE = pred_end(BB); PI != PE; ++PI) {
DOUT << "PRED: " << (*PI)->getName() << "\n"; DOUT << "PRED: " << (*PI)->getName() << "\n";
Value* e2 = avail[*PI]; Value* e2 = avail[*PI];
if (!find_leader(availableOut[*PI], VN.lookup(e2))) { if (!availableOut[*PI].test(VN.lookup(e2))) {
User* U = cast<User>(e2); User* U = cast<User>(e2);
Value* s1 = 0; Value* s1 = 0;
@ -1604,9 +1636,10 @@ void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
VN.add(newVal, VN.lookup(U)); VN.add(newVal, VN.lookup(U));
SmallPtrSet<Value*, 16>& predAvail = availableOut[*PI]; ValueNumberedSet& predAvail = availableOut[*PI];
val_replace(predAvail, newVal); val_replace(predAvail, newVal);
val_replace(new_sets[*PI], newVal); val_replace(new_sets[*PI], newVal);
predAvail.set(VN.lookup(newVal));
std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI); std::map<BasicBlock*, Value*>::iterator av = avail.find(*PI);
if (av != avail.end()) if (av != avail.end())
@ -1628,8 +1661,11 @@ void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
VN.add(p, VN.lookup(e)); VN.add(p, VN.lookup(e));
val_replace(availableOut[BB], p); val_replace(availableOut[BB], p);
availableOut[BB].set(VN.lookup(e));
generatedPhis[BB].insert(p); generatedPhis[BB].insert(p);
generatedPhis[BB].set(VN.lookup(e));
new_sets[BB].insert(p); new_sets[BB].insert(p);
new_sets[BB].set(VN.lookup(e));
++NumInsertedPhis; ++NumInsertedPhis;
} }
@ -1638,7 +1674,7 @@ void GVNPRE::insertion_pre(Value* e, BasicBlock* BB,
/// block for the possibility of a partial redundancy. If present, eliminate it /// block for the possibility of a partial redundancy. If present, eliminate it
unsigned GVNPRE::insertion_mergepoint(std::vector<Value*>& workList, unsigned GVNPRE::insertion_mergepoint(std::vector<Value*>& workList,
df_iterator<DomTreeNode*>& D, df_iterator<DomTreeNode*>& D,
std::map<BasicBlock*, SmallPtrSet<Value*, 16> >& new_sets) { std::map<BasicBlock*, ValueNumberedSet >& new_sets) {
bool changed_function = false; bool changed_function = false;
bool new_stuff = false; bool new_stuff = false;
@ -1650,8 +1686,7 @@ unsigned GVNPRE::insertion_mergepoint(std::vector<Value*>& workList,
isa<ExtractElementInst>(e) || isa<InsertElementInst>(e) || isa<ExtractElementInst>(e) || isa<InsertElementInst>(e) ||
isa<ShuffleVectorInst>(e) || isa<SelectInst>(e) || isa<CastInst>(e) || isa<ShuffleVectorInst>(e) || isa<SelectInst>(e) || isa<CastInst>(e) ||
isa<GetElementPtrInst>(e)) { isa<GetElementPtrInst>(e)) {
if (find_leader(availableOut[D->getIDom()->getBlock()], if (availableOut[D->getIDom()->getBlock()].test(VN.lookup(e)))
VN.lookup(e)) != 0)
continue; continue;
std::map<BasicBlock*, Value*> avail; std::map<BasicBlock*, Value*> avail;
@ -1685,7 +1720,7 @@ unsigned GVNPRE::insertion_mergepoint(std::vector<Value*>& workList,
} }
if (by_some && !all_same && if (by_some && !all_same &&
!find_leader(generatedPhis[BB], VN.lookup(e))) { !generatedPhis[BB].test(VN.lookup(e))) {
insertion_pre(e, BB, avail, new_sets); insertion_pre(e, BB, avail, new_sets);
changed_function = true; changed_function = true;
@ -1711,7 +1746,7 @@ bool GVNPRE::insertion(Function& F) {
DominatorTree &DT = getAnalysis<DominatorTree>(); DominatorTree &DT = getAnalysis<DominatorTree>();
std::map<BasicBlock*, SmallPtrSet<Value*, 16> > new_sets; std::map<BasicBlock*, ValueNumberedSet> new_sets;
bool new_stuff = true; bool new_stuff = true;
while (new_stuff) { while (new_stuff) {
new_stuff = false; new_stuff = false;
@ -1722,13 +1757,13 @@ bool GVNPRE::insertion(Function& F) {
if (BB == 0) if (BB == 0)
continue; continue;
SmallPtrSet<Value*, 16>& availOut = availableOut[BB]; ValueNumberedSet& availOut = availableOut[BB];
SmallPtrSet<Value*, 16>& anticIn = anticipatedIn[BB]; ValueNumberedSet& anticIn = anticipatedIn[BB];
// Replace leaders with leaders inherited from dominator // Replace leaders with leaders inherited from dominator
if (DI->getIDom() != 0) { if (DI->getIDom() != 0) {
SmallPtrSet<Value*, 16>& dom_set = new_sets[DI->getIDom()->getBlock()]; ValueNumberedSet& dom_set = new_sets[DI->getIDom()->getBlock()];
for (SmallPtrSet<Value*, 16>::iterator I = dom_set.begin(), for (ValueNumberedSet::iterator I = dom_set.begin(),
E = dom_set.end(); I != E; ++I) { E = dom_set.end(); I != E; ++I) {
val_replace(new_sets[BB], *I); val_replace(new_sets[BB], *I);
val_replace(availOut, *I); val_replace(availOut, *I);