http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51663
--- Comment #6 from Jan Hubicka <hubicka at gcc dot gnu.org> 2012-03-24 21:39:16 UTC --- OK, this should regularize the situation somewhat. We handle the -O0/-fno-toplevel-reorder semantics early at finalization time and thus we don't care about effect of -fwhole-program and friends. * varpool.c (varpool_finalize_decl): Handle toplevel_reorder here. (decide_is_variable_needed): Do not handle toplevel reorder here. * cgraph.h (varpool_can_remove_if_no_refs): Likewise. Index: varpool.c =================================================================== --- varpool.c (revision 183666) +++ varpool.c (working copy) @@ -334,10 +334,6 @@ decide_is_variable_needed (struct varpoo && !DECL_EXTERNAL (decl)) return true; - /* When not reordering top level variables, we have to assume that - we are going to keep everything. */ - if (!flag_toplevel_reorder) - return true; return false; } @@ -405,7 +401,11 @@ varpool_finalize_decl (tree decl) if (node->needed) varpool_enqueue_needed_node (node); node->finalized = true; - if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl)) + if (TREE_THIS_VOLATILE (decl) || DECL_PRESERVE_P (decl) + /* Traditionally we do not eliminate static variables when not + optimizing and when not doing toplevel reoder. */ + || (!flag_toplevel_reorder && !DECL_COMDAT (node->decl) + && !DECL_ARTIFICIAL (node->decl))) node->force_output = true; if (decide_is_variable_needed (node, decl)) Index: cgraph.h =================================================================== --- cgraph.h (revision 183666) +++ cgraph.h (working copy) @@ -947,8 +947,6 @@ static inline bool varpool_can_remove_if_no_refs (struct varpool_node *node) { return (!node->force_output && !node->used_from_other_partition - && (flag_toplevel_reorder || DECL_COMDAT (node->decl) - || DECL_ARTIFICIAL (node->decl)) && (DECL_COMDAT (node->decl) || !node->externally_visible)); }