OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-stable/] [gcc-4.5.1/] [gcc/] [tree-ssa-alias.c] - Blame information for rev 280

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 280 jeremybenn
/* Alias analysis for trees.
2
   Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010
3
   Free Software Foundation, Inc.
4
   Contributed by Diego Novillo <dnovillo@redhat.com>
5
 
6
This file is part of GCC.
7
 
8
GCC is free software; you can redistribute it and/or modify
9
it under the terms of the GNU General Public License as published by
10
the Free Software Foundation; either version 3, or (at your option)
11
any later version.
12
 
13
GCC is distributed in the hope that it will be useful,
14
but WITHOUT ANY WARRANTY; without even the implied warranty of
15
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16
GNU General Public License for more details.
17
 
18
You should have received a copy of the GNU General Public License
19
along with GCC; see the file COPYING3.  If not see
20
<http://www.gnu.org/licenses/>.  */
21
 
22
#include "config.h"
23
#include "system.h"
24
#include "coretypes.h"
25
#include "tm.h"
26
#include "tree.h"
27
#include "rtl.h"
28
#include "tm_p.h"
29
#include "hard-reg-set.h"
30
#include "basic-block.h"
31
#include "timevar.h"
32
#include "expr.h"
33
#include "ggc.h"
34
#include "langhooks.h"
35
#include "flags.h"
36
#include "function.h"
37
#include "diagnostic.h"
38
#include "tree-dump.h"
39
#include "gimple.h"
40
#include "tree-flow.h"
41
#include "tree-inline.h"
42
#include "tree-pass.h"
43
#include "convert.h"
44
#include "params.h"
45
#include "ipa-type-escape.h"
46
#include "vec.h"
47
#include "bitmap.h"
48
#include "vecprim.h"
49
#include "pointer-set.h"
50
#include "alloc-pool.h"
51
#include "tree-ssa-alias.h"
52
 
53
/* Broad overview of how alias analysis on gimple works:
54
 
55
   Statements clobbering or using memory are linked through the
56
   virtual operand factored use-def chain.  The virtual operand
57
   is unique per function, its symbol is accessible via gimple_vop (cfun).
58
   Virtual operands are used for efficiently walking memory statements
59
   in the gimple IL and are useful for things like value-numbering as
60
   a generation count for memory references.
61
 
62
   SSA_NAME pointers may have associated points-to information
63
   accessible via the SSA_NAME_PTR_INFO macro.  Flow-insensitive
64
   points-to information is (re-)computed by the TODO_rebuild_alias
65
   pass manager todo.  Points-to information is also used for more
66
   precise tracking of call-clobbered and call-used variables and
67
   related disambiguations.
68
 
69
   This file contains functions for disambiguating memory references,
70
   the so called alias-oracle and tools for walking of the gimple IL.
71
 
72
   The main alias-oracle entry-points are
73
 
74
   bool stmt_may_clobber_ref_p (gimple, tree)
75
 
76
     This function queries if a statement may invalidate (parts of)
77
     the memory designated by the reference tree argument.
78
 
79
   bool ref_maybe_used_by_stmt_p (gimple, tree)
80
 
81
     This function queries if a statement may need (parts of) the
82
     memory designated by the reference tree argument.
83
 
84
   There are variants of these functions that only handle the call
85
   part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
86
   Note that these do not disambiguate against a possible call lhs.
87
 
88
   bool refs_may_alias_p (tree, tree)
89
 
90
     This function tries to disambiguate two reference trees.
91
 
92
   bool ptr_deref_may_alias_global_p (tree)
93
 
94
     This function queries if dereferencing a pointer variable may
95
     alias global memory.
96
 
97
   More low-level disambiguators are available and documented in
98
   this file.  Low-level disambiguators dealing with points-to
99
   information are in tree-ssa-structalias.c.  */
100
 
101
 
102
/* Query statistics for the different low-level disambiguators.
103
   A high-level query may trigger multiple of them.  */
104
 
105
static struct {
106
  unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
107
  unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
108
  unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
109
  unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
110
  unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
111
  unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
112
} alias_stats;
113
 
114
void
115
dump_alias_stats (FILE *s)
116
{
117
  fprintf (s, "\nAlias oracle query stats:\n");
118
  fprintf (s, "  refs_may_alias_p: "
119
           HOST_WIDE_INT_PRINT_DEC" disambiguations, "
120
           HOST_WIDE_INT_PRINT_DEC" queries\n",
121
           alias_stats.refs_may_alias_p_no_alias,
122
           alias_stats.refs_may_alias_p_no_alias
123
           + alias_stats.refs_may_alias_p_may_alias);
124
  fprintf (s, "  ref_maybe_used_by_call_p: "
125
           HOST_WIDE_INT_PRINT_DEC" disambiguations, "
126
           HOST_WIDE_INT_PRINT_DEC" queries\n",
127
           alias_stats.ref_maybe_used_by_call_p_no_alias,
128
           alias_stats.refs_may_alias_p_no_alias
129
           + alias_stats.ref_maybe_used_by_call_p_may_alias);
130
  fprintf (s, "  call_may_clobber_ref_p: "
131
           HOST_WIDE_INT_PRINT_DEC" disambiguations, "
132
           HOST_WIDE_INT_PRINT_DEC" queries\n",
133
           alias_stats.call_may_clobber_ref_p_no_alias,
134
           alias_stats.call_may_clobber_ref_p_no_alias
135
           + alias_stats.call_may_clobber_ref_p_may_alias);
136
}
137
 
138
 
139
/* Return true, if dereferencing PTR may alias with a global variable.  */
140
 
141
bool
142
ptr_deref_may_alias_global_p (tree ptr)
143
{
144
  struct ptr_info_def *pi;
145
 
146
  /* If we end up with a pointer constant here that may point
147
     to global memory.  */
148
  if (TREE_CODE (ptr) != SSA_NAME)
149
    return true;
150
 
151
  pi = SSA_NAME_PTR_INFO (ptr);
152
 
153
  /* If we do not have points-to information for this variable,
154
     we have to punt.  */
155
  if (!pi)
156
    return true;
157
 
158
  /* ???  This does not use TBAA to prune globals ptr may not access.  */
159
  return pt_solution_includes_global (&pi->pt);
160
}
161
 
162
/* Return true if dereferencing PTR may alias DECL.
163
   The caller is responsible for applying TBAA to see if PTR
164
   may access DECL at all.  */
165
 
166
static bool
167
ptr_deref_may_alias_decl_p (tree ptr, tree decl)
168
{
169
  struct ptr_info_def *pi;
170
 
171
  gcc_assert ((TREE_CODE (ptr) == SSA_NAME
172
               || TREE_CODE (ptr) == ADDR_EXPR
173
               || TREE_CODE (ptr) == INTEGER_CST)
174
              && (TREE_CODE (decl) == VAR_DECL
175
                  || TREE_CODE (decl) == PARM_DECL
176
                  || TREE_CODE (decl) == RESULT_DECL));
177
 
178
  /* Non-aliased variables can not be pointed to.  */
179
  if (!may_be_aliased (decl))
180
    return false;
181
 
182
  /* ADDR_EXPR pointers either just offset another pointer or directly
183
     specify the pointed-to set.  */
184
  if (TREE_CODE (ptr) == ADDR_EXPR)
185
    {
186
      tree base = get_base_address (TREE_OPERAND (ptr, 0));
187
      if (base
188
          && INDIRECT_REF_P (base))
189
        ptr = TREE_OPERAND (base, 0);
190
      else if (base
191
               && SSA_VAR_P (base))
192
        return operand_equal_p (base, decl, 0);
193
      else if (base
194
               && CONSTANT_CLASS_P (base))
195
        return false;
196
      else
197
        return true;
198
    }
199
 
200
  /* We can end up with dereferencing constant pointers.
201
     Just bail out in this case.  */
202
  if (TREE_CODE (ptr) == INTEGER_CST)
203
    return true;
204
 
205
  /* If we do not have useful points-to information for this pointer
206
     we cannot disambiguate anything else.  */
207
  pi = SSA_NAME_PTR_INFO (ptr);
208
  if (!pi)
209
    return true;
210
 
211
  /* If the decl can be used as a restrict tag and we have a restrict
212
     pointer and that pointers points-to set doesn't contain this decl
213
     then they can't alias.  */
214
  if (DECL_RESTRICTED_P (decl)
215
      && TYPE_RESTRICT (TREE_TYPE (ptr))
216
      && pi->pt.vars_contains_restrict)
217
    return bitmap_bit_p (pi->pt.vars, DECL_UID (decl));
218
 
219
  return pt_solution_includes (&pi->pt, decl);
220
}
221
 
222
/* Return true if dereferenced PTR1 and PTR2 may alias.
223
   The caller is responsible for applying TBAA to see if accesses
224
   through PTR1 and PTR2 may conflict at all.  */
225
 
226
static bool
227
ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
228
{
229
  struct ptr_info_def *pi1, *pi2;
230
 
231
  gcc_assert ((TREE_CODE (ptr1) == SSA_NAME
232
               || TREE_CODE (ptr1) == ADDR_EXPR
233
               || TREE_CODE (ptr1) == INTEGER_CST)
234
              && (TREE_CODE (ptr2) == SSA_NAME
235
                  || TREE_CODE (ptr2) == ADDR_EXPR
236
                  || TREE_CODE (ptr2) == INTEGER_CST));
237
 
238
  /* ADDR_EXPR pointers either just offset another pointer or directly
239
     specify the pointed-to set.  */
240
  if (TREE_CODE (ptr1) == ADDR_EXPR)
241
    {
242
      tree base = get_base_address (TREE_OPERAND (ptr1, 0));
243
      if (base
244
          && INDIRECT_REF_P (base))
245
        ptr1 = TREE_OPERAND (base, 0);
246
      else if (base
247
               && SSA_VAR_P (base))
248
        return ptr_deref_may_alias_decl_p (ptr2, base);
249
      else
250
        return true;
251
    }
252
  if (TREE_CODE (ptr2) == ADDR_EXPR)
253
    {
254
      tree base = get_base_address (TREE_OPERAND (ptr2, 0));
255
      if (base
256
          && INDIRECT_REF_P (base))
257
        ptr2 = TREE_OPERAND (base, 0);
258
      else if (base
259
               && SSA_VAR_P (base))
260
        return ptr_deref_may_alias_decl_p (ptr1, base);
261
      else
262
        return true;
263
    }
264
 
265
  /* We can end up with dereferencing constant pointers.
266
     Just bail out in this case.  */
267
  if (TREE_CODE (ptr1) == INTEGER_CST
268
      || TREE_CODE (ptr2) == INTEGER_CST)
269
    return true;
270
 
271
  /* We may end up with two empty points-to solutions for two same pointers.
272
     In this case we still want to say both pointers alias, so shortcut
273
     that here.  */
274
  if (ptr1 == ptr2)
275
    return true;
276
 
277
  /* If we do not have useful points-to information for either pointer
278
     we cannot disambiguate anything else.  */
279
  pi1 = SSA_NAME_PTR_INFO (ptr1);
280
  pi2 = SSA_NAME_PTR_INFO (ptr2);
281
  if (!pi1 || !pi2)
282
    return true;
283
 
284
  /* If both pointers are restrict-qualified try to disambiguate
285
     with restrict information.  */
286
  if (TYPE_RESTRICT (TREE_TYPE (ptr1))
287
      && TYPE_RESTRICT (TREE_TYPE (ptr2))
288
      && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
289
    return false;
290
 
291
  /* ???  This does not use TBAA to prune decls from the intersection
292
     that not both pointers may access.  */
293
  return pt_solutions_intersect (&pi1->pt, &pi2->pt);
294
}
295
 
296
/* Return true if dereferencing PTR may alias *REF.
297
   The caller is responsible for applying TBAA to see if PTR
298
   may access *REF at all.  */
299
 
300
static bool
301
ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
302
{
303
  tree base = ao_ref_base (ref);
304
 
305
  if (INDIRECT_REF_P (base))
306
    return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
307
  else if (SSA_VAR_P (base))
308
    return ptr_deref_may_alias_decl_p (ptr, base);
309
 
310
  return true;
311
}
312
 
313
 
314
/* Dump alias information on FILE.  */
315
 
316
void
317
dump_alias_info (FILE *file)
318
{
319
  size_t i;
320
  const char *funcname
321
    = lang_hooks.decl_printable_name (current_function_decl, 2);
322
  referenced_var_iterator rvi;
323
  tree var;
324
 
325
  fprintf (file, "\n\nAlias information for %s\n\n", funcname);
326
 
327
  fprintf (file, "Aliased symbols\n\n");
328
 
329
  FOR_EACH_REFERENCED_VAR (var, rvi)
330
    {
331
      if (may_be_aliased (var))
332
        dump_variable (file, var);
333
    }
334
 
335
  fprintf (file, "\nCall clobber information\n");
336
 
337
  fprintf (file, "\nESCAPED");
338
  dump_points_to_solution (file, &cfun->gimple_df->escaped);
339
  fprintf (file, "\nCALLUSED");
340
  dump_points_to_solution (file, &cfun->gimple_df->callused);
341
 
342
  fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
343
 
344
  for (i = 1; i < num_ssa_names; i++)
345
    {
346
      tree ptr = ssa_name (i);
347
      struct ptr_info_def *pi;
348
 
349
      if (ptr == NULL_TREE
350
          || SSA_NAME_IN_FREE_LIST (ptr))
351
        continue;
352
 
353
      pi = SSA_NAME_PTR_INFO (ptr);
354
      if (pi)
355
        dump_points_to_info_for (file, ptr);
356
    }
357
 
358
  fprintf (file, "\n");
359
}
360
 
361
 
362
/* Dump alias information on stderr.  */
363
 
364
void
365
debug_alias_info (void)
366
{
367
  dump_alias_info (stderr);
368
}
369
 
370
 
371
/* Return the alias information associated with pointer T.  It creates a
372
   new instance if none existed.  */
373
 
374
struct ptr_info_def *
375
get_ptr_info (tree t)
376
{
377
  struct ptr_info_def *pi;
378
 
379
  gcc_assert (POINTER_TYPE_P (TREE_TYPE (t)));
380
 
381
  pi = SSA_NAME_PTR_INFO (t);
382
  if (pi == NULL)
383
    {
384
      pi = GGC_CNEW (struct ptr_info_def);
385
      pt_solution_reset (&pi->pt);
386
      SSA_NAME_PTR_INFO (t) = pi;
387
    }
388
 
389
  return pi;
390
}
391
 
392
/* Dump the points-to set *PT into FILE.  */
393
 
394
void
395
dump_points_to_solution (FILE *file, struct pt_solution *pt)
396
{
397
  if (pt->anything)
398
    fprintf (file, ", points-to anything");
399
 
400
  if (pt->nonlocal)
401
    fprintf (file, ", points-to non-local");
402
 
403
  if (pt->escaped)
404
    fprintf (file, ", points-to escaped");
405
 
406
  if (pt->null)
407
    fprintf (file, ", points-to NULL");
408
 
409
  if (pt->vars)
410
    {
411
      fprintf (file, ", points-to vars: ");
412
      dump_decl_set (file, pt->vars);
413
      if (pt->vars_contains_global)
414
        fprintf (file, " (includes global vars)");
415
    }
416
}
417
 
418
/* Dump points-to information for SSA_NAME PTR into FILE.  */
419
 
420
void
421
dump_points_to_info_for (FILE *file, tree ptr)
422
{
423
  struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
424
 
425
  print_generic_expr (file, ptr, dump_flags);
426
 
427
  if (pi)
428
    dump_points_to_solution (file, &pi->pt);
429
  else
430
    fprintf (file, ", points-to anything");
431
 
432
  fprintf (file, "\n");
433
}
434
 
435
 
436
/* Dump points-to information for VAR into stderr.  */
437
 
438
void
439
debug_points_to_info_for (tree var)
440
{
441
  dump_points_to_info_for (stderr, var);
442
}
443
 
444
 
445
/* Initializes the alias-oracle reference representation *R from REF.  */
446
 
447
void
448
ao_ref_init (ao_ref *r, tree ref)
449
{
450
  r->ref = ref;
451
  r->base = NULL_TREE;
452
  r->offset = 0;
453
  r->size = -1;
454
  r->max_size = -1;
455
  r->ref_alias_set = -1;
456
  r->base_alias_set = -1;
457
}
458
 
459
/* Returns the base object of the memory reference *REF.  */
460
 
461
tree
462
ao_ref_base (ao_ref *ref)
463
{
464
  if (ref->base)
465
    return ref->base;
466
  ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
467
                                       &ref->max_size);
468
  return ref->base;
469
}
470
 
471
/* Returns the base object alias set of the memory reference *REF.  */
472
 
473
static alias_set_type ATTRIBUTE_UNUSED
474
ao_ref_base_alias_set (ao_ref *ref)
475
{
476
  if (ref->base_alias_set != -1)
477
    return ref->base_alias_set;
478
  ref->base_alias_set = get_alias_set (ao_ref_base (ref));
479
  return ref->base_alias_set;
480
}
481
 
482
/* Returns the reference alias set of the memory reference *REF.  */
483
 
484
alias_set_type
485
ao_ref_alias_set (ao_ref *ref)
486
{
487
  if (ref->ref_alias_set != -1)
488
    return ref->ref_alias_set;
489
  ref->ref_alias_set = get_alias_set (ref->ref);
490
  return ref->ref_alias_set;
491
}
492
 
493
/* Init an alias-oracle reference representation from a gimple pointer
494
   PTR and a gimple size SIZE in bytes.  If SIZE is NULL_TREE the the
495
   size is assumed to be unknown.  The access is assumed to be only
496
   to or after of the pointer target, not before it.  */
497
 
498
void
499
ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
500
{
501
  HOST_WIDE_INT t1, t2;
502
  ref->ref = NULL_TREE;
503
  if (TREE_CODE (ptr) == ADDR_EXPR)
504
    ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
505
                                         &ref->offset, &t1, &t2);
506
  else
507
    {
508
      ref->base = build1 (INDIRECT_REF, char_type_node, ptr);
509
      ref->offset = 0;
510
    }
511
  if (size
512
      && host_integerp (size, 0)
513
      && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
514
    ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
515
  else
516
    ref->max_size = ref->size = -1;
517
  ref->ref_alias_set = 0;
518
  ref->base_alias_set = 0;
519
}
520
 
521
/* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
522
   purpose of TBAA.  Return 0 if they are distinct and -1 if we cannot
523
   decide.  */
524
 
525
static inline int
526
same_type_for_tbaa (tree type1, tree type2)
527
{
528
  type1 = TYPE_MAIN_VARIANT (type1);
529
  type2 = TYPE_MAIN_VARIANT (type2);
530
 
531
  /* If we would have to do structural comparison bail out.  */
532
  if (TYPE_STRUCTURAL_EQUALITY_P (type1)
533
      || TYPE_STRUCTURAL_EQUALITY_P (type2))
534
    return -1;
535
 
536
  /* Compare the canonical types.  */
537
  if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
538
    return 1;
539
 
540
  /* ??? Array types are not properly unified in all cases as we have
541
     spurious changes in the index types for example.  Removing this
542
     causes all sorts of problems with the Fortran frontend.  */
543
  if (TREE_CODE (type1) == ARRAY_TYPE
544
      && TREE_CODE (type2) == ARRAY_TYPE)
545
    return -1;
546
 
547
  /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
548
     object of one of its constrained subtypes, e.g. when a function with an
549
     unconstrained parameter passed by reference is called on an object and
550
     inlined.  But, even in the case of a fixed size, type and subtypes are
551
     not equivalent enough as to share the same TYPE_CANONICAL, since this
552
     would mean that conversions between them are useless, whereas they are
553
     not (e.g. type and subtypes can have different modes).  So, in the end,
554
     they are only guaranteed to have the same alias set.  */
555
  if (get_alias_set (type1) == get_alias_set (type2))
556
    return -1;
557
 
558
  /* The types are known to be not equal.  */
559
  return 0;
560
}
561
 
562
/* Determine if the two component references REF1 and REF2 which are
563
   based on access types TYPE1 and TYPE2 and of which at least one is based
564
   on an indirect reference may alias.  REF2 is the only one that can
565
   be a decl in which case REF2_IS_DECL is true.
566
   REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
567
   are the respective alias sets.  */
568
 
569
static bool
570
aliasing_component_refs_p (tree ref1, tree type1,
571
                           alias_set_type ref1_alias_set,
572
                           alias_set_type base1_alias_set,
573
                           HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
574
                           tree ref2, tree type2,
575
                           alias_set_type ref2_alias_set,
576
                           alias_set_type base2_alias_set,
577
                           HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
578
                           bool ref2_is_decl)
579
{
580
  /* If one reference is a component references through pointers try to find a
581
     common base and apply offset based disambiguation.  This handles
582
     for example
583
       struct A { int i; int j; } *q;
584
       struct B { struct A a; int k; } *p;
585
     disambiguating q->i and p->a.j.  */
586
  tree *refp;
587
  int same_p;
588
 
589
  /* Now search for the type1 in the access path of ref2.  This
590
     would be a common base for doing offset based disambiguation on.  */
591
  refp = &ref2;
592
  while (handled_component_p (*refp)
593
         && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
594
    refp = &TREE_OPERAND (*refp, 0);
595
  same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
596
  /* If we couldn't compare types we have to bail out.  */
597
  if (same_p == -1)
598
    return true;
599
  else if (same_p == 1)
600
    {
601
      HOST_WIDE_INT offadj, sztmp, msztmp;
602
      get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
603
      offset2 -= offadj;
604
      return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
605
    }
606
  /* If we didn't find a common base, try the other way around.  */
607
  refp = &ref1;
608
  while (handled_component_p (*refp)
609
         && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
610
    refp = &TREE_OPERAND (*refp, 0);
611
  same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
612
  /* If we couldn't compare types we have to bail out.  */
613
  if (same_p == -1)
614
    return true;
615
  else if (same_p == 1)
616
    {
617
      HOST_WIDE_INT offadj, sztmp, msztmp;
618
      get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
619
      offset1 -= offadj;
620
      return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
621
    }
622
 
623
  /* If we have two type access paths B1.path1 and B2.path2 they may
624
     only alias if either B1 is in B2.path2 or B2 is in B1.path1.
625
     But we can still have a path that goes B1.path1...B2.path2 with
626
     a part that we do not see.  So we can only disambiguate now
627
     if there is no B2 in the tail of path1 and no B1 on the
628
     tail of path2.  */
629
  if (base1_alias_set == ref2_alias_set
630
      || alias_set_subset_of (base1_alias_set, ref2_alias_set))
631
    return true;
632
  /* If this is ptr vs. decl then we know there is no ptr ... decl path.  */
633
  if (!ref2_is_decl)
634
    return (base2_alias_set == ref1_alias_set
635
            || alias_set_subset_of (base2_alias_set, ref1_alias_set));
636
  return false;
637
}
638
 
639
/* Return true if two memory references based on the variables BASE1
640
   and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
641
   [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  */
642
 
643
static bool
644
decl_refs_may_alias_p (tree base1,
645
                       HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
646
                       tree base2,
647
                       HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
648
{
649
  gcc_assert (SSA_VAR_P (base1) && SSA_VAR_P (base2));
650
 
651
  /* If both references are based on different variables, they cannot alias.  */
652
  if (!operand_equal_p (base1, base2, 0))
653
    return false;
654
 
655
  /* If both references are based on the same variable, they cannot alias if
656
     the accesses do not overlap.  */
657
  return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
658
}
659
 
660
/* Return true if an indirect reference based on *PTR1 constrained
661
   to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
662
   constrained to [OFFSET2, OFFSET2 + MAX_SIZE2).  *PTR1 and BASE2 have
663
   the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
664
   in which case they are computed on-demand.  REF1 and REF2
665
   if non-NULL are the complete memory reference trees.  */
666
 
667
static bool
668
indirect_ref_may_alias_decl_p (tree ref1, tree ptr1,
669
                               HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
670
                               alias_set_type ref1_alias_set,
671
                               alias_set_type base1_alias_set,
672
                               tree ref2, tree base2,
673
                               HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
674
                               alias_set_type ref2_alias_set,
675
                               alias_set_type base2_alias_set)
676
{
677
  /* If only one reference is based on a variable, they cannot alias if
678
     the pointer access is beyond the extent of the variable access.
679
     (the pointer base cannot validly point to an offset less than zero
680
     of the variable).
681
     They also cannot alias if the pointer may not point to the decl.  */
682
  if (max_size2 != -1
683
      && !ranges_overlap_p (offset1, max_size1, 0, offset2 + max_size2))
684
    return false;
685
  if (!ptr_deref_may_alias_decl_p (ptr1, base2))
686
    return false;
687
 
688
  /* Disambiguations that rely on strict aliasing rules follow.  */
689
  if (!flag_strict_aliasing)
690
    return true;
691
 
692
  /* If the alias set for a pointer access is zero all bets are off.  */
693
  if (base1_alias_set == -1)
694
    base1_alias_set = get_deref_alias_set (ptr1);
695
  if (base1_alias_set == 0)
696
    return true;
697
  if (base2_alias_set == -1)
698
    base2_alias_set = get_alias_set (base2);
699
 
700
  /* If both references are through the same type, they do not alias
701
     if the accesses do not overlap.  This does extra disambiguation
702
     for mixed/pointer accesses but requires strict aliasing.  */
703
  if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
704
                          TREE_TYPE (base2)) == 1)
705
    return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
706
 
707
  /* The only way to access a variable is through a pointer dereference
708
     of the same alias set or a subset of it.  */
709
  if (base1_alias_set != base2_alias_set
710
      && !alias_set_subset_of (base1_alias_set, base2_alias_set))
711
    return false;
712
 
713
  /* Do access-path based disambiguation.  */
714
  if (ref1 && ref2
715
      && handled_component_p (ref1)
716
      && handled_component_p (ref2))
717
    return aliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
718
                                      ref1_alias_set, base1_alias_set,
719
                                      offset1, max_size1,
720
                                      ref2, TREE_TYPE (base2),
721
                                      ref2_alias_set, base2_alias_set,
722
                                      offset2, max_size2, true);
723
 
724
  return true;
725
}
726
 
727
/* Return true if two indirect references based on *PTR1
728
   and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
729
   [OFFSET2, OFFSET2 + MAX_SIZE2) may alias.  *PTR1 and *PTR2 have
730
   the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
731
   in which case they are computed on-demand.  REF1 and REF2
732
   if non-NULL are the complete memory reference trees. */
733
 
734
static bool
735
indirect_refs_may_alias_p (tree ref1, tree ptr1,
736
                           HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
737
                           alias_set_type ref1_alias_set,
738
                           alias_set_type base1_alias_set,
739
                           tree ref2, tree ptr2,
740
                           HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
741
                           alias_set_type ref2_alias_set,
742
                           alias_set_type base2_alias_set)
743
{
744
  /* If both bases are based on pointers they cannot alias if they may not
745
     point to the same memory object or if they point to the same object
746
     and the accesses do not overlap.  */
747
  if (operand_equal_p (ptr1, ptr2, 0))
748
    return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
749
  if (!ptr_derefs_may_alias_p (ptr1, ptr2))
750
    return false;
751
 
752
  /* Disambiguations that rely on strict aliasing rules follow.  */
753
  if (!flag_strict_aliasing)
754
    return true;
755
 
756
  /* If the alias set for a pointer access is zero all bets are off.  */
757
  if (base1_alias_set == -1)
758
    base1_alias_set = get_deref_alias_set (ptr1);
759
  if (base1_alias_set == 0)
760
    return true;
761
  if (base2_alias_set == -1)
762
    base2_alias_set = get_deref_alias_set (ptr2);
763
  if (base2_alias_set == 0)
764
    return true;
765
 
766
  /* If both references are through the same type, they do not alias
767
     if the accesses do not overlap.  This does extra disambiguation
768
     for mixed/pointer accesses but requires strict aliasing.  */
769
  if (same_type_for_tbaa (TREE_TYPE (TREE_TYPE (ptr1)),
770
                          TREE_TYPE (TREE_TYPE (ptr2))) == 1)
771
    return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
772
 
773
  /* Do type-based disambiguation.  */
774
  if (base1_alias_set != base2_alias_set
775
      && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
776
    return false;
777
 
778
  /* Do access-path based disambiguation.  */
779
  if (ref1 && ref2
780
      && handled_component_p (ref1)
781
      && handled_component_p (ref2))
782
    return aliasing_component_refs_p (ref1, TREE_TYPE (TREE_TYPE (ptr1)),
783
                                      ref1_alias_set, base1_alias_set,
784
                                      offset1, max_size1,
785
                                      ref2, TREE_TYPE (TREE_TYPE (ptr2)),
786
                                      ref2_alias_set, base2_alias_set,
787
                                      offset2, max_size2, false);
788
 
789
  return true;
790
}
791
 
792
/* Return true, if the two memory references REF1 and REF2 may alias.  */
793
 
794
bool
795
refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
796
{
797
  tree base1, base2;
798
  HOST_WIDE_INT offset1 = 0, offset2 = 0;
799
  HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
800
  bool var1_p, var2_p, ind1_p, ind2_p;
801
  alias_set_type set;
802
 
803
#ifdef ENABLE_CHECKING
804
  gcc_assert ((!ref1->ref
805
               || TREE_CODE (ref1->ref) == SSA_NAME
806
               || DECL_P (ref1->ref)
807
               || handled_component_p (ref1->ref)
808
               || INDIRECT_REF_P (ref1->ref)
809
               || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
810
              && (!ref2->ref
811
                  || TREE_CODE (ref2->ref) == SSA_NAME
812
                  || DECL_P (ref2->ref)
813
                  || handled_component_p (ref2->ref)
814
                  || INDIRECT_REF_P (ref2->ref)
815
                  || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
816
#endif
817
 
818
  /* Decompose the references into their base objects and the access.  */
819
  base1 = ao_ref_base (ref1);
820
  offset1 = ref1->offset;
821
  max_size1 = ref1->max_size;
822
  base2 = ao_ref_base (ref2);
823
  offset2 = ref2->offset;
824
  max_size2 = ref2->max_size;
825
 
826
  /* We can end up with registers or constants as bases for example from
827
     *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
828
     which is seen as a struct copy.  */
829
  if (TREE_CODE (base1) == SSA_NAME
830
      || TREE_CODE (base2) == SSA_NAME
831
      || TREE_CODE (base1) == CONST_DECL
832
      || TREE_CODE (base2) == CONST_DECL
833
      || is_gimple_min_invariant (base1)
834
      || is_gimple_min_invariant (base2))
835
    return false;
836
 
837
  /* We can end up refering to code via function and label decls.
838
     As we likely do not properly track code aliases conservatively
839
     bail out.  */
840
  if (TREE_CODE (base1) == FUNCTION_DECL
841
      || TREE_CODE (base2) == FUNCTION_DECL
842
      || TREE_CODE (base1) == LABEL_DECL
843
      || TREE_CODE (base2) == LABEL_DECL)
844
    return true;
845
 
846
  /* Defer to simple offset based disambiguation if we have
847
     references based on two decls.  Do this before defering to
848
     TBAA to handle must-alias cases in conformance with the
849
     GCC extension of allowing type-punning through unions.  */
850
  var1_p = SSA_VAR_P (base1);
851
  var2_p = SSA_VAR_P (base2);
852
  if (var1_p && var2_p)
853
    return decl_refs_may_alias_p (base1, offset1, max_size1,
854
                                  base2, offset2, max_size2);
855
 
856
  ind1_p = INDIRECT_REF_P (base1);
857
  ind2_p = INDIRECT_REF_P (base2);
858
  /* Canonicalize the pointer-vs-decl case.  */
859
  if (ind1_p && var2_p)
860
    {
861
      HOST_WIDE_INT tmp1;
862
      tree tmp2;
863
      ao_ref *tmp3;
864
      tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
865
      tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
866
      tmp2 = base1; base1 = base2; base2 = tmp2;
867
      tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
868
      var1_p = true;
869
      ind1_p = false;
870
      var2_p = false;
871
      ind2_p = true;
872
    }
873
 
874
  /* If we are about to disambiguate pointer-vs-decl try harder to
875
     see must-aliases and give leeway to some invalid cases.
876
     This covers a pretty minimal set of cases only and does not
877
     when called from the RTL oracle.  It handles cases like
878
 
879
       int i = 1;
880
       return *(float *)&i;
881
 
882
     and also fixes gfortran.dg/lto/pr40725.  */
883
  if (var1_p && ind2_p
884
      && cfun
885
      && gimple_in_ssa_p (cfun)
886
      && TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME)
887
    {
888
      gimple def_stmt = SSA_NAME_DEF_STMT (TREE_OPERAND (base2, 0));
889
      while (is_gimple_assign (def_stmt)
890
             && (gimple_assign_rhs_code (def_stmt) == SSA_NAME
891
                 || CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt))))
892
        {
893
          tree rhs = gimple_assign_rhs1 (def_stmt);
894
          HOST_WIDE_INT offset, size, max_size;
895
 
896
          /* Look through SSA name copies and pointer conversions.  */
897
          if (TREE_CODE (rhs) == SSA_NAME
898
              && POINTER_TYPE_P (TREE_TYPE (rhs)))
899
            {
900
              def_stmt = SSA_NAME_DEF_STMT (rhs);
901
              continue;
902
            }
903
          if (TREE_CODE (rhs) != ADDR_EXPR)
904
            break;
905
 
906
          /* If the pointer is defined as an address based on a decl
907
             use plain offset disambiguation and ignore TBAA.  */
908
          rhs = TREE_OPERAND (rhs, 0);
909
          rhs = get_ref_base_and_extent (rhs, &offset, &size, &max_size);
910
          if (SSA_VAR_P (rhs))
911
            {
912
              base2 = rhs;
913
              offset2 += offset;
914
              if (size != max_size
915
                  || max_size == -1)
916
                max_size2 = -1;
917
              return decl_refs_may_alias_p (base1, offset1, max_size1,
918
                                            base2, offset2, max_size2);
919
            }
920
 
921
          /* Do not continue looking through &p->x to limit time
922
             complexity.  */
923
          break;
924
        }
925
    }
926
 
927
  /* First defer to TBAA if possible.  */
928
  if (tbaa_p
929
      && flag_strict_aliasing
930
      && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
931
                                 ao_ref_alias_set (ref2)))
932
    return false;
933
 
934
  /* If one reference is a TARGET_MEM_REF weird things are allowed.  Still
935
     TBAA disambiguation based on the access type is possible, so bail
936
     out only after that check.  */
937
  if ((ref1->ref && TREE_CODE (ref1->ref) == TARGET_MEM_REF)
938
      || (ref2->ref && TREE_CODE (ref2->ref) == TARGET_MEM_REF))
939
    return true;
940
 
941
  /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators.  */
942
  set = tbaa_p ? -1 : 0;
943
  if (var1_p && ind2_p)
944
    return indirect_ref_may_alias_decl_p (ref2->ref, TREE_OPERAND (base2, 0),
945
                                          offset2, max_size2,
946
                                          ao_ref_alias_set (ref2), set,
947
                                          ref1->ref, base1,
948
                                          offset1, max_size1,
949
                                          ao_ref_alias_set (ref1), set);
950
  else if (ind1_p && ind2_p)
951
    return indirect_refs_may_alias_p (ref1->ref, TREE_OPERAND (base1, 0),
952
                                      offset1, max_size1,
953
                                      ao_ref_alias_set (ref1), set,
954
                                      ref2->ref, TREE_OPERAND (base2, 0),
955
                                      offset2, max_size2,
956
                                      ao_ref_alias_set (ref2), set);
957
 
958
  gcc_unreachable ();
959
}
960
 
961
bool
962
refs_may_alias_p (tree ref1, tree ref2)
963
{
964
  ao_ref r1, r2;
965
  bool res;
966
  ao_ref_init (&r1, ref1);
967
  ao_ref_init (&r2, ref2);
968
  res = refs_may_alias_p_1 (&r1, &r2, true);
969
  if (res)
970
    ++alias_stats.refs_may_alias_p_may_alias;
971
  else
972
    ++alias_stats.refs_may_alias_p_no_alias;
973
  return res;
974
}
975
 
976
/* Returns true if there is a anti-dependence for the STORE that
977
   executes after the LOAD.  */
978
 
979
bool
980
refs_anti_dependent_p (tree load, tree store)
981
{
982
  ao_ref r1, r2;
983
  ao_ref_init (&r1, load);
984
  ao_ref_init (&r2, store);
985
  return refs_may_alias_p_1 (&r1, &r2, false);
986
}
987
 
988
/* Returns true if there is a output dependence for the stores
989
   STORE1 and STORE2.  */
990
 
991
bool
992
refs_output_dependent_p (tree store1, tree store2)
993
{
994
  ao_ref r1, r2;
995
  ao_ref_init (&r1, store1);
996
  ao_ref_init (&r2, store2);
997
  return refs_may_alias_p_1 (&r1, &r2, false);
998
}
999
 
1000
/* If the call CALL may use the memory reference REF return true,
1001
   otherwise return false.  */
1002
 
1003
static bool
1004
ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1005
{
1006
  tree base, callee;
1007
  unsigned i;
1008
  int flags = gimple_call_flags (call);
1009
 
1010
  /* Const functions without a static chain do not implicitly use memory.  */
1011
  if (!gimple_call_chain (call)
1012
      && (flags & (ECF_CONST|ECF_NOVOPS)))
1013
    goto process_args;
1014
 
1015
  base = ao_ref_base (ref);
1016
  if (!base)
1017
    return true;
1018
 
1019
  /* If the reference is based on a decl that is not aliased the call
1020
     cannot possibly use it.  */
1021
  if (DECL_P (base)
1022
      && !may_be_aliased (base)
1023
      /* But local statics can be used through recursion.  */
1024
      && !is_global_var (base))
1025
    goto process_args;
1026
 
1027
  callee = gimple_call_fndecl (call);
1028
 
1029
  /* Handle those builtin functions explicitly that do not act as
1030
     escape points.  See tree-ssa-structalias.c:find_func_aliases
1031
     for the list of builtins we might need to handle here.  */
1032
  if (callee != NULL_TREE
1033
      && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1034
    switch (DECL_FUNCTION_CODE (callee))
1035
      {
1036
        /* All the following functions clobber memory pointed to by
1037
           their first argument.  */
1038
        case BUILT_IN_STRCPY:
1039
        case BUILT_IN_STRNCPY:
1040
        case BUILT_IN_MEMCPY:
1041
        case BUILT_IN_MEMMOVE:
1042
        case BUILT_IN_MEMPCPY:
1043
        case BUILT_IN_STPCPY:
1044
        case BUILT_IN_STPNCPY:
1045
        case BUILT_IN_STRCAT:
1046
        case BUILT_IN_STRNCAT:
1047
          {
1048
            ao_ref dref;
1049
            tree size = NULL_TREE;
1050
            if (gimple_call_num_args (call) == 3)
1051
              size = gimple_call_arg (call, 2);
1052
            ao_ref_init_from_ptr_and_size (&dref,
1053
                                           gimple_call_arg (call, 1),
1054
                                           size);
1055
            return refs_may_alias_p_1 (&dref, ref, false);
1056
          }
1057
        case BUILT_IN_BCOPY:
1058
          {
1059
            ao_ref dref;
1060
            tree size = gimple_call_arg (call, 2);
1061
            ao_ref_init_from_ptr_and_size (&dref,
1062
                                           gimple_call_arg (call, 0),
1063
                                           size);
1064
            return refs_may_alias_p_1 (&dref, ref, false);
1065
          }
1066
        /* The following builtins do not read from memory.  */
1067
        case BUILT_IN_FREE:
1068
        case BUILT_IN_MALLOC:
1069
        case BUILT_IN_CALLOC:
1070
        case BUILT_IN_MEMSET:
1071
        case BUILT_IN_FREXP:
1072
        case BUILT_IN_FREXPF:
1073
        case BUILT_IN_FREXPL:
1074
        case BUILT_IN_GAMMA_R:
1075
        case BUILT_IN_GAMMAF_R:
1076
        case BUILT_IN_GAMMAL_R:
1077
        case BUILT_IN_LGAMMA_R:
1078
        case BUILT_IN_LGAMMAF_R:
1079
        case BUILT_IN_LGAMMAL_R:
1080
        case BUILT_IN_MODF:
1081
        case BUILT_IN_MODFF:
1082
        case BUILT_IN_MODFL:
1083
        case BUILT_IN_REMQUO:
1084
        case BUILT_IN_REMQUOF:
1085
        case BUILT_IN_REMQUOL:
1086
        case BUILT_IN_SINCOS:
1087
        case BUILT_IN_SINCOSF:
1088
        case BUILT_IN_SINCOSL:
1089
          return false;
1090
 
1091
        default:
1092
          /* Fallthru to general call handling.  */;
1093
      }
1094
 
1095
  /* Check if base is a global static variable that is not read
1096
     by the function.  */
1097
  if (TREE_CODE (base) == VAR_DECL
1098
      && TREE_STATIC (base)
1099
      && !TREE_PUBLIC (base))
1100
    {
1101
      bitmap not_read;
1102
 
1103
      if (callee != NULL_TREE
1104
          && (not_read
1105
                = ipa_reference_get_not_read_global (cgraph_node (callee)))
1106
          && bitmap_bit_p (not_read, DECL_UID (base)))
1107
        goto process_args;
1108
    }
1109
 
1110
  /* If the base variable is call-used or call-clobbered then
1111
     it may be used.  */
1112
  if (flags & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1113
    {
1114
      if (DECL_P (base))
1115
        {
1116
          if (is_call_used (base))
1117
            return true;
1118
        }
1119
      else if (INDIRECT_REF_P (base)
1120
               && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1121
        {
1122
          struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1123
          if (!pi)
1124
            return true;
1125
 
1126
          if (pt_solution_includes_global (&pi->pt)
1127
              || pt_solutions_intersect (&cfun->gimple_df->callused, &pi->pt)
1128
              || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt))
1129
            return true;
1130
        }
1131
      else
1132
        return true;
1133
    }
1134
  else
1135
    {
1136
      if (DECL_P (base))
1137
        {
1138
          if (is_call_clobbered (base))
1139
            return true;
1140
        }
1141
      else if (INDIRECT_REF_P (base)
1142
               && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1143
        {
1144
          struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1145
          if (!pi)
1146
            return true;
1147
 
1148
          if (pt_solution_includes_global (&pi->pt)
1149
              || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt))
1150
            return true;
1151
        }
1152
      else
1153
        return true;
1154
    }
1155
 
1156
  /* Inspect call arguments for passed-by-value aliases.  */
1157
process_args:
1158
  for (i = 0; i < gimple_call_num_args (call); ++i)
1159
    {
1160
      tree op = gimple_call_arg (call, i);
1161
 
1162
      if (TREE_CODE (op) == WITH_SIZE_EXPR)
1163
        op = TREE_OPERAND (op, 0);
1164
 
1165
      if (TREE_CODE (op) != SSA_NAME
1166
          && !is_gimple_min_invariant (op))
1167
        {
1168
          ao_ref r;
1169
          ao_ref_init (&r, op);
1170
          if (refs_may_alias_p_1 (&r, ref, true))
1171
            return true;
1172
        }
1173
    }
1174
 
1175
  return false;
1176
}
1177
 
1178
static bool
1179
ref_maybe_used_by_call_p (gimple call, tree ref)
1180
{
1181
  ao_ref r;
1182
  bool res;
1183
  ao_ref_init (&r, ref);
1184
  res = ref_maybe_used_by_call_p_1 (call, &r);
1185
  if (res)
1186
    ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1187
  else
1188
    ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1189
  return res;
1190
}
1191
 
1192
 
1193
/* If the statement STMT may use the memory reference REF return
1194
   true, otherwise return false.  */
1195
 
1196
bool
1197
ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1198
{
1199
  if (is_gimple_assign (stmt))
1200
    {
1201
      tree rhs;
1202
 
1203
      /* All memory assign statements are single.  */
1204
      if (!gimple_assign_single_p (stmt))
1205
        return false;
1206
 
1207
      rhs = gimple_assign_rhs1 (stmt);
1208
      if (is_gimple_reg (rhs)
1209
          || is_gimple_min_invariant (rhs)
1210
          || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1211
        return false;
1212
 
1213
      return refs_may_alias_p (rhs, ref);
1214
    }
1215
  else if (is_gimple_call (stmt))
1216
    return ref_maybe_used_by_call_p (stmt, ref);
1217
 
1218
  return true;
1219
}
1220
 
1221
/* If the call in statement CALL may clobber the memory reference REF
1222
   return true, otherwise return false.  */
1223
 
1224
static bool
1225
call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1226
{
1227
  tree base;
1228
  tree callee;
1229
 
1230
  /* If the call is pure or const it cannot clobber anything.  */
1231
  if (gimple_call_flags (call)
1232
      & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1233
    return false;
1234
 
1235
  base = ao_ref_base (ref);
1236
  if (!base)
1237
    return true;
1238
 
1239
  if (TREE_CODE (base) == SSA_NAME
1240
      || CONSTANT_CLASS_P (base))
1241
    return false;
1242
 
1243
  /* If the reference is based on a decl that is not aliased the call
1244
     cannot possibly clobber it.  */
1245
  if (DECL_P (base)
1246
      && !may_be_aliased (base)
1247
      /* But local non-readonly statics can be modified through recursion
1248
         or the call may implement a threading barrier which we must
1249
         treat as may-def.  */
1250
      && (TREE_READONLY (base)
1251
          || !is_global_var (base)))
1252
    return false;
1253
 
1254
  callee = gimple_call_fndecl (call);
1255
 
1256
  /* Handle those builtin functions explicitly that do not act as
1257
     escape points.  See tree-ssa-structalias.c:find_func_aliases
1258
     for the list of builtins we might need to handle here.  */
1259
  if (callee != NULL_TREE
1260
      && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1261
    switch (DECL_FUNCTION_CODE (callee))
1262
      {
1263
        /* All the following functions clobber memory pointed to by
1264
           their first argument.  */
1265
        case BUILT_IN_STRCPY:
1266
        case BUILT_IN_STRNCPY:
1267
        case BUILT_IN_MEMCPY:
1268
        case BUILT_IN_MEMMOVE:
1269
        case BUILT_IN_MEMPCPY:
1270
        case BUILT_IN_STPCPY:
1271
        case BUILT_IN_STPNCPY:
1272
        case BUILT_IN_STRCAT:
1273
        case BUILT_IN_STRNCAT:
1274
        case BUILT_IN_MEMSET:
1275
          {
1276
            ao_ref dref;
1277
            tree size = NULL_TREE;
1278
            if (gimple_call_num_args (call) == 3)
1279
              size = gimple_call_arg (call, 2);
1280
            ao_ref_init_from_ptr_and_size (&dref,
1281
                                           gimple_call_arg (call, 0),
1282
                                           size);
1283
            return refs_may_alias_p_1 (&dref, ref, false);
1284
          }
1285
        case BUILT_IN_BCOPY:
1286
          {
1287
            ao_ref dref;
1288
            tree size = gimple_call_arg (call, 2);
1289
            ao_ref_init_from_ptr_and_size (&dref,
1290
                                           gimple_call_arg (call, 1),
1291
                                           size);
1292
            return refs_may_alias_p_1 (&dref, ref, false);
1293
          }
1294
        /* Allocating memory does not have any side-effects apart from
1295
           being the definition point for the pointer.  */
1296
        case BUILT_IN_MALLOC:
1297
        case BUILT_IN_CALLOC:
1298
          /* Unix98 specifies that errno is set on allocation failure.
1299
             Until we properly can track the errno location assume it
1300
             is not a local decl but external or anonymous storage in
1301
             a different translation unit.  Also assume it is of
1302
             type int as required by the standard.  */
1303
          if (flag_errno_math
1304
              && TREE_TYPE (base) == integer_type_node)
1305
            {
1306
              struct ptr_info_def *pi;
1307
              if (DECL_P (base)
1308
                  && !TREE_STATIC (base))
1309
                return true;
1310
              else if (INDIRECT_REF_P (base)
1311
                       && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
1312
                       && (pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0))))
1313
                return pi->pt.anything || pi->pt.nonlocal;
1314
            }
1315
          return false;
1316
        /* Freeing memory kills the pointed-to memory.  More importantly
1317
           the call has to serve as a barrier for moving loads and stores
1318
           across it.  */
1319
        case BUILT_IN_FREE:
1320
          {
1321
            tree ptr = gimple_call_arg (call, 0);
1322
            return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1323
          }
1324
        case BUILT_IN_GAMMA_R:
1325
        case BUILT_IN_GAMMAF_R:
1326
        case BUILT_IN_GAMMAL_R:
1327
        case BUILT_IN_LGAMMA_R:
1328
        case BUILT_IN_LGAMMAF_R:
1329
        case BUILT_IN_LGAMMAL_R:
1330
          {
1331
            tree out = gimple_call_arg (call, 1);
1332
            if (ptr_deref_may_alias_ref_p_1 (out, ref))
1333
              return true;
1334
            if (flag_errno_math)
1335
              break;
1336
            return false;
1337
          }
1338
        case BUILT_IN_FREXP:
1339
        case BUILT_IN_FREXPF:
1340
        case BUILT_IN_FREXPL:
1341
        case BUILT_IN_MODF:
1342
        case BUILT_IN_MODFF:
1343
        case BUILT_IN_MODFL:
1344
          {
1345
            tree out = gimple_call_arg (call, 1);
1346
            return ptr_deref_may_alias_ref_p_1 (out, ref);
1347
          }
1348
        case BUILT_IN_REMQUO:
1349
        case BUILT_IN_REMQUOF:
1350
        case BUILT_IN_REMQUOL:
1351
          {
1352
            tree out = gimple_call_arg (call, 2);
1353
            if (ptr_deref_may_alias_ref_p_1 (out, ref))
1354
              return true;
1355
            if (flag_errno_math)
1356
              break;
1357
            return false;
1358
          }
1359
        case BUILT_IN_SINCOS:
1360
        case BUILT_IN_SINCOSF:
1361
        case BUILT_IN_SINCOSL:
1362
          {
1363
            tree sin = gimple_call_arg (call, 1);
1364
            tree cos = gimple_call_arg (call, 2);
1365
            return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1366
                    || ptr_deref_may_alias_ref_p_1 (cos, ref));
1367
          }
1368
        default:
1369
          /* Fallthru to general call handling.  */;
1370
      }
1371
 
1372
  /* Check if base is a global static variable that is not written
1373
     by the function.  */
1374
  if (callee != NULL_TREE
1375
      && TREE_CODE (base) == VAR_DECL
1376
      && TREE_STATIC (base)
1377
      && !TREE_PUBLIC (base))
1378
    {
1379
      bitmap not_written;
1380
 
1381
      if ((not_written
1382
             = ipa_reference_get_not_written_global (cgraph_node (callee)))
1383
          && bitmap_bit_p (not_written, DECL_UID (base)))
1384
        return false;
1385
    }
1386
 
1387
  if (DECL_P (base))
1388
    return is_call_clobbered (base);
1389
  else if (INDIRECT_REF_P (base)
1390
           && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1391
    {
1392
      struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1393
      if (!pi)
1394
        return true;
1395
 
1396
      return (pt_solution_includes_global (&pi->pt)
1397
              || pt_solutions_intersect (&cfun->gimple_df->escaped, &pi->pt));
1398
    }
1399
 
1400
  return true;
1401
}
1402
 
1403
static bool ATTRIBUTE_UNUSED
1404
call_may_clobber_ref_p (gimple call, tree ref)
1405
{
1406
  bool res;
1407
  ao_ref r;
1408
  ao_ref_init (&r, ref);
1409
  res = call_may_clobber_ref_p_1 (call, &r);
1410
  if (res)
1411
    ++alias_stats.call_may_clobber_ref_p_may_alias;
1412
  else
1413
    ++alias_stats.call_may_clobber_ref_p_no_alias;
1414
  return res;
1415
}
1416
 
1417
 
1418
/* If the statement STMT may clobber the memory reference REF return true,
1419
   otherwise return false.  */
1420
 
1421
bool
1422
stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1423
{
1424
  if (is_gimple_call (stmt))
1425
    {
1426
      tree lhs = gimple_call_lhs (stmt);
1427
      if (lhs
1428
          && !is_gimple_reg (lhs))
1429
        {
1430
          ao_ref r;
1431
          ao_ref_init (&r, lhs);
1432
          if (refs_may_alias_p_1 (ref, &r, true))
1433
            return true;
1434
        }
1435
 
1436
      return call_may_clobber_ref_p_1 (stmt, ref);
1437
    }
1438
  else if (gimple_assign_single_p (stmt))
1439
    {
1440
      tree lhs = gimple_assign_lhs (stmt);
1441
      if (!is_gimple_reg (lhs))
1442
        {
1443
          ao_ref r;
1444
          ao_ref_init (&r, gimple_assign_lhs (stmt));
1445
          return refs_may_alias_p_1 (ref, &r, true);
1446
        }
1447
    }
1448
  else if (gimple_code (stmt) == GIMPLE_ASM)
1449
    return true;
1450
 
1451
  return false;
1452
}
1453
 
1454
bool
1455
stmt_may_clobber_ref_p (gimple stmt, tree ref)
1456
{
1457
  ao_ref r;
1458
  ao_ref_init (&r, ref);
1459
  return stmt_may_clobber_ref_p_1 (stmt, &r);
1460
}
1461
 
1462
 
1463
/* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1464
   TARGET or a statement clobbering the memory reference REF in which
1465
   case false is returned.  The walk starts with VUSE, one argument of PHI.  */
1466
 
1467
static bool
1468
maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1469
                  tree vuse, bitmap *visited)
1470
{
1471
  if (!*visited)
1472
    *visited = BITMAP_ALLOC (NULL);
1473
 
1474
  bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1475
 
1476
  /* Walk until we hit the target.  */
1477
  while (vuse != target)
1478
    {
1479
      gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1480
      /* Recurse for PHI nodes.  */
1481
      if (gimple_code (def_stmt) == GIMPLE_PHI)
1482
        {
1483
          /* An already visited PHI node ends the walk successfully.  */
1484
          if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1485
            return true;
1486
          vuse = get_continuation_for_phi (def_stmt, ref, visited);
1487
          if (!vuse)
1488
            return false;
1489
          continue;
1490
        }
1491
      /* A clobbering statement or the end of the IL ends it failing.  */
1492
      else if (gimple_nop_p (def_stmt)
1493
               || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1494
        return false;
1495
      vuse = gimple_vuse (def_stmt);
1496
    }
1497
  return true;
1498
}
1499
 
1500
/* Starting from a PHI node for the virtual operand of the memory reference
1501
   REF find a continuation virtual operand that allows to continue walking
1502
   statements dominating PHI skipping only statements that cannot possibly
1503
   clobber REF.  Returns NULL_TREE if no suitable virtual operand can
1504
   be found.  */
1505
 
1506
tree
1507
get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1508
{
1509
  unsigned nargs = gimple_phi_num_args (phi);
1510
 
1511
  /* Through a single-argument PHI we can simply look through.  */
1512
  if (nargs == 1)
1513
    return PHI_ARG_DEF (phi, 0);
1514
 
1515
  /* For two arguments try to skip non-aliasing code until we hit
1516
     the phi argument definition that dominates the other one.  */
1517
  if (nargs == 2)
1518
    {
1519
      tree arg0 = PHI_ARG_DEF (phi, 0);
1520
      tree arg1 = PHI_ARG_DEF (phi, 1);
1521
      gimple def0 = SSA_NAME_DEF_STMT (arg0);
1522
      gimple def1 = SSA_NAME_DEF_STMT (arg1);
1523
      tree common_vuse;
1524
 
1525
      if (arg0 == arg1)
1526
        return arg0;
1527
      else if (gimple_nop_p (def0)
1528
               || (!gimple_nop_p (def1)
1529
                   && dominated_by_p (CDI_DOMINATORS,
1530
                                      gimple_bb (def1), gimple_bb (def0))))
1531
        {
1532
          if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1533
            return arg0;
1534
        }
1535
      else if (gimple_nop_p (def1)
1536
               || dominated_by_p (CDI_DOMINATORS,
1537
                                  gimple_bb (def0), gimple_bb (def1)))
1538
        {
1539
          if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1540
            return arg1;
1541
        }
1542
      /* Special case of a diamond:
1543
           MEM_1 = ...
1544
           goto (cond) ? L1 : L2
1545
           L1: store1 = ...    #MEM_2 = vuse(MEM_1)
1546
               goto L3
1547
           L2: store2 = ...    #MEM_3 = vuse(MEM_1)
1548
           L3: MEM_4 = PHI<MEM_2, MEM_3>
1549
         We were called with the PHI at L3, MEM_2 and MEM_3 don't
1550
         dominate each other, but still we can easily skip this PHI node
1551
         if we recognize that the vuse MEM operand is the same for both,
1552
         and that we can skip both statements (they don't clobber us).
1553
         This is still linear.  Don't use maybe_skip_until, that might
1554
         potentially be slow.  */
1555
      else if ((common_vuse = gimple_vuse (def0))
1556
               && common_vuse == gimple_vuse (def1))
1557
        {
1558
          if (!stmt_may_clobber_ref_p_1 (def0, ref)
1559
              && !stmt_may_clobber_ref_p_1 (def1, ref))
1560
            return common_vuse;
1561
        }
1562
    }
1563
 
1564
  return NULL_TREE;
1565
}
1566
 
1567
/* Based on the memory reference REF and its virtual use VUSE call
1568
   WALKER for each virtual use that is equivalent to VUSE, including VUSE
1569
   itself.  That is, for each virtual use for which its defining statement
1570
   does not clobber REF.
1571
 
1572
   WALKER is called with REF, the current virtual use and DATA.  If
1573
   WALKER returns non-NULL the walk stops and its result is returned.
1574
   At the end of a non-successful walk NULL is returned.
1575
 
1576
   TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1577
   use which definition is a statement that may clobber REF and DATA.
1578
   If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1579
   If TRANSLATE returns non-NULL the walk stops and its result is returned.
1580
   If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1581
   to adjust REF and *DATA to make that valid.
1582
 
1583
   TODO: Cache the vector of equivalent vuses per ref, vuse pair.  */
1584
 
1585
void *
1586
walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1587
                        void *(*walker)(ao_ref *, tree, void *),
1588
                        void *(*translate)(ao_ref *, tree, void *), void *data)
1589
{
1590
  bitmap visited = NULL;
1591
  void *res;
1592
 
1593
  timevar_push (TV_ALIAS_STMT_WALK);
1594
 
1595
  do
1596
    {
1597
      gimple def_stmt;
1598
 
1599
      /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1600
      res = (*walker) (ref, vuse, data);
1601
      if (res)
1602
        break;
1603
 
1604
      def_stmt = SSA_NAME_DEF_STMT (vuse);
1605
      if (gimple_nop_p (def_stmt))
1606
        break;
1607
      else if (gimple_code (def_stmt) == GIMPLE_PHI)
1608
        vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1609
      else
1610
        {
1611
          if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1612
            {
1613
              if (!translate)
1614
                break;
1615
              res = (*translate) (ref, vuse, data);
1616
              /* Failed lookup and translation.  */
1617
              if (res == (void *)-1)
1618
                {
1619
                  res = NULL;
1620
                  break;
1621
                }
1622
              /* Lookup succeeded.  */
1623
              else if (res != NULL)
1624
                break;
1625
              /* Translation succeeded, continue walking.  */
1626
            }
1627
          vuse = gimple_vuse (def_stmt);
1628
        }
1629
    }
1630
  while (vuse);
1631
 
1632
  if (visited)
1633
    BITMAP_FREE (visited);
1634
 
1635
  timevar_pop (TV_ALIAS_STMT_WALK);
1636
 
1637
  return res;
1638
}
1639
 
1640
 
1641
/* Based on the memory reference REF call WALKER for each vdef which
1642
   defining statement may clobber REF, starting with VDEF.  If REF
1643
   is NULL_TREE, each defining statement is visited.
1644
 
1645
   WALKER is called with REF, the current vdef and DATA.  If WALKER
1646
   returns true the walk is stopped, otherwise it continues.
1647
 
1648
   At PHI nodes walk_aliased_vdefs forks into one walk for reach
1649
   PHI argument (but only one walk continues on merge points), the
1650
   return value is true if any of the walks was successful.
1651
 
1652
   The function returns the number of statements walked.  */
1653
 
1654
static unsigned int
1655
walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
1656
                      bool (*walker)(ao_ref *, tree, void *), void *data,
1657
                      bitmap *visited, unsigned int cnt)
1658
{
1659
  do
1660
    {
1661
      gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
1662
 
1663
      if (*visited
1664
          && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
1665
        return cnt;
1666
 
1667
      if (gimple_nop_p (def_stmt))
1668
        return cnt;
1669
      else if (gimple_code (def_stmt) == GIMPLE_PHI)
1670
        {
1671
          unsigned i;
1672
          if (!*visited)
1673
            *visited = BITMAP_ALLOC (NULL);
1674
          for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
1675
            cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
1676
                                         walker, data, visited, 0);
1677
          return cnt;
1678
        }
1679
 
1680
      /* ???  Do we want to account this to TV_ALIAS_STMT_WALK?  */
1681
      cnt++;
1682
      if ((!ref
1683
           || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1684
          && (*walker) (ref, vdef, data))
1685
        return cnt;
1686
 
1687
      vdef = gimple_vuse (def_stmt);
1688
    }
1689
  while (1);
1690
}
1691
 
1692
unsigned int
1693
walk_aliased_vdefs (ao_ref *ref, tree vdef,
1694
                    bool (*walker)(ao_ref *, tree, void *), void *data,
1695
                    bitmap *visited)
1696
{
1697
  bitmap local_visited = NULL;
1698
  unsigned int ret;
1699
 
1700
  timevar_push (TV_ALIAS_STMT_WALK);
1701
 
1702
  ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
1703
                              visited ? visited : &local_visited, 0);
1704
  if (local_visited)
1705
    BITMAP_FREE (local_visited);
1706
 
1707
  timevar_pop (TV_ALIAS_STMT_WALK);
1708
 
1709
  return ret;
1710
}
1711
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.