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
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
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/>. */
23 #include "coretypes.h"
25 #include "c-family/c-common.h"
27 #include "langhooks.h"
28 #include "langhooks-def.h"
29 #include "diagnostic.h"
36 #include "tree-pretty-print.h"
38 struct GTY(()) saved_module_scope
40 VEC(tree
, gc
) *module_decls
;
44 static GTY (()) struct saved_module_scope
*current_module_scope
;
45 static GTY ((param_is (struct saved_module_scope
))) htab_t saved_module_scope_map
;
46 static int primary_module_last_funcdef_no
= 0;
47 /* Function id space for each module are qualified by the module id. After all the files
48 are parsed, we need to reset the funcdef_no to the max value from all module so that
49 the function clones do not assigned with ids colliding with some other orignal function
50 in the same module. */
51 static int max_funcdef_no
= 0;
52 static location_t primary_module_last_loc
;
53 /* Primary module pending templates. */
54 /* Referenced asm ids in primary module. */
55 static GTY (()) VEC(tree
, gc
) *referenced_asm_ids
= NULL
;
56 bool parser_parsing_start
= false;
57 /* Nonzero if we're done parsing and into end-of-file activities. */
60 static int aggr_has_equiv_id (tree t1
, tree t2
);
62 /* Module scope hash function. */
65 htab_module_scope_hash (const void *ent
)
67 const struct saved_module_scope
*const entry
68 = (const struct saved_module_scope
*) ent
;
69 return (hashval_t
) entry
->module_id
;
72 /* Module scope equality function. */
75 htab_module_scope_eq (const void *ent1
, const void *ent2
)
77 const struct saved_module_scope
*const entry1
78 = (const struct saved_module_scope
*) ent1
;
79 const struct saved_module_scope
*const entry2
80 = (const struct saved_module_scope
*) ent2
;
82 return entry1
->module_id
== entry2
->module_id
;
85 /* Returns the module scope given a module id MOD_ID. */
87 static struct saved_module_scope
*
88 get_module_scope (unsigned mod_id
)
90 struct saved_module_scope
**slot
, key
, *module_scope
;
94 if (saved_module_scope_map
== NULL
)
95 saved_module_scope_map
= htab_create_ggc (10, htab_module_scope_hash
,
96 htab_module_scope_eq
, NULL
);
97 key
.module_id
= mod_id
;
98 slot
= (struct saved_module_scope
**)
99 htab_find_slot (saved_module_scope_map
, &key
, INSERT
);
100 module_scope
= *slot
;
103 module_scope
= ggc_alloc_cleared_saved_module_scope ();
104 module_scope
->module_id
= mod_id
;
105 *slot
= module_scope
;
110 /* Allocate memory for struct lang_decl for tree T. */
112 static struct lang_decl
*
113 alloc_lang_decl (tree t
)
116 size
= lang_hooks
.l_ipo
.get_lang_decl_size (t
);
117 return ggc_alloc_cleared_lang_decl (size
);
120 /* Return a cloned copy of tree SRC. */
123 lipo_save_decl (tree src
)
125 tree saved
= copy_node (src
);
126 enum tree_code tc
= TREE_CODE (src
);
127 if (TREE_CODE_CLASS (tc
) == tcc_declaration
)
129 struct lang_decl
*ls
= NULL
;
130 struct function
*func
= NULL
;
131 DECL_CONTEXT (saved
) = DECL_CONTEXT (src
);
132 if (DECL_LANG_SPECIFIC (src
))
134 ls
= alloc_lang_decl (src
);
135 memcpy (ls
, DECL_LANG_SPECIFIC (src
),
136 lang_hooks
.l_ipo
.get_lang_decl_size (src
));
138 DECL_LANG_SPECIFIC (saved
) = ls
;
139 if (tc
== FUNCTION_DECL
&& DECL_STRUCT_FUNCTION (src
))
141 func
= ggc_alloc_cleared_function ();
142 *func
= *(DECL_STRUCT_FUNCTION (src
));
143 DECL_STRUCT_FUNCTION (saved
) = func
;
148 gcc_assert (TREE_CODE_CLASS (tc
) == tcc_type
&&
149 TYPE_MAIN_VARIANT (src
) == src
);
150 TYPE_CONTEXT (saved
) = TYPE_CONTEXT (src
);
151 lang_hooks
.l_ipo
.dup_lang_type (src
, saved
);
157 /* Copy tree SAVED to tree DEST. */
160 lipo_restore_decl (tree dest
, tree saved
)
164 struct lang_decl
*oldls
;
166 tc
= TREE_CODE (saved
);
167 if (TREE_CODE_CLASS (tc
) == tcc_declaration
)
169 struct function
*oldfunc
= NULL
;
170 old_uid
= DECL_UID (dest
);
171 oldls
= DECL_LANG_SPECIFIC (dest
);
173 = (tc
== FUNCTION_DECL
? DECL_STRUCT_FUNCTION (dest
) : NULL
);
175 memcpy ((char *) dest
+ sizeof (struct tree_common
),
176 (char *) saved
+ sizeof (struct tree_common
),
177 sizeof (struct tree_decl_common
) - sizeof (struct tree_common
));
179 if (tc
== FUNCTION_DECL
)
180 memcpy ((char *) dest
+ sizeof (struct tree_decl_common
),
181 (char *) saved
+ sizeof (struct tree_decl_common
),
182 sizeof (struct tree_function_decl
)
183 - sizeof (struct tree_decl_common
));
185 DECL_UID (dest
) = old_uid
;
186 if (DECL_LANG_SPECIFIC (saved
))
189 oldls
= alloc_lang_decl (dest
);
190 memcpy (oldls
, DECL_LANG_SPECIFIC (saved
),
191 lang_hooks
.l_ipo
.get_lang_decl_size (saved
));
192 DECL_LANG_SPECIFIC (dest
) = oldls
;
195 DECL_LANG_SPECIFIC (dest
) = NULL
;
197 if (tc
== FUNCTION_DECL
)
199 if (DECL_STRUCT_FUNCTION (saved
))
202 oldfunc
= ggc_alloc_cleared_function ();
203 *oldfunc
= *(DECL_STRUCT_FUNCTION (saved
));
204 DECL_STRUCT_FUNCTION (dest
) = oldfunc
;
207 DECL_STRUCT_FUNCTION (dest
) = NULL
;
212 gcc_assert (TREE_CODE_CLASS (tc
) == tcc_type
);
213 lang_hooks
.l_ipo
.copy_lang_type (saved
, dest
);
218 /* Return the name for tree TD which is either a decl or type. */
221 get_type_or_decl_name (tree td
)
236 /* For a DECL (a type or a decl) in SCOPE, check to see if it is in
237 global or namespace scope. If yes, add it to the current module scope. */
240 add_decl_to_current_module_scope (tree decl
, void *scope
)
242 struct saved_module_scope
*module_scope
;
248 if (!parser_parsing_start
)
250 /* The source file may contains only global variable declations
251 -- there is no module grouping data associated with it, so
252 neither primary_module_id nor current_module_id is set. */
253 lang_hooks
.l_ipo
.add_built_in_decl (decl
);
257 if (!L_IPO_COMP_MODE
)
260 if (!lang_hooks
.l_ipo
.has_global_name (decl
, scope
))
263 /* Unlike C++ where names are attached to type decls, for C, the type name
264 is identifier node. Thus we need to track type names as well. */
265 id
= get_type_or_decl_name (decl
);
269 module_scope
= current_module_scope
;
270 gcc_assert (module_scope
&& module_scope
->module_id
== current_module_id
);
271 VEC_safe_push (tree
, gc
, module_scope
->module_decls
, decl
);
274 /* Clear name bindings for all decls created in MODULE_SCOPE. */
277 clear_module_scope_bindings (struct saved_module_scope
*module_scope
)
283 VEC_iterate (tree
, module_scope
->module_decls
, i
, decl
);
286 lang_hooks
.l_ipo
.clear_global_name_bindings (
287 get_type_or_decl_name (decl
));
288 /* Now force creating assembly name. */
289 if (VAR_OR_FUNCTION_DECL_P (decl
))
293 assembler_name
= DECL_ASSEMBLER_NAME (decl
);
294 lang_hooks
.l_ipo
.clear_global_name_bindings (assembler_name
);
299 /* The referenced attribute of a decl is not associated with the
300 decl itself but with the assembler name. Remember the referenced
301 bits before clearing them. */
304 save_assembler_name_reference_bit (void)
306 varpool_get_referenced_asm_ids (&referenced_asm_ids
);
309 /* Clear the reference bits for assembler names before closing the
313 clear_assembler_name_reference_bit (void)
315 varpool_clear_asm_id_reference_bit ();
318 /* Restore the reference bits for assembler names. */
321 restore_assembler_name_reference_bit (void)
326 VEC_iterate (tree
, referenced_asm_ids
, i
, nm
);
328 TREE_SYMBOL_REFERENCED (nm
) = 1;
331 /* Set up the module scope before the parsing of the
332 associated source file. */
335 push_module_scope (void)
337 struct saved_module_scope
*prev_module_scope
;
339 if (!flag_dyn_ipa
|| !L_IPO_COMP_MODE
)
341 parser_parsing_start
= true;
345 prev_module_scope
= current_module_scope
;
346 if (L_IPO_IS_PRIMARY_MODULE
)
348 gcc_assert (!prev_module_scope
);
349 lang_hooks
.l_ipo
.save_built_in_decl_pre_parsing ();
350 parser_parsing_start
= true;
353 gcc_assert (current_module_id
);
355 /* Set up the module scope. */
356 current_module_scope
= get_module_scope (current_module_id
);
360 /* Restore the shared decls to their post parsing states. */
363 restore_post_parsing_states (void)
365 current_module_id
= primary_module_id
;
366 current_module_scope
= get_module_scope (primary_module_id
);
367 set_funcdef_no (max_funcdef_no
);
368 input_location
= primary_module_last_loc
;
370 restore_assembler_name_reference_bit ();
371 lang_hooks
.l_ipo
.restore_built_in_decl_post_module_parsing ();
374 /* Pop the current module scope (by clearing name bindings etc.)
375 and prepare for parsing of the next module. In particular,
376 built-in decls need to be restored to the state before file
380 pop_module_scope (void)
382 bool is_last
= false;
385 if (!flag_dyn_ipa
|| !L_IPO_COMP_MODE
)
388 gcc_assert (current_module_id
&& current_module_scope
);
390 if (L_IPO_IS_PRIMARY_MODULE
)
391 primary_module_last_loc
= input_location
;
394 lang_hooks
.l_ipo
.process_pending_decls (input_location
);
395 lang_hooks
.l_ipo
.clear_deferred_fns ();
398 is_last
= is_last_module (current_module_id
);
400 last_funcdef_no
= get_last_funcdef_no ();
401 if (last_funcdef_no
> max_funcdef_no
)
402 max_funcdef_no
= last_funcdef_no
;
404 lang_hooks
.l_ipo
.save_built_in_decl_post_module_parsing ();
405 /* Save primary module state if needed (when module group
407 if (L_IPO_IS_PRIMARY_MODULE
&& num_in_fnames
> 1)
409 save_assembler_name_reference_bit ();
410 primary_module_last_funcdef_no
= last_funcdef_no
;
415 /* More aux modules are anticipated, clear
416 the parsing state. */
417 gcc_assert (num_in_fnames
> 1);
418 clear_assembler_name_reference_bit ();
419 clear_module_scope_bindings (current_module_scope
);
420 /* Restore symtab bindings for builtins */
421 lang_hooks
.l_ipo
.restore_built_in_decl_pre_parsing ();
422 /* The map can not be cleared because the names of operator
423 decls are used to store the information about the conversion
424 target type. This forces the coversion operator ids to be
425 incremented across different modules, and assember id must
426 be used for checksum computation. */
427 /* cp_clear_conv_type_map (); */
429 else if (num_in_fnames
> 1)
431 clear_module_scope_bindings (current_module_scope
);
432 restore_post_parsing_states ();
435 gcc_assert (L_IPO_IS_PRIMARY_MODULE
&& num_in_fnames
== 1);
439 /* Type merging support for LIPO */
444 VEC(tree
, heap
) *eq_types
;
447 static VEC(tree
, heap
) *pending_types
= NULL
;
448 static struct pointer_set_t
*type_set
= NULL
;
449 static htab_t type_hash_tab
= NULL
;
451 /* Hash function for the type table. */
454 type_hash_hash (const void *ent
)
457 const struct type_ec
*const entry
458 = (const struct type_ec
*) ent
;
460 type
= entry
->rep_type
;
461 name
= TYPE_NAME (type
);
463 name
= DECL_NAME (name
);
465 return htab_hash_string (IDENTIFIER_POINTER (name
));
468 /* Equality function for type hash table. */
471 type_hash_eq (const void *ent1
, const void *ent2
)
474 const struct type_ec
*const entry1
475 = (const struct type_ec
*) ent1
;
476 const struct type_ec
*const entry2
477 = (const struct type_ec
*) ent2
;
479 type1
= entry1
->rep_type
;
480 type2
= entry2
->rep_type
;
482 return aggr_has_equiv_id (type1
, type2
);
485 /* Function to delete type hash entries. */
488 type_hash_del (void *ent
)
490 struct type_ec
*const entry
491 = (struct type_ec
*) ent
;
493 VEC_free (tree
, heap
, entry
->eq_types
);
497 struct GTY(()) type_ent
503 static GTY ((param_is (struct type_ent
))) htab_t l_ipo_type_tab
= 0;
504 static unsigned l_ipo_eq_id
= 0;
506 /* Address hash function for struct type_ent. */
509 type_addr_hash (const void *ent
)
511 const struct type_ent
*const entry
512 = (const struct type_ent
*) ent
;
513 return (hashval_t
) (long) entry
->type
;
516 /* Address equality function for type_ent. */
519 type_addr_eq (const void *ent1
, const void *ent2
)
521 const struct type_ent
*const entry1
522 = (const struct type_ent
*) ent1
;
523 const struct type_ent
*const entry2
524 = (const struct type_ent
*) ent2
;
525 return entry1
->type
== entry2
->type
;
528 /* Returns 1 if NS1 and NS2 refer to the same namespace. */
531 is_ns_equiv (tree ns1
, tree ns2
)
534 if (ns1
== NULL
&& ns2
== NULL
)
537 if ((!ns1
&& ns2
) || (ns1
&& !ns2
))
540 gcc_assert (DECL_P (ns1
) && DECL_P (ns2
));
542 if (!is_ns_equiv (DECL_CONTEXT (ns1
),
546 n1
= DECL_NAME (ns1
);
547 n2
= DECL_NAME (ns2
);
548 if (n1
== 0 && n2
== 0)
549 /* Conservative (which can happen when two NSes are from
550 different modules but with same UID) quivalence is allowed. */
551 return DECL_UID (ns1
) == DECL_UID (ns2
);
555 if (!strcmp (IDENTIFIER_POINTER (n1
),
556 IDENTIFIER_POINTER (n2
)))
562 /* Returns 1 if aggregate type T1 and T2 have equivalent qualified
566 aggr_has_equiv_id (tree t1
, tree t2
)
569 tree ctx1
, ctx2
, tn1
, tn2
;
570 gcc_assert (TYPE_P (t1
) && TYPE_P (t2
));
572 ctx1
= TYPE_CONTEXT (t1
);
573 ctx2
= TYPE_CONTEXT (t2
);
575 if ((ctx1
&& !ctx2
) || (!ctx1
&& ctx2
))
578 if (ctx1
&& TREE_CODE (ctx1
) != TREE_CODE (ctx2
))
581 if (ctx1
&& (TREE_CODE (ctx1
) == FUNCTION_DECL
582 || TREE_CODE (ctx1
) == BLOCK
))
590 else if (TREE_CODE (ctx1
) == NAMESPACE_DECL
)
591 ctx_match
= is_ns_equiv (ctx1
, ctx2
);
592 else if (TYPE_P (ctx1
))
593 ctx_match
= aggr_has_equiv_id (ctx1
, ctx2
);
596 gcc_assert (TREE_CODE (ctx1
) == TRANSLATION_UNIT_DECL
);
603 /* Now compare the name of the types. */
604 tn1
= TYPE_NAME (t1
);
605 tn2
= TYPE_NAME (t2
);
606 if ((tn1
&& !tn2
) || !(tn1
&& tn2
))
608 else if (!tn1
&& !tn2
)
609 /* Be conservative on unamed types. */
613 tn1
= DECL_NAME (tn1
);
615 tn2
= DECL_NAME (tn2
);
616 if (strcmp (IDENTIFIER_POINTER (tn1
),
617 IDENTIFIER_POINTER (tn2
)))
620 return lang_hooks
.l_ipo
.cmp_lang_type (t1
, t2
);
623 /* Return the canonical type of the type's main variant. */
625 get_norm_type (const_tree type
)
627 tree cano_type
= TYPE_MAIN_VARIANT (type
);
628 if (TYPE_CANONICAL (cano_type
))
629 cano_type
= TYPE_CANONICAL (cano_type
);
634 /* Return 1 if type T1 and T2 are equivalent. Struct/union/class
635 types are compared using qualified name ids. Alias sets of
636 equivalent types will be merged. Client code may choose to do
637 structural equivalence check for sanity. Note the difference
638 between the types_compatible_p (and its langhooks subroutines)
639 and this interface. The former is mainly used to remove useless
640 type conversion and value numbering computation. It returns 1
641 only when it is sure and should not be used in contexts where
642 erroneously returning 0 causes problems. This interface
643 lipo_cmp_type behaves differently - it returns 1 when it is not
644 sure -- as the primary purpose of the interface is for alias
648 lipo_cmp_type (tree t1
, tree t2
)
650 if (TREE_CODE (t1
) != TREE_CODE (t2
))
652 if (TYPE_READONLY (t1
) != TYPE_READONLY (t2
))
654 if (TYPE_VOLATILE (t1
) != TYPE_VOLATILE (t2
))
657 t1
= get_norm_type (t1
);
658 t2
= get_norm_type (t2
);
660 switch (TREE_CODE (t1
))
664 case QUAL_UNION_TYPE
:
665 return aggr_has_equiv_id (t1
, t2
);
670 return lipo_cmp_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
672 return (lipo_cmp_type (TYPE_DOMAIN (t1
), TYPE_DOMAIN (t2
))
673 && lipo_cmp_type (TREE_TYPE (t1
), TREE_TYPE (t2
)));
675 return lipo_cmp_type (TYPE_METHOD_BASETYPE (t1
),
676 TYPE_METHOD_BASETYPE (t2
));
680 for (arg1
= TYPE_ARG_TYPES (t1
), arg2
= TYPE_ARG_TYPES (t2
);
682 arg1
= TREE_CHAIN (arg1
), arg2
= TREE_CHAIN (arg2
))
683 if (!lipo_cmp_type (TREE_VALUE (arg1
),
691 return lipo_cmp_type (TYPE_OFFSET_BASETYPE (t1
),
692 TYPE_OFFSET_BASETYPE (t2
));
694 return lipo_cmp_type (TREE_TYPE (t1
), TREE_TYPE (t2
));
696 case FIXED_POINT_TYPE
:
698 return (TYPE_PRECISION (t1
) == TYPE_PRECISION (t2
)
699 && TYPE_MODE (t1
) == TYPE_MODE (t2
)
700 && TYPE_MIN_VALUE (t1
) == TYPE_MIN_VALUE (t2
)
701 && TYPE_MAX_VALUE (t1
) == TYPE_MAX_VALUE (t2
));
703 return (TYPE_VECTOR_SUBPARTS (t1
) == TYPE_VECTOR_SUBPARTS (t2
)
704 && lipo_cmp_type (TREE_TYPE (t1
), TREE_TYPE (t2
)));
708 case TEMPLATE_TYPE_PARM
:
715 #ifndef ANON_AGGRNAME_PREFIX
716 #define ANON_AGGRNAME_PREFIX "__anon_"
718 #ifndef ANON_AGGRNAME_P
719 #define ANON_AGGRNAME_P(ID_NODE) \
720 (!strncmp (IDENTIFIER_POINTER (ID_NODE), ANON_AGGRNAME_PREFIX, \
721 sizeof (ANON_AGGRNAME_PREFIX) - 1))
724 /* Callback function used in tree walk to find referenced struct types. */
727 find_struct_types (tree
*tp
,
728 int *walk_subtrees ATTRIBUTE_UNUSED
,
729 void *data ATTRIBUTE_UNUSED
)
736 if (lang_hooks
.l_ipo
.is_compiler_generated_type (*tp
))
739 switch (TREE_CODE (*tp
))
743 case QUAL_UNION_TYPE
:
745 tree cano_type
, name
;
749 cano_type
= get_norm_type (*tp
);
750 name
= TYPE_NAME (cano_type
);
753 /* the main variant of typedef of unnamed struct
754 has no name, use the orignal type for equivalence. */
756 name
= TYPE_NAME (cano_type
);
761 && (DECL_IGNORED_P (name
)
762 || ANON_AGGRNAME_P (DECL_NAME (name
))))
765 if (!pointer_set_insert (type_set
, cano_type
))
766 VEC_safe_push (tree
, heap
, pending_types
, cano_type
);
768 return NULL_TREE
; /* Or use walk tree without dups. */
770 context
= TYPE_CONTEXT (cano_type
);
771 if (context
&& TYPE_P (context
))
772 walk_tree (&context
, find_struct_types
, NULL
, NULL
);
774 /* Instantiate a nested work as the tree walker does not
775 get to the fields. */
776 if (TYPE_BINFO (cano_type
))
779 tree binfo
, base_binfo
;
781 for (binfo
= TYPE_BINFO (cano_type
), i
= 0;
782 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
783 walk_tree (&BINFO_TYPE (base_binfo
), find_struct_types
,
786 for (field
= TYPE_FIELDS (cano_type
);
788 field
= TREE_CHAIN (field
))
789 walk_tree (&TREE_TYPE (field
), find_struct_types
,
797 else if (DECL_P (*tp
))
798 /* walk tree does not walk down decls, so do a nested walk here. */
799 walk_tree (&(TREE_TYPE (*tp
)), find_struct_types
, NULL
, NULL
);
804 /* Collect referenced struct types. */
807 cgraph_collect_type_referenced (void)
810 gimple_stmt_iterator gi
;
814 for (gi
= gsi_start_bb (bb
); !gsi_end_p (gi
); gsi_next (&gi
))
817 gimple stmt
= gsi_stmt (gi
);
818 for (i
= 0; i
< gimple_num_ops (stmt
); i
++)
819 walk_tree (gimple_op_ptr (stmt
, i
), find_struct_types
, NULL
, NULL
);
824 /* Check type equivalence. Returns 1 if T1 and T2 are equivalent
825 for tbaa; return 0 if not. -1 is returned if it is unknown. */
828 equivalent_struct_types_for_tbaa (const_tree t1
, const_tree t2
)
830 struct type_ent key
, *tent1
, *tent2
, **slot
;
835 t1
= get_norm_type (t1
);
836 t2
= get_norm_type (t2
);
838 key
.type
= (tree
) (long) t1
;
839 slot
= (struct type_ent
**)
840 htab_find_slot (l_ipo_type_tab
, &key
, NO_INSERT
);
845 key
.type
= (tree
) (long) t2
;
846 slot
= (struct type_ent
**)
847 htab_find_slot (l_ipo_type_tab
, &key
, NO_INSERT
);
852 return tent1
->eq_id
== tent2
->eq_id
;
855 /* Build type hash table. */
858 cgraph_build_type_equivalent_classes (void)
861 n
= VEC_length (tree
, pending_types
);
862 for (i
= 0; i
< n
; i
++)
864 struct type_ec
**slot
;
866 te
.rep_type
= VEC_index (tree
, pending_types
, i
);
868 slot
= (struct type_ec
**) htab_find_slot (type_hash_tab
,
872 *slot
= XCNEW (struct type_ec
);
873 (*slot
)->rep_type
= te
.rep_type
;
875 VEC_safe_push (tree
, heap
, (*slot
)->eq_types
, te
.rep_type
);
879 /* Re-propagate component types's alias set to that of TYPE. PROCESSED
880 is the pointer set of processed types. */
883 re_record_component_aliases (tree type
,
884 struct pointer_set_t
*processed
)
886 alias_set_type superset
= get_alias_set (type
);
892 if (pointer_set_insert (processed
, type
))
895 switch (TREE_CODE (type
))
899 case QUAL_UNION_TYPE
:
900 /* Recursively record aliases for the base classes, if there are any. */
901 if (TYPE_BINFO (type
))
904 tree binfo
, base_binfo
;
906 for (binfo
= TYPE_BINFO (type
), i
= 0;
907 BINFO_BASE_ITERATE (binfo
, i
, base_binfo
); i
++)
909 re_record_component_aliases (BINFO_TYPE (base_binfo
),
911 record_alias_subset (superset
,
912 get_alias_set (BINFO_TYPE (base_binfo
)));
915 for (field
= TYPE_FIELDS (type
); field
!= 0; field
= TREE_CHAIN (field
))
916 if (TREE_CODE (field
) == FIELD_DECL
&& !DECL_NONADDRESSABLE_P (field
))
918 re_record_component_aliases (TREE_TYPE (field
), processed
);
919 record_alias_subset (superset
, get_alias_set (TREE_TYPE (field
)));
924 re_record_component_aliases (TREE_TYPE (type
), processed
);
925 record_alias_subset (superset
, get_alias_set (TREE_TYPE (type
)));
928 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
936 /* The callback function to merge alias sets of equivalent types. */
939 type_eq_process (void **slot
, void *data ATTRIBUTE_UNUSED
)
942 alias_set_type alias_set
, ptr_alias_set
= -1;
944 VEC(tree
, heap
) *eq_types
;
945 struct type_ec
** te
= (struct type_ec
**)slot
;
946 bool zero_set
= false, ptr_zero_set
= false;
947 struct type_ent
**slot2
, key
, *tent
;
950 rep_type
= (*te
)->rep_type
;
951 eq_types
= (*te
)->eq_types
;
952 alias_set
= get_alias_set (rep_type
);
955 VEC_iterate (tree
, eq_types
, i
, type
);
958 alias_set_type als
, ptr_als
= -1;
959 tree type_ptr
= TYPE_POINTER_TO (type
);;
961 als
= get_alias_set (type
);
965 if (alias_set
&& als
&& alias_set
!= als
)
966 record_alias_subset (alias_set
, als
);
970 ptr_als
= get_alias_set (type_ptr
);
974 if (ptr_alias_set
== -1)
975 ptr_alias_set
= ptr_als
;
978 if (!ptr_zero_set
&& ptr_alias_set
!= ptr_als
)
979 record_alias_subset (ptr_alias_set
, ptr_als
);
984 /* Now propagate back. */
986 VEC_iterate (tree
, eq_types
, i
, type
);
989 alias_set_type als
, ptr_als
;
990 tree ptr_type
= TYPE_POINTER_TO (type
);
992 als
= get_alias_set (type
);
995 TYPE_ALIAS_SET (type
) = 0;
996 else if (alias_set
!= als
)
997 record_alias_subset (als
, alias_set
);
1001 ptr_als
= get_alias_set (ptr_type
);
1003 TYPE_ALIAS_SET (ptr_type
) = 0;
1004 else if (ptr_alias_set
!= ptr_als
)
1005 record_alias_subset (ptr_als
, ptr_alias_set
);
1010 /* Now populate the type table. */
1013 VEC_iterate (tree
, eq_types
, i
, type
);
1017 slot2
= (struct type_ent
**)
1018 htab_find_slot (l_ipo_type_tab
, &key
, INSERT
);
1021 tent
= ggc_alloc_cleared_type_ent ();
1022 tent
->type
= key
.type
;
1023 tent
->eq_id
= l_ipo_eq_id
;
1030 /* Regenerate alias set for aggregate types. */
1033 record_components_for_parent_types (void)
1036 struct pointer_set_t
*processed_types
;
1038 processed_types
= pointer_set_create ();
1039 n
= VEC_length (tree
, pending_types
);
1040 for (i
= 0; i
< n
; i
++)
1042 tree type
= VEC_index (tree
, pending_types
, i
);
1043 re_record_component_aliases (type
, processed_types
);
1046 pointer_set_destroy (processed_types
);
1049 /* Unify type alias sets for equivalent types. */
1052 cgraph_unify_type_alias_sets (void)
1054 struct cgraph_node
*node
;
1055 struct varpool_node
*pv
;
1057 if (!L_IPO_COMP_MODE
)
1059 type_set
= pointer_set_create ();
1060 type_hash_tab
= htab_create (10, type_hash_hash
,
1061 type_hash_eq
, type_hash_del
);
1062 l_ipo_type_tab
= htab_create_ggc (10, type_addr_hash
,
1063 type_addr_eq
, NULL
);
1065 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1067 if (node
->analyzed
&& (node
->needed
|| node
->reachable
))
1069 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
1070 current_function_decl
= node
->decl
;
1071 cgraph_collect_type_referenced ();
1072 current_function_decl
= NULL
;
1077 for (pv
= varpool_nodes
; pv
; pv
= pv
->next
)
1078 walk_tree (&pv
->decl
, find_struct_types
, NULL
, NULL
);
1080 /* Compute type equivalent classes. */
1081 cgraph_build_type_equivalent_classes ();
1082 /* Now unify alias sets of equivelent types. */
1083 htab_traverse (type_hash_tab
, type_eq_process
, NULL
);
1084 /* Finally re-populating parent's alias set. */
1085 record_components_for_parent_types ();
1087 pointer_set_destroy (type_set
);
1088 VEC_free (tree
, heap
, pending_types
);
1089 htab_delete (type_hash_tab
);
1092 /* Return true if NODE->decl from an auxiliary module has external
1093 definition (and therefore is not needed for expansion). */
1096 cgraph_is_aux_decl_external (struct cgraph_node
*node
)
1098 tree decl
= node
->decl
;
1100 if (!(L_IPO_COMP_MODE
|| L_IPO_STREAM_COMP_MODE
))
1103 if (!cgraph_is_auxiliary (decl
))
1106 /* Versioned clones from auxiliary moduels are not
1108 if (node
->is_versioned_clone
)
1111 /* Reverse the process order for virtual and comdat functions
1112 in streaming lipo. */
1113 if (!flag_ripa_stream
)
1115 /* virtual functions won't be deleted in the primary module. */
1116 if (DECL_VIRTUAL_P (decl
))
1120 /* Comdat or weak functions in aux modules are not external --
1121 there is no guarantee that the definitition will be emitted
1122 in the primary compilation of this auxiliary module. */
1123 if (DECL_COMDAT (decl
) || DECL_WEAK (decl
))
1126 if (flag_ripa_stream
)
1128 if (DECL_VIRTUAL_P (decl
))
1132 if (!TREE_PUBLIC (decl
))
1135 /* The others from aux modules are external. */
1139 /* Linked function symbol (cgraph node) table. */
1140 static GTY((param_is (struct cgraph_sym
))) htab_t cgraph_symtab
;
1142 /* This is true when global linking is needed and performed (for C++).
1143 For C, symbol linking is performed on the fly during parsing, and
1144 the cgraph_symtab is used only for keeping additional information
1145 for any already merged symbol if needed. */
1147 static bool global_link_performed
= 0;
1149 /* For an external (non-defined) function DECL, return the primary
1150 module id (even though when the declaration is declared in an aux
1151 module). For a defined function DECL, return the module id in which
1155 cgraph_get_module_id (tree decl
)
1157 struct function
*func
= DECL_STRUCT_FUNCTION (decl
);
1160 return primary_module_id
;
1161 return FUNC_DECL_MODULE_ID (func
);
1164 /* Return true if function decl is defined in an auxiliary module. */
1167 cgraph_is_auxiliary (tree decl
)
1169 return (cgraph_get_module_id (decl
) != primary_module_id
);
1172 /* Return the hash value for cgraph_sym pointed to by P. The
1173 hash value is computed using function's assembler name. */
1176 hash_sym_by_assembler_name (const void *p
)
1178 const struct cgraph_sym
*n
= (const struct cgraph_sym
*) p
;
1179 return (hashval_t
) decl_assembler_name_hash (n
->assembler_name
);
1182 /* Return nonzero if P1 and P2 are equal. */
1185 eq_assembler_name (const void *p1
, const void *p2
)
1187 const struct cgraph_sym
*n1
= (const struct cgraph_sym
*) p1
;
1188 const_tree name
= (const_tree
) p2
;
1189 return (decl_assembler_name_equal (n1
->rep_decl
, name
));
1192 /* Return the cgraph_sym for function declaration DECL. */
1194 static struct cgraph_sym
**
1195 cgraph_sym (tree decl
)
1197 struct cgraph_sym
**slot
;
1202 gcc_assert (!global_link_performed
);
1206 name
= DECL_ASSEMBLER_NAME (decl
);
1207 slot
= (struct cgraph_sym
**)
1208 htab_find_slot_with_hash (cgraph_symtab
, name
,
1209 decl_assembler_name_hash (name
),
1214 /* Return the representative declaration for assembler name
1218 cgraph_find_decl (tree asm_name
)
1220 struct cgraph_sym
**slot
;
1221 if (!L_IPO_COMP_MODE
)
1223 if (!cgraph_symtab
|| !global_link_performed
)
1226 slot
= (struct cgraph_sym
**)
1227 htab_find_slot_with_hash (cgraph_symtab
, asm_name
,
1228 decl_assembler_name_hash (asm_name
),
1230 if (!slot
|| !*slot
)
1233 return (*slot
)->rep_node
->decl
;
1236 /* Return true if function declaration DECL is originally file scope
1237 static, which is promoted to global scope. */
1240 cgraph_is_promoted_static_func (tree decl
)
1242 struct cgraph_sym
** sym
;
1243 gcc_assert (L_IPO_COMP_MODE
);
1245 /* cgraph_symtab will be created when any symbol got
1250 sym
= cgraph_sym (decl
);
1253 return (*sym
)->is_promoted_static
;
1256 /* Hash function for module information table. ENT
1257 is a pointer to a cgraph_module_info. */
1260 htab_sym_hash (const void *ent
)
1262 const struct cgraph_mod_info
* const mi
1263 = (const struct cgraph_mod_info
* const ) ent
;
1264 return (hashval_t
) mi
->module_id
;
1267 /* Hash equality function for module information table. */
1270 htab_sym_eq (const void *ent1
, const void *ent2
)
1272 const struct cgraph_mod_info
* const mi1
1273 = (const struct cgraph_mod_info
* const ) ent1
;
1274 const struct cgraph_mod_info
* const mi2
1275 = (const struct cgraph_mod_info
* const ) ent2
;
1276 return (mi1
->module_id
== mi2
->module_id
);
1279 /* cgraph_sym SYM may be defined in more than one source modules.
1280 Add declaration DECL's definiting module to SYM. */
1283 add_define_module (struct cgraph_sym
*sym
, tree decl
)
1286 struct cgraph_mod_info
**slot
;
1287 struct cgraph_mod_info mi
;
1289 struct function
*f
= DECL_STRUCT_FUNCTION (decl
);
1292 module_id
= FUNC_DECL_MODULE_ID (f
);
1294 if (!sym
->def_module_hash
)
1295 sym
->def_module_hash
1296 = htab_create_ggc (10, htab_sym_hash
, htab_sym_eq
, NULL
);
1298 mi
.module_id
= module_id
;
1299 slot
= (struct cgraph_mod_info
**)htab_find_slot (sym
->def_module_hash
,
1303 *slot
= ggc_alloc_cleared_cgraph_mod_info ();
1304 (*slot
)->module_id
= module_id
;
1307 gcc_assert ((*slot
)->module_id
== module_id
);
1311 add_def_module (void **slot
, void *data
)
1313 struct cgraph_mod_info
**m
= (struct cgraph_mod_info
**)slot
;
1314 htab_t mod_set
= (htab_t
) data
;
1315 struct cgraph_mod_info
**new_slot
;
1317 new_slot
= (struct cgraph_mod_info
**)htab_find_slot (mod_set
, *m
, INSERT
);
1320 *new_slot
= ggc_alloc_cleared_cgraph_mod_info ();
1321 (*new_slot
)->module_id
= (*m
)->module_id
;
1324 gcc_assert ((*new_slot
)->module_id
== (*m
)->module_id
);
1328 /* Clone defined module hash table from ORIG to CLONE. */
1331 copy_defined_module_set (tree clone
, tree orig
)
1333 struct cgraph_sym
**orig_sym
, **clone_sym
;
1335 orig_sym
= cgraph_sym (orig
);
1336 clone_sym
= cgraph_sym (clone
);
1337 if (!orig_sym
|| !(*orig_sym
)->def_module_hash
)
1339 if (!(*clone_sym
)->def_module_hash
)
1340 (*clone_sym
)->def_module_hash
1341 = htab_create_ggc (10, htab_sym_hash
, htab_sym_eq
, NULL
);
1342 htab_traverse ((*orig_sym
)->def_module_hash
, add_def_module
, (*clone_sym
)->def_module_hash
);
1345 /* Return true if the symbol associated with DECL is defined in module
1346 MODULE_ID. This interface is used by the inliner to make sure profile-gen
1347 and profile-use pass (L-IPO mode) make consistent inline decision. */
1350 cgraph_is_inline_body_available_in_module (tree decl
, unsigned module_id
)
1352 struct cgraph_sym
**sym
;
1354 struct cgraph_mod_info mi
;
1356 gcc_assert (L_IPO_COMP_MODE
);
1358 if (DECL_BUILT_IN (decl
))
1361 /* TODO: revisit this. */
1362 if (DECL_IN_SYSTEM_HEADER (decl
) && DECL_DECLARED_INLINE_P (decl
))
1365 gcc_assert (TREE_STATIC (decl
) || DECL_DECLARED_INLINE_P (decl
));
1367 if (cgraph_get_module_id (decl
) == module_id
)
1370 sym
= cgraph_sym (decl
);
1371 if (!sym
|| !(*sym
)->def_module_hash
)
1374 mi
.module_id
= module_id
;
1375 slot
= htab_find_slot ((*sym
)->def_module_hash
, &mi
, NO_INSERT
);
1378 gcc_assert (((struct cgraph_mod_info
*)*slot
)->module_id
== module_id
);
1384 /* Return the linked cgraph node using DECL's assembler name. DO_ASSERT
1385 is a flag indicating that a non null link target must be returned. */
1387 struct cgraph_node
*
1388 cgraph_lipo_get_resolved_node_1 (tree decl
, bool do_assert
)
1390 struct cgraph_sym
**slot
;
1392 /* Handle alias decl. */
1393 slot
= cgraph_sym (cgraph_node (decl
)->decl
);
1395 if (!slot
|| !*slot
)
1401 /* Nodes that are indirectly called are not 'reachable' in
1402 the callgraph. If they are not needed (comdat, inline
1403 extern etc), they may be removed from the link table
1404 before direct calls to them are exposed (via indirect
1405 call promtion by const folding etc). When this happens,
1406 the node will be to be relinked. A probably better fix
1407 is to modify the callgraph so that they are not eliminated
1408 in the first place -- this will allow inlining to happen. */
1410 struct cgraph_node
*n
= cgraph_node (decl
);
1413 gcc_assert (DECL_EXTERNAL (decl
)
1414 || cgraph_is_aux_decl_external (n
)
1415 || DECL_VIRTUAL_P (decl
));
1416 gcc_assert ((!n
->reachable
&& !n
->needed
)
1417 /* This is the case for explicit extern instantiation,
1418 when cgraph node is not created before link. */
1419 || DECL_EXTERNAL (decl
));
1420 cgraph_link_node (n
);
1429 struct cgraph_sym
*sym
= *slot
;
1430 return sym
->rep_node
;
1434 /* Return the cgraph_node of DECL if decl has definition; otherwise return
1435 the cgraph node of the representative decl, which is the declaration DECL
1436 is resolved to after linking/symbol resolution. */
1438 struct cgraph_node
*
1439 cgraph_lipo_get_resolved_node (tree decl
)
1441 struct cgraph_node
*node
= NULL
;
1443 gcc_assert (L_IPO_COMP_MODE
&& global_link_performed
);
1444 gcc_assert (cgraph_symtab
);
1447 if (!TREE_PUBLIC (decl
) || DECL_ARTIFICIAL (decl
)
1448 /* builtin function decls are shared across modules, but 'linking'
1449 is still performed for them to keep track of the set of defining
1450 modules. Skip the real resolution here to avoid merging '__builtin_xxx'
1452 || DECL_BUILT_IN (decl
))
1453 return cgraph_node (decl
);
1455 node
= cgraph_lipo_get_resolved_node_1 (decl
, true);
1459 /* When NODE->decl is dead function eliminated,
1460 remove the entry in the link table. */
1463 cgraph_remove_link_node (struct cgraph_node
*node
)
1467 if (!L_IPO_COMP_MODE
|| !cgraph_symtab
)
1472 /* Skip nodes that are not in the link table. */
1473 if (!TREE_PUBLIC (decl
) || DECL_ARTIFICIAL (decl
))
1476 /* Skip if node is an inline clone or if the node has
1477 defintion that is not really resolved to the merged node. */
1478 if (cgraph_lipo_get_resolved_node_1 (decl
, false) != node
)
1481 name
= DECL_ASSEMBLER_NAME (decl
);
1482 htab_remove_elt_with_hash (cgraph_symtab
, name
,
1483 decl_assembler_name_hash (name
));
1486 /* Return true if the function body for DECL has profile information. */
1489 has_profile_info (tree decl
)
1491 gcov_type
*ctrs
= NULL
;
1493 struct function
* f
= DECL_STRUCT_FUNCTION (decl
);
1495 ctrs
= get_coverage_counts_no_warn (f
, GCOV_COUNTER_ARCS
, &n
);
1499 for (i
= 0; i
< n
; i
++)
1507 /* Resolve delaration NODE->decl for function symbol *SLOT. */
1510 resolve_cgraph_node (struct cgraph_sym
**slot
, struct cgraph_node
*node
)
1513 int decl1_defined
= 0;
1514 int decl2_defined
= 0;
1516 decl1
= (*slot
)->rep_decl
;
1519 decl1_defined
= gimple_has_body_p (decl1
);
1520 decl2_defined
= gimple_has_body_p (decl2
);
1522 if (decl1_defined
&& !decl2_defined
)
1525 if (!decl1_defined
&& decl2_defined
)
1527 (*slot
)->rep_node
= node
;
1528 (*slot
)->rep_decl
= decl2
;
1529 add_define_module (*slot
, decl2
);
1535 bool has_prof1
= false;
1536 bool has_prof2
= false;
1537 gcc_assert (decl1_defined
);
1538 add_define_module (*slot
, decl2
);
1540 has_prof1
= has_profile_info (decl1
);
1543 has_prof2
= has_profile_info (decl2
);
1546 (*slot
)->rep_node
= node
;
1547 (*slot
)->rep_decl
= decl2
;
1555 /* Resolve NODE->decl in the function symbol table. */
1558 cgraph_link_node (struct cgraph_node
*node
)
1563 if (!L_IPO_COMP_MODE
)
1569 /* Skip the cases when the defintion can be locally resolved, and
1570 when we do not need to keep track of defining modules. */
1571 if (!TREE_PUBLIC (node
->decl
) || DECL_ARTIFICIAL (node
->decl
))
1574 name
= DECL_ASSEMBLER_NAME (node
->decl
);
1575 slot
= htab_find_slot_with_hash (cgraph_symtab
, name
,
1576 decl_assembler_name_hash (name
),
1579 resolve_cgraph_node ((struct cgraph_sym
**) slot
, node
);
1582 struct cgraph_sym
*sym
= ggc_alloc_cleared_cgraph_sym ();
1583 sym
->rep_node
= node
;
1584 sym
->rep_decl
= node
->decl
;
1585 sym
->assembler_name
= name
;
1586 add_define_module (sym
, node
->decl
);
1589 return (struct cgraph_sym
*) *slot
;
1592 /* Perform cross module linking of function declarations. */
1595 cgraph_do_link (void)
1597 struct cgraph_node
*node
;
1599 if (!L_IPO_COMP_MODE
)
1602 global_link_performed
= 1;
1603 gcc_assert (cgraph_pre_profiling_inlining_done
);
1607 = htab_create_ggc (10, hash_sym_by_assembler_name
,
1608 eq_assembler_name
, NULL
);
1610 for (node
= cgraph_nodes
; node
; node
= node
->next
)
1612 gcc_assert (!node
->global
.inlined_to
);
1613 cgraph_link_node (node
);
1623 /* Hash function for promo_ent table. */
1626 promo_ent_hash (const void *ent
)
1628 const struct promo_ent
*const entry
1629 = (const struct promo_ent
*) ent
;
1631 return htab_hash_string (entry
->assemb_name
);
1634 /* Hash_eq function for promo_ent table. */
1637 promo_ent_eq (const void *ent1
, const void *ent2
)
1639 const struct promo_ent
*const entry1
1640 = (const struct promo_ent
*) ent1
;
1641 const struct promo_ent
*const entry2
1642 = (const struct promo_ent
*) ent2
;
1643 if (!strcmp (entry1
->assemb_name
, entry2
->assemb_name
))
1648 /* Delete function for promo_ent hash table. */
1651 promo_ent_del (void *ent
)
1653 struct promo_ent
*const entry
1654 = (struct promo_ent
*) ent
;
1656 free (entry
->assemb_name
);
1660 static htab_t promo_ent_hash_tab
= NULL
;
1662 /* Return a unique sequence number for NAME. This is needed to avoid
1663 name conflict -- function scope statics may have identical names.
1665 This function returns a zero sequence number if it is called with
1666 a particular NAME for the first time, and non-zero otherwise.
1667 This fact is used to keep track of unseen weak variables. */
1670 get_name_seq_num (const char *name
)
1672 struct promo_ent
**slot
;
1673 struct promo_ent ent
;
1674 ent
.assemb_name
= xstrdup (name
);
1677 slot
= (struct promo_ent
**)
1678 htab_find_slot (promo_ent_hash_tab
, &ent
, INSERT
);
1682 *slot
= XCNEW (struct promo_ent
);
1683 (*slot
)->assemb_name
= ent
.assemb_name
;
1688 free (ent
.assemb_name
);
1690 return (*slot
)->seq
;
1693 /* Returns a unique assembler name for DECL. */
1696 create_unique_name (tree decl
, unsigned module_id
)
1699 char *assembler_name
;
1701 struct function
*context
= NULL
;
1704 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1706 if (!DECL_CONTEXT (decl
)
1707 || TREE_CODE (DECL_CONTEXT (decl
)) == TRANSLATION_UNIT_DECL
)
1709 id
= DECL_NAME (decl
);
1710 /* if (IDENTIFIER_OPNAME_P (id)) */
1711 if (TREE_LANG_FLAG_2 (id
))
1712 id
= DECL_ASSEMBLER_NAME (decl
);
1715 id
= DECL_ASSEMBLER_NAME (decl
);
1719 if (!DECL_CONTEXT (decl
))
1720 id
= DECL_NAME (decl
);
1721 else if (TREE_CODE (DECL_CONTEXT (decl
)) == NAMESPACE_DECL
)
1722 id
= DECL_ASSEMBLER_NAME (decl
);
1723 else if (TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
1725 id
= DECL_NAME (decl
);
1726 context
= DECL_STRUCT_FUNCTION (DECL_CONTEXT (decl
));
1729 /* file scope context */
1730 id
= DECL_NAME (decl
);
1733 name
= IDENTIFIER_POINTER (id
);
1737 unsigned fno
= FUNC_DECL_FUNC_ID (context
);
1738 n
= (char *)alloca (strlen (name
) + 15);
1739 sprintf (n
, "%s_%u", name
, fno
);
1743 assembler_name
= (char*) alloca (strlen (name
) + 30);
1744 sprintf (assembler_name
, "%s_cmo_%u", name
, module_id
);
1745 seq
= get_name_seq_num (assembler_name
);
1747 sprintf (assembler_name
, "%s_%d", assembler_name
, seq
);
1749 assemb_id
= get_identifier (assembler_name
);
1754 /* Promote DECL to be global. MODULE_ID is the id of the module where
1755 DECL is defined. IS_EXTERN is a flag indicating if externalization
1759 promote_static_var_func (unsigned module_id
, tree decl
, bool is_extern
)
1764 /* No need to promote symbol alias. */
1765 alias
= lookup_attribute ("alias", DECL_ATTRIBUTES (decl
));
1769 /* Function decls in C++ may contain characters not taken by assembler.
1770 Similarly, function scope static variable has UID as the assembler name
1771 suffix which is not consistent across modules. */
1773 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
1774 && TREE_CODE (decl
) == FUNCTION_DECL
)
1775 cgraph_remove_assembler_hash_node (cgraph_node (decl
));
1777 assemb_id
= create_unique_name (decl
, module_id
);
1778 SET_DECL_ASSEMBLER_NAME (decl
, assemb_id
);
1779 TREE_PUBLIC (decl
) = 1;
1780 DECL_VISIBILITY (decl
) = VISIBILITY_HIDDEN
;
1781 DECL_VISIBILITY_SPECIFIED (decl
) = 1;
1783 if (TREE_CODE (decl
) == FUNCTION_DECL
)
1785 struct cgraph_node
*node
= cgraph_node (decl
);
1787 node
->resolution
= LDPR_UNKNOWN
;
1788 cgraph_add_assembler_hash_node (node
);
1789 if (flag_opt_info
>= OPT_INFO_MAX
)
1790 inform (UNKNOWN_LOCATION
, "Promote function %s to global (%d): %s",
1791 get_name (decl
),is_extern
,
1792 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME(decl
)));
1796 struct varpool_node
*node
= varpool_node (decl
);
1797 node
->resolution
= LDPR_UNKNOWN
;
1798 varpool_link_node (node
);
1799 if (flag_opt_info
>= OPT_INFO_MAX
)
1800 inform (UNKNOWN_LOCATION
, "Promote variable %s to global (%d): %s",
1801 get_name (decl
),is_extern
,
1802 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME(decl
)));
1807 if (TREE_CODE (decl
) == VAR_DECL
)
1809 TREE_STATIC (decl
) = 0;
1810 DECL_EXTERNAL (decl
) = 1;
1811 /* Keep the initializer to allow const prop. */
1812 /* DECL_INITIAL (decl) = 0; */
1813 DECL_CONTEXT (decl
) = 0;
1816 Function body will be deleted later before expansion. */
1819 TREE_STATIC (decl
) = 1;
1822 /* Externalize global variables from aux modules and promote
1824 WEAK variables are treated especially in
1825 varpool_remove_duplicate_weak_decls. */
1828 process_module_scope_static_var (struct varpool_node
*vnode
)
1830 tree decl
= vnode
->decl
;
1832 if (varpool_is_auxiliary (vnode
))
1834 gcc_assert (vnode
->module_id
!= primary_module_id
);
1835 if (TREE_PUBLIC (decl
))
1837 /* Externalize non-weak variables. */
1838 if (!DECL_WEAK (decl
))
1840 DECL_EXTERNAL (decl
) = 1;
1841 TREE_STATIC (decl
) = 0;
1842 /* Keep the initializer to allow const prop. */
1843 /* DECL_INITIAL (decl) = NULL; */
1844 if (DECL_CONTEXT (decl
))
1846 DECL_ASSEMBLER_NAME (decl
);
1848 DECL_CONTEXT (decl
) = NULL
;
1853 /* Promote static vars to global. */
1854 if (vnode
->module_id
)
1855 promote_static_var_func (vnode
->module_id
, decl
,
1856 varpool_is_auxiliary (vnode
));
1861 if (primary_module_id
&& primary_module_exported
&& !TREE_PUBLIC (decl
))
1862 promote_static_var_func (vnode
->module_id
, decl
,
1863 varpool_is_auxiliary (vnode
));
1867 /* Promote static function CNODE->decl to be global. */
1870 process_module_scope_static_func (struct cgraph_node
*cnode
)
1872 tree decl
= cnode
->decl
;
1875 if (TREE_PUBLIC (decl
)
1876 || !TREE_STATIC (decl
)
1877 || DECL_EXTERNAL (decl
)
1878 || DECL_ARTIFICIAL (decl
))
1881 if (flag_ripa_no_promote_always_inline
1882 && lookup_attribute ("always_inline", DECL_ATTRIBUTES (decl
)) != NULL
)
1885 /* Can be local -- the promotion pass need to be done after
1886 callgraph build when address taken bit is set. */
1887 addr_taken
= cnode
->address_taken
;
1888 if (!addr_taken
&& cnode
->same_body
)
1890 struct cgraph_node
*alias
= cnode
->same_body
;
1893 if (alias
->address_taken
)
1895 alias
= alias
->next
;
1900 tree assemb_id
= create_unique_name (decl
, cgraph_get_module_id (decl
));
1902 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
1903 cgraph_remove_assembler_hash_node (cnode
);
1904 SET_DECL_ASSEMBLER_NAME (decl
, assemb_id
);
1908 if (cgraph_is_auxiliary (decl
))
1912 gcc_assert (cgraph_get_module_id (decl
) != primary_module_id
);
1913 mod_id
= cgraph_get_module_id (decl
);
1914 /* Promote static function to global. */
1917 promote_static_var_func (mod_id
, decl
, 1);
1919 /* Process aliases */
1920 if (cnode
->same_body
)
1922 struct cgraph_node
*alias
= cnode
->same_body
;
1925 if (!alias
->thunk
.thunk_p
)
1927 tree alias_decl
= alias
->decl
;
1929 if (cgraph_get_module_id (alias_decl
) == mod_id
)
1930 promote_static_var_func (mod_id
, alias_decl
, 1);
1932 alias
= alias
->next
;
1939 if (primary_module_exported
1940 /* skip static_init routines. */
1941 && !DECL_ARTIFICIAL (decl
))
1943 promote_static_var_func (cgraph_get_module_id (decl
), decl
, 0);
1944 cgraph_mark_if_needed (decl
);
1946 /* Process aliases */
1947 if (cnode
->same_body
)
1949 struct cgraph_node
*alias
= cnode
->same_body
;
1952 if (!alias
->thunk
.thunk_p
)
1954 tree alias_decl
= alias
->decl
;
1956 if (cgraph_get_module_id (alias_decl
) == cgraph_get_module_id (decl
))
1957 promote_static_var_func (cgraph_get_module_id (decl
), alias_decl
, 0);
1959 alias
= alias
->next
;
1966 /* Process var_decls, func_decls with static storage. */
1969 cgraph_process_module_scope_statics (void)
1971 struct cgraph_node
*pf
;
1972 struct varpool_node
*pv
;
1974 if (!(L_IPO_COMP_MODE
|| L_IPO_STREAM_COMP_MODE
))
1977 promo_ent_hash_tab
= htab_create (10, promo_ent_hash
,
1978 promo_ent_eq
, promo_ent_del
);
1980 /* Process variable first. */
1981 for (pv
= varpool_nodes_queue
; pv
; pv
= pv
->next_needed
)
1982 process_module_scope_static_var (pv
);
1984 for (pf
= cgraph_nodes
; pf
; pf
= pf
->next
)
1985 process_module_scope_static_func (pf
);
1987 htab_delete (promo_ent_hash_tab
);
1990 /* There could be duplicate non-extern WEAK decls in the varpool queue,
1991 coming from different modules. All but one of these need to be externalized
1992 and removed from the varpool queue.
1993 Duplicate WEAK decls can be added to varpool queue as late as
1994 cgraph_expand_function, when a WEAK decl is marked referenced as assembler
1995 is being output. Therefore, a call to this function should be made after
1996 cgraph_expand_function. */
1999 varpool_remove_duplicate_weak_decls (void)
2001 struct varpool_node
*next
, *node
= varpool_nodes_queue
;
2003 if (!L_IPO_COMP_MODE
)
2006 varpool_reset_queue ();
2008 promo_ent_hash_tab
= htab_create (10, promo_ent_hash
,
2009 promo_ent_eq
, promo_ent_del
);
2013 tree decl
= node
->decl
;
2014 next
= node
->next_needed
;
2016 if (TREE_PUBLIC (decl
) && DECL_WEAK (decl
) && !DECL_EXTERNAL (decl
)
2017 && get_name_seq_num (IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
))))
2019 DECL_EXTERNAL (decl
) = 1;
2020 TREE_STATIC (decl
) = 0;
2021 DECL_INITIAL (decl
) = NULL
;
2022 DECL_CONTEXT (decl
) = NULL
;
2025 varpool_mark_needed_node (node
);
2029 htab_delete (promo_ent_hash_tab
);
2032 static GTY((param_is (struct varpool_node
))) htab_t varpool_symtab
;
2034 /* Hash function for varpool node. */
2037 hash_node_by_assembler_name (const void *p
)
2039 const struct varpool_node
*n
= (const struct varpool_node
*) p
;
2040 return (hashval_t
) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n
->decl
));
2043 /* Returns nonzero if P1 and P2 are equal. */
2046 eq_node_assembler_name (const void *p1
, const void *p2
)
2048 const struct varpool_node
*n1
= (const struct varpool_node
*) p1
;
2049 const_tree name
= (const_tree
)p2
;
2050 return (decl_assembler_name_equal (n1
->decl
, name
));
2053 /* Return true if NODE's decl is declared in an auxiliary module. */
2056 varpool_is_auxiliary (struct varpool_node
*node
)
2058 return (node
->module_id
2059 && node
->module_id
!= primary_module_id
);
2062 /* Return the varpool_node to which DECL is resolved to during linking.
2063 This method can not be used after static to global promotion happens. */
2065 struct varpool_node
*
2066 real_varpool_node (tree decl
)
2071 if (!L_IPO_COMP_MODE
|| !varpool_symtab
)
2072 return varpool_node (decl
);
2074 if (!TREE_PUBLIC (decl
) || DECL_ARTIFICIAL (decl
))
2075 return varpool_node (decl
);
2077 name
= DECL_ASSEMBLER_NAME (decl
);
2078 slot
= htab_find_slot_with_hash (varpool_symtab
, name
,
2079 decl_assembler_name_hash (name
),
2081 gcc_assert (slot
&& *slot
);
2082 return (struct varpool_node
*)*slot
;
2085 /* Remove NODE from the link table. */
2088 varpool_remove_link_node (struct varpool_node
*node
)
2093 if (!L_IPO_COMP_MODE
|| !varpool_symtab
)
2098 if (!TREE_PUBLIC (decl
) || DECL_ARTIFICIAL (decl
))
2101 if (real_varpool_node (decl
) != node
)
2104 name
= DECL_ASSEMBLER_NAME (decl
);
2105 htab_remove_elt_with_hash (varpool_symtab
, name
,
2106 decl_assembler_name_hash (name
));
2109 /* Merge the addressable attribute from DECL2 to DECL1. */
2112 merge_addressable_attr (tree decl1
, tree decl2
)
2114 if (TREE_ADDRESSABLE (decl2
))
2115 TREE_ADDRESSABLE (decl1
) = 1;
2118 /* Resolve NODE->decl to symbol table entry *SLOT. */
2121 resolve_varpool_node (struct varpool_node
**slot
, struct varpool_node
*node
)
2125 decl1
= (*slot
)->decl
;
2128 /* Take the decl with the complete type. */
2129 if (COMPLETE_TYPE_P (TREE_TYPE (decl1
))
2130 && !COMPLETE_TYPE_P (TREE_TYPE (decl2
)))
2132 merge_addressable_attr (decl1
, decl2
);
2135 if (!COMPLETE_TYPE_P (TREE_TYPE (decl1
))
2136 && COMPLETE_TYPE_P (TREE_TYPE (decl2
)))
2139 merge_addressable_attr (decl2
, decl1
);
2143 /* Either all complete or neither's type is complete. Just
2144 pick the primary module's decl. */
2145 if (!varpool_is_auxiliary (*slot
))
2147 merge_addressable_attr (decl1
, decl2
);
2151 if (!varpool_is_auxiliary (node
))
2154 merge_addressable_attr (decl2
, decl1
);
2158 merge_addressable_attr (decl1
, decl2
);
2162 /* Link NODE into var_decl symbol table. */
2165 varpool_link_node (struct varpool_node
*node
)
2170 if (!L_IPO_COMP_MODE
|| !varpool_symtab
)
2173 if (!TREE_PUBLIC (node
->decl
) || DECL_ARTIFICIAL (node
->decl
))
2176 name
= DECL_ASSEMBLER_NAME (node
->decl
);
2177 slot
= htab_find_slot_with_hash (varpool_symtab
, name
,
2178 decl_assembler_name_hash (name
),
2181 resolve_varpool_node ((struct varpool_node
**) slot
, node
);
2186 /* Perform cross module linking for var_decls. */
2189 varpool_do_link (void)
2191 struct varpool_node
*node
;
2193 if (!L_IPO_COMP_MODE
)
2197 = htab_create_ggc (10, hash_node_by_assembler_name
,
2198 eq_node_assembler_name
, NULL
);
2199 for (node
= varpool_nodes
; node
; node
= node
->next
)
2200 varpool_link_node (node
);
2202 /* Merge the externally visible attribute. */
2203 for (node
= varpool_nodes
; node
; node
= node
->next
)
2204 if (node
->externally_visible
)
2205 (real_varpool_node (node
->decl
))->externally_visible
= true;
2208 /* Get the list of assembler name ids with reference bit set. */
2211 varpool_get_referenced_asm_ids (VEC(tree
, gc
) ** ids
)
2213 struct varpool_node
*node
;
2214 for (node
= varpool_nodes
; node
; node
= node
->next
)
2217 tree decl
= node
->decl
;
2218 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
2220 asm_id
= DECL_ASSEMBLER_NAME (decl
);
2221 VEC_safe_push (tree
, gc
, *ids
, asm_id
);
2226 /* Clear the referenced bit in all assembler ids. */
2229 varpool_clear_asm_id_reference_bit (void)
2231 struct varpool_node
*node
;
2232 for (node
= varpool_nodes
; node
; node
= node
->next
)
2235 tree decl
= node
->decl
;
2236 if (DECL_ASSEMBLER_NAME_SET_P (decl
))
2238 asm_id
= DECL_ASSEMBLER_NAME (decl
);
2239 TREE_SYMBOL_REFERENCED (asm_id
) = 0;
2245 #include "gt-l-ipo.h"