cgraphbuild.c (compute_call_stmt_bb_frequency): Accept function argument; handle...
[gcc.git] / gcc / cgraph.h
1 /* Callgraph handling code.
2 Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Jan Hubicka
5
6 This file is part of GCC.
7
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
11 version.
12
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
16 for more details.
17
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/>. */
21
22 #ifndef GCC_CGRAPH_H
23 #define GCC_CGRAPH_H
24 #include "tree.h"
25 #include "basic-block.h"
26
27 enum availability
28 {
29 /* Not yet set by cgraph_function_body_availability. */
30 AVAIL_UNSET,
31 /* Function body/variable initializer is unknown. */
32 AVAIL_NOT_AVAILABLE,
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. */
38 AVAIL_OVERWRITABLE,
39 /* Function body/variable initializer is known and will be used in final
40 program. */
41 AVAIL_AVAILABLE,
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. */
46 AVAIL_LOCAL
47 };
48
49 extern const char * const cgraph_availability_names[];
50
51 /* Information about the function collected locally.
52 Available after function is analyzed. */
53
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;
58
59 /* Size of the function before inlining. */
60 int self_insns;
61 } inline_summary;
62
63 /* Set when function function is visible in current compilation unit only
64 and its address is never taken. */
65 unsigned local : 1;
66
67 /* Set when function is visible by other units. */
68 unsigned externally_visible : 1;
69
70 /* Set once it has been finalized so we consider it to be output. */
71 unsigned finalized : 1;
72
73 /* False when there something makes inlining impossible (such as va_arg). */
74 unsigned inlinable : 1;
75
76 /* True when function should be inlined independently on its size. */
77 unsigned disregard_inline_limits : 1;
78
79 /* True when the function has been originally extern inline, but it is
80 redefined now. */
81 unsigned redefined_extern_inline : 1;
82
83 /* True if statics_read_for_function and
84 statics_written_for_function contain valid data. */
85 unsigned for_functions_valid : 1;
86
87 /* True if the function is going to be emitted in some other translation
88 unit, referenced from vtable. */
89 unsigned vtable_method : 1;
90 };
91
92 /* Information about the function that needs to be computed globally
93 once compilation is finished. Available only with -funit-at-a-time. */
94
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;
100
101 /* For inline clones this points to the function they will be
102 inlined into. */
103 struct cgraph_node *inlined_to;
104
105 /* Estimated size of the function after inlining. */
106 int insns;
107
108 /* Estimated growth after inlining. INT_MIN if not computed. */
109 int estimated_growth;
110
111 /* Set iff the function has been inlined at least once. */
112 bool inlined;
113 };
114
115 /* Information about the function that is propagated by the RTL backend.
116 Available only for functions that has been already assembled. */
117
118 struct GTY(()) cgraph_rtl_info {
119 unsigned int preferred_incoming_stack_boundary;
120 };
121
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
125 {
126 /* The tree that will be replaced. */
127 tree old_tree;
128 /* The new (replacing) tree. */
129 tree new_tree;
130 /* True when a substitution should be done, false otherwise. */
131 bool replace_p;
132 /* True when we replace a reference to old_tree. */
133 bool ref_p;
134 };
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);
138
139 struct GTY(()) cgraph_clone_info
140 {
141 VEC(ipa_replace_map_p,gc)* tree_map;
142 bitmap args_to_skip;
143 };
144
145 /* The cgraph data structure.
146 Each function decl has assigned cgraph_node listing callees and callers. */
147
148 struct GTY((chain_next ("%h.next"), chain_prev ("%h.previous"))) cgraph_node {
149 tree decl;
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;
170
171 PTR GTY ((skip)) aux;
172
173 struct cgraph_local_info local;
174 struct cgraph_global_info global;
175 struct cgraph_rtl_info rtl;
176 struct cgraph_clone_info clone;
177
178 /* Expected number of executions: calculated in profile.c. */
179 gcov_type count;
180 /* Unique id of the node. */
181 int uid;
182 /* Ordering of all cgraph nodes. */
183 int order;
184
185 /* unique id for profiling. pid is not suitable because of different
186 number of cfg nodes with -fprofile-generate and -fprofile-use */
187 int pid;
188
189 /* Set when function must be output - it is externally visible
190 or its address is taken. */
191 unsigned needed : 1;
192 /* Set when decl is an abstract function pointed to by the
193 ABSTRACT_DECL_ORIGIN of a reachable function. */
194 unsigned abstract_and_needed : 1;
195 /* Set when function is reachable by call from other function
196 that is either reachable or needed. */
197 unsigned reachable : 1;
198 /* Set once the function is lowered (i.e. its CFG is built). */
199 unsigned lowered : 1;
200 /* Set once the function has been instantiated and its callee
201 lists created. */
202 unsigned analyzed : 1;
203 /* Set when function is scheduled to be processed by local passes. */
204 unsigned process : 1;
205 /* Set for aliases once they got through assemble_alias. */
206 unsigned alias : 1;
207
208 /* In non-unit-at-a-time mode the function body of inline candidates is saved
209 into clone before compiling so the function in original form can be
210 inlined later. This pointer points to the clone. */
211 tree inline_decl;
212 };
213
214 typedef struct cgraph_node *cgraph_node_ptr;
215
216 DEF_VEC_P(cgraph_node_ptr);
217 DEF_VEC_ALLOC_P(cgraph_node_ptr,heap);
218 DEF_VEC_ALLOC_P(cgraph_node_ptr,gc);
219
220 /* A cgraph node set is a collection of cgraph nodes. A cgraph node
221 can appear in multiple sets. */
222 struct GTY(()) cgraph_node_set_def
223 {
224 htab_t GTY((param_is (struct cgraph_node_set_element_def))) hashtab;
225 VEC(cgraph_node_ptr, gc) *nodes;
226 PTR GTY ((skip)) aux;
227 };
228
229 typedef struct cgraph_node_set_def *cgraph_node_set;
230
231 DEF_VEC_P(cgraph_node_set);
232 DEF_VEC_ALLOC_P(cgraph_node_set,gc);
233 DEF_VEC_ALLOC_P(cgraph_node_set,heap);
234
235 /* A cgraph node set element contains an index in the vector of nodes in
236 the set. */
237 struct GTY(()) cgraph_node_set_element_def
238 {
239 struct cgraph_node *node;
240 HOST_WIDE_INT index;
241 };
242
243 typedef struct cgraph_node_set_element_def *cgraph_node_set_element;
244 typedef const struct cgraph_node_set_element_def *const_cgraph_node_set_element;
245
246 /* Iterator structure for cgraph node sets. */
247 typedef struct
248 {
249 cgraph_node_set set;
250 unsigned index;
251 } cgraph_node_set_iterator;
252
253 #define DEFCIFCODE(code, string) CIF_ ## code,
254 /* Reasons for inlining failures. */
255 typedef enum {
256 #include "cif-code.def"
257 CIF_N_REASONS
258 } cgraph_inline_failed_t;
259
260 struct GTY((chain_next ("%h.next_caller"), chain_prev ("%h.prev_caller"))) cgraph_edge {
261 struct cgraph_node *caller;
262 struct cgraph_node *callee;
263 struct cgraph_edge *prev_caller;
264 struct cgraph_edge *next_caller;
265 struct cgraph_edge *prev_callee;
266 struct cgraph_edge *next_callee;
267 gimple call_stmt;
268 PTR GTY ((skip (""))) aux;
269 /* When equal to CIF_OK, inline this call. Otherwise, points to the
270 explanation why function was not inlined. */
271 cgraph_inline_failed_t inline_failed;
272 /* Expected number of executions: calculated in profile.c. */
273 gcov_type count;
274 /* Expected frequency of executions within the function.
275 When set to CGRAPH_FREQ_BASE, the edge is expected to be called once
276 per function call. The range is 0 to CGRAPH_FREQ_MAX. */
277 int frequency;
278 /* Depth of loop nest, 1 means no loop nest. */
279 unsigned int loop_nest : 30;
280 /* Whether this edge describes a call that was originally indirect. */
281 unsigned int indirect_call : 1;
282 /* Can this call throw externally? */
283 unsigned int can_throw_external : 1;
284 /* Unique id of the edge. */
285 int uid;
286 };
287
288 #define CGRAPH_FREQ_BASE 1000
289 #define CGRAPH_FREQ_MAX 100000
290
291 typedef struct cgraph_edge *cgraph_edge_p;
292
293 DEF_VEC_P(cgraph_edge_p);
294 DEF_VEC_ALLOC_P(cgraph_edge_p,heap);
295
296 /* The varpool data structure.
297 Each static variable decl has assigned varpool_node. */
298
299 struct GTY((chain_next ("%h.next"))) varpool_node {
300 tree decl;
301 /* Pointer to the next function in varpool_nodes. */
302 struct varpool_node *next;
303 /* Pointer to the next function in varpool_nodes_queue. */
304 struct varpool_node *next_needed;
305 /* Ordering of all cgraph nodes. */
306 int order;
307
308 /* Set when function must be output - it is externally visible
309 or its address is taken. */
310 unsigned needed : 1;
311 /* Needed variables might become dead by optimization. This flag
312 forces the variable to be output even if it appears dead otherwise. */
313 unsigned force_output : 1;
314 /* Set once the variable has been instantiated and its callee
315 lists created. */
316 unsigned analyzed : 1;
317 /* Set once it has been finalized so we consider it to be output. */
318 unsigned finalized : 1;
319 /* Set when variable is scheduled to be assembled. */
320 unsigned output : 1;
321 /* Set when function is visible by other units. */
322 unsigned externally_visible : 1;
323 /* Set for aliases once they got through assemble_alias. */
324 unsigned alias : 1;
325 };
326
327 /* Every top level asm statement is put into a cgraph_asm_node. */
328
329 struct GTY(()) cgraph_asm_node {
330 /* Next asm node. */
331 struct cgraph_asm_node *next;
332 /* String for this asm node. */
333 tree asm_str;
334 /* Ordering of all cgraph nodes. */
335 int order;
336 };
337
338 extern GTY(()) struct cgraph_node *cgraph_nodes;
339 extern GTY(()) int cgraph_n_nodes;
340 extern GTY(()) int cgraph_max_uid;
341 extern GTY(()) int cgraph_edge_max_uid;
342 extern GTY(()) int cgraph_max_pid;
343 extern bool cgraph_global_info_ready;
344 enum cgraph_state
345 {
346 /* Callgraph is being constructed. It is safe to add new functions. */
347 CGRAPH_STATE_CONSTRUCTION,
348 /* Callgraph is built and IPA passes are being run. */
349 CGRAPH_STATE_IPA,
350 /* Callgraph is built and all functions are transformed to SSA form. */
351 CGRAPH_STATE_IPA_SSA,
352 /* Functions are now ordered and being passed to RTL expanders. */
353 CGRAPH_STATE_EXPANSION,
354 /* All cgraph expansion is done. */
355 CGRAPH_STATE_FINISHED
356 };
357 extern enum cgraph_state cgraph_state;
358 extern bool cgraph_function_flags_ready;
359 extern GTY(()) struct cgraph_node *cgraph_nodes_queue;
360 extern GTY(()) struct cgraph_node *cgraph_new_nodes;
361
362 extern GTY(()) struct cgraph_asm_node *cgraph_asm_nodes;
363 extern GTY(()) int cgraph_order;
364
365 /* In cgraph.c */
366 void dump_cgraph (FILE *);
367 void debug_cgraph (void);
368 void dump_cgraph_node (FILE *, struct cgraph_node *);
369 void debug_cgraph_node (struct cgraph_node *);
370 void cgraph_insert_node_to_hashtable (struct cgraph_node *node);
371 void cgraph_remove_edge (struct cgraph_edge *);
372 void cgraph_remove_node (struct cgraph_node *);
373 void cgraph_remove_node_and_inline_clones (struct cgraph_node *);
374 void cgraph_release_function_body (struct cgraph_node *);
375 void cgraph_node_remove_callees (struct cgraph_node *node);
376 struct cgraph_edge *cgraph_create_edge (struct cgraph_node *,
377 struct cgraph_node *,
378 gimple, gcov_type, int, int);
379 struct cgraph_node *cgraph_node (tree);
380 struct cgraph_node *cgraph_node_for_asm (tree asmname);
381 struct cgraph_edge *cgraph_edge (struct cgraph_node *, gimple);
382 void cgraph_set_call_stmt (struct cgraph_edge *, gimple);
383 void cgraph_set_call_stmt_including_clones (struct cgraph_node *, gimple, gimple);
384 void cgraph_create_edge_including_clones (struct cgraph_node *,
385 struct cgraph_node *,
386 gimple, gcov_type, int, int,
387 cgraph_inline_failed_t);
388 void cgraph_update_edges_for_call_stmt (gimple, gimple);
389 struct cgraph_local_info *cgraph_local_info (tree);
390 struct cgraph_global_info *cgraph_global_info (tree);
391 struct cgraph_rtl_info *cgraph_rtl_info (tree);
392 const char * cgraph_node_name (struct cgraph_node *);
393 struct cgraph_edge * cgraph_clone_edge (struct cgraph_edge *,
394 struct cgraph_node *,
395 gimple, gcov_type, int, int, bool);
396 struct cgraph_node * cgraph_clone_node (struct cgraph_node *, gcov_type, int,
397 int, bool);
398
399 void cgraph_redirect_edge_callee (struct cgraph_edge *, struct cgraph_node *);
400
401 struct cgraph_asm_node *cgraph_add_asm_node (tree);
402
403 bool cgraph_function_possibly_inlined_p (tree);
404 void cgraph_unnest_node (struct cgraph_node *);
405
406 enum availability cgraph_function_body_availability (struct cgraph_node *);
407 void cgraph_add_new_function (tree, bool);
408 const char* cgraph_inline_failed_string (cgraph_inline_failed_t);
409 struct cgraph_node * cgraph_create_virtual_clone (struct cgraph_node *old_node,
410 VEC(cgraph_edge_p,heap)*,
411 VEC(ipa_replace_map_p,gc)* tree_map,
412 bitmap args_to_skip);
413
414 /* In cgraphunit.c */
415 void cgraph_finalize_function (tree, bool);
416 void cgraph_mark_if_needed (tree);
417 void cgraph_finalize_compilation_unit (void);
418 void cgraph_optimize (void);
419 void cgraph_mark_needed_node (struct cgraph_node *);
420 void cgraph_mark_reachable_node (struct cgraph_node *);
421 bool cgraph_inline_p (struct cgraph_edge *, cgraph_inline_failed_t *reason);
422 bool cgraph_preserve_function_body_p (tree);
423 void verify_cgraph (void);
424 void verify_cgraph_node (struct cgraph_node *);
425 void cgraph_build_static_cdtor (char which, tree body, int priority);
426 void cgraph_reset_static_var_maps (void);
427 void init_cgraph (void);
428 struct cgraph_node *cgraph_function_versioning (struct cgraph_node *,
429 VEC(cgraph_edge_p,heap)*,
430 VEC(ipa_replace_map_p,gc)*,
431 bitmap);
432 void tree_function_versioning (tree, tree, VEC (ipa_replace_map_p,gc)*, bool, bitmap);
433 void cgraph_analyze_function (struct cgraph_node *);
434 struct cgraph_node *save_inline_function_body (struct cgraph_node *);
435 void record_references_in_initializer (tree);
436 bool cgraph_process_new_functions (void);
437
438 typedef void (*cgraph_edge_hook)(struct cgraph_edge *, void *);
439 typedef void (*cgraph_node_hook)(struct cgraph_node *, void *);
440 typedef void (*cgraph_2edge_hook)(struct cgraph_edge *, struct cgraph_edge *,
441 void *);
442 typedef void (*cgraph_2node_hook)(struct cgraph_node *, struct cgraph_node *,
443 void *);
444 struct cgraph_edge_hook_list;
445 struct cgraph_node_hook_list;
446 struct cgraph_2edge_hook_list;
447 struct cgraph_2node_hook_list;
448 struct cgraph_edge_hook_list *cgraph_add_edge_removal_hook (cgraph_edge_hook, void *);
449 void cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *);
450 struct cgraph_node_hook_list *cgraph_add_node_removal_hook (cgraph_node_hook,
451 void *);
452 void cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *);
453 struct cgraph_node_hook_list *cgraph_add_function_insertion_hook (cgraph_node_hook,
454 void *);
455 void cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *);
456 void cgraph_call_function_insertion_hooks (struct cgraph_node *node);
457 struct cgraph_2edge_hook_list *cgraph_add_edge_duplication_hook (cgraph_2edge_hook, void *);
458 void cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *);
459 struct cgraph_2node_hook_list *cgraph_add_node_duplication_hook (cgraph_2node_hook, void *);
460 void cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *);
461 void cgraph_materialize_all_clones (void);
462
463 /* In cgraphbuild.c */
464 unsigned int rebuild_cgraph_edges (void);
465 int compute_call_stmt_bb_frequency (tree, basic_block bb);
466
467 /* In ipa.c */
468 bool cgraph_remove_unreachable_nodes (bool, FILE *);
469 int cgraph_postorder (struct cgraph_node **);
470 cgraph_node_set cgraph_node_set_new (void);
471 cgraph_node_set_iterator cgraph_node_set_find (cgraph_node_set,
472 struct cgraph_node *);
473 void cgraph_node_set_add (cgraph_node_set, struct cgraph_node *);
474 void cgraph_node_set_remove (cgraph_node_set, struct cgraph_node *);
475 void dump_cgraph_node_set (FILE *, cgraph_node_set);
476 void debug_cgraph_node_set (cgraph_node_set);
477
478
479 /* In predict.c */
480 bool cgraph_maybe_hot_edge_p (struct cgraph_edge *e);
481
482 /* In varpool.c */
483 extern GTY(()) struct varpool_node *varpool_nodes_queue;
484 extern GTY(()) struct varpool_node *varpool_nodes;
485
486 struct varpool_node *varpool_node (tree);
487 struct varpool_node *varpool_node_for_asm (tree asmname);
488 void varpool_mark_needed_node (struct varpool_node *);
489 void debug_varpool (void);
490 void dump_varpool (FILE *);
491 void dump_varpool_node (FILE *, struct varpool_node *);
492
493 void varpool_finalize_decl (tree);
494 bool decide_is_variable_needed (struct varpool_node *, tree);
495 enum availability cgraph_variable_initializer_availability (struct varpool_node *);
496
497 bool varpool_assemble_pending_decls (void);
498 bool varpool_assemble_decl (struct varpool_node *node);
499 bool varpool_analyze_pending_decls (void);
500 void varpool_remove_unreferenced_decls (void);
501 void varpool_empty_needed_queue (void);
502
503 /* Walk all reachable static variables. */
504 #define FOR_EACH_STATIC_VARIABLE(node) \
505 for ((node) = varpool_nodes_queue; (node); (node) = (node)->next_needed)
506
507 /* Return first reachable static variable with initializer. */
508 static inline struct varpool_node *
509 varpool_first_static_initializer (void)
510 {
511 struct varpool_node *node;
512 for (node = varpool_nodes_queue; node; node = node->next_needed)
513 {
514 gcc_assert (TREE_CODE (node->decl) == VAR_DECL);
515 if (DECL_INITIAL (node->decl))
516 return node;
517 }
518 return NULL;
519 }
520
521 /* Return next reachable static variable with initializer after NODE. */
522 static inline struct varpool_node *
523 varpool_next_static_initializer (struct varpool_node *node)
524 {
525 for (node = node->next_needed; node; node = node->next_needed)
526 {
527 gcc_assert (TREE_CODE (node->decl) == VAR_DECL);
528 if (DECL_INITIAL (node->decl))
529 return node;
530 }
531 return NULL;
532 }
533
534 /* Walk all static variables with initializer set. */
535 #define FOR_EACH_STATIC_INITIALIZER(node) \
536 for ((node) = varpool_first_static_initializer (); (node); \
537 (node) = varpool_next_static_initializer (node))
538
539 /* In ipa-inline.c */
540 void cgraph_clone_inlined_nodes (struct cgraph_edge *, bool, bool);
541 unsigned int compute_inline_parameters (struct cgraph_node *);
542
543
544 /* Create a new static variable of type TYPE. */
545 tree add_new_static_var (tree type);
546
547
548 /* Return true if iterator CSI points to nothing. */
549 static inline bool
550 csi_end_p (cgraph_node_set_iterator csi)
551 {
552 return csi.index >= VEC_length (cgraph_node_ptr, csi.set->nodes);
553 }
554
555 /* Advance iterator CSI. */
556 static inline void
557 csi_next (cgraph_node_set_iterator *csi)
558 {
559 csi->index++;
560 }
561
562 /* Return the node pointed to by CSI. */
563 static inline struct cgraph_node *
564 csi_node (cgraph_node_set_iterator csi)
565 {
566 return VEC_index (cgraph_node_ptr, csi.set->nodes, csi.index);
567 }
568
569 /* Return an iterator to the first node in SET. */
570 static inline cgraph_node_set_iterator
571 csi_start (cgraph_node_set set)
572 {
573 cgraph_node_set_iterator csi;
574
575 csi.set = set;
576 csi.index = 0;
577 return csi;
578 }
579
580 /* Return true if SET contains NODE. */
581 static inline bool
582 cgraph_node_in_set_p (struct cgraph_node *node, cgraph_node_set set)
583 {
584 cgraph_node_set_iterator csi;
585 csi = cgraph_node_set_find (set, node);
586 return !csi_end_p (csi);
587 }
588
589 /* Return number of nodes in SET. */
590 static inline size_t
591 cgraph_node_set_size (cgraph_node_set set)
592 {
593 return htab_elements (set->hashtab);
594 }
595
596
597 #endif /* GCC_CGRAPH_H */