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