[llvm] [ValueTracking] Recognize `LShr(UINT_MAX, Y) + 1` as a power-of-two (PR #91171)
via llvm-commits
llvm-commits at lists.llvm.org
Mon May 6 03:56:21 PDT 2024
https://github.com/YanWQ-monad updated https://github.com/llvm/llvm-project/pull/91171
>From 497a79e0f2a9201612ce30f34d185a73c7be7320 Mon Sep 17 00:00:00 2001
From: YanWQ-monad <YanWQmonad at gmail.com>
Date: Mon, 6 May 2024 15:24:19 +0800
Subject: [PATCH 1/4] [ValueTracking] Pre-commit: add test
---
.../ValueTracking/known-power-of-two-urem.ll | 24 +++++++++++++++++++
1 file changed, 24 insertions(+)
diff --git a/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll b/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
index 0fa8a74d250d25..38a216408117f1 100644
--- a/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
+++ b/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
@@ -387,3 +387,27 @@ for.end:
%r = phi i64 [ %sum, %for.body ]
ret i64 %r
}
+
+; https://alive2.llvm.org/ce/z/3QfEHm
+define i8 @known_power_of_two_rust_next_power_of_two(i8 %x, i8 %y) {
+; CHECK-LABEL: @known_power_of_two_rust_next_power_of_two(
+; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X:%.*]], -1
+; CHECK-NEXT: [[TMP2:%.*]] = tail call range(i8 0, 9) i8 @llvm.ctlz.i8(i8 [[TMP1]], i1 true)
+; CHECK-NEXT: [[TMP3:%.*]] = lshr i8 -1, [[TMP2]]
+; CHECK-NEXT: [[TMP4:%.*]] = add i8 [[TMP3]], 1
+; CHECK-NEXT: [[TMP5:%.*]] = icmp ugt i8 [[X]], 1
+; CHECK-NEXT: [[P:%.*]] = select i1 [[TMP5]], i8 [[TMP4]], i8 1
+; CHECK-NEXT: [[R:%.*]] = urem i8 [[Y:%.*]], [[P]]
+; CHECK-NEXT: ret i8 [[R]]
+;
+ %2 = add i8 %x, -1
+ %3 = tail call i8 @llvm.ctlz.i8(i8 %2, i1 true)
+ %4 = lshr i8 -1, %3
+ %5 = add i8 %4, 1
+ %6 = icmp ugt i8 %x, 1
+ %p = select i1 %6, i8 %5, i8 1
+ ; Rust's implementation of `%p = next_power_of_two(%x)`
+
+ %r = urem i8 %y, %p
+ ret i8 %r
+}
>From bd1e9ba76e5360f5b75b98cf30907521b775cd72 Mon Sep 17 00:00:00 2001
From: YanWQ-monad <YanWQmonad at gmail.com>
Date: Mon, 6 May 2024 15:45:24 +0800
Subject: [PATCH 2/4] [ValueTracking] Recognize `LShr(UINT_MAX, Y) + 1` as a
power-of-two
---
llvm/lib/Analysis/ValueTracking.cpp | 10 +++++++++-
.../Analysis/ValueTracking/known-power-of-two-urem.ll | 7 +++----
2 files changed, 12 insertions(+), 5 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 0dbb39d7c8ec46..33bedf7ad3d90d 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2144,6 +2144,8 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
return false;
case Instruction::Add: {
+ unsigned BitWidth = V->getType()->getScalarSizeInBits();
+
// Adding a power-of-two or zero to the same power-of-two or zero yields
// either the original power-of-two, a larger power-of-two or zero.
const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
@@ -2158,7 +2160,6 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q))
return true;
- unsigned BitWidth = V->getType()->getScalarSizeInBits();
KnownBits LHSBits(BitWidth);
computeKnownBits(I->getOperand(0), LHSBits, Depth, Q);
@@ -2173,6 +2174,13 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
if (OrZero || RHSBits.One.getBoolValue() || LHSBits.One.getBoolValue())
return true;
}
+
+ // LShr(UINT_MAX, Y) + 1 is a power of two (if nuw) or zero.
+ if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO)) {
+ APInt UIntMax = APInt::getMaxValue(BitWidth);
+ if (match(I, m_Add(m_LShr(m_SpecificInt(UIntMax), m_Value()), m_One())))
+ return true;
+ }
return false;
}
case Instruction::Select:
diff --git a/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll b/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
index 38a216408117f1..20497cb57d03d0 100644
--- a/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
+++ b/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
@@ -394,10 +394,9 @@ define i8 @known_power_of_two_rust_next_power_of_two(i8 %x, i8 %y) {
; CHECK-NEXT: [[TMP1:%.*]] = add i8 [[X:%.*]], -1
; CHECK-NEXT: [[TMP2:%.*]] = tail call range(i8 0, 9) i8 @llvm.ctlz.i8(i8 [[TMP1]], i1 true)
; CHECK-NEXT: [[TMP3:%.*]] = lshr i8 -1, [[TMP2]]
-; CHECK-NEXT: [[TMP4:%.*]] = add i8 [[TMP3]], 1
-; CHECK-NEXT: [[TMP5:%.*]] = icmp ugt i8 [[X]], 1
-; CHECK-NEXT: [[P:%.*]] = select i1 [[TMP5]], i8 [[TMP4]], i8 1
-; CHECK-NEXT: [[R:%.*]] = urem i8 [[Y:%.*]], [[P]]
+; CHECK-NEXT: [[TMP4:%.*]] = icmp ugt i8 [[X]], 1
+; CHECK-NEXT: [[TMP5:%.*]] = select i1 [[TMP4]], i8 [[TMP3]], i8 0
+; CHECK-NEXT: [[R:%.*]] = and i8 [[TMP5]], [[Y:%.*]]
; CHECK-NEXT: ret i8 [[R]]
;
%2 = add i8 %x, -1
>From 3a64b85b73697a016799f881f1f405863d996f72 Mon Sep 17 00:00:00 2001
From: YanWQ-monad <YanWQmonad at gmail.com>
Date: Mon, 6 May 2024 18:30:09 +0800
Subject: [PATCH 3/4] [ValueTracking] Use `m_AllOnes`
---
llvm/lib/Analysis/ValueTracking.cpp | 11 ++++-------
1 file changed, 4 insertions(+), 7 deletions(-)
diff --git a/llvm/lib/Analysis/ValueTracking.cpp b/llvm/lib/Analysis/ValueTracking.cpp
index 33bedf7ad3d90d..375385aca7a390 100644
--- a/llvm/lib/Analysis/ValueTracking.cpp
+++ b/llvm/lib/Analysis/ValueTracking.cpp
@@ -2144,8 +2144,6 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
return OrZero || isKnownNonZero(I->getOperand(0), Q, Depth);
return false;
case Instruction::Add: {
- unsigned BitWidth = V->getType()->getScalarSizeInBits();
-
// Adding a power-of-two or zero to the same power-of-two or zero yields
// either the original power-of-two, a larger power-of-two or zero.
const OverflowingBinaryOperator *VOBO = cast<OverflowingBinaryOperator>(V);
@@ -2160,6 +2158,7 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
isKnownToBeAPowerOfTwo(I->getOperand(0), OrZero, Depth, Q))
return true;
+ unsigned BitWidth = V->getType()->getScalarSizeInBits();
KnownBits LHSBits(BitWidth);
computeKnownBits(I->getOperand(0), LHSBits, Depth, Q);
@@ -2175,12 +2174,10 @@ bool isKnownToBeAPowerOfTwo(const Value *V, bool OrZero, unsigned Depth,
return true;
}
- // LShr(UINT_MAX, Y) + 1 is a power of two (if nuw) or zero.
- if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO)) {
- APInt UIntMax = APInt::getMaxValue(BitWidth);
- if (match(I, m_Add(m_LShr(m_SpecificInt(UIntMax), m_Value()), m_One())))
+ // LShr(UINT_MAX, Y) + 1 is a power of two (if add is nuw) or zero.
+ if (OrZero || Q.IIQ.hasNoUnsignedWrap(VOBO))
+ if (match(I, m_Add(m_LShr(m_AllOnes(), m_Value()), m_One())))
return true;
- }
return false;
}
case Instruction::Select:
>From b26bf5c5a67a09f2cc938886dde62762b619587e Mon Sep 17 00:00:00 2001
From: YanWQ-monad <YanWQmonad at gmail.com>
Date: Mon, 6 May 2024 18:54:14 +0800
Subject: [PATCH 4/4] [ValueTracking] Add more tests
---
.../ValueTracking/known-power-of-two-urem.ll | 93 +++++++++++++++++++
1 file changed, 93 insertions(+)
diff --git a/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll b/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
index 20497cb57d03d0..47c4587f6991bd 100644
--- a/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
+++ b/llvm/test/Analysis/ValueTracking/known-power-of-two-urem.ll
@@ -410,3 +410,96 @@ define i8 @known_power_of_two_rust_next_power_of_two(i8 %x, i8 %y) {
%r = urem i8 %y, %p
ret i8 %r
}
+
+define i8 @known_power_of_two_lshr_add_one_allow_zero(i8 %x, i8 %y) {
+; CHECK-LABEL: @known_power_of_two_lshr_add_one_allow_zero(
+; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 -1, [[X:%.*]]
+; CHECK-NEXT: [[R:%.*]] = and i8 [[TMP1]], [[Y:%.*]]
+; CHECK-NEXT: ret i8 [[R]]
+;
+ %4 = lshr i8 -1, %x
+ %p = add i8 %4, 1
+
+ ; Note: y % p --> y & (p - 1) allows p == 0
+ %r = urem i8 %y, %p
+ ret i8 %r
+}
+
+define i1 @known_power_of_two_lshr_add_one_nuw_deny_zero(i8 %x, i8 %y) {
+; CHECK-LABEL: @known_power_of_two_lshr_add_one_nuw_deny_zero(
+; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 -1, [[X:%.*]]
+; CHECK-NEXT: [[P:%.*]] = add nuw i8 [[TMP1]], 1
+; CHECK-NEXT: [[AND:%.*]] = and i8 [[P]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp eq i8 [[AND]], 0
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %4 = lshr i8 -1, %x
+ %p = add nuw i8 %4, 1
+
+ ; Note: A & B_Pow2 != B_Pow2 --> A & B_Pow2 == 0 requires B_Pow2 != 0
+ %and = and i8 %p, %y
+ %r = icmp ne i8 %and, %p
+ ret i1 %r
+}
+
+define i1 @negative_known_power_of_two_lshr_add_one_deny_zero(i8 %x, i8 %y) {
+; CHECK-LABEL: @negative_known_power_of_two_lshr_add_one_deny_zero(
+; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 -1, [[X:%.*]]
+; CHECK-NEXT: [[P:%.*]] = add i8 [[TMP1]], 1
+; CHECK-NEXT: [[AND:%.*]] = and i8 [[P]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[AND]], [[P]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %4 = lshr i8 -1, %x
+ %p = add i8 %4, 1
+
+ ; Note: A & B_Pow2 != B_Pow2 --> A & B_Pow2 == 0 requires B_Pow2 != 0
+ %and = and i8 %p, %y
+ %r = icmp ne i8 %and, %p
+ ret i1 %r
+}
+
+define i1 @negative_known_power_of_two_lshr_add_one_nsw_deny_zero(i8 %x, i8 %y) {
+; CHECK-LABEL: @negative_known_power_of_two_lshr_add_one_nsw_deny_zero(
+; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 -1, [[X:%.*]]
+; CHECK-NEXT: [[P:%.*]] = add nsw i8 [[TMP1]], 1
+; CHECK-NEXT: [[AND:%.*]] = and i8 [[P]], [[Y:%.*]]
+; CHECK-NEXT: [[R:%.*]] = icmp ne i8 [[AND]], [[P]]
+; CHECK-NEXT: ret i1 [[R]]
+;
+ %4 = lshr i8 -1, %x
+ %p = add nsw i8 %4, 1
+
+ ; Note: A & B_Pow2 != B_Pow2 --> A & B_Pow2 == 0 requires B_Pow2 != 0
+ %and = and i8 %p, %y
+ %r = icmp ne i8 %and, %p
+ ret i1 %r
+}
+
+define i8 @known_power_of_two_lshr_add_negative_1(i8 %x, i8 %y) {
+; CHECK-LABEL: @known_power_of_two_lshr_add_negative_1(
+; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 -2, [[X:%.*]]
+; CHECK-NEXT: [[P:%.*]] = add nuw i8 [[TMP1]], 1
+; CHECK-NEXT: [[R:%.*]] = urem i8 [[Y:%.*]], [[P]]
+; CHECK-NEXT: ret i8 [[R]]
+;
+ %4 = lshr i8 -2, %x
+ %p = add i8 %4, 1
+
+ %r = urem i8 %y, %p
+ ret i8 %r
+}
+
+define i8 @known_power_of_two_lshr_add_negative_2(i8 %x, i8 %y) {
+; CHECK-LABEL: @known_power_of_two_lshr_add_negative_2(
+; CHECK-NEXT: [[TMP1:%.*]] = lshr i8 -1, [[X:%.*]]
+; CHECK-NEXT: [[P:%.*]] = add nsw i8 [[TMP1]], -1
+; CHECK-NEXT: [[R:%.*]] = urem i8 [[Y:%.*]], [[P]]
+; CHECK-NEXT: ret i8 [[R]]
+;
+ %4 = lshr i8 -1, %x
+ %p = add i8 %4, -1
+
+ %r = urem i8 %y, %p
+ ret i8 %r
+}
More information about the llvm-commits
mailing list