[llvm] [SCEV] BECount to zero if `((-C + (C smax %x)) /u %x), C > 0` holds (PR #104580)
Antonio Frighetto via llvm-commits
llvm-commits at lists.llvm.org
Wed Sep 4 13:21:39 PDT 2024
https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/104580
>From 43647851a2a98b395d8c00c7584468c6632fd74a Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Fri, 16 Aug 2024 11:51:05 +0200
Subject: [PATCH 1/2] [SCEV] Introduce test for PR104580 (NFC)
---
.../udiv-of-x-xsmaxone-fold.ll | 25 +++++++++++++++++++
1 file changed, 25 insertions(+)
create mode 100644 llvm/test/Analysis/ScalarEvolution/udiv-of-x-xsmaxone-fold.ll
diff --git a/llvm/test/Analysis/ScalarEvolution/udiv-of-x-xsmaxone-fold.ll b/llvm/test/Analysis/ScalarEvolution/udiv-of-x-xsmaxone-fold.ll
new file mode 100644
index 00000000000000..1491e6daa6578c
--- /dev/null
+++ b/llvm/test/Analysis/ScalarEvolution/udiv-of-x-xsmaxone-fold.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_analyze_test_checks.py
+; RUN: opt -disable-output -passes="print<scalar-evolution>" < %s 2>&1 | FileCheck %s
+
+ at g_var = external global i32, align 4
+
+define i32 @test(i32 %x) {
+; CHECK-LABEL: 'test'
+; CHECK-NEXT: Classifying expressions for: @test
+; CHECK-NEXT: %g_var.promoted = load i32, ptr @g_var, align 4
+; CHECK-NEXT: --> %g_var.promoted U: full-set S: full-set
+; CHECK-NEXT: %smax = tail call i32 @llvm.smax.i32(i32 %x, i32 1)
+; CHECK-NEXT: --> (1 smax %x) U: [1,-2147483648) S: [1,-2147483648)
+; CHECK-NEXT: %add = add nsw i32 %smax, -1
+; CHECK-NEXT: --> (-1 + (1 smax %x))<nsw> U: [0,2147483647) S: [0,2147483647)
+; CHECK-NEXT: %udiv = udiv i32 %add, %x
+; CHECK-NEXT: --> ((-1 + (1 smax %x))<nsw> /u %x) U: [0,2147483647) S: [0,2147483647)
+; CHECK-NEXT: Determining loop execution counts for: @test
+;
+entry:
+ %g_var.promoted = load i32, ptr @g_var, align 4
+ %smax = tail call i32 @llvm.smax.i32(i32 %x, i32 1)
+ %add = add nsw i32 %smax, -1
+ %udiv = udiv i32 %add, %x
+ ret i32 %udiv
+}
>From 5badf73bb1cc3361aa1595c2d3c8584e91c820d9 Mon Sep 17 00:00:00 2001
From: Antonio Frighetto <me at antoniofrighetto.com>
Date: Fri, 16 Aug 2024 12:44:20 +0200
Subject: [PATCH 2/2] [SCEV] BECount to zero if `((-C + (C smax %x)) /u %x), C
> 0` holds
The SCEV expression `((-C + (C smax %x)) /u %x)` can be folded
to zero for any positive constant C.
Proof: https://alive2.llvm.org/ce/z/_dLm8C.
---
llvm/lib/Analysis/ScalarEvolution.cpp | 17 +++++++++++++++++
.../ScalarEvolution/udiv-of-x-xsmaxone-fold.ll | 2 +-
2 files changed, 18 insertions(+), 1 deletion(-)
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 6b4a81c217b3c2..4a278baced71e3 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -3547,6 +3547,23 @@ const SCEV *ScalarEvolution::getUDivExpr(const SCEV *LHS,
}
}
+ // ((-C + (C smax %x)) /u %x) evaluates to zero, for any positive constant C.
+ if (const auto *AE = dyn_cast<SCEVAddExpr>(LHS);
+ AE && AE->getNumOperands() == 2) {
+ if (const auto *VC = dyn_cast<SCEVConstant>(AE->getOperand(0))) {
+ const APInt &NegC = VC->getAPInt();
+ if (NegC.isNegative() &&
+ NegC != APInt::getSignedMinValue(NegC.getBitWidth())) {
+ const auto *MME = dyn_cast<SCEVSMaxExpr>(AE->getOperand(1));
+ if (MME && MME->getNumOperands() == 2 &&
+ isa<SCEVConstant>(MME->getOperand(0)) &&
+ cast<SCEVConstant>(MME->getOperand(0))->getAPInt() == -NegC &&
+ MME->getOperand(1) == RHS)
+ return getZero(LHS->getType());
+ }
+ }
+ }
+
// The Insertion Point (IP) might be invalid by now (due to UniqueSCEVs
// changes). Make sure we get a new one.
IP = nullptr;
diff --git a/llvm/test/Analysis/ScalarEvolution/udiv-of-x-xsmaxone-fold.ll b/llvm/test/Analysis/ScalarEvolution/udiv-of-x-xsmaxone-fold.ll
index 1491e6daa6578c..0d4701fc5d2587 100644
--- a/llvm/test/Analysis/ScalarEvolution/udiv-of-x-xsmaxone-fold.ll
+++ b/llvm/test/Analysis/ScalarEvolution/udiv-of-x-xsmaxone-fold.ll
@@ -13,7 +13,7 @@ define i32 @test(i32 %x) {
; CHECK-NEXT: %add = add nsw i32 %smax, -1
; CHECK-NEXT: --> (-1 + (1 smax %x))<nsw> U: [0,2147483647) S: [0,2147483647)
; CHECK-NEXT: %udiv = udiv i32 %add, %x
-; CHECK-NEXT: --> ((-1 + (1 smax %x))<nsw> /u %x) U: [0,2147483647) S: [0,2147483647)
+; CHECK-NEXT: --> 0 U: [0,1) S: [0,1)
; CHECK-NEXT: Determining loop execution counts for: @test
;
entry:
More information about the llvm-commits
mailing list