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

            Bug ID: 128883
           Summary: [17 Regression] wrong code at -O1:
                    __builtin_sub_overflow_p (0, 0, (_BitInt(129))0) after
                    a switch on a _BitInt(129) reports overflow
           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: shimizu2486 at gmail dot com
  Target Milestone: ---

Live reproducer: https://godbolt.org/z/TP9z87Wsh

$ cat t1.c
#include <stdio.h>
__attribute__((noipa)) int f(_Bool c) {
  int l = 0;
  switch ((_BitInt(129))c) {
  case 2:
    return 0;
  case 3:
    l = 1;
  }
  return __builtin_sub_overflow_p(0, l, (_BitInt(129))0);
}
__attribute__((noipa)) void dirty(void) {
  volatile char s[32];
  for (int i = 0; i < 32; i++)
    s[i] = -1;
}
int main(void) {
  dirty();
  printf("%d\n", f(0));
}

c is 0, so the switch takes neither case and l stays 0; 0 - 0 fits in a
_BitInt(129), so f returns 0 and the program should print 0.

$ gcc -O0 t1.c -o t1 && ./t1
0                    (correct)
$ gcc -O1 t1.c -o t1 && ./t1
1                    (wrong)

  x86_64 trunk (75dfd8b97da1)   wrong
  x86_64 16.2.0, 15.3.0, 14.4.0 correct

-O1 is wrong. -O0, -O2, -O3, -Os and -Og are correct.
-fno-tree-bit-ccp or -fno-tree-ccp makes the result correct.
Without dirty()m the program happens to print 0.

Reply via email to