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