[llvm] [LoongArch] Add patterns to match `cto.w/d` when meeting i8/i16 types `not+cttz` (PR #166124)

via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 2 22:29:50 PST 2025


https://github.com/zhaoqi5 created https://github.com/llvm/llvm-project/pull/166124

None

>From 3616b4b8947c4e0f919de6a39be4d33298c44f3c Mon Sep 17 00:00:00 2001
From: Qi Zhao <zhaoqi01 at loongson.cn>
Date: Mon, 3 Nov 2025 14:25:33 +0800
Subject: [PATCH] [LoongArch] Add patterns to match `cto.w/d` when meeting
 i8/i16 types `not+cttz`

---
 .../Target/LoongArch/LoongArchInstrInfo.td    |  4 ++++
 .../test/CodeGen/LoongArch/ctlz-cttz-ctpop.ll | 20 ++++++++-----------
 2 files changed, 12 insertions(+), 12 deletions(-)

diff --git a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
index 9565a55e4c6c5..41e8373603b09 100644
--- a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
+++ b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
@@ -1393,6 +1393,8 @@ def : PatGpr<ctlz, CLZ_D>;
 def : PatGpr<cttz, CTZ_D>;
 def : Pat<(ctlz (not GPR:$rj)), (CLO_D GPR:$rj)>;
 def : Pat<(cttz (not GPR:$rj)), (CTO_D GPR:$rj)>;
+def : Pat<(cttz (or (not GPR:$rj), 256)), (CTO_D (ANDI GPR:$rj, 255))>;
+def : Pat<(cttz (or (not GPR:$rj), 65536)), (CTO_D (BSTRPICK_D GPR:$rj, 15, 0))>;
 def : PatGpr<loongarch_clzw, CLZ_W>;
 def : PatGpr<loongarch_ctzw, CTZ_W>;
 def : Pat<(loongarch_clzw (not GPR:$rj)), (CLO_W GPR:$rj)>;
@@ -1404,6 +1406,8 @@ def : PatGpr<ctlz, CLZ_W>;
 def : PatGpr<cttz, CTZ_W>;
 def : Pat<(ctlz (not GPR:$rj)), (CLO_W GPR:$rj)>;
 def : Pat<(cttz (not GPR:$rj)), (CTO_W GPR:$rj)>;
+def : Pat<(cttz (or (not GPR:$rj), 256)), (CTO_W (ANDI GPR:$rj, 255))>;
+def : Pat<(cttz (or (not GPR:$rj), 65536)), (CTO_W (BSTRPICK_W GPR:$rj, 15, 0))>;
 } // Predicates = [IsLA32, Has32S]
 
 /// FrameIndex calculations
diff --git a/llvm/test/CodeGen/LoongArch/ctlz-cttz-ctpop.ll b/llvm/test/CodeGen/LoongArch/ctlz-cttz-ctpop.ll
index 27be02c50f1c7..e36e01978d730 100644
--- a/llvm/test/CodeGen/LoongArch/ctlz-cttz-ctpop.ll
+++ b/llvm/test/CodeGen/LoongArch/ctlz-cttz-ctpop.ll
@@ -901,16 +901,14 @@ define i8 @test_not_cttz_i8(i8 %a) nounwind {
 ;
 ; LA32S-LABEL: test_not_cttz_i8:
 ; LA32S:       # %bb.0:
-; LA32S-NEXT:    ori $a1, $zero, 256
-; LA32S-NEXT:    orn $a0, $a1, $a0
-; LA32S-NEXT:    ctz.w $a0, $a0
+; LA32S-NEXT:    andi $a0, $a0, 255
+; LA32S-NEXT:    cto.w $a0, $a0
 ; LA32S-NEXT:    ret
 ;
 ; LA64-LABEL: test_not_cttz_i8:
 ; LA64:       # %bb.0:
-; LA64-NEXT:    ori $a1, $zero, 256
-; LA64-NEXT:    orn $a0, $a1, $a0
-; LA64-NEXT:    ctz.d $a0, $a0
+; LA64-NEXT:    andi $a0, $a0, 255
+; LA64-NEXT:    cto.d $a0, $a0
 ; LA64-NEXT:    ret
   %neg = xor i8 %a, -1
   %tmp = call i8 @llvm.cttz.i8(i8 %neg, i1 false)
@@ -943,16 +941,14 @@ define i16 @test_not_cttz_i16(i16 %a) nounwind {
 ;
 ; LA32S-LABEL: test_not_cttz_i16:
 ; LA32S:       # %bb.0:
-; LA32S-NEXT:    lu12i.w $a1, 16
-; LA32S-NEXT:    orn $a0, $a1, $a0
-; LA32S-NEXT:    ctz.w $a0, $a0
+; LA32S-NEXT:    bstrpick.w $a0, $a0, 15, 0
+; LA32S-NEXT:    cto.w $a0, $a0
 ; LA32S-NEXT:    ret
 ;
 ; LA64-LABEL: test_not_cttz_i16:
 ; LA64:       # %bb.0:
-; LA64-NEXT:    lu12i.w $a1, 16
-; LA64-NEXT:    orn $a0, $a1, $a0
-; LA64-NEXT:    ctz.d $a0, $a0
+; LA64-NEXT:    bstrpick.d $a0, $a0, 15, 0
+; LA64-NEXT:    cto.d $a0, $a0
 ; LA64-NEXT:    ret
   %neg = xor i16 %a, -1
   %tmp = call i16 @llvm.cttz.i16(i16 %neg, i1 false)



More information about the llvm-commits mailing list