OSDN Git Service

Remove libcall notes.
[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 Free Software Foundation, Inc.
4
5 This file is part of GCC.
6
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
11
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
15 for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3.  If not see
19 <http://www.gnu.org/licenses/>.  */
20
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25
26 #include "rtl.h"
27 #include "tm_p.h"
28 #include "regs.h"
29 #include "hard-reg-set.h"
30 #include "flags.h"
31 #include "real.h"
32 #include "insn-config.h"
33 #include "recog.h"
34 #include "function.h"
35 #include "emit-rtl.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "ggc.h"
39 #include "hashtab.h"
40 #include "cselib.h"
41 #include "params.h"
42 #include "alloc-pool.h"
43 #include "target.h"
44
45 static bool cselib_record_memory;
46 static int entry_and_rtx_equal_p (const void *, const void *);
47 static hashval_t get_value_hash (const void *);
48 static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
49 static struct elt_loc_list *new_elt_loc_list (struct elt_loc_list *, rtx);
50 static void unchain_one_value (cselib_val *);
51 static void unchain_one_elt_list (struct elt_list **);
52 static void unchain_one_elt_loc_list (struct elt_loc_list **);
53 static int discard_useless_locs (void **, void *);
54 static int discard_useless_values (void **, void *);
55 static void remove_useless_values (void);
56 static rtx wrap_constant (enum machine_mode, rtx);
57 static unsigned int cselib_hash_rtx (rtx, int);
58 static cselib_val *new_cselib_val (unsigned int, enum machine_mode);
59 static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
60 static cselib_val *cselib_lookup_mem (rtx, int);
61 static void cselib_invalidate_regno (unsigned int, enum machine_mode);
62 static void cselib_invalidate_mem (rtx);
63 static void cselib_record_set (rtx, cselib_val *, cselib_val *);
64 static void cselib_record_sets (rtx);
65
66 /* There are three ways in which cselib can look up an rtx:
67    - for a REG, the reg_values table (which is indexed by regno) is used
68    - for a MEM, we recursively look up its address and then follow the
69      addr_list of that value
70    - for everything else, we compute a hash value and go through the hash
71      table.  Since different rtx's can still have the same hash value,
72      this involves walking the table entries for a given value and comparing
73      the locations of the entries with the rtx we are looking up.  */
74
75 /* A table that enables us to look up elts by their value.  */
76 static htab_t cselib_hash_table;
77
78 /* This is a global so we don't have to pass this through every function.
79    It is used in new_elt_loc_list to set SETTING_INSN.  */
80 static rtx cselib_current_insn;
81
82 /* Every new unknown value gets a unique number.  */
83 static unsigned int next_unknown_value;
84
85 /* The number of registers we had when the varrays were last resized.  */
86 static unsigned int cselib_nregs;
87
88 /* Count values without known locations.  Whenever this grows too big, we
89    remove these useless values from the table.  */
90 static int n_useless_values;
91
92 /* Number of useless values before we remove them from the hash table.  */
93 #define MAX_USELESS_VALUES 32
94
95 /* This table maps from register number to values.  It does not
96    contain pointers to cselib_val structures, but rather elt_lists.
97    The purpose is to be able to refer to the same register in
98    different modes.  The first element of the list defines the mode in
99    which the register was set; if the mode is unknown or the value is
100    no longer valid in that mode, ELT will be NULL for the first
101    element.  */
102 static struct elt_list **reg_values;
103 static unsigned int reg_values_size;
104 #define REG_VALUES(i) reg_values[i]
105
106 /* The largest number of hard regs used by any entry added to the
107    REG_VALUES table.  Cleared on each cselib_clear_table() invocation.  */
108 static unsigned int max_value_regs;
109
110 /* Here the set of indices I with REG_VALUES(I) != 0 is saved.  This is used
111    in cselib_clear_table() for fast emptying.  */
112 static unsigned int *used_regs;
113 static unsigned int n_used_regs;
114
115 /* We pass this to cselib_invalidate_mem to invalidate all of
116    memory for a non-const call instruction.  */
117 static GTY(()) rtx callmem;
118
119 /* Set by discard_useless_locs if it deleted the last location of any
120    value.  */
121 static int values_became_useless;
122
123 /* Used as stop element of the containing_mem list so we can check
124    presence in the list by checking the next pointer.  */
125 static cselib_val dummy_val;
126
127 /* Used to list all values that contain memory reference.
128    May or may not contain the useless values - the list is compacted
129    each time memory is invalidated.  */
130 static cselib_val *first_containing_mem = &dummy_val;
131 static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
132
133 /* If nonnull, cselib will call this function before freeing useless
134    VALUEs.  A VALUE is deemed useless if its "locs" field is null.  */
135 void (*cselib_discard_hook) (cselib_val *);
136 \f
137
138 /* Allocate a struct elt_list and fill in its two elements with the
139    arguments.  */
140
141 static inline struct elt_list *
142 new_elt_list (struct elt_list *next, cselib_val *elt)
143 {
144   struct elt_list *el;
145   el = pool_alloc (elt_list_pool);
146   el->next = next;
147   el->elt = elt;
148   return el;
149 }
150
151 /* Allocate a struct elt_loc_list and fill in its two elements with the
152    arguments.  */
153
154 static inline struct elt_loc_list *
155 new_elt_loc_list (struct elt_loc_list *next, rtx loc)
156 {
157   struct elt_loc_list *el;
158   el = pool_alloc (elt_loc_list_pool);
159   el->next = next;
160   el->loc = loc;
161   el->setting_insn = cselib_current_insn;
162   return el;
163 }
164
165 /* The elt_list at *PL is no longer needed.  Unchain it and free its
166    storage.  */
167
168 static inline void
169 unchain_one_elt_list (struct elt_list **pl)
170 {
171   struct elt_list *l = *pl;
172
173   *pl = l->next;
174   pool_free (elt_list_pool, l);
175 }
176
177 /* Likewise for elt_loc_lists.  */
178
179 static void
180 unchain_one_elt_loc_list (struct elt_loc_list **pl)
181 {
182   struct elt_loc_list *l = *pl;
183
184   *pl = l->next;
185   pool_free (elt_loc_list_pool, l);
186 }
187
188 /* Likewise for cselib_vals.  This also frees the addr_list associated with
189    V.  */
190
191 static void
192 unchain_one_value (cselib_val *v)
193 {
194   while (v->addr_list)
195     unchain_one_elt_list (&v->addr_list);
196
197   pool_free (cselib_val_pool, v);
198 }
199
200 /* Remove all entries from the hash table.  Also used during
201    initialization.  If CLEAR_ALL isn't set, then only clear the entries
202    which are known to have been used.  */
203
204 void
205 cselib_clear_table (void)
206 {
207   unsigned int i;
208
209   for (i = 0; i < n_used_regs; i++)
210     REG_VALUES (used_regs[i]) = 0;
211
212   max_value_regs = 0;
213
214   n_used_regs = 0;
215
216   htab_empty (cselib_hash_table);
217
218   n_useless_values = 0;
219
220   next_unknown_value = 0;
221
222   first_containing_mem = &dummy_val;
223 }
224
225 /* The equality test for our hash table.  The first argument ENTRY is a table
226    element (i.e. a cselib_val), while the second arg X is an rtx.  We know
227    that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
228    CONST of an appropriate mode.  */
229
230 static int
231 entry_and_rtx_equal_p (const void *entry, const void *x_arg)
232 {
233   struct elt_loc_list *l;
234   const cselib_val *const v = (const cselib_val *) entry;
235   rtx x = (rtx) x_arg;
236   enum machine_mode mode = GET_MODE (x);
237
238   gcc_assert (GET_CODE (x) != CONST_INT && GET_CODE (x) != CONST_FIXED
239               && (mode != VOIDmode || GET_CODE (x) != CONST_DOUBLE));
240   
241   if (mode != GET_MODE (v->val_rtx))
242     return 0;
243
244   /* Unwrap X if necessary.  */
245   if (GET_CODE (x) == CONST
246       && (GET_CODE (XEXP (x, 0)) == CONST_INT
247           || GET_CODE (XEXP (x, 0)) == CONST_FIXED
248           || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE))
249     x = XEXP (x, 0);
250
251   /* We don't guarantee that distinct rtx's have different hash values,
252      so we need to do a comparison.  */
253   for (l = v->locs; l; l = l->next)
254     if (rtx_equal_for_cselib_p (l->loc, x))
255       return 1;
256
257   return 0;
258 }
259
260 /* The hash function for our hash table.  The value is always computed with
261    cselib_hash_rtx when adding an element; this function just extracts the
262    hash value from a cselib_val structure.  */
263
264 static hashval_t
265 get_value_hash (const void *entry)
266 {
267   const cselib_val *const v = (const cselib_val *) entry;
268   return v->value;
269 }
270
271 /* Return true if X contains a VALUE rtx.  If ONLY_USELESS is set, we
272    only return true for values which point to a cselib_val whose value
273    element has been set to zero, which implies the cselib_val will be
274    removed.  */
275
276 int
277 references_value_p (const_rtx x, int only_useless)
278 {
279   const enum rtx_code code = GET_CODE (x);
280   const char *fmt = GET_RTX_FORMAT (code);
281   int i, j;
282
283   if (GET_CODE (x) == VALUE
284       && (! only_useless || CSELIB_VAL_PTR (x)->locs == 0))
285     return 1;
286
287   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
288     {
289       if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
290         return 1;
291       else if (fmt[i] == 'E')
292         for (j = 0; j < XVECLEN (x, i); j++)
293           if (references_value_p (XVECEXP (x, i, j), only_useless))
294             return 1;
295     }
296
297   return 0;
298 }
299
300 /* For all locations found in X, delete locations that reference useless
301    values (i.e. values without any location).  Called through
302    htab_traverse.  */
303
304 static int
305 discard_useless_locs (void **x, void *info ATTRIBUTE_UNUSED)
306 {
307   cselib_val *v = (cselib_val *)*x;
308   struct elt_loc_list **p = &v->locs;
309   int had_locs = v->locs != 0;
310
311   while (*p)
312     {
313       if (references_value_p ((*p)->loc, 1))
314         unchain_one_elt_loc_list (p);
315       else
316         p = &(*p)->next;
317     }
318
319   if (had_locs && v->locs == 0)
320     {
321       n_useless_values++;
322       values_became_useless = 1;
323     }
324   return 1;
325 }
326
327 /* If X is a value with no locations, remove it from the hashtable.  */
328
329 static int
330 discard_useless_values (void **x, void *info ATTRIBUTE_UNUSED)
331 {
332   cselib_val *v = (cselib_val *)*x;
333
334   if (v->locs == 0)
335     {
336       if (cselib_discard_hook)
337         cselib_discard_hook (v);
338
339       CSELIB_VAL_PTR (v->val_rtx) = NULL;
340       htab_clear_slot (cselib_hash_table, x);
341       unchain_one_value (v);
342       n_useless_values--;
343     }
344
345   return 1;
346 }
347
348 /* Clean out useless values (i.e. those which no longer have locations
349    associated with them) from the hash table.  */
350
351 static void
352 remove_useless_values (void)
353 {
354   cselib_val **p, *v;
355   /* First pass: eliminate locations that reference the value.  That in
356      turn can make more values useless.  */
357   do
358     {
359       values_became_useless = 0;
360       htab_traverse (cselib_hash_table, discard_useless_locs, 0);
361     }
362   while (values_became_useless);
363
364   /* Second pass: actually remove the values.  */
365
366   p = &first_containing_mem;
367   for (v = *p; v != &dummy_val; v = v->next_containing_mem)
368     if (v->locs)
369       {
370         *p = v;
371         p = &(*p)->next_containing_mem;
372       }
373   *p = &dummy_val;
374
375   htab_traverse (cselib_hash_table, discard_useless_values, 0);
376
377   gcc_assert (!n_useless_values);
378 }
379
380 /* Return the mode in which a register was last set.  If X is not a
381    register, return its mode.  If the mode in which the register was
382    set is not known, or the value was already clobbered, return
383    VOIDmode.  */
384
385 enum machine_mode
386 cselib_reg_set_mode (const_rtx x)
387 {
388   if (!REG_P (x))
389     return GET_MODE (x);
390
391   if (REG_VALUES (REGNO (x)) == NULL
392       || REG_VALUES (REGNO (x))->elt == NULL)
393     return VOIDmode;
394
395   return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
396 }
397
398 /* Return nonzero if we can prove that X and Y contain the same value, taking
399    our gathered information into account.  */
400
401 int
402 rtx_equal_for_cselib_p (rtx x, rtx y)
403 {
404   enum rtx_code code;
405   const char *fmt;
406   int i;
407
408   if (REG_P (x) || MEM_P (x))
409     {
410       cselib_val *e = cselib_lookup (x, GET_MODE (x), 0);
411
412       if (e)
413         x = e->val_rtx;
414     }
415
416   if (REG_P (y) || MEM_P (y))
417     {
418       cselib_val *e = cselib_lookup (y, GET_MODE (y), 0);
419
420       if (e)
421         y = e->val_rtx;
422     }
423
424   if (x == y)
425     return 1;
426
427   if (GET_CODE (x) == VALUE && GET_CODE (y) == VALUE)
428     return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
429
430   if (GET_CODE (x) == VALUE)
431     {
432       cselib_val *e = CSELIB_VAL_PTR (x);
433       struct elt_loc_list *l;
434
435       for (l = e->locs; l; l = l->next)
436         {
437           rtx t = l->loc;
438
439           /* Avoid infinite recursion.  */
440           if (REG_P (t) || MEM_P (t))
441             continue;
442           else if (rtx_equal_for_cselib_p (t, y))
443             return 1;
444         }
445
446       return 0;
447     }
448
449   if (GET_CODE (y) == VALUE)
450     {
451       cselib_val *e = CSELIB_VAL_PTR (y);
452       struct elt_loc_list *l;
453
454       for (l = e->locs; l; l = l->next)
455         {
456           rtx t = l->loc;
457
458           if (REG_P (t) || MEM_P (t))
459             continue;
460           else if (rtx_equal_for_cselib_p (x, t))
461             return 1;
462         }
463
464       return 0;
465     }
466
467   if (GET_CODE (x) != GET_CODE (y) || GET_MODE (x) != GET_MODE (y))
468     return 0;
469
470   /* These won't be handled correctly by the code below.  */
471   switch (GET_CODE (x))
472     {
473     case CONST_DOUBLE:
474     case CONST_FIXED:
475       return 0;
476
477     case LABEL_REF:
478       return XEXP (x, 0) == XEXP (y, 0);
479
480     default:
481       break;
482     }
483
484   code = GET_CODE (x);
485   fmt = GET_RTX_FORMAT (code);
486
487   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
488     {
489       int j;
490
491       switch (fmt[i])
492         {
493         case 'w':
494           if (XWINT (x, i) != XWINT (y, i))
495             return 0;
496           break;
497
498         case 'n':
499         case 'i':
500           if (XINT (x, i) != XINT (y, i))
501             return 0;
502           break;
503
504         case 'V':
505         case 'E':
506           /* Two vectors must have the same length.  */
507           if (XVECLEN (x, i) != XVECLEN (y, i))
508             return 0;
509
510           /* And the corresponding elements must match.  */
511           for (j = 0; j < XVECLEN (x, i); j++)
512             if (! rtx_equal_for_cselib_p (XVECEXP (x, i, j),
513                                           XVECEXP (y, i, j)))
514               return 0;
515           break;
516
517         case 'e':
518           if (i == 1
519               && targetm.commutative_p (x, UNKNOWN)
520               && rtx_equal_for_cselib_p (XEXP (x, 1), XEXP (y, 0))
521               && rtx_equal_for_cselib_p (XEXP (x, 0), XEXP (y, 1)))
522             return 1;
523           if (! rtx_equal_for_cselib_p (XEXP (x, i), XEXP (y, i)))
524             return 0;
525           break;
526
527         case 'S':
528         case 's':
529           if (strcmp (XSTR (x, i), XSTR (y, i)))
530             return 0;
531           break;
532
533         case 'u':
534           /* These are just backpointers, so they don't matter.  */
535           break;
536
537         case '0':
538         case 't':
539           break;
540
541           /* It is believed that rtx's at this level will never
542              contain anything but integers and other rtx's,
543              except for within LABEL_REFs and SYMBOL_REFs.  */
544         default:
545           gcc_unreachable ();
546         }
547     }
548   return 1;
549 }
550
551 /* We need to pass down the mode of constants through the hash table
552    functions.  For that purpose, wrap them in a CONST of the appropriate
553    mode.  */
554 static rtx
555 wrap_constant (enum machine_mode mode, rtx x)
556 {
557   if (GET_CODE (x) != CONST_INT && GET_CODE (x) != CONST_FIXED
558       && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode))
559     return x;
560   gcc_assert (mode != VOIDmode);
561   return gen_rtx_CONST (mode, x);
562 }
563
564 /* Hash an rtx.  Return 0 if we couldn't hash the rtx.
565    For registers and memory locations, we look up their cselib_val structure
566    and return its VALUE element.
567    Possible reasons for return 0 are: the object is volatile, or we couldn't
568    find a register or memory location in the table and CREATE is zero.  If
569    CREATE is nonzero, table elts are created for regs and mem.
570    N.B. this hash function returns the same hash value for RTXes that
571    differ only in the order of operands, thus it is suitable for comparisons
572    that take commutativity into account.
573    If we wanted to also support associative rules, we'd have to use a different
574    strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
575    We used to have a MODE argument for hashing for CONST_INTs, but that
576    didn't make sense, since it caused spurious hash differences between
577     (set (reg:SI 1) (const_int))
578     (plus:SI (reg:SI 2) (reg:SI 1))
579    and
580     (plus:SI (reg:SI 2) (const_int))
581    If the mode is important in any context, it must be checked specifically
582    in a comparison anyway, since relying on hash differences is unsafe.  */
583
584 static unsigned int
585 cselib_hash_rtx (rtx x, int create)
586 {
587   cselib_val *e;
588   int i, j;
589   enum rtx_code code;
590   const char *fmt;
591   unsigned int hash = 0;
592
593   code = GET_CODE (x);
594   hash += (unsigned) code + (unsigned) GET_MODE (x);
595
596   switch (code)
597     {
598     case MEM:
599     case REG:
600       e = cselib_lookup (x, GET_MODE (x), create);
601       if (! e)
602         return 0;
603
604       return e->value;
605
606     case CONST_INT:
607       hash += ((unsigned) CONST_INT << 7) + INTVAL (x);
608       return hash ? hash : (unsigned int) CONST_INT;
609
610     case CONST_DOUBLE:
611       /* This is like the general case, except that it only counts
612          the integers representing the constant.  */
613       hash += (unsigned) code + (unsigned) GET_MODE (x);
614       if (GET_MODE (x) != VOIDmode)
615         hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
616       else
617         hash += ((unsigned) CONST_DOUBLE_LOW (x)
618                  + (unsigned) CONST_DOUBLE_HIGH (x));
619       return hash ? hash : (unsigned int) CONST_DOUBLE;
620
621     case CONST_FIXED:
622       hash += (unsigned int) code + (unsigned int) GET_MODE (x);
623       hash += fixed_hash (CONST_FIXED_VALUE (x));
624       return hash ? hash : (unsigned int) CONST_FIXED;
625
626     case CONST_VECTOR:
627       {
628         int units;
629         rtx elt;
630
631         units = CONST_VECTOR_NUNITS (x);
632
633         for (i = 0; i < units; ++i)
634           {
635             elt = CONST_VECTOR_ELT (x, i);
636             hash += cselib_hash_rtx (elt, 0);
637           }
638
639         return hash;
640       }
641
642       /* Assume there is only one rtx object for any given label.  */
643     case LABEL_REF:
644       /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
645          differences and differences between each stage's debugging dumps.  */
646       hash += (((unsigned int) LABEL_REF << 7)
647                + CODE_LABEL_NUMBER (XEXP (x, 0)));
648       return hash ? hash : (unsigned int) LABEL_REF;
649
650     case SYMBOL_REF:
651       {
652         /* Don't hash on the symbol's address to avoid bootstrap differences.
653            Different hash values may cause expressions to be recorded in
654            different orders and thus different registers to be used in the
655            final assembler.  This also avoids differences in the dump files
656            between various stages.  */
657         unsigned int h = 0;
658         const unsigned char *p = (const unsigned char *) XSTR (x, 0);
659
660         while (*p)
661           h += (h << 7) + *p++; /* ??? revisit */
662
663         hash += ((unsigned int) SYMBOL_REF << 7) + h;
664         return hash ? hash : (unsigned int) SYMBOL_REF;
665       }
666
667     case PRE_DEC:
668     case PRE_INC:
669     case POST_DEC:
670     case POST_INC:
671     case POST_MODIFY:
672     case PRE_MODIFY:
673     case PC:
674     case CC0:
675     case CALL:
676     case UNSPEC_VOLATILE:
677       return 0;
678
679     case ASM_OPERANDS:
680       if (MEM_VOLATILE_P (x))
681         return 0;
682
683       break;
684
685     default:
686       break;
687     }
688
689   i = GET_RTX_LENGTH (code) - 1;
690   fmt = GET_RTX_FORMAT (code);
691   for (; i >= 0; i--)
692     {
693       switch (fmt[i])
694         {
695         case 'e':
696           {
697             rtx tem = XEXP (x, i);
698             unsigned int tem_hash = cselib_hash_rtx (tem, create);
699             
700             if (tem_hash == 0)
701               return 0;
702             
703             hash += tem_hash;
704           }
705           break;
706         case 'E':
707           for (j = 0; j < XVECLEN (x, i); j++)
708             {
709               unsigned int tem_hash
710                 = cselib_hash_rtx (XVECEXP (x, i, j), create);
711               
712               if (tem_hash == 0)
713                 return 0;
714               
715               hash += tem_hash;
716             }
717           break;
718
719         case 's':
720           {
721             const unsigned char *p = (const unsigned char *) XSTR (x, i);
722             
723             if (p)
724               while (*p)
725                 hash += *p++;
726             break;
727           }
728           
729         case 'i':
730           hash += XINT (x, i);
731           break;
732
733         case '0':
734         case 't':
735           /* unused */
736           break;
737           
738         default:
739           gcc_unreachable ();
740         }
741     }
742
743   return hash ? hash : 1 + (unsigned int) GET_CODE (x);
744 }
745
746 /* Create a new value structure for VALUE and initialize it.  The mode of the
747    value is MODE.  */
748
749 static inline cselib_val *
750 new_cselib_val (unsigned int value, enum machine_mode mode)
751 {
752   cselib_val *e = pool_alloc (cselib_val_pool);
753
754   gcc_assert (value);
755
756   e->value = value;
757   /* We use an alloc pool to allocate this RTL construct because it
758      accounts for about 8% of the overall memory usage.  We know
759      precisely when we can have VALUE RTXen (when cselib is active)
760      so we don't need to put them in garbage collected memory.
761      ??? Why should a VALUE be an RTX in the first place?  */
762   e->val_rtx = pool_alloc (value_pool);
763   memset (e->val_rtx, 0, RTX_HDR_SIZE);
764   PUT_CODE (e->val_rtx, VALUE);
765   PUT_MODE (e->val_rtx, mode);
766   CSELIB_VAL_PTR (e->val_rtx) = e;
767   e->addr_list = 0;
768   e->locs = 0;
769   e->next_containing_mem = 0;
770   return e;
771 }
772
773 /* ADDR_ELT is a value that is used as address.  MEM_ELT is the value that
774    contains the data at this address.  X is a MEM that represents the
775    value.  Update the two value structures to represent this situation.  */
776
777 static void
778 add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
779 {
780   struct elt_loc_list *l;
781
782   /* Avoid duplicates.  */
783   for (l = mem_elt->locs; l; l = l->next)
784     if (MEM_P (l->loc)
785         && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
786       return;
787
788   addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
789   mem_elt->locs
790     = new_elt_loc_list (mem_elt->locs,
791                         replace_equiv_address_nv (x, addr_elt->val_rtx));
792   if (mem_elt->next_containing_mem == NULL)
793     {
794       mem_elt->next_containing_mem = first_containing_mem;
795       first_containing_mem = mem_elt;
796     }
797 }
798
799 /* Subroutine of cselib_lookup.  Return a value for X, which is a MEM rtx.
800    If CREATE, make a new one if we haven't seen it before.  */
801
802 static cselib_val *
803 cselib_lookup_mem (rtx x, int create)
804 {
805   enum machine_mode mode = GET_MODE (x);
806   void **slot;
807   cselib_val *addr;
808   cselib_val *mem_elt;
809   struct elt_list *l;
810
811   if (MEM_VOLATILE_P (x) || mode == BLKmode
812       || !cselib_record_memory
813       || (FLOAT_MODE_P (mode) && flag_float_store))
814     return 0;
815
816   /* Look up the value for the address.  */
817   addr = cselib_lookup (XEXP (x, 0), mode, create);
818   if (! addr)
819     return 0;
820
821   /* Find a value that describes a value of our mode at that address.  */
822   for (l = addr->addr_list; l; l = l->next)
823     if (GET_MODE (l->elt->val_rtx) == mode)
824       return l->elt;
825
826   if (! create)
827     return 0;
828
829   mem_elt = new_cselib_val (++next_unknown_value, mode);
830   add_mem_for_addr (addr, mem_elt, x);
831   slot = htab_find_slot_with_hash (cselib_hash_table, wrap_constant (mode, x),
832                                    mem_elt->value, INSERT);
833   *slot = mem_elt;
834   return mem_elt;
835 }
836
837 /* Search thru the possible substitutions in P.  We prefer a non reg
838    substitution because this allows us to expand the tree further.  If
839    we find, just a reg, take the lowest regno.  There may be several
840    non-reg results, we just take the first one because they will all
841    expand to the same place.  */
842
843 static rtx 
844 expand_loc (struct elt_loc_list *p, bitmap regs_active, int max_depth)
845 {
846   rtx reg_result = NULL;
847   unsigned int regno = UINT_MAX;
848   struct elt_loc_list *p_in = p;
849
850   for (; p; p = p -> next)
851     {
852       /* Avoid infinite recursion trying to expand a reg into a
853          the same reg.  */
854       if ((REG_P (p->loc)) 
855           && (REGNO (p->loc) < regno) 
856           && !bitmap_bit_p (regs_active, REGNO (p->loc)))
857         {
858           reg_result = p->loc;
859           regno = REGNO (p->loc);
860         }
861       /* Avoid infinite recursion and do not try to expand the
862          value.  */
863       else if (GET_CODE (p->loc) == VALUE 
864                && CSELIB_VAL_PTR (p->loc)->locs == p_in)
865         continue;
866       else if (!REG_P (p->loc))
867         {
868           rtx result;
869           if (dump_file)
870             {
871               print_inline_rtx (dump_file, p->loc, 0);
872               fprintf (dump_file, "\n");
873             }
874           result = cselib_expand_value_rtx (p->loc, regs_active, max_depth - 1);
875           if (result)
876             return result;
877         }
878         
879     }
880   
881   if (regno != UINT_MAX)
882     {
883       rtx result;
884       if (dump_file)
885         fprintf (dump_file, "r%d\n", regno);
886
887       result = cselib_expand_value_rtx (reg_result, regs_active, max_depth - 1);
888       if (result)
889         return result;
890     }
891
892   if (dump_file)
893     {
894       if (reg_result)
895         {
896           print_inline_rtx (dump_file, reg_result, 0);
897           fprintf (dump_file, "\n");
898         }
899       else 
900         fprintf (dump_file, "NULL\n");
901     }
902   return reg_result;
903 }
904
905
906 /* Forward substitute and expand an expression out to its roots.
907    This is the opposite of common subexpression.  Because local value
908    numbering is such a weak optimization, the expanded expression is
909    pretty much unique (not from a pointer equals point of view but
910    from a tree shape point of view.  
911
912    This function returns NULL if the expansion fails.  The expansion
913    will fail if there is no value number for one of the operands or if
914    one of the operands has been overwritten between the current insn
915    and the beginning of the basic block.  For instance x has no
916    expansion in:
917
918    r1 <- r1 + 3
919    x <- r1 + 8
920
921    REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
922    It is clear on return.  */
923
924 rtx
925 cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
926 {
927   rtx copy, scopy;
928   int i, j;
929   RTX_CODE code;
930   const char *format_ptr;
931
932   code = GET_CODE (orig);
933
934   /* For the context of dse, if we end up expand into a huge tree, we
935      will not have a useful address, so we might as well just give up
936      quickly.  */
937   if (max_depth <= 0)
938     return NULL;
939
940   switch (code)
941     {
942     case REG:
943       {
944         struct elt_list *l = REG_VALUES (REGNO (orig));
945
946         if (l && l->elt == NULL)
947           l = l->next;
948         for (; l; l = l->next)
949           if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
950             {
951               rtx result;
952               int regno = REGNO (orig);
953               
954               /* The only thing that we are not willing to do (this
955                  is requirement of dse and if others potential uses
956                  need this function we should add a parm to control
957                  it) is that we will not substitute the
958                  STACK_POINTER_REGNUM, FRAME_POINTER or the
959                  HARD_FRAME_POINTER.
960
961                  These expansions confuses the code that notices that
962                  stores into the frame go dead at the end of the
963                  function and that the frame is not effected by calls
964                  to subroutines.  If you allow the
965                  STACK_POINTER_REGNUM substitution, then dse will
966                  think that parameter pushing also goes dead which is
967                  wrong.  If you allow the FRAME_POINTER or the
968                  HARD_FRAME_POINTER then you lose the opportunity to
969                  make the frame assumptions.  */
970               if (regno == STACK_POINTER_REGNUM
971                   || regno == FRAME_POINTER_REGNUM
972                   || regno == HARD_FRAME_POINTER_REGNUM)
973                 return orig;
974
975               bitmap_set_bit (regs_active, regno);
976
977               if (dump_file)
978                 fprintf (dump_file, "expanding: r%d into: ", regno);
979
980               result = expand_loc (l->elt->locs, regs_active, max_depth);
981               bitmap_clear_bit (regs_active, regno);
982
983               if (result)
984                 return result;
985               else 
986                 return orig;
987             }
988       }
989       
990     case CONST_INT:
991     case CONST_DOUBLE:
992     case CONST_VECTOR:
993     case SYMBOL_REF:
994     case CODE_LABEL:
995     case PC:
996     case CC0:
997     case SCRATCH:
998       /* SCRATCH must be shared because they represent distinct values.  */
999       return orig;
1000     case CLOBBER:
1001       if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1002         return orig;
1003       break;
1004
1005     case CONST:
1006       if (shared_const_p (orig))
1007         return orig;
1008       break;
1009
1010
1011     case VALUE:
1012       {
1013         rtx result;
1014         if (dump_file)
1015           fprintf (dump_file, "expanding value %s into: ", GET_MODE_NAME (GET_MODE (orig)));
1016         
1017         result = expand_loc (CSELIB_VAL_PTR (orig)->locs, regs_active, max_depth);
1018         if (result 
1019             && GET_CODE (result) == CONST_INT
1020             && GET_MODE (orig) != VOIDmode)
1021           {
1022             result = gen_rtx_CONST (GET_MODE (orig), result);
1023             if (dump_file)
1024               fprintf (dump_file, "  wrapping const_int result in const to preserve mode %s\n", 
1025                        GET_MODE_NAME (GET_MODE (orig)));
1026           }
1027         return result;
1028       }
1029     default:
1030       break;
1031     }
1032
1033   /* Copy the various flags, fields, and other information.  We assume
1034      that all fields need copying, and then clear the fields that should
1035      not be copied.  That is the sensible default behavior, and forces
1036      us to explicitly document why we are *not* copying a flag.  */
1037   copy = shallow_copy_rtx (orig);
1038
1039   format_ptr = GET_RTX_FORMAT (GET_CODE (copy));
1040
1041   for (i = 0; i < GET_RTX_LENGTH (GET_CODE (copy)); i++)
1042     switch (*format_ptr++)
1043       {
1044       case 'e':
1045         if (XEXP (orig, i) != NULL)
1046           {
1047             rtx result = cselib_expand_value_rtx (XEXP (orig, i), regs_active, max_depth - 1);
1048             if (!result)
1049               return NULL;
1050             XEXP (copy, i) = result;
1051           }
1052         break;
1053
1054       case 'E':
1055       case 'V':
1056         if (XVEC (orig, i) != NULL)
1057           {
1058             XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1059             for (j = 0; j < XVECLEN (copy, i); j++)
1060               {
1061                 rtx result = cselib_expand_value_rtx (XVECEXP (orig, i, j), regs_active, max_depth - 1);
1062                 if (!result)
1063                   return NULL;
1064                 XVECEXP (copy, i, j) = result;
1065               }
1066           }
1067         break;
1068
1069       case 't':
1070       case 'w':
1071       case 'i':
1072       case 's':
1073       case 'S':
1074       case 'T':
1075       case 'u':
1076       case 'B':
1077       case '0':
1078         /* These are left unchanged.  */
1079         break;
1080
1081       default:
1082         gcc_unreachable ();
1083       }
1084
1085   scopy = simplify_rtx (copy);
1086   if (scopy)
1087     return scopy;
1088   return copy;
1089 }
1090
1091 /* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1092    with VALUE expressions.  This way, it becomes independent of changes
1093    to registers and memory.
1094    X isn't actually modified; if modifications are needed, new rtl is
1095    allocated.  However, the return value can share rtl with X.  */
1096
1097 rtx
1098 cselib_subst_to_values (rtx x)
1099 {
1100   enum rtx_code code = GET_CODE (x);
1101   const char *fmt = GET_RTX_FORMAT (code);
1102   cselib_val *e;
1103   struct elt_list *l;
1104   rtx copy = x;
1105   int i;
1106
1107   switch (code)
1108     {
1109     case REG:
1110       l = REG_VALUES (REGNO (x));
1111       if (l && l->elt == NULL)
1112         l = l->next;
1113       for (; l; l = l->next)
1114         if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1115           return l->elt->val_rtx;
1116
1117       gcc_unreachable ();
1118
1119     case MEM:
1120       e = cselib_lookup_mem (x, 0);
1121       if (! e)
1122         {
1123           /* This happens for autoincrements.  Assign a value that doesn't
1124              match any other.  */
1125           e = new_cselib_val (++next_unknown_value, GET_MODE (x));
1126         }
1127       return e->val_rtx;
1128
1129     case CONST_DOUBLE:
1130     case CONST_VECTOR:
1131     case CONST_INT:
1132     case CONST_FIXED:
1133       return x;
1134
1135     case POST_INC:
1136     case PRE_INC:
1137     case POST_DEC:
1138     case PRE_DEC:
1139     case POST_MODIFY:
1140     case PRE_MODIFY:
1141       e = new_cselib_val (++next_unknown_value, GET_MODE (x));
1142       return e->val_rtx;
1143
1144     default:
1145       break;
1146     }
1147
1148   for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1149     {
1150       if (fmt[i] == 'e')
1151         {
1152           rtx t = cselib_subst_to_values (XEXP (x, i));
1153
1154           if (t != XEXP (x, i) && x == copy)
1155             copy = shallow_copy_rtx (x);
1156
1157           XEXP (copy, i) = t;
1158         }
1159       else if (fmt[i] == 'E')
1160         {
1161           int j, k;
1162
1163           for (j = 0; j < XVECLEN (x, i); j++)
1164             {
1165               rtx t = cselib_subst_to_values (XVECEXP (x, i, j));
1166
1167               if (t != XVECEXP (x, i, j) && XVEC (x, i) == XVEC (copy, i))
1168                 {
1169                   if (x == copy)
1170                     copy = shallow_copy_rtx (x);
1171
1172                   XVEC (copy, i) = rtvec_alloc (XVECLEN (x, i));
1173                   for (k = 0; k < j; k++)
1174                     XVECEXP (copy, i, k) = XVECEXP (x, i, k);
1175                 }
1176
1177               XVECEXP (copy, i, j) = t;
1178             }
1179         }
1180     }
1181
1182   return copy;
1183 }
1184
1185 /* Look up the rtl expression X in our tables and return the value it has.
1186    If CREATE is zero, we return NULL if we don't know the value.  Otherwise,
1187    we create a new one if possible, using mode MODE if X doesn't have a mode
1188    (i.e. because it's a constant).  */
1189
1190 cselib_val *
1191 cselib_lookup (rtx x, enum machine_mode mode, int create)
1192 {
1193   void **slot;
1194   cselib_val *e;
1195   unsigned int hashval;
1196
1197   if (GET_MODE (x) != VOIDmode)
1198     mode = GET_MODE (x);
1199
1200   if (GET_CODE (x) == VALUE)
1201     return CSELIB_VAL_PTR (x);
1202
1203   if (REG_P (x))
1204     {
1205       struct elt_list *l;
1206       unsigned int i = REGNO (x);
1207
1208       l = REG_VALUES (i);
1209       if (l && l->elt == NULL)
1210         l = l->next;
1211       for (; l; l = l->next)
1212         if (mode == GET_MODE (l->elt->val_rtx))
1213           return l->elt;
1214
1215       if (! create)
1216         return 0;
1217
1218       if (i < FIRST_PSEUDO_REGISTER)
1219         {
1220           unsigned int n = hard_regno_nregs[i][mode];
1221
1222           if (n > max_value_regs)
1223             max_value_regs = n;
1224         }
1225
1226       e = new_cselib_val (++next_unknown_value, GET_MODE (x));
1227       e->locs = new_elt_loc_list (e->locs, x);
1228       if (REG_VALUES (i) == 0)
1229         {
1230           /* Maintain the invariant that the first entry of
1231              REG_VALUES, if present, must be the value used to set the
1232              register, or NULL.  */
1233           used_regs[n_used_regs++] = i;
1234           REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
1235         }
1236       REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
1237       slot = htab_find_slot_with_hash (cselib_hash_table, x, e->value, INSERT);
1238       *slot = e;
1239       return e;
1240     }
1241
1242   if (MEM_P (x))
1243     return cselib_lookup_mem (x, create);
1244
1245   hashval = cselib_hash_rtx (x, create);
1246   /* Can't even create if hashing is not possible.  */
1247   if (! hashval)
1248     return 0;
1249
1250   slot = htab_find_slot_with_hash (cselib_hash_table, wrap_constant (mode, x),
1251                                    hashval, create ? INSERT : NO_INSERT);
1252   if (slot == 0)
1253     return 0;
1254
1255   e = (cselib_val *) *slot;
1256   if (e)
1257     return e;
1258
1259   e = new_cselib_val (hashval, mode);
1260
1261   /* We have to fill the slot before calling cselib_subst_to_values:
1262      the hash table is inconsistent until we do so, and
1263      cselib_subst_to_values will need to do lookups.  */
1264   *slot = (void *) e;
1265   e->locs = new_elt_loc_list (e->locs, cselib_subst_to_values (x));
1266   return e;
1267 }
1268
1269 /* Invalidate any entries in reg_values that overlap REGNO.  This is called
1270    if REGNO is changing.  MODE is the mode of the assignment to REGNO, which
1271    is used to determine how many hard registers are being changed.  If MODE
1272    is VOIDmode, then only REGNO is being changed; this is used when
1273    invalidating call clobbered registers across a call.  */
1274
1275 static void
1276 cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
1277 {
1278   unsigned int endregno;
1279   unsigned int i;
1280
1281   /* If we see pseudos after reload, something is _wrong_.  */
1282   gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
1283               || reg_renumber[regno] < 0);
1284
1285   /* Determine the range of registers that must be invalidated.  For
1286      pseudos, only REGNO is affected.  For hard regs, we must take MODE
1287      into account, and we must also invalidate lower register numbers
1288      if they contain values that overlap REGNO.  */
1289   if (regno < FIRST_PSEUDO_REGISTER)
1290     {
1291       gcc_assert (mode != VOIDmode);
1292
1293       if (regno < max_value_regs)
1294         i = 0;
1295       else
1296         i = regno - max_value_regs;
1297
1298       endregno = end_hard_regno (mode, regno);
1299     }
1300   else
1301     {
1302       i = regno;
1303       endregno = regno + 1;
1304     }
1305
1306   for (; i < endregno; i++)
1307     {
1308       struct elt_list **l = &REG_VALUES (i);
1309
1310       /* Go through all known values for this reg; if it overlaps the range
1311          we're invalidating, remove the value.  */
1312       while (*l)
1313         {
1314           cselib_val *v = (*l)->elt;
1315           struct elt_loc_list **p;
1316           unsigned int this_last = i;
1317
1318           if (i < FIRST_PSEUDO_REGISTER && v != NULL)
1319             this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
1320
1321           if (this_last < regno || v == NULL)
1322             {
1323               l = &(*l)->next;
1324               continue;
1325             }
1326
1327           /* We have an overlap.  */
1328           if (*l == REG_VALUES (i))
1329             {
1330               /* Maintain the invariant that the first entry of
1331                  REG_VALUES, if present, must be the value used to set
1332                  the register, or NULL.  This is also nice because
1333                  then we won't push the same regno onto user_regs
1334                  multiple times.  */
1335               (*l)->elt = NULL;
1336               l = &(*l)->next;
1337             }
1338           else
1339             unchain_one_elt_list (l);
1340
1341           /* Now, we clear the mapping from value to reg.  It must exist, so
1342              this code will crash intentionally if it doesn't.  */
1343           for (p = &v->locs; ; p = &(*p)->next)
1344             {
1345               rtx x = (*p)->loc;
1346
1347               if (REG_P (x) && REGNO (x) == i)
1348                 {
1349                   unchain_one_elt_loc_list (p);
1350                   break;
1351                 }
1352             }
1353           if (v->locs == 0)
1354             n_useless_values++;
1355         }
1356     }
1357 }
1358 \f
1359 /* Return 1 if X has a value that can vary even between two
1360    executions of the program.  0 means X can be compared reliably
1361    against certain constants or near-constants.  */
1362
1363 static bool
1364 cselib_rtx_varies_p (const_rtx x ATTRIBUTE_UNUSED, bool from_alias ATTRIBUTE_UNUSED)
1365 {
1366   /* We actually don't need to verify very hard.  This is because
1367      if X has actually changed, we invalidate the memory anyway,
1368      so assume that all common memory addresses are
1369      invariant.  */
1370   return 0;
1371 }
1372
1373 /* Invalidate any locations in the table which are changed because of a
1374    store to MEM_RTX.  If this is called because of a non-const call
1375    instruction, MEM_RTX is (mem:BLK const0_rtx).  */
1376
1377 static void
1378 cselib_invalidate_mem (rtx mem_rtx)
1379 {
1380   cselib_val **vp, *v, *next;
1381   int num_mems = 0;
1382   rtx mem_addr;
1383
1384   mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
1385   mem_rtx = canon_rtx (mem_rtx);
1386
1387   vp = &first_containing_mem;
1388   for (v = *vp; v != &dummy_val; v = next)
1389     {
1390       bool has_mem = false;
1391       struct elt_loc_list **p = &v->locs;
1392       int had_locs = v->locs != 0;
1393
1394       while (*p)
1395         {
1396           rtx x = (*p)->loc;
1397           cselib_val *addr;
1398           struct elt_list **mem_chain;
1399
1400           /* MEMs may occur in locations only at the top level; below
1401              that every MEM or REG is substituted by its VALUE.  */
1402           if (!MEM_P (x))
1403             {
1404               p = &(*p)->next;
1405               continue;
1406             }
1407           if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
1408               && ! canon_true_dependence (mem_rtx, GET_MODE (mem_rtx), mem_addr,
1409                                           x, cselib_rtx_varies_p))
1410             {
1411               has_mem = true;
1412               num_mems++;
1413               p = &(*p)->next;
1414               continue;
1415             }
1416
1417           /* This one overlaps.  */
1418           /* We must have a mapping from this MEM's address to the
1419              value (E).  Remove that, too.  */
1420           addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0);
1421           mem_chain = &addr->addr_list;
1422           for (;;)
1423             {
1424               if ((*mem_chain)->elt == v)
1425                 {
1426                   unchain_one_elt_list (mem_chain);
1427                   break;
1428                 }
1429
1430               mem_chain = &(*mem_chain)->next;
1431             }
1432
1433           unchain_one_elt_loc_list (p);
1434         }
1435
1436       if (had_locs && v->locs == 0)
1437         n_useless_values++;
1438
1439       next = v->next_containing_mem;
1440       if (has_mem)
1441         {
1442           *vp = v;
1443           vp = &(*vp)->next_containing_mem;
1444         }
1445       else
1446         v->next_containing_mem = NULL;
1447     }
1448   *vp = &dummy_val;
1449 }
1450
1451 /* Invalidate DEST, which is being assigned to or clobbered.  */
1452
1453 void
1454 cselib_invalidate_rtx (rtx dest)
1455 {
1456   while (GET_CODE (dest) == SUBREG
1457          || GET_CODE (dest) == ZERO_EXTRACT
1458          || GET_CODE (dest) == STRICT_LOW_PART)
1459     dest = XEXP (dest, 0);
1460
1461   if (REG_P (dest))
1462     cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
1463   else if (MEM_P (dest))
1464     cselib_invalidate_mem (dest);
1465
1466   /* Some machines don't define AUTO_INC_DEC, but they still use push
1467      instructions.  We need to catch that case here in order to
1468      invalidate the stack pointer correctly.  Note that invalidating
1469      the stack pointer is different from invalidating DEST.  */
1470   if (push_operand (dest, GET_MODE (dest)))
1471     cselib_invalidate_rtx (stack_pointer_rtx);
1472 }
1473
1474 /* A wrapper for cselib_invalidate_rtx to be called via note_stores.  */
1475
1476 static void
1477 cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
1478                                    void *data ATTRIBUTE_UNUSED)
1479 {
1480   cselib_invalidate_rtx (dest);
1481 }
1482
1483 /* Record the result of a SET instruction.  DEST is being set; the source
1484    contains the value described by SRC_ELT.  If DEST is a MEM, DEST_ADDR_ELT
1485    describes its address.  */
1486
1487 static void
1488 cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
1489 {
1490   int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
1491
1492   if (src_elt == 0 || side_effects_p (dest))
1493     return;
1494
1495   if (dreg >= 0)
1496     {
1497       if (dreg < FIRST_PSEUDO_REGISTER)
1498         {
1499           unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
1500
1501           if (n > max_value_regs)
1502             max_value_regs = n;
1503         }
1504
1505       if (REG_VALUES (dreg) == 0)
1506         {
1507           used_regs[n_used_regs++] = dreg;
1508           REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
1509         }
1510       else
1511         {
1512           /* The register should have been invalidated.  */
1513           gcc_assert (REG_VALUES (dreg)->elt == 0);
1514           REG_VALUES (dreg)->elt = src_elt;
1515         }
1516
1517       if (src_elt->locs == 0)
1518         n_useless_values--;
1519       src_elt->locs = new_elt_loc_list (src_elt->locs, dest);
1520     }
1521   else if (MEM_P (dest) && dest_addr_elt != 0
1522            && cselib_record_memory)
1523     {
1524       if (src_elt->locs == 0)
1525         n_useless_values--;
1526       add_mem_for_addr (dest_addr_elt, src_elt, dest);
1527     }
1528 }
1529
1530 /* Describe a single set that is part of an insn.  */
1531 struct set
1532 {
1533   rtx src;
1534   rtx dest;
1535   cselib_val *src_elt;
1536   cselib_val *dest_addr_elt;
1537 };
1538
1539 /* There is no good way to determine how many elements there can be
1540    in a PARALLEL.  Since it's fairly cheap, use a really large number.  */
1541 #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
1542
1543 /* Record the effects of any sets in INSN.  */
1544 static void
1545 cselib_record_sets (rtx insn)
1546 {
1547   int n_sets = 0;
1548   int i;
1549   struct set sets[MAX_SETS];
1550   rtx body = PATTERN (insn);
1551   rtx cond = 0;
1552
1553   body = PATTERN (insn);
1554   if (GET_CODE (body) == COND_EXEC)
1555     {
1556       cond = COND_EXEC_TEST (body);
1557       body = COND_EXEC_CODE (body);
1558     }
1559
1560   /* Find all sets.  */
1561   if (GET_CODE (body) == SET)
1562     {
1563       sets[0].src = SET_SRC (body);
1564       sets[0].dest = SET_DEST (body);
1565       n_sets = 1;
1566     }
1567   else if (GET_CODE (body) == PARALLEL)
1568     {
1569       /* Look through the PARALLEL and record the values being
1570          set, if possible.  Also handle any CLOBBERs.  */
1571       for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
1572         {
1573           rtx x = XVECEXP (body, 0, i);
1574
1575           if (GET_CODE (x) == SET)
1576             {
1577               sets[n_sets].src = SET_SRC (x);
1578               sets[n_sets].dest = SET_DEST (x);
1579               n_sets++;
1580             }
1581         }
1582     }
1583
1584   /* Look up the values that are read.  Do this before invalidating the
1585      locations that are written.  */
1586   for (i = 0; i < n_sets; i++)
1587     {
1588       rtx dest = sets[i].dest;
1589
1590       /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
1591          the low part after invalidating any knowledge about larger modes.  */
1592       if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
1593         sets[i].dest = dest = XEXP (dest, 0);
1594
1595       /* We don't know how to record anything but REG or MEM.  */
1596       if (REG_P (dest)
1597           || (MEM_P (dest) && cselib_record_memory))
1598         {
1599           rtx src = sets[i].src;
1600           if (cond)
1601             src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
1602           sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1);
1603           if (MEM_P (dest))
1604             sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0), Pmode, 1);
1605           else
1606             sets[i].dest_addr_elt = 0;
1607         }
1608     }
1609
1610   /* Invalidate all locations written by this insn.  Note that the elts we
1611      looked up in the previous loop aren't affected, just some of their
1612      locations may go away.  */
1613   note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
1614
1615   /* If this is an asm, look for duplicate sets.  This can happen when the
1616      user uses the same value as an output multiple times.  This is valid
1617      if the outputs are not actually used thereafter.  Treat this case as
1618      if the value isn't actually set.  We do this by smashing the destination
1619      to pc_rtx, so that we won't record the value later.  */
1620   if (n_sets >= 2 && asm_noperands (body) >= 0)
1621     {
1622       for (i = 0; i < n_sets; i++)
1623         {
1624           rtx dest = sets[i].dest;
1625           if (REG_P (dest) || MEM_P (dest))
1626             {
1627               int j;
1628               for (j = i + 1; j < n_sets; j++)
1629                 if (rtx_equal_p (dest, sets[j].dest))
1630                   {
1631                     sets[i].dest = pc_rtx;
1632                     sets[j].dest = pc_rtx;
1633                   }
1634             }
1635         }
1636     }
1637
1638   /* Now enter the equivalences in our tables.  */
1639   for (i = 0; i < n_sets; i++)
1640     {
1641       rtx dest = sets[i].dest;
1642       if (REG_P (dest)
1643           || (MEM_P (dest) && cselib_record_memory))
1644         cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
1645     }
1646 }
1647
1648 /* Record the effects of INSN.  */
1649
1650 void
1651 cselib_process_insn (rtx insn)
1652 {
1653   int i;
1654   rtx x;
1655
1656   cselib_current_insn = insn;
1657
1658   /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp.  */
1659   if (LABEL_P (insn)
1660       || (CALL_P (insn)
1661           && find_reg_note (insn, REG_SETJMP, NULL))
1662       || (NONJUMP_INSN_P (insn)
1663           && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
1664           && MEM_VOLATILE_P (PATTERN (insn))))
1665     {
1666       cselib_clear_table ();
1667       return;
1668     }
1669
1670   if (! INSN_P (insn))
1671     {
1672       cselib_current_insn = 0;
1673       return;
1674     }
1675
1676   /* If this is a call instruction, forget anything stored in a
1677      call clobbered register, or, if this is not a const call, in
1678      memory.  */
1679   if (CALL_P (insn))
1680     {
1681       for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
1682         if (call_used_regs[i]
1683             || (REG_VALUES (i) && REG_VALUES (i)->elt
1684                 && HARD_REGNO_CALL_PART_CLOBBERED (i, 
1685                       GET_MODE (REG_VALUES (i)->elt->val_rtx))))
1686           cselib_invalidate_regno (i, reg_raw_mode[i]);
1687
1688       /* Since it is not clear how cselib is going to be used, be
1689          conservative here and treat looping pure or const functions
1690          as if they were regular functions.  */
1691       if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
1692           || !(RTL_CONST_OR_PURE_CALL_P (insn)))
1693         cselib_invalidate_mem (callmem);
1694     }
1695
1696   cselib_record_sets (insn);
1697
1698 #ifdef AUTO_INC_DEC
1699   /* Clobber any registers which appear in REG_INC notes.  We
1700      could keep track of the changes to their values, but it is
1701      unlikely to help.  */
1702   for (x = REG_NOTES (insn); x; x = XEXP (x, 1))
1703     if (REG_NOTE_KIND (x) == REG_INC)
1704       cselib_invalidate_rtx (XEXP (x, 0));
1705 #endif
1706
1707   /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
1708      after we have processed the insn.  */
1709   if (CALL_P (insn))
1710     for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
1711       if (GET_CODE (XEXP (x, 0)) == CLOBBER)
1712         cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
1713
1714   cselib_current_insn = 0;
1715
1716   if (n_useless_values > MAX_USELESS_VALUES
1717       /* remove_useless_values is linear in the hash table size.  Avoid
1718          quadratic behavior for very large hashtables with very few
1719          useless elements.  */
1720       && (unsigned int)n_useless_values > cselib_hash_table->n_elements / 4)
1721     remove_useless_values ();
1722 }
1723
1724 /* Initialize cselib for one pass.  The caller must also call
1725    init_alias_analysis.  */
1726
1727 void
1728 cselib_init (bool record_memory)
1729 {
1730   elt_list_pool = create_alloc_pool ("elt_list", 
1731                                      sizeof (struct elt_list), 10);
1732   elt_loc_list_pool = create_alloc_pool ("elt_loc_list", 
1733                                          sizeof (struct elt_loc_list), 10);
1734   cselib_val_pool = create_alloc_pool ("cselib_val_list", 
1735                                        sizeof (cselib_val), 10);
1736   value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
1737   cselib_record_memory = record_memory;
1738
1739   /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
1740      see canon_true_dependence.  This is only created once.  */
1741   if (! callmem)
1742     callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
1743
1744   cselib_nregs = max_reg_num ();
1745
1746   /* We preserve reg_values to allow expensive clearing of the whole thing.
1747      Reallocate it however if it happens to be too large.  */
1748   if (!reg_values || reg_values_size < cselib_nregs
1749       || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
1750     {
1751       if (reg_values)
1752         free (reg_values);
1753       /* Some space for newly emit instructions so we don't end up
1754          reallocating in between passes.  */
1755       reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
1756       reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
1757     }
1758   used_regs = XNEWVEC (unsigned int, cselib_nregs);
1759   n_used_regs = 0;
1760   cselib_hash_table = htab_create (31, get_value_hash,
1761                                    entry_and_rtx_equal_p, NULL);
1762 }
1763
1764 /* Called when the current user is done with cselib.  */
1765
1766 void
1767 cselib_finish (void)
1768 {
1769   cselib_discard_hook = NULL;
1770   free_alloc_pool (elt_list_pool);
1771   free_alloc_pool (elt_loc_list_pool);
1772   free_alloc_pool (cselib_val_pool);
1773   free_alloc_pool (value_pool);
1774   cselib_clear_table ();
1775   htab_delete (cselib_hash_table);
1776   free (used_regs);
1777   used_regs = 0;
1778   cselib_hash_table = 0;
1779   n_useless_values = 0;
1780   next_unknown_value = 0;
1781 }
1782
1783 #include "gt-cselib.h"