OSDN Git Service

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