OSDN Git Service

2004-07-01 Jerry Quinn <jlquinn@optonline.net>
[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) != NULL && TYPE_BINFO_BASETYPES (type) != NULL)
722         {
723           int i;
724           for (i = 0; i < TREE_VEC_LENGTH (TYPE_BINFO_BASETYPES (type)); i++)
725             {
726               tree binfo = TREE_VEC_ELT (TYPE_BINFO_BASETYPES (type), i);
727               record_alias_subset (superset,
728                                    get_alias_set (BINFO_TYPE (binfo)));
729             }
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     case ADDRESSOF:
1260       return (XINT (x, 1) == XINT (y, 1)
1261               && rtx_equal_for_memref_p (XEXP (x, 0),
1262                                          XEXP (y, 0)));
1263
1264     default:
1265       break;
1266     }
1267
1268   /* canon_rtx knows how to handle plus.  No need to canonicalize.  */
1269   if (code == PLUS)
1270     return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1271              && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1272             || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1273                 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1274   /* For commutative operations, the RTX match if the operand match in any
1275      order.  Also handle the simple binary and unary cases without a loop.  */
1276   if (COMMUTATIVE_P (x))
1277     {
1278       rtx xop0 = canon_rtx (XEXP (x, 0));
1279       rtx yop0 = canon_rtx (XEXP (y, 0));
1280       rtx yop1 = canon_rtx (XEXP (y, 1));
1281
1282       return ((rtx_equal_for_memref_p (xop0, yop0)
1283                && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop1))
1284               || (rtx_equal_for_memref_p (xop0, yop1)
1285                   && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)), yop0)));
1286     }
1287   else if (NON_COMMUTATIVE_P (x))
1288     {
1289       return (rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1290                                       canon_rtx (XEXP (y, 0)))
1291               && rtx_equal_for_memref_p (canon_rtx (XEXP (x, 1)),
1292                                          canon_rtx (XEXP (y, 1))));
1293     }
1294   else if (UNARY_P (x))
1295     return rtx_equal_for_memref_p (canon_rtx (XEXP (x, 0)),
1296                                    canon_rtx (XEXP (y, 0)));
1297
1298   /* Compare the elements.  If any pair of corresponding elements
1299      fail to match, return 0 for the whole things.
1300
1301      Limit cases to types which actually appear in addresses.  */
1302
1303   fmt = GET_RTX_FORMAT (code);
1304   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1305     {
1306       switch (fmt[i])
1307         {
1308         case 'i':
1309           if (XINT (x, i) != XINT (y, i))
1310             return 0;
1311           break;
1312
1313         case 'E':
1314           /* Two vectors must have the same length.  */
1315           if (XVECLEN (x, i) != XVECLEN (y, i))
1316             return 0;
1317
1318           /* And the corresponding elements must match.  */
1319           for (j = 0; j < XVECLEN (x, i); j++)
1320             if (rtx_equal_for_memref_p (canon_rtx (XVECEXP (x, i, j)),
1321                                         canon_rtx (XVECEXP (y, i, j))) == 0)
1322               return 0;
1323           break;
1324
1325         case 'e':
1326           if (rtx_equal_for_memref_p (canon_rtx (XEXP (x, i)),
1327                                       canon_rtx (XEXP (y, i))) == 0)
1328             return 0;
1329           break;
1330
1331           /* This can happen for asm operands.  */
1332         case 's':
1333           if (strcmp (XSTR (x, i), XSTR (y, i)))
1334             return 0;
1335           break;
1336
1337         /* This can happen for an asm which clobbers memory.  */
1338         case '0':
1339           break;
1340
1341           /* It is believed that rtx's at this level will never
1342              contain anything but integers and other rtx's,
1343              except for within LABEL_REFs and SYMBOL_REFs.  */
1344         default:
1345           abort ();
1346         }
1347     }
1348   return 1;
1349 }
1350
1351 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
1352    X and return it, or return 0 if none found.  */
1353
1354 static rtx
1355 find_symbolic_term (rtx x)
1356 {
1357   int i;
1358   enum rtx_code code;
1359   const char *fmt;
1360
1361   code = GET_CODE (x);
1362   if (code == SYMBOL_REF || code == LABEL_REF)
1363     return x;
1364   if (OBJECT_P (x))
1365     return 0;
1366
1367   fmt = GET_RTX_FORMAT (code);
1368   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1369     {
1370       rtx t;
1371
1372       if (fmt[i] == 'e')
1373         {
1374           t = find_symbolic_term (XEXP (x, i));
1375           if (t != 0)
1376             return t;
1377         }
1378       else if (fmt[i] == 'E')
1379         break;
1380     }
1381   return 0;
1382 }
1383
1384 rtx
1385 find_base_term (rtx x)
1386 {
1387   cselib_val *val;
1388   struct elt_loc_list *l;
1389
1390 #if defined (FIND_BASE_TERM)
1391   /* Try machine-dependent ways to find the base term.  */
1392   x = FIND_BASE_TERM (x);
1393 #endif
1394
1395   switch (GET_CODE (x))
1396     {
1397     case REG:
1398       return REG_BASE_VALUE (x);
1399
1400     case TRUNCATE:
1401       if (GET_MODE_SIZE (GET_MODE (x)) < GET_MODE_SIZE (Pmode))
1402         return 0;
1403       /* Fall through.  */
1404     case HIGH:
1405     case PRE_INC:
1406     case PRE_DEC:
1407     case POST_INC:
1408     case POST_DEC:
1409     case PRE_MODIFY:
1410     case POST_MODIFY:
1411       return find_base_term (XEXP (x, 0));
1412
1413     case ZERO_EXTEND:
1414     case SIGN_EXTEND:   /* Used for Alpha/NT pointers */
1415       {
1416         rtx temp = find_base_term (XEXP (x, 0));
1417
1418         if (temp != 0 && CONSTANT_P (temp))
1419           temp = convert_memory_address (Pmode, temp);
1420
1421         return temp;
1422       }
1423
1424     case VALUE:
1425       val = CSELIB_VAL_PTR (x);
1426       if (!val)
1427         return 0;
1428       for (l = val->locs; l; l = l->next)
1429         if ((x = find_base_term (l->loc)) != 0)
1430           return x;
1431       return 0;
1432
1433     case CONST:
1434       x = XEXP (x, 0);
1435       if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1436         return 0;
1437       /* Fall through.  */
1438     case LO_SUM:
1439     case PLUS:
1440     case MINUS:
1441       {
1442         rtx tmp1 = XEXP (x, 0);
1443         rtx tmp2 = XEXP (x, 1);
1444
1445         /* This is a little bit tricky since we have to determine which of
1446            the two operands represents the real base address.  Otherwise this
1447            routine may return the index register instead of the base register.
1448
1449            That may cause us to believe no aliasing was possible, when in
1450            fact aliasing is possible.
1451
1452            We use a few simple tests to guess the base register.  Additional
1453            tests can certainly be added.  For example, if one of the operands
1454            is a shift or multiply, then it must be the index register and the
1455            other operand is the base register.  */
1456
1457         if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1458           return find_base_term (tmp2);
1459
1460         /* If either operand is known to be a pointer, then use it
1461            to determine the base term.  */
1462         if (REG_P (tmp1) && REG_POINTER (tmp1))
1463           return find_base_term (tmp1);
1464
1465         if (REG_P (tmp2) && REG_POINTER (tmp2))
1466           return find_base_term (tmp2);
1467
1468         /* Neither operand was known to be a pointer.  Go ahead and find the
1469            base term for both operands.  */
1470         tmp1 = find_base_term (tmp1);
1471         tmp2 = find_base_term (tmp2);
1472
1473         /* If either base term is named object or a special address
1474            (like an argument or stack reference), then use it for the
1475            base term.  */
1476         if (tmp1 != 0
1477             && (GET_CODE (tmp1) == SYMBOL_REF
1478                 || GET_CODE (tmp1) == LABEL_REF
1479                 || (GET_CODE (tmp1) == ADDRESS
1480                     && GET_MODE (tmp1) != VOIDmode)))
1481           return tmp1;
1482
1483         if (tmp2 != 0
1484             && (GET_CODE (tmp2) == SYMBOL_REF
1485                 || GET_CODE (tmp2) == LABEL_REF
1486                 || (GET_CODE (tmp2) == ADDRESS
1487                     && GET_MODE (tmp2) != VOIDmode)))
1488           return tmp2;
1489
1490         /* We could not determine which of the two operands was the
1491            base register and which was the index.  So we can determine
1492            nothing from the base alias check.  */
1493         return 0;
1494       }
1495
1496     case AND:
1497       if (GET_CODE (XEXP (x, 1)) == CONST_INT && INTVAL (XEXP (x, 1)) != 0)
1498         return find_base_term (XEXP (x, 0));
1499       return 0;
1500
1501     case SYMBOL_REF:
1502     case LABEL_REF:
1503       return x;
1504
1505     case ADDRESSOF:
1506       return REG_BASE_VALUE (frame_pointer_rtx);
1507
1508     default:
1509       return 0;
1510     }
1511 }
1512
1513 /* Return 0 if the addresses X and Y are known to point to different
1514    objects, 1 if they might be pointers to the same object.  */
1515
1516 static int
1517 base_alias_check (rtx x, rtx y, enum machine_mode x_mode,
1518                   enum machine_mode y_mode)
1519 {
1520   rtx x_base = find_base_term (x);
1521   rtx y_base = find_base_term (y);
1522
1523   /* If the address itself has no known base see if a known equivalent
1524      value has one.  If either address still has no known base, nothing
1525      is known about aliasing.  */
1526   if (x_base == 0)
1527     {
1528       rtx x_c;
1529
1530       if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1531         return 1;
1532
1533       x_base = find_base_term (x_c);
1534       if (x_base == 0)
1535         return 1;
1536     }
1537
1538   if (y_base == 0)
1539     {
1540       rtx y_c;
1541       if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1542         return 1;
1543
1544       y_base = find_base_term (y_c);
1545       if (y_base == 0)
1546         return 1;
1547     }
1548
1549   /* If the base addresses are equal nothing is known about aliasing.  */
1550   if (rtx_equal_p (x_base, y_base))
1551     return 1;
1552
1553   /* The base addresses of the read and write are different expressions.
1554      If they are both symbols and they are not accessed via AND, there is
1555      no conflict.  We can bring knowledge of object alignment into play
1556      here.  For example, on alpha, "char a, b;" can alias one another,
1557      though "char a; long b;" cannot.  */
1558   if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1559     {
1560       if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1561         return 1;
1562       if (GET_CODE (x) == AND
1563           && (GET_CODE (XEXP (x, 1)) != CONST_INT
1564               || (int) GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1565         return 1;
1566       if (GET_CODE (y) == AND
1567           && (GET_CODE (XEXP (y, 1)) != CONST_INT
1568               || (int) GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1569         return 1;
1570       /* Differing symbols never alias.  */
1571       return 0;
1572     }
1573
1574   /* If one address is a stack reference there can be no alias:
1575      stack references using different base registers do not alias,
1576      a stack reference can not alias a parameter, and a stack reference
1577      can not alias a global.  */
1578   if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
1579       || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
1580     return 0;
1581
1582   if (! flag_argument_noalias)
1583     return 1;
1584
1585   if (flag_argument_noalias > 1)
1586     return 0;
1587
1588   /* Weak noalias assertion (arguments are distinct, but may match globals).  */
1589   return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
1590 }
1591
1592 /* Convert the address X into something we can use.  This is done by returning
1593    it unchanged unless it is a value; in the latter case we call cselib to get
1594    a more useful rtx.  */
1595
1596 rtx
1597 get_addr (rtx x)
1598 {
1599   cselib_val *v;
1600   struct elt_loc_list *l;
1601
1602   if (GET_CODE (x) != VALUE)
1603     return x;
1604   v = CSELIB_VAL_PTR (x);
1605   if (v)
1606     {
1607       for (l = v->locs; l; l = l->next)
1608         if (CONSTANT_P (l->loc))
1609           return l->loc;
1610       for (l = v->locs; l; l = l->next)
1611         if (!REG_P (l->loc) && !MEM_P (l->loc))
1612           return l->loc;
1613       if (v->locs)
1614         return v->locs->loc;
1615     }
1616   return x;
1617 }
1618
1619 /*  Return the address of the (N_REFS + 1)th memory reference to ADDR
1620     where SIZE is the size in bytes of the memory reference.  If ADDR
1621     is not modified by the memory reference then ADDR is returned.  */
1622
1623 rtx
1624 addr_side_effect_eval (rtx addr, int size, int n_refs)
1625 {
1626   int offset = 0;
1627
1628   switch (GET_CODE (addr))
1629     {
1630     case PRE_INC:
1631       offset = (n_refs + 1) * size;
1632       break;
1633     case PRE_DEC:
1634       offset = -(n_refs + 1) * size;
1635       break;
1636     case POST_INC:
1637       offset = n_refs * size;
1638       break;
1639     case POST_DEC:
1640       offset = -n_refs * size;
1641       break;
1642
1643     default:
1644       return addr;
1645     }
1646
1647   if (offset)
1648     addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0),
1649                          GEN_INT (offset));
1650   else
1651     addr = XEXP (addr, 0);
1652   addr = canon_rtx (addr);
1653
1654   return addr;
1655 }
1656
1657 /* Return nonzero if X and Y (memory addresses) could reference the
1658    same location in memory.  C is an offset accumulator.  When
1659    C is nonzero, we are testing aliases between X and Y + C.
1660    XSIZE is the size in bytes of the X reference,
1661    similarly YSIZE is the size in bytes for Y.
1662    Expect that canon_rtx has been already called for X and Y.
1663
1664    If XSIZE or YSIZE is zero, we do not know the amount of memory being
1665    referenced (the reference was BLKmode), so make the most pessimistic
1666    assumptions.
1667
1668    If XSIZE or YSIZE is negative, we may access memory outside the object
1669    being referenced as a side effect.  This can happen when using AND to
1670    align memory references, as is done on the Alpha.
1671
1672    Nice to notice that varying addresses cannot conflict with fp if no
1673    local variables had their addresses taken, but that's too hard now.  */
1674
1675 static int
1676 memrefs_conflict_p (int xsize, rtx x, int ysize, rtx y, HOST_WIDE_INT c)
1677 {
1678   if (GET_CODE (x) == VALUE)
1679     x = get_addr (x);
1680   if (GET_CODE (y) == VALUE)
1681     y = get_addr (y);
1682   if (GET_CODE (x) == HIGH)
1683     x = XEXP (x, 0);
1684   else if (GET_CODE (x) == LO_SUM)
1685     x = XEXP (x, 1);
1686   else
1687     x = addr_side_effect_eval (x, xsize, 0);
1688   if (GET_CODE (y) == HIGH)
1689     y = XEXP (y, 0);
1690   else if (GET_CODE (y) == LO_SUM)
1691     y = XEXP (y, 1);
1692   else
1693     y = addr_side_effect_eval (y, ysize, 0);
1694
1695   if (rtx_equal_for_memref_p (x, y))
1696     {
1697       if (xsize <= 0 || ysize <= 0)
1698         return 1;
1699       if (c >= 0 && xsize > c)
1700         return 1;
1701       if (c < 0 && ysize+c > 0)
1702         return 1;
1703       return 0;
1704     }
1705
1706   /* This code used to check for conflicts involving stack references and
1707      globals but the base address alias code now handles these cases.  */
1708
1709   if (GET_CODE (x) == PLUS)
1710     {
1711       /* The fact that X is canonicalized means that this
1712          PLUS rtx is canonicalized.  */
1713       rtx x0 = XEXP (x, 0);
1714       rtx x1 = XEXP (x, 1);
1715
1716       if (GET_CODE (y) == PLUS)
1717         {
1718           /* The fact that Y is canonicalized means that this
1719              PLUS rtx is canonicalized.  */
1720           rtx y0 = XEXP (y, 0);
1721           rtx y1 = XEXP (y, 1);
1722
1723           if (rtx_equal_for_memref_p (x1, y1))
1724             return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1725           if (rtx_equal_for_memref_p (x0, y0))
1726             return memrefs_conflict_p (xsize, x1, ysize, y1, c);
1727           if (GET_CODE (x1) == CONST_INT)
1728             {
1729               if (GET_CODE (y1) == CONST_INT)
1730                 return memrefs_conflict_p (xsize, x0, ysize, y0,
1731                                            c - INTVAL (x1) + INTVAL (y1));
1732               else
1733                 return memrefs_conflict_p (xsize, x0, ysize, y,
1734                                            c - INTVAL (x1));
1735             }
1736           else if (GET_CODE (y1) == CONST_INT)
1737             return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1738
1739           return 1;
1740         }
1741       else if (GET_CODE (x1) == CONST_INT)
1742         return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
1743     }
1744   else if (GET_CODE (y) == PLUS)
1745     {
1746       /* The fact that Y is canonicalized means that this
1747          PLUS rtx is canonicalized.  */
1748       rtx y0 = XEXP (y, 0);
1749       rtx y1 = XEXP (y, 1);
1750
1751       if (GET_CODE (y1) == CONST_INT)
1752         return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1753       else
1754         return 1;
1755     }
1756
1757   if (GET_CODE (x) == GET_CODE (y))
1758     switch (GET_CODE (x))
1759       {
1760       case MULT:
1761         {
1762           /* Handle cases where we expect the second operands to be the
1763              same, and check only whether the first operand would conflict
1764              or not.  */
1765           rtx x0, y0;
1766           rtx x1 = canon_rtx (XEXP (x, 1));
1767           rtx y1 = canon_rtx (XEXP (y, 1));
1768           if (! rtx_equal_for_memref_p (x1, y1))
1769             return 1;
1770           x0 = canon_rtx (XEXP (x, 0));
1771           y0 = canon_rtx (XEXP (y, 0));
1772           if (rtx_equal_for_memref_p (x0, y0))
1773             return (xsize == 0 || ysize == 0
1774                     || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1775
1776           /* Can't properly adjust our sizes.  */
1777           if (GET_CODE (x1) != CONST_INT)
1778             return 1;
1779           xsize /= INTVAL (x1);
1780           ysize /= INTVAL (x1);
1781           c /= INTVAL (x1);
1782           return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1783         }
1784
1785       case REG:
1786         /* Are these registers known not to be equal?  */
1787         if (alias_invariant)
1788           {
1789             unsigned int r_x = REGNO (x), r_y = REGNO (y);
1790             rtx i_x, i_y;       /* invariant relationships of X and Y */
1791
1792             i_x = r_x >= alias_invariant_size ? 0 : alias_invariant[r_x];
1793             i_y = r_y >= alias_invariant_size ? 0 : alias_invariant[r_y];
1794
1795             if (i_x == 0 && i_y == 0)
1796               break;
1797
1798             if (! memrefs_conflict_p (xsize, i_x ? i_x : x,
1799                                       ysize, i_y ? i_y : y, c))
1800               return 0;
1801           }
1802         break;
1803
1804       default:
1805         break;
1806       }
1807
1808   /* Treat an access through an AND (e.g. a subword access on an Alpha)
1809      as an access with indeterminate size.  Assume that references
1810      besides AND are aligned, so if the size of the other reference is
1811      at least as large as the alignment, assume no other overlap.  */
1812   if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
1813     {
1814       if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
1815         xsize = -1;
1816       return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)), ysize, y, c);
1817     }
1818   if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
1819     {
1820       /* ??? If we are indexing far enough into the array/structure, we
1821          may yet be able to determine that we can not overlap.  But we
1822          also need to that we are far enough from the end not to overlap
1823          a following reference, so we do nothing with that for now.  */
1824       if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
1825         ysize = -1;
1826       return memrefs_conflict_p (xsize, x, ysize, canon_rtx (XEXP (y, 0)), c);
1827     }
1828
1829   if (GET_CODE (x) == ADDRESSOF)
1830     {
1831       if (y == frame_pointer_rtx
1832           || GET_CODE (y) == ADDRESSOF)
1833         return xsize <= 0 || ysize <= 0;
1834     }
1835   if (GET_CODE (y) == ADDRESSOF)
1836     {
1837       if (x == frame_pointer_rtx)
1838         return xsize <= 0 || ysize <= 0;
1839     }
1840
1841   if (CONSTANT_P (x))
1842     {
1843       if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
1844         {
1845           c += (INTVAL (y) - INTVAL (x));
1846           return (xsize <= 0 || ysize <= 0
1847                   || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1848         }
1849
1850       if (GET_CODE (x) == CONST)
1851         {
1852           if (GET_CODE (y) == CONST)
1853             return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1854                                        ysize, canon_rtx (XEXP (y, 0)), c);
1855           else
1856             return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1857                                        ysize, y, c);
1858         }
1859       if (GET_CODE (y) == CONST)
1860         return memrefs_conflict_p (xsize, x, ysize,
1861                                    canon_rtx (XEXP (y, 0)), c);
1862
1863       if (CONSTANT_P (y))
1864         return (xsize <= 0 || ysize <= 0
1865                 || (rtx_equal_for_memref_p (x, y)
1866                     && ((c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
1867
1868       return 1;
1869     }
1870   return 1;
1871 }
1872
1873 /* Functions to compute memory dependencies.
1874
1875    Since we process the insns in execution order, we can build tables
1876    to keep track of what registers are fixed (and not aliased), what registers
1877    are varying in known ways, and what registers are varying in unknown
1878    ways.
1879
1880    If both memory references are volatile, then there must always be a
1881    dependence between the two references, since their order can not be
1882    changed.  A volatile and non-volatile reference can be interchanged
1883    though.
1884
1885    A MEM_IN_STRUCT reference at a non-AND varying address can never
1886    conflict with a non-MEM_IN_STRUCT reference at a fixed address.  We
1887    also must allow AND addresses, because they may generate accesses
1888    outside the object being referenced.  This is used to generate
1889    aligned addresses from unaligned addresses, for instance, the alpha
1890    storeqi_unaligned pattern.  */
1891
1892 /* Read dependence: X is read after read in MEM takes place.  There can
1893    only be a dependence here if both reads are volatile.  */
1894
1895 int
1896 read_dependence (rtx mem, rtx x)
1897 {
1898   return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
1899 }
1900
1901 /* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
1902    MEM2 is a reference to a structure at a varying address, or returns
1903    MEM2 if vice versa.  Otherwise, returns NULL_RTX.  If a non-NULL
1904    value is returned MEM1 and MEM2 can never alias.  VARIES_P is used
1905    to decide whether or not an address may vary; it should return
1906    nonzero whenever variation is possible.
1907    MEM1_ADDR and MEM2_ADDR are the addresses of MEM1 and MEM2.  */
1908
1909 static rtx
1910 fixed_scalar_and_varying_struct_p (rtx mem1, rtx mem2, rtx mem1_addr,
1911                                    rtx mem2_addr,
1912                                    int (*varies_p) (rtx, int))
1913 {
1914   if (! flag_strict_aliasing)
1915     return NULL_RTX;
1916
1917   if (MEM_SCALAR_P (mem1) && MEM_IN_STRUCT_P (mem2)
1918       && !varies_p (mem1_addr, 1) && varies_p (mem2_addr, 1))
1919     /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
1920        varying address.  */
1921     return mem1;
1922
1923   if (MEM_IN_STRUCT_P (mem1) && MEM_SCALAR_P (mem2)
1924       && varies_p (mem1_addr, 1) && !varies_p (mem2_addr, 1))
1925     /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
1926        varying address.  */
1927     return mem2;
1928
1929   return NULL_RTX;
1930 }
1931
1932 /* Returns nonzero if something about the mode or address format MEM1
1933    indicates that it might well alias *anything*.  */
1934
1935 static int
1936 aliases_everything_p (rtx mem)
1937 {
1938   if (GET_CODE (XEXP (mem, 0)) == AND)
1939     /* If the address is an AND, its very hard to know at what it is
1940        actually pointing.  */
1941     return 1;
1942
1943   return 0;
1944 }
1945
1946 /* Return true if we can determine that the fields referenced cannot
1947    overlap for any pair of objects.  */
1948
1949 static bool
1950 nonoverlapping_component_refs_p (tree x, tree y)
1951 {
1952   tree fieldx, fieldy, typex, typey, orig_y;
1953
1954   do
1955     {
1956       /* The comparison has to be done at a common type, since we don't
1957          know how the inheritance hierarchy works.  */
1958       orig_y = y;
1959       do
1960         {
1961           fieldx = TREE_OPERAND (x, 1);
1962           typex = DECL_FIELD_CONTEXT (fieldx);
1963
1964           y = orig_y;
1965           do
1966             {
1967               fieldy = TREE_OPERAND (y, 1);
1968               typey = DECL_FIELD_CONTEXT (fieldy);
1969
1970               if (typex == typey)
1971                 goto found;
1972
1973               y = TREE_OPERAND (y, 0);
1974             }
1975           while (y && TREE_CODE (y) == COMPONENT_REF);
1976
1977           x = TREE_OPERAND (x, 0);
1978         }
1979       while (x && TREE_CODE (x) == COMPONENT_REF);
1980
1981       /* Never found a common type.  */
1982       return false;
1983
1984     found:
1985       /* If we're left with accessing different fields of a structure,
1986          then no overlap.  */
1987       if (TREE_CODE (typex) == RECORD_TYPE
1988           && fieldx != fieldy)
1989         return true;
1990
1991       /* The comparison on the current field failed.  If we're accessing
1992          a very nested structure, look at the next outer level.  */
1993       x = TREE_OPERAND (x, 0);
1994       y = TREE_OPERAND (y, 0);
1995     }
1996   while (x && y
1997          && TREE_CODE (x) == COMPONENT_REF
1998          && TREE_CODE (y) == COMPONENT_REF);
1999
2000   return false;
2001 }
2002
2003 /* Look at the bottom of the COMPONENT_REF list for a DECL, and return it.  */
2004
2005 static tree
2006 decl_for_component_ref (tree x)
2007 {
2008   do
2009     {
2010       x = TREE_OPERAND (x, 0);
2011     }
2012   while (x && TREE_CODE (x) == COMPONENT_REF);
2013
2014   return x && DECL_P (x) ? x : NULL_TREE;
2015 }
2016
2017 /* Walk up the COMPONENT_REF list and adjust OFFSET to compensate for the
2018    offset of the field reference.  */
2019
2020 static rtx
2021 adjust_offset_for_component_ref (tree x, rtx offset)
2022 {
2023   HOST_WIDE_INT ioffset;
2024
2025   if (! offset)
2026     return NULL_RTX;
2027
2028   ioffset = INTVAL (offset);
2029   do
2030     {
2031       tree offset = component_ref_field_offset (x);
2032       tree field = TREE_OPERAND (x, 1);
2033
2034       if (! host_integerp (offset, 1))
2035         return NULL_RTX;
2036       ioffset += (tree_low_cst (offset, 1)
2037                   + (tree_low_cst (DECL_FIELD_BIT_OFFSET (field), 1)
2038                      / BITS_PER_UNIT));
2039
2040       x = TREE_OPERAND (x, 0);
2041     }
2042   while (x && TREE_CODE (x) == COMPONENT_REF);
2043
2044   return GEN_INT (ioffset);
2045 }
2046
2047 /* Return nonzero if we can determine the exprs corresponding to memrefs
2048    X and Y and they do not overlap.  */
2049
2050 static int
2051 nonoverlapping_memrefs_p (rtx x, rtx y)
2052 {
2053   tree exprx = MEM_EXPR (x), expry = MEM_EXPR (y);
2054   rtx rtlx, rtly;
2055   rtx basex, basey;
2056   rtx moffsetx, moffsety;
2057   HOST_WIDE_INT offsetx = 0, offsety = 0, sizex, sizey, tem;
2058
2059   /* Unless both have exprs, we can't tell anything.  */
2060   if (exprx == 0 || expry == 0)
2061     return 0;
2062
2063   /* If both are field references, we may be able to determine something.  */
2064   if (TREE_CODE (exprx) == COMPONENT_REF
2065       && TREE_CODE (expry) == COMPONENT_REF
2066       && nonoverlapping_component_refs_p (exprx, expry))
2067     return 1;
2068
2069   /* If the field reference test failed, look at the DECLs involved.  */
2070   moffsetx = MEM_OFFSET (x);
2071   if (TREE_CODE (exprx) == COMPONENT_REF)
2072     {
2073       tree t = decl_for_component_ref (exprx);
2074       if (! t)
2075         return 0;
2076       moffsetx = adjust_offset_for_component_ref (exprx, moffsetx);
2077       exprx = t;
2078     }
2079   else if (TREE_CODE (exprx) == INDIRECT_REF)
2080     {
2081       exprx = TREE_OPERAND (exprx, 0);
2082       if (flag_argument_noalias < 2
2083           || TREE_CODE (exprx) != PARM_DECL)
2084         return 0;
2085     }
2086
2087   moffsety = MEM_OFFSET (y);
2088   if (TREE_CODE (expry) == COMPONENT_REF)
2089     {
2090       tree t = decl_for_component_ref (expry);
2091       if (! t)
2092         return 0;
2093       moffsety = adjust_offset_for_component_ref (expry, moffsety);
2094       expry = t;
2095     }
2096   else if (TREE_CODE (expry) == INDIRECT_REF)
2097     {
2098       expry = TREE_OPERAND (expry, 0);
2099       if (flag_argument_noalias < 2
2100           || TREE_CODE (expry) != PARM_DECL)
2101         return 0;
2102     }
2103
2104   if (! DECL_P (exprx) || ! DECL_P (expry))
2105     return 0;
2106
2107   rtlx = DECL_RTL (exprx);
2108   rtly = DECL_RTL (expry);
2109
2110   /* If either RTL is not a MEM, it must be a REG or CONCAT, meaning they
2111      can't overlap unless they are the same because we never reuse that part
2112      of the stack frame used for locals for spilled pseudos.  */
2113   if ((!MEM_P (rtlx) || !MEM_P (rtly))
2114       && ! rtx_equal_p (rtlx, rtly))
2115     return 1;
2116
2117   /* Get the base and offsets of both decls.  If either is a register, we
2118      know both are and are the same, so use that as the base.  The only
2119      we can avoid overlap is if we can deduce that they are nonoverlapping
2120      pieces of that decl, which is very rare.  */
2121   basex = MEM_P (rtlx) ? XEXP (rtlx, 0) : rtlx;
2122   if (GET_CODE (basex) == PLUS && GET_CODE (XEXP (basex, 1)) == CONST_INT)
2123     offsetx = INTVAL (XEXP (basex, 1)), basex = XEXP (basex, 0);
2124
2125   basey = MEM_P (rtly) ? XEXP (rtly, 0) : rtly;
2126   if (GET_CODE (basey) == PLUS && GET_CODE (XEXP (basey, 1)) == CONST_INT)
2127     offsety = INTVAL (XEXP (basey, 1)), basey = XEXP (basey, 0);
2128
2129   /* If the bases are different, we know they do not overlap if both
2130      are constants or if one is a constant and the other a pointer into the
2131      stack frame.  Otherwise a different base means we can't tell if they
2132      overlap or not.  */
2133   if (! rtx_equal_p (basex, basey))
2134     return ((CONSTANT_P (basex) && CONSTANT_P (basey))
2135             || (CONSTANT_P (basex) && REG_P (basey)
2136                 && REGNO_PTR_FRAME_P (REGNO (basey)))
2137             || (CONSTANT_P (basey) && REG_P (basex)
2138                 && REGNO_PTR_FRAME_P (REGNO (basex))));
2139
2140   sizex = (!MEM_P (rtlx) ? (int) GET_MODE_SIZE (GET_MODE (rtlx))
2141            : MEM_SIZE (rtlx) ? INTVAL (MEM_SIZE (rtlx))
2142            : -1);
2143   sizey = (!MEM_P (rtly) ? (int) GET_MODE_SIZE (GET_MODE (rtly))
2144            : MEM_SIZE (rtly) ? INTVAL (MEM_SIZE (rtly)) :
2145            -1);
2146
2147   /* If we have an offset for either memref, it can update the values computed
2148      above.  */
2149   if (moffsetx)
2150     offsetx += INTVAL (moffsetx), sizex -= INTVAL (moffsetx);
2151   if (moffsety)
2152     offsety += INTVAL (moffsety), sizey -= INTVAL (moffsety);
2153
2154   /* If a memref has both a size and an offset, we can use the smaller size.
2155      We can't do this if the offset isn't known because we must view this
2156      memref as being anywhere inside the DECL's MEM.  */
2157   if (MEM_SIZE (x) && moffsetx)
2158     sizex = INTVAL (MEM_SIZE (x));
2159   if (MEM_SIZE (y) && moffsety)
2160     sizey = INTVAL (MEM_SIZE (y));
2161
2162   /* Put the values of the memref with the lower offset in X's values.  */
2163   if (offsetx > offsety)
2164     {
2165       tem = offsetx, offsetx = offsety, offsety = tem;
2166       tem = sizex, sizex = sizey, sizey = tem;
2167     }
2168
2169   /* If we don't know the size of the lower-offset value, we can't tell
2170      if they conflict.  Otherwise, we do the test.  */
2171   return sizex >= 0 && offsety >= offsetx + sizex;
2172 }
2173
2174 /* True dependence: X is read after store in MEM takes place.  */
2175
2176 int
2177 true_dependence (rtx mem, enum machine_mode mem_mode, rtx x,
2178                  int (*varies) (rtx, int))
2179 {
2180   rtx x_addr, mem_addr;
2181   rtx base;
2182
2183   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2184     return 1;
2185
2186   /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2187      This is used in epilogue deallocation functions.  */
2188   if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2189     return 1;
2190   if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2191     return 1;
2192
2193   if (DIFFERENT_ALIAS_SETS_P (x, mem))
2194     return 0;
2195
2196   /* Unchanging memory can't conflict with non-unchanging memory.
2197      A non-unchanging read can conflict with a non-unchanging write.
2198      An unchanging read can conflict with an unchanging write since
2199      there may be a single store to this address to initialize it.
2200      Note that an unchanging store can conflict with a non-unchanging read
2201      since we have to make conservative assumptions when we have a
2202      record with readonly fields and we are copying the whole thing.
2203      Just fall through to the code below to resolve potential conflicts.
2204      This won't handle all cases optimally, but the possible performance
2205      loss should be negligible.  */
2206   if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
2207     return 0;
2208
2209   if (nonoverlapping_memrefs_p (mem, x))
2210     return 0;
2211
2212   if (mem_mode == VOIDmode)
2213     mem_mode = GET_MODE (mem);
2214
2215   x_addr = get_addr (XEXP (x, 0));
2216   mem_addr = get_addr (XEXP (mem, 0));
2217
2218   base = find_base_term (x_addr);
2219   if (base && (GET_CODE (base) == LABEL_REF
2220                || (GET_CODE (base) == SYMBOL_REF
2221                    && CONSTANT_POOL_ADDRESS_P (base))))
2222     return 0;
2223
2224   if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
2225     return 0;
2226
2227   x_addr = canon_rtx (x_addr);
2228   mem_addr = canon_rtx (mem_addr);
2229
2230   if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2231                             SIZE_FOR_MODE (x), x_addr, 0))
2232     return 0;
2233
2234   if (aliases_everything_p (x))
2235     return 1;
2236
2237   /* We cannot use aliases_everything_p to test MEM, since we must look
2238      at MEM_MODE, rather than GET_MODE (MEM).  */
2239   if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
2240     return 1;
2241
2242   /* In true_dependence we also allow BLKmode to alias anything.  Why
2243      don't we do this in anti_dependence and output_dependence?  */
2244   if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
2245     return 1;
2246
2247   return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2248                                               varies);
2249 }
2250
2251 /* Canonical true dependence: X is read after store in MEM takes place.
2252    Variant of true_dependence which assumes MEM has already been
2253    canonicalized (hence we no longer do that here).
2254    The mem_addr argument has been added, since true_dependence computed
2255    this value prior to canonicalizing.  */
2256
2257 int
2258 canon_true_dependence (rtx mem, enum machine_mode mem_mode, rtx mem_addr,
2259                        rtx x, int (*varies) (rtx, int))
2260 {
2261   rtx x_addr;
2262
2263   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2264     return 1;
2265
2266   /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2267      This is used in epilogue deallocation functions.  */
2268   if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2269     return 1;
2270   if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2271     return 1;
2272
2273   if (DIFFERENT_ALIAS_SETS_P (x, mem))
2274     return 0;
2275
2276   /* If X is an unchanging read, then it can't possibly conflict with any
2277      non-unchanging store.  It may conflict with an unchanging write though,
2278      because there may be a single store to this address to initialize it.
2279      Just fall through to the code below to resolve the case where we have
2280      both an unchanging read and an unchanging write.  This won't handle all
2281      cases optimally, but the possible performance loss should be
2282      negligible.  */
2283   if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
2284     return 0;
2285
2286   if (nonoverlapping_memrefs_p (x, mem))
2287     return 0;
2288
2289   x_addr = get_addr (XEXP (x, 0));
2290
2291   if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
2292     return 0;
2293
2294   x_addr = canon_rtx (x_addr);
2295   if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
2296                             SIZE_FOR_MODE (x), x_addr, 0))
2297     return 0;
2298
2299   if (aliases_everything_p (x))
2300     return 1;
2301
2302   /* We cannot use aliases_everything_p to test MEM, since we must look
2303      at MEM_MODE, rather than GET_MODE (MEM).  */
2304   if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
2305     return 1;
2306
2307   /* In true_dependence we also allow BLKmode to alias anything.  Why
2308      don't we do this in anti_dependence and output_dependence?  */
2309   if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
2310     return 1;
2311
2312   return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2313                                               varies);
2314 }
2315
2316 /* Returns nonzero if a write to X might alias a previous read from
2317    (or, if WRITEP is nonzero, a write to) MEM.  If CONSTP is nonzero,
2318    honor the RTX_UNCHANGING_P flags on X and MEM.  */
2319
2320 static int
2321 write_dependence_p (rtx mem, rtx x, int writep, int constp)
2322 {
2323   rtx x_addr, mem_addr;
2324   rtx fixed_scalar;
2325   rtx base;
2326
2327   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
2328     return 1;
2329
2330   /* (mem:BLK (scratch)) is a special mechanism to conflict with everything.
2331      This is used in epilogue deallocation functions.  */
2332   if (GET_MODE (x) == BLKmode && GET_CODE (XEXP (x, 0)) == SCRATCH)
2333     return 1;
2334   if (GET_MODE (mem) == BLKmode && GET_CODE (XEXP (mem, 0)) == SCRATCH)
2335     return 1;
2336
2337   if (DIFFERENT_ALIAS_SETS_P (x, mem))
2338     return 0;
2339
2340   if (constp)
2341     {
2342       /* Unchanging memory can't conflict with non-unchanging memory.  */
2343       if (RTX_UNCHANGING_P (x) != RTX_UNCHANGING_P (mem))
2344         return 0;
2345
2346       /* If MEM is an unchanging read, then it can't possibly conflict with
2347          the store to X, because there is at most one store to MEM, and it
2348          must have occurred somewhere before MEM.  */
2349       if (! writep && RTX_UNCHANGING_P (mem))
2350         return 0;
2351     }
2352
2353   if (nonoverlapping_memrefs_p (x, mem))
2354     return 0;
2355
2356   x_addr = get_addr (XEXP (x, 0));
2357   mem_addr = get_addr (XEXP (mem, 0));
2358
2359   if (! writep)
2360     {
2361       base = find_base_term (mem_addr);
2362       if (base && (GET_CODE (base) == LABEL_REF
2363                    || (GET_CODE (base) == SYMBOL_REF
2364                        && CONSTANT_POOL_ADDRESS_P (base))))
2365         return 0;
2366     }
2367
2368   if (! base_alias_check (x_addr, mem_addr, GET_MODE (x),
2369                           GET_MODE (mem)))
2370     return 0;
2371
2372   x_addr = canon_rtx (x_addr);
2373   mem_addr = canon_rtx (mem_addr);
2374
2375   if (!memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
2376                            SIZE_FOR_MODE (x), x_addr, 0))
2377     return 0;
2378
2379   fixed_scalar
2380     = fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
2381                                          rtx_addr_varies_p);
2382
2383   return (!(fixed_scalar == mem && !aliases_everything_p (x))
2384           && !(fixed_scalar == x && !aliases_everything_p (mem)));
2385 }
2386
2387 /* Anti dependence: X is written after read in MEM takes place.  */
2388
2389 int
2390 anti_dependence (rtx mem, rtx x)
2391 {
2392   return write_dependence_p (mem, x, /*writep=*/0, /*constp*/1);
2393 }
2394
2395 /* Output dependence: X is written after store in MEM takes place.  */
2396
2397 int
2398 output_dependence (rtx mem, rtx x)
2399 {
2400   return write_dependence_p (mem, x, /*writep=*/1, /*constp*/1);
2401 }
2402
2403 /* Unchanging anti dependence: Like anti_dependence but ignores
2404    the UNCHANGING_RTX_P property on const variable references.  */
2405
2406 int
2407 unchanging_anti_dependence (rtx mem, rtx x)
2408 {
2409   return write_dependence_p (mem, x, /*writep=*/0, /*constp*/0);
2410 }
2411 \f
2412 /* A subroutine of nonlocal_mentioned_p, returns 1 if *LOC mentions
2413    something which is not local to the function and is not constant.  */
2414
2415 static int
2416 nonlocal_mentioned_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
2417 {
2418   rtx x = *loc;
2419   rtx base;
2420   int regno;
2421
2422   if (! x)
2423     return 0;
2424
2425   switch (GET_CODE (x))
2426     {
2427     case SUBREG:
2428       if (REG_P (SUBREG_REG (x)))
2429         {
2430           /* Global registers are not local.  */
2431           if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
2432               && global_regs[subreg_regno (x)])
2433             return 1;
2434           return 0;
2435         }
2436       break;
2437
2438     case REG:
2439       regno = REGNO (x);
2440       /* Global registers are not local.  */
2441       if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
2442         return 1;
2443       return 0;
2444
2445     case SCRATCH:
2446     case PC:
2447     case CC0:
2448     case CONST_INT:
2449     case CONST_DOUBLE:
2450     case CONST_VECTOR:
2451     case CONST:
2452     case LABEL_REF:
2453       return 0;
2454
2455     case SYMBOL_REF:
2456       /* Constants in the function's constants pool are constant.  */
2457       if (CONSTANT_POOL_ADDRESS_P (x))
2458         return 0;
2459       return 1;
2460
2461     case CALL:
2462       /* Non-constant calls and recursion are not local.  */
2463       return 1;
2464
2465     case MEM:
2466       /* Be overly conservative and consider any volatile memory
2467          reference as not local.  */
2468       if (MEM_VOLATILE_P (x))
2469         return 1;
2470       base = find_base_term (XEXP (x, 0));
2471       if (base)
2472         {
2473           /* A Pmode ADDRESS could be a reference via the structure value
2474              address or static chain.  Such memory references are nonlocal.
2475
2476              Thus, we have to examine the contents of the ADDRESS to find
2477              out if this is a local reference or not.  */
2478           if (GET_CODE (base) == ADDRESS
2479               && GET_MODE (base) == Pmode
2480               && (XEXP (base, 0) == stack_pointer_rtx
2481                   || XEXP (base, 0) == arg_pointer_rtx
2482 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2483                   || XEXP (base, 0) == hard_frame_pointer_rtx
2484 #endif
2485                   || XEXP (base, 0) == frame_pointer_rtx))
2486             return 0;
2487           /* Constants in the function's constant pool are constant.  */
2488           if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
2489             return 0;
2490         }
2491       return 1;
2492
2493     case UNSPEC_VOLATILE:
2494     case ASM_INPUT:
2495       return 1;
2496
2497     case ASM_OPERANDS:
2498       if (MEM_VOLATILE_P (x))
2499         return 1;
2500
2501     /* Fall through.  */
2502
2503     default:
2504       break;
2505     }
2506
2507   return 0;
2508 }
2509
2510 /* Returns nonzero if X might mention something which is not
2511    local to the function and is not constant.  */
2512
2513 static int
2514 nonlocal_mentioned_p (rtx x)
2515 {
2516   if (INSN_P (x))
2517     {
2518       if (GET_CODE (x) == CALL_INSN)
2519         {
2520           if (! CONST_OR_PURE_CALL_P (x))
2521             return 1;
2522           x = CALL_INSN_FUNCTION_USAGE (x);
2523           if (x == 0)
2524             return 0;
2525         }
2526       else
2527         x = PATTERN (x);
2528     }
2529
2530   return for_each_rtx (&x, nonlocal_mentioned_p_1, NULL);
2531 }
2532
2533 /* A subroutine of nonlocal_referenced_p, returns 1 if *LOC references
2534    something which is not local to the function and is not constant.  */
2535
2536 static int
2537 nonlocal_referenced_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
2538 {
2539   rtx x = *loc;
2540
2541   if (! x)
2542     return 0;
2543
2544   switch (GET_CODE (x))
2545     {
2546     case MEM:
2547     case REG:
2548     case SYMBOL_REF:
2549     case SUBREG:
2550       return nonlocal_mentioned_p (x);
2551
2552     case CALL:
2553       /* Non-constant calls and recursion are not local.  */
2554       return 1;
2555
2556     case SET:
2557       if (nonlocal_mentioned_p (SET_SRC (x)))
2558         return 1;
2559
2560       if (MEM_P (SET_DEST (x)))
2561         return nonlocal_mentioned_p (XEXP (SET_DEST (x), 0));
2562
2563       /* If the destination is anything other than a CC0, PC,
2564          MEM, REG, or a SUBREG of a REG that occupies all of
2565          the REG, then X references nonlocal memory if it is
2566          mentioned in the destination.  */
2567       if (GET_CODE (SET_DEST (x)) != CC0
2568           && GET_CODE (SET_DEST (x)) != PC
2569           && !REG_P (SET_DEST (x))
2570           && ! (GET_CODE (SET_DEST (x)) == SUBREG
2571                 && REG_P (SUBREG_REG (SET_DEST (x)))
2572                 && (((GET_MODE_SIZE (GET_MODE (SUBREG_REG (SET_DEST (x))))
2573                       + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD)
2574                     == ((GET_MODE_SIZE (GET_MODE (SET_DEST (x)))
2575                          + (UNITS_PER_WORD - 1)) / UNITS_PER_WORD))))
2576         return nonlocal_mentioned_p (SET_DEST (x));
2577       return 0;
2578
2579     case CLOBBER:
2580       if (MEM_P (XEXP (x, 0)))
2581         return nonlocal_mentioned_p (XEXP (XEXP (x, 0), 0));
2582       return 0;
2583
2584     case USE:
2585       return nonlocal_mentioned_p (XEXP (x, 0));
2586
2587     case ASM_INPUT:
2588     case UNSPEC_VOLATILE:
2589       return 1;
2590
2591     case ASM_OPERANDS:
2592       if (MEM_VOLATILE_P (x))
2593         return 1;
2594
2595     /* Fall through.  */
2596
2597     default:
2598       break;
2599     }
2600
2601   return 0;
2602 }
2603
2604 /* Returns nonzero if X might reference something which is not
2605    local to the function and is not constant.  */
2606
2607 static int
2608 nonlocal_referenced_p (rtx x)
2609 {
2610   if (INSN_P (x))
2611     {
2612       if (GET_CODE (x) == CALL_INSN)
2613         {
2614           if (! CONST_OR_PURE_CALL_P (x))
2615             return 1;
2616           x = CALL_INSN_FUNCTION_USAGE (x);
2617           if (x == 0)
2618             return 0;
2619         }
2620       else
2621         x = PATTERN (x);
2622     }
2623
2624   return for_each_rtx (&x, nonlocal_referenced_p_1, NULL);
2625 }
2626
2627 /* A subroutine of nonlocal_set_p, returns 1 if *LOC sets
2628    something which is not local to the function and is not constant.  */
2629
2630 static int
2631 nonlocal_set_p_1 (rtx *loc, void *data ATTRIBUTE_UNUSED)
2632 {
2633   rtx x = *loc;
2634
2635   if (! x)
2636     return 0;
2637
2638   switch (GET_CODE (x))
2639     {
2640     case CALL:
2641       /* Non-constant calls and recursion are not local.  */
2642       return 1;
2643
2644     case PRE_INC:
2645     case PRE_DEC:
2646     case POST_INC:
2647     case POST_DEC:
2648     case PRE_MODIFY:
2649     case POST_MODIFY:
2650       return nonlocal_mentioned_p (XEXP (x, 0));
2651
2652     case SET:
2653       if (nonlocal_mentioned_p (SET_DEST (x)))
2654         return 1;
2655       return nonlocal_set_p (SET_SRC (x));
2656
2657     case CLOBBER:
2658       return nonlocal_mentioned_p (XEXP (x, 0));
2659
2660     case USE:
2661       return 0;
2662
2663     case ASM_INPUT:
2664     case UNSPEC_VOLATILE:
2665       return 1;
2666
2667     case ASM_OPERANDS:
2668       if (MEM_VOLATILE_P (x))
2669         return 1;
2670
2671     /* Fall through.  */
2672
2673     default:
2674       break;
2675     }
2676
2677   return 0;
2678 }
2679
2680 /* Returns nonzero if X might set something which is not
2681    local to the function and is not constant.  */
2682
2683 static int
2684 nonlocal_set_p (rtx x)
2685 {
2686   if (INSN_P (x))
2687     {
2688       if (GET_CODE (x) == CALL_INSN)
2689         {
2690           if (! CONST_OR_PURE_CALL_P (x))
2691             return 1;
2692           x = CALL_INSN_FUNCTION_USAGE (x);
2693           if (x == 0)
2694             return 0;
2695         }
2696       else
2697         x = PATTERN (x);
2698     }
2699
2700   return for_each_rtx (&x, nonlocal_set_p_1, NULL);
2701 }
2702
2703 /* Mark the function if it is pure or constant.  */
2704
2705 void
2706 mark_constant_function (void)
2707 {
2708   rtx insn;
2709   int nonlocal_memory_referenced;
2710
2711   if (TREE_READONLY (current_function_decl)
2712       || DECL_IS_PURE (current_function_decl)
2713       || TREE_THIS_VOLATILE (current_function_decl)
2714       || current_function_has_nonlocal_goto
2715       || !targetm.binds_local_p (current_function_decl))
2716     return;
2717
2718   /* A loop might not return which counts as a side effect.  */
2719   if (mark_dfs_back_edges ())
2720     return;
2721
2722   nonlocal_memory_referenced = 0;
2723
2724   init_alias_analysis ();
2725
2726   /* Determine if this is a constant or pure function.  */
2727
2728   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2729     {
2730       if (! INSN_P (insn))
2731         continue;
2732
2733       if (nonlocal_set_p (insn) || global_reg_mentioned_p (insn)
2734           || volatile_refs_p (PATTERN (insn)))
2735         break;
2736
2737       if (! nonlocal_memory_referenced)
2738         nonlocal_memory_referenced = nonlocal_referenced_p (insn);
2739     }
2740
2741   end_alias_analysis ();
2742
2743   /* Mark the function.  */
2744
2745   if (insn)
2746     ;
2747   else if (nonlocal_memory_referenced)
2748     {
2749       cgraph_rtl_info (current_function_decl)->pure_function = 1;
2750       DECL_IS_PURE (current_function_decl) = 1;
2751     }
2752   else
2753     {
2754       cgraph_rtl_info (current_function_decl)->const_function = 1;
2755       TREE_READONLY (current_function_decl) = 1;
2756     }
2757 }
2758 \f
2759
2760 void
2761 init_alias_once (void)
2762 {
2763   int i;
2764
2765   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2766     /* Check whether this register can hold an incoming pointer
2767        argument.  FUNCTION_ARG_REGNO_P tests outgoing register
2768        numbers, so translate if necessary due to register windows.  */
2769     if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2770         && HARD_REGNO_MODE_OK (i, Pmode))
2771       static_reg_base_value[i]
2772         = gen_rtx_ADDRESS (VOIDmode, gen_rtx_REG (Pmode, i));
2773
2774   static_reg_base_value[STACK_POINTER_REGNUM]
2775     = gen_rtx_ADDRESS (Pmode, stack_pointer_rtx);
2776   static_reg_base_value[ARG_POINTER_REGNUM]
2777     = gen_rtx_ADDRESS (Pmode, arg_pointer_rtx);
2778   static_reg_base_value[FRAME_POINTER_REGNUM]
2779     = gen_rtx_ADDRESS (Pmode, frame_pointer_rtx);
2780 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2781   static_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2782     = gen_rtx_ADDRESS (Pmode, hard_frame_pointer_rtx);
2783 #endif
2784 }
2785
2786 /* Set MEMORY_MODIFIED when X modifies DATA (that is assumed
2787    to be memory reference.  */
2788 static bool memory_modified;
2789 static void
2790 memory_modified_1 (rtx x, rtx pat ATTRIBUTE_UNUSED, void *data)
2791 {
2792   if (MEM_P (x))
2793     {
2794       if (anti_dependence (x, (rtx)data) || output_dependence (x, (rtx)data))
2795         memory_modified = true;
2796     }
2797 }
2798
2799
2800 /* Return true when INSN possibly modify memory contents of MEM
2801    (ie address can be modified).  */
2802 bool
2803 memory_modified_in_insn_p (rtx mem, rtx insn)
2804 {
2805   if (!INSN_P (insn))
2806     return false;
2807   memory_modified = false;
2808   note_stores (PATTERN (insn), memory_modified_1, mem);
2809   return memory_modified;
2810 }
2811
2812 /* Initialize the aliasing machinery.  Initialize the REG_KNOWN_VALUE
2813    array.  */
2814
2815 void
2816 init_alias_analysis (void)
2817 {
2818   unsigned int maxreg = max_reg_num ();
2819   int changed, pass;
2820   int i;
2821   unsigned int ui;
2822   rtx insn;
2823
2824   timevar_push (TV_ALIAS_ANALYSIS);
2825
2826   reg_known_value_size = maxreg - FIRST_PSEUDO_REGISTER;
2827   reg_known_value = ggc_calloc (reg_known_value_size, sizeof (rtx));
2828   reg_known_equiv_p = xcalloc (reg_known_value_size, sizeof (bool));
2829
2830   /* Overallocate reg_base_value to allow some growth during loop
2831      optimization.  Loop unrolling can create a large number of
2832      registers.  */
2833   if (old_reg_base_value)
2834     {
2835       reg_base_value = old_reg_base_value;
2836       /* If varray gets large zeroing cost may get important.  */
2837       if (VARRAY_SIZE (reg_base_value) > 256
2838           && VARRAY_SIZE (reg_base_value) > 4 * maxreg)
2839         VARRAY_GROW (reg_base_value, maxreg);
2840       VARRAY_CLEAR (reg_base_value);
2841       if (VARRAY_SIZE (reg_base_value) < maxreg)
2842         VARRAY_GROW (reg_base_value, maxreg);
2843     }
2844   else
2845     {
2846       VARRAY_RTX_INIT (reg_base_value, maxreg, "reg_base_value");
2847     }
2848
2849   new_reg_base_value = xmalloc (maxreg * sizeof (rtx));
2850   reg_seen = xmalloc (maxreg);
2851   if (! reload_completed && flag_old_unroll_loops)
2852     {
2853       alias_invariant = ggc_calloc (maxreg, sizeof (rtx));
2854       alias_invariant_size = maxreg;
2855     }
2856
2857   /* The basic idea is that each pass through this loop will use the
2858      "constant" information from the previous pass to propagate alias
2859      information through another level of assignments.
2860
2861      This could get expensive if the assignment chains are long.  Maybe
2862      we should throttle the number of iterations, possibly based on
2863      the optimization level or flag_expensive_optimizations.
2864
2865      We could propagate more information in the first pass by making use
2866      of REG_N_SETS to determine immediately that the alias information
2867      for a pseudo is "constant".
2868
2869      A program with an uninitialized variable can cause an infinite loop
2870      here.  Instead of doing a full dataflow analysis to detect such problems
2871      we just cap the number of iterations for the loop.
2872
2873      The state of the arrays for the set chain in question does not matter
2874      since the program has undefined behavior.  */
2875
2876   pass = 0;
2877   do
2878     {
2879       /* Assume nothing will change this iteration of the loop.  */
2880       changed = 0;
2881
2882       /* We want to assign the same IDs each iteration of this loop, so
2883          start counting from zero each iteration of the loop.  */
2884       unique_id = 0;
2885
2886       /* We're at the start of the function each iteration through the
2887          loop, so we're copying arguments.  */
2888       copying_arguments = true;
2889
2890       /* Wipe the potential alias information clean for this pass.  */
2891       memset (new_reg_base_value, 0, maxreg * sizeof (rtx));
2892
2893       /* Wipe the reg_seen array clean.  */
2894       memset (reg_seen, 0, maxreg);
2895
2896       /* Mark all hard registers which may contain an address.
2897          The stack, frame and argument pointers may contain an address.
2898          An argument register which can hold a Pmode value may contain
2899          an address even if it is not in BASE_REGS.
2900
2901          The address expression is VOIDmode for an argument and
2902          Pmode for other registers.  */
2903
2904       memcpy (new_reg_base_value, static_reg_base_value,
2905               FIRST_PSEUDO_REGISTER * sizeof (rtx));
2906
2907       /* Walk the insns adding values to the new_reg_base_value array.  */
2908       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2909         {
2910           if (INSN_P (insn))
2911             {
2912               rtx note, set;
2913
2914 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2915               /* The prologue/epilogue insns are not threaded onto the
2916                  insn chain until after reload has completed.  Thus,
2917                  there is no sense wasting time checking if INSN is in
2918                  the prologue/epilogue until after reload has completed.  */
2919               if (reload_completed
2920                   && prologue_epilogue_contains (insn))
2921                 continue;
2922 #endif
2923
2924               /* If this insn has a noalias note, process it,  Otherwise,
2925                  scan for sets.  A simple set will have no side effects
2926                  which could change the base value of any other register.  */
2927
2928               if (GET_CODE (PATTERN (insn)) == SET
2929                   && REG_NOTES (insn) != 0
2930                   && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
2931                 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
2932               else
2933                 note_stores (PATTERN (insn), record_set, NULL);
2934
2935               set = single_set (insn);
2936
2937               if (set != 0
2938                   && REG_P (SET_DEST (set))
2939                   && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER)
2940                 {
2941                   unsigned int regno = REGNO (SET_DEST (set));
2942                   rtx src = SET_SRC (set);
2943                   rtx t;
2944
2945                   if (REG_NOTES (insn) != 0
2946                       && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2947                            && REG_N_SETS (regno) == 1)
2948                           || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
2949                       && GET_CODE (XEXP (note, 0)) != EXPR_LIST
2950                       && ! rtx_varies_p (XEXP (note, 0), 1)
2951                       && ! reg_overlap_mentioned_p (SET_DEST (set),
2952                                                     XEXP (note, 0)))
2953                     {
2954                       set_reg_known_value (regno, XEXP (note, 0));
2955                       set_reg_known_equiv_p (regno,
2956                         REG_NOTE_KIND (note) == REG_EQUIV);
2957                     }
2958                   else if (REG_N_SETS (regno) == 1
2959                            && GET_CODE (src) == PLUS
2960                            && REG_P (XEXP (src, 0))
2961                            && (t = get_reg_known_value (REGNO (XEXP (src, 0))))
2962                            && GET_CODE (XEXP (src, 1)) == CONST_INT)
2963                     {
2964                       t = plus_constant (t, INTVAL (XEXP (src, 1)));
2965                       set_reg_known_value (regno, t);
2966                       set_reg_known_equiv_p (regno, 0);
2967                     }
2968                   else if (REG_N_SETS (regno) == 1
2969                            && ! rtx_varies_p (src, 1))
2970                     {
2971                       set_reg_known_value (regno, src);
2972                       set_reg_known_equiv_p (regno, 0);
2973                     }
2974                 }
2975             }
2976           else if (GET_CODE (insn) == NOTE
2977                    && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
2978             copying_arguments = false;
2979         }
2980
2981       /* Now propagate values from new_reg_base_value to reg_base_value.  */
2982       if (maxreg != (unsigned int) max_reg_num())
2983         abort ();
2984       for (ui = 0; ui < maxreg; ui++)
2985         {
2986           if (new_reg_base_value[ui]
2987               && new_reg_base_value[ui] != VARRAY_RTX (reg_base_value, ui)
2988               && ! rtx_equal_p (new_reg_base_value[ui],
2989                                 VARRAY_RTX (reg_base_value, ui)))
2990             {
2991               VARRAY_RTX (reg_base_value, ui) = new_reg_base_value[ui];
2992               changed = 1;
2993             }
2994         }
2995     }
2996   while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
2997
2998   /* Fill in the remaining entries.  */
2999   for (i = 0; i < (int)reg_known_value_size; i++)
3000     if (reg_known_value[i] == 0)
3001       reg_known_value[i] = regno_reg_rtx[i + FIRST_PSEUDO_REGISTER];
3002
3003   /* Simplify the reg_base_value array so that no register refers to
3004      another register, except to special registers indirectly through
3005      ADDRESS expressions.
3006
3007      In theory this loop can take as long as O(registers^2), but unless
3008      there are very long dependency chains it will run in close to linear
3009      time.
3010
3011      This loop may not be needed any longer now that the main loop does
3012      a better job at propagating alias information.  */
3013   pass = 0;
3014   do
3015     {
3016       changed = 0;
3017       pass++;
3018       for (ui = 0; ui < maxreg; ui++)
3019         {
3020           rtx base = VARRAY_RTX (reg_base_value, ui);
3021           if (base && REG_P (base))
3022             {
3023               unsigned int base_regno = REGNO (base);
3024               if (base_regno == ui)             /* register set from itself */
3025                 VARRAY_RTX (reg_base_value, ui) = 0;
3026               else
3027                 VARRAY_RTX (reg_base_value, ui)
3028                   = VARRAY_RTX (reg_base_value, base_regno);
3029               changed = 1;
3030             }
3031         }
3032     }
3033   while (changed && pass < MAX_ALIAS_LOOP_PASSES);
3034
3035   /* Clean up.  */
3036   free (new_reg_base_value);
3037   new_reg_base_value = 0;
3038   free (reg_seen);
3039   reg_seen = 0;
3040   timevar_pop (TV_ALIAS_ANALYSIS);
3041 }
3042
3043 void
3044 end_alias_analysis (void)
3045 {
3046   old_reg_base_value = reg_base_value;
3047   ggc_free (reg_known_value);
3048   reg_known_value = 0;
3049   reg_known_value_size = 0;
3050   free (reg_known_equiv_p);
3051   reg_known_equiv_p = 0;
3052   if (alias_invariant)
3053     {
3054       ggc_free (alias_invariant);
3055       alias_invariant = 0;
3056       alias_invariant_size = 0;
3057     }
3058 }
3059
3060 #include "gt-alias.h"