]>
Commit | Line | Data |
---|---|---|
fa49fd0f RK |
1 | /* Common subexpression elimination library for GNU compiler. |
2 | Copyright (C) 1987, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 1998, | |
d9221e01 | 3 | 1999, 2000, 2001, 2003, 2004 Free Software Foundation, Inc. |
fa49fd0f | 4 | |
1322177d | 5 | This file is part of GCC. |
fa49fd0f | 6 | |
1322177d LB |
7 | GCC is free software; you can redistribute it and/or modify it under |
8 | the terms of the GNU General Public License as published by the Free | |
9 | Software Foundation; either version 2, or (at your option) any later | |
10 | version. | |
fa49fd0f | 11 | |
1322177d LB |
12 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY |
13 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
14 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
15 | for more details. | |
fa49fd0f RK |
16 | |
17 | You should have received a copy of the GNU General Public License | |
1322177d LB |
18 | along with GCC; see the file COPYING. If not, write to the Free |
19 | Software Foundation, 59 Temple Place - Suite 330, Boston, MA | |
20 | 02111-1307, USA. */ | |
fa49fd0f RK |
21 | |
22 | #include "config.h" | |
23 | #include "system.h" | |
4977bab6 ZW |
24 | #include "coretypes.h" |
25 | #include "tm.h" | |
fa49fd0f RK |
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 "real.h" | |
33 | #include "insn-config.h" | |
34 | #include "recog.h" | |
35 | #include "function.h" | |
78528714 | 36 | #include "emit-rtl.h" |
fa49fd0f RK |
37 | #include "toplev.h" |
38 | #include "output.h" | |
39 | #include "ggc.h" | |
fa49fd0f RK |
40 | #include "hashtab.h" |
41 | #include "cselib.h" | |
c65ecebc | 42 | #include "params.h" |
6a59927d | 43 | #include "alloc-pool.h" |
fa49fd0f | 44 | |
463301c3 | 45 | static bool cselib_record_memory; |
7080f735 AJ |
46 | static int entry_and_rtx_equal_p (const void *, const void *); |
47 | static hashval_t get_value_hash (const void *); | |
48 | static struct elt_list *new_elt_list (struct elt_list *, cselib_val *); | |
49 | static struct elt_loc_list *new_elt_loc_list (struct elt_loc_list *, rtx); | |
50 | static void unchain_one_value (cselib_val *); | |
51 | static void unchain_one_elt_list (struct elt_list **); | |
52 | static void unchain_one_elt_loc_list (struct elt_loc_list **); | |
53 | static void clear_table (void); | |
54 | static int discard_useless_locs (void **, void *); | |
55 | static int discard_useless_values (void **, void *); | |
56 | static void remove_useless_values (void); | |
57 | static rtx wrap_constant (enum machine_mode, rtx); | |
0516f6fe | 58 | static unsigned int cselib_hash_rtx (rtx, enum machine_mode, int); |
7080f735 AJ |
59 | static cselib_val *new_cselib_val (unsigned int, enum machine_mode); |
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); | |
7080f735 | 63 | static void cselib_invalidate_mem (rtx); |
7080f735 AJ |
64 | static void cselib_record_set (rtx, cselib_val *, cselib_val *); |
65 | static void cselib_record_sets (rtx); | |
fa49fd0f RK |
66 | |
67 | /* There are three ways in which cselib can look up an rtx: | |
68 | - for a REG, the reg_values table (which is indexed by regno) is used | |
69 | - for a MEM, we recursively look up its address and then follow the | |
70 | addr_list of that value | |
71 | - for everything else, we compute a hash value and go through the hash | |
72 | table. Since different rtx's can still have the same hash value, | |
73 | this involves walking the table entries for a given value and comparing | |
74 | the locations of the entries with the rtx we are looking up. */ | |
75 | ||
76 | /* A table that enables us to look up elts by their value. */ | |
6790d1ab | 77 | static htab_t hash_table; |
fa49fd0f RK |
78 | |
79 | /* This is a global so we don't have to pass this through every function. | |
80 | It is used in new_elt_loc_list to set SETTING_INSN. */ | |
81 | static rtx cselib_current_insn; | |
9635cfad | 82 | static bool cselib_current_insn_in_libcall; |
fa49fd0f RK |
83 | |
84 | /* Every new unknown value gets a unique number. */ | |
85 | static unsigned int next_unknown_value; | |
86 | ||
87 | /* The number of registers we had when the varrays were last resized. */ | |
88 | static unsigned int cselib_nregs; | |
89 | ||
90 | /* Count values without known locations. Whenever this grows too big, we | |
91 | remove these useless values from the table. */ | |
92 | static int n_useless_values; | |
93 | ||
94 | /* Number of useless values before we remove them from the hash table. */ | |
95 | #define MAX_USELESS_VALUES 32 | |
96 | ||
60fa6660 AO |
97 | /* This table maps from register number to values. It does not |
98 | contain pointers to cselib_val structures, but rather elt_lists. | |
99 | The purpose is to be able to refer to the same register in | |
100 | different modes. The first element of the list defines the mode in | |
101 | which the register was set; if the mode is unknown or the value is | |
102 | no longer valid in that mode, ELT will be NULL for the first | |
103 | element. */ | |
6790d1ab JH |
104 | struct elt_list **reg_values; |
105 | unsigned int reg_values_size; | |
106 | #define REG_VALUES(i) reg_values[i] | |
fa49fd0f | 107 | |
31825e57 | 108 | /* The largest number of hard regs used by any entry added to the |
e34bb004 | 109 | REG_VALUES table. Cleared on each clear_table() invocation. */ |
31825e57 DM |
110 | static unsigned int max_value_regs; |
111 | ||
fa49fd0f RK |
112 | /* Here the set of indices I with REG_VALUES(I) != 0 is saved. This is used |
113 | in clear_table() for fast emptying. */ | |
6790d1ab JH |
114 | static unsigned int *used_regs; |
115 | static unsigned int n_used_regs; | |
fa49fd0f RK |
116 | |
117 | /* We pass this to cselib_invalidate_mem to invalidate all of | |
118 | memory for a non-const call instruction. */ | |
e2500fed | 119 | static GTY(()) rtx callmem; |
fa49fd0f | 120 | |
fa49fd0f RK |
121 | /* Set by discard_useless_locs if it deleted the last location of any |
122 | value. */ | |
123 | static int values_became_useless; | |
7101fb18 JH |
124 | |
125 | /* Used as stop element of the containing_mem list so we can check | |
126 | presence in the list by checking the next pointer. */ | |
127 | static cselib_val dummy_val; | |
128 | ||
7080f735 | 129 | /* Used to list all values that contain memory reference. |
7101fb18 JH |
130 | May or may not contain the useless values - the list is compacted |
131 | each time memory is invalidated. */ | |
132 | static cselib_val *first_containing_mem = &dummy_val; | |
23bd7a93 | 133 | static alloc_pool elt_loc_list_pool, elt_list_pool, cselib_val_pool, value_pool; |
fa49fd0f RK |
134 | \f |
135 | ||
136 | /* Allocate a struct elt_list and fill in its two elements with the | |
137 | arguments. */ | |
138 | ||
6a59927d | 139 | static inline struct elt_list * |
7080f735 | 140 | new_elt_list (struct elt_list *next, cselib_val *elt) |
fa49fd0f | 141 | { |
6a59927d JH |
142 | struct elt_list *el; |
143 | el = pool_alloc (elt_list_pool); | |
fa49fd0f RK |
144 | el->next = next; |
145 | el->elt = elt; | |
146 | return el; | |
147 | } | |
148 | ||
149 | /* Allocate a struct elt_loc_list and fill in its two elements with the | |
150 | arguments. */ | |
151 | ||
6a59927d | 152 | static inline struct elt_loc_list * |
7080f735 | 153 | new_elt_loc_list (struct elt_loc_list *next, rtx loc) |
fa49fd0f | 154 | { |
6a59927d JH |
155 | struct elt_loc_list *el; |
156 | el = pool_alloc (elt_loc_list_pool); | |
fa49fd0f RK |
157 | el->next = next; |
158 | el->loc = loc; | |
159 | el->setting_insn = cselib_current_insn; | |
9635cfad | 160 | el->in_libcall = cselib_current_insn_in_libcall; |
fa49fd0f RK |
161 | return el; |
162 | } | |
163 | ||
164 | /* The elt_list at *PL is no longer needed. Unchain it and free its | |
165 | storage. */ | |
166 | ||
6a59927d | 167 | static inline void |
7080f735 | 168 | unchain_one_elt_list (struct elt_list **pl) |
fa49fd0f RK |
169 | { |
170 | struct elt_list *l = *pl; | |
171 | ||
172 | *pl = l->next; | |
6a59927d | 173 | pool_free (elt_list_pool, l); |
fa49fd0f RK |
174 | } |
175 | ||
176 | /* Likewise for elt_loc_lists. */ | |
177 | ||
178 | static void | |
7080f735 | 179 | unchain_one_elt_loc_list (struct elt_loc_list **pl) |
fa49fd0f RK |
180 | { |
181 | struct elt_loc_list *l = *pl; | |
182 | ||
183 | *pl = l->next; | |
6a59927d | 184 | pool_free (elt_loc_list_pool, l); |
fa49fd0f RK |
185 | } |
186 | ||
187 | /* Likewise for cselib_vals. This also frees the addr_list associated with | |
188 | V. */ | |
189 | ||
190 | static void | |
7080f735 | 191 | unchain_one_value (cselib_val *v) |
fa49fd0f RK |
192 | { |
193 | while (v->addr_list) | |
194 | unchain_one_elt_list (&v->addr_list); | |
195 | ||
6a59927d | 196 | pool_free (cselib_val_pool, v); |
fa49fd0f RK |
197 | } |
198 | ||
199 | /* Remove all entries from the hash table. Also used during | |
200 | initialization. If CLEAR_ALL isn't set, then only clear the entries | |
201 | which are known to have been used. */ | |
202 | ||
203 | static void | |
7080f735 | 204 | clear_table (void) |
fa49fd0f RK |
205 | { |
206 | unsigned int i; | |
207 | ||
6790d1ab JH |
208 | for (i = 0; i < n_used_regs; i++) |
209 | REG_VALUES (used_regs[i]) = 0; | |
fa49fd0f | 210 | |
31825e57 DM |
211 | max_value_regs = 0; |
212 | ||
6790d1ab | 213 | n_used_regs = 0; |
fa49fd0f RK |
214 | |
215 | htab_empty (hash_table); | |
fa49fd0f | 216 | |
fa49fd0f RK |
217 | n_useless_values = 0; |
218 | ||
219 | next_unknown_value = 0; | |
7101fb18 JH |
220 | |
221 | first_containing_mem = &dummy_val; | |
fa49fd0f RK |
222 | } |
223 | ||
224 | /* The equality test for our hash table. The first argument ENTRY is a table | |
225 | element (i.e. a cselib_val), while the second arg X is an rtx. We know | |
226 | that all callers of htab_find_slot_with_hash will wrap CONST_INTs into a | |
227 | CONST of an appropriate mode. */ | |
228 | ||
229 | static int | |
7080f735 | 230 | entry_and_rtx_equal_p (const void *entry, const void *x_arg) |
fa49fd0f RK |
231 | { |
232 | struct elt_loc_list *l; | |
233 | const cselib_val *v = (const cselib_val *) entry; | |
234 | rtx x = (rtx) x_arg; | |
235 | enum machine_mode mode = GET_MODE (x); | |
236 | ||
341c100f NS |
237 | gcc_assert (GET_CODE (x) != CONST_INT |
238 | && (mode != VOIDmode || GET_CODE (x) != CONST_DOUBLE)); | |
239 | ||
fa49fd0f RK |
240 | if (mode != GET_MODE (v->u.val_rtx)) |
241 | return 0; | |
242 | ||
243 | /* Unwrap X if necessary. */ | |
244 | if (GET_CODE (x) == CONST | |
245 | && (GET_CODE (XEXP (x, 0)) == CONST_INT | |
246 | || GET_CODE (XEXP (x, 0)) == CONST_DOUBLE)) | |
247 | x = XEXP (x, 0); | |
7080f735 | 248 | |
fa49fd0f RK |
249 | /* We don't guarantee that distinct rtx's have different hash values, |
250 | so we need to do a comparison. */ | |
251 | for (l = v->locs; l; l = l->next) | |
252 | if (rtx_equal_for_cselib_p (l->loc, x)) | |
253 | return 1; | |
254 | ||
255 | return 0; | |
256 | } | |
257 | ||
258 | /* The hash function for our hash table. The value is always computed with | |
0516f6fe SB |
259 | cselib_hash_rtx when adding an element; this function just extracts the |
260 | hash value from a cselib_val structure. */ | |
fa49fd0f | 261 | |
fb7e6024 | 262 | static hashval_t |
7080f735 | 263 | get_value_hash (const void *entry) |
fa49fd0f RK |
264 | { |
265 | const cselib_val *v = (const cselib_val *) entry; | |
266 | return v->value; | |
267 | } | |
268 | ||
269 | /* Return true if X contains a VALUE rtx. If ONLY_USELESS is set, we | |
270 | only return true for values which point to a cselib_val whose value | |
271 | element has been set to zero, which implies the cselib_val will be | |
272 | removed. */ | |
273 | ||
274 | int | |
7080f735 | 275 | references_value_p (rtx x, int only_useless) |
fa49fd0f RK |
276 | { |
277 | enum rtx_code code = GET_CODE (x); | |
278 | const char *fmt = GET_RTX_FORMAT (code); | |
279 | int i, j; | |
280 | ||
281 | if (GET_CODE (x) == VALUE | |
282 | && (! only_useless || CSELIB_VAL_PTR (x)->locs == 0)) | |
283 | return 1; | |
284 | ||
285 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
286 | { | |
287 | if (fmt[i] == 'e' && references_value_p (XEXP (x, i), only_useless)) | |
288 | return 1; | |
289 | else if (fmt[i] == 'E') | |
290 | for (j = 0; j < XVECLEN (x, i); j++) | |
291 | if (references_value_p (XVECEXP (x, i, j), only_useless)) | |
292 | return 1; | |
293 | } | |
294 | ||
295 | return 0; | |
296 | } | |
297 | ||
298 | /* For all locations found in X, delete locations that reference useless | |
299 | values (i.e. values without any location). Called through | |
300 | htab_traverse. */ | |
301 | ||
302 | static int | |
7080f735 | 303 | discard_useless_locs (void **x, void *info ATTRIBUTE_UNUSED) |
fa49fd0f RK |
304 | { |
305 | cselib_val *v = (cselib_val *)*x; | |
306 | struct elt_loc_list **p = &v->locs; | |
307 | int had_locs = v->locs != 0; | |
308 | ||
309 | while (*p) | |
310 | { | |
311 | if (references_value_p ((*p)->loc, 1)) | |
312 | unchain_one_elt_loc_list (p); | |
313 | else | |
314 | p = &(*p)->next; | |
315 | } | |
316 | ||
317 | if (had_locs && v->locs == 0) | |
318 | { | |
319 | n_useless_values++; | |
320 | values_became_useless = 1; | |
321 | } | |
322 | return 1; | |
323 | } | |
324 | ||
325 | /* If X is a value with no locations, remove it from the hashtable. */ | |
326 | ||
327 | static int | |
7080f735 | 328 | discard_useless_values (void **x, void *info ATTRIBUTE_UNUSED) |
fa49fd0f RK |
329 | { |
330 | cselib_val *v = (cselib_val *)*x; | |
331 | ||
332 | if (v->locs == 0) | |
333 | { | |
18874af6 | 334 | CSELIB_VAL_PTR (v->u.val_rtx) = NULL; |
fa49fd0f RK |
335 | htab_clear_slot (hash_table, x); |
336 | unchain_one_value (v); | |
337 | n_useless_values--; | |
338 | } | |
339 | ||
340 | return 1; | |
341 | } | |
342 | ||
343 | /* Clean out useless values (i.e. those which no longer have locations | |
344 | associated with them) from the hash table. */ | |
345 | ||
346 | static void | |
7080f735 | 347 | remove_useless_values (void) |
fa49fd0f | 348 | { |
7101fb18 | 349 | cselib_val **p, *v; |
fa49fd0f RK |
350 | /* First pass: eliminate locations that reference the value. That in |
351 | turn can make more values useless. */ | |
352 | do | |
353 | { | |
354 | values_became_useless = 0; | |
355 | htab_traverse (hash_table, discard_useless_locs, 0); | |
356 | } | |
357 | while (values_became_useless); | |
358 | ||
359 | /* Second pass: actually remove the values. */ | |
fa49fd0f | 360 | |
7101fb18 JH |
361 | p = &first_containing_mem; |
362 | for (v = *p; v != &dummy_val; v = v->next_containing_mem) | |
363 | if (v->locs) | |
364 | { | |
365 | *p = v; | |
366 | p = &(*p)->next_containing_mem; | |
367 | } | |
368 | *p = &dummy_val; | |
369 | ||
3e2a0bd2 JH |
370 | htab_traverse (hash_table, discard_useless_values, 0); |
371 | ||
341c100f | 372 | gcc_assert (!n_useless_values); |
fa49fd0f RK |
373 | } |
374 | ||
60fa6660 AO |
375 | /* Return the mode in which a register was last set. If X is not a |
376 | register, return its mode. If the mode in which the register was | |
377 | set is not known, or the value was already clobbered, return | |
378 | VOIDmode. */ | |
379 | ||
380 | enum machine_mode | |
7080f735 | 381 | cselib_reg_set_mode (rtx x) |
60fa6660 | 382 | { |
f8cfc6aa | 383 | if (!REG_P (x)) |
60fa6660 AO |
384 | return GET_MODE (x); |
385 | ||
386 | if (REG_VALUES (REGNO (x)) == NULL | |
387 | || REG_VALUES (REGNO (x))->elt == NULL) | |
388 | return VOIDmode; | |
389 | ||
390 | return GET_MODE (REG_VALUES (REGNO (x))->elt->u.val_rtx); | |
391 | } | |
392 | ||
fa49fd0f RK |
393 | /* Return nonzero if we can prove that X and Y contain the same value, taking |
394 | our gathered information into account. */ | |
395 | ||
396 | int | |
7080f735 | 397 | rtx_equal_for_cselib_p (rtx x, rtx y) |
fa49fd0f RK |
398 | { |
399 | enum rtx_code code; | |
400 | const char *fmt; | |
401 | int i; | |
7080f735 | 402 | |
f8cfc6aa | 403 | if (REG_P (x) || MEM_P (x)) |
fa49fd0f RK |
404 | { |
405 | cselib_val *e = cselib_lookup (x, GET_MODE (x), 0); | |
406 | ||
407 | if (e) | |
408 | x = e->u.val_rtx; | |
409 | } | |
410 | ||
f8cfc6aa | 411 | if (REG_P (y) || MEM_P (y)) |
fa49fd0f RK |
412 | { |
413 | cselib_val *e = cselib_lookup (y, GET_MODE (y), 0); | |
414 | ||
415 | if (e) | |
416 | y = e->u.val_rtx; | |
417 | } | |
418 | ||
419 | if (x == y) | |
420 | return 1; | |
421 | ||
422 | if (GET_CODE (x) == VALUE && GET_CODE (y) == VALUE) | |
423 | return CSELIB_VAL_PTR (x) == CSELIB_VAL_PTR (y); | |
424 | ||
425 | if (GET_CODE (x) == VALUE) | |
426 | { | |
427 | cselib_val *e = CSELIB_VAL_PTR (x); | |
428 | struct elt_loc_list *l; | |
429 | ||
430 | for (l = e->locs; l; l = l->next) | |
431 | { | |
432 | rtx t = l->loc; | |
433 | ||
434 | /* Avoid infinite recursion. */ | |
3c0cb5de | 435 | if (REG_P (t) || MEM_P (t)) |
fa49fd0f RK |
436 | continue; |
437 | else if (rtx_equal_for_cselib_p (t, y)) | |
438 | return 1; | |
439 | } | |
7080f735 | 440 | |
fa49fd0f RK |
441 | return 0; |
442 | } | |
443 | ||
444 | if (GET_CODE (y) == VALUE) | |
445 | { | |
446 | cselib_val *e = CSELIB_VAL_PTR (y); | |
447 | struct elt_loc_list *l; | |
448 | ||
449 | for (l = e->locs; l; l = l->next) | |
450 | { | |
451 | rtx t = l->loc; | |
452 | ||
3c0cb5de | 453 | if (REG_P (t) || MEM_P (t)) |
fa49fd0f RK |
454 | continue; |
455 | else if (rtx_equal_for_cselib_p (x, t)) | |
456 | return 1; | |
457 | } | |
7080f735 | 458 | |
fa49fd0f RK |
459 | return 0; |
460 | } | |
461 | ||
462 | if (GET_CODE (x) != GET_CODE (y) || GET_MODE (x) != GET_MODE (y)) | |
463 | return 0; | |
464 | ||
465 | /* This won't be handled correctly by the code below. */ | |
466 | if (GET_CODE (x) == LABEL_REF) | |
467 | return XEXP (x, 0) == XEXP (y, 0); | |
7080f735 | 468 | |
fa49fd0f RK |
469 | code = GET_CODE (x); |
470 | fmt = GET_RTX_FORMAT (code); | |
471 | ||
472 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
473 | { | |
474 | int j; | |
475 | ||
476 | switch (fmt[i]) | |
477 | { | |
478 | case 'w': | |
479 | if (XWINT (x, i) != XWINT (y, i)) | |
480 | return 0; | |
481 | break; | |
482 | ||
483 | case 'n': | |
484 | case 'i': | |
485 | if (XINT (x, i) != XINT (y, i)) | |
486 | return 0; | |
487 | break; | |
488 | ||
489 | case 'V': | |
490 | case 'E': | |
491 | /* Two vectors must have the same length. */ | |
492 | if (XVECLEN (x, i) != XVECLEN (y, i)) | |
493 | return 0; | |
494 | ||
495 | /* And the corresponding elements must match. */ | |
496 | for (j = 0; j < XVECLEN (x, i); j++) | |
497 | if (! rtx_equal_for_cselib_p (XVECEXP (x, i, j), | |
498 | XVECEXP (y, i, j))) | |
499 | return 0; | |
500 | break; | |
501 | ||
502 | case 'e': | |
503 | if (! rtx_equal_for_cselib_p (XEXP (x, i), XEXP (y, i))) | |
504 | return 0; | |
505 | break; | |
506 | ||
507 | case 'S': | |
508 | case 's': | |
509 | if (strcmp (XSTR (x, i), XSTR (y, i))) | |
510 | return 0; | |
511 | break; | |
512 | ||
513 | case 'u': | |
514 | /* These are just backpointers, so they don't matter. */ | |
515 | break; | |
516 | ||
517 | case '0': | |
518 | case 't': | |
519 | break; | |
520 | ||
521 | /* It is believed that rtx's at this level will never | |
522 | contain anything but integers and other rtx's, | |
523 | except for within LABEL_REFs and SYMBOL_REFs. */ | |
524 | default: | |
341c100f | 525 | gcc_unreachable (); |
fa49fd0f RK |
526 | } |
527 | } | |
528 | return 1; | |
529 | } | |
530 | ||
531 | /* We need to pass down the mode of constants through the hash table | |
532 | functions. For that purpose, wrap them in a CONST of the appropriate | |
533 | mode. */ | |
534 | static rtx | |
7080f735 | 535 | wrap_constant (enum machine_mode mode, rtx x) |
fa49fd0f RK |
536 | { |
537 | if (GET_CODE (x) != CONST_INT | |
538 | && (GET_CODE (x) != CONST_DOUBLE || GET_MODE (x) != VOIDmode)) | |
539 | return x; | |
341c100f | 540 | gcc_assert (mode != VOIDmode); |
fa49fd0f RK |
541 | return gen_rtx_CONST (mode, x); |
542 | } | |
543 | ||
544 | /* Hash an rtx. Return 0 if we couldn't hash the rtx. | |
545 | For registers and memory locations, we look up their cselib_val structure | |
546 | and return its VALUE element. | |
547 | Possible reasons for return 0 are: the object is volatile, or we couldn't | |
548 | find a register or memory location in the table and CREATE is zero. If | |
549 | CREATE is nonzero, table elts are created for regs and mem. | |
550 | MODE is used in hashing for CONST_INTs only; | |
551 | otherwise the mode of X is used. */ | |
552 | ||
553 | static unsigned int | |
0516f6fe | 554 | cselib_hash_rtx (rtx x, enum machine_mode mode, int create) |
fa49fd0f RK |
555 | { |
556 | cselib_val *e; | |
557 | int i, j; | |
558 | enum rtx_code code; | |
559 | const char *fmt; | |
560 | unsigned int hash = 0; | |
561 | ||
fa49fd0f RK |
562 | code = GET_CODE (x); |
563 | hash += (unsigned) code + (unsigned) GET_MODE (x); | |
564 | ||
565 | switch (code) | |
566 | { | |
567 | case MEM: | |
568 | case REG: | |
569 | e = cselib_lookup (x, GET_MODE (x), create); | |
570 | if (! e) | |
571 | return 0; | |
572 | ||
a4f4333a | 573 | return e->value; |
fa49fd0f RK |
574 | |
575 | case CONST_INT: | |
576 | hash += ((unsigned) CONST_INT << 7) + (unsigned) mode + INTVAL (x); | |
dc76f41c | 577 | return hash ? hash : (unsigned int) CONST_INT; |
fa49fd0f RK |
578 | |
579 | case CONST_DOUBLE: | |
580 | /* This is like the general case, except that it only counts | |
581 | the integers representing the constant. */ | |
582 | hash += (unsigned) code + (unsigned) GET_MODE (x); | |
583 | if (GET_MODE (x) != VOIDmode) | |
46b33600 | 584 | hash += real_hash (CONST_DOUBLE_REAL_VALUE (x)); |
fa49fd0f RK |
585 | else |
586 | hash += ((unsigned) CONST_DOUBLE_LOW (x) | |
587 | + (unsigned) CONST_DOUBLE_HIGH (x)); | |
dc76f41c | 588 | return hash ? hash : (unsigned int) CONST_DOUBLE; |
fa49fd0f | 589 | |
69ef87e2 AH |
590 | case CONST_VECTOR: |
591 | { | |
592 | int units; | |
593 | rtx elt; | |
594 | ||
595 | units = CONST_VECTOR_NUNITS (x); | |
596 | ||
597 | for (i = 0; i < units; ++i) | |
598 | { | |
599 | elt = CONST_VECTOR_ELT (x, i); | |
0516f6fe | 600 | hash += cselib_hash_rtx (elt, GET_MODE (elt), 0); |
69ef87e2 AH |
601 | } |
602 | ||
603 | return hash; | |
604 | } | |
605 | ||
fa49fd0f RK |
606 | /* Assume there is only one rtx object for any given label. */ |
607 | case LABEL_REF: | |
608 | hash | |
609 | += ((unsigned) LABEL_REF << 7) + (unsigned long) XEXP (x, 0); | |
dc76f41c | 610 | return hash ? hash : (unsigned int) LABEL_REF; |
fa49fd0f RK |
611 | |
612 | case SYMBOL_REF: | |
613 | hash | |
614 | += ((unsigned) SYMBOL_REF << 7) + (unsigned long) XSTR (x, 0); | |
dc76f41c | 615 | return hash ? hash : (unsigned int) SYMBOL_REF; |
fa49fd0f RK |
616 | |
617 | case PRE_DEC: | |
618 | case PRE_INC: | |
619 | case POST_DEC: | |
620 | case POST_INC: | |
621 | case POST_MODIFY: | |
622 | case PRE_MODIFY: | |
623 | case PC: | |
624 | case CC0: | |
625 | case CALL: | |
626 | case UNSPEC_VOLATILE: | |
627 | return 0; | |
628 | ||
629 | case ASM_OPERANDS: | |
630 | if (MEM_VOLATILE_P (x)) | |
631 | return 0; | |
632 | ||
633 | break; | |
7080f735 | 634 | |
fa49fd0f RK |
635 | default: |
636 | break; | |
637 | } | |
638 | ||
639 | i = GET_RTX_LENGTH (code) - 1; | |
640 | fmt = GET_RTX_FORMAT (code); | |
641 | for (; i >= 0; i--) | |
642 | { | |
341c100f | 643 | switch (fmt[i]) |
fa49fd0f | 644 | { |
341c100f | 645 | case 'e': |
fa49fd0f | 646 | { |
341c100f NS |
647 | rtx tem = XEXP (x, i); |
648 | unsigned int tem_hash = cselib_hash_rtx (tem, 0, create); | |
649 | ||
fa49fd0f RK |
650 | if (tem_hash == 0) |
651 | return 0; | |
341c100f | 652 | |
fa49fd0f RK |
653 | hash += tem_hash; |
654 | } | |
341c100f NS |
655 | break; |
656 | case 'E': | |
657 | for (j = 0; j < XVECLEN (x, i); j++) | |
658 | { | |
659 | unsigned int tem_hash | |
660 | = cselib_hash_rtx (XVECEXP (x, i, j), 0, create); | |
661 | ||
662 | if (tem_hash == 0) | |
663 | return 0; | |
664 | ||
665 | hash += tem_hash; | |
666 | } | |
667 | break; | |
fa49fd0f | 668 | |
341c100f NS |
669 | case 's': |
670 | { | |
671 | const unsigned char *p = (const unsigned char *) XSTR (x, i); | |
672 | ||
673 | if (p) | |
674 | while (*p) | |
675 | hash += *p++; | |
676 | break; | |
677 | } | |
678 | ||
679 | case 'i': | |
680 | hash += XINT (x, i); | |
681 | break; | |
682 | ||
683 | case '0': | |
684 | case 't': | |
685 | /* unused */ | |
686 | break; | |
687 | ||
688 | default: | |
689 | gcc_unreachable (); | |
fa49fd0f | 690 | } |
fa49fd0f RK |
691 | } |
692 | ||
dc76f41c | 693 | return hash ? hash : 1 + (unsigned int) GET_CODE (x); |
fa49fd0f RK |
694 | } |
695 | ||
696 | /* Create a new value structure for VALUE and initialize it. The mode of the | |
697 | value is MODE. */ | |
698 | ||
6a59927d | 699 | static inline cselib_val * |
7080f735 | 700 | new_cselib_val (unsigned int value, enum machine_mode mode) |
fa49fd0f | 701 | { |
6a59927d | 702 | cselib_val *e = pool_alloc (cselib_val_pool); |
fa49fd0f | 703 | |
341c100f | 704 | gcc_assert (value); |
fa49fd0f RK |
705 | |
706 | e->value = value; | |
23bd7a93 JH |
707 | /* We use custom method to allocate this RTL construct because it accounts |
708 | about 8% of overall memory usage. */ | |
709 | e->u.val_rtx = pool_alloc (value_pool); | |
710 | memset (e->u.val_rtx, 0, RTX_HDR_SIZE); | |
711 | PUT_CODE (e->u.val_rtx, VALUE); | |
712 | PUT_MODE (e->u.val_rtx, mode); | |
fa49fd0f RK |
713 | CSELIB_VAL_PTR (e->u.val_rtx) = e; |
714 | e->addr_list = 0; | |
715 | e->locs = 0; | |
7101fb18 | 716 | e->next_containing_mem = 0; |
fa49fd0f RK |
717 | return e; |
718 | } | |
719 | ||
720 | /* ADDR_ELT is a value that is used as address. MEM_ELT is the value that | |
721 | contains the data at this address. X is a MEM that represents the | |
722 | value. Update the two value structures to represent this situation. */ | |
723 | ||
724 | static void | |
7080f735 | 725 | add_mem_for_addr (cselib_val *addr_elt, cselib_val *mem_elt, rtx x) |
fa49fd0f | 726 | { |
fa49fd0f RK |
727 | struct elt_loc_list *l; |
728 | ||
729 | /* Avoid duplicates. */ | |
730 | for (l = mem_elt->locs; l; l = l->next) | |
3c0cb5de | 731 | if (MEM_P (l->loc) |
fa49fd0f RK |
732 | && CSELIB_VAL_PTR (XEXP (l->loc, 0)) == addr_elt) |
733 | return; | |
734 | ||
fa49fd0f | 735 | addr_elt->addr_list = new_elt_list (addr_elt->addr_list, mem_elt); |
f1ec5147 RK |
736 | mem_elt->locs |
737 | = new_elt_loc_list (mem_elt->locs, | |
738 | replace_equiv_address_nv (x, addr_elt->u.val_rtx)); | |
7101fb18 JH |
739 | if (mem_elt->next_containing_mem == NULL) |
740 | { | |
741 | mem_elt->next_containing_mem = first_containing_mem; | |
742 | first_containing_mem = mem_elt; | |
743 | } | |
fa49fd0f RK |
744 | } |
745 | ||
746 | /* Subroutine of cselib_lookup. Return a value for X, which is a MEM rtx. | |
747 | If CREATE, make a new one if we haven't seen it before. */ | |
748 | ||
749 | static cselib_val * | |
7080f735 | 750 | cselib_lookup_mem (rtx x, int create) |
fa49fd0f RK |
751 | { |
752 | enum machine_mode mode = GET_MODE (x); | |
753 | void **slot; | |
754 | cselib_val *addr; | |
755 | cselib_val *mem_elt; | |
756 | struct elt_list *l; | |
757 | ||
758 | if (MEM_VOLATILE_P (x) || mode == BLKmode | |
463301c3 | 759 | || !cselib_record_memory |
fa49fd0f RK |
760 | || (FLOAT_MODE_P (mode) && flag_float_store)) |
761 | return 0; | |
762 | ||
763 | /* Look up the value for the address. */ | |
764 | addr = cselib_lookup (XEXP (x, 0), mode, create); | |
765 | if (! addr) | |
766 | return 0; | |
767 | ||
768 | /* Find a value that describes a value of our mode at that address. */ | |
769 | for (l = addr->addr_list; l; l = l->next) | |
770 | if (GET_MODE (l->elt->u.val_rtx) == mode) | |
771 | return l->elt; | |
772 | ||
773 | if (! create) | |
774 | return 0; | |
775 | ||
776 | mem_elt = new_cselib_val (++next_unknown_value, mode); | |
777 | add_mem_for_addr (addr, mem_elt, x); | |
778 | slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x), | |
779 | mem_elt->value, INSERT); | |
780 | *slot = mem_elt; | |
781 | return mem_elt; | |
782 | } | |
783 | ||
784 | /* Walk rtx X and replace all occurrences of REG and MEM subexpressions | |
785 | with VALUE expressions. This way, it becomes independent of changes | |
786 | to registers and memory. | |
787 | X isn't actually modified; if modifications are needed, new rtl is | |
788 | allocated. However, the return value can share rtl with X. */ | |
789 | ||
91700444 | 790 | rtx |
7080f735 | 791 | cselib_subst_to_values (rtx x) |
fa49fd0f RK |
792 | { |
793 | enum rtx_code code = GET_CODE (x); | |
794 | const char *fmt = GET_RTX_FORMAT (code); | |
795 | cselib_val *e; | |
796 | struct elt_list *l; | |
797 | rtx copy = x; | |
798 | int i; | |
799 | ||
800 | switch (code) | |
801 | { | |
802 | case REG: | |
60fa6660 AO |
803 | l = REG_VALUES (REGNO (x)); |
804 | if (l && l->elt == NULL) | |
805 | l = l->next; | |
806 | for (; l; l = l->next) | |
fa49fd0f RK |
807 | if (GET_MODE (l->elt->u.val_rtx) == GET_MODE (x)) |
808 | return l->elt->u.val_rtx; | |
809 | ||
341c100f | 810 | gcc_unreachable (); |
fa49fd0f RK |
811 | |
812 | case MEM: | |
813 | e = cselib_lookup_mem (x, 0); | |
814 | if (! e) | |
91700444 BS |
815 | { |
816 | /* This happens for autoincrements. Assign a value that doesn't | |
817 | match any other. */ | |
818 | e = new_cselib_val (++next_unknown_value, GET_MODE (x)); | |
819 | } | |
fa49fd0f RK |
820 | return e->u.val_rtx; |
821 | ||
fa49fd0f | 822 | case CONST_DOUBLE: |
69ef87e2 | 823 | case CONST_VECTOR: |
fa49fd0f RK |
824 | case CONST_INT: |
825 | return x; | |
826 | ||
91700444 BS |
827 | case POST_INC: |
828 | case PRE_INC: | |
829 | case POST_DEC: | |
830 | case PRE_DEC: | |
831 | case POST_MODIFY: | |
832 | case PRE_MODIFY: | |
833 | e = new_cselib_val (++next_unknown_value, GET_MODE (x)); | |
834 | return e->u.val_rtx; | |
7080f735 | 835 | |
fa49fd0f RK |
836 | default: |
837 | break; | |
838 | } | |
839 | ||
840 | for (i = GET_RTX_LENGTH (code) - 1; i >= 0; i--) | |
841 | { | |
842 | if (fmt[i] == 'e') | |
843 | { | |
844 | rtx t = cselib_subst_to_values (XEXP (x, i)); | |
845 | ||
846 | if (t != XEXP (x, i) && x == copy) | |
847 | copy = shallow_copy_rtx (x); | |
848 | ||
849 | XEXP (copy, i) = t; | |
850 | } | |
851 | else if (fmt[i] == 'E') | |
852 | { | |
853 | int j, k; | |
854 | ||
855 | for (j = 0; j < XVECLEN (x, i); j++) | |
856 | { | |
857 | rtx t = cselib_subst_to_values (XVECEXP (x, i, j)); | |
858 | ||
859 | if (t != XVECEXP (x, i, j) && XVEC (x, i) == XVEC (copy, i)) | |
860 | { | |
861 | if (x == copy) | |
862 | copy = shallow_copy_rtx (x); | |
863 | ||
864 | XVEC (copy, i) = rtvec_alloc (XVECLEN (x, i)); | |
865 | for (k = 0; k < j; k++) | |
866 | XVECEXP (copy, i, k) = XVECEXP (x, i, k); | |
867 | } | |
868 | ||
869 | XVECEXP (copy, i, j) = t; | |
870 | } | |
871 | } | |
872 | } | |
873 | ||
874 | return copy; | |
875 | } | |
876 | ||
877 | /* Look up the rtl expression X in our tables and return the value it has. | |
878 | If CREATE is zero, we return NULL if we don't know the value. Otherwise, | |
879 | we create a new one if possible, using mode MODE if X doesn't have a mode | |
880 | (i.e. because it's a constant). */ | |
881 | ||
882 | cselib_val * | |
7080f735 | 883 | cselib_lookup (rtx x, enum machine_mode mode, int create) |
fa49fd0f RK |
884 | { |
885 | void **slot; | |
886 | cselib_val *e; | |
887 | unsigned int hashval; | |
888 | ||
889 | if (GET_MODE (x) != VOIDmode) | |
890 | mode = GET_MODE (x); | |
891 | ||
892 | if (GET_CODE (x) == VALUE) | |
893 | return CSELIB_VAL_PTR (x); | |
894 | ||
f8cfc6aa | 895 | if (REG_P (x)) |
fa49fd0f RK |
896 | { |
897 | struct elt_list *l; | |
898 | unsigned int i = REGNO (x); | |
899 | ||
60fa6660 AO |
900 | l = REG_VALUES (i); |
901 | if (l && l->elt == NULL) | |
902 | l = l->next; | |
903 | for (; l; l = l->next) | |
fa49fd0f RK |
904 | if (mode == GET_MODE (l->elt->u.val_rtx)) |
905 | return l->elt; | |
906 | ||
907 | if (! create) | |
908 | return 0; | |
909 | ||
31825e57 DM |
910 | if (i < FIRST_PSEUDO_REGISTER) |
911 | { | |
66fd46b6 | 912 | unsigned int n = hard_regno_nregs[i][mode]; |
31825e57 DM |
913 | |
914 | if (n > max_value_regs) | |
915 | max_value_regs = n; | |
916 | } | |
917 | ||
fa49fd0f RK |
918 | e = new_cselib_val (++next_unknown_value, GET_MODE (x)); |
919 | e->locs = new_elt_loc_list (e->locs, x); | |
920 | if (REG_VALUES (i) == 0) | |
60fa6660 AO |
921 | { |
922 | /* Maintain the invariant that the first entry of | |
923 | REG_VALUES, if present, must be the value used to set the | |
924 | register, or NULL. */ | |
6790d1ab | 925 | used_regs[n_used_regs++] = i; |
60fa6660 AO |
926 | REG_VALUES (i) = new_elt_list (REG_VALUES (i), NULL); |
927 | } | |
928 | REG_VALUES (i)->next = new_elt_list (REG_VALUES (i)->next, e); | |
fa49fd0f RK |
929 | slot = htab_find_slot_with_hash (hash_table, x, e->value, INSERT); |
930 | *slot = e; | |
931 | return e; | |
932 | } | |
933 | ||
3c0cb5de | 934 | if (MEM_P (x)) |
fa49fd0f RK |
935 | return cselib_lookup_mem (x, create); |
936 | ||
0516f6fe | 937 | hashval = cselib_hash_rtx (x, mode, create); |
fa49fd0f RK |
938 | /* Can't even create if hashing is not possible. */ |
939 | if (! hashval) | |
940 | return 0; | |
941 | ||
942 | slot = htab_find_slot_with_hash (hash_table, wrap_constant (mode, x), | |
943 | hashval, create ? INSERT : NO_INSERT); | |
944 | if (slot == 0) | |
945 | return 0; | |
946 | ||
947 | e = (cselib_val *) *slot; | |
948 | if (e) | |
949 | return e; | |
950 | ||
951 | e = new_cselib_val (hashval, mode); | |
952 | ||
953 | /* We have to fill the slot before calling cselib_subst_to_values: | |
954 | the hash table is inconsistent until we do so, and | |
955 | cselib_subst_to_values will need to do lookups. */ | |
956 | *slot = (void *) e; | |
957 | e->locs = new_elt_loc_list (e->locs, cselib_subst_to_values (x)); | |
958 | return e; | |
959 | } | |
960 | ||
961 | /* Invalidate any entries in reg_values that overlap REGNO. This is called | |
962 | if REGNO is changing. MODE is the mode of the assignment to REGNO, which | |
963 | is used to determine how many hard registers are being changed. If MODE | |
964 | is VOIDmode, then only REGNO is being changed; this is used when | |
965 | invalidating call clobbered registers across a call. */ | |
966 | ||
967 | static void | |
7080f735 | 968 | cselib_invalidate_regno (unsigned int regno, enum machine_mode mode) |
fa49fd0f RK |
969 | { |
970 | unsigned int endregno; | |
971 | unsigned int i; | |
972 | ||
973 | /* If we see pseudos after reload, something is _wrong_. */ | |
341c100f NS |
974 | gcc_assert (!reload_completed || regno < FIRST_PSEUDO_REGISTER |
975 | || reg_renumber[regno] < 0); | |
fa49fd0f RK |
976 | |
977 | /* Determine the range of registers that must be invalidated. For | |
978 | pseudos, only REGNO is affected. For hard regs, we must take MODE | |
979 | into account, and we must also invalidate lower register numbers | |
980 | if they contain values that overlap REGNO. */ | |
291aac59 | 981 | if (regno < FIRST_PSEUDO_REGISTER) |
31825e57 | 982 | { |
341c100f | 983 | gcc_assert (mode != VOIDmode); |
7080f735 | 984 | |
31825e57 DM |
985 | if (regno < max_value_regs) |
986 | i = 0; | |
987 | else | |
988 | i = regno - max_value_regs; | |
fa49fd0f | 989 | |
66fd46b6 | 990 | endregno = regno + hard_regno_nregs[regno][mode]; |
31825e57 DM |
991 | } |
992 | else | |
993 | { | |
994 | i = regno; | |
995 | endregno = regno + 1; | |
996 | } | |
997 | ||
998 | for (; i < endregno; i++) | |
fa49fd0f RK |
999 | { |
1000 | struct elt_list **l = ®_VALUES (i); | |
1001 | ||
1002 | /* Go through all known values for this reg; if it overlaps the range | |
1003 | we're invalidating, remove the value. */ | |
1004 | while (*l) | |
1005 | { | |
1006 | cselib_val *v = (*l)->elt; | |
1007 | struct elt_loc_list **p; | |
1008 | unsigned int this_last = i; | |
1009 | ||
60fa6660 | 1010 | if (i < FIRST_PSEUDO_REGISTER && v != NULL) |
66fd46b6 | 1011 | this_last += hard_regno_nregs[i][GET_MODE (v->u.val_rtx)] - 1; |
fa49fd0f | 1012 | |
60fa6660 | 1013 | if (this_last < regno || v == NULL) |
fa49fd0f RK |
1014 | { |
1015 | l = &(*l)->next; | |
1016 | continue; | |
1017 | } | |
1018 | ||
1019 | /* We have an overlap. */ | |
60fa6660 AO |
1020 | if (*l == REG_VALUES (i)) |
1021 | { | |
1022 | /* Maintain the invariant that the first entry of | |
1023 | REG_VALUES, if present, must be the value used to set | |
1024 | the register, or NULL. This is also nice because | |
1025 | then we won't push the same regno onto user_regs | |
1026 | multiple times. */ | |
1027 | (*l)->elt = NULL; | |
1028 | l = &(*l)->next; | |
1029 | } | |
1030 | else | |
1031 | unchain_one_elt_list (l); | |
fa49fd0f RK |
1032 | |
1033 | /* Now, we clear the mapping from value to reg. It must exist, so | |
1034 | this code will crash intentionally if it doesn't. */ | |
1035 | for (p = &v->locs; ; p = &(*p)->next) | |
1036 | { | |
1037 | rtx x = (*p)->loc; | |
1038 | ||
f8cfc6aa | 1039 | if (REG_P (x) && REGNO (x) == i) |
fa49fd0f RK |
1040 | { |
1041 | unchain_one_elt_loc_list (p); | |
1042 | break; | |
1043 | } | |
1044 | } | |
1045 | if (v->locs == 0) | |
1046 | n_useless_values++; | |
1047 | } | |
1048 | } | |
1049 | } | |
9ddb66ca JH |
1050 | \f |
1051 | /* Return 1 if X has a value that can vary even between two | |
1052 | executions of the program. 0 means X can be compared reliably | |
1053 | against certain constants or near-constants. */ | |
fa49fd0f RK |
1054 | |
1055 | static int | |
9ddb66ca | 1056 | cselib_rtx_varies_p (rtx x ATTRIBUTE_UNUSED, int from_alias ATTRIBUTE_UNUSED) |
fa49fd0f | 1057 | { |
9ddb66ca JH |
1058 | /* We actually don't need to verify very hard. This is because |
1059 | if X has actually changed, we invalidate the memory anyway, | |
1060 | so assume that all common memory addresses are | |
1061 | invariant. */ | |
fa49fd0f RK |
1062 | return 0; |
1063 | } | |
1064 | ||
7101fb18 JH |
1065 | /* Invalidate any locations in the table which are changed because of a |
1066 | store to MEM_RTX. If this is called because of a non-const call | |
1067 | instruction, MEM_RTX is (mem:BLK const0_rtx). */ | |
fa49fd0f | 1068 | |
7101fb18 | 1069 | static void |
7080f735 | 1070 | cselib_invalidate_mem (rtx mem_rtx) |
fa49fd0f | 1071 | { |
7101fb18 | 1072 | cselib_val **vp, *v, *next; |
c65ecebc | 1073 | int num_mems = 0; |
9ddb66ca JH |
1074 | rtx mem_addr; |
1075 | ||
1076 | mem_addr = canon_rtx (get_addr (XEXP (mem_rtx, 0))); | |
1077 | mem_rtx = canon_rtx (mem_rtx); | |
fa49fd0f | 1078 | |
7101fb18 JH |
1079 | vp = &first_containing_mem; |
1080 | for (v = *vp; v != &dummy_val; v = next) | |
fa49fd0f | 1081 | { |
7101fb18 JH |
1082 | bool has_mem = false; |
1083 | struct elt_loc_list **p = &v->locs; | |
1084 | int had_locs = v->locs != 0; | |
fa49fd0f | 1085 | |
7101fb18 | 1086 | while (*p) |
fa49fd0f | 1087 | { |
7101fb18 JH |
1088 | rtx x = (*p)->loc; |
1089 | cselib_val *addr; | |
1090 | struct elt_list **mem_chain; | |
1091 | ||
1092 | /* MEMs may occur in locations only at the top level; below | |
1093 | that every MEM or REG is substituted by its VALUE. */ | |
3c0cb5de | 1094 | if (!MEM_P (x)) |
fa49fd0f | 1095 | { |
7101fb18 JH |
1096 | p = &(*p)->next; |
1097 | continue; | |
1098 | } | |
c65ecebc | 1099 | if (num_mems < PARAM_VALUE (PARAM_MAX_CSELIB_MEMORY_LOCATIONS) |
9ddb66ca JH |
1100 | && ! canon_true_dependence (mem_rtx, GET_MODE (mem_rtx), mem_addr, |
1101 | x, cselib_rtx_varies_p)) | |
7101fb18 JH |
1102 | { |
1103 | has_mem = true; | |
c65ecebc | 1104 | num_mems++; |
7101fb18 JH |
1105 | p = &(*p)->next; |
1106 | continue; | |
fa49fd0f RK |
1107 | } |
1108 | ||
7101fb18 JH |
1109 | /* This one overlaps. */ |
1110 | /* We must have a mapping from this MEM's address to the | |
1111 | value (E). Remove that, too. */ | |
1112 | addr = cselib_lookup (XEXP (x, 0), VOIDmode, 0); | |
1113 | mem_chain = &addr->addr_list; | |
1114 | for (;;) | |
1115 | { | |
1116 | if ((*mem_chain)->elt == v) | |
1117 | { | |
1118 | unchain_one_elt_list (mem_chain); | |
1119 | break; | |
1120 | } | |
fa49fd0f | 1121 | |
7101fb18 JH |
1122 | mem_chain = &(*mem_chain)->next; |
1123 | } | |
fa49fd0f | 1124 | |
7101fb18 JH |
1125 | unchain_one_elt_loc_list (p); |
1126 | } | |
fa49fd0f | 1127 | |
7101fb18 JH |
1128 | if (had_locs && v->locs == 0) |
1129 | n_useless_values++; | |
fa49fd0f | 1130 | |
7101fb18 JH |
1131 | next = v->next_containing_mem; |
1132 | if (has_mem) | |
1133 | { | |
1134 | *vp = v; | |
1135 | vp = &(*vp)->next_containing_mem; | |
1136 | } | |
1137 | else | |
1138 | v->next_containing_mem = NULL; | |
1139 | } | |
1140 | *vp = &dummy_val; | |
fa49fd0f RK |
1141 | } |
1142 | ||
0d87c765 | 1143 | /* Invalidate DEST, which is being assigned to or clobbered. */ |
fa49fd0f | 1144 | |
0d87c765 RH |
1145 | void |
1146 | cselib_invalidate_rtx (rtx dest) | |
fa49fd0f RK |
1147 | { |
1148 | while (GET_CODE (dest) == STRICT_LOW_PART || GET_CODE (dest) == SIGN_EXTRACT | |
1149 | || GET_CODE (dest) == ZERO_EXTRACT || GET_CODE (dest) == SUBREG) | |
1150 | dest = XEXP (dest, 0); | |
1151 | ||
f8cfc6aa | 1152 | if (REG_P (dest)) |
fa49fd0f | 1153 | cselib_invalidate_regno (REGNO (dest), GET_MODE (dest)); |
3c0cb5de | 1154 | else if (MEM_P (dest)) |
fa49fd0f RK |
1155 | cselib_invalidate_mem (dest); |
1156 | ||
1157 | /* Some machines don't define AUTO_INC_DEC, but they still use push | |
1158 | instructions. We need to catch that case here in order to | |
1159 | invalidate the stack pointer correctly. Note that invalidating | |
1160 | the stack pointer is different from invalidating DEST. */ | |
1161 | if (push_operand (dest, GET_MODE (dest))) | |
0d87c765 RH |
1162 | cselib_invalidate_rtx (stack_pointer_rtx); |
1163 | } | |
1164 | ||
1165 | /* A wrapper for cselib_invalidate_rtx to be called via note_stores. */ | |
1166 | ||
1167 | static void | |
1168 | cselib_invalidate_rtx_note_stores (rtx dest, rtx ignore ATTRIBUTE_UNUSED, | |
1169 | void *data ATTRIBUTE_UNUSED) | |
1170 | { | |
1171 | cselib_invalidate_rtx (dest); | |
fa49fd0f RK |
1172 | } |
1173 | ||
1174 | /* Record the result of a SET instruction. DEST is being set; the source | |
1175 | contains the value described by SRC_ELT. If DEST is a MEM, DEST_ADDR_ELT | |
1176 | describes its address. */ | |
1177 | ||
1178 | static void | |
7080f735 | 1179 | cselib_record_set (rtx dest, cselib_val *src_elt, cselib_val *dest_addr_elt) |
fa49fd0f | 1180 | { |
f8cfc6aa | 1181 | int dreg = REG_P (dest) ? (int) REGNO (dest) : -1; |
fa49fd0f RK |
1182 | |
1183 | if (src_elt == 0 || side_effects_p (dest)) | |
1184 | return; | |
1185 | ||
1186 | if (dreg >= 0) | |
1187 | { | |
31825e57 DM |
1188 | if (dreg < FIRST_PSEUDO_REGISTER) |
1189 | { | |
66fd46b6 | 1190 | unsigned int n = hard_regno_nregs[dreg][GET_MODE (dest)]; |
31825e57 DM |
1191 | |
1192 | if (n > max_value_regs) | |
1193 | max_value_regs = n; | |
1194 | } | |
1195 | ||
60fa6660 AO |
1196 | if (REG_VALUES (dreg) == 0) |
1197 | { | |
6790d1ab | 1198 | used_regs[n_used_regs++] = dreg; |
60fa6660 AO |
1199 | REG_VALUES (dreg) = new_elt_list (REG_VALUES (dreg), src_elt); |
1200 | } | |
1201 | else | |
1202 | { | |
341c100f NS |
1203 | /* The register should have been invalidated. */ |
1204 | gcc_assert (REG_VALUES (dreg)->elt == 0); | |
1205 | REG_VALUES (dreg)->elt = src_elt; | |
60fa6660 AO |
1206 | } |
1207 | ||
fa49fd0f RK |
1208 | if (src_elt->locs == 0) |
1209 | n_useless_values--; | |
1210 | src_elt->locs = new_elt_loc_list (src_elt->locs, dest); | |
1211 | } | |
3c0cb5de | 1212 | else if (MEM_P (dest) && dest_addr_elt != 0 |
463301c3 | 1213 | && cselib_record_memory) |
fa49fd0f RK |
1214 | { |
1215 | if (src_elt->locs == 0) | |
1216 | n_useless_values--; | |
1217 | add_mem_for_addr (dest_addr_elt, src_elt, dest); | |
1218 | } | |
1219 | } | |
1220 | ||
1221 | /* Describe a single set that is part of an insn. */ | |
1222 | struct set | |
1223 | { | |
1224 | rtx src; | |
1225 | rtx dest; | |
1226 | cselib_val *src_elt; | |
1227 | cselib_val *dest_addr_elt; | |
1228 | }; | |
1229 | ||
1230 | /* There is no good way to determine how many elements there can be | |
1231 | in a PARALLEL. Since it's fairly cheap, use a really large number. */ | |
1232 | #define MAX_SETS (FIRST_PSEUDO_REGISTER * 2) | |
1233 | ||
1234 | /* Record the effects of any sets in INSN. */ | |
1235 | static void | |
7080f735 | 1236 | cselib_record_sets (rtx insn) |
fa49fd0f RK |
1237 | { |
1238 | int n_sets = 0; | |
1239 | int i; | |
1240 | struct set sets[MAX_SETS]; | |
1241 | rtx body = PATTERN (insn); | |
b7933c21 | 1242 | rtx cond = 0; |
fa49fd0f RK |
1243 | |
1244 | body = PATTERN (insn); | |
b7933c21 BS |
1245 | if (GET_CODE (body) == COND_EXEC) |
1246 | { | |
1247 | cond = COND_EXEC_TEST (body); | |
1248 | body = COND_EXEC_CODE (body); | |
1249 | } | |
1250 | ||
fa49fd0f RK |
1251 | /* Find all sets. */ |
1252 | if (GET_CODE (body) == SET) | |
1253 | { | |
1254 | sets[0].src = SET_SRC (body); | |
1255 | sets[0].dest = SET_DEST (body); | |
1256 | n_sets = 1; | |
1257 | } | |
1258 | else if (GET_CODE (body) == PARALLEL) | |
1259 | { | |
1260 | /* Look through the PARALLEL and record the values being | |
1261 | set, if possible. Also handle any CLOBBERs. */ | |
1262 | for (i = XVECLEN (body, 0) - 1; i >= 0; --i) | |
1263 | { | |
1264 | rtx x = XVECEXP (body, 0, i); | |
1265 | ||
1266 | if (GET_CODE (x) == SET) | |
1267 | { | |
1268 | sets[n_sets].src = SET_SRC (x); | |
1269 | sets[n_sets].dest = SET_DEST (x); | |
1270 | n_sets++; | |
1271 | } | |
1272 | } | |
1273 | } | |
1274 | ||
1275 | /* Look up the values that are read. Do this before invalidating the | |
1276 | locations that are written. */ | |
1277 | for (i = 0; i < n_sets; i++) | |
1278 | { | |
1279 | rtx dest = sets[i].dest; | |
1280 | ||
1281 | /* A STRICT_LOW_PART can be ignored; we'll record the equivalence for | |
1282 | the low part after invalidating any knowledge about larger modes. */ | |
1283 | if (GET_CODE (sets[i].dest) == STRICT_LOW_PART) | |
1284 | sets[i].dest = dest = XEXP (dest, 0); | |
1285 | ||
1286 | /* We don't know how to record anything but REG or MEM. */ | |
f8cfc6aa | 1287 | if (REG_P (dest) |
3c0cb5de | 1288 | || (MEM_P (dest) && cselib_record_memory)) |
fa49fd0f | 1289 | { |
b7933c21 BS |
1290 | rtx src = sets[i].src; |
1291 | if (cond) | |
1292 | src = gen_rtx_IF_THEN_ELSE (GET_MODE (src), cond, src, dest); | |
37060e78 | 1293 | sets[i].src_elt = cselib_lookup (src, GET_MODE (dest), 1); |
3c0cb5de | 1294 | if (MEM_P (dest)) |
fa49fd0f RK |
1295 | sets[i].dest_addr_elt = cselib_lookup (XEXP (dest, 0), Pmode, 1); |
1296 | else | |
1297 | sets[i].dest_addr_elt = 0; | |
1298 | } | |
1299 | } | |
1300 | ||
1301 | /* Invalidate all locations written by this insn. Note that the elts we | |
1302 | looked up in the previous loop aren't affected, just some of their | |
1303 | locations may go away. */ | |
0d87c765 | 1304 | note_stores (body, cselib_invalidate_rtx_note_stores, NULL); |
fa49fd0f | 1305 | |
b7048ab7 RH |
1306 | /* If this is an asm, look for duplicate sets. This can happen when the |
1307 | user uses the same value as an output multiple times. This is valid | |
1308 | if the outputs are not actually used thereafter. Treat this case as | |
1309 | if the value isn't actually set. We do this by smashing the destination | |
1310 | to pc_rtx, so that we won't record the value later. */ | |
1311 | if (n_sets >= 2 && asm_noperands (body) >= 0) | |
1312 | { | |
1313 | for (i = 0; i < n_sets; i++) | |
1314 | { | |
1315 | rtx dest = sets[i].dest; | |
3c0cb5de | 1316 | if (REG_P (dest) || MEM_P (dest)) |
b7048ab7 RH |
1317 | { |
1318 | int j; | |
1319 | for (j = i + 1; j < n_sets; j++) | |
1320 | if (rtx_equal_p (dest, sets[j].dest)) | |
1321 | { | |
1322 | sets[i].dest = pc_rtx; | |
1323 | sets[j].dest = pc_rtx; | |
1324 | } | |
1325 | } | |
1326 | } | |
1327 | } | |
1328 | ||
fa49fd0f RK |
1329 | /* Now enter the equivalences in our tables. */ |
1330 | for (i = 0; i < n_sets; i++) | |
1331 | { | |
1332 | rtx dest = sets[i].dest; | |
f8cfc6aa | 1333 | if (REG_P (dest) |
3c0cb5de | 1334 | || (MEM_P (dest) && cselib_record_memory)) |
fa49fd0f RK |
1335 | cselib_record_set (dest, sets[i].src_elt, sets[i].dest_addr_elt); |
1336 | } | |
1337 | } | |
1338 | ||
1339 | /* Record the effects of INSN. */ | |
1340 | ||
1341 | void | |
7080f735 | 1342 | cselib_process_insn (rtx insn) |
fa49fd0f RK |
1343 | { |
1344 | int i; | |
1345 | rtx x; | |
1346 | ||
9635cfad JH |
1347 | if (find_reg_note (insn, REG_LIBCALL, NULL)) |
1348 | cselib_current_insn_in_libcall = true; | |
fa49fd0f RK |
1349 | cselib_current_insn = insn; |
1350 | ||
1351 | /* Forget everything at a CODE_LABEL, a volatile asm, or a setjmp. */ | |
4b4bf941 JQ |
1352 | if (LABEL_P (insn) |
1353 | || (CALL_P (insn) | |
570a98eb | 1354 | && find_reg_note (insn, REG_SETJMP, NULL)) |
4b4bf941 | 1355 | || (NONJUMP_INSN_P (insn) |
fa49fd0f RK |
1356 | && GET_CODE (PATTERN (insn)) == ASM_OPERANDS |
1357 | && MEM_VOLATILE_P (PATTERN (insn)))) | |
1358 | { | |
5976e643 RS |
1359 | if (find_reg_note (insn, REG_RETVAL, NULL)) |
1360 | cselib_current_insn_in_libcall = false; | |
cd648cec | 1361 | clear_table (); |
fa49fd0f RK |
1362 | return; |
1363 | } | |
1364 | ||
1365 | if (! INSN_P (insn)) | |
1366 | { | |
5976e643 RS |
1367 | if (find_reg_note (insn, REG_RETVAL, NULL)) |
1368 | cselib_current_insn_in_libcall = false; | |
fa49fd0f RK |
1369 | cselib_current_insn = 0; |
1370 | return; | |
1371 | } | |
1372 | ||
1373 | /* If this is a call instruction, forget anything stored in a | |
1374 | call clobbered register, or, if this is not a const call, in | |
1375 | memory. */ | |
4b4bf941 | 1376 | if (CALL_P (insn)) |
fa49fd0f RK |
1377 | { |
1378 | for (i = 0; i < FIRST_PSEUDO_REGISTER; i++) | |
1379 | if (call_used_regs[i]) | |
291aac59 | 1380 | cselib_invalidate_regno (i, reg_raw_mode[i]); |
fa49fd0f | 1381 | |
24a28584 | 1382 | if (! CONST_OR_PURE_CALL_P (insn)) |
fa49fd0f RK |
1383 | cselib_invalidate_mem (callmem); |
1384 | } | |
1385 | ||
1386 | cselib_record_sets (insn); | |
1387 | ||
1388 | #ifdef AUTO_INC_DEC | |
1389 | /* Clobber any registers which appear in REG_INC notes. We | |
1390 | could keep track of the changes to their values, but it is | |
1391 | unlikely to help. */ | |
1392 | for (x = REG_NOTES (insn); x; x = XEXP (x, 1)) | |
1393 | if (REG_NOTE_KIND (x) == REG_INC) | |
0d87c765 | 1394 | cselib_invalidate_rtx (XEXP (x, 0)); |
fa49fd0f RK |
1395 | #endif |
1396 | ||
1397 | /* Look for any CLOBBERs in CALL_INSN_FUNCTION_USAGE, but only | |
1398 | after we have processed the insn. */ | |
4b4bf941 | 1399 | if (CALL_P (insn)) |
fa49fd0f RK |
1400 | for (x = CALL_INSN_FUNCTION_USAGE (insn); x; x = XEXP (x, 1)) |
1401 | if (GET_CODE (XEXP (x, 0)) == CLOBBER) | |
0d87c765 | 1402 | cselib_invalidate_rtx (XEXP (XEXP (x, 0), 0)); |
fa49fd0f | 1403 | |
5976e643 RS |
1404 | if (find_reg_note (insn, REG_RETVAL, NULL)) |
1405 | cselib_current_insn_in_libcall = false; | |
fa49fd0f RK |
1406 | cselib_current_insn = 0; |
1407 | ||
1408 | if (n_useless_values > MAX_USELESS_VALUES) | |
1409 | remove_useless_values (); | |
1410 | } | |
1411 | ||
fa49fd0f RK |
1412 | /* Initialize cselib for one pass. The caller must also call |
1413 | init_alias_analysis. */ | |
1414 | ||
1415 | void | |
463301c3 | 1416 | cselib_init (bool record_memory) |
fa49fd0f | 1417 | { |
6a59927d JH |
1418 | elt_list_pool = create_alloc_pool ("elt_list", |
1419 | sizeof (struct elt_list), 10); | |
1420 | elt_loc_list_pool = create_alloc_pool ("elt_loc_list", | |
1421 | sizeof (struct elt_loc_list), 10); | |
1422 | cselib_val_pool = create_alloc_pool ("cselib_val_list", | |
1423 | sizeof (cselib_val), 10); | |
23bd7a93 JH |
1424 | value_pool = create_alloc_pool ("value", |
1425 | RTX_SIZE (VALUE), 100); | |
463301c3 | 1426 | cselib_record_memory = record_memory; |
e2500fed | 1427 | /* This is only created once. */ |
fa49fd0f | 1428 | if (! callmem) |
e2500fed | 1429 | callmem = gen_rtx_MEM (BLKmode, const0_rtx); |
fa49fd0f RK |
1430 | |
1431 | cselib_nregs = max_reg_num (); | |
6790d1ab JH |
1432 | |
1433 | /* We preserve reg_values to allow expensive clearing of the whole thing. | |
1434 | Reallocate it however if it happens to be too large. */ | |
1435 | if (!reg_values || reg_values_size < cselib_nregs | |
1436 | || (reg_values_size > 10 && reg_values_size > cselib_nregs * 4)) | |
e2500fed | 1437 | { |
6790d1ab JH |
1438 | if (reg_values) |
1439 | free (reg_values); | |
1440 | /* Some space for newly emit instructions so we don't end up | |
1441 | reallocating in between passes. */ | |
1442 | reg_values_size = cselib_nregs + (63 + cselib_nregs) / 16; | |
1443 | reg_values = xcalloc (reg_values_size, sizeof (reg_values)); | |
e2500fed | 1444 | } |
6790d1ab JH |
1445 | used_regs = xmalloc (sizeof (*used_regs) * cselib_nregs); |
1446 | n_used_regs = 0; | |
1447 | hash_table = htab_create (31, get_value_hash, entry_and_rtx_equal_p, NULL); | |
9635cfad | 1448 | cselib_current_insn_in_libcall = false; |
fa49fd0f RK |
1449 | } |
1450 | ||
1451 | /* Called when the current user is done with cselib. */ | |
1452 | ||
1453 | void | |
7080f735 | 1454 | cselib_finish (void) |
fa49fd0f | 1455 | { |
6a59927d JH |
1456 | free_alloc_pool (elt_list_pool); |
1457 | free_alloc_pool (elt_loc_list_pool); | |
1458 | free_alloc_pool (cselib_val_pool); | |
23bd7a93 | 1459 | free_alloc_pool (value_pool); |
cd648cec | 1460 | clear_table (); |
6790d1ab | 1461 | htab_delete (hash_table); |
0fc0c4c9 | 1462 | free (used_regs); |
e2500fed GK |
1463 | used_regs = 0; |
1464 | hash_table = 0; | |
1465 | n_useless_values = 0; | |
1466 | next_unknown_value = 0; | |
fa49fd0f | 1467 | } |
e2500fed GK |
1468 | |
1469 | #include "gt-cselib.h" |