On 10/20/24 3:18 PM, Andrew Pinski wrote:
The gimple (and generic) levels have this optmization since 
r12-2041-g7d6979197274a662da7bdc5.
It seems like a good idea to add a similar one to rtl just in case it is not 
caught at the
gimple level.

Note the loop case in csel-neg-1.c is not handled at the gimple level (even 
with phiopt turned back on),
this is because of casts to avoid signed integer overflow; a patch to fix this 
at the gimple level will be
submitted seperately.

Build and tested for aarch64-linux-gnu.

        PR rtl-optimization/58195

gcc/ChangeLog:

        * simplify-rtx.cc (simplify_context::simplify_ternary_operation): Handle
        `a != 0 ? -a : 0` and `a == 0 ? 0 : -a`.

gcc/testsuite/ChangeLog:

        * gcc.target/aarch64/csel-neg-1.c: New test.

Signed-off-by: Andrew Pinski <quic_apin...@quicinc.com>
---
  gcc/simplify-rtx.cc                           | 22 +++++++++++++
  gcc/testsuite/gcc.target/aarch64/csel-neg-1.c | 31 +++++++++++++++++++
  2 files changed, 53 insertions(+)
  create mode 100644 gcc/testsuite/gcc.target/aarch64/csel-neg-1.c

diff --git a/gcc/simplify-rtx.cc b/gcc/simplify-rtx.cc
index 4d024ec523b..93a1b707425 100644
--- a/gcc/simplify-rtx.cc
+++ b/gcc/simplify-rtx.cc
@@ -6909,6 +6909,28 @@ simplify_context::simplify_ternary_operation (rtx_code 
code, machine_mode mode,
                  && rtx_equal_p (XEXP (op0, 1), op1))))
        return op2;
+ /* Convert a != 0 ? -a : 0 into "-a". */
+      if (GET_CODE (op0) == NE
+         && ! side_effects_p (op0)
+         && ! HONOR_NANS (mode)
+         && ! HONOR_SIGNED_ZEROS (mode)
+         && XEXP (op0, 1) == const0_rtx
+         && op2 == const0_rtx
Shouldn't we be using CONST0_RTX (mode) rather than referencing const0_rtx directly?

OK with that change in both clauses.

Jeff

Reply via email to