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#include "config.h"
22#include "system.h"
23#include "coretypes.h"
24#include "tm.h"
25#include "hash-set.h"
26#include "machmode.h"
27#include "vec.h"
28#include "double-int.h"
29#include "input.h"
30#include "alias.h"
31#include "symtab.h"
32#include "wide-int.h"
33#include "inchash.h"
34#include "tree.h"
35#include "fold-const.h"
36#include "varasm.h"
37#include "predict.h"
38#include "basic-block.h"
39#include "hash-map.h"
40#include "is-a.h"
41#include "plugin-api.h"
42#include "hard-reg-set.h"
43#include "input.h"
44#include "function.h"
45#include "ipa-ref.h"
46#include "cgraph.h"
47#include "langhooks.h"
48#include "diagnostic-core.h"
49#include "timevar.h"
50#include "debug.h"
51#include "target.h"
52#include "output.h"
53#include "gimple-expr.h"
54#include "flags.h"
55#include "tree-ssa-alias.h"
56#include "gimple.h"
57#include "lto-streamer.h"
58#include "context.h"
59#include "omp-low.h"
60
61const char * const tls_model_names[]={"none", "emulated",
62				      "global-dynamic", "local-dynamic",
63				      "initial-exec", "local-exec"};
64
65/* List of hooks triggered on varpool_node events.  */
66struct varpool_node_hook_list {
67  varpool_node_hook hook;
68  void *data;
69  struct varpool_node_hook_list *next;
70};
71
72/* Register HOOK to be called with DATA on each removed node.  */
73varpool_node_hook_list *
74symbol_table::add_varpool_removal_hook (varpool_node_hook hook, void *data)
75{
76  varpool_node_hook_list *entry;
77  varpool_node_hook_list **ptr = &m_first_varpool_removal_hook;
78
79  entry = (varpool_node_hook_list *) xmalloc (sizeof (*entry));
80  entry->hook = hook;
81  entry->data = data;
82  entry->next = NULL;
83  while (*ptr)
84    ptr = &(*ptr)->next;
85  *ptr = entry;
86  return entry;
87}
88
89/* Remove ENTRY from the list of hooks called on removing nodes.  */
90void
91symbol_table::remove_varpool_removal_hook (varpool_node_hook_list *entry)
92{
93  varpool_node_hook_list **ptr = &m_first_varpool_removal_hook;
94
95  while (*ptr != entry)
96    ptr = &(*ptr)->next;
97  *ptr = entry->next;
98  free (entry);
99}
100
101/* Call all node removal hooks.  */
102void
103symbol_table::call_varpool_removal_hooks (varpool_node *node)
104{
105  varpool_node_hook_list *entry = m_first_varpool_removal_hook;
106  while (entry)
107  {
108    entry->hook (node, entry->data);
109    entry = entry->next;
110  }
111}
112
113/* Register HOOK to be called with DATA on each inserted node.  */
114varpool_node_hook_list *
115symbol_table::add_varpool_insertion_hook (varpool_node_hook hook, void *data)
116{
117  varpool_node_hook_list *entry;
118  varpool_node_hook_list **ptr = &m_first_varpool_insertion_hook;
119
120  entry = (varpool_node_hook_list *) xmalloc (sizeof (*entry));
121  entry->hook = hook;
122  entry->data = data;
123  entry->next = NULL;
124  while (*ptr)
125    ptr = &(*ptr)->next;
126  *ptr = entry;
127  return entry;
128}
129
130/* Remove ENTRY from the list of hooks called on inserted nodes.  */
131void
132symbol_table::remove_varpool_insertion_hook (varpool_node_hook_list *entry)
133{
134  varpool_node_hook_list **ptr = &m_first_varpool_insertion_hook;
135
136  while (*ptr != entry)
137    ptr = &(*ptr)->next;
138  *ptr = entry->next;
139  free (entry);
140}
141
142/* Call all node insertion hooks.  */
143void
144symbol_table::call_varpool_insertion_hooks (varpool_node *node)
145{
146  varpool_node_hook_list *entry = m_first_varpool_insertion_hook;
147  while (entry)
148  {
149    entry->hook (node, entry->data);
150    entry = entry->next;
151  }
152}
153
154/* Allocate new callgraph node and insert it into basic data structures.  */
155
156varpool_node *
157varpool_node::create_empty (void)
158{
159  varpool_node *node = ggc_cleared_alloc<varpool_node> ();
160  node->type = SYMTAB_VARIABLE;
161  return node;
162}
163
164/* Return varpool node assigned to DECL.  Create new one when needed.  */
165varpool_node *
166varpool_node::get_create (tree decl)
167{
168  varpool_node *node = varpool_node::get (decl);
169  gcc_checking_assert (TREE_CODE (decl) == VAR_DECL);
170  if (node)
171    return node;
172
173  node = varpool_node::create_empty ();
174  node->decl = decl;
175
176  if ((flag_openacc || flag_openmp) && !DECL_EXTERNAL (decl)
177      && lookup_attribute ("omp declare target", DECL_ATTRIBUTES (decl)))
178    {
179      node->offloadable = 1;
180#ifdef ENABLE_OFFLOADING
181      g->have_offload = true;
182      if (!in_lto_p)
183	vec_safe_push (offload_vars, decl);
184      node->force_output = 1;
185#endif
186    }
187
188  node->register_symbol ();
189  return node;
190}
191
192/* Remove variable from symbol table.  */
193
194void
195varpool_node::remove (void)
196{
197  symtab->call_varpool_removal_hooks (this);
198  if (lto_file_data)
199    {
200      lto_free_function_in_decl_state_for_node (this);
201      lto_file_data = NULL;
202    }
203
204  /* When streaming we can have multiple nodes associated with decl.  */
205  if (symtab->state == LTO_STREAMING)
206    ;
207  /* Keep constructor when it may be used for folding. We remove
208     references to external variables before final compilation.  */
209  else if (DECL_INITIAL (decl) && DECL_INITIAL (decl) != error_mark_node
210	   && !ctor_useable_for_folding_p ())
211    remove_initializer ();
212
213  unregister ();
214  ggc_free (this);
215}
216
217/* Remove node initializer when it is no longer needed.  */
218void
219varpool_node::remove_initializer (void)
220{
221  if (DECL_INITIAL (decl)
222      && !DECL_IN_CONSTANT_POOL (decl)
223      /* Keep vtables for BINFO folding.  */
224      && !DECL_VIRTUAL_P (decl)
225      /* FIXME: http://gcc.gnu.org/PR55395 */
226      && debug_info_level == DINFO_LEVEL_NONE
227      /* When doing declaration merging we have duplicate
228	 entries for given decl.  Do not attempt to remove
229	 the boides, or we will end up remiving
230	 wrong one.  */
231      && symtab->state != LTO_STREAMING)
232    DECL_INITIAL (decl) = error_mark_node;
233}
234
235/* Dump given varpool node to F.  */
236void
237varpool_node::dump (FILE *f)
238{
239  dump_base (f);
240  fprintf (f, "  Availability: %s\n",
241	   symtab->function_flags_ready
242	   ? cgraph_availability_names[get_availability ()]
243	   : "not-ready");
244  fprintf (f, "  Varpool flags:");
245  if (DECL_INITIAL (decl))
246    fprintf (f, " initialized");
247  if (output)
248    fprintf (f, " output");
249  if (used_by_single_function)
250    fprintf (f, " used-by-single-function");
251  if (need_bounds_init)
252    fprintf (f, " need-bounds-init");
253  if (TREE_READONLY (decl))
254    fprintf (f, " read-only");
255  if (ctor_useable_for_folding_p ())
256    fprintf (f, " const-value-known");
257  if (writeonly)
258    fprintf (f, " write-only");
259  if (tls_model)
260    fprintf (f, " tls-%s", tls_model_names [tls_model]);
261  fprintf (f, "\n");
262}
263
264
265/* Dump given varpool node to stderr.  */
266void varpool_node::debug (void)
267{
268  varpool_node::dump (stderr);
269}
270
271/* Dump the variable pool to F.  */
272void
273varpool_node::dump_varpool (FILE *f)
274{
275  varpool_node *node;
276
277  fprintf (f, "variable pool:\n\n");
278  FOR_EACH_VARIABLE (node)
279    node->dump (f);
280}
281
282/* Dump the variable pool to stderr.  */
283
284DEBUG_FUNCTION void
285varpool_node::debug_varpool (void)
286{
287  dump_varpool (stderr);
288}
289
290/* Given an assembler name, lookup node.  */
291varpool_node *
292varpool_node::get_for_asmname (tree asmname)
293{
294  if (symtab_node *node = symtab_node::get_for_asmname (asmname))
295    return dyn_cast <varpool_node *> (node);
296  else
297    return NULL;
298}
299
300/* When doing LTO, read variable's constructor from disk if
301   it is not already present.  */
302
303tree
304varpool_node::get_constructor (void)
305{
306  lto_file_decl_data *file_data;
307  const char *data, *name;
308  size_t len;
309
310  if (DECL_INITIAL (decl) != error_mark_node
311      || !in_lto_p
312      || !lto_file_data)
313    return DECL_INITIAL (decl);
314
315  timevar_push (TV_IPA_LTO_CTORS_IN);
316
317  file_data = lto_file_data;
318  name = IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
319
320  /* We may have renamed the declaration, e.g., a static function.  */
321  name = lto_get_decl_name_mapping (file_data, name);
322
323  data = lto_get_section_data (file_data, LTO_section_function_body,
324			       name, &len);
325  if (!data)
326    fatal_error (input_location, "%s: section %s is missing",
327		 file_data->file_name,
328		 name);
329
330  lto_input_variable_constructor (file_data, this, data);
331  gcc_assert (DECL_INITIAL (decl) != error_mark_node);
332  lto_stats.num_function_bodies++;
333  lto_free_section_data (file_data, LTO_section_function_body, name,
334			 data, len);
335  lto_free_function_in_decl_state_for_node (this);
336  timevar_pop (TV_IPA_LTO_CTORS_IN);
337  return DECL_INITIAL (decl);
338}
339
340/* Return true if variable has constructor that can be used for folding.  */
341
342bool
343varpool_node::ctor_useable_for_folding_p (void)
344{
345  varpool_node *real_node = this;
346
347  if (real_node->alias && real_node->definition)
348    real_node = ultimate_alias_target ();
349
350  if (TREE_CODE (decl) == CONST_DECL
351      || DECL_IN_CONSTANT_POOL (decl))
352    return true;
353  if (TREE_THIS_VOLATILE (decl))
354    return false;
355
356  /* If we do not have a constructor, we can't use it.  */
357  if (DECL_INITIAL (real_node->decl) == error_mark_node
358      && !real_node->lto_file_data)
359    return false;
360
361  /* Avoid attempts to load constructors that was not streamed.  */
362  if (flag_ltrans && DECL_INITIAL (real_node->decl) == error_mark_node
363      && real_node->body_removed)
364    return false;
365
366  /* Vtables are defined by their types and must match no matter of interposition
367     rules.  */
368  if (DECL_VIRTUAL_P (decl))
369    {
370      /* The C++ front end creates VAR_DECLs for vtables of typeinfo
371	 classes not defined in the current TU so that it can refer
372	 to them from typeinfo objects.  Avoid returning NULL_TREE.  */
373      return DECL_INITIAL (real_node->decl) != NULL;
374    }
375
376  /* Alias of readonly variable is also readonly, since the variable is stored
377     in readonly memory.  We also accept readonly aliases of non-readonly
378     locations assuming that user knows what he is asking for.  */
379  if (!TREE_READONLY (decl) && !TREE_READONLY (real_node->decl))
380    return false;
381
382  /* Variables declared 'const' without an initializer
383     have zero as the initializer if they may not be
384     overridden at link or run time.
385
386     It is actually requirement for C++ compiler to optimize const variables
387     consistently. As a GNU extension, do not enfore this rule for user defined
388     weak variables, so we support interposition on:
389     static const int dummy = 0;
390     extern const int foo __attribute__((__weak__, __alias__("dummy")));
391   */
392  if ((!DECL_INITIAL (real_node->decl)
393       || (DECL_WEAK (decl) && !DECL_COMDAT (decl)))
394      && (DECL_EXTERNAL (decl) || decl_replaceable_p (decl)))
395    return false;
396
397  /* Variables declared `const' with an initializer are considered
398     to not be overwritable with different initializer by default.
399
400     ??? Previously we behaved so for scalar variables but not for array
401     accesses.  */
402  return true;
403}
404
405/* If DECLARATION is constant variable and its initial value is known
406   (so we can do constant folding), return its constructor (DECL_INITIAL).
407   This may be an expression or NULL when DECL is initialized to 0.
408   Return ERROR_MARK_NODE otherwise.
409
410   In LTO this may actually trigger reading the constructor from disk.
411   For this reason varpool_ctor_useable_for_folding_p should be used when
412   the actual constructor value is not needed.  */
413
414tree
415ctor_for_folding (tree decl)
416{
417  varpool_node *node, *real_node;
418  tree real_decl;
419
420  if (TREE_CODE (decl) != VAR_DECL
421      && TREE_CODE (decl) != CONST_DECL)
422    return error_mark_node;
423
424  /* Static constant bounds are created to be
425     used instead of constants and therefore
426     do not let folding it.  */
427  if (POINTER_BOUNDS_P (decl))
428    return error_mark_node;
429
430  if (TREE_CODE (decl) == CONST_DECL
431      || DECL_IN_CONSTANT_POOL (decl))
432    return DECL_INITIAL (decl);
433
434  if (TREE_THIS_VOLATILE (decl))
435    return error_mark_node;
436
437  /* Do not care about automatic variables.  Those are never initialized
438     anyway, because gimplifier exapnds the code.  */
439  if (!TREE_STATIC (decl) && !DECL_EXTERNAL (decl))
440    {
441      gcc_assert (!TREE_PUBLIC (decl));
442      return error_mark_node;
443    }
444
445  gcc_assert (TREE_CODE (decl) == VAR_DECL);
446
447  real_node = node = varpool_node::get (decl);
448  if (node)
449    {
450      real_node = node->ultimate_alias_target ();
451      real_decl = real_node->decl;
452    }
453  else
454    real_decl = decl;
455
456  /* See if we are dealing with alias.
457     In most cases alias is just alternative symbol pointing to a given
458     constructor.  This allows us to use interposition rules of DECL
459     constructor of REAL_NODE.  However weakrefs are special by being just
460     alternative name of their target (if defined).  */
461  if (decl != real_decl)
462    {
463      gcc_assert (!DECL_INITIAL (decl)
464		  || (node->alias && node->get_alias_target () == real_node)
465		  || DECL_INITIAL (decl) == error_mark_node);
466      if (node->weakref)
467	{
468	  node = node->get_alias_target ();
469	  decl = node->decl;
470	}
471    }
472
473  if ((!DECL_VIRTUAL_P (real_decl)
474       || DECL_INITIAL (real_decl) == error_mark_node
475       || !DECL_INITIAL (real_decl))
476      && (!node || !node->ctor_useable_for_folding_p ()))
477    return error_mark_node;
478
479  /* OK, we can return constructor.  See if we need to fetch it from disk
480     in LTO mode.  */
481  if (DECL_INITIAL (real_decl) != error_mark_node
482      || !in_lto_p)
483    return DECL_INITIAL (real_decl);
484  return real_node->get_constructor ();
485}
486
487/* Add the variable DECL to the varpool.
488   Unlike finalize_decl function is intended to be used
489   by middle end and allows insertion of new variable at arbitrary point
490   of compilation.  */
491void
492varpool_node::add (tree decl)
493{
494  varpool_node *node;
495  varpool_node::finalize_decl (decl);
496  node = varpool_node::get_create (decl);
497  symtab->call_varpool_insertion_hooks (node);
498  if (node->externally_visible_p ())
499    node->externally_visible = true;
500  if (lookup_attribute ("no_reorder", DECL_ATTRIBUTES (decl)))
501    node->no_reorder = 1;
502}
503
504/* Return variable availability.  See cgraph.h for description of individual
505   return values.  */
506enum availability
507varpool_node::get_availability (void)
508{
509  if (!definition)
510    return AVAIL_NOT_AVAILABLE;
511  if (!TREE_PUBLIC (decl))
512    return AVAIL_AVAILABLE;
513  if (DECL_IN_CONSTANT_POOL (decl)
514      || DECL_VIRTUAL_P (decl))
515    return AVAIL_AVAILABLE;
516  if (alias && weakref)
517    {
518      enum availability avail;
519
520      ultimate_alias_target (&avail)->get_availability ();
521      return avail;
522    }
523  /* If the variable can be overwritten, return OVERWRITABLE.  Takes
524     care of at least one notable extension - the COMDAT variables
525     used to share template instantiations in C++.  */
526  if (decl_replaceable_p (decl)
527      || DECL_EXTERNAL (decl))
528    return AVAIL_INTERPOSABLE;
529  return AVAIL_AVAILABLE;
530}
531
532void
533varpool_node::analyze (void)
534{
535  /* When reading back varpool at LTO time, we re-construct the queue in order
536     to have "needed" list right by inserting all needed nodes into varpool.
537     We however don't want to re-analyze already analyzed nodes.  */
538  if (!analyzed)
539    {
540      gcc_assert (!in_lto_p || symtab->function_flags_ready);
541      /* Compute the alignment early so function body expanders are
542	 already informed about increased alignment.  */
543      align_variable (decl, 0);
544    }
545  if (alias)
546    resolve_alias (varpool_node::get (alias_target));
547  else if (DECL_INITIAL (decl))
548    record_references_in_initializer (decl, analyzed);
549  analyzed = true;
550}
551
552/* Assemble thunks and aliases associated to varpool node.  */
553
554void
555varpool_node::assemble_aliases (void)
556{
557  ipa_ref *ref;
558
559  FOR_EACH_ALIAS (this, ref)
560    {
561      varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
562      do_assemble_alias (alias->decl,
563			 DECL_ASSEMBLER_NAME (decl));
564      alias->assemble_aliases ();
565    }
566}
567
568/* Output one variable, if necessary.  Return whether we output it.  */
569
570bool
571varpool_node::assemble_decl (void)
572{
573  /* Aliases are outout when their target is produced or by
574     output_weakrefs.  */
575  if (alias)
576    return false;
577
578  /* Constant pool is output from RTL land when the reference
579     survive till this level.  */
580  if (DECL_IN_CONSTANT_POOL (decl) && TREE_ASM_WRITTEN (decl))
581    return false;
582
583  /* Decls with VALUE_EXPR should not be in the varpool at all.  They
584     are not real variables, but just info for debugging and codegen.
585     Unfortunately at the moment emutls is not updating varpool correctly
586     after turning real vars into value_expr vars.  */
587  if (DECL_HAS_VALUE_EXPR_P (decl)
588      && !targetm.have_tls)
589    return false;
590
591  /* Hard register vars do not need to be output.  */
592  if (DECL_HARD_REGISTER (decl))
593    return false;
594
595  gcc_checking_assert (!TREE_ASM_WRITTEN (decl)
596		       && TREE_CODE (decl) == VAR_DECL
597		       && !DECL_HAS_VALUE_EXPR_P (decl));
598
599  if (!in_other_partition
600      && !DECL_EXTERNAL (decl))
601    {
602      get_constructor ();
603      assemble_variable (decl, 0, 1, 0);
604      gcc_assert (TREE_ASM_WRITTEN (decl));
605      gcc_assert (definition);
606      assemble_aliases ();
607      return true;
608    }
609
610  return false;
611}
612
613/* Add NODE to queue starting at FIRST.
614   The queue is linked via AUX pointers and terminated by pointer to 1.  */
615
616static void
617enqueue_node (varpool_node *node, varpool_node **first)
618{
619  if (node->aux)
620    return;
621  gcc_checking_assert (*first);
622  node->aux = *first;
623  *first = node;
624}
625
626/* Optimization of function bodies might've rendered some variables as
627   unnecessary so we want to avoid these from being compiled.  Re-do
628   reachability starting from variables that are either externally visible
629   or was referred from the asm output routines.  */
630
631void
632symbol_table::remove_unreferenced_decls (void)
633{
634  varpool_node *next, *node;
635  varpool_node *first = (varpool_node *)(void *)1;
636  int i;
637  ipa_ref *ref = NULL;
638  hash_set<varpool_node *> referenced;
639
640  if (seen_error ())
641    return;
642
643  if (dump_file)
644    fprintf (dump_file, "Trivially needed variables:");
645  FOR_EACH_DEFINED_VARIABLE (node)
646    {
647      if (node->analyzed
648	  && (!node->can_remove_if_no_refs_p ()
649	      /* We just expanded all function bodies.  See if any of
650		 them needed the variable.  */
651	      || DECL_RTL_SET_P (node->decl)))
652	{
653	  enqueue_node (node, &first);
654	  if (dump_file)
655	    fprintf (dump_file, " %s", node->asm_name ());
656	}
657    }
658  while (first != (varpool_node *)(void *)1)
659    {
660      node = first;
661      first = (varpool_node *)first->aux;
662
663      if (node->same_comdat_group)
664	{
665	  symtab_node *next;
666	  for (next = node->same_comdat_group;
667	       next != node;
668	       next = next->same_comdat_group)
669	    {
670	      varpool_node *vnext = dyn_cast <varpool_node *> (next);
671	      if (vnext && vnext->analyzed && !next->comdat_local_p ())
672		enqueue_node (vnext, &first);
673	    }
674	}
675      for (i = 0; node->iterate_reference (i, ref); i++)
676	{
677	  varpool_node *vnode = dyn_cast <varpool_node *> (ref->referred);
678	  if (vnode
679	      && !vnode->in_other_partition
680	      && (!DECL_EXTERNAL (ref->referred->decl)
681		  || vnode->alias)
682	      && vnode->analyzed)
683	    enqueue_node (vnode, &first);
684	  else
685	    referenced.add (node);
686	}
687    }
688  if (dump_file)
689    fprintf (dump_file, "\nRemoving variables:");
690  for (node = first_defined_variable (); node; node = next)
691    {
692      next = next_defined_variable (node);
693      if (!node->aux && !node->no_reorder)
694	{
695	  if (dump_file)
696	    fprintf (dump_file, " %s", node->asm_name ());
697	  if (referenced.contains(node))
698	    node->remove_initializer ();
699	  else
700	    node->remove ();
701	}
702    }
703
704  if (dump_file)
705    fprintf (dump_file, "\n");
706}
707
708/* For variables in named sections make sure get_variable_section
709   is called before we switch to those sections.  Then section
710   conflicts between read-only and read-only requiring relocations
711   sections can be resolved.  */
712void
713varpool_node::finalize_named_section_flags (void)
714{
715  if (!TREE_ASM_WRITTEN (decl)
716      && !alias
717      && !in_other_partition
718      && !DECL_EXTERNAL (decl)
719      && TREE_CODE (decl) == VAR_DECL
720      && !DECL_HAS_VALUE_EXPR_P (decl)
721      && get_section ())
722    get_variable_section (decl, false);
723}
724
725/* Output all variables enqueued to be assembled.  */
726bool
727symbol_table::output_variables (void)
728{
729  bool changed = false;
730  varpool_node *node;
731
732  if (seen_error ())
733    return false;
734
735  remove_unreferenced_decls ();
736
737  timevar_push (TV_VAROUT);
738
739  FOR_EACH_VARIABLE (node)
740    if (!node->definition
741	&& !DECL_HAS_VALUE_EXPR_P (node->decl)
742 	&& !DECL_HARD_REGISTER (node->decl))
743      assemble_undefined_decl (node->decl);
744  FOR_EACH_DEFINED_VARIABLE (node)
745    {
746      /* Handled in output_in_order.  */
747      if (node->no_reorder)
748	continue;
749
750      node->finalize_named_section_flags ();
751    }
752
753  FOR_EACH_DEFINED_VARIABLE (node)
754    {
755      /* Handled in output_in_order.  */
756      if (node->no_reorder)
757	continue;
758      if (node->assemble_decl ())
759        changed = true;
760    }
761  timevar_pop (TV_VAROUT);
762  return changed;
763}
764
765/* Attempt to mark ALIAS as an alias to DECL.  Return TRUE if successful.
766   Extra name aliases are output whenever DECL is output.  */
767
768varpool_node *
769varpool_node::create_alias (tree alias, tree decl)
770{
771  varpool_node *alias_node;
772
773  gcc_assert (TREE_CODE (decl) == VAR_DECL);
774  gcc_assert (TREE_CODE (alias) == VAR_DECL);
775  alias_node = varpool_node::get_create (alias);
776  alias_node->alias = true;
777  alias_node->definition = true;
778  alias_node->alias_target = decl;
779  if (lookup_attribute ("weakref", DECL_ATTRIBUTES (alias)) != NULL)
780    alias_node->weakref = true;
781  return alias_node;
782}
783
784/* Attempt to mark ALIAS as an alias to DECL.  Return TRUE if successful.
785   Extra name aliases are output whenever DECL is output.  */
786
787varpool_node *
788varpool_node::create_extra_name_alias (tree alias, tree decl)
789{
790  varpool_node *alias_node;
791
792#ifndef ASM_OUTPUT_DEF
793  /* If aliases aren't supported by the assembler, fail.  */
794  return NULL;
795#endif
796  alias_node = varpool_node::create_alias (alias, decl);
797  alias_node->cpp_implicit_alias = true;
798
799  /* Extra name alias mechanizm creates aliases really late
800     via DECL_ASSEMBLER_NAME mechanizm.
801     This is unfortunate because they are not going through the
802     standard channels.  Ensure they get output.  */
803  if (symtab->cpp_implicit_aliases_done)
804    alias_node->resolve_alias (varpool_node::get_create (decl));
805  return alias_node;
806}
807
808/* Worker for call_for_symbol_and_aliases.  */
809
810bool
811varpool_node::call_for_symbol_and_aliases_1 (bool (*callback) (varpool_node *,
812							       void *),
813					     void *data,
814					     bool include_overwritable)
815{
816  ipa_ref *ref;
817
818  FOR_EACH_ALIAS (this, ref)
819    {
820      varpool_node *alias = dyn_cast <varpool_node *> (ref->referring);
821      if (include_overwritable
822	  || alias->get_availability () > AVAIL_INTERPOSABLE)
823	if (alias->call_for_symbol_and_aliases (callback, data,
824					        include_overwritable))
825	  return true;
826    }
827  return false;
828}
829