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
;
135 /* The linked list of cgraph varpool nodes. */
136 static GTY(()) struct cgraph_varpool_node
*cgraph_varpool_nodes
;
138 /* End of the varpool queue. Needs to be QTYed to work with PCH. */
139 static GTY(()) struct cgraph_varpool_node
*cgraph_varpool_last_needed_node
;
141 static hashval_t
hash_node (const void *);
142 static int eq_node (const void *, const void *);
144 /* Returns a hash code for P. */
147 hash_node (const void *p
)
149 const struct cgraph_node
*n
= p
;
150 return (hashval_t
) DECL_UID (n
->decl
);
153 /* Returns nonzero if P1 and P2 are equal. */
156 eq_node (const void *p1
, const void *p2
)
158 const struct cgraph_node
*n1
= p1
, *n2
= p2
;
159 return DECL_UID (n1
->decl
) == DECL_UID (n2
->decl
);
162 /* Allocate new callgraph node and insert it into basic data structures. */
163 static struct cgraph_node
*
164 cgraph_create_node (void)
166 struct cgraph_node
*node
;
168 node
= ggc_alloc_cleared (sizeof (*node
));
169 node
->next
= cgraph_nodes
;
170 node
->uid
= cgraph_max_uid
++;
172 cgraph_nodes
->previous
= node
;
173 node
->previous
= NULL
;
174 node
->global
.estimated_growth
= INT_MIN
;
180 /* Return cgraph node assigned to DECL. Create new one when needed. */
182 cgraph_node (tree decl
)
184 struct cgraph_node key
, *node
, **slot
;
186 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
189 cgraph_hash
= htab_create_ggc (10, hash_node
, eq_node
, NULL
);
193 slot
= (struct cgraph_node
**) htab_find_slot (cgraph_hash
, &key
, INSERT
);
198 if (!node
->master_clone
)
199 node
->master_clone
= node
;
203 node
= cgraph_create_node ();
206 if (DECL_CONTEXT (decl
) && TREE_CODE (DECL_CONTEXT (decl
)) == FUNCTION_DECL
)
208 node
->origin
= cgraph_node (DECL_CONTEXT (decl
));
209 node
->next_nested
= node
->origin
->nested
;
210 node
->origin
->nested
= node
;
211 node
->master_clone
= node
;
216 /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL. */
219 decl_assembler_name_equal (tree decl
, tree asmname
)
221 tree decl_asmname
= DECL_ASSEMBLER_NAME (decl
);
223 if (decl_asmname
== asmname
)
226 /* If the target assembler name was set by the user, things are trickier.
227 We have a leading '*' to begin with. After that, it's arguable what
228 is the correct thing to do with -fleading-underscore. Arguably, we've
229 historically been doing the wrong thing in assemble_alias by always
230 printing the leading underscore. Since we're not changing that, make
231 sure user_label_prefix follows the '*' before matching. */
232 if (IDENTIFIER_POINTER (decl_asmname
)[0] == '*')
234 const char *decl_str
= IDENTIFIER_POINTER (decl_asmname
) + 1;
235 size_t ulp_len
= strlen (user_label_prefix
);
239 else if (strncmp (decl_str
, user_label_prefix
, ulp_len
) == 0)
244 return strcmp (decl_str
, IDENTIFIER_POINTER (asmname
)) == 0;
251 /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
252 Return NULL if there's no such node. */
255 cgraph_node_for_asm (tree asmname
)
257 struct cgraph_node
*node
;
259 for (node
= cgraph_nodes
; node
; node
= node
->next
)
260 if (decl_assembler_name_equal (node
->decl
, asmname
))
266 /* Return callgraph edge representing CALL_EXPR statement. */
268 cgraph_edge (struct cgraph_node
*node
, tree call_stmt
)
270 struct cgraph_edge
*e
;
272 /* This loop may turn out to be performance problem. In such case adding
273 hashtables into call nodes with very many edges is probably best
274 solution. It is not good idea to add pointer into CALL_EXPR itself
275 because we want to make possible having multiple cgraph nodes representing
276 different clones of the same body before the body is actually cloned. */
277 for (e
= node
->callees
; e
; e
= e
->next_callee
)
278 if (e
->call_stmt
== call_stmt
)
283 /* Create edge from CALLER to CALLEE in the cgraph. */
286 cgraph_create_edge (struct cgraph_node
*caller
, struct cgraph_node
*callee
,
287 tree call_stmt
, gcov_type count
, int nest
)
289 struct cgraph_edge
*edge
= ggc_alloc (sizeof (struct cgraph_edge
));
290 #ifdef ENABLE_CHECKING
291 struct cgraph_edge
*e
;
293 for (e
= caller
->callees
; e
; e
= e
->next_callee
)
294 gcc_assert (e
->call_stmt
!= call_stmt
);
297 gcc_assert (get_call_expr_in (call_stmt
));
299 if (!DECL_SAVED_TREE (callee
->decl
))
300 edge
->inline_failed
= N_("function body not available");
301 else if (callee
->local
.redefined_extern_inline
)
302 edge
->inline_failed
= N_("redefined extern inline functions are not "
303 "considered for inlining");
304 else if (callee
->local
.inlinable
)
305 edge
->inline_failed
= N_("function not considered for inlining");
307 edge
->inline_failed
= N_("function not inlinable");
311 edge
->caller
= caller
;
312 edge
->callee
= callee
;
313 edge
->call_stmt
= call_stmt
;
314 edge
->prev_caller
= NULL
;
315 edge
->next_caller
= callee
->callers
;
317 callee
->callers
->prev_caller
= edge
;
318 edge
->prev_callee
= NULL
;
319 edge
->next_callee
= caller
->callees
;
321 caller
->callees
->prev_callee
= edge
;
322 caller
->callees
= edge
;
323 callee
->callers
= edge
;
325 edge
->loop_nest
= nest
;
329 /* Remove the edge E from the list of the callers of the callee. */
332 cgraph_edge_remove_callee (struct cgraph_edge
*e
)
335 e
->prev_caller
->next_caller
= e
->next_caller
;
337 e
->next_caller
->prev_caller
= e
->prev_caller
;
339 e
->callee
->callers
= e
->next_caller
;
342 /* Remove the edge E from the list of the callees of the caller. */
345 cgraph_edge_remove_caller (struct cgraph_edge
*e
)
348 e
->prev_callee
->next_callee
= e
->next_callee
;
350 e
->next_callee
->prev_callee
= e
->prev_callee
;
352 e
->caller
->callees
= e
->next_callee
;
355 /* Remove the edge E in the cgraph. */
358 cgraph_remove_edge (struct cgraph_edge
*e
)
360 /* Remove from callers list of the callee. */
361 cgraph_edge_remove_callee (e
);
363 /* Remove from callees list of the callers. */
364 cgraph_edge_remove_caller (e
);
367 /* Redirect callee of E to N. The function does not update underlying
371 cgraph_redirect_edge_callee (struct cgraph_edge
*e
, struct cgraph_node
*n
)
373 /* Remove from callers list of the current callee. */
374 cgraph_edge_remove_callee (e
);
376 /* Insert to callers list of the new callee. */
377 e
->prev_caller
= NULL
;
379 n
->callers
->prev_caller
= e
;
380 e
->next_caller
= n
->callers
;
385 /* Remove all callees from the node. */
388 cgraph_node_remove_callees (struct cgraph_node
*node
)
390 struct cgraph_edge
*e
;
392 /* It is sufficient to remove the edges from the lists of callers of
393 the callees. The callee list of the node can be zapped with one
395 for (e
= node
->callees
; e
; e
= e
->next_callee
)
396 cgraph_edge_remove_callee (e
);
397 node
->callees
= NULL
;
400 /* Remove all callers from the node. */
403 cgraph_node_remove_callers (struct cgraph_node
*node
)
405 struct cgraph_edge
*e
;
407 /* It is sufficient to remove the edges from the lists of callees of
408 the callers. The caller list of the node can be zapped with one
410 for (e
= node
->callers
; e
; e
= e
->next_caller
)
411 cgraph_edge_remove_caller (e
);
412 node
->callers
= NULL
;
415 /* Remove the node from cgraph. */
418 cgraph_remove_node (struct cgraph_node
*node
)
421 bool kill_body
= false;
423 cgraph_node_remove_callers (node
);
424 cgraph_node_remove_callees (node
);
426 cgraph_remove_node (node
->nested
);
429 struct cgraph_node
**node2
= &node
->origin
->nested
;
431 while (*node2
!= node
)
432 node2
= &(*node2
)->next_nested
;
433 *node2
= node
->next_nested
;
436 node
->previous
->next
= node
->next
;
438 cgraph_nodes
= node
->next
;
440 node
->next
->previous
= node
->previous
;
441 slot
= htab_find_slot (cgraph_hash
, node
, NO_INSERT
);
444 if (node
->next_clone
)
446 struct cgraph_node
*new_node
= node
->next_clone
;
447 struct cgraph_node
*n
;
449 /* Make the next clone be the master clone */
450 for (n
= new_node
; n
; n
= n
->next_clone
)
451 n
->master_clone
= new_node
;
454 node
->next_clone
->prev_clone
= NULL
;
458 htab_clear_slot (cgraph_hash
, slot
);
464 node
->prev_clone
->next_clone
= node
->next_clone
;
465 if (node
->next_clone
)
466 node
->next_clone
->prev_clone
= node
->prev_clone
;
469 /* While all the clones are removed after being proceeded, the function
470 itself is kept in the cgraph even after it is compiled. Check whether
471 we are done with this body and reclaim it proactively if this is the case.
473 if (!kill_body
&& *slot
)
475 struct cgraph_node
*n
= *slot
;
476 if (!n
->next_clone
&& !n
->global
.inlined_to
477 && (cgraph_global_info_ready
478 && (TREE_ASM_WRITTEN (n
->decl
) || DECL_EXTERNAL (n
->decl
))))
482 if (kill_body
&& !dump_enabled_p (TDI_tree_all
) && flag_unit_at_a_time
)
484 DECL_SAVED_TREE (node
->decl
) = NULL
;
485 DECL_STRUCT_FUNCTION (node
->decl
) = NULL
;
486 DECL_INITIAL (node
->decl
) = error_mark_node
;
489 /* Do not free the structure itself so the walk over chain can continue. */
492 /* Notify finalize_compilation_unit that given node is reachable. */
495 cgraph_mark_reachable_node (struct cgraph_node
*node
)
497 if (!node
->reachable
&& node
->local
.finalized
)
499 notice_global_symbol (node
->decl
);
501 gcc_assert (!cgraph_global_info_ready
);
503 node
->next_needed
= cgraph_nodes_queue
;
504 cgraph_nodes_queue
= node
;
508 /* Likewise indicate that a node is needed, i.e. reachable via some
512 cgraph_mark_needed_node (struct cgraph_node
*node
)
515 cgraph_mark_reachable_node (node
);
518 /* Return local info for the compiled function. */
520 struct cgraph_local_info
*
521 cgraph_local_info (tree decl
)
523 struct cgraph_node
*node
;
525 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
526 node
= cgraph_node (decl
);
530 /* Return local info for the compiled function. */
532 struct cgraph_global_info
*
533 cgraph_global_info (tree decl
)
535 struct cgraph_node
*node
;
537 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
&& cgraph_global_info_ready
);
538 node
= cgraph_node (decl
);
539 return &node
->global
;
542 /* Return local info for the compiled function. */
544 struct cgraph_rtl_info
*
545 cgraph_rtl_info (tree decl
)
547 struct cgraph_node
*node
;
549 gcc_assert (TREE_CODE (decl
) == FUNCTION_DECL
);
550 node
= cgraph_node (decl
);
551 if (decl
!= current_function_decl
552 && !TREE_ASM_WRITTEN (node
->decl
))
557 /* Return name of the node used in debug output. */
559 cgraph_node_name (struct cgraph_node
*node
)
561 return lang_hooks
.decl_printable_name (node
->decl
, 2);
564 /* Return name of the node used in debug output. */
566 cgraph_varpool_node_name (struct cgraph_varpool_node
*node
)
568 return lang_hooks
.decl_printable_name (node
->decl
, 2);
571 /* Names used to print out the availability enum. */
572 static const char * const availability_names
[] =
573 {"unset", "not_available", "overwrittable", "available", "local"};
575 /* Dump given cgraph node. */
577 dump_cgraph_node (FILE *f
, struct cgraph_node
*node
)
579 struct cgraph_edge
*edge
;
580 fprintf (f
, "%s/%i:", cgraph_node_name (node
), node
->uid
);
581 if (node
->global
.inlined_to
)
582 fprintf (f
, " (inline copy in %s/%i)",
583 cgraph_node_name (node
->global
.inlined_to
),
584 node
->global
.inlined_to
->uid
);
585 if (cgraph_function_flags_ready
)
586 fprintf (f
, " availability:%s",
587 availability_names
[cgraph_function_body_availability (node
)]);
588 if (node
->master_clone
&& node
->master_clone
->uid
!= node
->uid
)
589 fprintf (f
, "(%i)", node
->master_clone
->uid
);
591 fprintf (f
, " executed "HOST_WIDEST_INT_PRINT_DEC
"x",
592 (HOST_WIDEST_INT
)node
->count
);
593 if (node
->local
.self_insns
)
594 fprintf (f
, " %i insns", node
->local
.self_insns
);
595 if (node
->global
.insns
&& node
->global
.insns
!= node
->local
.self_insns
)
596 fprintf (f
, " (%i after inlining)", node
->global
.insns
);
598 fprintf (f
, " nested in: %s", cgraph_node_name (node
->origin
));
600 fprintf (f
, " needed");
601 else if (node
->reachable
)
602 fprintf (f
, " reachable");
603 if (DECL_SAVED_TREE (node
->decl
))
604 fprintf (f
, " tree");
606 fprintf (f
, " output");
607 if (node
->local
.local
)
608 fprintf (f
, " local");
609 if (node
->local
.externally_visible
)
610 fprintf (f
, " externally_visible");
611 if (node
->local
.finalized
)
612 fprintf (f
, " finalized");
613 if (node
->local
.disregard_inline_limits
)
614 fprintf (f
, " always_inline");
615 else if (node
->local
.inlinable
)
616 fprintf (f
, " inlinable");
617 if (node
->local
.redefined_extern_inline
)
618 fprintf (f
, " redefined_extern_inline");
619 if (TREE_ASM_WRITTEN (node
->decl
))
620 fprintf (f
, " asm_written");
622 fprintf (f
, "\n called by: ");
623 for (edge
= node
->callers
; edge
; edge
= edge
->next_caller
)
625 fprintf (f
, "%s/%i ", cgraph_node_name (edge
->caller
),
628 fprintf (f
, "("HOST_WIDEST_INT_PRINT_DEC
"x) ",
629 (HOST_WIDEST_INT
)edge
->count
);
630 if (!edge
->inline_failed
)
631 fprintf(f
, "(inlined) ");
634 fprintf (f
, "\n calls: ");
635 for (edge
= node
->callees
; edge
; edge
= edge
->next_callee
)
637 fprintf (f
, "%s/%i ", cgraph_node_name (edge
->callee
),
639 if (!edge
->inline_failed
)
640 fprintf(f
, "(inlined) ");
642 fprintf (f
, "("HOST_WIDEST_INT_PRINT_DEC
"x) ",
643 (HOST_WIDEST_INT
)edge
->count
);
645 fprintf (f
, "(nested in %i loops) ", edge
->loop_nest
);
650 /* Dump the callgraph. */
653 dump_cgraph (FILE *f
)
655 struct cgraph_node
*node
;
657 fprintf (f
, "callgraph:\n\n");
658 for (node
= cgraph_nodes
; node
; node
= node
->next
)
659 dump_cgraph_node (f
, node
);
662 /* Dump given cgraph node. */
664 dump_cgraph_varpool_node (FILE *f
, struct cgraph_varpool_node
*node
)
666 fprintf (f
, "%s:", cgraph_varpool_node_name (node
));
667 fprintf (f
, " availability:%s", availability_names
[cgraph_variable_initializer_availability (node
)]);
668 if (DECL_INITIAL (node
->decl
))
669 fprintf (f
, " initialized");
671 fprintf (f
, " needed");
673 fprintf (f
, " analyzed");
675 fprintf (f
, " finalized");
677 fprintf (f
, " output");
678 if (node
->externally_visible
)
679 fprintf (f
, " externally_visible");
683 /* Dump the callgraph. */
686 dump_varpool (FILE *f
)
688 struct cgraph_varpool_node
*node
;
690 fprintf (f
, "variable pool:\n\n");
691 for (node
= cgraph_varpool_nodes
; node
; node
= node
->next_needed
)
692 dump_cgraph_varpool_node (f
, node
);
695 /* Returns a hash code for P. */
698 hash_varpool_node (const void *p
)
700 const struct cgraph_varpool_node
*n
= p
;
701 return (hashval_t
) DECL_UID (n
->decl
);
704 /* Returns nonzero if P1 and P2 are equal. */
707 eq_varpool_node (const void *p1
, const void *p2
)
709 const struct cgraph_varpool_node
*n1
= p1
, *n2
= p2
;
710 return DECL_UID (n1
->decl
) == DECL_UID (n2
->decl
);
713 /* Return cgraph_varpool node assigned to DECL. Create new one when needed. */
714 struct cgraph_varpool_node
*
715 cgraph_varpool_node (tree decl
)
717 struct cgraph_varpool_node key
, *node
, **slot
;
719 gcc_assert (DECL_P (decl
) && TREE_CODE (decl
) != FUNCTION_DECL
);
721 if (!cgraph_varpool_hash
)
722 cgraph_varpool_hash
= htab_create_ggc (10, hash_varpool_node
,
723 eq_varpool_node
, NULL
);
725 slot
= (struct cgraph_varpool_node
**)
726 htab_find_slot (cgraph_varpool_hash
, &key
, INSERT
);
729 node
= ggc_alloc_cleared (sizeof (*node
));
731 node
->next
= cgraph_varpool_nodes
;
732 cgraph_varpool_nodes
= node
;
737 struct cgraph_varpool_node
*
738 cgraph_varpool_node_for_asm (tree asmname
)
740 struct cgraph_varpool_node
*node
;
742 for (node
= cgraph_varpool_nodes
; node
; node
= node
->next
)
743 if (decl_assembler_name_equal (node
->decl
, asmname
))
749 /* Set the DECL_ASSEMBLER_NAME and update cgraph hashtables. */
751 change_decl_assembler_name (tree decl
, tree name
)
753 if (!DECL_ASSEMBLER_NAME_SET_P (decl
))
755 SET_DECL_ASSEMBLER_NAME (decl
, name
);
758 if (name
== DECL_ASSEMBLER_NAME (decl
))
761 if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
))
762 && DECL_RTL_SET_P (decl
))
763 warning (0, "%D renamed after being referenced in assembly", decl
);
765 SET_DECL_ASSEMBLER_NAME (decl
, name
);
768 /* Helper function for finalization code - add node into lists so it will
769 be analyzed and compiled. */
771 cgraph_varpool_enqueue_needed_node (struct cgraph_varpool_node
*node
)
773 if (cgraph_varpool_last_needed_node
)
774 cgraph_varpool_last_needed_node
->next_needed
= node
;
775 cgraph_varpool_last_needed_node
= node
;
776 node
->next_needed
= NULL
;
777 if (!cgraph_varpool_nodes_queue
)
778 cgraph_varpool_nodes_queue
= node
;
779 if (!cgraph_varpool_first_unanalyzed_node
)
780 cgraph_varpool_first_unanalyzed_node
= node
;
781 notice_global_symbol (node
->decl
);
784 /* Reset the queue of needed nodes. */
786 cgraph_varpool_reset_queue (void)
788 cgraph_varpool_last_needed_node
= NULL
;
789 cgraph_varpool_nodes_queue
= NULL
;
790 cgraph_varpool_first_unanalyzed_node
= NULL
;
793 /* Notify finalize_compilation_unit that given node is reachable
796 cgraph_varpool_mark_needed_node (struct cgraph_varpool_node
*node
)
798 if (!node
->needed
&& node
->finalized
)
799 cgraph_varpool_enqueue_needed_node (node
);
803 /* Determine if variable DECL is needed. That is, visible to something
804 either outside this translation unit, something magic in the system
805 configury, or (if not doing unit-at-a-time) to something we haven't
809 decide_is_variable_needed (struct cgraph_varpool_node
*node
, tree decl
)
811 /* If the user told us it is used, then it must be so. */
812 if (node
->externally_visible
813 || lookup_attribute ("used", DECL_ATTRIBUTES (decl
)))
816 /* ??? If the assembler name is set by hand, it is possible to assemble
817 the name later after finalizing the function and the fact is noticed
818 in assemble_name then. This is arguably a bug. */
819 if (DECL_ASSEMBLER_NAME_SET_P (decl
)
820 && TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl
)))
823 /* If we decided it was needed before, but at the time we didn't have
824 the definition available, then it's still needed. */
828 /* Externally visible variables must be output. The exception is
829 COMDAT variables that must be output only when they are needed. */
830 if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
833 if (flag_unit_at_a_time
)
836 /* If not doing unit at a time, then we'll only defer this function
837 if its marked for inlining. Otherwise we want to emit it now. */
839 /* We want to emit COMDAT variables only when absolutely necessary. */
840 if (DECL_COMDAT (decl
))
846 cgraph_varpool_finalize_decl (tree decl
)
848 struct cgraph_varpool_node
*node
= cgraph_varpool_node (decl
);
850 /* The first declaration of a variable that comes through this function
851 decides whether it is global (in C, has external linkage)
852 or local (in C, has internal linkage). So do nothing more
853 if this function has already run. */
856 if (cgraph_global_info_ready
|| !flag_unit_at_a_time
)
857 cgraph_varpool_assemble_pending_decls ();
861 cgraph_varpool_enqueue_needed_node (node
);
862 node
->finalized
= true;
864 if (decide_is_variable_needed (node
, decl
))
865 cgraph_varpool_mark_needed_node (node
);
866 /* Since we reclaim unreachable nodes at the end of every language
867 level unit, we need to be conservative about possible entry points
869 else if (TREE_PUBLIC (decl
) && !DECL_COMDAT (decl
) && !DECL_EXTERNAL (decl
))
870 cgraph_varpool_mark_needed_node (node
);
871 if (cgraph_global_info_ready
|| !flag_unit_at_a_time
)
872 cgraph_varpool_assemble_pending_decls ();
875 /* Return true when the DECL can possibly be inlined. */
877 cgraph_function_possibly_inlined_p (tree decl
)
879 if (!cgraph_global_info_ready
)
880 return (DECL_INLINE (decl
) && !flag_really_no_inline
);
881 return DECL_POSSIBLY_INLINED (decl
);
884 /* Create clone of E in the node N represented by CALL_EXPR the callgraph. */
886 cgraph_clone_edge (struct cgraph_edge
*e
, struct cgraph_node
*n
,
887 tree call_stmt
, int count_scale
, int loop_nest
,
888 bool update_original
)
890 struct cgraph_edge
*new;
892 new = cgraph_create_edge (n
, e
->callee
, call_stmt
,
893 e
->count
* count_scale
/ REG_BR_PROB_BASE
,
894 e
->loop_nest
+ loop_nest
);
896 new->inline_failed
= e
->inline_failed
;
898 e
->count
-= new->count
;
902 /* Create node representing clone of N executed COUNT times. Decrease
903 the execution counts from original node too.
905 When UPDATE_ORIGINAL is true, the counts are subtracted from the original
906 function's profile to reflect the fact that part of execution is handled
909 cgraph_clone_node (struct cgraph_node
*n
, gcov_type count
, int loop_nest
,
910 bool update_original
)
912 struct cgraph_node
*new = cgraph_create_node ();
913 struct cgraph_edge
*e
;
917 new->origin
= n
->origin
;
920 new->next_nested
= new->origin
->nested
;
921 new->origin
->nested
= new;
923 new->analyzed
= n
->analyzed
;
924 new->local
= n
->local
;
925 new->global
= n
->global
;
927 new->master_clone
= n
->master_clone
;
930 count_scale
= new->count
* REG_BR_PROB_BASE
/ n
->count
;
936 for (e
= n
->callees
;e
; e
=e
->next_callee
)
937 cgraph_clone_edge (e
, new, e
->call_stmt
, count_scale
, loop_nest
,
940 new->next_clone
= n
->next_clone
;
944 new->next_clone
->prev_clone
= new;
949 /* Return true if N is an master_clone, (see cgraph_master_clone). */
952 cgraph_is_master_clone (struct cgraph_node
*n
)
954 return (n
== cgraph_master_clone (n
));
958 cgraph_master_clone (struct cgraph_node
*n
)
960 enum availability avail
= cgraph_function_body_availability (n
);
962 if (avail
== AVAIL_NOT_AVAILABLE
|| avail
== AVAIL_OVERWRITABLE
)
965 if (!n
->master_clone
)
966 n
->master_clone
= cgraph_node (n
->decl
);
968 return n
->master_clone
;
971 /* NODE is no longer nested function; update cgraph accordingly. */
973 cgraph_unnest_node (struct cgraph_node
*node
)
975 struct cgraph_node
**node2
= &node
->origin
->nested
;
976 gcc_assert (node
->origin
);
978 while (*node2
!= node
)
979 node2
= &(*node2
)->next_nested
;
980 *node2
= node
->next_nested
;
984 /* Return function availability. See cgraph.h for description of individual
987 cgraph_function_body_availability (struct cgraph_node
*node
)
989 enum availability avail
;
990 gcc_assert (cgraph_function_flags_ready
);
992 avail
= AVAIL_NOT_AVAILABLE
;
993 else if (node
->local
.local
)
995 else if (node
->local
.externally_visible
)
996 avail
= AVAIL_AVAILABLE
;
998 /* If the function can be overwritten, return OVERWRITABLE. Take
999 care at least of two notable extensions - the COMDAT functions
1000 used to share template instantiations in C++ (this is symmetric
1001 to code cp_cannot_inline_tree_fn and probably shall be shared and
1002 the inlinability hooks completely eliminated).
1004 ??? Does the C++ one definition rule allow us to always return
1005 AVAIL_AVAILABLE here? That would be good reason to preserve this
1006 hook Similarly deal with extern inline functions - this is again
1007 necessary to get C++ shared functions having keyed templates
1008 right and in the C extension documentation we probably should
1009 document the requirement of both versions of function (extern
1010 inline and offline) having same side effect characteristics as
1011 good optimization is what this optimization is about. */
1013 else if (!(*targetm
.binds_local_p
) (node
->decl
)
1014 && !DECL_COMDAT (node
->decl
) && !DECL_EXTERNAL (node
->decl
))
1015 avail
= AVAIL_OVERWRITABLE
;
1016 else avail
= AVAIL_AVAILABLE
;
1021 /* Return variable availability. See cgraph.h for description of individual
1024 cgraph_variable_initializer_availability (struct cgraph_varpool_node
*node
)
1026 gcc_assert (cgraph_function_flags_ready
);
1027 if (!node
->finalized
)
1028 return AVAIL_NOT_AVAILABLE
;
1029 if (!TREE_PUBLIC (node
->decl
))
1030 return AVAIL_AVAILABLE
;
1031 /* If the variable can be overwritten, return OVERWRITABLE. Takes
1032 care of at least two notable extensions - the COMDAT variables
1033 used to share template instantiations in C++. */
1034 if (!(*targetm
.binds_local_p
) (node
->decl
) && !DECL_COMDAT (node
->decl
))
1035 return AVAIL_OVERWRITABLE
;
1036 return AVAIL_AVAILABLE
;
1039 #include "gt-cgraph.h"