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