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

--- Comment #6 from Maxim Egorushkin <maxim.yegorushkin at gmail dot com> ---
(In reply to Uroš Bizjak from comment #3)
> (In reply to Maxim Egorushkin from comment #2)
> 
> > Could you provide an example where that "dangerous optimization" would break
> > well-formed code please?
> 
> --cut here--
> #include <stdio.h>
> 
> void positive (int a) { printf ("positive: %i\n", a); }
> void nonpositive (int a) { printf ("nonpositive: %i\n", a); }
> 
> void
> __attribute__((noinline))
> g (int a, int b)
> {
>   int diff = a - b;
> 
>   if (diff > 0)
>     return positive (diff);
>   else
>     return nonpositive (diff);
> }
> 
> int
> main ()
> {
>   int a = -0x80000000;
>   int b = 0x01;
> 
>   g (a, b);
> 
>   return 0;
> }
> --cut here--
> 
> $ gcc -O2 ttt.c
> $ ./a.out
> positive: 2147483647
> 
> $ gcc -O0 ttt.c
> $ ./a.out
> positive: 2147483647
> 
> $ clang -O2 ttt.c
> $ ./a.out
> nonpositive: 2147483647          <--------- HERE!
> 
> $ clang -O0 ttt.c
> $ ./a.out
> positive: 2147483647

This code underflows a signed integer, which is undefined behaviour, if I am
not mistaken. So, this would not be a valid example, would it?

Reply via email to