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 cgraph_local_info
GTY(())
56 struct inline_summary
{
57 /* Estimated stack frame consumption by the function. */
58 HOST_WIDE_INT estimated_self_stack_size
;
60 /* Size of the function before inlining. */
64 /* Set when function function is visible in current compilation unit only
65 and its address is never taken. */
68 /* Set when function is visible by other units. */
69 unsigned externally_visible
: 1;
71 /* Set once it has been finalized so we consider it to be output. */
72 unsigned finalized
: 1;
74 /* False when there something makes inlining impossible (such as va_arg). */
75 unsigned inlinable
: 1;
77 /* True when function should be inlined independently on its size. */
78 unsigned disregard_inline_limits
: 1;
80 /* True when the function has been originally extern inline, but it is
82 unsigned redefined_extern_inline
: 1;
84 /* True if statics_read_for_function and
85 statics_written_for_function contain valid data. */
86 unsigned for_functions_valid
: 1;
88 /* True if the function is going to be emitted in some other translation
89 unit, referenced from vtable. */
90 unsigned vtable_method
: 1;
93 /* Information about the function that needs to be computed globally
94 once compilation is finished. Available only with -funit-at-a-time. */
96 struct cgraph_global_info
GTY(())
98 /* Estimated stack frame consumption by the function. */
99 HOST_WIDE_INT estimated_stack_size
;
100 /* Expected offset of the stack frame of inlined function. */
101 HOST_WIDE_INT stack_frame_offset
;
103 /* For inline clones this points to the function they will be
105 struct cgraph_node
*inlined_to
;
107 /* Estimated size of the function after inlining. */
110 /* Estimated growth after inlining. INT_MIN if not computed. */
111 int estimated_growth
;
113 /* Set iff the function has been inlined at least once. */
117 /* Information about the function that is propagated by the RTL backend.
118 Available only for functions that has been already assembled. */
120 struct cgraph_rtl_info
GTY(())
122 unsigned int preferred_incoming_stack_boundary
;
125 /* The cgraph data structure.
126 Each function decl has assigned cgraph_node listing callees and callers. */
128 struct cgraph_node
GTY((chain_next ("%h.next"), chain_prev ("%h.previous")))
131 struct cgraph_edge
*callees
;
132 struct cgraph_edge
*callers
;
133 struct cgraph_node
*next
;
134 struct cgraph_node
*previous
;
135 /* For nested functions points to function the node is nested in. */
136 struct cgraph_node
*origin
;
137 /* Points to first nested function, if any. */
138 struct cgraph_node
*nested
;
139 /* Pointer to the next function with same origin, if any. */
140 struct cgraph_node
*next_nested
;
141 /* Pointer to the next function in cgraph_nodes_queue. */
142 struct cgraph_node
*next_needed
;
143 /* Pointer to the next clone. */
144 struct cgraph_node
*next_clone
;
145 struct cgraph_node
*prev_clone
;
146 /* For functions with many calls sites it holds map from call expression
147 to the edge to speed up cgraph_edge function. */
148 htab_t
GTY((param_is (struct cgraph_edge
))) call_site_hash
;
150 PTR
GTY ((skip
)) aux
;
152 struct cgraph_local_info local
;
153 struct cgraph_global_info global
;
154 struct cgraph_rtl_info rtl
;
156 /* Expected number of executions: calculated in profile.c. */
158 /* Unique id of the node. */
160 /* Ordering of all cgraph nodes. */
163 /* unique id for profiling. pid is not suitable because of different
164 number of cfg nodes with -fprofile-generate and -fprofile-use */
167 /* Set when function must be output - it is externally visible
168 or its address is taken. */
170 /* Set when decl is an abstract function pointed to by the
171 ABSTRACT_DECL_ORIGIN of a reachable function. */
172 unsigned abstract_and_needed
: 1;
173 /* Set when function is reachable by call from other function
174 that is either reachable or needed. */
175 unsigned reachable
: 1;
176 /* Set once the function is lowered (i.e. its CFG is built). */
177 unsigned lowered
: 1;
178 /* Set once the function has been instantiated and its callee
180 unsigned analyzed
: 1;
181 /* Set when function is scheduled to be processed by local passes. */
182 unsigned process
: 1;
183 /* Set for aliases once they got through assemble_alias. */
186 /* In non-unit-at-a-time mode the function body of inline candidates is saved
187 into clone before compiling so the function in original form can be
188 inlined later. This pointer points to the clone. */
192 typedef struct cgraph_node
*cgraph_node_ptr
;
194 DEF_VEC_P(cgraph_node_ptr
);
195 DEF_VEC_ALLOC_P(cgraph_node_ptr
,heap
);
196 DEF_VEC_ALLOC_P(cgraph_node_ptr
,gc
);
198 /* A cgraph node set is a collection of cgraph nodes. A cgraph node
199 can appear in multiple sets. */
200 struct cgraph_node_set_def
GTY(())
202 htab_t
GTY((param_is (struct cgraph_node_set_element_def
))) hashtab
;
203 VEC(cgraph_node_ptr
, gc
) *nodes
;
204 PTR
GTY ((skip
)) aux
;
207 typedef struct cgraph_node_set_def
*cgraph_node_set
;
209 DEF_VEC_P(cgraph_node_set
);
210 DEF_VEC_ALLOC_P(cgraph_node_set
,gc
);
211 DEF_VEC_ALLOC_P(cgraph_node_set
,heap
);
213 /* A cgraph node set element contains an index in the vector of nodes in
215 struct cgraph_node_set_element_def
GTY(())
217 struct cgraph_node
*node
;
221 typedef struct cgraph_node_set_element_def
*cgraph_node_set_element
;
222 typedef const struct cgraph_node_set_element_def
*const_cgraph_node_set_element
;
224 /* Iterator structure for cgraph node sets. */
229 } cgraph_node_set_iterator
;
231 #define DEFCIFCODE(code, string) CIF_ ## code,
232 /* Reasons for inlining failures. */
234 #include "cif-code.def"
236 } cgraph_inline_failed_t
;
238 struct cgraph_edge
GTY((chain_next ("%h.next_caller"), chain_prev ("%h.prev_caller")))
240 struct cgraph_node
*caller
;
241 struct cgraph_node
*callee
;
242 struct cgraph_edge
*prev_caller
;
243 struct cgraph_edge
*next_caller
;
244 struct cgraph_edge
*prev_callee
;
245 struct cgraph_edge
*next_callee
;
247 PTR
GTY ((skip (""))) aux
;
248 /* When equal to CIF_OK, inline this call. Otherwise, points to the
249 explanation why function was not inlined. */
250 cgraph_inline_failed_t inline_failed
;
251 /* Expected number of executions: calculated in profile.c. */
253 /* Expected frequency of executions within the function.
254 When set to CGRAPH_FREQ_BASE, the edge is expected to be called once
255 per function call. The range is 0 to CGRAPH_FREQ_MAX. */
257 /* Depth of loop nest, 1 means no loop nest. */
258 unsigned int loop_nest
: 30;
259 /* Whether this edge describes a call that was originally indirect. */
260 unsigned int indirect_call
: 1;
261 /* Can this call throw externally? */
262 unsigned int can_throw_external
: 1;
263 /* Unique id of the edge. */
267 #define CGRAPH_FREQ_BASE 1000
268 #define CGRAPH_FREQ_MAX 100000
270 typedef struct cgraph_edge
*cgraph_edge_p
;
272 DEF_VEC_P(cgraph_edge_p
);
273 DEF_VEC_ALLOC_P(cgraph_edge_p
,heap
);
275 /* The varpool data structure.
276 Each static variable decl has assigned varpool_node. */
278 struct varpool_node
GTY((chain_next ("%h.next")))
281 /* Pointer to the next function in varpool_nodes. */
282 struct varpool_node
*next
;
283 /* Pointer to the next function in varpool_nodes_queue. */
284 struct varpool_node
*next_needed
;
285 /* Ordering of all cgraph nodes. */
288 /* Set when function must be output - it is externally visible
289 or its address is taken. */
291 /* Needed variables might become dead by optimization. This flag
292 forces the variable to be output even if it appears dead otherwise. */
293 unsigned force_output
: 1;
294 /* Set once the variable has been instantiated and its callee
296 unsigned analyzed
: 1;
297 /* Set once it has been finalized so we consider it to be output. */
298 unsigned finalized
: 1;
299 /* Set when variable is scheduled to be assembled. */
301 /* Set when function is visible by other units. */
302 unsigned externally_visible
: 1;
303 /* Set for aliases once they got through assemble_alias. */
307 /* Every top level asm statement is put into a cgraph_asm_node. */
309 struct cgraph_asm_node
GTY(())
312 struct cgraph_asm_node
*next
;
313 /* String for this asm node. */
315 /* Ordering of all cgraph nodes. */
319 extern GTY(()) struct cgraph_node
*cgraph_nodes
;
320 extern GTY(()) int cgraph_n_nodes
;
321 extern GTY(()) int cgraph_max_uid
;
322 extern GTY(()) int cgraph_edge_max_uid
;
323 extern GTY(()) int cgraph_max_pid
;
324 extern bool cgraph_global_info_ready
;
327 /* Callgraph is being constructed. It is safe to add new functions. */
328 CGRAPH_STATE_CONSTRUCTION
,
329 /* Callgraph is built and IPA passes are being run. */
331 /* Callgraph is built and all functions are transformed to SSA form. */
332 CGRAPH_STATE_IPA_SSA
,
333 /* Functions are now ordered and being passed to RTL expanders. */
334 CGRAPH_STATE_EXPANSION
,
335 /* All cgraph expansion is done. */
336 CGRAPH_STATE_FINISHED
338 extern enum cgraph_state cgraph_state
;
339 extern bool cgraph_function_flags_ready
;
340 extern GTY(()) struct cgraph_node
*cgraph_nodes_queue
;
341 extern GTY(()) struct cgraph_node
*cgraph_new_nodes
;
343 extern GTY(()) struct cgraph_asm_node
*cgraph_asm_nodes
;
344 extern GTY(()) int cgraph_order
;
347 void dump_cgraph (FILE *);
348 void debug_cgraph (void);
349 void dump_cgraph_node (FILE *, struct cgraph_node
*);
350 void debug_cgraph_node (struct cgraph_node
*);
351 void cgraph_insert_node_to_hashtable (struct cgraph_node
*node
);
352 void cgraph_remove_edge (struct cgraph_edge
*);
353 void cgraph_remove_node (struct cgraph_node
*);
354 void cgraph_release_function_body (struct cgraph_node
*);
355 void cgraph_node_remove_callees (struct cgraph_node
*node
);
356 struct cgraph_edge
*cgraph_create_edge (struct cgraph_node
*,
357 struct cgraph_node
*,
358 gimple
, gcov_type
, int, int);
359 struct cgraph_node
*cgraph_node (tree
);
360 struct cgraph_node
*cgraph_node_for_asm (tree asmname
);
361 struct cgraph_edge
*cgraph_edge (struct cgraph_node
*, gimple
);
362 void cgraph_set_call_stmt (struct cgraph_edge
*, gimple
);
363 void cgraph_update_edges_for_call_stmt (gimple
, gimple
);
364 struct cgraph_local_info
*cgraph_local_info (tree
);
365 struct cgraph_global_info
*cgraph_global_info (tree
);
366 struct cgraph_rtl_info
*cgraph_rtl_info (tree
);
367 const char * cgraph_node_name (struct cgraph_node
*);
368 struct cgraph_edge
* cgraph_clone_edge (struct cgraph_edge
*,
369 struct cgraph_node
*,
370 gimple
, gcov_type
, int, int, bool);
371 struct cgraph_node
* cgraph_clone_node (struct cgraph_node
*, gcov_type
, int,
374 void cgraph_redirect_edge_callee (struct cgraph_edge
*, struct cgraph_node
*);
376 struct cgraph_asm_node
*cgraph_add_asm_node (tree
);
378 bool cgraph_function_possibly_inlined_p (tree
);
379 void cgraph_unnest_node (struct cgraph_node
*);
381 enum availability
cgraph_function_body_availability (struct cgraph_node
*);
382 void cgraph_add_new_function (tree
, bool);
383 const char* cgraph_inline_failed_string (cgraph_inline_failed_t
);
385 /* In cgraphunit.c */
386 void cgraph_finalize_function (tree
, bool);
387 void cgraph_mark_if_needed (tree
);
388 void cgraph_finalize_compilation_unit (void);
389 void cgraph_optimize (void);
390 void cgraph_mark_needed_node (struct cgraph_node
*);
391 void cgraph_mark_reachable_node (struct cgraph_node
*);
392 bool cgraph_inline_p (struct cgraph_edge
*, cgraph_inline_failed_t
*reason
);
393 bool cgraph_preserve_function_body_p (tree
);
394 void verify_cgraph (void);
395 void verify_cgraph_node (struct cgraph_node
*);
396 void cgraph_build_static_cdtor (char which
, tree body
, int priority
);
397 void cgraph_reset_static_var_maps (void);
398 void init_cgraph (void);
399 struct cgraph_node
*cgraph_function_versioning (struct cgraph_node
*,
400 VEC(cgraph_edge_p
,heap
)*,
403 void cgraph_analyze_function (struct cgraph_node
*);
404 struct cgraph_node
*save_inline_function_body (struct cgraph_node
*);
405 void record_references_in_initializer (tree
);
406 bool cgraph_process_new_functions (void);
408 typedef void (*cgraph_edge_hook
)(struct cgraph_edge
*, void *);
409 typedef void (*cgraph_node_hook
)(struct cgraph_node
*, void *);
410 typedef void (*cgraph_2edge_hook
)(struct cgraph_edge
*, struct cgraph_edge
*,
412 typedef void (*cgraph_2node_hook
)(struct cgraph_node
*, struct cgraph_node
*,
414 struct cgraph_edge_hook_list
;
415 struct cgraph_node_hook_list
;
416 struct cgraph_2edge_hook_list
;
417 struct cgraph_2node_hook_list
;
418 struct cgraph_edge_hook_list
*cgraph_add_edge_removal_hook (cgraph_edge_hook
, void *);
419 void cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list
*);
420 struct cgraph_node_hook_list
*cgraph_add_node_removal_hook (cgraph_node_hook
,
422 void cgraph_remove_node_removal_hook (struct cgraph_node_hook_list
*);
423 struct cgraph_node_hook_list
*cgraph_add_function_insertion_hook (cgraph_node_hook
,
425 void cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list
*);
426 void cgraph_call_function_insertion_hooks (struct cgraph_node
*node
);
427 struct cgraph_2edge_hook_list
*cgraph_add_edge_duplication_hook (cgraph_2edge_hook
, void *);
428 void cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list
*);
429 struct cgraph_2node_hook_list
*cgraph_add_node_duplication_hook (cgraph_2node_hook
, void *);
430 void cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list
*);
432 /* In cgraphbuild.c */
433 unsigned int rebuild_cgraph_edges (void);
434 int compute_call_stmt_bb_frequency (basic_block bb
);
437 bool cgraph_remove_unreachable_nodes (bool, FILE *);
438 int cgraph_postorder (struct cgraph_node
**);
439 cgraph_node_set
cgraph_node_set_new (void);
440 cgraph_node_set_iterator
cgraph_node_set_find (cgraph_node_set
,
441 struct cgraph_node
*);
442 void cgraph_node_set_add (cgraph_node_set
, struct cgraph_node
*);
443 void cgraph_node_set_remove (cgraph_node_set
, struct cgraph_node
*);
444 void dump_cgraph_node_set (FILE *, cgraph_node_set
);
445 void debug_cgraph_node_set (cgraph_node_set
);
449 bool cgraph_maybe_hot_edge_p (struct cgraph_edge
*e
);
452 extern GTY(()) struct varpool_node
*varpool_nodes_queue
;
453 extern GTY(()) struct varpool_node
*varpool_nodes
;
455 struct varpool_node
*varpool_node (tree
);
456 struct varpool_node
*varpool_node_for_asm (tree asmname
);
457 void varpool_mark_needed_node (struct varpool_node
*);
458 void debug_varpool (void);
459 void dump_varpool (FILE *);
460 void dump_varpool_node (FILE *, struct varpool_node
*);
462 void varpool_finalize_decl (tree
);
463 bool decide_is_variable_needed (struct varpool_node
*, tree
);
464 enum availability
cgraph_variable_initializer_availability (struct varpool_node
*);
466 bool varpool_assemble_pending_decls (void);
467 bool varpool_assemble_decl (struct varpool_node
*node
);
468 bool varpool_analyze_pending_decls (void);
469 void varpool_remove_unreferenced_decls (void);
470 void varpool_empty_needed_queue (void);
472 /* Walk all reachable static variables. */
473 #define FOR_EACH_STATIC_VARIABLE(node) \
474 for ((node) = varpool_nodes_queue; (node); (node) = (node)->next_needed)
476 /* Return first reachable static variable with initializer. */
477 static inline struct varpool_node
*
478 varpool_first_static_initializer (void)
480 struct varpool_node
*node
;
481 for (node
= varpool_nodes_queue
; node
; node
= node
->next_needed
)
483 gcc_assert (TREE_CODE (node
->decl
) == VAR_DECL
);
484 if (DECL_INITIAL (node
->decl
))
490 /* Return next reachable static variable with initializer after NODE. */
491 static inline struct varpool_node
*
492 varpool_next_static_initializer (struct varpool_node
*node
)
494 for (node
= node
->next_needed
; node
; node
= node
->next_needed
)
496 gcc_assert (TREE_CODE (node
->decl
) == VAR_DECL
);
497 if (DECL_INITIAL (node
->decl
))
503 /* Walk all static variables with initializer set. */
504 #define FOR_EACH_STATIC_INITIALIZER(node) \
505 for ((node) = varpool_first_static_initializer (); (node); \
506 (node) = varpool_next_static_initializer (node))
508 /* In ipa-inline.c */
509 void cgraph_clone_inlined_nodes (struct cgraph_edge
*, bool, bool);
510 unsigned int compute_inline_parameters (struct cgraph_node
*);
513 /* Create a new static variable of type TYPE. */
514 tree
add_new_static_var (tree type
);
517 /* Return true if iterator CSI points to nothing. */
519 csi_end_p (cgraph_node_set_iterator csi
)
521 return csi
.index
>= VEC_length (cgraph_node_ptr
, csi
.set
->nodes
);
524 /* Advance iterator CSI. */
526 csi_next (cgraph_node_set_iterator
*csi
)
531 /* Return the node pointed to by CSI. */
532 static inline struct cgraph_node
*
533 csi_node (cgraph_node_set_iterator csi
)
535 return VEC_index (cgraph_node_ptr
, csi
.set
->nodes
, csi
.index
);
538 /* Return an iterator to the first node in SET. */
539 static inline cgraph_node_set_iterator
540 csi_start (cgraph_node_set set
)
542 cgraph_node_set_iterator csi
;
549 /* Return true if SET contains NODE. */
551 cgraph_node_in_set_p (struct cgraph_node
*node
, cgraph_node_set set
)
553 cgraph_node_set_iterator csi
;
554 csi
= cgraph_node_set_find (set
, node
);
555 return !csi_end_p (csi
);
558 /* Return number of nodes in SET. */
560 cgraph_node_set_size (cgraph_node_set set
)
562 return htab_elements (set
->hashtab
);
566 #endif /* GCC_CGRAPH_H */