1 /* Callgraph handling code.
2 Copyright (C) 2003-2013 Free Software Foundation, Inc.
3 Contributed by Jan Hubicka
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
25 #include "plugin-api.h"
28 #include "basic-block.h"
32 /* Symbol table consists of functions and variables.
33 TODO: add labels and CONST_DECLs. */
41 /* Base of all entries in the symbol table.
42 The symtab_node is inherited by cgraph and varpol nodes. */
43 struct GTY(()) symtab_node_base
45 /* Type of the symbol. */
46 ENUM_BITFIELD (symtab_type) type : 8;
48 /* The symbols resolution. */
49 ENUM_BITFIELD (ld_plugin_symbol_resolution) resolution : 8;
51 /*** Flags representing the symbol type. ***/
53 /* True when symbol corresponds to a definition in current unit.
54 set via cgraph_finalize_function or varpool_finalize_decl */
55 unsigned definition : 1;
56 /* True when symbol is an alias.
57 Set by assemble_alias. */
59 /* Set once the definition was analyzed. The list of references and
60 other properties are built during analysis. */
61 unsigned analyzed : 1;
64 /*** Visibility and linkage flags. ***/
66 /* Set when function is visible by other units. */
67 unsigned externally_visible : 1;
68 /* Needed variables might become dead by optimization. This flag
69 forces the variable to be output even if it appears dead otherwise. */
70 unsigned force_output : 1;
71 /* True when the name is known to be unique and thus it does not need mangling. */
72 unsigned unique_name : 1;
75 /*** WHOPR Partitioning flags.
76 These flags are used at ltrans stage when only part of the callgraph is
79 /* Set when variable is used from other LTRANS partition. */
80 unsigned used_from_other_partition : 1;
81 /* Set when function is available in the other LTRANS partition.
82 During WPA output it is used to mark nodes that are present in
83 multiple partitions. */
84 unsigned in_other_partition : 1;
88 /*** other flags. ***/
90 /* Set when symbol has address taken. */
91 unsigned address_taken : 1;
94 /* Ordering of all symtab entries. */
97 /* Declaration representing the symbol. */
100 /* Linked list of symbol table entries starting with symtab_nodes. */
102 symtab_node previous;
104 /* Linked list of symbols with the same asm name. There may be multiple
105 entries for single symbol name during LTO, because symbols are renamed
106 only after partitioning.
108 Because inline clones are kept in the assembler name has, they also produce
111 There are also several long standing bugs where frontends and builtin
112 code produce duplicated decls. */
113 symtab_node next_sharing_asm_name;
114 symtab_node previous_sharing_asm_name;
116 /* Circular list of nodes in the same comdat group if non-NULL. */
117 symtab_node same_comdat_group;
119 /* Vectors of referring and referenced entities. */
120 struct ipa_ref_list ref_list;
122 /* File stream where this node is being written to. */
123 struct lto_file_decl_data * lto_file_data;
125 PTR GTY ((skip)) aux;
130 /* Not yet set by cgraph_function_body_availability. */
132 /* Function body/variable initializer is unknown. */
134 /* Function body/variable initializer is known but might be replaced
135 by a different one from other compilation unit and thus needs to
136 be dealt with a care. Like AVAIL_NOT_AVAILABLE it can have
137 arbitrary side effects on escaping variables and functions, while
138 like AVAILABLE it might access static variables. */
140 /* Function body/variable initializer is known and will be used in final
143 /* Function body/variable initializer is known and all it's uses are explicitly
144 visible within current unit (ie it's address is never taken and it is not
145 exported to other units).
146 Currently used only for functions. */
150 /* This is the information that is put into the cgraph local structure
151 to recover a function. */
152 struct lto_file_decl_data;
154 extern const char * const cgraph_availability_names[];
155 extern const char * const ld_plugin_symbol_resolution_names[];
157 /* Information about thunk, used only for same body aliases. */
159 struct GTY(()) cgraph_thunk_info {
160 /* Information about the thunk. */
161 HOST_WIDE_INT fixed_offset;
162 HOST_WIDE_INT virtual_value;
165 bool virtual_offset_p;
166 /* Set to true when alias node is thunk. */
170 /* Information about the function collected locally.
171 Available after function is analyzed. */
173 struct GTY(()) cgraph_local_info {
174 /* Set when function function is visible in current compilation unit only
175 and its address is never taken. */
178 /* False when there is something makes versioning impossible. */
179 unsigned versionable : 1;
181 /* False when function calling convention and signature can not be changed.
182 This is the case when __builtin_apply_args is used. */
183 unsigned can_change_signature : 1;
185 /* True when the function has been originally extern inline, but it is
187 unsigned redefined_extern_inline : 1;
189 /* True if the function may enter serial irrevocable mode. */
190 unsigned tm_may_enter_irr : 1;
193 /* Information about the function that needs to be computed globally
194 once compilation is finished. Available only with -funit-at-a-time. */
196 struct GTY(()) cgraph_global_info {
197 /* For inline clones this points to the function they will be
199 struct cgraph_node *inlined_to;
202 /* Information about the function that is propagated by the RTL backend.
203 Available only for functions that has been already assembled. */
205 struct GTY(()) cgraph_rtl_info {
206 unsigned int preferred_incoming_stack_boundary;
209 /* Represent which DECL tree (or reference to such tree)
210 will be replaced by another tree while versioning. */
211 struct GTY(()) ipa_replace_map
213 /* The tree that will be replaced. */
215 /* The new (replacing) tree. */
217 /* Parameter number to replace, when old_tree is NULL. */
219 /* True when a substitution should be done, false otherwise. */
221 /* True when we replace a reference to old_tree. */
224 typedef struct ipa_replace_map *ipa_replace_map_p;
226 struct GTY(()) cgraph_clone_info
228 vec<ipa_replace_map_p, va_gc> *tree_map;
230 bitmap combined_args_to_skip;
234 /* The cgraph data structure.
235 Each function decl has assigned cgraph_node listing callees and callers. */
237 struct GTY(()) cgraph_node {
238 struct symtab_node_base symbol;
239 struct cgraph_edge *callees;
240 struct cgraph_edge *callers;
241 /* List of edges representing indirect calls with a yet undetermined
243 struct cgraph_edge *indirect_calls;
244 /* For nested functions points to function the node is nested in. */
246 GTY ((nested_ptr (union symtab_node_def, "(struct cgraph_node *)(%h)", "(symtab_node)%h")))
248 /* Points to first nested function, if any. */
250 GTY ((nested_ptr (union symtab_node_def, "(struct cgraph_node *)(%h)", "(symtab_node)%h")))
252 /* Pointer to the next function with same origin, if any. */
254 GTY ((nested_ptr (union symtab_node_def, "(struct cgraph_node *)(%h)", "(symtab_node)%h")))
256 /* Pointer to the next clone. */
257 struct cgraph_node *next_sibling_clone;
258 struct cgraph_node *prev_sibling_clone;
259 struct cgraph_node *clones;
260 struct cgraph_node *clone_of;
261 /* For functions with many calls sites it holds map from call expression
262 to the edge to speed up cgraph_edge function. */
263 htab_t GTY((param_is (struct cgraph_edge))) call_site_hash;
264 /* Declaration node used to be clone of. */
265 tree former_clone_of;
267 /* Interprocedural passes scheduled to have their transform functions
268 applied next time we execute local pass on them. We maintain it
269 per-function in order to allow IPA passes to introduce new functions. */
270 vec<ipa_opt_pass> GTY((skip)) ipa_transforms_to_apply;
272 struct cgraph_local_info local;
273 struct cgraph_global_info global;
274 struct cgraph_rtl_info rtl;
275 struct cgraph_clone_info clone;
276 struct cgraph_thunk_info thunk;
278 /* Expected number of executions: calculated in profile.c. */
280 /* How to scale counts at materialization time; used to merge
281 LTO units with different number of profile runs. */
282 int count_materialization_scale;
283 /* Unique id of the node. */
286 /* Set when decl is an abstract function pointed to by the
287 ABSTRACT_DECL_ORIGIN of a reachable function. */
288 unsigned abstract_and_needed : 1;
289 /* Set once the function is lowered (i.e. its CFG is built). */
290 unsigned lowered : 1;
291 /* Set once the function has been instantiated and its callee
293 unsigned process : 1;
294 /* Set for aliases created as C++ same body aliases. */
295 unsigned same_body_alias : 1;
296 /* How commonly executed the node is. Initialized during branch
297 probabilities pass. */
298 ENUM_BITFIELD (node_frequency) frequency : 2;
299 /* True when function can only be called at startup (from static ctor). */
300 unsigned only_called_at_startup : 1;
301 /* True when function can only be called at startup (from static dtor). */
302 unsigned only_called_at_exit : 1;
303 /* True when function is the transactional clone of a function which
304 is called only from inside transactions. */
305 /* ?? We should be able to remove this. We have enough bits in
306 cgraph to calculate it. */
307 unsigned tm_clone : 1;
308 /* True if this decl is a dispatcher for function versions. */
309 unsigned dispatcher_function : 1;
313 typedef struct cgraph_node *cgraph_node_ptr;
316 /* Function Multiversioning info. */
317 struct GTY(()) cgraph_function_version_info {
318 /* The cgraph_node for which the function version info is stored. */
319 struct cgraph_node *this_node;
320 /* Chains all the semantically identical function versions. The
321 first function in this chain is the version_info node of the
323 struct cgraph_function_version_info *prev;
324 /* If this version node corresponds to a dispatcher for function
325 versions, this points to the version info node of the default
326 function, the first node in the chain. */
327 struct cgraph_function_version_info *next;
328 /* If this node corresponds to a function version, this points
329 to the dispatcher function decl, which is the function that must
330 be called to execute the right function version at run-time.
332 If this cgraph node is a dispatcher (if dispatcher_function is
333 true, in the cgraph_node struct) for function versions, this
334 points to resolver function, which holds the function body of the
335 dispatcher. The dispatcher decl is an alias to the resolver
337 tree dispatcher_resolver;
340 /* Get the cgraph_function_version_info node corresponding to node. */
341 struct cgraph_function_version_info *
342 get_cgraph_node_version (struct cgraph_node *node);
344 /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
345 corresponding to cgraph_node NODE. */
346 struct cgraph_function_version_info *
347 insert_new_cgraph_node_version (struct cgraph_node *node);
349 /* Record that DECL1 and DECL2 are semantically identical function
351 void record_function_versions (tree decl1, tree decl2);
353 /* Remove the cgraph_function_version_info and cgraph_node for DECL. This
354 DECL is a duplicate declaration. */
355 void delete_function_version (tree decl);
357 /* A cgraph node set is a collection of cgraph nodes. A cgraph node
358 can appear in multiple sets. */
359 struct cgraph_node_set_def
361 struct pointer_map_t *map;
362 vec<cgraph_node_ptr> nodes;
365 typedef struct varpool_node *varpool_node_ptr;
368 /* A varpool node set is a collection of varpool nodes. A varpool node
369 can appear in multiple sets. */
370 struct varpool_node_set_def
372 struct pointer_map_t * map;
373 vec<varpool_node_ptr> nodes;
376 typedef struct cgraph_node_set_def *cgraph_node_set;
379 typedef struct varpool_node_set_def *varpool_node_set;
382 /* Iterator structure for cgraph node sets. */
387 } cgraph_node_set_iterator;
389 /* Iterator structure for varpool node sets. */
392 varpool_node_set set;
394 } varpool_node_set_iterator;
396 #define DEFCIFCODE(code, string) CIF_ ## code,
397 /* Reasons for inlining failures. */
398 typedef enum cgraph_inline_failed_enum {
399 #include "cif-code.def"
401 } cgraph_inline_failed_t;
403 /* Structure containing additional information about an indirect call. */
405 struct GTY(()) cgraph_indirect_call_info
407 /* When polymorphic is set, this field contains offset where the object which
408 was actually used in the polymorphic resides within a larger structure.
409 If agg_contents is set, the field contains the offset within the aggregate
410 from which the address to call was loaded. */
411 HOST_WIDE_INT offset;
412 /* OBJ_TYPE_REF_TOKEN of a polymorphic call (if polymorphic is set). */
413 HOST_WIDE_INT otr_token;
414 /* Type of the object from OBJ_TYPE_REF_OBJECT. */
416 /* Index of the parameter that is called. */
418 /* ECF flags determined from the caller. */
421 /* Set when the call is a virtual call with the parameter being the
422 associated object pointer rather than a simple direct call. */
423 unsigned polymorphic : 1;
424 /* Set when the call is a call of a pointer loaded from contents of an
425 aggregate at offset. */
426 unsigned agg_contents : 1;
427 /* When the previous bit is set, this one determines whether the destination
428 is loaded from a parameter passed by reference. */
432 struct GTY((chain_next ("%h.next_caller"), chain_prev ("%h.prev_caller"))) cgraph_edge {
433 /* Expected number of executions: calculated in profile.c. */
435 struct cgraph_node *caller;
436 struct cgraph_node *callee;
437 struct cgraph_edge *prev_caller;
438 struct cgraph_edge *next_caller;
439 struct cgraph_edge *prev_callee;
440 struct cgraph_edge *next_callee;
442 /* Additional information about an indirect call. Not cleared when an edge
444 struct cgraph_indirect_call_info *indirect_info;
445 PTR GTY ((skip (""))) aux;
446 /* When equal to CIF_OK, inline this call. Otherwise, points to the
447 explanation why function was not inlined. */
448 cgraph_inline_failed_t inline_failed;
449 /* The stmt_uid of call_stmt. This is used by LTO to recover the call_stmt
450 when the function is serialized in. */
451 unsigned int lto_stmt_uid;
452 /* Expected frequency of executions within the function.
453 When set to CGRAPH_FREQ_BASE, the edge is expected to be called once
454 per function call. The range is 0 to CGRAPH_FREQ_MAX. */
456 /* Unique id of the edge. */
458 /* Whether this edge was made direct by indirect inlining. */
459 unsigned int indirect_inlining_edge : 1;
460 /* Whether this edge describes an indirect call with an undetermined
462 unsigned int indirect_unknown_callee : 1;
463 /* Whether this edge is still a dangling */
464 /* True if the corresponding CALL stmt cannot be inlined. */
465 unsigned int call_stmt_cannot_inline_p : 1;
466 /* Can this call throw externally? */
467 unsigned int can_throw_external : 1;
470 #define CGRAPH_FREQ_BASE 1000
471 #define CGRAPH_FREQ_MAX 100000
473 typedef struct cgraph_edge *cgraph_edge_p;
476 /* The varpool data structure.
477 Each static variable decl has assigned varpool_node. */
479 struct GTY(()) varpool_node {
480 struct symtab_node_base symbol;
481 /* For aliases points to declaration DECL is alias of. */
484 /* Set when variable is scheduled to be assembled. */
486 unsigned extra_name_alias : 1;
489 /* Every top level asm statement is put into a asm_node. */
491 struct GTY(()) asm_node {
493 struct asm_node *next;
494 /* String for this asm node. */
496 /* Ordering of all cgraph nodes. */
500 /* Symbol table entry. */
501 union GTY((desc ("%h.symbol.type"), chain_next ("%h.symbol.next"),
502 chain_prev ("%h.symbol.previous"))) symtab_node_def {
503 struct symtab_node_base GTY ((tag ("SYMTAB_SYMBOL"))) symbol;
504 /* To access the following fields,
505 use the use dyn_cast or as_a to obtain the concrete type. */
506 struct cgraph_node GTY ((tag ("SYMTAB_FUNCTION"))) x_function;
507 struct varpool_node GTY ((tag ("SYMTAB_VARIABLE"))) x_variable;
510 /* Report whether or not THIS symtab node is a function, aka cgraph_node. */
515 is_a_helper <cgraph_node>::test (symtab_node_def *p)
517 return p->symbol.type == SYMTAB_FUNCTION;
520 /* Report whether or not THIS symtab node is a vriable, aka varpool_node. */
525 is_a_helper <varpool_node>::test (symtab_node_def *p)
527 return p->symbol.type == SYMTAB_VARIABLE;
530 extern GTY(()) symtab_node symtab_nodes;
531 extern GTY(()) int cgraph_n_nodes;
532 extern GTY(()) int cgraph_max_uid;
533 extern GTY(()) int cgraph_edge_max_uid;
534 extern bool cgraph_global_info_ready;
537 /* Frontend is parsing and finalizing functions. */
538 CGRAPH_STATE_PARSING,
539 /* Callgraph is being constructed. It is safe to add new functions. */
540 CGRAPH_STATE_CONSTRUCTION,
541 /* Callgraph is built and IPA passes are being run. */
543 /* Callgraph is built and all functions are transformed to SSA form. */
544 CGRAPH_STATE_IPA_SSA,
545 /* Functions are now ordered and being passed to RTL expanders. */
546 CGRAPH_STATE_EXPANSION,
547 /* All cgraph expansion is done. */
548 CGRAPH_STATE_FINISHED
550 extern enum cgraph_state cgraph_state;
551 extern bool cgraph_function_flags_ready;
552 extern cgraph_node_set cgraph_new_nodes;
554 extern GTY(()) struct asm_node *asm_nodes;
555 extern GTY(()) int symtab_order;
556 extern bool same_body_aliases_done;
559 void symtab_register_node (symtab_node);
560 void symtab_unregister_node (symtab_node);
561 void symtab_remove_node (symtab_node);
562 symtab_node symtab_get_node (const_tree);
563 symtab_node symtab_node_for_asm (const_tree asmname);
564 const char * symtab_node_asm_name (symtab_node);
565 const char * symtab_node_name (symtab_node);
566 void symtab_insert_node_to_hashtable (symtab_node);
567 void symtab_add_to_same_comdat_group (symtab_node, symtab_node);
568 void symtab_dissolve_same_comdat_group_list (symtab_node node);
569 void dump_symtab (FILE *);
570 void debug_symtab (void);
571 void dump_symtab_node (FILE *, symtab_node);
572 void debug_symtab_node (symtab_node);
573 void dump_symtab_base (FILE *, symtab_node);
574 void verify_symtab (void);
575 void verify_symtab_node (symtab_node);
576 bool verify_symtab_base (symtab_node);
577 bool symtab_used_from_object_file_p (symtab_node);
578 void symtab_make_decl_local (tree);
579 symtab_node symtab_alias_ultimate_target (symtab_node, enum availability *);
582 void dump_cgraph (FILE *);
583 void debug_cgraph (void);
584 void dump_cgraph_node (FILE *, struct cgraph_node *);
585 void debug_cgraph_node (struct cgraph_node *);
586 void cgraph_remove_edge (struct cgraph_edge *);
587 void cgraph_remove_node (struct cgraph_node *);
588 void cgraph_release_function_body (struct cgraph_node *);
589 void cgraph_node_remove_callees (struct cgraph_node *node);
590 struct cgraph_edge *cgraph_create_edge (struct cgraph_node *,
591 struct cgraph_node *,
592 gimple, gcov_type, int);
593 struct cgraph_edge *cgraph_create_indirect_edge (struct cgraph_node *, gimple,
594 int, gcov_type, int);
595 struct cgraph_indirect_call_info *cgraph_allocate_init_indirect_info (void);
596 struct cgraph_node * cgraph_create_node (tree);
597 struct cgraph_node * cgraph_create_empty_node (void);
598 struct cgraph_node * cgraph_get_create_node (tree);
599 struct cgraph_node * cgraph_get_create_real_symbol_node (tree);
600 struct cgraph_node * cgraph_same_body_alias (struct cgraph_node *, tree, tree);
601 struct cgraph_node * cgraph_add_thunk (struct cgraph_node *, tree, tree, bool, HOST_WIDE_INT,
602 HOST_WIDE_INT, tree, tree);
603 struct cgraph_node *cgraph_node_for_asm (tree);
604 struct cgraph_edge *cgraph_edge (struct cgraph_node *, gimple);
605 void cgraph_set_call_stmt (struct cgraph_edge *, gimple);
606 void cgraph_update_edges_for_call_stmt (gimple, tree, gimple);
607 struct cgraph_local_info *cgraph_local_info (tree);
608 struct cgraph_global_info *cgraph_global_info (tree);
609 struct cgraph_rtl_info *cgraph_rtl_info (tree);
610 struct cgraph_node *cgraph_create_function_alias (tree, tree);
611 void cgraph_call_node_duplication_hooks (struct cgraph_node *,
612 struct cgraph_node *);
613 void cgraph_call_edge_duplication_hooks (struct cgraph_edge *,
614 struct cgraph_edge *);
616 void cgraph_redirect_edge_callee (struct cgraph_edge *, struct cgraph_node *);
617 void cgraph_make_edge_direct (struct cgraph_edge *, struct cgraph_node *);
618 bool cgraph_only_called_directly_p (struct cgraph_node *);
620 bool cgraph_function_possibly_inlined_p (tree);
621 void cgraph_unnest_node (struct cgraph_node *);
623 enum availability cgraph_function_body_availability (struct cgraph_node *);
624 void cgraph_add_new_function (tree, bool);
625 const char* cgraph_inline_failed_string (cgraph_inline_failed_t);
627 void cgraph_set_nothrow_flag (struct cgraph_node *, bool);
628 void cgraph_set_const_flag (struct cgraph_node *, bool, bool);
629 void cgraph_set_pure_flag (struct cgraph_node *, bool, bool);
630 bool cgraph_node_cannot_return (struct cgraph_node *);
631 bool cgraph_edge_cannot_lead_to_return (struct cgraph_edge *);
632 bool cgraph_will_be_removed_from_program_if_no_direct_calls
633 (struct cgraph_node *node);
634 bool cgraph_can_remove_if_no_direct_calls_and_refs_p
635 (struct cgraph_node *node);
636 bool cgraph_can_remove_if_no_direct_calls_p (struct cgraph_node *node);
637 bool resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution);
638 bool cgraph_for_node_thunks_and_aliases (struct cgraph_node *,
639 bool (*) (struct cgraph_node *, void *),
642 bool cgraph_for_node_and_aliases (struct cgraph_node *,
643 bool (*) (struct cgraph_node *, void *),
645 vec<cgraph_edge_p> collect_callers_of_node (struct cgraph_node *node);
646 void verify_cgraph (void);
647 void verify_cgraph_node (struct cgraph_node *);
648 void cgraph_mark_address_taken_node (struct cgraph_node *);
650 typedef void (*cgraph_edge_hook)(struct cgraph_edge *, void *);
651 typedef void (*cgraph_node_hook)(struct cgraph_node *, void *);
652 typedef void (*cgraph_2edge_hook)(struct cgraph_edge *, struct cgraph_edge *,
654 typedef void (*cgraph_2node_hook)(struct cgraph_node *, struct cgraph_node *,
656 struct cgraph_edge_hook_list;
657 struct cgraph_node_hook_list;
658 struct cgraph_2edge_hook_list;
659 struct cgraph_2node_hook_list;
660 struct cgraph_edge_hook_list *cgraph_add_edge_removal_hook (cgraph_edge_hook, void *);
661 void cgraph_remove_edge_removal_hook (struct cgraph_edge_hook_list *);
662 struct cgraph_node_hook_list *cgraph_add_node_removal_hook (cgraph_node_hook,
664 void cgraph_remove_node_removal_hook (struct cgraph_node_hook_list *);
665 struct cgraph_node_hook_list *cgraph_add_function_insertion_hook (cgraph_node_hook,
667 void cgraph_remove_function_insertion_hook (struct cgraph_node_hook_list *);
668 void cgraph_call_function_insertion_hooks (struct cgraph_node *node);
669 struct cgraph_2edge_hook_list *cgraph_add_edge_duplication_hook (cgraph_2edge_hook, void *);
670 void cgraph_remove_edge_duplication_hook (struct cgraph_2edge_hook_list *);
671 struct cgraph_2node_hook_list *cgraph_add_node_duplication_hook (cgraph_2node_hook, void *);
672 void cgraph_remove_node_duplication_hook (struct cgraph_2node_hook_list *);
673 gimple cgraph_redirect_edge_call_stmt_to_callee (struct cgraph_edge *);
674 bool cgraph_propagate_frequency (struct cgraph_node *node);
675 struct cgraph_node * cgraph_function_node (struct cgraph_node *, enum availability *);
677 /* In cgraphunit.c */
678 struct asm_node *add_asm_node (tree);
679 extern FILE *cgraph_dump_file;
680 void cgraph_finalize_function (tree, bool);
681 void finalize_compilation_unit (void);
683 void init_cgraph (void);
684 bool cgraph_process_new_functions (void);
685 void cgraph_process_same_body_aliases (void);
686 void fixup_same_cpp_alias_visibility (symtab_node, symtab_node target, tree);
687 /* Initialize datastructures so DECL is a function in lowered gimple form.
688 IN_SSA is true if the gimple is in SSA. */
689 basic_block init_lowered_empty_function (tree, bool);
690 void cgraph_reset_node (struct cgraph_node *);
692 /* In cgraphclones.c */
694 struct cgraph_edge * cgraph_clone_edge (struct cgraph_edge *,
695 struct cgraph_node *, gimple,
696 unsigned, gcov_type, int, bool);
697 struct cgraph_node * cgraph_clone_node (struct cgraph_node *, tree, gcov_type,
698 int, bool, vec<cgraph_edge_p>,
700 tree clone_function_name (tree decl, const char *);
701 struct cgraph_node * cgraph_create_virtual_clone (struct cgraph_node *old_node,
703 vec<ipa_replace_map_p, va_gc> *tree_map,
705 const char *clone_name);
706 struct cgraph_node *cgraph_find_replacement_node (struct cgraph_node *);
707 bool cgraph_remove_node_and_inline_clones (struct cgraph_node *, struct cgraph_node *);
708 void cgraph_set_call_stmt_including_clones (struct cgraph_node *, gimple, gimple);
709 void cgraph_create_edge_including_clones (struct cgraph_node *,
710 struct cgraph_node *,
711 gimple, gimple, gcov_type, int,
712 cgraph_inline_failed_t);
713 void cgraph_materialize_all_clones (void);
714 struct cgraph_node * cgraph_copy_node_for_versioning (struct cgraph_node *,
715 tree, vec<cgraph_edge_p>, bitmap);
716 struct cgraph_node *cgraph_function_versioning (struct cgraph_node *,
718 vec<ipa_replace_map_p, va_gc> *,
719 bitmap, bool, bitmap,
720 basic_block, const char *);
721 void tree_function_versioning (tree, tree, vec<ipa_replace_map_p, va_gc> *,
722 bool, bitmap, bool, bitmap, basic_block);
724 /* In cgraphbuild.c */
725 unsigned int rebuild_cgraph_edges (void);
726 void cgraph_rebuild_references (void);
727 int compute_call_stmt_bb_frequency (tree, basic_block bb);
728 void record_references_in_initializer (tree, bool);
729 void ipa_record_stmt_references (struct cgraph_node *, gimple);
732 bool symtab_remove_unreachable_nodes (bool, FILE *);
733 cgraph_node_set cgraph_node_set_new (void);
734 cgraph_node_set_iterator cgraph_node_set_find (cgraph_node_set,
735 struct cgraph_node *);
736 void cgraph_node_set_add (cgraph_node_set, struct cgraph_node *);
737 void cgraph_node_set_remove (cgraph_node_set, struct cgraph_node *);
738 void dump_cgraph_node_set (FILE *, cgraph_node_set);
739 void debug_cgraph_node_set (cgraph_node_set);
740 void free_cgraph_node_set (cgraph_node_set);
741 void cgraph_build_static_cdtor (char which, tree body, int priority);
743 varpool_node_set varpool_node_set_new (void);
744 varpool_node_set_iterator varpool_node_set_find (varpool_node_set,
745 struct varpool_node *);
746 void varpool_node_set_add (varpool_node_set, struct varpool_node *);
747 void varpool_node_set_remove (varpool_node_set, struct varpool_node *);
748 void dump_varpool_node_set (FILE *, varpool_node_set);
749 void debug_varpool_node_set (varpool_node_set);
750 void free_varpool_node_set (varpool_node_set);
751 void ipa_discover_readonly_nonaddressable_vars (void);
752 bool varpool_externally_visible_p (struct varpool_node *);
755 bool cgraph_maybe_hot_edge_p (struct cgraph_edge *e);
756 bool cgraph_optimize_for_size_p (struct cgraph_node *);
759 struct varpool_node *varpool_node_for_decl (tree);
760 struct varpool_node *varpool_node_for_asm (tree asmname);
761 void varpool_mark_needed_node (struct varpool_node *);
762 void debug_varpool (void);
763 void dump_varpool (FILE *);
764 void dump_varpool_node (FILE *, struct varpool_node *);
766 void varpool_finalize_decl (tree);
767 bool decide_is_variable_needed (struct varpool_node *, tree);
768 enum availability cgraph_variable_initializer_availability (struct varpool_node *);
769 void cgraph_make_node_local (struct cgraph_node *);
770 bool cgraph_node_can_be_local_p (struct cgraph_node *);
773 void varpool_remove_node (struct varpool_node *node);
774 void varpool_finalize_named_section_flags (struct varpool_node *node);
775 bool varpool_output_variables (void);
776 bool varpool_assemble_decl (struct varpool_node *node);
777 void varpool_analyze_node (struct varpool_node *);
778 struct varpool_node * varpool_extra_name_alias (tree, tree);
779 struct varpool_node * varpool_create_variable_alias (tree, tree);
780 void varpool_reset_queue (void);
781 bool const_value_known_p (tree);
782 bool varpool_for_node_and_aliases (struct varpool_node *,
783 bool (*) (struct varpool_node *, void *),
785 void varpool_add_new_variable (tree);
786 void symtab_initialize_asm_name_hash (void);
787 void symtab_prevail_in_asm_name_hash (symtab_node node);
788 void varpool_remove_initializer (struct varpool_node *);
791 /* Return callgraph node for given symbol and check it is a function. */
792 static inline struct cgraph_node *
793 cgraph (symtab_node node)
795 gcc_checking_assert (!node || node->symbol.type == SYMTAB_FUNCTION);
796 return (struct cgraph_node *)node;
799 /* Return varpool node for given symbol and check it is a variable. */
800 static inline struct varpool_node *
801 varpool (symtab_node node)
803 gcc_checking_assert (!node || node->symbol.type == SYMTAB_VARIABLE);
804 return (struct varpool_node *)node;
807 /* Return callgraph node for given symbol and check it is a function. */
808 static inline struct cgraph_node *
809 cgraph_get_node (const_tree decl)
811 gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL);
812 return cgraph (symtab_get_node (decl));
815 /* Return varpool node for given symbol and check it is a function. */
816 static inline struct varpool_node *
817 varpool_get_node (const_tree decl)
819 gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
820 return varpool (symtab_get_node (decl));
823 /* Return asm name of cgraph node. */
824 static inline const char *
825 cgraph_node_asm_name(struct cgraph_node *node)
827 return symtab_node_asm_name ((symtab_node)node);
830 /* Return asm name of varpool node. */
831 static inline const char *
832 varpool_node_asm_name(struct varpool_node *node)
834 return symtab_node_asm_name ((symtab_node)node);
837 /* Return name of cgraph node. */
838 static inline const char *
839 cgraph_node_name(struct cgraph_node *node)
841 return symtab_node_name ((symtab_node)node);
844 /* Return name of varpool node. */
845 static inline const char *
846 varpool_node_name(struct varpool_node *node)
848 return symtab_node_name ((symtab_node)node);
851 /* Walk all symbols. */
852 #define FOR_EACH_SYMBOL(node) \
853 for ((node) = symtab_nodes; (node); (node) = (node)->symbol.next)
856 /* Return first variable. */
857 static inline struct varpool_node *
858 varpool_first_variable (void)
861 for (node = symtab_nodes; node; node = node->symbol.next)
862 if (varpool_node *vnode = dyn_cast <varpool_node> (node))
867 /* Return next variable after NODE. */
868 static inline struct varpool_node *
869 varpool_next_variable (struct varpool_node *node)
871 symtab_node node1 = (symtab_node) node->symbol.next;
872 for (; node1; node1 = node1->symbol.next)
873 if (varpool_node *vnode1 = dyn_cast <varpool_node> (node1))
877 /* Walk all variables. */
878 #define FOR_EACH_VARIABLE(node) \
879 for ((node) = varpool_first_variable (); \
881 (node) = varpool_next_variable ((node)))
883 /* Return first reachable static variable with initializer. */
884 static inline struct varpool_node *
885 varpool_first_static_initializer (void)
888 for (node = symtab_nodes; node; node = node->symbol.next)
890 varpool_node *vnode = dyn_cast <varpool_node> (node);
891 if (vnode && DECL_INITIAL (node->symbol.decl))
897 /* Return next reachable static variable with initializer after NODE. */
898 static inline struct varpool_node *
899 varpool_next_static_initializer (struct varpool_node *node)
901 symtab_node node1 = (symtab_node) node->symbol.next;
902 for (; node1; node1 = node1->symbol.next)
904 varpool_node *vnode1 = dyn_cast <varpool_node> (node1);
905 if (vnode1 && DECL_INITIAL (node1->symbol.decl))
911 /* Walk all static variables with initializer set. */
912 #define FOR_EACH_STATIC_INITIALIZER(node) \
913 for ((node) = varpool_first_static_initializer (); (node); \
914 (node) = varpool_next_static_initializer (node))
916 /* Return first reachable static variable with initializer. */
917 static inline struct varpool_node *
918 varpool_first_defined_variable (void)
921 for (node = symtab_nodes; node; node = node->symbol.next)
923 varpool_node *vnode = dyn_cast <varpool_node> (node);
924 if (vnode && vnode->symbol.definition)
930 /* Return next reachable static variable with initializer after NODE. */
931 static inline struct varpool_node *
932 varpool_next_defined_variable (struct varpool_node *node)
934 symtab_node node1 = (symtab_node) node->symbol.next;
935 for (; node1; node1 = node1->symbol.next)
937 varpool_node *vnode1 = dyn_cast <varpool_node> (node1);
938 if (vnode1 && vnode1->symbol.definition)
943 /* Walk all variables with definitions in current unit. */
944 #define FOR_EACH_DEFINED_VARIABLE(node) \
945 for ((node) = varpool_first_defined_variable (); (node); \
946 (node) = varpool_next_defined_variable (node))
948 /* Return first function with body defined. */
949 static inline struct cgraph_node *
950 cgraph_first_defined_function (void)
953 for (node = symtab_nodes; node; node = node->symbol.next)
955 cgraph_node *cn = dyn_cast <cgraph_node> (node);
956 if (cn && cn->symbol.definition)
962 /* Return next function with body defined after NODE. */
963 static inline struct cgraph_node *
964 cgraph_next_defined_function (struct cgraph_node *node)
966 symtab_node node1 = (symtab_node) node->symbol.next;
967 for (; node1; node1 = node1->symbol.next)
969 cgraph_node *cn1 = dyn_cast <cgraph_node> (node1);
970 if (cn1 && cn1->symbol.definition)
976 /* Walk all functions with body defined. */
977 #define FOR_EACH_DEFINED_FUNCTION(node) \
978 for ((node) = cgraph_first_defined_function (); (node); \
979 (node) = cgraph_next_defined_function ((node)))
981 /* Return first function. */
982 static inline struct cgraph_node *
983 cgraph_first_function (void)
986 for (node = symtab_nodes; node; node = node->symbol.next)
987 if (cgraph_node *cn = dyn_cast <cgraph_node> (node))
992 /* Return next function. */
993 static inline struct cgraph_node *
994 cgraph_next_function (struct cgraph_node *node)
996 symtab_node node1 = (symtab_node) node->symbol.next;
997 for (; node1; node1 = node1->symbol.next)
998 if (cgraph_node *cn1 = dyn_cast <cgraph_node> (node1))
1002 /* Walk all functions. */
1003 #define FOR_EACH_FUNCTION(node) \
1004 for ((node) = cgraph_first_function (); (node); \
1005 (node) = cgraph_next_function ((node)))
1007 /* Return true when NODE is a function with Gimple body defined
1008 in current unit. Functions can also be define externally or they
1009 can be thunks with no Gimple representation.
1011 Note that at WPA stage, the function body may not be present in memory. */
1014 cgraph_function_with_gimple_body_p (struct cgraph_node *node)
1016 return node->symbol.definition && !node->thunk.thunk_p && !node->symbol.alias;
1019 /* Return first function with body defined. */
1020 static inline struct cgraph_node *
1021 cgraph_first_function_with_gimple_body (void)
1024 for (node = symtab_nodes; node; node = node->symbol.next)
1026 cgraph_node *cn = dyn_cast <cgraph_node> (node);
1027 if (cn && cgraph_function_with_gimple_body_p (cn))
1033 /* Return next reachable static variable with initializer after NODE. */
1034 static inline struct cgraph_node *
1035 cgraph_next_function_with_gimple_body (struct cgraph_node *node)
1037 symtab_node node1 = node->symbol.next;
1038 for (; node1; node1 = node1->symbol.next)
1040 cgraph_node *cn1 = dyn_cast <cgraph_node> (node1);
1041 if (cn1 && cgraph_function_with_gimple_body_p (cn1))
1047 /* Walk all functions with body defined. */
1048 #define FOR_EACH_FUNCTION_WITH_GIMPLE_BODY(node) \
1049 for ((node) = cgraph_first_function_with_gimple_body (); (node); \
1050 (node) = cgraph_next_function_with_gimple_body (node))
1052 /* Create a new static variable of type TYPE. */
1053 tree add_new_static_var (tree type);
1055 /* Return true if iterator CSI points to nothing. */
1057 csi_end_p (cgraph_node_set_iterator csi)
1059 return csi.index >= csi.set->nodes.length ();
1062 /* Advance iterator CSI. */
1064 csi_next (cgraph_node_set_iterator *csi)
1069 /* Return the node pointed to by CSI. */
1070 static inline struct cgraph_node *
1071 csi_node (cgraph_node_set_iterator csi)
1073 return csi.set->nodes[csi.index];
1076 /* Return an iterator to the first node in SET. */
1077 static inline cgraph_node_set_iterator
1078 csi_start (cgraph_node_set set)
1080 cgraph_node_set_iterator csi;
1087 /* Return true if SET contains NODE. */
1089 cgraph_node_in_set_p (struct cgraph_node *node, cgraph_node_set set)
1091 cgraph_node_set_iterator csi;
1092 csi = cgraph_node_set_find (set, node);
1093 return !csi_end_p (csi);
1096 /* Return number of nodes in SET. */
1097 static inline size_t
1098 cgraph_node_set_size (cgraph_node_set set)
1100 return set->nodes.length ();
1103 /* Return true if iterator VSI points to nothing. */
1105 vsi_end_p (varpool_node_set_iterator vsi)
1107 return vsi.index >= vsi.set->nodes.length ();
1110 /* Advance iterator VSI. */
1112 vsi_next (varpool_node_set_iterator *vsi)
1117 /* Return the node pointed to by VSI. */
1118 static inline struct varpool_node *
1119 vsi_node (varpool_node_set_iterator vsi)
1121 return vsi.set->nodes[vsi.index];
1124 /* Return an iterator to the first node in SET. */
1125 static inline varpool_node_set_iterator
1126 vsi_start (varpool_node_set set)
1128 varpool_node_set_iterator vsi;
1135 /* Return true if SET contains NODE. */
1137 varpool_node_in_set_p (struct varpool_node *node, varpool_node_set set)
1139 varpool_node_set_iterator vsi;
1140 vsi = varpool_node_set_find (set, node);
1141 return !vsi_end_p (vsi);
1144 /* Return number of nodes in SET. */
1145 static inline size_t
1146 varpool_node_set_size (varpool_node_set set)
1148 return set->nodes.length ();
1151 /* Uniquize all constants that appear in memory.
1152 Each constant in memory thus far output is recorded
1153 in `const_desc_table'. */
1155 struct GTY(()) constant_descriptor_tree {
1156 /* A MEM for the constant. */
1159 /* The value of the constant. */
1162 /* Hash of value. Computing the hash from value each time
1163 hashfn is called can't work properly, as that means recursive
1164 use of the hash table during hash table expansion. */
1168 /* Return true if set is nonempty. */
1170 cgraph_node_set_nonempty_p (cgraph_node_set set)
1172 return !set->nodes.is_empty ();
1175 /* Return true if set is nonempty. */
1177 varpool_node_set_nonempty_p (varpool_node_set set)
1179 return !set->nodes.is_empty ();
1182 /* Return true when function NODE is only called directly or it has alias.
1183 i.e. it is not externally visible, address was not taken and
1184 it is not used in any other non-standard way. */
1187 cgraph_only_called_directly_or_aliased_p (struct cgraph_node *node)
1189 gcc_assert (!node->global.inlined_to);
1190 return (!node->symbol.force_output && !node->symbol.address_taken
1191 && !node->symbol.used_from_other_partition
1192 && !DECL_VIRTUAL_P (node->symbol.decl)
1193 && !DECL_STATIC_CONSTRUCTOR (node->symbol.decl)
1194 && !DECL_STATIC_DESTRUCTOR (node->symbol.decl)
1195 && !node->symbol.externally_visible);
1198 /* Return true when function NODE can be removed from callgraph
1199 if all direct calls are eliminated. */
1202 varpool_can_remove_if_no_refs (struct varpool_node *node)
1204 if (DECL_EXTERNAL (node->symbol.decl))
1206 return (!node->symbol.force_output && !node->symbol.used_from_other_partition
1207 && ((DECL_COMDAT (node->symbol.decl)
1208 && !symtab_used_from_object_file_p ((symtab_node) node))
1209 || !node->symbol.externally_visible
1210 || DECL_HAS_VALUE_EXPR_P (node->symbol.decl)));
1213 /* Return true when all references to VNODE must be visible in ipa_ref_list.
1214 i.e. if the variable is not externally visible or not used in some magic
1215 way (asm statement or such).
1216 The magic uses are all summarized in force_output flag. */
1219 varpool_all_refs_explicit_p (struct varpool_node *vnode)
1221 return (vnode->symbol.definition
1222 && !vnode->symbol.externally_visible
1223 && !vnode->symbol.used_from_other_partition
1224 && !vnode->symbol.force_output);
1227 /* Constant pool accessor function. */
1228 htab_t constant_pool_htab (void);
1230 /* FIXME: inappropriate dependency of cgraph on IPA. */
1231 #include "ipa-ref-inline.h"
1233 /* Return node that alias N is aliasing. */
1235 static inline symtab_node
1236 symtab_alias_target (symtab_node n)
1238 struct ipa_ref *ref;
1239 ipa_ref_list_reference_iterate (&n->symbol.ref_list, 0, ref);
1240 gcc_checking_assert (ref->use == IPA_REF_ALIAS);
1241 return ref->referred;
1244 static inline struct cgraph_node *
1245 cgraph_alias_target (struct cgraph_node *n)
1247 return dyn_cast <cgraph_node> (symtab_alias_target ((symtab_node) n));
1250 static inline struct varpool_node *
1251 varpool_alias_target (struct varpool_node *n)
1253 return dyn_cast <varpool_node> (symtab_alias_target ((symtab_node) n));
1256 /* Given NODE, walk the alias chain to return the function NODE is alias of.
1257 Do not walk through thunks.
1258 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
1260 static inline struct cgraph_node *
1261 cgraph_function_or_thunk_node (struct cgraph_node *node, enum availability *availability)
1263 struct cgraph_node *n;
1265 n = dyn_cast <cgraph_node> (symtab_alias_ultimate_target ((symtab_node)node, availability));
1267 *availability = AVAIL_NOT_AVAILABLE;
1270 /* Given NODE, walk the alias chain to return the function NODE is alias of.
1271 Do not walk through thunks.
1272 When AVAILABILITY is non-NULL, get minimal availability in the chain. */
1274 static inline struct varpool_node *
1275 varpool_variable_node (struct varpool_node *node, enum availability *availability)
1277 struct varpool_node *n;
1279 n = dyn_cast <varpool_node> (symtab_alias_ultimate_target ((symtab_node)node, availability));
1281 *availability = AVAIL_NOT_AVAILABLE;
1285 /* Return true when the edge E represents a direct recursion. */
1287 cgraph_edge_recursive_p (struct cgraph_edge *e)
1289 struct cgraph_node *callee = cgraph_function_or_thunk_node (e->callee, NULL);
1290 if (e->caller->global.inlined_to)
1291 return e->caller->global.inlined_to->symbol.decl == callee->symbol.decl;
1293 return e->caller->symbol.decl == callee->symbol.decl;
1296 /* Return true if the TM_CLONE bit is set for a given FNDECL. */
1298 decl_is_tm_clone (const_tree fndecl)
1300 struct cgraph_node *n = cgraph_get_node (fndecl);
1306 /* Likewise indicate that a node is needed, i.e. reachable via some
1310 cgraph_mark_force_output_node (struct cgraph_node *node)
1312 node->symbol.force_output = 1;
1313 gcc_checking_assert (!node->global.inlined_to);
1316 /* Return true when the symbol is real symbol, i.e. it is not inline clone
1317 or abstract function kept for debug info purposes only. */
1320 symtab_real_symbol_p (symtab_node node)
1322 struct cgraph_node *cnode;
1324 if (!is_a <cgraph_node> (node))
1326 cnode = cgraph (node);
1327 if (cnode->global.inlined_to)
1329 if (cnode->abstract_and_needed)
1333 #endif /* GCC_CGRAPH_H */