Bug 44731

Summary: [4.5 Regression] Return value optimization produces inaccurate debug info
Product: gcc Reporter: Brian Bi <bbi5291>
Component: c++Assignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: dodji, gcc-bugs, pawel_sikora, tromey
Priority: P2 Keywords: wrong-debug
Version: 4.5.0   
Target Milestone: 4.6.0   
Host: i686-pc-linux-gnu Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu Known to work: 4.3.2, 4.6.0
Known to fail: Last reconfirmed:

Description Brian Bi 2010-06-30 16:32:36 UTC
Source file (no preprocessing required): (note, I made it C-compatible so I could see if the bug is present in the C compiler as well, the answer being no, for reasons that will become obvious immediately)
--------------------------------------------------------------------------------
struct st{
       int n;
};
struct st f(struct st x)
{
       struct st z;
       z=x;
       z.n++;
       return z;
}
int main()
{
    struct st c;
    c.n=1234;
    f(c);
    return 0;
}
--------------------------------------------------------------------------------
Terminal session:
--------------------------------------------------------------------------------
brian@bbi5291-laptop:~/programs/Other$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/usr/local/libexec/gcc/i686-pc-linux-gnu/4.5.0/lto-wrapper
Target: i686-pc-linux-gnu
Configured with: ./configure
Thread model: posix
gcc version 4.5.0 (GCC) 
brian@bbi5291-laptop:~/programs/Other$ g++ -g bug.c
brian@bbi5291-laptop:~/programs/Other$ gdb a.out
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) break 8
Breakpoint 1 at 0x80483d2: file bug.c, line 8.
(gdb) run
Starting program: /home/brian/programs/Other/a.out 

Breakpoint 1, f (x={n = 1234}) at bug.c:8
8	       z.n++;
Current language:  auto; currently c++
(gdb) print z.n
$1 = 2424820
(gdb) q
The program is running.  Exit anyway? (y or n) y
brian@bbi5291-laptop:~/programs/Other$ g++ -g -fno-elide-constructors bug.c
brian@bbi5291-laptop:~/programs/Other$ gdb a.out
GNU gdb 6.8-debian
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "i486-linux-gnu"...
(gdb) break 8
Breakpoint 1 at 0x80483e0: file bug.c, line 8.
(gdb) run
Starting program: /home/brian/programs/Other/a.out 

Breakpoint 1, f (x={n = 1234}) at bug.c:8
8	       z.n++;
Current language:  auto; currently c++
(gdb) print z.n
$1 = 1234
(gdb) q
The program is running.  Exit anyway? (y or n) y
brian@bbi5291-laptop:~/programs/Other$ 
--------------------------------------------------------------------------------
It is evident to me from these test runs that a bug in the return value optimization is causing the wrong address for the variable z (since this is presumably elided altogether when the optimization is used) to be reported in the debug information. I first noticed this bug in g++ 4.4.1, and installed g++ 4.5.0 and found that it was there too; it does not occur in g++ 4.3.4 or earlier.
Comment 1 Andrew Pinski 2010-07-24 18:27:08 UTC
Works in 4.3.2 and on the trunk.
Comment 2 Tom Tromey 2010-10-11 22:16:06 UTC
*** Bug 45973 has been marked as a duplicate of this bug. ***
Comment 3 Richard Biener 2012-07-02 10:25:15 UTC
Fixed in 4.6.0, the 4.5 branch is being closed.