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