From: Chris Lattner Date: Mon, 17 Jan 2005 05:10:15 +0000 (+0000) Subject: Delete PHI nodes that are not dead but are locked in a cycle of single X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=a3fd1c569c939a110f083a07b9ccec5a30fbba43;p=oota-llvm.git Delete PHI nodes that are not dead but are locked in a cycle of single useness. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@19629 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Transforms/Scalar/InstructionCombining.cpp b/lib/Transforms/Scalar/InstructionCombining.cpp index 482b5785609..e76c2ff8d5d 100644 --- a/lib/Transforms/Scalar/InstructionCombining.cpp +++ b/lib/Transforms/Scalar/InstructionCombining.cpp @@ -4051,6 +4051,22 @@ Instruction *InstCombiner::FoldPHIArgOpIntoPHI(PHINode &PN) { PhiVal, ConstantOp); } +/// DeadPHICycle - Return true if this PHI node is only used by a PHI node cycle +/// that is dead. +static bool DeadPHICycle(PHINode *PN, std::set &PotentiallyDeadPHIs) { + if (PN->use_empty()) return true; + if (!PN->hasOneUse()) return false; + + // Remember this node, and if we find the cycle, return. + if (!PotentiallyDeadPHIs.insert(PN).second) + return true; + + if (PHINode *PU = dyn_cast(PN->use_back())) + return DeadPHICycle(PU, PotentiallyDeadPHIs); + + return false; +} + // PHINode simplification // Instruction *InstCombiner::visitPHINode(PHINode &PN) { @@ -4109,6 +4125,16 @@ Instruction *InstCombiner::visitPHINode(PHINode &PN) { if (Instruction *Result = FoldPHIArgOpIntoPHI(PN)) return Result; + // If this is a trivial cycle in the PHI node graph, remove it. Basically, if + // this PHI only has a single use (a PHI), and if that PHI only has one use (a + // PHI)... break the cycle. + if (PN.hasOneUse()) + if (PHINode *PU = dyn_cast(PN.use_back())) { + std::set PotentiallyDeadPHIs; + PotentiallyDeadPHIs.insert(&PN); + if (DeadPHICycle(PU, PotentiallyDeadPHIs)) + return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType())); + } return 0; }