82307decaf4982de8042b7e86f93f62e6dcc1fb5
[gcc.git] / gcc / tree-ssa-alias.c
1 /* Alias analysis for trees.
2 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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 "tm_p.h"
28 #include "target.h"
29 #include "basic-block.h"
30 #include "timevar.h"
31 #include "ggc.h"
32 #include "langhooks.h"
33 #include "flags.h"
34 #include "function.h"
35 #include "tree-pretty-print.h"
36 #include "tree-dump.h"
37 #include "gimple.h"
38 #include "tree-flow.h"
39 #include "tree-inline.h"
40 #include "tree-pass.h"
41 #include "convert.h"
42 #include "params.h"
43 #include "vec.h"
44 #include "bitmap.h"
45 #include "vecprim.h"
46 #include "pointer-set.h"
47 #include "alloc-pool.h"
48 #include "tree-ssa-alias.h"
49
50 /* Broad overview of how alias analysis on gimple works:
51
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.
58
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.
65
66 This file contains functions for disambiguating memory references,
67 the so called alias-oracle and tools for walking of the gimple IL.
68
69 The main alias-oracle entry-points are
70
71 bool stmt_may_clobber_ref_p (gimple, tree)
72
73 This function queries if a statement may invalidate (parts of)
74 the memory designated by the reference tree argument.
75
76 bool ref_maybe_used_by_stmt_p (gimple, tree)
77
78 This function queries if a statement may need (parts of) the
79 memory designated by the reference tree argument.
80
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.
84
85 bool refs_may_alias_p (tree, tree)
86
87 This function tries to disambiguate two reference trees.
88
89 bool ptr_deref_may_alias_global_p (tree)
90
91 This function queries if dereferencing a pointer variable may
92 alias global memory.
93
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. */
97
98
99 /* Query statistics for the different low-level disambiguators.
100 A high-level query may trigger multiple of them. */
101
102 static 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;
110
111 void
112 dump_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. */
137
138 bool
139 ptr_deref_may_alias_global_p (tree ptr)
140 {
141 struct ptr_info_def *pi;
142
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;
147
148 pi = SSA_NAME_PTR_INFO (ptr);
149
150 /* If we do not have points-to information for this variable,
151 we have to punt. */
152 if (!pi)
153 return true;
154
155 /* ??? This does not use TBAA to prune globals ptr may not access. */
156 return pt_solution_includes_global (&pi->pt);
157 }
158
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. */
162
163 static bool
164 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
165 {
166 struct ptr_info_def *pi;
167
168 /* Conversions are irrelevant for points-to information and
169 data-dependence analysis can feed us those. */
170 STRIP_NOPS (ptr);
171
172 /* Anything we do not explicilty handle aliases. */
173 if ((TREE_CODE (ptr) != SSA_NAME
174 && TREE_CODE (ptr) != ADDR_EXPR
175 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
176 || !POINTER_TYPE_P (TREE_TYPE (ptr))
177 || (TREE_CODE (decl) != VAR_DECL
178 && TREE_CODE (decl) != PARM_DECL
179 && TREE_CODE (decl) != RESULT_DECL))
180 return true;
181
182 /* Disregard pointer offsetting. */
183 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
184 {
185 do
186 {
187 ptr = TREE_OPERAND (ptr, 0);
188 }
189 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
190 return ptr_deref_may_alias_decl_p (ptr, decl);
191 }
192
193 /* ADDR_EXPR pointers either just offset another pointer or directly
194 specify the pointed-to set. */
195 if (TREE_CODE (ptr) == ADDR_EXPR)
196 {
197 tree base = get_base_address (TREE_OPERAND (ptr, 0));
198 if (base
199 && (TREE_CODE (base) == MEM_REF
200 || TREE_CODE (base) == TARGET_MEM_REF))
201 ptr = TREE_OPERAND (base, 0);
202 else if (base
203 && DECL_P (base))
204 return base == decl;
205 else if (base
206 && CONSTANT_CLASS_P (base))
207 return false;
208 else
209 return true;
210 }
211
212 /* Non-aliased variables can not be pointed to. */
213 if (!may_be_aliased (decl))
214 return false;
215
216 /* If we do not have useful points-to information for this pointer
217 we cannot disambiguate anything else. */
218 pi = SSA_NAME_PTR_INFO (ptr);
219 if (!pi)
220 return true;
221
222 /* If the decl can be used as a restrict tag and we have a restrict
223 pointer and that pointers points-to set doesn't contain this decl
224 then they can't alias. */
225 if (DECL_RESTRICTED_P (decl)
226 && TYPE_RESTRICT (TREE_TYPE (ptr))
227 && pi->pt.vars_contains_restrict)
228 return bitmap_bit_p (pi->pt.vars, DECL_PT_UID (decl));
229
230 return pt_solution_includes (&pi->pt, decl);
231 }
232
233 /* Return true if dereferenced PTR1 and PTR2 may alias.
234 The caller is responsible for applying TBAA to see if accesses
235 through PTR1 and PTR2 may conflict at all. */
236
237 bool
238 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
239 {
240 struct ptr_info_def *pi1, *pi2;
241
242 /* Conversions are irrelevant for points-to information and
243 data-dependence analysis can feed us those. */
244 STRIP_NOPS (ptr1);
245 STRIP_NOPS (ptr2);
246
247 /* Anything we do not explicilty handle aliases. */
248 if ((TREE_CODE (ptr1) != SSA_NAME
249 && TREE_CODE (ptr1) != ADDR_EXPR
250 && TREE_CODE (ptr1) != POINTER_PLUS_EXPR)
251 || (TREE_CODE (ptr2) != SSA_NAME
252 && TREE_CODE (ptr2) != ADDR_EXPR
253 && TREE_CODE (ptr2) != POINTER_PLUS_EXPR)
254 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
255 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
256 return true;
257
258 /* Disregard pointer offsetting. */
259 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
260 {
261 do
262 {
263 ptr1 = TREE_OPERAND (ptr1, 0);
264 }
265 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
266 return ptr_derefs_may_alias_p (ptr1, ptr2);
267 }
268 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
269 {
270 do
271 {
272 ptr2 = TREE_OPERAND (ptr2, 0);
273 }
274 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
275 return ptr_derefs_may_alias_p (ptr1, ptr2);
276 }
277
278 /* ADDR_EXPR pointers either just offset another pointer or directly
279 specify the pointed-to set. */
280 if (TREE_CODE (ptr1) == ADDR_EXPR)
281 {
282 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
283 if (base
284 && (TREE_CODE (base) == MEM_REF
285 || TREE_CODE (base) == TARGET_MEM_REF))
286 ptr1 = TREE_OPERAND (base, 0);
287 else if (base
288 && DECL_P (base))
289 return ptr_deref_may_alias_decl_p (ptr2, base);
290 else
291 return true;
292 }
293 if (TREE_CODE (ptr2) == ADDR_EXPR)
294 {
295 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
296 if (base
297 && (TREE_CODE (base) == MEM_REF
298 || TREE_CODE (base) == TARGET_MEM_REF))
299 ptr2 = TREE_OPERAND (base, 0);
300 else if (base
301 && DECL_P (base))
302 return ptr_deref_may_alias_decl_p (ptr1, base);
303 else
304 return true;
305 }
306
307 /* We may end up with two empty points-to solutions for two same pointers.
308 In this case we still want to say both pointers alias, so shortcut
309 that here. */
310 if (ptr1 == ptr2)
311 return true;
312
313 /* If we do not have useful points-to information for either pointer
314 we cannot disambiguate anything else. */
315 pi1 = SSA_NAME_PTR_INFO (ptr1);
316 pi2 = SSA_NAME_PTR_INFO (ptr2);
317 if (!pi1 || !pi2)
318 return true;
319
320 /* If both pointers are restrict-qualified try to disambiguate
321 with restrict information. */
322 if (TYPE_RESTRICT (TREE_TYPE (ptr1))
323 && TYPE_RESTRICT (TREE_TYPE (ptr2))
324 && !pt_solutions_same_restrict_base (&pi1->pt, &pi2->pt))
325 return false;
326
327 /* ??? This does not use TBAA to prune decls from the intersection
328 that not both pointers may access. */
329 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
330 }
331
332 /* Return true if dereferencing PTR may alias *REF.
333 The caller is responsible for applying TBAA to see if PTR
334 may access *REF at all. */
335
336 static bool
337 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
338 {
339 tree base = ao_ref_base (ref);
340
341 if (TREE_CODE (base) == MEM_REF
342 || TREE_CODE (base) == TARGET_MEM_REF)
343 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
344 else if (DECL_P (base))
345 return ptr_deref_may_alias_decl_p (ptr, base);
346
347 return true;
348 }
349
350
351 /* Dump alias information on FILE. */
352
353 void
354 dump_alias_info (FILE *file)
355 {
356 size_t i;
357 const char *funcname
358 = lang_hooks.decl_printable_name (current_function_decl, 2);
359 referenced_var_iterator rvi;
360 tree var;
361
362 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
363
364 fprintf (file, "Aliased symbols\n\n");
365
366 FOR_EACH_REFERENCED_VAR (cfun, var, rvi)
367 {
368 if (may_be_aliased (var))
369 dump_variable (file, var);
370 }
371
372 fprintf (file, "\nCall clobber information\n");
373
374 fprintf (file, "\nESCAPED");
375 dump_points_to_solution (file, &cfun->gimple_df->escaped);
376
377 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
378
379 for (i = 1; i < num_ssa_names; i++)
380 {
381 tree ptr = ssa_name (i);
382 struct ptr_info_def *pi;
383
384 if (ptr == NULL_TREE
385 || SSA_NAME_IN_FREE_LIST (ptr))
386 continue;
387
388 pi = SSA_NAME_PTR_INFO (ptr);
389 if (pi)
390 dump_points_to_info_for (file, ptr);
391 }
392
393 fprintf (file, "\n");
394 }
395
396
397 /* Dump alias information on stderr. */
398
399 DEBUG_FUNCTION void
400 debug_alias_info (void)
401 {
402 dump_alias_info (stderr);
403 }
404
405
406 /* Dump the points-to set *PT into FILE. */
407
408 void
409 dump_points_to_solution (FILE *file, struct pt_solution *pt)
410 {
411 if (pt->anything)
412 fprintf (file, ", points-to anything");
413
414 if (pt->nonlocal)
415 fprintf (file, ", points-to non-local");
416
417 if (pt->escaped)
418 fprintf (file, ", points-to escaped");
419
420 if (pt->ipa_escaped)
421 fprintf (file, ", points-to unit escaped");
422
423 if (pt->null)
424 fprintf (file, ", points-to NULL");
425
426 if (pt->vars)
427 {
428 fprintf (file, ", points-to vars: ");
429 dump_decl_set (file, pt->vars);
430 if (pt->vars_contains_global)
431 fprintf (file, " (includes global vars)");
432 if (pt->vars_contains_restrict)
433 fprintf (file, " (includes restrict tags)");
434 }
435 }
436
437 /* Dump points-to information for SSA_NAME PTR into FILE. */
438
439 void
440 dump_points_to_info_for (FILE *file, tree ptr)
441 {
442 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
443
444 print_generic_expr (file, ptr, dump_flags);
445
446 if (pi)
447 dump_points_to_solution (file, &pi->pt);
448 else
449 fprintf (file, ", points-to anything");
450
451 fprintf (file, "\n");
452 }
453
454
455 /* Dump points-to information for VAR into stderr. */
456
457 DEBUG_FUNCTION void
458 debug_points_to_info_for (tree var)
459 {
460 dump_points_to_info_for (stderr, var);
461 }
462
463
464 /* Initializes the alias-oracle reference representation *R from REF. */
465
466 void
467 ao_ref_init (ao_ref *r, tree ref)
468 {
469 r->ref = ref;
470 r->base = NULL_TREE;
471 r->offset = 0;
472 r->size = -1;
473 r->max_size = -1;
474 r->ref_alias_set = -1;
475 r->base_alias_set = -1;
476 }
477
478 /* Returns the base object of the memory reference *REF. */
479
480 tree
481 ao_ref_base (ao_ref *ref)
482 {
483 if (ref->base)
484 return ref->base;
485 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
486 &ref->max_size);
487 return ref->base;
488 }
489
490 /* Returns the base object alias set of the memory reference *REF. */
491
492 static alias_set_type
493 ao_ref_base_alias_set (ao_ref *ref)
494 {
495 tree base_ref;
496 if (ref->base_alias_set != -1)
497 return ref->base_alias_set;
498 if (!ref->ref)
499 return 0;
500 base_ref = ref->ref;
501 while (handled_component_p (base_ref))
502 base_ref = TREE_OPERAND (base_ref, 0);
503 ref->base_alias_set = get_alias_set (base_ref);
504 return ref->base_alias_set;
505 }
506
507 /* Returns the reference alias set of the memory reference *REF. */
508
509 alias_set_type
510 ao_ref_alias_set (ao_ref *ref)
511 {
512 if (ref->ref_alias_set != -1)
513 return ref->ref_alias_set;
514 ref->ref_alias_set = get_alias_set (ref->ref);
515 return ref->ref_alias_set;
516 }
517
518 /* Init an alias-oracle reference representation from a gimple pointer
519 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE the the
520 size is assumed to be unknown. The access is assumed to be only
521 to or after of the pointer target, not before it. */
522
523 void
524 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
525 {
526 HOST_WIDE_INT t1, t2;
527 ref->ref = NULL_TREE;
528 if (TREE_CODE (ptr) == ADDR_EXPR)
529 ref->base = get_ref_base_and_extent (TREE_OPERAND (ptr, 0),
530 &ref->offset, &t1, &t2);
531 else
532 {
533 ref->base = build2 (MEM_REF, char_type_node,
534 ptr, null_pointer_node);
535 ref->offset = 0;
536 }
537 if (size
538 && host_integerp (size, 0)
539 && TREE_INT_CST_LOW (size) * 8 / 8 == TREE_INT_CST_LOW (size))
540 ref->max_size = ref->size = TREE_INT_CST_LOW (size) * 8;
541 else
542 ref->max_size = ref->size = -1;
543 ref->ref_alias_set = 0;
544 ref->base_alias_set = 0;
545 }
546
547 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
548 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
549 decide. */
550
551 static inline int
552 same_type_for_tbaa (tree type1, tree type2)
553 {
554 type1 = TYPE_MAIN_VARIANT (type1);
555 type2 = TYPE_MAIN_VARIANT (type2);
556
557 /* If we would have to do structural comparison bail out. */
558 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
559 || TYPE_STRUCTURAL_EQUALITY_P (type2))
560 return -1;
561
562 /* Compare the canonical types. */
563 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
564 return 1;
565
566 /* ??? Array types are not properly unified in all cases as we have
567 spurious changes in the index types for example. Removing this
568 causes all sorts of problems with the Fortran frontend. */
569 if (TREE_CODE (type1) == ARRAY_TYPE
570 && TREE_CODE (type2) == ARRAY_TYPE)
571 return -1;
572
573 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
574 object of one of its constrained subtypes, e.g. when a function with an
575 unconstrained parameter passed by reference is called on an object and
576 inlined. But, even in the case of a fixed size, type and subtypes are
577 not equivalent enough as to share the same TYPE_CANONICAL, since this
578 would mean that conversions between them are useless, whereas they are
579 not (e.g. type and subtypes can have different modes). So, in the end,
580 they are only guaranteed to have the same alias set. */
581 if (get_alias_set (type1) == get_alias_set (type2))
582 return -1;
583
584 /* The types are known to be not equal. */
585 return 0;
586 }
587
588 /* Determine if the two component references REF1 and REF2 which are
589 based on access types TYPE1 and TYPE2 and of which at least one is based
590 on an indirect reference may alias. REF2 is the only one that can
591 be a decl in which case REF2_IS_DECL is true.
592 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
593 are the respective alias sets. */
594
595 static bool
596 aliasing_component_refs_p (tree ref1,
597 alias_set_type ref1_alias_set,
598 alias_set_type base1_alias_set,
599 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
600 tree ref2,
601 alias_set_type ref2_alias_set,
602 alias_set_type base2_alias_set,
603 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
604 bool ref2_is_decl)
605 {
606 /* If one reference is a component references through pointers try to find a
607 common base and apply offset based disambiguation. This handles
608 for example
609 struct A { int i; int j; } *q;
610 struct B { struct A a; int k; } *p;
611 disambiguating q->i and p->a.j. */
612 tree base1, base2;
613 tree type1, type2;
614 tree *refp;
615 int same_p;
616
617 /* Choose bases and base types to search for. */
618 base1 = ref1;
619 while (handled_component_p (base1))
620 base1 = TREE_OPERAND (base1, 0);
621 type1 = TREE_TYPE (base1);
622 base2 = ref2;
623 while (handled_component_p (base2))
624 base2 = TREE_OPERAND (base2, 0);
625 type2 = TREE_TYPE (base2);
626
627 /* Now search for the type1 in the access path of ref2. This
628 would be a common base for doing offset based disambiguation on. */
629 refp = &ref2;
630 while (handled_component_p (*refp)
631 && same_type_for_tbaa (TREE_TYPE (*refp), type1) == 0)
632 refp = &TREE_OPERAND (*refp, 0);
633 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type1);
634 /* If we couldn't compare types we have to bail out. */
635 if (same_p == -1)
636 return true;
637 else if (same_p == 1)
638 {
639 HOST_WIDE_INT offadj, sztmp, msztmp;
640 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
641 offset2 -= offadj;
642 get_ref_base_and_extent (base1, &offadj, &sztmp, &msztmp);
643 offset1 -= offadj;
644 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
645 }
646 /* If we didn't find a common base, try the other way around. */
647 refp = &ref1;
648 while (handled_component_p (*refp)
649 && same_type_for_tbaa (TREE_TYPE (*refp), type2) == 0)
650 refp = &TREE_OPERAND (*refp, 0);
651 same_p = same_type_for_tbaa (TREE_TYPE (*refp), type2);
652 /* If we couldn't compare types we have to bail out. */
653 if (same_p == -1)
654 return true;
655 else if (same_p == 1)
656 {
657 HOST_WIDE_INT offadj, sztmp, msztmp;
658 get_ref_base_and_extent (*refp, &offadj, &sztmp, &msztmp);
659 offset1 -= offadj;
660 get_ref_base_and_extent (base2, &offadj, &sztmp, &msztmp);
661 offset2 -= offadj;
662 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
663 }
664
665 /* If we have two type access paths B1.path1 and B2.path2 they may
666 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
667 But we can still have a path that goes B1.path1...B2.path2 with
668 a part that we do not see. So we can only disambiguate now
669 if there is no B2 in the tail of path1 and no B1 on the
670 tail of path2. */
671 if (base1_alias_set == ref2_alias_set
672 || alias_set_subset_of (base1_alias_set, ref2_alias_set))
673 return true;
674 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
675 if (!ref2_is_decl)
676 return (base2_alias_set == ref1_alias_set
677 || alias_set_subset_of (base2_alias_set, ref1_alias_set));
678 return false;
679 }
680
681 /* Return true if two memory references based on the variables BASE1
682 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
683 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. */
684
685 static bool
686 decl_refs_may_alias_p (tree base1,
687 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
688 tree base2,
689 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2)
690 {
691 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
692
693 /* If both references are based on different variables, they cannot alias. */
694 if (base1 != base2)
695 return false;
696
697 /* If both references are based on the same variable, they cannot alias if
698 the accesses do not overlap. */
699 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
700 }
701
702 /* Return true if an indirect reference based on *PTR1 constrained
703 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
704 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
705 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
706 in which case they are computed on-demand. REF1 and REF2
707 if non-NULL are the complete memory reference trees. */
708
709 static bool
710 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
711 HOST_WIDE_INT offset1,
712 HOST_WIDE_INT max_size1 ATTRIBUTE_UNUSED,
713 alias_set_type ref1_alias_set,
714 alias_set_type base1_alias_set,
715 tree ref2 ATTRIBUTE_UNUSED, tree base2,
716 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
717 alias_set_type ref2_alias_set,
718 alias_set_type base2_alias_set, bool tbaa_p)
719 {
720 tree ptr1;
721 tree ptrtype1, dbase2;
722 HOST_WIDE_INT offset1p = offset1, offset2p = offset2;
723 HOST_WIDE_INT doffset1, doffset2;
724 double_int moff;
725
726 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
727 || TREE_CODE (base1) == TARGET_MEM_REF)
728 && DECL_P (base2));
729
730 ptr1 = TREE_OPERAND (base1, 0);
731
732 /* The offset embedded in MEM_REFs can be negative. Bias them
733 so that the resulting offset adjustment is positive. */
734 moff = mem_ref_offset (base1);
735 moff = double_int_lshift (moff,
736 BITS_PER_UNIT == 8
737 ? 3 : exact_log2 (BITS_PER_UNIT),
738 HOST_BITS_PER_DOUBLE_INT, true);
739 if (double_int_negative_p (moff))
740 offset2p += double_int_neg (moff).low;
741 else
742 offset1p += moff.low;
743
744 /* If only one reference is based on a variable, they cannot alias if
745 the pointer access is beyond the extent of the variable access.
746 (the pointer base cannot validly point to an offset less than zero
747 of the variable).
748 ??? IVOPTs creates bases that do not honor this restriction,
749 so do not apply this optimization for TARGET_MEM_REFs. */
750 if (TREE_CODE (base1) != TARGET_MEM_REF
751 && !ranges_overlap_p (MAX (0, offset1p), -1, offset2p, max_size2))
752 return false;
753 /* They also cannot alias if the pointer may not point to the decl. */
754 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
755 return false;
756
757 /* Disambiguations that rely on strict aliasing rules follow. */
758 if (!flag_strict_aliasing || !tbaa_p)
759 return true;
760
761 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
762
763 /* If the alias set for a pointer access is zero all bets are off. */
764 if (base1_alias_set == -1)
765 base1_alias_set = get_deref_alias_set (ptrtype1);
766 if (base1_alias_set == 0)
767 return true;
768 if (base2_alias_set == -1)
769 base2_alias_set = get_alias_set (base2);
770
771 /* When we are trying to disambiguate an access with a pointer dereference
772 as base versus one with a decl as base we can use both the size
773 of the decl and its dynamic type for extra disambiguation.
774 ??? We do not know anything about the dynamic type of the decl
775 other than that its alias-set contains base2_alias_set as a subset
776 which does not help us here. */
777 /* As we know nothing useful about the dynamic type of the decl just
778 use the usual conflict check rather than a subset test.
779 ??? We could introduce -fvery-strict-aliasing when the language
780 does not allow decls to have a dynamic type that differs from their
781 static type. Then we can check
782 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
783 if (base1_alias_set != base2_alias_set
784 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
785 return false;
786 /* If the size of the access relevant for TBAA through the pointer
787 is bigger than the size of the decl we can't possibly access the
788 decl via that pointer. */
789 if (DECL_SIZE (base2) && COMPLETE_TYPE_P (TREE_TYPE (ptrtype1))
790 && TREE_CODE (DECL_SIZE (base2)) == INTEGER_CST
791 && TREE_CODE (TYPE_SIZE (TREE_TYPE (ptrtype1))) == INTEGER_CST
792 /* ??? This in turn may run afoul when a decl of type T which is
793 a member of union type U is accessed through a pointer to
794 type U and sizeof T is smaller than sizeof U. */
795 && TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
796 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
797 && tree_int_cst_lt (DECL_SIZE (base2), TYPE_SIZE (TREE_TYPE (ptrtype1))))
798 return false;
799
800 if (!ref2)
801 return true;
802
803 /* If the decl is accessed via a MEM_REF, reconstruct the base
804 we can use for TBAA and an appropriately adjusted offset. */
805 dbase2 = ref2;
806 while (handled_component_p (dbase2))
807 dbase2 = TREE_OPERAND (dbase2, 0);
808 doffset1 = offset1;
809 doffset2 = offset2;
810 if (TREE_CODE (dbase2) == MEM_REF
811 || TREE_CODE (dbase2) == TARGET_MEM_REF)
812 {
813 double_int moff = mem_ref_offset (dbase2);
814 moff = double_int_lshift (moff,
815 BITS_PER_UNIT == 8
816 ? 3 : exact_log2 (BITS_PER_UNIT),
817 HOST_BITS_PER_DOUBLE_INT, true);
818 if (double_int_negative_p (moff))
819 doffset1 -= double_int_neg (moff).low;
820 else
821 doffset2 -= moff.low;
822 }
823
824 /* If either reference is view-converted, give up now. */
825 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
826 || same_type_for_tbaa (TREE_TYPE (dbase2),
827 TREE_TYPE (reference_alias_ptr_type (dbase2))) != 1)
828 return true;
829
830 /* If both references are through the same type, they do not alias
831 if the accesses do not overlap. This does extra disambiguation
832 for mixed/pointer accesses but requires strict aliasing.
833 For MEM_REFs we require that the component-ref offset we computed
834 is relative to the start of the type which we ensure by
835 comparing rvalue and access type and disregarding the constant
836 pointer offset. */
837 if ((TREE_CODE (base1) != TARGET_MEM_REF
838 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
839 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
840 return ranges_overlap_p (doffset1, max_size1, doffset2, max_size2);
841
842 /* Do access-path based disambiguation. */
843 if (ref1 && ref2
844 && (handled_component_p (ref1) || handled_component_p (ref2)))
845 return aliasing_component_refs_p (ref1,
846 ref1_alias_set, base1_alias_set,
847 offset1, max_size1,
848 ref2,
849 ref2_alias_set, base2_alias_set,
850 offset2, max_size2, true);
851
852 return true;
853 }
854
855 /* Return true if two indirect references based on *PTR1
856 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
857 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
858 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
859 in which case they are computed on-demand. REF1 and REF2
860 if non-NULL are the complete memory reference trees. */
861
862 static bool
863 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
864 HOST_WIDE_INT offset1, HOST_WIDE_INT max_size1,
865 alias_set_type ref1_alias_set,
866 alias_set_type base1_alias_set,
867 tree ref2 ATTRIBUTE_UNUSED, tree base2,
868 HOST_WIDE_INT offset2, HOST_WIDE_INT max_size2,
869 alias_set_type ref2_alias_set,
870 alias_set_type base2_alias_set, bool tbaa_p)
871 {
872 tree ptr1;
873 tree ptr2;
874 tree ptrtype1, ptrtype2;
875
876 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
877 || TREE_CODE (base1) == TARGET_MEM_REF)
878 && (TREE_CODE (base2) == MEM_REF
879 || TREE_CODE (base2) == TARGET_MEM_REF));
880
881 ptr1 = TREE_OPERAND (base1, 0);
882 ptr2 = TREE_OPERAND (base2, 0);
883
884 /* If both bases are based on pointers they cannot alias if they may not
885 point to the same memory object or if they point to the same object
886 and the accesses do not overlap. */
887 if ((!cfun || gimple_in_ssa_p (cfun))
888 && operand_equal_p (ptr1, ptr2, 0)
889 && (((TREE_CODE (base1) != TARGET_MEM_REF
890 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
891 && (TREE_CODE (base2) != TARGET_MEM_REF
892 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
893 || (TREE_CODE (base1) == TARGET_MEM_REF
894 && TREE_CODE (base2) == TARGET_MEM_REF
895 && (TMR_STEP (base1) == TMR_STEP (base2)
896 || (TMR_STEP (base1) && TMR_STEP (base2)
897 && operand_equal_p (TMR_STEP (base1),
898 TMR_STEP (base2), 0)))
899 && (TMR_INDEX (base1) == TMR_INDEX (base2)
900 || (TMR_INDEX (base1) && TMR_INDEX (base2)
901 && operand_equal_p (TMR_INDEX (base1),
902 TMR_INDEX (base2), 0)))
903 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
904 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
905 && operand_equal_p (TMR_INDEX2 (base1),
906 TMR_INDEX2 (base2), 0))))))
907 {
908 double_int moff;
909 /* The offset embedded in MEM_REFs can be negative. Bias them
910 so that the resulting offset adjustment is positive. */
911 moff = mem_ref_offset (base1);
912 moff = double_int_lshift (moff,
913 BITS_PER_UNIT == 8
914 ? 3 : exact_log2 (BITS_PER_UNIT),
915 HOST_BITS_PER_DOUBLE_INT, true);
916 if (double_int_negative_p (moff))
917 offset2 += double_int_neg (moff).low;
918 else
919 offset1 += moff.low;
920 moff = mem_ref_offset (base2);
921 moff = double_int_lshift (moff,
922 BITS_PER_UNIT == 8
923 ? 3 : exact_log2 (BITS_PER_UNIT),
924 HOST_BITS_PER_DOUBLE_INT, true);
925 if (double_int_negative_p (moff))
926 offset1 += double_int_neg (moff).low;
927 else
928 offset2 += moff.low;
929 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
930 }
931 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
932 return false;
933
934 /* Disambiguations that rely on strict aliasing rules follow. */
935 if (!flag_strict_aliasing || !tbaa_p)
936 return true;
937
938 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
939 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
940
941 /* If the alias set for a pointer access is zero all bets are off. */
942 if (base1_alias_set == -1)
943 base1_alias_set = get_deref_alias_set (ptrtype1);
944 if (base1_alias_set == 0)
945 return true;
946 if (base2_alias_set == -1)
947 base2_alias_set = get_deref_alias_set (ptrtype2);
948 if (base2_alias_set == 0)
949 return true;
950
951 /* If both references are through the same type, they do not alias
952 if the accesses do not overlap. This does extra disambiguation
953 for mixed/pointer accesses but requires strict aliasing. */
954 if ((TREE_CODE (base1) != TARGET_MEM_REF
955 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
956 && (TREE_CODE (base2) != TARGET_MEM_REF
957 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
958 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
959 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1
960 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
961 TREE_TYPE (ptrtype2)) == 1)
962 return ranges_overlap_p (offset1, max_size1, offset2, max_size2);
963
964 /* Do type-based disambiguation. */
965 if (base1_alias_set != base2_alias_set
966 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
967 return false;
968
969 /* Do access-path based disambiguation. */
970 if (ref1 && ref2
971 && (handled_component_p (ref1) || handled_component_p (ref2))
972 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) == 1
973 && same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) == 1)
974 return aliasing_component_refs_p (ref1,
975 ref1_alias_set, base1_alias_set,
976 offset1, max_size1,
977 ref2,
978 ref2_alias_set, base2_alias_set,
979 offset2, max_size2, false);
980
981 return true;
982 }
983
984 /* Return true, if the two memory references REF1 and REF2 may alias. */
985
986 bool
987 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
988 {
989 tree base1, base2;
990 HOST_WIDE_INT offset1 = 0, offset2 = 0;
991 HOST_WIDE_INT max_size1 = -1, max_size2 = -1;
992 bool var1_p, var2_p, ind1_p, ind2_p;
993
994 gcc_checking_assert ((!ref1->ref
995 || TREE_CODE (ref1->ref) == SSA_NAME
996 || DECL_P (ref1->ref)
997 || TREE_CODE (ref1->ref) == STRING_CST
998 || handled_component_p (ref1->ref)
999 || TREE_CODE (ref1->ref) == MEM_REF
1000 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1001 && (!ref2->ref
1002 || TREE_CODE (ref2->ref) == SSA_NAME
1003 || DECL_P (ref2->ref)
1004 || TREE_CODE (ref2->ref) == STRING_CST
1005 || handled_component_p (ref2->ref)
1006 || TREE_CODE (ref2->ref) == MEM_REF
1007 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1008
1009 /* Decompose the references into their base objects and the access. */
1010 base1 = ao_ref_base (ref1);
1011 offset1 = ref1->offset;
1012 max_size1 = ref1->max_size;
1013 base2 = ao_ref_base (ref2);
1014 offset2 = ref2->offset;
1015 max_size2 = ref2->max_size;
1016
1017 /* We can end up with registers or constants as bases for example from
1018 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1019 which is seen as a struct copy. */
1020 if (TREE_CODE (base1) == SSA_NAME
1021 || TREE_CODE (base1) == CONST_DECL
1022 || TREE_CODE (base1) == CONSTRUCTOR
1023 || TREE_CODE (base1) == ADDR_EXPR
1024 || CONSTANT_CLASS_P (base1)
1025 || TREE_CODE (base2) == SSA_NAME
1026 || TREE_CODE (base2) == CONST_DECL
1027 || TREE_CODE (base2) == CONSTRUCTOR
1028 || TREE_CODE (base2) == ADDR_EXPR
1029 || CONSTANT_CLASS_P (base2))
1030 return false;
1031
1032 /* We can end up refering to code via function and label decls.
1033 As we likely do not properly track code aliases conservatively
1034 bail out. */
1035 if (TREE_CODE (base1) == FUNCTION_DECL
1036 || TREE_CODE (base1) == LABEL_DECL
1037 || TREE_CODE (base2) == FUNCTION_DECL
1038 || TREE_CODE (base2) == LABEL_DECL)
1039 return true;
1040
1041 /* Defer to simple offset based disambiguation if we have
1042 references based on two decls. Do this before defering to
1043 TBAA to handle must-alias cases in conformance with the
1044 GCC extension of allowing type-punning through unions. */
1045 var1_p = DECL_P (base1);
1046 var2_p = DECL_P (base2);
1047 if (var1_p && var2_p)
1048 return decl_refs_may_alias_p (base1, offset1, max_size1,
1049 base2, offset2, max_size2);
1050
1051 ind1_p = (TREE_CODE (base1) == MEM_REF
1052 || TREE_CODE (base1) == TARGET_MEM_REF);
1053 ind2_p = (TREE_CODE (base2) == MEM_REF
1054 || TREE_CODE (base2) == TARGET_MEM_REF);
1055
1056 /* Canonicalize the pointer-vs-decl case. */
1057 if (ind1_p && var2_p)
1058 {
1059 HOST_WIDE_INT tmp1;
1060 tree tmp2;
1061 ao_ref *tmp3;
1062 tmp1 = offset1; offset1 = offset2; offset2 = tmp1;
1063 tmp1 = max_size1; max_size1 = max_size2; max_size2 = tmp1;
1064 tmp2 = base1; base1 = base2; base2 = tmp2;
1065 tmp3 = ref1; ref1 = ref2; ref2 = tmp3;
1066 var1_p = true;
1067 ind1_p = false;
1068 var2_p = false;
1069 ind2_p = true;
1070 }
1071
1072 /* First defer to TBAA if possible. */
1073 if (tbaa_p
1074 && flag_strict_aliasing
1075 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
1076 ao_ref_alias_set (ref2)))
1077 return false;
1078
1079 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
1080 if (var1_p && ind2_p)
1081 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
1082 offset2, max_size2,
1083 ao_ref_alias_set (ref2), -1,
1084 ref1->ref, base1,
1085 offset1, max_size1,
1086 ao_ref_alias_set (ref1),
1087 ao_ref_base_alias_set (ref1),
1088 tbaa_p);
1089 else if (ind1_p && ind2_p)
1090 return indirect_refs_may_alias_p (ref1->ref, base1,
1091 offset1, max_size1,
1092 ao_ref_alias_set (ref1), -1,
1093 ref2->ref, base2,
1094 offset2, max_size2,
1095 ao_ref_alias_set (ref2), -1,
1096 tbaa_p);
1097
1098 /* We really do not want to end up here, but returning true is safe. */
1099 #ifdef ENABLE_CHECKING
1100 gcc_unreachable ();
1101 #else
1102 return true;
1103 #endif
1104 }
1105
1106 bool
1107 refs_may_alias_p (tree ref1, tree ref2)
1108 {
1109 ao_ref r1, r2;
1110 bool res;
1111 ao_ref_init (&r1, ref1);
1112 ao_ref_init (&r2, ref2);
1113 res = refs_may_alias_p_1 (&r1, &r2, true);
1114 if (res)
1115 ++alias_stats.refs_may_alias_p_may_alias;
1116 else
1117 ++alias_stats.refs_may_alias_p_no_alias;
1118 return res;
1119 }
1120
1121 /* Returns true if there is a anti-dependence for the STORE that
1122 executes after the LOAD. */
1123
1124 bool
1125 refs_anti_dependent_p (tree load, tree store)
1126 {
1127 ao_ref r1, r2;
1128 ao_ref_init (&r1, load);
1129 ao_ref_init (&r2, store);
1130 return refs_may_alias_p_1 (&r1, &r2, false);
1131 }
1132
1133 /* Returns true if there is a output dependence for the stores
1134 STORE1 and STORE2. */
1135
1136 bool
1137 refs_output_dependent_p (tree store1, tree store2)
1138 {
1139 ao_ref r1, r2;
1140 ao_ref_init (&r1, store1);
1141 ao_ref_init (&r2, store2);
1142 return refs_may_alias_p_1 (&r1, &r2, false);
1143 }
1144
1145 /* If the call CALL may use the memory reference REF return true,
1146 otherwise return false. */
1147
1148 static bool
1149 ref_maybe_used_by_call_p_1 (gimple call, ao_ref *ref)
1150 {
1151 tree base, callee;
1152 unsigned i;
1153 int flags = gimple_call_flags (call);
1154
1155 /* Const functions without a static chain do not implicitly use memory. */
1156 if (!gimple_call_chain (call)
1157 && (flags & (ECF_CONST|ECF_NOVOPS)))
1158 goto process_args;
1159
1160 base = ao_ref_base (ref);
1161 if (!base)
1162 return true;
1163
1164 /* If the reference is based on a decl that is not aliased the call
1165 cannot possibly use it. */
1166 if (DECL_P (base)
1167 && !may_be_aliased (base)
1168 /* But local statics can be used through recursion. */
1169 && !is_global_var (base))
1170 goto process_args;
1171
1172 callee = gimple_call_fndecl (call);
1173
1174 /* Handle those builtin functions explicitly that do not act as
1175 escape points. See tree-ssa-structalias.c:find_func_aliases
1176 for the list of builtins we might need to handle here. */
1177 if (callee != NULL_TREE
1178 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1179 switch (DECL_FUNCTION_CODE (callee))
1180 {
1181 /* All the following functions read memory pointed to by
1182 their second argument. strcat/strncat additionally
1183 reads memory pointed to by the first argument. */
1184 case BUILT_IN_STRCAT:
1185 case BUILT_IN_STRNCAT:
1186 {
1187 ao_ref dref;
1188 ao_ref_init_from_ptr_and_size (&dref,
1189 gimple_call_arg (call, 0),
1190 NULL_TREE);
1191 if (refs_may_alias_p_1 (&dref, ref, false))
1192 return true;
1193 }
1194 /* FALLTHRU */
1195 case BUILT_IN_STRCPY:
1196 case BUILT_IN_STRNCPY:
1197 case BUILT_IN_MEMCPY:
1198 case BUILT_IN_MEMMOVE:
1199 case BUILT_IN_MEMPCPY:
1200 case BUILT_IN_STPCPY:
1201 case BUILT_IN_STPNCPY:
1202 {
1203 ao_ref dref;
1204 tree size = NULL_TREE;
1205 if (gimple_call_num_args (call) == 3)
1206 size = gimple_call_arg (call, 2);
1207 ao_ref_init_from_ptr_and_size (&dref,
1208 gimple_call_arg (call, 1),
1209 size);
1210 return refs_may_alias_p_1 (&dref, ref, false);
1211 }
1212 case BUILT_IN_STRCAT_CHK:
1213 case BUILT_IN_STRNCAT_CHK:
1214 {
1215 ao_ref dref;
1216 ao_ref_init_from_ptr_and_size (&dref,
1217 gimple_call_arg (call, 0),
1218 NULL_TREE);
1219 if (refs_may_alias_p_1 (&dref, ref, false))
1220 return true;
1221 }
1222 /* FALLTHRU */
1223 case BUILT_IN_STRCPY_CHK:
1224 case BUILT_IN_STRNCPY_CHK:
1225 case BUILT_IN_MEMCPY_CHK:
1226 case BUILT_IN_MEMMOVE_CHK:
1227 case BUILT_IN_MEMPCPY_CHK:
1228 case BUILT_IN_STPCPY_CHK:
1229 {
1230 ao_ref dref;
1231 tree size = NULL_TREE;
1232 if (gimple_call_num_args (call) == 4)
1233 size = gimple_call_arg (call, 2);
1234 ao_ref_init_from_ptr_and_size (&dref,
1235 gimple_call_arg (call, 1),
1236 size);
1237 return refs_may_alias_p_1 (&dref, ref, false);
1238 }
1239 case BUILT_IN_BCOPY:
1240 {
1241 ao_ref dref;
1242 tree size = gimple_call_arg (call, 2);
1243 ao_ref_init_from_ptr_and_size (&dref,
1244 gimple_call_arg (call, 0),
1245 size);
1246 return refs_may_alias_p_1 (&dref, ref, false);
1247 }
1248 /* These read memory pointed to by the first argument. */
1249 case BUILT_IN_STRDUP:
1250 case BUILT_IN_STRNDUP:
1251 {
1252 ao_ref dref;
1253 tree size = NULL_TREE;
1254 if (gimple_call_num_args (call) == 2)
1255 size = gimple_call_arg (call, 1);
1256 ao_ref_init_from_ptr_and_size (&dref,
1257 gimple_call_arg (call, 0),
1258 size);
1259 return refs_may_alias_p_1 (&dref, ref, false);
1260 }
1261 /* The following builtins do not read from memory. */
1262 case BUILT_IN_FREE:
1263 case BUILT_IN_MALLOC:
1264 case BUILT_IN_CALLOC:
1265 case BUILT_IN_ALLOCA:
1266 case BUILT_IN_STACK_SAVE:
1267 case BUILT_IN_STACK_RESTORE:
1268 case BUILT_IN_MEMSET:
1269 case BUILT_IN_MEMSET_CHK:
1270 case BUILT_IN_FREXP:
1271 case BUILT_IN_FREXPF:
1272 case BUILT_IN_FREXPL:
1273 case BUILT_IN_GAMMA_R:
1274 case BUILT_IN_GAMMAF_R:
1275 case BUILT_IN_GAMMAL_R:
1276 case BUILT_IN_LGAMMA_R:
1277 case BUILT_IN_LGAMMAF_R:
1278 case BUILT_IN_LGAMMAL_R:
1279 case BUILT_IN_MODF:
1280 case BUILT_IN_MODFF:
1281 case BUILT_IN_MODFL:
1282 case BUILT_IN_REMQUO:
1283 case BUILT_IN_REMQUOF:
1284 case BUILT_IN_REMQUOL:
1285 case BUILT_IN_SINCOS:
1286 case BUILT_IN_SINCOSF:
1287 case BUILT_IN_SINCOSL:
1288 case BUILT_IN_ASSUME_ALIGNED:
1289 case BUILT_IN_VA_END:
1290 return false;
1291 /* __sync_* builtins and some OpenMP builtins act as threading
1292 barriers. */
1293 #undef DEF_SYNC_BUILTIN
1294 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1295 #include "sync-builtins.def"
1296 #undef DEF_SYNC_BUILTIN
1297 case BUILT_IN_GOMP_ATOMIC_START:
1298 case BUILT_IN_GOMP_ATOMIC_END:
1299 case BUILT_IN_GOMP_BARRIER:
1300 case BUILT_IN_GOMP_TASKWAIT:
1301 case BUILT_IN_GOMP_CRITICAL_START:
1302 case BUILT_IN_GOMP_CRITICAL_END:
1303 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1304 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1305 case BUILT_IN_GOMP_LOOP_END:
1306 case BUILT_IN_GOMP_ORDERED_START:
1307 case BUILT_IN_GOMP_ORDERED_END:
1308 case BUILT_IN_GOMP_PARALLEL_END:
1309 case BUILT_IN_GOMP_SECTIONS_END:
1310 case BUILT_IN_GOMP_SINGLE_COPY_START:
1311 case BUILT_IN_GOMP_SINGLE_COPY_END:
1312 return true;
1313
1314 default:
1315 /* Fallthru to general call handling. */;
1316 }
1317
1318 /* Check if base is a global static variable that is not read
1319 by the function. */
1320 if (callee != NULL_TREE
1321 && TREE_CODE (base) == VAR_DECL
1322 && TREE_STATIC (base))
1323 {
1324 struct cgraph_node *node = cgraph_get_node (callee);
1325 bitmap not_read;
1326
1327 /* FIXME: Callee can be an OMP builtin that does not have a call graph
1328 node yet. We should enforce that there are nodes for all decls in the
1329 IL and remove this check instead. */
1330 if (node
1331 && (not_read = ipa_reference_get_not_read_global (node))
1332 && bitmap_bit_p (not_read, DECL_UID (base)))
1333 goto process_args;
1334 }
1335
1336 /* Check if the base variable is call-used. */
1337 if (DECL_P (base))
1338 {
1339 if (pt_solution_includes (gimple_call_use_set (call), base))
1340 return true;
1341 }
1342 else if ((TREE_CODE (base) == MEM_REF
1343 || TREE_CODE (base) == TARGET_MEM_REF)
1344 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1345 {
1346 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1347 if (!pi)
1348 return true;
1349
1350 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
1351 return true;
1352 }
1353 else
1354 return true;
1355
1356 /* Inspect call arguments for passed-by-value aliases. */
1357 process_args:
1358 for (i = 0; i < gimple_call_num_args (call); ++i)
1359 {
1360 tree op = gimple_call_arg (call, i);
1361 int flags = gimple_call_arg_flags (call, i);
1362
1363 if (flags & EAF_UNUSED)
1364 continue;
1365
1366 if (TREE_CODE (op) == WITH_SIZE_EXPR)
1367 op = TREE_OPERAND (op, 0);
1368
1369 if (TREE_CODE (op) != SSA_NAME
1370 && !is_gimple_min_invariant (op))
1371 {
1372 ao_ref r;
1373 ao_ref_init (&r, op);
1374 if (refs_may_alias_p_1 (&r, ref, true))
1375 return true;
1376 }
1377 }
1378
1379 return false;
1380 }
1381
1382 static bool
1383 ref_maybe_used_by_call_p (gimple call, tree ref)
1384 {
1385 ao_ref r;
1386 bool res;
1387 ao_ref_init (&r, ref);
1388 res = ref_maybe_used_by_call_p_1 (call, &r);
1389 if (res)
1390 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
1391 else
1392 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
1393 return res;
1394 }
1395
1396
1397 /* If the statement STMT may use the memory reference REF return
1398 true, otherwise return false. */
1399
1400 bool
1401 ref_maybe_used_by_stmt_p (gimple stmt, tree ref)
1402 {
1403 if (is_gimple_assign (stmt))
1404 {
1405 tree rhs;
1406
1407 /* All memory assign statements are single. */
1408 if (!gimple_assign_single_p (stmt))
1409 return false;
1410
1411 rhs = gimple_assign_rhs1 (stmt);
1412 if (is_gimple_reg (rhs)
1413 || is_gimple_min_invariant (rhs)
1414 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
1415 return false;
1416
1417 return refs_may_alias_p (rhs, ref);
1418 }
1419 else if (is_gimple_call (stmt))
1420 return ref_maybe_used_by_call_p (stmt, ref);
1421 else if (gimple_code (stmt) == GIMPLE_RETURN)
1422 {
1423 tree retval = gimple_return_retval (stmt);
1424 tree base;
1425 if (retval
1426 && TREE_CODE (retval) != SSA_NAME
1427 && !is_gimple_min_invariant (retval)
1428 && refs_may_alias_p (retval, ref))
1429 return true;
1430 /* If ref escapes the function then the return acts as a use. */
1431 base = get_base_address (ref);
1432 if (!base)
1433 ;
1434 else if (DECL_P (base))
1435 return is_global_var (base);
1436 else if (TREE_CODE (base) == MEM_REF
1437 || TREE_CODE (base) == TARGET_MEM_REF)
1438 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
1439 return false;
1440 }
1441
1442 return true;
1443 }
1444
1445 /* If the call in statement CALL may clobber the memory reference REF
1446 return true, otherwise return false. */
1447
1448 static bool
1449 call_may_clobber_ref_p_1 (gimple call, ao_ref *ref)
1450 {
1451 tree base;
1452 tree callee;
1453
1454 /* If the call is pure or const it cannot clobber anything. */
1455 if (gimple_call_flags (call)
1456 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
1457 return false;
1458
1459 base = ao_ref_base (ref);
1460 if (!base)
1461 return true;
1462
1463 if (TREE_CODE (base) == SSA_NAME
1464 || CONSTANT_CLASS_P (base))
1465 return false;
1466
1467 /* If the reference is based on a decl that is not aliased the call
1468 cannot possibly clobber it. */
1469 if (DECL_P (base)
1470 && !may_be_aliased (base)
1471 /* But local non-readonly statics can be modified through recursion
1472 or the call may implement a threading barrier which we must
1473 treat as may-def. */
1474 && (TREE_READONLY (base)
1475 || !is_global_var (base)))
1476 return false;
1477
1478 callee = gimple_call_fndecl (call);
1479
1480 /* Handle those builtin functions explicitly that do not act as
1481 escape points. See tree-ssa-structalias.c:find_func_aliases
1482 for the list of builtins we might need to handle here. */
1483 if (callee != NULL_TREE
1484 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1485 switch (DECL_FUNCTION_CODE (callee))
1486 {
1487 /* All the following functions clobber memory pointed to by
1488 their first argument. */
1489 case BUILT_IN_STRCPY:
1490 case BUILT_IN_STRNCPY:
1491 case BUILT_IN_MEMCPY:
1492 case BUILT_IN_MEMMOVE:
1493 case BUILT_IN_MEMPCPY:
1494 case BUILT_IN_STPCPY:
1495 case BUILT_IN_STPNCPY:
1496 case BUILT_IN_STRCAT:
1497 case BUILT_IN_STRNCAT:
1498 case BUILT_IN_MEMSET:
1499 {
1500 ao_ref dref;
1501 tree size = NULL_TREE;
1502 /* Don't pass in size for strncat, as the maximum size
1503 is strlen (dest) + n + 1 instead of n, resp.
1504 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1505 known. */
1506 if (gimple_call_num_args (call) == 3
1507 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
1508 size = gimple_call_arg (call, 2);
1509 ao_ref_init_from_ptr_and_size (&dref,
1510 gimple_call_arg (call, 0),
1511 size);
1512 return refs_may_alias_p_1 (&dref, ref, false);
1513 }
1514 case BUILT_IN_STRCPY_CHK:
1515 case BUILT_IN_STRNCPY_CHK:
1516 case BUILT_IN_MEMCPY_CHK:
1517 case BUILT_IN_MEMMOVE_CHK:
1518 case BUILT_IN_MEMPCPY_CHK:
1519 case BUILT_IN_STPCPY_CHK:
1520 case BUILT_IN_STRCAT_CHK:
1521 case BUILT_IN_STRNCAT_CHK:
1522 case BUILT_IN_MEMSET_CHK:
1523 {
1524 ao_ref dref;
1525 tree size = NULL_TREE;
1526 /* Don't pass in size for __strncat_chk, as the maximum size
1527 is strlen (dest) + n + 1 instead of n, resp.
1528 n + 1 at dest + strlen (dest), but strlen (dest) isn't
1529 known. */
1530 if (gimple_call_num_args (call) == 4
1531 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
1532 size = gimple_call_arg (call, 2);
1533 ao_ref_init_from_ptr_and_size (&dref,
1534 gimple_call_arg (call, 0),
1535 size);
1536 return refs_may_alias_p_1 (&dref, ref, false);
1537 }
1538 case BUILT_IN_BCOPY:
1539 {
1540 ao_ref dref;
1541 tree size = gimple_call_arg (call, 2);
1542 ao_ref_init_from_ptr_and_size (&dref,
1543 gimple_call_arg (call, 1),
1544 size);
1545 return refs_may_alias_p_1 (&dref, ref, false);
1546 }
1547 /* Allocating memory does not have any side-effects apart from
1548 being the definition point for the pointer. */
1549 case BUILT_IN_MALLOC:
1550 case BUILT_IN_CALLOC:
1551 case BUILT_IN_STRDUP:
1552 case BUILT_IN_STRNDUP:
1553 /* Unix98 specifies that errno is set on allocation failure. */
1554 if (flag_errno_math
1555 && targetm.ref_may_alias_errno (ref))
1556 return true;
1557 return false;
1558 case BUILT_IN_STACK_SAVE:
1559 case BUILT_IN_ALLOCA:
1560 case BUILT_IN_ASSUME_ALIGNED:
1561 return false;
1562 /* Freeing memory kills the pointed-to memory. More importantly
1563 the call has to serve as a barrier for moving loads and stores
1564 across it. */
1565 case BUILT_IN_FREE:
1566 case BUILT_IN_VA_END:
1567 {
1568 tree ptr = gimple_call_arg (call, 0);
1569 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
1570 }
1571 case BUILT_IN_GAMMA_R:
1572 case BUILT_IN_GAMMAF_R:
1573 case BUILT_IN_GAMMAL_R:
1574 case BUILT_IN_LGAMMA_R:
1575 case BUILT_IN_LGAMMAF_R:
1576 case BUILT_IN_LGAMMAL_R:
1577 {
1578 tree out = gimple_call_arg (call, 1);
1579 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1580 return true;
1581 if (flag_errno_math)
1582 break;
1583 return false;
1584 }
1585 case BUILT_IN_FREXP:
1586 case BUILT_IN_FREXPF:
1587 case BUILT_IN_FREXPL:
1588 case BUILT_IN_MODF:
1589 case BUILT_IN_MODFF:
1590 case BUILT_IN_MODFL:
1591 {
1592 tree out = gimple_call_arg (call, 1);
1593 return ptr_deref_may_alias_ref_p_1 (out, ref);
1594 }
1595 case BUILT_IN_REMQUO:
1596 case BUILT_IN_REMQUOF:
1597 case BUILT_IN_REMQUOL:
1598 {
1599 tree out = gimple_call_arg (call, 2);
1600 if (ptr_deref_may_alias_ref_p_1 (out, ref))
1601 return true;
1602 if (flag_errno_math)
1603 break;
1604 return false;
1605 }
1606 case BUILT_IN_SINCOS:
1607 case BUILT_IN_SINCOSF:
1608 case BUILT_IN_SINCOSL:
1609 {
1610 tree sin = gimple_call_arg (call, 1);
1611 tree cos = gimple_call_arg (call, 2);
1612 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
1613 || ptr_deref_may_alias_ref_p_1 (cos, ref));
1614 }
1615 /* __sync_* builtins and some OpenMP builtins act as threading
1616 barriers. */
1617 #undef DEF_SYNC_BUILTIN
1618 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
1619 #include "sync-builtins.def"
1620 #undef DEF_SYNC_BUILTIN
1621 case BUILT_IN_GOMP_ATOMIC_START:
1622 case BUILT_IN_GOMP_ATOMIC_END:
1623 case BUILT_IN_GOMP_BARRIER:
1624 case BUILT_IN_GOMP_TASKWAIT:
1625 case BUILT_IN_GOMP_CRITICAL_START:
1626 case BUILT_IN_GOMP_CRITICAL_END:
1627 case BUILT_IN_GOMP_CRITICAL_NAME_START:
1628 case BUILT_IN_GOMP_CRITICAL_NAME_END:
1629 case BUILT_IN_GOMP_LOOP_END:
1630 case BUILT_IN_GOMP_ORDERED_START:
1631 case BUILT_IN_GOMP_ORDERED_END:
1632 case BUILT_IN_GOMP_PARALLEL_END:
1633 case BUILT_IN_GOMP_SECTIONS_END:
1634 case BUILT_IN_GOMP_SINGLE_COPY_START:
1635 case BUILT_IN_GOMP_SINGLE_COPY_END:
1636 return true;
1637 default:
1638 /* Fallthru to general call handling. */;
1639 }
1640
1641 /* Check if base is a global static variable that is not written
1642 by the function. */
1643 if (callee != NULL_TREE
1644 && TREE_CODE (base) == VAR_DECL
1645 && TREE_STATIC (base))
1646 {
1647 struct cgraph_node *node = cgraph_get_node (callee);
1648 bitmap not_written;
1649
1650 if (node
1651 && (not_written = ipa_reference_get_not_written_global (node))
1652 && bitmap_bit_p (not_written, DECL_UID (base)))
1653 return false;
1654 }
1655
1656 /* Check if the base variable is call-clobbered. */
1657 if (DECL_P (base))
1658 return pt_solution_includes (gimple_call_clobber_set (call), base);
1659 else if ((TREE_CODE (base) == MEM_REF
1660 || TREE_CODE (base) == TARGET_MEM_REF)
1661 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
1662 {
1663 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
1664 if (!pi)
1665 return true;
1666
1667 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
1668 }
1669
1670 return true;
1671 }
1672
1673 /* If the call in statement CALL may clobber the memory reference REF
1674 return true, otherwise return false. */
1675
1676 bool
1677 call_may_clobber_ref_p (gimple call, tree ref)
1678 {
1679 bool res;
1680 ao_ref r;
1681 ao_ref_init (&r, ref);
1682 res = call_may_clobber_ref_p_1 (call, &r);
1683 if (res)
1684 ++alias_stats.call_may_clobber_ref_p_may_alias;
1685 else
1686 ++alias_stats.call_may_clobber_ref_p_no_alias;
1687 return res;
1688 }
1689
1690
1691 /* If the statement STMT may clobber the memory reference REF return true,
1692 otherwise return false. */
1693
1694 bool
1695 stmt_may_clobber_ref_p_1 (gimple stmt, ao_ref *ref)
1696 {
1697 if (is_gimple_call (stmt))
1698 {
1699 tree lhs = gimple_call_lhs (stmt);
1700 if (lhs
1701 && TREE_CODE (lhs) != SSA_NAME)
1702 {
1703 ao_ref r;
1704 ao_ref_init (&r, lhs);
1705 if (refs_may_alias_p_1 (ref, &r, true))
1706 return true;
1707 }
1708
1709 return call_may_clobber_ref_p_1 (stmt, ref);
1710 }
1711 else if (gimple_assign_single_p (stmt))
1712 {
1713 tree lhs = gimple_assign_lhs (stmt);
1714 if (TREE_CODE (lhs) != SSA_NAME)
1715 {
1716 ao_ref r;
1717 ao_ref_init (&r, lhs);
1718 return refs_may_alias_p_1 (ref, &r, true);
1719 }
1720 }
1721 else if (gimple_code (stmt) == GIMPLE_ASM)
1722 return true;
1723
1724 return false;
1725 }
1726
1727 bool
1728 stmt_may_clobber_ref_p (gimple stmt, tree ref)
1729 {
1730 ao_ref r;
1731 ao_ref_init (&r, ref);
1732 return stmt_may_clobber_ref_p_1 (stmt, &r);
1733 }
1734
1735 /* If STMT kills the memory reference REF return true, otherwise
1736 return false. */
1737
1738 static bool
1739 stmt_kills_ref_p_1 (gimple stmt, ao_ref *ref)
1740 {
1741 /* For a must-alias check we need to be able to constrain
1742 the access properly. */
1743 ao_ref_base (ref);
1744 if (ref->max_size == -1)
1745 return false;
1746
1747 if (gimple_has_lhs (stmt)
1748 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
1749 /* The assignment is not necessarily carried out if it can throw
1750 and we can catch it in the current function where we could inspect
1751 the previous value.
1752 ??? We only need to care about the RHS throwing. For aggregate
1753 assignments or similar calls and non-call exceptions the LHS
1754 might throw as well. */
1755 && !stmt_can_throw_internal (stmt))
1756 {
1757 tree base, lhs = gimple_get_lhs (stmt);
1758 HOST_WIDE_INT size, offset, max_size;
1759 base = get_ref_base_and_extent (lhs, &offset, &size, &max_size);
1760 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
1761 so base == ref->base does not always hold. */
1762 if (base == ref->base)
1763 {
1764 /* For a must-alias check we need to be able to constrain
1765 the access properly. */
1766 if (size != -1 && size == max_size)
1767 {
1768 if (offset <= ref->offset
1769 && offset + size >= ref->offset + ref->max_size)
1770 return true;
1771 }
1772 }
1773 }
1774
1775 if (is_gimple_call (stmt))
1776 {
1777 tree callee = gimple_call_fndecl (stmt);
1778 if (callee != NULL_TREE
1779 && DECL_BUILT_IN_CLASS (callee) == BUILT_IN_NORMAL)
1780 switch (DECL_FUNCTION_CODE (callee))
1781 {
1782 case BUILT_IN_MEMCPY:
1783 case BUILT_IN_MEMPCPY:
1784 case BUILT_IN_MEMMOVE:
1785 case BUILT_IN_MEMSET:
1786 case BUILT_IN_MEMCPY_CHK:
1787 case BUILT_IN_MEMPCPY_CHK:
1788 case BUILT_IN_MEMMOVE_CHK:
1789 case BUILT_IN_MEMSET_CHK:
1790 {
1791 tree dest = gimple_call_arg (stmt, 0);
1792 tree len = gimple_call_arg (stmt, 2);
1793 tree base = NULL_TREE;
1794 HOST_WIDE_INT offset = 0;
1795 if (!host_integerp (len, 0))
1796 return false;
1797 if (TREE_CODE (dest) == ADDR_EXPR)
1798 base = get_addr_base_and_unit_offset (TREE_OPERAND (dest, 0),
1799 &offset);
1800 else if (TREE_CODE (dest) == SSA_NAME)
1801 base = dest;
1802 if (base
1803 && base == ao_ref_base (ref))
1804 {
1805 HOST_WIDE_INT size = TREE_INT_CST_LOW (len);
1806 if (offset <= ref->offset / BITS_PER_UNIT
1807 && (offset + size
1808 >= ((ref->offset + ref->max_size + BITS_PER_UNIT - 1)
1809 / BITS_PER_UNIT)))
1810 return true;
1811 }
1812 break;
1813 }
1814
1815 case BUILT_IN_VA_END:
1816 {
1817 tree ptr = gimple_call_arg (stmt, 0);
1818 if (TREE_CODE (ptr) == ADDR_EXPR)
1819 {
1820 tree base = ao_ref_base (ref);
1821 if (TREE_OPERAND (ptr, 0) == base)
1822 return true;
1823 }
1824 break;
1825 }
1826
1827 default:;
1828 }
1829 }
1830 return false;
1831 }
1832
1833 bool
1834 stmt_kills_ref_p (gimple stmt, tree ref)
1835 {
1836 ao_ref r;
1837 ao_ref_init (&r, ref);
1838 return stmt_kills_ref_p_1 (stmt, &r);
1839 }
1840
1841
1842 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
1843 TARGET or a statement clobbering the memory reference REF in which
1844 case false is returned. The walk starts with VUSE, one argument of PHI. */
1845
1846 static bool
1847 maybe_skip_until (gimple phi, tree target, ao_ref *ref,
1848 tree vuse, bitmap *visited)
1849 {
1850 if (!*visited)
1851 *visited = BITMAP_ALLOC (NULL);
1852
1853 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
1854
1855 /* Walk until we hit the target. */
1856 while (vuse != target)
1857 {
1858 gimple def_stmt = SSA_NAME_DEF_STMT (vuse);
1859 /* Recurse for PHI nodes. */
1860 if (gimple_code (def_stmt) == GIMPLE_PHI)
1861 {
1862 /* An already visited PHI node ends the walk successfully. */
1863 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
1864 return true;
1865 vuse = get_continuation_for_phi (def_stmt, ref, visited);
1866 if (!vuse)
1867 return false;
1868 continue;
1869 }
1870 /* A clobbering statement or the end of the IL ends it failing. */
1871 else if (gimple_nop_p (def_stmt)
1872 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
1873 return false;
1874 vuse = gimple_vuse (def_stmt);
1875 }
1876 return true;
1877 }
1878
1879 /* Starting from a PHI node for the virtual operand of the memory reference
1880 REF find a continuation virtual operand that allows to continue walking
1881 statements dominating PHI skipping only statements that cannot possibly
1882 clobber REF. Returns NULL_TREE if no suitable virtual operand can
1883 be found. */
1884
1885 tree
1886 get_continuation_for_phi (gimple phi, ao_ref *ref, bitmap *visited)
1887 {
1888 unsigned nargs = gimple_phi_num_args (phi);
1889
1890 /* Through a single-argument PHI we can simply look through. */
1891 if (nargs == 1)
1892 return PHI_ARG_DEF (phi, 0);
1893
1894 /* For two arguments try to skip non-aliasing code until we hit
1895 the phi argument definition that dominates the other one. */
1896 if (nargs == 2)
1897 {
1898 tree arg0 = PHI_ARG_DEF (phi, 0);
1899 tree arg1 = PHI_ARG_DEF (phi, 1);
1900 gimple def0 = SSA_NAME_DEF_STMT (arg0);
1901 gimple def1 = SSA_NAME_DEF_STMT (arg1);
1902 tree common_vuse;
1903
1904 if (arg0 == arg1)
1905 return arg0;
1906 else if (gimple_nop_p (def0)
1907 || (!gimple_nop_p (def1)
1908 && dominated_by_p (CDI_DOMINATORS,
1909 gimple_bb (def1), gimple_bb (def0))))
1910 {
1911 if (maybe_skip_until (phi, arg0, ref, arg1, visited))
1912 return arg0;
1913 }
1914 else if (gimple_nop_p (def1)
1915 || dominated_by_p (CDI_DOMINATORS,
1916 gimple_bb (def0), gimple_bb (def1)))
1917 {
1918 if (maybe_skip_until (phi, arg1, ref, arg0, visited))
1919 return arg1;
1920 }
1921 /* Special case of a diamond:
1922 MEM_1 = ...
1923 goto (cond) ? L1 : L2
1924 L1: store1 = ... #MEM_2 = vuse(MEM_1)
1925 goto L3
1926 L2: store2 = ... #MEM_3 = vuse(MEM_1)
1927 L3: MEM_4 = PHI<MEM_2, MEM_3>
1928 We were called with the PHI at L3, MEM_2 and MEM_3 don't
1929 dominate each other, but still we can easily skip this PHI node
1930 if we recognize that the vuse MEM operand is the same for both,
1931 and that we can skip both statements (they don't clobber us).
1932 This is still linear. Don't use maybe_skip_until, that might
1933 potentially be slow. */
1934 else if ((common_vuse = gimple_vuse (def0))
1935 && common_vuse == gimple_vuse (def1))
1936 {
1937 if (!stmt_may_clobber_ref_p_1 (def0, ref)
1938 && !stmt_may_clobber_ref_p_1 (def1, ref))
1939 return common_vuse;
1940 }
1941 }
1942
1943 return NULL_TREE;
1944 }
1945
1946 /* Based on the memory reference REF and its virtual use VUSE call
1947 WALKER for each virtual use that is equivalent to VUSE, including VUSE
1948 itself. That is, for each virtual use for which its defining statement
1949 does not clobber REF.
1950
1951 WALKER is called with REF, the current virtual use and DATA. If
1952 WALKER returns non-NULL the walk stops and its result is returned.
1953 At the end of a non-successful walk NULL is returned.
1954
1955 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
1956 use which definition is a statement that may clobber REF and DATA.
1957 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
1958 If TRANSLATE returns non-NULL the walk stops and its result is returned.
1959 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
1960 to adjust REF and *DATA to make that valid.
1961
1962 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
1963
1964 void *
1965 walk_non_aliased_vuses (ao_ref *ref, tree vuse,
1966 void *(*walker)(ao_ref *, tree, void *),
1967 void *(*translate)(ao_ref *, tree, void *), void *data)
1968 {
1969 bitmap visited = NULL;
1970 void *res;
1971
1972 timevar_push (TV_ALIAS_STMT_WALK);
1973
1974 do
1975 {
1976 gimple def_stmt;
1977
1978 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
1979 res = (*walker) (ref, vuse, data);
1980 if (res)
1981 break;
1982
1983 def_stmt = SSA_NAME_DEF_STMT (vuse);
1984 if (gimple_nop_p (def_stmt))
1985 break;
1986 else if (gimple_code (def_stmt) == GIMPLE_PHI)
1987 vuse = get_continuation_for_phi (def_stmt, ref, &visited);
1988 else
1989 {
1990 if (stmt_may_clobber_ref_p_1 (def_stmt, ref))
1991 {
1992 if (!translate)
1993 break;
1994 res = (*translate) (ref, vuse, data);
1995 /* Failed lookup and translation. */
1996 if (res == (void *)-1)
1997 {
1998 res = NULL;
1999 break;
2000 }
2001 /* Lookup succeeded. */
2002 else if (res != NULL)
2003 break;
2004 /* Translation succeeded, continue walking. */
2005 }
2006 vuse = gimple_vuse (def_stmt);
2007 }
2008 }
2009 while (vuse);
2010
2011 if (visited)
2012 BITMAP_FREE (visited);
2013
2014 timevar_pop (TV_ALIAS_STMT_WALK);
2015
2016 return res;
2017 }
2018
2019
2020 /* Based on the memory reference REF call WALKER for each vdef which
2021 defining statement may clobber REF, starting with VDEF. If REF
2022 is NULL_TREE, each defining statement is visited.
2023
2024 WALKER is called with REF, the current vdef and DATA. If WALKER
2025 returns true the walk is stopped, otherwise it continues.
2026
2027 At PHI nodes walk_aliased_vdefs forks into one walk for reach
2028 PHI argument (but only one walk continues on merge points), the
2029 return value is true if any of the walks was successful.
2030
2031 The function returns the number of statements walked. */
2032
2033 static unsigned int
2034 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
2035 bool (*walker)(ao_ref *, tree, void *), void *data,
2036 bitmap *visited, unsigned int cnt)
2037 {
2038 do
2039 {
2040 gimple def_stmt = SSA_NAME_DEF_STMT (vdef);
2041
2042 if (*visited
2043 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
2044 return cnt;
2045
2046 if (gimple_nop_p (def_stmt))
2047 return cnt;
2048 else if (gimple_code (def_stmt) == GIMPLE_PHI)
2049 {
2050 unsigned i;
2051 if (!*visited)
2052 *visited = BITMAP_ALLOC (NULL);
2053 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
2054 cnt += walk_aliased_vdefs_1 (ref, gimple_phi_arg_def (def_stmt, i),
2055 walker, data, visited, 0);
2056 return cnt;
2057 }
2058
2059 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
2060 cnt++;
2061 if ((!ref
2062 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
2063 && (*walker) (ref, vdef, data))
2064 return cnt;
2065
2066 vdef = gimple_vuse (def_stmt);
2067 }
2068 while (1);
2069 }
2070
2071 unsigned int
2072 walk_aliased_vdefs (ao_ref *ref, tree vdef,
2073 bool (*walker)(ao_ref *, tree, void *), void *data,
2074 bitmap *visited)
2075 {
2076 bitmap local_visited = NULL;
2077 unsigned int ret;
2078
2079 timevar_push (TV_ALIAS_STMT_WALK);
2080
2081 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
2082 visited ? visited : &local_visited, 0);
2083 if (local_visited)
2084 BITMAP_FREE (local_visited);
2085
2086 timevar_pop (TV_ALIAS_STMT_WALK);
2087
2088 return ret;
2089 }
2090