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

            Bug ID: 128845
           Summary: postreload deletes a volatile store whose source is a
                    plain load of the same location (mem-to-mem move) at
                    -O1 / -fno-strict-aliasing
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: rtl-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: lzy20000419 at outlook dot com
  Target Milestone: ---

Fails with an m68k-linux-gnu cross trunk build (gcc 17.0.0 20261118,
1190f702416e) and with 13.1.0 through 16.1.0.
Target: m68k-linux-gnu; msp430-elf and vax-netbsdelf delete the store too.

    void
    touch (int *p)
    {
      *(volatile int *) p = *p;
    }

    m68k-linux-gnu-gcc -O1 -S t.c

Observed:

    touch:
            rts

Expected (what -O2 gives on 16.1 and trunk, and what -O1
-fdisable-rtl-postreload gives):

    touch:
            move.l 4(%sp),%a0
            move.l (%a0),(%a0)
            rts

On trunk and 16.1 the store is deleted at -O1 and at -O2 -fno-strict-aliasing.
On 15 and older it is also deleted at -O2 and -Os. At run time, calling touch
on a read-only page does not fault with the -O1 build, while the -O0 and -O2
builds fault as they should (checked under qemu-m68k with the trunk build).

Godbolt (16.1.0): https://godbolt.org/z/ePsqKnf69

Cause: cselib_redundant_set_p (cselib.cc), as changed by r16-6229 for PR122343:

      rtx src = SET_SRC (set);
      if ((MEM_P (src) && MEM_VOLATILE_P (src))
           || !rtx_equal_for_cselib_p (dest, src))
        return false;
      ...strip SUBREG/ZERO_EXTRACT/STRICT_LOW_PART...
      if (!flag_strict_aliasing || !MEM_P (dest))
        return true;

      if (MEM_VOLATILE_P (dest))
        return false;

The insn after reload is (set (mem/v:SI (reg %a0)) (mem:SI (reg %a0))).
rtx_equal_for_cselib_p (dest, src) is true, and with -fno-strict-aliasing the
function returns true before it checks MEM_VOLATILE_P (dest), so
reload_cse_simplify deletes the store. Before r16-6229 there was no volatile
check at all.

Possible fix: check MEM_P (dest) && MEM_VOLATILE_P (dest) before the
!flag_strict_aliasing early return. With that change on trunk, touch keeps
move.l (%a0),(%a0) at -O1 and -O2 -fno-strict-aliasing, and the read-only page
test faults as expected.

Related: PR122343 (r16-6229).

Disclaimer: This test case was found with LLM-assisted targeted testing, and
the report was written with LLM assistance.
  • [Bug rtl-optimization/128845] ... lzy20000419 at outlook dot com via Gcc-bugs

Reply via email to