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