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