decl.c (value_annotation_hasher::handle_cache_entry): Delete.
[gcc.git] / gcc / symtab.c
1 /* Symbol table.
2 Copyright (C) 2012-2015 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 "rtl.h"
26 #include "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "print-tree.h"
31 #include "varasm.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "emit-rtl.h"
35 #include "predict.h"
36 #include "basic-block.h"
37 #include "tree-ssa-alias.h"
38 #include "internal-fn.h"
39 #include "gimple-expr.h"
40 #include "gimple.h"
41 #include "tree-inline.h"
42 #include "langhooks.h"
43 #include "plugin-api.h"
44 #include "ipa-ref.h"
45 #include "cgraph.h"
46 #include "diagnostic.h"
47 #include "timevar.h"
48 #include "lto-streamer.h"
49 #include "output.h"
50 #include "ipa-utils.h"
51 #include "calls.h"
52
53 static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"};
54
55 const char * const ld_plugin_symbol_resolution_names[]=
56 {
57 "",
58 "undef",
59 "prevailing_def",
60 "prevailing_def_ironly",
61 "preempted_reg",
62 "preempted_ir",
63 "resolved_ir",
64 "resolved_exec",
65 "resolved_dyn",
66 "prevailing_def_ironly_exp"
67 };
68
69 /* Hash asmnames ignoring the user specified marks. */
70
71 hashval_t
72 symbol_table::decl_assembler_name_hash (const_tree asmname)
73 {
74 if (IDENTIFIER_POINTER (asmname)[0] == '*')
75 {
76 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
77 size_t ulp_len = strlen (user_label_prefix);
78
79 if (ulp_len == 0)
80 ;
81 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
82 decl_str += ulp_len;
83
84 return htab_hash_string (decl_str);
85 }
86
87 return htab_hash_string (IDENTIFIER_POINTER (asmname));
88 }
89
90
91 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
92
93 bool
94 symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
95 {
96 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
97 const char *decl_str;
98 const char *asmname_str;
99 bool test = false;
100
101 if (decl_asmname == asmname)
102 return true;
103
104 decl_str = IDENTIFIER_POINTER (decl_asmname);
105 asmname_str = IDENTIFIER_POINTER (asmname);
106
107
108 /* If the target assembler name was set by the user, things are trickier.
109 We have a leading '*' to begin with. After that, it's arguable what
110 is the correct thing to do with -fleading-underscore. Arguably, we've
111 historically been doing the wrong thing in assemble_alias by always
112 printing the leading underscore. Since we're not changing that, make
113 sure user_label_prefix follows the '*' before matching. */
114 if (decl_str[0] == '*')
115 {
116 size_t ulp_len = strlen (user_label_prefix);
117
118 decl_str ++;
119
120 if (ulp_len == 0)
121 test = true;
122 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
123 decl_str += ulp_len, test=true;
124 else
125 decl_str --;
126 }
127 if (asmname_str[0] == '*')
128 {
129 size_t ulp_len = strlen (user_label_prefix);
130
131 asmname_str ++;
132
133 if (ulp_len == 0)
134 test = true;
135 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
136 asmname_str += ulp_len, test=true;
137 else
138 asmname_str --;
139 }
140
141 if (!test)
142 return false;
143 return strcmp (decl_str, asmname_str) == 0;
144 }
145
146
147 /* Returns nonzero if P1 and P2 are equal. */
148
149 /* Insert NODE to assembler name hash. */
150
151 void
152 symbol_table::insert_to_assembler_name_hash (symtab_node *node,
153 bool with_clones)
154 {
155 if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
156 return;
157 gcc_checking_assert (!node->previous_sharing_asm_name
158 && !node->next_sharing_asm_name);
159 if (assembler_name_hash)
160 {
161 symtab_node **aslot;
162 cgraph_node *cnode;
163 tree decl = node->decl;
164
165 tree name = DECL_ASSEMBLER_NAME (node->decl);
166
167 /* C++ FE can produce decls without associated assembler name and insert
168 them to symtab to hold section or TLS information. */
169 if (!name)
170 return;
171
172 hashval_t hash = decl_assembler_name_hash (name);
173 aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
174 gcc_assert (*aslot != node);
175 node->next_sharing_asm_name = (symtab_node *)*aslot;
176 if (*aslot != NULL)
177 (*aslot)->previous_sharing_asm_name = node;
178 *aslot = node;
179
180 /* Update also possible inline clones sharing a decl. */
181 cnode = dyn_cast <cgraph_node *> (node);
182 if (cnode && cnode->clones && with_clones)
183 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
184 if (cnode->decl == decl)
185 insert_to_assembler_name_hash (cnode, true);
186 }
187
188 }
189
190 /* Remove NODE from assembler name hash. */
191
192 void
193 symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
194 bool with_clones)
195 {
196 if (assembler_name_hash)
197 {
198 cgraph_node *cnode;
199 tree decl = node->decl;
200
201 if (node->next_sharing_asm_name)
202 node->next_sharing_asm_name->previous_sharing_asm_name
203 = node->previous_sharing_asm_name;
204 if (node->previous_sharing_asm_name)
205 {
206 node->previous_sharing_asm_name->next_sharing_asm_name
207 = node->next_sharing_asm_name;
208 }
209 else
210 {
211 tree name = DECL_ASSEMBLER_NAME (node->decl);
212 symtab_node **slot;
213
214 if (!name)
215 return;
216
217 hashval_t hash = decl_assembler_name_hash (name);
218 slot = assembler_name_hash->find_slot_with_hash (name, hash,
219 NO_INSERT);
220 gcc_assert (*slot == node);
221 if (!node->next_sharing_asm_name)
222 assembler_name_hash->clear_slot (slot);
223 else
224 *slot = node->next_sharing_asm_name;
225 }
226 node->next_sharing_asm_name = NULL;
227 node->previous_sharing_asm_name = NULL;
228
229 /* Update also possible inline clones sharing a decl. */
230 cnode = dyn_cast <cgraph_node *> (node);
231 if (cnode && cnode->clones && with_clones)
232 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
233 if (cnode->decl == decl)
234 unlink_from_assembler_name_hash (cnode, true);
235 }
236 }
237
238 /* Arrange node to be first in its entry of assembler_name_hash. */
239
240 void
241 symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
242 {
243 unlink_from_assembler_name_hash (node, false);
244 insert_to_assembler_name_hash (node, false);
245 }
246
247 /* Initalize asm name hash unless. */
248
249 void
250 symbol_table::symtab_initialize_asm_name_hash (void)
251 {
252 symtab_node *node;
253 if (!assembler_name_hash)
254 {
255 assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
256 FOR_EACH_SYMBOL (node)
257 insert_to_assembler_name_hash (node, false);
258 }
259 }
260
261 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
262
263 void
264 symbol_table::change_decl_assembler_name (tree decl, tree name)
265 {
266 symtab_node *node = NULL;
267
268 /* We can have user ASM names on things, like global register variables, that
269 are not in the symbol table. */
270 if ((TREE_CODE (decl) == VAR_DECL
271 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
272 || TREE_CODE (decl) == FUNCTION_DECL)
273 node = symtab_node::get (decl);
274 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
275 {
276 SET_DECL_ASSEMBLER_NAME (decl, name);
277 if (node)
278 insert_to_assembler_name_hash (node, true);
279 }
280 else
281 {
282 if (name == DECL_ASSEMBLER_NAME (decl))
283 return;
284
285 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
286 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
287 : NULL);
288 if (node)
289 unlink_from_assembler_name_hash (node, true);
290 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
291 && DECL_RTL_SET_P (decl))
292 warning (0, "%D renamed after being referenced in assembly", decl);
293
294 SET_DECL_ASSEMBLER_NAME (decl, name);
295 if (alias)
296 {
297 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
298 TREE_CHAIN (name) = alias;
299 }
300 if (node)
301 insert_to_assembler_name_hash (node, true);
302 }
303 }
304
305 /* Hash sections by their names. */
306
307 hashval_t
308 section_name_hasher::hash (section_hash_entry *n)
309 {
310 return htab_hash_string (n->name);
311 }
312
313 /* Return true if section P1 name equals to P2. */
314
315 bool
316 section_name_hasher::equal (section_hash_entry *n1, const char *name)
317 {
318 return n1->name == name || !strcmp (n1->name, name);
319 }
320
321 /* Add node into symbol table. This function is not used directly, but via
322 cgraph/varpool node creation routines. */
323
324 void
325 symtab_node::register_symbol (void)
326 {
327 symtab->register_symbol (this);
328
329 if (!decl->decl_with_vis.symtab_node)
330 decl->decl_with_vis.symtab_node = this;
331
332 ref_list.clear ();
333
334 /* Be sure to do this last; C++ FE might create new nodes via
335 DECL_ASSEMBLER_NAME langhook! */
336 symtab->insert_to_assembler_name_hash (this, false);
337 }
338
339 /* Remove NODE from same comdat group. */
340
341 void
342 symtab_node::remove_from_same_comdat_group (void)
343 {
344 if (same_comdat_group)
345 {
346 symtab_node *prev;
347 for (prev = same_comdat_group;
348 prev->same_comdat_group != this;
349 prev = prev->same_comdat_group)
350 ;
351 if (same_comdat_group == prev)
352 prev->same_comdat_group = NULL;
353 else
354 prev->same_comdat_group = same_comdat_group;
355 same_comdat_group = NULL;
356 set_comdat_group (NULL);
357 }
358 }
359
360 /* Remove node from symbol table. This function is not used directly, but via
361 cgraph/varpool node removal routines. */
362
363 void
364 symtab_node::unregister (void)
365 {
366 remove_all_references ();
367 remove_all_referring ();
368
369 /* Remove reference to section. */
370 set_section_for_node (NULL);
371
372 remove_from_same_comdat_group ();
373
374 symtab->unregister (this);
375
376 /* During LTO symtab merging we temporarily corrupt decl to symtab node
377 hash. */
378 gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
379 if (decl->decl_with_vis.symtab_node == this)
380 {
381 symtab_node *replacement_node = NULL;
382 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
383 replacement_node = cnode->find_replacement ();
384 decl->decl_with_vis.symtab_node = replacement_node;
385 }
386 if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
387 symtab->unlink_from_assembler_name_hash (this, false);
388 if (in_init_priority_hash)
389 symtab->init_priority_hash->remove (this);
390 }
391
392
393 /* Remove symbol from symbol table. */
394
395 void
396 symtab_node::remove (void)
397 {
398 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
399 cnode->remove ();
400 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
401 vnode->remove ();
402 }
403
404 /* Add NEW_ to the same comdat group that OLD is in. */
405
406 void
407 symtab_node::add_to_same_comdat_group (symtab_node *old_node)
408 {
409 gcc_assert (old_node->get_comdat_group ());
410 gcc_assert (!same_comdat_group);
411 gcc_assert (this != old_node);
412
413 set_comdat_group (old_node->get_comdat_group ());
414 same_comdat_group = old_node;
415 if (!old_node->same_comdat_group)
416 old_node->same_comdat_group = this;
417 else
418 {
419 symtab_node *n;
420 for (n = old_node->same_comdat_group;
421 n->same_comdat_group != old_node;
422 n = n->same_comdat_group)
423 ;
424 n->same_comdat_group = this;
425 }
426 }
427
428 /* Dissolve the same_comdat_group list in which NODE resides. */
429
430 void
431 symtab_node::dissolve_same_comdat_group_list (void)
432 {
433 symtab_node *n = this;
434 symtab_node *next;
435
436 if (!same_comdat_group)
437 return;
438 do
439 {
440 next = n->same_comdat_group;
441 n->same_comdat_group = NULL;
442 /* Clear comdat_group for comdat locals, since
443 make_decl_local doesn't. */
444 if (!TREE_PUBLIC (n->decl))
445 n->set_comdat_group (NULL);
446 n = next;
447 }
448 while (n != this);
449 }
450
451 /* Return printable assembler name of NODE.
452 This function is used only for debugging. When assembler name
453 is unknown go with identifier name. */
454
455 const char *
456 symtab_node::asm_name () const
457 {
458 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
459 return lang_hooks.decl_printable_name (decl, 2);
460 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
461 }
462
463 /* Return printable identifier name. */
464
465 const char *
466 symtab_node::name () const
467 {
468 return lang_hooks.decl_printable_name (decl, 2);
469 }
470
471 /* Return ipa reference from this symtab_node to
472 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
473 of the use. */
474
475 ipa_ref *
476 symtab_node::create_reference (symtab_node *referred_node,
477 enum ipa_ref_use use_type)
478 {
479 return create_reference (referred_node, use_type, NULL);
480 }
481
482
483 /* Return ipa reference from this symtab_node to
484 REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
485 of the use and STMT the statement (if it exists). */
486
487 ipa_ref *
488 symtab_node::create_reference (symtab_node *referred_node,
489 enum ipa_ref_use use_type, gimple stmt)
490 {
491 ipa_ref *ref = NULL, *ref2 = NULL;
492 ipa_ref_list *list, *list2;
493 ipa_ref_t *old_references;
494
495 gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
496 gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);
497
498 list = &ref_list;
499 old_references = vec_safe_address (list->references);
500 vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
501 ref = &list->references->last ();
502
503 list2 = &referred_node->ref_list;
504
505 /* IPA_REF_ALIAS is always inserted at the beginning of the list. */
506 if(use_type == IPA_REF_ALIAS)
507 {
508 list2->referring.safe_insert (0, ref);
509 ref->referred_index = 0;
510
511 for (unsigned int i = 1; i < list2->referring.length (); i++)
512 list2->referring[i]->referred_index = i;
513 }
514 else
515 {
516 list2->referring.safe_push (ref);
517 ref->referred_index = list2->referring.length () - 1;
518 }
519
520 ref->referring = this;
521 ref->referred = referred_node;
522 ref->stmt = stmt;
523 ref->lto_stmt_uid = 0;
524 ref->use = use_type;
525 ref->speculative = 0;
526
527 /* If vector was moved in memory, update pointers. */
528 if (old_references != list->references->address ())
529 {
530 int i;
531 for (i = 0; iterate_reference(i, ref2); i++)
532 ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
533 }
534 return ref;
535 }
536
537 /* If VAL is a reference to a function or a variable, add a reference from
538 this symtab_node to the corresponding symbol table node. USE_TYPE specify
539 type of the use and STMT the statement (if it exists). Return the new
540 reference or NULL if none was created. */
541
542 ipa_ref *
543 symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
544 gimple stmt)
545 {
546 STRIP_NOPS (val);
547 if (TREE_CODE (val) != ADDR_EXPR)
548 return NULL;
549 val = get_base_var (val);
550 if (val && (TREE_CODE (val) == FUNCTION_DECL
551 || TREE_CODE (val) == VAR_DECL))
552 {
553 symtab_node *referred = symtab_node::get (val);
554 gcc_checking_assert (referred);
555 return create_reference (referred, use_type, stmt);
556 }
557 return NULL;
558 }
559
560 /* Clone all references from symtab NODE to this symtab_node. */
561
562 void
563 symtab_node::clone_references (symtab_node *node)
564 {
565 ipa_ref *ref = NULL, *ref2 = NULL;
566 int i;
567 for (i = 0; node->iterate_reference (i, ref); i++)
568 {
569 bool speculative = ref->speculative;
570 unsigned int stmt_uid = ref->lto_stmt_uid;
571
572 ref2 = create_reference (ref->referred, ref->use, ref->stmt);
573 ref2->speculative = speculative;
574 ref2->lto_stmt_uid = stmt_uid;
575 }
576 }
577
578 /* Clone all referring from symtab NODE to this symtab_node. */
579
580 void
581 symtab_node::clone_referring (symtab_node *node)
582 {
583 ipa_ref *ref = NULL, *ref2 = NULL;
584 int i;
585 for (i = 0; node->iterate_referring(i, ref); i++)
586 {
587 bool speculative = ref->speculative;
588 unsigned int stmt_uid = ref->lto_stmt_uid;
589
590 ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
591 ref2->speculative = speculative;
592 ref2->lto_stmt_uid = stmt_uid;
593 }
594 }
595
596 /* Clone reference REF to this symtab_node and set its stmt to STMT. */
597
598 ipa_ref *
599 symtab_node::clone_reference (ipa_ref *ref, gimple stmt)
600 {
601 bool speculative = ref->speculative;
602 unsigned int stmt_uid = ref->lto_stmt_uid;
603 ipa_ref *ref2;
604
605 ref2 = create_reference (ref->referred, ref->use, stmt);
606 ref2->speculative = speculative;
607 ref2->lto_stmt_uid = stmt_uid;
608 return ref2;
609 }
610
611 /* Find the structure describing a reference to REFERRED_NODE
612 and associated with statement STMT. */
613
614 ipa_ref *
615 symtab_node::find_reference (symtab_node *referred_node,
616 gimple stmt, unsigned int lto_stmt_uid)
617 {
618 ipa_ref *r = NULL;
619 int i;
620
621 for (i = 0; iterate_reference (i, r); i++)
622 if (r->referred == referred_node
623 && !r->speculative
624 && ((stmt && r->stmt == stmt)
625 || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
626 || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
627 return r;
628 return NULL;
629 }
630
631 /* Remove all references that are associated with statement STMT. */
632
633 void
634 symtab_node::remove_stmt_references (gimple stmt)
635 {
636 ipa_ref *r = NULL;
637 int i = 0;
638
639 while (iterate_reference (i, r))
640 if (r->stmt == stmt)
641 r->remove_reference ();
642 else
643 i++;
644 }
645
646 /* Remove all stmt references in non-speculative references.
647 Those are not maintained during inlining & clonning.
648 The exception are speculative references that are updated along
649 with callgraph edges associated with them. */
650
651 void
652 symtab_node::clear_stmts_in_references (void)
653 {
654 ipa_ref *r = NULL;
655 int i;
656
657 for (i = 0; iterate_reference (i, r); i++)
658 if (!r->speculative)
659 {
660 r->stmt = NULL;
661 r->lto_stmt_uid = 0;
662 }
663 }
664
665 /* Remove all references in ref list. */
666
667 void
668 symtab_node::remove_all_references (void)
669 {
670 while (vec_safe_length (ref_list.references))
671 ref_list.references->last ().remove_reference ();
672 vec_free (ref_list.references);
673 }
674
675 /* Remove all referring items in ref list. */
676
677 void
678 symtab_node::remove_all_referring (void)
679 {
680 while (ref_list.referring.length ())
681 ref_list.referring.last ()->remove_reference ();
682 ref_list.referring.release ();
683 }
684
685 /* Dump references in ref list to FILE. */
686
687 void
688 symtab_node::dump_references (FILE *file)
689 {
690 ipa_ref *ref = NULL;
691 int i;
692 for (i = 0; iterate_reference (i, ref); i++)
693 {
694 fprintf (file, "%s/%i (%s)",
695 ref->referred->asm_name (),
696 ref->referred->order,
697 ipa_ref_use_name [ref->use]);
698 if (ref->speculative)
699 fprintf (file, " (speculative)");
700 }
701 fprintf (file, "\n");
702 }
703
704 /* Dump referring in list to FILE. */
705
706 void
707 symtab_node::dump_referring (FILE *file)
708 {
709 ipa_ref *ref = NULL;
710 int i;
711 for (i = 0; iterate_referring(i, ref); i++)
712 {
713 fprintf (file, "%s/%i (%s)",
714 ref->referring->asm_name (),
715 ref->referring->order,
716 ipa_ref_use_name [ref->use]);
717 if (ref->speculative)
718 fprintf (file, " (speculative)");
719 }
720 fprintf (file, "\n");
721 }
722
723 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
724
725 /* Dump base fields of symtab nodes to F. Not to be used directly. */
726
727 void
728 symtab_node::dump_base (FILE *f)
729 {
730 static const char * const visibility_types[] = {
731 "default", "protected", "hidden", "internal"
732 };
733
734 fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
735 dump_addr (f, " @", (void *)this);
736 fprintf (f, "\n Type: %s", symtab_type_names[type]);
737
738 if (definition)
739 fprintf (f, " definition");
740 if (analyzed)
741 fprintf (f, " analyzed");
742 if (alias)
743 fprintf (f, " alias");
744 if (weakref)
745 fprintf (f, " weakref");
746 if (cpp_implicit_alias)
747 fprintf (f, " cpp_implicit_alias");
748 if (alias_target)
749 fprintf (f, " target:%s",
750 DECL_P (alias_target)
751 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
752 (alias_target))
753 : IDENTIFIER_POINTER (alias_target));
754 if (body_removed)
755 fprintf (f, "\n Body removed by symtab_remove_unreachable_nodes");
756 fprintf (f, "\n Visibility:");
757 if (in_other_partition)
758 fprintf (f, " in_other_partition");
759 if (used_from_other_partition)
760 fprintf (f, " used_from_other_partition");
761 if (force_output)
762 fprintf (f, " force_output");
763 if (forced_by_abi)
764 fprintf (f, " forced_by_abi");
765 if (externally_visible)
766 fprintf (f, " externally_visible");
767 if (no_reorder)
768 fprintf (f, " no_reorder");
769 if (resolution != LDPR_UNKNOWN)
770 fprintf (f, " %s",
771 ld_plugin_symbol_resolution_names[(int)resolution]);
772 if (TREE_ASM_WRITTEN (decl))
773 fprintf (f, " asm_written");
774 if (DECL_EXTERNAL (decl))
775 fprintf (f, " external");
776 if (TREE_PUBLIC (decl))
777 fprintf (f, " public");
778 if (DECL_COMMON (decl))
779 fprintf (f, " common");
780 if (DECL_WEAK (decl))
781 fprintf (f, " weak");
782 if (DECL_DLLIMPORT_P (decl))
783 fprintf (f, " dll_import");
784 if (DECL_COMDAT (decl))
785 fprintf (f, " comdat");
786 if (get_comdat_group ())
787 fprintf (f, " comdat_group:%s",
788 IDENTIFIER_POINTER (get_comdat_group_id ()));
789 if (DECL_ONE_ONLY (decl))
790 fprintf (f, " one_only");
791 if (get_section ())
792 fprintf (f, " section:%s",
793 get_section ());
794 if (implicit_section)
795 fprintf (f," (implicit_section)");
796 if (DECL_VISIBILITY_SPECIFIED (decl))
797 fprintf (f, " visibility_specified");
798 if (DECL_VISIBILITY (decl))
799 fprintf (f, " visibility:%s",
800 visibility_types [DECL_VISIBILITY (decl)]);
801 if (DECL_VIRTUAL_P (decl))
802 fprintf (f, " virtual");
803 if (DECL_ARTIFICIAL (decl))
804 fprintf (f, " artificial");
805 if (TREE_CODE (decl) == FUNCTION_DECL)
806 {
807 if (DECL_STATIC_CONSTRUCTOR (decl))
808 fprintf (f, " constructor");
809 if (DECL_STATIC_DESTRUCTOR (decl))
810 fprintf (f, " destructor");
811 }
812 fprintf (f, "\n");
813
814 if (same_comdat_group)
815 fprintf (f, " Same comdat group as: %s/%i\n",
816 same_comdat_group->asm_name (),
817 same_comdat_group->order);
818 if (next_sharing_asm_name)
819 fprintf (f, " next sharing asm name: %i\n",
820 next_sharing_asm_name->order);
821 if (previous_sharing_asm_name)
822 fprintf (f, " previous sharing asm name: %i\n",
823 previous_sharing_asm_name->order);
824
825 if (address_taken)
826 fprintf (f, " Address is taken.\n");
827 if (aux)
828 {
829 fprintf (f, " Aux:");
830 dump_addr (f, " @", (void *)aux);
831 }
832
833 fprintf (f, " References: ");
834 dump_references (f);
835 fprintf (f, " Referring: ");
836 dump_referring (f);
837 if (lto_file_data)
838 fprintf (f, " Read from file: %s\n",
839 lto_file_data->file_name);
840 }
841
842 /* Dump symtab node to F. */
843
844 void
845 symtab_node::dump (FILE *f)
846 {
847 if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
848 cnode->dump (f);
849 else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
850 vnode->dump (f);
851 }
852
853 /* Dump symbol table to F. */
854
855 void
856 symtab_node::dump_table (FILE *f)
857 {
858 symtab_node *node;
859 fprintf (f, "Symbol table:\n\n");
860 FOR_EACH_SYMBOL (node)
861 node->dump (f);
862 }
863
864
865 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
866 Return NULL if there's no such node. */
867
868 symtab_node *
869 symtab_node::get_for_asmname (const_tree asmname)
870 {
871 symtab_node *node;
872
873 symtab->symtab_initialize_asm_name_hash ();
874 hashval_t hash = symtab->decl_assembler_name_hash (asmname);
875 symtab_node **slot
876 = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
877 NO_INSERT);
878
879 if (slot)
880 {
881 node = *slot;
882 return node;
883 }
884 return NULL;
885 }
886
887 /* Dump symtab node NODE to stderr. */
888
889 DEBUG_FUNCTION void
890 symtab_node::debug (void)
891 {
892 dump (stderr);
893 }
894
895 /* Verify common part of symtab nodes. */
896
897 DEBUG_FUNCTION bool
898 symtab_node::verify_base (void)
899 {
900 bool error_found = false;
901 symtab_node *hashed_node;
902
903 if (is_a <cgraph_node *> (this))
904 {
905 if (TREE_CODE (decl) != FUNCTION_DECL)
906 {
907 error ("function symbol is not function");
908 error_found = true;
909 }
910 }
911 else if (is_a <varpool_node *> (this))
912 {
913 if (TREE_CODE (decl) != VAR_DECL)
914 {
915 error ("variable symbol is not variable");
916 error_found = true;
917 }
918 }
919 else
920 {
921 error ("node has unknown type");
922 error_found = true;
923 }
924
925 if (symtab->state != LTO_STREAMING)
926 {
927 hashed_node = symtab_node::get (decl);
928 if (!hashed_node)
929 {
930 error ("node not found node->decl->decl_with_vis.symtab_node");
931 error_found = true;
932 }
933 if (hashed_node != this
934 && (!is_a <cgraph_node *> (this)
935 || !dyn_cast <cgraph_node *> (this)->clone_of
936 || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
937 {
938 error ("node differs from node->decl->decl_with_vis.symtab_node");
939 error_found = true;
940 }
941 }
942 if (symtab->assembler_name_hash)
943 {
944 hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
945 if (hashed_node && hashed_node->previous_sharing_asm_name)
946 {
947 error ("assembler name hash list corrupted");
948 error_found = true;
949 }
950 while (hashed_node)
951 {
952 if (hashed_node == this)
953 break;
954 hashed_node = hashed_node->next_sharing_asm_name;
955 }
956 if (!hashed_node
957 && !(is_a <varpool_node *> (this)
958 || DECL_HARD_REGISTER (decl)))
959 {
960 error ("node not found in symtab assembler name hash");
961 error_found = true;
962 }
963 }
964 if (previous_sharing_asm_name
965 && previous_sharing_asm_name->next_sharing_asm_name != this)
966 {
967 error ("double linked list of assembler names corrupted");
968 error_found = true;
969 }
970 if (body_removed && definition)
971 {
972 error ("node has body_removed but is definition");
973 error_found = true;
974 }
975 if (analyzed && !definition)
976 {
977 error ("node is analyzed byt it is not a definition");
978 error_found = true;
979 }
980 if (cpp_implicit_alias && !alias)
981 {
982 error ("node is alias but not implicit alias");
983 error_found = true;
984 }
985 if (alias && !definition && !weakref)
986 {
987 error ("node is alias but not definition");
988 error_found = true;
989 }
990 if (weakref && !alias)
991 {
992 error ("node is weakref but not an alias");
993 error_found = true;
994 }
995 if (same_comdat_group)
996 {
997 symtab_node *n = same_comdat_group;
998
999 if (!n->get_comdat_group ())
1000 {
1001 error ("node is in same_comdat_group list but has no comdat_group");
1002 error_found = true;
1003 }
1004 if (n->get_comdat_group () != get_comdat_group ())
1005 {
1006 error ("same_comdat_group list across different groups");
1007 error_found = true;
1008 }
1009 if (n->type != type)
1010 {
1011 error ("mixing different types of symbol in same comdat groups is not supported");
1012 error_found = true;
1013 }
1014 if (n == this)
1015 {
1016 error ("node is alone in a comdat group");
1017 error_found = true;
1018 }
1019 do
1020 {
1021 if (!n->same_comdat_group)
1022 {
1023 error ("same_comdat_group is not a circular list");
1024 error_found = true;
1025 break;
1026 }
1027 n = n->same_comdat_group;
1028 }
1029 while (n != this);
1030 if (comdat_local_p ())
1031 {
1032 ipa_ref *ref = NULL;
1033
1034 for (int i = 0; iterate_referring (i, ref); ++i)
1035 {
1036 if (!in_same_comdat_group_p (ref->referring))
1037 {
1038 error ("comdat-local symbol referred to by %s outside its "
1039 "comdat",
1040 identifier_to_locale (ref->referring->name()));
1041 error_found = true;
1042 }
1043 }
1044 }
1045 }
1046 if (implicit_section && !get_section ())
1047 {
1048 error ("implicit_section flag is set but section isn't");
1049 error_found = true;
1050 }
1051 if (get_section () && get_comdat_group ()
1052 && !implicit_section
1053 && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1054 {
1055 error ("Both section and comdat group is set");
1056 error_found = true;
1057 }
1058 /* TODO: Add string table for sections, so we do not keep holding duplicated
1059 strings. */
1060 if (alias && definition
1061 && get_section () != get_alias_target ()->get_section ()
1062 && (!get_section()
1063 || !get_alias_target ()->get_section ()
1064 || strcmp (get_section(),
1065 get_alias_target ()->get_section ())))
1066 {
1067 error ("Alias and target's section differs");
1068 get_alias_target ()->dump (stderr);
1069 error_found = true;
1070 }
1071 if (alias && definition
1072 && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1073 {
1074 error ("Alias and target's comdat groups differs");
1075 get_alias_target ()->dump (stderr);
1076 error_found = true;
1077 }
1078
1079 return error_found;
1080 }
1081
1082 /* Verify consistency of NODE. */
1083
1084 DEBUG_FUNCTION void
1085 symtab_node::verify (void)
1086 {
1087 if (seen_error ())
1088 return;
1089
1090 timevar_push (TV_CGRAPH_VERIFY);
1091 if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
1092 node->verify_node ();
1093 else
1094 if (verify_base ())
1095 {
1096 debug ();
1097 internal_error ("symtab_node::verify failed");
1098 }
1099 timevar_pop (TV_CGRAPH_VERIFY);
1100 }
1101
1102 /* Verify symbol table for internal consistency. */
1103
1104 DEBUG_FUNCTION void
1105 symtab_node::verify_symtab_nodes (void)
1106 {
1107 symtab_node *node;
1108 hash_map<tree, symtab_node *> comdat_head_map (251);
1109
1110 FOR_EACH_SYMBOL (node)
1111 {
1112 node->verify ();
1113 if (node->get_comdat_group ())
1114 {
1115 symtab_node **entry, *s;
1116 bool existed;
1117
1118 entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
1119 &existed);
1120 if (!existed)
1121 *entry = node;
1122 else if (!DECL_EXTERNAL (node->decl))
1123 {
1124 for (s = (*entry)->same_comdat_group;
1125 s != NULL && s != node && s != *entry;
1126 s = s->same_comdat_group)
1127 ;
1128 if (!s || s == *entry)
1129 {
1130 error ("Two symbols with same comdat_group are not linked by "
1131 "the same_comdat_group list.");
1132 (*entry)->debug ();
1133 node->debug ();
1134 internal_error ("symtab_node::verify failed");
1135 }
1136 }
1137 }
1138 }
1139 }
1140
1141 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
1142 but other code such as notice_global_symbol generates rtl. */
1143
1144 void
1145 symtab_node::make_decl_local (void)
1146 {
1147 rtx rtl, symbol;
1148
1149 /* Avoid clearing comdat_groups on comdat-local decls. */
1150 if (TREE_PUBLIC (decl) == 0)
1151 return;
1152
1153 if (TREE_CODE (decl) == VAR_DECL)
1154 {
1155 DECL_COMMON (decl) = 0;
1156 /* ADDRESSABLE flag is not defined for public symbols. */
1157 TREE_ADDRESSABLE (decl) = 1;
1158 }
1159 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
1160
1161 DECL_COMDAT (decl) = 0;
1162 DECL_WEAK (decl) = 0;
1163 DECL_EXTERNAL (decl) = 0;
1164 DECL_VISIBILITY_SPECIFIED (decl) = 0;
1165 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1166 TREE_PUBLIC (decl) = 0;
1167 DECL_DLLIMPORT_P (decl) = 0;
1168 if (!DECL_RTL_SET_P (decl))
1169 return;
1170
1171 /* Update rtl flags. */
1172 make_decl_rtl (decl);
1173
1174 rtl = DECL_RTL (decl);
1175 if (!MEM_P (rtl))
1176 return;
1177
1178 symbol = XEXP (rtl, 0);
1179 if (GET_CODE (symbol) != SYMBOL_REF)
1180 return;
1181
1182 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
1183 }
1184
1185 /* Walk the alias chain to return the symbol NODE is alias of.
1186 If NODE is not an alias, return NODE.
1187 Assumes NODE is known to be alias. */
1188
1189 symtab_node *
1190 symtab_node::ultimate_alias_target_1 (enum availability *availability)
1191 {
1192 bool weakref_p = false;
1193
1194 /* To determine visibility of the target, we follow ELF semantic of aliases.
1195 Here alias is an alternative assembler name of a given definition. Its
1196 availability prevails the availability of its target (i.e. static alias of
1197 weak definition is available.
1198
1199 Weakref is a different animal (and not part of ELF per se). It is just
1200 alternative name of a given symbol used within one complation unit
1201 and is translated prior hitting the object file. It inherits the
1202 visibility of its target (i.e. weakref of non-overwritable definition
1203 is non-overwritable, while weakref of weak definition is weak).
1204
1205 If we ever get into supporting targets with different semantics, a target
1206 hook will be needed here. */
1207
1208 if (availability)
1209 {
1210 weakref_p = weakref;
1211 if (!weakref_p)
1212 *availability = get_availability ();
1213 else
1214 *availability = AVAIL_LOCAL;
1215 }
1216
1217 symtab_node *node = this;
1218 while (node)
1219 {
1220 if (node->alias && node->analyzed)
1221 node = node->get_alias_target ();
1222 else
1223 {
1224 if (!availability)
1225 ;
1226 else if (node->analyzed)
1227 {
1228 if (weakref_p)
1229 {
1230 enum availability a = node->get_availability ();
1231 if (a < *availability)
1232 *availability = a;
1233 }
1234 }
1235 else
1236 *availability = AVAIL_NOT_AVAILABLE;
1237 return node;
1238 }
1239 if (node && availability && weakref_p)
1240 {
1241 enum availability a = node->get_availability ();
1242 if (a < *availability)
1243 *availability = a;
1244 weakref_p = node->weakref;
1245 }
1246 }
1247 if (availability)
1248 *availability = AVAIL_NOT_AVAILABLE;
1249 return NULL;
1250 }
1251
1252 /* C++ FE sometimes change linkage flags after producing same body aliases.
1253
1254 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1255 are obviously equivalent. The way it is doing so is however somewhat
1256 kludgy and interferes with the visibility code. As a result we need to
1257 copy the visibility from the target to get things right. */
1258
1259 void
1260 symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1261 {
1262 if (is_a <cgraph_node *> (this))
1263 {
1264 DECL_DECLARED_INLINE_P (decl)
1265 = DECL_DECLARED_INLINE_P (target->decl);
1266 DECL_DISREGARD_INLINE_LIMITS (decl)
1267 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1268 }
1269 /* FIXME: It is not really clear why those flags should not be copied for
1270 functions, too. */
1271 else
1272 {
1273 DECL_WEAK (decl) = DECL_WEAK (target->decl);
1274 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1275 DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1276 }
1277 DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
1278 if (TREE_PUBLIC (decl))
1279 {
1280 tree group;
1281
1282 DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
1283 DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1284 group = target->get_comdat_group ();
1285 set_comdat_group (group);
1286 if (group && !same_comdat_group)
1287 add_to_same_comdat_group (target);
1288 }
1289 externally_visible = target->externally_visible;
1290 }
1291
1292 /* Set section, do not recurse into aliases.
1293 When one wants to change section of symbol and its aliases,
1294 use set_section. */
1295
1296 void
1297 symtab_node::set_section_for_node (const char *section)
1298 {
1299 const char *current = get_section ();
1300 section_hash_entry **slot;
1301
1302 if (current == section
1303 || (current && section
1304 && !strcmp (current, section)))
1305 return;
1306
1307 if (current)
1308 {
1309 x_section->ref_count--;
1310 if (!x_section->ref_count)
1311 {
1312 hashval_t hash = htab_hash_string (x_section->name);
1313 slot = symtab->section_hash->find_slot_with_hash (x_section->name,
1314 hash, INSERT);
1315 ggc_free (x_section);
1316 symtab->section_hash->clear_slot (slot);
1317 }
1318 x_section = NULL;
1319 }
1320 if (!section)
1321 {
1322 implicit_section = false;
1323 return;
1324 }
1325 if (!symtab->section_hash)
1326 symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
1327 slot = symtab->section_hash->find_slot_with_hash (section,
1328 htab_hash_string (section),
1329 INSERT);
1330 if (*slot)
1331 x_section = (section_hash_entry *)*slot;
1332 else
1333 {
1334 int len = strlen (section);
1335 *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
1336 x_section->name = ggc_vec_alloc<char> (len + 1);
1337 memcpy (x_section->name, section, len + 1);
1338 }
1339 x_section->ref_count++;
1340 }
1341
1342 /* Worker for set_section. */
1343
1344 bool
1345 symtab_node::set_section (symtab_node *n, void *s)
1346 {
1347 n->set_section_for_node ((char *)s);
1348 return false;
1349 }
1350
1351 /* Set section of symbol and its aliases. */
1352
1353 void
1354 symtab_node::set_section (const char *section)
1355 {
1356 gcc_assert (!this->alias);
1357 call_for_symbol_and_aliases
1358 (symtab_node::set_section, const_cast<char *>(section), true);
1359 }
1360
1361 /* Return the initialization priority. */
1362
1363 priority_type
1364 symtab_node::get_init_priority ()
1365 {
1366 if (!this->in_init_priority_hash)
1367 return DEFAULT_INIT_PRIORITY;
1368
1369 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1370 return h ? h->init : DEFAULT_INIT_PRIORITY;
1371 }
1372
1373 /* Return the finalization priority. */
1374
1375 priority_type
1376 cgraph_node::get_fini_priority ()
1377 {
1378 if (!this->in_init_priority_hash)
1379 return DEFAULT_INIT_PRIORITY;
1380 symbol_priority_map *h = symtab->init_priority_hash->get (this);
1381 return h ? h->fini : DEFAULT_INIT_PRIORITY;
1382 }
1383
1384 /* Return the initialization and finalization priority information for
1385 DECL. If there is no previous priority information, a freshly
1386 allocated structure is returned. */
1387
1388 symbol_priority_map *
1389 symtab_node::priority_info (void)
1390 {
1391 if (!symtab->init_priority_hash)
1392 symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1393
1394 bool existed;
1395 symbol_priority_map *h
1396 = &symtab->init_priority_hash->get_or_insert (this, &existed);
1397 if (!existed)
1398 {
1399 h->init = DEFAULT_INIT_PRIORITY;
1400 h->fini = DEFAULT_INIT_PRIORITY;
1401 in_init_priority_hash = true;
1402 }
1403
1404 return h;
1405 }
1406
1407 /* Set initialization priority to PRIORITY. */
1408
1409 void
1410 symtab_node::set_init_priority (priority_type priority)
1411 {
1412 symbol_priority_map *h;
1413
1414 if (is_a <cgraph_node *> (this))
1415 gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));
1416
1417 if (priority == DEFAULT_INIT_PRIORITY)
1418 {
1419 gcc_assert (get_init_priority() == priority);
1420 return;
1421 }
1422 h = priority_info ();
1423 h->init = priority;
1424 }
1425
1426 /* Set fialization priority to PRIORITY. */
1427
1428 void
1429 cgraph_node::set_fini_priority (priority_type priority)
1430 {
1431 symbol_priority_map *h;
1432
1433 gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));
1434
1435 if (priority == DEFAULT_INIT_PRIORITY)
1436 {
1437 gcc_assert (get_fini_priority() == priority);
1438 return;
1439 }
1440 h = priority_info ();
1441 h->fini = priority;
1442 }
1443
1444 /* Worker for symtab_resolve_alias. */
1445
1446 bool
1447 symtab_node::set_implicit_section (symtab_node *n,
1448 void *data ATTRIBUTE_UNUSED)
1449 {
1450 n->implicit_section = true;
1451 return false;
1452 }
1453
1454 /* Add reference recording that symtab node is alias of TARGET.
1455 The function can fail in the case of aliasing cycles; in this case
1456 it returns false. */
1457
1458 bool
1459 symtab_node::resolve_alias (symtab_node *target)
1460 {
1461 symtab_node *n;
1462
1463 gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1464
1465 /* Never let cycles to creep into the symbol table alias references;
1466 those will make alias walkers to be infinite. */
1467 for (n = target; n && n->alias;
1468 n = n->analyzed ? n->get_alias_target () : NULL)
1469 if (n == this)
1470 {
1471 if (is_a <cgraph_node *> (this))
1472 error ("function %q+D part of alias cycle", decl);
1473 else if (is_a <varpool_node *> (this))
1474 error ("variable %q+D part of alias cycle", decl);
1475 else
1476 gcc_unreachable ();
1477 alias = false;
1478 return false;
1479 }
1480
1481 /* "analyze" the node - i.e. mark the reference. */
1482 definition = true;
1483 alias = true;
1484 analyzed = true;
1485 create_reference (target, IPA_REF_ALIAS, NULL);
1486
1487 /* Add alias into the comdat group of its target unless it is already there. */
1488 if (same_comdat_group)
1489 remove_from_same_comdat_group ();
1490 set_comdat_group (NULL);
1491 if (target->get_comdat_group ())
1492 add_to_same_comdat_group (target);
1493
1494 if ((get_section () != target->get_section ()
1495 || target->get_comdat_group ()) && get_section () && !implicit_section)
1496 {
1497 error ("section of alias %q+D must match section of its target", decl);
1498 }
1499 call_for_symbol_and_aliases (symtab_node::set_section,
1500 const_cast<char *>(target->get_section ()), true);
1501 if (target->implicit_section)
1502 call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1503
1504 /* Alias targets become redundant after alias is resolved into an reference.
1505 We do not want to keep it around or we would have to mind updating them
1506 when renaming symbols. */
1507 alias_target = NULL;
1508
1509 if (cpp_implicit_alias && symtab->state >= CONSTRUCTION)
1510 fixup_same_cpp_alias_visibility (target);
1511
1512 /* If alias has address taken, so does the target. */
1513 if (address_taken)
1514 target->ultimate_alias_target ()->address_taken = true;
1515
1516 /* All non-weakref aliases of THIS are now in fact aliases of TARGET. */
1517 ipa_ref *ref;
1518 for (unsigned i = 0; iterate_direct_aliases (i, ref);)
1519 {
1520 struct symtab_node *alias_alias = ref->referring;
1521 if (!alias_alias->weakref)
1522 {
1523 alias_alias->remove_all_references ();
1524 alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
1525 }
1526 else i++;
1527 }
1528 return true;
1529 }
1530
1531 /* Worker searching noninterposable alias. */
1532
1533 bool
1534 symtab_node::noninterposable_alias (symtab_node *node, void *data)
1535 {
1536 if (decl_binds_to_current_def_p (node->decl))
1537 {
1538 symtab_node *fn = node->ultimate_alias_target ();
1539
1540 /* Ensure that the alias is well formed this may not be the case
1541 of user defined aliases and currently it is not always the case
1542 of C++ same body aliases (that is a bug). */
1543 if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
1544 || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
1545 || (TREE_CODE (node->decl) == FUNCTION_DECL
1546 && flags_from_decl_or_type (node->decl)
1547 != flags_from_decl_or_type (fn->decl))
1548 || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
1549 return false;
1550 *(symtab_node **)data = node;
1551 return true;
1552 }
1553 return false;
1554 }
1555
1556 /* If node can not be overwriten by static or dynamic linker to point to
1557 different definition, return NODE. Otherwise look for alias with such
1558 property and if none exists, introduce new one. */
1559
1560 symtab_node *
1561 symtab_node::noninterposable_alias (void)
1562 {
1563 tree new_decl;
1564 symtab_node *new_node = NULL;
1565
1566 /* First try to look up existing alias or base object
1567 (if that is already non-overwritable). */
1568 symtab_node *node = ultimate_alias_target ();
1569 gcc_assert (!node->alias && !node->weakref);
1570 node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
1571 (void *)&new_node, true);
1572 if (new_node)
1573 return new_node;
1574 #ifndef ASM_OUTPUT_DEF
1575 /* If aliases aren't supported by the assembler, fail. */
1576 return NULL;
1577 #endif
1578
1579 /* Otherwise create a new one. */
1580 new_decl = copy_node (node->decl);
1581 DECL_DLLIMPORT_P (new_decl) = 0;
1582 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1583 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1584 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1585 DECL_INITIAL (new_decl) = NULL;
1586 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1587 SET_DECL_RTL (new_decl, NULL);
1588
1589 /* Update the properties. */
1590 DECL_EXTERNAL (new_decl) = 0;
1591 TREE_PUBLIC (new_decl) = 0;
1592 DECL_COMDAT (new_decl) = 0;
1593 DECL_WEAK (new_decl) = 0;
1594
1595 /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag. */
1596 DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1597 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1598 {
1599 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1600 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1601 new_node = cgraph_node::create_alias (new_decl, node->decl);
1602 }
1603 else
1604 {
1605 TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1606 DECL_INITIAL (new_decl) = error_mark_node;
1607 new_node = varpool_node::create_alias (new_decl, node->decl);
1608 }
1609 new_node->resolve_alias (node);
1610 gcc_assert (decl_binds_to_current_def_p (new_decl)
1611 && targetm.binds_local_p (new_decl));
1612 return new_node;
1613 }
1614
1615 /* Return true if symtab node and TARGET represents
1616 semantically equivalent symbols. */
1617
1618 bool
1619 symtab_node::semantically_equivalent_p (symtab_node *target)
1620 {
1621 enum availability avail;
1622 symtab_node *ba;
1623 symtab_node *bb;
1624
1625 /* Equivalent functions are equivalent. */
1626 if (decl == target->decl)
1627 return true;
1628
1629 /* If symbol is not overwritable by different implementation,
1630 walk to the base object it defines. */
1631 ba = ultimate_alias_target (&avail);
1632 if (avail >= AVAIL_AVAILABLE)
1633 {
1634 if (target == ba)
1635 return true;
1636 }
1637 else
1638 ba = this;
1639 bb = target->ultimate_alias_target (&avail);
1640 if (avail >= AVAIL_AVAILABLE)
1641 {
1642 if (this == bb)
1643 return true;
1644 }
1645 else
1646 bb = target;
1647 return bb == ba;
1648 }
1649
1650 /* Classify symbol symtab node for partitioning. */
1651
1652 enum symbol_partitioning_class
1653 symtab_node::get_partitioning_class (void)
1654 {
1655 /* Inline clones are always duplicated.
1656 This include external delcarations. */
1657 cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1658
1659 if (DECL_ABSTRACT_P (decl))
1660 return SYMBOL_EXTERNAL;
1661
1662 if (cnode && cnode->global.inlined_to)
1663 return SYMBOL_DUPLICATE;
1664
1665 /* Weakref aliases are always duplicated. */
1666 if (weakref)
1667 return SYMBOL_DUPLICATE;
1668
1669 /* External declarations are external. */
1670 if (DECL_EXTERNAL (decl))
1671 return SYMBOL_EXTERNAL;
1672
1673 if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1674 {
1675 if (alias && definition && !ultimate_alias_target ()->definition)
1676 return SYMBOL_EXTERNAL;
1677 /* Constant pool references use local symbol names that can not
1678 be promoted global. We should never put into a constant pool
1679 objects that can not be duplicated across partitions. */
1680 if (DECL_IN_CONSTANT_POOL (decl))
1681 return SYMBOL_DUPLICATE;
1682 if (DECL_HARD_REGISTER (decl))
1683 return SYMBOL_DUPLICATE;
1684 gcc_checking_assert (vnode->definition);
1685 }
1686 /* Functions that are cloned may stay in callgraph even if they are unused.
1687 Handle them as external; compute_ltrans_boundary take care to make
1688 proper things to happen (i.e. to make them appear in the boundary but
1689 with body streamed, so clone can me materialized). */
1690 else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1691 return SYMBOL_EXTERNAL;
1692
1693 /* Linker discardable symbols are duplicated to every use unless they are
1694 keyed. */
1695 if (DECL_ONE_ONLY (decl)
1696 && !force_output
1697 && !forced_by_abi
1698 && !used_from_object_file_p ())
1699 return SYMBOL_DUPLICATE;
1700
1701 return SYMBOL_PARTITION;
1702 }
1703
1704 /* Return true when symbol is known to be non-zero. */
1705
1706 bool
1707 symtab_node::nonzero_address ()
1708 {
1709 /* Weakrefs may be NULL when their target is not defined. */
1710 if (alias && weakref)
1711 {
1712 if (analyzed)
1713 {
1714 symtab_node *target = ultimate_alias_target ();
1715
1716 if (target->alias && target->weakref)
1717 return false;
1718 /* We can not recurse to target::nonzero. It is possible that the
1719 target is used only via the alias.
1720 We may walk references and look for strong use, but we do not know
1721 if this strong use will survive to final binary, so be
1722 conservative here.
1723 ??? Maybe we could do the lookup during late optimization that
1724 could be useful to eliminate the NULL pointer checks in LTO
1725 programs. */
1726 if (target->definition && !DECL_EXTERNAL (target->decl))
1727 return true;
1728 if (target->resolution != LDPR_UNKNOWN
1729 && target->resolution != LDPR_UNDEF
1730 && flag_delete_null_pointer_checks)
1731 return true;
1732 return false;
1733 }
1734 else
1735 return false;
1736 }
1737
1738 /* With !flag_delete_null_pointer_checks we assume that symbols may
1739 bind to NULL. This is on by default on embedded targets only.
1740
1741 Otherwise all non-WEAK symbols must be defined and thus non-NULL or
1742 linking fails. Important case of WEAK we want to do well are comdats.
1743 Those are handled by later check for definition.
1744
1745 When parsing, beware the cases when WEAK attribute is added later. */
1746 if (!DECL_WEAK (decl)
1747 && flag_delete_null_pointer_checks)
1748 {
1749 refuse_visibility_changes = true;
1750 return true;
1751 }
1752
1753 /* If target is defined and not extern, we know it will be output and thus
1754 it will bind to non-NULL.
1755 Play safe for flag_delete_null_pointer_checks where weak definition maye
1756 be re-defined by NULL. */
1757 if (definition && !DECL_EXTERNAL (decl)
1758 && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
1759 {
1760 if (!DECL_WEAK (decl))
1761 refuse_visibility_changes = true;
1762 return true;
1763 }
1764
1765 /* As the last resort, check the resolution info. */
1766 if (resolution != LDPR_UNKNOWN
1767 && resolution != LDPR_UNDEF
1768 && flag_delete_null_pointer_checks)
1769 return true;
1770 return false;
1771 }
1772
1773 /* Return 0 if symbol is known to have different address than S2,
1774 Return 1 if symbol is known to have same address as S2,
1775 return 2 otherwise. */
1776 int
1777 symtab_node::equal_address_to (symtab_node *s2)
1778 {
1779 enum availability avail1, avail2;
1780
1781 /* A Shortcut: equivalent symbols are always equivalent. */
1782 if (this == s2)
1783 return 1;
1784
1785 /* For non-interposable aliases, lookup and compare their actual definitions.
1786 Also check if the symbol needs to bind to given definition. */
1787 symtab_node *rs1 = ultimate_alias_target (&avail1);
1788 symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
1789 bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
1790 bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
1791 bool really_binds_local1 = binds_local1;
1792 bool really_binds_local2 = binds_local2;
1793
1794 /* Addresses of vtables and virtual functions can not be used by user
1795 code and are used only within speculation. In this case we may make
1796 symbol equivalent to its alias even if interposition may break this
1797 rule. Doing so will allow us to turn speculative inlining into
1798 non-speculative more agressively. */
1799 if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
1800 binds_local1 = true;
1801 if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
1802 binds_local2 = true;
1803
1804 /* If both definitions are available we know that even if they are bound
1805 to other unit they must be defined same way and therefore we can use
1806 equivalence test. */
1807 if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
1808 binds_local1 = binds_local2 = true;
1809
1810 if ((binds_local1 ? rs1 : this)
1811 == (binds_local2 ? rs2 : s2))
1812 {
1813 /* We made use of the fact that alias is not weak. */
1814 if (binds_local1 && rs1 != this)
1815 refuse_visibility_changes = true;
1816 if (binds_local2 && rs2 != s2)
1817 s2->refuse_visibility_changes = true;
1818 return 1;
1819 }
1820
1821 /* If both symbols may resolve to NULL, we can not really prove them different. */
1822 if (!nonzero_address () && !s2->nonzero_address ())
1823 return 2;
1824
1825 /* Except for NULL, functions and variables never overlap. */
1826 if (TREE_CODE (decl) != TREE_CODE (s2->decl))
1827 return 0;
1828
1829 /* If one of the symbols is unresolved alias, punt. */
1830 if (rs1->alias || rs2->alias)
1831 return 2;
1832
1833 /* If we have a non-interposale definition of at least one of the symbols
1834 and the other symbol is different, we know other unit can not interpose
1835 it to the first symbol; all aliases of the definition needs to be
1836 present in the current unit. */
1837 if (((really_binds_local1 || really_binds_local2)
1838 /* If we have both definitions and they are different, we know they
1839 will be different even in units they binds to. */
1840 || (binds_local1 && binds_local2))
1841 && rs1 != rs2)
1842 {
1843 /* We make use of the fact that one symbol is not alias of the other
1844 and that the definition is non-interposable. */
1845 refuse_visibility_changes = true;
1846 s2->refuse_visibility_changes = true;
1847 rs1->refuse_visibility_changes = true;
1848 rs2->refuse_visibility_changes = true;
1849 return 0;
1850 }
1851
1852 /* TODO: Alias oracle basically assume that addresses of global variables
1853 are different unless they are declared as alias of one to another.
1854 We probably should be consistent and use this fact here, too, and update
1855 alias oracle to use this predicate. */
1856
1857 return 2;
1858 }
1859
1860 /* Worker for call_for_symbol_and_aliases. */
1861
1862 bool
1863 symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
1864 void *),
1865 void *data,
1866 bool include_overwritable)
1867 {
1868 ipa_ref *ref;
1869 FOR_EACH_ALIAS (this, ref)
1870 {
1871 symtab_node *alias = ref->referring;
1872 if (include_overwritable
1873 || alias->get_availability () > AVAIL_INTERPOSABLE)
1874 if (alias->call_for_symbol_and_aliases (callback, data,
1875 include_overwritable))
1876 return true;
1877 }
1878 return false;
1879 }
1880
1881 /* Return ture if address of N is possibly compared. */
1882
1883 static bool
1884 address_matters_1 (symtab_node *n, void *)
1885 {
1886 struct ipa_ref *ref;
1887
1888 if (!n->address_can_be_compared_p ())
1889 return false;
1890 if (n->externally_visible || n->force_output)
1891 return true;
1892
1893 for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
1894 if (ref->address_matters_p ())
1895 return true;
1896 return false;
1897 }
1898
1899 /* Return true if symbol's address may possibly be compared to other
1900 symbol's address. */
1901
1902 bool
1903 symtab_node::address_matters_p ()
1904 {
1905 gcc_assert (!alias);
1906 return call_for_symbol_and_aliases (address_matters_1, NULL, true);
1907 }
1908
1909 /* Return ture if symbol's alignment may be increased. */
1910
1911 bool
1912 symtab_node::can_increase_alignment_p (void)
1913 {
1914 symtab_node *target = ultimate_alias_target ();
1915
1916 /* For now support only variables. */
1917 if (TREE_CODE (decl) != VAR_DECL)
1918 return false;
1919
1920 /* With -fno-toplevel-reorder we may have already output the constant. */
1921 if (TREE_ASM_WRITTEN (target->decl))
1922 return false;
1923
1924 /* If target is already placed in an anchor, we can not touch its
1925 alignment. */
1926 if (DECL_RTL_SET_P (target->decl)
1927 && MEM_P (DECL_RTL (target->decl))
1928 && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
1929 return false;
1930
1931 /* Constant pool entries may be shared. */
1932 if (DECL_IN_CONSTANT_POOL (target->decl))
1933 return false;
1934
1935 /* We cannot change alignment of symbols that may bind to symbols
1936 in other translation unit that may contain a definition with lower
1937 alignment. */
1938 if (!decl_binds_to_current_def_p (decl))
1939 return false;
1940
1941 /* When compiling partition, be sure the symbol is not output by other
1942 partition. */
1943 if (flag_ltrans
1944 && (target->in_other_partition
1945 || target->get_partitioning_class () == SYMBOL_DUPLICATE))
1946 return false;
1947
1948 /* Do not override the alignment as specified by the ABI when the used
1949 attribute is set. */
1950 if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
1951 return false;
1952
1953 /* Do not override explicit alignment set by the user when an explicit
1954 section name is also used. This is a common idiom used by many
1955 software projects. */
1956 if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
1957 return false;
1958
1959 return true;
1960 }
1961
1962 /* Worker for symtab_node::increase_alignment. */
1963
1964 static bool
1965 increase_alignment_1 (symtab_node *n, void *v)
1966 {
1967 unsigned int align = (size_t)v;
1968 if (DECL_ALIGN (n->decl) < align
1969 && n->can_increase_alignment_p ())
1970 {
1971 DECL_ALIGN (n->decl) = align;
1972 DECL_USER_ALIGN (n->decl) = 1;
1973 }
1974 return false;
1975 }
1976
1977 /* Increase alignment of THIS to ALIGN. */
1978
1979 void
1980 symtab_node::increase_alignment (unsigned int align)
1981 {
1982 gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT);
1983 ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
1984 (void *)(size_t) align,
1985 true);
1986 gcc_assert (DECL_ALIGN (decl) >= align);
1987 }
1988
1989 /* Helper for symtab_node::definition_alignment. */
1990
1991 static bool
1992 get_alignment_1 (symtab_node *n, void *v)
1993 {
1994 *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
1995 return false;
1996 }
1997
1998 /* Return desired alignment of the definition. This is NOT alignment useful
1999 to access THIS, because THIS may be interposable and DECL_ALIGN should
2000 be used instead. It however must be guaranteed when output definition
2001 of THIS. */
2002
2003 unsigned int
2004 symtab_node::definition_alignment ()
2005 {
2006 unsigned int align = 0;
2007 gcc_assert (!alias);
2008 call_for_symbol_and_aliases (get_alignment_1, &align, true);
2009 return align;
2010 }