Print cgraph_uid in function header
[official-gcc.git] / gcc-4_6-mobile-vtable-security / gcc / l-ipo.c
blob2c34dfc15216a32a739d8c814a1ecdad9fe2e5f4
1 /* Copyright (C) 2009. Free Software Foundation, Inc.
2 Contributed by Xinliang David Li (davidxl@google.com) and
3 Raksit Ashok (raksit@google.com)
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tree.h"
25 #include "c-family/c-common.h"
26 #include "toplev.h"
27 #include "langhooks.h"
28 #include "langhooks-def.h"
29 #include "diagnostic.h"
30 #include "debug.h"
31 #include "gimple.h"
32 #include "cgraph.h"
33 #include "l-ipo.h"
34 #include "coverage.h"
35 #include "gcov-io.h"
36 #include "tree-pretty-print.h"
38 struct GTY(()) saved_module_scope
40 VEC(tree, gc) *module_decls;
41 unsigned module_id;
44 static GTY (()) struct saved_module_scope *current_module_scope;
45 static GTY ((param_is (struct saved_module_scope))) htab_t saved_module_scope_map;
46 static int primary_module_last_funcdef_no = 0;
47 /* Function id space for each module are qualified by the module id. After all the files
48 are parsed, we need to reset the funcdef_no to the max value from all module so that
49 the function clones do not assigned with ids colliding with some other orignal function
50 in the same module. */
51 static int max_funcdef_no = 0;
52 static location_t primary_module_last_loc;
53 /* Primary module pending templates. */
54 /* Referenced asm ids in primary module. */
55 static GTY (()) VEC(tree, gc) *referenced_asm_ids = NULL;
56 bool parser_parsing_start = false;
57 /* Nonzero if we're done parsing and into end-of-file activities. */
58 int at_eof;
60 static int aggr_has_equiv_id (tree t1, tree t2);
62 /* Module scope hash function. */
64 static hashval_t
65 htab_module_scope_hash (const void *ent)
67 const struct saved_module_scope *const entry
68 = (const struct saved_module_scope *) ent;
69 return (hashval_t) entry->module_id;
72 /* Module scope equality function. */
74 static int
75 htab_module_scope_eq (const void *ent1, const void *ent2)
77 const struct saved_module_scope *const entry1
78 = (const struct saved_module_scope *) ent1;
79 const struct saved_module_scope *const entry2
80 = (const struct saved_module_scope *) ent2;
82 return entry1->module_id == entry2->module_id;
85 /* Returns the module scope given a module id MOD_ID. */
87 static struct saved_module_scope *
88 get_module_scope (unsigned mod_id)
90 struct saved_module_scope **slot, key, *module_scope;
92 gcc_assert (mod_id);
94 if (saved_module_scope_map == NULL)
95 saved_module_scope_map = htab_create_ggc (10, htab_module_scope_hash,
96 htab_module_scope_eq, NULL);
97 key.module_id = mod_id;
98 slot = (struct saved_module_scope **)
99 htab_find_slot (saved_module_scope_map, &key, INSERT);
100 module_scope = *slot;
101 if (!module_scope)
103 module_scope = ggc_alloc_cleared_saved_module_scope ();
104 module_scope->module_id = mod_id;
105 *slot = module_scope;
107 return module_scope;
110 /* Allocate memory for struct lang_decl for tree T. */
112 static struct lang_decl *
113 alloc_lang_decl (tree t)
115 size_t size;
116 size = lang_hooks.l_ipo.get_lang_decl_size (t);
117 return ggc_alloc_cleared_lang_decl (size);
120 /* Return a cloned copy of tree SRC. */
122 tree
123 lipo_save_decl (tree src)
125 tree saved = copy_node (src);
126 enum tree_code tc = TREE_CODE (src);
127 if (TREE_CODE_CLASS (tc) == tcc_declaration)
129 struct lang_decl *ls = NULL;
130 struct function *func = NULL;
131 DECL_CONTEXT (saved) = DECL_CONTEXT (src);
132 if (DECL_LANG_SPECIFIC (src))
134 ls = alloc_lang_decl (src);
135 memcpy (ls, DECL_LANG_SPECIFIC (src),
136 lang_hooks.l_ipo.get_lang_decl_size (src));
138 DECL_LANG_SPECIFIC (saved) = ls;
139 if (tc == FUNCTION_DECL && DECL_STRUCT_FUNCTION (src))
141 func = ggc_alloc_cleared_function ();
142 *func = *(DECL_STRUCT_FUNCTION (src));
143 DECL_STRUCT_FUNCTION (saved) = func;
146 else
148 gcc_assert (TREE_CODE_CLASS (tc) == tcc_type &&
149 TYPE_MAIN_VARIANT (src) == src);
150 TYPE_CONTEXT (saved) = TYPE_CONTEXT (src);
151 lang_hooks.l_ipo.dup_lang_type (src, saved);
154 return saved;
157 /* Copy tree SAVED to tree DEST. */
159 void
160 lipo_restore_decl (tree dest, tree saved)
162 enum tree_code tc;
163 unsigned old_uid;
164 struct lang_decl *oldls;
166 tc = TREE_CODE (saved);
167 if (TREE_CODE_CLASS (tc) == tcc_declaration)
169 struct function *oldfunc = NULL;
170 old_uid = DECL_UID (dest);
171 oldls = DECL_LANG_SPECIFIC (dest);
172 oldfunc
173 = (tc == FUNCTION_DECL ? DECL_STRUCT_FUNCTION (dest) : NULL);
175 memcpy ((char *) dest + sizeof (struct tree_common),
176 (char *) saved + sizeof (struct tree_common),
177 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
179 if (tc == FUNCTION_DECL)
180 memcpy ((char *) dest + sizeof (struct tree_decl_common),
181 (char *) saved + sizeof (struct tree_decl_common),
182 sizeof (struct tree_function_decl)
183 - sizeof (struct tree_decl_common));
185 DECL_UID (dest) = old_uid;
186 if (DECL_LANG_SPECIFIC (saved))
188 if (!oldls)
189 oldls = alloc_lang_decl (dest);
190 memcpy (oldls, DECL_LANG_SPECIFIC (saved),
191 lang_hooks.l_ipo.get_lang_decl_size (saved));
192 DECL_LANG_SPECIFIC (dest) = oldls;
194 else
195 DECL_LANG_SPECIFIC (dest) = NULL;
197 if (tc == FUNCTION_DECL)
199 if (DECL_STRUCT_FUNCTION (saved))
201 if (!oldfunc)
202 oldfunc = ggc_alloc_cleared_function ();
203 *oldfunc = *(DECL_STRUCT_FUNCTION (saved));
204 DECL_STRUCT_FUNCTION (dest) = oldfunc;
206 else
207 DECL_STRUCT_FUNCTION (dest) = NULL;
210 else
212 gcc_assert (TREE_CODE_CLASS (tc) == tcc_type);
213 lang_hooks.l_ipo.copy_lang_type (saved, dest);
218 /* Return the name for tree TD which is either a decl or type. */
220 tree
221 get_type_or_decl_name (tree td)
223 tree id;
225 if (DECL_P (td))
226 id = DECL_NAME (td);
227 else
229 id = TYPE_NAME (td);
230 if (DECL_P (id))
231 id = DECL_NAME (id);
233 return id;
236 /* For a DECL (a type or a decl) in SCOPE, check to see if it is in
237 global or namespace scope. If yes, add it to the current module scope. */
239 void
240 add_decl_to_current_module_scope (tree decl, void *scope)
242 struct saved_module_scope *module_scope;
243 tree id;
245 if (!flag_dyn_ipa)
246 return;
248 if (!parser_parsing_start)
250 /* The source file may contains only global variable declations
251 -- there is no module grouping data associated with it, so
252 neither primary_module_id nor current_module_id is set. */
253 lang_hooks.l_ipo.add_built_in_decl (decl);
254 return;
257 if (!L_IPO_COMP_MODE)
258 return;
260 if (!lang_hooks.l_ipo.has_global_name (decl, scope))
261 return;
263 /* Unlike C++ where names are attached to type decls, for C, the type name
264 is identifier node. Thus we need to track type names as well. */
265 id = get_type_or_decl_name (decl);
266 if (!id)
267 return;
269 module_scope = current_module_scope;
270 gcc_assert (module_scope && module_scope->module_id == current_module_id);
271 VEC_safe_push (tree, gc, module_scope->module_decls, decl);
274 /* Clear name bindings for all decls created in MODULE_SCOPE. */
276 static void
277 clear_module_scope_bindings (struct saved_module_scope *module_scope)
279 size_t i;
280 tree decl;
282 for (i = 0;
283 VEC_iterate (tree, module_scope->module_decls, i, decl);
284 ++i)
286 lang_hooks.l_ipo.clear_global_name_bindings (
287 get_type_or_decl_name (decl));
288 /* Now force creating assembly name. */
289 if (VAR_OR_FUNCTION_DECL_P (decl))
291 tree assembler_name;
293 assembler_name = DECL_ASSEMBLER_NAME (decl);
294 lang_hooks.l_ipo.clear_global_name_bindings (assembler_name);
299 /* The referenced attribute of a decl is not associated with the
300 decl itself but with the assembler name. Remember the referenced
301 bits before clearing them. */
303 static void
304 save_assembler_name_reference_bit (void)
306 varpool_get_referenced_asm_ids (&referenced_asm_ids);
309 /* Clear the reference bits for assembler names before closing the
310 module scope. */
312 static void
313 clear_assembler_name_reference_bit (void)
315 varpool_clear_asm_id_reference_bit ();
318 /* Restore the reference bits for assembler names. */
320 static void
321 restore_assembler_name_reference_bit (void)
323 size_t i;
324 tree nm;
325 for (i = 0;
326 VEC_iterate (tree, referenced_asm_ids, i, nm);
327 ++i)
328 TREE_SYMBOL_REFERENCED (nm) = 1;
331 /* Set up the module scope before the parsing of the
332 associated source file. */
334 void
335 push_module_scope (void)
337 struct saved_module_scope *prev_module_scope;
339 if (!flag_dyn_ipa || !L_IPO_COMP_MODE)
341 parser_parsing_start = true;
342 return;
345 prev_module_scope = current_module_scope;
346 if (L_IPO_IS_PRIMARY_MODULE)
348 gcc_assert (!prev_module_scope);
349 lang_hooks.l_ipo.save_built_in_decl_pre_parsing ();
350 parser_parsing_start = true;
353 gcc_assert (current_module_id);
355 /* Set up the module scope. */
356 current_module_scope = get_module_scope (current_module_id);
357 return;
360 /* Restore the shared decls to their post parsing states. */
362 static void
363 restore_post_parsing_states (void)
365 current_module_id = primary_module_id;
366 current_module_scope = get_module_scope (primary_module_id);
367 set_funcdef_no (max_funcdef_no);
368 input_location = primary_module_last_loc;
370 restore_assembler_name_reference_bit ();
371 lang_hooks.l_ipo.restore_built_in_decl_post_module_parsing ();
374 /* Pop the current module scope (by clearing name bindings etc.)
375 and prepare for parsing of the next module. In particular,
376 built-in decls need to be restored to the state before file
377 parsing starts. */
379 void
380 pop_module_scope (void)
382 bool is_last = false;
383 int last_funcdef_no;
385 if (!flag_dyn_ipa || !L_IPO_COMP_MODE)
386 return;
388 gcc_assert (current_module_id && current_module_scope);
390 if (L_IPO_IS_PRIMARY_MODULE)
391 primary_module_last_loc = input_location;
393 at_eof = 1;
394 lang_hooks.l_ipo.process_pending_decls (input_location);
395 lang_hooks.l_ipo.clear_deferred_fns ();
396 at_eof = 0;
398 is_last = is_last_module (current_module_id);
400 last_funcdef_no = get_last_funcdef_no ();
401 if (last_funcdef_no > max_funcdef_no)
402 max_funcdef_no = last_funcdef_no;
404 lang_hooks.l_ipo.save_built_in_decl_post_module_parsing ();
405 /* Save primary module state if needed (when module group
406 size > 1) */
407 if (L_IPO_IS_PRIMARY_MODULE && num_in_fnames > 1)
409 save_assembler_name_reference_bit ();
410 primary_module_last_funcdef_no = last_funcdef_no;
413 if (!is_last)
415 /* More aux modules are anticipated, clear
416 the parsing state. */
417 gcc_assert (num_in_fnames > 1);
418 clear_assembler_name_reference_bit ();
419 clear_module_scope_bindings (current_module_scope);
420 /* Restore symtab bindings for builtins */
421 lang_hooks.l_ipo.restore_built_in_decl_pre_parsing ();
422 /* The map can not be cleared because the names of operator
423 decls are used to store the information about the conversion
424 target type. This forces the coversion operator ids to be
425 incremented across different modules, and assember id must
426 be used for checksum computation. */
427 /* cp_clear_conv_type_map (); */
429 else if (num_in_fnames > 1)
431 clear_module_scope_bindings (current_module_scope);
432 restore_post_parsing_states ();
434 else
435 gcc_assert (L_IPO_IS_PRIMARY_MODULE && num_in_fnames == 1);
439 /* Type merging support for LIPO */
441 struct type_ec
443 tree rep_type;
444 VEC(tree, heap) *eq_types;
447 static VEC(tree, heap) *pending_types = NULL;
448 static struct pointer_set_t *type_set = NULL;
449 static htab_t type_hash_tab = NULL;
451 /* Hash function for the type table. */
453 static hashval_t
454 type_hash_hash (const void *ent)
456 tree type, name;
457 const struct type_ec *const entry
458 = (const struct type_ec *) ent;
460 type = entry->rep_type;
461 name = TYPE_NAME (type);
462 if (DECL_P (name))
463 name = DECL_NAME (name);
465 return htab_hash_string (IDENTIFIER_POINTER (name));
468 /* Equality function for type hash table. */
470 static int
471 type_hash_eq (const void *ent1, const void *ent2)
473 tree type1, type2;
474 const struct type_ec *const entry1
475 = (const struct type_ec *) ent1;
476 const struct type_ec *const entry2
477 = (const struct type_ec *) ent2;
479 type1 = entry1->rep_type;
480 type2 = entry2->rep_type;
482 return aggr_has_equiv_id (type1, type2);
485 /* Function to delete type hash entries. */
487 static void
488 type_hash_del (void *ent)
490 struct type_ec *const entry
491 = (struct type_ec *) ent;
493 VEC_free (tree, heap, entry->eq_types);
494 free (entry);
497 struct GTY(()) type_ent
499 tree type;
500 unsigned eq_id;
503 static GTY ((param_is (struct type_ent))) htab_t l_ipo_type_tab = 0;
504 static unsigned l_ipo_eq_id = 0;
506 /* Address hash function for struct type_ent. */
508 static hashval_t
509 type_addr_hash (const void *ent)
511 const struct type_ent *const entry
512 = (const struct type_ent *) ent;
513 return (hashval_t) (long) entry->type;
516 /* Address equality function for type_ent. */
518 static int
519 type_addr_eq (const void *ent1, const void *ent2)
521 const struct type_ent *const entry1
522 = (const struct type_ent *) ent1;
523 const struct type_ent *const entry2
524 = (const struct type_ent *) ent2;
525 return entry1->type == entry2->type;
528 /* Returns 1 if NS1 and NS2 refer to the same namespace. */
530 static int
531 is_ns_equiv (tree ns1, tree ns2)
533 tree n1, n2;
534 if (ns1 == NULL && ns2 == NULL)
535 return 1;
537 if ((!ns1 && ns2) || (ns1 && !ns2))
538 return 0;
540 gcc_assert (DECL_P (ns1) && DECL_P (ns2));
542 if (!is_ns_equiv (DECL_CONTEXT (ns1),
543 DECL_CONTEXT (ns2)))
544 return 0;
546 n1 = DECL_NAME (ns1);
547 n2 = DECL_NAME (ns2);
548 if (n1 == 0 && n2 == 0)
549 /* Conservative (which can happen when two NSes are from
550 different modules but with same UID) quivalence is allowed. */
551 return DECL_UID (ns1) == DECL_UID (ns2);
552 if (!n1 || !n2)
553 return 0;
555 if (!strcmp (IDENTIFIER_POINTER (n1),
556 IDENTIFIER_POINTER (n2)))
557 return 1;
559 return 0;
562 /* Returns 1 if aggregate type T1 and T2 have equivalent qualified
563 ids. */
565 static int
566 aggr_has_equiv_id (tree t1, tree t2)
568 int ctx_match;
569 tree ctx1, ctx2, tn1, tn2;
570 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
572 ctx1 = TYPE_CONTEXT (t1);
573 ctx2 = TYPE_CONTEXT (t2);
575 if ((ctx1 && !ctx2) || (!ctx1 && ctx2))
576 return 0;
578 if (ctx1 && TREE_CODE (ctx1) != TREE_CODE (ctx2))
579 return 0;
581 if (ctx1 && (TREE_CODE (ctx1) == FUNCTION_DECL
582 || TREE_CODE (ctx1) == BLOCK))
583 return 0;
585 if (!ctx1)
587 ctx_match = 1;
588 gcc_assert (!ctx2);
590 else if (TREE_CODE (ctx1) == NAMESPACE_DECL)
591 ctx_match = is_ns_equiv (ctx1, ctx2);
592 else if (TYPE_P (ctx1))
593 ctx_match = aggr_has_equiv_id (ctx1, ctx2);
594 else
596 gcc_assert (TREE_CODE (ctx1) == TRANSLATION_UNIT_DECL);
597 ctx_match = 1;
600 if (!ctx_match)
601 return 0;
603 /* Now compare the name of the types. */
604 tn1 = TYPE_NAME (t1);
605 tn2 = TYPE_NAME (t2);
606 if ((tn1 && !tn2) || !(tn1 && tn2))
607 return 0;
608 else if (!tn1 && !tn2)
609 /* Be conservative on unamed types. */
610 return 1;
612 if (DECL_P (tn1))
613 tn1 = DECL_NAME (tn1);
614 if (DECL_P (tn2))
615 tn2 = DECL_NAME (tn2);
616 if (strcmp (IDENTIFIER_POINTER (tn1),
617 IDENTIFIER_POINTER (tn2)))
618 return 0;
620 return lang_hooks.l_ipo.cmp_lang_type (t1, t2);
623 /* Return the canonical type of the type's main variant. */
624 static inline tree
625 get_norm_type (const_tree type)
627 tree cano_type = TYPE_MAIN_VARIANT (type);
628 if (TYPE_CANONICAL (cano_type))
629 cano_type = TYPE_CANONICAL (cano_type);
631 return cano_type;
634 /* Return 1 if type T1 and T2 are equivalent. Struct/union/class
635 types are compared using qualified name ids. Alias sets of
636 equivalent types will be merged. Client code may choose to do
637 structural equivalence check for sanity. Note the difference
638 between the types_compatible_p (and its langhooks subroutines)
639 and this interface. The former is mainly used to remove useless
640 type conversion and value numbering computation. It returns 1
641 only when it is sure and should not be used in contexts where
642 erroneously returning 0 causes problems. This interface
643 lipo_cmp_type behaves differently - it returns 1 when it is not
644 sure -- as the primary purpose of the interface is for alias
645 set computation. */
648 lipo_cmp_type (tree t1, tree t2)
650 if (TREE_CODE (t1) != TREE_CODE (t2))
651 return 0;
652 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
653 return 0;
654 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
655 return 0;
657 t1 = get_norm_type (t1);
658 t2 = get_norm_type (t2);
660 switch (TREE_CODE (t1))
662 case RECORD_TYPE:
663 case UNION_TYPE:
664 case QUAL_UNION_TYPE:
665 return aggr_has_equiv_id (t1, t2);
667 case POINTER_TYPE:
668 case REFERENCE_TYPE:
669 case COMPLEX_TYPE:
670 return lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2));
671 case ARRAY_TYPE:
672 return (lipo_cmp_type (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2))
673 && lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2)));
674 case METHOD_TYPE:
675 return lipo_cmp_type (TYPE_METHOD_BASETYPE (t1),
676 TYPE_METHOD_BASETYPE (t2));
677 case FUNCTION_TYPE:
679 tree arg1, arg2;
680 for (arg1 = TYPE_ARG_TYPES (t1), arg2 = TYPE_ARG_TYPES (t2);
681 arg1 && arg2;
682 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
683 if (!lipo_cmp_type (TREE_VALUE (arg1),
684 TREE_VALUE (arg2)))
685 return 0;
686 if (arg1 || arg2)
687 return 0;
688 return 1;
690 case OFFSET_TYPE:
691 return lipo_cmp_type (TYPE_OFFSET_BASETYPE (t1),
692 TYPE_OFFSET_BASETYPE (t2));
693 case ENUMERAL_TYPE:
694 return lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2));
695 case REAL_TYPE:
696 case FIXED_POINT_TYPE:
697 case INTEGER_TYPE:
698 return (TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
699 && TYPE_MODE (t1) == TYPE_MODE (t2)
700 && TYPE_MIN_VALUE (t1) == TYPE_MIN_VALUE (t2)
701 && TYPE_MAX_VALUE (t1) == TYPE_MAX_VALUE (t2));
702 case VECTOR_TYPE:
703 return (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
704 && lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2)));
705 case VOID_TYPE:
706 case BOOLEAN_TYPE:
707 return 1;
708 case TEMPLATE_TYPE_PARM:
709 return 1;
710 default:
711 gcc_unreachable ();
715 #ifndef ANON_AGGRNAME_PREFIX
716 #define ANON_AGGRNAME_PREFIX "__anon_"
717 #endif
718 #ifndef ANON_AGGRNAME_P
719 #define ANON_AGGRNAME_P(ID_NODE) \
720 (!strncmp (IDENTIFIER_POINTER (ID_NODE), ANON_AGGRNAME_PREFIX, \
721 sizeof (ANON_AGGRNAME_PREFIX) - 1))
722 #endif
724 /* Callback function used in tree walk to find referenced struct types. */
726 static tree
727 find_struct_types (tree *tp,
728 int *walk_subtrees ATTRIBUTE_UNUSED,
729 void *data ATTRIBUTE_UNUSED)
731 if (!(*tp))
732 return NULL_TREE;
734 if (TYPE_P (*tp))
736 if (lang_hooks.l_ipo.is_compiler_generated_type (*tp))
737 return NULL_TREE;
739 switch (TREE_CODE (*tp))
741 case RECORD_TYPE:
742 case UNION_TYPE:
743 case QUAL_UNION_TYPE:
745 tree cano_type, name;
746 tree context;
747 tree field;
749 cano_type = get_norm_type (*tp);
750 name = TYPE_NAME (cano_type);
751 if (!name)
753 /* the main variant of typedef of unnamed struct
754 has no name, use the orignal type for equivalence. */
755 cano_type = *tp;
756 name = TYPE_NAME (cano_type);
758 if (!name)
759 return NULL_TREE;
760 if (DECL_P (name)
761 && (DECL_IGNORED_P (name)
762 || ANON_AGGRNAME_P (DECL_NAME (name))))
763 return NULL_TREE;
765 if (!pointer_set_insert (type_set, cano_type))
766 VEC_safe_push (tree, heap, pending_types, cano_type);
767 else
768 return NULL_TREE; /* Or use walk tree without dups. */
770 context = TYPE_CONTEXT (cano_type);
771 if (context && TYPE_P (context))
772 walk_tree (&context, find_struct_types, NULL, NULL);
774 /* Instantiate a nested work as the tree walker does not
775 get to the fields. */
776 if (TYPE_BINFO (cano_type))
778 int i;
779 tree binfo, base_binfo;
781 for (binfo = TYPE_BINFO (cano_type), i = 0;
782 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
783 walk_tree (&BINFO_TYPE (base_binfo), find_struct_types,
784 NULL, NULL);
786 for (field = TYPE_FIELDS (cano_type);
787 field != 0;
788 field = TREE_CHAIN (field))
789 walk_tree (&TREE_TYPE (field), find_struct_types,
790 NULL, NULL);
791 return NULL_TREE;
793 default:
794 return NULL_TREE;
797 else if (DECL_P (*tp))
798 /* walk tree does not walk down decls, so do a nested walk here. */
799 walk_tree (&(TREE_TYPE (*tp)), find_struct_types, NULL, NULL);
801 return NULL_TREE;
804 /* Collect referenced struct types. */
806 static void
807 cgraph_collect_type_referenced (void)
809 basic_block bb;
810 gimple_stmt_iterator gi;
812 FOR_EACH_BB (bb)
814 for (gi = gsi_start_bb (bb); !gsi_end_p (gi); gsi_next (&gi))
816 unsigned i;
817 gimple stmt = gsi_stmt (gi);
818 for (i = 0; i < gimple_num_ops (stmt); i++)
819 walk_tree (gimple_op_ptr (stmt, i), find_struct_types, NULL, NULL);
824 /* Check type equivalence. Returns 1 if T1 and T2 are equivalent
825 for tbaa; return 0 if not. -1 is returned if it is unknown. */
828 equivalent_struct_types_for_tbaa (const_tree t1, const_tree t2)
830 struct type_ent key, *tent1, *tent2, **slot;
832 if (!l_ipo_type_tab)
833 return -1;
835 t1 = get_norm_type (t1);
836 t2 = get_norm_type (t2);
838 key.type = (tree) (long) t1;
839 slot = (struct type_ent **)
840 htab_find_slot (l_ipo_type_tab, &key, NO_INSERT);
841 if (!slot || !*slot)
842 return -1;
843 tent1 = *slot;
845 key.type = (tree) (long) t2;
846 slot = (struct type_ent **)
847 htab_find_slot (l_ipo_type_tab, &key, NO_INSERT);
848 if (!slot || !*slot)
849 return -1;
850 tent2 = *slot;
852 return tent1->eq_id == tent2->eq_id;
855 /* Build type hash table. */
857 static void
858 cgraph_build_type_equivalent_classes (void)
860 unsigned n, i;
861 n = VEC_length (tree, pending_types);
862 for (i = 0; i < n; i++)
864 struct type_ec **slot;
865 struct type_ec te;
866 te.rep_type = VEC_index (tree, pending_types, i);
867 te.eq_types = NULL;
868 slot = (struct type_ec **) htab_find_slot (type_hash_tab,
869 &te, INSERT);
870 if (!*slot)
872 *slot = XCNEW (struct type_ec);
873 (*slot)->rep_type = te.rep_type;
875 VEC_safe_push (tree, heap, (*slot)->eq_types, te.rep_type);
879 /* Re-propagate component types's alias set to that of TYPE. PROCESSED
880 is the pointer set of processed types. */
882 static void
883 re_record_component_aliases (tree type,
884 struct pointer_set_t *processed)
886 alias_set_type superset = get_alias_set (type);
887 tree field;
889 if (superset == 0)
890 return;
892 if (pointer_set_insert (processed, type))
893 return;
895 switch (TREE_CODE (type))
897 case RECORD_TYPE:
898 case UNION_TYPE:
899 case QUAL_UNION_TYPE:
900 /* Recursively record aliases for the base classes, if there are any. */
901 if (TYPE_BINFO (type))
903 int i;
904 tree binfo, base_binfo;
906 for (binfo = TYPE_BINFO (type), i = 0;
907 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
909 re_record_component_aliases (BINFO_TYPE (base_binfo),
910 processed);
911 record_alias_subset (superset,
912 get_alias_set (BINFO_TYPE (base_binfo)));
915 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
916 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
918 re_record_component_aliases (TREE_TYPE (field), processed);
919 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
921 break;
923 case COMPLEX_TYPE:
924 re_record_component_aliases (TREE_TYPE (type), processed);
925 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
926 break;
928 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
929 element type. */
931 default:
932 break;
936 /* The callback function to merge alias sets of equivalent types. */
938 static int
939 type_eq_process (void **slot, void *data ATTRIBUTE_UNUSED)
941 unsigned i;
942 alias_set_type alias_set, ptr_alias_set = -1;
943 tree rep_type, type;
944 VEC(tree, heap) *eq_types;
945 struct type_ec ** te = (struct type_ec **)slot;
946 bool zero_set = false, ptr_zero_set = false;
947 struct type_ent **slot2, key, *tent;
950 rep_type = (*te)->rep_type;
951 eq_types = (*te)->eq_types;
952 alias_set = get_alias_set (rep_type);
954 for (i = 0;
955 VEC_iterate (tree, eq_types, i, type);
956 ++i)
958 alias_set_type als, ptr_als = -1;
959 tree type_ptr = TYPE_POINTER_TO (type);;
961 als = get_alias_set (type);
962 if (als == 0)
963 zero_set = true;
965 if (alias_set && als && alias_set != als)
966 record_alias_subset (alias_set, als);
968 if (type_ptr)
970 ptr_als = get_alias_set (type_ptr);
971 if (ptr_als == 0)
972 ptr_zero_set = true;
974 if (ptr_alias_set == -1)
975 ptr_alias_set = ptr_als;
976 else
978 if (!ptr_zero_set && ptr_alias_set != ptr_als)
979 record_alias_subset (ptr_alias_set, ptr_als);
984 /* Now propagate back. */
985 for (i = 0;
986 VEC_iterate (tree, eq_types, i, type);
987 ++i)
989 alias_set_type als, ptr_als;
990 tree ptr_type = TYPE_POINTER_TO (type);
992 als = get_alias_set (type);
994 if (zero_set)
995 TYPE_ALIAS_SET (type) = 0;
996 else if (alias_set != als)
997 record_alias_subset (als, alias_set);
999 if (ptr_type)
1001 ptr_als = get_alias_set (ptr_type);
1002 if (ptr_zero_set)
1003 TYPE_ALIAS_SET (ptr_type) = 0;
1004 else if (ptr_alias_set != ptr_als)
1005 record_alias_subset (ptr_als, ptr_alias_set);
1010 /* Now populate the type table. */
1011 l_ipo_eq_id++;
1012 for (i = 0;
1013 VEC_iterate (tree, eq_types, i, type);
1014 ++i)
1016 key.type = type;
1017 slot2 = (struct type_ent **)
1018 htab_find_slot (l_ipo_type_tab, &key, INSERT);
1019 tent = *slot2;
1020 gcc_assert (!tent);
1021 tent = ggc_alloc_cleared_type_ent ();
1022 tent->type = key.type;
1023 tent->eq_id = l_ipo_eq_id;
1024 *slot2 = tent;
1027 return 1;
1030 /* Regenerate alias set for aggregate types. */
1032 static void
1033 record_components_for_parent_types (void)
1035 unsigned n, i;
1036 struct pointer_set_t *processed_types;
1038 processed_types = pointer_set_create ();
1039 n = VEC_length (tree, pending_types);
1040 for (i = 0; i < n; i++)
1042 tree type = VEC_index (tree, pending_types, i);
1043 re_record_component_aliases (type, processed_types);
1046 pointer_set_destroy (processed_types);
1049 /* Unify type alias sets for equivalent types. */
1051 void
1052 cgraph_unify_type_alias_sets (void)
1054 struct cgraph_node *node;
1055 struct varpool_node *pv;
1057 if (!L_IPO_COMP_MODE)
1058 return;
1059 type_set = pointer_set_create ();
1060 type_hash_tab = htab_create (10, type_hash_hash,
1061 type_hash_eq, type_hash_del);
1062 l_ipo_type_tab = htab_create_ggc (10, type_addr_hash,
1063 type_addr_eq, NULL);
1065 for (node = cgraph_nodes; node; node = node->next)
1067 if (node->analyzed && (node->needed || node->reachable))
1069 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1070 current_function_decl = node->decl;
1071 cgraph_collect_type_referenced ();
1072 current_function_decl = NULL;
1073 pop_cfun ();
1077 for (pv = varpool_nodes; pv; pv = pv->next)
1078 walk_tree (&pv->decl, find_struct_types, NULL, NULL);
1080 /* Compute type equivalent classes. */
1081 cgraph_build_type_equivalent_classes ();
1082 /* Now unify alias sets of equivelent types. */
1083 htab_traverse (type_hash_tab, type_eq_process, NULL);
1084 /* Finally re-populating parent's alias set. */
1085 record_components_for_parent_types ();
1087 pointer_set_destroy (type_set);
1088 VEC_free (tree, heap, pending_types);
1089 htab_delete (type_hash_tab);
1092 /* Return true if NODE->decl from an auxiliary module has external
1093 definition (and therefore is not needed for expansion). */
1095 bool
1096 cgraph_is_aux_decl_external (struct cgraph_node *node)
1098 tree decl = node->decl;
1100 if (!(L_IPO_COMP_MODE || L_IPO_STREAM_COMP_MODE))
1101 return false;
1103 if (!cgraph_is_auxiliary (decl))
1104 return false;
1106 /* Versioned clones from auxiliary moduels are not
1107 external. */
1108 if (node->is_versioned_clone)
1109 return false;
1111 /* Reverse the process order for virtual and comdat functions
1112 in streaming lipo. */
1113 if (!flag_ripa_stream)
1115 /* virtual functions won't be deleted in the primary module. */
1116 if (DECL_VIRTUAL_P (decl))
1117 return true;
1120 /* Comdat or weak functions in aux modules are not external --
1121 there is no guarantee that the definitition will be emitted
1122 in the primary compilation of this auxiliary module. */
1123 if (DECL_COMDAT (decl) || DECL_WEAK (decl))
1124 return false;
1126 if (flag_ripa_stream)
1128 if (DECL_VIRTUAL_P (decl))
1129 return true;
1132 if (!TREE_PUBLIC (decl))
1133 return false;
1135 /* The others from aux modules are external. */
1136 return true;
1139 /* Linked function symbol (cgraph node) table. */
1140 static GTY((param_is (struct cgraph_sym))) htab_t cgraph_symtab;
1142 /* This is true when global linking is needed and performed (for C++).
1143 For C, symbol linking is performed on the fly during parsing, and
1144 the cgraph_symtab is used only for keeping additional information
1145 for any already merged symbol if needed. */
1147 static bool global_link_performed = 0;
1149 /* For an external (non-defined) function DECL, return the primary
1150 module id (even though when the declaration is declared in an aux
1151 module). For a defined function DECL, return the module id in which
1152 it is defined. */
1154 unsigned
1155 cgraph_get_module_id (tree decl)
1157 struct function *func = DECL_STRUCT_FUNCTION (decl);
1158 /* Not defined. */
1159 if (!func)
1160 return primary_module_id;
1161 return FUNC_DECL_MODULE_ID (func);
1164 /* Return true if function decl is defined in an auxiliary module. */
1166 bool
1167 cgraph_is_auxiliary (tree decl)
1169 return (cgraph_get_module_id (decl) != primary_module_id);
1172 /* Return the hash value for cgraph_sym pointed to by P. The
1173 hash value is computed using function's assembler name. */
1175 static hashval_t
1176 hash_sym_by_assembler_name (const void *p)
1178 const struct cgraph_sym *n = (const struct cgraph_sym *) p;
1179 return (hashval_t) decl_assembler_name_hash (n->assembler_name);
1182 /* Return nonzero if P1 and P2 are equal. */
1184 static int
1185 eq_assembler_name (const void *p1, const void *p2)
1187 const struct cgraph_sym *n1 = (const struct cgraph_sym *) p1;
1188 const_tree name = (const_tree) p2;
1189 return (decl_assembler_name_equal (n1->rep_decl, name));
1192 /* Return the cgraph_sym for function declaration DECL. */
1194 static struct cgraph_sym **
1195 cgraph_sym (tree decl)
1197 struct cgraph_sym **slot;
1198 tree name;
1200 if (!cgraph_symtab)
1202 gcc_assert (!global_link_performed);
1203 return NULL;
1206 name = DECL_ASSEMBLER_NAME (decl);
1207 slot = (struct cgraph_sym **)
1208 htab_find_slot_with_hash (cgraph_symtab, name,
1209 decl_assembler_name_hash (name),
1210 NO_INSERT);
1211 return slot;
1214 /* Return the representative declaration for assembler name
1215 ASM_NAME. */
1217 tree
1218 cgraph_find_decl (tree asm_name)
1220 struct cgraph_sym **slot;
1221 if (!L_IPO_COMP_MODE)
1222 return NULL;
1223 if (!cgraph_symtab || !global_link_performed)
1224 return NULL;
1226 slot = (struct cgraph_sym **)
1227 htab_find_slot_with_hash (cgraph_symtab, asm_name,
1228 decl_assembler_name_hash (asm_name),
1229 NO_INSERT);
1230 if (!slot || !*slot)
1231 return NULL;
1233 return (*slot)->rep_node->decl;
1236 /* Return true if function declaration DECL is originally file scope
1237 static, which is promoted to global scope. */
1239 bool
1240 cgraph_is_promoted_static_func (tree decl)
1242 struct cgraph_sym ** sym;
1243 gcc_assert (L_IPO_COMP_MODE);
1245 /* cgraph_symtab will be created when any symbol got
1246 promoted. */
1247 if (!cgraph_symtab)
1248 return false;
1250 sym = cgraph_sym (decl);
1251 if (!sym)
1252 return false;
1253 return (*sym)->is_promoted_static;
1256 /* Hash function for module information table. ENT
1257 is a pointer to a cgraph_module_info. */
1259 static hashval_t
1260 htab_sym_hash (const void *ent)
1262 const struct cgraph_mod_info * const mi
1263 = (const struct cgraph_mod_info * const ) ent;
1264 return (hashval_t) mi->module_id;
1267 /* Hash equality function for module information table. */
1269 static int
1270 htab_sym_eq (const void *ent1, const void *ent2)
1272 const struct cgraph_mod_info * const mi1
1273 = (const struct cgraph_mod_info * const ) ent1;
1274 const struct cgraph_mod_info * const mi2
1275 = (const struct cgraph_mod_info * const ) ent2;
1276 return (mi1->module_id == mi2->module_id);
1279 /* cgraph_sym SYM may be defined in more than one source modules.
1280 Add declaration DECL's definiting module to SYM. */
1282 static void
1283 add_define_module (struct cgraph_sym *sym, tree decl)
1285 unsigned module_id;
1286 struct cgraph_mod_info **slot;
1287 struct cgraph_mod_info mi;
1289 struct function *f = DECL_STRUCT_FUNCTION (decl);
1290 if (!f)
1291 return;
1292 module_id = FUNC_DECL_MODULE_ID (f);
1294 if (!sym->def_module_hash)
1295 sym->def_module_hash
1296 = htab_create_ggc (10, htab_sym_hash, htab_sym_eq, NULL);
1298 mi.module_id = module_id;
1299 slot = (struct cgraph_mod_info **)htab_find_slot (sym->def_module_hash,
1300 &mi, INSERT);
1301 if (!*slot)
1303 *slot = ggc_alloc_cleared_cgraph_mod_info ();
1304 (*slot)->module_id = module_id;
1306 else
1307 gcc_assert ((*slot)->module_id == module_id);
1310 static int
1311 add_def_module (void **slot, void *data)
1313 struct cgraph_mod_info **m = (struct cgraph_mod_info **)slot;
1314 htab_t mod_set = (htab_t) data;
1315 struct cgraph_mod_info **new_slot;
1317 new_slot = (struct cgraph_mod_info **)htab_find_slot (mod_set, *m, INSERT);
1318 if (!*new_slot)
1320 *new_slot = ggc_alloc_cleared_cgraph_mod_info ();
1321 (*new_slot)->module_id = (*m)->module_id;
1323 else
1324 gcc_assert ((*new_slot)->module_id == (*m)->module_id);
1325 return 1;
1328 /* Clone defined module hash table from ORIG to CLONE. */
1330 void
1331 copy_defined_module_set (tree clone, tree orig)
1333 struct cgraph_sym **orig_sym, **clone_sym;
1335 orig_sym = cgraph_sym (orig);
1336 clone_sym = cgraph_sym (clone);
1337 if (!orig_sym || !(*orig_sym)->def_module_hash)
1338 return;
1339 if (!(*clone_sym)->def_module_hash)
1340 (*clone_sym)->def_module_hash
1341 = htab_create_ggc (10, htab_sym_hash, htab_sym_eq, NULL);
1342 htab_traverse ((*orig_sym)->def_module_hash, add_def_module, (*clone_sym)->def_module_hash);
1345 /* Return true if the symbol associated with DECL is defined in module
1346 MODULE_ID. This interface is used by the inliner to make sure profile-gen
1347 and profile-use pass (L-IPO mode) make consistent inline decision. */
1349 bool
1350 cgraph_is_inline_body_available_in_module (tree decl, unsigned module_id)
1352 struct cgraph_sym **sym;
1353 void **slot;
1354 struct cgraph_mod_info mi;
1356 gcc_assert (L_IPO_COMP_MODE);
1358 if (DECL_BUILT_IN (decl))
1359 return true;
1361 /* TODO: revisit this. */
1362 if (DECL_IN_SYSTEM_HEADER (decl) && DECL_DECLARED_INLINE_P (decl))
1363 return true;
1365 gcc_assert (TREE_STATIC (decl) || DECL_DECLARED_INLINE_P (decl));
1367 if (cgraph_get_module_id (decl) == module_id)
1368 return true;
1370 sym = cgraph_sym (decl);
1371 if (!sym || !(*sym)->def_module_hash)
1372 return false;
1374 mi.module_id = module_id;
1375 slot = htab_find_slot ((*sym)->def_module_hash, &mi, NO_INSERT);
1376 if (slot)
1378 gcc_assert (((struct cgraph_mod_info*)*slot)->module_id == module_id);
1379 return true;
1381 return false;
1384 /* Return the linked cgraph node using DECL's assembler name. DO_ASSERT
1385 is a flag indicating that a non null link target must be returned. */
1387 struct cgraph_node *
1388 cgraph_lipo_get_resolved_node_1 (tree decl, bool do_assert)
1390 struct cgraph_sym **slot;
1392 /* Handle alias decl. */
1393 slot = cgraph_sym (cgraph_node (decl)->decl);
1395 if (!slot || !*slot)
1397 if (!do_assert)
1398 return NULL;
1399 else
1401 /* Nodes that are indirectly called are not 'reachable' in
1402 the callgraph. If they are not needed (comdat, inline
1403 extern etc), they may be removed from the link table
1404 before direct calls to them are exposed (via indirect
1405 call promtion by const folding etc). When this happens,
1406 the node will be to be relinked. A probably better fix
1407 is to modify the callgraph so that they are not eliminated
1408 in the first place -- this will allow inlining to happen. */
1410 struct cgraph_node *n = cgraph_node (decl);
1411 if (!n->analyzed)
1413 gcc_assert (DECL_EXTERNAL (decl)
1414 || cgraph_is_aux_decl_external (n)
1415 || DECL_VIRTUAL_P (decl));
1416 gcc_assert ((!n->reachable && !n->needed)
1417 /* This is the case for explicit extern instantiation,
1418 when cgraph node is not created before link. */
1419 || DECL_EXTERNAL (decl));
1420 cgraph_link_node (n);
1421 return n;
1423 else
1424 gcc_unreachable ();
1427 else
1429 struct cgraph_sym *sym = *slot;
1430 return sym->rep_node;
1434 /* Return the cgraph_node of DECL if decl has definition; otherwise return
1435 the cgraph node of the representative decl, which is the declaration DECL
1436 is resolved to after linking/symbol resolution. */
1438 struct cgraph_node *
1439 cgraph_lipo_get_resolved_node (tree decl)
1441 struct cgraph_node *node = NULL;
1443 gcc_assert (L_IPO_COMP_MODE && global_link_performed);
1444 gcc_assert (cgraph_symtab);
1446 /* Never merged. */
1447 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl)
1448 /* builtin function decls are shared across modules, but 'linking'
1449 is still performed for them to keep track of the set of defining
1450 modules. Skip the real resolution here to avoid merging '__builtin_xxx'
1451 with 'xxx'. */
1452 || DECL_BUILT_IN (decl))
1453 return cgraph_node (decl);
1455 node = cgraph_lipo_get_resolved_node_1 (decl, true);
1456 return node;
1459 /* When NODE->decl is dead function eliminated,
1460 remove the entry in the link table. */
1462 void
1463 cgraph_remove_link_node (struct cgraph_node *node)
1465 tree name, decl;
1467 if (!L_IPO_COMP_MODE || !cgraph_symtab)
1468 return;
1470 decl = node->decl;
1472 /* Skip nodes that are not in the link table. */
1473 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
1474 return;
1476 /* Skip if node is an inline clone or if the node has
1477 defintion that is not really resolved to the merged node. */
1478 if (cgraph_lipo_get_resolved_node_1 (decl, false) != node)
1479 return;
1481 name = DECL_ASSEMBLER_NAME (decl);
1482 htab_remove_elt_with_hash (cgraph_symtab, name,
1483 decl_assembler_name_hash (name));
1486 /* Return true if the function body for DECL has profile information. */
1488 static bool
1489 has_profile_info (tree decl)
1491 gcov_type *ctrs = NULL;
1492 unsigned n;
1493 struct function* f = DECL_STRUCT_FUNCTION (decl);
1495 ctrs = get_coverage_counts_no_warn (f, GCOV_COUNTER_ARCS, &n);
1496 if (ctrs)
1498 unsigned i;
1499 for (i = 0; i < n; i++)
1500 if (ctrs[i])
1501 return true;
1504 return false;
1507 /* Resolve delaration NODE->decl for function symbol *SLOT. */
1509 static void
1510 resolve_cgraph_node (struct cgraph_sym **slot, struct cgraph_node *node)
1512 tree decl1, decl2;
1513 int decl1_defined = 0;
1514 int decl2_defined = 0;
1516 decl1 = (*slot)->rep_decl;
1517 decl2 = node->decl;
1519 decl1_defined = gimple_has_body_p (decl1);
1520 decl2_defined = gimple_has_body_p (decl2);
1522 if (decl1_defined && !decl2_defined)
1523 return;
1525 if (!decl1_defined && decl2_defined)
1527 (*slot)->rep_node = node;
1528 (*slot)->rep_decl = decl2;
1529 add_define_module (*slot, decl2);
1530 return;
1533 if (decl2_defined)
1535 bool has_prof1 = false;
1536 bool has_prof2 = false;
1537 gcc_assert (decl1_defined);
1538 add_define_module (*slot, decl2);
1540 has_prof1 = has_profile_info (decl1);
1541 if (has_prof1)
1542 return;
1543 has_prof2 = has_profile_info (decl2);
1544 if (has_prof2)
1546 (*slot)->rep_node = node;
1547 (*slot)->rep_decl = decl2;
1549 return;
1551 return;
1555 /* Resolve NODE->decl in the function symbol table. */
1557 struct cgraph_sym *
1558 cgraph_link_node (struct cgraph_node *node)
1560 void **slot;
1561 tree name;
1563 if (!L_IPO_COMP_MODE)
1564 return NULL;
1566 if (!cgraph_symtab)
1567 return NULL;
1569 /* Skip the cases when the defintion can be locally resolved, and
1570 when we do not need to keep track of defining modules. */
1571 if (!TREE_PUBLIC (node->decl) || DECL_ARTIFICIAL (node->decl))
1572 return NULL;
1574 name = DECL_ASSEMBLER_NAME (node->decl);
1575 slot = htab_find_slot_with_hash (cgraph_symtab, name,
1576 decl_assembler_name_hash (name),
1577 INSERT);
1578 if (*slot)
1579 resolve_cgraph_node ((struct cgraph_sym **) slot, node);
1580 else
1582 struct cgraph_sym *sym = ggc_alloc_cleared_cgraph_sym ();
1583 sym->rep_node = node;
1584 sym->rep_decl = node->decl;
1585 sym->assembler_name = name;
1586 add_define_module (sym, node->decl);
1587 *slot = sym;
1589 return (struct cgraph_sym *) *slot;
1592 /* Perform cross module linking of function declarations. */
1594 void
1595 cgraph_do_link (void)
1597 struct cgraph_node *node;
1599 if (!L_IPO_COMP_MODE)
1600 return;
1602 global_link_performed = 1;
1603 gcc_assert (cgraph_pre_profiling_inlining_done);
1605 if (!cgraph_symtab)
1606 cgraph_symtab
1607 = htab_create_ggc (10, hash_sym_by_assembler_name,
1608 eq_assembler_name, NULL);
1610 for (node = cgraph_nodes; node; node = node->next)
1612 gcc_assert (!node->global.inlined_to);
1613 cgraph_link_node (node);
1617 struct promo_ent
1619 char* assemb_name;
1620 int seq;
1623 /* Hash function for promo_ent table. */
1625 static hashval_t
1626 promo_ent_hash (const void *ent)
1628 const struct promo_ent *const entry
1629 = (const struct promo_ent *) ent;
1631 return htab_hash_string (entry->assemb_name);
1634 /* Hash_eq function for promo_ent table. */
1636 static int
1637 promo_ent_eq (const void *ent1, const void *ent2)
1639 const struct promo_ent *const entry1
1640 = (const struct promo_ent *) ent1;
1641 const struct promo_ent *const entry2
1642 = (const struct promo_ent *) ent2;
1643 if (!strcmp (entry1->assemb_name, entry2->assemb_name))
1644 return 1;
1645 return 0;
1648 /* Delete function for promo_ent hash table. */
1650 static void
1651 promo_ent_del (void *ent)
1653 struct promo_ent *const entry
1654 = (struct promo_ent *) ent;
1656 free (entry->assemb_name);
1657 free (entry);
1660 static htab_t promo_ent_hash_tab = NULL;
1662 /* Return a unique sequence number for NAME. This is needed to avoid
1663 name conflict -- function scope statics may have identical names.
1665 This function returns a zero sequence number if it is called with
1666 a particular NAME for the first time, and non-zero otherwise.
1667 This fact is used to keep track of unseen weak variables. */
1669 static int
1670 get_name_seq_num (const char *name)
1672 struct promo_ent **slot;
1673 struct promo_ent ent;
1674 ent.assemb_name = xstrdup (name);
1675 ent.seq = 0;
1677 slot = (struct promo_ent **)
1678 htab_find_slot (promo_ent_hash_tab, &ent, INSERT);
1680 if (!*slot)
1682 *slot = XCNEW (struct promo_ent);
1683 (*slot)->assemb_name = ent.assemb_name;
1685 else
1687 (*slot)->seq++;
1688 free (ent.assemb_name);
1690 return (*slot)->seq;
1693 /* Returns a unique assembler name for DECL. */
1695 static tree
1696 create_unique_name (tree decl, unsigned module_id)
1698 tree id, assemb_id;
1699 char *assembler_name;
1700 const char *name;
1701 struct function *context = NULL;
1702 int seq = 0;
1704 if (TREE_CODE (decl) == FUNCTION_DECL)
1706 if (!DECL_CONTEXT (decl)
1707 || TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
1709 id = DECL_NAME (decl);
1710 /* if (IDENTIFIER_OPNAME_P (id)) */
1711 if (TREE_LANG_FLAG_2 (id))
1712 id = DECL_ASSEMBLER_NAME (decl);
1714 else
1715 id = DECL_ASSEMBLER_NAME (decl);
1717 else
1719 if (!DECL_CONTEXT (decl))
1720 id = DECL_NAME (decl);
1721 else if (TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
1722 id = DECL_ASSEMBLER_NAME (decl);
1723 else if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
1725 id = DECL_NAME (decl);
1726 context = DECL_STRUCT_FUNCTION (DECL_CONTEXT (decl));
1728 else
1729 /* file scope context */
1730 id = DECL_NAME (decl);
1733 name = IDENTIFIER_POINTER (id);
1734 if (context)
1736 char *n;
1737 unsigned fno = FUNC_DECL_FUNC_ID (context);
1738 n = (char *)alloca (strlen (name) + 15);
1739 sprintf (n, "%s_%u", name, fno);
1740 name = n;
1743 assembler_name = (char*) alloca (strlen (name) + 30);
1744 sprintf (assembler_name, "%s_cmo_%u", name, module_id);
1745 seq = get_name_seq_num (assembler_name);
1746 if (seq)
1747 sprintf (assembler_name, "%s_%d", assembler_name, seq);
1749 assemb_id = get_identifier (assembler_name);
1751 return assemb_id;
1754 /* Promote DECL to be global. MODULE_ID is the id of the module where
1755 DECL is defined. IS_EXTERN is a flag indicating if externalization
1756 is needed. */
1758 static void
1759 promote_static_var_func (unsigned module_id, tree decl, bool is_extern)
1761 tree assemb_id;
1762 tree alias;
1764 /* No need to promote symbol alias. */
1765 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1766 if (alias)
1767 return;
1769 /* Function decls in C++ may contain characters not taken by assembler.
1770 Similarly, function scope static variable has UID as the assembler name
1771 suffix which is not consistent across modules. */
1773 if (DECL_ASSEMBLER_NAME_SET_P (decl)
1774 && TREE_CODE (decl) == FUNCTION_DECL)
1775 cgraph_remove_assembler_hash_node (cgraph_node (decl));
1777 assemb_id = create_unique_name (decl, module_id);
1778 SET_DECL_ASSEMBLER_NAME (decl, assemb_id);
1779 TREE_PUBLIC (decl) = 1;
1780 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
1781 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1783 if (TREE_CODE (decl) == FUNCTION_DECL)
1785 struct cgraph_node *node = cgraph_node (decl);
1787 node->resolution = LDPR_UNKNOWN;
1788 cgraph_add_assembler_hash_node (node);
1789 if (flag_opt_info >= OPT_INFO_MAX)
1790 inform (UNKNOWN_LOCATION, "Promote function %s to global (%d): %s",
1791 get_name (decl),is_extern,
1792 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME(decl)));
1794 else
1796 struct varpool_node *node = varpool_node (decl);
1797 node->resolution = LDPR_UNKNOWN;
1798 varpool_link_node (node);
1799 if (flag_opt_info >= OPT_INFO_MAX)
1800 inform (UNKNOWN_LOCATION, "Promote variable %s to global (%d): %s",
1801 get_name (decl),is_extern,
1802 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME(decl)));
1805 if (is_extern)
1807 if (TREE_CODE (decl) == VAR_DECL)
1809 TREE_STATIC (decl) = 0;
1810 DECL_EXTERNAL (decl) = 1;
1811 /* Keep the initializer to allow const prop. */
1812 /* DECL_INITIAL (decl) = 0; */
1813 DECL_CONTEXT (decl) = 0;
1815 /* else
1816 Function body will be deleted later before expansion. */
1818 else
1819 TREE_STATIC (decl) = 1;
1822 /* Externalize global variables from aux modules and promote
1823 static variables.
1824 WEAK variables are treated especially in
1825 varpool_remove_duplicate_weak_decls. */
1827 static void
1828 process_module_scope_static_var (struct varpool_node *vnode)
1830 tree decl = vnode->decl;
1832 if (varpool_is_auxiliary (vnode))
1834 gcc_assert (vnode->module_id != primary_module_id);
1835 if (TREE_PUBLIC (decl))
1837 /* Externalize non-weak variables. */
1838 if (!DECL_WEAK (decl))
1840 DECL_EXTERNAL (decl) = 1;
1841 TREE_STATIC (decl) = 0;
1842 /* Keep the initializer to allow const prop. */
1843 /* DECL_INITIAL (decl) = NULL; */
1844 if (DECL_CONTEXT (decl))
1846 DECL_ASSEMBLER_NAME (decl);
1848 DECL_CONTEXT (decl) = NULL;
1851 else
1853 /* Promote static vars to global. */
1854 if (vnode->module_id)
1855 promote_static_var_func (vnode->module_id, decl,
1856 varpool_is_auxiliary (vnode));
1859 else
1861 if (primary_module_id && primary_module_exported && !TREE_PUBLIC (decl))
1862 promote_static_var_func (vnode->module_id, decl,
1863 varpool_is_auxiliary (vnode));
1867 /* Promote static function CNODE->decl to be global. */
1869 static void
1870 process_module_scope_static_func (struct cgraph_node *cnode)
1872 tree decl = cnode->decl;
1873 bool addr_taken;
1875 if (TREE_PUBLIC (decl)
1876 || !TREE_STATIC (decl)
1877 || DECL_EXTERNAL (decl)
1878 || DECL_ARTIFICIAL (decl))
1879 return;
1881 if (flag_ripa_no_promote_always_inline
1882 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)) != NULL)
1883 return;
1885 /* Can be local -- the promotion pass need to be done after
1886 callgraph build when address taken bit is set. */
1887 addr_taken = cnode->address_taken;
1888 if (!addr_taken && cnode->same_body)
1890 struct cgraph_node *alias = cnode->same_body;
1891 while (alias)
1893 if (alias->address_taken)
1894 addr_taken = true;
1895 alias = alias->next;
1898 if (!addr_taken)
1900 tree assemb_id = create_unique_name (decl, cgraph_get_module_id (decl));
1902 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1903 cgraph_remove_assembler_hash_node (cnode);
1904 SET_DECL_ASSEMBLER_NAME (decl, assemb_id);
1905 return;
1908 if (cgraph_is_auxiliary (decl))
1910 unsigned mod_id;
1912 gcc_assert (cgraph_get_module_id (decl) != primary_module_id);
1913 mod_id = cgraph_get_module_id (decl);
1914 /* Promote static function to global. */
1915 if (mod_id)
1917 promote_static_var_func (mod_id, decl, 1);
1919 /* Process aliases */
1920 if (cnode->same_body)
1922 struct cgraph_node *alias = cnode->same_body;
1923 while (alias)
1925 if (!alias->thunk.thunk_p)
1927 tree alias_decl = alias->decl;
1928 /* Should assert */
1929 if (cgraph_get_module_id (alias_decl) == mod_id)
1930 promote_static_var_func (mod_id, alias_decl, 1);
1932 alias = alias->next;
1937 else
1939 if (primary_module_exported
1940 /* skip static_init routines. */
1941 && !DECL_ARTIFICIAL (decl))
1943 promote_static_var_func (cgraph_get_module_id (decl), decl, 0);
1944 cgraph_mark_if_needed (decl);
1946 /* Process aliases */
1947 if (cnode->same_body)
1949 struct cgraph_node *alias = cnode->same_body;
1950 while (alias)
1952 if (!alias->thunk.thunk_p)
1954 tree alias_decl = alias->decl;
1955 /* Should assert */
1956 if (cgraph_get_module_id (alias_decl) == cgraph_get_module_id (decl))
1957 promote_static_var_func (cgraph_get_module_id (decl), alias_decl, 0);
1959 alias = alias->next;
1966 /* Process var_decls, func_decls with static storage. */
1968 void
1969 cgraph_process_module_scope_statics (void)
1971 struct cgraph_node *pf;
1972 struct varpool_node *pv;
1974 if (!(L_IPO_COMP_MODE || L_IPO_STREAM_COMP_MODE))
1975 return;
1977 promo_ent_hash_tab = htab_create (10, promo_ent_hash,
1978 promo_ent_eq, promo_ent_del);
1980 /* Process variable first. */
1981 for (pv = varpool_nodes_queue; pv; pv = pv->next_needed)
1982 process_module_scope_static_var (pv);
1984 for (pf = cgraph_nodes; pf; pf = pf->next)
1985 process_module_scope_static_func (pf);
1987 htab_delete (promo_ent_hash_tab);
1990 /* There could be duplicate non-extern WEAK decls in the varpool queue,
1991 coming from different modules. All but one of these need to be externalized
1992 and removed from the varpool queue.
1993 Duplicate WEAK decls can be added to varpool queue as late as
1994 cgraph_expand_function, when a WEAK decl is marked referenced as assembler
1995 is being output. Therefore, a call to this function should be made after
1996 cgraph_expand_function. */
1998 void
1999 varpool_remove_duplicate_weak_decls (void)
2001 struct varpool_node *next, *node = varpool_nodes_queue;
2003 if (!L_IPO_COMP_MODE)
2004 return;
2006 varpool_reset_queue ();
2008 promo_ent_hash_tab = htab_create (10, promo_ent_hash,
2009 promo_ent_eq, promo_ent_del);
2011 while (node)
2013 tree decl = node->decl;
2014 next = node->next_needed;
2015 node->needed = 0;
2016 if (TREE_PUBLIC (decl) && DECL_WEAK (decl) && !DECL_EXTERNAL (decl)
2017 && get_name_seq_num (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))))
2019 DECL_EXTERNAL (decl) = 1;
2020 TREE_STATIC (decl) = 0;
2021 DECL_INITIAL (decl) = NULL;
2022 DECL_CONTEXT (decl) = NULL;
2024 else
2025 varpool_mark_needed_node (node);
2026 node = next;
2029 htab_delete (promo_ent_hash_tab);
2032 static GTY((param_is (struct varpool_node))) htab_t varpool_symtab;
2034 /* Hash function for varpool node. */
2036 static hashval_t
2037 hash_node_by_assembler_name (const void *p)
2039 const struct varpool_node *n = (const struct varpool_node *) p;
2040 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
2043 /* Returns nonzero if P1 and P2 are equal. */
2045 static int
2046 eq_node_assembler_name (const void *p1, const void *p2)
2048 const struct varpool_node *n1 = (const struct varpool_node *) p1;
2049 const_tree name = (const_tree)p2;
2050 return (decl_assembler_name_equal (n1->decl, name));
2053 /* Return true if NODE's decl is declared in an auxiliary module. */
2055 bool
2056 varpool_is_auxiliary (struct varpool_node *node)
2058 return (node->module_id
2059 && node->module_id != primary_module_id);
2062 /* Return the varpool_node to which DECL is resolved to during linking.
2063 This method can not be used after static to global promotion happens. */
2065 struct varpool_node *
2066 real_varpool_node (tree decl)
2068 void **slot;
2069 tree name;
2071 if (!L_IPO_COMP_MODE || !varpool_symtab)
2072 return varpool_node (decl);
2074 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
2075 return varpool_node (decl);
2077 name = DECL_ASSEMBLER_NAME (decl);
2078 slot = htab_find_slot_with_hash (varpool_symtab, name,
2079 decl_assembler_name_hash (name),
2080 NO_INSERT);
2081 gcc_assert (slot && *slot);
2082 return (struct varpool_node *)*slot;
2085 /* Remove NODE from the link table. */
2087 void
2088 varpool_remove_link_node (struct varpool_node *node)
2090 tree name;
2091 tree decl;
2093 if (!L_IPO_COMP_MODE || !varpool_symtab)
2094 return;
2096 decl = node->decl;
2098 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
2099 return;
2101 if (real_varpool_node (decl) != node)
2102 return;
2104 name = DECL_ASSEMBLER_NAME (decl);
2105 htab_remove_elt_with_hash (varpool_symtab, name,
2106 decl_assembler_name_hash (name));
2109 /* Merge the addressable attribute from DECL2 to DECL1. */
2111 static inline void
2112 merge_addressable_attr (tree decl1, tree decl2)
2114 if (TREE_ADDRESSABLE (decl2))
2115 TREE_ADDRESSABLE (decl1) = 1;
2118 /* Resolve NODE->decl to symbol table entry *SLOT. */
2120 static void
2121 resolve_varpool_node (struct varpool_node **slot, struct varpool_node *node)
2123 tree decl1, decl2;
2125 decl1 = (*slot)->decl;
2126 decl2 = node->decl;
2128 /* Take the decl with the complete type. */
2129 if (COMPLETE_TYPE_P (TREE_TYPE (decl1))
2130 && !COMPLETE_TYPE_P (TREE_TYPE (decl2)))
2132 merge_addressable_attr (decl1, decl2);
2133 return;
2135 if (!COMPLETE_TYPE_P (TREE_TYPE (decl1))
2136 && COMPLETE_TYPE_P (TREE_TYPE (decl2)))
2138 *slot = node;
2139 merge_addressable_attr (decl2, decl1);
2140 return;
2143 /* Either all complete or neither's type is complete. Just
2144 pick the primary module's decl. */
2145 if (!varpool_is_auxiliary (*slot))
2147 merge_addressable_attr (decl1, decl2);
2148 return;
2151 if (!varpool_is_auxiliary (node))
2153 *slot = node;
2154 merge_addressable_attr (decl2, decl1);
2155 return;
2158 merge_addressable_attr (decl1, decl2);
2159 return;
2162 /* Link NODE into var_decl symbol table. */
2164 void
2165 varpool_link_node (struct varpool_node *node)
2167 tree name;
2168 void **slot;
2170 if (!L_IPO_COMP_MODE || !varpool_symtab)
2171 return;
2173 if (!TREE_PUBLIC (node->decl) || DECL_ARTIFICIAL (node->decl))
2174 return;
2176 name = DECL_ASSEMBLER_NAME (node->decl);
2177 slot = htab_find_slot_with_hash (varpool_symtab, name,
2178 decl_assembler_name_hash (name),
2179 INSERT);
2180 if (*slot)
2181 resolve_varpool_node ((struct varpool_node **) slot, node);
2182 else
2183 *slot = node;
2186 /* Perform cross module linking for var_decls. */
2188 void
2189 varpool_do_link (void)
2191 struct varpool_node *node;
2193 if (!L_IPO_COMP_MODE)
2194 return;
2196 varpool_symtab
2197 = htab_create_ggc (10, hash_node_by_assembler_name,
2198 eq_node_assembler_name, NULL);
2199 for (node = varpool_nodes; node; node = node->next)
2200 varpool_link_node (node);
2202 /* Merge the externally visible attribute. */
2203 for (node = varpool_nodes; node; node = node->next)
2204 if (node->externally_visible)
2205 (real_varpool_node (node->decl))->externally_visible = true;
2208 /* Get the list of assembler name ids with reference bit set. */
2210 void
2211 varpool_get_referenced_asm_ids (VEC(tree, gc) ** ids)
2213 struct varpool_node *node;
2214 for (node = varpool_nodes; node; node = node->next)
2216 tree asm_id = NULL;
2217 tree decl = node->decl;
2218 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2220 asm_id = DECL_ASSEMBLER_NAME (decl);
2221 VEC_safe_push (tree, gc, *ids, asm_id);
2226 /* Clear the referenced bit in all assembler ids. */
2228 void
2229 varpool_clear_asm_id_reference_bit (void)
2231 struct varpool_node *node;
2232 for (node = varpool_nodes; node; node = node->next)
2234 tree asm_id = NULL;
2235 tree decl = node->decl;
2236 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2238 asm_id = DECL_ASSEMBLER_NAME (decl);
2239 TREE_SYMBOL_REFERENCED (asm_id) = 0;
2245 #include "gt-l-ipo.h"