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