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