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