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