tree-ssa-alias.c (aliasing_component_refs_walk): Initialize same_p to 0.
[gcc.git] / gcc / tree-ssa-alias.c
1 /* Alias analysis for trees.
2 Copyright (C) 2004-2019 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_component_refs_since_match_p (tree, tree, tree, tree);
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_component_refs_since_match_p_may_alias;
108 unsigned HOST_WIDE_INT nonoverlapping_component_refs_since_match_p_must_overlap;
109 unsigned HOST_WIDE_INT nonoverlapping_component_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_component_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_component_refs_since_match_p_no_alias,
145 alias_stats.nonoverlapping_component_refs_since_match_p_must_overlap,
146 alias_stats.nonoverlapping_component_refs_since_match_p_no_alias
147 + alias_stats.nonoverlapping_component_refs_since_match_p_may_alias
148 + alias_stats.nonoverlapping_component_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.
860
861 Try to disambiguate using the access path starting from the match
862 and return false if there is no conflict.
863
864 Helper for aliasing_component_refs_p. */
865
866 static bool
867 aliasing_matching_component_refs_p (tree match1, tree ref1,
868 poly_int64 offset1, poly_int64 max_size1,
869 tree match2, tree ref2,
870 poly_int64 offset2, poly_int64 max_size2)
871 {
872 poly_int64 offadj, sztmp, msztmp;
873 bool reverse;
874
875
876 get_ref_base_and_extent (match2, &offadj, &sztmp, &msztmp, &reverse);
877 offset2 -= offadj;
878 get_ref_base_and_extent (match1, &offadj, &sztmp, &msztmp, &reverse);
879 offset1 -= offadj;
880 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
881 {
882 ++alias_stats.aliasing_component_refs_p_no_alias;
883 return false;
884 }
885
886 int cmp = nonoverlapping_component_refs_since_match_p (match1, ref1,
887 match2, ref2);
888 if (cmp == 1
889 || (cmp == -1 && nonoverlapping_component_refs_p (ref1, ref2)))
890 {
891 ++alias_stats.aliasing_component_refs_p_no_alias;
892 return false;
893 }
894 ++alias_stats.aliasing_component_refs_p_may_alias;
895 return true;
896 }
897
898 /* Return true if REF is reference to zero sized trailing array. I.e.
899 struct foo {int bar; int array[0];} *fooptr;
900 fooptr->array. */
901
902 static bool
903 component_ref_to_zero_sized_trailing_array_p (tree ref)
904 {
905 return (TREE_CODE (ref) == COMPONENT_REF
906 && TREE_CODE (TREE_TYPE (TREE_OPERAND (ref, 1))) == ARRAY_TYPE
907 && (!TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)))
908 || integer_zerop (TYPE_SIZE (TREE_TYPE (TREE_OPERAND (ref, 1)))))
909 && array_at_struct_end_p (ref));
910 }
911
912 /* Worker for aliasing_component_refs_p. Most parameters match parameters of
913 aliasing_component_refs_p.
914
915 Walk access path REF2 and try to find type matching TYPE1
916 (which is a start of possibly aliasing access path REF1).
917 If match is found, try to disambiguate.
918
919 Return 0 for sucessful disambiguation.
920 Return 1 if match was found but disambiguation failed
921 Return -1 if there is no match.
922 In this case MAYBE_MATCH is set to 0 if there is no type matching TYPE1
923 in access patch REF2 and -1 if we are not sure. */
924
925 static int
926 aliasing_component_refs_walk (tree ref1, tree type1, tree base1,
927 poly_int64 offset1, poly_int64 max_size1,
928 tree end_struct_ref1,
929 tree ref2, tree base2,
930 poly_int64 offset2, poly_int64 max_size2,
931 bool *maybe_match)
932 {
933 tree ref = ref2;
934 int same_p = 0;
935
936 while (true)
937 {
938 /* We walk from inner type to the outer types. If type we see is
939 already too large to be part of type1, terminate the search. */
940 int cmp = compare_type_sizes (type1, TREE_TYPE (ref));
941
942 if (cmp < 0
943 && (!end_struct_ref1
944 || compare_type_sizes (TREE_TYPE (end_struct_ref1),
945 TREE_TYPE (ref)) < 0))
946 break;
947 /* If types may be of same size, see if we can decide about their
948 equality. */
949 if (cmp == 0)
950 {
951 same_p = same_type_for_tbaa (TREE_TYPE (ref), type1);
952 if (same_p == 1)
953 break;
954 /* In case we can't decide whether types are same try to
955 continue looking for the exact match.
956 Remember however that we possibly saw a match
957 to bypass the access path continuations tests we do later. */
958 if (same_p == -1)
959 *maybe_match = true;
960 }
961 if (!handled_component_p (ref))
962 break;
963 ref = TREE_OPERAND (ref, 0);
964 }
965 if (same_p == 1)
966 {
967 /* We assume that arrays can overlap by multiple of their elements
968 size as tested in gcc.dg/torture/alias-2.c.
969 This partial overlap happen only when both arrays are bases of
970 the access and not contained within another component ref.
971 To be safe we also assume partial overlap for VLAs. */
972 if (TREE_CODE (TREE_TYPE (base1)) == ARRAY_TYPE
973 && (!TYPE_SIZE (TREE_TYPE (base1))
974 || TREE_CODE (TYPE_SIZE (TREE_TYPE (base1))) != INTEGER_CST
975 || ref == base2))
976 /* Setting maybe_match to true triggers
977 nonoverlapping_component_refs_p test later that still may do
978 useful disambiguation. */
979 *maybe_match = true;
980 else
981 return aliasing_matching_component_refs_p (base1, ref1,
982 offset1, max_size1,
983 ref, ref2,
984 offset2, max_size2);
985 }
986 return -1;
987 }
988
989 /* Determine if the two component references REF1 and REF2 which are
990 based on access types TYPE1 and TYPE2 and of which at least one is based
991 on an indirect reference may alias.
992 REF1_ALIAS_SET, BASE1_ALIAS_SET, REF2_ALIAS_SET and BASE2_ALIAS_SET
993 are the respective alias sets. */
994
995 static bool
996 aliasing_component_refs_p (tree ref1,
997 alias_set_type ref1_alias_set,
998 alias_set_type base1_alias_set,
999 poly_int64 offset1, poly_int64 max_size1,
1000 tree ref2,
1001 alias_set_type ref2_alias_set,
1002 alias_set_type base2_alias_set,
1003 poly_int64 offset2, poly_int64 max_size2)
1004 {
1005 /* If one reference is a component references through pointers try to find a
1006 common base and apply offset based disambiguation. This handles
1007 for example
1008 struct A { int i; int j; } *q;
1009 struct B { struct A a; int k; } *p;
1010 disambiguating q->i and p->a.j. */
1011 tree base1, base2;
1012 tree type1, type2;
1013 bool maybe_match = false;
1014 tree end_struct_ref1 = NULL, end_struct_ref2 = NULL;
1015
1016 /* Choose bases and base types to search for. */
1017 base1 = ref1;
1018 while (handled_component_p (base1))
1019 {
1020 /* Generally access paths are monotous in the size of object. The
1021 exception are trailing arrays of structures. I.e.
1022 struct a {int array[0];};
1023 or
1024 struct a {int array1[0]; int array[];};
1025 Such struct has size 0 but accesses to a.array may have non-zero size.
1026 In this case the size of TREE_TYPE (base1) is smaller than
1027 size of TREE_TYPE (TREE_OPERNAD (base1, 0)).
1028
1029 Because we compare sizes of arrays just by sizes of their elements,
1030 we only need to care about zero sized array fields here. */
1031 if (component_ref_to_zero_sized_trailing_array_p (base1))
1032 {
1033 gcc_checking_assert (!end_struct_ref1);
1034 end_struct_ref1 = base1;
1035 }
1036 if (TREE_CODE (base1) == VIEW_CONVERT_EXPR
1037 || TREE_CODE (base1) == BIT_FIELD_REF)
1038 ref1 = TREE_OPERAND (base1, 0);
1039 base1 = TREE_OPERAND (base1, 0);
1040 }
1041 type1 = TREE_TYPE (base1);
1042 base2 = ref2;
1043 while (handled_component_p (base2))
1044 {
1045 if (component_ref_to_zero_sized_trailing_array_p (base2))
1046 {
1047 gcc_checking_assert (!end_struct_ref2);
1048 end_struct_ref2 = base2;
1049 }
1050 if (TREE_CODE (base2) == VIEW_CONVERT_EXPR
1051 || TREE_CODE (base2) == BIT_FIELD_REF)
1052 ref2 = TREE_OPERAND (base2, 0);
1053 base2 = TREE_OPERAND (base2, 0);
1054 }
1055 type2 = TREE_TYPE (base2);
1056
1057 /* Now search for the type1 in the access path of ref2. This
1058 would be a common base for doing offset based disambiguation on.
1059 This however only makes sense if type2 is big enough to hold type1. */
1060 int cmp_outer = compare_type_sizes (type2, type1);
1061
1062 /* If type2 is big enough to contain type1 walk its access path.
1063 We also need to care of arrays at the end of structs that may extend
1064 beyond the end of structure. */
1065 if (cmp_outer >= 0
1066 || (end_struct_ref2
1067 && compare_type_sizes (TREE_TYPE (end_struct_ref2), type1) >= 0))
1068 {
1069 int res = aliasing_component_refs_walk (ref1, type1, base1,
1070 offset1, max_size1,
1071 end_struct_ref1,
1072 ref2, base2, offset2, max_size2,
1073 &maybe_match);
1074 if (res != -1)
1075 return res;
1076 }
1077
1078 /* If we didn't find a common base, try the other way around. */
1079 if (cmp_outer <= 0
1080 || (end_struct_ref1
1081 && compare_type_sizes (TREE_TYPE (end_struct_ref1), type1) <= 0))
1082 {
1083 int res = aliasing_component_refs_walk (ref2, type2, base2,
1084 offset2, max_size2,
1085 end_struct_ref2,
1086 ref1, base1, offset1, max_size1,
1087 &maybe_match);
1088 if (res != -1)
1089 return res;
1090 }
1091
1092 /* In the following code we make an assumption that the types in access
1093 paths do not overlap and thus accesses alias only if one path can be
1094 continuation of another. If we was not able to decide about equivalence,
1095 we need to give up. */
1096 if (maybe_match)
1097 {
1098 if (!nonoverlapping_component_refs_p (ref1, ref2))
1099 {
1100 ++alias_stats.aliasing_component_refs_p_may_alias;
1101 return true;
1102 }
1103 ++alias_stats.aliasing_component_refs_p_no_alias;
1104 return false;
1105 }
1106
1107 /* If we have two type access paths B1.path1 and B2.path2 they may
1108 only alias if either B1 is in B2.path2 or B2 is in B1.path1.
1109 But we can still have a path that goes B1.path1...B2.path2 with
1110 a part that we do not see. So we can only disambiguate now
1111 if there is no B2 in the tail of path1 and no B1 on the
1112 tail of path2. */
1113 if (compare_type_sizes (TREE_TYPE (ref2), type1) >= 0
1114 && (!end_struct_ref1
1115 || compare_type_sizes (TREE_TYPE (ref2),
1116 TREE_TYPE (end_struct_ref1)) >= 0)
1117 && type_has_components_p (TREE_TYPE (ref2))
1118 && (base1_alias_set == ref2_alias_set
1119 || alias_set_subset_of (base1_alias_set, ref2_alias_set)))
1120 {
1121 ++alias_stats.aliasing_component_refs_p_may_alias;
1122 return true;
1123 }
1124 /* If this is ptr vs. decl then we know there is no ptr ... decl path. */
1125 if (compare_type_sizes (TREE_TYPE (ref1), type2) >= 0
1126 && (!end_struct_ref2
1127 || compare_type_sizes (TREE_TYPE (ref1),
1128 TREE_TYPE (end_struct_ref2)) >= 0)
1129 && type_has_components_p (TREE_TYPE (ref1))
1130 && (base2_alias_set == ref1_alias_set
1131 || alias_set_subset_of (base2_alias_set, ref1_alias_set)))
1132 {
1133 ++alias_stats.aliasing_component_refs_p_may_alias;
1134 return true;
1135 }
1136 ++alias_stats.aliasing_component_refs_p_no_alias;
1137 return false;
1138 }
1139
1140 /* FIELD1 and FIELD2 are two fields of component refs. We assume
1141 that bases of both component refs are either equivalent or nonoverlapping.
1142 We do not assume that the containers of FIELD1 and FIELD2 are of the
1143 same type or size.
1144
1145 Return 0 in case the base address of component_refs are same then
1146 FIELD1 and FIELD2 have same address. Note that FIELD1 and FIELD2
1147 may not be of same type or size.
1148
1149 Return 1 if FIELD1 and FIELD2 are non-overlapping.
1150
1151 Return -1 otherwise.
1152
1153 Main difference between 0 and -1 is to let
1154 nonoverlapping_component_refs_since_match_p discover the semantically
1155 equivalent part of the access path.
1156
1157 Note that this function is used even with -fno-strict-aliasing
1158 and makes use of no TBAA assumptions. */
1159
1160 static int
1161 nonoverlapping_component_refs_p_1 (const_tree field1, const_tree field2)
1162 {
1163 /* If both fields are of the same type, we could save hard work of
1164 comparing offsets. */
1165 tree type1 = DECL_CONTEXT (field1);
1166 tree type2 = DECL_CONTEXT (field2);
1167
1168 if (TREE_CODE (type1) == RECORD_TYPE
1169 && DECL_BIT_FIELD_REPRESENTATIVE (field1))
1170 field1 = DECL_BIT_FIELD_REPRESENTATIVE (field1);
1171 if (TREE_CODE (type2) == RECORD_TYPE
1172 && DECL_BIT_FIELD_REPRESENTATIVE (field2))
1173 field2 = DECL_BIT_FIELD_REPRESENTATIVE (field2);
1174
1175 /* ??? Bitfields can overlap at RTL level so punt on them.
1176 FIXME: RTL expansion should be fixed by adjusting the access path
1177 when producing MEM_ATTRs for MEMs which are wider than
1178 the bitfields similarly as done in set_mem_attrs_minus_bitpos. */
1179 if (DECL_BIT_FIELD (field1) && DECL_BIT_FIELD (field2))
1180 return -1;
1181
1182 /* Assume that different FIELD_DECLs never overlap within a RECORD_TYPE. */
1183 if (type1 == type2 && TREE_CODE (type1) == RECORD_TYPE)
1184 return field1 != field2;
1185
1186 /* In common case the offsets and bit offsets will be the same.
1187 However if frontends do not agree on the alignment, they may be
1188 different even if they actually represent same address.
1189 Try the common case first and if that fails calcualte the
1190 actual bit offset. */
1191 if (tree_int_cst_equal (DECL_FIELD_OFFSET (field1),
1192 DECL_FIELD_OFFSET (field2))
1193 && tree_int_cst_equal (DECL_FIELD_BIT_OFFSET (field1),
1194 DECL_FIELD_BIT_OFFSET (field2)))
1195 return 0;
1196
1197 /* Note that it may be possible to use component_ref_field_offset
1198 which would provide offsets as trees. However constructing and folding
1199 trees is expensive and does not seem to be worth the compile time
1200 cost. */
1201
1202 poly_uint64 offset1, offset2;
1203 poly_uint64 bit_offset1, bit_offset2;
1204
1205 if (poly_int_tree_p (DECL_FIELD_OFFSET (field1), &offset1)
1206 && poly_int_tree_p (DECL_FIELD_OFFSET (field2), &offset2)
1207 && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field1), &bit_offset1)
1208 && poly_int_tree_p (DECL_FIELD_BIT_OFFSET (field2), &bit_offset2))
1209 {
1210 offset1 = (offset1 << LOG2_BITS_PER_UNIT) + bit_offset1;
1211 offset2 = (offset2 << LOG2_BITS_PER_UNIT) + bit_offset2;
1212
1213 if (known_eq (offset1, offset2))
1214 return 0;
1215
1216 poly_uint64 size1, size2;
1217
1218 if (poly_int_tree_p (DECL_SIZE (field1), &size1)
1219 && poly_int_tree_p (DECL_SIZE (field2), &size2)
1220 && !ranges_maybe_overlap_p (offset1, size1, offset2, size2))
1221 return 1;
1222 }
1223 /* Resort to slower overlap checking by looking for matching types in
1224 the middle of access path. */
1225 return -1;
1226 }
1227
1228 /* Try to disambiguate REF1 and REF2 under the assumption that MATCH1 and
1229 MATCH2 either point to the same address or are disjoint.
1230 MATCH1 and MATCH2 are assumed to be ref in the access path of REF1 and REF2
1231 respectively or NULL in the case we established equivalence of bases.
1232
1233 This test works by matching the initial segment of the access path
1234 and does not rely on TBAA thus is safe for !flag_strict_aliasing if
1235 match was determined without use of TBAA oracle.
1236
1237 Return 1 if we can determine that component references REF1 and REF2,
1238 that are within a common DECL, cannot overlap.
1239
1240 Return 0 if paths are same and thus there is nothing to disambiguate more
1241 (i.e. there is must alias assuming there is must alias between MATCH1 and
1242 MATCH2)
1243
1244 Return -1 if we can not determine 0 or 1 - this happens when we met
1245 non-matching types was met in the path.
1246 In this case it may make sense to continue by other disambiguation
1247 oracles. */
1248
1249 static int
1250 nonoverlapping_component_refs_since_match_p (tree match1, tree ref1,
1251 tree match2, tree ref2)
1252 {
1253 /* Early return if there are no references to match, we do not need
1254 to walk the access paths.
1255
1256 Do not consider this as may-alias for stats - it is more useful
1257 to have information how many disambiguations happened provided that
1258 the query was meaningful. */
1259
1260 if (match1 == ref1 || !handled_component_p (ref1)
1261 || match2 == ref2 || !handled_component_p (ref2))
1262 return -1;
1263
1264 auto_vec<tree, 16> component_refs1;
1265 auto_vec<tree, 16> component_refs2;
1266
1267 /* Create the stack of handled components for REF1. */
1268 while (handled_component_p (ref1) && ref1 != match1)
1269 {
1270 if (TREE_CODE (ref1) == VIEW_CONVERT_EXPR
1271 || TREE_CODE (ref1) == BIT_FIELD_REF)
1272 component_refs1.truncate (0);
1273 else
1274 component_refs1.safe_push (ref1);
1275 ref1 = TREE_OPERAND (ref1, 0);
1276 }
1277
1278 /* Create the stack of handled components for REF2. */
1279 while (handled_component_p (ref2) && ref2 != match2)
1280 {
1281 if (TREE_CODE (ref2) == VIEW_CONVERT_EXPR
1282 || TREE_CODE (ref2) == BIT_FIELD_REF)
1283 component_refs2.truncate (0);
1284 else
1285 component_refs2.safe_push (ref2);
1286 ref2 = TREE_OPERAND (ref2, 0);
1287 }
1288
1289 bool mem_ref1 = TREE_CODE (ref1) == MEM_REF && ref1 != match1;
1290 bool mem_ref2 = TREE_CODE (ref2) == MEM_REF && ref2 != match2;
1291
1292 /* If only one of access path starts with MEM_REF check that offset is 0
1293 so the addresses stays the same after stripping it.
1294 TODO: In this case we may walk the other access path until we get same
1295 offset.
1296
1297 If both starts with MEM_REF, offset has to be same. */
1298 if ((mem_ref1 && !mem_ref2 && !integer_zerop (TREE_OPERAND (ref1, 1)))
1299 || (mem_ref2 && !mem_ref1 && !integer_zerop (TREE_OPERAND (ref2, 1)))
1300 || (mem_ref1 && mem_ref2
1301 && !tree_int_cst_equal (TREE_OPERAND (ref1, 1),
1302 TREE_OPERAND (ref2, 1))))
1303 {
1304 ++alias_stats.nonoverlapping_component_refs_since_match_p_may_alias;
1305 return -1;
1306 }
1307
1308 /* TARGET_MEM_REF are never wrapped in handled components, so we do not need
1309 to handle them here at all. */
1310 gcc_checking_assert (TREE_CODE (ref1) != TARGET_MEM_REF
1311 && TREE_CODE (ref2) != TARGET_MEM_REF);
1312
1313 /* Pop the stacks in parallel and examine the COMPONENT_REFs of the same
1314 rank. This is sufficient because we start from the same DECL and you
1315 cannot reference several fields at a time with COMPONENT_REFs (unlike
1316 with ARRAY_RANGE_REFs for arrays) so you always need the same number
1317 of them to access a sub-component, unless you're in a union, in which
1318 case the return value will precisely be false. */
1319 while (true)
1320 {
1321 bool seen_noncomponent_ref_p = false;
1322 do
1323 {
1324 if (component_refs1.is_empty ())
1325 {
1326 ++alias_stats
1327 .nonoverlapping_component_refs_since_match_p_must_overlap;
1328 return 0;
1329 }
1330 ref1 = component_refs1.pop ();
1331 if (TREE_CODE (ref1) != COMPONENT_REF)
1332 seen_noncomponent_ref_p = true;
1333 }
1334 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref1, 0))));
1335
1336 do
1337 {
1338 if (component_refs2.is_empty ())
1339 {
1340 ++alias_stats
1341 .nonoverlapping_component_refs_since_match_p_must_overlap;
1342 return 0;
1343 }
1344 ref2 = component_refs2.pop ();
1345 if (TREE_CODE (ref2) != COMPONENT_REF)
1346 seen_noncomponent_ref_p = true;
1347 }
1348 while (!RECORD_OR_UNION_TYPE_P (TREE_TYPE (TREE_OPERAND (ref2, 0))));
1349
1350 /* BIT_FIELD_REF and VIEW_CONVERT_EXPR are taken off the vectors
1351 earlier. */
1352 gcc_checking_assert (TREE_CODE (ref1) == COMPONENT_REF
1353 && TREE_CODE (ref2) == COMPONENT_REF);
1354
1355 tree field1 = TREE_OPERAND (ref1, 1);
1356 tree field2 = TREE_OPERAND (ref2, 1);
1357
1358 /* ??? We cannot simply use the type of operand #0 of the refs here
1359 as the Fortran compiler smuggles type punning into COMPONENT_REFs
1360 for common blocks instead of using unions like everyone else. */
1361 tree type1 = DECL_CONTEXT (field1);
1362 tree type2 = DECL_CONTEXT (field2);
1363
1364 /* If we skipped array refs on type of different sizes, we can
1365 no longer be sure that there are not partial overlaps. */
1366 if (seen_noncomponent_ref_p
1367 && !operand_equal_p (TYPE_SIZE (type1), TYPE_SIZE (type2), 0))
1368 {
1369 ++alias_stats
1370 .nonoverlapping_component_refs_since_match_p_may_alias;
1371 return -1;
1372 }
1373
1374 int cmp = nonoverlapping_component_refs_p_1 (field1, field2);
1375 if (cmp == -1)
1376 {
1377 ++alias_stats
1378 .nonoverlapping_component_refs_since_match_p_may_alias;
1379 return -1;
1380 }
1381 else if (cmp == 1)
1382 {
1383 ++alias_stats
1384 .nonoverlapping_component_refs_since_match_p_no_alias;
1385 return 1;
1386 }
1387 }
1388
1389 ++alias_stats.nonoverlapping_component_refs_since_match_p_must_overlap;
1390 return 0;
1391 }
1392
1393 /* Return TYPE_UID which can be used to match record types we consider
1394 same for TBAA purposes. */
1395
1396 static inline int
1397 ncr_type_uid (const_tree field)
1398 {
1399 /* ??? We cannot simply use the type of operand #0 of the refs here
1400 as the Fortran compiler smuggles type punning into COMPONENT_REFs
1401 for common blocks instead of using unions like everyone else. */
1402 tree type = DECL_FIELD_CONTEXT (field);
1403 /* With LTO types considered same_type_for_tbaa_p
1404 from different translation unit may not have same
1405 main variant. They however have same TYPE_CANONICAL. */
1406 if (TYPE_CANONICAL (type))
1407 return TYPE_UID (TYPE_CANONICAL (type));
1408 return TYPE_UID (type);
1409 }
1410
1411 /* qsort compare function to sort FIELD_DECLs after their
1412 DECL_FIELD_CONTEXT TYPE_UID. */
1413
1414 static inline int
1415 ncr_compar (const void *field1_, const void *field2_)
1416 {
1417 const_tree field1 = *(const_tree *) const_cast <void *>(field1_);
1418 const_tree field2 = *(const_tree *) const_cast <void *>(field2_);
1419 unsigned int uid1 = ncr_type_uid (field1);
1420 unsigned int uid2 = ncr_type_uid (field2);
1421
1422 if (uid1 < uid2)
1423 return -1;
1424 else if (uid1 > uid2)
1425 return 1;
1426 return 0;
1427 }
1428
1429 /* Return true if we can determine that the fields referenced cannot
1430 overlap for any pair of objects. This relies on TBAA. */
1431
1432 static bool
1433 nonoverlapping_component_refs_p (const_tree x, const_tree y)
1434 {
1435 /* Early return if we have nothing to do.
1436
1437 Do not consider this as may-alias for stats - it is more useful
1438 to have information how many disambiguations happened provided that
1439 the query was meaningful. */
1440 if (!flag_strict_aliasing
1441 || !x || !y
1442 || !handled_component_p (x)
1443 || !handled_component_p (y))
1444 return false;
1445
1446 auto_vec<const_tree, 16> fieldsx;
1447 while (handled_component_p (x))
1448 {
1449 if (TREE_CODE (x) == COMPONENT_REF)
1450 {
1451 tree field = TREE_OPERAND (x, 1);
1452 tree type = DECL_FIELD_CONTEXT (field);
1453 if (TREE_CODE (type) == RECORD_TYPE)
1454 fieldsx.safe_push (field);
1455 }
1456 else if (TREE_CODE (x) == VIEW_CONVERT_EXPR
1457 || TREE_CODE (x) == BIT_FIELD_REF)
1458 fieldsx.truncate (0);
1459 x = TREE_OPERAND (x, 0);
1460 }
1461 if (fieldsx.length () == 0)
1462 return false;
1463 auto_vec<const_tree, 16> fieldsy;
1464 while (handled_component_p (y))
1465 {
1466 if (TREE_CODE (y) == COMPONENT_REF)
1467 {
1468 tree field = TREE_OPERAND (y, 1);
1469 tree type = DECL_FIELD_CONTEXT (field);
1470 if (TREE_CODE (type) == RECORD_TYPE)
1471 fieldsy.safe_push (TREE_OPERAND (y, 1));
1472 }
1473 else if (TREE_CODE (y) == VIEW_CONVERT_EXPR
1474 || TREE_CODE (y) == BIT_FIELD_REF)
1475 fieldsy.truncate (0);
1476 y = TREE_OPERAND (y, 0);
1477 }
1478 if (fieldsy.length () == 0)
1479 {
1480 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1481 return false;
1482 }
1483
1484 /* Most common case first. */
1485 if (fieldsx.length () == 1
1486 && fieldsy.length () == 1)
1487 {
1488 if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldsx[0]),
1489 DECL_FIELD_CONTEXT (fieldsy[0])) == 1
1490 && nonoverlapping_component_refs_p_1 (fieldsx[0], fieldsy[0]) == 1)
1491 {
1492 ++alias_stats.nonoverlapping_component_refs_p_no_alias;
1493 return true;
1494 }
1495 else
1496 {
1497 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1498 return false;
1499 }
1500 }
1501
1502 if (fieldsx.length () == 2)
1503 {
1504 if (ncr_compar (&fieldsx[0], &fieldsx[1]) == 1)
1505 std::swap (fieldsx[0], fieldsx[1]);
1506 }
1507 else
1508 fieldsx.qsort (ncr_compar);
1509
1510 if (fieldsy.length () == 2)
1511 {
1512 if (ncr_compar (&fieldsy[0], &fieldsy[1]) == 1)
1513 std::swap (fieldsy[0], fieldsy[1]);
1514 }
1515 else
1516 fieldsy.qsort (ncr_compar);
1517
1518 unsigned i = 0, j = 0;
1519 do
1520 {
1521 const_tree fieldx = fieldsx[i];
1522 const_tree fieldy = fieldsy[j];
1523
1524 /* We're left with accessing different fields of a structure,
1525 no possible overlap. */
1526 if (same_type_for_tbaa (DECL_FIELD_CONTEXT (fieldx),
1527 DECL_FIELD_CONTEXT (fieldy)) == 1
1528 && nonoverlapping_component_refs_p_1 (fieldx, fieldy) == 1)
1529 {
1530 ++alias_stats.nonoverlapping_component_refs_p_no_alias;
1531 return true;
1532 }
1533
1534 if (ncr_type_uid (fieldx) < ncr_type_uid (fieldy))
1535 {
1536 i++;
1537 if (i == fieldsx.length ())
1538 break;
1539 }
1540 else
1541 {
1542 j++;
1543 if (j == fieldsy.length ())
1544 break;
1545 }
1546 }
1547 while (1);
1548
1549 ++alias_stats.nonoverlapping_component_refs_p_may_alias;
1550 return false;
1551 }
1552
1553
1554 /* Return true if two memory references based on the variables BASE1
1555 and BASE2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1556 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. REF1 and REF2
1557 if non-NULL are the complete memory reference trees. */
1558
1559 static bool
1560 decl_refs_may_alias_p (tree ref1, tree base1,
1561 poly_int64 offset1, poly_int64 max_size1,
1562 poly_int64 size1,
1563 tree ref2, tree base2,
1564 poly_int64 offset2, poly_int64 max_size2,
1565 poly_int64 size2)
1566 {
1567 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
1568
1569 /* If both references are based on different variables, they cannot alias. */
1570 if (compare_base_decls (base1, base2) == 0)
1571 return false;
1572
1573 /* If both references are based on the same variable, they cannot alias if
1574 the accesses do not overlap. */
1575 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
1576 return false;
1577
1578 /* If there is must alias, there is no use disambiguating further. */
1579 if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
1580 return true;
1581
1582 /* For components with variable position, the above test isn't sufficient,
1583 so we disambiguate component references manually. */
1584 if (ref1 && ref2
1585 && handled_component_p (ref1) && handled_component_p (ref2)
1586 && nonoverlapping_component_refs_since_match_p (NULL, ref1,
1587 NULL, ref2) == 1)
1588 return false;
1589
1590 return true;
1591 }
1592
1593 /* Return true if an indirect reference based on *PTR1 constrained
1594 to [OFFSET1, OFFSET1 + MAX_SIZE1) may alias a variable based on BASE2
1595 constrained to [OFFSET2, OFFSET2 + MAX_SIZE2). *PTR1 and BASE2 have
1596 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1597 in which case they are computed on-demand. REF1 and REF2
1598 if non-NULL are the complete memory reference trees. */
1599
1600 static bool
1601 indirect_ref_may_alias_decl_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1602 poly_int64 offset1, poly_int64 max_size1,
1603 poly_int64 size1,
1604 alias_set_type ref1_alias_set,
1605 alias_set_type base1_alias_set,
1606 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1607 poly_int64 offset2, poly_int64 max_size2,
1608 poly_int64 size2,
1609 alias_set_type ref2_alias_set,
1610 alias_set_type base2_alias_set, bool tbaa_p)
1611 {
1612 tree ptr1;
1613 tree ptrtype1, dbase2;
1614
1615 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1616 || TREE_CODE (base1) == TARGET_MEM_REF)
1617 && DECL_P (base2));
1618
1619 ptr1 = TREE_OPERAND (base1, 0);
1620 poly_offset_int moff = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
1621
1622 /* If only one reference is based on a variable, they cannot alias if
1623 the pointer access is beyond the extent of the variable access.
1624 (the pointer base cannot validly point to an offset less than zero
1625 of the variable).
1626 ??? IVOPTs creates bases that do not honor this restriction,
1627 so do not apply this optimization for TARGET_MEM_REFs. */
1628 if (TREE_CODE (base1) != TARGET_MEM_REF
1629 && !ranges_maybe_overlap_p (offset1 + moff, -1, offset2, max_size2))
1630 return false;
1631 /* They also cannot alias if the pointer may not point to the decl. */
1632 if (!ptr_deref_may_alias_decl_p (ptr1, base2))
1633 return false;
1634
1635 /* Disambiguations that rely on strict aliasing rules follow. */
1636 if (!flag_strict_aliasing || !tbaa_p)
1637 return true;
1638
1639 /* If the alias set for a pointer access is zero all bets are off. */
1640 if (base1_alias_set == 0 || base2_alias_set == 0)
1641 return true;
1642
1643 /* When we are trying to disambiguate an access with a pointer dereference
1644 as base versus one with a decl as base we can use both the size
1645 of the decl and its dynamic type for extra disambiguation.
1646 ??? We do not know anything about the dynamic type of the decl
1647 other than that its alias-set contains base2_alias_set as a subset
1648 which does not help us here. */
1649 /* As we know nothing useful about the dynamic type of the decl just
1650 use the usual conflict check rather than a subset test.
1651 ??? We could introduce -fvery-strict-aliasing when the language
1652 does not allow decls to have a dynamic type that differs from their
1653 static type. Then we can check
1654 !alias_set_subset_of (base1_alias_set, base2_alias_set) instead. */
1655 if (base1_alias_set != base2_alias_set
1656 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1657 return false;
1658
1659 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1660
1661 /* If the size of the access relevant for TBAA through the pointer
1662 is bigger than the size of the decl we can't possibly access the
1663 decl via that pointer. */
1664 if (/* ??? This in turn may run afoul when a decl of type T which is
1665 a member of union type U is accessed through a pointer to
1666 type U and sizeof T is smaller than sizeof U. */
1667 TREE_CODE (TREE_TYPE (ptrtype1)) != UNION_TYPE
1668 && TREE_CODE (TREE_TYPE (ptrtype1)) != QUAL_UNION_TYPE
1669 && compare_sizes (DECL_SIZE (base2),
1670 TYPE_SIZE (TREE_TYPE (ptrtype1))) < 0)
1671 return false;
1672
1673 if (!ref2)
1674 return true;
1675
1676 /* If the decl is accessed via a MEM_REF, reconstruct the base
1677 we can use for TBAA and an appropriately adjusted offset. */
1678 dbase2 = ref2;
1679 while (handled_component_p (dbase2))
1680 dbase2 = TREE_OPERAND (dbase2, 0);
1681 poly_int64 doffset1 = offset1;
1682 poly_offset_int doffset2 = offset2;
1683 if (TREE_CODE (dbase2) == MEM_REF
1684 || TREE_CODE (dbase2) == TARGET_MEM_REF)
1685 {
1686 doffset2 -= mem_ref_offset (dbase2) << LOG2_BITS_PER_UNIT;
1687 tree ptrtype2 = TREE_TYPE (TREE_OPERAND (dbase2, 1));
1688 /* If second reference is view-converted, give up now. */
1689 if (same_type_for_tbaa (TREE_TYPE (dbase2), TREE_TYPE (ptrtype2)) != 1)
1690 return true;
1691 }
1692
1693 /* If first reference is view-converted, give up now. */
1694 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1)
1695 return true;
1696
1697 /* If both references are through the same type, they do not alias
1698 if the accesses do not overlap. This does extra disambiguation
1699 for mixed/pointer accesses but requires strict aliasing.
1700 For MEM_REFs we require that the component-ref offset we computed
1701 is relative to the start of the type which we ensure by
1702 comparing rvalue and access type and disregarding the constant
1703 pointer offset.
1704
1705 But avoid treating variable length arrays as "objects", instead assume they
1706 can overlap by an exact multiple of their element size.
1707 See gcc.dg/torture/alias-2.c. */
1708 if (((TREE_CODE (base1) != TARGET_MEM_REF
1709 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1710 && (TREE_CODE (dbase2) != TARGET_MEM_REF
1711 || (!TMR_INDEX (dbase2) && !TMR_INDEX2 (dbase2))))
1712 && same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (dbase2)) == 1
1713 && (TREE_CODE (TREE_TYPE (base1)) != ARRAY_TYPE
1714 || (TYPE_SIZE (TREE_TYPE (base1))
1715 && TREE_CODE (TYPE_SIZE (TREE_TYPE (base1))) == INTEGER_CST)))
1716 {
1717 if (!ranges_maybe_overlap_p (doffset1, max_size1, doffset2, max_size2))
1718 return false;
1719 if (!ref1 || !ref2
1720 /* If there is must alias, there is no use disambiguating further. */
1721 || (known_eq (size1, max_size1) && known_eq (size2, max_size2)))
1722 return true;
1723 int res = nonoverlapping_component_refs_since_match_p (base1, ref1,
1724 base2, ref2);
1725 if (res == -1)
1726 return !nonoverlapping_component_refs_p (ref1, ref2);
1727 return !res;
1728 }
1729
1730 /* Do access-path based disambiguation. */
1731 if (ref1 && ref2
1732 && (handled_component_p (ref1) || handled_component_p (ref2)))
1733 return aliasing_component_refs_p (ref1,
1734 ref1_alias_set, base1_alias_set,
1735 offset1, max_size1,
1736 ref2,
1737 ref2_alias_set, base2_alias_set,
1738 offset2, max_size2);
1739
1740 return true;
1741 }
1742
1743 /* Return true if two indirect references based on *PTR1
1744 and *PTR2 constrained to [OFFSET1, OFFSET1 + MAX_SIZE1) and
1745 [OFFSET2, OFFSET2 + MAX_SIZE2) may alias. *PTR1 and *PTR2 have
1746 the alias sets BASE1_ALIAS_SET and BASE2_ALIAS_SET which can be -1
1747 in which case they are computed on-demand. REF1 and REF2
1748 if non-NULL are the complete memory reference trees. */
1749
1750 static bool
1751 indirect_refs_may_alias_p (tree ref1 ATTRIBUTE_UNUSED, tree base1,
1752 poly_int64 offset1, poly_int64 max_size1,
1753 poly_int64 size1,
1754 alias_set_type ref1_alias_set,
1755 alias_set_type base1_alias_set,
1756 tree ref2 ATTRIBUTE_UNUSED, tree base2,
1757 poly_int64 offset2, poly_int64 max_size2,
1758 poly_int64 size2,
1759 alias_set_type ref2_alias_set,
1760 alias_set_type base2_alias_set, bool tbaa_p)
1761 {
1762 tree ptr1;
1763 tree ptr2;
1764 tree ptrtype1, ptrtype2;
1765
1766 gcc_checking_assert ((TREE_CODE (base1) == MEM_REF
1767 || TREE_CODE (base1) == TARGET_MEM_REF)
1768 && (TREE_CODE (base2) == MEM_REF
1769 || TREE_CODE (base2) == TARGET_MEM_REF));
1770
1771 ptr1 = TREE_OPERAND (base1, 0);
1772 ptr2 = TREE_OPERAND (base2, 0);
1773
1774 /* If both bases are based on pointers they cannot alias if they may not
1775 point to the same memory object or if they point to the same object
1776 and the accesses do not overlap. */
1777 if ((!cfun || gimple_in_ssa_p (cfun))
1778 && operand_equal_p (ptr1, ptr2, 0)
1779 && (((TREE_CODE (base1) != TARGET_MEM_REF
1780 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1781 && (TREE_CODE (base2) != TARGET_MEM_REF
1782 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2))))
1783 || (TREE_CODE (base1) == TARGET_MEM_REF
1784 && TREE_CODE (base2) == TARGET_MEM_REF
1785 && (TMR_STEP (base1) == TMR_STEP (base2)
1786 || (TMR_STEP (base1) && TMR_STEP (base2)
1787 && operand_equal_p (TMR_STEP (base1),
1788 TMR_STEP (base2), 0)))
1789 && (TMR_INDEX (base1) == TMR_INDEX (base2)
1790 || (TMR_INDEX (base1) && TMR_INDEX (base2)
1791 && operand_equal_p (TMR_INDEX (base1),
1792 TMR_INDEX (base2), 0)))
1793 && (TMR_INDEX2 (base1) == TMR_INDEX2 (base2)
1794 || (TMR_INDEX2 (base1) && TMR_INDEX2 (base2)
1795 && operand_equal_p (TMR_INDEX2 (base1),
1796 TMR_INDEX2 (base2), 0))))))
1797 {
1798 poly_offset_int moff1 = mem_ref_offset (base1) << LOG2_BITS_PER_UNIT;
1799 poly_offset_int moff2 = mem_ref_offset (base2) << LOG2_BITS_PER_UNIT;
1800 if (!ranges_maybe_overlap_p (offset1 + moff1, max_size1,
1801 offset2 + moff2, max_size2))
1802 return false;
1803 /* If there is must alias, there is no use disambiguating further. */
1804 if (known_eq (size1, max_size1) && known_eq (size2, max_size2))
1805 return true;
1806 if (ref1 && ref2)
1807 {
1808 int res = nonoverlapping_component_refs_since_match_p (NULL, ref1,
1809 NULL, ref2);
1810 if (res != -1)
1811 return !res;
1812 }
1813 }
1814 if (!ptr_derefs_may_alias_p (ptr1, ptr2))
1815 return false;
1816
1817 /* Disambiguations that rely on strict aliasing rules follow. */
1818 if (!flag_strict_aliasing || !tbaa_p)
1819 return true;
1820
1821 ptrtype1 = TREE_TYPE (TREE_OPERAND (base1, 1));
1822 ptrtype2 = TREE_TYPE (TREE_OPERAND (base2, 1));
1823
1824 /* If the alias set for a pointer access is zero all bets are off. */
1825 if (base1_alias_set == 0
1826 || base2_alias_set == 0)
1827 return true;
1828
1829 /* Do type-based disambiguation. */
1830 if (base1_alias_set != base2_alias_set
1831 && !alias_sets_conflict_p (base1_alias_set, base2_alias_set))
1832 return false;
1833
1834 /* If either reference is view-converted, give up now. */
1835 if (same_type_for_tbaa (TREE_TYPE (base1), TREE_TYPE (ptrtype1)) != 1
1836 || same_type_for_tbaa (TREE_TYPE (base2), TREE_TYPE (ptrtype2)) != 1)
1837 return true;
1838
1839 /* If both references are through the same type, they do not alias
1840 if the accesses do not overlap. This does extra disambiguation
1841 for mixed/pointer accesses but requires strict aliasing. */
1842 if ((TREE_CODE (base1) != TARGET_MEM_REF
1843 || (!TMR_INDEX (base1) && !TMR_INDEX2 (base1)))
1844 && (TREE_CODE (base2) != TARGET_MEM_REF
1845 || (!TMR_INDEX (base2) && !TMR_INDEX2 (base2)))
1846 && same_type_for_tbaa (TREE_TYPE (ptrtype1),
1847 TREE_TYPE (ptrtype2)) == 1
1848 /* But avoid treating arrays as "objects", instead assume they
1849 can overlap by an exact multiple of their element size.
1850 See gcc.dg/torture/alias-2.c. */
1851 && TREE_CODE (TREE_TYPE (ptrtype1)) != ARRAY_TYPE)
1852 {
1853 if (!ranges_maybe_overlap_p (offset1, max_size1, offset2, max_size2))
1854 return false;
1855 if (!ref1 || !ref2
1856 || (known_eq (size1, max_size1) && known_eq (size2, max_size2)))
1857 return true;
1858 int res = nonoverlapping_component_refs_since_match_p (base1, ref1,
1859 base2, ref2);
1860 if (res == -1)
1861 return !nonoverlapping_component_refs_p (ref1, ref2);
1862 return !res;
1863 }
1864
1865 /* Do access-path based disambiguation. */
1866 if (ref1 && ref2
1867 && (handled_component_p (ref1) || handled_component_p (ref2)))
1868 return aliasing_component_refs_p (ref1,
1869 ref1_alias_set, base1_alias_set,
1870 offset1, max_size1,
1871 ref2,
1872 ref2_alias_set, base2_alias_set,
1873 offset2, max_size2);
1874
1875 return true;
1876 }
1877
1878 /* Return true, if the two memory references REF1 and REF2 may alias. */
1879
1880 static bool
1881 refs_may_alias_p_2 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
1882 {
1883 tree base1, base2;
1884 poly_int64 offset1 = 0, offset2 = 0;
1885 poly_int64 max_size1 = -1, max_size2 = -1;
1886 bool var1_p, var2_p, ind1_p, ind2_p;
1887
1888 gcc_checking_assert ((!ref1->ref
1889 || TREE_CODE (ref1->ref) == SSA_NAME
1890 || DECL_P (ref1->ref)
1891 || TREE_CODE (ref1->ref) == STRING_CST
1892 || handled_component_p (ref1->ref)
1893 || TREE_CODE (ref1->ref) == MEM_REF
1894 || TREE_CODE (ref1->ref) == TARGET_MEM_REF)
1895 && (!ref2->ref
1896 || TREE_CODE (ref2->ref) == SSA_NAME
1897 || DECL_P (ref2->ref)
1898 || TREE_CODE (ref2->ref) == STRING_CST
1899 || handled_component_p (ref2->ref)
1900 || TREE_CODE (ref2->ref) == MEM_REF
1901 || TREE_CODE (ref2->ref) == TARGET_MEM_REF));
1902
1903 /* Decompose the references into their base objects and the access. */
1904 base1 = ao_ref_base (ref1);
1905 offset1 = ref1->offset;
1906 max_size1 = ref1->max_size;
1907 base2 = ao_ref_base (ref2);
1908 offset2 = ref2->offset;
1909 max_size2 = ref2->max_size;
1910
1911 /* We can end up with registers or constants as bases for example from
1912 *D.1663_44 = VIEW_CONVERT_EXPR<struct DB_LSN>(__tmp$B0F64_59);
1913 which is seen as a struct copy. */
1914 if (TREE_CODE (base1) == SSA_NAME
1915 || TREE_CODE (base1) == CONST_DECL
1916 || TREE_CODE (base1) == CONSTRUCTOR
1917 || TREE_CODE (base1) == ADDR_EXPR
1918 || CONSTANT_CLASS_P (base1)
1919 || TREE_CODE (base2) == SSA_NAME
1920 || TREE_CODE (base2) == CONST_DECL
1921 || TREE_CODE (base2) == CONSTRUCTOR
1922 || TREE_CODE (base2) == ADDR_EXPR
1923 || CONSTANT_CLASS_P (base2))
1924 return false;
1925
1926 /* We can end up referring to code via function and label decls.
1927 As we likely do not properly track code aliases conservatively
1928 bail out. */
1929 if (TREE_CODE (base1) == FUNCTION_DECL
1930 || TREE_CODE (base1) == LABEL_DECL
1931 || TREE_CODE (base2) == FUNCTION_DECL
1932 || TREE_CODE (base2) == LABEL_DECL)
1933 return true;
1934
1935 /* Two volatile accesses always conflict. */
1936 if (ref1->volatile_p
1937 && ref2->volatile_p)
1938 return true;
1939
1940 /* Defer to simple offset based disambiguation if we have
1941 references based on two decls. Do this before defering to
1942 TBAA to handle must-alias cases in conformance with the
1943 GCC extension of allowing type-punning through unions. */
1944 var1_p = DECL_P (base1);
1945 var2_p = DECL_P (base2);
1946 if (var1_p && var2_p)
1947 return decl_refs_may_alias_p (ref1->ref, base1, offset1, max_size1,
1948 ref1->size,
1949 ref2->ref, base2, offset2, max_size2,
1950 ref2->size);
1951
1952 /* Handle restrict based accesses.
1953 ??? ao_ref_base strips inner MEM_REF [&decl], recover from that
1954 here. */
1955 tree rbase1 = base1;
1956 tree rbase2 = base2;
1957 if (var1_p)
1958 {
1959 rbase1 = ref1->ref;
1960 if (rbase1)
1961 while (handled_component_p (rbase1))
1962 rbase1 = TREE_OPERAND (rbase1, 0);
1963 }
1964 if (var2_p)
1965 {
1966 rbase2 = ref2->ref;
1967 if (rbase2)
1968 while (handled_component_p (rbase2))
1969 rbase2 = TREE_OPERAND (rbase2, 0);
1970 }
1971 if (rbase1 && rbase2
1972 && (TREE_CODE (base1) == MEM_REF || TREE_CODE (base1) == TARGET_MEM_REF)
1973 && (TREE_CODE (base2) == MEM_REF || TREE_CODE (base2) == TARGET_MEM_REF)
1974 /* If the accesses are in the same restrict clique... */
1975 && MR_DEPENDENCE_CLIQUE (base1) == MR_DEPENDENCE_CLIQUE (base2)
1976 /* But based on different pointers they do not alias. */
1977 && MR_DEPENDENCE_BASE (base1) != MR_DEPENDENCE_BASE (base2))
1978 return false;
1979
1980 ind1_p = (TREE_CODE (base1) == MEM_REF
1981 || TREE_CODE (base1) == TARGET_MEM_REF);
1982 ind2_p = (TREE_CODE (base2) == MEM_REF
1983 || TREE_CODE (base2) == TARGET_MEM_REF);
1984
1985 /* Canonicalize the pointer-vs-decl case. */
1986 if (ind1_p && var2_p)
1987 {
1988 std::swap (offset1, offset2);
1989 std::swap (max_size1, max_size2);
1990 std::swap (base1, base2);
1991 std::swap (ref1, ref2);
1992 var1_p = true;
1993 ind1_p = false;
1994 var2_p = false;
1995 ind2_p = true;
1996 }
1997
1998 /* First defer to TBAA if possible. */
1999 if (tbaa_p
2000 && flag_strict_aliasing
2001 && !alias_sets_conflict_p (ao_ref_alias_set (ref1),
2002 ao_ref_alias_set (ref2)))
2003 return false;
2004
2005 /* If the reference is based on a pointer that points to memory
2006 that may not be written to then the other reference cannot possibly
2007 clobber it. */
2008 if ((TREE_CODE (TREE_OPERAND (base2, 0)) == SSA_NAME
2009 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base2, 0)))
2010 || (ind1_p
2011 && TREE_CODE (TREE_OPERAND (base1, 0)) == SSA_NAME
2012 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base1, 0))))
2013 return false;
2014
2015 /* Dispatch to the pointer-vs-decl or pointer-vs-pointer disambiguators. */
2016 if (var1_p && ind2_p)
2017 return indirect_ref_may_alias_decl_p (ref2->ref, base2,
2018 offset2, max_size2, ref2->size,
2019 ao_ref_alias_set (ref2),
2020 ao_ref_base_alias_set (ref2),
2021 ref1->ref, base1,
2022 offset1, max_size1, ref1->size,
2023 ao_ref_alias_set (ref1),
2024 ao_ref_base_alias_set (ref1),
2025 tbaa_p);
2026 else if (ind1_p && ind2_p)
2027 return indirect_refs_may_alias_p (ref1->ref, base1,
2028 offset1, max_size1, ref1->size,
2029 ao_ref_alias_set (ref1),
2030 ao_ref_base_alias_set (ref1),
2031 ref2->ref, base2,
2032 offset2, max_size2, ref2->size,
2033 ao_ref_alias_set (ref2),
2034 ao_ref_base_alias_set (ref2),
2035 tbaa_p);
2036
2037 gcc_unreachable ();
2038 }
2039
2040 /* Return true, if the two memory references REF1 and REF2 may alias
2041 and update statistics. */
2042
2043 bool
2044 refs_may_alias_p_1 (ao_ref *ref1, ao_ref *ref2, bool tbaa_p)
2045 {
2046 bool res = refs_may_alias_p_2 (ref1, ref2, tbaa_p);
2047 if (res)
2048 ++alias_stats.refs_may_alias_p_may_alias;
2049 else
2050 ++alias_stats.refs_may_alias_p_no_alias;
2051 return res;
2052 }
2053
2054 static bool
2055 refs_may_alias_p (tree ref1, ao_ref *ref2, bool tbaa_p)
2056 {
2057 ao_ref r1;
2058 ao_ref_init (&r1, ref1);
2059 return refs_may_alias_p_1 (&r1, ref2, tbaa_p);
2060 }
2061
2062 bool
2063 refs_may_alias_p (tree ref1, tree ref2, bool tbaa_p)
2064 {
2065 ao_ref r1, r2;
2066 ao_ref_init (&r1, ref1);
2067 ao_ref_init (&r2, ref2);
2068 return refs_may_alias_p_1 (&r1, &r2, tbaa_p);
2069 }
2070
2071 /* Returns true if there is a anti-dependence for the STORE that
2072 executes after the LOAD. */
2073
2074 bool
2075 refs_anti_dependent_p (tree load, tree store)
2076 {
2077 ao_ref r1, r2;
2078 ao_ref_init (&r1, load);
2079 ao_ref_init (&r2, store);
2080 return refs_may_alias_p_1 (&r1, &r2, false);
2081 }
2082
2083 /* Returns true if there is a output dependence for the stores
2084 STORE1 and STORE2. */
2085
2086 bool
2087 refs_output_dependent_p (tree store1, tree store2)
2088 {
2089 ao_ref r1, r2;
2090 ao_ref_init (&r1, store1);
2091 ao_ref_init (&r2, store2);
2092 return refs_may_alias_p_1 (&r1, &r2, false);
2093 }
2094
2095 /* If the call CALL may use the memory reference REF return true,
2096 otherwise return false. */
2097
2098 static bool
2099 ref_maybe_used_by_call_p_1 (gcall *call, ao_ref *ref, bool tbaa_p)
2100 {
2101 tree base, callee;
2102 unsigned i;
2103 int flags = gimple_call_flags (call);
2104
2105 /* Const functions without a static chain do not implicitly use memory. */
2106 if (!gimple_call_chain (call)
2107 && (flags & (ECF_CONST|ECF_NOVOPS)))
2108 goto process_args;
2109
2110 base = ao_ref_base (ref);
2111 if (!base)
2112 return true;
2113
2114 /* A call that is not without side-effects might involve volatile
2115 accesses and thus conflicts with all other volatile accesses. */
2116 if (ref->volatile_p)
2117 return true;
2118
2119 /* If the reference is based on a decl that is not aliased the call
2120 cannot possibly use it. */
2121 if (DECL_P (base)
2122 && !may_be_aliased (base)
2123 /* But local statics can be used through recursion. */
2124 && !is_global_var (base))
2125 goto process_args;
2126
2127 callee = gimple_call_fndecl (call);
2128
2129 /* Handle those builtin functions explicitly that do not act as
2130 escape points. See tree-ssa-structalias.c:find_func_aliases
2131 for the list of builtins we might need to handle here. */
2132 if (callee != NULL_TREE
2133 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2134 switch (DECL_FUNCTION_CODE (callee))
2135 {
2136 /* All the following functions read memory pointed to by
2137 their second argument. strcat/strncat additionally
2138 reads memory pointed to by the first argument. */
2139 case BUILT_IN_STRCAT:
2140 case BUILT_IN_STRNCAT:
2141 {
2142 ao_ref dref;
2143 ao_ref_init_from_ptr_and_size (&dref,
2144 gimple_call_arg (call, 0),
2145 NULL_TREE);
2146 if (refs_may_alias_p_1 (&dref, ref, false))
2147 return true;
2148 }
2149 /* FALLTHRU */
2150 case BUILT_IN_STRCPY:
2151 case BUILT_IN_STRNCPY:
2152 case BUILT_IN_MEMCPY:
2153 case BUILT_IN_MEMMOVE:
2154 case BUILT_IN_MEMPCPY:
2155 case BUILT_IN_STPCPY:
2156 case BUILT_IN_STPNCPY:
2157 case BUILT_IN_TM_MEMCPY:
2158 case BUILT_IN_TM_MEMMOVE:
2159 {
2160 ao_ref dref;
2161 tree size = NULL_TREE;
2162 if (gimple_call_num_args (call) == 3)
2163 size = gimple_call_arg (call, 2);
2164 ao_ref_init_from_ptr_and_size (&dref,
2165 gimple_call_arg (call, 1),
2166 size);
2167 return refs_may_alias_p_1 (&dref, ref, false);
2168 }
2169 case BUILT_IN_STRCAT_CHK:
2170 case BUILT_IN_STRNCAT_CHK:
2171 {
2172 ao_ref dref;
2173 ao_ref_init_from_ptr_and_size (&dref,
2174 gimple_call_arg (call, 0),
2175 NULL_TREE);
2176 if (refs_may_alias_p_1 (&dref, ref, false))
2177 return true;
2178 }
2179 /* FALLTHRU */
2180 case BUILT_IN_STRCPY_CHK:
2181 case BUILT_IN_STRNCPY_CHK:
2182 case BUILT_IN_MEMCPY_CHK:
2183 case BUILT_IN_MEMMOVE_CHK:
2184 case BUILT_IN_MEMPCPY_CHK:
2185 case BUILT_IN_STPCPY_CHK:
2186 case BUILT_IN_STPNCPY_CHK:
2187 {
2188 ao_ref dref;
2189 tree size = NULL_TREE;
2190 if (gimple_call_num_args (call) == 4)
2191 size = gimple_call_arg (call, 2);
2192 ao_ref_init_from_ptr_and_size (&dref,
2193 gimple_call_arg (call, 1),
2194 size);
2195 return refs_may_alias_p_1 (&dref, ref, false);
2196 }
2197 case BUILT_IN_BCOPY:
2198 {
2199 ao_ref dref;
2200 tree size = gimple_call_arg (call, 2);
2201 ao_ref_init_from_ptr_and_size (&dref,
2202 gimple_call_arg (call, 0),
2203 size);
2204 return refs_may_alias_p_1 (&dref, ref, false);
2205 }
2206
2207 /* The following functions read memory pointed to by their
2208 first argument. */
2209 CASE_BUILT_IN_TM_LOAD (1):
2210 CASE_BUILT_IN_TM_LOAD (2):
2211 CASE_BUILT_IN_TM_LOAD (4):
2212 CASE_BUILT_IN_TM_LOAD (8):
2213 CASE_BUILT_IN_TM_LOAD (FLOAT):
2214 CASE_BUILT_IN_TM_LOAD (DOUBLE):
2215 CASE_BUILT_IN_TM_LOAD (LDOUBLE):
2216 CASE_BUILT_IN_TM_LOAD (M64):
2217 CASE_BUILT_IN_TM_LOAD (M128):
2218 CASE_BUILT_IN_TM_LOAD (M256):
2219 case BUILT_IN_TM_LOG:
2220 case BUILT_IN_TM_LOG_1:
2221 case BUILT_IN_TM_LOG_2:
2222 case BUILT_IN_TM_LOG_4:
2223 case BUILT_IN_TM_LOG_8:
2224 case BUILT_IN_TM_LOG_FLOAT:
2225 case BUILT_IN_TM_LOG_DOUBLE:
2226 case BUILT_IN_TM_LOG_LDOUBLE:
2227 case BUILT_IN_TM_LOG_M64:
2228 case BUILT_IN_TM_LOG_M128:
2229 case BUILT_IN_TM_LOG_M256:
2230 return ptr_deref_may_alias_ref_p_1 (gimple_call_arg (call, 0), ref);
2231
2232 /* These read memory pointed to by the first argument. */
2233 case BUILT_IN_STRDUP:
2234 case BUILT_IN_STRNDUP:
2235 case BUILT_IN_REALLOC:
2236 {
2237 ao_ref dref;
2238 tree size = NULL_TREE;
2239 if (gimple_call_num_args (call) == 2)
2240 size = gimple_call_arg (call, 1);
2241 ao_ref_init_from_ptr_and_size (&dref,
2242 gimple_call_arg (call, 0),
2243 size);
2244 return refs_may_alias_p_1 (&dref, ref, false);
2245 }
2246 /* These read memory pointed to by the first argument. */
2247 case BUILT_IN_INDEX:
2248 case BUILT_IN_STRCHR:
2249 case BUILT_IN_STRRCHR:
2250 {
2251 ao_ref dref;
2252 ao_ref_init_from_ptr_and_size (&dref,
2253 gimple_call_arg (call, 0),
2254 NULL_TREE);
2255 return refs_may_alias_p_1 (&dref, ref, false);
2256 }
2257 /* These read memory pointed to by the first argument with size
2258 in the third argument. */
2259 case BUILT_IN_MEMCHR:
2260 {
2261 ao_ref dref;
2262 ao_ref_init_from_ptr_and_size (&dref,
2263 gimple_call_arg (call, 0),
2264 gimple_call_arg (call, 2));
2265 return refs_may_alias_p_1 (&dref, ref, false);
2266 }
2267 /* These read memory pointed to by the first and second arguments. */
2268 case BUILT_IN_STRSTR:
2269 case BUILT_IN_STRPBRK:
2270 {
2271 ao_ref dref;
2272 ao_ref_init_from_ptr_and_size (&dref,
2273 gimple_call_arg (call, 0),
2274 NULL_TREE);
2275 if (refs_may_alias_p_1 (&dref, ref, false))
2276 return true;
2277 ao_ref_init_from_ptr_and_size (&dref,
2278 gimple_call_arg (call, 1),
2279 NULL_TREE);
2280 return refs_may_alias_p_1 (&dref, ref, false);
2281 }
2282
2283 /* The following builtins do not read from memory. */
2284 case BUILT_IN_FREE:
2285 case BUILT_IN_MALLOC:
2286 case BUILT_IN_POSIX_MEMALIGN:
2287 case BUILT_IN_ALIGNED_ALLOC:
2288 case BUILT_IN_CALLOC:
2289 CASE_BUILT_IN_ALLOCA:
2290 case BUILT_IN_STACK_SAVE:
2291 case BUILT_IN_STACK_RESTORE:
2292 case BUILT_IN_MEMSET:
2293 case BUILT_IN_TM_MEMSET:
2294 case BUILT_IN_MEMSET_CHK:
2295 case BUILT_IN_FREXP:
2296 case BUILT_IN_FREXPF:
2297 case BUILT_IN_FREXPL:
2298 case BUILT_IN_GAMMA_R:
2299 case BUILT_IN_GAMMAF_R:
2300 case BUILT_IN_GAMMAL_R:
2301 case BUILT_IN_LGAMMA_R:
2302 case BUILT_IN_LGAMMAF_R:
2303 case BUILT_IN_LGAMMAL_R:
2304 case BUILT_IN_MODF:
2305 case BUILT_IN_MODFF:
2306 case BUILT_IN_MODFL:
2307 case BUILT_IN_REMQUO:
2308 case BUILT_IN_REMQUOF:
2309 case BUILT_IN_REMQUOL:
2310 case BUILT_IN_SINCOS:
2311 case BUILT_IN_SINCOSF:
2312 case BUILT_IN_SINCOSL:
2313 case BUILT_IN_ASSUME_ALIGNED:
2314 case BUILT_IN_VA_END:
2315 return false;
2316 /* __sync_* builtins and some OpenMP builtins act as threading
2317 barriers. */
2318 #undef DEF_SYNC_BUILTIN
2319 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2320 #include "sync-builtins.def"
2321 #undef DEF_SYNC_BUILTIN
2322 case BUILT_IN_GOMP_ATOMIC_START:
2323 case BUILT_IN_GOMP_ATOMIC_END:
2324 case BUILT_IN_GOMP_BARRIER:
2325 case BUILT_IN_GOMP_BARRIER_CANCEL:
2326 case BUILT_IN_GOMP_TASKWAIT:
2327 case BUILT_IN_GOMP_TASKGROUP_END:
2328 case BUILT_IN_GOMP_CRITICAL_START:
2329 case BUILT_IN_GOMP_CRITICAL_END:
2330 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2331 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2332 case BUILT_IN_GOMP_LOOP_END:
2333 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2334 case BUILT_IN_GOMP_ORDERED_START:
2335 case BUILT_IN_GOMP_ORDERED_END:
2336 case BUILT_IN_GOMP_SECTIONS_END:
2337 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2338 case BUILT_IN_GOMP_SINGLE_COPY_START:
2339 case BUILT_IN_GOMP_SINGLE_COPY_END:
2340 return true;
2341
2342 default:
2343 /* Fallthru to general call handling. */;
2344 }
2345
2346 /* Check if base is a global static variable that is not read
2347 by the function. */
2348 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2349 {
2350 struct cgraph_node *node = cgraph_node::get (callee);
2351 bitmap not_read;
2352
2353 /* FIXME: Callee can be an OMP builtin that does not have a call graph
2354 node yet. We should enforce that there are nodes for all decls in the
2355 IL and remove this check instead. */
2356 if (node
2357 && (not_read = ipa_reference_get_not_read_global (node))
2358 && bitmap_bit_p (not_read, ipa_reference_var_uid (base)))
2359 goto process_args;
2360 }
2361
2362 /* Check if the base variable is call-used. */
2363 if (DECL_P (base))
2364 {
2365 if (pt_solution_includes (gimple_call_use_set (call), base))
2366 return true;
2367 }
2368 else if ((TREE_CODE (base) == MEM_REF
2369 || TREE_CODE (base) == TARGET_MEM_REF)
2370 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2371 {
2372 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2373 if (!pi)
2374 return true;
2375
2376 if (pt_solutions_intersect (gimple_call_use_set (call), &pi->pt))
2377 return true;
2378 }
2379 else
2380 return true;
2381
2382 /* Inspect call arguments for passed-by-value aliases. */
2383 process_args:
2384 for (i = 0; i < gimple_call_num_args (call); ++i)
2385 {
2386 tree op = gimple_call_arg (call, i);
2387 int flags = gimple_call_arg_flags (call, i);
2388
2389 if (flags & EAF_UNUSED)
2390 continue;
2391
2392 if (TREE_CODE (op) == WITH_SIZE_EXPR)
2393 op = TREE_OPERAND (op, 0);
2394
2395 if (TREE_CODE (op) != SSA_NAME
2396 && !is_gimple_min_invariant (op))
2397 {
2398 ao_ref r;
2399 ao_ref_init (&r, op);
2400 if (refs_may_alias_p_1 (&r, ref, tbaa_p))
2401 return true;
2402 }
2403 }
2404
2405 return false;
2406 }
2407
2408 static bool
2409 ref_maybe_used_by_call_p (gcall *call, ao_ref *ref, bool tbaa_p)
2410 {
2411 bool res;
2412 res = ref_maybe_used_by_call_p_1 (call, ref, tbaa_p);
2413 if (res)
2414 ++alias_stats.ref_maybe_used_by_call_p_may_alias;
2415 else
2416 ++alias_stats.ref_maybe_used_by_call_p_no_alias;
2417 return res;
2418 }
2419
2420
2421 /* If the statement STMT may use the memory reference REF return
2422 true, otherwise return false. */
2423
2424 bool
2425 ref_maybe_used_by_stmt_p (gimple *stmt, ao_ref *ref, bool tbaa_p)
2426 {
2427 if (is_gimple_assign (stmt))
2428 {
2429 tree rhs;
2430
2431 /* All memory assign statements are single. */
2432 if (!gimple_assign_single_p (stmt))
2433 return false;
2434
2435 rhs = gimple_assign_rhs1 (stmt);
2436 if (is_gimple_reg (rhs)
2437 || is_gimple_min_invariant (rhs)
2438 || gimple_assign_rhs_code (stmt) == CONSTRUCTOR)
2439 return false;
2440
2441 return refs_may_alias_p (rhs, ref, tbaa_p);
2442 }
2443 else if (is_gimple_call (stmt))
2444 return ref_maybe_used_by_call_p (as_a <gcall *> (stmt), ref, tbaa_p);
2445 else if (greturn *return_stmt = dyn_cast <greturn *> (stmt))
2446 {
2447 tree retval = gimple_return_retval (return_stmt);
2448 if (retval
2449 && TREE_CODE (retval) != SSA_NAME
2450 && !is_gimple_min_invariant (retval)
2451 && refs_may_alias_p (retval, ref, tbaa_p))
2452 return true;
2453 /* If ref escapes the function then the return acts as a use. */
2454 tree base = ao_ref_base (ref);
2455 if (!base)
2456 ;
2457 else if (DECL_P (base))
2458 return is_global_var (base);
2459 else if (TREE_CODE (base) == MEM_REF
2460 || TREE_CODE (base) == TARGET_MEM_REF)
2461 return ptr_deref_may_alias_global_p (TREE_OPERAND (base, 0));
2462 return false;
2463 }
2464
2465 return true;
2466 }
2467
2468 bool
2469 ref_maybe_used_by_stmt_p (gimple *stmt, tree ref, bool tbaa_p)
2470 {
2471 ao_ref r;
2472 ao_ref_init (&r, ref);
2473 return ref_maybe_used_by_stmt_p (stmt, &r, tbaa_p);
2474 }
2475
2476 /* If the call in statement CALL may clobber the memory reference REF
2477 return true, otherwise return false. */
2478
2479 bool
2480 call_may_clobber_ref_p_1 (gcall *call, ao_ref *ref)
2481 {
2482 tree base;
2483 tree callee;
2484
2485 /* If the call is pure or const it cannot clobber anything. */
2486 if (gimple_call_flags (call)
2487 & (ECF_PURE|ECF_CONST|ECF_LOOPING_CONST_OR_PURE|ECF_NOVOPS))
2488 return false;
2489 if (gimple_call_internal_p (call))
2490 switch (gimple_call_internal_fn (call))
2491 {
2492 /* Treat these internal calls like ECF_PURE for aliasing,
2493 they don't write to any memory the program should care about.
2494 They have important other side-effects, and read memory,
2495 so can't be ECF_NOVOPS. */
2496 case IFN_UBSAN_NULL:
2497 case IFN_UBSAN_BOUNDS:
2498 case IFN_UBSAN_VPTR:
2499 case IFN_UBSAN_OBJECT_SIZE:
2500 case IFN_UBSAN_PTR:
2501 case IFN_ASAN_CHECK:
2502 return false;
2503 default:
2504 break;
2505 }
2506
2507 base = ao_ref_base (ref);
2508 if (!base)
2509 return true;
2510
2511 if (TREE_CODE (base) == SSA_NAME
2512 || CONSTANT_CLASS_P (base))
2513 return false;
2514
2515 /* A call that is not without side-effects might involve volatile
2516 accesses and thus conflicts with all other volatile accesses. */
2517 if (ref->volatile_p)
2518 return true;
2519
2520 /* If the reference is based on a decl that is not aliased the call
2521 cannot possibly clobber it. */
2522 if (DECL_P (base)
2523 && !may_be_aliased (base)
2524 /* But local non-readonly statics can be modified through recursion
2525 or the call may implement a threading barrier which we must
2526 treat as may-def. */
2527 && (TREE_READONLY (base)
2528 || !is_global_var (base)))
2529 return false;
2530
2531 /* If the reference is based on a pointer that points to memory
2532 that may not be written to then the call cannot possibly clobber it. */
2533 if ((TREE_CODE (base) == MEM_REF
2534 || TREE_CODE (base) == TARGET_MEM_REF)
2535 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME
2536 && SSA_NAME_POINTS_TO_READONLY_MEMORY (TREE_OPERAND (base, 0)))
2537 return false;
2538
2539 callee = gimple_call_fndecl (call);
2540
2541 /* Handle those builtin functions explicitly that do not act as
2542 escape points. See tree-ssa-structalias.c:find_func_aliases
2543 for the list of builtins we might need to handle here. */
2544 if (callee != NULL_TREE
2545 && gimple_call_builtin_p (call, BUILT_IN_NORMAL))
2546 switch (DECL_FUNCTION_CODE (callee))
2547 {
2548 /* All the following functions clobber memory pointed to by
2549 their first argument. */
2550 case BUILT_IN_STRCPY:
2551 case BUILT_IN_STRNCPY:
2552 case BUILT_IN_MEMCPY:
2553 case BUILT_IN_MEMMOVE:
2554 case BUILT_IN_MEMPCPY:
2555 case BUILT_IN_STPCPY:
2556 case BUILT_IN_STPNCPY:
2557 case BUILT_IN_STRCAT:
2558 case BUILT_IN_STRNCAT:
2559 case BUILT_IN_MEMSET:
2560 case BUILT_IN_TM_MEMSET:
2561 CASE_BUILT_IN_TM_STORE (1):
2562 CASE_BUILT_IN_TM_STORE (2):
2563 CASE_BUILT_IN_TM_STORE (4):
2564 CASE_BUILT_IN_TM_STORE (8):
2565 CASE_BUILT_IN_TM_STORE (FLOAT):
2566 CASE_BUILT_IN_TM_STORE (DOUBLE):
2567 CASE_BUILT_IN_TM_STORE (LDOUBLE):
2568 CASE_BUILT_IN_TM_STORE (M64):
2569 CASE_BUILT_IN_TM_STORE (M128):
2570 CASE_BUILT_IN_TM_STORE (M256):
2571 case BUILT_IN_TM_MEMCPY:
2572 case BUILT_IN_TM_MEMMOVE:
2573 {
2574 ao_ref dref;
2575 tree size = NULL_TREE;
2576 /* Don't pass in size for strncat, as the maximum size
2577 is strlen (dest) + n + 1 instead of n, resp.
2578 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2579 known. */
2580 if (gimple_call_num_args (call) == 3
2581 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT)
2582 size = gimple_call_arg (call, 2);
2583 ao_ref_init_from_ptr_and_size (&dref,
2584 gimple_call_arg (call, 0),
2585 size);
2586 return refs_may_alias_p_1 (&dref, ref, false);
2587 }
2588 case BUILT_IN_STRCPY_CHK:
2589 case BUILT_IN_STRNCPY_CHK:
2590 case BUILT_IN_MEMCPY_CHK:
2591 case BUILT_IN_MEMMOVE_CHK:
2592 case BUILT_IN_MEMPCPY_CHK:
2593 case BUILT_IN_STPCPY_CHK:
2594 case BUILT_IN_STPNCPY_CHK:
2595 case BUILT_IN_STRCAT_CHK:
2596 case BUILT_IN_STRNCAT_CHK:
2597 case BUILT_IN_MEMSET_CHK:
2598 {
2599 ao_ref dref;
2600 tree size = NULL_TREE;
2601 /* Don't pass in size for __strncat_chk, as the maximum size
2602 is strlen (dest) + n + 1 instead of n, resp.
2603 n + 1 at dest + strlen (dest), but strlen (dest) isn't
2604 known. */
2605 if (gimple_call_num_args (call) == 4
2606 && DECL_FUNCTION_CODE (callee) != BUILT_IN_STRNCAT_CHK)
2607 size = gimple_call_arg (call, 2);
2608 ao_ref_init_from_ptr_and_size (&dref,
2609 gimple_call_arg (call, 0),
2610 size);
2611 return refs_may_alias_p_1 (&dref, ref, false);
2612 }
2613 case BUILT_IN_BCOPY:
2614 {
2615 ao_ref dref;
2616 tree size = gimple_call_arg (call, 2);
2617 ao_ref_init_from_ptr_and_size (&dref,
2618 gimple_call_arg (call, 1),
2619 size);
2620 return refs_may_alias_p_1 (&dref, ref, false);
2621 }
2622 /* Allocating memory does not have any side-effects apart from
2623 being the definition point for the pointer. */
2624 case BUILT_IN_MALLOC:
2625 case BUILT_IN_ALIGNED_ALLOC:
2626 case BUILT_IN_CALLOC:
2627 case BUILT_IN_STRDUP:
2628 case BUILT_IN_STRNDUP:
2629 /* Unix98 specifies that errno is set on allocation failure. */
2630 if (flag_errno_math
2631 && targetm.ref_may_alias_errno (ref))
2632 return true;
2633 return false;
2634 case BUILT_IN_STACK_SAVE:
2635 CASE_BUILT_IN_ALLOCA:
2636 case BUILT_IN_ASSUME_ALIGNED:
2637 return false;
2638 /* But posix_memalign stores a pointer into the memory pointed to
2639 by its first argument. */
2640 case BUILT_IN_POSIX_MEMALIGN:
2641 {
2642 tree ptrptr = gimple_call_arg (call, 0);
2643 ao_ref dref;
2644 ao_ref_init_from_ptr_and_size (&dref, ptrptr,
2645 TYPE_SIZE_UNIT (ptr_type_node));
2646 return (refs_may_alias_p_1 (&dref, ref, false)
2647 || (flag_errno_math
2648 && targetm.ref_may_alias_errno (ref)));
2649 }
2650 /* Freeing memory kills the pointed-to memory. More importantly
2651 the call has to serve as a barrier for moving loads and stores
2652 across it. */
2653 case BUILT_IN_FREE:
2654 case BUILT_IN_VA_END:
2655 {
2656 tree ptr = gimple_call_arg (call, 0);
2657 return ptr_deref_may_alias_ref_p_1 (ptr, ref);
2658 }
2659 /* Realloc serves both as allocation point and deallocation point. */
2660 case BUILT_IN_REALLOC:
2661 {
2662 tree ptr = gimple_call_arg (call, 0);
2663 /* Unix98 specifies that errno is set on allocation failure. */
2664 return ((flag_errno_math
2665 && targetm.ref_may_alias_errno (ref))
2666 || ptr_deref_may_alias_ref_p_1 (ptr, ref));
2667 }
2668 case BUILT_IN_GAMMA_R:
2669 case BUILT_IN_GAMMAF_R:
2670 case BUILT_IN_GAMMAL_R:
2671 case BUILT_IN_LGAMMA_R:
2672 case BUILT_IN_LGAMMAF_R:
2673 case BUILT_IN_LGAMMAL_R:
2674 {
2675 tree out = gimple_call_arg (call, 1);
2676 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2677 return true;
2678 if (flag_errno_math)
2679 break;
2680 return false;
2681 }
2682 case BUILT_IN_FREXP:
2683 case BUILT_IN_FREXPF:
2684 case BUILT_IN_FREXPL:
2685 case BUILT_IN_MODF:
2686 case BUILT_IN_MODFF:
2687 case BUILT_IN_MODFL:
2688 {
2689 tree out = gimple_call_arg (call, 1);
2690 return ptr_deref_may_alias_ref_p_1 (out, ref);
2691 }
2692 case BUILT_IN_REMQUO:
2693 case BUILT_IN_REMQUOF:
2694 case BUILT_IN_REMQUOL:
2695 {
2696 tree out = gimple_call_arg (call, 2);
2697 if (ptr_deref_may_alias_ref_p_1 (out, ref))
2698 return true;
2699 if (flag_errno_math)
2700 break;
2701 return false;
2702 }
2703 case BUILT_IN_SINCOS:
2704 case BUILT_IN_SINCOSF:
2705 case BUILT_IN_SINCOSL:
2706 {
2707 tree sin = gimple_call_arg (call, 1);
2708 tree cos = gimple_call_arg (call, 2);
2709 return (ptr_deref_may_alias_ref_p_1 (sin, ref)
2710 || ptr_deref_may_alias_ref_p_1 (cos, ref));
2711 }
2712 /* __sync_* builtins and some OpenMP builtins act as threading
2713 barriers. */
2714 #undef DEF_SYNC_BUILTIN
2715 #define DEF_SYNC_BUILTIN(ENUM, NAME, TYPE, ATTRS) case ENUM:
2716 #include "sync-builtins.def"
2717 #undef DEF_SYNC_BUILTIN
2718 case BUILT_IN_GOMP_ATOMIC_START:
2719 case BUILT_IN_GOMP_ATOMIC_END:
2720 case BUILT_IN_GOMP_BARRIER:
2721 case BUILT_IN_GOMP_BARRIER_CANCEL:
2722 case BUILT_IN_GOMP_TASKWAIT:
2723 case BUILT_IN_GOMP_TASKGROUP_END:
2724 case BUILT_IN_GOMP_CRITICAL_START:
2725 case BUILT_IN_GOMP_CRITICAL_END:
2726 case BUILT_IN_GOMP_CRITICAL_NAME_START:
2727 case BUILT_IN_GOMP_CRITICAL_NAME_END:
2728 case BUILT_IN_GOMP_LOOP_END:
2729 case BUILT_IN_GOMP_LOOP_END_CANCEL:
2730 case BUILT_IN_GOMP_ORDERED_START:
2731 case BUILT_IN_GOMP_ORDERED_END:
2732 case BUILT_IN_GOMP_SECTIONS_END:
2733 case BUILT_IN_GOMP_SECTIONS_END_CANCEL:
2734 case BUILT_IN_GOMP_SINGLE_COPY_START:
2735 case BUILT_IN_GOMP_SINGLE_COPY_END:
2736 return true;
2737 default:
2738 /* Fallthru to general call handling. */;
2739 }
2740
2741 /* Check if base is a global static variable that is not written
2742 by the function. */
2743 if (callee != NULL_TREE && VAR_P (base) && TREE_STATIC (base))
2744 {
2745 struct cgraph_node *node = cgraph_node::get (callee);
2746 bitmap not_written;
2747
2748 if (node
2749 && (not_written = ipa_reference_get_not_written_global (node))
2750 && bitmap_bit_p (not_written, ipa_reference_var_uid (base)))
2751 return false;
2752 }
2753
2754 /* Check if the base variable is call-clobbered. */
2755 if (DECL_P (base))
2756 return pt_solution_includes (gimple_call_clobber_set (call), base);
2757 else if ((TREE_CODE (base) == MEM_REF
2758 || TREE_CODE (base) == TARGET_MEM_REF)
2759 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
2760 {
2761 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (TREE_OPERAND (base, 0));
2762 if (!pi)
2763 return true;
2764
2765 return pt_solutions_intersect (gimple_call_clobber_set (call), &pi->pt);
2766 }
2767
2768 return true;
2769 }
2770
2771 /* If the call in statement CALL may clobber the memory reference REF
2772 return true, otherwise return false. */
2773
2774 bool
2775 call_may_clobber_ref_p (gcall *call, tree ref)
2776 {
2777 bool res;
2778 ao_ref r;
2779 ao_ref_init (&r, ref);
2780 res = call_may_clobber_ref_p_1 (call, &r);
2781 if (res)
2782 ++alias_stats.call_may_clobber_ref_p_may_alias;
2783 else
2784 ++alias_stats.call_may_clobber_ref_p_no_alias;
2785 return res;
2786 }
2787
2788
2789 /* If the statement STMT may clobber the memory reference REF return true,
2790 otherwise return false. */
2791
2792 bool
2793 stmt_may_clobber_ref_p_1 (gimple *stmt, ao_ref *ref, bool tbaa_p)
2794 {
2795 if (is_gimple_call (stmt))
2796 {
2797 tree lhs = gimple_call_lhs (stmt);
2798 if (lhs
2799 && TREE_CODE (lhs) != SSA_NAME)
2800 {
2801 ao_ref r;
2802 ao_ref_init (&r, lhs);
2803 if (refs_may_alias_p_1 (ref, &r, tbaa_p))
2804 return true;
2805 }
2806
2807 return call_may_clobber_ref_p_1 (as_a <gcall *> (stmt), ref);
2808 }
2809 else if (gimple_assign_single_p (stmt))
2810 {
2811 tree lhs = gimple_assign_lhs (stmt);
2812 if (TREE_CODE (lhs) != SSA_NAME)
2813 {
2814 ao_ref r;
2815 ao_ref_init (&r, lhs);
2816 return refs_may_alias_p_1 (ref, &r, tbaa_p);
2817 }
2818 }
2819 else if (gimple_code (stmt) == GIMPLE_ASM)
2820 return true;
2821
2822 return false;
2823 }
2824
2825 bool
2826 stmt_may_clobber_ref_p (gimple *stmt, tree ref, bool tbaa_p)
2827 {
2828 ao_ref r;
2829 ao_ref_init (&r, ref);
2830 return stmt_may_clobber_ref_p_1 (stmt, &r, tbaa_p);
2831 }
2832
2833 /* Return true if store1 and store2 described by corresponding tuples
2834 <BASE, OFFSET, SIZE, MAX_SIZE> have the same size and store to the same
2835 address. */
2836
2837 static bool
2838 same_addr_size_stores_p (tree base1, poly_int64 offset1, poly_int64 size1,
2839 poly_int64 max_size1,
2840 tree base2, poly_int64 offset2, poly_int64 size2,
2841 poly_int64 max_size2)
2842 {
2843 /* Offsets need to be 0. */
2844 if (maybe_ne (offset1, 0)
2845 || maybe_ne (offset2, 0))
2846 return false;
2847
2848 bool base1_obj_p = SSA_VAR_P (base1);
2849 bool base2_obj_p = SSA_VAR_P (base2);
2850
2851 /* We need one object. */
2852 if (base1_obj_p == base2_obj_p)
2853 return false;
2854 tree obj = base1_obj_p ? base1 : base2;
2855
2856 /* And we need one MEM_REF. */
2857 bool base1_memref_p = TREE_CODE (base1) == MEM_REF;
2858 bool base2_memref_p = TREE_CODE (base2) == MEM_REF;
2859 if (base1_memref_p == base2_memref_p)
2860 return false;
2861 tree memref = base1_memref_p ? base1 : base2;
2862
2863 /* Sizes need to be valid. */
2864 if (!known_size_p (max_size1)
2865 || !known_size_p (max_size2)
2866 || !known_size_p (size1)
2867 || !known_size_p (size2))
2868 return false;
2869
2870 /* Max_size needs to match size. */
2871 if (maybe_ne (max_size1, size1)
2872 || maybe_ne (max_size2, size2))
2873 return false;
2874
2875 /* Sizes need to match. */
2876 if (maybe_ne (size1, size2))
2877 return false;
2878
2879
2880 /* Check that memref is a store to pointer with singleton points-to info. */
2881 if (!integer_zerop (TREE_OPERAND (memref, 1)))
2882 return false;
2883 tree ptr = TREE_OPERAND (memref, 0);
2884 if (TREE_CODE (ptr) != SSA_NAME)
2885 return false;
2886 struct ptr_info_def *pi = SSA_NAME_PTR_INFO (ptr);
2887 unsigned int pt_uid;
2888 if (pi == NULL
2889 || !pt_solution_singleton_or_null_p (&pi->pt, &pt_uid))
2890 return false;
2891
2892 /* Be conservative with non-call exceptions when the address might
2893 be NULL. */
2894 if (cfun->can_throw_non_call_exceptions && pi->pt.null)
2895 return false;
2896
2897 /* Check that ptr points relative to obj. */
2898 unsigned int obj_uid = DECL_PT_UID (obj);
2899 if (obj_uid != pt_uid)
2900 return false;
2901
2902 /* Check that the object size is the same as the store size. That ensures us
2903 that ptr points to the start of obj. */
2904 return (DECL_SIZE (obj)
2905 && poly_int_tree_p (DECL_SIZE (obj))
2906 && known_eq (wi::to_poly_offset (DECL_SIZE (obj)), size1));
2907 }
2908
2909 /* If STMT kills the memory reference REF return true, otherwise
2910 return false. */
2911
2912 bool
2913 stmt_kills_ref_p (gimple *stmt, ao_ref *ref)
2914 {
2915 if (!ao_ref_base (ref))
2916 return false;
2917
2918 if (gimple_has_lhs (stmt)
2919 && TREE_CODE (gimple_get_lhs (stmt)) != SSA_NAME
2920 /* The assignment is not necessarily carried out if it can throw
2921 and we can catch it in the current function where we could inspect
2922 the previous value.
2923 ??? We only need to care about the RHS throwing. For aggregate
2924 assignments or similar calls and non-call exceptions the LHS
2925 might throw as well. */
2926 && !stmt_can_throw_internal (cfun, stmt))
2927 {
2928 tree lhs = gimple_get_lhs (stmt);
2929 /* If LHS is literally a base of the access we are done. */
2930 if (ref->ref)
2931 {
2932 tree base = ref->ref;
2933 tree innermost_dropped_array_ref = NULL_TREE;
2934 if (handled_component_p (base))
2935 {
2936 tree saved_lhs0 = NULL_TREE;
2937 if (handled_component_p (lhs))
2938 {
2939 saved_lhs0 = TREE_OPERAND (lhs, 0);
2940 TREE_OPERAND (lhs, 0) = integer_zero_node;
2941 }
2942 do
2943 {
2944 /* Just compare the outermost handled component, if
2945 they are equal we have found a possible common
2946 base. */
2947 tree saved_base0 = TREE_OPERAND (base, 0);
2948 TREE_OPERAND (base, 0) = integer_zero_node;
2949 bool res = operand_equal_p (lhs, base, 0);
2950 TREE_OPERAND (base, 0) = saved_base0;
2951 if (res)
2952 break;
2953 /* Remember if we drop an array-ref that we need to
2954 double-check not being at struct end. */
2955 if (TREE_CODE (base) == ARRAY_REF
2956 || TREE_CODE (base) == ARRAY_RANGE_REF)
2957 innermost_dropped_array_ref = base;
2958 /* Otherwise drop handled components of the access. */
2959 base = saved_base0;
2960 }
2961 while (handled_component_p (base));
2962 if (saved_lhs0)
2963 TREE_OPERAND (lhs, 0) = saved_lhs0;
2964 }
2965 /* Finally check if the lhs has the same address and size as the
2966 base candidate of the access. Watch out if we have dropped
2967 an array-ref that was at struct end, this means ref->ref may
2968 be outside of the TYPE_SIZE of its base. */
2969 if ((! innermost_dropped_array_ref
2970 || ! array_at_struct_end_p (innermost_dropped_array_ref))
2971 && (lhs == base
2972 || (((TYPE_SIZE (TREE_TYPE (lhs))
2973 == TYPE_SIZE (TREE_TYPE (base)))
2974 || (TYPE_SIZE (TREE_TYPE (lhs))
2975 && TYPE_SIZE (TREE_TYPE (base))
2976 && operand_equal_p (TYPE_SIZE (TREE_TYPE (lhs)),
2977 TYPE_SIZE (TREE_TYPE (base)),
2978 0)))
2979 && operand_equal_p (lhs, base,
2980 OEP_ADDRESS_OF
2981 | OEP_MATCH_SIDE_EFFECTS))))
2982 return true;
2983 }
2984
2985 /* Now look for non-literal equal bases with the restriction of
2986 handling constant offset and size. */
2987 /* For a must-alias check we need to be able to constrain
2988 the access properly. */
2989 if (!ref->max_size_known_p ())
2990 return false;
2991 poly_int64 size, offset, max_size, ref_offset = ref->offset;
2992 bool reverse;
2993 tree base = get_ref_base_and_extent (lhs, &offset, &size, &max_size,
2994 &reverse);
2995 /* We can get MEM[symbol: sZ, index: D.8862_1] here,
2996 so base == ref->base does not always hold. */
2997 if (base != ref->base)
2998 {
2999 /* Try using points-to info. */
3000 if (same_addr_size_stores_p (base, offset, size, max_size, ref->base,
3001 ref->offset, ref->size, ref->max_size))
3002 return true;
3003
3004 /* If both base and ref->base are MEM_REFs, only compare the
3005 first operand, and if the second operand isn't equal constant,
3006 try to add the offsets into offset and ref_offset. */
3007 if (TREE_CODE (base) == MEM_REF && TREE_CODE (ref->base) == MEM_REF
3008 && TREE_OPERAND (base, 0) == TREE_OPERAND (ref->base, 0))
3009 {
3010 if (!tree_int_cst_equal (TREE_OPERAND (base, 1),
3011 TREE_OPERAND (ref->base, 1)))
3012 {
3013 poly_offset_int off1 = mem_ref_offset (base);
3014 off1 <<= LOG2_BITS_PER_UNIT;
3015 off1 += offset;
3016 poly_offset_int off2 = mem_ref_offset (ref->base);
3017 off2 <<= LOG2_BITS_PER_UNIT;
3018 off2 += ref_offset;
3019 if (!off1.to_shwi (&offset) || !off2.to_shwi (&ref_offset))
3020 size = -1;
3021 }
3022 }
3023 else
3024 size = -1;
3025 }
3026 /* For a must-alias check we need to be able to constrain
3027 the access properly. */
3028 if (known_eq (size, max_size)
3029 && known_subrange_p (ref_offset, ref->max_size, offset, size))
3030 return true;
3031 }
3032
3033 if (is_gimple_call (stmt))
3034 {
3035 tree callee = gimple_call_fndecl (stmt);
3036 if (callee != NULL_TREE
3037 && gimple_call_builtin_p (stmt, BUILT_IN_NORMAL))
3038 switch (DECL_FUNCTION_CODE (callee))
3039 {
3040 case BUILT_IN_FREE:
3041 {
3042 tree ptr = gimple_call_arg (stmt, 0);
3043 tree base = ao_ref_base (ref);
3044 if (base && TREE_CODE (base) == MEM_REF
3045 && TREE_OPERAND (base, 0) == ptr)
3046 return true;
3047 break;
3048 }
3049
3050 case BUILT_IN_MEMCPY:
3051 case BUILT_IN_MEMPCPY:
3052 case BUILT_IN_MEMMOVE:
3053 case BUILT_IN_MEMSET:
3054 case BUILT_IN_MEMCPY_CHK:
3055 case BUILT_IN_MEMPCPY_CHK:
3056 case BUILT_IN_MEMMOVE_CHK:
3057 case BUILT_IN_MEMSET_CHK:
3058 case BUILT_IN_STRNCPY:
3059 case BUILT_IN_STPNCPY:
3060 case BUILT_IN_CALLOC:
3061 {
3062 /* For a must-alias check we need to be able to constrain
3063 the access properly. */
3064 if (!ref->max_size_known_p ())
3065 return false;
3066 tree dest;
3067 tree len;
3068
3069 /* In execution order a calloc call will never kill
3070 anything. However, DSE will (ab)use this interface
3071 to ask if a calloc call writes the same memory locations
3072 as a later assignment, memset, etc. So handle calloc
3073 in the expected way. */
3074 if (DECL_FUNCTION_CODE (callee) == BUILT_IN_CALLOC)
3075 {
3076 tree arg0 = gimple_call_arg (stmt, 0);
3077 tree arg1 = gimple_call_arg (stmt, 1);
3078 if (TREE_CODE (arg0) != INTEGER_CST
3079 || TREE_CODE (arg1) != INTEGER_CST)
3080 return false;
3081
3082 dest = gimple_call_lhs (stmt);
3083 len = fold_build2 (MULT_EXPR, TREE_TYPE (arg0), arg0, arg1);
3084 }
3085 else
3086 {
3087 dest = gimple_call_arg (stmt, 0);
3088 len = gimple_call_arg (stmt, 2);
3089 }
3090 if (!poly_int_tree_p (len))
3091 return false;
3092 tree rbase = ref->base;
3093 poly_offset_int roffset = ref->offset;
3094 ao_ref dref;
3095 ao_ref_init_from_ptr_and_size (&dref, dest, len);
3096 tree base = ao_ref_base (&dref);
3097 poly_offset_int offset = dref.offset;
3098 if (!base || !known_size_p (dref.size))
3099 return false;
3100 if (TREE_CODE (base) == MEM_REF)
3101 {
3102 if (TREE_CODE (rbase) != MEM_REF)
3103 return false;
3104 // Compare pointers.
3105 offset += mem_ref_offset (base) << LOG2_BITS_PER_UNIT;
3106 roffset += mem_ref_offset (rbase) << LOG2_BITS_PER_UNIT;
3107 base = TREE_OPERAND (base, 0);
3108 rbase = TREE_OPERAND (rbase, 0);
3109 }
3110 if (base == rbase
3111 && known_subrange_p (roffset, ref->max_size, offset,
3112 wi::to_poly_offset (len)
3113 << LOG2_BITS_PER_UNIT))
3114 return true;
3115 break;
3116 }
3117
3118 case BUILT_IN_VA_END:
3119 {
3120 tree ptr = gimple_call_arg (stmt, 0);
3121 if (TREE_CODE (ptr) == ADDR_EXPR)
3122 {
3123 tree base = ao_ref_base (ref);
3124 if (TREE_OPERAND (ptr, 0) == base)
3125 return true;
3126 }
3127 break;
3128 }
3129
3130 default:;
3131 }
3132 }
3133 return false;
3134 }
3135
3136 bool
3137 stmt_kills_ref_p (gimple *stmt, tree ref)
3138 {
3139 ao_ref r;
3140 ao_ref_init (&r, ref);
3141 return stmt_kills_ref_p (stmt, &r);
3142 }
3143
3144
3145 /* Walk the virtual use-def chain of VUSE until hitting the virtual operand
3146 TARGET or a statement clobbering the memory reference REF in which
3147 case false is returned. The walk starts with VUSE, one argument of PHI. */
3148
3149 static bool
3150 maybe_skip_until (gimple *phi, tree &target, basic_block target_bb,
3151 ao_ref *ref, tree vuse, bool tbaa_p, unsigned int &limit,
3152 bitmap *visited, bool abort_on_visited,
3153 void *(*translate)(ao_ref *, tree, void *, bool *),
3154 void *data)
3155 {
3156 basic_block bb = gimple_bb (phi);
3157
3158 if (!*visited)
3159 *visited = BITMAP_ALLOC (NULL);
3160
3161 bitmap_set_bit (*visited, SSA_NAME_VERSION (PHI_RESULT (phi)));
3162
3163 /* Walk until we hit the target. */
3164 while (vuse != target)
3165 {
3166 gimple *def_stmt = SSA_NAME_DEF_STMT (vuse);
3167 /* If we are searching for the target VUSE by walking up to
3168 TARGET_BB dominating the original PHI we are finished once
3169 we reach a default def or a definition in a block dominating
3170 that block. Update TARGET and return. */
3171 if (!target
3172 && (gimple_nop_p (def_stmt)
3173 || dominated_by_p (CDI_DOMINATORS,
3174 target_bb, gimple_bb (def_stmt))))
3175 {
3176 target = vuse;
3177 return true;
3178 }
3179
3180 /* Recurse for PHI nodes. */
3181 if (gimple_code (def_stmt) == GIMPLE_PHI)
3182 {
3183 /* An already visited PHI node ends the walk successfully. */
3184 if (bitmap_bit_p (*visited, SSA_NAME_VERSION (PHI_RESULT (def_stmt))))
3185 return !abort_on_visited;
3186 vuse = get_continuation_for_phi (def_stmt, ref, tbaa_p, limit,
3187 visited, abort_on_visited,
3188 translate, data);
3189 if (!vuse)
3190 return false;
3191 continue;
3192 }
3193 else if (gimple_nop_p (def_stmt))
3194 return false;
3195 else
3196 {
3197 /* A clobbering statement or the end of the IL ends it failing. */
3198 if ((int)limit <= 0)
3199 return false;
3200 --limit;
3201 if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
3202 {
3203 bool disambiguate_only = true;
3204 if (translate
3205 && (*translate) (ref, vuse, data, &disambiguate_only) == NULL)
3206 ;
3207 else
3208 return false;
3209 }
3210 }
3211 /* If we reach a new basic-block see if we already skipped it
3212 in a previous walk that ended successfully. */
3213 if (gimple_bb (def_stmt) != bb)
3214 {
3215 if (!bitmap_set_bit (*visited, SSA_NAME_VERSION (vuse)))
3216 return !abort_on_visited;
3217 bb = gimple_bb (def_stmt);
3218 }
3219 vuse = gimple_vuse (def_stmt);
3220 }
3221 return true;
3222 }
3223
3224
3225 /* Starting from a PHI node for the virtual operand of the memory reference
3226 REF find a continuation virtual operand that allows to continue walking
3227 statements dominating PHI skipping only statements that cannot possibly
3228 clobber REF. Decrements LIMIT for each alias disambiguation done
3229 and aborts the walk, returning NULL_TREE if it reaches zero.
3230 Returns NULL_TREE if no suitable virtual operand can be found. */
3231
3232 tree
3233 get_continuation_for_phi (gimple *phi, ao_ref *ref, bool tbaa_p,
3234 unsigned int &limit, bitmap *visited,
3235 bool abort_on_visited,
3236 void *(*translate)(ao_ref *, tree, void *, bool *),
3237 void *data)
3238 {
3239 unsigned nargs = gimple_phi_num_args (phi);
3240
3241 /* Through a single-argument PHI we can simply look through. */
3242 if (nargs == 1)
3243 return PHI_ARG_DEF (phi, 0);
3244
3245 /* For two or more arguments try to pairwise skip non-aliasing code
3246 until we hit the phi argument definition that dominates the other one. */
3247 basic_block phi_bb = gimple_bb (phi);
3248 tree arg0, arg1;
3249 unsigned i;
3250
3251 /* Find a candidate for the virtual operand which definition
3252 dominates those of all others. */
3253 /* First look if any of the args themselves satisfy this. */
3254 for (i = 0; i < nargs; ++i)
3255 {
3256 arg0 = PHI_ARG_DEF (phi, i);
3257 if (SSA_NAME_IS_DEFAULT_DEF (arg0))
3258 break;
3259 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (arg0));
3260 if (def_bb != phi_bb
3261 && dominated_by_p (CDI_DOMINATORS, phi_bb, def_bb))
3262 break;
3263 arg0 = NULL_TREE;
3264 }
3265 /* If not, look if we can reach such candidate by walking defs
3266 until we hit the immediate dominator. maybe_skip_until will
3267 do that for us. */
3268 basic_block dom = get_immediate_dominator (CDI_DOMINATORS, phi_bb);
3269
3270 /* Then check against the (to be) found candidate. */
3271 for (i = 0; i < nargs; ++i)
3272 {
3273 arg1 = PHI_ARG_DEF (phi, i);
3274 if (arg1 == arg0)
3275 ;
3276 else if (! maybe_skip_until (phi, arg0, dom, ref, arg1, tbaa_p,
3277 limit, visited,
3278 abort_on_visited,
3279 /* Do not translate when walking over
3280 backedges. */
3281 dominated_by_p
3282 (CDI_DOMINATORS,
3283 gimple_bb (SSA_NAME_DEF_STMT (arg1)),
3284 phi_bb)
3285 ? NULL : translate, data))
3286 return NULL_TREE;
3287 }
3288
3289 return arg0;
3290 }
3291
3292 /* Based on the memory reference REF and its virtual use VUSE call
3293 WALKER for each virtual use that is equivalent to VUSE, including VUSE
3294 itself. That is, for each virtual use for which its defining statement
3295 does not clobber REF.
3296
3297 WALKER is called with REF, the current virtual use and DATA. If
3298 WALKER returns non-NULL the walk stops and its result is returned.
3299 At the end of a non-successful walk NULL is returned.
3300
3301 TRANSLATE if non-NULL is called with a pointer to REF, the virtual
3302 use which definition is a statement that may clobber REF and DATA.
3303 If TRANSLATE returns (void *)-1 the walk stops and NULL is returned.
3304 If TRANSLATE returns non-NULL the walk stops and its result is returned.
3305 If TRANSLATE returns NULL the walk continues and TRANSLATE is supposed
3306 to adjust REF and *DATA to make that valid.
3307
3308 VALUEIZE if non-NULL is called with the next VUSE that is considered
3309 and return value is substituted for that. This can be used to
3310 implement optimistic value-numbering for example. Note that the
3311 VUSE argument is assumed to be valueized already.
3312
3313 LIMIT specifies the number of alias queries we are allowed to do,
3314 the walk stops when it reaches zero and NULL is returned. LIMIT
3315 is decremented by the number of alias queries (plus adjustments
3316 done by the callbacks) upon return.
3317
3318 TODO: Cache the vector of equivalent vuses per ref, vuse pair. */
3319
3320 void *
3321 walk_non_aliased_vuses (ao_ref *ref, tree vuse, bool tbaa_p,
3322 void *(*walker)(ao_ref *, tree, void *),
3323 void *(*translate)(ao_ref *, tree, void *, bool *),
3324 tree (*valueize)(tree),
3325 unsigned &limit, void *data)
3326 {
3327 bitmap visited = NULL;
3328 void *res;
3329 bool translated = false;
3330
3331 timevar_push (TV_ALIAS_STMT_WALK);
3332
3333 do
3334 {
3335 gimple *def_stmt;
3336
3337 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3338 res = (*walker) (ref, vuse, data);
3339 /* Abort walk. */
3340 if (res == (void *)-1)
3341 {
3342 res = NULL;
3343 break;
3344 }
3345 /* Lookup succeeded. */
3346 else if (res != NULL)
3347 break;
3348
3349 if (valueize)
3350 {
3351 vuse = valueize (vuse);
3352 if (!vuse)
3353 {
3354 res = NULL;
3355 break;
3356 }
3357 }
3358 def_stmt = SSA_NAME_DEF_STMT (vuse);
3359 if (gimple_nop_p (def_stmt))
3360 break;
3361 else if (gimple_code (def_stmt) == GIMPLE_PHI)
3362 vuse = get_continuation_for_phi (def_stmt, ref, tbaa_p, limit,
3363 &visited, translated, translate, data);
3364 else
3365 {
3366 if ((int)limit <= 0)
3367 {
3368 res = NULL;
3369 break;
3370 }
3371 --limit;
3372 if (stmt_may_clobber_ref_p_1 (def_stmt, ref, tbaa_p))
3373 {
3374 if (!translate)
3375 break;
3376 bool disambiguate_only = false;
3377 res = (*translate) (ref, vuse, data, &disambiguate_only);
3378 /* Failed lookup and translation. */
3379 if (res == (void *)-1)
3380 {
3381 res = NULL;
3382 break;
3383 }
3384 /* Lookup succeeded. */
3385 else if (res != NULL)
3386 break;
3387 /* Translation succeeded, continue walking. */
3388 translated = translated || !disambiguate_only;
3389 }
3390 vuse = gimple_vuse (def_stmt);
3391 }
3392 }
3393 while (vuse);
3394
3395 if (visited)
3396 BITMAP_FREE (visited);
3397
3398 timevar_pop (TV_ALIAS_STMT_WALK);
3399
3400 return res;
3401 }
3402
3403
3404 /* Based on the memory reference REF call WALKER for each vdef which
3405 defining statement may clobber REF, starting with VDEF. If REF
3406 is NULL_TREE, each defining statement is visited.
3407
3408 WALKER is called with REF, the current vdef and DATA. If WALKER
3409 returns true the walk is stopped, otherwise it continues.
3410
3411 If function entry is reached, FUNCTION_ENTRY_REACHED is set to true.
3412 The pointer may be NULL and then we do not track this information.
3413
3414 At PHI nodes walk_aliased_vdefs forks into one walk for reach
3415 PHI argument (but only one walk continues on merge points), the
3416 return value is true if any of the walks was successful.
3417
3418 The function returns the number of statements walked or -1 if
3419 LIMIT stmts were walked and the walk was aborted at this point.
3420 If LIMIT is zero the walk is not aborted. */
3421
3422 static int
3423 walk_aliased_vdefs_1 (ao_ref *ref, tree vdef,
3424 bool (*walker)(ao_ref *, tree, void *), void *data,
3425 bitmap *visited, unsigned int cnt,
3426 bool *function_entry_reached, unsigned limit)
3427 {
3428 do
3429 {
3430 gimple *def_stmt = SSA_NAME_DEF_STMT (vdef);
3431
3432 if (*visited
3433 && !bitmap_set_bit (*visited, SSA_NAME_VERSION (vdef)))
3434 return cnt;
3435
3436 if (gimple_nop_p (def_stmt))
3437 {
3438 if (function_entry_reached)
3439 *function_entry_reached = true;
3440 return cnt;
3441 }
3442 else if (gimple_code (def_stmt) == GIMPLE_PHI)
3443 {
3444 unsigned i;
3445 if (!*visited)
3446 *visited = BITMAP_ALLOC (NULL);
3447 for (i = 0; i < gimple_phi_num_args (def_stmt); ++i)
3448 {
3449 int res = walk_aliased_vdefs_1 (ref,
3450 gimple_phi_arg_def (def_stmt, i),
3451 walker, data, visited, cnt,
3452 function_entry_reached, limit);
3453 if (res == -1)
3454 return -1;
3455 cnt = res;
3456 }
3457 return cnt;
3458 }
3459
3460 /* ??? Do we want to account this to TV_ALIAS_STMT_WALK? */
3461 cnt++;
3462 if (cnt == limit)
3463 return -1;
3464 if ((!ref
3465 || stmt_may_clobber_ref_p_1 (def_stmt, ref))
3466 && (*walker) (ref, vdef, data))
3467 return cnt;
3468
3469 vdef = gimple_vuse (def_stmt);
3470 }
3471 while (1);
3472 }
3473
3474 int
3475 walk_aliased_vdefs (ao_ref *ref, tree vdef,
3476 bool (*walker)(ao_ref *, tree, void *), void *data,
3477 bitmap *visited,
3478 bool *function_entry_reached, unsigned int limit)
3479 {
3480 bitmap local_visited = NULL;
3481 int ret;
3482
3483 timevar_push (TV_ALIAS_STMT_WALK);
3484
3485 if (function_entry_reached)
3486 *function_entry_reached = false;
3487
3488 ret = walk_aliased_vdefs_1 (ref, vdef, walker, data,
3489 visited ? visited : &local_visited, 0,
3490 function_entry_reached, limit);
3491 if (local_visited)
3492 BITMAP_FREE (local_visited);
3493
3494 timevar_pop (TV_ALIAS_STMT_WALK);
3495
3496 return ret;
3497 }
3498