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