OSDN Git Service

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