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