This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Optimization bug on x86


This very simple code compiles correctly without optimizarion:

---------------------
char get();

int getint(){
        int cnt=4, i=0;
        do{
                i<<=8;
                *(char*)&i=get();
        }while(--cnt);
        return i;
}
------------------------


But when -O1 or -O2 are used and variable i is optimized into register the
compiler disregards the cast to char and "optimizes out" the rest:
----------------------------
gcc -S test.c -O2
..............
getint:
        pushl %ebp
        movl %esp,%ebp
        pushl %ebx
        movl $4,%ebx
        .p2align 4,,7
.L2:
        call get
        decl %ebx
        jnz .L2
        movl -4(%ebp),%ebx
        movl %ebp,%esp
        popl %ebp
        ret
.Lfe1:
        .size    getint,.Lfe1-getint
        .ident  "GCC: (GNU) egcs-2.91.66 19990314 (egcs-1.1.2 release)"
...........................

This is equivalent to

	int cnt=4, i;
	do{
		i=get();
	}while(--cnt);
	return i;

On C++ (where I encountered the bug) I tried also

(char&)i=get();
reinterpret_cast<char&>(i)=get();
*reinterpret_cast<char*>(&i)=get();

and other types of cast without success.



Rimantas Plaipa,

Department of Biochemistry and Biophysics,
Vilnius University,
Ciurlionio 21/27, Vilnius 2009, Lithuania.

E-mail: rp010gf@voruta.vu.lt
Phone: (370-2)-650381 
Fax: (370-2)-235049



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]