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