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