tree-optimization: fix access path oracle on mismatched array refs [PR93586]
[gcc.git] / gcc / tree-ssa-alias.c
1 /* Alias analysis for trees.
2 Copyright (C) 2004-2020 Free Software Foundation, Inc.
3 Contributed by Diego Novillo <dnovillo@redhat.com>
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 3, or (at your option)
10 any later version.
11
12 GCC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "backend.h"
25 #include "target.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "timevar.h" /* for TV_ALIAS_STMT_WALK */
30 #include "ssa.h"
31 #include "cgraph.h"
32 #include "tree-pretty-print.h"
33 #include "alias.h"
34 #include "fold-const.h"
35 #include "langhooks.h"
36 #include "dumpfile.h"
37 #include "tree-eh.h"
38 #include "tree-dfa.h"
39 #include "ipa-reference.h"
40 #include "varasm.h"
41
42 /* Broad overview of how alias analysis on gimple works:
43
44 Statements clobbering or using memory are linked through the
45 virtual operand factored use-def chain. The virtual operand
46 is unique per function, its symbol is accessible via gimple_vop (cfun).
47 Virtual operands are used for efficiently walking memory statements
48 in the gimple IL and are useful for things like value-numbering as
49 a generation count for memory references.
50
51 SSA_NAME pointers may have associated points-to information
52 accessible via the SSA_NAME_PTR_INFO macro. Flow-insensitive
53 points-to information is (re-)computed by the TODO_rebuild_alias
54 pass manager todo. Points-to information is also used for more
55 precise tracking of call-clobbered and call-used variables and
56 related disambiguations.
57
58 This file contains functions for disambiguating memory references,
59 the so called alias-oracle and tools for walking of the gimple IL.
60
61 The main alias-oracle entry-points are
62
63 bool stmt_may_clobber_ref_p (gimple *, tree)
64
65 This function queries if a statement may invalidate (parts of)
66 the memory designated by the reference tree argument.
67
68 bool ref_maybe_used_by_stmt_p (gimple *, tree)
69
70 This function queries if a statement may need (parts of) the
71 memory designated by the reference tree argument.
72
73 There are variants of these functions that only handle the call
74 part of a statement, call_may_clobber_ref_p and ref_maybe_used_by_call_p.
75 Note that these do not disambiguate against a possible call lhs.
76
77 bool refs_may_alias_p (tree, tree)
78
79 This function tries to disambiguate two reference trees.
80
81 bool ptr_deref_may_alias_global_p (tree)
82
83 This function queries if dereferencing a pointer variable may
84 alias global memory.
85
86 More low-level disambiguators are available and documented in
87 this file. Low-level disambiguators dealing with points-to
88 information are in tree-ssa-structalias.c. */
89
90 static int nonoverlapping_refs_since_match_p (tree, tree, tree, tree, bool);
91 static bool nonoverlapping_component_refs_p (const_tree, const_tree);
92
93 /* Query statistics for the different low-level disambiguators.
94 A high-level query may trigger multiple of them. */
95
96 static struct {
97 unsigned HOST_WIDE_INT refs_may_alias_p_may_alias;
98 unsigned HOST_WIDE_INT refs_may_alias_p_no_alias;
99 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_may_alias;
100 unsigned HOST_WIDE_INT ref_maybe_used_by_call_p_no_alias;
101 unsigned HOST_WIDE_INT call_may_clobber_ref_p_may_alias;
102 unsigned HOST_WIDE_INT call_may_clobber_ref_p_no_alias;
103 unsigned HOST_WIDE_INT aliasing_component_refs_p_may_alias;
104 unsigned HOST_WIDE_INT aliasing_component_refs_p_no_alias;
105 unsigned HOST_WIDE_INT nonoverlapping_component_refs_p_may_alias;
106 unsigned HOST_WIDE_INT nonoverlapping_component_refs_p_no_alias;
107 unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_may_alias;
108 unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_must_overlap;
109 unsigned HOST_WIDE_INT nonoverlapping_refs_since_match_p_no_alias;
110 } alias_stats;
111
112 void
113 dump_alias_stats (FILE *s)
114 {
115 fprintf (s, "\nAlias oracle query stats:\n");
116 fprintf (s, " refs_may_alias_p: "
117 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
118 HOST_WIDE_INT_PRINT_DEC" queries\n",
119 alias_stats.refs_may_alias_p_no_alias,
120 alias_stats.refs_may_alias_p_no_alias
121 + alias_stats.refs_may_alias_p_may_alias);
122 fprintf (s, " ref_maybe_used_by_call_p: "
123 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
124 HOST_WIDE_INT_PRINT_DEC" queries\n",
125 alias_stats.ref_maybe_used_by_call_p_no_alias,
126 alias_stats.refs_may_alias_p_no_alias
127 + alias_stats.ref_maybe_used_by_call_p_may_alias);
128 fprintf (s, " call_may_clobber_ref_p: "
129 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
130 HOST_WIDE_INT_PRINT_DEC" queries\n",
131 alias_stats.call_may_clobber_ref_p_no_alias,
132 alias_stats.call_may_clobber_ref_p_no_alias
133 + alias_stats.call_may_clobber_ref_p_may_alias);
134 fprintf (s, " nonoverlapping_component_refs_p: "
135 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
136 HOST_WIDE_INT_PRINT_DEC" queries\n",
137 alias_stats.nonoverlapping_component_refs_p_no_alias,
138 alias_stats.nonoverlapping_component_refs_p_no_alias
139 + alias_stats.nonoverlapping_component_refs_p_may_alias);
140 fprintf (s, " nonoverlapping_refs_since_match_p: "
141 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
142 HOST_WIDE_INT_PRINT_DEC" must overlaps, "
143 HOST_WIDE_INT_PRINT_DEC" queries\n",
144 alias_stats.nonoverlapping_refs_since_match_p_no_alias,
145 alias_stats.nonoverlapping_refs_since_match_p_must_overlap,
146 alias_stats.nonoverlapping_refs_since_match_p_no_alias
147 + alias_stats.nonoverlapping_refs_since_match_p_may_alias
148 + alias_stats.nonoverlapping_refs_since_match_p_must_overlap);
149 fprintf (s, " aliasing_component_refs_p: "
150 HOST_WIDE_INT_PRINT_DEC" disambiguations, "
151 HOST_WIDE_INT_PRINT_DEC" queries\n",
152 alias_stats.aliasing_component_refs_p_no_alias,
153 alias_stats.aliasing_component_refs_p_no_alias
154 + alias_stats.aliasing_component_refs_p_may_alias);
155 dump_alias_stats_in_alias_c (s);
156 }
157
158
159 /* Return true, if dereferencing PTR may alias with a global variable. */
160
161 bool
162 ptr_deref_may_alias_global_p (tree ptr)
163 {
164 struct ptr_info_def *pi;
165
166 /* If we end up with a pointer constant here that may point
167 to global memory. */
168 if (TREE_CODE (ptr) != SSA_NAME)
169 return true;
170
171 pi = SSA_NAME_PTR_INFO (ptr);
172
173 /* If we do not have points-to information for this variable,
174 we have to punt. */
175 if (!pi)
176 return true;
177
178 /* ??? This does not use TBAA to prune globals ptr may not access. */
179 return pt_solution_includes_global (&pi->pt);
180 }
181
182 /* Return true if dereferencing PTR may alias DECL.
183 The caller is responsible for applying TBAA to see if PTR
184 may access DECL at all. */
185
186 static bool
187 ptr_deref_may_alias_decl_p (tree ptr, tree decl)
188 {
189 struct ptr_info_def *pi;
190
191 /* Conversions are irrelevant for points-to information and
192 data-dependence analysis can feed us those. */
193 STRIP_NOPS (ptr);
194
195 /* Anything we do not explicilty handle aliases. */
196 if ((TREE_CODE (ptr) != SSA_NAME
197 && TREE_CODE (ptr) != ADDR_EXPR
198 && TREE_CODE (ptr) != POINTER_PLUS_EXPR)
199 || !POINTER_TYPE_P (TREE_TYPE (ptr))
200 || (!VAR_P (decl)
201 && TREE_CODE (decl) != PARM_DECL
202 && TREE_CODE (decl) != RESULT_DECL))
203 return true;
204
205 /* Disregard pointer offsetting. */
206 if (TREE_CODE (ptr) == POINTER_PLUS_EXPR)
207 {
208 do
209 {
210 ptr = TREE_OPERAND (ptr, 0);
211 }
212 while (TREE_CODE (ptr) == POINTER_PLUS_EXPR);
213 return ptr_deref_may_alias_decl_p (ptr, decl);
214 }
215
216 /* ADDR_EXPR pointers either just offset another pointer or directly
217 specify the pointed-to set. */
218 if (TREE_CODE (ptr) == ADDR_EXPR)
219 {
220 tree base = get_base_address (TREE_OPERAND (ptr, 0));
221 if (base
222 && (TREE_CODE (base) == MEM_REF
223 || TREE_CODE (base) == TARGET_MEM_REF))
224 ptr = TREE_OPERAND (base, 0);
225 else if (base
226 && DECL_P (base))
227 return compare_base_decls (base, decl) != 0;
228 else if (base
229 && CONSTANT_CLASS_P (base))
230 return false;
231 else
232 return true;
233 }
234
235 /* Non-aliased variables cannot be pointed to. */
236 if (!may_be_aliased (decl))
237 return false;
238
239 /* If we do not have useful points-to information for this pointer
240 we cannot disambiguate anything else. */
241 pi = SSA_NAME_PTR_INFO (ptr);
242 if (!pi)
243 return true;
244
245 return pt_solution_includes (&pi->pt, decl);
246 }
247
248 /* Return true if dereferenced PTR1 and PTR2 may alias.
249 The caller is responsible for applying TBAA to see if accesses
250 through PTR1 and PTR2 may conflict at all. */
251
252 bool
253 ptr_derefs_may_alias_p (tree ptr1, tree ptr2)
254 {
255 struct ptr_info_def *pi1, *pi2;
256
257 /* Conversions are irrelevant for points-to information and
258 data-dependence analysis can feed us those. */
259 STRIP_NOPS (ptr1);
260 STRIP_NOPS (ptr2);
261
262 /* Disregard pointer offsetting. */
263 if (TREE_CODE (ptr1) == POINTER_PLUS_EXPR)
264 {
265 do
266 {
267 ptr1 = TREE_OPERAND (ptr1, 0);
268 }
269 while (TREE_CODE (ptr1) == POINTER_PLUS_EXPR);
270 return ptr_derefs_may_alias_p (ptr1, ptr2);
271 }
272 if (TREE_CODE (ptr2) == POINTER_PLUS_EXPR)
273 {
274 do
275 {
276 ptr2 = TREE_OPERAND (ptr2, 0);
277 }
278 while (TREE_CODE (ptr2) == POINTER_PLUS_EXPR);
279 return ptr_derefs_may_alias_p (ptr1, ptr2);
280 }
281
282 /* ADDR_EXPR pointers either just offset another pointer or directly
283 specify the pointed-to set. */
284 if (TREE_CODE (ptr1) == ADDR_EXPR)
285 {
286 tree base = get_base_address (TREE_OPERAND (ptr1, 0));
287 if (base
288 && (TREE_CODE (base) == MEM_REF
289 || TREE_CODE (base) == TARGET_MEM_REF))
290 return ptr_derefs_may_alias_p (TREE_OPERAND (base, 0), ptr2);
291 else if (base
292 && DECL_P (base))
293 return ptr_deref_may_alias_decl_p (ptr2, base);
294 else
295 return true;
296 }
297 if (TREE_CODE (ptr2) == ADDR_EXPR)
298 {
299 tree base = get_base_address (TREE_OPERAND (ptr2, 0));
300 if (base
301 && (TREE_CODE (base) == MEM_REF
302 || TREE_CODE (base) == TARGET_MEM_REF))
303 return ptr_derefs_may_alias_p (ptr1, TREE_OPERAND (base, 0));
304 else if (base
305 && DECL_P (base))
306 return ptr_deref_may_alias_decl_p (ptr1, base);
307 else
308 return true;
309 }
310
311 /* From here we require SSA name pointers. Anything else aliases. */
312 if (TREE_CODE (ptr1) != SSA_NAME
313 || TREE_CODE (ptr2) != SSA_NAME
314 || !POINTER_TYPE_P (TREE_TYPE (ptr1))
315 || !POINTER_TYPE_P (TREE_TYPE (ptr2)))
316 return true;
317
318 /* We may end up with two empty points-to solutions for two same pointers.
319 In this case we still want to say both pointers alias, so shortcut
320 that here. */
321 if (ptr1 == ptr2)
322 return true;
323
324 /* If we do not have useful points-to information for either pointer
325 we cannot disambiguate anything else. */
326 pi1 = SSA_NAME_PTR_INFO (ptr1);
327 pi2 = SSA_NAME_PTR_INFO (ptr2);
328 if (!pi1 || !pi2)
329 return true;
330
331 /* ??? This does not use TBAA to prune decls from the intersection
332 that not both pointers may access. */
333 return pt_solutions_intersect (&pi1->pt, &pi2->pt);
334 }
335
336 /* Return true if dereferencing PTR may alias *REF.
337 The caller is responsible for applying TBAA to see if PTR
338 may access *REF at all. */
339
340 static bool
341 ptr_deref_may_alias_ref_p_1 (tree ptr, ao_ref *ref)
342 {
343 tree base = ao_ref_base (ref);
344
345 if (TREE_CODE (base) == MEM_REF
346 || TREE_CODE (base) == TARGET_MEM_REF)
347 return ptr_derefs_may_alias_p (ptr, TREE_OPERAND (base, 0));
348 else if (DECL_P (base))
349 return ptr_deref_may_alias_decl_p (ptr, base);
350
351 return true;
352 }
353
354 /* Returns true if PTR1 and PTR2 compare unequal because of points-to. */
355
356 bool
357 ptrs_compare_unequal (tree ptr1, tree ptr2)
358 {
359 /* First resolve the pointers down to a SSA name pointer base or
360 a VAR_DECL, PARM_DECL or RESULT_DECL. This explicitely does
361 not yet try to handle LABEL_DECLs, FUNCTION_DECLs, CONST_DECLs
362 or STRING_CSTs which needs points-to adjustments to track them
363 in the points-to sets. */
364 tree obj1 = NULL_TREE;
365 tree obj2 = NULL_TREE;
366 if (TREE_CODE (ptr1) == ADDR_EXPR)
367 {
368 tree tem = get_base_address (TREE_OPERAND (ptr1, 0));
369 if (! tem)
370 return false;
371 if (VAR_P (tem)
372 || TREE_CODE (tem) == PARM_DECL
373 || TREE_CODE (tem) == RESULT_DECL)
374 obj1 = tem;
375 else if (TREE_CODE (tem) == MEM_REF)
376 ptr1 = TREE_OPERAND (tem, 0);
377 }
378 if (TREE_CODE (ptr2) == ADDR_EXPR)
379 {
380 tree tem = get_base_address (TREE_OPERAND (ptr2, 0));
381 if (! tem)
382 return false;
383 if (VAR_P (tem)
384 || TREE_CODE (tem) == PARM_DECL
385 || TREE_CODE (tem) == RESULT_DECL)
386 obj2 = tem;
387 else if (TREE_CODE (tem) == MEM_REF)
388 ptr2 = TREE_OPERAND (tem, 0);
389 }
390
391 /* Canonicalize ptr vs. object. */
392 if (TREE_CODE (ptr1) == SSA_NAME && obj2)
393 {
394 std::swap (ptr1, ptr2);
395 std::swap (obj1, obj2);
396 }
397
398 if (obj1 && obj2)
399 /* Other code handles this correctly, no need to duplicate it here. */;
400 else if (obj1 && TREE_CODE (ptr2) == SSA_NAME)
401 {
402 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr2);
403 /* We may not use restrict to optimize pointer comparisons.
404 See PR71062. So we have to assume that restrict-pointed-to
405 may be in fact obj1. */
406 if (!pi
407 || pi->pt.vars_contains_restrict
408 || pi->pt.vars_contains_interposable)
409 return false;
410 if (VAR_P (obj1)
411 && (TREE_STATIC (obj1) || DECL_EXTERNAL (obj1)))
412 {
413 varpool_node *node = varpool_node::get (obj1);
414 /* If obj1 may bind to NULL give up (see below). */
415 if (! node
416 || ! node->nonzero_address ()
417 || ! decl_binds_to_current_def_p (obj1))
418 return false;
419 }
420 return !pt_solution_includes (&pi->pt, obj1);
421 }
422
423 /* ??? We'd like to handle ptr1 != NULL and ptr1 != ptr2
424 but those require pt.null to be conservatively correct. */
425
426 return false;
427 }
428
429 /* Returns whether reference REF to BASE may refer to global memory. */
430
431 static bool
432 ref_may_alias_global_p_1 (tree base)
433 {
434 if (DECL_P (base))
435 return is_global_var (base);
436 else if (TREE_CODE (base) == MEM_REF
437 || TREE_CODE (base) == TARGET_MEM_REF)
438 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
439 return true;
440 }
441
442 bool
443 ref_may_alias_global_p (ao_ref *ref)
444 {
445 tree base = ao_ref_base (ref);
446 return ref_may_alias_global_p_1 (base);
447 }
448
449 bool
450 ref_may_alias_global_p (tree ref)
451 {
452 tree base = get_base_address (ref);
453 return ref_may_alias_global_p_1 (base);
454 }
455
456 /* Return true whether STMT may clobber global memory. */
457
458 bool
459 stmt_may_clobber_global_p (gimple *stmt)
460 {
461 tree lhs;
462
463 if (!gimple_vdef (stmt))
464 return false;
465
466 /* ??? We can ask the oracle whether an artificial pointer
467 dereference with a pointer with points-to information covering
468 all global memory (what about non-address taken memory?) maybe
469 clobbered by this call. As there is at the moment no convenient
470 way of doing that without generating garbage do some manual
471 checking instead.
472 ??? We could make a NULL ao_ref argument to the various
473 predicates special, meaning any global memory. */
474
475 switch (gimple_code (stmt))
476 {
477 case GIMPLE_ASSIGN:
478 lhs = gimple_assign_lhs (stmt);
479 return (TREE_CODE (lhs) != SSA_NAME
480 && ref_may_alias_global_p (lhs));
481 case GIMPLE_CALL:
482 return true;
483 default:
484 return true;
485 }
486 }
487
488
489 /* Dump alias information on FILE. */
490
491 void
492 dump_alias_info (FILE *file)
493 {
494 unsigned i;
495 tree ptr;
496 const char *funcname
497 = lang_hooks.decl_printable_name (current_function_decl, 2);
498 tree var;
499
500 fprintf (file, "\n\nAlias information for %s\n\n", funcname);
501
502 fprintf (file, "Aliased symbols\n\n");
503
504 FOR_EACH_LOCAL_DECL (cfun, i, var)
505 {
506 if (may_be_aliased (var))
507 dump_variable (file, var);
508 }
509
510 fprintf (file, "\nCall clobber information\n");
511
512 fprintf (file, "\nESCAPED");
513 dump_points_to_solution (file, &cfun->gimple_df->escaped);
514
515 fprintf (file, "\n\nFlow-insensitive points-to information\n\n");
516
517 FOR_EACH_SSA_NAME (i, ptr, cfun)
518 {
519 struct ptr_info_def *pi;
520
521 if (!POINTER_TYPE_P (TREE_TYPE (ptr))
522 || SSA_NAME_IN_FREE_LIST (ptr))
523 continue;
524
525 pi = SSA_NAME_PTR_INFO (ptr);
526 if (pi)
527 dump_points_to_info_for (file, ptr);
528 }
529
530 fprintf (file, "\n");
531 }
532
533
534 /* Dump alias information on stderr. */
535
536 DEBUG_FUNCTION void
537 debug_alias_info (void)
538 {
539 dump_alias_info (stderr);
540 }
541
542
543 /* Dump the points-to set *PT into FILE. */
544
545 void
546 dump_points_to_solution (FILE *file, struct pt_solution *pt)
547 {
548 if (pt->anything)
549 fprintf (file, ", points-to anything");
550
551 if (pt->nonlocal)
552 fprintf (file, ", points-to non-local");
553
554 if (pt->escaped)
555 fprintf (file, ", points-to escaped");
556
557 if (pt->ipa_escaped)
558 fprintf (file, ", points-to unit escaped");
559
560 if (pt->null)
561 fprintf (file, ", points-to NULL");
562
563 if (pt->vars)
564 {
565 fprintf (file, ", points-to vars: ");
566 dump_decl_set (file, pt->vars);
567 if (pt->vars_contains_nonlocal
568 || pt->vars_contains_escaped
569 || pt->vars_contains_escaped_heap
570 || pt->vars_contains_restrict)
571 {
572 const char *comma = "";
573 fprintf (file, " (");
574 if (pt->vars_contains_nonlocal)
575 {
576 fprintf (file, "nonlocal");
577 comma = ", ";
578 }
579 if (pt->vars_contains_escaped)
580 {
581 fprintf (file, "%sescaped", comma);
582 comma = ", ";
583 }
584 if (pt->vars_contains_escaped_heap)
585 {
586 fprintf (file, "%sescaped heap", comma);
587 comma = ", ";
588 }
589 if (pt->vars_contains_restrict)
590 {
591 fprintf (file, "%srestrict", comma);
592 comma = ", ";
593 }
594 if (pt->vars_contains_interposable)
595 fprintf (file, "%sinterposable", comma);
596 fprintf (file, ")");
597 }
598 }
599 }
600
601
602 /* Unified dump function for pt_solution. */
603
604 DEBUG_FUNCTION void
605 debug (pt_solution &ref)
606 {
607 dump_points_to_solution (stderr, &ref);
608 }
609
610 DEBUG_FUNCTION void
611 debug (pt_solution *ptr)
612 {
613 if (ptr)
614 debug (*ptr);
615 else
616 fprintf (stderr, "<nil>\n");
617 }
618
619
620 /* Dump points-to information for SSA_NAME PTR into FILE. */
621
622 void
623 dump_points_to_info_for (FILE *file, tree ptr)
624 {
625 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
626
627 print_generic_expr (file, ptr, dump_flags);
628
629 if (pi)
630 dump_points_to_solution (file, &pi->pt);
631 else
632 fprintf (file, ", points-to anything");
633
634 fprintf (file, "\n");
635 }
636
637
638 /* Dump points-to information for VAR into stderr. */
639
640 DEBUG_FUNCTION void
641 debug_points_to_info_for (tree var)
642 {
643 dump_points_to_info_for (stderr, var);
644 }
645
646
647 /* Initializes the alias-oracle reference representation *R from REF. */
648
649 void
650 ao_ref_init (ao_ref *r, tree ref)
651 {
652 r->ref = ref;
653 r->base = NULL_TREE;
654 r->offset = 0;
655 r->size = -1;
656 r->max_size = -1;
657 r->ref_alias_set = -1;
658 r->base_alias_set = -1;
659 r->volatile_p = ref ? TREE_THIS_VOLATILE (ref) : false;
660 }
661
662 /* Returns the base object of the memory reference *REF. */
663
664 tree
665 ao_ref_base (ao_ref *ref)
666 {
667 bool reverse;
668
669 if (ref->base)
670 return ref->base;
671 ref->base = get_ref_base_and_extent (ref->ref, &ref->offset, &ref->size,
672 &ref->max_size, &reverse);
673 return ref->base;
674 }
675
676 /* Returns the base object alias set of the memory reference *REF. */
677
678 alias_set_type
679 ao_ref_base_alias_set (ao_ref *ref)
680 {
681 tree base_ref;
682 if (ref->base_alias_set != -1)
683 return ref->base_alias_set;
684 if (!ref->ref)
685 return 0;
686 base_ref = ref->ref;
687 while (handled_component_p (base_ref))
688 base_ref = TREE_OPERAND (base_ref, 0);
689 ref->base_alias_set = get_alias_set (base_ref);
690 return ref->base_alias_set;
691 }
692
693 /* Returns the reference alias set of the memory reference *REF. */
694
695 alias_set_type
696 ao_ref_alias_set (ao_ref *ref)
697 {
698 if (ref->ref_alias_set != -1)
699 return ref->ref_alias_set;
700 ref->ref_alias_set = get_alias_set (ref->ref);
701 return ref->ref_alias_set;
702 }
703
704 /* Init an alias-oracle reference representation from a gimple pointer
705 PTR and a gimple size SIZE in bytes. If SIZE is NULL_TREE then the
706 size is assumed to be unknown. The access is assumed to be only
707 to or after of the pointer target, not before it. */
708
709 void
710 ao_ref_init_from_ptr_and_size (ao_ref *ref, tree ptr, tree size)
711 {
712 poly_int64 t, size_hwi, extra_offset = 0;
713 ref->ref = NULL_TREE;
714 if (TREE_CODE (ptr) == SSA_NAME)
715 {
716 gimple *stmt = SSA_NAME_DEF_STMT (ptr);
717 if (gimple_assign_single_p (stmt)
718 && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
719 ptr = gimple_assign_rhs1 (stmt);
720 else if (is_gimple_assign (stmt)
721 && gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR
722 && ptrdiff_tree_p (gimple_assign_rhs2 (stmt), &extra_offset))
723 {
724 ptr = gimple_assign_rhs1 (stmt);
725 extra_offset *= BITS_PER_UNIT;
726 }
727 }
728
729 if (TREE_CODE (ptr) == ADDR_EXPR)
730 {
731 ref->base = get_addr_base_and_unit_offset (TREE_OPERAND (ptr, 0), &t);
732 if (ref->base)
733 ref->offset = BITS_PER_UNIT * t;
734 else
735 {
736 size = NULL_TREE;
737 ref->offset = 0;
738 ref->base = get_base_address (TREE_OPERAND (ptr, 0));
739 }
740 }
741 else
742 {
743 gcc_assert (POINTER_TYPE_P (TREE_TYPE (ptr)));
744 ref->base = build2 (MEM_REF, char_type_node,
745 ptr, null_pointer_node);
746 ref->offset = 0;
747 }
748 ref->offset += extra_offset;
749 if (size
750 && poly_int_tree_p (size, &size_hwi)
751 && coeffs_in_range_p (size_hwi, 0, HOST_WIDE_INT_MAX / BITS_PER_UNIT))
752 ref->max_size = ref->size = size_hwi * BITS_PER_UNIT;
753 else
754 ref->max_size = ref->size = -1;
755 ref->ref_alias_set = 0;
756 ref->base_alias_set = 0;
757 ref->volatile_p = false;
758 }
759
760 /* S1 and S2 are TYPE_SIZE or DECL_SIZE. Compare them:
761 Return -1 if S1 < S2
762 Return 1 if S1 > S2
763 Return 0 if equal or incomparable. */
764
765 static int
766 compare_sizes (tree s1, tree s2)
767 {
768 if (!s1 || !s2)
769 return 0;
770
771 poly_uint64 size1;
772 poly_uint64 size2;
773
774 if (!poly_int_tree_p (s1, &size1) || !poly_int_tree_p (s2, &size2))
775 return 0;
776 if (known_lt (size1, size2))
777 return -1;
778 if (known_lt (size2, size1))
779 return 1;
780 return 0;
781 }
782
783 /* Compare TYPE1 and TYPE2 by its size.
784 Return -1 if size of TYPE1 < size of TYPE2
785 Return 1 if size of TYPE1 > size of TYPE2
786 Return 0 if types are of equal sizes or we can not compare them. */
787
788 static int
789 compare_type_sizes (tree type1, tree type2)
790 {
791 /* Be conservative for arrays and vectors. We want to support partial
792 overlap on int[3] and int[3] as tested in gcc.dg/torture/alias-2.c. */
793 while (TREE_CODE (type1) == ARRAY_TYPE
794 || TREE_CODE (type1) == VECTOR_TYPE)
795 type1 = TREE_TYPE (type1);
796 while (TREE_CODE (type2) == ARRAY_TYPE
797 || TREE_CODE (type2) == VECTOR_TYPE)
798 type2 = TREE_TYPE (type2);
799 return compare_sizes (TYPE_SIZE (type1), TYPE_SIZE (type2));
800 }
801
802 /* Return 1 if TYPE1 and TYPE2 are to be considered equivalent for the
803 purpose of TBAA. Return 0 if they are distinct and -1 if we cannot
804 decide. */
805
806 static inline int
807 same_type_for_tbaa (tree type1, tree type2)
808 {
809 type1 = TYPE_MAIN_VARIANT (type1);
810 type2 = TYPE_MAIN_VARIANT (type2);
811
812 /* Handle the most common case first. */
813 if (type1 == type2)
814 return 1;
815
816 /* If we would have to do structural comparison bail out. */
817 if (TYPE_STRUCTURAL_EQUALITY_P (type1)
818 || TYPE_STRUCTURAL_EQUALITY_P (type2))
819 return -1;
820
821 /* Compare the canonical types. */
822 if (TYPE_CANONICAL (type1) == TYPE_CANONICAL (type2))
823 return 1;
824
825 /* ??? Array types are not properly unified in all cases as we have
826 spurious changes in the index types for example. Removing this
827 causes all sorts of problems with the Fortran frontend. */
828 if (TREE_CODE (type1) == ARRAY_TYPE
829 && TREE_CODE (type2) == ARRAY_TYPE)
830 return -1;
831
832 /* ??? In Ada, an lvalue of an unconstrained type can be used to access an
833 object of one of its constrained subtypes, e.g. when a function with an
834 unconstrained parameter passed by reference is called on an object and
835 inlined. But, even in the case of a fixed size, type and subtypes are
836 not equivalent enough as to share the same TYPE_CANONICAL, since this
837 would mean that conversions between them are useless, whereas they are
838 not (e.g. type and subtypes can have different modes). So, in the end,
839 they are only guaranteed to have the same alias set. */
840 if (get_alias_set (type1) == get_alias_set (type2))
841 return -1;
842
843 /* The types are known to be not equal. */
844 return 0;
845 }
846
847 /* Return true if TYPE is a composite type (i.e. we may apply one of handled
848 components on it). */
849
850 static bool
851 type_has_components_p (tree type)
852 {
853 return AGGREGATE_TYPE_P (type) || VECTOR_TYPE_P (type)
854 || TREE_CODE (type) == COMPLEX_TYPE;
855 }
856
857 /* MATCH1 and MATCH2 which are part of access path of REF1 and REF2
858 respectively are either pointing to same address or are completely
859 disjoint. If PARTIAL_OVERLAP is true, assume that outermost arrays may
860 just partly overlap.
861
862 Try to disambiguate using the access path starting from the match
863 and return false if there is no conflict.
864
865 Helper for aliasing_component_refs_p. */
866
867 static bool
868 aliasing_matching_component_refs_p (tree match1, tree ref1,
869 poly_int64 offset1, poly_int64 max_size1,
870 tree match2, tree ref2,
871 poly_int64 offset2, poly_int64 max_size2,
872 bool partial_overlap)
873 {
874 poly_int64 offadj, sztmp, msztmp;
875 bool reverse;
876
877 if (!partial_overlap)
878 {
879 get_ref_base_and_extent (match2, &offadj, &sztmp, &msztmp, &reverse);
880 offset2 -= offadj;
881 get_ref_base_and_extent (match1, &offadj, &sztmp, &msztmp, &reverse);
882 offset1 -= offadj;
883 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
884 {
885 ++alias_stats.aliasing_component_refs_p_no_alias;
886 return false;
887 }
888 }
889
890 int cmp = nonoverlapping_refs_since_match_p (match1, ref1, match2, ref2,
891 partial_overlap);
892 if (cmp == 1
893 || (cmp == -1 && nonoverlapping_component_refs_p (ref1, ref2)))
894 {
895 ++alias_stats.aliasing_component_refs_p_no_alias;
896 return false;
897 }
898 ++alias_stats.aliasing_component_refs_p_may_alias;
899 return true;
900 }
901
902 /* Return true if REF is reference to zero sized trailing array. I.e.
903 struct foo {int bar; int array[0];} *fooptr;
904 fooptr->array. */
905
906 static bool
907 component_ref_to_zero_sized_trailing_array_p (tree ref)
908 {
909 return (TREE_CODE (ref) == COMPONENT_REF
910 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE
911 && (!TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)))
912 || integer_zerop (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)))))
913 && array_at_struct_end_p (ref));
914 }
915
916 /* Worker for aliasing_component_refs_p. Most parameters match parameters of
917 aliasing_component_refs_p.
918
919 Walk access path REF2 and try to find type matching TYPE1
920 (which is a start of possibly aliasing access path REF1).
921 If match is found, try to disambiguate.
922
923 Return 0 for sucessful disambiguation.
924 Return 1 if match was found but disambiguation failed
925 Return -1 if there is no match.
926 In this case MAYBE_MATCH is set to 0 if there is no type matching TYPE1
927 in access patch REF2 and -1 if we are not sure. */
928
929 static int
930 aliasing_component_refs_walk (tree ref1, tree type1, tree base1,
931 poly_int64 offset1, poly_int64 max_size1,
932 tree end_struct_ref1,
933 tree ref2, tree base2,
934 poly_int64 offset2, poly_int64 max_size2,
935 bool *maybe_match)
936 {
937 tree ref = ref2;
938 int same_p = 0;
939
940 while (true)
941 {
942 /* We walk from inner type to the outer types. If type we see is
943 already too large to be part of type1, terminate the search. */
944 int cmp = compare_type_sizes (type1, TREE_TYPE (ref));
945
946 if (cmp < 0
947 && (!end_struct_ref1
948 || compare_type_sizes (TREE_TYPE (end_struct_ref1),
949 TREE_TYPE (ref)) < 0))
950 break;
951 /* If types may be of same size, see if we can decide about their
952 equality. */
953 if (cmp == 0)
954 {
955 same_p = same_type_for_tbaa (TREE_TYPE (ref), type1);
956 if (same_p == 1)
957 break;
958 /* In case we can't decide whether types are same try to
959 continue looking for the exact match.
960 Remember however that we possibly saw a match
961 to bypass the access path continuations tests we do later. */
962 if (same_p == -1)
963 *maybe_match = true;
964 }
965 if (!handled_component_p (ref))
966 break;
967 ref = TREE_OPERAND (ref, 0);
968 }
969 if (same_p == 1)
970 {
971 bool partial_overlap = false;
972
973 /* We assume that arrays can overlap by multiple of their elements
974 size as tested in gcc.dg/torture/alias-2.c.
975 This partial overlap happen only when both arrays are bases of
976 the access and not contained within another component ref.
977 To be safe we also assume partial overlap for VLAs. */
978 if (TREE_CODE (TREE_TYPE (base1)) == ARRAY_TYPE
979 && (!TYPE_SIZE (TREE_TYPE (base1))
980 || TREE_CODE (TYPE_SIZE (TREE_TYPE (base1))) != INTEGER_CST
981 || ref == base2))
982 {
983 /* Setting maybe_match to true triggers
984 nonoverlapping_component_refs_p test later that still may do
985 useful disambiguation. */
986 *maybe_match = true;
987 partial_overlap = true;
988 }
989 return aliasing_matching_component_refs_p (base1, ref1,
990 offset1, max_size1,
991 ref, ref2,
992 offset2, max_size2,
993 partial_overlap);
994 }
995 return -1;
996 }
997
998 /* Determine if the two component references REF1 and REF2 which are
999 based on access types TYPE1 and TYPE2 and of which at least one is based
1000 on an indirect reference may alias.
1001 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
1002 are the respective alias sets. */
1003
1004 static bool
1005 aliasing_component_refs_p (tree ref1,
1006 alias_set_type ref1_alias_set,
1007 alias_set_type base1_alias_set,
1008 poly_int64 offset1, poly_int64 max_size1,
1009 tree ref2,
1010 alias_set_type ref2_alias_set,
1011 alias_set_type base2_alias_set,
1012 poly_int64 offset2, poly_int64 max_size2)
1013 {
1014 /* If one reference is a component references through pointers try to find a
1015 common base and apply offset based disambiguation. This handles
1016 for example
1017 struct A { int i; int j; } *q;
1018 struct B { struct A a; int k; } *p;
1019 disambiguating q->i and p->a.j. */
1020 tree base1, base2;
1021 tree type1, type2;
1022 bool maybe_match = false;
1023 tree end_struct_ref1 = NULL, end_struct_ref2 = NULL;
1024
1025 /* Choose bases and base types to search for. */
1026 base1 = ref1;
1027 while (handled_component_p (base1))
1028 {
1029 /* Generally access paths are monotous in the size of object. The
1030 exception are trailing arrays of structures. I.e.
1031 struct a {int array[0];};
1032 or
1033 struct a {int array1[0]; int array[];};
1034 Such struct has size 0 but accesses to a.array may have non-zero size.
1035 In this case the size of TREE_TYPE (base1) is smaller than
1036 size of TREE_TYPE (TREE_OPERNAD (base1, 0)).
1037
1038 Because we compare sizes of arrays just by sizes of their elements,
1039 we only need to care about zero sized array fields here. */
1040 if (component_ref_to_zero_sized_trailing_array_p (base1))
1041 {
1042 gcc_checking_assert (!end_struct_ref1);
1043 end_struct_ref1 = base1;
1044 }
1045 if (TREE_CODE (base1) == VIEW_CONVERT_EXPR
1046 || TREE_CODE (base1) == BIT_FIELD_REF)
1047 ref1 = TREE_OPERAND (base1, 0);
1048 base1 = TREE_OPERAND (base1, 0);
1049 }
1050 type1 = TREE_TYPE (base1);
1051 base2 = ref2;
1052 while (handled_component_p (base2))
1053 {
1054 if (component_ref_to_zero_sized_trailing_array_p (base2))
1055 {
1056 gcc_checking_assert (!end_struct_ref2);
1057 end_struct_ref2 = base2;
1058 }
1059 if (TREE_CODE (base2) == VIEW_CONVERT_EXPR
1060 || TREE_CODE (base2) == BIT_FIELD_REF)
1061 ref2 = TREE_OPERAND (base2, 0);
1062 base2 = TREE_OPERAND (base2, 0);
1063 }
1064 type2 = TREE_TYPE (base2);
1065
1066 /* Now search for the type1 in the access path of ref2. This
1067 would be a common base for doing offset based disambiguation on.
1068 This however only makes sense if type2 is big enough to hold type1. */
1069 int cmp_outer = compare_type_sizes (type2, type1);
1070
1071 /* If type2 is big enough to contain type1 walk its access path.
1072 We also need to care of arrays at the end of structs that may extend
1073 beyond the end of structure. */
1074 if (cmp_outer >= 0
1075 || (end_struct_ref2
1076 && compare_type_sizes (TREE_TYPE (end_struct_ref2), type1) >= 0))
1077 {
1078 int res = aliasing_component_refs_walk (ref1, type1, base1,
1079 offset1, max_size1,
1080 end_struct_ref1,
1081 ref2, base2, offset2, max_size2,
1082 &maybe_match);
1083 if (res != -1)
1084 return res;
1085 }
1086
1087 /* If we didn't find a common base, try the other way around. */
1088 if (cmp_outer <= 0
1089 || (end_struct_ref1
1090 && compare_type_sizes (TREE_TYPE (end_struct_ref1), type1) <= 0))
1091 {
1092 int res = aliasing_component_refs_walk (ref2, type2, base2,
1093 offset2, max_size2,
1094 end_struct_ref2,
1095 ref1, base1, offset1, max_size1,
1096 &maybe_match);
1097 if (res != -1)
1098 return res;
1099 }
1100
1101 /* In the following code we make an assumption that the types in access
1102 paths do not overlap and thus accesses alias only if one path can be
1103 continuation of another. If we was not able to decide about equivalence,
1104 we need to give up. */
1105 if (maybe_match)
1106 {
1107 if (!nonoverlapping_component_refs_p (ref1, ref2))
1108 {
1109 ++alias_stats.aliasing_component_refs_p_may_alias;
1110 return true;
1111 }
1112 ++alias_stats.aliasing_component_refs_p_no_alias;
1113 return false;
1114 }
1115
1116 /* If we have two type access paths B1.path1 and B2.path2 they may
1117 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
1118 But we can still have a path that goes B1.path1...B2.path2 with
1119 a part that we do not see. So we can only disambiguate now
1120 if there is no B2 in the tail of path1 and no B1 on the
1121 tail of path2. */
1122 if (compare_type_sizes (TREE_TYPE (ref2), type1) >= 0
1123 && (!end_struct_ref1
1124 || compare_type_sizes (TREE_TYPE (ref2),
1125 TREE_TYPE (end_struct_ref1)) >= 0)
1126 && type_has_components_p (TREE_TYPE (ref2))
1127 && (base1_alias_set == ref2_alias_set
1128 || alias_set_subset_of (base1_alias_set, ref2_alias_set)))
1129 {
1130 ++alias_stats.aliasing_component_refs_p_may_alias;
1131 return true;
1132 }
1133 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
1134 if (compare_type_sizes (TREE_TYPE (ref1), type2) >= 0
1135 && (!end_struct_ref2
1136 || compare_type_sizes (TREE_TYPE (ref1),
1137 TREE_TYPE (end_struct_ref2)) >= 0)
1138 && type_has_components_p (TREE_TYPE (ref1))
1139 && (base2_alias_set == ref1_alias_set
1140 || alias_set_subset_of (base2_alias_set, ref1_alias_set)))
1141 {
1142 ++alias_stats.aliasing_component_refs_p_may_alias;
1143 return true;
1144 }
1145 ++alias_stats.aliasing_component_refs_p_no_alias;
1146 return false;
1147 }
1148
1149 /* FIELD1 and FIELD2 are two fields of component refs. We assume
1150 that bases of both component refs are either equivalent or nonoverlapping.
1151 We do not assume that the containers of FIELD1 and FIELD2 are of the
1152 same type or size.
1153
1154 Return 0 in case the base address of component_refs are same then
1155 FIELD1 and FIELD2 have same address. Note that FIELD1 and FIELD2
1156 may not be of same type or size.
1157
1158 Return 1 if FIELD1 and FIELD2 are non-overlapping.
1159
1160 Return -1 otherwise.
1161
1162 Main difference between 0 and -1 is to let
1163 nonoverlapping_component_refs_since_match_p discover the semantically
1164 equivalent part of the access path.
1165
1166 Note that this function is used even with -fno-strict-aliasing
1167 and makes use of no TBAA assumptions. */
1168
1169 static int
1170 nonoverlapping_component_refs_p_1 (const_tree field1, const_tree field2)
1171 {
1172 /* If both fields are of the same type, we could save hard work of
1173 comparing offsets. */
1174 tree type1 = DECL_CONTEXT (field1);
1175 tree type2 = DECL_CONTEXT (field2);
1176
1177 if (TREE_CODE (type1) == RECORD_TYPE
1178 && DECL_BIT_FIELD_REPRESENTATIVE (field1))
1179 field1 = DECL_BIT_FIELD_REPRESENTATIVE (field1);
1180 if (TREE_CODE (type2) == RECORD_TYPE
1181 && DECL_BIT_FIELD_REPRESENTATIVE (field2))
1182 field2 = DECL_BIT_FIELD_REPRESENTATIVE (field2);
1183
1184 /* ??? Bitfields can overlap at RTL level so punt on them.
1185 FIXME: RTL expansion should be fixed by adjusting the access path
1186 when producing MEM_ATTRs for MEMs which are wider than
1187 the bitfields similarly as done in set_mem_attrs_minus_bitpos. */
1188 if (DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2))
1189 return -1;
1190
1191 /* Assume that different FIELD_DECLs never overlap within a RECORD_TYPE. */
1192 if (type1 == type2 && TREE_CODE (type1) == RECORD_TYPE)
1193 return field1 != field2;
1194
1195 /* In common case the offsets and bit offsets will be the same.
1196 However if frontends do not agree on the alignment, they may be
1197 different even if they actually represent same address.
1198 Try the common case first and if that fails calcualte the
1199 actual bit offset. */
1200 if (tree_int_cst_equal (DECL_FIELD_OFFSET (field1),
1201 DECL_FIELD_OFFSET (field2))
1202 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (field1),
1203 DECL_FIELD_BIT_OFFSET (field2)))
1204 return 0;
1205
1206 /* Note that it may be possible to use component_ref_field_offset
1207 which would provide offsets as trees. However constructing and folding
1208 trees is expensive and does not seem to be worth the compile time
1209 cost. */
1210
1211 poly_uint64 offset1, offset2;
1212 poly_uint64 bit_offset1, bit_offset2;
1213
1214 if (poly_int_tree_p (DECL_FIELD_OFFSET (field1), &offset1)
1215 && poly_int_tree_p (DECL_FIELD_OFFSET (field2), &offset2)
1216 && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field1), &bit_offset1)
1217 && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field2), &bit_offset2))
1218 {
1219 offset1 = (offset1 << LOG2_BITS_PER_UNIT) + bit_offset1;
1220 offset2 = (offset2 << LOG2_BITS_PER_UNIT) + bit_offset2;
1221
1222 if (known_eq (offset1, offset2))
1223 return 0;
1224
1225 poly_uint64 size1, size2;
1226
1227 if (poly_int_tree_p (DECL_SIZE (field1), &size1)
1228 && poly_int_tree_p (DECL_SIZE (field2), &size2)
1229 && !ranges_maybe_overlap_p (offset1, size1, offset2, size2))
1230 return 1;
1231 }
1232 /* Resort to slower overlap checking by looking for matching types in
1233 the middle of access path. */
1234 return -1;
1235 }
1236
1237 /* Return low bound of array. Do not produce new trees
1238 and thus do not care about particular type of integer constant
1239 and placeholder exprs. */
1240
1241 static tree
1242 cheap_array_ref_low_bound (tree ref)
1243 {
1244 tree domain_type = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
1245
1246 /* Avoid expensive array_ref_low_bound.
1247 low bound is either stored in operand2, or it is TYPE_MIN_VALUE of domain
1248 type or it is zero. */
1249 if (TREE_OPERAND (ref, 2))
1250 return TREE_OPERAND (ref, 2);
1251 else if (domain_type && TYPE_MIN_VALUE (domain_type))
1252 return TYPE_MIN_VALUE (domain_type);
1253 else
1254 return integer_zero_node;
1255 }
1256
1257 /* REF1 and REF2 are ARRAY_REFs with either same base address or which are
1258 completely disjoint.
1259
1260 Return 1 if the refs are non-overlapping.
1261 Return 0 if they are possibly overlapping but if so the overlap again
1262 starts on the same address.
1263 Return -1 otherwise. */
1264
1265 int
1266 nonoverlapping_array_refs_p (tree ref1, tree ref2)
1267 {
1268 tree index1 = TREE_OPERAND (ref1, 1);
1269 tree index2 = TREE_OPERAND (ref2, 1);
1270 tree low_bound1 = cheap_array_ref_low_bound(ref1);
1271 tree low_bound2 = cheap_array_ref_low_bound(ref2);
1272
1273 /* Handle zero offsets first: we do not need to match type size in this
1274 case. */
1275 if (operand_equal_p (index1, low_bound1, 0)
1276 && operand_equal_p (index2, low_bound2, 0))
1277 return 0;
1278
1279 /* If type sizes are different, give up.
1280
1281 Avoid expensive array_ref_element_size.
1282 If operand 3 is present it denotes size in the alignmnet units.
1283 Otherwise size is TYPE_SIZE of the element type.
1284 Handle only common cases where types are of the same "kind". */
1285 if ((TREE_OPERAND (ref1, 3) == NULL) != (TREE_OPERAND (ref2, 3) == NULL))
1286 return -1;
1287
1288 tree elmt_type1 = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref1, 0)));
1289 tree elmt_type2 = TREE_TYPE (TREE_TYPE (TREE_OPERAND (ref2, 0)));
1290
1291 if (TREE_OPERAND (ref1, 3))
1292 {
1293 if (TYPE_ALIGN (elmt_type1) != TYPE_ALIGN (elmt_type2)
1294 || !operand_equal_p (TREE_OPERAND (ref1, 3),
1295 TREE_OPERAND (ref2, 3), 0))
1296 return -1;
1297 }
1298 else
1299 {
1300 if (!operand_equal_p (TYPE_SIZE_UNIT (elmt_type1),
1301 TYPE_SIZE_UNIT (elmt_type2), 0))
1302 return -1;
1303 }
1304
1305 /* Since we know that type sizes are the same, there is no need to return
1306 -1 after this point. Partial overlap can not be introduced. */
1307
1308 /* We may need to fold trees in this case.
1309 TODO: Handle integer constant case at least. */
1310 if (!operand_equal_p (low_bound1, low_bound2, 0))
1311 return 0;
1312
1313 if (TREE_CODE (index1) == INTEGER_CST && TREE_CODE (index2) == INTEGER_CST)
1314 {
1315 if (tree_int_cst_equal (index1, index2))
1316 return 0;
1317 return 1;
1318 }
1319 /* TODO: We can use VRP to further disambiguate here. */
1320 return 0;
1321 }
1322
1323 /* Try to disambiguate REF1 and REF2 under the assumption that MATCH1 and
1324 MATCH2 either point to the same address or are disjoint.
1325 MATCH1 and MATCH2 are assumed to be ref in the access path of REF1 and REF2
1326 respectively or NULL in the case we established equivalence of bases.
1327 If PARTIAL_OVERLAP is true assume that the toplevel arrays may actually
1328 overlap by exact multiply of their element size.
1329
1330 This test works by matching the initial segment of the access path
1331 and does not rely on TBAA thus is safe for !flag_strict_aliasing if
1332 match was determined without use of TBAA oracle.
1333
1334 Return 1 if we can determine that component references REF1 and REF2,
1335 that are within a common DECL, cannot overlap.
1336
1337 Return 0 if paths are same and thus there is nothing to disambiguate more
1338 (i.e. there is must alias assuming there is must alias between MATCH1 and
1339 MATCH2)
1340
1341 Return -1 if we can not determine 0 or 1 - this happens when we met
1342 non-matching types was met in the path.
1343 In this case it may make sense to continue by other disambiguation
1344 oracles. */
1345
1346 static int
1347 nonoverlapping_refs_since_match_p (tree match1, tree ref1,
1348 tree match2, tree ref2,
1349 bool partial_overlap)
1350 {
1351 /* Early return if there are no references to match, we do not need
1352 to walk the access paths.
1353
1354 Do not consider this as may-alias for stats - it is more useful
1355 to have information how many disambiguations happened provided that
1356 the query was meaningful. */
1357
1358 if (match1 == ref1 || !handled_component_p (ref1)
1359 || match2 == ref2 || !handled_component_p (ref2))
1360 return -1;
1361
1362 auto_vec<tree, 16> component_refs1;
1363 auto_vec<tree, 16> component_refs2;
1364
1365 /* Create the stack of handled components for REF1. */
1366 while (handled_component_p (ref1) && ref1 != match1)
1367 {
1368 if (TREE_CODE (ref1) == VIEW_CONVERT_EXPR
1369 || TREE_CODE (ref1) == BIT_FIELD_REF)
1370 component_refs1.truncate (0);
1371 else
1372 component_refs1.safe_push (ref1);
1373 ref1 = TREE_OPERAND (ref1, 0);
1374 }
1375
1376 /* Create the stack of handled components for REF2. */
1377 while (handled_component_p (ref2) && ref2 != match2)
1378 {
1379 if (TREE_CODE (ref2) == VIEW_CONVERT_EXPR
1380 || TREE_CODE (ref2) == BIT_FIELD_REF)
1381 component_refs2.truncate (0);
1382 else
1383 component_refs2.safe_push (ref2);
1384 ref2 = TREE_OPERAND (ref2, 0);
1385 }
1386
1387 bool mem_ref1 = TREE_CODE (ref1) == MEM_REF && ref1 != match1;
1388 bool mem_ref2 = TREE_CODE (ref2) == MEM_REF && ref2 != match2;
1389
1390 /* If only one of access path starts with MEM_REF check that offset is 0
1391 so the addresses stays the same after stripping it.
1392 TODO: In this case we may walk the other access path until we get same
1393 offset.
1394
1395 If both starts with MEM_REF, offset has to be same. */
1396 if ((mem_ref1 && !mem_ref2 && !integer_zerop (TREE_OPERAND (ref1, 1)))
1397 || (mem_ref2 && !mem_ref1 && !integer_zerop (TREE_OPERAND (ref2, 1)))
1398 || (mem_ref1 && mem_ref2
1399 && !tree_int_cst_equal (TREE_OPERAND (ref1, 1),
1400 TREE_OPERAND (ref2, 1))))
1401 {
1402 ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1403 return -1;
1404 }
1405
1406 /* TARGET_MEM_REF are never wrapped in handled components, so we do not need
1407 to handle them here at all. */
1408 gcc_checking_assert (TREE_CODE (ref1) != TARGET_MEM_REF
1409 && TREE_CODE (ref2) != TARGET_MEM_REF);
1410
1411 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
1412 rank. This is sufficient because we start from the same DECL and you
1413 cannot reference several fields at a time with COMPONENT_REFs (unlike
1414 with ARRAY_RANGE_REFs for arrays) so you always need the same number
1415 of them to access a sub-component, unless you're in a union, in which
1416 case the return value will precisely be false. */
1417 while (true)
1418 {
1419 /* Track if we seen unmatched ref with non-zero offset. In this case
1420 we must look for partial overlaps. */
1421 bool seen_unmatched_ref_p = false;
1422
1423 /* First match ARRAY_REFs an try to disambiguate. */
1424 if (!component_refs1.is_empty ()
1425 && !component_refs2.is_empty ())
1426 {
1427 unsigned int narray_refs1=0, narray_refs2=0;
1428
1429 /* We generally assume that both access paths starts by same sequence
1430 of refs. However if number of array refs is not in sync, try
1431 to recover and pop elts until number match. This helps the case
1432 where one access path starts by array and other by element. */
1433 for (narray_refs1 = 0; narray_refs1 < component_refs1.length ();
1434 narray_refs1++)
1435 if (TREE_CODE (component_refs1 [component_refs1.length()
1436 - 1 - narray_refs1]) != ARRAY_REF)
1437 break;
1438
1439 for (narray_refs2 = 0; narray_refs2 < component_refs2.length ();
1440 narray_refs2++)
1441 if (TREE_CODE (component_refs2 [component_refs2.length()
1442 - 1 - narray_refs2]) != ARRAY_REF)
1443 break;
1444 for (; narray_refs1 > narray_refs2; narray_refs1--)
1445 {
1446 ref1 = component_refs1.pop ();
1447
1448 /* If index is non-zero we need to check whether the reference
1449 does not break the main invariant that bases are either
1450 disjoint or equal. Consider the example:
1451
1452 unsigned char out[][1];
1453 out[1]="a";
1454 out[i][0];
1455
1456 Here bases out and out are same, but after removing the
1457 [i] index, this invariant no longer holds, because
1458 out[i] points to the middle of array out.
1459
1460 TODO: If size of type of the skipped reference is an integer
1461 multiply of the size of type of the other reference this
1462 invariant can be verified, but even then it is not completely
1463 safe with !flag_strict_aliasing if the other reference contains
1464 unbounded array accesses.
1465 See */
1466
1467 if (!operand_equal_p (TREE_OPERAND (ref1, 1),
1468 cheap_array_ref_low_bound (ref1), 0))
1469 return 0;
1470 }
1471 for (; narray_refs2 > narray_refs1; narray_refs2--)
1472 {
1473 ref2 = component_refs2.pop ();
1474 if (!operand_equal_p (TREE_OPERAND (ref2, 1),
1475 cheap_array_ref_low_bound (ref2), 0))
1476 return 0;
1477 }
1478 /* Try to disambiguate matched arrays. */
1479 for (unsigned int i = 0; i < narray_refs1; i++)
1480 {
1481 int cmp = nonoverlapping_array_refs_p (component_refs1.pop (),
1482 component_refs2.pop ());
1483 if (cmp == 1 && !partial_overlap)
1484 {
1485 ++alias_stats
1486 .nonoverlapping_refs_since_match_p_no_alias;
1487 return 1;
1488 }
1489 if (cmp == -1)
1490 {
1491 seen_unmatched_ref_p = true;
1492 /* We can not maintain the invariant that bases are either
1493 same or completely disjoint. However we can still recover
1494 from type based alias analysis if we reach referneces to
1495 same sizes. We do not attempt to match array sizes, so
1496 just finish array walking and look for component refs. */
1497 if (!flag_strict_aliasing)
1498 {
1499 ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1500 return -1;
1501 }
1502 for (i++; i < narray_refs1; i++)
1503 {
1504 component_refs1.pop ();
1505 component_refs2.pop ();
1506 }
1507 break;
1508 }
1509 partial_overlap = false;
1510 }
1511 }
1512
1513 /* Next look for component_refs. */
1514 do
1515 {
1516 if (component_refs1.is_empty ())
1517 {
1518 ++alias_stats
1519 .nonoverlapping_refs_since_match_p_must_overlap;
1520 return 0;
1521 }
1522 ref1 = component_refs1.pop ();
1523 if (TREE_CODE (ref1) != COMPONENT_REF)
1524 {
1525 seen_unmatched_ref_p = true;
1526 if (!flag_strict_aliasing)
1527 {
1528 ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1529 return -1;
1530 }
1531 }
1532 }
1533 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
1534
1535 do
1536 {
1537 if (component_refs2.is_empty ())
1538 {
1539 ++alias_stats
1540 .nonoverlapping_refs_since_match_p_must_overlap;
1541 return 0;
1542 }
1543 ref2 = component_refs2.pop ();
1544 if (TREE_CODE (ref2) != COMPONENT_REF)
1545 {
1546 if (!flag_strict_aliasing)
1547 {
1548 ++alias_stats.nonoverlapping_refs_since_match_p_may_alias;
1549 return -1;
1550 }
1551 seen_unmatched_ref_p = true;
1552 }
1553 }
1554 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
1555
1556 /* BIT_FIELD_REF and VIEW_CONVERT_EXPR are taken off the vectors
1557 earlier. */
1558 gcc_checking_assert (TREE_CODE (ref1) == COMPONENT_REF
1559 && TREE_CODE (ref2) == COMPONENT_REF);
1560
1561 tree field1 = TREE_OPERAND (ref1, 1);
1562 tree field2 = TREE_OPERAND (ref2, 1);
1563
1564 /* ??? We cannot simply use the type of operand #0 of the refs here
1565 as the Fortran compiler smuggles type punning into COMPONENT_REFs
1566 for common blocks instead of using unions like everyone else. */
1567 tree type1 = DECL_CONTEXT (field1);
1568 tree type2 = DECL_CONTEXT (field2);
1569
1570 partial_overlap = false;
1571
1572 gcc_checking_assert (!seen_unmatched_ref_p || flag_strict_aliasing);
1573
1574 /* If we skipped array refs on type of different sizes, we can
1575 no longer be sure that there are not partial overlaps. */
1576 if (seen_unmatched_ref_p
1577 && !operand_equal_p (TYPE_SIZE (type1), TYPE_SIZE (type2), 0))
1578 {
1579 ++alias_stats
1580 .nonoverlapping_refs_since_match_p_may_alias;
1581 return -1;
1582 }
1583
1584 int cmp = nonoverlapping_component_refs_p_1 (field1, field2);
1585 if (cmp == -1)
1586 {
1587 ++alias_stats
1588 .nonoverlapping_refs_since_match_p_may_alias;
1589 return -1;
1590 }
1591 else if (cmp == 1)
1592 {
1593 ++alias_stats
1594 .nonoverlapping_refs_since_match_p_no_alias;
1595 return 1;
1596 }
1597 }
1598
1599 ++alias_stats.nonoverlapping_refs_since_match_p_must_overlap;
1600 return 0;
1601 }
1602
1603 /* Return TYPE_UID which can be used to match record types we consider
1604 same for TBAA purposes. */
1605
1606 static inline int
1607 ncr_type_uid (const_tree field)
1608 {
1609 /* ??? We cannot simply use the type of operand #0 of the refs here
1610 as the Fortran compiler smuggles type punning into COMPONENT_REFs
1611 for common blocks instead of using unions like everyone else. */
1612 tree type = DECL_FIELD_CONTEXT (field);
1613 /* With LTO types considered same_type_for_tbaa_p
1614 from different translation unit may not have same
1615 main variant. They however have same TYPE_CANONICAL. */
1616 if (TYPE_CANONICAL (type))
1617 return TYPE_UID (TYPE_CANONICAL (type));
1618 return TYPE_UID (type);
1619 }
1620
1621 /* qsort compare function to sort FIELD_DECLs after their
1622 DECL_FIELD_CONTEXT TYPE_UID. */
1623
1624 static inline int
1625 ncr_compar (const void *field1_, const void *field2_)
1626 {
1627 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
1628 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
1629 unsigned int uid1 = ncr_type_uid (field1);
1630 unsigned int uid2 = ncr_type_uid (field2);
1631
1632 if (uid1 < uid2)
1633 return -1;
1634 else if (uid1 > uid2)
1635 return 1;
1636 return 0;
1637 }
1638
1639 /* Return true if we can determine that the fields referenced cannot
1640 overlap for any pair of objects. This relies on TBAA. */
1641
1642 static bool
1643 nonoverlapping_component_refs_p (const_tree x, const_tree y)
1644 {
1645 /* Early return if we have nothing to do.
1646
1647 Do not consider this as may-alias for stats - it is more useful
1648 to have information how many disambiguations happened provided that
1649 the query was meaningful. */
1650 if (!flag_strict_aliasing
1651 || !x || !y
1652 || !handled_component_p (x)
1653 || !handled_component_p (y))
1654 return false;
1655
1656 auto_vec<const_tree, 16> fieldsx;
1657 while (handled_component_p (x))
1658 {
1659 if (TREE_CODE (x) == COMPONENT_REF)
1660 {
1661 tree field = TREE_OPERAND (x, 1);
1662 tree type = DECL_FIELD_CONTEXT (field);
1663 if (TREE_CODE (type) == RECORD_TYPE)
1664 fieldsx.safe_push (field);
1665 }
1666 else if (TREE_CODE (x) == VIEW_CONVERT_EXPR
1667 || TREE_CODE (x) == BIT_FIELD_REF)
1668 fieldsx.truncate (0);
1669 x = TREE_OPERAND (x, 0);
1670 }
1671 if (fieldsx.length () == 0)
1672 return false;
1673 auto_vec<const_tree, 16> fieldsy;
1674 while (handled_component_p (y))
1675 {
1676 if (TREE_CODE (y) == COMPONENT_REF)
1677 {
1678 tree field = TREE_OPERAND (y, 1);
1679 tree type = DECL_FIELD_CONTEXT (field);
1680 if (TREE_CODE (type) == RECORD_TYPE)
1681 fieldsy.safe_push (TREE_OPERAND (y, 1));
1682 }
1683 else if (TREE_CODE (y) == VIEW_CONVERT_EXPR
1684 || TREE_CODE (y) == BIT_FIELD_REF)
1685 fieldsy.truncate (0);
1686 y = TREE_OPERAND (y, 0);
1687 }
1688 if (fieldsy.length () == 0)
1689 {
1690 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1691 return false;
1692 }
1693
1694 /* Most common case first. */
1695 if (fieldsx.length () == 1
1696 && fieldsy.length () == 1)
1697 {
1698 if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldsx[0]),
1699 DECL_FIELD_CONTEXT (fieldsy[0])) == 1
1700 && nonoverlapping_component_refs_p_1 (fieldsx[0], fieldsy[0]) == 1)
1701 {
1702 ++alias_stats.nonoverlapping_component_refs_p_no_alias;
1703 return true;
1704 }
1705 else
1706 {
1707 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1708 return false;
1709 }
1710 }
1711
1712 if (fieldsx.length () == 2)
1713 {
1714 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
1715 std::swap (fieldsx[0], fieldsx[1]);
1716 }
1717 else
1718 fieldsx.qsort (ncr_compar);
1719
1720 if (fieldsy.length () == 2)
1721 {
1722 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
1723 std::swap (fieldsy[0], fieldsy[1]);
1724 }
1725 else
1726 fieldsy.qsort (ncr_compar);
1727
1728 unsigned i = 0, j = 0;
1729 do
1730 {
1731 const_tree fieldx = fieldsx[i];
1732 const_tree fieldy = fieldsy[j];
1733
1734 /* We're left with accessing different fields of a structure,
1735 no possible overlap. */
1736 if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldx),
1737 DECL_FIELD_CONTEXT (fieldy)) == 1
1738 && nonoverlapping_component_refs_p_1 (fieldx, fieldy) == 1)
1739 {
1740 ++alias_stats.nonoverlapping_component_refs_p_no_alias;
1741 return true;
1742 }
1743
1744 if (ncr_type_uid (fieldx) < ncr_type_uid (fieldy))
1745 {
1746 i++;
1747 if (i == fieldsx.length ())
1748 break;
1749 }
1750 else
1751 {
1752 j++;
1753 if (j == fieldsy.length ())
1754 break;
1755 }
1756 }
1757 while (1);
1758
1759 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1760 return false;
1761 }
1762
1763
1764 /* Return true if two memory references based on the variables BASE1
1765 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1766 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1767 if non-NULL are the complete memory reference trees. */
1768
1769 static bool
1770 decl_refs_may_alias_p (tree ref1, tree base1,
1771 poly_int64 offset1, poly_int64 max_size1,
1772 poly_int64 size1,
1773 tree ref2, tree base2,
1774 poly_int64 offset2, poly_int64 max_size2,
1775 poly_int64 size2)
1776 {
1777 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1778
1779 /* If both references are based on different variables, they cannot alias. */
1780 if (compare_base_decls (base1, base2) == 0)
1781 return false;
1782
1783 /* If both references are based on the same variable, they cannot alias if
1784 the accesses do not overlap. */
1785 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
1786 return false;
1787
1788 /* If there is must alias, there is no use disambiguating further. */
1789 if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
1790 return true;
1791
1792 /* For components with variable position, the above test isn't sufficient,
1793 so we disambiguate component references manually. */
1794 if (ref1 && ref2
1795 && handled_component_p (ref1) && handled_component_p (ref2)
1796 && nonoverlapping_refs_since_match_p (NULL, ref1, NULL, ref2, false) == 1)
1797 return false;
1798
1799 return true;
1800 }
1801
1802 /* Return true if an indirect reference based on *PTR1 constrained
1803 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1804 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1805 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1806 in which case they are computed on-demand. REF1 and REF2
1807 if non-NULL are the complete memory reference trees. */
1808
1809 static bool
1810 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1811 poly_int64 offset1, poly_int64 max_size1,
1812 poly_int64 size1,
1813 alias_set_type ref1_alias_set,
1814 alias_set_type base1_alias_set,
1815 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1816 poly_int64 offset2, poly_int64 max_size2,
1817 poly_int64 size2,
1818 alias_set_type ref2_alias_set,
1819 alias_set_type base2_alias_set, bool tbaa_p)
1820 {
1821 tree ptr1;
1822 tree ptrtype1, dbase2;
1823
1824 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1825 || TREE_CODE (base1) == TARGET_MEM_REF)
1826 && DECL_P (base2));
1827
1828 ptr1 = TREE_OPERAND (base1, 0);
1829 poly_offset_int moff = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
1830
1831 /* If only one reference is based on a variable, they cannot alias if
1832 the pointer access is beyond the extent of the variable access.
1833 (the pointer base cannot validly point to an offset less than zero
1834 of the variable).
1835 ??? IVOPTs creates bases that do not honor this restriction,
1836 so do not apply this optimization for TARGET_MEM_REFs. */
1837 if (TREE_CODE (base1) != TARGET_MEM_REF
1838 && !ranges_maybe_overlap_p (offset1 + moff, -1, offset2, max_size2))
1839 return false;
1840 /* They also cannot alias if the pointer may not point to the decl. */
1841 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1842 return false;
1843
1844 /* Disambiguations that rely on strict aliasing rules follow. */
1845 if (!flag_strict_aliasing || !tbaa_p)
1846 return true;
1847
1848 /* If the alias set for a pointer access is zero all bets are off. */
1849 if (base1_alias_set == 0 || base2_alias_set == 0)
1850 return true;
1851
1852 /* When we are trying to disambiguate an access with a pointer dereference
1853 as base versus one with a decl as base we can use both the size
1854 of the decl and its dynamic type for extra disambiguation.
1855 ??? We do not know anything about the dynamic type of the decl
1856 other than that its alias-set contains base2_alias_set as a subset
1857 which does not help us here. */
1858 /* As we know nothing useful about the dynamic type of the decl just
1859 use the usual conflict check rather than a subset test.
1860 ??? We could introduce -fvery-strict-aliasing when the language
1861 does not allow decls to have a dynamic type that differs from their
1862 static type. Then we can check
1863 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1864 if (base1_alias_set != base2_alias_set
1865 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1866 return false;
1867
1868 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1869
1870 /* If the size of the access relevant for TBAA through the pointer
1871 is bigger than the size of the decl we can't possibly access the
1872 decl via that pointer. */
1873 if (/* ??? This in turn may run afoul when a decl of type T which is
1874 a member of union type U is accessed through a pointer to
1875 type U and sizeof T is smaller than sizeof U. */
1876 TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1877 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1878 && compare_sizes (DECL_SIZE (base2),
1879 TYPE_SIZE (TREE_TYPE (ptrtype1))) < 0)
1880 return false;
1881
1882 if (!ref2)
1883 return true;
1884
1885 /* If the decl is accessed via a MEM_REF, reconstruct the base
1886 we can use for TBAA and an appropriately adjusted offset. */
1887 dbase2 = ref2;
1888 while (handled_component_p (dbase2))
1889 dbase2 = TREE_OPERAND (dbase2, 0);
1890 poly_int64 doffset1 = offset1;
1891 poly_offset_int doffset2 = offset2;
1892 if (TREE_CODE (dbase2) == MEM_REF
1893 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1894 {
1895 doffset2 -= mem_ref_offset (dbase2) << LOG2_BITS_PER_UNIT;
1896 tree ptrtype2 = TREE_TYPE (TREE_OPERAND (dbase2, 1));
1897 /* If second reference is view-converted, give up now. */
1898 if (same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (ptrtype2)) != 1)
1899 return true;
1900 }
1901
1902 /* If first reference is view-converted, give up now. */
1903 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1)
1904 return true;
1905
1906 /* If both references are through the same type, they do not alias
1907 if the accesses do not overlap. This does extra disambiguation
1908 for mixed/pointer accesses but requires strict aliasing.
1909 For MEM_REFs we require that the component-ref offset we computed
1910 is relative to the start of the type which we ensure by
1911 comparing rvalue and access type and disregarding the constant
1912 pointer offset.
1913
1914 But avoid treating variable length arrays as "objects", instead assume they
1915 can overlap by an exact multiple of their element size.
1916 See gcc.dg/torture/alias-2.c. */
1917 if (((TREE_CODE (base1) != TARGET_MEM_REF
1918 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1919 && (TREE_CODE (dbase2) != TARGET_MEM_REF
1920 || (!TMR_INDEX (dbase2) && !TMR_INDEX2 (dbase2))))
1921 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1)
1922 {
1923 bool partial_overlap = (TREE_CODE (TREE_TYPE (base1)) == ARRAY_TYPE
1924 && (TYPE_SIZE (TREE_TYPE (base1))
1925 && TREE_CODE (TYPE_SIZE (TREE_TYPE (base1)))
1926 != INTEGER_CST));
1927 if (!partial_overlap
1928 && !ranges_maybe_overlap_p (doffset1, max_size1, doffset2, max_size2))
1929 return false;
1930 if (!ref1 || !ref2
1931 /* If there is must alias, there is no use disambiguating further. */
1932 || (!partial_overlap
1933 && known_eq (size1, max_size1) && known_eq (size2, max_size2)))
1934 return true;
1935 int res = nonoverlapping_refs_since_match_p (base1, ref1, base2, ref2,
1936 partial_overlap);
1937 if (res == -1)
1938 return !nonoverlapping_component_refs_p (ref1, ref2);
1939 return !res;
1940 }
1941
1942 /* Do access-path based disambiguation. */
1943 if (ref1 && ref2
1944 && (handled_component_p (ref1) || handled_component_p (ref2)))
1945 return aliasing_component_refs_p (ref1,
1946 ref1_alias_set, base1_alias_set,
1947 offset1, max_size1,
1948 ref2,
1949 ref2_alias_set, base2_alias_set,
1950 offset2, max_size2);
1951
1952 return true;
1953 }
1954
1955 /* Return true if two indirect references based on *PTR1
1956 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1957 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1958 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1959 in which case they are computed on-demand. REF1 and REF2
1960 if non-NULL are the complete memory reference trees. */
1961
1962 static bool
1963 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1964 poly_int64 offset1, poly_int64 max_size1,
1965 poly_int64 size1,
1966 alias_set_type ref1_alias_set,
1967 alias_set_type base1_alias_set,
1968 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1969 poly_int64 offset2, poly_int64 max_size2,
1970 poly_int64 size2,
1971 alias_set_type ref2_alias_set,
1972 alias_set_type base2_alias_set, bool tbaa_p)
1973 {
1974 tree ptr1;
1975 tree ptr2;
1976 tree ptrtype1, ptrtype2;
1977
1978 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1979 || TREE_CODE (base1) == TARGET_MEM_REF)
1980 && (TREE_CODE (base2) == MEM_REF
1981 || TREE_CODE (base2) == TARGET_MEM_REF));
1982
1983 ptr1 = TREE_OPERAND (base1, 0);
1984 ptr2 = TREE_OPERAND (base2, 0);
1985
1986 /* If both bases are based on pointers they cannot alias if they may not
1987 point to the same memory object or if they point to the same object
1988 and the accesses do not overlap. */
1989 if ((!cfun || gimple_in_ssa_p (cfun))
1990 && operand_equal_p (ptr1, ptr2, 0)
1991 && (((TREE_CODE (base1) != TARGET_MEM_REF
1992 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1993 && (TREE_CODE (base2) != TARGET_MEM_REF
1994 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1995 || (TREE_CODE (base1) == TARGET_MEM_REF
1996 && TREE_CODE (base2) == TARGET_MEM_REF
1997 && (TMR_STEP (base1) == TMR_STEP (base2)
1998 || (TMR_STEP (base1) && TMR_STEP (base2)
1999 && operand_equal_p (TMR_STEP (base1),
2000 TMR_STEP (base2), 0)))
2001 && (TMR_INDEX (base1) == TMR_INDEX (base2)
2002 || (TMR_INDEX (base1) && TMR_INDEX (base2)
2003 && operand_equal_p (TMR_INDEX (base1),
2004 TMR_INDEX (base2), 0)))
2005 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
2006 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
2007 && operand_equal_p (TMR_INDEX2 (base1),
2008 TMR_INDEX2 (base2), 0))))))
2009 {
2010 poly_offset_int moff1 = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
2011 poly_offset_int moff2 = mem_ref_offset (base2) << LOG2_BITS_PER_UNIT;
2012 if (!ranges_maybe_overlap_p (offset1 + moff1, max_size1,
2013 offset2 + moff2, max_size2))
2014 return false;
2015 /* If there is must alias, there is no use disambiguating further. */
2016 if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
2017 return true;
2018 if (ref1 && ref2)
2019 {
2020 int res = nonoverlapping_refs_since_match_p (NULL, ref1, NULL, ref2,
2021 false);
2022 if (res != -1)
2023 return !res;
2024 }
2025 }
2026 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
2027 return false;
2028
2029 /* Disambiguations that rely on strict aliasing rules follow. */
2030 if (!flag_strict_aliasing || !tbaa_p)
2031 return true;
2032
2033 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
2034 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
2035
2036 /* If the alias set for a pointer access is zero all bets are off. */
2037 if (base1_alias_set == 0
2038 || base2_alias_set == 0)
2039 return true;
2040
2041 /* Do type-based disambiguation. */
2042 if (base1_alias_set != base2_alias_set
2043 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
2044 return false;
2045
2046 /* If either reference is view-converted, give up now. */
2047 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
2048 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
2049 return true;
2050
2051 /* If both references are through the same type, they do not alias
2052 if the accesses do not overlap. This does extra disambiguation
2053 for mixed/pointer accesses but requires strict aliasing. */
2054 if ((TREE_CODE (base1) != TARGET_MEM_REF
2055 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
2056 && (TREE_CODE (base2) != TARGET_MEM_REF
2057 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
2058 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
2059 TREE_TYPE (ptrtype2)) == 1)
2060 {
2061 /* But avoid treating arrays as "objects", instead assume they
2062 can overlap by an exact multiple of their element size.
2063 See gcc.dg/torture/alias-2.c. */
2064 bool partial_overlap = TREE_CODE (TREE_TYPE (ptrtype1)) == ARRAY_TYPE;
2065
2066 if (!partial_overlap
2067 && !ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
2068 return false;
2069 if (!ref1 || !ref2
2070 || (!partial_overlap
2071 && known_eq (size1, max_size1) && known_eq (size2, max_size2)))
2072 return true;
2073 int res = nonoverlapping_refs_since_match_p (base1, ref1, base2, ref2,
2074 partial_overlap);
2075 if (res == -1)
2076 return !nonoverlapping_component_refs_p (ref1, ref2);
2077 return !res;
2078 }
2079
2080 /* Do access-path based disambiguation. */
2081 if (ref1 && ref2
2082 && (handled_component_p (ref1) || handled_component_p (ref2)))
2083 return aliasing_component_refs_p (ref1,
2084 ref1_alias_set, base1_alias_set,
2085 offset1, max_size1,
2086 ref2,
2087 ref2_alias_set, base2_alias_set,
2088 offset2, max_size2);
2089
2090 return true;
2091 }
2092
2093 /* Return true, if the two memory references REF1 and REF2 may alias. */
2094
2095 static bool
2096 refs_may_alias_p_2 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
2097 {
2098 tree base1, base2;
2099 poly_int64 offset1 = 0, offset2 = 0;
2100 poly_int64 max_size1 = -1, max_size2 = -1;
2101 bool var1_p, var2_p, ind1_p, ind2_p;
2102
2103 gcc_checking_assert ((!ref1->ref
2104 || TREE_CODE (ref1->ref) == SSA_NAME
2105 || DECL_P (ref1->ref)
2106 || TREE_CODE (ref1->ref) == STRING_CST
2107 || handled_component_p (ref1->ref)
2108 || TREE_CODE (ref1->ref) == MEM_REF
2109 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
2110 && (!ref2->ref
2111 || TREE_CODE (ref2->ref) == SSA_NAME
2112 || DECL_P (ref2->ref)
2113 || TREE_CODE (ref2->ref) == STRING_CST
2114 || handled_component_p (ref2->ref)
2115 || TREE_CODE (ref2->ref) == MEM_REF
2116 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
2117
2118 /* Decompose the references into their base objects and the access. */
2119 base1 = ao_ref_base (ref1);
2120 offset1 = ref1->offset;
2121 max_size1 = ref1->max_size;
2122 base2 = ao_ref_base (ref2);
2123 offset2 = ref2->offset;
2124 max_size2 = ref2->max_size;
2125
2126 /* We can end up with registers or constants as bases for example from
2127 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
2128 which is seen as a struct copy. */
2129 if (TREE_CODE (base1) == SSA_NAME
2130 || TREE_CODE (base1) == CONST_DECL
2131 || TREE_CODE (base1) == CONSTRUCTOR
2132 || TREE_CODE (base1) == ADDR_EXPR
2133 || CONSTANT_CLASS_P (base1)
2134 || TREE_CODE (base2) == SSA_NAME
2135 || TREE_CODE (base2) == CONST_DECL
2136 || TREE_CODE (base2) == CONSTRUCTOR
2137 || TREE_CODE (base2) == ADDR_EXPR
2138 || CONSTANT_CLASS_P (base2))
2139 return false;
2140
2141 /* We can end up referring to code via function and label decls.
2142 As we likely do not properly track code aliases conservatively
2143 bail out. */
2144 if (TREE_CODE (base1) == FUNCTION_DECL
2145 || TREE_CODE (base1) == LABEL_DECL
2146 || TREE_CODE (base2) == FUNCTION_DECL
2147 || TREE_CODE (base2) == LABEL_DECL)
2148 return true;
2149
2150 /* Two volatile accesses always conflict. */
2151 if (ref1->volatile_p
2152 && ref2->volatile_p)
2153 return true;
2154
2155 /* Defer to simple offset based disambiguation if we have
2156 references based on two decls. Do this before defering to
2157 TBAA to handle must-alias cases in conformance with the
2158 GCC extension of allowing type-punning through unions. */
2159 var1_p = DECL_P (base1);
2160 var2_p = DECL_P (base2);
2161 if (var1_p && var2_p)
2162 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
2163 ref1->size,
2164 ref2->ref, base2, offset2, max_size2,
2165 ref2->size);
2166
2167 /* Handle restrict based accesses.
2168 ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
2169 here. */
2170 tree rbase1 = base1;
2171 tree rbase2 = base2;
2172 if (var1_p)
2173 {
2174 rbase1 = ref1->ref;
2175 if (rbase1)
2176 while (handled_component_p (rbase1))
2177 rbase1 = TREE_OPERAND (rbase1, 0);
2178 }
2179 if (var2_p)
2180 {
2181 rbase2 = ref2->ref;
2182 if (rbase2)
2183 while (handled_component_p (rbase2))
2184 rbase2 = TREE_OPERAND (rbase2, 0);
2185 }
2186 if (rbase1 && rbase2
2187 && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
2188 && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
2189 /* If the accesses are in the same restrict clique... */
2190 && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
2191 /* But based on different pointers they do not alias. */
2192 && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
2193 return false;
2194
2195 ind1_p = (TREE_CODE (base1) == MEM_REF
2196 || TREE_CODE (base1) == TARGET_MEM_REF);
2197 ind2_p = (TREE_CODE (base2) == MEM_REF
2198 || TREE_CODE (base2) == TARGET_MEM_REF);
2199
2200 /* Canonicalize the pointer-vs-decl case. */
2201 if (ind1_p && var2_p)
2202 {
2203 std::swap (offset1, offset2);
2204 std::swap (max_size1, max_size2);
2205 std::swap (base1, base2);
2206 std::swap (ref1, ref2);
2207 var1_p = true;
2208 ind1_p = false;
2209 var2_p = false;
2210 ind2_p = true;
2211 }
2212
2213 /* First defer to TBAA if possible. */
2214 if (tbaa_p
2215 && flag_strict_aliasing
2216 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
2217 ao_ref_alias_set (ref2)))
2218 return false;
2219
2220 /* If the reference is based on a pointer that points to memory
2221 that may not be written to then the other reference cannot possibly
2222 clobber it. */
2223 if ((TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME
2224 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base2, 0)))
2225 || (ind1_p
2226 && TREE_CODE (TREE_OPERAND (base1, 0)) == SSA_NAME
2227 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base1, 0))))
2228 return false;
2229
2230 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
2231 if (var1_p && ind2_p)
2232 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
2233 offset2, max_size2, ref2->size,
2234 ao_ref_alias_set (ref2),
2235 ao_ref_base_alias_set (ref2),
2236 ref1->ref, base1,
2237 offset1, max_size1, ref1->size,
2238 ao_ref_alias_set (ref1),
2239 ao_ref_base_alias_set (ref1),
2240 tbaa_p);
2241 else if (ind1_p && ind2_p)
2242 return indirect_refs_may_alias_p (ref1->ref, base1,
2243 offset1, max_size1, ref1->size,
2244 ao_ref_alias_set (ref1),
2245 ao_ref_base_alias_set (ref1),
2246 ref2->ref, base2,
2247 offset2, max_size2, ref2->size,
2248 ao_ref_alias_set (ref2),
2249 ao_ref_base_alias_set (ref2),
2250 tbaa_p);
2251
2252 gcc_unreachable ();
2253 }
2254
2255 /* Return true, if the two memory references REF1 and REF2 may alias
2256 and update statistics. */
2257
2258 bool
2259 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
2260 {
2261 bool res = refs_may_alias_p_2 (ref1, ref2, tbaa_p);
2262 if (res)
2263 ++alias_stats.refs_may_alias_p_may_alias;
2264 else
2265 ++alias_stats.refs_may_alias_p_no_alias;
2266 return res;
2267 }
2268
2269 static bool
2270 refs_may_alias_p (tree ref1, ao_ref *ref2, bool tbaa_p)
2271 {
2272 ao_ref r1;
2273 ao_ref_init (&r1, ref1);
2274 return refs_may_alias_p_1 (&r1, ref2, tbaa_p);
2275 }
2276
2277 bool
2278 refs_may_alias_p (tree ref1, tree ref2, bool tbaa_p)
2279 {
2280 ao_ref r1, r2;
2281 ao_ref_init (&r1, ref1);
2282 ao_ref_init (&r2, ref2);
2283 return refs_may_alias_p_1 (&r1, &r2, tbaa_p);
2284 }
2285
2286 /* Returns true if there is a anti-dependence for the STORE that
2287 executes after the LOAD. */
2288
2289 bool
2290 refs_anti_dependent_p (tree load, tree store)
2291 {
2292 ao_ref r1, r2;
2293 ao_ref_init (&r1, load);
2294 ao_ref_init (&r2, store);
2295 return refs_may_alias_p_1 (&r1, &r2, false);
2296 }
2297
2298 /* Returns true if there is a output dependence for the stores
2299 STORE1 and STORE2. */
2300
2301 bool
2302 refs_output_dependent_p (tree store1, tree store2)
2303 {
2304 ao_ref r1, r2;
2305 ao_ref_init (&r1, store1);
2306 ao_ref_init (&r2, store2);
2307 return refs_may_alias_p_1 (&r1, &r2, false);
2308 }
2309
2310 /* If the call CALL may use the memory reference REF return true,
2311 otherwise return false. */
2312
2313 static bool
2314 ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref, bool tbaa_p)
2315 {
2316 tree base, callee;
2317 unsigned i;
2318 int flags = gimple_call_flags (call);
2319
2320 /* Const functions without a static chain do not implicitly use memory. */
2321 if (!gimple_call_chain (call)
2322 && (flags & (ECF_CONST|ECF_NOVOPS)))
2323 goto process_args;
2324
2325 base = ao_ref_base (ref);
2326 if (!base)
2327 return true;
2328
2329 /* A call that is not without side-effects might involve volatile
2330 accesses and thus conflicts with all other volatile accesses. */
2331 if (ref->volatile_p)
2332 return true;
2333
2334 /* If the reference is based on a decl that is not aliased the call
2335 cannot possibly use it. */
2336 if (DECL_P (base)
2337 && !may_be_aliased (base)
2338 /* But local statics can be used through recursion. */
2339 && !is_global_var (base))
2340 goto process_args;
2341
2342 callee = gimple_call_fndecl (call);
2343
2344 /* Handle those builtin functions explicitly that do not act as
2345 escape points. See tree-ssa-structalias.c:find_func_aliases
2346 for the list of builtins we might need to handle here. */
2347 if (callee != NULL_TREE
2348 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2349 switch (DECL_FUNCTION_CODE (callee))
2350 {
2351 /* All the following functions read memory pointed to by
2352 their second argument. strcat/strncat additionally
2353 reads memory pointed to by the first argument. */
2354 case BUILT_IN_STRCAT:
2355 case BUILT_IN_STRNCAT:
2356 {
2357 ao_ref dref;
2358 ao_ref_init_from_ptr_and_size (&dref,
2359 gimple_call_arg (call, 0),
2360 NULL_TREE);
2361 if (refs_may_alias_p_1 (&dref, ref, false))
2362 return true;
2363 }
2364 /* FALLTHRU */
2365 case BUILT_IN_STRCPY:
2366 case BUILT_IN_STRNCPY:
2367 case BUILT_IN_MEMCPY:
2368 case BUILT_IN_MEMMOVE:
2369 case BUILT_IN_MEMPCPY:
2370 case BUILT_IN_STPCPY:
2371 case BUILT_IN_STPNCPY:
2372 case BUILT_IN_TM_MEMCPY:
2373 case BUILT_IN_TM_MEMMOVE:
2374 {
2375 ao_ref dref;
2376 tree size = NULL_TREE;
2377 if (gimple_call_num_args (call) == 3)
2378 size = gimple_call_arg (call, 2);
2379 ao_ref_init_from_ptr_and_size (&dref,
2380 gimple_call_arg (call, 1),
2381 size);
2382 return refs_may_alias_p_1 (&dref, ref, false);
2383 }
2384 case BUILT_IN_STRCAT_CHK:
2385 case BUILT_IN_STRNCAT_CHK:
2386 {
2387 ao_ref dref;
2388 ao_ref_init_from_ptr_and_size (&dref,
2389 gimple_call_arg (call, 0),
2390 NULL_TREE);
2391 if (refs_may_alias_p_1 (&dref, ref, false))
2392 return true;
2393 }
2394 /* FALLTHRU */
2395 case BUILT_IN_STRCPY_CHK:
2396 case BUILT_IN_STRNCPY_CHK:
2397 case BUILT_IN_MEMCPY_CHK:
2398 case BUILT_IN_MEMMOVE_CHK:
2399 case BUILT_IN_MEMPCPY_CHK:
2400 case BUILT_IN_STPCPY_CHK:
2401 case BUILT_IN_STPNCPY_CHK:
2402 {
2403 ao_ref dref;
2404 tree size = NULL_TREE;
2405 if (gimple_call_num_args (call) == 4)
2406 size = gimple_call_arg (call, 2);
2407 ao_ref_init_from_ptr_and_size (&dref,
2408 gimple_call_arg (call, 1),
2409 size);
2410 return refs_may_alias_p_1 (&dref, ref, false);
2411 }
2412 case BUILT_IN_BCOPY:
2413 {
2414 ao_ref dref;
2415 tree size = gimple_call_arg (call, 2);
2416 ao_ref_init_from_ptr_and_size (&dref,
2417 gimple_call_arg (call, 0),
2418 size);
2419 return refs_may_alias_p_1 (&dref, ref, false);
2420 }
2421
2422 /* The following functions read memory pointed to by their
2423 first argument. */
2424 CASE_BUILT_IN_TM_LOAD (1):
2425 CASE_BUILT_IN_TM_LOAD (2):
2426 CASE_BUILT_IN_TM_LOAD (4):
2427 CASE_BUILT_IN_TM_LOAD (8):
2428 CASE_BUILT_IN_TM_LOAD (FLOAT):
2429 CASE_BUILT_IN_TM_LOAD (DOUBLE):
2430 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
2431 CASE_BUILT_IN_TM_LOAD (M64):
2432 CASE_BUILT_IN_TM_LOAD (M128):
2433 CASE_BUILT_IN_TM_LOAD (M256):
2434 case BUILT_IN_TM_LOG:
2435 case BUILT_IN_TM_LOG_1:
2436 case BUILT_IN_TM_LOG_2:
2437 case BUILT_IN_TM_LOG_4:
2438 case BUILT_IN_TM_LOG_8:
2439 case BUILT_IN_TM_LOG_FLOAT:
2440 case BUILT_IN_TM_LOG_DOUBLE:
2441 case BUILT_IN_TM_LOG_LDOUBLE:
2442 case BUILT_IN_TM_LOG_M64:
2443 case BUILT_IN_TM_LOG_M128:
2444 case BUILT_IN_TM_LOG_M256:
2445 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
2446
2447 /* These read memory pointed to by the first argument. */
2448 case BUILT_IN_STRDUP:
2449 case BUILT_IN_STRNDUP:
2450 case BUILT_IN_REALLOC:
2451 {
2452 ao_ref dref;
2453 tree size = NULL_TREE;
2454 if (gimple_call_num_args (call) == 2)
2455 size = gimple_call_arg (call, 1);
2456 ao_ref_init_from_ptr_and_size (&dref,
2457 gimple_call_arg (call, 0),
2458 size);
2459 return refs_may_alias_p_1 (&dref, ref, false);
2460 }
2461 /* These read memory pointed to by the first argument. */
2462 case BUILT_IN_INDEX:
2463 case BUILT_IN_STRCHR:
2464 case BUILT_IN_STRRCHR:
2465 {
2466 ao_ref dref;
2467 ao_ref_init_from_ptr_and_size (&dref,
2468 gimple_call_arg (call, 0),
2469 NULL_TREE);
2470 return refs_may_alias_p_1 (&dref, ref, false);
2471 }
2472 /* These read memory pointed to by the first argument with size
2473 in the third argument. */
2474 case BUILT_IN_MEMCHR:
2475 {
2476 ao_ref dref;
2477 ao_ref_init_from_ptr_and_size (&dref,
2478 gimple_call_arg (call, 0),
2479 gimple_call_arg (call, 2));
2480 return refs_may_alias_p_1 (&dref, ref, false);
2481 }
2482 /* These read memory pointed to by the first and second arguments. */
2483 case BUILT_IN_STRSTR:
2484 case BUILT_IN_STRPBRK:
2485 {
2486 ao_ref dref;
2487 ao_ref_init_from_ptr_and_size (&dref,
2488 gimple_call_arg (call, 0),
2489 NULL_TREE);
2490 if (refs_may_alias_p_1 (&dref, ref, false))
2491 return true;
2492 ao_ref_init_from_ptr_and_size (&dref,
2493 gimple_call_arg (call, 1),
2494 NULL_TREE);
2495 return refs_may_alias_p_1 (&dref, ref, false);
2496 }
2497
2498 /* The following builtins do not read from memory. */
2499 case BUILT_IN_FREE:
2500 case BUILT_IN_MALLOC:
2501 case BUILT_IN_POSIX_MEMALIGN:
2502 case BUILT_IN_ALIGNED_ALLOC:
2503 case BUILT_IN_CALLOC:
2504 CASE_BUILT_IN_ALLOCA:
2505 case BUILT_IN_STACK_SAVE:
2506 case BUILT_IN_STACK_RESTORE:
2507 case BUILT_IN_MEMSET:
2508 case BUILT_IN_TM_MEMSET:
2509 case BUILT_IN_MEMSET_CHK:
2510 case BUILT_IN_FREXP:
2511 case BUILT_IN_FREXPF:
2512 case BUILT_IN_FREXPL:
2513 case BUILT_IN_GAMMA_R:
2514 case BUILT_IN_GAMMAF_R:
2515 case BUILT_IN_GAMMAL_R:
2516 case BUILT_IN_LGAMMA_R:
2517 case BUILT_IN_LGAMMAF_R:
2518 case BUILT_IN_LGAMMAL_R:
2519 case BUILT_IN_MODF:
2520 case BUILT_IN_MODFF:
2521 case BUILT_IN_MODFL:
2522 case BUILT_IN_REMQUO:
2523 case BUILT_IN_REMQUOF:
2524 case BUILT_IN_REMQUOL:
2525 case BUILT_IN_SINCOS:
2526 case BUILT_IN_SINCOSF:
2527 case BUILT_IN_SINCOSL:
2528 case BUILT_IN_ASSUME_ALIGNED:
2529 case BUILT_IN_VA_END:
2530 return false;
2531 /* __sync_* builtins and some OpenMP builtins act as threading
2532 barriers. */
2533 #undef DEF_SYNC_BUILTIN
2534 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2535 #include "sync-builtins.def"
2536 #undef DEF_SYNC_BUILTIN
2537 case BUILT_IN_GOMP_ATOMIC_START:
2538 case BUILT_IN_GOMP_ATOMIC_END:
2539 case BUILT_IN_GOMP_BARRIER:
2540 case BUILT_IN_GOMP_BARRIER_CANCEL:
2541 case BUILT_IN_GOMP_TASKWAIT:
2542 case BUILT_IN_GOMP_TASKGROUP_END:
2543 case BUILT_IN_GOMP_CRITICAL_START:
2544 case BUILT_IN_GOMP_CRITICAL_END:
2545 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2546 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2547 case BUILT_IN_GOMP_LOOP_END:
2548 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2549 case BUILT_IN_GOMP_ORDERED_START:
2550 case BUILT_IN_GOMP_ORDERED_END:
2551 case BUILT_IN_GOMP_SECTIONS_END:
2552 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2553 case BUILT_IN_GOMP_SINGLE_COPY_START:
2554 case BUILT_IN_GOMP_SINGLE_COPY_END:
2555 return true;
2556
2557 default:
2558 /* Fallthru to general call handling. */;
2559 }
2560
2561 /* Check if base is a global static variable that is not read
2562 by the function. */
2563 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2564 {
2565 struct cgraph_node *node = cgraph_node::get (callee);
2566 bitmap read;
2567 int id;
2568
2569 /* FIXME: Callee can be an OMP builtin that does not have a call graph
2570 node yet. We should enforce that there are nodes for all decls in the
2571 IL and remove this check instead. */
2572 if (node
2573 && (id = ipa_reference_var_uid (base)) != -1
2574 && (read = ipa_reference_get_read_global (node))
2575 && !bitmap_bit_p (read, id))
2576 goto process_args;
2577 }
2578
2579 /* Check if the base variable is call-used. */
2580 if (DECL_P (base))
2581 {
2582 if (pt_solution_includes (gimple_call_use_set (call), base))
2583 return true;
2584 }
2585 else if ((TREE_CODE (base) == MEM_REF
2586 || TREE_CODE (base) == TARGET_MEM_REF)
2587 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2588 {
2589 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2590 if (!pi)
2591 return true;
2592
2593 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
2594 return true;
2595 }
2596 else
2597 return true;
2598
2599 /* Inspect call arguments for passed-by-value aliases. */
2600 process_args:
2601 for (i = 0; i < gimple_call_num_args (call); ++i)
2602 {
2603 tree op = gimple_call_arg (call, i);
2604 int flags = gimple_call_arg_flags (call, i);
2605
2606 if (flags & EAF_UNUSED)
2607 continue;
2608
2609 if (TREE_CODE (op) == WITH_SIZE_EXPR)
2610 op = TREE_OPERAND (op, 0);
2611
2612 if (TREE_CODE (op) != SSA_NAME
2613 && !is_gimple_min_invariant (op))
2614 {
2615 ao_ref r;
2616 ao_ref_init (&r, op);
2617 if (refs_may_alias_p_1 (&r, ref, tbaa_p))
2618 return true;
2619 }
2620 }
2621
2622 return false;
2623 }
2624
2625 static bool
2626 ref_maybe_used_by_call_p (gcall *call, ao_ref *ref, bool tbaa_p)
2627 {
2628 bool res;
2629 res = ref_maybe_used_by_call_p_1 (call, ref, tbaa_p);
2630 if (res)
2631 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
2632 else
2633 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
2634 return res;
2635 }
2636
2637
2638 /* If the statement STMT may use the memory reference REF return
2639 true, otherwise return false. */
2640
2641 bool
2642 ref_maybe_used_by_stmt_p (gimple *stmt, ao_ref *ref, bool tbaa_p)
2643 {
2644 if (is_gimple_assign (stmt))
2645 {
2646 tree rhs;
2647
2648 /* All memory assign statements are single. */
2649 if (!gimple_assign_single_p (stmt))
2650 return false;
2651
2652 rhs = gimple_assign_rhs1 (stmt);
2653 if (is_gimple_reg (rhs)
2654 || is_gimple_min_invariant (rhs)
2655 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
2656 return false;
2657
2658 return refs_may_alias_p (rhs, ref, tbaa_p);
2659 }
2660 else if (is_gimple_call (stmt))
2661 return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref, tbaa_p);
2662 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
2663 {
2664 tree retval = gimple_return_retval (return_stmt);
2665 if (retval
2666 && TREE_CODE (retval) != SSA_NAME
2667 && !is_gimple_min_invariant (retval)
2668 && refs_may_alias_p (retval, ref, tbaa_p))
2669 return true;
2670 /* If ref escapes the function then the return acts as a use. */
2671 tree base = ao_ref_base (ref);
2672 if (!base)
2673 ;
2674 else if (DECL_P (base))
2675 return is_global_var (base);
2676 else if (TREE_CODE (base) == MEM_REF
2677 || TREE_CODE (base) == TARGET_MEM_REF)
2678 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
2679 return false;
2680 }
2681
2682 return true;
2683 }
2684
2685 bool
2686 ref_maybe_used_by_stmt_p (gimple *stmt, tree ref, bool tbaa_p)
2687 {
2688 ao_ref r;
2689 ao_ref_init (&r, ref);
2690 return ref_maybe_used_by_stmt_p (stmt, &r, tbaa_p);
2691 }
2692
2693 /* If the call in statement CALL may clobber the memory reference REF
2694 return true, otherwise return false. */
2695
2696 bool
2697 call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref)
2698 {
2699 tree base;
2700 tree callee;
2701
2702 /* If the call is pure or const it cannot clobber anything. */
2703 if (gimple_call_flags (call)
2704 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
2705 return false;
2706 if (gimple_call_internal_p (call))
2707 switch (gimple_call_internal_fn (call))
2708 {
2709 /* Treat these internal calls like ECF_PURE for aliasing,
2710 they don't write to any memory the program should care about.
2711 They have important other side-effects, and read memory,
2712 so can't be ECF_NOVOPS. */
2713 case IFN_UBSAN_NULL:
2714 case IFN_UBSAN_BOUNDS:
2715 case IFN_UBSAN_VPTR:
2716 case IFN_UBSAN_OBJECT_SIZE:
2717 case IFN_UBSAN_PTR:
2718 case IFN_ASAN_CHECK:
2719 return false;
2720 default:
2721 break;
2722 }
2723
2724 base = ao_ref_base (ref);
2725 if (!base)
2726 return true;
2727
2728 if (TREE_CODE (base) == SSA_NAME
2729 || CONSTANT_CLASS_P (base))
2730 return false;
2731
2732 /* A call that is not without side-effects might involve volatile
2733 accesses and thus conflicts with all other volatile accesses. */
2734 if (ref->volatile_p)
2735 return true;
2736
2737 /* If the reference is based on a decl that is not aliased the call
2738 cannot possibly clobber it. */
2739 if (DECL_P (base)
2740 && !may_be_aliased (base)
2741 /* But local non-readonly statics can be modified through recursion
2742 or the call may implement a threading barrier which we must
2743 treat as may-def. */
2744 && (TREE_READONLY (base)
2745 || !is_global_var (base)))
2746 return false;
2747
2748 /* If the reference is based on a pointer that points to memory
2749 that may not be written to then the call cannot possibly clobber it. */
2750 if ((TREE_CODE (base) == MEM_REF
2751 || TREE_CODE (base) == TARGET_MEM_REF)
2752 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2753 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base, 0)))
2754 return false;
2755
2756 callee = gimple_call_fndecl (call);
2757
2758 /* Handle those builtin functions explicitly that do not act as
2759 escape points. See tree-ssa-structalias.c:find_func_aliases
2760 for the list of builtins we might need to handle here. */
2761 if (callee != NULL_TREE
2762 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2763 switch (DECL_FUNCTION_CODE (callee))
2764 {
2765 /* All the following functions clobber memory pointed to by
2766 their first argument. */
2767 case BUILT_IN_STRCPY:
2768 case BUILT_IN_STRNCPY:
2769 case BUILT_IN_MEMCPY:
2770 case BUILT_IN_MEMMOVE:
2771 case BUILT_IN_MEMPCPY:
2772 case BUILT_IN_STPCPY:
2773 case BUILT_IN_STPNCPY:
2774 case BUILT_IN_STRCAT:
2775 case BUILT_IN_STRNCAT:
2776 case BUILT_IN_MEMSET:
2777 case BUILT_IN_TM_MEMSET:
2778 CASE_BUILT_IN_TM_STORE (1):
2779 CASE_BUILT_IN_TM_STORE (2):
2780 CASE_BUILT_IN_TM_STORE (4):
2781 CASE_BUILT_IN_TM_STORE (8):
2782 CASE_BUILT_IN_TM_STORE (FLOAT):
2783 CASE_BUILT_IN_TM_STORE (DOUBLE):
2784 CASE_BUILT_IN_TM_STORE (LDOUBLE):
2785 CASE_BUILT_IN_TM_STORE (M64):
2786 CASE_BUILT_IN_TM_STORE (M128):
2787 CASE_BUILT_IN_TM_STORE (M256):
2788 case BUILT_IN_TM_MEMCPY:
2789 case BUILT_IN_TM_MEMMOVE:
2790 {
2791 ao_ref dref;
2792 tree size = NULL_TREE;
2793 /* Don't pass in size for strncat, as the maximum size
2794 is strlen (dest) + n + 1 instead of n, resp.
2795 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2796 known. */
2797 if (gimple_call_num_args (call) == 3
2798 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
2799 size = gimple_call_arg (call, 2);
2800 ao_ref_init_from_ptr_and_size (&dref,
2801 gimple_call_arg (call, 0),
2802 size);
2803 return refs_may_alias_p_1 (&dref, ref, false);
2804 }
2805 case BUILT_IN_STRCPY_CHK:
2806 case BUILT_IN_STRNCPY_CHK:
2807 case BUILT_IN_MEMCPY_CHK:
2808 case BUILT_IN_MEMMOVE_CHK:
2809 case BUILT_IN_MEMPCPY_CHK:
2810 case BUILT_IN_STPCPY_CHK:
2811 case BUILT_IN_STPNCPY_CHK:
2812 case BUILT_IN_STRCAT_CHK:
2813 case BUILT_IN_STRNCAT_CHK:
2814 case BUILT_IN_MEMSET_CHK:
2815 {
2816 ao_ref dref;
2817 tree size = NULL_TREE;
2818 /* Don't pass in size for __strncat_chk, as the maximum size
2819 is strlen (dest) + n + 1 instead of n, resp.
2820 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2821 known. */
2822 if (gimple_call_num_args (call) == 4
2823 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
2824 size = gimple_call_arg (call, 2);
2825 ao_ref_init_from_ptr_and_size (&dref,
2826 gimple_call_arg (call, 0),
2827 size);
2828 return refs_may_alias_p_1 (&dref, ref, false);
2829 }
2830 case BUILT_IN_BCOPY:
2831 {
2832 ao_ref dref;
2833 tree size = gimple_call_arg (call, 2);
2834 ao_ref_init_from_ptr_and_size (&dref,
2835 gimple_call_arg (call, 1),
2836 size);
2837 return refs_may_alias_p_1 (&dref, ref, false);
2838 }
2839 /* Allocating memory does not have any side-effects apart from
2840 being the definition point for the pointer. */
2841 case BUILT_IN_MALLOC:
2842 case BUILT_IN_ALIGNED_ALLOC:
2843 case BUILT_IN_CALLOC:
2844 case BUILT_IN_STRDUP:
2845 case BUILT_IN_STRNDUP:
2846 /* Unix98 specifies that errno is set on allocation failure. */
2847 if (flag_errno_math
2848 && targetm.ref_may_alias_errno (ref))
2849 return true;
2850 return false;
2851 case BUILT_IN_STACK_SAVE:
2852 CASE_BUILT_IN_ALLOCA:
2853 case BUILT_IN_ASSUME_ALIGNED:
2854 return false;
2855 /* But posix_memalign stores a pointer into the memory pointed to
2856 by its first argument. */
2857 case BUILT_IN_POSIX_MEMALIGN:
2858 {
2859 tree ptrptr = gimple_call_arg (call, 0);
2860 ao_ref dref;
2861 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2862 TYPE_SIZE_UNIT (ptr_type_node));
2863 return (refs_may_alias_p_1 (&dref, ref, false)
2864 || (flag_errno_math
2865 && targetm.ref_may_alias_errno (ref)));
2866 }
2867 /* Freeing memory kills the pointed-to memory. More importantly
2868 the call has to serve as a barrier for moving loads and stores
2869 across it. */
2870 case BUILT_IN_FREE:
2871 case BUILT_IN_VA_END:
2872 {
2873 tree ptr = gimple_call_arg (call, 0);
2874 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2875 }
2876 /* Realloc serves both as allocation point and deallocation point. */
2877 case BUILT_IN_REALLOC:
2878 {
2879 tree ptr = gimple_call_arg (call, 0);
2880 /* Unix98 specifies that errno is set on allocation failure. */
2881 return ((flag_errno_math
2882 && targetm.ref_may_alias_errno (ref))
2883 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2884 }
2885 case BUILT_IN_GAMMA_R:
2886 case BUILT_IN_GAMMAF_R:
2887 case BUILT_IN_GAMMAL_R:
2888 case BUILT_IN_LGAMMA_R:
2889 case BUILT_IN_LGAMMAF_R:
2890 case BUILT_IN_LGAMMAL_R:
2891 {
2892 tree out = gimple_call_arg (call, 1);
2893 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2894 return true;
2895 if (flag_errno_math)
2896 break;
2897 return false;
2898 }
2899 case BUILT_IN_FREXP:
2900 case BUILT_IN_FREXPF:
2901 case BUILT_IN_FREXPL:
2902 case BUILT_IN_MODF:
2903 case BUILT_IN_MODFF:
2904 case BUILT_IN_MODFL:
2905 {
2906 tree out = gimple_call_arg (call, 1);
2907 return ptr_deref_may_alias_ref_p_1 (out, ref);
2908 }
2909 case BUILT_IN_REMQUO:
2910 case BUILT_IN_REMQUOF:
2911 case BUILT_IN_REMQUOL:
2912 {
2913 tree out = gimple_call_arg (call, 2);
2914 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2915 return true;
2916 if (flag_errno_math)
2917 break;
2918 return false;
2919 }
2920 case BUILT_IN_SINCOS:
2921 case BUILT_IN_SINCOSF:
2922 case BUILT_IN_SINCOSL:
2923 {
2924 tree sin = gimple_call_arg (call, 1);
2925 tree cos = gimple_call_arg (call, 2);
2926 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2927 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2928 }
2929 /* __sync_* builtins and some OpenMP builtins act as threading
2930 barriers. */
2931 #undef DEF_SYNC_BUILTIN
2932 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2933 #include "sync-builtins.def"
2934 #undef DEF_SYNC_BUILTIN
2935 case BUILT_IN_GOMP_ATOMIC_START:
2936 case BUILT_IN_GOMP_ATOMIC_END:
2937 case BUILT_IN_GOMP_BARRIER:
2938 case BUILT_IN_GOMP_BARRIER_CANCEL:
2939 case BUILT_IN_GOMP_TASKWAIT:
2940 case BUILT_IN_GOMP_TASKGROUP_END:
2941 case BUILT_IN_GOMP_CRITICAL_START:
2942 case BUILT_IN_GOMP_CRITICAL_END:
2943 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2944 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2945 case BUILT_IN_GOMP_LOOP_END:
2946 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2947 case BUILT_IN_GOMP_ORDERED_START:
2948 case BUILT_IN_GOMP_ORDERED_END:
2949 case BUILT_IN_GOMP_SECTIONS_END:
2950 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2951 case BUILT_IN_GOMP_SINGLE_COPY_START:
2952 case BUILT_IN_GOMP_SINGLE_COPY_END:
2953 return true;
2954 default:
2955 /* Fallthru to general call handling. */;
2956 }
2957
2958 /* Check if base is a global static variable that is not written
2959 by the function. */
2960 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2961 {
2962 struct cgraph_node *node = cgraph_node::get (callee);
2963 bitmap written;
2964 int id;
2965
2966 if (node
2967 && (id = ipa_reference_var_uid (base)) != -1
2968 && (written = ipa_reference_get_written_global (node))
2969 && !bitmap_bit_p (written, id))
2970 return false;
2971 }
2972
2973 /* Check if the base variable is call-clobbered. */
2974 if (DECL_P (base))
2975 return pt_solution_includes (gimple_call_clobber_set (call), base);
2976 else if ((TREE_CODE (base) == MEM_REF
2977 || TREE_CODE (base) == TARGET_MEM_REF)
2978 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2979 {
2980 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2981 if (!pi)
2982 return true;
2983
2984 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2985 }
2986
2987 return true;
2988 }
2989
2990 /* If the call in statement CALL may clobber the memory reference REF
2991 return true, otherwise return false. */
2992
2993 bool
2994 call_may_clobber_ref_p (gcall *call, tree ref)
2995 {
2996 bool res;
2997 ao_ref r;
2998 ao_ref_init (&r, ref);
2999 res = call_may_clobber_ref_p_1 (call, &r);
3000 if (res)
3001 ++alias_stats.call_may_clobber_ref_p_may_alias;
3002 else
3003 ++alias_stats.call_may_clobber_ref_p_no_alias;
3004 return res;
3005 }
3006
3007
3008 /* If the statement STMT may clobber the memory reference REF return true,
3009 otherwise return false. */
3010
3011 bool
3012 stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref, bool tbaa_p)
3013 {
3014 if (is_gimple_call (stmt))
3015 {
3016 tree lhs = gimple_call_lhs (stmt);
3017 if (lhs
3018 && TREE_CODE (lhs) != SSA_NAME)
3019 {
3020 ao_ref r;
3021 ao_ref_init (&r, lhs);
3022 if (refs_may_alias_p_1 (ref, &r, tbaa_p))
3023 return true;
3024 }
3025
3026 return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref);
3027 }
3028 else if (gimple_assign_single_p (stmt))
3029 {
3030 tree lhs = gimple_assign_lhs (stmt);
3031 if (TREE_CODE (lhs) != SSA_NAME)
3032 {
3033 ao_ref r;
3034 ao_ref_init (&r, lhs);
3035 return refs_may_alias_p_1 (ref, &r, tbaa_p);
3036 }
3037 }
3038 else if (gimple_code (stmt) == GIMPLE_ASM)
3039 return true;
3040
3041 return false;
3042 }
3043
3044 bool
3045 stmt_may_clobber_ref_p (gimple *stmt, tree ref, bool tbaa_p)
3046 {
3047 ao_ref r;
3048 ao_ref_init (&r, ref);
3049 return stmt_may_clobber_ref_p_1 (stmt, &r, tbaa_p);
3050 }
3051
3052 /* Return true if store1 and store2 described by corresponding tuples
3053 <BASE, OFFSET, SIZE, MAX_SIZE> have the same size and store to the same
3054 address. */
3055
3056 static bool
3057 same_addr_size_stores_p (tree base1, poly_int64 offset1, poly_int64 size1,
3058 poly_int64 max_size1,
3059 tree base2, poly_int64 offset2, poly_int64 size2,
3060 poly_int64 max_size2)
3061 {
3062 /* Offsets need to be 0. */
3063 if (maybe_ne (offset1, 0)
3064 || maybe_ne (offset2, 0))
3065 return false;
3066
3067 bool base1_obj_p = SSA_VAR_P (base1);
3068 bool base2_obj_p = SSA_VAR_P (base2);
3069
3070 /* We need one object. */
3071 if (base1_obj_p == base2_obj_p)
3072 return false;
3073 tree obj = base1_obj_p ? base1 : base2;
3074
3075 /* And we need one MEM_REF. */
3076 bool base1_memref_p = TREE_CODE (base1) == MEM_REF;
3077 bool base2_memref_p = TREE_CODE (base2) == MEM_REF;
3078 if (base1_memref_p == base2_memref_p)
3079 return false;
3080 tree memref = base1_memref_p ? base1 : base2;
3081
3082 /* Sizes need to be valid. */
3083 if (!known_size_p (max_size1)
3084 || !known_size_p (max_size2)
3085 || !known_size_p (size1)
3086 || !known_size_p (size2))
3087 return false;
3088
3089 /* Max_size needs to match size. */
3090 if (maybe_ne (max_size1, size1)
3091 || maybe_ne (max_size2, size2))
3092 return false;
3093
3094 /* Sizes need to match. */
3095 if (maybe_ne (size1, size2))
3096 return false;
3097
3098
3099 /* Check that memref is a store to pointer with singleton points-to info. */
3100 if (!integer_zerop (TREE_OPERAND (memref, 1)))
3101 return false;
3102 tree ptr = TREE_OPERAND (memref, 0);
3103 if (TREE_CODE (ptr) != SSA_NAME)
3104 return false;
3105 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
3106 unsigned int pt_uid;
3107 if (pi == NULL
3108 || !pt_solution_singleton_or_null_p (&pi->pt, &pt_uid))
3109 return false;
3110
3111 /* Be conservative with non-call exceptions when the address might
3112 be NULL. */
3113 if (cfun->can_throw_non_call_exceptions && pi->pt.null)
3114 return false;
3115
3116 /* Check that ptr points relative to obj. */
3117 unsigned int obj_uid = DECL_PT_UID (obj);
3118 if (obj_uid != pt_uid)
3119 return false;
3120
3121 /* Check that the object size is the same as the store size. That ensures us
3122 that ptr points to the start of obj. */
3123 return (DECL_SIZE (obj)
3124 && poly_int_tree_p (DECL_SIZE (obj))
3125 && known_eq (wi::to_poly_offset (DECL_SIZE (obj)), size1));
3126 }
3127
3128 /* If STMT kills the memory reference REF return true, otherwise
3129 return false. */
3130
3131 bool
3132 stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
3133 {
3134 if (!ao_ref_base (ref))
3135 return false;
3136
3137 if (gimple_has_lhs (stmt)
3138 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
3139 /* The assignment is not necessarily carried out if it can throw
3140 and we can catch it in the current function where we could inspect
3141 the previous value.
3142 ??? We only need to care about the RHS throwing. For aggregate
3143 assignments or similar calls and non-call exceptions the LHS
3144 might throw as well. */
3145 && !stmt_can_throw_internal (cfun, stmt))
3146 {
3147 tree lhs = gimple_get_lhs (stmt);
3148 /* If LHS is literally a base of the access we are done. */
3149 if (ref->ref)
3150 {
3151 tree base = ref->ref;
3152 tree innermost_dropped_array_ref = NULL_TREE;
3153 if (handled_component_p (base))
3154 {
3155 tree saved_lhs0 = NULL_TREE;
3156 if (handled_component_p (lhs))
3157 {
3158 saved_lhs0 = TREE_OPERAND (lhs, 0);
3159 TREE_OPERAND (lhs, 0) = integer_zero_node;
3160 }
3161 do
3162 {
3163 /* Just compare the outermost handled component, if
3164 they are equal we have found a possible common
3165 base. */
3166 tree saved_base0 = TREE_OPERAND (base, 0);
3167 TREE_OPERAND (base, 0) = integer_zero_node;
3168 bool res = operand_equal_p (lhs, base, 0);
3169 TREE_OPERAND (base, 0) = saved_base0;
3170 if (res)
3171 break;
3172 /* Remember if we drop an array-ref that we need to
3173 double-check not being at struct end. */
3174 if (TREE_CODE (base) == ARRAY_REF
3175 || TREE_CODE (base) == ARRAY_RANGE_REF)
3176 innermost_dropped_array_ref = base;
3177 /* Otherwise drop handled components of the access. */
3178 base = saved_base0;
3179 }
3180 while (handled_component_p (base));
3181 if (saved_lhs0)
3182 TREE_OPERAND (lhs, 0) = saved_lhs0;
3183 }
3184 /* Finally check if the lhs has the same address and size as the
3185 base candidate of the access. Watch out if we have dropped
3186 an array-ref that was at struct end, this means ref->ref may
3187 be outside of the TYPE_SIZE of its base. */
3188 if ((! innermost_dropped_array_ref
3189 || ! array_at_struct_end_p (innermost_dropped_array_ref))
3190 && (lhs == base
3191 || (((TYPE_SIZE (TREE_TYPE (lhs))
3192 == TYPE_SIZE (TREE_TYPE (base)))
3193 || (TYPE_SIZE (TREE_TYPE (lhs))
3194 && TYPE_SIZE (TREE_TYPE (base))
3195 && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
3196 TYPE_SIZE (TREE_TYPE (base)),
3197 0)))
3198 && operand_equal_p (lhs, base,
3199 OEP_ADDRESS_OF
3200 | OEP_MATCH_SIDE_EFFECTS))))
3201 return true;
3202 }
3203
3204 /* Now look for non-literal equal bases with the restriction of
3205 handling constant offset and size. */
3206 /* For a must-alias check we need to be able to constrain
3207 the access properly. */
3208 if (!ref->max_size_known_p ())
3209 return false;
3210 poly_int64 size, offset, max_size, ref_offset = ref->offset;
3211 bool reverse;
3212 tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size,
3213 &reverse);
3214 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
3215 so base == ref->base does not always hold. */
3216 if (base != ref->base)
3217 {
3218 /* Try using points-to info. */
3219 if (same_addr_size_stores_p (base, offset, size, max_size, ref->base,
3220 ref->offset, ref->size, ref->max_size))
3221 return true;
3222
3223 /* If both base and ref->base are MEM_REFs, only compare the
3224 first operand, and if the second operand isn't equal constant,
3225 try to add the offsets into offset and ref_offset. */
3226 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
3227 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
3228 {
3229 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
3230 TREE_OPERAND (ref->base, 1)))
3231 {
3232 poly_offset_int off1 = mem_ref_offset (base);
3233 off1 <<= LOG2_BITS_PER_UNIT;
3234 off1 += offset;
3235 poly_offset_int off2 = mem_ref_offset (ref->base);
3236 off2 <<= LOG2_BITS_PER_UNIT;
3237 off2 += ref_offset;
3238 if (!off1.to_shwi (&offset) || !off2.to_shwi (&ref_offset))
3239 size = -1;
3240 }
3241 }
3242 else
3243 size = -1;
3244 }
3245 /* For a must-alias check we need to be able to constrain
3246 the access properly. */
3247 if (known_eq (size, max_size)
3248 && known_subrange_p (ref_offset, ref->max_size, offset, size))
3249 return true;
3250 }
3251
3252 if (is_gimple_call (stmt))
3253 {
3254 tree callee = gimple_call_fndecl (stmt);
3255 if (callee != NULL_TREE
3256 && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3257 switch (DECL_FUNCTION_CODE (callee))
3258 {
3259 case BUILT_IN_FREE:
3260 {
3261 tree ptr = gimple_call_arg (stmt, 0);
3262 tree base = ao_ref_base (ref);
3263 if (base && TREE_CODE (base) == MEM_REF
3264 && TREE_OPERAND (base, 0) == ptr)
3265 return true;
3266 break;
3267 }
3268
3269 case BUILT_IN_MEMCPY:
3270 case BUILT_IN_MEMPCPY:
3271 case BUILT_IN_MEMMOVE:
3272 case BUILT_IN_MEMSET:
3273 case BUILT_IN_MEMCPY_CHK:
3274 case BUILT_IN_MEMPCPY_CHK:
3275 case BUILT_IN_MEMMOVE_CHK:
3276 case BUILT_IN_MEMSET_CHK:
3277 case BUILT_IN_STRNCPY:
3278 case BUILT_IN_STPNCPY:
3279 case BUILT_IN_CALLOC:
3280 {
3281 /* For a must-alias check we need to be able to constrain
3282 the access properly. */
3283 if (!ref->max_size_known_p ())
3284 return false;
3285 tree dest;
3286 tree len;
3287
3288 /* In execution order a calloc call will never kill
3289 anything. However, DSE will (ab)use this interface
3290 to ask if a calloc call writes the same memory locations
3291 as a later assignment, memset, etc. So handle calloc
3292 in the expected way. */
3293 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_CALLOC)
3294 {
3295 tree arg0 = gimple_call_arg (stmt, 0);
3296 tree arg1 = gimple_call_arg (stmt, 1);
3297 if (TREE_CODE (arg0) != INTEGER_CST
3298 || TREE_CODE (arg1) != INTEGER_CST)
3299 return false;
3300
3301 dest = gimple_call_lhs (stmt);
3302 if (!dest)
3303 return false;
3304 len = fold_build2 (MULT_EXPR, TREE_TYPE (arg0), arg0, arg1);
3305 }
3306 else
3307 {
3308 dest = gimple_call_arg (stmt, 0);
3309 len = gimple_call_arg (stmt, 2);
3310 }
3311 if (!poly_int_tree_p (len))
3312 return false;
3313 tree rbase = ref->base;
3314 poly_offset_int roffset = ref->offset;
3315 ao_ref dref;
3316 ao_ref_init_from_ptr_and_size (&dref, dest, len);
3317 tree base = ao_ref_base (&dref);
3318 poly_offset_int offset = dref.offset;
3319 if (!base || !known_size_p (dref.size))
3320 return false;
3321 if (TREE_CODE (base) == MEM_REF)
3322 {
3323 if (TREE_CODE (rbase) != MEM_REF)
3324 return false;
3325 // Compare pointers.
3326 offset += mem_ref_offset (base) << LOG2_BITS_PER_UNIT;
3327 roffset += mem_ref_offset (rbase) << LOG2_BITS_PER_UNIT;
3328 base = TREE_OPERAND (base, 0);
3329 rbase = TREE_OPERAND (rbase, 0);
3330 }
3331 if (base == rbase
3332 && known_subrange_p (roffset, ref->max_size, offset,
3333 wi::to_poly_offset (len)
3334 << LOG2_BITS_PER_UNIT))
3335 return true;
3336 break;
3337 }
3338
3339 case BUILT_IN_VA_END:
3340 {
3341 tree ptr = gimple_call_arg (stmt, 0);
3342 if (TREE_CODE (ptr) == ADDR_EXPR)
3343 {
3344 tree base = ao_ref_base (ref);
3345 if (TREE_OPERAND (ptr, 0) == base)
3346 return true;
3347 }
3348 break;
3349 }
3350
3351 default:;
3352 }
3353 }
3354 return false;
3355 }
3356
3357 bool
3358 stmt_kills_ref_p (gimple *stmt, tree ref)
3359 {
3360 ao_ref r;
3361 ao_ref_init (&r, ref);
3362 return stmt_kills_ref_p (stmt, &r);
3363 }
3364
3365
3366 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
3367 TARGET or a statement clobbering the memory reference REF in which
3368 case false is returned. The walk starts with VUSE, one argument of PHI. */
3369
3370 static bool
3371 maybe_skip_until (gimple *phi, tree &target, basic_block target_bb,
3372 ao_ref *ref, tree vuse, bool tbaa_p, unsigned int &limit,
3373 bitmap *visited, bool abort_on_visited,
3374 void *(*translate)(ao_ref *, tree, void *, translate_flags *),
3375 translate_flags disambiguate_only,
3376 void *data)
3377 {
3378 basic_block bb = gimple_bb (phi);
3379
3380 if (!*visited)
3381 *visited = BITMAP_ALLOC (NULL);
3382
3383 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
3384
3385 /* Walk until we hit the target. */
3386 while (vuse != target)
3387 {
3388 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
3389 /* If we are searching for the target VUSE by walking up to
3390 TARGET_BB dominating the original PHI we are finished once
3391 we reach a default def or a definition in a block dominating
3392 that block. Update TARGET and return. */
3393 if (!target
3394 && (gimple_nop_p (def_stmt)
3395 || dominated_by_p (CDI_DOMINATORS,
3396 target_bb, gimple_bb (def_stmt))))
3397 {
3398 target = vuse;
3399 return true;
3400 }
3401
3402 /* Recurse for PHI nodes. */
3403 if (gimple_code (def_stmt) == GIMPLE_PHI)
3404 {
3405 /* An already visited PHI node ends the walk successfully. */
3406 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
3407 return !abort_on_visited;
3408 vuse = get_continuation_for_phi (def_stmt, ref, tbaa_p, limit,
3409 visited, abort_on_visited,
3410 translate, data, disambiguate_only);
3411 if (!vuse)
3412 return false;
3413 continue;
3414 }
3415 else if (gimple_nop_p (def_stmt))
3416 return false;
3417 else
3418 {
3419 /* A clobbering statement or the end of the IL ends it failing. */
3420 if ((int)limit <= 0)
3421 return false;
3422 --limit;
3423 if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
3424 {
3425 translate_flags tf = disambiguate_only;
3426 if (translate
3427 && (*translate) (ref, vuse, data, &tf) == NULL)
3428 ;
3429 else
3430 return false;
3431 }
3432 }
3433 /* If we reach a new basic-block see if we already skipped it
3434 in a previous walk that ended successfully. */
3435 if (gimple_bb (def_stmt) != bb)
3436 {
3437 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
3438 return !abort_on_visited;
3439 bb = gimple_bb (def_stmt);
3440 }
3441 vuse = gimple_vuse (def_stmt);
3442 }
3443 return true;
3444 }
3445
3446
3447 /* Starting from a PHI node for the virtual operand of the memory reference
3448 REF find a continuation virtual operand that allows to continue walking
3449 statements dominating PHI skipping only statements that cannot possibly
3450 clobber REF. Decrements LIMIT for each alias disambiguation done
3451 and aborts the walk, returning NULL_TREE if it reaches zero.
3452 Returns NULL_TREE if no suitable virtual operand can be found. */
3453
3454 tree
3455 get_continuation_for_phi (gimple *phi, ao_ref *ref, bool tbaa_p,
3456 unsigned int &limit, bitmap *visited,
3457 bool abort_on_visited,
3458 void *(*translate)(ao_ref *, tree, void *,
3459 translate_flags *),
3460 void *data,
3461 translate_flags disambiguate_only)
3462 {
3463 unsigned nargs = gimple_phi_num_args (phi);
3464
3465 /* Through a single-argument PHI we can simply look through. */
3466 if (nargs == 1)
3467 return PHI_ARG_DEF (phi, 0);
3468
3469 /* For two or more arguments try to pairwise skip non-aliasing code
3470 until we hit the phi argument definition that dominates the other one. */
3471 basic_block phi_bb = gimple_bb (phi);
3472 tree arg0, arg1;
3473 unsigned i;
3474
3475 /* Find a candidate for the virtual operand which definition
3476 dominates those of all others. */
3477 /* First look if any of the args themselves satisfy this. */
3478 for (i = 0; i < nargs; ++i)
3479 {
3480 arg0 = PHI_ARG_DEF (phi, i);
3481 if (SSA_NAME_IS_DEFAULT_DEF (arg0))
3482 break;
3483 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (arg0));
3484 if (def_bb != phi_bb
3485 && dominated_by_p (CDI_DOMINATORS, phi_bb, def_bb))
3486 break;
3487 arg0 = NULL_TREE;
3488 }
3489 /* If not, look if we can reach such candidate by walking defs
3490 until we hit the immediate dominator. maybe_skip_until will
3491 do that for us. */
3492 basic_block dom = get_immediate_dominator (CDI_DOMINATORS, phi_bb);
3493
3494 /* Then check against the (to be) found candidate. */
3495 for (i = 0; i < nargs; ++i)
3496 {
3497 arg1 = PHI_ARG_DEF (phi, i);
3498 if (arg1 == arg0)
3499 ;
3500 else if (! maybe_skip_until (phi, arg0, dom, ref, arg1, tbaa_p,
3501 limit, visited,
3502 abort_on_visited,
3503 translate,
3504 /* Do not valueize when walking over
3505 backedges. */
3506 dominated_by_p
3507 (CDI_DOMINATORS,
3508 gimple_bb (SSA_NAME_DEF_STMT (arg1)),
3509 phi_bb)
3510 ? TR_DISAMBIGUATE
3511 : disambiguate_only, data))
3512 return NULL_TREE;
3513 }
3514
3515 return arg0;
3516 }
3517
3518 /* Based on the memory reference REF and its virtual use VUSE call
3519 WALKER for each virtual use that is equivalent to VUSE, including VUSE
3520 itself. That is, for each virtual use for which its defining statement
3521 does not clobber REF.
3522
3523 WALKER is called with REF, the current virtual use and DATA. If
3524 WALKER returns non-NULL the walk stops and its result is returned.
3525 At the end of a non-successful walk NULL is returned.
3526
3527 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
3528 use which definition is a statement that may clobber REF and DATA.
3529 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
3530 If TRANSLATE returns non-NULL the walk stops and its result is returned.
3531 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
3532 to adjust REF and *DATA to make that valid.
3533
3534 VALUEIZE if non-NULL is called with the next VUSE that is considered
3535 and return value is substituted for that. This can be used to
3536 implement optimistic value-numbering for example. Note that the
3537 VUSE argument is assumed to be valueized already.
3538
3539 LIMIT specifies the number of alias queries we are allowed to do,
3540 the walk stops when it reaches zero and NULL is returned. LIMIT
3541 is decremented by the number of alias queries (plus adjustments
3542 done by the callbacks) upon return.
3543
3544 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
3545
3546 void *
3547 walk_non_aliased_vuses (ao_ref *ref, tree vuse, bool tbaa_p,
3548 void *(*walker)(ao_ref *, tree, void *),
3549 void *(*translate)(ao_ref *, tree, void *,
3550 translate_flags *),
3551 tree (*valueize)(tree),
3552 unsigned &limit, void *data)
3553 {
3554 bitmap visited = NULL;
3555 void *res;
3556 bool translated = false;
3557
3558 timevar_push (TV_ALIAS_STMT_WALK);
3559
3560 do
3561 {
3562 gimple *def_stmt;
3563
3564 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3565 res = (*walker) (ref, vuse, data);
3566 /* Abort walk. */
3567 if (res == (void *)-1)
3568 {
3569 res = NULL;
3570 break;
3571 }
3572 /* Lookup succeeded. */
3573 else if (res != NULL)
3574 break;
3575
3576 if (valueize)
3577 {
3578 vuse = valueize (vuse);
3579 if (!vuse)
3580 {
3581 res = NULL;
3582 break;
3583 }
3584 }
3585 def_stmt = SSA_NAME_DEF_STMT (vuse);
3586 if (gimple_nop_p (def_stmt))
3587 break;
3588 else if (gimple_code (def_stmt) == GIMPLE_PHI)
3589 vuse = get_continuation_for_phi (def_stmt, ref, tbaa_p, limit,
3590 &visited, translated, translate, data);
3591 else
3592 {
3593 if ((int)limit <= 0)
3594 {
3595 res = NULL;
3596 break;
3597 }
3598 --limit;
3599 if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
3600 {
3601 if (!translate)
3602 break;
3603 translate_flags disambiguate_only = TR_TRANSLATE;
3604 res = (*translate) (ref, vuse, data, &disambiguate_only);
3605 /* Failed lookup and translation. */
3606 if (res == (void *)-1)
3607 {
3608 res = NULL;
3609 break;
3610 }
3611 /* Lookup succeeded. */
3612 else if (res != NULL)
3613 break;
3614 /* Translation succeeded, continue walking. */
3615 translated = translated || disambiguate_only == TR_TRANSLATE;
3616 }
3617 vuse = gimple_vuse (def_stmt);
3618 }
3619 }
3620 while (vuse);
3621
3622 if (visited)
3623 BITMAP_FREE (visited);
3624
3625 timevar_pop (TV_ALIAS_STMT_WALK);
3626
3627 return res;
3628 }
3629
3630
3631 /* Based on the memory reference REF call WALKER for each vdef which
3632 defining statement may clobber REF, starting with VDEF. If REF
3633 is NULL_TREE, each defining statement is visited.
3634
3635 WALKER is called with REF, the current vdef and DATA. If WALKER
3636 returns true the walk is stopped, otherwise it continues.
3637
3638 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
3639 The pointer may be NULL and then we do not track this information.
3640
3641 At PHI nodes walk_aliased_vdefs forks into one walk for reach
3642 PHI argument (but only one walk continues on merge points), the
3643 return value is true if any of the walks was successful.
3644
3645 The function returns the number of statements walked or -1 if
3646 LIMIT stmts were walked and the walk was aborted at this point.
3647 If LIMIT is zero the walk is not aborted. */
3648
3649 static int
3650 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
3651 bool (*walker)(ao_ref *, tree, void *), void *data,
3652 bitmap *visited, unsigned int cnt,
3653 bool *function_entry_reached, unsigned limit)
3654 {
3655 do
3656 {
3657 gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
3658
3659 if (*visited
3660 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
3661 return cnt;
3662
3663 if (gimple_nop_p (def_stmt))
3664 {
3665 if (function_entry_reached)
3666 *function_entry_reached = true;
3667 return cnt;
3668 }
3669 else if (gimple_code (def_stmt) == GIMPLE_PHI)
3670 {
3671 unsigned i;
3672 if (!*visited)
3673 *visited = BITMAP_ALLOC (NULL);
3674 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
3675 {
3676 int res = walk_aliased_vdefs_1 (ref,
3677 gimple_phi_arg_def (def_stmt, i),
3678 walker, data, visited, cnt,
3679 function_entry_reached, limit);
3680 if (res == -1)
3681 return -1;
3682 cnt = res;
3683 }
3684 return cnt;
3685 }
3686
3687 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3688 cnt++;
3689 if (cnt == limit)
3690 return -1;
3691 if ((!ref
3692 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
3693 && (*walker) (ref, vdef, data))
3694 return cnt;
3695
3696 vdef = gimple_vuse (def_stmt);
3697 }
3698 while (1);
3699 }
3700
3701 int
3702 walk_aliased_vdefs (ao_ref *ref, tree vdef,
3703 bool (*walker)(ao_ref *, tree, void *), void *data,
3704 bitmap *visited,
3705 bool *function_entry_reached, unsigned int limit)
3706 {
3707 bitmap local_visited = NULL;
3708 int ret;
3709
3710 timevar_push (TV_ALIAS_STMT_WALK);
3711
3712 if (function_entry_reached)
3713 *function_entry_reached = false;
3714
3715 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
3716 visited ? visited : &local_visited, 0,
3717 function_entry_reached, limit);
3718 if (local_visited)
3719 BITMAP_FREE (local_visited);
3720
3721 timevar_pop (TV_ALIAS_STMT_WALK);
3722
3723 return ret;
3724 }
3725