[llvm] [LoopSafety] Cache whether block is guaranteed to execute (NFC) (PR #217943)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 21 11:25:30 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-analysis

Author: Nikita Popov (nikic)

<details>
<summary>Changes</summary>

To avoid doing the CFG walk multiple times if instructions from the same block are queried.

This is for an upcoming change that does more isGuaranteedToExecute() queries.

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


2 Files Affected:

- (modified) llvm/include/llvm/Analysis/MustExecute.h (+8) 
- (modified) llvm/lib/Analysis/MustExecute.cpp (+8) 


``````````diff
diff --git a/llvm/include/llvm/Analysis/MustExecute.h b/llvm/include/llvm/Analysis/MustExecute.h
index c5af3b72c8d05..af78d8e5e982d 100644
--- a/llvm/include/llvm/Analysis/MustExecute.h
+++ b/llvm/include/llvm/Analysis/MustExecute.h
@@ -61,6 +61,10 @@ class LoopSafetyInfo {
   // Used to update funclet bundle operands.
   DenseMap<BasicBlock *, ColorVector> BlockColors;
 
+  // Cache whether (the start of) this block is guaranteed to execute if the
+  // loop is entered.
+  mutable DenseMap<const BasicBlock *, bool> GuaranteedToExecute;
+
 protected:
   /// Computes block colors.
   LLVM_ABI void computeBlockColors(const Loop *CurLoop);
@@ -86,6 +90,10 @@ class LoopSafetyInfo {
                                         const BasicBlock *BB,
                                         const DominatorTree *DT) const;
 
+  LLVM_ABI bool allLoopPathsLeadToBlockImpl(const Loop *CurLoop,
+                                            const BasicBlock *BB,
+                                            const DominatorTree *DT) const;
+
   /// Computes safety information for a loop checks loop body & header for
   /// the possibility of may throw exception, it takes LoopSafetyInfo and loop
   /// as argument. Updates safety information in LoopSafetyInfo argument.
diff --git a/llvm/lib/Analysis/MustExecute.cpp b/llvm/lib/Analysis/MustExecute.cpp
index 5d0eae6b258ee..cbdeaf8d990d8 100644
--- a/llvm/lib/Analysis/MustExecute.cpp
+++ b/llvm/lib/Analysis/MustExecute.cpp
@@ -204,6 +204,14 @@ bool LoopSafetyInfo::allLoopPathsLeadToBlock(const Loop *CurLoop,
   if (BB == CurLoop->getHeader())
     return true;
 
+  auto [It, Inserted] = GuaranteedToExecute.try_emplace(BB, false);
+  if (Inserted)
+    It->second = allLoopPathsLeadToBlockImpl(CurLoop, BB, DT);
+  return It->second;
+}
+
+bool LoopSafetyInfo::allLoopPathsLeadToBlockImpl(
+    const Loop *CurLoop, const BasicBlock *BB, const DominatorTree *DT) const {
   // Collect all transitive predecessors of BB in the same loop. This set will
   // be a subset of the blocks within the loop.
   SmallPtrSet<const BasicBlock *, 4> Predecessors;

``````````

</details>


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


More information about the llvm-commits mailing list