cgraphbuild.c (record_reference): Use cgraph_mark_address_taken_node.
[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 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
203 lists created. */
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. */
208 unsigned alias : 1;
209
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. */
213 tree inline_decl;
214 };
215
216 typedef struct cgraph_node *cgraph_node_ptr;
217
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);
221
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
225 {
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;
229 };
230
231 typedef struct cgraph_node_set_def *cgraph_node_set;
232
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);
236
237 /* A cgraph node set element contains an index in the vector of nodes in
238 the set. */
239 struct GTY(()) cgraph_node_set_element_def
240 {
241 struct cgraph_node *node;
242 HOST_WIDE_INT index;
243 };
244
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;
247
248 /* Iterator structure for cgraph node sets. */
249 typedef struct
250 {
251 cgraph_node_set set;
252 unsigned index;
253 } cgraph_node_set_iterator;
254
255 #define DEFCIFCODE(code, string) CIF_ ## code,
256 /* Reasons for inlining failures. */
257 typedef enum {
258 #include "cif-code.def"
259 CIF_N_REASONS
260 } cgraph_inline_failed_t;
261
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;
269 gimple call_stmt;
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. */
275 gcov_type count;
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. */
279 int frequency;
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. */
287 int uid;
288 };
289
290 #define CGRAPH_FREQ_BASE 1000
291 #define CGRAPH_FREQ_MAX 100000
292
293 typedef struct cgraph_edge *cgraph_edge_p;
294
295 DEF_VEC_P(cgraph_edge_p);
296 DEF_VEC_ALLOC_P(cgraph_edge_p,heap);
297
298 /* The varpool data structure.
299 Each static variable decl has assigned varpool_node. */
300
301 struct GTY((chain_next ("%h.next"))) varpool_node {
302 tree decl;
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. */
308 int order;
309
310 /* Set when function must be output - it is externally visible
311 or its address is taken. */
312 unsigned needed : 1;
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
317 lists created. */
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. */
322 unsigned output : 1;
323 /* Set when function is visible by other units. */
324 unsigned externally_visible : 1;
325 /* Set for aliases once they got through assemble_alias. */
326 unsigned alias : 1;
327 };
328
329 /* Every top level asm statement is put into a cgraph_asm_node. */
330
331 struct GTY(()) cgraph_asm_node {
332 /* Next asm node. */
333 struct cgraph_asm_node *next;
334 /* String for this asm node. */
335 tree asm_str;
336 /* Ordering of all cgraph nodes. */
337 int order;
338 };
339
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;
346 enum cgraph_state
347 {
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. */
351 CGRAPH_STATE_IPA,
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
358 };
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;
363
364 extern GTY(()) struct cgraph_asm_node *cgraph_asm_nodes;
365 extern GTY(()) int cgraph_order;
366
367 /* In cgraph.c */
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,
399 int, bool);
400
401 void cgraph_redirect_edge_callee (struct cgraph_edge *, struct cgraph_node *);
402
403 struct cgraph_asm_node *cgraph_add_asm_node (tree);
404
405 bool cgraph_function_possibly_inlined_p (tree);
406 void cgraph_unnest_node (struct cgraph_node *);
407
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);
415
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)*,
434 bitmap);
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);
440
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 *,
444 void *);
445 typedef void (*cgraph_2node_hook)(struct cgraph_node *, struct cgraph_node *,
446 void *);
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,
454 void *);
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,
457 void *);
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);
465
466 /* In cgraphbuild.c */
467 unsigned int rebuild_cgraph_edges (void);
468 int compute_call_stmt_bb_frequency (tree, basic_block bb);
469
470 /* In ipa.c */
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);
480
481
482 /* In predict.c */
483 bool cgraph_maybe_hot_edge_p (struct cgraph_edge *e);
484
485 /* In varpool.c */
486 extern GTY(()) struct varpool_node *varpool_nodes_queue;
487 extern GTY(()) struct varpool_node *varpool_nodes;
488
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 *);
495
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 *);
499
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);
505
506 /* Walk all reachable static variables. */
507 #define FOR_EACH_STATIC_VARIABLE(node) \
508 for ((node) = varpool_nodes_queue; (node); (node) = (node)->next_needed)
509
510 /* Return first reachable static variable with initializer. */
511 static inline struct varpool_node *
512 varpool_first_static_initializer (void)
513 {
514 struct varpool_node *node;
515 for (node = varpool_nodes_queue; node; node = node->next_needed)
516 {
517 gcc_assert (TREE_CODE (node->decl) == VAR_DECL);
518 if (DECL_INITIAL (node->decl))
519 return node;
520 }
521 return NULL;
522 }
523
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)
527 {
528 for (node = node->next_needed; node; node = node->next_needed)
529 {
530 gcc_assert (TREE_CODE (node->decl) == VAR_DECL);
531 if (DECL_INITIAL (node->decl))
532 return node;
533 }
534 return NULL;
535 }
536
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))
541
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 *);
545
546
547 /* Create a new static variable of type TYPE. */
548 tree add_new_static_var (tree type);
549
550
551 /* Return true if iterator CSI points to nothing. */
552 static inline bool
553 csi_end_p (cgraph_node_set_iterator csi)
554 {
555 return csi.index >= VEC_length (cgraph_node_ptr, csi.set->nodes);
556 }
557
558 /* Advance iterator CSI. */
559 static inline void
560 csi_next (cgraph_node_set_iterator *csi)
561 {
562 csi->index++;
563 }
564
565 /* Return the node pointed to by CSI. */
566 static inline struct cgraph_node *
567 csi_node (cgraph_node_set_iterator csi)
568 {
569 return VEC_index (cgraph_node_ptr, csi.set->nodes, csi.index);
570 }
571
572 /* Return an iterator to the first node in SET. */
573 static inline cgraph_node_set_iterator
574 csi_start (cgraph_node_set set)
575 {
576 cgraph_node_set_iterator csi;
577
578 csi.set = set;
579 csi.index = 0;
580 return csi;
581 }
582
583 /* Return true if SET contains NODE. */
584 static inline bool
585 cgraph_node_in_set_p (struct cgraph_node *node, cgraph_node_set set)
586 {
587 cgraph_node_set_iterator csi;
588 csi = cgraph_node_set_find (set, node);
589 return !csi_end_p (csi);
590 }
591
592 /* Return number of nodes in SET. */
593 static inline size_t
594 cgraph_node_set_size (cgraph_node_set set)
595 {
596 return htab_elements (set->hashtab);
597 }
598
599
600 #endif /* GCC_CGRAPH_H */