c++/2781: bad code generated for reference call with -O2 (regression from 2.95)
snyder@fnal.gov
snyder@fnal.gov
Tue May 8 17:06:00 GMT 2001
>Number: 2781
>Category: c++
>Synopsis: bad code generated for reference call with -O2 (regression from 2.95)
>Confidential: no
>Severity: serious
>Priority: medium
>Responsible: unassigned
>State: open
>Class: wrong-code
>Submitter-Id: net
>Arrival-Date: Tue May 08 17:06:01 PDT 2001
>Closed-Date:
>Last-Modified:
>Originator: scott snyder
>Release: 3.0 20010507 (prerelease)
>Organization:
>Environment:
System: Linux karma 2.2.16-22 #1 Tue Aug 22 16:49:06 EDT 2000 i686 unknown
Architecture: i686
host: i686-pc-linux-gnu
build: i686-pc-linux-gnu
target: i686-pc-linux-gnu
configured with: ../egcs/configure --prefix=/usr/local/egcs --enable-threads=posix --enable-long-long
>Description:
g++ miscompiles the program below if -O2 is used.
I expect it to print 2.00000; what it actually prints is 1.00000.
I get the correct behavior if i compile without -O2.
(gcc 2.95 also behaves correctly, so this is a regression.)
$ g++ -O2 -o x x.cc
$ ./x
1.000000
$ g++ -o x x.cc
$ ./x
2.000000
$
Here is the relevant extract of the assembly output:
.globl _ZN12PSurfXYPlaneC2Ed
.type _ZN12PSurfXYPlaneC2Ed,@function
_ZN12PSurfXYPlaneC2Ed:
.LFB1:
pushl %ebp
.LCFI0:
movl %esp, %ebp
.LCFI1:
pushl %esi
.LCFI2:
pushl %ebx
.LCFI3:
subl $28, %esp
.LCFI4:
leal -24(%ebp), %eax
movl 16(%ebp), %esi
movl 12(%ebp), %ebx
pushl %eax
movl %esi, -20(%ebp)
movl %ebx, -24(%ebp)
.LCFI5:
call _Z5xswapRd
leal -8(%ebp), %esp
movl %ebx, xxx
popl %ebx
movl %esi, xxx+4
popl %esi
popl %ebp
ret
We start by loading v1 into registers. To make the call to xswap,
v1 is saved to memory, and we pass a reference to that. However,
after returning from xswap, we continue to use the value of v1
contained in registers, rather than reading the (modified) value
back to memory.
Are we erroneously making the call using a temporary?
I also observe that the problem goes away if i change the function
being compiled to an ordinary member instead of a ctor.
If i compile with -fno-optimize-sibling-calls, then the problem
is evident in the .00.rtl dump, so it's not a backend problem.
>How-To-Repeat:
struct PSurfXYPlane
{
PSurfXYPlane(double v1);
};
void xswap(double& x1) ;
double xxx = 0;
PSurfXYPlane::PSurfXYPlane(double v1)
{
xswap(v1);
xxx = v1;
}
void xswap (double& x1) { x1 = 2; }
extern "C" void printf (...);
int main ()
{
PSurfXYPlane p (1);
printf ("%lf\n", xxx);
return 0;
}
>Fix:
>Release-Note:
>Audit-Trail:
>Unformatted:
More information about the Gcc-bugs
mailing list