]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[PR 122701] Emit fresh reg->reg copy rather than modifying existing insnO
authorJeff Law <jlaw@ventanamicro.com>
Sat, 22 Nov 2025 18:33:57 +0000 (11:33 -0700)
committerJeff Law <jlaw@ventanamicro.com>
Sat, 22 Nov 2025 18:37:45 +0000 (11:37 -0700)
I took an ill-advised short-cut with the recent ext-dce improvement to detect
certain shift pairs as sign/zero extensions.  Specifically I was adjusting the
SET_SRC of an object.

Often we can get away with that, but as this case shows it's simply not safe
for RTL.  The core issue is the right shift we're modifying into a simple
reg->reg move may have things like CLOBBERs outside the set resulting in

(parallel
  (set (dstreg) (srcreg))
  (clobber (whatever)))

Even that is often OK as targets which have these kinds of clobbers often need them on their basic moves because those moves often set condition codes.  But that's not true for GCN.

On GCN that transformation leads to an unrecognizable insn as seen in the pr.
The fix is pretty simple.  Just emit a new move and delete the shift.  Of
course we have to be prepared to handle multiple insns once we use
emit_move_insn, but that's not too bad.

PR rtl-optimization/122701
gcc/
* ext-dce.cc (ext_dce_try_optimize_rshift): Emit a fresh reg->reg
copy rather than modifying the existing right shift.

gcc/testsuite/
* gcc.dg/torture/pr122701.c: New test.

gcc/ext-dce.cc
gcc/testsuite/gcc.dg/torture/pr122701.c [new file with mode: 0644]

index 851b56454ceeddba8c1bfbc523909c1891c84bc9..e6189f973bfc22aa2c70c20d3671536a8f5d4b46 100644 (file)
@@ -26,6 +26,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "memmodel.h"
 #include "insn-config.h"
 #include "emit-rtl.h"
+#include "expr.h"
 #include "recog.h"
 #include "cfganal.h"
 #include "tree-pass.h"
@@ -421,13 +422,12 @@ ext_dce_try_optimize_rshift (rtx_insn *insn, rtx set, rtx new_src, rtx_insn *new
       return;
     }
 
-  /* Replace SET_SRC (set) with NEW_SRC.  This changes the form of INSN, so
-     force rerecognition.  We also need to force DF to rescan INSN.  */
-  SET_SRC (set) = new_src;
-  INSN_CODE (insn) = -1;
-  df_insn_rescan (insn);
-
-  rtx new_pattern = PATTERN (insn);
+  /* We're going to generate a fresh insn for the move, so put it
+     into a sequence that we can emit after the current insn.   */
+  start_sequence ();
+  emit_move_insn (SET_DEST (set), new_src);
+  rtx_insn *seq = end_sequence (); 
+  emit_insn_after (seq, insn);
 
   /* Mark the destination as changed.  */
   rtx x = SET_DEST (set);
@@ -439,14 +439,11 @@ ext_dce_try_optimize_rshift (rtx_insn *insn, rtx set, rtx new_src, rtx_insn *new
   if (dump_file)
     {
       fprintf (dump_file, "Successfully transformed to:\n");
-      print_rtl_single (dump_file, new_pattern);
+      print_rtl_single (dump_file, PATTERN (seq));
       fprintf (dump_file, "\n");
     }
 
-  /* INSN may have a REG_EQUAL note indicating that the value was
-     sign or zero extended.  That note is no longer valid since we've
-     just removed the extension.  Just wipe the notes.  */
-  remove_reg_equal_equiv_notes (insn, false);
+  delete_insn (insn);
 
   /* If NEW_SRC died in its prior location, then we need to remove the
      death note and move it to the new location.  */
diff --git a/gcc/testsuite/gcc.dg/torture/pr122701.c b/gcc/testsuite/gcc.dg/torture/pr122701.c
new file mode 100644 (file)
index 0000000..62d3e3d
--- /dev/null
@@ -0,0 +1,11 @@
+/* { dg-do compile } */
+
+char _strtoimax_r_c;
+void _strtoimax_r() {
+  for (;; _strtoimax_r_c++) {
+    if (_strtoimax_r_c <= '9')
+      _strtoimax_r_c -= '0';
+    if (_strtoimax_r_c >= 'A')
+      break;
+  }
+}