Merge aosp-toolchain/gcc/gcc-4_9 changes.
[official-gcc.git] / gcc-4_9-mobile / gcc / l-ipo.c
blob7ed78ea3f2c249f88eceb1f90be1419fd36612cb
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 "is-a.h"
26 #include "predict.h"
27 #include "function.h"
28 #include "basic-block.h"
29 #include "stor-layout.h"
30 #include "pointer-set.h"
31 #include "stringpool.h"
32 #include "c-family/c-common.h"
33 #include "toplev.h"
34 #include "langhooks.h"
35 #include "langhooks-def.h"
36 #include "diagnostic.h"
37 #include "debug.h"
38 #include "tree-ssa-alias.h"
39 #include "internal-fn.h"
40 #include "gimple-expr.h"
41 #include "gimple.h"
42 #include "gimple-iterator.h"
43 #include "cgraph.h"
44 #include "l-ipo.h"
45 #include "coverage.h"
46 #include "gcov-io.h"
47 #include "timevar.h"
48 #include "vec.h"
49 #include "params.h"
50 #include "rtl.h"
51 #include "varasm.h"
53 unsigned ggc_total_memory; /* in KB */
55 struct GTY(()) saved_module_scope
57 vec<tree, va_gc> *module_decls;
58 unsigned module_id;
61 static GTY (()) struct saved_module_scope *current_module_scope;
62 static GTY ((param_is (saved_module_scope))) htab_t saved_module_scope_map;
63 static int primary_module_last_funcdef_no = 0;
64 /* Function id space for each module are qualified by the module id. After all the files
65 are parsed, we need to reset the funcdef_no to the max value from all module so that
66 the function clones do not assigned with ids colliding with some other orignal function
67 in the same module. */
68 static int max_funcdef_no = 0;
69 static location_t primary_module_last_loc;
70 /* Primary module pending templates. */
71 /* Referenced asm ids in primary module. */
72 static GTY (()) vec<tree, va_gc> *referenced_asm_ids = NULL;
73 bool parser_parsing_start = false;
74 /* Nonzero if we're done parsing and into end-of-file activities. */
75 int at_eof;
77 static int aggr_has_equiv_id (tree t1, tree t2);
79 /* Module scope hash function. */
81 static hashval_t
82 htab_module_scope_hash (const void *ent)
84 const struct saved_module_scope *const entry
85 = (const struct saved_module_scope *) ent;
86 return (hashval_t) entry->module_id;
89 /* Module scope equality function. */
91 static int
92 htab_module_scope_eq (const void *ent1, const void *ent2)
94 const struct saved_module_scope *const entry1
95 = (const struct saved_module_scope *) ent1;
96 const struct saved_module_scope *const entry2
97 = (const struct saved_module_scope *) ent2;
99 return entry1->module_id == entry2->module_id;
102 /* Returns the module scope given a module id MOD_ID. */
104 static struct saved_module_scope *
105 get_module_scope (unsigned mod_id)
107 struct saved_module_scope **slot, key, *module_scope;
109 gcc_assert (mod_id);
111 if (saved_module_scope_map == NULL)
112 saved_module_scope_map = htab_create_ggc (10, htab_module_scope_hash,
113 htab_module_scope_eq, NULL);
114 key.module_id = mod_id;
115 slot = (struct saved_module_scope **)
116 htab_find_slot (saved_module_scope_map, &key, INSERT);
117 module_scope = *slot;
118 if (!module_scope)
120 module_scope = ggc_alloc_cleared_saved_module_scope ();
121 module_scope->module_id = mod_id;
122 *slot = module_scope;
124 return module_scope;
127 /* Allocate memory for struct lang_decl for tree T. */
129 static struct lang_decl *
130 alloc_lang_decl (tree t)
132 size_t size;
133 size = lang_hooks.l_ipo.get_lang_decl_size (t);
134 return ggc_alloc_cleared_lang_decl (size);
137 /* Return a cloned copy of tree SRC. */
139 tree
140 lipo_save_decl (tree src)
142 tree saved = copy_node (src);
143 enum tree_code tc = TREE_CODE (src);
144 if (TREE_CODE_CLASS (tc) == tcc_declaration)
146 struct lang_decl *ls = NULL;
147 struct function *func = NULL;
148 DECL_CONTEXT (saved) = DECL_CONTEXT (src);
149 if (DECL_LANG_SPECIFIC (src))
151 ls = alloc_lang_decl (src);
152 memcpy (ls, DECL_LANG_SPECIFIC (src),
153 lang_hooks.l_ipo.get_lang_decl_size (src));
155 DECL_LANG_SPECIFIC (saved) = ls;
156 if (tc == FUNCTION_DECL && DECL_STRUCT_FUNCTION (src))
158 func = ggc_alloc_cleared_function ();
159 *func = *(DECL_STRUCT_FUNCTION (src));
160 DECL_STRUCT_FUNCTION (saved) = func;
163 else
165 gcc_assert (TREE_CODE_CLASS (tc) == tcc_type &&
166 TYPE_MAIN_VARIANT (src) == src);
167 TYPE_CONTEXT (saved) = TYPE_CONTEXT (src);
168 lang_hooks.l_ipo.dup_lang_type (src, saved);
171 return saved;
174 /* Copy tree SAVED to tree DEST. */
176 void
177 lipo_restore_decl (tree dest, tree saved)
179 enum tree_code tc;
180 unsigned old_uid;
181 struct lang_decl *oldls;
183 tc = TREE_CODE (saved);
184 if (TREE_CODE_CLASS (tc) == tcc_declaration)
186 struct function *oldfunc = NULL;
187 old_uid = DECL_UID (dest);
188 oldls = DECL_LANG_SPECIFIC (dest);
189 oldfunc
190 = (tc == FUNCTION_DECL ? DECL_STRUCT_FUNCTION (dest) : NULL);
192 memcpy ((char *) dest + sizeof (struct tree_common),
193 (char *) saved + sizeof (struct tree_common),
194 sizeof (struct tree_decl_common) - sizeof (struct tree_common));
196 if (tc == FUNCTION_DECL)
197 memcpy ((char *) dest + sizeof (struct tree_decl_common),
198 (char *) saved + sizeof (struct tree_decl_common),
199 sizeof (struct tree_function_decl)
200 - sizeof (struct tree_decl_common));
202 DECL_UID (dest) = old_uid;
203 if (DECL_LANG_SPECIFIC (saved))
205 if (!oldls)
206 oldls = alloc_lang_decl (dest);
207 memcpy (oldls, DECL_LANG_SPECIFIC (saved),
208 lang_hooks.l_ipo.get_lang_decl_size (saved));
209 DECL_LANG_SPECIFIC (dest) = oldls;
211 else
212 DECL_LANG_SPECIFIC (dest) = NULL;
214 if (tc == FUNCTION_DECL)
216 if (DECL_STRUCT_FUNCTION (saved))
218 if (!oldfunc)
219 oldfunc = ggc_alloc_cleared_function ();
220 *oldfunc = *(DECL_STRUCT_FUNCTION (saved));
221 DECL_STRUCT_FUNCTION (dest) = oldfunc;
223 else
224 DECL_STRUCT_FUNCTION (dest) = NULL;
227 else
229 gcc_assert (TREE_CODE_CLASS (tc) == tcc_type);
230 lang_hooks.l_ipo.copy_lang_type (saved, dest);
235 /* Return the name for tree TD which is either a decl or type. */
237 tree
238 get_type_or_decl_name (tree td)
240 tree id;
242 if (DECL_P (td))
243 id = DECL_NAME (td);
244 else
246 id = TYPE_NAME (td);
247 if (DECL_P (id))
248 id = DECL_NAME (id);
250 return id;
253 /* For a DECL (a type or a decl) in SCOPE, check to see if it is in
254 global or namespace scope. If yes, add it to the current module scope. */
256 void
257 add_decl_to_current_module_scope (tree decl, void *scope)
259 struct saved_module_scope *module_scope;
260 tree id;
262 if (!flag_dyn_ipa)
263 return;
265 if (!parser_parsing_start)
267 /* The source file may contains only global variable declations
268 -- there is no module grouping data associated with it, so
269 neither primary_module_id nor current_module_id is set. */
270 lang_hooks.l_ipo.add_built_in_decl (decl);
271 return;
274 if (!L_IPO_COMP_MODE)
275 return;
277 if (!lang_hooks.l_ipo.has_global_name (decl, scope))
278 return;
280 /* Unlike C++ where names are attached to type decls, for C, the type name
281 is identifier node. Thus we need to track type names as well. */
282 id = get_type_or_decl_name (decl);
283 if (!id)
284 return;
286 module_scope = current_module_scope;
287 gcc_assert (module_scope && module_scope->module_id == current_module_id);
288 vec_safe_push (module_scope->module_decls, decl);
291 /* Clear name bindings for all decls created in MODULE_SCOPE. */
293 static void
294 clear_module_scope_bindings (struct saved_module_scope *module_scope)
296 size_t i;
297 tree decl;
299 for (i = 0;
300 vec_safe_iterate (module_scope->module_decls, i, &decl);
301 ++i)
303 lang_hooks.l_ipo.clear_global_name_bindings (
304 get_type_or_decl_name (decl));
305 /* Now force creating assembly name. */
306 if (VAR_OR_FUNCTION_DECL_P (decl))
308 tree assembler_name;
310 if (HAS_DECL_ASSEMBLER_NAME_P (decl)
311 && DECL_ASSEMBLER_NAME_SET_P (decl))
313 assembler_name = DECL_ASSEMBLER_NAME (decl);
314 lang_hooks.l_ipo.clear_global_name_bindings (assembler_name);
320 /* The referenced attribute of a decl is not associated with the
321 decl itself but with the assembler name. Remember the referenced
322 bits before clearing them. */
324 static void
325 save_assembler_name_reference_bit (void)
327 varpool_get_referenced_asm_ids (&referenced_asm_ids);
330 /* Clear the reference bits for assembler names before closing the
331 module scope. */
333 static void
334 clear_assembler_name_reference_bit (void)
336 varpool_clear_asm_id_reference_bit ();
339 /* Restore the reference bits for assembler names. */
341 static void
342 restore_assembler_name_reference_bit (void)
344 size_t i;
345 tree nm;
346 for (i = 0;
347 vec_safe_iterate (referenced_asm_ids, i, &nm);
348 ++i)
349 TREE_SYMBOL_REFERENCED (nm) = 1;
352 /* Set up the module scope before the parsing of the
353 associated source file. */
355 void
356 push_module_scope (void)
358 struct saved_module_scope *prev_module_scope;
360 if (!flag_dyn_ipa || !L_IPO_COMP_MODE)
362 parser_parsing_start = true;
363 return;
366 prev_module_scope = current_module_scope;
367 if (L_IPO_IS_PRIMARY_MODULE)
369 gcc_assert (!prev_module_scope);
370 lang_hooks.l_ipo.save_built_in_decl_pre_parsing ();
371 parser_parsing_start = true;
374 gcc_assert (current_module_id);
376 /* Set up the module scope. */
377 current_module_scope = get_module_scope (current_module_id);
378 return;
381 /* Restore the shared decls to their post parsing states. */
383 static void
384 restore_post_parsing_states (void)
386 current_module_id = primary_module_id;
387 current_module_scope = get_module_scope (primary_module_id);
388 set_funcdef_no (max_funcdef_no);
389 input_location = primary_module_last_loc;
391 restore_assembler_name_reference_bit ();
392 lang_hooks.l_ipo.restore_built_in_decl_post_module_parsing ();
395 /* Pop the current module scope (by clearing name bindings etc.)
396 and prepare for parsing of the next module. In particular,
397 built-in decls need to be restored to the state before file
398 parsing starts. */
400 void
401 pop_module_scope (void)
403 bool is_last = false;
404 int last_funcdef_no;
406 if (!flag_dyn_ipa || !L_IPO_COMP_MODE)
407 return;
409 gcc_assert (current_module_id && current_module_scope);
411 if (L_IPO_IS_PRIMARY_MODULE)
412 primary_module_last_loc = input_location;
414 at_eof = 1;
415 cgraph_process_same_body_aliases ();
416 lang_hooks.l_ipo.process_pending_decls (input_location);
417 lang_hooks.l_ipo.clear_deferred_fns ();
418 at_eof = 0;
420 is_last = is_last_module (current_module_id);
422 last_funcdef_no = get_last_funcdef_no ();
423 if (last_funcdef_no > max_funcdef_no)
424 max_funcdef_no = last_funcdef_no;
426 lang_hooks.l_ipo.save_built_in_decl_post_module_parsing ();
427 /* Save primary module state if needed (when module group
428 size > 1) */
429 if (L_IPO_IS_PRIMARY_MODULE && num_in_fnames > 1)
431 save_assembler_name_reference_bit ();
432 primary_module_last_funcdef_no = last_funcdef_no;
435 if (!is_last)
437 /* More aux modules are anticipated, clear
438 the parsing state. */
439 gcc_assert (num_in_fnames > 1);
440 clear_assembler_name_reference_bit ();
441 clear_module_scope_bindings (current_module_scope);
442 /* Restore symtab bindings for builtins */
443 lang_hooks.l_ipo.restore_built_in_decl_pre_parsing ();
444 /* The map can not be cleared because the names of operator
445 decls are used to store the information about the conversion
446 target type. This forces the coversion operator ids to be
447 incremented across different modules, and assember id must
448 be used for checksum computation. */
449 /* cp_clear_conv_type_map (); */
451 else if (num_in_fnames > 1)
453 clear_module_scope_bindings (current_module_scope);
454 restore_post_parsing_states ();
456 else
457 gcc_assert (L_IPO_IS_PRIMARY_MODULE && num_in_fnames == 1);
461 /* Type merging support for LIPO */
463 struct type_ec
465 tree rep_type;
466 vec<tree> *eq_types;
469 static vec<tree> *pending_types = NULL;
470 static struct pointer_set_t *type_set = NULL;
471 static htab_t type_hash_tab = NULL;
473 /* Hash function for the type table. */
475 static hashval_t
476 type_hash_hash (const void *ent)
478 tree type, name;
479 const struct type_ec *const entry
480 = (const struct type_ec *) ent;
482 type = entry->rep_type;
483 name = TYPE_NAME (type);
484 if (DECL_P (name))
485 name = DECL_NAME (name);
487 return htab_hash_string (IDENTIFIER_POINTER (name));
490 /* Equality function for type hash table. */
492 static int
493 type_hash_eq (const void *ent1, const void *ent2)
495 tree type1, type2;
496 const struct type_ec *const entry1
497 = (const struct type_ec *) ent1;
498 const struct type_ec *const entry2
499 = (const struct type_ec *) ent2;
501 type1 = entry1->rep_type;
502 type2 = entry2->rep_type;
504 return aggr_has_equiv_id (type1, type2);
507 /* Function to delete type hash entries. */
509 static void
510 type_hash_del (void *ent)
512 struct type_ec *const entry
513 = (struct type_ec *) ent;
515 vec_free (entry->eq_types);
516 free (entry);
519 struct GTY(()) type_ent
521 tree type;
522 unsigned eq_id;
525 static GTY ((param_is (type_ent))) htab_t l_ipo_type_tab = 0;
526 static unsigned l_ipo_eq_id = 0;
528 /* Address hash function for struct type_ent. */
530 static hashval_t
531 type_addr_hash (const void *ent)
533 const struct type_ent *const entry
534 = (const struct type_ent *) ent;
535 return (hashval_t) (uintptr_t) entry->type;
538 /* Address equality function for type_ent. */
540 static int
541 type_addr_eq (const void *ent1, const void *ent2)
543 const struct type_ent *const entry1
544 = (const struct type_ent *) ent1;
545 const struct type_ent *const entry2
546 = (const struct type_ent *) ent2;
547 return entry1->type == entry2->type;
550 /* Returns 1 if NS1 and NS2 refer to the same namespace. */
552 static int
553 is_ns_equiv (tree ns1, tree ns2)
555 tree n1, n2;
556 if (ns1 == NULL && ns2 == NULL)
557 return 1;
559 if ((!ns1 && ns2) || (ns1 && !ns2))
560 return 0;
562 gcc_assert (DECL_P (ns1) && DECL_P (ns2));
564 if (!is_ns_equiv (DECL_CONTEXT (ns1),
565 DECL_CONTEXT (ns2)))
566 return 0;
568 n1 = DECL_NAME (ns1);
569 n2 = DECL_NAME (ns2);
570 if (n1 == 0 && n2 == 0)
571 /* Conservative (which can happen when two NSes are from
572 different modules but with same UID) quivalence is allowed. */
573 return DECL_UID (ns1) == DECL_UID (ns2);
574 if (!n1 || !n2)
575 return 0;
577 if (!strcmp (IDENTIFIER_POINTER (n1),
578 IDENTIFIER_POINTER (n2)))
579 return 1;
581 return 0;
584 /* Returns 1 if aggregate type T1 and T2 have equivalent qualified
585 ids. */
587 static int
588 aggr_has_equiv_id (tree t1, tree t2)
590 int ctx_match;
591 tree ctx1, ctx2, tn1, tn2;
592 gcc_assert (TYPE_P (t1) && TYPE_P (t2));
594 ctx1 = TYPE_CONTEXT (t1);
595 ctx2 = TYPE_CONTEXT (t2);
597 if ((ctx1 && !ctx2) || (!ctx1 && ctx2))
598 return 0;
600 if (ctx1 && TREE_CODE (ctx1) != TREE_CODE (ctx2))
601 return 0;
603 if (ctx1 && (TREE_CODE (ctx1) == FUNCTION_DECL
604 || TREE_CODE (ctx1) == BLOCK))
605 return 0;
607 if (!ctx1)
609 ctx_match = 1;
610 gcc_assert (!ctx2);
612 else if (TREE_CODE (ctx1) == NAMESPACE_DECL)
613 ctx_match = is_ns_equiv (ctx1, ctx2);
614 else if (TYPE_P (ctx1))
615 ctx_match = aggr_has_equiv_id (ctx1, ctx2);
616 else
618 gcc_assert (TREE_CODE (ctx1) == TRANSLATION_UNIT_DECL);
619 ctx_match = 1;
622 if (!ctx_match)
623 return 0;
625 /* Now compare the name of the types. */
626 tn1 = TYPE_NAME (t1);
627 tn2 = TYPE_NAME (t2);
628 if ((tn1 && !tn2) || !(tn1 && tn2))
629 return 0;
630 else if (!tn1 && !tn2)
631 /* Be conservative on unamed types. */
632 return 1;
634 if (DECL_P (tn1))
635 tn1 = DECL_NAME (tn1);
636 if (DECL_P (tn2))
637 tn2 = DECL_NAME (tn2);
638 if (strcmp (IDENTIFIER_POINTER (tn1),
639 IDENTIFIER_POINTER (tn2)))
640 return 0;
642 return lang_hooks.l_ipo.cmp_lang_type (t1, t2);
645 /* Return the canonical type of the type's main variant. */
646 static inline tree
647 get_norm_type (const_tree type)
649 tree cano_type = TYPE_MAIN_VARIANT (type);
650 if (TYPE_CANONICAL (cano_type))
651 cano_type = TYPE_CANONICAL (cano_type);
653 return cano_type;
656 /* Return 1 if type T1 and T2 are equivalent. Struct/union/class
657 types are compared using qualified name ids. Alias sets of
658 equivalent types will be merged. Client code may choose to do
659 structural equivalence check for sanity. Note the difference
660 between the types_compatible_p (and its langhooks subroutines)
661 and this interface. The former is mainly used to remove useless
662 type conversion and value numbering computation. It returns 1
663 only when it is sure and should not be used in contexts where
664 erroneously returning 0 causes problems. This interface
665 lipo_cmp_type behaves differently - it returns 1 when it is not
666 sure -- as the primary purpose of the interface is for alias
667 set computation. */
670 lipo_cmp_type (tree t1, tree t2)
672 if (TREE_CODE (t1) != TREE_CODE (t2))
673 return 0;
674 if (TYPE_READONLY (t1) != TYPE_READONLY (t2))
675 return 0;
676 if (TYPE_VOLATILE (t1) != TYPE_VOLATILE (t2))
677 return 0;
679 t1 = get_norm_type (t1);
680 t2 = get_norm_type (t2);
682 switch (TREE_CODE (t1))
684 case RECORD_TYPE:
685 case UNION_TYPE:
686 case QUAL_UNION_TYPE:
687 return aggr_has_equiv_id (t1, t2);
689 case POINTER_TYPE:
690 case REFERENCE_TYPE:
691 case COMPLEX_TYPE:
692 case TYPE_PACK_EXPANSION:
693 return lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2));
694 case ARRAY_TYPE:
695 return (TYPE_DOMAIN (t1) == NULL || TYPE_DOMAIN (t2) == NULL
696 || (lipo_cmp_type (TYPE_DOMAIN (t1), TYPE_DOMAIN (t2))
697 && lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2))));
698 case METHOD_TYPE:
699 return lipo_cmp_type (TYPE_METHOD_BASETYPE (t1),
700 TYPE_METHOD_BASETYPE (t2));
701 case FUNCTION_TYPE:
703 tree arg1, arg2;
704 for (arg1 = TYPE_ARG_TYPES (t1), arg2 = TYPE_ARG_TYPES (t2);
705 arg1 && arg2;
706 arg1 = TREE_CHAIN (arg1), arg2 = TREE_CHAIN (arg2))
707 if (!lipo_cmp_type (TREE_VALUE (arg1),
708 TREE_VALUE (arg2)))
709 return 0;
710 if (arg1 || arg2)
711 return 0;
712 return 1;
714 case OFFSET_TYPE:
715 return lipo_cmp_type (TYPE_OFFSET_BASETYPE (t1),
716 TYPE_OFFSET_BASETYPE (t2));
717 case ENUMERAL_TYPE:
718 return lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2));
719 case REAL_TYPE:
720 case FIXED_POINT_TYPE:
721 case INTEGER_TYPE:
722 return (TYPE_PRECISION (t1) == TYPE_PRECISION (t2)
723 && TYPE_MODE (t1) == TYPE_MODE (t2)
724 && TYPE_MIN_VALUE (t1) == TYPE_MIN_VALUE (t2)
725 && TYPE_MAX_VALUE (t1) == TYPE_MAX_VALUE (t2));
726 case VECTOR_TYPE:
727 return (TYPE_VECTOR_SUBPARTS (t1) == TYPE_VECTOR_SUBPARTS (t2)
728 && lipo_cmp_type (TREE_TYPE (t1), TREE_TYPE (t2)));
729 case VOID_TYPE:
730 case BOOLEAN_TYPE:
731 case NULLPTR_TYPE:
732 return 1;
733 case TEMPLATE_TYPE_PARM:
734 return 1;
735 default:
736 gcc_unreachable ();
740 #ifndef ANON_AGGRNAME_PREFIX
741 #define ANON_AGGRNAME_PREFIX "__anon_"
742 #endif
743 #ifndef ANON_AGGRNAME_P
744 #define ANON_AGGRNAME_P(ID_NODE) \
745 (!strncmp (IDENTIFIER_POINTER (ID_NODE), ANON_AGGRNAME_PREFIX, \
746 sizeof (ANON_AGGRNAME_PREFIX) - 1))
747 #endif
749 /* Callback function used in tree walk to find referenced struct types. */
751 static tree
752 find_struct_types (tree *tp,
753 int *walk_subtrees ATTRIBUTE_UNUSED,
754 void *data ATTRIBUTE_UNUSED)
756 if (!(*tp))
757 return NULL_TREE;
759 if (TYPE_P (*tp))
761 if (lang_hooks.l_ipo.is_compiler_generated_type (*tp))
762 return NULL_TREE;
764 switch (TREE_CODE (*tp))
766 case RECORD_TYPE:
767 case UNION_TYPE:
768 case QUAL_UNION_TYPE:
770 tree cano_type, name;
771 tree context;
772 tree field;
774 cano_type = get_norm_type (*tp);
775 name = TYPE_NAME (cano_type);
776 if (!name)
778 /* the main variant of typedef of unnamed struct
779 has no name, use the orignal type for equivalence. */
780 cano_type = *tp;
781 name = TYPE_NAME (cano_type);
783 if (!name)
784 return NULL_TREE;
785 if (DECL_P (name)
786 && (DECL_IGNORED_P (name)
787 || ANON_AGGRNAME_P (DECL_NAME (name))))
788 return NULL_TREE;
790 if (!pointer_set_insert (type_set, cano_type))
791 pending_types->safe_push (cano_type);
792 else
793 return NULL_TREE; /* Or use walk tree without dups. */
795 context = TYPE_CONTEXT (cano_type);
796 if (context && TYPE_P (context))
797 walk_tree (&context, find_struct_types, NULL, NULL);
799 /* Instantiate a nested work as the tree walker does not
800 get to the fields. */
801 if (TYPE_BINFO (cano_type))
803 int i;
804 tree binfo, base_binfo;
806 for (binfo = TYPE_BINFO (cano_type), i = 0;
807 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
808 walk_tree (&BINFO_TYPE (base_binfo), find_struct_types,
809 NULL, NULL);
811 for (field = TYPE_FIELDS (cano_type);
812 field != 0;
813 field = TREE_CHAIN (field))
814 walk_tree (&TREE_TYPE (field), find_struct_types,
815 NULL, NULL);
816 return NULL_TREE;
818 default:
819 return NULL_TREE;
822 else if (DECL_P (*tp))
823 /* walk tree does not walk down decls, so do a nested walk here. */
824 walk_tree (&(TREE_TYPE (*tp)), find_struct_types, NULL, NULL);
826 return NULL_TREE;
829 /* Collect referenced struct types. */
831 static void
832 cgraph_collect_type_referenced (void)
834 basic_block bb;
835 gimple_stmt_iterator gi;
837 FOR_EACH_BB_FN (bb, cfun)
839 for (gi = gsi_start_bb (bb); !gsi_end_p (gi); gsi_next (&gi))
841 unsigned i;
842 gimple stmt = gsi_stmt (gi);
843 for (i = 0; i < gimple_num_ops (stmt); i++)
844 walk_tree (gimple_op_ptr (stmt, i), find_struct_types, NULL, NULL);
849 /* Check type equivalence. Returns 1 if T1 and T2 are equivalent
850 for tbaa; return 0 if not. -1 is returned if it is unknown. */
853 equivalent_struct_types_for_tbaa (const_tree t1, const_tree t2)
855 struct type_ent key, *tent1, *tent2, **slot;
857 if (!l_ipo_type_tab)
858 return -1;
860 t1 = get_norm_type (t1);
861 t2 = get_norm_type (t2);
863 key.type = (tree) (uintptr_t) t1;
864 slot = (struct type_ent **)
865 htab_find_slot (l_ipo_type_tab, &key, NO_INSERT);
866 if (!slot || !*slot)
867 return -1;
868 tent1 = *slot;
870 key.type = (tree) (uintptr_t) t2;
871 slot = (struct type_ent **)
872 htab_find_slot (l_ipo_type_tab, &key, NO_INSERT);
873 if (!slot || !*slot)
874 return -1;
875 tent2 = *slot;
877 return tent1->eq_id == tent2->eq_id;
880 /* Build type hash table. */
882 static void
883 cgraph_build_type_equivalent_classes (void)
885 unsigned n, i;
886 n = pending_types->length ();
887 for (i = 0; i < n; i++)
889 struct type_ec **slot;
890 struct type_ec te;
891 te.rep_type = (*pending_types)[i];
892 te.eq_types = NULL;
893 slot = (struct type_ec **) htab_find_slot (type_hash_tab,
894 &te, INSERT);
895 if (!*slot)
897 *slot = XCNEW (struct type_ec);
898 (*slot)->rep_type = te.rep_type;
899 vec_alloc ((*slot)->eq_types, 10);
901 (*slot)->eq_types->safe_push (te.rep_type);
905 /* Re-propagate component types's alias set to that of TYPE. PROCESSED
906 is the pointer set of processed types. */
908 static void
909 re_record_component_aliases (tree type,
910 struct pointer_set_t *processed)
912 alias_set_type superset = get_alias_set (type);
913 tree field;
915 if (superset == 0)
916 return;
918 if (pointer_set_insert (processed, type))
919 return;
921 switch (TREE_CODE (type))
923 case RECORD_TYPE:
924 case UNION_TYPE:
925 case QUAL_UNION_TYPE:
926 /* Recursively record aliases for the base classes, if there are any. */
927 if (TYPE_BINFO (type))
929 int i;
930 tree binfo, base_binfo;
932 for (binfo = TYPE_BINFO (type), i = 0;
933 BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
935 re_record_component_aliases (BINFO_TYPE (base_binfo),
936 processed);
937 record_alias_subset (superset,
938 get_alias_set (BINFO_TYPE (base_binfo)));
941 for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
942 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
944 re_record_component_aliases (TREE_TYPE (field), processed);
945 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
947 break;
949 case COMPLEX_TYPE:
950 re_record_component_aliases (TREE_TYPE (type), processed);
951 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
952 break;
954 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
955 element type. */
957 default:
958 break;
962 /* The callback function to merge alias sets of equivalent types. */
964 static int
965 type_eq_process (void **slot, void *data ATTRIBUTE_UNUSED)
967 unsigned i;
968 alias_set_type alias_set, ptr_alias_set = -1;
969 tree rep_type, type;
970 vec<tree> *eq_types;
971 struct type_ec ** te = (struct type_ec **)slot;
972 bool zero_set = false, ptr_zero_set = false;
973 struct type_ent **slot2, key, *tent;
976 rep_type = (*te)->rep_type;
977 eq_types = (*te)->eq_types;
978 alias_set = get_alias_set (rep_type);
980 for (i = 0; eq_types->iterate (i, &type); ++i)
982 alias_set_type als, ptr_als = -1;
983 tree type_ptr = TYPE_POINTER_TO (type);;
985 als = get_alias_set (type);
986 if (als == 0)
987 zero_set = true;
989 if (alias_set && als && alias_set != als)
990 record_alias_subset (alias_set, als);
992 if (type_ptr)
994 ptr_als = get_alias_set (type_ptr);
995 if (ptr_als == 0)
996 ptr_zero_set = true;
998 if (ptr_alias_set == -1)
999 ptr_alias_set = ptr_als;
1000 else
1002 if (!ptr_zero_set && ptr_alias_set != ptr_als)
1003 record_alias_subset (ptr_alias_set, ptr_als);
1008 /* Now propagate back. */
1009 for (i = 0; eq_types->iterate (i, &type); ++i)
1011 alias_set_type als, ptr_als;
1012 tree ptr_type = TYPE_POINTER_TO (type);
1014 als = get_alias_set (type);
1016 if (zero_set)
1017 TYPE_ALIAS_SET (type) = 0;
1018 else if (alias_set != als)
1019 record_alias_subset (als, alias_set);
1021 if (ptr_type)
1023 ptr_als = get_alias_set (ptr_type);
1024 if (ptr_zero_set)
1025 TYPE_ALIAS_SET (ptr_type) = 0;
1026 else if (ptr_alias_set != ptr_als)
1027 record_alias_subset (ptr_als, ptr_alias_set);
1032 /* Now populate the type table. */
1033 l_ipo_eq_id++;
1034 for (i = 0; eq_types->iterate (i, &type); ++i)
1036 key.type = type;
1037 slot2 = (struct type_ent **)
1038 htab_find_slot (l_ipo_type_tab, &key, INSERT);
1039 tent = *slot2;
1040 gcc_assert (!tent);
1041 tent = ggc_alloc_cleared_type_ent ();
1042 tent->type = key.type;
1043 tent->eq_id = l_ipo_eq_id;
1044 *slot2 = tent;
1047 return 1;
1050 /* Regenerate alias set for aggregate types. */
1052 static void
1053 record_components_for_parent_types (void)
1055 unsigned n, i;
1056 struct pointer_set_t *processed_types;
1058 processed_types = pointer_set_create ();
1059 n = pending_types->length ();
1060 for (i = 0; i < n; i++)
1062 tree type = (*pending_types)[i];
1063 re_record_component_aliases (type, processed_types);
1066 pointer_set_destroy (processed_types);
1069 /* Unify type alias sets for equivalent types. */
1071 void
1072 cgraph_unify_type_alias_sets (void)
1074 struct cgraph_node *node;
1075 struct varpool_node *pv;
1077 /* Only need to do type unification when we are in LIPO mode
1078 and have a non-trivial module group (size is >1). However,
1079 override the size check under non-zero PARAM_LIPO_RANDOM_GROUP_SIZE,
1080 which indicates that we are stress-testing LIPO. In that case
1081 try to flush out problems with type unification by always
1082 performing it. */
1083 if (!L_IPO_COMP_MODE
1084 || (num_in_fnames == 1
1085 && PARAM_VALUE (PARAM_LIPO_RANDOM_GROUP_SIZE) == 0))
1086 return;
1088 vec_alloc (pending_types, 100);
1089 type_set = pointer_set_create ();
1090 type_hash_tab = htab_create (10, type_hash_hash,
1091 type_hash_eq, type_hash_del);
1092 l_ipo_type_tab = htab_create_ggc (10, type_addr_hash,
1093 type_addr_eq, NULL);
1095 FOR_EACH_DEFINED_FUNCTION (node)
1097 if (!gimple_has_body_p (node->decl))
1098 continue;
1099 push_cfun (DECL_STRUCT_FUNCTION (node->decl));
1100 current_function_decl = node->decl;
1101 if (gimple_has_body_p (current_function_decl))
1102 cgraph_collect_type_referenced ();
1103 current_function_decl = NULL;
1104 set_cfun (NULL);
1105 pop_cfun ();
1108 FOR_EACH_VARIABLE (pv)
1109 walk_tree (&pv->decl, find_struct_types, NULL, NULL);
1111 /* Compute type equivalent classes. */
1112 cgraph_build_type_equivalent_classes ();
1113 /* Now unify alias sets of equivelent types. */
1114 htab_traverse (type_hash_tab, type_eq_process, NULL);
1115 /* Finally re-populating parent's alias set. */
1116 record_components_for_parent_types ();
1118 pointer_set_destroy (type_set);
1119 vec_free (pending_types);
1120 htab_delete (type_hash_tab);
1123 /* Return true if NODE->decl from an auxiliary module has external
1124 definition (and therefore is not needed for expansion). */
1126 bool
1127 cgraph_is_aux_decl_external (struct cgraph_node *node)
1129 tree decl = node->decl;
1131 if (!L_IPO_COMP_MODE)
1132 return false;
1134 if (!cgraph_is_auxiliary (decl))
1135 return false;
1137 /* Versioned clones from auxiliary moduels are not external. */
1138 if (node->is_versioned_clone)
1139 return false;
1141 /* Comdat or weak functions in aux modules are not external --
1142 there is no guarantee that the definitition will be emitted
1143 in the primary compilation of this auxiliary module.
1144 Functions marked artificial (e.g. an implicitly instantiated virtual
1145 destructor) are also not guaranteed to be available in the primary module,
1146 as they are not promoted by process_module_scope_static_func. */
1147 if (DECL_COMDAT (decl) || DECL_WEAK (decl) || DECL_ARTIFICIAL (decl))
1148 return false;
1150 /* virtual functions won't be deleted in the primary module. */
1151 if (DECL_VIRTUAL_P (decl))
1152 return true;
1154 if (!TREE_PUBLIC (decl))
1155 return false;
1157 /* The others from aux modules are external. */
1158 return true;
1161 /* Linked function symbol (cgraph node) table. */
1162 static GTY((param_is (cgraph_sym))) htab_t cgraph_symtab;
1164 /* This is true when global linking is needed and performed (for C++).
1165 For C, symbol linking is performed on the fly during parsing, and
1166 the cgraph_symtab is used only for keeping additional information
1167 for any already merged symbol if needed. */
1169 static bool global_link_performed = 0;
1171 /* For an external (non-defined) function DECL, return the primary
1172 module id (even though when the declaration is declared in an aux
1173 module). For a defined function DECL, return the module id in which
1174 it is defined. */
1176 unsigned
1177 cgraph_get_module_id (tree decl)
1179 struct function *func = DECL_STRUCT_FUNCTION (decl);
1180 /* Not defined. */
1181 if (!func)
1182 return primary_module_id;
1183 return FUNC_DECL_MODULE_ID (func);
1186 /* Return true if function decl is defined in an auxiliary module. */
1188 bool
1189 cgraph_is_auxiliary (tree decl)
1191 return (cgraph_get_module_id (decl) != primary_module_id);
1194 /* Return the hash value for cgraph_sym pointed to by P. The
1195 hash value is computed using function's assembler name. */
1197 static hashval_t
1198 hash_sym_by_assembler_name (const void *p)
1200 const struct cgraph_sym *n = (const struct cgraph_sym *) p;
1201 return (hashval_t) decl_assembler_name_hash (n->assembler_name);
1204 /* Return nonzero if P1 and P2 are equal. */
1206 static int
1207 eq_assembler_name (const void *p1, const void *p2)
1209 const struct cgraph_sym *n1 = (const struct cgraph_sym *) p1;
1210 const_tree name = (const_tree) p2;
1211 return (decl_assembler_name_equal (n1->rep_decl, name));
1214 /* Return the cgraph_sym for function declaration DECL. */
1216 static struct cgraph_sym **
1217 cgraph_sym (tree decl)
1219 struct cgraph_sym **slot;
1220 tree name;
1222 if (!cgraph_symtab)
1224 gcc_assert (!global_link_performed);
1225 return NULL;
1228 name = DECL_ASSEMBLER_NAME (decl);
1229 slot = (struct cgraph_sym **)
1230 htab_find_slot_with_hash (cgraph_symtab, name,
1231 decl_assembler_name_hash (name),
1232 NO_INSERT);
1233 return slot;
1236 /* Return the representative declaration for assembler name
1237 ASM_NAME. */
1239 tree
1240 cgraph_find_decl (tree asm_name)
1242 struct cgraph_sym **slot;
1243 if (!L_IPO_COMP_MODE)
1244 return NULL;
1245 if (!cgraph_symtab || !global_link_performed)
1246 return NULL;
1248 slot = (struct cgraph_sym **)
1249 htab_find_slot_with_hash (cgraph_symtab, asm_name,
1250 decl_assembler_name_hash (asm_name),
1251 NO_INSERT);
1252 if (!slot || !*slot)
1253 return NULL;
1255 return (*slot)->rep_node->decl;
1258 /* Return true if function declaration DECL is originally file scope
1259 static, which is promoted to global scope. */
1261 bool
1262 cgraph_is_promoted_static_func (tree decl)
1264 struct cgraph_sym ** sym;
1265 gcc_assert (L_IPO_COMP_MODE);
1267 /* cgraph_symtab will be created when any symbol got
1268 promoted. */
1269 if (!cgraph_symtab)
1270 return false;
1272 sym = cgraph_sym (decl);
1273 if (!sym)
1274 return false;
1275 return (*sym)->is_promoted_static;
1278 /* Hash function for module information table. ENT
1279 is a pointer to a cgraph_module_info. */
1281 static hashval_t
1282 htab_sym_hash (const void *ent)
1284 const struct cgraph_mod_info * const mi
1285 = (const struct cgraph_mod_info * const ) ent;
1286 return (hashval_t) mi->module_id;
1289 /* Hash equality function for module information table. */
1291 static int
1292 htab_sym_eq (const void *ent1, const void *ent2)
1294 const struct cgraph_mod_info * const mi1
1295 = (const struct cgraph_mod_info * const ) ent1;
1296 const struct cgraph_mod_info * const mi2
1297 = (const struct cgraph_mod_info * const ) ent2;
1298 return (mi1->module_id == mi2->module_id);
1301 /* cgraph_sym SYM may be defined in more than one source modules.
1302 Add declaration DECL's definiting module to SYM. */
1304 static void
1305 add_define_module (struct cgraph_sym *sym, tree decl)
1307 unsigned module_id;
1308 struct cgraph_mod_info **slot;
1309 struct cgraph_mod_info mi;
1311 struct function *f = DECL_STRUCT_FUNCTION (decl);
1312 if (!f)
1313 return;
1314 module_id = FUNC_DECL_MODULE_ID (f);
1316 if (!sym->def_module_hash)
1317 sym->def_module_hash
1318 = htab_create_ggc (10, htab_sym_hash, htab_sym_eq, NULL);
1320 mi.module_id = module_id;
1321 slot = (struct cgraph_mod_info **)htab_find_slot (sym->def_module_hash,
1322 &mi, INSERT);
1323 if (!*slot)
1325 *slot = ggc_alloc_cleared_cgraph_mod_info ();
1326 (*slot)->module_id = module_id;
1328 else
1329 gcc_assert ((*slot)->module_id == module_id);
1332 static int
1333 add_def_module (void **slot, void *data)
1335 struct cgraph_mod_info **m = (struct cgraph_mod_info **)slot;
1336 htab_t mod_set = (htab_t) data;
1337 struct cgraph_mod_info **new_slot;
1339 new_slot = (struct cgraph_mod_info **)htab_find_slot (mod_set, *m, INSERT);
1340 if (!*new_slot)
1342 *new_slot = ggc_alloc_cleared_cgraph_mod_info ();
1343 (*new_slot)->module_id = (*m)->module_id;
1345 else
1346 gcc_assert ((*new_slot)->module_id == (*m)->module_id);
1347 return 1;
1350 /* Clone defined module hash table from ORIG to CLONE. */
1352 void
1353 copy_defined_module_set (tree clone, tree orig)
1355 struct cgraph_sym **orig_sym, **clone_sym;
1357 orig_sym = cgraph_sym (orig);
1358 clone_sym = cgraph_sym (clone);
1359 if (!orig_sym || !(*orig_sym)->def_module_hash)
1360 return;
1361 if (!(*clone_sym)->def_module_hash)
1362 (*clone_sym)->def_module_hash
1363 = htab_create_ggc (10, htab_sym_hash, htab_sym_eq, NULL);
1364 htab_traverse ((*orig_sym)->def_module_hash, add_def_module, (*clone_sym)->def_module_hash);
1367 /* Return true if the symbol associated with DECL is defined in module
1368 MODULE_ID. This interface is used by the inliner to make sure profile-gen
1369 and profile-use pass (L-IPO mode) make consistent inline decision. */
1371 bool
1372 cgraph_is_inline_body_available_in_module (tree decl, unsigned module_id)
1374 struct cgraph_sym **sym;
1375 void **slot;
1376 struct cgraph_mod_info mi;
1378 gcc_assert (L_IPO_COMP_MODE);
1380 if (DECL_BUILT_IN (decl))
1381 return true;
1383 /* TODO: revisit this. */
1384 if (DECL_IN_SYSTEM_HEADER (decl) && DECL_DECLARED_INLINE_P (decl))
1385 return true;
1387 gcc_assert (TREE_STATIC (decl) || DECL_DECLARED_INLINE_P (decl));
1389 if (cgraph_get_module_id (decl) == module_id)
1390 return true;
1392 sym = cgraph_sym (decl);
1393 if (!sym || !(*sym)->def_module_hash)
1394 return false;
1396 mi.module_id = module_id;
1397 slot = htab_find_slot ((*sym)->def_module_hash, &mi, NO_INSERT);
1398 if (slot)
1400 gcc_assert (((struct cgraph_mod_info*)*slot)->module_id == module_id);
1401 return true;
1403 return false;
1406 /* Return the linked cgraph node using DECL's assembler name. DO_ASSERT
1407 is a flag indicating that a non null link target must be returned. */
1409 struct cgraph_node *
1410 cgraph_lipo_get_resolved_node_1 (tree decl, bool do_assert)
1412 struct cgraph_sym **slot;
1414 /* Handle alias decl. */
1415 slot = cgraph_sym (decl);
1417 if (!slot || !*slot)
1419 if (!do_assert)
1420 return NULL;
1421 else
1423 /* Nodes that are indirectly called are not 'reachable' in
1424 the callgraph. If they are not needed (comdat, inline
1425 extern etc), they may be removed from the link table
1426 before direct calls to them are exposed (via indirect
1427 call promtion by const folding etc). When this happens,
1428 the node will need to be relinked. A probably better fix
1429 is to modify the callgraph so that they are not eliminated
1430 in the first place -- this will allow inlining to happen. */
1432 struct cgraph_node *n = cgraph_get_create_node (decl);
1433 if (!n->analyzed)
1435 gcc_assert (DECL_EXTERNAL (decl)
1436 || cgraph_is_aux_decl_external (n)
1437 || DECL_VIRTUAL_P (decl));
1438 gcc_assert (/* This is the case for explicit extern instantiation,
1439 when cgraph node is not created before link. */
1440 DECL_EXTERNAL (decl));
1441 cgraph_link_node (n);
1442 return n;
1444 else
1445 gcc_unreachable ();
1448 else
1450 struct cgraph_sym *sym = *slot;
1451 return sym->rep_node;
1455 /* Return the cgraph_node of DECL if decl has definition; otherwise return
1456 the cgraph node of the representative decl, which is the declaration DECL
1457 is resolved to after linking/symbol resolution. */
1459 struct cgraph_node *
1460 cgraph_lipo_get_resolved_node (tree decl)
1462 struct cgraph_node *node = NULL;
1464 gcc_assert (L_IPO_COMP_MODE && global_link_performed);
1465 gcc_assert (cgraph_symtab);
1467 /* Never merged. */
1468 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl)
1469 /* builtin function decls are shared across modules, but 'linking'
1470 is still performed for them to keep track of the set of defining
1471 modules. Skip the real resolution here to avoid merging '__builtin_xxx'
1472 with 'xxx'. */
1473 || DECL_BUILT_IN (decl))
1474 return cgraph_get_create_node (decl);
1476 node = cgraph_lipo_get_resolved_node_1 (decl, true);
1477 return node;
1480 /* When NODE->decl is dead function eliminated,
1481 remove the entry in the link table. */
1483 void
1484 cgraph_remove_link_node (struct cgraph_node *node)
1486 tree name, decl;
1488 if (!L_IPO_COMP_MODE || !cgraph_symtab)
1489 return;
1491 decl = node->decl;
1493 /* Skip nodes that are not in the link table. */
1494 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
1495 return;
1497 /* Skip if node is an inline clone or if the node has
1498 defintion that is not really resolved to the merged node. */
1499 if (cgraph_lipo_get_resolved_node_1 (decl, false) != node)
1500 return;
1502 name = DECL_ASSEMBLER_NAME (decl);
1503 htab_remove_elt_with_hash (cgraph_symtab, name,
1504 decl_assembler_name_hash (name));
1507 /* Return true if the function body for DECL has profile information. */
1509 static bool
1510 has_profile_info (tree decl)
1512 gcov_type *ctrs = NULL;
1513 unsigned n;
1514 struct function* f = DECL_STRUCT_FUNCTION (decl);
1516 ctrs = get_coverage_counts_no_warn (f, GCOV_COUNTER_ARCS, &n);
1517 if (ctrs)
1519 unsigned i;
1520 for (i = 0; i < n; i++)
1521 if (ctrs[i])
1522 return true;
1525 return false;
1528 /* Resolve delaration NODE->decl for function symbol *SLOT. */
1530 static void
1531 resolve_cgraph_node (struct cgraph_sym **slot, struct cgraph_node *node)
1533 tree decl1, decl2;
1534 int decl1_defined = 0;
1535 int decl2_defined = 0;
1537 decl1 = (*slot)->rep_decl;
1538 decl2 = node->decl;
1540 decl1_defined = gimple_has_body_p (decl1);
1541 decl2_defined = gimple_has_body_p (decl2);
1543 if (decl1_defined && !decl2_defined)
1544 return;
1546 if (!decl1_defined && decl2_defined)
1548 (*slot)->rep_node = node;
1549 (*slot)->rep_decl = decl2;
1550 add_define_module (*slot, decl2);
1551 return;
1554 if (decl2_defined)
1556 bool has_prof1 = false;
1557 bool has_prof2 = false;
1558 gcc_assert (decl1_defined);
1559 add_define_module (*slot, decl2);
1561 /* Pick the node that cannot be removed, to avoid a situation
1562 where we remove the resolved node and later try to access
1563 it for the remaining non-removable copy. E.g. one may be
1564 extern and the other weak, only the extern copy can be removed. */
1565 if (cgraph_can_remove_if_no_direct_calls_and_refs_p ((*slot)->rep_node)
1566 && !cgraph_can_remove_if_no_direct_calls_and_refs_p (node))
1568 (*slot)->rep_node = node;
1569 (*slot)->rep_decl = decl2;
1570 return;
1572 /* Similarly, pick the non-external symbol, since external
1573 symbols may be eliminated by symtab_remove_unreachable_nodes
1574 after ipa inlining (see process_references). */
1575 if (DECL_EXTERNAL (decl1) && !DECL_EXTERNAL (decl2))
1577 (*slot)->rep_node = node;
1578 (*slot)->rep_decl = decl2;
1579 return;
1582 has_prof1 = has_profile_info (decl1);
1583 bool is_aux1 = cgraph_is_auxiliary (decl1);
1584 bool is_aux2 = cgraph_is_auxiliary (decl2);
1585 /* Pick the copy from the primary module if multiple copies
1586 have profile. */
1587 if (has_prof1 && (!is_aux1 || is_aux2))
1588 return;
1589 has_prof2 = has_profile_info (decl2);
1590 if (has_prof2)
1592 (*slot)->rep_node = node;
1593 (*slot)->rep_decl = decl2;
1595 return;
1598 /* Handle aliases properly. Make sure the alias symbol resolution
1599 is consistent with alias target */
1600 if (node->alias && !node->thunk.thunk_p)
1602 struct cgraph_node *decl2_tgt = cgraph_function_or_thunk_node (node, NULL);
1603 if (cgraph_lipo_get_resolved_node_1 (decl2_tgt->decl, false) == decl2_tgt)
1605 (*slot)->rep_node = node;
1606 (*slot)->rep_decl = decl2;
1609 return;
1613 /* Resolve NODE->decl in the function symbol table. */
1615 struct cgraph_sym *
1616 cgraph_link_node (struct cgraph_node *node)
1618 void **slot;
1619 tree name;
1621 if (!L_IPO_COMP_MODE)
1622 return NULL;
1624 if (!cgraph_symtab)
1625 return NULL;
1627 /* Skip the cases when the defintion can be locally resolved, and
1628 when we do not need to keep track of defining modules. */
1629 if (!TREE_PUBLIC (node->decl) || DECL_ARTIFICIAL (node->decl))
1630 return NULL;
1632 name = DECL_ASSEMBLER_NAME (node->decl);
1633 slot = htab_find_slot_with_hash (cgraph_symtab, name,
1634 decl_assembler_name_hash (name),
1635 INSERT);
1636 if (*slot)
1637 resolve_cgraph_node ((struct cgraph_sym **) slot, node);
1638 else
1640 struct cgraph_sym *sym = ggc_alloc_cleared_cgraph_sym ();
1641 sym->rep_node = node;
1642 sym->rep_decl = node->decl;
1643 sym->assembler_name = name;
1644 add_define_module (sym, node->decl);
1645 *slot = sym;
1647 return (struct cgraph_sym *) *slot;
1650 /* Perform cross module linking of function declarations. */
1652 void
1653 cgraph_do_link (void)
1655 struct cgraph_node *node;
1657 if (!L_IPO_COMP_MODE)
1658 return;
1660 global_link_performed = 1;
1661 gcc_assert (cgraph_pre_profiling_inlining_done);
1663 if (!cgraph_symtab)
1664 cgraph_symtab
1665 = htab_create_ggc (10, hash_sym_by_assembler_name,
1666 eq_assembler_name, NULL);
1668 FOR_EACH_FUNCTION (node)
1670 gcc_assert (!node->global.inlined_to);
1671 /* Delay aliases */
1672 if (node->alias && !node->thunk.thunk_p)
1673 continue;
1674 cgraph_link_node (node);
1677 /* Now handle aliases */
1678 FOR_EACH_FUNCTION (node)
1680 if (node->alias && !node->thunk.thunk_p)
1681 cgraph_link_node (node);
1685 struct promo_ent
1687 char* assemb_name;
1688 tree decl;
1689 int seq;
1692 /* Hash function for promo_ent table. */
1694 static hashval_t
1695 promo_ent_hash (const void *ent)
1697 const struct promo_ent *const entry
1698 = (const struct promo_ent *) ent;
1700 return htab_hash_string (entry->assemb_name);
1703 /* Hash_eq function for promo_ent table. */
1705 static int
1706 promo_ent_eq (const void *ent1, const void *ent2)
1708 const struct promo_ent *const entry1
1709 = (const struct promo_ent *) ent1;
1710 const struct promo_ent *const entry2
1711 = (const struct promo_ent *) ent2;
1712 if (!strcmp (entry1->assemb_name, entry2->assemb_name))
1713 return 1;
1714 return 0;
1717 /* Delete function for promo_ent hash table. */
1719 static void
1720 promo_ent_del (void *ent)
1722 struct promo_ent *const entry
1723 = (struct promo_ent *) ent;
1725 free (entry->assemb_name);
1726 free (entry);
1729 static htab_t promo_ent_hash_tab = NULL;
1731 /* Make the var decl for weak symbol as extern. */
1733 static inline void
1734 externalize_weak_decl (tree decl)
1736 gcc_assert (TREE_CODE (decl) == VAR_DECL && DECL_WEAK (decl));
1738 DECL_EXTERNAL (decl) = 1;
1739 TREE_STATIC (decl) = 0;
1740 DECL_INITIAL (decl) = NULL;
1742 /* Keep the context so that devirt_variable_node_removal_hook
1743 can do cleanup properly for vtables.
1744 DECL_CONTEXT (decl) = NULL; */
1747 /* Return a unique sequence number for NAME. This is needed to avoid
1748 name conflict -- function scope statics may have identical names.
1750 When DECL is NULL,
1751 this function returns a zero sequence number if it is called with
1752 a particular NAME for the first time, and non-zero otherwise.
1753 This fact is used to keep track of unseen weak variables.
1755 When DECL is not NULL, this function is supposed to be called by
1756 varpool_remove_duplicate_weak_decls. */
1758 static int
1759 get_name_seq_num (const char *name, tree decl)
1761 struct promo_ent **slot;
1762 struct promo_ent ent;
1763 int ret = 0;
1765 gcc_assert (!decl || TREE_CODE (decl) == VAR_DECL);
1766 ent.assemb_name = xstrdup (name);
1767 ent.seq = 0;
1769 slot = (struct promo_ent **)
1770 htab_find_slot (promo_ent_hash_tab, &ent, INSERT);
1772 if (!*slot)
1774 *slot = XCNEW (struct promo_ent);
1775 (*slot)->assemb_name = ent.assemb_name;
1776 (*slot)->decl = decl;
1778 else
1780 /* During output, the previously selected weak decl may not be
1781 referenced by any function that is expanded thus they do not have
1782 DECL_RTL_SET_P to be true and therefore can be eliminated by
1783 varpool_remove_unreferenced_decls later. To avoid that, logic is
1784 added to replace previously selected decl when needed. */
1785 if (decl && DECL_RTL_SET_P (decl)
1786 && !DECL_RTL_SET_P ((*slot)->decl))
1788 externalize_weak_decl ((*slot)->decl);
1789 (*slot)->decl = decl;
1790 ret = 0;
1792 else
1793 ret = ++(*slot)->seq;
1794 free (ent.assemb_name);
1796 return ret;
1799 /* Returns a unique assembler name for DECL. */
1801 static tree
1802 create_unique_name (tree decl, unsigned module_id)
1804 tree id, assemb_id;
1805 char *assembler_name;
1806 const char *name;
1807 struct function *context = NULL;
1808 int seq = 0;
1810 if (TREE_CODE (decl) == FUNCTION_DECL)
1812 if (!DECL_CONTEXT (decl)
1813 || TREE_CODE (DECL_CONTEXT (decl)) == TRANSLATION_UNIT_DECL)
1815 id = DECL_NAME (decl);
1816 /* if (IDENTIFIER_OPNAME_P (id)) */
1817 if (TREE_LANG_FLAG_2 (id))
1818 id = DECL_ASSEMBLER_NAME (decl);
1820 else
1821 id = DECL_ASSEMBLER_NAME (decl);
1823 else
1825 if (!DECL_CONTEXT (decl))
1826 id = DECL_NAME (decl);
1827 else if (TREE_CODE (DECL_CONTEXT (decl)) == NAMESPACE_DECL)
1828 id = DECL_ASSEMBLER_NAME (decl);
1829 else if (TREE_CODE (DECL_CONTEXT (decl)) == FUNCTION_DECL)
1831 id = DECL_NAME (decl);
1832 context = DECL_STRUCT_FUNCTION (DECL_CONTEXT (decl));
1834 else
1835 /* file scope context */
1836 id = DECL_NAME (decl);
1839 name = IDENTIFIER_POINTER (id);
1840 if (context)
1842 char *n;
1843 unsigned fno = FUNC_DECL_FUNC_ID (context);
1844 n = (char *)alloca (strlen (name) + 15);
1845 sprintf (n, "%s.%u", name, fno);
1846 name = n;
1849 assembler_name = (char*) alloca (strlen (name) + 30);
1850 sprintf (assembler_name, "%s.cmo.%u", name, module_id);
1851 seq = get_name_seq_num (assembler_name, NULL);
1852 if (seq)
1853 sprintf (assembler_name, "%s.%d", assembler_name, seq);
1855 assemb_id = get_identifier (assembler_name);
1857 return assemb_id;
1860 /* Promote DECL to be global. MODULE_ID is the id of the module where
1861 DECL is defined. IS_EXTERN is a flag indicating if externalization
1862 is needed. */
1864 static void
1865 promote_static_var_func (unsigned module_id, tree decl, bool is_extern)
1867 tree assemb_id;
1868 tree alias;
1870 /* No need to promote symbol alias. */
1871 alias = lookup_attribute ("alias", DECL_ATTRIBUTES (decl));
1872 if (alias)
1873 return;
1875 /* Function decls in C++ may contain characters not taken by assembler.
1876 Similarly, function scope static variable has UID as the assembler name
1877 suffix which is not consistent across modules. */
1878 assemb_id = create_unique_name (decl, module_id);
1880 if (DECL_ASSEMBLER_NAME_SET_P (decl))
1882 if (TREE_CODE (decl) == FUNCTION_DECL)
1883 unlink_from_assembler_name_hash (cgraph_get_create_node (decl),
1884 false);
1885 else
1886 unlink_from_assembler_name_hash (varpool_get_node (decl), false);
1889 SET_DECL_ASSEMBLER_NAME (decl, assemb_id);
1890 TREE_PUBLIC (decl) = 1;
1891 DECL_VISIBILITY (decl) = VISIBILITY_HIDDEN;
1892 DECL_VISIBILITY_SPECIFIED (decl) = 1;
1894 if (TREE_CODE (decl) == FUNCTION_DECL)
1896 struct cgraph_node *node = cgraph_get_create_node (decl);
1898 node->resolution = LDPR_UNKNOWN;
1899 insert_to_assembler_name_hash (node, false);
1901 else
1903 struct varpool_node *node = varpool_get_node (decl);
1904 node->resolution = LDPR_UNKNOWN;
1905 /* Statics from exported primary module are very likely
1906 referenced by other modules, so they should be made
1907 externally visible (to be avoided to be localized again).
1908 Another way to do this is to set force_output bit or
1909 change the logic in varpool_externally_visible in ipa.c. */
1910 if (!is_extern)
1912 node->resolution = LDPR_PREVAILING_DEF;
1913 node->externally_visible = true;
1915 varpool_link_node (node);
1916 insert_to_assembler_name_hash (node, false);
1917 /* Possibly update the RTL name as well. */
1918 if (DECL_RTL_SET_P (decl))
1919 XSTR (XEXP (DECL_RTL (decl), 0), 0) = IDENTIFIER_POINTER (assemb_id);
1922 if (is_extern)
1924 if (TREE_CODE (decl) == VAR_DECL)
1926 TREE_STATIC (decl) = 0;
1927 DECL_EXTERNAL (decl) = 1;
1928 /* Keep the initializer to allow const prop. */
1929 /* DECL_INITIAL (decl) = 0; */
1930 /* Keep the context so that devirt_variable_node_removal_hook
1931 can do cleanup properly for vtables.
1932 DECL_CONTEXT (decl) = 0; */
1934 /* else
1935 Function body will be deleted later before expansion. */
1937 else
1938 TREE_STATIC (decl) = 1;
1941 /* Externalize global variables from aux modules and promote
1942 static variables.
1943 WEAK variables are treated especially in
1944 varpool_remove_duplicate_weak_decls. */
1946 static void
1947 process_module_scope_static_var (struct varpool_node *vnode)
1949 tree decl = vnode->decl;
1951 if (varpool_is_auxiliary (vnode))
1953 gcc_assert (vnode->module_id != primary_module_id);
1954 if (TREE_PUBLIC (decl))
1956 /* Externalize non-weak variables. */
1957 if (!DECL_WEAK (decl))
1959 DECL_EXTERNAL (decl) = 1;
1960 TREE_STATIC (decl) = 0;
1961 /* Keep the initializer to allow const prop. */
1962 /* DECL_INITIAL (decl) = NULL; */
1963 if (DECL_CONTEXT (decl))
1965 DECL_ASSEMBLER_NAME (decl);
1967 /* Keep the context so that devirt_variable_node_removal_hook
1968 can do cleanup properly for vtables.
1969 DECL_CONTEXT (decl) = NULL; */
1972 else
1974 /* Promote static vars to global. */
1975 if (vnode->module_id)
1976 promote_static_var_func (vnode->module_id, decl,
1977 varpool_is_auxiliary (vnode));
1980 else
1982 if (PRIMARY_MODULE_EXPORTED && !TREE_PUBLIC (decl))
1983 promote_static_var_func (vnode->module_id, decl,
1984 varpool_is_auxiliary (vnode));
1988 /* Promote all aliases of CNODE. */
1990 static void
1991 promote_function_aliases (struct cgraph_node *cnode, unsigned mod_id,
1992 bool is_extern)
1994 int i;
1995 struct ipa_ref *ref;
1997 for (i = 0; ipa_ref_list_referring_iterate (&cnode->ref_list, i, ref);
1998 i++)
2000 if (ref->use == IPA_REF_ALIAS)
2002 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2003 tree alias_decl = alias->decl;
2004 /* Should assert */
2005 if (cgraph_get_module_id (alias_decl) == mod_id)
2006 promote_static_var_func (mod_id, alias_decl, is_extern);
2011 /* Promote static function CNODE->decl to be global. */
2013 static void
2014 process_module_scope_static_func (struct cgraph_node *cnode)
2016 tree decl = cnode->decl;
2017 bool addr_taken;
2018 unsigned mod_id;
2019 struct ipa_ref *ref;
2020 int i;
2022 if (TREE_PUBLIC (decl)
2023 || !TREE_STATIC (decl)
2024 || DECL_EXTERNAL (decl)
2025 || DECL_ARTIFICIAL (decl))
2026 return;
2028 if (flag_ripa_no_promote_always_inline
2029 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl)) != NULL)
2030 return;
2032 /* Can be local -- the promotion pass need to be done after
2033 callgraph build when address taken bit is set. */
2034 addr_taken = cnode->address_taken;
2035 if (!addr_taken)
2037 for (i = 0; ipa_ref_list_referring_iterate (&cnode->ref_list, i, ref);
2038 i++)
2039 if (ref->use == IPA_REF_ALIAS)
2041 struct cgraph_node *alias = ipa_ref_referring_node (ref);
2042 if (alias->address_taken)
2043 addr_taken = true;
2046 if (!addr_taken)
2048 tree assemb_id = create_unique_name (decl, cgraph_get_module_id (decl));
2050 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2051 unlink_from_assembler_name_hash (cnode, false);
2052 SET_DECL_ASSEMBLER_NAME (decl, assemb_id);
2053 insert_to_assembler_name_hash (cnode, false);
2054 return;
2057 mod_id = cgraph_get_module_id (decl);
2058 if (cgraph_is_auxiliary (decl))
2060 gcc_assert (mod_id != primary_module_id);
2061 /* Promote static function to global. */
2062 if (mod_id)
2064 promote_static_var_func (mod_id, decl, 1);
2065 promote_function_aliases (cnode, mod_id, 1);
2068 else
2070 if (PRIMARY_MODULE_EXPORTED
2071 /* skip static_init routines. */
2072 && !DECL_ARTIFICIAL (decl))
2074 promote_static_var_func (mod_id, decl, 0);
2076 promote_function_aliases (cnode, mod_id, 0);
2081 /* Process var_decls, func_decls with static storage. */
2083 void
2084 cgraph_process_module_scope_statics (void)
2086 struct cgraph_node *pf;
2087 struct varpool_node *pv;
2089 if (!L_IPO_COMP_MODE)
2090 return;
2092 promo_ent_hash_tab = htab_create (10, promo_ent_hash,
2093 promo_ent_eq, promo_ent_del);
2095 /* Process variable first. */
2096 FOR_EACH_DEFINED_VARIABLE (pv)
2097 process_module_scope_static_var (pv);
2099 FOR_EACH_FUNCTION (pf)
2100 process_module_scope_static_func (pf);
2102 htab_delete (promo_ent_hash_tab);
2105 /* There could be duplicate non-extern WEAK decls in the varpool queue,
2106 coming from different modules. All but one of these need to be externalized
2107 and removed from the varpool queue.
2108 Duplicate WEAK decls can be added to varpool queue as late as
2109 cgraph_expand_function, when a WEAK decl is marked referenced as assembler
2110 is being output. Therefore, a call to this function should be made after
2111 cgraph_expand_function. */
2113 void
2114 varpool_remove_duplicate_weak_decls (void)
2116 struct varpool_node *node = NULL;
2118 if (!L_IPO_COMP_MODE)
2119 return;
2121 promo_ent_hash_tab = htab_create (10, promo_ent_hash,
2122 promo_ent_eq, promo_ent_del);
2124 FOR_EACH_VARIABLE (node)
2126 tree decl = node->decl;
2128 if (TREE_PUBLIC (decl) && DECL_WEAK (decl) && !DECL_EXTERNAL (decl)
2129 && get_name_seq_num (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl)), decl))
2130 externalize_weak_decl (decl);
2133 htab_delete (promo_ent_hash_tab);
2136 static GTY((param_is (symtab_node))) htab_t varpool_symtab;
2138 /* Hash function for varpool node. */
2140 static hashval_t
2141 hash_node_by_assembler_name (const void *p)
2143 const struct varpool_node *n = (const struct varpool_node *) p;
2144 return (hashval_t) decl_assembler_name_hash (
2145 DECL_ASSEMBLER_NAME (n->decl));
2148 /* Returns nonzero if P1 and P2 are equal. */
2150 static int
2151 eq_node_assembler_name (const void *p1, const void *p2)
2153 const struct varpool_node *n1 = (const struct varpool_node *) p1;
2154 const_tree name = (const_tree)p2;
2155 return (decl_assembler_name_equal (n1->decl, name));
2158 /* Return true if NODE's decl is declared in an auxiliary module. */
2160 bool
2161 varpool_is_auxiliary (struct varpool_node *node)
2163 return (node->module_id
2164 && node->module_id != primary_module_id);
2167 /* Return the varpool_node to which DECL is resolved to during linking.
2168 This method can not be used after static to global promotion happens. */
2170 static struct varpool_node *
2171 real_varpool_node_1 (tree decl, bool assert)
2173 void **slot;
2174 tree name;
2176 if (!L_IPO_COMP_MODE || !varpool_symtab)
2177 return varpool_get_node (decl);
2179 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
2180 return varpool_get_node (decl);
2182 name = DECL_ASSEMBLER_NAME (decl);
2183 slot = htab_find_slot_with_hash (varpool_symtab, name,
2184 decl_assembler_name_hash (name),
2185 NO_INSERT);
2186 if (!slot)
2188 gcc_assert (!assert);
2189 return NULL;
2192 gcc_assert (slot && *slot);
2193 return (struct varpool_node *)*slot;
2196 struct varpool_node *
2197 real_varpool_node (tree decl)
2199 return real_varpool_node_1 (decl, true);
2202 /* Remove NODE from the link table. */
2204 void
2205 varpool_remove_link_node (struct varpool_node *node)
2207 tree name;
2208 tree decl;
2210 if (!L_IPO_COMP_MODE || !varpool_symtab)
2211 return;
2213 decl = node->decl;
2215 if (!TREE_PUBLIC (decl) || DECL_ARTIFICIAL (decl))
2216 return;
2218 if (real_varpool_node_1 (decl, false) != node)
2219 return;
2221 name = DECL_ASSEMBLER_NAME (decl);
2222 htab_remove_elt_with_hash (varpool_symtab, name,
2223 decl_assembler_name_hash (name));
2226 /* Merge the addressable attribute from DECL2 to DECL1. */
2228 static inline void
2229 merge_addressable_attr (tree decl1, tree decl2)
2231 if (TREE_ADDRESSABLE (decl2))
2232 TREE_ADDRESSABLE (decl1) = 1;
2235 /* Resolve NODE->decl to symbol table entry *SLOT. */
2237 static void
2238 resolve_varpool_node (struct varpool_node **slot, struct varpool_node *node)
2240 tree decl1, decl2;
2242 decl1 = (*slot)->decl;
2243 decl2 = node->decl;
2245 /* Take the decl with the complete type. */
2246 if (COMPLETE_TYPE_P (TREE_TYPE (decl1))
2247 && !COMPLETE_TYPE_P (TREE_TYPE (decl2)))
2249 merge_addressable_attr (decl1, decl2);
2250 return;
2252 if (!COMPLETE_TYPE_P (TREE_TYPE (decl1))
2253 && COMPLETE_TYPE_P (TREE_TYPE (decl2)))
2255 *slot = node;
2256 merge_addressable_attr (decl2, decl1);
2257 return;
2260 if (DECL_INITIAL (decl1) && !DECL_INITIAL (decl2))
2262 merge_addressable_attr (decl1, decl2);
2263 return;
2266 if (!DECL_INITIAL (decl1) && DECL_INITIAL (decl2))
2268 *slot = node;
2269 merge_addressable_attr (decl2, decl1);
2270 return;
2273 /* Either all complete or neither's type is complete. Just
2274 pick the primary module's decl. */
2275 if (!varpool_is_auxiliary (*slot))
2277 merge_addressable_attr (decl1, decl2);
2278 return;
2281 if (!varpool_is_auxiliary (node))
2283 *slot = node;
2284 merge_addressable_attr (decl2, decl1);
2285 return;
2288 merge_addressable_attr (decl1, decl2);
2289 return;
2292 /* Link NODE into var_decl symbol table. */
2294 void
2295 varpool_link_node (struct varpool_node *node)
2297 tree name;
2298 void **slot;
2300 if (!L_IPO_COMP_MODE || !varpool_symtab)
2301 return;
2303 if (!TREE_PUBLIC (node->decl) || DECL_ARTIFICIAL (node->decl))
2304 return;
2306 name = DECL_ASSEMBLER_NAME (node->decl);
2307 slot = htab_find_slot_with_hash (varpool_symtab, name,
2308 decl_assembler_name_hash (name),
2309 INSERT);
2310 if (*slot)
2311 resolve_varpool_node ((struct varpool_node **) slot, node);
2312 else
2313 *slot = node;
2316 /* Fixup references of VNODE. */
2318 static void
2319 fixup_reference_list (struct varpool_node *node)
2321 int i;
2322 struct ipa_ref *ref;
2323 struct ipa_ref_list *list = &node->ref_list;
2324 vec<symtab_node *> new_refered;
2325 vec<int> new_refered_type;
2326 struct symtab_node *sym_node;
2327 enum ipa_ref_use use_type = IPA_REF_LOAD;
2329 new_refered.create (10);
2330 new_refered_type.create (10);
2331 for (i = 0; ipa_ref_list_reference_iterate (list, i, ref); i++)
2333 if (is_a <cgraph_node> (ref->referred))
2335 struct cgraph_node *cnode = ipa_ref_node (ref);
2336 struct cgraph_node *r_cnode
2337 = cgraph_lipo_get_resolved_node (cnode->decl);
2338 new_refered.safe_push (r_cnode);
2339 use_type = ref->use;
2340 new_refered_type.safe_push ((int) use_type);
2341 gcc_assert (use_type != IPA_REF_ADDR
2342 || cnode->global.inlined_to
2343 || cnode->address_taken);
2344 if (use_type == IPA_REF_ADDR)
2345 cgraph_mark_address_taken_node (r_cnode);
2347 else if (is_a <varpool_node> (ref->referred))
2349 struct varpool_node *var = ipa_ref_varpool_node (ref);
2350 struct varpool_node *r_var = real_varpool_node (var->decl);
2351 new_refered.safe_push (r_var);
2352 use_type = ref->use;
2353 new_refered_type.safe_push ((int) use_type);
2355 else
2356 gcc_assert (false);
2358 ipa_remove_all_references (&node->ref_list);
2359 for (i = 0; new_refered.iterate (i, &sym_node); ++i)
2361 ipa_record_reference (node, sym_node,
2362 (enum ipa_ref_use) new_refered_type[i], NULL);
2366 /* Perform cross module linking for var_decls. */
2368 void
2369 varpool_do_link (void)
2371 struct varpool_node *node;
2373 if (!L_IPO_COMP_MODE)
2374 return;
2376 varpool_symtab
2377 = htab_create_ggc (10, hash_node_by_assembler_name,
2378 eq_node_assembler_name, NULL);
2379 FOR_EACH_VARIABLE (node)
2380 varpool_link_node (node);
2382 /* Merge the externally visible attribute. */
2383 FOR_EACH_VARIABLE (node)
2385 if (node->externally_visible)
2386 (real_varpool_node (node->decl))->externally_visible = true;
2387 fixup_reference_list (node);
2391 /* Get the list of assembler name ids with reference bit set. */
2393 void
2394 varpool_get_referenced_asm_ids (vec<tree,va_gc> **ids)
2396 struct varpool_node *node;
2397 FOR_EACH_VARIABLE (node)
2399 tree asm_id = NULL;
2400 tree decl = node->decl;
2401 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2403 asm_id = DECL_ASSEMBLER_NAME (decl);
2404 vec_safe_push (*ids, asm_id);
2409 /* Clear the referenced bit in all assembler ids. */
2411 void
2412 varpool_clear_asm_id_reference_bit (void)
2414 struct varpool_node *node;
2415 FOR_EACH_VARIABLE (node)
2417 tree asm_id = NULL;
2418 tree decl = node->decl;
2419 if (DECL_ASSEMBLER_NAME_SET_P (decl))
2421 asm_id = DECL_ASSEMBLER_NAME (decl);
2422 TREE_SYMBOL_REFERENCED (asm_id) = 0;
2428 #include "gt-l-ipo.h"