introduce BasicBlock::getUniquePredecessor()
authorTorok Edwin <edwintorok@gmail.com>
Thu, 11 Dec 2008 10:36:07 +0000 (10:36 +0000)
committerTorok Edwin <edwintorok@gmail.com>
Thu, 11 Dec 2008 10:36:07 +0000 (10:36 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@60872 91177308-0d34-0410-b5e6-96231b3b80d8

include/llvm/BasicBlock.h
lib/VMCore/BasicBlock.cpp

index ff45800f1e561d4780b1f6812a5b4bb41c8096ba..ee5ceb3683a9b2d16852835aaba31e4a0da39d85 100644 (file)
@@ -134,6 +134,16 @@ public:
     return const_cast<BasicBlock*>(this)->getSinglePredecessor();
   }
 
+  /// getUniquePredecessor - If this basic block has a unique predecessor block,
+  /// return the block, otherwise return a null pointer.
+  /// Note that unique predecessor doesn't mean single edge, there can be 
+  /// multiple edges from the unique predecessor to this block (for example in
+  /// case of a switch statement with multiple cases having same destination).
+  BasicBlock *getUniquePredecessor();
+  const BasicBlock *getUniquePredecessor() const {
+    return const_cast<BasicBlock*>(this)->getUniquePredecessor();
+  }
+
   //===--------------------------------------------------------------------===//
   /// Instruction iterator methods
   ///
index 514aa1de237704547b52bbca636482c0c34b69d1..ab2cbebb90ccbeec17878e134cc8c8d3afcd4411 100644 (file)
@@ -169,6 +169,25 @@ BasicBlock *BasicBlock::getSinglePredecessor() {
   return (PI == E) ? ThePred : 0 /*multiple preds*/;
 }
 
+/// getUniquePredecessor - If this basic block has a unique predecessor block,
+/// return the block, otherwise return a null pointer.
+/// Note that unique predecessor doesn't mean single edge, there can be 
+/// multiple edges from the unique predecessor to this block (for example in
+/// case of a switch statement with multiple cases having same destination).
+BasicBlock *BasicBlock::getUniquePredecessor() {
+  pred_iterator PI = pred_begin(this), E = pred_end(this);
+  if (PI == E) return 0; // No preds.
+  BasicBlock *PredBB = *PI;
+  ++PI;
+  for (;PI != E; ++PI) {
+    if (*PI != PredBB)
+      return 0;
+    // same predecessor appears multiple times in predecessor list,
+    // this is ok
+  }
+  return PredBB;
+}
+
 /// removePredecessor - This method is used to notify a BasicBlock that the
 /// specified Predecessor of the block is no longer able to reach it.  This is
 /// actually not used to update the Predecessor list, but is actually used to