From e2ca540e7c29703c3b943a96fed7e383c7bd35c0 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Wed, 5 Mar 2003 21:01:52 +0000 Subject: [PATCH] Implement testcase CFGSimplify/EqualPHIEdgeBlockMerge.ll git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@5699 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/SimplifyCFG.cpp | 34 ++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 10 deletions(-) diff --git a/lib/Transforms/Utils/SimplifyCFG.cpp b/lib/Transforms/Utils/SimplifyCFG.cpp index 08fe1c5638f..b1a1bdee057 100644 --- a/lib/Transforms/Utils/SimplifyCFG.cpp +++ b/lib/Transforms/Utils/SimplifyCFG.cpp @@ -14,8 +14,9 @@ // PropagatePredecessors - This gets "Succ" ready to have the predecessors from // "BB". This is a little tricky because "Succ" has PHI nodes, which need to // have extra slots added to them to hold the merge edges from BB's -// predecessors. This function returns true (failure) if the Succ BB already -// has a predecessor that is a predecessor of BB. +// predecessors, and BB itself might have had PHI nodes in it. This function +// returns true (failure) if the Succ BB already has a predecessor that is a +// predecessor of BB and incoming PHI arguments would not be discernable. // // Assumption: Succ is the single successor for BB. // @@ -31,12 +32,27 @@ static bool PropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { const std::vector BBPreds(pred_begin(BB), pred_end(BB)); // Check to see if one of the predecessors of BB is already a predecessor of - // Succ. If so, we cannot do the transformation! + // Succ. If so, we cannot do the transformation if there are any PHI nodes + // with incompatible values coming in from the two edges! // - for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ); - PI != PE; ++PI) - if (find(BBPreds.begin(), BBPreds.end(), *PI) != BBPreds.end()) - return true; + for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ); PI != PE; ++PI) + if (find(BBPreds.begin(), BBPreds.end(), *PI) != BBPreds.end()) { + // Loop over all of the PHI nodes checking to see if there are + // incompatible values coming in. + BasicBlock::iterator BBI = Succ->begin(); + while (PHINode *PN = dyn_cast(&*BBI++)) { + // Loop up the entries in the PHI node for BB and for *PI if the values + // coming in are non-equal, we cannot merge these two blocks (instead we + // should insert a conditional move or something, then merge the + // blocks). + int Idx1 = PN->getBasicBlockIndex(BB); + int Idx2 = PN->getBasicBlockIndex(*PI); + assert(Idx1 != -1 && Idx2 != -1 && + "Didn't have entries for my predecessors??"); + if (PN->getIncomingValue(Idx1) != PN->getIncomingValue(Idx2)) + return true; // Values are not equal... + } + } // Loop over all of the PHI nodes in the successor BB for (BasicBlock::iterator I = Succ->begin(); @@ -57,9 +73,7 @@ static bool PropagatePredecessorsForPHIs(BasicBlock *BB, BasicBlock *Succ) { // SimplifyCFG - This function is used to do simplification of a CFG. For // example, it adjusts branches to branches to eliminate the extra hop, it // eliminates unreachable basic blocks, and does other "peephole" optimization -// of the CFG. It returns true if a modification was made, and returns an -// iterator that designates the first element remaining after the block that -// was deleted. +// of the CFG. It returns true if a modification was made. // // WARNING: The entry node of a function may not be simplified. //