port r196072 (passing GCC mem usage to dyn-ipa) from google/gcc-4_7
[official-gcc.git] / gcc-4_8 / gcc / l-ipo.c
blobf8b795a688f01822c4d18b9bcd74e0fe2d8a707e
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 "timevar.h"
37 #include "vec.h"
39 unsigned ggc_total_memory; /* in KB */
41 struct GTY(()) saved_module_scope
43 vec<tree, va_gc> *module_decls;
44 unsigned module_id;
47 static GTY (()) struct saved_module_scope *current_module_scope;
48 static GTY ((param_is (struct saved_module_scope))) htab_t saved_module_scope_map;
49 static int primary_module_last_funcdef_no = 0;
50 /* Function id space for each module are qualified by the module id. After all the files
51 are parsed, we need to reset the funcdef_no to the max value from all module so that
52 the function clones do not assigned with ids colliding with some other orignal function
53 in the same module. */
54 static int max_funcdef_no = 0;
55 static location_t primary_module_last_loc;
56 /* Primary module pending templates. */
57 /* Referenced asm ids in primary module. */
58 static GTY (()) vec<tree, va_gc> *referenced_asm_ids = NULL;
59 bool parser_parsing_start = false;
60 /* Nonzero if we're done parsing and into end-of-file activities. */
61 int at_eof;
63 static int aggr_has_equiv_id (tree t1, tree t2);
65 /* Module scope hash function. */
67 static hashval_t
68 htab_module_scope_hash (const void *ent)
70 const struct saved_module_scope *const entry
71 = (const struct saved_module_scope *) ent;
72 return (hashval_t) entry->module_id;
75 /* Module scope equality function. */
77 static int
78 htab_module_scope_eq (const void *ent1, const void *ent2)
80 const struct saved_module_scope *const entry1
81 = (const struct saved_module_scope *) ent1;
82 const struct saved_module_scope *const entry2
83 = (const struct saved_module_scope *) ent2;
85 return entry1->module_id == entry2->module_id;
88 /* Returns the module scope given a module id MOD_ID. */
90 static struct saved_module_scope *
91 get_module_scope (unsigned mod_id)
93 struct saved_module_scope **slot, key, *module_scope;
95 gcc_assert (mod_id);
97 if (saved_module_scope_map == NULL)
98 saved_module_scope_map = htab_create_ggc (10, htab_module_scope_hash,
99 htab_module_scope_eq, NULL);
100 key.module_id = mod_id;
101 slot = (struct saved_module_scope **)
102 htab_find_slot (saved_module_scope_map, &key, INSERT);
103 module_scope = *slot;
104 if (!module_scope)
106 module_scope = ggc_alloc_cleared_saved_module_scope ();
107 module_scope->module_id = mod_id;
108 *slot = module_scope;
110 return module_scope;
113 /* Allocate memory for struct lang_decl for tree T. */
115 static struct lang_decl *
116 alloc_lang_decl (tree t)
118 size_t size;
119 size = lang_hooks.l_ipo.get_lang_decl_size (t);
120 return ggc_alloc_cleared_lang_decl (size);
123 /* Return a cloned copy of tree SRC. */
125 tree
126 lipo_save_decl (tree src)
128 tree saved = copy_node (src);
129 enum tree_code tc = TREE_CODE (src);
130 if (TREE_CODE_CLASS (tc) == tcc_declaration)
132 struct lang_decl *ls = NULL;
133 struct function *func = NULL;
134 DECL_CONTEXT (saved) = DECL_CONTEXT (src);
135 if (DECL_LANG_SPECIFIC (src))
137 ls = alloc_lang_decl (src);
138 memcpy (ls, DECL_LANG_SPECIFIC (src),
139 lang_hooks.l_ipo.get_lang_decl_size (src));
141 DECL_LANG_SPECIFIC (saved) = ls;
142 if (tc == FUNCTION_DECL && DECL_STRUCT_FUNCTION (src))
144 func = ggc_alloc_cleared_function ();
145 *func = *(DECL_STRUCT_FUNCTION (src));
146 DECL_STRUCT_FUNCTION (saved) = func;
149 else
151 gcc_assert (TREE_CODE_CLASS (tc) == tcc_type &&
152 TYPE_MAIN_VARIANT (src) == src);
153 TYPE_CONTEXT (saved) = TYPE_CONTEXT (src);
154 lang_hooks.l_ipo.dup_lang_type (src, saved);
157 return saved;
160 /* Copy tree SAVED to tree DEST. */
162 void
163 lipo_restore_decl (tree dest, tree saved)
165 enum tree_code tc;
166 unsigned old_uid;
167 struct lang_decl *oldls;
169 tc = TREE_CODE (saved);
170 if (TREE_CODE_CLASS (tc) == tcc_declaration)
172 struct function *oldfunc = NULL;
173 old_uid = DECL_UID (dest);
174 oldls = DECL_LANG_SPECIFIC (dest);
175 oldfunc
176 = (tc == FUNCTION_DECL ? DECL_STRUCT_FUNCTION (dest) : NULL);
178 memcpy ((char *) dest + sizeof (struct tree_common),
179 (char *) saved + sizeof (struct tree_common),
180 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
182 if (tc == FUNCTION_DECL)
183 memcpy ((char *) dest + sizeof (struct tree_decl_common),
184 (char *) saved + sizeof (struct tree_decl_common),
185 sizeof (struct tree_function_decl)
186 - sizeof (struct tree_decl_common));
188 DECL_UID (dest) = old_uid;
189 if (DECL_LANG_SPECIFIC (saved))
191 if (!oldls)
192 oldls = alloc_lang_decl (dest);
193 memcpy (oldls, DECL_LANG_SPECIFIC (saved),
194 lang_hooks.l_ipo.get_lang_decl_size (saved));
195 DECL_LANG_SPECIFIC (dest) = oldls;
197 else
198 DECL_LANG_SPECIFIC (dest) = NULL;
200 if (tc == FUNCTION_DECL)
202 if (DECL_STRUCT_FUNCTION (saved))
204 if (!oldfunc)
205 oldfunc = ggc_alloc_cleared_function ();
206 *oldfunc = *(DECL_STRUCT_FUNCTION (saved));
207 DECL_STRUCT_FUNCTION (dest) = oldfunc;
209 else
210 DECL_STRUCT_FUNCTION (dest) = NULL;
213 else
215 gcc_assert (TREE_CODE_CLASS (tc) == tcc_type);
216 lang_hooks.l_ipo.copy_lang_type (saved, dest);
221 /* Return the name for tree TD which is either a decl or type. */
223 tree
224 get_type_or_decl_name (tree td)
226 tree id;
228 if (DECL_P (td))
229 id = DECL_NAME (td);
230 else
232 id = TYPE_NAME (td);
233 if (DECL_P (id))
234 id = DECL_NAME (id);
236 return id;
239 /* For a DECL (a type or a decl) in SCOPE, check to see if it is in
240 global or namespace scope. If yes, add it to the current module scope. */
242 void
243 add_decl_to_current_module_scope (tree decl, void *scope)
245 struct saved_module_scope *module_scope;
246 tree id;
248 if (!flag_dyn_ipa)
249 return;
251 if (!parser_parsing_start)
253 /* The source file may contains only global variable declations
254 -- there is no module grouping data associated with it, so
255 neither primary_module_id nor current_module_id is set. */
256 lang_hooks.l_ipo.add_built_in_decl (decl);
257 return;
260 if (!L_IPO_COMP_MODE)
261 return;
263 if (!lang_hooks.l_ipo.has_global_name (decl, scope))
264 return;
266 /* Unlike C++ where names are attached to type decls, for C, the type name
267 is identifier node. Thus we need to track type names as well. */
268 id = get_type_or_decl_name (decl);
269 if (!id)
270 return;
272 module_scope = current_module_scope;
273 gcc_assert (module_scope && module_scope->module_id == current_module_id);
274 vec_safe_push (module_scope->module_decls, decl);
277 /* Clear name bindings for all decls created in MODULE_SCOPE. */
279 static void
280 clear_module_scope_bindings (struct saved_module_scope *module_scope)
282 size_t i;
283 tree decl;
285 for (i = 0;
286 vec_safe_iterate (module_scope->module_decls, i, &decl);
287 ++i)
289 lang_hooks.l_ipo.clear_global_name_bindings (
290 get_type_or_decl_name (decl));
291 /* Now force creating assembly name. */
292 if (VAR_OR_FUNCTION_DECL_P (decl))
294 tree assembler_name;
296 if (HAS_DECL_ASSEMBLER_NAME_P (decl)
297 && DECL_ASSEMBLER_NAME_SET_P (decl))
299 assembler_name = DECL_ASSEMBLER_NAME (decl);
300 lang_hooks.l_ipo.clear_global_name_bindings (assembler_name);
306 /* The referenced attribute of a decl is not associated with the
307 decl itself but with the assembler name. Remember the referenced
308 bits before clearing them. */
310 static void
311 save_assembler_name_reference_bit (void)
313 varpool_get_referenced_asm_ids (&referenced_asm_ids);
316 /* Clear the reference bits for assembler names before closing the
317 module scope. */
319 static void
320 clear_assembler_name_reference_bit (void)
322 varpool_clear_asm_id_reference_bit ();
325 /* Restore the reference bits for assembler names. */
327 static void
328 restore_assembler_name_reference_bit (void)
330 size_t i;
331 tree nm;
332 for (i = 0;
333 vec_safe_iterate (referenced_asm_ids, i, &nm);
334 ++i)
335 TREE_SYMBOL_REFERENCED (nm) = 1;
338 /* Set up the module scope before the parsing of the
339 associated source file. */
341 void
342 push_module_scope (void)
344 struct saved_module_scope *prev_module_scope;
346 if (!flag_dyn_ipa || !L_IPO_COMP_MODE)
348 parser_parsing_start = true;
349 return;
352 prev_module_scope = current_module_scope;
353 if (L_IPO_IS_PRIMARY_MODULE)
355 gcc_assert (!prev_module_scope);
356 lang_hooks.l_ipo.save_built_in_decl_pre_parsing ();
357 parser_parsing_start = true;
360 gcc_assert (current_module_id);
362 /* Set up the module scope. */
363 current_module_scope = get_module_scope (current_module_id);
364 return;
367 /* Restore the shared decls to their post parsing states. */
369 static void
370 restore_post_parsing_states (void)
372 current_module_id = primary_module_id;
373 current_module_scope = get_module_scope (primary_module_id);
374 set_funcdef_no (max_funcdef_no);
375 input_location = primary_module_last_loc;
377 restore_assembler_name_reference_bit ();
378 lang_hooks.l_ipo.restore_built_in_decl_post_module_parsing ();
381 /* Pop the current module scope (by clearing name bindings etc.)
382 and prepare for parsing of the next module. In particular,
383 built-in decls need to be restored to the state before file
384 parsing starts. */
386 void
387 pop_module_scope (void)
389 bool is_last = false;
390 int last_funcdef_no;
392 if (!flag_dyn_ipa || !L_IPO_COMP_MODE)
393 return;
395 gcc_assert (current_module_id && current_module_scope);
397 if (L_IPO_IS_PRIMARY_MODULE)
398 primary_module_last_loc = input_location;
400 at_eof = 1;
401 cgraph_process_same_body_aliases ();
402 lang_hooks.l_ipo.process_pending_decls (input_location);
403 lang_hooks.l_ipo.clear_deferred_fns ();
404 at_eof = 0;
406 is_last = is_last_module (current_module_id);
408 last_funcdef_no = get_last_funcdef_no ();
409 if (last_funcdef_no > max_funcdef_no)
410 max_funcdef_no = last_funcdef_no;
412 lang_hooks.l_ipo.save_built_in_decl_post_module_parsing ();
413 /* Save primary module state if needed (when module group
414 size > 1) */
415 if (L_IPO_IS_PRIMARY_MODULE && num_in_fnames > 1)
417 save_assembler_name_reference_bit ();
418 primary_module_last_funcdef_no = last_funcdef_no;
421 if (!is_last)
423 /* More aux modules are anticipated, clear
424 the parsing state. */
425 gcc_assert (num_in_fnames > 1);
426 clear_assembler_name_reference_bit ();
427 clear_module_scope_bindings (current_module_scope);
428 /* Restore symtab bindings for builtins */
429 lang_hooks.l_ipo.restore_built_in_decl_pre_parsing ();
430 /* The map can not be cleared because the names of operator
431 decls are used to store the information about the conversion
432 target type. This forces the coversion operator ids to be
433 incremented across different modules, and assember id must
434 be used for checksum computation. */
435 /* cp_clear_conv_type_map (); */
437 else if (num_in_fnames > 1)
439 clear_module_scope_bindings (current_module_scope);
440 restore_post_parsing_states ();
442 else
443 gcc_assert (L_IPO_IS_PRIMARY_MODULE && num_in_fnames == 1);
447 /* Type merging support for LIPO */
449 struct type_ec
451 tree rep_type;
452 vec<tree> *eq_types;
455 static vec<tree> *pending_types = NULL;
456 static struct pointer_set_t *type_set = NULL;
457 static htab_t type_hash_tab = NULL;
459 /* Hash function for the type table. */
461 static hashval_t
462 type_hash_hash (const void *ent)
464 tree type, name;
465 const struct type_ec *const entry
466 = (const struct type_ec *) ent;
468 type = entry->rep_type;
469 name = TYPE_NAME (type);
470 if (DECL_P (name))
471 name = DECL_NAME (name);
473 return htab_hash_string (IDENTIFIER_POINTER (name));
476 /* Equality function for type hash table. */
478 static int
479 type_hash_eq (const void *ent1, const void *ent2)
481 tree type1, type2;
482 const struct type_ec *const entry1
483 = (const struct type_ec *) ent1;
484 const struct type_ec *const entry2
485 = (const struct type_ec *) ent2;
487 type1 = entry1->rep_type;
488 type2 = entry2->rep_type;
490 return aggr_has_equiv_id (type1, type2);
493 /* Function to delete type hash entries. */
495 static void
496 type_hash_del (void *ent)
498 struct type_ec *const entry
499 = (struct type_ec *) ent;
501 vec_free (entry->eq_types);
502 free (entry);
505 struct GTY(()) type_ent
507 tree type;
508 unsigned eq_id;
511 static GTY ((param_is (struct type_ent))) htab_t l_ipo_type_tab = 0;
512 static unsigned l_ipo_eq_id = 0;
514 /* Address hash function for struct type_ent. */
516 static hashval_t
517 type_addr_hash (const void *ent)
519 const struct type_ent *const entry
520 = (const struct type_ent *) ent;
521 return (hashval_t) (long) entry->type;
524 /* Address equality function for type_ent. */
526 static int
527 type_addr_eq (const void *ent1, const void *ent2)
529 const struct type_ent *const entry1
530 = (const struct type_ent *) ent1;
531 const struct type_ent *const entry2
532 = (const struct type_ent *) ent2;
533 return entry1->type == entry2->type;
536 /* Returns 1 if NS1 and NS2 refer to the same namespace. */
538 static int
539 is_ns_equiv (tree ns1, tree ns2)
541 tree n1, n2;
542 if (ns1 == NULL && ns2 == NULL)
543 return 1;
545 if ((!ns1 && ns2) || (ns1 && !ns2))
546 return 0;
548 gcc_assert (DECL_P (ns1) && DECL_P (ns2));
550 if (!is_ns_equiv (DECL_CONTEXT (ns1),
551 DECL_CONTEXT (ns2)))
552 return 0;
554 n1 = DECL_NAME (ns1);
555 n2 = DECL_NAME (ns2);
556 if (n1 == 0 && n2 == 0)
557 /* Conservative (which can happen when two NSes are from
558 different modules but with same UID) quivalence is allowed. */
559 return DECL_UID (ns1) == DECL_UID (ns2);
560 if (!n1 || !n2)
561 return 0;
563 if (!strcmp (IDENTIFIER_POINTER (n1),
564 IDENTIFIER_POINTER (n2)))
565 return 1;
567 return 0;
570 /* Returns 1 if aggregate type T1 and T2 have equivalent qualified
571 ids. */
573 static int
574 aggr_has_equiv_id (tree t1, tree t2)
576 int ctx_match;
577 tree ctx1, ctx2, tn1, tn2;
578 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
580 ctx1 = TYPE_CONTEXT (t1);
581 ctx2 = TYPE_CONTEXT (t2);
583 if ((ctx1 && !ctx2) || (!ctx1 && ctx2))
584 return 0;
586 if (ctx1 && TREE_CODE (ctx1) != TREE_CODE (ctx2))
587 return 0;
589 if (ctx1 && (TREE_CODE (ctx1) == FUNCTION_DECL
590 || TREE_CODE (ctx1) == BLOCK))
591 return 0;
593 if (!ctx1)
595 ctx_match = 1;
596 gcc_assert (!ctx2);
598 else if (TREE_CODE (ctx1) == NAMESPACE_DECL)
599 ctx_match = is_ns_equiv (ctx1, ctx2);
600 else if (TYPE_P (ctx1))
601 ctx_match = aggr_has_equiv_id (ctx1, ctx2);
602 else
604 gcc_assert (TREE_CODE (ctx1) == TRANSLATION_UNIT_DECL);
605 ctx_match = 1;
608 if (!ctx_match)
609 return 0;
611 /* Now compare the name of the types. */
612 tn1 = TYPE_NAME (t1);
613 tn2 = TYPE_NAME (t2);
614 if ((tn1 && !tn2) || !(tn1 && tn2))
615 return 0;
616 else if (!tn1 && !tn2)
617 /* Be conservative on unamed types. */
618 return 1;
620 if (DECL_P (tn1))
621 tn1 = DECL_NAME (tn1);
622 if (DECL_P (tn2))
623 tn2 = DECL_NAME (tn2);
624 if (strcmp (IDENTIFIER_POINTER (tn1),
625 IDENTIFIER_POINTER (tn2)))
626 return 0;
628 return lang_hooks.l_ipo.cmp_lang_type (t1, t2);
631 /* Return the canonical type of the type's main variant. */
632 static inline tree
633 get_norm_type (const_tree type)
635 tree cano_type = TYPE_MAIN_VARIANT (type);
636 if (TYPE_CANONICAL (cano_type))
637 cano_type = TYPE_CANONICAL (cano_type);
639 return cano_type;
642 /* Return 1 if type T1 and T2 are equivalent. Struct/union/class
643 types are compared using qualified name ids. Alias sets of
644 equivalent types will be merged. Client code may choose to do
645 structural equivalence check for sanity. Note the difference
646 between the types_compatible_p (and its langhooks subroutines)
647 and this interface. The former is mainly used to remove useless
648 type conversion and value numbering computation. It returns 1
649 only when it is sure and should not be used in contexts where
650 erroneously returning 0 causes problems. This interface
651 lipo_cmp_type behaves differently - it returns 1 when it is not
652 sure -- as the primary purpose of the interface is for alias
653 set computation. */
656 lipo_cmp_type (tree t1, tree t2)
658 if (TREE_CODE (t1) != TREE_CODE (t2))
659 return 0;
660 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
661 return 0;
662 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
663 return 0;
665 t1 = get_norm_type (t1);
666 t2 = get_norm_type (t2);
668 switch (TREE_CODE (t1))
670 case RECORD_TYPE:
671 case UNION_TYPE:
672 case QUAL_UNION_TYPE:
673 return aggr_has_equiv_id (t1, t2);
675 case POINTER_TYPE:
676 case REFERENCE_TYPE:
677 case COMPLEX_TYPE:
678 return lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2));
679 case ARRAY_TYPE:
680 return (TYPE_DOMAIN (t1) == NULL || TYPE_DOMAIN (t2) == NULL
681 || (lipo_cmp_type (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2))
682 && lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2))));
683 case METHOD_TYPE:
684 return lipo_cmp_type (TYPE_METHOD_BASETYPE (t1),
685 TYPE_METHOD_BASETYPE (t2));
686 case FUNCTION_TYPE:
688 tree arg1, arg2;
689 for (arg1 = TYPE_ARG_TYPES (t1), arg2 = TYPE_ARG_TYPES (t2);
690 arg1 && arg2;
691 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
692 if (!lipo_cmp_type (TREE_VALUE (arg1),
693 TREE_VALUE (arg2)))
694 return 0;
695 if (arg1 || arg2)
696 return 0;
697 return 1;
699 case OFFSET_TYPE:
700 return lipo_cmp_type (TYPE_OFFSET_BASETYPE (t1),
701 TYPE_OFFSET_BASETYPE (t2));
702 case ENUMERAL_TYPE:
703 return lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2));
704 case REAL_TYPE:
705 case FIXED_POINT_TYPE:
706 case INTEGER_TYPE:
707 return (TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
708 && TYPE_MODE (t1) == TYPE_MODE (t2)
709 && TYPE_MIN_VALUE (t1) == TYPE_MIN_VALUE (t2)
710 && TYPE_MAX_VALUE (t1) == TYPE_MAX_VALUE (t2));
711 case VECTOR_TYPE:
712 return (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
713 && lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2)));
714 case VOID_TYPE:
715 case BOOLEAN_TYPE:
716 return 1;
717 case TEMPLATE_TYPE_PARM:
718 return 1;
719 default:
720 gcc_unreachable ();
724 #ifndef ANON_AGGRNAME_PREFIX
725 #define ANON_AGGRNAME_PREFIX "__anon_"
726 #endif
727 #ifndef ANON_AGGRNAME_P
728 #define ANON_AGGRNAME_P(ID_NODE) \
729 (!strncmp (IDENTIFIER_POINTER (ID_NODE), ANON_AGGRNAME_PREFIX, \
730 sizeof (ANON_AGGRNAME_PREFIX) - 1))
731 #endif
733 /* Callback function used in tree walk to find referenced struct types. */
735 static tree
736 find_struct_types (tree *tp,
737 int *walk_subtrees ATTRIBUTE_UNUSED,
738 void *data ATTRIBUTE_UNUSED)
740 if (!(*tp))
741 return NULL_TREE;
743 if (TYPE_P (*tp))
745 if (lang_hooks.l_ipo.is_compiler_generated_type (*tp))
746 return NULL_TREE;
748 switch (TREE_CODE (*tp))
750 case RECORD_TYPE:
751 case UNION_TYPE:
752 case QUAL_UNION_TYPE:
754 tree cano_type, name;
755 tree context;
756 tree field;
758 cano_type = get_norm_type (*tp);
759 name = TYPE_NAME (cano_type);
760 if (!name)
762 /* the main variant of typedef of unnamed struct
763 has no name, use the orignal type for equivalence. */
764 cano_type = *tp;
765 name = TYPE_NAME (cano_type);
767 if (!name)
768 return NULL_TREE;
769 if (DECL_P (name)
770 && (DECL_IGNORED_P (name)
771 || ANON_AGGRNAME_P (DECL_NAME (name))))
772 return NULL_TREE;
774 if (!pointer_set_insert (type_set, cano_type))
775 pending_types->safe_push (cano_type);
776 else
777 return NULL_TREE; /* Or use walk tree without dups. */
779 context = TYPE_CONTEXT (cano_type);
780 if (context && TYPE_P (context))
781 walk_tree (&context, find_struct_types, NULL, NULL);
783 /* Instantiate a nested work as the tree walker does not
784 get to the fields. */
785 if (TYPE_BINFO (cano_type))
787 int i;
788 tree binfo, base_binfo;
790 for (binfo = TYPE_BINFO (cano_type), i = 0;
791 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
792 walk_tree (&BINFO_TYPE (base_binfo), find_struct_types,
793 NULL, NULL);
795 for (field = TYPE_FIELDS (cano_type);
796 field != 0;
797 field = TREE_CHAIN (field))
798 walk_tree (&TREE_TYPE (field), find_struct_types,
799 NULL, NULL);
800 return NULL_TREE;
802 default:
803 return NULL_TREE;
806 else if (DECL_P (*tp))
807 /* walk tree does not walk down decls, so do a nested walk here. */
808 walk_tree (&(TREE_TYPE (*tp)), find_struct_types, NULL, NULL);
810 return NULL_TREE;
813 /* Collect referenced struct types. */
815 static void
816 cgraph_collect_type_referenced (void)
818 basic_block bb;
819 gimple_stmt_iterator gi;
821 FOR_EACH_BB (bb)
823 for (gi = gsi_start_bb (bb); !gsi_end_p (gi); gsi_next (&gi))
825 unsigned i;
826 gimple stmt = gsi_stmt (gi);
827 for (i = 0; i < gimple_num_ops (stmt); i++)
828 walk_tree (gimple_op_ptr (stmt, i), find_struct_types, NULL, NULL);
833 /* Check type equivalence. Returns 1 if T1 and T2 are equivalent
834 for tbaa; return 0 if not. -1 is returned if it is unknown. */
837 equivalent_struct_types_for_tbaa (const_tree t1, const_tree t2)
839 struct type_ent key, *tent1, *tent2, **slot;
841 if (!l_ipo_type_tab)
842 return -1;
844 t1 = get_norm_type (t1);
845 t2 = get_norm_type (t2);
847 key.type = (tree) (long) t1;
848 slot = (struct type_ent **)
849 htab_find_slot (l_ipo_type_tab, &key, NO_INSERT);
850 if (!slot || !*slot)
851 return -1;
852 tent1 = *slot;
854 key.type = (tree) (long) t2;
855 slot = (struct type_ent **)
856 htab_find_slot (l_ipo_type_tab, &key, NO_INSERT);
857 if (!slot || !*slot)
858 return -1;
859 tent2 = *slot;
861 return tent1->eq_id == tent2->eq_id;
864 /* Build type hash table. */
866 static void
867 cgraph_build_type_equivalent_classes (void)
869 unsigned n, i;
870 n = pending_types->length ();
871 for (i = 0; i < n; i++)
873 struct type_ec **slot;
874 struct type_ec te;
875 te.rep_type = (*pending_types)[i];
876 te.eq_types = NULL;
877 slot = (struct type_ec **) htab_find_slot (type_hash_tab,
878 &te, INSERT);
879 if (!*slot)
881 *slot = XCNEW (struct type_ec);
882 (*slot)->rep_type = te.rep_type;
883 vec_alloc ((*slot)->eq_types, 10);
885 (*slot)->eq_types->safe_push (te.rep_type);
889 /* Re-propagate component types's alias set to that of TYPE. PROCESSED
890 is the pointer set of processed types. */
892 static void
893 re_record_component_aliases (tree type,
894 struct pointer_set_t *processed)
896 alias_set_type superset = get_alias_set (type);
897 tree field;
899 if (superset == 0)
900 return;
902 if (pointer_set_insert (processed, type))
903 return;
905 switch (TREE_CODE (type))
907 case RECORD_TYPE:
908 case UNION_TYPE:
909 case QUAL_UNION_TYPE:
910 /* Recursively record aliases for the base classes, if there are any. */
911 if (TYPE_BINFO (type))
913 int i;
914 tree binfo, base_binfo;
916 for (binfo = TYPE_BINFO (type), i = 0;
917 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
919 re_record_component_aliases (BINFO_TYPE (base_binfo),
920 processed);
921 record_alias_subset (superset,
922 get_alias_set (BINFO_TYPE (base_binfo)));
925 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
926 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
928 re_record_component_aliases (TREE_TYPE (field), processed);
929 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
931 break;
933 case COMPLEX_TYPE:
934 re_record_component_aliases (TREE_TYPE (type), processed);
935 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
936 break;
938 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
939 element type. */
941 default:
942 break;
946 /* The callback function to merge alias sets of equivalent types. */
948 static int
949 type_eq_process (void **slot, void *data ATTRIBUTE_UNUSED)
951 unsigned i;
952 alias_set_type alias_set, ptr_alias_set = -1;
953 tree rep_type, type;
954 vec<tree> *eq_types;
955 struct type_ec ** te = (struct type_ec **)slot;
956 bool zero_set = false, ptr_zero_set = false;
957 struct type_ent **slot2, key, *tent;
960 rep_type = (*te)->rep_type;
961 eq_types = (*te)->eq_types;
962 alias_set = get_alias_set (rep_type);
964 for (i = 0; eq_types->iterate (i, &type); ++i)
966 alias_set_type als, ptr_als = -1;
967 tree type_ptr = TYPE_POINTER_TO (type);;
969 als = get_alias_set (type);
970 if (als == 0)
971 zero_set = true;
973 if (alias_set && als && alias_set != als)
974 record_alias_subset (alias_set, als);
976 if (type_ptr)
978 ptr_als = get_alias_set (type_ptr);
979 if (ptr_als == 0)
980 ptr_zero_set = true;
982 if (ptr_alias_set == -1)
983 ptr_alias_set = ptr_als;
984 else
986 if (!ptr_zero_set && ptr_alias_set != ptr_als)
987 record_alias_subset (ptr_alias_set, ptr_als);
992 /* Now propagate back. */
993 for (i = 0; eq_types->iterate (i, &type); ++i)
995 alias_set_type als, ptr_als;
996 tree ptr_type = TYPE_POINTER_TO (type);
998 als = get_alias_set (type);
1000 if (zero_set)
1001 TYPE_ALIAS_SET (type) = 0;
1002 else if (alias_set != als)
1003 record_alias_subset (als, alias_set);
1005 if (ptr_type)
1007 ptr_als = get_alias_set (ptr_type);
1008 if (ptr_zero_set)
1009 TYPE_ALIAS_SET (ptr_type) = 0;
1010 else if (ptr_alias_set != ptr_als)
1011 record_alias_subset (ptr_als, ptr_alias_set);
1016 /* Now populate the type table. */
1017 l_ipo_eq_id++;
1018 for (i = 0; eq_types->iterate (i, &type); ++i)
1020 key.type = type;
1021 slot2 = (struct type_ent **)
1022 htab_find_slot (l_ipo_type_tab, &key, INSERT);
1023 tent = *slot2;
1024 gcc_assert (!tent);
1025 tent = ggc_alloc_cleared_type_ent ();
1026 tent->type = key.type;
1027 tent->eq_id = l_ipo_eq_id;
1028 *slot2 = tent;
1031 return 1;
1034 /* Regenerate alias set for aggregate types. */
1036 static void
1037 record_components_for_parent_types (void)
1039 unsigned n, i;
1040 struct pointer_set_t *processed_types;
1042 processed_types = pointer_set_create ();
1043 n = pending_types->length ();
1044 for (i = 0; i < n; i++)
1046 tree type = (*pending_types)[i];
1047 re_record_component_aliases (type, processed_types);
1050 pointer_set_destroy (processed_types);
1053 /* Unify type alias sets for equivalent types. */
1055 void
1056 cgraph_unify_type_alias_sets (void)
1058 struct cgraph_node *node;
1059 struct varpool_node *pv;
1061 if (!L_IPO_COMP_MODE)
1062 return;
1064 vec_alloc (pending_types, 100);
1065 type_set = pointer_set_create ();
1066 type_hash_tab = htab_create (10, type_hash_hash,
1067 type_hash_eq, type_hash_del);
1068 l_ipo_type_tab = htab_create_ggc (10, type_addr_hash,
1069 type_addr_eq, NULL);
1071 FOR_EACH_DEFINED_FUNCTION (node)
1073 if (!gimple_has_body_p (node->symbol.decl))
1074 continue;
1075 push_cfun (DECL_STRUCT_FUNCTION (node->symbol.decl));
1076 current_function_decl = node->symbol.decl;
1077 if (gimple_has_body_p (current_function_decl))
1078 cgraph_collect_type_referenced ();
1079 current_function_decl = NULL;
1080 set_cfun (NULL);
1081 pop_cfun ();
1084 FOR_EACH_VARIABLE (pv)
1085 walk_tree (&pv->symbol.decl, find_struct_types, NULL, NULL);
1087 /* Compute type equivalent classes. */
1088 cgraph_build_type_equivalent_classes ();
1089 /* Now unify alias sets of equivelent types. */
1090 htab_traverse (type_hash_tab, type_eq_process, NULL);
1091 /* Finally re-populating parent's alias set. */
1092 record_components_for_parent_types ();
1094 pointer_set_destroy (type_set);
1095 vec_free (pending_types);
1096 htab_delete (type_hash_tab);
1099 /* Return true if NODE->decl from an auxiliary module has external
1100 definition (and therefore is not needed for expansion). */
1102 bool
1103 cgraph_is_aux_decl_external (struct cgraph_node *node)
1105 tree decl = node->symbol.decl;
1107 if (!L_IPO_COMP_MODE)
1108 return false;
1110 if (!cgraph_is_auxiliary (decl))
1111 return false;
1113 /* Versioned clones from auxiliary moduels are not external. */
1114 if (node->is_versioned_clone)
1115 return false;
1117 /* Comdat or weak functions in aux modules are not external --
1118 there is no guarantee that the definitition will be emitted
1119 in the primary compilation of this auxiliary module. */
1120 if (DECL_COMDAT (decl) || DECL_WEAK (decl))
1121 return false;
1123 /* virtual functions won't be deleted in the primary module. */
1124 if (DECL_VIRTUAL_P (decl))
1125 return true;
1127 if (!TREE_PUBLIC (decl))
1128 return false;
1130 /* The others from aux modules are external. */
1131 return true;
1134 /* Linked function symbol (cgraph node) table. */
1135 static GTY((param_is (struct cgraph_sym))) htab_t cgraph_symtab;
1137 /* This is true when global linking is needed and performed (for C++).
1138 For C, symbol linking is performed on the fly during parsing, and
1139 the cgraph_symtab is used only for keeping additional information
1140 for any already merged symbol if needed. */
1142 static bool global_link_performed = 0;
1144 /* For an external (non-defined) function DECL, return the primary
1145 module id (even though when the declaration is declared in an aux
1146 module). For a defined function DECL, return the module id in which
1147 it is defined. */
1149 unsigned
1150 cgraph_get_module_id (tree decl)
1152 struct function *func = DECL_STRUCT_FUNCTION (decl);
1153 /* Not defined. */
1154 if (!func)
1155 return primary_module_id;
1156 return FUNC_DECL_MODULE_ID (func);
1159 /* Return true if function decl is defined in an auxiliary module. */
1161 bool
1162 cgraph_is_auxiliary (tree decl)
1164 return (cgraph_get_module_id (decl) != primary_module_id);
1167 /* Return the hash value for cgraph_sym pointed to by P. The
1168 hash value is computed using function's assembler name. */
1170 static hashval_t
1171 hash_sym_by_assembler_name (const void *p)
1173 const struct cgraph_sym *n = (const struct cgraph_sym *) p;
1174 return (hashval_t) decl_assembler_name_hash (n->assembler_name);
1177 /* Return nonzero if P1 and P2 are equal. */
1179 static int
1180 eq_assembler_name (const void *p1, const void *p2)
1182 const struct cgraph_sym *n1 = (const struct cgraph_sym *) p1;
1183 const_tree name = (const_tree) p2;
1184 return (decl_assembler_name_equal (n1->rep_decl, name));
1187 /* Return the cgraph_sym for function declaration DECL. */
1189 static struct cgraph_sym **
1190 cgraph_sym (tree decl)
1192 struct cgraph_sym **slot;
1193 tree name;
1195 if (!cgraph_symtab)
1197 gcc_assert (!global_link_performed);
1198 return NULL;
1201 name = DECL_ASSEMBLER_NAME (decl);
1202 slot = (struct cgraph_sym **)
1203 htab_find_slot_with_hash (cgraph_symtab, name,
1204 decl_assembler_name_hash (name),
1205 NO_INSERT);
1206 return slot;
1209 /* Return the representative declaration for assembler name
1210 ASM_NAME. */
1212 tree
1213 cgraph_find_decl (tree asm_name)
1215 struct cgraph_sym **slot;
1216 if (!L_IPO_COMP_MODE)
1217 return NULL;
1218 if (!cgraph_symtab || !global_link_performed)
1219 return NULL;
1221 slot = (struct cgraph_sym **)
1222 htab_find_slot_with_hash (cgraph_symtab, asm_name,
1223 decl_assembler_name_hash (asm_name),
1224 NO_INSERT);
1225 if (!slot || !*slot)
1226 return NULL;
1228 return (*slot)->rep_node->symbol.decl;
1231 /* Return true if function declaration DECL is originally file scope
1232 static, which is promoted to global scope. */
1234 bool
1235 cgraph_is_promoted_static_func (tree decl)
1237 struct cgraph_sym ** sym;
1238 gcc_assert (L_IPO_COMP_MODE);
1240 /* cgraph_symtab will be created when any symbol got
1241 promoted. */
1242 if (!cgraph_symtab)
1243 return false;
1245 sym = cgraph_sym (decl);
1246 if (!sym)
1247 return false;
1248 return (*sym)->is_promoted_static;
1251 /* Hash function for module information table. ENT
1252 is a pointer to a cgraph_module_info. */
1254 static hashval_t
1255 htab_sym_hash (const void *ent)
1257 const struct cgraph_mod_info * const mi
1258 = (const struct cgraph_mod_info * const ) ent;
1259 return (hashval_t) mi->module_id;
1262 /* Hash equality function for module information table. */
1264 static int
1265 htab_sym_eq (const void *ent1, const void *ent2)
1267 const struct cgraph_mod_info * const mi1
1268 = (const struct cgraph_mod_info * const ) ent1;
1269 const struct cgraph_mod_info * const mi2
1270 = (const struct cgraph_mod_info * const ) ent2;
1271 return (mi1->module_id == mi2->module_id);
1274 /* cgraph_sym SYM may be defined in more than one source modules.
1275 Add declaration DECL's definiting module to SYM. */
1277 static void
1278 add_define_module (struct cgraph_sym *sym, tree decl)
1280 unsigned module_id;
1281 struct cgraph_mod_info **slot;
1282 struct cgraph_mod_info mi;
1284 struct function *f = DECL_STRUCT_FUNCTION (decl);
1285 if (!f)
1286 return;
1287 module_id = FUNC_DECL_MODULE_ID (f);
1289 if (!sym->def_module_hash)
1290 sym->def_module_hash
1291 = htab_create_ggc (10, htab_sym_hash, htab_sym_eq, NULL);
1293 mi.module_id = module_id;
1294 slot = (struct cgraph_mod_info **)htab_find_slot (sym->def_module_hash,
1295 &mi, INSERT);
1296 if (!*slot)
1298 *slot = ggc_alloc_cleared_cgraph_mod_info ();
1299 (*slot)->module_id = module_id;
1301 else
1302 gcc_assert ((*slot)->module_id == module_id);
1305 static int
1306 add_def_module (void **slot, void *data)
1308 struct cgraph_mod_info **m = (struct cgraph_mod_info **)slot;
1309 htab_t mod_set = (htab_t) data;
1310 struct cgraph_mod_info **new_slot;
1312 new_slot = (struct cgraph_mod_info **)htab_find_slot (mod_set, *m, INSERT);
1313 if (!*new_slot)
1315 *new_slot = ggc_alloc_cleared_cgraph_mod_info ();
1316 (*new_slot)->module_id = (*m)->module_id;
1318 else
1319 gcc_assert ((*new_slot)->module_id == (*m)->module_id);
1320 return 1;
1323 /* Clone defined module hash table from ORIG to CLONE. */
1325 void
1326 copy_defined_module_set (tree clone, tree orig)
1328 struct cgraph_sym **orig_sym, **clone_sym;
1330 orig_sym = cgraph_sym (orig);
1331 clone_sym = cgraph_sym (clone);
1332 if (!orig_sym || !(*orig_sym)->def_module_hash)
1333 return;
1334 if (!(*clone_sym)->def_module_hash)
1335 (*clone_sym)->def_module_hash
1336 = htab_create_ggc (10, htab_sym_hash, htab_sym_eq, NULL);
1337 htab_traverse ((*orig_sym)->def_module_hash, add_def_module, (*clone_sym)->def_module_hash);
1340 /* Return true if the symbol associated with DECL is defined in module
1341 MODULE_ID. This interface is used by the inliner to make sure profile-gen
1342 and profile-use pass (L-IPO mode) make consistent inline decision. */
1344 bool
1345 cgraph_is_inline_body_available_in_module (tree decl, unsigned module_id)
1347 struct cgraph_sym **sym;
1348 void **slot;
1349 struct cgraph_mod_info mi;
1351 gcc_assert (L_IPO_COMP_MODE);
1353 if (DECL_BUILT_IN (decl))
1354 return true;
1356 /* TODO: revisit this. */
1357 if (DECL_IN_SYSTEM_HEADER (decl) && DECL_DECLARED_INLINE_P (decl))
1358 return true;
1360 gcc_assert (TREE_STATIC (decl) || DECL_DECLARED_INLINE_P (decl));
1362 if (cgraph_get_module_id (decl) == module_id)
1363 return true;
1365 sym = cgraph_sym (decl);
1366 if (!sym || !(*sym)->def_module_hash)
1367 return false;
1369 mi.module_id = module_id;
1370 slot = htab_find_slot ((*sym)->def_module_hash, &mi, NO_INSERT);
1371 if (slot)
1373 gcc_assert (((struct cgraph_mod_info*)*slot)->module_id == module_id);
1374 return true;
1376 return false;
1379 /* Return the linked cgraph node using DECL's assembler name. DO_ASSERT
1380 is a flag indicating that a non null link target must be returned. */
1382 struct cgraph_node *
1383 cgraph_lipo_get_resolved_node_1 (tree decl, bool do_assert)
1385 struct cgraph_sym **slot;
1387 /* Handle alias decl. */
1388 slot = cgraph_sym (decl);
1390 if (!slot || !*slot)
1392 if (!do_assert)
1393 return NULL;
1394 else
1396 /* Nodes that are indirectly called are not 'reachable' in
1397 the callgraph. If they are not needed (comdat, inline
1398 extern etc), they may be removed from the link table
1399 before direct calls to them are exposed (via indirect
1400 call promtion by const folding etc). When this happens,
1401 the node will need to be relinked. A probably better fix
1402 is to modify the callgraph so that they are not eliminated
1403 in the first place -- this will allow inlining to happen. */
1405 struct cgraph_node *n = cgraph_get_create_node (decl);
1406 if (!n->analyzed)
1408 gcc_assert (DECL_EXTERNAL (decl)
1409 || cgraph_is_aux_decl_external (n)
1410 || DECL_VIRTUAL_P (decl));
1411 gcc_assert (/* This is the case for explicit extern instantiation,
1412 when cgraph node is not created before link. */
1413 DECL_EXTERNAL (decl));
1414 cgraph_link_node (n);
1415 return n;
1417 else
1418 gcc_unreachable ();
1421 else
1423 struct cgraph_sym *sym = *slot;
1424 return sym->rep_node;
1428 /* Return the cgraph_node of DECL if decl has definition; otherwise return
1429 the cgraph node of the representative decl, which is the declaration DECL
1430 is resolved to after linking/symbol resolution. */
1432 struct cgraph_node *
1433 cgraph_lipo_get_resolved_node (tree decl)
1435 struct cgraph_node *node = NULL;
1437 gcc_assert (L_IPO_COMP_MODE && global_link_performed);
1438 gcc_assert (cgraph_symtab);
1440 /* Never merged. */
1441 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl)
1442 /* builtin function decls are shared across modules, but 'linking'
1443 is still performed for them to keep track of the set of defining
1444 modules. Skip the real resolution here to avoid merging '__builtin_xxx'
1445 with 'xxx'. */
1446 || DECL_BUILT_IN (decl))
1447 return cgraph_get_create_node (decl);
1449 node = cgraph_lipo_get_resolved_node_1 (decl, true);
1450 return node;
1453 /* When NODE->decl is dead function eliminated,
1454 remove the entry in the link table. */
1456 void
1457 cgraph_remove_link_node (struct cgraph_node *node)
1459 tree name, decl;
1461 if (!L_IPO_COMP_MODE || !cgraph_symtab)
1462 return;
1464 decl = node->symbol.decl;
1466 /* Skip nodes that are not in the link table. */
1467 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
1468 return;
1470 /* Skip if node is an inline clone or if the node has
1471 defintion that is not really resolved to the merged node. */
1472 if (cgraph_lipo_get_resolved_node_1 (decl, false) != node)
1473 return;
1475 name = DECL_ASSEMBLER_NAME (decl);
1476 htab_remove_elt_with_hash (cgraph_symtab, name,
1477 decl_assembler_name_hash (name));
1480 /* Return true if the function body for DECL has profile information. */
1482 static bool
1483 has_profile_info (tree decl)
1485 gcov_type *ctrs = NULL;
1486 unsigned n;
1487 struct function* f = DECL_STRUCT_FUNCTION (decl);
1489 ctrs = get_coverage_counts_no_warn (f, GCOV_COUNTER_ARCS, &n);
1490 if (ctrs)
1492 unsigned i;
1493 for (i = 0; i < n; i++)
1494 if (ctrs[i])
1495 return true;
1498 return false;
1501 /* Resolve delaration NODE->decl for function symbol *SLOT. */
1503 static void
1504 resolve_cgraph_node (struct cgraph_sym **slot, struct cgraph_node *node)
1506 tree decl1, decl2;
1507 int decl1_defined = 0;
1508 int decl2_defined = 0;
1510 decl1 = (*slot)->rep_decl;
1511 decl2 = node->symbol.decl;
1513 decl1_defined = gimple_has_body_p (decl1);
1514 decl2_defined = gimple_has_body_p (decl2);
1516 if (decl1_defined && !decl2_defined)
1517 return;
1519 if (!decl1_defined && decl2_defined)
1521 (*slot)->rep_node = node;
1522 (*slot)->rep_decl = decl2;
1523 add_define_module (*slot, decl2);
1524 return;
1527 if (decl2_defined)
1529 bool has_prof1 = false;
1530 bool has_prof2 = false;
1531 gcc_assert (decl1_defined);
1532 add_define_module (*slot, decl2);
1534 has_prof1 = has_profile_info (decl1);
1535 if (has_prof1)
1536 return;
1537 has_prof2 = has_profile_info (decl2);
1538 if (has_prof2)
1540 (*slot)->rep_node = node;
1541 (*slot)->rep_decl = decl2;
1543 return;
1545 return;
1549 /* Resolve NODE->decl in the function symbol table. */
1551 struct cgraph_sym *
1552 cgraph_link_node (struct cgraph_node *node)
1554 void **slot;
1555 tree name;
1557 if (!L_IPO_COMP_MODE)
1558 return NULL;
1560 if (!cgraph_symtab)
1561 return NULL;
1563 /* Skip the cases when the defintion can be locally resolved, and
1564 when we do not need to keep track of defining modules. */
1565 if (!TREE_PUBLIC (node->symbol.decl) || DECL_ARTIFICIAL (node->symbol.decl))
1566 return NULL;
1568 name = DECL_ASSEMBLER_NAME (node->symbol.decl);
1569 slot = htab_find_slot_with_hash (cgraph_symtab, name,
1570 decl_assembler_name_hash (name),
1571 INSERT);
1572 if (*slot)
1573 resolve_cgraph_node ((struct cgraph_sym **) slot, node);
1574 else
1576 struct cgraph_sym *sym = ggc_alloc_cleared_cgraph_sym ();
1577 sym->rep_node = node;
1578 sym->rep_decl = node->symbol.decl;
1579 sym->assembler_name = name;
1580 add_define_module (sym, node->symbol.decl);
1581 *slot = sym;
1583 return (struct cgraph_sym *) *slot;
1586 /* Perform cross module linking of function declarations. */
1588 void
1589 cgraph_do_link (void)
1591 struct cgraph_node *node;
1593 if (!L_IPO_COMP_MODE)
1594 return;
1596 global_link_performed = 1;
1597 gcc_assert (cgraph_pre_profiling_inlining_done);
1599 if (!cgraph_symtab)
1600 cgraph_symtab
1601 = htab_create_ggc (10, hash_sym_by_assembler_name,
1602 eq_assembler_name, NULL);
1604 FOR_EACH_FUNCTION (node)
1606 gcc_assert (!node->global.inlined_to);
1607 cgraph_link_node (node);
1611 struct promo_ent
1613 char* assemb_name;
1614 int seq;
1617 /* Hash function for promo_ent table. */
1619 static hashval_t
1620 promo_ent_hash (const void *ent)
1622 const struct promo_ent *const entry
1623 = (const struct promo_ent *) ent;
1625 return htab_hash_string (entry->assemb_name);
1628 /* Hash_eq function for promo_ent table. */
1630 static int
1631 promo_ent_eq (const void *ent1, const void *ent2)
1633 const struct promo_ent *const entry1
1634 = (const struct promo_ent *) ent1;
1635 const struct promo_ent *const entry2
1636 = (const struct promo_ent *) ent2;
1637 if (!strcmp (entry1->assemb_name, entry2->assemb_name))
1638 return 1;
1639 return 0;
1642 /* Delete function for promo_ent hash table. */
1644 static void
1645 promo_ent_del (void *ent)
1647 struct promo_ent *const entry
1648 = (struct promo_ent *) ent;
1650 free (entry->assemb_name);
1651 free (entry);
1654 static htab_t promo_ent_hash_tab = NULL;
1656 /* Return a unique sequence number for NAME. This is needed to avoid
1657 name conflict -- function scope statics may have identical names.
1659 This function returns a zero sequence number if it is called with
1660 a particular NAME for the first time, and non-zero otherwise.
1661 This fact is used to keep track of unseen weak variables. */
1663 static int
1664 get_name_seq_num (const char *name)
1666 struct promo_ent **slot;
1667 struct promo_ent ent;
1668 ent.assemb_name = xstrdup (name);
1669 ent.seq = 0;
1671 slot = (struct promo_ent **)
1672 htab_find_slot (promo_ent_hash_tab, &ent, INSERT);
1674 if (!*slot)
1676 *slot = XCNEW (struct promo_ent);
1677 (*slot)->assemb_name = ent.assemb_name;
1679 else
1681 (*slot)->seq++;
1682 free (ent.assemb_name);
1684 return (*slot)->seq;
1687 /* Returns a unique assembler name for DECL. */
1689 static tree
1690 create_unique_name (tree decl, unsigned module_id)
1692 tree id, assemb_id;
1693 char *assembler_name;
1694 const char *name;
1695 struct function *context = NULL;
1696 int seq = 0;
1698 if (TREE_CODE (decl) == FUNCTION_DECL)
1700 if (!DECL_CONTEXT (decl)
1701 || TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
1703 id = DECL_NAME (decl);
1704 /* if (IDENTIFIER_OPNAME_P (id)) */
1705 if (TREE_LANG_FLAG_2 (id))
1706 id = DECL_ASSEMBLER_NAME (decl);
1708 else
1709 id = DECL_ASSEMBLER_NAME (decl);
1711 else
1713 if (!DECL_CONTEXT (decl))
1714 id = DECL_NAME (decl);
1715 else if (TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
1716 id = DECL_ASSEMBLER_NAME (decl);
1717 else if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
1719 id = DECL_NAME (decl);
1720 context = DECL_STRUCT_FUNCTION (DECL_CONTEXT (decl));
1722 else
1723 /* file scope context */
1724 id = DECL_NAME (decl);
1727 name = IDENTIFIER_POINTER (id);
1728 if (context)
1730 char *n;
1731 unsigned fno = FUNC_DECL_FUNC_ID (context);
1732 n = (char *)alloca (strlen (name) + 15);
1733 sprintf (n, "%s.%u", name, fno);
1734 name = n;
1737 assembler_name = (char*) alloca (strlen (name) + 30);
1738 sprintf (assembler_name, "%s.cmo.%u", name, module_id);
1739 seq = get_name_seq_num (assembler_name);
1740 if (seq)
1741 sprintf (assembler_name, "%s.%d", assembler_name, seq);
1743 assemb_id = get_identifier (assembler_name);
1745 return assemb_id;
1748 /* Promote DECL to be global. MODULE_ID is the id of the module where
1749 DECL is defined. IS_EXTERN is a flag indicating if externalization
1750 is needed. */
1752 static void
1753 promote_static_var_func (unsigned module_id, tree decl, bool is_extern)
1755 tree assemb_id;
1756 tree alias;
1758 /* No need to promote symbol alias. */
1759 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1760 if (alias)
1761 return;
1763 /* Function decls in C++ may contain characters not taken by assembler.
1764 Similarly, function scope static variable has UID as the assembler name
1765 suffix which is not consistent across modules. */
1766 assemb_id = create_unique_name (decl, module_id);
1768 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1770 if (TREE_CODE (decl) == FUNCTION_DECL)
1771 unlink_from_assembler_name_hash ((symtab_node) cgraph_get_create_node (decl));
1772 else
1773 unlink_from_assembler_name_hash ((symtab_node) varpool_get_node (decl));
1776 SET_DECL_ASSEMBLER_NAME (decl, assemb_id);
1777 TREE_PUBLIC (decl) = 1;
1778 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
1779 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1781 if (TREE_CODE (decl) == FUNCTION_DECL)
1783 struct cgraph_node *node = cgraph_get_create_node (decl);
1785 node->symbol.resolution = LDPR_UNKNOWN;
1786 insert_to_assembler_name_hash ((symtab_node) node);
1788 else
1790 struct varpool_node *node = varpool_get_node (decl);
1791 node->symbol.resolution = LDPR_UNKNOWN;
1792 /* Statics from exported primary module are very likely
1793 referenced by other modules, so they should be made
1794 externally visible (to be avoided to be localized again).
1795 Another way to do this is to set force_output bit or
1796 change the logic in varpool_externally_visible in ipa.c. */
1797 if (!is_extern)
1799 node->symbol.resolution = LDPR_PREVAILING_DEF;
1800 node->symbol.externally_visible = true;
1802 varpool_link_node (node);
1803 insert_to_assembler_name_hash ((symtab_node) node);
1806 if (is_extern)
1808 if (TREE_CODE (decl) == VAR_DECL)
1810 TREE_STATIC (decl) = 0;
1811 DECL_EXTERNAL (decl) = 1;
1812 /* Keep the initializer to allow const prop. */
1813 /* DECL_INITIAL (decl) = 0; */
1814 DECL_CONTEXT (decl) = 0;
1816 /* else
1817 Function body will be deleted later before expansion. */
1819 else
1820 TREE_STATIC (decl) = 1;
1823 /* Externalize global variables from aux modules and promote
1824 static variables.
1825 WEAK variables are treated especially in
1826 varpool_remove_duplicate_weak_decls. */
1828 static void
1829 process_module_scope_static_var (struct varpool_node *vnode)
1831 tree decl = vnode->symbol.decl;
1833 if (varpool_is_auxiliary (vnode))
1835 gcc_assert (vnode->module_id != primary_module_id);
1836 if (TREE_PUBLIC (decl))
1838 /* Externalize non-weak variables. */
1839 if (!DECL_WEAK (decl))
1841 DECL_EXTERNAL (decl) = 1;
1842 TREE_STATIC (decl) = 0;
1843 /* Keep the initializer to allow const prop. */
1844 /* DECL_INITIAL (decl) = NULL; */
1845 if (DECL_CONTEXT (decl))
1847 DECL_ASSEMBLER_NAME (decl);
1849 DECL_CONTEXT (decl) = NULL;
1852 else
1854 /* Promote static vars to global. */
1855 if (vnode->module_id)
1856 promote_static_var_func (vnode->module_id, decl,
1857 varpool_is_auxiliary (vnode));
1860 else
1862 if (PRIMARY_MODULE_EXPORTED && !TREE_PUBLIC (decl))
1863 promote_static_var_func (vnode->module_id, decl,
1864 varpool_is_auxiliary (vnode));
1868 /* Promote all aliases of CNODE. */
1870 static void
1871 promote_function_aliases (struct cgraph_node *cnode, unsigned mod_id,
1872 bool is_extern)
1874 int i;
1875 struct ipa_ref *ref;
1877 for (i = 0; ipa_ref_list_referring_iterate (&cnode->symbol.ref_list, i, ref);
1878 i++)
1880 if (ref->use == IPA_REF_ALIAS)
1882 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1883 tree alias_decl = alias->symbol.decl;
1884 /* Should assert */
1885 if (cgraph_get_module_id (alias_decl) == mod_id)
1886 promote_static_var_func (mod_id, alias_decl, is_extern);
1891 /* Promote static function CNODE->decl to be global. */
1893 static void
1894 process_module_scope_static_func (struct cgraph_node *cnode)
1896 tree decl = cnode->symbol.decl;
1897 bool addr_taken;
1898 unsigned mod_id;
1899 struct ipa_ref *ref;
1900 int i;
1902 if (TREE_PUBLIC (decl)
1903 || !TREE_STATIC (decl)
1904 || DECL_EXTERNAL (decl)
1905 || DECL_ARTIFICIAL (decl))
1906 return;
1908 if (flag_ripa_no_promote_always_inline
1909 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)) != NULL)
1910 return;
1912 /* Can be local -- the promotion pass need to be done after
1913 callgraph build when address taken bit is set. */
1914 addr_taken = cnode->symbol.address_taken;
1915 if (!addr_taken)
1917 for (i = 0; ipa_ref_list_referring_iterate (&cnode->symbol.ref_list, i, ref);
1918 i++)
1919 if (ref->use == IPA_REF_ALIAS)
1921 struct cgraph_node *alias = ipa_ref_referring_node (ref);
1922 if (alias->symbol.address_taken)
1923 addr_taken = true;
1926 if (!addr_taken)
1928 tree assemb_id = create_unique_name (decl, cgraph_get_module_id (decl));
1930 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1931 unlink_from_assembler_name_hash ((symtab_node) cnode);
1932 SET_DECL_ASSEMBLER_NAME (decl, assemb_id);
1933 insert_to_assembler_name_hash ((symtab_node) cnode);
1934 return;
1937 mod_id = cgraph_get_module_id (decl);
1938 if (cgraph_is_auxiliary (decl))
1940 gcc_assert (mod_id != primary_module_id);
1941 /* Promote static function to global. */
1942 if (mod_id)
1944 promote_static_var_func (mod_id, decl, 1);
1945 promote_function_aliases (cnode, mod_id, 1);
1948 else
1950 if (PRIMARY_MODULE_EXPORTED
1951 /* skip static_init routines. */
1952 && !DECL_ARTIFICIAL (decl))
1954 promote_static_var_func (mod_id, decl, 0);
1956 promote_function_aliases (cnode, mod_id, 0);
1961 /* Process var_decls, func_decls with static storage. */
1963 void
1964 cgraph_process_module_scope_statics (void)
1966 struct cgraph_node *pf;
1967 struct varpool_node *pv;
1969 if (!L_IPO_COMP_MODE)
1970 return;
1972 promo_ent_hash_tab = htab_create (10, promo_ent_hash,
1973 promo_ent_eq, promo_ent_del);
1975 /* Process variable first. */
1976 FOR_EACH_DEFINED_VARIABLE (pv)
1977 process_module_scope_static_var (pv);
1979 FOR_EACH_FUNCTION (pf)
1980 process_module_scope_static_func (pf);
1982 htab_delete (promo_ent_hash_tab);
1985 /* There could be duplicate non-extern WEAK decls in the varpool queue,
1986 coming from different modules. All but one of these need to be externalized
1987 and removed from the varpool queue.
1988 Duplicate WEAK decls can be added to varpool queue as late as
1989 cgraph_expand_function, when a WEAK decl is marked referenced as assembler
1990 is being output. Therefore, a call to this function should be made after
1991 cgraph_expand_function. */
1993 void
1994 varpool_remove_duplicate_weak_decls (void)
1996 struct varpool_node *node = NULL;
1998 if (!L_IPO_COMP_MODE)
1999 return;
2001 promo_ent_hash_tab = htab_create (10, promo_ent_hash,
2002 promo_ent_eq, promo_ent_del);
2004 FOR_EACH_VARIABLE (node)
2006 tree decl = node->symbol.decl;
2008 if (TREE_PUBLIC (decl) && DECL_WEAK (decl) && !DECL_EXTERNAL (decl)
2009 && get_name_seq_num (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))))
2011 DECL_EXTERNAL (decl) = 1;
2012 TREE_STATIC (decl) = 0;
2013 DECL_INITIAL (decl) = NULL;
2014 DECL_CONTEXT (decl) = NULL;
2018 htab_delete (promo_ent_hash_tab);
2021 static GTY((param_is (struct varpool_node))) htab_t varpool_symtab;
2023 /* Hash function for varpool node. */
2025 static hashval_t
2026 hash_node_by_assembler_name (const void *p)
2028 const struct varpool_node *n = (const struct varpool_node *) p;
2029 return (hashval_t) decl_assembler_name_hash (
2030 DECL_ASSEMBLER_NAME (n->symbol.decl));
2033 /* Returns nonzero if P1 and P2 are equal. */
2035 static int
2036 eq_node_assembler_name (const void *p1, const void *p2)
2038 const struct varpool_node *n1 = (const struct varpool_node *) p1;
2039 const_tree name = (const_tree)p2;
2040 return (decl_assembler_name_equal (n1->symbol.decl, name));
2043 /* Return true if NODE's decl is declared in an auxiliary module. */
2045 bool
2046 varpool_is_auxiliary (struct varpool_node *node)
2048 return (node->module_id
2049 && node->module_id != primary_module_id);
2052 /* Return the varpool_node to which DECL is resolved to during linking.
2053 This method can not be used after static to global promotion happens. */
2055 static struct varpool_node *
2056 real_varpool_node_1 (tree decl, bool assert)
2058 void **slot;
2059 tree name;
2061 if (!L_IPO_COMP_MODE || !varpool_symtab)
2062 return varpool_get_node (decl);
2064 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
2065 return varpool_get_node (decl);
2067 name = DECL_ASSEMBLER_NAME (decl);
2068 slot = htab_find_slot_with_hash (varpool_symtab, name,
2069 decl_assembler_name_hash (name),
2070 NO_INSERT);
2071 if (!slot)
2073 gcc_assert (!assert);
2074 return NULL;
2077 gcc_assert (slot && *slot);
2078 return (struct varpool_node *)*slot;
2081 struct varpool_node *
2082 real_varpool_node (tree decl)
2084 return real_varpool_node_1 (decl, true);
2087 /* Remove NODE from the link table. */
2089 void
2090 varpool_remove_link_node (struct varpool_node *node)
2092 tree name;
2093 tree decl;
2095 if (!L_IPO_COMP_MODE || !varpool_symtab)
2096 return;
2098 decl = node->symbol.decl;
2100 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
2101 return;
2103 if (real_varpool_node_1 (decl, false) != node)
2104 return;
2106 name = DECL_ASSEMBLER_NAME (decl);
2107 htab_remove_elt_with_hash (varpool_symtab, name,
2108 decl_assembler_name_hash (name));
2111 /* Merge the addressable attribute from DECL2 to DECL1. */
2113 static inline void
2114 merge_addressable_attr (tree decl1, tree decl2)
2116 if (TREE_ADDRESSABLE (decl2))
2117 TREE_ADDRESSABLE (decl1) = 1;
2120 /* Resolve NODE->decl to symbol table entry *SLOT. */
2122 static void
2123 resolve_varpool_node (struct varpool_node **slot, struct varpool_node *node)
2125 tree decl1, decl2;
2127 decl1 = (*slot)->symbol.decl;
2128 decl2 = node->symbol.decl;
2130 /* Take the decl with the complete type. */
2131 if (COMPLETE_TYPE_P (TREE_TYPE (decl1))
2132 && !COMPLETE_TYPE_P (TREE_TYPE (decl2)))
2134 merge_addressable_attr (decl1, decl2);
2135 return;
2137 if (!COMPLETE_TYPE_P (TREE_TYPE (decl1))
2138 && COMPLETE_TYPE_P (TREE_TYPE (decl2)))
2140 *slot = node;
2141 merge_addressable_attr (decl2, decl1);
2142 return;
2145 /* Either all complete or neither's type is complete. Just
2146 pick the primary module's decl. */
2147 if (!varpool_is_auxiliary (*slot))
2149 merge_addressable_attr (decl1, decl2);
2150 return;
2153 if (!varpool_is_auxiliary (node))
2155 *slot = node;
2156 merge_addressable_attr (decl2, decl1);
2157 return;
2160 merge_addressable_attr (decl1, decl2);
2161 return;
2164 /* Link NODE into var_decl symbol table. */
2166 void
2167 varpool_link_node (struct varpool_node *node)
2169 tree name;
2170 void **slot;
2172 if (!L_IPO_COMP_MODE || !varpool_symtab)
2173 return;
2175 if (!TREE_PUBLIC (node->symbol.decl) || DECL_ARTIFICIAL (node->symbol.decl))
2176 return;
2178 name = DECL_ASSEMBLER_NAME (node->symbol.decl);
2179 slot = htab_find_slot_with_hash (varpool_symtab, name,
2180 decl_assembler_name_hash (name),
2181 INSERT);
2182 if (*slot)
2183 resolve_varpool_node ((struct varpool_node **) slot, node);
2184 else
2185 *slot = node;
2188 /* Fixup references of VNODE. */
2190 static void
2191 fixup_reference_list (struct varpool_node *node)
2193 int i;
2194 struct ipa_ref *ref;
2195 struct ipa_ref_list *list = &node->symbol.ref_list;
2196 vec<cgraph_node_ptr> new_refered;
2197 vec<int> new_refered_type;
2198 struct cgraph_node *c;
2199 enum ipa_ref_use use_type = IPA_REF_LOAD;
2201 new_refered.create (10);
2202 new_refered_type.create (10);
2203 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
2205 if (!is_a <cgraph_node> (ref->referred))
2206 continue;
2208 struct cgraph_node *cnode = ipa_ref_node (ref);
2209 struct cgraph_node *r_cnode
2210 = cgraph_lipo_get_resolved_node (cnode->symbol.decl);
2211 if (r_cnode != cnode)
2213 new_refered.safe_push (r_cnode);
2214 use_type = ref->use;
2215 new_refered_type.safe_push ((int) use_type);
2218 for (i = 0; new_refered.iterate (i, &c); ++i)
2220 ipa_record_reference ((symtab_node)node, (symtab_node)c,
2221 (enum ipa_ref_use) new_refered_type[i], NULL);
2225 /* Perform cross module linking for var_decls. */
2227 void
2228 varpool_do_link (void)
2230 struct varpool_node *node;
2232 if (!L_IPO_COMP_MODE)
2233 return;
2235 varpool_symtab
2236 = htab_create_ggc (10, hash_node_by_assembler_name,
2237 eq_node_assembler_name, NULL);
2238 FOR_EACH_VARIABLE (node)
2239 varpool_link_node (node);
2241 /* Merge the externally visible attribute. */
2242 FOR_EACH_VARIABLE (node)
2244 if (node->symbol.externally_visible)
2245 (real_varpool_node (node->symbol.decl))->symbol.externally_visible = true;
2246 fixup_reference_list (node);
2250 /* Get the list of assembler name ids with reference bit set. */
2252 void
2253 varpool_get_referenced_asm_ids (vec<tree,va_gc> **ids)
2255 struct varpool_node *node;
2256 FOR_EACH_VARIABLE (node)
2258 tree asm_id = NULL;
2259 tree decl = node->symbol.decl;
2260 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2262 asm_id = DECL_ASSEMBLER_NAME (decl);
2263 vec_safe_push (*ids, asm_id);
2268 /* Clear the referenced bit in all assembler ids. */
2270 void
2271 varpool_clear_asm_id_reference_bit (void)
2273 struct varpool_node *node;
2274 FOR_EACH_VARIABLE (node)
2276 tree asm_id = NULL;
2277 tree decl = node->symbol.decl;
2278 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2280 asm_id = DECL_ASSEMBLER_NAME (decl);
2281 TREE_SYMBOL_REFERENCED (asm_id) = 0;
2287 #include "gt-l-ipo.h"