diff --git a/lib/Transforms/Utils/LoopSimplify.cpp b/lib/Transforms/Utils/LoopSimplify.cpp index da1a6550a49..69b2ec44470 100644 --- a/lib/Transforms/Utils/LoopSimplify.cpp +++ b/lib/Transforms/Utils/LoopSimplify.cpp @@ -71,7 +71,7 @@ namespace { AU.addPreserved(); AU.addPreserved(); AU.addPreserved(); - AU.addPreservedID(BreakCriticalEdgesID); // No crit edges added.... + AU.addPreservedID(BreakCriticalEdgesID); // No critical edges added. } private: bool ProcessLoop(Loop *L); @@ -190,8 +190,23 @@ bool LoopSimplify::ProcessLoop(Loop *L) { Changed = true; } + // Scan over the PHI nodes in the loop header. Since they now have only two + // incoming values (the loop is canonicalized), we may have simplified the PHI + // down to 'X = phi [X, Y]', which should be replaced with 'Y'. + PHINode *PN; + DominatorSet &DS = getAnalysis(); + for (BasicBlock::iterator I = L->getHeader()->begin(); + (PN = dyn_cast(I++)); ) + if (Value *V = PN->hasConstantValue(true)) + if (!isa(V) || + DS.dominates(cast(V)->getParent(), L->getHeader())) { + PN->replaceAllUsesWith(V); + PN->eraseFromParent(); + } + for (Loop::iterator I = L->begin(), E = L->end(); I != E; ++I) Changed |= ProcessLoop(*I); + return Changed; }