diff --git a/llvm/include/llvm/Transforms/Scalar/ConstantProp.h b/llvm/include/llvm/Transforms/Scalar/ConstantProp.h index 3a8fa5b6d9a1..0da13e9fdbd9 100644 --- a/llvm/include/llvm/Transforms/Scalar/ConstantProp.h +++ b/llvm/include/llvm/Transforms/Scalar/ConstantProp.h @@ -23,9 +23,11 @@ bool doConstantPropogation(BasicBlock *BB, BasicBlock::iterator &I); // ConstantFoldTerminator - If a terminator instruction is predicated on a // constant value, convert it into an unconditional branch to the constant -// destination. +// destination. This is a nontrivial operation because the successors of this +// basic block must have their PHI nodes updated. // -bool ConstantFoldTerminator(TerminatorInst *T); +bool ConstantFoldTerminator(BasicBlock *BB, BasicBlock::iterator &I, + TerminatorInst *T); //===----------------------------------------------------------------------===// diff --git a/llvm/lib/Transforms/Scalar/ConstantProp.cpp b/llvm/lib/Transforms/Scalar/ConstantProp.cpp index fc4e3bf0e3ea..624e6da39da2 100644 --- a/llvm/lib/Transforms/Scalar/ConstantProp.cpp +++ b/llvm/lib/Transforms/Scalar/ConstantProp.cpp @@ -102,7 +102,8 @@ ConstantFoldBinaryInst(BasicBlock *BB, BasicBlock::iterator &II, // constant value, convert it into an unconditional branch to the constant // destination. // -bool ConstantFoldTerminator(TerminatorInst *T) { +bool ConstantFoldTerminator(BasicBlock *BB, BasicBlock::iterator &II, + TerminatorInst *T) { // Branch - See if we are conditional jumping on constant if (BranchInst *BI = dyn_cast(T)) { if (BI->isUnconditional()) return false; // Can't optimize uncond branch @@ -127,6 +128,7 @@ bool ConstantFoldTerminator(TerminatorInst *T) { // Set the unconditional destination, and change the insn to be an // unconditional branch. BI->setUnconditionalDest(Destination); + II = BB->end()-1; // Update instruction iterator! return true; } #if 0 @@ -171,7 +173,7 @@ bool doConstantPropogation(BasicBlock *BB, BasicBlock::iterator &II) { Constant *D = dyn_cast(UInst->getOperand(0)); if (D) return ConstantFoldUnaryInst(BB, II, UInst, D); } else if (TerminatorInst *TInst = dyn_cast(Inst)) { - return ConstantFoldTerminator(TInst); + return ConstantFoldTerminator(BB, II, TInst); } else if (PHINode *PN = dyn_cast(Inst)) { // If it's a PHI node and only has one operand diff --git a/llvm/lib/Transforms/Scalar/SCCP.cpp b/llvm/lib/Transforms/Scalar/SCCP.cpp index 447a3e196000..2bf6c86e95ac 100644 --- a/llvm/lib/Transforms/Scalar/SCCP.cpp +++ b/llvm/lib/Transforms/Scalar/SCCP.cpp @@ -270,7 +270,7 @@ bool SCCP::doSCCP() { // Hey, we just changed something! MadeChanges = true; } else if (TerminatorInst *TI = dyn_cast(Inst)) { - MadeChanges |= ConstantFoldTerminator(TI); + MadeChanges |= ConstantFoldTerminator(BB, BI, TI); } ++BI;