OSDN Git Service

PR debug/45882
[pf3gnuchains/gcc-fork.git] / gcc / cselib.c
1 /* Common subexpression elimination library for GNU compiler.
2    Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998,
3    1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4    Free Software Foundation, Inc.
5
6 This file is part of GCC.
7
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
12
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
16 for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26
27 #include "rtl.h"
28 #include "tm_p.h"
29 #include "regs.h"
30 #include "hard-reg-set.h"
31 #include "flags.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "function.h"
35 #include "emit-rtl.h"
36 #include "diagnostic-core.h"
37 #include "output.h"
38 #include "ggc.h"
39 #include "hashtab.h"
40 #include "tree-pass.h"
41 #include "cselib.h"
42 #include "params.h"
43 #include "alloc-pool.h"
44 #include "target.h"
45 #include "bitmap.h"
46
47 /* A list of cselib_val structures.  */
48 struct elt_list {
49     struct elt_list *next;
50     cselib_val *elt;
51 };
52
53 static bool cselib_record_memory;
54 static bool cselib_preserve_constants;
55 static int entry_and_rtx_equal_p (const void *, const void *);
56 static hashval_t get_value_hash (const void *);
57 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
58 static struct elt_loc_list *new_elt_loc_list (struct elt_loc_list *, rtx);
59 static void unchain_one_value (cselib_val *);
60 static void unchain_one_elt_list (struct elt_list **);
61 static void unchain_one_elt_loc_list (struct elt_loc_list **);
62 static int discard_useless_locs (void **, void *);
63 static int discard_useless_values (void **, void *);
64 static void remove_useless_values (void);
65 static int rtx_equal_for_cselib_1 (rtx, rtx, enum machine_mode);
66 static unsigned int cselib_hash_rtx (rtx, int, enum machine_mode);
67 static cselib_val *new_cselib_val (unsigned int, enum machine_mode, rtx);
68 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
69 static cselib_val *cselib_lookup_mem (rtx, int);
70 static void cselib_invalidate_regno (unsigned int, enum machine_mode);
71 static void cselib_invalidate_mem (rtx);
72 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
73 static void cselib_record_sets (rtx);
74
75 struct expand_value_data
76 {
77   bitmap regs_active;
78   cselib_expand_callback callback;
79   void *callback_arg;
80   bool dummy;
81 };
82
83 static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
84
85 /* There are three ways in which cselib can look up an rtx:
86    - for a REG, the reg_values table (which is indexed by regno) is used
87    - for a MEM, we recursively look up its address and then follow the
88      addr_list of that value
89    - for everything else, we compute a hash value and go through the hash
90      table.  Since different rtx's can still have the same hash value,
91      this involves walking the table entries for a given value and comparing
92      the locations of the entries with the rtx we are looking up.  */
93
94 /* A table that enables us to look up elts by their value.  */
95 static htab_t cselib_hash_table;
96
97 /* This is a global so we don't have to pass this through every function.
98    It is used in new_elt_loc_list to set SETTING_INSN.  */
99 static rtx cselib_current_insn;
100
101 /* The unique id that the next create value will take.  */
102 static unsigned int next_uid;
103
104 /* The number of registers we had when the varrays were last resized.  */
105 static unsigned int cselib_nregs;
106
107 /* Count values without known locations, or with only locations that
108    wouldn't have been known except for debug insns.  Whenever this
109    grows too big, we remove these useless values from the table.
110
111    Counting values with only debug values is a bit tricky.  We don't
112    want to increment n_useless_values when we create a value for a
113    debug insn, for this would get n_useless_values out of sync, but we
114    want increment it if all locs in the list that were ever referenced
115    in nondebug insns are removed from the list.
116
117    In the general case, once we do that, we'd have to stop accepting
118    nondebug expressions in the loc list, to avoid having two values
119    equivalent that, without debug insns, would have been made into
120    separate values.  However, because debug insns never introduce
121    equivalences themselves (no assignments), the only means for
122    growing loc lists is through nondebug assignments.  If the locs
123    also happen to be referenced in debug insns, it will work just fine.
124
125    A consequence of this is that there's at most one debug-only loc in
126    each loc list.  If we keep it in the first entry, testing whether
127    we have a debug-only loc list takes O(1).
128
129    Furthermore, since any additional entry in a loc list containing a
130    debug loc would have to come from an assignment (nondebug) that
131    references both the initial debug loc and the newly-equivalent loc,
132    the initial debug loc would be promoted to a nondebug loc, and the
133    loc list would not contain debug locs any more.
134
135    So the only case we have to be careful with in order to keep
136    n_useless_values in sync between debug and nondebug compilations is
137    to avoid incrementing n_useless_values when removing the single loc
138    from a value that turns out to not appear outside debug values.  We
139    increment n_useless_debug_values instead, and leave such values
140    alone until, for other reasons, we garbage-collect useless
141    values.  */
142 static int n_useless_values;
143 static int n_useless_debug_values;
144
145 /* Count values whose locs have been taken exclusively from debug
146    insns for the entire life of the value.  */
147 static int n_debug_values;
148
149 /* Number of useless values before we remove them from the hash table.  */
150 #define MAX_USELESS_VALUES 32
151
152 /* This table maps from register number to values.  It does not
153    contain pointers to cselib_val structures, but rather elt_lists.
154    The purpose is to be able to refer to the same register in
155    different modes.  The first element of the list defines the mode in
156    which the register was set; if the mode is unknown or the value is
157    no longer valid in that mode, ELT will be NULL for the first
158    element.  */
159 static struct elt_list **reg_values;
160 static unsigned int reg_values_size;
161 #define REG_VALUES(i) reg_values[i]
162
163 /* The largest number of hard regs used by any entry added to the
164    REG_VALUES table.  Cleared on each cselib_clear_table() invocation.  */
165 static unsigned int max_value_regs;
166
167 /* Here the set of indices I with REG_VALUES(I) != 0 is saved.  This is used
168    in cselib_clear_table() for fast emptying.  */
169 static unsigned int *used_regs;
170 static unsigned int n_used_regs;
171
172 /* We pass this to cselib_invalidate_mem to invalidate all of
173    memory for a non-const call instruction.  */
174 static GTY(()) rtx callmem;
175
176 /* Set by discard_useless_locs if it deleted the last location of any
177    value.  */
178 static int values_became_useless;
179
180 /* Used as stop element of the containing_mem list so we can check
181    presence in the list by checking the next pointer.  */
182 static cselib_val dummy_val;
183
184 /* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
185    that is constant through the whole function and should never be
186    eliminated.  */
187 static cselib_val *cfa_base_preserved_val;
188 static unsigned int cfa_base_preserved_regno;
189
190 /* Used to list all values that contain memory reference.
191    May or may not contain the useless values - the list is compacted
192    each time memory is invalidated.  */
193 static cselib_val *first_containing_mem = &dummy_val;
194 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
195
196 /* If nonnull, cselib will call this function before freeing useless
197    VALUEs.  A VALUE is deemed useless if its "locs" field is null.  */
198 void (*cselib_discard_hook) (cselib_val *);
199
200 /* If nonnull, cselib will call this function before recording sets or
201    even clobbering outputs of INSN.  All the recorded sets will be
202    represented in the array sets[n_sets].  new_val_min can be used to
203    tell whether values present in sets are introduced by this
204    instruction.  */
205 void (*cselib_record_sets_hook) (rtx insn, struct cselib_set *sets,
206                                  int n_sets);
207
208 #define PRESERVED_VALUE_P(RTX) \
209   (RTL_FLAG_CHECK1("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
210
211 \f
212
213 /* Allocate a struct elt_list and fill in its two elements with the
214    arguments.  */
215
216 static inline struct elt_list *
217 new_elt_list (struct elt_list *next, cselib_val *elt)
218 {
219   struct elt_list *el;
220   el = (struct elt_list *) pool_alloc (elt_list_pool);
221   el->next = next;
222   el->elt = elt;
223   return el;
224 }
225
226 /* Allocate a struct elt_loc_list and fill in its two elements with the
227    arguments.  */
228
229 static inline struct elt_loc_list *
230 new_elt_loc_list (struct elt_loc_list *next, rtx loc)
231 {
232   struct elt_loc_list *el;
233   el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
234   el->next = next;
235   el->loc = loc;
236   el->setting_insn = cselib_current_insn;
237   gcc_assert (!next || !next->setting_insn
238               || !DEBUG_INSN_P (next->setting_insn));
239
240   /* If we're creating the first loc in a debug insn context, we've
241      just created a debug value.  Count it.  */
242   if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
243     n_debug_values++;
244
245   return el;
246 }
247
248 /* Promote loc L to a nondebug cselib_current_insn if L is marked as
249    originating from a debug insn, maintaining the debug values
250    count.  */
251
252 static inline void
253 promote_debug_loc (struct elt_loc_list *l)
254 {
255   if (l->setting_insn && DEBUG_INSN_P (l->setting_insn)
256       && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
257     {
258       n_debug_values--;
259       l->setting_insn = cselib_current_insn;
260       gcc_assert (!l->next);
261     }
262 }
263
264 /* The elt_list at *PL is no longer needed.  Unchain it and free its
265    storage.  */
266
267 static inline void
268 unchain_one_elt_list (struct elt_list **pl)
269 {
270   struct elt_list *l = *pl;
271
272   *pl = l->next;
273   pool_free (elt_list_pool, l);
274 }
275
276 /* Likewise for elt_loc_lists.  */
277
278 static void
279 unchain_one_elt_loc_list (struct elt_loc_list **pl)
280 {
281   struct elt_loc_list *l = *pl;
282
283   *pl = l->next;
284   pool_free (elt_loc_list_pool, l);
285 }
286
287 /* Likewise for cselib_vals.  This also frees the addr_list associated with
288    V.  */
289
290 static void
291 unchain_one_value (cselib_val *v)
292 {
293   while (v->addr_list)
294     unchain_one_elt_list (&v->addr_list);
295
296   pool_free (cselib_val_pool, v);
297 }
298
299 /* Remove all entries from the hash table.  Also used during
300    initialization.  */
301
302 void
303 cselib_clear_table (void)
304 {
305   cselib_reset_table (1);
306 }
307
308 /* Remove from hash table all VALUEs except constants.  */
309
310 static int
311 preserve_only_constants (void **x, void *info ATTRIBUTE_UNUSED)
312 {
313   cselib_val *v = (cselib_val *)*x;
314
315   if (v->locs != NULL
316       && v->locs->next == NULL)
317     {
318       if (CONSTANT_P (v->locs->loc)
319           && (GET_CODE (v->locs->loc) != CONST
320               || !references_value_p (v->locs->loc, 0)))
321         return 1;
322       if (cfa_base_preserved_val)
323         {
324           if (v == cfa_base_preserved_val)
325             return 1;
326           if (GET_CODE (v->locs->loc) == PLUS
327               && CONST_INT_P (XEXP (v->locs->loc, 1))
328               && XEXP (v->locs->loc, 0) == cfa_base_preserved_val->val_rtx)
329             return 1;
330         }
331     }
332
333   htab_clear_slot (cselib_hash_table, x);
334   return 1;
335 }
336
337 /* Remove all entries from the hash table, arranging for the next
338    value to be numbered NUM.  */
339
340 void
341 cselib_reset_table (unsigned int num)
342 {
343   unsigned int i;
344
345   max_value_regs = 0;
346
347   if (cfa_base_preserved_val)
348     {
349       unsigned int regno = cfa_base_preserved_regno;
350       unsigned int new_used_regs = 0;
351       for (i = 0; i < n_used_regs; i++)
352         if (used_regs[i] == regno)
353           {
354             new_used_regs = 1;
355             continue;
356           }
357         else
358           REG_VALUES (used_regs[i]) = 0;
359       gcc_assert (new_used_regs == 1);
360       n_used_regs = new_used_regs;
361       used_regs[0] = regno;
362       max_value_regs
363         = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
364     }
365   else
366     {
367       for (i = 0; i < n_used_regs; i++)
368         REG_VALUES (used_regs[i]) = 0;
369       n_used_regs = 0;
370     }
371
372   if (cselib_preserve_constants)
373     htab_traverse (cselib_hash_table, preserve_only_constants, NULL);
374   else
375     htab_empty (cselib_hash_table);
376
377   n_useless_values = 0;
378   n_useless_debug_values = 0;
379   n_debug_values = 0;
380
381   next_uid = num;
382
383   first_containing_mem = &dummy_val;
384 }
385
386 /* Return the number of the next value that will be generated.  */
387
388 unsigned int
389 cselib_get_next_uid (void)
390 {
391   return next_uid;
392 }
393
394 /* See the documentation of cselib_find_slot below.  */
395 static enum machine_mode find_slot_memmode;
396
397 /* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
398    INSERTing if requested.  When X is part of the address of a MEM,
399    MEMMODE should specify the mode of the MEM.  While searching the
400    table, MEMMODE is held in FIND_SLOT_MEMMODE, so that autoinc RTXs
401    in X can be resolved.  */
402
403 static void **
404 cselib_find_slot (rtx x, hashval_t hash, enum insert_option insert,
405                   enum machine_mode memmode)
406 {
407   void **slot;
408   find_slot_memmode = memmode;
409   slot = htab_find_slot_with_hash (cselib_hash_table, x, hash, insert);
410   find_slot_memmode = VOIDmode;
411   return slot;
412 }
413
414 /* The equality test for our hash table.  The first argument ENTRY is a table
415    element (i.e. a cselib_val), while the second arg X is an rtx.  We know
416    that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
417    CONST of an appropriate mode.  */
418
419 static int
420 entry_and_rtx_equal_p (const void *entry, const void *x_arg)
421 {
422   struct elt_loc_list *l;
423   const cselib_val *const v = (const cselib_val *) entry;
424   rtx x = CONST_CAST_RTX ((const_rtx)x_arg);
425   enum machine_mode mode = GET_MODE (x);
426
427   gcc_assert (!CONST_INT_P (x) && GET_CODE (x) != CONST_FIXED
428               && (mode != VOIDmode || GET_CODE (x) != CONST_DOUBLE));
429
430   if (mode != GET_MODE (v->val_rtx))
431     return 0;
432
433   /* Unwrap X if necessary.  */
434   if (GET_CODE (x) == CONST
435       && (CONST_INT_P (XEXP (x, 0))
436           || GET_CODE (XEXP (x, 0)) == CONST_FIXED
437           || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE))
438     x = XEXP (x, 0);
439
440   /* We don't guarantee that distinct rtx's have different hash values,
441      so we need to do a comparison.  */
442   for (l = v->locs; l; l = l->next)
443     if (rtx_equal_for_cselib_1 (l->loc, x, find_slot_memmode))
444       {
445         promote_debug_loc (l);
446         return 1;
447       }
448
449   return 0;
450 }
451
452 /* The hash function for our hash table.  The value is always computed with
453    cselib_hash_rtx when adding an element; this function just extracts the
454    hash value from a cselib_val structure.  */
455
456 static hashval_t
457 get_value_hash (const void *entry)
458 {
459   const cselib_val *const v = (const cselib_val *) entry;
460   return v->hash;
461 }
462
463 /* Return true if X contains a VALUE rtx.  If ONLY_USELESS is set, we
464    only return true for values which point to a cselib_val whose value
465    element has been set to zero, which implies the cselib_val will be
466    removed.  */
467
468 int
469 references_value_p (const_rtx x, int only_useless)
470 {
471   const enum rtx_code code = GET_CODE (x);
472   const char *fmt = GET_RTX_FORMAT (code);
473   int i, j;
474
475   if (GET_CODE (x) == VALUE
476       && (! only_useless || CSELIB_VAL_PTR (x)->locs == 0))
477     return 1;
478
479   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
480     {
481       if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
482         return 1;
483       else if (fmt[i] == 'E')
484         for (j = 0; j < XVECLEN (x, i); j++)
485           if (references_value_p (XVECEXP (x, i, j), only_useless))
486             return 1;
487     }
488
489   return 0;
490 }
491
492 /* For all locations found in X, delete locations that reference useless
493    values (i.e. values without any location).  Called through
494    htab_traverse.  */
495
496 static int
497 discard_useless_locs (void **x, void *info ATTRIBUTE_UNUSED)
498 {
499   cselib_val *v = (cselib_val *)*x;
500   struct elt_loc_list **p = &v->locs;
501   bool had_locs = v->locs != NULL;
502   rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
503
504   while (*p)
505     {
506       if (references_value_p ((*p)->loc, 1))
507         unchain_one_elt_loc_list (p);
508       else
509         p = &(*p)->next;
510     }
511
512   if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
513     {
514       if (setting_insn && DEBUG_INSN_P (setting_insn))
515         n_useless_debug_values++;
516       else
517         n_useless_values++;
518       values_became_useless = 1;
519     }
520   return 1;
521 }
522
523 /* If X is a value with no locations, remove it from the hashtable.  */
524
525 static int
526 discard_useless_values (void **x, void *info ATTRIBUTE_UNUSED)
527 {
528   cselib_val *v = (cselib_val *)*x;
529
530   if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
531     {
532       if (cselib_discard_hook)
533         cselib_discard_hook (v);
534
535       CSELIB_VAL_PTR (v->val_rtx) = NULL;
536       htab_clear_slot (cselib_hash_table, x);
537       unchain_one_value (v);
538       n_useless_values--;
539     }
540
541   return 1;
542 }
543
544 /* Clean out useless values (i.e. those which no longer have locations
545    associated with them) from the hash table.  */
546
547 static void
548 remove_useless_values (void)
549 {
550   cselib_val **p, *v;
551
552   /* First pass: eliminate locations that reference the value.  That in
553      turn can make more values useless.  */
554   do
555     {
556       values_became_useless = 0;
557       htab_traverse (cselib_hash_table, discard_useless_locs, 0);
558     }
559   while (values_became_useless);
560
561   /* Second pass: actually remove the values.  */
562
563   p = &first_containing_mem;
564   for (v = *p; v != &dummy_val; v = v->next_containing_mem)
565     if (v->locs)
566       {
567         *p = v;
568         p = &(*p)->next_containing_mem;
569       }
570   *p = &dummy_val;
571
572   n_useless_values += n_useless_debug_values;
573   n_debug_values -= n_useless_debug_values;
574   n_useless_debug_values = 0;
575
576   htab_traverse (cselib_hash_table, discard_useless_values, 0);
577
578   gcc_assert (!n_useless_values);
579 }
580
581 /* Arrange for a value to not be removed from the hash table even if
582    it becomes useless.  */
583
584 void
585 cselib_preserve_value (cselib_val *v)
586 {
587   PRESERVED_VALUE_P (v->val_rtx) = 1;
588 }
589
590 /* Test whether a value is preserved.  */
591
592 bool
593 cselib_preserved_value_p (cselib_val *v)
594 {
595   return PRESERVED_VALUE_P (v->val_rtx);
596 }
597
598 /* Arrange for a REG value to be assumed constant through the whole function,
599    never invalidated and preserved across cselib_reset_table calls.  */
600
601 void
602 cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
603 {
604   if (cselib_preserve_constants
605       && v->locs
606       && REG_P (v->locs->loc))
607     {
608       cfa_base_preserved_val = v;
609       cfa_base_preserved_regno = regno;
610     }
611 }
612
613 /* Clean all non-constant expressions in the hash table, but retain
614    their values.  */
615
616 void
617 cselib_preserve_only_values (void)
618 {
619   int i;
620
621   for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
622     cselib_invalidate_regno (i, reg_raw_mode[i]);
623
624   cselib_invalidate_mem (callmem);
625
626   remove_useless_values ();
627
628   gcc_assert (first_containing_mem == &dummy_val);
629 }
630
631 /* Return the mode in which a register was last set.  If X is not a
632    register, return its mode.  If the mode in which the register was
633    set is not known, or the value was already clobbered, return
634    VOIDmode.  */
635
636 enum machine_mode
637 cselib_reg_set_mode (const_rtx x)
638 {
639   if (!REG_P (x))
640     return GET_MODE (x);
641
642   if (REG_VALUES (REGNO (x)) == NULL
643       || REG_VALUES (REGNO (x))->elt == NULL)
644     return VOIDmode;
645
646   return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
647 }
648
649 /* Return nonzero if we can prove that X and Y contain the same value, taking
650    our gathered information into account.  */
651
652 int
653 rtx_equal_for_cselib_p (rtx x, rtx y)
654 {
655   return rtx_equal_for_cselib_1 (x, y, VOIDmode);
656 }
657
658 /* If x is a PLUS or an autoinc operation, expand the operation,
659    storing the offset, if any, in *OFF.  */
660
661 static rtx
662 autoinc_split (rtx x, rtx *off, enum machine_mode memmode)
663 {
664   switch (GET_CODE (x))
665     {
666     case PLUS:
667       *off = XEXP (x, 1);
668       return XEXP (x, 0);
669
670     case PRE_DEC:
671       if (memmode == VOIDmode)
672         return x;
673
674       *off = GEN_INT (-GET_MODE_SIZE (memmode));
675       return XEXP (x, 0);
676       break;
677
678     case PRE_INC:
679       if (memmode == VOIDmode)
680         return x;
681
682       *off = GEN_INT (GET_MODE_SIZE (memmode));
683       return XEXP (x, 0);
684
685     case PRE_MODIFY:
686       return XEXP (x, 1);
687
688     case POST_DEC:
689     case POST_INC:
690     case POST_MODIFY:
691       return XEXP (x, 0);
692
693     default:
694       return x;
695     }
696 }
697
698 /* Return nonzero if we can prove that X and Y contain the same value,
699    taking our gathered information into account.  MEMMODE holds the
700    mode of the enclosing MEM, if any, as required to deal with autoinc
701    addressing modes.  If X and Y are not (known to be) part of
702    addresses, MEMMODE should be VOIDmode.  */
703
704 static int
705 rtx_equal_for_cselib_1 (rtx x, rtx y, enum machine_mode memmode)
706 {
707   enum rtx_code code;
708   const char *fmt;
709   int i;
710
711   if (REG_P (x) || MEM_P (x))
712     {
713       cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
714
715       if (e)
716         x = e->val_rtx;
717     }
718
719   if (REG_P (y) || MEM_P (y))
720     {
721       cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
722
723       if (e)
724         y = e->val_rtx;
725     }
726
727   if (x == y)
728     return 1;
729
730   if (GET_CODE (x) == VALUE && GET_CODE (y) == VALUE)
731     return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
732
733   if (GET_CODE (x) == VALUE)
734     {
735       cselib_val *e = CSELIB_VAL_PTR (x);
736       struct elt_loc_list *l;
737
738       for (l = e->locs; l; l = l->next)
739         {
740           rtx t = l->loc;
741
742           /* Avoid infinite recursion.  */
743           if (REG_P (t) || MEM_P (t))
744             continue;
745           else if (rtx_equal_for_cselib_1 (t, y, memmode))
746             return 1;
747         }
748
749       return 0;
750     }
751
752   if (GET_CODE (y) == VALUE)
753     {
754       cselib_val *e = CSELIB_VAL_PTR (y);
755       struct elt_loc_list *l;
756
757       for (l = e->locs; l; l = l->next)
758         {
759           rtx t = l->loc;
760
761           if (REG_P (t) || MEM_P (t))
762             continue;
763           else if (rtx_equal_for_cselib_1 (x, t, memmode))
764             return 1;
765         }
766
767       return 0;
768     }
769
770   if (GET_MODE (x) != GET_MODE (y))
771     return 0;
772
773   if (GET_CODE (x) != GET_CODE (y))
774     {
775       rtx xorig = x, yorig = y;
776       rtx xoff = NULL, yoff = NULL;
777
778       x = autoinc_split (x, &xoff, memmode);
779       y = autoinc_split (y, &yoff, memmode);
780
781       if (!xoff != !yoff)
782         return 0;
783
784       if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
785         return 0;
786
787       /* Don't recurse if nothing changed.  */
788       if (x != xorig || y != yorig)
789         return rtx_equal_for_cselib_1 (x, y, memmode);
790
791       return 0;
792     }
793
794   /* These won't be handled correctly by the code below.  */
795   switch (GET_CODE (x))
796     {
797     case CONST_DOUBLE:
798     case CONST_FIXED:
799     case DEBUG_EXPR:
800       return 0;
801
802     case DEBUG_IMPLICIT_PTR:
803       return DEBUG_IMPLICIT_PTR_DECL (x)
804              == DEBUG_IMPLICIT_PTR_DECL (y);
805
806     case ENTRY_VALUE:
807       return rtx_equal_for_cselib_1 (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y),
808                                      memmode);
809
810     case LABEL_REF:
811       return XEXP (x, 0) == XEXP (y, 0);
812
813     case MEM:
814       /* We have to compare any autoinc operations in the addresses
815          using this MEM's mode.  */
816       return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
817
818     default:
819       break;
820     }
821
822   code = GET_CODE (x);
823   fmt = GET_RTX_FORMAT (code);
824
825   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
826     {
827       int j;
828
829       switch (fmt[i])
830         {
831         case 'w':
832           if (XWINT (x, i) != XWINT (y, i))
833             return 0;
834           break;
835
836         case 'n':
837         case 'i':
838           if (XINT (x, i) != XINT (y, i))
839             return 0;
840           break;
841
842         case 'V':
843         case 'E':
844           /* Two vectors must have the same length.  */
845           if (XVECLEN (x, i) != XVECLEN (y, i))
846             return 0;
847
848           /* And the corresponding elements must match.  */
849           for (j = 0; j < XVECLEN (x, i); j++)
850             if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
851                                           XVECEXP (y, i, j), memmode))
852               return 0;
853           break;
854
855         case 'e':
856           if (i == 1
857               && targetm.commutative_p (x, UNKNOWN)
858               && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
859               && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
860             return 1;
861           if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
862             return 0;
863           break;
864
865         case 'S':
866         case 's':
867           if (strcmp (XSTR (x, i), XSTR (y, i)))
868             return 0;
869           break;
870
871         case 'u':
872           /* These are just backpointers, so they don't matter.  */
873           break;
874
875         case '0':
876         case 't':
877           break;
878
879           /* It is believed that rtx's at this level will never
880              contain anything but integers and other rtx's,
881              except for within LABEL_REFs and SYMBOL_REFs.  */
882         default:
883           gcc_unreachable ();
884         }
885     }
886   return 1;
887 }
888
889 /* We need to pass down the mode of constants through the hash table
890    functions.  For that purpose, wrap them in a CONST of the appropriate
891    mode.  */
892 static rtx
893 wrap_constant (enum machine_mode mode, rtx x)
894 {
895   if (!CONST_INT_P (x) && GET_CODE (x) != CONST_FIXED
896       && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode))
897     return x;
898   gcc_assert (mode != VOIDmode);
899   return gen_rtx_CONST (mode, x);
900 }
901
902 /* Hash an rtx.  Return 0 if we couldn't hash the rtx.
903    For registers and memory locations, we look up their cselib_val structure
904    and return its VALUE element.
905    Possible reasons for return 0 are: the object is volatile, or we couldn't
906    find a register or memory location in the table and CREATE is zero.  If
907    CREATE is nonzero, table elts are created for regs and mem.
908    N.B. this hash function returns the same hash value for RTXes that
909    differ only in the order of operands, thus it is suitable for comparisons
910    that take commutativity into account.
911    If we wanted to also support associative rules, we'd have to use a different
912    strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
913    MEMMODE indicates the mode of an enclosing MEM, and it's only
914    used to compute autoinc values.
915    We used to have a MODE argument for hashing for CONST_INTs, but that
916    didn't make sense, since it caused spurious hash differences between
917     (set (reg:SI 1) (const_int))
918     (plus:SI (reg:SI 2) (reg:SI 1))
919    and
920     (plus:SI (reg:SI 2) (const_int))
921    If the mode is important in any context, it must be checked specifically
922    in a comparison anyway, since relying on hash differences is unsafe.  */
923
924 static unsigned int
925 cselib_hash_rtx (rtx x, int create, enum machine_mode memmode)
926 {
927   cselib_val *e;
928   int i, j;
929   enum rtx_code code;
930   const char *fmt;
931   unsigned int hash = 0;
932
933   code = GET_CODE (x);
934   hash += (unsigned) code + (unsigned) GET_MODE (x);
935
936   switch (code)
937     {
938     case MEM:
939     case REG:
940       e = cselib_lookup (x, GET_MODE (x), create, memmode);
941       if (! e)
942         return 0;
943
944       return e->hash;
945
946     case DEBUG_EXPR:
947       hash += ((unsigned) DEBUG_EXPR << 7)
948               + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
949       return hash ? hash : (unsigned int) DEBUG_EXPR;
950
951     case DEBUG_IMPLICIT_PTR:
952       hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
953               + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
954       return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
955
956     case ENTRY_VALUE:
957       hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
958       return hash ? hash : (unsigned int) ENTRY_VALUE;
959
960     case CONST_INT:
961       hash += ((unsigned) CONST_INT << 7) + INTVAL (x);
962       return hash ? hash : (unsigned int) CONST_INT;
963
964     case CONST_DOUBLE:
965       /* This is like the general case, except that it only counts
966          the integers representing the constant.  */
967       hash += (unsigned) code + (unsigned) GET_MODE (x);
968       if (GET_MODE (x) != VOIDmode)
969         hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
970       else
971         hash += ((unsigned) CONST_DOUBLE_LOW (x)
972                  + (unsigned) CONST_DOUBLE_HIGH (x));
973       return hash ? hash : (unsigned int) CONST_DOUBLE;
974
975     case CONST_FIXED:
976       hash += (unsigned int) code + (unsigned int) GET_MODE (x);
977       hash += fixed_hash (CONST_FIXED_VALUE (x));
978       return hash ? hash : (unsigned int) CONST_FIXED;
979
980     case CONST_VECTOR:
981       {
982         int units;
983         rtx elt;
984
985         units = CONST_VECTOR_NUNITS (x);
986
987         for (i = 0; i < units; ++i)
988           {
989             elt = CONST_VECTOR_ELT (x, i);
990             hash += cselib_hash_rtx (elt, 0, memmode);
991           }
992
993         return hash;
994       }
995
996       /* Assume there is only one rtx object for any given label.  */
997     case LABEL_REF:
998       /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
999          differences and differences between each stage's debugging dumps.  */
1000       hash += (((unsigned int) LABEL_REF << 7)
1001                + CODE_LABEL_NUMBER (XEXP (x, 0)));
1002       return hash ? hash : (unsigned int) LABEL_REF;
1003
1004     case SYMBOL_REF:
1005       {
1006         /* Don't hash on the symbol's address to avoid bootstrap differences.
1007            Different hash values may cause expressions to be recorded in
1008            different orders and thus different registers to be used in the
1009            final assembler.  This also avoids differences in the dump files
1010            between various stages.  */
1011         unsigned int h = 0;
1012         const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1013
1014         while (*p)
1015           h += (h << 7) + *p++; /* ??? revisit */
1016
1017         hash += ((unsigned int) SYMBOL_REF << 7) + h;
1018         return hash ? hash : (unsigned int) SYMBOL_REF;
1019       }
1020
1021     case PRE_DEC:
1022     case PRE_INC:
1023       /* We can't compute these without knowing the MEM mode.  */
1024       gcc_assert (memmode != VOIDmode);
1025       i = GET_MODE_SIZE (memmode);
1026       if (code == PRE_DEC)
1027         i = -i;
1028       /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1029          like (mem:MEMMODE (plus (reg) (const_int I))).  */
1030       hash += (unsigned) PLUS - (unsigned)code
1031         + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1032         + cselib_hash_rtx (GEN_INT (i), create, memmode);
1033       return hash ? hash : 1 + (unsigned) PLUS;
1034
1035     case PRE_MODIFY:
1036       gcc_assert (memmode != VOIDmode);
1037       return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1038
1039     case POST_DEC:
1040     case POST_INC:
1041     case POST_MODIFY:
1042       gcc_assert (memmode != VOIDmode);
1043       return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1044
1045     case PC:
1046     case CC0:
1047     case CALL:
1048     case UNSPEC_VOLATILE:
1049       return 0;
1050
1051     case ASM_OPERANDS:
1052       if (MEM_VOLATILE_P (x))
1053         return 0;
1054
1055       break;
1056
1057     default:
1058       break;
1059     }
1060
1061   i = GET_RTX_LENGTH (code) - 1;
1062   fmt = GET_RTX_FORMAT (code);
1063   for (; i >= 0; i--)
1064     {
1065       switch (fmt[i])
1066         {
1067         case 'e':
1068           {
1069             rtx tem = XEXP (x, i);
1070             unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
1071
1072             if (tem_hash == 0)
1073               return 0;
1074
1075             hash += tem_hash;
1076           }
1077           break;
1078         case 'E':
1079           for (j = 0; j < XVECLEN (x, i); j++)
1080             {
1081               unsigned int tem_hash
1082                 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
1083
1084               if (tem_hash == 0)
1085                 return 0;
1086
1087               hash += tem_hash;
1088             }
1089           break;
1090
1091         case 's':
1092           {
1093             const unsigned char *p = (const unsigned char *) XSTR (x, i);
1094
1095             if (p)
1096               while (*p)
1097                 hash += *p++;
1098             break;
1099           }
1100
1101         case 'i':
1102           hash += XINT (x, i);
1103           break;
1104
1105         case '0':
1106         case 't':
1107           /* unused */
1108           break;
1109
1110         default:
1111           gcc_unreachable ();
1112         }
1113     }
1114
1115   return hash ? hash : 1 + (unsigned int) GET_CODE (x);
1116 }
1117
1118 /* Create a new value structure for VALUE and initialize it.  The mode of the
1119    value is MODE.  */
1120
1121 static inline cselib_val *
1122 new_cselib_val (unsigned int hash, enum machine_mode mode, rtx x)
1123 {
1124   cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
1125
1126   gcc_assert (hash);
1127   gcc_assert (next_uid);
1128
1129   e->hash = hash;
1130   e->uid = next_uid++;
1131   /* We use an alloc pool to allocate this RTL construct because it
1132      accounts for about 8% of the overall memory usage.  We know
1133      precisely when we can have VALUE RTXen (when cselib is active)
1134      so we don't need to put them in garbage collected memory.
1135      ??? Why should a VALUE be an RTX in the first place?  */
1136   e->val_rtx = (rtx) pool_alloc (value_pool);
1137   memset (e->val_rtx, 0, RTX_HDR_SIZE);
1138   PUT_CODE (e->val_rtx, VALUE);
1139   PUT_MODE (e->val_rtx, mode);
1140   CSELIB_VAL_PTR (e->val_rtx) = e;
1141   e->addr_list = 0;
1142   e->locs = 0;
1143   e->next_containing_mem = 0;
1144
1145   if (dump_file && (dump_flags & TDF_CSELIB))
1146     {
1147       fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
1148       if (flag_dump_noaddr || flag_dump_unnumbered)
1149         fputs ("# ", dump_file);
1150       else
1151         fprintf (dump_file, "%p ", (void*)e);
1152       print_rtl_single (dump_file, x);
1153       fputc ('\n', dump_file);
1154     }
1155
1156   return e;
1157 }
1158
1159 /* ADDR_ELT is a value that is used as address.  MEM_ELT is the value that
1160    contains the data at this address.  X is a MEM that represents the
1161    value.  Update the two value structures to represent this situation.  */
1162
1163 static void
1164 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
1165 {
1166   struct elt_loc_list *l;
1167
1168   /* Avoid duplicates.  */
1169   for (l = mem_elt->locs; l; l = l->next)
1170     if (MEM_P (l->loc)
1171         && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
1172       {
1173         promote_debug_loc (l);
1174         return;
1175       }
1176
1177   addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
1178   mem_elt->locs
1179     = new_elt_loc_list (mem_elt->locs,
1180                         replace_equiv_address_nv (x, addr_elt->val_rtx));
1181   if (mem_elt->next_containing_mem == NULL)
1182     {
1183       mem_elt->next_containing_mem = first_containing_mem;
1184       first_containing_mem = mem_elt;
1185     }
1186 }
1187
1188 /* Subroutine of cselib_lookup.  Return a value for X, which is a MEM rtx.
1189    If CREATE, make a new one if we haven't seen it before.  */
1190
1191 static cselib_val *
1192 cselib_lookup_mem (rtx x, int create)
1193 {
1194   enum machine_mode mode = GET_MODE (x);
1195   enum machine_mode addr_mode;
1196   void **slot;
1197   cselib_val *addr;
1198   cselib_val *mem_elt;
1199   struct elt_list *l;
1200
1201   if (MEM_VOLATILE_P (x) || mode == BLKmode
1202       || !cselib_record_memory
1203       || (FLOAT_MODE_P (mode) && flag_float_store))
1204     return 0;
1205
1206   addr_mode = GET_MODE (XEXP (x, 0));
1207   if (addr_mode == VOIDmode)
1208     addr_mode = Pmode;
1209
1210   /* Look up the value for the address.  */
1211   addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
1212   if (! addr)
1213     return 0;
1214
1215   /* Find a value that describes a value of our mode at that address.  */
1216   for (l = addr->addr_list; l; l = l->next)
1217     if (GET_MODE (l->elt->val_rtx) == mode)
1218       {
1219         promote_debug_loc (l->elt->locs);
1220         return l->elt;
1221       }
1222
1223   if (! create)
1224     return 0;
1225
1226   mem_elt = new_cselib_val (next_uid, mode, x);
1227   add_mem_for_addr (addr, mem_elt, x);
1228   slot = cselib_find_slot (wrap_constant (mode, x), mem_elt->hash,
1229                            INSERT, mode);
1230   *slot = mem_elt;
1231   return mem_elt;
1232 }
1233
1234 /* Search thru the possible substitutions in P.  We prefer a non reg
1235    substitution because this allows us to expand the tree further.  If
1236    we find, just a reg, take the lowest regno.  There may be several
1237    non-reg results, we just take the first one because they will all
1238    expand to the same place.  */
1239
1240 static rtx
1241 expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1242             int max_depth)
1243 {
1244   rtx reg_result = NULL;
1245   unsigned int regno = UINT_MAX;
1246   struct elt_loc_list *p_in = p;
1247
1248   for (; p; p = p -> next)
1249     {
1250       /* Avoid infinite recursion trying to expand a reg into a
1251          the same reg.  */
1252       if ((REG_P (p->loc))
1253           && (REGNO (p->loc) < regno)
1254           && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
1255         {
1256           reg_result = p->loc;
1257           regno = REGNO (p->loc);
1258         }
1259       /* Avoid infinite recursion and do not try to expand the
1260          value.  */
1261       else if (GET_CODE (p->loc) == VALUE
1262                && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1263         continue;
1264       else if (!REG_P (p->loc))
1265         {
1266           rtx result, note;
1267           if (dump_file && (dump_flags & TDF_CSELIB))
1268             {
1269               print_inline_rtx (dump_file, p->loc, 0);
1270               fprintf (dump_file, "\n");
1271             }
1272           if (GET_CODE (p->loc) == LO_SUM
1273               && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1274               && p->setting_insn
1275               && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1276               && XEXP (note, 0) == XEXP (p->loc, 1))
1277             return XEXP (p->loc, 1);
1278           result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
1279           if (result)
1280             return result;
1281         }
1282
1283     }
1284
1285   if (regno != UINT_MAX)
1286     {
1287       rtx result;
1288       if (dump_file && (dump_flags & TDF_CSELIB))
1289         fprintf (dump_file, "r%d\n", regno);
1290
1291       result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
1292       if (result)
1293         return result;
1294     }
1295
1296   if (dump_file && (dump_flags & TDF_CSELIB))
1297     {
1298       if (reg_result)
1299         {
1300           print_inline_rtx (dump_file, reg_result, 0);
1301           fprintf (dump_file, "\n");
1302         }
1303       else
1304         fprintf (dump_file, "NULL\n");
1305     }
1306   return reg_result;
1307 }
1308
1309
1310 /* Forward substitute and expand an expression out to its roots.
1311    This is the opposite of common subexpression.  Because local value
1312    numbering is such a weak optimization, the expanded expression is
1313    pretty much unique (not from a pointer equals point of view but
1314    from a tree shape point of view.
1315
1316    This function returns NULL if the expansion fails.  The expansion
1317    will fail if there is no value number for one of the operands or if
1318    one of the operands has been overwritten between the current insn
1319    and the beginning of the basic block.  For instance x has no
1320    expansion in:
1321
1322    r1 <- r1 + 3
1323    x <- r1 + 8
1324
1325    REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1326    It is clear on return.  */
1327
1328 rtx
1329 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
1330 {
1331   struct expand_value_data evd;
1332
1333   evd.regs_active = regs_active;
1334   evd.callback = NULL;
1335   evd.callback_arg = NULL;
1336   evd.dummy = false;
1337
1338   return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1339 }
1340
1341 /* Same as cselib_expand_value_rtx, but using a callback to try to
1342    resolve some expressions.  The CB function should return ORIG if it
1343    can't or does not want to deal with a certain RTX.  Any other
1344    return value, including NULL, will be used as the expansion for
1345    VALUE, without any further changes.  */
1346
1347 rtx
1348 cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1349                             cselib_expand_callback cb, void *data)
1350 {
1351   struct expand_value_data evd;
1352
1353   evd.regs_active = regs_active;
1354   evd.callback = cb;
1355   evd.callback_arg = data;
1356   evd.dummy = false;
1357
1358   return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1359 }
1360
1361 /* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1362    or simplified.  Useful to find out whether cselib_expand_value_rtx_cb
1363    would return NULL or non-NULL, without allocating new rtx.  */
1364
1365 bool
1366 cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1367                                   cselib_expand_callback cb, void *data)
1368 {
1369   struct expand_value_data evd;
1370
1371   evd.regs_active = regs_active;
1372   evd.callback = cb;
1373   evd.callback_arg = data;
1374   evd.dummy = true;
1375
1376   return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1377 }
1378
1379 /* Internal implementation of cselib_expand_value_rtx and
1380    cselib_expand_value_rtx_cb.  */
1381
1382 static rtx
1383 cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1384                            int max_depth)
1385 {
1386   rtx copy, scopy;
1387   int i, j;
1388   RTX_CODE code;
1389   const char *format_ptr;
1390   enum machine_mode mode;
1391
1392   code = GET_CODE (orig);
1393
1394   /* For the context of dse, if we end up expand into a huge tree, we
1395      will not have a useful address, so we might as well just give up
1396      quickly.  */
1397   if (max_depth <= 0)
1398     return NULL;
1399
1400   switch (code)
1401     {
1402     case REG:
1403       {
1404         struct elt_list *l = REG_VALUES (REGNO (orig));
1405
1406         if (l && l->elt == NULL)
1407           l = l->next;
1408         for (; l; l = l->next)
1409           if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1410             {
1411               rtx result;
1412               int regno = REGNO (orig);
1413
1414               /* The only thing that we are not willing to do (this
1415                  is requirement of dse and if others potential uses
1416                  need this function we should add a parm to control
1417                  it) is that we will not substitute the
1418                  STACK_POINTER_REGNUM, FRAME_POINTER or the
1419                  HARD_FRAME_POINTER.
1420
1421                  These expansions confuses the code that notices that
1422                  stores into the frame go dead at the end of the
1423                  function and that the frame is not effected by calls
1424                  to subroutines.  If you allow the
1425                  STACK_POINTER_REGNUM substitution, then dse will
1426                  think that parameter pushing also goes dead which is
1427                  wrong.  If you allow the FRAME_POINTER or the
1428                  HARD_FRAME_POINTER then you lose the opportunity to
1429                  make the frame assumptions.  */
1430               if (regno == STACK_POINTER_REGNUM
1431                   || regno == FRAME_POINTER_REGNUM
1432                   || regno == HARD_FRAME_POINTER_REGNUM)
1433                 return orig;
1434
1435               bitmap_set_bit (evd->regs_active, regno);
1436
1437               if (dump_file && (dump_flags & TDF_CSELIB))
1438                 fprintf (dump_file, "expanding: r%d into: ", regno);
1439
1440               result = expand_loc (l->elt->locs, evd, max_depth);
1441               bitmap_clear_bit (evd->regs_active, regno);
1442
1443               if (result)
1444                 return result;
1445               else
1446                 return orig;
1447             }
1448       }
1449
1450     case CONST_INT:
1451     case CONST_DOUBLE:
1452     case CONST_VECTOR:
1453     case SYMBOL_REF:
1454     case CODE_LABEL:
1455     case PC:
1456     case CC0:
1457     case SCRATCH:
1458       /* SCRATCH must be shared because they represent distinct values.  */
1459       return orig;
1460     case CLOBBER:
1461       if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1462         return orig;
1463       break;
1464
1465     case CONST:
1466       if (shared_const_p (orig))
1467         return orig;
1468       break;
1469
1470     case SUBREG:
1471       {
1472         rtx subreg;
1473
1474         if (evd->callback)
1475           {
1476             subreg = evd->callback (orig, evd->regs_active, max_depth,
1477                                     evd->callback_arg);
1478             if (subreg != orig)
1479               return subreg;
1480           }
1481
1482         subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1483                                             max_depth - 1);
1484         if (!subreg)
1485           return NULL;
1486         scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1487                                      GET_MODE (SUBREG_REG (orig)),
1488                                      SUBREG_BYTE (orig));
1489         if (scopy == NULL
1490             || (GET_CODE (scopy) == SUBREG
1491                 && !REG_P (SUBREG_REG (scopy))
1492                 && !MEM_P (SUBREG_REG (scopy))))
1493           return NULL;
1494
1495         return scopy;
1496       }
1497
1498     case VALUE:
1499       {
1500         rtx result;
1501
1502         if (dump_file && (dump_flags & TDF_CSELIB))
1503           {
1504             fputs ("\nexpanding ", dump_file);
1505             print_rtl_single (dump_file, orig);
1506             fputs (" into...", dump_file);
1507           }
1508
1509         if (evd->callback)
1510           {
1511             result = evd->callback (orig, evd->regs_active, max_depth,
1512                                     evd->callback_arg);
1513
1514             if (result != orig)
1515               return result;
1516           }
1517
1518         result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
1519         return result;
1520       }
1521
1522     case DEBUG_EXPR:
1523       if (evd->callback)
1524         return evd->callback (orig, evd->regs_active, max_depth,
1525                               evd->callback_arg);
1526       return orig;
1527
1528     default:
1529       break;
1530     }
1531
1532   /* Copy the various flags, fields, and other information.  We assume
1533      that all fields need copying, and then clear the fields that should
1534      not be copied.  That is the sensible default behavior, and forces
1535      us to explicitly document why we are *not* copying a flag.  */
1536   if (evd->dummy)
1537     copy = NULL;
1538   else
1539     copy = shallow_copy_rtx (orig);
1540
1541   format_ptr = GET_RTX_FORMAT (code);
1542
1543   for (i = 0; i < GET_RTX_LENGTH (code); i++)
1544     switch (*format_ptr++)
1545       {
1546       case 'e':
1547         if (XEXP (orig, i) != NULL)
1548           {
1549             rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1550                                                     max_depth - 1);
1551             if (!result)
1552               return NULL;
1553             if (copy)
1554               XEXP (copy, i) = result;
1555           }
1556         break;
1557
1558       case 'E':
1559       case 'V':
1560         if (XVEC (orig, i) != NULL)
1561           {
1562             if (copy)
1563               XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1564             for (j = 0; j < XVECLEN (orig, i); j++)
1565               {
1566                 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1567                                                         evd, max_depth - 1);
1568                 if (!result)
1569                   return NULL;
1570                 if (copy)
1571                   XVECEXP (copy, i, j) = result;
1572               }
1573           }
1574         break;
1575
1576       case 't':
1577       case 'w':
1578       case 'i':
1579       case 's':
1580       case 'S':
1581       case 'T':
1582       case 'u':
1583       case 'B':
1584       case '0':
1585         /* These are left unchanged.  */
1586         break;
1587
1588       default:
1589         gcc_unreachable ();
1590       }
1591
1592   if (evd->dummy)
1593     return orig;
1594
1595   mode = GET_MODE (copy);
1596   /* If an operand has been simplified into CONST_INT, which doesn't
1597      have a mode and the mode isn't derivable from whole rtx's mode,
1598      try simplify_*_operation first with mode from original's operand
1599      and as a fallback wrap CONST_INT into gen_rtx_CONST.  */
1600   scopy = copy;
1601   switch (GET_RTX_CLASS (code))
1602     {
1603     case RTX_UNARY:
1604       if (CONST_INT_P (XEXP (copy, 0))
1605           && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1606         {
1607           scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1608                                             GET_MODE (XEXP (orig, 0)));
1609           if (scopy)
1610             return scopy;
1611         }
1612       break;
1613     case RTX_COMM_ARITH:
1614     case RTX_BIN_ARITH:
1615       /* These expressions can derive operand modes from the whole rtx's mode.  */
1616       break;
1617     case RTX_TERNARY:
1618     case RTX_BITFIELD_OPS:
1619       if (CONST_INT_P (XEXP (copy, 0))
1620           && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1621         {
1622           scopy = simplify_ternary_operation (code, mode,
1623                                               GET_MODE (XEXP (orig, 0)),
1624                                               XEXP (copy, 0), XEXP (copy, 1),
1625                                               XEXP (copy, 2));
1626           if (scopy)
1627             return scopy;
1628         }
1629       break;
1630     case RTX_COMPARE:
1631     case RTX_COMM_COMPARE:
1632       if (CONST_INT_P (XEXP (copy, 0))
1633           && GET_MODE (XEXP (copy, 1)) == VOIDmode
1634           && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1635               || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1636         {
1637           scopy = simplify_relational_operation (code, mode,
1638                                                  (GET_MODE (XEXP (orig, 0))
1639                                                   != VOIDmode)
1640                                                  ? GET_MODE (XEXP (orig, 0))
1641                                                  : GET_MODE (XEXP (orig, 1)),
1642                                                  XEXP (copy, 0),
1643                                                  XEXP (copy, 1));
1644           if (scopy)
1645             return scopy;
1646         }
1647       break;
1648     default:
1649       break;
1650     }
1651   scopy = simplify_rtx (copy);
1652   if (scopy)
1653     return scopy;
1654   return copy;
1655 }
1656
1657 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1658    with VALUE expressions.  This way, it becomes independent of changes
1659    to registers and memory.
1660    X isn't actually modified; if modifications are needed, new rtl is
1661    allocated.  However, the return value can share rtl with X.
1662    If X is within a MEM, MEMMODE must be the mode of the MEM.  */
1663
1664 rtx
1665 cselib_subst_to_values (rtx x, enum machine_mode memmode)
1666 {
1667   enum rtx_code code = GET_CODE (x);
1668   const char *fmt = GET_RTX_FORMAT (code);
1669   cselib_val *e;
1670   struct elt_list *l;
1671   rtx copy = x;
1672   int i;
1673
1674   switch (code)
1675     {
1676     case REG:
1677       l = REG_VALUES (REGNO (x));
1678       if (l && l->elt == NULL)
1679         l = l->next;
1680       for (; l; l = l->next)
1681         if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1682           return l->elt->val_rtx;
1683
1684       gcc_unreachable ();
1685
1686     case MEM:
1687       e = cselib_lookup_mem (x, 0);
1688       /* This used to happen for autoincrements, but we deal with them
1689          properly now.  Remove the if stmt for the next release.  */
1690       if (! e)
1691         {
1692           /* Assign a value that doesn't match any other.  */
1693           e = new_cselib_val (next_uid, GET_MODE (x), x);
1694         }
1695       return e->val_rtx;
1696
1697     case CONST_DOUBLE:
1698     case CONST_VECTOR:
1699     case CONST_INT:
1700     case CONST_FIXED:
1701       return x;
1702
1703     case PRE_DEC:
1704     case PRE_INC:
1705       gcc_assert (memmode != VOIDmode);
1706       i = GET_MODE_SIZE (memmode);
1707       if (code == PRE_DEC)
1708         i = -i;
1709       return cselib_subst_to_values (plus_constant (XEXP (x, 0), i),
1710                                      memmode);
1711
1712     case PRE_MODIFY:
1713       gcc_assert (memmode != VOIDmode);
1714       return cselib_subst_to_values (XEXP (x, 1), memmode);
1715
1716     case POST_DEC:
1717     case POST_INC:
1718     case POST_MODIFY:
1719       gcc_assert (memmode != VOIDmode);
1720       return cselib_subst_to_values (XEXP (x, 0), memmode);
1721
1722     default:
1723       break;
1724     }
1725
1726   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1727     {
1728       if (fmt[i] == 'e')
1729         {
1730           rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
1731
1732           if (t != XEXP (x, i))
1733             {
1734               if (x == copy)
1735                 copy = shallow_copy_rtx (x);
1736               XEXP (copy, i) = t;
1737             }
1738         }
1739       else if (fmt[i] == 'E')
1740         {
1741           int j;
1742
1743           for (j = 0; j < XVECLEN (x, i); j++)
1744             {
1745               rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
1746
1747               if (t != XVECEXP (x, i, j))
1748                 {
1749                   if (XVEC (x, i) == XVEC (copy, i))
1750                     {
1751                       if (x == copy)
1752                         copy = shallow_copy_rtx (x);
1753                       XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1754                     }
1755                   XVECEXP (copy, i, j) = t;
1756                 }
1757             }
1758         }
1759     }
1760
1761   return copy;
1762 }
1763
1764 /* Look up the rtl expression X in our tables and return the value it
1765    has.  If CREATE is zero, we return NULL if we don't know the value.
1766    Otherwise, we create a new one if possible, using mode MODE if X
1767    doesn't have a mode (i.e. because it's a constant).  When X is part
1768    of an address, MEMMODE should be the mode of the enclosing MEM if
1769    we're tracking autoinc expressions.  */
1770
1771 static cselib_val *
1772 cselib_lookup_1 (rtx x, enum machine_mode mode,
1773                  int create, enum machine_mode memmode)
1774 {
1775   void **slot;
1776   cselib_val *e;
1777   unsigned int hashval;
1778
1779   if (GET_MODE (x) != VOIDmode)
1780     mode = GET_MODE (x);
1781
1782   if (GET_CODE (x) == VALUE)
1783     return CSELIB_VAL_PTR (x);
1784
1785   if (REG_P (x))
1786     {
1787       struct elt_list *l;
1788       unsigned int i = REGNO (x);
1789
1790       l = REG_VALUES (i);
1791       if (l && l->elt == NULL)
1792         l = l->next;
1793       for (; l; l = l->next)
1794         if (mode == GET_MODE (l->elt->val_rtx))
1795           {
1796             promote_debug_loc (l->elt->locs);
1797             return l->elt;
1798           }
1799
1800       if (! create)
1801         return 0;
1802
1803       if (i < FIRST_PSEUDO_REGISTER)
1804         {
1805           unsigned int n = hard_regno_nregs[i][mode];
1806
1807           if (n > max_value_regs)
1808             max_value_regs = n;
1809         }
1810
1811       e = new_cselib_val (next_uid, GET_MODE (x), x);
1812       e->locs = new_elt_loc_list (e->locs, x);
1813       if (REG_VALUES (i) == 0)
1814         {
1815           /* Maintain the invariant that the first entry of
1816              REG_VALUES, if present, must be the value used to set the
1817              register, or NULL.  */
1818           used_regs[n_used_regs++] = i;
1819           REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
1820         }
1821       REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
1822       slot = cselib_find_slot (x, e->hash, INSERT, memmode);
1823       *slot = e;
1824       return e;
1825     }
1826
1827   if (MEM_P (x))
1828     return cselib_lookup_mem (x, create);
1829
1830   hashval = cselib_hash_rtx (x, create, memmode);
1831   /* Can't even create if hashing is not possible.  */
1832   if (! hashval)
1833     return 0;
1834
1835   slot = cselib_find_slot (wrap_constant (mode, x), hashval,
1836                            create ? INSERT : NO_INSERT, memmode);
1837   if (slot == 0)
1838     return 0;
1839
1840   e = (cselib_val *) *slot;
1841   if (e)
1842     return e;
1843
1844   e = new_cselib_val (hashval, mode, x);
1845
1846   /* We have to fill the slot before calling cselib_subst_to_values:
1847      the hash table is inconsistent until we do so, and
1848      cselib_subst_to_values will need to do lookups.  */
1849   *slot = (void *) e;
1850   e->locs = new_elt_loc_list (e->locs,
1851                               cselib_subst_to_values (x, memmode));
1852   return e;
1853 }
1854
1855 /* Wrapper for cselib_lookup, that indicates X is in INSN.  */
1856
1857 cselib_val *
1858 cselib_lookup_from_insn (rtx x, enum machine_mode mode,
1859                          int create, enum machine_mode memmode, rtx insn)
1860 {
1861   cselib_val *ret;
1862
1863   gcc_assert (!cselib_current_insn);
1864   cselib_current_insn = insn;
1865
1866   ret = cselib_lookup (x, mode, create, memmode);
1867
1868   cselib_current_insn = NULL;
1869
1870   return ret;
1871 }
1872
1873 /* Wrapper for cselib_lookup_1, that logs the lookup result and
1874    maintains invariants related with debug insns.  */
1875
1876 cselib_val *
1877 cselib_lookup (rtx x, enum machine_mode mode,
1878                int create, enum machine_mode memmode)
1879 {
1880   cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
1881
1882   /* ??? Should we return NULL if we're not to create an entry, the
1883      found loc is a debug loc and cselib_current_insn is not DEBUG?
1884      If so, we should also avoid converting val to non-DEBUG; probably
1885      easiest setting cselib_current_insn to NULL before the call
1886      above.  */
1887
1888   if (dump_file && (dump_flags & TDF_CSELIB))
1889     {
1890       fputs ("cselib lookup ", dump_file);
1891       print_inline_rtx (dump_file, x, 2);
1892       fprintf (dump_file, " => %u:%u\n",
1893                ret ? ret->uid : 0,
1894                ret ? ret->hash : 0);
1895     }
1896
1897   return ret;
1898 }
1899
1900 /* Invalidate any entries in reg_values that overlap REGNO.  This is called
1901    if REGNO is changing.  MODE is the mode of the assignment to REGNO, which
1902    is used to determine how many hard registers are being changed.  If MODE
1903    is VOIDmode, then only REGNO is being changed; this is used when
1904    invalidating call clobbered registers across a call.  */
1905
1906 static void
1907 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
1908 {
1909   unsigned int endregno;
1910   unsigned int i;
1911
1912   /* If we see pseudos after reload, something is _wrong_.  */
1913   gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
1914               || reg_renumber[regno] < 0);
1915
1916   /* Determine the range of registers that must be invalidated.  For
1917      pseudos, only REGNO is affected.  For hard regs, we must take MODE
1918      into account, and we must also invalidate lower register numbers
1919      if they contain values that overlap REGNO.  */
1920   if (regno < FIRST_PSEUDO_REGISTER)
1921     {
1922       gcc_assert (mode != VOIDmode);
1923
1924       if (regno < max_value_regs)
1925         i = 0;
1926       else
1927         i = regno - max_value_regs;
1928
1929       endregno = end_hard_regno (mode, regno);
1930     }
1931   else
1932     {
1933       i = regno;
1934       endregno = regno + 1;
1935     }
1936
1937   for (; i < endregno; i++)
1938     {
1939       struct elt_list **l = &REG_VALUES (i);
1940
1941       /* Go through all known values for this reg; if it overlaps the range
1942          we're invalidating, remove the value.  */
1943       while (*l)
1944         {
1945           cselib_val *v = (*l)->elt;
1946           bool had_locs;
1947           rtx setting_insn;
1948           struct elt_loc_list **p;
1949           unsigned int this_last = i;
1950
1951           if (i < FIRST_PSEUDO_REGISTER && v != NULL)
1952             this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
1953
1954           if (this_last < regno || v == NULL
1955               || (v == cfa_base_preserved_val
1956                   && i == cfa_base_preserved_regno))
1957             {
1958               l = &(*l)->next;
1959               continue;
1960             }
1961
1962           /* We have an overlap.  */
1963           if (*l == REG_VALUES (i))
1964             {
1965               /* Maintain the invariant that the first entry of
1966                  REG_VALUES, if present, must be the value used to set
1967                  the register, or NULL.  This is also nice because
1968                  then we won't push the same regno onto user_regs
1969                  multiple times.  */
1970               (*l)->elt = NULL;
1971               l = &(*l)->next;
1972             }
1973           else
1974             unchain_one_elt_list (l);
1975
1976           had_locs = v->locs != NULL;
1977           setting_insn = v->locs ? v->locs->setting_insn : NULL;
1978
1979           /* Now, we clear the mapping from value to reg.  It must exist, so
1980              this code will crash intentionally if it doesn't.  */
1981           for (p = &v->locs; ; p = &(*p)->next)
1982             {
1983               rtx x = (*p)->loc;
1984
1985               if (REG_P (x) && REGNO (x) == i)
1986                 {
1987                   unchain_one_elt_loc_list (p);
1988                   break;
1989                 }
1990             }
1991
1992           if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
1993             {
1994               if (setting_insn && DEBUG_INSN_P (setting_insn))
1995                 n_useless_debug_values++;
1996               else
1997                 n_useless_values++;
1998             }
1999         }
2000     }
2001 }
2002 \f
2003 /* Return 1 if X has a value that can vary even between two
2004    executions of the program.  0 means X can be compared reliably
2005    against certain constants or near-constants.  */
2006
2007 static bool
2008 cselib_rtx_varies_p (const_rtx x ATTRIBUTE_UNUSED, bool from_alias ATTRIBUTE_UNUSED)
2009 {
2010   /* We actually don't need to verify very hard.  This is because
2011      if X has actually changed, we invalidate the memory anyway,
2012      so assume that all common memory addresses are
2013      invariant.  */
2014   return 0;
2015 }
2016
2017 /* Invalidate any locations in the table which are changed because of a
2018    store to MEM_RTX.  If this is called because of a non-const call
2019    instruction, MEM_RTX is (mem:BLK const0_rtx).  */
2020
2021 static void
2022 cselib_invalidate_mem (rtx mem_rtx)
2023 {
2024   cselib_val **vp, *v, *next;
2025   int num_mems = 0;
2026   rtx mem_addr;
2027
2028   mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2029   mem_rtx = canon_rtx (mem_rtx);
2030
2031   vp = &first_containing_mem;
2032   for (v = *vp; v != &dummy_val; v = next)
2033     {
2034       bool has_mem = false;
2035       struct elt_loc_list **p = &v->locs;
2036       bool had_locs = v->locs != NULL;
2037       rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
2038
2039       while (*p)
2040         {
2041           rtx x = (*p)->loc;
2042           cselib_val *addr;
2043           struct elt_list **mem_chain;
2044
2045           /* MEMs may occur in locations only at the top level; below
2046              that every MEM or REG is substituted by its VALUE.  */
2047           if (!MEM_P (x))
2048             {
2049               p = &(*p)->next;
2050               continue;
2051             }
2052           if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
2053               && ! canon_true_dependence (mem_rtx, GET_MODE (mem_rtx), mem_addr,
2054                                           x, NULL_RTX, cselib_rtx_varies_p))
2055             {
2056               has_mem = true;
2057               num_mems++;
2058               p = &(*p)->next;
2059               continue;
2060             }
2061
2062           /* This one overlaps.  */
2063           /* We must have a mapping from this MEM's address to the
2064              value (E).  Remove that, too.  */
2065           addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
2066           mem_chain = &addr->addr_list;
2067           for (;;)
2068             {
2069               if ((*mem_chain)->elt == v)
2070                 {
2071                   unchain_one_elt_list (mem_chain);
2072                   break;
2073                 }
2074
2075               mem_chain = &(*mem_chain)->next;
2076             }
2077
2078           unchain_one_elt_loc_list (p);
2079         }
2080
2081       if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2082         {
2083           if (setting_insn && DEBUG_INSN_P (setting_insn))
2084             n_useless_debug_values++;
2085           else
2086             n_useless_values++;
2087         }
2088
2089       next = v->next_containing_mem;
2090       if (has_mem)
2091         {
2092           *vp = v;
2093           vp = &(*vp)->next_containing_mem;
2094         }
2095       else
2096         v->next_containing_mem = NULL;
2097     }
2098   *vp = &dummy_val;
2099 }
2100
2101 /* Invalidate DEST, which is being assigned to or clobbered.  */
2102
2103 void
2104 cselib_invalidate_rtx (rtx dest)
2105 {
2106   while (GET_CODE (dest) == SUBREG
2107          || GET_CODE (dest) == ZERO_EXTRACT
2108          || GET_CODE (dest) == STRICT_LOW_PART)
2109     dest = XEXP (dest, 0);
2110
2111   if (REG_P (dest))
2112     cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
2113   else if (MEM_P (dest))
2114     cselib_invalidate_mem (dest);
2115 }
2116
2117 /* A wrapper for cselib_invalidate_rtx to be called via note_stores.  */
2118
2119 static void
2120 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
2121                                    void *data ATTRIBUTE_UNUSED)
2122 {
2123   cselib_invalidate_rtx (dest);
2124 }
2125
2126 /* Record the result of a SET instruction.  DEST is being set; the source
2127    contains the value described by SRC_ELT.  If DEST is a MEM, DEST_ADDR_ELT
2128    describes its address.  */
2129
2130 static void
2131 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
2132 {
2133   int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
2134
2135   if (src_elt == 0 || side_effects_p (dest))
2136     return;
2137
2138   if (dreg >= 0)
2139     {
2140       if (dreg < FIRST_PSEUDO_REGISTER)
2141         {
2142           unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
2143
2144           if (n > max_value_regs)
2145             max_value_regs = n;
2146         }
2147
2148       if (REG_VALUES (dreg) == 0)
2149         {
2150           used_regs[n_used_regs++] = dreg;
2151           REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2152         }
2153       else
2154         {
2155           /* The register should have been invalidated.  */
2156           gcc_assert (REG_VALUES (dreg)->elt == 0);
2157           REG_VALUES (dreg)->elt = src_elt;
2158         }
2159
2160       if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2161         n_useless_values--;
2162       src_elt->locs = new_elt_loc_list (src_elt->locs, dest);
2163     }
2164   else if (MEM_P (dest) && dest_addr_elt != 0
2165            && cselib_record_memory)
2166     {
2167       if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
2168         n_useless_values--;
2169       add_mem_for_addr (dest_addr_elt, src_elt, dest);
2170     }
2171 }
2172
2173 /* There is no good way to determine how many elements there can be
2174    in a PARALLEL.  Since it's fairly cheap, use a really large number.  */
2175 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2176
2177 struct cselib_record_autoinc_data
2178 {
2179   struct cselib_set *sets;
2180   int n_sets;
2181 };
2182
2183 /* Callback for for_each_inc_dec.  Records in ARG the SETs implied by
2184    autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST.  */
2185
2186 static int
2187 cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2188                           rtx dest, rtx src, rtx srcoff, void *arg)
2189 {
2190   struct cselib_record_autoinc_data *data;
2191   data = (struct cselib_record_autoinc_data *)arg;
2192
2193   data->sets[data->n_sets].dest = dest;
2194
2195   if (srcoff)
2196     data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2197   else
2198     data->sets[data->n_sets].src = src;
2199
2200   data->n_sets++;
2201
2202   return -1;
2203 }
2204
2205 /* Record the effects of any sets and autoincs in INSN.  */
2206 static void
2207 cselib_record_sets (rtx insn)
2208 {
2209   int n_sets = 0;
2210   int i;
2211   struct cselib_set sets[MAX_SETS];
2212   rtx body = PATTERN (insn);
2213   rtx cond = 0;
2214   int n_sets_before_autoinc;
2215   struct cselib_record_autoinc_data data;
2216
2217   body = PATTERN (insn);
2218   if (GET_CODE (body) == COND_EXEC)
2219     {
2220       cond = COND_EXEC_TEST (body);
2221       body = COND_EXEC_CODE (body);
2222     }
2223
2224   /* Find all sets.  */
2225   if (GET_CODE (body) == SET)
2226     {
2227       sets[0].src = SET_SRC (body);
2228       sets[0].dest = SET_DEST (body);
2229       n_sets = 1;
2230     }
2231   else if (GET_CODE (body) == PARALLEL)
2232     {
2233       /* Look through the PARALLEL and record the values being
2234          set, if possible.  Also handle any CLOBBERs.  */
2235       for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2236         {
2237           rtx x = XVECEXP (body, 0, i);
2238
2239           if (GET_CODE (x) == SET)
2240             {
2241               sets[n_sets].src = SET_SRC (x);
2242               sets[n_sets].dest = SET_DEST (x);
2243               n_sets++;
2244             }
2245         }
2246     }
2247
2248   if (n_sets == 1
2249       && MEM_P (sets[0].src)
2250       && !cselib_record_memory
2251       && MEM_READONLY_P (sets[0].src))
2252     {
2253       rtx note = find_reg_equal_equiv_note (insn);
2254
2255       if (note && CONSTANT_P (XEXP (note, 0)))
2256         sets[0].src = XEXP (note, 0);
2257     }
2258
2259   data.sets = sets;
2260   data.n_sets = n_sets_before_autoinc = n_sets;
2261   for_each_inc_dec (&insn, cselib_record_autoinc_cb, &data);
2262   n_sets = data.n_sets;
2263
2264   /* Look up the values that are read.  Do this before invalidating the
2265      locations that are written.  */
2266   for (i = 0; i < n_sets; i++)
2267     {
2268       rtx dest = sets[i].dest;
2269
2270       /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2271          the low part after invalidating any knowledge about larger modes.  */
2272       if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2273         sets[i].dest = dest = XEXP (dest, 0);
2274
2275       /* We don't know how to record anything but REG or MEM.  */
2276       if (REG_P (dest)
2277           || (MEM_P (dest) && cselib_record_memory))
2278         {
2279           rtx src = sets[i].src;
2280           if (cond)
2281             src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
2282           sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
2283           if (MEM_P (dest))
2284             {
2285               enum machine_mode address_mode
2286                 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (dest));
2287
2288               sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
2289                                                      address_mode, 1,
2290                                                      GET_MODE (dest));
2291             }
2292           else
2293             sets[i].dest_addr_elt = 0;
2294         }
2295     }
2296
2297   if (cselib_record_sets_hook)
2298     cselib_record_sets_hook (insn, sets, n_sets);
2299
2300   /* Invalidate all locations written by this insn.  Note that the elts we
2301      looked up in the previous loop aren't affected, just some of their
2302      locations may go away.  */
2303   note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
2304
2305   for (i = n_sets_before_autoinc; i < n_sets; i++)
2306     cselib_invalidate_rtx (sets[i].dest);
2307
2308   /* If this is an asm, look for duplicate sets.  This can happen when the
2309      user uses the same value as an output multiple times.  This is valid
2310      if the outputs are not actually used thereafter.  Treat this case as
2311      if the value isn't actually set.  We do this by smashing the destination
2312      to pc_rtx, so that we won't record the value later.  */
2313   if (n_sets >= 2 && asm_noperands (body) >= 0)
2314     {
2315       for (i = 0; i < n_sets; i++)
2316         {
2317           rtx dest = sets[i].dest;
2318           if (REG_P (dest) || MEM_P (dest))
2319             {
2320               int j;
2321               for (j = i + 1; j < n_sets; j++)
2322                 if (rtx_equal_p (dest, sets[j].dest))
2323                   {
2324                     sets[i].dest = pc_rtx;
2325                     sets[j].dest = pc_rtx;
2326                   }
2327             }
2328         }
2329     }
2330
2331   /* Now enter the equivalences in our tables.  */
2332   for (i = 0; i < n_sets; i++)
2333     {
2334       rtx dest = sets[i].dest;
2335       if (REG_P (dest)
2336           || (MEM_P (dest) && cselib_record_memory))
2337         cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2338     }
2339 }
2340
2341 /* Record the effects of INSN.  */
2342
2343 void
2344 cselib_process_insn (rtx insn)
2345 {
2346   int i;
2347   rtx x;
2348
2349   cselib_current_insn = insn;
2350
2351   /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp.  */
2352   if (LABEL_P (insn)
2353       || (CALL_P (insn)
2354           && find_reg_note (insn, REG_SETJMP, NULL))
2355       || (NONJUMP_INSN_P (insn)
2356           && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
2357           && MEM_VOLATILE_P (PATTERN (insn))))
2358     {
2359       cselib_reset_table (next_uid);
2360       cselib_current_insn = NULL_RTX;
2361       return;
2362     }
2363
2364   if (! INSN_P (insn))
2365     {
2366       cselib_current_insn = NULL_RTX;
2367       return;
2368     }
2369
2370   /* If this is a call instruction, forget anything stored in a
2371      call clobbered register, or, if this is not a const call, in
2372      memory.  */
2373   if (CALL_P (insn))
2374     {
2375       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
2376         if (call_used_regs[i]
2377             || (REG_VALUES (i) && REG_VALUES (i)->elt
2378                 && HARD_REGNO_CALL_PART_CLOBBERED (i,
2379                       GET_MODE (REG_VALUES (i)->elt->val_rtx))))
2380           cselib_invalidate_regno (i, reg_raw_mode[i]);
2381
2382       /* Since it is not clear how cselib is going to be used, be
2383          conservative here and treat looping pure or const functions
2384          as if they were regular functions.  */
2385       if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2386           || !(RTL_CONST_OR_PURE_CALL_P (insn)))
2387         cselib_invalidate_mem (callmem);
2388     }
2389
2390   cselib_record_sets (insn);
2391
2392   /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2393      after we have processed the insn.  */
2394   if (CALL_P (insn))
2395     for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2396       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
2397         cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
2398
2399   cselib_current_insn = NULL_RTX;
2400
2401   if (n_useless_values > MAX_USELESS_VALUES
2402       /* remove_useless_values is linear in the hash table size.  Avoid
2403          quadratic behavior for very large hashtables with very few
2404          useless elements.  */
2405       && ((unsigned int)n_useless_values
2406           > (cselib_hash_table->n_elements
2407              - cselib_hash_table->n_deleted
2408              - n_debug_values) / 4))
2409     remove_useless_values ();
2410 }
2411
2412 /* Initialize cselib for one pass.  The caller must also call
2413    init_alias_analysis.  */
2414
2415 void
2416 cselib_init (int record_what)
2417 {
2418   elt_list_pool = create_alloc_pool ("elt_list",
2419                                      sizeof (struct elt_list), 10);
2420   elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
2421                                          sizeof (struct elt_loc_list), 10);
2422   cselib_val_pool = create_alloc_pool ("cselib_val_list",
2423                                        sizeof (cselib_val), 10);
2424   value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
2425   cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2426   cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
2427
2428   /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2429      see canon_true_dependence.  This is only created once.  */
2430   if (! callmem)
2431     callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
2432
2433   cselib_nregs = max_reg_num ();
2434
2435   /* We preserve reg_values to allow expensive clearing of the whole thing.
2436      Reallocate it however if it happens to be too large.  */
2437   if (!reg_values || reg_values_size < cselib_nregs
2438       || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
2439     {
2440       if (reg_values)
2441         free (reg_values);
2442       /* Some space for newly emit instructions so we don't end up
2443          reallocating in between passes.  */
2444       reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
2445       reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
2446     }
2447   used_regs = XNEWVEC (unsigned int, cselib_nregs);
2448   n_used_regs = 0;
2449   cselib_hash_table = htab_create (31, get_value_hash,
2450                                    entry_and_rtx_equal_p, NULL);
2451   next_uid = 1;
2452 }
2453
2454 /* Called when the current user is done with cselib.  */
2455
2456 void
2457 cselib_finish (void)
2458 {
2459   cselib_discard_hook = NULL;
2460   cselib_preserve_constants = false;
2461   cfa_base_preserved_val = NULL;
2462   cfa_base_preserved_regno = INVALID_REGNUM;
2463   free_alloc_pool (elt_list_pool);
2464   free_alloc_pool (elt_loc_list_pool);
2465   free_alloc_pool (cselib_val_pool);
2466   free_alloc_pool (value_pool);
2467   cselib_clear_table ();
2468   htab_delete (cselib_hash_table);
2469   free (used_regs);
2470   used_regs = 0;
2471   cselib_hash_table = 0;
2472   n_useless_values = 0;
2473   n_useless_debug_values = 0;
2474   n_debug_values = 0;
2475   next_uid = 0;
2476 }
2477
2478 /* Dump the cselib_val *X to FILE *info.  */
2479
2480 static int
2481 dump_cselib_val (void **x, void *info)
2482 {
2483   cselib_val *v = (cselib_val *)*x;
2484   FILE *out = (FILE *)info;
2485   bool need_lf = true;
2486
2487   print_inline_rtx (out, v->val_rtx, 0);
2488
2489   if (v->locs)
2490     {
2491       struct elt_loc_list *l = v->locs;
2492       if (need_lf)
2493         {
2494           fputc ('\n', out);
2495           need_lf = false;
2496         }
2497       fputs (" locs:", out);
2498       do
2499         {
2500           fprintf (out, "\n  from insn %i ",
2501                    INSN_UID (l->setting_insn));
2502           print_inline_rtx (out, l->loc, 4);
2503         }
2504       while ((l = l->next));
2505       fputc ('\n', out);
2506     }
2507   else
2508     {
2509       fputs (" no locs", out);
2510       need_lf = true;
2511     }
2512
2513   if (v->addr_list)
2514     {
2515       struct elt_list *e = v->addr_list;
2516       if (need_lf)
2517         {
2518           fputc ('\n', out);
2519           need_lf = false;
2520         }
2521       fputs (" addr list:", out);
2522       do
2523         {
2524           fputs ("\n  ", out);
2525           print_inline_rtx (out, e->elt->val_rtx, 2);
2526         }
2527       while ((e = e->next));
2528       fputc ('\n', out);
2529     }
2530   else
2531     {
2532       fputs (" no addrs", out);
2533       need_lf = true;
2534     }
2535
2536   if (v->next_containing_mem == &dummy_val)
2537     fputs (" last mem\n", out);
2538   else if (v->next_containing_mem)
2539     {
2540       fputs (" next mem ", out);
2541       print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2542       fputc ('\n', out);
2543     }
2544   else if (need_lf)
2545     fputc ('\n', out);
2546
2547   return 1;
2548 }
2549
2550 /* Dump to OUT everything in the CSELIB table.  */
2551
2552 void
2553 dump_cselib_table (FILE *out)
2554 {
2555   fprintf (out, "cselib hash table:\n");
2556   htab_traverse (cselib_hash_table, dump_cselib_val, out);
2557   if (first_containing_mem != &dummy_val)
2558     {
2559       fputs ("first mem ", out);
2560       print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2561       fputc ('\n', out);
2562     }
2563   fprintf (out, "next uid %i\n", next_uid);
2564 }
2565
2566 #include "gt-cselib.h"