[llvm] [ADT] Align `GenericCycle::getExitBlocks`/`getExitingBlocks` methods with LoopBase (PR #189916)

via llvm-commits llvm-commits at lists.llvm.org
Wed Apr 1 01:38:19 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-adt

Author: Antonio Frighetto (antoniofrighetto)

<details>
<summary>Changes</summary>

Ensure `GenericCycle::getExitBlocks` does not replace the content of the output vector and behaves as `LoopBase::getExitBlocks`, previously exposing an issue within `isPotentiallyReachable`.

Fixes: https://github.com/llvm/llvm-project/issues/189800.
Fixes: https://github.com/llvm/llvm-project/issues/189289.
Fixes: https://github.com/llvm/llvm-project/issues/189234.
Fixes: https://github.com/llvm/llvm-project/issues/189180.

---
Full diff: https://github.com/llvm/llvm-project/pull/189916.diff


2 Files Affected:

- (modified) llvm/include/llvm/ADT/GenericCycleImpl.h (+12-13) 
- (modified) llvm/unittests/Analysis/CFGTest.cpp (+20) 


``````````diff
diff --git a/llvm/include/llvm/ADT/GenericCycleImpl.h b/llvm/include/llvm/ADT/GenericCycleImpl.h
index 1a28fb1cef921..cccbfa9412a25 100644
--- a/llvm/include/llvm/ADT/GenericCycleImpl.h
+++ b/llvm/include/llvm/ADT/GenericCycleImpl.h
@@ -48,36 +48,35 @@ template <typename ContextT>
 void GenericCycle<ContextT>::getExitBlocks(
     SmallVectorImpl<BlockT *> &TmpStorage) const {
   if (!ExitBlocksCache.empty()) {
-    TmpStorage = ExitBlocksCache;
+    TmpStorage.append(ExitBlocksCache.begin(), ExitBlocksCache.end());
     return;
   }
 
-  TmpStorage.clear();
-
+  SmallVector<BlockT *, 8> ExitBlocks;
   size_t NumExitBlocks = 0;
   for (BlockT *Block : blocks()) {
-    llvm::append_range(TmpStorage, successors(Block));
+    llvm::append_range(ExitBlocks, successors(Block));
 
-    for (size_t Idx = NumExitBlocks, End = TmpStorage.size(); Idx < End;
+    for (size_t Idx = NumExitBlocks, End = ExitBlocks.size(); Idx < End;
          ++Idx) {
-      BlockT *Succ = TmpStorage[Idx];
+      BlockT *Succ = ExitBlocks[Idx];
       if (!contains(Succ)) {
-        auto ExitEndIt = TmpStorage.begin() + NumExitBlocks;
-        if (std::find(TmpStorage.begin(), ExitEndIt, Succ) == ExitEndIt)
-          TmpStorage[NumExitBlocks++] = Succ;
+        auto ExitEndIt = ExitBlocks.begin() + NumExitBlocks;
+        if (std::find(ExitBlocks.begin(), ExitEndIt, Succ) == ExitEndIt)
+          ExitBlocks[NumExitBlocks++] = Succ;
       }
     }
 
-    TmpStorage.resize(NumExitBlocks);
+    ExitBlocks.resize(NumExitBlocks);
   }
-  ExitBlocksCache.append(TmpStorage.begin(), TmpStorage.end());
+
+  ExitBlocksCache.append(ExitBlocks.begin(), ExitBlocks.end());
+  TmpStorage.append(ExitBlocks.begin(), ExitBlocks.end());
 }
 
 template <typename ContextT>
 void GenericCycle<ContextT>::getExitingBlocks(
     SmallVectorImpl<BlockT *> &TmpStorage) const {
-  TmpStorage.clear();
-
   for (BlockT *Block : blocks()) {
     for (BlockT *Succ : successors(Block)) {
       if (!contains(Succ)) {
diff --git a/llvm/unittests/Analysis/CFGTest.cpp b/llvm/unittests/Analysis/CFGTest.cpp
index a9938b381f9db..d4ac8ecfdd3c8 100644
--- a/llvm/unittests/Analysis/CFGTest.cpp
+++ b/llvm/unittests/Analysis/CFGTest.cpp
@@ -517,3 +517,23 @@ TEST_F(IsPotentiallyReachableTest, UnreachableToReachable) {
                 "}");
   ExpectPath(true);
 }
+
+TEST_F(IsPotentiallyReachableTest, CycleExitBlocksSelfLoop) {
+  ParseAssembly("define void @test() {\n"
+                "entry:\n"
+                "  br label %loop.header\n"
+                "loop.header:\n"
+                "  %A = bitcast i32 0 to i32\n"
+                "  br i1 undef, label %loop.latch, label %loop.exit\n"
+                "loop.latch:\n"
+                "  br label %loop.header\n"
+                "loop.exit:\n"
+                "  br i1 undef, label %exit, label %loop.body\n"
+                "loop.body:\n"
+                "  br label %loop.body\n"
+                "exit:\n"
+                "  %B = bitcast i32 0 to i32\n"
+                "  ret void\n"
+                "}");
+  ExpectPath(true);
+}

``````````

</details>


https://github.com/llvm/llvm-project/pull/189916


More information about the llvm-commits mailing list