[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
Thu Sep 5 08:00:36 PDT 2024
https://github.com/antoniofrighetto updated https://github.com/llvm/llvm-project/pull/104580
>From db71af2e6202ed5bc6b291ea850845b54ce81345 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] [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 | 16 ++++
.../udiv-of-x-xsmaxone-fold.ll | 96 +++++++++++++++++++
2 files changed, 112 insertions(+)
create mode 100644 llvm/test/Analysis/ScalarEvolution/udiv-of-x-xsmaxone-fold.ll
diff --git a/llvm/lib/Analysis/ScalarEvolution.cpp b/llvm/lib/Analysis/ScalarEvolution.cpp
index 6b4a81c217b3c2..57e03f667ba6ff 100644
--- a/llvm/lib/Analysis/ScalarEvolution.cpp
+++ b/llvm/lib/Analysis/ScalarEvolution.cpp
@@ -3547,6 +3547,22 @@ 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.isMinSignedValue()) {
+ 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
new file mode 100644
index 00000000000000..9405c0f726ac7f
--- /dev/null
+++ b/llvm/test/Analysis/ScalarEvolution/udiv-of-x-xsmaxone-fold.ll
@@ -0,0 +1,96 @@
+; 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
+
+define i32 @test_expr_with_constant_1(i32 %x) {
+; CHECK-LABEL: 'test_expr_with_constant_1'
+; CHECK-NEXT: Classifying expressions for: @test_expr_with_constant_1
+; 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: --> 0 U: [0,1) S: [0,1)
+; CHECK-NEXT: Determining loop execution counts for: @test_expr_with_constant_1
+;
+entry:
+ %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
+}
+
+; Non-1 constant: (-2 + (2 smax %x)) /u %x
+define i32 @test_expr_with_constant_2(i32 %x) {
+; CHECK-LABEL: 'test_expr_with_constant_2'
+; CHECK-NEXT: Classifying expressions for: @test_expr_with_constant_2
+; CHECK-NEXT: %smax = tail call i32 @llvm.smax.i32(i32 %x, i32 2)
+; CHECK-NEXT: --> (2 smax %x) U: [2,-2147483648) S: [2,-2147483648)
+; CHECK-NEXT: %add = add nsw i32 %smax, -2
+; CHECK-NEXT: --> (-2 + (2 smax %x))<nsw> U: [0,2147483646) S: [0,2147483646)
+; CHECK-NEXT: %udiv = udiv i32 %add, %x
+; CHECK-NEXT: --> 0 U: [0,1) S: [0,1)
+; CHECK-NEXT: Determining loop execution counts for: @test_expr_with_constant_2
+;
+entry:
+ %smax = tail call i32 @llvm.smax.i32(i32 %x, i32 2)
+ %add = add nsw i32 %smax, -2
+ %udiv = udiv i32 %add, %x
+ ret i32 %udiv
+}
+
+; Negative test, constants mismatch: (-3 + (2 smax %x)) /u %x
+define i32 @test_expr_mismatch_constants(i32 %x) {
+; CHECK-LABEL: 'test_expr_mismatch_constants'
+; CHECK-NEXT: Classifying expressions for: @test_expr_mismatch_constants
+; CHECK-NEXT: %smax = tail call i32 @llvm.smax.i32(i32 %x, i32 2)
+; CHECK-NEXT: --> (2 smax %x) U: [2,-2147483648) S: [2,-2147483648)
+; CHECK-NEXT: %add = add nsw i32 %smax, -3
+; CHECK-NEXT: --> (-3 + (2 smax %x))<nsw> U: [-1,2147483645) S: [-1,2147483645)
+; CHECK-NEXT: %udiv = udiv i32 %add, %x
+; CHECK-NEXT: --> ((-3 + (2 smax %x))<nsw> /u %x) U: full-set S: full-set
+; CHECK-NEXT: Determining loop execution counts for: @test_expr_mismatch_constants
+;
+entry:
+ %smax = tail call i32 @llvm.smax.i32(i32 %x, i32 2)
+ %add = add nsw i32 %smax, -3
+ %udiv = udiv i32 %add, %x
+ ret i32 %udiv
+}
+
+; Negative constant: (3 + (-3 smax %x)) /u %x
+define i32 @test_expr_negative_constant(i32 %x) {
+; CHECK-LABEL: 'test_expr_negative_constant'
+; CHECK-NEXT: Classifying expressions for: @test_expr_negative_constant
+; CHECK-NEXT: %smax = tail call i32 @llvm.smax.i32(i32 %x, i32 -3)
+; CHECK-NEXT: --> (-3 smax %x) U: [-3,-2147483648) S: [-3,-2147483648)
+; CHECK-NEXT: %add = add nsw i32 %smax, 3
+; CHECK-NEXT: --> (3 + (-3 smax %x)) U: [0,-2147483645) S: [0,-2147483645)
+; CHECK-NEXT: %udiv = udiv i32 %add, %x
+; CHECK-NEXT: --> ((3 + (-3 smax %x)) /u %x) U: [0,-2147483645) S: [0,-2147483645)
+; CHECK-NEXT: Determining loop execution counts for: @test_expr_negative_constant
+;
+entry:
+ %smax = tail call i32 @llvm.smax.i32(i32 %x, i32 -3)
+ %add = add nsw i32 %smax, 3
+ %udiv = udiv i32 %add, %x
+ ret i32 %udiv
+}
+
+; Negative signed minimum value.
+define i8 @text_expr_with_constant_signed_min(i8 %x) {
+; CHECK-LABEL: 'text_expr_with_constant_signed_min'
+; CHECK-NEXT: Classifying expressions for: @text_expr_with_constant_signed_min
+; CHECK-NEXT: %smax = tail call i8 @llvm.smax.i8(i8 %x, i8 -128)
+; CHECK-NEXT: --> %x U: full-set S: full-set
+; CHECK-NEXT: %add = add nsw i8 %smax, -128
+; CHECK-NEXT: --> (-128 + %x) U: full-set S: full-set
+; CHECK-NEXT: %udiv = udiv i8 %add, %x
+; CHECK-NEXT: --> ((-128 + %x) /u %x) U: full-set S: full-set
+; CHECK-NEXT: Determining loop execution counts for: @text_expr_with_constant_signed_min
+;
+entry:
+ %smax = tail call i8 @llvm.smax.i8(i8 %x, i8 128)
+ %add = add nsw i8 %smax, -128
+ %udiv = udiv i8 %add, %x
+ ret i8 %udiv
+}
More information about the llvm-commits
mailing list