[RFC] arm: Add expander for `negv2di2`

Message ID SA5PR07MB1174556175A371A42E0B4BED4DD692@SA5PR07MB11745.namprd07.prod.outlook.com
State New
Headers
Series [RFC] arm: Add expander for `negv2di2` |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 warning Skipped because it is an RFC
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap warning Skipped because it is an RFC
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap warning Skipped because it is an RFC
linaro-tcwg-bot/tcwg_gcc_build--master-arm warning Skipped because it is an RFC

Commit Message

Onalan, T Nov. 3, 2026, 5:22 p.m. UTC
  GCC>=12 scalarizes `i64` negation: <https://godbolt.org/z/bfaz9ao59>.
Bisection pointed to commit b8f604da25bf.  Add an expander for
`negv2di2`.

Discovered while developing
<https://gitlab.com/libeigen/eigen/-/merge_requests/2817>.

Marked as RFC pending feedback on legal significance.

Disclosure: LLM assistance used for the entirety of this change (though
not this email).

Thank you,
-T

gcc/ChangeLog:

	* config/arm/neon.md (negv2di2): New expander using vector
	subtraction from zero.

gcc/testsuite/ChangeLog:

	* gcc.target/arm/neon-neg64.c: New test.
	* gcc.target/arm/neon-neg64-autovec.c: Likewise.

Assisted-by: GPT-6.1 Sol

---
  

Comments

Richard Earnshaw (foss) Nov. 4, 2026, 6:29 p.m. UTC | #1
On 03/11/2026 17:22, Onalan, T wrote:
> GCC>=12 scalarizes `i64` negation: <https://godbolt.org/z/bfaz9ao59>.
> Bisection pointed to commit b8f604da25bf.  Add an expander for
> `negv2di2`.
> 
> Discovered while developing
> <https://gitlab.com/libeigen/eigen/-/merge_requests/2817>.
> 
> Marked as RFC pending feedback on legal significance.
> 
> Disclosure: LLM assistance used for the entirety of this change (though
> not this email).
> 
> Thank you,
> -T
> 
> gcc/ChangeLog:
> 
> 	* config/arm/neon.md (negv2di2): New expander using vector
> 	subtraction from zero.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* gcc.target/arm/neon-neg64.c: New test.
> 	* gcc.target/arm/neon-neg64-autovec.c: Likewise.
> 
> Assisted-by: GPT-6.1 Sol
> 

Thanks for the patch.

I think this is small enough, and obvious enough to accept.  I've cleaned up the comment and the commit message and put this in.

R.

> ---
> 
> diff --git a/gcc/config/arm/neon.md b/gcc/config/arm/neon.md
> index d8f5c56ea..e36d36652 100644
> --- a/gcc/config/arm/neon.md
> +++ b/gcc/config/arm/neon.md
> @@ -812,6 +812,17 @@
>                      (const_string "neon_abs<q>")))]
>  )
> 
> +;; ARM32 has no VNEG.I64; subtract from a zero vector instead.
> +(define_expand "negv2di2"
> +  [(set (match_operand:V2DI 0 "s_register_operand")
> +	(neg:V2DI (match_operand:V2DI 1 "s_register_operand")))]
> +  "TARGET_NEON"
> +{
> +  rtx zero = force_reg (V2DImode, CONST0_RTX (V2DImode));
> +  emit_insn (gen_subv2di3 (operands[0], zero, operands[1]));
> +  DONE;
> +})
> +
>  (define_insn "neon_neg<mode>2"
>    [(set (match_operand:VDQW 0 "s_register_operand" "=w")
>  	(neg:VDQW (match_operand:VDQW 1 "s_register_operand" "w")))]
> diff --git a/gcc/testsuite/gcc.target/arm/neon-neg64-autovec.c b/gcc/testsuite/gcc.target/arm/neon-neg64-autovec.c
> new file mode 100644
> index 000000000..171cf42a3
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/arm/neon-neg64-autovec.c
> @@ -0,0 +1,15 @@
> +/* { dg-do compile } */
> +/* { dg-require-effective-target arm_neon_ok } */
> +/* { dg-options "-O3 -fvect-cost-model=unlimited -fdump-tree-vect-details" } */
> +/* { dg-add-options arm_neon } */
> +
> +void
> +negate_array (unsigned long long *__restrict output,
> +              const unsigned long long *__restrict input, unsigned int count)
> +{
> +  for (unsigned int i = 0; i < count; ++i)
> +    output[i] = -input[i];
> +}
> +
> +/* { dg-final { scan-assembler {vsub\.i64} } } */
> +/* { dg-final { scan-tree-dump "vectorized 1 loops" "vect" } } */
> diff --git a/gcc/testsuite/gcc.target/arm/neon-neg64.c b/gcc/testsuite/gcc.target/arm/neon-neg64.c
> new file mode 100644
> index 000000000..be707a8ac
> --- /dev/null
> +++ b/gcc/testsuite/gcc.target/arm/neon-neg64.c
> @@ -0,0 +1,34 @@
> +/* { dg-do assemble } */
> +/* { dg-require-effective-target arm_neon_ok } */
> +/* { dg-options "-O2 -save-temps -fno-ipa-icf" } */
> +/* { dg-add-options arm_neon } */
> +
> +#include <arm_neon.h>
> +
> +int64x2_t
> +negate_intrinsic (int64x2_t value)
> +{
> +  return vsubq_s64 (vdupq_n_s64 (0), value);
> +}
> +
> +int64x2_t
> +negate_signed (int64x2_t value)
> +{
> +  return -value;
> +}
> +
> +uint64x2_t
> +negate_unsigned (uint64x2_t value)
> +{
> +  return -value;
> +}
> +
> +int64x2_t
> +negate_memset (int64x2_t value)
> +{
> +  int64x2_t zero;
> +  __builtin_memset (&zero, 0, sizeof (zero));
> +  return vsubq_s64 (zero, value);
> +}
> +
> +/* { dg-final { scan-assembler-times {vsub\.i64} 4 } } */
  

Patch

diff --git a/gcc/config/arm/neon.md b/gcc/config/arm/neon.md
index d8f5c56ea..e36d36652 100644
--- a/gcc/config/arm/neon.md
+++ b/gcc/config/arm/neon.md
@@ -812,6 +812,17 @@ 
                     (const_string "neon_abs<q>")))]
 )

+;; ARM32 has no VNEG.I64; subtract from a zero vector instead.
+(define_expand "negv2di2"
+  [(set (match_operand:V2DI 0 "s_register_operand")
+	(neg:V2DI (match_operand:V2DI 1 "s_register_operand")))]
+  "TARGET_NEON"
+{
+  rtx zero = force_reg (V2DImode, CONST0_RTX (V2DImode));
+  emit_insn (gen_subv2di3 (operands[0], zero, operands[1]));
+  DONE;
+})
+
 (define_insn "neon_neg<mode>2"
   [(set (match_operand:VDQW 0 "s_register_operand" "=w")
 	(neg:VDQW (match_operand:VDQW 1 "s_register_operand" "w")))]
diff --git a/gcc/testsuite/gcc.target/arm/neon-neg64-autovec.c b/gcc/testsuite/gcc.target/arm/neon-neg64-autovec.c
new file mode 100644
index 000000000..171cf42a3
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/neon-neg64-autovec.c
@@ -0,0 +1,15 @@ 
+/* { dg-do compile } */
+/* { dg-require-effective-target arm_neon_ok } */
+/* { dg-options "-O3 -fvect-cost-model=unlimited -fdump-tree-vect-details" } */
+/* { dg-add-options arm_neon } */
+
+void
+negate_array (unsigned long long *__restrict output,
+              const unsigned long long *__restrict input, unsigned int count)
+{
+  for (unsigned int i = 0; i < count; ++i)
+    output[i] = -input[i];
+}
+
+/* { dg-final { scan-assembler {vsub\.i64} } } */
+/* { dg-final { scan-tree-dump "vectorized 1 loops" "vect" } } */
diff --git a/gcc/testsuite/gcc.target/arm/neon-neg64.c b/gcc/testsuite/gcc.target/arm/neon-neg64.c
new file mode 100644
index 000000000..be707a8ac
--- /dev/null
+++ b/gcc/testsuite/gcc.target/arm/neon-neg64.c
@@ -0,0 +1,34 @@ 
+/* { dg-do assemble } */
+/* { dg-require-effective-target arm_neon_ok } */
+/* { dg-options "-O2 -save-temps -fno-ipa-icf" } */
+/* { dg-add-options arm_neon } */
+
+#include <arm_neon.h>
+
+int64x2_t
+negate_intrinsic (int64x2_t value)
+{
+  return vsubq_s64 (vdupq_n_s64 (0), value);
+}
+
+int64x2_t
+negate_signed (int64x2_t value)
+{
+  return -value;
+}
+
+uint64x2_t
+negate_unsigned (uint64x2_t value)
+{
+  return -value;
+}
+
+int64x2_t
+negate_memset (int64x2_t value)
+{
+  int64x2_t zero;
+  __builtin_memset (&zero, 0, sizeof (zero));
+  return vsubq_s64 (zero, value);
+}
+
+/* { dg-final { scan-assembler-times {vsub\.i64} 4 } } */