OSDN Git Service

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