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