formatting cleanups, no functionality change.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82426 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2009-09-21 02:42:51 +00:00
parent 22884ddaa6
commit b2412a8bec

View File

@ -83,7 +83,7 @@ namespace {
uint32_t secondVN; uint32_t secondVN;
uint32_t thirdVN; uint32_t thirdVN;
SmallVector<uint32_t, 4> varargs; SmallVector<uint32_t, 4> varargs;
Value* function; Value *function;
Expression() { } Expression() { }
Expression(ExpressionOpcode o) : opcode(o) { } Expression(ExpressionOpcode o) : opcode(o) { }
@ -145,11 +145,11 @@ namespace {
Expression create_expression(Constant* C); Expression create_expression(Constant* C);
public: public:
ValueTable() : nextValueNumber(1) { } ValueTable() : nextValueNumber(1) { }
uint32_t lookup_or_add(Value* V); uint32_t lookup_or_add(Value *V);
uint32_t lookup(Value* V) const; uint32_t lookup(Value *V) const;
void add(Value* V, uint32_t num); void add(Value *V, uint32_t num);
void clear(); void clear();
void erase(Value* v); void erase(Value *v);
unsigned size(); unsigned size();
void setAliasAnalysis(AliasAnalysis* A) { AA = A; } void setAliasAnalysis(AliasAnalysis* A) { AA = A; }
AliasAnalysis *getAliasAnalysis() const { return AA; } AliasAnalysis *getAliasAnalysis() const { return AA; }
@ -413,13 +413,13 @@ Expression ValueTable::create_expression(GetElementPtrInst* G) {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// add - Insert a value into the table with a specified value number. /// add - Insert a value into the table with a specified value number.
void ValueTable::add(Value* V, uint32_t num) { void ValueTable::add(Value *V, uint32_t num) {
valueNumbering.insert(std::make_pair(V, num)); valueNumbering.insert(std::make_pair(V, num));
} }
/// lookup_or_add - Returns the value number for the specified value, assigning /// lookup_or_add - Returns the value number for the specified value, assigning
/// it a new number if it did not have one before. /// it a new number if it did not have one before.
uint32_t ValueTable::lookup_or_add(Value* V) { uint32_t ValueTable::lookup_or_add(Value *V) {
DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V); DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
if (VI != valueNumbering.end()) if (VI != valueNumbering.end())
return VI->second; return VI->second;
@ -647,7 +647,7 @@ uint32_t ValueTable::lookup_or_add(Value* V) {
/// lookup - Returns the value number of the specified value. Fails if /// lookup - Returns the value number of the specified value. Fails if
/// the value has not yet been numbered. /// the value has not yet been numbered.
uint32_t ValueTable::lookup(Value* V) const { uint32_t ValueTable::lookup(Value *V) const {
DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V); DenseMap<Value*, uint32_t>::iterator VI = valueNumbering.find(V);
assert(VI != valueNumbering.end() && "Value not numbered?"); assert(VI != valueNumbering.end() && "Value not numbered?");
return VI->second; return VI->second;
@ -661,7 +661,7 @@ void ValueTable::clear() {
} }
/// erase - Remove a value from the value numbering /// erase - Remove a value from the value numbering
void ValueTable::erase(Value* V) { void ValueTable::erase(Value *V) {
valueNumbering.erase(V); valueNumbering.erase(V);
} }
@ -720,20 +720,20 @@ namespace {
// FIXME: eliminate or document these better // FIXME: eliminate or document these better
bool processLoad(LoadInst* L, bool processLoad(LoadInst* L,
SmallVectorImpl<Instruction*> &toErase); SmallVectorImpl<Instruction*> &toErase);
bool processInstruction(Instruction* I, bool processInstruction(Instruction *I,
SmallVectorImpl<Instruction*> &toErase); SmallVectorImpl<Instruction*> &toErase);
bool processNonLocalLoad(LoadInst* L, bool processNonLocalLoad(LoadInst* L,
SmallVectorImpl<Instruction*> &toErase); SmallVectorImpl<Instruction*> &toErase);
bool processBlock(BasicBlock* BB); bool processBlock(BasicBlock *BB);
Value *GetValueForBlock(BasicBlock *BB, Instruction* orig, Value *GetValueForBlock(BasicBlock *BB, Instruction *orig,
DenseMap<BasicBlock*, Value*> &Phis, DenseMap<BasicBlock*, Value*> &Phis,
bool top_level = false); bool top_level = false);
void dump(DenseMap<uint32_t, Value*>& d); void dump(DenseMap<uint32_t, Value*>& d);
bool iterateOnFunction(Function &F); bool iterateOnFunction(Function &F);
Value* CollapsePhi(PHINode* p); Value *CollapsePhi(PHINode* p);
bool performPRE(Function& F); bool performPRE(Function& F);
Value* lookupNumber(BasicBlock* BB, uint32_t num); Value *lookupNumber(BasicBlock *BB, uint32_t num);
Value* AttemptRedundancyElimination(Instruction* orig, unsigned valno); Value *AttemptRedundancyElimination(Instruction *orig, unsigned valno);
void cleanupGlobalSets(); void cleanupGlobalSets();
void verifyRemoved(const Instruction *I) const; void verifyRemoved(const Instruction *I) const;
}; };
@ -757,7 +757,7 @@ void GVN::dump(DenseMap<uint32_t, Value*>& d) {
printf("}\n"); printf("}\n");
} }
static bool isSafeReplacement(PHINode* p, Instruction* inst) { static bool isSafeReplacement(PHINode* p, Instruction *inst) {
if (!isa<PHINode>(inst)) if (!isa<PHINode>(inst))
return true; return true;
@ -770,37 +770,37 @@ static bool isSafeReplacement(PHINode* p, Instruction* inst) {
return true; return true;
} }
Value* GVN::CollapsePhi(PHINode* p) { Value *GVN::CollapsePhi(PHINode *PN) {
Value* constVal = p->hasConstantValue(DT); Value *ConstVal = PN->hasConstantValue(DT);
if (!constVal) return 0; if (!ConstVal) return 0;
Instruction* inst = dyn_cast<Instruction>(constVal); Instruction *Inst = dyn_cast<Instruction>(ConstVal);
if (!inst) if (!Inst)
return constVal; return ConstVal;
if (DT->dominates(inst, p)) if (DT->dominates(Inst, PN))
if (isSafeReplacement(p, inst)) if (isSafeReplacement(PN, Inst))
return inst; return Inst;
return 0; return 0;
} }
/// GetValueForBlock - Get the value to use within the specified basic block. /// GetValueForBlock - Get the value to use within the specified basic block.
/// available values are in Phis. /// available values are in Phis.
Value *GVN::GetValueForBlock(BasicBlock *BB, Instruction* orig, Value *GVN::GetValueForBlock(BasicBlock *BB, Instruction *Orig,
DenseMap<BasicBlock*, Value*> &Phis, DenseMap<BasicBlock*, Value*> &Phis,
bool top_level) { bool TopLevel) {
// If we have already computed this value, return the previously computed val. // If we have already computed this value, return the previously computed val.
DenseMap<BasicBlock*, Value*>::iterator V = Phis.find(BB); DenseMap<BasicBlock*, Value*>::iterator V = Phis.find(BB);
if (V != Phis.end() && !top_level) return V->second; if (V != Phis.end() && !TopLevel) return V->second;
// If the block is unreachable, just return undef, since this path // If the block is unreachable, just return undef, since this path
// can't actually occur at runtime. // can't actually occur at runtime.
if (!DT->isReachableFromEntry(BB)) if (!DT->isReachableFromEntry(BB))
return Phis[BB] = UndefValue::get(orig->getType()); return Phis[BB] = UndefValue::get(Orig->getType());
if (BasicBlock *Pred = BB->getSinglePredecessor()) { if (BasicBlock *Pred = BB->getSinglePredecessor()) {
Value *ret = GetValueForBlock(Pred, orig, Phis); Value *ret = GetValueForBlock(Pred, Orig, Phis);
Phis[BB] = ret; Phis[BB] = ret;
return ret; return ret;
} }
@ -816,7 +816,7 @@ Value *GVN::GetValueForBlock(BasicBlock *BB, Instruction* orig,
// Otherwise, the idom is the loop, so we need to insert a PHI node. Do so // Otherwise, the idom is the loop, so we need to insert a PHI node. Do so
// now, then get values to fill in the incoming values for the PHI. // now, then get values to fill in the incoming values for the PHI.
PHINode *PN = PHINode::Create(orig->getType(), orig->getName()+".rle", PHINode *PN = PHINode::Create(Orig->getType(), Orig->getName()+".rle",
BB->begin()); BB->begin());
PN->reserveOperandSpace(NumPreds); PN->reserveOperandSpace(NumPreds);
@ -824,20 +824,20 @@ Value *GVN::GetValueForBlock(BasicBlock *BB, Instruction* orig,
// Fill in the incoming values for the block. // Fill in the incoming values for the block.
for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) { for (pred_iterator PI = pred_begin(BB), E = pred_end(BB); PI != E; ++PI) {
Value* val = GetValueForBlock(*PI, orig, Phis); Value *val = GetValueForBlock(*PI, Orig, Phis);
PN->addIncoming(val, *PI); PN->addIncoming(val, *PI);
} }
VN.getAliasAnalysis()->copyValue(orig, PN); VN.getAliasAnalysis()->copyValue(Orig, PN);
// Attempt to collapse PHI nodes that are trivially redundant // Attempt to collapse PHI nodes that are trivially redundant
Value* v = CollapsePhi(PN); Value *v = CollapsePhi(PN);
if (!v) { if (!v) {
// Cache our phi construction results // Cache our phi construction results
if (LoadInst* L = dyn_cast<LoadInst>(orig)) if (LoadInst* L = dyn_cast<LoadInst>(Orig))
phiMap[L->getPointerOperand()].insert(PN); phiMap[L->getPointerOperand()].insert(PN);
else else
phiMap[orig].insert(PN); phiMap[Orig].insert(PN);
return PN; return PN;
} }
@ -1348,10 +1348,10 @@ bool GVN::processLoad(LoadInst *L, SmallVectorImpl<Instruction*> &toErase) {
return false; return false;
// ... to a pointer that has been loaded from before... // ... to a pointer that has been loaded from before...
MemDepResult dep = MD->getDependency(L); MemDepResult Dep = MD->getDependency(L);
// If the value isn't available, don't do anything! // If the value isn't available, don't do anything!
if (dep.isClobber()) { if (Dep.isClobber()) {
// FIXME: In the future, we should handle things like: // FIXME: In the future, we should handle things like:
// store i32 123, i32* %P // store i32 123, i32* %P
// %A = bitcast i32* %P to i8* // %A = bitcast i32* %P to i8*
@ -1366,17 +1366,17 @@ bool GVN::processLoad(LoadInst *L, SmallVectorImpl<Instruction*> &toErase) {
// fast print dep, using operator<< on instruction would be too slow // fast print dep, using operator<< on instruction would be too slow
errs() << "GVN: load "; errs() << "GVN: load ";
WriteAsOperand(errs(), L); WriteAsOperand(errs(), L);
Instruction *I = dep.getInst(); Instruction *I = Dep.getInst();
errs() << " is clobbered by " << *I << '\n'; errs() << " is clobbered by " << *I << '\n';
); );
return false; return false;
} }
// If it is defined in another block, try harder. // If it is defined in another block, try harder.
if (dep.isNonLocal()) if (Dep.isNonLocal())
return processNonLocalLoad(L, toErase); return processNonLocalLoad(L, toErase);
Instruction *DepInst = dep.getInst(); Instruction *DepInst = Dep.getInst();
if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) { if (StoreInst *DepSI = dyn_cast<StoreInst>(DepInst)) {
Value *StoredVal = DepSI->getOperand(0); Value *StoredVal = DepSI->getOperand(0);
@ -1448,19 +1448,17 @@ bool GVN::processLoad(LoadInst *L, SmallVectorImpl<Instruction*> &toErase) {
return false; return false;
} }
Value* GVN::lookupNumber(BasicBlock* BB, uint32_t num) { Value *GVN::lookupNumber(BasicBlock *BB, uint32_t num) {
DenseMap<BasicBlock*, ValueNumberScope*>::iterator I = localAvail.find(BB); DenseMap<BasicBlock*, ValueNumberScope*>::iterator I = localAvail.find(BB);
if (I == localAvail.end()) if (I == localAvail.end())
return 0; return 0;
ValueNumberScope* locals = I->second; ValueNumberScope *Locals = I->second;
while (Locals) {
while (locals) { DenseMap<uint32_t, Value*>::iterator I = Locals->table.find(num);
DenseMap<uint32_t, Value*>::iterator I = locals->table.find(num); if (I != Locals->table.end())
if (I != locals->table.end())
return I->second; return I->second;
else Locals = Locals->parent;
locals = locals->parent;
} }
return 0; return 0;
@ -1469,8 +1467,8 @@ Value* GVN::lookupNumber(BasicBlock* BB, uint32_t num) {
/// AttemptRedundancyElimination - If the "fast path" of redundancy elimination /// AttemptRedundancyElimination - If the "fast path" of redundancy elimination
/// by inheritance from the dominator fails, see if we can perform phi /// by inheritance from the dominator fails, see if we can perform phi
/// construction to eliminate the redundancy. /// construction to eliminate the redundancy.
Value* GVN::AttemptRedundancyElimination(Instruction* orig, unsigned valno) { Value *GVN::AttemptRedundancyElimination(Instruction *orig, unsigned valno) {
BasicBlock* BaseBlock = orig->getParent(); BasicBlock *BaseBlock = orig->getParent();
SmallPtrSet<BasicBlock*, 4> Visited; SmallPtrSet<BasicBlock*, 4> Visited;
SmallVector<BasicBlock*, 8> Stack; SmallVector<BasicBlock*, 8> Stack;
@ -1482,7 +1480,7 @@ Value* GVN::AttemptRedundancyElimination(Instruction* orig, unsigned valno) {
// value number we're looking for. Instances are recorded in the Results // value number we're looking for. Instances are recorded in the Results
// map, which is then used to perform phi construction. // map, which is then used to perform phi construction.
while (!Stack.empty()) { while (!Stack.empty()) {
BasicBlock* Current = Stack.back(); BasicBlock *Current = Stack.back();
Stack.pop_back(); Stack.pop_back();
// If we've walked all the way to a proper dominator, then give up. Cases // If we've walked all the way to a proper dominator, then give up. Cases
@ -1524,51 +1522,51 @@ Value* GVN::AttemptRedundancyElimination(Instruction* orig, unsigned valno) {
/// by inserting it into the appropriate sets /// by inserting it into the appropriate sets
bool GVN::processInstruction(Instruction *I, bool GVN::processInstruction(Instruction *I,
SmallVectorImpl<Instruction*> &toErase) { SmallVectorImpl<Instruction*> &toErase) {
if (LoadInst* L = dyn_cast<LoadInst>(I)) { if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
bool changed = processLoad(L, toErase); bool Changed = processLoad(LI, toErase);
if (!changed) { if (!Changed) {
unsigned num = VN.lookup_or_add(L); unsigned Num = VN.lookup_or_add(LI);
localAvail[I->getParent()]->table.insert(std::make_pair(num, L)); localAvail[I->getParent()]->table.insert(std::make_pair(Num, LI));
} }
return changed; return Changed;
} }
uint32_t nextNum = VN.getNextUnusedValueNumber(); uint32_t NextNum = VN.getNextUnusedValueNumber();
unsigned num = VN.lookup_or_add(I); unsigned Num = VN.lookup_or_add(I);
if (BranchInst* BI = dyn_cast<BranchInst>(I)) { if (BranchInst *BI = dyn_cast<BranchInst>(I)) {
localAvail[I->getParent()]->table.insert(std::make_pair(num, I)); localAvail[I->getParent()]->table.insert(std::make_pair(Num, I));
if (!BI->isConditional() || isa<Constant>(BI->getCondition())) if (!BI->isConditional() || isa<Constant>(BI->getCondition()))
return false; return false;
Value* branchCond = BI->getCondition(); Value *BranchCond = BI->getCondition();
uint32_t condVN = VN.lookup_or_add(branchCond); uint32_t CondVN = VN.lookup_or_add(BranchCond);
BasicBlock* trueSucc = BI->getSuccessor(0); BasicBlock *TrueSucc = BI->getSuccessor(0);
BasicBlock* falseSucc = BI->getSuccessor(1); BasicBlock *FalseSucc = BI->getSuccessor(1);
if (trueSucc->getSinglePredecessor()) if (TrueSucc->getSinglePredecessor())
localAvail[trueSucc]->table[condVN] = localAvail[TrueSucc]->table[CondVN] =
ConstantInt::getTrue(trueSucc->getContext()); ConstantInt::getTrue(TrueSucc->getContext());
if (falseSucc->getSinglePredecessor()) if (FalseSucc->getSinglePredecessor())
localAvail[falseSucc]->table[condVN] = localAvail[FalseSucc]->table[CondVN] =
ConstantInt::getFalse(trueSucc->getContext()); ConstantInt::getFalse(TrueSucc->getContext());
return false; return false;
// Allocations are always uniquely numbered, so we can save time and memory // Allocations are always uniquely numbered, so we can save time and memory
// by fast failing them. // by fast failing them.
} else if (isa<AllocationInst>(I) || isMalloc(I) || isa<TerminatorInst>(I)) { } else if (isa<AllocationInst>(I) || isMalloc(I) || isa<TerminatorInst>(I)) {
localAvail[I->getParent()]->table.insert(std::make_pair(num, I)); localAvail[I->getParent()]->table.insert(std::make_pair(Num, I));
return false; return false;
} }
// Collapse PHI nodes // Collapse PHI nodes
if (PHINode* p = dyn_cast<PHINode>(I)) { if (PHINode* p = dyn_cast<PHINode>(I)) {
Value* constVal = CollapsePhi(p); Value *constVal = CollapsePhi(p);
if (constVal) { if (constVal) {
for (PhiMapType::iterator PI = phiMap.begin(), PE = phiMap.end(); for (PhiMapType::iterator PI = phiMap.begin(), PE = phiMap.end();
@ -1582,18 +1580,18 @@ bool GVN::processInstruction(Instruction *I,
toErase.push_back(p); toErase.push_back(p);
} else { } else {
localAvail[I->getParent()]->table.insert(std::make_pair(num, I)); localAvail[I->getParent()]->table.insert(std::make_pair(Num, I));
} }
// If the number we were assigned was a brand new VN, then we don't // If the number we were assigned was a brand new VN, then we don't
// need to do a lookup to see if the number already exists // need to do a lookup to see if the number already exists
// somewhere in the domtree: it can't! // somewhere in the domtree: it can't!
} else if (num == nextNum) { } else if (Num == NextNum) {
localAvail[I->getParent()]->table.insert(std::make_pair(num, I)); localAvail[I->getParent()]->table.insert(std::make_pair(Num, I));
// Perform fast-path value-number based elimination of values inherited from // Perform fast-path value-number based elimination of values inherited from
// dominators. // dominators.
} else if (Value* repl = lookupNumber(I->getParent(), num)) { } else if (Value *repl = lookupNumber(I->getParent(), Num)) {
// Remove it! // Remove it!
VN.erase(I); VN.erase(I);
I->replaceAllUsesWith(repl); I->replaceAllUsesWith(repl);
@ -1604,7 +1602,7 @@ bool GVN::processInstruction(Instruction *I,
#if 0 #if 0
// Perform slow-pathvalue-number based elimination with phi construction. // Perform slow-pathvalue-number based elimination with phi construction.
} else if (Value* repl = AttemptRedundancyElimination(I, num)) { } else if (Value *repl = AttemptRedundancyElimination(I, Num)) {
// Remove it! // Remove it!
VN.erase(I); VN.erase(I);
I->replaceAllUsesWith(repl); I->replaceAllUsesWith(repl);
@ -1614,7 +1612,7 @@ bool GVN::processInstruction(Instruction *I,
return true; return true;
#endif #endif
} else { } else {
localAvail[I->getParent()]->table.insert(std::make_pair(num, I)); localAvail[I->getParent()]->table.insert(std::make_pair(Num, I));
} }
return false; return false;
@ -1628,26 +1626,26 @@ bool GVN::runOnFunction(Function& F) {
VN.setMemDep(MD); VN.setMemDep(MD);
VN.setDomTree(DT); VN.setDomTree(DT);
bool changed = false; bool Changed = false;
bool shouldContinue = true; bool ShouldContinue = true;
// Merge unconditional branches, allowing PRE to catch more // Merge unconditional branches, allowing PRE to catch more
// optimization opportunities. // optimization opportunities.
for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) { for (Function::iterator FI = F.begin(), FE = F.end(); FI != FE; ) {
BasicBlock* BB = FI; BasicBlock *BB = FI;
++FI; ++FI;
bool removedBlock = MergeBlockIntoPredecessor(BB, this); bool removedBlock = MergeBlockIntoPredecessor(BB, this);
if (removedBlock) NumGVNBlocks++; if (removedBlock) NumGVNBlocks++;
changed |= removedBlock; Changed |= removedBlock;
} }
unsigned Iteration = 0; unsigned Iteration = 0;
while (shouldContinue) { while (ShouldContinue) {
DEBUG(errs() << "GVN iteration: " << Iteration << "\n"); DEBUG(errs() << "GVN iteration: " << Iteration << "\n");
shouldContinue = iterateOnFunction(F); ShouldContinue = iterateOnFunction(F);
changed |= shouldContinue; Changed |= ShouldContinue;
++Iteration; ++Iteration;
} }
@ -1655,7 +1653,7 @@ bool GVN::runOnFunction(Function& F) {
bool PREChanged = true; bool PREChanged = true;
while (PREChanged) { while (PREChanged) {
PREChanged = performPRE(F); PREChanged = performPRE(F);
changed |= PREChanged; Changed |= PREChanged;
} }
} }
// FIXME: Should perform GVN again after PRE does something. PRE can move // FIXME: Should perform GVN again after PRE does something. PRE can move
@ -1665,19 +1663,19 @@ bool GVN::runOnFunction(Function& F) {
cleanupGlobalSets(); cleanupGlobalSets();
return changed; return Changed;
} }
bool GVN::processBlock(BasicBlock* BB) { bool GVN::processBlock(BasicBlock *BB) {
// FIXME: Kill off toErase by doing erasing eagerly in a helper function (and // FIXME: Kill off toErase by doing erasing eagerly in a helper function (and
// incrementing BI before processing an instruction). // incrementing BI before processing an instruction).
SmallVector<Instruction*, 8> toErase; SmallVector<Instruction*, 8> toErase;
bool changed_function = false; bool ChangedFunction = false;
for (BasicBlock::iterator BI = BB->begin(), BE = BB->end(); for (BasicBlock::iterator BI = BB->begin(), BE = BB->end();
BI != BE;) { BI != BE;) {
changed_function |= processInstruction(BI, toErase); ChangedFunction |= processInstruction(BI, toErase);
if (toErase.empty()) { if (toErase.empty()) {
++BI; ++BI;
continue; continue;
@ -1706,7 +1704,7 @@ bool GVN::processBlock(BasicBlock* BB) {
++BI; ++BI;
} }
return changed_function; return ChangedFunction;
} }
/// performPRE - Perform a purely local form of PRE that looks for diamond /// performPRE - Perform a purely local form of PRE that looks for diamond
@ -1717,7 +1715,7 @@ bool GVN::performPRE(Function& F) {
DenseMap<BasicBlock*, Value*> predMap; DenseMap<BasicBlock*, Value*> predMap;
for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()), for (df_iterator<BasicBlock*> DI = df_begin(&F.getEntryBlock()),
DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) { DE = df_end(&F.getEntryBlock()); DI != DE; ++DI) {
BasicBlock* CurrentBlock = *DI; BasicBlock *CurrentBlock = *DI;
// Nothing to PRE in the entry block. // Nothing to PRE in the entry block.
if (CurrentBlock == &F.getEntryBlock()) continue; if (CurrentBlock == &F.getEntryBlock()) continue;
@ -1733,7 +1731,7 @@ bool GVN::performPRE(Function& F) {
isa<DbgInfoIntrinsic>(CurInst)) isa<DbgInfoIntrinsic>(CurInst))
continue; continue;
uint32_t valno = VN.lookup(CurInst); uint32_t ValNo = VN.lookup(CurInst);
// Look for the predecessors for PRE opportunities. We're // Look for the predecessors for PRE opportunities. We're
// only trying to solve the basic diamond case, where // only trying to solve the basic diamond case, where
@ -1741,9 +1739,9 @@ bool GVN::performPRE(Function& F) {
// but not the other. We also explicitly disallow cases // but not the other. We also explicitly disallow cases
// where the successor is its own predecessor, because they're // where the successor is its own predecessor, because they're
// more complicated to get right. // more complicated to get right.
unsigned numWith = 0; unsigned NumWith = 0;
unsigned numWithout = 0; unsigned NumWithout = 0;
BasicBlock* PREPred = 0; BasicBlock *PREPred = 0;
predMap.clear(); predMap.clear();
for (pred_iterator PI = pred_begin(CurrentBlock), for (pred_iterator PI = pred_begin(CurrentBlock),
@ -1752,44 +1750,44 @@ bool GVN::performPRE(Function& F) {
// own predecessor, on in blocks with predecessors // own predecessor, on in blocks with predecessors
// that are not reachable. // that are not reachable.
if (*PI == CurrentBlock) { if (*PI == CurrentBlock) {
numWithout = 2; NumWithout = 2;
break; break;
} else if (!localAvail.count(*PI)) { } else if (!localAvail.count(*PI)) {
numWithout = 2; NumWithout = 2;
break; break;
} }
DenseMap<uint32_t, Value*>::iterator predV = DenseMap<uint32_t, Value*>::iterator predV =
localAvail[*PI]->table.find(valno); localAvail[*PI]->table.find(ValNo);
if (predV == localAvail[*PI]->table.end()) { if (predV == localAvail[*PI]->table.end()) {
PREPred = *PI; PREPred = *PI;
numWithout++; NumWithout++;
} else if (predV->second == CurInst) { } else if (predV->second == CurInst) {
numWithout = 2; NumWithout = 2;
} else { } else {
predMap[*PI] = predV->second; predMap[*PI] = predV->second;
numWith++; NumWith++;
} }
} }
// Don't do PRE when it might increase code size, i.e. when // Don't do PRE when it might increase code size, i.e. when
// we would need to insert instructions in more than one pred. // we would need to insert instructions in more than one pred.
if (numWithout != 1 || numWith == 0) if (NumWithout != 1 || NumWith == 0)
continue; continue;
// We can't do PRE safely on a critical edge, so instead we schedule // We can't do PRE safely on a critical edge, so instead we schedule
// the edge to be split and perform the PRE the next time we iterate // the edge to be split and perform the PRE the next time we iterate
// on the function. // on the function.
unsigned succNum = 0; unsigned SuccNum = 0;
for (unsigned i = 0, e = PREPred->getTerminator()->getNumSuccessors(); for (unsigned i = 0, e = PREPred->getTerminator()->getNumSuccessors();
i != e; ++i) i != e; ++i)
if (PREPred->getTerminator()->getSuccessor(i) == CurrentBlock) { if (PREPred->getTerminator()->getSuccessor(i) == CurrentBlock) {
succNum = i; SuccNum = i;
break; break;
} }
if (isCriticalEdge(PREPred->getTerminator(), succNum)) { if (isCriticalEdge(PREPred->getTerminator(), SuccNum)) {
toSplit.push_back(std::make_pair(PREPred->getTerminator(), succNum)); toSplit.push_back(std::make_pair(PREPred->getTerminator(), SuccNum));
continue; continue;
} }
@ -1798,7 +1796,7 @@ bool GVN::performPRE(Function& F) {
// will be available in the predecessor by the time we need them. Any // will be available in the predecessor by the time we need them. Any
// that weren't original present will have been instantiated earlier // that weren't original present will have been instantiated earlier
// in this loop. // in this loop.
Instruction* PREInstr = CurInst->clone(CurInst->getContext()); Instruction *PREInstr = CurInst->clone(CurInst->getContext());
bool success = true; bool success = true;
for (unsigned i = 0, e = CurInst->getNumOperands(); i != e; ++i) { for (unsigned i = 0, e = CurInst->getNumOperands(); i != e; ++i) {
Value *Op = PREInstr->getOperand(i); Value *Op = PREInstr->getOperand(i);
@ -1825,11 +1823,11 @@ bool GVN::performPRE(Function& F) {
PREInstr->insertBefore(PREPred->getTerminator()); PREInstr->insertBefore(PREPred->getTerminator());
PREInstr->setName(CurInst->getName() + ".pre"); PREInstr->setName(CurInst->getName() + ".pre");
predMap[PREPred] = PREInstr; predMap[PREPred] = PREInstr;
VN.add(PREInstr, valno); VN.add(PREInstr, ValNo);
NumGVNPRE++; NumGVNPRE++;
// Update the availability map to include the new instruction. // Update the availability map to include the new instruction.
localAvail[PREPred]->table.insert(std::make_pair(valno, PREInstr)); localAvail[PREPred]->table.insert(std::make_pair(ValNo, PREInstr));
// Create a PHI to make the value available in this block. // Create a PHI to make the value available in this block.
PHINode* Phi = PHINode::Create(CurInst->getType(), PHINode* Phi = PHINode::Create(CurInst->getType(),
@ -1839,8 +1837,8 @@ bool GVN::performPRE(Function& F) {
PE = pred_end(CurrentBlock); PI != PE; ++PI) PE = pred_end(CurrentBlock); PI != PE; ++PI)
Phi->addIncoming(predMap[*PI], *PI); Phi->addIncoming(predMap[*PI], *PI);
VN.add(Phi, valno); VN.add(Phi, ValNo);
localAvail[CurrentBlock]->table[valno] = Phi; localAvail[CurrentBlock]->table[ValNo] = Phi;
CurInst->replaceAllUsesWith(Phi); CurInst->replaceAllUsesWith(Phi);
if (isa<PointerType>(Phi->getType())) if (isa<PointerType>(Phi->getType()))
@ -1876,20 +1874,20 @@ bool GVN::iterateOnFunction(Function &F) {
} }
// Top-down walk of the dominator tree // Top-down walk of the dominator tree
bool changed = false; bool Changed = false;
#if 0 #if 0
// Needed for value numbering with phi construction to work. // Needed for value numbering with phi construction to work.
ReversePostOrderTraversal<Function*> RPOT(&F); ReversePostOrderTraversal<Function*> RPOT(&F);
for (ReversePostOrderTraversal<Function*>::rpo_iterator RI = RPOT.begin(), for (ReversePostOrderTraversal<Function*>::rpo_iterator RI = RPOT.begin(),
RE = RPOT.end(); RI != RE; ++RI) RE = RPOT.end(); RI != RE; ++RI)
changed |= processBlock(*RI); Changed |= processBlock(*RI);
#else #else
for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()), for (df_iterator<DomTreeNode*> DI = df_begin(DT->getRootNode()),
DE = df_end(DT->getRootNode()); DI != DE; ++DI) DE = df_end(DT->getRootNode()); DI != DE; ++DI)
changed |= processBlock(DI->getBlock()); Changed |= processBlock(DI->getBlock());
#endif #endif
return changed; return Changed;
} }
void GVN::cleanupGlobalSets() { void GVN::cleanupGlobalSets() {