* gcc.dg/vect/vect-outer-simd-1.c: Remove cleanup-tree-dump directive.
[official-gcc.git] / gcc / cprop.c
blob41ca20124edd418f59a6a458ebee7085338f9f83
1 /* Global constant/copy propagation for RTL.
2 Copyright (C) 1997-2015 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"
26 #include "rtl.h"
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "tm_p.h"
38 #include "regs.h"
39 #include "hard-reg-set.h"
40 #include "flags.h"
41 #include "insn-config.h"
42 #include "recog.h"
43 #include "predict.h"
44 #include "hashtab.h"
45 #include "function.h"
46 #include "dominance.h"
47 #include "cfg.h"
48 #include "cfgrtl.h"
49 #include "cfganal.h"
50 #include "lcm.h"
51 #include "cfgcleanup.h"
52 #include "basic-block.h"
53 #include "statistics.h"
54 #include "real.h"
55 #include "fixed-value.h"
56 #include "expmed.h"
57 #include "dojump.h"
58 #include "explow.h"
59 #include "calls.h"
60 #include "emit-rtl.h"
61 #include "varasm.h"
62 #include "stmt.h"
63 #include "expr.h"
64 #include "except.h"
65 #include "params.h"
66 #include "alloc-pool.h"
67 #include "cselib.h"
68 #include "intl.h"
69 #include "obstack.h"
70 #include "tree-pass.h"
71 #include "df.h"
72 #include "dbgcnt.h"
73 #include "target.h"
74 #include "cfgloop.h"
77 /* An obstack for our working variables. */
78 static struct obstack cprop_obstack;
80 /* Occurrence of an expression.
81 There is one per basic block. If a pattern appears more than once the
82 last appearance is used. */
84 struct cprop_occr
86 /* Next occurrence of this expression. */
87 struct cprop_occr *next;
88 /* The insn that computes the expression. */
89 rtx_insn *insn;
92 typedef struct cprop_occr *occr_t;
94 /* Hash table entry for assignment expressions. */
96 struct cprop_expr
98 /* The expression (DEST := SRC). */
99 rtx dest;
100 rtx src;
102 /* Index in the available expression bitmaps. */
103 int bitmap_index;
104 /* Next entry with the same hash. */
105 struct cprop_expr *next_same_hash;
106 /* List of available occurrence in basic blocks in the function.
107 An "available occurrence" is one that is the last occurrence in the
108 basic block and whose operands are not modified by following statements
109 in the basic block [including this insn]. */
110 struct cprop_occr *avail_occr;
113 /* Hash table for copy propagation expressions.
114 Each hash table is an array of buckets.
115 ??? It is known that if it were an array of entries, structure elements
116 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
117 not clear whether in the final analysis a sufficient amount of memory would
118 be saved as the size of the available expression bitmaps would be larger
119 [one could build a mapping table without holes afterwards though].
120 Someday I'll perform the computation and figure it out. */
122 struct hash_table_d
124 /* The table itself.
125 This is an array of `set_hash_table_size' elements. */
126 struct cprop_expr **table;
128 /* Size of the hash table, in elements. */
129 unsigned int size;
131 /* Number of hash table elements. */
132 unsigned int n_elems;
135 /* Copy propagation hash table. */
136 static struct hash_table_d set_hash_table;
138 /* Array of implicit set patterns indexed by basic block index. */
139 static rtx *implicit_sets;
141 /* Array of indexes of expressions for implicit set patterns indexed by basic
142 block index. In other words, implicit_set_indexes[i] is the bitmap_index
143 of the expression whose RTX is implicit_sets[i]. */
144 static int *implicit_set_indexes;
146 /* Bitmap containing one bit for each register in the program.
147 Used when performing GCSE to track which registers have been set since
148 the start or end of the basic block while traversing that block. */
149 static regset reg_set_bitmap;
151 /* Various variables for statistics gathering. */
153 /* Memory used in a pass.
154 This isn't intended to be absolutely precise. Its intent is only
155 to keep an eye on memory usage. */
156 static int bytes_used;
158 /* Number of local constants propagated. */
159 static int local_const_prop_count;
160 /* Number of local copies propagated. */
161 static int local_copy_prop_count;
162 /* Number of global constants propagated. */
163 static int global_const_prop_count;
164 /* Number of global copies propagated. */
165 static int global_copy_prop_count;
167 #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T)))
168 #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S)))
170 /* Cover function to obstack_alloc. */
172 static void *
173 cprop_alloc (unsigned long size)
175 bytes_used += size;
176 return obstack_alloc (&cprop_obstack, size);
179 /* Return nonzero if register X is unchanged from INSN to the end
180 of INSN's basic block. */
182 static int
183 reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
185 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
188 /* Hash a set of register REGNO.
190 Sets are hashed on the register that is set. This simplifies the PRE copy
191 propagation code.
193 ??? May need to make things more elaborate. Later, as necessary. */
195 static unsigned int
196 hash_mod (int regno, int hash_table_size)
198 return (unsigned) regno % hash_table_size;
201 /* Insert assignment DEST:=SET from INSN in the hash table.
202 DEST is a register and SET is a register or a suitable constant.
203 If the assignment is already present in the table, record it as
204 the last occurrence in INSN's basic block.
205 IMPLICIT is true if it's an implicit set, false otherwise. */
207 static void
208 insert_set_in_table (rtx dest, rtx src, rtx_insn *insn,
209 struct hash_table_d *table, bool implicit)
211 bool found = false;
212 unsigned int hash;
213 struct cprop_expr *cur_expr, *last_expr = NULL;
214 struct cprop_occr *cur_occr;
216 hash = hash_mod (REGNO (dest), table->size);
218 for (cur_expr = table->table[hash]; cur_expr;
219 cur_expr = cur_expr->next_same_hash)
221 if (dest == cur_expr->dest
222 && src == cur_expr->src)
224 found = true;
225 break;
227 last_expr = cur_expr;
230 if (! found)
232 cur_expr = GOBNEW (struct cprop_expr);
233 bytes_used += sizeof (struct cprop_expr);
234 if (table->table[hash] == NULL)
235 /* This is the first pattern that hashed to this index. */
236 table->table[hash] = cur_expr;
237 else
238 /* Add EXPR to end of this hash chain. */
239 last_expr->next_same_hash = cur_expr;
241 /* Set the fields of the expr element.
242 We must copy X because it can be modified when copy propagation is
243 performed on its operands. */
244 cur_expr->dest = copy_rtx (dest);
245 cur_expr->src = copy_rtx (src);
246 cur_expr->bitmap_index = table->n_elems++;
247 cur_expr->next_same_hash = NULL;
248 cur_expr->avail_occr = NULL;
251 /* Now record the occurrence. */
252 cur_occr = cur_expr->avail_occr;
254 if (cur_occr
255 && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
257 /* Found another instance of the expression in the same basic block.
258 Prefer this occurrence to the currently recorded one. We want
259 the last one in the block and the block is scanned from start
260 to end. */
261 cur_occr->insn = insn;
263 else
265 /* First occurrence of this expression in this basic block. */
266 cur_occr = GOBNEW (struct cprop_occr);
267 bytes_used += sizeof (struct cprop_occr);
268 cur_occr->insn = insn;
269 cur_occr->next = cur_expr->avail_occr;
270 cur_expr->avail_occr = cur_occr;
273 /* Record bitmap_index of the implicit set in implicit_set_indexes. */
274 if (implicit)
275 implicit_set_indexes[BLOCK_FOR_INSN (insn)->index]
276 = cur_expr->bitmap_index;
279 /* Determine whether the rtx X should be treated as a constant for CPROP.
280 Since X might be inserted more than once we have to take care that it
281 is sharable. */
283 static bool
284 cprop_constant_p (const_rtx x)
286 return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
289 /* Determine whether the rtx X should be treated as a register that can
290 be propagated. Any pseudo-register is fine. */
292 static bool
293 cprop_reg_p (const_rtx x)
295 return REG_P (x) && !HARD_REGISTER_P (x);
298 /* Scan SET present in INSN and add an entry to the hash TABLE.
299 IMPLICIT is true if it's an implicit set, false otherwise. */
301 static void
302 hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table,
303 bool implicit)
305 rtx src = SET_SRC (set);
306 rtx dest = SET_DEST (set);
308 if (cprop_reg_p (dest)
309 && reg_available_p (dest, insn)
310 && can_copy_p (GET_MODE (dest)))
312 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
314 This allows us to do a single CPROP pass and still eliminate
315 redundant constants, addresses or other expressions that are
316 constructed with multiple instructions.
318 However, keep the original SRC if INSN is a simple reg-reg move. In
319 In this case, there will almost always be a REG_EQUAL note on the
320 insn that sets SRC. By recording the REG_EQUAL value here as SRC
321 for INSN, we miss copy propagation opportunities.
323 Note that this does not impede profitable constant propagations. We
324 "look through" reg-reg sets in lookup_set. */
325 rtx note = find_reg_equal_equiv_note (insn);
326 if (note != 0
327 && REG_NOTE_KIND (note) == REG_EQUAL
328 && !REG_P (src)
329 && cprop_constant_p (XEXP (note, 0)))
330 src = XEXP (note, 0), set = gen_rtx_SET (dest, src);
332 /* Record sets for constant/copy propagation. */
333 if ((cprop_reg_p (src)
334 && src != dest
335 && reg_available_p (src, insn))
336 || cprop_constant_p (src))
337 insert_set_in_table (dest, src, insn, table, implicit);
341 /* Process INSN and add hash table entries as appropriate. */
343 static void
344 hash_scan_insn (rtx_insn *insn, struct hash_table_d *table)
346 rtx pat = PATTERN (insn);
347 int i;
349 /* Pick out the sets of INSN and for other forms of instructions record
350 what's been modified. */
352 if (GET_CODE (pat) == SET)
353 hash_scan_set (pat, insn, table, false);
354 else if (GET_CODE (pat) == PARALLEL)
355 for (i = 0; i < XVECLEN (pat, 0); i++)
357 rtx x = XVECEXP (pat, 0, i);
359 if (GET_CODE (x) == SET)
360 hash_scan_set (x, insn, table, false);
364 /* Dump the hash table TABLE to file FILE under the name NAME. */
366 static void
367 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
369 int i;
370 /* Flattened out table, so it's printed in proper order. */
371 struct cprop_expr **flat_table;
372 unsigned int *hash_val;
373 struct cprop_expr *expr;
375 flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems);
376 hash_val = XNEWVEC (unsigned int, table->n_elems);
378 for (i = 0; i < (int) table->size; i++)
379 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
381 flat_table[expr->bitmap_index] = expr;
382 hash_val[expr->bitmap_index] = i;
385 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
386 name, table->size, table->n_elems);
388 for (i = 0; i < (int) table->n_elems; i++)
389 if (flat_table[i] != 0)
391 expr = flat_table[i];
392 fprintf (file, "Index %d (hash value %d)\n ",
393 expr->bitmap_index, hash_val[i]);
394 print_rtl (file, expr->dest);
395 fprintf (file, " := ");
396 print_rtl (file, expr->src);
397 fprintf (file, "\n");
400 fprintf (file, "\n");
402 free (flat_table);
403 free (hash_val);
406 /* Record as unavailable all registers that are DEF operands of INSN. */
408 static void
409 make_set_regs_unavailable (rtx_insn *insn)
411 df_ref def;
413 FOR_EACH_INSN_DEF (def, insn)
414 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
417 /* Top level function to create an assignment hash table.
419 Assignment entries are placed in the hash table if
420 - they are of the form (set (pseudo-reg) src),
421 - src is something we want to perform const/copy propagation on,
422 - none of the operands or target are subsequently modified in the block
424 Currently src must be a pseudo-reg or a const_int.
426 TABLE is the table computed. */
428 static void
429 compute_hash_table_work (struct hash_table_d *table)
431 basic_block bb;
433 /* Allocate vars to track sets of regs. */
434 reg_set_bitmap = ALLOC_REG_SET (NULL);
436 FOR_EACH_BB_FN (bb, cfun)
438 rtx_insn *insn;
440 /* Reset tables used to keep track of what's not yet invalid [since
441 the end of the block]. */
442 CLEAR_REG_SET (reg_set_bitmap);
444 /* Go over all insns from the last to the first. This is convenient
445 for tracking available registers, i.e. not set between INSN and
446 the end of the basic block BB. */
447 FOR_BB_INSNS_REVERSE (bb, insn)
449 /* Only real insns are interesting. */
450 if (!NONDEBUG_INSN_P (insn))
451 continue;
453 /* Record interesting sets from INSN in the hash table. */
454 hash_scan_insn (insn, table);
456 /* Any registers set in INSN will make SETs above it not AVAIL. */
457 make_set_regs_unavailable (insn);
460 /* Insert implicit sets in the hash table, pretending they appear as
461 insns at the head of the basic block. */
462 if (implicit_sets[bb->index] != NULL_RTX)
463 hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
466 FREE_REG_SET (reg_set_bitmap);
469 /* Allocate space for the set/expr hash TABLE.
470 It is used to determine the number of buckets to use. */
472 static void
473 alloc_hash_table (struct hash_table_d *table)
475 int n;
477 n = get_max_insn_count ();
479 table->size = n / 4;
480 if (table->size < 11)
481 table->size = 11;
483 /* Attempt to maintain efficient use of hash table.
484 Making it an odd number is simplest for now.
485 ??? Later take some measurements. */
486 table->size |= 1;
487 n = table->size * sizeof (struct cprop_expr *);
488 table->table = XNEWVAR (struct cprop_expr *, n);
491 /* Free things allocated by alloc_hash_table. */
493 static void
494 free_hash_table (struct hash_table_d *table)
496 free (table->table);
499 /* Compute the hash TABLE for doing copy/const propagation or
500 expression hash table. */
502 static void
503 compute_hash_table (struct hash_table_d *table)
505 /* Initialize count of number of entries in hash table. */
506 table->n_elems = 0;
507 memset (table->table, 0, table->size * sizeof (struct cprop_expr *));
509 compute_hash_table_work (table);
512 /* Expression tracking support. */
514 /* Lookup REGNO in the set TABLE. The result is a pointer to the
515 table entry, or NULL if not found. */
517 static struct cprop_expr *
518 lookup_set (unsigned int regno, struct hash_table_d *table)
520 unsigned int hash = hash_mod (regno, table->size);
521 struct cprop_expr *expr;
523 expr = table->table[hash];
525 while (expr && REGNO (expr->dest) != regno)
526 expr = expr->next_same_hash;
528 return expr;
531 /* Return the next entry for REGNO in list EXPR. */
533 static struct cprop_expr *
534 next_set (unsigned int regno, struct cprop_expr *expr)
537 expr = expr->next_same_hash;
538 while (expr && REGNO (expr->dest) != regno);
540 return expr;
543 /* Reset tables used to keep track of what's still available [since the
544 start of the block]. */
546 static void
547 reset_opr_set_tables (void)
549 /* Maintain a bitmap of which regs have been set since beginning of
550 the block. */
551 CLEAR_REG_SET (reg_set_bitmap);
554 /* Return nonzero if the register X has not been set yet [since the
555 start of the basic block containing INSN]. */
557 static int
558 reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
560 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
563 /* Record things set by INSN.
564 This data is used by reg_not_set_p. */
566 static void
567 mark_oprs_set (rtx_insn *insn)
569 df_ref def;
571 FOR_EACH_INSN_DEF (def, insn)
572 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
575 /* Compute copy/constant propagation working variables. */
577 /* Local properties of assignments. */
578 static sbitmap *cprop_avloc;
579 static sbitmap *cprop_kill;
581 /* Global properties of assignments (computed from the local properties). */
582 static sbitmap *cprop_avin;
583 static sbitmap *cprop_avout;
585 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
586 basic blocks. N_SETS is the number of sets. */
588 static void
589 alloc_cprop_mem (int n_blocks, int n_sets)
591 cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
592 cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
594 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
595 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
598 /* Free vars used by copy/const propagation. */
600 static void
601 free_cprop_mem (void)
603 sbitmap_vector_free (cprop_avloc);
604 sbitmap_vector_free (cprop_kill);
605 sbitmap_vector_free (cprop_avin);
606 sbitmap_vector_free (cprop_avout);
609 /* Compute the local properties of each recorded expression.
611 Local properties are those that are defined by the block, irrespective of
612 other blocks.
614 An expression is killed in a block if its operands, either DEST or SRC, are
615 modified in the block.
617 An expression is computed (locally available) in a block if it is computed
618 at least once and expression would contain the same value if the
619 computation was moved to the end of the block.
621 KILL and COMP are destination sbitmaps for recording local properties. */
623 static void
624 compute_local_properties (sbitmap *kill, sbitmap *comp,
625 struct hash_table_d *table)
627 unsigned int i;
629 /* Initialize the bitmaps that were passed in. */
630 bitmap_vector_clear (kill, last_basic_block_for_fn (cfun));
631 bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
633 for (i = 0; i < table->size; i++)
635 struct cprop_expr *expr;
637 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
639 int indx = expr->bitmap_index;
640 df_ref def;
641 struct cprop_occr *occr;
643 /* For each definition of the destination pseudo-reg, the expression
644 is killed in the block where the definition is. */
645 for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
646 def; def = DF_REF_NEXT_REG (def))
647 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
649 /* If the source is a pseudo-reg, for each definition of the source,
650 the expression is killed in the block where the definition is. */
651 if (REG_P (expr->src))
652 for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
653 def; def = DF_REF_NEXT_REG (def))
654 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
656 /* The occurrences recorded in avail_occr are exactly those that
657 are locally available in the block where they are. */
658 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
660 bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
666 /* Hash table support. */
668 /* Top level routine to do the dataflow analysis needed by copy/const
669 propagation. */
671 static void
672 compute_cprop_data (void)
674 basic_block bb;
676 compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
677 compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
679 /* Merge implicit sets into CPROP_AVIN. They are always available at the
680 entry of their basic block. We need to do this because 1) implicit sets
681 aren't recorded for the local pass so they cannot be propagated within
682 their basic block by this pass and 2) the global pass would otherwise
683 propagate them only in the successors of their basic block. */
684 FOR_EACH_BB_FN (bb, cfun)
686 int index = implicit_set_indexes[bb->index];
687 if (index != -1)
688 bitmap_set_bit (cprop_avin[bb->index], index);
692 /* Copy/constant propagation. */
694 /* Maximum number of register uses in an insn that we handle. */
695 #define MAX_USES 8
697 /* Table of uses (registers, both hard and pseudo) found in an insn.
698 Allocated statically to avoid alloc/free complexity and overhead. */
699 static rtx reg_use_table[MAX_USES];
701 /* Index into `reg_use_table' while building it. */
702 static unsigned reg_use_count;
704 /* Set up a list of register numbers used in INSN. The found uses are stored
705 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
706 and contains the number of uses in the table upon exit.
708 ??? If a register appears multiple times we will record it multiple times.
709 This doesn't hurt anything but it will slow things down. */
711 static void
712 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
714 int i, j;
715 enum rtx_code code;
716 const char *fmt;
717 rtx x = *xptr;
719 /* repeat is used to turn tail-recursion into iteration since GCC
720 can't do it when there's no return value. */
721 repeat:
722 if (x == 0)
723 return;
725 code = GET_CODE (x);
726 if (REG_P (x))
728 if (reg_use_count == MAX_USES)
729 return;
731 reg_use_table[reg_use_count] = x;
732 reg_use_count++;
735 /* Recursively scan the operands of this expression. */
737 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
739 if (fmt[i] == 'e')
741 /* If we are about to do the last recursive call
742 needed at this level, change it into iteration.
743 This function is called enough to be worth it. */
744 if (i == 0)
746 x = XEXP (x, 0);
747 goto repeat;
750 find_used_regs (&XEXP (x, i), data);
752 else if (fmt[i] == 'E')
753 for (j = 0; j < XVECLEN (x, i); j++)
754 find_used_regs (&XVECEXP (x, i, j), data);
758 /* Try to replace all uses of FROM in INSN with TO.
759 Return nonzero if successful. */
761 static int
762 try_replace_reg (rtx from, rtx to, rtx_insn *insn)
764 rtx note = find_reg_equal_equiv_note (insn);
765 rtx src = 0;
766 int success = 0;
767 rtx set = single_set (insn);
769 /* Usually we substitute easy stuff, so we won't copy everything.
770 We however need to take care to not duplicate non-trivial CONST
771 expressions. */
772 to = copy_rtx (to);
774 validate_replace_src_group (from, to, insn);
775 if (num_changes_pending () && apply_change_group ())
776 success = 1;
778 /* Try to simplify SET_SRC if we have substituted a constant. */
779 if (success && set && CONSTANT_P (to))
781 src = simplify_rtx (SET_SRC (set));
783 if (src)
784 validate_change (insn, &SET_SRC (set), src, 0);
787 /* If there is already a REG_EQUAL note, update the expression in it
788 with our replacement. */
789 if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
790 set_unique_reg_note (insn, REG_EQUAL,
791 simplify_replace_rtx (XEXP (note, 0), from, to));
792 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
794 /* If above failed and this is a single set, try to simplify the source
795 of the set given our substitution. We could perhaps try this for
796 multiple SETs, but it probably won't buy us anything. */
797 src = simplify_replace_rtx (SET_SRC (set), from, to);
799 if (!rtx_equal_p (src, SET_SRC (set))
800 && validate_change (insn, &SET_SRC (set), src, 0))
801 success = 1;
803 /* If we've failed perform the replacement, have a single SET to
804 a REG destination and don't yet have a note, add a REG_EQUAL note
805 to not lose information. */
806 if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
807 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
810 if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
812 /* Registers can also appear as uses in SET_DEST if it is a MEM.
813 We could perhaps try this for multiple SETs, but it probably
814 won't buy us anything. */
815 rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
817 if (!rtx_equal_p (dest, SET_DEST (set))
818 && validate_change (insn, &SET_DEST (set), dest, 0))
819 success = 1;
822 /* REG_EQUAL may get simplified into register.
823 We don't allow that. Remove that note. This code ought
824 not to happen, because previous code ought to synthesize
825 reg-reg move, but be on the safe side. */
826 if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
827 remove_note (insn, note);
829 return success;
832 /* Find a set of REGNOs that are available on entry to INSN's block. If found,
833 SET_RET[0] will be assigned a set with a register source and SET_RET[1] a
834 set with a constant source. If not found the corresponding entry is set to
835 NULL. */
837 static void
838 find_avail_set (int regno, rtx_insn *insn, struct cprop_expr *set_ret[2])
840 set_ret[0] = set_ret[1] = NULL;
842 /* Loops are not possible here. To get a loop we would need two sets
843 available at the start of the block containing INSN. i.e. we would
844 need two sets like this available at the start of the block:
846 (set (reg X) (reg Y))
847 (set (reg Y) (reg X))
849 This can not happen since the set of (reg Y) would have killed the
850 set of (reg X) making it unavailable at the start of this block. */
851 while (1)
853 rtx src;
854 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
856 /* Find a set that is available at the start of the block
857 which contains INSN. */
858 while (set)
860 if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
861 set->bitmap_index))
862 break;
863 set = next_set (regno, set);
866 /* If no available set was found we've reached the end of the
867 (possibly empty) copy chain. */
868 if (set == 0)
869 break;
871 src = set->src;
873 /* We know the set is available.
874 Now check that SRC is locally anticipatable (i.e. none of the
875 source operands have changed since the start of the block).
877 If the source operand changed, we may still use it for the next
878 iteration of this loop, but we may not use it for substitutions. */
880 if (cprop_constant_p (src))
881 set_ret[1] = set;
882 else if (reg_not_set_p (src, insn))
883 set_ret[0] = set;
885 /* If the source of the set is anything except a register, then
886 we have reached the end of the copy chain. */
887 if (! REG_P (src))
888 break;
890 /* Follow the copy chain, i.e. start another iteration of the loop
891 and see if we have an available copy into SRC. */
892 regno = REGNO (src);
896 /* Subroutine of cprop_insn that tries to propagate constants into
897 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
898 it is the instruction that immediately precedes JUMP, and must be a
899 single SET of a register. FROM is what we will try to replace,
900 SRC is the constant we will try to substitute for it. Return nonzero
901 if a change was made. */
903 static int
904 cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src)
906 rtx new_rtx, set_src, note_src;
907 rtx set = pc_set (jump);
908 rtx note = find_reg_equal_equiv_note (jump);
910 if (note)
912 note_src = XEXP (note, 0);
913 if (GET_CODE (note_src) == EXPR_LIST)
914 note_src = NULL_RTX;
916 else note_src = NULL_RTX;
918 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
919 set_src = note_src ? note_src : SET_SRC (set);
921 /* First substitute the SETCC condition into the JUMP instruction,
922 then substitute that given values into this expanded JUMP. */
923 if (setcc != NULL_RTX
924 && !modified_between_p (from, setcc, jump)
925 && !modified_between_p (src, setcc, jump))
927 rtx setcc_src;
928 rtx setcc_set = single_set (setcc);
929 rtx setcc_note = find_reg_equal_equiv_note (setcc);
930 setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
931 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
932 set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
933 setcc_src);
935 else
936 setcc = NULL;
938 new_rtx = simplify_replace_rtx (set_src, from, src);
940 /* If no simplification can be made, then try the next register. */
941 if (rtx_equal_p (new_rtx, SET_SRC (set)))
942 return 0;
944 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
945 if (new_rtx == pc_rtx)
946 delete_insn (jump);
947 else
949 /* Ensure the value computed inside the jump insn to be equivalent
950 to one computed by setcc. */
951 if (setcc && modified_in_p (new_rtx, setcc))
952 return 0;
953 if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
955 /* When (some) constants are not valid in a comparison, and there
956 are two registers to be replaced by constants before the entire
957 comparison can be folded into a constant, we need to keep
958 intermediate information in REG_EQUAL notes. For targets with
959 separate compare insns, such notes are added by try_replace_reg.
960 When we have a combined compare-and-branch instruction, however,
961 we need to attach a note to the branch itself to make this
962 optimization work. */
964 if (!rtx_equal_p (new_rtx, note_src))
965 set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
966 return 0;
969 /* Remove REG_EQUAL note after simplification. */
970 if (note_src)
971 remove_note (jump, note);
974 /* Delete the cc0 setter. */
975 if (HAVE_cc0 && setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
976 delete_insn (setcc);
978 global_const_prop_count++;
979 if (dump_file != NULL)
981 fprintf (dump_file,
982 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with"
983 "constant ", REGNO (from), INSN_UID (jump));
984 print_rtl (dump_file, src);
985 fprintf (dump_file, "\n");
987 purge_dead_edges (bb);
989 /* If a conditional jump has been changed into unconditional jump, remove
990 the jump and make the edge fallthru - this is always called in
991 cfglayout mode. */
992 if (new_rtx != pc_rtx && simplejump_p (jump))
994 edge e;
995 edge_iterator ei;
997 FOR_EACH_EDGE (e, ei, bb->succs)
998 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
999 && BB_HEAD (e->dest) == JUMP_LABEL (jump))
1001 e->flags |= EDGE_FALLTHRU;
1002 break;
1004 delete_insn (jump);
1007 return 1;
1010 /* Subroutine of cprop_insn that tries to propagate constants. FROM is what
1011 we will try to replace, SRC is the constant we will try to substitute for
1012 it and INSN is the instruction where this will be happening. */
1014 static int
1015 constprop_register (rtx from, rtx src, rtx_insn *insn)
1017 rtx sset;
1019 /* Check for reg or cc0 setting instructions followed by
1020 conditional branch instructions first. */
1021 if ((sset = single_set (insn)) != NULL
1022 && NEXT_INSN (insn)
1023 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
1025 rtx dest = SET_DEST (sset);
1026 if ((REG_P (dest) || CC0_P (dest))
1027 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
1028 from, src))
1029 return 1;
1032 /* Handle normal insns next. */
1033 if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
1034 return 1;
1036 /* Try to propagate a CONST_INT into a conditional jump.
1037 We're pretty specific about what we will handle in this
1038 code, we can extend this as necessary over time.
1040 Right now the insn in question must look like
1041 (set (pc) (if_then_else ...)) */
1042 else if (any_condjump_p (insn) && onlyjump_p (insn))
1043 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
1044 return 0;
1047 /* Perform constant and copy propagation on INSN.
1048 Return nonzero if a change was made. */
1050 static int
1051 cprop_insn (rtx_insn *insn)
1053 unsigned i;
1054 int changed = 0, changed_this_round;
1055 rtx note;
1059 changed_this_round = 0;
1060 reg_use_count = 0;
1061 note_uses (&PATTERN (insn), find_used_regs, NULL);
1063 /* We may win even when propagating constants into notes. */
1064 note = find_reg_equal_equiv_note (insn);
1065 if (note)
1066 find_used_regs (&XEXP (note, 0), NULL);
1068 for (i = 0; i < reg_use_count; i++)
1070 rtx reg_used = reg_use_table[i];
1071 unsigned int regno = REGNO (reg_used);
1072 rtx src_cst = NULL, src_reg = NULL;
1073 struct cprop_expr *set[2];
1075 /* If the register has already been set in this block, there's
1076 nothing we can do. */
1077 if (! reg_not_set_p (reg_used, insn))
1078 continue;
1080 /* Find an assignment that sets reg_used and is available
1081 at the start of the block. */
1082 find_avail_set (regno, insn, set);
1083 if (set[0])
1084 src_reg = set[0]->src;
1085 if (set[1])
1086 src_cst = set[1]->src;
1088 /* Constant propagation. */
1089 if (src_cst && cprop_constant_p (src_cst)
1090 && constprop_register (reg_used, src_cst, insn))
1092 changed_this_round = changed = 1;
1093 global_const_prop_count++;
1094 if (dump_file != NULL)
1096 fprintf (dump_file,
1097 "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
1098 fprintf (dump_file, "insn %d with constant ",
1099 INSN_UID (insn));
1100 print_rtl (dump_file, src_cst);
1101 fprintf (dump_file, "\n");
1103 if (insn->deleted ())
1104 return 1;
1106 /* Copy propagation. */
1107 else if (src_reg && cprop_reg_p (src_reg)
1108 && REGNO (src_reg) != regno
1109 && try_replace_reg (reg_used, src_reg, insn))
1111 changed_this_round = changed = 1;
1112 global_copy_prop_count++;
1113 if (dump_file != NULL)
1115 fprintf (dump_file,
1116 "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1117 regno, INSN_UID (insn));
1118 fprintf (dump_file, " with reg %d\n", REGNO (src_reg));
1121 /* The original insn setting reg_used may or may not now be
1122 deletable. We leave the deletion to DCE. */
1123 /* FIXME: If it turns out that the insn isn't deletable,
1124 then we may have unnecessarily extended register lifetimes
1125 and made things worse. */
1129 /* If try_replace_reg simplified the insn, the regs found by find_used_regs
1130 may not be valid anymore. Start over. */
1131 while (changed_this_round);
1133 if (changed && DEBUG_INSN_P (insn))
1134 return 0;
1136 return changed;
1139 /* Like find_used_regs, but avoid recording uses that appear in
1140 input-output contexts such as zero_extract or pre_dec. This
1141 restricts the cases we consider to those for which local cprop
1142 can legitimately make replacements. */
1144 static void
1145 local_cprop_find_used_regs (rtx *xptr, void *data)
1147 rtx x = *xptr;
1149 if (x == 0)
1150 return;
1152 switch (GET_CODE (x))
1154 case ZERO_EXTRACT:
1155 case SIGN_EXTRACT:
1156 case STRICT_LOW_PART:
1157 return;
1159 case PRE_DEC:
1160 case PRE_INC:
1161 case POST_DEC:
1162 case POST_INC:
1163 case PRE_MODIFY:
1164 case POST_MODIFY:
1165 /* Can only legitimately appear this early in the context of
1166 stack pushes for function arguments, but handle all of the
1167 codes nonetheless. */
1168 return;
1170 case SUBREG:
1171 /* Setting a subreg of a register larger than word_mode leaves
1172 the non-written words unchanged. */
1173 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
1174 return;
1175 break;
1177 default:
1178 break;
1181 find_used_regs (xptr, data);
1184 /* Try to perform local const/copy propagation on X in INSN. */
1186 static bool
1187 do_local_cprop (rtx x, rtx_insn *insn)
1189 rtx newreg = NULL, newcnst = NULL;
1191 /* Rule out USE instructions and ASM statements as we don't want to
1192 change the hard registers mentioned. */
1193 if (REG_P (x)
1194 && (cprop_reg_p (x)
1195 || (GET_CODE (PATTERN (insn)) != USE
1196 && asm_noperands (PATTERN (insn)) < 0)))
1198 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
1199 struct elt_loc_list *l;
1201 if (!val)
1202 return false;
1203 for (l = val->locs; l; l = l->next)
1205 rtx this_rtx = l->loc;
1206 rtx note;
1208 if (cprop_constant_p (this_rtx))
1209 newcnst = this_rtx;
1210 if (cprop_reg_p (this_rtx)
1211 /* Don't copy propagate if it has attached REG_EQUIV note.
1212 At this point this only function parameters should have
1213 REG_EQUIV notes and if the argument slot is used somewhere
1214 explicitly, it means address of parameter has been taken,
1215 so we should not extend the lifetime of the pseudo. */
1216 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
1217 || ! MEM_P (XEXP (note, 0))))
1218 newreg = this_rtx;
1220 if (newcnst && constprop_register (x, newcnst, insn))
1222 if (dump_file != NULL)
1224 fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
1225 REGNO (x));
1226 fprintf (dump_file, "insn %d with constant ",
1227 INSN_UID (insn));
1228 print_rtl (dump_file, newcnst);
1229 fprintf (dump_file, "\n");
1231 local_const_prop_count++;
1232 return true;
1234 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
1236 if (dump_file != NULL)
1238 fprintf (dump_file,
1239 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
1240 REGNO (x), INSN_UID (insn));
1241 fprintf (dump_file, " with reg %d\n", REGNO (newreg));
1243 local_copy_prop_count++;
1244 return true;
1247 return false;
1250 /* Do local const/copy propagation (i.e. within each basic block). */
1252 static int
1253 local_cprop_pass (void)
1255 basic_block bb;
1256 rtx_insn *insn;
1257 bool changed = false;
1258 unsigned i;
1260 cselib_init (0);
1261 FOR_EACH_BB_FN (bb, cfun)
1263 FOR_BB_INSNS (bb, insn)
1265 if (INSN_P (insn))
1267 rtx note = find_reg_equal_equiv_note (insn);
1270 reg_use_count = 0;
1271 note_uses (&PATTERN (insn), local_cprop_find_used_regs,
1272 NULL);
1273 if (note)
1274 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
1276 for (i = 0; i < reg_use_count; i++)
1278 if (do_local_cprop (reg_use_table[i], insn))
1280 if (!DEBUG_INSN_P (insn))
1281 changed = true;
1282 break;
1285 if (insn->deleted ())
1286 break;
1288 while (i < reg_use_count);
1290 cselib_process_insn (insn);
1293 /* Forget everything at the end of a basic block. */
1294 cselib_clear_table ();
1297 cselib_finish ();
1299 return changed;
1302 /* Similar to get_condition, only the resulting condition must be
1303 valid at JUMP, instead of at EARLIEST.
1305 This differs from noce_get_condition in ifcvt.c in that we prefer not to
1306 settle for the condition variable in the jump instruction being integral.
1307 We prefer to be able to record the value of a user variable, rather than
1308 the value of a temporary used in a condition. This could be solved by
1309 recording the value of *every* register scanned by canonicalize_condition,
1310 but this would require some code reorganization. */
1313 fis_get_condition (rtx_insn *jump)
1315 return get_condition (jump, NULL, false, true);
1318 /* Check the comparison COND to see if we can safely form an implicit
1319 set from it. */
1321 static bool
1322 implicit_set_cond_p (const_rtx cond)
1324 machine_mode mode;
1325 rtx cst;
1327 /* COND must be either an EQ or NE comparison. */
1328 if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
1329 return false;
1331 /* The first operand of COND must be a register we can propagate. */
1332 if (!cprop_reg_p (XEXP (cond, 0)))
1333 return false;
1335 /* The second operand of COND must be a suitable constant. */
1336 mode = GET_MODE (XEXP (cond, 0));
1337 cst = XEXP (cond, 1);
1339 /* We can't perform this optimization if either operand might be or might
1340 contain a signed zero. */
1341 if (HONOR_SIGNED_ZEROS (mode))
1343 /* It is sufficient to check if CST is or contains a zero. We must
1344 handle float, complex, and vector. If any subpart is a zero, then
1345 the optimization can't be performed. */
1346 /* ??? The complex and vector checks are not implemented yet. We just
1347 always return zero for them. */
1348 if (CONST_DOUBLE_AS_FLOAT_P (cst))
1350 REAL_VALUE_TYPE d;
1351 REAL_VALUE_FROM_CONST_DOUBLE (d, cst);
1352 if (REAL_VALUES_EQUAL (d, dconst0))
1353 return 0;
1355 else
1356 return 0;
1359 return cprop_constant_p (cst);
1362 /* Find the implicit sets of a function. An "implicit set" is a constraint
1363 on the value of a variable, implied by a conditional jump. For example,
1364 following "if (x == 2)", the then branch may be optimized as though the
1365 conditional performed an "explicit set", in this example, "x = 2". This
1366 function records the set patterns that are implicit at the start of each
1367 basic block.
1369 If an implicit set is found but the set is implicit on a critical edge,
1370 this critical edge is split.
1372 Return true if the CFG was modified, false otherwise. */
1374 static bool
1375 find_implicit_sets (void)
1377 basic_block bb, dest;
1378 rtx cond, new_rtx;
1379 unsigned int count = 0;
1380 bool edges_split = false;
1381 size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10;
1383 implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1385 FOR_EACH_BB_FN (bb, cfun)
1387 /* Check for more than one successor. */
1388 if (EDGE_COUNT (bb->succs) <= 1)
1389 continue;
1391 cond = fis_get_condition (BB_END (bb));
1393 /* If no condition is found or if it isn't of a suitable form,
1394 ignore it. */
1395 if (! cond || ! implicit_set_cond_p (cond))
1396 continue;
1398 dest = GET_CODE (cond) == EQ
1399 ? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
1401 /* If DEST doesn't go anywhere, ignore it. */
1402 if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1403 continue;
1405 /* We have found a suitable implicit set. Try to record it now as
1406 a SET in DEST. If DEST has more than one predecessor, the edge
1407 between BB and DEST is a critical edge and we must split it,
1408 because we can only record one implicit set per DEST basic block. */
1409 if (! single_pred_p (dest))
1411 dest = split_edge (find_edge (bb, dest));
1412 edges_split = true;
1415 if (implicit_sets_size <= (size_t) dest->index)
1417 size_t old_implicit_sets_size = implicit_sets_size;
1418 implicit_sets_size *= 2;
1419 implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
1420 memset (implicit_sets + old_implicit_sets_size, 0,
1421 (implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1424 new_rtx = gen_rtx_SET (XEXP (cond, 0), XEXP (cond, 1));
1425 implicit_sets[dest->index] = new_rtx;
1426 if (dump_file)
1428 fprintf (dump_file, "Implicit set of reg %d in ",
1429 REGNO (XEXP (cond, 0)));
1430 fprintf (dump_file, "basic block %d\n", dest->index);
1432 count++;
1435 if (dump_file)
1436 fprintf (dump_file, "Found %d implicit sets\n", count);
1438 /* Confess our sins. */
1439 return edges_split;
1442 /* Bypass conditional jumps. */
1444 /* The value of last_basic_block at the beginning of the jump_bypass
1445 pass. The use of redirect_edge_and_branch_force may introduce new
1446 basic blocks, but the data flow analysis is only valid for basic
1447 block indices less than bypass_last_basic_block. */
1449 static int bypass_last_basic_block;
1451 /* Find a set of REGNO to a constant that is available at the end of basic
1452 block BB. Return NULL if no such set is found. Based heavily upon
1453 find_avail_set. */
1455 static struct cprop_expr *
1456 find_bypass_set (int regno, int bb)
1458 struct cprop_expr *result = 0;
1460 for (;;)
1462 rtx src;
1463 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
1465 while (set)
1467 if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
1468 break;
1469 set = next_set (regno, set);
1472 if (set == 0)
1473 break;
1475 src = set->src;
1476 if (cprop_constant_p (src))
1477 result = set;
1479 if (! REG_P (src))
1480 break;
1482 regno = REGNO (src);
1484 return result;
1487 /* Subroutine of bypass_block that checks whether a pseudo is killed by
1488 any of the instructions inserted on an edge. Jump bypassing places
1489 condition code setters on CFG edges using insert_insn_on_edge. This
1490 function is required to check that our data flow analysis is still
1491 valid prior to commit_edge_insertions. */
1493 static bool
1494 reg_killed_on_edge (const_rtx reg, const_edge e)
1496 rtx_insn *insn;
1498 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
1499 if (INSN_P (insn) && reg_set_p (reg, insn))
1500 return true;
1502 return false;
1505 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
1506 basic block BB which has more than one predecessor. If not NULL, SETCC
1507 is the first instruction of BB, which is immediately followed by JUMP_INSN
1508 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
1509 Returns nonzero if a change was made.
1511 During the jump bypassing pass, we may place copies of SETCC instructions
1512 on CFG edges. The following routine must be careful to pay attention to
1513 these inserted insns when performing its transformations. */
1515 static int
1516 bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump)
1518 rtx_insn *insn;
1519 rtx note;
1520 edge e, edest;
1521 int change;
1522 int may_be_loop_header = false;
1523 unsigned removed_p;
1524 unsigned i;
1525 edge_iterator ei;
1527 insn = (setcc != NULL) ? setcc : jump;
1529 /* Determine set of register uses in INSN. */
1530 reg_use_count = 0;
1531 note_uses (&PATTERN (insn), find_used_regs, NULL);
1532 note = find_reg_equal_equiv_note (insn);
1533 if (note)
1534 find_used_regs (&XEXP (note, 0), NULL);
1536 if (current_loops)
1538 /* If we are to preserve loop structure then do not bypass
1539 a loop header. This will either rotate the loop, create
1540 multiple entry loops or even irreducible regions. */
1541 if (bb == bb->loop_father->header)
1542 return 0;
1544 else
1546 FOR_EACH_EDGE (e, ei, bb->preds)
1547 if (e->flags & EDGE_DFS_BACK)
1549 may_be_loop_header = true;
1550 break;
1554 change = 0;
1555 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1557 removed_p = 0;
1559 if (e->flags & EDGE_COMPLEX)
1561 ei_next (&ei);
1562 continue;
1565 /* We can't redirect edges from new basic blocks. */
1566 if (e->src->index >= bypass_last_basic_block)
1568 ei_next (&ei);
1569 continue;
1572 /* The irreducible loops created by redirecting of edges entering the
1573 loop from outside would decrease effectiveness of some of the
1574 following optimizations, so prevent this. */
1575 if (may_be_loop_header
1576 && !(e->flags & EDGE_DFS_BACK))
1578 ei_next (&ei);
1579 continue;
1582 for (i = 0; i < reg_use_count; i++)
1584 rtx reg_used = reg_use_table[i];
1585 unsigned int regno = REGNO (reg_used);
1586 basic_block dest, old_dest;
1587 struct cprop_expr *set;
1588 rtx src, new_rtx;
1590 set = find_bypass_set (regno, e->src->index);
1592 if (! set)
1593 continue;
1595 /* Check the data flow is valid after edge insertions. */
1596 if (e->insns.r && reg_killed_on_edge (reg_used, e))
1597 continue;
1599 src = SET_SRC (pc_set (jump));
1601 if (setcc != NULL)
1602 src = simplify_replace_rtx (src,
1603 SET_DEST (PATTERN (setcc)),
1604 SET_SRC (PATTERN (setcc)));
1606 new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1608 /* Jump bypassing may have already placed instructions on
1609 edges of the CFG. We can't bypass an outgoing edge that
1610 has instructions associated with it, as these insns won't
1611 get executed if the incoming edge is redirected. */
1612 if (new_rtx == pc_rtx)
1614 edest = FALLTHRU_EDGE (bb);
1615 dest = edest->insns.r ? NULL : edest->dest;
1617 else if (GET_CODE (new_rtx) == LABEL_REF)
1619 dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
1620 /* Don't bypass edges containing instructions. */
1621 edest = find_edge (bb, dest);
1622 if (edest && edest->insns.r)
1623 dest = NULL;
1625 else
1626 dest = NULL;
1628 /* Avoid unification of the edge with other edges from original
1629 branch. We would end up emitting the instruction on "both"
1630 edges. */
1631 if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
1632 && find_edge (e->src, dest))
1633 dest = NULL;
1635 old_dest = e->dest;
1636 if (dest != NULL
1637 && dest != old_dest
1638 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1640 redirect_edge_and_branch_force (e, dest);
1642 /* Copy the register setter to the redirected edge.
1643 Don't copy CC0 setters, as CC0 is dead after jump. */
1644 if (setcc)
1646 rtx pat = PATTERN (setcc);
1647 if (!CC0_P (SET_DEST (pat)))
1648 insert_insn_on_edge (copy_insn (pat), e);
1651 if (dump_file != NULL)
1653 fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
1654 "in jump_insn %d equals constant ",
1655 regno, INSN_UID (jump));
1656 print_rtl (dump_file, set->src);
1657 fprintf (dump_file, "\n\t when BB %d is entered from "
1658 "BB %d. Redirect edge %d->%d to %d.\n",
1659 old_dest->index, e->src->index, e->src->index,
1660 old_dest->index, dest->index);
1662 change = 1;
1663 removed_p = 1;
1664 break;
1667 if (!removed_p)
1668 ei_next (&ei);
1670 return change;
1673 /* Find basic blocks with more than one predecessor that only contain a
1674 single conditional jump. If the result of the comparison is known at
1675 compile-time from any incoming edge, redirect that edge to the
1676 appropriate target. Return nonzero if a change was made.
1678 This function is now mis-named, because we also handle indirect jumps. */
1680 static int
1681 bypass_conditional_jumps (void)
1683 basic_block bb;
1684 int changed;
1685 rtx_insn *setcc;
1686 rtx_insn *insn;
1687 rtx dest;
1689 /* Note we start at block 1. */
1690 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1691 return 0;
1693 bypass_last_basic_block = last_basic_block_for_fn (cfun);
1694 mark_dfs_back_edges ();
1696 changed = 0;
1697 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1698 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1700 /* Check for more than one predecessor. */
1701 if (!single_pred_p (bb))
1703 setcc = NULL;
1704 FOR_BB_INSNS (bb, insn)
1705 if (DEBUG_INSN_P (insn))
1706 continue;
1707 else if (NONJUMP_INSN_P (insn))
1709 if (setcc)
1710 break;
1711 if (GET_CODE (PATTERN (insn)) != SET)
1712 break;
1714 dest = SET_DEST (PATTERN (insn));
1715 if (REG_P (dest) || CC0_P (dest))
1716 setcc = insn;
1717 else
1718 break;
1720 else if (JUMP_P (insn))
1722 if ((any_condjump_p (insn) || computed_jump_p (insn))
1723 && onlyjump_p (insn))
1724 changed |= bypass_block (bb, setcc, insn);
1725 break;
1727 else if (INSN_P (insn))
1728 break;
1732 /* If we bypassed any register setting insns, we inserted a
1733 copy on the redirected edge. These need to be committed. */
1734 if (changed)
1735 commit_edge_insertions ();
1737 return changed;
1740 /* Return true if the graph is too expensive to optimize. PASS is the
1741 optimization about to be performed. */
1743 static bool
1744 is_too_expensive (const char *pass)
1746 /* Trying to perform global optimizations on flow graphs which have
1747 a high connectivity will take a long time and is unlikely to be
1748 particularly useful.
1750 In normal circumstances a cfg should have about twice as many
1751 edges as blocks. But we do not want to punish small functions
1752 which have a couple switch statements. Rather than simply
1753 threshold the number of blocks, uses something with a more
1754 graceful degradation. */
1755 if (n_edges_for_fn (cfun) > 20000 + n_basic_blocks_for_fn (cfun) * 4)
1757 warning (OPT_Wdisabled_optimization,
1758 "%s: %d basic blocks and %d edges/basic block",
1759 pass, n_basic_blocks_for_fn (cfun),
1760 n_edges_for_fn (cfun) / n_basic_blocks_for_fn (cfun));
1762 return true;
1765 /* If allocating memory for the cprop bitmap would take up too much
1766 storage it's better just to disable the optimization. */
1767 if ((n_basic_blocks_for_fn (cfun)
1768 * SBITMAP_SET_SIZE (max_reg_num ())
1769 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
1771 warning (OPT_Wdisabled_optimization,
1772 "%s: %d basic blocks and %d registers",
1773 pass, n_basic_blocks_for_fn (cfun), max_reg_num ());
1775 return true;
1778 return false;
1781 /* Main function for the CPROP pass. */
1783 static int
1784 one_cprop_pass (void)
1786 int i;
1787 int changed = 0;
1789 /* Return if there's nothing to do, or it is too expensive. */
1790 if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
1791 || is_too_expensive (_ ("const/copy propagation disabled")))
1792 return 0;
1794 global_const_prop_count = local_const_prop_count = 0;
1795 global_copy_prop_count = local_copy_prop_count = 0;
1797 bytes_used = 0;
1798 gcc_obstack_init (&cprop_obstack);
1800 /* Do a local const/copy propagation pass first. The global pass
1801 only handles global opportunities.
1802 If the local pass changes something, remove any unreachable blocks
1803 because the CPROP global dataflow analysis may get into infinite
1804 loops for CFGs with unreachable blocks.
1806 FIXME: This local pass should not be necessary after CSE (but for
1807 some reason it still is). It is also (proven) not necessary
1808 to run the local pass right after FWPWOP.
1810 FIXME: The global analysis would not get into infinite loops if it
1811 would use the DF solver (via df_simple_dataflow) instead of
1812 the solver implemented in this file. */
1813 changed |= local_cprop_pass ();
1814 if (changed)
1815 delete_unreachable_blocks ();
1817 /* Determine implicit sets. This may change the CFG (split critical
1818 edges if that exposes an implicit set).
1819 Note that find_implicit_sets() does not rely on up-to-date DF caches
1820 so that we do not have to re-run df_analyze() even if local CPROP
1821 changed something.
1822 ??? This could run earlier so that any uncovered implicit sets
1823 sets could be exploited in local_cprop_pass() also. Later. */
1824 changed |= find_implicit_sets ();
1826 /* If local_cprop_pass() or find_implicit_sets() changed something,
1827 run df_analyze() to bring all insn caches up-to-date, and to take
1828 new basic blocks from edge splitting on the DF radar.
1829 NB: This also runs the fast DCE pass, because execute_rtl_cprop
1830 sets DF_LR_RUN_DCE. */
1831 if (changed)
1832 df_analyze ();
1834 /* Initialize implicit_set_indexes array. */
1835 implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun));
1836 for (i = 0; i < last_basic_block_for_fn (cfun); i++)
1837 implicit_set_indexes[i] = -1;
1839 alloc_hash_table (&set_hash_table);
1840 compute_hash_table (&set_hash_table);
1842 /* Free implicit_sets before peak usage. */
1843 free (implicit_sets);
1844 implicit_sets = NULL;
1846 if (dump_file)
1847 dump_hash_table (dump_file, "SET", &set_hash_table);
1848 if (set_hash_table.n_elems > 0)
1850 basic_block bb;
1851 rtx_insn *insn;
1853 alloc_cprop_mem (last_basic_block_for_fn (cfun),
1854 set_hash_table.n_elems);
1855 compute_cprop_data ();
1857 free (implicit_set_indexes);
1858 implicit_set_indexes = NULL;
1860 /* Allocate vars to track sets of regs. */
1861 reg_set_bitmap = ALLOC_REG_SET (NULL);
1863 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1864 EXIT_BLOCK_PTR_FOR_FN (cfun),
1865 next_bb)
1867 /* Reset tables used to keep track of what's still valid [since
1868 the start of the block]. */
1869 reset_opr_set_tables ();
1871 FOR_BB_INSNS (bb, insn)
1872 if (INSN_P (insn))
1874 changed |= cprop_insn (insn);
1876 /* Keep track of everything modified by this insn. */
1877 /* ??? Need to be careful w.r.t. mods done to INSN.
1878 Don't call mark_oprs_set if we turned the
1879 insn into a NOTE, or deleted the insn. */
1880 if (! NOTE_P (insn) && ! insn->deleted ())
1881 mark_oprs_set (insn);
1885 changed |= bypass_conditional_jumps ();
1887 FREE_REG_SET (reg_set_bitmap);
1888 free_cprop_mem ();
1890 else
1892 free (implicit_set_indexes);
1893 implicit_set_indexes = NULL;
1896 free_hash_table (&set_hash_table);
1897 obstack_free (&cprop_obstack, NULL);
1899 if (dump_file)
1901 fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1902 current_function_name (), n_basic_blocks_for_fn (cfun),
1903 bytes_used);
1904 fprintf (dump_file, "%d local const props, %d local copy props, ",
1905 local_const_prop_count, local_copy_prop_count);
1906 fprintf (dump_file, "%d global const props, %d global copy props\n\n",
1907 global_const_prop_count, global_copy_prop_count);
1910 return changed;
1913 /* All the passes implemented in this file. Each pass has its
1914 own gate and execute function, and at the end of the file a
1915 pass definition for passes.c.
1917 We do not construct an accurate cfg in functions which call
1918 setjmp, so none of these passes runs if the function calls
1919 setjmp.
1920 FIXME: Should just handle setjmp via REG_SETJMP notes. */
1922 static unsigned int
1923 execute_rtl_cprop (void)
1925 int changed;
1926 delete_unreachable_blocks ();
1927 df_set_flags (DF_LR_RUN_DCE);
1928 df_analyze ();
1929 changed = one_cprop_pass ();
1930 flag_rerun_cse_after_global_opts |= changed;
1931 if (changed)
1932 cleanup_cfg (CLEANUP_CFG_CHANGED);
1933 return 0;
1936 namespace {
1938 const pass_data pass_data_rtl_cprop =
1940 RTL_PASS, /* type */
1941 "cprop", /* name */
1942 OPTGROUP_NONE, /* optinfo_flags */
1943 TV_CPROP, /* tv_id */
1944 PROP_cfglayout, /* properties_required */
1945 0, /* properties_provided */
1946 0, /* properties_destroyed */
1947 0, /* todo_flags_start */
1948 TODO_df_finish, /* todo_flags_finish */
1951 class pass_rtl_cprop : public rtl_opt_pass
1953 public:
1954 pass_rtl_cprop (gcc::context *ctxt)
1955 : rtl_opt_pass (pass_data_rtl_cprop, ctxt)
1958 /* opt_pass methods: */
1959 opt_pass * clone () { return new pass_rtl_cprop (m_ctxt); }
1960 virtual bool gate (function *fun)
1962 return optimize > 0 && flag_gcse
1963 && !fun->calls_setjmp
1964 && dbg_cnt (cprop);
1967 virtual unsigned int execute (function *) { return execute_rtl_cprop (); }
1969 }; // class pass_rtl_cprop
1971 } // anon namespace
1973 rtl_opt_pass *
1974 make_pass_rtl_cprop (gcc::context *ctxt)
1976 return new pass_rtl_cprop (ctxt);