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