1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005 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 2, 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 COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
22 /* This file contains basic routines manipulating call graph and variable pool
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 (in
32 contrast to tree DECL nodes where we can have multiple nodes for each
35 The mapping from declarations to call-graph nodes is done using hash table
36 based on DECL_ASSEMBLER_NAME, so it is essential for assembler name to
37 not change once the declaration is inserted into the call-graph.
38 The call-graph nodes are created lazily using cgraph_node function when
39 called for unknown declaration.
41 When built, there is one edge for each direct call. It is possible that
42 the reference will be later optimized out. The call-graph is built
43 conservatively in order to make conservative data flow analysis possible.
45 The callgraph at the moment does not represent indirect calls or calls
46 from other compilation unit. Flag NEEDED is set for each node that may
47 be accessed in such an invisible way and it shall be considered an
48 entry point to the callgraph.
50 Intraprocedural information:
52 Callgraph is place to store data needed for intraprocedural optimization.
53 All data structures are divided into three components: local_info that
54 is produced while analyzing the function, global_info that is result
55 of global walking of the callgraph on the end of compilation and
56 rtl_info used by RTL backend to propagate data from already compiled
57 functions to their callers.
61 The function inlining information is decided in advance and maintained
62 in the callgraph as so called inline plan.
63 For each inlined call, the callee's node is cloned to represent the
64 new function copy produced by inliner.
65 Each inlined call gets a unique corresponding clone node of the callee
66 and the data structure is updated while inlining is performed, so
67 the clones are eliminated and their callee edges redirected to the
70 Each edge has "inline_failed" field. When the field is set to NULL,
71 the call will be inlined. When it is non-NULL it contains a reason
72 why inlining wasn't performed.
75 The varpool data structure:
77 Varpool is used to maintain variables in similar manner as call-graph
78 is used for functions. Most of the API is symmetric replacing cgraph
79 function prefix by cgraph_varpool */
84 #include "coretypes.h"
87 #include "tree-inline.h"
88 #include "langhooks.h"
95 #include "basic-block.h"
100 #include "tree-gimple.h"
101 #include "tree-dump.h"
103 static void cgraph_node_remove_callers (struct cgraph_node
*node
);
104 static inline void cgraph_edge_remove_caller (struct cgraph_edge
*e
);
105 static inline void cgraph_edge_remove_callee (struct cgraph_edge
*e
);
107 /* Hash table used to convert declarations into nodes. */
108 static GTY((param_is (struct cgraph_node
))) htab_t cgraph_hash
;
110 /* The linked list of cgraph nodes. */
111 struct cgraph_node
*cgraph_nodes
;
113 /* Queue of cgraph nodes scheduled to be lowered. */
114 struct cgraph_node
*cgraph_nodes_queue
;
116 /* Number of nodes in existence. */
119 /* Maximal uid used in cgraph nodes. */
122 /* Set when whole unit has been analyzed so we can access global info. */
123 bool cgraph_global_info_ready
= false;
125 /* Set when the cgraph is fully build and the basic flags are computed. */
126 bool cgraph_function_flags_ready
= false;
128 /* Hash table used to convert declarations into nodes. */
129 static GTY((param_is (struct cgraph_varpool_node
))) htab_t cgraph_varpool_hash
;
131 /* Queue of cgraph nodes scheduled to be lowered and output. */
132 struct cgraph_varpool_node
*cgraph_varpool_nodes_queue
, *cgraph_varpool_first_unanalyzed_node
;
134 /* The linked list of cgraph varpool nodes. */
135 static GTY(()) struct cgraph_varpool_node
*cgraph_varpool_nodes
;
137 /* End of the varpool queue. Needs to be QTYed to work with PCH. */
138 static GTY(()) struct cgraph_varpool_node
*cgraph_varpool_last_needed_node
;
140 /* Linked list of cgraph asm nodes. */
141 struct cgraph_asm_node
*cgraph_asm_nodes
;
143 /* Last node in cgraph_asm_nodes. */
144 static GTY(()) struct cgraph_asm_node
*cgraph_asm_last_node
;
146 /* The order index of the next cgraph node to be created. This is
147 used so that we can sort the cgraph nodes in order by when we saw
148 them, to support -fno-toplevel-reorder. */
151 static hashval_t
hash_node (const void *);
152 static int eq_node (const void *, const void *);
154 /* Returns a hash code for P. */
157 hash_node (const void *p
)
159 const struct cgraph_node
*n
= (const struct cgraph_node
*) p
;
160 return (hashval_t
) DECL_UID (n
->decl
);
163 /* Returns nonzero if P1 and P2 are equal. */
166 eq_node (const void *p1
, const void *p2
)
168 const struct cgraph_node
*n1
= (const struct cgraph_node
*) p1
;
169 const struct cgraph_node
*n2
= (const struct cgraph_node
*) p2
;
170 return DECL_UID (n1
->decl
) == DECL_UID (n2
->decl
);
173 /* Allocate new callgraph node and insert it into basic data structures. */
174 static struct cgraph_node
*
175 cgraph_create_node (void)
177 struct cgraph_node
*node
;
179 node
= GGC_CNEW (struct cgraph_node
);
180 node
->next
= cgraph_nodes
;
181 node
->uid
= cgraph_max_uid
++;
182 node
->order
= cgraph_order
++;
184 cgraph_nodes
->previous
= node
;
185 node
->previous
= NULL
;
186 node
->global
.estimated_growth
= INT_MIN
;
192 /* Return cgraph node assigned to DECL. Create new one when needed. */
194 cgraph_node (tree decl
)
196 struct cgraph_node key
, *node
, **slot
;
198 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
201 cgraph_hash
= htab_create_ggc (10, hash_node
, eq_node
, NULL
);
205 slot
= (struct cgraph_node
**) htab_find_slot (cgraph_hash
, &key
, INSERT
);
210 if (!node
->master_clone
)
211 node
->master_clone
= node
;
215 node
= cgraph_create_node ();
218 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
220 node
->origin
= cgraph_node (DECL_CONTEXT (decl
));
221 node
->next_nested
= node
->origin
->nested
;
222 node
->origin
->nested
= node
;
223 node
->master_clone
= node
;
228 /* Insert already constructed node into hashtable. */
231 cgraph_insert_node_to_hashtable (struct cgraph_node
*node
)
233 struct cgraph_node
**slot
;
235 slot
= (struct cgraph_node
**) htab_find_slot (cgraph_hash
, node
, INSERT
);
241 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
244 decl_assembler_name_equal (tree decl
, tree asmname
)
246 tree decl_asmname
= DECL_ASSEMBLER_NAME (decl
);
248 if (decl_asmname
== asmname
)
251 /* If the target assembler name was set by the user, things are trickier.
252 We have a leading '*' to begin with. After that, it's arguable what
253 is the correct thing to do with -fleading-underscore. Arguably, we've
254 historically been doing the wrong thing in assemble_alias by always
255 printing the leading underscore. Since we're not changing that, make
256 sure user_label_prefix follows the '*' before matching. */
257 if (IDENTIFIER_POINTER (decl_asmname
)[0] == '*')
259 const char *decl_str
= IDENTIFIER_POINTER (decl_asmname
) + 1;
260 size_t ulp_len
= strlen (user_label_prefix
);
264 else if (strncmp (decl_str
, user_label_prefix
, ulp_len
) == 0)
269 return strcmp (decl_str
, IDENTIFIER_POINTER (asmname
)) == 0;
276 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
277 Return NULL if there's no such node. */
280 cgraph_node_for_asm (tree asmname
)
282 struct cgraph_node
*node
;
284 for (node
= cgraph_nodes
; node
; node
= node
->next
)
285 if (decl_assembler_name_equal (node
->decl
, asmname
))
291 /* Return callgraph edge representing CALL_EXPR statement. */
293 cgraph_edge (struct cgraph_node
*node
, tree call_stmt
)
295 struct cgraph_edge
*e
;
297 /* This loop may turn out to be performance problem. In such case adding
298 hashtables into call nodes with very many edges is probably best
299 solution. It is not good idea to add pointer into CALL_EXPR itself
300 because we want to make possible having multiple cgraph nodes representing
301 different clones of the same body before the body is actually cloned. */
302 for (e
= node
->callees
; e
; e
= e
->next_callee
)
303 if (e
->call_stmt
== call_stmt
)
308 /* Create edge from CALLER to CALLEE in the cgraph. */
311 cgraph_create_edge (struct cgraph_node
*caller
, struct cgraph_node
*callee
,
312 tree call_stmt
, gcov_type count
, int nest
)
314 struct cgraph_edge
*edge
= GGC_NEW (struct cgraph_edge
);
315 #ifdef ENABLE_CHECKING
316 struct cgraph_edge
*e
;
318 for (e
= caller
->callees
; e
; e
= e
->next_callee
)
319 gcc_assert (e
->call_stmt
!= call_stmt
);
322 gcc_assert (get_call_expr_in (call_stmt
));
324 if (!DECL_SAVED_TREE (callee
->decl
))
325 edge
->inline_failed
= N_("function body not available");
326 else if (callee
->local
.redefined_extern_inline
)
327 edge
->inline_failed
= N_("redefined extern inline functions are not "
328 "considered for inlining");
329 else if (callee
->local
.inlinable
)
330 edge
->inline_failed
= N_("function not considered for inlining");
332 edge
->inline_failed
= N_("function not inlinable");
336 edge
->caller
= caller
;
337 edge
->callee
= callee
;
338 edge
->call_stmt
= call_stmt
;
339 edge
->prev_caller
= NULL
;
340 edge
->next_caller
= callee
->callers
;
342 callee
->callers
->prev_caller
= edge
;
343 edge
->prev_callee
= NULL
;
344 edge
->next_callee
= caller
->callees
;
346 caller
->callees
->prev_callee
= edge
;
347 caller
->callees
= edge
;
348 callee
->callers
= edge
;
350 edge
->loop_nest
= nest
;
354 /* Remove the edge E from the list of the callers of the callee. */
357 cgraph_edge_remove_callee (struct cgraph_edge
*e
)
360 e
->prev_caller
->next_caller
= e
->next_caller
;
362 e
->next_caller
->prev_caller
= e
->prev_caller
;
364 e
->callee
->callers
= e
->next_caller
;
367 /* Remove the edge E from the list of the callees of the caller. */
370 cgraph_edge_remove_caller (struct cgraph_edge
*e
)
373 e
->prev_callee
->next_callee
= e
->next_callee
;
375 e
->next_callee
->prev_callee
= e
->prev_callee
;
377 e
->caller
->callees
= e
->next_callee
;
380 /* Remove the edge E in the cgraph. */
383 cgraph_remove_edge (struct cgraph_edge
*e
)
385 /* Remove from callers list of the callee. */
386 cgraph_edge_remove_callee (e
);
388 /* Remove from callees list of the callers. */
389 cgraph_edge_remove_caller (e
);
392 /* Redirect callee of E to N. The function does not update underlying
396 cgraph_redirect_edge_callee (struct cgraph_edge
*e
, struct cgraph_node
*n
)
398 /* Remove from callers list of the current callee. */
399 cgraph_edge_remove_callee (e
);
401 /* Insert to callers list of the new callee. */
402 e
->prev_caller
= NULL
;
404 n
->callers
->prev_caller
= e
;
405 e
->next_caller
= n
->callers
;
410 /* Remove all callees from the node. */
413 cgraph_node_remove_callees (struct cgraph_node
*node
)
415 struct cgraph_edge
*e
;
417 /* It is sufficient to remove the edges from the lists of callers of
418 the callees. The callee list of the node can be zapped with one
420 for (e
= node
->callees
; e
; e
= e
->next_callee
)
421 cgraph_edge_remove_callee (e
);
422 node
->callees
= NULL
;
425 /* Remove all callers from the node. */
428 cgraph_node_remove_callers (struct cgraph_node
*node
)
430 struct cgraph_edge
*e
;
432 /* It is sufficient to remove the edges from the lists of callees of
433 the callers. The caller list of the node can be zapped with one
435 for (e
= node
->callers
; e
; e
= e
->next_caller
)
436 cgraph_edge_remove_caller (e
);
437 node
->callers
= NULL
;
440 /* Remove the node from cgraph. */
443 cgraph_remove_node (struct cgraph_node
*node
)
446 bool kill_body
= false;
448 cgraph_node_remove_callers (node
);
449 cgraph_node_remove_callees (node
);
451 cgraph_remove_node (node
->nested
);
454 struct cgraph_node
**node2
= &node
->origin
->nested
;
456 while (*node2
!= node
)
457 node2
= &(*node2
)->next_nested
;
458 *node2
= node
->next_nested
;
461 node
->previous
->next
= node
->next
;
463 cgraph_nodes
= node
->next
;
465 node
->next
->previous
= node
->previous
;
466 slot
= htab_find_slot (cgraph_hash
, node
, NO_INSERT
);
469 if (node
->next_clone
)
471 struct cgraph_node
*new_node
= node
->next_clone
;
472 struct cgraph_node
*n
;
474 /* Make the next clone be the master clone */
475 for (n
= new_node
; n
; n
= n
->next_clone
)
476 n
->master_clone
= new_node
;
479 node
->next_clone
->prev_clone
= NULL
;
483 htab_clear_slot (cgraph_hash
, slot
);
489 node
->prev_clone
->next_clone
= node
->next_clone
;
490 if (node
->next_clone
)
491 node
->next_clone
->prev_clone
= node
->prev_clone
;
494 /* While all the clones are removed after being proceeded, the function
495 itself is kept in the cgraph even after it is compiled. Check whether
496 we are done with this body and reclaim it proactively if this is the case.
498 if (!kill_body
&& *slot
)
500 struct cgraph_node
*n
= (struct cgraph_node
*) *slot
;
501 if (!n
->next_clone
&& !n
->global
.inlined_to
502 && (cgraph_global_info_ready
503 && (TREE_ASM_WRITTEN (n
->decl
) || DECL_EXTERNAL (n
->decl
))))
507 if (kill_body
&& !dump_enabled_p (TDI_tree_all
) && flag_unit_at_a_time
)
509 DECL_SAVED_TREE (node
->decl
) = NULL
;
510 DECL_STRUCT_FUNCTION (node
->decl
) = NULL
;
511 DECL_INITIAL (node
->decl
) = error_mark_node
;
514 /* Do not free the structure itself so the walk over chain can continue. */
517 /* Notify finalize_compilation_unit that given node is reachable. */
520 cgraph_mark_reachable_node (struct cgraph_node
*node
)
522 if (!node
->reachable
&& node
->local
.finalized
)
524 notice_global_symbol (node
->decl
);
526 gcc_assert (!cgraph_global_info_ready
);
528 node
->next_needed
= cgraph_nodes_queue
;
529 cgraph_nodes_queue
= node
;
533 /* Likewise indicate that a node is needed, i.e. reachable via some
537 cgraph_mark_needed_node (struct cgraph_node
*node
)
540 cgraph_mark_reachable_node (node
);
543 /* Return local info for the compiled function. */
545 struct cgraph_local_info
*
546 cgraph_local_info (tree decl
)
548 struct cgraph_node
*node
;
550 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
551 node
= cgraph_node (decl
);
555 /* Return local info for the compiled function. */
557 struct cgraph_global_info
*
558 cgraph_global_info (tree decl
)
560 struct cgraph_node
*node
;
562 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
&& cgraph_global_info_ready
);
563 node
= cgraph_node (decl
);
564 return &node
->global
;
567 /* Return local info for the compiled function. */
569 struct cgraph_rtl_info
*
570 cgraph_rtl_info (tree decl
)
572 struct cgraph_node
*node
;
574 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
575 node
= cgraph_node (decl
);
576 if (decl
!= current_function_decl
577 && !TREE_ASM_WRITTEN (node
->decl
))
582 /* Return name of the node used in debug output. */
584 cgraph_node_name (struct cgraph_node
*node
)
586 return lang_hooks
.decl_printable_name (node
->decl
, 2);
589 /* Return name of the node used in debug output. */
591 cgraph_varpool_node_name (struct cgraph_varpool_node
*node
)
593 return lang_hooks
.decl_printable_name (node
->decl
, 2);
596 /* Names used to print out the availability enum. */
597 static const char * const availability_names
[] =
598 {"unset", "not_available", "overwrittable", "available", "local"};
600 /* Dump given cgraph node. */
602 dump_cgraph_node (FILE *f
, struct cgraph_node
*node
)
604 struct cgraph_edge
*edge
;
605 fprintf (f
, "%s/%i:", cgraph_node_name (node
), node
->uid
);
606 if (node
->global
.inlined_to
)
607 fprintf (f
, " (inline copy in %s/%i)",
608 cgraph_node_name (node
->global
.inlined_to
),
609 node
->global
.inlined_to
->uid
);
610 if (cgraph_function_flags_ready
)
611 fprintf (f
, " availability:%s",
612 availability_names
[cgraph_function_body_availability (node
)]);
613 if (node
->master_clone
&& node
->master_clone
->uid
!= node
->uid
)
614 fprintf (f
, "(%i)", node
->master_clone
->uid
);
616 fprintf (f
, " executed "HOST_WIDEST_INT_PRINT_DEC
"x",
617 (HOST_WIDEST_INT
)node
->count
);
618 if (node
->local
.self_insns
)
619 fprintf (f
, " %i insns", node
->local
.self_insns
);
620 if (node
->global
.insns
&& node
->global
.insns
!= node
->local
.self_insns
)
621 fprintf (f
, " (%i after inlining)", node
->global
.insns
);
623 fprintf (f
, " nested in: %s", cgraph_node_name (node
->origin
));
625 fprintf (f
, " needed");
626 else if (node
->reachable
)
627 fprintf (f
, " reachable");
628 if (DECL_SAVED_TREE (node
->decl
))
629 fprintf (f
, " tree");
631 fprintf (f
, " output");
632 if (node
->local
.local
)
633 fprintf (f
, " local");
634 if (node
->local
.externally_visible
)
635 fprintf (f
, " externally_visible");
636 if (node
->local
.finalized
)
637 fprintf (f
, " finalized");
638 if (node
->local
.disregard_inline_limits
)
639 fprintf (f
, " always_inline");
640 else if (node
->local
.inlinable
)
641 fprintf (f
, " inlinable");
642 if (node
->local
.redefined_extern_inline
)
643 fprintf (f
, " redefined_extern_inline");
644 if (TREE_ASM_WRITTEN (node
->decl
))
645 fprintf (f
, " asm_written");
647 fprintf (f
, "\n called by: ");
648 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
650 fprintf (f
, "%s/%i ", cgraph_node_name (edge
->caller
),
653 fprintf (f
, "("HOST_WIDEST_INT_PRINT_DEC
"x) ",
654 (HOST_WIDEST_INT
)edge
->count
);
655 if (!edge
->inline_failed
)
656 fprintf(f
, "(inlined) ");
659 fprintf (f
, "\n calls: ");
660 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
662 fprintf (f
, "%s/%i ", cgraph_node_name (edge
->callee
),
664 if (!edge
->inline_failed
)
665 fprintf(f
, "(inlined) ");
667 fprintf (f
, "("HOST_WIDEST_INT_PRINT_DEC
"x) ",
668 (HOST_WIDEST_INT
)edge
->count
);
670 fprintf (f
, "(nested in %i loops) ", edge
->loop_nest
);
675 /* Dump the callgraph. */
678 dump_cgraph (FILE *f
)
680 struct cgraph_node
*node
;
682 fprintf (f
, "callgraph:\n\n");
683 for (node
= cgraph_nodes
; node
; node
= node
->next
)
684 dump_cgraph_node (f
, node
);
687 /* Dump given cgraph node. */
689 dump_cgraph_varpool_node (FILE *f
, struct cgraph_varpool_node
*node
)
691 fprintf (f
, "%s:", cgraph_varpool_node_name (node
));
692 fprintf (f
, " availability:%s", availability_names
[cgraph_variable_initializer_availability (node
)]);
693 if (DECL_INITIAL (node
->decl
))
694 fprintf (f
, " initialized");
696 fprintf (f
, " needed");
698 fprintf (f
, " analyzed");
700 fprintf (f
, " finalized");
702 fprintf (f
, " output");
703 if (node
->externally_visible
)
704 fprintf (f
, " externally_visible");
708 /* Dump the callgraph. */
711 dump_varpool (FILE *f
)
713 struct cgraph_varpool_node
*node
;
715 fprintf (f
, "variable pool:\n\n");
716 for (node
= cgraph_varpool_nodes
; node
; node
= node
->next_needed
)
717 dump_cgraph_varpool_node (f
, node
);
720 /* Returns a hash code for P. */
723 hash_varpool_node (const void *p
)
725 const struct cgraph_varpool_node
*n
= (const struct cgraph_varpool_node
*) p
;
726 return (hashval_t
) DECL_UID (n
->decl
);
729 /* Returns nonzero if P1 and P2 are equal. */
732 eq_varpool_node (const void *p1
, const void *p2
)
734 const struct cgraph_varpool_node
*n1
=
735 (const struct cgraph_varpool_node
*) p1
;
736 const struct cgraph_varpool_node
*n2
=
737 (const struct cgraph_varpool_node
*) p2
;
738 return DECL_UID (n1
->decl
) == DECL_UID (n2
->decl
);
741 /* Return cgraph_varpool node assigned to DECL. Create new one when needed. */
742 struct cgraph_varpool_node
*
743 cgraph_varpool_node (tree decl
)
745 struct cgraph_varpool_node key
, *node
, **slot
;
747 gcc_assert (DECL_P (decl
) && TREE_CODE (decl
) != FUNCTION_DECL
);
749 if (!cgraph_varpool_hash
)
750 cgraph_varpool_hash
= htab_create_ggc (10, hash_varpool_node
,
751 eq_varpool_node
, NULL
);
753 slot
= (struct cgraph_varpool_node
**)
754 htab_find_slot (cgraph_varpool_hash
, &key
, INSERT
);
757 node
= GGC_CNEW (struct cgraph_varpool_node
);
759 node
->order
= cgraph_order
++;
760 node
->next
= cgraph_varpool_nodes
;
761 cgraph_varpool_nodes
= node
;
766 struct cgraph_varpool_node
*
767 cgraph_varpool_node_for_asm (tree asmname
)
769 struct cgraph_varpool_node
*node
;
771 for (node
= cgraph_varpool_nodes
; node
; node
= node
->next
)
772 if (decl_assembler_name_equal (node
->decl
, asmname
))
778 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
780 change_decl_assembler_name (tree decl
, tree name
)
782 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
784 SET_DECL_ASSEMBLER_NAME (decl
, name
);
787 if (name
== DECL_ASSEMBLER_NAME (decl
))
790 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
))
791 && DECL_RTL_SET_P (decl
))
792 warning (0, "%D renamed after being referenced in assembly", decl
);
794 SET_DECL_ASSEMBLER_NAME (decl
, name
);
797 /* Helper function for finalization code - add node into lists so it will
798 be analyzed and compiled. */
800 cgraph_varpool_enqueue_needed_node (struct cgraph_varpool_node
*node
)
802 if (cgraph_varpool_last_needed_node
)
803 cgraph_varpool_last_needed_node
->next_needed
= node
;
804 cgraph_varpool_last_needed_node
= node
;
805 node
->next_needed
= NULL
;
806 if (!cgraph_varpool_nodes_queue
)
807 cgraph_varpool_nodes_queue
= node
;
808 if (!cgraph_varpool_first_unanalyzed_node
)
809 cgraph_varpool_first_unanalyzed_node
= node
;
810 notice_global_symbol (node
->decl
);
813 /* Reset the queue of needed nodes. */
815 cgraph_varpool_reset_queue (void)
817 cgraph_varpool_last_needed_node
= NULL
;
818 cgraph_varpool_nodes_queue
= NULL
;
819 cgraph_varpool_first_unanalyzed_node
= NULL
;
822 /* Notify finalize_compilation_unit that given node is reachable
825 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node
*node
)
827 if (!node
->needed
&& node
->finalized
)
828 cgraph_varpool_enqueue_needed_node (node
);
832 /* Determine if variable DECL is needed. That is, visible to something
833 either outside this translation unit, something magic in the system
834 configury, or (if not doing unit-at-a-time) to something we haven't
838 decide_is_variable_needed (struct cgraph_varpool_node
*node
, tree decl
)
840 /* If the user told us it is used, then it must be so. */
841 if (node
->externally_visible
842 || lookup_attribute ("used", DECL_ATTRIBUTES (decl
)))
845 /* ??? If the assembler name is set by hand, it is possible to assemble
846 the name later after finalizing the function and the fact is noticed
847 in assemble_name then. This is arguably a bug. */
848 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
849 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
852 /* If we decided it was needed before, but at the time we didn't have
853 the definition available, then it's still needed. */
857 /* Externally visible variables must be output. The exception is
858 COMDAT variables that must be output only when they are needed. */
859 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
862 /* When not reordering top level variables, we have to assume that
863 we are going to keep everything. */
864 if (flag_unit_at_a_time
&& flag_toplevel_reorder
)
867 /* We want to emit COMDAT variables only when absolutely necessary. */
868 if (DECL_COMDAT (decl
))
874 cgraph_varpool_finalize_decl (tree decl
)
876 struct cgraph_varpool_node
*node
= cgraph_varpool_node (decl
);
878 /* The first declaration of a variable that comes through this function
879 decides whether it is global (in C, has external linkage)
880 or local (in C, has internal linkage). So do nothing more
881 if this function has already run. */
884 if (cgraph_global_info_ready
|| !flag_unit_at_a_time
)
885 cgraph_varpool_assemble_pending_decls ();
889 cgraph_varpool_enqueue_needed_node (node
);
890 node
->finalized
= true;
892 if (decide_is_variable_needed (node
, decl
))
893 cgraph_varpool_mark_needed_node (node
);
894 /* Since we reclaim unreachable nodes at the end of every language
895 level unit, we need to be conservative about possible entry points
897 else if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
898 cgraph_varpool_mark_needed_node (node
);
899 if (cgraph_global_info_ready
|| !flag_unit_at_a_time
)
900 cgraph_varpool_assemble_pending_decls ();
903 /* Add a top-level asm statement to the list. */
905 struct cgraph_asm_node
*
906 cgraph_add_asm_node (tree asm_str
)
908 struct cgraph_asm_node
*node
;
910 node
= GGC_CNEW (struct cgraph_asm_node
);
911 node
->asm_str
= asm_str
;
912 node
->order
= cgraph_order
++;
914 if (cgraph_asm_nodes
== NULL
)
915 cgraph_asm_nodes
= node
;
917 cgraph_asm_last_node
->next
= node
;
918 cgraph_asm_last_node
= node
;
922 /* Return true when the DECL can possibly be inlined. */
924 cgraph_function_possibly_inlined_p (tree decl
)
926 if (!cgraph_global_info_ready
)
927 return (DECL_INLINE (decl
) && !flag_really_no_inline
);
928 return DECL_POSSIBLY_INLINED (decl
);
931 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
933 cgraph_clone_edge (struct cgraph_edge
*e
, struct cgraph_node
*n
,
934 tree call_stmt
, gcov_type count_scale
, int loop_nest
,
935 bool update_original
)
937 struct cgraph_edge
*new;
939 new = cgraph_create_edge (n
, e
->callee
, call_stmt
,
940 e
->count
* count_scale
/ REG_BR_PROB_BASE
,
941 e
->loop_nest
+ loop_nest
);
943 new->inline_failed
= e
->inline_failed
;
946 e
->count
-= new->count
;
953 /* Create node representing clone of N executed COUNT times. Decrease
954 the execution counts from original node too.
956 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
957 function's profile to reflect the fact that part of execution is handled
960 cgraph_clone_node (struct cgraph_node
*n
, gcov_type count
, int loop_nest
,
961 bool update_original
)
963 struct cgraph_node
*new = cgraph_create_node ();
964 struct cgraph_edge
*e
;
965 gcov_type count_scale
;
968 new->origin
= n
->origin
;
971 new->next_nested
= new->origin
->nested
;
972 new->origin
->nested
= new;
974 new->analyzed
= n
->analyzed
;
975 new->local
= n
->local
;
976 new->global
= n
->global
;
978 new->master_clone
= n
->master_clone
;
981 count_scale
= new->count
* REG_BR_PROB_BASE
/ n
->count
;
991 for (e
= n
->callees
;e
; e
=e
->next_callee
)
992 cgraph_clone_edge (e
, new, e
->call_stmt
, count_scale
, loop_nest
,
995 new->next_clone
= n
->next_clone
;
999 new->next_clone
->prev_clone
= new;
1004 /* Return true if N is an master_clone, (see cgraph_master_clone). */
1007 cgraph_is_master_clone (struct cgraph_node
*n
)
1009 return (n
== cgraph_master_clone (n
));
1012 struct cgraph_node
*
1013 cgraph_master_clone (struct cgraph_node
*n
)
1015 enum availability avail
= cgraph_function_body_availability (n
);
1017 if (avail
== AVAIL_NOT_AVAILABLE
|| avail
== AVAIL_OVERWRITABLE
)
1020 if (!n
->master_clone
)
1021 n
->master_clone
= cgraph_node (n
->decl
);
1023 return n
->master_clone
;
1026 /* NODE is no longer nested function; update cgraph accordingly. */
1028 cgraph_unnest_node (struct cgraph_node
*node
)
1030 struct cgraph_node
**node2
= &node
->origin
->nested
;
1031 gcc_assert (node
->origin
);
1033 while (*node2
!= node
)
1034 node2
= &(*node2
)->next_nested
;
1035 *node2
= node
->next_nested
;
1036 node
->origin
= NULL
;
1039 /* Return function availability. See cgraph.h for description of individual
1042 cgraph_function_body_availability (struct cgraph_node
*node
)
1044 enum availability avail
;
1045 gcc_assert (cgraph_function_flags_ready
);
1046 if (!node
->analyzed
)
1047 avail
= AVAIL_NOT_AVAILABLE
;
1048 else if (node
->local
.local
)
1049 avail
= AVAIL_LOCAL
;
1050 else if (node
->local
.externally_visible
)
1051 avail
= AVAIL_AVAILABLE
;
1053 /* If the function can be overwritten, return OVERWRITABLE. Take
1054 care at least of two notable extensions - the COMDAT functions
1055 used to share template instantiations in C++ (this is symmetric
1056 to code cp_cannot_inline_tree_fn and probably shall be shared and
1057 the inlinability hooks completely eliminated).
1059 ??? Does the C++ one definition rule allow us to always return
1060 AVAIL_AVAILABLE here? That would be good reason to preserve this
1061 hook Similarly deal with extern inline functions - this is again
1062 necessary to get C++ shared functions having keyed templates
1063 right and in the C extension documentation we probably should
1064 document the requirement of both versions of function (extern
1065 inline and offline) having same side effect characteristics as
1066 good optimization is what this optimization is about. */
1068 else if (!(*targetm
.binds_local_p
) (node
->decl
)
1069 && !DECL_COMDAT (node
->decl
) && !DECL_EXTERNAL (node
->decl
))
1070 avail
= AVAIL_OVERWRITABLE
;
1071 else avail
= AVAIL_AVAILABLE
;
1076 /* Return variable availability. See cgraph.h for description of individual
1079 cgraph_variable_initializer_availability (struct cgraph_varpool_node
*node
)
1081 gcc_assert (cgraph_function_flags_ready
);
1082 if (!node
->finalized
)
1083 return AVAIL_NOT_AVAILABLE
;
1084 if (!TREE_PUBLIC (node
->decl
))
1085 return AVAIL_AVAILABLE
;
1086 /* If the variable can be overwritten, return OVERWRITABLE. Takes
1087 care of at least two notable extensions - the COMDAT variables
1088 used to share template instantiations in C++. */
1089 if (!(*targetm
.binds_local_p
) (node
->decl
) && !DECL_COMDAT (node
->decl
))
1090 return AVAIL_OVERWRITABLE
;
1091 return AVAIL_AVAILABLE
;
1094 #include "gt-cgraph.h"