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