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

            Bug ID: 129191
           Summary: [14/15/16/17 Regression] wrong code at -O2: code
                    hoisting materializes a `char` addition that forwprop
                    narrowed on a jump-threaded path (`(unsigned short)(3
                    + (char)c)`, c = 127) since r14-2709
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: tree-optimization
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

Hi,

$cat repro.c

char g;

__attribute__((noinline)) unsigned long f(unsigned c) {
  short d = 6 > c ?: g;
  unsigned long e = (unsigned short)(3 + (char)c);
  for (;;) {
    if (c)
      break;
    if (d)
      return 0;
  }
  return e;
}

int main(void) {
  /* c == 127: (char)127 + 3 == 130 (int arithmetic), (unsigned short)130 ==
130 */
  if (f(127) != 130)
    __builtin_abort();
  return 0;
}

$ gcc -O0 repro.c && ./a.out      # ok (also -O1, -O3, -Os)
$ gcc -O2 repro.c && ./a.out
Aborted
$ gcc -O2 -fno-code-hoisting repro.c && ./a.out    # ok
```

Expected: `f(127)` returns 130. `3 + (char)c` is an `int` addition (127 + 3 =
130), and `(unsigned short)130` is 130.
Actual at -O2: `f(127)` returns 65410. The addition is done in `char`, wraps to
-126, and is then sign-extended to `unsigned short` (0xff82):

```
f:
        leal    3(%rdi), %eax
        xorl    %edx, %edx
        testl   %edi, %edi
        cbtw                    # sign-extends the 8-bit sum
        movzwl  %ax, %eax
        cmove   %rdx, %rax
        ret
```

## Bisect

**Bisected to r14-2709** (65ff4a45b11, "loop-ch improvements, part 5", Jan
Hubicka, 2023-07-21):
https://gcc.gnu.org/git/?p=gcc.git;a=commit;h=65ff4a45b11b5ab13ef849bd5721ab28ff316202

The commit lets loop header copying handle loops that are not do-while: it
moves invariant computations out and duplicates invariant conditionals. Here
that creates the path on which `(char)c + 3` is computed for `c <= 5`; forwprop
narrows it there, and code hoisting then moves the narrow addition to where `c
== 127`. 

`git bisect run` between basepoints/gcc-14 (48f0f297774, ok) and
basepoints/gcc-15 (48a320a1757, aborts), C-only builds,
repro.c at -O2, no skipped commits:

| commit | repro.c -O2 |
|---|---|
| 8cbdb2e4d64 (parent of r14-2709) "tree-optimization/41320 - remove bogus
XFAILed testcase" | ok |
| **65ff4a45b11 (r14-2709) "loop-ch improvements, part 5"** | **aborts** |

## Checked versions

- gcc trunk c10b819941b (2026-11-30, checking): -O2 aborts; -O0, -O1, -O3 and
-Os ok.
- gcc 16.0.1 (r16-8100): -O2 aborts.
- gcc 15.2.0: -O2 and -O3 abort.
- Godbolt gcc 14.1, 14.2, 14.3, 14.4, 15.3 and 16.2: -O2 aborts.
- gcc 13.4.0 (local and Godbolt), gcc 12, gcc 11: ok.
- clang trunk, clang 21: ok.
- UB: none; clean under ASan+UBSan and MSan (clang 21, -O0), and gcc -O0 and
clang agree.

Godbolt (gcc trunk, -O2 vs -O0, two execution panes):
https://godbolt.org/z/6KvvvW8so

Thanks.

Reply via email to