Implement support for predecessor iterators on basic blocks, use them to print
out predecessor information in the asmprinter.
PiperOrigin-RevId: 206343174
diff --git a/lib/IR/BasicBlock.cpp b/lib/IR/BasicBlock.cpp
index 7cb6440..11201d5 100644
--- a/lib/IR/BasicBlock.cpp
+++ b/lib/IR/BasicBlock.cpp
@@ -71,6 +71,26 @@
inst->block = this;
}
+/// Return true if this block has no predecessors.
+bool BasicBlock::hasNoPredecessors() const {
+ return pred_begin() == pred_end();
+}
+
+/// If this basic block has exactly one predecessor, return it. Otherwise,
+/// return null.
+///
+/// Note that multiple edges from a single block (e.g. if you have a cond
+/// branch with the same block as the true/false destinations) is not
+/// considered to be a single predecessor.
+BasicBlock *BasicBlock::getSinglePredecessor() {
+ auto it = pred_begin();
+ if (it == pred_end())
+ return nullptr;
+ auto *firstPred = *it;
+ ++it;
+ return it == pred_end() ? firstPred : nullptr;
+}
+
//===----------------------------------------------------------------------===//
// ilist_traits for BasicBlock
//===----------------------------------------------------------------------===//