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