2014-05-20 Sebastian Huber <sebastian.huber@embedded-brains.de>
[official-gcc.git] / gcc / ipa-comdats.c
blobd6840648fec5102970e954e8b54271b2985eaf8f
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 "pointer-set.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, pointer_map <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 ipa_ref_list_referring_iterate (&symbol->ref_list, 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.contains (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.contains (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;
166 for (i = 0; ipa_ref_list_reference_iterate (&symbol->ref_list, 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 (!DECL_COMDAT_GROUP (symbol->decl));
206 DECL_COMDAT_GROUP (symbol->decl) = DECL_COMDAT_GROUP (head->decl);
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 pointer_map<tree> map;
217 pointer_map<symtab_node *> comdat_head_map;
218 symtab_node *symbol;
219 bool comdat_group_seen = false;
220 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 (!symtab_real_symbol_p (symbol))
229 else if (DECL_COMDAT_GROUP (symbol->decl))
231 *map.insert (symbol) = DECL_COMDAT_GROUP (symbol->decl);
232 *comdat_head_map.insert (DECL_COMDAT_GROUP (symbol->decl)) = 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 || DECL_SECTION_NAME (symbol->decl)
246 || (TREE_CODE (symbol->decl) == FUNCTION_DECL
247 && (DECL_STATIC_CONSTRUCTOR (symbol->decl)
248 || DECL_STATIC_DESTRUCTOR (symbol->decl))))
250 *map.insert (symtab_alias_ultimate_target (symbol, NULL)) = error_mark_node;
252 /* Mark the symbol so we won't waste time visiting it for dataflow. */
253 symbol->aux = (symtab_node *) (void *) 1;
255 else
257 /* Enqueue symbol for dataflow. */
258 symbol->aux = first;
259 first = symbol;
262 if (!comdat_group_seen)
264 FOR_EACH_DEFINED_SYMBOL (symbol)
265 symbol->aux = NULL;
266 return 0;
269 /* The actual dataflow. */
271 while (first != (void *) 1)
273 tree group = NULL;
274 tree newgroup, *val;
276 symbol = first;
277 first = (symtab_node *)first->aux;
279 /* Get current lattice value of SYMBOL. */
280 val = map.contains (symbol);
281 if (val)
282 group = *val;
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)
287 continue;
289 newgroup = propagate_comdat_group (symbol, group, map);
291 /* If nothing changed, proceed to next symbol. */
292 if (newgroup == group)
294 symbol->aux = NULL;
295 continue;
298 /* Update lattice value and enqueue all references for re-visiting. */
299 gcc_assert (newgroup);
300 if (val)
301 *val = newgroup;
302 else
303 *map.insert (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)
308 symbol->aux = NULL;
311 /* Finally assign symbols to the sections. */
313 FOR_EACH_DEFINED_SYMBOL (symbol)
315 symbol->aux = NULL;
316 if (!DECL_COMDAT_GROUP (symbol->decl)
317 && !symbol->alias
318 && symtab_real_symbol_p (symbol))
320 tree group = *map.contains (symbol);
322 if (group == error_mark_node)
323 continue;
324 if (dump_file)
326 fprintf (dump_file, "Localizing symbol\n");
327 dump_symtab_node (dump_file, symbol);
328 fprintf (dump_file, "To group: %s\n", IDENTIFIER_POINTER (group));
330 symtab_for_node_and_aliases (symbol, set_comdat_group,
331 *comdat_head_map.contains (group), true);
334 return 0;
337 namespace {
339 const pass_data pass_data_ipa_comdats =
341 IPA_PASS, /* type */
342 "comdats", /* name */
343 OPTGROUP_NONE, /* optinfo_flags */
344 true, /* has_execute */
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);