optimization/7145: Optimize + structure init + return value optimization fails

tlb@tlb.org tlb@tlb.org
Thu Jun 27 14:46:00 GMT 2002


>Number:         7145
>Category:       optimization
>Synopsis:       g++ -O with structure initializer & return value optimization generates bad code
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          wrong-code
>Submitter-Id:   net
>Arrival-Date:   Thu Jun 27 13:46:03 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Trevor Blackwell
>Release:        3.1.1 20020617 (prerelease) [FreeBSD]
>Organization:
Umbrella Research
>Environment:
System: FreeBSD tlb.blackwells.net 4.5-RELEASE-p2 FreeBSD 4.5-RELEASE-p2 #3: Sat Mar 9 12:32:15 PST 2002 root@tlb.umbrellaresearch.com:/d1/usr-obj/usr/src/sys/TLB i386


	
host: i386-portbld-freebsd4.5
build: i386-portbld-freebsd4.5
target: i386-portbld-freebsd4.5
configured with: ./..//gcc-20020617/configure --disable-nls --with-gnu-as --with-gnu-ld --with-gxx-include-dir=/usr/local/lib/gcc-lib/i386-portbld-freebsd4.5/3.1.1/include/g++ --disable-libgcj --disable-shared --prefix=/usr/local i386-portbld-freebsd4.5
>Description:

An inline function subject to return value optimization, where the returned argument is initialized
with an initializer-list generates bad code with -O. It seems to only happen when it's an odd
number of halfwords.


>How-To-Repeat:

Compile the following C++ code with g++ -O. On my system, it prints junk values:

  mkcolor: pixel=-1077938364 r=-2244 g=-16449 b=0

It depends on the structure; if I remove the 'blue' member it works. I suspect it has
to do with whether it's a multiple of the word size.

It seems to complete optimize away the initialization as you can see in the assembly output.

-------------------------------------------------------------------------------------------------------

extern "C" int printf(const char *, ...);

struct GdkColor {
  long  pixel;
  short red;
  short green;
  short blue;
};

inline GdkColor mkcolor() {
  GdkColor ret={0,1,2,3};
  return ret;
}

int
main()
{
  GdkColor col=mkcolor();
  printf("mkcolor: pixel=%d r=%d g=%d b=%d\n", col.pixel, col.red, col.green, col.blue);

}

-----------------------------------------------------------------------------------------------------

main:
.LFB1:
        pushl   %ebp
.LCFI0:
        movl    %esp, %ebp
.LCFI1:
        subl    $24, %esp
.LCFI2:
        andl    $-16, %esp
        subl    $12, %esp
        movswl  -16(%ebp),%eax
        pushl   %eax
        movswl  -18(%ebp),%eax
        pushl   %eax
        movswl  -20(%ebp),%eax
        pushl   %eax
        pushl   -24(%ebp)
        pushl   $.LC0
.LCFI3:
        call    printf
        movl    $0, %eax
        leave
        ret

-----------------------------------------------------------------------------------------------------

>Fix:

>Release-Note:
>Audit-Trail:
>Unformatted:



More information about the Gcc-bugs mailing list