]> git.ipfire.org Git - thirdparty/gcc.git/blame - gcc/cselib.c
[Ada] Improved support for aspect alignment in CCG
[thirdparty/gcc.git] / gcc / cselib.c
CommitLineData
fa49fd0f 1/* Common subexpression elimination library for GNU compiler.
8d9254fc 2 Copyright (C) 1987-2020 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
c8a27c40
JJ
1051 case DEBUG_IMPLICIT_PTR:
1052 return DEBUG_IMPLICIT_PTR_DECL (x)
1053 == DEBUG_IMPLICIT_PTR_DECL (y);
1054
ddb555ed
JJ
1055 case DEBUG_PARAMETER_REF:
1056 return DEBUG_PARAMETER_REF_DECL (x)
1057 == DEBUG_PARAMETER_REF_DECL (y);
1058
a58a8e4b 1059 case ENTRY_VALUE:
2b80199f
JJ
1060 /* ENTRY_VALUEs are function invariant, it is thus undesirable to
1061 use rtx_equal_for_cselib_1 to compare the operands. */
1062 return rtx_equal_p (ENTRY_VALUE_EXP (x), ENTRY_VALUE_EXP (y));
a58a8e4b 1063
37cf6116 1064 case LABEL_REF:
04a121a7 1065 return label_ref_label (x) == label_ref_label (y);
37cf6116 1066
9fccb335
RS
1067 case REG:
1068 return REGNO (x) == REGNO (y);
1069
4deef538
AO
1070 case MEM:
1071 /* We have to compare any autoinc operations in the addresses
1072 using this MEM's mode. */
005f12bf
JJ
1073 return rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 0), GET_MODE (x),
1074 depth);
4deef538 1075
37cf6116
RH
1076 default:
1077 break;
1078 }
7080f735 1079
fa49fd0f
RK
1080 code = GET_CODE (x);
1081 fmt = GET_RTX_FORMAT (code);
1082
1083 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
1084 {
1085 int j;
1086
1087 switch (fmt[i])
1088 {
1089 case 'w':
1090 if (XWINT (x, i) != XWINT (y, i))
1091 return 0;
1092 break;
1093
1094 case 'n':
1095 case 'i':
1096 if (XINT (x, i) != XINT (y, i))
1097 return 0;
1098 break;
1099
91914e56
RS
1100 case 'p':
1101 if (maybe_ne (SUBREG_BYTE (x), SUBREG_BYTE (y)))
1102 return 0;
1103 break;
1104
fa49fd0f
RK
1105 case 'V':
1106 case 'E':
1107 /* Two vectors must have the same length. */
1108 if (XVECLEN (x, i) != XVECLEN (y, i))
1109 return 0;
1110
1111 /* And the corresponding elements must match. */
1112 for (j = 0; j < XVECLEN (x, i); j++)
4deef538 1113 if (! rtx_equal_for_cselib_1 (XVECEXP (x, i, j),
005f12bf 1114 XVECEXP (y, i, j), memmode, depth))
fa49fd0f
RK
1115 return 0;
1116 break;
1117
1118 case 'e':
29c1846b
R
1119 if (i == 1
1120 && targetm.commutative_p (x, UNKNOWN)
005f12bf
JJ
1121 && rtx_equal_for_cselib_1 (XEXP (x, 1), XEXP (y, 0), memmode,
1122 depth)
1123 && rtx_equal_for_cselib_1 (XEXP (x, 0), XEXP (y, 1), memmode,
1124 depth))
29c1846b 1125 return 1;
005f12bf
JJ
1126 if (! rtx_equal_for_cselib_1 (XEXP (x, i), XEXP (y, i), memmode,
1127 depth))
fa49fd0f
RK
1128 return 0;
1129 break;
1130
1131 case 'S':
1132 case 's':
1133 if (strcmp (XSTR (x, i), XSTR (y, i)))
1134 return 0;
1135 break;
1136
1137 case 'u':
1138 /* These are just backpointers, so they don't matter. */
1139 break;
1140
1141 case '0':
1142 case 't':
1143 break;
1144
1145 /* It is believed that rtx's at this level will never
1146 contain anything but integers and other rtx's,
1147 except for within LABEL_REFs and SYMBOL_REFs. */
1148 default:
341c100f 1149 gcc_unreachable ();
fa49fd0f
RK
1150 }
1151 }
1152 return 1;
1153}
1154
2c0fa3ec
JJ
1155/* Helper function for cselib_hash_rtx. Arguments like for cselib_hash_rtx,
1156 except that it hashes (plus:P x c). */
1157
1158static unsigned int
1159cselib_hash_plus_const_int (rtx x, HOST_WIDE_INT c, int create,
1160 machine_mode memmode)
1161{
1162 cselib_val *e = cselib_lookup (x, GET_MODE (x), create, memmode);
1163 if (! e)
1164 return 0;
1165
1166 if (! SP_DERIVED_VALUE_P (e->val_rtx))
1167 for (struct elt_loc_list *l = e->locs; l; l = l->next)
1168 if (GET_CODE (l->loc) == PLUS
1169 && GET_CODE (XEXP (l->loc, 0)) == VALUE
1170 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
1171 && CONST_INT_P (XEXP (l->loc, 1)))
1172 {
1173 e = CSELIB_VAL_PTR (XEXP (l->loc, 0));
1174 c = trunc_int_for_mode (c + UINTVAL (XEXP (l->loc, 1)), Pmode);
1175 break;
1176 }
1177 if (c == 0)
1178 return e->hash;
1179
1180 unsigned hash = (unsigned) PLUS + (unsigned) GET_MODE (x);
1181 hash += e->hash;
1182 unsigned int tem_hash = (unsigned) CONST_INT + (unsigned) VOIDmode;
1183 tem_hash += ((unsigned) CONST_INT << 7) + (unsigned HOST_WIDE_INT) c;
1184 if (tem_hash == 0)
1185 tem_hash = (unsigned int) CONST_INT;
1186 hash += tem_hash;
1187 return hash ? hash : 1 + (unsigned int) PLUS;
1188}
1189
fa49fd0f
RK
1190/* Hash an rtx. Return 0 if we couldn't hash the rtx.
1191 For registers and memory locations, we look up their cselib_val structure
1192 and return its VALUE element.
1193 Possible reasons for return 0 are: the object is volatile, or we couldn't
1194 find a register or memory location in the table and CREATE is zero. If
1195 CREATE is nonzero, table elts are created for regs and mem.
29c1846b
R
1196 N.B. this hash function returns the same hash value for RTXes that
1197 differ only in the order of operands, thus it is suitable for comparisons
1198 that take commutativity into account.
1199 If we wanted to also support associative rules, we'd have to use a different
1200 strategy to avoid returning spurious 0, e.g. return ~(~0U >> 1) .
4deef538
AO
1201 MEMMODE indicates the mode of an enclosing MEM, and it's only
1202 used to compute autoinc values.
29c1846b
R
1203 We used to have a MODE argument for hashing for CONST_INTs, but that
1204 didn't make sense, since it caused spurious hash differences between
1205 (set (reg:SI 1) (const_int))
1206 (plus:SI (reg:SI 2) (reg:SI 1))
1207 and
1208 (plus:SI (reg:SI 2) (const_int))
1209 If the mode is important in any context, it must be checked specifically
1210 in a comparison anyway, since relying on hash differences is unsafe. */
fa49fd0f
RK
1211
1212static unsigned int
ef4bddc2 1213cselib_hash_rtx (rtx x, int create, machine_mode memmode)
fa49fd0f
RK
1214{
1215 cselib_val *e;
cf098191 1216 poly_int64 offset;
fa49fd0f
RK
1217 int i, j;
1218 enum rtx_code code;
1219 const char *fmt;
1220 unsigned int hash = 0;
1221
fa49fd0f
RK
1222 code = GET_CODE (x);
1223 hash += (unsigned) code + (unsigned) GET_MODE (x);
1224
1225 switch (code)
1226 {
7483eef8
AO
1227 case VALUE:
1228 e = CSELIB_VAL_PTR (x);
1229 return e->hash;
1230
fa49fd0f
RK
1231 case MEM:
1232 case REG:
4deef538 1233 e = cselib_lookup (x, GET_MODE (x), create, memmode);
fa49fd0f
RK
1234 if (! e)
1235 return 0;
1236
5440c0e7 1237 return e->hash;
fa49fd0f 1238
0ca5af51 1239 case DEBUG_EXPR:
e4fb38bd
JJ
1240 hash += ((unsigned) DEBUG_EXPR << 7)
1241 + DEBUG_TEMP_UID (DEBUG_EXPR_TREE_DECL (x));
0ca5af51
AO
1242 return hash ? hash : (unsigned int) DEBUG_EXPR;
1243
c8a27c40
JJ
1244 case DEBUG_IMPLICIT_PTR:
1245 hash += ((unsigned) DEBUG_IMPLICIT_PTR << 7)
1246 + DECL_UID (DEBUG_IMPLICIT_PTR_DECL (x));
1247 return hash ? hash : (unsigned int) DEBUG_IMPLICIT_PTR;
1248
ddb555ed
JJ
1249 case DEBUG_PARAMETER_REF:
1250 hash += ((unsigned) DEBUG_PARAMETER_REF << 7)
1251 + DECL_UID (DEBUG_PARAMETER_REF_DECL (x));
1252 return hash ? hash : (unsigned int) DEBUG_PARAMETER_REF;
1253
a58a8e4b 1254 case ENTRY_VALUE:
2b80199f
JJ
1255 /* ENTRY_VALUEs are function invariant, thus try to avoid
1256 recursing on argument if ENTRY_VALUE is one of the
1257 forms emitted by expand_debug_expr, otherwise
1258 ENTRY_VALUE hash would depend on the current value
1259 in some register or memory. */
1260 if (REG_P (ENTRY_VALUE_EXP (x)))
1261 hash += (unsigned int) REG
1262 + (unsigned int) GET_MODE (ENTRY_VALUE_EXP (x))
1263 + (unsigned int) REGNO (ENTRY_VALUE_EXP (x));
1264 else if (MEM_P (ENTRY_VALUE_EXP (x))
1265 && REG_P (XEXP (ENTRY_VALUE_EXP (x), 0)))
1266 hash += (unsigned int) MEM
1267 + (unsigned int) GET_MODE (XEXP (ENTRY_VALUE_EXP (x), 0))
1268 + (unsigned int) REGNO (XEXP (ENTRY_VALUE_EXP (x), 0));
1269 else
1270 hash += cselib_hash_rtx (ENTRY_VALUE_EXP (x), create, memmode);
a58a8e4b
JJ
1271 return hash ? hash : (unsigned int) ENTRY_VALUE;
1272
fa49fd0f 1273 case CONST_INT:
a8acccdd 1274 hash += ((unsigned) CONST_INT << 7) + UINTVAL (x);
dc76f41c 1275 return hash ? hash : (unsigned int) CONST_INT;
fa49fd0f 1276
807e902e
KZ
1277 case CONST_WIDE_INT:
1278 for (i = 0; i < CONST_WIDE_INT_NUNITS (x); i++)
1279 hash += CONST_WIDE_INT_ELT (x, i);
1280 return hash;
1281
0c12fc9b
RS
1282 case CONST_POLY_INT:
1283 {
1284 inchash::hash h;
1285 h.add_int (hash);
1286 for (unsigned int i = 0; i < NUM_POLY_INT_COEFFS; ++i)
1287 h.add_wide_int (CONST_POLY_INT_COEFFS (x)[i]);
1288 return h.end ();
1289 }
1290
fa49fd0f
RK
1291 case CONST_DOUBLE:
1292 /* This is like the general case, except that it only counts
1293 the integers representing the constant. */
1294 hash += (unsigned) code + (unsigned) GET_MODE (x);
807e902e 1295 if (TARGET_SUPPORTS_WIDE_INT == 0 && GET_MODE (x) == VOIDmode)
fa49fd0f
RK
1296 hash += ((unsigned) CONST_DOUBLE_LOW (x)
1297 + (unsigned) CONST_DOUBLE_HIGH (x));
807e902e
KZ
1298 else
1299 hash += real_hash (CONST_DOUBLE_REAL_VALUE (x));
dc76f41c 1300 return hash ? hash : (unsigned int) CONST_DOUBLE;
fa49fd0f 1301
091a3ac7
CF
1302 case CONST_FIXED:
1303 hash += (unsigned int) code + (unsigned int) GET_MODE (x);
1304 hash += fixed_hash (CONST_FIXED_VALUE (x));
1305 return hash ? hash : (unsigned int) CONST_FIXED;
1306
69ef87e2
AH
1307 case CONST_VECTOR:
1308 {
1309 int units;
1310 rtx elt;
1311
16c78b66 1312 units = const_vector_encoded_nelts (x);
69ef87e2
AH
1313
1314 for (i = 0; i < units; ++i)
1315 {
16c78b66 1316 elt = CONST_VECTOR_ENCODED_ELT (x, i);
4deef538 1317 hash += cselib_hash_rtx (elt, 0, memmode);
69ef87e2
AH
1318 }
1319
1320 return hash;
1321 }
1322
fa49fd0f
RK
1323 /* Assume there is only one rtx object for any given label. */
1324 case LABEL_REF:
4c6669c2
RS
1325 /* We don't hash on the address of the CODE_LABEL to avoid bootstrap
1326 differences and differences between each stage's debugging dumps. */
1327 hash += (((unsigned int) LABEL_REF << 7)
04a121a7 1328 + CODE_LABEL_NUMBER (label_ref_label (x)));
dc76f41c 1329 return hash ? hash : (unsigned int) LABEL_REF;
fa49fd0f
RK
1330
1331 case SYMBOL_REF:
4c6669c2
RS
1332 {
1333 /* Don't hash on the symbol's address to avoid bootstrap differences.
1334 Different hash values may cause expressions to be recorded in
1335 different orders and thus different registers to be used in the
1336 final assembler. This also avoids differences in the dump files
1337 between various stages. */
1338 unsigned int h = 0;
1339 const unsigned char *p = (const unsigned char *) XSTR (x, 0);
1340
1341 while (*p)
1342 h += (h << 7) + *p++; /* ??? revisit */
1343
1344 hash += ((unsigned int) SYMBOL_REF << 7) + h;
1345 return hash ? hash : (unsigned int) SYMBOL_REF;
1346 }
fa49fd0f
RK
1347
1348 case PRE_DEC:
1349 case PRE_INC:
4deef538
AO
1350 /* We can't compute these without knowing the MEM mode. */
1351 gcc_assert (memmode != VOIDmode);
cf098191 1352 offset = GET_MODE_SIZE (memmode);
4deef538 1353 if (code == PRE_DEC)
cf098191 1354 offset = -offset;
4deef538
AO
1355 /* Adjust the hash so that (mem:MEMMODE (pre_* (reg))) hashes
1356 like (mem:MEMMODE (plus (reg) (const_int I))). */
2c0fa3ec
JJ
1357 if (GET_MODE (x) == Pmode
1358 && (REG_P (XEXP (x, 0))
1359 || MEM_P (XEXP (x, 0))
1360 || GET_CODE (XEXP (x, 0)) == VALUE))
1361 {
1362 HOST_WIDE_INT c;
1363 if (offset.is_constant (&c))
1364 return cselib_hash_plus_const_int (XEXP (x, 0),
1365 trunc_int_for_mode (c, Pmode),
1366 create, memmode);
1367 }
1368 hash = ((unsigned) PLUS + (unsigned) GET_MODE (x)
1369 + cselib_hash_rtx (XEXP (x, 0), create, memmode)
1370 + cselib_hash_rtx (gen_int_mode (offset, GET_MODE (x)),
1371 create, memmode));
4deef538
AO
1372 return hash ? hash : 1 + (unsigned) PLUS;
1373
1374 case PRE_MODIFY:
1375 gcc_assert (memmode != VOIDmode);
1376 return cselib_hash_rtx (XEXP (x, 1), create, memmode);
1377
fa49fd0f
RK
1378 case POST_DEC:
1379 case POST_INC:
1380 case POST_MODIFY:
4deef538
AO
1381 gcc_assert (memmode != VOIDmode);
1382 return cselib_hash_rtx (XEXP (x, 0), create, memmode);
1383
fa49fd0f
RK
1384 case PC:
1385 case CC0:
1386 case CALL:
1387 case UNSPEC_VOLATILE:
1388 return 0;
1389
1390 case ASM_OPERANDS:
1391 if (MEM_VOLATILE_P (x))
1392 return 0;
1393
1394 break;
7080f735 1395
2c0fa3ec
JJ
1396 case PLUS:
1397 if (GET_MODE (x) == Pmode
1398 && (REG_P (XEXP (x, 0))
1399 || MEM_P (XEXP (x, 0))
1400 || GET_CODE (XEXP (x, 0)) == VALUE)
1401 && CONST_INT_P (XEXP (x, 1)))
1402 return cselib_hash_plus_const_int (XEXP (x, 0), INTVAL (XEXP (x, 1)),
1403 create, memmode);
1404 break;
1405
fa49fd0f
RK
1406 default:
1407 break;
1408 }
1409
1410 i = GET_RTX_LENGTH (code) - 1;
1411 fmt = GET_RTX_FORMAT (code);
1412 for (; i >= 0; i--)
1413 {
341c100f 1414 switch (fmt[i])
fa49fd0f 1415 {
341c100f 1416 case 'e':
fa49fd0f 1417 {
341c100f 1418 rtx tem = XEXP (x, i);
4deef538 1419 unsigned int tem_hash = cselib_hash_rtx (tem, create, memmode);
b8698a0f 1420
fa49fd0f
RK
1421 if (tem_hash == 0)
1422 return 0;
b8698a0f 1423
fa49fd0f
RK
1424 hash += tem_hash;
1425 }
341c100f
NS
1426 break;
1427 case 'E':
1428 for (j = 0; j < XVECLEN (x, i); j++)
1429 {
1430 unsigned int tem_hash
4deef538 1431 = cselib_hash_rtx (XVECEXP (x, i, j), create, memmode);
b8698a0f 1432
341c100f
NS
1433 if (tem_hash == 0)
1434 return 0;
b8698a0f 1435
341c100f
NS
1436 hash += tem_hash;
1437 }
1438 break;
fa49fd0f 1439
341c100f
NS
1440 case 's':
1441 {
1442 const unsigned char *p = (const unsigned char *) XSTR (x, i);
b8698a0f 1443
341c100f
NS
1444 if (p)
1445 while (*p)
1446 hash += *p++;
1447 break;
1448 }
b8698a0f 1449
341c100f
NS
1450 case 'i':
1451 hash += XINT (x, i);
1452 break;
1453
91914e56
RS
1454 case 'p':
1455 hash += constant_lower_bound (SUBREG_BYTE (x));
1456 break;
1457
341c100f
NS
1458 case '0':
1459 case 't':
1460 /* unused */
1461 break;
b8698a0f 1462
341c100f
NS
1463 default:
1464 gcc_unreachable ();
fa49fd0f 1465 }
fa49fd0f
RK
1466 }
1467
dc76f41c 1468 return hash ? hash : 1 + (unsigned int) GET_CODE (x);
fa49fd0f
RK
1469}
1470
1471/* Create a new value structure for VALUE and initialize it. The mode of the
1472 value is MODE. */
1473
6a59927d 1474static inline cselib_val *
ef4bddc2 1475new_cselib_val (unsigned int hash, machine_mode mode, rtx x)
fa49fd0f 1476{
fb0b2914 1477 cselib_val *e = cselib_val_pool.allocate ();
fa49fd0f 1478
5440c0e7
AO
1479 gcc_assert (hash);
1480 gcc_assert (next_uid);
fa49fd0f 1481
5440c0e7
AO
1482 e->hash = hash;
1483 e->uid = next_uid++;
d67fb775
SB
1484 /* We use an alloc pool to allocate this RTL construct because it
1485 accounts for about 8% of the overall memory usage. We know
1486 precisely when we can have VALUE RTXen (when cselib is active)
daa956d0 1487 so we don't need to put them in garbage collected memory.
d67fb775 1488 ??? Why should a VALUE be an RTX in the first place? */
fb0b2914 1489 e->val_rtx = (rtx_def*) value_pool.allocate ();
757bbef8
SB
1490 memset (e->val_rtx, 0, RTX_HDR_SIZE);
1491 PUT_CODE (e->val_rtx, VALUE);
1492 PUT_MODE (e->val_rtx, mode);
1493 CSELIB_VAL_PTR (e->val_rtx) = e;
fa49fd0f
RK
1494 e->addr_list = 0;
1495 e->locs = 0;
7101fb18 1496 e->next_containing_mem = 0;
b5b8b0ac 1497
4a3c9687 1498 if (dump_file && (dump_flags & TDF_CSELIB))
b5b8b0ac 1499 {
5440c0e7 1500 fprintf (dump_file, "cselib value %u:%u ", e->uid, hash);
b5b8b0ac
AO
1501 if (flag_dump_noaddr || flag_dump_unnumbered)
1502 fputs ("# ", dump_file);
1503 else
1504 fprintf (dump_file, "%p ", (void*)e);
1505 print_rtl_single (dump_file, x);
1506 fputc ('\n', dump_file);
1507 }
1508
fa49fd0f
RK
1509 return e;
1510}
1511
1512/* ADDR_ELT is a value that is used as address. MEM_ELT is the value that
1513 contains the data at this address. X is a MEM that represents the
1514 value. Update the two value structures to represent this situation. */
1515
1516static void
7080f735 1517add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x)
fa49fd0f 1518{
faead9f7 1519 addr_elt = canonical_cselib_val (addr_elt);
a4f436ff
JJ
1520 mem_elt = canonical_cselib_val (mem_elt);
1521
fa49fd0f 1522 /* Avoid duplicates. */
bd68a3a7
RH
1523 addr_space_t as = MEM_ADDR_SPACE (x);
1524 for (elt_loc_list *l = mem_elt->locs; l; l = l->next)
3c0cb5de 1525 if (MEM_P (l->loc)
bd68a3a7
RH
1526 && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt
1527 && MEM_ADDR_SPACE (l->loc) == as)
5847e8da
AO
1528 {
1529 promote_debug_loc (l);
1530 return;
1531 }
fa49fd0f 1532
fa49fd0f 1533 addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt);
6f2ffb4b
AO
1534 new_elt_loc_list (mem_elt,
1535 replace_equiv_address_nv (x, addr_elt->val_rtx));
7101fb18
JH
1536 if (mem_elt->next_containing_mem == NULL)
1537 {
1538 mem_elt->next_containing_mem = first_containing_mem;
1539 first_containing_mem = mem_elt;
1540 }
fa49fd0f
RK
1541}
1542
1543/* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx.
1544 If CREATE, make a new one if we haven't seen it before. */
1545
1546static cselib_val *
7080f735 1547cselib_lookup_mem (rtx x, int create)
fa49fd0f 1548{
ef4bddc2
RS
1549 machine_mode mode = GET_MODE (x);
1550 machine_mode addr_mode;
4a8fb1a1 1551 cselib_val **slot;
fa49fd0f
RK
1552 cselib_val *addr;
1553 cselib_val *mem_elt;
fa49fd0f
RK
1554
1555 if (MEM_VOLATILE_P (x) || mode == BLKmode
463301c3 1556 || !cselib_record_memory
fa49fd0f
RK
1557 || (FLOAT_MODE_P (mode) && flag_float_store))
1558 return 0;
1559
4deef538
AO
1560 addr_mode = GET_MODE (XEXP (x, 0));
1561 if (addr_mode == VOIDmode)
1562 addr_mode = Pmode;
1563
fa49fd0f 1564 /* Look up the value for the address. */
4deef538 1565 addr = cselib_lookup (XEXP (x, 0), addr_mode, create, mode);
fa49fd0f
RK
1566 if (! addr)
1567 return 0;
faead9f7 1568 addr = canonical_cselib_val (addr);
bd68a3a7 1569
fa49fd0f 1570 /* Find a value that describes a value of our mode at that address. */
bd68a3a7
RH
1571 addr_space_t as = MEM_ADDR_SPACE (x);
1572 for (elt_list *l = addr->addr_list; l; l = l->next)
757bbef8 1573 if (GET_MODE (l->elt->val_rtx) == mode)
5847e8da 1574 {
bd68a3a7
RH
1575 for (elt_loc_list *l2 = l->elt->locs; l2; l2 = l2->next)
1576 if (MEM_P (l2->loc) && MEM_ADDR_SPACE (l2->loc) == as)
1577 {
1578 promote_debug_loc (l->elt->locs);
1579 return l->elt;
1580 }
5847e8da 1581 }
fa49fd0f
RK
1582
1583 if (! create)
1584 return 0;
1585
5440c0e7 1586 mem_elt = new_cselib_val (next_uid, mode, x);
fa49fd0f 1587 add_mem_for_addr (addr, mem_elt, x);
f956adb9 1588 slot = cselib_find_slot (mode, x, mem_elt->hash, INSERT, VOIDmode);
fa49fd0f
RK
1589 *slot = mem_elt;
1590 return mem_elt;
1591}
1592
073a8998 1593/* Search through the possible substitutions in P. We prefer a non reg
6fb5fa3c
DB
1594 substitution because this allows us to expand the tree further. If
1595 we find, just a reg, take the lowest regno. There may be several
1596 non-reg results, we just take the first one because they will all
1597 expand to the same place. */
1598
b8698a0f 1599static rtx
b5b8b0ac
AO
1600expand_loc (struct elt_loc_list *p, struct expand_value_data *evd,
1601 int max_depth)
6fb5fa3c
DB
1602{
1603 rtx reg_result = NULL;
1604 unsigned int regno = UINT_MAX;
1605 struct elt_loc_list *p_in = p;
1606
67b977ad 1607 for (; p; p = p->next)
6fb5fa3c 1608 {
67b977ad
JJ
1609 /* Return these right away to avoid returning stack pointer based
1610 expressions for frame pointer and vice versa, which is something
1611 that would confuse DSE. See the comment in cselib_expand_value_rtx_1
1612 for more details. */
1613 if (REG_P (p->loc)
1614 && (REGNO (p->loc) == STACK_POINTER_REGNUM
1615 || REGNO (p->loc) == FRAME_POINTER_REGNUM
1616 || REGNO (p->loc) == HARD_FRAME_POINTER_REGNUM
1617 || REGNO (p->loc) == cfa_base_preserved_regno))
1618 return p->loc;
6fb5fa3c
DB
1619 /* Avoid infinite recursion trying to expand a reg into a
1620 the same reg. */
b8698a0f
L
1621 if ((REG_P (p->loc))
1622 && (REGNO (p->loc) < regno)
b5b8b0ac 1623 && !bitmap_bit_p (evd->regs_active, REGNO (p->loc)))
6fb5fa3c
DB
1624 {
1625 reg_result = p->loc;
1626 regno = REGNO (p->loc);
1627 }
1628 /* Avoid infinite recursion and do not try to expand the
1629 value. */
b8698a0f 1630 else if (GET_CODE (p->loc) == VALUE
6fb5fa3c
DB
1631 && CSELIB_VAL_PTR (p->loc)->locs == p_in)
1632 continue;
1633 else if (!REG_P (p->loc))
1634 {
8dd5516b 1635 rtx result, note;
4a3c9687 1636 if (dump_file && (dump_flags & TDF_CSELIB))
6fb5fa3c
DB
1637 {
1638 print_inline_rtx (dump_file, p->loc, 0);
1639 fprintf (dump_file, "\n");
1640 }
8dd5516b
JJ
1641 if (GET_CODE (p->loc) == LO_SUM
1642 && GET_CODE (XEXP (p->loc, 1)) == SYMBOL_REF
1643 && p->setting_insn
1644 && (note = find_reg_note (p->setting_insn, REG_EQUAL, NULL_RTX))
1645 && XEXP (note, 0) == XEXP (p->loc, 1))
1646 return XEXP (p->loc, 1);
b5b8b0ac 1647 result = cselib_expand_value_rtx_1 (p->loc, evd, max_depth - 1);
6fb5fa3c
DB
1648 if (result)
1649 return result;
1650 }
b8698a0f 1651
6fb5fa3c 1652 }
b8698a0f 1653
6fb5fa3c
DB
1654 if (regno != UINT_MAX)
1655 {
1656 rtx result;
4a3c9687 1657 if (dump_file && (dump_flags & TDF_CSELIB))
6fb5fa3c
DB
1658 fprintf (dump_file, "r%d\n", regno);
1659
b5b8b0ac 1660 result = cselib_expand_value_rtx_1 (reg_result, evd, max_depth - 1);
6fb5fa3c
DB
1661 if (result)
1662 return result;
1663 }
1664
4a3c9687 1665 if (dump_file && (dump_flags & TDF_CSELIB))
6fb5fa3c
DB
1666 {
1667 if (reg_result)
1668 {
1669 print_inline_rtx (dump_file, reg_result, 0);
1670 fprintf (dump_file, "\n");
1671 }
b8698a0f 1672 else
6fb5fa3c
DB
1673 fprintf (dump_file, "NULL\n");
1674 }
1675 return reg_result;
1676}
1677
1678
1679/* Forward substitute and expand an expression out to its roots.
1680 This is the opposite of common subexpression. Because local value
1681 numbering is such a weak optimization, the expanded expression is
1682 pretty much unique (not from a pointer equals point of view but
b8698a0f 1683 from a tree shape point of view.
6fb5fa3c
DB
1684
1685 This function returns NULL if the expansion fails. The expansion
1686 will fail if there is no value number for one of the operands or if
1687 one of the operands has been overwritten between the current insn
1688 and the beginning of the basic block. For instance x has no
1689 expansion in:
1690
1691 r1 <- r1 + 3
1692 x <- r1 + 8
1693
1694 REGS_ACTIVE is a scratch bitmap that should be clear when passing in.
1695 It is clear on return. */
1696
1697rtx
1698cselib_expand_value_rtx (rtx orig, bitmap regs_active, int max_depth)
b5b8b0ac
AO
1699{
1700 struct expand_value_data evd;
1701
1702 evd.regs_active = regs_active;
1703 evd.callback = NULL;
1704 evd.callback_arg = NULL;
864ddef7 1705 evd.dummy = false;
b5b8b0ac
AO
1706
1707 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1708}
1709
1710/* Same as cselib_expand_value_rtx, but using a callback to try to
0b7e34d7
AO
1711 resolve some expressions. The CB function should return ORIG if it
1712 can't or does not want to deal with a certain RTX. Any other
1713 return value, including NULL, will be used as the expansion for
1714 VALUE, without any further changes. */
b5b8b0ac
AO
1715
1716rtx
1717cselib_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1718 cselib_expand_callback cb, void *data)
1719{
1720 struct expand_value_data evd;
1721
1722 evd.regs_active = regs_active;
1723 evd.callback = cb;
1724 evd.callback_arg = data;
864ddef7 1725 evd.dummy = false;
b5b8b0ac
AO
1726
1727 return cselib_expand_value_rtx_1 (orig, &evd, max_depth);
1728}
1729
864ddef7
JJ
1730/* Similar to cselib_expand_value_rtx_cb, but no rtxs are actually copied
1731 or simplified. Useful to find out whether cselib_expand_value_rtx_cb
1732 would return NULL or non-NULL, without allocating new rtx. */
1733
1734bool
1735cselib_dummy_expand_value_rtx_cb (rtx orig, bitmap regs_active, int max_depth,
1736 cselib_expand_callback cb, void *data)
1737{
1738 struct expand_value_data evd;
1739
1740 evd.regs_active = regs_active;
1741 evd.callback = cb;
1742 evd.callback_arg = data;
1743 evd.dummy = true;
1744
1745 return cselib_expand_value_rtx_1 (orig, &evd, max_depth) != NULL;
1746}
1747
0b7e34d7
AO
1748/* Internal implementation of cselib_expand_value_rtx and
1749 cselib_expand_value_rtx_cb. */
1750
b5b8b0ac
AO
1751static rtx
1752cselib_expand_value_rtx_1 (rtx orig, struct expand_value_data *evd,
1753 int max_depth)
6fb5fa3c
DB
1754{
1755 rtx copy, scopy;
1756 int i, j;
1757 RTX_CODE code;
1758 const char *format_ptr;
ef4bddc2 1759 machine_mode mode;
6fb5fa3c
DB
1760
1761 code = GET_CODE (orig);
1762
1763 /* For the context of dse, if we end up expand into a huge tree, we
1764 will not have a useful address, so we might as well just give up
1765 quickly. */
1766 if (max_depth <= 0)
1767 return NULL;
1768
1769 switch (code)
1770 {
1771 case REG:
1772 {
1773 struct elt_list *l = REG_VALUES (REGNO (orig));
1774
1775 if (l && l->elt == NULL)
1776 l = l->next;
1777 for (; l; l = l->next)
1778 if (GET_MODE (l->elt->val_rtx) == GET_MODE (orig))
1779 {
1780 rtx result;
5a9fbcf1 1781 unsigned regno = REGNO (orig);
b8698a0f 1782
6fb5fa3c 1783 /* The only thing that we are not willing to do (this
6ed3da00 1784 is requirement of dse and if others potential uses
6fb5fa3c
DB
1785 need this function we should add a parm to control
1786 it) is that we will not substitute the
1787 STACK_POINTER_REGNUM, FRAME_POINTER or the
1788 HARD_FRAME_POINTER.
1789
cea618ac 1790 These expansions confuses the code that notices that
6fb5fa3c
DB
1791 stores into the frame go dead at the end of the
1792 function and that the frame is not effected by calls
1793 to subroutines. If you allow the
1794 STACK_POINTER_REGNUM substitution, then dse will
1795 think that parameter pushing also goes dead which is
1796 wrong. If you allow the FRAME_POINTER or the
1797 HARD_FRAME_POINTER then you lose the opportunity to
1798 make the frame assumptions. */
1799 if (regno == STACK_POINTER_REGNUM
1800 || regno == FRAME_POINTER_REGNUM
5a9fbcf1
AO
1801 || regno == HARD_FRAME_POINTER_REGNUM
1802 || regno == cfa_base_preserved_regno)
6fb5fa3c
DB
1803 return orig;
1804
b5b8b0ac 1805 bitmap_set_bit (evd->regs_active, regno);
6fb5fa3c 1806
4a3c9687 1807 if (dump_file && (dump_flags & TDF_CSELIB))
6fb5fa3c
DB
1808 fprintf (dump_file, "expanding: r%d into: ", regno);
1809
b5b8b0ac
AO
1810 result = expand_loc (l->elt->locs, evd, max_depth);
1811 bitmap_clear_bit (evd->regs_active, regno);
6fb5fa3c
DB
1812
1813 if (result)
1814 return result;
b8698a0f 1815 else
6fb5fa3c
DB
1816 return orig;
1817 }
f0bc3323 1818 return orig;
6fb5fa3c 1819 }
b8698a0f 1820
d8116890 1821 CASE_CONST_ANY:
6fb5fa3c
DB
1822 case SYMBOL_REF:
1823 case CODE_LABEL:
1824 case PC:
1825 case CC0:
1826 case SCRATCH:
1827 /* SCRATCH must be shared because they represent distinct values. */
1828 return orig;
1829 case CLOBBER:
1830 if (REG_P (XEXP (orig, 0)) && HARD_REGISTER_NUM_P (REGNO (XEXP (orig, 0))))
1831 return orig;
1832 break;
1833
1834 case CONST:
1835 if (shared_const_p (orig))
1836 return orig;
1837 break;
1838
8dd5516b 1839 case SUBREG:
6fb5fa3c 1840 {
0b7e34d7
AO
1841 rtx subreg;
1842
1843 if (evd->callback)
1844 {
1845 subreg = evd->callback (orig, evd->regs_active, max_depth,
1846 evd->callback_arg);
1847 if (subreg != orig)
1848 return subreg;
1849 }
1850
1851 subreg = cselib_expand_value_rtx_1 (SUBREG_REG (orig), evd,
1852 max_depth - 1);
8dd5516b
JJ
1853 if (!subreg)
1854 return NULL;
1855 scopy = simplify_gen_subreg (GET_MODE (orig), subreg,
1856 GET_MODE (SUBREG_REG (orig)),
1857 SUBREG_BYTE (orig));
0b7e34d7
AO
1858 if (scopy == NULL
1859 || (GET_CODE (scopy) == SUBREG
1860 && !REG_P (SUBREG_REG (scopy))
1861 && !MEM_P (SUBREG_REG (scopy))))
1862 return NULL;
1863
8dd5516b 1864 return scopy;
6fb5fa3c 1865 }
8dd5516b
JJ
1866
1867 case VALUE:
b5b8b0ac
AO
1868 {
1869 rtx result;
0b7e34d7 1870
4a3c9687 1871 if (dump_file && (dump_flags & TDF_CSELIB))
b5b8b0ac
AO
1872 {
1873 fputs ("\nexpanding ", dump_file);
1874 print_rtl_single (dump_file, orig);
1875 fputs (" into...", dump_file);
1876 }
8dd5516b 1877
0b7e34d7 1878 if (evd->callback)
b5b8b0ac
AO
1879 {
1880 result = evd->callback (orig, evd->regs_active, max_depth,
1881 evd->callback_arg);
0b7e34d7
AO
1882
1883 if (result != orig)
1884 return result;
b5b8b0ac 1885 }
8dd5516b 1886
0b7e34d7 1887 result = expand_loc (CSELIB_VAL_PTR (orig)->locs, evd, max_depth);
b5b8b0ac
AO
1888 return result;
1889 }
0ca5af51
AO
1890
1891 case DEBUG_EXPR:
1892 if (evd->callback)
1893 return evd->callback (orig, evd->regs_active, max_depth,
1894 evd->callback_arg);
1895 return orig;
1896
6fb5fa3c
DB
1897 default:
1898 break;
1899 }
1900
1901 /* Copy the various flags, fields, and other information. We assume
1902 that all fields need copying, and then clear the fields that should
1903 not be copied. That is the sensible default behavior, and forces
1904 us to explicitly document why we are *not* copying a flag. */
864ddef7
JJ
1905 if (evd->dummy)
1906 copy = NULL;
1907 else
1908 copy = shallow_copy_rtx (orig);
6fb5fa3c 1909
8dd5516b 1910 format_ptr = GET_RTX_FORMAT (code);
6fb5fa3c 1911
8dd5516b 1912 for (i = 0; i < GET_RTX_LENGTH (code); i++)
6fb5fa3c
DB
1913 switch (*format_ptr++)
1914 {
1915 case 'e':
1916 if (XEXP (orig, i) != NULL)
1917 {
b5b8b0ac
AO
1918 rtx result = cselib_expand_value_rtx_1 (XEXP (orig, i), evd,
1919 max_depth - 1);
6fb5fa3c
DB
1920 if (!result)
1921 return NULL;
864ddef7
JJ
1922 if (copy)
1923 XEXP (copy, i) = result;
6fb5fa3c
DB
1924 }
1925 break;
1926
1927 case 'E':
1928 case 'V':
1929 if (XVEC (orig, i) != NULL)
1930 {
864ddef7
JJ
1931 if (copy)
1932 XVEC (copy, i) = rtvec_alloc (XVECLEN (orig, i));
1933 for (j = 0; j < XVECLEN (orig, i); j++)
6fb5fa3c 1934 {
b5b8b0ac
AO
1935 rtx result = cselib_expand_value_rtx_1 (XVECEXP (orig, i, j),
1936 evd, max_depth - 1);
6fb5fa3c
DB
1937 if (!result)
1938 return NULL;
864ddef7
JJ
1939 if (copy)
1940 XVECEXP (copy, i, j) = result;
6fb5fa3c
DB
1941 }
1942 }
1943 break;
1944
1945 case 't':
1946 case 'w':
1947 case 'i':
1948 case 's':
1949 case 'S':
1950 case 'T':
1951 case 'u':
1952 case 'B':
1953 case '0':
1954 /* These are left unchanged. */
1955 break;
1956
1957 default:
1958 gcc_unreachable ();
1959 }
1960
864ddef7
JJ
1961 if (evd->dummy)
1962 return orig;
1963
8dd5516b
JJ
1964 mode = GET_MODE (copy);
1965 /* If an operand has been simplified into CONST_INT, which doesn't
1966 have a mode and the mode isn't derivable from whole rtx's mode,
1967 try simplify_*_operation first with mode from original's operand
1968 and as a fallback wrap CONST_INT into gen_rtx_CONST. */
1969 scopy = copy;
1970 switch (GET_RTX_CLASS (code))
1971 {
1972 case RTX_UNARY:
1973 if (CONST_INT_P (XEXP (copy, 0))
1974 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1975 {
1976 scopy = simplify_unary_operation (code, mode, XEXP (copy, 0),
1977 GET_MODE (XEXP (orig, 0)));
1978 if (scopy)
1979 return scopy;
1980 }
1981 break;
1982 case RTX_COMM_ARITH:
1983 case RTX_BIN_ARITH:
1984 /* These expressions can derive operand modes from the whole rtx's mode. */
1985 break;
1986 case RTX_TERNARY:
1987 case RTX_BITFIELD_OPS:
1988 if (CONST_INT_P (XEXP (copy, 0))
1989 && GET_MODE (XEXP (orig, 0)) != VOIDmode)
1990 {
1991 scopy = simplify_ternary_operation (code, mode,
1992 GET_MODE (XEXP (orig, 0)),
1993 XEXP (copy, 0), XEXP (copy, 1),
1994 XEXP (copy, 2));
1995 if (scopy)
1996 return scopy;
1997 }
1998 break;
1999 case RTX_COMPARE:
2000 case RTX_COMM_COMPARE:
2001 if (CONST_INT_P (XEXP (copy, 0))
2002 && GET_MODE (XEXP (copy, 1)) == VOIDmode
2003 && (GET_MODE (XEXP (orig, 0)) != VOIDmode
2004 || GET_MODE (XEXP (orig, 1)) != VOIDmode))
2005 {
2006 scopy = simplify_relational_operation (code, mode,
2007 (GET_MODE (XEXP (orig, 0))
2008 != VOIDmode)
2009 ? GET_MODE (XEXP (orig, 0))
2010 : GET_MODE (XEXP (orig, 1)),
2011 XEXP (copy, 0),
2012 XEXP (copy, 1));
2013 if (scopy)
2014 return scopy;
2015 }
2016 break;
2017 default:
2018 break;
2019 }
6fb5fa3c
DB
2020 scopy = simplify_rtx (copy);
2021 if (scopy)
3af4ba41 2022 return scopy;
6fb5fa3c
DB
2023 return copy;
2024}
2025
fa49fd0f
RK
2026/* Walk rtx X and replace all occurrences of REG and MEM subexpressions
2027 with VALUE expressions. This way, it becomes independent of changes
2028 to registers and memory.
2029 X isn't actually modified; if modifications are needed, new rtl is
4deef538
AO
2030 allocated. However, the return value can share rtl with X.
2031 If X is within a MEM, MEMMODE must be the mode of the MEM. */
fa49fd0f 2032
91700444 2033rtx
ef4bddc2 2034cselib_subst_to_values (rtx x, machine_mode memmode)
fa49fd0f
RK
2035{
2036 enum rtx_code code = GET_CODE (x);
2037 const char *fmt = GET_RTX_FORMAT (code);
2038 cselib_val *e;
2039 struct elt_list *l;
2040 rtx copy = x;
2041 int i;
cf098191 2042 poly_int64 offset;
fa49fd0f
RK
2043
2044 switch (code)
2045 {
2046 case REG:
60fa6660
AO
2047 l = REG_VALUES (REGNO (x));
2048 if (l && l->elt == NULL)
2049 l = l->next;
2050 for (; l; l = l->next)
757bbef8
SB
2051 if (GET_MODE (l->elt->val_rtx) == GET_MODE (x))
2052 return l->elt->val_rtx;
fa49fd0f 2053
341c100f 2054 gcc_unreachable ();
fa49fd0f
RK
2055
2056 case MEM:
2057 e = cselib_lookup_mem (x, 0);
4deef538
AO
2058 /* This used to happen for autoincrements, but we deal with them
2059 properly now. Remove the if stmt for the next release. */
fa49fd0f 2060 if (! e)
91700444 2061 {
4deef538 2062 /* Assign a value that doesn't match any other. */
5440c0e7 2063 e = new_cselib_val (next_uid, GET_MODE (x), x);
91700444 2064 }
757bbef8 2065 return e->val_rtx;
fa49fd0f 2066
509f4495
JJ
2067 case ENTRY_VALUE:
2068 e = cselib_lookup (x, GET_MODE (x), 0, memmode);
2069 if (! e)
2070 break;
2071 return e->val_rtx;
2072
d8116890 2073 CASE_CONST_ANY:
fa49fd0f
RK
2074 return x;
2075
4deef538 2076 case PRE_DEC:
91700444 2077 case PRE_INC:
4deef538 2078 gcc_assert (memmode != VOIDmode);
cf098191 2079 offset = GET_MODE_SIZE (memmode);
4deef538 2080 if (code == PRE_DEC)
cf098191 2081 offset = -offset;
0a81f074 2082 return cselib_subst_to_values (plus_constant (GET_MODE (x),
cf098191 2083 XEXP (x, 0), offset),
4deef538
AO
2084 memmode);
2085
2086 case PRE_MODIFY:
2087 gcc_assert (memmode != VOIDmode);
2088 return cselib_subst_to_values (XEXP (x, 1), memmode);
2089
91700444 2090 case POST_DEC:
4deef538 2091 case POST_INC:
91700444 2092 case POST_MODIFY:
4deef538
AO
2093 gcc_assert (memmode != VOIDmode);
2094 return cselib_subst_to_values (XEXP (x, 0), memmode);
7080f735 2095
2c0fa3ec
JJ
2096 case PLUS:
2097 if (GET_MODE (x) == Pmode && CONST_INT_P (XEXP (x, 1)))
2098 {
2099 rtx t = cselib_subst_to_values (XEXP (x, 0), memmode);
2100 if (GET_CODE (t) == VALUE)
8662d059
JJ
2101 {
2102 if (SP_DERIVED_VALUE_P (t) && XEXP (x, 1) == const0_rtx)
2103 return t;
2104 for (struct elt_loc_list *l = CSELIB_VAL_PTR (t)->locs;
2105 l; l = l->next)
2106 if (GET_CODE (l->loc) == PLUS
2107 && GET_CODE (XEXP (l->loc, 0)) == VALUE
2108 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2109 && CONST_INT_P (XEXP (l->loc, 1)))
2110 return plus_constant (Pmode, l->loc, INTVAL (XEXP (x, 1)));
2111 }
2c0fa3ec
JJ
2112 if (t != XEXP (x, 0))
2113 {
2114 copy = shallow_copy_rtx (x);
2115 XEXP (copy, 0) = t;
2116 }
2117 return copy;
2118 }
2119
fa49fd0f
RK
2120 default:
2121 break;
2122 }
2123
2124 for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--)
2125 {
2126 if (fmt[i] == 'e')
2127 {
4deef538 2128 rtx t = cselib_subst_to_values (XEXP (x, i), memmode);
fa49fd0f 2129
bd7960b1
RS
2130 if (t != XEXP (x, i))
2131 {
2132 if (x == copy)
2133 copy = shallow_copy_rtx (x);
2134 XEXP (copy, i) = t;
2135 }
fa49fd0f
RK
2136 }
2137 else if (fmt[i] == 'E')
2138 {
bd7960b1 2139 int j;
fa49fd0f
RK
2140
2141 for (j = 0; j < XVECLEN (x, i); j++)
2142 {
4deef538 2143 rtx t = cselib_subst_to_values (XVECEXP (x, i, j), memmode);
fa49fd0f 2144
bd7960b1 2145 if (t != XVECEXP (x, i, j))
fa49fd0f 2146 {
bd7960b1
RS
2147 if (XVEC (x, i) == XVEC (copy, i))
2148 {
2149 if (x == copy)
2150 copy = shallow_copy_rtx (x);
2151 XVEC (copy, i) = shallow_copy_rtvec (XVEC (x, i));
2152 }
2153 XVECEXP (copy, i, j) = t;
fa49fd0f 2154 }
fa49fd0f
RK
2155 }
2156 }
2157 }
2158
2159 return copy;
2160}
2161
9a76e83d
JJ
2162/* Wrapper for cselib_subst_to_values, that indicates X is in INSN. */
2163
2164rtx
ef4bddc2 2165cselib_subst_to_values_from_insn (rtx x, machine_mode memmode, rtx_insn *insn)
9a76e83d
JJ
2166{
2167 rtx ret;
2168 gcc_assert (!cselib_current_insn);
2169 cselib_current_insn = insn;
2170 ret = cselib_subst_to_values (x, memmode);
2171 cselib_current_insn = NULL;
2172 return ret;
2173}
2174
4deef538
AO
2175/* Look up the rtl expression X in our tables and return the value it
2176 has. If CREATE is zero, we return NULL if we don't know the value.
2177 Otherwise, we create a new one if possible, using mode MODE if X
2178 doesn't have a mode (i.e. because it's a constant). When X is part
2179 of an address, MEMMODE should be the mode of the enclosing MEM if
2180 we're tracking autoinc expressions. */
fa49fd0f 2181
5847e8da 2182static cselib_val *
ef4bddc2
RS
2183cselib_lookup_1 (rtx x, machine_mode mode,
2184 int create, machine_mode memmode)
fa49fd0f 2185{
4a8fb1a1 2186 cselib_val **slot;
fa49fd0f
RK
2187 cselib_val *e;
2188 unsigned int hashval;
2189
2190 if (GET_MODE (x) != VOIDmode)
2191 mode = GET_MODE (x);
2192
2193 if (GET_CODE (x) == VALUE)
2194 return CSELIB_VAL_PTR (x);
2195
f8cfc6aa 2196 if (REG_P (x))
fa49fd0f
RK
2197 {
2198 struct elt_list *l;
2199 unsigned int i = REGNO (x);
2200
60fa6660
AO
2201 l = REG_VALUES (i);
2202 if (l && l->elt == NULL)
2203 l = l->next;
2204 for (; l; l = l->next)
757bbef8 2205 if (mode == GET_MODE (l->elt->val_rtx))
5847e8da
AO
2206 {
2207 promote_debug_loc (l->elt->locs);
2208 return l->elt;
2209 }
fa49fd0f
RK
2210
2211 if (! create)
5847e8da 2212 return 0;
fa49fd0f 2213
31825e57
DM
2214 if (i < FIRST_PSEUDO_REGISTER)
2215 {
ad474626 2216 unsigned int n = hard_regno_nregs (i, mode);
31825e57
DM
2217
2218 if (n > max_value_regs)
2219 max_value_regs = n;
2220 }
2221
5440c0e7 2222 e = new_cselib_val (next_uid, GET_MODE (x), x);
2c0fa3ec
JJ
2223 if (GET_MODE (x) == Pmode && x == stack_pointer_rtx)
2224 SP_DERIVED_VALUE_P (e->val_rtx) = 1;
6f2ffb4b 2225 new_elt_loc_list (e, x);
b4206259
RS
2226
2227 scalar_int_mode int_mode;
fa49fd0f 2228 if (REG_VALUES (i) == 0)
60fa6660
AO
2229 {
2230 /* Maintain the invariant that the first entry of
2231 REG_VALUES, if present, must be the value used to set the
2232 register, or NULL. */
6790d1ab 2233 used_regs[n_used_regs++] = i;
60fa6660
AO
2234 REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL);
2235 }
509f4495 2236 else if (cselib_preserve_constants
b4206259 2237 && is_int_mode (mode, &int_mode))
509f4495
JJ
2238 {
2239 /* During var-tracking, try harder to find equivalences
2240 for SUBREGs. If a setter sets say a DImode register
2241 and user uses that register only in SImode, add a lowpart
2242 subreg location. */
2243 struct elt_list *lwider = NULL;
b4206259 2244 scalar_int_mode lmode;
509f4495
JJ
2245 l = REG_VALUES (i);
2246 if (l && l->elt == NULL)
2247 l = l->next;
2248 for (; l; l = l->next)
b4206259
RS
2249 if (is_int_mode (GET_MODE (l->elt->val_rtx), &lmode)
2250 && GET_MODE_SIZE (lmode) > GET_MODE_SIZE (int_mode)
509f4495 2251 && (lwider == NULL
bd4288c0
RS
2252 || partial_subreg_p (lmode,
2253 GET_MODE (lwider->elt->val_rtx))))
509f4495
JJ
2254 {
2255 struct elt_loc_list *el;
2256 if (i < FIRST_PSEUDO_REGISTER
ad474626 2257 && hard_regno_nregs (i, lmode) != 1)
509f4495
JJ
2258 continue;
2259 for (el = l->elt->locs; el; el = el->next)
2260 if (!REG_P (el->loc))
2261 break;
2262 if (el)
2263 lwider = l;
2264 }
2265 if (lwider)
2266 {
b4206259 2267 rtx sub = lowpart_subreg (int_mode, lwider->elt->val_rtx,
509f4495
JJ
2268 GET_MODE (lwider->elt->val_rtx));
2269 if (sub)
6f2ffb4b 2270 new_elt_loc_list (e, sub);
509f4495
JJ
2271 }
2272 }
60fa6660 2273 REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e);
f956adb9 2274 slot = cselib_find_slot (mode, x, e->hash, INSERT, memmode);
fa49fd0f 2275 *slot = e;
5847e8da 2276 return e;
fa49fd0f
RK
2277 }
2278
3c0cb5de 2279 if (MEM_P (x))
5847e8da 2280 return cselib_lookup_mem (x, create);
fa49fd0f 2281
4deef538 2282 hashval = cselib_hash_rtx (x, create, memmode);
fa49fd0f
RK
2283 /* Can't even create if hashing is not possible. */
2284 if (! hashval)
5847e8da 2285 return 0;
fa49fd0f 2286
f956adb9 2287 slot = cselib_find_slot (mode, x, hashval,
4deef538 2288 create ? INSERT : NO_INSERT, memmode);
fa49fd0f 2289 if (slot == 0)
5847e8da 2290 return 0;
fa49fd0f
RK
2291
2292 e = (cselib_val *) *slot;
2293 if (e)
5847e8da 2294 return e;
fa49fd0f 2295
b5b8b0ac 2296 e = new_cselib_val (hashval, mode, x);
fa49fd0f
RK
2297
2298 /* We have to fill the slot before calling cselib_subst_to_values:
2299 the hash table is inconsistent until we do so, and
2300 cselib_subst_to_values will need to do lookups. */
4a8fb1a1 2301 *slot = e;
2c0fa3ec
JJ
2302 rtx v = cselib_subst_to_values (x, memmode);
2303
2304 /* If cselib_preserve_constants, we might get a SP_DERIVED_VALUE_P
2305 VALUE that isn't in the hash tables anymore. */
2306 if (GET_CODE (v) == VALUE && SP_DERIVED_VALUE_P (v) && PRESERVED_VALUE_P (v))
2307 PRESERVED_VALUE_P (e->val_rtx) = 1;
2308
2309 new_elt_loc_list (e, v);
5847e8da
AO
2310 return e;
2311}
2312
2313/* Wrapper for cselib_lookup, that indicates X is in INSN. */
2314
2315cselib_val *
ef4bddc2
RS
2316cselib_lookup_from_insn (rtx x, machine_mode mode,
2317 int create, machine_mode memmode, rtx_insn *insn)
5847e8da
AO
2318{
2319 cselib_val *ret;
2320
2321 gcc_assert (!cselib_current_insn);
2322 cselib_current_insn = insn;
2323
4deef538 2324 ret = cselib_lookup (x, mode, create, memmode);
5847e8da
AO
2325
2326 cselib_current_insn = NULL;
2327
2328 return ret;
2329}
2330
2331/* Wrapper for cselib_lookup_1, that logs the lookup result and
2332 maintains invariants related with debug insns. */
2333
2334cselib_val *
ef4bddc2
RS
2335cselib_lookup (rtx x, machine_mode mode,
2336 int create, machine_mode memmode)
5847e8da 2337{
4deef538 2338 cselib_val *ret = cselib_lookup_1 (x, mode, create, memmode);
5847e8da
AO
2339
2340 /* ??? Should we return NULL if we're not to create an entry, the
2341 found loc is a debug loc and cselib_current_insn is not DEBUG?
2342 If so, we should also avoid converting val to non-DEBUG; probably
2343 easiest setting cselib_current_insn to NULL before the call
2344 above. */
2345
4a3c9687 2346 if (dump_file && (dump_flags & TDF_CSELIB))
5847e8da
AO
2347 {
2348 fputs ("cselib lookup ", dump_file);
2349 print_inline_rtx (dump_file, x, 2);
2350 fprintf (dump_file, " => %u:%u\n",
2351 ret ? ret->uid : 0,
2352 ret ? ret->hash : 0);
2353 }
2354
2355 return ret;
fa49fd0f
RK
2356}
2357
150760dd
RS
2358/* Invalidate the value at *L, which is part of REG_VALUES (REGNO). */
2359
2360static void
2361cselib_invalidate_regno_val (unsigned int regno, struct elt_list **l)
2362{
2363 cselib_val *v = (*l)->elt;
2364 if (*l == REG_VALUES (regno))
2365 {
2366 /* Maintain the invariant that the first entry of
2367 REG_VALUES, if present, must be the value used to set
2368 the register, or NULL. This is also nice because
2369 then we won't push the same regno onto user_regs
2370 multiple times. */
2371 (*l)->elt = NULL;
2372 l = &(*l)->next;
2373 }
2374 else
2375 unchain_one_elt_list (l);
2376
2377 v = canonical_cselib_val (v);
2378
2379 bool had_locs = v->locs != NULL;
2380 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
2381
2382 /* Now, we clear the mapping from value to reg. It must exist, so
2383 this code will crash intentionally if it doesn't. */
2384 for (elt_loc_list **p = &v->locs; ; p = &(*p)->next)
2385 {
2386 rtx x = (*p)->loc;
2387
2388 if (REG_P (x) && REGNO (x) == regno)
2389 {
2390 unchain_one_elt_loc_list (p);
2391 break;
2392 }
2393 }
2394
bab8d962 2395 if (had_locs && cselib_useless_value_p (v))
150760dd
RS
2396 {
2397 if (setting_insn && DEBUG_INSN_P (setting_insn))
2398 n_useless_debug_values++;
2399 else
2400 n_useless_values++;
2401 }
2402}
2403
fa49fd0f
RK
2404/* Invalidate any entries in reg_values that overlap REGNO. This is called
2405 if REGNO is changing. MODE is the mode of the assignment to REGNO, which
2406 is used to determine how many hard registers are being changed. If MODE
2407 is VOIDmode, then only REGNO is being changed; this is used when
2408 invalidating call clobbered registers across a call. */
2409
2410static void
17d184e5 2411cselib_invalidate_regno (unsigned int regno, machine_mode mode)
fa49fd0f
RK
2412{
2413 unsigned int endregno;
2414 unsigned int i;
2415
2416 /* If we see pseudos after reload, something is _wrong_. */
341c100f
NS
2417 gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER
2418 || reg_renumber[regno] < 0);
fa49fd0f
RK
2419
2420 /* Determine the range of registers that must be invalidated. For
2421 pseudos, only REGNO is affected. For hard regs, we must take MODE
2422 into account, and we must also invalidate lower register numbers
2423 if they contain values that overlap REGNO. */
291aac59 2424 if (regno < FIRST_PSEUDO_REGISTER)
31825e57 2425 {
341c100f 2426 gcc_assert (mode != VOIDmode);
7080f735 2427
31825e57
DM
2428 if (regno < max_value_regs)
2429 i = 0;
2430 else
2431 i = regno - max_value_regs;
fa49fd0f 2432
09e18274 2433 endregno = end_hard_regno (mode, regno);
31825e57
DM
2434 }
2435 else
2436 {
2437 i = regno;
2438 endregno = regno + 1;
2439 }
2440
2441 for (; i < endregno; i++)
fa49fd0f
RK
2442 {
2443 struct elt_list **l = &REG_VALUES (i);
2444
2445 /* Go through all known values for this reg; if it overlaps the range
2446 we're invalidating, remove the value. */
2447 while (*l)
2448 {
2449 cselib_val *v = (*l)->elt;
fa49fd0f
RK
2450 unsigned int this_last = i;
2451
60fa6660 2452 if (i < FIRST_PSEUDO_REGISTER && v != NULL)
09e18274 2453 this_last = end_hard_regno (GET_MODE (v->val_rtx), i) - 1;
fa49fd0f 2454
9de9cbaf
JJ
2455 if (this_last < regno || v == NULL
2456 || (v == cfa_base_preserved_val
2457 && i == cfa_base_preserved_regno))
fa49fd0f
RK
2458 {
2459 l = &(*l)->next;
2460 continue;
2461 }
2462
2463 /* We have an overlap. */
150760dd 2464 cselib_invalidate_regno_val (i, l);
fa49fd0f
RK
2465 }
2466 }
2467}
9ddb66ca 2468\f
7101fb18
JH
2469/* Invalidate any locations in the table which are changed because of a
2470 store to MEM_RTX. If this is called because of a non-const call
2471 instruction, MEM_RTX is (mem:BLK const0_rtx). */
fa49fd0f 2472
7101fb18 2473static void
7080f735 2474cselib_invalidate_mem (rtx mem_rtx)
fa49fd0f 2475{
7101fb18 2476 cselib_val **vp, *v, *next;
c65ecebc 2477 int num_mems = 0;
9ddb66ca
JH
2478 rtx mem_addr;
2479
2480 mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0)));
2481 mem_rtx = canon_rtx (mem_rtx);
fa49fd0f 2482
7101fb18
JH
2483 vp = &first_containing_mem;
2484 for (v = *vp; v != &dummy_val; v = next)
fa49fd0f 2485 {
7101fb18
JH
2486 bool has_mem = false;
2487 struct elt_loc_list **p = &v->locs;
5847e8da 2488 bool had_locs = v->locs != NULL;
21afc57d 2489 rtx_insn *setting_insn = v->locs ? v->locs->setting_insn : NULL;
fa49fd0f 2490
7101fb18 2491 while (*p)
fa49fd0f 2492 {
7101fb18
JH
2493 rtx x = (*p)->loc;
2494 cselib_val *addr;
2495 struct elt_list **mem_chain;
2496
2497 /* MEMs may occur in locations only at the top level; below
2498 that every MEM or REG is substituted by its VALUE. */
3c0cb5de 2499 if (!MEM_P (x))
fa49fd0f 2500 {
7101fb18
JH
2501 p = &(*p)->next;
2502 continue;
2503 }
028d4092 2504 if (num_mems < param_max_cselib_memory_locations
bd280792
JR
2505 && ! canon_anti_dependence (x, false, mem_rtx,
2506 GET_MODE (mem_rtx), mem_addr))
7101fb18
JH
2507 {
2508 has_mem = true;
c65ecebc 2509 num_mems++;
7101fb18
JH
2510 p = &(*p)->next;
2511 continue;
fa49fd0f
RK
2512 }
2513
7101fb18
JH
2514 /* This one overlaps. */
2515 /* We must have a mapping from this MEM's address to the
2516 value (E). Remove that, too. */
4deef538 2517 addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0, GET_MODE (x));
faead9f7
AO
2518 addr = canonical_cselib_val (addr);
2519 gcc_checking_assert (v == canonical_cselib_val (v));
7101fb18
JH
2520 mem_chain = &addr->addr_list;
2521 for (;;)
2522 {
faead9f7
AO
2523 cselib_val *canon = canonical_cselib_val ((*mem_chain)->elt);
2524
2525 if (canon == v)
7101fb18
JH
2526 {
2527 unchain_one_elt_list (mem_chain);
2528 break;
2529 }
fa49fd0f 2530
faead9f7
AO
2531 /* Record canonicalized elt. */
2532 (*mem_chain)->elt = canon;
2533
7101fb18
JH
2534 mem_chain = &(*mem_chain)->next;
2535 }
fa49fd0f 2536
7101fb18
JH
2537 unchain_one_elt_loc_list (p);
2538 }
fa49fd0f 2539
bab8d962 2540 if (had_locs && cselib_useless_value_p (v))
5847e8da
AO
2541 {
2542 if (setting_insn && DEBUG_INSN_P (setting_insn))
2543 n_useless_debug_values++;
2544 else
2545 n_useless_values++;
2546 }
fa49fd0f 2547
7101fb18
JH
2548 next = v->next_containing_mem;
2549 if (has_mem)
2550 {
2551 *vp = v;
2552 vp = &(*vp)->next_containing_mem;
2553 }
2554 else
2555 v->next_containing_mem = NULL;
2556 }
2557 *vp = &dummy_val;
fa49fd0f
RK
2558}
2559
17d184e5 2560/* Invalidate DEST. */
fa49fd0f 2561
0d87c765 2562void
17d184e5 2563cselib_invalidate_rtx (rtx dest)
fa49fd0f 2564{
46d096a3
SB
2565 while (GET_CODE (dest) == SUBREG
2566 || GET_CODE (dest) == ZERO_EXTRACT
2567 || GET_CODE (dest) == STRICT_LOW_PART)
fa49fd0f
RK
2568 dest = XEXP (dest, 0);
2569
f8cfc6aa 2570 if (REG_P (dest))
17d184e5 2571 cselib_invalidate_regno (REGNO (dest), GET_MODE (dest));
3c0cb5de 2572 else if (MEM_P (dest))
fa49fd0f 2573 cselib_invalidate_mem (dest);
0d87c765
RH
2574}
2575
2576/* A wrapper for cselib_invalidate_rtx to be called via note_stores. */
2577
2578static void
17d184e5 2579cselib_invalidate_rtx_note_stores (rtx dest, const_rtx,
0d87c765
RH
2580 void *data ATTRIBUTE_UNUSED)
2581{
17d184e5 2582 cselib_invalidate_rtx (dest);
fa49fd0f
RK
2583}
2584
2585/* Record the result of a SET instruction. DEST is being set; the source
2586 contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT
2587 describes its address. */
2588
2589static void
7080f735 2590cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt)
fa49fd0f 2591{
fa49fd0f
RK
2592 if (src_elt == 0 || side_effects_p (dest))
2593 return;
2594
dc8afb70 2595 if (REG_P (dest))
fa49fd0f 2596 {
dc8afb70 2597 unsigned int dreg = REGNO (dest);
31825e57
DM
2598 if (dreg < FIRST_PSEUDO_REGISTER)
2599 {
dc8afb70 2600 unsigned int n = REG_NREGS (dest);
31825e57
DM
2601
2602 if (n > max_value_regs)
2603 max_value_regs = n;
2604 }
2605
60fa6660
AO
2606 if (REG_VALUES (dreg) == 0)
2607 {
6790d1ab 2608 used_regs[n_used_regs++] = dreg;
60fa6660
AO
2609 REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt);
2610 }
2611 else
2612 {
341c100f
NS
2613 /* The register should have been invalidated. */
2614 gcc_assert (REG_VALUES (dreg)->elt == 0);
2615 REG_VALUES (dreg)->elt = src_elt;
60fa6660
AO
2616 }
2617
bab8d962 2618 if (cselib_useless_value_p (src_elt))
fa49fd0f 2619 n_useless_values--;
6f2ffb4b 2620 new_elt_loc_list (src_elt, dest);
fa49fd0f 2621 }
3c0cb5de 2622 else if (MEM_P (dest) && dest_addr_elt != 0
463301c3 2623 && cselib_record_memory)
fa49fd0f 2624 {
bab8d962 2625 if (cselib_useless_value_p (src_elt))
fa49fd0f
RK
2626 n_useless_values--;
2627 add_mem_for_addr (dest_addr_elt, src_elt, dest);
2628 }
2629}
2630
6f2ffb4b
AO
2631/* Make ELT and X's VALUE equivalent to each other at INSN. */
2632
2633void
12ea1b95 2634cselib_add_permanent_equiv (cselib_val *elt, rtx x, rtx_insn *insn)
6f2ffb4b
AO
2635{
2636 cselib_val *nelt;
12ea1b95 2637 rtx_insn *save_cselib_current_insn = cselib_current_insn;
6f2ffb4b
AO
2638
2639 gcc_checking_assert (elt);
2640 gcc_checking_assert (PRESERVED_VALUE_P (elt->val_rtx));
2641 gcc_checking_assert (!side_effects_p (x));
2642
2643 cselib_current_insn = insn;
2644
2645 nelt = cselib_lookup (x, GET_MODE (elt->val_rtx), 1, VOIDmode);
2646
2647 if (nelt != elt)
2648 {
0f68ba3e
AO
2649 cselib_any_perm_equivs = true;
2650
6f2ffb4b
AO
2651 if (!PRESERVED_VALUE_P (nelt->val_rtx))
2652 cselib_preserve_value (nelt);
2653
2654 new_elt_loc_list (nelt, elt->val_rtx);
2655 }
2656
2657 cselib_current_insn = save_cselib_current_insn;
2658}
2659
0f68ba3e
AO
2660/* Return TRUE if any permanent equivalences have been recorded since
2661 the table was last initialized. */
2662bool
2663cselib_have_permanent_equivalences (void)
2664{
2665 return cselib_any_perm_equivs;
2666}
2667
33c45e51
JJ
2668/* Record stack_pointer_rtx to be equal to
2669 (plus:P cfa_base_preserved_val offset). Used by var-tracking
2670 at the start of basic blocks for !frame_pointer_needed functions. */
2671
2672void
2673cselib_record_sp_cfa_base_equiv (HOST_WIDE_INT offset, rtx_insn *insn)
2674{
2675 rtx sp_derived_value = NULL_RTX;
2676 for (struct elt_loc_list *l = cfa_base_preserved_val->locs; l; l = l->next)
2677 if (GET_CODE (l->loc) == VALUE
2678 && SP_DERIVED_VALUE_P (l->loc))
2679 {
2680 sp_derived_value = l->loc;
2681 break;
2682 }
2683 else if (GET_CODE (l->loc) == PLUS
2684 && GET_CODE (XEXP (l->loc, 0)) == VALUE
2685 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2686 && CONST_INT_P (XEXP (l->loc, 1)))
2687 {
2688 sp_derived_value = XEXP (l->loc, 0);
2689 offset = offset + UINTVAL (XEXP (l->loc, 1));
2690 break;
2691 }
2692 if (sp_derived_value == NULL_RTX)
2693 return;
2694 cselib_val *val
2695 = cselib_lookup_from_insn (plus_constant (Pmode, sp_derived_value, offset),
2696 Pmode, 1, VOIDmode, insn);
2697 if (val != NULL)
a615ea71
JJ
2698 {
2699 PRESERVED_VALUE_P (val->val_rtx) = 1;
2700 cselib_record_set (stack_pointer_rtx, val, NULL);
2701 }
33c45e51
JJ
2702}
2703
2704/* Return true if V is SP_DERIVED_VALUE_P (or SP_DERIVED_VALUE_P + CONST_INT)
2705 that can be expressed using cfa_base_preserved_val + CONST_INT. */
2706
2707bool
2708cselib_sp_derived_value_p (cselib_val *v)
2709{
2710 if (!SP_DERIVED_VALUE_P (v->val_rtx))
2711 for (struct elt_loc_list *l = v->locs; l; l = l->next)
2712 if (GET_CODE (l->loc) == PLUS
2713 && GET_CODE (XEXP (l->loc, 0)) == VALUE
2714 && SP_DERIVED_VALUE_P (XEXP (l->loc, 0))
2715 && CONST_INT_P (XEXP (l->loc, 1)))
2716 v = CSELIB_VAL_PTR (XEXP (l->loc, 0));
2717 if (!SP_DERIVED_VALUE_P (v->val_rtx))
2718 return false;
2719 for (struct elt_loc_list *l = v->locs; l; l = l->next)
2720 if (l->loc == cfa_base_preserved_val->val_rtx)
2721 return true;
2722 else if (GET_CODE (l->loc) == PLUS
2723 && XEXP (l->loc, 0) == cfa_base_preserved_val->val_rtx
2724 && CONST_INT_P (XEXP (l->loc, 1)))
2725 return true;
2726 return false;
2727}
2728
fa49fd0f
RK
2729/* There is no good way to determine how many elements there can be
2730 in a PARALLEL. Since it's fairly cheap, use a really large number. */
2731#define MAX_SETS (FIRST_PSEUDO_REGISTER * 2)
2732
4deef538
AO
2733struct cselib_record_autoinc_data
2734{
2735 struct cselib_set *sets;
2736 int n_sets;
2737};
2738
2739/* Callback for for_each_inc_dec. Records in ARG the SETs implied by
2740 autoinc RTXs: SRC plus SRCOFF if non-NULL is stored in DEST. */
2741
2742static int
2743cselib_record_autoinc_cb (rtx mem ATTRIBUTE_UNUSED, rtx op ATTRIBUTE_UNUSED,
2744 rtx dest, rtx src, rtx srcoff, void *arg)
2745{
2746 struct cselib_record_autoinc_data *data;
2747 data = (struct cselib_record_autoinc_data *)arg;
2748
2749 data->sets[data->n_sets].dest = dest;
2750
2751 if (srcoff)
2752 data->sets[data->n_sets].src = gen_rtx_PLUS (GET_MODE (src), src, srcoff);
2753 else
2754 data->sets[data->n_sets].src = src;
2755
2756 data->n_sets++;
2757
8d8e205b 2758 return 0;
4deef538
AO
2759}
2760
2761/* Record the effects of any sets and autoincs in INSN. */
fa49fd0f 2762static void
dd60a84c 2763cselib_record_sets (rtx_insn *insn)
fa49fd0f
RK
2764{
2765 int n_sets = 0;
2766 int i;
b5b8b0ac 2767 struct cselib_set sets[MAX_SETS];
b7933c21 2768 rtx cond = 0;
4deef538 2769 int n_sets_before_autoinc;
ff111948 2770 int n_strict_low_parts = 0;
4deef538 2771 struct cselib_record_autoinc_data data;
fa49fd0f 2772
45309d28 2773 rtx body = PATTERN (insn);
b7933c21
BS
2774 if (GET_CODE (body) == COND_EXEC)
2775 {
2776 cond = COND_EXEC_TEST (body);
2777 body = COND_EXEC_CODE (body);
2778 }
2779
fa49fd0f
RK
2780 /* Find all sets. */
2781 if (GET_CODE (body) == SET)
2782 {
2783 sets[0].src = SET_SRC (body);
2784 sets[0].dest = SET_DEST (body);
2785 n_sets = 1;
2786 }
2787 else if (GET_CODE (body) == PARALLEL)
2788 {
2789 /* Look through the PARALLEL and record the values being
2790 set, if possible. Also handle any CLOBBERs. */
2791 for (i = XVECLEN (body, 0) - 1; i >= 0; --i)
2792 {
2793 rtx x = XVECEXP (body, 0, i);
2794
2795 if (GET_CODE (x) == SET)
2796 {
2797 sets[n_sets].src = SET_SRC (x);
2798 sets[n_sets].dest = SET_DEST (x);
2799 n_sets++;
2800 }
2801 }
2802 }
2803
8dd5516b
JJ
2804 if (n_sets == 1
2805 && MEM_P (sets[0].src)
2806 && !cselib_record_memory
2807 && MEM_READONLY_P (sets[0].src))
2808 {
2809 rtx note = find_reg_equal_equiv_note (insn);
2810
2811 if (note && CONSTANT_P (XEXP (note, 0)))
2812 sets[0].src = XEXP (note, 0);
2813 }
2814
4deef538
AO
2815 data.sets = sets;
2816 data.n_sets = n_sets_before_autoinc = n_sets;
8d8e205b 2817 for_each_inc_dec (PATTERN (insn), cselib_record_autoinc_cb, &data);
4deef538
AO
2818 n_sets = data.n_sets;
2819
fa49fd0f
RK
2820 /* Look up the values that are read. Do this before invalidating the
2821 locations that are written. */
2822 for (i = 0; i < n_sets; i++)
2823 {
2824 rtx dest = sets[i].dest;
ff111948 2825 rtx orig = dest;
fa49fd0f
RK
2826
2827 /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for
2828 the low part after invalidating any knowledge about larger modes. */
2829 if (GET_CODE (sets[i].dest) == STRICT_LOW_PART)
2830 sets[i].dest = dest = XEXP (dest, 0);
2831
2832 /* We don't know how to record anything but REG or MEM. */
f8cfc6aa 2833 if (REG_P (dest)
3c0cb5de 2834 || (MEM_P (dest) && cselib_record_memory))
fa49fd0f 2835 {
b7933c21
BS
2836 rtx src = sets[i].src;
2837 if (cond)
be9ed5d5 2838 src = gen_rtx_IF_THEN_ELSE (GET_MODE (dest), cond, src, dest);
4deef538 2839 sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1, VOIDmode);
3c0cb5de 2840 if (MEM_P (dest))
d4ebfa65 2841 {
ef4bddc2 2842 machine_mode address_mode = get_address_mode (dest);
d4ebfa65
BE
2843
2844 sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0),
4deef538
AO
2845 address_mode, 1,
2846 GET_MODE (dest));
d4ebfa65 2847 }
fa49fd0f
RK
2848 else
2849 sets[i].dest_addr_elt = 0;
2850 }
ff111948
JJ
2851
2852 /* Improve handling of STRICT_LOW_PART if the current value is known
2853 to be const0_rtx, then the low bits will be set to dest and higher
2854 bits will remain zero. Used in code like:
2855
2856 {di:SI=0;clobber flags:CC;}
2857 flags:CCNO=cmp(bx:SI,0)
2858 strict_low_part(di:QI)=flags:CCNO<=0
2859
2860 where we can note both that di:QI=flags:CCNO<=0 and
2861 also that because di:SI is known to be 0 and strict_low_part(di:QI)
2862 preserves the upper bits that di:SI=zero_extend(flags:CCNO<=0). */
2863 scalar_int_mode mode;
2864 if (dest != orig
2865 && cselib_record_sets_hook
2866 && REG_P (dest)
2867 && HARD_REGISTER_P (dest)
bbc8d04f 2868 && sets[i].src_elt
ff111948
JJ
2869 && is_a <scalar_int_mode> (GET_MODE (dest), &mode)
2870 && n_sets + n_strict_low_parts < MAX_SETS)
2871 {
2872 opt_scalar_int_mode wider_mode_iter;
2873 FOR_EACH_WIDER_MODE (wider_mode_iter, mode)
2874 {
2875 scalar_int_mode wider_mode = wider_mode_iter.require ();
2876 if (GET_MODE_PRECISION (wider_mode) > BITS_PER_WORD)
2877 break;
2878
2879 rtx reg = gen_lowpart (wider_mode, dest);
2880 if (!REG_P (reg))
2881 break;
2882
2883 cselib_val *v = cselib_lookup (reg, wider_mode, 0, VOIDmode);
2884 if (!v)
2885 continue;
2886
2887 struct elt_loc_list *l;
2888 for (l = v->locs; l; l = l->next)
2889 if (l->loc == const0_rtx)
2890 break;
2891
2892 if (!l)
2893 continue;
2894
2895 sets[n_sets + n_strict_low_parts].dest = reg;
2896 sets[n_sets + n_strict_low_parts].src = dest;
2897 sets[n_sets + n_strict_low_parts++].src_elt = sets[i].src_elt;
2898 break;
2899 }
2900 }
fa49fd0f
RK
2901 }
2902
b5b8b0ac
AO
2903 if (cselib_record_sets_hook)
2904 cselib_record_sets_hook (insn, sets, n_sets);
2905
fa49fd0f
RK
2906 /* Invalidate all locations written by this insn. Note that the elts we
2907 looked up in the previous loop aren't affected, just some of their
2908 locations may go away. */
e8448ba5 2909 note_pattern_stores (body, cselib_invalidate_rtx_note_stores, NULL);
fa49fd0f 2910
4deef538
AO
2911 for (i = n_sets_before_autoinc; i < n_sets; i++)
2912 cselib_invalidate_rtx (sets[i].dest);
2913
b7048ab7
RH
2914 /* If this is an asm, look for duplicate sets. This can happen when the
2915 user uses the same value as an output multiple times. This is valid
2916 if the outputs are not actually used thereafter. Treat this case as
2917 if the value isn't actually set. We do this by smashing the destination
2918 to pc_rtx, so that we won't record the value later. */
2919 if (n_sets >= 2 && asm_noperands (body) >= 0)
2920 {
2921 for (i = 0; i < n_sets; i++)
2922 {
2923 rtx dest = sets[i].dest;
3c0cb5de 2924 if (REG_P (dest) || MEM_P (dest))
b7048ab7
RH
2925 {
2926 int j;
2927 for (j = i + 1; j < n_sets; j++)
2928 if (rtx_equal_p (dest, sets[j].dest))
2929 {
2930 sets[i].dest = pc_rtx;
2931 sets[j].dest = pc_rtx;
2932 }
2933 }
2934 }
2935 }
2936
fa49fd0f
RK
2937 /* Now enter the equivalences in our tables. */
2938 for (i = 0; i < n_sets; i++)
2939 {
2940 rtx dest = sets[i].dest;
f8cfc6aa 2941 if (REG_P (dest)
3c0cb5de 2942 || (MEM_P (dest) && cselib_record_memory))
fa49fd0f
RK
2943 cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt);
2944 }
ff111948
JJ
2945
2946 /* And deal with STRICT_LOW_PART. */
2947 for (i = 0; i < n_strict_low_parts; i++)
2948 {
2949 if (! PRESERVED_VALUE_P (sets[n_sets + i].src_elt->val_rtx))
2950 continue;
2951 machine_mode dest_mode = GET_MODE (sets[n_sets + i].dest);
2952 cselib_val *v
2953 = cselib_lookup (sets[n_sets + i].dest, dest_mode, 1, VOIDmode);
2954 cselib_preserve_value (v);
2955 rtx r = gen_rtx_ZERO_EXTEND (dest_mode,
2956 sets[n_sets + i].src_elt->val_rtx);
2957 cselib_add_permanent_equiv (v, r, insn);
2958 }
fa49fd0f
RK
2959}
2960
40155239
JJ
2961/* Return true if INSN in the prologue initializes hard_frame_pointer_rtx. */
2962
2963bool
8df68a82 2964fp_setter_insn (rtx_insn *insn)
40155239
JJ
2965{
2966 rtx expr, pat = NULL_RTX;
2967
2968 if (!RTX_FRAME_RELATED_P (insn))
2969 return false;
2970
2971 expr = find_reg_note (insn, REG_FRAME_RELATED_EXPR, NULL_RTX);
2972 if (expr)
2973 pat = XEXP (expr, 0);
2974 if (!modified_in_p (hard_frame_pointer_rtx, pat ? pat : insn))
2975 return false;
2976
2977 /* Don't return true for frame pointer restores in the epilogue. */
2978 if (find_reg_note (insn, REG_CFA_RESTORE, hard_frame_pointer_rtx))
2979 return false;
2980 return true;
2981}
2982
150760dd
RS
2983/* V is one of the values in REG_VALUES (REGNO). Return true if it
2984 would be invalidated by CALLEE_ABI. */
2985
2986static bool
2987cselib_invalidated_by_call_p (const function_abi &callee_abi,
2988 unsigned int regno, cselib_val *v)
2989{
2990 machine_mode mode = GET_MODE (v->val_rtx);
2991 if (mode == VOIDmode)
2992 {
2993 v = REG_VALUES (regno)->elt;
2994 if (!v)
2995 /* If we don't know what the mode of the constant value is, and we
2996 don't know what mode the register was set in, conservatively
2997 assume that the register is clobbered. The value's going to be
2998 essentially useless in this case anyway. */
2999 return true;
3000 mode = GET_MODE (v->val_rtx);
3001 }
3002 return callee_abi.clobbers_reg_p (mode, regno);
3003}
3004
fa49fd0f
RK
3005/* Record the effects of INSN. */
3006
3007void
dd60a84c 3008cselib_process_insn (rtx_insn *insn)
fa49fd0f
RK
3009{
3010 int i;
3011 rtx x;
3012
3013 cselib_current_insn = insn;
3014
f1257268 3015 /* Forget everything at a CODE_LABEL or a setjmp. */
f5d30aa6
JJ
3016 if ((LABEL_P (insn)
3017 || (CALL_P (insn)
f1257268 3018 && find_reg_note (insn, REG_SETJMP, NULL)))
f5d30aa6 3019 && !cselib_preserve_constants)
fa49fd0f 3020 {
5440c0e7 3021 cselib_reset_table (next_uid);
12ea1b95 3022 cselib_current_insn = NULL;
fa49fd0f
RK
3023 return;
3024 }
3025
3026 if (! INSN_P (insn))
3027 {
12ea1b95 3028 cselib_current_insn = NULL;
fa49fd0f
RK
3029 return;
3030 }
3031
3032 /* If this is a call instruction, forget anything stored in a
3033 call clobbered register, or, if this is not a const call, in
3034 memory. */
4b4bf941 3035 if (CALL_P (insn))
fa49fd0f 3036 {
6ee2cc70 3037 function_abi callee_abi = insn_callee_abi (insn);
fa49fd0f 3038 for (i = 0; i < FIRST_PSEUDO_REGISTER; i++)
150760dd
RS
3039 {
3040 elt_list **l = &REG_VALUES (i);
3041 while (*l)
3042 {
3043 cselib_val *v = (*l)->elt;
3044 if (v && cselib_invalidated_by_call_p (callee_abi, i, v))
3045 cselib_invalidate_regno_val (i, l);
3046 else
3047 l = &(*l)->next;
3048 }
3049 }
fa49fd0f 3050
becfd6e5
KZ
3051 /* Since it is not clear how cselib is going to be used, be
3052 conservative here and treat looping pure or const functions
3053 as if they were regular functions. */
3054 if (RTL_LOOPING_CONST_OR_PURE_CALL_P (insn)
3055 || !(RTL_CONST_OR_PURE_CALL_P (insn)))
fa49fd0f 3056 cselib_invalidate_mem (callmem);
007b405b
JDA
3057 else
3058 /* For const/pure calls, invalidate any argument slots because
3059 they are owned by the callee. */
3060 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
3061 if (GET_CODE (XEXP (x, 0)) == USE
3062 && MEM_P (XEXP (XEXP (x, 0), 0)))
3063 cselib_invalidate_mem (XEXP (XEXP (x, 0), 0));
fa49fd0f
RK
3064 }
3065
3066 cselib_record_sets (insn);
3067
fa49fd0f
RK
3068 /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only
3069 after we have processed the insn. */
4b4bf941 3070 if (CALL_P (insn))
f5d30aa6
JJ
3071 {
3072 for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1))
17d184e5
RS
3073 if (GET_CODE (XEXP (x, 0)) == CLOBBER)
3074 cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0));
3075
99788e06 3076 /* Flush everything on setjmp. */
f5d30aa6
JJ
3077 if (cselib_preserve_constants
3078 && find_reg_note (insn, REG_SETJMP, NULL))
3079 {
3080 cselib_preserve_only_values ();
3081 cselib_reset_table (next_uid);
3082 }
3083 }
fa49fd0f 3084
40155239
JJ
3085 /* On setter of the hard frame pointer if frame_pointer_needed,
3086 invalidate stack_pointer_rtx, so that sp and {,h}fp based
3087 VALUEs are distinct. */
3088 if (reload_completed
3089 && frame_pointer_needed
3090 && fp_setter_insn (insn))
3091 cselib_invalidate_rtx (stack_pointer_rtx);
3092
12ea1b95 3093 cselib_current_insn = NULL;
fa49fd0f 3094
80662856
AO
3095 if (n_useless_values > MAX_USELESS_VALUES
3096 /* remove_useless_values is linear in the hash table size. Avoid
3097 quadratic behavior for very large hashtables with very few
3098 useless elements. */
3099 && ((unsigned int)n_useless_values
c203e8a7 3100 > (cselib_hash_table->elements () - n_debug_values) / 4))
80662856 3101 remove_useless_values ();
fa49fd0f
RK
3102}
3103
fa49fd0f
RK
3104/* Initialize cselib for one pass. The caller must also call
3105 init_alias_analysis. */
3106
3107void
457eeaae 3108cselib_init (int record_what)
fa49fd0f 3109{
457eeaae
JJ
3110 cselib_record_memory = record_what & CSELIB_RECORD_MEMORY;
3111 cselib_preserve_constants = record_what & CSELIB_PRESERVE_CONSTANTS;
0f68ba3e 3112 cselib_any_perm_equivs = false;
ac3768f6
SB
3113
3114 /* (mem:BLK (scratch)) is a special mechanism to conflict with everything,
3115 see canon_true_dependence. This is only created once. */
fa49fd0f 3116 if (! callmem)
ac3768f6 3117 callmem = gen_rtx_MEM (BLKmode, gen_rtx_SCRATCH (VOIDmode));
fa49fd0f
RK
3118
3119 cselib_nregs = max_reg_num ();
6790d1ab
JH
3120
3121 /* We preserve reg_values to allow expensive clearing of the whole thing.
3122 Reallocate it however if it happens to be too large. */
3123 if (!reg_values || reg_values_size < cselib_nregs
3124 || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4))
e2500fed 3125 {
04695783 3126 free (reg_values);
6790d1ab
JH
3127 /* Some space for newly emit instructions so we don't end up
3128 reallocating in between passes. */
3129 reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16;
5ed6ace5 3130 reg_values = XCNEWVEC (struct elt_list *, reg_values_size);
e2500fed 3131 }
5ed6ace5 3132 used_regs = XNEWVEC (unsigned int, cselib_nregs);
6790d1ab 3133 n_used_regs = 0;
510c9192
ML
3134 /* FIXME: enable sanitization (PR87845) */
3135 cselib_hash_table
3136 = new hash_table<cselib_hasher> (31, /* ggc */ false,
3137 /* sanitize_eq_and_hash */ false);
0618dee5 3138 if (cselib_preserve_constants)
510c9192
ML
3139 cselib_preserved_hash_table
3140 = new hash_table<cselib_hasher> (31, /* ggc */ false,
3141 /* sanitize_eq_and_hash */ false);
5440c0e7 3142 next_uid = 1;
fa49fd0f
RK
3143}
3144
3145/* Called when the current user is done with cselib. */
3146
3147void
7080f735 3148cselib_finish (void)
fa49fd0f 3149{
0618dee5 3150 bool preserved = cselib_preserve_constants;
6fb5fa3c 3151 cselib_discard_hook = NULL;
457eeaae 3152 cselib_preserve_constants = false;
0f68ba3e 3153 cselib_any_perm_equivs = false;
457eeaae 3154 cfa_base_preserved_val = NULL;
9de9cbaf 3155 cfa_base_preserved_regno = INVALID_REGNUM;
fb0b2914
ML
3156 elt_list_pool.release ();
3157 elt_loc_list_pool.release ();
3158 cselib_val_pool.release ();
a78a26f1 3159 value_pool.release ();
eb232f4e 3160 cselib_clear_table ();
c203e8a7
TS
3161 delete cselib_hash_table;
3162 cselib_hash_table = NULL;
0618dee5 3163 if (preserved)
c203e8a7
TS
3164 delete cselib_preserved_hash_table;
3165 cselib_preserved_hash_table = NULL;
0fc0c4c9 3166 free (used_regs);
e2500fed 3167 used_regs = 0;
e2500fed 3168 n_useless_values = 0;
5847e8da
AO
3169 n_useless_debug_values = 0;
3170 n_debug_values = 0;
5440c0e7 3171 next_uid = 0;
fa49fd0f 3172}
e2500fed 3173
4a8fb1a1 3174/* Dump the cselib_val *X to FILE *OUT. */
b5b8b0ac 3175
4a8fb1a1
LC
3176int
3177dump_cselib_val (cselib_val **x, FILE *out)
b5b8b0ac 3178{
4a8fb1a1 3179 cselib_val *v = *x;
b5b8b0ac
AO
3180 bool need_lf = true;
3181
3182 print_inline_rtx (out, v->val_rtx, 0);
3183
3184 if (v->locs)
3185 {
3186 struct elt_loc_list *l = v->locs;
3187 if (need_lf)
3188 {
3189 fputc ('\n', out);
3190 need_lf = false;
3191 }
3192 fputs (" locs:", out);
3193 do
3194 {
42286976
JJ
3195 if (l->setting_insn)
3196 fprintf (out, "\n from insn %i ",
3197 INSN_UID (l->setting_insn));
3198 else
3199 fprintf (out, "\n ");
b5b8b0ac
AO
3200 print_inline_rtx (out, l->loc, 4);
3201 }
3202 while ((l = l->next));
3203 fputc ('\n', out);
3204 }
3205 else
3206 {
3207 fputs (" no locs", out);
3208 need_lf = true;
3209 }
3210
3211 if (v->addr_list)
3212 {
3213 struct elt_list *e = v->addr_list;
3214 if (need_lf)
3215 {
3216 fputc ('\n', out);
3217 need_lf = false;
3218 }
3219 fputs (" addr list:", out);
3220 do
3221 {
3222 fputs ("\n ", out);
3223 print_inline_rtx (out, e->elt->val_rtx, 2);
3224 }
3225 while ((e = e->next));
3226 fputc ('\n', out);
3227 }
3228 else
3229 {
3230 fputs (" no addrs", out);
3231 need_lf = true;
3232 }
3233
3234 if (v->next_containing_mem == &dummy_val)
3235 fputs (" last mem\n", out);
3236 else if (v->next_containing_mem)
3237 {
3238 fputs (" next mem ", out);
3239 print_inline_rtx (out, v->next_containing_mem->val_rtx, 2);
3240 fputc ('\n', out);
3241 }
3242 else if (need_lf)
3243 fputc ('\n', out);
3244
3245 return 1;
3246}
3247
3248/* Dump to OUT everything in the CSELIB table. */
3249
3250void
3251dump_cselib_table (FILE *out)
3252{
3253 fprintf (out, "cselib hash table:\n");
c203e8a7 3254 cselib_hash_table->traverse <FILE *, dump_cselib_val> (out);
0618dee5 3255 fprintf (out, "cselib preserved hash table:\n");
c203e8a7 3256 cselib_preserved_hash_table->traverse <FILE *, dump_cselib_val> (out);
b5b8b0ac
AO
3257 if (first_containing_mem != &dummy_val)
3258 {
3259 fputs ("first mem ", out);
3260 print_inline_rtx (out, first_containing_mem->val_rtx, 2);
3261 fputc ('\n', out);
3262 }
5440c0e7 3263 fprintf (out, "next uid %i\n", next_uid);
b5b8b0ac
AO
3264}
3265
e2500fed 3266#include "gt-cselib.h"