Rename some variables, only increment BI once at the start of the loop instead of throughout it.

llvm-svn: 60339
This commit is contained in:
Chris Lattner 2008-12-01 07:35:54 +00:00
parent f00aae4968
commit 6f5bf6a718
1 changed files with 31 additions and 39 deletions

View File

@ -1223,7 +1223,7 @@ bool GVN::processBlock(DomTreeNode* DTN) {
/// performPRE - Perform a purely local form of PRE that looks for diamond /// performPRE - Perform a purely local form of PRE that looks for diamond
/// control flow patterns and attempts to perform simple PRE at the join point. /// control flow patterns and attempts to perform simple PRE at the join point.
bool GVN::performPRE(Function& F) { bool GVN::performPRE(Function& F) {
bool changed = false; bool Changed = false;
SmallVector<std::pair<TerminatorInst*, unsigned>, 4> toSplit; SmallVector<std::pair<TerminatorInst*, unsigned>, 4> toSplit;
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()),
@ -1235,14 +1235,14 @@ bool GVN::performPRE(Function& F) {
for (BasicBlock::iterator BI = CurrentBlock->begin(), for (BasicBlock::iterator BI = CurrentBlock->begin(),
BE = CurrentBlock->end(); BI != BE; ) { BE = CurrentBlock->end(); BI != BE; ) {
if (isa<AllocationInst>(BI) || isa<TerminatorInst>(BI) || Instruction *CurInst = BI++;
isa<PHINode>(BI) || BI->mayReadFromMemory() ||
BI->mayWriteToMemory()) {
BI++;
continue;
}
uint32_t valno = VN.lookup(BI); if (isa<AllocationInst>(CurInst) || isa<TerminatorInst>(CurInst) ||
isa<PHINode>(CurInst) || CurInst->mayReadFromMemory() ||
CurInst->mayWriteToMemory())
continue;
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
@ -1273,7 +1273,7 @@ bool GVN::performPRE(Function& F) {
if (predV == localAvail[*PI]->table.end()) { if (predV == localAvail[*PI]->table.end()) {
PREPred = *PI; PREPred = *PI;
numWithout++; numWithout++;
} else if (predV->second == BI) { } else if (predV->second == CurInst) {
numWithout = 2; numWithout = 2;
} else { } else {
predMap[*PI] = predV->second; predMap[*PI] = predV->second;
@ -1283,10 +1283,8 @@ bool GVN::performPRE(Function& F) {
// 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)
BI++;
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
@ -1301,8 +1299,6 @@ bool GVN::performPRE(Function& F) {
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));
changed = true;
BI++;
continue; continue;
} }
@ -1311,19 +1307,18 @@ 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 = BI->clone(); Instruction* PREInstr = CurInst->clone();
bool success = true; bool success = true;
for (unsigned i = 0; i < BI->getNumOperands(); ++i) { for (unsigned i = 0, e = CurInst->getNumOperands(); i != e; ++i) {
Value* op = BI->getOperand(i); Value *Op = PREInstr->getOperand(i);
if (isa<Argument>(op) || isa<Constant>(op) || isa<GlobalValue>(op)) if (isa<Argument>(Op) || isa<Constant>(Op) || isa<GlobalValue>(Op))
PREInstr->setOperand(i, op); continue;
else {
Value* V = lookupNumber(PREPred, VN.lookup(op)); if (Value *V = lookupNumber(PREPred, VN.lookup(Op))) {
if (!V) { PREInstr->setOperand(i, V);
success = false; } else {
break; success = false;
} else break;
PREInstr->setOperand(i, V);
} }
} }
@ -1332,12 +1327,11 @@ bool GVN::performPRE(Function& F) {
// are not value numbered precisely. // are not value numbered precisely.
if (!success) { if (!success) {
delete PREInstr; delete PREInstr;
BI++;
continue; continue;
} }
PREInstr->insertBefore(PREPred->getTerminator()); PREInstr->insertBefore(PREPred->getTerminator());
PREInstr->setName(BI->getName() + ".pre"); PREInstr->setName(CurInst->getName() + ".pre");
predMap[PREPred] = PREInstr; predMap[PREPred] = PREInstr;
VN.add(PREInstr, valno); VN.add(PREInstr, valno);
NumGVNPRE++; NumGVNPRE++;
@ -1346,8 +1340,8 @@ bool GVN::performPRE(Function& F) {
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(BI->getType(), PHINode* Phi = PHINode::Create(CurInst->getType(),
BI->getName() + ".pre-phi", CurInst->getName() + ".pre-phi",
CurrentBlock->begin()); CurrentBlock->begin());
for (pred_iterator PI = pred_begin(CurrentBlock), for (pred_iterator PI = pred_begin(CurrentBlock),
PE = pred_end(CurrentBlock); PI != PE; ++PI) PE = pred_end(CurrentBlock); PI != PE; ++PI)
@ -1356,15 +1350,13 @@ bool GVN::performPRE(Function& F) {
VN.add(Phi, valno); VN.add(Phi, valno);
localAvail[CurrentBlock]->table[valno] = Phi; localAvail[CurrentBlock]->table[valno] = Phi;
BI->replaceAllUsesWith(Phi); CurInst->replaceAllUsesWith(Phi);
VN.erase(BI); VN.erase(CurInst);
Instruction* erase = BI; DEBUG(cerr << "GVN PRE removed: " << *CurInst);
BI++; MD->removeInstruction(CurInst);
DEBUG(cerr << "GVN PRE removed: " << *erase); CurInst->eraseFromParent();
MD->removeInstruction(erase); Changed = true;
erase->eraseFromParent();
changed = true;
} }
} }
@ -1372,7 +1364,7 @@ bool GVN::performPRE(Function& F) {
I = toSplit.begin(), E = toSplit.end(); I != E; ++I) I = toSplit.begin(), E = toSplit.end(); I != E; ++I)
SplitCriticalEdge(I->first, I->second, this); SplitCriticalEdge(I->first, I->second, this);
return changed || toSplit.size(); return Changed || toSplit.size();
} }
// iterateOnFunction - Executes one iteration of GVN // iterateOnFunction - Executes one iteration of GVN