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

            Bug ID: 87123
           Summary: incorrect optimization involving INT_MIN and
                    conditional operator
           Product: gcc
           Version: 7.3.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andy.koppe at analog dot com
  Target Milestone: ---

Reproduced using gcc 5.4.0 on Ubuntu 16.04, gcc 7.3.0 on Cygwin64, and also a
gcc 7.2.1 for ARM:

$ cat test.c
#include <stdio.h>
int x = 0x80000000;
int main(void)
{
  unsigned y = (unsigned)(x < 0 ? -x : x) >> 8;
  if (y == 0x800000)
    printf("Test passed\n");
  else
    printf("Test failed: y = %#x, expected 0x800000\n", y);
}
$ gcc -O2 test.c
$ ./a.out
Test failed: y = 0x800000, expected 0x800000

Looking at the generated assembly, the "Test passed" path has been optimized
out. The test passes below -O2, or if "(x < 0 ? -x : x)" is replaced with just
"-x".

Reply via email to