1/* Callgraph handling code.
2   Copyright (C) 2003-2015 Free Software Foundation, Inc.
3   Contributed by Jan Hubicka
4
5This file is part of GCC.
6
7GCC is free software; you can redistribute it and/or modify it under
8the terms of the GNU General Public License as published by the Free
9Software Foundation; either version 3, or (at your option) any later
10version.
11
12GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or
14FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15for more details.
16
17You should have received a copy of the GNU General Public License
18along with GCC; see the file COPYING3.  If not see
19<http://www.gnu.org/licenses/>.  */
20
21#ifndef GCC_CGRAPH_H
22#define GCC_CGRAPH_H
23
24
25/* Symbol table consists of functions and variables.
26   TODO: add labels and CONST_DECLs.  */
27enum symtab_type
28{
29  SYMTAB_SYMBOL,
30  SYMTAB_FUNCTION,
31  SYMTAB_VARIABLE
32};
33
34/* Section names are stored as reference counted strings in GGC safe hashtable
35   (to make them survive through PCH).  */
36
37struct GTY((for_user)) section_hash_entry_d
38{
39  int ref_count;
40  char *name;  /* As long as this datastructure stays in GGC, we can not put
41		  string at the tail of structure of GGC dies in horrible
42		  way  */
43};
44
45typedef struct section_hash_entry_d section_hash_entry;
46
47struct section_name_hasher : ggc_hasher<section_hash_entry *>
48{
49  typedef const char *compare_type;
50
51  static hashval_t hash (section_hash_entry *);
52  static bool equal (section_hash_entry *, const char *);
53};
54
55enum availability
56{
57  /* Not yet set by cgraph_function_body_availability.  */
58  AVAIL_UNSET,
59  /* Function body/variable initializer is unknown.  */
60  AVAIL_NOT_AVAILABLE,
61  /* Function body/variable initializer is known but might be replaced
62     by a different one from other compilation unit and thus needs to
63     be dealt with a care.  Like AVAIL_NOT_AVAILABLE it can have
64     arbitrary side effects on escaping variables and functions, while
65     like AVAILABLE it might access static variables.  */
66  AVAIL_INTERPOSABLE,
67  /* Function body/variable initializer is known and will be used in final
68     program.  */
69  AVAIL_AVAILABLE,
70  /* Function body/variable initializer is known and all it's uses are
71     explicitly visible within current unit (ie it's address is never taken and
72     it is not exported to other units). Currently used only for functions.  */
73  AVAIL_LOCAL
74};
75
76/* Classification of symbols WRT partitioning.  */
77enum symbol_partitioning_class
78{
79   /* External declarations are ignored by partitioning algorithms and they are
80      added into the boundary later via compute_ltrans_boundary.  */
81   SYMBOL_EXTERNAL,
82   /* Partitioned symbols are pur into one of partitions.  */
83   SYMBOL_PARTITION,
84   /* Duplicated symbols (such as comdat or constant pool references) are
85      copied into every node needing them via add_symbol_to_partition.  */
86   SYMBOL_DUPLICATE
87};
88
89/* Base of all entries in the symbol table.
90   The symtab_node is inherited by cgraph and varpol nodes.  */
91class GTY((desc ("%h.type"), tag ("SYMTAB_SYMBOL"),
92	   chain_next ("%h.next"), chain_prev ("%h.previous")))
93  symtab_node
94{
95public:
96  /* Return name.  */
97  const char *name () const;
98
99  /* Return asm name.  */
100  const char * asm_name () const;
101
102  /* Add node into symbol table.  This function is not used directly, but via
103     cgraph/varpool node creation routines.  */
104  void register_symbol (void);
105
106  /* Remove symbol from symbol table.  */
107  void remove (void);
108
109  /* Dump symtab node to F.  */
110  void dump (FILE *f);
111
112  /* Dump symtab node to stderr.  */
113  void DEBUG_FUNCTION debug (void);
114
115  /* Verify consistency of node.  */
116  void DEBUG_FUNCTION verify (void);
117
118  /* Return ipa reference from this symtab_node to
119     REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
120     of the use and STMT the statement (if it exists).  */
121  ipa_ref *create_reference (symtab_node *referred_node,
122			     enum ipa_ref_use use_type);
123
124  /* Return ipa reference from this symtab_node to
125     REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
126     of the use and STMT the statement (if it exists).  */
127  ipa_ref *create_reference (symtab_node *referred_node,
128			     enum ipa_ref_use use_type, gimple stmt);
129
130  /* If VAL is a reference to a function or a variable, add a reference from
131     this symtab_node to the corresponding symbol table node.  USE_TYPE specify
132     type of the use and STMT the statement (if it exists).  Return the new
133     reference or NULL if none was created.  */
134  ipa_ref *maybe_create_reference (tree val, enum ipa_ref_use use_type,
135				   gimple stmt);
136
137  /* Clone all references from symtab NODE to this symtab_node.  */
138  void clone_references (symtab_node *node);
139
140  /* Remove all stmt references in non-speculative references.
141     Those are not maintained during inlining & clonning.
142     The exception are speculative references that are updated along
143     with callgraph edges associated with them.  */
144  void clone_referring (symtab_node *node);
145
146  /* Clone reference REF to this symtab_node and set its stmt to STMT.  */
147  ipa_ref *clone_reference (ipa_ref *ref, gimple stmt);
148
149  /* Find the structure describing a reference to REFERRED_NODE
150     and associated with statement STMT.  */
151  ipa_ref *find_reference (symtab_node *referred_node, gimple stmt,
152			   unsigned int lto_stmt_uid);
153
154  /* Remove all references that are associated with statement STMT.  */
155  void remove_stmt_references (gimple stmt);
156
157  /* Remove all stmt references in non-speculative references.
158     Those are not maintained during inlining & clonning.
159     The exception are speculative references that are updated along
160     with callgraph edges associated with them.  */
161  void clear_stmts_in_references (void);
162
163  /* Remove all references in ref list.  */
164  void remove_all_references (void);
165
166  /* Remove all referring items in ref list.  */
167  void remove_all_referring (void);
168
169  /* Dump references in ref list to FILE.  */
170  void dump_references (FILE *file);
171
172  /* Dump referring in list to FILE.  */
173  void dump_referring (FILE *);
174
175  /* Get number of references for this node.  */
176  inline unsigned num_references (void)
177  {
178    return ref_list.references ? ref_list.references->length () : 0;
179  }
180
181  /* Iterates I-th reference in the list, REF is also set.  */
182  ipa_ref *iterate_reference (unsigned i, ipa_ref *&ref);
183
184  /* Iterates I-th referring item in the list, REF is also set.  */
185  ipa_ref *iterate_referring (unsigned i, ipa_ref *&ref);
186
187  /* Iterates I-th referring alias item in the list, REF is also set.  */
188  ipa_ref *iterate_direct_aliases (unsigned i, ipa_ref *&ref);
189
190  /* Return true if symtab node and TARGET represents
191     semantically equivalent symbols.  */
192  bool semantically_equivalent_p (symtab_node *target);
193
194  /* Classify symbol symtab node for partitioning.  */
195  enum symbol_partitioning_class get_partitioning_class (void);
196
197  /* Return comdat group.  */
198  tree get_comdat_group ()
199    {
200      return x_comdat_group;
201    }
202
203  /* Return comdat group as identifier_node.  */
204  tree get_comdat_group_id ()
205    {
206      if (x_comdat_group && TREE_CODE (x_comdat_group) != IDENTIFIER_NODE)
207	x_comdat_group = DECL_ASSEMBLER_NAME (x_comdat_group);
208      return x_comdat_group;
209    }
210
211  /* Set comdat group.  */
212  void set_comdat_group (tree group)
213    {
214      gcc_checking_assert (!group || TREE_CODE (group) == IDENTIFIER_NODE
215			   || DECL_P (group));
216      x_comdat_group = group;
217    }
218
219  /* Return section as string.  */
220  const char * get_section ()
221    {
222      if (!x_section)
223	return NULL;
224      return x_section->name;
225    }
226
227  /* Remove node from same comdat group.   */
228  void remove_from_same_comdat_group (void);
229
230  /* Add this symtab_node to the same comdat group that OLD is in.  */
231  void add_to_same_comdat_group (symtab_node *old_node);
232
233  /* Dissolve the same_comdat_group list in which NODE resides.  */
234  void dissolve_same_comdat_group_list (void);
235
236  /* Return true when symtab_node is known to be used from other (non-LTO)
237     object file. Known only when doing LTO via linker plugin.  */
238  bool used_from_object_file_p (void);
239
240  /* Walk the alias chain to return the symbol NODE is alias of.
241     If NODE is not an alias, return NODE.
242     When AVAILABILITY is non-NULL, get minimal availability in the chain.  */
243  symtab_node *ultimate_alias_target (enum availability *avail = NULL);
244
245  /* Return next reachable static symbol with initializer after NODE.  */
246  inline symtab_node *next_defined_symbol (void);
247
248  /* Add reference recording that symtab node is alias of TARGET.
249     The function can fail in the case of aliasing cycles; in this case
250     it returns false.  */
251  bool resolve_alias (symtab_node *target);
252
253  /* C++ FE sometimes change linkage flags after producing same
254     body aliases.  */
255  void fixup_same_cpp_alias_visibility (symtab_node *target);
256
257  /* Call callback on symtab node and aliases associated to this node.
258     When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
259     skipped.  */
260  bool call_for_symbol_and_aliases (bool (*callback) (symtab_node *, void *),
261				    void *data,
262				    bool include_overwrite);
263
264  /* If node can not be interposable by static or dynamic linker to point to
265     different definition, return this symbol. Otherwise look for alias with
266     such property and if none exists, introduce new one.  */
267  symtab_node *noninterposable_alias (void);
268
269  /* Return node that alias is aliasing.  */
270  inline symtab_node *get_alias_target (void);
271
272  /* Set section for symbol and its aliases.  */
273  void set_section (const char *section);
274
275  /* Set section, do not recurse into aliases.
276     When one wants to change section of symbol and its aliases,
277     use set_section.  */
278  void set_section_for_node (const char *section);
279
280  /* Set initialization priority to PRIORITY.  */
281  void set_init_priority (priority_type priority);
282
283  /* Return the initialization priority.  */
284  priority_type get_init_priority ();
285
286  /* Return availability of NODE.  */
287  enum availability get_availability (void);
288
289  /* Make DECL local.  */
290  void make_decl_local (void);
291
292  /* Return desired alignment of the definition.  This is NOT alignment useful
293     to access THIS, because THIS may be interposable and DECL_ALIGN should
294     be used instead.  It however must be guaranteed when output definition
295     of THIS.  */
296  unsigned int definition_alignment ();
297
298  /* Return true if alignment can be increased.  */
299  bool can_increase_alignment_p ();
300
301  /* Increase alignment of symbol to ALIGN.  */
302  void increase_alignment (unsigned int align);
303
304  /* Return true if list contains an alias.  */
305  bool has_aliases_p (void);
306
307  /* Return true when the symbol is real symbol, i.e. it is not inline clone
308     or abstract function kept for debug info purposes only.  */
309  bool real_symbol_p (void);
310
311  /* Determine if symbol declaration is needed.  That is, visible to something
312     either outside this translation unit, something magic in the system
313     configury. This function is used just during symbol creation.  */
314  bool needed_p (void);
315
316  /* Return true when there are references to the node.  */
317  bool referred_to_p (void);
318
319  /* Return true if NODE can be discarded by linker from the binary.  */
320  inline bool
321  can_be_discarded_p (void)
322  {
323    return (DECL_EXTERNAL (decl)
324	    || (get_comdat_group ()
325		&& resolution != LDPR_PREVAILING_DEF
326		&& resolution != LDPR_PREVAILING_DEF_IRONLY
327		&& resolution != LDPR_PREVAILING_DEF_IRONLY_EXP));
328  }
329
330  /* Return true if NODE is local to a particular COMDAT group, and must not
331     be named from outside the COMDAT.  This is used for C++ decloned
332     constructors.  */
333  inline bool comdat_local_p (void)
334  {
335    return (same_comdat_group && !TREE_PUBLIC (decl));
336  }
337
338  /* Return true if ONE and TWO are part of the same COMDAT group.  */
339  inline bool in_same_comdat_group_p (symtab_node *target);
340
341  /* Return true if symbol is known to be nonzero.  */
342  bool nonzero_address ();
343
344  /* Return 0 if symbol is known to have different address than S2,
345     Return 1 if symbol is known to have same address as S2,
346     return 2 otherwise.   */
347  int equal_address_to (symtab_node *s2);
348
349  /* Return true if symbol's address may possibly be compared to other
350     symbol's address.  */
351  bool address_matters_p ();
352
353  /* Return true if NODE's address can be compared.  This use properties
354     of NODE only and does not look if the address is actually taken in
355     interesting way.  For that use ADDRESS_MATTERS_P instead.  */
356  bool address_can_be_compared_p (void);
357
358  /* Return symbol table node associated with DECL, if any,
359     and NULL otherwise.  */
360  static inline symtab_node *get (const_tree decl)
361  {
362#ifdef ENABLE_CHECKING
363    /* Check that we are called for sane type of object - functions
364       and static or external variables.  */
365    gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL
366			 || (TREE_CODE (decl) == VAR_DECL
367			     && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)
368				 || in_lto_p)));
369    /* Check that the mapping is sane - perhaps this check can go away,
370       but at the moment frontends tends to corrupt the mapping by calling
371       memcpy/memset on the tree nodes.  */
372    gcc_checking_assert (!decl->decl_with_vis.symtab_node
373			 || decl->decl_with_vis.symtab_node->decl == decl);
374#endif
375    return decl->decl_with_vis.symtab_node;
376  }
377
378  /* Try to find a symtab node for declaration DECL and if it does not
379     exist or if it corresponds to an inline clone, create a new one.  */
380  static inline symtab_node * get_create (tree node);
381
382  /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
383     Return NULL if there's no such node.  */
384  static symtab_node *get_for_asmname (const_tree asmname);
385
386  /* Dump symbol table to F.  */
387  static void dump_table (FILE *);
388
389  /* Dump symbol table to stderr.  */
390  static inline DEBUG_FUNCTION void debug_symtab (void)
391  {
392    dump_table (stderr);
393  }
394
395  /* Verify symbol table for internal consistency.  */
396  static DEBUG_FUNCTION void verify_symtab_nodes (void);
397
398  /* Type of the symbol.  */
399  ENUM_BITFIELD (symtab_type) type : 8;
400
401  /* The symbols resolution.  */
402  ENUM_BITFIELD (ld_plugin_symbol_resolution) resolution : 8;
403
404  /*** Flags representing the symbol type.  ***/
405
406  /* True when symbol corresponds to a definition in current unit.
407     set via finalize_function or finalize_decl  */
408  unsigned definition : 1;
409  /* True when symbol is an alias.
410     Set by ssemble_alias.  */
411  unsigned alias : 1;
412  /* True when alias is a weakref.  */
413  unsigned weakref : 1;
414  /* C++ frontend produce same body aliases and extra name aliases for
415     virtual functions and vtables that are obviously equivalent.
416     Those aliases are bit special, especially because C++ frontend
417     visibility code is so ugly it can not get them right at first time
418     and their visibility needs to be copied from their "masters" at
419     the end of parsing.  */
420  unsigned cpp_implicit_alias : 1;
421  /* Set once the definition was analyzed.  The list of references and
422     other properties are built during analysis.  */
423  unsigned analyzed : 1;
424  /* Set for write-only variables.  */
425  unsigned writeonly : 1;
426  /* Visibility of symbol was used for further optimization; do not
427     permit further changes.  */
428  unsigned refuse_visibility_changes : 1;
429
430  /*** Visibility and linkage flags.  ***/
431
432  /* Set when function is visible by other units.  */
433  unsigned externally_visible : 1;
434  /* Don't reorder to other symbols having this set.  */
435  unsigned no_reorder : 1;
436  /* The symbol will be assumed to be used in an invisible way (like
437     by an toplevel asm statement).  */
438  unsigned force_output : 1;
439  /* Like FORCE_OUTPUT, but in the case it is ABI requiring the symbol to be
440     exported.  Unlike FORCE_OUTPUT this flag gets cleared to symbols promoted
441     to static and it does not inhibit optimization.  */
442  unsigned forced_by_abi : 1;
443  /* True when the name is known to be unique and thus it does not need mangling.  */
444  unsigned unique_name : 1;
445  /* Specify whether the section was set by user or by
446     compiler via -ffunction-sections.  */
447  unsigned implicit_section : 1;
448  /* True when body and other characteristics have been removed by
449     symtab_remove_unreachable_nodes. */
450  unsigned body_removed : 1;
451
452  /*** WHOPR Partitioning flags.
453       These flags are used at ltrans stage when only part of the callgraph is
454       available. ***/
455
456  /* Set when variable is used from other LTRANS partition.  */
457  unsigned used_from_other_partition : 1;
458  /* Set when function is available in the other LTRANS partition.
459     During WPA output it is used to mark nodes that are present in
460     multiple partitions.  */
461  unsigned in_other_partition : 1;
462
463
464
465  /*** other flags.  ***/
466
467  /* Set when symbol has address taken. */
468  unsigned address_taken : 1;
469  /* Set when init priority is set.  */
470  unsigned in_init_priority_hash : 1;
471
472  /* Set when symbol needs to be streamed into LTO bytecode for LTO, or in case
473     of offloading, for separate compilation for a different target.  */
474  unsigned need_lto_streaming : 1;
475
476  /* Set when symbol can be streamed into bytecode for offloading.  */
477  unsigned offloadable : 1;
478
479
480  /* Ordering of all symtab entries.  */
481  int order;
482
483  /* Declaration representing the symbol.  */
484  tree decl;
485
486  /* Linked list of symbol table entries starting with symtab_nodes.  */
487  symtab_node *next;
488  symtab_node *previous;
489
490  /* Linked list of symbols with the same asm name.  There may be multiple
491     entries for single symbol name during LTO, because symbols are renamed
492     only after partitioning.
493
494     Because inline clones are kept in the assembler name has, they also produce
495     duplicate entries.
496
497     There are also several long standing bugs where frontends and builtin
498     code produce duplicated decls.  */
499  symtab_node *next_sharing_asm_name;
500  symtab_node *previous_sharing_asm_name;
501
502  /* Circular list of nodes in the same comdat group if non-NULL.  */
503  symtab_node *same_comdat_group;
504
505  /* Vectors of referring and referenced entities.  */
506  ipa_ref_list ref_list;
507
508  /* Alias target. May be either DECL pointer or ASSEMBLER_NAME pointer
509     depending to what was known to frontend on the creation time.
510     Once alias is resolved, this pointer become NULL.  */
511  tree alias_target;
512
513  /* File stream where this node is being written to.  */
514  struct lto_file_decl_data * lto_file_data;
515
516  PTR GTY ((skip)) aux;
517
518  /* Comdat group the symbol is in.  Can be private if GGC allowed that.  */
519  tree x_comdat_group;
520
521  /* Section name. Again can be private, if allowed.  */
522  section_hash_entry *x_section;
523
524protected:
525  /* Dump base fields of symtab nodes to F.  Not to be used directly.  */
526  void dump_base (FILE *);
527
528  /* Verify common part of symtab node.  */
529  bool DEBUG_FUNCTION verify_base (void);
530
531  /* Remove node from symbol table.  This function is not used directly, but via
532     cgraph/varpool node removal routines.  */
533  void unregister (void);
534
535  /* Return the initialization and finalization priority information for
536     DECL.  If there is no previous priority information, a freshly
537     allocated structure is returned.  */
538  struct symbol_priority_map *priority_info (void);
539
540  /* Worker for call_for_symbol_and_aliases_1.  */
541  bool call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *, void *),
542				      void *data,
543				      bool include_overwrite);
544private:
545  /* Worker for set_section.  */
546  static bool set_section (symtab_node *n, void *s);
547
548  /* Worker for symtab_resolve_alias.  */
549  static bool set_implicit_section (symtab_node *n, void *);
550
551  /* Worker searching noninterposable alias.  */
552  static bool noninterposable_alias (symtab_node *node, void *data);
553
554  /* Worker for ultimate_alias_target.  */
555  symtab_node *ultimate_alias_target_1 (enum availability *avail = NULL);
556};
557
558/* Walk all aliases for NODE.  */
559#define FOR_EACH_ALIAS(node, alias) \
560  for (unsigned x_i = 0; node->iterate_direct_aliases (x_i, alias); x_i++)
561
562/* This is the information that is put into the cgraph local structure
563   to recover a function.  */
564struct lto_file_decl_data;
565
566extern const char * const cgraph_availability_names[];
567extern const char * const ld_plugin_symbol_resolution_names[];
568extern const char * const tls_model_names[];
569
570/* Information about thunk, used only for same body aliases.  */
571
572struct GTY(()) cgraph_thunk_info {
573  /* Information about the thunk.  */
574  HOST_WIDE_INT fixed_offset;
575  HOST_WIDE_INT virtual_value;
576  tree alias;
577  bool this_adjusting;
578  bool virtual_offset_p;
579  bool add_pointer_bounds_args;
580  /* Set to true when alias node is thunk.  */
581  bool thunk_p;
582};
583
584/* Information about the function collected locally.
585   Available after function is analyzed.  */
586
587struct GTY(()) cgraph_local_info {
588  /* Set when function function is visible in current compilation unit only
589     and its address is never taken.  */
590  unsigned local : 1;
591
592  /* False when there is something makes versioning impossible.  */
593  unsigned versionable : 1;
594
595  /* False when function calling convention and signature can not be changed.
596     This is the case when __builtin_apply_args is used.  */
597  unsigned can_change_signature : 1;
598
599  /* True when the function has been originally extern inline, but it is
600     redefined now.  */
601  unsigned redefined_extern_inline : 1;
602
603  /* True if the function may enter serial irrevocable mode.  */
604  unsigned tm_may_enter_irr : 1;
605};
606
607/* Information about the function that needs to be computed globally
608   once compilation is finished.  Available only with -funit-at-a-time.  */
609
610struct GTY(()) cgraph_global_info {
611  /* For inline clones this points to the function they will be
612     inlined into.  */
613  cgraph_node *inlined_to;
614};
615
616/* Information about the function that is propagated by the RTL backend.
617   Available only for functions that has been already assembled.  */
618
619struct GTY(()) cgraph_rtl_info {
620   unsigned int preferred_incoming_stack_boundary;
621
622  /* Call unsaved hard registers really used by the corresponding
623     function (including ones used by functions called by the
624     function).  */
625  HARD_REG_SET function_used_regs;
626  /* Set if function_used_regs is valid.  */
627  unsigned function_used_regs_valid: 1;
628};
629
630/* Represent which DECL tree (or reference to such tree)
631   will be replaced by another tree while versioning.  */
632struct GTY(()) ipa_replace_map
633{
634  /* The tree that will be replaced.  */
635  tree old_tree;
636  /* The new (replacing) tree.  */
637  tree new_tree;
638  /* Parameter number to replace, when old_tree is NULL.  */
639  int parm_num;
640  /* True when a substitution should be done, false otherwise.  */
641  bool replace_p;
642  /* True when we replace a reference to old_tree.  */
643  bool ref_p;
644};
645
646struct GTY(()) cgraph_clone_info
647{
648  vec<ipa_replace_map *, va_gc> *tree_map;
649  bitmap args_to_skip;
650  bitmap combined_args_to_skip;
651};
652
653enum cgraph_simd_clone_arg_type
654{
655  SIMD_CLONE_ARG_TYPE_VECTOR,
656  SIMD_CLONE_ARG_TYPE_UNIFORM,
657  SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP,
658  SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP,
659  SIMD_CLONE_ARG_TYPE_MASK
660};
661
662/* Function arguments in the original function of a SIMD clone.
663   Supplementary data for `struct simd_clone'.  */
664
665struct GTY(()) cgraph_simd_clone_arg {
666  /* Original function argument as it originally existed in
667     DECL_ARGUMENTS.  */
668  tree orig_arg;
669
670  /* orig_arg's function (or for extern functions type from
671     TYPE_ARG_TYPES).  */
672  tree orig_type;
673
674  /* If argument is a vector, this holds the vector version of
675     orig_arg that after adjusting the argument types will live in
676     DECL_ARGUMENTS.  Otherwise, this is NULL.
677
678     This basically holds:
679       vector(simdlen) __typeof__(orig_arg) new_arg.  */
680  tree vector_arg;
681
682  /* vector_arg's type (or for extern functions new vector type.  */
683  tree vector_type;
684
685  /* If argument is a vector, this holds the array where the simd
686     argument is held while executing the simd clone function.  This
687     is a local variable in the cloned function.  Its content is
688     copied from vector_arg upon entry to the clone.
689
690     This basically holds:
691       __typeof__(orig_arg) simd_array[simdlen].  */
692  tree simd_array;
693
694  /* A SIMD clone's argument can be either linear (constant or
695     variable), uniform, or vector.  */
696  enum cgraph_simd_clone_arg_type arg_type;
697
698  /* For arg_type SIMD_CLONE_ARG_TYPE_LINEAR_CONSTANT_STEP this is
699     the constant linear step, if arg_type is
700     SIMD_CLONE_ARG_TYPE_LINEAR_VARIABLE_STEP, this is index of
701     the uniform argument holding the step, otherwise 0.  */
702  HOST_WIDE_INT linear_step;
703
704  /* Variable alignment if available, otherwise 0.  */
705  unsigned int alignment;
706};
707
708/* Specific data for a SIMD function clone.  */
709
710struct GTY(()) cgraph_simd_clone {
711  /* Number of words in the SIMD lane associated with this clone.  */
712  unsigned int simdlen;
713
714  /* Number of annotated function arguments in `args'.  This is
715     usually the number of named arguments in FNDECL.  */
716  unsigned int nargs;
717
718  /* Max hardware vector size in bits for integral vectors.  */
719  unsigned int vecsize_int;
720
721  /* Max hardware vector size in bits for floating point vectors.  */
722  unsigned int vecsize_float;
723
724  /* The mangling character for a given vector size.  This is is used
725     to determine the ISA mangling bit as specified in the Intel
726     Vector ABI.  */
727  unsigned char vecsize_mangle;
728
729  /* True if this is the masked, in-branch version of the clone,
730     otherwise false.  */
731  unsigned int inbranch : 1;
732
733  /* True if this is a Cilk Plus variant.  */
734  unsigned int cilk_elemental : 1;
735
736  /* Doubly linked list of SIMD clones.  */
737  cgraph_node *prev_clone, *next_clone;
738
739  /* Original cgraph node the SIMD clones were created for.  */
740  cgraph_node *origin;
741
742  /* Annotated function arguments for the original function.  */
743  cgraph_simd_clone_arg GTY((length ("%h.nargs"))) args[1];
744};
745
746/* Function Multiversioning info.  */
747struct GTY((for_user)) cgraph_function_version_info {
748  /* The cgraph_node for which the function version info is stored.  */
749  cgraph_node *this_node;
750  /* Chains all the semantically identical function versions.  The
751     first function in this chain is the version_info node of the
752     default function.  */
753  cgraph_function_version_info *prev;
754  /* If this version node corresponds to a dispatcher for function
755     versions, this points to the version info node of the default
756     function, the first node in the chain.  */
757  cgraph_function_version_info *next;
758  /* If this node corresponds to a function version, this points
759     to the dispatcher function decl, which is the function that must
760     be called to execute the right function version at run-time.
761
762     If this cgraph node is a dispatcher (if dispatcher_function is
763     true, in the cgraph_node struct) for function versions, this
764     points to resolver function, which holds the function body of the
765     dispatcher. The dispatcher decl is an alias to the resolver
766     function decl.  */
767  tree dispatcher_resolver;
768};
769
770#define DEFCIFCODE(code, type, string)	CIF_ ## code,
771/* Reasons for inlining failures.  */
772
773enum cgraph_inline_failed_t {
774#include "cif-code.def"
775  CIF_N_REASONS
776};
777
778enum cgraph_inline_failed_type_t
779{
780  CIF_FINAL_NORMAL = 0,
781  CIF_FINAL_ERROR
782};
783
784struct cgraph_edge;
785
786struct cgraph_edge_hasher : ggc_hasher<cgraph_edge *>
787{
788  typedef gimple compare_type;
789
790  static hashval_t hash (cgraph_edge *);
791  static hashval_t hash (gimple);
792  static bool equal (cgraph_edge *, gimple);
793};
794
795/* The cgraph data structure.
796   Each function decl has assigned cgraph_node listing callees and callers.  */
797
798struct GTY((tag ("SYMTAB_FUNCTION"))) cgraph_node : public symtab_node {
799public:
800  /* Remove the node from cgraph and all inline clones inlined into it.
801     Skip however removal of FORBIDDEN_NODE and return true if it needs to be
802     removed.  This allows to call the function from outer loop walking clone
803     tree.  */
804  bool remove_symbol_and_inline_clones (cgraph_node *forbidden_node = NULL);
805
806  /* Record all references from cgraph_node that are taken
807     in statement STMT.  */
808  void record_stmt_references (gimple stmt);
809
810  /* Like cgraph_set_call_stmt but walk the clone tree and update all
811     clones sharing the same function body.
812     When WHOLE_SPECULATIVE_EDGES is true, all three components of
813     speculative edge gets updated.  Otherwise we update only direct
814     call.  */
815  void set_call_stmt_including_clones (gimple old_stmt, gcall *new_stmt,
816				       bool update_speculative = true);
817
818  /* Walk the alias chain to return the function cgraph_node is alias of.
819     Walk through thunk, too.
820     When AVAILABILITY is non-NULL, get minimal availability in the chain.  */
821  cgraph_node *function_symbol (enum availability *avail = NULL);
822
823  /* Walk the alias chain to return the function cgraph_node is alias of.
824     Walk through non virtual thunks, too.  Thus we return either a function
825     or a virtual thunk node.
826     When AVAILABILITY is non-NULL, get minimal availability in the chain.  */
827  cgraph_node *function_or_virtual_thunk_symbol
828				(enum availability *avail = NULL);
829
830  /* Create node representing clone of N executed COUNT times.  Decrease
831     the execution counts from original node too.
832     The new clone will have decl set to DECL that may or may not be the same
833     as decl of N.
834
835     When UPDATE_ORIGINAL is true, the counts are subtracted from the original
836     function's profile to reflect the fact that part of execution is handled
837     by node.
838     When CALL_DUPLICATOIN_HOOK is true, the ipa passes are acknowledged about
839     the new clone. Otherwise the caller is responsible for doing so later.
840
841     If the new node is being inlined into another one, NEW_INLINED_TO should be
842     the outline function the new one is (even indirectly) inlined to.
843     All hooks will see this in node's global.inlined_to, when invoked.
844     Can be NULL if the node is not inlined.  */
845  cgraph_node *create_clone (tree decl, gcov_type count, int freq,
846			     bool update_original,
847			     vec<cgraph_edge *> redirect_callers,
848			     bool call_duplication_hook,
849			     cgraph_node *new_inlined_to,
850			     bitmap args_to_skip);
851
852  /* Create callgraph node clone with new declaration.  The actual body will
853     be copied later at compilation stage.  */
854  cgraph_node *create_virtual_clone (vec<cgraph_edge *> redirect_callers,
855				     vec<ipa_replace_map *, va_gc> *tree_map,
856				     bitmap args_to_skip, const char * suffix);
857
858  /* cgraph node being removed from symbol table; see if its entry can be
859   replaced by other inline clone.  */
860  cgraph_node *find_replacement (void);
861
862  /* Create a new cgraph node which is the new version of
863     callgraph node.  REDIRECT_CALLERS holds the callers
864     edges which should be redirected to point to
865     NEW_VERSION.  ALL the callees edges of the node
866     are cloned to the new version node.  Return the new
867     version node.
868
869     If non-NULL BLOCK_TO_COPY determine what basic blocks
870     was copied to prevent duplications of calls that are dead
871     in the clone.  */
872
873  cgraph_node *create_version_clone (tree new_decl,
874				    vec<cgraph_edge *> redirect_callers,
875				    bitmap bbs_to_copy);
876
877  /* Perform function versioning.
878     Function versioning includes copying of the tree and
879     a callgraph update (creating a new cgraph node and updating
880     its callees and callers).
881
882     REDIRECT_CALLERS varray includes the edges to be redirected
883     to the new version.
884
885     TREE_MAP is a mapping of tree nodes we want to replace with
886     new ones (according to results of prior analysis).
887
888     If non-NULL ARGS_TO_SKIP determine function parameters to remove
889     from new version.
890     If SKIP_RETURN is true, the new version will return void.
891     If non-NULL BLOCK_TO_COPY determine what basic blocks to copy.
892     If non_NULL NEW_ENTRY determine new entry BB of the clone.
893
894     Return the new version's cgraph node.  */
895  cgraph_node *create_version_clone_with_body
896    (vec<cgraph_edge *> redirect_callers,
897     vec<ipa_replace_map *, va_gc> *tree_map, bitmap args_to_skip,
898     bool skip_return, bitmap bbs_to_copy, basic_block new_entry_block,
899     const char *clone_name);
900
901  /* Insert a new cgraph_function_version_info node into cgraph_fnver_htab
902     corresponding to cgraph_node.  */
903  cgraph_function_version_info *insert_new_function_version (void);
904
905  /* Get the cgraph_function_version_info node corresponding to node.  */
906  cgraph_function_version_info *function_version (void);
907
908  /* Discover all functions and variables that are trivially needed, analyze
909     them as well as all functions and variables referred by them  */
910  void analyze (void);
911
912  /* Add thunk alias into callgraph.  The alias declaration is ALIAS and it
913     aliases DECL with an adjustments made into the first parameter.
914     See comments in thunk_adjust for detail on the parameters.  */
915  cgraph_node * create_thunk (tree alias, tree, bool this_adjusting,
916			      HOST_WIDE_INT fixed_offset,
917			      HOST_WIDE_INT virtual_value,
918			      tree virtual_offset,
919			      tree real_alias);
920
921
922  /* Return node that alias is aliasing.  */
923  inline cgraph_node *get_alias_target (void);
924
925  /* Given function symbol, walk the alias chain to return the function node
926     is alias of. Do not walk through thunks.
927     When AVAILABILITY is non-NULL, get minimal availability in the chain.  */
928
929  cgraph_node *ultimate_alias_target (availability *availability = NULL);
930
931  /* Expand thunk NODE to gimple if possible.
932     When FORCE_GIMPLE_THUNK is true, gimple thunk is created and
933     no assembler is produced.
934     When OUTPUT_ASM_THUNK is true, also produce assembler for
935     thunks that are not lowered.  */
936  bool expand_thunk (bool output_asm_thunks, bool force_gimple_thunk);
937
938  /*  Call expand_thunk on all callers that are thunks and analyze those
939      nodes that were expanded.  */
940  void expand_all_artificial_thunks ();
941
942  /* Assemble thunks and aliases associated to node.  */
943  void assemble_thunks_and_aliases (void);
944
945  /* Expand function specified by node.  */
946  void expand (void);
947
948  /* As an GCC extension we allow redefinition of the function.  The
949     semantics when both copies of bodies differ is not well defined.
950     We replace the old body with new body so in unit at a time mode
951     we always use new body, while in normal mode we may end up with
952     old body inlined into some functions and new body expanded and
953     inlined in others.  */
954  void reset (void);
955
956  /* Creates a wrapper from cgraph_node to TARGET node. Thunk is used for this
957     kind of wrapper method.  */
958  void create_wrapper (cgraph_node *target);
959
960  /* Verify cgraph nodes of the cgraph node.  */
961  void DEBUG_FUNCTION verify_node (void);
962
963  /* Remove function from symbol table.  */
964  void remove (void);
965
966  /* Dump call graph node to file F.  */
967  void dump (FILE *f);
968
969  /* Dump call graph node to stderr.  */
970  void DEBUG_FUNCTION debug (void);
971
972  /* When doing LTO, read cgraph_node's body from disk if it is not already
973     present.  */
974  bool get_untransformed_body (void);
975
976  /* Prepare function body.  When doing LTO, read cgraph_node's body from disk
977     if it is not already present.  When some IPA transformations are scheduled,
978     apply them.  */
979  bool get_body (void);
980
981  /* Release memory used to represent body of function.
982     Use this only for functions that are released before being translated to
983     target code (i.e. RTL).  Functions that are compiled to RTL and beyond
984     are free'd in final.c via free_after_compilation().  */
985  void release_body (bool keep_arguments = false);
986
987  /* Return the DECL_STRUCT_FUNCTION of the function.  */
988  struct function *get_fun (void);
989
990  /* cgraph_node is no longer nested function; update cgraph accordingly.  */
991  void unnest (void);
992
993  /* Bring cgraph node local.  */
994  void make_local (void);
995
996  /* Likewise indicate that a node is having address taken.  */
997  void mark_address_taken (void);
998
999  /* Set fialization priority to PRIORITY.  */
1000  void set_fini_priority (priority_type priority);
1001
1002  /* Return the finalization priority.  */
1003  priority_type get_fini_priority (void);
1004
1005  /* Create edge from a given function to CALLEE in the cgraph.  */
1006  cgraph_edge *create_edge (cgraph_node *callee,
1007			    gcall *call_stmt, gcov_type count,
1008			    int freq);
1009
1010  /* Create an indirect edge with a yet-undetermined callee where the call
1011     statement destination is a formal parameter of the caller with index
1012     PARAM_INDEX. */
1013  cgraph_edge *create_indirect_edge (gcall *call_stmt, int ecf_flags,
1014				     gcov_type count, int freq,
1015				     bool compute_indirect_info = true);
1016
1017  /* Like cgraph_create_edge walk the clone tree and update all clones sharing
1018   same function body.  If clones already have edge for OLD_STMT; only
1019   update the edge same way as cgraph_set_call_stmt_including_clones does.  */
1020  void create_edge_including_clones (cgraph_node *callee,
1021				     gimple old_stmt, gcall *stmt,
1022				     gcov_type count,
1023				     int freq,
1024				     cgraph_inline_failed_t reason);
1025
1026  /* Return the callgraph edge representing the GIMPLE_CALL statement
1027     CALL_STMT.  */
1028  cgraph_edge *get_edge (gimple call_stmt);
1029
1030  /* Collect all callers of cgraph_node and its aliases that are known to lead
1031     to NODE (i.e. are not overwritable) and that are not thunks.  */
1032  vec<cgraph_edge *> collect_callers (void);
1033
1034  /* Remove all callers from the node.  */
1035  void remove_callers (void);
1036
1037  /* Remove all callees from the node.  */
1038  void remove_callees (void);
1039
1040  /* Return function availability.  See cgraph.h for description of individual
1041     return values.  */
1042  enum availability get_availability (void);
1043
1044  /* Set TREE_NOTHROW on cgraph_node's decl and on aliases of the node
1045     if any to NOTHROW.  */
1046  void set_nothrow_flag (bool nothrow);
1047
1048  /* Set TREE_READONLY on cgraph_node's decl and on aliases of the node
1049     if any to READONLY.  */
1050  void set_const_flag (bool readonly, bool looping);
1051
1052  /* Set DECL_PURE_P on cgraph_node's decl and on aliases of the node
1053     if any to PURE.  */
1054  void set_pure_flag (bool pure, bool looping);
1055
1056  /* Call callback on function and aliases associated to the function.
1057     When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1058     skipped. */
1059
1060  bool call_for_symbol_and_aliases (bool (*callback) (cgraph_node *,
1061						      void *),
1062				    void *data, bool include_overwritable);
1063
1064  /* Call callback on cgraph_node, thunks and aliases associated to NODE.
1065     When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1066     skipped.  When EXCLUDE_VIRTUAL_THUNKS is true, virtual thunks are
1067     skipped.  */
1068  bool call_for_symbol_thunks_and_aliases (bool (*callback) (cgraph_node *node,
1069							     void *data),
1070					   void *data,
1071					   bool include_overwritable,
1072					   bool exclude_virtual_thunks = false);
1073
1074  /* Likewise indicate that a node is needed, i.e. reachable via some
1075     external means.  */
1076  inline void mark_force_output (void);
1077
1078  /* Return true when function can be marked local.  */
1079  bool local_p (void);
1080
1081  /* Return true if cgraph_node can be made local for API change.
1082     Extern inline functions and C++ COMDAT functions can be made local
1083     at the expense of possible code size growth if function is used in multiple
1084     compilation units.  */
1085  bool can_be_local_p (void);
1086
1087  /* Return true when cgraph_node can not return or throw and thus
1088     it is safe to ignore its side effects for IPA analysis.  */
1089  bool cannot_return_p (void);
1090
1091  /* Return true when function cgraph_node and all its aliases are only called
1092     directly.
1093     i.e. it is not externally visible, address was not taken and
1094     it is not used in any other non-standard way.  */
1095  bool only_called_directly_p (void);
1096
1097  /* Return true when function is only called directly or it has alias.
1098     i.e. it is not externally visible, address was not taken and
1099     it is not used in any other non-standard way.  */
1100  inline bool only_called_directly_or_aliased_p (void);
1101
1102  /* Return true when function cgraph_node can be expected to be removed
1103     from program when direct calls in this compilation unit are removed.
1104
1105     As a special case COMDAT functions are
1106     cgraph_can_remove_if_no_direct_calls_p while the are not
1107     cgraph_only_called_directly_p (it is possible they are called from other
1108     unit)
1109
1110     This function behaves as cgraph_only_called_directly_p because eliminating
1111     all uses of COMDAT function does not make it necessarily disappear from
1112     the program unless we are compiling whole program or we do LTO.  In this
1113     case we know we win since dynamic linking will not really discard the
1114     linkonce section.
1115
1116     If WILL_INLINE is true, assume that function will be inlined into all the
1117     direct calls.  */
1118  bool will_be_removed_from_program_if_no_direct_calls_p
1119	 (bool will_inline = false);
1120
1121  /* Return true when function can be removed from callgraph
1122     if all direct calls and references are eliminated.  The function does
1123     not take into account comdat groups.  */
1124  bool can_remove_if_no_direct_calls_and_refs_p (void);
1125
1126  /* Return true when function cgraph_node and its aliases can be removed from
1127     callgraph if all direct calls are eliminated.
1128     If WILL_INLINE is true, assume that function will be inlined into all the
1129     direct calls.  */
1130  bool can_remove_if_no_direct_calls_p (bool will_inline = false);
1131
1132  /* Return true when callgraph node is a function with Gimple body defined
1133     in current unit.  Functions can also be define externally or they
1134     can be thunks with no Gimple representation.
1135
1136     Note that at WPA stage, the function body may not be present in memory.  */
1137  inline bool has_gimple_body_p (void);
1138
1139  /* Return true if function should be optimized for size.  */
1140  bool optimize_for_size_p (void);
1141
1142  /* Dump the callgraph to file F.  */
1143  static void dump_cgraph (FILE *f);
1144
1145  /* Dump the call graph to stderr.  */
1146  static inline
1147  void debug_cgraph (void)
1148  {
1149    dump_cgraph (stderr);
1150  }
1151
1152  /* Record that DECL1 and DECL2 are semantically identical function
1153     versions.  */
1154  static void record_function_versions (tree decl1, tree decl2);
1155
1156  /* Remove the cgraph_function_version_info and cgraph_node for DECL.  This
1157     DECL is a duplicate declaration.  */
1158  static void delete_function_version (tree decl);
1159
1160  /* Add the function FNDECL to the call graph.
1161     Unlike finalize_function, this function is intended to be used
1162     by middle end and allows insertion of new function at arbitrary point
1163     of compilation.  The function can be either in high, low or SSA form
1164     GIMPLE.
1165
1166     The function is assumed to be reachable and have address taken (so no
1167     API breaking optimizations are performed on it).
1168
1169     Main work done by this function is to enqueue the function for later
1170     processing to avoid need the passes to be re-entrant.  */
1171  static void add_new_function (tree fndecl, bool lowered);
1172
1173  /* Return callgraph node for given symbol and check it is a function. */
1174  static inline cgraph_node *get (const_tree decl)
1175  {
1176    gcc_checking_assert (TREE_CODE (decl) == FUNCTION_DECL);
1177    return dyn_cast <cgraph_node *> (symtab_node::get (decl));
1178  }
1179
1180  /* DECL has been parsed.  Take it, queue it, compile it at the whim of the
1181     logic in effect.  If NO_COLLECT is true, then our caller cannot stand to
1182     have the garbage collector run at the moment.  We would need to either
1183     create a new GC context, or just not compile right now.  */
1184  static void finalize_function (tree, bool);
1185
1186  /* Return cgraph node assigned to DECL.  Create new one when needed.  */
1187  static cgraph_node * create (tree decl);
1188
1189  /* Try to find a call graph node for declaration DECL and if it does not
1190     exist or if it corresponds to an inline clone, create a new one.  */
1191  static cgraph_node * get_create (tree);
1192
1193  /* Return local info for the compiled function.  */
1194  static cgraph_local_info *local_info (tree decl);
1195
1196  /* Return local info for the compiled function.  */
1197  static cgraph_rtl_info *rtl_info (tree);
1198
1199  /* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
1200     Return NULL if there's no such node.  */
1201  static cgraph_node *get_for_asmname (tree asmname);
1202
1203  /* Attempt to mark ALIAS as an alias to DECL.  Return alias node if
1204     successful and NULL otherwise.
1205     Same body aliases are output whenever the body of DECL is output,
1206     and cgraph_node::get (ALIAS) transparently
1207     returns cgraph_node::get (DECL).  */
1208  static cgraph_node * create_same_body_alias (tree alias, tree decl);
1209
1210  /* Verify whole cgraph structure.  */
1211  static void DEBUG_FUNCTION verify_cgraph_nodes (void);
1212
1213  /* Worker to bring NODE local.  */
1214  static bool make_local (cgraph_node *node, void *);
1215
1216  /* Mark ALIAS as an alias to DECL.  DECL_NODE is cgraph node representing
1217     the function body is associated
1218     with (not necessarily cgraph_node (DECL).  */
1219  static cgraph_node *create_alias (tree alias, tree target);
1220
1221  /* Return true if NODE has thunk.  */
1222  static bool has_thunk_p (cgraph_node *node, void *);
1223
1224  cgraph_edge *callees;
1225  cgraph_edge *callers;
1226  /* List of edges representing indirect calls with a yet undetermined
1227     callee.  */
1228  cgraph_edge *indirect_calls;
1229  /* For nested functions points to function the node is nested in.  */
1230  cgraph_node *origin;
1231  /* Points to first nested function, if any.  */
1232  cgraph_node *nested;
1233  /* Pointer to the next function with same origin, if any.  */
1234  cgraph_node *next_nested;
1235  /* Pointer to the next clone.  */
1236  cgraph_node *next_sibling_clone;
1237  cgraph_node *prev_sibling_clone;
1238  cgraph_node *clones;
1239  cgraph_node *clone_of;
1240  /* If instrumentation_clone is 1 then instrumented_version points
1241     to the original function used to make instrumented version.
1242     Otherwise points to instrumented version of the function.  */
1243  cgraph_node *instrumented_version;
1244  /* If instrumentation_clone is 1 then orig_decl is the original
1245     function declaration.  */
1246  tree orig_decl;
1247  /* For functions with many calls sites it holds map from call expression
1248     to the edge to speed up cgraph_edge function.  */
1249  hash_table<cgraph_edge_hasher> *GTY(()) call_site_hash;
1250  /* Declaration node used to be clone of. */
1251  tree former_clone_of;
1252
1253  /* If this is a SIMD clone, this points to the SIMD specific
1254     information for it.  */
1255  cgraph_simd_clone *simdclone;
1256  /* If this function has SIMD clones, this points to the first clone.  */
1257  cgraph_node *simd_clones;
1258
1259  /* Interprocedural passes scheduled to have their transform functions
1260     applied next time we execute local pass on them.  We maintain it
1261     per-function in order to allow IPA passes to introduce new functions.  */
1262  vec<ipa_opt_pass> GTY((skip)) ipa_transforms_to_apply;
1263
1264  cgraph_local_info local;
1265  cgraph_global_info global;
1266  cgraph_rtl_info rtl;
1267  cgraph_clone_info clone;
1268  cgraph_thunk_info thunk;
1269
1270  /* Expected number of executions: calculated in profile.c.  */
1271  gcov_type count;
1272  /* How to scale counts at materialization time; used to merge
1273     LTO units with different number of profile runs.  */
1274  int count_materialization_scale;
1275  /* Unique id of the node.  */
1276  int uid;
1277  /* Summary unique id of the node.  */
1278  int summary_uid;
1279  /* ID assigned by the profiling.  */
1280  unsigned int profile_id;
1281  /* Time profiler: first run of function.  */
1282  int tp_first_run;
1283
1284  /* Set when decl is an abstract function pointed to by the
1285     ABSTRACT_DECL_ORIGIN of a reachable function.  */
1286  unsigned used_as_abstract_origin : 1;
1287  /* Set once the function is lowered (i.e. its CFG is built).  */
1288  unsigned lowered : 1;
1289  /* Set once the function has been instantiated and its callee
1290     lists created.  */
1291  unsigned process : 1;
1292  /* How commonly executed the node is.  Initialized during branch
1293     probabilities pass.  */
1294  ENUM_BITFIELD (node_frequency) frequency : 2;
1295  /* True when function can only be called at startup (from static ctor).  */
1296  unsigned only_called_at_startup : 1;
1297  /* True when function can only be called at startup (from static dtor).  */
1298  unsigned only_called_at_exit : 1;
1299  /* True when function is the transactional clone of a function which
1300     is called only from inside transactions.  */
1301  /* ?? We should be able to remove this.  We have enough bits in
1302     cgraph to calculate it.  */
1303  unsigned tm_clone : 1;
1304  /* True if this decl is a dispatcher for function versions.  */
1305  unsigned dispatcher_function : 1;
1306  /* True if this decl calls a COMDAT-local function.  This is set up in
1307     compute_inline_parameters and inline_call.  */
1308  unsigned calls_comdat_local : 1;
1309  /* True if node has been created by merge operation in IPA-ICF.  */
1310  unsigned icf_merged: 1;
1311  /* True when function is clone created for Pointer Bounds Checker
1312     instrumentation.  */
1313  unsigned instrumentation_clone : 1;
1314  /* True if call to node can't result in a call to free, munmap or
1315     other operation that could make previously non-trapping memory
1316     accesses trapping.  */
1317  unsigned nonfreeing_fn : 1;
1318  /* True if there was multiple COMDAT bodies merged by lto-symtab.  */
1319  unsigned merged : 1;
1320  /* True if function was created to be executed in parallel.  */
1321  unsigned parallelized_function : 1;
1322  /* True if function is part split out by ipa-split.  */
1323  unsigned split_part : 1;
1324
1325private:
1326  /* Worker for call_for_symbol_and_aliases.  */
1327  bool call_for_symbol_and_aliases_1 (bool (*callback) (cgraph_node *,
1328						        void *),
1329				      void *data, bool include_overwritable);
1330};
1331
1332/* A cgraph node set is a collection of cgraph nodes.  A cgraph node
1333   can appear in multiple sets.  */
1334struct cgraph_node_set_def
1335{
1336  hash_map<cgraph_node *, size_t> *map;
1337  vec<cgraph_node *> nodes;
1338};
1339
1340typedef cgraph_node_set_def *cgraph_node_set;
1341typedef struct varpool_node_set_def *varpool_node_set;
1342
1343class varpool_node;
1344
1345/* A varpool node set is a collection of varpool nodes.  A varpool node
1346   can appear in multiple sets.  */
1347struct varpool_node_set_def
1348{
1349  hash_map<varpool_node *, size_t> * map;
1350  vec<varpool_node *> nodes;
1351};
1352
1353/* Iterator structure for cgraph node sets.  */
1354struct cgraph_node_set_iterator
1355{
1356  cgraph_node_set set;
1357  unsigned index;
1358};
1359
1360/* Iterator structure for varpool node sets.  */
1361struct varpool_node_set_iterator
1362{
1363  varpool_node_set set;
1364  unsigned index;
1365};
1366
1367/* Context of polymorphic call. It represent information about the type of
1368   instance that may reach the call.  This is used by ipa-devirt walkers of the
1369   type inheritance graph.  */
1370
1371class GTY(()) ipa_polymorphic_call_context {
1372public:
1373  /* The called object appears in an object of type OUTER_TYPE
1374     at offset OFFSET.  When information is not 100% reliable, we
1375     use SPECULATIVE_OUTER_TYPE and SPECULATIVE_OFFSET. */
1376  HOST_WIDE_INT offset;
1377  HOST_WIDE_INT speculative_offset;
1378  tree outer_type;
1379  tree speculative_outer_type;
1380  /* True if outer object may be in construction or destruction.  */
1381  unsigned maybe_in_construction : 1;
1382  /* True if outer object may be of derived type.  */
1383  unsigned maybe_derived_type : 1;
1384  /* True if speculative outer object may be of derived type.  We always
1385     speculate that construction does not happen.  */
1386  unsigned speculative_maybe_derived_type : 1;
1387  /* True if the context is invalid and all calls should be redirected
1388     to BUILTIN_UNREACHABLE.  */
1389  unsigned invalid : 1;
1390  /* True if the outer type is dynamic.  */
1391  unsigned dynamic : 1;
1392
1393  /* Build empty "I know nothing" context.  */
1394  ipa_polymorphic_call_context ();
1395  /* Build polymorphic call context for indirect call E.  */
1396  ipa_polymorphic_call_context (cgraph_edge *e);
1397  /* Build polymorphic call context for IP invariant CST.
1398     If specified, OTR_TYPE specify the type of polymorphic call
1399     that takes CST+OFFSET as a prameter.  */
1400  ipa_polymorphic_call_context (tree cst, tree otr_type = NULL,
1401				HOST_WIDE_INT offset = 0);
1402  /* Build context for pointer REF contained in FNDECL at statement STMT.
1403     if INSTANCE is non-NULL, return pointer to the object described by
1404     the context.  */
1405  ipa_polymorphic_call_context (tree fndecl, tree ref, gimple stmt,
1406				tree *instance = NULL);
1407
1408  /* Look for vtable stores or constructor calls to work out dynamic type
1409     of memory location.  */
1410  bool get_dynamic_type (tree, tree, tree, gimple);
1411
1412  /* Make context non-speculative.  */
1413  void clear_speculation ();
1414
1415  /* Produce context specifying all derrived types of OTR_TYPE.  If OTR_TYPE is
1416     NULL, the context is set to dummy "I know nothing" setting.  */
1417  void clear_outer_type (tree otr_type = NULL);
1418
1419  /* Walk container types and modify context to point to actual class
1420     containing OTR_TYPE (if non-NULL) as base class.
1421     Return true if resulting context is valid.
1422
1423     When CONSIDER_PLACEMENT_NEW is false, reject contexts that may be made
1424     valid only via alocation of new polymorphic type inside by means
1425     of placement new.
1426
1427     When CONSIDER_BASES is false, only look for actual fields, not base types
1428     of TYPE.  */
1429  bool restrict_to_inner_class (tree otr_type,
1430				bool consider_placement_new = true,
1431				bool consider_bases = true);
1432
1433  /* Adjust all offsets in contexts by given number of bits.  */
1434  void offset_by (HOST_WIDE_INT);
1435  /* Use when we can not track dynamic type change.  This speculatively assume
1436     type change is not happening.  */
1437  void possible_dynamic_type_change (bool, tree otr_type = NULL);
1438  /* Assume that both THIS and a given context is valid and strenghten THIS
1439     if possible.  Return true if any strenghtening was made.
1440     If actual type the context is being used in is known, OTR_TYPE should be
1441     set accordingly. This improves quality of combined result.  */
1442  bool combine_with (ipa_polymorphic_call_context, tree otr_type = NULL);
1443  bool meet_with (ipa_polymorphic_call_context, tree otr_type = NULL);
1444
1445  /* Return TRUE if context is fully useless.  */
1446  bool useless_p () const;
1447  /* Return TRUE if this context conveys the same information as X.  */
1448  bool equal_to (const ipa_polymorphic_call_context &x) const;
1449
1450  /* Dump human readable context to F.  If NEWLINE is true, it will be
1451     terminated by a newline.  */
1452  void dump (FILE *f, bool newline = true) const;
1453  void DEBUG_FUNCTION debug () const;
1454
1455  /* LTO streaming.  */
1456  void stream_out (struct output_block *) const;
1457  void stream_in (struct lto_input_block *, struct data_in *data_in);
1458
1459private:
1460  bool combine_speculation_with (tree, HOST_WIDE_INT, bool, tree);
1461  bool meet_speculation_with (tree, HOST_WIDE_INT, bool, tree);
1462  void set_by_decl (tree, HOST_WIDE_INT);
1463  bool set_by_invariant (tree, tree, HOST_WIDE_INT);
1464  bool speculation_consistent_p (tree, HOST_WIDE_INT, bool, tree) const;
1465  void make_speculative (tree otr_type = NULL);
1466};
1467
1468/* Structure containing additional information about an indirect call.  */
1469
1470struct GTY(()) cgraph_indirect_call_info
1471{
1472  /* When agg_content is set, an offset where the call pointer is located
1473     within the aggregate.  */
1474  HOST_WIDE_INT offset;
1475  /* Context of the polymorphic call; use only when POLYMORPHIC flag is set.  */
1476  ipa_polymorphic_call_context context;
1477  /* OBJ_TYPE_REF_TOKEN of a polymorphic call (if polymorphic is set).  */
1478  HOST_WIDE_INT otr_token;
1479  /* Type of the object from OBJ_TYPE_REF_OBJECT. */
1480  tree otr_type;
1481  /* Index of the parameter that is called.  */
1482  int param_index;
1483  /* ECF flags determined from the caller.  */
1484  int ecf_flags;
1485  /* Profile_id of common target obtrained from profile.  */
1486  int common_target_id;
1487  /* Probability that call will land in function with COMMON_TARGET_ID.  */
1488  int common_target_probability;
1489
1490  /* Set when the call is a virtual call with the parameter being the
1491     associated object pointer rather than a simple direct call.  */
1492  unsigned polymorphic : 1;
1493  /* Set when the call is a call of a pointer loaded from contents of an
1494     aggregate at offset.  */
1495  unsigned agg_contents : 1;
1496  /* Set when this is a call through a member pointer.  */
1497  unsigned member_ptr : 1;
1498  /* When the previous bit is set, this one determines whether the destination
1499     is loaded from a parameter passed by reference. */
1500  unsigned by_ref : 1;
1501  /* For polymorphic calls this specify whether the virtual table pointer
1502     may have changed in between function entry and the call.  */
1503  unsigned vptr_changed : 1;
1504};
1505
1506struct GTY((chain_next ("%h.next_caller"), chain_prev ("%h.prev_caller"),
1507	    for_user)) cgraph_edge {
1508  friend class cgraph_node;
1509
1510  /* Remove the edge in the cgraph.  */
1511  void remove (void);
1512
1513  /* Change field call_stmt of edge to NEW_STMT.
1514     If UPDATE_SPECULATIVE and E is any component of speculative
1515     edge, then update all components.  */
1516  void set_call_stmt (gcall *new_stmt, bool update_speculative = true);
1517
1518  /* Redirect callee of the edge to N.  The function does not update underlying
1519     call expression.  */
1520  void redirect_callee (cgraph_node *n);
1521
1522  /* If the edge does not lead to a thunk, simply redirect it to N.  Otherwise
1523     create one or more equivalent thunks for N and redirect E to the first in
1524     the chain.  Note that it is then necessary to call
1525     n->expand_all_artificial_thunks once all callers are redirected.  */
1526  void redirect_callee_duplicating_thunks (cgraph_node *n);
1527
1528  /* Make an indirect edge with an unknown callee an ordinary edge leading to
1529     CALLEE.  DELTA is an integer constant that is to be added to the this
1530     pointer (first parameter) to compensate for skipping
1531     a thunk adjustment.  */
1532  cgraph_edge *make_direct (cgraph_node *callee);
1533
1534  /* Turn edge into speculative call calling N2. Update
1535     the profile so the direct call is taken COUNT times
1536     with FREQUENCY.  */
1537  cgraph_edge *make_speculative (cgraph_node *n2, gcov_type direct_count,
1538				 int direct_frequency);
1539
1540   /* Given speculative call edge, return all three components.  */
1541  void speculative_call_info (cgraph_edge *&direct, cgraph_edge *&indirect,
1542			      ipa_ref *&reference);
1543
1544  /* Speculative call edge turned out to be direct call to CALLE_DECL.
1545     Remove the speculative call sequence and return edge representing the call.
1546     It is up to caller to redirect the call as appropriate. */
1547  cgraph_edge *resolve_speculation (tree callee_decl = NULL);
1548
1549  /* If necessary, change the function declaration in the call statement
1550     associated with the edge so that it corresponds to the edge callee.  */
1551  gimple redirect_call_stmt_to_callee (void);
1552
1553  /* Create clone of edge in the node N represented
1554     by CALL_EXPR the callgraph.  */
1555  cgraph_edge * clone (cgraph_node *n, gcall *call_stmt, unsigned stmt_uid,
1556		       gcov_type count_scale, int freq_scale, bool update_original);
1557
1558  /* Verify edge count and frequency.  */
1559  bool verify_count_and_frequency ();
1560
1561  /* Return true when call of edge can not lead to return from caller
1562     and thus it is safe to ignore its side effects for IPA analysis
1563     when computing side effects of the caller.  */
1564  bool cannot_lead_to_return_p (void);
1565
1566  /* Return true when the edge represents a direct recursion.  */
1567  bool recursive_p (void);
1568
1569  /* Return true if the call can be hot.  */
1570  bool maybe_hot_p (void);
1571
1572  /* Rebuild cgraph edges for current function node.  This needs to be run after
1573     passes that don't update the cgraph.  */
1574  static unsigned int rebuild_edges (void);
1575
1576  /* Rebuild cgraph references for current function node.  This needs to be run
1577     after passes that don't update the cgraph.  */
1578  static void rebuild_references (void);
1579
1580  /* Expected number of executions: calculated in profile.c.  */
1581  gcov_type count;
1582  cgraph_node *caller;
1583  cgraph_node *callee;
1584  cgraph_edge *prev_caller;
1585  cgraph_edge *next_caller;
1586  cgraph_edge *prev_callee;
1587  cgraph_edge *next_callee;
1588  gcall *call_stmt;
1589  /* Additional information about an indirect call.  Not cleared when an edge
1590     becomes direct.  */
1591  cgraph_indirect_call_info *indirect_info;
1592  PTR GTY ((skip (""))) aux;
1593  /* When equal to CIF_OK, inline this call.  Otherwise, points to the
1594     explanation why function was not inlined.  */
1595  enum cgraph_inline_failed_t inline_failed;
1596  /* The stmt_uid of call_stmt.  This is used by LTO to recover the call_stmt
1597     when the function is serialized in.  */
1598  unsigned int lto_stmt_uid;
1599  /* Expected frequency of executions within the function.
1600     When set to CGRAPH_FREQ_BASE, the edge is expected to be called once
1601     per function call.  The range is 0 to CGRAPH_FREQ_MAX.  */
1602  int frequency;
1603  /* Unique id of the edge.  */
1604  int uid;
1605  /* Whether this edge was made direct by indirect inlining.  */
1606  unsigned int indirect_inlining_edge : 1;
1607  /* Whether this edge describes an indirect call with an undetermined
1608     callee.  */
1609  unsigned int indirect_unknown_callee : 1;
1610  /* Whether this edge is still a dangling  */
1611  /* True if the corresponding CALL stmt cannot be inlined.  */
1612  unsigned int call_stmt_cannot_inline_p : 1;
1613  /* Can this call throw externally?  */
1614  unsigned int can_throw_external : 1;
1615  /* Edges with SPECULATIVE flag represents indirect calls that was
1616     speculatively turned into direct (i.e. by profile feedback).
1617     The final code sequence will have form:
1618
1619     if (call_target == expected_fn)
1620       expected_fn ();
1621     else
1622       call_target ();
1623
1624     Every speculative call is represented by three components attached
1625     to a same call statement:
1626     1) a direct call (to expected_fn)
1627     2) an indirect call (to call_target)
1628     3) a IPA_REF_ADDR refrence to expected_fn.
1629
1630     Optimizers may later redirect direct call to clone, so 1) and 3)
1631     do not need to necesarily agree with destination.  */
1632  unsigned int speculative : 1;
1633  /* Set to true when caller is a constructor or destructor of polymorphic
1634     type.  */
1635  unsigned in_polymorphic_cdtor : 1;
1636
1637private:
1638  /* Remove the edge from the list of the callers of the callee.  */
1639  void remove_caller (void);
1640
1641  /* Remove the edge from the list of the callees of the caller.  */
1642  void remove_callee (void);
1643
1644  /* Set callee N of call graph edge and add it to the corresponding set of
1645     callers. */
1646  void set_callee (cgraph_node *n);
1647
1648  /* Output flags of edge to a file F.  */
1649  void dump_edge_flags (FILE *f);
1650
1651  /* Verify that call graph edge corresponds to DECL from the associated
1652     statement.  Return true if the verification should fail.  */
1653  bool verify_corresponds_to_fndecl (tree decl);
1654};
1655
1656#define CGRAPH_FREQ_BASE 1000
1657#define CGRAPH_FREQ_MAX 100000
1658
1659/* The varpool data structure.
1660   Each static variable decl has assigned varpool_node.  */
1661
1662class GTY((tag ("SYMTAB_VARIABLE"))) varpool_node : public symtab_node {
1663public:
1664  /* Dump given varpool node to F.  */
1665  void dump (FILE *f);
1666
1667  /* Dump given varpool node to stderr.  */
1668  void DEBUG_FUNCTION debug (void);
1669
1670  /* Remove variable from symbol table.  */
1671  void remove (void);
1672
1673  /* Remove node initializer when it is no longer needed.  */
1674  void remove_initializer (void);
1675
1676  void analyze (void);
1677
1678  /* Return variable availability.  */
1679  availability get_availability (void);
1680
1681  /* When doing LTO, read variable's constructor from disk if
1682     it is not already present.  */
1683  tree get_constructor (void);
1684
1685  /* Return true if variable has constructor that can be used for folding.  */
1686  bool ctor_useable_for_folding_p (void);
1687
1688  /* For given variable pool node, walk the alias chain to return the function
1689     the variable is alias of. Do not walk through thunks.
1690     When AVAILABILITY is non-NULL, get minimal availability in the chain.  */
1691  inline varpool_node *ultimate_alias_target
1692    (availability *availability = NULL);
1693
1694  /* Return node that alias is aliasing.  */
1695  inline varpool_node *get_alias_target (void);
1696
1697  /* Output one variable, if necessary.  Return whether we output it.  */
1698  bool assemble_decl (void);
1699
1700  /* For variables in named sections make sure get_variable_section
1701     is called before we switch to those sections.  Then section
1702     conflicts between read-only and read-only requiring relocations
1703     sections can be resolved.  */
1704  void finalize_named_section_flags (void);
1705
1706  /* Call calback on varpool symbol and aliases associated to varpool symbol.
1707     When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
1708     skipped. */
1709  bool call_for_symbol_and_aliases (bool (*callback) (varpool_node *, void *),
1710				    void *data,
1711				    bool include_overwritable);
1712
1713  /* Return true when variable should be considered externally visible.  */
1714  bool externally_visible_p (void);
1715
1716  /* Return true when all references to variable must be visible
1717     in ipa_ref_list.
1718     i.e. if the variable is not externally visible or not used in some magic
1719     way (asm statement or such).
1720     The magic uses are all summarized in force_output flag.  */
1721  inline bool all_refs_explicit_p ();
1722
1723  /* Return true when variable can be removed from variable pool
1724     if all direct calls are eliminated.  */
1725  inline bool can_remove_if_no_refs_p (void);
1726
1727  /* Add the variable DECL to the varpool.
1728     Unlike finalize_decl function is intended to be used
1729     by middle end and allows insertion of new variable at arbitrary point
1730     of compilation.  */
1731  static void add (tree decl);
1732
1733  /* Return varpool node for given symbol and check it is a function. */
1734  static inline varpool_node *get (const_tree decl);
1735
1736  /* Mark DECL as finalized.  By finalizing the declaration, frontend instruct
1737     the middle end to output the variable to asm file, if needed or externally
1738     visible.  */
1739  static void finalize_decl (tree decl);
1740
1741  /* Attempt to mark ALIAS as an alias to DECL.  Return TRUE if successful.
1742     Extra name aliases are output whenever DECL is output.  */
1743  static varpool_node * create_extra_name_alias (tree alias, tree decl);
1744
1745  /* Attempt to mark ALIAS as an alias to DECL.  Return TRUE if successful.
1746     Extra name aliases are output whenever DECL is output.  */
1747  static varpool_node * create_alias (tree, tree);
1748
1749  /* Dump the variable pool to F.  */
1750  static void dump_varpool (FILE *f);
1751
1752  /* Dump the variable pool to stderr.  */
1753  static void DEBUG_FUNCTION debug_varpool (void);
1754
1755  /* Allocate new callgraph node and insert it into basic data structures.  */
1756  static varpool_node *create_empty (void);
1757
1758  /* Return varpool node assigned to DECL.  Create new one when needed.  */
1759  static varpool_node *get_create (tree decl);
1760
1761  /* Given an assembler name, lookup node.  */
1762  static varpool_node *get_for_asmname (tree asmname);
1763
1764  /* Set when variable is scheduled to be assembled.  */
1765  unsigned output : 1;
1766
1767  /* Set when variable has statically initialized pointer
1768     or is a static bounds variable and needs initalization.  */
1769  unsigned need_bounds_init : 1;
1770
1771  /* Set if the variable is dynamically initialized, except for
1772     function local statics.   */
1773  unsigned dynamically_initialized : 1;
1774
1775  ENUM_BITFIELD(tls_model) tls_model : 3;
1776
1777  /* Set if the variable is known to be used by single function only.
1778     This is computed by ipa_signle_use pass and used by late optimizations
1779     in places where optimization would be valid for local static variable
1780     if we did not do any inter-procedural code movement.  */
1781  unsigned used_by_single_function : 1;
1782
1783private:
1784  /* Assemble thunks and aliases associated to varpool node.  */
1785  void assemble_aliases (void);
1786
1787  /* Worker for call_for_node_and_aliases.  */
1788  bool call_for_symbol_and_aliases_1 (bool (*callback) (varpool_node *, void *),
1789				      void *data,
1790				      bool include_overwritable);
1791};
1792
1793/* Every top level asm statement is put into a asm_node.  */
1794
1795struct GTY(()) asm_node {
1796
1797
1798  /* Next asm node.  */
1799  asm_node *next;
1800  /* String for this asm node.  */
1801  tree asm_str;
1802  /* Ordering of all cgraph nodes.  */
1803  int order;
1804};
1805
1806/* Report whether or not THIS symtab node is a function, aka cgraph_node.  */
1807
1808template <>
1809template <>
1810inline bool
1811is_a_helper <cgraph_node *>::test (symtab_node *p)
1812{
1813  return p && p->type == SYMTAB_FUNCTION;
1814}
1815
1816/* Report whether or not THIS symtab node is a vriable, aka varpool_node.  */
1817
1818template <>
1819template <>
1820inline bool
1821is_a_helper <varpool_node *>::test (symtab_node *p)
1822{
1823  return p && p->type == SYMTAB_VARIABLE;
1824}
1825
1826/* Macros to access the next item in the list of free cgraph nodes and
1827   edges. */
1828#define NEXT_FREE_NODE(NODE) dyn_cast<cgraph_node *> ((NODE)->next)
1829#define SET_NEXT_FREE_NODE(NODE,NODE2) ((NODE))->next = NODE2
1830#define NEXT_FREE_EDGE(EDGE) (EDGE)->prev_caller
1831
1832typedef void (*cgraph_edge_hook)(cgraph_edge *, void *);
1833typedef void (*cgraph_node_hook)(cgraph_node *, void *);
1834typedef void (*varpool_node_hook)(varpool_node *, void *);
1835typedef void (*cgraph_2edge_hook)(cgraph_edge *, cgraph_edge *, void *);
1836typedef void (*cgraph_2node_hook)(cgraph_node *, cgraph_node *, void *);
1837
1838struct cgraph_edge_hook_list;
1839struct cgraph_node_hook_list;
1840struct varpool_node_hook_list;
1841struct cgraph_2edge_hook_list;
1842struct cgraph_2node_hook_list;
1843
1844/* Map from a symbol to initialization/finalization priorities.  */
1845struct GTY(()) symbol_priority_map {
1846  priority_type init;
1847  priority_type fini;
1848};
1849
1850enum symtab_state
1851{
1852  /* Frontend is parsing and finalizing functions.  */
1853  PARSING,
1854  /* Callgraph is being constructed.  It is safe to add new functions.  */
1855  CONSTRUCTION,
1856  /* Callgraph is being streamed-in at LTO time.  */
1857  LTO_STREAMING,
1858  /* Callgraph is built and early IPA passes are being run.  */
1859  IPA,
1860  /* Callgraph is built and all functions are transformed to SSA form.  */
1861  IPA_SSA,
1862  /* All inline decisions are done; it is now possible to remove extern inline
1863     functions and virtual call targets.  */
1864  IPA_SSA_AFTER_INLINING,
1865  /* Functions are now ordered and being passed to RTL expanders.  */
1866  EXPANSION,
1867  /* All cgraph expansion is done.  */
1868  FINISHED
1869};
1870
1871struct asmname_hasher
1872{
1873  typedef symtab_node *value_type;
1874  typedef const_tree compare_type;
1875  typedef int store_values_directly;
1876
1877  static hashval_t hash (symtab_node *n);
1878  static bool equal (symtab_node *n, const_tree t);
1879  static void ggc_mx (symtab_node *n);
1880  static void pch_nx (symtab_node *&);
1881  static void pch_nx (symtab_node *&, gt_pointer_operator, void *);
1882  static void remove (symtab_node *) {}
1883};
1884
1885class GTY((tag ("SYMTAB"))) symbol_table
1886{
1887public:
1888  friend class symtab_node;
1889  friend class cgraph_node;
1890  friend class cgraph_edge;
1891
1892  symbol_table (): cgraph_max_summary_uid (1)
1893  {
1894  }
1895
1896  /* Initialize callgraph dump file.  */
1897  void initialize (void);
1898
1899  /* Register a top-level asm statement ASM_STR.  */
1900  inline asm_node *finalize_toplevel_asm (tree asm_str);
1901
1902  /* Analyze the whole compilation unit once it is parsed completely.  */
1903  void finalize_compilation_unit (void);
1904
1905  /* C++ frontend produce same body aliases all over the place, even before PCH
1906     gets streamed out. It relies on us linking the aliases with their function
1907     in order to do the fixups, but ipa-ref is not PCH safe.  Consequentely we
1908     first produce aliases without links, but once C++ FE is sure he won't sream
1909     PCH we build the links via this function.  */
1910  void process_same_body_aliases (void);
1911
1912  /* Perform simple optimizations based on callgraph.  */
1913  void compile (void);
1914
1915  /* Process CGRAPH_NEW_FUNCTIONS and perform actions necessary to add these
1916     functions into callgraph in a way so they look like ordinary reachable
1917     functions inserted into callgraph already at construction time.  */
1918  void process_new_functions (void);
1919
1920  /* Once all functions from compilation unit are in memory, produce all clones
1921     and update all calls.  We might also do this on demand if we don't want to
1922     bring all functions to memory prior compilation, but current WHOPR
1923     implementation does that and it is is bit easier to keep everything right
1924     in this order.  */
1925  void materialize_all_clones (void);
1926
1927  /* Register a symbol NODE.  */
1928  inline void register_symbol (symtab_node *node);
1929
1930  inline void
1931  clear_asm_symbols (void)
1932  {
1933    asmnodes = NULL;
1934    asm_last_node = NULL;
1935  }
1936
1937  /* Perform reachability analysis and reclaim all unreachable nodes.  */
1938  bool remove_unreachable_nodes (FILE *file);
1939
1940  /* Optimization of function bodies might've rendered some variables as
1941     unnecessary so we want to avoid these from being compiled.  Re-do
1942     reachability starting from variables that are either externally visible
1943     or was referred from the asm output routines.  */
1944  void remove_unreferenced_decls (void);
1945
1946  /* Unregister a symbol NODE.  */
1947  inline void unregister (symtab_node *node);
1948
1949  /* Allocate new callgraph node and insert it into basic data structures.  */
1950  cgraph_node *create_empty (void);
1951
1952  /* Release a callgraph NODE with UID and put in to the list
1953     of free nodes.  */
1954  void release_symbol (cgraph_node *node, int uid);
1955
1956  /* Output all variables enqueued to be assembled.  */
1957  bool output_variables (void);
1958
1959  /* Weakrefs may be associated to external decls and thus not output
1960     at expansion time.  Emit all necessary aliases.  */
1961  void output_weakrefs (void);
1962
1963  /* Return first static symbol with definition.  */
1964  inline symtab_node *first_symbol (void);
1965
1966  /* Return first assembler symbol.  */
1967  inline asm_node *
1968  first_asm_symbol (void)
1969  {
1970    return asmnodes;
1971  }
1972
1973  /* Return first static symbol with definition.  */
1974  inline symtab_node *first_defined_symbol (void);
1975
1976  /* Return first variable.  */
1977  inline varpool_node *first_variable (void);
1978
1979  /* Return next variable after NODE.  */
1980  inline varpool_node *next_variable (varpool_node *node);
1981
1982  /* Return first static variable with initializer.  */
1983  inline varpool_node *first_static_initializer (void);
1984
1985  /* Return next static variable with initializer after NODE.  */
1986  inline varpool_node *next_static_initializer (varpool_node *node);
1987
1988  /* Return first static variable with definition.  */
1989  inline varpool_node *first_defined_variable (void);
1990
1991  /* Return next static variable with definition after NODE.  */
1992  inline varpool_node *next_defined_variable (varpool_node *node);
1993
1994  /* Return first function with body defined.  */
1995  inline cgraph_node *first_defined_function (void);
1996
1997  /* Return next function with body defined after NODE.  */
1998  inline cgraph_node *next_defined_function (cgraph_node *node);
1999
2000  /* Return first function.  */
2001  inline cgraph_node *first_function (void);
2002
2003  /* Return next function.  */
2004  inline cgraph_node *next_function (cgraph_node *node);
2005
2006  /* Return first function with body defined.  */
2007  cgraph_node *first_function_with_gimple_body (void);
2008
2009  /* Return next reachable static variable with initializer after NODE.  */
2010  inline cgraph_node *next_function_with_gimple_body (cgraph_node *node);
2011
2012  /* Register HOOK to be called with DATA on each removed edge.  */
2013  cgraph_edge_hook_list *add_edge_removal_hook (cgraph_edge_hook hook,
2014						void *data);
2015
2016  /* Remove ENTRY from the list of hooks called on removing edges.  */
2017  void remove_edge_removal_hook (cgraph_edge_hook_list *entry);
2018
2019  /* Register HOOK to be called with DATA on each removed node.  */
2020  cgraph_node_hook_list *add_cgraph_removal_hook (cgraph_node_hook hook,
2021						  void *data);
2022
2023  /* Remove ENTRY from the list of hooks called on removing nodes.  */
2024  void remove_cgraph_removal_hook (cgraph_node_hook_list *entry);
2025
2026  /* Register HOOK to be called with DATA on each removed node.  */
2027  varpool_node_hook_list *add_varpool_removal_hook (varpool_node_hook hook,
2028						    void *data);
2029
2030  /* Remove ENTRY from the list of hooks called on removing nodes.  */
2031  void remove_varpool_removal_hook (varpool_node_hook_list *entry);
2032
2033  /* Register HOOK to be called with DATA on each inserted node.  */
2034  cgraph_node_hook_list *add_cgraph_insertion_hook (cgraph_node_hook hook,
2035						    void *data);
2036
2037  /* Remove ENTRY from the list of hooks called on inserted nodes.  */
2038  void remove_cgraph_insertion_hook (cgraph_node_hook_list *entry);
2039
2040  /* Register HOOK to be called with DATA on each inserted node.  */
2041  varpool_node_hook_list *add_varpool_insertion_hook (varpool_node_hook hook,
2042						      void *data);
2043
2044  /* Remove ENTRY from the list of hooks called on inserted nodes.  */
2045  void remove_varpool_insertion_hook (varpool_node_hook_list *entry);
2046
2047  /* Register HOOK to be called with DATA on each duplicated edge.  */
2048  cgraph_2edge_hook_list *add_edge_duplication_hook (cgraph_2edge_hook hook,
2049						     void *data);
2050  /* Remove ENTRY from the list of hooks called on duplicating edges.  */
2051  void remove_edge_duplication_hook (cgraph_2edge_hook_list *entry);
2052
2053  /* Register HOOK to be called with DATA on each duplicated node.  */
2054  cgraph_2node_hook_list *add_cgraph_duplication_hook (cgraph_2node_hook hook,
2055						       void *data);
2056
2057  /* Remove ENTRY from the list of hooks called on duplicating nodes.  */
2058  void remove_cgraph_duplication_hook (cgraph_2node_hook_list *entry);
2059
2060  /* Call all edge removal hooks.  */
2061  void call_edge_removal_hooks (cgraph_edge *e);
2062
2063  /* Call all node insertion hooks.  */
2064  void call_cgraph_insertion_hooks (cgraph_node *node);
2065
2066  /* Call all node removal hooks.  */
2067  void call_cgraph_removal_hooks (cgraph_node *node);
2068
2069  /* Call all node duplication hooks.  */
2070  void call_cgraph_duplication_hooks (cgraph_node *node, cgraph_node *node2);
2071
2072  /* Call all edge duplication hooks.  */
2073  void call_edge_duplication_hooks (cgraph_edge *cs1, cgraph_edge *cs2);
2074
2075  /* Call all node removal hooks.  */
2076  void call_varpool_removal_hooks (varpool_node *node);
2077
2078  /* Call all node insertion hooks.  */
2079  void call_varpool_insertion_hooks (varpool_node *node);
2080
2081  /* Arrange node to be first in its entry of assembler_name_hash.  */
2082  void symtab_prevail_in_asm_name_hash (symtab_node *node);
2083
2084  /* Initalize asm name hash unless.  */
2085  void symtab_initialize_asm_name_hash (void);
2086
2087  /* Set the DECL_ASSEMBLER_NAME and update symtab hashtables.  */
2088  void change_decl_assembler_name (tree decl, tree name);
2089
2090  int cgraph_count;
2091  int cgraph_max_uid;
2092  int cgraph_max_summary_uid;
2093
2094  int edges_count;
2095  int edges_max_uid;
2096
2097  symtab_node* GTY(()) nodes;
2098  asm_node* GTY(()) asmnodes;
2099  asm_node* GTY(()) asm_last_node;
2100  cgraph_node* GTY(()) free_nodes;
2101
2102  /* Head of a linked list of unused (freed) call graph edges.
2103     Do not GTY((delete)) this list so UIDs gets reliably recycled.  */
2104  cgraph_edge * GTY(()) free_edges;
2105
2106  /* The order index of the next symtab node to be created.  This is
2107     used so that we can sort the cgraph nodes in order by when we saw
2108     them, to support -fno-toplevel-reorder.  */
2109  int order;
2110
2111  /* Set when whole unit has been analyzed so we can access global info.  */
2112  bool global_info_ready;
2113  /* What state callgraph is in right now.  */
2114  enum symtab_state state;
2115  /* Set when the cgraph is fully build and the basic flags are computed.  */
2116  bool function_flags_ready;
2117
2118  bool cpp_implicit_aliases_done;
2119
2120  /* Hash table used to hold sectoons.  */
2121  hash_table<section_name_hasher> *GTY(()) section_hash;
2122
2123  /* Hash table used to convert assembler names into nodes.  */
2124  hash_table<asmname_hasher> *assembler_name_hash;
2125
2126  /* Hash table used to hold init priorities.  */
2127  hash_map<symtab_node *, symbol_priority_map> *init_priority_hash;
2128
2129  FILE* GTY ((skip)) dump_file;
2130
2131private:
2132  /* Allocate new callgraph node.  */
2133  inline cgraph_node * allocate_cgraph_symbol (void);
2134
2135  /* Allocate a cgraph_edge structure and fill it with data according to the
2136     parameters of which only CALLEE can be NULL (when creating an indirect call
2137     edge).  */
2138  cgraph_edge *create_edge (cgraph_node *caller, cgraph_node *callee,
2139			    gcall *call_stmt, gcov_type count, int freq,
2140			    bool indir_unknown_callee);
2141
2142  /* Put the edge onto the free list.  */
2143  void free_edge (cgraph_edge *e);
2144
2145  /* Insert NODE to assembler name hash.  */
2146  void insert_to_assembler_name_hash (symtab_node *node, bool with_clones);
2147
2148  /* Remove NODE from assembler name hash.  */
2149  void unlink_from_assembler_name_hash (symtab_node *node, bool with_clones);
2150
2151  /* Hash asmnames ignoring the user specified marks.  */
2152  static hashval_t decl_assembler_name_hash (const_tree asmname);
2153
2154  /* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL.  */
2155  static bool decl_assembler_name_equal (tree decl, const_tree asmname);
2156
2157  friend struct asmname_hasher;
2158
2159  /* List of hooks triggered when an edge is removed.  */
2160  cgraph_edge_hook_list * GTY((skip)) m_first_edge_removal_hook;
2161  /* List of hooks triggem_red when a cgraph node is removed.  */
2162  cgraph_node_hook_list * GTY((skip)) m_first_cgraph_removal_hook;
2163  /* List of hooks triggered when an edge is duplicated.  */
2164  cgraph_2edge_hook_list * GTY((skip)) m_first_edge_duplicated_hook;
2165  /* List of hooks triggered when a node is duplicated.  */
2166  cgraph_2node_hook_list * GTY((skip)) m_first_cgraph_duplicated_hook;
2167  /* List of hooks triggered when an function is inserted.  */
2168  cgraph_node_hook_list * GTY((skip)) m_first_cgraph_insertion_hook;
2169  /* List of hooks triggered when an variable is inserted.  */
2170  varpool_node_hook_list * GTY((skip)) m_first_varpool_insertion_hook;
2171  /* List of hooks triggered when a node is removed.  */
2172  varpool_node_hook_list * GTY((skip)) m_first_varpool_removal_hook;
2173};
2174
2175extern GTY(()) symbol_table *symtab;
2176
2177extern vec<cgraph_node *> cgraph_new_nodes;
2178
2179inline hashval_t
2180asmname_hasher::hash (symtab_node *n)
2181{
2182  return symbol_table::decl_assembler_name_hash
2183    (DECL_ASSEMBLER_NAME (n->decl));
2184}
2185
2186inline bool
2187asmname_hasher::equal (symtab_node *n, const_tree t)
2188{
2189  return symbol_table::decl_assembler_name_equal (n->decl, t);
2190}
2191
2192extern void gt_ggc_mx (symtab_node *&);
2193
2194inline void
2195asmname_hasher::ggc_mx (symtab_node *n)
2196{
2197  gt_ggc_mx (n);
2198}
2199
2200extern void gt_pch_nx (symtab_node *&);
2201
2202inline void
2203asmname_hasher::pch_nx (symtab_node *&n)
2204{
2205  gt_pch_nx (n);
2206}
2207
2208inline void
2209asmname_hasher::pch_nx (symtab_node *&n, gt_pointer_operator op, void *cookie)
2210{
2211  op (&n, cookie);
2212}
2213
2214/* In cgraph.c  */
2215void cgraph_c_finalize (void);
2216void release_function_body (tree);
2217cgraph_indirect_call_info *cgraph_allocate_init_indirect_info (void);
2218
2219void cgraph_update_edges_for_call_stmt (gimple, tree, gimple);
2220bool cgraph_function_possibly_inlined_p (tree);
2221
2222const char* cgraph_inline_failed_string (cgraph_inline_failed_t);
2223cgraph_inline_failed_type_t cgraph_inline_failed_type (cgraph_inline_failed_t);
2224
2225extern bool gimple_check_call_matching_types (gimple, tree, bool);
2226
2227/* In cgraphunit.c  */
2228void cgraphunit_c_finalize (void);
2229
2230/*  Initialize datastructures so DECL is a function in lowered gimple form.
2231    IN_SSA is true if the gimple is in SSA.  */
2232basic_block init_lowered_empty_function (tree, bool, gcov_type);
2233
2234/* In cgraphclones.c  */
2235
2236tree clone_function_name_1 (const char *, const char *);
2237tree clone_function_name (tree decl, const char *);
2238
2239void tree_function_versioning (tree, tree, vec<ipa_replace_map *, va_gc> *,
2240			       bool, bitmap, bool, bitmap, basic_block);
2241
2242/* In cgraphbuild.c  */
2243int compute_call_stmt_bb_frequency (tree, basic_block bb);
2244void record_references_in_initializer (tree, bool);
2245
2246/* In ipa.c  */
2247void cgraph_build_static_cdtor (char which, tree body, int priority);
2248bool ipa_discover_readonly_nonaddressable_vars (void);
2249
2250/* In varpool.c  */
2251tree ctor_for_folding (tree);
2252
2253/* In tree-chkp.c  */
2254extern bool chkp_function_instrumented_p (tree fndecl);
2255
2256/* Return true when the symbol is real symbol, i.e. it is not inline clone
2257   or abstract function kept for debug info purposes only.  */
2258inline bool
2259symtab_node::real_symbol_p (void)
2260{
2261  cgraph_node *cnode;
2262
2263  if (DECL_ABSTRACT_P (decl))
2264    return false;
2265  if (!is_a <cgraph_node *> (this))
2266    return true;
2267  cnode = dyn_cast <cgraph_node *> (this);
2268  if (cnode->global.inlined_to)
2269    return false;
2270  return true;
2271}
2272
2273/* Return true if DECL should have entry in symbol table if used.
2274   Those are functions and static & external veriables*/
2275
2276static inline bool
2277decl_in_symtab_p (const_tree decl)
2278{
2279  return (TREE_CODE (decl) == FUNCTION_DECL
2280          || (TREE_CODE (decl) == VAR_DECL
2281	      && (TREE_STATIC (decl) || DECL_EXTERNAL (decl))));
2282}
2283
2284inline bool
2285symtab_node::in_same_comdat_group_p (symtab_node *target)
2286{
2287  symtab_node *source = this;
2288
2289  if (cgraph_node *cn = dyn_cast <cgraph_node *> (target))
2290    {
2291      if (cn->global.inlined_to)
2292	source = cn->global.inlined_to;
2293    }
2294  if (cgraph_node *cn = dyn_cast <cgraph_node *> (target))
2295    {
2296      if (cn->global.inlined_to)
2297	target = cn->global.inlined_to;
2298    }
2299
2300  return source->get_comdat_group () == target->get_comdat_group ();
2301}
2302
2303/* Return node that alias is aliasing.  */
2304
2305inline symtab_node *
2306symtab_node::get_alias_target (void)
2307{
2308  ipa_ref *ref = NULL;
2309  iterate_reference (0, ref);
2310  if (ref->use == IPA_REF_CHKP)
2311    iterate_reference (1, ref);
2312  gcc_checking_assert (ref->use == IPA_REF_ALIAS);
2313  return ref->referred;
2314}
2315
2316/* Return next reachable static symbol with initializer after the node.  */
2317
2318inline symtab_node *
2319symtab_node::next_defined_symbol (void)
2320{
2321  symtab_node *node1 = next;
2322
2323  for (; node1; node1 = node1->next)
2324    if (node1->definition)
2325      return node1;
2326
2327  return NULL;
2328}
2329
2330/* Iterates I-th reference in the list, REF is also set.  */
2331
2332inline ipa_ref *
2333symtab_node::iterate_reference (unsigned i, ipa_ref *&ref)
2334{
2335  vec_safe_iterate (ref_list.references, i, &ref);
2336
2337  return ref;
2338}
2339
2340/* Iterates I-th referring item in the list, REF is also set.  */
2341
2342inline ipa_ref *
2343symtab_node::iterate_referring (unsigned i, ipa_ref *&ref)
2344{
2345  ref_list.referring.iterate (i, &ref);
2346
2347  return ref;
2348}
2349
2350/* Iterates I-th referring alias item in the list, REF is also set.  */
2351
2352inline ipa_ref *
2353symtab_node::iterate_direct_aliases (unsigned i, ipa_ref *&ref)
2354{
2355  ref_list.referring.iterate (i, &ref);
2356
2357  if (ref && ref->use != IPA_REF_ALIAS)
2358    return NULL;
2359
2360  return ref;
2361}
2362
2363/* Return true if list contains an alias.  */
2364
2365inline bool
2366symtab_node::has_aliases_p (void)
2367{
2368  ipa_ref *ref = NULL;
2369
2370  return (iterate_direct_aliases (0, ref) != NULL);
2371}
2372
2373/* Return true when RESOLUTION indicate that linker will use
2374   the symbol from non-LTO object files.  */
2375
2376inline bool
2377resolution_used_from_other_file_p (enum ld_plugin_symbol_resolution resolution)
2378{
2379  return (resolution == LDPR_PREVAILING_DEF
2380	  || resolution == LDPR_PREEMPTED_REG
2381	  || resolution == LDPR_RESOLVED_EXEC
2382	  || resolution == LDPR_RESOLVED_DYN);
2383}
2384
2385/* Return true when symtab_node is known to be used from other (non-LTO)
2386   object file. Known only when doing LTO via linker plugin.  */
2387
2388inline bool
2389symtab_node::used_from_object_file_p (void)
2390{
2391  if (!TREE_PUBLIC (decl) || DECL_EXTERNAL (decl))
2392    return false;
2393  if (resolution_used_from_other_file_p (resolution))
2394    return true;
2395  return false;
2396}
2397
2398/* Return varpool node for given symbol and check it is a function. */
2399
2400inline varpool_node *
2401varpool_node::get (const_tree decl)
2402{
2403  gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
2404  return dyn_cast<varpool_node *> (symtab_node::get (decl));
2405}
2406
2407/* Register a symbol NODE.  */
2408
2409inline void
2410symbol_table::register_symbol (symtab_node *node)
2411{
2412  node->next = nodes;
2413  node->previous = NULL;
2414
2415  if (nodes)
2416    nodes->previous = node;
2417  nodes = node;
2418
2419  node->order = order++;
2420}
2421
2422/* Register a top-level asm statement ASM_STR.  */
2423
2424asm_node *
2425symbol_table::finalize_toplevel_asm (tree asm_str)
2426{
2427  asm_node *node;
2428
2429  node = ggc_cleared_alloc<asm_node> ();
2430  node->asm_str = asm_str;
2431  node->order = order++;
2432  node->next = NULL;
2433
2434  if (asmnodes == NULL)
2435    asmnodes = node;
2436  else
2437    asm_last_node->next = node;
2438
2439  asm_last_node = node;
2440  return node;
2441}
2442
2443/* Unregister a symbol NODE.  */
2444inline void
2445symbol_table::unregister (symtab_node *node)
2446{
2447  if (node->previous)
2448    node->previous->next = node->next;
2449  else
2450    nodes = node->next;
2451
2452  if (node->next)
2453    node->next->previous = node->previous;
2454
2455  node->next = NULL;
2456  node->previous = NULL;
2457}
2458
2459/* Release a callgraph NODE with UID and put in to the list of free nodes.  */
2460
2461inline void
2462symbol_table::release_symbol (cgraph_node *node, int uid)
2463{
2464  cgraph_count--;
2465
2466  /* Clear out the node to NULL all pointers and add the node to the free
2467     list.  */
2468  memset (node, 0, sizeof (*node));
2469  node->type = SYMTAB_FUNCTION;
2470  node->uid = uid;
2471  SET_NEXT_FREE_NODE (node, free_nodes);
2472  free_nodes = node;
2473}
2474
2475/* Allocate new callgraph node.  */
2476
2477inline cgraph_node *
2478symbol_table::allocate_cgraph_symbol (void)
2479{
2480  cgraph_node *node;
2481
2482  if (free_nodes)
2483    {
2484      node = free_nodes;
2485      free_nodes = NEXT_FREE_NODE (node);
2486    }
2487  else
2488    {
2489      node = ggc_cleared_alloc<cgraph_node> ();
2490      node->uid = cgraph_max_uid++;
2491    }
2492
2493  node->summary_uid = cgraph_max_summary_uid++;
2494  return node;
2495}
2496
2497
2498/* Return first static symbol with definition.  */
2499inline symtab_node *
2500symbol_table::first_symbol (void)
2501{
2502  return nodes;
2503}
2504
2505/* Walk all symbols.  */
2506#define FOR_EACH_SYMBOL(node) \
2507   for ((node) = symtab->first_symbol (); (node); (node) = (node)->next)
2508
2509/* Return first static symbol with definition.  */
2510inline symtab_node *
2511symbol_table::first_defined_symbol (void)
2512{
2513  symtab_node *node;
2514
2515  for (node = nodes; node; node = node->next)
2516    if (node->definition)
2517      return node;
2518
2519  return NULL;
2520}
2521
2522/* Walk all symbols with definitions in current unit.  */
2523#define FOR_EACH_DEFINED_SYMBOL(node) \
2524   for ((node) = symtab->first_defined_symbol (); (node); \
2525	(node) = node->next_defined_symbol ())
2526
2527/* Return first variable.  */
2528inline varpool_node *
2529symbol_table::first_variable (void)
2530{
2531  symtab_node *node;
2532  for (node = nodes; node; node = node->next)
2533    if (varpool_node *vnode = dyn_cast <varpool_node *> (node))
2534      return vnode;
2535  return NULL;
2536}
2537
2538/* Return next variable after NODE.  */
2539inline varpool_node *
2540symbol_table::next_variable (varpool_node *node)
2541{
2542  symtab_node *node1 = node->next;
2543  for (; node1; node1 = node1->next)
2544    if (varpool_node *vnode1 = dyn_cast <varpool_node *> (node1))
2545      return vnode1;
2546  return NULL;
2547}
2548/* Walk all variables.  */
2549#define FOR_EACH_VARIABLE(node) \
2550   for ((node) = symtab->first_variable (); \
2551        (node); \
2552	(node) = symtab->next_variable ((node)))
2553
2554/* Return first static variable with initializer.  */
2555inline varpool_node *
2556symbol_table::first_static_initializer (void)
2557{
2558  symtab_node *node;
2559  for (node = nodes; node; node = node->next)
2560    {
2561      varpool_node *vnode = dyn_cast <varpool_node *> (node);
2562      if (vnode && DECL_INITIAL (node->decl))
2563	return vnode;
2564    }
2565  return NULL;
2566}
2567
2568/* Return next static variable with initializer after NODE.  */
2569inline varpool_node *
2570symbol_table::next_static_initializer (varpool_node *node)
2571{
2572  symtab_node *node1 = node->next;
2573  for (; node1; node1 = node1->next)
2574    {
2575      varpool_node *vnode1 = dyn_cast <varpool_node *> (node1);
2576      if (vnode1 && DECL_INITIAL (node1->decl))
2577	return vnode1;
2578    }
2579  return NULL;
2580}
2581
2582/* Walk all static variables with initializer set.  */
2583#define FOR_EACH_STATIC_INITIALIZER(node) \
2584   for ((node) = symtab->first_static_initializer (); (node); \
2585	(node) = symtab->next_static_initializer (node))
2586
2587/* Return first static variable with definition.  */
2588inline varpool_node *
2589symbol_table::first_defined_variable (void)
2590{
2591  symtab_node *node;
2592  for (node = nodes; node; node = node->next)
2593    {
2594      varpool_node *vnode = dyn_cast <varpool_node *> (node);
2595      if (vnode && vnode->definition)
2596	return vnode;
2597    }
2598  return NULL;
2599}
2600
2601/* Return next static variable with definition after NODE.  */
2602inline varpool_node *
2603symbol_table::next_defined_variable (varpool_node *node)
2604{
2605  symtab_node *node1 = node->next;
2606  for (; node1; node1 = node1->next)
2607    {
2608      varpool_node *vnode1 = dyn_cast <varpool_node *> (node1);
2609      if (vnode1 && vnode1->definition)
2610	return vnode1;
2611    }
2612  return NULL;
2613}
2614/* Walk all variables with definitions in current unit.  */
2615#define FOR_EACH_DEFINED_VARIABLE(node) \
2616   for ((node) = symtab->first_defined_variable (); (node); \
2617	(node) = symtab->next_defined_variable (node))
2618
2619/* Return first function with body defined.  */
2620inline cgraph_node *
2621symbol_table::first_defined_function (void)
2622{
2623  symtab_node *node;
2624  for (node = nodes; node; node = node->next)
2625    {
2626      cgraph_node *cn = dyn_cast <cgraph_node *> (node);
2627      if (cn && cn->definition)
2628	return cn;
2629    }
2630  return NULL;
2631}
2632
2633/* Return next function with body defined after NODE.  */
2634inline cgraph_node *
2635symbol_table::next_defined_function (cgraph_node *node)
2636{
2637  symtab_node *node1 = node->next;
2638  for (; node1; node1 = node1->next)
2639    {
2640      cgraph_node *cn1 = dyn_cast <cgraph_node *> (node1);
2641      if (cn1 && cn1->definition)
2642	return cn1;
2643    }
2644  return NULL;
2645}
2646
2647/* Walk all functions with body defined.  */
2648#define FOR_EACH_DEFINED_FUNCTION(node) \
2649   for ((node) = symtab->first_defined_function (); (node); \
2650	(node) = symtab->next_defined_function ((node)))
2651
2652/* Return first function.  */
2653inline cgraph_node *
2654symbol_table::first_function (void)
2655{
2656  symtab_node *node;
2657  for (node = nodes; node; node = node->next)
2658    if (cgraph_node *cn = dyn_cast <cgraph_node *> (node))
2659      return cn;
2660  return NULL;
2661}
2662
2663/* Return next function.  */
2664inline cgraph_node *
2665symbol_table::next_function (cgraph_node *node)
2666{
2667  symtab_node *node1 = node->next;
2668  for (; node1; node1 = node1->next)
2669    if (cgraph_node *cn1 = dyn_cast <cgraph_node *> (node1))
2670      return cn1;
2671  return NULL;
2672}
2673
2674/* Return first function with body defined.  */
2675inline cgraph_node *
2676symbol_table::first_function_with_gimple_body (void)
2677{
2678  symtab_node *node;
2679  for (node = nodes; node; node = node->next)
2680    {
2681      cgraph_node *cn = dyn_cast <cgraph_node *> (node);
2682      if (cn && cn->has_gimple_body_p ())
2683	return cn;
2684    }
2685  return NULL;
2686}
2687
2688/* Return next reachable static variable with initializer after NODE.  */
2689inline cgraph_node *
2690symbol_table::next_function_with_gimple_body (cgraph_node *node)
2691{
2692  symtab_node *node1 = node->next;
2693  for (; node1; node1 = node1->next)
2694    {
2695      cgraph_node *cn1 = dyn_cast <cgraph_node *> (node1);
2696      if (cn1 && cn1->has_gimple_body_p ())
2697	return cn1;
2698    }
2699  return NULL;
2700}
2701
2702/* Walk all functions.  */
2703#define FOR_EACH_FUNCTION(node) \
2704   for ((node) = symtab->first_function (); (node); \
2705	(node) = symtab->next_function ((node)))
2706
2707/* Return true when callgraph node is a function with Gimple body defined
2708   in current unit.  Functions can also be define externally or they
2709   can be thunks with no Gimple representation.
2710
2711   Note that at WPA stage, the function body may not be present in memory.  */
2712
2713inline bool
2714cgraph_node::has_gimple_body_p (void)
2715{
2716  return definition && !thunk.thunk_p && !alias;
2717}
2718
2719/* Walk all functions with body defined.  */
2720#define FOR_EACH_FUNCTION_WITH_GIMPLE_BODY(node) \
2721   for ((node) = symtab->first_function_with_gimple_body (); (node); \
2722	(node) = symtab->next_function_with_gimple_body (node))
2723
2724/* Uniquize all constants that appear in memory.
2725   Each constant in memory thus far output is recorded
2726   in `const_desc_table'.  */
2727
2728struct GTY((for_user)) constant_descriptor_tree {
2729  /* A MEM for the constant.  */
2730  rtx rtl;
2731
2732  /* The value of the constant.  */
2733  tree value;
2734
2735  /* Hash of value.  Computing the hash from value each time
2736     hashfn is called can't work properly, as that means recursive
2737     use of the hash table during hash table expansion.  */
2738  hashval_t hash;
2739};
2740
2741/* Return true when function is only called directly or it has alias.
2742   i.e. it is not externally visible, address was not taken and
2743   it is not used in any other non-standard way.  */
2744
2745inline bool
2746cgraph_node::only_called_directly_or_aliased_p (void)
2747{
2748  gcc_assert (!global.inlined_to);
2749  return (!force_output && !address_taken
2750	  && !used_from_other_partition
2751	  && !DECL_VIRTUAL_P (decl)
2752	  && !DECL_STATIC_CONSTRUCTOR (decl)
2753	  && !DECL_STATIC_DESTRUCTOR (decl)
2754	  && !used_from_object_file_p ()
2755	  && !externally_visible);
2756}
2757
2758/* Return true when function can be removed from callgraph
2759   if all direct calls are eliminated.  */
2760
2761inline bool
2762cgraph_node::can_remove_if_no_direct_calls_and_refs_p (void)
2763{
2764  gcc_checking_assert (!global.inlined_to);
2765  /* Instrumentation clones should not be removed before
2766     instrumentation happens.  New callers may appear after
2767     instrumentation.  */
2768  if (instrumentation_clone
2769      && !chkp_function_instrumented_p (decl))
2770    return false;
2771  /* Extern inlines can always go, we will use the external definition.  */
2772  if (DECL_EXTERNAL (decl))
2773    return true;
2774  /* When function is needed, we can not remove it.  */
2775  if (force_output || used_from_other_partition)
2776    return false;
2777  if (DECL_STATIC_CONSTRUCTOR (decl)
2778      || DECL_STATIC_DESTRUCTOR (decl))
2779    return false;
2780  /* Only COMDAT functions can be removed if externally visible.  */
2781  if (externally_visible
2782      && (!DECL_COMDAT (decl)
2783	  || forced_by_abi
2784	  || used_from_object_file_p ()))
2785    return false;
2786  return true;
2787}
2788
2789/* Return true when variable can be removed from variable pool
2790   if all direct calls are eliminated.  */
2791
2792inline bool
2793varpool_node::can_remove_if_no_refs_p (void)
2794{
2795  if (DECL_EXTERNAL (decl))
2796    return true;
2797  return (!force_output && !used_from_other_partition
2798	  && ((DECL_COMDAT (decl)
2799	       && !forced_by_abi
2800	       && !used_from_object_file_p ())
2801	      || !externally_visible
2802	      || DECL_HAS_VALUE_EXPR_P (decl)));
2803}
2804
2805/* Return true when all references to variable must be visible in ipa_ref_list.
2806   i.e. if the variable is not externally visible or not used in some magic
2807   way (asm statement or such).
2808   The magic uses are all summarized in force_output flag.  */
2809
2810inline bool
2811varpool_node::all_refs_explicit_p ()
2812{
2813  return (definition
2814	  && !externally_visible
2815	  && !used_from_other_partition
2816	  && !force_output);
2817}
2818
2819struct tree_descriptor_hasher : ggc_hasher<constant_descriptor_tree *>
2820{
2821  static hashval_t hash (constant_descriptor_tree *);
2822  static bool equal (constant_descriptor_tree *, constant_descriptor_tree *);
2823};
2824
2825/* Constant pool accessor function.  */
2826hash_table<tree_descriptor_hasher> *constant_pool_htab (void);
2827
2828/* Return node that alias is aliasing.  */
2829
2830inline cgraph_node *
2831cgraph_node::get_alias_target (void)
2832{
2833  return dyn_cast <cgraph_node *> (symtab_node::get_alias_target ());
2834}
2835
2836/* Return node that alias is aliasing.  */
2837
2838inline varpool_node *
2839varpool_node::get_alias_target (void)
2840{
2841  return dyn_cast <varpool_node *> (symtab_node::get_alias_target ());
2842}
2843
2844/* Walk the alias chain to return the symbol NODE is alias of.
2845   If NODE is not an alias, return NODE.
2846   When AVAILABILITY is non-NULL, get minimal availability in the chain.  */
2847
2848inline symtab_node *
2849symtab_node::ultimate_alias_target (enum availability *availability)
2850{
2851  if (!alias)
2852    {
2853      if (availability)
2854	*availability = get_availability ();
2855      return this;
2856    }
2857
2858  return ultimate_alias_target_1 (availability);
2859}
2860
2861/* Given function symbol, walk the alias chain to return the function node
2862   is alias of. Do not walk through thunks.
2863   When AVAILABILITY is non-NULL, get minimal availability in the chain.  */
2864
2865inline cgraph_node *
2866cgraph_node::ultimate_alias_target (enum availability *availability)
2867{
2868  cgraph_node *n = dyn_cast <cgraph_node *>
2869    (symtab_node::ultimate_alias_target (availability));
2870  if (!n && availability)
2871    *availability = AVAIL_NOT_AVAILABLE;
2872  return n;
2873}
2874
2875/* For given variable pool node, walk the alias chain to return the function
2876   the variable is alias of. Do not walk through thunks.
2877   When AVAILABILITY is non-NULL, get minimal availability in the chain.  */
2878
2879inline varpool_node *
2880varpool_node::ultimate_alias_target (availability *availability)
2881{
2882  varpool_node *n = dyn_cast <varpool_node *>
2883    (symtab_node::ultimate_alias_target (availability));
2884
2885  if (!n && availability)
2886    *availability = AVAIL_NOT_AVAILABLE;
2887  return n;
2888}
2889
2890/* Set callee N of call graph edge and add it to the corresponding set of
2891   callers. */
2892
2893inline void
2894cgraph_edge::set_callee (cgraph_node *n)
2895{
2896  prev_caller = NULL;
2897  if (n->callers)
2898    n->callers->prev_caller = this;
2899  next_caller = n->callers;
2900  n->callers = this;
2901  callee = n;
2902}
2903
2904/* Redirect callee of the edge to N.  The function does not update underlying
2905   call expression.  */
2906
2907inline void
2908cgraph_edge::redirect_callee (cgraph_node *n)
2909{
2910  /* Remove from callers list of the current callee.  */
2911  remove_callee ();
2912
2913  /* Insert to callers list of the new callee.  */
2914  set_callee (n);
2915}
2916
2917/* Return true when the edge represents a direct recursion.  */
2918
2919inline bool
2920cgraph_edge::recursive_p (void)
2921{
2922  cgraph_node *c = callee->ultimate_alias_target ();
2923  if (caller->global.inlined_to)
2924    return caller->global.inlined_to->decl == c->decl;
2925  else
2926    return caller->decl == c->decl;
2927}
2928
2929/* Remove the edge from the list of the callers of the callee.  */
2930
2931inline void
2932cgraph_edge::remove_callee (void)
2933{
2934  gcc_assert (!indirect_unknown_callee);
2935  if (prev_caller)
2936    prev_caller->next_caller = next_caller;
2937  if (next_caller)
2938    next_caller->prev_caller = prev_caller;
2939  if (!prev_caller)
2940    callee->callers = next_caller;
2941}
2942
2943/* Return true if the TM_CLONE bit is set for a given FNDECL.  */
2944static inline bool
2945decl_is_tm_clone (const_tree fndecl)
2946{
2947  cgraph_node *n = cgraph_node::get (fndecl);
2948  if (n)
2949    return n->tm_clone;
2950  return false;
2951}
2952
2953/* Likewise indicate that a node is needed, i.e. reachable via some
2954   external means.  */
2955
2956inline void
2957cgraph_node::mark_force_output (void)
2958{
2959  force_output = 1;
2960  gcc_checking_assert (!global.inlined_to);
2961}
2962
2963/* Return true if function should be optimized for size.  */
2964
2965inline bool
2966cgraph_node::optimize_for_size_p (void)
2967{
2968  if (opt_for_fn (decl, optimize_size))
2969    return true;
2970  if (frequency == NODE_FREQUENCY_UNLIKELY_EXECUTED)
2971    return true;
2972  else
2973    return false;
2974}
2975
2976/* Return symtab_node for NODE or create one if it is not present
2977   in symtab.  */
2978
2979inline symtab_node *
2980symtab_node::get_create (tree node)
2981{
2982  if (TREE_CODE (node) == VAR_DECL)
2983    return varpool_node::get_create (node);
2984  else
2985    return cgraph_node::get_create (node);
2986}
2987
2988/* Return availability of NODE.  */
2989
2990inline enum availability
2991symtab_node::get_availability (void)
2992{
2993  if (is_a <cgraph_node *> (this))
2994    return dyn_cast <cgraph_node *> (this)->get_availability ();
2995  else
2996    return dyn_cast <varpool_node *> (this)->get_availability ();;
2997}
2998
2999/* Call calback on symtab node and aliases associated to this node.
3000   When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
3001   skipped. */
3002
3003inline bool
3004symtab_node::call_for_symbol_and_aliases (bool (*callback) (symtab_node *,
3005							    void *),
3006					  void *data,
3007					  bool include_overwritable)
3008{
3009  if (callback (this, data))
3010    return true;
3011  if (has_aliases_p ())
3012    return call_for_symbol_and_aliases_1 (callback, data, include_overwritable);
3013  return false;
3014}
3015
3016/* Call callback on function and aliases associated to the function.
3017   When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
3018   skipped.  */
3019
3020inline bool
3021cgraph_node::call_for_symbol_and_aliases (bool (*callback) (cgraph_node *,
3022							    void *),
3023					  void *data,
3024					  bool include_overwritable)
3025{
3026  if (callback (this, data))
3027    return true;
3028  if (has_aliases_p ())
3029    return call_for_symbol_and_aliases_1 (callback, data, include_overwritable);
3030  return false;
3031}
3032
3033/* Call calback on varpool symbol and aliases associated to varpool symbol.
3034   When INCLUDE_OVERWRITABLE is false, overwritable aliases and thunks are
3035   skipped. */
3036
3037inline bool
3038varpool_node::call_for_symbol_and_aliases (bool (*callback) (varpool_node *,
3039							     void *),
3040					   void *data,
3041					   bool include_overwritable)
3042{
3043  if (callback (this, data))
3044    return true;
3045  if (has_aliases_p ())
3046    return call_for_symbol_and_aliases_1 (callback, data, include_overwritable);
3047  return false;
3048}
3049
3050/* Return true if NODE's address can be compared.  */
3051
3052inline bool
3053symtab_node::address_can_be_compared_p ()
3054{
3055  /* Address of virtual tables and functions is never compared.  */
3056  if (DECL_VIRTUAL_P (decl))
3057    return false;
3058  /* Address of C++ cdtors is never compared.  */
3059  if (is_a <cgraph_node *> (this)
3060      && (DECL_CXX_CONSTRUCTOR_P (decl)
3061	  || DECL_CXX_DESTRUCTOR_P (decl)))
3062    return false;
3063  /* Constant pool symbols addresses are never compared.
3064     flag_merge_constants permits us to assume the same on readonly vars.  */
3065  if (is_a <varpool_node *> (this)
3066      && (DECL_IN_CONSTANT_POOL (decl)
3067	  || (flag_merge_constants >= 2
3068	      && TREE_READONLY (decl) && !TREE_THIS_VOLATILE (decl))))
3069    return false;
3070  return true;
3071}
3072
3073/* Return true if refernece may be used in address compare.  */
3074
3075inline bool
3076ipa_ref::address_matters_p ()
3077{
3078  if (use != IPA_REF_ADDR)
3079    return false;
3080  /* Addresses taken from virtual tables are never compared.  */
3081  if (is_a <varpool_node *> (referring)
3082      && DECL_VIRTUAL_P (referring->decl))
3083    return false;
3084  return referred->address_can_be_compared_p ();
3085}
3086
3087/* Build polymorphic call context for indirect call E.  */
3088
3089inline
3090ipa_polymorphic_call_context::ipa_polymorphic_call_context (cgraph_edge *e)
3091{
3092  gcc_checking_assert (e->indirect_info->polymorphic);
3093  *this = e->indirect_info->context;
3094}
3095
3096/* Build empty "I know nothing" context.  */
3097
3098inline
3099ipa_polymorphic_call_context::ipa_polymorphic_call_context ()
3100{
3101  clear_speculation ();
3102  clear_outer_type ();
3103  invalid = false;
3104}
3105
3106/* Make context non-speculative.  */
3107
3108inline void
3109ipa_polymorphic_call_context::clear_speculation ()
3110{
3111  speculative_outer_type = NULL;
3112  speculative_offset = 0;
3113  speculative_maybe_derived_type = false;
3114}
3115
3116/* Produce context specifying all derrived types of OTR_TYPE.  If OTR_TYPE is
3117   NULL, the context is set to dummy "I know nothing" setting.  */
3118
3119inline void
3120ipa_polymorphic_call_context::clear_outer_type (tree otr_type)
3121{
3122  outer_type = otr_type ? TYPE_MAIN_VARIANT (otr_type) : NULL;
3123  offset = 0;
3124  maybe_derived_type = true;
3125  maybe_in_construction = true;
3126  dynamic = true;
3127}
3128
3129/* Adjust all offsets in contexts by OFF bits.  */
3130
3131inline void
3132ipa_polymorphic_call_context::offset_by (HOST_WIDE_INT off)
3133{
3134  if (outer_type)
3135    offset += off;
3136  if (speculative_outer_type)
3137    speculative_offset += off;
3138}
3139
3140/* Return TRUE if context is fully useless.  */
3141
3142inline bool
3143ipa_polymorphic_call_context::useless_p () const
3144{
3145  return (!outer_type && !speculative_outer_type);
3146}
3147
3148/* Return true if NODE is local.  Instrumentation clones are counted as local
3149   only when original function is local.  */
3150
3151static inline bool
3152cgraph_local_p (cgraph_node *node)
3153{
3154  if (!node->instrumentation_clone || !node->instrumented_version)
3155    return node->local.local;
3156
3157  return node->local.local && node->instrumented_version->local.local;
3158}
3159
3160/* When using fprintf (or similar), problems can arise with
3161   transient generated strings.  Many string-generation APIs
3162   only support one result being alive at once (e.g. by
3163   returning a pointer to a statically-allocated buffer).
3164
3165   If there is more than one generated string within one
3166   fprintf call: the first string gets evicted or overwritten
3167   by the second, before fprintf is fully evaluated.
3168   See e.g. PR/53136.
3169
3170   This function provides a workaround for this, by providing
3171   a simple way to create copies of these transient strings,
3172   without the need to have explicit cleanup:
3173
3174       fprintf (dumpfile, "string 1: %s string 2:%s\n",
3175                xstrdup_for_dump (EXPR_1),
3176                xstrdup_for_dump (EXPR_2));
3177
3178   This is actually a simple wrapper around ggc_strdup, but
3179   the name documents the intent.  We require that no GC can occur
3180   within the fprintf call.  */
3181
3182static inline const char *
3183xstrdup_for_dump (const char *transient_str)
3184{
3185  return ggc_strdup (transient_str);
3186}
3187
3188#endif  /* GCC_CGRAPH_H  */
3189