https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127336

--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
--- a/gcc/optabs.cc     2026-09-08 12:52:03.000000000 +0200
+++ b/gcc/optabs.cc     2026-09-11 17:53:40.257036917 +0200
@@ -8153,6 +8153,12 @@ expand_atomic_fetch_op_no_fallback (rtx
      try that operation.  */
   if (after || unused_result || optab.reverse_code != UNKNOWN)
     {
+      rtx_insn *start = get_last_insn ();
+      /* Force val into a register if it could change value when the
+        atomic insn updates mem.  */
+      if (!unused_result && reg_overlap_mentioned_p (mem, val))
+       val = force_reg (mode, val);
+
       /* Try the __atomic version, then the older __sync version.  */
       result = maybe_emit_op (&optab, target, mem, val, true, model, !after);
       if (!result)
@@ -8179,6 +8185,8 @@ expand_atomic_fetch_op_no_fallback (rtx
                                          true, OPTAB_LIB_WIDEN);
          return result;
        }
+      else
+       delete_insns_since (start);
     }

   /* No direct opcode can be generated.  */
fixes this, but need to look also whether the expansion as compare and exchange
loop doesn't have similar bug.

Reply via email to