1 /* Callgraph handling code.
2 Copyright (C) 2003-2014 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
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/>. */
21 /* This file contains basic routines manipulating call graph
23 The call-graph is a data structure designed for intra-procedural optimization.
24 It represents a multi-graph where nodes are functions and edges are call sites. */
28 #include "coretypes.h"
33 #include "print-tree.h"
34 #include "tree-inline.h"
35 #include "langhooks.h"
44 #include "tree-ssa-alias.h"
45 #include "internal-fn.h"
47 #include "gimple-expr.h"
49 #include "gimple-iterator.h"
52 #include "gimple-ssa.h"
56 #include "value-prof.h"
58 #include "diagnostic-core.h"
60 #include "ipa-utils.h"
61 #include "lto-streamer.h"
62 #include "ipa-inline.h"
64 #include "gimple-pretty-print.h"
68 /* FIXME: Only for PROP_loops, but cgraph shouldn't have to know about this. */
69 #include "tree-pass.h"
71 static inline void cgraph_edge_remove_caller (struct cgraph_edge
*e
);
72 static inline void cgraph_edge_remove_callee (struct cgraph_edge
*e
);
74 /* Queue of cgraph nodes scheduled to be lowered. */
75 symtab_node
*x_cgraph_nodes_queue
;
76 #define cgraph_nodes_queue ((struct cgraph_node *)x_cgraph_nodes_queue)
78 /* Number of nodes in existence. */
81 /* Maximal uid used in cgraph nodes. */
84 /* Maximal uid used in cgraph edges. */
85 int cgraph_edge_max_uid
;
87 /* Set when whole unit has been analyzed so we can access global info. */
88 bool cgraph_global_info_ready
= false;
90 /* What state callgraph is in right now. */
91 enum cgraph_state cgraph_state
= CGRAPH_STATE_PARSING
;
93 /* Set when the cgraph is fully build and the basic flags are computed. */
94 bool cgraph_function_flags_ready
= false;
96 /* List of hooks triggered on cgraph_edge events. */
97 struct cgraph_edge_hook_list
{
98 cgraph_edge_hook hook
;
100 struct cgraph_edge_hook_list
*next
;
103 /* List of hooks triggered on cgraph_node events. */
104 struct cgraph_node_hook_list
{
105 cgraph_node_hook hook
;
107 struct cgraph_node_hook_list
*next
;
110 /* List of hooks triggered on events involving two cgraph_edges. */
111 struct cgraph_2edge_hook_list
{
112 cgraph_2edge_hook hook
;
114 struct cgraph_2edge_hook_list
*next
;
117 /* List of hooks triggered on events involving two cgraph_nodes. */
118 struct cgraph_2node_hook_list
{
119 cgraph_2node_hook hook
;
121 struct cgraph_2node_hook_list
*next
;
124 /* List of hooks triggered when an edge is removed. */
125 struct cgraph_edge_hook_list
*first_cgraph_edge_removal_hook
;
126 /* List of hooks triggered when a node is removed. */
127 struct cgraph_node_hook_list
*first_cgraph_node_removal_hook
;
128 /* List of hooks triggered when an edge is duplicated. */
129 struct cgraph_2edge_hook_list
*first_cgraph_edge_duplicated_hook
;
130 /* List of hooks triggered when a node is duplicated. */
131 struct cgraph_2node_hook_list
*first_cgraph_node_duplicated_hook
;
132 /* List of hooks triggered when an function is inserted. */
133 struct cgraph_node_hook_list
*first_cgraph_function_insertion_hook
;
135 /* Head of a linked list of unused (freed) call graph nodes.
136 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
137 static GTY(()) struct cgraph_node
*free_nodes
;
138 /* Head of a linked list of unused (freed) call graph edges.
139 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
140 static GTY(()) struct cgraph_edge
*free_edges
;
142 /* Did procss_same_body_aliases run? */
143 bool cpp_implicit_aliases_done
;
145 /* Map a cgraph_node to cgraph_function_version_info using this htab.
146 The cgraph_function_version_info has a THIS_NODE field that is the
147 corresponding cgraph_node.. */
149 static GTY((param_is (struct cgraph_function_version_info
))) htab_t
150 cgraph_fnver_htab
= NULL
;
152 /* Hash function for cgraph_fnver_htab. */
154 cgraph_fnver_htab_hash (const void *ptr
)
156 int uid
= ((const struct cgraph_function_version_info
*)ptr
)->this_node
->uid
;
157 return (hashval_t
)(uid
);
160 /* eq function for cgraph_fnver_htab. */
162 cgraph_fnver_htab_eq (const void *p1
, const void *p2
)
164 const struct cgraph_function_version_info
*n1
165 = (const struct cgraph_function_version_info
*)p1
;
166 const struct cgraph_function_version_info
*n2
167 = (const struct cgraph_function_version_info
*)p2
;
169 return n1
->this_node
->uid
== n2
->this_node
->uid
;
172 /* Mark as GC root all allocated nodes. */
173 static GTY(()) struct cgraph_function_version_info
*
174 version_info_node
= NULL
;
176 /* Get the cgraph_function_version_info node corresponding to node. */
177 struct cgraph_function_version_info
*
178 cgraph_node::function_version (void)
180 struct cgraph_function_version_info
*ret
;
181 struct cgraph_function_version_info key
;
182 key
.this_node
= this;
184 if (cgraph_fnver_htab
== NULL
)
187 ret
= (struct cgraph_function_version_info
*)
188 htab_find (cgraph_fnver_htab
, &key
);
193 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
194 corresponding to cgraph_node NODE. */
195 struct cgraph_function_version_info
*
196 cgraph_node::insert_new_function_version (void)
200 version_info_node
= NULL
;
201 version_info_node
= ggc_cleared_alloc
<cgraph_function_version_info
> ();
202 version_info_node
->this_node
= this;
204 if (cgraph_fnver_htab
== NULL
)
205 cgraph_fnver_htab
= htab_create_ggc (2, cgraph_fnver_htab_hash
,
206 cgraph_fnver_htab_eq
, NULL
);
208 slot
= htab_find_slot (cgraph_fnver_htab
, version_info_node
, INSERT
);
209 gcc_assert (slot
!= NULL
);
210 *slot
= version_info_node
;
211 return version_info_node
;
214 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
215 DECL is a duplicate declaration. */
217 cgraph_node::delete_function_version (tree decl
)
219 struct cgraph_node
*decl_node
= cgraph_node::get (decl
);
220 struct cgraph_function_version_info
*decl_v
= NULL
;
222 if (decl_node
== NULL
)
225 decl_v
= decl_node
->function_version ();
230 if (decl_v
->prev
!= NULL
)
231 decl_v
->prev
->next
= decl_v
->next
;
233 if (decl_v
->next
!= NULL
)
234 decl_v
->next
->prev
= decl_v
->prev
;
236 if (cgraph_fnver_htab
!= NULL
)
237 htab_remove_elt (cgraph_fnver_htab
, decl_v
);
239 decl_node
->remove ();
242 /* Record that DECL1 and DECL2 are semantically identical function
245 cgraph_node::record_function_versions (tree decl1
, tree decl2
)
247 struct cgraph_node
*decl1_node
= cgraph_node::get_create (decl1
);
248 struct cgraph_node
*decl2_node
= cgraph_node::get_create (decl2
);
249 struct cgraph_function_version_info
*decl1_v
= NULL
;
250 struct cgraph_function_version_info
*decl2_v
= NULL
;
251 struct cgraph_function_version_info
*before
;
252 struct cgraph_function_version_info
*after
;
254 gcc_assert (decl1_node
!= NULL
&& decl2_node
!= NULL
);
255 decl1_v
= decl1_node
->function_version ();
256 decl2_v
= decl2_node
->function_version ();
258 if (decl1_v
!= NULL
&& decl2_v
!= NULL
)
262 decl1_v
= decl1_node
->insert_new_function_version ();
265 decl2_v
= decl2_node
->insert_new_function_version ();
267 /* Chain decl2_v and decl1_v. All semantically identical versions
268 will be chained together. */
273 while (before
->next
!= NULL
)
274 before
= before
->next
;
276 while (after
->prev
!= NULL
)
279 before
->next
= after
;
280 after
->prev
= before
;
283 /* Macros to access the next item in the list of free cgraph nodes and
285 #define NEXT_FREE_NODE(NODE) dyn_cast<cgraph_node *> ((NODE)->next)
286 #define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->next = NODE2
287 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
289 /* Register HOOK to be called with DATA on each removed edge. */
290 struct cgraph_edge_hook_list
*
291 cgraph_add_edge_removal_hook (cgraph_edge_hook hook
, void *data
)
293 struct cgraph_edge_hook_list
*entry
;
294 struct cgraph_edge_hook_list
**ptr
= &first_cgraph_edge_removal_hook
;
296 entry
= (struct cgraph_edge_hook_list
*) xmalloc (sizeof (*entry
));
306 /* Remove ENTRY from the list of hooks called on removing edges. */
308 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list
*entry
)
310 struct cgraph_edge_hook_list
**ptr
= &first_cgraph_edge_removal_hook
;
312 while (*ptr
!= entry
)
318 /* Call all edge removal hooks. */
320 cgraph_call_edge_removal_hooks (struct cgraph_edge
*e
)
322 struct cgraph_edge_hook_list
*entry
= first_cgraph_edge_removal_hook
;
325 entry
->hook (e
, entry
->data
);
330 /* Register HOOK to be called with DATA on each removed node. */
331 struct cgraph_node_hook_list
*
332 cgraph_add_node_removal_hook (cgraph_node_hook hook
, void *data
)
334 struct cgraph_node_hook_list
*entry
;
335 struct cgraph_node_hook_list
**ptr
= &first_cgraph_node_removal_hook
;
337 entry
= (struct cgraph_node_hook_list
*) xmalloc (sizeof (*entry
));
347 /* Remove ENTRY from the list of hooks called on removing nodes. */
349 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list
*entry
)
351 struct cgraph_node_hook_list
**ptr
= &first_cgraph_node_removal_hook
;
353 while (*ptr
!= entry
)
359 /* Call all node removal hooks. */
361 cgraph_call_node_removal_hooks (struct cgraph_node
*node
)
363 struct cgraph_node_hook_list
*entry
= first_cgraph_node_removal_hook
;
366 entry
->hook (node
, entry
->data
);
371 /* Register HOOK to be called with DATA on each inserted node. */
372 struct cgraph_node_hook_list
*
373 cgraph_add_function_insertion_hook (cgraph_node_hook hook
, void *data
)
375 struct cgraph_node_hook_list
*entry
;
376 struct cgraph_node_hook_list
**ptr
= &first_cgraph_function_insertion_hook
;
378 entry
= (struct cgraph_node_hook_list
*) xmalloc (sizeof (*entry
));
388 /* Remove ENTRY from the list of hooks called on inserted nodes. */
390 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list
*entry
)
392 struct cgraph_node_hook_list
**ptr
= &first_cgraph_function_insertion_hook
;
394 while (*ptr
!= entry
)
400 /* Call all node insertion hooks. */
402 cgraph_node::call_function_insertion_hooks (void)
404 struct cgraph_node_hook_list
*entry
= first_cgraph_function_insertion_hook
;
407 entry
->hook (this, entry
->data
);
412 /* Register HOOK to be called with DATA on each duplicated edge. */
413 struct cgraph_2edge_hook_list
*
414 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook
, void *data
)
416 struct cgraph_2edge_hook_list
*entry
;
417 struct cgraph_2edge_hook_list
**ptr
= &first_cgraph_edge_duplicated_hook
;
419 entry
= (struct cgraph_2edge_hook_list
*) xmalloc (sizeof (*entry
));
429 /* Remove ENTRY from the list of hooks called on duplicating edges. */
431 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list
*entry
)
433 struct cgraph_2edge_hook_list
**ptr
= &first_cgraph_edge_duplicated_hook
;
435 while (*ptr
!= entry
)
441 /* Call all edge duplication hooks. */
443 cgraph_call_edge_duplication_hooks (struct cgraph_edge
*cs1
,
444 struct cgraph_edge
*cs2
)
446 struct cgraph_2edge_hook_list
*entry
= first_cgraph_edge_duplicated_hook
;
449 entry
->hook (cs1
, cs2
, entry
->data
);
454 /* Register HOOK to be called with DATA on each duplicated node. */
455 struct cgraph_2node_hook_list
*
456 cgraph_add_node_duplication_hook (cgraph_2node_hook hook
, void *data
)
458 struct cgraph_2node_hook_list
*entry
;
459 struct cgraph_2node_hook_list
**ptr
= &first_cgraph_node_duplicated_hook
;
461 entry
= (struct cgraph_2node_hook_list
*) xmalloc (sizeof (*entry
));
471 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
473 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list
*entry
)
475 struct cgraph_2node_hook_list
**ptr
= &first_cgraph_node_duplicated_hook
;
477 while (*ptr
!= entry
)
483 /* Call all node duplication hooks. */
485 cgraph_node::call_duplication_hooks (struct cgraph_node
*node2
)
487 struct cgraph_2node_hook_list
*entry
= first_cgraph_node_duplicated_hook
;
490 entry
->hook (this, node2
, entry
->data
);
495 /* Allocate new callgraph node. */
497 static inline struct cgraph_node
*
498 cgraph_allocate_node (void)
500 struct cgraph_node
*node
;
505 free_nodes
= NEXT_FREE_NODE (node
);
509 node
= ggc_cleared_alloc
<cgraph_node
> ();
510 node
->uid
= cgraph_max_uid
++;
516 /* Allocate new callgraph node and insert it into basic data structures. */
519 cgraph_node::create_empty (void)
521 struct cgraph_node
*node
= cgraph_allocate_node ();
523 node
->type
= SYMTAB_FUNCTION
;
524 node
->frequency
= NODE_FREQUENCY_NORMAL
;
525 node
->count_materialization_scale
= REG_BR_PROB_BASE
;
530 /* Return cgraph node assigned to DECL. Create new one when needed. */
533 cgraph_node::create (tree decl
)
535 struct cgraph_node
*node
= cgraph_node::create_empty ();
536 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
539 node
->register_symbol ();
541 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
543 node
->origin
= cgraph_node::get_create (DECL_CONTEXT (decl
));
544 node
->next_nested
= node
->origin
->nested
;
545 node
->origin
->nested
= node
;
550 /* Try to find a call graph node for declaration DECL and if it does not exist
551 or if it corresponds to an inline clone, create a new one. */
554 cgraph_node::get_create (tree decl
)
556 struct cgraph_node
*first_clone
= cgraph_node::get (decl
);
558 if (first_clone
&& !first_clone
->global
.inlined_to
)
561 struct cgraph_node
*node
= cgraph_node::create (decl
);
564 first_clone
->clone_of
= node
;
565 node
->clones
= first_clone
;
566 symtab_prevail_in_asm_name_hash (node
);
567 node
->decl
->decl_with_vis
.symtab_node
= node
;
569 fprintf (dump_file
, "Introduced new external node "
570 "(%s/%i) and turned into root of the clone tree.\n",
571 xstrdup (node
->name ()), node
->order
);
574 fprintf (dump_file
, "Introduced new external node "
575 "(%s/%i).\n", xstrdup (node
->name ()),
580 /* Mark ALIAS as an alias to DECL. DECL_NODE is cgraph node representing
581 the function body is associated with (not necessarily cgraph_node (DECL). */
584 cgraph_node::create_alias (tree alias
, tree target
)
586 cgraph_node
*alias_node
;
588 gcc_assert (TREE_CODE (target
) == FUNCTION_DECL
589 || TREE_CODE (target
) == IDENTIFIER_NODE
);
590 gcc_assert (TREE_CODE (alias
) == FUNCTION_DECL
);
591 alias_node
= cgraph_node::get_create (alias
);
592 gcc_assert (!alias_node
->definition
);
593 alias_node
->alias_target
= target
;
594 alias_node
->definition
= true;
595 alias_node
->alias
= true;
596 if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias
)) != NULL
)
597 alias_node
->weakref
= true;
601 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
603 Same body aliases are output whenever the body of DECL is output,
604 and cgraph_node::get (ALIAS) transparently returns
605 cgraph_node::get (DECL). */
608 cgraph_node::create_same_body_alias (tree alias
, tree decl
)
610 struct cgraph_node
*n
;
611 #ifndef ASM_OUTPUT_DEF
612 /* If aliases aren't supported by the assembler, fail. */
615 /* Langhooks can create same body aliases of symbols not defined.
616 Those are useless. Drop them on the floor. */
617 if (cgraph_global_info_ready
)
620 n
= cgraph_node::create_alias (alias
, decl
);
621 n
->cpp_implicit_alias
= true;
622 if (cpp_implicit_aliases_done
)
623 n
->resolve_alias (cgraph_node::get (decl
));
627 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
628 aliases DECL with an adjustments made into the first parameter.
629 See comments in thunk_adjust for detail on the parameters. */
632 cgraph_node::create_thunk (tree alias
, tree
, bool this_adjusting
,
633 HOST_WIDE_INT fixed_offset
,
634 HOST_WIDE_INT virtual_value
,
638 struct cgraph_node
*node
;
640 node
= cgraph_node::get (alias
);
644 node
= cgraph_node::create (alias
);
645 gcc_checking_assert (!virtual_offset
646 || wi::eq_p (virtual_offset
, virtual_value
));
647 node
->thunk
.fixed_offset
= fixed_offset
;
648 node
->thunk
.this_adjusting
= this_adjusting
;
649 node
->thunk
.virtual_value
= virtual_value
;
650 node
->thunk
.virtual_offset_p
= virtual_offset
!= NULL
;
651 node
->thunk
.alias
= real_alias
;
652 node
->thunk
.thunk_p
= true;
653 node
->definition
= true;
658 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
659 Return NULL if there's no such node. */
662 cgraph_node::get_for_asmname (tree asmname
)
664 /* We do not want to look at inline clones. */
665 for (symtab_node
*node
= symtab_node_for_asm (asmname
);
667 node
= node
->next_sharing_asm_name
)
669 cgraph_node
*cn
= dyn_cast
<cgraph_node
*> (node
);
670 if (cn
&& !cn
->global
.inlined_to
)
676 /* Returns a hash value for X (which really is a cgraph_edge). */
679 edge_hash (const void *x
)
681 return htab_hash_pointer (((const struct cgraph_edge
*) x
)->call_stmt
);
684 /* Return nonzero if the call_stmt of of cgraph_edge X is stmt *Y. */
687 edge_eq (const void *x
, const void *y
)
689 return ((const struct cgraph_edge
*) x
)->call_stmt
== y
;
692 /* Add call graph edge E to call site hash of its caller. */
695 cgraph_update_edge_in_call_site_hash (struct cgraph_edge
*e
)
698 slot
= htab_find_slot_with_hash (e
->caller
->call_site_hash
,
700 htab_hash_pointer (e
->call_stmt
),
705 /* Add call graph edge E to call site hash of its caller. */
708 cgraph_add_edge_to_call_site_hash (struct cgraph_edge
*e
)
711 /* There are two speculative edges for every statement (one direct,
712 one indirect); always hash the direct one. */
713 if (e
->speculative
&& e
->indirect_unknown_callee
)
715 slot
= htab_find_slot_with_hash (e
->caller
->call_site_hash
,
717 htab_hash_pointer (e
->call_stmt
),
721 gcc_assert (((struct cgraph_edge
*)*slot
)->speculative
);
726 gcc_assert (!*slot
|| e
->speculative
);
730 /* Return the callgraph edge representing the GIMPLE_CALL statement
734 cgraph_node::get_edge (gimple call_stmt
)
736 struct cgraph_edge
*e
, *e2
;
740 return (struct cgraph_edge
*)
741 htab_find_with_hash (call_site_hash
, call_stmt
,
742 htab_hash_pointer (call_stmt
));
744 /* This loop may turn out to be performance problem. In such case adding
745 hashtables into call nodes with very many edges is probably best
746 solution. It is not good idea to add pointer into CALL_EXPR itself
747 because we want to make possible having multiple cgraph nodes representing
748 different clones of the same body before the body is actually cloned. */
749 for (e
= callees
; e
; e
= e
->next_callee
)
751 if (e
->call_stmt
== call_stmt
)
757 for (e
= indirect_calls
; e
; e
= e
->next_callee
)
759 if (e
->call_stmt
== call_stmt
)
766 call_site_hash
= htab_create_ggc (120, edge_hash
, edge_eq
, NULL
);
767 for (e2
= callees
; e2
; e2
= e2
->next_callee
)
768 cgraph_add_edge_to_call_site_hash (e2
);
769 for (e2
= indirect_calls
; e2
; e2
= e2
->next_callee
)
770 cgraph_add_edge_to_call_site_hash (e2
);
777 /* Change field call_stmt of edge E to NEW_STMT.
778 If UPDATE_SPECULATIVE and E is any component of speculative
779 edge, then update all components. */
782 cgraph_set_call_stmt (struct cgraph_edge
*e
, gimple new_stmt
,
783 bool update_speculative
)
787 /* Speculative edges has three component, update all of them
789 if (update_speculative
&& e
->speculative
)
791 struct cgraph_edge
*direct
, *indirect
;
794 cgraph_speculative_call_info (e
, direct
, indirect
, ref
);
795 cgraph_set_call_stmt (direct
, new_stmt
, false);
796 cgraph_set_call_stmt (indirect
, new_stmt
, false);
797 ref
->stmt
= new_stmt
;
801 /* Only direct speculative edges go to call_site_hash. */
802 if (e
->caller
->call_site_hash
803 && (!e
->speculative
|| !e
->indirect_unknown_callee
))
805 htab_remove_elt_with_hash (e
->caller
->call_site_hash
,
807 htab_hash_pointer (e
->call_stmt
));
810 e
->call_stmt
= new_stmt
;
811 if (e
->indirect_unknown_callee
812 && (decl
= gimple_call_fndecl (new_stmt
)))
814 /* Constant propagation (and possibly also inlining?) can turn an
815 indirect call into a direct one. */
816 struct cgraph_node
*new_callee
= cgraph_node::get (decl
);
818 gcc_checking_assert (new_callee
);
819 e
= cgraph_make_edge_direct (e
, new_callee
);
822 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
823 e
->can_throw_external
= stmt_can_throw_external (new_stmt
);
825 if (e
->caller
->call_site_hash
)
826 cgraph_add_edge_to_call_site_hash (e
);
829 /* Allocate a cgraph_edge structure and fill it with data according to the
830 parameters of which only CALLEE can be NULL (when creating an indirect call
834 cgraph_node::create_edge (cgraph_node
*caller
, cgraph_node
*callee
,
835 gimple call_stmt
, gcov_type count
, int freq
,
836 bool indir_unknown_callee
)
840 /* LTO does not actually have access to the call_stmt since these
841 have not been loaded yet. */
844 /* This is a rather expensive check possibly triggering
845 construction of call stmt hashtable. */
846 #ifdef ENABLE_CHECKING
847 struct cgraph_edge
*e
;
848 gcc_checking_assert (
849 !(e
= caller
->get_edge (call_stmt
)) || e
->speculative
);
852 gcc_assert (is_gimple_call (call_stmt
));
858 free_edges
= NEXT_FREE_EDGE (edge
);
862 edge
= ggc_alloc
<struct cgraph_edge
> ();
863 edge
->uid
= cgraph_edge_max_uid
++;
867 edge
->caller
= caller
;
868 edge
->callee
= callee
;
869 edge
->prev_caller
= NULL
;
870 edge
->next_caller
= NULL
;
871 edge
->prev_callee
= NULL
;
872 edge
->next_callee
= NULL
;
873 edge
->lto_stmt_uid
= 0;
876 gcc_assert (count
>= 0);
877 edge
->frequency
= freq
;
878 gcc_assert (freq
>= 0);
879 gcc_assert (freq
<= CGRAPH_FREQ_MAX
);
881 edge
->call_stmt
= call_stmt
;
882 push_cfun (DECL_STRUCT_FUNCTION (caller
->decl
));
883 edge
->can_throw_external
884 = call_stmt
? stmt_can_throw_external (call_stmt
) : false;
887 && callee
&& callee
->decl
888 && !gimple_check_call_matching_types (call_stmt
, callee
->decl
,
890 edge
->call_stmt_cannot_inline_p
= true;
892 edge
->call_stmt_cannot_inline_p
= false;
894 edge
->indirect_info
= NULL
;
895 edge
->indirect_inlining_edge
= 0;
896 edge
->speculative
= false;
897 edge
->indirect_unknown_callee
= indir_unknown_callee
;
898 if (call_stmt
&& caller
->call_site_hash
)
899 cgraph_add_edge_to_call_site_hash (edge
);
904 /* Create edge from a given function to CALLEE in the cgraph. */
907 cgraph_node::create_edge (struct cgraph_node
*callee
,
908 gimple call_stmt
, gcov_type count
, int freq
)
910 cgraph_edge
*edge
= cgraph_node::create_edge (this, callee
, call_stmt
,
913 initialize_inline_failed (edge
);
915 edge
->next_caller
= callee
->callers
;
917 callee
->callers
->prev_caller
= edge
;
918 edge
->next_callee
= callees
;
920 callees
->prev_callee
= edge
;
922 callee
->callers
= edge
;
927 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
929 struct cgraph_indirect_call_info
*
930 cgraph_allocate_init_indirect_info (void)
932 struct cgraph_indirect_call_info
*ii
;
934 ii
= ggc_cleared_alloc
<cgraph_indirect_call_info
> ();
935 ii
->param_index
= -1;
939 /* Create an indirect edge with a yet-undetermined callee where the call
940 statement destination is a formal parameter of the caller with index
944 cgraph_node::create_indirect_edge (gimple call_stmt
, int ecf_flags
,
945 gcov_type count
, int freq
)
947 struct cgraph_edge
*edge
= cgraph_node::create_edge (this, NULL
, call_stmt
,
951 initialize_inline_failed (edge
);
953 edge
->indirect_info
= cgraph_allocate_init_indirect_info ();
954 edge
->indirect_info
->ecf_flags
= ecf_flags
;
956 /* Record polymorphic call info. */
958 && (target
= gimple_call_fn (call_stmt
))
959 && virtual_method_call_p (target
))
962 HOST_WIDE_INT otr_token
;
963 ipa_polymorphic_call_context context
;
965 get_polymorphic_call_info (decl
,
967 &otr_type
, &otr_token
,
968 &context
, call_stmt
);
970 /* Only record types can have virtual calls. */
971 gcc_assert (TREE_CODE (otr_type
) == RECORD_TYPE
);
972 edge
->indirect_info
->polymorphic
= true;
973 edge
->indirect_info
->param_index
= -1;
974 edge
->indirect_info
->otr_token
= otr_token
;
975 edge
->indirect_info
->otr_type
= otr_type
;
976 edge
->indirect_info
->outer_type
= context
.outer_type
;
977 edge
->indirect_info
->speculative_outer_type
978 = context
.speculative_outer_type
;
979 edge
->indirect_info
->offset
= context
.offset
;
980 edge
->indirect_info
->speculative_offset
= context
.speculative_offset
;
981 edge
->indirect_info
->maybe_in_construction
982 = context
.maybe_in_construction
;
983 edge
->indirect_info
->maybe_derived_type
= context
.maybe_derived_type
;
984 edge
->indirect_info
->speculative_maybe_derived_type
985 = context
.speculative_maybe_derived_type
;
988 edge
->next_callee
= indirect_calls
;
990 indirect_calls
->prev_callee
= edge
;
991 indirect_calls
= edge
;
996 /* Remove the edge E from the list of the callers of the callee. */
999 cgraph_edge_remove_callee (struct cgraph_edge
*e
)
1001 gcc_assert (!e
->indirect_unknown_callee
);
1003 e
->prev_caller
->next_caller
= e
->next_caller
;
1005 e
->next_caller
->prev_caller
= e
->prev_caller
;
1006 if (!e
->prev_caller
)
1007 e
->callee
->callers
= e
->next_caller
;
1010 /* Remove the edge E from the list of the callees of the caller. */
1013 cgraph_edge_remove_caller (struct cgraph_edge
*e
)
1016 e
->prev_callee
->next_callee
= e
->next_callee
;
1018 e
->next_callee
->prev_callee
= e
->prev_callee
;
1019 if (!e
->prev_callee
)
1021 if (e
->indirect_unknown_callee
)
1022 e
->caller
->indirect_calls
= e
->next_callee
;
1024 e
->caller
->callees
= e
->next_callee
;
1026 if (e
->caller
->call_site_hash
)
1027 htab_remove_elt_with_hash (e
->caller
->call_site_hash
,
1029 htab_hash_pointer (e
->call_stmt
));
1032 /* Put the edge onto the free list. */
1035 cgraph_free_edge (struct cgraph_edge
*e
)
1039 if (e
->indirect_info
)
1040 ggc_free (e
->indirect_info
);
1042 /* Clear out the edge so we do not dangle pointers. */
1043 memset (e
, 0, sizeof (*e
));
1045 NEXT_FREE_EDGE (e
) = free_edges
;
1049 /* Remove the edge E in the cgraph. */
1052 cgraph_remove_edge (struct cgraph_edge
*e
)
1054 /* Call all edge removal hooks. */
1055 cgraph_call_edge_removal_hooks (e
);
1057 if (!e
->indirect_unknown_callee
)
1058 /* Remove from callers list of the callee. */
1059 cgraph_edge_remove_callee (e
);
1061 /* Remove from callees list of the callers. */
1062 cgraph_edge_remove_caller (e
);
1064 /* Put the edge onto the free list. */
1065 cgraph_free_edge (e
);
1068 /* Set callee of call graph edge E and add it to the corresponding set of
1072 cgraph_set_edge_callee (struct cgraph_edge
*e
, struct cgraph_node
*n
)
1074 e
->prev_caller
= NULL
;
1076 n
->callers
->prev_caller
= e
;
1077 e
->next_caller
= n
->callers
;
1082 /* Turn edge E into speculative call calling N2. Update
1083 the profile so the direct call is taken COUNT times
1086 At clone materialization time, the indirect call E will
1089 if (call_dest == N2)
1094 At this time the function just creates the direct call,
1095 the referencd representing the if conditional and attaches
1096 them all to the orginal indirect call statement.
1098 Return direct edge created. */
1100 struct cgraph_edge
*
1101 cgraph_turn_edge_to_speculative (struct cgraph_edge
*e
,
1102 struct cgraph_node
*n2
,
1103 gcov_type direct_count
,
1104 int direct_frequency
)
1106 struct cgraph_node
*n
= e
->caller
;
1107 struct ipa_ref
*ref
= NULL
;
1108 struct cgraph_edge
*e2
;
1112 fprintf (dump_file
, "Indirect call -> speculative call"
1113 " %s/%i => %s/%i\n",
1114 xstrdup (n
->name ()), n
->order
,
1115 xstrdup (n2
->name ()), n2
->order
);
1117 e
->speculative
= true;
1118 e2
= n
->create_edge (n2
, e
->call_stmt
, direct_count
, direct_frequency
);
1119 initialize_inline_failed (e2
);
1120 e2
->speculative
= true;
1121 if (TREE_NOTHROW (n2
->decl
))
1122 e2
->can_throw_external
= false;
1124 e2
->can_throw_external
= e
->can_throw_external
;
1125 e2
->lto_stmt_uid
= e
->lto_stmt_uid
;
1126 e
->count
-= e2
->count
;
1127 e
->frequency
-= e2
->frequency
;
1128 cgraph_call_edge_duplication_hooks (e
, e2
);
1129 ref
= n
->add_reference (n2
, IPA_REF_ADDR
, e
->call_stmt
);
1130 ref
->lto_stmt_uid
= e
->lto_stmt_uid
;
1131 ref
->speculative
= e
->speculative
;
1132 n2
->mark_address_taken ();
1136 /* Speculative call consist of three components:
1137 1) an indirect edge representing the original call
1138 2) an direct edge representing the new call
1139 3) ADDR_EXPR reference representing the speculative check.
1140 All three components are attached to single statement (the indirect
1141 call) and if one of them exists, all of them must exist.
1143 Given speculative call edge E, return all three components.
1147 cgraph_speculative_call_info (struct cgraph_edge
*e
,
1148 struct cgraph_edge
*&direct
,
1149 struct cgraph_edge
*&indirect
,
1150 struct ipa_ref
*&reference
)
1152 struct ipa_ref
*ref
;
1154 struct cgraph_edge
*e2
;
1156 if (!e
->indirect_unknown_callee
)
1157 for (e2
= e
->caller
->indirect_calls
;
1158 e2
->call_stmt
!= e
->call_stmt
|| e2
->lto_stmt_uid
!= e
->lto_stmt_uid
;
1159 e2
= e2
->next_callee
)
1164 /* We can take advantage of the call stmt hash. */
1167 e
= e
->caller
->get_edge (e2
->call_stmt
);
1168 gcc_assert (e
->speculative
&& !e
->indirect_unknown_callee
);
1171 for (e
= e
->caller
->callees
;
1172 e2
->call_stmt
!= e
->call_stmt
1173 || e2
->lto_stmt_uid
!= e
->lto_stmt_uid
;
1177 gcc_assert (e
->speculative
&& e2
->speculative
);
1182 for (i
= 0; e
->caller
->iterate_reference (i
, ref
); i
++)
1183 if (ref
->speculative
1184 && ((ref
->stmt
&& ref
->stmt
== e
->call_stmt
)
1185 || (!ref
->stmt
&& ref
->lto_stmt_uid
== e
->lto_stmt_uid
)))
1191 /* Speculative edge always consist of all three components - direct edge,
1192 indirect and reference. */
1194 gcc_assert (e
&& e2
&& ref
);
1197 /* Redirect callee of E to N. The function does not update underlying
1201 cgraph_redirect_edge_callee (struct cgraph_edge
*e
, struct cgraph_node
*n
)
1203 /* Remove from callers list of the current callee. */
1204 cgraph_edge_remove_callee (e
);
1206 /* Insert to callers list of the new callee. */
1207 cgraph_set_edge_callee (e
, n
);
1210 /* Speculative call EDGE turned out to be direct call to CALLE_DECL.
1211 Remove the speculative call sequence and return edge representing the call.
1212 It is up to caller to redirect the call as appropriate. */
1214 struct cgraph_edge
*
1215 cgraph_resolve_speculation (struct cgraph_edge
*edge
, tree callee_decl
)
1217 struct cgraph_edge
*e2
;
1218 struct ipa_ref
*ref
;
1220 gcc_assert (edge
->speculative
);
1221 cgraph_speculative_call_info (edge
, e2
, edge
, ref
);
1223 || !ref
->referred
->semantically_equivalent_p
1224 (symtab_node::get (callee_decl
)))
1230 fprintf (dump_file
, "Speculative indirect call %s/%i => %s/%i has "
1231 "turned out to have contradicting known target ",
1232 xstrdup (edge
->caller
->name ()), edge
->caller
->order
,
1233 xstrdup (e2
->callee
->name ()), e2
->callee
->order
);
1234 print_generic_expr (dump_file
, callee_decl
, 0);
1235 fprintf (dump_file
, "\n");
1239 fprintf (dump_file
, "Removing speculative call %s/%i => %s/%i\n",
1240 xstrdup (edge
->caller
->name ()), edge
->caller
->order
,
1241 xstrdup (e2
->callee
->name ()), e2
->callee
->order
);
1247 struct cgraph_edge
*tmp
= edge
;
1249 fprintf (dump_file
, "Speculative call turned into direct call.\n");
1252 /* FIXME: If EDGE is inlined, we should scale up the frequencies and counts
1253 in the functions inlined through it. */
1255 edge
->count
+= e2
->count
;
1256 edge
->frequency
+= e2
->frequency
;
1257 if (edge
->frequency
> CGRAPH_FREQ_MAX
)
1258 edge
->frequency
= CGRAPH_FREQ_MAX
;
1259 edge
->speculative
= false;
1260 e2
->speculative
= false;
1261 ref
->remove_reference ();
1262 if (e2
->indirect_unknown_callee
|| e2
->inline_failed
)
1263 cgraph_remove_edge (e2
);
1265 e2
->callee
->remove_symbol_and_inline_clones ();
1266 if (edge
->caller
->call_site_hash
)
1267 cgraph_update_edge_in_call_site_hash (edge
);
1271 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1272 CALLEE. DELTA is an integer constant that is to be added to the this
1273 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1275 struct cgraph_edge
*
1276 cgraph_make_edge_direct (struct cgraph_edge
*edge
, struct cgraph_node
*callee
)
1278 gcc_assert (edge
->indirect_unknown_callee
);
1280 /* If we are redirecting speculative call, make it non-speculative. */
1281 if (edge
->indirect_unknown_callee
&& edge
->speculative
)
1283 edge
= cgraph_resolve_speculation (edge
, callee
->decl
);
1285 /* On successful speculation just return the pre existing direct edge. */
1286 if (!edge
->indirect_unknown_callee
)
1290 edge
->indirect_unknown_callee
= 0;
1291 ggc_free (edge
->indirect_info
);
1292 edge
->indirect_info
= NULL
;
1294 /* Get the edge out of the indirect edge list. */
1295 if (edge
->prev_callee
)
1296 edge
->prev_callee
->next_callee
= edge
->next_callee
;
1297 if (edge
->next_callee
)
1298 edge
->next_callee
->prev_callee
= edge
->prev_callee
;
1299 if (!edge
->prev_callee
)
1300 edge
->caller
->indirect_calls
= edge
->next_callee
;
1302 /* Put it into the normal callee list */
1303 edge
->prev_callee
= NULL
;
1304 edge
->next_callee
= edge
->caller
->callees
;
1305 if (edge
->caller
->callees
)
1306 edge
->caller
->callees
->prev_callee
= edge
;
1307 edge
->caller
->callees
= edge
;
1309 /* Insert to callers list of the new callee. */
1310 cgraph_set_edge_callee (edge
, callee
);
1312 if (edge
->call_stmt
)
1313 edge
->call_stmt_cannot_inline_p
1314 = !gimple_check_call_matching_types (edge
->call_stmt
, callee
->decl
,
1317 /* We need to re-determine the inlining status of the edge. */
1318 initialize_inline_failed (edge
);
1322 /* If necessary, change the function declaration in the call statement
1323 associated with E so that it corresponds to the edge callee. */
1326 cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge
*e
)
1328 tree decl
= gimple_call_fndecl (e
->call_stmt
);
1329 tree lhs
= gimple_call_lhs (e
->call_stmt
);
1331 gimple_stmt_iterator gsi
;
1332 #ifdef ENABLE_CHECKING
1333 struct cgraph_node
*node
;
1338 struct cgraph_edge
*e2
;
1340 struct ipa_ref
*ref
;
1342 cgraph_speculative_call_info (e
, e
, e2
, ref
);
1343 /* If there already is an direct call (i.e. as a result of inliner's
1344 substitution), forget about speculating. */
1346 e
= cgraph_resolve_speculation (e
, decl
);
1347 /* If types do not match, speculation was likely wrong.
1348 The direct edge was posisbly redirected to the clone with a different
1349 signature. We did not update the call statement yet, so compare it
1350 with the reference that still points to the proper type. */
1351 else if (!gimple_check_call_matching_types (e
->call_stmt
,
1352 ref
->referred
->decl
,
1356 fprintf (dump_file
, "Not expanding speculative call of %s/%i -> %s/%i\n"
1358 xstrdup (e
->caller
->name ()),
1360 xstrdup (e
->callee
->name ()),
1362 e
= cgraph_resolve_speculation (e
, NULL
);
1363 /* We are producing the final function body and will throw away the
1364 callgraph edges really soon. Reset the counts/frequencies to
1365 keep verifier happy in the case of roundoff errors. */
1366 e
->count
= gimple_bb (e
->call_stmt
)->count
;
1367 e
->frequency
= compute_call_stmt_bb_frequency
1368 (e
->caller
->decl
, gimple_bb (e
->call_stmt
));
1370 /* Expand speculation into GIMPLE code. */
1375 "Expanding speculative call of %s/%i -> %s/%i count:"
1377 xstrdup (e
->caller
->name ()),
1379 xstrdup (e
->callee
->name ()),
1382 gcc_assert (e2
->speculative
);
1383 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
1384 new_stmt
= gimple_ic (e
->call_stmt
, dyn_cast
<cgraph_node
*> (ref
->referred
),
1385 e
->count
|| e2
->count
1386 ? RDIV (e
->count
* REG_BR_PROB_BASE
,
1387 e
->count
+ e2
->count
)
1388 : e
->frequency
|| e2
->frequency
1389 ? RDIV (e
->frequency
* REG_BR_PROB_BASE
,
1390 e
->frequency
+ e2
->frequency
)
1391 : REG_BR_PROB_BASE
/ 2,
1392 e
->count
, e
->count
+ e2
->count
);
1393 e
->speculative
= false;
1394 e
->caller
->set_call_stmt_including_clones (e
->call_stmt
, new_stmt
,
1396 e
->frequency
= compute_call_stmt_bb_frequency
1397 (e
->caller
->decl
, gimple_bb (e
->call_stmt
));
1398 e2
->frequency
= compute_call_stmt_bb_frequency
1399 (e2
->caller
->decl
, gimple_bb (e2
->call_stmt
));
1400 e2
->speculative
= false;
1401 ref
->speculative
= false;
1403 /* Indirect edges are not both in the call site hash.
1405 if (e
->caller
->call_site_hash
)
1406 cgraph_update_edge_in_call_site_hash (e2
);
1408 /* Continue redirecting E to proper target. */
1412 if (e
->indirect_unknown_callee
1413 || decl
== e
->callee
->decl
)
1414 return e
->call_stmt
;
1416 #ifdef ENABLE_CHECKING
1419 node
= cgraph_node::get (decl
);
1420 gcc_assert (!node
|| !node
->clone
.combined_args_to_skip
);
1424 if (cgraph_dump_file
)
1426 fprintf (cgraph_dump_file
, "updating call of %s/%i -> %s/%i: ",
1427 xstrdup (e
->caller
->name ()), e
->caller
->order
,
1428 xstrdup (e
->callee
->name ()), e
->callee
->order
);
1429 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
1430 if (e
->callee
->clone
.combined_args_to_skip
)
1432 fprintf (cgraph_dump_file
, " combined args to skip: ");
1433 dump_bitmap (cgraph_dump_file
,
1434 e
->callee
->clone
.combined_args_to_skip
);
1438 if (e
->callee
->clone
.combined_args_to_skip
)
1443 = gimple_call_copy_skip_args (e
->call_stmt
,
1444 e
->callee
->clone
.combined_args_to_skip
);
1445 gimple_call_set_fndecl (new_stmt
, e
->callee
->decl
);
1446 gimple_call_set_fntype (new_stmt
, gimple_call_fntype (e
->call_stmt
));
1448 if (gimple_vdef (new_stmt
)
1449 && TREE_CODE (gimple_vdef (new_stmt
)) == SSA_NAME
)
1450 SSA_NAME_DEF_STMT (gimple_vdef (new_stmt
)) = new_stmt
;
1452 gsi
= gsi_for_stmt (e
->call_stmt
);
1453 gsi_replace (&gsi
, new_stmt
, false);
1454 /* We need to defer cleaning EH info on the new statement to
1455 fixup-cfg. We may not have dominator information at this point
1456 and thus would end up with unreachable blocks and have no way
1457 to communicate that we need to run CFG cleanup then. */
1458 lp_nr
= lookup_stmt_eh_lp (e
->call_stmt
);
1461 remove_stmt_from_eh_lp (e
->call_stmt
);
1462 add_stmt_to_eh_lp (new_stmt
, lp_nr
);
1467 new_stmt
= e
->call_stmt
;
1468 gimple_call_set_fndecl (new_stmt
, e
->callee
->decl
);
1469 update_stmt_fn (DECL_STRUCT_FUNCTION (e
->caller
->decl
), new_stmt
);
1472 /* If the call becomes noreturn, remove the lhs. */
1473 if (lhs
&& (gimple_call_flags (new_stmt
) & ECF_NORETURN
))
1475 if (TREE_CODE (lhs
) == SSA_NAME
)
1477 tree var
= create_tmp_reg_fn (DECL_STRUCT_FUNCTION (e
->caller
->decl
),
1478 TREE_TYPE (lhs
), NULL
);
1479 var
= get_or_create_ssa_default_def
1480 (DECL_STRUCT_FUNCTION (e
->caller
->decl
), var
);
1481 gimple set_stmt
= gimple_build_assign (lhs
, var
);
1482 gsi
= gsi_for_stmt (new_stmt
);
1483 gsi_insert_before_without_update (&gsi
, set_stmt
, GSI_SAME_STMT
);
1484 update_stmt_fn (DECL_STRUCT_FUNCTION (e
->caller
->decl
), set_stmt
);
1486 gimple_call_set_lhs (new_stmt
, NULL_TREE
);
1487 update_stmt_fn (DECL_STRUCT_FUNCTION (e
->caller
->decl
), new_stmt
);
1490 /* If new callee has no static chain, remove it. */
1491 if (gimple_call_chain (new_stmt
) && !DECL_STATIC_CHAIN (e
->callee
->decl
))
1493 gimple_call_set_chain (new_stmt
, NULL
);
1494 update_stmt_fn (DECL_STRUCT_FUNCTION (e
->caller
->decl
), new_stmt
);
1497 e
->caller
->set_call_stmt_including_clones (e
->call_stmt
, new_stmt
, false);
1499 if (cgraph_dump_file
)
1501 fprintf (cgraph_dump_file
, " updated to:");
1502 print_gimple_stmt (cgraph_dump_file
, e
->call_stmt
, 0, dump_flags
);
1507 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1508 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1509 of OLD_STMT if it was previously call statement.
1510 If NEW_STMT is NULL, the call has been dropped without any
1514 cgraph_update_edges_for_call_stmt_node (struct cgraph_node
*node
,
1515 gimple old_stmt
, tree old_call
,
1518 tree new_call
= (new_stmt
&& is_gimple_call (new_stmt
))
1519 ? gimple_call_fndecl (new_stmt
) : 0;
1521 /* We are seeing indirect calls, then there is nothing to update. */
1522 if (!new_call
&& !old_call
)
1524 /* See if we turned indirect call into direct call or folded call to one builtin
1525 into different builtin. */
1526 if (old_call
!= new_call
)
1528 struct cgraph_edge
*e
= node
->get_edge (old_stmt
);
1529 struct cgraph_edge
*ne
= NULL
;
1535 /* See if the edge is already there and has the correct callee. It
1536 might be so because of indirect inlining has already updated
1537 it. We also might've cloned and redirected the edge. */
1538 if (new_call
&& e
->callee
)
1540 struct cgraph_node
*callee
= e
->callee
;
1543 if (callee
->decl
== new_call
1544 || callee
->former_clone_of
== new_call
)
1546 cgraph_set_call_stmt (e
, new_stmt
);
1549 callee
= callee
->clone_of
;
1553 /* Otherwise remove edge and create new one; we can't simply redirect
1554 since function has changed, so inline plan and other information
1555 attached to edge is invalid. */
1557 frequency
= e
->frequency
;
1558 if (e
->indirect_unknown_callee
|| e
->inline_failed
)
1559 cgraph_remove_edge (e
);
1561 e
->callee
->remove_symbol_and_inline_clones ();
1565 /* We are seeing new direct call; compute profile info based on BB. */
1566 basic_block bb
= gimple_bb (new_stmt
);
1568 frequency
= compute_call_stmt_bb_frequency (current_function_decl
,
1574 ne
= node
->create_edge (cgraph_node::get_create (new_call
),
1575 new_stmt
, count
, frequency
);
1576 gcc_assert (ne
->inline_failed
);
1579 /* We only updated the call stmt; update pointer in cgraph edge.. */
1580 else if (old_stmt
!= new_stmt
)
1581 cgraph_set_call_stmt (node
->get_edge (old_stmt
), new_stmt
);
1584 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1585 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1586 of OLD_STMT before it was updated (updating can happen inplace). */
1589 cgraph_update_edges_for_call_stmt (gimple old_stmt
, tree old_decl
, gimple new_stmt
)
1591 struct cgraph_node
*orig
= cgraph_node::get (cfun
->decl
);
1592 struct cgraph_node
*node
;
1594 gcc_checking_assert (orig
);
1595 cgraph_update_edges_for_call_stmt_node (orig
, old_stmt
, old_decl
, new_stmt
);
1597 for (node
= orig
->clones
; node
!= orig
;)
1599 cgraph_update_edges_for_call_stmt_node (node
, old_stmt
, old_decl
, new_stmt
);
1601 node
= node
->clones
;
1602 else if (node
->next_sibling_clone
)
1603 node
= node
->next_sibling_clone
;
1606 while (node
!= orig
&& !node
->next_sibling_clone
)
1607 node
= node
->clone_of
;
1609 node
= node
->next_sibling_clone
;
1615 /* Remove all callees from the node. */
1618 cgraph_node::remove_callees (void)
1620 struct cgraph_edge
*e
, *f
;
1622 /* It is sufficient to remove the edges from the lists of callers of
1623 the callees. The callee list of the node can be zapped with one
1625 for (e
= callees
; e
; e
= f
)
1628 cgraph_call_edge_removal_hooks (e
);
1629 if (!e
->indirect_unknown_callee
)
1630 cgraph_edge_remove_callee (e
);
1631 cgraph_free_edge (e
);
1633 for (e
= indirect_calls
; e
; e
= f
)
1636 cgraph_call_edge_removal_hooks (e
);
1637 if (!e
->indirect_unknown_callee
)
1638 cgraph_edge_remove_callee (e
);
1639 cgraph_free_edge (e
);
1641 indirect_calls
= NULL
;
1645 htab_delete (call_site_hash
);
1646 call_site_hash
= NULL
;
1650 /* Remove all callers from the node. */
1653 cgraph_node::remove_callers (void)
1655 struct cgraph_edge
*e
, *f
;
1657 /* It is sufficient to remove the edges from the lists of callees of
1658 the callers. The caller list of the node can be zapped with one
1660 for (e
= callers
; e
; e
= f
)
1663 cgraph_call_edge_removal_hooks (e
);
1664 cgraph_edge_remove_caller (e
);
1665 cgraph_free_edge (e
);
1670 /* Helper function for cgraph_release_function_body and free_lang_data.
1671 It releases body from function DECL without having to inspect its
1672 possibly non-existent symtab node. */
1675 release_function_body (tree decl
)
1677 if (DECL_STRUCT_FUNCTION (decl
))
1679 push_cfun (DECL_STRUCT_FUNCTION (decl
));
1683 cfun
->curr_properties
&= ~PROP_loops
;
1684 loop_optimizer_finalize ();
1686 if (cfun
->gimple_df
)
1689 delete_tree_cfg_annotations ();
1694 gcc_assert (!dom_info_available_p (CDI_DOMINATORS
));
1695 gcc_assert (!dom_info_available_p (CDI_POST_DOMINATORS
));
1699 if (cfun
->value_histograms
)
1702 gimple_set_body (decl
, NULL
);
1703 /* Struct function hangs a lot of data that would leak if we didn't
1704 removed all pointers to it. */
1705 ggc_free (DECL_STRUCT_FUNCTION (decl
));
1706 DECL_STRUCT_FUNCTION (decl
) = NULL
;
1708 DECL_SAVED_TREE (decl
) = NULL
;
1711 /* Release memory used to represent body of function.
1712 Use this only for functions that are released before being translated to
1713 target code (i.e. RTL). Functions that are compiled to RTL and beyond
1714 are free'd in final.c via free_after_compilation(). */
1717 cgraph_node::release_body (void)
1719 ipa_transforms_to_apply
.release ();
1720 if (!used_as_abstract_origin
&& cgraph_state
!= CGRAPH_STATE_PARSING
)
1722 DECL_RESULT (decl
) = NULL
;
1723 DECL_ARGUMENTS (decl
) = NULL
;
1725 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1726 of its associated function function declaration because it's
1727 needed to emit debug info later. */
1728 if (!used_as_abstract_origin
&& DECL_INITIAL (decl
))
1729 DECL_INITIAL (decl
) = error_mark_node
;
1730 release_function_body (decl
);
1732 lto_free_function_in_decl_state_for_node (this);
1735 /* Remove function from symbol table. */
1738 cgraph_node::remove (void)
1740 struct cgraph_node
*n
;
1741 int uid
= this->uid
;
1743 cgraph_call_node_removal_hooks (this);
1746 ipa_transforms_to_apply
.release ();
1748 /* Incremental inlining access removed nodes stored in the postorder list.
1750 force_output
= false;
1751 forced_by_abi
= false;
1752 for (n
= nested
; n
; n
= n
->next_nested
)
1757 struct cgraph_node
**node2
= &origin
->nested
;
1759 while (*node2
!= this)
1760 node2
= &(*node2
)->next_nested
;
1761 *node2
= next_nested
;
1764 if (prev_sibling_clone
)
1765 prev_sibling_clone
->next_sibling_clone
= next_sibling_clone
;
1767 clone_of
->clones
= next_sibling_clone
;
1768 if (next_sibling_clone
)
1769 next_sibling_clone
->prev_sibling_clone
= prev_sibling_clone
;
1772 struct cgraph_node
*n
, *next
;
1776 for (n
= clones
; n
->next_sibling_clone
; n
= n
->next_sibling_clone
)
1777 n
->clone_of
= clone_of
;
1778 n
->clone_of
= clone_of
;
1779 n
->next_sibling_clone
= clone_of
->clones
;
1780 if (clone_of
->clones
)
1781 clone_of
->clones
->prev_sibling_clone
= n
;
1782 clone_of
->clones
= clones
;
1786 /* We are removing node with clones. This makes clones inconsistent,
1787 but assume they will be removed subsequently and just keep clone
1788 tree intact. This can happen in unreachable function removal since
1789 we remove unreachable functions in random order, not by bottom-up
1790 walk of clone trees. */
1791 for (n
= clones
; n
; n
= next
)
1793 next
= n
->next_sibling_clone
;
1794 n
->next_sibling_clone
= NULL
;
1795 n
->prev_sibling_clone
= NULL
;
1801 /* While all the clones are removed after being proceeded, the function
1802 itself is kept in the cgraph even after it is compiled. Check whether
1803 we are done with this body and reclaim it proactively if this is the case.
1805 if (cgraph_state
!= CGRAPH_LTO_STREAMING
)
1807 n
= cgraph_node::get (decl
);
1809 || (!n
->clones
&& !n
->clone_of
&& !n
->global
.inlined_to
1810 && (cgraph_global_info_ready
1811 && (TREE_ASM_WRITTEN (n
->decl
)
1812 || DECL_EXTERNAL (n
->decl
)
1814 || (!flag_wpa
&& n
->in_other_partition
)))))
1821 htab_delete (call_site_hash
);
1822 call_site_hash
= NULL
;
1826 /* Clear out the node to NULL all pointers and add the node to the free
1828 memset (this, 0, sizeof (*this));
1829 type
= SYMTAB_FUNCTION
;
1831 SET_NEXT_FREE_NODE (this, free_nodes
);
1835 /* Likewise indicate that a node is having address taken. */
1838 cgraph_node::mark_address_taken (void)
1840 /* Indirect inlining can figure out that all uses of the address are
1842 if (global
.inlined_to
)
1844 gcc_assert (cfun
->after_inlining
);
1845 gcc_assert (callers
->indirect_inlining_edge
);
1848 /* FIXME: address_taken flag is used both as a shortcut for testing whether
1849 IPA_REF_ADDR reference exists (and thus it should be set on node
1850 representing alias we take address of) and as a test whether address
1851 of the object was taken (and thus it should be set on node alias is
1852 referring to). We should remove the first use and the remove the
1855 cgraph_node
*node
= ultimate_alias_target ();
1856 node
->address_taken
= 1;
1859 /* Return local info for the compiled function. */
1861 struct cgraph_local_info
*
1862 cgraph_local_info (tree decl
)
1864 struct cgraph_node
*node
;
1866 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
1867 node
= cgraph_node::get (decl
);
1870 return &node
->local
;
1873 /* Return local info for the compiled function. */
1875 struct cgraph_global_info
*
1876 cgraph_global_info (tree decl
)
1878 struct cgraph_node
*node
;
1880 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
&& cgraph_global_info_ready
);
1881 node
= cgraph_node::get (decl
);
1884 return &node
->global
;
1887 /* Return local info for the compiled function. */
1889 struct cgraph_rtl_info
*
1890 cgraph_rtl_info (tree decl
)
1892 struct cgraph_node
*node
;
1894 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
1895 node
= cgraph_node::get (decl
);
1897 || (decl
!= current_function_decl
1898 && !TREE_ASM_WRITTEN (node
->decl
)))
1903 /* Return a string describing the failure REASON. */
1906 cgraph_inline_failed_string (cgraph_inline_failed_t reason
)
1909 #define DEFCIFCODE(code, type, string) string,
1911 static const char *cif_string_table
[CIF_N_REASONS
] = {
1912 #include "cif-code.def"
1915 /* Signedness of an enum type is implementation defined, so cast it
1916 to unsigned before testing. */
1917 gcc_assert ((unsigned) reason
< CIF_N_REASONS
);
1918 return cif_string_table
[reason
];
1921 /* Return a type describing the failure REASON. */
1923 cgraph_inline_failed_type_t
1924 cgraph_inline_failed_type (cgraph_inline_failed_t reason
)
1927 #define DEFCIFCODE(code, type, string) type,
1929 static cgraph_inline_failed_type_t cif_type_table
[CIF_N_REASONS
] = {
1930 #include "cif-code.def"
1933 /* Signedness of an enum type is implementation defined, so cast it
1934 to unsigned before testing. */
1935 gcc_assert ((unsigned) reason
< CIF_N_REASONS
);
1936 return cif_type_table
[reason
];
1939 /* Names used to print out the availability enum. */
1940 const char * const cgraph_availability_names
[] =
1941 {"unset", "not_available", "overwritable", "available", "local"};
1944 /* Dump call graph node to file F. */
1947 cgraph_node::dump (FILE *f
)
1949 struct cgraph_edge
*edge
;
1950 int indirect_calls_count
= 0;
1954 if (global
.inlined_to
)
1955 fprintf (f
, " Function %s/%i is inline copy in %s/%i\n",
1958 xstrdup (global
.inlined_to
->name ()),
1959 global
.inlined_to
->order
);
1961 fprintf (f
, " Clone of %s/%i\n",
1962 clone_of
->asm_name (),
1964 if (cgraph_function_flags_ready
)
1965 fprintf (f
, " Availability: %s\n",
1966 cgraph_availability_names
[get_availability ()]);
1969 fprintf (f
, " Profile id: %i\n",
1971 fprintf (f
, " First run: %i\n", tp_first_run
);
1972 fprintf (f
, " Function flags:");
1974 fprintf (f
, " executed %"PRId64
"x",
1977 fprintf (f
, " nested in: %s", origin
->asm_name ());
1978 if (gimple_has_body_p (decl
))
1979 fprintf (f
, " body");
1981 fprintf (f
, " process");
1983 fprintf (f
, " local");
1984 if (local
.redefined_extern_inline
)
1985 fprintf (f
, " redefined_extern_inline");
1986 if (only_called_at_startup
)
1987 fprintf (f
, " only_called_at_startup");
1988 if (only_called_at_exit
)
1989 fprintf (f
, " only_called_at_exit");
1991 fprintf (f
, " tm_clone");
1992 if (DECL_STATIC_CONSTRUCTOR (decl
))
1993 fprintf (f
," static_constructor (priority:%i)", get_init_priority ());
1994 if (DECL_STATIC_DESTRUCTOR (decl
))
1995 fprintf (f
," static_destructor (priority:%i)", get_fini_priority ());
2001 fprintf (f
, " Thunk");
2003 fprintf (f
, " of %s (asm: %s)",
2004 lang_hooks
.decl_printable_name (thunk
.alias
, 2),
2005 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk
.alias
)));
2006 fprintf (f
, " fixed offset %i virtual value %i has "
2007 "virtual offset %i)\n",
2008 (int)thunk
.fixed_offset
,
2009 (int)thunk
.virtual_value
,
2010 (int)thunk
.virtual_offset_p
);
2012 if (alias
&& thunk
.alias
2013 && DECL_P (thunk
.alias
))
2015 fprintf (f
, " Alias of %s",
2016 lang_hooks
.decl_printable_name (thunk
.alias
, 2));
2017 if (DECL_ASSEMBLER_NAME_SET_P (thunk
.alias
))
2018 fprintf (f
, " (asm: %s)",
2019 IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (thunk
.alias
)));
2023 fprintf (f
, " Called by: ");
2025 for (edge
= callers
; edge
; edge
= edge
->next_caller
)
2027 fprintf (f
, "%s/%i ", edge
->caller
->asm_name (),
2028 edge
->caller
->order
);
2030 fprintf (f
, "(%"PRId64
"x) ",
2031 (int64_t)edge
->count
);
2032 if (edge
->frequency
)
2033 fprintf (f
, "(%.2f per call) ",
2034 edge
->frequency
/ (double)CGRAPH_FREQ_BASE
);
2035 if (edge
->speculative
)
2036 fprintf (f
, "(speculative) ");
2037 if (!edge
->inline_failed
)
2038 fprintf (f
, "(inlined) ");
2039 if (edge
->indirect_inlining_edge
)
2040 fprintf (f
, "(indirect_inlining) ");
2041 if (edge
->can_throw_external
)
2042 fprintf (f
, "(can throw external) ");
2045 fprintf (f
, "\n Calls: ");
2046 for (edge
= callees
; edge
; edge
= edge
->next_callee
)
2048 fprintf (f
, "%s/%i ", edge
->callee
->asm_name (),
2049 edge
->callee
->order
);
2050 if (edge
->speculative
)
2051 fprintf (f
, "(speculative) ");
2052 if (!edge
->inline_failed
)
2053 fprintf (f
, "(inlined) ");
2054 if (edge
->indirect_inlining_edge
)
2055 fprintf (f
, "(indirect_inlining) ");
2057 fprintf (f
, "(%"PRId64
"x) ",
2058 (int64_t)edge
->count
);
2059 if (edge
->frequency
)
2060 fprintf (f
, "(%.2f per call) ",
2061 edge
->frequency
/ (double)CGRAPH_FREQ_BASE
);
2062 if (edge
->can_throw_external
)
2063 fprintf (f
, "(can throw external) ");
2067 for (edge
= indirect_calls
; edge
; edge
= edge
->next_callee
)
2068 indirect_calls_count
++;
2069 if (indirect_calls_count
)
2070 fprintf (f
, " Has %i outgoing edges for indirect calls.\n",
2071 indirect_calls_count
);
2074 /* Dump call graph node NODE to stderr. */
2077 cgraph_node::debug (void)
2082 /* Dump the callgraph to file F. */
2085 cgraph_node::dump_cgraph (FILE *f
)
2087 struct cgraph_node
*node
;
2089 fprintf (f
, "callgraph:\n\n");
2090 FOR_EACH_FUNCTION (node
)
2094 /* Return true when the DECL can possibly be inlined. */
2097 cgraph_function_possibly_inlined_p (tree decl
)
2099 if (!cgraph_global_info_ready
)
2100 return !DECL_UNINLINABLE (decl
);
2101 return DECL_POSSIBLY_INLINED (decl
);
2104 /* cgraph_node is no longer nested function; update cgraph accordingly. */
2106 cgraph_node::unnest (void)
2108 struct cgraph_node
**node2
= &origin
->nested
;
2109 gcc_assert (origin
);
2111 while (*node2
!= this)
2112 node2
= &(*node2
)->next_nested
;
2113 *node2
= next_nested
;
2117 /* Return function availability. See cgraph.h for description of individual
2120 cgraph_node::get_availability (void)
2122 enum availability avail
;
2124 avail
= AVAIL_NOT_AVAILABLE
;
2125 else if (local
.local
)
2126 avail
= AVAIL_LOCAL
;
2127 else if (alias
&& weakref
)
2128 ultimate_alias_target (&avail
);
2129 else if (lookup_attribute ("ifunc", DECL_ATTRIBUTES (decl
)))
2130 avail
= AVAIL_INTERPOSABLE
;
2131 else if (!externally_visible
)
2132 avail
= AVAIL_AVAILABLE
;
2133 /* Inline functions are safe to be analyzed even if their symbol can
2134 be overwritten at runtime. It is not meaningful to enforce any sane
2135 behaviour on replacing inline function by different body. */
2136 else if (DECL_DECLARED_INLINE_P (decl
))
2137 avail
= AVAIL_AVAILABLE
;
2139 /* If the function can be overwritten, return OVERWRITABLE. Take
2140 care at least of two notable extensions - the COMDAT functions
2141 used to share template instantiations in C++ (this is symmetric
2142 to code cp_cannot_inline_tree_fn and probably shall be shared and
2143 the inlinability hooks completely eliminated).
2145 ??? Does the C++ one definition rule allow us to always return
2146 AVAIL_AVAILABLE here? That would be good reason to preserve this
2149 else if (decl_replaceable_p (decl
) && !DECL_EXTERNAL (decl
))
2150 avail
= AVAIL_INTERPOSABLE
;
2151 else avail
= AVAIL_AVAILABLE
;
2156 /* Worker for cgraph_node_can_be_local_p. */
2158 cgraph_node_cannot_be_local_p_1 (struct cgraph_node
*node
, void *)
2160 return !(!node
->force_output
2161 && ((DECL_COMDAT (node
->decl
)
2162 && !node
->forced_by_abi
2163 && !node
->used_from_object_file_p ()
2164 && !node
->same_comdat_group
)
2165 || !node
->externally_visible
));
2168 /* Return true if cgraph_node can be made local for API change.
2169 Extern inline functions and C++ COMDAT functions can be made local
2170 at the expense of possible code size growth if function is used in multiple
2171 compilation units. */
2173 cgraph_node::can_be_local_p (void)
2175 return (!address_taken
2176 && !call_for_symbol_thunks_and_aliases (cgraph_node_cannot_be_local_p_1
,
2180 /* Call calback on cgraph_node, thunks and aliases associated to cgraph_node.
2181 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2185 cgraph_node::call_for_symbol_thunks_and_aliases (bool (*callback
)
2186 (cgraph_node
*, void *),
2188 bool include_overwritable
)
2190 struct cgraph_edge
*e
;
2191 struct ipa_ref
*ref
;
2193 if (callback (this, data
))
2195 for (e
= callers
; e
; e
= e
->next_caller
)
2196 if (e
->caller
->thunk
.thunk_p
2197 && (include_overwritable
2198 || e
->caller
->get_availability () > AVAIL_INTERPOSABLE
))
2199 if (e
->caller
->call_for_symbol_thunks_and_aliases (callback
, data
,
2200 include_overwritable
))
2203 FOR_EACH_ALIAS (this, ref
)
2205 struct cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
2206 if (include_overwritable
2207 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
2208 if (alias
->call_for_symbol_thunks_and_aliases (callback
, data
,
2209 include_overwritable
))
2215 /* Call calback on function and aliases associated to the function.
2216 When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
2220 cgraph_node::call_for_symbol_and_aliases (bool (*callback
) (cgraph_node
*,
2223 bool include_overwritable
)
2225 struct ipa_ref
*ref
;
2227 if (callback (this, data
))
2230 FOR_EACH_ALIAS (this, ref
)
2232 struct cgraph_node
*alias
= dyn_cast
<cgraph_node
*> (ref
->referring
);
2233 if (include_overwritable
2234 || alias
->get_availability () > AVAIL_INTERPOSABLE
)
2235 if (alias
->call_for_symbol_and_aliases (callback
, data
,
2236 include_overwritable
))
2242 /* Worker to bring NODE local. */
2245 cgraph_node::make_local (struct cgraph_node
*node
, void *)
2247 gcc_checking_assert (node
->can_be_local_p ());
2248 if (DECL_COMDAT (node
->decl
) || DECL_EXTERNAL (node
->decl
))
2250 node
->make_decl_local ();
2251 node
->set_section (NULL
);
2252 node
->set_comdat_group (NULL
);
2253 node
->externally_visible
= false;
2254 node
->forced_by_abi
= false;
2255 node
->local
.local
= true;
2256 node
->set_section (NULL
);
2257 node
->unique_name
= (node
->resolution
== LDPR_PREVAILING_DEF_IRONLY
2258 || node
->resolution
== LDPR_PREVAILING_DEF_IRONLY_EXP
);
2259 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
2260 gcc_assert (node
->get_availability () == AVAIL_LOCAL
);
2265 /* Bring cgraph node local. */
2268 cgraph_node::make_local (void)
2270 call_for_symbol_thunks_and_aliases (cgraph_node::make_local
, NULL
, true);
2273 /* Worker to set nothrow flag. */
2276 cgraph_set_nothrow_flag_1 (struct cgraph_node
*node
, void *data
)
2278 struct cgraph_edge
*e
;
2280 TREE_NOTHROW (node
->decl
) = data
!= NULL
;
2283 for (e
= node
->callers
; e
; e
= e
->next_caller
)
2284 e
->can_throw_external
= false;
2288 /* Set TREE_NOTHROW on NODE's decl and on aliases of NODE
2289 if any to NOTHROW. */
2292 cgraph_node::set_nothrow_flag (bool nothrow
)
2294 call_for_symbol_thunks_and_aliases (cgraph_set_nothrow_flag_1
,
2295 (void *)(size_t)nothrow
, false);
2298 /* Worker to set const flag. */
2301 cgraph_set_const_flag_1 (struct cgraph_node
*node
, void *data
)
2303 /* Static constructors and destructors without a side effect can be
2305 if (data
&& !((size_t)data
& 2))
2307 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
2308 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
2309 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
2310 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
2312 TREE_READONLY (node
->decl
) = data
!= NULL
;
2313 DECL_LOOPING_CONST_OR_PURE_P (node
->decl
) = ((size_t)data
& 2) != 0;
2317 /* Set TREE_READONLY on cgraph_node's decl and on aliases of the node
2318 if any to READONLY. */
2321 cgraph_node::set_const_flag (bool readonly
, bool looping
)
2323 call_for_symbol_thunks_and_aliases (cgraph_set_const_flag_1
,
2324 (void *)(size_t)(readonly
+ (int)looping
* 2),
2328 /* Worker to set pure flag. */
2331 cgraph_set_pure_flag_1 (struct cgraph_node
*node
, void *data
)
2333 /* Static constructors and destructors without a side effect can be
2335 if (data
&& !((size_t)data
& 2))
2337 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
2338 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
2339 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
2340 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
2342 DECL_PURE_P (node
->decl
) = data
!= NULL
;
2343 DECL_LOOPING_CONST_OR_PURE_P (node
->decl
) = ((size_t)data
& 2) != 0;
2347 /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
2351 cgraph_node::set_pure_flag (bool pure
, bool looping
)
2353 call_for_symbol_thunks_and_aliases (cgraph_set_pure_flag_1
,
2354 (void *)(size_t)(pure
+ (int)looping
* 2),
2358 /* Return true when cgraph_node can not return or throw and thus
2359 it is safe to ignore its side effects for IPA analysis. */
2362 cgraph_node::cannot_return_p (void)
2364 int flags
= flags_from_decl_or_type (decl
);
2365 if (!flag_exceptions
)
2366 return (flags
& ECF_NORETURN
) != 0;
2368 return ((flags
& (ECF_NORETURN
| ECF_NOTHROW
))
2369 == (ECF_NORETURN
| ECF_NOTHROW
));
2372 /* Return true when call of E can not lead to return from caller
2373 and thus it is safe to ignore its side effects for IPA analysis
2374 when computing side effects of the caller.
2375 FIXME: We could actually mark all edges that have no reaching
2376 patch to the exit block or throw to get better results. */
2378 cgraph_edge_cannot_lead_to_return (struct cgraph_edge
*e
)
2380 if (e
->caller
->cannot_return_p ())
2382 if (e
->indirect_unknown_callee
)
2384 int flags
= e
->indirect_info
->ecf_flags
;
2385 if (!flag_exceptions
)
2386 return (flags
& ECF_NORETURN
) != 0;
2388 return ((flags
& (ECF_NORETURN
| ECF_NOTHROW
))
2389 == (ECF_NORETURN
| ECF_NOTHROW
));
2392 return e
->callee
->cannot_return_p ();
2395 /* Return true when function can be removed from callgraph
2396 if all direct calls are eliminated. */
2399 cgraph_node::can_remove_if_no_direct_calls_and_refs_p (void)
2401 gcc_assert (!global
.inlined_to
);
2402 /* Extern inlines can always go, we will use the external definition. */
2403 if (DECL_EXTERNAL (decl
))
2405 /* When function is needed, we can not remove it. */
2406 if (force_output
|| used_from_other_partition
)
2408 if (DECL_STATIC_CONSTRUCTOR (decl
)
2409 || DECL_STATIC_DESTRUCTOR (decl
))
2411 /* Only COMDAT functions can be removed if externally visible. */
2412 if (externally_visible
2413 && (!DECL_COMDAT (decl
)
2415 || used_from_object_file_p ()))
2420 /* Worker for cgraph_can_remove_if_no_direct_calls_p. */
2423 nonremovable_p (struct cgraph_node
*node
, void *)
2425 return !node
->can_remove_if_no_direct_calls_and_refs_p ();
2428 /* Return true when function cgraph_node and its aliases can be removed from
2429 callgraph if all direct calls are eliminated. */
2432 cgraph_node::can_remove_if_no_direct_calls_p (void)
2434 /* Extern inlines can always go, we will use the external definition. */
2435 if (DECL_EXTERNAL (decl
))
2439 return !call_for_symbol_and_aliases (nonremovable_p
, NULL
, true);
2442 /* Return true when function cgraph_node can be expected to be removed
2443 from program when direct calls in this compilation unit are removed.
2445 As a special case COMDAT functions are
2446 cgraph_can_remove_if_no_direct_calls_p while the are not
2447 cgraph_only_called_directly_p (it is possible they are called from other
2450 This function behaves as cgraph_only_called_directly_p because eliminating
2451 all uses of COMDAT function does not make it necessarily disappear from
2452 the program unless we are compiling whole program or we do LTO. In this
2453 case we know we win since dynamic linking will not really discard the
2454 linkonce section. */
2457 cgraph_node::will_be_removed_from_program_if_no_direct_calls_p (void)
2459 gcc_assert (!global
.inlined_to
);
2461 if (call_for_symbol_and_aliases (used_from_object_file_p_worker
,
2464 if (!in_lto_p
&& !flag_whole_program
)
2465 return only_called_directly_p ();
2468 if (DECL_EXTERNAL (decl
))
2470 return can_remove_if_no_direct_calls_p ();
2475 /* Worker for cgraph_only_called_directly_p. */
2478 cgraph_not_only_called_directly_p_1 (struct cgraph_node
*node
, void *)
2480 return !node
->only_called_directly_or_aliased_p ();
2483 /* Return true when function cgraph_node and all its aliases are only called
2485 i.e. it is not externally visible, address was not taken and
2486 it is not used in any other non-standard way. */
2489 cgraph_node::only_called_directly_p (void)
2491 gcc_assert (ultimate_alias_target () == this);
2492 return !call_for_symbol_and_aliases (cgraph_not_only_called_directly_p_1
,
2497 /* Collect all callers of NODE. Worker for collect_callers_of_node. */
2500 collect_callers_of_node_1 (struct cgraph_node
*node
, void *data
)
2502 vec
<cgraph_edge
*> *redirect_callers
= (vec
<cgraph_edge
*> *)data
;
2503 struct cgraph_edge
*cs
;
2504 enum availability avail
;
2505 node
->ultimate_alias_target (&avail
);
2507 if (avail
> AVAIL_INTERPOSABLE
)
2508 for (cs
= node
->callers
; cs
!= NULL
; cs
= cs
->next_caller
)
2509 if (!cs
->indirect_inlining_edge
)
2510 redirect_callers
->safe_push (cs
);
2514 /* Collect all callers of cgraph_node and its aliases that are known to lead to
2515 cgraph_node (i.e. are not overwritable). */
2518 cgraph_node::collect_callers (void)
2520 vec
<cgraph_edge
*> redirect_callers
= vNULL
;
2521 call_for_symbol_thunks_and_aliases (collect_callers_of_node_1
,
2522 &redirect_callers
, false);
2523 return redirect_callers
;
2526 /* Return TRUE if NODE2 a clone of NODE or is equivalent to it. */
2529 clone_of_p (struct cgraph_node
*node
, struct cgraph_node
*node2
)
2531 bool skipped_thunk
= false;
2532 node
= node
->ultimate_alias_target ();
2533 node2
= node2
->ultimate_alias_target ();
2535 /* There are no virtual clones of thunks so check former_clone_of or if we
2536 might have skipped thunks because this adjustments are no longer
2538 while (node
->thunk
.thunk_p
)
2540 if (node2
->former_clone_of
== node
->decl
)
2542 if (!node
->thunk
.this_adjusting
)
2544 node
= node
->callees
->callee
->ultimate_alias_target ();
2545 skipped_thunk
= true;
2550 if (!node2
->clone
.args_to_skip
2551 || !bitmap_bit_p (node2
->clone
.args_to_skip
, 0))
2553 if (node2
->former_clone_of
== node
->decl
)
2555 else if (!node2
->clone_of
)
2559 while (node
!= node2
&& node2
)
2560 node2
= node2
->clone_of
;
2561 return node2
!= NULL
;
2564 /* Verify edge E count and frequency. */
2567 verify_edge_count_and_frequency (struct cgraph_edge
*e
)
2569 bool error_found
= false;
2572 error ("caller edge count is negative");
2575 if (e
->frequency
< 0)
2577 error ("caller edge frequency is negative");
2580 if (e
->frequency
> CGRAPH_FREQ_MAX
)
2582 error ("caller edge frequency is too large");
2585 if (gimple_has_body_p (e
->caller
->decl
)
2586 && !e
->caller
->global
.inlined_to
2588 /* FIXME: Inline-analysis sets frequency to 0 when edge is optimized out.
2589 Remove this once edges are actually removed from the function at that time. */
2591 || (inline_edge_summary_vec
.exists ()
2592 && ((inline_edge_summary_vec
.length () <= (unsigned) e
->uid
)
2593 || !inline_edge_summary (e
)->predicate
)))
2595 != compute_call_stmt_bb_frequency (e
->caller
->decl
,
2596 gimple_bb (e
->call_stmt
))))
2598 error ("caller edge frequency %i does not match BB frequency %i",
2600 compute_call_stmt_bb_frequency (e
->caller
->decl
,
2601 gimple_bb (e
->call_stmt
)));
2607 /* Switch to THIS_CFUN if needed and print STMT to stderr. */
2609 cgraph_debug_gimple_stmt (struct function
*this_cfun
, gimple stmt
)
2611 bool fndecl_was_null
= false;
2612 /* debug_gimple_stmt needs correct cfun */
2613 if (cfun
!= this_cfun
)
2614 set_cfun (this_cfun
);
2615 /* ...and an actual current_function_decl */
2616 if (!current_function_decl
)
2618 current_function_decl
= this_cfun
->decl
;
2619 fndecl_was_null
= true;
2621 debug_gimple_stmt (stmt
);
2622 if (fndecl_was_null
)
2623 current_function_decl
= NULL
;
2626 /* Verify that call graph edge E corresponds to DECL from the associated
2627 statement. Return true if the verification should fail. */
2630 verify_edge_corresponds_to_fndecl (struct cgraph_edge
*e
, tree decl
)
2632 struct cgraph_node
*node
;
2634 if (!decl
|| e
->callee
->global
.inlined_to
)
2636 if (cgraph_state
== CGRAPH_LTO_STREAMING
)
2638 node
= cgraph_node::get (decl
);
2640 /* We do not know if a node from a different partition is an alias or what it
2641 aliases and therefore cannot do the former_clone_of check reliably. When
2642 body_removed is set, we have lost all information about what was alias or
2643 thunk of and also cannot proceed. */
2645 || node
->body_removed
2646 || node
->in_other_partition
2647 || e
->callee
->in_other_partition
)
2650 node
= node
->ultimate_alias_target ();
2652 /* Optimizers can redirect unreachable calls or calls triggering undefined
2653 behaviour to builtin_unreachable. */
2654 if (DECL_BUILT_IN_CLASS (e
->callee
->decl
) == BUILT_IN_NORMAL
2655 && DECL_FUNCTION_CODE (e
->callee
->decl
) == BUILT_IN_UNREACHABLE
)
2658 if (e
->callee
->former_clone_of
!= node
->decl
2659 && (node
!= e
->callee
->ultimate_alias_target ())
2660 && !clone_of_p (node
, e
->callee
))
2666 /* Verify cgraph nodes of given cgraph node. */
2668 cgraph_node::verify_node (void)
2670 struct cgraph_edge
*e
;
2671 struct function
*this_cfun
= DECL_STRUCT_FUNCTION (decl
);
2672 basic_block this_block
;
2673 gimple_stmt_iterator gsi
;
2674 bool error_found
= false;
2679 timevar_push (TV_CGRAPH_VERIFY
);
2680 error_found
|= verify_base ();
2681 for (e
= callees
; e
; e
= e
->next_callee
)
2684 error ("aux field set for edge %s->%s",
2685 identifier_to_locale (e
->caller
->name ()),
2686 identifier_to_locale (e
->callee
->name ()));
2691 error ("execution count is negative");
2694 if (global
.inlined_to
&& same_comdat_group
)
2696 error ("inline clone in same comdat group list");
2699 if (!definition
&& !in_other_partition
&& local
.local
)
2701 error ("local symbols must be defined");
2704 if (global
.inlined_to
&& externally_visible
)
2706 error ("externally visible inline clone");
2709 if (global
.inlined_to
&& address_taken
)
2711 error ("inline clone with address taken");
2714 if (global
.inlined_to
&& force_output
)
2716 error ("inline clone is forced to output");
2719 for (e
= indirect_calls
; e
; e
= e
->next_callee
)
2723 error ("aux field set for indirect edge from %s",
2724 identifier_to_locale (e
->caller
->name ()));
2727 if (!e
->indirect_unknown_callee
2728 || !e
->indirect_info
)
2730 error ("An indirect edge from %s is not marked as indirect or has "
2731 "associated indirect_info, the corresponding statement is: ",
2732 identifier_to_locale (e
->caller
->name ()));
2733 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2737 bool check_comdat
= comdat_local_p ();
2738 for (e
= callers
; e
; e
= e
->next_caller
)
2740 if (verify_edge_count_and_frequency (e
))
2743 && !in_same_comdat_group_p (e
->caller
))
2745 error ("comdat-local function called by %s outside its comdat",
2746 identifier_to_locale (e
->caller
->name ()));
2749 if (!e
->inline_failed
)
2751 if (global
.inlined_to
2752 != (e
->caller
->global
.inlined_to
2753 ? e
->caller
->global
.inlined_to
: e
->caller
))
2755 error ("inlined_to pointer is wrong");
2758 if (callers
->next_caller
)
2760 error ("multiple inline callers");
2765 if (global
.inlined_to
)
2767 error ("inlined_to pointer set for noninline callers");
2771 for (e
= indirect_calls
; e
; e
= e
->next_callee
)
2772 if (verify_edge_count_and_frequency (e
))
2774 if (!callers
&& global
.inlined_to
)
2776 error ("inlined_to pointer is set but no predecessors found");
2779 if (global
.inlined_to
== this)
2781 error ("inlined_to pointer refers to itself");
2787 struct cgraph_node
*n
;
2788 for (n
= clone_of
->clones
; n
; n
= n
->next_sibling_clone
)
2793 error ("cgraph_node has wrong clone_of");
2799 struct cgraph_node
*n
;
2800 for (n
= clones
; n
; n
= n
->next_sibling_clone
)
2801 if (n
->clone_of
!= this)
2805 error ("cgraph_node has wrong clone list");
2809 if ((prev_sibling_clone
|| next_sibling_clone
) && !clone_of
)
2811 error ("cgraph_node is in clone list but it is not clone");
2814 if (!prev_sibling_clone
&& clone_of
&& clone_of
->clones
!= this)
2816 error ("cgraph_node has wrong prev_clone pointer");
2819 if (prev_sibling_clone
&& prev_sibling_clone
->next_sibling_clone
!= this)
2821 error ("double linked list of clones corrupted");
2825 if (analyzed
&& alias
)
2827 bool ref_found
= false;
2829 struct ipa_ref
*ref
= NULL
;
2833 error ("Alias has call edges");
2836 for (i
= 0; iterate_reference (i
, ref
); i
++)
2837 if (ref
->use
!= IPA_REF_ALIAS
)
2839 error ("Alias has non-alias reference");
2844 error ("Alias has more than one alias reference");
2851 error ("Analyzed alias has no reference");
2855 if (analyzed
&& thunk
.thunk_p
)
2859 error ("No edge out of thunk node");
2862 else if (callees
->next_callee
)
2864 error ("More than one edge out of thunk node");
2867 if (gimple_has_body_p (decl
))
2869 error ("Thunk is not supposed to have body");
2873 else if (analyzed
&& gimple_has_body_p (decl
)
2874 && !TREE_ASM_WRITTEN (decl
)
2875 && (!DECL_EXTERNAL (decl
) || global
.inlined_to
)
2880 hash_set
<gimple
> stmts
;
2882 struct ipa_ref
*ref
= NULL
;
2884 /* Reach the trees by walking over the CFG, and note the
2885 enclosing basic-blocks in the call edges. */
2886 FOR_EACH_BB_FN (this_block
, this_cfun
)
2888 for (gsi
= gsi_start_phis (this_block
);
2889 !gsi_end_p (gsi
); gsi_next (&gsi
))
2890 stmts
.add (gsi_stmt (gsi
));
2891 for (gsi
= gsi_start_bb (this_block
);
2895 gimple stmt
= gsi_stmt (gsi
);
2897 if (is_gimple_call (stmt
))
2899 struct cgraph_edge
*e
= get_edge (stmt
);
2900 tree decl
= gimple_call_fndecl (stmt
);
2905 error ("shared call_stmt:");
2906 cgraph_debug_gimple_stmt (this_cfun
, stmt
);
2909 if (!e
->indirect_unknown_callee
)
2911 if (verify_edge_corresponds_to_fndecl (e
, decl
))
2913 error ("edge points to wrong declaration:");
2914 debug_tree (e
->callee
->decl
);
2915 fprintf (stderr
," Instead of:");
2922 error ("an indirect edge with unknown callee "
2923 "corresponding to a call_stmt with "
2924 "a known declaration:");
2926 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2932 error ("missing callgraph edge for call stmt:");
2933 cgraph_debug_gimple_stmt (this_cfun
, stmt
);
2939 for (i
= 0; iterate_reference (i
, ref
); i
++)
2940 if (ref
->stmt
&& !stmts
.contains (ref
->stmt
))
2942 error ("reference to dead statement");
2943 cgraph_debug_gimple_stmt (this_cfun
, ref
->stmt
);
2948 /* No CFG available?! */
2951 for (e
= callees
; e
; e
= e
->next_callee
)
2955 error ("edge %s->%s has no corresponding call_stmt",
2956 identifier_to_locale (e
->caller
->name ()),
2957 identifier_to_locale (e
->callee
->name ()));
2958 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2963 for (e
= indirect_calls
; e
; e
= e
->next_callee
)
2965 if (!e
->aux
&& !e
->speculative
)
2967 error ("an indirect edge from %s has no corresponding call_stmt",
2968 identifier_to_locale (e
->caller
->name ()));
2969 cgraph_debug_gimple_stmt (this_cfun
, e
->call_stmt
);
2978 internal_error ("verify_cgraph_node failed");
2980 timevar_pop (TV_CGRAPH_VERIFY
);
2983 /* Verify whole cgraph structure. */
2985 cgraph_node::verify_cgraph_nodes (void)
2987 struct cgraph_node
*node
;
2992 FOR_EACH_FUNCTION (node
)
2996 /* Walk the alias chain to return the function cgraph_node is alias of.
2997 Walk through thunk, too.
2998 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
3001 cgraph_node::function_symbol (enum availability
*availability
)
3003 cgraph_node
*node
= NULL
;
3007 node
= ultimate_alias_target (availability
);
3008 if (node
->thunk
.thunk_p
)
3010 node
= node
->callees
->callee
;
3013 enum availability a
;
3014 a
= node
->get_availability ();
3015 if (a
< *availability
)
3018 node
= node
->ultimate_alias_target (availability
);
3020 } while (node
&& node
->thunk
.thunk_p
);
3024 /* When doing LTO, read cgraph_node's body from disk if it is not already
3028 cgraph_node::get_body (void)
3030 struct lto_file_decl_data
*file_data
;
3031 const char *data
, *name
;
3033 tree decl
= this->decl
;
3035 if (DECL_RESULT (decl
))
3038 gcc_assert (in_lto_p
);
3040 timevar_push (TV_IPA_LTO_GIMPLE_IN
);
3042 file_data
= lto_file_data
;
3043 name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
3045 /* We may have renamed the declaration, e.g., a static function. */
3046 name
= lto_get_decl_name_mapping (file_data
, name
);
3048 data
= lto_get_section_data (file_data
, LTO_section_function_body
,
3051 fatal_error ("%s: section %s is missing",
3052 file_data
->file_name
,
3055 gcc_assert (DECL_STRUCT_FUNCTION (decl
) == NULL
);
3057 lto_input_function_body (file_data
, this, data
);
3058 lto_stats
.num_function_bodies
++;
3059 lto_free_section_data (file_data
, LTO_section_function_body
, name
,
3061 lto_free_function_in_decl_state_for_node (this);
3063 timevar_pop (TV_IPA_LTO_GIMPLE_IN
);
3068 /* Verify if the type of the argument matches that of the function
3069 declaration. If we cannot verify this or there is a mismatch,
3073 gimple_check_call_args (gimple stmt
, tree fndecl
, bool args_count_match
)
3076 unsigned int i
, nargs
;
3078 /* Calls to internal functions always match their signature. */
3079 if (gimple_call_internal_p (stmt
))
3082 nargs
= gimple_call_num_args (stmt
);
3084 /* Get argument types for verification. */
3086 parms
= TYPE_ARG_TYPES (TREE_TYPE (fndecl
));
3088 parms
= TYPE_ARG_TYPES (gimple_call_fntype (stmt
));
3090 /* Verify if the type of the argument matches that of the function
3091 declaration. If we cannot verify this or there is a mismatch,
3093 if (fndecl
&& DECL_ARGUMENTS (fndecl
))
3095 for (i
= 0, p
= DECL_ARGUMENTS (fndecl
);
3097 i
++, p
= DECL_CHAIN (p
))
3100 /* We cannot distinguish a varargs function from the case
3101 of excess parameters, still deferring the inlining decision
3102 to the callee is possible. */
3105 arg
= gimple_call_arg (stmt
, i
);
3106 if (p
== error_mark_node
3107 || DECL_ARG_TYPE (p
) == error_mark_node
3108 || arg
== error_mark_node
3109 || (!types_compatible_p (DECL_ARG_TYPE (p
), TREE_TYPE (arg
))
3110 && !fold_convertible_p (DECL_ARG_TYPE (p
), arg
)))
3113 if (args_count_match
&& p
)
3118 for (i
= 0, p
= parms
; i
< nargs
; i
++, p
= TREE_CHAIN (p
))
3121 /* If this is a varargs function defer inlining decision
3125 arg
= gimple_call_arg (stmt
, i
);
3126 if (TREE_VALUE (p
) == error_mark_node
3127 || arg
== error_mark_node
3128 || TREE_CODE (TREE_VALUE (p
)) == VOID_TYPE
3129 || (!types_compatible_p (TREE_VALUE (p
), TREE_TYPE (arg
))
3130 && !fold_convertible_p (TREE_VALUE (p
), arg
)))
3142 /* Verify if the type of the argument and lhs of CALL_STMT matches
3143 that of the function declaration CALLEE. If ARGS_COUNT_MATCH is
3144 true, the arg count needs to be the same.
3145 If we cannot verify this or there is a mismatch, return false. */
3148 gimple_check_call_matching_types (gimple call_stmt
, tree callee
,
3149 bool args_count_match
)
3153 if ((DECL_RESULT (callee
)
3154 && !DECL_BY_REFERENCE (DECL_RESULT (callee
))
3155 && (lhs
= gimple_call_lhs (call_stmt
)) != NULL_TREE
3156 && !useless_type_conversion_p (TREE_TYPE (DECL_RESULT (callee
)),
3158 && !fold_convertible_p (TREE_TYPE (DECL_RESULT (callee
)), lhs
))
3159 || !gimple_check_call_args (call_stmt
, callee
, args_count_match
))
3164 #include "gt-cgraph.h"