OSDN Git Service

* alias.c (alias_sets_conflict_p): New function.
[pf3gnuchains/gcc-fork.git] / gcc / alias.c
1 /* Alias analysis for GNU C
2    Copyright (C) 1997, 1998, 1999, 2000 Free Software Foundation, Inc.
3    Contributed by John Carr (jfc@mit.edu).
4
5 This file is part of GNU CC.
6
7 GNU CC is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
11
12 GNU CC is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GNU CC; see the file COPYING.  If not, write to
19 the Free Software Foundation, 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "rtl.h"
25 #include "tree.h"
26 #include "tm_p.h"
27 #include "function.h"
28 #include "insn-flags.h"
29 #include "expr.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "basic-block.h"
33 #include "flags.h"
34 #include "output.h"
35 #include "toplev.h"
36 #include "cselib.h"
37 #include "splay-tree.h"
38 #include "ggc.h"
39
40 /* The alias sets assigned to MEMs assist the back-end in determining
41    which MEMs can alias which other MEMs.  In general, two MEMs in
42    different alias sets cannot alias each other, with one important
43    exception.  Consider something like:
44
45      struct S {int i; double d; };
46
47    a store to an `S' can alias something of either type `int' or type
48    `double'.  (However, a store to an `int' cannot alias a `double'
49    and vice versa.)  We indicate this via a tree structure that looks
50    like:
51            struct S
52             /   \
53            /     \
54          |/_     _\|
55          int    double
56
57    (The arrows are directed and point downwards.)
58     In this situation we say the alias set for `struct S' is the
59    `superset' and that those for `int' and `double' are `subsets'.
60
61    To see whether two alias sets can point to the same memory, we must
62    see if either alias set is a subset of the other. We need not trace
63    past immediate decendents, however, since we propagate all
64    grandchildren up one level.
65
66    Alias set zero is implicitly a superset of all other alias sets.
67    However, this is no actual entry for alias set zero.  It is an
68    error to attempt to explicitly construct a subset of zero.  */
69
70 typedef struct alias_set_entry
71 {
72   /* The alias set number, as stored in MEM_ALIAS_SET.  */
73   HOST_WIDE_INT alias_set;
74
75   /* The children of the alias set.  These are not just the immediate
76      children, but, in fact, all decendents.  So, if we have:
77
78        struct T { struct S s; float f; } 
79
80      continuing our example above, the children here will be all of
81      `int', `double', `float', and `struct S'.  */
82   splay_tree children;
83
84   /* Nonzero if would have a child of zero: this effectively makes this
85      alias set the same as alias set zero.  */
86   int has_zero_child;
87 } *alias_set_entry;
88
89 static int rtx_equal_for_memref_p       PARAMS ((rtx, rtx));
90 static rtx find_symbolic_term           PARAMS ((rtx));
91 static rtx get_addr                     PARAMS ((rtx));
92 static int memrefs_conflict_p           PARAMS ((int, rtx, int, rtx,
93                                                  HOST_WIDE_INT));
94 static void record_set                  PARAMS ((rtx, rtx, void *));
95 static rtx find_base_term               PARAMS ((rtx));
96 static int base_alias_check             PARAMS ((rtx, rtx, enum machine_mode,
97                                                  enum machine_mode));
98 static rtx find_base_value              PARAMS ((rtx));
99 static int mems_in_disjoint_alias_sets_p PARAMS ((rtx, rtx));
100 static int insert_subset_children       PARAMS ((splay_tree_node, void*));
101 static tree find_base_decl            PARAMS ((tree));
102 static alias_set_entry get_alias_set_entry PARAMS ((HOST_WIDE_INT));
103 static rtx fixed_scalar_and_varying_struct_p PARAMS ((rtx, rtx, rtx, rtx,
104                                                       int (*) (rtx)));
105 static int aliases_everything_p         PARAMS ((rtx));
106 static int write_dependence_p           PARAMS ((rtx, rtx, int));
107 static int nonlocal_mentioned_p         PARAMS ((rtx));
108
109 static int loop_p                       PARAMS ((void));
110
111 /* Set up all info needed to perform alias analysis on memory references.  */
112
113 /* Returns the size in bytes of the mode of X.  */
114 #define SIZE_FOR_MODE(X) (GET_MODE_SIZE (GET_MODE (X)))
115
116 /* Returns nonzero if MEM1 and MEM2 do not alias because they are in
117    different alias sets.  We ignore alias sets in functions making use
118    of variable arguments because the va_arg macros on some systems are
119    not legal ANSI C.  */
120 #define DIFFERENT_ALIAS_SETS_P(MEM1, MEM2)                      \
121   mems_in_disjoint_alias_sets_p (MEM1, MEM2)
122
123 /* Cap the number of passes we make over the insns propagating alias
124    information through set chains.   10 is a completely arbitrary choice.  */
125 #define MAX_ALIAS_LOOP_PASSES 10
126    
127 /* reg_base_value[N] gives an address to which register N is related.
128    If all sets after the first add or subtract to the current value
129    or otherwise modify it so it does not point to a different top level
130    object, reg_base_value[N] is equal to the address part of the source
131    of the first set.
132
133    A base address can be an ADDRESS, SYMBOL_REF, or LABEL_REF.  ADDRESS
134    expressions represent certain special values: function arguments and
135    the stack, frame, and argument pointers.  
136
137    The contents of an ADDRESS is not normally used, the mode of the
138    ADDRESS determines whether the ADDRESS is a function argument or some
139    other special value.  Pointer equality, not rtx_equal_p, determines whether
140    two ADDRESS expressions refer to the same base address.
141
142    The only use of the contents of an ADDRESS is for determining if the
143    current function performs nonlocal memory memory references for the
144    purposes of marking the function as a constant function.  */
145
146 static rtx *reg_base_value;
147 static rtx *new_reg_base_value;
148 static unsigned int reg_base_value_size; /* size of reg_base_value array */
149
150 #define REG_BASE_VALUE(X) \
151   (REGNO (X) < reg_base_value_size ? reg_base_value[REGNO (X)] : 0)
152
153 /* Vector of known invariant relationships between registers.  Set in
154    loop unrolling.  Indexed by register number, if nonzero the value
155    is an expression describing this register in terms of another.
156
157    The length of this array is REG_BASE_VALUE_SIZE.
158
159    Because this array contains only pseudo registers it has no effect
160    after reload.  */
161 static rtx *alias_invariant;
162
163 /* Vector indexed by N giving the initial (unchanging) value known for
164    pseudo-register N.  This array is initialized in
165    init_alias_analysis, and does not change until end_alias_analysis
166    is called.  */
167 rtx *reg_known_value;
168
169 /* Indicates number of valid entries in reg_known_value.  */
170 static unsigned int reg_known_value_size;
171
172 /* Vector recording for each reg_known_value whether it is due to a
173    REG_EQUIV note.  Future passes (viz., reload) may replace the
174    pseudo with the equivalent expression and so we account for the
175    dependences that would be introduced if that happens.
176
177    The REG_EQUIV notes created in assign_parms may mention the arg
178    pointer, and there are explicit insns in the RTL that modify the
179    arg pointer.  Thus we must ensure that such insns don't get
180    scheduled across each other because that would invalidate the
181    REG_EQUIV notes.  One could argue that the REG_EQUIV notes are
182    wrong, but solving the problem in the scheduler will likely give
183    better code, so we do it here.  */
184 char *reg_known_equiv_p;
185
186 /* True when scanning insns from the start of the rtl to the
187    NOTE_INSN_FUNCTION_BEG note.  */
188 static int copying_arguments;
189
190 /* The splay-tree used to store the various alias set entries.  */
191 static splay_tree alias_sets;
192 \f
193 /* Returns a pointer to the alias set entry for ALIAS_SET, if there is
194    such an entry, or NULL otherwise.  */
195
196 static alias_set_entry
197 get_alias_set_entry (alias_set)
198      HOST_WIDE_INT alias_set;
199 {
200   splay_tree_node sn
201     = splay_tree_lookup (alias_sets, (splay_tree_key) alias_set);
202
203   return sn != 0 ? ((alias_set_entry) sn->value) : 0;
204 }
205
206 /* Returns nonzero if the alias sets for MEM1 and MEM2 are such that
207    the two MEMs cannot alias each other.  */
208
209 static int 
210 mems_in_disjoint_alias_sets_p (mem1, mem2)
211      rtx mem1;
212      rtx mem2;
213 {
214 #ifdef ENABLE_CHECKING  
215 /* Perform a basic sanity check.  Namely, that there are no alias sets
216    if we're not using strict aliasing.  This helps to catch bugs
217    whereby someone uses PUT_CODE, but doesn't clear MEM_ALIAS_SET, or
218    where a MEM is allocated in some way other than by the use of
219    gen_rtx_MEM, and the MEM_ALIAS_SET is not cleared.  If we begin to
220    use alias sets to indicate that spilled registers cannot alias each
221    other, we might need to remove this check.  */
222   if (! flag_strict_aliasing
223       && (MEM_ALIAS_SET (mem1) != 0 || MEM_ALIAS_SET (mem2) != 0))
224     abort ();
225 #endif
226
227   return ! alias_sets_conflict_p (MEM_ALIAS_SET (mem1), MEM_ALIAS_SET (mem2));
228 }
229
230 /* Insert the NODE into the splay tree given by DATA.  Used by
231    record_alias_subset via splay_tree_foreach.  */
232
233 static int
234 insert_subset_children (node, data)
235      splay_tree_node node;
236      void *data;
237 {
238   splay_tree_insert ((splay_tree) data, node->key, node->value);
239
240   return 0;
241 }
242
243 /* Return 1 if the two specified alias sets may conflict.  */
244
245 int
246 alias_sets_conflict_p (set1, set2)
247      HOST_WIDE_INT set1, set2;
248 {
249   alias_set_entry ase;
250
251   /* If have no alias set information for one of the operands, we have
252      to assume it can alias anything.  */
253   if (set1 == 0 || set2 == 0
254       /* If the two alias sets are the same, they may alias.  */
255       || set1 == set2)
256     return 1;
257
258   /* See if the first alias set is a subset of the second.  */
259   ase = get_alias_set_entry (set1);
260   if (ase != 0
261       && (ase->has_zero_child
262           || splay_tree_lookup (ase->children,
263                                 (splay_tree_key) set2)))
264     return 1;
265
266   /* Now do the same, but with the alias sets reversed.  */
267   ase = get_alias_set_entry (set2);
268   if (ase != 0
269       && (ase->has_zero_child
270           || splay_tree_lookup (ase->children,
271                                 (splay_tree_key) set1)))
272     return 1;
273
274   /* The two alias sets are distinct and neither one is the
275      child of the other.  Therefore, they cannot alias.  */
276   return 0;
277 }
278 \f
279 /* Return 1 if TYPE is a RECORD_TYPE, UNION_TYPE, or QUAL_UNION_TYPE and has
280    has any readonly fields.  If any of the fields have types that
281    contain readonly fields, return true as well.  */
282
283 int
284 readonly_fields_p (type)
285      tree type;
286 {
287   tree field;
288
289   if (TREE_CODE (type) != RECORD_TYPE && TREE_CODE (type) != UNION_TYPE
290       && TREE_CODE (type) != QUAL_UNION_TYPE)
291     return 0;
292
293   for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
294     if (TREE_CODE (field) == FIELD_DECL
295         && (TREE_READONLY (field)
296             || readonly_fields_p (TREE_TYPE (field))))
297       return 1;
298
299   return 0;
300 }
301 \f
302 /* Return 1 if any MEM object of type T1 will always conflict (using the
303    dependency routines in this file) with any MEM object of type T2.
304    This is used when allocating temporary storage.  If T1 and/or T2 are
305    NULL_TREE, it means we know nothing about the storage.  */
306
307 int
308 objects_must_conflict_p (t1, t2)
309      tree t1, t2;
310 {
311   /* If they are the same type, they must conflict.  */
312   if (t1 == t2
313       /* Likewise if both are volatile.  */
314       || (t1 != 0 && TYPE_VOLATILE (t1) && t2 != 0 && TYPE_VOLATILE (t2)))
315     return 1;
316
317   /* We now know they are different types.  If one or both has readonly fields
318      or if one is readonly and the other not, they may not conflict.
319      Likewise if one is aggregate and the other is scalar.  */
320   if ((t1 != 0 && readonly_fields_p (t1))
321       || (t2 != 0 && readonly_fields_p (t2))
322       || ((t1 != 0 && TYPE_READONLY (t1))
323           != (t2 != 0 && TYPE_READONLY (t2)))
324       || ((t1 != 0 && AGGREGATE_TYPE_P (t1))
325           != (t2 != 0 && AGGREGATE_TYPE_P (t2))))
326     return 0;
327
328   /* Otherwise they conflict only if the alias sets conflict. */
329   return alias_sets_conflict_p (t1 ? get_alias_set (t1) : 0,
330                                 t2 ? get_alias_set (t2) : 0);
331 }
332 \f
333 /* T is an expression with pointer type.  Find the DECL on which this
334    expression is based.  (For example, in `a[i]' this would be `a'.)
335    If there is no such DECL, or a unique decl cannot be determined,
336    NULL_TREE is retured.  */
337
338 static tree
339 find_base_decl (t)
340      tree t;
341 {
342   tree d0, d1, d2;
343
344   if (t == 0 || t == error_mark_node || ! POINTER_TYPE_P (TREE_TYPE (t)))
345     return 0;
346
347   /* If this is a declaration, return it.  */
348   if (TREE_CODE_CLASS (TREE_CODE (t)) == 'd')
349     return t;
350
351   /* Handle general expressions.  It would be nice to deal with
352      COMPONENT_REFs here.  If we could tell that `a' and `b' were the
353      same, then `a->f' and `b->f' are also the same.  */
354   switch (TREE_CODE_CLASS (TREE_CODE (t)))
355     {
356     case '1':
357       return find_base_decl (TREE_OPERAND (t, 0));
358
359     case '2':
360       /* Return 0 if found in neither or both are the same.  */
361       d0 = find_base_decl (TREE_OPERAND (t, 0));
362       d1 = find_base_decl (TREE_OPERAND (t, 1));
363       if (d0 == d1)
364         return d0;
365       else if (d0 == 0)
366         return d1;
367       else if (d1 == 0)
368         return d0;
369       else
370         return 0;
371
372     case '3':
373       d0 = find_base_decl (TREE_OPERAND (t, 0));
374       d1 = find_base_decl (TREE_OPERAND (t, 1));
375       d0 = find_base_decl (TREE_OPERAND (t, 0));
376       d2 = find_base_decl (TREE_OPERAND (t, 2));
377
378       /* Set any nonzero values from the last, then from the first.  */
379       if (d1 == 0) d1 = d2;
380       if (d0 == 0) d0 = d1;
381       if (d1 == 0) d1 = d0;
382       if (d2 == 0) d2 = d1;
383
384       /* At this point all are nonzero or all are zero.  If all three are the
385          same, return it.  Otherwise, return zero.  */
386       return (d0 == d1 && d1 == d2) ? d0 : 0;
387
388     default:
389       return 0;
390     }
391 }
392
393 /* Return the alias set for T, which may be either a type or an
394    expression.  Call language-specific routine for help, if needed.  */
395
396 HOST_WIDE_INT
397 get_alias_set (t)
398      tree t;
399 {
400   tree orig_t;
401   HOST_WIDE_INT set;
402
403   /* If we're not doing any alias analysis, just assume everything
404      aliases everything else.  Also return 0 if this or its type is
405      an error.  */
406   if (! flag_strict_aliasing || t == error_mark_node
407       || (! TYPE_P (t)
408           && (TREE_TYPE (t) == 0 || TREE_TYPE (t) == error_mark_node)))
409     return 0;
410
411   /* We can be passed either an expression or a type.  This and the
412      language-specific routine may make mutually-recursive calls to
413      each other to figure out what to do.  At each juncture, we see if
414      this is a tree that the language may need to handle specially.
415      First handle things that aren't types and start by removing nops
416      since we care only about the actual object.  */
417   if (! TYPE_P (t))
418     {
419       while (TREE_CODE (t) == NOP_EXPR || TREE_CODE (t) == CONVERT_EXPR
420              || TREE_CODE (t) == NON_LVALUE_EXPR)
421         t = TREE_OPERAND (t, 0);
422
423       /* Now give the language a chance to do something but record what we
424          gave it this time.  */
425       orig_t = t;
426       if ((set = lang_get_alias_set (t)) != -1)
427         return set;
428
429       /* Now loop the same way as get_inner_reference and get the alias
430          set to use.  Pick up the outermost object that we could have
431          a pointer to.  */
432       while (1)
433         {
434           /* Unnamed bitfields are not an addressable object.  */
435           if (TREE_CODE (t) == BIT_FIELD_REF)
436             ;
437           else if (TREE_CODE (t) == COMPONENT_REF)
438             {
439               if (! DECL_NONADDRESSABLE_P (TREE_OPERAND (t, 1)))
440                 /* Stop at an adressable decl.  */
441                 break;
442             }
443           else if (TREE_CODE (t) == ARRAY_REF)
444             {
445               if (! TYPE_NONALIASED_COMPONENT
446                   (TREE_TYPE (TREE_OPERAND (t, 0))))
447                 /* Stop at an addresssable array element.  */
448                 break;
449             }
450           else if (TREE_CODE (t) != NON_LVALUE_EXPR
451                    && ! ((TREE_CODE (t) == NOP_EXPR
452                       || TREE_CODE (t) == CONVERT_EXPR)
453                      && (TYPE_MODE (TREE_TYPE (t))
454                          == TYPE_MODE (TREE_TYPE (TREE_OPERAND (t, 0))))))
455             /* Stop if not one of above and not mode-preserving conversion. */
456             break;
457
458           t = TREE_OPERAND (t, 0);
459         }
460                    
461       if (TREE_CODE (t) == INDIRECT_REF)
462         {
463           /* Check for accesses through restrict-qualified pointers.  */
464           tree decl = find_base_decl (TREE_OPERAND (t, 0));
465
466           if (decl && DECL_POINTER_ALIAS_SET_KNOWN_P (decl))
467             /* We use the alias set indicated in the declaration.  */
468             return DECL_POINTER_ALIAS_SET (decl);
469
470           /* If we have an INDIRECT_REF via a void pointer, we don't
471              know anything about what that might alias.  */
472           if (TREE_CODE (TREE_TYPE (t)) == VOID_TYPE)
473             return 0;
474         }
475
476       /* Give the language another chance to do something special.  */
477       if (orig_t != t
478           && (set = lang_get_alias_set (t)) != -1)
479         return set;
480
481       /* Now all we care about is the type.  */
482       t = TREE_TYPE (t);
483     }
484
485   /* Variant qualifiers don't affect the alias set, so get the main
486      variant. If this is a type with a known alias set, return it.  */
487   t = TYPE_MAIN_VARIANT (t);
488   if (TYPE_P (t) && TYPE_ALIAS_SET_KNOWN_P (t))
489     return TYPE_ALIAS_SET (t);
490
491   /* See if the language has special handling for this type.  */
492   if ((set = lang_get_alias_set (t)) != -1)
493     {
494       /* If the alias set is now known, we are done.  */
495       if (TYPE_ALIAS_SET_KNOWN_P (t))
496         return TYPE_ALIAS_SET (t);
497     }
498
499   /* There are no objects of FUNCTION_TYPE, so there's no point in
500      using up an alias set for them.  (There are, of course, pointers
501      and references to functions, but that's different.)  */
502   else if (TREE_CODE (t) == FUNCTION_TYPE)
503     set = 0;
504   else
505     /* Otherwise make a new alias set for this type.  */
506     set = new_alias_set ();
507
508   TYPE_ALIAS_SET (t) = set;
509
510   /* If this is an aggregate type, we must record any component aliasing
511      information.  */
512   if (AGGREGATE_TYPE_P (t) || TREE_CODE (t) == COMPLEX_TYPE)
513     record_component_aliases (t);
514
515   return set;
516 }
517
518 /* Return a brand-new alias set.  */
519
520 HOST_WIDE_INT
521 new_alias_set ()
522 {
523   static HOST_WIDE_INT last_alias_set;
524
525   if (flag_strict_aliasing)
526     return ++last_alias_set;
527   else
528     return 0;
529 }
530
531 /* Indicate that things in SUBSET can alias things in SUPERSET, but
532    not vice versa.  For example, in C, a store to an `int' can alias a
533    structure containing an `int', but not vice versa.  Here, the
534    structure would be the SUPERSET and `int' the SUBSET.  This
535    function should be called only once per SUPERSET/SUBSET pair. 
536
537    It is illegal for SUPERSET to be zero; everything is implicitly a
538    subset of alias set zero.  */
539
540 void
541 record_alias_subset (superset, subset)
542      HOST_WIDE_INT superset;
543      HOST_WIDE_INT subset;
544 {
545   alias_set_entry superset_entry;
546   alias_set_entry subset_entry;
547
548   if (superset == 0)
549     abort ();
550
551   superset_entry = get_alias_set_entry (superset);
552   if (superset_entry == 0) 
553     {
554       /* Create an entry for the SUPERSET, so that we have a place to
555          attach the SUBSET.  */
556       superset_entry
557         = (alias_set_entry) xmalloc (sizeof (struct alias_set_entry));
558       superset_entry->alias_set = superset;
559       superset_entry->children 
560         = splay_tree_new (splay_tree_compare_ints, 0, 0);
561       superset_entry->has_zero_child = 0;
562       splay_tree_insert (alias_sets, (splay_tree_key) superset,
563                          (splay_tree_value) superset_entry);
564     }
565
566   if (subset == 0)
567     superset_entry->has_zero_child = 1;
568   else
569     {
570       subset_entry = get_alias_set_entry (subset);
571       /* If there is an entry for the subset, enter all of its children
572          (if they are not already present) as children of the SUPERSET.  */
573       if (subset_entry) 
574         {
575           if (subset_entry->has_zero_child)
576             superset_entry->has_zero_child = 1;
577
578           splay_tree_foreach (subset_entry->children, insert_subset_children,
579                               superset_entry->children);
580         }
581
582       /* Enter the SUBSET itself as a child of the SUPERSET.  */
583       splay_tree_insert (superset_entry->children, 
584                          (splay_tree_key) subset, 0);
585     }
586 }
587
588 /* Record that component types of TYPE, if any, are part of that type for
589    aliasing purposes.  For record types, we only record component types
590    for fields that are marked addressable.  For array types, we always
591    record the component types, so the front end should not call this
592    function if the individual component aren't addressable.  */
593
594 void
595 record_component_aliases (type)
596      tree type;
597 {
598   HOST_WIDE_INT superset = get_alias_set (type);
599   tree field;
600
601   if (superset == 0)
602     return;
603
604   switch (TREE_CODE (type))
605     {
606     case ARRAY_TYPE:
607       if (! TYPE_NONALIASED_COMPONENT (type))
608         record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
609       break;
610
611     case RECORD_TYPE:
612     case UNION_TYPE:
613     case QUAL_UNION_TYPE:
614       for (field = TYPE_FIELDS (type); field != 0; field = TREE_CHAIN (field))
615         if (TREE_CODE (field) == FIELD_DECL && ! DECL_NONADDRESSABLE_P (field))
616           record_alias_subset (superset, get_alias_set (TREE_TYPE (field)));
617       break;
618
619     case COMPLEX_TYPE:
620       record_alias_subset (superset, get_alias_set (TREE_TYPE (type)));
621       break;
622
623     default:
624       break;
625     }
626 }
627
628 /* Allocate an alias set for use in storing and reading from the varargs
629    spill area.  */
630
631 HOST_WIDE_INT
632 get_varargs_alias_set ()
633 {
634   static HOST_WIDE_INT set = -1;
635
636   if (set == -1)
637     set = new_alias_set ();
638
639   return set;
640 }
641
642 /* Likewise, but used for the fixed portions of the frame, e.g., register
643    save areas.  */
644
645 HOST_WIDE_INT
646 get_frame_alias_set ()
647 {
648   static HOST_WIDE_INT set = -1;
649
650   if (set == -1)
651     set = new_alias_set ();
652
653   return set;
654 }
655
656 /* Inside SRC, the source of a SET, find a base address.  */
657
658 static rtx
659 find_base_value (src)
660      register rtx src;
661 {
662   switch (GET_CODE (src))
663     {
664     case SYMBOL_REF:
665     case LABEL_REF:
666       return src;
667
668     case REG:
669       /* At the start of a function, argument registers have known base
670          values which may be lost later.  Returning an ADDRESS
671          expression here allows optimization based on argument values
672          even when the argument registers are used for other purposes.  */
673       if (REGNO (src) < FIRST_PSEUDO_REGISTER && copying_arguments)
674         return new_reg_base_value[REGNO (src)];
675
676       /* If a pseudo has a known base value, return it.  Do not do this
677          for hard regs since it can result in a circular dependency
678          chain for registers which have values at function entry.
679
680          The test above is not sufficient because the scheduler may move
681          a copy out of an arg reg past the NOTE_INSN_FUNCTION_BEGIN.  */
682       if (REGNO (src) >= FIRST_PSEUDO_REGISTER
683           && (unsigned) REGNO (src) < reg_base_value_size
684           && reg_base_value[REGNO (src)])
685         return reg_base_value[REGNO (src)];
686
687       return src;
688
689     case MEM:
690       /* Check for an argument passed in memory.  Only record in the
691          copying-arguments block; it is too hard to track changes
692          otherwise.  */
693       if (copying_arguments
694           && (XEXP (src, 0) == arg_pointer_rtx
695               || (GET_CODE (XEXP (src, 0)) == PLUS
696                   && XEXP (XEXP (src, 0), 0) == arg_pointer_rtx)))
697         return gen_rtx_ADDRESS (VOIDmode, src);
698       return 0;
699
700     case CONST:
701       src = XEXP (src, 0);
702       if (GET_CODE (src) != PLUS && GET_CODE (src) != MINUS)
703         break;
704
705       /* ... fall through ... */
706
707     case PLUS:
708     case MINUS:
709       {
710         rtx temp, src_0 = XEXP (src, 0), src_1 = XEXP (src, 1);
711
712         /* If either operand is a REG, then see if we already have
713            a known value for it.  */
714         if (GET_CODE (src_0) == REG)
715           {
716             temp = find_base_value (src_0);
717             if (temp != 0)
718               src_0 = temp;
719           }
720
721         if (GET_CODE (src_1) == REG)
722           {
723             temp = find_base_value (src_1);
724             if (temp!= 0)
725               src_1 = temp;
726           }
727
728         /* Guess which operand is the base address:
729            If either operand is a symbol, then it is the base.  If
730            either operand is a CONST_INT, then the other is the base.  */
731         if (GET_CODE (src_1) == CONST_INT || CONSTANT_P (src_0))
732           return find_base_value (src_0);
733         else if (GET_CODE (src_0) == CONST_INT || CONSTANT_P (src_1))
734           return find_base_value (src_1);
735
736         /* This might not be necessary anymore:
737            If either operand is a REG that is a known pointer, then it
738            is the base.  */
739         else if (GET_CODE (src_0) == REG && REG_POINTER (src_0))
740           return find_base_value (src_0);
741         else if (GET_CODE (src_1) == REG && REG_POINTER (src_1))
742           return find_base_value (src_1);
743
744         return 0;
745       }
746
747     case LO_SUM:
748       /* The standard form is (lo_sum reg sym) so look only at the
749          second operand.  */
750       return find_base_value (XEXP (src, 1));
751
752     case AND:
753       /* If the second operand is constant set the base
754          address to the first operand. */
755       if (GET_CODE (XEXP (src, 1)) == CONST_INT && INTVAL (XEXP (src, 1)) != 0)
756         return find_base_value (XEXP (src, 0));
757       return 0;
758
759     case ZERO_EXTEND:
760     case SIGN_EXTEND:   /* used for NT/Alpha pointers */
761     case HIGH:
762       return find_base_value (XEXP (src, 0));
763
764     default:
765       break;
766     }
767
768   return 0;
769 }
770
771 /* Called from init_alias_analysis indirectly through note_stores.  */
772
773 /* While scanning insns to find base values, reg_seen[N] is nonzero if
774    register N has been set in this function.  */
775 static char *reg_seen;
776
777 /* Addresses which are known not to alias anything else are identified
778    by a unique integer.  */
779 static int unique_id;
780
781 static void
782 record_set (dest, set, data)
783      rtx dest, set;
784      void *data ATTRIBUTE_UNUSED;
785 {
786   register unsigned regno;
787   rtx src;
788
789   if (GET_CODE (dest) != REG)
790     return;
791
792   regno = REGNO (dest);
793
794   if (regno >= reg_base_value_size)
795     abort ();
796
797   if (set)
798     {
799       /* A CLOBBER wipes out any old value but does not prevent a previously
800          unset register from acquiring a base address (i.e. reg_seen is not
801          set).  */
802       if (GET_CODE (set) == CLOBBER)
803         {
804           new_reg_base_value[regno] = 0;
805           return;
806         }
807       src = SET_SRC (set);
808     }
809   else
810     {
811       if (reg_seen[regno])
812         {
813           new_reg_base_value[regno] = 0;
814           return;
815         }
816       reg_seen[regno] = 1;
817       new_reg_base_value[regno] = gen_rtx_ADDRESS (Pmode,
818                                                    GEN_INT (unique_id++));
819       return;
820     }
821
822   /* This is not the first set.  If the new value is not related to the
823      old value, forget the base value. Note that the following code is
824      not detected:
825      extern int x, y;  int *p = &x; p += (&y-&x);
826      ANSI C does not allow computing the difference of addresses
827      of distinct top level objects.  */
828   if (new_reg_base_value[regno])
829     switch (GET_CODE (src))
830       {
831       case LO_SUM:
832       case PLUS:
833       case MINUS:
834         if (XEXP (src, 0) != dest && XEXP (src, 1) != dest)
835           new_reg_base_value[regno] = 0;
836         break;
837       case AND:
838         if (XEXP (src, 0) != dest || GET_CODE (XEXP (src, 1)) != CONST_INT)
839           new_reg_base_value[regno] = 0;
840         break;
841       default:
842         new_reg_base_value[regno] = 0;
843         break;
844       }
845   /* If this is the first set of a register, record the value.  */
846   else if ((regno >= FIRST_PSEUDO_REGISTER || ! fixed_regs[regno])
847            && ! reg_seen[regno] && new_reg_base_value[regno] == 0)
848     new_reg_base_value[regno] = find_base_value (src);
849
850   reg_seen[regno] = 1;
851 }
852
853 /* Called from loop optimization when a new pseudo-register is
854    created.  It indicates that REGNO is being set to VAL.  f INVARIANT
855    is true then this value also describes an invariant relationship
856    which can be used to deduce that two registers with unknown values
857    are different.  */
858
859 void
860 record_base_value (regno, val, invariant)
861      unsigned int regno;
862      rtx val;
863      int invariant;
864 {
865   if (regno >= reg_base_value_size)
866     return;
867
868   if (invariant && alias_invariant)
869     alias_invariant[regno] = val;
870
871   if (GET_CODE (val) == REG)
872     {
873       if (REGNO (val) < reg_base_value_size)
874         reg_base_value[regno] = reg_base_value[REGNO (val)];
875
876       return;
877     }
878
879   reg_base_value[regno] = find_base_value (val);
880 }
881
882 /* Returns a canonical version of X, from the point of view alias
883    analysis.  (For example, if X is a MEM whose address is a register,
884    and the register has a known value (say a SYMBOL_REF), then a MEM
885    whose address is the SYMBOL_REF is returned.)  */
886
887 rtx
888 canon_rtx (x)
889      rtx x;
890 {
891   /* Recursively look for equivalences.  */
892   if (GET_CODE (x) == REG && REGNO (x) >= FIRST_PSEUDO_REGISTER
893       && REGNO (x) < reg_known_value_size)
894     return reg_known_value[REGNO (x)] == x
895       ? x : canon_rtx (reg_known_value[REGNO (x)]);
896   else if (GET_CODE (x) == PLUS)
897     {
898       rtx x0 = canon_rtx (XEXP (x, 0));
899       rtx x1 = canon_rtx (XEXP (x, 1));
900
901       if (x0 != XEXP (x, 0) || x1 != XEXP (x, 1))
902         {
903           /* We can tolerate LO_SUMs being offset here; these
904              rtl are used for nothing other than comparisons.  */
905           if (GET_CODE (x0) == CONST_INT)
906             return plus_constant_for_output (x1, INTVAL (x0));
907           else if (GET_CODE (x1) == CONST_INT)
908             return plus_constant_for_output (x0, INTVAL (x1));
909           return gen_rtx_PLUS (GET_MODE (x), x0, x1);
910         }
911     }
912
913   /* This gives us much better alias analysis when called from
914      the loop optimizer.   Note we want to leave the original
915      MEM alone, but need to return the canonicalized MEM with
916      all the flags with their original values.  */
917   else if (GET_CODE (x) == MEM)
918     {
919       rtx addr = canon_rtx (XEXP (x, 0));
920
921       if (addr != XEXP (x, 0))
922         {
923           rtx new = gen_rtx_MEM (GET_MODE (x), addr);
924
925           MEM_COPY_ATTRIBUTES (new, x);
926           x = new;
927         }
928     }
929   return x;
930 }
931
932 /* Return 1 if X and Y are identical-looking rtx's.
933
934    We use the data in reg_known_value above to see if two registers with
935    different numbers are, in fact, equivalent.  */
936
937 static int
938 rtx_equal_for_memref_p (x, y)
939      rtx x, y;
940 {
941   register int i;
942   register int j;
943   register enum rtx_code code;
944   register const char *fmt;
945
946   if (x == 0 && y == 0)
947     return 1;
948   if (x == 0 || y == 0)
949     return 0;
950
951   x = canon_rtx (x);
952   y = canon_rtx (y);
953
954   if (x == y)
955     return 1;
956
957   code = GET_CODE (x);
958   /* Rtx's of different codes cannot be equal.  */
959   if (code != GET_CODE (y))
960     return 0;
961
962   /* (MULT:SI x y) and (MULT:HI x y) are NOT equivalent.
963      (REG:SI x) and (REG:HI x) are NOT equivalent.  */
964
965   if (GET_MODE (x) != GET_MODE (y))
966     return 0;
967
968   /* Some RTL can be compared without a recursive examination.  */
969   switch (code)
970     {
971     case REG:
972       return REGNO (x) == REGNO (y);
973
974     case LABEL_REF:
975       return XEXP (x, 0) == XEXP (y, 0);
976       
977     case SYMBOL_REF:
978       return XSTR (x, 0) == XSTR (y, 0);
979
980     case CONST_INT:
981     case CONST_DOUBLE:
982       /* There's no need to compare the contents of CONST_DOUBLEs or
983          CONST_INTs because pointer equality is a good enough
984          comparison for these nodes.  */
985       return 0;
986
987     case ADDRESSOF:
988       return (REGNO (XEXP (x, 0)) == REGNO (XEXP (y, 0))
989               && XINT (x, 1) == XINT (y, 1));
990
991     default:
992       break;
993     }
994
995   /* For commutative operations, the RTX match if the operand match in any
996      order.  Also handle the simple binary and unary cases without a loop.  */
997   if (code == EQ || code == NE || GET_RTX_CLASS (code) == 'c')
998     return ((rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
999              && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)))
1000             || (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 1))
1001                 && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 0))));
1002   else if (GET_RTX_CLASS (code) == '<' || GET_RTX_CLASS (code) == '2')
1003     return (rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0))
1004             && rtx_equal_for_memref_p (XEXP (x, 1), XEXP (y, 1)));
1005   else if (GET_RTX_CLASS (code) == '1')
1006     return rtx_equal_for_memref_p (XEXP (x, 0), XEXP (y, 0));
1007
1008   /* Compare the elements.  If any pair of corresponding elements
1009      fail to match, return 0 for the whole things.
1010
1011      Limit cases to types which actually appear in addresses.  */
1012
1013   fmt = GET_RTX_FORMAT (code);
1014   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1015     {
1016       switch (fmt[i])
1017         {
1018         case 'i':
1019           if (XINT (x, i) != XINT (y, i))
1020             return 0;
1021           break;
1022
1023         case 'E':
1024           /* Two vectors must have the same length.  */
1025           if (XVECLEN (x, i) != XVECLEN (y, i))
1026             return 0;
1027
1028           /* And the corresponding elements must match.  */
1029           for (j = 0; j < XVECLEN (x, i); j++)
1030             if (rtx_equal_for_memref_p (XVECEXP (x, i, j),
1031                                         XVECEXP (y, i, j)) == 0)
1032               return 0;
1033           break;
1034
1035         case 'e':
1036           if (rtx_equal_for_memref_p (XEXP (x, i), XEXP (y, i)) == 0)
1037             return 0;
1038           break;
1039
1040         /* This can happen for an asm which clobbers memory.  */
1041         case '0':
1042           break;
1043
1044           /* It is believed that rtx's at this level will never
1045              contain anything but integers and other rtx's,
1046              except for within LABEL_REFs and SYMBOL_REFs.  */
1047         default:
1048           abort ();
1049         }
1050     }
1051   return 1;
1052 }
1053
1054 /* Given an rtx X, find a SYMBOL_REF or LABEL_REF within
1055    X and return it, or return 0 if none found.  */
1056
1057 static rtx
1058 find_symbolic_term (x)
1059      rtx x;
1060 {
1061   register int i;
1062   register enum rtx_code code;
1063   register const char *fmt;
1064
1065   code = GET_CODE (x);
1066   if (code == SYMBOL_REF || code == LABEL_REF)
1067     return x;
1068   if (GET_RTX_CLASS (code) == 'o')
1069     return 0;
1070
1071   fmt = GET_RTX_FORMAT (code);
1072   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1073     {
1074       rtx t;
1075
1076       if (fmt[i] == 'e')
1077         {
1078           t = find_symbolic_term (XEXP (x, i));
1079           if (t != 0)
1080             return t;
1081         }
1082       else if (fmt[i] == 'E')
1083         break;
1084     }
1085   return 0;
1086 }
1087
1088 static rtx
1089 find_base_term (x)
1090      register rtx x;
1091 {
1092   cselib_val *val;
1093   struct elt_loc_list *l;
1094
1095 #if defined (FIND_BASE_TERM)
1096   /* Try machine-dependent ways to find the base term.  */
1097   x = FIND_BASE_TERM (x);
1098 #endif
1099
1100   switch (GET_CODE (x))
1101     {
1102     case REG:
1103       return REG_BASE_VALUE (x);
1104
1105     case ZERO_EXTEND:
1106     case SIGN_EXTEND:   /* Used for Alpha/NT pointers */
1107     case HIGH:
1108     case PRE_INC:
1109     case PRE_DEC:
1110     case POST_INC:
1111     case POST_DEC:
1112       return find_base_term (XEXP (x, 0));
1113
1114     case VALUE:
1115       val = CSELIB_VAL_PTR (x);
1116       for (l = val->locs; l; l = l->next)
1117         if ((x = find_base_term (l->loc)) != 0)
1118           return x;
1119       return 0;
1120
1121     case CONST:
1122       x = XEXP (x, 0);
1123       if (GET_CODE (x) != PLUS && GET_CODE (x) != MINUS)
1124         return 0;
1125       /* fall through */
1126     case LO_SUM:
1127     case PLUS:
1128     case MINUS:
1129       {
1130         rtx tmp1 = XEXP (x, 0);
1131         rtx tmp2 = XEXP (x, 1);
1132
1133         /* This is a litle bit tricky since we have to determine which of
1134            the two operands represents the real base address.  Otherwise this
1135            routine may return the index register instead of the base register.
1136
1137            That may cause us to believe no aliasing was possible, when in
1138            fact aliasing is possible.
1139
1140            We use a few simple tests to guess the base register.  Additional
1141            tests can certainly be added.  For example, if one of the operands
1142            is a shift or multiply, then it must be the index register and the
1143            other operand is the base register.  */
1144         
1145         if (tmp1 == pic_offset_table_rtx && CONSTANT_P (tmp2))
1146           return find_base_term (tmp2);
1147
1148         /* If either operand is known to be a pointer, then use it
1149            to determine the base term.  */
1150         if (REG_P (tmp1) && REG_POINTER (tmp1))
1151           return find_base_term (tmp1);
1152
1153         if (REG_P (tmp2) && REG_POINTER (tmp2))
1154           return find_base_term (tmp2);
1155
1156         /* Neither operand was known to be a pointer.  Go ahead and find the
1157            base term for both operands.  */
1158         tmp1 = find_base_term (tmp1);
1159         tmp2 = find_base_term (tmp2);
1160
1161         /* If either base term is named object or a special address
1162            (like an argument or stack reference), then use it for the
1163            base term.  */
1164         if (tmp1 != 0
1165             && (GET_CODE (tmp1) == SYMBOL_REF
1166                 || GET_CODE (tmp1) == LABEL_REF
1167                 || (GET_CODE (tmp1) == ADDRESS
1168                     && GET_MODE (tmp1) != VOIDmode)))
1169           return tmp1;
1170
1171         if (tmp2 != 0
1172             && (GET_CODE (tmp2) == SYMBOL_REF
1173                 || GET_CODE (tmp2) == LABEL_REF
1174                 || (GET_CODE (tmp2) == ADDRESS
1175                     && GET_MODE (tmp2) != VOIDmode)))
1176           return tmp2;
1177
1178         /* We could not determine which of the two operands was the
1179            base register and which was the index.  So we can determine
1180            nothing from the base alias check.  */
1181         return 0;
1182       }
1183
1184     case AND:
1185       if (GET_CODE (XEXP (x, 0)) == REG && GET_CODE (XEXP (x, 1)) == CONST_INT)
1186         return REG_BASE_VALUE (XEXP (x, 0));
1187       return 0;
1188
1189     case SYMBOL_REF:
1190     case LABEL_REF:
1191       return x;
1192
1193     case ADDRESSOF:
1194       return REG_BASE_VALUE (frame_pointer_rtx);
1195
1196     default:
1197       return 0;
1198     }
1199 }
1200
1201 /* Return 0 if the addresses X and Y are known to point to different
1202    objects, 1 if they might be pointers to the same object.  */
1203
1204 static int
1205 base_alias_check (x, y, x_mode, y_mode)
1206      rtx x, y;
1207      enum machine_mode x_mode, y_mode;
1208 {
1209   rtx x_base = find_base_term (x);
1210   rtx y_base = find_base_term (y);
1211
1212   /* If the address itself has no known base see if a known equivalent
1213      value has one.  If either address still has no known base, nothing
1214      is known about aliasing.  */
1215   if (x_base == 0)
1216     {
1217       rtx x_c;
1218
1219       if (! flag_expensive_optimizations || (x_c = canon_rtx (x)) == x)
1220         return 1;
1221
1222       x_base = find_base_term (x_c);
1223       if (x_base == 0)
1224         return 1;
1225     }
1226
1227   if (y_base == 0)
1228     {
1229       rtx y_c;
1230       if (! flag_expensive_optimizations || (y_c = canon_rtx (y)) == y)
1231         return 1;
1232
1233       y_base = find_base_term (y_c);
1234       if (y_base == 0)
1235         return 1;
1236     }
1237
1238   /* If the base addresses are equal nothing is known about aliasing.  */
1239   if (rtx_equal_p (x_base, y_base))
1240     return 1;
1241
1242   /* The base addresses of the read and write are different expressions. 
1243      If they are both symbols and they are not accessed via AND, there is
1244      no conflict.  We can bring knowledge of object alignment into play
1245      here.  For example, on alpha, "char a, b;" can alias one another,
1246      though "char a; long b;" cannot.  */
1247   if (GET_CODE (x_base) != ADDRESS && GET_CODE (y_base) != ADDRESS)
1248     {
1249       if (GET_CODE (x) == AND && GET_CODE (y) == AND)
1250         return 1;
1251       if (GET_CODE (x) == AND
1252           && (GET_CODE (XEXP (x, 1)) != CONST_INT
1253               || GET_MODE_UNIT_SIZE (y_mode) < -INTVAL (XEXP (x, 1))))
1254         return 1;
1255       if (GET_CODE (y) == AND
1256           && (GET_CODE (XEXP (y, 1)) != CONST_INT
1257               || GET_MODE_UNIT_SIZE (x_mode) < -INTVAL (XEXP (y, 1))))
1258         return 1;
1259       /* Differing symbols never alias.  */
1260       return 0;
1261     }
1262
1263   /* If one address is a stack reference there can be no alias:
1264      stack references using different base registers do not alias,
1265      a stack reference can not alias a parameter, and a stack reference
1266      can not alias a global.  */
1267   if ((GET_CODE (x_base) == ADDRESS && GET_MODE (x_base) == Pmode)
1268       || (GET_CODE (y_base) == ADDRESS && GET_MODE (y_base) == Pmode))
1269     return 0;
1270
1271   if (! flag_argument_noalias)
1272     return 1;
1273
1274   if (flag_argument_noalias > 1)
1275     return 0;
1276
1277   /* Weak noalias assertion (arguments are distinct, but may match globals). */
1278   return ! (GET_MODE (x_base) == VOIDmode && GET_MODE (y_base) == VOIDmode);
1279 }
1280
1281 /* Convert the address X into something we can use.  This is done by returning
1282    it unchanged unless it is a value; in the latter case we call cselib to get
1283    a more useful rtx.  */
1284
1285 static rtx
1286 get_addr (x)
1287      rtx x;
1288 {
1289   cselib_val *v;
1290   struct elt_loc_list *l;
1291
1292   if (GET_CODE (x) != VALUE)
1293     return x;
1294   v = CSELIB_VAL_PTR (x);
1295   for (l = v->locs; l; l = l->next)
1296     if (CONSTANT_P (l->loc))
1297       return l->loc;
1298   for (l = v->locs; l; l = l->next)
1299     if (GET_CODE (l->loc) != REG && GET_CODE (l->loc) != MEM)
1300       return l->loc;
1301   if (v->locs)
1302     return v->locs->loc;
1303   return x;
1304 }
1305
1306 /*  Return the address of the (N_REFS + 1)th memory reference to ADDR
1307     where SIZE is the size in bytes of the memory reference.  If ADDR
1308     is not modified by the memory reference then ADDR is returned.  */
1309
1310 rtx
1311 addr_side_effect_eval (addr, size, n_refs)
1312      rtx addr;
1313      int size;
1314      int n_refs;
1315 {
1316   int offset = 0;
1317   
1318   switch (GET_CODE (addr))
1319     {
1320     case PRE_INC:
1321       offset = (n_refs + 1) * size;
1322       break;
1323     case PRE_DEC:
1324       offset = -(n_refs + 1) * size;
1325       break;
1326     case POST_INC:
1327       offset = n_refs * size;
1328       break;
1329     case POST_DEC:
1330       offset = -n_refs * size;
1331       break;
1332
1333     default:
1334       return addr;
1335     }
1336   
1337   if (offset)
1338     addr = gen_rtx_PLUS (GET_MODE (addr), XEXP (addr, 0), GEN_INT (offset));
1339   else
1340     addr = XEXP (addr, 0);
1341
1342   return addr;
1343 }
1344
1345 /* Return nonzero if X and Y (memory addresses) could reference the
1346    same location in memory.  C is an offset accumulator.  When
1347    C is nonzero, we are testing aliases between X and Y + C.
1348    XSIZE is the size in bytes of the X reference,
1349    similarly YSIZE is the size in bytes for Y.
1350
1351    If XSIZE or YSIZE is zero, we do not know the amount of memory being
1352    referenced (the reference was BLKmode), so make the most pessimistic
1353    assumptions.
1354
1355    If XSIZE or YSIZE is negative, we may access memory outside the object
1356    being referenced as a side effect.  This can happen when using AND to
1357    align memory references, as is done on the Alpha.
1358
1359    Nice to notice that varying addresses cannot conflict with fp if no
1360    local variables had their addresses taken, but that's too hard now.  */
1361
1362 static int
1363 memrefs_conflict_p (xsize, x, ysize, y, c)
1364      register rtx x, y;
1365      int xsize, ysize;
1366      HOST_WIDE_INT c;
1367 {
1368   if (GET_CODE (x) == VALUE)
1369     x = get_addr (x);
1370   if (GET_CODE (y) == VALUE)
1371     y = get_addr (y);
1372   if (GET_CODE (x) == HIGH)
1373     x = XEXP (x, 0);
1374   else if (GET_CODE (x) == LO_SUM)
1375     x = XEXP (x, 1);
1376   else
1377     x = canon_rtx (addr_side_effect_eval (x, xsize, 0));
1378   if (GET_CODE (y) == HIGH)
1379     y = XEXP (y, 0);
1380   else if (GET_CODE (y) == LO_SUM)
1381     y = XEXP (y, 1);
1382   else
1383     y = canon_rtx (addr_side_effect_eval (y, ysize, 0));
1384
1385   if (rtx_equal_for_memref_p (x, y))
1386     {
1387       if (xsize <= 0 || ysize <= 0)
1388         return 1;
1389       if (c >= 0 && xsize > c)
1390         return 1;
1391       if (c < 0 && ysize+c > 0)
1392         return 1;
1393       return 0;
1394     }
1395
1396   /* This code used to check for conflicts involving stack references and
1397      globals but the base address alias code now handles these cases.  */
1398
1399   if (GET_CODE (x) == PLUS)
1400     {
1401       /* The fact that X is canonicalized means that this
1402          PLUS rtx is canonicalized.  */
1403       rtx x0 = XEXP (x, 0);
1404       rtx x1 = XEXP (x, 1);
1405
1406       if (GET_CODE (y) == PLUS)
1407         {
1408           /* The fact that Y is canonicalized means that this
1409              PLUS rtx is canonicalized.  */
1410           rtx y0 = XEXP (y, 0);
1411           rtx y1 = XEXP (y, 1);
1412
1413           if (rtx_equal_for_memref_p (x1, y1))
1414             return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1415           if (rtx_equal_for_memref_p (x0, y0))
1416             return memrefs_conflict_p (xsize, x1, ysize, y1, c);
1417           if (GET_CODE (x1) == CONST_INT)
1418             {
1419               if (GET_CODE (y1) == CONST_INT)
1420                 return memrefs_conflict_p (xsize, x0, ysize, y0,
1421                                            c - INTVAL (x1) + INTVAL (y1));
1422               else
1423                 return memrefs_conflict_p (xsize, x0, ysize, y,
1424                                            c - INTVAL (x1));
1425             }
1426           else if (GET_CODE (y1) == CONST_INT)
1427             return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1428
1429           return 1;
1430         }
1431       else if (GET_CODE (x1) == CONST_INT)
1432         return memrefs_conflict_p (xsize, x0, ysize, y, c - INTVAL (x1));
1433     }
1434   else if (GET_CODE (y) == PLUS)
1435     {
1436       /* The fact that Y is canonicalized means that this
1437          PLUS rtx is canonicalized.  */
1438       rtx y0 = XEXP (y, 0);
1439       rtx y1 = XEXP (y, 1);
1440
1441       if (GET_CODE (y1) == CONST_INT)
1442         return memrefs_conflict_p (xsize, x, ysize, y0, c + INTVAL (y1));
1443       else
1444         return 1;
1445     }
1446
1447   if (GET_CODE (x) == GET_CODE (y))
1448     switch (GET_CODE (x))
1449       {
1450       case MULT:
1451         {
1452           /* Handle cases where we expect the second operands to be the
1453              same, and check only whether the first operand would conflict
1454              or not.  */
1455           rtx x0, y0;
1456           rtx x1 = canon_rtx (XEXP (x, 1));
1457           rtx y1 = canon_rtx (XEXP (y, 1));
1458           if (! rtx_equal_for_memref_p (x1, y1))
1459             return 1;
1460           x0 = canon_rtx (XEXP (x, 0));
1461           y0 = canon_rtx (XEXP (y, 0));
1462           if (rtx_equal_for_memref_p (x0, y0))
1463             return (xsize == 0 || ysize == 0
1464                     || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1465
1466           /* Can't properly adjust our sizes.  */
1467           if (GET_CODE (x1) != CONST_INT)
1468             return 1;
1469           xsize /= INTVAL (x1);
1470           ysize /= INTVAL (x1);
1471           c /= INTVAL (x1);
1472           return memrefs_conflict_p (xsize, x0, ysize, y0, c);
1473         }
1474
1475       case REG:
1476         /* Are these registers known not to be equal?  */
1477         if (alias_invariant)
1478           {
1479             unsigned int r_x = REGNO (x), r_y = REGNO (y);
1480             rtx i_x, i_y;       /* invariant relationships of X and Y */
1481
1482             i_x = r_x >= reg_base_value_size ? 0 : alias_invariant[r_x];
1483             i_y = r_y >= reg_base_value_size ? 0 : alias_invariant[r_y];
1484
1485             if (i_x == 0 && i_y == 0)
1486               break;
1487
1488             if (! memrefs_conflict_p (xsize, i_x ? i_x : x,
1489                                       ysize, i_y ? i_y : y, c))
1490               return 0;
1491           }
1492         break;
1493
1494       default:
1495         break;
1496       }
1497
1498   /* Treat an access through an AND (e.g. a subword access on an Alpha)
1499      as an access with indeterminate size.  Assume that references 
1500      besides AND are aligned, so if the size of the other reference is
1501      at least as large as the alignment, assume no other overlap.  */
1502   if (GET_CODE (x) == AND && GET_CODE (XEXP (x, 1)) == CONST_INT)
1503     {
1504       if (GET_CODE (y) == AND || ysize < -INTVAL (XEXP (x, 1)))
1505         xsize = -1;
1506       return memrefs_conflict_p (xsize, XEXP (x, 0), ysize, y, c);
1507     }
1508   if (GET_CODE (y) == AND && GET_CODE (XEXP (y, 1)) == CONST_INT)
1509     {
1510       /* ??? If we are indexing far enough into the array/structure, we
1511          may yet be able to determine that we can not overlap.  But we 
1512          also need to that we are far enough from the end not to overlap
1513          a following reference, so we do nothing with that for now.  */
1514       if (GET_CODE (x) == AND || xsize < -INTVAL (XEXP (y, 1)))
1515         ysize = -1;
1516       return memrefs_conflict_p (xsize, x, ysize, XEXP (y, 0), c);
1517     }
1518
1519   if (GET_CODE (x) == ADDRESSOF)
1520     {
1521       if (y == frame_pointer_rtx
1522           || GET_CODE (y) == ADDRESSOF)
1523         return xsize <= 0 || ysize <= 0;
1524     }
1525   if (GET_CODE (y) == ADDRESSOF)
1526     {
1527       if (x == frame_pointer_rtx)
1528         return xsize <= 0 || ysize <= 0;
1529     }
1530
1531   if (CONSTANT_P (x))
1532     {
1533       if (GET_CODE (x) == CONST_INT && GET_CODE (y) == CONST_INT)
1534         {
1535           c += (INTVAL (y) - INTVAL (x));
1536           return (xsize <= 0 || ysize <= 0
1537                   || (c >= 0 && xsize > c) || (c < 0 && ysize+c > 0));
1538         }
1539
1540       if (GET_CODE (x) == CONST)
1541         {
1542           if (GET_CODE (y) == CONST)
1543             return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1544                                        ysize, canon_rtx (XEXP (y, 0)), c);
1545           else
1546             return memrefs_conflict_p (xsize, canon_rtx (XEXP (x, 0)),
1547                                        ysize, y, c);
1548         }
1549       if (GET_CODE (y) == CONST)
1550         return memrefs_conflict_p (xsize, x, ysize,
1551                                    canon_rtx (XEXP (y, 0)), c);
1552
1553       if (CONSTANT_P (y))
1554         return (xsize <= 0 || ysize <= 0
1555                 || (rtx_equal_for_memref_p (x, y)
1556                     && ((c >= 0 && xsize > c) || (c < 0 && ysize+c > 0))));
1557
1558       return 1;
1559     }
1560   return 1;
1561 }
1562
1563 /* Functions to compute memory dependencies.
1564
1565    Since we process the insns in execution order, we can build tables
1566    to keep track of what registers are fixed (and not aliased), what registers
1567    are varying in known ways, and what registers are varying in unknown
1568    ways.
1569
1570    If both memory references are volatile, then there must always be a
1571    dependence between the two references, since their order can not be
1572    changed.  A volatile and non-volatile reference can be interchanged
1573    though. 
1574
1575    A MEM_IN_STRUCT reference at a non-AND varying address can never
1576    conflict with a non-MEM_IN_STRUCT reference at a fixed address.  We
1577    also must allow AND addresses, because they may generate accesses
1578    outside the object being referenced.  This is used to generate
1579    aligned addresses from unaligned addresses, for instance, the alpha
1580    storeqi_unaligned pattern.  */
1581
1582 /* Read dependence: X is read after read in MEM takes place.  There can
1583    only be a dependence here if both reads are volatile.  */
1584
1585 int
1586 read_dependence (mem, x)
1587      rtx mem;
1588      rtx x;
1589 {
1590   return MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem);
1591 }
1592
1593 /* Returns MEM1 if and only if MEM1 is a scalar at a fixed address and
1594    MEM2 is a reference to a structure at a varying address, or returns
1595    MEM2 if vice versa.  Otherwise, returns NULL_RTX.  If a non-NULL
1596    value is returned MEM1 and MEM2 can never alias.  VARIES_P is used
1597    to decide whether or not an address may vary; it should return
1598    nonzero whenever variation is possible.
1599    MEM1_ADDR and MEM2_ADDR are the addresses of MEM1 and MEM2.  */
1600   
1601 static rtx
1602 fixed_scalar_and_varying_struct_p (mem1, mem2, mem1_addr, mem2_addr, varies_p)
1603      rtx mem1, mem2;
1604      rtx mem1_addr, mem2_addr;
1605      int (*varies_p) PARAMS ((rtx));
1606 {  
1607   if (! flag_strict_aliasing)
1608     return NULL_RTX;
1609
1610   if (MEM_SCALAR_P (mem1) && MEM_IN_STRUCT_P (mem2) 
1611       && !varies_p (mem1_addr) && varies_p (mem2_addr))
1612     /* MEM1 is a scalar at a fixed address; MEM2 is a struct at a
1613        varying address.  */
1614     return mem1;
1615
1616   if (MEM_IN_STRUCT_P (mem1) && MEM_SCALAR_P (mem2) 
1617       && varies_p (mem1_addr) && !varies_p (mem2_addr))
1618     /* MEM2 is a scalar at a fixed address; MEM1 is a struct at a
1619        varying address.  */
1620     return mem2;
1621
1622   return NULL_RTX;
1623 }
1624
1625 /* Returns nonzero if something about the mode or address format MEM1
1626    indicates that it might well alias *anything*.  */
1627
1628 static int
1629 aliases_everything_p (mem)
1630      rtx mem;
1631 {
1632   if (GET_CODE (XEXP (mem, 0)) == AND)
1633     /* If the address is an AND, its very hard to know at what it is
1634        actually pointing.  */
1635     return 1;
1636     
1637   return 0;
1638 }
1639
1640 /* True dependence: X is read after store in MEM takes place.  */
1641
1642 int
1643 true_dependence (mem, mem_mode, x, varies)
1644      rtx mem;
1645      enum machine_mode mem_mode;
1646      rtx x;
1647      int (*varies) PARAMS ((rtx));
1648 {
1649   register rtx x_addr, mem_addr;
1650   rtx base;
1651
1652   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1653     return 1;
1654
1655   if (DIFFERENT_ALIAS_SETS_P (x, mem))
1656     return 0;
1657
1658   /* Unchanging memory can't conflict with non-unchanging memory.
1659      A non-unchanging read can conflict with a non-unchanging write.
1660      An unchanging read can conflict with an unchanging write since
1661      there may be a single store to this address to initialize it.
1662      Note that an unchanging store can conflict with a non-unchanging read
1663      since we have to make conservative assumptions when we have a
1664      record with readonly fields and we are copying the whole thing.
1665      Just fall through to the code below to resolve potential conflicts.
1666      This won't handle all cases optimally, but the possible performance
1667      loss should be negligible.  */
1668   if (RTX_UNCHANGING_P (x) && ! RTX_UNCHANGING_P (mem))
1669     return 0;
1670
1671   if (mem_mode == VOIDmode)
1672     mem_mode = GET_MODE (mem);
1673
1674   x_addr = get_addr (XEXP (x, 0));
1675   mem_addr = get_addr (XEXP (mem, 0));
1676
1677   base = find_base_term (x_addr);
1678   if (base && (GET_CODE (base) == LABEL_REF
1679                || (GET_CODE (base) == SYMBOL_REF
1680                    && CONSTANT_POOL_ADDRESS_P (base))))
1681     return 0;
1682
1683   if (! base_alias_check (x_addr, mem_addr, GET_MODE (x), mem_mode))
1684     return 0;
1685
1686   x_addr = canon_rtx (x_addr);
1687   mem_addr = canon_rtx (mem_addr);
1688
1689   if (! memrefs_conflict_p (GET_MODE_SIZE (mem_mode), mem_addr,
1690                             SIZE_FOR_MODE (x), x_addr, 0))
1691     return 0;
1692
1693   if (aliases_everything_p (x))
1694     return 1;
1695
1696   /* We cannot use aliases_everyting_p to test MEM, since we must look
1697      at MEM_MODE, rather than GET_MODE (MEM).  */
1698   if (mem_mode == QImode || GET_CODE (mem_addr) == AND)
1699     return 1;
1700
1701   /* In true_dependence we also allow BLKmode to alias anything.  Why
1702      don't we do this in anti_dependence and output_dependence?  */
1703   if (mem_mode == BLKmode || GET_MODE (x) == BLKmode)
1704     return 1;
1705
1706   return ! fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
1707                                               varies);
1708 }
1709
1710 /* Returns non-zero if a write to X might alias a previous read from
1711    (or, if WRITEP is non-zero, a write to) MEM.  */
1712
1713 static int
1714 write_dependence_p (mem, x, writep)
1715      rtx mem;
1716      rtx x;
1717      int writep;
1718 {
1719   rtx x_addr, mem_addr;
1720   rtx fixed_scalar;
1721   rtx base;
1722
1723   if (MEM_VOLATILE_P (x) && MEM_VOLATILE_P (mem))
1724     return 1;
1725
1726   if (DIFFERENT_ALIAS_SETS_P (x, mem))
1727     return 0;
1728
1729   /* Unchanging memory can't conflict with non-unchanging memory.  */
1730   if (RTX_UNCHANGING_P (x) != RTX_UNCHANGING_P (mem))
1731     return 0;
1732
1733   /* If MEM is an unchanging read, then it can't possibly conflict with
1734      the store to X, because there is at most one store to MEM, and it must
1735      have occurred somewhere before MEM.  */
1736   if (! writep && RTX_UNCHANGING_P (mem))
1737     return 0;
1738
1739   x_addr = get_addr (XEXP (x, 0));
1740   mem_addr = get_addr (XEXP (mem, 0));
1741
1742   if (! writep)
1743     {
1744       base = find_base_term (mem_addr);
1745       if (base && (GET_CODE (base) == LABEL_REF
1746                    || (GET_CODE (base) == SYMBOL_REF
1747                        && CONSTANT_POOL_ADDRESS_P (base))))
1748         return 0;
1749     }
1750
1751   if (! base_alias_check (x_addr, mem_addr, GET_MODE (x),
1752                           GET_MODE (mem)))
1753     return 0;
1754
1755   x_addr = canon_rtx (x_addr);
1756   mem_addr = canon_rtx (mem_addr);
1757
1758   if (!memrefs_conflict_p (SIZE_FOR_MODE (mem), mem_addr,
1759                            SIZE_FOR_MODE (x), x_addr, 0))
1760     return 0;
1761
1762   fixed_scalar 
1763     = fixed_scalar_and_varying_struct_p (mem, x, mem_addr, x_addr,
1764                                          rtx_addr_varies_p);
1765
1766   return (!(fixed_scalar == mem && !aliases_everything_p (x))
1767           && !(fixed_scalar == x && !aliases_everything_p (mem)));
1768 }
1769
1770 /* Anti dependence: X is written after read in MEM takes place.  */
1771
1772 int
1773 anti_dependence (mem, x)
1774      rtx mem;
1775      rtx x;
1776 {
1777   return write_dependence_p (mem, x, /*writep=*/0);
1778 }
1779
1780 /* Output dependence: X is written after store in MEM takes place.  */
1781
1782 int
1783 output_dependence (mem, x)
1784      register rtx mem;
1785      register rtx x;
1786 {
1787   return write_dependence_p (mem, x, /*writep=*/1);
1788 }
1789
1790 /* Returns non-zero if X mentions something which is not
1791    local to the function and is not constant.  */
1792
1793 static int
1794 nonlocal_mentioned_p (x)
1795      rtx x;
1796 {
1797   rtx base;
1798   register RTX_CODE code;
1799   int regno;
1800
1801   code = GET_CODE (x);
1802
1803   if (GET_RTX_CLASS (code) == 'i')
1804     {
1805       /* Constant functions can be constant if they don't use
1806          scratch memory used to mark function w/o side effects.  */
1807       if (code == CALL_INSN && CONST_CALL_P (x))
1808         {
1809           x = CALL_INSN_FUNCTION_USAGE (x);
1810           if (x == 0)
1811             return 0;
1812         }
1813       else
1814         x = PATTERN (x);
1815       code = GET_CODE (x);
1816     }
1817
1818   switch (code)
1819     {
1820     case SUBREG:
1821       if (GET_CODE (SUBREG_REG (x)) == REG)
1822         {
1823           /* Global registers are not local.  */
1824           if (REGNO (SUBREG_REG (x)) < FIRST_PSEUDO_REGISTER
1825               && global_regs[REGNO (SUBREG_REG (x)) + SUBREG_WORD (x)])
1826             return 1;
1827           return 0;
1828         }
1829       break;
1830
1831     case REG:
1832       regno = REGNO (x);
1833       /* Global registers are not local.  */
1834       if (regno < FIRST_PSEUDO_REGISTER && global_regs[regno])
1835         return 1;
1836       return 0;
1837
1838     case SCRATCH:
1839     case PC:
1840     case CC0:
1841     case CONST_INT:
1842     case CONST_DOUBLE:
1843     case CONST:
1844     case LABEL_REF:
1845       return 0;
1846
1847     case SYMBOL_REF:
1848       /* Constants in the function's constants pool are constant.  */
1849       if (CONSTANT_POOL_ADDRESS_P (x))
1850         return 0;
1851       return 1;
1852
1853     case CALL:
1854       /* Non-constant calls and recursion are not local.  */
1855       return 1;
1856
1857     case MEM:
1858       /* Be overly conservative and consider any volatile memory
1859          reference as not local.  */
1860       if (MEM_VOLATILE_P (x))
1861         return 1;
1862       base = find_base_term (XEXP (x, 0));
1863       if (base)
1864         {
1865           /* A Pmode ADDRESS could be a reference via the structure value
1866              address or static chain.  Such memory references are nonlocal.
1867
1868              Thus, we have to examine the contents of the ADDRESS to find
1869              out if this is a local reference or not.  */
1870           if (GET_CODE (base) == ADDRESS
1871               && GET_MODE (base) == Pmode
1872               && (XEXP (base, 0) == stack_pointer_rtx
1873                   || XEXP (base, 0) == arg_pointer_rtx
1874 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
1875                   || XEXP (base, 0) == hard_frame_pointer_rtx
1876 #endif
1877                   || XEXP (base, 0) == frame_pointer_rtx))
1878             return 0;
1879           /* Constants in the function's constant pool are constant.  */
1880           if (GET_CODE (base) == SYMBOL_REF && CONSTANT_POOL_ADDRESS_P (base))
1881             return 0;
1882         }
1883       return 1;
1884
1885     case UNSPEC_VOLATILE:
1886     case ASM_INPUT:
1887       return 1;
1888
1889     case ASM_OPERANDS:
1890       if (MEM_VOLATILE_P (x))
1891         return 1;
1892
1893     /* FALLTHROUGH */
1894
1895     default:
1896       break;
1897     }
1898
1899   /* Recursively scan the operands of this expression.  */
1900
1901   {
1902     register const char *fmt = GET_RTX_FORMAT (code);
1903     register int i;
1904     
1905     for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1906       {
1907         if (fmt[i] == 'e' && XEXP (x, i))
1908           {
1909             if (nonlocal_mentioned_p (XEXP (x, i)))
1910               return 1;
1911           }
1912         else if (fmt[i] == 'E')
1913           {
1914             register int j;
1915             for (j = 0; j < XVECLEN (x, i); j++)
1916               if (nonlocal_mentioned_p (XVECEXP (x, i, j)))
1917                 return 1;
1918           }
1919       }
1920   }
1921
1922   return 0;
1923 }
1924
1925 /* Return non-zero if a loop (natural or otherwise) is present.
1926    Inspired by Depth_First_Search_PP described in:
1927
1928      Advanced Compiler Design and Implementation
1929      Steven Muchnick
1930      Morgan Kaufmann, 1997
1931
1932    and heavily borrowed from flow_depth_first_order_compute.  */
1933
1934 static int
1935 loop_p ()
1936 {
1937   edge *stack;
1938   int *pre;
1939   int *post;
1940   int sp;
1941   int prenum = 1;
1942   int postnum = 1;
1943   sbitmap visited;
1944
1945   /* Allocate the preorder and postorder number arrays.  */
1946   pre = (int *) xcalloc (n_basic_blocks, sizeof (int));
1947   post = (int *) xcalloc (n_basic_blocks, sizeof (int));
1948
1949   /* Allocate stack for back-tracking up CFG.  */
1950   stack = (edge *) xmalloc ((n_basic_blocks + 1) * sizeof (edge));
1951   sp = 0;
1952
1953   /* Allocate bitmap to track nodes that have been visited.  */
1954   visited = sbitmap_alloc (n_basic_blocks);
1955
1956   /* None of the nodes in the CFG have been visited yet.  */
1957   sbitmap_zero (visited);
1958
1959   /* Push the first edge on to the stack.  */
1960   stack[sp++] = ENTRY_BLOCK_PTR->succ;
1961
1962   while (sp)
1963     {
1964       edge e;
1965       basic_block src;
1966       basic_block dest;
1967
1968       /* Look at the edge on the top of the stack.  */
1969       e = stack[sp - 1];
1970       src = e->src;
1971       dest = e->dest;
1972
1973       /* Check if the edge destination has been visited yet.  */
1974       if (dest != EXIT_BLOCK_PTR && ! TEST_BIT (visited, dest->index))
1975         {
1976           /* Mark that we have visited the destination.  */
1977           SET_BIT (visited, dest->index);
1978
1979           pre[dest->index] = prenum++;
1980
1981           if (dest->succ)
1982             {
1983               /* Since the DEST node has been visited for the first
1984                  time, check its successors.  */
1985               stack[sp++] = dest->succ;
1986             }
1987           else
1988             post[dest->index] = postnum++;
1989         }
1990       else
1991         {
1992           if (dest != EXIT_BLOCK_PTR
1993               && pre[src->index] >= pre[dest->index]
1994               && post[dest->index] == 0)
1995             break;
1996
1997           if (! e->succ_next && src != ENTRY_BLOCK_PTR)
1998             post[src->index] = postnum++;
1999
2000           if (e->succ_next)
2001             stack[sp - 1] = e->succ_next;
2002           else
2003             sp--;
2004         }
2005     }
2006
2007   free (pre);
2008   free (post);
2009   free (stack);
2010   sbitmap_free (visited);
2011
2012   return sp;
2013 }
2014
2015 /* Mark the function if it is constant.  */
2016
2017 void
2018 mark_constant_function ()
2019 {
2020   rtx insn;
2021   int nonlocal_mentioned;
2022
2023   if (TREE_PUBLIC (current_function_decl)
2024       || TREE_READONLY (current_function_decl)
2025       || DECL_IS_PURE (current_function_decl)
2026       || TREE_THIS_VOLATILE (current_function_decl)
2027       || TYPE_MODE (TREE_TYPE (current_function_decl)) == VOIDmode)
2028     return;
2029
2030   /* A loop might not return which counts as a side effect.  */
2031   if (loop_p ())
2032     return;
2033
2034   nonlocal_mentioned = 0;
2035
2036   init_alias_analysis ();
2037
2038   /* Determine if this is a constant function.  */
2039
2040   for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2041     if (INSN_P (insn) && nonlocal_mentioned_p (insn))
2042       {
2043         nonlocal_mentioned = 1;
2044         break;
2045       }
2046
2047   end_alias_analysis ();
2048
2049   /* Mark the function.  */
2050
2051   if (! nonlocal_mentioned)
2052     TREE_READONLY (current_function_decl) = 1;
2053 }
2054
2055
2056 static HARD_REG_SET argument_registers;
2057
2058 void
2059 init_alias_once ()
2060 {
2061   register int i;
2062
2063 #ifndef OUTGOING_REGNO
2064 #define OUTGOING_REGNO(N) N
2065 #endif
2066   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2067     /* Check whether this register can hold an incoming pointer
2068        argument.  FUNCTION_ARG_REGNO_P tests outgoing register
2069        numbers, so translate if necessary due to register windows. */
2070     if (FUNCTION_ARG_REGNO_P (OUTGOING_REGNO (i))
2071         && HARD_REGNO_MODE_OK (i, Pmode))
2072       SET_HARD_REG_BIT (argument_registers, i);
2073
2074   alias_sets = splay_tree_new (splay_tree_compare_ints, 0, 0);
2075 }
2076
2077 /* Initialize the aliasing machinery.  Initialize the REG_KNOWN_VALUE
2078    array.  */
2079
2080 void
2081 init_alias_analysis ()
2082 {
2083   int maxreg = max_reg_num ();
2084   int changed, pass;
2085   register int i;
2086   register unsigned int ui;
2087   register rtx insn;
2088
2089   reg_known_value_size = maxreg;
2090
2091   reg_known_value 
2092     = (rtx *) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (rtx))
2093     - FIRST_PSEUDO_REGISTER;
2094   reg_known_equiv_p 
2095     = (char*) xcalloc ((maxreg - FIRST_PSEUDO_REGISTER), sizeof (char))
2096     - FIRST_PSEUDO_REGISTER;
2097
2098   /* Overallocate reg_base_value to allow some growth during loop
2099      optimization.  Loop unrolling can create a large number of
2100      registers.  */
2101   reg_base_value_size = maxreg * 2;
2102   reg_base_value = (rtx *) xcalloc (reg_base_value_size, sizeof (rtx));
2103   ggc_add_rtx_root (reg_base_value, reg_base_value_size);
2104
2105   new_reg_base_value = (rtx *) xmalloc (reg_base_value_size * sizeof (rtx));
2106   reg_seen = (char *) xmalloc (reg_base_value_size);
2107   if (! reload_completed && flag_unroll_loops)
2108     {
2109       /* ??? Why are we realloc'ing if we're just going to zero it?  */
2110       alias_invariant = (rtx *)xrealloc (alias_invariant,
2111                                          reg_base_value_size * sizeof (rtx));
2112       memset ((char *)alias_invariant, 0, reg_base_value_size * sizeof (rtx));
2113     }
2114     
2115
2116   /* The basic idea is that each pass through this loop will use the
2117      "constant" information from the previous pass to propagate alias
2118      information through another level of assignments.
2119
2120      This could get expensive if the assignment chains are long.  Maybe
2121      we should throttle the number of iterations, possibly based on
2122      the optimization level or flag_expensive_optimizations.
2123
2124      We could propagate more information in the first pass by making use
2125      of REG_N_SETS to determine immediately that the alias information
2126      for a pseudo is "constant".
2127
2128      A program with an uninitialized variable can cause an infinite loop
2129      here.  Instead of doing a full dataflow analysis to detect such problems
2130      we just cap the number of iterations for the loop.
2131
2132      The state of the arrays for the set chain in question does not matter
2133      since the program has undefined behavior.  */
2134
2135   pass = 0;
2136   do
2137     {
2138       /* Assume nothing will change this iteration of the loop.  */
2139       changed = 0;
2140
2141       /* We want to assign the same IDs each iteration of this loop, so
2142          start counting from zero each iteration of the loop.  */
2143       unique_id = 0;
2144
2145       /* We're at the start of the funtion each iteration through the
2146          loop, so we're copying arguments.  */
2147       copying_arguments = 1;
2148
2149       /* Wipe the potential alias information clean for this pass.  */
2150       memset ((char *) new_reg_base_value, 0, reg_base_value_size * sizeof (rtx));
2151
2152       /* Wipe the reg_seen array clean.  */
2153       memset ((char *) reg_seen, 0, reg_base_value_size);
2154
2155       /* Mark all hard registers which may contain an address.
2156          The stack, frame and argument pointers may contain an address.
2157          An argument register which can hold a Pmode value may contain
2158          an address even if it is not in BASE_REGS.
2159
2160          The address expression is VOIDmode for an argument and
2161          Pmode for other registers.  */
2162
2163       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2164         if (TEST_HARD_REG_BIT (argument_registers, i))
2165           new_reg_base_value[i] = gen_rtx_ADDRESS (VOIDmode,
2166                                                    gen_rtx_REG (Pmode, i));
2167
2168       new_reg_base_value[STACK_POINTER_REGNUM]
2169         = gen_rtx_ADDRESS (Pmode, stack_pointer_rtx);
2170       new_reg_base_value[ARG_POINTER_REGNUM]
2171         = gen_rtx_ADDRESS (Pmode, arg_pointer_rtx);
2172       new_reg_base_value[FRAME_POINTER_REGNUM]
2173         = gen_rtx_ADDRESS (Pmode, frame_pointer_rtx);
2174 #if HARD_FRAME_POINTER_REGNUM != FRAME_POINTER_REGNUM
2175       new_reg_base_value[HARD_FRAME_POINTER_REGNUM]
2176         = gen_rtx_ADDRESS (Pmode, hard_frame_pointer_rtx);
2177 #endif
2178
2179       /* Walk the insns adding values to the new_reg_base_value array.  */
2180       for (insn = get_insns (); insn; insn = NEXT_INSN (insn))
2181         {
2182           if (INSN_P (insn))
2183             {
2184               rtx note, set;
2185
2186 #if defined (HAVE_prologue) || defined (HAVE_epilogue)
2187               /* The prologue/epilouge insns are not threaded onto the
2188                  insn chain until after reload has completed.  Thus,
2189                  there is no sense wasting time checking if INSN is in
2190                  the prologue/epilogue until after reload has completed.  */
2191               if (reload_completed
2192                   && prologue_epilogue_contains (insn))
2193                 continue;
2194 #endif
2195
2196               /* If this insn has a noalias note, process it,  Otherwise,
2197                  scan for sets.  A simple set will have no side effects
2198                  which could change the base value of any other register. */
2199
2200               if (GET_CODE (PATTERN (insn)) == SET
2201                   && REG_NOTES (insn) != 0
2202                   && find_reg_note (insn, REG_NOALIAS, NULL_RTX))
2203                 record_set (SET_DEST (PATTERN (insn)), NULL_RTX, NULL);
2204               else
2205                 note_stores (PATTERN (insn), record_set, NULL);
2206
2207               set = single_set (insn);
2208
2209               if (set != 0
2210                   && GET_CODE (SET_DEST (set)) == REG
2211                   && REGNO (SET_DEST (set)) >= FIRST_PSEUDO_REGISTER
2212                   && REG_NOTES (insn) != 0
2213                   && (((note = find_reg_note (insn, REG_EQUAL, 0)) != 0
2214                        && REG_N_SETS (REGNO (SET_DEST (set))) == 1)
2215                       || (note = find_reg_note (insn, REG_EQUIV, NULL_RTX)) != 0)
2216                   && GET_CODE (XEXP (note, 0)) != EXPR_LIST
2217                   && ! reg_overlap_mentioned_p (SET_DEST (set), XEXP (note, 0)))
2218                 {
2219                   int regno = REGNO (SET_DEST (set));
2220                   reg_known_value[regno] = XEXP (note, 0);
2221                   reg_known_equiv_p[regno] = REG_NOTE_KIND (note) == REG_EQUIV;
2222                 }
2223             }
2224           else if (GET_CODE (insn) == NOTE
2225                    && NOTE_LINE_NUMBER (insn) == NOTE_INSN_FUNCTION_BEG)
2226             copying_arguments = 0;
2227         }
2228
2229       /* Now propagate values from new_reg_base_value to reg_base_value.  */
2230       for (ui = 0; ui < reg_base_value_size; ui++)
2231         {
2232           if (new_reg_base_value[ui]
2233               && new_reg_base_value[ui] != reg_base_value[ui]
2234               && ! rtx_equal_p (new_reg_base_value[ui], reg_base_value[ui]))
2235             {
2236               reg_base_value[ui] = new_reg_base_value[ui];
2237               changed = 1;
2238             }
2239         }
2240     }
2241   while (changed && ++pass < MAX_ALIAS_LOOP_PASSES);
2242
2243   /* Fill in the remaining entries.  */
2244   for (i = FIRST_PSEUDO_REGISTER; i < maxreg; i++)
2245     if (reg_known_value[i] == 0)
2246       reg_known_value[i] = regno_reg_rtx[i];
2247
2248   /* Simplify the reg_base_value array so that no register refers to
2249      another register, except to special registers indirectly through
2250      ADDRESS expressions.
2251
2252      In theory this loop can take as long as O(registers^2), but unless
2253      there are very long dependency chains it will run in close to linear
2254      time.
2255
2256      This loop may not be needed any longer now that the main loop does
2257      a better job at propagating alias information.  */
2258   pass = 0;
2259   do
2260     {
2261       changed = 0;
2262       pass++;
2263       for (ui = 0; ui < reg_base_value_size; ui++)
2264         {
2265           rtx base = reg_base_value[ui];
2266           if (base && GET_CODE (base) == REG)
2267             {
2268               unsigned int base_regno = REGNO (base);
2269               if (base_regno == ui)             /* register set from itself */
2270                 reg_base_value[ui] = 0;
2271               else
2272                 reg_base_value[ui] = reg_base_value[base_regno];
2273               changed = 1;
2274             }
2275         }
2276     }
2277   while (changed && pass < MAX_ALIAS_LOOP_PASSES);
2278
2279   /* Clean up.  */
2280   free (new_reg_base_value);
2281   new_reg_base_value = 0;
2282   free (reg_seen);
2283   reg_seen = 0;
2284 }
2285
2286 void
2287 end_alias_analysis ()
2288 {
2289   free (reg_known_value + FIRST_PSEUDO_REGISTER);
2290   reg_known_value = 0;
2291   reg_known_value_size = 0;
2292   free (reg_known_equiv_p + FIRST_PSEUDO_REGISTER);
2293   reg_known_equiv_p = 0;
2294   if (reg_base_value)
2295     {
2296       ggc_del_root (reg_base_value);
2297       free (reg_base_value);
2298       reg_base_value = 0;
2299     }
2300   reg_base_value_size = 0;
2301   if (alias_invariant)
2302     {
2303       free (alias_invariant);
2304       alias_invariant = 0;
2305     }
2306 }