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