2 Copyright (C) 2014 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 /* This is very simple pass that looks for static symbols that are used
21 exlusively by symbol within one comdat group. In this case it makes
22 sense to bring the symbol itself into the group to avoid dead code
23 that would arrise when the comdat group from current unit is replaced
24 by a different copy. Consider for example:
35 if Q is used only by T, it makes sense to put Q into T's comdat group.
37 The pass solve simple dataflow across the callgraph trying to prove what
38 symbols are used exclusively from a given comdat group.
40 The implementation maintains a queue linked by AUX pointer terminated by
41 pointer value 1. Lattice values are NULL for TOP, actual comdat group, or
42 ERROR_MARK_NODE for bottom.
44 TODO: When symbol is used only by comdat symbols, but from different groups,
45 it would make sense to produce a new comdat group for it with anonymous name.
47 TODO2: We can't mix variables and functions within one group. Currently
48 we just give up on references of symbols of different types. We also should
49 handle this by anonymous comdat group section. */
53 #include "coretypes.h"
57 #include "tree-pass.h"
60 /* Main dataflow loop propagating comdat groups across
61 the symbol table. All references to SYMBOL are examined
62 and NEWGROUP is updated accordingly. MAP holds current lattice
63 values for individual symbols. */
66 propagate_comdat_group (struct symtab_node
*symbol
,
67 tree newgroup
, hash_map
<symtab_node
*, tree
> &map
)
72 /* Walk all references to SYMBOL, recursively dive into aliases. */
75 symbol
->iterate_referring (i
, ref
)
76 && newgroup
!= error_mark_node
; i
++)
78 struct symtab_node
*symbol2
= ref
->referring
;
80 if (ref
->use
== IPA_REF_ALIAS
)
82 newgroup
= propagate_comdat_group (symbol2
, newgroup
, map
);
86 /* One COMDAT group can not hold both variables and functions at
87 a same time. For now we just go to BOTTOM, in future we may
88 invent special comdat groups for this case. */
90 if (symbol
->type
!= symbol2
->type
)
92 newgroup
= error_mark_node
;
96 /* If we see inline clone, its comdat group actually
97 corresponds to the comdat group of the function it is inlined
100 if (cgraph_node
* cn
= dyn_cast
<cgraph_node
*> (symbol2
))
102 if (cn
->global
.inlined_to
)
103 symbol2
= cn
->global
.inlined_to
;
106 /* The actual merge operation. */
108 tree
*val2
= map
.get (symbol2
);
110 if (val2
&& *val2
!= newgroup
)
115 newgroup
= error_mark_node
;
119 /* If we analyze function, walk also callers. */
121 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (symbol
);
124 for (struct cgraph_edge
* edge
= cnode
->callers
;
125 edge
&& newgroup
!= error_mark_node
; edge
= edge
->next_caller
)
127 struct symtab_node
*symbol2
= edge
->caller
;
129 /* If we see inline clone, its comdat group actually
130 corresponds to the comdat group of the function it is inlined
133 if (cgraph_node
* cn
= dyn_cast
<cgraph_node
*> (symbol2
))
135 if (cn
->global
.inlined_to
)
136 symbol2
= cn
->global
.inlined_to
;
139 /* The actual merge operation. */
141 tree
*val2
= map
.get (symbol2
);
143 if (val2
&& *val2
!= newgroup
)
148 newgroup
= error_mark_node
;
155 /* Add all references of SYMBOL that are defined into queue started by FIRST
156 and linked by AUX pointer (unless they are already enqueued).
157 Walk recursively inlined functions. */
160 enqueue_references (symtab_node
**first
,
164 struct ipa_ref
*ref
= NULL
;
166 for (i
= 0; symbol
->iterate_reference (i
, ref
); i
++)
168 symtab_node
*node
= ref
->referred
->ultimate_alias_target ();
169 if (!node
->aux
&& node
->definition
)
176 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (symbol
))
178 struct cgraph_edge
*edge
;
180 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
181 if (!edge
->inline_failed
)
182 enqueue_references (first
, edge
->callee
);
185 symtab_node
*node
= edge
->callee
->ultimate_alias_target ();
186 if (!node
->aux
&& node
->definition
)
195 /* Set comdat group of SYMBOL to GROUP.
196 Callback for symtab_for_node_and_aliases. */
199 set_comdat_group (symtab_node
*symbol
,
202 symtab_node
*head
= (symtab_node
*)head_p
;
204 gcc_assert (!symbol
->get_comdat_group ());
205 symbol
->set_comdat_group (head
->get_comdat_group ());
206 symbol
->add_to_same_comdat_group (head
);
210 /* The actual pass with the main dataflow loop. */
215 hash_map
<symtab_node
*, tree
> map (251);
216 hash_map
<tree
, symtab_node
*> comdat_head_map (251);
218 bool comdat_group_seen
= false;
219 symtab_node
*first
= (symtab_node
*) (void *) 1;
222 /* Start the dataflow by assigning comdat group to symbols that are in comdat
223 groups already. All other externally visible symbols must stay, we use
224 ERROR_MARK_NODE as bottom for the propagation. */
226 FOR_EACH_DEFINED_SYMBOL (symbol
)
227 if (!symbol
->real_symbol_p ())
229 else if ((group
= symbol
->get_comdat_group ()) != NULL
)
231 map
.put (symbol
, group
);
232 comdat_head_map
.put (group
, symbol
);
233 comdat_group_seen
= true;
235 /* Mark the symbol so we won't waste time visiting it for dataflow. */
236 symbol
->aux
= (symtab_node
*) (void *) 1;
238 /* See symbols that can not be privatized to comdats; that is externally
239 visible symbols or otherwise used ones. We also do not want to mangle
240 user section names. */
241 else if (symbol
->externally_visible
242 || symbol
->force_output
243 || symbol
->used_from_other_partition
244 || TREE_THIS_VOLATILE (symbol
->decl
)
245 || symbol
->get_section ()
246 || (TREE_CODE (symbol
->decl
) == FUNCTION_DECL
247 && (DECL_STATIC_CONSTRUCTOR (symbol
->decl
)
248 || DECL_STATIC_DESTRUCTOR (symbol
->decl
))))
250 map
.put (symbol
->ultimate_alias_target (), error_mark_node
);
252 /* Mark the symbol so we won't waste time visiting it for dataflow. */
253 symbol
->aux
= (symtab_node
*) (void *) 1;
257 /* Enqueue symbol for dataflow. */
262 if (!comdat_group_seen
)
264 FOR_EACH_DEFINED_SYMBOL (symbol
)
269 /* The actual dataflow. */
271 while (first
!= (void *) 1)
277 first
= (symtab_node
*)first
->aux
;
279 /* Get current lattice value of SYMBOL. */
280 val
= map
.get (symbol
);
284 /* If it is bottom, there is nothing to do; do not clear AUX
285 so we won't re-queue the symbol. */
286 if (group
== error_mark_node
)
289 newgroup
= propagate_comdat_group (symbol
, group
, map
);
291 /* If nothing changed, proceed to next symbol. */
292 if (newgroup
== group
)
298 /* Update lattice value and enqueue all references for re-visiting. */
299 gcc_assert (newgroup
);
303 map
.put (symbol
, newgroup
);
304 enqueue_references (&first
, symbol
);
306 /* We may need to revisit the symbol unless it is BOTTOM. */
307 if (newgroup
!= error_mark_node
)
311 /* Finally assign symbols to the sections. */
313 FOR_EACH_DEFINED_SYMBOL (symbol
)
316 if (!symbol
->get_comdat_group ()
318 && symbol
->real_symbol_p ())
320 tree group
= *map
.get (symbol
);
322 if (group
== error_mark_node
)
326 fprintf (dump_file
, "Localizing symbol\n");
327 symbol
->dump (dump_file
);
328 fprintf (dump_file
, "To group: %s\n", IDENTIFIER_POINTER (group
));
330 symbol
->call_for_symbol_and_aliases (set_comdat_group
,
331 *comdat_head_map
.get (group
),
340 const pass_data pass_data_ipa_comdats
=
343 "comdats", /* name */
344 OPTGROUP_NONE
, /* optinfo_flags */
345 TV_IPA_COMDATS
, /* tv_id */
346 0, /* properties_required */
347 0, /* properties_provided */
348 0, /* properties_destroyed */
349 0, /* todo_flags_start */
350 0, /* todo_flags_finish */
353 class pass_ipa_comdats
: public ipa_opt_pass_d
356 pass_ipa_comdats (gcc::context
*ctxt
)
357 : ipa_opt_pass_d (pass_data_ipa_comdats
, ctxt
,
358 NULL
, /* generate_summary */
359 NULL
, /* write_summary */
360 NULL
, /* read_summary */
361 NULL
, /* write_optimization_summary */
362 NULL
, /* read_optimization_summary */
363 NULL
, /* stmt_fixup */
364 0, /* function_transform_todo_flags_start */
365 NULL
, /* function_transform */
366 NULL
) /* variable_transform */
369 /* opt_pass methods: */
370 virtual bool gate (function
*);
371 virtual unsigned int execute (function
*) { return ipa_comdats (); }
373 }; // class pass_ipa_comdats
376 pass_ipa_comdats::gate (function
*)
384 make_pass_ipa_comdats (gcc::context
*ctxt
)
386 return new pass_ipa_comdats (ctxt
);