1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
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/>. */
25 #include "basic-block.h"
29 /* Not yet set by cgraph_function_body_availability. */
31 /* Function body/variable initializer is unknown. */
33 /* Function body/variable initializer is known but might be replaced
34 by a different one from other compilation unit and thus needs to
35 be dealt with a care. Like AVAIL_NOT_AVAILABLE it can have
36 arbitrary side effects on escaping variables and functions, while
37 like AVAILABLE it might access static variables. */
39 /* Function body/variable initializer is known and will be used in final
42 /* Function body/variable initializer is known and all it's uses are explicitly
43 visible within current unit (ie it's address is never taken and it is not
44 exported to other units).
45 Currently used only for functions. */
49 extern const char * const cgraph_availability_names
[];
51 /* Information about the function collected locally.
52 Available after function is analyzed. */
54 struct GTY(()) cgraph_local_info
{
55 struct inline_summary
{
56 /* Estimated stack frame consumption by the function. */
57 HOST_WIDE_INT estimated_self_stack_size
;
59 /* Size of the function before inlining. */
63 /* Set when function function is visible in current compilation unit only
64 and its address is never taken. */
67 /* Set when function is visible by other units. */
68 unsigned externally_visible
: 1;
70 /* Set once it has been finalized so we consider it to be output. */
71 unsigned finalized
: 1;
73 /* False when there something makes inlining impossible (such as va_arg). */
74 unsigned inlinable
: 1;
76 /* True when function should be inlined independently on its size. */
77 unsigned disregard_inline_limits
: 1;
79 /* True when the function has been originally extern inline, but it is
81 unsigned redefined_extern_inline
: 1;
83 /* True if statics_read_for_function and
84 statics_written_for_function contain valid data. */
85 unsigned for_functions_valid
: 1;
87 /* True if the function is going to be emitted in some other translation
88 unit, referenced from vtable. */
89 unsigned vtable_method
: 1;
92 /* Information about the function that needs to be computed globally
93 once compilation is finished. Available only with -funit-at-a-time. */
95 struct GTY(()) cgraph_global_info
{
96 /* Estimated stack frame consumption by the function. */
97 HOST_WIDE_INT estimated_stack_size
;
98 /* Expected offset of the stack frame of inlined function. */
99 HOST_WIDE_INT stack_frame_offset
;
101 /* For inline clones this points to the function they will be
103 struct cgraph_node
*inlined_to
;
105 /* Estimated size of the function after inlining. */
108 /* Estimated growth after inlining. INT_MIN if not computed. */
109 int estimated_growth
;
111 /* Set iff the function has been inlined at least once. */
115 /* Information about the function that is propagated by the RTL backend.
116 Available only for functions that has been already assembled. */
118 struct GTY(()) cgraph_rtl_info
{
119 unsigned int preferred_incoming_stack_boundary
;
122 /* Represent which DECL tree (or reference to such tree)
123 will be replaced by another tree while versioning. */
124 struct GTY(()) ipa_replace_map
126 /* The tree that will be replaced. */
128 /* The new (replacing) tree. */
130 /* True when a substitution should be done, false otherwise. */
132 /* True when we replace a reference to old_tree. */
135 typedef struct ipa_replace_map
*ipa_replace_map_p
;
136 DEF_VEC_P(ipa_replace_map_p
);
137 DEF_VEC_ALLOC_P(ipa_replace_map_p
,gc
);
139 struct GTY(()) cgraph_clone_info
141 VEC(ipa_replace_map_p
,gc
)* tree_map
;
145 /* The cgraph data structure.
146 Each function decl has assigned cgraph_node listing callees and callers. */
148 struct GTY((chain_next ("%h.next"), chain_prev ("%h.previous"))) cgraph_node
{
150 struct cgraph_edge
*callees
;
151 struct cgraph_edge
*callers
;
152 struct cgraph_node
*next
;
153 struct cgraph_node
*previous
;
154 /* For nested functions points to function the node is nested in. */
155 struct cgraph_node
*origin
;
156 /* Points to first nested function, if any. */
157 struct cgraph_node
*nested
;
158 /* Pointer to the next function with same origin, if any. */
159 struct cgraph_node
*next_nested
;
160 /* Pointer to the next function in cgraph_nodes_queue. */
161 struct cgraph_node
*next_needed
;
162 /* Pointer to the next clone. */
163 struct cgraph_node
*next_sibling_clone
;
164 struct cgraph_node
*prev_sibling_clone
;
165 struct cgraph_node
*clones
;
166 struct cgraph_node
*clone_of
;
167 /* For functions with many calls sites it holds map from call expression
168 to the edge to speed up cgraph_edge function. */
169 htab_t
GTY((param_is (struct cgraph_edge
))) call_site_hash
;
171 PTR
GTY ((skip
)) aux
;
173 struct cgraph_local_info local
;
174 struct cgraph_global_info global
;
175 struct cgraph_rtl_info rtl
;
176 struct cgraph_clone_info clone
;
178 /* Expected number of executions: calculated in profile.c. */
180 /* Unique id of the node. */
182 /* Ordering of all cgraph nodes. */
185 /* unique id for profiling. pid is not suitable because of different
186 number of cfg nodes with -fprofile-generate and -fprofile-use */
189 /* Set when function must be output - it is externally visible
190 or its address is taken. */
192 /* Set when function has address taken. */
193 unsigned address_taken
: 1;
194 /* Set when decl is an abstract function pointed to by the
195 ABSTRACT_DECL_ORIGIN of a reachable function. */
196 unsigned abstract_and_needed
: 1;
197 /* Set when function is reachable by call from other function
198 that is either reachable or needed. */
199 unsigned reachable
: 1;
200 /* Set once the function is lowered (i.e. its CFG is built). */
201 unsigned lowered
: 1;
202 /* Set once the function has been instantiated and its callee
204 unsigned analyzed
: 1;
205 /* Set when function is scheduled to be processed by local passes. */
206 unsigned process
: 1;
207 /* Set for aliases once they got through assemble_alias. */
210 /* In non-unit-at-a-time mode the function body of inline candidates is saved
211 into clone before compiling so the function in original form can be
212 inlined later. This pointer points to the clone. */
216 typedef struct cgraph_node
*cgraph_node_ptr
;
218 DEF_VEC_P(cgraph_node_ptr
);
219 DEF_VEC_ALLOC_P(cgraph_node_ptr
,heap
);
220 DEF_VEC_ALLOC_P(cgraph_node_ptr
,gc
);
222 /* A cgraph node set is a collection of cgraph nodes. A cgraph node
223 can appear in multiple sets. */
224 struct GTY(()) cgraph_node_set_def
226 htab_t
GTY((param_is (struct cgraph_node_set_element_def
))) hashtab
;
227 VEC(cgraph_node_ptr
, gc
) *nodes
;
228 PTR
GTY ((skip
)) aux
;
231 typedef struct cgraph_node_set_def
*cgraph_node_set
;
233 DEF_VEC_P(cgraph_node_set
);
234 DEF_VEC_ALLOC_P(cgraph_node_set
,gc
);
235 DEF_VEC_ALLOC_P(cgraph_node_set
,heap
);
237 /* A cgraph node set element contains an index in the vector of nodes in
239 struct GTY(()) cgraph_node_set_element_def
241 struct cgraph_node
*node
;
245 typedef struct cgraph_node_set_element_def
*cgraph_node_set_element
;
246 typedef const struct cgraph_node_set_element_def
*const_cgraph_node_set_element
;
248 /* Iterator structure for cgraph node sets. */
253 } cgraph_node_set_iterator
;
255 #define DEFCIFCODE(code, string) CIF_ ## code,
256 /* Reasons for inlining failures. */
258 #include "cif-code.def"
260 } cgraph_inline_failed_t
;
262 struct GTY((chain_next ("%h.next_caller"), chain_prev ("%h.prev_caller"))) cgraph_edge
{
263 struct cgraph_node
*caller
;
264 struct cgraph_node
*callee
;
265 struct cgraph_edge
*prev_caller
;
266 struct cgraph_edge
*next_caller
;
267 struct cgraph_edge
*prev_callee
;
268 struct cgraph_edge
*next_callee
;
270 PTR
GTY ((skip (""))) aux
;
271 /* When equal to CIF_OK, inline this call. Otherwise, points to the
272 explanation why function was not inlined. */
273 cgraph_inline_failed_t inline_failed
;
274 /* Expected number of executions: calculated in profile.c. */
276 /* Expected frequency of executions within the function.
277 When set to CGRAPH_FREQ_BASE, the edge is expected to be called once
278 per function call. The range is 0 to CGRAPH_FREQ_MAX. */
280 /* Depth of loop nest, 1 means no loop nest. */
281 unsigned int loop_nest
: 30;
282 /* Whether this edge describes a call that was originally indirect. */
283 unsigned int indirect_call
: 1;
284 /* Can this call throw externally? */
285 unsigned int can_throw_external
: 1;
286 /* Unique id of the edge. */
290 #define CGRAPH_FREQ_BASE 1000
291 #define CGRAPH_FREQ_MAX 100000
293 typedef struct cgraph_edge
*cgraph_edge_p
;
295 DEF_VEC_P(cgraph_edge_p
);
296 DEF_VEC_ALLOC_P(cgraph_edge_p
,heap
);
298 /* The varpool data structure.
299 Each static variable decl has assigned varpool_node. */
301 struct GTY((chain_next ("%h.next"))) varpool_node
{
303 /* Pointer to the next function in varpool_nodes. */
304 struct varpool_node
*next
;
305 /* Pointer to the next function in varpool_nodes_queue. */
306 struct varpool_node
*next_needed
;
307 /* Ordering of all cgraph nodes. */
310 /* Set when function must be output - it is externally visible
311 or its address is taken. */
313 /* Needed variables might become dead by optimization. This flag
314 forces the variable to be output even if it appears dead otherwise. */
315 unsigned force_output
: 1;
316 /* Set once the variable has been instantiated and its callee
318 unsigned analyzed
: 1;
319 /* Set once it has been finalized so we consider it to be output. */
320 unsigned finalized
: 1;
321 /* Set when variable is scheduled to be assembled. */
323 /* Set when function is visible by other units. */
324 unsigned externally_visible
: 1;
325 /* Set for aliases once they got through assemble_alias. */
329 /* Every top level asm statement is put into a cgraph_asm_node. */
331 struct GTY(()) cgraph_asm_node
{
333 struct cgraph_asm_node
*next
;
334 /* String for this asm node. */
336 /* Ordering of all cgraph nodes. */
340 extern GTY(()) struct cgraph_node
*cgraph_nodes
;
341 extern GTY(()) int cgraph_n_nodes
;
342 extern GTY(()) int cgraph_max_uid
;
343 extern GTY(()) int cgraph_edge_max_uid
;
344 extern GTY(()) int cgraph_max_pid
;
345 extern bool cgraph_global_info_ready
;
348 /* Callgraph is being constructed. It is safe to add new functions. */
349 CGRAPH_STATE_CONSTRUCTION
,
350 /* Callgraph is built and IPA passes are being run. */
352 /* Callgraph is built and all functions are transformed to SSA form. */
353 CGRAPH_STATE_IPA_SSA
,
354 /* Functions are now ordered and being passed to RTL expanders. */
355 CGRAPH_STATE_EXPANSION
,
356 /* All cgraph expansion is done. */
357 CGRAPH_STATE_FINISHED
359 extern enum cgraph_state cgraph_state
;
360 extern bool cgraph_function_flags_ready
;
361 extern GTY(()) struct cgraph_node
*cgraph_nodes_queue
;
362 extern GTY(()) struct cgraph_node
*cgraph_new_nodes
;
364 extern GTY(()) struct cgraph_asm_node
*cgraph_asm_nodes
;
365 extern GTY(()) int cgraph_order
;
368 void dump_cgraph (FILE *);
369 void debug_cgraph (void);
370 void dump_cgraph_node (FILE *, struct cgraph_node
*);
371 void debug_cgraph_node (struct cgraph_node
*);
372 void cgraph_insert_node_to_hashtable (struct cgraph_node
*node
);
373 void cgraph_remove_edge (struct cgraph_edge
*);
374 void cgraph_remove_node (struct cgraph_node
*);
375 void cgraph_remove_node_and_inline_clones (struct cgraph_node
*);
376 void cgraph_release_function_body (struct cgraph_node
*);
377 void cgraph_node_remove_callees (struct cgraph_node
*node
);
378 struct cgraph_edge
*cgraph_create_edge (struct cgraph_node
*,
379 struct cgraph_node
*,
380 gimple
, gcov_type
, int, int);
381 struct cgraph_node
*cgraph_node (tree
);
382 struct cgraph_node
*cgraph_node_for_asm (tree asmname
);
383 struct cgraph_edge
*cgraph_edge (struct cgraph_node
*, gimple
);
384 void cgraph_set_call_stmt (struct cgraph_edge
*, gimple
);
385 void cgraph_set_call_stmt_including_clones (struct cgraph_node
*, gimple
, gimple
);
386 void cgraph_create_edge_including_clones (struct cgraph_node
*,
387 struct cgraph_node
*,
388 gimple
, gcov_type
, int, int,
389 cgraph_inline_failed_t
);
390 void cgraph_update_edges_for_call_stmt (gimple
, tree
, gimple
);
391 struct cgraph_local_info
*cgraph_local_info (tree
);
392 struct cgraph_global_info
*cgraph_global_info (tree
);
393 struct cgraph_rtl_info
*cgraph_rtl_info (tree
);
394 const char * cgraph_node_name (struct cgraph_node
*);
395 struct cgraph_edge
* cgraph_clone_edge (struct cgraph_edge
*,
396 struct cgraph_node
*,
397 gimple
, gcov_type
, int, int, bool);
398 struct cgraph_node
* cgraph_clone_node (struct cgraph_node
*, gcov_type
, int,
401 void cgraph_redirect_edge_callee (struct cgraph_edge
*, struct cgraph_node
*);
403 struct cgraph_asm_node
*cgraph_add_asm_node (tree
);
405 bool cgraph_function_possibly_inlined_p (tree
);
406 void cgraph_unnest_node (struct cgraph_node
*);
408 enum availability
cgraph_function_body_availability (struct cgraph_node
*);
409 void cgraph_add_new_function (tree
, bool);
410 const char* cgraph_inline_failed_string (cgraph_inline_failed_t
);
411 struct cgraph_node
* cgraph_create_virtual_clone (struct cgraph_node
*old_node
,
412 VEC(cgraph_edge_p
,heap
)*,
413 VEC(ipa_replace_map_p
,gc
)* tree_map
,
414 bitmap args_to_skip
);
416 /* In cgraphunit.c */
417 void cgraph_finalize_function (tree
, bool);
418 void cgraph_mark_if_needed (tree
);
419 void cgraph_finalize_compilation_unit (void);
420 void cgraph_optimize (void);
421 void cgraph_mark_needed_node (struct cgraph_node
*);
422 void cgraph_mark_address_taken_node (struct cgraph_node
*);
423 void cgraph_mark_reachable_node (struct cgraph_node
*);
424 bool cgraph_inline_p (struct cgraph_edge
*, cgraph_inline_failed_t
*reason
);
425 bool cgraph_preserve_function_body_p (tree
);
426 void verify_cgraph (void);
427 void verify_cgraph_node (struct cgraph_node
*);
428 void cgraph_build_static_cdtor (char which
, tree body
, int priority
);
429 void cgraph_reset_static_var_maps (void);
430 void init_cgraph (void);
431 struct cgraph_node
*cgraph_function_versioning (struct cgraph_node
*,
432 VEC(cgraph_edge_p
,heap
)*,
433 VEC(ipa_replace_map_p
,gc
)*,
435 void tree_function_versioning (tree
, tree
, VEC (ipa_replace_map_p
,gc
)*, bool, bitmap
);
436 void cgraph_analyze_function (struct cgraph_node
*);
437 struct cgraph_node
*save_inline_function_body (struct cgraph_node
*);
438 void record_references_in_initializer (tree
);
439 bool cgraph_process_new_functions (void);
441 typedef void (*cgraph_edge_hook
)(struct cgraph_edge
*, void *);
442 typedef void (*cgraph_node_hook
)(struct cgraph_node
*, void *);
443 typedef void (*cgraph_2edge_hook
)(struct cgraph_edge
*, struct cgraph_edge
*,
445 typedef void (*cgraph_2node_hook
)(struct cgraph_node
*, struct cgraph_node
*,
447 struct cgraph_edge_hook_list
;
448 struct cgraph_node_hook_list
;
449 struct cgraph_2edge_hook_list
;
450 struct cgraph_2node_hook_list
;
451 struct cgraph_edge_hook_list
*cgraph_add_edge_removal_hook (cgraph_edge_hook
, void *);
452 void cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list
*);
453 struct cgraph_node_hook_list
*cgraph_add_node_removal_hook (cgraph_node_hook
,
455 void cgraph_remove_node_removal_hook (struct cgraph_node_hook_list
*);
456 struct cgraph_node_hook_list
*cgraph_add_function_insertion_hook (cgraph_node_hook
,
458 void cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list
*);
459 void cgraph_call_function_insertion_hooks (struct cgraph_node
*node
);
460 struct cgraph_2edge_hook_list
*cgraph_add_edge_duplication_hook (cgraph_2edge_hook
, void *);
461 void cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list
*);
462 struct cgraph_2node_hook_list
*cgraph_add_node_duplication_hook (cgraph_2node_hook
, void *);
463 void cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list
*);
464 void cgraph_materialize_all_clones (void);
466 /* In cgraphbuild.c */
467 unsigned int rebuild_cgraph_edges (void);
468 int compute_call_stmt_bb_frequency (tree
, basic_block bb
);
471 bool cgraph_remove_unreachable_nodes (bool, FILE *);
472 int cgraph_postorder (struct cgraph_node
**);
473 cgraph_node_set
cgraph_node_set_new (void);
474 cgraph_node_set_iterator
cgraph_node_set_find (cgraph_node_set
,
475 struct cgraph_node
*);
476 void cgraph_node_set_add (cgraph_node_set
, struct cgraph_node
*);
477 void cgraph_node_set_remove (cgraph_node_set
, struct cgraph_node
*);
478 void dump_cgraph_node_set (FILE *, cgraph_node_set
);
479 void debug_cgraph_node_set (cgraph_node_set
);
483 bool cgraph_maybe_hot_edge_p (struct cgraph_edge
*e
);
486 extern GTY(()) struct varpool_node
*varpool_nodes_queue
;
487 extern GTY(()) struct varpool_node
*varpool_nodes
;
489 struct varpool_node
*varpool_node (tree
);
490 struct varpool_node
*varpool_node_for_asm (tree asmname
);
491 void varpool_mark_needed_node (struct varpool_node
*);
492 void debug_varpool (void);
493 void dump_varpool (FILE *);
494 void dump_varpool_node (FILE *, struct varpool_node
*);
496 void varpool_finalize_decl (tree
);
497 bool decide_is_variable_needed (struct varpool_node
*, tree
);
498 enum availability
cgraph_variable_initializer_availability (struct varpool_node
*);
500 bool varpool_assemble_pending_decls (void);
501 bool varpool_assemble_decl (struct varpool_node
*node
);
502 bool varpool_analyze_pending_decls (void);
503 void varpool_remove_unreferenced_decls (void);
504 void varpool_empty_needed_queue (void);
506 /* Walk all reachable static variables. */
507 #define FOR_EACH_STATIC_VARIABLE(node) \
508 for ((node) = varpool_nodes_queue; (node); (node) = (node)->next_needed)
510 /* Return first reachable static variable with initializer. */
511 static inline struct varpool_node
*
512 varpool_first_static_initializer (void)
514 struct varpool_node
*node
;
515 for (node
= varpool_nodes_queue
; node
; node
= node
->next_needed
)
517 gcc_assert (TREE_CODE (node
->decl
) == VAR_DECL
);
518 if (DECL_INITIAL (node
->decl
))
524 /* Return next reachable static variable with initializer after NODE. */
525 static inline struct varpool_node
*
526 varpool_next_static_initializer (struct varpool_node
*node
)
528 for (node
= node
->next_needed
; node
; node
= node
->next_needed
)
530 gcc_assert (TREE_CODE (node
->decl
) == VAR_DECL
);
531 if (DECL_INITIAL (node
->decl
))
537 /* Walk all static variables with initializer set. */
538 #define FOR_EACH_STATIC_INITIALIZER(node) \
539 for ((node) = varpool_first_static_initializer (); (node); \
540 (node) = varpool_next_static_initializer (node))
542 /* In ipa-inline.c */
543 void cgraph_clone_inlined_nodes (struct cgraph_edge
*, bool, bool);
544 unsigned int compute_inline_parameters (struct cgraph_node
*);
547 /* Create a new static variable of type TYPE. */
548 tree
add_new_static_var (tree type
);
551 /* Return true if iterator CSI points to nothing. */
553 csi_end_p (cgraph_node_set_iterator csi
)
555 return csi
.index
>= VEC_length (cgraph_node_ptr
, csi
.set
->nodes
);
558 /* Advance iterator CSI. */
560 csi_next (cgraph_node_set_iterator
*csi
)
565 /* Return the node pointed to by CSI. */
566 static inline struct cgraph_node
*
567 csi_node (cgraph_node_set_iterator csi
)
569 return VEC_index (cgraph_node_ptr
, csi
.set
->nodes
, csi
.index
);
572 /* Return an iterator to the first node in SET. */
573 static inline cgraph_node_set_iterator
574 csi_start (cgraph_node_set set
)
576 cgraph_node_set_iterator csi
;
583 /* Return true if SET contains NODE. */
585 cgraph_node_in_set_p (struct cgraph_node
*node
, cgraph_node_set set
)
587 cgraph_node_set_iterator csi
;
588 csi
= cgraph_node_set_find (set
, node
);
589 return !csi_end_p (csi
);
592 /* Return number of nodes in SET. */
594 cgraph_node_set_size (cgraph_node_set set
)
596 return htab_elements (set
->hashtab
);
600 #endif /* GCC_CGRAPH_H */