From c30bda7540de573c887e00bb76ac78d85f56acd4 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Sun, 17 Oct 2004 21:22:38 +0000 Subject: [PATCH] hasConstantValue will soon return instructions that don't dominate the PHI node, so prepare for this. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@17095 91177308-0d34-0410-b5e6-96231b3b80d8 --- .../Scalar/InstructionCombining.cpp | 22 +++++++++-- lib/Transforms/Utils/LoopSimplify.cpp | 37 +++++++++++-------- 2 files changed, 39 insertions(+), 20 deletions(-) diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 6aef136cf52..784e80429d9 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -3364,7 +3364,7 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { } AddUsersToWorkList(*Caller); } else { - NV = Constant::getNullValue(Caller->getType()); + NV = UndefValue::get(Caller->getType()); } } @@ -3380,9 +3380,23 @@ bool InstCombiner::transformConstExprCastCall(CallSite CS) { // PHINode simplification // Instruction *InstCombiner::visitPHINode(PHINode &PN) { - // FIXME: hasConstantValue should ignore undef values! - if (Value *V = hasConstantValue(&PN)) - return ReplaceInstUsesWith(PN, V); + if (Value *V = hasConstantValue(&PN)) { + // If V is an instruction, we have to be certain that it dominates PN. + // However, because we don't have dom info, we can't do a perfect job. + if (Instruction *I = dyn_cast(V)) { + // We know that the instruction dominates the PHI if there are no undef + // values coming in. + for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) + if (isa(PN.getIncomingValue(i))) { + std::cerr << "HAD TO DISABLE PHI ELIM IN IC!\n"; + V = 0; + break; + } + } + + if (V) + return ReplaceInstUsesWith(PN, V); + } // If the only user of this instruction is a cast instruction, and all of the // incoming values are constants, change this PHI to merge together the casted diff --git a/lib/Transforms/Utils/LoopSimplify.cpp b/lib/Transforms/Utils/LoopSimplify.cpp index aa3a86573ac..c7b92bbdda4 100644 --- a/lib/Transforms/Utils/LoopSimplify.cpp +++ b/lib/Transforms/Utils/LoopSimplify.cpp @@ -250,8 +250,11 @@ BasicBlock *LoopSimplify::SplitBlockPredecessors(BasicBlock *BB, // Can we eliminate this phi node now? if (Value *V = hasConstantValue(PN)) { - PN->replaceAllUsesWith(V); - BB->getInstList().erase(PN); + if (!isa(V) || + getAnalysis().dominates(cast(V), PN)) { + PN->replaceAllUsesWith(V); + BB->getInstList().erase(PN); + } } } @@ -426,22 +429,24 @@ static void AddBlockAndPredsToSet(BasicBlock *BB, BasicBlock *StopBlock, /// FindPHIToPartitionLoops - The first part of loop-nestification is to find a /// PHI node that tells us how to partition the loops. -static PHINode *FindPHIToPartitionLoops(Loop *L) { +static PHINode *FindPHIToPartitionLoops(Loop *L, DominatorSet &DS) { for (BasicBlock::iterator I = L->getHeader()->begin(); isa(I); ) { PHINode *PN = cast(I); ++I; - if (Value *V = hasConstantValue(PN)) { - // This is a degenerate PHI already, don't modify it! - PN->replaceAllUsesWith(V); - PN->getParent()->getInstList().erase(PN); - } else { - // Scan this PHI node looking for a use of the PHI node by itself. - for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) - if (PN->getIncomingValue(i) == PN && - L->contains(PN->getIncomingBlock(i))) - // We found something tasty to remove. - return PN; - } + if (Value *V = hasConstantValue(PN)) + if (!isa(V) || DS.dominates(cast(V), PN)) { + // This is a degenerate PHI already, don't modify it! + PN->replaceAllUsesWith(V); + PN->getParent()->getInstList().erase(PN); + continue; + } + + // Scan this PHI node looking for a use of the PHI node by itself. + for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) + if (PN->getIncomingValue(i) == PN && + L->contains(PN->getIncomingBlock(i))) + // We found something tasty to remove. + return PN; } return 0; } @@ -464,7 +469,7 @@ static PHINode *FindPHIToPartitionLoops(Loop *L) { /// created. /// Loop *LoopSimplify::SeparateNestedLoop(Loop *L) { - PHINode *PN = FindPHIToPartitionLoops(L); + PHINode *PN = FindPHIToPartitionLoops(L, getAnalysis()); if (PN == 0) return 0; // No known way to partition. // Pull out all predecessors that have varying values in the loop. This