OSDN Git Service

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