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

via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 27 01:52:48 PDT 2026


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Nikita Popov (nikic)

<details>
<summary>Changes</summary>

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.

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


1 Files Affected:

- (modified) llvm/lib/Analysis/CFG.cpp (+9) 


``````````diff
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) {

``````````

</details>


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


More information about the llvm-commits mailing list