cgraph.c (cgraph_create_indirect_edge): Discover polymorphic calls and record basic...
[gcc.git] / gcc / varpool.c
1 /* Callgraph handling code.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "tree.h"
26 #include "cgraph.h"
27 #include "langhooks.h"
28 #include "diagnostic-core.h"
29 #include "hashtab.h"
30 #include "ggc.h"
31 #include "timevar.h"
32 #include "debug.h"
33 #include "target.h"
34 #include "output.h"
35 #include "gimple.h"
36 #include "tree-flow.h"
37 #include "flags.h"
38
39 /* Allocate new callgraph node and insert it into basic data structures. */
40
41 struct varpool_node *
42 varpool_create_empty_node (void)
43 {
44 struct varpool_node *node = ggc_alloc_cleared_varpool_node ();
45 node->symbol.type = SYMTAB_VARIABLE;
46 return node;
47 }
48
49 /* Return varpool node assigned to DECL. Create new one when needed. */
50 struct varpool_node *
51 varpool_node_for_decl (tree decl)
52 {
53 struct varpool_node *node = varpool_get_node (decl);
54 gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
55 if (node)
56 return node;
57
58 node = varpool_create_empty_node ();
59 node->symbol.decl = decl;
60 symtab_register_node ((symtab_node)node);
61 return node;
62 }
63
64 /* Remove node from the varpool. */
65 void
66 varpool_remove_node (struct varpool_node *node)
67 {
68 symtab_unregister_node ((symtab_node)node);
69 tree init;
70
71 /* Because we remove references from external functions before final compilation,
72 we may end up removing useful constructors.
73 FIXME: We probably want to trace boundaries better. */
74 if ((init = ctor_for_folding (node->symbol.decl)) == error_mark_node)
75 varpool_remove_initializer (node);
76 else
77 DECL_INITIAL (node->symbol.decl) = init;
78 ggc_free (node);
79 }
80
81 /* Renove node initializer when it is no longer needed. */
82 void
83 varpool_remove_initializer (struct varpool_node *node)
84 {
85 if (DECL_INITIAL (node->symbol.decl)
86 && !DECL_IN_CONSTANT_POOL (node->symbol.decl)
87 /* Keep vtables for BINFO folding. */
88 && !DECL_VIRTUAL_P (node->symbol.decl)
89 /* FIXME: http://gcc.gnu.org/PR55395 */
90 && debug_info_level == DINFO_LEVEL_NONE
91 /* When doing declaration merging we have duplicate
92 entries for given decl. Do not attempt to remove
93 the boides, or we will end up remiving
94 wrong one. */
95 && cgraph_state != CGRAPH_LTO_STREAMING)
96 DECL_INITIAL (node->symbol.decl) = error_mark_node;
97 }
98
99 /* Dump given cgraph node. */
100 void
101 dump_varpool_node (FILE *f, struct varpool_node *node)
102 {
103 dump_symtab_base (f, (symtab_node)node);
104 fprintf (f, " Availability: %s\n",
105 cgraph_function_flags_ready
106 ? cgraph_availability_names[cgraph_variable_initializer_availability (node)]
107 : "not-ready");
108 fprintf (f, " Varpool flags:");
109 if (DECL_INITIAL (node->symbol.decl))
110 fprintf (f, " initialized");
111 if (node->output)
112 fprintf (f, " output");
113 if (TREE_READONLY (node->symbol.decl))
114 fprintf (f, " read-only");
115 if (ctor_for_folding (node->symbol.decl) != error_mark_node)
116 fprintf (f, " const-value-known");
117 fprintf (f, "\n");
118 }
119
120 /* Dump the variable pool. */
121 void
122 dump_varpool (FILE *f)
123 {
124 struct varpool_node *node;
125
126 fprintf (f, "variable pool:\n\n");
127 FOR_EACH_VARIABLE (node)
128 dump_varpool_node (f, node);
129 }
130
131 /* Dump the variable pool to stderr. */
132
133 DEBUG_FUNCTION void
134 debug_varpool (void)
135 {
136 dump_varpool (stderr);
137 }
138
139 /* Given an assembler name, lookup node. */
140 struct varpool_node *
141 varpool_node_for_asm (tree asmname)
142 {
143 if (symtab_node node = symtab_node_for_asm (asmname))
144 return dyn_cast <varpool_node> (node);
145 else
146 return NULL;
147 }
148
149 /* Return if DECL is constant and its initial value is known (so we can do
150 constant folding using DECL_INITIAL (decl)).
151 Return ERROR_MARK_NODE when value is unknown. */
152
153 tree
154 ctor_for_folding (tree decl)
155 {
156 struct varpool_node *node, *real_node;
157 tree real_decl;
158
159 if (TREE_CODE (decl) != VAR_DECL
160 && TREE_CODE (decl) != CONST_DECL)
161 return error_mark_node;
162
163 if (TREE_CODE (decl) == CONST_DECL
164 || DECL_IN_CONSTANT_POOL (decl))
165 return DECL_INITIAL (decl);
166
167 if (TREE_THIS_VOLATILE (decl))
168 return error_mark_node;
169
170 /* Do not care about automatic variables. Those are never initialized
171 anyway, because gimplifier exapnds the code*/
172 if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
173 {
174 gcc_assert (!TREE_PUBLIC (decl));
175 return error_mark_node;
176 }
177
178 gcc_assert (TREE_CODE (decl) == VAR_DECL);
179
180 node = varpool_get_node (decl);
181 if (node)
182 {
183 real_node = varpool_variable_node (node);
184 real_decl = real_node->symbol.decl;
185 }
186 else
187 real_decl = decl;
188
189 /* See if we are dealing with alias.
190 In most cases alias is just alternative symbol pointing to a given
191 constructor. This allows us to use interposition rules of DECL
192 constructor of REAL_NODE. However weakrefs are special by being just
193 alternative name of their target (if defined). */
194 if (decl != real_decl)
195 {
196 gcc_assert (!DECL_INITIAL (decl)
197 || DECL_INITIAL (decl) == error_mark_node);
198 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (decl)))
199 {
200 node = varpool_alias_target (node);
201 decl = node->symbol.decl;
202 }
203 }
204
205 /* Vtables are defined by their types and must match no matter of interposition
206 rules. */
207 if (DECL_VIRTUAL_P (real_decl))
208 {
209 gcc_checking_assert (TREE_READONLY (real_decl));
210 return DECL_INITIAL (real_decl);
211 }
212
213 /* If thre is no constructor, we have nothing to do. */
214 if (DECL_INITIAL (real_decl) == error_mark_node)
215 return error_mark_node;
216
217 /* Non-readonly alias of readonly variable is also de-facto readonly,
218 because the variable itself is in readonly section.
219 We also honnor READONLY flag on alias assuming that user knows
220 what he is doing. */
221 if (!TREE_READONLY (decl) && !TREE_READONLY (real_decl))
222 return error_mark_node;
223
224 /* Variables declared 'const' without an initializer
225 have zero as the initializer if they may not be
226 overridden at link or run time. */
227 if (!DECL_INITIAL (real_decl)
228 && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
229 return error_mark_node;
230
231 /* Variables declared `const' with an initializer are considered
232 to not be overwritable with different initializer by default.
233
234 ??? Previously we behaved so for scalar variables but not for array
235 accesses. */
236 return DECL_INITIAL (real_decl);
237 }
238
239 /* Add the variable DECL to the varpool.
240 Unlike varpool_finalize_decl function is intended to be used
241 by middle end and allows insertion of new variable at arbitrary point
242 of compilation. */
243 void
244 varpool_add_new_variable (tree decl)
245 {
246 struct varpool_node *node;
247 varpool_finalize_decl (decl);
248 node = varpool_node_for_decl (decl);
249 if (varpool_externally_visible_p (node))
250 node->symbol.externally_visible = true;
251 }
252
253 /* Return variable availability. See cgraph.h for description of individual
254 return values. */
255 enum availability
256 cgraph_variable_initializer_availability (struct varpool_node *node)
257 {
258 gcc_assert (cgraph_function_flags_ready);
259 if (!node->symbol.definition)
260 return AVAIL_NOT_AVAILABLE;
261 if (!TREE_PUBLIC (node->symbol.decl))
262 return AVAIL_AVAILABLE;
263 /* If the variable can be overwritten, return OVERWRITABLE. Takes
264 care of at least one notable extension - the COMDAT variables
265 used to share template instantiations in C++. */
266 if (!decl_replaceable_p (node->symbol.decl))
267 return AVAIL_OVERWRITABLE;
268 return AVAIL_AVAILABLE;
269 }
270
271 void
272 varpool_analyze_node (struct varpool_node *node)
273 {
274 tree decl = node->symbol.decl;
275
276 /* When reading back varpool at LTO time, we re-construct the queue in order
277 to have "needed" list right by inserting all needed nodes into varpool.
278 We however don't want to re-analyze already analyzed nodes. */
279 if (!node->symbol.analyzed)
280 {
281 gcc_assert (!in_lto_p || cgraph_function_flags_ready);
282 /* Compute the alignment early so function body expanders are
283 already informed about increased alignment. */
284 align_variable (decl, 0);
285 }
286 if (node->symbol.alias)
287 symtab_resolve_alias
288 ((symtab_node) node, (symtab_node) varpool_get_node (node->symbol.alias_target));
289 else if (DECL_INITIAL (decl))
290 record_references_in_initializer (decl, node->symbol.analyzed);
291 node->symbol.analyzed = true;
292 }
293
294 /* Assemble thunks and aliases associated to NODE. */
295
296 static void
297 assemble_aliases (struct varpool_node *node)
298 {
299 int i;
300 struct ipa_ref *ref;
301 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
302 if (ref->use == IPA_REF_ALIAS)
303 {
304 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
305 do_assemble_alias (alias->symbol.decl,
306 DECL_ASSEMBLER_NAME (node->symbol.decl));
307 assemble_aliases (alias);
308 }
309 }
310
311 /* Output one variable, if necessary. Return whether we output it. */
312
313 bool
314 varpool_assemble_decl (struct varpool_node *node)
315 {
316 tree decl = node->symbol.decl;
317
318 /* Aliases are outout when their target is produced or by
319 output_weakrefs. */
320 if (node->symbol.alias)
321 return false;
322
323 /* Constant pool is output from RTL land when the reference
324 survive till this level. */
325 if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
326 return false;
327
328 /* Decls with VALUE_EXPR should not be in the varpool at all. They
329 are not real variables, but just info for debugging and codegen.
330 Unfortunately at the moment emutls is not updating varpool correctly
331 after turning real vars into value_expr vars. */
332 if (DECL_HAS_VALUE_EXPR_P (decl)
333 && !targetm.have_tls)
334 return false;
335
336 /* Hard register vars do not need to be output. */
337 if (DECL_HARD_REGISTER (decl))
338 return false;
339
340 gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
341 && TREE_CODE (decl) == VAR_DECL
342 && !DECL_HAS_VALUE_EXPR_P (decl));
343
344 if (!node->symbol.in_other_partition
345 && !DECL_EXTERNAL (decl))
346 {
347 assemble_variable (decl, 0, 1, 0);
348 gcc_assert (TREE_ASM_WRITTEN (decl));
349 node->symbol.definition = true;
350 assemble_aliases (node);
351 return true;
352 }
353
354 return false;
355 }
356
357 /* Add NODE to queue starting at FIRST.
358 The queue is linked via AUX pointers and terminated by pointer to 1. */
359
360 static void
361 enqueue_node (struct varpool_node *node, struct varpool_node **first)
362 {
363 if (node->symbol.aux)
364 return;
365 gcc_checking_assert (*first);
366 node->symbol.aux = *first;
367 *first = node;
368 }
369
370 /* Optimization of function bodies might've rendered some variables as
371 unnecessary so we want to avoid these from being compiled. Re-do
372 reachability starting from variables that are either externally visible
373 or was referred from the asm output routines. */
374
375 static void
376 varpool_remove_unreferenced_decls (void)
377 {
378 struct varpool_node *next, *node;
379 struct varpool_node *first = (struct varpool_node *)(void *)1;
380 int i;
381 struct ipa_ref *ref;
382
383 if (seen_error ())
384 return;
385
386 if (cgraph_dump_file)
387 fprintf (cgraph_dump_file, "Trivially needed variables:");
388 FOR_EACH_DEFINED_VARIABLE (node)
389 {
390 if (node->symbol.analyzed
391 && (!varpool_can_remove_if_no_refs (node)
392 /* We just expanded all function bodies. See if any of
393 them needed the variable. */
394 || DECL_RTL_SET_P (node->symbol.decl)))
395 {
396 enqueue_node (node, &first);
397 if (cgraph_dump_file)
398 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
399 }
400 }
401 while (first != (struct varpool_node *)(void *)1)
402 {
403 node = first;
404 first = (struct varpool_node *)first->symbol.aux;
405
406 if (node->symbol.same_comdat_group)
407 {
408 symtab_node next;
409 for (next = node->symbol.same_comdat_group;
410 next != (symtab_node)node;
411 next = next->symbol.same_comdat_group)
412 {
413 varpool_node *vnext = dyn_cast <varpool_node> (next);
414 if (vnext && vnext->symbol.analyzed)
415 enqueue_node (vnext, &first);
416 }
417 }
418 for (i = 0; ipa_ref_list_reference_iterate (&node->symbol.ref_list, i, ref); i++)
419 {
420 varpool_node *vnode = dyn_cast <varpool_node> (ref->referred);
421 if (vnode
422 && (!DECL_EXTERNAL (ref->referred->symbol.decl)
423 || vnode->symbol.alias)
424 && vnode->symbol.analyzed)
425 enqueue_node (vnode, &first);
426 }
427 }
428 if (cgraph_dump_file)
429 fprintf (cgraph_dump_file, "\nRemoving variables:");
430 for (node = varpool_first_defined_variable (); node; node = next)
431 {
432 next = varpool_next_defined_variable (node);
433 if (!node->symbol.aux)
434 {
435 if (cgraph_dump_file)
436 fprintf (cgraph_dump_file, " %s", varpool_node_asm_name (node));
437 varpool_remove_node (node);
438 }
439 }
440 if (cgraph_dump_file)
441 fprintf (cgraph_dump_file, "\n");
442 }
443
444 /* For variables in named sections make sure get_variable_section
445 is called before we switch to those sections. Then section
446 conflicts between read-only and read-only requiring relocations
447 sections can be resolved. */
448 void
449 varpool_finalize_named_section_flags (struct varpool_node *node)
450 {
451 if (!TREE_ASM_WRITTEN (node->symbol.decl)
452 && !node->symbol.alias
453 && !node->symbol.in_other_partition
454 && !DECL_EXTERNAL (node->symbol.decl)
455 && TREE_CODE (node->symbol.decl) == VAR_DECL
456 && !DECL_HAS_VALUE_EXPR_P (node->symbol.decl)
457 && DECL_SECTION_NAME (node->symbol.decl))
458 get_variable_section (node->symbol.decl, false);
459 }
460
461 /* Output all variables enqueued to be assembled. */
462 bool
463 varpool_output_variables (void)
464 {
465 bool changed = false;
466 struct varpool_node *node;
467
468 if (seen_error ())
469 return false;
470
471 varpool_remove_unreferenced_decls ();
472
473 timevar_push (TV_VAROUT);
474
475 FOR_EACH_DEFINED_VARIABLE (node)
476 varpool_finalize_named_section_flags (node);
477
478 FOR_EACH_DEFINED_VARIABLE (node)
479 if (varpool_assemble_decl (node))
480 changed = true;
481 timevar_pop (TV_VAROUT);
482 return changed;
483 }
484
485 /* Create a new global variable of type TYPE. */
486 tree
487 add_new_static_var (tree type)
488 {
489 tree new_decl;
490 struct varpool_node *new_node;
491
492 new_decl = create_tmp_var_raw (type, NULL);
493 DECL_NAME (new_decl) = create_tmp_var_name (NULL);
494 TREE_READONLY (new_decl) = 0;
495 TREE_STATIC (new_decl) = 1;
496 TREE_USED (new_decl) = 1;
497 DECL_CONTEXT (new_decl) = NULL_TREE;
498 DECL_ABSTRACT (new_decl) = 0;
499 lang_hooks.dup_lang_specific_decl (new_decl);
500 new_node = varpool_node_for_decl (new_decl);
501 varpool_finalize_decl (new_decl);
502
503 return new_node->symbol.decl;
504 }
505
506 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
507 Extra name aliases are output whenever DECL is output. */
508
509 struct varpool_node *
510 varpool_create_variable_alias (tree alias, tree decl)
511 {
512 struct varpool_node *alias_node;
513
514 gcc_assert (TREE_CODE (decl) == VAR_DECL);
515 gcc_assert (TREE_CODE (alias) == VAR_DECL);
516 alias_node = varpool_node_for_decl (alias);
517 alias_node->symbol.alias = true;
518 alias_node->symbol.definition = true;
519 alias_node->symbol.alias_target = decl;
520 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
521 alias_node->symbol.weakref = true;
522 return alias_node;
523 }
524
525 /* Attempt to mark ALIAS as an alias to DECL. Return TRUE if successful.
526 Extra name aliases are output whenever DECL is output. */
527
528 struct varpool_node *
529 varpool_extra_name_alias (tree alias, tree decl)
530 {
531 struct varpool_node *alias_node;
532
533 #ifndef ASM_OUTPUT_DEF
534 /* If aliases aren't supported by the assembler, fail. */
535 return NULL;
536 #endif
537 alias_node = varpool_create_variable_alias (alias, decl);
538 alias_node->symbol.cpp_implicit_alias = true;
539
540 /* Extra name alias mechanizm creates aliases really late
541 via DECL_ASSEMBLER_NAME mechanizm.
542 This is unfortunate because they are not going through the
543 standard channels. Ensure they get output. */
544 if (cpp_implicit_aliases_done)
545 symtab_resolve_alias ((symtab_node)alias_node,
546 (symtab_node)varpool_node_for_decl (decl));
547 return alias_node;
548 }
549
550 /* Call calback on NODE and aliases associated to NODE.
551 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
552 skipped. */
553
554 bool
555 varpool_for_node_and_aliases (struct varpool_node *node,
556 bool (*callback) (struct varpool_node *, void *),
557 void *data,
558 bool include_overwritable)
559 {
560 int i;
561 struct ipa_ref *ref;
562
563 if (callback (node, data))
564 return true;
565 for (i = 0; ipa_ref_list_referring_iterate (&node->symbol.ref_list, i, ref); i++)
566 if (ref->use == IPA_REF_ALIAS)
567 {
568 struct varpool_node *alias = ipa_ref_referring_varpool_node (ref);
569 if (include_overwritable
570 || cgraph_variable_initializer_availability (alias) > AVAIL_OVERWRITABLE)
571 if (varpool_for_node_and_aliases (alias, callback, data,
572 include_overwritable))
573 return true;
574 }
575 return false;
576 }