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