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