cond.md (stzx_16): Use register_operand for operand 0.
[gcc.git] / gcc / symtab.c
1 /* Symbol table.
2 Copyright (C) 2012-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "print-tree.h"
28 #include "varasm.h"
29 #include "function.h"
30 #include "emit-rtl.h"
31 #include "gimple.h"
32 #include "tree-inline.h"
33 #include "langhooks.h"
34 #include "hashtab.h"
35 #include "ggc.h"
36 #include "cgraph.h"
37 #include "diagnostic.h"
38 #include "timevar.h"
39 #include "lto-streamer.h"
40 #include "output.h"
41
42 const char * const ld_plugin_symbol_resolution_names[]=
43 {
44 "",
45 "undef",
46 "prevailing_def",
47 "prevailing_def_ironly",
48 "preempted_reg",
49 "preempted_ir",
50 "resolved_ir",
51 "resolved_exec",
52 "resolved_dyn",
53 "prevailing_def_ironly_exp"
54 };
55
56 /* Hash table used to convert declarations into nodes. */
57 static GTY((param_is (symtab_node))) htab_t symtab_hash;
58 /* Hash table used to convert assembler names into nodes. */
59 static GTY((param_is (symtab_node))) htab_t assembler_name_hash;
60
61 /* Linked list of symbol table nodes. */
62 symtab_node *symtab_nodes;
63
64 /* The order index of the next symtab node to be created. This is
65 used so that we can sort the cgraph nodes in order by when we saw
66 them, to support -fno-toplevel-reorder. */
67 int symtab_order;
68
69 /* Returns a hash code for P. */
70
71 static hashval_t
72 hash_node (const void *p)
73 {
74 const symtab_node *n = (const symtab_node *) p;
75 return (hashval_t) DECL_UID (n->decl);
76 }
77
78
79 /* Returns nonzero if P1 and P2 are equal. */
80
81 static int
82 eq_node (const void *p1, const void *p2)
83 {
84 const symtab_node *n1 = (const symtab_node *) p1;
85 const symtab_node *n2 = (const symtab_node *) p2;
86 return DECL_UID (n1->decl) == DECL_UID (n2->decl);
87 }
88
89 /* Hash asmnames ignoring the user specified marks. */
90
91 static hashval_t
92 decl_assembler_name_hash (const_tree asmname)
93 {
94 if (IDENTIFIER_POINTER (asmname)[0] == '*')
95 {
96 const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
97 size_t ulp_len = strlen (user_label_prefix);
98
99 if (ulp_len == 0)
100 ;
101 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
102 decl_str += ulp_len;
103
104 return htab_hash_string (decl_str);
105 }
106
107 return htab_hash_string (IDENTIFIER_POINTER (asmname));
108 }
109
110
111 /* Returns a hash code for P. */
112
113 static hashval_t
114 hash_node_by_assembler_name (const void *p)
115 {
116 const symtab_node *n = (const symtab_node *) p;
117 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
118 }
119
120 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
121
122 static bool
123 decl_assembler_name_equal (tree decl, const_tree asmname)
124 {
125 tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
126 const char *decl_str;
127 const char *asmname_str;
128 bool test = false;
129
130 if (decl_asmname == asmname)
131 return true;
132
133 decl_str = IDENTIFIER_POINTER (decl_asmname);
134 asmname_str = IDENTIFIER_POINTER (asmname);
135
136
137 /* If the target assembler name was set by the user, things are trickier.
138 We have a leading '*' to begin with. After that, it's arguable what
139 is the correct thing to do with -fleading-underscore. Arguably, we've
140 historically been doing the wrong thing in assemble_alias by always
141 printing the leading underscore. Since we're not changing that, make
142 sure user_label_prefix follows the '*' before matching. */
143 if (decl_str[0] == '*')
144 {
145 size_t ulp_len = strlen (user_label_prefix);
146
147 decl_str ++;
148
149 if (ulp_len == 0)
150 test = true;
151 else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
152 decl_str += ulp_len, test=true;
153 else
154 decl_str --;
155 }
156 if (asmname_str[0] == '*')
157 {
158 size_t ulp_len = strlen (user_label_prefix);
159
160 asmname_str ++;
161
162 if (ulp_len == 0)
163 test = true;
164 else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
165 asmname_str += ulp_len, test=true;
166 else
167 asmname_str --;
168 }
169
170 if (!test)
171 return false;
172 return strcmp (decl_str, asmname_str) == 0;
173 }
174
175
176 /* Returns nonzero if P1 and P2 are equal. */
177
178 static int
179 eq_assembler_name (const void *p1, const void *p2)
180 {
181 const symtab_node *n1 = (const symtab_node *) p1;
182 const_tree name = (const_tree)p2;
183 return (decl_assembler_name_equal (n1->decl, name));
184 }
185
186 /* Insert NODE to assembler name hash. */
187
188 static void
189 insert_to_assembler_name_hash (symtab_node *node, bool with_clones)
190 {
191 if (is_a <varpool_node> (node) && DECL_HARD_REGISTER (node->decl))
192 return;
193 gcc_checking_assert (!node->previous_sharing_asm_name
194 && !node->next_sharing_asm_name);
195 if (assembler_name_hash)
196 {
197 void **aslot;
198 struct cgraph_node *cnode;
199 tree decl = node->decl;
200
201 tree name = DECL_ASSEMBLER_NAME (node->decl);
202
203 aslot = htab_find_slot_with_hash (assembler_name_hash, name,
204 decl_assembler_name_hash (name),
205 INSERT);
206 gcc_assert (*aslot != node);
207 node->next_sharing_asm_name = (symtab_node *)*aslot;
208 if (*aslot != NULL)
209 ((symtab_node *)*aslot)->previous_sharing_asm_name = node;
210 *aslot = node;
211
212 /* Update also possible inline clones sharing a decl. */
213 cnode = dyn_cast <cgraph_node> (node);
214 if (cnode && cnode->clones && with_clones)
215 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
216 if (cnode->decl == decl)
217 insert_to_assembler_name_hash (cnode, true);
218 }
219
220 }
221
222 /* Remove NODE from assembler name hash. */
223
224 static void
225 unlink_from_assembler_name_hash (symtab_node *node, bool with_clones)
226 {
227 if (assembler_name_hash)
228 {
229 struct cgraph_node *cnode;
230 tree decl = node->decl;
231
232 if (node->next_sharing_asm_name)
233 node->next_sharing_asm_name->previous_sharing_asm_name
234 = node->previous_sharing_asm_name;
235 if (node->previous_sharing_asm_name)
236 {
237 node->previous_sharing_asm_name->next_sharing_asm_name
238 = node->next_sharing_asm_name;
239 }
240 else
241 {
242 tree name = DECL_ASSEMBLER_NAME (node->decl);
243 void **slot;
244 slot = htab_find_slot_with_hash (assembler_name_hash, name,
245 decl_assembler_name_hash (name),
246 NO_INSERT);
247 gcc_assert (*slot == node);
248 if (!node->next_sharing_asm_name)
249 htab_clear_slot (assembler_name_hash, slot);
250 else
251 *slot = node->next_sharing_asm_name;
252 }
253 node->next_sharing_asm_name = NULL;
254 node->previous_sharing_asm_name = NULL;
255
256 /* Update also possible inline clones sharing a decl. */
257 cnode = dyn_cast <cgraph_node> (node);
258 if (cnode && cnode->clones && with_clones)
259 for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
260 if (cnode->decl == decl)
261 unlink_from_assembler_name_hash (cnode, true);
262 }
263 }
264
265 /* Arrange node to be first in its entry of assembler_name_hash. */
266
267 void
268 symtab_prevail_in_asm_name_hash (symtab_node *node)
269 {
270 unlink_from_assembler_name_hash (node, false);
271 insert_to_assembler_name_hash (node, false);
272 }
273
274
275 /* Add node into symbol table. This function is not used directly, but via
276 cgraph/varpool node creation routines. */
277
278 void
279 symtab_register_node (symtab_node *node)
280 {
281 struct symtab_node key;
282 symtab_node **slot;
283
284 node->next = symtab_nodes;
285 node->previous = NULL;
286 if (symtab_nodes)
287 symtab_nodes->previous = node;
288 symtab_nodes = node;
289
290 if (!symtab_hash)
291 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
292 key.decl = node->decl;
293 slot = (symtab_node **) htab_find_slot (symtab_hash, &key, INSERT);
294 if (*slot == NULL)
295 *slot = node;
296
297 ipa_empty_ref_list (&node->ref_list);
298
299 node->order = symtab_order++;
300
301 /* Be sure to do this last; C++ FE might create new nodes via
302 DECL_ASSEMBLER_NAME langhook! */
303 insert_to_assembler_name_hash (node, false);
304 }
305
306 /* Make NODE to be the one symtab hash is pointing to. Used when reshaping tree
307 of inline clones. */
308
309 void
310 symtab_insert_node_to_hashtable (symtab_node *node)
311 {
312 struct symtab_node key;
313 symtab_node **slot;
314
315 if (!symtab_hash)
316 symtab_hash = htab_create_ggc (10, hash_node, eq_node, NULL);
317 key.decl = node->decl;
318 slot = (symtab_node **) htab_find_slot (symtab_hash, &key, INSERT);
319 *slot = node;
320 }
321
322 /* Remove node from symbol table. This function is not used directly, but via
323 cgraph/varpool node removal routines. */
324
325 void
326 symtab_unregister_node (symtab_node *node)
327 {
328 void **slot;
329 ipa_remove_all_references (&node->ref_list);
330 ipa_remove_all_referring (&node->ref_list);
331
332 if (node->same_comdat_group)
333 {
334 symtab_node *prev;
335 for (prev = node->same_comdat_group;
336 prev->same_comdat_group != node;
337 prev = prev->same_comdat_group)
338 ;
339 if (node->same_comdat_group == prev)
340 prev->same_comdat_group = NULL;
341 else
342 prev->same_comdat_group = node->same_comdat_group;
343 node->same_comdat_group = NULL;
344 }
345
346 if (node->previous)
347 node->previous->next = node->next;
348 else
349 symtab_nodes = node->next;
350 if (node->next)
351 node->next->previous = node->previous;
352 node->next = NULL;
353 node->previous = NULL;
354
355 slot = htab_find_slot (symtab_hash, node, NO_INSERT);
356
357 /* During LTO symtab merging we temporarily corrupt decl to symtab node
358 hash. */
359 gcc_assert ((slot && *slot) || in_lto_p);
360 if (slot && *slot && *slot == node)
361 {
362 symtab_node *replacement_node = NULL;
363 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
364 replacement_node = cgraph_find_replacement_node (cnode);
365 if (!replacement_node)
366 htab_clear_slot (symtab_hash, slot);
367 else
368 *slot = replacement_node;
369 }
370 if (!is_a <varpool_node> (node) || !DECL_HARD_REGISTER (node->decl))
371 unlink_from_assembler_name_hash (node, false);
372 }
373
374 /* Return symbol table node associated with DECL, if any,
375 and NULL otherwise. */
376
377 symtab_node *
378 symtab_get_node (const_tree decl)
379 {
380 symtab_node **slot;
381 struct symtab_node key;
382
383 #ifdef ENABLE_CHECKING
384 /* Check that we are called for sane type of object - functions
385 and static or external variables. */
386 gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL
387 || (TREE_CODE (decl) == VAR_DECL
388 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
389 || in_lto_p)));
390 #endif
391
392 if (!symtab_hash)
393 return NULL;
394
395 key.decl = CONST_CAST2 (tree, const_tree, decl);
396
397 slot = (symtab_node **) htab_find_slot (symtab_hash, &key,
398 NO_INSERT);
399
400 if (slot)
401 return *slot;
402 return NULL;
403 }
404
405 /* Remove symtab NODE from the symbol table. */
406
407 void
408 symtab_remove_node (symtab_node *node)
409 {
410 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
411 cgraph_remove_node (cnode);
412 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
413 varpool_remove_node (vnode);
414 }
415
416 /* Initalize asm name hash unless. */
417
418 void
419 symtab_initialize_asm_name_hash (void)
420 {
421 symtab_node *node;
422 if (!assembler_name_hash)
423 {
424 assembler_name_hash =
425 htab_create_ggc (10, hash_node_by_assembler_name, eq_assembler_name,
426 NULL);
427 FOR_EACH_SYMBOL (node)
428 insert_to_assembler_name_hash (node, false);
429 }
430 }
431
432 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
433 Return NULL if there's no such node. */
434
435 symtab_node *
436 symtab_node_for_asm (const_tree asmname)
437 {
438 symtab_node *node;
439 void **slot;
440
441 symtab_initialize_asm_name_hash ();
442 slot = htab_find_slot_with_hash (assembler_name_hash, asmname,
443 decl_assembler_name_hash (asmname),
444 NO_INSERT);
445
446 if (slot)
447 {
448 node = (symtab_node *) *slot;
449 return node;
450 }
451 return NULL;
452 }
453
454 /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables. */
455
456 void
457 change_decl_assembler_name (tree decl, tree name)
458 {
459 symtab_node *node = NULL;
460
461 /* We can have user ASM names on things, like global register variables, that
462 are not in the symbol table. */
463 if ((TREE_CODE (decl) == VAR_DECL
464 && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
465 || TREE_CODE (decl) == FUNCTION_DECL)
466 node = symtab_get_node (decl);
467 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
468 {
469 SET_DECL_ASSEMBLER_NAME (decl, name);
470 if (node)
471 insert_to_assembler_name_hash (node, true);
472 }
473 else
474 {
475 if (name == DECL_ASSEMBLER_NAME (decl))
476 return;
477
478 tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
479 ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
480 : NULL);
481 if (node)
482 unlink_from_assembler_name_hash (node, true);
483 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
484 && DECL_RTL_SET_P (decl))
485 warning (0, "%D renamed after being referenced in assembly", decl);
486
487 SET_DECL_ASSEMBLER_NAME (decl, name);
488 if (alias)
489 {
490 IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
491 TREE_CHAIN (DECL_ASSEMBLER_NAME (name)) = alias;
492 }
493 if (node)
494 insert_to_assembler_name_hash (node, true);
495 }
496 }
497
498 /* Add NEW_ to the same comdat group that OLD is in. */
499
500 void
501 symtab_add_to_same_comdat_group (symtab_node *new_node,
502 symtab_node *old_node)
503 {
504 gcc_assert (DECL_ONE_ONLY (old_node->decl));
505 gcc_assert (!new_node->same_comdat_group);
506 gcc_assert (new_node != old_node);
507
508 DECL_COMDAT_GROUP (new_node->decl) = DECL_COMDAT_GROUP (old_node->decl);
509 new_node->same_comdat_group = old_node;
510 if (!old_node->same_comdat_group)
511 old_node->same_comdat_group = new_node;
512 else
513 {
514 symtab_node *n;
515 for (n = old_node->same_comdat_group;
516 n->same_comdat_group != old_node;
517 n = n->same_comdat_group)
518 ;
519 n->same_comdat_group = new_node;
520 }
521 }
522
523 /* Dissolve the same_comdat_group list in which NODE resides. */
524
525 void
526 symtab_dissolve_same_comdat_group_list (symtab_node *node)
527 {
528 symtab_node *n = node;
529 symtab_node *next;
530
531 if (!node->same_comdat_group)
532 return;
533 do
534 {
535 next = n->same_comdat_group;
536 n->same_comdat_group = NULL;
537 n = next;
538 }
539 while (n != node);
540 }
541
542 /* Return printable assembler name of NODE.
543 This function is used only for debugging. When assembler name
544 is unknown go with identifier name. */
545
546 const char *
547 symtab_node::asm_name () const
548 {
549 if (!DECL_ASSEMBLER_NAME_SET_P (decl))
550 return lang_hooks.decl_printable_name (decl, 2);
551 return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
552 }
553
554 /* Return printable identifier name. */
555
556 const char *
557 symtab_node::name () const
558 {
559 return lang_hooks.decl_printable_name (decl, 2);
560 }
561
562 static const char * const symtab_type_names[] = {"symbol", "function", "variable"};
563
564 /* Dump base fields of symtab nodes. Not to be used directly. */
565
566 void
567 dump_symtab_base (FILE *f, symtab_node *node)
568 {
569 static const char * const visibility_types[] = {
570 "default", "protected", "hidden", "internal"
571 };
572
573 fprintf (f, "%s/%i (%s)",
574 node->asm_name (),
575 node->order,
576 node->name ());
577 dump_addr (f, " @", (void *)node);
578 fprintf (f, "\n Type: %s", symtab_type_names[node->type]);
579
580 if (node->definition)
581 fprintf (f, " definition");
582 if (node->analyzed)
583 fprintf (f, " analyzed");
584 if (node->alias)
585 fprintf (f, " alias");
586 if (node->weakref)
587 fprintf (f, " weakref");
588 if (node->cpp_implicit_alias)
589 fprintf (f, " cpp_implicit_alias");
590 if (node->alias_target)
591 fprintf (f, " target:%s",
592 DECL_P (node->alias_target)
593 ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
594 (node->alias_target))
595 : IDENTIFIER_POINTER (node->alias_target));
596 fprintf (f, "\n Visibility:");
597 if (node->in_other_partition)
598 fprintf (f, " in_other_partition");
599 if (node->used_from_other_partition)
600 fprintf (f, " used_from_other_partition");
601 if (node->force_output)
602 fprintf (f, " force_output");
603 if (node->forced_by_abi)
604 fprintf (f, " forced_by_abi");
605 if (node->externally_visible)
606 fprintf (f, " externally_visible");
607 if (node->resolution != LDPR_UNKNOWN)
608 fprintf (f, " %s",
609 ld_plugin_symbol_resolution_names[(int)node->resolution]);
610 if (TREE_ASM_WRITTEN (node->decl))
611 fprintf (f, " asm_written");
612 if (DECL_EXTERNAL (node->decl))
613 fprintf (f, " external");
614 if (TREE_PUBLIC (node->decl))
615 fprintf (f, " public");
616 if (DECL_COMMON (node->decl))
617 fprintf (f, " common");
618 if (DECL_WEAK (node->decl))
619 fprintf (f, " weak");
620 if (DECL_DLLIMPORT_P (node->decl))
621 fprintf (f, " dll_import");
622 if (DECL_COMDAT (node->decl))
623 fprintf (f, " comdat");
624 if (DECL_COMDAT_GROUP (node->decl))
625 fprintf (f, " comdat_group:%s",
626 IDENTIFIER_POINTER (DECL_COMDAT_GROUP (node->decl)));
627 if (DECL_ONE_ONLY (node->decl))
628 fprintf (f, " one_only");
629 if (DECL_SECTION_NAME (node->decl))
630 fprintf (f, " section_name:%s",
631 TREE_STRING_POINTER (DECL_SECTION_NAME (node->decl)));
632 if (DECL_VISIBILITY_SPECIFIED (node->decl))
633 fprintf (f, " visibility_specified");
634 if (DECL_VISIBILITY (node->decl))
635 fprintf (f, " visibility:%s",
636 visibility_types [DECL_VISIBILITY (node->decl)]);
637 if (DECL_VIRTUAL_P (node->decl))
638 fprintf (f, " virtual");
639 if (DECL_ARTIFICIAL (node->decl))
640 fprintf (f, " artificial");
641 if (TREE_CODE (node->decl) == FUNCTION_DECL)
642 {
643 if (DECL_STATIC_CONSTRUCTOR (node->decl))
644 fprintf (f, " constructor");
645 if (DECL_STATIC_DESTRUCTOR (node->decl))
646 fprintf (f, " destructor");
647 }
648 fprintf (f, "\n");
649
650 if (node->same_comdat_group)
651 fprintf (f, " Same comdat group as: %s/%i\n",
652 node->same_comdat_group->asm_name (),
653 node->same_comdat_group->order);
654 if (node->next_sharing_asm_name)
655 fprintf (f, " next sharing asm name: %i\n",
656 node->next_sharing_asm_name->order);
657 if (node->previous_sharing_asm_name)
658 fprintf (f, " previous sharing asm name: %i\n",
659 node->previous_sharing_asm_name->order);
660
661 if (node->address_taken)
662 fprintf (f, " Address is taken.\n");
663 if (node->aux)
664 {
665 fprintf (f, " Aux:");
666 dump_addr (f, " @", (void *)node->aux);
667 }
668
669 fprintf (f, " References: ");
670 ipa_dump_references (f, &node->ref_list);
671 fprintf (f, " Referring: ");
672 ipa_dump_referring (f, &node->ref_list);
673 if (node->lto_file_data)
674 fprintf (f, " Read from file: %s\n",
675 node->lto_file_data->file_name);
676 }
677
678 /* Dump symtab node. */
679
680 void
681 dump_symtab_node (FILE *f, symtab_node *node)
682 {
683 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
684 dump_cgraph_node (f, cnode);
685 else if (varpool_node *vnode = dyn_cast <varpool_node> (node))
686 dump_varpool_node (f, vnode);
687 }
688
689 /* Dump symbol table. */
690
691 void
692 dump_symtab (FILE *f)
693 {
694 symtab_node *node;
695 fprintf (f, "Symbol table:\n\n");
696 FOR_EACH_SYMBOL (node)
697 dump_symtab_node (f, node);
698 }
699
700 /* Dump symtab node NODE to stderr. */
701
702 DEBUG_FUNCTION void
703 debug_symtab_node (symtab_node *node)
704 {
705 dump_symtab_node (stderr, node);
706 }
707
708 /* Dump symbol table to stderr. */
709
710 DEBUG_FUNCTION void
711 debug_symtab (void)
712 {
713 dump_symtab (stderr);
714 }
715
716 /* Verify common part of symtab nodes. */
717
718 DEBUG_FUNCTION bool
719 verify_symtab_base (symtab_node *node)
720 {
721 bool error_found = false;
722 symtab_node *hashed_node;
723
724 if (is_a <cgraph_node> (node))
725 {
726 if (TREE_CODE (node->decl) != FUNCTION_DECL)
727 {
728 error ("function symbol is not function");
729 error_found = true;
730 }
731 }
732 else if (is_a <varpool_node> (node))
733 {
734 if (TREE_CODE (node->decl) != VAR_DECL)
735 {
736 error ("variable symbol is not variable");
737 error_found = true;
738 }
739 }
740 else
741 {
742 error ("node has unknown type");
743 error_found = true;
744 }
745
746 if (cgraph_state != CGRAPH_LTO_STREAMING)
747 {
748 hashed_node = symtab_get_node (node->decl);
749 if (!hashed_node)
750 {
751 error ("node not found in symtab decl hashtable");
752 error_found = true;
753 }
754 if (hashed_node != node
755 && (!is_a <cgraph_node> (node)
756 || !dyn_cast <cgraph_node> (node)->clone_of
757 || dyn_cast <cgraph_node> (node)->clone_of->decl
758 != node->decl))
759 {
760 error ("node differs from symtab decl hashtable");
761 error_found = true;
762 }
763 }
764 if (assembler_name_hash)
765 {
766 hashed_node = symtab_node_for_asm (DECL_ASSEMBLER_NAME (node->decl));
767 if (hashed_node && hashed_node->previous_sharing_asm_name)
768 {
769 error ("assembler name hash list corrupted");
770 error_found = true;
771 }
772 while (hashed_node)
773 {
774 if (hashed_node == node)
775 break;
776 hashed_node = hashed_node->next_sharing_asm_name;
777 }
778 if (!hashed_node
779 && !(is_a <varpool_node> (node)
780 || DECL_HARD_REGISTER (node->decl)))
781 {
782 error ("node not found in symtab assembler name hash");
783 error_found = true;
784 }
785 }
786 if (node->previous_sharing_asm_name
787 && node->previous_sharing_asm_name->next_sharing_asm_name != node)
788 {
789 error ("double linked list of assembler names corrupted");
790 error_found = true;
791 }
792 if (node->analyzed && !node->definition)
793 {
794 error ("node is analyzed byt it is not a definition");
795 error_found = true;
796 }
797 if (node->cpp_implicit_alias && !node->alias)
798 {
799 error ("node is alias but not implicit alias");
800 error_found = true;
801 }
802 if (node->alias && !node->definition
803 && !node->weakref)
804 {
805 error ("node is alias but not definition");
806 error_found = true;
807 }
808 if (node->weakref && !node->alias)
809 {
810 error ("node is weakref but not an alias");
811 error_found = true;
812 }
813 if (node->same_comdat_group)
814 {
815 symtab_node *n = node->same_comdat_group;
816
817 if (!DECL_ONE_ONLY (n->decl))
818 {
819 error ("non-DECL_ONE_ONLY node in a same_comdat_group list");
820 error_found = true;
821 }
822 if (n->type != node->type)
823 {
824 error ("mixing different types of symbol in same comdat groups is not supported");
825 error_found = true;
826 }
827 if (n == node)
828 {
829 error ("node is alone in a comdat group");
830 error_found = true;
831 }
832 do
833 {
834 if (!n->same_comdat_group)
835 {
836 error ("same_comdat_group is not a circular list");
837 error_found = true;
838 break;
839 }
840 n = n->same_comdat_group;
841 }
842 while (n != node);
843 }
844 return error_found;
845 }
846
847 /* Verify consistency of NODE. */
848
849 DEBUG_FUNCTION void
850 verify_symtab_node (symtab_node *node)
851 {
852 if (seen_error ())
853 return;
854
855 timevar_push (TV_CGRAPH_VERIFY);
856 if (cgraph_node *cnode = dyn_cast <cgraph_node> (node))
857 verify_cgraph_node (cnode);
858 else
859 if (verify_symtab_base (node))
860 {
861 dump_symtab_node (stderr, node);
862 internal_error ("verify_symtab_node failed");
863 }
864 timevar_pop (TV_CGRAPH_VERIFY);
865 }
866
867 /* Verify symbol table for internal consistency. */
868
869 DEBUG_FUNCTION void
870 verify_symtab (void)
871 {
872 symtab_node *node;
873 FOR_EACH_SYMBOL (node)
874 verify_symtab_node (node);
875 }
876
877 /* Return true when RESOLUTION indicate that linker will use
878 the symbol from non-LTO object files. */
879
880 bool
881 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
882 {
883 return (resolution == LDPR_PREVAILING_DEF
884 || resolution == LDPR_PREEMPTED_REG
885 || resolution == LDPR_RESOLVED_EXEC
886 || resolution == LDPR_RESOLVED_DYN);
887 }
888
889 /* Return true when NODE is known to be used from other (non-LTO) object file.
890 Known only when doing LTO via linker plugin. */
891
892 bool
893 symtab_used_from_object_file_p (symtab_node *node)
894 {
895 if (!TREE_PUBLIC (node->decl) || DECL_EXTERNAL (node->decl))
896 return false;
897 if (resolution_used_from_other_file_p (node->resolution))
898 return true;
899 return false;
900 }
901
902 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
903 but other code such as notice_global_symbol generates rtl. */
904
905 void
906 symtab_make_decl_local (tree decl)
907 {
908 rtx rtl, symbol;
909
910 if (TREE_CODE (decl) == VAR_DECL)
911 DECL_COMMON (decl) = 0;
912 else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);
913
914 if (DECL_ONE_ONLY (decl) || DECL_COMDAT (decl))
915 {
916 DECL_SECTION_NAME (decl) = 0;
917 DECL_COMDAT (decl) = 0;
918 }
919 DECL_COMDAT_GROUP (decl) = 0;
920 DECL_WEAK (decl) = 0;
921 DECL_EXTERNAL (decl) = 0;
922 DECL_VISIBILITY_SPECIFIED (decl) = 0;
923 DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
924 TREE_PUBLIC (decl) = 0;
925 if (!DECL_RTL_SET_P (decl))
926 return;
927
928 /* Update rtl flags. */
929 make_decl_rtl (decl);
930
931 rtl = DECL_RTL (decl);
932 if (!MEM_P (rtl))
933 return;
934
935 symbol = XEXP (rtl, 0);
936 if (GET_CODE (symbol) != SYMBOL_REF)
937 return;
938
939 SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
940 }
941
942 /* Return availability of NODE. */
943
944 enum availability
945 symtab_node_availability (symtab_node *node)
946 {
947 if (is_a <cgraph_node> (node))
948 return cgraph_function_body_availability (cgraph (node));
949 else
950 return cgraph_variable_initializer_availability (varpool (node));
951 }
952
953 /* Given NODE, walk the alias chain to return the symbol NODE is alias of.
954 If NODE is not an alias, return NODE.
955 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
956
957 symtab_node *
958 symtab_alias_ultimate_target (symtab_node *node, enum availability *availability)
959 {
960 bool weakref_p = false;
961
962 if (!node->alias)
963 {
964 if (availability)
965 *availability = symtab_node_availability (node);
966 return node;
967 }
968
969 /* To determine visibility of the target, we follow ELF semantic of aliases.
970 Here alias is an alternative assembler name of a given definition. Its
971 availability prevails the availability of its target (i.e. static alias of
972 weak definition is available.
973
974 Weakref is a different animal (and not part of ELF per se). It is just
975 alternative name of a given symbol used within one complation unit
976 and is translated prior hitting the object file. It inherits the
977 visibility of its target (i.e. weakref of non-overwritable definition
978 is non-overwritable, while weakref of weak definition is weak).
979
980 If we ever get into supporting targets with different semantics, a target
981 hook will be needed here. */
982
983 if (availability)
984 {
985 weakref_p = node->weakref;
986 if (!weakref_p)
987 *availability = symtab_node_availability (node);
988 else
989 *availability = AVAIL_LOCAL;
990 }
991 while (node)
992 {
993 if (node->alias && node->analyzed)
994 node = symtab_alias_target (node);
995 else
996 {
997 if (!availability)
998 ;
999 else if (node->analyzed)
1000 {
1001 if (weakref_p)
1002 {
1003 enum availability a = symtab_node_availability (node);
1004 if (a < *availability)
1005 *availability = a;
1006 }
1007 }
1008 else
1009 *availability = AVAIL_NOT_AVAILABLE;
1010 return node;
1011 }
1012 if (node && availability && weakref_p)
1013 {
1014 enum availability a = symtab_node_availability (node);
1015 if (a < *availability)
1016 *availability = a;
1017 weakref_p = node->weakref;
1018 }
1019 }
1020 if (availability)
1021 *availability = AVAIL_NOT_AVAILABLE;
1022 return NULL;
1023 }
1024
1025 /* C++ FE sometimes change linkage flags after producing same body aliases.
1026
1027 FIXME: C++ produce implicit aliases for virtual functions and vtables that
1028 are obviously equivalent. The way it is doing so is however somewhat
1029 kludgy and interferes with the visibility code. As a result we need to
1030 copy the visibility from the target to get things right. */
1031
1032 void
1033 fixup_same_cpp_alias_visibility (symtab_node *node, symtab_node *target)
1034 {
1035 if (is_a <cgraph_node> (node))
1036 {
1037 DECL_DECLARED_INLINE_P (node->decl)
1038 = DECL_DECLARED_INLINE_P (target->decl);
1039 DECL_DISREGARD_INLINE_LIMITS (node->decl)
1040 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1041 }
1042 /* FIXME: It is not really clear why those flags should not be copied for
1043 functions, too. */
1044 else
1045 {
1046 DECL_WEAK (node->decl) = DECL_WEAK (target->decl);
1047 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1048 DECL_VISIBILITY (node->decl) = DECL_VISIBILITY (target->decl);
1049 }
1050 DECL_VIRTUAL_P (node->decl) = DECL_VIRTUAL_P (target->decl);
1051 if (TREE_PUBLIC (node->decl))
1052 {
1053 DECL_EXTERNAL (node->decl) = DECL_EXTERNAL (target->decl);
1054 DECL_COMDAT (node->decl) = DECL_COMDAT (target->decl);
1055 DECL_COMDAT_GROUP (node->decl)
1056 = DECL_COMDAT_GROUP (target->decl);
1057 if (DECL_ONE_ONLY (target->decl)
1058 && !node->same_comdat_group)
1059 symtab_add_to_same_comdat_group (node, target);
1060 }
1061 node->externally_visible = target->externally_visible;
1062 }
1063
1064 /* Add reference recording that NODE is alias of TARGET.
1065 The function can fail in the case of aliasing cycles; in this case
1066 it returns false. */
1067
1068 bool
1069 symtab_resolve_alias (symtab_node *node, symtab_node *target)
1070 {
1071 symtab_node *n;
1072
1073 gcc_assert (!node->analyzed
1074 && !vec_safe_length (node->ref_list.references));
1075
1076 /* Never let cycles to creep into the symbol table alias references;
1077 those will make alias walkers to be infinite. */
1078 for (n = target; n && n->alias;
1079 n = n->analyzed ? symtab_alias_target (n) : NULL)
1080 if (n == node)
1081 {
1082 if (is_a <cgraph_node> (node))
1083 error ("function %q+D part of alias cycle", node->decl);
1084 else if (is_a <varpool_node> (node))
1085 error ("variable %q+D part of alias cycle", node->decl);
1086 else
1087 gcc_unreachable ();
1088 node->alias = false;
1089 return false;
1090 }
1091
1092 /* "analyze" the node - i.e. mark the reference. */
1093 node->definition = true;
1094 node->alias = true;
1095 node->analyzed = true;
1096 ipa_record_reference (node, target, IPA_REF_ALIAS, NULL);
1097
1098 /* Alias targets become reudndant after alias is resolved into an reference.
1099 We do not want to keep it around or we would have to mind updating them
1100 when renaming symbols. */
1101 node->alias_target = NULL;
1102
1103 if (node->cpp_implicit_alias && cgraph_state >= CGRAPH_STATE_CONSTRUCTION)
1104 fixup_same_cpp_alias_visibility (node, target);
1105
1106 /* If alias has address taken, so does the target. */
1107 if (node->address_taken)
1108 symtab_alias_ultimate_target (target, NULL)->address_taken = true;
1109 return true;
1110 }
1111
1112 /* Call calback on NODE and aliases associated to NODE.
1113 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1114 skipped. */
1115
1116 bool
1117 symtab_for_node_and_aliases (symtab_node *node,
1118 bool (*callback) (symtab_node *, void *),
1119 void *data,
1120 bool include_overwritable)
1121 {
1122 int i;
1123 struct ipa_ref *ref;
1124
1125 if (callback (node, data))
1126 return true;
1127 for (i = 0; ipa_ref_list_referring_iterate (&node->ref_list, i, ref); i++)
1128 if (ref->use == IPA_REF_ALIAS)
1129 {
1130 symtab_node *alias = ref->referring;
1131 if (include_overwritable
1132 || symtab_node_availability (alias) > AVAIL_OVERWRITABLE)
1133 if (symtab_for_node_and_aliases (alias, callback, data,
1134 include_overwritable))
1135 return true;
1136 }
1137 return false;
1138 }
1139
1140 /* Worker searching nonoverwritable alias. */
1141
1142 static bool
1143 symtab_nonoverwritable_alias_1 (symtab_node *node, void *data)
1144 {
1145 if (decl_binds_to_current_def_p (node->decl))
1146 {
1147 *(symtab_node **)data = node;
1148 return true;
1149 }
1150 return false;
1151 }
1152
1153 /* If NODE can not be overwriten by static or dynamic linker to point to different
1154 definition, return NODE. Otherwise look for alias with such property and if
1155 none exists, introduce new one. */
1156
1157 symtab_node *
1158 symtab_nonoverwritable_alias (symtab_node *node)
1159 {
1160 tree new_decl;
1161 symtab_node *new_node = NULL;
1162
1163 /* First try to look up existing alias or base object
1164 (if that is already non-overwritable). */
1165 node = symtab_alias_ultimate_target (node, NULL);
1166 gcc_assert (!node->alias && !node->weakref);
1167 symtab_for_node_and_aliases (node, symtab_nonoverwritable_alias_1,
1168 (void *)&new_node, true);
1169 if (new_node)
1170 return new_node;
1171 #ifndef ASM_OUTPUT_DEF
1172 /* If aliases aren't supported by the assembler, fail. */
1173 return NULL;
1174 #endif
1175
1176 /* Otherwise create a new one. */
1177 new_decl = copy_node (node->decl);
1178 DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1179 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1180 DECL_STRUCT_FUNCTION (new_decl) = NULL;
1181 DECL_INITIAL (new_decl) = NULL;
1182 SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
1183 SET_DECL_RTL (new_decl, NULL);
1184
1185 /* Update the properties. */
1186 DECL_EXTERNAL (new_decl) = 0;
1187 if (DECL_ONE_ONLY (node->decl))
1188 DECL_SECTION_NAME (new_decl) = NULL;
1189 DECL_COMDAT_GROUP (new_decl) = 0;
1190 TREE_PUBLIC (new_decl) = 0;
1191 DECL_COMDAT (new_decl) = 0;
1192 DECL_WEAK (new_decl) = 0;
1193 DECL_VIRTUAL_P (new_decl) = 0;
1194 if (TREE_CODE (new_decl) == FUNCTION_DECL)
1195 {
1196 DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
1197 DECL_STATIC_DESTRUCTOR (new_decl) = 0;
1198 new_node = cgraph_create_function_alias
1199 (new_decl, node->decl);
1200 }
1201 else
1202 new_node = varpool_create_variable_alias (new_decl,
1203 node->decl);
1204 symtab_resolve_alias (new_node, node);
1205 gcc_assert (decl_binds_to_current_def_p (new_decl));
1206 return new_node;
1207 }
1208
1209 /* Return true if A and B represents semantically equivalent symbols. */
1210
1211 bool
1212 symtab_semantically_equivalent_p (symtab_node *a,
1213 symtab_node *b)
1214 {
1215 enum availability avail;
1216 symtab_node *ba;
1217 symtab_node *bb;
1218
1219 /* Equivalent functions are equivalent. */
1220 if (a->decl == b->decl)
1221 return true;
1222
1223 /* If symbol is not overwritable by different implementation,
1224 walk to the base object it defines. */
1225 ba = symtab_alias_ultimate_target (a, &avail);
1226 if (avail >= AVAIL_AVAILABLE)
1227 {
1228 if (ba == b)
1229 return true;
1230 }
1231 else
1232 ba = a;
1233 bb = symtab_alias_ultimate_target (b, &avail);
1234 if (avail >= AVAIL_AVAILABLE)
1235 {
1236 if (a == bb)
1237 return true;
1238 }
1239 else
1240 bb = b;
1241 return bb == ba;
1242 }
1243 #include "gt-symtab.h"