OSDN Git Service

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