Bug 17736 - Optimization for global initialization with empty constructors
Summary: Optimization for global initialization with empty constructors
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: middle-end (show other bugs)
Version: 4.0.0
: P2 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: missed-optimization
: 36310 (view as bug list)
Depends on:
Blocks:
 
Reported: 2004-09-29 19:35 UTC by Matt Austern
Modified: 2010-08-21 09:46 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-02-05 21:22:33


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Matt Austern 2004-09-29 19:35:20 UTC
Consider the following file:
struct X { X() { } };
struct Y { Y() { } };
X x1, x2, x3;
Y y1, y2, y3;

Compiling this to assembly code with -Os, we can see that the compiler is correctly optimizing away 
the do-nothing constructors: nothing is being done for any of these six global objects.  However, the 
compiler still generates a do-nothing mod_init function.  The relevant part of the assembly file is:
.mod_init_func
        .align 2
        .long   __GLOBAL__I_x1
...
__GLOBAL__I_x1:
LFB9:
        blr
LFE9:

At least we're only getting one do-nothing function per translation unit, instead of one per class or one 
per global.  But it would be nice if we didn't get it at all.
Comment 1 Wolfgang Bangerth 2004-09-29 20:14:37 UTC
Confirmed. The same holds for x86-linux, so I remove the target fields 
for this PR. 
 
W. 
Comment 2 Andrew Pinski 2004-09-29 22:26:03 UTC
The same can be true of the C code too, I reported this once before for C in a another bug which 
caused an ICE for some target.
Comment 3 Mike Stump 2007-12-06 23:46:47 UTC
radr://2961456
Comment 4 Andrew Pinski 2008-05-28 08:38:07 UTC
These constructors are not trivial as defined by the C++ standard :).
Comment 5 Andrew Pinski 2008-05-28 08:38:37 UTC
*** Bug 36310 has been marked as a duplicate of this bug. ***
Comment 6 Jan Hubicka 2010-08-21 09:46:34 UTC
Subject: Bug 17736

Author: hubicka
Date: Sat Aug 21 09:46:15 2010
New Revision: 163439

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=163439
Log:

	PR c++/45307
	PR c++/17736
	* cgraph.h (cgraph_only_called_directly_p,
	cgraph_can_remove_if_no_direct_calls_and_refs_p): Handle
	static cdtors.
	* cgraphunit.c (cgraph_decide_is_function_needed): Static cdtors
	are not needed.
	(cgraph_finalize_function): Static cdtors are reachable.
	(cgraph_mark_functions_to_output): Use cgraph_only_called_directly_p.

	* gcc.dg/ipa/ctor-empty-1.c: Add testcase.
	* g++.dg/tree-ssa/empty-2.C: Check that constructor got optimized out.


Added:
    trunk/gcc/testsuite/gcc.dg/ipa/ctor-empty-1.c
Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/cgraph.c
    trunk/gcc/cgraph.h
    trunk/gcc/cgraphunit.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/tree-ssa/empty-2.C

Comment 7 Jan Hubicka 2010-08-21 09:46:56 UTC
We now remove pure constructors