[multiple changes]
[gcc.git] / gcc / ipa-comdats.c
1 /* Localize comdats.
2 Copyright (C) 2014 Free Software Foundation, Inc.
3
4 This file is part of GCC.
5
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.
10
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.
15
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/>. */
19
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:
25
26 static int q(void)
27 {
28 ....
29 }
30 inline int t(void)
31 {
32 return q();
33 }
34
35 if Q is used only by T, it makes sense to put Q into T's comdat group.
36
37 The pass solve simple dataflow across the callgraph trying to prove what
38 symbols are used exclusively from a given comdat group.
39
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.
43
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.
46
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. */
50
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"
59
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. */
64
65 tree
66 propagate_comdat_group (struct symtab_node *symbol,
67 tree newgroup, hash_map<symtab_node *, tree> &map)
68 {
69 int i;
70 struct ipa_ref *ref;
71
72 /* Walk all references to SYMBOL, recursively dive into aliases. */
73
74 for (i = 0;
75 symbol->iterate_referring (i, ref)
76 && newgroup != error_mark_node; i++)
77 {
78 struct symtab_node *symbol2 = ref->referring;
79
80 if (ref->use == IPA_REF_ALIAS)
81 {
82 newgroup = propagate_comdat_group (symbol2, newgroup, map);
83 continue;
84 }
85
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. */
89
90 if (symbol->type != symbol2->type)
91 {
92 newgroup = error_mark_node;
93 break;
94 }
95
96 /* If we see inline clone, its comdat group actually
97 corresponds to the comdat group of the function it is inlined
98 to. */
99
100 if (cgraph_node * cn = dyn_cast <cgraph_node *> (symbol2))
101 {
102 if (cn->global.inlined_to)
103 symbol2 = cn->global.inlined_to;
104 }
105
106 /* The actual merge operation. */
107
108 tree *val2 = map.get (symbol2);
109
110 if (val2 && *val2 != newgroup)
111 {
112 if (!newgroup)
113 newgroup = *val2;
114 else
115 newgroup = error_mark_node;
116 }
117 }
118
119 /* If we analyze function, walk also callers. */
120
121 cgraph_node *cnode = dyn_cast <cgraph_node *> (symbol);
122
123 if (cnode)
124 for (struct cgraph_edge * edge = cnode->callers;
125 edge && newgroup != error_mark_node; edge = edge->next_caller)
126 {
127 struct symtab_node *symbol2 = edge->caller;
128
129 /* If we see inline clone, its comdat group actually
130 corresponds to the comdat group of the function it is inlined
131 to. */
132
133 if (cgraph_node * cn = dyn_cast <cgraph_node *> (symbol2))
134 {
135 if (cn->global.inlined_to)
136 symbol2 = cn->global.inlined_to;
137 }
138
139 /* The actual merge operation. */
140
141 tree *val2 = map.get (symbol2);
142
143 if (val2 && *val2 != newgroup)
144 {
145 if (!newgroup)
146 newgroup = *val2;
147 else
148 newgroup = error_mark_node;
149 }
150 }
151 return newgroup;
152 }
153
154
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. */
158
159 void
160 enqueue_references (symtab_node **first,
161 symtab_node *symbol)
162 {
163 int i;
164 struct ipa_ref *ref = NULL;
165
166 for (i = 0; symbol->iterate_reference (i, ref); i++)
167 {
168 symtab_node *node = ref->referred->ultimate_alias_target ();
169 if (!node->aux && node->definition)
170 {
171 node->aux = *first;
172 *first = node;
173 }
174 }
175
176 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (symbol))
177 {
178 struct cgraph_edge *edge;
179
180 for (edge = cnode->callees; edge; edge = edge->next_callee)
181 if (!edge->inline_failed)
182 enqueue_references (first, edge->callee);
183 else
184 {
185 symtab_node *node = edge->callee->ultimate_alias_target ();
186 if (!node->aux && node->definition)
187 {
188 node->aux = *first;
189 *first = node;
190 }
191 }
192 }
193 }
194
195 /* Set comdat group of SYMBOL to GROUP.
196 Callback for symtab_for_node_and_aliases. */
197
198 bool
199 set_comdat_group (symtab_node *symbol,
200 void *head_p)
201 {
202 symtab_node *head = (symtab_node *)head_p;
203
204 gcc_assert (!symbol->get_comdat_group ());
205 symbol->set_comdat_group (head->get_comdat_group ());
206 symbol->add_to_same_comdat_group (head);
207 return false;
208 }
209
210 /* The actual pass with the main dataflow loop. */
211
212 static unsigned int
213 ipa_comdats (void)
214 {
215 hash_map<symtab_node *, tree> map (251);
216 hash_map<tree, symtab_node *> comdat_head_map (251);
217 symtab_node *symbol;
218 bool comdat_group_seen = false;
219 symtab_node *first = (symtab_node *) (void *) 1;
220 tree group;
221
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. */
225
226 FOR_EACH_DEFINED_SYMBOL (symbol)
227 if (!symbol->real_symbol_p ())
228 ;
229 else if ((group = symbol->get_comdat_group ()) != NULL)
230 {
231 map.put (symbol, group);
232 comdat_head_map.put (group, symbol);
233 comdat_group_seen = true;
234
235 /* Mark the symbol so we won't waste time visiting it for dataflow. */
236 symbol->aux = (symtab_node *) (void *) 1;
237 }
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))))
249 {
250 map.put (symbol->ultimate_alias_target (), error_mark_node);
251
252 /* Mark the symbol so we won't waste time visiting it for dataflow. */
253 symbol->aux = (symtab_node *) (void *) 1;
254 }
255 else
256 {
257 /* Enqueue symbol for dataflow. */
258 symbol->aux = first;
259 first = symbol;
260 }
261
262 if (!comdat_group_seen)
263 {
264 FOR_EACH_DEFINED_SYMBOL (symbol)
265 symbol->aux = NULL;
266 return 0;
267 }
268
269 /* The actual dataflow. */
270
271 while (first != (void *) 1)
272 {
273 tree group = NULL;
274 tree newgroup, *val;
275
276 symbol = first;
277 first = (symtab_node *)first->aux;
278
279 /* Get current lattice value of SYMBOL. */
280 val = map.get (symbol);
281 if (val)
282 group = *val;
283
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;
288
289 newgroup = propagate_comdat_group (symbol, group, map);
290
291 /* If nothing changed, proceed to next symbol. */
292 if (newgroup == group)
293 {
294 symbol->aux = NULL;
295 continue;
296 }
297
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.put (symbol, newgroup);
304 enqueue_references (&first, symbol);
305
306 /* We may need to revisit the symbol unless it is BOTTOM. */
307 if (newgroup != error_mark_node)
308 symbol->aux = NULL;
309 }
310
311 /* Finally assign symbols to the sections. */
312
313 FOR_EACH_DEFINED_SYMBOL (symbol)
314 {
315 symbol->aux = NULL;
316 if (!symbol->get_comdat_group ()
317 && !symbol->alias
318 && symbol->real_symbol_p ())
319 {
320 tree group = *map.get (symbol);
321
322 if (group == error_mark_node)
323 continue;
324 if (dump_file)
325 {
326 fprintf (dump_file, "Localizing symbol\n");
327 symbol->dump (dump_file);
328 fprintf (dump_file, "To group: %s\n", IDENTIFIER_POINTER (group));
329 }
330 symbol->call_for_symbol_and_aliases (set_comdat_group,
331 *comdat_head_map.get (group),
332 true);
333 }
334 }
335 return 0;
336 }
337
338 namespace {
339
340 const pass_data pass_data_ipa_comdats =
341 {
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 */
351 };
352
353 class pass_ipa_comdats : public ipa_opt_pass_d
354 {
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 */
367 {}
368
369 /* opt_pass methods: */
370 virtual bool gate (function *);
371 virtual unsigned int execute (function *) { return ipa_comdats (); }
372
373 }; // class pass_ipa_comdats
374
375 bool
376 pass_ipa_comdats::gate (function *)
377 {
378 return optimize;
379 }
380
381 } // anon namespace
382
383 ipa_opt_pass_d *
384 make_pass_ipa_comdats (gcc::context *ctxt)
385 {
386 return new pass_ipa_comdats (ctxt);
387 }