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