This patch removes the copy insn from the form

(insn 9 8 11 3 (set (reg:DI 137)
        (sign_extend:DI (plus:SI (subreg/s/u:SI (reg/v:DI 135 [ x ]) 0)
                (const_int 1 [0x1])))) "test.c":3:24 discrim 1 6 
{addsi3_extended}
     (expr_list:REG_DEAD (reg/v:DI 135 [ x ])
        (nil)))
 (insn 11 9 24 3 (set (reg:DI 134 [ <retval> ])
        (reg:DI 137)) "test.c":3:24 discrim 1 275 {*movdi_64bit}
     (nil))

by deleting the copy which is insn 11 in this case and substituting the return 
value
register into insn 9 and thereby making the block simple for 
noce_try_cond_arith to
succeed and produce the optimal code as in :

test_05_3:
        sgt     a5,a0,zero
        addw    a0,a5,a0
        ret

The patch also additionally checks if the src of the else set is a simple 
register
without which it produces branched instructions for cases where the src is a 
sign
extend.

This patch was bootstrapped and regtested in local x86_64-pc-linux-gnu with no 
new
fails and cross compile tested on qemu as riscv64-linux-gnu with no new fails.
Is this patch safe for trunk?

gcc/ChangeLog:

        PR rtl-optimization/122335
        * ifcvt.cc (*noce_process_if_block) : Added checks to safely delete the 
copy insn.

gcc/testsuite/ChangeLog:

        PR rtl-optimization/122335
        * gcc.target/riscv/pr122335.c : New Test.

Signed-off-by: Souradipto Das <[email protected]>
---
 gcc/ifcvt.cc                              | 54 +++++++++++++++++++++++
 gcc/testsuite/gcc.target/riscv/pr122335.c |  8 ++++
 2 files changed, 62 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/riscv/pr122335.c

diff --git a/gcc/ifcvt.cc b/gcc/ifcvt.cc
index fad341ead6c..00e8f89da18 100644
--- a/gcc/ifcvt.cc
+++ b/gcc/ifcvt.cc
@@ -4900,6 +4900,60 @@ noce_process_if_block (noce_if_info *if_info)
        }
     }
 
+/* PR122335 : Deleting an extraneous copy.  */
+  if (HAVE_conditional_move)
+    {
+       /* after fwprop runs the copy insn becomes the immediate
+       neighbour of the rtx which holds x + 1 for the case
+
+       x > 0 ? x + 1 : x;
+
+       where the retval register
+       can be substituted into the previous insn
+       to prevent the czero sequences.  */
+
+    rtx_insn *last = last_active_insn (then_bb, false);
+    rtx set_last_then = last ? single_set (last) : NULL_RTX;
+    rtx set_last_else = NULL_RTX;
+
+    if (else_bb)
+      {
+      rtx_insn *last_else = last_active_insn (else_bb, false);
+      set_last_else = last_else ? single_set (last_else) : NULL_RTX;
+      }
+
+    if (set_last_then
+       && set_last_else != NULL_RTX
+       && REG_P (SET_SRC (set_last_else))
+       && REG_P (SET_SRC (set_last_then))
+       && REG_P (SET_DEST (set_last_then))
+       && !HARD_REGISTER_P (SET_SRC (set_last_then))
+       && !HARD_REGISTER_P (SET_DEST (set_last_then))
+       && !contains_mem_rtx_p (SET_SRC (set_last_then)))
+      {
+
+       rtx_insn *prev = prev_nonnote_nondebug_insn_bb (last);
+       rtx set_prev = (prev && NONJUMP_INSN_P (prev)) ? single_set (prev) : 
NULL_RTX;
+
+       if (set_prev
+          && REG_P (SET_DEST (set_prev))
+          && REGNO (SET_DEST (set_prev)) == REGNO (SET_SRC (set_last_then))
+          && DF_REG_USE_COUNT (REGNO (SET_SRC (set_last_then))) == 1
+          && validate_change (prev, &SET_DEST (set_prev),
+               SET_DEST (set_last_then), false))
+       {
+
+       if (dump_file)
+       fprintf (dump_file,
+       "Replacing dest of insn %d with %d\n", INSN_UID (prev),
+        REGNO (SET_DEST (set_last_then)));
+
+       delete_insn (last);
+
+       }
+       }
+    }
+
   unsigned int then_cost = 0, else_cost = 0;
   if (!bb_valid_for_noce_process_p (then_bb, cond, &then_cost,
                                    &if_info->then_simple))
diff --git a/gcc/testsuite/gcc.target/riscv/pr122335.c 
b/gcc/testsuite/gcc.target/riscv/pr122335.c
new file mode 100644
index 00000000000..0f21249e9c2
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/pr122335.c
@@ -0,0 +1,8 @@
+/* { dg-do compile } */
+/* { dg-options "-march=rv64gcb_zicond -O2" { target { rv64 } } } */
+/* { dg-skip-if "" { *-*-* } { "-O0" "-Og" "-O1" "-Os" "-O3" "-Oz" } } */
+int test_05_3 (int x)
+{
+  return x > 0 ? x + 1 : x;
+}
+/* { dg-final { scan-assembler-times "czero" 0 { target rv64 } } } */
-- 
2.53.0

Reply via email to