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

            Bug ID: 128994
           Summary: [14/15/16/17 Regression] wrong code at -O1 and above:
                    bitintlower lowers an unused MIN_EXPR of a
                    _BitInt(129) parameter in place into the parameter,
                    which is still live
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

Hi,

$cat repro-small.c

typedef unsigned _BitInt(129) T;
static T c[4];
__attribute__((noinline)) T f(T a) {
  if ((236 < a ? 236 : a) < -a)
    c[a] = a;              /* taken only for a = 1, 2, 3 */
  return a;
}
int main(void) {
  if (f((T)-2) != (T)-2)   /* gcc -O1: f returns 236 */
    __builtin_abort();
  return 0;
}



$ gcc -O0 repro-small.c && ./a.out        # ok
$ gcc -O1 repro-small.c && ./a.out
Aborted
$ gcc -O1 -fno-tree-coalesce-vars repro-small.c && ./a.out   # ok


The store to `c` is dead (c is never read), so before bitintlower `f` is:

```
  _1 = MIN_EXPR <a_5(D), 236>;
  _2 = -a_5(D);
  <retval> = a_5(D);
  return <retval>;
```

After bitintlower (`-fdump-tree-bitintlower1`), the MIN_EXPR is lowered in
place into the parameter's own storage:

```
  <bb 8>:
  VIEW_CONVERT_EXPR<unsigned long>(a) = 236;
  MEM <unsigned long[2]> [(T *)&a + 8B] = {};

  <bb 9>:
  _11 = MEM <unsigned long> [(T *)&a];        /* -a, then the return value,
read the clobbered a */
  ...
  _4 = MEM <unsigned long> [(T *)&a];
  MEM <unsigned long> [(T *)&<retval>] = _4;
```

So `f((T)-2)` returns 236. The `.L2` path of the -O1 assembly writes 236 into
the argument's stack slot (`movq $236, 8(%rsp)`, ...) before copying the
"argument" to the return value. Presumably, the MIN_EXPR result `_1`, which has
no uses, is coalesced with the default definition of the PARM_DECL `a_5(D)`
while `a` is still live. `-fno-tree-coalesce-vars` avoids it.

Reproduced in Godbolt (gcc trunk: compiler panel, execution panels at -O1 and
-O0): https://godbolt.org/z/5Wr5czqTG

## Checked versions
- gcc trunk 1ed04a18ef6 (2026-11-24, --enable-checking=yes): -O1, -O2, -O3, -Os
abort; -O0 ok
- gcc 16.0.1 20260315, gcc 15.2.0: -O1 abort
- gcc 14.1 : -O1 abort
- gcc 13: no _BitInt support
- clang trunk: ok

Thanks.

Reply via email to