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