[llvm] [SCEV] Fix exit condition for recursive loop guard collection (PR #120442)

Julian Nagele via llvm-commits llvm-commits at lists.llvm.org
Wed Dec 18 07:44:07 PST 2024


https://github.com/juliannagele created https://github.com/llvm/llvm-project/pull/120442

When assumptions are present `Terms.size()` does not actually count the number of conditions collected from dominating branches; introduce a separate counter.

>From 057a47aaaf256c3a9d1c8dab442bc03fe6aa5c60 Mon Sep 17 00:00:00 2001
From: Julian Nagele <j.nagele at apple.com>
Date: Wed, 18 Dec 2024 16:30:13 +0100
Subject: [PATCH] [SCEV] Fix exit condition for recursive loop guard collection

When assumptions are present Terms.size() does not actually count the
number of conditions collected from dominating branches; introduce a
separate counter.
---
 llvm/lib/Analysis/ScalarEvolution.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index d55d09020fc147..6ba91c4417f724 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -15753,6 +15753,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
   // predecessors that can be found that have unique successors leading to the
   // original header.
   // TODO: share this logic with isLoopEntryGuardedByCond.
+  unsigned NumCollectedConditions = 0;
   std::pair<const BasicBlock *, const BasicBlock *> Pair(Pred, Block);
   for (; Pair.first;
        Pair = SE.getPredecessorWithUniqueSuccessorForBB(Pair.first)) {
@@ -15767,7 +15768,7 @@ void ScalarEvolution::LoopGuards::collectFromBlock(
 
     // If we are recursively collecting guards stop after 2
     // predecessors to limit compile-time impact for now.
-    if (Depth > 0 && Terms.size() == 2)
+    if (Depth > 0 && ++NumCollectedConditions == 2)
       break;
   }
   // Finally, if we stopped climbing the predecessor chain because



More information about the llvm-commits mailing list