[llvm-branch-commits] [llvm] release/21.x: [SCEV] Don't perform implication checks with many predicates (#158652) (PR #158798)

via llvm-branch-commits llvm-branch-commits at lists.llvm.org
Tue Sep 23 12:22:37 PDT 2025


https://github.com/llvmbot updated https://github.com/llvm/llvm-project/pull/158798

>From b708aea0bc7127adf4ec643660699c8bcdde1273 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 16 Sep 2025 09:24:02 +0200
Subject: [PATCH] [SCEV] Don't perform implication checks with many predicates
 (#158652)

When adding a new predicate to a union, we currently do a bidirectional
implication for all the contained predicates. This means that the number
of implication checks is quadratic in the number of total predicates (if
they don't end up being eliminated).

Fix this by not checking for implication if the number of predicates
grows too large. The expectation is that if there is a large number of
predicates, we should be discarding them later anyway, as expanding them
would be too expensive.

Fixes https://github.com/llvm/llvm-project/issues/156114.

(cherry picked from commit 7af659d0f117802627fc47f73ca5dd20439c5d7b)
---
 llvm/lib/Analysis/ScalarEvolution.cpp | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 24adfa346c642..221468a2d1a84 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -15093,15 +15093,20 @@ void SCEVUnionPredicate::add(const SCEVPredicate *N, ScalarEvolution &SE) {
     return;
   }
 
+  // Implication checks are quadratic in the number of predicates. Stop doing
+  // them if there are many predicates, as they should be too expensive to use
+  // anyway at that point.
+  bool CheckImplies = Preds.size() < 16;
+
   // Only add predicate if it is not already implied by this union predicate.
-  if (implies(N, SE))
+  if (CheckImplies && implies(N, SE))
     return;
 
   // Build a new vector containing the current predicates, except the ones that
   // are implied by the new predicate N.
   SmallVector<const SCEVPredicate *> PrunedPreds;
   for (auto *P : Preds) {
-    if (N->implies(P, SE))
+    if (CheckImplies && N->implies(P, SE))
       continue;
     PrunedPreds.push_back(P);
   }



More information about the llvm-branch-commits mailing list