[llvm-branch-commits] [llvm] [SCEV] Drop samesign in getLoopInvariantPredicate (PR #236093)
Dmitry Makogon via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Oct 27 05:36:06 PDT 2026
https://github.com/d-makogon updated https://github.com/llvm/llvm-project/pull/236093
>From cecdd532c97bb2addcfa6c115ea83ed19274a504 Mon Sep 17 00:00:00 2001
From: Dmitrii Makogon <dmakogon at azul.com>
Date: Tue, 27 Oct 2026 10:16:02 +0000
Subject: [PATCH] [SCEV] Drop samesign in getLoopInvariantPredicate
SCEV's getLoopInvariantPredicate tries to replace a comparison of an IV
against a loop-invariant value with the comparison value on loop entry.
For that it tries to prove that the comparison is:
- (A) monotonic: i.e. if c_i is true, then c_{i+1} is true,
- (B) guards the backedge: the backedge is taken only if c_i is true.
Then each c_i may be replaced with c_0.
It works for the icmp instruction without the samesign flag.
With the samesign flag, however, the comparison is poison when its
operands have different signs.
To prove the backedge guard (B), we may rely on picking that poison as
true, while proving the monotonicity (A) may need the same poison
to be picked as false.
E.g. here with start = 127 and inv = -1:
for (i8 iv = start; iv >s inv; iv++ /* nuw */)
use(iv samesign >u inv);
The comparison 'iv samesign >u inv' is poison at iv = 127. Then the
backedge is taken, and the comparison is false at iv = -128.
I.e. c_0 = poison, c_1 = false.
To prove (A), we must pick false for the poison c_0 at iv = 127, because
the comparison c_1 is false at the next iteration.
To prove (B), we must pick true for c_0, because the backedge is taken
after the first iteration.
So the picks are contradictory.
Now drop samesign, so that both facts are proven for the predicate
without it. If we're able to prove it, the samesign predicate is then
true or poison, and therefore can be replaced with true.
This is conservative: it also rejects cases where the picks are not
contradictory.
E.g. with a non-negative inv, the comparison is true before the IV
changes sign and poison after, so picking true satisfies both (A) and
(B).
Fixes #236050.
Assisted-By: Claude
---
llvm/lib/Analysis/ScalarEvolution.cpp | 10 ++++++++++
.../loop-invariant-predicate-samesign.ll | 14 ++++++++------
2 files changed, 18 insertions(+), 6 deletions(-)
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 59264115fe1cd7..b3f28610691b04 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -11579,6 +11579,16 @@ ScalarEvolution::getLoopInvariantPredicate(CmpPredicate Pred, const SCEV *LHS,
if (!ArLHS || ArLHS->getLoop() != L)
return std::nullopt;
+ // A samesign compare is poison when its operands have different signs.
+ // To prove that the comparison holds on the backedge, we may need to pick
+ // that poison as true.
+ // But proving that it is monotonic may need the same poison to be false.
+ // E.g. when the IV changes its sign and the comparison becomes false.
+ // Both facts must hold for the same picked values for poisons. So drop
+ // samesign.
+ // If the predicate without samesign is true, then the samesign predicate
+ // is either true or poison.
+ Pred = Pred.dropSameSign();
auto MonotonicType = getMonotonicPredicateType(ArLHS, Pred);
if (!MonotonicType)
return std::nullopt;
diff --git a/llvm/test/Transforms/IndVarSimplify/loop-invariant-predicate-samesign.ll b/llvm/test/Transforms/IndVarSimplify/loop-invariant-predicate-samesign.ll
index dab1ef61c14a65..29df9d7f1dfe9d 100644
--- a/llvm/test/Transforms/IndVarSimplify/loop-invariant-predicate-samesign.ll
+++ b/llvm/test/Transforms/IndVarSimplify/loop-invariant-predicate-samesign.ll
@@ -10,7 +10,7 @@
declare void @use(i1)
-; FIXME: %cmp must not be replaced with %start samesign >u %inv.
+; %cmp must not be replaced with %start samesign >u %inv.
; E.g. for %start = 127, %inv = -1, %cmp is poison at %iv = 127, and false at
; %iv = -128.
; Monotonicity: needs the poison at %iv = 127 to be picked false, because
@@ -24,7 +24,7 @@ define void @samesign_ugt_signed_latch(i8 %start, i8 %inv) {
; CHECK-NEXT: br label %[[LOOP:.*]]
; CHECK: [[LOOP]]:
; CHECK-NEXT: [[IV:%.*]] = phi i8 [ [[START]], %[[ENTRY]] ], [ [[IV_NEXT:%.*]], %[[LOOP]] ]
-; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ugt i8 [[START]], [[INV]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ugt i8 [[IV]], [[INV]]
; CHECK-NEXT: call void @use(i1 [[CMP]])
; CHECK-NEXT: [[IV_NEXT]] = add nuw i8 [[IV]], 1
; CHECK-NEXT: [[LATCH_COND:%.*]] = icmp sgt i8 [[IV]], [[INV]]
@@ -78,7 +78,7 @@ exit:
ret void
}
-; %cmp can be replaced with %start samesign >u %inv.
+; TODO: %cmp can be replaced with %start samesign >u %inv.
; Backedge guard: %inv >=s 0, so when the latch %iv >s %inv is true, both
; operands are non-negative, and %cmp is %iv >u %inv, which is true.
; Monotonicity: once %iv >u %inv is true, it stays true, because %iv only grows
@@ -95,7 +95,7 @@ define void @samesign_ugt_signed_latch_nonneg_inv(i8 %start, i8 %inv) {
; CHECK-NEXT: br label %[[LOOP:.*]]
; CHECK: [[LOOP]]:
; CHECK-NEXT: [[IV:%.*]] = phi i8 [ [[IV_NEXT:%.*]], %[[LOOP]] ], [ [[START]], %[[LOOP_PREHEADER]] ]
-; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ugt i8 [[START]], [[INV]]
+; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ugt i8 [[IV]], [[INV]]
; CHECK-NEXT: call void @use(i1 [[CMP]])
; CHECK-NEXT: [[IV_NEXT]] = add nuw i8 [[IV]], 1
; CHECK-NEXT: [[LATCH_COND:%.*]] = icmp sgt i8 [[IV]], [[INV]]
@@ -119,7 +119,7 @@ exit:
ret void
}
-; %cmp can be replaced with %start samesign >u %inv.
+; TODO: %cmp can be replaced with %start samesign >u %inv.
; %start >=s 0 and %iv steps with nuw nsw, so %iv stays non-negative.
; %inv <s 0, so the operand signs always differ, and %cmp is poison on
; every iteration, so it is safe to use the value on the first iteration.
@@ -134,8 +134,10 @@ define void @samesign_ugt_signed_latch_iv_keeps_sign(i8 %start, i8 %inv) {
; CHECK: [[LOOP_PREHEADER]]:
; CHECK-NEXT: br label %[[LOOP:.*]]
; CHECK: [[LOOP]]:
-; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ugt i8 [[START]], [[INV]]
+; CHECK-NEXT: [[IV:%.*]] = phi i8 [ [[IV_NEXT:%.*]], %[[LOOP]] ], [ [[START]], %[[LOOP_PREHEADER]] ]
+; CHECK-NEXT: [[CMP:%.*]] = icmp samesign ugt i8 [[IV]], [[INV]]
; CHECK-NEXT: call void @use(i1 [[CMP]])
+; CHECK-NEXT: [[IV_NEXT]] = add nuw nsw i8 [[IV]], 1
; CHECK-NEXT: [[LATCH_COND:%.*]] = icmp sgt i8 [[START]], [[INV]]
; CHECK-NEXT: br i1 [[LATCH_COND]], label %[[LOOP]], label %[[EXIT_LOOPEXIT:.*]]
; CHECK: [[EXIT_LOOPEXIT]]:
More information about the llvm-branch-commits
mailing list