[llvm] [CycleInfo] Deduplicate getExitBlocks with a set. NFC (PR #210557)

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Sat Jul 18 16:08:07 PDT 2026


https://github.com/MaskRay created https://github.com/llvm/llvm-project/pull/210557

Fix the O(exit_blocks^2) hazard.

>From f5af61f9ad4636e08fcd5bcb00f7500b36fd9d21 Mon Sep 17 00:00:00 2001
From: Fangrui Song <i at maskray.me>
Date: Sat, 18 Jul 2026 00:32:36 -0700
Subject: [PATCH] [CycleInfo] Deduplicate getExitBlocks with a set. NFC

Fix the O(exit_blocks^2) hazard.
---
 llvm/include/llvm/ADT/GenericCycleImpl.h | 26 ++++++------------------
 1 file changed, 6 insertions(+), 20 deletions(-)

diff --git a/llvm/include/llvm/ADT/GenericCycleImpl.h b/llvm/include/llvm/ADT/GenericCycleImpl.h
index e2da17af20826..9ebdb5972780d 100644
--- a/llvm/include/llvm/ADT/GenericCycleImpl.h
+++ b/llvm/include/llvm/ADT/GenericCycleImpl.h
@@ -40,27 +40,13 @@ void GenericCycleInfo<ContextT>::getExitBlocks(
   if (ExitBlocksCaches.empty())
     ExitBlocksCaches.resize(NumCycles);
   auto &Cache = ExitBlocksCaches[C.Index];
-  if (!Cache.empty()) {
-    TmpStorage.append(Cache.begin(), Cache.end());
-    return;
-  }
-
-  size_t NumExitBlocks = 0;
-  for (BlockT *Block : getBlocks(C)) {
-    llvm::append_range(Cache, successors(Block));
-
-    for (size_t Idx = NumExitBlocks, End = Cache.size(); Idx < End; ++Idx) {
-      BlockT *Succ = Cache[Idx];
-      if (!contains(C, Succ)) {
-        auto ExitEndIt = Cache.begin() + NumExitBlocks;
-        if (std::find(Cache.begin(), ExitEndIt, Succ) == ExitEndIt)
-          Cache[NumExitBlocks++] = Succ;
-      }
-    }
-
-    Cache.resize(NumExitBlocks);
+  if (Cache.empty()) {
+    SmallPtrSet<BlockT *, 4> Seen;
+    for (BlockT *Block : getBlocks(C))
+      for (BlockT *Succ : successors(Block))
+        if (!contains(C, Succ) && Seen.insert(Succ).second)
+          Cache.push_back(Succ);
   }
-
   TmpStorage.append(Cache.begin(), Cache.end());
 }
 



More information about the llvm-commits mailing list