2 Copyright (C) 2014-2022 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 exclusively 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"
56 #include "tree-pass.h"
59 /* Main dataflow loop propagating comdat groups across
60 the symbol table. All references to SYMBOL are examined
61 and NEWGROUP is updated accordingly. MAP holds current lattice
62 values for individual symbols. */
65 propagate_comdat_group (struct symtab_node
*symbol
,
66 tree newgroup
, hash_map
<symtab_node
*, tree
> &map
)
71 /* Walk all references to SYMBOL, recursively dive into aliases. */
74 symbol
->iterate_referring (i
, ref
)
75 && newgroup
!= error_mark_node
; i
++)
77 struct symtab_node
*symbol2
= ref
->referring
;
79 if (ref
->use
== IPA_REF_ALIAS
)
81 newgroup
= propagate_comdat_group (symbol2
, newgroup
, map
);
85 /* One COMDAT group cannot hold both variables and functions at
86 a same time. For now we just go to BOTTOM, in future we may
87 invent special comdat groups for this case. */
89 if (symbol
->type
!= symbol2
->type
)
91 newgroup
= error_mark_node
;
95 /* If we see inline clone, its comdat group actually
96 corresponds to the comdat group of the function it is inlined
99 if (cgraph_node
* cn
= dyn_cast
<cgraph_node
*> (symbol2
))
102 symbol2
= cn
->inlined_to
;
105 /* The actual merge operation. */
107 tree
*val2
= map
.get (symbol2
);
109 if (val2
&& *val2
!= newgroup
)
114 newgroup
= error_mark_node
;
118 /* If we analyze function, walk also callers. */
120 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (symbol
);
123 for (struct cgraph_edge
* edge
= cnode
->callers
;
124 edge
&& newgroup
!= error_mark_node
; edge
= edge
->next_caller
)
126 struct symtab_node
*symbol2
= edge
->caller
;
128 if (cgraph_node
* cn
= dyn_cast
<cgraph_node
*> (symbol2
))
130 /* Thunks cannot call across section boundary. */
132 newgroup
= propagate_comdat_group (symbol2
, newgroup
, map
);
133 /* If we see inline clone, its comdat group actually
134 corresponds to the comdat group of the function it
137 symbol2
= cn
->inlined_to
;
140 /* The actual merge operation. */
142 tree
*val2
= map
.get (symbol2
);
144 if (val2
&& *val2
!= newgroup
)
149 newgroup
= error_mark_node
;
156 /* Add all references of SYMBOL that are defined into queue started by FIRST
157 and linked by AUX pointer (unless they are already enqueued).
158 Walk recursively inlined functions. */
161 enqueue_references (symtab_node
**first
,
165 struct ipa_ref
*ref
= NULL
;
167 for (i
= 0; symbol
->iterate_reference (i
, ref
); i
++)
169 symtab_node
*node
= ref
->referred
->ultimate_alias_target ();
171 /* Always keep thunks in same sections as target function. */
172 if (is_a
<cgraph_node
*>(node
))
173 node
= dyn_cast
<cgraph_node
*> (node
)->function_symbol ();
174 if (!node
->aux
&& node
->definition
)
181 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (symbol
))
183 struct cgraph_edge
*edge
;
185 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
186 if (!edge
->inline_failed
)
187 enqueue_references (first
, edge
->callee
);
190 symtab_node
*node
= edge
->callee
->ultimate_alias_target ();
192 /* Always keep thunks in same sections as target function. */
193 if (is_a
<cgraph_node
*>(node
))
194 node
= dyn_cast
<cgraph_node
*> (node
)->function_symbol ();
195 if (!node
->aux
&& node
->definition
)
204 /* Set comdat group of SYMBOL to GROUP.
205 Callback for for_node_and_aliases. */
208 set_comdat_group (symtab_node
*symbol
,
211 symtab_node
*head
= (symtab_node
*)head_p
;
213 gcc_assert (!symbol
->get_comdat_group ());
214 if (symbol
->real_symbol_p ())
216 symbol
->set_comdat_group (head
->get_comdat_group ());
217 symbol
->add_to_same_comdat_group (head
);
222 /* Set comdat group of SYMBOL to GROUP.
223 Callback for for_node_thunks_and_aliases. */
226 set_comdat_group_1 (cgraph_node
*symbol
,
229 return set_comdat_group (symbol
, head_p
);
232 /* The actual pass with the main dataflow loop. */
237 hash_map
<symtab_node
*, tree
> map (251);
238 hash_map
<tree
, symtab_node
*> comdat_head_map (251);
240 bool comdat_group_seen
= false;
241 symtab_node
*first
= (symtab_node
*) (void *) 1;
244 /* Start the dataflow by assigning comdat group to symbols that are in comdat
245 groups already. All other externally visible symbols must stay, we use
246 ERROR_MARK_NODE as bottom for the propagation. */
248 FOR_EACH_DEFINED_SYMBOL (symbol
)
249 if (!symbol
->real_symbol_p ())
251 else if ((group
= symbol
->get_comdat_group ()) != NULL
)
253 map
.put (symbol
, group
);
254 comdat_head_map
.put (group
, symbol
);
255 comdat_group_seen
= true;
257 /* Mark the symbol so we won't waste time visiting it for dataflow. */
258 symbol
->aux
= (symtab_node
*) (void *) 1;
260 /* See symbols that cannot be privatized to comdats; that is externally
261 visible symbols or otherwise used ones. We also do not want to mangle
262 user section names. */
263 else if (symbol
->externally_visible
264 || symbol
->force_output
265 || symbol
->used_from_other_partition
266 || TREE_THIS_VOLATILE (symbol
->decl
)
267 || symbol
->get_section ()
268 || (TREE_CODE (symbol
->decl
) == FUNCTION_DECL
269 && (DECL_STATIC_CONSTRUCTOR (symbol
->decl
)
270 || DECL_STATIC_DESTRUCTOR (symbol
->decl
))))
272 symtab_node
*target
= symbol
->ultimate_alias_target ();
274 /* Always keep thunks in same sections as target function. */
275 if (is_a
<cgraph_node
*>(target
))
276 target
= dyn_cast
<cgraph_node
*> (target
)->function_symbol ();
277 map
.put (target
, error_mark_node
);
279 /* Mark the symbol so we won't waste time visiting it for dataflow. */
280 symbol
->aux
= (symtab_node
*) (void *) 1;
284 /* Enqueue symbol for dataflow. */
289 if (!comdat_group_seen
)
291 FOR_EACH_DEFINED_SYMBOL (symbol
)
296 /* The actual dataflow. */
298 while (first
!= (void *) 1)
304 first
= (symtab_node
*)first
->aux
;
306 /* Get current lattice value of SYMBOL. */
307 val
= map
.get (symbol
);
311 /* If it is bottom, there is nothing to do; do not clear AUX
312 so we won't re-queue the symbol. */
313 if (group
== error_mark_node
)
316 newgroup
= propagate_comdat_group (symbol
, group
, map
);
318 /* If nothing changed, proceed to next symbol. */
319 if (newgroup
== group
)
325 /* Update lattice value and enqueue all references for re-visiting. */
326 gcc_assert (newgroup
);
330 map
.put (symbol
, newgroup
);
331 enqueue_references (&first
, symbol
);
333 /* We may need to revisit the symbol unless it is BOTTOM. */
334 if (newgroup
!= error_mark_node
)
338 /* Finally assign symbols to the sections. */
340 FOR_EACH_DEFINED_SYMBOL (symbol
)
342 struct cgraph_node
*fun
;
344 if (!symbol
->get_comdat_group ()
346 && (!(fun
= dyn_cast
<cgraph_node
*> (symbol
))
348 && symbol
->real_symbol_p ())
350 tree
*val
= map
.get (symbol
);
352 /* A NULL here means that SYMBOL is unreachable in the definition
353 of ipa-comdats. Either ipa-comdats is wrong about this or someone
354 forgot to cleanup and remove unreachable functions earlier. */
359 if (group
== error_mark_node
)
363 fprintf (dump_file
, "Localizing symbol\n");
364 symbol
->dump (dump_file
);
365 fprintf (dump_file
, "To group: %s\n", IDENTIFIER_POINTER (group
));
367 if (is_a
<cgraph_node
*> (symbol
))
368 dyn_cast
<cgraph_node
*>(symbol
)->call_for_symbol_thunks_and_aliases
370 *comdat_head_map
.get (group
),
373 symbol
->call_for_symbol_and_aliases
375 *comdat_head_map
.get (group
),
381 /* Recompute calls comdat local flag. This need to be done after all changes
383 cgraph_node
*function
;
384 FOR_EACH_DEFINED_FUNCTION (function
)
385 if (function
->get_comdat_group ())
386 function
->calls_comdat_local
= function
->check_calls_comdat_local_p ();
393 const pass_data pass_data_ipa_comdats
=
396 "comdats", /* name */
397 OPTGROUP_NONE
, /* optinfo_flags */
398 TV_IPA_COMDATS
, /* tv_id */
399 0, /* properties_required */
400 0, /* properties_provided */
401 0, /* properties_destroyed */
402 0, /* todo_flags_start */
403 0, /* todo_flags_finish */
406 class pass_ipa_comdats
: public ipa_opt_pass_d
409 pass_ipa_comdats (gcc::context
*ctxt
)
410 : ipa_opt_pass_d (pass_data_ipa_comdats
, ctxt
,
411 NULL
, /* generate_summary */
412 NULL
, /* write_summary */
413 NULL
, /* read_summary */
414 NULL
, /* write_optimization_summary */
415 NULL
, /* read_optimization_summary */
416 NULL
, /* stmt_fixup */
417 0, /* function_transform_todo_flags_start */
418 NULL
, /* function_transform */
419 NULL
) /* variable_transform */
422 /* opt_pass methods: */
423 bool gate (function
*) final override
;
424 unsigned int execute (function
*) final override
{ return ipa_comdats (); }
426 }; // class pass_ipa_comdats
429 pass_ipa_comdats::gate (function
*)
431 return HAVE_COMDAT_GROUP
;
437 make_pass_ipa_comdats (gcc::context
*ctxt
)
439 return new pass_ipa_comdats (ctxt
);