PR c/64423
[official-gcc.git] / gcc / cprop.c
blob15ebc17ab75a929890cf2268c87ee6b935541d4c
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 "predict.h"
36 #include "vec.h"
37 #include "hashtab.h"
38 #include "hash-set.h"
39 #include "machmode.h"
40 #include "input.h"
41 #include "function.h"
42 #include "dominance.h"
43 #include "cfg.h"
44 #include "cfgrtl.h"
45 #include "cfganal.h"
46 #include "lcm.h"
47 #include "cfgcleanup.h"
48 #include "basic-block.h"
49 #include "expr.h"
50 #include "except.h"
51 #include "params.h"
52 #include "cselib.h"
53 #include "intl.h"
54 #include "obstack.h"
55 #include "tree-pass.h"
56 #include "df.h"
57 #include "dbgcnt.h"
58 #include "target.h"
59 #include "cfgloop.h"
62 /* An obstack for our working variables. */
63 static struct obstack cprop_obstack;
65 /* Occurrence of an expression.
66 There is one per basic block. If a pattern appears more than once the
67 last appearance is used. */
69 struct cprop_occr
71 /* Next occurrence of this expression. */
72 struct cprop_occr *next;
73 /* The insn that computes the expression. */
74 rtx_insn *insn;
77 typedef struct cprop_occr *occr_t;
79 /* Hash table entry for assignment expressions. */
81 struct cprop_expr
83 /* The expression (DEST := SRC). */
84 rtx dest;
85 rtx src;
87 /* Index in the available expression bitmaps. */
88 int bitmap_index;
89 /* Next entry with the same hash. */
90 struct cprop_expr *next_same_hash;
91 /* List of available occurrence in basic blocks in the function.
92 An "available occurrence" is one that is the last occurrence in the
93 basic block and whose operands are not modified by following statements
94 in the basic block [including this insn]. */
95 struct cprop_occr *avail_occr;
98 /* Hash table for copy propagation expressions.
99 Each hash table is an array of buckets.
100 ??? It is known that if it were an array of entries, structure elements
101 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
102 not clear whether in the final analysis a sufficient amount of memory would
103 be saved as the size of the available expression bitmaps would be larger
104 [one could build a mapping table without holes afterwards though].
105 Someday I'll perform the computation and figure it out. */
107 struct hash_table_d
109 /* The table itself.
110 This is an array of `set_hash_table_size' elements. */
111 struct cprop_expr **table;
113 /* Size of the hash table, in elements. */
114 unsigned int size;
116 /* Number of hash table elements. */
117 unsigned int n_elems;
120 /* Copy propagation hash table. */
121 static struct hash_table_d set_hash_table;
123 /* Array of implicit set patterns indexed by basic block index. */
124 static rtx *implicit_sets;
126 /* Array of indexes of expressions for implicit set patterns indexed by basic
127 block index. In other words, implicit_set_indexes[i] is the bitmap_index
128 of the expression whose RTX is implicit_sets[i]. */
129 static int *implicit_set_indexes;
131 /* Bitmap containing one bit for each register in the program.
132 Used when performing GCSE to track which registers have been set since
133 the start or end of the basic block while traversing that block. */
134 static regset reg_set_bitmap;
136 /* Various variables for statistics gathering. */
138 /* Memory used in a pass.
139 This isn't intended to be absolutely precise. Its intent is only
140 to keep an eye on memory usage. */
141 static int bytes_used;
143 /* Number of local constants propagated. */
144 static int local_const_prop_count;
145 /* Number of local copies propagated. */
146 static int local_copy_prop_count;
147 /* Number of global constants propagated. */
148 static int global_const_prop_count;
149 /* Number of global copies propagated. */
150 static int global_copy_prop_count;
152 #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T)))
153 #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S)))
155 /* Cover function to obstack_alloc. */
157 static void *
158 cprop_alloc (unsigned long size)
160 bytes_used += size;
161 return obstack_alloc (&cprop_obstack, size);
164 /* Return nonzero if register X is unchanged from INSN to the end
165 of INSN's basic block. */
167 static int
168 reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
170 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
173 /* Hash a set of register REGNO.
175 Sets are hashed on the register that is set. This simplifies the PRE copy
176 propagation code.
178 ??? May need to make things more elaborate. Later, as necessary. */
180 static unsigned int
181 hash_mod (int regno, int hash_table_size)
183 return (unsigned) regno % hash_table_size;
186 /* Insert assignment DEST:=SET from INSN in the hash table.
187 DEST is a register and SET is a register or a suitable constant.
188 If the assignment is already present in the table, record it as
189 the last occurrence in INSN's basic block.
190 IMPLICIT is true if it's an implicit set, false otherwise. */
192 static void
193 insert_set_in_table (rtx dest, rtx src, rtx_insn *insn,
194 struct hash_table_d *table, bool implicit)
196 bool found = false;
197 unsigned int hash;
198 struct cprop_expr *cur_expr, *last_expr = NULL;
199 struct cprop_occr *cur_occr;
201 hash = hash_mod (REGNO (dest), table->size);
203 for (cur_expr = table->table[hash]; cur_expr;
204 cur_expr = cur_expr->next_same_hash)
206 if (dest == cur_expr->dest
207 && src == cur_expr->src)
209 found = true;
210 break;
212 last_expr = cur_expr;
215 if (! found)
217 cur_expr = GOBNEW (struct cprop_expr);
218 bytes_used += sizeof (struct cprop_expr);
219 if (table->table[hash] == NULL)
220 /* This is the first pattern that hashed to this index. */
221 table->table[hash] = cur_expr;
222 else
223 /* Add EXPR to end of this hash chain. */
224 last_expr->next_same_hash = cur_expr;
226 /* Set the fields of the expr element.
227 We must copy X because it can be modified when copy propagation is
228 performed on its operands. */
229 cur_expr->dest = copy_rtx (dest);
230 cur_expr->src = copy_rtx (src);
231 cur_expr->bitmap_index = table->n_elems++;
232 cur_expr->next_same_hash = NULL;
233 cur_expr->avail_occr = NULL;
236 /* Now record the occurrence. */
237 cur_occr = cur_expr->avail_occr;
239 if (cur_occr
240 && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
242 /* Found another instance of the expression in the same basic block.
243 Prefer this occurrence to the currently recorded one. We want
244 the last one in the block and the block is scanned from start
245 to end. */
246 cur_occr->insn = insn;
248 else
250 /* First occurrence of this expression in this basic block. */
251 cur_occr = GOBNEW (struct cprop_occr);
252 bytes_used += sizeof (struct cprop_occr);
253 cur_occr->insn = insn;
254 cur_occr->next = cur_expr->avail_occr;
255 cur_expr->avail_occr = cur_occr;
258 /* Record bitmap_index of the implicit set in implicit_set_indexes. */
259 if (implicit)
260 implicit_set_indexes[BLOCK_FOR_INSN (insn)->index]
261 = cur_expr->bitmap_index;
264 /* Determine whether the rtx X should be treated as a constant for CPROP.
265 Since X might be inserted more than once we have to take care that it
266 is sharable. */
268 static bool
269 cprop_constant_p (const_rtx x)
271 return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
274 /* Scan SET present in INSN and add an entry to the hash TABLE.
275 IMPLICIT is true if it's an implicit set, false otherwise. */
277 static void
278 hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table,
279 bool implicit)
281 rtx src = SET_SRC (set);
282 rtx dest = SET_DEST (set);
284 if (REG_P (dest)
285 && ! HARD_REGISTER_P (dest)
286 && reg_available_p (dest, insn)
287 && can_copy_p (GET_MODE (dest)))
289 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
291 This allows us to do a single CPROP pass and still eliminate
292 redundant constants, addresses or other expressions that are
293 constructed with multiple instructions.
295 However, keep the original SRC if INSN is a simple reg-reg move. In
296 In this case, there will almost always be a REG_EQUAL note on the
297 insn that sets SRC. By recording the REG_EQUAL value here as SRC
298 for INSN, we miss copy propagation opportunities.
300 Note that this does not impede profitable constant propagations. We
301 "look through" reg-reg sets in lookup_set. */
302 rtx note = find_reg_equal_equiv_note (insn);
303 if (note != 0
304 && REG_NOTE_KIND (note) == REG_EQUAL
305 && !REG_P (src)
306 && cprop_constant_p (XEXP (note, 0)))
307 src = XEXP (note, 0), set = gen_rtx_SET (VOIDmode, dest, src);
309 /* Record sets for constant/copy propagation. */
310 if ((REG_P (src)
311 && src != dest
312 && ! HARD_REGISTER_P (src)
313 && reg_available_p (src, insn))
314 || cprop_constant_p (src))
315 insert_set_in_table (dest, src, insn, table, implicit);
319 /* Process INSN and add hash table entries as appropriate. */
321 static void
322 hash_scan_insn (rtx_insn *insn, struct hash_table_d *table)
324 rtx pat = PATTERN (insn);
325 int i;
327 /* Pick out the sets of INSN and for other forms of instructions record
328 what's been modified. */
330 if (GET_CODE (pat) == SET)
331 hash_scan_set (pat, insn, table, false);
332 else if (GET_CODE (pat) == PARALLEL)
333 for (i = 0; i < XVECLEN (pat, 0); i++)
335 rtx x = XVECEXP (pat, 0, i);
337 if (GET_CODE (x) == SET)
338 hash_scan_set (x, insn, table, false);
342 /* Dump the hash table TABLE to file FILE under the name NAME. */
344 static void
345 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
347 int i;
348 /* Flattened out table, so it's printed in proper order. */
349 struct cprop_expr **flat_table;
350 unsigned int *hash_val;
351 struct cprop_expr *expr;
353 flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems);
354 hash_val = XNEWVEC (unsigned int, table->n_elems);
356 for (i = 0; i < (int) table->size; i++)
357 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
359 flat_table[expr->bitmap_index] = expr;
360 hash_val[expr->bitmap_index] = i;
363 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
364 name, table->size, table->n_elems);
366 for (i = 0; i < (int) table->n_elems; i++)
367 if (flat_table[i] != 0)
369 expr = flat_table[i];
370 fprintf (file, "Index %d (hash value %d)\n ",
371 expr->bitmap_index, hash_val[i]);
372 print_rtl (file, expr->dest);
373 fprintf (file, " := ");
374 print_rtl (file, expr->src);
375 fprintf (file, "\n");
378 fprintf (file, "\n");
380 free (flat_table);
381 free (hash_val);
384 /* Record as unavailable all registers that are DEF operands of INSN. */
386 static void
387 make_set_regs_unavailable (rtx_insn *insn)
389 df_ref def;
391 FOR_EACH_INSN_DEF (def, insn)
392 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
395 /* Top level function to create an assignment hash table.
397 Assignment entries are placed in the hash table if
398 - they are of the form (set (pseudo-reg) src),
399 - src is something we want to perform const/copy propagation on,
400 - none of the operands or target are subsequently modified in the block
402 Currently src must be a pseudo-reg or a const_int.
404 TABLE is the table computed. */
406 static void
407 compute_hash_table_work (struct hash_table_d *table)
409 basic_block bb;
411 /* Allocate vars to track sets of regs. */
412 reg_set_bitmap = ALLOC_REG_SET (NULL);
414 FOR_EACH_BB_FN (bb, cfun)
416 rtx_insn *insn;
418 /* Reset tables used to keep track of what's not yet invalid [since
419 the end of the block]. */
420 CLEAR_REG_SET (reg_set_bitmap);
422 /* Go over all insns from the last to the first. This is convenient
423 for tracking available registers, i.e. not set between INSN and
424 the end of the basic block BB. */
425 FOR_BB_INSNS_REVERSE (bb, insn)
427 /* Only real insns are interesting. */
428 if (!NONDEBUG_INSN_P (insn))
429 continue;
431 /* Record interesting sets from INSN in the hash table. */
432 hash_scan_insn (insn, table);
434 /* Any registers set in INSN will make SETs above it not AVAIL. */
435 make_set_regs_unavailable (insn);
438 /* Insert implicit sets in the hash table, pretending they appear as
439 insns at the head of the basic block. */
440 if (implicit_sets[bb->index] != NULL_RTX)
441 hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
444 FREE_REG_SET (reg_set_bitmap);
447 /* Allocate space for the set/expr hash TABLE.
448 It is used to determine the number of buckets to use. */
450 static void
451 alloc_hash_table (struct hash_table_d *table)
453 int n;
455 n = get_max_insn_count ();
457 table->size = n / 4;
458 if (table->size < 11)
459 table->size = 11;
461 /* Attempt to maintain efficient use of hash table.
462 Making it an odd number is simplest for now.
463 ??? Later take some measurements. */
464 table->size |= 1;
465 n = table->size * sizeof (struct cprop_expr *);
466 table->table = XNEWVAR (struct cprop_expr *, n);
469 /* Free things allocated by alloc_hash_table. */
471 static void
472 free_hash_table (struct hash_table_d *table)
474 free (table->table);
477 /* Compute the hash TABLE for doing copy/const propagation or
478 expression hash table. */
480 static void
481 compute_hash_table (struct hash_table_d *table)
483 /* Initialize count of number of entries in hash table. */
484 table->n_elems = 0;
485 memset (table->table, 0, table->size * sizeof (struct cprop_expr *));
487 compute_hash_table_work (table);
490 /* Expression tracking support. */
492 /* Lookup REGNO in the set TABLE. The result is a pointer to the
493 table entry, or NULL if not found. */
495 static struct cprop_expr *
496 lookup_set (unsigned int regno, struct hash_table_d *table)
498 unsigned int hash = hash_mod (regno, table->size);
499 struct cprop_expr *expr;
501 expr = table->table[hash];
503 while (expr && REGNO (expr->dest) != regno)
504 expr = expr->next_same_hash;
506 return expr;
509 /* Return the next entry for REGNO in list EXPR. */
511 static struct cprop_expr *
512 next_set (unsigned int regno, struct cprop_expr *expr)
515 expr = expr->next_same_hash;
516 while (expr && REGNO (expr->dest) != regno);
518 return expr;
521 /* Reset tables used to keep track of what's still available [since the
522 start of the block]. */
524 static void
525 reset_opr_set_tables (void)
527 /* Maintain a bitmap of which regs have been set since beginning of
528 the block. */
529 CLEAR_REG_SET (reg_set_bitmap);
532 /* Return nonzero if the register X has not been set yet [since the
533 start of the basic block containing INSN]. */
535 static int
536 reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
538 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
541 /* Record things set by INSN.
542 This data is used by reg_not_set_p. */
544 static void
545 mark_oprs_set (rtx_insn *insn)
547 df_ref def;
549 FOR_EACH_INSN_DEF (def, insn)
550 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
553 /* Compute copy/constant propagation working variables. */
555 /* Local properties of assignments. */
556 static sbitmap *cprop_avloc;
557 static sbitmap *cprop_kill;
559 /* Global properties of assignments (computed from the local properties). */
560 static sbitmap *cprop_avin;
561 static sbitmap *cprop_avout;
563 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
564 basic blocks. N_SETS is the number of sets. */
566 static void
567 alloc_cprop_mem (int n_blocks, int n_sets)
569 cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
570 cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
572 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
573 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
576 /* Free vars used by copy/const propagation. */
578 static void
579 free_cprop_mem (void)
581 sbitmap_vector_free (cprop_avloc);
582 sbitmap_vector_free (cprop_kill);
583 sbitmap_vector_free (cprop_avin);
584 sbitmap_vector_free (cprop_avout);
587 /* Compute the local properties of each recorded expression.
589 Local properties are those that are defined by the block, irrespective of
590 other blocks.
592 An expression is killed in a block if its operands, either DEST or SRC, are
593 modified in the block.
595 An expression is computed (locally available) in a block if it is computed
596 at least once and expression would contain the same value if the
597 computation was moved to the end of the block.
599 KILL and COMP are destination sbitmaps for recording local properties. */
601 static void
602 compute_local_properties (sbitmap *kill, sbitmap *comp,
603 struct hash_table_d *table)
605 unsigned int i;
607 /* Initialize the bitmaps that were passed in. */
608 bitmap_vector_clear (kill, last_basic_block_for_fn (cfun));
609 bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
611 for (i = 0; i < table->size; i++)
613 struct cprop_expr *expr;
615 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
617 int indx = expr->bitmap_index;
618 df_ref def;
619 struct cprop_occr *occr;
621 /* For each definition of the destination pseudo-reg, the expression
622 is killed in the block where the definition is. */
623 for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
624 def; def = DF_REF_NEXT_REG (def))
625 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
627 /* If the source is a pseudo-reg, for each definition of the source,
628 the expression is killed in the block where the definition is. */
629 if (REG_P (expr->src))
630 for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
631 def; def = DF_REF_NEXT_REG (def))
632 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
634 /* The occurrences recorded in avail_occr are exactly those that
635 are locally available in the block where they are. */
636 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
638 bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
644 /* Hash table support. */
646 /* Top level routine to do the dataflow analysis needed by copy/const
647 propagation. */
649 static void
650 compute_cprop_data (void)
652 basic_block bb;
654 compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
655 compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
657 /* Merge implicit sets into CPROP_AVIN. They are always available at the
658 entry of their basic block. We need to do this because 1) implicit sets
659 aren't recorded for the local pass so they cannot be propagated within
660 their basic block by this pass and 2) the global pass would otherwise
661 propagate them only in the successors of their basic block. */
662 FOR_EACH_BB_FN (bb, cfun)
664 int index = implicit_set_indexes[bb->index];
665 if (index != -1)
666 bitmap_set_bit (cprop_avin[bb->index], index);
670 /* Copy/constant propagation. */
672 /* Maximum number of register uses in an insn that we handle. */
673 #define MAX_USES 8
675 /* Table of uses (registers, both hard and pseudo) found in an insn.
676 Allocated statically to avoid alloc/free complexity and overhead. */
677 static rtx reg_use_table[MAX_USES];
679 /* Index into `reg_use_table' while building it. */
680 static unsigned reg_use_count;
682 /* Set up a list of register numbers used in INSN. The found uses are stored
683 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
684 and contains the number of uses in the table upon exit.
686 ??? If a register appears multiple times we will record it multiple times.
687 This doesn't hurt anything but it will slow things down. */
689 static void
690 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
692 int i, j;
693 enum rtx_code code;
694 const char *fmt;
695 rtx x = *xptr;
697 /* repeat is used to turn tail-recursion into iteration since GCC
698 can't do it when there's no return value. */
699 repeat:
700 if (x == 0)
701 return;
703 code = GET_CODE (x);
704 if (REG_P (x))
706 if (reg_use_count == MAX_USES)
707 return;
709 reg_use_table[reg_use_count] = x;
710 reg_use_count++;
713 /* Recursively scan the operands of this expression. */
715 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
717 if (fmt[i] == 'e')
719 /* If we are about to do the last recursive call
720 needed at this level, change it into iteration.
721 This function is called enough to be worth it. */
722 if (i == 0)
724 x = XEXP (x, 0);
725 goto repeat;
728 find_used_regs (&XEXP (x, i), data);
730 else if (fmt[i] == 'E')
731 for (j = 0; j < XVECLEN (x, i); j++)
732 find_used_regs (&XVECEXP (x, i, j), data);
736 /* Try to replace all uses of FROM in INSN with TO.
737 Return nonzero if successful. */
739 static int
740 try_replace_reg (rtx from, rtx to, rtx_insn *insn)
742 rtx note = find_reg_equal_equiv_note (insn);
743 rtx src = 0;
744 int success = 0;
745 rtx set = single_set (insn);
747 /* Usually we substitute easy stuff, so we won't copy everything.
748 We however need to take care to not duplicate non-trivial CONST
749 expressions. */
750 to = copy_rtx (to);
752 validate_replace_src_group (from, to, insn);
753 if (num_changes_pending () && apply_change_group ())
754 success = 1;
756 /* Try to simplify SET_SRC if we have substituted a constant. */
757 if (success && set && CONSTANT_P (to))
759 src = simplify_rtx (SET_SRC (set));
761 if (src)
762 validate_change (insn, &SET_SRC (set), src, 0);
765 /* If there is already a REG_EQUAL note, update the expression in it
766 with our replacement. */
767 if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
768 set_unique_reg_note (insn, REG_EQUAL,
769 simplify_replace_rtx (XEXP (note, 0), from, to));
770 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
772 /* If above failed and this is a single set, try to simplify the source
773 of the set given our substitution. We could perhaps try this for
774 multiple SETs, but it probably won't buy us anything. */
775 src = simplify_replace_rtx (SET_SRC (set), from, to);
777 if (!rtx_equal_p (src, SET_SRC (set))
778 && validate_change (insn, &SET_SRC (set), src, 0))
779 success = 1;
781 /* If we've failed perform the replacement, have a single SET to
782 a REG destination and don't yet have a note, add a REG_EQUAL note
783 to not lose information. */
784 if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
785 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
788 if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
790 /* Registers can also appear as uses in SET_DEST if it is a MEM.
791 We could perhaps try this for multiple SETs, but it probably
792 won't buy us anything. */
793 rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
795 if (!rtx_equal_p (dest, SET_DEST (set))
796 && validate_change (insn, &SET_DEST (set), dest, 0))
797 success = 1;
800 /* REG_EQUAL may get simplified into register.
801 We don't allow that. Remove that note. This code ought
802 not to happen, because previous code ought to synthesize
803 reg-reg move, but be on the safe side. */
804 if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
805 remove_note (insn, note);
807 return success;
810 /* Find a set of REGNOs that are available on entry to INSN's block. Return
811 NULL no such set is found. */
813 static struct cprop_expr *
814 find_avail_set (int regno, rtx_insn *insn)
816 /* SET1 contains the last set found that can be returned to the caller for
817 use in a substitution. */
818 struct cprop_expr *set1 = 0;
820 /* Loops are not possible here. To get a loop we would need two sets
821 available at the start of the block containing INSN. i.e. we would
822 need two sets like this available at the start of the block:
824 (set (reg X) (reg Y))
825 (set (reg Y) (reg X))
827 This can not happen since the set of (reg Y) would have killed the
828 set of (reg X) making it unavailable at the start of this block. */
829 while (1)
831 rtx src;
832 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
834 /* Find a set that is available at the start of the block
835 which contains INSN. */
836 while (set)
838 if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
839 set->bitmap_index))
840 break;
841 set = next_set (regno, set);
844 /* If no available set was found we've reached the end of the
845 (possibly empty) copy chain. */
846 if (set == 0)
847 break;
849 src = set->src;
851 /* We know the set is available.
852 Now check that SRC is locally anticipatable (i.e. none of the
853 source operands have changed since the start of the block).
855 If the source operand changed, we may still use it for the next
856 iteration of this loop, but we may not use it for substitutions. */
858 if (cprop_constant_p (src) || reg_not_set_p (src, insn))
859 set1 = set;
861 /* If the source of the set is anything except a register, then
862 we have reached the end of the copy chain. */
863 if (! REG_P (src))
864 break;
866 /* Follow the copy chain, i.e. start another iteration of the loop
867 and see if we have an available copy into SRC. */
868 regno = REGNO (src);
871 /* SET1 holds the last set that was available and anticipatable at
872 INSN. */
873 return set1;
876 /* Subroutine of cprop_insn that tries to propagate constants into
877 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
878 it is the instruction that immediately precedes JUMP, and must be a
879 single SET of a register. FROM is what we will try to replace,
880 SRC is the constant we will try to substitute for it. Return nonzero
881 if a change was made. */
883 static int
884 cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src)
886 rtx new_rtx, set_src, note_src;
887 rtx set = pc_set (jump);
888 rtx note = find_reg_equal_equiv_note (jump);
890 if (note)
892 note_src = XEXP (note, 0);
893 if (GET_CODE (note_src) == EXPR_LIST)
894 note_src = NULL_RTX;
896 else note_src = NULL_RTX;
898 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
899 set_src = note_src ? note_src : SET_SRC (set);
901 /* First substitute the SETCC condition into the JUMP instruction,
902 then substitute that given values into this expanded JUMP. */
903 if (setcc != NULL_RTX
904 && !modified_between_p (from, setcc, jump)
905 && !modified_between_p (src, setcc, jump))
907 rtx setcc_src;
908 rtx setcc_set = single_set (setcc);
909 rtx setcc_note = find_reg_equal_equiv_note (setcc);
910 setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
911 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
912 set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
913 setcc_src);
915 else
916 setcc = NULL;
918 new_rtx = simplify_replace_rtx (set_src, from, src);
920 /* If no simplification can be made, then try the next register. */
921 if (rtx_equal_p (new_rtx, SET_SRC (set)))
922 return 0;
924 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
925 if (new_rtx == pc_rtx)
926 delete_insn (jump);
927 else
929 /* Ensure the value computed inside the jump insn to be equivalent
930 to one computed by setcc. */
931 if (setcc && modified_in_p (new_rtx, setcc))
932 return 0;
933 if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
935 /* When (some) constants are not valid in a comparison, and there
936 are two registers to be replaced by constants before the entire
937 comparison can be folded into a constant, we need to keep
938 intermediate information in REG_EQUAL notes. For targets with
939 separate compare insns, such notes are added by try_replace_reg.
940 When we have a combined compare-and-branch instruction, however,
941 we need to attach a note to the branch itself to make this
942 optimization work. */
944 if (!rtx_equal_p (new_rtx, note_src))
945 set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
946 return 0;
949 /* Remove REG_EQUAL note after simplification. */
950 if (note_src)
951 remove_note (jump, note);
954 #ifdef HAVE_cc0
955 /* Delete the cc0 setter. */
956 if (setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
957 delete_insn (setcc);
958 #endif
960 global_const_prop_count++;
961 if (dump_file != NULL)
963 fprintf (dump_file,
964 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with"
965 "constant ", REGNO (from), INSN_UID (jump));
966 print_rtl (dump_file, src);
967 fprintf (dump_file, "\n");
969 purge_dead_edges (bb);
971 /* If a conditional jump has been changed into unconditional jump, remove
972 the jump and make the edge fallthru - this is always called in
973 cfglayout mode. */
974 if (new_rtx != pc_rtx && simplejump_p (jump))
976 edge e;
977 edge_iterator ei;
979 FOR_EACH_EDGE (e, ei, bb->succs)
980 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
981 && BB_HEAD (e->dest) == JUMP_LABEL (jump))
983 e->flags |= EDGE_FALLTHRU;
984 break;
986 delete_insn (jump);
989 return 1;
992 /* Subroutine of cprop_insn that tries to propagate constants. FROM is what
993 we will try to replace, SRC is the constant we will try to substitute for
994 it and INSN is the instruction where this will be happening. */
996 static int
997 constprop_register (rtx from, rtx src, rtx_insn *insn)
999 rtx sset;
1001 /* Check for reg or cc0 setting instructions followed by
1002 conditional branch instructions first. */
1003 if ((sset = single_set (insn)) != NULL
1004 && NEXT_INSN (insn)
1005 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
1007 rtx dest = SET_DEST (sset);
1008 if ((REG_P (dest) || CC0_P (dest))
1009 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
1010 from, src))
1011 return 1;
1014 /* Handle normal insns next. */
1015 if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
1016 return 1;
1018 /* Try to propagate a CONST_INT into a conditional jump.
1019 We're pretty specific about what we will handle in this
1020 code, we can extend this as necessary over time.
1022 Right now the insn in question must look like
1023 (set (pc) (if_then_else ...)) */
1024 else if (any_condjump_p (insn) && onlyjump_p (insn))
1025 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
1026 return 0;
1029 /* Perform constant and copy propagation on INSN.
1030 Return nonzero if a change was made. */
1032 static int
1033 cprop_insn (rtx_insn *insn)
1035 unsigned i;
1036 int changed = 0, changed_this_round;
1037 rtx note;
1039 retry:
1040 changed_this_round = 0;
1041 reg_use_count = 0;
1042 note_uses (&PATTERN (insn), find_used_regs, NULL);
1044 /* We may win even when propagating constants into notes. */
1045 note = find_reg_equal_equiv_note (insn);
1046 if (note)
1047 find_used_regs (&XEXP (note, 0), NULL);
1049 for (i = 0; i < reg_use_count; i++)
1051 rtx reg_used = reg_use_table[i];
1052 unsigned int regno = REGNO (reg_used);
1053 rtx src;
1054 struct cprop_expr *set;
1056 /* If the register has already been set in this block, there's
1057 nothing we can do. */
1058 if (! reg_not_set_p (reg_used, insn))
1059 continue;
1061 /* Find an assignment that sets reg_used and is available
1062 at the start of the block. */
1063 set = find_avail_set (regno, insn);
1064 if (! set)
1065 continue;
1067 src = set->src;
1069 /* Constant propagation. */
1070 if (cprop_constant_p (src))
1072 if (constprop_register (reg_used, src, insn))
1074 changed_this_round = changed = 1;
1075 global_const_prop_count++;
1076 if (dump_file != NULL)
1078 fprintf (dump_file,
1079 "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
1080 fprintf (dump_file, "insn %d with constant ",
1081 INSN_UID (insn));
1082 print_rtl (dump_file, src);
1083 fprintf (dump_file, "\n");
1085 if (insn->deleted ())
1086 return 1;
1089 else if (REG_P (src)
1090 && REGNO (src) >= FIRST_PSEUDO_REGISTER
1091 && REGNO (src) != regno)
1093 if (try_replace_reg (reg_used, src, insn))
1095 changed_this_round = changed = 1;
1096 global_copy_prop_count++;
1097 if (dump_file != NULL)
1099 fprintf (dump_file,
1100 "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1101 regno, INSN_UID (insn));
1102 fprintf (dump_file, " with reg %d\n", REGNO (src));
1105 /* The original insn setting reg_used may or may not now be
1106 deletable. We leave the deletion to DCE. */
1107 /* FIXME: If it turns out that the insn isn't deletable,
1108 then we may have unnecessarily extended register lifetimes
1109 and made things worse. */
1113 /* If try_replace_reg simplified the insn, the regs found
1114 by find_used_regs may not be valid anymore. Start over. */
1115 if (changed_this_round)
1116 goto retry;
1119 if (changed && DEBUG_INSN_P (insn))
1120 return 0;
1122 return changed;
1125 /* Like find_used_regs, but avoid recording uses that appear in
1126 input-output contexts such as zero_extract or pre_dec. This
1127 restricts the cases we consider to those for which local cprop
1128 can legitimately make replacements. */
1130 static void
1131 local_cprop_find_used_regs (rtx *xptr, void *data)
1133 rtx x = *xptr;
1135 if (x == 0)
1136 return;
1138 switch (GET_CODE (x))
1140 case ZERO_EXTRACT:
1141 case SIGN_EXTRACT:
1142 case STRICT_LOW_PART:
1143 return;
1145 case PRE_DEC:
1146 case PRE_INC:
1147 case POST_DEC:
1148 case POST_INC:
1149 case PRE_MODIFY:
1150 case POST_MODIFY:
1151 /* Can only legitimately appear this early in the context of
1152 stack pushes for function arguments, but handle all of the
1153 codes nonetheless. */
1154 return;
1156 case SUBREG:
1157 /* Setting a subreg of a register larger than word_mode leaves
1158 the non-written words unchanged. */
1159 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
1160 return;
1161 break;
1163 default:
1164 break;
1167 find_used_regs (xptr, data);
1170 /* Try to perform local const/copy propagation on X in INSN. */
1172 static bool
1173 do_local_cprop (rtx x, rtx_insn *insn)
1175 rtx newreg = NULL, newcnst = NULL;
1177 /* Rule out USE instructions and ASM statements as we don't want to
1178 change the hard registers mentioned. */
1179 if (REG_P (x)
1180 && (REGNO (x) >= FIRST_PSEUDO_REGISTER
1181 || (GET_CODE (PATTERN (insn)) != USE
1182 && asm_noperands (PATTERN (insn)) < 0)))
1184 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
1185 struct elt_loc_list *l;
1187 if (!val)
1188 return false;
1189 for (l = val->locs; l; l = l->next)
1191 rtx this_rtx = l->loc;
1192 rtx note;
1194 if (cprop_constant_p (this_rtx))
1195 newcnst = this_rtx;
1196 if (REG_P (this_rtx) && REGNO (this_rtx) >= FIRST_PSEUDO_REGISTER
1197 /* Don't copy propagate if it has attached REG_EQUIV note.
1198 At this point this only function parameters should have
1199 REG_EQUIV notes and if the argument slot is used somewhere
1200 explicitly, it means address of parameter has been taken,
1201 so we should not extend the lifetime of the pseudo. */
1202 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
1203 || ! MEM_P (XEXP (note, 0))))
1204 newreg = this_rtx;
1206 if (newcnst && constprop_register (x, newcnst, insn))
1208 if (dump_file != NULL)
1210 fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
1211 REGNO (x));
1212 fprintf (dump_file, "insn %d with constant ",
1213 INSN_UID (insn));
1214 print_rtl (dump_file, newcnst);
1215 fprintf (dump_file, "\n");
1217 local_const_prop_count++;
1218 return true;
1220 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
1222 if (dump_file != NULL)
1224 fprintf (dump_file,
1225 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
1226 REGNO (x), INSN_UID (insn));
1227 fprintf (dump_file, " with reg %d\n", REGNO (newreg));
1229 local_copy_prop_count++;
1230 return true;
1233 return false;
1236 /* Do local const/copy propagation (i.e. within each basic block). */
1238 static int
1239 local_cprop_pass (void)
1241 basic_block bb;
1242 rtx_insn *insn;
1243 bool changed = false;
1244 unsigned i;
1246 cselib_init (0);
1247 FOR_EACH_BB_FN (bb, cfun)
1249 FOR_BB_INSNS (bb, insn)
1251 if (INSN_P (insn))
1253 rtx note = find_reg_equal_equiv_note (insn);
1256 reg_use_count = 0;
1257 note_uses (&PATTERN (insn), local_cprop_find_used_regs,
1258 NULL);
1259 if (note)
1260 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
1262 for (i = 0; i < reg_use_count; i++)
1264 if (do_local_cprop (reg_use_table[i], insn))
1266 if (!DEBUG_INSN_P (insn))
1267 changed = true;
1268 break;
1271 if (insn->deleted ())
1272 break;
1274 while (i < reg_use_count);
1276 cselib_process_insn (insn);
1279 /* Forget everything at the end of a basic block. */
1280 cselib_clear_table ();
1283 cselib_finish ();
1285 return changed;
1288 /* Similar to get_condition, only the resulting condition must be
1289 valid at JUMP, instead of at EARLIEST.
1291 This differs from noce_get_condition in ifcvt.c in that we prefer not to
1292 settle for the condition variable in the jump instruction being integral.
1293 We prefer to be able to record the value of a user variable, rather than
1294 the value of a temporary used in a condition. This could be solved by
1295 recording the value of *every* register scanned by canonicalize_condition,
1296 but this would require some code reorganization. */
1299 fis_get_condition (rtx_insn *jump)
1301 return get_condition (jump, NULL, false, true);
1304 /* Check the comparison COND to see if we can safely form an implicit
1305 set from it. */
1307 static bool
1308 implicit_set_cond_p (const_rtx cond)
1310 machine_mode mode;
1311 rtx cst;
1313 /* COND must be either an EQ or NE comparison. */
1314 if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
1315 return false;
1317 /* The first operand of COND must be a pseudo-reg. */
1318 if (! REG_P (XEXP (cond, 0))
1319 || HARD_REGISTER_P (XEXP (cond, 0)))
1320 return false;
1322 /* The second operand of COND must be a suitable constant. */
1323 mode = GET_MODE (XEXP (cond, 0));
1324 cst = XEXP (cond, 1);
1326 /* We can't perform this optimization if either operand might be or might
1327 contain a signed zero. */
1328 if (HONOR_SIGNED_ZEROS (mode))
1330 /* It is sufficient to check if CST is or contains a zero. We must
1331 handle float, complex, and vector. If any subpart is a zero, then
1332 the optimization can't be performed. */
1333 /* ??? The complex and vector checks are not implemented yet. We just
1334 always return zero for them. */
1335 if (CONST_DOUBLE_AS_FLOAT_P (cst))
1337 REAL_VALUE_TYPE d;
1338 REAL_VALUE_FROM_CONST_DOUBLE (d, cst);
1339 if (REAL_VALUES_EQUAL (d, dconst0))
1340 return 0;
1342 else
1343 return 0;
1346 return cprop_constant_p (cst);
1349 /* Find the implicit sets of a function. An "implicit set" is a constraint
1350 on the value of a variable, implied by a conditional jump. For example,
1351 following "if (x == 2)", the then branch may be optimized as though the
1352 conditional performed an "explicit set", in this example, "x = 2". This
1353 function records the set patterns that are implicit at the start of each
1354 basic block.
1356 If an implicit set is found but the set is implicit on a critical edge,
1357 this critical edge is split.
1359 Return true if the CFG was modified, false otherwise. */
1361 static bool
1362 find_implicit_sets (void)
1364 basic_block bb, dest;
1365 rtx cond, new_rtx;
1366 unsigned int count = 0;
1367 bool edges_split = false;
1368 size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10;
1370 implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1372 FOR_EACH_BB_FN (bb, cfun)
1374 /* Check for more than one successor. */
1375 if (EDGE_COUNT (bb->succs) <= 1)
1376 continue;
1378 cond = fis_get_condition (BB_END (bb));
1380 /* If no condition is found or if it isn't of a suitable form,
1381 ignore it. */
1382 if (! cond || ! implicit_set_cond_p (cond))
1383 continue;
1385 dest = GET_CODE (cond) == EQ
1386 ? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
1388 /* If DEST doesn't go anywhere, ignore it. */
1389 if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1390 continue;
1392 /* We have found a suitable implicit set. Try to record it now as
1393 a SET in DEST. If DEST has more than one predecessor, the edge
1394 between BB and DEST is a critical edge and we must split it,
1395 because we can only record one implicit set per DEST basic block. */
1396 if (! single_pred_p (dest))
1398 dest = split_edge (find_edge (bb, dest));
1399 edges_split = true;
1402 if (implicit_sets_size <= (size_t) dest->index)
1404 size_t old_implicit_sets_size = implicit_sets_size;
1405 implicit_sets_size *= 2;
1406 implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
1407 memset (implicit_sets + old_implicit_sets_size, 0,
1408 (implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1411 new_rtx = gen_rtx_SET (VOIDmode, XEXP (cond, 0),
1412 XEXP (cond, 1));
1413 implicit_sets[dest->index] = new_rtx;
1414 if (dump_file)
1416 fprintf (dump_file, "Implicit set of reg %d in ",
1417 REGNO (XEXP (cond, 0)));
1418 fprintf (dump_file, "basic block %d\n", dest->index);
1420 count++;
1423 if (dump_file)
1424 fprintf (dump_file, "Found %d implicit sets\n", count);
1426 /* Confess our sins. */
1427 return edges_split;
1430 /* Bypass conditional jumps. */
1432 /* The value of last_basic_block at the beginning of the jump_bypass
1433 pass. The use of redirect_edge_and_branch_force may introduce new
1434 basic blocks, but the data flow analysis is only valid for basic
1435 block indices less than bypass_last_basic_block. */
1437 static int bypass_last_basic_block;
1439 /* Find a set of REGNO to a constant that is available at the end of basic
1440 block BB. Return NULL if no such set is found. Based heavily upon
1441 find_avail_set. */
1443 static struct cprop_expr *
1444 find_bypass_set (int regno, int bb)
1446 struct cprop_expr *result = 0;
1448 for (;;)
1450 rtx src;
1451 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
1453 while (set)
1455 if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
1456 break;
1457 set = next_set (regno, set);
1460 if (set == 0)
1461 break;
1463 src = set->src;
1464 if (cprop_constant_p (src))
1465 result = set;
1467 if (! REG_P (src))
1468 break;
1470 regno = REGNO (src);
1472 return result;
1475 /* Subroutine of bypass_block that checks whether a pseudo is killed by
1476 any of the instructions inserted on an edge. Jump bypassing places
1477 condition code setters on CFG edges using insert_insn_on_edge. This
1478 function is required to check that our data flow analysis is still
1479 valid prior to commit_edge_insertions. */
1481 static bool
1482 reg_killed_on_edge (const_rtx reg, const_edge e)
1484 rtx_insn *insn;
1486 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
1487 if (INSN_P (insn) && reg_set_p (reg, insn))
1488 return true;
1490 return false;
1493 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
1494 basic block BB which has more than one predecessor. If not NULL, SETCC
1495 is the first instruction of BB, which is immediately followed by JUMP_INSN
1496 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
1497 Returns nonzero if a change was made.
1499 During the jump bypassing pass, we may place copies of SETCC instructions
1500 on CFG edges. The following routine must be careful to pay attention to
1501 these inserted insns when performing its transformations. */
1503 static int
1504 bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump)
1506 rtx_insn *insn;
1507 rtx note;
1508 edge e, edest;
1509 int change;
1510 int may_be_loop_header = false;
1511 unsigned removed_p;
1512 unsigned i;
1513 edge_iterator ei;
1515 insn = (setcc != NULL) ? setcc : jump;
1517 /* Determine set of register uses in INSN. */
1518 reg_use_count = 0;
1519 note_uses (&PATTERN (insn), find_used_regs, NULL);
1520 note = find_reg_equal_equiv_note (insn);
1521 if (note)
1522 find_used_regs (&XEXP (note, 0), NULL);
1524 if (current_loops)
1526 /* If we are to preserve loop structure then do not bypass
1527 a loop header. This will either rotate the loop, create
1528 multiple entry loops or even irreducible regions. */
1529 if (bb == bb->loop_father->header)
1530 return 0;
1532 else
1534 FOR_EACH_EDGE (e, ei, bb->preds)
1535 if (e->flags & EDGE_DFS_BACK)
1537 may_be_loop_header = true;
1538 break;
1542 change = 0;
1543 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1545 removed_p = 0;
1547 if (e->flags & EDGE_COMPLEX)
1549 ei_next (&ei);
1550 continue;
1553 /* We can't redirect edges from new basic blocks. */
1554 if (e->src->index >= bypass_last_basic_block)
1556 ei_next (&ei);
1557 continue;
1560 /* The irreducible loops created by redirecting of edges entering the
1561 loop from outside would decrease effectiveness of some of the
1562 following optimizations, so prevent this. */
1563 if (may_be_loop_header
1564 && !(e->flags & EDGE_DFS_BACK))
1566 ei_next (&ei);
1567 continue;
1570 for (i = 0; i < reg_use_count; i++)
1572 rtx reg_used = reg_use_table[i];
1573 unsigned int regno = REGNO (reg_used);
1574 basic_block dest, old_dest;
1575 struct cprop_expr *set;
1576 rtx src, new_rtx;
1578 set = find_bypass_set (regno, e->src->index);
1580 if (! set)
1581 continue;
1583 /* Check the data flow is valid after edge insertions. */
1584 if (e->insns.r && reg_killed_on_edge (reg_used, e))
1585 continue;
1587 src = SET_SRC (pc_set (jump));
1589 if (setcc != NULL)
1590 src = simplify_replace_rtx (src,
1591 SET_DEST (PATTERN (setcc)),
1592 SET_SRC (PATTERN (setcc)));
1594 new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1596 /* Jump bypassing may have already placed instructions on
1597 edges of the CFG. We can't bypass an outgoing edge that
1598 has instructions associated with it, as these insns won't
1599 get executed if the incoming edge is redirected. */
1600 if (new_rtx == pc_rtx)
1602 edest = FALLTHRU_EDGE (bb);
1603 dest = edest->insns.r ? NULL : edest->dest;
1605 else if (GET_CODE (new_rtx) == LABEL_REF)
1607 dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
1608 /* Don't bypass edges containing instructions. */
1609 edest = find_edge (bb, dest);
1610 if (edest && edest->insns.r)
1611 dest = NULL;
1613 else
1614 dest = NULL;
1616 /* Avoid unification of the edge with other edges from original
1617 branch. We would end up emitting the instruction on "both"
1618 edges. */
1619 if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
1620 && find_edge (e->src, dest))
1621 dest = NULL;
1623 old_dest = e->dest;
1624 if (dest != NULL
1625 && dest != old_dest
1626 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1628 redirect_edge_and_branch_force (e, dest);
1630 /* Copy the register setter to the redirected edge.
1631 Don't copy CC0 setters, as CC0 is dead after jump. */
1632 if (setcc)
1634 rtx pat = PATTERN (setcc);
1635 if (!CC0_P (SET_DEST (pat)))
1636 insert_insn_on_edge (copy_insn (pat), e);
1639 if (dump_file != NULL)
1641 fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
1642 "in jump_insn %d equals constant ",
1643 regno, INSN_UID (jump));
1644 print_rtl (dump_file, set->src);
1645 fprintf (dump_file, "\n\t when BB %d is entered from "
1646 "BB %d. Redirect edge %d->%d to %d.\n",
1647 old_dest->index, e->src->index, e->src->index,
1648 old_dest->index, dest->index);
1650 change = 1;
1651 removed_p = 1;
1652 break;
1655 if (!removed_p)
1656 ei_next (&ei);
1658 return change;
1661 /* Find basic blocks with more than one predecessor that only contain a
1662 single conditional jump. If the result of the comparison is known at
1663 compile-time from any incoming edge, redirect that edge to the
1664 appropriate target. Return nonzero if a change was made.
1666 This function is now mis-named, because we also handle indirect jumps. */
1668 static int
1669 bypass_conditional_jumps (void)
1671 basic_block bb;
1672 int changed;
1673 rtx_insn *setcc;
1674 rtx_insn *insn;
1675 rtx dest;
1677 /* Note we start at block 1. */
1678 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1679 return 0;
1681 bypass_last_basic_block = last_basic_block_for_fn (cfun);
1682 mark_dfs_back_edges ();
1684 changed = 0;
1685 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1686 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1688 /* Check for more than one predecessor. */
1689 if (!single_pred_p (bb))
1691 setcc = NULL;
1692 FOR_BB_INSNS (bb, insn)
1693 if (DEBUG_INSN_P (insn))
1694 continue;
1695 else if (NONJUMP_INSN_P (insn))
1697 if (setcc)
1698 break;
1699 if (GET_CODE (PATTERN (insn)) != SET)
1700 break;
1702 dest = SET_DEST (PATTERN (insn));
1703 if (REG_P (dest) || CC0_P (dest))
1704 setcc = insn;
1705 else
1706 break;
1708 else if (JUMP_P (insn))
1710 if ((any_condjump_p (insn) || computed_jump_p (insn))
1711 && onlyjump_p (insn))
1712 changed |= bypass_block (bb, setcc, insn);
1713 break;
1715 else if (INSN_P (insn))
1716 break;
1720 /* If we bypassed any register setting insns, we inserted a
1721 copy on the redirected edge. These need to be committed. */
1722 if (changed)
1723 commit_edge_insertions ();
1725 return changed;
1728 /* Return true if the graph is too expensive to optimize. PASS is the
1729 optimization about to be performed. */
1731 static bool
1732 is_too_expensive (const char *pass)
1734 /* Trying to perform global optimizations on flow graphs which have
1735 a high connectivity will take a long time and is unlikely to be
1736 particularly useful.
1738 In normal circumstances a cfg should have about twice as many
1739 edges as blocks. But we do not want to punish small functions
1740 which have a couple switch statements. Rather than simply
1741 threshold the number of blocks, uses something with a more
1742 graceful degradation. */
1743 if (n_edges_for_fn (cfun) > 20000 + n_basic_blocks_for_fn (cfun) * 4)
1745 warning (OPT_Wdisabled_optimization,
1746 "%s: %d basic blocks and %d edges/basic block",
1747 pass, n_basic_blocks_for_fn (cfun),
1748 n_edges_for_fn (cfun) / n_basic_blocks_for_fn (cfun));
1750 return true;
1753 /* If allocating memory for the cprop bitmap would take up too much
1754 storage it's better just to disable the optimization. */
1755 if ((n_basic_blocks_for_fn (cfun)
1756 * SBITMAP_SET_SIZE (max_reg_num ())
1757 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
1759 warning (OPT_Wdisabled_optimization,
1760 "%s: %d basic blocks and %d registers",
1761 pass, n_basic_blocks_for_fn (cfun), max_reg_num ());
1763 return true;
1766 return false;
1769 /* Main function for the CPROP pass. */
1771 static int
1772 one_cprop_pass (void)
1774 int i;
1775 int changed = 0;
1777 /* Return if there's nothing to do, or it is too expensive. */
1778 if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
1779 || is_too_expensive (_ ("const/copy propagation disabled")))
1780 return 0;
1782 global_const_prop_count = local_const_prop_count = 0;
1783 global_copy_prop_count = local_copy_prop_count = 0;
1785 bytes_used = 0;
1786 gcc_obstack_init (&cprop_obstack);
1788 /* Do a local const/copy propagation pass first. The global pass
1789 only handles global opportunities.
1790 If the local pass changes something, remove any unreachable blocks
1791 because the CPROP global dataflow analysis may get into infinite
1792 loops for CFGs with unreachable blocks.
1794 FIXME: This local pass should not be necessary after CSE (but for
1795 some reason it still is). It is also (proven) not necessary
1796 to run the local pass right after FWPWOP.
1798 FIXME: The global analysis would not get into infinite loops if it
1799 would use the DF solver (via df_simple_dataflow) instead of
1800 the solver implemented in this file. */
1801 changed |= local_cprop_pass ();
1802 if (changed)
1803 delete_unreachable_blocks ();
1805 /* Determine implicit sets. This may change the CFG (split critical
1806 edges if that exposes an implicit set).
1807 Note that find_implicit_sets() does not rely on up-to-date DF caches
1808 so that we do not have to re-run df_analyze() even if local CPROP
1809 changed something.
1810 ??? This could run earlier so that any uncovered implicit sets
1811 sets could be exploited in local_cprop_pass() also. Later. */
1812 changed |= find_implicit_sets ();
1814 /* If local_cprop_pass() or find_implicit_sets() changed something,
1815 run df_analyze() to bring all insn caches up-to-date, and to take
1816 new basic blocks from edge splitting on the DF radar.
1817 NB: This also runs the fast DCE pass, because execute_rtl_cprop
1818 sets DF_LR_RUN_DCE. */
1819 if (changed)
1820 df_analyze ();
1822 /* Initialize implicit_set_indexes array. */
1823 implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun));
1824 for (i = 0; i < last_basic_block_for_fn (cfun); i++)
1825 implicit_set_indexes[i] = -1;
1827 alloc_hash_table (&set_hash_table);
1828 compute_hash_table (&set_hash_table);
1830 /* Free implicit_sets before peak usage. */
1831 free (implicit_sets);
1832 implicit_sets = NULL;
1834 if (dump_file)
1835 dump_hash_table (dump_file, "SET", &set_hash_table);
1836 if (set_hash_table.n_elems > 0)
1838 basic_block bb;
1839 rtx_insn *insn;
1841 alloc_cprop_mem (last_basic_block_for_fn (cfun),
1842 set_hash_table.n_elems);
1843 compute_cprop_data ();
1845 free (implicit_set_indexes);
1846 implicit_set_indexes = NULL;
1848 /* Allocate vars to track sets of regs. */
1849 reg_set_bitmap = ALLOC_REG_SET (NULL);
1851 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1852 EXIT_BLOCK_PTR_FOR_FN (cfun),
1853 next_bb)
1855 /* Reset tables used to keep track of what's still valid [since
1856 the start of the block]. */
1857 reset_opr_set_tables ();
1859 FOR_BB_INSNS (bb, insn)
1860 if (INSN_P (insn))
1862 changed |= cprop_insn (insn);
1864 /* Keep track of everything modified by this insn. */
1865 /* ??? Need to be careful w.r.t. mods done to INSN.
1866 Don't call mark_oprs_set if we turned the
1867 insn into a NOTE, or deleted the insn. */
1868 if (! NOTE_P (insn) && ! insn->deleted ())
1869 mark_oprs_set (insn);
1873 changed |= bypass_conditional_jumps ();
1875 FREE_REG_SET (reg_set_bitmap);
1876 free_cprop_mem ();
1878 else
1880 free (implicit_set_indexes);
1881 implicit_set_indexes = NULL;
1884 free_hash_table (&set_hash_table);
1885 obstack_free (&cprop_obstack, NULL);
1887 if (dump_file)
1889 fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1890 current_function_name (), n_basic_blocks_for_fn (cfun),
1891 bytes_used);
1892 fprintf (dump_file, "%d local const props, %d local copy props, ",
1893 local_const_prop_count, local_copy_prop_count);
1894 fprintf (dump_file, "%d global const props, %d global copy props\n\n",
1895 global_const_prop_count, global_copy_prop_count);
1898 return changed;
1901 /* All the passes implemented in this file. Each pass has its
1902 own gate and execute function, and at the end of the file a
1903 pass definition for passes.c.
1905 We do not construct an accurate cfg in functions which call
1906 setjmp, so none of these passes runs if the function calls
1907 setjmp.
1908 FIXME: Should just handle setjmp via REG_SETJMP notes. */
1910 static unsigned int
1911 execute_rtl_cprop (void)
1913 int changed;
1914 delete_unreachable_blocks ();
1915 df_set_flags (DF_LR_RUN_DCE);
1916 df_analyze ();
1917 changed = one_cprop_pass ();
1918 flag_rerun_cse_after_global_opts |= changed;
1919 if (changed)
1920 cleanup_cfg (CLEANUP_CFG_CHANGED);
1921 return 0;
1924 namespace {
1926 const pass_data pass_data_rtl_cprop =
1928 RTL_PASS, /* type */
1929 "cprop", /* name */
1930 OPTGROUP_NONE, /* optinfo_flags */
1931 TV_CPROP, /* tv_id */
1932 PROP_cfglayout, /* properties_required */
1933 0, /* properties_provided */
1934 0, /* properties_destroyed */
1935 0, /* todo_flags_start */
1936 TODO_df_finish, /* todo_flags_finish */
1939 class pass_rtl_cprop : public rtl_opt_pass
1941 public:
1942 pass_rtl_cprop (gcc::context *ctxt)
1943 : rtl_opt_pass (pass_data_rtl_cprop, ctxt)
1946 /* opt_pass methods: */
1947 opt_pass * clone () { return new pass_rtl_cprop (m_ctxt); }
1948 virtual bool gate (function *fun)
1950 return optimize > 0 && flag_gcse
1951 && !fun->calls_setjmp
1952 && dbg_cnt (cprop);
1955 virtual unsigned int execute (function *) { return execute_rtl_cprop (); }
1957 }; // class pass_rtl_cprop
1959 } // anon namespace
1961 rtl_opt_pass *
1962 make_pass_rtl_cprop (gcc::context *ctxt)
1964 return new pass_rtl_cprop (ctxt);