[llvm] [InstCombine] Fold `(ct{t, l}z Pow2)` -> `Log2(Pow2)` (PR #122620)

via llvm-commits llvm-commits at lists.llvm.org
Sun Jan 12 08:26:02 PST 2025


https://github.com/goldsteinn updated https://github.com/llvm/llvm-project/pull/122620

>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/8] [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 32d75820776bdab656ddc2ca6953902cbeb24303 Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Sun, 12 Jan 2025 10:01:24 -0600
Subject: [PATCH 2/8] Kk

---
 llvm/test/Transforms/InstCombine/cttz.ll | 32 ++++++++++++++++--------
 1 file changed, 22 insertions(+), 10 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/cttz.ll b/llvm/test/Transforms/InstCombine/cttz.ll
index 7802a1eafdfce9..dfb2c7d240b0b5 100644
--- a/llvm/test/Transforms/InstCombine/cttz.ll
+++ b/llvm/test/Transforms/InstCombine/cttz.ll
@@ -356,17 +356,29 @@ define i8 @fold_clz_log2(i8 %x) {
   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]])
+define i8 @fold_clz_log2_multiuse(i8 %x) {
+; CHECK-LABEL: @fold_clz_log2_multiuse(
+; CHECK-NEXT:    [[V:%.*]] = shl i8 2, [[X:%.*]]
+; CHECK-NEXT:    call void @use.i8(i8 [[V]])
+; CHECK-NEXT:    [[R:%.*]] = call range(i8 0, 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)
-  call void @use.i8(i8 %r)
+  %v = shl i8 2, %x
+  call void @use.i8(i8 %v)
+  %r = call i8 @llvm.ctlz(i8 %v, i1 true)
   ret i8 %r
 }
+
+
+  define i9 @fold_clz_log2_i9(i9 %x) {
+; CHECK-LABEL: @fold_clz_log2_i9(
+; CHECK-NEXT:    [[P2:%.*]] = shl nuw i9 1, [[X:%.*]]
+; CHECK-NEXT:    [[V:%.*]] = call i9 @llvm.umin.i9(i9 [[P2]], i9 32)
+; CHECK-NEXT:    [[R:%.*]] = call range(i9 3, 10) i9 @llvm.ctlz.i9(i9 [[V]], i1 true)
+; CHECK-NEXT:    ret i9 [[R]]
+;
+  %p2 = shl i9 1, %x
+  %v = call i9 @llvm.umin(i9 %p2, i9 32)
+  %r = call i9 @llvm.ctlz(i9 %v, i1 false)
+  ret i9 %r
+}

>From b00d3bf7c037387b142ae2217dbb8f3ee68e6ae8 Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Sun, 12 Jan 2025 10:08:15 -0600
Subject: [PATCH 3/8] tests

---
 llvm/test/Transforms/InstCombine/cttz.ll | 23 ++++++++++++++++++-----
 1 file changed, 18 insertions(+), 5 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/cttz.ll b/llvm/test/Transforms/InstCombine/cttz.ll
index dfb2c7d240b0b5..0c9b1b1d1f0241 100644
--- a/llvm/test/Transforms/InstCombine/cttz.ll
+++ b/llvm/test/Transforms/InstCombine/cttz.ll
@@ -313,6 +313,19 @@ define i8 @fold_ctz_log2(i8 %x) {
   ret i8 %r
 }
 
+define i9 @fold_ctz_log2_i9_okay(i9 %x) {
+; CHECK-LABEL: @fold_ctz_log2_i9_okay(
+; CHECK-NEXT:    [[P2:%.*]] = shl nuw i9 1, [[X:%.*]]
+; CHECK-NEXT:    [[V:%.*]] = call i9 @llvm.umin.i9(i9 [[P2]], i9 32)
+; CHECK-NEXT:    [[R:%.*]] = call range(i9 0, 10) i9 @llvm.cttz.i9(i9 [[V]], i1 true)
+; CHECK-NEXT:    ret i9 [[R]]
+;
+  %p2 = shl i9 1, %x
+  %v = call i9 @llvm.umin(i9 %p2, i9 32)
+  %r = call i9 @llvm.cttz(i9 %v, i1 false)
+  ret i9 %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:%.*]]
@@ -356,8 +369,8 @@ define i8 @fold_clz_log2(i8 %x) {
   ret i8 %r
 }
 
-define i8 @fold_clz_log2_multiuse(i8 %x) {
-; CHECK-LABEL: @fold_clz_log2_multiuse(
+define i8 @fold_clz_log2_multiuse_fail(i8 %x) {
+; CHECK-LABEL: @fold_clz_log2_multiuse_fail(
 ; CHECK-NEXT:    [[V:%.*]] = shl i8 2, [[X:%.*]]
 ; CHECK-NEXT:    call void @use.i8(i8 [[V]])
 ; CHECK-NEXT:    [[R:%.*]] = call range(i8 0, 9) i8 @llvm.ctlz.i8(i8 [[V]], i1 true)
@@ -370,8 +383,8 @@ define i8 @fold_clz_log2_multiuse(i8 %x) {
 }
 
 
-  define i9 @fold_clz_log2_i9(i9 %x) {
-; CHECK-LABEL: @fold_clz_log2_i9(
+define i9 @fold_clz_log2_i9_fail(i9 %x) {
+; CHECK-LABEL: @fold_clz_log2_i9_fail(
 ; CHECK-NEXT:    [[P2:%.*]] = shl nuw i9 1, [[X:%.*]]
 ; CHECK-NEXT:    [[V:%.*]] = call i9 @llvm.umin.i9(i9 [[P2]], i9 32)
 ; CHECK-NEXT:    [[R:%.*]] = call range(i9 3, 10) i9 @llvm.ctlz.i9(i9 [[V]], i1 true)
@@ -379,6 +392,6 @@ define i8 @fold_clz_log2_multiuse(i8 %x) {
 ;
   %p2 = shl i9 1, %x
   %v = call i9 @llvm.umin(i9 %p2, i9 32)
-  %r = call i9 @llvm.ctlz(i9 %v, i1 false)
+  %r = call i9 @llvm.ctlz(i9 %v, i1 true)
   ret i9 %r
 }

>From bc03417b806caf7393cc1e4b6e47aabaef9d1708 Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Sun, 12 Jan 2025 10:24:58 -0600
Subject: [PATCH 4/8] kk

---
 llvm/test/Transforms/InstCombine/cttz.ll | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/cttz.ll b/llvm/test/Transforms/InstCombine/cttz.ll
index 0c9b1b1d1f0241..339b401c959265 100644
--- a/llvm/test/Transforms/InstCombine/cttz.ll
+++ b/llvm/test/Transforms/InstCombine/cttz.ll
@@ -371,20 +371,22 @@ define i8 @fold_clz_log2(i8 %x) {
 
 define i8 @fold_clz_log2_multiuse_fail(i8 %x) {
 ; CHECK-LABEL: @fold_clz_log2_multiuse_fail(
-; CHECK-NEXT:    [[V:%.*]] = shl i8 2, [[X:%.*]]
+; CHECK-NEXT:    [[P2:%.*]] = shl nuw i8 2, [[X:%.*]]
+; CHECK-NEXT:    [[V:%.*]] = call i8 @llvm.umin.i8(i8 [[P2]], i8 32)
 ; CHECK-NEXT:    call void @use.i8(i8 [[V]])
-; CHECK-NEXT:    [[R:%.*]] = call range(i8 0, 9) i8 @llvm.ctlz.i8(i8 [[V]], i1 true)
+; CHECK-NEXT:    [[R:%.*]] = call range(i8 2, 9) i8 @llvm.ctlz.i8(i8 [[V]], i1 true)
 ; CHECK-NEXT:    ret i8 [[R]]
 ;
-  %v = shl i8 2, %x
+  %p2 = shl nuw i8 2, %x
+  %v = call i8 @llvm.umin(i8 %p2, i8 32)
   call void @use.i8(i8 %v)
   %r = call i8 @llvm.ctlz(i8 %v, i1 true)
   ret i8 %r
 }
 
 
-define i9 @fold_clz_log2_i9_fail(i9 %x) {
-; CHECK-LABEL: @fold_clz_log2_i9_fail(
+define i9 @fold_clz_log2_i9(i9 %x) {
+; CHECK-LABEL: @fold_clz_log2_i9(
 ; CHECK-NEXT:    [[P2:%.*]] = shl nuw i9 1, [[X:%.*]]
 ; CHECK-NEXT:    [[V:%.*]] = call i9 @llvm.umin.i9(i9 [[P2]], i9 32)
 ; CHECK-NEXT:    [[R:%.*]] = call range(i9 3, 10) i9 @llvm.ctlz.i9(i9 [[V]], i1 true)

>From fc7692aa78ec75273b340d910dc777f64b38ac7a 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 5/8] [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 339b401c959265..2896d69376d4e8 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
@@ -343,11 +341,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
@@ -358,9 +355,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

>From 43ae7cdbeb3fe2dcf29c04e618a40fed1d5262bd Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Sun, 12 Jan 2025 10:07:22 -0600
Subject: [PATCH 6/8] Any Uses

---
 llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 94240773f46a80..0eb6d45343aff5 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -590,7 +590,7 @@ static Instruction *foldCttzCtlz(IntrinsicInst &II, InstCombinerImpl &IC) {
 
   // cttz(Pow2) -> Log2(Pow2)
   // ctlz(Pow2) -> BitWidth - 1 - Log2(Pow2)
-  if (IsTZ || II.hasOneUse()) {
+  if (IsTZ) {
     if (auto *R = IC.tryGetLog2(Op0, match(Op1, m_One()))) {
       if (IsTZ)
         return IC.replaceInstUsesWith(II, R);

>From 80abf99569b2e6f7a558d699710a53086114b99c Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Sun, 12 Jan 2025 10:08:45 -0600
Subject: [PATCH 7/8] Updated

---
 llvm/test/Transforms/InstCombine/cttz.ll | 9 ++++-----
 1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/llvm/test/Transforms/InstCombine/cttz.ll b/llvm/test/Transforms/InstCombine/cttz.ll
index 2896d69376d4e8..9946877934ba65 100644
--- a/llvm/test/Transforms/InstCombine/cttz.ll
+++ b/llvm/test/Transforms/InstCombine/cttz.ll
@@ -313,9 +313,7 @@ define i8 @fold_ctz_log2(i8 %x) {
 
 define i9 @fold_ctz_log2_i9_okay(i9 %x) {
 ; CHECK-LABEL: @fold_ctz_log2_i9_okay(
-; CHECK-NEXT:    [[P2:%.*]] = shl nuw i9 1, [[X:%.*]]
-; CHECK-NEXT:    [[V:%.*]] = call i9 @llvm.umin.i9(i9 [[P2]], i9 32)
-; CHECK-NEXT:    [[R:%.*]] = call range(i9 0, 10) i9 @llvm.cttz.i9(i9 [[V]], i1 true)
+; CHECK-NEXT:    [[R:%.*]] = call i9 @llvm.umin.i9(i9 [[X:%.*]], i9 5)
 ; CHECK-NEXT:    ret i9 [[R]]
 ;
   %p2 = shl i9 1, %x
@@ -355,8 +353,9 @@ 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:    [[TMP1:%.*]] = call i8 @llvm.umin.i8(i8 [[X:%.*]], i8 5)
-; CHECK-NEXT:    [[R:%.*]] = xor i8 [[TMP1]], 7
+; 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

>From 72ef809e17c2a8c59f2fc4f563f22f8296cb77ea Mon Sep 17 00:00:00 2001
From: Noah Goldstein <goldstein.w.n at gmail.com>
Date: Sun, 12 Jan 2025 10:25:36 -0600
Subject: [PATCH 8/8] Okay

---
 .../InstCombine/InstCombineCalls.cpp          | 21 ++++++++-----------
 llvm/test/Transforms/InstCombine/cttz.ll      | 10 ++++-----
 2 files changed, 13 insertions(+), 18 deletions(-)

diff --git a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
index 0eb6d45343aff5..dd5a4ba5a4724a 100644
--- a/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstCombineCalls.cpp
@@ -590,18 +590,15 @@ static Instruction *foldCttzCtlz(IntrinsicInst &II, InstCombinerImpl &IC) {
 
   // cttz(Pow2) -> Log2(Pow2)
   // ctlz(Pow2) -> BitWidth - 1 - Log2(Pow2)
-  if (IsTZ) {
-    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;
-    }
+  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);
diff --git a/llvm/test/Transforms/InstCombine/cttz.ll b/llvm/test/Transforms/InstCombine/cttz.ll
index 9946877934ba65..829213b24e93e4 100644
--- a/llvm/test/Transforms/InstCombine/cttz.ll
+++ b/llvm/test/Transforms/InstCombine/cttz.ll
@@ -353,9 +353,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
@@ -382,9 +381,8 @@ define i8 @fold_clz_log2_multiuse_fail(i8 %x) {
 
 define i9 @fold_clz_log2_i9(i9 %x) {
 ; CHECK-LABEL: @fold_clz_log2_i9(
-; CHECK-NEXT:    [[P2:%.*]] = shl nuw i9 1, [[X:%.*]]
-; CHECK-NEXT:    [[V:%.*]] = call i9 @llvm.umin.i9(i9 [[P2]], i9 32)
-; CHECK-NEXT:    [[R:%.*]] = call range(i9 3, 10) i9 @llvm.ctlz.i9(i9 [[V]], i1 true)
+; CHECK-NEXT:    [[TMP1:%.*]] = call i9 @llvm.umin.i9(i9 [[X:%.*]], i9 5)
+; CHECK-NEXT:    [[R:%.*]] = sub nuw nsw i9 8, [[TMP1]]
 ; CHECK-NEXT:    ret i9 [[R]]
 ;
   %p2 = shl i9 1, %x



More information about the llvm-commits mailing list