OSDN Git Service

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