[llvm] [SCEV] Collect loop guards once in getSmallConstantTripMultiple (NFC). (PR #219722)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 31 01:54:35 PDT 2026


================
@@ -8411,23 +8411,29 @@ unsigned ScalarEvolution::getSmallConstantTripMultiple(const Loop *L) {
   SmallVector<BasicBlock *, 8> ExitingBlocks;
   L->getExitingBlocks(ExitingBlocks);
 
-  std::optional<unsigned> Res;
-  for (auto *ExitingBB : ExitingBlocks) {
-    unsigned Multiple = getSmallConstantTripMultiple(L, ExitingBB);
-    if (!Res)
-      Res = Multiple;
-    Res = std::gcd(*Res, Multiple);
-  }
-  return Res.value_or(1);
+  // An exit with an uncomputable exit count makes the result 1.
+  if (ExitingBlocks.empty() ||
+      any_of(ExitingBlocks, [this, L](BasicBlock *ExitingBB) {
+        return isa<SCEVCouldNotCompute>(getExitCount(L, ExitingBB));
+      }))
+    return 1;
+
+  LoopGuards Guards = LoopGuards::collect(L, *this);
+  unsigned Res = 0;
+  for (BasicBlock *ExitingBB : ExitingBlocks)
+    Res = std::gcd(
+        Res, getSmallConstantTripMultiple(getExitCount(L, ExitingBB), Guards));
+  return Res;
 }
----------------
fhahn wrote:

> I don't think this counts as a non-functional change. Because of the introduction of cyclic protection.

I am not sure I follow; we still take effectively the same code paths, just in slightly different order; if there was an issue with cycles before (which I do not think there is AFAICT), I don't think the patch would change it

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


More information about the llvm-commits mailing list