[llvm] [SCEV] Prove implied conditions via matching SCEV differences (PR #201839)

Timur Golubovich via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 16 08:36:36 PDT 2026


https://github.com/timurgol007 updated https://github.com/llvm/llvm-project/pull/201839

>From 41f1bd4498bbec137740e383588a6acb4d839ffe Mon Sep 17 00:00:00 2001
From: Timur Golubovich <timur.golubovich at intel.com>
Date: Tue, 9 Jun 2026 12:52:59 +0200
Subject: [PATCH 1/2] [SCEV] Prove implied conditions via matching SCEV
 differences

Add isImpliedCondOperandsViaMatchingDiff to fold equality comparisons
when getMinusSCEV(LHS, RHS) == getMinusSCEV(FoundLHS, FoundRHS).
This handles correlated IV comparisons in loops with multiple pointer
IVs sharing the same stride.
---
 llvm/include/llvm/Analysis/ScalarEvolution.h  | 10 ++++++
 llvm/lib/Analysis/ScalarEvolution.cpp         | 32 +++++++++++++++++
 .../X86/fold-correlated-iv-comparisons.ll}    | 36 +++++++++----------
 3 files changed, 60 insertions(+), 18 deletions(-)
 rename llvm/test/Transforms/{LoopStrengthReduce/X86/lsr-icmpzero-no-inttoptr.ll => IndVarSimplify/X86/fold-correlated-iv-comparisons.ll} (56%)

diff --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index 50af763614a31..0f5906ad39e70 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -2324,6 +2324,16 @@ class ScalarEvolution {
                                      const SCEV *RHS, const SCEV *FoundLHS,
                                      const SCEV *FoundRHS);
 
+  /// Test whether the condition described by Pred, LHS, and RHS is true
+  /// whenever the condition described by Pred, FoundLHS, and FoundRHS is
+  /// true.
+  ///
+  /// This routine tries to analyze if the SCEV differences match.
+  bool isImpliedCondOperandsViaMatchingDiff(CmpPredicate Pred, const SCEV *LHS,
+                                            const SCEV *RHS,
+                                            const SCEV *FoundLHS,
+                                            const SCEV *FoundRHS);
+
   /// If we know that the specified Phi is in the header of its containing
   /// loop, we know the loop executes a constant number of times, and the PHI
   /// node is just a recurrence involving constants, fold it.
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index c0cdce982e623..f84e4f1f6f91b 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -12843,6 +12843,36 @@ bool ScalarEvolution::isImpliedCondOperandsViaShift(CmpPredicate Pred,
   return false;
 }
 
+bool ScalarEvolution::isImpliedCondOperandsViaMatchingDiff(
+    CmpPredicate Pred, const SCEV *LHS, const SCEV *RHS, const SCEV *FoundLHS,
+    const SCEV *FoundRHS) {
+  // Only valid for equality predicates: (A == B) implies (C == D) when
+  // the SCEV difference A - B equals C - D (they check the same
+  // underlying relationship at every iteration).
+  if (!ICmpInst::isEquality(Pred))
+    return false;
+
+  // Restrict to cases involving loop recurrences - that's where this
+  // pattern arises (correlated IV comparisons). This avoids calling
+  // getMinusSCEV on arbitrary non-loop expressions.
+  if (!isa<SCEVAddRecExpr>(LHS) && !isa<SCEVAddRecExpr>(RHS) &&
+      !isa<SCEVAddRecExpr>(FoundLHS) && !isa<SCEVAddRecExpr>(FoundRHS))
+    return false;
+
+  // Compute differences. For pointer-typed operands sharing the same base,
+  // getMinusSCEV strips the common base and returns an integer SCEV.
+  // For example, {base,+,8} - (base+8*n) = {-8n,+,8}
+  const SCEV *FoundDiff = getMinusSCEV(FoundLHS, FoundRHS);
+  if (isa<SCEVCouldNotCompute>(FoundDiff))
+    return false;
+
+  const SCEV *Diff = getMinusSCEV(LHS, RHS);
+  if (isa<SCEVCouldNotCompute>(Diff))
+    return false;
+
+  return Diff == FoundDiff;
+}
+
 bool ScalarEvolution::isImpliedCondOperands(CmpPredicate Pred, const SCEV *LHS,
                                             const SCEV *RHS,
                                             const SCEV *FoundLHS,
@@ -12855,6 +12885,8 @@ bool ScalarEvolution::isImpliedCondOperands(CmpPredicate Pred, const SCEV *LHS,
          isImpliedCondOperandsViaShift(Pred, LHS, RHS, FoundLHS, FoundRHS) ||
          isImpliedCondOperandsViaAddRecStart(Pred, LHS, RHS, FoundLHS, FoundRHS,
                                              CtxI) ||
+         isImpliedCondOperandsViaMatchingDiff(Pred, LHS, RHS, FoundLHS,
+                                              FoundRHS) ||
          isImpliedCondOperandsHelper(Pred, LHS, RHS, FoundLHS, FoundRHS);
 }
 
diff --git a/llvm/test/Transforms/LoopStrengthReduce/X86/lsr-icmpzero-no-inttoptr.ll b/llvm/test/Transforms/IndVarSimplify/X86/fold-correlated-iv-comparisons.ll
similarity index 56%
rename from llvm/test/Transforms/LoopStrengthReduce/X86/lsr-icmpzero-no-inttoptr.ll
rename to llvm/test/Transforms/IndVarSimplify/X86/fold-correlated-iv-comparisons.ll
index 0d6a374f86692..8a974ca670cc8 100644
--- a/llvm/test/Transforms/LoopStrengthReduce/X86/lsr-icmpzero-no-inttoptr.ll
+++ b/llvm/test/Transforms/IndVarSimplify/X86/fold-correlated-iv-comparisons.ll
@@ -1,31 +1,29 @@
 ; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
-; RUN: opt -passes=loop-reduce -S %s | FileCheck %s
+; RUN: opt -passes=indvars -S %s | FileCheck %s
 ;
-; Verify that LSR keeps ICmpZero comparisons in the integer domain when the
-; original operands are pointers, avoiding inttoptr casts entirely. The loop
-; has three pointer IVs sharing the same stride; LSR should produce a single
-; integer IV and compare it directly against the precomputed bound.
-; Target-dependent because TTI must report that scaled addressing is legal.
+; Verify that IndVarSimplify folds correlated IV comparisons. When multiple
+; pointer IVs share the same stride and their bounds are equidistant from their
+; bases, a dominating equality check (p2 == bound2) implies the same for the
+; other IVs (p1 == bound1, p3 == bound3). The redundant icmp ne instructions
+; in the dominated block should be folded to false.
+; Target-dependent because SCEV pointer handling depends on the data layout.
 
 target triple = "x86_64-unknown-linux-gnu"
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
 
-define void @test_no_inttoptr_casts(ptr %base1, ptr %base2, ptr %base3, i64 %n) {
-; CHECK-LABEL: @test_no_inttoptr_casts(
+define void @fold_correlated_iv_comparisons(ptr %base1, ptr %base2, ptr %base3, i64 %n) {
+; CHECK-LABEL: @fold_correlated_iv_comparisons(
 ; CHECK-NEXT:  entry:
-; CHECK-NEXT:    [[TMP0:%.*]] = shl i64 [[N:%.*]], 3
+; CHECK-NEXT:    [[BOUND2:%.*]] = getelementptr i64, ptr [[BASE2:%.*]], i64 [[N:%.*]]
 ; CHECK-NEXT:    br label [[LOOP:%.*]]
 ; CHECK:       loop:
-; CHECK-NEXT:    [[LSR_IV:%.*]] = phi i64 [ [[LSR_IV_NEXT:%.*]], [[LOOP_LATCH:%.*]] ], [ 0, [[ENTRY:%.*]] ]
-; CHECK-NEXT:    [[SCEVGEP:%.*]] = getelementptr i8, ptr [[BASE1:%.*]], i64 [[LSR_IV]]
-; CHECK-NEXT:    [[SCEVGEP1:%.*]] = getelementptr i8, ptr [[BASE2:%.*]], i64 [[LSR_IV]]
-; CHECK-NEXT:    [[SCEVGEP2:%.*]] = getelementptr i8, ptr [[BASE3:%.*]], i64 [[LSR_IV]]
-; CHECK-NEXT:    [[CMP_EXIT:%.*]] = icmp eq i64 [[TMP0]], [[LSR_IV]]
+; CHECK-NEXT:    [[SCEVGEP:%.*]] = phi ptr [ [[BASE1:%.*]], [[ENTRY:%.*]] ], [ [[P1_NEXT:%.*]], [[LOOP_LATCH:%.*]] ]
+; CHECK-NEXT:    [[SCEVGEP1:%.*]] = phi ptr [ [[BASE2]], [[ENTRY]] ], [ [[P2_NEXT:%.*]], [[LOOP_LATCH]] ]
+; CHECK-NEXT:    [[SCEVGEP2:%.*]] = phi ptr [ [[BASE3:%.*]], [[ENTRY]] ], [ [[P3_NEXT:%.*]], [[LOOP_LATCH]] ]
+; CHECK-NEXT:    [[CMP_EXIT:%.*]] = icmp eq ptr [[SCEVGEP1]], [[BOUND2]]
 ; CHECK-NEXT:    br i1 [[CMP_EXIT]], label [[CHECK:%.*]], label [[BODY:%.*]]
 ; CHECK:       check:
-; CHECK-NEXT:    [[CMP1:%.*]] = icmp ne i64 [[TMP0]], [[LSR_IV]]
-; CHECK-NEXT:    [[CMP3:%.*]] = icmp ne i64 [[TMP0]], [[LSR_IV]]
-; CHECK-NEXT:    [[SEL:%.*]] = select i1 [[CMP1]], i1 true, i1 [[CMP3]]
+; CHECK-NEXT:    [[SEL:%.*]] = select i1 false, i1 true, i1 false
 ; CHECK-NEXT:    br i1 [[SEL]], label [[BODY]], label [[EXIT:%.*]]
 ; CHECK:       body:
 ; CHECK-NEXT:    [[V1:%.*]] = load i64, ptr [[SCEVGEP]], align 8
@@ -34,7 +32,9 @@ define void @test_no_inttoptr_casts(ptr %base1, ptr %base2, ptr %base3, i64 %n)
 ; CHECK-NEXT:    store i64 [[SUM]], ptr [[SCEVGEP2]], align 8
 ; CHECK-NEXT:    br label [[LOOP_LATCH]]
 ; CHECK:       loop.latch:
-; CHECK-NEXT:    [[LSR_IV_NEXT]] = add i64 [[LSR_IV]], 8
+; CHECK-NEXT:    [[P1_NEXT]] = getelementptr inbounds i8, ptr [[SCEVGEP]], i64 8
+; CHECK-NEXT:    [[P2_NEXT]] = getelementptr inbounds i8, ptr [[SCEVGEP1]], i64 8
+; CHECK-NEXT:    [[P3_NEXT]] = getelementptr inbounds i8, ptr [[SCEVGEP2]], i64 8
 ; CHECK-NEXT:    br label [[LOOP]]
 ; CHECK:       exit:
 ; CHECK-NEXT:    ret void

>From b87a854e75e2735f85ce511819aadcde1c155c79 Mon Sep 17 00:00:00 2001
From: Timur Golubovich <timur.golubovich at intel.com>
Date: Tue, 16 Jun 2026 17:34:17 +0200
Subject: [PATCH 2/2] improved condition, thanks @nikic for remark

---
 llvm/lib/Analysis/ScalarEvolution.cpp | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index f84e4f1f6f91b..3d67fb71addb6 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -12855,8 +12855,8 @@ bool ScalarEvolution::isImpliedCondOperandsViaMatchingDiff(
   // Restrict to cases involving loop recurrences - that's where this
   // pattern arises (correlated IV comparisons). This avoids calling
   // getMinusSCEV on arbitrary non-loop expressions.
-  if (!isa<SCEVAddRecExpr>(LHS) && !isa<SCEVAddRecExpr>(RHS) &&
-      !isa<SCEVAddRecExpr>(FoundLHS) && !isa<SCEVAddRecExpr>(FoundRHS))
+  if ((!isa<SCEVAddRecExpr>(LHS) && !isa<SCEVAddRecExpr>(RHS)) ||
+      (!isa<SCEVAddRecExpr>(FoundLHS) && !isa<SCEVAddRecExpr>(FoundRHS)))
     return false;
 
   // Compute differences. For pointer-typed operands sharing the same base,



More information about the llvm-commits mailing list