[cfe-users] Weird optimization

Csaba Raduly rcsaba at gmail.com
Mon Feb 23 00:44:26 PST 2015


Hi,
I think you have a problem with nasal demons:

$ gcc-4.9.2 -fsanitize=undefined -O3 -Wall -pedantic -Wextra minus.cpp
$ ./a.out
minus.cpp:6:17: runtime error: negation of -9223372036854775808 cannot
be represented in type 'long int'; cast to an unsigned type to negate
this value to itself

On Fri, Feb 20, 2015 at 9:33 PM, Artem Alimarin
<artem.alimarin at gmail.com> wrote:
> Hi,
>
> Moving to CLANG toolset we bumped in a problem illustrated by the following
> small reproducer. Reproducible with clang34, clang35, clang36 on FreeBSD 9.3
> x64. It is reproducible only with -O3. The program works as expected on GCC
> and MSVC.
>
> #include <stdio.h>
> #include <stdint.h>
>
> bool test_fp_lt(int64_t x) {
>     // Take care of -0
>     return (-x) > 0;
> }
>
> void test_lt(int64_t x, bool res) {
>     bool r = test_fp_lt(x);
>     if(r != res) {
>         printf("test_lt failed for %016lx: expected %d, got %d\n", x, res,
> r);
>     }
> }
>
> int main() {
>     test_lt(0x0000000000000000ul, false);
>     test_lt(0x8000000000000000ul, false);
>     test_lt(0x4000000000000000ul, false);
>     test_lt(0x8000000000000001ul, true);
>     test_lt(0xFFFFFFFFFFFFFFF0ul, true);
>     return 0;
> }
>
> The program should not print anything. It fails for 0x8000000000000000ul,
> though.
>
> The geneated code for test_fp_lt is:
> shr rdi, 63
> mov al, dil
>
> whereas the expected working code (from GCC) is
> neg rdi
> test rdi, rdi
> setg al
>
> As far as I understand the compiler co,piler optimizes (-x) > 0 into x < 0.
> What is wrong here?
>
> Best regards,
> Artem
>
> _______________________________________________
> cfe-users mailing list
> cfe-users at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/cfe-users
>



-- 
GCS a+ e++ d- C++ ULS$ L+$ !E- W++ P+++$ w++$ tv+ b++ DI D++ 5++
The Tao of math: The numbers you can count are not the real numbers.
Life is complex, with real and imaginary parts.
"Ok, it boots. Which means it must be bug-free and perfect. " -- Linus Torvalds
"People disagree with me. I just ignore them." -- Linus Torvalds



More information about the cfe-users mailing list