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

            Bug ID: 129019
           Summary: [14/15/16/17 Regression] m68k -mcpu=68000: wrong code
                    at -O2/-O3: reload deletes an output-reload store
                    because a REG_DEAD note for an overlapping DImode
                    register is treated as killing the narrower hard
                    register
           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
                CC: schwab at gcc dot gnu.org
  Target Milestone: ---
            Target: m68k-linux-gnu

Fails with an m68k-linux-gnu cross trunk build (gcc 17.0.0 20261125,
7543d3c4c4a4) and with every tested release from 12.5.0 to 16.2.0; 11.5.0
works.
Target: m68k-linux-gnu.

The source has no undefined behavior: the wrapping arithmetic is unsigned, the
s16 conversions are implementation-defined truncations, and array indexes are
masked. -O0 -fsanitize=undefined,address is clean.

    #include <stdio.h>
    #include <stdint.h>
    typedef uint64_t u64;
    typedef uint32_t u32;
    typedef uint16_t u16;
    typedef uint8_t u8;
    typedef int16_t s16;
    typedef int8_t s8;
    static u64 HH = 1469598103934665603ULL;
    static void mix (u64 v) { HH = (HH ^ v) * 1099511628211ULL; }
    __attribute__((noipa)) static u32 id (u32 v) { return v; }
    __attribute__((noipa)) static u32 cf (u32 a, u32 b) { return a *
2654435761u + (b >> 3); }
    struct S0 { u32 f0; u32 f1; s16 f2; u32 f3; };
    struct S1 { u16 f0; u64 f1; u64 f2; };
    struct S2 { u8 f0; u32 f1; u16 f2; s8 f3; };
    struct S1 A0[64];
    struct S2 A1[16];
    s16 IX[8];
    __attribute__((noipa)) u32 fn0 (u32 k)
    {
      struct S0 A2[256];
      for (int i = 0; i < 256; i++)
        {
          A2[i].f0 = (u32) (i * 341u + k);
          A2[i].f1 = (u32) (i * 281u + k);
          A2[i].f2 = (s16) (i * 312u + k);
          A2[i].f3 = (u32) (i * 714u + k);
        }
      u64 v0 = id (k ^ 86u);
      u32 v1 = id (k * 43u);
      u64 v2 = id (k + 53u);
      u32 v3 = id (k * 69u);
      u32 v4 = id (k * 59u);
      u32 v5 = id (k + 31u);
      u32 v6 = id (k + 85u);
      u32 v7 = id (k * 94u);
      s16 s0 = IX[(k + 0) & 7];
      s16 s1 = IX[(k + 1) & 7];
      s16 s2 = IX[(k + 2) & 7];
      s16 s3 = IX[(k + 3) & 7];
      u64 *p1 = &A0[(s16) (s2 & 63)].f2;
      u16 *p3 = &A0[(s16) (s1 & 63)].f0;
      for (int q = 0; q < 7; q++)
        {
          s0 = (s16) ((s0 + 3) & 255);
          v2 ^= (u32) *p3;
          v6 = cf (v6, v1);
          if (v5 & 1) v3 += (u32) A0[(s16) (s2 & 63)].f0;
          {
            s16 h40 = (s16) A1[(s16) (s3 & 15)].f3;
            v2 = cf (v2, v7);
            v7 += h40;
            v7 ^= (u32) (v5 + (s16) h40);
          }
          A1[(s16) (s1 & 15)].f1 ^= (u32) v1;
          A2[(s16) (s0 & 255)].f3 = (u32) A1[(s16) (s1 & 15)].f3;
          v4 = v1 * v5;
          {
            s16 h45 = (s16) A2[(s16) (s1 & 255)].f2;
            v1 = cf (v1, v5);
            v2 += h45;
            v5 ^= (u32) (v2 + (s16) h45);
          }
          v2 ^= (u32) *p3;
          v3 ^= (u32) *p1;
          if (v4 & 1) v0 += (u32) A0[(s16) (s1 & 63)].f2;
        }
      u32 r = 0;
      r = r * 31u + (u32) v0;
      r = r * 31u + (u32) v2;
      r = r * 31u + (u32) v3;
      r ^= (u32) *p1;
      r ^= (u32) *p3;
      for (int i = 0; i < 256; i++)
        {
          mix (A2[i].f0);
          mix (A2[i].f1);
          mix (A2[i].f2);
          mix (A2[i].f3);
        }
      return r;
    }
    int main (void)
    {
      for (int i = 0; i < 64; i++)
        A0[i].f0 = (u16) (i * 5557u + 16u);
      for (u32 k = 0; k < 4; k++)
        mix (fn0 (k));
      printf ("%llx\n", (unsigned long long) HH);
    }


    m68k-linux-gnu-gcc -O2 -mcpu=68000 -static t.c && qemu-m68k ./a.out

Observed on trunk, 15.2.0, 15.3.0, 16.1.0 and 16.2.0: cd63b94a3f38adc0. 15.1.0
prints a different wrong hash (bd2ce73c6b3b5c0). Expected: f3b3a3e5eb0808b3.
-O3 prints the same wrong hash as -O2 on trunk. -O0, -O1, -Os, -mcpu=68020 and
-O2 -mlra print the expected hash. 12.5.0 through 14.4.0 print a different
wrong hash (44eeed2b99cc0a18).

Godbolt (16.2.0): https://godbolt.org/z/oa7TqE14v

Cause: IRA puts the loop-carried SImode value (u32) *p3 and a DImode temporary
in the same %a0. On big-endian m68k, %a0 is the high word of that DImode
register. The high word is dead after the low word is used, so the use insn has
a REG_DEAD note for the whole DImode hard register. find_regno_note
(rtlanal.cc) accepts a note that only overlaps the queried regno, so
dead_or_set_p reports the still-live SImode register as dead. Both
delete_output_reload paths in reload1.cc (in emit_input_reload_insns and
do_input_reload) then drop the store back to %a0, and the next iteration reads
a stale value. m68k still uses old reload by default; -mlra does not go through
this code.

Possible fix (tested on this trunk): in reload1.cc, replace those two
dead_or_set_p checks with a helper that returns false when the only REG_DEAD
note that overlaps the stored-to hard register is for a wider register. After
that change, the test prints the expected hash at -O2, -O3, -O2 -mno-lra and
-O2 -fwrapv, and the already-correct -O0, -O1, -Os, -O2 -mlra and -O2
-mcpu=68020 results are unchanged.

Related: PR123853, PR124569, PR113939 (LRA tracker) and PR15265 (an older
output-reload deletion bug).

Disclaimer: This test case was generated by a fuzzer, and the report was
written with LLM assistance.

Reply via email to