symtab.c 54.2 KB
Newer Older
1
/* Symbol table.
Jakub Jelinek committed
2
   Copyright (C) 2012-2015 Free Software Foundation, Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
   Contributed by Jan Hubicka

This file is part of GCC.

GCC is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License as published by the Free
Software Foundation; either version 3, or (at your option) any later
version.

GCC is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */

#include "config.h"
#include "system.h"
#include "coretypes.h"
#include "tm.h"
25
#include "rtl.h"
26 27 28 29 30 31 32 33 34
#include "hash-set.h"
#include "machmode.h"
#include "vec.h"
#include "double-int.h"
#include "input.h"
#include "alias.h"
#include "symtab.h"
#include "wide-int.h"
#include "inchash.h"
35
#include "tree.h"
36
#include "fold-const.h"
37 38
#include "print-tree.h"
#include "varasm.h"
39 40 41
#include "hashtab.h"
#include "hard-reg-set.h"
#include "input.h"
42 43
#include "function.h"
#include "emit-rtl.h"
44
#include "predict.h"
45 46 47 48 49
#include "basic-block.h"
#include "tree-ssa-alias.h"
#include "internal-fn.h"
#include "gimple-expr.h"
#include "is-a.h"
50
#include "gimple.h"
51
#include "tree-inline.h"
52
#include "langhooks.h"
Andrew MacLeod committed
53 54 55
#include "hash-map.h"
#include "plugin-api.h"
#include "ipa-ref.h"
56
#include "cgraph.h"
57
#include "diagnostic.h"
58
#include "timevar.h"
59
#include "lto-streamer.h"
60
#include "output.h"
Martin Liska committed
61
#include "ipa-utils.h"
Jan Hubicka committed
62
#include "calls.h"
Martin Liska committed
63

Ilya Enkovich committed
64
static const char *ipa_ref_use_name[] = {"read","write","addr","alias","chkp"};
65 66 67 68 69 70 71 72 73 74 75 76 77 78

const char * const ld_plugin_symbol_resolution_names[]=
{
  "",
  "undef",
  "prevailing_def",
  "prevailing_def_ironly",
  "preempted_reg",
  "preempted_ir",
  "resolved_ir",
  "resolved_exec",
  "resolved_dyn",
  "prevailing_def_ironly_exp"
};
79

80 81
/* Hash asmnames ignoring the user specified marks.  */

Martin Liska committed
82 83
hashval_t
symbol_table::decl_assembler_name_hash (const_tree asmname)
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
{
  if (IDENTIFIER_POINTER (asmname)[0] == '*')
    {
      const char *decl_str = IDENTIFIER_POINTER (asmname) + 1;
      size_t ulp_len = strlen (user_label_prefix);

      if (ulp_len == 0)
	;
      else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
	decl_str += ulp_len;

      return htab_hash_string (decl_str);
    }

  return htab_hash_string (IDENTIFIER_POINTER (asmname));
}


/* Compare ASMNAME with the DECL_ASSEMBLER_NAME of DECL.  */

Martin Liska committed
104 105
bool
symbol_table::decl_assembler_name_equal (tree decl, const_tree asmname)
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
{
  tree decl_asmname = DECL_ASSEMBLER_NAME (decl);
  const char *decl_str;
  const char *asmname_str;
  bool test = false;

  if (decl_asmname == asmname)
    return true;

  decl_str = IDENTIFIER_POINTER (decl_asmname);
  asmname_str = IDENTIFIER_POINTER (asmname);


  /* If the target assembler name was set by the user, things are trickier.
     We have a leading '*' to begin with.  After that, it's arguable what
     is the correct thing to do with -fleading-underscore.  Arguably, we've
     historically been doing the wrong thing in assemble_alias by always
     printing the leading underscore.  Since we're not changing that, make
     sure user_label_prefix follows the '*' before matching.  */
  if (decl_str[0] == '*')
    {
      size_t ulp_len = strlen (user_label_prefix);

      decl_str ++;

      if (ulp_len == 0)
	test = true;
      else if (strncmp (decl_str, user_label_prefix, ulp_len) == 0)
	decl_str += ulp_len, test=true;
      else
	decl_str --;
    }
  if (asmname_str[0] == '*')
    {
      size_t ulp_len = strlen (user_label_prefix);

      asmname_str ++;

      if (ulp_len == 0)
	test = true;
      else if (strncmp (asmname_str, user_label_prefix, ulp_len) == 0)
	asmname_str += ulp_len, test=true;
      else
	asmname_str --;
    }

  if (!test)
    return false;
  return strcmp (decl_str, asmname_str) == 0;
}


158 159 160 161
/* Returns nonzero if P1 and P2 are equal.  */

/* Insert NODE to assembler name hash.  */

Martin Liska committed
162 163 164
void
symbol_table::insert_to_assembler_name_hash (symtab_node *node,
					     bool with_clones)
165
{
166
  if (is_a <varpool_node *> (node) && DECL_HARD_REGISTER (node->decl))
167
    return;
168 169
  gcc_checking_assert (!node->previous_sharing_asm_name
		       && !node->next_sharing_asm_name);
170 171
  if (assembler_name_hash)
    {
172
      symtab_node **aslot;
Martin Liska committed
173
      cgraph_node *cnode;
174
      tree decl = node->decl;
175

176
      tree name = DECL_ASSEMBLER_NAME (node->decl);
177

178 179 180 181 182
      /* C++ FE can produce decls without associated assembler name and insert
	 them to symtab to hold section or TLS information.  */
      if (!name)
	return;

183 184
      hashval_t hash = decl_assembler_name_hash (name);
      aslot = assembler_name_hash->find_slot_with_hash (name, hash, INSERT);
185
      gcc_assert (*aslot != node);
186
      node->next_sharing_asm_name = (symtab_node *)*aslot;
187
      if (*aslot != NULL)
188
	(*aslot)->previous_sharing_asm_name = node;
189
      *aslot = node;
190 191

      /* Update also possible inline clones sharing a decl.  */
192
      cnode = dyn_cast <cgraph_node *> (node);
193 194
      if (cnode && cnode->clones && with_clones)
	for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
195 196
	  if (cnode->decl == decl)
	    insert_to_assembler_name_hash (cnode, true);
197 198 199 200 201 202
    }

}

/* Remove NODE from assembler name hash.  */

Martin Liska committed
203 204 205
void
symbol_table::unlink_from_assembler_name_hash (symtab_node *node,
					       bool with_clones)
206 207 208
{
  if (assembler_name_hash)
    {
Martin Liska committed
209
      cgraph_node *cnode;
210
      tree decl = node->decl;
211

212 213 214 215
      if (node->next_sharing_asm_name)
	node->next_sharing_asm_name->previous_sharing_asm_name
	  = node->previous_sharing_asm_name;
      if (node->previous_sharing_asm_name)
216
	{
217 218
	  node->previous_sharing_asm_name->next_sharing_asm_name
	    = node->next_sharing_asm_name;
219 220 221
	}
      else
	{
222
	  tree name = DECL_ASSEMBLER_NAME (node->decl);
223
          symtab_node **slot;
224 225 226 227

	  if (!name)
	    return;

228 229 230
	  hashval_t hash = decl_assembler_name_hash (name);
	  slot = assembler_name_hash->find_slot_with_hash (name, hash,
							   NO_INSERT);
231
	  gcc_assert (*slot == node);
232
	  if (!node->next_sharing_asm_name)
233
	    assembler_name_hash->clear_slot (slot);
234
	  else
235
	    *slot = node->next_sharing_asm_name;
236
	}
237 238
      node->next_sharing_asm_name = NULL;
      node->previous_sharing_asm_name = NULL;
239 240

      /* Update also possible inline clones sharing a decl.  */
241
      cnode = dyn_cast <cgraph_node *> (node);
242 243
      if (cnode && cnode->clones && with_clones)
	for (cnode = cnode->clones; cnode; cnode = cnode->next_sibling_clone)
244 245
	  if (cnode->decl == decl)
	    unlink_from_assembler_name_hash (cnode, true);
246 247 248
    }
}

249 250 251
/* Arrange node to be first in its entry of assembler_name_hash.  */

void
Martin Liska committed
252
symbol_table::symtab_prevail_in_asm_name_hash (symtab_node *node)
253
{
254 255
  unlink_from_assembler_name_hash (node, false);
  insert_to_assembler_name_hash (node, false);
256 257 258
}

/* Initalize asm name hash unless.  */
259

260
void
Martin Liska committed
261
symbol_table::symtab_initialize_asm_name_hash (void)
262
{
263
  symtab_node *node;
264 265
  if (!assembler_name_hash)
    {
266
      assembler_name_hash = hash_table<asmname_hasher>::create_ggc (10);
267
      FOR_EACH_SYMBOL (node)
268
	insert_to_assembler_name_hash (node, false);
269
    }
270
}
271 272 273 274

/* Set the DECL_ASSEMBLER_NAME and update symtab hashtables.  */

void
Martin Liska committed
275
symbol_table::change_decl_assembler_name (tree decl, tree name)
276
{
277
  symtab_node *node = NULL;
278 279 280 281 282 283

  /* We can have user ASM names on things, like global register variables, that
     are not in the symbol table.  */
  if ((TREE_CODE (decl) == VAR_DECL
       && (TREE_STATIC (decl) || DECL_EXTERNAL (decl)))
      || TREE_CODE (decl) == FUNCTION_DECL)
Martin Liska committed
284
    node = symtab_node::get (decl);
285 286 287 288
  if (!DECL_ASSEMBLER_NAME_SET_P (decl))
    {
      SET_DECL_ASSEMBLER_NAME (decl, name);
      if (node)
289
	insert_to_assembler_name_hash (node, true);
290 291 292 293 294 295
    }
  else
    {
      if (name == DECL_ASSEMBLER_NAME (decl))
	return;

296 297 298
      tree alias = (IDENTIFIER_TRANSPARENT_ALIAS (DECL_ASSEMBLER_NAME (decl))
		    ? TREE_CHAIN (DECL_ASSEMBLER_NAME (decl))
		    : NULL);
299
      if (node)
300
	unlink_from_assembler_name_hash (node, true);
301 302 303 304 305
      if (TREE_SYMBOL_REFERENCED (DECL_ASSEMBLER_NAME (decl))
	  && DECL_RTL_SET_P (decl))
	warning (0, "%D renamed after being referenced in assembly", decl);

      SET_DECL_ASSEMBLER_NAME (decl, name);
306 307 308
      if (alias)
	{
	  IDENTIFIER_TRANSPARENT_ALIAS (name) = 1;
309
	  TREE_CHAIN (name) = alias;
310
	}
311
      if (node)
312
	insert_to_assembler_name_hash (node, true);
313 314 315
    }
}

Martin Liska committed
316 317
/* Hash sections by their names.  */

318 319
hashval_t
section_name_hasher::hash (section_hash_entry *n)
Martin Liska committed
320 321 322 323 324 325
{
  return htab_hash_string (n->name);
}

/* Return true if section P1 name equals to P2.  */

326 327
bool
section_name_hasher::equal (section_hash_entry *n1, const char *name)
Martin Liska committed
328 329 330 331 332 333 334 335 336 337
{
  return n1->name == name || !strcmp (n1->name, name);
}

/* Add node into symbol table.  This function is not used directly, but via
   cgraph/varpool node creation routines.  */

void
symtab_node::register_symbol (void)
{
Martin Liska committed
338
  symtab->register_symbol (this);
Martin Liska committed
339 340 341 342 343 344 345 346

  if (!decl->decl_with_vis.symtab_node)
    decl->decl_with_vis.symtab_node = this;

  ref_list.clear ();

  /* Be sure to do this last; C++ FE might create new nodes via
     DECL_ASSEMBLER_NAME langhook!  */
Martin Liska committed
347
  symtab->insert_to_assembler_name_hash (this, false);
Martin Liska committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
}

/* Remove NODE from same comdat group.   */

void
symtab_node::remove_from_same_comdat_group (void)
{
  if (same_comdat_group)
    {
      symtab_node *prev;
      for (prev = same_comdat_group;
	   prev->same_comdat_group != this;
	   prev = prev->same_comdat_group)
	;
      if (same_comdat_group == prev)
	prev->same_comdat_group = NULL;
      else
	prev->same_comdat_group = same_comdat_group;
      same_comdat_group = NULL;
      set_comdat_group (NULL);
    }
}

/* Remove node from symbol table.  This function is not used directly, but via
   cgraph/varpool node removal routines.  */

void
symtab_node::unregister (void)
{
  remove_all_references ();
  remove_all_referring ();

  /* Remove reference to section.  */
  set_section_for_node (NULL);

  remove_from_same_comdat_group ();

Martin Liska committed
385
  symtab->unregister (this);
Martin Liska committed
386 387 388 389 390 391 392 393 394 395 396 397

  /* During LTO symtab merging we temporarily corrupt decl to symtab node
     hash.  */
  gcc_assert (decl->decl_with_vis.symtab_node || in_lto_p);
  if (decl->decl_with_vis.symtab_node == this)
    {
      symtab_node *replacement_node = NULL;
      if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
	replacement_node = cnode->find_replacement ();
      decl->decl_with_vis.symtab_node = replacement_node;
    }
  if (!is_a <varpool_node *> (this) || !DECL_HARD_REGISTER (decl))
Martin Liska committed
398
    symtab->unlink_from_assembler_name_hash (this, false);
Martin Liska committed
399
  if (in_init_priority_hash)
400
    symtab->init_priority_hash->remove (this);
Martin Liska committed
401 402 403 404 405 406 407 408 409 410 411 412 413 414
}


/* Remove symbol from symbol table.  */

void
symtab_node::remove (void)
{
  if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
    cnode->remove ();
  else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
    vnode->remove ();
}

415 416 417
/* Add NEW_ to the same comdat group that OLD is in.  */

void
Martin Liska committed
418
symtab_node::add_to_same_comdat_group (symtab_node *old_node)
419
{
420
  gcc_assert (old_node->get_comdat_group ());
Martin Liska committed
421 422
  gcc_assert (!same_comdat_group);
  gcc_assert (this != old_node);
423

Martin Liska committed
424 425
  set_comdat_group (old_node->get_comdat_group ());
  same_comdat_group = old_node;
426
  if (!old_node->same_comdat_group)
Martin Liska committed
427
    old_node->same_comdat_group = this;
428 429
  else
    {
430
      symtab_node *n;
431 432 433
      for (n = old_node->same_comdat_group;
	   n->same_comdat_group != old_node;
	   n = n->same_comdat_group)
434
	;
Martin Liska committed
435
      n->same_comdat_group = this;
436 437 438 439 440 441
    }
}

/* Dissolve the same_comdat_group list in which NODE resides.  */

void
Martin Liska committed
442
symtab_node::dissolve_same_comdat_group_list (void)
443
{
Martin Liska committed
444
  symtab_node *n = this;
445
  symtab_node *next;
446

Martin Liska committed
447
  if (!same_comdat_group)
448 449 450
    return;
  do
    {
451 452
      next = n->same_comdat_group;
      n->same_comdat_group = NULL;
453
      /* Clear comdat_group for comdat locals, since
454 455
         make_decl_local doesn't.  */
      if (!TREE_PUBLIC (n->decl))
456
	n->set_comdat_group (NULL);
457 458
      n = next;
    }
Martin Liska committed
459
  while (n != this);
460 461
}

462 463 464 465 466
/* Return printable assembler name of NODE.
   This function is used only for debugging.  When assembler name
   is unknown go with identifier name.  */

const char *
467
symtab_node::asm_name () const
468
{
469 470 471
  if (!DECL_ASSEMBLER_NAME_SET_P (decl))
    return lang_hooks.decl_printable_name (decl, 2);
  return IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME (decl));
472 473 474 475 476
}

/* Return printable identifier name.  */

const char *
477
symtab_node::name () const
478
{
479
  return lang_hooks.decl_printable_name (decl, 2);
480 481
}

Martin Liska committed
482 483 484 485
/* Return ipa reference from this symtab_node to
   REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
   of the use.  */

Martin Liska committed
486 487 488
ipa_ref *
symtab_node::create_reference (symtab_node *referred_node,
			       enum ipa_ref_use use_type)
Martin Liska committed
489
{
Martin Liska committed
490
  return create_reference (referred_node, use_type, NULL);
Martin Liska committed
491 492 493 494 495 496 497
}


/* Return ipa reference from this symtab_node to
   REFERED_NODE or REFERED_VARPOOL_NODE. USE_TYPE specify type
   of the use and STMT the statement (if it exists).  */

Martin Liska committed
498 499 500
ipa_ref *
symtab_node::create_reference (symtab_node *referred_node,
			       enum ipa_ref_use use_type, gimple stmt)
Martin Liska committed
501
{
Martin Liska committed
502 503
  ipa_ref *ref = NULL, *ref2 = NULL;
  ipa_ref_list *list, *list2;
Martin Liska committed
504 505 506 507 508 509 510 511 512 513 514
  ipa_ref_t *old_references;

  gcc_checking_assert (!stmt || is_a <cgraph_node *> (this));
  gcc_checking_assert (use_type != IPA_REF_ALIAS || !stmt);

  list = &ref_list;
  old_references = vec_safe_address (list->references);
  vec_safe_grow (list->references, vec_safe_length (list->references) + 1);
  ref = &list->references->last ();

  list2 = &referred_node->ref_list;
Martin Liska committed
515 516 517

  /* IPA_REF_ALIAS is always inserted at the beginning of the list.   */
  if(use_type == IPA_REF_ALIAS)
518 519 520
    {
      list2->referring.safe_insert (0, ref);
      ref->referred_index = 0;
Martin Liska committed
521

522 523 524
      for (unsigned int i = 1; i < list2->referring.length (); i++)
	list2->referring[i]->referred_index = i;
    }
Martin Liska committed
525
  else
526 527 528 529
    {
      list2->referring.safe_push (ref);
      ref->referred_index = list2->referring.length () - 1;
    }
Martin Liska committed
530

Martin Liska committed
531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
  ref->referring = this;
  ref->referred = referred_node;
  ref->stmt = stmt;
  ref->lto_stmt_uid = 0;
  ref->use = use_type;
  ref->speculative = 0;

  /* If vector was moved in memory, update pointers.  */
  if (old_references != list->references->address ())
    {
      int i;
      for (i = 0; iterate_reference(i, ref2); i++)
	ref2->referred_ref_list ()->referring[ref2->referred_index] = ref2;
    }
  return ref;
}

/* If VAL is a reference to a function or a variable, add a reference from
   this symtab_node to the corresponding symbol table node.  USE_TYPE specify
   type of the use and STMT the statement (if it exists).  Return the new
   reference or NULL if none was created.  */

Martin Liska committed
553 554 555
ipa_ref *
symtab_node::maybe_create_reference (tree val, enum ipa_ref_use use_type,
				     gimple stmt)
Martin Liska committed
556 557 558 559 560 561 562 563
{
  STRIP_NOPS (val);
  if (TREE_CODE (val) != ADDR_EXPR)
    return NULL;
  val = get_base_var (val);
  if (val && (TREE_CODE (val) == FUNCTION_DECL
	       || TREE_CODE (val) == VAR_DECL))
    {
Martin Liska committed
564
      symtab_node *referred = symtab_node::get (val);
Martin Liska committed
565
      gcc_checking_assert (referred);
Martin Liska committed
566
      return create_reference (referred, use_type, stmt);
Martin Liska committed
567 568 569 570 571 572 573
    }
  return NULL;
}

/* Clone all references from symtab NODE to this symtab_node.  */

void
Martin Liska committed
574
symtab_node::clone_references (symtab_node *node)
Martin Liska committed
575
{
Martin Liska committed
576
  ipa_ref *ref = NULL, *ref2 = NULL;
Martin Liska committed
577 578 579 580 581 582
  int i;
  for (i = 0; node->iterate_reference (i, ref); i++)
    {
      bool speculative = ref->speculative;
      unsigned int stmt_uid = ref->lto_stmt_uid;

Martin Liska committed
583
      ref2 = create_reference (ref->referred, ref->use, ref->stmt);
Martin Liska committed
584 585 586 587 588 589 590 591
      ref2->speculative = speculative;
      ref2->lto_stmt_uid = stmt_uid;
    }
}

/* Clone all referring from symtab NODE to this symtab_node.  */

void
Martin Liska committed
592
symtab_node::clone_referring (symtab_node *node)
Martin Liska committed
593
{
Martin Liska committed
594
  ipa_ref *ref = NULL, *ref2 = NULL;
Martin Liska committed
595 596 597 598 599 600
  int i;
  for (i = 0; node->iterate_referring(i, ref); i++)
    {
      bool speculative = ref->speculative;
      unsigned int stmt_uid = ref->lto_stmt_uid;

Martin Liska committed
601
      ref2 = ref->referring->create_reference (this, ref->use, ref->stmt);
Martin Liska committed
602 603 604 605 606 607 608
      ref2->speculative = speculative;
      ref2->lto_stmt_uid = stmt_uid;
    }
}

/* Clone reference REF to this symtab_node and set its stmt to STMT.  */

Martin Liska committed
609 610
ipa_ref *
symtab_node::clone_reference (ipa_ref *ref, gimple stmt)
Martin Liska committed
611 612 613
{
  bool speculative = ref->speculative;
  unsigned int stmt_uid = ref->lto_stmt_uid;
Martin Liska committed
614
  ipa_ref *ref2;
Martin Liska committed
615

Martin Liska committed
616
  ref2 = create_reference (ref->referred, ref->use, stmt);
Martin Liska committed
617 618 619 620 621 622 623 624
  ref2->speculative = speculative;
  ref2->lto_stmt_uid = stmt_uid;
  return ref2;
}

/* Find the structure describing a reference to REFERRED_NODE
   and associated with statement STMT.  */

Martin Liska committed
625
ipa_ref *
Martin Liska committed
626 627 628
symtab_node::find_reference (symtab_node *referred_node,
			     gimple stmt, unsigned int lto_stmt_uid)
{
Martin Liska committed
629
  ipa_ref *r = NULL;
Martin Liska committed
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
  int i;

  for (i = 0; iterate_reference (i, r); i++)
    if (r->referred == referred_node
	&& !r->speculative
	&& ((stmt && r->stmt == stmt)
	    || (lto_stmt_uid && r->lto_stmt_uid == lto_stmt_uid)
	    || (!stmt && !lto_stmt_uid && !r->stmt && !r->lto_stmt_uid)))
      return r;
  return NULL;
}

/* Remove all references that are associated with statement STMT.  */

void
symtab_node::remove_stmt_references (gimple stmt)
{
Martin Liska committed
647
  ipa_ref *r = NULL;
Martin Liska committed
648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664
  int i = 0;

  while (iterate_reference (i, r))
    if (r->stmt == stmt)
      r->remove_reference ();
    else
      i++;
}

/* Remove all stmt references in non-speculative references.
   Those are not maintained during inlining & clonning.
   The exception are speculative references that are updated along
   with callgraph edges associated with them.  */

void
symtab_node::clear_stmts_in_references (void)
{
Martin Liska committed
665
  ipa_ref *r = NULL;
Martin Liska committed
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700
  int i;

  for (i = 0; iterate_reference (i, r); i++)
    if (!r->speculative)
      {
	r->stmt = NULL;
	r->lto_stmt_uid = 0;
      }
}

/* Remove all references in ref list.  */

void
symtab_node::remove_all_references (void)
{
  while (vec_safe_length (ref_list.references))
    ref_list.references->last ().remove_reference ();
  vec_free (ref_list.references);
}

/* Remove all referring items in ref list.  */

void
symtab_node::remove_all_referring (void)
{
  while (ref_list.referring.length ())
    ref_list.referring.last ()->remove_reference ();
  ref_list.referring.release ();
}

/* Dump references in ref list to FILE.  */

void
symtab_node::dump_references (FILE *file)
{
Martin Liska committed
701
  ipa_ref *ref = NULL;
Martin Liska committed
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719
  int i;
  for (i = 0; iterate_reference (i, ref); i++)
    {
      fprintf (file, "%s/%i (%s)",
               ref->referred->asm_name (),
               ref->referred->order,
	       ipa_ref_use_name [ref->use]);
      if (ref->speculative)
	fprintf (file, " (speculative)");
    }
  fprintf (file, "\n");
}

/* Dump referring in list to FILE.  */

void
symtab_node::dump_referring (FILE *file)
{
Martin Liska committed
720
  ipa_ref *ref = NULL;
Martin Liska committed
721 722 723 724 725 726 727 728 729 730 731 732 733
  int i;
  for (i = 0; iterate_referring(i, ref); i++)
    {
      fprintf (file, "%s/%i (%s)",
               ref->referring->asm_name (),
               ref->referring->order,
	       ipa_ref_use_name [ref->use]);
      if (ref->speculative)
	fprintf (file, " (speculative)");
    }
  fprintf (file, "\n");
}

734 735
static const char * const symtab_type_names[] = {"symbol", "function", "variable"};

Martin Liska committed
736
/* Dump base fields of symtab nodes to F.  Not to be used directly.  */
737 738

void
Martin Liska committed
739
symtab_node::dump_base (FILE *f)
740 741 742 743 744
{
  static const char * const visibility_types[] = {
    "default", "protected", "hidden", "internal"
  };

Martin Liska committed
745 746 747
  fprintf (f, "%s/%i (%s)", asm_name (), order, name ());
  dump_addr (f, " @", (void *)this);
  fprintf (f, "\n  Type: %s", symtab_type_names[type]);
748

Martin Liska committed
749
  if (definition)
750
    fprintf (f, " definition");
Martin Liska committed
751
  if (analyzed)
752
    fprintf (f, " analyzed");
Martin Liska committed
753
  if (alias)
754
    fprintf (f, " alias");
Martin Liska committed
755
  if (weakref)
756
    fprintf (f, " weakref");
Martin Liska committed
757
  if (cpp_implicit_alias)
758
    fprintf (f, " cpp_implicit_alias");
Martin Liska committed
759
  if (alias_target)
760
    fprintf (f, " target:%s",
Martin Liska committed
761
	     DECL_P (alias_target)
762
	     ? IDENTIFIER_POINTER (DECL_ASSEMBLER_NAME
Martin Liska committed
763 764 765
				     (alias_target))
	     : IDENTIFIER_POINTER (alias_target));
  if (body_removed)
766
    fprintf (f, "\n  Body removed by symtab_remove_unreachable_nodes");
767
  fprintf (f, "\n  Visibility:");
Martin Liska committed
768
  if (in_other_partition)
769
    fprintf (f, " in_other_partition");
Martin Liska committed
770
  if (used_from_other_partition)
771
    fprintf (f, " used_from_other_partition");
Martin Liska committed
772
  if (force_output)
773
    fprintf (f, " force_output");
Martin Liska committed
774
  if (forced_by_abi)
775
    fprintf (f, " forced_by_abi");
Martin Liska committed
776
  if (externally_visible)
777
    fprintf (f, " externally_visible");
778 779
  if (no_reorder)
    fprintf (f, " no_reorder");
Martin Liska committed
780
  if (resolution != LDPR_UNKNOWN)
781
    fprintf (f, " %s",
Martin Liska committed
782 783
 	     ld_plugin_symbol_resolution_names[(int)resolution]);
  if (TREE_ASM_WRITTEN (decl))
784
    fprintf (f, " asm_written");
Martin Liska committed
785
  if (DECL_EXTERNAL (decl))
786
    fprintf (f, " external");
Martin Liska committed
787
  if (TREE_PUBLIC (decl))
788
    fprintf (f, " public");
Martin Liska committed
789
  if (DECL_COMMON (decl))
790
    fprintf (f, " common");
Martin Liska committed
791
  if (DECL_WEAK (decl))
792
    fprintf (f, " weak");
Martin Liska committed
793
  if (DECL_DLLIMPORT_P (decl))
794
    fprintf (f, " dll_import");
Martin Liska committed
795
  if (DECL_COMDAT (decl))
796
    fprintf (f, " comdat");
Martin Liska committed
797
  if (get_comdat_group ())
798
    fprintf (f, " comdat_group:%s",
Martin Liska committed
799 800
	     IDENTIFIER_POINTER (get_comdat_group_id ()));
  if (DECL_ONE_ONLY (decl))
801
    fprintf (f, " one_only");
Martin Liska committed
802
  if (get_section ())
803
    fprintf (f, " section:%s",
Martin Liska committed
804 805
	     get_section ());
  if (implicit_section)
806
    fprintf (f," (implicit_section)");
Martin Liska committed
807
  if (DECL_VISIBILITY_SPECIFIED (decl))
808
    fprintf (f, " visibility_specified");
Martin Liska committed
809
  if (DECL_VISIBILITY (decl))
810
    fprintf (f, " visibility:%s",
Martin Liska committed
811 812
	     visibility_types [DECL_VISIBILITY (decl)]);
  if (DECL_VIRTUAL_P (decl))
813
    fprintf (f, " virtual");
Martin Liska committed
814
  if (DECL_ARTIFICIAL (decl))
815
    fprintf (f, " artificial");
Martin Liska committed
816
  if (TREE_CODE (decl) == FUNCTION_DECL)
817
    {
Martin Liska committed
818
      if (DECL_STATIC_CONSTRUCTOR (decl))
819
	fprintf (f, " constructor");
Martin Liska committed
820
      if (DECL_STATIC_DESTRUCTOR (decl))
821 822
	fprintf (f, " destructor");
    }
823 824
  fprintf (f, "\n");
  
Martin Liska committed
825
  if (same_comdat_group)
826
    fprintf (f, "  Same comdat group as: %s/%i\n",
Martin Liska committed
827 828 829
	     same_comdat_group->asm_name (),
	     same_comdat_group->order);
  if (next_sharing_asm_name)
830
    fprintf (f, "  next sharing asm name: %i\n",
Martin Liska committed
831 832
	     next_sharing_asm_name->order);
  if (previous_sharing_asm_name)
833
    fprintf (f, "  previous sharing asm name: %i\n",
Martin Liska committed
834
	     previous_sharing_asm_name->order);
835

Martin Liska committed
836
  if (address_taken)
837
    fprintf (f, "  Address is taken.\n");
Martin Liska committed
838
  if (aux)
839 840
    {
      fprintf (f, "  Aux:");
Martin Liska committed
841
      dump_addr (f, " @", (void *)aux);
842
    }
843 844

  fprintf (f, "  References: ");
Martin Liska committed
845
  dump_references (f);
846
  fprintf (f, "  Referring: ");
Martin Liska committed
847 848
  dump_referring (f);
  if (lto_file_data)
849
    fprintf (f, "  Read from file: %s\n",
Martin Liska committed
850
	     lto_file_data->file_name);
851 852
}

Martin Liska committed
853
/* Dump symtab node to F.  */
854 855

void
Martin Liska committed
856
symtab_node::dump (FILE *f)
857
{
Martin Liska committed
858 859 860 861
  if (cgraph_node *cnode = dyn_cast <cgraph_node *> (this))
    cnode->dump (f);
  else if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
    vnode->dump (f);
862 863
}

Martin Liska committed
864
/* Dump symbol table to F.  */
865 866

void
Martin Liska committed
867
symtab_node::dump_table (FILE *f)
868
{
869
  symtab_node *node;
870 871
  fprintf (f, "Symbol table:\n\n");
  FOR_EACH_SYMBOL (node)
Martin Liska committed
872
    node->dump (f);
873 874
}

Martin Liska committed
875 876 877 878 879 880 881 882 883 884

/* Return the cgraph node that has ASMNAME for its DECL_ASSEMBLER_NAME.
   Return NULL if there's no such node.  */

symtab_node *
symtab_node::get_for_asmname (const_tree asmname)
{
  symtab_node *node;

  symtab->symtab_initialize_asm_name_hash ();
885 886 887 888
  hashval_t hash = symtab->decl_assembler_name_hash (asmname);
  symtab_node **slot
    = symtab->assembler_name_hash->find_slot_with_hash (asmname, hash,
							NO_INSERT);
Martin Liska committed
889 890 891

  if (slot)
    {
892
      node = *slot;
Martin Liska committed
893 894 895 896 897
      return node;
    }
  return NULL;
}

898 899 900
/* Dump symtab node NODE to stderr.  */

DEBUG_FUNCTION void
Martin Liska committed
901
symtab_node::debug (void)
902
{
Martin Liska committed
903
  dump (stderr);
904 905
}

906 907 908
/* Verify common part of symtab nodes.  */

DEBUG_FUNCTION bool
Martin Liska committed
909
symtab_node::verify_base (void)
910 911
{
  bool error_found = false;
912
  symtab_node *hashed_node;
913

Martin Liska committed
914
  if (is_a <cgraph_node *> (this))
915
    {
Martin Liska committed
916
      if (TREE_CODE (decl) != FUNCTION_DECL)
917 918 919 920 921
	{
          error ("function symbol is not function");
          error_found = true;
	}
    }
Martin Liska committed
922
  else if (is_a <varpool_node *> (this))
923
    {
Martin Liska committed
924
      if (TREE_CODE (decl) != VAR_DECL)
925 926 927 928 929 930 931 932 933 934 935
	{
          error ("variable symbol is not variable");
          error_found = true;
	}
    }
  else
    {
      error ("node has unknown type");
      error_found = true;
    }
   
Martin Liska committed
936
  if (symtab->state != LTO_STREAMING)
937
    {
Martin Liska committed
938
      hashed_node = symtab_node::get (decl);
939 940
      if (!hashed_node)
	{
941
	  error ("node not found node->decl->decl_with_vis.symtab_node");
942 943
	  error_found = true;
	}
Martin Liska committed
944 945 946 947
      if (hashed_node != this
	  && (!is_a <cgraph_node *> (this)
	      || !dyn_cast <cgraph_node *> (this)->clone_of
	      || dyn_cast <cgraph_node *> (this)->clone_of->decl != decl))
948
	{
949
	  error ("node differs from node->decl->decl_with_vis.symtab_node");
950 951
	  error_found = true;
	}
952
    }
Martin Liska committed
953
  if (symtab->assembler_name_hash)
954
    {
Martin Liska committed
955
      hashed_node = symtab_node::get_for_asmname (DECL_ASSEMBLER_NAME (decl));
956
      if (hashed_node && hashed_node->previous_sharing_asm_name)
957 958 959 960 961 962
	{
          error ("assembler name hash list corrupted");
          error_found = true;
	}
      while (hashed_node)
	{
Martin Liska committed
963
	  if (hashed_node == this)
964
	    break;
965
	  hashed_node = hashed_node->next_sharing_asm_name;
966
	}
967
      if (!hashed_node
Martin Liska committed
968 969
	  && !(is_a <varpool_node *> (this)
	       || DECL_HARD_REGISTER (decl)))
970 971 972 973 974
	{
          error ("node not found in symtab assembler name hash");
          error_found = true;
	}
    }
Martin Liska committed
975 976
  if (previous_sharing_asm_name
      && previous_sharing_asm_name->next_sharing_asm_name != this)
977 978
    {
      error ("double linked list of assembler names corrupted");
979 980 981 982 983
      error_found = true;
    }
  if (body_removed && definition)
    {
      error ("node has body_removed but is definition");
984 985
      error_found = true;
    }
Martin Liska committed
986
  if (analyzed && !definition)
987 988 989
    {
      error ("node is analyzed byt it is not a definition");
      error_found = true;
990
    }
Martin Liska committed
991
  if (cpp_implicit_alias && !alias)
992 993 994 995
    {
      error ("node is alias but not implicit alias");
      error_found = true;
    }
Martin Liska committed
996
  if (alias && !definition && !weakref)
997 998 999 1000
    {
      error ("node is alias but not definition");
      error_found = true;
    }
Martin Liska committed
1001
  if (weakref && !alias)
1002 1003 1004 1005
    {
      error ("node is weakref but not an alias");
      error_found = true;
    }
Martin Liska committed
1006
  if (same_comdat_group)
1007
    {
Martin Liska committed
1008
      symtab_node *n = same_comdat_group;
1009

1010
      if (!n->get_comdat_group ())
1011
	{
1012
	  error ("node is in same_comdat_group list but has no comdat_group");
1013 1014
	  error_found = true;
	}
Martin Liska committed
1015
      if (n->get_comdat_group () != get_comdat_group ())
1016 1017 1018 1019
	{
	  error ("same_comdat_group list across different groups");
	  error_found = true;
	}
Martin Liska committed
1020
      if (n->type != type)
1021 1022 1023 1024
	{
	  error ("mixing different types of symbol in same comdat groups is not supported");
	  error_found = true;
	}
Martin Liska committed
1025
      if (n == this)
1026 1027 1028 1029 1030 1031
	{
	  error ("node is alone in a comdat group");
	  error_found = true;
	}
      do
	{
1032
	  if (!n->same_comdat_group)
1033 1034 1035 1036 1037
	    {
	      error ("same_comdat_group is not a circular list");
	      error_found = true;
	      break;
	    }
1038
	  n = n->same_comdat_group;
1039
	}
Martin Liska committed
1040 1041
      while (n != this);
      if (comdat_local_p ())
1042
	{
Martin Liska committed
1043
	  ipa_ref *ref = NULL;
1044

Martin Liska committed
1045
	  for (int i = 0; iterate_referring (i, ref); ++i)
1046
	    {
Martin Liska committed
1047
	      if (!in_same_comdat_group_p (ref->referring))
1048 1049 1050 1051 1052 1053 1054 1055
		{
		  error ("comdat-local symbol referred to by %s outside its "
			 "comdat",
			 identifier_to_locale (ref->referring->name()));
		  error_found = true;
		}
	    }
	}
1056
    }
Martin Liska committed
1057
  if (implicit_section && !get_section ())
1058 1059 1060 1061
    {
      error ("implicit_section flag is set but section isn't");
      error_found = true;
    }
Martin Liska committed
1062
  if (get_section () && get_comdat_group ()
1063 1064
      && !implicit_section
      && !lookup_attribute ("section", DECL_ATTRIBUTES (decl)))
1065 1066 1067 1068 1069 1070
    {
      error ("Both section and comdat group is set");
      error_found = true;
    }
  /* TODO: Add string table for sections, so we do not keep holding duplicated
     strings.  */
Martin Liska committed
1071 1072 1073 1074 1075 1076
  if (alias && definition
      && get_section () != get_alias_target ()->get_section ()
      && (!get_section()
	  || !get_alias_target ()->get_section ()
	  || strcmp (get_section(),
		     get_alias_target ()->get_section ())))
1077 1078
    {
      error ("Alias and target's section differs");
Martin Liska committed
1079
      get_alias_target ()->dump (stderr);
1080 1081
      error_found = true;
    }
Martin Liska committed
1082 1083
  if (alias && definition
      && get_comdat_group () != get_alias_target ()->get_comdat_group ())
1084 1085
    {
      error ("Alias and target's comdat groups differs");
Martin Liska committed
1086
      get_alias_target ()->dump (stderr);
1087 1088 1089
      error_found = true;
    }

1090 1091 1092 1093 1094 1095
  return error_found;
}

/* Verify consistency of NODE.  */

DEBUG_FUNCTION void
Martin Liska committed
1096
symtab_node::verify (void)
1097 1098 1099 1100 1101
{
  if (seen_error ())
    return;

  timevar_push (TV_CGRAPH_VERIFY);
Martin Liska committed
1102 1103
  if (cgraph_node *node = dyn_cast <cgraph_node *> (this))
    node->verify_node ();
1104
  else
Martin Liska committed
1105
    if (verify_base ())
1106
      {
Martin Liska committed
1107 1108
	debug ();
	internal_error ("symtab_node::verify failed");
1109 1110 1111 1112 1113 1114 1115
      }
  timevar_pop (TV_CGRAPH_VERIFY);
}

/* Verify symbol table for internal consistency.  */

DEBUG_FUNCTION void
Martin Liska committed
1116
symtab_node::verify_symtab_nodes (void)
1117
{
1118
  symtab_node *node;
Trevor Saunders committed
1119
  hash_map<tree, symtab_node *> comdat_head_map (251);
1120

1121
  FOR_EACH_SYMBOL (node)
1122
    {
Martin Liska committed
1123
      node->verify ();
1124 1125 1126 1127 1128
      if (node->get_comdat_group ())
	{
	  symtab_node **entry, *s;
	  bool existed;

Trevor Saunders committed
1129 1130
	  entry = &comdat_head_map.get_or_insert (node->get_comdat_group (),
						  &existed);
1131 1132 1133 1134 1135 1136 1137
	  if (!existed)
	    *entry = node;
	  else
	    for (s = (*entry)->same_comdat_group; s != NULL && s != node; s = s->same_comdat_group)
	      if (!s || s == *entry)
		{
		  error ("Two symbols with same comdat_group are not linked by the same_comdat_group list.");
Martin Liska committed
1138 1139 1140
		  (*entry)->debug ();
		  node->debug ();
		  internal_error ("symtab_node::verify failed");
1141 1142 1143
		}
	}
    }
1144 1145
}

1146 1147
/* Make DECL local.  FIXME: We shouldn't need to mess with rtl this early,
   but other code such as notice_global_symbol generates rtl.  */
1148

1149
void
Martin Liska committed
1150
symtab_node::make_decl_local (void)
1151 1152 1153
{
  rtx rtl, symbol;

1154
  /* Avoid clearing comdat_groups on comdat-local decls.  */
1155 1156 1157
  if (TREE_PUBLIC (decl) == 0)
    return;

1158
  if (TREE_CODE (decl) == VAR_DECL)
1159 1160 1161 1162 1163
    {
      DECL_COMMON (decl) = 0;
      /* ADDRESSABLE flag is not defined for public symbols.  */
      TREE_ADDRESSABLE (decl) = 1;
    }
1164 1165
  else gcc_assert (TREE_CODE (decl) == FUNCTION_DECL);

1166
  DECL_COMDAT (decl) = 0;
1167 1168
  DECL_WEAK (decl) = 0;
  DECL_EXTERNAL (decl) = 0;
1169 1170
  DECL_VISIBILITY_SPECIFIED (decl) = 0;
  DECL_VISIBILITY (decl) = VISIBILITY_DEFAULT;
1171
  TREE_PUBLIC (decl) = 0;
1172
  DECL_DLLIMPORT_P (decl) = 0;
1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
  if (!DECL_RTL_SET_P (decl))
    return;

  /* Update rtl flags.  */
  make_decl_rtl (decl);

  rtl = DECL_RTL (decl);
  if (!MEM_P (rtl))
    return;

  symbol = XEXP (rtl, 0);
  if (GET_CODE (symbol) != SYMBOL_REF)
    return;

  SYMBOL_REF_WEAK (symbol) = DECL_WEAK (decl);
}
1189

Martin Liska committed
1190
/* Walk the alias chain to return the symbol NODE is alias of.
1191
   If NODE is not an alias, return NODE.
1192
   Assumes NODE is known to be alias.  */
1193

1194
symtab_node *
1195
symtab_node::ultimate_alias_target_1 (enum availability *availability)
1196
{
1197 1198 1199 1200
  bool weakref_p = false;

  /* To determine visibility of the target, we follow ELF semantic of aliases.
     Here alias is an alternative assembler name of a given definition. Its
Ondřej Bílka committed
1201
     availability prevails the availability of its target (i.e. static alias of
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
     weak definition is available.

     Weakref is a different animal (and not part of ELF per se). It is just
     alternative name of a given symbol used within one complation unit
     and is translated prior hitting the object file.  It inherits the
     visibility of its target (i.e. weakref of non-overwritable definition
     is non-overwritable, while weakref of weak definition is weak).

     If we ever get into supporting targets with different semantics, a target
     hook will be needed here.  */

1213
  if (availability)
1214
    {
Martin Liska committed
1215
      weakref_p = weakref;
1216
      if (!weakref_p)
Martin Liska committed
1217
	*availability = get_availability ();
1218 1219 1220
      else
	*availability = AVAIL_LOCAL;
    }
Martin Liska committed
1221 1222

  symtab_node *node = this;
1223 1224
  while (node)
    {
1225
      if (node->alias && node->analyzed)
Martin Liska committed
1226
	node = node->get_alias_target ();
1227
      else
1228 1229 1230
	{
	  if (!availability)
	    ;
1231
	  else if (node->analyzed)
1232 1233 1234
	    {
	      if (weakref_p)
		{
Martin Liska committed
1235
		  enum availability a = node->get_availability ();
1236 1237 1238 1239 1240 1241 1242 1243 1244
		  if (a < *availability)
		    *availability = a;
		}
	    }
	  else
	    *availability = AVAIL_NOT_AVAILABLE;
	  return node;
	}
      if (node && availability && weakref_p)
1245
	{
Martin Liska committed
1246
	  enum availability a = node->get_availability ();
1247 1248
	  if (a < *availability)
	    *availability = a;
1249
          weakref_p = node->weakref;
1250 1251 1252 1253 1254 1255
	}
    }
  if (availability)
    *availability = AVAIL_NOT_AVAILABLE;
  return NULL;
}
1256 1257 1258 1259 1260 1261 1262 1263 1264

/* C++ FE sometimes change linkage flags after producing same body aliases.

   FIXME: C++ produce implicit aliases for virtual functions and vtables that
   are obviously equivalent.  The way it is doing so is however somewhat
   kludgy and interferes with the visibility code. As a result we need to
   copy the visibility from the target to get things right.  */

void
Martin Liska committed
1265
symtab_node::fixup_same_cpp_alias_visibility (symtab_node *target)
1266
{
Martin Liska committed
1267
  if (is_a <cgraph_node *> (this))
1268
    {
Martin Liska committed
1269
      DECL_DECLARED_INLINE_P (decl)
1270
	 = DECL_DECLARED_INLINE_P (target->decl);
Martin Liska committed
1271
      DECL_DISREGARD_INLINE_LIMITS (decl)
1272
	 = DECL_DISREGARD_INLINE_LIMITS (target->decl);
1273 1274 1275 1276 1277
    }
  /* FIXME: It is not really clear why those flags should not be copied for
     functions, too.  */
  else
    {
Martin Liska committed
1278 1279 1280
      DECL_WEAK (decl) = DECL_WEAK (target->decl);
      DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
      DECL_VISIBILITY (decl) = DECL_VISIBILITY (target->decl);
1281
    }
Martin Liska committed
1282 1283
  DECL_VIRTUAL_P (decl) = DECL_VIRTUAL_P (target->decl);
  if (TREE_PUBLIC (decl))
1284
    {
1285 1286
      tree group;

Martin Liska committed
1287 1288
      DECL_EXTERNAL (decl) = DECL_EXTERNAL (target->decl);
      DECL_COMDAT (decl) = DECL_COMDAT (target->decl);
1289
      group = target->get_comdat_group ();
Martin Liska committed
1290 1291 1292
      set_comdat_group (group);
      if (group && !same_comdat_group)
	add_to_same_comdat_group (target);
1293
    }
Martin Liska committed
1294
  externally_visible = target->externally_visible;
1295 1296 1297 1298
}

/* Set section, do not recurse into aliases.
   When one wants to change section of symbol and its aliases,
Martin Liska committed
1299
   use set_section.  */
1300 1301 1302 1303 1304

void
symtab_node::set_section_for_node (const char *section)
{
  const char *current = get_section ();
1305
  section_hash_entry **slot;
1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316

  if (current == section
      || (current && section
	  && !strcmp (current, section)))
    return;

  if (current)
    {
      x_section->ref_count--;
      if (!x_section->ref_count)
	{
1317 1318 1319
	  hashval_t hash = htab_hash_string (x_section->name);
	  slot = symtab->section_hash->find_slot_with_hash (x_section->name,
							    hash, INSERT);
1320
	  ggc_free (x_section);
1321
	  symtab->section_hash->clear_slot (slot);
1322 1323 1324 1325 1326 1327 1328 1329
	}
      x_section = NULL;
    }
  if (!section)
    {
      implicit_section = false;
      return;
    }
Martin Liska committed
1330
  if (!symtab->section_hash)
1331 1332 1333 1334
    symtab->section_hash = hash_table<section_name_hasher>::create_ggc (10);
  slot = symtab->section_hash->find_slot_with_hash (section,
						    htab_hash_string (section),
						    INSERT);
1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346
  if (*slot)
    x_section = (section_hash_entry *)*slot;
  else
    {
      int len = strlen (section);
      *slot = x_section = ggc_cleared_alloc<section_hash_entry> ();
      x_section->name = ggc_vec_alloc<char> (len + 1);
      memcpy (x_section->name, section, len + 1);
    }
  x_section->ref_count++;
}

1347 1348
/* Worker for set_section.  */

Martin Liska committed
1349 1350
bool
symtab_node::set_section (symtab_node *n, void *s)
1351
{
1352
  n->set_section_for_node ((char *)s);
1353 1354 1355 1356 1357 1358
  return false;
}

/* Set section of symbol and its aliases.  */

void
1359
symtab_node::set_section (const char *section)
1360 1361
{
  gcc_assert (!this->alias);
Martin Liska committed
1362 1363
  call_for_symbol_and_aliases
    (symtab_node::set_section, const_cast<char *>(section), true);
1364 1365
}

1366 1367 1368 1369 1370 1371 1372
/* Return the initialization priority.  */

priority_type
symtab_node::get_init_priority ()
{
  if (!this->in_init_priority_hash)
    return DEFAULT_INIT_PRIORITY;
1373 1374

  symbol_priority_map *h = symtab->init_priority_hash->get (this);
1375 1376 1377 1378 1379 1380 1381 1382 1383 1384
  return h ? h->init : DEFAULT_INIT_PRIORITY;
}

/* Return the finalization priority.  */

priority_type
cgraph_node::get_fini_priority ()
{
  if (!this->in_init_priority_hash)
    return DEFAULT_INIT_PRIORITY;
1385
  symbol_priority_map *h = symtab->init_priority_hash->get (this);
1386 1387 1388 1389 1390 1391 1392
  return h ? h->fini : DEFAULT_INIT_PRIORITY;
}

/* Return the initialization and finalization priority information for
   DECL.  If there is no previous priority information, a freshly
   allocated structure is returned.  */

Martin Liska committed
1393
symbol_priority_map *
Martin Liska committed
1394
symtab_node::priority_info (void)
1395
{
Martin Liska committed
1396
  if (!symtab->init_priority_hash)
1397
    symtab->init_priority_hash = hash_map<symtab_node *, symbol_priority_map>::create_ggc (13);
1398

1399 1400 1401 1402
  bool existed;
  symbol_priority_map *h
    = &symtab->init_priority_hash->get_or_insert (this, &existed);
  if (!existed)
1403 1404 1405
    {
      h->init = DEFAULT_INIT_PRIORITY;
      h->fini = DEFAULT_INIT_PRIORITY;
Martin Liska committed
1406
      in_init_priority_hash = true;
1407 1408 1409 1410 1411 1412 1413 1414 1415 1416
    }

  return h;
}

/* Set initialization priority to PRIORITY.  */

void
symtab_node::set_init_priority (priority_type priority)
{
Martin Liska committed
1417
  symbol_priority_map *h;
1418 1419 1420 1421 1422 1423 1424 1425 1426

  if (is_a <cgraph_node *> (this))
    gcc_assert (DECL_STATIC_CONSTRUCTOR (this->decl));

  if (priority == DEFAULT_INIT_PRIORITY)
    {
      gcc_assert (get_init_priority() == priority);
      return;
    }
Martin Liska committed
1427
  h = priority_info ();
1428 1429 1430 1431 1432 1433 1434 1435
  h->init = priority;
}

/* Set fialization priority to PRIORITY.  */

void
cgraph_node::set_fini_priority (priority_type priority)
{
Martin Liska committed
1436
  symbol_priority_map *h;
1437 1438 1439 1440 1441 1442 1443 1444

  gcc_assert (DECL_STATIC_DESTRUCTOR (this->decl));

  if (priority == DEFAULT_INIT_PRIORITY)
    {
      gcc_assert (get_fini_priority() == priority);
      return;
    }
Martin Liska committed
1445
  h = priority_info ();
1446 1447 1448
  h->fini = priority;
}

1449 1450
/* Worker for symtab_resolve_alias.  */

Martin Liska committed
1451 1452 1453
bool
symtab_node::set_implicit_section (symtab_node *n,
				   void *data ATTRIBUTE_UNUSED)
1454 1455 1456 1457 1458
{
  n->implicit_section = true;
  return false;
}

Martin Liska committed
1459
/* Add reference recording that symtab node is alias of TARGET.
1460 1461 1462 1463
   The function can fail in the case of aliasing cycles; in this case
   it returns false.  */

bool
Martin Liska committed
1464
symtab_node::resolve_alias (symtab_node *target)
1465
{
1466
  symtab_node *n;
1467

Martin Liska committed
1468
  gcc_assert (!analyzed && !vec_safe_length (ref_list.references));
1469 1470 1471

  /* Never let cycles to creep into the symbol table alias references;
     those will make alias walkers to be infinite.  */
1472
  for (n = target; n && n->alias;
Martin Liska committed
1473 1474
       n = n->analyzed ? n->get_alias_target () : NULL)
    if (n == this)
1475
       {
Martin Liska committed
1476 1477 1478 1479
	 if (is_a <cgraph_node *> (this))
	   error ("function %q+D part of alias cycle", decl);
	 else if (is_a <varpool_node *> (this))
	   error ("variable %q+D part of alias cycle", decl);
1480 1481
	 else
	   gcc_unreachable ();
Martin Liska committed
1482
	 alias = false;
1483 1484 1485 1486
	 return false;
       }

  /* "analyze" the node - i.e. mark the reference.  */
Martin Liska committed
1487 1488 1489
  definition = true;
  alias = true;
  analyzed = true;
Martin Liska committed
1490
  create_reference (target, IPA_REF_ALIAS, NULL);
1491

1492
  /* Add alias into the comdat group of its target unless it is already there.  */
Martin Liska committed
1493 1494 1495
  if (same_comdat_group)
    remove_from_same_comdat_group ();
  set_comdat_group (NULL);
1496
  if (target->get_comdat_group ())
Martin Liska committed
1497
    add_to_same_comdat_group (target);
1498

Martin Liska committed
1499 1500
  if ((get_section () != target->get_section ()
       || target->get_comdat_group ()) && get_section () && !implicit_section)
1501
    {
Martin Liska committed
1502
      error ("section of alias %q+D must match section of its target", decl);
1503
    }
Martin Liska committed
1504 1505
  call_for_symbol_and_aliases (symtab_node::set_section,
			     const_cast<char *>(target->get_section ()), true);
1506
  if (target->implicit_section)
Martin Liska committed
1507
    call_for_symbol_and_aliases (set_implicit_section, NULL, true);
1508

1509
  /* Alias targets become redundant after alias is resolved into an reference.
1510 1511
     We do not want to keep it around or we would have to mind updating them
     when renaming symbols.  */
Martin Liska committed
1512
  alias_target = NULL;
1513

Martin Liska committed
1514
  if (cpp_implicit_alias && symtab->state >= CONSTRUCTION)
Martin Liska committed
1515
    fixup_same_cpp_alias_visibility (target);
1516 1517

  /* If alias has address taken, so does the target.  */
Martin Liska committed
1518 1519
  if (address_taken)
    target->ultimate_alias_target ()->address_taken = true;
1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532

  /* All non-weakref aliases of THIS are now in fact aliases of TARGET.  */
  ipa_ref *ref;
  for (unsigned i = 0; iterate_direct_aliases (i, ref);)
    {
      struct symtab_node *alias_alias = ref->referring;
      if (!alias_alias->weakref)
	{
	  alias_alias->remove_all_references ();
	  alias_alias->create_reference (target, IPA_REF_ALIAS, NULL);
	}
      else i++;
    }
1533 1534
  return true;
}
1535

Martin Liska committed
1536
/* Worker searching noninterposable alias.  */
1537

Martin Liska committed
1538 1539
bool
symtab_node::noninterposable_alias (symtab_node *node, void *data)
1540
{
1541
  if (decl_binds_to_current_def_p (node->decl))
1542
    {
Martin Liska committed
1543
      symtab_node *fn = node->ultimate_alias_target ();
Jan Hubicka committed
1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554

      /* Ensure that the alias is well formed this may not be the case
	 of user defined aliases and currently it is not always the case
	 of C++ same body aliases (that is a bug).  */
      if (TREE_TYPE (node->decl) != TREE_TYPE (fn->decl)
	  || DECL_CONTEXT (node->decl) != DECL_CONTEXT (fn->decl)
	  || (TREE_CODE (node->decl) == FUNCTION_DECL
	      && flags_from_decl_or_type (node->decl)
		 != flags_from_decl_or_type (fn->decl))
	  || DECL_ATTRIBUTES (node->decl) != DECL_ATTRIBUTES (fn->decl))
	return false;
1555
      *(symtab_node **)data = node;
1556 1557 1558 1559 1560
      return true;
    }
  return false;
}

Martin Liska committed
1561 1562 1563
/* If node can not be overwriten by static or dynamic linker to point to
   different definition, return NODE. Otherwise look for alias with such
   property and if none exists, introduce new one.  */
1564

1565
symtab_node *
Martin Liska committed
1566
symtab_node::noninterposable_alias (void)
1567 1568
{
  tree new_decl;
1569
  symtab_node *new_node = NULL;
1570 1571 1572

  /* First try to look up existing alias or base object
     (if that is already non-overwritable).  */
Martin Liska committed
1573
  symtab_node *node = ultimate_alias_target ();
1574
  gcc_assert (!node->alias && !node->weakref);
Martin Liska committed
1575 1576
  node->call_for_symbol_and_aliases (symtab_node::noninterposable_alias,
				   (void *)&new_node, true);
1577 1578
  if (new_node)
    return new_node;
1579 1580 1581 1582
#ifndef ASM_OUTPUT_DEF
  /* If aliases aren't supported by the assembler, fail.  */
  return NULL;
#endif
1583

1584
  /* Otherwise create a new one.  */
1585
  new_decl = copy_node (node->decl);
1586
  DECL_DLLIMPORT_P (new_decl) = 0;
1587
  DECL_NAME (new_decl) = clone_function_name (node->decl, "localalias");
1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598
  if (TREE_CODE (new_decl) == FUNCTION_DECL)
    DECL_STRUCT_FUNCTION (new_decl) = NULL;
  DECL_INITIAL (new_decl) = NULL;
  SET_DECL_ASSEMBLER_NAME (new_decl, DECL_NAME (new_decl));
  SET_DECL_RTL (new_decl, NULL);

  /* Update the properties.  */
  DECL_EXTERNAL (new_decl) = 0;
  TREE_PUBLIC (new_decl) = 0;
  DECL_COMDAT (new_decl) = 0;
  DECL_WEAK (new_decl) = 0;
1599 1600 1601

  /* Since the aliases can be added to vtables, keep DECL_VIRTUAL flag.  */
  DECL_VIRTUAL_P (new_decl) = DECL_VIRTUAL_P (node->decl);
1602 1603 1604 1605
  if (TREE_CODE (new_decl) == FUNCTION_DECL)
    {
      DECL_STATIC_CONSTRUCTOR (new_decl) = 0;
      DECL_STATIC_DESTRUCTOR (new_decl) = 0;
Martin Liska committed
1606
      new_node = cgraph_node::create_alias (new_decl, node->decl);
1607 1608
    }
  else
1609 1610
    {
      TREE_READONLY (new_decl) = TREE_READONLY (node->decl);
1611
      DECL_INITIAL (new_decl) = error_mark_node;
Martin Liska committed
1612
      new_node = varpool_node::create_alias (new_decl, node->decl);
1613
    }
Martin Liska committed
1614
  new_node->resolve_alias (node);
1615 1616
  gcc_assert (decl_binds_to_current_def_p (new_decl)
	      && targetm.binds_local_p (new_decl));
1617 1618
  return new_node;
}
1619

Martin Liska committed
1620 1621
/* Return true if symtab node and TARGET represents
   semantically equivalent symbols.  */
1622 1623

bool
Martin Liska committed
1624
symtab_node::semantically_equivalent_p (symtab_node *target)
1625 1626
{
  enum availability avail;
1627 1628
  symtab_node *ba;
  symtab_node *bb;
1629 1630

  /* Equivalent functions are equivalent.  */
Martin Liska committed
1631
  if (decl == target->decl)
1632 1633 1634 1635
    return true;

  /* If symbol is not overwritable by different implementation,
     walk to the base object it defines.  */
Martin Liska committed
1636
  ba = ultimate_alias_target (&avail);
1637 1638
  if (avail >= AVAIL_AVAILABLE)
    {
Martin Liska committed
1639
      if (target == ba)
1640 1641 1642
	return true;
    }
  else
Martin Liska committed
1643 1644
    ba = this;
  bb = target->ultimate_alias_target (&avail);
1645 1646
  if (avail >= AVAIL_AVAILABLE)
    {
Martin Liska committed
1647
      if (this == bb)
1648 1649 1650
	return true;
    }
  else
Martin Liska committed
1651
    bb = target;
1652 1653
  return bb == ba;
}
1654

Martin Liska committed
1655
/* Classify symbol symtab node for partitioning.  */
1656 1657

enum symbol_partitioning_class
Martin Liska committed
1658
symtab_node::get_partitioning_class (void)
1659 1660 1661
{
  /* Inline clones are always duplicated.
     This include external delcarations.   */
Martin Liska committed
1662
  cgraph_node *cnode = dyn_cast <cgraph_node *> (this);
1663

1664
  if (DECL_ABSTRACT_P (decl))
1665 1666 1667 1668 1669 1670
    return SYMBOL_EXTERNAL;

  if (cnode && cnode->global.inlined_to)
    return SYMBOL_DUPLICATE;

  /* Weakref aliases are always duplicated.  */
Martin Liska committed
1671
  if (weakref)
1672 1673 1674
    return SYMBOL_DUPLICATE;

  /* External declarations are external.  */
Martin Liska committed
1675
  if (DECL_EXTERNAL (decl))
1676 1677
    return SYMBOL_EXTERNAL;

Martin Liska committed
1678
  if (varpool_node *vnode = dyn_cast <varpool_node *> (this))
1679
    {
1680 1681
      if (alias && definition && !ultimate_alias_target ()->definition)
	return SYMBOL_EXTERNAL;
1682 1683 1684
      /* Constant pool references use local symbol names that can not
         be promoted global.  We should never put into a constant pool
         objects that can not be duplicated across partitions.  */
Martin Liska committed
1685
      if (DECL_IN_CONSTANT_POOL (decl))
1686 1687 1688 1689 1690 1691 1692
	return SYMBOL_DUPLICATE;
      gcc_checking_assert (vnode->definition);
    }
  /* Functions that are cloned may stay in callgraph even if they are unused.
     Handle them as external; compute_ltrans_boundary take care to make
     proper things to happen (i.e. to make them appear in the boundary but
     with body streamed, so clone can me materialized).  */
1693
  else if (!dyn_cast <cgraph_node *> (this)->function_symbol ()->definition)
1694 1695 1696
    return SYMBOL_EXTERNAL;

  /* Linker discardable symbols are duplicated to every use unless they are
1697
     keyed.  */
Martin Liska committed
1698 1699 1700 1701
  if (DECL_ONE_ONLY (decl)
      && !force_output
      && !forced_by_abi
      && !used_from_object_file_p ())
1702 1703 1704 1705
    return SYMBOL_DUPLICATE;

  return SYMBOL_PARTITION;
}
1706 1707 1708 1709 1710 1711 1712

/* Return true when symbol is known to be non-zero.  */

bool
symtab_node::nonzero_address ()
{
  /* Weakrefs may be NULL when their target is not defined.  */
1713
  if (alias && weakref)
1714
    {
1715
      if (analyzed)
1716
	{
Martin Liska committed
1717
	  symtab_node *target = ultimate_alias_target ();
1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729

	  if (target->alias && target->weakref)
	    return false;
	  /* We can not recurse to target::nonzero.  It is possible that the
	     target is used only via the alias.
	     We may walk references and look for strong use, but we do not know
	     if this strong use will survive to final binary, so be
	     conservative here.  
	     ??? Maybe we could do the lookup during late optimization that
	     could be useful to eliminate the NULL pointer checks in LTO
	     programs.  */
	  if (target->definition && !DECL_EXTERNAL (target->decl))
1730
	      return true;
1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748
	  if (target->resolution != LDPR_UNKNOWN
	      && target->resolution != LDPR_UNDEF
	      && flag_delete_null_pointer_checks)
	    return true;
	  return false;
	}
      else
        return false;
    }

  /* With !flag_delete_null_pointer_checks we assume that symbols may
     bind to NULL. This is on by default on embedded targets only.

     Otherwise all non-WEAK symbols must be defined and thus non-NULL or
     linking fails.  Important case of WEAK we want to do well are comdats.
     Those are handled by later check for definition.

     When parsing, beware the cases when WEAK attribute is added later.  */
1749 1750 1751 1752 1753 1754
  if (!DECL_WEAK (decl)
      && flag_delete_null_pointer_checks)
    {
      refuse_visibility_changes = true;
      return true;
    }
1755 1756 1757 1758 1759

  /* If target is defined and not extern, we know it will be output and thus
     it will bind to non-NULL.
     Play safe for flag_delete_null_pointer_checks where weak definition maye
     be re-defined by NULL.  */
1760 1761 1762 1763 1764 1765 1766
  if (definition && !DECL_EXTERNAL (decl)
      && (flag_delete_null_pointer_checks || !DECL_WEAK (decl)))
    {
      if (!DECL_WEAK (decl))
        refuse_visibility_changes = true;
      return true;
    }
1767 1768

  /* As the last resort, check the resolution info.  */
1769 1770
  if (resolution != LDPR_UNKNOWN
      && resolution != LDPR_UNDEF
1771 1772 1773 1774
      && flag_delete_null_pointer_checks)
    return true;
  return false;
}
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861

/* Return 0 if symbol is known to have different address than S2,
   Return 1 if symbol is known to have same address as S2,
   return 2 otherwise.   */
int
symtab_node::equal_address_to (symtab_node *s2)
{
  enum availability avail1, avail2;

  /* A Shortcut: equivalent symbols are always equivalent.  */
  if (this == s2)
    return 1;

  /* For non-interposable aliases, lookup and compare their actual definitions.
     Also check if the symbol needs to bind to given definition.  */
  symtab_node *rs1 = ultimate_alias_target (&avail1);
  symtab_node *rs2 = s2->ultimate_alias_target (&avail2);
  bool binds_local1 = rs1->analyzed && decl_binds_to_current_def_p (this->decl);
  bool binds_local2 = rs2->analyzed && decl_binds_to_current_def_p (s2->decl);
  bool really_binds_local1 = binds_local1;
  bool really_binds_local2 = binds_local2;

  /* Addresses of vtables and virtual functions can not be used by user
     code and are used only within speculation.  In this case we may make
     symbol equivalent to its alias even if interposition may break this
     rule.  Doing so will allow us to turn speculative inlining into
     non-speculative more agressively.  */
  if (DECL_VIRTUAL_P (this->decl) && avail1 >= AVAIL_AVAILABLE)
    binds_local1 = true;
  if (DECL_VIRTUAL_P (s2->decl) && avail2 >= AVAIL_AVAILABLE)
    binds_local2 = true;

  /* If both definitions are available we know that even if they are bound
     to other unit they must be defined same way and therefore we can use
     equivalence test.  */
  if (rs1 != rs2 && avail1 >= AVAIL_AVAILABLE && avail2 >= AVAIL_AVAILABLE)
    binds_local1 = binds_local2 = true;

  if ((binds_local1 ? rs1 : this)
       == (binds_local2 ? rs2 : s2))
    {
      /* We made use of the fact that alias is not weak.  */
      if (binds_local1 && rs1 != this)
        refuse_visibility_changes = true;
      if (binds_local2 && rs2 != s2)
        s2->refuse_visibility_changes = true;
      return 1;
    }

  /* If both symbols may resolve to NULL, we can not really prove them different.  */
  if (!nonzero_address () && !s2->nonzero_address ())
    return 2;

  /* Except for NULL, functions and variables never overlap.  */
  if (TREE_CODE (decl) != TREE_CODE (s2->decl))
    return 0;

  /* If one of the symbols is unresolved alias, punt.  */
  if (rs1->alias || rs2->alias)
    return 2;

  /* If we have a non-interposale definition of at least one of the symbols
     and the other symbol is different, we know other unit can not interpose
     it to the first symbol; all aliases of the definition needs to be 
     present in the current unit.  */
  if (((really_binds_local1 || really_binds_local2)
      /* If we have both definitions and they are different, we know they
	 will be different even in units they binds to.  */
       || (binds_local1 && binds_local2))
      && rs1 != rs2)
    {
      /* We make use of the fact that one symbol is not alias of the other
	 and that the definition is non-interposable.  */
      refuse_visibility_changes = true;
      s2->refuse_visibility_changes = true;
      rs1->refuse_visibility_changes = true;
      rs2->refuse_visibility_changes = true;
      return 0;
    }

  /* TODO: Alias oracle basically assume that addresses of global variables
     are different unless they are declared as alias of one to another.
     We probably should be consistent and use this fact here, too, and update
     alias oracle to use this predicate.  */

  return 2;
}
1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882

/* Worker for call_for_symbol_and_aliases.  */

bool
symtab_node::call_for_symbol_and_aliases_1 (bool (*callback) (symtab_node *,
							      void *),
					    void *data,
					    bool include_overwritable)
{
  ipa_ref *ref;
  FOR_EACH_ALIAS (this, ref)
    {
      symtab_node *alias = ref->referring;
      if (include_overwritable
	  || alias->get_availability () > AVAIL_INTERPOSABLE)
	if (alias->call_for_symbol_and_aliases (callback, data,
					      include_overwritable))
	  return true;
    }
  return false;
}
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910

/* Return ture if address of N is possibly compared.  */

static bool
address_matters_1 (symtab_node *n, void *)
{
  struct ipa_ref *ref;

  if (!n->address_can_be_compared_p ())
    return false;
  if (n->externally_visible || n->force_output)
    return true;

  for (unsigned int i = 0; n->iterate_referring (i, ref); i++)
    if (ref->address_matters_p ())
      return true;
  return false;
}

/* Return true if symbol's address may possibly be compared to other
   symbol's address.  */

bool
symtab_node::address_matters_p ()
{
  gcc_assert (!alias);
  return call_for_symbol_and_aliases (address_matters_1, NULL, true);
}
1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926

/* Return ture if symbol's alignment may be increased.  */

bool
symtab_node::can_increase_alignment_p (void)
{
  symtab_node *target = ultimate_alias_target ();

  /* For now support only variables.  */
  if (TREE_CODE (decl) != VAR_DECL)
    return false;

  /* With -fno-toplevel-reorder we may have already output the constant.  */
  if (TREE_ASM_WRITTEN (target->decl))
    return false;

1927 1928 1929 1930 1931 1932 1933
  /* If target is already placed in an anchor, we can not touch its
     alignment.  */
  if (DECL_RTL_SET_P (target->decl)
      && MEM_P (DECL_RTL (target->decl))
      && SYMBOL_REF_HAS_BLOCK_INFO_P (XEXP (DECL_RTL (target->decl), 0)))
    return false;

1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013
  /* Constant pool entries may be shared.  */
  if (DECL_IN_CONSTANT_POOL (target->decl))
    return false;

  /* We cannot change alignment of symbols that may bind to symbols
     in other translation unit that may contain a definition with lower
     alignment.  */
  if (!decl_binds_to_current_def_p (decl))
    return false;

  /* When compiling partition, be sure the symbol is not output by other
     partition.  */
  if (flag_ltrans
      && (target->in_other_partition
	  || target->get_partitioning_class () == SYMBOL_DUPLICATE))
    return false;

  /* Do not override the alignment as specified by the ABI when the used
     attribute is set.  */
  if (DECL_PRESERVE_P (decl) || DECL_PRESERVE_P (target->decl))
    return false;

  /* Do not override explicit alignment set by the user when an explicit
     section name is also used.  This is a common idiom used by many
     software projects.  */
  if (DECL_SECTION_NAME (target->decl) != NULL && !target->implicit_section)
    return false;

  return true;
}

/* Worker for symtab_node::increase_alignment.  */

static bool
increase_alignment_1 (symtab_node *n, void *v)
{
  unsigned int align = (size_t)v;
  if (DECL_ALIGN (n->decl) < align
      && n->can_increase_alignment_p ())
    {
      DECL_ALIGN (n->decl) = align;
      DECL_USER_ALIGN (n->decl) = 1;
    }
  return false;
}

/* Increase alignment of THIS to ALIGN.  */

void
symtab_node::increase_alignment (unsigned int align)
{
  gcc_assert (can_increase_alignment_p () && align < MAX_OFILE_ALIGNMENT);
  ultimate_alias_target()->call_for_symbol_and_aliases (increase_alignment_1,
						        (void *)(size_t) align,
						        true);
  gcc_assert (DECL_ALIGN (decl) >= align);
}

/* Helper for symtab_node::definition_alignment.  */

static bool
get_alignment_1 (symtab_node *n, void *v)
{
  *((unsigned int *)v) = MAX (*((unsigned int *)v), DECL_ALIGN (n->decl));
  return false;
}

/* Return desired alignment of the definition.  This is NOT alignment useful
   to access THIS, because THIS may be interposable and DECL_ALIGN should
   be used instead.  It however must be guaranteed when output definition
   of THIS.  */

unsigned int
symtab_node::definition_alignment ()
{
  unsigned int align = 0;
  gcc_assert (!alias);
  call_for_symbol_and_aliases (get_alignment_1, &align, true);
  return align;
}