]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/alias.c
tree-optimization/93946 - fix bogus redundant store removal in FRE, DSE and DOM
[thirdparty/gcc.git] / gcc / alias.c
1 /* Alias analysis for GNU C
2 Copyright (C) 1997-2020 Free Software Foundation, Inc.
3 Contributed by John Carr (jfc@mit.edu).
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 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 "df.h"
30 #include "memmodel.h"
31 #include "tm_p.h"
32 #include "gimple-ssa.h"
33 #include "emit-rtl.h"
34 #include "alias.h"
35 #include "fold-const.h"
36 #include "varasm.h"
37 #include "cselib.h"
38 #include "langhooks.h"
39 #include "cfganal.h"
40 #include "rtl-iter.h"
41 #include "cgraph.h"
42 #include "ipa-utils.h"
43
44 /* The aliasing API provided here solves related but different problems:
45
46 Say there exists (in c)
47
48 struct X {
49 struct Y y1;
50 struct Z z2;
51 } x1, *px1, *px2;
52
53 struct Y y2, *py;
54 struct Z z2, *pz;
55
56
57 py = &x1.y1;
58 px2 = &x1;
59
60 Consider the four questions:
61
62 Can a store to x1 interfere with px2->y1?
63 Can a store to x1 interfere with px2->z2?
64 Can a store to x1 change the value pointed to by with py?
65 Can a store to x1 change the value pointed to by with pz?
66
67 The answer to these questions can be yes, yes, yes, and maybe.
68
69 The first two questions can be answered with a simple examination
70 of the type system. If structure X contains a field of type Y then
71 a store through a pointer to an X can overwrite any field that is
72 contained (recursively) in an X (unless we know that px1 != px2).
73
74 The last two questions can be solved in the same way as the first
75 two questions but this is too conservative. The observation is
76 that in some cases we can know which (if any) fields are addressed
77 and if those addresses are used in bad ways. This analysis may be
78 language specific. In C, arbitrary operations may be applied to
79 pointers. However, there is some indication that this may be too
80 conservative for some C++ types.
81
82 The pass ipa-type-escape does this analysis for the types whose
83 instances do not escape across the compilation boundary.
84
85 Historically in GCC, these two problems were combined and a single
86 data structure that was used to represent the solution to these
87 problems. We now have two similar but different data structures,
88 The data structure to solve the last two questions is similar to
89 the first, but does not contain the fields whose address are never
90 taken. For types that do escape the compilation unit, the data
91 structures will have identical information.
92 */
93
94 /* The alias sets assigned to MEMs assist the back-end in determining
95 which MEMs can alias which other MEMs. In general, two MEMs in
96 different alias sets cannot alias each other, with one important
97 exception. Consider something like:
98
99 struct S { int i; double d; };
100
101 a store to an `S' can alias something of either type `int' or type
102 `double'. (However, a store to an `int' cannot alias a `double'
103 and vice versa.) We indicate this via a tree structure that looks
104 like:
105 struct S
106 / \
107 / \
108 |/_ _\|
109 int double
110
111 (The arrows are directed and point downwards.)
112 In this situation we say the alias set for `struct S' is the
113 `superset' and that those for `int' and `double' are `subsets'.
114
115 To see whether two alias sets can point to the same memory, we must
116 see if either alias set is a subset of the other. We need not trace
117 past immediate descendants, however, since we propagate all
118 grandchildren up one level.
119
120 Alias set zero is implicitly a superset of all other alias sets.
121 However, this is no actual entry for alias set zero. It is an
122 error to attempt to explicitly construct a subset of zero. */
123
124 struct alias_set_hash : int_hash <int, INT_MIN, INT_MIN + 1> {};
125
126 struct GTY(()) alias_set_entry {
127 /* The alias set number, as stored in MEM_ALIAS_SET. */
128 alias_set_type alias_set;
129
130 /* Nonzero if would have a child of zero: this effectively makes this
131 alias set the same as alias set zero. */
132 bool has_zero_child;
133 /* Nonzero if alias set corresponds to pointer type itself (i.e. not to
134 aggregate contaiing pointer.
135 This is used for a special case where we need an universal pointer type
136 compatible with all other pointer types. */
137 bool is_pointer;
138 /* Nonzero if is_pointer or if one of childs have has_pointer set. */
139 bool has_pointer;
140
141 /* The children of the alias set. These are not just the immediate
142 children, but, in fact, all descendants. So, if we have:
143
144 struct T { struct S s; float f; }
145
146 continuing our example above, the children here will be all of
147 `int', `double', `float', and `struct S'. */
148 hash_map<alias_set_hash, int> *children;
149 };
150
151 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
152 static void record_set (rtx, const_rtx, void *);
153 static int base_alias_check (rtx, rtx, rtx, rtx, machine_mode,
154 machine_mode);
155 static rtx find_base_value (rtx);
156 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
157 static alias_set_entry *get_alias_set_entry (alias_set_type);
158 static tree decl_for_component_ref (tree);
159 static int write_dependence_p (const_rtx,
160 const_rtx, machine_mode, rtx,
161 bool, bool, bool);
162 static int compare_base_symbol_refs (const_rtx, const_rtx);
163
164 static void memory_modified_1 (rtx, const_rtx, void *);
165
166 /* Query statistics for the different low-level disambiguators.
167 A high-level query may trigger multiple of them. */
168
169 static struct {
170 unsigned long long num_alias_zero;
171 unsigned long long num_same_alias_set;
172 unsigned long long num_same_objects;
173 unsigned long long num_volatile;
174 unsigned long long num_dag;
175 unsigned long long num_universal;
176 unsigned long long num_disambiguated;
177 } alias_stats;
178
179
180 /* Set up all info needed to perform alias analysis on memory references. */
181
182 /* Returns the size in bytes of the mode of X. */
183 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
184
185 /* Cap the number of passes we make over the insns propagating alias
186 information through set chains.
187 ??? 10 is a completely arbitrary choice. This should be based on the
188 maximum loop depth in the CFG, but we do not have this information
189 available (even if current_loops _is_ available). */
190 #define MAX_ALIAS_LOOP_PASSES 10
191
192 /* reg_base_value[N] gives an address to which register N is related.
193 If all sets after the first add or subtract to the current value
194 or otherwise modify it so it does not point to a different top level
195 object, reg_base_value[N] is equal to the address part of the source
196 of the first set.
197
198 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
199 expressions represent three types of base:
200
201 1. incoming arguments. There is just one ADDRESS to represent all
202 arguments, since we do not know at this level whether accesses
203 based on different arguments can alias. The ADDRESS has id 0.
204
205 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
206 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
207 Each of these rtxes has a separate ADDRESS associated with it,
208 each with a negative id.
209
210 GCC is (and is required to be) precise in which register it
211 chooses to access a particular region of stack. We can therefore
212 assume that accesses based on one of these rtxes do not alias
213 accesses based on another of these rtxes.
214
215 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
216 Each such piece of memory has a separate ADDRESS associated
217 with it, each with an id greater than 0.
218
219 Accesses based on one ADDRESS do not alias accesses based on other
220 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
221 alias globals either; the ADDRESSes have Pmode to indicate this.
222 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
223 indicate this. */
224
225 static GTY(()) vec<rtx, va_gc> *reg_base_value;
226 static rtx *new_reg_base_value;
227
228 /* The single VOIDmode ADDRESS that represents all argument bases.
229 It has id 0. */
230 static GTY(()) rtx arg_base_value;
231
232 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
233 static int unique_id;
234
235 /* We preserve the copy of old array around to avoid amount of garbage
236 produced. About 8% of garbage produced were attributed to this
237 array. */
238 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
239
240 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
241 registers. */
242 #define UNIQUE_BASE_VALUE_SP -1
243 #define UNIQUE_BASE_VALUE_ARGP -2
244 #define UNIQUE_BASE_VALUE_FP -3
245 #define UNIQUE_BASE_VALUE_HFP -4
246
247 #define static_reg_base_value \
248 (this_target_rtl->x_static_reg_base_value)
249
250 #define REG_BASE_VALUE(X) \
251 (REGNO (X) < vec_safe_length (reg_base_value) \
252 ? (*reg_base_value)[REGNO (X)] : 0)
253
254 /* Vector indexed by N giving the initial (unchanging) value known for
255 pseudo-register N. This vector is initialized in init_alias_analysis,
256 and does not change until end_alias_analysis is called. */
257 static GTY(()) vec<rtx, va_gc> *reg_known_value;
258
259 /* Vector recording for each reg_known_value whether it is due to a
260 REG_EQUIV note. Future passes (viz., reload) may replace the
261 pseudo with the equivalent expression and so we account for the
262 dependences that would be introduced if that happens.
263
264 The REG_EQUIV notes created in assign_parms may mention the arg
265 pointer, and there are explicit insns in the RTL that modify the
266 arg pointer. Thus we must ensure that such insns don't get
267 scheduled across each other because that would invalidate the
268 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
269 wrong, but solving the problem in the scheduler will likely give
270 better code, so we do it here. */
271 static sbitmap reg_known_equiv_p;
272
273 /* True when scanning insns from the start of the rtl to the
274 NOTE_INSN_FUNCTION_BEG note. */
275 static bool copying_arguments;
276
277
278 /* The splay-tree used to store the various alias set entries. */
279 static GTY (()) vec<alias_set_entry *, va_gc> *alias_sets;
280 \f
281 /* Build a decomposed reference object for querying the alias-oracle
282 from the MEM rtx and store it in *REF.
283 Returns false if MEM is not suitable for the alias-oracle. */
284
285 static bool
286 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
287 {
288 tree expr = MEM_EXPR (mem);
289 tree base;
290
291 if (!expr)
292 return false;
293
294 ao_ref_init (ref, expr);
295
296 /* Get the base of the reference and see if we have to reject or
297 adjust it. */
298 base = ao_ref_base (ref);
299 if (base == NULL_TREE)
300 return false;
301
302 /* The tree oracle doesn't like bases that are neither decls
303 nor indirect references of SSA names. */
304 if (!(DECL_P (base)
305 || (TREE_CODE (base) == MEM_REF
306 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
307 || (TREE_CODE (base) == TARGET_MEM_REF
308 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
309 return false;
310
311 ref->ref_alias_set = MEM_ALIAS_SET (mem);
312
313 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
314 is conservative, so trust it. */
315 if (!MEM_OFFSET_KNOWN_P (mem)
316 || !MEM_SIZE_KNOWN_P (mem))
317 return true;
318
319 /* If MEM_OFFSET/MEM_SIZE get us outside of ref->offset/ref->max_size
320 drop ref->ref. */
321 if (maybe_lt (MEM_OFFSET (mem), 0)
322 || (ref->max_size_known_p ()
323 && maybe_gt ((MEM_OFFSET (mem) + MEM_SIZE (mem)) * BITS_PER_UNIT,
324 ref->max_size)))
325 ref->ref = NULL_TREE;
326
327 /* Refine size and offset we got from analyzing MEM_EXPR by using
328 MEM_SIZE and MEM_OFFSET. */
329
330 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
331 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
332
333 /* The MEM may extend into adjacent fields, so adjust max_size if
334 necessary. */
335 if (ref->max_size_known_p ())
336 ref->max_size = upper_bound (ref->max_size, ref->size);
337
338 /* If MEM_OFFSET and MEM_SIZE might get us outside of the base object of
339 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
340 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
341 && (maybe_lt (ref->offset, 0)
342 || (DECL_P (ref->base)
343 && (DECL_SIZE (ref->base) == NULL_TREE
344 || !poly_int_tree_p (DECL_SIZE (ref->base))
345 || maybe_lt (wi::to_poly_offset (DECL_SIZE (ref->base)),
346 ref->offset + ref->size)))))
347 return false;
348
349 return true;
350 }
351
352 /* Query the alias-oracle on whether the two memory rtx X and MEM may
353 alias. If TBAA_P is set also apply TBAA. Returns true if the
354 two rtxen may alias, false otherwise. */
355
356 static bool
357 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
358 {
359 ao_ref ref1, ref2;
360
361 if (!ao_ref_from_mem (&ref1, x)
362 || !ao_ref_from_mem (&ref2, mem))
363 return true;
364
365 return refs_may_alias_p_1 (&ref1, &ref2,
366 tbaa_p
367 && MEM_ALIAS_SET (x) != 0
368 && MEM_ALIAS_SET (mem) != 0);
369 }
370
371 /* Return true if the ref EARLIER behaves the same as LATER with respect
372 to TBAA for every memory reference that might follow LATER. */
373
374 bool
375 refs_same_for_tbaa_p (tree earlier, tree later)
376 {
377 ao_ref earlier_ref, later_ref;
378 ao_ref_init (&earlier_ref, earlier);
379 ao_ref_init (&later_ref, later);
380 alias_set_type earlier_set = ao_ref_alias_set (&earlier_ref);
381 alias_set_type later_set = ao_ref_alias_set (&later_ref);
382 if (!(earlier_set == later_set
383 || alias_set_subset_of (later_set, earlier_set)))
384 return false;
385 alias_set_type later_base_set = ao_ref_base_alias_set (&later_ref);
386 alias_set_type earlier_base_set = ao_ref_base_alias_set (&earlier_ref);
387 return (earlier_base_set == later_base_set
388 || alias_set_subset_of (later_base_set, earlier_base_set));
389 }
390
391 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
392 such an entry, or NULL otherwise. */
393
394 static inline alias_set_entry *
395 get_alias_set_entry (alias_set_type alias_set)
396 {
397 return (*alias_sets)[alias_set];
398 }
399
400 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
401 the two MEMs cannot alias each other. */
402
403 static inline int
404 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
405 {
406 return (flag_strict_aliasing
407 && ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1),
408 MEM_ALIAS_SET (mem2)));
409 }
410
411 /* Return true if the first alias set is a subset of the second. */
412
413 bool
414 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
415 {
416 alias_set_entry *ase2;
417
418 /* Disable TBAA oracle with !flag_strict_aliasing. */
419 if (!flag_strict_aliasing)
420 return true;
421
422 /* Everything is a subset of the "aliases everything" set. */
423 if (set2 == 0)
424 return true;
425
426 /* Check if set1 is a subset of set2. */
427 ase2 = get_alias_set_entry (set2);
428 if (ase2 != 0
429 && (ase2->has_zero_child
430 || (ase2->children && ase2->children->get (set1))))
431 return true;
432
433 /* As a special case we consider alias set of "void *" to be both subset
434 and superset of every alias set of a pointer. This extra symmetry does
435 not matter for alias_sets_conflict_p but it makes aliasing_component_refs_p
436 to return true on the following testcase:
437
438 void *ptr;
439 char **ptr2=(char **)&ptr;
440 *ptr2 = ...
441
442 Additionally if a set contains universal pointer, we consider every pointer
443 to be a subset of it, but we do not represent this explicitely - doing so
444 would require us to update transitive closure each time we introduce new
445 pointer type. This makes aliasing_component_refs_p to return true
446 on the following testcase:
447
448 struct a {void *ptr;}
449 char **ptr = (char **)&a.ptr;
450 ptr = ...
451
452 This makes void * truly universal pointer type. See pointer handling in
453 get_alias_set for more details. */
454 if (ase2 && ase2->has_pointer)
455 {
456 alias_set_entry *ase1 = get_alias_set_entry (set1);
457
458 if (ase1 && ase1->is_pointer)
459 {
460 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
461 /* If one is ptr_type_node and other is pointer, then we consider
462 them subset of each other. */
463 if (set1 == voidptr_set || set2 == voidptr_set)
464 return true;
465 /* If SET2 contains universal pointer's alias set, then we consdier
466 every (non-universal) pointer. */
467 if (ase2->children && set1 != voidptr_set
468 && ase2->children->get (voidptr_set))
469 return true;
470 }
471 }
472 return false;
473 }
474
475 /* Return 1 if the two specified alias sets may conflict. */
476
477 int
478 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
479 {
480 alias_set_entry *ase1;
481 alias_set_entry *ase2;
482
483 /* The easy case. */
484 if (alias_sets_must_conflict_p (set1, set2))
485 return 1;
486
487 /* See if the first alias set is a subset of the second. */
488 ase1 = get_alias_set_entry (set1);
489 if (ase1 != 0
490 && ase1->children && ase1->children->get (set2))
491 {
492 ++alias_stats.num_dag;
493 return 1;
494 }
495
496 /* Now do the same, but with the alias sets reversed. */
497 ase2 = get_alias_set_entry (set2);
498 if (ase2 != 0
499 && ase2->children && ase2->children->get (set1))
500 {
501 ++alias_stats.num_dag;
502 return 1;
503 }
504
505 /* We want void * to be compatible with any other pointer without
506 really dropping it to alias set 0. Doing so would make it
507 compatible with all non-pointer types too.
508
509 This is not strictly necessary by the C/C++ language
510 standards, but avoids common type punning mistakes. In
511 addition to that, we need the existence of such universal
512 pointer to implement Fortran's C_PTR type (which is defined as
513 type compatible with all C pointers). */
514 if (ase1 && ase2 && ase1->has_pointer && ase2->has_pointer)
515 {
516 alias_set_type voidptr_set = TYPE_ALIAS_SET (ptr_type_node);
517
518 /* If one of the sets corresponds to universal pointer,
519 we consider it to conflict with anything that is
520 or contains pointer. */
521 if (set1 == voidptr_set || set2 == voidptr_set)
522 {
523 ++alias_stats.num_universal;
524 return true;
525 }
526 /* If one of sets is (non-universal) pointer and the other
527 contains universal pointer, we also get conflict. */
528 if (ase1->is_pointer && set2 != voidptr_set
529 && ase2->children && ase2->children->get (voidptr_set))
530 {
531 ++alias_stats.num_universal;
532 return true;
533 }
534 if (ase2->is_pointer && set1 != voidptr_set
535 && ase1->children && ase1->children->get (voidptr_set))
536 {
537 ++alias_stats.num_universal;
538 return true;
539 }
540 }
541
542 ++alias_stats.num_disambiguated;
543
544 /* The two alias sets are distinct and neither one is the
545 child of the other. Therefore, they cannot conflict. */
546 return 0;
547 }
548
549 /* Return 1 if the two specified alias sets will always conflict. */
550
551 int
552 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
553 {
554 /* Disable TBAA oracle with !flag_strict_aliasing. */
555 if (!flag_strict_aliasing)
556 return 1;
557 if (set1 == 0 || set2 == 0)
558 {
559 ++alias_stats.num_alias_zero;
560 return 1;
561 }
562 if (set1 == set2)
563 {
564 ++alias_stats.num_same_alias_set;
565 return 1;
566 }
567
568 return 0;
569 }
570
571 /* Return 1 if any MEM object of type T1 will always conflict (using the
572 dependency routines in this file) with any MEM object of type T2.
573 This is used when allocating temporary storage. If T1 and/or T2 are
574 NULL_TREE, it means we know nothing about the storage. */
575
576 int
577 objects_must_conflict_p (tree t1, tree t2)
578 {
579 alias_set_type set1, set2;
580
581 /* If neither has a type specified, we don't know if they'll conflict
582 because we may be using them to store objects of various types, for
583 example the argument and local variables areas of inlined functions. */
584 if (t1 == 0 && t2 == 0)
585 return 0;
586
587 /* If they are the same type, they must conflict. */
588 if (t1 == t2)
589 {
590 ++alias_stats.num_same_objects;
591 return 1;
592 }
593 /* Likewise if both are volatile. */
594 if (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2))
595 {
596 ++alias_stats.num_volatile;
597 return 1;
598 }
599
600 set1 = t1 ? get_alias_set (t1) : 0;
601 set2 = t2 ? get_alias_set (t2) : 0;
602
603 /* We can't use alias_sets_conflict_p because we must make sure
604 that every subtype of t1 will conflict with every subtype of
605 t2 for which a pair of subobjects of these respective subtypes
606 overlaps on the stack. */
607 return alias_sets_must_conflict_p (set1, set2);
608 }
609 \f
610 /* Return true if T is an end of the access path which can be used
611 by type based alias oracle. */
612
613 bool
614 ends_tbaa_access_path_p (const_tree t)
615 {
616 switch (TREE_CODE (t))
617 {
618 case COMPONENT_REF:
619 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
620 return true;
621 /* Permit type-punning when accessing a union, provided the access
622 is directly through the union. For example, this code does not
623 permit taking the address of a union member and then storing
624 through it. Even the type-punning allowed here is a GCC
625 extension, albeit a common and useful one; the C standard says
626 that such accesses have implementation-defined behavior. */
627 else if (TREE_CODE (TREE_TYPE (TREE_OPERAND (t, 0))) == UNION_TYPE)
628 return true;
629 break;
630
631 case ARRAY_REF:
632 case ARRAY_RANGE_REF:
633 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
634 return true;
635 break;
636
637 case REALPART_EXPR:
638 case IMAGPART_EXPR:
639 break;
640
641 case BIT_FIELD_REF:
642 case VIEW_CONVERT_EXPR:
643 /* Bitfields and casts are never addressable. */
644 return true;
645 break;
646
647 default:
648 gcc_unreachable ();
649 }
650 return false;
651 }
652
653 /* Return the outermost parent of component present in the chain of
654 component references handled by get_inner_reference in T with the
655 following property:
656 - the component is non-addressable
657 or NULL_TREE if no such parent exists. In the former cases, the alias
658 set of this parent is the alias set that must be used for T itself. */
659
660 tree
661 component_uses_parent_alias_set_from (const_tree t)
662 {
663 const_tree found = NULL_TREE;
664
665 while (handled_component_p (t))
666 {
667 if (ends_tbaa_access_path_p (t))
668 found = t;
669
670 t = TREE_OPERAND (t, 0);
671 }
672
673 if (found)
674 return TREE_OPERAND (found, 0);
675
676 return NULL_TREE;
677 }
678
679
680 /* Return whether the pointer-type T effective for aliasing may
681 access everything and thus the reference has to be assigned
682 alias-set zero. */
683
684 static bool
685 ref_all_alias_ptr_type_p (const_tree t)
686 {
687 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
688 || TYPE_REF_CAN_ALIAS_ALL (t));
689 }
690
691 /* Return the alias set for the memory pointed to by T, which may be
692 either a type or an expression. Return -1 if there is nothing
693 special about dereferencing T. */
694
695 static alias_set_type
696 get_deref_alias_set_1 (tree t)
697 {
698 /* All we care about is the type. */
699 if (! TYPE_P (t))
700 t = TREE_TYPE (t);
701
702 /* If we have an INDIRECT_REF via a void pointer, we don't
703 know anything about what that might alias. Likewise if the
704 pointer is marked that way. */
705 if (ref_all_alias_ptr_type_p (t))
706 return 0;
707
708 return -1;
709 }
710
711 /* Return the alias set for the memory pointed to by T, which may be
712 either a type or an expression. */
713
714 alias_set_type
715 get_deref_alias_set (tree t)
716 {
717 /* If we're not doing any alias analysis, just assume everything
718 aliases everything else. */
719 if (!flag_strict_aliasing)
720 return 0;
721
722 alias_set_type set = get_deref_alias_set_1 (t);
723
724 /* Fall back to the alias-set of the pointed-to type. */
725 if (set == -1)
726 {
727 if (! TYPE_P (t))
728 t = TREE_TYPE (t);
729 set = get_alias_set (TREE_TYPE (t));
730 }
731
732 return set;
733 }
734
735 /* Return the pointer-type relevant for TBAA purposes from the
736 memory reference tree *T or NULL_TREE in which case *T is
737 adjusted to point to the outermost component reference that
738 can be used for assigning an alias set. */
739
740 static tree
741 reference_alias_ptr_type_1 (tree *t)
742 {
743 tree inner;
744
745 /* Get the base object of the reference. */
746 inner = *t;
747 while (handled_component_p (inner))
748 {
749 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
750 the type of any component references that wrap it to
751 determine the alias-set. */
752 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
753 *t = TREE_OPERAND (inner, 0);
754 inner = TREE_OPERAND (inner, 0);
755 }
756
757 /* Handle pointer dereferences here, they can override the
758 alias-set. */
759 if (INDIRECT_REF_P (inner)
760 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
761 return TREE_TYPE (TREE_OPERAND (inner, 0));
762 else if (TREE_CODE (inner) == TARGET_MEM_REF)
763 return TREE_TYPE (TMR_OFFSET (inner));
764 else if (TREE_CODE (inner) == MEM_REF
765 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
766 return TREE_TYPE (TREE_OPERAND (inner, 1));
767
768 /* If the innermost reference is a MEM_REF that has a
769 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
770 using the memory access type for determining the alias-set. */
771 if (TREE_CODE (inner) == MEM_REF
772 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
773 != TYPE_MAIN_VARIANT
774 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
775 return TREE_TYPE (TREE_OPERAND (inner, 1));
776
777 /* Otherwise, pick up the outermost object that we could have
778 a pointer to. */
779 tree tem = component_uses_parent_alias_set_from (*t);
780 if (tem)
781 *t = tem;
782
783 return NULL_TREE;
784 }
785
786 /* Return the pointer-type relevant for TBAA purposes from the
787 gimple memory reference tree T. This is the type to be used for
788 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
789 and guarantees that get_alias_set will return the same alias
790 set for T and the replacement. */
791
792 tree
793 reference_alias_ptr_type (tree t)
794 {
795 /* If the frontend assigns this alias-set zero, preserve that. */
796 if (lang_hooks.get_alias_set (t) == 0)
797 return ptr_type_node;
798
799 tree ptype = reference_alias_ptr_type_1 (&t);
800 /* If there is a given pointer type for aliasing purposes, return it. */
801 if (ptype != NULL_TREE)
802 return ptype;
803
804 /* Otherwise build one from the outermost component reference we
805 may use. */
806 if (TREE_CODE (t) == MEM_REF
807 || TREE_CODE (t) == TARGET_MEM_REF)
808 return TREE_TYPE (TREE_OPERAND (t, 1));
809 else
810 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
811 }
812
813 /* Return whether the pointer-types T1 and T2 used to determine
814 two alias sets of two references will yield the same answer
815 from get_deref_alias_set. */
816
817 bool
818 alias_ptr_types_compatible_p (tree t1, tree t2)
819 {
820 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
821 return true;
822
823 if (ref_all_alias_ptr_type_p (t1)
824 || ref_all_alias_ptr_type_p (t2))
825 return false;
826
827 /* This function originally abstracts from simply comparing
828 get_deref_alias_set so that we are sure this still computes
829 the same result after LTO type merging is applied.
830 When in LTO type merging is done we can actually do this compare.
831 */
832 if (in_lto_p)
833 return get_deref_alias_set (t1) == get_deref_alias_set (t2);
834 else
835 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
836 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
837 }
838
839 /* Create emptry alias set entry. */
840
841 alias_set_entry *
842 init_alias_set_entry (alias_set_type set)
843 {
844 alias_set_entry *ase = ggc_alloc<alias_set_entry> ();
845 ase->alias_set = set;
846 ase->children = NULL;
847 ase->has_zero_child = false;
848 ase->is_pointer = false;
849 ase->has_pointer = false;
850 gcc_checking_assert (!get_alias_set_entry (set));
851 (*alias_sets)[set] = ase;
852 return ase;
853 }
854
855 /* Return the alias set for T, which may be either a type or an
856 expression. Call language-specific routine for help, if needed. */
857
858 alias_set_type
859 get_alias_set (tree t)
860 {
861 alias_set_type set;
862
863 /* We cannot give up with -fno-strict-aliasing because we need to build
864 proper type representation for possible functions which are build with
865 -fstrict-aliasing. */
866
867 /* return 0 if this or its type is an error. */
868 if (t == error_mark_node
869 || (! TYPE_P (t)
870 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
871 return 0;
872
873 /* We can be passed either an expression or a type. This and the
874 language-specific routine may make mutually-recursive calls to each other
875 to figure out what to do. At each juncture, we see if this is a tree
876 that the language may need to handle specially. First handle things that
877 aren't types. */
878 if (! TYPE_P (t))
879 {
880 /* Give the language a chance to do something with this tree
881 before we look at it. */
882 STRIP_NOPS (t);
883 set = lang_hooks.get_alias_set (t);
884 if (set != -1)
885 return set;
886
887 /* Get the alias pointer-type to use or the outermost object
888 that we could have a pointer to. */
889 tree ptype = reference_alias_ptr_type_1 (&t);
890 if (ptype != NULL)
891 return get_deref_alias_set (ptype);
892
893 /* If we've already determined the alias set for a decl, just return
894 it. This is necessary for C++ anonymous unions, whose component
895 variables don't look like union members (boo!). */
896 if (VAR_P (t)
897 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
898 return MEM_ALIAS_SET (DECL_RTL (t));
899
900 /* Now all we care about is the type. */
901 t = TREE_TYPE (t);
902 }
903
904 /* Variant qualifiers don't affect the alias set, so get the main
905 variant. */
906 t = TYPE_MAIN_VARIANT (t);
907
908 if (AGGREGATE_TYPE_P (t)
909 && TYPE_TYPELESS_STORAGE (t))
910 return 0;
911
912 /* Always use the canonical type as well. If this is a type that
913 requires structural comparisons to identify compatible types
914 use alias set zero. */
915 if (TYPE_STRUCTURAL_EQUALITY_P (t))
916 {
917 /* Allow the language to specify another alias set for this
918 type. */
919 set = lang_hooks.get_alias_set (t);
920 if (set != -1)
921 return set;
922 /* Handle structure type equality for pointer types, arrays and vectors.
923 This is easy to do, because the code bellow ignore canonical types on
924 these anyway. This is important for LTO, where TYPE_CANONICAL for
925 pointers cannot be meaningfuly computed by the frotnend. */
926 if (canonical_type_used_p (t))
927 {
928 /* In LTO we set canonical types for all types where it makes
929 sense to do so. Double check we did not miss some type. */
930 gcc_checking_assert (!in_lto_p || !type_with_alias_set_p (t));
931 return 0;
932 }
933 }
934 else
935 {
936 t = TYPE_CANONICAL (t);
937 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
938 }
939
940 /* If this is a type with a known alias set, return it. */
941 gcc_checking_assert (t == TYPE_MAIN_VARIANT (t));
942 if (TYPE_ALIAS_SET_KNOWN_P (t))
943 return TYPE_ALIAS_SET (t);
944
945 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
946 if (!COMPLETE_TYPE_P (t))
947 {
948 /* For arrays with unknown size the conservative answer is the
949 alias set of the element type. */
950 if (TREE_CODE (t) == ARRAY_TYPE)
951 return get_alias_set (TREE_TYPE (t));
952
953 /* But return zero as a conservative answer for incomplete types. */
954 return 0;
955 }
956
957 /* See if the language has special handling for this type. */
958 set = lang_hooks.get_alias_set (t);
959 if (set != -1)
960 return set;
961
962 /* There are no objects of FUNCTION_TYPE, so there's no point in
963 using up an alias set for them. (There are, of course, pointers
964 and references to functions, but that's different.) */
965 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
966 set = 0;
967
968 /* Unless the language specifies otherwise, let vector types alias
969 their components. This avoids some nasty type punning issues in
970 normal usage. And indeed lets vectors be treated more like an
971 array slice. */
972 else if (TREE_CODE (t) == VECTOR_TYPE)
973 set = get_alias_set (TREE_TYPE (t));
974
975 /* Unless the language specifies otherwise, treat array types the
976 same as their components. This avoids the asymmetry we get
977 through recording the components. Consider accessing a
978 character(kind=1) through a reference to a character(kind=1)[1:1].
979 Or consider if we want to assign integer(kind=4)[0:D.1387] and
980 integer(kind=4)[4] the same alias set or not.
981 Just be pragmatic here and make sure the array and its element
982 type get the same alias set assigned. */
983 else if (TREE_CODE (t) == ARRAY_TYPE
984 && (!TYPE_NONALIASED_COMPONENT (t)
985 || TYPE_STRUCTURAL_EQUALITY_P (t)))
986 set = get_alias_set (TREE_TYPE (t));
987
988 /* From the former common C and C++ langhook implementation:
989
990 Unfortunately, there is no canonical form of a pointer type.
991 In particular, if we have `typedef int I', then `int *', and
992 `I *' are different types. So, we have to pick a canonical
993 representative. We do this below.
994
995 Technically, this approach is actually more conservative that
996 it needs to be. In particular, `const int *' and `int *'
997 should be in different alias sets, according to the C and C++
998 standard, since their types are not the same, and so,
999 technically, an `int **' and `const int **' cannot point at
1000 the same thing.
1001
1002 But, the standard is wrong. In particular, this code is
1003 legal C++:
1004
1005 int *ip;
1006 int **ipp = &ip;
1007 const int* const* cipp = ipp;
1008 And, it doesn't make sense for that to be legal unless you
1009 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
1010 the pointed-to types. This issue has been reported to the
1011 C++ committee.
1012
1013 For this reason go to canonical type of the unqalified pointer type.
1014 Until GCC 6 this code set all pointers sets to have alias set of
1015 ptr_type_node but that is a bad idea, because it prevents disabiguations
1016 in between pointers. For Firefox this accounts about 20% of all
1017 disambiguations in the program. */
1018 else if (POINTER_TYPE_P (t) && t != ptr_type_node)
1019 {
1020 tree p;
1021 auto_vec <bool, 8> reference;
1022
1023 /* Unnest all pointers and references.
1024 We also want to make pointer to array/vector equivalent to pointer to
1025 its element (see the reasoning above). Skip all those types, too. */
1026 for (p = t; POINTER_TYPE_P (p)
1027 || (TREE_CODE (p) == ARRAY_TYPE
1028 && (!TYPE_NONALIASED_COMPONENT (p)
1029 || !COMPLETE_TYPE_P (p)
1030 || TYPE_STRUCTURAL_EQUALITY_P (p)))
1031 || TREE_CODE (p) == VECTOR_TYPE;
1032 p = TREE_TYPE (p))
1033 {
1034 /* Ada supports recusive pointers. Instead of doing recrusion check
1035 just give up once the preallocated space of 8 elements is up.
1036 In this case just punt to void * alias set. */
1037 if (reference.length () == 8)
1038 {
1039 p = ptr_type_node;
1040 break;
1041 }
1042 if (TREE_CODE (p) == REFERENCE_TYPE)
1043 /* In LTO we want languages that use references to be compatible
1044 with languages that use pointers. */
1045 reference.safe_push (true && !in_lto_p);
1046 if (TREE_CODE (p) == POINTER_TYPE)
1047 reference.safe_push (false);
1048 }
1049 p = TYPE_MAIN_VARIANT (p);
1050
1051 /* In LTO for C++ programs we can turn in complete types to complete
1052 using ODR name lookup. */
1053 if (in_lto_p && TYPE_STRUCTURAL_EQUALITY_P (p) && odr_type_p (p))
1054 {
1055 p = prevailing_odr_type (p);
1056 gcc_checking_assert (TYPE_MAIN_VARIANT (p) == p);
1057 }
1058
1059 /* Make void * compatible with char * and also void **.
1060 Programs are commonly violating TBAA by this.
1061
1062 We also make void * to conflict with every pointer
1063 (see record_component_aliases) and thus it is safe it to use it for
1064 pointers to types with TYPE_STRUCTURAL_EQUALITY_P. */
1065 if (TREE_CODE (p) == VOID_TYPE || TYPE_STRUCTURAL_EQUALITY_P (p))
1066 set = get_alias_set (ptr_type_node);
1067 else
1068 {
1069 /* Rebuild pointer type starting from canonical types using
1070 unqualified pointers and references only. This way all such
1071 pointers will have the same alias set and will conflict with
1072 each other.
1073
1074 Most of time we already have pointers or references of a given type.
1075 If not we build new one just to be sure that if someone later
1076 (probably only middle-end can, as we should assign all alias
1077 classes only after finishing translation unit) builds the pointer
1078 type, the canonical type will match. */
1079 p = TYPE_CANONICAL (p);
1080 while (!reference.is_empty ())
1081 {
1082 if (reference.pop ())
1083 p = build_reference_type (p);
1084 else
1085 p = build_pointer_type (p);
1086 gcc_checking_assert (p == TYPE_MAIN_VARIANT (p));
1087 /* build_pointer_type should always return the canonical type.
1088 For LTO TYPE_CANOINCAL may be NULL, because we do not compute
1089 them. Be sure that frontends do not glob canonical types of
1090 pointers in unexpected way and that p == TYPE_CANONICAL (p)
1091 in all other cases. */
1092 gcc_checking_assert (!TYPE_CANONICAL (p)
1093 || p == TYPE_CANONICAL (p));
1094 }
1095
1096 /* Assign the alias set to both p and t.
1097 We cannot call get_alias_set (p) here as that would trigger
1098 infinite recursion when p == t. In other cases it would just
1099 trigger unnecesary legwork of rebuilding the pointer again. */
1100 gcc_checking_assert (p == TYPE_MAIN_VARIANT (p));
1101 if (TYPE_ALIAS_SET_KNOWN_P (p))
1102 set = TYPE_ALIAS_SET (p);
1103 else
1104 {
1105 set = new_alias_set ();
1106 TYPE_ALIAS_SET (p) = set;
1107 }
1108 }
1109 }
1110 /* Alias set of ptr_type_node is special and serve as universal pointer which
1111 is TBAA compatible with every other pointer type. Be sure we have the
1112 alias set built even for LTO which otherwise keeps all TYPE_CANONICAL
1113 of pointer types NULL. */
1114 else if (t == ptr_type_node)
1115 set = new_alias_set ();
1116
1117 /* Otherwise make a new alias set for this type. */
1118 else
1119 {
1120 /* Each canonical type gets its own alias set, so canonical types
1121 shouldn't form a tree. It doesn't really matter for types
1122 we handle specially above, so only check it where it possibly
1123 would result in a bogus alias set. */
1124 gcc_checking_assert (TYPE_CANONICAL (t) == t);
1125
1126 set = new_alias_set ();
1127 }
1128
1129 TYPE_ALIAS_SET (t) = set;
1130
1131 /* If this is an aggregate type or a complex type, we must record any
1132 component aliasing information. */
1133 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
1134 record_component_aliases (t);
1135
1136 /* We treat pointer types specially in alias_set_subset_of. */
1137 if (POINTER_TYPE_P (t) && set)
1138 {
1139 alias_set_entry *ase = get_alias_set_entry (set);
1140 if (!ase)
1141 ase = init_alias_set_entry (set);
1142 ase->is_pointer = true;
1143 ase->has_pointer = true;
1144 }
1145
1146 return set;
1147 }
1148
1149 /* Return a brand-new alias set. */
1150
1151 alias_set_type
1152 new_alias_set (void)
1153 {
1154 if (alias_sets == 0)
1155 vec_safe_push (alias_sets, (alias_set_entry *) NULL);
1156 vec_safe_push (alias_sets, (alias_set_entry *) NULL);
1157 return alias_sets->length () - 1;
1158 }
1159
1160 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
1161 not everything that aliases SUPERSET also aliases SUBSET. For example,
1162 in C, a store to an `int' can alias a load of a structure containing an
1163 `int', and vice versa. But it can't alias a load of a 'double' member
1164 of the same structure. Here, the structure would be the SUPERSET and
1165 `int' the SUBSET. This relationship is also described in the comment at
1166 the beginning of this file.
1167
1168 This function should be called only once per SUPERSET/SUBSET pair.
1169
1170 It is illegal for SUPERSET to be zero; everything is implicitly a
1171 subset of alias set zero. */
1172
1173 void
1174 record_alias_subset (alias_set_type superset, alias_set_type subset)
1175 {
1176 alias_set_entry *superset_entry;
1177 alias_set_entry *subset_entry;
1178
1179 /* It is possible in complex type situations for both sets to be the same,
1180 in which case we can ignore this operation. */
1181 if (superset == subset)
1182 return;
1183
1184 gcc_assert (superset);
1185
1186 superset_entry = get_alias_set_entry (superset);
1187 if (superset_entry == 0)
1188 {
1189 /* Create an entry for the SUPERSET, so that we have a place to
1190 attach the SUBSET. */
1191 superset_entry = init_alias_set_entry (superset);
1192 }
1193
1194 if (subset == 0)
1195 superset_entry->has_zero_child = 1;
1196 else
1197 {
1198 if (!superset_entry->children)
1199 superset_entry->children
1200 = hash_map<alias_set_hash, int>::create_ggc (64);
1201
1202 /* Enter the SUBSET itself as a child of the SUPERSET. If it was
1203 already there we're done. */
1204 if (superset_entry->children->put (subset, 0))
1205 return;
1206
1207 subset_entry = get_alias_set_entry (subset);
1208 /* If there is an entry for the subset, enter all of its children
1209 (if they are not already present) as children of the SUPERSET. */
1210 if (subset_entry)
1211 {
1212 if (subset_entry->has_zero_child)
1213 superset_entry->has_zero_child = true;
1214 if (subset_entry->has_pointer)
1215 superset_entry->has_pointer = true;
1216
1217 if (subset_entry->children)
1218 {
1219 hash_map<alias_set_hash, int>::iterator iter
1220 = subset_entry->children->begin ();
1221 for (; iter != subset_entry->children->end (); ++iter)
1222 superset_entry->children->put ((*iter).first, (*iter).second);
1223 }
1224 }
1225 }
1226 }
1227
1228 /* Record that component types of TYPE, if any, are part of SUPERSET for
1229 aliasing purposes. For record types, we only record component types
1230 for fields that are not marked non-addressable. For array types, we
1231 only record the component type if it is not marked non-aliased. */
1232
1233 void
1234 record_component_aliases (tree type, alias_set_type superset)
1235 {
1236 tree field;
1237
1238 if (superset == 0)
1239 return;
1240
1241 switch (TREE_CODE (type))
1242 {
1243 case RECORD_TYPE:
1244 case UNION_TYPE:
1245 case QUAL_UNION_TYPE:
1246 {
1247 /* LTO non-ODR type merging does not make any difference between
1248 component pointer types. We may have
1249
1250 struct foo {int *a;};
1251
1252 as TYPE_CANONICAL of
1253
1254 struct bar {float *a;};
1255
1256 Because accesses to int * and float * do not alias, we would get
1257 false negative when accessing the same memory location by
1258 float ** and bar *. We thus record the canonical type as:
1259
1260 struct {void *a;};
1261
1262 void * is special cased and works as a universal pointer type.
1263 Accesses to it conflicts with accesses to any other pointer
1264 type. */
1265 bool void_pointers = in_lto_p
1266 && (!odr_type_p (type)
1267 || !odr_based_tbaa_p (type));
1268 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1269 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1270 {
1271 tree t = TREE_TYPE (field);
1272 if (void_pointers)
1273 {
1274 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1275 element type and that type has to be normalized to void *,
1276 too, in the case it is a pointer. */
1277 while (!canonical_type_used_p (t) && !POINTER_TYPE_P (t))
1278 {
1279 gcc_checking_assert (TYPE_STRUCTURAL_EQUALITY_P (t));
1280 t = TREE_TYPE (t);
1281 }
1282 if (POINTER_TYPE_P (t))
1283 t = ptr_type_node;
1284 else if (flag_checking)
1285 gcc_checking_assert (get_alias_set (t)
1286 == get_alias_set (TREE_TYPE (field)));
1287 }
1288
1289 alias_set_type set = get_alias_set (t);
1290 record_alias_subset (superset, set);
1291 /* If the field has alias-set zero make sure to still record
1292 any componets of it. This makes sure that for
1293 struct A {
1294 struct B {
1295 int i;
1296 char c[4];
1297 } b;
1298 };
1299 in C++ even though 'B' has alias-set zero because
1300 TYPE_TYPELESS_STORAGE is set, 'A' has the alias-set of
1301 'int' as subset. */
1302 if (set == 0)
1303 record_component_aliases (t, superset);
1304 }
1305 }
1306 break;
1307
1308 case COMPLEX_TYPE:
1309 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1310 break;
1311
1312 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1313 element type. */
1314
1315 default:
1316 break;
1317 }
1318 }
1319
1320 /* Record that component types of TYPE, if any, are part of that type for
1321 aliasing purposes. For record types, we only record component types
1322 for fields that are not marked non-addressable. For array types, we
1323 only record the component type if it is not marked non-aliased. */
1324
1325 void
1326 record_component_aliases (tree type)
1327 {
1328 alias_set_type superset = get_alias_set (type);
1329 record_component_aliases (type, superset);
1330 }
1331
1332
1333 /* Allocate an alias set for use in storing and reading from the varargs
1334 spill area. */
1335
1336 static GTY(()) alias_set_type varargs_set = -1;
1337
1338 alias_set_type
1339 get_varargs_alias_set (void)
1340 {
1341 #if 1
1342 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1343 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1344 consistently use the varargs alias set for loads from the varargs
1345 area. So don't use it anywhere. */
1346 return 0;
1347 #else
1348 if (varargs_set == -1)
1349 varargs_set = new_alias_set ();
1350
1351 return varargs_set;
1352 #endif
1353 }
1354
1355 /* Likewise, but used for the fixed portions of the frame, e.g., register
1356 save areas. */
1357
1358 static GTY(()) alias_set_type frame_set = -1;
1359
1360 alias_set_type
1361 get_frame_alias_set (void)
1362 {
1363 if (frame_set == -1)
1364 frame_set = new_alias_set ();
1365
1366 return frame_set;
1367 }
1368
1369 /* Create a new, unique base with id ID. */
1370
1371 static rtx
1372 unique_base_value (HOST_WIDE_INT id)
1373 {
1374 return gen_rtx_ADDRESS (Pmode, id);
1375 }
1376
1377 /* Return true if accesses based on any other base value cannot alias
1378 those based on X. */
1379
1380 static bool
1381 unique_base_value_p (rtx x)
1382 {
1383 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1384 }
1385
1386 /* Return true if X is known to be a base value. */
1387
1388 static bool
1389 known_base_value_p (rtx x)
1390 {
1391 switch (GET_CODE (x))
1392 {
1393 case LABEL_REF:
1394 case SYMBOL_REF:
1395 return true;
1396
1397 case ADDRESS:
1398 /* Arguments may or may not be bases; we don't know for sure. */
1399 return GET_MODE (x) != VOIDmode;
1400
1401 default:
1402 return false;
1403 }
1404 }
1405
1406 /* Inside SRC, the source of a SET, find a base address. */
1407
1408 static rtx
1409 find_base_value (rtx src)
1410 {
1411 unsigned int regno;
1412 scalar_int_mode int_mode;
1413
1414 #if defined (FIND_BASE_TERM)
1415 /* Try machine-dependent ways to find the base term. */
1416 src = FIND_BASE_TERM (src);
1417 #endif
1418
1419 switch (GET_CODE (src))
1420 {
1421 case SYMBOL_REF:
1422 case LABEL_REF:
1423 return src;
1424
1425 case REG:
1426 regno = REGNO (src);
1427 /* At the start of a function, argument registers have known base
1428 values which may be lost later. Returning an ADDRESS
1429 expression here allows optimization based on argument values
1430 even when the argument registers are used for other purposes. */
1431 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1432 return new_reg_base_value[regno];
1433
1434 /* If a pseudo has a known base value, return it. Do not do this
1435 for non-fixed hard regs since it can result in a circular
1436 dependency chain for registers which have values at function entry.
1437
1438 The test above is not sufficient because the scheduler may move
1439 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1440 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1441 && regno < vec_safe_length (reg_base_value))
1442 {
1443 /* If we're inside init_alias_analysis, use new_reg_base_value
1444 to reduce the number of relaxation iterations. */
1445 if (new_reg_base_value && new_reg_base_value[regno]
1446 && DF_REG_DEF_COUNT (regno) == 1)
1447 return new_reg_base_value[regno];
1448
1449 if ((*reg_base_value)[regno])
1450 return (*reg_base_value)[regno];
1451 }
1452
1453 return 0;
1454
1455 case MEM:
1456 /* Check for an argument passed in memory. Only record in the
1457 copying-arguments block; it is too hard to track changes
1458 otherwise. */
1459 if (copying_arguments
1460 && (XEXP (src, 0) == arg_pointer_rtx
1461 || (GET_CODE (XEXP (src, 0)) == PLUS
1462 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1463 return arg_base_value;
1464 return 0;
1465
1466 case CONST:
1467 src = XEXP (src, 0);
1468 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1469 break;
1470
1471 /* fall through */
1472
1473 case PLUS:
1474 case MINUS:
1475 {
1476 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1477
1478 /* If either operand is a REG that is a known pointer, then it
1479 is the base. */
1480 if (REG_P (src_0) && REG_POINTER (src_0))
1481 return find_base_value (src_0);
1482 if (REG_P (src_1) && REG_POINTER (src_1))
1483 return find_base_value (src_1);
1484
1485 /* If either operand is a REG, then see if we already have
1486 a known value for it. */
1487 if (REG_P (src_0))
1488 {
1489 temp = find_base_value (src_0);
1490 if (temp != 0)
1491 src_0 = temp;
1492 }
1493
1494 if (REG_P (src_1))
1495 {
1496 temp = find_base_value (src_1);
1497 if (temp!= 0)
1498 src_1 = temp;
1499 }
1500
1501 /* If either base is named object or a special address
1502 (like an argument or stack reference), then use it for the
1503 base term. */
1504 if (src_0 != 0 && known_base_value_p (src_0))
1505 return src_0;
1506
1507 if (src_1 != 0 && known_base_value_p (src_1))
1508 return src_1;
1509
1510 /* Guess which operand is the base address:
1511 If either operand is a symbol, then it is the base. If
1512 either operand is a CONST_INT, then the other is the base. */
1513 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1514 return find_base_value (src_0);
1515 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1516 return find_base_value (src_1);
1517
1518 return 0;
1519 }
1520
1521 case LO_SUM:
1522 /* The standard form is (lo_sum reg sym) so look only at the
1523 second operand. */
1524 return find_base_value (XEXP (src, 1));
1525
1526 case AND:
1527 /* Look through aligning ANDs. And AND with zero or one with
1528 the LSB set isn't one (see for example PR92462). */
1529 if (CONST_INT_P (XEXP (src, 1))
1530 && INTVAL (XEXP (src, 1)) != 0
1531 && (INTVAL (XEXP (src, 1)) & 1) == 0)
1532 return find_base_value (XEXP (src, 0));
1533 return 0;
1534
1535 case TRUNCATE:
1536 /* As we do not know which address space the pointer is referring to, we can
1537 handle this only if the target does not support different pointer or
1538 address modes depending on the address space. */
1539 if (!target_default_pointer_address_modes_p ())
1540 break;
1541 if (!is_a <scalar_int_mode> (GET_MODE (src), &int_mode)
1542 || GET_MODE_PRECISION (int_mode) < GET_MODE_PRECISION (Pmode))
1543 break;
1544 /* Fall through. */
1545 case HIGH:
1546 case PRE_INC:
1547 case PRE_DEC:
1548 case POST_INC:
1549 case POST_DEC:
1550 case PRE_MODIFY:
1551 case POST_MODIFY:
1552 return find_base_value (XEXP (src, 0));
1553
1554 case ZERO_EXTEND:
1555 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1556 /* As we do not know which address space the pointer is referring to, we can
1557 handle this only if the target does not support different pointer or
1558 address modes depending on the address space. */
1559 if (!target_default_pointer_address_modes_p ())
1560 break;
1561
1562 {
1563 rtx temp = find_base_value (XEXP (src, 0));
1564
1565 if (temp != 0 && CONSTANT_P (temp))
1566 temp = convert_memory_address (Pmode, temp);
1567
1568 return temp;
1569 }
1570
1571 default:
1572 break;
1573 }
1574
1575 return 0;
1576 }
1577
1578 /* Called from init_alias_analysis indirectly through note_stores,
1579 or directly if DEST is a register with a REG_NOALIAS note attached.
1580 SET is null in the latter case. */
1581
1582 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1583 register N has been set in this function. */
1584 static sbitmap reg_seen;
1585
1586 static void
1587 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1588 {
1589 unsigned regno;
1590 rtx src;
1591 int n;
1592
1593 if (!REG_P (dest))
1594 return;
1595
1596 regno = REGNO (dest);
1597
1598 gcc_checking_assert (regno < reg_base_value->length ());
1599
1600 n = REG_NREGS (dest);
1601 if (n != 1)
1602 {
1603 while (--n >= 0)
1604 {
1605 bitmap_set_bit (reg_seen, regno + n);
1606 new_reg_base_value[regno + n] = 0;
1607 }
1608 return;
1609 }
1610
1611 if (set)
1612 {
1613 /* A CLOBBER wipes out any old value but does not prevent a previously
1614 unset register from acquiring a base address (i.e. reg_seen is not
1615 set). */
1616 if (GET_CODE (set) == CLOBBER)
1617 {
1618 new_reg_base_value[regno] = 0;
1619 return;
1620 }
1621
1622 src = SET_SRC (set);
1623 }
1624 else
1625 {
1626 /* There's a REG_NOALIAS note against DEST. */
1627 if (bitmap_bit_p (reg_seen, regno))
1628 {
1629 new_reg_base_value[regno] = 0;
1630 return;
1631 }
1632 bitmap_set_bit (reg_seen, regno);
1633 new_reg_base_value[regno] = unique_base_value (unique_id++);
1634 return;
1635 }
1636
1637 /* If this is not the first set of REGNO, see whether the new value
1638 is related to the old one. There are two cases of interest:
1639
1640 (1) The register might be assigned an entirely new value
1641 that has the same base term as the original set.
1642
1643 (2) The set might be a simple self-modification that
1644 cannot change REGNO's base value.
1645
1646 If neither case holds, reject the original base value as invalid.
1647 Note that the following situation is not detected:
1648
1649 extern int x, y; int *p = &x; p += (&y-&x);
1650
1651 ANSI C does not allow computing the difference of addresses
1652 of distinct top level objects. */
1653 if (new_reg_base_value[regno] != 0
1654 && find_base_value (src) != new_reg_base_value[regno])
1655 switch (GET_CODE (src))
1656 {
1657 case LO_SUM:
1658 case MINUS:
1659 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1660 new_reg_base_value[regno] = 0;
1661 break;
1662 case PLUS:
1663 /* If the value we add in the PLUS is also a valid base value,
1664 this might be the actual base value, and the original value
1665 an index. */
1666 {
1667 rtx other = NULL_RTX;
1668
1669 if (XEXP (src, 0) == dest)
1670 other = XEXP (src, 1);
1671 else if (XEXP (src, 1) == dest)
1672 other = XEXP (src, 0);
1673
1674 if (! other || find_base_value (other))
1675 new_reg_base_value[regno] = 0;
1676 break;
1677 }
1678 case AND:
1679 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1680 new_reg_base_value[regno] = 0;
1681 break;
1682 default:
1683 new_reg_base_value[regno] = 0;
1684 break;
1685 }
1686 /* If this is the first set of a register, record the value. */
1687 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1688 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1689 new_reg_base_value[regno] = find_base_value (src);
1690
1691 bitmap_set_bit (reg_seen, regno);
1692 }
1693
1694 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1695 using hard registers with non-null REG_BASE_VALUE for renaming. */
1696 rtx
1697 get_reg_base_value (unsigned int regno)
1698 {
1699 return (*reg_base_value)[regno];
1700 }
1701
1702 /* If a value is known for REGNO, return it. */
1703
1704 rtx
1705 get_reg_known_value (unsigned int regno)
1706 {
1707 if (regno >= FIRST_PSEUDO_REGISTER)
1708 {
1709 regno -= FIRST_PSEUDO_REGISTER;
1710 if (regno < vec_safe_length (reg_known_value))
1711 return (*reg_known_value)[regno];
1712 }
1713 return NULL;
1714 }
1715
1716 /* Set it. */
1717
1718 static void
1719 set_reg_known_value (unsigned int regno, rtx val)
1720 {
1721 if (regno >= FIRST_PSEUDO_REGISTER)
1722 {
1723 regno -= FIRST_PSEUDO_REGISTER;
1724 if (regno < vec_safe_length (reg_known_value))
1725 (*reg_known_value)[regno] = val;
1726 }
1727 }
1728
1729 /* Similarly for reg_known_equiv_p. */
1730
1731 bool
1732 get_reg_known_equiv_p (unsigned int regno)
1733 {
1734 if (regno >= FIRST_PSEUDO_REGISTER)
1735 {
1736 regno -= FIRST_PSEUDO_REGISTER;
1737 if (regno < vec_safe_length (reg_known_value))
1738 return bitmap_bit_p (reg_known_equiv_p, regno);
1739 }
1740 return false;
1741 }
1742
1743 static void
1744 set_reg_known_equiv_p (unsigned int regno, bool val)
1745 {
1746 if (regno >= FIRST_PSEUDO_REGISTER)
1747 {
1748 regno -= FIRST_PSEUDO_REGISTER;
1749 if (regno < vec_safe_length (reg_known_value))
1750 {
1751 if (val)
1752 bitmap_set_bit (reg_known_equiv_p, regno);
1753 else
1754 bitmap_clear_bit (reg_known_equiv_p, regno);
1755 }
1756 }
1757 }
1758
1759
1760 /* Returns a canonical version of X, from the point of view alias
1761 analysis. (For example, if X is a MEM whose address is a register,
1762 and the register has a known value (say a SYMBOL_REF), then a MEM
1763 whose address is the SYMBOL_REF is returned.) */
1764
1765 rtx
1766 canon_rtx (rtx x)
1767 {
1768 /* Recursively look for equivalences. */
1769 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1770 {
1771 rtx t = get_reg_known_value (REGNO (x));
1772 if (t == x)
1773 return x;
1774 if (t)
1775 return canon_rtx (t);
1776 }
1777
1778 if (GET_CODE (x) == PLUS)
1779 {
1780 rtx x0 = canon_rtx (XEXP (x, 0));
1781 rtx x1 = canon_rtx (XEXP (x, 1));
1782
1783 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1784 return simplify_gen_binary (PLUS, GET_MODE (x), x0, x1);
1785 }
1786
1787 /* This gives us much better alias analysis when called from
1788 the loop optimizer. Note we want to leave the original
1789 MEM alone, but need to return the canonicalized MEM with
1790 all the flags with their original values. */
1791 else if (MEM_P (x))
1792 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1793
1794 return x;
1795 }
1796
1797 /* Return 1 if X and Y are identical-looking rtx's.
1798 Expect that X and Y has been already canonicalized.
1799
1800 We use the data in reg_known_value above to see if two registers with
1801 different numbers are, in fact, equivalent. */
1802
1803 static int
1804 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1805 {
1806 int i;
1807 int j;
1808 enum rtx_code code;
1809 const char *fmt;
1810
1811 if (x == 0 && y == 0)
1812 return 1;
1813 if (x == 0 || y == 0)
1814 return 0;
1815
1816 if (x == y)
1817 return 1;
1818
1819 code = GET_CODE (x);
1820 /* Rtx's of different codes cannot be equal. */
1821 if (code != GET_CODE (y))
1822 return 0;
1823
1824 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1825 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1826
1827 if (GET_MODE (x) != GET_MODE (y))
1828 return 0;
1829
1830 /* Some RTL can be compared without a recursive examination. */
1831 switch (code)
1832 {
1833 case REG:
1834 return REGNO (x) == REGNO (y);
1835
1836 case LABEL_REF:
1837 return label_ref_label (x) == label_ref_label (y);
1838
1839 case SYMBOL_REF:
1840 return compare_base_symbol_refs (x, y) == 1;
1841
1842 case ENTRY_VALUE:
1843 /* This is magic, don't go through canonicalization et al. */
1844 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1845
1846 case VALUE:
1847 CASE_CONST_UNIQUE:
1848 /* Pointer equality guarantees equality for these nodes. */
1849 return 0;
1850
1851 default:
1852 break;
1853 }
1854
1855 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1856 if (code == PLUS)
1857 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1858 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1859 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1860 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1861 /* For commutative operations, the RTX match if the operand match in any
1862 order. Also handle the simple binary and unary cases without a loop. */
1863 if (COMMUTATIVE_P (x))
1864 {
1865 rtx xop0 = canon_rtx (XEXP (x, 0));
1866 rtx yop0 = canon_rtx (XEXP (y, 0));
1867 rtx yop1 = canon_rtx (XEXP (y, 1));
1868
1869 return ((rtx_equal_for_memref_p (xop0, yop0)
1870 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1871 || (rtx_equal_for_memref_p (xop0, yop1)
1872 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1873 }
1874 else if (NON_COMMUTATIVE_P (x))
1875 {
1876 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1877 canon_rtx (XEXP (y, 0)))
1878 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1879 canon_rtx (XEXP (y, 1))));
1880 }
1881 else if (UNARY_P (x))
1882 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1883 canon_rtx (XEXP (y, 0)));
1884
1885 /* Compare the elements. If any pair of corresponding elements
1886 fail to match, return 0 for the whole things.
1887
1888 Limit cases to types which actually appear in addresses. */
1889
1890 fmt = GET_RTX_FORMAT (code);
1891 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1892 {
1893 switch (fmt[i])
1894 {
1895 case 'i':
1896 if (XINT (x, i) != XINT (y, i))
1897 return 0;
1898 break;
1899
1900 case 'p':
1901 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
1902 return 0;
1903 break;
1904
1905 case 'E':
1906 /* Two vectors must have the same length. */
1907 if (XVECLEN (x, i) != XVECLEN (y, i))
1908 return 0;
1909
1910 /* And the corresponding elements must match. */
1911 for (j = 0; j < XVECLEN (x, i); j++)
1912 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1913 canon_rtx (XVECEXP (y, i, j))) == 0)
1914 return 0;
1915 break;
1916
1917 case 'e':
1918 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1919 canon_rtx (XEXP (y, i))) == 0)
1920 return 0;
1921 break;
1922
1923 /* This can happen for asm operands. */
1924 case 's':
1925 if (strcmp (XSTR (x, i), XSTR (y, i)))
1926 return 0;
1927 break;
1928
1929 /* This can happen for an asm which clobbers memory. */
1930 case '0':
1931 break;
1932
1933 /* It is believed that rtx's at this level will never
1934 contain anything but integers and other rtx's,
1935 except for within LABEL_REFs and SYMBOL_REFs. */
1936 default:
1937 gcc_unreachable ();
1938 }
1939 }
1940 return 1;
1941 }
1942
1943 static rtx
1944 find_base_term (rtx x, vec<std::pair<cselib_val *,
1945 struct elt_loc_list *> > &visited_vals)
1946 {
1947 cselib_val *val;
1948 struct elt_loc_list *l, *f;
1949 rtx ret;
1950 scalar_int_mode int_mode;
1951
1952 #if defined (FIND_BASE_TERM)
1953 /* Try machine-dependent ways to find the base term. */
1954 x = FIND_BASE_TERM (x);
1955 #endif
1956
1957 switch (GET_CODE (x))
1958 {
1959 case REG:
1960 return REG_BASE_VALUE (x);
1961
1962 case TRUNCATE:
1963 /* As we do not know which address space the pointer is referring to, we can
1964 handle this only if the target does not support different pointer or
1965 address modes depending on the address space. */
1966 if (!target_default_pointer_address_modes_p ())
1967 return 0;
1968 if (!is_a <scalar_int_mode> (GET_MODE (x), &int_mode)
1969 || GET_MODE_PRECISION (int_mode) < GET_MODE_PRECISION (Pmode))
1970 return 0;
1971 /* Fall through. */
1972 case HIGH:
1973 case PRE_INC:
1974 case PRE_DEC:
1975 case POST_INC:
1976 case POST_DEC:
1977 case PRE_MODIFY:
1978 case POST_MODIFY:
1979 return find_base_term (XEXP (x, 0), visited_vals);
1980
1981 case ZERO_EXTEND:
1982 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1983 /* As we do not know which address space the pointer is referring to, we can
1984 handle this only if the target does not support different pointer or
1985 address modes depending on the address space. */
1986 if (!target_default_pointer_address_modes_p ())
1987 return 0;
1988
1989 {
1990 rtx temp = find_base_term (XEXP (x, 0), visited_vals);
1991
1992 if (temp != 0 && CONSTANT_P (temp))
1993 temp = convert_memory_address (Pmode, temp);
1994
1995 return temp;
1996 }
1997
1998 case VALUE:
1999 val = CSELIB_VAL_PTR (x);
2000 ret = NULL_RTX;
2001
2002 if (!val)
2003 return ret;
2004
2005 if (cselib_sp_based_value_p (val))
2006 return static_reg_base_value[STACK_POINTER_REGNUM];
2007
2008 f = val->locs;
2009 /* Reset val->locs to avoid infinite recursion. */
2010 if (f)
2011 visited_vals.safe_push (std::make_pair (val, f));
2012 val->locs = NULL;
2013
2014 for (l = f; l; l = l->next)
2015 if (GET_CODE (l->loc) == VALUE
2016 && CSELIB_VAL_PTR (l->loc)->locs
2017 && !CSELIB_VAL_PTR (l->loc)->locs->next
2018 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
2019 continue;
2020 else if ((ret = find_base_term (l->loc, visited_vals)) != 0)
2021 break;
2022
2023 return ret;
2024
2025 case LO_SUM:
2026 /* The standard form is (lo_sum reg sym) so look only at the
2027 second operand. */
2028 return find_base_term (XEXP (x, 1), visited_vals);
2029
2030 case CONST:
2031 x = XEXP (x, 0);
2032 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
2033 return 0;
2034 /* Fall through. */
2035 case PLUS:
2036 case MINUS:
2037 {
2038 rtx tmp1 = XEXP (x, 0);
2039 rtx tmp2 = XEXP (x, 1);
2040
2041 /* This is a little bit tricky since we have to determine which of
2042 the two operands represents the real base address. Otherwise this
2043 routine may return the index register instead of the base register.
2044
2045 That may cause us to believe no aliasing was possible, when in
2046 fact aliasing is possible.
2047
2048 We use a few simple tests to guess the base register. Additional
2049 tests can certainly be added. For example, if one of the operands
2050 is a shift or multiply, then it must be the index register and the
2051 other operand is the base register. */
2052
2053 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
2054 return find_base_term (tmp2, visited_vals);
2055
2056 /* If either operand is known to be a pointer, then prefer it
2057 to determine the base term. */
2058 if (REG_P (tmp1) && REG_POINTER (tmp1))
2059 ;
2060 else if (REG_P (tmp2) && REG_POINTER (tmp2))
2061 std::swap (tmp1, tmp2);
2062 /* If second argument is constant which has base term, prefer it
2063 over variable tmp1. See PR64025. */
2064 else if (CONSTANT_P (tmp2) && !CONST_INT_P (tmp2))
2065 std::swap (tmp1, tmp2);
2066
2067 /* Go ahead and find the base term for both operands. If either base
2068 term is from a pointer or is a named object or a special address
2069 (like an argument or stack reference), then use it for the
2070 base term. */
2071 rtx base = find_base_term (tmp1, visited_vals);
2072 if (base != NULL_RTX
2073 && ((REG_P (tmp1) && REG_POINTER (tmp1))
2074 || known_base_value_p (base)))
2075 return base;
2076 base = find_base_term (tmp2, visited_vals);
2077 if (base != NULL_RTX
2078 && ((REG_P (tmp2) && REG_POINTER (tmp2))
2079 || known_base_value_p (base)))
2080 return base;
2081
2082 /* We could not determine which of the two operands was the
2083 base register and which was the index. So we can determine
2084 nothing from the base alias check. */
2085 return 0;
2086 }
2087
2088 case AND:
2089 /* Look through aligning ANDs. And AND with zero or one with
2090 the LSB set isn't one (see for example PR92462). */
2091 if (CONST_INT_P (XEXP (x, 1))
2092 && INTVAL (XEXP (x, 1)) != 0
2093 && (INTVAL (XEXP (x, 1)) & 1) == 0)
2094 return find_base_term (XEXP (x, 0), visited_vals);
2095 return 0;
2096
2097 case SYMBOL_REF:
2098 case LABEL_REF:
2099 return x;
2100
2101 default:
2102 return 0;
2103 }
2104 }
2105
2106 /* Wrapper around the worker above which removes locs from visited VALUEs
2107 to avoid visiting them multiple times. We unwind that changes here. */
2108
2109 static rtx
2110 find_base_term (rtx x)
2111 {
2112 auto_vec<std::pair<cselib_val *, struct elt_loc_list *>, 32> visited_vals;
2113 rtx res = find_base_term (x, visited_vals);
2114 for (unsigned i = 0; i < visited_vals.length (); ++i)
2115 visited_vals[i].first->locs = visited_vals[i].second;
2116 return res;
2117 }
2118
2119 /* Return true if accesses to address X may alias accesses based
2120 on the stack pointer. */
2121
2122 bool
2123 may_be_sp_based_p (rtx x)
2124 {
2125 rtx base = find_base_term (x);
2126 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
2127 }
2128
2129 /* BASE1 and BASE2 are decls. Return 1 if they refer to same object, 0
2130 if they refer to different objects and -1 if we cannot decide. */
2131
2132 int
2133 compare_base_decls (tree base1, tree base2)
2134 {
2135 int ret;
2136 gcc_checking_assert (DECL_P (base1) && DECL_P (base2));
2137 if (base1 == base2)
2138 return 1;
2139
2140 /* If we have two register decls with register specification we
2141 cannot decide unless their assembler names are the same. */
2142 if (DECL_REGISTER (base1)
2143 && DECL_REGISTER (base2)
2144 && HAS_DECL_ASSEMBLER_NAME_P (base1)
2145 && HAS_DECL_ASSEMBLER_NAME_P (base2)
2146 && DECL_ASSEMBLER_NAME_SET_P (base1)
2147 && DECL_ASSEMBLER_NAME_SET_P (base2))
2148 {
2149 if (DECL_ASSEMBLER_NAME_RAW (base1) == DECL_ASSEMBLER_NAME_RAW (base2))
2150 return 1;
2151 return -1;
2152 }
2153
2154 /* Declarations of non-automatic variables may have aliases. All other
2155 decls are unique. */
2156 if (!decl_in_symtab_p (base1)
2157 || !decl_in_symtab_p (base2))
2158 return 0;
2159
2160 /* Don't cause symbols to be inserted by the act of checking. */
2161 symtab_node *node1 = symtab_node::get (base1);
2162 if (!node1)
2163 return 0;
2164 symtab_node *node2 = symtab_node::get (base2);
2165 if (!node2)
2166 return 0;
2167
2168 ret = node1->equal_address_to (node2, true);
2169 return ret;
2170 }
2171
2172 /* Same as compare_base_decls but for SYMBOL_REF. */
2173
2174 static int
2175 compare_base_symbol_refs (const_rtx x_base, const_rtx y_base)
2176 {
2177 tree x_decl = SYMBOL_REF_DECL (x_base);
2178 tree y_decl = SYMBOL_REF_DECL (y_base);
2179 bool binds_def = true;
2180
2181 if (XSTR (x_base, 0) == XSTR (y_base, 0))
2182 return 1;
2183 if (x_decl && y_decl)
2184 return compare_base_decls (x_decl, y_decl);
2185 if (x_decl || y_decl)
2186 {
2187 if (!x_decl)
2188 {
2189 std::swap (x_decl, y_decl);
2190 std::swap (x_base, y_base);
2191 }
2192 /* We handle specially only section anchors and assume that other
2193 labels may overlap with user variables in an arbitrary way. */
2194 if (!SYMBOL_REF_HAS_BLOCK_INFO_P (y_base))
2195 return -1;
2196 /* Anchors contains static VAR_DECLs and CONST_DECLs. We are safe
2197 to ignore CONST_DECLs because they are readonly. */
2198 if (!VAR_P (x_decl)
2199 || (!TREE_STATIC (x_decl) && !TREE_PUBLIC (x_decl)))
2200 return 0;
2201
2202 symtab_node *x_node = symtab_node::get_create (x_decl)
2203 ->ultimate_alias_target ();
2204 /* External variable cannot be in section anchor. */
2205 if (!x_node->definition)
2206 return 0;
2207 x_base = XEXP (DECL_RTL (x_node->decl), 0);
2208 /* If not in anchor, we can disambiguate. */
2209 if (!SYMBOL_REF_HAS_BLOCK_INFO_P (x_base))
2210 return 0;
2211
2212 /* We have an alias of anchored variable. If it can be interposed;
2213 we must assume it may or may not alias its anchor. */
2214 binds_def = decl_binds_to_current_def_p (x_decl);
2215 }
2216 /* If we have variable in section anchor, we can compare by offset. */
2217 if (SYMBOL_REF_HAS_BLOCK_INFO_P (x_base)
2218 && SYMBOL_REF_HAS_BLOCK_INFO_P (y_base))
2219 {
2220 if (SYMBOL_REF_BLOCK (x_base) != SYMBOL_REF_BLOCK (y_base))
2221 return 0;
2222 if (SYMBOL_REF_BLOCK_OFFSET (x_base) == SYMBOL_REF_BLOCK_OFFSET (y_base))
2223 return binds_def ? 1 : -1;
2224 if (SYMBOL_REF_ANCHOR_P (x_base) != SYMBOL_REF_ANCHOR_P (y_base))
2225 return -1;
2226 return 0;
2227 }
2228 /* In general we assume that memory locations pointed to by different labels
2229 may overlap in undefined ways. */
2230 return -1;
2231 }
2232
2233 /* Return 0 if the addresses X and Y are known to point to different
2234 objects, 1 if they might be pointers to the same object. */
2235
2236 static int
2237 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
2238 machine_mode x_mode, machine_mode y_mode)
2239 {
2240 /* If the address itself has no known base see if a known equivalent
2241 value has one. If either address still has no known base, nothing
2242 is known about aliasing. */
2243 if (x_base == 0)
2244 {
2245 rtx x_c;
2246
2247 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
2248 return 1;
2249
2250 x_base = find_base_term (x_c);
2251 if (x_base == 0)
2252 return 1;
2253 }
2254
2255 if (y_base == 0)
2256 {
2257 rtx y_c;
2258 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
2259 return 1;
2260
2261 y_base = find_base_term (y_c);
2262 if (y_base == 0)
2263 return 1;
2264 }
2265
2266 /* If the base addresses are equal nothing is known about aliasing. */
2267 if (rtx_equal_p (x_base, y_base))
2268 return 1;
2269
2270 /* The base addresses are different expressions. If they are not accessed
2271 via AND, there is no conflict. We can bring knowledge of object
2272 alignment into play here. For example, on alpha, "char a, b;" can
2273 alias one another, though "char a; long b;" cannot. AND addresses may
2274 implicitly alias surrounding objects; i.e. unaligned access in DImode
2275 via AND address can alias all surrounding object types except those
2276 with aligment 8 or higher. */
2277 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
2278 return 1;
2279 if (GET_CODE (x) == AND
2280 && (!CONST_INT_P (XEXP (x, 1))
2281 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
2282 return 1;
2283 if (GET_CODE (y) == AND
2284 && (!CONST_INT_P (XEXP (y, 1))
2285 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
2286 return 1;
2287
2288 /* Differing symbols not accessed via AND never alias. */
2289 if (GET_CODE (x_base) == SYMBOL_REF && GET_CODE (y_base) == SYMBOL_REF)
2290 return compare_base_symbol_refs (x_base, y_base) != 0;
2291
2292 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
2293 return 0;
2294
2295 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
2296 return 0;
2297
2298 return 1;
2299 }
2300
2301 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
2302 (or equal to) that of V. */
2303
2304 static bool
2305 refs_newer_value_p (const_rtx expr, rtx v)
2306 {
2307 int minuid = CSELIB_VAL_PTR (v)->uid;
2308 subrtx_iterator::array_type array;
2309 FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
2310 if (GET_CODE (*iter) == VALUE && CSELIB_VAL_PTR (*iter)->uid >= minuid)
2311 return true;
2312 return false;
2313 }
2314
2315 /* Convert the address X into something we can use. This is done by returning
2316 it unchanged unless it is a VALUE or VALUE +/- constant; for VALUE
2317 we call cselib to get a more useful rtx. */
2318
2319 rtx
2320 get_addr (rtx x)
2321 {
2322 cselib_val *v;
2323 struct elt_loc_list *l;
2324
2325 if (GET_CODE (x) != VALUE)
2326 {
2327 if ((GET_CODE (x) == PLUS || GET_CODE (x) == MINUS)
2328 && GET_CODE (XEXP (x, 0)) == VALUE
2329 && CONST_SCALAR_INT_P (XEXP (x, 1)))
2330 {
2331 rtx op0 = get_addr (XEXP (x, 0));
2332 if (op0 != XEXP (x, 0))
2333 {
2334 poly_int64 c;
2335 if (GET_CODE (x) == PLUS
2336 && poly_int_rtx_p (XEXP (x, 1), &c))
2337 return plus_constant (GET_MODE (x), op0, c);
2338 return simplify_gen_binary (GET_CODE (x), GET_MODE (x),
2339 op0, XEXP (x, 1));
2340 }
2341 }
2342 return x;
2343 }
2344 v = CSELIB_VAL_PTR (x);
2345 if (v)
2346 {
2347 bool have_equivs = cselib_have_permanent_equivalences ();
2348 if (have_equivs)
2349 v = canonical_cselib_val (v);
2350 for (l = v->locs; l; l = l->next)
2351 if (CONSTANT_P (l->loc))
2352 return l->loc;
2353 for (l = v->locs; l; l = l->next)
2354 if (!REG_P (l->loc) && !MEM_P (l->loc)
2355 /* Avoid infinite recursion when potentially dealing with
2356 var-tracking artificial equivalences, by skipping the
2357 equivalences themselves, and not choosing expressions
2358 that refer to newer VALUEs. */
2359 && (!have_equivs
2360 || (GET_CODE (l->loc) != VALUE
2361 && !refs_newer_value_p (l->loc, x))))
2362 return l->loc;
2363 if (have_equivs)
2364 {
2365 for (l = v->locs; l; l = l->next)
2366 if (REG_P (l->loc)
2367 || (GET_CODE (l->loc) != VALUE
2368 && !refs_newer_value_p (l->loc, x)))
2369 return l->loc;
2370 /* Return the canonical value. */
2371 return v->val_rtx;
2372 }
2373 if (v->locs)
2374 return v->locs->loc;
2375 }
2376 return x;
2377 }
2378
2379 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
2380 where SIZE is the size in bytes of the memory reference. If ADDR
2381 is not modified by the memory reference then ADDR is returned. */
2382
2383 static rtx
2384 addr_side_effect_eval (rtx addr, poly_int64 size, int n_refs)
2385 {
2386 poly_int64 offset = 0;
2387
2388 switch (GET_CODE (addr))
2389 {
2390 case PRE_INC:
2391 offset = (n_refs + 1) * size;
2392 break;
2393 case PRE_DEC:
2394 offset = -(n_refs + 1) * size;
2395 break;
2396 case POST_INC:
2397 offset = n_refs * size;
2398 break;
2399 case POST_DEC:
2400 offset = -n_refs * size;
2401 break;
2402
2403 default:
2404 return addr;
2405 }
2406
2407 addr = plus_constant (GET_MODE (addr), XEXP (addr, 0), offset);
2408 addr = canon_rtx (addr);
2409
2410 return addr;
2411 }
2412
2413 /* Return TRUE if an object X sized at XSIZE bytes and another object
2414 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
2415 any of the sizes is zero, assume an overlap, otherwise use the
2416 absolute value of the sizes as the actual sizes. */
2417
2418 static inline bool
2419 offset_overlap_p (poly_int64 c, poly_int64 xsize, poly_int64 ysize)
2420 {
2421 if (known_eq (xsize, 0) || known_eq (ysize, 0))
2422 return true;
2423
2424 if (maybe_ge (c, 0))
2425 return maybe_gt (maybe_lt (xsize, 0) ? -xsize : xsize, c);
2426 else
2427 return maybe_gt (maybe_lt (ysize, 0) ? -ysize : ysize, -c);
2428 }
2429
2430 /* Return one if X and Y (memory addresses) reference the
2431 same location in memory or if the references overlap.
2432 Return zero if they do not overlap, else return
2433 minus one in which case they still might reference the same location.
2434
2435 C is an offset accumulator. When
2436 C is nonzero, we are testing aliases between X and Y + C.
2437 XSIZE is the size in bytes of the X reference,
2438 similarly YSIZE is the size in bytes for Y.
2439 Expect that canon_rtx has been already called for X and Y.
2440
2441 If XSIZE or YSIZE is zero, we do not know the amount of memory being
2442 referenced (the reference was BLKmode), so make the most pessimistic
2443 assumptions.
2444
2445 If XSIZE or YSIZE is negative, we may access memory outside the object
2446 being referenced as a side effect. This can happen when using AND to
2447 align memory references, as is done on the Alpha.
2448
2449 Nice to notice that varying addresses cannot conflict with fp if no
2450 local variables had their addresses taken, but that's too hard now.
2451
2452 ??? Contrary to the tree alias oracle this does not return
2453 one for X + non-constant and Y + non-constant when X and Y are equal.
2454 If that is fixed the TBAA hack for union type-punning can be removed. */
2455
2456 static int
2457 memrefs_conflict_p (poly_int64 xsize, rtx x, poly_int64 ysize, rtx y,
2458 poly_int64 c)
2459 {
2460 if (GET_CODE (x) == VALUE)
2461 {
2462 if (REG_P (y))
2463 {
2464 struct elt_loc_list *l = NULL;
2465 if (CSELIB_VAL_PTR (x))
2466 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2467 l; l = l->next)
2468 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2469 break;
2470 if (l)
2471 x = y;
2472 else
2473 x = get_addr (x);
2474 }
2475 /* Don't call get_addr if y is the same VALUE. */
2476 else if (x != y)
2477 x = get_addr (x);
2478 }
2479 if (GET_CODE (y) == VALUE)
2480 {
2481 if (REG_P (x))
2482 {
2483 struct elt_loc_list *l = NULL;
2484 if (CSELIB_VAL_PTR (y))
2485 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2486 l; l = l->next)
2487 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2488 break;
2489 if (l)
2490 y = x;
2491 else
2492 y = get_addr (y);
2493 }
2494 /* Don't call get_addr if x is the same VALUE. */
2495 else if (y != x)
2496 y = get_addr (y);
2497 }
2498 if (GET_CODE (x) == HIGH)
2499 x = XEXP (x, 0);
2500 else if (GET_CODE (x) == LO_SUM)
2501 x = XEXP (x, 1);
2502 else
2503 x = addr_side_effect_eval (x, maybe_lt (xsize, 0) ? -xsize : xsize, 0);
2504 if (GET_CODE (y) == HIGH)
2505 y = XEXP (y, 0);
2506 else if (GET_CODE (y) == LO_SUM)
2507 y = XEXP (y, 1);
2508 else
2509 y = addr_side_effect_eval (y, maybe_lt (ysize, 0) ? -ysize : ysize, 0);
2510
2511 if (GET_CODE (x) == SYMBOL_REF && GET_CODE (y) == SYMBOL_REF)
2512 {
2513 int cmp = compare_base_symbol_refs (x,y);
2514
2515 /* If both decls are the same, decide by offsets. */
2516 if (cmp == 1)
2517 return offset_overlap_p (c, xsize, ysize);
2518 /* Assume a potential overlap for symbolic addresses that went
2519 through alignment adjustments (i.e., that have negative
2520 sizes), because we can't know how far they are from each
2521 other. */
2522 if (maybe_lt (xsize, 0) || maybe_lt (ysize, 0))
2523 return -1;
2524 /* If decls are different or we know by offsets that there is no overlap,
2525 we win. */
2526 if (!cmp || !offset_overlap_p (c, xsize, ysize))
2527 return 0;
2528 /* Decls may or may not be different and offsets overlap....*/
2529 return -1;
2530 }
2531 else if (rtx_equal_for_memref_p (x, y))
2532 {
2533 return offset_overlap_p (c, xsize, ysize);
2534 }
2535
2536 /* This code used to check for conflicts involving stack references and
2537 globals but the base address alias code now handles these cases. */
2538
2539 if (GET_CODE (x) == PLUS)
2540 {
2541 /* The fact that X is canonicalized means that this
2542 PLUS rtx is canonicalized. */
2543 rtx x0 = XEXP (x, 0);
2544 rtx x1 = XEXP (x, 1);
2545
2546 /* However, VALUEs might end up in different positions even in
2547 canonical PLUSes. Comparing their addresses is enough. */
2548 if (x0 == y)
2549 return memrefs_conflict_p (xsize, x1, ysize, const0_rtx, c);
2550 else if (x1 == y)
2551 return memrefs_conflict_p (xsize, x0, ysize, const0_rtx, c);
2552
2553 poly_int64 cx1, cy1;
2554 if (GET_CODE (y) == PLUS)
2555 {
2556 /* The fact that Y is canonicalized means that this
2557 PLUS rtx is canonicalized. */
2558 rtx y0 = XEXP (y, 0);
2559 rtx y1 = XEXP (y, 1);
2560
2561 if (x0 == y1)
2562 return memrefs_conflict_p (xsize, x1, ysize, y0, c);
2563 if (x1 == y0)
2564 return memrefs_conflict_p (xsize, x0, ysize, y1, c);
2565
2566 if (rtx_equal_for_memref_p (x1, y1))
2567 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2568 if (rtx_equal_for_memref_p (x0, y0))
2569 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2570 if (poly_int_rtx_p (x1, &cx1))
2571 {
2572 if (poly_int_rtx_p (y1, &cy1))
2573 return memrefs_conflict_p (xsize, x0, ysize, y0,
2574 c - cx1 + cy1);
2575 else
2576 return memrefs_conflict_p (xsize, x0, ysize, y, c - cx1);
2577 }
2578 else if (poly_int_rtx_p (y1, &cy1))
2579 return memrefs_conflict_p (xsize, x, ysize, y0, c + cy1);
2580
2581 return -1;
2582 }
2583 else if (poly_int_rtx_p (x1, &cx1))
2584 return memrefs_conflict_p (xsize, x0, ysize, y, c - cx1);
2585 }
2586 else if (GET_CODE (y) == PLUS)
2587 {
2588 /* The fact that Y is canonicalized means that this
2589 PLUS rtx is canonicalized. */
2590 rtx y0 = XEXP (y, 0);
2591 rtx y1 = XEXP (y, 1);
2592
2593 if (x == y0)
2594 return memrefs_conflict_p (xsize, const0_rtx, ysize, y1, c);
2595 if (x == y1)
2596 return memrefs_conflict_p (xsize, const0_rtx, ysize, y0, c);
2597
2598 poly_int64 cy1;
2599 if (poly_int_rtx_p (y1, &cy1))
2600 return memrefs_conflict_p (xsize, x, ysize, y0, c + cy1);
2601 else
2602 return -1;
2603 }
2604
2605 if (GET_CODE (x) == GET_CODE (y))
2606 switch (GET_CODE (x))
2607 {
2608 case MULT:
2609 {
2610 /* Handle cases where we expect the second operands to be the
2611 same, and check only whether the first operand would conflict
2612 or not. */
2613 rtx x0, y0;
2614 rtx x1 = canon_rtx (XEXP (x, 1));
2615 rtx y1 = canon_rtx (XEXP (y, 1));
2616 if (! rtx_equal_for_memref_p (x1, y1))
2617 return -1;
2618 x0 = canon_rtx (XEXP (x, 0));
2619 y0 = canon_rtx (XEXP (y, 0));
2620 if (rtx_equal_for_memref_p (x0, y0))
2621 return offset_overlap_p (c, xsize, ysize);
2622
2623 /* Can't properly adjust our sizes. */
2624 poly_int64 c1;
2625 if (!poly_int_rtx_p (x1, &c1)
2626 || !can_div_trunc_p (xsize, c1, &xsize)
2627 || !can_div_trunc_p (ysize, c1, &ysize)
2628 || !can_div_trunc_p (c, c1, &c))
2629 return -1;
2630 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2631 }
2632
2633 default:
2634 break;
2635 }
2636
2637 /* Deal with alignment ANDs by adjusting offset and size so as to
2638 cover the maximum range, without taking any previously known
2639 alignment into account. Make a size negative after such an
2640 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2641 assume a potential overlap, because they may end up in contiguous
2642 memory locations and the stricter-alignment access may span over
2643 part of both. */
2644 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2645 {
2646 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2647 unsigned HOST_WIDE_INT uc = sc;
2648 if (sc < 0 && pow2_or_zerop (-uc))
2649 {
2650 if (maybe_gt (xsize, 0))
2651 xsize = -xsize;
2652 if (maybe_ne (xsize, 0))
2653 xsize += sc + 1;
2654 c -= sc + 1;
2655 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2656 ysize, y, c);
2657 }
2658 }
2659 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2660 {
2661 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2662 unsigned HOST_WIDE_INT uc = sc;
2663 if (sc < 0 && pow2_or_zerop (-uc))
2664 {
2665 if (maybe_gt (ysize, 0))
2666 ysize = -ysize;
2667 if (maybe_ne (ysize, 0))
2668 ysize += sc + 1;
2669 c += sc + 1;
2670 return memrefs_conflict_p (xsize, x,
2671 ysize, canon_rtx (XEXP (y, 0)), c);
2672 }
2673 }
2674
2675 if (CONSTANT_P (x))
2676 {
2677 poly_int64 cx, cy;
2678 if (poly_int_rtx_p (x, &cx) && poly_int_rtx_p (y, &cy))
2679 {
2680 c += cy - cx;
2681 return offset_overlap_p (c, xsize, ysize);
2682 }
2683
2684 if (GET_CODE (x) == CONST)
2685 {
2686 if (GET_CODE (y) == CONST)
2687 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2688 ysize, canon_rtx (XEXP (y, 0)), c);
2689 else
2690 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2691 ysize, y, c);
2692 }
2693 if (GET_CODE (y) == CONST)
2694 return memrefs_conflict_p (xsize, x, ysize,
2695 canon_rtx (XEXP (y, 0)), c);
2696
2697 /* Assume a potential overlap for symbolic addresses that went
2698 through alignment adjustments (i.e., that have negative
2699 sizes), because we can't know how far they are from each
2700 other. */
2701 if (CONSTANT_P (y))
2702 return (maybe_lt (xsize, 0)
2703 || maybe_lt (ysize, 0)
2704 || offset_overlap_p (c, xsize, ysize));
2705
2706 return -1;
2707 }
2708
2709 return -1;
2710 }
2711
2712 /* Functions to compute memory dependencies.
2713
2714 Since we process the insns in execution order, we can build tables
2715 to keep track of what registers are fixed (and not aliased), what registers
2716 are varying in known ways, and what registers are varying in unknown
2717 ways.
2718
2719 If both memory references are volatile, then there must always be a
2720 dependence between the two references, since their order cannot be
2721 changed. A volatile and non-volatile reference can be interchanged
2722 though.
2723
2724 We also must allow AND addresses, because they may generate accesses
2725 outside the object being referenced. This is used to generate aligned
2726 addresses from unaligned addresses, for instance, the alpha
2727 storeqi_unaligned pattern. */
2728
2729 /* Read dependence: X is read after read in MEM takes place. There can
2730 only be a dependence here if both reads are volatile, or if either is
2731 an explicit barrier. */
2732
2733 int
2734 read_dependence (const_rtx mem, const_rtx x)
2735 {
2736 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2737 return true;
2738 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2739 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2740 return true;
2741 return false;
2742 }
2743
2744 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2745
2746 static tree
2747 decl_for_component_ref (tree x)
2748 {
2749 do
2750 {
2751 x = TREE_OPERAND (x, 0);
2752 }
2753 while (x && TREE_CODE (x) == COMPONENT_REF);
2754
2755 return x && DECL_P (x) ? x : NULL_TREE;
2756 }
2757
2758 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2759 for the offset of the field reference. *KNOWN_P says whether the
2760 offset is known. */
2761
2762 static void
2763 adjust_offset_for_component_ref (tree x, bool *known_p,
2764 poly_int64 *offset)
2765 {
2766 if (!*known_p)
2767 return;
2768 do
2769 {
2770 tree xoffset = component_ref_field_offset (x);
2771 tree field = TREE_OPERAND (x, 1);
2772 if (!poly_int_tree_p (xoffset))
2773 {
2774 *known_p = false;
2775 return;
2776 }
2777
2778 poly_offset_int woffset
2779 = (wi::to_poly_offset (xoffset)
2780 + (wi::to_offset (DECL_FIELD_BIT_OFFSET (field))
2781 >> LOG2_BITS_PER_UNIT)
2782 + *offset);
2783 if (!woffset.to_shwi (offset))
2784 {
2785 *known_p = false;
2786 return;
2787 }
2788
2789 x = TREE_OPERAND (x, 0);
2790 }
2791 while (x && TREE_CODE (x) == COMPONENT_REF);
2792 }
2793
2794 /* Return nonzero if we can determine the exprs corresponding to memrefs
2795 X and Y and they do not overlap.
2796 If LOOP_VARIANT is set, skip offset-based disambiguation */
2797
2798 int
2799 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2800 {
2801 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2802 rtx rtlx, rtly;
2803 rtx basex, basey;
2804 bool moffsetx_known_p, moffsety_known_p;
2805 poly_int64 moffsetx = 0, moffsety = 0;
2806 poly_int64 offsetx = 0, offsety = 0, sizex, sizey;
2807
2808 /* Unless both have exprs, we can't tell anything. */
2809 if (exprx == 0 || expry == 0)
2810 return 0;
2811
2812 /* For spill-slot accesses make sure we have valid offsets. */
2813 if ((exprx == get_spill_slot_decl (false)
2814 && ! MEM_OFFSET_KNOWN_P (x))
2815 || (expry == get_spill_slot_decl (false)
2816 && ! MEM_OFFSET_KNOWN_P (y)))
2817 return 0;
2818
2819 /* If the field reference test failed, look at the DECLs involved. */
2820 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2821 if (moffsetx_known_p)
2822 moffsetx = MEM_OFFSET (x);
2823 if (TREE_CODE (exprx) == COMPONENT_REF)
2824 {
2825 tree t = decl_for_component_ref (exprx);
2826 if (! t)
2827 return 0;
2828 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2829 exprx = t;
2830 }
2831
2832 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2833 if (moffsety_known_p)
2834 moffsety = MEM_OFFSET (y);
2835 if (TREE_CODE (expry) == COMPONENT_REF)
2836 {
2837 tree t = decl_for_component_ref (expry);
2838 if (! t)
2839 return 0;
2840 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2841 expry = t;
2842 }
2843
2844 if (! DECL_P (exprx) || ! DECL_P (expry))
2845 return 0;
2846
2847 /* If we refer to different gimple registers, or one gimple register
2848 and one non-gimple-register, we know they can't overlap. First,
2849 gimple registers don't have their addresses taken. Now, there
2850 could be more than one stack slot for (different versions of) the
2851 same gimple register, but we can presumably tell they don't
2852 overlap based on offsets from stack base addresses elsewhere.
2853 It's important that we don't proceed to DECL_RTL, because gimple
2854 registers may not pass DECL_RTL_SET_P, and make_decl_rtl won't be
2855 able to do anything about them since no SSA information will have
2856 remained to guide it. */
2857 if (is_gimple_reg (exprx) || is_gimple_reg (expry))
2858 return exprx != expry
2859 || (moffsetx_known_p && moffsety_known_p
2860 && MEM_SIZE_KNOWN_P (x) && MEM_SIZE_KNOWN_P (y)
2861 && !offset_overlap_p (moffsety - moffsetx,
2862 MEM_SIZE (x), MEM_SIZE (y)));
2863
2864 /* With invalid code we can end up storing into the constant pool.
2865 Bail out to avoid ICEing when creating RTL for this.
2866 See gfortran.dg/lto/20091028-2_0.f90. */
2867 if (TREE_CODE (exprx) == CONST_DECL
2868 || TREE_CODE (expry) == CONST_DECL)
2869 return 1;
2870
2871 /* If one decl is known to be a function or label in a function and
2872 the other is some kind of data, they can't overlap. */
2873 if ((TREE_CODE (exprx) == FUNCTION_DECL
2874 || TREE_CODE (exprx) == LABEL_DECL)
2875 != (TREE_CODE (expry) == FUNCTION_DECL
2876 || TREE_CODE (expry) == LABEL_DECL))
2877 return 1;
2878
2879 /* If either of the decls doesn't have DECL_RTL set (e.g. marked as
2880 living in multiple places), we can't tell anything. Exception
2881 are FUNCTION_DECLs for which we can create DECL_RTL on demand. */
2882 if ((!DECL_RTL_SET_P (exprx) && TREE_CODE (exprx) != FUNCTION_DECL)
2883 || (!DECL_RTL_SET_P (expry) && TREE_CODE (expry) != FUNCTION_DECL))
2884 return 0;
2885
2886 rtlx = DECL_RTL (exprx);
2887 rtly = DECL_RTL (expry);
2888
2889 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2890 can't overlap unless they are the same because we never reuse that part
2891 of the stack frame used for locals for spilled pseudos. */
2892 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2893 && ! rtx_equal_p (rtlx, rtly))
2894 return 1;
2895
2896 /* If we have MEMs referring to different address spaces (which can
2897 potentially overlap), we cannot easily tell from the addresses
2898 whether the references overlap. */
2899 if (MEM_P (rtlx) && MEM_P (rtly)
2900 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2901 return 0;
2902
2903 /* Get the base and offsets of both decls. If either is a register, we
2904 know both are and are the same, so use that as the base. The only
2905 we can avoid overlap is if we can deduce that they are nonoverlapping
2906 pieces of that decl, which is very rare. */
2907 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2908 basex = strip_offset_and_add (basex, &offsetx);
2909
2910 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2911 basey = strip_offset_and_add (basey, &offsety);
2912
2913 /* If the bases are different, we know they do not overlap if both
2914 are constants or if one is a constant and the other a pointer into the
2915 stack frame. Otherwise a different base means we can't tell if they
2916 overlap or not. */
2917 if (compare_base_decls (exprx, expry) == 0)
2918 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2919 || (CONSTANT_P (basex) && REG_P (basey)
2920 && REGNO_PTR_FRAME_P (REGNO (basey)))
2921 || (CONSTANT_P (basey) && REG_P (basex)
2922 && REGNO_PTR_FRAME_P (REGNO (basex))));
2923
2924 /* Offset based disambiguation not appropriate for loop invariant */
2925 if (loop_invariant)
2926 return 0;
2927
2928 /* Offset based disambiguation is OK even if we do not know that the
2929 declarations are necessarily different
2930 (i.e. compare_base_decls (exprx, expry) == -1) */
2931
2932 sizex = (!MEM_P (rtlx) ? poly_int64 (GET_MODE_SIZE (GET_MODE (rtlx)))
2933 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2934 : -1);
2935 sizey = (!MEM_P (rtly) ? poly_int64 (GET_MODE_SIZE (GET_MODE (rtly)))
2936 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2937 : -1);
2938
2939 /* If we have an offset for either memref, it can update the values computed
2940 above. */
2941 if (moffsetx_known_p)
2942 offsetx += moffsetx, sizex -= moffsetx;
2943 if (moffsety_known_p)
2944 offsety += moffsety, sizey -= moffsety;
2945
2946 /* If a memref has both a size and an offset, we can use the smaller size.
2947 We can't do this if the offset isn't known because we must view this
2948 memref as being anywhere inside the DECL's MEM. */
2949 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2950 sizex = MEM_SIZE (x);
2951 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2952 sizey = MEM_SIZE (y);
2953
2954 return !ranges_maybe_overlap_p (offsetx, sizex, offsety, sizey);
2955 }
2956
2957 /* Helper for true_dependence and canon_true_dependence.
2958 Checks for true dependence: X is read after store in MEM takes place.
2959
2960 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2961 NULL_RTX, and the canonical addresses of MEM and X are both computed
2962 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2963
2964 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2965
2966 Returns 1 if there is a true dependence, 0 otherwise. */
2967
2968 static int
2969 true_dependence_1 (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2970 const_rtx x, rtx x_addr, bool mem_canonicalized)
2971 {
2972 rtx true_mem_addr;
2973 rtx base;
2974 int ret;
2975
2976 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2977 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2978
2979 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2980 return 1;
2981
2982 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2983 This is used in epilogue deallocation functions, and in cselib. */
2984 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2985 return 1;
2986 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2987 return 1;
2988 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2989 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2990 return 1;
2991
2992 if (! x_addr)
2993 x_addr = XEXP (x, 0);
2994 x_addr = get_addr (x_addr);
2995
2996 if (! mem_addr)
2997 {
2998 mem_addr = XEXP (mem, 0);
2999 if (mem_mode == VOIDmode)
3000 mem_mode = GET_MODE (mem);
3001 }
3002 true_mem_addr = get_addr (mem_addr);
3003
3004 /* Read-only memory is by definition never modified, and therefore can't
3005 conflict with anything. However, don't assume anything when AND
3006 addresses are involved and leave to the code below to determine
3007 dependence. We don't expect to find read-only set on MEM, but
3008 stupid user tricks can produce them, so don't die. */
3009 if (MEM_READONLY_P (x)
3010 && GET_CODE (x_addr) != AND
3011 && GET_CODE (true_mem_addr) != AND)
3012 return 0;
3013
3014 /* If we have MEMs referring to different address spaces (which can
3015 potentially overlap), we cannot easily tell from the addresses
3016 whether the references overlap. */
3017 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
3018 return 1;
3019
3020 base = find_base_term (x_addr);
3021 if (base && (GET_CODE (base) == LABEL_REF
3022 || (GET_CODE (base) == SYMBOL_REF
3023 && CONSTANT_POOL_ADDRESS_P (base))))
3024 return 0;
3025
3026 rtx mem_base = find_base_term (true_mem_addr);
3027 if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
3028 GET_MODE (x), mem_mode))
3029 return 0;
3030
3031 x_addr = canon_rtx (x_addr);
3032 if (!mem_canonicalized)
3033 mem_addr = canon_rtx (true_mem_addr);
3034
3035 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
3036 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
3037 return ret;
3038
3039 if (mems_in_disjoint_alias_sets_p (x, mem))
3040 return 0;
3041
3042 if (nonoverlapping_memrefs_p (mem, x, false))
3043 return 0;
3044
3045 return rtx_refs_may_alias_p (x, mem, true);
3046 }
3047
3048 /* True dependence: X is read after store in MEM takes place. */
3049
3050 int
3051 true_dependence (const_rtx mem, machine_mode mem_mode, const_rtx x)
3052 {
3053 return true_dependence_1 (mem, mem_mode, NULL_RTX,
3054 x, NULL_RTX, /*mem_canonicalized=*/false);
3055 }
3056
3057 /* Canonical true dependence: X is read after store in MEM takes place.
3058 Variant of true_dependence which assumes MEM has already been
3059 canonicalized (hence we no longer do that here).
3060 The mem_addr argument has been added, since true_dependence_1 computed
3061 this value prior to canonicalizing. */
3062
3063 int
3064 canon_true_dependence (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
3065 const_rtx x, rtx x_addr)
3066 {
3067 return true_dependence_1 (mem, mem_mode, mem_addr,
3068 x, x_addr, /*mem_canonicalized=*/true);
3069 }
3070
3071 /* Returns nonzero if a write to X might alias a previous read from
3072 (or, if WRITEP is true, a write to) MEM.
3073 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
3074 and X_MODE the mode for that access.
3075 If MEM_CANONICALIZED is true, MEM is canonicalized. */
3076
3077 static int
3078 write_dependence_p (const_rtx mem,
3079 const_rtx x, machine_mode x_mode, rtx x_addr,
3080 bool mem_canonicalized, bool x_canonicalized, bool writep)
3081 {
3082 rtx mem_addr;
3083 rtx true_mem_addr, true_x_addr;
3084 rtx base;
3085 int ret;
3086
3087 gcc_checking_assert (x_canonicalized
3088 ? (x_addr != NULL_RTX
3089 && (x_mode != VOIDmode || GET_MODE (x) == VOIDmode))
3090 : (x_addr == NULL_RTX && x_mode == VOIDmode));
3091
3092 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
3093 return 1;
3094
3095 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
3096 This is used in epilogue deallocation functions. */
3097 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
3098 return 1;
3099 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3100 return 1;
3101 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
3102 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
3103 return 1;
3104
3105 if (!x_addr)
3106 x_addr = XEXP (x, 0);
3107 true_x_addr = get_addr (x_addr);
3108
3109 mem_addr = XEXP (mem, 0);
3110 true_mem_addr = get_addr (mem_addr);
3111
3112 /* A read from read-only memory can't conflict with read-write memory.
3113 Don't assume anything when AND addresses are involved and leave to
3114 the code below to determine dependence. */
3115 if (!writep
3116 && MEM_READONLY_P (mem)
3117 && GET_CODE (true_x_addr) != AND
3118 && GET_CODE (true_mem_addr) != AND)
3119 return 0;
3120
3121 /* If we have MEMs referring to different address spaces (which can
3122 potentially overlap), we cannot easily tell from the addresses
3123 whether the references overlap. */
3124 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
3125 return 1;
3126
3127 base = find_base_term (true_mem_addr);
3128 if (! writep
3129 && base
3130 && (GET_CODE (base) == LABEL_REF
3131 || (GET_CODE (base) == SYMBOL_REF
3132 && CONSTANT_POOL_ADDRESS_P (base))))
3133 return 0;
3134
3135 rtx x_base = find_base_term (true_x_addr);
3136 if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
3137 GET_MODE (x), GET_MODE (mem)))
3138 return 0;
3139
3140 if (!x_canonicalized)
3141 {
3142 x_addr = canon_rtx (true_x_addr);
3143 x_mode = GET_MODE (x);
3144 }
3145 if (!mem_canonicalized)
3146 mem_addr = canon_rtx (true_mem_addr);
3147
3148 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
3149 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
3150 return ret;
3151
3152 if (nonoverlapping_memrefs_p (x, mem, false))
3153 return 0;
3154
3155 return rtx_refs_may_alias_p (x, mem, false);
3156 }
3157
3158 /* Anti dependence: X is written after read in MEM takes place. */
3159
3160 int
3161 anti_dependence (const_rtx mem, const_rtx x)
3162 {
3163 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
3164 /*mem_canonicalized=*/false,
3165 /*x_canonicalized*/false, /*writep=*/false);
3166 }
3167
3168 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
3169 Also, consider X in X_MODE (which might be from an enclosing
3170 STRICT_LOW_PART / ZERO_EXTRACT).
3171 If MEM_CANONICALIZED is true, MEM is canonicalized. */
3172
3173 int
3174 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
3175 const_rtx x, machine_mode x_mode, rtx x_addr)
3176 {
3177 return write_dependence_p (mem, x, x_mode, x_addr,
3178 mem_canonicalized, /*x_canonicalized=*/true,
3179 /*writep=*/false);
3180 }
3181
3182 /* Output dependence: X is written after store in MEM takes place. */
3183
3184 int
3185 output_dependence (const_rtx mem, const_rtx x)
3186 {
3187 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
3188 /*mem_canonicalized=*/false,
3189 /*x_canonicalized*/false, /*writep=*/true);
3190 }
3191
3192 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
3193 Also, consider X in X_MODE (which might be from an enclosing
3194 STRICT_LOW_PART / ZERO_EXTRACT).
3195 If MEM_CANONICALIZED is true, MEM is canonicalized. */
3196
3197 int
3198 canon_output_dependence (const_rtx mem, bool mem_canonicalized,
3199 const_rtx x, machine_mode x_mode, rtx x_addr)
3200 {
3201 return write_dependence_p (mem, x, x_mode, x_addr,
3202 mem_canonicalized, /*x_canonicalized=*/true,
3203 /*writep=*/true);
3204 }
3205 \f
3206
3207
3208 /* Check whether X may be aliased with MEM. Don't do offset-based
3209 memory disambiguation & TBAA. */
3210 int
3211 may_alias_p (const_rtx mem, const_rtx x)
3212 {
3213 rtx x_addr, mem_addr;
3214
3215 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
3216 return 1;
3217
3218 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
3219 This is used in epilogue deallocation functions. */
3220 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
3221 return 1;
3222 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
3223 return 1;
3224 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
3225 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
3226 return 1;
3227
3228 x_addr = XEXP (x, 0);
3229 x_addr = get_addr (x_addr);
3230
3231 mem_addr = XEXP (mem, 0);
3232 mem_addr = get_addr (mem_addr);
3233
3234 /* Read-only memory is by definition never modified, and therefore can't
3235 conflict with anything. However, don't assume anything when AND
3236 addresses are involved and leave to the code below to determine
3237 dependence. We don't expect to find read-only set on MEM, but
3238 stupid user tricks can produce them, so don't die. */
3239 if (MEM_READONLY_P (x)
3240 && GET_CODE (x_addr) != AND
3241 && GET_CODE (mem_addr) != AND)
3242 return 0;
3243
3244 /* If we have MEMs referring to different address spaces (which can
3245 potentially overlap), we cannot easily tell from the addresses
3246 whether the references overlap. */
3247 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
3248 return 1;
3249
3250 rtx x_base = find_base_term (x_addr);
3251 rtx mem_base = find_base_term (mem_addr);
3252 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
3253 GET_MODE (x), GET_MODE (mem_addr)))
3254 return 0;
3255
3256 if (nonoverlapping_memrefs_p (mem, x, true))
3257 return 0;
3258
3259 /* TBAA not valid for loop_invarint */
3260 return rtx_refs_may_alias_p (x, mem, false);
3261 }
3262
3263 void
3264 init_alias_target (void)
3265 {
3266 int i;
3267
3268 if (!arg_base_value)
3269 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
3270
3271 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
3272
3273 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3274 /* Check whether this register can hold an incoming pointer
3275 argument. FUNCTION_ARG_REGNO_P tests outgoing register
3276 numbers, so translate if necessary due to register windows. */
3277 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
3278 && targetm.hard_regno_mode_ok (i, Pmode))
3279 static_reg_base_value[i] = arg_base_value;
3280
3281 /* RTL code is required to be consistent about whether it uses the
3282 stack pointer, the frame pointer or the argument pointer to
3283 access a given area of the frame. We can therefore use the
3284 base address to distinguish between the different areas. */
3285 static_reg_base_value[STACK_POINTER_REGNUM]
3286 = unique_base_value (UNIQUE_BASE_VALUE_SP);
3287 static_reg_base_value[ARG_POINTER_REGNUM]
3288 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
3289 static_reg_base_value[FRAME_POINTER_REGNUM]
3290 = unique_base_value (UNIQUE_BASE_VALUE_FP);
3291
3292 /* The above rules extend post-reload, with eliminations applying
3293 consistently to each of the three pointers. Cope with cases in
3294 which the frame pointer is eliminated to the hard frame pointer
3295 rather than the stack pointer. */
3296 if (!HARD_FRAME_POINTER_IS_FRAME_POINTER)
3297 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
3298 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
3299 }
3300
3301 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
3302 to be memory reference. */
3303 static bool memory_modified;
3304 static void
3305 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
3306 {
3307 if (MEM_P (x))
3308 {
3309 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
3310 memory_modified = true;
3311 }
3312 }
3313
3314
3315 /* Return true when INSN possibly modify memory contents of MEM
3316 (i.e. address can be modified). */
3317 bool
3318 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
3319 {
3320 if (!INSN_P (insn))
3321 return false;
3322 /* Conservatively assume all non-readonly MEMs might be modified in
3323 calls. */
3324 if (CALL_P (insn))
3325 return true;
3326 memory_modified = false;
3327 note_stores (as_a<const rtx_insn *> (insn), memory_modified_1,
3328 CONST_CAST_RTX(mem));
3329 return memory_modified;
3330 }
3331
3332 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
3333 array. */
3334
3335 void
3336 init_alias_analysis (void)
3337 {
3338 unsigned int maxreg = max_reg_num ();
3339 int changed, pass;
3340 int i;
3341 unsigned int ui;
3342 rtx_insn *insn;
3343 rtx val;
3344 int rpo_cnt;
3345 int *rpo;
3346
3347 timevar_push (TV_ALIAS_ANALYSIS);
3348
3349 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
3350 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
3351 bitmap_clear (reg_known_equiv_p);
3352
3353 /* If we have memory allocated from the previous run, use it. */
3354 if (old_reg_base_value)
3355 reg_base_value = old_reg_base_value;
3356
3357 if (reg_base_value)
3358 reg_base_value->truncate (0);
3359
3360 vec_safe_grow_cleared (reg_base_value, maxreg);
3361
3362 new_reg_base_value = XNEWVEC (rtx, maxreg);
3363 reg_seen = sbitmap_alloc (maxreg);
3364
3365 /* The basic idea is that each pass through this loop will use the
3366 "constant" information from the previous pass to propagate alias
3367 information through another level of assignments.
3368
3369 The propagation is done on the CFG in reverse post-order, to propagate
3370 things forward as far as possible in each iteration.
3371
3372 This could get expensive if the assignment chains are long. Maybe
3373 we should throttle the number of iterations, possibly based on
3374 the optimization level or flag_expensive_optimizations.
3375
3376 We could propagate more information in the first pass by making use
3377 of DF_REG_DEF_COUNT to determine immediately that the alias information
3378 for a pseudo is "constant".
3379
3380 A program with an uninitialized variable can cause an infinite loop
3381 here. Instead of doing a full dataflow analysis to detect such problems
3382 we just cap the number of iterations for the loop.
3383
3384 The state of the arrays for the set chain in question does not matter
3385 since the program has undefined behavior. */
3386
3387 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3388 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
3389
3390 /* The prologue/epilogue insns are not threaded onto the
3391 insn chain until after reload has completed. Thus,
3392 there is no sense wasting time checking if INSN is in
3393 the prologue/epilogue until after reload has completed. */
3394 bool could_be_prologue_epilogue = ((targetm.have_prologue ()
3395 || targetm.have_epilogue ())
3396 && reload_completed);
3397
3398 pass = 0;
3399 do
3400 {
3401 /* Assume nothing will change this iteration of the loop. */
3402 changed = 0;
3403
3404 /* We want to assign the same IDs each iteration of this loop, so
3405 start counting from one each iteration of the loop. */
3406 unique_id = 1;
3407
3408 /* We're at the start of the function each iteration through the
3409 loop, so we're copying arguments. */
3410 copying_arguments = true;
3411
3412 /* Wipe the potential alias information clean for this pass. */
3413 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
3414
3415 /* Wipe the reg_seen array clean. */
3416 bitmap_clear (reg_seen);
3417
3418 /* Initialize the alias information for this pass. */
3419 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
3420 if (static_reg_base_value[i]
3421 /* Don't treat the hard frame pointer as special if we
3422 eliminated the frame pointer to the stack pointer instead. */
3423 && !(i == HARD_FRAME_POINTER_REGNUM
3424 && reload_completed
3425 && !frame_pointer_needed
3426 && targetm.can_eliminate (FRAME_POINTER_REGNUM,
3427 STACK_POINTER_REGNUM)))
3428 {
3429 new_reg_base_value[i] = static_reg_base_value[i];
3430 bitmap_set_bit (reg_seen, i);
3431 }
3432
3433 /* Walk the insns adding values to the new_reg_base_value array. */
3434 for (i = 0; i < rpo_cnt; i++)
3435 {
3436 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
3437 FOR_BB_INSNS (bb, insn)
3438 {
3439 if (NONDEBUG_INSN_P (insn))
3440 {
3441 rtx note, set;
3442
3443 if (could_be_prologue_epilogue
3444 && prologue_epilogue_contains (insn))
3445 continue;
3446
3447 /* If this insn has a noalias note, process it, Otherwise,
3448 scan for sets. A simple set will have no side effects
3449 which could change the base value of any other register. */
3450
3451 if (GET_CODE (PATTERN (insn)) == SET
3452 && REG_NOTES (insn) != 0
3453 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
3454 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
3455 else
3456 note_stores (insn, record_set, NULL);
3457
3458 set = single_set (insn);
3459
3460 if (set != 0
3461 && REG_P (SET_DEST (set))
3462 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
3463 {
3464 unsigned int regno = REGNO (SET_DEST (set));
3465 rtx src = SET_SRC (set);
3466 rtx t;
3467
3468 note = find_reg_equal_equiv_note (insn);
3469 if (note && REG_NOTE_KIND (note) == REG_EQUAL
3470 && DF_REG_DEF_COUNT (regno) != 1)
3471 note = NULL_RTX;
3472
3473 poly_int64 offset;
3474 if (note != NULL_RTX
3475 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
3476 && ! rtx_varies_p (XEXP (note, 0), 1)
3477 && ! reg_overlap_mentioned_p (SET_DEST (set),
3478 XEXP (note, 0)))
3479 {
3480 set_reg_known_value (regno, XEXP (note, 0));
3481 set_reg_known_equiv_p (regno,
3482 REG_NOTE_KIND (note) == REG_EQUIV);
3483 }
3484 else if (DF_REG_DEF_COUNT (regno) == 1
3485 && GET_CODE (src) == PLUS
3486 && REG_P (XEXP (src, 0))
3487 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
3488 && poly_int_rtx_p (XEXP (src, 1), &offset))
3489 {
3490 t = plus_constant (GET_MODE (src), t, offset);
3491 set_reg_known_value (regno, t);
3492 set_reg_known_equiv_p (regno, false);
3493 }
3494 else if (DF_REG_DEF_COUNT (regno) == 1
3495 && ! rtx_varies_p (src, 1))
3496 {
3497 set_reg_known_value (regno, src);
3498 set_reg_known_equiv_p (regno, false);
3499 }
3500 }
3501 }
3502 else if (NOTE_P (insn)
3503 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
3504 copying_arguments = false;
3505 }
3506 }
3507
3508 /* Now propagate values from new_reg_base_value to reg_base_value. */
3509 gcc_assert (maxreg == (unsigned int) max_reg_num ());
3510
3511 for (ui = 0; ui < maxreg; ui++)
3512 {
3513 if (new_reg_base_value[ui]
3514 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3515 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3516 {
3517 (*reg_base_value)[ui] = new_reg_base_value[ui];
3518 changed = 1;
3519 }
3520 }
3521 }
3522 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3523 XDELETEVEC (rpo);
3524
3525 /* Fill in the remaining entries. */
3526 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3527 {
3528 int regno = i + FIRST_PSEUDO_REGISTER;
3529 if (! val)
3530 set_reg_known_value (regno, regno_reg_rtx[regno]);
3531 }
3532
3533 /* Clean up. */
3534 free (new_reg_base_value);
3535 new_reg_base_value = 0;
3536 sbitmap_free (reg_seen);
3537 reg_seen = 0;
3538 timevar_pop (TV_ALIAS_ANALYSIS);
3539 }
3540
3541 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3542 Special API for var-tracking pass purposes. */
3543
3544 void
3545 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3546 {
3547 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3548 }
3549
3550 void
3551 end_alias_analysis (void)
3552 {
3553 old_reg_base_value = reg_base_value;
3554 vec_free (reg_known_value);
3555 sbitmap_free (reg_known_equiv_p);
3556 }
3557
3558 void
3559 dump_alias_stats_in_alias_c (FILE *s)
3560 {
3561 fprintf (s, " TBAA oracle: %llu disambiguations %llu queries\n"
3562 " %llu are in alias set 0\n"
3563 " %llu queries asked about the same object\n"
3564 " %llu queries asked about the same alias set\n"
3565 " %llu access volatile\n"
3566 " %llu are dependent in the DAG\n"
3567 " %llu are aritificially in conflict with void *\n",
3568 alias_stats.num_disambiguated,
3569 alias_stats.num_alias_zero + alias_stats.num_same_alias_set
3570 + alias_stats.num_same_objects + alias_stats.num_volatile
3571 + alias_stats.num_dag + alias_stats.num_disambiguated
3572 + alias_stats.num_universal,
3573 alias_stats.num_alias_zero, alias_stats.num_same_alias_set,
3574 alias_stats.num_same_objects, alias_stats.num_volatile,
3575 alias_stats.num_dag, alias_stats.num_universal);
3576 }
3577 #include "gt-alias.h"