[Bug tree-optimization/28778] [4.0/4.1 Regression] alias bug with cast and call clobbered

2006-11-05 Thread chuck at vertica dot com


--- Comment #46 from chuck at vertica dot com  2006-11-05 22:17 ---
Folks, can anyone please tell me if this is the same problem as I am seeing
here using gcc 4.0.2 for x86_64:

#include 

inline long long Vgetbytes(double f) {
   return *reinterpret_cast(&f);
}

int main (int argc, char **argv)
{
double dd = 2.0;

printf ("%llx\n", Vgetbytes(dd));
printf ("%llx\n", Vgetbytes(dd));
}

When compiled with -02 I get:
build0:/home/cbear $ g++4 --version
g++4 (GCC) 4.0.2 20051130 (Red Hat 4.0.2-14.EL4)
build0:/home/cbear $ g++4 -O2 ftp.cpp
build0:/home/cbear $ ./a.out
0
4000

When I look at the disassemby I see the problem:
main:
.LFB14:
pushq   %rbx# Save EBX as required
.LCFI0:
movl$.LC1, %edi #, Load format string for printf. edi = arg 0
movabsq $4611686018427387904, %rbx  # rbx gets 2.0.
xorl%eax, %eax  # irrelevant
subq$16, %rsp   # Stack frame for calling printf
.LCFI1:
movq8(%rsp), %rsi   # ERROR second argument to printf call wrong
movq%rbx, 8(%rsp)   # ERROR if this line came before prior we would
be OK
callprintf  # printf gets rdi and rsi as args.  rsi bad.
movq8(%rsp), %rsi   # Better luck this time, rsi set properly
movl$.LC1, %edi # rdi initialized againg
xorl%eax, %eax  # irrelevant
movq%rbx, 8(%rsp)   # extraneous, rbx need not be preserved
callprintf  # This one will work
addq$16, %rsp   # Clean up the printf stack
xorl%eax, %eax  # 
popq%rbx# restore rbx for caller
ret # done

If I compile with -fno-strict-aliasing I do get valid code and right answers.

However I like the assembly code produced by gcc 3.4.4 much better because it
doesn't bother to allocate stack space, and just uses rbx:
build0:/home/cbear $ g++ --version
g++ (GCC) 3.4.4 20050721 (Red Hat 3.4.4-2)
build0:/home/cbear $ g++ -O2 -S -fverbose-asm ftp.cpp

main:
.LFB14:
pushq   %rbx#
.LCFI0:
movabsq $4611686018427387904, %rbx  #, 
movl$.LC1, %edi #,
movq%rbx, %rsi  # , 
xorl%eax, %eax  #
callprintf  #
movq%rbx, %rsi  # , 
movl$.LC1, %edi #,
xorl%eax, %eax  #
callprintf  #
popq%rbx#
xorl%eax, %eax  # 
ret


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28778



[Bug tree-optimization/28778] [4.0/4.1 Regression] alias bug with cast and call clobbered

2006-11-05 Thread chuck at vertica dot com


--- Comment #49 from chuck at vertica dot com  2006-11-05 23:39 ---
Sorry.

But maybe it is a FAQ because even with "-Wall" or "-Wstrict-aliasing=2" g++
4.0.2 generates invalid code for this without so much as a peep.  I here 4.1 is
better about giving a warning.

I guess it was too much to think "reinterpret_cast means I know what I'm asking
for, so just do what I say".


-- 


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=28778