* gcc.dg/store-motion-fgcse-sm.c (dg-final): Cleanup
[official-gcc.git] / gcc / alias.c
blob425eb0be72195fcb7aac647209a588ac971d53c8
1 /* Alias analysis for GNU C
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
3 Contributed by John Carr (jfc@mit.edu).
5 This file is part of GCC.
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.
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.
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/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "rtl.h"
26 #include "tree.h"
27 #include "varasm.h"
28 #include "expr.h"
29 #include "tm_p.h"
30 #include "hashtab.h"
31 #include "hash-set.h"
32 #include "vec.h"
33 #include "machmode.h"
34 #include "hard-reg-set.h"
35 #include "input.h"
36 #include "function.h"
37 #include "alias.h"
38 #include "emit-rtl.h"
39 #include "regs.h"
40 #include "flags.h"
41 #include "diagnostic-core.h"
42 #include "cselib.h"
43 #include "hash-map.h"
44 #include "langhooks.h"
45 #include "timevar.h"
46 #include "dumpfile.h"
47 #include "target.h"
48 #include "dominance.h"
49 #include "cfg.h"
50 #include "cfganal.h"
51 #include "predict.h"
52 #include "basic-block.h"
53 #include "df.h"
54 #include "tree-ssa-alias.h"
55 #include "internal-fn.h"
56 #include "gimple-expr.h"
57 #include "is-a.h"
58 #include "gimple.h"
59 #include "gimple-ssa.h"
60 #include "rtl-iter.h"
62 /* The aliasing API provided here solves related but different problems:
64 Say there exists (in c)
66 struct X {
67 struct Y y1;
68 struct Z z2;
69 } x1, *px1, *px2;
71 struct Y y2, *py;
72 struct Z z2, *pz;
75 py = &x1.y1;
76 px2 = &x1;
78 Consider the four questions:
80 Can a store to x1 interfere with px2->y1?
81 Can a store to x1 interfere with px2->z2?
82 Can a store to x1 change the value pointed to by with py?
83 Can a store to x1 change the value pointed to by with pz?
85 The answer to these questions can be yes, yes, yes, and maybe.
87 The first two questions can be answered with a simple examination
88 of the type system. If structure X contains a field of type Y then
89 a store through a pointer to an X can overwrite any field that is
90 contained (recursively) in an X (unless we know that px1 != px2).
92 The last two questions can be solved in the same way as the first
93 two questions but this is too conservative. The observation is
94 that in some cases we can know which (if any) fields are addressed
95 and if those addresses are used in bad ways. This analysis may be
96 language specific. In C, arbitrary operations may be applied to
97 pointers. However, there is some indication that this may be too
98 conservative for some C++ types.
100 The pass ipa-type-escape does this analysis for the types whose
101 instances do not escape across the compilation boundary.
103 Historically in GCC, these two problems were combined and a single
104 data structure that was used to represent the solution to these
105 problems. We now have two similar but different data structures,
106 The data structure to solve the last two questions is similar to
107 the first, but does not contain the fields whose address are never
108 taken. For types that do escape the compilation unit, the data
109 structures will have identical information.
112 /* The alias sets assigned to MEMs assist the back-end in determining
113 which MEMs can alias which other MEMs. In general, two MEMs in
114 different alias sets cannot alias each other, with one important
115 exception. Consider something like:
117 struct S { int i; double d; };
119 a store to an `S' can alias something of either type `int' or type
120 `double'. (However, a store to an `int' cannot alias a `double'
121 and vice versa.) We indicate this via a tree structure that looks
122 like:
123 struct S
126 |/_ _\|
127 int double
129 (The arrows are directed and point downwards.)
130 In this situation we say the alias set for `struct S' is the
131 `superset' and that those for `int' and `double' are `subsets'.
133 To see whether two alias sets can point to the same memory, we must
134 see if either alias set is a subset of the other. We need not trace
135 past immediate descendants, however, since we propagate all
136 grandchildren up one level.
138 Alias set zero is implicitly a superset of all other alias sets.
139 However, this is no actual entry for alias set zero. It is an
140 error to attempt to explicitly construct a subset of zero. */
142 struct alias_set_traits : default_hashmap_traits
144 template<typename T>
145 static bool
146 is_empty (T &e)
148 return e.m_key == INT_MIN;
151 template<typename T>
152 static bool
153 is_deleted (T &e)
155 return e.m_key == (INT_MIN + 1);
158 template<typename T> static void mark_empty (T &e) { e.m_key = INT_MIN; }
160 template<typename T>
161 static void
162 mark_deleted (T &e)
164 e.m_key = INT_MIN + 1;
168 struct GTY(()) alias_set_entry_d {
169 /* The alias set number, as stored in MEM_ALIAS_SET. */
170 alias_set_type alias_set;
172 /* Nonzero if would have a child of zero: this effectively makes this
173 alias set the same as alias set zero. */
174 int has_zero_child;
176 /* The children of the alias set. These are not just the immediate
177 children, but, in fact, all descendants. So, if we have:
179 struct T { struct S s; float f; }
181 continuing our example above, the children here will be all of
182 `int', `double', `float', and `struct S'. */
183 hash_map<int, int, alias_set_traits> *children;
185 typedef struct alias_set_entry_d *alias_set_entry;
187 static int rtx_equal_for_memref_p (const_rtx, const_rtx);
188 static int memrefs_conflict_p (int, rtx, int, rtx, HOST_WIDE_INT);
189 static void record_set (rtx, const_rtx, void *);
190 static int base_alias_check (rtx, rtx, rtx, rtx, machine_mode,
191 machine_mode);
192 static rtx find_base_value (rtx);
193 static int mems_in_disjoint_alias_sets_p (const_rtx, const_rtx);
194 static alias_set_entry get_alias_set_entry (alias_set_type);
195 static tree decl_for_component_ref (tree);
196 static int write_dependence_p (const_rtx,
197 const_rtx, machine_mode, rtx,
198 bool, bool, bool);
200 static void memory_modified_1 (rtx, const_rtx, void *);
202 /* Set up all info needed to perform alias analysis on memory references. */
204 /* Returns the size in bytes of the mode of X. */
205 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
207 /* Cap the number of passes we make over the insns propagating alias
208 information through set chains.
209 ??? 10 is a completely arbitrary choice. This should be based on the
210 maximum loop depth in the CFG, but we do not have this information
211 available (even if current_loops _is_ available). */
212 #define MAX_ALIAS_LOOP_PASSES 10
214 /* reg_base_value[N] gives an address to which register N is related.
215 If all sets after the first add or subtract to the current value
216 or otherwise modify it so it does not point to a different top level
217 object, reg_base_value[N] is equal to the address part of the source
218 of the first set.
220 A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF. ADDRESS
221 expressions represent three types of base:
223 1. incoming arguments. There is just one ADDRESS to represent all
224 arguments, since we do not know at this level whether accesses
225 based on different arguments can alias. The ADDRESS has id 0.
227 2. stack_pointer_rtx, frame_pointer_rtx, hard_frame_pointer_rtx
228 (if distinct from frame_pointer_rtx) and arg_pointer_rtx.
229 Each of these rtxes has a separate ADDRESS associated with it,
230 each with a negative id.
232 GCC is (and is required to be) precise in which register it
233 chooses to access a particular region of stack. We can therefore
234 assume that accesses based on one of these rtxes do not alias
235 accesses based on another of these rtxes.
237 3. bases that are derived from malloc()ed memory (REG_NOALIAS).
238 Each such piece of memory has a separate ADDRESS associated
239 with it, each with an id greater than 0.
241 Accesses based on one ADDRESS do not alias accesses based on other
242 ADDRESSes. Accesses based on ADDRESSes in groups (2) and (3) do not
243 alias globals either; the ADDRESSes have Pmode to indicate this.
244 The ADDRESS in group (1) _may_ alias globals; it has VOIDmode to
245 indicate this. */
247 static GTY(()) vec<rtx, va_gc> *reg_base_value;
248 static rtx *new_reg_base_value;
250 /* The single VOIDmode ADDRESS that represents all argument bases.
251 It has id 0. */
252 static GTY(()) rtx arg_base_value;
254 /* Used to allocate unique ids to each REG_NOALIAS ADDRESS. */
255 static int unique_id;
257 /* We preserve the copy of old array around to avoid amount of garbage
258 produced. About 8% of garbage produced were attributed to this
259 array. */
260 static GTY((deletable)) vec<rtx, va_gc> *old_reg_base_value;
262 /* Values of XINT (address, 0) of Pmode ADDRESS rtxes for special
263 registers. */
264 #define UNIQUE_BASE_VALUE_SP -1
265 #define UNIQUE_BASE_VALUE_ARGP -2
266 #define UNIQUE_BASE_VALUE_FP -3
267 #define UNIQUE_BASE_VALUE_HFP -4
269 #define static_reg_base_value \
270 (this_target_rtl->x_static_reg_base_value)
272 #define REG_BASE_VALUE(X) \
273 (REGNO (X) < vec_safe_length (reg_base_value) \
274 ? (*reg_base_value)[REGNO (X)] : 0)
276 /* Vector indexed by N giving the initial (unchanging) value known for
277 pseudo-register N. This vector is initialized in init_alias_analysis,
278 and does not change until end_alias_analysis is called. */
279 static GTY(()) vec<rtx, va_gc> *reg_known_value;
281 /* Vector recording for each reg_known_value whether it is due to a
282 REG_EQUIV note. Future passes (viz., reload) may replace the
283 pseudo with the equivalent expression and so we account for the
284 dependences that would be introduced if that happens.
286 The REG_EQUIV notes created in assign_parms may mention the arg
287 pointer, and there are explicit insns in the RTL that modify the
288 arg pointer. Thus we must ensure that such insns don't get
289 scheduled across each other because that would invalidate the
290 REG_EQUIV notes. One could argue that the REG_EQUIV notes are
291 wrong, but solving the problem in the scheduler will likely give
292 better code, so we do it here. */
293 static sbitmap reg_known_equiv_p;
295 /* True when scanning insns from the start of the rtl to the
296 NOTE_INSN_FUNCTION_BEG note. */
297 static bool copying_arguments;
300 /* The splay-tree used to store the various alias set entries. */
301 static GTY (()) vec<alias_set_entry, va_gc> *alias_sets;
303 /* Build a decomposed reference object for querying the alias-oracle
304 from the MEM rtx and store it in *REF.
305 Returns false if MEM is not suitable for the alias-oracle. */
307 static bool
308 ao_ref_from_mem (ao_ref *ref, const_rtx mem)
310 tree expr = MEM_EXPR (mem);
311 tree base;
313 if (!expr)
314 return false;
316 ao_ref_init (ref, expr);
318 /* Get the base of the reference and see if we have to reject or
319 adjust it. */
320 base = ao_ref_base (ref);
321 if (base == NULL_TREE)
322 return false;
324 /* The tree oracle doesn't like bases that are neither decls
325 nor indirect references of SSA names. */
326 if (!(DECL_P (base)
327 || (TREE_CODE (base) == MEM_REF
328 && TREE_CODE (TREE_OPERAND (base, 0)) == SSA_NAME)
329 || (TREE_CODE (base) == TARGET_MEM_REF
330 && TREE_CODE (TMR_BASE (base)) == SSA_NAME)))
331 return false;
333 /* If this is a reference based on a partitioned decl replace the
334 base with a MEM_REF of the pointer representative we
335 created during stack slot partitioning. */
336 if (TREE_CODE (base) == VAR_DECL
337 && ! is_global_var (base)
338 && cfun->gimple_df->decls_to_pointers != NULL)
340 tree *namep = cfun->gimple_df->decls_to_pointers->get (base);
341 if (namep)
342 ref->base = build_simple_mem_ref (*namep);
345 ref->ref_alias_set = MEM_ALIAS_SET (mem);
347 /* If MEM_OFFSET or MEM_SIZE are unknown what we got from MEM_EXPR
348 is conservative, so trust it. */
349 if (!MEM_OFFSET_KNOWN_P (mem)
350 || !MEM_SIZE_KNOWN_P (mem))
351 return true;
353 /* If the base decl is a parameter we can have negative MEM_OFFSET in
354 case of promoted subregs on bigendian targets. Trust the MEM_EXPR
355 here. */
356 if (MEM_OFFSET (mem) < 0
357 && (MEM_SIZE (mem) + MEM_OFFSET (mem)) * BITS_PER_UNIT == ref->size)
358 return true;
360 /* Otherwise continue and refine size and offset we got from analyzing
361 MEM_EXPR by using MEM_SIZE and MEM_OFFSET. */
363 ref->offset += MEM_OFFSET (mem) * BITS_PER_UNIT;
364 ref->size = MEM_SIZE (mem) * BITS_PER_UNIT;
366 /* The MEM may extend into adjacent fields, so adjust max_size if
367 necessary. */
368 if (ref->max_size != -1
369 && ref->size > ref->max_size)
370 ref->max_size = ref->size;
372 /* If MEM_OFFSET and MEM_SIZE get us outside of the base object of
373 the MEM_EXPR punt. This happens for STRICT_ALIGNMENT targets a lot. */
374 if (MEM_EXPR (mem) != get_spill_slot_decl (false)
375 && (ref->offset < 0
376 || (DECL_P (ref->base)
377 && (DECL_SIZE (ref->base) == NULL_TREE
378 || TREE_CODE (DECL_SIZE (ref->base)) != INTEGER_CST
379 || wi::ltu_p (wi::to_offset (DECL_SIZE (ref->base)),
380 ref->offset + ref->size)))))
381 return false;
383 return true;
386 /* Query the alias-oracle on whether the two memory rtx X and MEM may
387 alias. If TBAA_P is set also apply TBAA. Returns true if the
388 two rtxen may alias, false otherwise. */
390 static bool
391 rtx_refs_may_alias_p (const_rtx x, const_rtx mem, bool tbaa_p)
393 ao_ref ref1, ref2;
395 if (!ao_ref_from_mem (&ref1, x)
396 || !ao_ref_from_mem (&ref2, mem))
397 return true;
399 return refs_may_alias_p_1 (&ref1, &ref2,
400 tbaa_p
401 && MEM_ALIAS_SET (x) != 0
402 && MEM_ALIAS_SET (mem) != 0);
405 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
406 such an entry, or NULL otherwise. */
408 static inline alias_set_entry
409 get_alias_set_entry (alias_set_type alias_set)
411 return (*alias_sets)[alias_set];
414 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
415 the two MEMs cannot alias each other. */
417 static inline int
418 mems_in_disjoint_alias_sets_p (const_rtx mem1, const_rtx mem2)
420 /* Perform a basic sanity check. Namely, that there are no alias sets
421 if we're not using strict aliasing. This helps to catch bugs
422 whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
423 where a MEM is allocated in some way other than by the use of
424 gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared. If we begin to
425 use alias sets to indicate that spilled registers cannot alias each
426 other, we might need to remove this check. */
427 gcc_assert (flag_strict_aliasing
428 || (!MEM_ALIAS_SET (mem1) && !MEM_ALIAS_SET (mem2)));
430 return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
433 /* Return true if the first alias set is a subset of the second. */
435 bool
436 alias_set_subset_of (alias_set_type set1, alias_set_type set2)
438 alias_set_entry ase;
440 /* Everything is a subset of the "aliases everything" set. */
441 if (set2 == 0)
442 return true;
444 /* Otherwise, check if set1 is a subset of set2. */
445 ase = get_alias_set_entry (set2);
446 if (ase != 0
447 && (ase->has_zero_child
448 || ase->children->get (set1)))
449 return true;
450 return false;
453 /* Return 1 if the two specified alias sets may conflict. */
456 alias_sets_conflict_p (alias_set_type set1, alias_set_type set2)
458 alias_set_entry ase;
460 /* The easy case. */
461 if (alias_sets_must_conflict_p (set1, set2))
462 return 1;
464 /* See if the first alias set is a subset of the second. */
465 ase = get_alias_set_entry (set1);
466 if (ase != 0
467 && (ase->has_zero_child
468 || ase->children->get (set2)))
469 return 1;
471 /* Now do the same, but with the alias sets reversed. */
472 ase = get_alias_set_entry (set2);
473 if (ase != 0
474 && (ase->has_zero_child
475 || ase->children->get (set1)))
476 return 1;
478 /* The two alias sets are distinct and neither one is the
479 child of the other. Therefore, they cannot conflict. */
480 return 0;
483 /* Return 1 if the two specified alias sets will always conflict. */
486 alias_sets_must_conflict_p (alias_set_type set1, alias_set_type set2)
488 if (set1 == 0 || set2 == 0 || set1 == set2)
489 return 1;
491 return 0;
494 /* Return 1 if any MEM object of type T1 will always conflict (using the
495 dependency routines in this file) with any MEM object of type T2.
496 This is used when allocating temporary storage. If T1 and/or T2 are
497 NULL_TREE, it means we know nothing about the storage. */
500 objects_must_conflict_p (tree t1, tree t2)
502 alias_set_type set1, set2;
504 /* If neither has a type specified, we don't know if they'll conflict
505 because we may be using them to store objects of various types, for
506 example the argument and local variables areas of inlined functions. */
507 if (t1 == 0 && t2 == 0)
508 return 0;
510 /* If they are the same type, they must conflict. */
511 if (t1 == t2
512 /* Likewise if both are volatile. */
513 || (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2)))
514 return 1;
516 set1 = t1 ? get_alias_set (t1) : 0;
517 set2 = t2 ? get_alias_set (t2) : 0;
519 /* We can't use alias_sets_conflict_p because we must make sure
520 that every subtype of t1 will conflict with every subtype of
521 t2 for which a pair of subobjects of these respective subtypes
522 overlaps on the stack. */
523 return alias_sets_must_conflict_p (set1, set2);
526 /* Return the outermost parent of component present in the chain of
527 component references handled by get_inner_reference in T with the
528 following property:
529 - the component is non-addressable, or
530 - the parent has alias set zero,
531 or NULL_TREE if no such parent exists. In the former cases, the alias
532 set of this parent is the alias set that must be used for T itself. */
534 tree
535 component_uses_parent_alias_set_from (const_tree t)
537 const_tree found = NULL_TREE;
539 while (handled_component_p (t))
541 switch (TREE_CODE (t))
543 case COMPONENT_REF:
544 if (DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
545 found = t;
546 break;
548 case ARRAY_REF:
549 case ARRAY_RANGE_REF:
550 if (TYPE_NONALIASED_COMPONENT (TREE_TYPE (TREE_OPERAND (t, 0))))
551 found = t;
552 break;
554 case REALPART_EXPR:
555 case IMAGPART_EXPR:
556 break;
558 case BIT_FIELD_REF:
559 case VIEW_CONVERT_EXPR:
560 /* Bitfields and casts are never addressable. */
561 found = t;
562 break;
564 default:
565 gcc_unreachable ();
568 if (get_alias_set (TREE_TYPE (TREE_OPERAND (t, 0))) == 0)
569 found = t;
571 t = TREE_OPERAND (t, 0);
574 if (found)
575 return TREE_OPERAND (found, 0);
577 return NULL_TREE;
581 /* Return whether the pointer-type T effective for aliasing may
582 access everything and thus the reference has to be assigned
583 alias-set zero. */
585 static bool
586 ref_all_alias_ptr_type_p (const_tree t)
588 return (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE
589 || TYPE_REF_CAN_ALIAS_ALL (t));
592 /* Return the alias set for the memory pointed to by T, which may be
593 either a type or an expression. Return -1 if there is nothing
594 special about dereferencing T. */
596 static alias_set_type
597 get_deref_alias_set_1 (tree t)
599 /* All we care about is the type. */
600 if (! TYPE_P (t))
601 t = TREE_TYPE (t);
603 /* If we have an INDIRECT_REF via a void pointer, we don't
604 know anything about what that might alias. Likewise if the
605 pointer is marked that way. */
606 if (ref_all_alias_ptr_type_p (t))
607 return 0;
609 return -1;
612 /* Return the alias set for the memory pointed to by T, which may be
613 either a type or an expression. */
615 alias_set_type
616 get_deref_alias_set (tree t)
618 /* If we're not doing any alias analysis, just assume everything
619 aliases everything else. */
620 if (!flag_strict_aliasing)
621 return 0;
623 alias_set_type set = get_deref_alias_set_1 (t);
625 /* Fall back to the alias-set of the pointed-to type. */
626 if (set == -1)
628 if (! TYPE_P (t))
629 t = TREE_TYPE (t);
630 set = get_alias_set (TREE_TYPE (t));
633 return set;
636 /* Return the pointer-type relevant for TBAA purposes from the
637 memory reference tree *T or NULL_TREE in which case *T is
638 adjusted to point to the outermost component reference that
639 can be used for assigning an alias set. */
641 static tree
642 reference_alias_ptr_type_1 (tree *t)
644 tree inner;
646 /* Get the base object of the reference. */
647 inner = *t;
648 while (handled_component_p (inner))
650 /* If there is a VIEW_CONVERT_EXPR in the chain we cannot use
651 the type of any component references that wrap it to
652 determine the alias-set. */
653 if (TREE_CODE (inner) == VIEW_CONVERT_EXPR)
654 *t = TREE_OPERAND (inner, 0);
655 inner = TREE_OPERAND (inner, 0);
658 /* Handle pointer dereferences here, they can override the
659 alias-set. */
660 if (INDIRECT_REF_P (inner)
661 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 0))))
662 return TREE_TYPE (TREE_OPERAND (inner, 0));
663 else if (TREE_CODE (inner) == TARGET_MEM_REF)
664 return TREE_TYPE (TMR_OFFSET (inner));
665 else if (TREE_CODE (inner) == MEM_REF
666 && ref_all_alias_ptr_type_p (TREE_TYPE (TREE_OPERAND (inner, 1))))
667 return TREE_TYPE (TREE_OPERAND (inner, 1));
669 /* If the innermost reference is a MEM_REF that has a
670 conversion embedded treat it like a VIEW_CONVERT_EXPR above,
671 using the memory access type for determining the alias-set. */
672 if (TREE_CODE (inner) == MEM_REF
673 && (TYPE_MAIN_VARIANT (TREE_TYPE (inner))
674 != TYPE_MAIN_VARIANT
675 (TREE_TYPE (TREE_TYPE (TREE_OPERAND (inner, 1))))))
676 return TREE_TYPE (TREE_OPERAND (inner, 1));
678 /* Otherwise, pick up the outermost object that we could have
679 a pointer to. */
680 tree tem = component_uses_parent_alias_set_from (*t);
681 if (tem)
682 *t = tem;
684 return NULL_TREE;
687 /* Return the pointer-type relevant for TBAA purposes from the
688 gimple memory reference tree T. This is the type to be used for
689 the offset operand of MEM_REF or TARGET_MEM_REF replacements of T
690 and guarantees that get_alias_set will return the same alias
691 set for T and the replacement. */
693 tree
694 reference_alias_ptr_type (tree t)
696 tree ptype = reference_alias_ptr_type_1 (&t);
697 /* If there is a given pointer type for aliasing purposes, return it. */
698 if (ptype != NULL_TREE)
699 return ptype;
701 /* Otherwise build one from the outermost component reference we
702 may use. */
703 if (TREE_CODE (t) == MEM_REF
704 || TREE_CODE (t) == TARGET_MEM_REF)
705 return TREE_TYPE (TREE_OPERAND (t, 1));
706 else
707 return build_pointer_type (TYPE_MAIN_VARIANT (TREE_TYPE (t)));
710 /* Return whether the pointer-types T1 and T2 used to determine
711 two alias sets of two references will yield the same answer
712 from get_deref_alias_set. */
714 bool
715 alias_ptr_types_compatible_p (tree t1, tree t2)
717 if (TYPE_MAIN_VARIANT (t1) == TYPE_MAIN_VARIANT (t2))
718 return true;
720 if (ref_all_alias_ptr_type_p (t1)
721 || ref_all_alias_ptr_type_p (t2))
722 return false;
724 return (TYPE_MAIN_VARIANT (TREE_TYPE (t1))
725 == TYPE_MAIN_VARIANT (TREE_TYPE (t2)));
728 /* Return the alias set for T, which may be either a type or an
729 expression. Call language-specific routine for help, if needed. */
731 alias_set_type
732 get_alias_set (tree t)
734 alias_set_type set;
736 /* If we're not doing any alias analysis, just assume everything
737 aliases everything else. Also return 0 if this or its type is
738 an error. */
739 if (! flag_strict_aliasing || t == error_mark_node
740 || (! TYPE_P (t)
741 && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
742 return 0;
744 /* We can be passed either an expression or a type. This and the
745 language-specific routine may make mutually-recursive calls to each other
746 to figure out what to do. At each juncture, we see if this is a tree
747 that the language may need to handle specially. First handle things that
748 aren't types. */
749 if (! TYPE_P (t))
751 /* Give the language a chance to do something with this tree
752 before we look at it. */
753 STRIP_NOPS (t);
754 set = lang_hooks.get_alias_set (t);
755 if (set != -1)
756 return set;
758 /* Get the alias pointer-type to use or the outermost object
759 that we could have a pointer to. */
760 tree ptype = reference_alias_ptr_type_1 (&t);
761 if (ptype != NULL)
762 return get_deref_alias_set (ptype);
764 /* If we've already determined the alias set for a decl, just return
765 it. This is necessary for C++ anonymous unions, whose component
766 variables don't look like union members (boo!). */
767 if (TREE_CODE (t) == VAR_DECL
768 && DECL_RTL_SET_P (t) && MEM_P (DECL_RTL (t)))
769 return MEM_ALIAS_SET (DECL_RTL (t));
771 /* Now all we care about is the type. */
772 t = TREE_TYPE (t);
775 /* Variant qualifiers don't affect the alias set, so get the main
776 variant. */
777 t = TYPE_MAIN_VARIANT (t);
779 /* Always use the canonical type as well. If this is a type that
780 requires structural comparisons to identify compatible types
781 use alias set zero. */
782 if (TYPE_STRUCTURAL_EQUALITY_P (t))
784 /* Allow the language to specify another alias set for this
785 type. */
786 set = lang_hooks.get_alias_set (t);
787 if (set != -1)
788 return set;
789 return 0;
792 t = TYPE_CANONICAL (t);
794 /* The canonical type should not require structural equality checks. */
795 gcc_checking_assert (!TYPE_STRUCTURAL_EQUALITY_P (t));
797 /* If this is a type with a known alias set, return it. */
798 if (TYPE_ALIAS_SET_KNOWN_P (t))
799 return TYPE_ALIAS_SET (t);
801 /* We don't want to set TYPE_ALIAS_SET for incomplete types. */
802 if (!COMPLETE_TYPE_P (t))
804 /* For arrays with unknown size the conservative answer is the
805 alias set of the element type. */
806 if (TREE_CODE (t) == ARRAY_TYPE)
807 return get_alias_set (TREE_TYPE (t));
809 /* But return zero as a conservative answer for incomplete types. */
810 return 0;
813 /* See if the language has special handling for this type. */
814 set = lang_hooks.get_alias_set (t);
815 if (set != -1)
816 return set;
818 /* There are no objects of FUNCTION_TYPE, so there's no point in
819 using up an alias set for them. (There are, of course, pointers
820 and references to functions, but that's different.) */
821 else if (TREE_CODE (t) == FUNCTION_TYPE || TREE_CODE (t) == METHOD_TYPE)
822 set = 0;
824 /* Unless the language specifies otherwise, let vector types alias
825 their components. This avoids some nasty type punning issues in
826 normal usage. And indeed lets vectors be treated more like an
827 array slice. */
828 else if (TREE_CODE (t) == VECTOR_TYPE)
829 set = get_alias_set (TREE_TYPE (t));
831 /* Unless the language specifies otherwise, treat array types the
832 same as their components. This avoids the asymmetry we get
833 through recording the components. Consider accessing a
834 character(kind=1) through a reference to a character(kind=1)[1:1].
835 Or consider if we want to assign integer(kind=4)[0:D.1387] and
836 integer(kind=4)[4] the same alias set or not.
837 Just be pragmatic here and make sure the array and its element
838 type get the same alias set assigned. */
839 else if (TREE_CODE (t) == ARRAY_TYPE && !TYPE_NONALIASED_COMPONENT (t))
840 set = get_alias_set (TREE_TYPE (t));
842 /* From the former common C and C++ langhook implementation:
844 Unfortunately, there is no canonical form of a pointer type.
845 In particular, if we have `typedef int I', then `int *', and
846 `I *' are different types. So, we have to pick a canonical
847 representative. We do this below.
849 Technically, this approach is actually more conservative that
850 it needs to be. In particular, `const int *' and `int *'
851 should be in different alias sets, according to the C and C++
852 standard, since their types are not the same, and so,
853 technically, an `int **' and `const int **' cannot point at
854 the same thing.
856 But, the standard is wrong. In particular, this code is
857 legal C++:
859 int *ip;
860 int **ipp = &ip;
861 const int* const* cipp = ipp;
862 And, it doesn't make sense for that to be legal unless you
863 can dereference IPP and CIPP. So, we ignore cv-qualifiers on
864 the pointed-to types. This issue has been reported to the
865 C++ committee.
867 In addition to the above canonicalization issue, with LTO
868 we should also canonicalize `T (*)[]' to `T *' avoiding
869 alias issues with pointer-to element types and pointer-to
870 array types.
872 Likewise we need to deal with the situation of incomplete
873 pointed-to types and make `*(struct X **)&a' and
874 `*(struct X {} **)&a' alias. Otherwise we will have to
875 guarantee that all pointer-to incomplete type variants
876 will be replaced by pointer-to complete type variants if
877 they are available.
879 With LTO the convenient situation of using `void *' to
880 access and store any pointer type will also become
881 more apparent (and `void *' is just another pointer-to
882 incomplete type). Assigning alias-set zero to `void *'
883 and all pointer-to incomplete types is a not appealing
884 solution. Assigning an effective alias-set zero only
885 affecting pointers might be - by recording proper subset
886 relationships of all pointer alias-sets.
888 Pointer-to function types are another grey area which
889 needs caution. Globbing them all into one alias-set
890 or the above effective zero set would work.
892 For now just assign the same alias-set to all pointers.
893 That's simple and avoids all the above problems. */
894 else if (POINTER_TYPE_P (t)
895 && t != ptr_type_node)
896 set = get_alias_set (ptr_type_node);
898 /* Otherwise make a new alias set for this type. */
899 else
901 /* Each canonical type gets its own alias set, so canonical types
902 shouldn't form a tree. It doesn't really matter for types
903 we handle specially above, so only check it where it possibly
904 would result in a bogus alias set. */
905 gcc_checking_assert (TYPE_CANONICAL (t) == t);
907 set = new_alias_set ();
910 TYPE_ALIAS_SET (t) = set;
912 /* If this is an aggregate type or a complex type, we must record any
913 component aliasing information. */
914 if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
915 record_component_aliases (t);
917 return set;
920 /* Return a brand-new alias set. */
922 alias_set_type
923 new_alias_set (void)
925 if (flag_strict_aliasing)
927 if (alias_sets == 0)
928 vec_safe_push (alias_sets, (alias_set_entry) 0);
929 vec_safe_push (alias_sets, (alias_set_entry) 0);
930 return alias_sets->length () - 1;
932 else
933 return 0;
936 /* Indicate that things in SUBSET can alias things in SUPERSET, but that
937 not everything that aliases SUPERSET also aliases SUBSET. For example,
938 in C, a store to an `int' can alias a load of a structure containing an
939 `int', and vice versa. But it can't alias a load of a 'double' member
940 of the same structure. Here, the structure would be the SUPERSET and
941 `int' the SUBSET. This relationship is also described in the comment at
942 the beginning of this file.
944 This function should be called only once per SUPERSET/SUBSET pair.
946 It is illegal for SUPERSET to be zero; everything is implicitly a
947 subset of alias set zero. */
949 void
950 record_alias_subset (alias_set_type superset, alias_set_type subset)
952 alias_set_entry superset_entry;
953 alias_set_entry subset_entry;
955 /* It is possible in complex type situations for both sets to be the same,
956 in which case we can ignore this operation. */
957 if (superset == subset)
958 return;
960 gcc_assert (superset);
962 superset_entry = get_alias_set_entry (superset);
963 if (superset_entry == 0)
965 /* Create an entry for the SUPERSET, so that we have a place to
966 attach the SUBSET. */
967 superset_entry = ggc_cleared_alloc<alias_set_entry_d> ();
968 superset_entry->alias_set = superset;
969 superset_entry->children
970 = hash_map<int, int, alias_set_traits>::create_ggc (64);
971 superset_entry->has_zero_child = 0;
972 (*alias_sets)[superset] = superset_entry;
975 if (subset == 0)
976 superset_entry->has_zero_child = 1;
977 else
979 subset_entry = get_alias_set_entry (subset);
980 /* If there is an entry for the subset, enter all of its children
981 (if they are not already present) as children of the SUPERSET. */
982 if (subset_entry)
984 if (subset_entry->has_zero_child)
985 superset_entry->has_zero_child = 1;
987 hash_map<int, int, alias_set_traits>::iterator iter
988 = subset_entry->children->begin ();
989 for (; iter != subset_entry->children->end (); ++iter)
990 superset_entry->children->put ((*iter).first, (*iter).second);
993 /* Enter the SUBSET itself as a child of the SUPERSET. */
994 superset_entry->children->put (subset, 0);
998 /* Record that component types of TYPE, if any, are part of that type for
999 aliasing purposes. For record types, we only record component types
1000 for fields that are not marked non-addressable. For array types, we
1001 only record the component type if it is not marked non-aliased. */
1003 void
1004 record_component_aliases (tree type)
1006 alias_set_type superset = get_alias_set (type);
1007 tree field;
1009 if (superset == 0)
1010 return;
1012 switch (TREE_CODE (type))
1014 case RECORD_TYPE:
1015 case UNION_TYPE:
1016 case QUAL_UNION_TYPE:
1017 for (field = TYPE_FIELDS (type); field != 0; field = DECL_CHAIN (field))
1018 if (TREE_CODE (field) == FIELD_DECL && !DECL_NONADDRESSABLE_P (field))
1019 record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
1020 break;
1022 case COMPLEX_TYPE:
1023 record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
1024 break;
1026 /* VECTOR_TYPE and ARRAY_TYPE share the alias set with their
1027 element type. */
1029 default:
1030 break;
1034 /* Allocate an alias set for use in storing and reading from the varargs
1035 spill area. */
1037 static GTY(()) alias_set_type varargs_set = -1;
1039 alias_set_type
1040 get_varargs_alias_set (void)
1042 #if 1
1043 /* We now lower VA_ARG_EXPR, and there's currently no way to attach the
1044 varargs alias set to an INDIRECT_REF (FIXME!), so we can't
1045 consistently use the varargs alias set for loads from the varargs
1046 area. So don't use it anywhere. */
1047 return 0;
1048 #else
1049 if (varargs_set == -1)
1050 varargs_set = new_alias_set ();
1052 return varargs_set;
1053 #endif
1056 /* Likewise, but used for the fixed portions of the frame, e.g., register
1057 save areas. */
1059 static GTY(()) alias_set_type frame_set = -1;
1061 alias_set_type
1062 get_frame_alias_set (void)
1064 if (frame_set == -1)
1065 frame_set = new_alias_set ();
1067 return frame_set;
1070 /* Create a new, unique base with id ID. */
1072 static rtx
1073 unique_base_value (HOST_WIDE_INT id)
1075 return gen_rtx_ADDRESS (Pmode, id);
1078 /* Return true if accesses based on any other base value cannot alias
1079 those based on X. */
1081 static bool
1082 unique_base_value_p (rtx x)
1084 return GET_CODE (x) == ADDRESS && GET_MODE (x) == Pmode;
1087 /* Return true if X is known to be a base value. */
1089 static bool
1090 known_base_value_p (rtx x)
1092 switch (GET_CODE (x))
1094 case LABEL_REF:
1095 case SYMBOL_REF:
1096 return true;
1098 case ADDRESS:
1099 /* Arguments may or may not be bases; we don't know for sure. */
1100 return GET_MODE (x) != VOIDmode;
1102 default:
1103 return false;
1107 /* Inside SRC, the source of a SET, find a base address. */
1109 static rtx
1110 find_base_value (rtx src)
1112 unsigned int regno;
1114 #if defined (FIND_BASE_TERM)
1115 /* Try machine-dependent ways to find the base term. */
1116 src = FIND_BASE_TERM (src);
1117 #endif
1119 switch (GET_CODE (src))
1121 case SYMBOL_REF:
1122 case LABEL_REF:
1123 return src;
1125 case REG:
1126 regno = REGNO (src);
1127 /* At the start of a function, argument registers have known base
1128 values which may be lost later. Returning an ADDRESS
1129 expression here allows optimization based on argument values
1130 even when the argument registers are used for other purposes. */
1131 if (regno < FIRST_PSEUDO_REGISTER && copying_arguments)
1132 return new_reg_base_value[regno];
1134 /* If a pseudo has a known base value, return it. Do not do this
1135 for non-fixed hard regs since it can result in a circular
1136 dependency chain for registers which have values at function entry.
1138 The test above is not sufficient because the scheduler may move
1139 a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN. */
1140 if ((regno >= FIRST_PSEUDO_REGISTER || fixed_regs[regno])
1141 && regno < vec_safe_length (reg_base_value))
1143 /* If we're inside init_alias_analysis, use new_reg_base_value
1144 to reduce the number of relaxation iterations. */
1145 if (new_reg_base_value && new_reg_base_value[regno]
1146 && DF_REG_DEF_COUNT (regno) == 1)
1147 return new_reg_base_value[regno];
1149 if ((*reg_base_value)[regno])
1150 return (*reg_base_value)[regno];
1153 return 0;
1155 case MEM:
1156 /* Check for an argument passed in memory. Only record in the
1157 copying-arguments block; it is too hard to track changes
1158 otherwise. */
1159 if (copying_arguments
1160 && (XEXP (src, 0) == arg_pointer_rtx
1161 || (GET_CODE (XEXP (src, 0)) == PLUS
1162 && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
1163 return arg_base_value;
1164 return 0;
1166 case CONST:
1167 src = XEXP (src, 0);
1168 if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
1169 break;
1171 /* ... fall through ... */
1173 case PLUS:
1174 case MINUS:
1176 rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
1178 /* If either operand is a REG that is a known pointer, then it
1179 is the base. */
1180 if (REG_P (src_0) && REG_POINTER (src_0))
1181 return find_base_value (src_0);
1182 if (REG_P (src_1) && REG_POINTER (src_1))
1183 return find_base_value (src_1);
1185 /* If either operand is a REG, then see if we already have
1186 a known value for it. */
1187 if (REG_P (src_0))
1189 temp = find_base_value (src_0);
1190 if (temp != 0)
1191 src_0 = temp;
1194 if (REG_P (src_1))
1196 temp = find_base_value (src_1);
1197 if (temp!= 0)
1198 src_1 = temp;
1201 /* If either base is named object or a special address
1202 (like an argument or stack reference), then use it for the
1203 base term. */
1204 if (src_0 != 0 && known_base_value_p (src_0))
1205 return src_0;
1207 if (src_1 != 0 && known_base_value_p (src_1))
1208 return src_1;
1210 /* Guess which operand is the base address:
1211 If either operand is a symbol, then it is the base. If
1212 either operand is a CONST_INT, then the other is the base. */
1213 if (CONST_INT_P (src_1) || CONSTANT_P (src_0))
1214 return find_base_value (src_0);
1215 else if (CONST_INT_P (src_0) || CONSTANT_P (src_1))
1216 return find_base_value (src_1);
1218 return 0;
1221 case LO_SUM:
1222 /* The standard form is (lo_sum reg sym) so look only at the
1223 second operand. */
1224 return find_base_value (XEXP (src, 1));
1226 case AND:
1227 /* If the second operand is constant set the base
1228 address to the first operand. */
1229 if (CONST_INT_P (XEXP (src, 1)) && INTVAL (XEXP (src, 1)) != 0)
1230 return find_base_value (XEXP (src, 0));
1231 return 0;
1233 case TRUNCATE:
1234 /* As we do not know which address space the pointer is referring to, we can
1235 handle this only if the target does not support different pointer or
1236 address modes depending on the address space. */
1237 if (!target_default_pointer_address_modes_p ())
1238 break;
1239 if (GET_MODE_SIZE (GET_MODE (src)) < GET_MODE_SIZE (Pmode))
1240 break;
1241 /* Fall through. */
1242 case HIGH:
1243 case PRE_INC:
1244 case PRE_DEC:
1245 case POST_INC:
1246 case POST_DEC:
1247 case PRE_MODIFY:
1248 case POST_MODIFY:
1249 return find_base_value (XEXP (src, 0));
1251 case ZERO_EXTEND:
1252 case SIGN_EXTEND: /* used for NT/Alpha pointers */
1253 /* As we do not know which address space the pointer is referring to, we can
1254 handle this only if the target does not support different pointer or
1255 address modes depending on the address space. */
1256 if (!target_default_pointer_address_modes_p ())
1257 break;
1260 rtx temp = find_base_value (XEXP (src, 0));
1262 if (temp != 0 && CONSTANT_P (temp))
1263 temp = convert_memory_address (Pmode, temp);
1265 return temp;
1268 default:
1269 break;
1272 return 0;
1275 /* Called from init_alias_analysis indirectly through note_stores,
1276 or directly if DEST is a register with a REG_NOALIAS note attached.
1277 SET is null in the latter case. */
1279 /* While scanning insns to find base values, reg_seen[N] is nonzero if
1280 register N has been set in this function. */
1281 static sbitmap reg_seen;
1283 static void
1284 record_set (rtx dest, const_rtx set, void *data ATTRIBUTE_UNUSED)
1286 unsigned regno;
1287 rtx src;
1288 int n;
1290 if (!REG_P (dest))
1291 return;
1293 regno = REGNO (dest);
1295 gcc_checking_assert (regno < reg_base_value->length ());
1297 /* If this spans multiple hard registers, then we must indicate that every
1298 register has an unusable value. */
1299 if (regno < FIRST_PSEUDO_REGISTER)
1300 n = hard_regno_nregs[regno][GET_MODE (dest)];
1301 else
1302 n = 1;
1303 if (n != 1)
1305 while (--n >= 0)
1307 bitmap_set_bit (reg_seen, regno + n);
1308 new_reg_base_value[regno + n] = 0;
1310 return;
1313 if (set)
1315 /* A CLOBBER wipes out any old value but does not prevent a previously
1316 unset register from acquiring a base address (i.e. reg_seen is not
1317 set). */
1318 if (GET_CODE (set) == CLOBBER)
1320 new_reg_base_value[regno] = 0;
1321 return;
1323 src = SET_SRC (set);
1325 else
1327 /* There's a REG_NOALIAS note against DEST. */
1328 if (bitmap_bit_p (reg_seen, regno))
1330 new_reg_base_value[regno] = 0;
1331 return;
1333 bitmap_set_bit (reg_seen, regno);
1334 new_reg_base_value[regno] = unique_base_value (unique_id++);
1335 return;
1338 /* If this is not the first set of REGNO, see whether the new value
1339 is related to the old one. There are two cases of interest:
1341 (1) The register might be assigned an entirely new value
1342 that has the same base term as the original set.
1344 (2) The set might be a simple self-modification that
1345 cannot change REGNO's base value.
1347 If neither case holds, reject the original base value as invalid.
1348 Note that the following situation is not detected:
1350 extern int x, y; int *p = &x; p += (&y-&x);
1352 ANSI C does not allow computing the difference of addresses
1353 of distinct top level objects. */
1354 if (new_reg_base_value[regno] != 0
1355 && find_base_value (src) != new_reg_base_value[regno])
1356 switch (GET_CODE (src))
1358 case LO_SUM:
1359 case MINUS:
1360 if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
1361 new_reg_base_value[regno] = 0;
1362 break;
1363 case PLUS:
1364 /* If the value we add in the PLUS is also a valid base value,
1365 this might be the actual base value, and the original value
1366 an index. */
1368 rtx other = NULL_RTX;
1370 if (XEXP (src, 0) == dest)
1371 other = XEXP (src, 1);
1372 else if (XEXP (src, 1) == dest)
1373 other = XEXP (src, 0);
1375 if (! other || find_base_value (other))
1376 new_reg_base_value[regno] = 0;
1377 break;
1379 case AND:
1380 if (XEXP (src, 0) != dest || !CONST_INT_P (XEXP (src, 1)))
1381 new_reg_base_value[regno] = 0;
1382 break;
1383 default:
1384 new_reg_base_value[regno] = 0;
1385 break;
1387 /* If this is the first set of a register, record the value. */
1388 else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
1389 && ! bitmap_bit_p (reg_seen, regno) && new_reg_base_value[regno] == 0)
1390 new_reg_base_value[regno] = find_base_value (src);
1392 bitmap_set_bit (reg_seen, regno);
1395 /* Return REG_BASE_VALUE for REGNO. Selective scheduler uses this to avoid
1396 using hard registers with non-null REG_BASE_VALUE for renaming. */
1398 get_reg_base_value (unsigned int regno)
1400 return (*reg_base_value)[regno];
1403 /* If a value is known for REGNO, return it. */
1406 get_reg_known_value (unsigned int regno)
1408 if (regno >= FIRST_PSEUDO_REGISTER)
1410 regno -= FIRST_PSEUDO_REGISTER;
1411 if (regno < vec_safe_length (reg_known_value))
1412 return (*reg_known_value)[regno];
1414 return NULL;
1417 /* Set it. */
1419 static void
1420 set_reg_known_value (unsigned int regno, rtx val)
1422 if (regno >= FIRST_PSEUDO_REGISTER)
1424 regno -= FIRST_PSEUDO_REGISTER;
1425 if (regno < vec_safe_length (reg_known_value))
1426 (*reg_known_value)[regno] = val;
1430 /* Similarly for reg_known_equiv_p. */
1432 bool
1433 get_reg_known_equiv_p (unsigned int regno)
1435 if (regno >= FIRST_PSEUDO_REGISTER)
1437 regno -= FIRST_PSEUDO_REGISTER;
1438 if (regno < vec_safe_length (reg_known_value))
1439 return bitmap_bit_p (reg_known_equiv_p, regno);
1441 return false;
1444 static void
1445 set_reg_known_equiv_p (unsigned int regno, bool val)
1447 if (regno >= FIRST_PSEUDO_REGISTER)
1449 regno -= FIRST_PSEUDO_REGISTER;
1450 if (regno < vec_safe_length (reg_known_value))
1452 if (val)
1453 bitmap_set_bit (reg_known_equiv_p, regno);
1454 else
1455 bitmap_clear_bit (reg_known_equiv_p, regno);
1461 /* Returns a canonical version of X, from the point of view alias
1462 analysis. (For example, if X is a MEM whose address is a register,
1463 and the register has a known value (say a SYMBOL_REF), then a MEM
1464 whose address is the SYMBOL_REF is returned.) */
1467 canon_rtx (rtx x)
1469 /* Recursively look for equivalences. */
1470 if (REG_P (x) && REGNO (x) >= FIRST_PSEUDO_REGISTER)
1472 rtx t = get_reg_known_value (REGNO (x));
1473 if (t == x)
1474 return x;
1475 if (t)
1476 return canon_rtx (t);
1479 if (GET_CODE (x) == PLUS)
1481 rtx x0 = canon_rtx (XEXP (x, 0));
1482 rtx x1 = canon_rtx (XEXP (x, 1));
1484 if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
1486 if (CONST_INT_P (x0))
1487 return plus_constant (GET_MODE (x), x1, INTVAL (x0));
1488 else if (CONST_INT_P (x1))
1489 return plus_constant (GET_MODE (x), x0, INTVAL (x1));
1490 return gen_rtx_PLUS (GET_MODE (x), x0, x1);
1494 /* This gives us much better alias analysis when called from
1495 the loop optimizer. Note we want to leave the original
1496 MEM alone, but need to return the canonicalized MEM with
1497 all the flags with their original values. */
1498 else if (MEM_P (x))
1499 x = replace_equiv_address_nv (x, canon_rtx (XEXP (x, 0)));
1501 return x;
1504 /* Return 1 if X and Y are identical-looking rtx's.
1505 Expect that X and Y has been already canonicalized.
1507 We use the data in reg_known_value above to see if two registers with
1508 different numbers are, in fact, equivalent. */
1510 static int
1511 rtx_equal_for_memref_p (const_rtx x, const_rtx y)
1513 int i;
1514 int j;
1515 enum rtx_code code;
1516 const char *fmt;
1518 if (x == 0 && y == 0)
1519 return 1;
1520 if (x == 0 || y == 0)
1521 return 0;
1523 if (x == y)
1524 return 1;
1526 code = GET_CODE (x);
1527 /* Rtx's of different codes cannot be equal. */
1528 if (code != GET_CODE (y))
1529 return 0;
1531 /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
1532 (REG:SI x) and (REG:HI x) are NOT equivalent. */
1534 if (GET_MODE (x) != GET_MODE (y))
1535 return 0;
1537 /* Some RTL can be compared without a recursive examination. */
1538 switch (code)
1540 case REG:
1541 return REGNO (x) == REGNO (y);
1543 case LABEL_REF:
1544 return LABEL_REF_LABEL (x) == LABEL_REF_LABEL (y);
1546 case SYMBOL_REF:
1547 return XSTR (x, 0) == XSTR (y, 0);
1549 case ENTRY_VALUE:
1550 /* This is magic, don't go through canonicalization et al. */
1551 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
1553 case VALUE:
1554 CASE_CONST_UNIQUE:
1555 /* Pointer equality guarantees equality for these nodes. */
1556 return 0;
1558 default:
1559 break;
1562 /* canon_rtx knows how to handle plus. No need to canonicalize. */
1563 if (code == PLUS)
1564 return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1565 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1566 || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1567 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1568 /* For commutative operations, the RTX match if the operand match in any
1569 order. Also handle the simple binary and unary cases without a loop. */
1570 if (COMMUTATIVE_P (x))
1572 rtx xop0 = canon_rtx (XEXP (x, 0));
1573 rtx yop0 = canon_rtx (XEXP (y, 0));
1574 rtx yop1 = canon_rtx (XEXP (y, 1));
1576 return ((rtx_equal_for_memref_p (xop0, yop0)
1577 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1578 || (rtx_equal_for_memref_p (xop0, yop1)
1579 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1581 else if (NON_COMMUTATIVE_P (x))
1583 return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1584 canon_rtx (XEXP (y, 0)))
1585 && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1586 canon_rtx (XEXP (y, 1))));
1588 else if (UNARY_P (x))
1589 return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1590 canon_rtx (XEXP (y, 0)));
1592 /* Compare the elements. If any pair of corresponding elements
1593 fail to match, return 0 for the whole things.
1595 Limit cases to types which actually appear in addresses. */
1597 fmt = GET_RTX_FORMAT (code);
1598 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1600 switch (fmt[i])
1602 case 'i':
1603 if (XINT (x, i) != XINT (y, i))
1604 return 0;
1605 break;
1607 case 'E':
1608 /* Two vectors must have the same length. */
1609 if (XVECLEN (x, i) != XVECLEN (y, i))
1610 return 0;
1612 /* And the corresponding elements must match. */
1613 for (j = 0; j < XVECLEN (x, i); j++)
1614 if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1615 canon_rtx (XVECEXP (y, i, j))) == 0)
1616 return 0;
1617 break;
1619 case 'e':
1620 if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1621 canon_rtx (XEXP (y, i))) == 0)
1622 return 0;
1623 break;
1625 /* This can happen for asm operands. */
1626 case 's':
1627 if (strcmp (XSTR (x, i), XSTR (y, i)))
1628 return 0;
1629 break;
1631 /* This can happen for an asm which clobbers memory. */
1632 case '0':
1633 break;
1635 /* It is believed that rtx's at this level will never
1636 contain anything but integers and other rtx's,
1637 except for within LABEL_REFs and SYMBOL_REFs. */
1638 default:
1639 gcc_unreachable ();
1642 return 1;
1645 static rtx
1646 find_base_term (rtx x)
1648 cselib_val *val;
1649 struct elt_loc_list *l, *f;
1650 rtx ret;
1652 #if defined (FIND_BASE_TERM)
1653 /* Try machine-dependent ways to find the base term. */
1654 x = FIND_BASE_TERM (x);
1655 #endif
1657 switch (GET_CODE (x))
1659 case REG:
1660 return REG_BASE_VALUE (x);
1662 case TRUNCATE:
1663 /* As we do not know which address space the pointer is referring to, we can
1664 handle this only if the target does not support different pointer or
1665 address modes depending on the address space. */
1666 if (!target_default_pointer_address_modes_p ())
1667 return 0;
1668 if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1669 return 0;
1670 /* Fall through. */
1671 case HIGH:
1672 case PRE_INC:
1673 case PRE_DEC:
1674 case POST_INC:
1675 case POST_DEC:
1676 case PRE_MODIFY:
1677 case POST_MODIFY:
1678 return find_base_term (XEXP (x, 0));
1680 case ZERO_EXTEND:
1681 case SIGN_EXTEND: /* Used for Alpha/NT pointers */
1682 /* As we do not know which address space the pointer is referring to, we can
1683 handle this only if the target does not support different pointer or
1684 address modes depending on the address space. */
1685 if (!target_default_pointer_address_modes_p ())
1686 return 0;
1689 rtx temp = find_base_term (XEXP (x, 0));
1691 if (temp != 0 && CONSTANT_P (temp))
1692 temp = convert_memory_address (Pmode, temp);
1694 return temp;
1697 case VALUE:
1698 val = CSELIB_VAL_PTR (x);
1699 ret = NULL_RTX;
1701 if (!val)
1702 return ret;
1704 if (cselib_sp_based_value_p (val))
1705 return static_reg_base_value[STACK_POINTER_REGNUM];
1707 f = val->locs;
1708 /* Temporarily reset val->locs to avoid infinite recursion. */
1709 val->locs = NULL;
1711 for (l = f; l; l = l->next)
1712 if (GET_CODE (l->loc) == VALUE
1713 && CSELIB_VAL_PTR (l->loc)->locs
1714 && !CSELIB_VAL_PTR (l->loc)->locs->next
1715 && CSELIB_VAL_PTR (l->loc)->locs->loc == x)
1716 continue;
1717 else if ((ret = find_base_term (l->loc)) != 0)
1718 break;
1720 val->locs = f;
1721 return ret;
1723 case LO_SUM:
1724 /* The standard form is (lo_sum reg sym) so look only at the
1725 second operand. */
1726 return find_base_term (XEXP (x, 1));
1728 case CONST:
1729 x = XEXP (x, 0);
1730 if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1731 return 0;
1732 /* Fall through. */
1733 case PLUS:
1734 case MINUS:
1736 rtx tmp1 = XEXP (x, 0);
1737 rtx tmp2 = XEXP (x, 1);
1739 /* This is a little bit tricky since we have to determine which of
1740 the two operands represents the real base address. Otherwise this
1741 routine may return the index register instead of the base register.
1743 That may cause us to believe no aliasing was possible, when in
1744 fact aliasing is possible.
1746 We use a few simple tests to guess the base register. Additional
1747 tests can certainly be added. For example, if one of the operands
1748 is a shift or multiply, then it must be the index register and the
1749 other operand is the base register. */
1751 if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1752 return find_base_term (tmp2);
1754 /* If either operand is known to be a pointer, then prefer it
1755 to determine the base term. */
1756 if (REG_P (tmp1) && REG_POINTER (tmp1))
1758 else if (REG_P (tmp2) && REG_POINTER (tmp2))
1760 rtx tem = tmp1;
1761 tmp1 = tmp2;
1762 tmp2 = tem;
1765 /* Go ahead and find the base term for both operands. If either base
1766 term is from a pointer or is a named object or a special address
1767 (like an argument or stack reference), then use it for the
1768 base term. */
1769 rtx base = find_base_term (tmp1);
1770 if (base != NULL_RTX
1771 && ((REG_P (tmp1) && REG_POINTER (tmp1))
1772 || known_base_value_p (base)))
1773 return base;
1774 base = find_base_term (tmp2);
1775 if (base != NULL_RTX
1776 && ((REG_P (tmp2) && REG_POINTER (tmp2))
1777 || known_base_value_p (base)))
1778 return base;
1780 /* We could not determine which of the two operands was the
1781 base register and which was the index. So we can determine
1782 nothing from the base alias check. */
1783 return 0;
1786 case AND:
1787 if (CONST_INT_P (XEXP (x, 1)) && INTVAL (XEXP (x, 1)) != 0)
1788 return find_base_term (XEXP (x, 0));
1789 return 0;
1791 case SYMBOL_REF:
1792 case LABEL_REF:
1793 return x;
1795 default:
1796 return 0;
1800 /* Return true if accesses to address X may alias accesses based
1801 on the stack pointer. */
1803 bool
1804 may_be_sp_based_p (rtx x)
1806 rtx base = find_base_term (x);
1807 return !base || base == static_reg_base_value[STACK_POINTER_REGNUM];
1810 /* Return 0 if the addresses X and Y are known to point to different
1811 objects, 1 if they might be pointers to the same object. */
1813 static int
1814 base_alias_check (rtx x, rtx x_base, rtx y, rtx y_base,
1815 machine_mode x_mode, machine_mode y_mode)
1817 /* If the address itself has no known base see if a known equivalent
1818 value has one. If either address still has no known base, nothing
1819 is known about aliasing. */
1820 if (x_base == 0)
1822 rtx x_c;
1824 if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1825 return 1;
1827 x_base = find_base_term (x_c);
1828 if (x_base == 0)
1829 return 1;
1832 if (y_base == 0)
1834 rtx y_c;
1835 if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1836 return 1;
1838 y_base = find_base_term (y_c);
1839 if (y_base == 0)
1840 return 1;
1843 /* If the base addresses are equal nothing is known about aliasing. */
1844 if (rtx_equal_p (x_base, y_base))
1845 return 1;
1847 /* The base addresses are different expressions. If they are not accessed
1848 via AND, there is no conflict. We can bring knowledge of object
1849 alignment into play here. For example, on alpha, "char a, b;" can
1850 alias one another, though "char a; long b;" cannot. AND addesses may
1851 implicitly alias surrounding objects; i.e. unaligned access in DImode
1852 via AND address can alias all surrounding object types except those
1853 with aligment 8 or higher. */
1854 if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1855 return 1;
1856 if (GET_CODE (x) == AND
1857 && (!CONST_INT_P (XEXP (x, 1))
1858 || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1859 return 1;
1860 if (GET_CODE (y) == AND
1861 && (!CONST_INT_P (XEXP (y, 1))
1862 || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1863 return 1;
1865 /* Differing symbols not accessed via AND never alias. */
1866 if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1867 return 0;
1869 if (unique_base_value_p (x_base) || unique_base_value_p (y_base))
1870 return 0;
1872 return 1;
1875 /* Return TRUE if EXPR refers to a VALUE whose uid is greater than
1876 that of V. */
1878 static bool
1879 refs_newer_value_p (const_rtx expr, rtx v)
1881 int minuid = CSELIB_VAL_PTR (v)->uid;
1882 subrtx_iterator::array_type array;
1883 FOR_EACH_SUBRTX (iter, array, expr, NONCONST)
1884 if (GET_CODE (*iter) == VALUE && CSELIB_VAL_PTR (*iter)->uid > minuid)
1885 return true;
1886 return false;
1889 /* Convert the address X into something we can use. This is done by returning
1890 it unchanged unless it is a value; in the latter case we call cselib to get
1891 a more useful rtx. */
1894 get_addr (rtx x)
1896 cselib_val *v;
1897 struct elt_loc_list *l;
1899 if (GET_CODE (x) != VALUE)
1900 return x;
1901 v = CSELIB_VAL_PTR (x);
1902 if (v)
1904 bool have_equivs = cselib_have_permanent_equivalences ();
1905 if (have_equivs)
1906 v = canonical_cselib_val (v);
1907 for (l = v->locs; l; l = l->next)
1908 if (CONSTANT_P (l->loc))
1909 return l->loc;
1910 for (l = v->locs; l; l = l->next)
1911 if (!REG_P (l->loc) && !MEM_P (l->loc)
1912 /* Avoid infinite recursion when potentially dealing with
1913 var-tracking artificial equivalences, by skipping the
1914 equivalences themselves, and not choosing expressions
1915 that refer to newer VALUEs. */
1916 && (!have_equivs
1917 || (GET_CODE (l->loc) != VALUE
1918 && !refs_newer_value_p (l->loc, x))))
1919 return l->loc;
1920 if (have_equivs)
1922 for (l = v->locs; l; l = l->next)
1923 if (REG_P (l->loc)
1924 || (GET_CODE (l->loc) != VALUE
1925 && !refs_newer_value_p (l->loc, x)))
1926 return l->loc;
1927 /* Return the canonical value. */
1928 return v->val_rtx;
1930 if (v->locs)
1931 return v->locs->loc;
1933 return x;
1936 /* Return the address of the (N_REFS + 1)th memory reference to ADDR
1937 where SIZE is the size in bytes of the memory reference. If ADDR
1938 is not modified by the memory reference then ADDR is returned. */
1940 static rtx
1941 addr_side_effect_eval (rtx addr, int size, int n_refs)
1943 int offset = 0;
1945 switch (GET_CODE (addr))
1947 case PRE_INC:
1948 offset = (n_refs + 1) * size;
1949 break;
1950 case PRE_DEC:
1951 offset = -(n_refs + 1) * size;
1952 break;
1953 case POST_INC:
1954 offset = n_refs * size;
1955 break;
1956 case POST_DEC:
1957 offset = -n_refs * size;
1958 break;
1960 default:
1961 return addr;
1964 if (offset)
1965 addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
1966 gen_int_mode (offset, GET_MODE (addr)));
1967 else
1968 addr = XEXP (addr, 0);
1969 addr = canon_rtx (addr);
1971 return addr;
1974 /* Return TRUE if an object X sized at XSIZE bytes and another object
1975 Y sized at YSIZE bytes, starting C bytes after X, may overlap. If
1976 any of the sizes is zero, assume an overlap, otherwise use the
1977 absolute value of the sizes as the actual sizes. */
1979 static inline bool
1980 offset_overlap_p (HOST_WIDE_INT c, int xsize, int ysize)
1982 return (xsize == 0 || ysize == 0
1983 || (c >= 0
1984 ? (abs (xsize) > c)
1985 : (abs (ysize) > -c)));
1988 /* Return one if X and Y (memory addresses) reference the
1989 same location in memory or if the references overlap.
1990 Return zero if they do not overlap, else return
1991 minus one in which case they still might reference the same location.
1993 C is an offset accumulator. When
1994 C is nonzero, we are testing aliases between X and Y + C.
1995 XSIZE is the size in bytes of the X reference,
1996 similarly YSIZE is the size in bytes for Y.
1997 Expect that canon_rtx has been already called for X and Y.
1999 If XSIZE or YSIZE is zero, we do not know the amount of memory being
2000 referenced (the reference was BLKmode), so make the most pessimistic
2001 assumptions.
2003 If XSIZE or YSIZE is negative, we may access memory outside the object
2004 being referenced as a side effect. This can happen when using AND to
2005 align memory references, as is done on the Alpha.
2007 Nice to notice that varying addresses cannot conflict with fp if no
2008 local variables had their addresses taken, but that's too hard now.
2010 ??? Contrary to the tree alias oracle this does not return
2011 one for X + non-constant and Y + non-constant when X and Y are equal.
2012 If that is fixed the TBAA hack for union type-punning can be removed. */
2014 static int
2015 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
2017 if (GET_CODE (x) == VALUE)
2019 if (REG_P (y))
2021 struct elt_loc_list *l = NULL;
2022 if (CSELIB_VAL_PTR (x))
2023 for (l = canonical_cselib_val (CSELIB_VAL_PTR (x))->locs;
2024 l; l = l->next)
2025 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, y))
2026 break;
2027 if (l)
2028 x = y;
2029 else
2030 x = get_addr (x);
2032 /* Don't call get_addr if y is the same VALUE. */
2033 else if (x != y)
2034 x = get_addr (x);
2036 if (GET_CODE (y) == VALUE)
2038 if (REG_P (x))
2040 struct elt_loc_list *l = NULL;
2041 if (CSELIB_VAL_PTR (y))
2042 for (l = canonical_cselib_val (CSELIB_VAL_PTR (y))->locs;
2043 l; l = l->next)
2044 if (REG_P (l->loc) && rtx_equal_for_memref_p (l->loc, x))
2045 break;
2046 if (l)
2047 y = x;
2048 else
2049 y = get_addr (y);
2051 /* Don't call get_addr if x is the same VALUE. */
2052 else if (y != x)
2053 y = get_addr (y);
2055 if (GET_CODE (x) == HIGH)
2056 x = XEXP (x, 0);
2057 else if (GET_CODE (x) == LO_SUM)
2058 x = XEXP (x, 1);
2059 else
2060 x = addr_side_effect_eval (x, abs (xsize), 0);
2061 if (GET_CODE (y) == HIGH)
2062 y = XEXP (y, 0);
2063 else if (GET_CODE (y) == LO_SUM)
2064 y = XEXP (y, 1);
2065 else
2066 y = addr_side_effect_eval (y, abs (ysize), 0);
2068 if (rtx_equal_for_memref_p (x, y))
2070 return offset_overlap_p (c, xsize, ysize);
2073 /* This code used to check for conflicts involving stack references and
2074 globals but the base address alias code now handles these cases. */
2076 if (GET_CODE (x) == PLUS)
2078 /* The fact that X is canonicalized means that this
2079 PLUS rtx is canonicalized. */
2080 rtx x0 = XEXP (x, 0);
2081 rtx x1 = XEXP (x, 1);
2083 if (GET_CODE (y) == PLUS)
2085 /* The fact that Y is canonicalized means that this
2086 PLUS rtx is canonicalized. */
2087 rtx y0 = XEXP (y, 0);
2088 rtx y1 = XEXP (y, 1);
2090 if (rtx_equal_for_memref_p (x1, y1))
2091 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2092 if (rtx_equal_for_memref_p (x0, y0))
2093 return memrefs_conflict_p (xsize, x1, ysize, y1, c);
2094 if (CONST_INT_P (x1))
2096 if (CONST_INT_P (y1))
2097 return memrefs_conflict_p (xsize, x0, ysize, y0,
2098 c - INTVAL (x1) + INTVAL (y1));
2099 else
2100 return memrefs_conflict_p (xsize, x0, ysize, y,
2101 c - INTVAL (x1));
2103 else if (CONST_INT_P (y1))
2104 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2106 return -1;
2108 else if (CONST_INT_P (x1))
2109 return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
2111 else if (GET_CODE (y) == PLUS)
2113 /* The fact that Y is canonicalized means that this
2114 PLUS rtx is canonicalized. */
2115 rtx y0 = XEXP (y, 0);
2116 rtx y1 = XEXP (y, 1);
2118 if (CONST_INT_P (y1))
2119 return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
2120 else
2121 return -1;
2124 if (GET_CODE (x) == GET_CODE (y))
2125 switch (GET_CODE (x))
2127 case MULT:
2129 /* Handle cases where we expect the second operands to be the
2130 same, and check only whether the first operand would conflict
2131 or not. */
2132 rtx x0, y0;
2133 rtx x1 = canon_rtx (XEXP (x, 1));
2134 rtx y1 = canon_rtx (XEXP (y, 1));
2135 if (! rtx_equal_for_memref_p (x1, y1))
2136 return -1;
2137 x0 = canon_rtx (XEXP (x, 0));
2138 y0 = canon_rtx (XEXP (y, 0));
2139 if (rtx_equal_for_memref_p (x0, y0))
2140 return offset_overlap_p (c, xsize, ysize);
2142 /* Can't properly adjust our sizes. */
2143 if (!CONST_INT_P (x1))
2144 return -1;
2145 xsize /= INTVAL (x1);
2146 ysize /= INTVAL (x1);
2147 c /= INTVAL (x1);
2148 return memrefs_conflict_p (xsize, x0, ysize, y0, c);
2151 default:
2152 break;
2155 /* Deal with alignment ANDs by adjusting offset and size so as to
2156 cover the maximum range, without taking any previously known
2157 alignment into account. Make a size negative after such an
2158 adjustments, so that, if we end up with e.g. two SYMBOL_REFs, we
2159 assume a potential overlap, because they may end up in contiguous
2160 memory locations and the stricter-alignment access may span over
2161 part of both. */
2162 if (GET_CODE (x) == AND && CONST_INT_P (XEXP (x, 1)))
2164 HOST_WIDE_INT sc = INTVAL (XEXP (x, 1));
2165 unsigned HOST_WIDE_INT uc = sc;
2166 if (sc < 0 && -uc == (uc & -uc))
2168 if (xsize > 0)
2169 xsize = -xsize;
2170 if (xsize)
2171 xsize += sc + 1;
2172 c -= sc + 1;
2173 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2174 ysize, y, c);
2177 if (GET_CODE (y) == AND && CONST_INT_P (XEXP (y, 1)))
2179 HOST_WIDE_INT sc = INTVAL (XEXP (y, 1));
2180 unsigned HOST_WIDE_INT uc = sc;
2181 if (sc < 0 && -uc == (uc & -uc))
2183 if (ysize > 0)
2184 ysize = -ysize;
2185 if (ysize)
2186 ysize += sc + 1;
2187 c += sc + 1;
2188 return memrefs_conflict_p (xsize, x,
2189 ysize, canon_rtx (XEXP (y, 0)), c);
2193 if (CONSTANT_P (x))
2195 if (CONST_INT_P (x) && CONST_INT_P (y))
2197 c += (INTVAL (y) - INTVAL (x));
2198 return offset_overlap_p (c, xsize, ysize);
2201 if (GET_CODE (x) == CONST)
2203 if (GET_CODE (y) == CONST)
2204 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2205 ysize, canon_rtx (XEXP (y, 0)), c);
2206 else
2207 return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
2208 ysize, y, c);
2210 if (GET_CODE (y) == CONST)
2211 return memrefs_conflict_p (xsize, x, ysize,
2212 canon_rtx (XEXP (y, 0)), c);
2214 /* Assume a potential overlap for symbolic addresses that went
2215 through alignment adjustments (i.e., that have negative
2216 sizes), because we can't know how far they are from each
2217 other. */
2218 if (CONSTANT_P (y))
2219 return (xsize < 0 || ysize < 0 || offset_overlap_p (c, xsize, ysize));
2221 return -1;
2224 return -1;
2227 /* Functions to compute memory dependencies.
2229 Since we process the insns in execution order, we can build tables
2230 to keep track of what registers are fixed (and not aliased), what registers
2231 are varying in known ways, and what registers are varying in unknown
2232 ways.
2234 If both memory references are volatile, then there must always be a
2235 dependence between the two references, since their order can not be
2236 changed. A volatile and non-volatile reference can be interchanged
2237 though.
2239 We also must allow AND addresses, because they may generate accesses
2240 outside the object being referenced. This is used to generate aligned
2241 addresses from unaligned addresses, for instance, the alpha
2242 storeqi_unaligned pattern. */
2244 /* Read dependence: X is read after read in MEM takes place. There can
2245 only be a dependence here if both reads are volatile, or if either is
2246 an explicit barrier. */
2249 read_dependence (const_rtx mem, const_rtx x)
2251 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2252 return true;
2253 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2254 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2255 return true;
2256 return false;
2259 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it. */
2261 static tree
2262 decl_for_component_ref (tree x)
2266 x = TREE_OPERAND (x, 0);
2268 while (x && TREE_CODE (x) == COMPONENT_REF);
2270 return x && DECL_P (x) ? x : NULL_TREE;
2273 /* Walk up the COMPONENT_REF list in X and adjust *OFFSET to compensate
2274 for the offset of the field reference. *KNOWN_P says whether the
2275 offset is known. */
2277 static void
2278 adjust_offset_for_component_ref (tree x, bool *known_p,
2279 HOST_WIDE_INT *offset)
2281 if (!*known_p)
2282 return;
2285 tree xoffset = component_ref_field_offset (x);
2286 tree field = TREE_OPERAND (x, 1);
2287 if (TREE_CODE (xoffset) != INTEGER_CST)
2289 *known_p = false;
2290 return;
2293 offset_int woffset
2294 = (wi::to_offset (xoffset)
2295 + wi::lrshift (wi::to_offset (DECL_FIELD_BIT_OFFSET (field)),
2296 LOG2_BITS_PER_UNIT));
2297 if (!wi::fits_uhwi_p (woffset))
2299 *known_p = false;
2300 return;
2302 *offset += woffset.to_uhwi ();
2304 x = TREE_OPERAND (x, 0);
2306 while (x && TREE_CODE (x) == COMPONENT_REF);
2309 /* Return nonzero if we can determine the exprs corresponding to memrefs
2310 X and Y and they do not overlap.
2311 If LOOP_VARIANT is set, skip offset-based disambiguation */
2314 nonoverlapping_memrefs_p (const_rtx x, const_rtx y, bool loop_invariant)
2316 tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2317 rtx rtlx, rtly;
2318 rtx basex, basey;
2319 bool moffsetx_known_p, moffsety_known_p;
2320 HOST_WIDE_INT moffsetx = 0, moffsety = 0;
2321 HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2323 /* Unless both have exprs, we can't tell anything. */
2324 if (exprx == 0 || expry == 0)
2325 return 0;
2327 /* For spill-slot accesses make sure we have valid offsets. */
2328 if ((exprx == get_spill_slot_decl (false)
2329 && ! MEM_OFFSET_KNOWN_P (x))
2330 || (expry == get_spill_slot_decl (false)
2331 && ! MEM_OFFSET_KNOWN_P (y)))
2332 return 0;
2334 /* If the field reference test failed, look at the DECLs involved. */
2335 moffsetx_known_p = MEM_OFFSET_KNOWN_P (x);
2336 if (moffsetx_known_p)
2337 moffsetx = MEM_OFFSET (x);
2338 if (TREE_CODE (exprx) == COMPONENT_REF)
2340 tree t = decl_for_component_ref (exprx);
2341 if (! t)
2342 return 0;
2343 adjust_offset_for_component_ref (exprx, &moffsetx_known_p, &moffsetx);
2344 exprx = t;
2347 moffsety_known_p = MEM_OFFSET_KNOWN_P (y);
2348 if (moffsety_known_p)
2349 moffsety = MEM_OFFSET (y);
2350 if (TREE_CODE (expry) == COMPONENT_REF)
2352 tree t = decl_for_component_ref (expry);
2353 if (! t)
2354 return 0;
2355 adjust_offset_for_component_ref (expry, &moffsety_known_p, &moffsety);
2356 expry = t;
2359 if (! DECL_P (exprx) || ! DECL_P (expry))
2360 return 0;
2362 /* With invalid code we can end up storing into the constant pool.
2363 Bail out to avoid ICEing when creating RTL for this.
2364 See gfortran.dg/lto/20091028-2_0.f90. */
2365 if (TREE_CODE (exprx) == CONST_DECL
2366 || TREE_CODE (expry) == CONST_DECL)
2367 return 1;
2369 rtlx = DECL_RTL (exprx);
2370 rtly = DECL_RTL (expry);
2372 /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2373 can't overlap unless they are the same because we never reuse that part
2374 of the stack frame used for locals for spilled pseudos. */
2375 if ((!MEM_P (rtlx) || !MEM_P (rtly))
2376 && ! rtx_equal_p (rtlx, rtly))
2377 return 1;
2379 /* If we have MEMs referring to different address spaces (which can
2380 potentially overlap), we cannot easily tell from the addresses
2381 whether the references overlap. */
2382 if (MEM_P (rtlx) && MEM_P (rtly)
2383 && MEM_ADDR_SPACE (rtlx) != MEM_ADDR_SPACE (rtly))
2384 return 0;
2386 /* Get the base and offsets of both decls. If either is a register, we
2387 know both are and are the same, so use that as the base. The only
2388 we can avoid overlap is if we can deduce that they are nonoverlapping
2389 pieces of that decl, which is very rare. */
2390 basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2391 if (GET_CODE (basex) == PLUS && CONST_INT_P (XEXP (basex, 1)))
2392 offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2394 basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2395 if (GET_CODE (basey) == PLUS && CONST_INT_P (XEXP (basey, 1)))
2396 offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2398 /* If the bases are different, we know they do not overlap if both
2399 are constants or if one is a constant and the other a pointer into the
2400 stack frame. Otherwise a different base means we can't tell if they
2401 overlap or not. */
2402 if (! rtx_equal_p (basex, basey))
2403 return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2404 || (CONSTANT_P (basex) && REG_P (basey)
2405 && REGNO_PTR_FRAME_P (REGNO (basey)))
2406 || (CONSTANT_P (basey) && REG_P (basex)
2407 && REGNO_PTR_FRAME_P (REGNO (basex))));
2409 /* Offset based disambiguation not appropriate for loop invariant */
2410 if (loop_invariant)
2411 return 0;
2413 sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2414 : MEM_SIZE_KNOWN_P (rtlx) ? MEM_SIZE (rtlx)
2415 : -1);
2416 sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2417 : MEM_SIZE_KNOWN_P (rtly) ? MEM_SIZE (rtly)
2418 : -1);
2420 /* If we have an offset for either memref, it can update the values computed
2421 above. */
2422 if (moffsetx_known_p)
2423 offsetx += moffsetx, sizex -= moffsetx;
2424 if (moffsety_known_p)
2425 offsety += moffsety, sizey -= moffsety;
2427 /* If a memref has both a size and an offset, we can use the smaller size.
2428 We can't do this if the offset isn't known because we must view this
2429 memref as being anywhere inside the DECL's MEM. */
2430 if (MEM_SIZE_KNOWN_P (x) && moffsetx_known_p)
2431 sizex = MEM_SIZE (x);
2432 if (MEM_SIZE_KNOWN_P (y) && moffsety_known_p)
2433 sizey = MEM_SIZE (y);
2435 /* Put the values of the memref with the lower offset in X's values. */
2436 if (offsetx > offsety)
2438 tem = offsetx, offsetx = offsety, offsety = tem;
2439 tem = sizex, sizex = sizey, sizey = tem;
2442 /* If we don't know the size of the lower-offset value, we can't tell
2443 if they conflict. Otherwise, we do the test. */
2444 return sizex >= 0 && offsety >= offsetx + sizex;
2447 /* Helper for true_dependence and canon_true_dependence.
2448 Checks for true dependence: X is read after store in MEM takes place.
2450 If MEM_CANONICALIZED is FALSE, then X_ADDR and MEM_ADDR should be
2451 NULL_RTX, and the canonical addresses of MEM and X are both computed
2452 here. If MEM_CANONICALIZED, then MEM must be already canonicalized.
2454 If X_ADDR is non-NULL, it is used in preference of XEXP (x, 0).
2456 Returns 1 if there is a true dependence, 0 otherwise. */
2458 static int
2459 true_dependence_1 (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2460 const_rtx x, rtx x_addr, bool mem_canonicalized)
2462 rtx true_mem_addr;
2463 rtx base;
2464 int ret;
2466 gcc_checking_assert (mem_canonicalized ? (mem_addr != NULL_RTX)
2467 : (mem_addr == NULL_RTX && x_addr == NULL_RTX));
2469 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2470 return 1;
2472 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2473 This is used in epilogue deallocation functions, and in cselib. */
2474 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2475 return 1;
2476 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2477 return 1;
2478 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2479 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2480 return 1;
2482 if (! x_addr)
2483 x_addr = XEXP (x, 0);
2484 x_addr = get_addr (x_addr);
2486 if (! mem_addr)
2488 mem_addr = XEXP (mem, 0);
2489 if (mem_mode == VOIDmode)
2490 mem_mode = GET_MODE (mem);
2492 true_mem_addr = get_addr (mem_addr);
2494 /* Read-only memory is by definition never modified, and therefore can't
2495 conflict with anything. However, don't assume anything when AND
2496 addresses are involved and leave to the code below to determine
2497 dependence. We don't expect to find read-only set on MEM, but
2498 stupid user tricks can produce them, so don't die. */
2499 if (MEM_READONLY_P (x)
2500 && GET_CODE (x_addr) != AND
2501 && GET_CODE (true_mem_addr) != AND)
2502 return 0;
2504 /* If we have MEMs referring to different address spaces (which can
2505 potentially overlap), we cannot easily tell from the addresses
2506 whether the references overlap. */
2507 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2508 return 1;
2510 base = find_base_term (x_addr);
2511 if (base && (GET_CODE (base) == LABEL_REF
2512 || (GET_CODE (base) == SYMBOL_REF
2513 && CONSTANT_POOL_ADDRESS_P (base))))
2514 return 0;
2516 rtx mem_base = find_base_term (true_mem_addr);
2517 if (! base_alias_check (x_addr, base, true_mem_addr, mem_base,
2518 GET_MODE (x), mem_mode))
2519 return 0;
2521 x_addr = canon_rtx (x_addr);
2522 if (!mem_canonicalized)
2523 mem_addr = canon_rtx (true_mem_addr);
2525 if ((ret = memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2526 SIZE_FOR_MODE (x), x_addr, 0)) != -1)
2527 return ret;
2529 if (mems_in_disjoint_alias_sets_p (x, mem))
2530 return 0;
2532 if (nonoverlapping_memrefs_p (mem, x, false))
2533 return 0;
2535 return rtx_refs_may_alias_p (x, mem, true);
2538 /* True dependence: X is read after store in MEM takes place. */
2541 true_dependence (const_rtx mem, machine_mode mem_mode, const_rtx x)
2543 return true_dependence_1 (mem, mem_mode, NULL_RTX,
2544 x, NULL_RTX, /*mem_canonicalized=*/false);
2547 /* Canonical true dependence: X is read after store in MEM takes place.
2548 Variant of true_dependence which assumes MEM has already been
2549 canonicalized (hence we no longer do that here).
2550 The mem_addr argument has been added, since true_dependence_1 computed
2551 this value prior to canonicalizing. */
2554 canon_true_dependence (const_rtx mem, machine_mode mem_mode, rtx mem_addr,
2555 const_rtx x, rtx x_addr)
2557 return true_dependence_1 (mem, mem_mode, mem_addr,
2558 x, x_addr, /*mem_canonicalized=*/true);
2561 /* Returns nonzero if a write to X might alias a previous read from
2562 (or, if WRITEP is true, a write to) MEM.
2563 If X_CANONCALIZED is true, then X_ADDR is the canonicalized address of X,
2564 and X_MODE the mode for that access.
2565 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2567 static int
2568 write_dependence_p (const_rtx mem,
2569 const_rtx x, machine_mode x_mode, rtx x_addr,
2570 bool mem_canonicalized, bool x_canonicalized, bool writep)
2572 rtx mem_addr;
2573 rtx true_mem_addr, true_x_addr;
2574 rtx base;
2575 int ret;
2577 gcc_checking_assert (x_canonicalized
2578 ? (x_addr != NULL_RTX && x_mode != VOIDmode)
2579 : (x_addr == NULL_RTX && x_mode == VOIDmode));
2581 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2582 return 1;
2584 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2585 This is used in epilogue deallocation functions. */
2586 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2587 return 1;
2588 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2589 return 1;
2590 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2591 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2592 return 1;
2594 if (!x_addr)
2595 x_addr = XEXP (x, 0);
2596 true_x_addr = get_addr (x_addr);
2598 mem_addr = XEXP (mem, 0);
2599 true_mem_addr = get_addr (mem_addr);
2601 /* A read from read-only memory can't conflict with read-write memory.
2602 Don't assume anything when AND addresses are involved and leave to
2603 the code below to determine dependence. */
2604 if (!writep
2605 && MEM_READONLY_P (mem)
2606 && GET_CODE (true_x_addr) != AND
2607 && GET_CODE (true_mem_addr) != AND)
2608 return 0;
2610 /* If we have MEMs referring to different address spaces (which can
2611 potentially overlap), we cannot easily tell from the addresses
2612 whether the references overlap. */
2613 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2614 return 1;
2616 base = find_base_term (true_mem_addr);
2617 if (! writep
2618 && base
2619 && (GET_CODE (base) == LABEL_REF
2620 || (GET_CODE (base) == SYMBOL_REF
2621 && CONSTANT_POOL_ADDRESS_P (base))))
2622 return 0;
2624 rtx x_base = find_base_term (true_x_addr);
2625 if (! base_alias_check (true_x_addr, x_base, true_mem_addr, base,
2626 GET_MODE (x), GET_MODE (mem)))
2627 return 0;
2629 if (!x_canonicalized)
2631 x_addr = canon_rtx (true_x_addr);
2632 x_mode = GET_MODE (x);
2634 if (!mem_canonicalized)
2635 mem_addr = canon_rtx (true_mem_addr);
2637 if ((ret = memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2638 GET_MODE_SIZE (x_mode), x_addr, 0)) != -1)
2639 return ret;
2641 if (nonoverlapping_memrefs_p (x, mem, false))
2642 return 0;
2644 return rtx_refs_may_alias_p (x, mem, false);
2647 /* Anti dependence: X is written after read in MEM takes place. */
2650 anti_dependence (const_rtx mem, const_rtx x)
2652 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2653 /*mem_canonicalized=*/false,
2654 /*x_canonicalized*/false, /*writep=*/false);
2657 /* Likewise, but we already have a canonicalized MEM, and X_ADDR for X.
2658 Also, consider X in X_MODE (which might be from an enclosing
2659 STRICT_LOW_PART / ZERO_EXTRACT).
2660 If MEM_CANONICALIZED is true, MEM is canonicalized. */
2663 canon_anti_dependence (const_rtx mem, bool mem_canonicalized,
2664 const_rtx x, machine_mode x_mode, rtx x_addr)
2666 return write_dependence_p (mem, x, x_mode, x_addr,
2667 mem_canonicalized, /*x_canonicalized=*/true,
2668 /*writep=*/false);
2671 /* Output dependence: X is written after store in MEM takes place. */
2674 output_dependence (const_rtx mem, const_rtx x)
2676 return write_dependence_p (mem, x, VOIDmode, NULL_RTX,
2677 /*mem_canonicalized=*/false,
2678 /*x_canonicalized*/false, /*writep=*/true);
2683 /* Check whether X may be aliased with MEM. Don't do offset-based
2684 memory disambiguation & TBAA. */
2686 may_alias_p (const_rtx mem, const_rtx x)
2688 rtx x_addr, mem_addr;
2690 if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2691 return 1;
2693 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2694 This is used in epilogue deallocation functions. */
2695 if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2696 return 1;
2697 if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2698 return 1;
2699 if (MEM_ALIAS_SET (x) == ALIAS_SET_MEMORY_BARRIER
2700 || MEM_ALIAS_SET (mem) == ALIAS_SET_MEMORY_BARRIER)
2701 return 1;
2703 x_addr = XEXP (x, 0);
2704 x_addr = get_addr (x_addr);
2706 mem_addr = XEXP (mem, 0);
2707 mem_addr = get_addr (mem_addr);
2709 /* Read-only memory is by definition never modified, and therefore can't
2710 conflict with anything. However, don't assume anything when AND
2711 addresses are involved and leave to the code below to determine
2712 dependence. We don't expect to find read-only set on MEM, but
2713 stupid user tricks can produce them, so don't die. */
2714 if (MEM_READONLY_P (x)
2715 && GET_CODE (x_addr) != AND
2716 && GET_CODE (mem_addr) != AND)
2717 return 0;
2719 /* If we have MEMs referring to different address spaces (which can
2720 potentially overlap), we cannot easily tell from the addresses
2721 whether the references overlap. */
2722 if (MEM_ADDR_SPACE (mem) != MEM_ADDR_SPACE (x))
2723 return 1;
2725 rtx x_base = find_base_term (x_addr);
2726 rtx mem_base = find_base_term (mem_addr);
2727 if (! base_alias_check (x_addr, x_base, mem_addr, mem_base,
2728 GET_MODE (x), GET_MODE (mem_addr)))
2729 return 0;
2731 if (nonoverlapping_memrefs_p (mem, x, true))
2732 return 0;
2734 /* TBAA not valid for loop_invarint */
2735 return rtx_refs_may_alias_p (x, mem, false);
2738 void
2739 init_alias_target (void)
2741 int i;
2743 if (!arg_base_value)
2744 arg_base_value = gen_rtx_ADDRESS (VOIDmode, 0);
2746 memset (static_reg_base_value, 0, sizeof static_reg_base_value);
2748 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2749 /* Check whether this register can hold an incoming pointer
2750 argument. FUNCTION_ARG_REGNO_P tests outgoing register
2751 numbers, so translate if necessary due to register windows. */
2752 if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2753 && HARD_REGNO_MODE_OK (i, Pmode))
2754 static_reg_base_value[i] = arg_base_value;
2756 static_reg_base_value[STACK_POINTER_REGNUM]
2757 = unique_base_value (UNIQUE_BASE_VALUE_SP);
2758 static_reg_base_value[ARG_POINTER_REGNUM]
2759 = unique_base_value (UNIQUE_BASE_VALUE_ARGP);
2760 static_reg_base_value[FRAME_POINTER_REGNUM]
2761 = unique_base_value (UNIQUE_BASE_VALUE_FP);
2762 #if !HARD_FRAME_POINTER_IS_FRAME_POINTER
2763 static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2764 = unique_base_value (UNIQUE_BASE_VALUE_HFP);
2765 #endif
2768 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2769 to be memory reference. */
2770 static bool memory_modified;
2771 static void
2772 memory_modified_1 (rtx x, const_rtx pat ATTRIBUTE_UNUSED, void *data)
2774 if (MEM_P (x))
2776 if (anti_dependence (x, (const_rtx)data) || output_dependence (x, (const_rtx)data))
2777 memory_modified = true;
2782 /* Return true when INSN possibly modify memory contents of MEM
2783 (i.e. address can be modified). */
2784 bool
2785 memory_modified_in_insn_p (const_rtx mem, const_rtx insn)
2787 if (!INSN_P (insn))
2788 return false;
2789 memory_modified = false;
2790 note_stores (PATTERN (insn), memory_modified_1, CONST_CAST_RTX(mem));
2791 return memory_modified;
2794 /* Return TRUE if the destination of a set is rtx identical to
2795 ITEM. */
2796 static inline bool
2797 set_dest_equal_p (const_rtx set, const_rtx item)
2799 rtx dest = SET_DEST (set);
2800 return rtx_equal_p (dest, item);
2803 /* Like memory_modified_in_insn_p, but return TRUE if INSN will
2804 *DEFINITELY* modify the memory contents of MEM. */
2805 bool
2806 memory_must_be_modified_in_insn_p (const_rtx mem, const_rtx insn)
2808 if (!INSN_P (insn))
2809 return false;
2810 insn = PATTERN (insn);
2811 if (GET_CODE (insn) == SET)
2812 return set_dest_equal_p (insn, mem);
2813 else if (GET_CODE (insn) == PARALLEL)
2815 int i;
2816 for (i = 0; i < XVECLEN (insn, 0); i++)
2818 rtx sub = XVECEXP (insn, 0, i);
2819 if (GET_CODE (sub) == SET
2820 && set_dest_equal_p (sub, mem))
2821 return true;
2824 return false;
2827 /* Initialize the aliasing machinery. Initialize the REG_KNOWN_VALUE
2828 array. */
2830 void
2831 init_alias_analysis (void)
2833 unsigned int maxreg = max_reg_num ();
2834 int changed, pass;
2835 int i;
2836 unsigned int ui;
2837 rtx_insn *insn;
2838 rtx val;
2839 int rpo_cnt;
2840 int *rpo;
2842 timevar_push (TV_ALIAS_ANALYSIS);
2844 vec_safe_grow_cleared (reg_known_value, maxreg - FIRST_PSEUDO_REGISTER);
2845 reg_known_equiv_p = sbitmap_alloc (maxreg - FIRST_PSEUDO_REGISTER);
2846 bitmap_clear (reg_known_equiv_p);
2848 /* If we have memory allocated from the previous run, use it. */
2849 if (old_reg_base_value)
2850 reg_base_value = old_reg_base_value;
2852 if (reg_base_value)
2853 reg_base_value->truncate (0);
2855 vec_safe_grow_cleared (reg_base_value, maxreg);
2857 new_reg_base_value = XNEWVEC (rtx, maxreg);
2858 reg_seen = sbitmap_alloc (maxreg);
2860 /* The basic idea is that each pass through this loop will use the
2861 "constant" information from the previous pass to propagate alias
2862 information through another level of assignments.
2864 The propagation is done on the CFG in reverse post-order, to propagate
2865 things forward as far as possible in each iteration.
2867 This could get expensive if the assignment chains are long. Maybe
2868 we should throttle the number of iterations, possibly based on
2869 the optimization level or flag_expensive_optimizations.
2871 We could propagate more information in the first pass by making use
2872 of DF_REG_DEF_COUNT to determine immediately that the alias information
2873 for a pseudo is "constant".
2875 A program with an uninitialized variable can cause an infinite loop
2876 here. Instead of doing a full dataflow analysis to detect such problems
2877 we just cap the number of iterations for the loop.
2879 The state of the arrays for the set chain in question does not matter
2880 since the program has undefined behavior. */
2882 rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2883 rpo_cnt = pre_and_rev_post_order_compute (NULL, rpo, false);
2885 pass = 0;
2888 /* Assume nothing will change this iteration of the loop. */
2889 changed = 0;
2891 /* We want to assign the same IDs each iteration of this loop, so
2892 start counting from one each iteration of the loop. */
2893 unique_id = 1;
2895 /* We're at the start of the function each iteration through the
2896 loop, so we're copying arguments. */
2897 copying_arguments = true;
2899 /* Wipe the potential alias information clean for this pass. */
2900 memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
2902 /* Wipe the reg_seen array clean. */
2903 bitmap_clear (reg_seen);
2905 /* Initialize the alias information for this pass. */
2906 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2907 if (static_reg_base_value[i])
2909 new_reg_base_value[i] = static_reg_base_value[i];
2910 bitmap_set_bit (reg_seen, i);
2913 /* Walk the insns adding values to the new_reg_base_value array. */
2914 for (i = 0; i < rpo_cnt; i++)
2916 basic_block bb = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2917 FOR_BB_INSNS (bb, insn)
2919 if (NONDEBUG_INSN_P (insn))
2921 rtx note, set;
2923 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2924 /* The prologue/epilogue insns are not threaded onto the
2925 insn chain until after reload has completed. Thus,
2926 there is no sense wasting time checking if INSN is in
2927 the prologue/epilogue until after reload has completed. */
2928 if (reload_completed
2929 && prologue_epilogue_contains (insn))
2930 continue;
2931 #endif
2933 /* If this insn has a noalias note, process it, Otherwise,
2934 scan for sets. A simple set will have no side effects
2935 which could change the base value of any other register. */
2937 if (GET_CODE (PATTERN (insn)) == SET
2938 && REG_NOTES (insn) != 0
2939 && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
2940 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
2941 else
2942 note_stores (PATTERN (insn), record_set, NULL);
2944 set = single_set (insn);
2946 if (set != 0
2947 && REG_P (SET_DEST (set))
2948 && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
2950 unsigned int regno = REGNO (SET_DEST (set));
2951 rtx src = SET_SRC (set);
2952 rtx t;
2954 note = find_reg_equal_equiv_note (insn);
2955 if (note && REG_NOTE_KIND (note) == REG_EQUAL
2956 && DF_REG_DEF_COUNT (regno) != 1)
2957 note = NULL_RTX;
2959 if (note != NULL_RTX
2960 && GET_CODE (XEXP (note, 0)) != EXPR_LIST
2961 && ! rtx_varies_p (XEXP (note, 0), 1)
2962 && ! reg_overlap_mentioned_p (SET_DEST (set),
2963 XEXP (note, 0)))
2965 set_reg_known_value (regno, XEXP (note, 0));
2966 set_reg_known_equiv_p (regno,
2967 REG_NOTE_KIND (note) == REG_EQUIV);
2969 else if (DF_REG_DEF_COUNT (regno) == 1
2970 && GET_CODE (src) == PLUS
2971 && REG_P (XEXP (src, 0))
2972 && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
2973 && CONST_INT_P (XEXP (src, 1)))
2975 t = plus_constant (GET_MODE (src), t,
2976 INTVAL (XEXP (src, 1)));
2977 set_reg_known_value (regno, t);
2978 set_reg_known_equiv_p (regno, false);
2980 else if (DF_REG_DEF_COUNT (regno) == 1
2981 && ! rtx_varies_p (src, 1))
2983 set_reg_known_value (regno, src);
2984 set_reg_known_equiv_p (regno, false);
2988 else if (NOTE_P (insn)
2989 && NOTE_KIND (insn) == NOTE_INSN_FUNCTION_BEG)
2990 copying_arguments = false;
2994 /* Now propagate values from new_reg_base_value to reg_base_value. */
2995 gcc_assert (maxreg == (unsigned int) max_reg_num ());
2997 for (ui = 0; ui < maxreg; ui++)
2999 if (new_reg_base_value[ui]
3000 && new_reg_base_value[ui] != (*reg_base_value)[ui]
3001 && ! rtx_equal_p (new_reg_base_value[ui], (*reg_base_value)[ui]))
3003 (*reg_base_value)[ui] = new_reg_base_value[ui];
3004 changed = 1;
3008 while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
3009 XDELETEVEC (rpo);
3011 /* Fill in the remaining entries. */
3012 FOR_EACH_VEC_ELT (*reg_known_value, i, val)
3014 int regno = i + FIRST_PSEUDO_REGISTER;
3015 if (! val)
3016 set_reg_known_value (regno, regno_reg_rtx[regno]);
3019 /* Clean up. */
3020 free (new_reg_base_value);
3021 new_reg_base_value = 0;
3022 sbitmap_free (reg_seen);
3023 reg_seen = 0;
3024 timevar_pop (TV_ALIAS_ANALYSIS);
3027 /* Equate REG_BASE_VALUE (reg1) to REG_BASE_VALUE (reg2).
3028 Special API for var-tracking pass purposes. */
3030 void
3031 vt_equate_reg_base_value (const_rtx reg1, const_rtx reg2)
3033 (*reg_base_value)[REGNO (reg1)] = REG_BASE_VALUE (reg2);
3036 void
3037 end_alias_analysis (void)
3039 old_reg_base_value = reg_base_value;
3040 vec_free (reg_known_value);
3041 sbitmap_free (reg_known_equiv_p);
3044 #include "gt-alias.h"