[llvm] goldsteinn/cttz ctlz of p2 (PR #122620)
via llvm-commits
llvm-commits at lists.llvm.org
Sat Jan 11 15:03:23 PST 2025
https://github.com/goldsteinn created https://github.com/llvm/llvm-project/pull/122620
- **[InstCombine] Add tests for folding `(ct{t,l}z Pow2)`; NFC**
- **[InstCombine] Fold `(ct{t,l}z Pow2)` -> `Log2(Pow2)`**
>From ab4234154c67b6390567e19a744c480ceb657d44 Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Sat, 11 Jan 2025 16:55:36 -0600
Subject: [PATCH 1/2] [InstCombine] Add tests for folding `(ct{t,l}z Pow2)`;
NFC
---
llvm/test/Transforms/InstCombine/cttz.ll | 73 ++++++++++++++++++++++++
1 file changed, 73 insertions(+)
diff --git a/llvm/test/Transforms/InstCombine/cttz.ll b/llvm/test/Transforms/InstCombine/cttz.ll
index cb0bc59ae79958..7802a1eafdfce9 100644
--- a/llvm/test/Transforms/InstCombine/cttz.ll
+++ b/llvm/test/Transforms/InstCombine/cttz.ll
@@ -297,3 +297,76 @@ define i16 @cttz_assume(i16 %x) {
%cttz = call i16 @llvm.cttz.i16(i16 %x, i1 false)
ret i16 %cttz
}
+
+
+declare void @use.i8(i8)
+define i8 @fold_ctz_log2(i8 %x) {
+; CHECK-LABEL: @fold_ctz_log2(
+; CHECK-NEXT: [[P2:%.*]] = shl nuw i8 1, [[X:%.*]]
+; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.umin.i8(i8 [[P2]], i8 32)
+; CHECK-NEXT: [[R:%.*]] = call range(i8 0, 9) i8 @llvm.cttz.i8(i8 [[V]], i1 true)
+; CHECK-NEXT: ret i8 [[R]]
+;
+ %p2 = shl i8 1, %x
+ %v = call i8 @llvm.umin(i8 %p2, i8 32)
+ %r = call i8 @llvm.cttz(i8 %v, i1 false)
+ ret i8 %r
+}
+
+define i8 @fold_ctz_log2_maybe_z(i8 %x, i8 %y, i1 %c) {
+; CHECK-LABEL: @fold_ctz_log2_maybe_z(
+; CHECK-NEXT: [[V:%.*]] = shl i8 2, [[V_V:%.*]]
+; CHECK-NEXT: [[P2_2:%.*]] = shl i8 4, [[Y:%.*]]
+; CHECK-NEXT: [[V1:%.*]] = select i1 [[C:%.*]], i8 [[V]], i8 [[P2_2]]
+; CHECK-NEXT: [[R:%.*]] = call range(i8 1, 9) i8 @llvm.cttz.i8(i8 [[V1]], i1 false)
+; CHECK-NEXT: ret i8 [[R]]
+;
+ %p2 = shl i8 2, %x
+ %p2_2 = shl i8 4, %y
+ %v = select i1 %c, i8 %p2, i8 %p2_2
+ %r = call i8 @llvm.cttz(i8 %v, i1 false)
+ ret i8 %r
+}
+
+define i8 @fold_ctz_log2_maybe_z_okay(i8 %x, i8 %y, i1 %c) {
+; CHECK-LABEL: @fold_ctz_log2_maybe_z_okay(
+; CHECK-NEXT: [[X:%.*]] = shl i8 2, [[X1:%.*]]
+; CHECK-NEXT: [[Y:%.*]] = shl i8 4, [[Y1:%.*]]
+; CHECK-NEXT: [[V_V:%.*]] = select i1 [[C:%.*]], i8 [[X]], i8 [[Y]]
+; CHECK-NEXT: [[R:%.*]] = call range(i8 1, 9) i8 @llvm.cttz.i8(i8 [[V_V]], i1 true)
+; CHECK-NEXT: ret i8 [[R]]
+;
+ %p2 = shl i8 2, %x
+ %p2_2 = shl i8 4, %y
+ %v = select i1 %c, i8 %p2, i8 %p2_2
+ %r = call i8 @llvm.cttz(i8 %v, i1 true)
+ ret i8 %r
+}
+
+define i8 @fold_clz_log2(i8 %x) {
+; CHECK-LABEL: @fold_clz_log2(
+; CHECK-NEXT: [[P2:%.*]] = shl nuw i8 1, [[X:%.*]]
+; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.umin.i8(i8 [[P2]], i8 32)
+; CHECK-NEXT: [[R:%.*]] = call range(i8 2, 9) i8 @llvm.ctlz.i8(i8 [[V]], i1 true)
+; CHECK-NEXT: ret i8 [[R]]
+;
+ %p2 = shl i8 1, %x
+ %v = call i8 @llvm.umin(i8 %p2, i8 32)
+ %r = call i8 @llvm.ctlz(i8 %v, i1 false)
+ ret i8 %r
+}
+
+define i8 @fold_clz_log2_fail_multi_use(i8 %x) {
+; CHECK-LABEL: @fold_clz_log2_fail_multi_use(
+; CHECK-NEXT: [[P2:%.*]] = shl nuw i8 1, [[X:%.*]]
+; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.umin.i8(i8 [[P2]], i8 32)
+; CHECK-NEXT: [[R:%.*]] = call range(i8 2, 9) i8 @llvm.ctlz.i8(i8 [[V]], i1 true)
+; CHECK-NEXT: call void @use.i8(i8 [[R]])
+; CHECK-NEXT: ret i8 [[R]]
+;
+ %p2 = shl i8 1, %x
+ %v = call i8 @llvm.umin(i8 %p2, i8 32)
+ %r = call i8 @llvm.ctlz(i8 %v, i1 false)
+ call void @use.i8(i8 %r)
+ ret i8 %r
+}
>From aa093345f314aaaafda5f0a010b432c4f6ae4954 Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Sat, 11 Jan 2025 16:55:42 -0600
Subject: [PATCH 2/2] [InstCombine] Fold `(ct{t,l}z Pow2)` -> `Log2(Pow2)`
Do so we can find `Log2(Pow2)` for "free" with `takeLog2`
---
.../Transforms/InstCombine/InstCombineCalls.cpp | 16 ++++++++++++++++
llvm/test/Transforms/InstCombine/cttz.ll | 16 ++++++----------
2 files changed, 22 insertions(+), 10 deletions(-)
diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 7454382412369f..94240773f46a80 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -588,6 +588,22 @@ static Instruction *foldCttzCtlz(IntrinsicInst &II, InstCombinerImpl &IC) {
}
}
+ // cttz(Pow2) -> Log2(Pow2)
+ // ctlz(Pow2) -> BitWidth - 1 - Log2(Pow2)
+ if (IsTZ || II.hasOneUse()) {
+ if (auto *R = IC.tryGetLog2(Op0, match(Op1, m_One()))) {
+ if (IsTZ)
+ return IC.replaceInstUsesWith(II, R);
+ BinaryOperator *BO = BinaryOperator::CreateSub(
+ ConstantInt::get(R->getType(),
+ R->getType()->getScalarSizeInBits() - 1),
+ R);
+ BO->setHasNoSignedWrap();
+ BO->setHasNoUnsignedWrap();
+ return BO;
+ }
+ }
+
KnownBits Known = IC.computeKnownBits(Op0, 0, &II);
// Create a mask for bits above (ctlz) or below (cttz) the first known one.
diff --git a/llvm/test/Transforms/InstCombine/cttz.ll b/llvm/test/Transforms/InstCombine/cttz.ll
index 7802a1eafdfce9..5717e352c81e13 100644
--- a/llvm/test/Transforms/InstCombine/cttz.ll
+++ b/llvm/test/Transforms/InstCombine/cttz.ll
@@ -302,9 +302,7 @@ define i16 @cttz_assume(i16 %x) {
declare void @use.i8(i8)
define i8 @fold_ctz_log2(i8 %x) {
; CHECK-LABEL: @fold_ctz_log2(
-; CHECK-NEXT: [[P2:%.*]] = shl nuw i8 1, [[X:%.*]]
-; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.umin.i8(i8 [[P2]], i8 32)
-; CHECK-NEXT: [[R:%.*]] = call range(i8 0, 9) i8 @llvm.cttz.i8(i8 [[V]], i1 true)
+; CHECK-NEXT: [[R:%.*]] = call i8 @llvm.umin.i8(i8 [[X:%.*]], i8 5)
; CHECK-NEXT: ret i8 [[R]]
;
%p2 = shl i8 1, %x
@@ -330,11 +328,10 @@ define i8 @fold_ctz_log2_maybe_z(i8 %x, i8 %y, i1 %c) {
define i8 @fold_ctz_log2_maybe_z_okay(i8 %x, i8 %y, i1 %c) {
; CHECK-LABEL: @fold_ctz_log2_maybe_z_okay(
-; CHECK-NEXT: [[X:%.*]] = shl i8 2, [[X1:%.*]]
-; CHECK-NEXT: [[Y:%.*]] = shl i8 4, [[Y1:%.*]]
+; CHECK-NEXT: [[X:%.*]] = add i8 [[X1:%.*]], 1
+; CHECK-NEXT: [[Y:%.*]] = add i8 [[Y1:%.*]], 2
; CHECK-NEXT: [[V_V:%.*]] = select i1 [[C:%.*]], i8 [[X]], i8 [[Y]]
-; CHECK-NEXT: [[R:%.*]] = call range(i8 1, 9) i8 @llvm.cttz.i8(i8 [[V_V]], i1 true)
-; CHECK-NEXT: ret i8 [[R]]
+; CHECK-NEXT: ret i8 [[V_V]]
;
%p2 = shl i8 2, %x
%p2_2 = shl i8 4, %y
@@ -345,9 +342,8 @@ define i8 @fold_ctz_log2_maybe_z_okay(i8 %x, i8 %y, i1 %c) {
define i8 @fold_clz_log2(i8 %x) {
; CHECK-LABEL: @fold_clz_log2(
-; CHECK-NEXT: [[P2:%.*]] = shl nuw i8 1, [[X:%.*]]
-; CHECK-NEXT: [[V:%.*]] = call i8 @llvm.umin.i8(i8 [[P2]], i8 32)
-; CHECK-NEXT: [[R:%.*]] = call range(i8 2, 9) i8 @llvm.ctlz.i8(i8 [[V]], i1 true)
+; CHECK-NEXT: [[TMP1:%.*]] = call i8 @llvm.umin.i8(i8 [[X:%.*]], i8 5)
+; CHECK-NEXT: [[R:%.*]] = xor i8 [[TMP1]], 7
; CHECK-NEXT: ret i8 [[R]]
;
%p2 = shl i8 1, %x
More information about the llvm-commits
mailing list