OSDN Git Service

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