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