From 2486686689af91c02357fd0925ed213e4dfe0ed1 Mon Sep 17 00:00:00 2001 From: Owen Anderson Date: Sun, 16 Sep 2007 08:04:16 +0000 Subject: [PATCH] Be more careful when constant-folding PHI nodes. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@41998 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Transforms/Scalar/GVN.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/Transforms/Scalar/GVN.cpp b/lib/Transforms/Scalar/GVN.cpp index 75edd8eaf9e..7f809e73978 100644 --- a/lib/Transforms/Scalar/GVN.cpp +++ b/lib/Transforms/Scalar/GVN.cpp @@ -673,6 +673,7 @@ namespace { void dump(DenseMap& d); bool iterateOnFunction(Function &F); Value* CollapsePhi(PHINode* p); + bool isSafeReplacement(PHINode* p, Instruction* inst); }; char GVN::ID = 0; @@ -731,7 +732,8 @@ Value* GVN::CollapsePhi(PHINode* p) { if (constVal) { if (Instruction* inst = dyn_cast(constVal)) { if (DT.dominates(inst, p)) - return inst; + if (isSafeReplacement(p, inst)) + return inst; } else { return constVal; } @@ -740,6 +742,19 @@ Value* GVN::CollapsePhi(PHINode* p) { return 0; } +bool GVN::isSafeReplacement(PHINode* p, Instruction* inst) { + if (!isa(inst)) + return true; + + for (Instruction::use_iterator UI = p->use_begin(), E = p->use_end(); + UI != E; ++UI) + if (PHINode* use_phi = dyn_cast(UI)) + if (use_phi->getParent() == inst->getParent()) + return false; + + return true; +} + /// GetValueForBlock - Get the value to use within the specified basic block. /// available values are in Phis. Value *GVN::GetValueForBlock(BasicBlock *BB, LoadInst* orig,