Fix a bug where weak decls could be multiply emitted, and another which caused bogus...
[official-gcc.git] / gcc / l-ipo.c
blobacfba1379a7440f32f8c95a2ad839dd6aee6aeb7
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-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"
37 struct GTY(()) saved_module_scope
39 VEC(tree, gc) *module_decls;
40 unsigned module_id;
43 static GTY (()) struct saved_module_scope *current_module_scope;
44 static GTY ((param_is (struct saved_module_scope))) htab_t saved_module_scope_map;
45 static int primary_module_last_fundef_no = 0;
46 static location_t primary_module_last_loc;
47 /* Primary module pending templates. */
48 /* Referenced asm ids in primary module. */
49 static GTY (()) VEC(tree, gc) *referenced_asm_ids = NULL;
50 bool parser_parsing_start = false;
51 /* Nonzero if we're done parsing and into end-of-file activities. */
52 int at_eof;
54 static int aggr_has_equiv_id (tree t1, tree t2);
56 /* Module scope hash function. */
58 static hashval_t
59 htab_module_scope_hash (const void *ent)
61 const struct saved_module_scope *const entry
62 = (const struct saved_module_scope *) ent;
63 return (hashval_t) entry->module_id;
66 /* Module scope equality function. */
68 static int
69 htab_module_scope_eq (const void *ent1, const void *ent2)
71 const struct saved_module_scope *const entry1
72 = (const struct saved_module_scope *) ent1;
73 const struct saved_module_scope *const entry2
74 = (const struct saved_module_scope *) ent2;
76 return entry1->module_id == entry2->module_id;
79 /* Returns the module scope given a module id MOD_ID. */
81 static struct saved_module_scope *
82 get_module_scope (unsigned mod_id)
84 struct saved_module_scope **slot, key, *module_scope;
86 gcc_assert (mod_id);
88 if (saved_module_scope_map == NULL)
89 saved_module_scope_map = htab_create_ggc (10, htab_module_scope_hash,
90 htab_module_scope_eq, NULL);
91 key.module_id = mod_id;
92 slot = (struct saved_module_scope **)
93 htab_find_slot (saved_module_scope_map, &key, INSERT);
94 module_scope = *slot;
95 if (!module_scope)
97 module_scope = GGC_CNEW (struct saved_module_scope);
98 module_scope->module_id = mod_id;
99 *slot = module_scope;
101 return module_scope;
104 /* Allocate memory for struct lang_decl for tree T. */
106 static struct lang_decl *
107 alloc_lang_decl (tree t)
109 size_t size;
110 size = lang_hooks.l_ipo.get_lang_decl_size (t);
111 return GGC_CNEWVAR (struct lang_decl, size);
114 /* Return a cloned copy of tree SRC. */
116 tree
117 lipo_save_decl (tree src)
119 tree saved = copy_node (src);
120 enum tree_code tc = TREE_CODE (src);
121 if (TREE_CODE_CLASS (tc) == tcc_declaration)
123 struct lang_decl *ls = NULL;
124 struct function *func = NULL;
125 DECL_CONTEXT (saved) = DECL_CONTEXT (src);
126 if (DECL_LANG_SPECIFIC (src))
128 ls = alloc_lang_decl (src);
129 memcpy (ls, DECL_LANG_SPECIFIC (src),
130 lang_hooks.l_ipo.get_lang_decl_size (src));
132 DECL_LANG_SPECIFIC (saved) = ls;
133 if (tc == FUNCTION_DECL && DECL_STRUCT_FUNCTION (src))
135 func = GGC_CNEW (struct function);
136 *func = *(DECL_STRUCT_FUNCTION (src));
137 DECL_STRUCT_FUNCTION (saved) = func;
140 else
142 gcc_assert (TREE_CODE_CLASS (tc) == tcc_type &&
143 TYPE_MAIN_VARIANT (src) == src);
144 TYPE_CONTEXT (saved) = TYPE_CONTEXT (src);
145 lang_hooks.l_ipo.dup_lang_type (src, saved);
148 return saved;
151 /* Copy tree SAVED to tree DEST. */
153 void
154 lipo_restore_decl (tree dest, tree saved)
156 enum tree_code tc;
157 unsigned old_uid;
158 struct lang_decl *oldls;
160 tc = TREE_CODE (saved);
161 if (TREE_CODE_CLASS (tc) == tcc_declaration)
163 struct function *oldfunc = NULL;
164 old_uid = DECL_UID (dest);
165 oldls = DECL_LANG_SPECIFIC (dest);
166 oldfunc
167 = (tc == FUNCTION_DECL ? DECL_STRUCT_FUNCTION (dest) : NULL);
169 memcpy ((char *) dest + sizeof (struct tree_common),
170 (char *) saved + sizeof (struct tree_common),
171 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
173 if (tc == FUNCTION_DECL)
174 memcpy ((char *) dest + sizeof (struct tree_decl_common),
175 (char *) saved + sizeof (struct tree_decl_common),
176 sizeof (struct tree_function_decl)
177 - sizeof (struct tree_decl_common));
179 DECL_UID (dest) = old_uid;
180 if (DECL_LANG_SPECIFIC (saved))
182 if (!oldls)
183 oldls = alloc_lang_decl (dest);
184 memcpy (oldls, DECL_LANG_SPECIFIC (saved),
185 lang_hooks.l_ipo.get_lang_decl_size (saved));
186 DECL_LANG_SPECIFIC (dest) = oldls;
188 else
189 DECL_LANG_SPECIFIC (dest) = NULL;
191 if (tc == FUNCTION_DECL)
193 if (DECL_STRUCT_FUNCTION (saved))
195 if (!oldfunc)
196 oldfunc = GGC_CNEW (struct function);
197 *oldfunc = *(DECL_STRUCT_FUNCTION (saved));
198 DECL_STRUCT_FUNCTION (dest) = oldfunc;
200 else
201 DECL_STRUCT_FUNCTION (dest) = NULL;
204 else
206 gcc_assert (TREE_CODE_CLASS (tc) == tcc_type);
207 lang_hooks.l_ipo.copy_lang_type (saved, dest);
212 /* Return the name for tree TD which is either a decl or type. */
214 tree
215 get_type_or_decl_name (tree td)
217 tree id;
219 if (DECL_P (td))
220 id = DECL_NAME (td);
221 else
223 id = TYPE_NAME (td);
224 if (DECL_P (id))
225 id = DECL_NAME (id);
227 return id;
230 /* For a DECL (a type or a decl) in SCOPE, check to see if it is in
231 global or namespace scope. If yes, add it to the current module scope. */
233 void
234 add_decl_to_current_module_scope (tree decl, void *scope)
236 struct saved_module_scope *module_scope;
237 tree id;
239 if (!flag_dyn_ipa)
240 return;
242 if (!parser_parsing_start)
244 /* The source file may contains only global variable declations
245 -- there is no module grouping data associated with it, so
246 neither primary_module_id nor current_module_id is set. */
247 lang_hooks.l_ipo.add_built_in_decl (decl);
248 return;
251 if (!L_IPO_COMP_MODE)
252 return;
254 if (!lang_hooks.l_ipo.has_global_name (decl, scope))
255 return;
257 /* Unlike C++ where names are attached to type decls, for C, the type name
258 is identifier node. Thus we need to track type names as well. */
259 id = get_type_or_decl_name (decl);
260 if (!id)
261 return;
263 module_scope = current_module_scope;
264 gcc_assert (module_scope && module_scope->module_id == current_module_id);
265 VEC_safe_push (tree, gc, module_scope->module_decls, decl);
268 /* Clear name bindings for all decls created in MODULE_SCOPE. */
270 static void
271 clear_module_scope_bindings (struct saved_module_scope *module_scope)
273 size_t i;
274 tree decl;
276 for (i = 0;
277 VEC_iterate (tree, module_scope->module_decls, i, decl);
278 ++i)
279 lang_hooks.l_ipo.clear_global_name_bindings (
280 get_type_or_decl_name (decl));
283 /* The referenced attribute of a decl is not associated with the
284 decl itself but with the assembler name. Remember the referenced
285 bits before clearing them. */
287 static void
288 save_assembler_name_reference_bit (void)
290 varpool_get_referenced_asm_ids (&referenced_asm_ids);
293 /* Clear the reference bits for assembler names before closing the
294 module scope. */
296 static void
297 clear_assembler_name_reference_bit (void)
299 varpool_clear_asm_id_reference_bit ();
302 /* Restore the reference bits for assembler names. */
304 static void
305 restore_assembler_name_reference_bit (void)
307 size_t i;
308 tree nm;
309 for (i = 0;
310 VEC_iterate (tree, referenced_asm_ids, i, nm);
311 ++i)
312 TREE_SYMBOL_REFERENCED (nm) = 1;
315 /* Set up the module scope before the parsing of the
316 associated source file. */
318 void
319 push_module_scope (void)
321 struct saved_module_scope *prev_module_scope;
323 if (!flag_dyn_ipa || !L_IPO_COMP_MODE)
325 parser_parsing_start = true;
326 return;
329 prev_module_scope = current_module_scope;
330 if (L_IPO_IS_PRIMARY_MODULE)
332 gcc_assert (!prev_module_scope);
333 lang_hooks.l_ipo.save_built_in_decl_pre_parsing ();
334 parser_parsing_start = true;
337 gcc_assert (current_module_id);
339 /* Set up the module scope. */
340 current_module_scope = get_module_scope (current_module_id);
341 return;
344 /* Restore the shared decls to their post parsing states. */
346 static void
347 restore_post_parsing_states (void)
349 current_module_id = primary_module_id;
350 current_module_scope = get_module_scope (primary_module_id);
351 set_funcdef_no (primary_module_last_fundef_no);
352 input_location = primary_module_last_loc;
354 restore_assembler_name_reference_bit ();
355 lang_hooks.l_ipo.restore_built_in_decl_post_module_parsing ();
358 /* Pop the current module scope (by clearing name bindings etc.)
359 and prepare for parsing of the next module. In particular,
360 built-in decls need to be restored to the state before file
361 parsing starts. */
363 void
364 pop_module_scope (void)
366 bool is_last = false;
367 if (!flag_dyn_ipa || !L_IPO_COMP_MODE)
368 return;
370 gcc_assert (current_module_id && current_module_scope);
372 if (L_IPO_IS_PRIMARY_MODULE)
373 primary_module_last_loc = input_location;
375 at_eof = 1;
376 lang_hooks.l_ipo.process_pending_decls (input_location);
377 lang_hooks.l_ipo.clear_deferred_fns ();
378 at_eof = 0;
380 is_last = is_last_module (current_module_id);
382 lang_hooks.l_ipo.save_built_in_decl_post_module_parsing ();
383 /* Save primary module state if needed (when module group
384 size > 1) */
385 if (L_IPO_IS_PRIMARY_MODULE && num_in_fnames > 1)
387 save_assembler_name_reference_bit ();
388 primary_module_last_fundef_no = get_current_funcdef_no ();
391 if (!is_last)
393 /* More aux modules are anticipated, clear
394 the parsing state. */
395 gcc_assert (num_in_fnames > 1);
396 clear_assembler_name_reference_bit ();
397 clear_module_scope_bindings (current_module_scope);
398 /* Restore symtab bindings for builtins */
399 lang_hooks.l_ipo.restore_built_in_decl_pre_parsing ();
400 /* The map can not be cleared because the names of operator
401 decls are used to store the information about the conversion
402 target type. This forces the coversion operator ids to be
403 incremented across different modules, and assember id must
404 be used for checksum computation. */
405 /* cp_clear_conv_type_map (); */
407 else if (num_in_fnames > 1)
408 restore_post_parsing_states ();
409 else
410 gcc_assert (L_IPO_IS_PRIMARY_MODULE && num_in_fnames == 1);
414 /* Type merging support for LIPO */
416 struct type_ec
418 tree rep_type;
419 VEC(tree, heap) *eq_types;
422 static VEC(tree, heap) *pending_types = NULL;
423 static struct pointer_set_t *type_set = NULL;
424 static htab_t type_hash_tab = NULL;
426 /* Hash function for the type table. */
428 static hashval_t
429 type_hash_hash (const void *ent)
431 tree type, name;
432 const struct type_ec *const entry
433 = (const struct type_ec *) ent;
435 type = entry->rep_type;
436 name = TYPE_NAME (type);
437 if (DECL_P (name))
438 name = DECL_NAME (name);
440 return htab_hash_string (IDENTIFIER_POINTER (name));
443 /* Equality function for type hash table. */
445 static int
446 type_hash_eq (const void *ent1, const void *ent2)
448 tree type1, type2;
449 const struct type_ec *const entry1
450 = (const struct type_ec *) ent1;
451 const struct type_ec *const entry2
452 = (const struct type_ec *) ent2;
454 type1 = entry1->rep_type;
455 type2 = entry2->rep_type;
457 return aggr_has_equiv_id (type1, type2);
460 /* Function to delete type hash entries. */
462 static void
463 type_hash_del (void *ent)
465 struct type_ec *const entry
466 = (struct type_ec *) ent;
468 VEC_free (tree, heap, entry->eq_types);
469 free (entry);
472 struct GTY(()) type_ent
474 tree type;
475 unsigned eq_id;
478 static GTY ((param_is (struct type_ent))) htab_t l_ipo_type_tab = 0;
479 static unsigned l_ipo_eq_id = 0;
481 /* Address hash function for struct type_ent. */
483 static hashval_t
484 type_addr_hash (const void *ent)
486 const struct type_ent *const entry
487 = (const struct type_ent *) ent;
488 return (hashval_t) (long) entry->type;
491 /* Address equality function for type_ent. */
493 static int
494 type_addr_eq (const void *ent1, const void *ent2)
496 const struct type_ent *const entry1
497 = (const struct type_ent *) ent1;
498 const struct type_ent *const entry2
499 = (const struct type_ent *) ent2;
500 return entry1->type == entry2->type;
503 /* Returns 1 if NS1 and NS2 refer to the same namespace. */
505 static int
506 is_ns_equiv (tree ns1, tree ns2)
508 tree n1, n2;
509 if (ns1 == NULL && ns2 == NULL)
510 return 1;
512 if ((!ns1 && ns2) || (ns1 && !ns2))
513 return 0;
515 gcc_assert (DECL_P (ns1) && DECL_P (ns2));
517 if (!is_ns_equiv (DECL_CONTEXT (ns1),
518 DECL_CONTEXT (ns2)))
519 return 0;
521 n1 = DECL_NAME (ns1);
522 n2 = DECL_NAME (ns2);
523 if (n1 == 0 && n2 == 0)
524 /* Conservative (which can happen when two NSes are from
525 different modules but with same UID) quivalence is allowed. */
526 return DECL_UID (ns1) == DECL_UID (ns2);
527 if (!n1 || !n2)
528 return 0;
530 if (!strcmp (IDENTIFIER_POINTER (n1),
531 IDENTIFIER_POINTER (n2)))
532 return 1;
534 return 0;
537 /* Returns 1 if aggregate type T1 and T2 have equivalent qualified
538 ids. */
540 static int
541 aggr_has_equiv_id (tree t1, tree t2)
543 int ctx_match;
544 tree ctx1, ctx2, tn1, tn2;
545 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
547 ctx1 = TYPE_CONTEXT (t1);
548 ctx2 = TYPE_CONTEXT (t2);
550 if ((ctx1 && !ctx2) || (!ctx1 && ctx2))
551 return 0;
553 if (ctx1 && TREE_CODE (ctx1) != TREE_CODE (ctx2))
554 return 0;
556 if (ctx1 && (TREE_CODE (ctx1) == FUNCTION_DECL
557 || TREE_CODE (ctx1) == BLOCK))
558 return 0;
560 if (!ctx1)
562 ctx_match = 1;
563 gcc_assert (!ctx2);
565 else if (TREE_CODE (ctx1) == NAMESPACE_DECL)
566 ctx_match = is_ns_equiv (ctx1, ctx2);
567 else if (TYPE_P (ctx1))
568 ctx_match = aggr_has_equiv_id (ctx1, ctx2);
569 else
571 gcc_assert (TREE_CODE (ctx1) == TRANSLATION_UNIT_DECL);
572 ctx_match = 1;
575 if (!ctx_match)
576 return 0;
578 /* Now compare the name of the types. */
579 tn1 = TYPE_NAME (t1);
580 tn2 = TYPE_NAME (t2);
581 if ((tn1 && !tn2) || !(tn1 && tn2))
582 return 0;
583 else if (!tn1 && !tn2)
584 /* Be conservative on unamed types. */
585 return 1;
587 if (DECL_P (tn1))
588 tn1 = DECL_NAME (tn1);
589 if (DECL_P (tn2))
590 tn2 = DECL_NAME (tn2);
591 if (strcmp (IDENTIFIER_POINTER (tn1),
592 IDENTIFIER_POINTER (tn2)))
593 return 0;
595 return lang_hooks.l_ipo.cmp_lang_type (t1, t2);
598 /* Return the canonical type of the type's main variant. */
599 static inline tree
600 get_norm_type (const_tree type)
602 tree cano_type = TYPE_MAIN_VARIANT (type);
603 if (TYPE_CANONICAL (cano_type))
604 cano_type = TYPE_CANONICAL (cano_type);
606 return cano_type;
609 /* Return 1 if type T1 and T2 are equivalent. Struct/union/class
610 types are compared using qualified name ids. Alias sets of
611 equivalent types will be merged. Client code may choose to do
612 structural equivalence check for sanity. Note the difference
613 between the types_compatible_p (and its langhooks subroutines)
614 and this interface. The former is mainly used to remove useless
615 type conversion and value numbering computation. It returns 1
616 only when it is sure and should not be used in contexts where
617 erroneously returning 0 causes problems. This interface
618 lipo_cmp_type behaves differently - it returns 1 when it is not
619 sure -- as the primary purpose of the interface is for alias
620 set computation. */
623 lipo_cmp_type (tree t1, tree t2)
625 if (TREE_CODE (t1) != TREE_CODE (t2))
626 return 0;
627 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
628 return 0;
629 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
630 return 0;
632 t1 = get_norm_type (t1);
633 t2 = get_norm_type (t2);
635 switch (TREE_CODE (t1))
637 case RECORD_TYPE:
638 case UNION_TYPE:
639 case QUAL_UNION_TYPE:
640 return aggr_has_equiv_id (t1, t2);
642 case POINTER_TYPE:
643 case REFERENCE_TYPE:
644 case COMPLEX_TYPE:
645 return lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2));
646 case ARRAY_TYPE:
647 return (lipo_cmp_type (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2))
648 && lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2)));
649 case METHOD_TYPE:
650 return lipo_cmp_type (TYPE_METHOD_BASETYPE (t1),
651 TYPE_METHOD_BASETYPE (t2));
652 case FUNCTION_TYPE:
654 tree arg1, arg2;
655 for (arg1 = TYPE_ARG_TYPES (t1), arg2 = TYPE_ARG_TYPES (t2);
656 arg1 && arg2;
657 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
658 if (!lipo_cmp_type (TREE_VALUE (arg1),
659 TREE_VALUE (arg2)))
660 return 0;
661 if (arg1 || arg2)
662 return 0;
663 return 1;
665 case OFFSET_TYPE:
666 return lipo_cmp_type (TYPE_OFFSET_BASETYPE (t1),
667 TYPE_OFFSET_BASETYPE (t2));
668 case ENUMERAL_TYPE:
669 return lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2));
670 case REAL_TYPE:
671 case FIXED_POINT_TYPE:
672 case INTEGER_TYPE:
673 return (TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
674 && TYPE_MODE (t1) == TYPE_MODE (t2)
675 && TYPE_MIN_VALUE (t1) == TYPE_MIN_VALUE (t2)
676 && TYPE_MAX_VALUE (t1) == TYPE_MAX_VALUE (t2));
677 case VECTOR_TYPE:
678 return (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
679 && lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2)));
680 case VOID_TYPE:
681 case BOOLEAN_TYPE:
682 return 1;
683 default:
684 gcc_unreachable ();
688 #ifndef ANON_AGGRNAME_PREFIX
689 #define ANON_AGGRNAME_PREFIX "__anon_"
690 #endif
691 #ifndef ANON_AGGRNAME_P
692 #define ANON_AGGRNAME_P(ID_NODE) \
693 (!strncmp (IDENTIFIER_POINTER (ID_NODE), ANON_AGGRNAME_PREFIX, \
694 sizeof (ANON_AGGRNAME_PREFIX) - 1))
695 #endif
697 /* Callback function used in tree walk to find referenced struct types. */
699 static tree
700 find_struct_types (tree *tp,
701 int *walk_subtrees ATTRIBUTE_UNUSED,
702 void *data ATTRIBUTE_UNUSED)
704 if (!(*tp))
705 return NULL_TREE;
707 if (TYPE_P (*tp))
709 if (lang_hooks.l_ipo.is_compiler_generated_type (*tp))
710 return NULL_TREE;
712 switch (TREE_CODE (*tp))
714 case RECORD_TYPE:
715 case UNION_TYPE:
716 case QUAL_UNION_TYPE:
718 tree cano_type, name;
719 tree context;
720 tree field;
722 cano_type = get_norm_type (*tp);
723 name = TYPE_NAME (cano_type);
724 if (!name)
726 /* the main variant of typedef of unnamed struct
727 has no name, use the orignal type for equivalence. */
728 cano_type = *tp;
729 name = TYPE_NAME (cano_type);
731 if (!name)
732 return NULL_TREE;
733 if (DECL_P (name)
734 && (DECL_IGNORED_P (name)
735 || ANON_AGGRNAME_P (DECL_NAME (name))))
736 return NULL_TREE;
738 if (!pointer_set_insert (type_set, cano_type))
739 VEC_safe_push (tree, heap, pending_types, cano_type);
740 else
741 return NULL_TREE; /* Or use walk tree without dups. */
743 context = TYPE_CONTEXT (cano_type);
744 if (context && TYPE_P (context))
745 walk_tree (&context, find_struct_types, NULL, NULL);
747 /* Instantiate a nested work as the tree walker does not
748 get to the fields. */
749 if (TYPE_BINFO (cano_type))
751 int i;
752 tree binfo, base_binfo;
754 for (binfo = TYPE_BINFO (cano_type), i = 0;
755 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
756 walk_tree (&BINFO_TYPE (base_binfo), find_struct_types,
757 NULL, NULL);
759 for (field = TYPE_FIELDS (cano_type);
760 field != 0;
761 field = TREE_CHAIN (field))
762 walk_tree (&TREE_TYPE (field), find_struct_types,
763 NULL, NULL);
764 return NULL_TREE;
766 default:
767 return NULL_TREE;
770 else if (DECL_P (*tp))
771 /* walk tree does not walk down decls, so do a nested walk here. */
772 walk_tree (&(TREE_TYPE (*tp)), find_struct_types, NULL, NULL);
774 return NULL_TREE;
777 /* Collect referenced struct types. */
779 static void
780 cgraph_collect_type_referenced (void)
782 basic_block bb;
783 gimple_stmt_iterator gi;
785 FOR_EACH_BB (bb)
787 for (gi = gsi_start_bb (bb); !gsi_end_p (gi); gsi_next (&gi))
789 unsigned i;
790 gimple stmt = gsi_stmt (gi);
791 for (i = 0; i < gimple_num_ops (stmt); i++)
792 walk_tree (gimple_op_ptr (stmt, i), find_struct_types, NULL, NULL);
797 /* Check type equivalence. Returns 1 if T1 and T2 are equivalent
798 for tbaa; return 0 if not. -1 is returned if it is unknown. */
801 equivalent_struct_types_for_tbaa (const_tree t1, const_tree t2)
803 struct type_ent key, *tent1, *tent2, **slot;
805 if (!l_ipo_type_tab)
806 return -1;
808 t1 = get_norm_type (t1);
809 t2 = get_norm_type (t2);
811 key.type = (tree) (long) t1;
812 slot = (struct type_ent **)
813 htab_find_slot (l_ipo_type_tab, &key, NO_INSERT);
814 if (!slot || !*slot)
815 return -1;
816 tent1 = *slot;
818 key.type = (tree) (long) t2;
819 slot = (struct type_ent **)
820 htab_find_slot (l_ipo_type_tab, &key, NO_INSERT);
821 if (!slot || !*slot)
822 return -1;
823 tent2 = *slot;
825 return tent1->eq_id == tent2->eq_id;
828 /* Build type hash table. */
830 static void
831 cgraph_build_type_equivalent_classes (void)
833 unsigned n, i;
834 n = VEC_length (tree, pending_types);
835 for (i = 0; i < n; i++)
837 struct type_ec **slot;
838 struct type_ec te;
839 te.rep_type = VEC_index (tree, pending_types, i);
840 te.eq_types = NULL;
841 slot = (struct type_ec **) htab_find_slot (type_hash_tab,
842 &te, INSERT);
843 if (!*slot)
845 *slot = XCNEW (struct type_ec);
846 (*slot)->rep_type = te.rep_type;
848 VEC_safe_push (tree, heap, (*slot)->eq_types, te.rep_type);
852 /* Re-propagate component types's alias set to that of TYPE. PROCESSED
853 is the pointer set of processed types. */
855 static void
856 re_record_component_aliases (tree type,
857 struct pointer_set_t *processed)
859 alias_set_type superset = get_alias_set (type);
860 tree field;
862 if (superset == 0)
863 return;
865 if (pointer_set_insert (processed, type))
866 return;
868 switch (TREE_CODE (type))
870 case RECORD_TYPE:
871 case UNION_TYPE:
872 case QUAL_UNION_TYPE:
873 /* Recursively record aliases for the base classes, if there are any. */
874 if (TYPE_BINFO (type))
876 int i;
877 tree binfo, base_binfo;
879 for (binfo = TYPE_BINFO (type), i = 0;
880 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
882 re_record_component_aliases (BINFO_TYPE (base_binfo),
883 processed);
884 record_alias_subset (superset,
885 get_alias_set (BINFO_TYPE (base_binfo)));
888 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
889 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
891 re_record_component_aliases (TREE_TYPE (field), processed);
892 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
894 break;
896 case COMPLEX_TYPE:
897 re_record_component_aliases (TREE_TYPE (type), processed);
898 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
899 break;
901 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
902 element type. */
904 default:
905 break;
909 /* The callback function to merge alias sets of equivalent types. */
911 static int
912 type_eq_process (void **slot, void *data ATTRIBUTE_UNUSED)
914 unsigned i;
915 alias_set_type alias_set, ptr_alias_set = -1;
916 tree rep_type, type;
917 VEC(tree, heap) *eq_types;
918 struct type_ec ** te = (struct type_ec **)slot;
919 bool zero_set = false, ptr_zero_set = false;
920 struct type_ent **slot2, key, *tent;
923 rep_type = (*te)->rep_type;
924 eq_types = (*te)->eq_types;
925 alias_set = get_alias_set (rep_type);
927 for (i = 0;
928 VEC_iterate (tree, eq_types, i, type);
929 ++i)
931 alias_set_type als, ptr_als = -1;
932 tree type_ptr = TYPE_POINTER_TO (type);;
934 als = get_alias_set (type);
935 if (als == 0)
936 zero_set = true;
938 if (alias_set && als && alias_set != als)
939 record_alias_subset (alias_set, als);
941 if (type_ptr)
943 ptr_als = get_alias_set (type_ptr);
944 if (ptr_als == 0)
945 ptr_zero_set = true;
947 if (ptr_alias_set == -1)
948 ptr_alias_set = ptr_als;
949 else
951 if (!ptr_zero_set && ptr_alias_set != ptr_als)
952 record_alias_subset (ptr_alias_set, ptr_als);
957 /* Now propagate back. */
958 for (i = 0;
959 VEC_iterate (tree, eq_types, i, type);
960 ++i)
962 alias_set_type als, ptr_als;
963 tree ptr_type = TYPE_POINTER_TO (type);
965 als = get_alias_set (type);
967 if (zero_set)
968 TYPE_ALIAS_SET (type) = 0;
969 else if (alias_set != als)
970 record_alias_subset (als, alias_set);
972 if (ptr_type)
974 ptr_als = get_alias_set (ptr_type);
975 if (ptr_zero_set)
976 TYPE_ALIAS_SET (ptr_type) = 0;
977 else if (ptr_alias_set != ptr_als)
978 record_alias_subset (ptr_als, ptr_alias_set);
983 /* Now populate the type table. */
984 l_ipo_eq_id++;
985 for (i = 0;
986 VEC_iterate (tree, eq_types, i, type);
987 ++i)
989 key.type = type;
990 slot2 = (struct type_ent **)
991 htab_find_slot (l_ipo_type_tab, &key, INSERT);
992 tent = *slot2;
993 gcc_assert (!tent);
994 tent = GGC_CNEW (struct type_ent);
995 tent->type = key.type;
996 tent->eq_id = l_ipo_eq_id;
997 *slot2 = tent;
1000 return 1;
1003 /* Regenerate alias set for aggregate types. */
1005 static void
1006 record_components_for_parent_types (void)
1008 unsigned n, i;
1009 struct pointer_set_t *processed_types;
1011 processed_types = pointer_set_create ();
1012 n = VEC_length (tree, pending_types);
1013 for (i = 0; i < n; i++)
1015 tree type = VEC_index (tree, pending_types, i);
1016 re_record_component_aliases (type, processed_types);
1019 pointer_set_destroy (processed_types);
1022 /* Unify type alias sets for equivalent types. */
1024 void
1025 cgraph_unify_type_alias_sets (void)
1027 struct cgraph_node *node;
1028 struct varpool_node *pv;
1030 if (!L_IPO_COMP_MODE)
1031 return;
1032 type_set = pointer_set_create ();
1033 type_hash_tab = htab_create (10, type_hash_hash,
1034 type_hash_eq, type_hash_del);
1035 l_ipo_type_tab = htab_create_ggc (10, type_addr_hash,
1036 type_addr_eq, NULL);
1038 for (node = cgraph_nodes; node; node = node->next)
1040 if (node->analyzed && (node->needed || node->reachable))
1042 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1043 current_function_decl = node->decl;
1044 cgraph_collect_type_referenced ();
1045 current_function_decl = NULL;
1046 pop_cfun ();
1050 for (pv = varpool_nodes; pv; pv = pv->next)
1051 walk_tree (&pv->decl, find_struct_types, NULL, NULL);
1053 /* Compute type equivalent classes. */
1054 cgraph_build_type_equivalent_classes ();
1055 /* Now unify alias sets of equivelent types. */
1056 htab_traverse (type_hash_tab, type_eq_process, NULL);
1057 /* Finally re-populating parent's alias set. */
1058 record_components_for_parent_types ();
1060 pointer_set_destroy (type_set);
1061 VEC_free (tree, heap, pending_types);
1062 htab_delete (type_hash_tab);
1065 /* Return true if NODE->decl from an auxiliary module has external
1066 definition (and therefore is not needed for expansion). */
1068 bool
1069 cgraph_is_aux_decl_external (struct cgraph_node *node)
1071 tree decl = node->decl;
1073 if (!L_IPO_COMP_MODE)
1074 return false;
1076 if (!cgraph_is_auxiliary (decl))
1077 return false;
1079 /* Versioned clones from auxiliary moduels are not
1080 external. */
1081 if (node->is_versioned_clone)
1082 return false;
1084 /* virtual functions won't be deleted in the primary module. */
1085 if (DECL_VIRTUAL_P (decl))
1086 return true;
1088 /* Comdat or weak functions in aux modules are not external --
1089 there is no guarantee that the definitition will be emitted
1090 in the primary compilation of this auxiliary module. */
1091 if (DECL_COMDAT (decl) || DECL_WEAK (decl))
1092 return false;
1094 /* The others from aux modules are external. */
1095 return true;
1098 /* Linked function symbol (cgraph node) table. */
1099 static GTY((param_is (struct cgraph_sym))) htab_t cgraph_symtab;
1101 /* This is true when global linking is needed and performed (for C++).
1102 For C, symbol linking is performed on the fly during parsing, and
1103 the cgraph_symtab is used only for keeping additional information
1104 for any already merged symbol if needed. */
1106 static bool global_link_performed = 0;
1108 /* For an external (non-defined) function DECL, return the primary
1109 module id (even though when the declaration is declared in an aux
1110 module). For a defined function DECL, return the module id in which
1111 it is defined. */
1113 unsigned
1114 cgraph_get_module_id (tree decl)
1116 struct function *func = DECL_STRUCT_FUNCTION (decl);
1117 /* Not defined. */
1118 if (!func)
1119 return primary_module_id;
1120 return FUNC_DECL_MODULE_ID (func);
1123 /* Return true if function decl is defined in an auxiliary module. */
1125 bool
1126 cgraph_is_auxiliary (tree decl)
1128 return (cgraph_get_module_id (decl) != primary_module_id);
1131 /* Return the hash value for cgraph_sym pointed to by P. The
1132 hash value is computed using function's assembler name. */
1134 static hashval_t
1135 hash_sym_by_assembler_name (const void *p)
1137 const struct cgraph_sym *n = (const struct cgraph_sym *) p;
1138 return (hashval_t) decl_assembler_name_hash (n->assembler_name);
1141 /* Return nonzero if P1 and P2 are equal. */
1143 static int
1144 eq_assembler_name (const void *p1, const void *p2)
1146 const struct cgraph_sym *n1 = (const struct cgraph_sym *) p1;
1147 const_tree name = (const_tree) p2;
1148 return (decl_assembler_name_equal (n1->rep_decl, name));
1151 /* Return the cgraph_sym for function declaration DECL. */
1153 static struct cgraph_sym **
1154 cgraph_sym (tree decl)
1156 struct cgraph_sym **slot;
1157 tree name;
1159 if (!cgraph_symtab)
1161 gcc_assert (!global_link_performed);
1162 return NULL;
1165 name = DECL_ASSEMBLER_NAME (decl);
1166 slot = (struct cgraph_sym **)
1167 htab_find_slot_with_hash (cgraph_symtab, name,
1168 decl_assembler_name_hash (name),
1169 NO_INSERT);
1170 return slot;
1173 /* Return the representative declaration for assembler name
1174 ASM_NAME. */
1176 tree
1177 cgraph_find_decl (tree asm_name)
1179 struct cgraph_sym **slot;
1180 if (!L_IPO_COMP_MODE)
1181 return NULL;
1182 if (!cgraph_symtab || !global_link_performed)
1183 return NULL;
1185 slot = (struct cgraph_sym **)
1186 htab_find_slot_with_hash (cgraph_symtab, asm_name,
1187 decl_assembler_name_hash (asm_name),
1188 NO_INSERT);
1189 if (!slot || !*slot)
1190 return NULL;
1192 return (*slot)->rep_node->decl;
1195 /* Return true if function declaration DECL is originally file scope
1196 static, which is promoted to global scope. */
1198 bool
1199 cgraph_is_promoted_static_func (tree decl)
1201 struct cgraph_sym ** sym;
1202 gcc_assert (L_IPO_COMP_MODE);
1204 /* cgraph_symtab will be created when any symbol got
1205 promoted. */
1206 if (!cgraph_symtab)
1207 return false;
1209 sym = cgraph_sym (decl);
1210 if (!sym)
1211 return false;
1212 return (*sym)->is_promoted_static;
1215 /* Hash function for module information table. ENT
1216 is a pointer to a cgraph_module_info. */
1218 static hashval_t
1219 htab_sym_hash (const void *ent)
1221 const struct cgraph_mod_info * const mi
1222 = (const struct cgraph_mod_info * const ) ent;
1223 return (hashval_t) mi->module_id;
1226 /* Hash equality function for module information table. */
1228 static int
1229 htab_sym_eq (const void *ent1, const void *ent2)
1231 const struct cgraph_mod_info * const mi1
1232 = (const struct cgraph_mod_info * const ) ent1;
1233 const struct cgraph_mod_info * const mi2
1234 = (const struct cgraph_mod_info * const ) ent2;
1235 return (mi1->module_id == mi2->module_id);
1238 /* cgraph_sym SYM may be defined in more than one source modules.
1239 Add declaration DECL's definiting module to SYM. */
1241 static void
1242 add_define_module (struct cgraph_sym *sym, tree decl)
1244 unsigned module_id;
1245 struct cgraph_mod_info **slot;
1246 struct cgraph_mod_info mi;
1248 struct function *f = DECL_STRUCT_FUNCTION (decl);
1249 if (!f)
1250 return;
1251 module_id = FUNC_DECL_MODULE_ID (f);
1253 if (!sym->def_module_hash)
1254 sym->def_module_hash
1255 = htab_create_ggc (10, htab_sym_hash, htab_sym_eq, NULL);
1257 mi.module_id = module_id;
1258 slot = (struct cgraph_mod_info **)htab_find_slot (sym->def_module_hash,
1259 &mi, INSERT);
1260 if (!*slot)
1262 *slot = GGC_CNEW (struct cgraph_mod_info);
1263 (*slot)->module_id = module_id;
1265 else
1266 gcc_assert ((*slot)->module_id == module_id);
1269 /* Return true if the symbol associated with DECL is defined in module
1270 MODULE_ID. This interface is used by the inliner to make sure profile-gen
1271 and profile-use pass (L-IPO mode) make consistent inline decision. */
1273 bool
1274 cgraph_is_inline_body_available_in_module (tree decl, unsigned module_id)
1276 struct cgraph_sym **sym;
1277 void **slot;
1278 struct cgraph_mod_info mi;
1280 gcc_assert (L_IPO_COMP_MODE);
1282 if (DECL_BUILT_IN (decl))
1283 return true;
1285 /* TODO: revisit this. */
1286 if (DECL_IN_SYSTEM_HEADER (decl) && DECL_DECLARED_INLINE_P (decl))
1287 return true;
1289 gcc_assert (TREE_STATIC (decl) || DECL_DECLARED_INLINE_P (decl));
1291 if (cgraph_get_module_id (decl) == module_id)
1292 return true;
1294 sym = cgraph_sym (decl);
1295 if (!sym || !(*sym)->def_module_hash)
1296 return false;
1298 mi.module_id = module_id;
1299 slot = htab_find_slot ((*sym)->def_module_hash, &mi, NO_INSERT);
1300 if (slot)
1302 gcc_assert (((struct cgraph_mod_info*)*slot)->module_id == module_id);
1303 return true;
1305 return false;
1308 /* Return the linked cgraph node using DECL's assembler name. DO_ASSERT
1309 is a flag indicating that a non null link target must be returned. */
1311 struct cgraph_node *
1312 cgraph_lipo_get_resolved_node_1 (tree decl, bool do_assert)
1314 struct cgraph_sym **slot;
1316 slot = cgraph_sym (decl);
1318 if (!slot || !*slot)
1320 if (!do_assert)
1321 return NULL;
1322 else
1324 /* Nodes that are indirectly called are not 'reachable' in
1325 the callgraph. If they are not needed (comdat, inline
1326 extern etc), they may be removed from the link table
1327 before direct calls to them are exposed (via indirect
1328 call promtion by const folding etc). When this happens,
1329 the node will be to be relinked. A probably better fix
1330 is to modify the callgraph so that they are not eliminated
1331 in the first place -- this will allow inlining to happen. */
1333 struct cgraph_node *n = cgraph_node (decl);
1334 if (!n->analyzed)
1336 gcc_assert (DECL_EXTERNAL (decl)
1337 || cgraph_is_aux_decl_external (n)
1338 || DECL_VIRTUAL_P (decl));
1339 gcc_assert ((!n->reachable && !n->needed)
1340 /* This is the case for explicit extern instantiation,
1341 when cgraph node is not created before link. */
1342 || DECL_EXTERNAL (decl));
1343 cgraph_link_node (n);
1344 return n;
1346 else
1347 gcc_unreachable ();
1350 else
1352 struct cgraph_sym *sym = *slot;
1353 return sym->rep_node;
1357 /* Return the cgraph_node of DECL if decl has definition; otherwise return
1358 the cgraph node of the representative decl, which is the declaration DECL
1359 is resolved to after linking/symbol resolution. */
1361 struct cgraph_node *
1362 cgraph_lipo_get_resolved_node (tree decl)
1364 struct cgraph_node *node = NULL;
1366 gcc_assert (L_IPO_COMP_MODE && global_link_performed);
1367 gcc_assert (cgraph_symtab);
1369 /* Never merged. */
1370 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl)
1371 /* builtin function decls are shared across modules, but 'linking'
1372 is still performed for them to keep track of the set of defining
1373 modules. Skip the real resolution here to avoid merging '__builtin_xxx'
1374 with 'xxx'. */
1375 || DECL_BUILT_IN (decl))
1376 return cgraph_node (decl);
1378 /* if (gimple_has_body_p (decl)) */
1379 if (TREE_STATIC (decl))
1380 return cgraph_node (decl);
1382 node = cgraph_lipo_get_resolved_node_1 (decl, true);
1383 return node;
1386 /* When NODE->decl is dead function eliminated,
1387 remove the entry in the link table. */
1389 void
1390 cgraph_remove_link_node (struct cgraph_node *node)
1392 tree name, decl;
1394 if (!L_IPO_COMP_MODE || !cgraph_symtab)
1395 return;
1397 decl = node->decl;
1399 /* Skip nodes that are not in the link table. */
1400 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
1401 return;
1403 /* Skip if node is an inline clone or if the node has
1404 defintion that is not really resolved to the merged node. */
1405 if (cgraph_lipo_get_resolved_node_1 (decl, false) != node)
1406 return;
1408 name = DECL_ASSEMBLER_NAME (decl);
1409 htab_remove_elt_with_hash (cgraph_symtab, name,
1410 decl_assembler_name_hash (name));
1413 /* Return true if the function body for DECL has profile information. */
1415 static bool
1416 has_profile_info (tree decl)
1418 gcov_type *ctrs = NULL;
1419 unsigned n;
1420 struct function* f = DECL_STRUCT_FUNCTION (decl);
1422 ctrs = get_coverage_counts_no_warn (f, GCOV_COUNTER_ARCS, &n);
1423 if (ctrs)
1425 unsigned i;
1426 for (i = 0; i < n; i++)
1427 if (ctrs[i])
1428 return true;
1431 return false;
1434 /* Resolve delaration NODE->decl for function symbol *SLOT. */
1436 static void
1437 resolve_cgraph_node (struct cgraph_sym **slot, struct cgraph_node *node)
1439 tree decl1, decl2;
1440 int decl1_defined = 0;
1441 int decl2_defined = 0;
1443 decl1 = (*slot)->rep_decl;
1444 decl2 = node->decl;
1446 /* Can not use gimple_has_body_p because there is no
1447 guarantee functions are gimplified at this point. */
1448 decl1_defined = TREE_STATIC (decl1);
1449 decl2_defined = TREE_STATIC (decl2);
1451 if (decl1_defined && !decl2_defined)
1452 return;
1454 if (!decl1_defined && decl2_defined)
1456 (*slot)->rep_node = node;
1457 (*slot)->rep_decl = decl2;
1458 add_define_module (*slot, decl2);
1459 return;
1462 if (decl2_defined)
1464 bool has_prof1 = false;
1465 bool has_prof2 = false;
1466 gcc_assert (decl1_defined);
1467 add_define_module (*slot, decl2);
1469 has_prof1 = has_profile_info (decl1);
1470 if (has_prof1)
1471 return;
1472 has_prof2 = has_profile_info (decl2);
1473 if (has_prof2)
1475 (*slot)->rep_node = node;
1476 (*slot)->rep_decl = decl2;
1478 return;
1480 return;
1484 /* Resolve NODE->decl in the function symbol table. */
1486 struct cgraph_sym *
1487 cgraph_link_node (struct cgraph_node *node)
1489 void **slot;
1490 tree name;
1492 if (!L_IPO_COMP_MODE)
1493 return NULL;
1495 if (!cgraph_symtab)
1496 cgraph_symtab
1497 = htab_create_ggc (10, hash_sym_by_assembler_name,
1498 eq_assembler_name, NULL);
1500 /* Skip the cases when the defintion can be locally resolved, and
1501 when we do not need to keep track of defining modules. */
1502 if (!TREE_PUBLIC (node->decl) || DECL_ARTIFICIAL (node->decl))
1503 return NULL;
1505 name = DECL_ASSEMBLER_NAME (node->decl);
1506 slot = htab_find_slot_with_hash (cgraph_symtab, name,
1507 decl_assembler_name_hash (name),
1508 INSERT);
1509 if (*slot)
1510 resolve_cgraph_node ((struct cgraph_sym **) slot, node);
1511 else
1513 struct cgraph_sym *sym = GGC_CNEW (struct cgraph_sym);
1514 sym->rep_node = node;
1515 sym->rep_decl = node->decl;
1516 sym->assembler_name = name;
1517 add_define_module (sym, node->decl);
1518 *slot = sym;
1520 return (struct cgraph_sym *) *slot;
1523 /* Perform cross module linking of function declarations. */
1525 void
1526 cgraph_do_link (void)
1528 struct cgraph_node *node;
1530 if (!L_IPO_COMP_MODE)
1531 return;
1533 global_link_performed = 1;
1535 if (!cgraph_symtab)
1536 cgraph_symtab
1537 = htab_create_ggc (10, hash_sym_by_assembler_name,
1538 eq_assembler_name, NULL);
1540 for (node = cgraph_nodes; node; node = node->next)
1542 gcc_assert (!node->global.inlined_to);
1543 cgraph_link_node (node);
1547 struct promo_ent
1549 char* assemb_name;
1550 int seq;
1553 /* Hash function for promo_ent table. */
1555 static hashval_t
1556 promo_ent_hash (const void *ent)
1558 const struct promo_ent *const entry
1559 = (const struct promo_ent *) ent;
1561 return htab_hash_string (entry->assemb_name);
1564 /* Hash_eq function for promo_ent table. */
1566 static int
1567 promo_ent_eq (const void *ent1, const void *ent2)
1569 const struct promo_ent *const entry1
1570 = (const struct promo_ent *) ent1;
1571 const struct promo_ent *const entry2
1572 = (const struct promo_ent *) ent2;
1573 if (!strcmp (entry1->assemb_name, entry2->assemb_name))
1574 return 1;
1575 return 0;
1578 /* Delete function for promo_ent hash table. */
1580 static void
1581 promo_ent_del (void *ent)
1583 struct promo_ent *const entry
1584 = (struct promo_ent *) ent;
1586 free (entry->assemb_name);
1587 free (entry);
1590 static htab_t promo_ent_hash_tab = NULL;
1592 /* Return a unique sequence number for NAME. This is needed to avoid
1593 name conflict -- function scope statics may have identical names.
1595 This function returns a zero sequence number if it is called with
1596 a particular NAME for the first time, and non-zero otherwise.
1597 This fact is used to keep track of unseen weak variables. */
1599 static int
1600 get_name_seq_num (const char *name)
1602 struct promo_ent **slot;
1603 struct promo_ent ent;
1604 ent.assemb_name = xstrdup (name);
1605 ent.seq = 0;
1607 slot = (struct promo_ent **)
1608 htab_find_slot (promo_ent_hash_tab, &ent, INSERT);
1610 if (!*slot)
1612 *slot = XCNEW (struct promo_ent);
1613 (*slot)->assemb_name = ent.assemb_name;
1615 else
1617 (*slot)->seq++;
1618 free (ent.assemb_name);
1620 return (*slot)->seq;
1623 /* Promote DECL to be global. MODULE_ID is the id of the module where
1624 DECL is defined. IS_EXTERN is a flag indicating if externalization
1625 is needed. */
1627 static void
1628 promote_static_var_func (unsigned module_id, tree decl, bool is_extern)
1630 tree id, assemb_id;
1631 char *assembler_name;
1632 const char *name;
1633 struct function *context = NULL;
1634 tree alias;
1635 int seq = 0;
1637 /* No need to promote symbol alias. */
1638 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1639 if (alias)
1640 return;
1642 /* Function decls in C++ may contain characters not taken by assembler.
1643 Similarly, function scope static variable has UID as the assembler name
1644 suffix which is not consistent across modules. */
1646 if (DECL_ASSEMBLER_NAME_SET_P (decl)
1647 && TREE_CODE (decl) == FUNCTION_DECL)
1648 cgraph_remove_assembler_hash_node (cgraph_node (decl));
1650 if (TREE_CODE (decl) == FUNCTION_DECL)
1652 if (!DECL_CONTEXT (decl)
1653 || TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
1655 id = DECL_NAME (decl);
1656 /* if (IDENTIFIER_OPNAME_P (id)) */
1657 if (TREE_LANG_FLAG_2 (id))
1658 id = DECL_ASSEMBLER_NAME (decl);
1660 else
1661 id = DECL_ASSEMBLER_NAME (decl);
1663 else
1665 if (!DECL_CONTEXT (decl))
1666 id = DECL_NAME (decl);
1667 else if (TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
1668 id = DECL_ASSEMBLER_NAME (decl);
1669 else if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
1671 id = DECL_NAME (decl);
1672 context = DECL_STRUCT_FUNCTION (DECL_CONTEXT (decl));
1674 else
1675 /* file scope context */
1676 id = DECL_NAME (decl);
1679 name = IDENTIFIER_POINTER (id);
1680 if (context)
1682 char *n;
1683 unsigned fno = FUNC_DECL_FUNC_ID (context);
1684 n = (char *)alloca (strlen (name) + 15);
1685 sprintf (n, "%s_%u", name, fno);
1686 name = n;
1689 assembler_name = (char*) alloca (strlen (name) + 30);
1690 sprintf (assembler_name, "%s_cmo_%u", name, module_id);
1691 seq = get_name_seq_num (assembler_name);
1692 if (seq)
1693 sprintf (assembler_name, "%s_%d", assembler_name, seq);
1695 assemb_id = get_identifier (assembler_name);
1696 SET_DECL_ASSEMBLER_NAME (decl, assemb_id);
1697 TREE_PUBLIC (decl) = 1;
1698 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
1699 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1701 if (TREE_CODE (decl) == FUNCTION_DECL)
1703 struct cgraph_sym *resolved_sym = NULL;
1704 struct cgraph_node *node = cgraph_node (decl);
1705 cgraph_add_assembler_hash_node (node);
1706 /* incremental update the link table -- or
1707 can introduce a flag in cgraph node to indicate
1708 non global origin. */
1709 resolved_sym = cgraph_link_node (node);
1710 gcc_assert (resolved_sym);
1711 resolved_sym->is_promoted_static = 1;
1713 else
1715 struct varpool_node *node = varpool_node (decl);
1716 varpool_link_node (node);
1719 if (is_extern)
1721 if (TREE_CODE (decl) == VAR_DECL)
1723 TREE_STATIC (decl) = 0;
1724 DECL_EXTERNAL (decl) = 1;
1725 DECL_INITIAL (decl) = 0;
1726 DECL_CONTEXT (decl) = 0;
1728 /* else
1729 Function body will be deleted later before expansion. */
1731 else
1732 TREE_STATIC (decl) = 1;
1735 /* Externalize global variables from aux modules and promote
1736 static variables.
1737 WEAK variables are treated especially in
1738 varpool_remove_duplicate_weak_decls. */
1740 static void
1741 process_module_scope_static_var (struct varpool_node *vnode)
1743 tree decl = vnode->decl;
1745 if (varpool_is_auxiliary (vnode))
1747 gcc_assert (vnode->module_id != primary_module_id);
1748 if (TREE_PUBLIC (decl))
1750 /* Externalize non-weak variables. */
1751 if (!DECL_WEAK (decl))
1753 DECL_EXTERNAL (decl) = 1;
1754 TREE_STATIC (decl) = 0;
1755 DECL_INITIAL (decl) = NULL;
1756 DECL_CONTEXT (decl) = NULL;
1759 else
1761 /* Promote static vars to global. */
1762 if (vnode->module_id)
1763 promote_static_var_func (vnode->module_id, decl,
1764 varpool_is_auxiliary (vnode));
1767 else
1769 if (PRIMARY_MODULE_EXPORTED && !TREE_PUBLIC (decl))
1770 promote_static_var_func (vnode->module_id, decl,
1771 varpool_is_auxiliary (vnode));
1775 /* Promote static function CNODE->decl to be global. */
1777 static void
1778 process_module_scope_static_func (struct cgraph_node *cnode)
1780 tree decl = cnode->decl;
1782 if (TREE_PUBLIC (decl)
1783 || !TREE_STATIC (decl)
1784 || DECL_EXTERNAL (decl)
1785 || DECL_ARTIFICIAL (decl))
1786 return;
1788 if (cgraph_is_auxiliary (cnode->decl))
1790 gcc_assert (cgraph_get_module_id (cnode->decl)
1791 != primary_module_id);
1792 /* Promote static function to global. */
1793 if (cgraph_get_module_id (cnode->decl))
1794 promote_static_var_func (cgraph_get_module_id (cnode->decl), decl, 1);
1796 else
1798 if (PRIMARY_MODULE_EXPORTED
1799 /* skip static_init routines. */
1800 && !DECL_ARTIFICIAL (decl))
1802 promote_static_var_func (cgraph_get_module_id (cnode->decl), decl, 0);
1803 cgraph_mark_if_needed (decl);
1808 /* Process var_decls, func_decls with static storage. */
1810 void
1811 cgraph_process_module_scope_statics (void)
1813 struct cgraph_node *pf;
1814 struct varpool_node *pv;
1816 if (!L_IPO_COMP_MODE)
1817 return;
1819 promo_ent_hash_tab = htab_create (10, promo_ent_hash,
1820 promo_ent_eq, promo_ent_del);
1822 /* Process variable first. */
1823 for (pv = varpool_nodes_queue; pv; pv = pv->next_needed)
1824 process_module_scope_static_var (pv);
1826 for (pf = cgraph_nodes; pf; pf = pf->next)
1827 process_module_scope_static_func (pf);
1829 htab_delete (promo_ent_hash_tab);
1832 /* There could be duplicate non-extern WEAK decls in the varpool queue,
1833 coming from different modules. All but one of these need to be externalized
1834 and removed from the varpool queue.
1835 Duplicate WEAK decls can be added to varpool queue as late as
1836 cgraph_expand_function, when a WEAK decl is marked referenced as assembler
1837 is being output. Therefore, a call to this function should be made after
1838 cgraph_expand_function. */
1840 void
1841 varpool_remove_duplicate_weak_decls (void)
1843 struct varpool_node *next, *node = varpool_nodes_queue;
1845 if (!L_IPO_COMP_MODE)
1846 return;
1848 varpool_reset_queue ();
1850 promo_ent_hash_tab = htab_create (10, promo_ent_hash,
1851 promo_ent_eq, promo_ent_del);
1853 while (node)
1855 tree decl = node->decl;
1856 next = node->next_needed;
1857 node->needed = 0;
1858 if (TREE_PUBLIC (decl) && DECL_WEAK (decl) && !DECL_EXTERNAL (decl)
1859 && get_name_seq_num (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl))))
1861 DECL_EXTERNAL (decl) = 1;
1862 TREE_STATIC (decl) = 0;
1863 DECL_INITIAL (decl) = NULL;
1864 DECL_CONTEXT (decl) = NULL;
1866 else
1867 varpool_mark_needed_node (node);
1868 node = next;
1871 htab_delete (promo_ent_hash_tab);
1874 static GTY((param_is (struct varpool_node))) htab_t varpool_symtab;
1876 /* Hash function for varpool node. */
1878 static hashval_t
1879 hash_node_by_assembler_name (const void *p)
1881 const struct varpool_node *n = (const struct varpool_node *) p;
1882 return (hashval_t) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n->decl));
1885 /* Returns nonzero if P1 and P2 are equal. */
1887 static int
1888 eq_node_assembler_name (const void *p1, const void *p2)
1890 const struct varpool_node *n1 = (const struct varpool_node *) p1;
1891 const_tree name = (const_tree)p2;
1892 return (decl_assembler_name_equal (n1->decl, name));
1895 /* Return true if NODE's decl is declared in an auxiliary module. */
1897 bool
1898 varpool_is_auxiliary (struct varpool_node *node)
1900 return (node->module_id
1901 && node->module_id != primary_module_id);
1904 /* Return the varpool_node to which DECL is resolved to during linking.
1905 This method can not be used after static to global promotion happens. */
1907 struct varpool_node *
1908 real_varpool_node (tree decl)
1910 void **slot;
1911 tree name;
1913 if (!L_IPO_COMP_MODE || !varpool_symtab)
1914 return varpool_node (decl);
1916 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
1917 return varpool_node (decl);
1919 name = DECL_ASSEMBLER_NAME (decl);
1920 slot = htab_find_slot_with_hash (varpool_symtab, name,
1921 decl_assembler_name_hash (name),
1922 NO_INSERT);
1923 gcc_assert (slot && *slot);
1924 return (struct varpool_node *)*slot;
1927 /* Remove NODE from the link table. */
1929 void
1930 varpool_remove_link_node (struct varpool_node *node)
1932 tree name;
1933 tree decl;
1935 if (!L_IPO_COMP_MODE || !varpool_symtab)
1936 return;
1938 decl = node->decl;
1940 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
1941 return;
1943 if (real_varpool_node (decl) != node)
1944 return;
1946 name = DECL_ASSEMBLER_NAME (decl);
1947 htab_remove_elt_with_hash (varpool_symtab, name,
1948 decl_assembler_name_hash (name));
1951 /* Merge the addressable attribute from DECL2 to DECL1. */
1953 static inline void
1954 merge_addressable_attr (tree decl1, tree decl2)
1956 if (TREE_ADDRESSABLE (decl2))
1957 TREE_ADDRESSABLE (decl1) = 1;
1960 /* Resolve NODE->decl to symbol table entry *SLOT. */
1962 static void
1963 resolve_varpool_node (struct varpool_node **slot, struct varpool_node *node)
1965 tree decl1, decl2;
1967 decl1 = (*slot)->decl;
1968 decl2 = node->decl;
1970 /* Take the decl with the complete type. */
1971 if (COMPLETE_TYPE_P (TREE_TYPE (decl1))
1972 && !COMPLETE_TYPE_P (TREE_TYPE (decl2)))
1974 merge_addressable_attr (decl1, decl2);
1975 return;
1977 if (!COMPLETE_TYPE_P (TREE_TYPE (decl1))
1978 && COMPLETE_TYPE_P (TREE_TYPE (decl2)))
1980 *slot = node;
1981 merge_addressable_attr (decl2, decl1);
1982 return;
1985 /* Either all complete or neither's type is complete. Just
1986 pick the primary module's decl. */
1987 if (!varpool_is_auxiliary (*slot))
1989 merge_addressable_attr (decl1, decl2);
1990 return;
1993 if (!varpool_is_auxiliary (node))
1995 *slot = node;
1996 merge_addressable_attr (decl2, decl1);
1997 return;
2000 merge_addressable_attr (decl1, decl2);
2001 return;
2004 /* Link NODE into var_decl symbol table. */
2006 void
2007 varpool_link_node (struct varpool_node *node)
2009 tree name;
2010 void **slot;
2012 if (!L_IPO_COMP_MODE || !varpool_symtab)
2013 return;
2015 if (!TREE_PUBLIC (node->decl) || DECL_ARTIFICIAL (node->decl))
2016 return;
2018 name = DECL_ASSEMBLER_NAME (node->decl);
2019 slot = htab_find_slot_with_hash (varpool_symtab, name,
2020 decl_assembler_name_hash (name),
2021 INSERT);
2022 if (*slot)
2023 resolve_varpool_node ((struct varpool_node **) slot, node);
2024 else
2025 *slot = node;
2028 /* Perform cross module linking for var_decls. */
2030 void
2031 varpool_do_link (void)
2033 struct varpool_node *node;
2035 if (!L_IPO_COMP_MODE)
2036 return;
2038 varpool_symtab
2039 = htab_create_ggc (10, hash_node_by_assembler_name,
2040 eq_node_assembler_name, NULL);
2041 for (node = varpool_nodes; node; node = node->next)
2042 varpool_link_node (node);
2045 /* Get the list of assembler name ids with reference bit set. */
2047 void
2048 varpool_get_referenced_asm_ids (VEC(tree, gc) ** ids)
2050 struct varpool_node *node;
2051 for (node = varpool_nodes; node; node = node->next)
2053 tree asm_id = NULL;
2054 tree decl = node->decl;
2055 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2057 asm_id = DECL_ASSEMBLER_NAME (decl);
2058 VEC_safe_push (tree, gc, *ids, asm_id);
2063 /* Clear the referenced bit in all assembler ids. */
2065 void
2066 varpool_clear_asm_id_reference_bit (void)
2068 struct varpool_node *node;
2069 for (node = varpool_nodes; node; node = node->next)
2071 tree asm_id = NULL;
2072 tree decl = node->decl;
2073 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2075 asm_id = DECL_ASSEMBLER_NAME (decl);
2076 TREE_SYMBOL_REFERENCED (asm_id) = 0;
2082 #include "gt-l-ipo.h"