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