]>
Commit | Line | Data |
---|---|---|
e45425ec | 1 | /* Global constant/copy propagation for RTL. |
7adcbafe | 2 | Copyright (C) 1997-2022 Free Software Foundation, Inc. |
e45425ec SB |
3 | |
4 | This file is part of GCC. | |
5 | ||
6 | GCC is free software; you can redistribute it and/or modify it under | |
7 | the terms of the GNU General Public License as published by the Free | |
8 | Software Foundation; either version 3, or (at your option) any later | |
9 | version. | |
10 | ||
11 | GCC is distributed in the hope that it will be useful, but WITHOUT ANY | |
12 | WARRANTY; without even the implied warranty of MERCHANTABILITY or | |
13 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
14 | for more details. | |
15 | ||
16 | You should have received a copy of the GNU General Public License | |
17 | along with GCC; see the file COPYING3. If not see | |
18 | <http://www.gnu.org/licenses/>. */ | |
19 | ||
20 | #include "config.h" | |
21 | #include "system.h" | |
22 | #include "coretypes.h" | |
c7131fb2 | 23 | #include "backend.h" |
c7131fb2 | 24 | #include "rtl.h" |
957060b5 | 25 | #include "cfghooks.h" |
c7131fb2 | 26 | #include "df.h" |
957060b5 | 27 | #include "insn-config.h" |
4d0cdd0c | 28 | #include "memmodel.h" |
957060b5 AM |
29 | #include "emit-rtl.h" |
30 | #include "recog.h" | |
e45425ec SB |
31 | #include "diagnostic-core.h" |
32 | #include "toplev.h" | |
60393bbc AM |
33 | #include "cfgrtl.h" |
34 | #include "cfganal.h" | |
35 | #include "lcm.h" | |
36 | #include "cfgcleanup.h" | |
e45425ec SB |
37 | #include "cselib.h" |
38 | #include "intl.h" | |
e45425ec | 39 | #include "tree-pass.h" |
e45425ec | 40 | #include "dbgcnt.h" |
7d776ee2 | 41 | #include "cfgloop.h" |
87d8dd6c | 42 | #include "gcse.h" |
e45425ec SB |
43 | |
44 | \f | |
45 | /* An obstack for our working variables. */ | |
3084ce69 | 46 | static struct obstack cprop_obstack; |
e45425ec | 47 | |
e45425ec SB |
48 | /* Occurrence of an expression. |
49 | There is one per basic block. If a pattern appears more than once the | |
50 | last appearance is used. */ | |
51 | ||
11478306 | 52 | struct cprop_occr |
e45425ec SB |
53 | { |
54 | /* Next occurrence of this expression. */ | |
11478306 | 55 | struct cprop_occr *next; |
e45425ec | 56 | /* The insn that computes the expression. */ |
997c5639 | 57 | rtx_insn *insn; |
e45425ec SB |
58 | }; |
59 | ||
ba5c3b24 | 60 | /* Hash table entry for assignment expressions. */ |
b77f9eab | 61 | |
11478306 | 62 | struct cprop_expr |
b77f9eab SB |
63 | { |
64 | /* The expression (DEST := SRC). */ | |
65 | rtx dest; | |
66 | rtx src; | |
67 | ||
68 | /* Index in the available expression bitmaps. */ | |
69 | int bitmap_index; | |
70 | /* Next entry with the same hash. */ | |
11478306 | 71 | struct cprop_expr *next_same_hash; |
b77f9eab SB |
72 | /* List of available occurrence in basic blocks in the function. |
73 | An "available occurrence" is one that is the last occurrence in the | |
ba5c3b24 EB |
74 | basic block and whose operands are not modified by following statements |
75 | in the basic block [including this insn]. */ | |
11478306 | 76 | struct cprop_occr *avail_occr; |
b77f9eab SB |
77 | }; |
78 | ||
79 | /* Hash table for copy propagation expressions. | |
e45425ec SB |
80 | Each hash table is an array of buckets. |
81 | ??? It is known that if it were an array of entries, structure elements | |
82 | `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is | |
83 | not clear whether in the final analysis a sufficient amount of memory would | |
84 | be saved as the size of the available expression bitmaps would be larger | |
85 | [one could build a mapping table without holes afterwards though]. | |
86 | Someday I'll perform the computation and figure it out. */ | |
87 | ||
88 | struct hash_table_d | |
89 | { | |
90 | /* The table itself. | |
91 | This is an array of `set_hash_table_size' elements. */ | |
11478306 | 92 | struct cprop_expr **table; |
e45425ec SB |
93 | |
94 | /* Size of the hash table, in elements. */ | |
95 | unsigned int size; | |
96 | ||
97 | /* Number of hash table elements. */ | |
98 | unsigned int n_elems; | |
99 | }; | |
100 | ||
101 | /* Copy propagation hash table. */ | |
102 | static struct hash_table_d set_hash_table; | |
103 | ||
104 | /* Array of implicit set patterns indexed by basic block index. */ | |
105 | static rtx *implicit_sets; | |
106 | ||
cfb99521 BC |
107 | /* Array of indexes of expressions for implicit set patterns indexed by basic |
108 | block index. In other words, implicit_set_indexes[i] is the bitmap_index | |
109 | of the expression whose RTX is implicit_sets[i]. */ | |
110 | static int *implicit_set_indexes; | |
111 | ||
e45425ec SB |
112 | /* Bitmap containing one bit for each register in the program. |
113 | Used when performing GCSE to track which registers have been set since | |
7d11cebe | 114 | the start or end of the basic block while traversing that block. */ |
e45425ec SB |
115 | static regset reg_set_bitmap; |
116 | ||
117 | /* Various variables for statistics gathering. */ | |
118 | ||
119 | /* Memory used in a pass. | |
120 | This isn't intended to be absolutely precise. Its intent is only | |
121 | to keep an eye on memory usage. */ | |
122 | static int bytes_used; | |
123 | ||
124 | /* Number of local constants propagated. */ | |
125 | static int local_const_prop_count; | |
126 | /* Number of local copies propagated. */ | |
127 | static int local_copy_prop_count; | |
128 | /* Number of global constants propagated. */ | |
129 | static int global_const_prop_count; | |
130 | /* Number of global copies propagated. */ | |
131 | static int global_copy_prop_count; | |
e45425ec | 132 | |
3084ce69 SB |
133 | #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T))) |
134 | #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S))) | |
e45425ec SB |
135 | |
136 | /* Cover function to obstack_alloc. */ | |
137 | ||
138 | static void * | |
3084ce69 | 139 | cprop_alloc (unsigned long size) |
e45425ec SB |
140 | { |
141 | bytes_used += size; | |
3084ce69 | 142 | return obstack_alloc (&cprop_obstack, size); |
e45425ec SB |
143 | } |
144 | \f | |
7d11cebe SB |
145 | /* Return nonzero if register X is unchanged from INSN to the end |
146 | of INSN's basic block. */ | |
e45425ec SB |
147 | |
148 | static int | |
997c5639 | 149 | reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED) |
e45425ec | 150 | { |
7d11cebe | 151 | return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x)); |
e45425ec SB |
152 | } |
153 | ||
154 | /* Hash a set of register REGNO. | |
155 | ||
156 | Sets are hashed on the register that is set. This simplifies the PRE copy | |
157 | propagation code. | |
158 | ||
159 | ??? May need to make things more elaborate. Later, as necessary. */ | |
160 | ||
161 | static unsigned int | |
6e2830c3 | 162 | hash_mod (int regno, int hash_table_size) |
e45425ec | 163 | { |
f460c170 | 164 | return (unsigned) regno % hash_table_size; |
e45425ec SB |
165 | } |
166 | ||
b77f9eab SB |
167 | /* Insert assignment DEST:=SET from INSN in the hash table. |
168 | DEST is a register and SET is a register or a suitable constant. | |
169 | If the assignment is already present in the table, record it as | |
cfb99521 BC |
170 | the last occurrence in INSN's basic block. |
171 | IMPLICIT is true if it's an implicit set, false otherwise. */ | |
e45425ec SB |
172 | |
173 | static void | |
997c5639 DM |
174 | insert_set_in_table (rtx dest, rtx src, rtx_insn *insn, |
175 | struct hash_table_d *table, bool implicit) | |
e45425ec | 176 | { |
b77f9eab | 177 | bool found = false; |
e45425ec | 178 | unsigned int hash; |
11478306 JH |
179 | struct cprop_expr *cur_expr, *last_expr = NULL; |
180 | struct cprop_occr *cur_occr; | |
e45425ec | 181 | |
6e2830c3 | 182 | hash = hash_mod (REGNO (dest), table->size); |
e45425ec | 183 | |
b77f9eab SB |
184 | for (cur_expr = table->table[hash]; cur_expr; |
185 | cur_expr = cur_expr->next_same_hash) | |
e45425ec | 186 | { |
b77f9eab SB |
187 | if (dest == cur_expr->dest |
188 | && src == cur_expr->src) | |
189 | { | |
190 | found = true; | |
191 | break; | |
192 | } | |
e45425ec | 193 | last_expr = cur_expr; |
e45425ec SB |
194 | } |
195 | ||
196 | if (! found) | |
197 | { | |
11478306 JH |
198 | cur_expr = GOBNEW (struct cprop_expr); |
199 | bytes_used += sizeof (struct cprop_expr); | |
e45425ec SB |
200 | if (table->table[hash] == NULL) |
201 | /* This is the first pattern that hashed to this index. */ | |
202 | table->table[hash] = cur_expr; | |
203 | else | |
204 | /* Add EXPR to end of this hash chain. */ | |
205 | last_expr->next_same_hash = cur_expr; | |
206 | ||
207 | /* Set the fields of the expr element. | |
208 | We must copy X because it can be modified when copy propagation is | |
209 | performed on its operands. */ | |
b77f9eab SB |
210 | cur_expr->dest = copy_rtx (dest); |
211 | cur_expr->src = copy_rtx (src); | |
e45425ec SB |
212 | cur_expr->bitmap_index = table->n_elems++; |
213 | cur_expr->next_same_hash = NULL; | |
214 | cur_expr->avail_occr = NULL; | |
215 | } | |
216 | ||
217 | /* Now record the occurrence. */ | |
218 | cur_occr = cur_expr->avail_occr; | |
219 | ||
220 | if (cur_occr | |
221 | && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn)) | |
222 | { | |
223 | /* Found another instance of the expression in the same basic block. | |
224 | Prefer this occurrence to the currently recorded one. We want | |
225 | the last one in the block and the block is scanned from start | |
226 | to end. */ | |
227 | cur_occr->insn = insn; | |
228 | } | |
229 | else | |
230 | { | |
231 | /* First occurrence of this expression in this basic block. */ | |
11478306 JH |
232 | cur_occr = GOBNEW (struct cprop_occr); |
233 | bytes_used += sizeof (struct cprop_occr); | |
e45425ec SB |
234 | cur_occr->insn = insn; |
235 | cur_occr->next = cur_expr->avail_occr; | |
236 | cur_expr->avail_occr = cur_occr; | |
237 | } | |
cfb99521 BC |
238 | |
239 | /* Record bitmap_index of the implicit set in implicit_set_indexes. */ | |
240 | if (implicit) | |
c3284718 RS |
241 | implicit_set_indexes[BLOCK_FOR_INSN (insn)->index] |
242 | = cur_expr->bitmap_index; | |
e45425ec SB |
243 | } |
244 | ||
7d11cebe SB |
245 | /* Determine whether the rtx X should be treated as a constant for CPROP. |
246 | Since X might be inserted more than once we have to take care that it | |
247 | is sharable. */ | |
e45425ec SB |
248 | |
249 | static bool | |
3084ce69 | 250 | cprop_constant_p (const_rtx x) |
e45425ec | 251 | { |
e45425ec SB |
252 | return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x)); |
253 | } | |
254 | ||
8fceae61 TP |
255 | /* Determine whether the rtx X should be treated as a register that can |
256 | be propagated. Any pseudo-register is fine. */ | |
257 | ||
258 | static bool | |
259 | cprop_reg_p (const_rtx x) | |
260 | { | |
261 | return REG_P (x) && !HARD_REGISTER_P (x); | |
262 | } | |
263 | ||
cfb99521 BC |
264 | /* Scan SET present in INSN and add an entry to the hash TABLE. |
265 | IMPLICIT is true if it's an implicit set, false otherwise. */ | |
e45425ec SB |
266 | |
267 | static void | |
997c5639 DM |
268 | hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table, |
269 | bool implicit) | |
e45425ec | 270 | { |
ba5c3b24 EB |
271 | rtx src = SET_SRC (set); |
272 | rtx dest = SET_DEST (set); | |
e45425ec | 273 | |
8fceae61 | 274 | if (cprop_reg_p (dest) |
7d11cebe SB |
275 | && reg_available_p (dest, insn) |
276 | && can_copy_p (GET_MODE (dest))) | |
e45425ec | 277 | { |
e45425ec SB |
278 | /* See if a REG_EQUAL note shows this equivalent to a simpler expression. |
279 | ||
7d11cebe | 280 | This allows us to do a single CPROP pass and still eliminate |
e45425ec SB |
281 | redundant constants, addresses or other expressions that are |
282 | constructed with multiple instructions. | |
283 | ||
284 | However, keep the original SRC if INSN is a simple reg-reg move. In | |
285 | In this case, there will almost always be a REG_EQUAL note on the | |
286 | insn that sets SRC. By recording the REG_EQUAL value here as SRC | |
7d11cebe | 287 | for INSN, we miss copy propagation opportunities. |
e45425ec SB |
288 | |
289 | Note that this does not impede profitable constant propagations. We | |
b77f9eab | 290 | "look through" reg-reg sets in lookup_set. */ |
7d11cebe | 291 | rtx note = find_reg_equal_equiv_note (insn); |
e45425ec SB |
292 | if (note != 0 |
293 | && REG_NOTE_KIND (note) == REG_EQUAL | |
294 | && !REG_P (src) | |
3084ce69 | 295 | && cprop_constant_p (XEXP (note, 0))) |
f7df4a84 | 296 | src = XEXP (note, 0), set = gen_rtx_SET (dest, src); |
e45425ec SB |
297 | |
298 | /* Record sets for constant/copy propagation. */ | |
8fceae61 | 299 | if ((cprop_reg_p (src) |
7d11cebe | 300 | && src != dest |
7d11cebe | 301 | && reg_available_p (src, insn)) |
3084ce69 | 302 | || cprop_constant_p (src)) |
cfb99521 | 303 | insert_set_in_table (dest, src, insn, table, implicit); |
e45425ec SB |
304 | } |
305 | } | |
306 | ||
ba5c3b24 | 307 | /* Process INSN and add hash table entries as appropriate. */ |
e45425ec SB |
308 | |
309 | static void | |
997c5639 | 310 | hash_scan_insn (rtx_insn *insn, struct hash_table_d *table) |
e45425ec SB |
311 | { |
312 | rtx pat = PATTERN (insn); | |
313 | int i; | |
314 | ||
315 | /* Pick out the sets of INSN and for other forms of instructions record | |
316 | what's been modified. */ | |
317 | ||
318 | if (GET_CODE (pat) == SET) | |
cfb99521 | 319 | hash_scan_set (pat, insn, table, false); |
e45425ec SB |
320 | else if (GET_CODE (pat) == PARALLEL) |
321 | for (i = 0; i < XVECLEN (pat, 0); i++) | |
322 | { | |
323 | rtx x = XVECEXP (pat, 0, i); | |
324 | ||
325 | if (GET_CODE (x) == SET) | |
cfb99521 | 326 | hash_scan_set (x, insn, table, false); |
e45425ec SB |
327 | } |
328 | } | |
329 | ||
ba5c3b24 EB |
330 | /* Dump the hash table TABLE to file FILE under the name NAME. */ |
331 | ||
e45425ec SB |
332 | static void |
333 | dump_hash_table (FILE *file, const char *name, struct hash_table_d *table) | |
334 | { | |
335 | int i; | |
336 | /* Flattened out table, so it's printed in proper order. */ | |
11478306 | 337 | struct cprop_expr **flat_table; |
e45425ec | 338 | unsigned int *hash_val; |
11478306 | 339 | struct cprop_expr *expr; |
e45425ec | 340 | |
11478306 | 341 | flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems); |
e45425ec SB |
342 | hash_val = XNEWVEC (unsigned int, table->n_elems); |
343 | ||
344 | for (i = 0; i < (int) table->size; i++) | |
345 | for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash) | |
346 | { | |
347 | flat_table[expr->bitmap_index] = expr; | |
348 | hash_val[expr->bitmap_index] = i; | |
349 | } | |
350 | ||
351 | fprintf (file, "%s hash table (%d buckets, %d entries)\n", | |
352 | name, table->size, table->n_elems); | |
353 | ||
354 | for (i = 0; i < (int) table->n_elems; i++) | |
355 | if (flat_table[i] != 0) | |
356 | { | |
357 | expr = flat_table[i]; | |
358 | fprintf (file, "Index %d (hash value %d)\n ", | |
359 | expr->bitmap_index, hash_val[i]); | |
b77f9eab SB |
360 | print_rtl (file, expr->dest); |
361 | fprintf (file, " := "); | |
362 | print_rtl (file, expr->src); | |
e45425ec SB |
363 | fprintf (file, "\n"); |
364 | } | |
365 | ||
366 | fprintf (file, "\n"); | |
367 | ||
368 | free (flat_table); | |
369 | free (hash_val); | |
370 | } | |
371 | ||
7d11cebe | 372 | /* Record as unavailable all registers that are DEF operands of INSN. */ |
ba5c3b24 | 373 | |
e45425ec | 374 | static void |
997c5639 | 375 | make_set_regs_unavailable (rtx_insn *insn) |
e45425ec | 376 | { |
bfac633a | 377 | df_ref def; |
e45425ec | 378 | |
bfac633a RS |
379 | FOR_EACH_INSN_DEF (def, insn) |
380 | SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def)); | |
e45425ec SB |
381 | } |
382 | ||
ba5c3b24 | 383 | /* Top level function to create an assignment hash table. |
e45425ec SB |
384 | |
385 | Assignment entries are placed in the hash table if | |
386 | - they are of the form (set (pseudo-reg) src), | |
387 | - src is something we want to perform const/copy propagation on, | |
388 | - none of the operands or target are subsequently modified in the block | |
389 | ||
390 | Currently src must be a pseudo-reg or a const_int. | |
391 | ||
392 | TABLE is the table computed. */ | |
393 | ||
394 | static void | |
395 | compute_hash_table_work (struct hash_table_d *table) | |
396 | { | |
7d11cebe | 397 | basic_block bb; |
e45425ec | 398 | |
3084ce69 SB |
399 | /* Allocate vars to track sets of regs. */ |
400 | reg_set_bitmap = ALLOC_REG_SET (NULL); | |
401 | ||
11cd3bed | 402 | FOR_EACH_BB_FN (bb, cfun) |
e45425ec | 403 | { |
997c5639 | 404 | rtx_insn *insn; |
e45425ec | 405 | |
7d11cebe SB |
406 | /* Reset tables used to keep track of what's not yet invalid [since |
407 | the end of the block]. */ | |
408 | CLEAR_REG_SET (reg_set_bitmap); | |
409 | ||
410 | /* Go over all insns from the last to the first. This is convenient | |
411 | for tracking available registers, i.e. not set between INSN and | |
412 | the end of the basic block BB. */ | |
413 | FOR_BB_INSNS_REVERSE (bb, insn) | |
414 | { | |
415 | /* Only real insns are interesting. */ | |
e45425ec SB |
416 | if (!NONDEBUG_INSN_P (insn)) |
417 | continue; | |
418 | ||
7d11cebe SB |
419 | /* Record interesting sets from INSN in the hash table. */ |
420 | hash_scan_insn (insn, table); | |
e45425ec | 421 | |
7d11cebe SB |
422 | /* Any registers set in INSN will make SETs above it not AVAIL. */ |
423 | make_set_regs_unavailable (insn); | |
e45425ec SB |
424 | } |
425 | ||
7d11cebe SB |
426 | /* Insert implicit sets in the hash table, pretending they appear as |
427 | insns at the head of the basic block. */ | |
428 | if (implicit_sets[bb->index] != NULL_RTX) | |
cfb99521 | 429 | hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true); |
e45425ec | 430 | } |
3084ce69 SB |
431 | |
432 | FREE_REG_SET (reg_set_bitmap); | |
e45425ec SB |
433 | } |
434 | ||
435 | /* Allocate space for the set/expr hash TABLE. | |
436 | It is used to determine the number of buckets to use. */ | |
437 | ||
438 | static void | |
439 | alloc_hash_table (struct hash_table_d *table) | |
440 | { | |
441 | int n; | |
442 | ||
443 | n = get_max_insn_count (); | |
444 | ||
445 | table->size = n / 4; | |
446 | if (table->size < 11) | |
447 | table->size = 11; | |
448 | ||
449 | /* Attempt to maintain efficient use of hash table. | |
450 | Making it an odd number is simplest for now. | |
451 | ??? Later take some measurements. */ | |
452 | table->size |= 1; | |
11478306 JH |
453 | n = table->size * sizeof (struct cprop_expr *); |
454 | table->table = XNEWVAR (struct cprop_expr *, n); | |
e45425ec SB |
455 | } |
456 | ||
457 | /* Free things allocated by alloc_hash_table. */ | |
458 | ||
459 | static void | |
460 | free_hash_table (struct hash_table_d *table) | |
461 | { | |
462 | free (table->table); | |
463 | } | |
464 | ||
465 | /* Compute the hash TABLE for doing copy/const propagation or | |
466 | expression hash table. */ | |
467 | ||
468 | static void | |
469 | compute_hash_table (struct hash_table_d *table) | |
470 | { | |
471 | /* Initialize count of number of entries in hash table. */ | |
472 | table->n_elems = 0; | |
11478306 | 473 | memset (table->table, 0, table->size * sizeof (struct cprop_expr *)); |
e45425ec SB |
474 | |
475 | compute_hash_table_work (table); | |
476 | } | |
477 | \f | |
478 | /* Expression tracking support. */ | |
479 | ||
480 | /* Lookup REGNO in the set TABLE. The result is a pointer to the | |
481 | table entry, or NULL if not found. */ | |
482 | ||
11478306 | 483 | static struct cprop_expr * |
e45425ec SB |
484 | lookup_set (unsigned int regno, struct hash_table_d *table) |
485 | { | |
6e2830c3 | 486 | unsigned int hash = hash_mod (regno, table->size); |
11478306 | 487 | struct cprop_expr *expr; |
e45425ec SB |
488 | |
489 | expr = table->table[hash]; | |
490 | ||
b77f9eab | 491 | while (expr && REGNO (expr->dest) != regno) |
e45425ec SB |
492 | expr = expr->next_same_hash; |
493 | ||
494 | return expr; | |
495 | } | |
496 | ||
497 | /* Return the next entry for REGNO in list EXPR. */ | |
498 | ||
11478306 JH |
499 | static struct cprop_expr * |
500 | next_set (unsigned int regno, struct cprop_expr *expr) | |
e45425ec SB |
501 | { |
502 | do | |
503 | expr = expr->next_same_hash; | |
b77f9eab | 504 | while (expr && REGNO (expr->dest) != regno); |
e45425ec SB |
505 | |
506 | return expr; | |
507 | } | |
508 | ||
509 | /* Reset tables used to keep track of what's still available [since the | |
510 | start of the block]. */ | |
511 | ||
512 | static void | |
513 | reset_opr_set_tables (void) | |
514 | { | |
515 | /* Maintain a bitmap of which regs have been set since beginning of | |
516 | the block. */ | |
517 | CLEAR_REG_SET (reg_set_bitmap); | |
518 | } | |
519 | ||
196565d4 SB |
520 | /* Return nonzero if the register X has not been set yet [since the |
521 | start of the basic block containing INSN]. */ | |
e45425ec SB |
522 | |
523 | static int | |
997c5639 | 524 | reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED) |
e45425ec | 525 | { |
196565d4 | 526 | return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x)); |
e45425ec SB |
527 | } |
528 | ||
529 | /* Record things set by INSN. | |
196565d4 | 530 | This data is used by reg_not_set_p. */ |
e45425ec SB |
531 | |
532 | static void | |
997c5639 | 533 | mark_oprs_set (rtx_insn *insn) |
e45425ec | 534 | { |
bfac633a | 535 | df_ref def; |
e45425ec | 536 | |
bfac633a RS |
537 | FOR_EACH_INSN_DEF (def, insn) |
538 | SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def)); | |
e45425ec | 539 | } |
e45425ec SB |
540 | \f |
541 | /* Compute copy/constant propagation working variables. */ | |
542 | ||
543 | /* Local properties of assignments. */ | |
ba5c3b24 EB |
544 | static sbitmap *cprop_avloc; |
545 | static sbitmap *cprop_kill; | |
e45425ec SB |
546 | |
547 | /* Global properties of assignments (computed from the local properties). */ | |
548 | static sbitmap *cprop_avin; | |
549 | static sbitmap *cprop_avout; | |
550 | ||
551 | /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of | |
552 | basic blocks. N_SETS is the number of sets. */ | |
553 | ||
554 | static void | |
555 | alloc_cprop_mem (int n_blocks, int n_sets) | |
556 | { | |
ba5c3b24 EB |
557 | cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets); |
558 | cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets); | |
e45425ec SB |
559 | |
560 | cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets); | |
561 | cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets); | |
562 | } | |
563 | ||
564 | /* Free vars used by copy/const propagation. */ | |
565 | ||
566 | static void | |
567 | free_cprop_mem (void) | |
568 | { | |
ba5c3b24 EB |
569 | sbitmap_vector_free (cprop_avloc); |
570 | sbitmap_vector_free (cprop_kill); | |
e45425ec SB |
571 | sbitmap_vector_free (cprop_avin); |
572 | sbitmap_vector_free (cprop_avout); | |
573 | } | |
574 | ||
e45425ec SB |
575 | /* Compute the local properties of each recorded expression. |
576 | ||
577 | Local properties are those that are defined by the block, irrespective of | |
578 | other blocks. | |
579 | ||
ba5c3b24 EB |
580 | An expression is killed in a block if its operands, either DEST or SRC, are |
581 | modified in the block. | |
e45425ec SB |
582 | |
583 | An expression is computed (locally available) in a block if it is computed | |
584 | at least once and expression would contain the same value if the | |
585 | computation was moved to the end of the block. | |
586 | ||
ba5c3b24 | 587 | KILL and COMP are destination sbitmaps for recording local properties. */ |
e45425ec SB |
588 | |
589 | static void | |
ba5c3b24 | 590 | compute_local_properties (sbitmap *kill, sbitmap *comp, |
e45425ec SB |
591 | struct hash_table_d *table) |
592 | { | |
593 | unsigned int i; | |
594 | ||
b77f9eab | 595 | /* Initialize the bitmaps that were passed in. */ |
8b1c6fd7 DM |
596 | bitmap_vector_clear (kill, last_basic_block_for_fn (cfun)); |
597 | bitmap_vector_clear (comp, last_basic_block_for_fn (cfun)); | |
e45425ec SB |
598 | |
599 | for (i = 0; i < table->size; i++) | |
600 | { | |
11478306 | 601 | struct cprop_expr *expr; |
e45425ec SB |
602 | |
603 | for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash) | |
604 | { | |
605 | int indx = expr->bitmap_index; | |
b77f9eab | 606 | df_ref def; |
11478306 | 607 | struct cprop_occr *occr; |
e45425ec | 608 | |
ba5c3b24 EB |
609 | /* For each definition of the destination pseudo-reg, the expression |
610 | is killed in the block where the definition is. */ | |
b77f9eab SB |
611 | for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest)); |
612 | def; def = DF_REF_NEXT_REG (def)) | |
d7c028c0 | 613 | bitmap_set_bit (kill[DF_REF_BB (def)->index], indx); |
ba5c3b24 EB |
614 | |
615 | /* If the source is a pseudo-reg, for each definition of the source, | |
616 | the expression is killed in the block where the definition is. */ | |
b77f9eab SB |
617 | if (REG_P (expr->src)) |
618 | for (def = DF_REG_DEF_CHAIN (REGNO (expr->src)); | |
619 | def; def = DF_REF_NEXT_REG (def)) | |
d7c028c0 | 620 | bitmap_set_bit (kill[DF_REF_BB (def)->index], indx); |
e45425ec SB |
621 | |
622 | /* The occurrences recorded in avail_occr are exactly those that | |
ba5c3b24 | 623 | are locally available in the block where they are. */ |
b77f9eab SB |
624 | for (occr = expr->avail_occr; occr != NULL; occr = occr->next) |
625 | { | |
d7c028c0 | 626 | bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx); |
b77f9eab | 627 | } |
e45425ec SB |
628 | } |
629 | } | |
630 | } | |
631 | \f | |
632 | /* Hash table support. */ | |
633 | ||
634 | /* Top level routine to do the dataflow analysis needed by copy/const | |
635 | propagation. */ | |
636 | ||
637 | static void | |
638 | compute_cprop_data (void) | |
639 | { | |
cfb99521 BC |
640 | basic_block bb; |
641 | ||
ba5c3b24 EB |
642 | compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table); |
643 | compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin); | |
cfb99521 BC |
644 | |
645 | /* Merge implicit sets into CPROP_AVIN. They are always available at the | |
646 | entry of their basic block. We need to do this because 1) implicit sets | |
647 | aren't recorded for the local pass so they cannot be propagated within | |
648 | their basic block by this pass and 2) the global pass would otherwise | |
649 | propagate them only in the successors of their basic block. */ | |
11cd3bed | 650 | FOR_EACH_BB_FN (bb, cfun) |
cfb99521 BC |
651 | { |
652 | int index = implicit_set_indexes[bb->index]; | |
653 | if (index != -1) | |
d7c028c0 | 654 | bitmap_set_bit (cprop_avin[bb->index], index); |
cfb99521 | 655 | } |
e45425ec SB |
656 | } |
657 | \f | |
658 | /* Copy/constant propagation. */ | |
659 | ||
660 | /* Maximum number of register uses in an insn that we handle. */ | |
661 | #define MAX_USES 8 | |
662 | ||
c1e2610e | 663 | /* Table of uses (registers, both hard and pseudo) found in an insn. |
e45425ec | 664 | Allocated statically to avoid alloc/free complexity and overhead. */ |
c1e2610e | 665 | static rtx reg_use_table[MAX_USES]; |
e45425ec SB |
666 | |
667 | /* Index into `reg_use_table' while building it. */ | |
c1e2610e | 668 | static unsigned reg_use_count; |
e45425ec SB |
669 | |
670 | /* Set up a list of register numbers used in INSN. The found uses are stored | |
671 | in `reg_use_table'. `reg_use_count' is initialized to zero before entry, | |
672 | and contains the number of uses in the table upon exit. | |
673 | ||
674 | ??? If a register appears multiple times we will record it multiple times. | |
675 | This doesn't hurt anything but it will slow things down. */ | |
676 | ||
677 | static void | |
678 | find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED) | |
679 | { | |
680 | int i, j; | |
681 | enum rtx_code code; | |
682 | const char *fmt; | |
683 | rtx x = *xptr; | |
684 | ||
685 | /* repeat is used to turn tail-recursion into iteration since GCC | |
686 | can't do it when there's no return value. */ | |
687 | repeat: | |
688 | if (x == 0) | |
689 | return; | |
690 | ||
691 | code = GET_CODE (x); | |
692 | if (REG_P (x)) | |
693 | { | |
694 | if (reg_use_count == MAX_USES) | |
695 | return; | |
696 | ||
c1e2610e | 697 | reg_use_table[reg_use_count] = x; |
e45425ec SB |
698 | reg_use_count++; |
699 | } | |
700 | ||
701 | /* Recursively scan the operands of this expression. */ | |
702 | ||
703 | for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--) | |
704 | { | |
705 | if (fmt[i] == 'e') | |
706 | { | |
707 | /* If we are about to do the last recursive call | |
708 | needed at this level, change it into iteration. | |
709 | This function is called enough to be worth it. */ | |
710 | if (i == 0) | |
711 | { | |
712 | x = XEXP (x, 0); | |
713 | goto repeat; | |
714 | } | |
715 | ||
716 | find_used_regs (&XEXP (x, i), data); | |
717 | } | |
718 | else if (fmt[i] == 'E') | |
719 | for (j = 0; j < XVECLEN (x, i); j++) | |
720 | find_used_regs (&XVECEXP (x, i, j), data); | |
721 | } | |
722 | } | |
723 | ||
8eee318f GJL |
724 | /* Try to replace all uses of FROM in INSN with TO. |
725 | Return nonzero if successful. */ | |
e45425ec SB |
726 | |
727 | static int | |
997c5639 | 728 | try_replace_reg (rtx from, rtx to, rtx_insn *insn) |
e45425ec SB |
729 | { |
730 | rtx note = find_reg_equal_equiv_note (insn); | |
731 | rtx src = 0; | |
732 | int success = 0; | |
733 | rtx set = single_set (insn); | |
734 | ||
32e0a7d9 KV |
735 | bool check_rtx_costs = true; |
736 | bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn)); | |
737 | int old_cost = set ? set_rtx_cost (set, speed) : 0; | |
738 | ||
e548c9df AM |
739 | if (!set |
740 | || CONSTANT_P (SET_SRC (set)) | |
741 | || (note != 0 | |
742 | && REG_NOTE_KIND (note) == REG_EQUAL | |
743 | && (GET_CODE (XEXP (note, 0)) == CONST | |
744 | || CONSTANT_P (XEXP (note, 0))))) | |
32e0a7d9 KV |
745 | check_rtx_costs = false; |
746 | ||
e45425ec SB |
747 | /* Usually we substitute easy stuff, so we won't copy everything. |
748 | We however need to take care to not duplicate non-trivial CONST | |
749 | expressions. */ | |
750 | to = copy_rtx (to); | |
751 | ||
752 | validate_replace_src_group (from, to, insn); | |
32e0a7d9 KV |
753 | |
754 | /* If TO is a constant, check the cost of the set after propagation | |
755 | to the cost of the set before the propagation. If the cost is | |
756 | higher, then do not replace FROM with TO. */ | |
757 | ||
758 | if (check_rtx_costs | |
759 | && CONSTANT_P (to) | |
e548c9df | 760 | && set_rtx_cost (set, speed) > old_cost) |
32e0a7d9 KV |
761 | { |
762 | cancel_changes (0); | |
763 | return false; | |
764 | } | |
765 | ||
766 | ||
e45425ec SB |
767 | if (num_changes_pending () && apply_change_group ()) |
768 | success = 1; | |
769 | ||
770 | /* Try to simplify SET_SRC if we have substituted a constant. */ | |
771 | if (success && set && CONSTANT_P (to)) | |
772 | { | |
773 | src = simplify_rtx (SET_SRC (set)); | |
774 | ||
775 | if (src) | |
776 | validate_change (insn, &SET_SRC (set), src, 0); | |
777 | } | |
778 | ||
779 | /* If there is already a REG_EQUAL note, update the expression in it | |
780 | with our replacement. */ | |
781 | if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL) | |
782 | set_unique_reg_note (insn, REG_EQUAL, | |
783 | simplify_replace_rtx (XEXP (note, 0), from, to)); | |
784 | if (!success && set && reg_mentioned_p (from, SET_SRC (set))) | |
785 | { | |
ba5c3b24 EB |
786 | /* If above failed and this is a single set, try to simplify the source |
787 | of the set given our substitution. We could perhaps try this for | |
788 | multiple SETs, but it probably won't buy us anything. */ | |
e45425ec SB |
789 | src = simplify_replace_rtx (SET_SRC (set), from, to); |
790 | ||
791 | if (!rtx_equal_p (src, SET_SRC (set)) | |
792 | && validate_change (insn, &SET_SRC (set), src, 0)) | |
793 | success = 1; | |
794 | ||
795 | /* If we've failed perform the replacement, have a single SET to | |
796 | a REG destination and don't yet have a note, add a REG_EQUAL note | |
797 | to not lose information. */ | |
798 | if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set))) | |
799 | note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src)); | |
800 | } | |
801 | ||
8eee318f GJL |
802 | if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set))) |
803 | { | |
804 | /* Registers can also appear as uses in SET_DEST if it is a MEM. | |
805 | We could perhaps try this for multiple SETs, but it probably | |
806 | won't buy us anything. */ | |
807 | rtx dest = simplify_replace_rtx (SET_DEST (set), from, to); | |
ba5c3b24 | 808 | |
8eee318f GJL |
809 | if (!rtx_equal_p (dest, SET_DEST (set)) |
810 | && validate_change (insn, &SET_DEST (set), dest, 0)) | |
811 | success = 1; | |
812 | } | |
813 | ||
e45425ec SB |
814 | /* REG_EQUAL may get simplified into register. |
815 | We don't allow that. Remove that note. This code ought | |
816 | not to happen, because previous code ought to synthesize | |
817 | reg-reg move, but be on the safe side. */ | |
818 | if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0))) | |
819 | remove_note (insn, note); | |
820 | ||
821 | return success; | |
822 | } | |
823 | ||
8fceae61 TP |
824 | /* Find a set of REGNOs that are available on entry to INSN's block. If found, |
825 | SET_RET[0] will be assigned a set with a register source and SET_RET[1] a | |
826 | set with a constant source. If not found the corresponding entry is set to | |
827 | NULL. */ | |
e45425ec | 828 | |
8fceae61 TP |
829 | static void |
830 | find_avail_set (int regno, rtx_insn *insn, struct cprop_expr *set_ret[2]) | |
e45425ec | 831 | { |
8fceae61 | 832 | set_ret[0] = set_ret[1] = NULL; |
e45425ec SB |
833 | |
834 | /* Loops are not possible here. To get a loop we would need two sets | |
835 | available at the start of the block containing INSN. i.e. we would | |
836 | need two sets like this available at the start of the block: | |
837 | ||
838 | (set (reg X) (reg Y)) | |
839 | (set (reg Y) (reg X)) | |
840 | ||
67914693 | 841 | This cannot happen since the set of (reg Y) would have killed the |
e45425ec SB |
842 | set of (reg X) making it unavailable at the start of this block. */ |
843 | while (1) | |
844 | { | |
845 | rtx src; | |
11478306 | 846 | struct cprop_expr *set = lookup_set (regno, &set_hash_table); |
e45425ec SB |
847 | |
848 | /* Find a set that is available at the start of the block | |
849 | which contains INSN. */ | |
850 | while (set) | |
851 | { | |
d7c028c0 | 852 | if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index], |
e45425ec SB |
853 | set->bitmap_index)) |
854 | break; | |
855 | set = next_set (regno, set); | |
856 | } | |
857 | ||
858 | /* If no available set was found we've reached the end of the | |
859 | (possibly empty) copy chain. */ | |
860 | if (set == 0) | |
861 | break; | |
862 | ||
b77f9eab | 863 | src = set->src; |
e45425ec SB |
864 | |
865 | /* We know the set is available. | |
866 | Now check that SRC is locally anticipatable (i.e. none of the | |
867 | source operands have changed since the start of the block). | |
868 | ||
869 | If the source operand changed, we may still use it for the next | |
870 | iteration of this loop, but we may not use it for substitutions. */ | |
871 | ||
8fceae61 TP |
872 | if (cprop_constant_p (src)) |
873 | set_ret[1] = set; | |
874 | else if (reg_not_set_p (src, insn)) | |
875 | set_ret[0] = set; | |
e45425ec SB |
876 | |
877 | /* If the source of the set is anything except a register, then | |
878 | we have reached the end of the copy chain. */ | |
879 | if (! REG_P (src)) | |
880 | break; | |
881 | ||
882 | /* Follow the copy chain, i.e. start another iteration of the loop | |
883 | and see if we have an available copy into SRC. */ | |
884 | regno = REGNO (src); | |
885 | } | |
e45425ec SB |
886 | } |
887 | ||
888 | /* Subroutine of cprop_insn that tries to propagate constants into | |
889 | JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL | |
890 | it is the instruction that immediately precedes JUMP, and must be a | |
891 | single SET of a register. FROM is what we will try to replace, | |
ba5c3b24 | 892 | SRC is the constant we will try to substitute for it. Return nonzero |
e45425ec SB |
893 | if a change was made. */ |
894 | ||
895 | static int | |
997c5639 | 896 | cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src) |
e45425ec SB |
897 | { |
898 | rtx new_rtx, set_src, note_src; | |
899 | rtx set = pc_set (jump); | |
900 | rtx note = find_reg_equal_equiv_note (jump); | |
901 | ||
902 | if (note) | |
903 | { | |
904 | note_src = XEXP (note, 0); | |
905 | if (GET_CODE (note_src) == EXPR_LIST) | |
906 | note_src = NULL_RTX; | |
907 | } | |
908 | else note_src = NULL_RTX; | |
909 | ||
910 | /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */ | |
911 | set_src = note_src ? note_src : SET_SRC (set); | |
912 | ||
913 | /* First substitute the SETCC condition into the JUMP instruction, | |
914 | then substitute that given values into this expanded JUMP. */ | |
915 | if (setcc != NULL_RTX | |
916 | && !modified_between_p (from, setcc, jump) | |
917 | && !modified_between_p (src, setcc, jump)) | |
918 | { | |
919 | rtx setcc_src; | |
920 | rtx setcc_set = single_set (setcc); | |
921 | rtx setcc_note = find_reg_equal_equiv_note (setcc); | |
922 | setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST) | |
923 | ? XEXP (setcc_note, 0) : SET_SRC (setcc_set); | |
924 | set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set), | |
925 | setcc_src); | |
926 | } | |
927 | else | |
997c5639 | 928 | setcc = NULL; |
e45425ec SB |
929 | |
930 | new_rtx = simplify_replace_rtx (set_src, from, src); | |
931 | ||
932 | /* If no simplification can be made, then try the next register. */ | |
933 | if (rtx_equal_p (new_rtx, SET_SRC (set))) | |
934 | return 0; | |
935 | ||
936 | /* If this is now a no-op delete it, otherwise this must be a valid insn. */ | |
937 | if (new_rtx == pc_rtx) | |
938 | delete_insn (jump); | |
939 | else | |
940 | { | |
941 | /* Ensure the value computed inside the jump insn to be equivalent | |
942 | to one computed by setcc. */ | |
943 | if (setcc && modified_in_p (new_rtx, setcc)) | |
944 | return 0; | |
945 | if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0)) | |
946 | { | |
947 | /* When (some) constants are not valid in a comparison, and there | |
948 | are two registers to be replaced by constants before the entire | |
949 | comparison can be folded into a constant, we need to keep | |
950 | intermediate information in REG_EQUAL notes. For targets with | |
951 | separate compare insns, such notes are added by try_replace_reg. | |
952 | When we have a combined compare-and-branch instruction, however, | |
953 | we need to attach a note to the branch itself to make this | |
954 | optimization work. */ | |
955 | ||
956 | if (!rtx_equal_p (new_rtx, note_src)) | |
957 | set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx)); | |
958 | return 0; | |
959 | } | |
960 | ||
961 | /* Remove REG_EQUAL note after simplification. */ | |
962 | if (note_src) | |
963 | remove_note (jump, note); | |
964 | } | |
965 | ||
e45425ec SB |
966 | global_const_prop_count++; |
967 | if (dump_file != NULL) | |
968 | { | |
969 | fprintf (dump_file, | |
aa326bfb | 970 | "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with " |
ba5c3b24 | 971 | "constant ", REGNO (from), INSN_UID (jump)); |
e45425ec SB |
972 | print_rtl (dump_file, src); |
973 | fprintf (dump_file, "\n"); | |
974 | } | |
975 | purge_dead_edges (bb); | |
976 | ||
977 | /* If a conditional jump has been changed into unconditional jump, remove | |
978 | the jump and make the edge fallthru - this is always called in | |
979 | cfglayout mode. */ | |
980 | if (new_rtx != pc_rtx && simplejump_p (jump)) | |
981 | { | |
982 | edge e; | |
983 | edge_iterator ei; | |
984 | ||
196565d4 | 985 | FOR_EACH_EDGE (e, ei, bb->succs) |
fefa31b5 | 986 | if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun) |
e45425ec SB |
987 | && BB_HEAD (e->dest) == JUMP_LABEL (jump)) |
988 | { | |
989 | e->flags |= EDGE_FALLTHRU; | |
990 | break; | |
991 | } | |
992 | delete_insn (jump); | |
993 | } | |
994 | ||
995 | return 1; | |
996 | } | |
997 | ||
ba5c3b24 EB |
998 | /* Subroutine of cprop_insn that tries to propagate constants. FROM is what |
999 | we will try to replace, SRC is the constant we will try to substitute for | |
1000 | it and INSN is the instruction where this will be happening. */ | |
1001 | ||
1002 | static int | |
997c5639 | 1003 | constprop_register (rtx from, rtx src, rtx_insn *insn) |
e45425ec SB |
1004 | { |
1005 | rtx sset; | |
022f6ee3 | 1006 | rtx_insn *next_insn; |
e45425ec | 1007 | |
bd1cd0d0 SB |
1008 | /* Check for reg setting instructions followed by conditional branch |
1009 | instructions first. */ | |
e45425ec | 1010 | if ((sset = single_set (insn)) != NULL |
022f6ee3 JJ |
1011 | && (next_insn = next_nondebug_insn (insn)) != NULL |
1012 | && any_condjump_p (next_insn) | |
1013 | && onlyjump_p (next_insn)) | |
e45425ec SB |
1014 | { |
1015 | rtx dest = SET_DEST (sset); | |
bd1cd0d0 | 1016 | if (REG_P (dest) |
022f6ee3 | 1017 | && cprop_jump (BLOCK_FOR_INSN (insn), insn, next_insn, |
ba5c3b24 | 1018 | from, src)) |
e45425ec SB |
1019 | return 1; |
1020 | } | |
1021 | ||
1022 | /* Handle normal insns next. */ | |
ba5c3b24 | 1023 | if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn)) |
e45425ec SB |
1024 | return 1; |
1025 | ||
1026 | /* Try to propagate a CONST_INT into a conditional jump. | |
1027 | We're pretty specific about what we will handle in this | |
1028 | code, we can extend this as necessary over time. | |
1029 | ||
1030 | Right now the insn in question must look like | |
1031 | (set (pc) (if_then_else ...)) */ | |
1032 | else if (any_condjump_p (insn) && onlyjump_p (insn)) | |
ba5c3b24 | 1033 | return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src); |
e45425ec SB |
1034 | return 0; |
1035 | } | |
1036 | ||
1037 | /* Perform constant and copy propagation on INSN. | |
ba5c3b24 | 1038 | Return nonzero if a change was made. */ |
e45425ec SB |
1039 | |
1040 | static int | |
997c5639 | 1041 | cprop_insn (rtx_insn *insn) |
e45425ec | 1042 | { |
c1e2610e SB |
1043 | unsigned i; |
1044 | int changed = 0, changed_this_round; | |
e45425ec SB |
1045 | rtx note; |
1046 | ||
8fceae61 | 1047 | do |
e45425ec | 1048 | { |
8fceae61 TP |
1049 | changed_this_round = 0; |
1050 | reg_use_count = 0; | |
1051 | note_uses (&PATTERN (insn), find_used_regs, NULL); | |
e45425ec | 1052 | |
8fceae61 TP |
1053 | /* We may win even when propagating constants into notes. */ |
1054 | note = find_reg_equal_equiv_note (insn); | |
1055 | if (note) | |
1056 | find_used_regs (&XEXP (note, 0), NULL); | |
e45425ec | 1057 | |
8fceae61 | 1058 | for (i = 0; i < reg_use_count; i++) |
e45425ec | 1059 | { |
8fceae61 TP |
1060 | rtx reg_used = reg_use_table[i]; |
1061 | unsigned int regno = REGNO (reg_used); | |
1062 | rtx src_cst = NULL, src_reg = NULL; | |
1063 | struct cprop_expr *set[2]; | |
1064 | ||
1065 | /* If the register has already been set in this block, there's | |
1066 | nothing we can do. */ | |
1067 | if (! reg_not_set_p (reg_used, insn)) | |
1068 | continue; | |
1069 | ||
1070 | /* Find an assignment that sets reg_used and is available | |
1071 | at the start of the block. */ | |
1072 | find_avail_set (regno, insn, set); | |
1073 | if (set[0]) | |
1074 | src_reg = set[0]->src; | |
1075 | if (set[1]) | |
1076 | src_cst = set[1]->src; | |
1077 | ||
1078 | /* Constant propagation. */ | |
1079 | if (src_cst && cprop_constant_p (src_cst) | |
1080 | && constprop_register (reg_used, src_cst, insn)) | |
e45425ec | 1081 | { |
c1e2610e | 1082 | changed_this_round = changed = 1; |
e45425ec SB |
1083 | global_const_prop_count++; |
1084 | if (dump_file != NULL) | |
1085 | { | |
ba5c3b24 EB |
1086 | fprintf (dump_file, |
1087 | "GLOBAL CONST-PROP: Replacing reg %d in ", regno); | |
1088 | fprintf (dump_file, "insn %d with constant ", | |
1089 | INSN_UID (insn)); | |
8fceae61 | 1090 | print_rtl (dump_file, src_cst); |
e45425ec SB |
1091 | fprintf (dump_file, "\n"); |
1092 | } | |
4654c0cf | 1093 | if (insn->deleted ()) |
e45425ec SB |
1094 | return 1; |
1095 | } | |
8fceae61 TP |
1096 | /* Copy propagation. */ |
1097 | else if (src_reg && cprop_reg_p (src_reg) | |
1098 | && REGNO (src_reg) != regno | |
1099 | && try_replace_reg (reg_used, src_reg, insn)) | |
e45425ec | 1100 | { |
c1e2610e | 1101 | changed_this_round = changed = 1; |
e45425ec SB |
1102 | global_copy_prop_count++; |
1103 | if (dump_file != NULL) | |
1104 | { | |
ba5c3b24 EB |
1105 | fprintf (dump_file, |
1106 | "GLOBAL COPY-PROP: Replacing reg %d in insn %d", | |
e45425ec | 1107 | regno, INSN_UID (insn)); |
8fceae61 | 1108 | fprintf (dump_file, " with reg %d\n", REGNO (src_reg)); |
e45425ec SB |
1109 | } |
1110 | ||
1111 | /* The original insn setting reg_used may or may not now be | |
b77f9eab | 1112 | deletable. We leave the deletion to DCE. */ |
e45425ec SB |
1113 | /* FIXME: If it turns out that the insn isn't deletable, |
1114 | then we may have unnecessarily extended register lifetimes | |
1115 | and made things worse. */ | |
1116 | } | |
1117 | } | |
1118 | } | |
8fceae61 TP |
1119 | /* If try_replace_reg simplified the insn, the regs found by find_used_regs |
1120 | may not be valid anymore. Start over. */ | |
1121 | while (changed_this_round); | |
e45425ec SB |
1122 | |
1123 | if (changed && DEBUG_INSN_P (insn)) | |
1124 | return 0; | |
1125 | ||
1126 | return changed; | |
1127 | } | |
1128 | ||
1129 | /* Like find_used_regs, but avoid recording uses that appear in | |
1130 | input-output contexts such as zero_extract or pre_dec. This | |
1131 | restricts the cases we consider to those for which local cprop | |
1132 | can legitimately make replacements. */ | |
1133 | ||
1134 | static void | |
1135 | local_cprop_find_used_regs (rtx *xptr, void *data) | |
1136 | { | |
1137 | rtx x = *xptr; | |
1138 | ||
1139 | if (x == 0) | |
1140 | return; | |
1141 | ||
1142 | switch (GET_CODE (x)) | |
1143 | { | |
1144 | case ZERO_EXTRACT: | |
1145 | case SIGN_EXTRACT: | |
1146 | case STRICT_LOW_PART: | |
1147 | return; | |
1148 | ||
1149 | case PRE_DEC: | |
1150 | case PRE_INC: | |
1151 | case POST_DEC: | |
1152 | case POST_INC: | |
1153 | case PRE_MODIFY: | |
1154 | case POST_MODIFY: | |
1155 | /* Can only legitimately appear this early in the context of | |
1156 | stack pushes for function arguments, but handle all of the | |
1157 | codes nonetheless. */ | |
1158 | return; | |
1159 | ||
1160 | case SUBREG: | |
33845ca9 | 1161 | if (read_modify_subreg_p (x)) |
e45425ec SB |
1162 | return; |
1163 | break; | |
1164 | ||
1165 | default: | |
1166 | break; | |
1167 | } | |
1168 | ||
1169 | find_used_regs (xptr, data); | |
1170 | } | |
1171 | ||
1172 | /* Try to perform local const/copy propagation on X in INSN. */ | |
1173 | ||
1174 | static bool | |
997c5639 | 1175 | do_local_cprop (rtx x, rtx_insn *insn) |
e45425ec SB |
1176 | { |
1177 | rtx newreg = NULL, newcnst = NULL; | |
1178 | ||
1179 | /* Rule out USE instructions and ASM statements as we don't want to | |
aed38127 | 1180 | change the hard registers mentioned. */ |
e45425ec | 1181 | if (REG_P (x) |
8fceae61 | 1182 | && (cprop_reg_p (x) |
e45425ec SB |
1183 | || (GET_CODE (PATTERN (insn)) != USE |
1184 | && asm_noperands (PATTERN (insn)) < 0))) | |
1185 | { | |
1186 | cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode); | |
1187 | struct elt_loc_list *l; | |
1188 | ||
1189 | if (!val) | |
1190 | return false; | |
1191 | for (l = val->locs; l; l = l->next) | |
1192 | { | |
1193 | rtx this_rtx = l->loc; | |
1194 | rtx note; | |
1195 | ||
3084ce69 | 1196 | if (cprop_constant_p (this_rtx)) |
e45425ec | 1197 | newcnst = this_rtx; |
8fceae61 | 1198 | if (cprop_reg_p (this_rtx) |
e45425ec SB |
1199 | /* Don't copy propagate if it has attached REG_EQUIV note. |
1200 | At this point this only function parameters should have | |
1201 | REG_EQUIV notes and if the argument slot is used somewhere | |
1202 | explicitly, it means address of parameter has been taken, | |
1203 | so we should not extend the lifetime of the pseudo. */ | |
1204 | && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX)) | |
1205 | || ! MEM_P (XEXP (note, 0)))) | |
1206 | newreg = this_rtx; | |
1207 | } | |
ba5c3b24 | 1208 | if (newcnst && constprop_register (x, newcnst, insn)) |
e45425ec SB |
1209 | { |
1210 | if (dump_file != NULL) | |
1211 | { | |
1212 | fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ", | |
1213 | REGNO (x)); | |
1214 | fprintf (dump_file, "insn %d with constant ", | |
1215 | INSN_UID (insn)); | |
1216 | print_rtl (dump_file, newcnst); | |
1217 | fprintf (dump_file, "\n"); | |
1218 | } | |
1219 | local_const_prop_count++; | |
1220 | return true; | |
1221 | } | |
1222 | else if (newreg && newreg != x && try_replace_reg (x, newreg, insn)) | |
1223 | { | |
1224 | if (dump_file != NULL) | |
1225 | { | |
1226 | fprintf (dump_file, | |
1227 | "LOCAL COPY-PROP: Replacing reg %d in insn %d", | |
1228 | REGNO (x), INSN_UID (insn)); | |
1229 | fprintf (dump_file, " with reg %d\n", REGNO (newreg)); | |
1230 | } | |
1231 | local_copy_prop_count++; | |
1232 | return true; | |
1233 | } | |
1234 | } | |
1235 | return false; | |
1236 | } | |
1237 | ||
1238 | /* Do local const/copy propagation (i.e. within each basic block). */ | |
1239 | ||
1240 | static int | |
1241 | local_cprop_pass (void) | |
1242 | { | |
1243 | basic_block bb; | |
997c5639 | 1244 | rtx_insn *insn; |
e45425ec | 1245 | bool changed = false; |
c1e2610e | 1246 | unsigned i; |
e45425ec | 1247 | |
ea9f867b BS |
1248 | auto_vec<rtx_insn *> uncond_traps; |
1249 | ||
e45425ec | 1250 | cselib_init (0); |
11cd3bed | 1251 | FOR_EACH_BB_FN (bb, cfun) |
e45425ec SB |
1252 | { |
1253 | FOR_BB_INSNS (bb, insn) | |
1254 | { | |
1255 | if (INSN_P (insn)) | |
1256 | { | |
ea9f867b BS |
1257 | bool was_uncond_trap |
1258 | = (GET_CODE (PATTERN (insn)) == TRAP_IF | |
1259 | && XEXP (PATTERN (insn), 0) == const1_rtx); | |
e45425ec SB |
1260 | rtx note = find_reg_equal_equiv_note (insn); |
1261 | do | |
1262 | { | |
1263 | reg_use_count = 0; | |
1264 | note_uses (&PATTERN (insn), local_cprop_find_used_regs, | |
1265 | NULL); | |
1266 | if (note) | |
1267 | local_cprop_find_used_regs (&XEXP (note, 0), NULL); | |
1268 | ||
c1e2610e | 1269 | for (i = 0; i < reg_use_count; i++) |
e45425ec | 1270 | { |
c1e2610e | 1271 | if (do_local_cprop (reg_use_table[i], insn)) |
e45425ec | 1272 | { |
3598cabd AO |
1273 | if (!DEBUG_INSN_P (insn)) |
1274 | changed = true; | |
e45425ec SB |
1275 | break; |
1276 | } | |
1277 | } | |
ea9f867b BS |
1278 | if (!was_uncond_trap |
1279 | && GET_CODE (PATTERN (insn)) == TRAP_IF | |
1280 | && XEXP (PATTERN (insn), 0) == const1_rtx) | |
1281 | { | |
1282 | uncond_traps.safe_push (insn); | |
1283 | break; | |
1284 | } | |
4654c0cf | 1285 | if (insn->deleted ()) |
e45425ec SB |
1286 | break; |
1287 | } | |
c1e2610e | 1288 | while (i < reg_use_count); |
e45425ec SB |
1289 | } |
1290 | cselib_process_insn (insn); | |
1291 | } | |
1292 | ||
1293 | /* Forget everything at the end of a basic block. */ | |
1294 | cselib_clear_table (); | |
1295 | } | |
1296 | ||
1297 | cselib_finish (); | |
1298 | ||
ea9f867b BS |
1299 | while (!uncond_traps.is_empty ()) |
1300 | { | |
1301 | rtx_insn *insn = uncond_traps.pop (); | |
1302 | basic_block to_split = BLOCK_FOR_INSN (insn); | |
1303 | remove_edge (split_block (to_split, insn)); | |
1304 | emit_barrier_after_bb (to_split); | |
1305 | } | |
1306 | ||
e45425ec SB |
1307 | return changed; |
1308 | } | |
1309 | ||
1310 | /* Similar to get_condition, only the resulting condition must be | |
1311 | valid at JUMP, instead of at EARLIEST. | |
1312 | ||
e53b6e56 | 1313 | This differs from noce_get_condition in ifcvt.cc in that we prefer not to |
e45425ec SB |
1314 | settle for the condition variable in the jump instruction being integral. |
1315 | We prefer to be able to record the value of a user variable, rather than | |
1316 | the value of a temporary used in a condition. This could be solved by | |
1317 | recording the value of *every* register scanned by canonicalize_condition, | |
1318 | but this would require some code reorganization. */ | |
1319 | ||
1320 | rtx | |
f90af2e0 | 1321 | fis_get_condition (rtx_insn *jump) |
e45425ec SB |
1322 | { |
1323 | return get_condition (jump, NULL, false, true); | |
1324 | } | |
1325 | ||
384d7a55 SB |
1326 | /* Check the comparison COND to see if we can safely form an implicit |
1327 | set from it. */ | |
e45425ec SB |
1328 | |
1329 | static bool | |
1330 | implicit_set_cond_p (const_rtx cond) | |
1331 | { | |
ef4bddc2 | 1332 | machine_mode mode; |
384d7a55 SB |
1333 | rtx cst; |
1334 | ||
1335 | /* COND must be either an EQ or NE comparison. */ | |
1336 | if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE) | |
1337 | return false; | |
1338 | ||
8fceae61 TP |
1339 | /* The first operand of COND must be a register we can propagate. */ |
1340 | if (!cprop_reg_p (XEXP (cond, 0))) | |
384d7a55 SB |
1341 | return false; |
1342 | ||
1343 | /* The second operand of COND must be a suitable constant. */ | |
1344 | mode = GET_MODE (XEXP (cond, 0)); | |
1345 | cst = XEXP (cond, 1); | |
e45425ec SB |
1346 | |
1347 | /* We can't perform this optimization if either operand might be or might | |
1348 | contain a signed zero. */ | |
1349 | if (HONOR_SIGNED_ZEROS (mode)) | |
1350 | { | |
1351 | /* It is sufficient to check if CST is or contains a zero. We must | |
1352 | handle float, complex, and vector. If any subpart is a zero, then | |
1353 | the optimization can't be performed. */ | |
1354 | /* ??? The complex and vector checks are not implemented yet. We just | |
1355 | always return zero for them. */ | |
34a72c33 RS |
1356 | if (CONST_DOUBLE_AS_FLOAT_P (cst) |
1357 | && real_equal (CONST_DOUBLE_REAL_VALUE (cst), &dconst0)) | |
1358 | return 0; | |
e45425ec SB |
1359 | else |
1360 | return 0; | |
1361 | } | |
1362 | ||
3084ce69 | 1363 | return cprop_constant_p (cst); |
e45425ec SB |
1364 | } |
1365 | ||
1366 | /* Find the implicit sets of a function. An "implicit set" is a constraint | |
1367 | on the value of a variable, implied by a conditional jump. For example, | |
1368 | following "if (x == 2)", the then branch may be optimized as though the | |
1369 | conditional performed an "explicit set", in this example, "x = 2". This | |
1370 | function records the set patterns that are implicit at the start of each | |
1371 | basic block. | |
1372 | ||
384d7a55 SB |
1373 | If an implicit set is found but the set is implicit on a critical edge, |
1374 | this critical edge is split. | |
e45425ec | 1375 | |
384d7a55 SB |
1376 | Return true if the CFG was modified, false otherwise. */ |
1377 | ||
1378 | static bool | |
e45425ec SB |
1379 | find_implicit_sets (void) |
1380 | { | |
1381 | basic_block bb, dest; | |
e45425ec | 1382 | rtx cond, new_rtx; |
384d7a55 SB |
1383 | unsigned int count = 0; |
1384 | bool edges_split = false; | |
8b1c6fd7 | 1385 | size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10; |
384d7a55 SB |
1386 | |
1387 | implicit_sets = XCNEWVEC (rtx, implicit_sets_size); | |
e45425ec | 1388 | |
11cd3bed | 1389 | FOR_EACH_BB_FN (bb, cfun) |
384d7a55 SB |
1390 | { |
1391 | /* Check for more than one successor. */ | |
10b75750 | 1392 | if (EDGE_COUNT (bb->succs) <= 1) |
384d7a55 | 1393 | continue; |
e45425ec | 1394 | |
384d7a55 | 1395 | cond = fis_get_condition (BB_END (bb)); |
e45425ec | 1396 | |
384d7a55 SB |
1397 | /* If no condition is found or if it isn't of a suitable form, |
1398 | ignore it. */ | |
1399 | if (! cond || ! implicit_set_cond_p (cond)) | |
1400 | continue; | |
1401 | ||
1402 | dest = GET_CODE (cond) == EQ | |
1403 | ? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest; | |
1404 | ||
1405 | /* If DEST doesn't go anywhere, ignore it. */ | |
fefa31b5 | 1406 | if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
384d7a55 SB |
1407 | continue; |
1408 | ||
1409 | /* We have found a suitable implicit set. Try to record it now as | |
1410 | a SET in DEST. If DEST has more than one predecessor, the edge | |
1411 | between BB and DEST is a critical edge and we must split it, | |
1412 | because we can only record one implicit set per DEST basic block. */ | |
1413 | if (! single_pred_p (dest)) | |
1414 | { | |
1415 | dest = split_edge (find_edge (bb, dest)); | |
1416 | edges_split = true; | |
1417 | } | |
1418 | ||
1419 | if (implicit_sets_size <= (size_t) dest->index) | |
1420 | { | |
1421 | size_t old_implicit_sets_size = implicit_sets_size; | |
1422 | implicit_sets_size *= 2; | |
1423 | implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size); | |
1424 | memset (implicit_sets + old_implicit_sets_size, 0, | |
1425 | (implicit_sets_size - old_implicit_sets_size) * sizeof (rtx)); | |
e45425ec SB |
1426 | } |
1427 | ||
f7df4a84 | 1428 | new_rtx = gen_rtx_SET (XEXP (cond, 0), XEXP (cond, 1)); |
384d7a55 SB |
1429 | implicit_sets[dest->index] = new_rtx; |
1430 | if (dump_file) | |
1431 | { | |
c3284718 RS |
1432 | fprintf (dump_file, "Implicit set of reg %d in ", |
1433 | REGNO (XEXP (cond, 0))); | |
1434 | fprintf (dump_file, "basic block %d\n", dest->index); | |
384d7a55 SB |
1435 | } |
1436 | count++; | |
1437 | } | |
1438 | ||
e45425ec SB |
1439 | if (dump_file) |
1440 | fprintf (dump_file, "Found %d implicit sets\n", count); | |
384d7a55 SB |
1441 | |
1442 | /* Confess our sins. */ | |
1443 | return edges_split; | |
e45425ec SB |
1444 | } |
1445 | ||
1446 | /* Bypass conditional jumps. */ | |
1447 | ||
1448 | /* The value of last_basic_block at the beginning of the jump_bypass | |
1449 | pass. The use of redirect_edge_and_branch_force may introduce new | |
1450 | basic blocks, but the data flow analysis is only valid for basic | |
1451 | block indices less than bypass_last_basic_block. */ | |
1452 | ||
1453 | static int bypass_last_basic_block; | |
1454 | ||
1455 | /* Find a set of REGNO to a constant that is available at the end of basic | |
ba5c3b24 | 1456 | block BB. Return NULL if no such set is found. Based heavily upon |
e45425ec SB |
1457 | find_avail_set. */ |
1458 | ||
11478306 | 1459 | static struct cprop_expr * |
e45425ec SB |
1460 | find_bypass_set (int regno, int bb) |
1461 | { | |
11478306 | 1462 | struct cprop_expr *result = 0; |
e45425ec SB |
1463 | |
1464 | for (;;) | |
1465 | { | |
1466 | rtx src; | |
11478306 | 1467 | struct cprop_expr *set = lookup_set (regno, &set_hash_table); |
e45425ec SB |
1468 | |
1469 | while (set) | |
1470 | { | |
d7c028c0 | 1471 | if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index)) |
e45425ec SB |
1472 | break; |
1473 | set = next_set (regno, set); | |
1474 | } | |
1475 | ||
1476 | if (set == 0) | |
1477 | break; | |
1478 | ||
b77f9eab | 1479 | src = set->src; |
3084ce69 | 1480 | if (cprop_constant_p (src)) |
e45425ec SB |
1481 | result = set; |
1482 | ||
1483 | if (! REG_P (src)) | |
1484 | break; | |
1485 | ||
1486 | regno = REGNO (src); | |
1487 | } | |
1488 | return result; | |
1489 | } | |
1490 | ||
e45425ec SB |
1491 | /* Subroutine of bypass_block that checks whether a pseudo is killed by |
1492 | any of the instructions inserted on an edge. Jump bypassing places | |
1493 | condition code setters on CFG edges using insert_insn_on_edge. This | |
1494 | function is required to check that our data flow analysis is still | |
1495 | valid prior to commit_edge_insertions. */ | |
1496 | ||
1497 | static bool | |
1498 | reg_killed_on_edge (const_rtx reg, const_edge e) | |
1499 | { | |
3ffa95c2 | 1500 | rtx_insn *insn; |
e45425ec SB |
1501 | |
1502 | for (insn = e->insns.r; insn; insn = NEXT_INSN (insn)) | |
1503 | if (INSN_P (insn) && reg_set_p (reg, insn)) | |
1504 | return true; | |
1505 | ||
1506 | return false; | |
1507 | } | |
1508 | ||
1509 | /* Subroutine of bypass_conditional_jumps that attempts to bypass the given | |
1510 | basic block BB which has more than one predecessor. If not NULL, SETCC | |
1511 | is the first instruction of BB, which is immediately followed by JUMP_INSN | |
1512 | JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB. | |
1513 | Returns nonzero if a change was made. | |
1514 | ||
1515 | During the jump bypassing pass, we may place copies of SETCC instructions | |
1516 | on CFG edges. The following routine must be careful to pay attention to | |
1517 | these inserted insns when performing its transformations. */ | |
1518 | ||
1519 | static int | |
997c5639 | 1520 | bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump) |
e45425ec | 1521 | { |
997c5639 DM |
1522 | rtx_insn *insn; |
1523 | rtx note; | |
e45425ec | 1524 | edge e, edest; |
c1e2610e | 1525 | int change; |
811ee85b | 1526 | int may_be_loop_header = false; |
e45425ec | 1527 | unsigned removed_p; |
c1e2610e | 1528 | unsigned i; |
e45425ec SB |
1529 | edge_iterator ei; |
1530 | ||
1531 | insn = (setcc != NULL) ? setcc : jump; | |
1532 | ||
1533 | /* Determine set of register uses in INSN. */ | |
1534 | reg_use_count = 0; | |
1535 | note_uses (&PATTERN (insn), find_used_regs, NULL); | |
1536 | note = find_reg_equal_equiv_note (insn); | |
1537 | if (note) | |
1538 | find_used_regs (&XEXP (note, 0), NULL); | |
1539 | ||
60cec1fd MP |
1540 | if (current_loops) |
1541 | { | |
811ee85b RB |
1542 | /* If we are to preserve loop structure then do not bypass |
1543 | a loop header. This will either rotate the loop, create | |
1544 | multiple entry loops or even irreducible regions. */ | |
1545 | if (bb == bb->loop_father->header) | |
60cec1fd MP |
1546 | return 0; |
1547 | } | |
1548 | else | |
1549 | { | |
60cec1fd MP |
1550 | FOR_EACH_EDGE (e, ei, bb->preds) |
1551 | if (e->flags & EDGE_DFS_BACK) | |
811ee85b RB |
1552 | { |
1553 | may_be_loop_header = true; | |
1554 | break; | |
1555 | } | |
60cec1fd | 1556 | } |
e45425ec SB |
1557 | |
1558 | change = 0; | |
1559 | for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); ) | |
1560 | { | |
1561 | removed_p = 0; | |
1562 | ||
1563 | if (e->flags & EDGE_COMPLEX) | |
1564 | { | |
1565 | ei_next (&ei); | |
1566 | continue; | |
1567 | } | |
1568 | ||
1569 | /* We can't redirect edges from new basic blocks. */ | |
1570 | if (e->src->index >= bypass_last_basic_block) | |
1571 | { | |
1572 | ei_next (&ei); | |
1573 | continue; | |
1574 | } | |
1575 | ||
1576 | /* The irreducible loops created by redirecting of edges entering the | |
ba5c3b24 EB |
1577 | loop from outside would decrease effectiveness of some of the |
1578 | following optimizations, so prevent this. */ | |
e45425ec SB |
1579 | if (may_be_loop_header |
1580 | && !(e->flags & EDGE_DFS_BACK)) | |
1581 | { | |
1582 | ei_next (&ei); | |
1583 | continue; | |
1584 | } | |
1585 | ||
1586 | for (i = 0; i < reg_use_count; i++) | |
1587 | { | |
c1e2610e SB |
1588 | rtx reg_used = reg_use_table[i]; |
1589 | unsigned int regno = REGNO (reg_used); | |
e45425ec | 1590 | basic_block dest, old_dest; |
11478306 | 1591 | struct cprop_expr *set; |
e45425ec SB |
1592 | rtx src, new_rtx; |
1593 | ||
1594 | set = find_bypass_set (regno, e->src->index); | |
1595 | ||
1596 | if (! set) | |
1597 | continue; | |
1598 | ||
1599 | /* Check the data flow is valid after edge insertions. */ | |
c1e2610e | 1600 | if (e->insns.r && reg_killed_on_edge (reg_used, e)) |
e45425ec SB |
1601 | continue; |
1602 | ||
1603 | src = SET_SRC (pc_set (jump)); | |
1604 | ||
1605 | if (setcc != NULL) | |
1606 | src = simplify_replace_rtx (src, | |
1607 | SET_DEST (PATTERN (setcc)), | |
1608 | SET_SRC (PATTERN (setcc))); | |
1609 | ||
c1e2610e | 1610 | new_rtx = simplify_replace_rtx (src, reg_used, set->src); |
e45425ec SB |
1611 | |
1612 | /* Jump bypassing may have already placed instructions on | |
1613 | edges of the CFG. We can't bypass an outgoing edge that | |
1614 | has instructions associated with it, as these insns won't | |
1615 | get executed if the incoming edge is redirected. */ | |
e45425ec SB |
1616 | if (new_rtx == pc_rtx) |
1617 | { | |
1618 | edest = FALLTHRU_EDGE (bb); | |
1619 | dest = edest->insns.r ? NULL : edest->dest; | |
1620 | } | |
1621 | else if (GET_CODE (new_rtx) == LABEL_REF) | |
1622 | { | |
1623 | dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0)); | |
1624 | /* Don't bypass edges containing instructions. */ | |
1625 | edest = find_edge (bb, dest); | |
1626 | if (edest && edest->insns.r) | |
1627 | dest = NULL; | |
1628 | } | |
1629 | else | |
1630 | dest = NULL; | |
1631 | ||
1632 | /* Avoid unification of the edge with other edges from original | |
1633 | branch. We would end up emitting the instruction on "both" | |
1634 | edges. */ | |
bd1cd0d0 | 1635 | if (dest && setcc && find_edge (e->src, dest)) |
e45425ec SB |
1636 | dest = NULL; |
1637 | ||
1638 | old_dest = e->dest; | |
1639 | if (dest != NULL | |
1640 | && dest != old_dest | |
fefa31b5 | 1641 | && dest != EXIT_BLOCK_PTR_FOR_FN (cfun)) |
e45425ec SB |
1642 | { |
1643 | redirect_edge_and_branch_force (e, dest); | |
1644 | ||
bd1cd0d0 | 1645 | /* Copy the register setter to the redirected edge. */ |
e45425ec SB |
1646 | if (setcc) |
1647 | { | |
1648 | rtx pat = PATTERN (setcc); | |
bd1cd0d0 | 1649 | insert_insn_on_edge (copy_insn (pat), e); |
e45425ec SB |
1650 | } |
1651 | ||
1652 | if (dump_file != NULL) | |
1653 | { | |
1654 | fprintf (dump_file, "JUMP-BYPASS: Proved reg %d " | |
1655 | "in jump_insn %d equals constant ", | |
1656 | regno, INSN_UID (jump)); | |
b77f9eab | 1657 | print_rtl (dump_file, set->src); |
bfe7af89 MP |
1658 | fprintf (dump_file, "\n\t when BB %d is entered from " |
1659 | "BB %d. Redirect edge %d->%d to %d.\n", | |
1660 | old_dest->index, e->src->index, e->src->index, | |
1661 | old_dest->index, dest->index); | |
e45425ec SB |
1662 | } |
1663 | change = 1; | |
1664 | removed_p = 1; | |
1665 | break; | |
1666 | } | |
1667 | } | |
1668 | if (!removed_p) | |
1669 | ei_next (&ei); | |
1670 | } | |
1671 | return change; | |
1672 | } | |
1673 | ||
1674 | /* Find basic blocks with more than one predecessor that only contain a | |
1675 | single conditional jump. If the result of the comparison is known at | |
1676 | compile-time from any incoming edge, redirect that edge to the | |
ba5c3b24 | 1677 | appropriate target. Return nonzero if a change was made. |
e45425ec SB |
1678 | |
1679 | This function is now mis-named, because we also handle indirect jumps. */ | |
1680 | ||
1681 | static int | |
1682 | bypass_conditional_jumps (void) | |
1683 | { | |
1684 | basic_block bb; | |
1685 | int changed; | |
997c5639 DM |
1686 | rtx_insn *setcc; |
1687 | rtx_insn *insn; | |
e45425ec SB |
1688 | rtx dest; |
1689 | ||
1690 | /* Note we start at block 1. */ | |
fefa31b5 | 1691 | if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun)) |
e45425ec SB |
1692 | return 0; |
1693 | ||
e45425ec SB |
1694 | mark_dfs_back_edges (); |
1695 | ||
1696 | changed = 0; | |
fefa31b5 DM |
1697 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb, |
1698 | EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb) | |
e45425ec SB |
1699 | { |
1700 | /* Check for more than one predecessor. */ | |
1701 | if (!single_pred_p (bb)) | |
1702 | { | |
997c5639 | 1703 | setcc = NULL; |
e45425ec SB |
1704 | FOR_BB_INSNS (bb, insn) |
1705 | if (DEBUG_INSN_P (insn)) | |
1706 | continue; | |
1707 | else if (NONJUMP_INSN_P (insn)) | |
1708 | { | |
1709 | if (setcc) | |
1710 | break; | |
1711 | if (GET_CODE (PATTERN (insn)) != SET) | |
1712 | break; | |
1713 | ||
1714 | dest = SET_DEST (PATTERN (insn)); | |
bd1cd0d0 | 1715 | if (REG_P (dest)) |
e45425ec SB |
1716 | setcc = insn; |
1717 | else | |
1718 | break; | |
1719 | } | |
1720 | else if (JUMP_P (insn)) | |
1721 | { | |
1722 | if ((any_condjump_p (insn) || computed_jump_p (insn)) | |
1723 | && onlyjump_p (insn)) | |
1724 | changed |= bypass_block (bb, setcc, insn); | |
1725 | break; | |
1726 | } | |
1727 | else if (INSN_P (insn)) | |
1728 | break; | |
1729 | } | |
1730 | } | |
1731 | ||
1732 | /* If we bypassed any register setting insns, we inserted a | |
1733 | copy on the redirected edge. These need to be committed. */ | |
1734 | if (changed) | |
1735 | commit_edge_insertions (); | |
1736 | ||
1737 | return changed; | |
1738 | } | |
1739 | \f | |
e45425ec SB |
1740 | /* Main function for the CPROP pass. */ |
1741 | ||
1742 | static int | |
1743 | one_cprop_pass (void) | |
1744 | { | |
cfb99521 | 1745 | int i; |
e45425ec SB |
1746 | int changed = 0; |
1747 | ||
1748 | /* Return if there's nothing to do, or it is too expensive. */ | |
0cae8d31 | 1749 | if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1 |
87d8dd6c | 1750 | || gcse_or_cprop_is_too_expensive (_ ("const/copy propagation disabled"))) |
e45425ec SB |
1751 | return 0; |
1752 | ||
1753 | global_const_prop_count = local_const_prop_count = 0; | |
1754 | global_copy_prop_count = local_copy_prop_count = 0; | |
1755 | ||
1756 | bytes_used = 0; | |
3084ce69 | 1757 | gcc_obstack_init (&cprop_obstack); |
e45425ec SB |
1758 | |
1759 | /* Do a local const/copy propagation pass first. The global pass | |
1760 | only handles global opportunities. | |
1761 | If the local pass changes something, remove any unreachable blocks | |
1762 | because the CPROP global dataflow analysis may get into infinite | |
1763 | loops for CFGs with unreachable blocks. | |
1764 | ||
1765 | FIXME: This local pass should not be necessary after CSE (but for | |
1766 | some reason it still is). It is also (proven) not necessary | |
1767 | to run the local pass right after FWPWOP. | |
1768 | ||
1769 | FIXME: The global analysis would not get into infinite loops if it | |
1770 | would use the DF solver (via df_simple_dataflow) instead of | |
1771 | the solver implemented in this file. */ | |
3084ce69 SB |
1772 | changed |= local_cprop_pass (); |
1773 | if (changed) | |
384d7a55 SB |
1774 | delete_unreachable_blocks (); |
1775 | ||
1776 | /* Determine implicit sets. This may change the CFG (split critical | |
1777 | edges if that exposes an implicit set). | |
1778 | Note that find_implicit_sets() does not rely on up-to-date DF caches | |
1779 | so that we do not have to re-run df_analyze() even if local CPROP | |
1780 | changed something. | |
1781 | ??? This could run earlier so that any uncovered implicit sets | |
1782 | sets could be exploited in local_cprop_pass() also. Later. */ | |
1783 | changed |= find_implicit_sets (); | |
1784 | ||
1785 | /* If local_cprop_pass() or find_implicit_sets() changed something, | |
1786 | run df_analyze() to bring all insn caches up-to-date, and to take | |
1787 | new basic blocks from edge splitting on the DF radar. | |
1788 | NB: This also runs the fast DCE pass, because execute_rtl_cprop | |
1789 | sets DF_LR_RUN_DCE. */ | |
1790 | if (changed) | |
1791 | df_analyze (); | |
e45425ec | 1792 | |
cfb99521 | 1793 | /* Initialize implicit_set_indexes array. */ |
8b1c6fd7 DM |
1794 | implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun)); |
1795 | for (i = 0; i < last_basic_block_for_fn (cfun); i++) | |
cfb99521 BC |
1796 | implicit_set_indexes[i] = -1; |
1797 | ||
e45425ec SB |
1798 | alloc_hash_table (&set_hash_table); |
1799 | compute_hash_table (&set_hash_table); | |
1800 | ||
1801 | /* Free implicit_sets before peak usage. */ | |
1802 | free (implicit_sets); | |
1803 | implicit_sets = NULL; | |
1804 | ||
1805 | if (dump_file) | |
1806 | dump_hash_table (dump_file, "SET", &set_hash_table); | |
1807 | if (set_hash_table.n_elems > 0) | |
1808 | { | |
1809 | basic_block bb; | |
90aead21 | 1810 | auto_vec<rtx_insn *> uncond_traps; |
e45425ec | 1811 | |
8b1c6fd7 DM |
1812 | alloc_cprop_mem (last_basic_block_for_fn (cfun), |
1813 | set_hash_table.n_elems); | |
e45425ec SB |
1814 | compute_cprop_data (); |
1815 | ||
cfb99521 BC |
1816 | free (implicit_set_indexes); |
1817 | implicit_set_indexes = NULL; | |
1818 | ||
3084ce69 SB |
1819 | /* Allocate vars to track sets of regs. */ |
1820 | reg_set_bitmap = ALLOC_REG_SET (NULL); | |
1821 | ||
fefa31b5 DM |
1822 | FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb, |
1823 | EXIT_BLOCK_PTR_FOR_FN (cfun), | |
ba5c3b24 | 1824 | next_bb) |
e45425ec | 1825 | { |
90aead21 BS |
1826 | bool seen_uncond_trap = false; |
1827 | rtx_insn *insn; | |
1828 | ||
e45425ec SB |
1829 | /* Reset tables used to keep track of what's still valid [since |
1830 | the start of the block]. */ | |
1831 | reset_opr_set_tables (); | |
1832 | ||
1833 | FOR_BB_INSNS (bb, insn) | |
1834 | if (INSN_P (insn)) | |
1835 | { | |
90aead21 BS |
1836 | bool was_uncond_trap |
1837 | = (GET_CODE (PATTERN (insn)) == TRAP_IF | |
1838 | && XEXP (PATTERN (insn), 0) == const1_rtx); | |
1839 | ||
e45425ec SB |
1840 | changed |= cprop_insn (insn); |
1841 | ||
1842 | /* Keep track of everything modified by this insn. */ | |
1843 | /* ??? Need to be careful w.r.t. mods done to INSN. | |
1844 | Don't call mark_oprs_set if we turned the | |
b71b43d9 | 1845 | insn into a NOTE, or deleted the insn. */ |
4654c0cf | 1846 | if (! NOTE_P (insn) && ! insn->deleted ()) |
e45425ec | 1847 | mark_oprs_set (insn); |
90aead21 | 1848 | |
fe792dd8 | 1849 | if (!was_uncond_trap |
90aead21 BS |
1850 | && GET_CODE (PATTERN (insn)) == TRAP_IF |
1851 | && XEXP (PATTERN (insn), 0) == const1_rtx) | |
1852 | { | |
fe792dd8 JJ |
1853 | /* If we have already seen an unconditional trap |
1854 | earlier, the rest of the bb is going to be removed | |
1855 | as unreachable. Just turn it into a note, so that | |
1856 | RTL verification doesn't complain about it before | |
1857 | it is finally removed. */ | |
1858 | if (seen_uncond_trap) | |
1859 | set_insn_deleted (insn); | |
1860 | else | |
1861 | { | |
1862 | seen_uncond_trap = true; | |
1863 | uncond_traps.safe_push (insn); | |
1864 | } | |
90aead21 | 1865 | } |
e45425ec SB |
1866 | } |
1867 | } | |
1868 | ||
ec48209a JJ |
1869 | /* Make sure bypass_conditional_jumps will ignore not just its new |
1870 | basic blocks, but also the ones after unconditional traps (those are | |
1871 | unreachable and will be eventually removed as such). */ | |
1872 | bypass_last_basic_block = last_basic_block_for_fn (cfun); | |
1873 | ||
90aead21 BS |
1874 | while (!uncond_traps.is_empty ()) |
1875 | { | |
1876 | rtx_insn *insn = uncond_traps.pop (); | |
1877 | basic_block to_split = BLOCK_FOR_INSN (insn); | |
1878 | remove_edge (split_block (to_split, insn)); | |
1879 | emit_barrier_after_bb (to_split); | |
1880 | } | |
1881 | ||
711ce021 BS |
1882 | changed |= bypass_conditional_jumps (); |
1883 | ||
3084ce69 | 1884 | FREE_REG_SET (reg_set_bitmap); |
e45425ec SB |
1885 | free_cprop_mem (); |
1886 | } | |
cfb99521 BC |
1887 | else |
1888 | { | |
1889 | free (implicit_set_indexes); | |
1890 | implicit_set_indexes = NULL; | |
1891 | } | |
e45425ec SB |
1892 | |
1893 | free_hash_table (&set_hash_table); | |
3084ce69 | 1894 | obstack_free (&cprop_obstack, NULL); |
e45425ec SB |
1895 | |
1896 | if (dump_file) | |
1897 | { | |
1898 | fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ", | |
0cae8d31 DM |
1899 | current_function_name (), n_basic_blocks_for_fn (cfun), |
1900 | bytes_used); | |
e45425ec SB |
1901 | fprintf (dump_file, "%d local const props, %d local copy props, ", |
1902 | local_const_prop_count, local_copy_prop_count); | |
1903 | fprintf (dump_file, "%d global const props, %d global copy props\n\n", | |
1904 | global_const_prop_count, global_copy_prop_count); | |
1905 | } | |
1906 | ||
1907 | return changed; | |
1908 | } | |
e45425ec SB |
1909 | \f |
1910 | /* All the passes implemented in this file. Each pass has its | |
1911 | own gate and execute function, and at the end of the file a | |
e53b6e56 | 1912 | pass definition for passes.cc. |
e45425ec SB |
1913 | |
1914 | We do not construct an accurate cfg in functions which call | |
1915 | setjmp, so none of these passes runs if the function calls | |
1916 | setjmp. | |
1917 | FIXME: Should just handle setjmp via REG_SETJMP notes. */ | |
1918 | ||
e45425ec SB |
1919 | static unsigned int |
1920 | execute_rtl_cprop (void) | |
1921 | { | |
1922 | int changed; | |
1923 | delete_unreachable_blocks (); | |
1924 | df_set_flags (DF_LR_RUN_DCE); | |
1925 | df_analyze (); | |
1926 | changed = one_cprop_pass (); | |
1927 | flag_rerun_cse_after_global_opts |= changed; | |
1928 | if (changed) | |
df09560e | 1929 | cleanup_cfg (CLEANUP_CFG_CHANGED); |
e45425ec SB |
1930 | return 0; |
1931 | } | |
1932 | ||
27a4cd48 DM |
1933 | namespace { |
1934 | ||
1935 | const pass_data pass_data_rtl_cprop = | |
e45425ec | 1936 | { |
27a4cd48 DM |
1937 | RTL_PASS, /* type */ |
1938 | "cprop", /* name */ | |
1939 | OPTGROUP_NONE, /* optinfo_flags */ | |
27a4cd48 DM |
1940 | TV_CPROP, /* tv_id */ |
1941 | PROP_cfglayout, /* properties_required */ | |
1942 | 0, /* properties_provided */ | |
1943 | 0, /* properties_destroyed */ | |
1944 | 0, /* todo_flags_start */ | |
3bea341f | 1945 | TODO_df_finish, /* todo_flags_finish */ |
e45425ec | 1946 | }; |
27a4cd48 DM |
1947 | |
1948 | class pass_rtl_cprop : public rtl_opt_pass | |
1949 | { | |
1950 | public: | |
c3284718 RS |
1951 | pass_rtl_cprop (gcc::context *ctxt) |
1952 | : rtl_opt_pass (pass_data_rtl_cprop, ctxt) | |
27a4cd48 DM |
1953 | {} |
1954 | ||
1955 | /* opt_pass methods: */ | |
725793af DM |
1956 | opt_pass * clone () final override { return new pass_rtl_cprop (m_ctxt); } |
1957 | bool gate (function *fun) final override | |
1a3d085c TS |
1958 | { |
1959 | return optimize > 0 && flag_gcse | |
1960 | && !fun->calls_setjmp | |
1961 | && dbg_cnt (cprop); | |
1962 | } | |
1963 | ||
725793af DM |
1964 | unsigned int execute (function *) final override |
1965 | { | |
1966 | return execute_rtl_cprop (); | |
1967 | } | |
27a4cd48 DM |
1968 | |
1969 | }; // class pass_rtl_cprop | |
1970 | ||
1971 | } // anon namespace | |
1972 | ||
1973 | rtl_opt_pass * | |
1974 | make_pass_rtl_cprop (gcc::context *ctxt) | |
1975 | { | |
1976 | return new pass_rtl_cprop (ctxt); | |
1977 | } |