[cfe-users] Weird optimization

Artem Alimarin artem.alimarin at gmail.com
Fri Feb 20 12:33:39 PST 2015


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,
-------------- next part --------------
A non-text attachment was scrubbed...
Name: artem_alimarin.vcf
Type: text/x-vcard
Size: 362 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-users/attachments/20150220/2c00c313/attachment.vcf>

More information about the cfe-users mailing list