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

via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 28 02:10:29 PDT 2026


Author: ZhaoQi
Date: 2026-04-28T17:10:24+08:00
New Revision: f409c264f2cd18d069a1cc627850f14ddcea914f

URL: https://github.com/llvm/llvm-project/commit/f409c264f2cd18d069a1cc627850f14ddcea914f
DIFF: https://github.com/llvm/llvm-project/commit/f409c264f2cd18d069a1cc627850f14ddcea914f.diff

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

Added: 
    

Modified: 
    llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
    llvm/test/CodeGen/LoongArch/ctlz-cttz-ctpop.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
index 555dc31e77755..54d6e51d4d6db 100644
--- a/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
+++ b/llvm/lib/Target/LoongArch/LoongArchInstrInfo.td
@@ -1437,6 +1437,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)>;
@@ -1448,6 +1450,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 384c2d25135ea..2c18c45e0a096 100644
--- a/llvm/test/CodeGen/LoongArch/ctlz-cttz-ctpop.ll
+++ b/llvm/test/CodeGen/LoongArch/ctlz-cttz-ctpop.ll
@@ -903,16 +903,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)
@@ -945,16 +943,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