This patch helps the noce_try_cond_arith look through a copy insn
which makes that block not simple, given that the else was simple
in the first place. Only a single copy insn is looked through here
so the insn where the copy's value is defined is the first insn in
that block ignoring the note and debug insns. SET_SRC gets to the
definition if all the conditions are satisfied and successfully
looks through the copy.

This patch also optimizes the code for the symmetric case which was
like return x <= 0 ? x : x + 1; at -O2 on riscv64 with zicond which
Sashiko flagged on the last version. Also this patch does not behave
differently based on the presence or absence of debug insns because
this patch relies on the liveness check on bb_valid_for_noce_process_p.
I also included the PATTERN check so as to not drop a clobber.

Bootstrapped and regtested in x86_64-pc-linux-gnu with no new fails
and I also ran the riscv.exp (compile tests) with -march=rv64gcb_zicond
-mabi=lp64d and noticed no new fails.

gcc/ChangeLog:

        PR rtl-optimization/122335
        * ifcvt.cc (noce_try_cond_arith) : Made the function look through a 
copy insn given the conditions passed.

gcc/testsuite/ChangeLog:

        PR rtl-optimization/122335
        * gcc.target/riscv/pr122335-1.c : New Test.
        * gcc.target/riscv/pr122335-2.c : New Test which failed on v1.

Signed-off-by: Souradipto Das <[email protected]>
---
v1:https://gcc.gnu.org/pipermail/gcc-patches/2026-November/737924.html

Changes in v2:
 -Made the looking through a copy insn process less destructive to the original 
insns
  and the changes are only made if the optimization succeeds.
 -Added a new testcase on the symmetric case to pr122335-1.c and a newtestfile 
on pr122335-2.c
  which failed on v1 because it failed to optimize in presence of debug insns. 
Testcase on
  pr122335-2.c was not from Sashiko's review but from a different LLM generated 
testcase which
  reproduced the error on v1.

OK for trunk?

 gcc/ifcvt.cc                                | 26 +++++++++++++++++++--
 gcc/testsuite/gcc.target/riscv/pr122335-1.c | 14 +++++++++++
 gcc/testsuite/gcc.target/riscv/pr122335-2.c | 11 +++++++++
 3 files changed, 49 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.target/riscv/pr122335-1.c
 create mode 100644 gcc/testsuite/gcc.target/riscv/pr122335-2.c

diff --git a/gcc/ifcvt.cc b/gcc/ifcvt.cc
index eb3a958074d..d7da8660962 100644
--- a/gcc/ifcvt.cc
+++ b/gcc/ifcvt.cc
@@ -3486,6 +3486,7 @@ noce_try_cond_arith (noce_if_info *if_info)
   rtx_insn *seq;
   rtx_code op;
   machine_mode mode = GET_MODE (if_info->x);
+  rtx a_val = if_info->a;
 
   /* Scalar integral modes are only supported here.
      Could support scalar floating point but that
@@ -3494,10 +3495,31 @@ noce_try_cond_arith (noce_if_info *if_info)
   if (!SCALAR_INT_MODE_P (mode))
     return false;
 
+  /* Even if the block is not simple it can have
+     an extraneous copy which can be traced back to
+     the definition and the value of 'a' can be
+     computed and copied from there.  For this we
+     require that there are exactly 2 non debug
+     non note insns in then_bb one a copy and the other
+     its definition.  We also check the pattern of
+     def matches def_set so as to not drop a clobber.  */
   if (!noce_simple_bbs (if_info))
-    return false;
+    {
+      rtx_insn *def = prev_nonnote_nondebug_insn_bb (if_info->insn_a);
+      rtx def_set = def ? single_set (def) : NULL_RTX;
+
+       if (!def_set
+           || !REG_P (SET_DEST (def_set))
+           || !rtx_equal_p (SET_DEST (def_set), if_info->a)
+           || PATTERN (def) != def_set
+           || first_active_insn (if_info->then_bb) != def
+           || !if_info->else_simple)
+         return false;
+
+      a_val = SET_SRC (def_set);
+    }
 
-  a = copy_rtx (if_info->a);
+  a = copy_rtx (a_val);
   b = copy_rtx (if_info->b);
   rtx a_arith = noce_cond_arith_operation (a);
   rtx b_arith = noce_cond_arith_operation (b);
diff --git a/gcc/testsuite/gcc.target/riscv/pr122335-1.c 
b/gcc/testsuite/gcc.target/riscv/pr122335-1.c
new file mode 100644
index 00000000000..359e12f083b
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pr122335-1.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcb_zicond -mabi=lp64d -O2" { target { rv64 } } } 
*/
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" "-O1" "-Os" "-O3" "-Oz" } } */
+int test_05_3 (int x)
+{
+  return x > 0 ? x + 1 : x;
+}
+
+int mirror (int x)
+{
+  return x <= 0 ? x : x + 1;
+}
+/* { dg-final { scan-assembler-times "sgt" 2 } } */
+/* { dg-final { scan-assembler-not "czero" } } */
diff --git a/gcc/testsuite/gcc.target/riscv/pr122335-2.c 
b/gcc/testsuite/gcc.target/riscv/pr122335-2.c
new file mode 100644
index 00000000000..88a1f2daf9a
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pr122335-2.c
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcb_zicond -mabi=lp64d -O2 -g" { target { rv64 } } 
} */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" "-O1" "-Os" "-O3" "-Oz" } } */
+int f (int x)
+{
+  int r;
+  if (x > 0) { int t = x + 1; r = t; } else r = x;
+  return r;
+}
+/* { dg-final { scan-assembler "sgt" } } */
+/* { dg-final { scan-assembler-not "czero" } } */
-- 
2.53.0

Reply via email to