[llvm] [CFG] Add shortcut if CycleInfo is available (PR #188928)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 27 01:59:25 PDT 2026


https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/188928

>From 7124b8073336b0317143310c476ade6ea4460db0 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Fri, 20 Mar 2026 15:38:50 +0100
Subject: [PATCH 1/2] [CFG] Add shortcut if CycleInfo is available

isPotentiallyReachable() currently return true early if BB dominates
StopBB. If CycleInfo is available, and BB is not part of a cycle,
we can also perform the reverse inference: Return not reachable if
StopBB dominates BB.

This both allows aborting the walk earlier, and provides a more
precise result.
---
 llvm/lib/Analysis/CFG.cpp | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index e466ab64ef6c9..9c0ce85f63b1d 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -235,10 +235,19 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
     const Cycle *OuterC = nullptr;
     if (CI) {
       OuterC = CI->getTopLevelParentCycle(BB);
+      bool InCycle = OuterC != nullptr;
       if (CyclesWithHoles.count(OuterC))
         OuterC = nullptr;
       else if (StopCycles.contains(OuterC))
         return true;
+      // If BB is not part of a cycle, then it can't reach any block that
+      // dominates it. An exception is if the block is unreachable, as all
+      // reachable blocks dominate an unreachable block.
+      if (!InCycle && DT &&
+          llvm::all_of(StopSet, [&](const BasicBlock *StopBB) {
+            return DT->isReachableFromEntry(BB) && DT->dominates(StopBB, BB);
+          }))
+        continue;
     }
 
     if (!--Limit) {

>From e522dce55fad5ffef739f0b10e5b0b38b3e99b01 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Fri, 27 Mar 2026 09:58:34 +0100
Subject: [PATCH 2/2] Move check out of loop

---
 llvm/lib/Analysis/CFG.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 9c0ce85f63b1d..cb0e67c5eba39 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -243,9 +243,9 @@ static bool isReachableImpl(SmallVectorImpl<BasicBlock *> &Worklist,
       // If BB is not part of a cycle, then it can't reach any block that
       // dominates it. An exception is if the block is unreachable, as all
       // reachable blocks dominate an unreachable block.
-      if (!InCycle && DT &&
+      if (!InCycle && DT && DT->isReachableFromEntry(BB) &&
           llvm::all_of(StopSet, [&](const BasicBlock *StopBB) {
-            return DT->isReachableFromEntry(BB) && DT->dominates(StopBB, BB);
+            return DT->dominates(StopBB, BB);
           }))
         continue;
     }



More information about the llvm-commits mailing list