[llvm] [SCEV] Add test coverage for ext-nusw/nsuw inference (PR #218943)
via llvm-commits
llvm-commits at lists.llvm.org
Wed Aug 26 07:58:15 PDT 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-llvm-analysis
Author: Ramkumar Ramachandra (artagnon)
<details>
<summary>Changes</summary>
Complete the existing incorrect-nsw test, showing that zext-addrec nusw is not implied by nw on the pre-inc AR, and that sext-addrec nsuw is not implied by nw on the pre-inc AR. See also: #<!-- -->217405 and #<!-- -->217362.
Assisted-by: AI
---
Full diff: https://github.com/llvm/llvm-project/pull/218943.diff
2 Files Affected:
- (added) llvm/test/Analysis/ScalarEvolution/ext-addrec-wrap-flags.ll (+174)
- (removed) llvm/test/Analysis/ScalarEvolution/incorrect-nsw.ll (-26)
``````````diff
diff --git a/llvm/test/Analysis/ScalarEvolution/ext-addrec-wrap-flags.ll b/llvm/test/Analysis/ScalarEvolution/ext-addrec-wrap-flags.ll
new file mode 100644
index 0000000000000..9e1e1387eead5
--- /dev/null
+++ b/llvm/test/Analysis/ScalarEvolution/ext-addrec-wrap-flags.ll
@@ -0,0 +1,174 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py UTC_ARGS: --version 6
+; RUN: opt -disable-output "-passes=print<scalar-evolution>" %s 2>&1 | FileCheck %s
+
+; The sext expression should be {-1,+,-128}<nw>.
+; nw should be inferred correctly.
+define void @sext.nw.pre.inc() {
+; CHECK-LABEL: 'sext.nw.pre.inc'
+; CHECK-NEXT: Classifying expressions for: @sext.nw.pre.inc
+; CHECK-NEXT: %i = phi i8 [ -1, %entry ], [ %i.inc, %loop ]
+; CHECK-NEXT: --> {-1,+,-128}<%loop> U: [-1,-128) S: [-1,-128) Exits: 127 LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %counter = phi i8 [ 0, %entry ], [ %counter.inc, %loop ]
+; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,2) S: [0,2) Exits: 1 LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %i.inc = add i8 %i, -128
+; CHECK-NEXT: --> {127,+,-128}<%loop> U: [127,0) S: [127,0) Exits: -1 LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %i.sext = sext i8 %i to i16
+; CHECK-NEXT: --> {-1,+,128}<nw><%loop> U: [-1,128) S: [-1,128) Exits: 127 LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %counter.inc = add i8 %counter, 1
+; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,3) S: [1,3) Exits: 2 LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: Determining loop execution counts for: @sext.nw.pre.inc
+; CHECK-NEXT: Loop %loop: backedge-taken count is i8 1
+; CHECK-NEXT: Loop %loop: constant max backedge-taken count is i8 1
+; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is i8 1
+; CHECK-NEXT: Loop %loop: Trip multiple is 2
+;
+ entry:
+ br label %loop
+
+ loop:
+ %i = phi i8 [ -1, %entry ], [ %i.inc, %loop ]
+ %counter = phi i8 [ 0, %entry ], [ %counter.inc, %loop ]
+ %i.inc = add i8 %i, -128
+ %i.sext = sext i8 %i to i16
+ %counter.inc = add i8 %counter, 1
+ %continue = icmp eq i8 %counter, 1
+ br i1 %continue, label %exit, label %loop
+
+ exit:
+ ret void
+}
+
+
+; The zext expression should be {255,+,-128}<nw>.
+; nw should be inferred correctly.
+define void @zext.nw.pre.inc() {
+; CHECK-LABEL: 'zext.nw.pre.inc'
+; CHECK-NEXT: Classifying expressions for: @zext.nw.pre.inc
+; CHECK-NEXT: %i = phi i8 [ -1, %entry ], [ %i.inc, %loop ]
+; CHECK-NEXT: --> {-1,+,-128}<%loop> U: [-1,-128) S: [-1,-128) Exits: 127 LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %counter = phi i8 [ 0, %entry ], [ %counter.inc, %loop ]
+; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,2) S: [0,2) Exits: 1 LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %i.inc = add i8 %i, -128
+; CHECK-NEXT: --> {127,+,-128}<%loop> U: [127,0) S: [127,0) Exits: -1 LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %i.sext = zext i8 %i to i16
+; CHECK-NEXT: --> {255,+,-128}<nw><%loop> U: [127,256) S: [127,256) Exits: 127 LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %counter.inc = add i8 %counter, 1
+; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,3) S: [1,3) Exits: 2 LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: Determining loop execution counts for: @zext.nw.pre.inc
+; CHECK-NEXT: Loop %loop: backedge-taken count is i8 1
+; CHECK-NEXT: Loop %loop: constant max backedge-taken count is i8 1
+; CHECK-NEXT: Loop %loop: symbolic max backedge-taken count is i8 1
+; CHECK-NEXT: Loop %loop: Trip multiple is 2
+;
+ entry:
+ br label %loop
+
+ loop:
+ %i = phi i8 [ -1, %entry ], [ %i.inc, %loop ]
+ %counter = phi i8 [ 0, %entry ], [ %counter.inc, %loop ]
+ %i.inc = add i8 %i, -128
+ %i.sext = zext i8 %i to i16
+ %counter.inc = add i8 %counter, 1
+ %continue = icmp eq i8 %counter, 1
+ br i1 %continue, label %exit, label %loop
+
+ exit:
+ ret void
+}
+
+; nw on the pre-inc does not imply nsuw on the sext.
+; The sext-addrec should not be folded.
+; The pre-inc AR {-1,+,-2} is nw-only: nw is inferred from the sgt-exit of a
+; finite loop whose step is a power of two. The constant 1 is first peeled off
+; the sext operand, so the varying-start argument runs for Delta = 1, between
+; {0,+,-2} and the pre-inc AR. nw is not enough: it only forbids the pre-inc
+; AR from revisiting its start, not from crossing the signed boundary. If %i
+; runs down through -2147483647 and crosses to 2147483647, 2147483645, then
+; %j takes the values 1, -1, ..., -2147483645, -2147483647, 2147483647. The
+; transition -2147483647 -> 2147483647 wraps past signed-min, so at that
+; iteration sext(%j) is 2147483647 whereas {1,+,-2} evaluated in i64 gives
+; -2147483649.
+define void @sext.nsuw.nw.pre.inc(ptr %buf, i32 %n) mustprogress {
+; CHECK-LABEL: 'sext.nsuw.nw.pre.inc'
+; CHECK-NEXT: Classifying expressions for: @sext.nsuw.nw.pre.inc
+; CHECK-NEXT: %i = phi i32 [ -1, %entry ], [ %i.next, %loop ]
+; CHECK-NEXT: --> {-1,+,-2}<nw><%loop> U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %i.next = add i32 %i, -2
+; CHECK-NEXT: --> {-3,+,-2}<nw><%loop> U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %j = add i32 %i, 2
+; CHECK-NEXT: --> {1,+,-2}<nw><%loop> U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %idx = sext i32 %j to i64
+; CHECK-NEXT: --> (1 + (sext i32 {0,+,-2}<nw><%loop> to i64))<nuw><nsw> U: [1,0) S: [-2147483647,2147483648) Exits: <<Unknown>> LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %gep = getelementptr inbounds i64, ptr %buf, i64 %idx
+; CHECK-NEXT: --> (8 + (8 * (sext i32 {0,+,-2}<nw><%loop> to i64))<nsw> + %buf) U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: Determining loop execution counts for: @sext.nsuw.nw.pre.inc
+; CHECK-NEXT: Loop %loop: Unpredictable backedge-taken count.
+; CHECK-NEXT: Loop %loop: Unpredictable constant max backedge-taken count.
+; CHECK-NEXT: Loop %loop: Unpredictable symbolic max backedge-taken count.
+;
+ entry:
+ br label %loop
+
+ loop:
+ %i = phi i32 [ -1, %entry ], [ %i.next, %loop ]
+ %i.next = add i32 %i, -2
+ %j = add i32 %i, 2
+ %idx = sext i32 %j to i64
+ %gep = getelementptr inbounds i64, ptr %buf, i64 %idx
+ store i64 7, ptr %gep
+ %cmp = icmp sgt i32 %i, %n
+ br i1 %cmp, label %loop, label %exit
+
+ exit:
+ ret void
+}
+
+; nw on the pre-inc does not imply nusw on the zext.
+; The zext should not be folded.
+; The pre-inc AR {98,+,(-1 * vscale)} is nw-only: nw is inferred from the
+; eq-exit of a finite loop whose step is a power of two. The assume guarantees
+; %i.next u< 254 on every backedge, so all values of the pre-inc AR are
+; u< 255, which "proves" the no-overflow side of the varying-start argument
+; for Delta = 1. That is not enough: with vscale = 8 and n = 242, %i takes
+; the values 98, 90, ..., 2, 250, 242 (the assume holds throughout, and the
+; pre-inc AR does not self-wrap), while %j takes the values 99, 91, ..., 3,
+; 251, 243. The transition 3 -> 251 wraps below zero, so at that iteration
+; zext(%j) is 251 whereas {99,+,-8} evaluated in i16 gives -5.
+define void @zext.nusw.nw.pre.inc(i8 %n) mustprogress willreturn {
+; CHECK-LABEL: 'zext.nusw.nw.pre.inc'
+; CHECK-NEXT: Classifying expressions for: @zext.nusw.nw.pre.inc
+; CHECK-NEXT: %vs = call i8 @llvm.vscale.i8()
+; CHECK-NEXT: --> vscale U: [1,0) S: [1,0)
+; CHECK-NEXT: %step = sub i8 0, %vs
+; CHECK-NEXT: --> (-1 * vscale) U: [1,0) S: [1,0)
+; CHECK-NEXT: %i = phi i8 [ 98, %entry ], [ %i.next, %loop ]
+; CHECK-NEXT: --> {98,+,(-1 * vscale)}<nw><%loop> U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %i.next = add i8 %i, %step
+; CHECK-NEXT: --> {(98 + (-1 * vscale)),+,(-1 * vscale)}<nw><%loop> U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %j = add i8 %i, 1
+; CHECK-NEXT: --> {99,+,(-1 * vscale)}<nw><%loop> U: full-set S: full-set Exits: <<Unknown>> LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: %idx = zext i8 %j to i16
+; CHECK-NEXT: --> (zext i8 {99,+,(-1 * vscale)}<nw><%loop> to i16) U: [0,256) S: [0,256) Exits: <<Unknown>> LoopDispositions: { %loop: Computable }
+; CHECK-NEXT: Determining loop execution counts for: @zext.nusw.nw.pre.inc
+; CHECK-NEXT: Loop %loop: Unpredictable backedge-taken count.
+; CHECK-NEXT: Loop %loop: Unpredictable constant max backedge-taken count.
+; CHECK-NEXT: Loop %loop: Unpredictable symbolic max backedge-taken count.
+;
+entry:
+ %vs = call i8 @llvm.vscale.i8()
+ %step = sub i8 0, %vs
+ br label %loop
+
+loop:
+ %i = phi i8 [ 98, %entry ], [ %i.next, %loop ]
+ %i.next = add i8 %i, %step
+ %j = add i8 %i, 1
+ %idx = zext i8 %j to i16
+ %in.bounds = icmp ult i8 %i.next, -2
+ call void @llvm.assume(i1 %in.bounds)
+ %exitcond = icmp eq i8 %i, %n
+ br i1 %exitcond, label %exit, label %loop
+
+exit:
+ ret void
+}
diff --git a/llvm/test/Analysis/ScalarEvolution/incorrect-nsw.ll b/llvm/test/Analysis/ScalarEvolution/incorrect-nsw.ll
deleted file mode 100644
index f7edf493c64c4..0000000000000
--- a/llvm/test/Analysis/ScalarEvolution/incorrect-nsw.ll
+++ /dev/null
@@ -1,26 +0,0 @@
-; RUN: opt -disable-output "-passes=print<scalar-evolution>,print<scalar-evolution>" < %s 2>&1 | FileCheck %s
-
-define void @bad.nsw() {
-; CHECK-LABEL: Classifying expressions for: @bad.nsw
-; CHECK-LABEL: Classifying expressions for: @bad.nsw
- entry:
- br label %loop
-
- loop:
- %i = phi i8 [ -1, %entry ], [ %i.inc, %loop ]
-; CHECK: %i = phi i8 [ -1, %entry ], [ %i.inc, %loop ]
-; CHECK-NEXT: --> {-1,+,-128}<nw><%loop>
-; CHECK-NOT: --> {-1,+,-128}<nsw><%loop>
-
- %counter = phi i8 [ 0, %entry ], [ %counter.inc, %loop ]
-
- %i.inc = add i8 %i, -128
- %i.sext = sext i8 %i to i16
-
- %counter.inc = add i8 %counter, 1
- %continue = icmp eq i8 %counter, 1
- br i1 %continue, label %exit, label %loop
-
- exit:
- ret void
-}
``````````
</details>
https://github.com/llvm/llvm-project/pull/218943
More information about the llvm-commits
mailing list