add function isLoopLatch
authorSebastian Pop <sebpop@gmail.com>
Wed, 27 Jul 2016 05:02:15 +0000 (05:02 +0000)
committerSebastian Pop <sebpop@gmail.com>
Wed, 27 Jul 2016 05:02:15 +0000 (05:02 +0000)
Differential Revision: https://reviews.llvm.org/D22817

llvm-svn: 276837

llvm/include/llvm/Analysis/LoopInfo.h

index a7d9ac5..0888530 100644 (file)
@@ -168,6 +168,19 @@ public:
     return false;
   }
 
+  /// Returns true if \p BB is a loop-latch.
+  /// A latch block is a block that contains a branch back to the header.
+  /// This function is useful when there are multiple latches in a loop
+  /// because \fn getLoopLatch will return nullptr in that case.
+  bool isLoopLatch(const BlockT *BB) const {
+    assert(contains(BB) && "block does not belong to the loop");
+
+    BlockT *Header = getHeader();
+    auto PredBegin = GraphTraits<Inverse<BlockT*> >::child_begin(Header);
+    auto PredEnd = GraphTraits<Inverse<BlockT*> >::child_end(Header);
+    return std::find(PredBegin, PredEnd, BB) != PredEnd;
+  }
+
   /// Calculate the number of back edges to the loop header.
   unsigned getNumBackEdges() const {
     unsigned NumBackEdges = 0;