1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009
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 /* Function inlining information. */
53 struct GTY(()) inline_summary
55 /* Estimated stack frame consumption by the function. */
56 HOST_WIDE_INT estimated_self_stack_size
;
58 /* Size of the function before inlining. */
62 /* Information about the function collected locally.
63 Available after function is analyzed. */
65 struct GTY(()) cgraph_local_info
{
66 struct inline_summary inline_summary
;
68 /* Set when function function is visible in current compilation unit only
69 and its address is never taken. */
72 /* Set when function is visible by other units. */
73 unsigned externally_visible
: 1;
75 /* Set once it has been finalized so we consider it to be output. */
76 unsigned finalized
: 1;
78 /* False when there something makes inlining impossible (such as va_arg). */
79 unsigned inlinable
: 1;
81 /* True when function should be inlined independently on its size. */
82 unsigned disregard_inline_limits
: 1;
84 /* True when the function has been originally extern inline, but it is
86 unsigned redefined_extern_inline
: 1;
88 /* True if statics_read_for_function and
89 statics_written_for_function contain valid data. */
90 unsigned for_functions_valid
: 1;
92 /* True if the function is going to be emitted in some other translation
93 unit, referenced from vtable. */
94 unsigned vtable_method
: 1;
97 /* Information about the function that needs to be computed globally
98 once compilation is finished. Available only with -funit-at-a-time. */
100 struct GTY(()) cgraph_global_info
{
101 /* Estimated stack frame consumption by the function. */
102 HOST_WIDE_INT estimated_stack_size
;
103 /* Expected offset of the stack frame of inlined function. */
104 HOST_WIDE_INT stack_frame_offset
;
106 /* For inline clones this points to the function they will be
108 struct cgraph_node
*inlined_to
;
110 /* Estimated size of the function after inlining. */
113 /* Estimated growth after inlining. INT_MIN if not computed. */
114 int estimated_growth
;
116 /* Set iff the function has been inlined at least once. */
120 /* Information about the function that is propagated by the RTL backend.
121 Available only for functions that has been already assembled. */
123 struct GTY(()) cgraph_rtl_info
{
124 unsigned int preferred_incoming_stack_boundary
;
127 /* Represent which DECL tree (or reference to such tree)
128 will be replaced by another tree while versioning. */
129 struct GTY(()) ipa_replace_map
131 /* The tree that will be replaced. */
133 /* The new (replacing) tree. */
135 /* True when a substitution should be done, false otherwise. */
137 /* True when we replace a reference to old_tree. */
140 typedef struct ipa_replace_map
*ipa_replace_map_p
;
141 DEF_VEC_P(ipa_replace_map_p
);
142 DEF_VEC_ALLOC_P(ipa_replace_map_p
,gc
);
144 struct GTY(()) cgraph_clone_info
146 VEC(ipa_replace_map_p
,gc
)* tree_map
;
150 /* The cgraph data structure.
151 Each function decl has assigned cgraph_node listing callees and callers. */
153 struct GTY((chain_next ("%h.next"), chain_prev ("%h.previous"))) cgraph_node
{
155 struct cgraph_edge
*callees
;
156 struct cgraph_edge
*callers
;
157 struct cgraph_node
*next
;
158 struct cgraph_node
*previous
;
159 /* For nested functions points to function the node is nested in. */
160 struct cgraph_node
*origin
;
161 /* Points to first nested function, if any. */
162 struct cgraph_node
*nested
;
163 /* Pointer to the next function with same origin, if any. */
164 struct cgraph_node
*next_nested
;
165 /* Pointer to the next function in cgraph_nodes_queue. */
166 struct cgraph_node
*next_needed
;
167 /* Pointer to the next clone. */
168 struct cgraph_node
*next_sibling_clone
;
169 struct cgraph_node
*prev_sibling_clone
;
170 struct cgraph_node
*clones
;
171 struct cgraph_node
*clone_of
;
172 /* For functions with many calls sites it holds map from call expression
173 to the edge to speed up cgraph_edge function. */
174 htab_t
GTY((param_is (struct cgraph_edge
))) call_site_hash
;
176 PTR
GTY ((skip
)) aux
;
178 struct cgraph_local_info local
;
179 struct cgraph_global_info global
;
180 struct cgraph_rtl_info rtl
;
181 struct cgraph_clone_info clone
;
183 /* Expected number of executions: calculated in profile.c. */
185 /* Unique id of the node. */
187 /* Ordering of all cgraph nodes. */
190 /* unique id for profiling. pid is not suitable because of different
191 number of cfg nodes with -fprofile-generate and -fprofile-use */
194 /* Set when function must be output - it is externally visible
195 or its address is taken. */
197 /* Set when function has address taken. */
198 unsigned address_taken
: 1;
199 /* Set when decl is an abstract function pointed to by the
200 ABSTRACT_DECL_ORIGIN of a reachable function. */
201 unsigned abstract_and_needed
: 1;
202 /* Set when function is reachable by call from other function
203 that is either reachable or needed. */
204 unsigned reachable
: 1;
205 /* Set once the function is lowered (i.e. its CFG is built). */
206 unsigned lowered
: 1;
207 /* Set once the function has been instantiated and its callee
209 unsigned analyzed
: 1;
210 /* Set when function is scheduled to be processed by local passes. */
211 unsigned process
: 1;
212 /* Set for aliases once they got through assemble_alias. */
215 /* In non-unit-at-a-time mode the function body of inline candidates is saved
216 into clone before compiling so the function in original form can be
217 inlined later. This pointer points to the clone. */
221 typedef struct cgraph_node
*cgraph_node_ptr
;
223 DEF_VEC_P(cgraph_node_ptr
);
224 DEF_VEC_ALLOC_P(cgraph_node_ptr
,heap
);
225 DEF_VEC_ALLOC_P(cgraph_node_ptr
,gc
);
227 /* A cgraph node set is a collection of cgraph nodes. A cgraph node
228 can appear in multiple sets. */
229 struct GTY(()) cgraph_node_set_def
231 htab_t
GTY((param_is (struct cgraph_node_set_element_def
))) hashtab
;
232 VEC(cgraph_node_ptr
, gc
) *nodes
;
233 PTR
GTY ((skip
)) aux
;
236 typedef struct cgraph_node_set_def
*cgraph_node_set
;
238 DEF_VEC_P(cgraph_node_set
);
239 DEF_VEC_ALLOC_P(cgraph_node_set
,gc
);
240 DEF_VEC_ALLOC_P(cgraph_node_set
,heap
);
242 /* A cgraph node set element contains an index in the vector of nodes in
244 struct GTY(()) cgraph_node_set_element_def
246 struct cgraph_node
*node
;
250 typedef struct cgraph_node_set_element_def
*cgraph_node_set_element
;
251 typedef const struct cgraph_node_set_element_def
*const_cgraph_node_set_element
;
253 /* Iterator structure for cgraph node sets. */
258 } cgraph_node_set_iterator
;
260 #define DEFCIFCODE(code, string) CIF_ ## code,
261 /* Reasons for inlining failures. */
263 #include "cif-code.def"
265 } cgraph_inline_failed_t
;
267 struct GTY((chain_next ("%h.next_caller"), chain_prev ("%h.prev_caller"))) cgraph_edge
{
268 struct cgraph_node
*caller
;
269 struct cgraph_node
*callee
;
270 struct cgraph_edge
*prev_caller
;
271 struct cgraph_edge
*next_caller
;
272 struct cgraph_edge
*prev_callee
;
273 struct cgraph_edge
*next_callee
;
275 PTR
GTY ((skip (""))) aux
;
276 /* When equal to CIF_OK, inline this call. Otherwise, points to the
277 explanation why function was not inlined. */
278 cgraph_inline_failed_t inline_failed
;
279 /* Expected number of executions: calculated in profile.c. */
281 /* Expected frequency of executions within the function.
282 When set to CGRAPH_FREQ_BASE, the edge is expected to be called once
283 per function call. The range is 0 to CGRAPH_FREQ_MAX. */
285 /* Depth of loop nest, 1 means no loop nest. */
286 unsigned int loop_nest
: 30;
287 /* Whether this edge describes a call that was originally indirect. */
288 unsigned int indirect_call
: 1;
289 /* Can this call throw externally? */
290 unsigned int can_throw_external
: 1;
291 /* Unique id of the edge. */
295 #define CGRAPH_FREQ_BASE 1000
296 #define CGRAPH_FREQ_MAX 100000
298 typedef struct cgraph_edge
*cgraph_edge_p
;
300 DEF_VEC_P(cgraph_edge_p
);
301 DEF_VEC_ALLOC_P(cgraph_edge_p
,heap
);
303 /* The varpool data structure.
304 Each static variable decl has assigned varpool_node. */
306 struct GTY((chain_next ("%h.next"))) varpool_node
{
308 /* Pointer to the next function in varpool_nodes. */
309 struct varpool_node
*next
;
310 /* Pointer to the next function in varpool_nodes_queue. */
311 struct varpool_node
*next_needed
;
312 /* Ordering of all cgraph nodes. */
315 /* Set when function must be output - it is externally visible
316 or its address is taken. */
318 /* Needed variables might become dead by optimization. This flag
319 forces the variable to be output even if it appears dead otherwise. */
320 unsigned force_output
: 1;
321 /* Set once the variable has been instantiated and its callee
323 unsigned analyzed
: 1;
324 /* Set once it has been finalized so we consider it to be output. */
325 unsigned finalized
: 1;
326 /* Set when variable is scheduled to be assembled. */
328 /* Set when function is visible by other units. */
329 unsigned externally_visible
: 1;
330 /* Set for aliases once they got through assemble_alias. */
334 /* Every top level asm statement is put into a cgraph_asm_node. */
336 struct GTY(()) cgraph_asm_node
{
338 struct cgraph_asm_node
*next
;
339 /* String for this asm node. */
341 /* Ordering of all cgraph nodes. */
345 extern GTY(()) struct cgraph_node
*cgraph_nodes
;
346 extern GTY(()) int cgraph_n_nodes
;
347 extern GTY(()) int cgraph_max_uid
;
348 extern GTY(()) int cgraph_edge_max_uid
;
349 extern GTY(()) int cgraph_max_pid
;
350 extern bool cgraph_global_info_ready
;
353 /* Callgraph is being constructed. It is safe to add new functions. */
354 CGRAPH_STATE_CONSTRUCTION
,
355 /* Callgraph is built and IPA passes are being run. */
357 /* Callgraph is built and all functions are transformed to SSA form. */
358 CGRAPH_STATE_IPA_SSA
,
359 /* Functions are now ordered and being passed to RTL expanders. */
360 CGRAPH_STATE_EXPANSION
,
361 /* All cgraph expansion is done. */
362 CGRAPH_STATE_FINISHED
364 extern enum cgraph_state cgraph_state
;
365 extern bool cgraph_function_flags_ready
;
366 extern GTY(()) struct cgraph_node
*cgraph_nodes_queue
;
367 extern GTY(()) struct cgraph_node
*cgraph_new_nodes
;
369 extern GTY(()) struct cgraph_asm_node
*cgraph_asm_nodes
;
370 extern GTY(()) int cgraph_order
;
373 void dump_cgraph (FILE *);
374 void debug_cgraph (void);
375 void dump_cgraph_node (FILE *, struct cgraph_node
*);
376 void debug_cgraph_node (struct cgraph_node
*);
377 void cgraph_insert_node_to_hashtable (struct cgraph_node
*node
);
378 void cgraph_remove_edge (struct cgraph_edge
*);
379 void cgraph_remove_node (struct cgraph_node
*);
380 void cgraph_remove_node_and_inline_clones (struct cgraph_node
*);
381 void cgraph_release_function_body (struct cgraph_node
*);
382 void cgraph_node_remove_callees (struct cgraph_node
*node
);
383 struct cgraph_edge
*cgraph_create_edge (struct cgraph_node
*,
384 struct cgraph_node
*,
385 gimple
, gcov_type
, int, int);
386 struct cgraph_node
*cgraph_node (tree
);
387 struct cgraph_node
*cgraph_node_for_asm (tree asmname
);
388 struct cgraph_edge
*cgraph_edge (struct cgraph_node
*, gimple
);
389 void cgraph_set_call_stmt (struct cgraph_edge
*, gimple
);
390 void cgraph_set_call_stmt_including_clones (struct cgraph_node
*, gimple
, gimple
);
391 void cgraph_create_edge_including_clones (struct cgraph_node
*,
392 struct cgraph_node
*,
393 gimple
, gcov_type
, int, int,
394 cgraph_inline_failed_t
);
395 void cgraph_update_edges_for_call_stmt (gimple
, tree
, gimple
);
396 struct cgraph_local_info
*cgraph_local_info (tree
);
397 struct cgraph_global_info
*cgraph_global_info (tree
);
398 struct cgraph_rtl_info
*cgraph_rtl_info (tree
);
399 const char * cgraph_node_name (struct cgraph_node
*);
400 struct cgraph_edge
* cgraph_clone_edge (struct cgraph_edge
*,
401 struct cgraph_node
*,
402 gimple
, gcov_type
, int, int, bool);
403 struct cgraph_node
* cgraph_clone_node (struct cgraph_node
*, gcov_type
, int,
406 void cgraph_redirect_edge_callee (struct cgraph_edge
*, struct cgraph_node
*);
408 struct cgraph_asm_node
*cgraph_add_asm_node (tree
);
410 bool cgraph_function_possibly_inlined_p (tree
);
411 void cgraph_unnest_node (struct cgraph_node
*);
413 enum availability
cgraph_function_body_availability (struct cgraph_node
*);
414 void cgraph_add_new_function (tree
, bool);
415 const char* cgraph_inline_failed_string (cgraph_inline_failed_t
);
416 struct cgraph_node
* cgraph_create_virtual_clone (struct cgraph_node
*old_node
,
417 VEC(cgraph_edge_p
,heap
)*,
418 VEC(ipa_replace_map_p
,gc
)* tree_map
,
419 bitmap args_to_skip
);
421 /* In cgraphunit.c */
422 void cgraph_finalize_function (tree
, bool);
423 void cgraph_mark_if_needed (tree
);
424 void cgraph_finalize_compilation_unit (void);
425 void cgraph_optimize (void);
426 void cgraph_mark_needed_node (struct cgraph_node
*);
427 void cgraph_mark_address_taken_node (struct cgraph_node
*);
428 void cgraph_mark_reachable_node (struct cgraph_node
*);
429 bool cgraph_inline_p (struct cgraph_edge
*, cgraph_inline_failed_t
*reason
);
430 bool cgraph_preserve_function_body_p (tree
);
431 void verify_cgraph (void);
432 void verify_cgraph_node (struct cgraph_node
*);
433 void cgraph_build_static_cdtor (char which
, tree body
, int priority
);
434 void cgraph_reset_static_var_maps (void);
435 void init_cgraph (void);
436 struct cgraph_node
*cgraph_function_versioning (struct cgraph_node
*,
437 VEC(cgraph_edge_p
,heap
)*,
438 VEC(ipa_replace_map_p
,gc
)*,
440 void tree_function_versioning (tree
, tree
, VEC (ipa_replace_map_p
,gc
)*, bool, bitmap
);
441 void cgraph_analyze_function (struct cgraph_node
*);
442 struct cgraph_node
*save_inline_function_body (struct cgraph_node
*);
443 void record_references_in_initializer (tree
);
444 bool cgraph_process_new_functions (void);
446 typedef void (*cgraph_edge_hook
)(struct cgraph_edge
*, void *);
447 typedef void (*cgraph_node_hook
)(struct cgraph_node
*, void *);
448 typedef void (*cgraph_2edge_hook
)(struct cgraph_edge
*, struct cgraph_edge
*,
450 typedef void (*cgraph_2node_hook
)(struct cgraph_node
*, struct cgraph_node
*,
452 struct cgraph_edge_hook_list
;
453 struct cgraph_node_hook_list
;
454 struct cgraph_2edge_hook_list
;
455 struct cgraph_2node_hook_list
;
456 struct cgraph_edge_hook_list
*cgraph_add_edge_removal_hook (cgraph_edge_hook
, void *);
457 void cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list
*);
458 struct cgraph_node_hook_list
*cgraph_add_node_removal_hook (cgraph_node_hook
,
460 void cgraph_remove_node_removal_hook (struct cgraph_node_hook_list
*);
461 struct cgraph_node_hook_list
*cgraph_add_function_insertion_hook (cgraph_node_hook
,
463 void cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list
*);
464 void cgraph_call_function_insertion_hooks (struct cgraph_node
*node
);
465 struct cgraph_2edge_hook_list
*cgraph_add_edge_duplication_hook (cgraph_2edge_hook
, void *);
466 void cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list
*);
467 struct cgraph_2node_hook_list
*cgraph_add_node_duplication_hook (cgraph_2node_hook
, void *);
468 void cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list
*);
469 void cgraph_materialize_all_clones (void);
471 /* In cgraphbuild.c */
472 unsigned int rebuild_cgraph_edges (void);
473 int compute_call_stmt_bb_frequency (tree
, basic_block bb
);
476 bool cgraph_remove_unreachable_nodes (bool, FILE *);
477 int cgraph_postorder (struct cgraph_node
**);
478 cgraph_node_set
cgraph_node_set_new (void);
479 cgraph_node_set_iterator
cgraph_node_set_find (cgraph_node_set
,
480 struct cgraph_node
*);
481 void cgraph_node_set_add (cgraph_node_set
, struct cgraph_node
*);
482 void cgraph_node_set_remove (cgraph_node_set
, struct cgraph_node
*);
483 void dump_cgraph_node_set (FILE *, cgraph_node_set
);
484 void debug_cgraph_node_set (cgraph_node_set
);
488 bool cgraph_maybe_hot_edge_p (struct cgraph_edge
*e
);
491 extern GTY(()) struct varpool_node
*varpool_nodes_queue
;
492 extern GTY(()) struct varpool_node
*varpool_nodes
;
494 struct varpool_node
*varpool_node (tree
);
495 struct varpool_node
*varpool_node_for_asm (tree asmname
);
496 void varpool_mark_needed_node (struct varpool_node
*);
497 void debug_varpool (void);
498 void dump_varpool (FILE *);
499 void dump_varpool_node (FILE *, struct varpool_node
*);
501 void varpool_finalize_decl (tree
);
502 bool decide_is_variable_needed (struct varpool_node
*, tree
);
503 enum availability
cgraph_variable_initializer_availability (struct varpool_node
*);
505 bool varpool_assemble_pending_decls (void);
506 bool varpool_assemble_decl (struct varpool_node
*node
);
507 bool varpool_analyze_pending_decls (void);
508 void varpool_remove_unreferenced_decls (void);
509 void varpool_empty_needed_queue (void);
511 /* Walk all reachable static variables. */
512 #define FOR_EACH_STATIC_VARIABLE(node) \
513 for ((node) = varpool_nodes_queue; (node); (node) = (node)->next_needed)
515 /* Return first reachable static variable with initializer. */
516 static inline struct varpool_node
*
517 varpool_first_static_initializer (void)
519 struct varpool_node
*node
;
520 for (node
= varpool_nodes_queue
; node
; node
= node
->next_needed
)
522 gcc_assert (TREE_CODE (node
->decl
) == VAR_DECL
);
523 if (DECL_INITIAL (node
->decl
))
529 /* Return next reachable static variable with initializer after NODE. */
530 static inline struct varpool_node
*
531 varpool_next_static_initializer (struct varpool_node
*node
)
533 for (node
= node
->next_needed
; node
; node
= node
->next_needed
)
535 gcc_assert (TREE_CODE (node
->decl
) == VAR_DECL
);
536 if (DECL_INITIAL (node
->decl
))
542 /* Walk all static variables with initializer set. */
543 #define FOR_EACH_STATIC_INITIALIZER(node) \
544 for ((node) = varpool_first_static_initializer (); (node); \
545 (node) = varpool_next_static_initializer (node))
547 /* In ipa-inline.c */
548 void cgraph_clone_inlined_nodes (struct cgraph_edge
*, bool, bool);
549 unsigned int compute_inline_parameters (struct cgraph_node
*);
552 /* Create a new static variable of type TYPE. */
553 tree
add_new_static_var (tree type
);
556 /* Return true if iterator CSI points to nothing. */
558 csi_end_p (cgraph_node_set_iterator csi
)
560 return csi
.index
>= VEC_length (cgraph_node_ptr
, csi
.set
->nodes
);
563 /* Advance iterator CSI. */
565 csi_next (cgraph_node_set_iterator
*csi
)
570 /* Return the node pointed to by CSI. */
571 static inline struct cgraph_node
*
572 csi_node (cgraph_node_set_iterator csi
)
574 return VEC_index (cgraph_node_ptr
, csi
.set
->nodes
, csi
.index
);
577 /* Return an iterator to the first node in SET. */
578 static inline cgraph_node_set_iterator
579 csi_start (cgraph_node_set set
)
581 cgraph_node_set_iterator csi
;
588 /* Return true if SET contains NODE. */
590 cgraph_node_in_set_p (struct cgraph_node
*node
, cgraph_node_set set
)
592 cgraph_node_set_iterator csi
;
593 csi
= cgraph_node_set_find (set
, node
);
594 return !csi_end_p (csi
);
597 /* Return number of nodes in SET. */
599 cgraph_node_set_size (cgraph_node_set set
)
601 return htab_elements (set
->hashtab
);
605 #endif /* GCC_CGRAPH_H */