From 0f98e75adff9024dcfe1d2afbfa83625d60ebaa8 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 19 Dec 2003 06:27:08 +0000 Subject: [PATCH 1/1] Generalize a special case to fix PR187 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@10531 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Utils/LoopSimplify.cpp | 46 ++++++++++++++++----------- 1 file changed, 28 insertions(+), 18 deletions(-) diff --git a/lib/Transforms/Utils/LoopSimplify.cpp b/lib/Transforms/Utils/LoopSimplify.cpp index 64b0a7cc708..9beae4fd075 100644 --- a/lib/Transforms/Utils/LoopSimplify.cpp +++ b/lib/Transforms/Utils/LoopSimplify.cpp @@ -33,12 +33,12 @@ //===----------------------------------------------------------------------===// #include "llvm/Transforms/Scalar.h" -#include "llvm/Analysis/Dominators.h" -#include "llvm/Analysis/LoopInfo.h" #include "llvm/Function.h" #include "llvm/iTerminators.h" #include "llvm/iPHINode.h" #include "llvm/Constant.h" +#include "llvm/Analysis/Dominators.h" +#include "llvm/Analysis/LoopInfo.h" #include "llvm/Support/CFG.h" #include "Support/SetOperations.h" #include "Support/Statistic.h" @@ -164,18 +164,22 @@ BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB, // incoming edges in BB into new PHI nodes in NewBB. // if (!Preds.empty()) { // Is the loop not obviously dead? - if (Preds.size() == 1) { - // No need to insert one operand PHI nodes! Instead, just update the - // incoming block ID's. - for (BasicBlock::iterator I = BB->begin(); - PHINode *PN = dyn_cast(I); ++I) { - unsigned i = PN->getBasicBlockIndex(Preds[0]); - PN->setIncomingBlock(i, NewBB); - } - } else { - for (BasicBlock::iterator I = BB->begin(); - PHINode *PN = dyn_cast(I); ++I) { - + // Check to see if the values being merged into the new block need PHI + // nodes. If so, insert them. + for (BasicBlock::iterator I = BB->begin(); + PHINode *PN = dyn_cast(I); ++I) { + + // Check to see if all of the values coming in are the same. If so, we + // don't need to create a new PHI node. + Value *InVal = PN->getIncomingValueForBlock(Preds[0]); + for (unsigned i = 1, e = Preds.size(); i != e; ++i) + if (InVal != PN->getIncomingValueForBlock(Preds[i])) { + InVal = 0; + break; + } + + // If the values coming into the block are not the same, we need a PHI. + if (InVal == 0) { // Create the new PHI node, insert it into NewBB at the end of the block PHINode *NewPHI = new PHINode(PN->getType(), PN->getName()+".ph", BI); @@ -184,11 +188,17 @@ BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB, Value *V = PN->removeIncomingValue(Preds[i]); NewPHI->addIncoming(V, Preds[i]); } - - // Add an incoming value to the PHI node in the loop for the preheader - // edge. - PN->addIncoming(NewPHI, NewBB); + InVal = NewPHI; + } else { + // Remove all of the edges coming into the PHI nodes from outside of the + // block. + for (unsigned i = 0, e = Preds.size(); i != e; ++i) + PN->removeIncomingValue(Preds[i], false); } + + // Add an incoming value to the PHI node in the loop for the preheader + // edge. + PN->addIncoming(InVal, NewBB); } // Now that the PHI nodes are updated, actually move the edges from -- 2.34.1