Fix PR5258, jump-threading creating invalid PHIs.
When an incoming value for a PHI is updated, we must also updated all other
incoming values for the same BB to match, otherwise we create invalid PHIs.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@84638 91177308-0d34-0410-b5e6-96231b3b80d8
diff --git a/lib/Transforms/Utils/SSAUpdater.cpp b/lib/Transforms/Utils/SSAUpdater.cpp
index ee2f37b..ed9c0ee 100644
--- a/lib/Transforms/Utils/SSAUpdater.cpp
+++ b/lib/Transforms/Utils/SSAUpdater.cpp
@@ -178,10 +178,18 @@
void SSAUpdater::RewriteUse(Use &U) {
Instruction *User = cast<Instruction>(U.getUser());
BasicBlock *UseBB = User->getParent();
- if (PHINode *UserPN = dyn_cast<PHINode>(User))
+ PHINode *UserPN = dyn_cast<PHINode>(User);
+ if (UserPN)
UseBB = UserPN->getIncomingBlock(U);
- U.set(GetValueInMiddleOfBlock(UseBB));
+ Value *V = GetValueInMiddleOfBlock(UseBB);
+ U.set(V);
+ if (UserPN) {
+ // Incoming value from the same BB must be consistent
+ for (unsigned i=0;i<UserPN->getNumIncomingValues();i++)
+ if (UserPN->getIncomingBlock(i) == UseBB)
+ UserPN->setIncomingValue(i, V);
+ }
}