Bug 15461 - [3.4 regression] ICE due to NRV and inlining
Summary: [3.4 regression] ICE due to NRV and inlining
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.4.0
: P2 critical
Target Milestone: 3.4.2
Assignee: Jason Merrill
URL:
Keywords: ice-on-valid-code, monitored
: 16432 17054 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-05-15 13:16 UTC by Kirill Smelkov
Modified: 2004-09-09 23:40 UTC (History)
7 users (show)

See Also:
Host: i686-pc-linux-gnu
Target: i686-pc-linux-gnu
Build: i686-pc-linux-gnu
Known to work: 4.0 3.3.3
Known to fail: 3.4.0
Last reconfirmed: 2004-05-15 16:09:06


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Kirill Smelkov 2004-05-15 13:16:17 UTC
---time.cpp--- 
struct A { 
    long tv_sec; 
}; 
 
class B { 
public: 
    operator A () const; 
}; 
 
inline B::operator A () const 
{ 
    int sec = 1; 
 
    A tv = { 
        tv_sec:  sec, 
    }; 
 
    return tv; 
} 
 
 
void xxx(const B& t) 
{ 
    A tv(t); 
} 
---end of time.cpp--- 
 
with gcc-3.3.3 it is ok. 
can't see whether this is related to PR14203 or not. 
 
[kirr@tugrik bug]$ g++340-check -O -Wall  -I include -c time.cpp -o time.o 
time.cpp: In function `void xxx(const B&)': 
time.cpp:11: internal compiler error: in make_decl_rtl, at varasm.c:752 
Please submit a full bug report, 
with preprocessed source if appropriate. 
See <URL:http://gcc.gnu.org/bugs.html> for instructions. 
 
[kirr@tugrik bug]$ g++340-check -v 
Reading specs from 
/usr/local/gcc-3.4.0-check/lib/gcc/i686-pc-linux-gnu/3.4.0/specs 
Configured with: ../gcc-3.4.0/configure --prefix=/usr/local/gcc-3.4.0-check 
--with-gnu-as --with-gnu-ld --enable-threads=posix --with-arch=i686 
--with-tune=pentium4 --enable-__cxa_atexit --enable-languages=c,c++,f77 
--enable-checking --disable-nls 
Thread model: posix 
gcc version 3.4.0
Comment 1 Andrew Pinski 2004-05-15 16:09:05 UTC
Confirmed (but I do not have a tree with 3.4.1 in it).
But here is the backtrace

#4  0x082da8ea in make_decl_rtl (decl=0x40129804, asmspec=0x0) at ../../gcc/varasm.c:756
#5  0x0815e10c in expand_expr_real (exp=0x40129804, target=0x0, tmode=VOIDmode, 
modifier=EXPAND_NORMAL, alt_rtl=0x0)
    at ../../gcc/expr.c:6403
#6  0x0815f994 in expand_expr_real (exp=0x4012a6cc, target=0x0, tmode=VOIDmode, 
modifier=EXPAND_NORMAL, alt_rtl=0x0)
    at ../../gcc/expr.c:7593
#7  0x08163c30 in store_field (target=0x4011b930, bitsize=32, bitpos=0, mode=SImode, 
exp=0x4012a6cc, value_mode=VOIDmode,
    unsignedp=0, type=0x401246c0, alias_set=0) at ../../gcc/expr.c:5331
#8  0x081664be in store_constructor_field (target=0x4011b930, bitsize=32, bitpos=0, 
mode=1108532352, exp=0x4012a6cc, type=0x83,
    cleared=0, alias_set=0) at ../../gcc/expr.c:4524
#9  0x08164f83 in store_constructor (exp=0x4012a654, target=0x4011b930, cleared=0, size=4) at 
../../gcc/expr.c:4723
#10 0x0815ecc4 in expand_expr_real (exp=0x4012a654, target=0x4011b930, tmode=SImode, 
modifier=EXPAND_NORMAL, alt_rtl=0x0)
    at ../../gcc/expr.c:6872
#11 0x081630df in store_expr (exp=0x4012a654, target=0x4011b900, want_value=0) at ../../gcc/
expr.c:4221
#12 0x081640ab in expand_assignment (to=0x40129798, from=0x4012a654, want_value=0) at ../../
gcc/expr.c:4001
#13 0x082be1ef in expand_decl_init (decl=0x40129798) at ../../gcc/stmt.c:4086
#14 0x080e5061 in emit_local_var (decl=0x40129798) at ../../gcc/c-semantics.c:287
#15 0x080e5c4c in expand_stmt (t=0x4012a4ec) at ../../gcc/c-semantics.c:806
#16 0x080e5b21 in expand_stmt (t=0x4012a488) at ../../gcc/c-semantics.c:826
#17 0x080dea48 in c_expand_expr (exp=0x4012a49c, target=0x0, tmode=SImode, modifier=0, 
alt_rtl=0x0) at ../../gcc/c-common.c:4105
#18 0x0815afb5 in expand_expr_real (exp=0x4012a49c, target=0x0, tmode=SImode, 
modifier=EXPAND_NORMAL, alt_rtl=0x0)
Comment 2 Eric Botcazou 2004-06-19 06:10:04 UTC
Investigating.
Comment 3 Eric Botcazou 2004-06-20 11:47:14 UTC
I think the problem lies in the C++ front-end.

The ICE happens because the tree-inliner inserts a DECL_STMT corresponding to
the RESULT_DECL 'tv' when inlining the operator:

    arg 0 <compound_stmt 0x4024b488
        arg 0 <scope_stmt 0x4024b4c4 tree_0 tree_3
            arg 0 <block 0x4024c118 used vars <var_decl 0x4024a6c0 this>
abstract_origin <function_decl 0x40248a20 operator 1>>
            chain <decl_stmt 0x4024b4b0 arg 0 <var_decl 0x4024a6c0 this>
-->             chain <decl_stmt 0x4024b4ec arg 0 <var_decl 0x4024a798 tv>
                    chain <compound_stmt 0x4024b500 tree_1 tree_2 tree_3
                        arg 0 <scope_stmt 0x4024b514 tree_0 tree_1 arg 0 <block
0x4024c140>
                        chain <compound_stmt 0x4024b528>>

Now the VAR_DECL of 'tv' has a constructor as its DECL_INITIAL, and the
constructor references the VAR_DECL of 'sec'.  But the declaration of 'sec' has
not yet been seen so the compiler invokes make_decl_rtl on a automatic variable
and aborts on the entry sanity check.

I'm not sure how this should work.  In the 3.3.x series, the DECL_STMT is not
inserted by tree-inline.c:declare_return_variable

  /* Build the declaration statement if FN does not return an
     aggregate.  */
  if (need_return_decl)
    return build_stmt (DECL_STMT, var);

because FN (the operator) is recognized as returning an aggregate.

In the 3.4.x series, this is not true because CALL_EXPR_HAS_RETURN_SLOT_ADDR
is not set by simplify_aggr_init_exprs_r/simplify_aggr_init_expr (there is no
AGGR_INIT_EXPR in the tree).  So it would appear that the front-end has
optimized away the aggregate-ness of struct A.

Maybe the fix is to remove the DECL_INITIAL when the RESULT_DECL is copied to
the VAR_DECL in cp_copy_res_decl_for_inlining.  Or maybe it is to treat the
operator as returning an aggregate like in the 3.3.x series.
Comment 4 Mark Mitchell 2004-06-22 08:03:22 UTC
Jason, this is a rather severe problem with interactions between NRV and inlining.  

Would you please take a look at this ASAP?
Comment 5 Wolfgang Bangerth 2004-06-22 13:55:03 UTC
This is slightly shorter, and still shows the same bug: 
---------------------- 
struct A {  
    int i;  
};  
  
inline A foo () {  
    int j = 1; 
    A a = { j }; 
    return a;  
} 
 
A tv = foo(); 
---------------------- 
 
g/x> /home/bangerth/bin/gcc-3.4-pre/bin/c++ -c x.cc -O2 
x.cc: In function `(static initializers for x.cc)': 
x.cc:5: internal compiler error: in make_decl_rtl, at varasm.c:752 
Please submit a full bug report, 
with preprocessed source if appropriate. 
See <URL:http://gcc.gnu.org/bugs.html> for instructions. 
 
W. 
Comment 6 Mark Mitchell 2004-06-25 21:25:40 UTC
Jason, are you going to be able to look at this problem?
Comment 7 Eric Botcazou 2004-07-08 11:32:27 UTC
*** Bug 16432 has been marked as a duplicate of this bug. ***
Comment 8 Andrew Pinski 2004-08-10 21:36:06 UTC
: Search converges between 2002-12-14-trunk (#159) and 2002-12-29-trunk (#160).
Comment 9 Andrew Pinski 2004-08-17 00:15:46 UTC
*** Bug 17054 has been marked as a duplicate of this bug. ***
Comment 10 Mark Mitchell 2004-08-17 21:10:36 UTC
Jason, this is a problem with NRV, so I've assigned it to you.
Comment 11 CVS Commits 2004-08-25 05:33:11 UTC
Subject: Bug 15461

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_4-branch
Changes by:	jason@gcc.gnu.org	2004-08-25 05:33:03

Modified files:
	gcc/cp         : ChangeLog tree.c semantics.c 

Log message:
	PR c++/16851
	* tree.c (stabilize_init): See through a COMPOUND_EXPR.
	
	PR c++/15461
	* semantics.c (nullify_returns_r): Replace a DECL_STMT
	for the NRV with an INIT_EXPR.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.3892.2.152&r2=1.3892.2.153
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/tree.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.360.4.8&r2=1.360.4.9
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/semantics.c.diff?cvsroot=gcc&only_with_tag=gcc-3_4-branch&r1=1.381.4.13&r2=1.381.4.14

Comment 12 Andrew Pinski 2004-08-25 05:52:33 UTC
Fixed.
Comment 13 CVS Commits 2004-08-25 15:16:49 UTC
Subject: Bug 15461

CVSROOT:	/cvs/gcc
Module name:	gcc
Changes by:	jason@gcc.gnu.org	2004-08-25 15:16:37

Added files:
	gcc/testsuite/g++.dg/opt: nrv7.C 

Log message:
	PR c++/15461
	* semantics.c (nullify_returns_r): Replace a DECL_STMT
	for the NRV with an INIT_EXPR.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/opt/nrv7.C.diff?cvsroot=gcc&r1=NONE&r2=1.1

Comment 14 CVS Commits 2004-09-13 08:39:34 UTC
Subject: Bug 15461

CVSROOT:	/cvs/gcc
Module name:	gcc
Branch: 	gcc-3_3-branch
Changes by:	gdr@gcc.gnu.org	2004-09-13 08:39:17

Modified files:
	gcc/cp         : ChangeLog semantics.c 
Added files:
	gcc/testsuite/g++.dg/opt: nrv7.C 

Log message:
	PR c++/15857
	Backport from gcc-3_4-branch
	2004-08-24  Jason Merrill  <jason@redhat.com>
	PR c++/15461
	* semantics.c (nullify_returns_r): Replace a DECL_STMT
	for the NRV with an INIT_EXPR.

Patches:
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/ChangeLog.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.3076.2.273&r2=1.3076.2.274
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/cp/semantics.c.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=1.282.4.6&r2=1.282.4.7
http://gcc.gnu.org/cgi-bin/cvsweb.cgi/gcc/gcc/testsuite/g++.dg/opt/nrv7.C.diff?cvsroot=gcc&only_with_tag=gcc-3_3-branch&r1=NONE&r2=1.1.10.1