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

            Bug ID: 128715
           Summary: [15/16/17 Regression] wrong code at -O2: tail merging
                    merges loads that differ in restrict clique/base, DSE
                    then deletes a store the executed load reads
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: 0599jiangyc at gmail dot com
  Target Milestone: ---

The following code:

```c
/* Godbolt: x86-64 gcc (trunk), flags: -O2
 * GCC trunk wrong code: tail merging (tree-ssa-tail-merge.cc) merges two
blocks whose loads differ only in
 * restrict info (MR_DEPENDENCE_CLIQUE/BASE), keeps the restrict one, and DSE
then deletes a store that the
 * other (executed) load reads.
 * Flags: -O2 (also -O3, -Os, -O2 -fno-strict-aliasing).
 * OK: -O0, -O1, -O2 -fno-tree-tail-merge, -O2 -fno-tree-dse, gcc 11.4 -O2,
clang -O2.
 * No UB: in g only `a` is restrict and *a is never read (x == 0); b and z are
plain pointers.
 * EXPECTED: "8 2", exit 0.   BUGGY: "12 2", exit 1.
 */
#include <stdio.h>

int x;          /* always 0 */
int one = 1;
int m;

static inline int g (int *restrict a, int *b, int *z, int k, int k2)
{
  *b = 1;
  if (k)
    return *a + 7;          /* never executed; same value as *z + 7 below */
  if (k2)
    return *z + 7;          /* executed: must read the 1 stored by *b */
  return 0;
}

__attribute__((noipa)) int h (int *p, int *q)
{
  int t = g (p, q, p, x, one);
  *q = 2;
  return t;
}

int main (void)
{
  m = 5;
  int r = h (&m, &m);
  printf ("%d %d\n", r, m);
  return r != 8 || m != 2;
}
```

Resulted in this output:
```
$ gcc -O2 repro.c && ./a.out
12 2 (exit 1)
(expected 8 2: -O0, -O1, -O2 -fno-tree-tail-merge, -O2 -fno-tree-dse, GCC 14.2,
GCC 11.4 and clang -O2)
```

Trunk: https://godbolt.org/z/3785bPreb (x86-64 gcc trunk, -O2, executed; prints
12 2 as of 2026-11-14; GCC 15.1 and 16.1 also wrong, GCC 14.2 OK)

Root cause:
After inlining g into h, the two return paths load `*a` (MEM_REF with restrict
MR_DEPENDENCE_CLIQUE/BASE from
g's `int *restrict a`) and `*z`; with p == p the loads have the same value
number.  tail merging
(tree-ssa-tail-merge.cc, gimple_equal_p) compares the MEM_REF loads'
volatility, alignment, pointer value and
operand 1 but not MR_DEPENDENCE_CLIQUE/BASE, merges the blocks and keeps the
restrict copy.  DSE then uses the
restrict info of the kept load (`*a` cannot alias `*b` under restrict) and
deletes `*b = 1` as dead,
although the load that actually executes is `*z`, which reads it.  No UB: *a is
never read (x == 0), so the
restrict condition does not apply in this execution.

Fix: in gimple_equal_p also require equal MR_DEPENDENCE_CLIQUE/BASE for MEM_REF
loads (operand_equal_p already
does so for stores), or clear them on the kept block when they differ.
  • [Bug c/128715] New: [15/16/17 Re... 0599jiangyc at gmail dot com via Gcc-bugs

Reply via email to