[llvm-commits] CVS: llvm/lib/Transforms/Scalar/InstructionCombining.cpp

Chris Lattner lattner at cs.uiuc.edu
Sun Jan 16 21:10:30 PST 2005



Changes in directory llvm/lib/Transforms/Scalar:

InstructionCombining.cpp updated: 1.306 -> 1.307
---
Log message:

Delete PHI nodes that are not dead but are locked in a cycle of single
useness.


---
Diffs of the changes:  (+26 -0)

Index: llvm/lib/Transforms/Scalar/InstructionCombining.cpp
diff -u llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.306 llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.307
--- llvm/lib/Transforms/Scalar/InstructionCombining.cpp:1.306	Sun Jan 16 21:20:02 2005
+++ llvm/lib/Transforms/Scalar/InstructionCombining.cpp	Sun Jan 16 23:10:15 2005
@@ -4051,6 +4051,22 @@
                          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<PHINode*> &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<PHINode>(PN->use_back()))
+    return DeadPHICycle(PU, PotentiallyDeadPHIs);
+  
+  return false;
+}
+
 // PHINode simplification
 //
 Instruction *InstCombiner::visitPHINode(PHINode &PN) {
@@ -4109,6 +4125,16 @@
     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<PHINode>(PN.use_back())) {
+      std::set<PHINode*> PotentiallyDeadPHIs;
+      PotentiallyDeadPHIs.insert(&PN);
+      if (DeadPHICycle(PU, PotentiallyDeadPHIs))
+        return ReplaceInstUsesWith(PN, UndefValue::get(PN.getType()));
+    }
   
   return 0;
 }






More information about the llvm-commits mailing list