[llvm-commits] [llvm] r60501 - in /llvm/trunk: include/llvm/Transforms/Utils/BasicBlockUtils.h include/llvm/Transforms/Utils/Local.h lib/Transforms/Scalar/CondPropagate.cpp lib/Transforms/Utils/BasicBlockUtils.cpp lib/Transforms/Utils/SimplifyCFG.cpp lib/Transforms/Utils/UnrollLoop.cpp

Chris Lattner sabre at nondot.org
Wed Dec 3 11:44:11 PST 2008


Author: lattner
Date: Wed Dec  3 13:44:02 2008
New Revision: 60501

URL: http://llvm.org/viewvc/llvm-project?rev=60501&view=rev
Log:
Factor some code into a new FoldSingleEntryPHINodes method.

Modified:
    llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h
    llvm/trunk/include/llvm/Transforms/Utils/Local.h
    llvm/trunk/lib/Transforms/Scalar/CondPropagate.cpp
    llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp
    llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
    llvm/trunk/lib/Transforms/Utils/UnrollLoop.cpp

Modified: llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h?rev=60501&r1=60500&r2=60501&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h (original)
+++ llvm/trunk/include/llvm/Transforms/Utils/BasicBlockUtils.h Wed Dec  3 13:44:02 2008
@@ -30,6 +30,14 @@
 /// predecessors.
 void DeleteDeadBlock(BasicBlock *BB);
   
+  
+/// FoldSingleEntryPHINodes - We know that BB has one predecessor.  If there are
+/// any single-entry PHI nodes in it, fold them away.  This handles the case
+/// when all entries to the PHI nodes in a block are guaranteed equal, such as
+/// when the block has exactly one predecessor.
+void FoldSingleEntryPHINodes(BasicBlock *BB);
+  
+  
 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
 /// if possible.  The return value indicates success or failure.
 bool MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P = 0);

Modified: llvm/trunk/include/llvm/Transforms/Utils/Local.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Transforms/Utils/Local.h?rev=60501&r1=60500&r2=60501&view=diff

==============================================================================
--- llvm/trunk/include/llvm/Transforms/Utils/Local.h (original)
+++ llvm/trunk/include/llvm/Transforms/Utils/Local.h Wed Dec  3 13:44:02 2008
@@ -56,7 +56,7 @@
                                   SmallVectorImpl<Instruction*> *DeadInst = 0);
     
 //===----------------------------------------------------------------------===//
-//  Control Flow Graph Restructuring...
+//  Control Flow Graph Restructuring.
 //
 
 /// MergeBasicBlockIntoOnlyPred - BB is a block with one predecessor and its

Modified: llvm/trunk/lib/Transforms/Scalar/CondPropagate.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/CondPropagate.cpp?rev=60501&r1=60500&r2=60501&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/CondPropagate.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/CondPropagate.cpp Wed Dec  3 13:44:02 2008
@@ -14,12 +14,13 @@
 
 #define DEBUG_TYPE "condprop"
 #include "llvm/Transforms/Scalar.h"
-#include "llvm/Transforms/Utils/Local.h"
 #include "llvm/Constants.h"
 #include "llvm/Function.h"
 #include "llvm/Instructions.h"
 #include "llvm/Pass.h"
 #include "llvm/Type.h"
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"
+#include "llvm/Transforms/Utils/Local.h"
 #include "llvm/ADT/STLExtras.h"
 #include "llvm/ADT/Statistic.h"
 #include "llvm/Support/Compiler.h"
@@ -95,13 +96,9 @@
         BB != BI->getSuccessor(0)) {
       BasicBlock *Succ = BI->getSuccessor(0);
       
-      // If Succ has any PHI nodes, they are all single-entry PHI's.
-      while (PHINode *PN = dyn_cast<PHINode>(Succ->begin())) {
-        assert(PN->getNumIncomingValues() == 1 &&
-               "PHI doesn't match parent block");
-        PN->replaceAllUsesWith(PN->getIncomingValue(0));
-        PN->eraseFromParent();
-      }
+      // If Succ has any PHI nodes, they are all single-entry PHI's.  Eliminate
+      // them.
+      FoldSingleEntryPHINodes(Succ);
       
       // Remove BI.
       BI->eraseFromParent();
@@ -205,11 +202,7 @@
   // OldSucc had multiple successors. If ToBB has multiple predecessors, then 
   // the edge between them would be critical, which we already took care of.
   // If ToBB has single operand PHI node then take care of it here.
-  while (PHINode *PN = dyn_cast<PHINode>(ToBB->begin())) {
-    assert(PN->getNumIncomingValues() == 1 && "Critical Edge Found!");    
-    PN->replaceAllUsesWith(PN->getIncomingValue(0));
-    PN->eraseFromParent();
-  }
+  FoldSingleEntryPHINodes(ToBB);
 
   // Update PHI nodes in OldSucc to know that FromBB no longer branches to it.
   OldSucc->removePredecessor(FromBB);

Modified: llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp?rev=60501&r1=60500&r2=60501&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/BasicBlockUtils.cpp Wed Dec  3 13:44:02 2008
@@ -54,6 +54,24 @@
   BB->eraseFromParent();
 }
 
+/// FoldSingleEntryPHINodes - We know that BB has one predecessor.  If there are
+/// any single-entry PHI nodes in it, fold them away.  This handles the case
+/// when all entries to the PHI nodes in a block are guaranteed equal, such as
+/// when the block has exactly one predecessor.
+void llvm::FoldSingleEntryPHINodes(BasicBlock *BB) {
+  if (!isa<PHINode>(BB->begin()))
+    return;
+  
+  while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
+    if (PN->getIncomingValue(0) != PN)
+      PN->replaceAllUsesWith(PN->getIncomingValue(0));
+    else
+      PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
+    PN->eraseFromParent();
+  }
+}
+
+
 /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
 /// if possible.  The return value indicates success or failure.
 bool llvm::MergeBlockIntoPredecessor(BasicBlock* BB, Pass* P) {

Modified: llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp?rev=60501&r1=60500&r2=60501&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/SimplifyCFG.cpp Wed Dec  3 13:44:02 2008
@@ -1101,10 +1101,7 @@
   
   // Degenerate case of a single entry PHI.
   if (PN->getNumIncomingValues() == 1) {
-    if (PN->getIncomingValue(0) != PN)
-      PN->replaceAllUsesWith(PN->getIncomingValue(0));
-    else
-      PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
+    FoldSingleEntryPHINodes(PN->getParent());
     PN->eraseFromParent();
     return true;    
   }

Modified: llvm/trunk/lib/Transforms/Utils/UnrollLoop.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Utils/UnrollLoop.cpp?rev=60501&r1=60500&r2=60501&view=diff

==============================================================================
--- llvm/trunk/lib/Transforms/Utils/UnrollLoop.cpp (original)
+++ llvm/trunk/lib/Transforms/Utils/UnrollLoop.cpp Wed Dec  3 13:44:02 2008
@@ -25,13 +25,14 @@
 #include "llvm/Analysis/ConstantFolding.h"
 #include "llvm/Analysis/LoopPass.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/Transforms/Utils/Cloning.h"
 #include "llvm/Transforms/Utils/Local.h"
 #include <cstdio>
 
 using namespace llvm;
 
-/* TODO: Should these be here or in LoopUnroll? */
+// TODO: Should these be here or in LoopUnroll?
 STATISTIC(NumCompletelyUnrolled, "Number of loops completely unrolled");
 STATISTIC(NumUnrolled,    "Number of loops unrolled (completely or otherwise)");
 
@@ -68,11 +69,7 @@
   // multiple duplicate (but guaranteed to be equal) entries for the
   // incoming edges.  This occurs when there are multiple edges from
   // OnlyPred to OnlySucc.
-  //
-  while (PHINode *PN = dyn_cast<PHINode>(&BB->front())) {
-    PN->replaceAllUsesWith(PN->getIncomingValue(0));
-    BB->getInstList().pop_front();  // Delete the phi node...
-  }
+  FoldSingleEntryPHINodes(BB);
 
   // Delete the unconditional branch from the predecessor...
   OnlyPred->getInstList().pop_back();





More information about the llvm-commits mailing list