tree-ssa-alias.c 85 KB
Newer Older
1
/* Alias analysis for trees.
2
   Copyright (C) 2004-2016 Free Software Foundation, Inc.
3 4 5 6 7 8
   Contributed by Diego Novillo <dnovillo@redhat.com>

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
9
the Free Software Foundation; either version 3, or (at your option)
10 11 12 13 14 15 16 17
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
18 19
along with GCC; see the file COPYING3.  If not see
<http://www.gnu.org/licenses/>.  */
20 21 22 23

#include "config.h"
#include "system.h"
#include "coretypes.h"
24
#include "backend.h"
25 26
#include "target.h"
#include "rtl.h"
27
#include "tree.h"
28
#include "gimple.h"
29
#include "timevar.h"	/* for TV_ALIAS_STMT_WALK */
30
#include "ssa.h"
31 32
#include "cgraph.h"
#include "tree-pretty-print.h"
33
#include "alias.h"
34 35
#include "fold-const.h"

36
#include "langhooks.h"
37
#include "dumpfile.h"
38
#include "tree-eh.h"
39
#include "tree-dfa.h"
40
#include "ipa-reference.h"
Daniel Berlin committed
41

42
/* Broad overview of how alias analysis on gimple works:
Diego Novillo committed
43

44 45 46 47 48 49
   Statements clobbering or using memory are linked through the
   virtual operand factored use-def chain.  The virtual operand
   is unique per function, its symbol is accessible via gimple_vop (cfun).
   Virtual operands are used for efficiently walking memory statements
   in the gimple IL and are useful for things like value-numbering as
   a generation count for memory references.
50

51 52 53 54 55 56
   SSA_NAME pointers may have associated points-to information
   accessible via the SSA_NAME_PTR_INFO macro.  Flow-insensitive
   points-to information is (re-)computed by the TODO_rebuild_alias
   pass manager todo.  Points-to information is also used for more
   precise tracking of call-clobbered and call-used variables and
   related disambiguations.
57

58 59
   This file contains functions for disambiguating memory references,
   the so called alias-oracle and tools for walking of the gimple IL.
60

61
   The main alias-oracle entry-points are
62

63
   bool stmt_may_clobber_ref_p (gimple *, tree)
64

65 66
     This function queries if a statement may invalidate (parts of)
     the memory designated by the reference tree argument.
67

68
   bool ref_maybe_used_by_stmt_p (gimple *, tree)
69

70 71
     This function queries if a statement may need (parts of) the
     memory designated by the reference tree argument.
72

73 74 75
   There are variants of these functions that only handle the call
   part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
   Note that these do not disambiguate against a possible call lhs.
76

77
   bool refs_may_alias_p (tree, tree)
78

79
     This function tries to disambiguate two reference trees.
80

81
   bool ptr_deref_may_alias_global_p (tree)
82

83 84
     This function queries if dereferencing a pointer variable may
     alias global memory.
85

86 87 88
   More low-level disambiguators are available and documented in
   this file.  Low-level disambiguators dealing with points-to
   information are in tree-ssa-structalias.c.  */
89 90


91 92
/* Query statistics for the different low-level disambiguators.
   A high-level query may trigger multiple of them.  */
93

94 95 96 97 98 99 100 101
static struct {
  unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
  unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
  unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
  unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
  unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
  unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
} alias_stats;
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
void
dump_alias_stats (FILE *s)
{
  fprintf (s, "\nAlias oracle query stats:\n");
  fprintf (s, "  refs_may_alias_p: "
	   HOST_WIDE_INT_PRINT_DEC" disambiguations, "
	   HOST_WIDE_INT_PRINT_DEC" queries\n",
	   alias_stats.refs_may_alias_p_no_alias,
	   alias_stats.refs_may_alias_p_no_alias
	   + alias_stats.refs_may_alias_p_may_alias);
  fprintf (s, "  ref_maybe_used_by_call_p: "
	   HOST_WIDE_INT_PRINT_DEC" disambiguations, "
	   HOST_WIDE_INT_PRINT_DEC" queries\n",
	   alias_stats.ref_maybe_used_by_call_p_no_alias,
	   alias_stats.refs_may_alias_p_no_alias
	   + alias_stats.ref_maybe_used_by_call_p_may_alias);
  fprintf (s, "  call_may_clobber_ref_p: "
	   HOST_WIDE_INT_PRINT_DEC" disambiguations, "
	   HOST_WIDE_INT_PRINT_DEC" queries\n",
	   alias_stats.call_may_clobber_ref_p_no_alias,
	   alias_stats.call_may_clobber_ref_p_no_alias
	   + alias_stats.call_may_clobber_ref_p_may_alias);
125
  dump_alias_stats_in_alias_c (s);
126 127 128 129
}


/* Return true, if dereferencing PTR may alias with a global variable.  */
130

131 132
bool
ptr_deref_may_alias_global_p (tree ptr)
133
{
134
  struct ptr_info_def *pi;
Diego Novillo committed
135

136 137 138 139
  /* If we end up with a pointer constant here that may point
     to global memory.  */
  if (TREE_CODE (ptr) != SSA_NAME)
    return true;
140

141
  pi = SSA_NAME_PTR_INFO (ptr);
142

143 144 145 146
  /* If we do not have points-to information for this variable,
     we have to punt.  */
  if (!pi)
    return true;
147

148
  /* ???  This does not use TBAA to prune globals ptr may not access.  */
149
  return pt_solution_includes_global (&pi->pt);
150 151
}

152 153 154
/* Return true if dereferencing PTR may alias DECL.
   The caller is responsible for applying TBAA to see if PTR
   may access DECL at all.  */
155

156 157
static bool
ptr_deref_may_alias_decl_p (tree ptr, tree decl)
158
{
159
  struct ptr_info_def *pi;
160

161 162 163 164 165 166 167 168 169 170 171 172
  /* Conversions are irrelevant for points-to information and
     data-dependence analysis can feed us those.  */
  STRIP_NOPS (ptr);

  /* Anything we do not explicilty handle aliases.  */
  if ((TREE_CODE (ptr) != SSA_NAME
       && TREE_CODE (ptr) != ADDR_EXPR
       && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
      || !POINTER_TYPE_P (TREE_TYPE (ptr))
      || (TREE_CODE (decl) != VAR_DECL
	  && TREE_CODE (decl) != PARM_DECL
	  && TREE_CODE (decl) != RESULT_DECL))
173
    return true;
174

175 176 177 178 179 180 181 182 183 184 185
  /* Disregard pointer offsetting.  */
  if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
    {
      do
	{
	  ptr = TREE_OPERAND (ptr, 0);
	}
      while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
      return ptr_deref_may_alias_decl_p (ptr, decl);
    }

186 187 188 189 190 191
  /* ADDR_EXPR pointers either just offset another pointer or directly
     specify the pointed-to set.  */
  if (TREE_CODE (ptr) == ADDR_EXPR)
    {
      tree base = get_base_address (TREE_OPERAND (ptr, 0));
      if (base
192 193
	  && (TREE_CODE (base) == MEM_REF
	      || TREE_CODE (base) == TARGET_MEM_REF))
194 195
	ptr = TREE_OPERAND (base, 0);
      else if (base
196
	       && DECL_P (base))
197
	return compare_base_decls (base, decl) != 0;
198 199 200 201 202 203 204
      else if (base
	       && CONSTANT_CLASS_P (base))
	return false;
      else
	return true;
    }

205 206 207
  /* Non-aliased variables can not be pointed to.  */
  if (!may_be_aliased (decl))
    return false;
208

209 210 211 212
  /* If we do not have useful points-to information for this pointer
     we cannot disambiguate anything else.  */
  pi = SSA_NAME_PTR_INFO (ptr);
  if (!pi)
213 214
    return true;

215
  return pt_solution_includes (&pi->pt, decl);
216
}
217

218 219 220
/* Return true if dereferenced PTR1 and PTR2 may alias.
   The caller is responsible for applying TBAA to see if accesses
   through PTR1 and PTR2 may conflict at all.  */
221

222
bool
223
ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
224
{
225
  struct ptr_info_def *pi1, *pi2;
226

227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
  /* Conversions are irrelevant for points-to information and
     data-dependence analysis can feed us those.  */
  STRIP_NOPS (ptr1);
  STRIP_NOPS (ptr2);

  /* Disregard pointer offsetting.  */
  if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
    {
      do
	{
	  ptr1 = TREE_OPERAND (ptr1, 0);
	}
      while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
      return ptr_derefs_may_alias_p (ptr1, ptr2);
    }
  if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
    {
      do
	{
	  ptr2 = TREE_OPERAND (ptr2, 0);
	}
      while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
      return ptr_derefs_may_alias_p (ptr1, ptr2);
    }
251

252 253 254 255 256 257
  /* ADDR_EXPR pointers either just offset another pointer or directly
     specify the pointed-to set.  */
  if (TREE_CODE (ptr1) == ADDR_EXPR)
    {
      tree base = get_base_address (TREE_OPERAND (ptr1, 0));
      if (base
258 259
	  && (TREE_CODE (base) == MEM_REF
	      || TREE_CODE (base) == TARGET_MEM_REF))
260
	return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
261
      else if (base
262
	       && DECL_P (base))
263 264 265 266 267 268 269 270
	return ptr_deref_may_alias_decl_p (ptr2, base);
      else
	return true;
    }
  if (TREE_CODE (ptr2) == ADDR_EXPR)
    {
      tree base = get_base_address (TREE_OPERAND (ptr2, 0));
      if (base
271 272
	  && (TREE_CODE (base) == MEM_REF
	      || TREE_CODE (base) == TARGET_MEM_REF))
273
	return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
274
      else if (base
275
	       && DECL_P (base))
276 277 278 279 280
	return ptr_deref_may_alias_decl_p (ptr1, base);
      else
	return true;
    }

281 282 283 284 285 286 287
  /* From here we require SSA name pointers.  Anything else aliases.  */
  if (TREE_CODE (ptr1) != SSA_NAME
      || TREE_CODE (ptr2) != SSA_NAME
      || !POINTER_TYPE_P (TREE_TYPE (ptr1))
      || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
    return true;

288 289 290 291 292
  /* We may end up with two empty points-to solutions for two same pointers.
     In this case we still want to say both pointers alias, so shortcut
     that here.  */
  if (ptr1 == ptr2)
    return true;
293

294 295 296 297 298 299
  /* If we do not have useful points-to information for either pointer
     we cannot disambiguate anything else.  */
  pi1 = SSA_NAME_PTR_INFO (ptr1);
  pi2 = SSA_NAME_PTR_INFO (ptr2);
  if (!pi1 || !pi2)
    return true;
300

301 302
  /* ???  This does not use TBAA to prune decls from the intersection
     that not both pointers may access.  */
303
  return pt_solutions_intersect (&pi1->pt, &pi2->pt);
304 305
}

306 307 308 309 310 311 312 313 314
/* Return true if dereferencing PTR may alias *REF.
   The caller is responsible for applying TBAA to see if PTR
   may access *REF at all.  */

static bool
ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
{
  tree base = ao_ref_base (ref);

315 316
  if (TREE_CODE (base) == MEM_REF
      || TREE_CODE (base) == TARGET_MEM_REF)
317
    return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
318
  else if (DECL_P (base))
319 320 321 322 323
    return ptr_deref_may_alias_decl_p (ptr, base);

  return true;
}

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
/* Returns true if PTR1 and PTR2 compare unequal because of points-to.  */

bool
ptrs_compare_unequal (tree ptr1, tree ptr2)
{
  /* First resolve the pointers down to a SSA name pointer base or
     a VAR_DECL, PARM_DECL or RESULT_DECL.  This explicitely does
     not yet try to handle LABEL_DECLs, FUNCTION_DECLs, CONST_DECLs
     or STRING_CSTs which needs points-to adjustments to track them
     in the points-to sets.  */
  tree obj1 = NULL_TREE;
  tree obj2 = NULL_TREE;
  if (TREE_CODE (ptr1) == ADDR_EXPR)
    {
      tree tem = get_base_address (TREE_OPERAND (ptr1, 0));
      if (! tem)
	return false;
      if (TREE_CODE (tem) == VAR_DECL
	  || TREE_CODE (tem) == PARM_DECL
	  || TREE_CODE (tem) == RESULT_DECL)
	obj1 = tem;
      else if (TREE_CODE (tem) == MEM_REF)
	ptr1 = TREE_OPERAND (tem, 0);
    }
  if (TREE_CODE (ptr2) == ADDR_EXPR)
    {
      tree tem = get_base_address (TREE_OPERAND (ptr2, 0));
      if (! tem)
	return false;
      if (TREE_CODE (tem) == VAR_DECL
	  || TREE_CODE (tem) == PARM_DECL
	  || TREE_CODE (tem) == RESULT_DECL)
	obj2 = tem;
      else if (TREE_CODE (tem) == MEM_REF)
	ptr2 = TREE_OPERAND (tem, 0);
    }

  if (obj1 && obj2)
    /* Other code handles this correctly, no need to duplicate it here.  */;
  else if (obj1 && TREE_CODE (ptr2) == SSA_NAME)
    {
      struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr2);
366 367 368 369
      /* We may not use restrict to optimize pointer comparisons.
         See PR71062.  So we have to assume that restrict-pointed-to
	 may be in fact obj1.  */
      if (!pi || pi->pt.vars_contains_restrict)
370 371 372 373 374 375
	return false;
      return !pt_solution_includes (&pi->pt, obj1);
    }
  else if (TREE_CODE (ptr1) == SSA_NAME && obj2)
    {
      struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr1);
376
      if (!pi || pi->pt.vars_contains_restrict)
377 378 379 380 381 382 383 384 385 386
	return false;
      return !pt_solution_includes (&pi->pt, obj2);
    }

  /* ???  We'd like to handle ptr1 != NULL and ptr1 != ptr2
     but those require pt.null to be conservatively correct.  */

  return false;
}

387
/* Returns whether reference REF to BASE may refer to global memory.  */
388

389 390
static bool
ref_may_alias_global_p_1 (tree base)
391 392 393 394 395 396 397 398 399
{
  if (DECL_P (base))
    return is_global_var (base);
  else if (TREE_CODE (base) == MEM_REF
	   || TREE_CODE (base) == TARGET_MEM_REF)
    return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
  return true;
}

400 401 402 403 404 405 406 407 408 409 410 411 412 413
bool
ref_may_alias_global_p (ao_ref *ref)
{
  tree base = ao_ref_base (ref);
  return ref_may_alias_global_p_1 (base);
}

bool
ref_may_alias_global_p (tree ref)
{
  tree base = get_base_address (ref);
  return ref_may_alias_global_p_1 (base);
}

414 415 416
/* Return true whether STMT may clobber global memory.  */

bool
417
stmt_may_clobber_global_p (gimple *stmt)
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
{
  tree lhs;

  if (!gimple_vdef (stmt))
    return false;

  /* ???  We can ask the oracle whether an artificial pointer
     dereference with a pointer with points-to information covering
     all global memory (what about non-address taken memory?) maybe
     clobbered by this call.  As there is at the moment no convenient
     way of doing that without generating garbage do some manual
     checking instead.
     ???  We could make a NULL ao_ref argument to the various
     predicates special, meaning any global memory.  */

  switch (gimple_code (stmt))
    {
    case GIMPLE_ASSIGN:
      lhs = gimple_assign_lhs (stmt);
      return (TREE_CODE (lhs) != SSA_NAME
	      && ref_may_alias_global_p (lhs));
    case GIMPLE_CALL:
      return true;
    default:
      return true;
    }
}

446 447 448 449 450 451

/* Dump alias information on FILE.  */

void
dump_alias_info (FILE *file)
{
452
  unsigned i;
453
  const char *funcname
454
    = lang_hooks.decl_printable_name (current_function_decl, 2);
Daniel Berlin committed
455
  tree var;
456

457
  fprintf (file, "\n\nAlias information for %s\n\n", funcname);
458 459

  fprintf (file, "Aliased symbols\n\n");
H.J. Lu committed
460

461
  FOR_EACH_LOCAL_DECL (cfun, i, var)
462 463 464 465 466
    {
      if (may_be_aliased (var))
	dump_variable (file, var);
    }

467 468 469 470 471 472
  fprintf (file, "\nCall clobber information\n");

  fprintf (file, "\nESCAPED");
  dump_points_to_solution (file, &cfun->gimple_df->escaped);

  fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
473 474 475 476

  for (i = 1; i < num_ssa_names; i++)
    {
      tree ptr = ssa_name (i);
477
      struct ptr_info_def *pi;
H.J. Lu committed
478

479
      if (ptr == NULL_TREE
480
	  || !POINTER_TYPE_P (TREE_TYPE (ptr))
481
	  || SSA_NAME_IN_FREE_LIST (ptr))
482 483 484
	continue;

      pi = SSA_NAME_PTR_INFO (ptr);
485
      if (pi)
486 487
	dump_points_to_info_for (file, ptr);
    }
488 489 490 491 492 493 494

  fprintf (file, "\n");
}


/* Dump alias information on stderr.  */

495
DEBUG_FUNCTION void
496 497 498 499 500 501
debug_alias_info (void)
{
  dump_alias_info (stderr);
}


502
/* Dump the points-to set *PT into FILE.  */
503

504
void
505
dump_points_to_solution (FILE *file, struct pt_solution *pt)
506
{
507 508
  if (pt->anything)
    fprintf (file, ", points-to anything");
509

510 511
  if (pt->nonlocal)
    fprintf (file, ", points-to non-local");
512

513 514 515
  if (pt->escaped)
    fprintf (file, ", points-to escaped");

516 517 518
  if (pt->ipa_escaped)
    fprintf (file, ", points-to unit escaped");

519 520 521 522
  if (pt->null)
    fprintf (file, ", points-to NULL");

  if (pt->vars)
523
    {
524 525
      fprintf (file, ", points-to vars: ");
      dump_decl_set (file, pt->vars);
526
      if (pt->vars_contains_nonlocal
527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551
	  || pt->vars_contains_escaped
	  || pt->vars_contains_escaped_heap
	  || pt->vars_contains_restrict)
	{
	  const char *comma = "";
	  fprintf (file, " (");
	  if (pt->vars_contains_nonlocal)
	    {
	      fprintf (file, "nonlocal");
	      comma = ", ";
	    }
	  if (pt->vars_contains_escaped)
	    {
	      fprintf (file, "%sescaped", comma);
	      comma = ", ";
	    }
	  if (pt->vars_contains_escaped_heap)
	    {
	      fprintf (file, "%sescaped heap", comma);
	      comma = ", ";
	    }
	  if (pt->vars_contains_restrict)
	    fprintf (file, "%srestrict", comma);
	  fprintf (file, ")");
	}
552 553
    }
}
554

555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573

/* Unified dump function for pt_solution.  */

DEBUG_FUNCTION void
debug (pt_solution &ref)
{
  dump_points_to_solution (stderr, &ref);
}

DEBUG_FUNCTION void
debug (pt_solution *ptr)
{
  if (ptr)
    debug (*ptr);
  else
    fprintf (stderr, "<nil>\n");
}


574
/* Dump points-to information for SSA_NAME PTR into FILE.  */
575

576 577 578 579
void
dump_points_to_info_for (FILE *file, tree ptr)
{
  struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
580

581
  print_generic_expr (file, ptr, dump_flags);
582

583 584 585 586
  if (pi)
    dump_points_to_solution (file, &pi->pt);
  else
    fprintf (file, ", points-to anything");
587 588 589 590 591

  fprintf (file, "\n");
}


592 593
/* Dump points-to information for VAR into stderr.  */

594
DEBUG_FUNCTION void
595 596 597 598 599
debug_points_to_info_for (tree var)
{
  dump_points_to_info_for (stderr, var);
}

600 601 602 603 604 605 606 607 608 609 610 611 612

/* Initializes the alias-oracle reference representation *R from REF.  */

void
ao_ref_init (ao_ref *r, tree ref)
{
  r->ref = ref;
  r->base = NULL_TREE;
  r->offset = 0;
  r->size = -1;
  r->max_size = -1;
  r->ref_alias_set = -1;
  r->base_alias_set = -1;
613
  r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
614 615 616 617 618 619 620
}

/* Returns the base object of the memory reference *REF.  */

tree
ao_ref_base (ao_ref *ref)
{
621 622
  bool reverse;

623 624 625
  if (ref->base)
    return ref->base;
  ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
626
				       &ref->max_size, &reverse);
627 628 629 630 631
  return ref->base;
}

/* Returns the base object alias set of the memory reference *REF.  */

632
alias_set_type
633 634
ao_ref_base_alias_set (ao_ref *ref)
{
635
  tree base_ref;
636 637
  if (ref->base_alias_set != -1)
    return ref->base_alias_set;
638 639 640 641 642 643
  if (!ref->ref)
    return 0;
  base_ref = ref->ref;
  while (handled_component_p (base_ref))
    base_ref = TREE_OPERAND (base_ref, 0);
  ref->base_alias_set = get_alias_set (base_ref);
644 645 646 647 648 649 650 651 652 653 654 655 656 657
  return ref->base_alias_set;
}

/* Returns the reference alias set of the memory reference *REF.  */

alias_set_type
ao_ref_alias_set (ao_ref *ref)
{
  if (ref->ref_alias_set != -1)
    return ref->ref_alias_set;
  ref->ref_alias_set = get_alias_set (ref->ref);
  return ref->ref_alias_set;
}

658
/* Init an alias-oracle reference representation from a gimple pointer
659
   PTR and a gimple size SIZE in bytes.  If SIZE is NULL_TREE then the
660 661 662 663 664 665
   size is assumed to be unknown.  The access is assumed to be only
   to or after of the pointer target, not before it.  */

void
ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
{
666
  HOST_WIDE_INT t, size_hwi, extra_offset = 0;
667
  ref->ref = NULL_TREE;
668 669
  if (TREE_CODE (ptr) == SSA_NAME)
    {
670
      gimple *stmt = SSA_NAME_DEF_STMT (ptr);
671 672 673
      if (gimple_assign_single_p (stmt)
	  && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
	ptr = gimple_assign_rhs1 (stmt);
674 675
      else if (is_gimple_assign (stmt)
	       && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
676
	       && TREE_CODE (gimple_assign_rhs2 (stmt)) == INTEGER_CST)
677 678
	{
	  ptr = gimple_assign_rhs1 (stmt);
679 680
	  extra_offset = BITS_PER_UNIT
			 * int_cst_value (gimple_assign_rhs2 (stmt));
681
	}
682 683
    }

684
  if (TREE_CODE (ptr) == ADDR_EXPR)
685 686 687 688 689 690 691 692 693 694 695
    {
      ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
      if (ref->base)
	ref->offset = BITS_PER_UNIT * t;
      else
	{
	  size = NULL_TREE;
	  ref->offset = 0;
	  ref->base = get_base_address (TREE_OPERAND (ptr, 0));
	}
    }
696 697
  else
    {
698
      ref->base = build2 (MEM_REF, char_type_node,
699
			  ptr, null_pointer_node);
700 701
      ref->offset = 0;
    }
702
  ref->offset += extra_offset;
703
  if (size
704
      && tree_fits_shwi_p (size)
705 706
      && (size_hwi = tree_to_shwi (size)) <= HOST_WIDE_INT_MAX / BITS_PER_UNIT)
    ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
707 708 709 710
  else
    ref->max_size = ref->size = -1;
  ref->ref_alias_set = 0;
  ref->base_alias_set = 0;
711
  ref->volatile_p = false;
712 713
}

714 715 716
/* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
   purpose of TBAA.  Return 0 if they are distinct and -1 if we cannot
   decide.  */
717

718 719 720 721 722
static inline int
same_type_for_tbaa (tree type1, tree type2)
{
  type1 = TYPE_MAIN_VARIANT (type1);
  type2 = TYPE_MAIN_VARIANT (type2);
723

724 725 726 727 728 729 730 731 732
  /* If we would have to do structural comparison bail out.  */
  if (TYPE_STRUCTURAL_EQUALITY_P (type1)
      || TYPE_STRUCTURAL_EQUALITY_P (type2))
    return -1;

  /* Compare the canonical types.  */
  if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
    return 1;

733
  /* ??? Array types are not properly unified in all cases as we have
734 735 736 737 738 739
     spurious changes in the index types for example.  Removing this
     causes all sorts of problems with the Fortran frontend.  */
  if (TREE_CODE (type1) == ARRAY_TYPE
      && TREE_CODE (type2) == ARRAY_TYPE)
    return -1;

740 741 742 743 744 745 746 747 748
  /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
     object of one of its constrained subtypes, e.g. when a function with an
     unconstrained parameter passed by reference is called on an object and
     inlined.  But, even in the case of a fixed size, type and subtypes are
     not equivalent enough as to share the same TYPE_CANONICAL, since this
     would mean that conversions between them are useless, whereas they are
     not (e.g. type and subtypes can have different modes).  So, in the end,
     they are only guaranteed to have the same alias set.  */
  if (get_alias_set (type1) == get_alias_set (type2))
749 750
    return -1;

751 752 753 754 755 756
  /* The types are known to be not equal.  */
  return 0;
}

/* Determine if the two component references REF1 and REF2 which are
   based on access types TYPE1 and TYPE2 and of which at least one is based
757 758 759 760
   on an indirect reference may alias.  REF2 is the only one that can
   be a decl in which case REF2_IS_DECL is true.
   REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
   are the respective alias sets.  */
761 762

static bool
763
aliasing_component_refs_p (tree ref1,
764 765
			   alias_set_type ref1_alias_set,
			   alias_set_type base1_alias_set,
766
			   HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
767
			   tree ref2,
768 769 770 771
			   alias_set_type ref2_alias_set,
			   alias_set_type base2_alias_set,
			   HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
			   bool ref2_is_decl)
772 773 774 775 776 777 778
{
  /* If one reference is a component references through pointers try to find a
     common base and apply offset based disambiguation.  This handles
     for example
       struct A { int i; int j; } *q;
       struct B { struct A a; int k; } *p;
     disambiguating q->i and p->a.j.  */
779 780
  tree base1, base2;
  tree type1, type2;
781 782 783
  tree *refp;
  int same_p;

784 785 786 787 788 789 790 791 792 793
  /* Choose bases and base types to search for.  */
  base1 = ref1;
  while (handled_component_p (base1))
    base1 = TREE_OPERAND (base1, 0);
  type1 = TREE_TYPE (base1);
  base2 = ref2;
  while (handled_component_p (base2))
    base2 = TREE_OPERAND (base2, 0);
  type2 = TREE_TYPE (base2);

794 795
  /* Now search for the type1 in the access path of ref2.  This
     would be a common base for doing offset based disambiguation on.  */
796
  refp = &ref2;
797 798 799 800 801 802 803 804 805 806
  while (handled_component_p (*refp)
	 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
    refp = &TREE_OPERAND (*refp, 0);
  same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
  /* If we couldn't compare types we have to bail out.  */
  if (same_p == -1)
    return true;
  else if (same_p == 1)
    {
      HOST_WIDE_INT offadj, sztmp, msztmp;
807 808
      bool reverse;
      get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp, &reverse);
809
      offset2 -= offadj;
810
      get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp, &reverse);
811
      offset1 -= offadj;
812 813 814
      return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
    }
  /* If we didn't find a common base, try the other way around.  */
815
  refp = &ref1;
816 817 818 819 820 821 822 823 824 825
  while (handled_component_p (*refp)
	 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
    refp = &TREE_OPERAND (*refp, 0);
  same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
  /* If we couldn't compare types we have to bail out.  */
  if (same_p == -1)
    return true;
  else if (same_p == 1)
    {
      HOST_WIDE_INT offadj, sztmp, msztmp;
826 827
      bool reverse;
      get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp, &reverse);
828
      offset1 -= offadj;
829
      get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp, &reverse);
830
      offset2 -= offadj;
831 832
      return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
    }
833

834
  /* If we have two type access paths B1.path1 and B2.path2 they may
835 836 837 838 839 840 841 842 843 844 845 846
     only alias if either B1 is in B2.path2 or B2 is in B1.path1.
     But we can still have a path that goes B1.path1...B2.path2 with
     a part that we do not see.  So we can only disambiguate now
     if there is no B2 in the tail of path1 and no B1 on the
     tail of path2.  */
  if (base1_alias_set == ref2_alias_set
      || alias_set_subset_of (base1_alias_set, ref2_alias_set))
    return true;
  /* If this is ptr vs. decl then we know there is no ptr ... decl path.  */
  if (!ref2_is_decl)
    return (base2_alias_set == ref1_alias_set
	    || alias_set_subset_of (base2_alias_set, ref1_alias_set));
847
  return false;
848 849
}

850 851 852 853 854 855
/* Return true if we can determine that component references REF1 and REF2,
   that are within a common DECL, cannot overlap.  */

static bool
nonoverlapping_component_refs_of_decl_p (tree ref1, tree ref2)
{
856 857
  auto_vec<tree, 16> component_refs1;
  auto_vec<tree, 16> component_refs2;
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884

  /* Create the stack of handled components for REF1.  */
  while (handled_component_p (ref1))
    {
      component_refs1.safe_push (ref1);
      ref1 = TREE_OPERAND (ref1, 0);
    }
  if (TREE_CODE (ref1) == MEM_REF)
    {
      if (!integer_zerop (TREE_OPERAND (ref1, 1)))
	goto may_overlap;
      ref1 = TREE_OPERAND (TREE_OPERAND (ref1, 0), 0);
    }

  /* Create the stack of handled components for REF2.  */
  while (handled_component_p (ref2))
    {
      component_refs2.safe_push (ref2);
      ref2 = TREE_OPERAND (ref2, 0);
    }
  if (TREE_CODE (ref2) == MEM_REF)
    {
      if (!integer_zerop (TREE_OPERAND (ref2, 1)))
	goto may_overlap;
      ref2 = TREE_OPERAND (TREE_OPERAND (ref2, 0), 0);
    }

885 886 887 888
  /* Bases must be either same or uncomparable.  */
  gcc_checking_assert (ref1 == ref2
		       || (DECL_P (ref1) && DECL_P (ref2)
			   && compare_base_decls (ref1, ref2) != 0));
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924

  /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
     rank.  This is sufficient because we start from the same DECL and you
     cannot reference several fields at a time with COMPONENT_REFs (unlike
     with ARRAY_RANGE_REFs for arrays) so you always need the same number
     of them to access a sub-component, unless you're in a union, in which
     case the return value will precisely be false.  */
  while (true)
    {
      do
	{
	  if (component_refs1.is_empty ())
	    goto may_overlap;
	  ref1 = component_refs1.pop ();
	}
      while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));

      do
	{
	  if (component_refs2.is_empty ())
	     goto may_overlap;
	  ref2 = component_refs2.pop ();
	}
      while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));

      /* Beware of BIT_FIELD_REF.  */
      if (TREE_CODE (ref1) != COMPONENT_REF
	  || TREE_CODE (ref2) != COMPONENT_REF)
	goto may_overlap;

      tree field1 = TREE_OPERAND (ref1, 1);
      tree field2 = TREE_OPERAND (ref2, 1);

      /* ??? We cannot simply use the type of operand #0 of the refs here
	 as the Fortran compiler smuggles type punning into COMPONENT_REFs
	 for common blocks instead of using unions like everyone else.  */
925 926
      tree type1 = DECL_CONTEXT (field1);
      tree type2 = DECL_CONTEXT (field2);
927 928 929 930 931 932 933 934 935

      /* We cannot disambiguate fields in a union or qualified union.  */
      if (type1 != type2 || TREE_CODE (type1) != RECORD_TYPE)
	 goto may_overlap;

      if (field1 != field2)
	{
	  component_refs1.release ();
	  component_refs2.release ();
936 937 938 939 940 941 942 943 944 945
	  /* A field and its representative need to be considered the
	     same.  */
	  if (DECL_BIT_FIELD_REPRESENTATIVE (field1) == field2
	      || DECL_BIT_FIELD_REPRESENTATIVE (field2) == field1)
	    return false;
	  /* Different fields of the same record type cannot overlap.
	     ??? Bitfields can overlap at RTL level so punt on them.  */
	  if (DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2))
	    return false;
	  return true;
946 947 948 949 950 951 952 953 954
	}
    }

may_overlap:
  component_refs1.release ();
  component_refs2.release ();
  return false;
}

955 956 957 958 959 960 961 962
/* qsort compare function to sort FIELD_DECLs after their
   DECL_FIELD_CONTEXT TYPE_UID.  */

static inline int
ncr_compar (const void *field1_, const void *field2_)
{
  const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
  const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
963 964
  unsigned int uid1 = TYPE_UID (DECL_FIELD_CONTEXT (field1));
  unsigned int uid2 = TYPE_UID (DECL_FIELD_CONTEXT (field2));
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987
  if (uid1 < uid2)
    return -1;
  else if (uid1 > uid2)
    return 1;
  return 0;
}

/* Return true if we can determine that the fields referenced cannot
   overlap for any pair of objects.  */

static bool
nonoverlapping_component_refs_p (const_tree x, const_tree y)
{
  if (!flag_strict_aliasing
      || !x || !y
      || TREE_CODE (x) != COMPONENT_REF
      || TREE_CODE (y) != COMPONENT_REF)
    return false;

  auto_vec<const_tree, 16> fieldsx;
  while (TREE_CODE (x) == COMPONENT_REF)
    {
      tree field = TREE_OPERAND (x, 1);
988
      tree type = DECL_FIELD_CONTEXT (field);
989 990 991 992 993 994 995 996 997 998
      if (TREE_CODE (type) == RECORD_TYPE)
	fieldsx.safe_push (field);
      x = TREE_OPERAND (x, 0);
    }
  if (fieldsx.length () == 0)
    return false;
  auto_vec<const_tree, 16> fieldsy;
  while (TREE_CODE (y) == COMPONENT_REF)
    {
      tree field = TREE_OPERAND (y, 1);
999
      tree type = DECL_FIELD_CONTEXT (field);
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
      if (TREE_CODE (type) == RECORD_TYPE)
	fieldsy.safe_push (TREE_OPERAND (y, 1));
      y = TREE_OPERAND (y, 0);
    }
  if (fieldsy.length () == 0)
    return false;

  /* Most common case first.  */
  if (fieldsx.length () == 1
      && fieldsy.length () == 1)
1010 1011
    return ((DECL_FIELD_CONTEXT (fieldsx[0])
	     == DECL_FIELD_CONTEXT (fieldsy[0]))
1012 1013 1014 1015 1016 1017
	    && fieldsx[0] != fieldsy[0]
	    && !(DECL_BIT_FIELD (fieldsx[0]) && DECL_BIT_FIELD (fieldsy[0])));

  if (fieldsx.length () == 2)
    {
      if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
1018
	std::swap (fieldsx[0], fieldsx[1]);
1019 1020 1021 1022 1023 1024 1025
    }
  else
    fieldsx.qsort (ncr_compar);

  if (fieldsy.length () == 2)
    {
      if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
1026
	std::swap (fieldsy[0], fieldsy[1]);
1027 1028 1029 1030 1031 1032 1033 1034 1035
    }
  else
    fieldsy.qsort (ncr_compar);

  unsigned i = 0, j = 0;
  do
    {
      const_tree fieldx = fieldsx[i];
      const_tree fieldy = fieldsy[j];
1036 1037
      tree typex = DECL_FIELD_CONTEXT (fieldx);
      tree typey = DECL_FIELD_CONTEXT (fieldy);
1038 1039 1040
      if (typex == typey)
	{
	  /* We're left with accessing different fields of a structure,
1041
	     no possible overlap.  */
1042
	  if (fieldx != fieldy)
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
	    {
	      /* A field and its representative need to be considered the
		 same.  */
	      if (DECL_BIT_FIELD_REPRESENTATIVE (fieldx) == fieldy
		  || DECL_BIT_FIELD_REPRESENTATIVE (fieldy) == fieldx)
		return false;
	      /* Different fields of the same record type cannot overlap.
		 ??? Bitfields can overlap at RTL level so punt on them.  */
	      if (DECL_BIT_FIELD (fieldx) && DECL_BIT_FIELD (fieldy))
		return false;
	      return true;
	    }
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074
	}
      if (TYPE_UID (typex) < TYPE_UID (typey))
	{
	  i++;
	  if (i == fieldsx.length ())
	    break;
	}
      else
	{
	  j++;
	  if (j == fieldsy.length ())
	    break;
	}
    }
  while (1);

  return false;
}


1075
/* Return true if two memory references based on the variables BASE1
1076
   and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1077 1078
   [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  REF1 and REF2
   if non-NULL are the complete memory reference trees.  */
1079 1080

static bool
1081
decl_refs_may_alias_p (tree ref1, tree base1,
1082
		       HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1083
		       tree ref2, tree base2,
1084
		       HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
1085
{
1086
  gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1087

1088
  /* If both references are based on different variables, they cannot alias.  */
1089
  if (compare_base_decls (base1, base2) == 0)
1090
    return false;
1091

1092 1093
  /* If both references are based on the same variable, they cannot alias if
     the accesses do not overlap.  */
1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
  if (!ranges_overlap_p (offset1, max_size1, offset2, max_size2))
    return false;

  /* For components with variable position, the above test isn't sufficient,
     so we disambiguate component references manually.  */
  if (ref1 && ref2
      && handled_component_p (ref1) && handled_component_p (ref2)
      && nonoverlapping_component_refs_of_decl_p (ref1, ref2))
    return false;

  return true;     
1105 1106 1107
}

/* Return true if an indirect reference based on *PTR1 constrained
1108 1109
   to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
   constrained to [OFFSET2, OFFSET2 + MAX_SIZE2).  *PTR1 and BASE2 have
1110 1111 1112 1113 1114
   the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
   in which case they are computed on-demand.  REF1 and REF2
   if non-NULL are the complete memory reference trees.  */

static bool
1115 1116 1117
indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
			       HOST_WIDE_INT offset1,
			       HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
1118
			       alias_set_type ref1_alias_set,
1119
			       alias_set_type base1_alias_set,
1120
			       tree ref2 ATTRIBUTE_UNUSED, tree base2,
1121
			       HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1122
			       alias_set_type ref2_alias_set,
1123
			       alias_set_type base2_alias_set, bool tbaa_p)
1124
{
1125
  tree ptr1;
1126
  tree ptrtype1, dbase2;
1127
  HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
1128
  HOST_WIDE_INT doffset1, doffset2;
1129 1130 1131 1132

  gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
			|| TREE_CODE (base1) == TARGET_MEM_REF)
		       && DECL_P (base2));
1133

1134
  ptr1 = TREE_OPERAND (base1, 0);
1135

1136 1137
  /* The offset embedded in MEM_REFs can be negative.  Bias them
     so that the resulting offset adjustment is positive.  */
Kenneth Zadeck committed
1138
  offset_int moff = mem_ref_offset (base1);
1139
  moff <<= LOG2_BITS_PER_UNIT;
Kenneth Zadeck committed
1140 1141
  if (wi::neg_p (moff))
    offset2p += (-moff).to_short_addr ();
1142
  else
Kenneth Zadeck committed
1143
    offset1p += moff.to_short_addr ();
1144

1145 1146 1147 1148
  /* If only one reference is based on a variable, they cannot alias if
     the pointer access is beyond the extent of the variable access.
     (the pointer base cannot validly point to an offset less than zero
     of the variable).
1149 1150 1151
     ???  IVOPTs creates bases that do not honor this restriction,
     so do not apply this optimization for TARGET_MEM_REFs.  */
  if (TREE_CODE (base1) != TARGET_MEM_REF
1152
      && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
1153
    return false;
1154
  /* They also cannot alias if the pointer may not point to the decl.  */
1155 1156 1157 1158
  if (!ptr_deref_may_alias_decl_p (ptr1, base2))
    return false;

  /* Disambiguations that rely on strict aliasing rules follow.  */
1159
  if (!flag_strict_aliasing || !tbaa_p)
1160 1161
    return true;

1162
  ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1163

1164 1165 1166 1167
  /* If the alias set for a pointer access is zero all bets are off.  */
  if (base1_alias_set == 0)
    return true;

1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
  /* When we are trying to disambiguate an access with a pointer dereference
     as base versus one with a decl as base we can use both the size
     of the decl and its dynamic type for extra disambiguation.
     ???  We do not know anything about the dynamic type of the decl
     other than that its alias-set contains base2_alias_set as a subset
     which does not help us here.  */
  /* As we know nothing useful about the dynamic type of the decl just
     use the usual conflict check rather than a subset test.
     ???  We could introduce -fvery-strict-aliasing when the language
     does not allow decls to have a dynamic type that differs from their
     static type.  Then we can check 
     !alias_set_subset_of (base1_alias_set, base2_alias_set) instead.  */
1180
  if (base1_alias_set != base2_alias_set
1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194
      && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
    return false;
  /* If the size of the access relevant for TBAA through the pointer
     is bigger than the size of the decl we can't possibly access the
     decl via that pointer.  */
  if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
      && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
      && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
      /* ???  This in turn may run afoul when a decl of type T which is
	 a member of union type U is accessed through a pointer to
	 type U and sizeof T is smaller than sizeof U.  */
      && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
      && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
      && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
1195 1196
    return false;

1197 1198 1199
  if (!ref2)
    return true;

1200
  /* If the decl is accessed via a MEM_REF, reconstruct the base
1201 1202 1203 1204 1205 1206 1207 1208 1209
     we can use for TBAA and an appropriately adjusted offset.  */
  dbase2 = ref2;
  while (handled_component_p (dbase2))
    dbase2 = TREE_OPERAND (dbase2, 0);
  doffset1 = offset1;
  doffset2 = offset2;
  if (TREE_CODE (dbase2) == MEM_REF
      || TREE_CODE (dbase2) == TARGET_MEM_REF)
    {
Kenneth Zadeck committed
1210
      offset_int moff = mem_ref_offset (dbase2);
1211
      moff <<= LOG2_BITS_PER_UNIT;
Kenneth Zadeck committed
1212 1213
      if (wi::neg_p (moff))
	doffset1 -= (-moff).to_short_addr ();
1214
      else
Kenneth Zadeck committed
1215
	doffset2 -= moff.to_short_addr ();
1216 1217 1218 1219
    }

  /* If either reference is view-converted, give up now.  */
  if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1220
      || same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (base2)) != 1)
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
    return true;

  /* If both references are through the same type, they do not alias
     if the accesses do not overlap.  This does extra disambiguation
     for mixed/pointer accesses but requires strict aliasing.
     For MEM_REFs we require that the component-ref offset we computed
     is relative to the start of the type which we ensure by
     comparing rvalue and access type and disregarding the constant
     pointer offset.  */
  if ((TREE_CODE (base1) != TARGET_MEM_REF
       || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
      && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
    return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);

1235 1236 1237 1238
  if (ref1 && ref2
      && nonoverlapping_component_refs_p (ref1, ref2))
    return false;

1239 1240
  /* Do access-path based disambiguation.  */
  if (ref1 && ref2
1241
      && (handled_component_p (ref1) || handled_component_p (ref2)))
1242
    return aliasing_component_refs_p (ref1,
1243
				      ref1_alias_set, base1_alias_set,
1244
				      offset1, max_size1,
1245
				      ref2,
1246 1247
				      ref2_alias_set, base2_alias_set,
				      offset2, max_size2, true);
1248 1249 1250 1251 1252

  return true;
}

/* Return true if two indirect references based on *PTR1
1253 1254
   and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
   [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  *PTR1 and *PTR2 have
1255 1256 1257 1258 1259
   the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
   in which case they are computed on-demand.  REF1 and REF2
   if non-NULL are the complete memory reference trees. */

static bool
1260
indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1261
			   HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
1262
			   alias_set_type ref1_alias_set,
1263
			   alias_set_type base1_alias_set,
1264
			   tree ref2 ATTRIBUTE_UNUSED, tree base2,
1265
			   HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
1266
			   alias_set_type ref2_alias_set,
1267
			   alias_set_type base2_alias_set, bool tbaa_p)
1268
{
1269 1270
  tree ptr1;
  tree ptr2;
1271 1272
  tree ptrtype1, ptrtype2;

1273 1274 1275 1276 1277
  gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
			|| TREE_CODE (base1) == TARGET_MEM_REF)
		       && (TREE_CODE (base2) == MEM_REF
			   || TREE_CODE (base2) == TARGET_MEM_REF));

1278 1279
  ptr1 = TREE_OPERAND (base1, 0);
  ptr2 = TREE_OPERAND (base2, 0);
1280

1281 1282 1283
  /* If both bases are based on pointers they cannot alias if they may not
     point to the same memory object or if they point to the same object
     and the accesses do not overlap.  */
1284
  if ((!cfun || gimple_in_ssa_p (cfun))
1285 1286
      && operand_equal_p (ptr1, ptr2, 0)
      && (((TREE_CODE (base1) != TARGET_MEM_REF
1287
	    || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1288
	   && (TREE_CODE (base2) != TARGET_MEM_REF
1289
	       || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1290 1291 1292 1293 1294 1295
	  || (TREE_CODE (base1) == TARGET_MEM_REF
	      && TREE_CODE (base2) == TARGET_MEM_REF
	      && (TMR_STEP (base1) == TMR_STEP (base2)
		  || (TMR_STEP (base1) && TMR_STEP (base2)
		      && operand_equal_p (TMR_STEP (base1),
					  TMR_STEP (base2), 0)))
1296 1297 1298 1299 1300 1301 1302 1303
	      && (TMR_INDEX (base1) == TMR_INDEX (base2)
		  || (TMR_INDEX (base1) && TMR_INDEX (base2)
		      && operand_equal_p (TMR_INDEX (base1),
					  TMR_INDEX (base2), 0)))
	      && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
		  || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
		      && operand_equal_p (TMR_INDEX2 (base1),
					  TMR_INDEX2 (base2), 0))))))
1304
    {
Kenneth Zadeck committed
1305
      offset_int moff;
1306 1307
      /* The offset embedded in MEM_REFs can be negative.  Bias them
	 so that the resulting offset adjustment is positive.  */
1308
      moff = mem_ref_offset (base1);
1309
      moff <<= LOG2_BITS_PER_UNIT;
Kenneth Zadeck committed
1310 1311
      if (wi::neg_p (moff))
	offset2 += (-moff).to_short_addr ();
1312
      else
Kenneth Zadeck committed
1313
	offset1 += moff.to_shwi ();
1314
      moff = mem_ref_offset (base2);
1315
      moff <<= LOG2_BITS_PER_UNIT;
Kenneth Zadeck committed
1316 1317
      if (wi::neg_p (moff))
	offset1 += (-moff).to_short_addr ();
1318
      else
Kenneth Zadeck committed
1319
	offset2 += moff.to_short_addr ();
1320 1321
      return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
    }
1322 1323 1324 1325
  if (!ptr_derefs_may_alias_p (ptr1, ptr2))
    return false;

  /* Disambiguations that rely on strict aliasing rules follow.  */
1326
  if (!flag_strict_aliasing || !tbaa_p)
1327 1328
    return true;

1329 1330
  ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
  ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1331

1332
  /* If the alias set for a pointer access is zero all bets are off.  */
1333 1334
  if (base1_alias_set == 0
      || base2_alias_set == 0)
1335 1336 1337 1338 1339
    return true;

  /* If both references are through the same type, they do not alias
     if the accesses do not overlap.  This does extra disambiguation
     for mixed/pointer accesses but requires strict aliasing.  */
1340 1341 1342 1343 1344 1345
  if ((TREE_CODE (base1) != TARGET_MEM_REF
       || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
      && (TREE_CODE (base2) != TARGET_MEM_REF
	  || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
      && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
      && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
1346 1347
      && same_type_for_tbaa (TREE_TYPE (ptrtype1),
			     TREE_TYPE (ptrtype2)) == 1)
1348 1349 1350 1351 1352 1353 1354
    return ranges_overlap_p (offset1, max_size1, offset2, max_size2);

  /* Do type-based disambiguation.  */
  if (base1_alias_set != base2_alias_set
      && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
    return false;

1355 1356 1357 1358 1359 1360 1361 1362 1363
  /* If either reference is view-converted, give up now.  */
  if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
      || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
    return true;

  if (ref1 && ref2
      && nonoverlapping_component_refs_p (ref1, ref2))
    return false;

1364 1365
  /* Do access-path based disambiguation.  */
  if (ref1 && ref2
1366
      && (handled_component_p (ref1) || handled_component_p (ref2)))
1367
    return aliasing_component_refs_p (ref1,
1368
				      ref1_alias_set, base1_alias_set,
1369
				      offset1, max_size1,
1370
				      ref2,
1371 1372
				      ref2_alias_set, base2_alias_set,
				      offset2, max_size2, false);
1373 1374 1375 1376 1377 1378

  return true;
}

/* Return true, if the two memory references REF1 and REF2 may alias.  */

1379
bool
1380
refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1381 1382 1383 1384 1385 1386
{
  tree base1, base2;
  HOST_WIDE_INT offset1 = 0, offset2 = 0;
  HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
  bool var1_p, var2_p, ind1_p, ind2_p;

1387
  gcc_checking_assert ((!ref1->ref
1388
			|| TREE_CODE (ref1->ref) == SSA_NAME
1389
			|| DECL_P (ref1->ref)
1390
			|| TREE_CODE (ref1->ref) == STRING_CST
1391
			|| handled_component_p (ref1->ref)
1392
			|| TREE_CODE (ref1->ref) == MEM_REF
1393 1394
			|| TREE_CODE (ref1->ref) == TARGET_MEM_REF)
		       && (!ref2->ref
1395
			   || TREE_CODE (ref2->ref) == SSA_NAME
1396
			   || DECL_P (ref2->ref)
1397
			   || TREE_CODE (ref2->ref) == STRING_CST
1398
			   || handled_component_p (ref2->ref)
1399
			   || TREE_CODE (ref2->ref) == MEM_REF
1400
			   || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1401 1402

  /* Decompose the references into their base objects and the access.  */
1403 1404 1405 1406 1407 1408
  base1 = ao_ref_base (ref1);
  offset1 = ref1->offset;
  max_size1 = ref1->max_size;
  base2 = ao_ref_base (ref2);
  offset2 = ref2->offset;
  max_size2 = ref2->max_size;
1409 1410 1411 1412 1413

  /* We can end up with registers or constants as bases for example from
     *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
     which is seen as a struct copy.  */
  if (TREE_CODE (base1) == SSA_NAME
1414
      || TREE_CODE (base1) == CONST_DECL
1415 1416 1417 1418
      || TREE_CODE (base1) == CONSTRUCTOR
      || TREE_CODE (base1) == ADDR_EXPR
      || CONSTANT_CLASS_P (base1)
      || TREE_CODE (base2) == SSA_NAME
1419
      || TREE_CODE (base2) == CONST_DECL
1420 1421 1422
      || TREE_CODE (base2) == CONSTRUCTOR
      || TREE_CODE (base2) == ADDR_EXPR
      || CONSTANT_CLASS_P (base2))
1423 1424
    return false;

1425
  /* We can end up referring to code via function and label decls.
1426 1427
     As we likely do not properly track code aliases conservatively
     bail out.  */
1428
  if (TREE_CODE (base1) == FUNCTION_DECL
1429
      || TREE_CODE (base1) == LABEL_DECL
1430
      || TREE_CODE (base2) == FUNCTION_DECL
1431
      || TREE_CODE (base2) == LABEL_DECL)
1432 1433
    return true;

1434 1435 1436 1437 1438
  /* Two volatile accesses always conflict.  */
  if (ref1->volatile_p
      && ref2->volatile_p)
    return true;

1439 1440 1441 1442
  /* Defer to simple offset based disambiguation if we have
     references based on two decls.  Do this before defering to
     TBAA to handle must-alias cases in conformance with the
     GCC extension of allowing type-punning through unions.  */
1443 1444
  var1_p = DECL_P (base1);
  var2_p = DECL_P (base2);
1445
  if (var1_p && var2_p)
1446 1447
    return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
				  ref2->ref, base2, offset2, max_size2);
1448

1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476
  /* Handle restrict based accesses.
     ???  ao_ref_base strips inner MEM_REF [&decl], recover from that
     here.  */
  tree rbase1 = base1;
  tree rbase2 = base2;
  if (var1_p)
    {
      rbase1 = ref1->ref;
      if (rbase1)
	while (handled_component_p (rbase1))
	  rbase1 = TREE_OPERAND (rbase1, 0);
    }
  if (var2_p)
    {
      rbase2 = ref2->ref;
      if (rbase2)
	while (handled_component_p (rbase2))
	  rbase2 = TREE_OPERAND (rbase2, 0);
    }
  if (rbase1 && rbase2
      && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
      && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
      /* If the accesses are in the same restrict clique... */
      && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
      /* But based on different pointers they do not alias.  */
      && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
    return false;

1477 1478 1479 1480
  ind1_p = (TREE_CODE (base1) == MEM_REF
	    || TREE_CODE (base1) == TARGET_MEM_REF);
  ind2_p = (TREE_CODE (base2) == MEM_REF
	    || TREE_CODE (base2) == TARGET_MEM_REF);
1481

1482 1483 1484
  /* Canonicalize the pointer-vs-decl case.  */
  if (ind1_p && var2_p)
    {
1485 1486 1487 1488
      std::swap (offset1, offset2);
      std::swap (max_size1, max_size2);
      std::swap (base1, base2);
      std::swap (ref1, ref2);
1489 1490 1491 1492 1493 1494
      var1_p = true;
      ind1_p = false;
      var2_p = false;
      ind2_p = true;
    }

1495
  /* First defer to TBAA if possible.  */
1496 1497
  if (tbaa_p
      && flag_strict_aliasing
1498 1499
      && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
				 ao_ref_alias_set (ref2)))
1500 1501 1502 1503
    return false;

  /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators.  */
  if (var1_p && ind2_p)
1504
    return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1505
					  offset2, max_size2,
1506 1507
					  ao_ref_alias_set (ref2),
					  ao_ref_base_alias_set (ref2),
1508
					  ref1->ref, base1,
1509
					  offset1, max_size1,
1510 1511 1512
					  ao_ref_alias_set (ref1),
					  ao_ref_base_alias_set (ref1),
					  tbaa_p);
1513
  else if (ind1_p && ind2_p)
1514
    return indirect_refs_may_alias_p (ref1->ref, base1,
1515
				      offset1, max_size1,
1516 1517
				      ao_ref_alias_set (ref1),
				      ao_ref_base_alias_set (ref1),
1518
				      ref2->ref, base2,
1519
				      offset2, max_size2,
1520 1521
				      ao_ref_alias_set (ref2),
				      ao_ref_base_alias_set (ref2),
1522
				      tbaa_p);
1523 1524 1525 1526

  gcc_unreachable ();
}

1527 1528 1529 1530 1531 1532 1533 1534
static bool
refs_may_alias_p (tree ref1, ao_ref *ref2)
{
  ao_ref r1;
  ao_ref_init (&r1, ref1);
  return refs_may_alias_p_1 (&r1, ref2, true);
}

1535 1536 1537
bool
refs_may_alias_p (tree ref1, tree ref2)
{
1538 1539 1540 1541 1542
  ao_ref r1, r2;
  bool res;
  ao_ref_init (&r1, ref1);
  ao_ref_init (&r2, ref2);
  res = refs_may_alias_p_1 (&r1, &r2, true);
1543 1544 1545 1546 1547 1548 1549
  if (res)
    ++alias_stats.refs_may_alias_p_may_alias;
  else
    ++alias_stats.refs_may_alias_p_no_alias;
  return res;
}

1550 1551 1552 1553 1554 1555
/* Returns true if there is a anti-dependence for the STORE that
   executes after the LOAD.  */

bool
refs_anti_dependent_p (tree load, tree store)
{
1556 1557 1558 1559
  ao_ref r1, r2;
  ao_ref_init (&r1, load);
  ao_ref_init (&r2, store);
  return refs_may_alias_p_1 (&r1, &r2, false);
1560 1561 1562 1563 1564 1565 1566 1567
}

/* Returns true if there is a output dependence for the stores
   STORE1 and STORE2.  */

bool
refs_output_dependent_p (tree store1, tree store2)
{
1568 1569 1570 1571
  ao_ref r1, r2;
  ao_ref_init (&r1, store1);
  ao_ref_init (&r2, store2);
  return refs_may_alias_p_1 (&r1, &r2, false);
1572
}
1573 1574 1575 1576 1577

/* If the call CALL may use the memory reference REF return true,
   otherwise return false.  */

static bool
1578
ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref)
1579
{
1580
  tree base, callee;
1581 1582 1583 1584 1585 1586 1587 1588
  unsigned i;
  int flags = gimple_call_flags (call);

  /* Const functions without a static chain do not implicitly use memory.  */
  if (!gimple_call_chain (call)
      && (flags & (ECF_CONST|ECF_NOVOPS)))
    goto process_args;

1589
  base = ao_ref_base (ref);
1590
  if (!base)
1591 1592
    return true;

1593 1594 1595 1596 1597
  /* A call that is not without side-effects might involve volatile
     accesses and thus conflicts with all other volatile accesses.  */
  if (ref->volatile_p)
    return true;

1598 1599 1600 1601
  /* If the reference is based on a decl that is not aliased the call
     cannot possibly use it.  */
  if (DECL_P (base)
      && !may_be_aliased (base)
1602 1603
      /* But local statics can be used through recursion.  */
      && !is_global_var (base))
1604 1605
    goto process_args;

1606 1607 1608 1609 1610 1611
  callee = gimple_call_fndecl (call);

  /* Handle those builtin functions explicitly that do not act as
     escape points.  See tree-ssa-structalias.c:find_func_aliases
     for the list of builtins we might need to handle here.  */
  if (callee != NULL_TREE
1612
      && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
1613 1614
    switch (DECL_FUNCTION_CODE (callee))
      {
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628
	/* All the following functions read memory pointed to by
	   their second argument.  strcat/strncat additionally
	   reads memory pointed to by the first argument.  */
	case BUILT_IN_STRCAT:
	case BUILT_IN_STRNCAT:
	  {
	    ao_ref dref;
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 0),
					   NULL_TREE);
	    if (refs_may_alias_p_1 (&dref, ref, false))
	      return true;
	  }
	  /* FALLTHRU */
1629 1630 1631 1632 1633 1634 1635
	case BUILT_IN_STRCPY:
	case BUILT_IN_STRNCPY:
	case BUILT_IN_MEMCPY:
	case BUILT_IN_MEMMOVE:
	case BUILT_IN_MEMPCPY:
	case BUILT_IN_STPCPY:
	case BUILT_IN_STPNCPY:
1636 1637
	case BUILT_IN_TM_MEMCPY:
	case BUILT_IN_TM_MEMMOVE:
1638
	  {
1639 1640 1641 1642 1643 1644 1645 1646
	    ao_ref dref;
	    tree size = NULL_TREE;
	    if (gimple_call_num_args (call) == 3)
	      size = gimple_call_arg (call, 2);
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 1),
					   size);
	    return refs_may_alias_p_1 (&dref, ref, false);
1647
	  }
1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658
	case BUILT_IN_STRCAT_CHK:
	case BUILT_IN_STRNCAT_CHK:
	  {
	    ao_ref dref;
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 0),
					   NULL_TREE);
	    if (refs_may_alias_p_1 (&dref, ref, false))
	      return true;
	  }
	  /* FALLTHRU */
1659 1660 1661 1662 1663 1664
	case BUILT_IN_STRCPY_CHK:
	case BUILT_IN_STRNCPY_CHK:
	case BUILT_IN_MEMCPY_CHK:
	case BUILT_IN_MEMMOVE_CHK:
	case BUILT_IN_MEMPCPY_CHK:
	case BUILT_IN_STPCPY_CHK:
1665
	case BUILT_IN_STPNCPY_CHK:
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675
	  {
	    ao_ref dref;
	    tree size = NULL_TREE;
	    if (gimple_call_num_args (call) == 4)
	      size = gimple_call_arg (call, 2);
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 1),
					   size);
	    return refs_may_alias_p_1 (&dref, ref, false);
	  }
1676 1677 1678 1679 1680 1681 1682 1683 1684
	case BUILT_IN_BCOPY:
	  {
	    ao_ref dref;
	    tree size = gimple_call_arg (call, 2);
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 0),
					   size);
	    return refs_may_alias_p_1 (&dref, ref, false);
	  }
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710

	/* The following functions read memory pointed to by their
	   first argument.  */
	CASE_BUILT_IN_TM_LOAD (1):
	CASE_BUILT_IN_TM_LOAD (2):
	CASE_BUILT_IN_TM_LOAD (4):
	CASE_BUILT_IN_TM_LOAD (8):
	CASE_BUILT_IN_TM_LOAD (FLOAT):
	CASE_BUILT_IN_TM_LOAD (DOUBLE):
	CASE_BUILT_IN_TM_LOAD (LDOUBLE):
	CASE_BUILT_IN_TM_LOAD (M64):
	CASE_BUILT_IN_TM_LOAD (M128):
	CASE_BUILT_IN_TM_LOAD (M256):
	case BUILT_IN_TM_LOG:
	case BUILT_IN_TM_LOG_1:
	case BUILT_IN_TM_LOG_2:
	case BUILT_IN_TM_LOG_4:
	case BUILT_IN_TM_LOG_8:
	case BUILT_IN_TM_LOG_FLOAT:
	case BUILT_IN_TM_LOG_DOUBLE:
	case BUILT_IN_TM_LOG_LDOUBLE:
	case BUILT_IN_TM_LOG_M64:
	case BUILT_IN_TM_LOG_M128:
	case BUILT_IN_TM_LOG_M256:
	  return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);

1711 1712 1713
	/* These read memory pointed to by the first argument.  */
	case BUILT_IN_STRDUP:
	case BUILT_IN_STRNDUP:
1714
	case BUILT_IN_REALLOC:
1715 1716 1717 1718 1719 1720 1721 1722 1723 1724
	  {
	    ao_ref dref;
	    tree size = NULL_TREE;
	    if (gimple_call_num_args (call) == 2)
	      size = gimple_call_arg (call, 1);
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 0),
					   size);
	    return refs_may_alias_p_1 (&dref, ref, false);
	  }
1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761
	/* These read memory pointed to by the first argument.  */
	case BUILT_IN_INDEX:
	case BUILT_IN_STRCHR:
	case BUILT_IN_STRRCHR:
	  {
	    ao_ref dref;
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 0),
					   NULL_TREE);
	    return refs_may_alias_p_1 (&dref, ref, false);
	  }
	/* These read memory pointed to by the first argument with size
	   in the third argument.  */
	case BUILT_IN_MEMCHR:
	  {
	    ao_ref dref;
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 0),
					   gimple_call_arg (call, 2));
	    return refs_may_alias_p_1 (&dref, ref, false);
	  }
	/* These read memory pointed to by the first and second arguments.  */
	case BUILT_IN_STRSTR:
	case BUILT_IN_STRPBRK:
	  {
	    ao_ref dref;
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 0),
					   NULL_TREE);
	    if (refs_may_alias_p_1 (&dref, ref, false))
	      return true;
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 1),
					   NULL_TREE);
	    return refs_may_alias_p_1 (&dref, ref, false);
	  }

1762 1763
	/* The following builtins do not read from memory.  */
	case BUILT_IN_FREE:
1764
	case BUILT_IN_MALLOC:
1765
	case BUILT_IN_POSIX_MEMALIGN:
1766
	case BUILT_IN_ALIGNED_ALLOC:
1767
	case BUILT_IN_CALLOC:
1768
	case BUILT_IN_ALLOCA:
1769
	case BUILT_IN_ALLOCA_WITH_ALIGN:
1770 1771
	case BUILT_IN_STACK_SAVE:
	case BUILT_IN_STACK_RESTORE:
1772
	case BUILT_IN_MEMSET:
1773
	case BUILT_IN_TM_MEMSET:
1774
	case BUILT_IN_MEMSET_CHK:
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792
	case BUILT_IN_FREXP:
	case BUILT_IN_FREXPF:
	case BUILT_IN_FREXPL:
	case BUILT_IN_GAMMA_R:
	case BUILT_IN_GAMMAF_R:
	case BUILT_IN_GAMMAL_R:
	case BUILT_IN_LGAMMA_R:
	case BUILT_IN_LGAMMAF_R:
	case BUILT_IN_LGAMMAL_R:
	case BUILT_IN_MODF:
	case BUILT_IN_MODFF:
	case BUILT_IN_MODFL:
	case BUILT_IN_REMQUO:
	case BUILT_IN_REMQUOF:
	case BUILT_IN_REMQUOL:
	case BUILT_IN_SINCOS:
	case BUILT_IN_SINCOSF:
	case BUILT_IN_SINCOSL:
1793
	case BUILT_IN_ASSUME_ALIGNED:
1794
	case BUILT_IN_VA_END:
1795
	  return false;
1796 1797 1798 1799 1800 1801 1802 1803 1804
	/* __sync_* builtins and some OpenMP builtins act as threading
	   barriers.  */
#undef DEF_SYNC_BUILTIN
#define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
#include "sync-builtins.def"
#undef DEF_SYNC_BUILTIN
	case BUILT_IN_GOMP_ATOMIC_START:
	case BUILT_IN_GOMP_ATOMIC_END:
	case BUILT_IN_GOMP_BARRIER:
Jakub Jelinek committed
1805
	case BUILT_IN_GOMP_BARRIER_CANCEL:
1806
	case BUILT_IN_GOMP_TASKWAIT:
Jakub Jelinek committed
1807
	case BUILT_IN_GOMP_TASKGROUP_END:
1808 1809 1810 1811 1812
	case BUILT_IN_GOMP_CRITICAL_START:
	case BUILT_IN_GOMP_CRITICAL_END:
	case BUILT_IN_GOMP_CRITICAL_NAME_START:
	case BUILT_IN_GOMP_CRITICAL_NAME_END:
	case BUILT_IN_GOMP_LOOP_END:
Jakub Jelinek committed
1813
	case BUILT_IN_GOMP_LOOP_END_CANCEL:
1814 1815 1816
	case BUILT_IN_GOMP_ORDERED_START:
	case BUILT_IN_GOMP_ORDERED_END:
	case BUILT_IN_GOMP_SECTIONS_END:
Jakub Jelinek committed
1817
	case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
1818 1819 1820
	case BUILT_IN_GOMP_SINGLE_COPY_START:
	case BUILT_IN_GOMP_SINGLE_COPY_END:
	  return true;
1821

1822 1823 1824 1825
	default:
	  /* Fallthru to general call handling.  */;
      }

1826 1827
  /* Check if base is a global static variable that is not read
     by the function.  */
1828 1829
  if (callee != NULL_TREE
      && TREE_CODE (base) == VAR_DECL
1830
      && TREE_STATIC (base))
1831
    {
Martin Liska committed
1832
      struct cgraph_node *node = cgraph_node::get (callee);
1833
      bitmap not_read;
1834

1835 1836 1837
      /* FIXME: Callee can be an OMP builtin that does not have a call graph
	 node yet.  We should enforce that there are nodes for all decls in the
	 IL and remove this check instead.  */
1838 1839 1840 1841
      if (node
	  && (not_read = ipa_reference_get_not_read_global (node))
	  && bitmap_bit_p (not_read, ipa_reference_var_uid (base)))
	goto process_args;
1842 1843
    }

1844 1845
  /* Check if the base variable is call-used.  */
  if (DECL_P (base))
1846
    {
1847
      if (pt_solution_includes (gimple_call_use_set (call), base))
1848 1849
	return true;
    }
1850 1851
  else if ((TREE_CODE (base) == MEM_REF
	    || TREE_CODE (base) == TARGET_MEM_REF)
1852
	   && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1853
    {
1854 1855 1856
      struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
      if (!pi)
	return true;
1857

1858
      if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1859 1860
	return true;
    }
1861 1862
  else
    return true;
1863 1864 1865 1866 1867 1868

  /* Inspect call arguments for passed-by-value aliases.  */
process_args:
  for (i = 0; i < gimple_call_num_args (call); ++i)
    {
      tree op = gimple_call_arg (call, i);
1869 1870 1871 1872
      int flags = gimple_call_arg_flags (call, i);

      if (flags & EAF_UNUSED)
	continue;
1873 1874 1875

      if (TREE_CODE (op) == WITH_SIZE_EXPR)
	op = TREE_OPERAND (op, 0);
1876

1877
      if (TREE_CODE (op) != SSA_NAME
1878 1879 1880 1881 1882 1883 1884
	  && !is_gimple_min_invariant (op))
	{
	  ao_ref r;
	  ao_ref_init (&r, op);
	  if (refs_may_alias_p_1 (&r, ref, true))
	    return true;
	}
1885 1886
    }

1887 1888 1889 1890
  return false;
}

static bool
1891
ref_maybe_used_by_call_p (gcall *call, ao_ref *ref)
1892
{
1893
  bool res;
1894
  res = ref_maybe_used_by_call_p_1 (call, ref);
1895 1896 1897 1898 1899
  if (res)
    ++alias_stats.ref_maybe_used_by_call_p_may_alias;
  else
    ++alias_stats.ref_maybe_used_by_call_p_no_alias;
  return res;
1900 1901 1902
}


1903 1904
/* If the statement STMT may use the memory reference REF return
   true, otherwise return false.  */
1905

1906
bool
1907
ref_maybe_used_by_stmt_p (gimple *stmt, ao_ref *ref)
1908
{
1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925
  if (is_gimple_assign (stmt))
    {
      tree rhs;

      /* All memory assign statements are single.  */
      if (!gimple_assign_single_p (stmt))
	return false;

      rhs = gimple_assign_rhs1 (stmt);
      if (is_gimple_reg (rhs)
	  || is_gimple_min_invariant (rhs)
	  || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
	return false;

      return refs_may_alias_p (rhs, ref);
    }
  else if (is_gimple_call (stmt))
1926 1927
    return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref);
  else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
1928
    {
1929
      tree retval = gimple_return_retval (return_stmt);
1930 1931 1932 1933 1934 1935
      if (retval
	  && TREE_CODE (retval) != SSA_NAME
	  && !is_gimple_min_invariant (retval)
	  && refs_may_alias_p (retval, ref))
	return true;
      /* If ref escapes the function then the return acts as a use.  */
1936
      tree base = ao_ref_base (ref);
1937 1938 1939 1940 1941 1942 1943 1944 1945
      if (!base)
	;
      else if (DECL_P (base))
	return is_global_var (base);
      else if (TREE_CODE (base) == MEM_REF
	       || TREE_CODE (base) == TARGET_MEM_REF)
	return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
      return false;
    }
1946 1947

  return true;
1948 1949
}

1950
bool
1951
ref_maybe_used_by_stmt_p (gimple *stmt, tree ref)
1952 1953 1954 1955 1956 1957
{
  ao_ref r;
  ao_ref_init (&r, ref);
  return ref_maybe_used_by_stmt_p (stmt, &r);
}

1958 1959
/* If the call in statement CALL may clobber the memory reference REF
   return true, otherwise return false.  */
1960

1961
bool
1962
call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref)
1963
{
1964
  tree base;
1965
  tree callee;
1966 1967 1968 1969 1970

  /* If the call is pure or const it cannot clobber anything.  */
  if (gimple_call_flags (call)
      & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
    return false;
1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986
  if (gimple_call_internal_p (call))
    switch (gimple_call_internal_fn (call))
      {
	/* Treat these internal calls like ECF_PURE for aliasing,
	   they don't write to any memory the program should care about.
	   They have important other side-effects, and read memory,
	   so can't be ECF_NOVOPS.  */
      case IFN_UBSAN_NULL:
      case IFN_UBSAN_BOUNDS:
      case IFN_UBSAN_VPTR:
      case IFN_UBSAN_OBJECT_SIZE:
      case IFN_ASAN_CHECK:
	return false;
      default:
	break;
      }
1987

1988
  base = ao_ref_base (ref);
1989 1990 1991 1992 1993 1994 1995
  if (!base)
    return true;

  if (TREE_CODE (base) == SSA_NAME
      || CONSTANT_CLASS_P (base))
    return false;

1996 1997 1998 1999 2000
  /* A call that is not without side-effects might involve volatile
     accesses and thus conflicts with all other volatile accesses.  */
  if (ref->volatile_p)
    return true;

2001 2002 2003 2004
  /* If the reference is based on a decl that is not aliased the call
     cannot possibly clobber it.  */
  if (DECL_P (base)
      && !may_be_aliased (base)
2005 2006 2007
      /* But local non-readonly statics can be modified through recursion
         or the call may implement a threading barrier which we must
	 treat as may-def.  */
2008
      && (TREE_READONLY (base)
2009
	  || !is_global_var (base)))
2010 2011
    return false;

2012 2013 2014 2015 2016 2017
  callee = gimple_call_fndecl (call);

  /* Handle those builtin functions explicitly that do not act as
     escape points.  See tree-ssa-structalias.c:find_func_aliases
     for the list of builtins we might need to handle here.  */
  if (callee != NULL_TREE
2018
      && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031
    switch (DECL_FUNCTION_CODE (callee))
      {
	/* All the following functions clobber memory pointed to by
	   their first argument.  */
	case BUILT_IN_STRCPY:
	case BUILT_IN_STRNCPY:
	case BUILT_IN_MEMCPY:
	case BUILT_IN_MEMMOVE:
	case BUILT_IN_MEMPCPY:
	case BUILT_IN_STPCPY:
	case BUILT_IN_STPNCPY:
	case BUILT_IN_STRCAT:
	case BUILT_IN_STRNCAT:
2032
	case BUILT_IN_MEMSET:
2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045
	case BUILT_IN_TM_MEMSET:
	CASE_BUILT_IN_TM_STORE (1):
	CASE_BUILT_IN_TM_STORE (2):
	CASE_BUILT_IN_TM_STORE (4):
	CASE_BUILT_IN_TM_STORE (8):
	CASE_BUILT_IN_TM_STORE (FLOAT):
	CASE_BUILT_IN_TM_STORE (DOUBLE):
	CASE_BUILT_IN_TM_STORE (LDOUBLE):
	CASE_BUILT_IN_TM_STORE (M64):
	CASE_BUILT_IN_TM_STORE (M128):
	CASE_BUILT_IN_TM_STORE (M256):
	case BUILT_IN_TM_MEMCPY:
	case BUILT_IN_TM_MEMMOVE:
2046
	  {
2047 2048
	    ao_ref dref;
	    tree size = NULL_TREE;
2049 2050 2051 2052 2053 2054
	    /* Don't pass in size for strncat, as the maximum size
	       is strlen (dest) + n + 1 instead of n, resp.
	       n + 1 at dest + strlen (dest), but strlen (dest) isn't
	       known.  */
	    if (gimple_call_num_args (call) == 3
		&& DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
2055 2056 2057 2058 2059
	      size = gimple_call_arg (call, 2);
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 0),
					   size);
	    return refs_may_alias_p_1 (&dref, ref, false);
2060
	  }
2061 2062 2063 2064 2065 2066
	case BUILT_IN_STRCPY_CHK:
	case BUILT_IN_STRNCPY_CHK:
	case BUILT_IN_MEMCPY_CHK:
	case BUILT_IN_MEMMOVE_CHK:
	case BUILT_IN_MEMPCPY_CHK:
	case BUILT_IN_STPCPY_CHK:
2067
	case BUILT_IN_STPNCPY_CHK:
2068 2069 2070 2071 2072 2073
	case BUILT_IN_STRCAT_CHK:
	case BUILT_IN_STRNCAT_CHK:
	case BUILT_IN_MEMSET_CHK:
	  {
	    ao_ref dref;
	    tree size = NULL_TREE;
2074 2075 2076 2077 2078 2079
	    /* Don't pass in size for __strncat_chk, as the maximum size
	       is strlen (dest) + n + 1 instead of n, resp.
	       n + 1 at dest + strlen (dest), but strlen (dest) isn't
	       known.  */
	    if (gimple_call_num_args (call) == 4
		&& DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
2080 2081 2082 2083 2084 2085
	      size = gimple_call_arg (call, 2);
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 0),
					   size);
	    return refs_may_alias_p_1 (&dref, ref, false);
	  }
2086 2087 2088 2089 2090 2091 2092 2093 2094
	case BUILT_IN_BCOPY:
	  {
	    ao_ref dref;
	    tree size = gimple_call_arg (call, 2);
	    ao_ref_init_from_ptr_and_size (&dref,
					   gimple_call_arg (call, 1),
					   size);
	    return refs_may_alias_p_1 (&dref, ref, false);
	  }
2095 2096 2097
	/* Allocating memory does not have any side-effects apart from
	   being the definition point for the pointer.  */
	case BUILT_IN_MALLOC:
2098
	case BUILT_IN_ALIGNED_ALLOC:
2099
	case BUILT_IN_CALLOC:
2100 2101
	case BUILT_IN_STRDUP:
	case BUILT_IN_STRNDUP:
2102
	  /* Unix98 specifies that errno is set on allocation failure.  */
2103
	  if (flag_errno_math
2104 2105
	      && targetm.ref_may_alias_errno (ref))
	    return true;
2106
	  return false;
2107 2108
	case BUILT_IN_STACK_SAVE:
	case BUILT_IN_ALLOCA:
2109
	case BUILT_IN_ALLOCA_WITH_ALIGN:
2110
	case BUILT_IN_ASSUME_ALIGNED:
2111
	  return false;
2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123
	/* But posix_memalign stores a pointer into the memory pointed to
	   by its first argument.  */
	case BUILT_IN_POSIX_MEMALIGN:
	  {
	    tree ptrptr = gimple_call_arg (call, 0);
	    ao_ref dref;
	    ao_ref_init_from_ptr_and_size (&dref, ptrptr,
					   TYPE_SIZE_UNIT (ptr_type_node));
	    return (refs_may_alias_p_1 (&dref, ref, false)
		    || (flag_errno_math
			&& targetm.ref_may_alias_errno (ref)));
	  }
2124 2125
	/* Freeing memory kills the pointed-to memory.  More importantly
	   the call has to serve as a barrier for moving loads and stores
2126
	   across it.  */
2127
	case BUILT_IN_FREE:
2128
	case BUILT_IN_VA_END:
2129 2130 2131 2132
	  {
	    tree ptr = gimple_call_arg (call, 0);
	    return ptr_deref_may_alias_ref_p_1 (ptr, ref);
	  }
2133 2134 2135 2136 2137 2138 2139 2140 2141
	/* Realloc serves both as allocation point and deallocation point.  */
	case BUILT_IN_REALLOC:
	  {
	    tree ptr = gimple_call_arg (call, 0);
	    /* Unix98 specifies that errno is set on allocation failure.  */
	    return ((flag_errno_math
		     && targetm.ref_may_alias_errno (ref))
		    || ptr_deref_may_alias_ref_p_1 (ptr, ref));
	  }
2142 2143 2144 2145 2146 2147
	case BUILT_IN_GAMMA_R:
	case BUILT_IN_GAMMAF_R:
	case BUILT_IN_GAMMAL_R:
	case BUILT_IN_LGAMMA_R:
	case BUILT_IN_LGAMMAF_R:
	case BUILT_IN_LGAMMAL_R:
2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158
	  {
	    tree out = gimple_call_arg (call, 1);
	    if (ptr_deref_may_alias_ref_p_1 (out, ref))
	      return true;
	    if (flag_errno_math)
	      break;
	    return false;
	  }
	case BUILT_IN_FREXP:
	case BUILT_IN_FREXPF:
	case BUILT_IN_FREXPL:
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170
	case BUILT_IN_MODF:
	case BUILT_IN_MODFF:
	case BUILT_IN_MODFL:
	  {
	    tree out = gimple_call_arg (call, 1);
	    return ptr_deref_may_alias_ref_p_1 (out, ref);
	  }
	case BUILT_IN_REMQUO:
	case BUILT_IN_REMQUOF:
	case BUILT_IN_REMQUOL:
	  {
	    tree out = gimple_call_arg (call, 2);
2171 2172 2173 2174 2175
	    if (ptr_deref_may_alias_ref_p_1 (out, ref))
	      return true;
	    if (flag_errno_math)
	      break;
	    return false;
2176 2177 2178 2179 2180 2181 2182 2183 2184 2185
	  }
	case BUILT_IN_SINCOS:
	case BUILT_IN_SINCOSF:
	case BUILT_IN_SINCOSL:
	  {
	    tree sin = gimple_call_arg (call, 1);
	    tree cos = gimple_call_arg (call, 2);
	    return (ptr_deref_may_alias_ref_p_1 (sin, ref)
		    || ptr_deref_may_alias_ref_p_1 (cos, ref));
	  }
2186 2187 2188 2189 2190 2191 2192 2193 2194
	/* __sync_* builtins and some OpenMP builtins act as threading
	   barriers.  */
#undef DEF_SYNC_BUILTIN
#define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
#include "sync-builtins.def"
#undef DEF_SYNC_BUILTIN
	case BUILT_IN_GOMP_ATOMIC_START:
	case BUILT_IN_GOMP_ATOMIC_END:
	case BUILT_IN_GOMP_BARRIER:
Jakub Jelinek committed
2195
	case BUILT_IN_GOMP_BARRIER_CANCEL:
2196
	case BUILT_IN_GOMP_TASKWAIT:
Jakub Jelinek committed
2197
	case BUILT_IN_GOMP_TASKGROUP_END:
2198 2199 2200 2201 2202
	case BUILT_IN_GOMP_CRITICAL_START:
	case BUILT_IN_GOMP_CRITICAL_END:
	case BUILT_IN_GOMP_CRITICAL_NAME_START:
	case BUILT_IN_GOMP_CRITICAL_NAME_END:
	case BUILT_IN_GOMP_LOOP_END:
Jakub Jelinek committed
2203
	case BUILT_IN_GOMP_LOOP_END_CANCEL:
2204 2205 2206
	case BUILT_IN_GOMP_ORDERED_START:
	case BUILT_IN_GOMP_ORDERED_END:
	case BUILT_IN_GOMP_SECTIONS_END:
Jakub Jelinek committed
2207
	case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2208 2209 2210
	case BUILT_IN_GOMP_SINGLE_COPY_START:
	case BUILT_IN_GOMP_SINGLE_COPY_END:
	  return true;
2211 2212 2213 2214
	default:
	  /* Fallthru to general call handling.  */;
      }

2215 2216
  /* Check if base is a global static variable that is not written
     by the function.  */
2217 2218
  if (callee != NULL_TREE
      && TREE_CODE (base) == VAR_DECL
2219
      && TREE_STATIC (base))
2220
    {
Martin Liska committed
2221
      struct cgraph_node *node = cgraph_node::get (callee);
2222
      bitmap not_written;
2223

2224 2225 2226 2227
      if (node
	  && (not_written = ipa_reference_get_not_written_global (node))
	  && bitmap_bit_p (not_written, ipa_reference_var_uid (base)))
	return false;
2228 2229
    }

2230
  /* Check if the base variable is call-clobbered.  */
2231
  if (DECL_P (base))
2232
    return pt_solution_includes (gimple_call_clobber_set (call), base);
2233 2234
  else if ((TREE_CODE (base) == MEM_REF
	    || TREE_CODE (base) == TARGET_MEM_REF)
2235 2236 2237 2238 2239 2240
	   && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
    {
      struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
      if (!pi)
	return true;

2241
      return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2242
    }
2243

2244 2245
  return true;
}
2246

2247 2248 2249 2250
/* If the call in statement CALL may clobber the memory reference REF
   return true, otherwise return false.  */

bool
2251
call_may_clobber_ref_p (gcall *call, tree ref)
2252
{
2253 2254 2255 2256
  bool res;
  ao_ref r;
  ao_ref_init (&r, ref);
  res = call_may_clobber_ref_p_1 (call, &r);
2257 2258 2259 2260 2261
  if (res)
    ++alias_stats.call_may_clobber_ref_p_may_alias;
  else
    ++alias_stats.call_may_clobber_ref_p_no_alias;
  return res;
2262
}
2263

2264 2265 2266

/* If the statement STMT may clobber the memory reference REF return true,
   otherwise return false.  */
2267 2268

bool
2269
stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref)
2270
{
2271 2272 2273 2274
  if (is_gimple_call (stmt))
    {
      tree lhs = gimple_call_lhs (stmt);
      if (lhs
2275
	  && TREE_CODE (lhs) != SSA_NAME)
2276 2277 2278 2279 2280 2281
	{
	  ao_ref r;
	  ao_ref_init (&r, lhs);
	  if (refs_may_alias_p_1 (ref, &r, true))
	    return true;
	}
2282

2283
      return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref);
2284
    }
2285
  else if (gimple_assign_single_p (stmt))
2286
    {
2287
      tree lhs = gimple_assign_lhs (stmt);
2288
      if (TREE_CODE (lhs) != SSA_NAME)
2289 2290
	{
	  ao_ref r;
2291
	  ao_ref_init (&r, lhs);
2292 2293
	  return refs_may_alias_p_1 (ref, &r, true);
	}
2294
    }
2295
  else if (gimple_code (stmt) == GIMPLE_ASM)
2296 2297
    return true;

2298
  return false;
2299
}
2300

2301
bool
2302
stmt_may_clobber_ref_p (gimple *stmt, tree ref)
2303 2304 2305 2306 2307 2308
{
  ao_ref r;
  ao_ref_init (&r, ref);
  return stmt_may_clobber_ref_p_1 (stmt, &r);
}

2309 2310 2311
/* If STMT kills the memory reference REF return true, otherwise
   return false.  */

2312
bool
2313
stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
2314
{
2315
  if (!ao_ref_base (ref))
2316 2317
    return false;

2318
  if (gimple_has_lhs (stmt)
2319 2320 2321 2322 2323 2324 2325 2326
      && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
      /* The assignment is not necessarily carried out if it can throw
	 and we can catch it in the current function where we could inspect
	 the previous value.
	 ???  We only need to care about the RHS throwing.  For aggregate
	 assignments or similar calls and non-call exceptions the LHS
	 might throw as well.  */
      && !stmt_can_throw_internal (stmt))
2327
    {
2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358
      tree lhs = gimple_get_lhs (stmt);
      /* If LHS is literally a base of the access we are done.  */
      if (ref->ref)
	{
	  tree base = ref->ref;
	  if (handled_component_p (base))
	    {
	      tree saved_lhs0 = NULL_TREE;
	      if (handled_component_p (lhs))
		{
		  saved_lhs0 = TREE_OPERAND (lhs, 0);
		  TREE_OPERAND (lhs, 0) = integer_zero_node;
		}
	      do
		{
		  /* Just compare the outermost handled component, if
		     they are equal we have found a possible common
		     base.  */
		  tree saved_base0 = TREE_OPERAND (base, 0);
		  TREE_OPERAND (base, 0) = integer_zero_node;
		  bool res = operand_equal_p (lhs, base, 0);
		  TREE_OPERAND (base, 0) = saved_base0;
		  if (res)
		    break;
		  /* Otherwise drop handled components of the access.  */
		  base = saved_base0;
		}
	      while (handled_component_p (base));
	      if (saved_lhs0)
		TREE_OPERAND (lhs, 0) = saved_lhs0;
	    }
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
	  /* Finally check if the lhs has the same address and size as the
	     base candidate of the access.  */
	  if (lhs == base
	      || (((TYPE_SIZE (TREE_TYPE (lhs))
		    == TYPE_SIZE (TREE_TYPE (base)))
		   || (TYPE_SIZE (TREE_TYPE (lhs))
		       && TYPE_SIZE (TREE_TYPE (base))
		       && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
					   TYPE_SIZE (TREE_TYPE (base)), 0)))
		  && operand_equal_p (lhs, base, OEP_ADDRESS_OF)))
2369 2370 2371 2372 2373 2374 2375 2376 2377
	    return true;
	}

      /* Now look for non-literal equal bases with the restriction of
         handling constant offset and size.  */
      /* For a must-alias check we need to be able to constrain
	 the access properly.  */
      if (ref->max_size == -1)
	return false;
2378
      HOST_WIDE_INT size, offset, max_size, ref_offset = ref->offset;
2379 2380 2381
      bool reverse;
      tree base
	= get_ref_base_and_extent (lhs, &offset, &size, &max_size, &reverse);
2382 2383
      /* We can get MEM[symbol: sZ, index: D.8862_1] here,
	 so base == ref->base does not always hold.  */
2384
      if (base != ref->base)
2385
	{
2386 2387 2388 2389 2390
	  /* If both base and ref->base are MEM_REFs, only compare the
	     first operand, and if the second operand isn't equal constant,
	     try to add the offsets into offset and ref_offset.  */
	  if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
	      && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
2391
	    {
2392 2393
	      if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
				       TREE_OPERAND (ref->base, 1)))
2394
		{
Kenneth Zadeck committed
2395
		  offset_int off1 = mem_ref_offset (base);
2396
		  off1 <<= LOG2_BITS_PER_UNIT;
Kenneth Zadeck committed
2397 2398
		  off1 += offset;
		  offset_int off2 = mem_ref_offset (ref->base);
2399
		  off2 <<= LOG2_BITS_PER_UNIT;
Kenneth Zadeck committed
2400 2401
		  off2 += ref_offset;
		  if (wi::fits_shwi_p (off1) && wi::fits_shwi_p (off2))
2402 2403 2404 2405 2406 2407 2408
		    {
		      offset = off1.to_shwi ();
		      ref_offset = off2.to_shwi ();
		    }
		  else
		    size = -1;
		}
2409
	    }
2410 2411 2412 2413 2414 2415 2416 2417 2418 2419
	  else
	    size = -1;
	}
      /* For a must-alias check we need to be able to constrain
	 the access properly.  */
      if (size != -1 && size == max_size)
	{
	  if (offset <= ref_offset
	      && offset + size >= ref_offset + ref->max_size)
	    return true;
2420 2421
	}
    }
2422 2423 2424 2425 2426

  if (is_gimple_call (stmt))
    {
      tree callee = gimple_call_fndecl (stmt);
      if (callee != NULL_TREE
2427
	  && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
2428 2429
	switch (DECL_FUNCTION_CODE (callee))
	  {
2430 2431 2432 2433 2434 2435 2436 2437 2438 2439
	  case BUILT_IN_FREE:
	    {
	      tree ptr = gimple_call_arg (stmt, 0);
	      tree base = ao_ref_base (ref);
	      if (base && TREE_CODE (base) == MEM_REF
		  && TREE_OPERAND (base, 0) == ptr)
		return true;
	      break;
	    }

2440 2441 2442 2443
	  case BUILT_IN_MEMCPY:
	  case BUILT_IN_MEMPCPY:
	  case BUILT_IN_MEMMOVE:
	  case BUILT_IN_MEMSET:
2444 2445 2446 2447
	  case BUILT_IN_MEMCPY_CHK:
	  case BUILT_IN_MEMPCPY_CHK:
	  case BUILT_IN_MEMMOVE_CHK:
	  case BUILT_IN_MEMSET_CHK:
2448
	    {
2449 2450 2451 2452
	      /* For a must-alias check we need to be able to constrain
		 the access properly.  */
	      if (ref->max_size == -1)
		return false;
2453 2454
	      tree dest = gimple_call_arg (stmt, 0);
	      tree len = gimple_call_arg (stmt, 2);
2455
	      if (!tree_fits_shwi_p (len))
2456
		return false;
2457
	      tree rbase = ref->base;
Kenneth Zadeck committed
2458
	      offset_int roffset = ref->offset;
2459 2460 2461
	      ao_ref dref;
	      ao_ref_init_from_ptr_and_size (&dref, dest, len);
	      tree base = ao_ref_base (&dref);
Kenneth Zadeck committed
2462
	      offset_int offset = dref.offset;
2463 2464 2465 2466 2467 2468 2469
	      if (!base || dref.size == -1)
		return false;
	      if (TREE_CODE (base) == MEM_REF)
		{
		  if (TREE_CODE (rbase) != MEM_REF)
		    return false;
		  // Compare pointers.
2470 2471
		  offset += mem_ref_offset (base) << LOG2_BITS_PER_UNIT;
		  roffset += mem_ref_offset (rbase) << LOG2_BITS_PER_UNIT;
2472 2473 2474
		  base = TREE_OPERAND (base, 0);
		  rbase = TREE_OPERAND (rbase, 0);
		}
Kenneth Zadeck committed
2475
	      if (base == rbase
2476 2477
		  && offset <= roffset
		  && (roffset + ref->max_size
2478
		      <= offset + (wi::to_offset (len) << LOG2_BITS_PER_UNIT)))
Kenneth Zadeck committed
2479
		return true;
2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492
	      break;
	    }

	  case BUILT_IN_VA_END:
	    {
	      tree ptr = gimple_call_arg (stmt, 0);
	      if (TREE_CODE (ptr) == ADDR_EXPR)
		{
		  tree base = ao_ref_base (ref);
		  if (TREE_OPERAND (ptr, 0) == base)
		    return true;
		}
	      break;
2493
	    }
2494

2495 2496 2497
	  default:;
	  }
    }
2498 2499 2500 2501
  return false;
}

bool
2502
stmt_kills_ref_p (gimple *stmt, tree ref)
2503 2504 2505
{
  ao_ref r;
  ao_ref_init (&r, ref);
2506
  return stmt_kills_ref_p (stmt, &r);
2507 2508
}

2509

2510 2511 2512 2513 2514
/* Walk the virtual use-def chain of VUSE until hitting the virtual operand
   TARGET or a statement clobbering the memory reference REF in which
   case false is returned.  The walk starts with VUSE, one argument of PHI.  */

static bool
2515
maybe_skip_until (gimple *phi, tree target, ao_ref *ref,
2516
		  tree vuse, unsigned int *cnt, bitmap *visited,
2517
		  bool abort_on_visited,
2518
		  void *(*translate)(ao_ref *, tree, void *, bool *),
2519
		  void *data)
2520
{
2521 2522
  basic_block bb = gimple_bb (phi);

2523 2524
  if (!*visited)
    *visited = BITMAP_ALLOC (NULL);
2525

2526 2527 2528 2529
  bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));

  /* Walk until we hit the target.  */
  while (vuse != target)
2530
    {
2531
      gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
2532 2533 2534 2535 2536
      /* Recurse for PHI nodes.  */
      if (gimple_code (def_stmt) == GIMPLE_PHI)
	{
	  /* An already visited PHI node ends the walk successfully.  */
	  if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
2537 2538
	    return !abort_on_visited;
	  vuse = get_continuation_for_phi (def_stmt, ref, cnt,
2539 2540
					   visited, abort_on_visited,
					   translate, data);
2541 2542 2543 2544
	  if (!vuse)
	    return false;
	  continue;
	}
2545
      else if (gimple_nop_p (def_stmt))
2546
	return false;
2547 2548 2549 2550 2551
      else
	{
	  /* A clobbering statement or the end of the IL ends it failing.  */
	  ++*cnt;
	  if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2552
	    {
2553
	      bool disambiguate_only = true;
2554
	      if (translate
2555
		  && (*translate) (ref, vuse, data, &disambiguate_only) == NULL)
2556 2557 2558 2559
		;
	      else
		return false;
	    }
2560
	}
2561 2562 2563 2564 2565
      /* If we reach a new basic-block see if we already skipped it
         in a previous walk that ended successfully.  */
      if (gimple_bb (def_stmt) != bb)
	{
	  if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
2566
	    return !abort_on_visited;
2567 2568
	  bb = gimple_bb (def_stmt);
	}
2569
      vuse = gimple_vuse (def_stmt);
2570
    }
2571 2572
  return true;
}
2573

2574 2575 2576 2577 2578
/* For two PHI arguments ARG0 and ARG1 try to skip non-aliasing code
   until we hit the phi argument definition that dominates the other one.
   Return that, or NULL_TREE if there is no such definition.  */

static tree
2579
get_continuation_for_phi_1 (gimple *phi, tree arg0, tree arg1,
2580
			    ao_ref *ref, unsigned int *cnt,
2581
			    bitmap *visited, bool abort_on_visited,
2582
			    void *(*translate)(ao_ref *, tree, void *, bool *),
2583
			    void *data)
2584
{
2585 2586
  gimple *def0 = SSA_NAME_DEF_STMT (arg0);
  gimple *def1 = SSA_NAME_DEF_STMT (arg1);
2587 2588 2589 2590 2591 2592 2593 2594 2595
  tree common_vuse;

  if (arg0 == arg1)
    return arg0;
  else if (gimple_nop_p (def0)
	   || (!gimple_nop_p (def1)
	       && dominated_by_p (CDI_DOMINATORS,
				  gimple_bb (def1), gimple_bb (def0))))
    {
2596
      if (maybe_skip_until (phi, arg0, ref, arg1, cnt,
2597
			    visited, abort_on_visited, translate, data))
2598 2599 2600 2601 2602 2603
	return arg0;
    }
  else if (gimple_nop_p (def1)
	   || dominated_by_p (CDI_DOMINATORS,
			      gimple_bb (def0), gimple_bb (def1)))
    {
2604
      if (maybe_skip_until (phi, arg1, ref, arg0, cnt,
2605
			    visited, abort_on_visited, translate, data))
2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623
	return arg1;
    }
  /* Special case of a diamond:
       MEM_1 = ...
       goto (cond) ? L1 : L2
       L1: store1 = ...    #MEM_2 = vuse(MEM_1)
	   goto L3
       L2: store2 = ...    #MEM_3 = vuse(MEM_1)
       L3: MEM_4 = PHI<MEM_2, MEM_3>
     We were called with the PHI at L3, MEM_2 and MEM_3 don't
     dominate each other, but still we can easily skip this PHI node
     if we recognize that the vuse MEM operand is the same for both,
     and that we can skip both statements (they don't clobber us).
     This is still linear.  Don't use maybe_skip_until, that might
     potentially be slow.  */
  else if ((common_vuse = gimple_vuse (def0))
	   && common_vuse == gimple_vuse (def1))
    {
2624
      bool disambiguate_only = true;
2625
      *cnt += 2;
2626 2627
      if ((!stmt_may_clobber_ref_p_1 (def0, ref)
	   || (translate
2628
	       && (*translate) (ref, arg0, data, &disambiguate_only) == NULL))
2629 2630
	  && (!stmt_may_clobber_ref_p_1 (def1, ref)
	      || (translate
2631
		  && (*translate) (ref, arg1, data, &disambiguate_only) == NULL)))
2632 2633 2634 2635 2636 2637 2638
	return common_vuse;
    }

  return NULL_TREE;
}


2639 2640 2641
/* Starting from a PHI node for the virtual operand of the memory reference
   REF find a continuation virtual operand that allows to continue walking
   statements dominating PHI skipping only statements that cannot possibly
2642 2643
   clobber REF.  Increments *CNT for each alias disambiguation done.
   Returns NULL_TREE if no suitable virtual operand can be found.  */
2644

2645
tree
2646
get_continuation_for_phi (gimple *phi, ao_ref *ref,
2647
			  unsigned int *cnt, bitmap *visited,
2648
			  bool abort_on_visited,
2649
			  void *(*translate)(ao_ref *, tree, void *, bool *),
2650
			  void *data)
2651 2652 2653 2654 2655 2656 2657
{
  unsigned nargs = gimple_phi_num_args (phi);

  /* Through a single-argument PHI we can simply look through.  */
  if (nargs == 1)
    return PHI_ARG_DEF (phi, 0);

2658 2659 2660
  /* For two or more arguments try to pairwise skip non-aliasing code
     until we hit the phi argument definition that dominates the other one.  */
  else if (nargs >= 2)
2661
    {
2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684
      tree arg0, arg1;
      unsigned i;

      /* Find a candidate for the virtual operand which definition
	 dominates those of all others.  */
      arg0 = PHI_ARG_DEF (phi, 0);
      if (!SSA_NAME_IS_DEFAULT_DEF (arg0))
	for (i = 1; i < nargs; ++i)
	  {
	    arg1 = PHI_ARG_DEF (phi, i);
	    if (SSA_NAME_IS_DEFAULT_DEF (arg1))
	      {
		arg0 = arg1;
		break;
	      }
	    if (dominated_by_p (CDI_DOMINATORS,
				gimple_bb (SSA_NAME_DEF_STMT (arg0)),
				gimple_bb (SSA_NAME_DEF_STMT (arg1))))
	      arg0 = arg1;
	  }

      /* Then pairwise reduce against the found candidate.  */
      for (i = 0; i < nargs; ++i)
2685
	{
2686
	  arg1 = PHI_ARG_DEF (phi, i);
2687
	  arg0 = get_continuation_for_phi_1 (phi, arg0, arg1, ref,
2688 2689
					     cnt, visited, abort_on_visited,
					     translate, data);
2690 2691
	  if (!arg0)
	    return NULL_TREE;
2692
	}
2693 2694

      return arg0;
2695
    }
2696 2697

  return NULL_TREE;
2698
}
2699

2700 2701 2702 2703
/* Based on the memory reference REF and its virtual use VUSE call
   WALKER for each virtual use that is equivalent to VUSE, including VUSE
   itself.  That is, for each virtual use for which its defining statement
   does not clobber REF.
2704

2705 2706 2707
   WALKER is called with REF, the current virtual use and DATA.  If
   WALKER returns non-NULL the walk stops and its result is returned.
   At the end of a non-successful walk NULL is returned.
2708

2709 2710 2711 2712 2713 2714 2715
   TRANSLATE if non-NULL is called with a pointer to REF, the virtual
   use which definition is a statement that may clobber REF and DATA.
   If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
   If TRANSLATE returns non-NULL the walk stops and its result is returned.
   If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
   to adjust REF and *DATA to make that valid.

2716 2717 2718 2719 2720
   VALUEIZE if non-NULL is called with the next VUSE that is considered
   and return value is substituted for that.  This can be used to
   implement optimistic value-numbering for example.  Note that the
   VUSE argument is assumed to be valueized already.

2721 2722 2723
   TODO: Cache the vector of equivalent vuses per ref, vuse pair.  */

void *
2724
walk_non_aliased_vuses (ao_ref *ref, tree vuse,
2725
			void *(*walker)(ao_ref *, tree, unsigned int, void *),
2726
			void *(*translate)(ao_ref *, tree, void *, bool *),
2727
			tree (*valueize)(tree),
2728
			void *data)
2729
{
2730 2731
  bitmap visited = NULL;
  void *res;
2732
  unsigned int cnt = 0;
2733
  bool translated = false;
2734

2735 2736
  timevar_push (TV_ALIAS_STMT_WALK);

2737 2738
  do
    {
2739
      gimple *def_stmt;
2740

2741
      /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
2742 2743 2744 2745 2746 2747 2748 2749 2750
      res = (*walker) (ref, vuse, cnt, data);
      /* Abort walk.  */
      if (res == (void *)-1)
	{
	  res = NULL;
	  break;
	}
      /* Lookup succeeded.  */
      else if (res != NULL)
2751
	break;
2752

2753 2754
      if (valueize)
	vuse = valueize (vuse);
2755 2756 2757 2758
      def_stmt = SSA_NAME_DEF_STMT (vuse);
      if (gimple_nop_p (def_stmt))
	break;
      else if (gimple_code (def_stmt) == GIMPLE_PHI)
2759
	vuse = get_continuation_for_phi (def_stmt, ref, &cnt,
2760
					 &visited, translated, translate, data);
2761 2762
      else
	{
2763
	  cnt++;
2764
	  if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
2765 2766 2767
	    {
	      if (!translate)
		break;
2768 2769
	      bool disambiguate_only = false;
	      res = (*translate) (ref, vuse, data, &disambiguate_only);
2770 2771 2772 2773 2774 2775 2776 2777 2778 2779
	      /* Failed lookup and translation.  */
	      if (res == (void *)-1)
		{
		  res = NULL;
		  break;
		}
	      /* Lookup succeeded.  */
	      else if (res != NULL)
		break;
	      /* Translation succeeded, continue walking.  */
2780
	      translated = translated || !disambiguate_only;
2781
	    }
2782 2783 2784 2785
	  vuse = gimple_vuse (def_stmt);
	}
    }
  while (vuse);
2786

2787 2788
  if (visited)
    BITMAP_FREE (visited);
2789

2790 2791
  timevar_pop (TV_ALIAS_STMT_WALK);

2792
  return res;
2793 2794
}

2795

2796 2797 2798 2799 2800 2801 2802
/* Based on the memory reference REF call WALKER for each vdef which
   defining statement may clobber REF, starting with VDEF.  If REF
   is NULL_TREE, each defining statement is visited.

   WALKER is called with REF, the current vdef and DATA.  If WALKER
   returns true the walk is stopped, otherwise it continues.

2803 2804 2805
   If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
   The pointer may be NULL and then we do not track this information.

2806 2807 2808 2809 2810
   At PHI nodes walk_aliased_vdefs forks into one walk for reach
   PHI argument (but only one walk continues on merge points), the
   return value is true if any of the walks was successful.

   The function returns the number of statements walked.  */
2811 2812

static unsigned int
2813 2814
walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
		      bool (*walker)(ao_ref *, tree, void *), void *data,
2815 2816
		      bitmap *visited, unsigned int cnt,
		      bool *function_entry_reached)
2817
{
2818 2819
  do
    {
2820
      gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
2821

2822 2823 2824 2825 2826
      if (*visited
	  && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
	return cnt;

      if (gimple_nop_p (def_stmt))
2827 2828 2829 2830 2831
	{
	  if (function_entry_reached)
	    *function_entry_reached = true;
	  return cnt;
	}
2832 2833 2834 2835 2836 2837 2838
      else if (gimple_code (def_stmt) == GIMPLE_PHI)
	{
	  unsigned i;
	  if (!*visited)
	    *visited = BITMAP_ALLOC (NULL);
	  for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
	    cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2839 2840
					 walker, data, visited, 0,
					 function_entry_reached);
2841 2842 2843
	  return cnt;
	}

2844
      /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
2845 2846
      cnt++;
      if ((!ref
2847
	   || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2848 2849 2850 2851 2852 2853
	  && (*walker) (ref, vdef, data))
	return cnt;

      vdef = gimple_vuse (def_stmt);
    }
  while (1);
2854 2855
}

2856
unsigned int
2857 2858
walk_aliased_vdefs (ao_ref *ref, tree vdef,
		    bool (*walker)(ao_ref *, tree, void *), void *data,
2859 2860
		    bitmap *visited,
		    bool *function_entry_reached)
2861
{
2862 2863 2864
  bitmap local_visited = NULL;
  unsigned int ret;

2865 2866
  timevar_push (TV_ALIAS_STMT_WALK);

2867 2868 2869
  if (function_entry_reached)
    *function_entry_reached = false;

2870
  ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2871 2872
			      visited ? visited : &local_visited, 0,
			      function_entry_reached);
2873 2874 2875
  if (local_visited)
    BITMAP_FREE (local_visited);

2876 2877
  timevar_pop (TV_ALIAS_STMT_WALK);

2878 2879 2880
  return ret;
}