]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
LoongArch: eliminate redundant extension after ctz.w
authorXi Ruoyao <xry111@xry111.site>
Wed, 25 Feb 2026 15:17:10 +0000 (23:17 +0800)
committerXi Ruoyao <xry111@xry111.site>
Fri, 8 May 2026 09:17:27 +0000 (17:17 +0800)
The ctz.w instruction writes to the entire $rd.  So due to the range of
the CTZ result ([0, 32]), $rd is already both sign-extended and
zero-extended from its lower half.  But in pr90838.c we can see two
redundant sign-extension (slli.w ...,0).  Now get rid of them.

The "andi" instructions in pr90393.c are really needed, because the
source code logic should produce 0 for zero input, but the ctz.[dw]
instructions actually produce 64/32.

gcc/

* config/loongarch/loongarch.md (*ctzsi2_extend): New
define_insn.

gcc/testsuite/

* gcc.dg/pr90838.c: Adjust expectation for LoongArch.

gcc/config/loongarch/loongarch.md
gcc/testsuite/gcc.dg/pr90838.c

index 77b3298078af55544531dcba3fda259f15035702..f8693b7f5939f05aba52cf2f3190c4f04d8b23d9 100644 (file)
   [(set_attr "type" "clz")
    (set_attr "mode" "<MODE>")])
 
+(define_insn "*ctzsi2_extend"
+  [(set (match_operand:DI 0 "register_operand" "=r")
+       (any_extend:DI
+         (ctz:SI (match_operand:SI 1 "register_operand" "r"))))]
+  "TARGET_64BIT"
+  "ctz.w\t%0,%1"
+  [(set_attr "type" "clz")
+   (set_attr "mode" "SI")])
+
 ;;
 ;;  ....................
 ;;
index 99c413e26271fb434f9f60d71b6aee6c22d53f69..fc7a045d53f6ea1ff43d4b9630ecc6eb49f5a755 100644 (file)
@@ -87,4 +87,4 @@ int ctz4 (unsigned long x)
 /* { dg-final { scan-tree-dump-times {= \.CTZ} 4 "forwprop2" { target { loongarch64*-*-* } } } } */
 /* { dg-final { scan-assembler-times "ctz.d\t" 1 { target { loongarch64*-*-* } } } } */
 /* { dg-final { scan-assembler-times "ctz.w\t" 3 { target { loongarch64*-*-* } } } } */
-/* { dg-final { scan-assembler-times "\(andi|slli.w\)\t" 4 { target { loongarch64*-*-* } } } } */
+/* { dg-final { scan-assembler-times "andi\t" 2 { target { loongarch64*-*-* } } } } */