From 6e1d5a8b28b681e7fd7e1efa69e01fe92aaa7268 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 3 Aug 2005 00:59:12 +0000 Subject: [PATCH] Finally, add the required constraint checks to fix Transforms/SimplifyCFG/2005-08-01-PHIUpdateFail.ll the right way llvm-svn: 22615 --- lib/Transforms/Utils/SimplifyCFG.cpp | 29 +++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index 5f368bc786f..742efe61844 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -98,8 +98,35 @@ static bool CanPropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { } } } + + // Finally, if BB has PHI nodes that are used by things other than the PHIs in + // Succ and Succ has predecessors that are not Succ and not Pred, we cannot + // fold these blocks, as we don't know whether BB dominates Succ or not to + // update the PHI nodes correctly. + if (!isa(BB->begin()) || Succ->getSinglePredecessor()) return true; - return true; + // If the predecessors of Succ are only BB and Succ itself, we can handle this. + bool IsSafe = true; + for (pred_iterator PI = pred_begin(Succ), E = pred_end(Succ); PI != E; ++PI) + if (*PI != Succ && *PI != BB) { + IsSafe = false; + break; + } + if (IsSafe) return true; + + // If the PHI nodes in BB are only used by instructions in Succ, we are ok. + IsSafe = true; + for (BasicBlock::iterator I = BB->begin(); isa(I) && IsSafe; ++I) { + PHINode *PN = cast(I); + for (Value::use_iterator UI = PN->use_begin(), E = PN->use_end(); UI != E; + ++UI) + if (cast(*UI)->getParent() != Succ) { + IsSafe = false; + break; + } + } + + return IsSafe; } /// TryToSimplifyUncondBranchFromEmptyBlock - BB contains an unconditional