2 Copyright (C) 2014-2015 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"
60 #include "plugin-api.h"
61 #include "hard-reg-set.h"
66 #include "tree-pass.h"
68 /* Main dataflow loop propagating comdat groups across
69 the symbol table. All references to SYMBOL are examined
70 and NEWGROUP is updated accordingly. MAP holds current lattice
71 values for individual symbols. */
74 propagate_comdat_group (struct symtab_node
*symbol
,
75 tree newgroup
, hash_map
<symtab_node
*, tree
> &map
)
80 /* Walk all references to SYMBOL, recursively dive into aliases. */
83 symbol
->iterate_referring (i
, ref
)
84 && newgroup
!= error_mark_node
; i
++)
86 struct symtab_node
*symbol2
= ref
->referring
;
88 if (ref
->use
== IPA_REF_ALIAS
)
90 newgroup
= propagate_comdat_group (symbol2
, newgroup
, map
);
94 /* One COMDAT group can not hold both variables and functions at
95 a same time. For now we just go to BOTTOM, in future we may
96 invent special comdat groups for this case. */
98 if (symbol
->type
!= symbol2
->type
)
100 newgroup
= error_mark_node
;
104 /* If we see inline clone, its comdat group actually
105 corresponds to the comdat group of the function it is inlined
108 if (cgraph_node
* cn
= dyn_cast
<cgraph_node
*> (symbol2
))
110 if (cn
->global
.inlined_to
)
111 symbol2
= cn
->global
.inlined_to
;
114 /* The actual merge operation. */
116 tree
*val2
= map
.get (symbol2
);
118 if (val2
&& *val2
!= newgroup
)
123 newgroup
= error_mark_node
;
127 /* If we analyze function, walk also callers. */
129 cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (symbol
);
132 for (struct cgraph_edge
* edge
= cnode
->callers
;
133 edge
&& newgroup
!= error_mark_node
; edge
= edge
->next_caller
)
135 struct symtab_node
*symbol2
= edge
->caller
;
137 if (cgraph_node
* cn
= dyn_cast
<cgraph_node
*> (symbol2
))
139 /* Thunks can not call across section boundary. */
140 if (cn
->thunk
.thunk_p
)
141 newgroup
= propagate_comdat_group (symbol2
, newgroup
, map
);
142 /* If we see inline clone, its comdat group actually
143 corresponds to the comdat group of the function it
145 if (cn
->global
.inlined_to
)
146 symbol2
= cn
->global
.inlined_to
;
149 /* The actual merge operation. */
151 tree
*val2
= map
.get (symbol2
);
153 if (val2
&& *val2
!= newgroup
)
158 newgroup
= error_mark_node
;
165 /* Add all references of SYMBOL that are defined into queue started by FIRST
166 and linked by AUX pointer (unless they are already enqueued).
167 Walk recursively inlined functions. */
170 enqueue_references (symtab_node
**first
,
174 struct ipa_ref
*ref
= NULL
;
176 for (i
= 0; symbol
->iterate_reference (i
, ref
); i
++)
178 symtab_node
*node
= ref
->referred
->ultimate_alias_target ();
180 /* Always keep thunks in same sections as target function. */
181 if (is_a
<cgraph_node
*>(node
))
182 node
= dyn_cast
<cgraph_node
*> (node
)->function_symbol ();
183 if (!node
->aux
&& node
->definition
)
190 if (cgraph_node
*cnode
= dyn_cast
<cgraph_node
*> (symbol
))
192 struct cgraph_edge
*edge
;
194 for (edge
= cnode
->callees
; edge
; edge
= edge
->next_callee
)
195 if (!edge
->inline_failed
)
196 enqueue_references (first
, edge
->callee
);
199 symtab_node
*node
= edge
->callee
->ultimate_alias_target ();
201 /* Always keep thunks in same sections as target function. */
202 if (is_a
<cgraph_node
*>(node
))
203 node
= dyn_cast
<cgraph_node
*> (node
)->function_symbol ();
204 if (!node
->aux
&& node
->definition
)
213 /* Set comdat group of SYMBOL to GROUP.
214 Callback for for_node_and_aliases. */
217 set_comdat_group (symtab_node
*symbol
,
220 symtab_node
*head
= (symtab_node
*)head_p
;
222 gcc_assert (!symbol
->get_comdat_group ());
223 symbol
->set_comdat_group (head
->get_comdat_group ());
224 symbol
->add_to_same_comdat_group (head
);
228 /* Set comdat group of SYMBOL to GROUP.
229 Callback for for_node_thunks_and_aliases. */
232 set_comdat_group_1 (cgraph_node
*symbol
,
235 return set_comdat_group (symbol
, head_p
);
238 /* The actual pass with the main dataflow loop. */
243 hash_map
<symtab_node
*, tree
> map (251);
244 hash_map
<tree
, symtab_node
*> comdat_head_map (251);
246 bool comdat_group_seen
= false;
247 symtab_node
*first
= (symtab_node
*) (void *) 1;
250 /* Start the dataflow by assigning comdat group to symbols that are in comdat
251 groups already. All other externally visible symbols must stay, we use
252 ERROR_MARK_NODE as bottom for the propagation. */
254 FOR_EACH_DEFINED_SYMBOL (symbol
)
255 if (!symbol
->real_symbol_p ())
257 else if ((group
= symbol
->get_comdat_group ()) != NULL
)
259 map
.put (symbol
, group
);
260 comdat_head_map
.put (group
, symbol
);
261 comdat_group_seen
= true;
263 /* Mark the symbol so we won't waste time visiting it for dataflow. */
264 symbol
->aux
= (symtab_node
*) (void *) 1;
266 /* See symbols that can not be privatized to comdats; that is externally
267 visible symbols or otherwise used ones. We also do not want to mangle
268 user section names. */
269 else if (symbol
->externally_visible
270 || symbol
->force_output
271 || symbol
->used_from_other_partition
272 || TREE_THIS_VOLATILE (symbol
->decl
)
273 || symbol
->get_section ()
274 || (TREE_CODE (symbol
->decl
) == FUNCTION_DECL
275 && (DECL_STATIC_CONSTRUCTOR (symbol
->decl
)
276 || DECL_STATIC_DESTRUCTOR (symbol
->decl
))))
278 symtab_node
*target
= symbol
->ultimate_alias_target ();
280 /* Always keep thunks in same sections as target function. */
281 if (is_a
<cgraph_node
*>(target
))
282 target
= dyn_cast
<cgraph_node
*> (target
)->function_symbol ();
283 map
.put (target
, error_mark_node
);
285 /* Mark the symbol so we won't waste time visiting it for dataflow. */
286 symbol
->aux
= (symtab_node
*) (void *) 1;
290 /* Enqueue symbol for dataflow. */
295 if (!comdat_group_seen
)
297 FOR_EACH_DEFINED_SYMBOL (symbol
)
302 /* The actual dataflow. */
304 while (first
!= (void *) 1)
310 first
= (symtab_node
*)first
->aux
;
312 /* Get current lattice value of SYMBOL. */
313 val
= map
.get (symbol
);
317 /* If it is bottom, there is nothing to do; do not clear AUX
318 so we won't re-queue the symbol. */
319 if (group
== error_mark_node
)
322 newgroup
= propagate_comdat_group (symbol
, group
, map
);
324 /* If nothing changed, proceed to next symbol. */
325 if (newgroup
== group
)
331 /* Update lattice value and enqueue all references for re-visiting. */
332 gcc_assert (newgroup
);
336 map
.put (symbol
, newgroup
);
337 enqueue_references (&first
, symbol
);
339 /* We may need to revisit the symbol unless it is BOTTOM. */
340 if (newgroup
!= error_mark_node
)
344 /* Finally assign symbols to the sections. */
346 FOR_EACH_DEFINED_SYMBOL (symbol
)
348 struct cgraph_node
*fun
;
350 if (!symbol
->get_comdat_group ()
352 && (!(fun
= dyn_cast
<cgraph_node
*> (symbol
))
353 || !fun
->thunk
.thunk_p
)
354 && symbol
->real_symbol_p ())
356 tree
*val
= map
.get (symbol
);
358 /* A NULL here means that SYMBOL is unreachable in the definition
359 of ipa-comdats. Either ipa-comdats is wrong about this or someone
360 forgot to cleanup and remove unreachable functions earlier. */
365 if (group
== error_mark_node
)
369 fprintf (dump_file
, "Localizing symbol\n");
370 symbol
->dump (dump_file
);
371 fprintf (dump_file
, "To group: %s\n", IDENTIFIER_POINTER (group
));
373 if (is_a
<cgraph_node
*> (symbol
))
374 dyn_cast
<cgraph_node
*>(symbol
)->call_for_symbol_thunks_and_aliases
376 *comdat_head_map
.get (group
),
379 symbol
->call_for_symbol_and_aliases
381 *comdat_head_map
.get (group
),
390 const pass_data pass_data_ipa_comdats
=
393 "comdats", /* name */
394 OPTGROUP_NONE
, /* optinfo_flags */
395 TV_IPA_COMDATS
, /* tv_id */
396 0, /* properties_required */
397 0, /* properties_provided */
398 0, /* properties_destroyed */
399 0, /* todo_flags_start */
400 0, /* todo_flags_finish */
403 class pass_ipa_comdats
: public ipa_opt_pass_d
406 pass_ipa_comdats (gcc::context
*ctxt
)
407 : ipa_opt_pass_d (pass_data_ipa_comdats
, ctxt
,
408 NULL
, /* generate_summary */
409 NULL
, /* write_summary */
410 NULL
, /* read_summary */
411 NULL
, /* write_optimization_summary */
412 NULL
, /* read_optimization_summary */
413 NULL
, /* stmt_fixup */
414 0, /* function_transform_todo_flags_start */
415 NULL
, /* function_transform */
416 NULL
) /* variable_transform */
419 /* opt_pass methods: */
420 virtual bool gate (function
*);
421 virtual unsigned int execute (function
*) { return ipa_comdats (); }
423 }; // class pass_ipa_comdats
426 pass_ipa_comdats::gate (function
*)
434 make_pass_ipa_comdats (gcc::context
*ctxt
)
436 return new pass_ipa_comdats (ctxt
);