[llvm-commits] CVS: llvm/lib/Transforms/Utils/SimplifyCFG.cpp

Chris Lattner lattner at cs.uiuc.edu
Tue Aug 2 17:19:56 PDT 2005



Changes in directory llvm/lib/Transforms/Utils:

SimplifyCFG.cpp updated: 1.76 -> 1.77
---
Log message:

move two functions up in the file, use SafeToMergeTerminators to eliminate
some duplicated code


---
Diffs of the changes:  (+45 -61)

 SimplifyCFG.cpp |  106 +++++++++++++++++++++++---------------------------------
 1 files changed, 45 insertions(+), 61 deletions(-)


Index: llvm/lib/Transforms/Utils/SimplifyCFG.cpp
diff -u llvm/lib/Transforms/Utils/SimplifyCFG.cpp:1.76 llvm/lib/Transforms/Utils/SimplifyCFG.cpp:1.77
--- llvm/lib/Transforms/Utils/SimplifyCFG.cpp:1.76	Tue Aug  2 19:11:16 2005
+++ llvm/lib/Transforms/Utils/SimplifyCFG.cpp	Tue Aug  2 19:19:45 2005
@@ -24,6 +24,49 @@
 #include <map>
 using namespace llvm;
 
+/// SafeToMergeTerminators - Return true if it is safe to merge these two
+/// terminator instructions together.
+///
+static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) {
+  if (SI1 == SI2) return false;  // Can't merge with self!
+  
+  // It is not safe to merge these two switch instructions if they have a common
+  // successor, and if that successor has a PHI node, and if *that* PHI node has
+  // conflicting incoming values from the two switch blocks.
+  BasicBlock *SI1BB = SI1->getParent();
+  BasicBlock *SI2BB = SI2->getParent();
+  std::set<BasicBlock*> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
+  
+  for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
+    if (SI1Succs.count(*I))
+      for (BasicBlock::iterator BBI = (*I)->begin();
+           isa<PHINode>(BBI); ++BBI) {
+        PHINode *PN = cast<PHINode>(BBI);
+        if (PN->getIncomingValueForBlock(SI1BB) !=
+            PN->getIncomingValueForBlock(SI2BB))
+          return false;
+      }
+        
+  return true;
+}
+
+/// AddPredecessorToBlock - Update PHI nodes in Succ to indicate that there will
+/// now be entries in it from the 'NewPred' block.  The values that will be
+/// flowing into the PHI nodes will be the same as those coming in from
+/// ExistPred, an existing predecessor of Succ.
+static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
+                                  BasicBlock *ExistPred) {
+  assert(std::find(succ_begin(ExistPred), succ_end(ExistPred), Succ) !=
+         succ_end(ExistPred) && "ExistPred is not a predecessor of Succ!");
+  if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do
+  
+  for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
+    PHINode *PN = cast<PHINode>(I);
+    Value *V = PN->getIncomingValueForBlock(ExistPred);
+    PN->addIncoming(V, NewPred);
+  }
+}
+
 // PropagatePredecessorsForPHIs - This gets "Succ" ready to have the
 // predecessors from "BB".  This is a little tricky because "Succ" has PHI
 // nodes, which need to have extra slots added to them to hold the merge edges
@@ -48,24 +91,8 @@
   // Succ.  If so, we cannot do the transformation if there are any PHI nodes
   // with incompatible values coming in from the two edges!
   //
-  for (pred_iterator PI = pred_begin(Succ), PE = pred_end(Succ); PI != PE; ++PI)
-    if (std::find(BBPreds.begin(), BBPreds.end(), *PI) != BBPreds.end()) {
-      // Loop over all of the PHI nodes checking to see if there are
-      // incompatible values coming in.
-      for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
-        PHINode *PN = cast<PHINode>(I);
-        // Loop up the entries in the PHI node for BB and for *PI if the values
-        // coming in are non-equal, we cannot merge these two blocks (instead we
-        // should insert a conditional move or something, then merge the
-        // blocks).
-        int Idx1 = PN->getBasicBlockIndex(BB);
-        int Idx2 = PN->getBasicBlockIndex(*PI);
-        assert(Idx1 != -1 && Idx2 != -1 &&
-               "Didn't have entries for my predecessors??");
-        if (PN->getIncomingValue(Idx1) != PN->getIncomingValue(Idx2))
-          return true;  // Values are not equal...
-      }
-    }
+  if (!SafeToMergeTerminators(BB->getTerminator(), Succ->getTerminator()))
+    return true;   // Cannot merge.
 
   // Loop over all of the PHI nodes in the successor BB.
   for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
@@ -398,49 +425,6 @@
   }
 }
 
-/// SafeToMergeTerminators - Return true if it is safe to merge these two
-/// terminator instructions together.
-///
-static bool SafeToMergeTerminators(TerminatorInst *SI1, TerminatorInst *SI2) {
-  if (SI1 == SI2) return false;  // Can't merge with self!
-
-  // It is not safe to merge these two switch instructions if they have a common
-  // successor, and if that successor has a PHI node, and if *that* PHI node has
-  // conflicting incoming values from the two switch blocks.
-  BasicBlock *SI1BB = SI1->getParent();
-  BasicBlock *SI2BB = SI2->getParent();
-  std::set<BasicBlock*> SI1Succs(succ_begin(SI1BB), succ_end(SI1BB));
-
-  for (succ_iterator I = succ_begin(SI2BB), E = succ_end(SI2BB); I != E; ++I)
-    if (SI1Succs.count(*I))
-      for (BasicBlock::iterator BBI = (*I)->begin();
-           isa<PHINode>(BBI); ++BBI) {
-        PHINode *PN = cast<PHINode>(BBI);
-        if (PN->getIncomingValueForBlock(SI1BB) !=
-            PN->getIncomingValueForBlock(SI2BB))
-          return false;
-      }
-
-  return true;
-}
-
-/// AddPredecessorToBlock - Update PHI nodes in Succ to indicate that there will
-/// now be entries in it from the 'NewPred' block.  The values that will be
-/// flowing into the PHI nodes will be the same as those coming in from
-/// ExistPred, an existing predecessor of Succ.
-static void AddPredecessorToBlock(BasicBlock *Succ, BasicBlock *NewPred,
-                                  BasicBlock *ExistPred) {
-  assert(std::find(succ_begin(ExistPred), succ_end(ExistPred), Succ) !=
-         succ_end(ExistPred) && "ExistPred is not a predecessor of Succ!");
-  if (!isa<PHINode>(Succ->begin())) return; // Quick exit if nothing to do
-
-  for (BasicBlock::iterator I = Succ->begin(); isa<PHINode>(I); ++I) {
-    PHINode *PN = cast<PHINode>(I);
-    Value *V = PN->getIncomingValueForBlock(ExistPred);
-    PN->addIncoming(V, NewPred);
-  }
-}
-
 // isValueEqualityComparison - Return true if the specified terminator checks to
 // see if a value is equal to constant integer value.
 static Value *isValueEqualityComparison(TerminatorInst *TI) {






More information about the llvm-commits mailing list