]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cselib.c
re PR libmudflap/38738 (libmudflap could be enabled for Solaris when using GNU ld)
[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,
4a3c9687 3 1999, 2000, 2001, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
8dd5516b 4 Free Software Foundation, Inc.
fa49fd0f 5
1322177d 6This file is part of GCC.
fa49fd0f 7
1322177d
LB
8GCC is free software; you can redistribute it and/or modify it under
9the terms of the GNU General Public License as published by the Free
9dcd6f09 10Software Foundation; either version 3, or (at your option) any later
1322177d 11version.
fa49fd0f 12
1322177d
LB
13GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14WARRANTY; without even the implied warranty of MERCHANTABILITY or
15FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16for more details.
fa49fd0f
RK
17
18You should have received a copy of the GNU General Public License
9dcd6f09
NC
19along with GCC; see the file COPYING3. If not see
20<http://www.gnu.org/licenses/>. */
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"
fa49fd0f
RK
32#include "insn-config.h"
33#include "recog.h"
34#include "function.h"
78528714 35#include "emit-rtl.h"
718f9c0f 36#include "diagnostic-core.h"
fa49fd0f
RK
37#include "output.h"
38#include "ggc.h"
fa49fd0f 39#include "hashtab.h"
b5b8b0ac 40#include "tree-pass.h"
fa49fd0f 41#include "cselib.h"
c65ecebc 42#include "params.h"
6a59927d 43#include "alloc-pool.h"
29c1846b 44#include "target.h"
7a8cba34 45#include "bitmap.h"
fa49fd0f 46
fba4cb03
LB
47/* A list of cselib_val structures. */
48struct elt_list {
49 struct elt_list *next;
50 cselib_val *elt;
51};
52
463301c3 53static bool cselib_record_memory;
457eeaae 54static bool cselib_preserve_constants;
7080f735
AJ
55static int entry_and_rtx_equal_p (const void *, const void *);
56static hashval_t get_value_hash (const void *);
57static struct elt_list *new_elt_list (struct elt_list *, cselib_val *);
58static struct elt_loc_list *new_elt_loc_list (struct elt_loc_list *, rtx);
59static void unchain_one_value (cselib_val *);
60static void unchain_one_elt_list (struct elt_list **);
61static void unchain_one_elt_loc_list (struct elt_loc_list **);
7080f735
AJ
62static int discard_useless_locs (void **, void *);
63static int discard_useless_values (void **, void *);
64static void remove_useless_values (void);
4deef538
AO
65static int rtx_equal_for_cselib_1 (rtx, rtx, enum machine_mode);
66static unsigned int cselib_hash_rtx (rtx, int, enum machine_mode);
b5b8b0ac 67static cselib_val *new_cselib_val (unsigned int, enum machine_mode, rtx);
7080f735
AJ
68static void add_mem_for_addr (cselib_val *, cselib_val *, rtx);
69static cselib_val *cselib_lookup_mem (rtx, int);
70static void cselib_invalidate_regno (unsigned int, enum machine_mode);
7080f735 71static void cselib_invalidate_mem (rtx);
7080f735
AJ
72static void cselib_record_set (rtx, cselib_val *, cselib_val *);
73static void cselib_record_sets (rtx);
fa49fd0f 74
b5b8b0ac
AO
75struct expand_value_data
76{
77 bitmap regs_active;
78 cselib_expand_callback callback;
79 void *callback_arg;
864ddef7 80 bool dummy;
b5b8b0ac
AO
81};
82
83static rtx cselib_expand_value_rtx_1 (rtx, struct expand_value_data *, int);
84
fa49fd0f
RK
85/* There are three ways in which cselib can look up an rtx:
86 - for a REG, the reg_values table (which is indexed by regno) is used
87 - for a MEM, we recursively look up its address and then follow the
88 addr_list of that value
89 - for everything else, we compute a hash value and go through the hash
90 table. Since different rtx's can still have the same hash value,
91 this involves walking the table entries for a given value and comparing
92 the locations of the entries with the rtx we are looking up. */
93
94/* A table that enables us to look up elts by their value. */
7c514720 95static htab_t cselib_hash_table;
fa49fd0f
RK
96
97/* This is a global so we don't have to pass this through every function.
98 It is used in new_elt_loc_list to set SETTING_INSN. */
99static rtx cselib_current_insn;
100
5440c0e7
AO
101/* The unique id that the next create value will take. */
102static unsigned int next_uid;
fa49fd0f
RK
103
104/* The number of registers we had when the varrays were last resized. */
105static unsigned int cselib_nregs;
106
5847e8da
AO
107/* Count values without known locations, or with only locations that
108 wouldn't have been known except for debug insns. Whenever this
109 grows too big, we remove these useless values from the table.
110
111 Counting values with only debug values is a bit tricky. We don't
112 want to increment n_useless_values when we create a value for a
113 debug insn, for this would get n_useless_values out of sync, but we
114 want increment it if all locs in the list that were ever referenced
115 in nondebug insns are removed from the list.
116
117 In the general case, once we do that, we'd have to stop accepting
118 nondebug expressions in the loc list, to avoid having two values
119 equivalent that, without debug insns, would have been made into
120 separate values. However, because debug insns never introduce
121 equivalences themselves (no assignments), the only means for
122 growing loc lists is through nondebug assignments. If the locs
123 also happen to be referenced in debug insns, it will work just fine.
124
125 A consequence of this is that there's at most one debug-only loc in
126 each loc list. If we keep it in the first entry, testing whether
127 we have a debug-only loc list takes O(1).
128
129 Furthermore, since any additional entry in a loc list containing a
130 debug loc would have to come from an assignment (nondebug) that
131 references both the initial debug loc and the newly-equivalent loc,
132 the initial debug loc would be promoted to a nondebug loc, and the
133 loc list would not contain debug locs any more.
134
135 So the only case we have to be careful with in order to keep
136 n_useless_values in sync between debug and nondebug compilations is
137 to avoid incrementing n_useless_values when removing the single loc
138 from a value that turns out to not appear outside debug values. We
139 increment n_useless_debug_values instead, and leave such values
140 alone until, for other reasons, we garbage-collect useless
141 values. */
fa49fd0f 142static int n_useless_values;
5847e8da
AO
143static int n_useless_debug_values;
144
145/* Count values whose locs have been taken exclusively from debug
146 insns for the entire life of the value. */
147static int n_debug_values;
fa49fd0f
RK
148
149/* Number of useless values before we remove them from the hash table. */
150#define MAX_USELESS_VALUES 32
151
60fa6660
AO
152/* This table maps from register number to values. It does not
153 contain pointers to cselib_val structures, but rather elt_lists.
154 The purpose is to be able to refer to the same register in
155 different modes. The first element of the list defines the mode in
156 which the register was set; if the mode is unknown or the value is
157 no longer valid in that mode, ELT will be NULL for the first
158 element. */
5211d65a
KH
159static struct elt_list **reg_values;
160static unsigned int reg_values_size;
6790d1ab 161#define REG_VALUES(i) reg_values[i]
fa49fd0f 162
31825e57 163/* The largest number of hard regs used by any entry added to the
eb232f4e 164 REG_VALUES table. Cleared on each cselib_clear_table() invocation. */
31825e57
DM
165static unsigned int max_value_regs;
166
fa49fd0f 167/* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used
eb232f4e 168 in cselib_clear_table() for fast emptying. */
6790d1ab
JH
169static unsigned int *used_regs;
170static unsigned int n_used_regs;
fa49fd0f
RK
171
172/* We pass this to cselib_invalidate_mem to invalidate all of
173 memory for a non-const call instruction. */
e2500fed 174static GTY(()) rtx callmem;
fa49fd0f 175
fa49fd0f
RK
176/* Set by discard_useless_locs if it deleted the last location of any
177 value. */
178static int values_became_useless;
7101fb18
JH
179
180/* Used as stop element of the containing_mem list so we can check
181 presence in the list by checking the next pointer. */
182static cselib_val dummy_val;
183
457eeaae
JJ
184/* If non-NULL, value of the eliminated arg_pointer_rtx or frame_pointer_rtx
185 that is constant through the whole function and should never be
186 eliminated. */
187static cselib_val *cfa_base_preserved_val;
9de9cbaf 188static unsigned int cfa_base_preserved_regno;
457eeaae 189
7080f735 190/* Used to list all values that contain memory reference.
7101fb18
JH
191 May or may not contain the useless values - the list is compacted
192 each time memory is invalidated. */
193static cselib_val *first_containing_mem = &dummy_val;
23bd7a93 194static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool;
6fb5fa3c
DB
195
196/* If nonnull, cselib will call this function before freeing useless
197 VALUEs. A VALUE is deemed useless if its "locs" field is null. */
198void (*cselib_discard_hook) (cselib_val *);
b5b8b0ac
AO
199
200/* If nonnull, cselib will call this function before recording sets or
201 even clobbering outputs of INSN. All the recorded sets will be
202 represented in the array sets[n_sets]. new_val_min can be used to
203 tell whether values present in sets are introduced by this
204 instruction. */
205void (*cselib_record_sets_hook) (rtx insn, struct cselib_set *sets,
206 int n_sets);
207
208#define PRESERVED_VALUE_P(RTX) \
209 (RTL_FLAG_CHECK1("PRESERVED_VALUE_P", (RTX), VALUE)->unchanging)
b5b8b0ac 210
fa49fd0f
RK
211\f
212
213/* Allocate a struct elt_list and fill in its two elements with the
214 arguments. */
215
6a59927d 216static inline struct elt_list *
7080f735 217new_elt_list (struct elt_list *next, cselib_val *elt)
fa49fd0f 218{
6a59927d 219 struct elt_list *el;
f883e0a7 220 el = (struct elt_list *) pool_alloc (elt_list_pool);
fa49fd0f
RK
221 el->next = next;
222 el->elt = elt;
223 return el;
224}
225
226/* Allocate a struct elt_loc_list and fill in its two elements with the
227 arguments. */
228
6a59927d 229static inline struct elt_loc_list *
7080f735 230new_elt_loc_list (struct elt_loc_list *next, rtx loc)
fa49fd0f 231{
6a59927d 232 struct elt_loc_list *el;
f883e0a7 233 el = (struct elt_loc_list *) pool_alloc (elt_loc_list_pool);
fa49fd0f
RK
234 el->next = next;
235 el->loc = loc;
236 el->setting_insn = cselib_current_insn;
5847e8da
AO
237 gcc_assert (!next || !next->setting_insn
238 || !DEBUG_INSN_P (next->setting_insn));
239
240 /* If we're creating the first loc in a debug insn context, we've
241 just created a debug value. Count it. */
242 if (!next && cselib_current_insn && DEBUG_INSN_P (cselib_current_insn))
243 n_debug_values++;
244
fa49fd0f
RK
245 return el;
246}
247
5847e8da
AO
248/* Promote loc L to a nondebug cselib_current_insn if L is marked as
249 originating from a debug insn, maintaining the debug values
250 count. */
251
252static inline void
253promote_debug_loc (struct elt_loc_list *l)
254{
255 if (l->setting_insn && DEBUG_INSN_P (l->setting_insn)
256 && (!cselib_current_insn || !DEBUG_INSN_P (cselib_current_insn)))
257 {
258 n_debug_values--;
259 l->setting_insn = cselib_current_insn;
509f4495 260 gcc_assert (!l->next || cselib_preserve_constants);
5847e8da
AO
261 }
262}
263
fa49fd0f
RK
264/* The elt_list at *PL is no longer needed. Unchain it and free its
265 storage. */
266
6a59927d 267static inline void
7080f735 268unchain_one_elt_list (struct elt_list **pl)
fa49fd0f
RK
269{
270 struct elt_list *l = *pl;
271
272 *pl = l->next;
6a59927d 273 pool_free (elt_list_pool, l);
fa49fd0f
RK
274}
275
276/* Likewise for elt_loc_lists. */
277
278static void
7080f735 279unchain_one_elt_loc_list (struct elt_loc_list **pl)
fa49fd0f
RK
280{
281 struct elt_loc_list *l = *pl;
282
283 *pl = l->next;
6a59927d 284 pool_free (elt_loc_list_pool, l);
fa49fd0f
RK
285}
286
287/* Likewise for cselib_vals. This also frees the addr_list associated with
288 V. */
289
290static void
7080f735 291unchain_one_value (cselib_val *v)
fa49fd0f
RK
292{
293 while (v->addr_list)
294 unchain_one_elt_list (&v->addr_list);
295
6a59927d 296 pool_free (cselib_val_pool, v);
fa49fd0f
RK
297}
298
299/* Remove all entries from the hash table. Also used during
b5b8b0ac 300 initialization. */
fa49fd0f 301
eb232f4e
SB
302void
303cselib_clear_table (void)
b5b8b0ac 304{
5440c0e7 305 cselib_reset_table (1);
b5b8b0ac
AO
306}
307
2b80199f
JJ
308/* Remove from hash table all VALUEs except constants
309 and function invariants. */
457eeaae
JJ
310
311static int
312preserve_only_constants (void **x, void *info ATTRIBUTE_UNUSED)
313{
314 cselib_val *v = (cselib_val *)*x;
315
316 if (v->locs != NULL
317 && v->locs->next == NULL)
318 {
319 if (CONSTANT_P (v->locs->loc)
320 && (GET_CODE (v->locs->loc) != CONST
321 || !references_value_p (v->locs->loc, 0)))
322 return 1;
323 if (cfa_base_preserved_val)
324 {
325 if (v == cfa_base_preserved_val)
326 return 1;
327 if (GET_CODE (v->locs->loc) == PLUS
328 && CONST_INT_P (XEXP (v->locs->loc, 1))
329 && XEXP (v->locs->loc, 0) == cfa_base_preserved_val->val_rtx)
330 return 1;
331 }
332 }
2b80199f
JJ
333 /* Keep around VALUEs that forward function invariant ENTRY_VALUEs
334 to corresponding parameter VALUEs. */
335 if (v->locs != NULL
336 && v->locs->next != NULL
337 && v->locs->next->next == NULL
338 && GET_CODE (v->locs->next->loc) == ENTRY_VALUE
339 && GET_CODE (v->locs->loc) == VALUE)
340 return 1;
457eeaae
JJ
341
342 htab_clear_slot (cselib_hash_table, x);
343 return 1;
344}
345
b5b8b0ac
AO
346/* Remove all entries from the hash table, arranging for the next
347 value to be numbered NUM. */
348
349void
5440c0e7 350cselib_reset_table (unsigned int num)
fa49fd0f
RK
351{
352 unsigned int i;
353
31825e57
DM
354 max_value_regs = 0;
355
457eeaae
JJ
356 if (cfa_base_preserved_val)
357 {
9de9cbaf 358 unsigned int regno = cfa_base_preserved_regno;
457eeaae
JJ
359 unsigned int new_used_regs = 0;
360 for (i = 0; i < n_used_regs; i++)
361 if (used_regs[i] == regno)
362 {
363 new_used_regs = 1;
364 continue;
365 }
366 else
367 REG_VALUES (used_regs[i]) = 0;
368 gcc_assert (new_used_regs == 1);
369 n_used_regs = new_used_regs;
370 used_regs[0] = regno;
371 max_value_regs
372 = hard_regno_nregs[regno][GET_MODE (cfa_base_preserved_val->locs->loc)];
373 }
374 else
375 {
376 for (i = 0; i < n_used_regs; i++)
377 REG_VALUES (used_regs[i]) = 0;
378 n_used_regs = 0;
379 }
fa49fd0f 380
457eeaae
JJ
381 if (cselib_preserve_constants)
382 htab_traverse (cselib_hash_table, preserve_only_constants, NULL);
383 else
384 htab_empty (cselib_hash_table);
fa49fd0f 385
fa49fd0f 386 n_useless_values = 0;
5847e8da
AO
387 n_useless_debug_values = 0;
388 n_debug_values = 0;
fa49fd0f 389
5440c0e7 390 next_uid = num;
7101fb18
JH
391
392 first_containing_mem = &dummy_val;
fa49fd0f
RK
393}
394
b5b8b0ac
AO
395/* Return the number of the next value that will be generated. */
396
397unsigned int
5440c0e7 398cselib_get_next_uid (void)
b5b8b0ac 399{
5440c0e7 400 return next_uid;
b5b8b0ac
AO
401}
402
4deef538
AO
403/* See the documentation of cselib_find_slot below. */
404static enum machine_mode find_slot_memmode;
405
406/* Search for X, whose hashcode is HASH, in CSELIB_HASH_TABLE,
407 INSERTing if requested. When X is part of the address of a MEM,
408 MEMMODE should specify the mode of the MEM. While searching the
409 table, MEMMODE is held in FIND_SLOT_MEMMODE, so that autoinc RTXs
410 in X can be resolved. */
411
412static void **
413cselib_find_slot (rtx x, hashval_t hash, enum insert_option insert,
414 enum machine_mode memmode)
415{
416 void **slot;
417 find_slot_memmode = memmode;
418 slot = htab_find_slot_with_hash (cselib_hash_table, x, hash, insert);
419 find_slot_memmode = VOIDmode;
420 return slot;
421}
422
fa49fd0f
RK
423/* The equality test for our hash table. The first argument ENTRY is a table
424 element (i.e. a cselib_val), while the second arg X is an rtx. We know
425 that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a
426 CONST of an appropriate mode. */
427
428static int
7080f735 429entry_and_rtx_equal_p (const void *entry, const void *x_arg)
fa49fd0f
RK
430{
431 struct elt_loc_list *l;
e5cfc29f 432 const cselib_val *const v = (const cselib_val *) entry;
f883e0a7 433 rtx x = CONST_CAST_RTX ((const_rtx)x_arg);
fa49fd0f
RK
434 enum machine_mode mode = GET_MODE (x);
435
481683e1 436 gcc_assert (!CONST_INT_P (x) && GET_CODE (x) != CONST_FIXED
341c100f 437 && (mode != VOIDmode || GET_CODE (x) != CONST_DOUBLE));
b8698a0f 438
757bbef8 439 if (mode != GET_MODE (v->val_rtx))
fa49fd0f
RK
440 return 0;
441
442 /* Unwrap X if necessary. */
443 if (GET_CODE (x) == CONST
481683e1 444 && (CONST_INT_P (XEXP (x, 0))
091a3ac7 445 || GET_CODE (XEXP (x, 0)) == CONST_FIXED
fa49fd0f
RK
446 || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE))
447 x = XEXP (x, 0);
7080f735 448
fa49fd0f
RK
449 /* We don't guarantee that distinct rtx's have different hash values,
450 so we need to do a comparison. */
451 for (l = v->locs; l; l = l->next)
4deef538 452 if (rtx_equal_for_cselib_1 (l->loc, x, find_slot_memmode))
5847e8da
AO
453 {
454 promote_debug_loc (l);
455 return 1;
456 }
fa49fd0f
RK
457
458 return 0;
459}
460
461/* The hash function for our hash table. The value is always computed with
0516f6fe
SB
462 cselib_hash_rtx when adding an element; this function just extracts the
463 hash value from a cselib_val structure. */
fa49fd0f 464
fb7e6024 465static hashval_t
7080f735 466get_value_hash (const void *entry)
fa49fd0f 467{
4f588890 468 const cselib_val *const v = (const cselib_val *) entry;
5440c0e7 469 return v->hash;
fa49fd0f
RK
470}
471
472/* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we
473 only return true for values which point to a cselib_val whose value
474 element has been set to zero, which implies the cselib_val will be
475 removed. */
476
477int
4f588890 478references_value_p (const_rtx x, int only_useless)
fa49fd0f 479{
4f588890 480 const enum rtx_code code = GET_CODE (x);
fa49fd0f
RK
481 const char *fmt = GET_RTX_FORMAT (code);
482 int i, j;
483
484 if (GET_CODE (x) == VALUE
485 && (! only_useless || CSELIB_VAL_PTR (x)->locs == 0))
486 return 1;
487
488 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
489 {
490 if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless))
491 return 1;
492 else if (fmt[i] == 'E')
493 for (j = 0; j < XVECLEN (x, i); j++)
494 if (references_value_p (XVECEXP (x, i, j), only_useless))
495 return 1;
496 }
497
498 return 0;
499}
500
501/* For all locations found in X, delete locations that reference useless
502 values (i.e. values without any location). Called through
503 htab_traverse. */
504
505static int
7080f735 506discard_useless_locs (void **x, void *info ATTRIBUTE_UNUSED)
fa49fd0f
RK
507{
508 cselib_val *v = (cselib_val *)*x;
509 struct elt_loc_list **p = &v->locs;
5847e8da
AO
510 bool had_locs = v->locs != NULL;
511 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
fa49fd0f
RK
512
513 while (*p)
514 {
515 if (references_value_p ((*p)->loc, 1))
516 unchain_one_elt_loc_list (p);
517 else
518 p = &(*p)->next;
519 }
520
b5b8b0ac 521 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
fa49fd0f 522 {
5847e8da
AO
523 if (setting_insn && DEBUG_INSN_P (setting_insn))
524 n_useless_debug_values++;
525 else
526 n_useless_values++;
fa49fd0f
RK
527 values_became_useless = 1;
528 }
529 return 1;
530}
531
532/* If X is a value with no locations, remove it from the hashtable. */
533
534static int
7080f735 535discard_useless_values (void **x, void *info ATTRIBUTE_UNUSED)
fa49fd0f
RK
536{
537 cselib_val *v = (cselib_val *)*x;
538
b5b8b0ac 539 if (v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
fa49fd0f 540 {
6fb5fa3c
DB
541 if (cselib_discard_hook)
542 cselib_discard_hook (v);
543
757bbef8 544 CSELIB_VAL_PTR (v->val_rtx) = NULL;
7c514720 545 htab_clear_slot (cselib_hash_table, x);
fa49fd0f
RK
546 unchain_one_value (v);
547 n_useless_values--;
548 }
549
550 return 1;
551}
552
553/* Clean out useless values (i.e. those which no longer have locations
554 associated with them) from the hash table. */
555
556static void
7080f735 557remove_useless_values (void)
fa49fd0f 558{
7101fb18 559 cselib_val **p, *v;
5847e8da 560
fa49fd0f
RK
561 /* First pass: eliminate locations that reference the value. That in
562 turn can make more values useless. */
563 do
564 {
565 values_became_useless = 0;
7c514720 566 htab_traverse (cselib_hash_table, discard_useless_locs, 0);
fa49fd0f
RK
567 }
568 while (values_became_useless);
569
570 /* Second pass: actually remove the values. */
fa49fd0f 571
7101fb18
JH
572 p = &first_containing_mem;
573 for (v = *p; v != &dummy_val; v = v->next_containing_mem)
574 if (v->locs)
575 {
576 *p = v;
577 p = &(*p)->next_containing_mem;
578 }
579 *p = &dummy_val;
580
5847e8da
AO
581 n_useless_values += n_useless_debug_values;
582 n_debug_values -= n_useless_debug_values;
583 n_useless_debug_values = 0;
584
7c514720 585 htab_traverse (cselib_hash_table, discard_useless_values, 0);
3e2a0bd2 586
341c100f 587 gcc_assert (!n_useless_values);
fa49fd0f
RK
588}
589
b5b8b0ac
AO
590/* Arrange for a value to not be removed from the hash table even if
591 it becomes useless. */
592
593void
594cselib_preserve_value (cselib_val *v)
595{
596 PRESERVED_VALUE_P (v->val_rtx) = 1;
597}
598
599/* Test whether a value is preserved. */
600
601bool
602cselib_preserved_value_p (cselib_val *v)
603{
604 return PRESERVED_VALUE_P (v->val_rtx);
605}
606
457eeaae
JJ
607/* Arrange for a REG value to be assumed constant through the whole function,
608 never invalidated and preserved across cselib_reset_table calls. */
609
610void
9de9cbaf 611cselib_preserve_cfa_base_value (cselib_val *v, unsigned int regno)
457eeaae
JJ
612{
613 if (cselib_preserve_constants
614 && v->locs
615 && REG_P (v->locs->loc))
9de9cbaf
JJ
616 {
617 cfa_base_preserved_val = v;
618 cfa_base_preserved_regno = regno;
619 }
457eeaae
JJ
620}
621
b5b8b0ac
AO
622/* Clean all non-constant expressions in the hash table, but retain
623 their values. */
624
625void
0de3e43f 626cselib_preserve_only_values (void)
b5b8b0ac
AO
627{
628 int i;
629
b5b8b0ac
AO
630 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
631 cselib_invalidate_regno (i, reg_raw_mode[i]);
632
633 cselib_invalidate_mem (callmem);
634
635 remove_useless_values ();
636
637 gcc_assert (first_containing_mem == &dummy_val);
638}
639
60fa6660
AO
640/* Return the mode in which a register was last set. If X is not a
641 register, return its mode. If the mode in which the register was
642 set is not known, or the value was already clobbered, return
643 VOIDmode. */
644
645enum machine_mode
4f588890 646cselib_reg_set_mode (const_rtx x)
60fa6660 647{
f8cfc6aa 648 if (!REG_P (x))
60fa6660
AO
649 return GET_MODE (x);
650
651 if (REG_VALUES (REGNO (x)) == NULL
652 || REG_VALUES (REGNO (x))->elt == NULL)
653 return VOIDmode;
654
757bbef8 655 return GET_MODE (REG_VALUES (REGNO (x))->elt->val_rtx);
60fa6660
AO
656}
657
fa49fd0f
RK
658/* Return nonzero if we can prove that X and Y contain the same value, taking
659 our gathered information into account. */
660
661int
7080f735 662rtx_equal_for_cselib_p (rtx x, rtx y)
4deef538
AO
663{
664 return rtx_equal_for_cselib_1 (x, y, VOIDmode);
665}
666
667/* If x is a PLUS or an autoinc operation, expand the operation,
668 storing the offset, if any, in *OFF. */
669
670static rtx
671autoinc_split (rtx x, rtx *off, enum machine_mode memmode)
672{
673 switch (GET_CODE (x))
674 {
675 case PLUS:
676 *off = XEXP (x, 1);
677 return XEXP (x, 0);
678
679 case PRE_DEC:
680 if (memmode == VOIDmode)
681 return x;
682
683 *off = GEN_INT (-GET_MODE_SIZE (memmode));
684 return XEXP (x, 0);
685 break;
686
687 case PRE_INC:
688 if (memmode == VOIDmode)
689 return x;
690
691 *off = GEN_INT (GET_MODE_SIZE (memmode));
692 return XEXP (x, 0);
693
694 case PRE_MODIFY:
695 return XEXP (x, 1);
696
697 case POST_DEC:
698 case POST_INC:
699 case POST_MODIFY:
700 return XEXP (x, 0);
701
702 default:
703 return x;
704 }
705}
706
707/* Return nonzero if we can prove that X and Y contain the same value,
708 taking our gathered information into account. MEMMODE holds the
709 mode of the enclosing MEM, if any, as required to deal with autoinc
710 addressing modes. If X and Y are not (known to be) part of
711 addresses, MEMMODE should be VOIDmode. */
712
713static int
714rtx_equal_for_cselib_1 (rtx x, rtx y, enum machine_mode memmode)
fa49fd0f
RK
715{
716 enum rtx_code code;
717 const char *fmt;
718 int i;
7080f735 719
f8cfc6aa 720 if (REG_P (x) || MEM_P (x))
fa49fd0f 721 {
4deef538 722 cselib_val *e = cselib_lookup (x, GET_MODE (x), 0, memmode);
fa49fd0f
RK
723
724 if (e)
757bbef8 725 x = e->val_rtx;
fa49fd0f
RK
726 }
727
f8cfc6aa 728 if (REG_P (y) || MEM_P (y))
fa49fd0f 729 {
4deef538 730 cselib_val *e = cselib_lookup (y, GET_MODE (y), 0, memmode);
fa49fd0f
RK
731
732 if (e)
757bbef8 733 y = e->val_rtx;
fa49fd0f
RK
734 }
735
736 if (x == y)
737 return 1;
738
739 if (GET_CODE (x) == VALUE && GET_CODE (y) == VALUE)
740 return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y);
741
742 if (GET_CODE (x) == VALUE)
743 {
744 cselib_val *e = CSELIB_VAL_PTR (x);
745 struct elt_loc_list *l;
746
747 for (l = e->locs; l; l = l->next)
748 {
749 rtx t = l->loc;
750
751 /* Avoid infinite recursion. */
3c0cb5de 752 if (REG_P (t) || MEM_P (t))
fa49fd0f 753 continue;
4deef538 754 else if (rtx_equal_for_cselib_1 (t, y, memmode))
fa49fd0f
RK
755 return 1;
756 }
7080f735 757
fa49fd0f
RK
758 return 0;
759 }
760
761 if (GET_CODE (y) == VALUE)
762 {
763 cselib_val *e = CSELIB_VAL_PTR (y);
764 struct elt_loc_list *l;
765
766 for (l = e->locs; l; l = l->next)
767 {
768 rtx t = l->loc;
769
3c0cb5de 770 if (REG_P (t) || MEM_P (t))
fa49fd0f 771 continue;
4deef538 772 else if (rtx_equal_for_cselib_1 (x, t, memmode))
fa49fd0f
RK
773 return 1;
774 }
7080f735 775
fa49fd0f
RK
776 return 0;
777 }
778
4deef538 779 if (GET_MODE (x) != GET_MODE (y))
fa49fd0f
RK
780 return 0;
781
4deef538
AO
782 if (GET_CODE (x) != GET_CODE (y))
783 {
784 rtx xorig = x, yorig = y;
785 rtx xoff = NULL, yoff = NULL;
786
787 x = autoinc_split (x, &xoff, memmode);
788 y = autoinc_split (y, &yoff, memmode);
789
790 if (!xoff != !yoff)
791 return 0;
792
793 if (xoff && !rtx_equal_for_cselib_1 (xoff, yoff, memmode))
794 return 0;
795
796 /* Don't recurse if nothing changed. */
797 if (x != xorig || y != yorig)
798 return rtx_equal_for_cselib_1 (x, y, memmode);
799
800 return 0;
801 }
802
37cf6116
RH
803 /* These won't be handled correctly by the code below. */
804 switch (GET_CODE (x))
805 {
806 case CONST_DOUBLE:
091a3ac7 807 case CONST_FIXED:
0ca5af51 808 case DEBUG_EXPR:
37cf6116
RH
809 return 0;
810
c8a27c40
JJ
811 case DEBUG_IMPLICIT_PTR:
812 return DEBUG_IMPLICIT_PTR_DECL (x)
813 == DEBUG_IMPLICIT_PTR_DECL (y);
814
ddb555ed
JJ
815 case DEBUG_PARAMETER_REF:
816 return DEBUG_PARAMETER_REF_DECL (x)
817 == DEBUG_PARAMETER_REF_DECL (y);
818
a58a8e4b 819 case ENTRY_VALUE:
2b80199f
JJ
820 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
821 use rtx_equal_for_cselib_1 to compare the operands. */
822 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
a58a8e4b 823
37cf6116
RH
824 case LABEL_REF:
825 return XEXP (x, 0) == XEXP (y, 0);
826
4deef538
AO
827 case MEM:
828 /* We have to compare any autoinc operations in the addresses
829 using this MEM's mode. */
830 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x));
831
37cf6116
RH
832 default:
833 break;
834 }
7080f735 835
fa49fd0f
RK
836 code = GET_CODE (x);
837 fmt = GET_RTX_FORMAT (code);
838
839 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
840 {
841 int j;
842
843 switch (fmt[i])
844 {
845 case 'w':
846 if (XWINT (x, i) != XWINT (y, i))
847 return 0;
848 break;
849
850 case 'n':
851 case 'i':
852 if (XINT (x, i) != XINT (y, i))
853 return 0;
854 break;
855
856 case 'V':
857 case 'E':
858 /* Two vectors must have the same length. */
859 if (XVECLEN (x, i) != XVECLEN (y, i))
860 return 0;
861
862 /* And the corresponding elements must match. */
863 for (j = 0; j < XVECLEN (x, i); j++)
4deef538
AO
864 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
865 XVECEXP (y, i, j), memmode))
fa49fd0f
RK
866 return 0;
867 break;
868
869 case 'e':
29c1846b
R
870 if (i == 1
871 && targetm.commutative_p (x, UNKNOWN)
4deef538
AO
872 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode)
873 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode))
29c1846b 874 return 1;
4deef538 875 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode))
fa49fd0f
RK
876 return 0;
877 break;
878
879 case 'S':
880 case 's':
881 if (strcmp (XSTR (x, i), XSTR (y, i)))
882 return 0;
883 break;
884
885 case 'u':
886 /* These are just backpointers, so they don't matter. */
887 break;
888
889 case '0':
890 case 't':
891 break;
892
893 /* It is believed that rtx's at this level will never
894 contain anything but integers and other rtx's,
895 except for within LABEL_REFs and SYMBOL_REFs. */
896 default:
341c100f 897 gcc_unreachable ();
fa49fd0f
RK
898 }
899 }
900 return 1;
901}
902
3af4ba41
RS
903/* We need to pass down the mode of constants through the hash table
904 functions. For that purpose, wrap them in a CONST of the appropriate
905 mode. */
906static rtx
907wrap_constant (enum machine_mode mode, rtx x)
908{
909 if (!CONST_INT_P (x) && GET_CODE (x) != CONST_FIXED
910 && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode))
911 return x;
912 gcc_assert (mode != VOIDmode);
913 return gen_rtx_CONST (mode, x);
914}
915
fa49fd0f
RK
916/* Hash an rtx. Return 0 if we couldn't hash the rtx.
917 For registers and memory locations, we look up their cselib_val structure
918 and return its VALUE element.
919 Possible reasons for return 0 are: the object is volatile, or we couldn't
920 find a register or memory location in the table and CREATE is zero. If
921 CREATE is nonzero, table elts are created for regs and mem.
29c1846b
R
922 N.B. this hash function returns the same hash value for RTXes that
923 differ only in the order of operands, thus it is suitable for comparisons
924 that take commutativity into account.
925 If we wanted to also support associative rules, we'd have to use a different
926 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
4deef538
AO
927 MEMMODE indicates the mode of an enclosing MEM, and it's only
928 used to compute autoinc values.
29c1846b
R
929 We used to have a MODE argument for hashing for CONST_INTs, but that
930 didn't make sense, since it caused spurious hash differences between
931 (set (reg:SI 1) (const_int))
932 (plus:SI (reg:SI 2) (reg:SI 1))
933 and
934 (plus:SI (reg:SI 2) (const_int))
935 If the mode is important in any context, it must be checked specifically
936 in a comparison anyway, since relying on hash differences is unsafe. */
fa49fd0f
RK
937
938static unsigned int
4deef538 939cselib_hash_rtx (rtx x, int create, enum machine_mode memmode)
fa49fd0f
RK
940{
941 cselib_val *e;
942 int i, j;
943 enum rtx_code code;
944 const char *fmt;
945 unsigned int hash = 0;
946
fa49fd0f
RK
947 code = GET_CODE (x);
948 hash += (unsigned) code + (unsigned) GET_MODE (x);
949
950 switch (code)
951 {
952 case MEM:
953 case REG:
4deef538 954 e = cselib_lookup (x, GET_MODE (x), create, memmode);
fa49fd0f
RK
955 if (! e)
956 return 0;
957
5440c0e7 958 return e->hash;
fa49fd0f 959
0ca5af51 960 case DEBUG_EXPR:
e4fb38bd
JJ
961 hash += ((unsigned) DEBUG_EXPR << 7)
962 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
0ca5af51
AO
963 return hash ? hash : (unsigned int) DEBUG_EXPR;
964
c8a27c40
JJ
965 case DEBUG_IMPLICIT_PTR:
966 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
967 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
968 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
969
ddb555ed
JJ
970 case DEBUG_PARAMETER_REF:
971 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
972 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
973 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
974
a58a8e4b 975 case ENTRY_VALUE:
2b80199f
JJ
976 /* ENTRY_VALUEs are function invariant, thus try to avoid
977 recursing on argument if ENTRY_VALUE is one of the
978 forms emitted by expand_debug_expr, otherwise
979 ENTRY_VALUE hash would depend on the current value
980 in some register or memory. */
981 if (REG_P (ENTRY_VALUE_EXP (x)))
982 hash += (unsigned int) REG
983 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
984 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
985 else if (MEM_P (ENTRY_VALUE_EXP (x))
986 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
987 hash += (unsigned int) MEM
988 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
989 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
990 else
991 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
a58a8e4b
JJ
992 return hash ? hash : (unsigned int) ENTRY_VALUE;
993
fa49fd0f 994 case CONST_INT:
29c1846b 995 hash += ((unsigned) CONST_INT << 7) + INTVAL (x);
dc76f41c 996 return hash ? hash : (unsigned int) CONST_INT;
fa49fd0f
RK
997
998 case CONST_DOUBLE:
999 /* This is like the general case, except that it only counts
1000 the integers representing the constant. */
1001 hash += (unsigned) code + (unsigned) GET_MODE (x);
1002 if (GET_MODE (x) != VOIDmode)
46b33600 1003 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
fa49fd0f
RK
1004 else
1005 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1006 + (unsigned) CONST_DOUBLE_HIGH (x));
dc76f41c 1007 return hash ? hash : (unsigned int) CONST_DOUBLE;
fa49fd0f 1008
091a3ac7
CF
1009 case CONST_FIXED:
1010 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1011 hash += fixed_hash (CONST_FIXED_VALUE (x));
1012 return hash ? hash : (unsigned int) CONST_FIXED;
1013
69ef87e2
AH
1014 case CONST_VECTOR:
1015 {
1016 int units;
1017 rtx elt;
1018
1019 units = CONST_VECTOR_NUNITS (x);
1020
1021 for (i = 0; i < units; ++i)
1022 {
1023 elt = CONST_VECTOR_ELT (x, i);
4deef538 1024 hash += cselib_hash_rtx (elt, 0, memmode);
69ef87e2
AH
1025 }
1026
1027 return hash;
1028 }
1029
fa49fd0f
RK
1030 /* Assume there is only one rtx object for any given label. */
1031 case LABEL_REF:
4c6669c2
RS
1032 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1033 differences and differences between each stage's debugging dumps. */
1034 hash += (((unsigned int) LABEL_REF << 7)
1035 + CODE_LABEL_NUMBER (XEXP (x, 0)));
dc76f41c 1036 return hash ? hash : (unsigned int) LABEL_REF;
fa49fd0f
RK
1037
1038 case SYMBOL_REF:
4c6669c2
RS
1039 {
1040 /* Don't hash on the symbol's address to avoid bootstrap differences.
1041 Different hash values may cause expressions to be recorded in
1042 different orders and thus different registers to be used in the
1043 final assembler. This also avoids differences in the dump files
1044 between various stages. */
1045 unsigned int h = 0;
1046 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1047
1048 while (*p)
1049 h += (h << 7) + *p++; /* ??? revisit */
1050
1051 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1052 return hash ? hash : (unsigned int) SYMBOL_REF;
1053 }
fa49fd0f
RK
1054
1055 case PRE_DEC:
1056 case PRE_INC:
4deef538
AO
1057 /* We can't compute these without knowing the MEM mode. */
1058 gcc_assert (memmode != VOIDmode);
1059 i = GET_MODE_SIZE (memmode);
1060 if (code == PRE_DEC)
1061 i = -i;
1062 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1063 like (mem:MEMMODE (plus (reg) (const_int I))). */
1064 hash += (unsigned) PLUS - (unsigned)code
1065 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1066 + cselib_hash_rtx (GEN_INT (i), create, memmode);
1067 return hash ? hash : 1 + (unsigned) PLUS;
1068
1069 case PRE_MODIFY:
1070 gcc_assert (memmode != VOIDmode);
1071 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1072
fa49fd0f
RK
1073 case POST_DEC:
1074 case POST_INC:
1075 case POST_MODIFY:
4deef538
AO
1076 gcc_assert (memmode != VOIDmode);
1077 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1078
fa49fd0f
RK
1079 case PC:
1080 case CC0:
1081 case CALL:
1082 case UNSPEC_VOLATILE:
1083 return 0;
1084
1085 case ASM_OPERANDS:
1086 if (MEM_VOLATILE_P (x))
1087 return 0;
1088
1089 break;
7080f735 1090
fa49fd0f
RK
1091 default:
1092 break;
1093 }
1094
1095 i = GET_RTX_LENGTH (code) - 1;
1096 fmt = GET_RTX_FORMAT (code);
1097 for (; i >= 0; i--)
1098 {
341c100f 1099 switch (fmt[i])
fa49fd0f 1100 {
341c100f 1101 case 'e':
fa49fd0f 1102 {
341c100f 1103 rtx tem = XEXP (x, i);
4deef538 1104 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
b8698a0f 1105
fa49fd0f
RK
1106 if (tem_hash == 0)
1107 return 0;
b8698a0f 1108
fa49fd0f
RK
1109 hash += tem_hash;
1110 }
341c100f
NS
1111 break;
1112 case 'E':
1113 for (j = 0; j < XVECLEN (x, i); j++)
1114 {
1115 unsigned int tem_hash
4deef538 1116 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
b8698a0f 1117
341c100f
NS
1118 if (tem_hash == 0)
1119 return 0;
b8698a0f 1120
341c100f
NS
1121 hash += tem_hash;
1122 }
1123 break;
fa49fd0f 1124
341c100f
NS
1125 case 's':
1126 {
1127 const unsigned char *p = (const unsigned char *) XSTR (x, i);
b8698a0f 1128
341c100f
NS
1129 if (p)
1130 while (*p)
1131 hash += *p++;
1132 break;
1133 }
b8698a0f 1134
341c100f
NS
1135 case 'i':
1136 hash += XINT (x, i);
1137 break;
1138
1139 case '0':
1140 case 't':
1141 /* unused */
1142 break;
b8698a0f 1143
341c100f
NS
1144 default:
1145 gcc_unreachable ();
fa49fd0f 1146 }
fa49fd0f
RK
1147 }
1148
dc76f41c 1149 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
fa49fd0f
RK
1150}
1151
1152/* Create a new value structure for VALUE and initialize it. The mode of the
1153 value is MODE. */
1154
6a59927d 1155static inline cselib_val *
5440c0e7 1156new_cselib_val (unsigned int hash, enum machine_mode mode, rtx x)
fa49fd0f 1157{
f883e0a7 1158 cselib_val *e = (cselib_val *) pool_alloc (cselib_val_pool);
fa49fd0f 1159
5440c0e7
AO
1160 gcc_assert (hash);
1161 gcc_assert (next_uid);
fa49fd0f 1162
5440c0e7
AO
1163 e->hash = hash;
1164 e->uid = next_uid++;
d67fb775
SB
1165 /* We use an alloc pool to allocate this RTL construct because it
1166 accounts for about 8% of the overall memory usage. We know
1167 precisely when we can have VALUE RTXen (when cselib is active)
daa956d0 1168 so we don't need to put them in garbage collected memory.
d67fb775 1169 ??? Why should a VALUE be an RTX in the first place? */
f883e0a7 1170 e->val_rtx = (rtx) pool_alloc (value_pool);
757bbef8
SB
1171 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1172 PUT_CODE (e->val_rtx, VALUE);
1173 PUT_MODE (e->val_rtx, mode);
1174 CSELIB_VAL_PTR (e->val_rtx) = e;
fa49fd0f
RK
1175 e->addr_list = 0;
1176 e->locs = 0;
7101fb18 1177 e->next_containing_mem = 0;
b5b8b0ac 1178
4a3c9687 1179 if (dump_file && (dump_flags & TDF_CSELIB))
b5b8b0ac 1180 {
5440c0e7 1181 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
b5b8b0ac
AO
1182 if (flag_dump_noaddr || flag_dump_unnumbered)
1183 fputs ("# ", dump_file);
1184 else
1185 fprintf (dump_file, "%p ", (void*)e);
1186 print_rtl_single (dump_file, x);
1187 fputc ('\n', dump_file);
1188 }
1189
fa49fd0f
RK
1190 return e;
1191}
1192
1193/* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1194 contains the data at this address. X is a MEM that represents the
1195 value. Update the two value structures to represent this situation. */
1196
1197static void
7080f735 1198add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
fa49fd0f 1199{
fa49fd0f
RK
1200 struct elt_loc_list *l;
1201
1202 /* Avoid duplicates. */
1203 for (l = mem_elt->locs; l; l = l->next)
3c0cb5de 1204 if (MEM_P (l->loc)
fa49fd0f 1205 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt)
5847e8da
AO
1206 {
1207 promote_debug_loc (l);
1208 return;
1209 }
fa49fd0f 1210
fa49fd0f 1211 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
f1ec5147
RK
1212 mem_elt->locs
1213 = new_elt_loc_list (mem_elt->locs,
757bbef8 1214 replace_equiv_address_nv (x, addr_elt->val_rtx));
7101fb18
JH
1215 if (mem_elt->next_containing_mem == NULL)
1216 {
1217 mem_elt->next_containing_mem = first_containing_mem;
1218 first_containing_mem = mem_elt;
1219 }
fa49fd0f
RK
1220}
1221
1222/* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1223 If CREATE, make a new one if we haven't seen it before. */
1224
1225static cselib_val *
7080f735 1226cselib_lookup_mem (rtx x, int create)
fa49fd0f
RK
1227{
1228 enum machine_mode mode = GET_MODE (x);
4deef538 1229 enum machine_mode addr_mode;
fa49fd0f
RK
1230 void **slot;
1231 cselib_val *addr;
1232 cselib_val *mem_elt;
1233 struct elt_list *l;
1234
1235 if (MEM_VOLATILE_P (x) || mode == BLKmode
463301c3 1236 || !cselib_record_memory
fa49fd0f
RK
1237 || (FLOAT_MODE_P (mode) && flag_float_store))
1238 return 0;
1239
4deef538
AO
1240 addr_mode = GET_MODE (XEXP (x, 0));
1241 if (addr_mode == VOIDmode)
1242 addr_mode = Pmode;
1243
fa49fd0f 1244 /* Look up the value for the address. */
4deef538 1245 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
fa49fd0f
RK
1246 if (! addr)
1247 return 0;
1248
1249 /* Find a value that describes a value of our mode at that address. */
1250 for (l = addr->addr_list; l; l = l->next)
757bbef8 1251 if (GET_MODE (l->elt->val_rtx) == mode)
5847e8da
AO
1252 {
1253 promote_debug_loc (l->elt->locs);
1254 return l->elt;
1255 }
fa49fd0f
RK
1256
1257 if (! create)
1258 return 0;
1259
5440c0e7 1260 mem_elt = new_cselib_val (next_uid, mode, x);
fa49fd0f 1261 add_mem_for_addr (addr, mem_elt, x);
4deef538
AO
1262 slot = cselib_find_slot (wrap_constant (mode, x), mem_elt->hash,
1263 INSERT, mode);
fa49fd0f
RK
1264 *slot = mem_elt;
1265 return mem_elt;
1266}
1267
6fb5fa3c
DB
1268/* Search thru the possible substitutions in P. We prefer a non reg
1269 substitution because this allows us to expand the tree further. If
1270 we find, just a reg, take the lowest regno. There may be several
1271 non-reg results, we just take the first one because they will all
1272 expand to the same place. */
1273
b8698a0f 1274static rtx
b5b8b0ac
AO
1275expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1276 int max_depth)
6fb5fa3c
DB
1277{
1278 rtx reg_result = NULL;
1279 unsigned int regno = UINT_MAX;
1280 struct elt_loc_list *p_in = p;
1281
1282 for (; p; p = p -> next)
1283 {
1284 /* Avoid infinite recursion trying to expand a reg into a
1285 the same reg. */
b8698a0f
L
1286 if ((REG_P (p->loc))
1287 && (REGNO (p->loc) < regno)
b5b8b0ac 1288 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
6fb5fa3c
DB
1289 {
1290 reg_result = p->loc;
1291 regno = REGNO (p->loc);
1292 }
1293 /* Avoid infinite recursion and do not try to expand the
1294 value. */
b8698a0f 1295 else if (GET_CODE (p->loc) == VALUE
6fb5fa3c
DB
1296 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1297 continue;
1298 else if (!REG_P (p->loc))
1299 {
8dd5516b 1300 rtx result, note;
4a3c9687 1301 if (dump_file && (dump_flags & TDF_CSELIB))
6fb5fa3c
DB
1302 {
1303 print_inline_rtx (dump_file, p->loc, 0);
1304 fprintf (dump_file, "\n");
1305 }
8dd5516b
JJ
1306 if (GET_CODE (p->loc) == LO_SUM
1307 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1308 && p->setting_insn
1309 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1310 && XEXP (note, 0) == XEXP (p->loc, 1))
1311 return XEXP (p->loc, 1);
b5b8b0ac 1312 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
6fb5fa3c
DB
1313 if (result)
1314 return result;
1315 }
b8698a0f 1316
6fb5fa3c 1317 }
b8698a0f 1318
6fb5fa3c
DB
1319 if (regno != UINT_MAX)
1320 {
1321 rtx result;
4a3c9687 1322 if (dump_file && (dump_flags & TDF_CSELIB))
6fb5fa3c
DB
1323 fprintf (dump_file, "r%d\n", regno);
1324
b5b8b0ac 1325 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
6fb5fa3c
DB
1326 if (result)
1327 return result;
1328 }
1329
4a3c9687 1330 if (dump_file && (dump_flags & TDF_CSELIB))
6fb5fa3c
DB
1331 {
1332 if (reg_result)
1333 {
1334 print_inline_rtx (dump_file, reg_result, 0);
1335 fprintf (dump_file, "\n");
1336 }
b8698a0f 1337 else
6fb5fa3c
DB
1338 fprintf (dump_file, "NULL\n");
1339 }
1340 return reg_result;
1341}
1342
1343
1344/* Forward substitute and expand an expression out to its roots.
1345 This is the opposite of common subexpression. Because local value
1346 numbering is such a weak optimization, the expanded expression is
1347 pretty much unique (not from a pointer equals point of view but
b8698a0f 1348 from a tree shape point of view.
6fb5fa3c
DB
1349
1350 This function returns NULL if the expansion fails. The expansion
1351 will fail if there is no value number for one of the operands or if
1352 one of the operands has been overwritten between the current insn
1353 and the beginning of the basic block. For instance x has no
1354 expansion in:
1355
1356 r1 <- r1 + 3
1357 x <- r1 + 8
1358
1359 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1360 It is clear on return. */
1361
1362rtx
1363cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
b5b8b0ac
AO
1364{
1365 struct expand_value_data evd;
1366
1367 evd.regs_active = regs_active;
1368 evd.callback = NULL;
1369 evd.callback_arg = NULL;
864ddef7 1370 evd.dummy = false;
b5b8b0ac
AO
1371
1372 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1373}
1374
1375/* Same as cselib_expand_value_rtx, but using a callback to try to
0b7e34d7
AO
1376 resolve some expressions. The CB function should return ORIG if it
1377 can't or does not want to deal with a certain RTX. Any other
1378 return value, including NULL, will be used as the expansion for
1379 VALUE, without any further changes. */
b5b8b0ac
AO
1380
1381rtx
1382cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1383 cselib_expand_callback cb, void *data)
1384{
1385 struct expand_value_data evd;
1386
1387 evd.regs_active = regs_active;
1388 evd.callback = cb;
1389 evd.callback_arg = data;
864ddef7 1390 evd.dummy = false;
b5b8b0ac
AO
1391
1392 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1393}
1394
864ddef7
JJ
1395/* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1396 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1397 would return NULL or non-NULL, without allocating new rtx. */
1398
1399bool
1400cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1401 cselib_expand_callback cb, void *data)
1402{
1403 struct expand_value_data evd;
1404
1405 evd.regs_active = regs_active;
1406 evd.callback = cb;
1407 evd.callback_arg = data;
1408 evd.dummy = true;
1409
1410 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1411}
1412
0b7e34d7
AO
1413/* Internal implementation of cselib_expand_value_rtx and
1414 cselib_expand_value_rtx_cb. */
1415
b5b8b0ac
AO
1416static rtx
1417cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1418 int max_depth)
6fb5fa3c
DB
1419{
1420 rtx copy, scopy;
1421 int i, j;
1422 RTX_CODE code;
1423 const char *format_ptr;
8dd5516b 1424 enum machine_mode mode;
6fb5fa3c
DB
1425
1426 code = GET_CODE (orig);
1427
1428 /* For the context of dse, if we end up expand into a huge tree, we
1429 will not have a useful address, so we might as well just give up
1430 quickly. */
1431 if (max_depth <= 0)
1432 return NULL;
1433
1434 switch (code)
1435 {
1436 case REG:
1437 {
1438 struct elt_list *l = REG_VALUES (REGNO (orig));
1439
1440 if (l && l->elt == NULL)
1441 l = l->next;
1442 for (; l; l = l->next)
1443 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1444 {
1445 rtx result;
1446 int regno = REGNO (orig);
b8698a0f 1447
6fb5fa3c 1448 /* The only thing that we are not willing to do (this
6ed3da00 1449 is requirement of dse and if others potential uses
6fb5fa3c
DB
1450 need this function we should add a parm to control
1451 it) is that we will not substitute the
1452 STACK_POINTER_REGNUM, FRAME_POINTER or the
1453 HARD_FRAME_POINTER.
1454
cea618ac 1455 These expansions confuses the code that notices that
6fb5fa3c
DB
1456 stores into the frame go dead at the end of the
1457 function and that the frame is not effected by calls
1458 to subroutines. If you allow the
1459 STACK_POINTER_REGNUM substitution, then dse will
1460 think that parameter pushing also goes dead which is
1461 wrong. If you allow the FRAME_POINTER or the
1462 HARD_FRAME_POINTER then you lose the opportunity to
1463 make the frame assumptions. */
1464 if (regno == STACK_POINTER_REGNUM
1465 || regno == FRAME_POINTER_REGNUM
1466 || regno == HARD_FRAME_POINTER_REGNUM)
1467 return orig;
1468
b5b8b0ac 1469 bitmap_set_bit (evd->regs_active, regno);
6fb5fa3c 1470
4a3c9687 1471 if (dump_file && (dump_flags & TDF_CSELIB))
6fb5fa3c
DB
1472 fprintf (dump_file, "expanding: r%d into: ", regno);
1473
b5b8b0ac
AO
1474 result = expand_loc (l->elt->locs, evd, max_depth);
1475 bitmap_clear_bit (evd->regs_active, regno);
6fb5fa3c
DB
1476
1477 if (result)
1478 return result;
b8698a0f 1479 else
6fb5fa3c
DB
1480 return orig;
1481 }
1482 }
b8698a0f 1483
6fb5fa3c
DB
1484 case CONST_INT:
1485 case CONST_DOUBLE:
1486 case CONST_VECTOR:
1487 case SYMBOL_REF:
1488 case CODE_LABEL:
1489 case PC:
1490 case CC0:
1491 case SCRATCH:
1492 /* SCRATCH must be shared because they represent distinct values. */
1493 return orig;
1494 case CLOBBER:
1495 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1496 return orig;
1497 break;
1498
1499 case CONST:
1500 if (shared_const_p (orig))
1501 return orig;
1502 break;
1503
8dd5516b 1504 case SUBREG:
6fb5fa3c 1505 {
0b7e34d7
AO
1506 rtx subreg;
1507
1508 if (evd->callback)
1509 {
1510 subreg = evd->callback (orig, evd->regs_active, max_depth,
1511 evd->callback_arg);
1512 if (subreg != orig)
1513 return subreg;
1514 }
1515
1516 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1517 max_depth - 1);
8dd5516b
JJ
1518 if (!subreg)
1519 return NULL;
1520 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1521 GET_MODE (SUBREG_REG (orig)),
1522 SUBREG_BYTE (orig));
0b7e34d7
AO
1523 if (scopy == NULL
1524 || (GET_CODE (scopy) == SUBREG
1525 && !REG_P (SUBREG_REG (scopy))
1526 && !MEM_P (SUBREG_REG (scopy))))
1527 return NULL;
1528
8dd5516b 1529 return scopy;
6fb5fa3c 1530 }
8dd5516b
JJ
1531
1532 case VALUE:
b5b8b0ac
AO
1533 {
1534 rtx result;
0b7e34d7 1535
4a3c9687 1536 if (dump_file && (dump_flags & TDF_CSELIB))
b5b8b0ac
AO
1537 {
1538 fputs ("\nexpanding ", dump_file);
1539 print_rtl_single (dump_file, orig);
1540 fputs (" into...", dump_file);
1541 }
8dd5516b 1542
0b7e34d7 1543 if (evd->callback)
b5b8b0ac
AO
1544 {
1545 result = evd->callback (orig, evd->regs_active, max_depth,
1546 evd->callback_arg);
0b7e34d7
AO
1547
1548 if (result != orig)
1549 return result;
b5b8b0ac 1550 }
8dd5516b 1551
0b7e34d7 1552 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
b5b8b0ac
AO
1553 return result;
1554 }
0ca5af51
AO
1555
1556 case DEBUG_EXPR:
1557 if (evd->callback)
1558 return evd->callback (orig, evd->regs_active, max_depth,
1559 evd->callback_arg);
1560 return orig;
1561
6fb5fa3c
DB
1562 default:
1563 break;
1564 }
1565
1566 /* Copy the various flags, fields, and other information. We assume
1567 that all fields need copying, and then clear the fields that should
1568 not be copied. That is the sensible default behavior, and forces
1569 us to explicitly document why we are *not* copying a flag. */
864ddef7
JJ
1570 if (evd->dummy)
1571 copy = NULL;
1572 else
1573 copy = shallow_copy_rtx (orig);
6fb5fa3c 1574
8dd5516b 1575 format_ptr = GET_RTX_FORMAT (code);
6fb5fa3c 1576
8dd5516b 1577 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6fb5fa3c
DB
1578 switch (*format_ptr++)
1579 {
1580 case 'e':
1581 if (XEXP (orig, i) != NULL)
1582 {
b5b8b0ac
AO
1583 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1584 max_depth - 1);
6fb5fa3c
DB
1585 if (!result)
1586 return NULL;
864ddef7
JJ
1587 if (copy)
1588 XEXP (copy, i) = result;
6fb5fa3c
DB
1589 }
1590 break;
1591
1592 case 'E':
1593 case 'V':
1594 if (XVEC (orig, i) != NULL)
1595 {
864ddef7
JJ
1596 if (copy)
1597 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1598 for (j = 0; j < XVECLEN (orig, i); j++)
6fb5fa3c 1599 {
b5b8b0ac
AO
1600 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1601 evd, max_depth - 1);
6fb5fa3c
DB
1602 if (!result)
1603 return NULL;
864ddef7
JJ
1604 if (copy)
1605 XVECEXP (copy, i, j) = result;
6fb5fa3c
DB
1606 }
1607 }
1608 break;
1609
1610 case 't':
1611 case 'w':
1612 case 'i':
1613 case 's':
1614 case 'S':
1615 case 'T':
1616 case 'u':
1617 case 'B':
1618 case '0':
1619 /* These are left unchanged. */
1620 break;
1621
1622 default:
1623 gcc_unreachable ();
1624 }
1625
864ddef7
JJ
1626 if (evd->dummy)
1627 return orig;
1628
8dd5516b
JJ
1629 mode = GET_MODE (copy);
1630 /* If an operand has been simplified into CONST_INT, which doesn't
1631 have a mode and the mode isn't derivable from whole rtx's mode,
1632 try simplify_*_operation first with mode from original's operand
1633 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1634 scopy = copy;
1635 switch (GET_RTX_CLASS (code))
1636 {
1637 case RTX_UNARY:
1638 if (CONST_INT_P (XEXP (copy, 0))
1639 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1640 {
1641 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1642 GET_MODE (XEXP (orig, 0)));
1643 if (scopy)
1644 return scopy;
1645 }
1646 break;
1647 case RTX_COMM_ARITH:
1648 case RTX_BIN_ARITH:
1649 /* These expressions can derive operand modes from the whole rtx's mode. */
1650 break;
1651 case RTX_TERNARY:
1652 case RTX_BITFIELD_OPS:
1653 if (CONST_INT_P (XEXP (copy, 0))
1654 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1655 {
1656 scopy = simplify_ternary_operation (code, mode,
1657 GET_MODE (XEXP (orig, 0)),
1658 XEXP (copy, 0), XEXP (copy, 1),
1659 XEXP (copy, 2));
1660 if (scopy)
1661 return scopy;
1662 }
1663 break;
1664 case RTX_COMPARE:
1665 case RTX_COMM_COMPARE:
1666 if (CONST_INT_P (XEXP (copy, 0))
1667 && GET_MODE (XEXP (copy, 1)) == VOIDmode
1668 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
1669 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
1670 {
1671 scopy = simplify_relational_operation (code, mode,
1672 (GET_MODE (XEXP (orig, 0))
1673 != VOIDmode)
1674 ? GET_MODE (XEXP (orig, 0))
1675 : GET_MODE (XEXP (orig, 1)),
1676 XEXP (copy, 0),
1677 XEXP (copy, 1));
1678 if (scopy)
1679 return scopy;
1680 }
1681 break;
1682 default:
1683 break;
1684 }
6fb5fa3c
DB
1685 scopy = simplify_rtx (copy);
1686 if (scopy)
3af4ba41 1687 return scopy;
6fb5fa3c
DB
1688 return copy;
1689}
1690
fa49fd0f
RK
1691/* Walk rtx X and replace all occurrences of REG and MEM subexpressions
1692 with VALUE expressions. This way, it becomes independent of changes
1693 to registers and memory.
1694 X isn't actually modified; if modifications are needed, new rtl is
4deef538
AO
1695 allocated. However, the return value can share rtl with X.
1696 If X is within a MEM, MEMMODE must be the mode of the MEM. */
fa49fd0f 1697
91700444 1698rtx
4deef538 1699cselib_subst_to_values (rtx x, enum machine_mode memmode)
fa49fd0f
RK
1700{
1701 enum rtx_code code = GET_CODE (x);
1702 const char *fmt = GET_RTX_FORMAT (code);
1703 cselib_val *e;
1704 struct elt_list *l;
1705 rtx copy = x;
1706 int i;
1707
1708 switch (code)
1709 {
1710 case REG:
60fa6660
AO
1711 l = REG_VALUES (REGNO (x));
1712 if (l && l->elt == NULL)
1713 l = l->next;
1714 for (; l; l = l->next)
757bbef8
SB
1715 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
1716 return l->elt->val_rtx;
fa49fd0f 1717
341c100f 1718 gcc_unreachable ();
fa49fd0f
RK
1719
1720 case MEM:
1721 e = cselib_lookup_mem (x, 0);
4deef538
AO
1722 /* This used to happen for autoincrements, but we deal with them
1723 properly now. Remove the if stmt for the next release. */
fa49fd0f 1724 if (! e)
91700444 1725 {
4deef538 1726 /* Assign a value that doesn't match any other. */
5440c0e7 1727 e = new_cselib_val (next_uid, GET_MODE (x), x);
91700444 1728 }
757bbef8 1729 return e->val_rtx;
fa49fd0f 1730
509f4495
JJ
1731 case ENTRY_VALUE:
1732 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
1733 if (! e)
1734 break;
1735 return e->val_rtx;
1736
fa49fd0f 1737 case CONST_DOUBLE:
69ef87e2 1738 case CONST_VECTOR:
fa49fd0f 1739 case CONST_INT:
091a3ac7 1740 case CONST_FIXED:
fa49fd0f
RK
1741 return x;
1742
4deef538 1743 case PRE_DEC:
91700444 1744 case PRE_INC:
4deef538
AO
1745 gcc_assert (memmode != VOIDmode);
1746 i = GET_MODE_SIZE (memmode);
1747 if (code == PRE_DEC)
1748 i = -i;
1749 return cselib_subst_to_values (plus_constant (XEXP (x, 0), i),
1750 memmode);
1751
1752 case PRE_MODIFY:
1753 gcc_assert (memmode != VOIDmode);
1754 return cselib_subst_to_values (XEXP (x, 1), memmode);
1755
91700444 1756 case POST_DEC:
4deef538 1757 case POST_INC:
91700444 1758 case POST_MODIFY:
4deef538
AO
1759 gcc_assert (memmode != VOIDmode);
1760 return cselib_subst_to_values (XEXP (x, 0), memmode);
7080f735 1761
fa49fd0f
RK
1762 default:
1763 break;
1764 }
1765
1766 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1767 {
1768 if (fmt[i] == 'e')
1769 {
4deef538 1770 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
fa49fd0f 1771
bd7960b1
RS
1772 if (t != XEXP (x, i))
1773 {
1774 if (x == copy)
1775 copy = shallow_copy_rtx (x);
1776 XEXP (copy, i) = t;
1777 }
fa49fd0f
RK
1778 }
1779 else if (fmt[i] == 'E')
1780 {
bd7960b1 1781 int j;
fa49fd0f
RK
1782
1783 for (j = 0; j < XVECLEN (x, i); j++)
1784 {
4deef538 1785 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
fa49fd0f 1786
bd7960b1 1787 if (t != XVECEXP (x, i, j))
fa49fd0f 1788 {
bd7960b1
RS
1789 if (XVEC (x, i) == XVEC (copy, i))
1790 {
1791 if (x == copy)
1792 copy = shallow_copy_rtx (x);
1793 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
1794 }
1795 XVECEXP (copy, i, j) = t;
fa49fd0f 1796 }
fa49fd0f
RK
1797 }
1798 }
1799 }
1800
1801 return copy;
1802}
1803
4deef538
AO
1804/* Look up the rtl expression X in our tables and return the value it
1805 has. If CREATE is zero, we return NULL if we don't know the value.
1806 Otherwise, we create a new one if possible, using mode MODE if X
1807 doesn't have a mode (i.e. because it's a constant). When X is part
1808 of an address, MEMMODE should be the mode of the enclosing MEM if
1809 we're tracking autoinc expressions. */
fa49fd0f 1810
5847e8da 1811static cselib_val *
4deef538
AO
1812cselib_lookup_1 (rtx x, enum machine_mode mode,
1813 int create, enum machine_mode memmode)
fa49fd0f
RK
1814{
1815 void **slot;
1816 cselib_val *e;
1817 unsigned int hashval;
1818
1819 if (GET_MODE (x) != VOIDmode)
1820 mode = GET_MODE (x);
1821
1822 if (GET_CODE (x) == VALUE)
1823 return CSELIB_VAL_PTR (x);
1824
f8cfc6aa 1825 if (REG_P (x))
fa49fd0f
RK
1826 {
1827 struct elt_list *l;
1828 unsigned int i = REGNO (x);
1829
60fa6660
AO
1830 l = REG_VALUES (i);
1831 if (l && l->elt == NULL)
1832 l = l->next;
1833 for (; l; l = l->next)
757bbef8 1834 if (mode == GET_MODE (l->elt->val_rtx))
5847e8da
AO
1835 {
1836 promote_debug_loc (l->elt->locs);
1837 return l->elt;
1838 }
fa49fd0f
RK
1839
1840 if (! create)
5847e8da 1841 return 0;
fa49fd0f 1842
31825e57
DM
1843 if (i < FIRST_PSEUDO_REGISTER)
1844 {
66fd46b6 1845 unsigned int n = hard_regno_nregs[i][mode];
31825e57
DM
1846
1847 if (n > max_value_regs)
1848 max_value_regs = n;
1849 }
1850
5440c0e7 1851 e = new_cselib_val (next_uid, GET_MODE (x), x);
fa49fd0f
RK
1852 e->locs = new_elt_loc_list (e->locs, x);
1853 if (REG_VALUES (i) == 0)
60fa6660
AO
1854 {
1855 /* Maintain the invariant that the first entry of
1856 REG_VALUES, if present, must be the value used to set the
1857 register, or NULL. */
6790d1ab 1858 used_regs[n_used_regs++] = i;
60fa6660
AO
1859 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
1860 }
509f4495
JJ
1861 else if (cselib_preserve_constants
1862 && GET_MODE_CLASS (mode) == MODE_INT)
1863 {
1864 /* During var-tracking, try harder to find equivalences
1865 for SUBREGs. If a setter sets say a DImode register
1866 and user uses that register only in SImode, add a lowpart
1867 subreg location. */
1868 struct elt_list *lwider = NULL;
1869 l = REG_VALUES (i);
1870 if (l && l->elt == NULL)
1871 l = l->next;
1872 for (; l; l = l->next)
1873 if (GET_MODE_CLASS (GET_MODE (l->elt->val_rtx)) == MODE_INT
1874 && GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
1875 > GET_MODE_SIZE (mode)
1876 && (lwider == NULL
1877 || GET_MODE_SIZE (GET_MODE (l->elt->val_rtx))
1878 < GET_MODE_SIZE (GET_MODE (lwider->elt->val_rtx))))
1879 {
1880 struct elt_loc_list *el;
1881 if (i < FIRST_PSEUDO_REGISTER
1882 && hard_regno_nregs[i][GET_MODE (l->elt->val_rtx)] != 1)
1883 continue;
1884 for (el = l->elt->locs; el; el = el->next)
1885 if (!REG_P (el->loc))
1886 break;
1887 if (el)
1888 lwider = l;
1889 }
1890 if (lwider)
1891 {
1892 rtx sub = lowpart_subreg (mode, lwider->elt->val_rtx,
1893 GET_MODE (lwider->elt->val_rtx));
1894 if (sub)
1895 e->locs->next = new_elt_loc_list (e->locs->next, sub);
1896 }
1897 }
60fa6660 1898 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
4deef538 1899 slot = cselib_find_slot (x, e->hash, INSERT, memmode);
fa49fd0f 1900 *slot = e;
5847e8da 1901 return e;
fa49fd0f
RK
1902 }
1903
3c0cb5de 1904 if (MEM_P (x))
5847e8da 1905 return cselib_lookup_mem (x, create);
fa49fd0f 1906
4deef538 1907 hashval = cselib_hash_rtx (x, create, memmode);
fa49fd0f
RK
1908 /* Can't even create if hashing is not possible. */
1909 if (! hashval)
5847e8da 1910 return 0;
fa49fd0f 1911
4deef538
AO
1912 slot = cselib_find_slot (wrap_constant (mode, x), hashval,
1913 create ? INSERT : NO_INSERT, memmode);
fa49fd0f 1914 if (slot == 0)
5847e8da 1915 return 0;
fa49fd0f
RK
1916
1917 e = (cselib_val *) *slot;
1918 if (e)
5847e8da 1919 return e;
fa49fd0f 1920
b5b8b0ac 1921 e = new_cselib_val (hashval, mode, x);
fa49fd0f
RK
1922
1923 /* We have to fill the slot before calling cselib_subst_to_values:
1924 the hash table is inconsistent until we do so, and
1925 cselib_subst_to_values will need to do lookups. */
1926 *slot = (void *) e;
4deef538
AO
1927 e->locs = new_elt_loc_list (e->locs,
1928 cselib_subst_to_values (x, memmode));
5847e8da
AO
1929 return e;
1930}
1931
1932/* Wrapper for cselib_lookup, that indicates X is in INSN. */
1933
1934cselib_val *
1935cselib_lookup_from_insn (rtx x, enum machine_mode mode,
4deef538 1936 int create, enum machine_mode memmode, rtx insn)
5847e8da
AO
1937{
1938 cselib_val *ret;
1939
1940 gcc_assert (!cselib_current_insn);
1941 cselib_current_insn = insn;
1942
4deef538 1943 ret = cselib_lookup (x, mode, create, memmode);
5847e8da
AO
1944
1945 cselib_current_insn = NULL;
1946
1947 return ret;
1948}
1949
1950/* Wrapper for cselib_lookup_1, that logs the lookup result and
1951 maintains invariants related with debug insns. */
1952
1953cselib_val *
4deef538
AO
1954cselib_lookup (rtx x, enum machine_mode mode,
1955 int create, enum machine_mode memmode)
5847e8da 1956{
4deef538 1957 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
5847e8da
AO
1958
1959 /* ??? Should we return NULL if we're not to create an entry, the
1960 found loc is a debug loc and cselib_current_insn is not DEBUG?
1961 If so, we should also avoid converting val to non-DEBUG; probably
1962 easiest setting cselib_current_insn to NULL before the call
1963 above. */
1964
4a3c9687 1965 if (dump_file && (dump_flags & TDF_CSELIB))
5847e8da
AO
1966 {
1967 fputs ("cselib lookup ", dump_file);
1968 print_inline_rtx (dump_file, x, 2);
1969 fprintf (dump_file, " => %u:%u\n",
1970 ret ? ret->uid : 0,
1971 ret ? ret->hash : 0);
1972 }
1973
1974 return ret;
fa49fd0f
RK
1975}
1976
1977/* Invalidate any entries in reg_values that overlap REGNO. This is called
1978 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
1979 is used to determine how many hard registers are being changed. If MODE
1980 is VOIDmode, then only REGNO is being changed; this is used when
1981 invalidating call clobbered registers across a call. */
1982
1983static void
7080f735 1984cselib_invalidate_regno (unsigned int regno, enum machine_mode mode)
fa49fd0f
RK
1985{
1986 unsigned int endregno;
1987 unsigned int i;
1988
1989 /* If we see pseudos after reload, something is _wrong_. */
341c100f
NS
1990 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
1991 || reg_renumber[regno] < 0);
fa49fd0f
RK
1992
1993 /* Determine the range of registers that must be invalidated. For
1994 pseudos, only REGNO is affected. For hard regs, we must take MODE
1995 into account, and we must also invalidate lower register numbers
1996 if they contain values that overlap REGNO. */
291aac59 1997 if (regno < FIRST_PSEUDO_REGISTER)
31825e57 1998 {
341c100f 1999 gcc_assert (mode != VOIDmode);
7080f735 2000
31825e57
DM
2001 if (regno < max_value_regs)
2002 i = 0;
2003 else
2004 i = regno - max_value_regs;
fa49fd0f 2005
09e18274 2006 endregno = end_hard_regno (mode, regno);
31825e57
DM
2007 }
2008 else
2009 {
2010 i = regno;
2011 endregno = regno + 1;
2012 }
2013
2014 for (; i < endregno; i++)
fa49fd0f
RK
2015 {
2016 struct elt_list **l = &REG_VALUES (i);
2017
2018 /* Go through all known values for this reg; if it overlaps the range
2019 we're invalidating, remove the value. */
2020 while (*l)
2021 {
2022 cselib_val *v = (*l)->elt;
5847e8da
AO
2023 bool had_locs;
2024 rtx setting_insn;
fa49fd0f
RK
2025 struct elt_loc_list **p;
2026 unsigned int this_last = i;
2027
60fa6660 2028 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
09e18274 2029 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
fa49fd0f 2030
9de9cbaf
JJ
2031 if (this_last < regno || v == NULL
2032 || (v == cfa_base_preserved_val
2033 && i == cfa_base_preserved_regno))
fa49fd0f
RK
2034 {
2035 l = &(*l)->next;
2036 continue;
2037 }
2038
2039 /* We have an overlap. */
60fa6660
AO
2040 if (*l == REG_VALUES (i))
2041 {
2042 /* Maintain the invariant that the first entry of
2043 REG_VALUES, if present, must be the value used to set
2044 the register, or NULL. This is also nice because
2045 then we won't push the same regno onto user_regs
2046 multiple times. */
2047 (*l)->elt = NULL;
2048 l = &(*l)->next;
2049 }
2050 else
2051 unchain_one_elt_list (l);
fa49fd0f 2052
5847e8da
AO
2053 had_locs = v->locs != NULL;
2054 setting_insn = v->locs ? v->locs->setting_insn : NULL;
2055
fa49fd0f
RK
2056 /* Now, we clear the mapping from value to reg. It must exist, so
2057 this code will crash intentionally if it doesn't. */
2058 for (p = &v->locs; ; p = &(*p)->next)
2059 {
2060 rtx x = (*p)->loc;
2061
f8cfc6aa 2062 if (REG_P (x) && REGNO (x) == i)
fa49fd0f
RK
2063 {
2064 unchain_one_elt_loc_list (p);
2065 break;
2066 }
2067 }
5847e8da
AO
2068
2069 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
2070 {
2071 if (setting_insn && DEBUG_INSN_P (setting_insn))
2072 n_useless_debug_values++;
2073 else
2074 n_useless_values++;
2075 }
fa49fd0f
RK
2076 }
2077 }
2078}
9ddb66ca
JH
2079\f
2080/* Return 1 if X has a value that can vary even between two
2081 executions of the program. 0 means X can be compared reliably
2082 against certain constants or near-constants. */
fa49fd0f 2083
4f588890
KG
2084static bool
2085cselib_rtx_varies_p (const_rtx x ATTRIBUTE_UNUSED, bool from_alias ATTRIBUTE_UNUSED)
fa49fd0f 2086{
9ddb66ca
JH
2087 /* We actually don't need to verify very hard. This is because
2088 if X has actually changed, we invalidate the memory anyway,
2089 so assume that all common memory addresses are
2090 invariant. */
fa49fd0f
RK
2091 return 0;
2092}
2093
7101fb18
JH
2094/* Invalidate any locations in the table which are changed because of a
2095 store to MEM_RTX. If this is called because of a non-const call
2096 instruction, MEM_RTX is (mem:BLK const0_rtx). */
fa49fd0f 2097
7101fb18 2098static void
7080f735 2099cselib_invalidate_mem (rtx mem_rtx)
fa49fd0f 2100{
7101fb18 2101 cselib_val **vp, *v, *next;
c65ecebc 2102 int num_mems = 0;
9ddb66ca
JH
2103 rtx mem_addr;
2104
2105 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2106 mem_rtx = canon_rtx (mem_rtx);
fa49fd0f 2107
7101fb18
JH
2108 vp = &first_containing_mem;
2109 for (v = *vp; v != &dummy_val; v = next)
fa49fd0f 2110 {
7101fb18
JH
2111 bool has_mem = false;
2112 struct elt_loc_list **p = &v->locs;
5847e8da
AO
2113 bool had_locs = v->locs != NULL;
2114 rtx setting_insn = v->locs ? v->locs->setting_insn : NULL;
fa49fd0f 2115
7101fb18 2116 while (*p)
fa49fd0f 2117 {
7101fb18
JH
2118 rtx x = (*p)->loc;
2119 cselib_val *addr;
2120 struct elt_list **mem_chain;
2121
2122 /* MEMs may occur in locations only at the top level; below
2123 that every MEM or REG is substituted by its VALUE. */
3c0cb5de 2124 if (!MEM_P (x))
fa49fd0f 2125 {
7101fb18
JH
2126 p = &(*p)->next;
2127 continue;
2128 }
c65ecebc 2129 if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS)
9ddb66ca 2130 && ! canon_true_dependence (mem_rtx, GET_MODE (mem_rtx), mem_addr,
6216f94e 2131 x, NULL_RTX, cselib_rtx_varies_p))
7101fb18
JH
2132 {
2133 has_mem = true;
c65ecebc 2134 num_mems++;
7101fb18
JH
2135 p = &(*p)->next;
2136 continue;
fa49fd0f
RK
2137 }
2138
7101fb18
JH
2139 /* This one overlaps. */
2140 /* We must have a mapping from this MEM's address to the
2141 value (E). Remove that, too. */
4deef538 2142 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
7101fb18
JH
2143 mem_chain = &addr->addr_list;
2144 for (;;)
2145 {
2146 if ((*mem_chain)->elt == v)
2147 {
2148 unchain_one_elt_list (mem_chain);
2149 break;
2150 }
fa49fd0f 2151
7101fb18
JH
2152 mem_chain = &(*mem_chain)->next;
2153 }
fa49fd0f 2154
7101fb18
JH
2155 unchain_one_elt_loc_list (p);
2156 }
fa49fd0f 2157
b5b8b0ac 2158 if (had_locs && v->locs == 0 && !PRESERVED_VALUE_P (v->val_rtx))
5847e8da
AO
2159 {
2160 if (setting_insn && DEBUG_INSN_P (setting_insn))
2161 n_useless_debug_values++;
2162 else
2163 n_useless_values++;
2164 }
fa49fd0f 2165
7101fb18
JH
2166 next = v->next_containing_mem;
2167 if (has_mem)
2168 {
2169 *vp = v;
2170 vp = &(*vp)->next_containing_mem;
2171 }
2172 else
2173 v->next_containing_mem = NULL;
2174 }
2175 *vp = &dummy_val;
fa49fd0f
RK
2176}
2177
0d87c765 2178/* Invalidate DEST, which is being assigned to or clobbered. */
fa49fd0f 2179
0d87c765
RH
2180void
2181cselib_invalidate_rtx (rtx dest)
fa49fd0f 2182{
46d096a3
SB
2183 while (GET_CODE (dest) == SUBREG
2184 || GET_CODE (dest) == ZERO_EXTRACT
2185 || GET_CODE (dest) == STRICT_LOW_PART)
fa49fd0f
RK
2186 dest = XEXP (dest, 0);
2187
f8cfc6aa 2188 if (REG_P (dest))
fa49fd0f 2189 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
3c0cb5de 2190 else if (MEM_P (dest))
fa49fd0f 2191 cselib_invalidate_mem (dest);
0d87c765
RH
2192}
2193
2194/* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2195
2196static void
7bc980e1 2197cselib_invalidate_rtx_note_stores (rtx dest, const_rtx ignore ATTRIBUTE_UNUSED,
0d87c765
RH
2198 void *data ATTRIBUTE_UNUSED)
2199{
2200 cselib_invalidate_rtx (dest);
fa49fd0f
RK
2201}
2202
2203/* Record the result of a SET instruction. DEST is being set; the source
2204 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2205 describes its address. */
2206
2207static void
7080f735 2208cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
fa49fd0f 2209{
f8cfc6aa 2210 int dreg = REG_P (dest) ? (int) REGNO (dest) : -1;
fa49fd0f
RK
2211
2212 if (src_elt == 0 || side_effects_p (dest))
2213 return;
2214
2215 if (dreg >= 0)
2216 {
31825e57
DM
2217 if (dreg < FIRST_PSEUDO_REGISTER)
2218 {
66fd46b6 2219 unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)];
31825e57
DM
2220
2221 if (n > max_value_regs)
2222 max_value_regs = n;
2223 }
2224
60fa6660
AO
2225 if (REG_VALUES (dreg) == 0)
2226 {
6790d1ab 2227 used_regs[n_used_regs++] = dreg;
60fa6660
AO
2228 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2229 }
2230 else
2231 {
341c100f
NS
2232 /* The register should have been invalidated. */
2233 gcc_assert (REG_VALUES (dreg)->elt == 0);
2234 REG_VALUES (dreg)->elt = src_elt;
60fa6660
AO
2235 }
2236
b5b8b0ac 2237 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
fa49fd0f
RK
2238 n_useless_values--;
2239 src_elt->locs = new_elt_loc_list (src_elt->locs, dest);
2240 }
3c0cb5de 2241 else if (MEM_P (dest) && dest_addr_elt != 0
463301c3 2242 && cselib_record_memory)
fa49fd0f 2243 {
b5b8b0ac 2244 if (src_elt->locs == 0 && !PRESERVED_VALUE_P (src_elt->val_rtx))
fa49fd0f
RK
2245 n_useless_values--;
2246 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2247 }
2248}
2249
fa49fd0f
RK
2250/* There is no good way to determine how many elements there can be
2251 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2252#define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2253
4deef538
AO
2254struct cselib_record_autoinc_data
2255{
2256 struct cselib_set *sets;
2257 int n_sets;
2258};
2259
2260/* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2261 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2262
2263static int
2264cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2265 rtx dest, rtx src, rtx srcoff, void *arg)
2266{
2267 struct cselib_record_autoinc_data *data;
2268 data = (struct cselib_record_autoinc_data *)arg;
2269
2270 data->sets[data->n_sets].dest = dest;
2271
2272 if (srcoff)
2273 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2274 else
2275 data->sets[data->n_sets].src = src;
2276
2277 data->n_sets++;
2278
2279 return -1;
2280}
2281
2282/* Record the effects of any sets and autoincs in INSN. */
fa49fd0f 2283static void
7080f735 2284cselib_record_sets (rtx insn)
fa49fd0f
RK
2285{
2286 int n_sets = 0;
2287 int i;
b5b8b0ac 2288 struct cselib_set sets[MAX_SETS];
fa49fd0f 2289 rtx body = PATTERN (insn);
b7933c21 2290 rtx cond = 0;
4deef538
AO
2291 int n_sets_before_autoinc;
2292 struct cselib_record_autoinc_data data;
fa49fd0f
RK
2293
2294 body = PATTERN (insn);
b7933c21
BS
2295 if (GET_CODE (body) == COND_EXEC)
2296 {
2297 cond = COND_EXEC_TEST (body);
2298 body = COND_EXEC_CODE (body);
2299 }
2300
fa49fd0f
RK
2301 /* Find all sets. */
2302 if (GET_CODE (body) == SET)
2303 {
2304 sets[0].src = SET_SRC (body);
2305 sets[0].dest = SET_DEST (body);
2306 n_sets = 1;
2307 }
2308 else if (GET_CODE (body) == PARALLEL)
2309 {
2310 /* Look through the PARALLEL and record the values being
2311 set, if possible. Also handle any CLOBBERs. */
2312 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2313 {
2314 rtx x = XVECEXP (body, 0, i);
2315
2316 if (GET_CODE (x) == SET)
2317 {
2318 sets[n_sets].src = SET_SRC (x);
2319 sets[n_sets].dest = SET_DEST (x);
2320 n_sets++;
2321 }
2322 }
2323 }
2324
8dd5516b
JJ
2325 if (n_sets == 1
2326 && MEM_P (sets[0].src)
2327 && !cselib_record_memory
2328 && MEM_READONLY_P (sets[0].src))
2329 {
2330 rtx note = find_reg_equal_equiv_note (insn);
2331
2332 if (note && CONSTANT_P (XEXP (note, 0)))
2333 sets[0].src = XEXP (note, 0);
2334 }
2335
4deef538
AO
2336 data.sets = sets;
2337 data.n_sets = n_sets_before_autoinc = n_sets;
2338 for_each_inc_dec (&insn, cselib_record_autoinc_cb, &data);
2339 n_sets = data.n_sets;
2340
fa49fd0f
RK
2341 /* Look up the values that are read. Do this before invalidating the
2342 locations that are written. */
2343 for (i = 0; i < n_sets; i++)
2344 {
2345 rtx dest = sets[i].dest;
2346
2347 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2348 the low part after invalidating any knowledge about larger modes. */
2349 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2350 sets[i].dest = dest = XEXP (dest, 0);
2351
2352 /* We don't know how to record anything but REG or MEM. */
f8cfc6aa 2353 if (REG_P (dest)
3c0cb5de 2354 || (MEM_P (dest) && cselib_record_memory))
fa49fd0f 2355 {
b7933c21
BS
2356 rtx src = sets[i].src;
2357 if (cond)
be9ed5d5 2358 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
4deef538 2359 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
3c0cb5de 2360 if (MEM_P (dest))
d4ebfa65
BE
2361 {
2362 enum machine_mode address_mode
2363 = targetm.addr_space.address_mode (MEM_ADDR_SPACE (dest));
2364
2365 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
4deef538
AO
2366 address_mode, 1,
2367 GET_MODE (dest));
d4ebfa65 2368 }
fa49fd0f
RK
2369 else
2370 sets[i].dest_addr_elt = 0;
2371 }
2372 }
2373
b5b8b0ac
AO
2374 if (cselib_record_sets_hook)
2375 cselib_record_sets_hook (insn, sets, n_sets);
2376
fa49fd0f
RK
2377 /* Invalidate all locations written by this insn. Note that the elts we
2378 looked up in the previous loop aren't affected, just some of their
2379 locations may go away. */
0d87c765 2380 note_stores (body, cselib_invalidate_rtx_note_stores, NULL);
fa49fd0f 2381
4deef538
AO
2382 for (i = n_sets_before_autoinc; i < n_sets; i++)
2383 cselib_invalidate_rtx (sets[i].dest);
2384
b7048ab7
RH
2385 /* If this is an asm, look for duplicate sets. This can happen when the
2386 user uses the same value as an output multiple times. This is valid
2387 if the outputs are not actually used thereafter. Treat this case as
2388 if the value isn't actually set. We do this by smashing the destination
2389 to pc_rtx, so that we won't record the value later. */
2390 if (n_sets >= 2 && asm_noperands (body) >= 0)
2391 {
2392 for (i = 0; i < n_sets; i++)
2393 {
2394 rtx dest = sets[i].dest;
3c0cb5de 2395 if (REG_P (dest) || MEM_P (dest))
b7048ab7
RH
2396 {
2397 int j;
2398 for (j = i + 1; j < n_sets; j++)
2399 if (rtx_equal_p (dest, sets[j].dest))
2400 {
2401 sets[i].dest = pc_rtx;
2402 sets[j].dest = pc_rtx;
2403 }
2404 }
2405 }
2406 }
2407
fa49fd0f
RK
2408 /* Now enter the equivalences in our tables. */
2409 for (i = 0; i < n_sets; i++)
2410 {
2411 rtx dest = sets[i].dest;
f8cfc6aa 2412 if (REG_P (dest)
3c0cb5de 2413 || (MEM_P (dest) && cselib_record_memory))
fa49fd0f
RK
2414 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2415 }
2416}
2417
2418/* Record the effects of INSN. */
2419
2420void
7080f735 2421cselib_process_insn (rtx insn)
fa49fd0f
RK
2422{
2423 int i;
2424 rtx x;
2425
2426 cselib_current_insn = insn;
2427
2428 /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */
4b4bf941
JQ
2429 if (LABEL_P (insn)
2430 || (CALL_P (insn)
570a98eb 2431 && find_reg_note (insn, REG_SETJMP, NULL))
4b4bf941 2432 || (NONJUMP_INSN_P (insn)
fa49fd0f
RK
2433 && GET_CODE (PATTERN (insn)) == ASM_OPERANDS
2434 && MEM_VOLATILE_P (PATTERN (insn))))
2435 {
5440c0e7 2436 cselib_reset_table (next_uid);
2080bd29 2437 cselib_current_insn = NULL_RTX;
fa49fd0f
RK
2438 return;
2439 }
2440
2441 if (! INSN_P (insn))
2442 {
2080bd29 2443 cselib_current_insn = NULL_RTX;
fa49fd0f
RK
2444 return;
2445 }
2446
2447 /* If this is a call instruction, forget anything stored in a
2448 call clobbered register, or, if this is not a const call, in
2449 memory. */
4b4bf941 2450 if (CALL_P (insn))
fa49fd0f
RK
2451 {
2452 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
7e42db17
DJ
2453 if (call_used_regs[i]
2454 || (REG_VALUES (i) && REG_VALUES (i)->elt
b8698a0f 2455 && HARD_REGNO_CALL_PART_CLOBBERED (i,
757bbef8 2456 GET_MODE (REG_VALUES (i)->elt->val_rtx))))
291aac59 2457 cselib_invalidate_regno (i, reg_raw_mode[i]);
fa49fd0f 2458
becfd6e5
KZ
2459 /* Since it is not clear how cselib is going to be used, be
2460 conservative here and treat looping pure or const functions
2461 as if they were regular functions. */
2462 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
2463 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
fa49fd0f
RK
2464 cselib_invalidate_mem (callmem);
2465 }
2466
2467 cselib_record_sets (insn);
2468
fa49fd0f
RK
2469 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
2470 after we have processed the insn. */
4b4bf941 2471 if (CALL_P (insn))
fa49fd0f
RK
2472 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
2473 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
0d87c765 2474 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
fa49fd0f 2475
2080bd29 2476 cselib_current_insn = NULL_RTX;
fa49fd0f 2477
96d0cc81
JH
2478 if (n_useless_values > MAX_USELESS_VALUES
2479 /* remove_useless_values is linear in the hash table size. Avoid
9f5ed61a 2480 quadratic behavior for very large hashtables with very few
96d0cc81 2481 useless elements. */
5847e8da
AO
2482 && ((unsigned int)n_useless_values
2483 > (cselib_hash_table->n_elements
2484 - cselib_hash_table->n_deleted
2485 - n_debug_values) / 4))
fa49fd0f
RK
2486 remove_useless_values ();
2487}
2488
fa49fd0f
RK
2489/* Initialize cselib for one pass. The caller must also call
2490 init_alias_analysis. */
2491
2492void
457eeaae 2493cselib_init (int record_what)
fa49fd0f 2494{
b8698a0f 2495 elt_list_pool = create_alloc_pool ("elt_list",
6a59927d 2496 sizeof (struct elt_list), 10);
b8698a0f 2497 elt_loc_list_pool = create_alloc_pool ("elt_loc_list",
6a59927d 2498 sizeof (struct elt_loc_list), 10);
b8698a0f 2499 cselib_val_pool = create_alloc_pool ("cselib_val_list",
6a59927d 2500 sizeof (cselib_val), 10);
aacd3885 2501 value_pool = create_alloc_pool ("value", RTX_CODE_SIZE (VALUE), 100);
457eeaae
JJ
2502 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
2503 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
ac3768f6
SB
2504
2505 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
2506 see canon_true_dependence. This is only created once. */
fa49fd0f 2507 if (! callmem)
ac3768f6 2508 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
fa49fd0f
RK
2509
2510 cselib_nregs = max_reg_num ();
6790d1ab
JH
2511
2512 /* We preserve reg_values to allow expensive clearing of the whole thing.
2513 Reallocate it however if it happens to be too large. */
2514 if (!reg_values || reg_values_size < cselib_nregs
2515 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
e2500fed 2516 {
04695783 2517 free (reg_values);
6790d1ab
JH
2518 /* Some space for newly emit instructions so we don't end up
2519 reallocating in between passes. */
2520 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
5ed6ace5 2521 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
e2500fed 2522 }
5ed6ace5 2523 used_regs = XNEWVEC (unsigned int, cselib_nregs);
6790d1ab 2524 n_used_regs = 0;
7c514720
KH
2525 cselib_hash_table = htab_create (31, get_value_hash,
2526 entry_and_rtx_equal_p, NULL);
5440c0e7 2527 next_uid = 1;
fa49fd0f
RK
2528}
2529
2530/* Called when the current user is done with cselib. */
2531
2532void
7080f735 2533cselib_finish (void)
fa49fd0f 2534{
6fb5fa3c 2535 cselib_discard_hook = NULL;
457eeaae
JJ
2536 cselib_preserve_constants = false;
2537 cfa_base_preserved_val = NULL;
9de9cbaf 2538 cfa_base_preserved_regno = INVALID_REGNUM;
6a59927d
JH
2539 free_alloc_pool (elt_list_pool);
2540 free_alloc_pool (elt_loc_list_pool);
2541 free_alloc_pool (cselib_val_pool);
23bd7a93 2542 free_alloc_pool (value_pool);
eb232f4e 2543 cselib_clear_table ();
7c514720 2544 htab_delete (cselib_hash_table);
0fc0c4c9 2545 free (used_regs);
e2500fed 2546 used_regs = 0;
7c514720 2547 cselib_hash_table = 0;
e2500fed 2548 n_useless_values = 0;
5847e8da
AO
2549 n_useless_debug_values = 0;
2550 n_debug_values = 0;
5440c0e7 2551 next_uid = 0;
fa49fd0f 2552}
e2500fed 2553
b5b8b0ac
AO
2554/* Dump the cselib_val *X to FILE *info. */
2555
2556static int
2557dump_cselib_val (void **x, void *info)
2558{
2559 cselib_val *v = (cselib_val *)*x;
2560 FILE *out = (FILE *)info;
2561 bool need_lf = true;
2562
2563 print_inline_rtx (out, v->val_rtx, 0);
2564
2565 if (v->locs)
2566 {
2567 struct elt_loc_list *l = v->locs;
2568 if (need_lf)
2569 {
2570 fputc ('\n', out);
2571 need_lf = false;
2572 }
2573 fputs (" locs:", out);
2574 do
2575 {
2576 fprintf (out, "\n from insn %i ",
2577 INSN_UID (l->setting_insn));
2578 print_inline_rtx (out, l->loc, 4);
2579 }
2580 while ((l = l->next));
2581 fputc ('\n', out);
2582 }
2583 else
2584 {
2585 fputs (" no locs", out);
2586 need_lf = true;
2587 }
2588
2589 if (v->addr_list)
2590 {
2591 struct elt_list *e = v->addr_list;
2592 if (need_lf)
2593 {
2594 fputc ('\n', out);
2595 need_lf = false;
2596 }
2597 fputs (" addr list:", out);
2598 do
2599 {
2600 fputs ("\n ", out);
2601 print_inline_rtx (out, e->elt->val_rtx, 2);
2602 }
2603 while ((e = e->next));
2604 fputc ('\n', out);
2605 }
2606 else
2607 {
2608 fputs (" no addrs", out);
2609 need_lf = true;
2610 }
2611
2612 if (v->next_containing_mem == &dummy_val)
2613 fputs (" last mem\n", out);
2614 else if (v->next_containing_mem)
2615 {
2616 fputs (" next mem ", out);
2617 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
2618 fputc ('\n', out);
2619 }
2620 else if (need_lf)
2621 fputc ('\n', out);
2622
2623 return 1;
2624}
2625
2626/* Dump to OUT everything in the CSELIB table. */
2627
2628void
2629dump_cselib_table (FILE *out)
2630{
2631 fprintf (out, "cselib hash table:\n");
2632 htab_traverse (cselib_hash_table, dump_cselib_val, out);
2633 if (first_containing_mem != &dummy_val)
2634 {
2635 fputs ("first mem ", out);
2636 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
2637 fputc ('\n', out);
2638 }
5440c0e7 2639 fprintf (out, "next uid %i\n", next_uid);
b5b8b0ac
AO
2640}
2641
e2500fed 2642#include "gt-cselib.h"