1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /* This file contains basic routines manipulating call graph
26 The call-graph is data structure designed for intra-procedural optimization
27 but it is also used in non-unit-at-a-time compilation to allow easier code
30 The call-graph consist of nodes and edges represented via linked lists.
31 Each function (external or not) corresponds to the unique node.
33 The mapping from declarations to call-graph nodes is done using hash table
34 based on DECL_UID. The call-graph nodes are created lazily using
35 cgraph_node function when called for unknown declaration.
37 The callgraph at the moment does not represent all indirect calls or calls
38 from other compilation units. Flag NEEDED is set for each node that may be
39 accessed in such an invisible way and it shall be considered an entry point
42 On the other hand, the callgraph currently does contain some edges for
43 indirect calls with unknown callees which can be accessed through
44 indirect_calls field of a node. It should be noted however that at the
45 moment only calls which are potential candidates for indirect inlining are
48 Interprocedural information:
50 Callgraph is place to store data needed for interprocedural optimization.
51 All data structures are divided into three components: local_info that
52 is produced while analyzing the function, global_info that is result
53 of global walking of the callgraph on the end of compilation and
54 rtl_info used by RTL backend to propagate data from already compiled
55 functions to their callers.
57 Moreover, each node has a uid which can be used to keep information in
58 on-the-side arrays. UIDs are reused and therefore reasonably dense.
62 The function inlining information is decided in advance and maintained
63 in the callgraph as so called inline plan.
64 For each inlined call, the callee's node is cloned to represent the
65 new function copy produced by inliner.
66 Each inlined call gets a unique corresponding clone node of the callee
67 and the data structure is updated while inlining is performed, so
68 the clones are eliminated and their callee edges redirected to the
71 Each edge has "inline_failed" field. When the field is set to NULL,
72 the call will be inlined. When it is non-NULL it contains a reason
73 why inlining wasn't performed. */
77 #include "coretypes.h"
80 #include "tree-inline.h"
81 #include "langhooks.h"
88 #include "basic-block.h"
93 #include "tree-dump.h"
94 #include "tree-flow.h"
95 #include "value-prof.h"
97 #include "diagnostic-core.h"
99 #include "ipa-utils.h"
100 #include "lto-streamer.h"
102 const char * const ld_plugin_symbol_resolution_names
[]=
107 "prevailing_def_ironly",
115 static void cgraph_node_remove_callers (struct cgraph_node
*node
);
116 static inline void cgraph_edge_remove_caller (struct cgraph_edge
*e
);
117 static inline void cgraph_edge_remove_callee (struct cgraph_edge
*e
);
119 /* Hash table used to convert declarations into nodes. */
120 static GTY((param_is (struct cgraph_node
))) htab_t cgraph_hash
;
121 /* Hash table used to convert assembler names into nodes. */
122 static GTY((param_is (struct cgraph_node
))) htab_t assembler_name_hash
;
124 /* The linked list of cgraph nodes. */
125 struct cgraph_node
*cgraph_nodes
;
127 /* Queue of cgraph nodes scheduled to be lowered. */
128 struct cgraph_node
*cgraph_nodes_queue
;
130 /* Queue of cgraph nodes scheduled to be added into cgraph. This is a
131 secondary queue used during optimization to accommodate passes that
132 may generate new functions that need to be optimized and expanded. */
133 struct cgraph_node
*cgraph_new_nodes
;
135 /* Number of nodes in existence. */
138 /* Maximal uid used in cgraph nodes. */
141 /* Maximal uid used in cgraph edges. */
142 int cgraph_edge_max_uid
;
144 /* Maximal pid used for profiling */
147 /* Set when whole unit has been analyzed so we can access global info. */
148 bool cgraph_global_info_ready
= false;
150 /* What state callgraph is in right now. */
151 enum cgraph_state cgraph_state
= CGRAPH_STATE_CONSTRUCTION
;
153 /* Set when the cgraph is fully build and the basic flags are computed. */
154 bool cgraph_function_flags_ready
= false;
156 /* Linked list of cgraph asm nodes. */
157 struct cgraph_asm_node
*cgraph_asm_nodes
;
159 /* Last node in cgraph_asm_nodes. */
160 static GTY(()) struct cgraph_asm_node
*cgraph_asm_last_node
;
162 /* The order index of the next cgraph node to be created. This is
163 used so that we can sort the cgraph nodes in order by when we saw
164 them, to support -fno-toplevel-reorder. */
167 /* List of hooks triggered on cgraph_edge events. */
168 struct cgraph_edge_hook_list
{
169 cgraph_edge_hook hook
;
171 struct cgraph_edge_hook_list
*next
;
174 /* List of hooks triggered on cgraph_node events. */
175 struct cgraph_node_hook_list
{
176 cgraph_node_hook hook
;
178 struct cgraph_node_hook_list
*next
;
181 /* List of hooks triggered on events involving two cgraph_edges. */
182 struct cgraph_2edge_hook_list
{
183 cgraph_2edge_hook hook
;
185 struct cgraph_2edge_hook_list
*next
;
188 /* List of hooks triggered on events involving two cgraph_nodes. */
189 struct cgraph_2node_hook_list
{
190 cgraph_2node_hook hook
;
192 struct cgraph_2node_hook_list
*next
;
195 /* List of hooks triggered when an edge is removed. */
196 struct cgraph_edge_hook_list
*first_cgraph_edge_removal_hook
;
197 /* List of hooks triggered when a node is removed. */
198 struct cgraph_node_hook_list
*first_cgraph_node_removal_hook
;
199 /* List of hooks triggered when an edge is duplicated. */
200 struct cgraph_2edge_hook_list
*first_cgraph_edge_duplicated_hook
;
201 /* List of hooks triggered when a node is duplicated. */
202 struct cgraph_2node_hook_list
*first_cgraph_node_duplicated_hook
;
203 /* List of hooks triggered when an function is inserted. */
204 struct cgraph_node_hook_list
*first_cgraph_function_insertion_hook
;
206 /* Head of a linked list of unused (freed) call graph nodes.
207 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
208 static GTY(()) struct cgraph_node
*free_nodes
;
209 /* Head of a linked list of unused (freed) call graph edges.
210 Do not GTY((delete)) this list so UIDs gets reliably recycled. */
211 static GTY(()) struct cgraph_edge
*free_edges
;
213 /* Macros to access the next item in the list of free cgraph nodes and
215 #define NEXT_FREE_NODE(NODE) (NODE)->next
216 #define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
218 /* Register HOOK to be called with DATA on each removed edge. */
219 struct cgraph_edge_hook_list
*
220 cgraph_add_edge_removal_hook (cgraph_edge_hook hook
, void *data
)
222 struct cgraph_edge_hook_list
*entry
;
223 struct cgraph_edge_hook_list
**ptr
= &first_cgraph_edge_removal_hook
;
225 entry
= (struct cgraph_edge_hook_list
*) xmalloc (sizeof (*entry
));
235 /* Remove ENTRY from the list of hooks called on removing edges. */
237 cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list
*entry
)
239 struct cgraph_edge_hook_list
**ptr
= &first_cgraph_edge_removal_hook
;
241 while (*ptr
!= entry
)
247 /* Call all edge removal hooks. */
249 cgraph_call_edge_removal_hooks (struct cgraph_edge
*e
)
251 struct cgraph_edge_hook_list
*entry
= first_cgraph_edge_removal_hook
;
254 entry
->hook (e
, entry
->data
);
259 /* Register HOOK to be called with DATA on each removed node. */
260 struct cgraph_node_hook_list
*
261 cgraph_add_node_removal_hook (cgraph_node_hook hook
, void *data
)
263 struct cgraph_node_hook_list
*entry
;
264 struct cgraph_node_hook_list
**ptr
= &first_cgraph_node_removal_hook
;
266 entry
= (struct cgraph_node_hook_list
*) xmalloc (sizeof (*entry
));
276 /* Remove ENTRY from the list of hooks called on removing nodes. */
278 cgraph_remove_node_removal_hook (struct cgraph_node_hook_list
*entry
)
280 struct cgraph_node_hook_list
**ptr
= &first_cgraph_node_removal_hook
;
282 while (*ptr
!= entry
)
288 /* Call all node removal hooks. */
290 cgraph_call_node_removal_hooks (struct cgraph_node
*node
)
292 struct cgraph_node_hook_list
*entry
= first_cgraph_node_removal_hook
;
295 entry
->hook (node
, entry
->data
);
300 /* Register HOOK to be called with DATA on each inserted node. */
301 struct cgraph_node_hook_list
*
302 cgraph_add_function_insertion_hook (cgraph_node_hook hook
, void *data
)
304 struct cgraph_node_hook_list
*entry
;
305 struct cgraph_node_hook_list
**ptr
= &first_cgraph_function_insertion_hook
;
307 entry
= (struct cgraph_node_hook_list
*) xmalloc (sizeof (*entry
));
317 /* Remove ENTRY from the list of hooks called on inserted nodes. */
319 cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list
*entry
)
321 struct cgraph_node_hook_list
**ptr
= &first_cgraph_function_insertion_hook
;
323 while (*ptr
!= entry
)
329 /* Call all node insertion hooks. */
331 cgraph_call_function_insertion_hooks (struct cgraph_node
*node
)
333 struct cgraph_node_hook_list
*entry
= first_cgraph_function_insertion_hook
;
336 entry
->hook (node
, entry
->data
);
341 /* Register HOOK to be called with DATA on each duplicated edge. */
342 struct cgraph_2edge_hook_list
*
343 cgraph_add_edge_duplication_hook (cgraph_2edge_hook hook
, void *data
)
345 struct cgraph_2edge_hook_list
*entry
;
346 struct cgraph_2edge_hook_list
**ptr
= &first_cgraph_edge_duplicated_hook
;
348 entry
= (struct cgraph_2edge_hook_list
*) xmalloc (sizeof (*entry
));
358 /* Remove ENTRY from the list of hooks called on duplicating edges. */
360 cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list
*entry
)
362 struct cgraph_2edge_hook_list
**ptr
= &first_cgraph_edge_duplicated_hook
;
364 while (*ptr
!= entry
)
370 /* Call all edge duplication hooks. */
372 cgraph_call_edge_duplication_hooks (struct cgraph_edge
*cs1
,
373 struct cgraph_edge
*cs2
)
375 struct cgraph_2edge_hook_list
*entry
= first_cgraph_edge_duplicated_hook
;
378 entry
->hook (cs1
, cs2
, entry
->data
);
383 /* Register HOOK to be called with DATA on each duplicated node. */
384 struct cgraph_2node_hook_list
*
385 cgraph_add_node_duplication_hook (cgraph_2node_hook hook
, void *data
)
387 struct cgraph_2node_hook_list
*entry
;
388 struct cgraph_2node_hook_list
**ptr
= &first_cgraph_node_duplicated_hook
;
390 entry
= (struct cgraph_2node_hook_list
*) xmalloc (sizeof (*entry
));
400 /* Remove ENTRY from the list of hooks called on duplicating nodes. */
402 cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list
*entry
)
404 struct cgraph_2node_hook_list
**ptr
= &first_cgraph_node_duplicated_hook
;
406 while (*ptr
!= entry
)
412 /* Call all node duplication hooks. */
414 cgraph_call_node_duplication_hooks (struct cgraph_node
*node1
,
415 struct cgraph_node
*node2
)
417 struct cgraph_2node_hook_list
*entry
= first_cgraph_node_duplicated_hook
;
420 entry
->hook (node1
, node2
, entry
->data
);
425 /* Returns a hash code for P. */
428 hash_node (const void *p
)
430 const struct cgraph_node
*n
= (const struct cgraph_node
*) p
;
431 return (hashval_t
) DECL_UID (n
->decl
);
435 /* Returns nonzero if P1 and P2 are equal. */
438 eq_node (const void *p1
, const void *p2
)
440 const struct cgraph_node
*n1
= (const struct cgraph_node
*) p1
;
441 const struct cgraph_node
*n2
= (const struct cgraph_node
*) p2
;
442 return DECL_UID (n1
->decl
) == DECL_UID (n2
->decl
);
445 /* Allocate new callgraph node. */
447 static inline struct cgraph_node
*
448 cgraph_allocate_node (void)
450 struct cgraph_node
*node
;
455 free_nodes
= NEXT_FREE_NODE (node
);
459 node
= ggc_alloc_cleared_cgraph_node ();
460 node
->uid
= cgraph_max_uid
++;
466 /* Allocate new callgraph node and insert it into basic data structures. */
468 static struct cgraph_node
*
469 cgraph_create_node (void)
471 struct cgraph_node
*node
= cgraph_allocate_node ();
473 node
->next
= cgraph_nodes
;
475 node
->order
= cgraph_order
++;
477 cgraph_nodes
->previous
= node
;
478 node
->previous
= NULL
;
479 node
->global
.estimated_growth
= INT_MIN
;
480 node
->frequency
= NODE_FREQUENCY_NORMAL
;
481 node
->count_materialization_scale
= REG_BR_PROB_BASE
;
482 ipa_empty_ref_list (&node
->ref_list
);
488 /* Return cgraph node assigned to DECL. Create new one when needed. */
491 cgraph_node (tree decl
)
493 struct cgraph_node key
, *node
, **slot
;
495 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
498 cgraph_hash
= htab_create_ggc (10, hash_node
, eq_node
, NULL
);
502 slot
= (struct cgraph_node
**) htab_find_slot (cgraph_hash
, &key
, INSERT
);
507 if (node
->same_body_alias
)
508 node
= node
->same_body
;
512 node
= cgraph_create_node ();
515 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
517 node
->origin
= cgraph_node (DECL_CONTEXT (decl
));
518 node
->next_nested
= node
->origin
->nested
;
519 node
->origin
->nested
= node
;
521 if (assembler_name_hash
)
524 tree name
= DECL_ASSEMBLER_NAME (decl
);
526 aslot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
527 decl_assembler_name_hash (name
),
529 /* We can have multiple declarations with same assembler name. For C++
530 it is __builtin_strlen and strlen, for instance. Do we need to
531 record them all? Original implementation marked just first one
532 so lets hope for the best. */
539 /* Mark ALIAS as an alias to DECL. */
541 static struct cgraph_node
*
542 cgraph_same_body_alias_1 (tree alias
, tree decl
)
544 struct cgraph_node key
, *alias_node
, *decl_node
, **slot
;
546 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
547 gcc_assert (TREE_CODE (alias
) == FUNCTION_DECL
);
548 decl_node
= cgraph_node (decl
);
552 slot
= (struct cgraph_node
**) htab_find_slot (cgraph_hash
, &key
, INSERT
);
554 /* If the cgraph_node has been already created, fail. */
558 alias_node
= cgraph_allocate_node ();
559 alias_node
->decl
= alias
;
560 alias_node
->same_body_alias
= 1;
561 alias_node
->same_body
= decl_node
;
562 alias_node
->previous
= NULL
;
563 if (decl_node
->same_body
)
564 decl_node
->same_body
->previous
= alias_node
;
565 alias_node
->next
= decl_node
->same_body
;
566 alias_node
->thunk
.alias
= decl
;
567 decl_node
->same_body
= alias_node
;
572 /* Attempt to mark ALIAS as an alias to DECL. Return alias node if successful
574 Same body aliases are output whenever the body of DECL is output,
575 and cgraph_node (ALIAS) transparently returns cgraph_node (DECL). */
578 cgraph_same_body_alias (tree alias
, tree decl
)
580 #ifndef ASM_OUTPUT_DEF
581 /* If aliases aren't supported by the assembler, fail. */
585 /*gcc_assert (!assembler_name_hash);*/
587 return cgraph_same_body_alias_1 (alias
, decl
);
590 /* Add thunk alias into callgraph. The alias declaration is ALIAS and it
591 aliases DECL with an adjustments made into the first parameter.
592 See comments in thunk_adjust for detail on the parameters. */
595 cgraph_add_thunk (tree alias
, tree decl
, bool this_adjusting
,
596 HOST_WIDE_INT fixed_offset
, HOST_WIDE_INT virtual_value
,
600 struct cgraph_node
*node
= cgraph_get_node (alias
);
604 gcc_assert (node
->local
.finalized
);
605 gcc_assert (!node
->same_body
);
606 cgraph_remove_node (node
);
609 node
= cgraph_same_body_alias_1 (alias
, decl
);
611 gcc_checking_assert (!virtual_offset
612 || tree_int_cst_equal (virtual_offset
,
613 size_int (virtual_value
)));
614 node
->thunk
.fixed_offset
= fixed_offset
;
615 node
->thunk
.this_adjusting
= this_adjusting
;
616 node
->thunk
.virtual_value
= virtual_value
;
617 node
->thunk
.virtual_offset_p
= virtual_offset
!= NULL
;
618 node
->thunk
.alias
= real_alias
;
619 node
->thunk
.thunk_p
= true;
623 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
627 cgraph_get_node_or_alias (const_tree decl
)
629 struct cgraph_node key
, *node
= NULL
, **slot
;
631 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
636 key
.decl
= CONST_CAST2 (tree
, const_tree
, decl
);
638 slot
= (struct cgraph_node
**) htab_find_slot (cgraph_hash
, &key
,
646 /* Returns the cgraph node assigned to DECL or NULL if no cgraph node
650 cgraph_get_node (const_tree decl
)
652 struct cgraph_node key
, *node
= NULL
, **slot
;
654 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
659 key
.decl
= CONST_CAST2 (tree
, const_tree
, decl
);
661 slot
= (struct cgraph_node
**) htab_find_slot (cgraph_hash
, &key
,
667 if (node
->same_body_alias
)
668 node
= node
->same_body
;
673 /* Insert already constructed node into hashtable. */
676 cgraph_insert_node_to_hashtable (struct cgraph_node
*node
)
678 struct cgraph_node
**slot
;
680 slot
= (struct cgraph_node
**) htab_find_slot (cgraph_hash
, node
, INSERT
);
686 /* Returns a hash code for P. */
689 hash_node_by_assembler_name (const void *p
)
691 const struct cgraph_node
*n
= (const struct cgraph_node
*) p
;
692 return (hashval_t
) decl_assembler_name_hash (DECL_ASSEMBLER_NAME (n
->decl
));
695 /* Returns nonzero if P1 and P2 are equal. */
698 eq_assembler_name (const void *p1
, const void *p2
)
700 const struct cgraph_node
*n1
= (const struct cgraph_node
*) p1
;
701 const_tree name
= (const_tree
)p2
;
702 return (decl_assembler_name_equal (n1
->decl
, name
));
705 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
706 Return NULL if there's no such node. */
709 cgraph_node_for_asm (tree asmname
)
711 struct cgraph_node
*node
;
714 if (!assembler_name_hash
)
716 assembler_name_hash
=
717 htab_create_ggc (10, hash_node_by_assembler_name
, eq_assembler_name
,
719 for (node
= cgraph_nodes
; node
; node
= node
->next
)
720 if (!node
->global
.inlined_to
)
722 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
723 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
724 decl_assembler_name_hash (name
),
726 /* We can have multiple declarations with same assembler name. For C++
727 it is __builtin_strlen and strlen, for instance. Do we need to
728 record them all? Original implementation marked just first one
729 so lets hope for the best. */
734 struct cgraph_node
*alias
;
736 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
739 name
= DECL_ASSEMBLER_NAME (alias
->decl
);
740 hash
= decl_assembler_name_hash (name
);
741 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
750 slot
= htab_find_slot_with_hash (assembler_name_hash
, asmname
,
751 decl_assembler_name_hash (asmname
),
756 node
= (struct cgraph_node
*) *slot
;
757 if (node
->same_body_alias
)
758 node
= node
->same_body
;
764 /* Returns a hash value for X (which really is a die_struct). */
767 edge_hash (const void *x
)
769 return htab_hash_pointer (((const struct cgraph_edge
*) x
)->call_stmt
);
772 /* Return nonzero if decl_id of die_struct X is the same as UID of decl *Y. */
775 edge_eq (const void *x
, const void *y
)
777 return ((const struct cgraph_edge
*) x
)->call_stmt
== y
;
780 /* Add call graph edge E to call site hash of its caller. */
783 cgraph_add_edge_to_call_site_hash (struct cgraph_edge
*e
)
786 slot
= htab_find_slot_with_hash (e
->caller
->call_site_hash
,
788 htab_hash_pointer (e
->call_stmt
),
794 /* Return the callgraph edge representing the GIMPLE_CALL statement
798 cgraph_edge (struct cgraph_node
*node
, gimple call_stmt
)
800 struct cgraph_edge
*e
, *e2
;
803 if (node
->call_site_hash
)
804 return (struct cgraph_edge
*)
805 htab_find_with_hash (node
->call_site_hash
, call_stmt
,
806 htab_hash_pointer (call_stmt
));
808 /* This loop may turn out to be performance problem. In such case adding
809 hashtables into call nodes with very many edges is probably best
810 solution. It is not good idea to add pointer into CALL_EXPR itself
811 because we want to make possible having multiple cgraph nodes representing
812 different clones of the same body before the body is actually cloned. */
813 for (e
= node
->callees
; e
; e
= e
->next_callee
)
815 if (e
->call_stmt
== call_stmt
)
821 for (e
= node
->indirect_calls
; e
; e
= e
->next_callee
)
823 if (e
->call_stmt
== call_stmt
)
830 node
->call_site_hash
= htab_create_ggc (120, edge_hash
, edge_eq
, NULL
);
831 for (e2
= node
->callees
; e2
; e2
= e2
->next_callee
)
832 cgraph_add_edge_to_call_site_hash (e2
);
833 for (e2
= node
->indirect_calls
; e2
; e2
= e2
->next_callee
)
834 cgraph_add_edge_to_call_site_hash (e2
);
841 /* Change field call_stmt of edge E to NEW_STMT. */
844 cgraph_set_call_stmt (struct cgraph_edge
*e
, gimple new_stmt
)
848 if (e
->caller
->call_site_hash
)
850 htab_remove_elt_with_hash (e
->caller
->call_site_hash
,
852 htab_hash_pointer (e
->call_stmt
));
855 e
->call_stmt
= new_stmt
;
856 if (e
->indirect_unknown_callee
857 && (decl
= gimple_call_fndecl (new_stmt
)))
859 /* Constant propagation (and possibly also inlining?) can turn an
860 indirect call into a direct one. */
861 struct cgraph_node
*new_callee
= cgraph_node (decl
);
863 cgraph_make_edge_direct (e
, new_callee
, 0);
866 push_cfun (DECL_STRUCT_FUNCTION (e
->caller
->decl
));
867 e
->can_throw_external
= stmt_can_throw_external (new_stmt
);
869 if (e
->caller
->call_site_hash
)
870 cgraph_add_edge_to_call_site_hash (e
);
873 /* Like cgraph_set_call_stmt but walk the clone tree and update all
874 clones sharing the same function body. */
877 cgraph_set_call_stmt_including_clones (struct cgraph_node
*orig
,
878 gimple old_stmt
, gimple new_stmt
)
880 struct cgraph_node
*node
;
881 struct cgraph_edge
*edge
= cgraph_edge (orig
, old_stmt
);
884 cgraph_set_call_stmt (edge
, new_stmt
);
890 struct cgraph_edge
*edge
= cgraph_edge (node
, old_stmt
);
892 cgraph_set_call_stmt (edge
, new_stmt
);
895 else if (node
->next_sibling_clone
)
896 node
= node
->next_sibling_clone
;
899 while (node
!= orig
&& !node
->next_sibling_clone
)
900 node
= node
->clone_of
;
902 node
= node
->next_sibling_clone
;
907 /* Like cgraph_create_edge walk the clone tree and update all clones sharing
908 same function body. If clones already have edge for OLD_STMT; only
909 update the edge same way as cgraph_set_call_stmt_including_clones does.
911 TODO: COUNT and LOOP_DEPTH should be properly distributed based on relative
912 frequencies of the clones. */
915 cgraph_create_edge_including_clones (struct cgraph_node
*orig
,
916 struct cgraph_node
*callee
,
918 gimple stmt
, gcov_type count
,
919 int freq
, int loop_depth
,
920 cgraph_inline_failed_t reason
)
922 struct cgraph_node
*node
;
923 struct cgraph_edge
*edge
;
925 if (!cgraph_edge (orig
, stmt
))
927 edge
= cgraph_create_edge (orig
, callee
, stmt
, count
, freq
, loop_depth
);
928 edge
->inline_failed
= reason
;
935 struct cgraph_edge
*edge
= cgraph_edge (node
, old_stmt
);
937 /* It is possible that clones already contain the edge while
938 master didn't. Either we promoted indirect call into direct
939 call in the clone or we are processing clones of unreachable
940 master where edges has been removed. */
942 cgraph_set_call_stmt (edge
, stmt
);
943 else if (!cgraph_edge (node
, stmt
))
945 edge
= cgraph_create_edge (node
, callee
, stmt
, count
,
947 edge
->inline_failed
= reason
;
952 else if (node
->next_sibling_clone
)
953 node
= node
->next_sibling_clone
;
956 while (node
!= orig
&& !node
->next_sibling_clone
)
957 node
= node
->clone_of
;
959 node
= node
->next_sibling_clone
;
964 /* Give initial reasons why inlining would fail on EDGE. This gets either
965 nullified or usually overwritten by more precise reasons later. */
968 initialize_inline_failed (struct cgraph_edge
*e
)
970 struct cgraph_node
*callee
= e
->callee
;
972 if (e
->indirect_unknown_callee
)
973 e
->inline_failed
= CIF_INDIRECT_UNKNOWN_CALL
;
974 else if (!callee
->analyzed
)
975 e
->inline_failed
= CIF_BODY_NOT_AVAILABLE
;
976 else if (callee
->local
.redefined_extern_inline
)
977 e
->inline_failed
= CIF_REDEFINED_EXTERN_INLINE
;
978 else if (!callee
->local
.inlinable
)
979 e
->inline_failed
= CIF_FUNCTION_NOT_INLINABLE
;
980 else if (e
->call_stmt
&& gimple_call_cannot_inline_p (e
->call_stmt
))
981 e
->inline_failed
= CIF_MISMATCHED_ARGUMENTS
;
983 e
->inline_failed
= CIF_FUNCTION_NOT_CONSIDERED
;
986 /* Allocate a cgraph_edge structure and fill it with data according to the
987 parameters of which only CALLEE can be NULL (when creating an indirect call
990 static struct cgraph_edge
*
991 cgraph_create_edge_1 (struct cgraph_node
*caller
, struct cgraph_node
*callee
,
992 gimple call_stmt
, gcov_type count
, int freq
, int nest
)
994 struct cgraph_edge
*edge
;
996 /* LTO does not actually have access to the call_stmt since these
997 have not been loaded yet. */
1000 /* This is a rather expensive check possibly triggering
1001 construction of call stmt hashtable. */
1002 gcc_checking_assert (!cgraph_edge (caller
, call_stmt
));
1004 gcc_assert (is_gimple_call (call_stmt
));
1010 free_edges
= NEXT_FREE_EDGE (edge
);
1014 edge
= ggc_alloc_cgraph_edge ();
1015 edge
->uid
= cgraph_edge_max_uid
++;
1019 edge
->caller
= caller
;
1020 edge
->callee
= callee
;
1021 edge
->prev_caller
= NULL
;
1022 edge
->next_caller
= NULL
;
1023 edge
->prev_callee
= NULL
;
1024 edge
->next_callee
= NULL
;
1026 edge
->count
= count
;
1027 gcc_assert (count
>= 0);
1028 edge
->frequency
= freq
;
1029 gcc_assert (freq
>= 0);
1030 gcc_assert (freq
<= CGRAPH_FREQ_MAX
);
1031 edge
->loop_nest
= nest
;
1033 edge
->call_stmt
= call_stmt
;
1034 push_cfun (DECL_STRUCT_FUNCTION (caller
->decl
));
1035 edge
->can_throw_external
1036 = call_stmt
? stmt_can_throw_external (call_stmt
) : false;
1038 edge
->call_stmt_cannot_inline_p
=
1039 (call_stmt
? gimple_call_cannot_inline_p (call_stmt
) : false);
1040 if (call_stmt
&& caller
->call_site_hash
)
1041 cgraph_add_edge_to_call_site_hash (edge
);
1043 edge
->indirect_info
= NULL
;
1044 edge
->indirect_inlining_edge
= 0;
1049 /* Create edge from CALLER to CALLEE in the cgraph. */
1051 struct cgraph_edge
*
1052 cgraph_create_edge (struct cgraph_node
*caller
, struct cgraph_node
*callee
,
1053 gimple call_stmt
, gcov_type count
, int freq
, int nest
)
1055 struct cgraph_edge
*edge
= cgraph_create_edge_1 (caller
, callee
, call_stmt
,
1058 edge
->indirect_unknown_callee
= 0;
1059 initialize_inline_failed (edge
);
1061 edge
->next_caller
= callee
->callers
;
1062 if (callee
->callers
)
1063 callee
->callers
->prev_caller
= edge
;
1064 edge
->next_callee
= caller
->callees
;
1065 if (caller
->callees
)
1066 caller
->callees
->prev_callee
= edge
;
1067 caller
->callees
= edge
;
1068 callee
->callers
= edge
;
1073 /* Allocate cgraph_indirect_call_info and set its fields to default values. */
1075 struct cgraph_indirect_call_info
*
1076 cgraph_allocate_init_indirect_info (void)
1078 struct cgraph_indirect_call_info
*ii
;
1080 ii
= ggc_alloc_cleared_cgraph_indirect_call_info ();
1081 ii
->param_index
= -1;
1085 /* Create an indirect edge with a yet-undetermined callee where the call
1086 statement destination is a formal parameter of the caller with index
1089 struct cgraph_edge
*
1090 cgraph_create_indirect_edge (struct cgraph_node
*caller
, gimple call_stmt
,
1092 gcov_type count
, int freq
, int nest
)
1094 struct cgraph_edge
*edge
= cgraph_create_edge_1 (caller
, NULL
, call_stmt
,
1097 edge
->indirect_unknown_callee
= 1;
1098 initialize_inline_failed (edge
);
1100 edge
->indirect_info
= cgraph_allocate_init_indirect_info ();
1101 edge
->indirect_info
->ecf_flags
= ecf_flags
;
1103 edge
->next_callee
= caller
->indirect_calls
;
1104 if (caller
->indirect_calls
)
1105 caller
->indirect_calls
->prev_callee
= edge
;
1106 caller
->indirect_calls
= edge
;
1111 /* Remove the edge E from the list of the callers of the callee. */
1114 cgraph_edge_remove_callee (struct cgraph_edge
*e
)
1116 gcc_assert (!e
->indirect_unknown_callee
);
1118 e
->prev_caller
->next_caller
= e
->next_caller
;
1120 e
->next_caller
->prev_caller
= e
->prev_caller
;
1121 if (!e
->prev_caller
)
1122 e
->callee
->callers
= e
->next_caller
;
1125 /* Remove the edge E from the list of the callees of the caller. */
1128 cgraph_edge_remove_caller (struct cgraph_edge
*e
)
1131 e
->prev_callee
->next_callee
= e
->next_callee
;
1133 e
->next_callee
->prev_callee
= e
->prev_callee
;
1134 if (!e
->prev_callee
)
1136 if (e
->indirect_unknown_callee
)
1137 e
->caller
->indirect_calls
= e
->next_callee
;
1139 e
->caller
->callees
= e
->next_callee
;
1141 if (e
->caller
->call_site_hash
)
1142 htab_remove_elt_with_hash (e
->caller
->call_site_hash
,
1144 htab_hash_pointer (e
->call_stmt
));
1147 /* Put the edge onto the free list. */
1150 cgraph_free_edge (struct cgraph_edge
*e
)
1154 /* Clear out the edge so we do not dangle pointers. */
1155 memset (e
, 0, sizeof (*e
));
1157 NEXT_FREE_EDGE (e
) = free_edges
;
1161 /* Remove the edge E in the cgraph. */
1164 cgraph_remove_edge (struct cgraph_edge
*e
)
1166 /* Call all edge removal hooks. */
1167 cgraph_call_edge_removal_hooks (e
);
1169 if (!e
->indirect_unknown_callee
)
1170 /* Remove from callers list of the callee. */
1171 cgraph_edge_remove_callee (e
);
1173 /* Remove from callees list of the callers. */
1174 cgraph_edge_remove_caller (e
);
1176 /* Put the edge onto the free list. */
1177 cgraph_free_edge (e
);
1180 /* Set callee of call graph edge E and add it to the corresponding set of
1184 cgraph_set_edge_callee (struct cgraph_edge
*e
, struct cgraph_node
*n
)
1186 e
->prev_caller
= NULL
;
1188 n
->callers
->prev_caller
= e
;
1189 e
->next_caller
= n
->callers
;
1194 /* Redirect callee of E to N. The function does not update underlying
1198 cgraph_redirect_edge_callee (struct cgraph_edge
*e
, struct cgraph_node
*n
)
1200 /* Remove from callers list of the current callee. */
1201 cgraph_edge_remove_callee (e
);
1203 /* Insert to callers list of the new callee. */
1204 cgraph_set_edge_callee (e
, n
);
1207 /* Make an indirect EDGE with an unknown callee an ordinary edge leading to
1208 CALLEE. DELTA is an integer constant that is to be added to the this
1209 pointer (first parameter) to compensate for skipping a thunk adjustment. */
1212 cgraph_make_edge_direct (struct cgraph_edge
*edge
, struct cgraph_node
*callee
,
1213 HOST_WIDE_INT delta
)
1215 edge
->indirect_unknown_callee
= 0;
1216 edge
->indirect_info
->thunk_delta
= delta
;
1218 /* Get the edge out of the indirect edge list. */
1219 if (edge
->prev_callee
)
1220 edge
->prev_callee
->next_callee
= edge
->next_callee
;
1221 if (edge
->next_callee
)
1222 edge
->next_callee
->prev_callee
= edge
->prev_callee
;
1223 if (!edge
->prev_callee
)
1224 edge
->caller
->indirect_calls
= edge
->next_callee
;
1226 /* Put it into the normal callee list */
1227 edge
->prev_callee
= NULL
;
1228 edge
->next_callee
= edge
->caller
->callees
;
1229 if (edge
->caller
->callees
)
1230 edge
->caller
->callees
->prev_callee
= edge
;
1231 edge
->caller
->callees
= edge
;
1233 /* Insert to callers list of the new callee. */
1234 cgraph_set_edge_callee (edge
, callee
);
1236 /* We need to re-determine the inlining status of the edge. */
1237 initialize_inline_failed (edge
);
1241 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1242 OLD_STMT changed into NEW_STMT. OLD_CALL is gimple_call_fndecl
1243 of OLD_STMT if it was previously call statement. */
1246 cgraph_update_edges_for_call_stmt_node (struct cgraph_node
*node
,
1247 gimple old_stmt
, tree old_call
, gimple new_stmt
)
1249 tree new_call
= (is_gimple_call (new_stmt
)) ? gimple_call_fndecl (new_stmt
) : 0;
1251 /* We are seeing indirect calls, then there is nothing to update. */
1252 if (!new_call
&& !old_call
)
1254 /* See if we turned indirect call into direct call or folded call to one builtin
1255 into different builtin. */
1256 if (old_call
!= new_call
)
1258 struct cgraph_edge
*e
= cgraph_edge (node
, old_stmt
);
1259 struct cgraph_edge
*ne
= NULL
;
1266 /* See if the edge is already there and has the correct callee. It
1267 might be so because of indirect inlining has already updated
1268 it. We also might've cloned and redirected the edge. */
1269 if (new_call
&& e
->callee
)
1271 struct cgraph_node
*callee
= e
->callee
;
1274 if (callee
->decl
== new_call
1275 || callee
->former_clone_of
== new_call
)
1277 callee
= callee
->clone_of
;
1281 /* Otherwise remove edge and create new one; we can't simply redirect
1282 since function has changed, so inline plan and other information
1283 attached to edge is invalid. */
1285 frequency
= e
->frequency
;
1286 loop_nest
= e
->loop_nest
;
1287 cgraph_remove_edge (e
);
1291 /* We are seeing new direct call; compute profile info based on BB. */
1292 basic_block bb
= gimple_bb (new_stmt
);
1294 frequency
= compute_call_stmt_bb_frequency (current_function_decl
,
1296 loop_nest
= bb
->loop_depth
;
1301 ne
= cgraph_create_edge (node
, cgraph_node (new_call
),
1302 new_stmt
, count
, frequency
,
1304 gcc_assert (ne
->inline_failed
);
1307 /* We only updated the call stmt; update pointer in cgraph edge.. */
1308 else if (old_stmt
!= new_stmt
)
1309 cgraph_set_call_stmt (cgraph_edge (node
, old_stmt
), new_stmt
);
1312 /* Update or remove the corresponding cgraph edge if a GIMPLE_CALL
1313 OLD_STMT changed into NEW_STMT. OLD_DECL is gimple_call_fndecl
1314 of OLD_STMT before it was updated (updating can happen inplace). */
1317 cgraph_update_edges_for_call_stmt (gimple old_stmt
, tree old_decl
, gimple new_stmt
)
1319 struct cgraph_node
*orig
= cgraph_node (cfun
->decl
);
1320 struct cgraph_node
*node
;
1322 cgraph_update_edges_for_call_stmt_node (orig
, old_stmt
, old_decl
, new_stmt
);
1324 for (node
= orig
->clones
; node
!= orig
;)
1326 cgraph_update_edges_for_call_stmt_node (node
, old_stmt
, old_decl
, new_stmt
);
1328 node
= node
->clones
;
1329 else if (node
->next_sibling_clone
)
1330 node
= node
->next_sibling_clone
;
1333 while (node
!= orig
&& !node
->next_sibling_clone
)
1334 node
= node
->clone_of
;
1336 node
= node
->next_sibling_clone
;
1342 /* Remove all callees from the node. */
1345 cgraph_node_remove_callees (struct cgraph_node
*node
)
1347 struct cgraph_edge
*e
, *f
;
1349 /* It is sufficient to remove the edges from the lists of callers of
1350 the callees. The callee list of the node can be zapped with one
1352 for (e
= node
->callees
; e
; e
= f
)
1355 cgraph_call_edge_removal_hooks (e
);
1356 if (!e
->indirect_unknown_callee
)
1357 cgraph_edge_remove_callee (e
);
1358 cgraph_free_edge (e
);
1360 for (e
= node
->indirect_calls
; e
; e
= f
)
1363 cgraph_call_edge_removal_hooks (e
);
1364 if (!e
->indirect_unknown_callee
)
1365 cgraph_edge_remove_callee (e
);
1366 cgraph_free_edge (e
);
1368 node
->indirect_calls
= NULL
;
1369 node
->callees
= NULL
;
1370 if (node
->call_site_hash
)
1372 htab_delete (node
->call_site_hash
);
1373 node
->call_site_hash
= NULL
;
1377 /* Remove all callers from the node. */
1380 cgraph_node_remove_callers (struct cgraph_node
*node
)
1382 struct cgraph_edge
*e
, *f
;
1384 /* It is sufficient to remove the edges from the lists of callees of
1385 the callers. The caller list of the node can be zapped with one
1387 for (e
= node
->callers
; e
; e
= f
)
1390 cgraph_call_edge_removal_hooks (e
);
1391 cgraph_edge_remove_caller (e
);
1392 cgraph_free_edge (e
);
1394 node
->callers
= NULL
;
1397 /* Release memory used to represent body of function NODE. */
1400 cgraph_release_function_body (struct cgraph_node
*node
)
1402 if (DECL_STRUCT_FUNCTION (node
->decl
))
1404 tree old_decl
= current_function_decl
;
1405 push_cfun (DECL_STRUCT_FUNCTION (node
->decl
));
1406 if (cfun
->gimple_df
)
1408 current_function_decl
= node
->decl
;
1410 delete_tree_cfg_annotations ();
1412 current_function_decl
= old_decl
;
1416 gcc_assert (dom_computed
[0] == DOM_NONE
);
1417 gcc_assert (dom_computed
[1] == DOM_NONE
);
1420 if (cfun
->value_histograms
)
1422 gcc_assert (!current_loops
);
1424 gimple_set_body (node
->decl
, NULL
);
1425 VEC_free (ipa_opt_pass
, heap
,
1426 node
->ipa_transforms_to_apply
);
1427 /* Struct function hangs a lot of data that would leak if we didn't
1428 removed all pointers to it. */
1429 ggc_free (DECL_STRUCT_FUNCTION (node
->decl
));
1430 DECL_STRUCT_FUNCTION (node
->decl
) = NULL
;
1432 DECL_SAVED_TREE (node
->decl
) = NULL
;
1433 /* If the node is abstract and needed, then do not clear DECL_INITIAL
1434 of its associated function function declaration because it's
1435 needed to emit debug info later. */
1436 if (!node
->abstract_and_needed
)
1437 DECL_INITIAL (node
->decl
) = error_mark_node
;
1440 /* Remove same body alias node. */
1443 cgraph_remove_same_body_alias (struct cgraph_node
*node
)
1446 int uid
= node
->uid
;
1448 gcc_assert (node
->same_body_alias
);
1450 node
->previous
->next
= node
->next
;
1452 node
->same_body
->same_body
= node
->next
;
1454 node
->next
->previous
= node
->previous
;
1456 node
->previous
= NULL
;
1457 slot
= htab_find_slot (cgraph_hash
, node
, NO_INSERT
);
1459 htab_clear_slot (cgraph_hash
, slot
);
1460 if (assembler_name_hash
)
1462 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
1463 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
1464 decl_assembler_name_hash (name
),
1466 if (slot
&& *slot
== node
)
1467 htab_clear_slot (assembler_name_hash
, slot
);
1470 /* Clear out the node to NULL all pointers and add the node to the free
1472 memset (node
, 0, sizeof(*node
));
1474 NEXT_FREE_NODE (node
) = free_nodes
;
1478 /* Remove the node from cgraph. */
1481 cgraph_remove_node (struct cgraph_node
*node
)
1484 bool kill_body
= false;
1485 struct cgraph_node
*n
;
1486 int uid
= node
->uid
;
1488 cgraph_call_node_removal_hooks (node
);
1489 cgraph_node_remove_callers (node
);
1490 cgraph_node_remove_callees (node
);
1491 ipa_remove_all_references (&node
->ref_list
);
1492 ipa_remove_all_refering (&node
->ref_list
);
1493 VEC_free (ipa_opt_pass
, heap
,
1494 node
->ipa_transforms_to_apply
);
1496 /* Incremental inlining access removed nodes stored in the postorder list.
1498 node
->needed
= node
->reachable
= false;
1499 for (n
= node
->nested
; n
; n
= n
->next_nested
)
1501 node
->nested
= NULL
;
1504 struct cgraph_node
**node2
= &node
->origin
->nested
;
1506 while (*node2
!= node
)
1507 node2
= &(*node2
)->next_nested
;
1508 *node2
= node
->next_nested
;
1511 node
->previous
->next
= node
->next
;
1513 cgraph_nodes
= node
->next
;
1515 node
->next
->previous
= node
->previous
;
1517 node
->previous
= NULL
;
1518 slot
= htab_find_slot (cgraph_hash
, node
, NO_INSERT
);
1521 struct cgraph_node
*next_inline_clone
;
1523 for (next_inline_clone
= node
->clones
;
1524 next_inline_clone
&& next_inline_clone
->decl
!= node
->decl
;
1525 next_inline_clone
= next_inline_clone
->next_sibling_clone
)
1528 /* If there is inline clone of the node being removed, we need
1529 to put it into the position of removed node and reorganize all
1530 other clones to be based on it. */
1531 if (next_inline_clone
)
1533 struct cgraph_node
*n
;
1534 struct cgraph_node
*new_clones
;
1536 *slot
= next_inline_clone
;
1538 /* Unlink inline clone from the list of clones of removed node. */
1539 if (next_inline_clone
->next_sibling_clone
)
1540 next_inline_clone
->next_sibling_clone
->prev_sibling_clone
1541 = next_inline_clone
->prev_sibling_clone
;
1542 if (next_inline_clone
->prev_sibling_clone
)
1544 gcc_assert (node
->clones
!= next_inline_clone
);
1545 next_inline_clone
->prev_sibling_clone
->next_sibling_clone
1546 = next_inline_clone
->next_sibling_clone
;
1550 gcc_assert (node
->clones
== next_inline_clone
);
1551 node
->clones
= next_inline_clone
->next_sibling_clone
;
1554 new_clones
= node
->clones
;
1555 node
->clones
= NULL
;
1557 /* Copy clone info. */
1558 next_inline_clone
->clone
= node
->clone
;
1560 /* Now place it into clone tree at same level at NODE. */
1561 next_inline_clone
->clone_of
= node
->clone_of
;
1562 next_inline_clone
->prev_sibling_clone
= NULL
;
1563 next_inline_clone
->next_sibling_clone
= NULL
;
1566 if (node
->clone_of
->clones
)
1567 node
->clone_of
->clones
->prev_sibling_clone
= next_inline_clone
;
1568 next_inline_clone
->next_sibling_clone
= node
->clone_of
->clones
;
1569 node
->clone_of
->clones
= next_inline_clone
;
1572 /* Merge the clone list. */
1575 if (!next_inline_clone
->clones
)
1576 next_inline_clone
->clones
= new_clones
;
1579 n
= next_inline_clone
->clones
;
1580 while (n
->next_sibling_clone
)
1581 n
= n
->next_sibling_clone
;
1582 n
->next_sibling_clone
= new_clones
;
1583 new_clones
->prev_sibling_clone
= n
;
1587 /* Update clone_of pointers. */
1591 n
->clone_of
= next_inline_clone
;
1592 n
= n
->next_sibling_clone
;
1597 htab_clear_slot (cgraph_hash
, slot
);
1602 if (node
->prev_sibling_clone
)
1603 node
->prev_sibling_clone
->next_sibling_clone
= node
->next_sibling_clone
;
1604 else if (node
->clone_of
)
1605 node
->clone_of
->clones
= node
->next_sibling_clone
;
1606 if (node
->next_sibling_clone
)
1607 node
->next_sibling_clone
->prev_sibling_clone
= node
->prev_sibling_clone
;
1610 struct cgraph_node
*n
, *next
;
1614 for (n
= node
->clones
; n
->next_sibling_clone
; n
= n
->next_sibling_clone
)
1615 n
->clone_of
= node
->clone_of
;
1616 n
->clone_of
= node
->clone_of
;
1617 n
->next_sibling_clone
= node
->clone_of
->clones
;
1618 if (node
->clone_of
->clones
)
1619 node
->clone_of
->clones
->prev_sibling_clone
= n
;
1620 node
->clone_of
->clones
= node
->clones
;
1624 /* We are removing node with clones. this makes clones inconsistent,
1625 but assume they will be removed subsequently and just keep clone
1626 tree intact. This can happen in unreachable function removal since
1627 we remove unreachable functions in random order, not by bottom-up
1628 walk of clone trees. */
1629 for (n
= node
->clones
; n
; n
= next
)
1631 next
= n
->next_sibling_clone
;
1632 n
->next_sibling_clone
= NULL
;
1633 n
->prev_sibling_clone
= NULL
;
1639 while (node
->same_body
)
1640 cgraph_remove_same_body_alias (node
->same_body
);
1642 if (node
->same_comdat_group
)
1644 struct cgraph_node
*prev
;
1645 for (prev
= node
->same_comdat_group
;
1646 prev
->same_comdat_group
!= node
;
1647 prev
= prev
->same_comdat_group
)
1649 if (node
->same_comdat_group
== prev
)
1650 prev
->same_comdat_group
= NULL
;
1652 prev
->same_comdat_group
= node
->same_comdat_group
;
1653 node
->same_comdat_group
= NULL
;
1656 /* While all the clones are removed after being proceeded, the function
1657 itself is kept in the cgraph even after it is compiled. Check whether
1658 we are done with this body and reclaim it proactively if this is the case.
1660 if (!kill_body
&& *slot
)
1662 struct cgraph_node
*n
= (struct cgraph_node
*) *slot
;
1663 if (!n
->clones
&& !n
->clone_of
&& !n
->global
.inlined_to
1664 && (cgraph_global_info_ready
1665 && (TREE_ASM_WRITTEN (n
->decl
) || DECL_EXTERNAL (n
->decl
)
1666 || n
->in_other_partition
)))
1669 if (assembler_name_hash
)
1671 tree name
= DECL_ASSEMBLER_NAME (node
->decl
);
1672 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
1673 decl_assembler_name_hash (name
),
1675 /* Inline clones are not hashed. */
1676 if (slot
&& *slot
== node
)
1677 htab_clear_slot (assembler_name_hash
, slot
);
1681 cgraph_release_function_body (node
);
1683 if (node
->call_site_hash
)
1685 htab_delete (node
->call_site_hash
);
1686 node
->call_site_hash
= NULL
;
1690 /* Clear out the node to NULL all pointers and add the node to the free
1692 memset (node
, 0, sizeof(*node
));
1694 NEXT_FREE_NODE (node
) = free_nodes
;
1698 /* Remove the node from cgraph. */
1701 cgraph_remove_node_and_inline_clones (struct cgraph_node
*node
)
1703 struct cgraph_edge
*e
, *next
;
1704 for (e
= node
->callees
; e
; e
= next
)
1706 next
= e
->next_callee
;
1707 if (!e
->inline_failed
)
1708 cgraph_remove_node_and_inline_clones (e
->callee
);
1710 cgraph_remove_node (node
);
1713 /* Notify finalize_compilation_unit that given node is reachable. */
1716 cgraph_mark_reachable_node (struct cgraph_node
*node
)
1718 if (!node
->reachable
&& node
->local
.finalized
)
1720 if (cgraph_global_info_ready
)
1722 /* Verify that function does not appear to be needed out of blue
1723 during the optimization process. This can happen for extern
1724 inlines when bodies was removed after inlining. */
1725 gcc_assert ((node
->analyzed
|| node
->in_other_partition
1726 || DECL_EXTERNAL (node
->decl
)));
1729 notice_global_symbol (node
->decl
);
1730 node
->reachable
= 1;
1732 node
->next_needed
= cgraph_nodes_queue
;
1733 cgraph_nodes_queue
= node
;
1737 /* Likewise indicate that a node is needed, i.e. reachable via some
1741 cgraph_mark_needed_node (struct cgraph_node
*node
)
1744 gcc_assert (!node
->global
.inlined_to
);
1745 cgraph_mark_reachable_node (node
);
1748 /* Likewise indicate that a node is having address taken. */
1751 cgraph_mark_address_taken_node (struct cgraph_node
*node
)
1753 gcc_assert (!node
->global
.inlined_to
);
1754 cgraph_mark_reachable_node (node
);
1755 node
->address_taken
= 1;
1758 /* Return local info for the compiled function. */
1760 struct cgraph_local_info
*
1761 cgraph_local_info (tree decl
)
1763 struct cgraph_node
*node
;
1765 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
1766 node
= cgraph_node (decl
);
1767 return &node
->local
;
1770 /* Return local info for the compiled function. */
1772 struct cgraph_global_info
*
1773 cgraph_global_info (tree decl
)
1775 struct cgraph_node
*node
;
1777 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
&& cgraph_global_info_ready
);
1778 node
= cgraph_node (decl
);
1779 return &node
->global
;
1782 /* Return local info for the compiled function. */
1784 struct cgraph_rtl_info
*
1785 cgraph_rtl_info (tree decl
)
1787 struct cgraph_node
*node
;
1789 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
1790 node
= cgraph_node (decl
);
1791 if (decl
!= current_function_decl
1792 && !TREE_ASM_WRITTEN (node
->decl
))
1797 /* Return a string describing the failure REASON. */
1800 cgraph_inline_failed_string (cgraph_inline_failed_t reason
)
1803 #define DEFCIFCODE(code, string) string,
1805 static const char *cif_string_table
[CIF_N_REASONS
] = {
1806 #include "cif-code.def"
1809 /* Signedness of an enum type is implementation defined, so cast it
1810 to unsigned before testing. */
1811 gcc_assert ((unsigned) reason
< CIF_N_REASONS
);
1812 return cif_string_table
[reason
];
1815 /* Return name of the node used in debug output. */
1817 cgraph_node_name (struct cgraph_node
*node
)
1819 return lang_hooks
.decl_printable_name (node
->decl
, 2);
1822 /* Names used to print out the availability enum. */
1823 const char * const cgraph_availability_names
[] =
1824 {"unset", "not_available", "overwritable", "available", "local"};
1827 /* Dump call graph node NODE to file F. */
1830 dump_cgraph_node (FILE *f
, struct cgraph_node
*node
)
1832 struct cgraph_edge
*edge
;
1833 int indirect_calls_count
= 0;
1835 fprintf (f
, "%s/%i(%i)", cgraph_node_name (node
), node
->uid
,
1837 dump_addr (f
, " @", (void *)node
);
1838 if (DECL_ASSEMBLER_NAME_SET_P (node
->decl
))
1839 fprintf (f
, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (node
->decl
)));
1840 if (node
->global
.inlined_to
)
1841 fprintf (f
, " (inline copy in %s/%i)",
1842 cgraph_node_name (node
->global
.inlined_to
),
1843 node
->global
.inlined_to
->uid
);
1844 if (node
->same_comdat_group
)
1845 fprintf (f
, " (same comdat group as %s/%i)",
1846 cgraph_node_name (node
->same_comdat_group
),
1847 node
->same_comdat_group
->uid
);
1849 fprintf (f
, " (clone of %s/%i)",
1850 cgraph_node_name (node
->clone_of
),
1851 node
->clone_of
->uid
);
1852 if (cgraph_function_flags_ready
)
1853 fprintf (f
, " availability:%s",
1854 cgraph_availability_names
[cgraph_function_body_availability (node
)]);
1856 fprintf (f
, " analyzed");
1857 if (node
->in_other_partition
)
1858 fprintf (f
, " in_other_partition");
1860 fprintf (f
, " executed "HOST_WIDEST_INT_PRINT_DEC
"x",
1861 (HOST_WIDEST_INT
)node
->count
);
1862 if (node
->local
.inline_summary
.self_time
)
1863 fprintf (f
, " %i time, %i benefit", node
->local
.inline_summary
.self_time
,
1864 node
->local
.inline_summary
.time_inlining_benefit
);
1865 if (node
->global
.time
&& node
->global
.time
1866 != node
->local
.inline_summary
.self_time
)
1867 fprintf (f
, " (%i after inlining)", node
->global
.time
);
1868 if (node
->local
.inline_summary
.self_size
)
1869 fprintf (f
, " %i size, %i benefit", node
->local
.inline_summary
.self_size
,
1870 node
->local
.inline_summary
.size_inlining_benefit
);
1871 if (node
->global
.size
&& node
->global
.size
1872 != node
->local
.inline_summary
.self_size
)
1873 fprintf (f
, " (%i after inlining)", node
->global
.size
);
1874 if (node
->local
.inline_summary
.estimated_self_stack_size
)
1875 fprintf (f
, " %i bytes stack usage", (int)node
->local
.inline_summary
.estimated_self_stack_size
);
1876 if (node
->global
.estimated_stack_size
!= node
->local
.inline_summary
.estimated_self_stack_size
)
1877 fprintf (f
, " %i bytes after inlining", (int)node
->global
.estimated_stack_size
);
1879 fprintf (f
, " nested in: %s", cgraph_node_name (node
->origin
));
1881 fprintf (f
, " needed");
1882 if (node
->address_taken
)
1883 fprintf (f
, " address_taken");
1884 else if (node
->reachable
)
1885 fprintf (f
, " reachable");
1886 else if (node
->reachable_from_other_partition
)
1887 fprintf (f
, " reachable_from_other_partition");
1888 if (gimple_has_body_p (node
->decl
))
1889 fprintf (f
, " body");
1891 fprintf (f
, " process");
1892 if (node
->local
.local
)
1893 fprintf (f
, " local");
1894 if (node
->local
.externally_visible
)
1895 fprintf (f
, " externally_visible");
1896 if (node
->resolution
!= LDPR_UNKNOWN
)
1898 ld_plugin_symbol_resolution_names
[(int)node
->resolution
]);
1899 if (node
->local
.finalized
)
1900 fprintf (f
, " finalized");
1901 if (node
->local
.disregard_inline_limits
)
1902 fprintf (f
, " always_inline");
1903 else if (node
->local
.inlinable
)
1904 fprintf (f
, " inlinable");
1905 else if (node
->local
.versionable
)
1906 fprintf (f
, " versionable");
1907 if (node
->local
.redefined_extern_inline
)
1908 fprintf (f
, " redefined_extern_inline");
1909 if (TREE_ASM_WRITTEN (node
->decl
))
1910 fprintf (f
, " asm_written");
1911 if (node
->only_called_at_startup
)
1912 fprintf (f
, " only_called_at_startup");
1913 if (node
->only_called_at_exit
)
1914 fprintf (f
, " only_called_at_exit");
1916 fprintf (f
, "\n called by: ");
1917 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
1919 fprintf (f
, "%s/%i ", cgraph_node_name (edge
->caller
),
1922 fprintf (f
, "("HOST_WIDEST_INT_PRINT_DEC
"x) ",
1923 (HOST_WIDEST_INT
)edge
->count
);
1924 if (edge
->frequency
)
1925 fprintf (f
, "(%.2f per call) ",
1926 edge
->frequency
/ (double)CGRAPH_FREQ_BASE
);
1927 if (!edge
->inline_failed
)
1928 fprintf(f
, "(inlined) ");
1929 if (edge
->indirect_inlining_edge
)
1930 fprintf(f
, "(indirect_inlining) ");
1931 if (edge
->can_throw_external
)
1932 fprintf(f
, "(can throw external) ");
1935 fprintf (f
, "\n calls: ");
1936 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
1938 fprintf (f
, "%s/%i ", cgraph_node_name (edge
->callee
),
1940 if (!edge
->inline_failed
)
1941 fprintf(f
, "(inlined) ");
1942 if (edge
->indirect_inlining_edge
)
1943 fprintf(f
, "(indirect_inlining) ");
1945 fprintf (f
, "("HOST_WIDEST_INT_PRINT_DEC
"x) ",
1946 (HOST_WIDEST_INT
)edge
->count
);
1947 if (edge
->frequency
)
1948 fprintf (f
, "(%.2f per call) ",
1949 edge
->frequency
/ (double)CGRAPH_FREQ_BASE
);
1950 if (edge
->loop_nest
)
1951 fprintf (f
, "(nested in %i loops) ", edge
->loop_nest
);
1952 if (edge
->can_throw_external
)
1953 fprintf(f
, "(can throw external) ");
1956 fprintf (f
, " References: ");
1957 ipa_dump_references (f
, &node
->ref_list
);
1958 fprintf (f
, " Refering this function: ");
1959 ipa_dump_refering (f
, &node
->ref_list
);
1961 for (edge
= node
->indirect_calls
; edge
; edge
= edge
->next_callee
)
1962 indirect_calls_count
++;
1963 if (indirect_calls_count
)
1964 fprintf (f
, " has %i outgoing edges for indirect calls.\n",
1965 indirect_calls_count
);
1967 if (node
->same_body
)
1969 struct cgraph_node
*n
;
1970 fprintf (f
, " aliases & thunks:");
1971 for (n
= node
->same_body
; n
; n
= n
->next
)
1973 fprintf (f
, " %s/%i", cgraph_node_name (n
), n
->uid
);
1974 if (n
->thunk
.thunk_p
)
1976 fprintf (f
, " (thunk of %s fixed offset %i virtual value %i has "
1977 "virtual offset %i",
1978 lang_hooks
.decl_printable_name (n
->thunk
.alias
, 2),
1979 (int)n
->thunk
.fixed_offset
,
1980 (int)n
->thunk
.virtual_value
,
1981 (int)n
->thunk
.virtual_offset_p
);
1984 if (DECL_ASSEMBLER_NAME_SET_P (n
->decl
))
1985 fprintf (f
, " (asm: %s)", IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (n
->decl
)));
1992 /* Dump call graph node NODE to stderr. */
1995 debug_cgraph_node (struct cgraph_node
*node
)
1997 dump_cgraph_node (stderr
, node
);
2001 /* Dump the callgraph to file F. */
2004 dump_cgraph (FILE *f
)
2006 struct cgraph_node
*node
;
2008 fprintf (f
, "callgraph:\n\n");
2009 for (node
= cgraph_nodes
; node
; node
= node
->next
)
2010 dump_cgraph_node (f
, node
);
2014 /* Dump the call graph to stderr. */
2019 dump_cgraph (stderr
);
2023 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
2026 change_decl_assembler_name (tree decl
, tree name
)
2028 struct cgraph_node
*node
;
2030 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
2031 SET_DECL_ASSEMBLER_NAME (decl
, name
);
2034 if (name
== DECL_ASSEMBLER_NAME (decl
))
2037 if (assembler_name_hash
2038 && TREE_CODE (decl
) == FUNCTION_DECL
2039 && (node
= cgraph_get_node_or_alias (decl
)) != NULL
)
2041 tree old_name
= DECL_ASSEMBLER_NAME (decl
);
2042 slot
= htab_find_slot_with_hash (assembler_name_hash
, old_name
,
2043 decl_assembler_name_hash (old_name
),
2045 /* Inline clones are not hashed. */
2046 if (slot
&& *slot
== node
)
2047 htab_clear_slot (assembler_name_hash
, slot
);
2049 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
))
2050 && DECL_RTL_SET_P (decl
))
2051 warning (0, "%D renamed after being referenced in assembly", decl
);
2053 SET_DECL_ASSEMBLER_NAME (decl
, name
);
2055 if (assembler_name_hash
2056 && TREE_CODE (decl
) == FUNCTION_DECL
2057 && (node
= cgraph_get_node_or_alias (decl
)) != NULL
)
2059 slot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
2060 decl_assembler_name_hash (name
),
2062 gcc_assert (!*slot
);
2067 /* Add a top-level asm statement to the list. */
2069 struct cgraph_asm_node
*
2070 cgraph_add_asm_node (tree asm_str
)
2072 struct cgraph_asm_node
*node
;
2074 node
= ggc_alloc_cleared_cgraph_asm_node ();
2075 node
->asm_str
= asm_str
;
2076 node
->order
= cgraph_order
++;
2078 if (cgraph_asm_nodes
== NULL
)
2079 cgraph_asm_nodes
= node
;
2081 cgraph_asm_last_node
->next
= node
;
2082 cgraph_asm_last_node
= node
;
2086 /* Return true when the DECL can possibly be inlined. */
2088 cgraph_function_possibly_inlined_p (tree decl
)
2090 if (!cgraph_global_info_ready
)
2091 return !DECL_UNINLINABLE (decl
);
2092 return DECL_POSSIBLY_INLINED (decl
);
2095 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
2096 struct cgraph_edge
*
2097 cgraph_clone_edge (struct cgraph_edge
*e
, struct cgraph_node
*n
,
2098 gimple call_stmt
, unsigned stmt_uid
, gcov_type count_scale
,
2099 int freq_scale
, int loop_nest
, bool update_original
)
2101 struct cgraph_edge
*new_edge
;
2102 gcov_type count
= e
->count
* count_scale
/ REG_BR_PROB_BASE
;
2105 /* We do not want to ignore loop nest after frequency drops to 0. */
2108 freq
= e
->frequency
* (gcov_type
) freq_scale
/ CGRAPH_FREQ_BASE
;
2109 if (freq
> CGRAPH_FREQ_MAX
)
2110 freq
= CGRAPH_FREQ_MAX
;
2112 if (e
->indirect_unknown_callee
)
2116 if (call_stmt
&& (decl
= gimple_call_fndecl (call_stmt
)))
2118 struct cgraph_node
*callee
= cgraph_node (decl
);
2119 new_edge
= cgraph_create_edge (n
, callee
, call_stmt
, count
, freq
,
2120 e
->loop_nest
+ loop_nest
);
2124 new_edge
= cgraph_create_indirect_edge (n
, call_stmt
,
2125 e
->indirect_info
->ecf_flags
,
2127 e
->loop_nest
+ loop_nest
);
2128 *new_edge
->indirect_info
= *e
->indirect_info
;
2133 new_edge
= cgraph_create_edge (n
, e
->callee
, call_stmt
, count
, freq
,
2134 e
->loop_nest
+ loop_nest
);
2135 if (e
->indirect_info
)
2137 new_edge
->indirect_info
2138 = ggc_alloc_cleared_cgraph_indirect_call_info ();
2139 *new_edge
->indirect_info
= *e
->indirect_info
;
2143 new_edge
->inline_failed
= e
->inline_failed
;
2144 new_edge
->indirect_inlining_edge
= e
->indirect_inlining_edge
;
2145 new_edge
->lto_stmt_uid
= stmt_uid
;
2146 /* Clone flags that depend on call_stmt availability manually. */
2147 new_edge
->can_throw_external
= e
->can_throw_external
;
2148 new_edge
->call_stmt_cannot_inline_p
= e
->call_stmt_cannot_inline_p
;
2149 if (update_original
)
2151 e
->count
-= new_edge
->count
;
2155 cgraph_call_edge_duplication_hooks (e
, new_edge
);
2159 /* Create node representing clone of N executed COUNT times. Decrease
2160 the execution counts from original node too.
2161 The new clone will have decl set to DECL that may or may not be the same
2164 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
2165 function's profile to reflect the fact that part of execution is handled
2167 struct cgraph_node
*
2168 cgraph_clone_node (struct cgraph_node
*n
, tree decl
, gcov_type count
, int freq
,
2169 int loop_nest
, bool update_original
,
2170 VEC(cgraph_edge_p
,heap
) *redirect_callers
)
2172 struct cgraph_node
*new_node
= cgraph_create_node ();
2173 struct cgraph_edge
*e
;
2174 gcov_type count_scale
;
2177 new_node
->decl
= decl
;
2178 new_node
->origin
= n
->origin
;
2179 if (new_node
->origin
)
2181 new_node
->next_nested
= new_node
->origin
->nested
;
2182 new_node
->origin
->nested
= new_node
;
2184 new_node
->analyzed
= n
->analyzed
;
2185 new_node
->local
= n
->local
;
2186 new_node
->local
.externally_visible
= false;
2187 new_node
->local
.local
= true;
2188 new_node
->local
.vtable_method
= false;
2189 new_node
->global
= n
->global
;
2190 new_node
->rtl
= n
->rtl
;
2191 new_node
->count
= count
;
2192 new_node
->frequency
= n
->frequency
;
2193 new_node
->clone
= n
->clone
;
2194 new_node
->clone
.tree_map
= 0;
2197 if (new_node
->count
> n
->count
)
2198 count_scale
= REG_BR_PROB_BASE
;
2200 count_scale
= new_node
->count
* REG_BR_PROB_BASE
/ n
->count
;
2204 if (update_original
)
2211 FOR_EACH_VEC_ELT (cgraph_edge_p
, redirect_callers
, i
, e
)
2213 /* Redirect calls to the old version node to point to its new
2215 cgraph_redirect_edge_callee (e
, new_node
);
2219 for (e
= n
->callees
;e
; e
=e
->next_callee
)
2220 cgraph_clone_edge (e
, new_node
, e
->call_stmt
, e
->lto_stmt_uid
,
2221 count_scale
, freq
, loop_nest
, update_original
);
2223 for (e
= n
->indirect_calls
; e
; e
= e
->next_callee
)
2224 cgraph_clone_edge (e
, new_node
, e
->call_stmt
, e
->lto_stmt_uid
,
2225 count_scale
, freq
, loop_nest
, update_original
);
2226 ipa_clone_references (new_node
, NULL
, &n
->ref_list
);
2228 new_node
->next_sibling_clone
= n
->clones
;
2230 n
->clones
->prev_sibling_clone
= new_node
;
2231 n
->clones
= new_node
;
2232 new_node
->clone_of
= n
;
2234 cgraph_call_node_duplication_hooks (n
, new_node
);
2235 if (n
->decl
!= decl
)
2237 struct cgraph_node
**slot
;
2238 slot
= (struct cgraph_node
**) htab_find_slot (cgraph_hash
, new_node
, INSERT
);
2239 gcc_assert (!*slot
);
2241 if (assembler_name_hash
)
2244 tree name
= DECL_ASSEMBLER_NAME (decl
);
2246 aslot
= htab_find_slot_with_hash (assembler_name_hash
, name
,
2247 decl_assembler_name_hash (name
),
2249 gcc_assert (!*aslot
);
2256 /* Create a new name for clone of DECL, add SUFFIX. Returns an identifier. */
2258 static GTY(()) unsigned int clone_fn_id_num
;
2261 clone_function_name (tree decl
, const char *suffix
)
2263 tree name
= DECL_ASSEMBLER_NAME (decl
);
2264 size_t len
= IDENTIFIER_LENGTH (name
);
2265 char *tmp_name
, *prefix
;
2267 prefix
= XALLOCAVEC (char, len
+ strlen (suffix
) + 2);
2268 memcpy (prefix
, IDENTIFIER_POINTER (name
), len
);
2269 strcpy (prefix
+ len
+ 1, suffix
);
2270 #ifndef NO_DOT_IN_LABEL
2272 #elif !defined NO_DOLLAR_IN_LABEL
2277 ASM_FORMAT_PRIVATE_NAME (tmp_name
, prefix
, clone_fn_id_num
++);
2278 return get_identifier (tmp_name
);
2281 /* Create callgraph node clone with new declaration. The actual body will
2282 be copied later at compilation stage.
2284 TODO: after merging in ipa-sra use function call notes instead of args_to_skip
2287 struct cgraph_node
*
2288 cgraph_create_virtual_clone (struct cgraph_node
*old_node
,
2289 VEC(cgraph_edge_p
,heap
) *redirect_callers
,
2290 VEC(ipa_replace_map_p
,gc
) *tree_map
,
2291 bitmap args_to_skip
,
2292 const char * suffix
)
2294 tree old_decl
= old_node
->decl
;
2295 struct cgraph_node
*new_node
= NULL
;
2298 struct ipa_replace_map
*map
;
2301 gcc_checking_assert (tree_versionable_function_p (old_decl
));
2303 gcc_assert (old_node
->local
.can_change_signature
|| !args_to_skip
);
2305 /* Make a new FUNCTION_DECL tree node */
2307 new_decl
= copy_node (old_decl
);
2309 new_decl
= build_function_decl_skip_args (old_decl
, args_to_skip
);
2310 DECL_STRUCT_FUNCTION (new_decl
) = NULL
;
2312 /* Generate a new name for the new version. */
2313 DECL_NAME (new_decl
) = clone_function_name (old_decl
, suffix
);
2314 SET_DECL_ASSEMBLER_NAME (new_decl
, DECL_NAME (new_decl
));
2315 SET_DECL_RTL (new_decl
, NULL
);
2317 new_node
= cgraph_clone_node (old_node
, new_decl
, old_node
->count
,
2318 CGRAPH_FREQ_BASE
, 0, false,
2320 /* Update the properties.
2321 Make clone visible only within this translation unit. Make sure
2322 that is not weak also.
2323 ??? We cannot use COMDAT linkage because there is no
2324 ABI support for this. */
2325 DECL_EXTERNAL (new_node
->decl
) = 0;
2326 if (DECL_ONE_ONLY (old_decl
))
2327 DECL_SECTION_NAME (new_node
->decl
) = NULL
;
2328 DECL_COMDAT_GROUP (new_node
->decl
) = 0;
2329 TREE_PUBLIC (new_node
->decl
) = 0;
2330 DECL_COMDAT (new_node
->decl
) = 0;
2331 DECL_WEAK (new_node
->decl
) = 0;
2332 new_node
->clone
.tree_map
= tree_map
;
2333 new_node
->clone
.args_to_skip
= args_to_skip
;
2334 FOR_EACH_VEC_ELT (ipa_replace_map_p
, tree_map
, i
, map
)
2336 tree var
= map
->new_tree
;
2339 if (TREE_CODE (var
) != ADDR_EXPR
)
2341 var
= get_base_var (var
);
2345 /* Record references of the future statement initializing the constant
2347 if (TREE_CODE (var
) == FUNCTION_DECL
)
2348 ipa_record_reference (new_node
, NULL
, cgraph_node (var
),
2349 NULL
, IPA_REF_ADDR
, NULL
);
2350 else if (TREE_CODE (var
) == VAR_DECL
)
2351 ipa_record_reference (new_node
, NULL
, NULL
, varpool_node (var
),
2352 IPA_REF_ADDR
, NULL
);
2355 new_node
->clone
.combined_args_to_skip
= old_node
->clone
.combined_args_to_skip
;
2356 else if (old_node
->clone
.combined_args_to_skip
)
2358 int newi
= 0, oldi
= 0;
2360 bitmap new_args_to_skip
= BITMAP_GGC_ALLOC ();
2361 struct cgraph_node
*orig_node
;
2362 for (orig_node
= old_node
; orig_node
->clone_of
; orig_node
= orig_node
->clone_of
)
2364 for (arg
= DECL_ARGUMENTS (orig_node
->decl
); arg
; arg
= DECL_CHAIN (arg
), oldi
++)
2366 if (bitmap_bit_p (old_node
->clone
.combined_args_to_skip
, oldi
))
2368 bitmap_set_bit (new_args_to_skip
, oldi
);
2371 if (bitmap_bit_p (args_to_skip
, newi
))
2372 bitmap_set_bit (new_args_to_skip
, oldi
);
2375 new_node
->clone
.combined_args_to_skip
= new_args_to_skip
;
2378 new_node
->clone
.combined_args_to_skip
= args_to_skip
;
2379 new_node
->local
.externally_visible
= 0;
2380 new_node
->local
.local
= 1;
2381 new_node
->lowered
= true;
2382 new_node
->reachable
= true;
2388 /* NODE is no longer nested function; update cgraph accordingly. */
2390 cgraph_unnest_node (struct cgraph_node
*node
)
2392 struct cgraph_node
**node2
= &node
->origin
->nested
;
2393 gcc_assert (node
->origin
);
2395 while (*node2
!= node
)
2396 node2
= &(*node2
)->next_nested
;
2397 *node2
= node
->next_nested
;
2398 node
->origin
= NULL
;
2401 /* Return function availability. See cgraph.h for description of individual
2404 cgraph_function_body_availability (struct cgraph_node
*node
)
2406 enum availability avail
;
2407 gcc_assert (cgraph_function_flags_ready
);
2408 if (!node
->analyzed
)
2409 avail
= AVAIL_NOT_AVAILABLE
;
2410 else if (node
->local
.local
)
2411 avail
= AVAIL_LOCAL
;
2412 else if (!node
->local
.externally_visible
)
2413 avail
= AVAIL_AVAILABLE
;
2414 /* Inline functions are safe to be analyzed even if their symbol can
2415 be overwritten at runtime. It is not meaningful to enforce any sane
2416 behaviour on replacing inline function by different body. */
2417 else if (DECL_DECLARED_INLINE_P (node
->decl
))
2418 avail
= AVAIL_AVAILABLE
;
2420 /* If the function can be overwritten, return OVERWRITABLE. Take
2421 care at least of two notable extensions - the COMDAT functions
2422 used to share template instantiations in C++ (this is symmetric
2423 to code cp_cannot_inline_tree_fn and probably shall be shared and
2424 the inlinability hooks completely eliminated).
2426 ??? Does the C++ one definition rule allow us to always return
2427 AVAIL_AVAILABLE here? That would be good reason to preserve this
2430 else if (decl_replaceable_p (node
->decl
) && !DECL_EXTERNAL (node
->decl
))
2431 avail
= AVAIL_OVERWRITABLE
;
2432 else avail
= AVAIL_AVAILABLE
;
2437 /* Add the function FNDECL to the call graph.
2438 Unlike cgraph_finalize_function, this function is intended to be used
2439 by middle end and allows insertion of new function at arbitrary point
2440 of compilation. The function can be either in high, low or SSA form
2443 The function is assumed to be reachable and have address taken (so no
2444 API breaking optimizations are performed on it).
2446 Main work done by this function is to enqueue the function for later
2447 processing to avoid need the passes to be re-entrant. */
2450 cgraph_add_new_function (tree fndecl
, bool lowered
)
2452 struct cgraph_node
*node
;
2453 switch (cgraph_state
)
2455 case CGRAPH_STATE_CONSTRUCTION
:
2456 /* Just enqueue function to be processed at nearest occurrence. */
2457 node
= cgraph_node (fndecl
);
2458 node
->next_needed
= cgraph_new_nodes
;
2460 node
->lowered
= true;
2461 cgraph_new_nodes
= node
;
2464 case CGRAPH_STATE_IPA
:
2465 case CGRAPH_STATE_IPA_SSA
:
2466 case CGRAPH_STATE_EXPANSION
:
2467 /* Bring the function into finalized state and enqueue for later
2468 analyzing and compilation. */
2469 node
= cgraph_node (fndecl
);
2470 node
->local
.local
= false;
2471 node
->local
.finalized
= true;
2472 node
->reachable
= node
->needed
= true;
2473 if (!lowered
&& cgraph_state
== CGRAPH_STATE_EXPANSION
)
2475 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
2476 current_function_decl
= fndecl
;
2477 gimple_register_cfg_hooks ();
2478 tree_lowering_passes (fndecl
);
2479 bitmap_obstack_initialize (NULL
);
2480 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
2481 execute_pass_list (pass_early_local_passes
.pass
.sub
);
2482 bitmap_obstack_release (NULL
);
2484 current_function_decl
= NULL
;
2489 node
->lowered
= true;
2490 node
->next_needed
= cgraph_new_nodes
;
2491 cgraph_new_nodes
= node
;
2494 case CGRAPH_STATE_FINISHED
:
2495 /* At the very end of compilation we have to do all the work up
2497 push_cfun (DECL_STRUCT_FUNCTION (fndecl
));
2498 current_function_decl
= fndecl
;
2499 gimple_register_cfg_hooks ();
2501 tree_lowering_passes (fndecl
);
2502 bitmap_obstack_initialize (NULL
);
2503 if (!gimple_in_ssa_p (DECL_STRUCT_FUNCTION (fndecl
)))
2504 execute_pass_list (pass_early_local_passes
.pass
.sub
);
2505 bitmap_obstack_release (NULL
);
2506 tree_rest_of_compilation (fndecl
);
2508 current_function_decl
= NULL
;
2512 /* Set a personality if required and we already passed EH lowering. */
2514 && (function_needs_eh_personality (DECL_STRUCT_FUNCTION (fndecl
))
2515 == eh_personality_lang
))
2516 DECL_FUNCTION_PERSONALITY (fndecl
) = lang_hooks
.eh_personality ();
2519 /* Return true if NODE can be made local for API change.
2520 Extern inline functions and C++ COMDAT functions can be made local
2521 at the expense of possible code size growth if function is used in multiple
2522 compilation units. */
2524 cgraph_node_can_be_local_p (struct cgraph_node
*node
)
2526 return (!node
->needed
&& !node
->address_taken
2527 && ((DECL_COMDAT (node
->decl
) && !node
->same_comdat_group
)
2528 || !node
->local
.externally_visible
));
2531 /* Make DECL local. FIXME: We shouldn't need to mess with rtl this early,
2532 but other code such as notice_global_symbol generates rtl. */
2534 cgraph_make_decl_local (tree decl
)
2538 if (TREE_CODE (decl
) == VAR_DECL
)
2539 DECL_COMMON (decl
) = 0;
2540 else gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
2542 if (DECL_COMDAT (decl
))
2544 /* It is possible that we are linking against library defining same COMDAT
2545 function. To avoid conflict we need to rename our local name of the
2546 function just in the case WHOPR partitioning decide to make it hidden
2547 to avoid cross partition references. */
2550 const char *old_name
;
2552 old_name
= IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl
));
2553 if (TREE_CODE (decl
) == FUNCTION_DECL
)
2555 struct cgraph_node
*node
= cgraph_get_node_or_alias (decl
);
2556 change_decl_assembler_name (decl
,
2557 clone_function_name (decl
, "local"));
2558 if (node
->local
.lto_file_data
)
2559 lto_record_renamed_decl (node
->local
.lto_file_data
,
2562 (DECL_ASSEMBLER_NAME (decl
)));
2564 else if (TREE_CODE (decl
) == VAR_DECL
)
2566 struct varpool_node
*vnode
= varpool_get_node (decl
);
2567 /* change_decl_assembler_name will warn here on vtables because
2568 C++ frontend still sets TREE_SYMBOL_REFERENCED on them. */
2569 SET_DECL_ASSEMBLER_NAME (decl
,
2570 clone_function_name (decl
, "local"));
2571 if (vnode
->lto_file_data
)
2572 lto_record_renamed_decl (vnode
->lto_file_data
,
2575 (DECL_ASSEMBLER_NAME (decl
)));
2578 DECL_SECTION_NAME (decl
) = 0;
2579 DECL_COMDAT (decl
) = 0;
2581 DECL_COMDAT_GROUP (decl
) = 0;
2582 DECL_WEAK (decl
) = 0;
2583 DECL_EXTERNAL (decl
) = 0;
2584 TREE_PUBLIC (decl
) = 0;
2585 if (!DECL_RTL_SET_P (decl
))
2588 /* Update rtl flags. */
2589 make_decl_rtl (decl
);
2591 rtl
= DECL_RTL (decl
);
2595 symbol
= XEXP (rtl
, 0);
2596 if (GET_CODE (symbol
) != SYMBOL_REF
)
2599 SYMBOL_REF_WEAK (symbol
) = DECL_WEAK (decl
);
2602 /* Bring NODE local. */
2604 cgraph_make_node_local (struct cgraph_node
*node
)
2606 gcc_assert (cgraph_node_can_be_local_p (node
));
2607 if (DECL_COMDAT (node
->decl
) || DECL_EXTERNAL (node
->decl
))
2609 struct cgraph_node
*alias
;
2610 cgraph_make_decl_local (node
->decl
);
2612 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
2613 cgraph_make_decl_local (alias
->decl
);
2615 node
->local
.externally_visible
= false;
2616 node
->local
.local
= true;
2617 node
->resolution
= LDPR_PREVAILING_DEF_IRONLY
;
2618 gcc_assert (cgraph_function_body_availability (node
) == AVAIL_LOCAL
);
2622 /* Set TREE_NOTHROW on NODE's decl and on same_body aliases of NODE
2623 if any to NOTHROW. */
2626 cgraph_set_nothrow_flag (struct cgraph_node
*node
, bool nothrow
)
2628 struct cgraph_node
*alias
;
2629 TREE_NOTHROW (node
->decl
) = nothrow
;
2630 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
2631 TREE_NOTHROW (alias
->decl
) = nothrow
;
2634 /* Set TREE_READONLY on NODE's decl and on same_body aliases of NODE
2635 if any to READONLY. */
2638 cgraph_set_const_flag (struct cgraph_node
*node
, bool readonly
, bool looping
)
2640 struct cgraph_node
*alias
;
2641 /* Static constructors and destructors without a side effect can be
2643 if (!looping
&& readonly
)
2645 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
2646 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
2647 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
2648 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
2650 TREE_READONLY (node
->decl
) = readonly
;
2651 DECL_LOOPING_CONST_OR_PURE_P (node
->decl
) = looping
;
2652 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
2654 TREE_READONLY (alias
->decl
) = readonly
;
2655 DECL_LOOPING_CONST_OR_PURE_P (alias
->decl
) = looping
;
2659 /* Set DECL_PURE_P on NODE's decl and on same_body aliases of NODE
2663 cgraph_set_pure_flag (struct cgraph_node
*node
, bool pure
, bool looping
)
2665 struct cgraph_node
*alias
;
2666 /* Static constructors and destructors without a side effect can be
2668 if (!looping
&& pure
)
2670 if (DECL_STATIC_CONSTRUCTOR (node
->decl
))
2671 DECL_STATIC_CONSTRUCTOR (node
->decl
) = 0;
2672 if (DECL_STATIC_DESTRUCTOR (node
->decl
))
2673 DECL_STATIC_DESTRUCTOR (node
->decl
) = 0;
2675 DECL_PURE_P (node
->decl
) = pure
;
2676 DECL_LOOPING_CONST_OR_PURE_P (node
->decl
) = looping
;
2677 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
2679 DECL_PURE_P (alias
->decl
) = pure
;
2680 DECL_LOOPING_CONST_OR_PURE_P (alias
->decl
) = looping
;
2684 /* See if the frequency of NODE can be updated based on frequencies of its
2687 cgraph_propagate_frequency (struct cgraph_node
*node
)
2689 bool maybe_unlikely_executed
= true, maybe_executed_once
= true;
2690 bool only_called_at_startup
= true;
2691 bool only_called_at_exit
= true;
2692 bool changed
= false;
2693 struct cgraph_edge
*edge
;
2695 if (!node
->local
.local
)
2697 gcc_assert (node
->analyzed
);
2698 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2699 fprintf (dump_file
, "Processing frequency %s\n", cgraph_node_name (node
));
2701 for (edge
= node
->callers
;
2702 edge
&& (maybe_unlikely_executed
|| maybe_executed_once
2703 || only_called_at_startup
|| only_called_at_exit
);
2704 edge
= edge
->next_caller
)
2706 if (edge
->caller
!= node
)
2708 only_called_at_startup
&= edge
->caller
->only_called_at_startup
;
2709 /* It makes sense to put main() together with the static constructors.
2710 It will be executed for sure, but rest of functions called from
2711 main are definitely not at startup only. */
2712 if (MAIN_NAME_P (DECL_NAME (edge
->caller
->decl
)))
2713 only_called_at_startup
= 0;
2714 only_called_at_exit
&= edge
->caller
->only_called_at_exit
;
2716 if (!edge
->frequency
)
2718 switch (edge
->caller
->frequency
)
2720 case NODE_FREQUENCY_UNLIKELY_EXECUTED
:
2722 case NODE_FREQUENCY_EXECUTED_ONCE
:
2723 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2724 fprintf (dump_file
, " Called by %s that is executed once\n",
2725 cgraph_node_name (node
));
2726 maybe_unlikely_executed
= false;
2727 if (edge
->loop_nest
)
2729 maybe_executed_once
= false;
2730 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2731 fprintf (dump_file
, " Called in loop\n");
2734 case NODE_FREQUENCY_HOT
:
2735 case NODE_FREQUENCY_NORMAL
:
2736 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2737 fprintf (dump_file
, " Called by %s that is normal or hot\n",
2738 cgraph_node_name (node
));
2739 maybe_unlikely_executed
= false;
2740 maybe_executed_once
= false;
2744 if ((only_called_at_startup
&& !only_called_at_exit
)
2745 && !node
->only_called_at_startup
)
2747 node
->only_called_at_startup
= true;
2749 fprintf (dump_file
, "Node %s promoted to only called at startup.\n",
2750 cgraph_node_name (node
));
2753 if ((only_called_at_exit
&& !only_called_at_startup
)
2754 && !node
->only_called_at_exit
)
2756 node
->only_called_at_exit
= true;
2758 fprintf (dump_file
, "Node %s promoted to only called at exit.\n",
2759 cgraph_node_name (node
));
2762 /* These come either from profile or user hints; never update them. */
2763 if (node
->frequency
== NODE_FREQUENCY_HOT
2764 || node
->frequency
== NODE_FREQUENCY_UNLIKELY_EXECUTED
)
2766 if (maybe_unlikely_executed
)
2768 node
->frequency
= NODE_FREQUENCY_UNLIKELY_EXECUTED
;
2770 fprintf (dump_file
, "Node %s promoted to unlikely executed.\n",
2771 cgraph_node_name (node
));
2774 else if (maybe_executed_once
&& node
->frequency
!= NODE_FREQUENCY_EXECUTED_ONCE
)
2776 node
->frequency
= NODE_FREQUENCY_EXECUTED_ONCE
;
2778 fprintf (dump_file
, "Node %s promoted to executed once.\n",
2779 cgraph_node_name (node
));
2785 /* Return true when NODE can not return or throw and thus
2786 it is safe to ignore its side effects for IPA analysis. */
2789 cgraph_node_cannot_return (struct cgraph_node
*node
)
2791 int flags
= flags_from_decl_or_type (node
->decl
);
2792 if (!flag_exceptions
)
2793 return (flags
& ECF_NORETURN
) != 0;
2795 return ((flags
& (ECF_NORETURN
| ECF_NOTHROW
))
2796 == (ECF_NORETURN
| ECF_NOTHROW
));
2799 /* Return true when call of E can not lead to return from caller
2800 and thus it is safe to ignore its side effects for IPA analysis
2801 when computing side effects of the caller.
2802 FIXME: We could actually mark all edges that have no reaching
2803 patch to EXIT_BLOCK_PTR or throw to get better results. */
2805 cgraph_edge_cannot_lead_to_return (struct cgraph_edge
*e
)
2807 if (cgraph_node_cannot_return (e
->caller
))
2809 if (e
->indirect_unknown_callee
)
2811 int flags
= e
->indirect_info
->ecf_flags
;
2812 if (!flag_exceptions
)
2813 return (flags
& ECF_NORETURN
) != 0;
2815 return ((flags
& (ECF_NORETURN
| ECF_NOTHROW
))
2816 == (ECF_NORETURN
| ECF_NOTHROW
));
2819 return cgraph_node_cannot_return (e
->callee
);
2822 /* Return true when function NODE can be removed from callgraph
2823 if all direct calls are eliminated. */
2826 cgraph_can_remove_if_no_direct_calls_and_refs_p (struct cgraph_node
*node
)
2828 gcc_assert (!node
->global
.inlined_to
);
2829 /* Extern inlines can always go, we will use the external definition. */
2830 if (DECL_EXTERNAL (node
->decl
))
2832 /* When function is needed, we can not remove it. */
2833 if (node
->needed
|| node
->reachable_from_other_partition
)
2835 if (DECL_STATIC_CONSTRUCTOR (node
->decl
)
2836 || DECL_STATIC_DESTRUCTOR (node
->decl
))
2838 /* Only COMDAT functions can be removed if externally visible. */
2839 if (node
->local
.externally_visible
2840 && (!DECL_COMDAT (node
->decl
)
2841 || cgraph_used_from_object_file_p (node
)))
2846 /* Return true when function NODE can be expected to be removed
2847 from program when direct calls in this compilation unit are removed.
2849 As a special case COMDAT functions are
2850 cgraph_can_remove_if_no_direct_calls_p while the are not
2851 cgraph_only_called_directly_p (it is possible they are called from other
2854 This function behaves as cgraph_only_called_directly_p because eliminating
2855 all uses of COMDAT function does not make it necessarily disappear from
2856 the program unless we are compiling whole program or we do LTO. In this
2857 case we know we win since dynamic linking will not really discard the
2858 linkonce section. */
2861 cgraph_will_be_removed_from_program_if_no_direct_calls (struct cgraph_node
*node
)
2863 gcc_assert (!node
->global
.inlined_to
);
2864 if (cgraph_used_from_object_file_p (node
))
2866 if (!in_lto_p
&& !flag_whole_program
)
2867 return cgraph_only_called_directly_p (node
);
2870 if (DECL_EXTERNAL (node
->decl
))
2872 return cgraph_can_remove_if_no_direct_calls_p (node
);
2876 /* Return true when RESOLUTION indicate that linker will use
2877 the symbol from non-LTO object files. */
2880 resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution
)
2882 return (resolution
== LDPR_PREVAILING_DEF
2883 || resolution
== LDPR_PREEMPTED_REG
2884 || resolution
== LDPR_RESOLVED_EXEC
2885 || resolution
== LDPR_RESOLVED_DYN
);
2888 /* Return true when NODE is known to be used from other (non-LTO) object file.
2889 Known only when doing LTO via linker plugin. */
2892 cgraph_used_from_object_file_p (struct cgraph_node
*node
)
2894 struct cgraph_node
*alias
;
2896 gcc_assert (!node
->global
.inlined_to
);
2897 if (!TREE_PUBLIC (node
->decl
) || DECL_EXTERNAL (node
->decl
))
2899 if (resolution_used_from_other_file_p (node
->resolution
))
2901 for (alias
= node
->same_body
; alias
; alias
= alias
->next
)
2902 if (TREE_PUBLIC (alias
->decl
)
2903 && resolution_used_from_other_file_p (alias
->resolution
))
2908 #include "gt-cgraph.h"