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