On Sun, 2026-02-08 at 06:17 +0800, Xi Ruoyao wrote:
> On Sat, 2026-02-07 at 11:54 -0700, Jeffrey Law wrote:
> > Robin's patch to fix nonzero bits for the result of a popcount triggered 
> > a minor testsuite scan failure for loongarch.  Essentially it was 
> > expecting to see an ANDI for a zero-extend, but after the change we get 
> > a slli with a count 0, which is used for sign extension on loongarch.
> > 
> > Both are likely equally performant.  So this just adjusts the test to 
> > accept both forms.  Now one could argue that the result of the popcount 
> 
> This case is ctz instead of popcount.  On LoongArch popcount also has a
> redundant ANDI but due to a different mechanism:  LoongArch has no
> scalar popcount so popcount is implemented by moving the value into a
> vector register and invoking vector popcount (if vector extension is
> available).  Finally when the SI is moved back from vector reg to
> general reg, we hit https://gcc.gnu.org/PR119013.
> 
> This ctz case can be fixed with the SUBREG_PROMOTED_SET trick in
> define_expand, I guess.

Hmm, the trick does not work because combine insist to modify its result
to zero_extend (?!).

The following seems to work for ctz2 and 3:

diff --git a/gcc/config/loongarch/loongarch.md 
b/gcc/config/loongarch/loongarch.md
index 77b3298078a..f8693b7f593 100644
--- a/gcc/config/loongarch/loongarch.md
+++ b/gcc/config/loongarch/loongarch.md
@@ -1452,6 +1452,15 @@ (define_insn "ctz<mode>2"
   [(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")])
+
 ;;
 ;;  ....................
 ;;

But for vtz 1 and 4 it seems the andi is already in the tree:

int ctz1 (unsigned int x)
{
  int _4;
  int _5;

  <bb 2> [local count: 1073741824]:
  _4 = .CTZ (x_1(D), 32);
  _5 = _4 & 31;
  return _5;

}

int ctz4 (long unsigned int x)
{
  int _4;
  int _5;

  <bb 2> [local count: 1073741824]:
  _4 = .CTZ (x_1(D), 64);
  _5 = _4 & 63;
  return _5;

}

Is there an existing bug report for them?

-- 
Xi Ruoyao <[email protected]>

Reply via email to