cgraph.c (assembler_name_hash): New static var.
[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 cgraph_local_info GTY(())
55 {
56 struct inline_summary {
57 /* Estimated stack frame consumption by the function. */
58 HOST_WIDE_INT estimated_self_stack_size;
59
60 /* Size of the function before inlining. */
61 int self_insns;
62 } inline_summary;
63
64 /* Set when function function is visible in current compilation unit only
65 and its address is never taken. */
66 unsigned local : 1;
67
68 /* Set when function is visible by other units. */
69 unsigned externally_visible : 1;
70
71 /* Set once it has been finalized so we consider it to be output. */
72 unsigned finalized : 1;
73
74 /* False when there something makes inlining impossible (such as va_arg). */
75 unsigned inlinable : 1;
76
77 /* True when function should be inlined independently on its size. */
78 unsigned disregard_inline_limits : 1;
79
80 /* True when the function has been originally extern inline, but it is
81 redefined now. */
82 unsigned redefined_extern_inline : 1;
83
84 /* True if statics_read_for_function and
85 statics_written_for_function contain valid data. */
86 unsigned for_functions_valid : 1;
87
88 /* True if the function is going to be emitted in some other translation
89 unit, referenced from vtable. */
90 unsigned vtable_method : 1;
91 };
92
93 /* Information about the function that needs to be computed globally
94 once compilation is finished. Available only with -funit-at-time. */
95
96 struct cgraph_global_info GTY(())
97 {
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;
102
103 /* For inline clones this points to the function they will be inlined into. */
104 struct cgraph_node *inlined_to;
105
106 /* Estimated size of the function after inlining. */
107 int insns;
108
109 /* Estimated growth after inlining. INT_MIN if not computed. */
110 int estimated_growth;
111
112 /* Set iff the function has been inlined at least once. */
113 bool inlined;
114 };
115
116 /* Information about the function that is propagated by the RTL backend.
117 Available only for functions that has been already assembled. */
118
119 struct cgraph_rtl_info GTY(())
120 {
121 unsigned int preferred_incoming_stack_boundary;
122 };
123
124 /* The cgraph data structure.
125 Each function decl has assigned cgraph_node listing callees and callers. */
126
127 struct cgraph_node GTY((chain_next ("%h.next"), chain_prev ("%h.previous")))
128 {
129 tree decl;
130 struct cgraph_edge *callees;
131 struct cgraph_edge *callers;
132 struct cgraph_node *next;
133 struct cgraph_node *previous;
134 /* For nested functions points to function the node is nested in. */
135 struct cgraph_node *origin;
136 /* Points to first nested function, if any. */
137 struct cgraph_node *nested;
138 /* Pointer to the next function with same origin, if any. */
139 struct cgraph_node *next_nested;
140 /* Pointer to the next function in cgraph_nodes_queue. */
141 struct cgraph_node *next_needed;
142 /* Pointer to the next clone. */
143 struct cgraph_node *next_clone;
144 struct cgraph_node *prev_clone;
145 /* Pointer to a single unique cgraph node for this function. If the
146 function is to be output, this is the copy that will survive. */
147 struct cgraph_node *master_clone;
148 /* For functions with many calls sites it holds map from call expression
149 to the edge to speed up cgraph_edge function. */
150 htab_t GTY((param_is (struct cgraph_edge))) call_site_hash;
151
152 PTR GTY ((skip)) aux;
153
154 struct cgraph_local_info local;
155 struct cgraph_global_info global;
156 struct cgraph_rtl_info rtl;
157
158 /* Expected number of executions: calculated in profile.c. */
159 gcov_type count;
160 /* Unique id of the node. */
161 int uid;
162 /* Ordering of all cgraph nodes. */
163 int order;
164
165 /* Set when function must be output - it is externally visible
166 or its address is taken. */
167 unsigned needed : 1;
168 /* Set when function is reachable by call from other function
169 that is either reachable or needed. */
170 unsigned reachable : 1;
171 /* Set once the function is lowered (i.e. its CFG is built). */
172 unsigned lowered : 1;
173 /* Set once the function has been instantiated and its callee
174 lists created. */
175 unsigned analyzed : 1;
176 /* Set when function is scheduled to be assembled. */
177 unsigned output : 1;
178 /* Set for aliases once they got through assemble_alias. */
179 unsigned alias : 1;
180
181 /* In non-unit-at-a-time mode the function body of inline candidates is saved
182 into clone before compiling so the function in original form can be
183 inlined later. This pointer points to the clone. */
184 tree inline_decl;
185
186 /* unique id for profiling. pid is not suitable because of different
187 number of cfg nodes with -fprofile-generate and -fprofile-use */
188 int pid;
189 };
190
191 struct cgraph_edge GTY((chain_next ("%h.next_caller"), chain_prev ("%h.prev_caller")))
192 {
193 struct cgraph_node *caller;
194 struct cgraph_node *callee;
195 struct cgraph_edge *prev_caller;
196 struct cgraph_edge *next_caller;
197 struct cgraph_edge *prev_callee;
198 struct cgraph_edge *next_callee;
199 tree call_stmt;
200 PTR GTY ((skip (""))) aux;
201 /* When NULL, inline this call. When non-NULL, points to the explanation
202 why function was not inlined. */
203 const char *inline_failed;
204 /* Expected number of executions: calculated in profile.c. */
205 gcov_type count;
206 /* Expected frequency of executions within the function.
207 When set to CGRAPH_FREQ_BASE, the edge is expected to be called once
208 per function call. The range is 0 to CGRAPH_FREQ_MAX. */
209 int frequency;
210 /* Depth of loop nest, 1 means no loop nest. */
211 int loop_nest;
212 /* Unique id of the edge. */
213 int uid;
214 };
215
216 #define CGRAPH_FREQ_BASE 1000
217 #define CGRAPH_FREQ_MAX 100000
218
219 typedef struct cgraph_edge *cgraph_edge_p;
220
221 DEF_VEC_P(cgraph_edge_p);
222 DEF_VEC_ALLOC_P(cgraph_edge_p,heap);
223
224 /* The varpool data structure.
225 Each static variable decl has assigned varpool_node. */
226
227 struct varpool_node GTY((chain_next ("%h.next")))
228 {
229 tree decl;
230 /* Pointer to the next function in varpool_nodes. */
231 struct varpool_node *next;
232 /* Pointer to the next function in varpool_nodes_queue. */
233 struct varpool_node *next_needed;
234 /* Ordering of all cgraph nodes. */
235 int order;
236
237 /* Set when function must be output - it is externally visible
238 or its address is taken. */
239 unsigned needed : 1;
240 /* Needed variables might become dead by optimization. This flag
241 forces the variable to be output even if it appears dead otherwise. */
242 unsigned force_output : 1;
243 /* Set once the variable has been instantiated and its callee
244 lists created. */
245 unsigned analyzed : 1;
246 /* Set once it has been finalized so we consider it to be output. */
247 unsigned finalized : 1;
248 /* Set when variable is scheduled to be assembled. */
249 unsigned output : 1;
250 /* Set when function is visible by other units. */
251 unsigned externally_visible : 1;
252 /* Set for aliases once they got through assemble_alias. */
253 unsigned alias : 1;
254 };
255
256 /* Every top level asm statement is put into a cgraph_asm_node. */
257
258 struct cgraph_asm_node GTY(())
259 {
260 /* Next asm node. */
261 struct cgraph_asm_node *next;
262 /* String for this asm node. */
263 tree asm_str;
264 /* Ordering of all cgraph nodes. */
265 int order;
266 };
267
268 extern GTY(()) struct cgraph_node *cgraph_nodes;
269 extern GTY(()) int cgraph_n_nodes;
270 extern GTY(()) int cgraph_max_uid;
271 extern GTY(()) int cgraph_edge_max_uid;
272 extern GTY(()) int cgraph_max_pid;
273 extern bool cgraph_global_info_ready;
274 enum cgraph_state
275 {
276 /* Callgraph is being constructed. It is safe to add new functions. */
277 CGRAPH_STATE_CONSTRUCTION,
278 /* Callgraph is built and IPA passes are being run. */
279 CGRAPH_STATE_IPA,
280 /* Callgraph is built and all functions are transformed to SSA form. */
281 CGRAPH_STATE_IPA_SSA,
282 /* Functions are now ordered and being passed to RTL expanders. */
283 CGRAPH_STATE_EXPANSION,
284 /* All cgraph expansion is done. */
285 CGRAPH_STATE_FINISHED
286 };
287 extern enum cgraph_state cgraph_state;
288 extern bool cgraph_function_flags_ready;
289 extern GTY(()) struct cgraph_node *cgraph_nodes_queue;
290 extern GTY(()) struct cgraph_node *cgraph_new_nodes;
291
292 extern GTY(()) struct cgraph_asm_node *cgraph_asm_nodes;
293 extern GTY(()) int cgraph_order;
294
295 /* In cgraph.c */
296 void dump_cgraph (FILE *);
297 void debug_cgraph (void);
298 void dump_cgraph_node (FILE *, struct cgraph_node *);
299 void debug_cgraph_node (struct cgraph_node *);
300 void cgraph_insert_node_to_hashtable (struct cgraph_node *node);
301 void cgraph_remove_edge (struct cgraph_edge *);
302 void cgraph_remove_node (struct cgraph_node *);
303 void cgraph_release_function_body (struct cgraph_node *);
304 void cgraph_node_remove_callees (struct cgraph_node *node);
305 struct cgraph_edge *cgraph_create_edge (struct cgraph_node *,
306 struct cgraph_node *,
307 tree, gcov_type, int, int);
308 struct cgraph_node *cgraph_node (tree);
309 struct cgraph_node *cgraph_node_for_asm (tree asmname);
310 struct cgraph_edge *cgraph_edge (struct cgraph_node *, tree);
311 void cgraph_set_call_stmt (struct cgraph_edge *, tree);
312 void cgraph_update_edges_for_call_stmt (tree, tree, tree);
313 struct cgraph_local_info *cgraph_local_info (tree);
314 struct cgraph_global_info *cgraph_global_info (tree);
315 struct cgraph_rtl_info *cgraph_rtl_info (tree);
316 const char * cgraph_node_name (struct cgraph_node *);
317 struct cgraph_edge * cgraph_clone_edge (struct cgraph_edge *,
318 struct cgraph_node *,
319 tree, gcov_type, int, int, bool);
320 struct cgraph_node * cgraph_clone_node (struct cgraph_node *, gcov_type, int,
321 int, bool);
322
323 void cgraph_redirect_edge_callee (struct cgraph_edge *, struct cgraph_node *);
324
325 struct cgraph_asm_node *cgraph_add_asm_node (tree);
326
327 bool cgraph_function_possibly_inlined_p (tree);
328 void cgraph_unnest_node (struct cgraph_node *);
329
330 enum availability cgraph_function_body_availability (struct cgraph_node *);
331 bool cgraph_is_master_clone (struct cgraph_node *);
332 struct cgraph_node *cgraph_master_clone (struct cgraph_node *);
333 void cgraph_add_new_function (tree, bool);
334
335 /* In cgraphunit.c */
336 void cgraph_finalize_function (tree, bool);
337 void cgraph_mark_if_needed (tree);
338 void cgraph_finalize_compilation_unit (void);
339 void cgraph_optimize (void);
340 void cgraph_mark_needed_node (struct cgraph_node *);
341 void cgraph_mark_reachable_node (struct cgraph_node *);
342 bool cgraph_inline_p (struct cgraph_edge *, const char **reason);
343 bool cgraph_preserve_function_body_p (tree);
344 void verify_cgraph (void);
345 void verify_cgraph_node (struct cgraph_node *);
346 void cgraph_build_static_cdtor (char which, tree body, int priority);
347 void cgraph_reset_static_var_maps (void);
348 void init_cgraph (void);
349 struct cgraph_node *cgraph_function_versioning (struct cgraph_node *,
350 VEC(cgraph_edge_p,heap)*,
351 varray_type);
352 void cgraph_analyze_function (struct cgraph_node *);
353 struct cgraph_node *save_inline_function_body (struct cgraph_node *);
354 void record_references_in_initializer (tree);
355 bool cgraph_process_new_functions (void);
356
357 typedef void (*cgraph_edge_hook)(struct cgraph_edge *, void *);
358 typedef void (*cgraph_node_hook)(struct cgraph_node *, void *);
359 typedef void (*cgraph_2edge_hook)(struct cgraph_edge *, struct cgraph_edge *,
360 void *);
361 typedef void (*cgraph_2node_hook)(struct cgraph_node *, struct cgraph_node *,
362 void *);
363 struct cgraph_edge_hook_list;
364 struct cgraph_node_hook_list;
365 struct cgraph_2edge_hook_list;
366 struct cgraph_2node_hook_list;
367 struct cgraph_edge_hook_list *cgraph_add_edge_removal_hook (cgraph_edge_hook, void *);
368 void cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *);
369 struct cgraph_node_hook_list *cgraph_add_node_removal_hook (cgraph_node_hook,
370 void *);
371 void cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *);
372 struct cgraph_2edge_hook_list *cgraph_add_edge_duplication_hook (cgraph_2edge_hook, void *);
373 void cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *);
374 struct cgraph_2node_hook_list *cgraph_add_node_duplication_hook (cgraph_2node_hook, void *);
375 void cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *);
376
377 /* In cgraphbuild.c */
378 unsigned int rebuild_cgraph_edges (void);
379
380 /* In ipa.c */
381 bool cgraph_remove_unreachable_nodes (bool, FILE *);
382 int cgraph_postorder (struct cgraph_node **);
383
384 /* In varpool.c */
385
386 extern GTY(()) struct varpool_node *varpool_nodes_queue;
387 extern GTY(()) struct varpool_node *varpool_nodes;
388
389 struct varpool_node *varpool_node (tree);
390 struct varpool_node *varpool_node_for_asm (tree asmname);
391 void varpool_mark_needed_node (struct varpool_node *);
392 void dump_varpool (FILE *);
393 void dump_varpool_node (FILE *, struct varpool_node *);
394
395 void varpool_finalize_decl (tree);
396 bool decide_is_variable_needed (struct varpool_node *, tree);
397 enum availability cgraph_variable_initializer_availability (struct varpool_node *);
398
399 bool varpool_assemble_pending_decls (void);
400 bool varpool_assemble_decl (struct varpool_node *node);
401 bool varpool_analyze_pending_decls (void);
402 void varpool_output_debug_info (void);
403 void varpool_remove_unreferenced_decls (void);
404
405 /* Walk all reachable static variables. */
406 #define FOR_EACH_STATIC_VARIABLE(node) \
407 for ((node) = varpool_nodes_queue; (node); (node) = (node)->next_needed)
408
409 /* Return first reachable static variable with initializer. */
410 static inline struct varpool_node *
411 varpool_first_static_initializer (void)
412 {
413 struct varpool_node *node;
414 for (node = varpool_nodes_queue; node; node = node->next_needed)
415 {
416 gcc_assert (TREE_CODE (node->decl) == VAR_DECL);
417 if (DECL_INITIAL (node->decl))
418 return node;
419 }
420 return NULL;
421 }
422
423 /* Return next reachable static variable with initializer after NODE. */
424 static inline struct varpool_node *
425 varpool_next_static_initializer (struct varpool_node *node)
426 {
427 for (node = node->next_needed; node; node = node->next_needed)
428 {
429 gcc_assert (TREE_CODE (node->decl) == VAR_DECL);
430 if (DECL_INITIAL (node->decl))
431 return node;
432 }
433 return NULL;
434 }
435
436 /* Walk all static variables with initializer set. */
437 #define FOR_EACH_STATIC_INITIALIZER(node) \
438 for ((node) = varpool_first_static_initializer (); (node); \
439 (node) = varpool_next_static_initializer (node))
440
441 /* In ipa-inline.c */
442 void cgraph_clone_inlined_nodes (struct cgraph_edge *, bool, bool);
443 void cgraph_mark_inline_edge (struct cgraph_edge *, bool);
444 bool cgraph_default_inline_p (struct cgraph_node *, const char **);
445 unsigned int compute_inline_parameters (struct cgraph_node *);
446
447
448 /* Create a new static variable of type TYPE. */
449 tree add_new_static_var (tree type);
450
451 #endif /* GCC_CGRAPH_H */