* config/xtensa/predicates.md (call expander): Update for
[official-gcc.git] / gcc / ipa-comdats.c
blob7aa7ccf9f391ab01b3f0cf3b10aee4fbc7896069
1 /* Localize comdats.
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
9 version.
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
14 for more details.
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:
26 static int q(void)
28 ....
30 inline int t(void)
32 return q();
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. */
51 #include "config.h"
52 #include "system.h"
53 #include "coretypes.h"
54 #include "tm.h"
55 #include "tree.h"
56 #include "cgraph.h"
57 #include "tree-pass.h"
58 #include "hash-map.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. */
65 tree
66 propagate_comdat_group (struct symtab_node *symbol,
67 tree newgroup, hash_map<symtab_node *, tree> &map)
69 int i;
70 struct ipa_ref *ref;
72 /* Walk all references to SYMBOL, recursively dive into aliases. */
74 for (i = 0;
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);
83 continue;
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;
93 break;
96 /* If we see inline clone, its comdat group actually
97 corresponds to the comdat group of the function it is inlined
98 to. */
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)
112 if (!newgroup)
113 newgroup = *val2;
114 else
115 newgroup = error_mark_node;
119 /* If we analyze function, walk also callers. */
121 cgraph_node *cnode = dyn_cast <cgraph_node *> (symbol);
123 if (cnode)
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
131 to. */
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)
145 if (!newgroup)
146 newgroup = *val2;
147 else
148 newgroup = error_mark_node;
151 return newgroup;
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. */
159 void
160 enqueue_references (symtab_node **first,
161 symtab_node *symbol)
163 int i;
164 struct ipa_ref *ref = NULL;
166 for (i = 0; symbol->iterate_reference (i, ref); i++)
168 symtab_node *node = symtab_alias_ultimate_target (ref->referred, NULL);
169 if (!node->aux && node->definition)
171 node->aux = *first;
172 *first = node;
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);
183 else
185 symtab_node *node = symtab_alias_ultimate_target (edge->callee,
186 NULL);
187 if (!node->aux && node->definition)
189 node->aux = *first;
190 *first = node;
196 /* Set comdat group of SYMBOL to GROUP.
197 Callback for symtab_for_node_and_aliases. */
199 bool
200 set_comdat_group (symtab_node *symbol,
201 void *head_p)
203 symtab_node *head = (symtab_node *)head_p;
205 gcc_assert (!symbol->get_comdat_group ());
206 symbol->set_comdat_group (head->get_comdat_group ());
207 symtab_add_to_same_comdat_group (symbol, head);
208 return false;
211 /* The actual pass with the main dataflow loop. */
213 static unsigned int
214 ipa_comdats (void)
216 hash_map<symtab_node *, tree> map (251);
217 hash_map<tree, symtab_node *> comdat_head_map (251);
218 symtab_node *symbol;
219 bool comdat_group_seen = false;
220 symtab_node *first = (symtab_node *) (void *) 1;
221 tree group;
223 /* Start the dataflow by assigning comdat group to symbols that are in comdat
224 groups already. All other externally visible symbols must stay, we use
225 ERROR_MARK_NODE as bottom for the propagation. */
227 FOR_EACH_DEFINED_SYMBOL (symbol)
228 if (!symtab_real_symbol_p (symbol))
230 else if ((group = symbol->get_comdat_group ()) != NULL)
232 map.put (symbol, group);
233 comdat_head_map.put (group, symbol);
234 comdat_group_seen = true;
236 /* Mark the symbol so we won't waste time visiting it for dataflow. */
237 symbol->aux = (symtab_node *) (void *) 1;
239 /* See symbols that can not be privatized to comdats; that is externally
240 visible symbols or otherwise used ones. We also do not want to mangle
241 user section names. */
242 else if (symbol->externally_visible
243 || symbol->force_output
244 || symbol->used_from_other_partition
245 || TREE_THIS_VOLATILE (symbol->decl)
246 || symbol->get_section ()
247 || (TREE_CODE (symbol->decl) == FUNCTION_DECL
248 && (DECL_STATIC_CONSTRUCTOR (symbol->decl)
249 || DECL_STATIC_DESTRUCTOR (symbol->decl))))
251 map.put (symtab_alias_ultimate_target (symbol, NULL), error_mark_node);
253 /* Mark the symbol so we won't waste time visiting it for dataflow. */
254 symbol->aux = (symtab_node *) (void *) 1;
256 else
258 /* Enqueue symbol for dataflow. */
259 symbol->aux = first;
260 first = symbol;
263 if (!comdat_group_seen)
265 FOR_EACH_DEFINED_SYMBOL (symbol)
266 symbol->aux = NULL;
267 return 0;
270 /* The actual dataflow. */
272 while (first != (void *) 1)
274 tree group = NULL;
275 tree newgroup, *val;
277 symbol = first;
278 first = (symtab_node *)first->aux;
280 /* Get current lattice value of SYMBOL. */
281 val = map.get (symbol);
282 if (val)
283 group = *val;
285 /* If it is bottom, there is nothing to do; do not clear AUX
286 so we won't re-queue the symbol. */
287 if (group == error_mark_node)
288 continue;
290 newgroup = propagate_comdat_group (symbol, group, map);
292 /* If nothing changed, proceed to next symbol. */
293 if (newgroup == group)
295 symbol->aux = NULL;
296 continue;
299 /* Update lattice value and enqueue all references for re-visiting. */
300 gcc_assert (newgroup);
301 if (val)
302 *val = newgroup;
303 else
304 map.put (symbol, newgroup);
305 enqueue_references (&first, symbol);
307 /* We may need to revisit the symbol unless it is BOTTOM. */
308 if (newgroup != error_mark_node)
309 symbol->aux = NULL;
312 /* Finally assign symbols to the sections. */
314 FOR_EACH_DEFINED_SYMBOL (symbol)
316 symbol->aux = NULL;
317 if (!symbol->get_comdat_group ()
318 && !symbol->alias
319 && symtab_real_symbol_p (symbol))
321 tree group = *map.get (symbol);
323 if (group == error_mark_node)
324 continue;
325 if (dump_file)
327 fprintf (dump_file, "Localizing symbol\n");
328 dump_symtab_node (dump_file, symbol);
329 fprintf (dump_file, "To group: %s\n", IDENTIFIER_POINTER (group));
331 symtab_for_node_and_aliases (symbol, set_comdat_group,
332 *comdat_head_map.get (group), true);
335 return 0;
338 namespace {
340 const pass_data pass_data_ipa_comdats =
342 IPA_PASS, /* type */
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
355 public:
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
375 bool
376 pass_ipa_comdats::gate (function *)
378 return optimize;
381 } // anon namespace
383 ipa_opt_pass_d *
384 make_pass_ipa_comdats (gcc::context *ctxt)
386 return new pass_ipa_comdats (ctxt);