[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:52:08 PDT 2026


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

isPotentiallyReachable() currently returns "reachable" 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.

>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] [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) {



More information about the llvm-commits mailing list