Fix bootstrap/PR63632
[official-gcc.git] / gcc / cprop.c
blob1ac06e0262f5ff7029bbdd0c54870551aca72254
1 /* Global constant/copy propagation for RTL.
2 Copyright (C) 1997-2014 Free Software Foundation, Inc.
4 This file is part of GCC.
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.
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.
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/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "diagnostic-core.h"
25 #include "toplev.h"
27 #include "rtl.h"
28 #include "tree.h"
29 #include "tm_p.h"
30 #include "regs.h"
31 #include "hard-reg-set.h"
32 #include "flags.h"
33 #include "insn-config.h"
34 #include "recog.h"
35 #include "basic-block.h"
36 #include "hashtab.h"
37 #include "hash-set.h"
38 #include "vec.h"
39 #include "machmode.h"
40 #include "input.h"
41 #include "function.h"
42 #include "expr.h"
43 #include "except.h"
44 #include "params.h"
45 #include "cselib.h"
46 #include "intl.h"
47 #include "obstack.h"
48 #include "tree-pass.h"
49 #include "df.h"
50 #include "dbgcnt.h"
51 #include "target.h"
52 #include "cfgloop.h"
55 /* An obstack for our working variables. */
56 static struct obstack cprop_obstack;
58 /* Occurrence of an expression.
59 There is one per basic block. If a pattern appears more than once the
60 last appearance is used. */
62 struct cprop_occr
64 /* Next occurrence of this expression. */
65 struct cprop_occr *next;
66 /* The insn that computes the expression. */
67 rtx_insn *insn;
70 typedef struct cprop_occr *occr_t;
72 /* Hash table entry for assignment expressions. */
74 struct cprop_expr
76 /* The expression (DEST := SRC). */
77 rtx dest;
78 rtx src;
80 /* Index in the available expression bitmaps. */
81 int bitmap_index;
82 /* Next entry with the same hash. */
83 struct cprop_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 whose operands are not modified by following statements
87 in the basic block [including this insn]. */
88 struct cprop_occr *avail_occr;
91 /* Hash table for copy propagation expressions.
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. */
100 struct hash_table_d
102 /* The table itself.
103 This is an array of `set_hash_table_size' elements. */
104 struct cprop_expr **table;
106 /* Size of the hash table, in elements. */
107 unsigned int size;
109 /* Number of hash table elements. */
110 unsigned int n_elems;
113 /* Copy propagation hash table. */
114 static struct hash_table_d set_hash_table;
116 /* Array of implicit set patterns indexed by basic block index. */
117 static rtx *implicit_sets;
119 /* Array of indexes of expressions for implicit set patterns indexed by basic
120 block index. In other words, implicit_set_indexes[i] is the bitmap_index
121 of the expression whose RTX is implicit_sets[i]. */
122 static int *implicit_set_indexes;
124 /* Bitmap containing one bit for each register in the program.
125 Used when performing GCSE to track which registers have been set since
126 the start or end of the basic block while traversing that block. */
127 static regset reg_set_bitmap;
129 /* Various variables for statistics gathering. */
131 /* Memory used in a pass.
132 This isn't intended to be absolutely precise. Its intent is only
133 to keep an eye on memory usage. */
134 static int bytes_used;
136 /* Number of local constants propagated. */
137 static int local_const_prop_count;
138 /* Number of local copies propagated. */
139 static int local_copy_prop_count;
140 /* Number of global constants propagated. */
141 static int global_const_prop_count;
142 /* Number of global copies propagated. */
143 static int global_copy_prop_count;
145 #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T)))
146 #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S)))
148 /* Cover function to obstack_alloc. */
150 static void *
151 cprop_alloc (unsigned long size)
153 bytes_used += size;
154 return obstack_alloc (&cprop_obstack, size);
157 /* Return nonzero if register X is unchanged from INSN to the end
158 of INSN's basic block. */
160 static int
161 reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
163 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
166 /* Hash a set of register REGNO.
168 Sets are hashed on the register that is set. This simplifies the PRE copy
169 propagation code.
171 ??? May need to make things more elaborate. Later, as necessary. */
173 static unsigned int
174 hash_mod (int regno, int hash_table_size)
176 return (unsigned) regno % hash_table_size;
179 /* Insert assignment DEST:=SET from INSN in the hash table.
180 DEST is a register and SET is a register or a suitable constant.
181 If the assignment is already present in the table, record it as
182 the last occurrence in INSN's basic block.
183 IMPLICIT is true if it's an implicit set, false otherwise. */
185 static void
186 insert_set_in_table (rtx dest, rtx src, rtx_insn *insn,
187 struct hash_table_d *table, bool implicit)
189 bool found = false;
190 unsigned int hash;
191 struct cprop_expr *cur_expr, *last_expr = NULL;
192 struct cprop_occr *cur_occr;
194 hash = hash_mod (REGNO (dest), table->size);
196 for (cur_expr = table->table[hash]; cur_expr;
197 cur_expr = cur_expr->next_same_hash)
199 if (dest == cur_expr->dest
200 && src == cur_expr->src)
202 found = true;
203 break;
205 last_expr = cur_expr;
208 if (! found)
210 cur_expr = GOBNEW (struct cprop_expr);
211 bytes_used += sizeof (struct cprop_expr);
212 if (table->table[hash] == NULL)
213 /* This is the first pattern that hashed to this index. */
214 table->table[hash] = cur_expr;
215 else
216 /* Add EXPR to end of this hash chain. */
217 last_expr->next_same_hash = cur_expr;
219 /* Set the fields of the expr element.
220 We must copy X because it can be modified when copy propagation is
221 performed on its operands. */
222 cur_expr->dest = copy_rtx (dest);
223 cur_expr->src = copy_rtx (src);
224 cur_expr->bitmap_index = table->n_elems++;
225 cur_expr->next_same_hash = NULL;
226 cur_expr->avail_occr = NULL;
229 /* Now record the occurrence. */
230 cur_occr = cur_expr->avail_occr;
232 if (cur_occr
233 && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
235 /* Found another instance of the expression in the same basic block.
236 Prefer this occurrence to the currently recorded one. We want
237 the last one in the block and the block is scanned from start
238 to end. */
239 cur_occr->insn = insn;
241 else
243 /* First occurrence of this expression in this basic block. */
244 cur_occr = GOBNEW (struct cprop_occr);
245 bytes_used += sizeof (struct cprop_occr);
246 cur_occr->insn = insn;
247 cur_occr->next = cur_expr->avail_occr;
248 cur_expr->avail_occr = cur_occr;
251 /* Record bitmap_index of the implicit set in implicit_set_indexes. */
252 if (implicit)
253 implicit_set_indexes[BLOCK_FOR_INSN (insn)->index]
254 = cur_expr->bitmap_index;
257 /* Determine whether the rtx X should be treated as a constant for CPROP.
258 Since X might be inserted more than once we have to take care that it
259 is sharable. */
261 static bool
262 cprop_constant_p (const_rtx x)
264 return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
267 /* Scan SET present in INSN and add an entry to the hash TABLE.
268 IMPLICIT is true if it's an implicit set, false otherwise. */
270 static void
271 hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table,
272 bool implicit)
274 rtx src = SET_SRC (set);
275 rtx dest = SET_DEST (set);
277 if (REG_P (dest)
278 && ! HARD_REGISTER_P (dest)
279 && reg_available_p (dest, insn)
280 && can_copy_p (GET_MODE (dest)))
282 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
284 This allows us to do a single CPROP pass and still eliminate
285 redundant constants, addresses or other expressions that are
286 constructed with multiple instructions.
288 However, keep the original SRC if INSN is a simple reg-reg move. In
289 In this case, there will almost always be a REG_EQUAL note on the
290 insn that sets SRC. By recording the REG_EQUAL value here as SRC
291 for INSN, we miss copy propagation opportunities.
293 Note that this does not impede profitable constant propagations. We
294 "look through" reg-reg sets in lookup_set. */
295 rtx note = find_reg_equal_equiv_note (insn);
296 if (note != 0
297 && REG_NOTE_KIND (note) == REG_EQUAL
298 && !REG_P (src)
299 && cprop_constant_p (XEXP (note, 0)))
300 src = XEXP (note, 0), set = gen_rtx_SET (VOIDmode, dest, src);
302 /* Record sets for constant/copy propagation. */
303 if ((REG_P (src)
304 && src != dest
305 && ! HARD_REGISTER_P (src)
306 && reg_available_p (src, insn))
307 || cprop_constant_p (src))
308 insert_set_in_table (dest, src, insn, table, implicit);
312 /* Process INSN and add hash table entries as appropriate. */
314 static void
315 hash_scan_insn (rtx_insn *insn, struct hash_table_d *table)
317 rtx pat = PATTERN (insn);
318 int i;
320 /* Pick out the sets of INSN and for other forms of instructions record
321 what's been modified. */
323 if (GET_CODE (pat) == SET)
324 hash_scan_set (pat, insn, table, false);
325 else if (GET_CODE (pat) == PARALLEL)
326 for (i = 0; i < XVECLEN (pat, 0); i++)
328 rtx x = XVECEXP (pat, 0, i);
330 if (GET_CODE (x) == SET)
331 hash_scan_set (x, insn, table, false);
335 /* Dump the hash table TABLE to file FILE under the name NAME. */
337 static void
338 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
340 int i;
341 /* Flattened out table, so it's printed in proper order. */
342 struct cprop_expr **flat_table;
343 unsigned int *hash_val;
344 struct cprop_expr *expr;
346 flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems);
347 hash_val = XNEWVEC (unsigned int, table->n_elems);
349 for (i = 0; i < (int) table->size; i++)
350 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
352 flat_table[expr->bitmap_index] = expr;
353 hash_val[expr->bitmap_index] = i;
356 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
357 name, table->size, table->n_elems);
359 for (i = 0; i < (int) table->n_elems; i++)
360 if (flat_table[i] != 0)
362 expr = flat_table[i];
363 fprintf (file, "Index %d (hash value %d)\n ",
364 expr->bitmap_index, hash_val[i]);
365 print_rtl (file, expr->dest);
366 fprintf (file, " := ");
367 print_rtl (file, expr->src);
368 fprintf (file, "\n");
371 fprintf (file, "\n");
373 free (flat_table);
374 free (hash_val);
377 /* Record as unavailable all registers that are DEF operands of INSN. */
379 static void
380 make_set_regs_unavailable (rtx_insn *insn)
382 df_ref def;
384 FOR_EACH_INSN_DEF (def, insn)
385 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
388 /* Top level function to create an assignment hash table.
390 Assignment entries are placed in the hash table if
391 - they are of the form (set (pseudo-reg) src),
392 - src is something we want to perform const/copy propagation on,
393 - none of the operands or target are subsequently modified in the block
395 Currently src must be a pseudo-reg or a const_int.
397 TABLE is the table computed. */
399 static void
400 compute_hash_table_work (struct hash_table_d *table)
402 basic_block bb;
404 /* Allocate vars to track sets of regs. */
405 reg_set_bitmap = ALLOC_REG_SET (NULL);
407 FOR_EACH_BB_FN (bb, cfun)
409 rtx_insn *insn;
411 /* Reset tables used to keep track of what's not yet invalid [since
412 the end of the block]. */
413 CLEAR_REG_SET (reg_set_bitmap);
415 /* Go over all insns from the last to the first. This is convenient
416 for tracking available registers, i.e. not set between INSN and
417 the end of the basic block BB. */
418 FOR_BB_INSNS_REVERSE (bb, insn)
420 /* Only real insns are interesting. */
421 if (!NONDEBUG_INSN_P (insn))
422 continue;
424 /* Record interesting sets from INSN in the hash table. */
425 hash_scan_insn (insn, table);
427 /* Any registers set in INSN will make SETs above it not AVAIL. */
428 make_set_regs_unavailable (insn);
431 /* Insert implicit sets in the hash table, pretending they appear as
432 insns at the head of the basic block. */
433 if (implicit_sets[bb->index] != NULL_RTX)
434 hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
437 FREE_REG_SET (reg_set_bitmap);
440 /* Allocate space for the set/expr hash TABLE.
441 It is used to determine the number of buckets to use. */
443 static void
444 alloc_hash_table (struct hash_table_d *table)
446 int n;
448 n = get_max_insn_count ();
450 table->size = n / 4;
451 if (table->size < 11)
452 table->size = 11;
454 /* Attempt to maintain efficient use of hash table.
455 Making it an odd number is simplest for now.
456 ??? Later take some measurements. */
457 table->size |= 1;
458 n = table->size * sizeof (struct cprop_expr *);
459 table->table = XNEWVAR (struct cprop_expr *, n);
462 /* Free things allocated by alloc_hash_table. */
464 static void
465 free_hash_table (struct hash_table_d *table)
467 free (table->table);
470 /* Compute the hash TABLE for doing copy/const propagation or
471 expression hash table. */
473 static void
474 compute_hash_table (struct hash_table_d *table)
476 /* Initialize count of number of entries in hash table. */
477 table->n_elems = 0;
478 memset (table->table, 0, table->size * sizeof (struct cprop_expr *));
480 compute_hash_table_work (table);
483 /* Expression tracking support. */
485 /* Lookup REGNO in the set TABLE. The result is a pointer to the
486 table entry, or NULL if not found. */
488 static struct cprop_expr *
489 lookup_set (unsigned int regno, struct hash_table_d *table)
491 unsigned int hash = hash_mod (regno, table->size);
492 struct cprop_expr *expr;
494 expr = table->table[hash];
496 while (expr && REGNO (expr->dest) != regno)
497 expr = expr->next_same_hash;
499 return expr;
502 /* Return the next entry for REGNO in list EXPR. */
504 static struct cprop_expr *
505 next_set (unsigned int regno, struct cprop_expr *expr)
508 expr = expr->next_same_hash;
509 while (expr && REGNO (expr->dest) != regno);
511 return expr;
514 /* Reset tables used to keep track of what's still available [since the
515 start of the block]. */
517 static void
518 reset_opr_set_tables (void)
520 /* Maintain a bitmap of which regs have been set since beginning of
521 the block. */
522 CLEAR_REG_SET (reg_set_bitmap);
525 /* Return nonzero if the register X has not been set yet [since the
526 start of the basic block containing INSN]. */
528 static int
529 reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
531 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
534 /* Record things set by INSN.
535 This data is used by reg_not_set_p. */
537 static void
538 mark_oprs_set (rtx_insn *insn)
540 df_ref def;
542 FOR_EACH_INSN_DEF (def, insn)
543 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
546 /* Compute copy/constant propagation working variables. */
548 /* Local properties of assignments. */
549 static sbitmap *cprop_avloc;
550 static sbitmap *cprop_kill;
552 /* Global properties of assignments (computed from the local properties). */
553 static sbitmap *cprop_avin;
554 static sbitmap *cprop_avout;
556 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
557 basic blocks. N_SETS is the number of sets. */
559 static void
560 alloc_cprop_mem (int n_blocks, int n_sets)
562 cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
563 cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
565 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
566 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
569 /* Free vars used by copy/const propagation. */
571 static void
572 free_cprop_mem (void)
574 sbitmap_vector_free (cprop_avloc);
575 sbitmap_vector_free (cprop_kill);
576 sbitmap_vector_free (cprop_avin);
577 sbitmap_vector_free (cprop_avout);
580 /* Compute the local properties of each recorded expression.
582 Local properties are those that are defined by the block, irrespective of
583 other blocks.
585 An expression is killed in a block if its operands, either DEST or SRC, are
586 modified in the block.
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.
592 KILL and COMP are destination sbitmaps for recording local properties. */
594 static void
595 compute_local_properties (sbitmap *kill, sbitmap *comp,
596 struct hash_table_d *table)
598 unsigned int i;
600 /* Initialize the bitmaps that were passed in. */
601 bitmap_vector_clear (kill, last_basic_block_for_fn (cfun));
602 bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
604 for (i = 0; i < table->size; i++)
606 struct cprop_expr *expr;
608 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
610 int indx = expr->bitmap_index;
611 df_ref def;
612 struct cprop_occr *occr;
614 /* For each definition of the destination pseudo-reg, the expression
615 is killed in the block where the definition is. */
616 for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
617 def; def = DF_REF_NEXT_REG (def))
618 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
620 /* If the source is a pseudo-reg, for each definition of the source,
621 the expression is killed in the block where the definition is. */
622 if (REG_P (expr->src))
623 for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
624 def; def = DF_REF_NEXT_REG (def))
625 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
627 /* The occurrences recorded in avail_occr are exactly those that
628 are locally available in the block where they are. */
629 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
631 bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
637 /* Hash table support. */
639 /* Top level routine to do the dataflow analysis needed by copy/const
640 propagation. */
642 static void
643 compute_cprop_data (void)
645 basic_block bb;
647 compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
648 compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
650 /* Merge implicit sets into CPROP_AVIN. They are always available at the
651 entry of their basic block. We need to do this because 1) implicit sets
652 aren't recorded for the local pass so they cannot be propagated within
653 their basic block by this pass and 2) the global pass would otherwise
654 propagate them only in the successors of their basic block. */
655 FOR_EACH_BB_FN (bb, cfun)
657 int index = implicit_set_indexes[bb->index];
658 if (index != -1)
659 bitmap_set_bit (cprop_avin[bb->index], index);
663 /* Copy/constant propagation. */
665 /* Maximum number of register uses in an insn that we handle. */
666 #define MAX_USES 8
668 /* Table of uses (registers, both hard and pseudo) found in an insn.
669 Allocated statically to avoid alloc/free complexity and overhead. */
670 static rtx reg_use_table[MAX_USES];
672 /* Index into `reg_use_table' while building it. */
673 static unsigned reg_use_count;
675 /* Set up a list of register numbers used in INSN. The found uses are stored
676 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
677 and contains the number of uses in the table upon exit.
679 ??? If a register appears multiple times we will record it multiple times.
680 This doesn't hurt anything but it will slow things down. */
682 static void
683 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
685 int i, j;
686 enum rtx_code code;
687 const char *fmt;
688 rtx x = *xptr;
690 /* repeat is used to turn tail-recursion into iteration since GCC
691 can't do it when there's no return value. */
692 repeat:
693 if (x == 0)
694 return;
696 code = GET_CODE (x);
697 if (REG_P (x))
699 if (reg_use_count == MAX_USES)
700 return;
702 reg_use_table[reg_use_count] = x;
703 reg_use_count++;
706 /* Recursively scan the operands of this expression. */
708 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
710 if (fmt[i] == 'e')
712 /* If we are about to do the last recursive call
713 needed at this level, change it into iteration.
714 This function is called enough to be worth it. */
715 if (i == 0)
717 x = XEXP (x, 0);
718 goto repeat;
721 find_used_regs (&XEXP (x, i), data);
723 else if (fmt[i] == 'E')
724 for (j = 0; j < XVECLEN (x, i); j++)
725 find_used_regs (&XVECEXP (x, i, j), data);
729 /* Try to replace all uses of FROM in INSN with TO.
730 Return nonzero if successful. */
732 static int
733 try_replace_reg (rtx from, rtx to, rtx_insn *insn)
735 rtx note = find_reg_equal_equiv_note (insn);
736 rtx src = 0;
737 int success = 0;
738 rtx set = single_set (insn);
740 /* Usually we substitute easy stuff, so we won't copy everything.
741 We however need to take care to not duplicate non-trivial CONST
742 expressions. */
743 to = copy_rtx (to);
745 validate_replace_src_group (from, to, insn);
746 if (num_changes_pending () && apply_change_group ())
747 success = 1;
749 /* Try to simplify SET_SRC if we have substituted a constant. */
750 if (success && set && CONSTANT_P (to))
752 src = simplify_rtx (SET_SRC (set));
754 if (src)
755 validate_change (insn, &SET_SRC (set), src, 0);
758 /* If there is already a REG_EQUAL note, update the expression in it
759 with our replacement. */
760 if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
761 set_unique_reg_note (insn, REG_EQUAL,
762 simplify_replace_rtx (XEXP (note, 0), from, to));
763 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
765 /* If above failed and this is a single set, try to simplify the source
766 of the set given our substitution. We could perhaps try this for
767 multiple SETs, but it probably won't buy us anything. */
768 src = simplify_replace_rtx (SET_SRC (set), from, to);
770 if (!rtx_equal_p (src, SET_SRC (set))
771 && validate_change (insn, &SET_SRC (set), src, 0))
772 success = 1;
774 /* If we've failed perform the replacement, have a single SET to
775 a REG destination and don't yet have a note, add a REG_EQUAL note
776 to not lose information. */
777 if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
778 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
781 if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
783 /* Registers can also appear as uses in SET_DEST if it is a MEM.
784 We could perhaps try this for multiple SETs, but it probably
785 won't buy us anything. */
786 rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
788 if (!rtx_equal_p (dest, SET_DEST (set))
789 && validate_change (insn, &SET_DEST (set), dest, 0))
790 success = 1;
793 /* REG_EQUAL may get simplified into register.
794 We don't allow that. Remove that note. This code ought
795 not to happen, because previous code ought to synthesize
796 reg-reg move, but be on the safe side. */
797 if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
798 remove_note (insn, note);
800 return success;
803 /* Find a set of REGNOs that are available on entry to INSN's block. Return
804 NULL no such set is found. */
806 static struct cprop_expr *
807 find_avail_set (int regno, rtx_insn *insn)
809 /* SET1 contains the last set found that can be returned to the caller for
810 use in a substitution. */
811 struct cprop_expr *set1 = 0;
813 /* Loops are not possible here. To get a loop we would need two sets
814 available at the start of the block containing INSN. i.e. we would
815 need two sets like this available at the start of the block:
817 (set (reg X) (reg Y))
818 (set (reg Y) (reg X))
820 This can not happen since the set of (reg Y) would have killed the
821 set of (reg X) making it unavailable at the start of this block. */
822 while (1)
824 rtx src;
825 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
827 /* Find a set that is available at the start of the block
828 which contains INSN. */
829 while (set)
831 if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
832 set->bitmap_index))
833 break;
834 set = next_set (regno, set);
837 /* If no available set was found we've reached the end of the
838 (possibly empty) copy chain. */
839 if (set == 0)
840 break;
842 src = set->src;
844 /* We know the set is available.
845 Now check that SRC is locally anticipatable (i.e. none of the
846 source operands have changed since the start of the block).
848 If the source operand changed, we may still use it for the next
849 iteration of this loop, but we may not use it for substitutions. */
851 if (cprop_constant_p (src) || reg_not_set_p (src, insn))
852 set1 = set;
854 /* If the source of the set is anything except a register, then
855 we have reached the end of the copy chain. */
856 if (! REG_P (src))
857 break;
859 /* Follow the copy chain, i.e. start another iteration of the loop
860 and see if we have an available copy into SRC. */
861 regno = REGNO (src);
864 /* SET1 holds the last set that was available and anticipatable at
865 INSN. */
866 return set1;
869 /* Subroutine of cprop_insn that tries to propagate constants into
870 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
871 it is the instruction that immediately precedes JUMP, and must be a
872 single SET of a register. FROM is what we will try to replace,
873 SRC is the constant we will try to substitute for it. Return nonzero
874 if a change was made. */
876 static int
877 cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src)
879 rtx new_rtx, set_src, note_src;
880 rtx set = pc_set (jump);
881 rtx note = find_reg_equal_equiv_note (jump);
883 if (note)
885 note_src = XEXP (note, 0);
886 if (GET_CODE (note_src) == EXPR_LIST)
887 note_src = NULL_RTX;
889 else note_src = NULL_RTX;
891 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
892 set_src = note_src ? note_src : SET_SRC (set);
894 /* First substitute the SETCC condition into the JUMP instruction,
895 then substitute that given values into this expanded JUMP. */
896 if (setcc != NULL_RTX
897 && !modified_between_p (from, setcc, jump)
898 && !modified_between_p (src, setcc, jump))
900 rtx setcc_src;
901 rtx setcc_set = single_set (setcc);
902 rtx setcc_note = find_reg_equal_equiv_note (setcc);
903 setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
904 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
905 set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
906 setcc_src);
908 else
909 setcc = NULL;
911 new_rtx = simplify_replace_rtx (set_src, from, src);
913 /* If no simplification can be made, then try the next register. */
914 if (rtx_equal_p (new_rtx, SET_SRC (set)))
915 return 0;
917 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
918 if (new_rtx == pc_rtx)
919 delete_insn (jump);
920 else
922 /* Ensure the value computed inside the jump insn to be equivalent
923 to one computed by setcc. */
924 if (setcc && modified_in_p (new_rtx, setcc))
925 return 0;
926 if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
928 /* When (some) constants are not valid in a comparison, and there
929 are two registers to be replaced by constants before the entire
930 comparison can be folded into a constant, we need to keep
931 intermediate information in REG_EQUAL notes. For targets with
932 separate compare insns, such notes are added by try_replace_reg.
933 When we have a combined compare-and-branch instruction, however,
934 we need to attach a note to the branch itself to make this
935 optimization work. */
937 if (!rtx_equal_p (new_rtx, note_src))
938 set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
939 return 0;
942 /* Remove REG_EQUAL note after simplification. */
943 if (note_src)
944 remove_note (jump, note);
947 #ifdef HAVE_cc0
948 /* Delete the cc0 setter. */
949 if (setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
950 delete_insn (setcc);
951 #endif
953 global_const_prop_count++;
954 if (dump_file != NULL)
956 fprintf (dump_file,
957 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with"
958 "constant ", REGNO (from), INSN_UID (jump));
959 print_rtl (dump_file, src);
960 fprintf (dump_file, "\n");
962 purge_dead_edges (bb);
964 /* If a conditional jump has been changed into unconditional jump, remove
965 the jump and make the edge fallthru - this is always called in
966 cfglayout mode. */
967 if (new_rtx != pc_rtx && simplejump_p (jump))
969 edge e;
970 edge_iterator ei;
972 FOR_EACH_EDGE (e, ei, bb->succs)
973 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
974 && BB_HEAD (e->dest) == JUMP_LABEL (jump))
976 e->flags |= EDGE_FALLTHRU;
977 break;
979 delete_insn (jump);
982 return 1;
985 /* Subroutine of cprop_insn that tries to propagate constants. FROM is what
986 we will try to replace, SRC is the constant we will try to substitute for
987 it and INSN is the instruction where this will be happening. */
989 static int
990 constprop_register (rtx from, rtx src, rtx_insn *insn)
992 rtx sset;
994 /* Check for reg or cc0 setting instructions followed by
995 conditional branch instructions first. */
996 if ((sset = single_set (insn)) != NULL
997 && NEXT_INSN (insn)
998 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
1000 rtx dest = SET_DEST (sset);
1001 if ((REG_P (dest) || CC0_P (dest))
1002 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
1003 from, src))
1004 return 1;
1007 /* Handle normal insns next. */
1008 if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
1009 return 1;
1011 /* Try to propagate a CONST_INT into a conditional jump.
1012 We're pretty specific about what we will handle in this
1013 code, we can extend this as necessary over time.
1015 Right now the insn in question must look like
1016 (set (pc) (if_then_else ...)) */
1017 else if (any_condjump_p (insn) && onlyjump_p (insn))
1018 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
1019 return 0;
1022 /* Perform constant and copy propagation on INSN.
1023 Return nonzero if a change was made. */
1025 static int
1026 cprop_insn (rtx_insn *insn)
1028 unsigned i;
1029 int changed = 0, changed_this_round;
1030 rtx note;
1032 retry:
1033 changed_this_round = 0;
1034 reg_use_count = 0;
1035 note_uses (&PATTERN (insn), find_used_regs, NULL);
1037 /* We may win even when propagating constants into notes. */
1038 note = find_reg_equal_equiv_note (insn);
1039 if (note)
1040 find_used_regs (&XEXP (note, 0), NULL);
1042 for (i = 0; i < reg_use_count; i++)
1044 rtx reg_used = reg_use_table[i];
1045 unsigned int regno = REGNO (reg_used);
1046 rtx src;
1047 struct cprop_expr *set;
1049 /* If the register has already been set in this block, there's
1050 nothing we can do. */
1051 if (! reg_not_set_p (reg_used, insn))
1052 continue;
1054 /* Find an assignment that sets reg_used and is available
1055 at the start of the block. */
1056 set = find_avail_set (regno, insn);
1057 if (! set)
1058 continue;
1060 src = set->src;
1062 /* Constant propagation. */
1063 if (cprop_constant_p (src))
1065 if (constprop_register (reg_used, src, insn))
1067 changed_this_round = changed = 1;
1068 global_const_prop_count++;
1069 if (dump_file != NULL)
1071 fprintf (dump_file,
1072 "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
1073 fprintf (dump_file, "insn %d with constant ",
1074 INSN_UID (insn));
1075 print_rtl (dump_file, src);
1076 fprintf (dump_file, "\n");
1078 if (insn->deleted ())
1079 return 1;
1082 else if (REG_P (src)
1083 && REGNO (src) >= FIRST_PSEUDO_REGISTER
1084 && REGNO (src) != regno)
1086 if (try_replace_reg (reg_used, src, insn))
1088 changed_this_round = changed = 1;
1089 global_copy_prop_count++;
1090 if (dump_file != NULL)
1092 fprintf (dump_file,
1093 "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1094 regno, INSN_UID (insn));
1095 fprintf (dump_file, " with reg %d\n", REGNO (src));
1098 /* The original insn setting reg_used may or may not now be
1099 deletable. We leave the deletion to DCE. */
1100 /* FIXME: If it turns out that the insn isn't deletable,
1101 then we may have unnecessarily extended register lifetimes
1102 and made things worse. */
1106 /* If try_replace_reg simplified the insn, the regs found
1107 by find_used_regs may not be valid anymore. Start over. */
1108 if (changed_this_round)
1109 goto retry;
1112 if (changed && DEBUG_INSN_P (insn))
1113 return 0;
1115 return changed;
1118 /* Like find_used_regs, but avoid recording uses that appear in
1119 input-output contexts such as zero_extract or pre_dec. This
1120 restricts the cases we consider to those for which local cprop
1121 can legitimately make replacements. */
1123 static void
1124 local_cprop_find_used_regs (rtx *xptr, void *data)
1126 rtx x = *xptr;
1128 if (x == 0)
1129 return;
1131 switch (GET_CODE (x))
1133 case ZERO_EXTRACT:
1134 case SIGN_EXTRACT:
1135 case STRICT_LOW_PART:
1136 return;
1138 case PRE_DEC:
1139 case PRE_INC:
1140 case POST_DEC:
1141 case POST_INC:
1142 case PRE_MODIFY:
1143 case POST_MODIFY:
1144 /* Can only legitimately appear this early in the context of
1145 stack pushes for function arguments, but handle all of the
1146 codes nonetheless. */
1147 return;
1149 case SUBREG:
1150 /* Setting a subreg of a register larger than word_mode leaves
1151 the non-written words unchanged. */
1152 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
1153 return;
1154 break;
1156 default:
1157 break;
1160 find_used_regs (xptr, data);
1163 /* Try to perform local const/copy propagation on X in INSN. */
1165 static bool
1166 do_local_cprop (rtx x, rtx_insn *insn)
1168 rtx newreg = NULL, newcnst = NULL;
1170 /* Rule out USE instructions and ASM statements as we don't want to
1171 change the hard registers mentioned. */
1172 if (REG_P (x)
1173 && (REGNO (x) >= FIRST_PSEUDO_REGISTER
1174 || (GET_CODE (PATTERN (insn)) != USE
1175 && asm_noperands (PATTERN (insn)) < 0)))
1177 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
1178 struct elt_loc_list *l;
1180 if (!val)
1181 return false;
1182 for (l = val->locs; l; l = l->next)
1184 rtx this_rtx = l->loc;
1185 rtx note;
1187 if (cprop_constant_p (this_rtx))
1188 newcnst = this_rtx;
1189 if (REG_P (this_rtx) && REGNO (this_rtx) >= FIRST_PSEUDO_REGISTER
1190 /* Don't copy propagate if it has attached REG_EQUIV note.
1191 At this point this only function parameters should have
1192 REG_EQUIV notes and if the argument slot is used somewhere
1193 explicitly, it means address of parameter has been taken,
1194 so we should not extend the lifetime of the pseudo. */
1195 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
1196 || ! MEM_P (XEXP (note, 0))))
1197 newreg = this_rtx;
1199 if (newcnst && constprop_register (x, newcnst, insn))
1201 if (dump_file != NULL)
1203 fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
1204 REGNO (x));
1205 fprintf (dump_file, "insn %d with constant ",
1206 INSN_UID (insn));
1207 print_rtl (dump_file, newcnst);
1208 fprintf (dump_file, "\n");
1210 local_const_prop_count++;
1211 return true;
1213 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
1215 if (dump_file != NULL)
1217 fprintf (dump_file,
1218 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
1219 REGNO (x), INSN_UID (insn));
1220 fprintf (dump_file, " with reg %d\n", REGNO (newreg));
1222 local_copy_prop_count++;
1223 return true;
1226 return false;
1229 /* Do local const/copy propagation (i.e. within each basic block). */
1231 static int
1232 local_cprop_pass (void)
1234 basic_block bb;
1235 rtx_insn *insn;
1236 bool changed = false;
1237 unsigned i;
1239 cselib_init (0);
1240 FOR_EACH_BB_FN (bb, cfun)
1242 FOR_BB_INSNS (bb, insn)
1244 if (INSN_P (insn))
1246 rtx note = find_reg_equal_equiv_note (insn);
1249 reg_use_count = 0;
1250 note_uses (&PATTERN (insn), local_cprop_find_used_regs,
1251 NULL);
1252 if (note)
1253 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
1255 for (i = 0; i < reg_use_count; i++)
1257 if (do_local_cprop (reg_use_table[i], insn))
1259 if (!DEBUG_INSN_P (insn))
1260 changed = true;
1261 break;
1264 if (insn->deleted ())
1265 break;
1267 while (i < reg_use_count);
1269 cselib_process_insn (insn);
1272 /* Forget everything at the end of a basic block. */
1273 cselib_clear_table ();
1276 cselib_finish ();
1278 return changed;
1281 /* Similar to get_condition, only the resulting condition must be
1282 valid at JUMP, instead of at EARLIEST.
1284 This differs from noce_get_condition in ifcvt.c in that we prefer not to
1285 settle for the condition variable in the jump instruction being integral.
1286 We prefer to be able to record the value of a user variable, rather than
1287 the value of a temporary used in a condition. This could be solved by
1288 recording the value of *every* register scanned by canonicalize_condition,
1289 but this would require some code reorganization. */
1292 fis_get_condition (rtx_insn *jump)
1294 return get_condition (jump, NULL, false, true);
1297 /* Check the comparison COND to see if we can safely form an implicit
1298 set from it. */
1300 static bool
1301 implicit_set_cond_p (const_rtx cond)
1303 enum machine_mode mode;
1304 rtx cst;
1306 /* COND must be either an EQ or NE comparison. */
1307 if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
1308 return false;
1310 /* The first operand of COND must be a pseudo-reg. */
1311 if (! REG_P (XEXP (cond, 0))
1312 || HARD_REGISTER_P (XEXP (cond, 0)))
1313 return false;
1315 /* The second operand of COND must be a suitable constant. */
1316 mode = GET_MODE (XEXP (cond, 0));
1317 cst = XEXP (cond, 1);
1319 /* We can't perform this optimization if either operand might be or might
1320 contain a signed zero. */
1321 if (HONOR_SIGNED_ZEROS (mode))
1323 /* It is sufficient to check if CST is or contains a zero. We must
1324 handle float, complex, and vector. If any subpart is a zero, then
1325 the optimization can't be performed. */
1326 /* ??? The complex and vector checks are not implemented yet. We just
1327 always return zero for them. */
1328 if (CONST_DOUBLE_AS_FLOAT_P (cst))
1330 REAL_VALUE_TYPE d;
1331 REAL_VALUE_FROM_CONST_DOUBLE (d, cst);
1332 if (REAL_VALUES_EQUAL (d, dconst0))
1333 return 0;
1335 else
1336 return 0;
1339 return cprop_constant_p (cst);
1342 /* Find the implicit sets of a function. An "implicit set" is a constraint
1343 on the value of a variable, implied by a conditional jump. For example,
1344 following "if (x == 2)", the then branch may be optimized as though the
1345 conditional performed an "explicit set", in this example, "x = 2". This
1346 function records the set patterns that are implicit at the start of each
1347 basic block.
1349 If an implicit set is found but the set is implicit on a critical edge,
1350 this critical edge is split.
1352 Return true if the CFG was modified, false otherwise. */
1354 static bool
1355 find_implicit_sets (void)
1357 basic_block bb, dest;
1358 rtx cond, new_rtx;
1359 unsigned int count = 0;
1360 bool edges_split = false;
1361 size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10;
1363 implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1365 FOR_EACH_BB_FN (bb, cfun)
1367 /* Check for more than one successor. */
1368 if (EDGE_COUNT (bb->succs) <= 1)
1369 continue;
1371 cond = fis_get_condition (BB_END (bb));
1373 /* If no condition is found or if it isn't of a suitable form,
1374 ignore it. */
1375 if (! cond || ! implicit_set_cond_p (cond))
1376 continue;
1378 dest = GET_CODE (cond) == EQ
1379 ? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
1381 /* If DEST doesn't go anywhere, ignore it. */
1382 if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1383 continue;
1385 /* We have found a suitable implicit set. Try to record it now as
1386 a SET in DEST. If DEST has more than one predecessor, the edge
1387 between BB and DEST is a critical edge and we must split it,
1388 because we can only record one implicit set per DEST basic block. */
1389 if (! single_pred_p (dest))
1391 dest = split_edge (find_edge (bb, dest));
1392 edges_split = true;
1395 if (implicit_sets_size <= (size_t) dest->index)
1397 size_t old_implicit_sets_size = implicit_sets_size;
1398 implicit_sets_size *= 2;
1399 implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
1400 memset (implicit_sets + old_implicit_sets_size, 0,
1401 (implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1404 new_rtx = gen_rtx_SET (VOIDmode, XEXP (cond, 0),
1405 XEXP (cond, 1));
1406 implicit_sets[dest->index] = new_rtx;
1407 if (dump_file)
1409 fprintf (dump_file, "Implicit set of reg %d in ",
1410 REGNO (XEXP (cond, 0)));
1411 fprintf (dump_file, "basic block %d\n", dest->index);
1413 count++;
1416 if (dump_file)
1417 fprintf (dump_file, "Found %d implicit sets\n", count);
1419 /* Confess our sins. */
1420 return edges_split;
1423 /* Bypass conditional jumps. */
1425 /* The value of last_basic_block at the beginning of the jump_bypass
1426 pass. The use of redirect_edge_and_branch_force may introduce new
1427 basic blocks, but the data flow analysis is only valid for basic
1428 block indices less than bypass_last_basic_block. */
1430 static int bypass_last_basic_block;
1432 /* Find a set of REGNO to a constant that is available at the end of basic
1433 block BB. Return NULL if no such set is found. Based heavily upon
1434 find_avail_set. */
1436 static struct cprop_expr *
1437 find_bypass_set (int regno, int bb)
1439 struct cprop_expr *result = 0;
1441 for (;;)
1443 rtx src;
1444 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
1446 while (set)
1448 if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
1449 break;
1450 set = next_set (regno, set);
1453 if (set == 0)
1454 break;
1456 src = set->src;
1457 if (cprop_constant_p (src))
1458 result = set;
1460 if (! REG_P (src))
1461 break;
1463 regno = REGNO (src);
1465 return result;
1468 /* Subroutine of bypass_block that checks whether a pseudo is killed by
1469 any of the instructions inserted on an edge. Jump bypassing places
1470 condition code setters on CFG edges using insert_insn_on_edge. This
1471 function is required to check that our data flow analysis is still
1472 valid prior to commit_edge_insertions. */
1474 static bool
1475 reg_killed_on_edge (const_rtx reg, const_edge e)
1477 rtx_insn *insn;
1479 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
1480 if (INSN_P (insn) && reg_set_p (reg, insn))
1481 return true;
1483 return false;
1486 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
1487 basic block BB which has more than one predecessor. If not NULL, SETCC
1488 is the first instruction of BB, which is immediately followed by JUMP_INSN
1489 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
1490 Returns nonzero if a change was made.
1492 During the jump bypassing pass, we may place copies of SETCC instructions
1493 on CFG edges. The following routine must be careful to pay attention to
1494 these inserted insns when performing its transformations. */
1496 static int
1497 bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump)
1499 rtx_insn *insn;
1500 rtx note;
1501 edge e, edest;
1502 int change;
1503 int may_be_loop_header = false;
1504 unsigned removed_p;
1505 unsigned i;
1506 edge_iterator ei;
1508 insn = (setcc != NULL) ? setcc : jump;
1510 /* Determine set of register uses in INSN. */
1511 reg_use_count = 0;
1512 note_uses (&PATTERN (insn), find_used_regs, NULL);
1513 note = find_reg_equal_equiv_note (insn);
1514 if (note)
1515 find_used_regs (&XEXP (note, 0), NULL);
1517 if (current_loops)
1519 /* If we are to preserve loop structure then do not bypass
1520 a loop header. This will either rotate the loop, create
1521 multiple entry loops or even irreducible regions. */
1522 if (bb == bb->loop_father->header)
1523 return 0;
1525 else
1527 FOR_EACH_EDGE (e, ei, bb->preds)
1528 if (e->flags & EDGE_DFS_BACK)
1530 may_be_loop_header = true;
1531 break;
1535 change = 0;
1536 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1538 removed_p = 0;
1540 if (e->flags & EDGE_COMPLEX)
1542 ei_next (&ei);
1543 continue;
1546 /* We can't redirect edges from new basic blocks. */
1547 if (e->src->index >= bypass_last_basic_block)
1549 ei_next (&ei);
1550 continue;
1553 /* The irreducible loops created by redirecting of edges entering the
1554 loop from outside would decrease effectiveness of some of the
1555 following optimizations, so prevent this. */
1556 if (may_be_loop_header
1557 && !(e->flags & EDGE_DFS_BACK))
1559 ei_next (&ei);
1560 continue;
1563 for (i = 0; i < reg_use_count; i++)
1565 rtx reg_used = reg_use_table[i];
1566 unsigned int regno = REGNO (reg_used);
1567 basic_block dest, old_dest;
1568 struct cprop_expr *set;
1569 rtx src, new_rtx;
1571 set = find_bypass_set (regno, e->src->index);
1573 if (! set)
1574 continue;
1576 /* Check the data flow is valid after edge insertions. */
1577 if (e->insns.r && reg_killed_on_edge (reg_used, e))
1578 continue;
1580 src = SET_SRC (pc_set (jump));
1582 if (setcc != NULL)
1583 src = simplify_replace_rtx (src,
1584 SET_DEST (PATTERN (setcc)),
1585 SET_SRC (PATTERN (setcc)));
1587 new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1589 /* Jump bypassing may have already placed instructions on
1590 edges of the CFG. We can't bypass an outgoing edge that
1591 has instructions associated with it, as these insns won't
1592 get executed if the incoming edge is redirected. */
1593 if (new_rtx == pc_rtx)
1595 edest = FALLTHRU_EDGE (bb);
1596 dest = edest->insns.r ? NULL : edest->dest;
1598 else if (GET_CODE (new_rtx) == LABEL_REF)
1600 dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
1601 /* Don't bypass edges containing instructions. */
1602 edest = find_edge (bb, dest);
1603 if (edest && edest->insns.r)
1604 dest = NULL;
1606 else
1607 dest = NULL;
1609 /* Avoid unification of the edge with other edges from original
1610 branch. We would end up emitting the instruction on "both"
1611 edges. */
1612 if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
1613 && find_edge (e->src, dest))
1614 dest = NULL;
1616 old_dest = e->dest;
1617 if (dest != NULL
1618 && dest != old_dest
1619 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1621 redirect_edge_and_branch_force (e, dest);
1623 /* Copy the register setter to the redirected edge.
1624 Don't copy CC0 setters, as CC0 is dead after jump. */
1625 if (setcc)
1627 rtx pat = PATTERN (setcc);
1628 if (!CC0_P (SET_DEST (pat)))
1629 insert_insn_on_edge (copy_insn (pat), e);
1632 if (dump_file != NULL)
1634 fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
1635 "in jump_insn %d equals constant ",
1636 regno, INSN_UID (jump));
1637 print_rtl (dump_file, set->src);
1638 fprintf (dump_file, "\n\t when BB %d is entered from "
1639 "BB %d. Redirect edge %d->%d to %d.\n",
1640 old_dest->index, e->src->index, e->src->index,
1641 old_dest->index, dest->index);
1643 change = 1;
1644 removed_p = 1;
1645 break;
1648 if (!removed_p)
1649 ei_next (&ei);
1651 return change;
1654 /* Find basic blocks with more than one predecessor that only contain a
1655 single conditional jump. If the result of the comparison is known at
1656 compile-time from any incoming edge, redirect that edge to the
1657 appropriate target. Return nonzero if a change was made.
1659 This function is now mis-named, because we also handle indirect jumps. */
1661 static int
1662 bypass_conditional_jumps (void)
1664 basic_block bb;
1665 int changed;
1666 rtx_insn *setcc;
1667 rtx_insn *insn;
1668 rtx dest;
1670 /* Note we start at block 1. */
1671 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1672 return 0;
1674 bypass_last_basic_block = last_basic_block_for_fn (cfun);
1675 mark_dfs_back_edges ();
1677 changed = 0;
1678 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1679 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1681 /* Check for more than one predecessor. */
1682 if (!single_pred_p (bb))
1684 setcc = NULL;
1685 FOR_BB_INSNS (bb, insn)
1686 if (DEBUG_INSN_P (insn))
1687 continue;
1688 else if (NONJUMP_INSN_P (insn))
1690 if (setcc)
1691 break;
1692 if (GET_CODE (PATTERN (insn)) != SET)
1693 break;
1695 dest = SET_DEST (PATTERN (insn));
1696 if (REG_P (dest) || CC0_P (dest))
1697 setcc = insn;
1698 else
1699 break;
1701 else if (JUMP_P (insn))
1703 if ((any_condjump_p (insn) || computed_jump_p (insn))
1704 && onlyjump_p (insn))
1705 changed |= bypass_block (bb, setcc, insn);
1706 break;
1708 else if (INSN_P (insn))
1709 break;
1713 /* If we bypassed any register setting insns, we inserted a
1714 copy on the redirected edge. These need to be committed. */
1715 if (changed)
1716 commit_edge_insertions ();
1718 return changed;
1721 /* Return true if the graph is too expensive to optimize. PASS is the
1722 optimization about to be performed. */
1724 static bool
1725 is_too_expensive (const char *pass)
1727 /* Trying to perform global optimizations on flow graphs which have
1728 a high connectivity will take a long time and is unlikely to be
1729 particularly useful.
1731 In normal circumstances a cfg should have about twice as many
1732 edges as blocks. But we do not want to punish small functions
1733 which have a couple switch statements. Rather than simply
1734 threshold the number of blocks, uses something with a more
1735 graceful degradation. */
1736 if (n_edges_for_fn (cfun) > 20000 + n_basic_blocks_for_fn (cfun) * 4)
1738 warning (OPT_Wdisabled_optimization,
1739 "%s: %d basic blocks and %d edges/basic block",
1740 pass, n_basic_blocks_for_fn (cfun),
1741 n_edges_for_fn (cfun) / n_basic_blocks_for_fn (cfun));
1743 return true;
1746 /* If allocating memory for the cprop bitmap would take up too much
1747 storage it's better just to disable the optimization. */
1748 if ((n_basic_blocks_for_fn (cfun)
1749 * SBITMAP_SET_SIZE (max_reg_num ())
1750 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
1752 warning (OPT_Wdisabled_optimization,
1753 "%s: %d basic blocks and %d registers",
1754 pass, n_basic_blocks_for_fn (cfun), max_reg_num ());
1756 return true;
1759 return false;
1762 /* Main function for the CPROP pass. */
1764 static int
1765 one_cprop_pass (void)
1767 int i;
1768 int changed = 0;
1770 /* Return if there's nothing to do, or it is too expensive. */
1771 if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
1772 || is_too_expensive (_ ("const/copy propagation disabled")))
1773 return 0;
1775 global_const_prop_count = local_const_prop_count = 0;
1776 global_copy_prop_count = local_copy_prop_count = 0;
1778 bytes_used = 0;
1779 gcc_obstack_init (&cprop_obstack);
1781 /* Do a local const/copy propagation pass first. The global pass
1782 only handles global opportunities.
1783 If the local pass changes something, remove any unreachable blocks
1784 because the CPROP global dataflow analysis may get into infinite
1785 loops for CFGs with unreachable blocks.
1787 FIXME: This local pass should not be necessary after CSE (but for
1788 some reason it still is). It is also (proven) not necessary
1789 to run the local pass right after FWPWOP.
1791 FIXME: The global analysis would not get into infinite loops if it
1792 would use the DF solver (via df_simple_dataflow) instead of
1793 the solver implemented in this file. */
1794 changed |= local_cprop_pass ();
1795 if (changed)
1796 delete_unreachable_blocks ();
1798 /* Determine implicit sets. This may change the CFG (split critical
1799 edges if that exposes an implicit set).
1800 Note that find_implicit_sets() does not rely on up-to-date DF caches
1801 so that we do not have to re-run df_analyze() even if local CPROP
1802 changed something.
1803 ??? This could run earlier so that any uncovered implicit sets
1804 sets could be exploited in local_cprop_pass() also. Later. */
1805 changed |= find_implicit_sets ();
1807 /* If local_cprop_pass() or find_implicit_sets() changed something,
1808 run df_analyze() to bring all insn caches up-to-date, and to take
1809 new basic blocks from edge splitting on the DF radar.
1810 NB: This also runs the fast DCE pass, because execute_rtl_cprop
1811 sets DF_LR_RUN_DCE. */
1812 if (changed)
1813 df_analyze ();
1815 /* Initialize implicit_set_indexes array. */
1816 implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun));
1817 for (i = 0; i < last_basic_block_for_fn (cfun); i++)
1818 implicit_set_indexes[i] = -1;
1820 alloc_hash_table (&set_hash_table);
1821 compute_hash_table (&set_hash_table);
1823 /* Free implicit_sets before peak usage. */
1824 free (implicit_sets);
1825 implicit_sets = NULL;
1827 if (dump_file)
1828 dump_hash_table (dump_file, "SET", &set_hash_table);
1829 if (set_hash_table.n_elems > 0)
1831 basic_block bb;
1832 rtx_insn *insn;
1834 alloc_cprop_mem (last_basic_block_for_fn (cfun),
1835 set_hash_table.n_elems);
1836 compute_cprop_data ();
1838 free (implicit_set_indexes);
1839 implicit_set_indexes = NULL;
1841 /* Allocate vars to track sets of regs. */
1842 reg_set_bitmap = ALLOC_REG_SET (NULL);
1844 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1845 EXIT_BLOCK_PTR_FOR_FN (cfun),
1846 next_bb)
1848 /* Reset tables used to keep track of what's still valid [since
1849 the start of the block]. */
1850 reset_opr_set_tables ();
1852 FOR_BB_INSNS (bb, insn)
1853 if (INSN_P (insn))
1855 changed |= cprop_insn (insn);
1857 /* Keep track of everything modified by this insn. */
1858 /* ??? Need to be careful w.r.t. mods done to INSN.
1859 Don't call mark_oprs_set if we turned the
1860 insn into a NOTE, or deleted the insn. */
1861 if (! NOTE_P (insn) && ! insn->deleted ())
1862 mark_oprs_set (insn);
1866 changed |= bypass_conditional_jumps ();
1868 FREE_REG_SET (reg_set_bitmap);
1869 free_cprop_mem ();
1871 else
1873 free (implicit_set_indexes);
1874 implicit_set_indexes = NULL;
1877 free_hash_table (&set_hash_table);
1878 obstack_free (&cprop_obstack, NULL);
1880 if (dump_file)
1882 fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1883 current_function_name (), n_basic_blocks_for_fn (cfun),
1884 bytes_used);
1885 fprintf (dump_file, "%d local const props, %d local copy props, ",
1886 local_const_prop_count, local_copy_prop_count);
1887 fprintf (dump_file, "%d global const props, %d global copy props\n\n",
1888 global_const_prop_count, global_copy_prop_count);
1891 return changed;
1894 /* All the passes implemented in this file. Each pass has its
1895 own gate and execute function, and at the end of the file a
1896 pass definition for passes.c.
1898 We do not construct an accurate cfg in functions which call
1899 setjmp, so none of these passes runs if the function calls
1900 setjmp.
1901 FIXME: Should just handle setjmp via REG_SETJMP notes. */
1903 static unsigned int
1904 execute_rtl_cprop (void)
1906 int changed;
1907 delete_unreachable_blocks ();
1908 df_set_flags (DF_LR_RUN_DCE);
1909 df_analyze ();
1910 changed = one_cprop_pass ();
1911 flag_rerun_cse_after_global_opts |= changed;
1912 if (changed)
1913 cleanup_cfg (CLEANUP_CFG_CHANGED);
1914 return 0;
1917 namespace {
1919 const pass_data pass_data_rtl_cprop =
1921 RTL_PASS, /* type */
1922 "cprop", /* name */
1923 OPTGROUP_NONE, /* optinfo_flags */
1924 TV_CPROP, /* tv_id */
1925 PROP_cfglayout, /* properties_required */
1926 0, /* properties_provided */
1927 0, /* properties_destroyed */
1928 0, /* todo_flags_start */
1929 TODO_df_finish, /* todo_flags_finish */
1932 class pass_rtl_cprop : public rtl_opt_pass
1934 public:
1935 pass_rtl_cprop (gcc::context *ctxt)
1936 : rtl_opt_pass (pass_data_rtl_cprop, ctxt)
1939 /* opt_pass methods: */
1940 opt_pass * clone () { return new pass_rtl_cprop (m_ctxt); }
1941 virtual bool gate (function *fun)
1943 return optimize > 0 && flag_gcse
1944 && !fun->calls_setjmp
1945 && dbg_cnt (cprop);
1948 virtual unsigned int execute (function *) { return execute_rtl_cprop (); }
1950 }; // class pass_rtl_cprop
1952 } // anon namespace
1954 rtl_opt_pass *
1955 make_pass_rtl_cprop (gcc::context *ctxt)
1957 return new pass_rtl_cprop (ctxt);