jit: API change to gcc_jit_context_new_global
[official-gcc.git] / gcc / cprop.c
blob4538291c078e77f8db28d5b4a9caadfb18b55e92
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 "vec.h"
45 #include "hashtab.h"
46 #include "hash-set.h"
47 #include "machmode.h"
48 #include "input.h"
49 #include "function.h"
50 #include "dominance.h"
51 #include "cfg.h"
52 #include "cfgrtl.h"
53 #include "cfganal.h"
54 #include "lcm.h"
55 #include "cfgcleanup.h"
56 #include "basic-block.h"
57 #include "expr.h"
58 #include "except.h"
59 #include "params.h"
60 #include "cselib.h"
61 #include "intl.h"
62 #include "obstack.h"
63 #include "tree-pass.h"
64 #include "df.h"
65 #include "dbgcnt.h"
66 #include "target.h"
67 #include "cfgloop.h"
70 /* An obstack for our working variables. */
71 static struct obstack cprop_obstack;
73 /* Occurrence of an expression.
74 There is one per basic block. If a pattern appears more than once the
75 last appearance is used. */
77 struct cprop_occr
79 /* Next occurrence of this expression. */
80 struct cprop_occr *next;
81 /* The insn that computes the expression. */
82 rtx_insn *insn;
85 typedef struct cprop_occr *occr_t;
87 /* Hash table entry for assignment expressions. */
89 struct cprop_expr
91 /* The expression (DEST := SRC). */
92 rtx dest;
93 rtx src;
95 /* Index in the available expression bitmaps. */
96 int bitmap_index;
97 /* Next entry with the same hash. */
98 struct cprop_expr *next_same_hash;
99 /* List of available occurrence in basic blocks in the function.
100 An "available occurrence" is one that is the last occurrence in the
101 basic block and whose operands are not modified by following statements
102 in the basic block [including this insn]. */
103 struct cprop_occr *avail_occr;
106 /* Hash table for copy propagation expressions.
107 Each hash table is an array of buckets.
108 ??? It is known that if it were an array of entries, structure elements
109 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
110 not clear whether in the final analysis a sufficient amount of memory would
111 be saved as the size of the available expression bitmaps would be larger
112 [one could build a mapping table without holes afterwards though].
113 Someday I'll perform the computation and figure it out. */
115 struct hash_table_d
117 /* The table itself.
118 This is an array of `set_hash_table_size' elements. */
119 struct cprop_expr **table;
121 /* Size of the hash table, in elements. */
122 unsigned int size;
124 /* Number of hash table elements. */
125 unsigned int n_elems;
128 /* Copy propagation hash table. */
129 static struct hash_table_d set_hash_table;
131 /* Array of implicit set patterns indexed by basic block index. */
132 static rtx *implicit_sets;
134 /* Array of indexes of expressions for implicit set patterns indexed by basic
135 block index. In other words, implicit_set_indexes[i] is the bitmap_index
136 of the expression whose RTX is implicit_sets[i]. */
137 static int *implicit_set_indexes;
139 /* Bitmap containing one bit for each register in the program.
140 Used when performing GCSE to track which registers have been set since
141 the start or end of the basic block while traversing that block. */
142 static regset reg_set_bitmap;
144 /* Various variables for statistics gathering. */
146 /* Memory used in a pass.
147 This isn't intended to be absolutely precise. Its intent is only
148 to keep an eye on memory usage. */
149 static int bytes_used;
151 /* Number of local constants propagated. */
152 static int local_const_prop_count;
153 /* Number of local copies propagated. */
154 static int local_copy_prop_count;
155 /* Number of global constants propagated. */
156 static int global_const_prop_count;
157 /* Number of global copies propagated. */
158 static int global_copy_prop_count;
160 #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T)))
161 #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S)))
163 /* Cover function to obstack_alloc. */
165 static void *
166 cprop_alloc (unsigned long size)
168 bytes_used += size;
169 return obstack_alloc (&cprop_obstack, size);
172 /* Return nonzero if register X is unchanged from INSN to the end
173 of INSN's basic block. */
175 static int
176 reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
178 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
181 /* Hash a set of register REGNO.
183 Sets are hashed on the register that is set. This simplifies the PRE copy
184 propagation code.
186 ??? May need to make things more elaborate. Later, as necessary. */
188 static unsigned int
189 hash_mod (int regno, int hash_table_size)
191 return (unsigned) regno % hash_table_size;
194 /* Insert assignment DEST:=SET from INSN in the hash table.
195 DEST is a register and SET is a register or a suitable constant.
196 If the assignment is already present in the table, record it as
197 the last occurrence in INSN's basic block.
198 IMPLICIT is true if it's an implicit set, false otherwise. */
200 static void
201 insert_set_in_table (rtx dest, rtx src, rtx_insn *insn,
202 struct hash_table_d *table, bool implicit)
204 bool found = false;
205 unsigned int hash;
206 struct cprop_expr *cur_expr, *last_expr = NULL;
207 struct cprop_occr *cur_occr;
209 hash = hash_mod (REGNO (dest), table->size);
211 for (cur_expr = table->table[hash]; cur_expr;
212 cur_expr = cur_expr->next_same_hash)
214 if (dest == cur_expr->dest
215 && src == cur_expr->src)
217 found = true;
218 break;
220 last_expr = cur_expr;
223 if (! found)
225 cur_expr = GOBNEW (struct cprop_expr);
226 bytes_used += sizeof (struct cprop_expr);
227 if (table->table[hash] == NULL)
228 /* This is the first pattern that hashed to this index. */
229 table->table[hash] = cur_expr;
230 else
231 /* Add EXPR to end of this hash chain. */
232 last_expr->next_same_hash = cur_expr;
234 /* Set the fields of the expr element.
235 We must copy X because it can be modified when copy propagation is
236 performed on its operands. */
237 cur_expr->dest = copy_rtx (dest);
238 cur_expr->src = copy_rtx (src);
239 cur_expr->bitmap_index = table->n_elems++;
240 cur_expr->next_same_hash = NULL;
241 cur_expr->avail_occr = NULL;
244 /* Now record the occurrence. */
245 cur_occr = cur_expr->avail_occr;
247 if (cur_occr
248 && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
250 /* Found another instance of the expression in the same basic block.
251 Prefer this occurrence to the currently recorded one. We want
252 the last one in the block and the block is scanned from start
253 to end. */
254 cur_occr->insn = insn;
256 else
258 /* First occurrence of this expression in this basic block. */
259 cur_occr = GOBNEW (struct cprop_occr);
260 bytes_used += sizeof (struct cprop_occr);
261 cur_occr->insn = insn;
262 cur_occr->next = cur_expr->avail_occr;
263 cur_expr->avail_occr = cur_occr;
266 /* Record bitmap_index of the implicit set in implicit_set_indexes. */
267 if (implicit)
268 implicit_set_indexes[BLOCK_FOR_INSN (insn)->index]
269 = cur_expr->bitmap_index;
272 /* Determine whether the rtx X should be treated as a constant for CPROP.
273 Since X might be inserted more than once we have to take care that it
274 is sharable. */
276 static bool
277 cprop_constant_p (const_rtx x)
279 return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
282 /* Scan SET present in INSN and add an entry to the hash TABLE.
283 IMPLICIT is true if it's an implicit set, false otherwise. */
285 static void
286 hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table,
287 bool implicit)
289 rtx src = SET_SRC (set);
290 rtx dest = SET_DEST (set);
292 if (REG_P (dest)
293 && ! HARD_REGISTER_P (dest)
294 && reg_available_p (dest, insn)
295 && can_copy_p (GET_MODE (dest)))
297 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
299 This allows us to do a single CPROP pass and still eliminate
300 redundant constants, addresses or other expressions that are
301 constructed with multiple instructions.
303 However, keep the original SRC if INSN is a simple reg-reg move. In
304 In this case, there will almost always be a REG_EQUAL note on the
305 insn that sets SRC. By recording the REG_EQUAL value here as SRC
306 for INSN, we miss copy propagation opportunities.
308 Note that this does not impede profitable constant propagations. We
309 "look through" reg-reg sets in lookup_set. */
310 rtx note = find_reg_equal_equiv_note (insn);
311 if (note != 0
312 && REG_NOTE_KIND (note) == REG_EQUAL
313 && !REG_P (src)
314 && cprop_constant_p (XEXP (note, 0)))
315 src = XEXP (note, 0), set = gen_rtx_SET (VOIDmode, dest, src);
317 /* Record sets for constant/copy propagation. */
318 if ((REG_P (src)
319 && src != dest
320 && ! HARD_REGISTER_P (src)
321 && reg_available_p (src, insn))
322 || cprop_constant_p (src))
323 insert_set_in_table (dest, src, insn, table, implicit);
327 /* Process INSN and add hash table entries as appropriate. */
329 static void
330 hash_scan_insn (rtx_insn *insn, struct hash_table_d *table)
332 rtx pat = PATTERN (insn);
333 int i;
335 /* Pick out the sets of INSN and for other forms of instructions record
336 what's been modified. */
338 if (GET_CODE (pat) == SET)
339 hash_scan_set (pat, insn, table, false);
340 else if (GET_CODE (pat) == PARALLEL)
341 for (i = 0; i < XVECLEN (pat, 0); i++)
343 rtx x = XVECEXP (pat, 0, i);
345 if (GET_CODE (x) == SET)
346 hash_scan_set (x, insn, table, false);
350 /* Dump the hash table TABLE to file FILE under the name NAME. */
352 static void
353 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
355 int i;
356 /* Flattened out table, so it's printed in proper order. */
357 struct cprop_expr **flat_table;
358 unsigned int *hash_val;
359 struct cprop_expr *expr;
361 flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems);
362 hash_val = XNEWVEC (unsigned int, table->n_elems);
364 for (i = 0; i < (int) table->size; i++)
365 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
367 flat_table[expr->bitmap_index] = expr;
368 hash_val[expr->bitmap_index] = i;
371 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
372 name, table->size, table->n_elems);
374 for (i = 0; i < (int) table->n_elems; i++)
375 if (flat_table[i] != 0)
377 expr = flat_table[i];
378 fprintf (file, "Index %d (hash value %d)\n ",
379 expr->bitmap_index, hash_val[i]);
380 print_rtl (file, expr->dest);
381 fprintf (file, " := ");
382 print_rtl (file, expr->src);
383 fprintf (file, "\n");
386 fprintf (file, "\n");
388 free (flat_table);
389 free (hash_val);
392 /* Record as unavailable all registers that are DEF operands of INSN. */
394 static void
395 make_set_regs_unavailable (rtx_insn *insn)
397 df_ref def;
399 FOR_EACH_INSN_DEF (def, insn)
400 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
403 /* Top level function to create an assignment hash table.
405 Assignment entries are placed in the hash table if
406 - they are of the form (set (pseudo-reg) src),
407 - src is something we want to perform const/copy propagation on,
408 - none of the operands or target are subsequently modified in the block
410 Currently src must be a pseudo-reg or a const_int.
412 TABLE is the table computed. */
414 static void
415 compute_hash_table_work (struct hash_table_d *table)
417 basic_block bb;
419 /* Allocate vars to track sets of regs. */
420 reg_set_bitmap = ALLOC_REG_SET (NULL);
422 FOR_EACH_BB_FN (bb, cfun)
424 rtx_insn *insn;
426 /* Reset tables used to keep track of what's not yet invalid [since
427 the end of the block]. */
428 CLEAR_REG_SET (reg_set_bitmap);
430 /* Go over all insns from the last to the first. This is convenient
431 for tracking available registers, i.e. not set between INSN and
432 the end of the basic block BB. */
433 FOR_BB_INSNS_REVERSE (bb, insn)
435 /* Only real insns are interesting. */
436 if (!NONDEBUG_INSN_P (insn))
437 continue;
439 /* Record interesting sets from INSN in the hash table. */
440 hash_scan_insn (insn, table);
442 /* Any registers set in INSN will make SETs above it not AVAIL. */
443 make_set_regs_unavailable (insn);
446 /* Insert implicit sets in the hash table, pretending they appear as
447 insns at the head of the basic block. */
448 if (implicit_sets[bb->index] != NULL_RTX)
449 hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
452 FREE_REG_SET (reg_set_bitmap);
455 /* Allocate space for the set/expr hash TABLE.
456 It is used to determine the number of buckets to use. */
458 static void
459 alloc_hash_table (struct hash_table_d *table)
461 int n;
463 n = get_max_insn_count ();
465 table->size = n / 4;
466 if (table->size < 11)
467 table->size = 11;
469 /* Attempt to maintain efficient use of hash table.
470 Making it an odd number is simplest for now.
471 ??? Later take some measurements. */
472 table->size |= 1;
473 n = table->size * sizeof (struct cprop_expr *);
474 table->table = XNEWVAR (struct cprop_expr *, n);
477 /* Free things allocated by alloc_hash_table. */
479 static void
480 free_hash_table (struct hash_table_d *table)
482 free (table->table);
485 /* Compute the hash TABLE for doing copy/const propagation or
486 expression hash table. */
488 static void
489 compute_hash_table (struct hash_table_d *table)
491 /* Initialize count of number of entries in hash table. */
492 table->n_elems = 0;
493 memset (table->table, 0, table->size * sizeof (struct cprop_expr *));
495 compute_hash_table_work (table);
498 /* Expression tracking support. */
500 /* Lookup REGNO in the set TABLE. The result is a pointer to the
501 table entry, or NULL if not found. */
503 static struct cprop_expr *
504 lookup_set (unsigned int regno, struct hash_table_d *table)
506 unsigned int hash = hash_mod (regno, table->size);
507 struct cprop_expr *expr;
509 expr = table->table[hash];
511 while (expr && REGNO (expr->dest) != regno)
512 expr = expr->next_same_hash;
514 return expr;
517 /* Return the next entry for REGNO in list EXPR. */
519 static struct cprop_expr *
520 next_set (unsigned int regno, struct cprop_expr *expr)
523 expr = expr->next_same_hash;
524 while (expr && REGNO (expr->dest) != regno);
526 return expr;
529 /* Reset tables used to keep track of what's still available [since the
530 start of the block]. */
532 static void
533 reset_opr_set_tables (void)
535 /* Maintain a bitmap of which regs have been set since beginning of
536 the block. */
537 CLEAR_REG_SET (reg_set_bitmap);
540 /* Return nonzero if the register X has not been set yet [since the
541 start of the basic block containing INSN]. */
543 static int
544 reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
546 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
549 /* Record things set by INSN.
550 This data is used by reg_not_set_p. */
552 static void
553 mark_oprs_set (rtx_insn *insn)
555 df_ref def;
557 FOR_EACH_INSN_DEF (def, insn)
558 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
561 /* Compute copy/constant propagation working variables. */
563 /* Local properties of assignments. */
564 static sbitmap *cprop_avloc;
565 static sbitmap *cprop_kill;
567 /* Global properties of assignments (computed from the local properties). */
568 static sbitmap *cprop_avin;
569 static sbitmap *cprop_avout;
571 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
572 basic blocks. N_SETS is the number of sets. */
574 static void
575 alloc_cprop_mem (int n_blocks, int n_sets)
577 cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
578 cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
580 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
581 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
584 /* Free vars used by copy/const propagation. */
586 static void
587 free_cprop_mem (void)
589 sbitmap_vector_free (cprop_avloc);
590 sbitmap_vector_free (cprop_kill);
591 sbitmap_vector_free (cprop_avin);
592 sbitmap_vector_free (cprop_avout);
595 /* Compute the local properties of each recorded expression.
597 Local properties are those that are defined by the block, irrespective of
598 other blocks.
600 An expression is killed in a block if its operands, either DEST or SRC, are
601 modified in the block.
603 An expression is computed (locally available) in a block if it is computed
604 at least once and expression would contain the same value if the
605 computation was moved to the end of the block.
607 KILL and COMP are destination sbitmaps for recording local properties. */
609 static void
610 compute_local_properties (sbitmap *kill, sbitmap *comp,
611 struct hash_table_d *table)
613 unsigned int i;
615 /* Initialize the bitmaps that were passed in. */
616 bitmap_vector_clear (kill, last_basic_block_for_fn (cfun));
617 bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
619 for (i = 0; i < table->size; i++)
621 struct cprop_expr *expr;
623 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
625 int indx = expr->bitmap_index;
626 df_ref def;
627 struct cprop_occr *occr;
629 /* For each definition of the destination pseudo-reg, the expression
630 is killed in the block where the definition is. */
631 for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
632 def; def = DF_REF_NEXT_REG (def))
633 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
635 /* If the source is a pseudo-reg, for each definition of the source,
636 the expression is killed in the block where the definition is. */
637 if (REG_P (expr->src))
638 for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
639 def; def = DF_REF_NEXT_REG (def))
640 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
642 /* The occurrences recorded in avail_occr are exactly those that
643 are locally available in the block where they are. */
644 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
646 bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
652 /* Hash table support. */
654 /* Top level routine to do the dataflow analysis needed by copy/const
655 propagation. */
657 static void
658 compute_cprop_data (void)
660 basic_block bb;
662 compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
663 compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
665 /* Merge implicit sets into CPROP_AVIN. They are always available at the
666 entry of their basic block. We need to do this because 1) implicit sets
667 aren't recorded for the local pass so they cannot be propagated within
668 their basic block by this pass and 2) the global pass would otherwise
669 propagate them only in the successors of their basic block. */
670 FOR_EACH_BB_FN (bb, cfun)
672 int index = implicit_set_indexes[bb->index];
673 if (index != -1)
674 bitmap_set_bit (cprop_avin[bb->index], index);
678 /* Copy/constant propagation. */
680 /* Maximum number of register uses in an insn that we handle. */
681 #define MAX_USES 8
683 /* Table of uses (registers, both hard and pseudo) found in an insn.
684 Allocated statically to avoid alloc/free complexity and overhead. */
685 static rtx reg_use_table[MAX_USES];
687 /* Index into `reg_use_table' while building it. */
688 static unsigned reg_use_count;
690 /* Set up a list of register numbers used in INSN. The found uses are stored
691 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
692 and contains the number of uses in the table upon exit.
694 ??? If a register appears multiple times we will record it multiple times.
695 This doesn't hurt anything but it will slow things down. */
697 static void
698 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
700 int i, j;
701 enum rtx_code code;
702 const char *fmt;
703 rtx x = *xptr;
705 /* repeat is used to turn tail-recursion into iteration since GCC
706 can't do it when there's no return value. */
707 repeat:
708 if (x == 0)
709 return;
711 code = GET_CODE (x);
712 if (REG_P (x))
714 if (reg_use_count == MAX_USES)
715 return;
717 reg_use_table[reg_use_count] = x;
718 reg_use_count++;
721 /* Recursively scan the operands of this expression. */
723 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
725 if (fmt[i] == 'e')
727 /* If we are about to do the last recursive call
728 needed at this level, change it into iteration.
729 This function is called enough to be worth it. */
730 if (i == 0)
732 x = XEXP (x, 0);
733 goto repeat;
736 find_used_regs (&XEXP (x, i), data);
738 else if (fmt[i] == 'E')
739 for (j = 0; j < XVECLEN (x, i); j++)
740 find_used_regs (&XVECEXP (x, i, j), data);
744 /* Try to replace all uses of FROM in INSN with TO.
745 Return nonzero if successful. */
747 static int
748 try_replace_reg (rtx from, rtx to, rtx_insn *insn)
750 rtx note = find_reg_equal_equiv_note (insn);
751 rtx src = 0;
752 int success = 0;
753 rtx set = single_set (insn);
755 /* Usually we substitute easy stuff, so we won't copy everything.
756 We however need to take care to not duplicate non-trivial CONST
757 expressions. */
758 to = copy_rtx (to);
760 validate_replace_src_group (from, to, insn);
761 if (num_changes_pending () && apply_change_group ())
762 success = 1;
764 /* Try to simplify SET_SRC if we have substituted a constant. */
765 if (success && set && CONSTANT_P (to))
767 src = simplify_rtx (SET_SRC (set));
769 if (src)
770 validate_change (insn, &SET_SRC (set), src, 0);
773 /* If there is already a REG_EQUAL note, update the expression in it
774 with our replacement. */
775 if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
776 set_unique_reg_note (insn, REG_EQUAL,
777 simplify_replace_rtx (XEXP (note, 0), from, to));
778 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
780 /* If above failed and this is a single set, try to simplify the source
781 of the set given our substitution. We could perhaps try this for
782 multiple SETs, but it probably won't buy us anything. */
783 src = simplify_replace_rtx (SET_SRC (set), from, to);
785 if (!rtx_equal_p (src, SET_SRC (set))
786 && validate_change (insn, &SET_SRC (set), src, 0))
787 success = 1;
789 /* If we've failed perform the replacement, have a single SET to
790 a REG destination and don't yet have a note, add a REG_EQUAL note
791 to not lose information. */
792 if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
793 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
796 if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
798 /* Registers can also appear as uses in SET_DEST if it is a MEM.
799 We could perhaps try this for multiple SETs, but it probably
800 won't buy us anything. */
801 rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
803 if (!rtx_equal_p (dest, SET_DEST (set))
804 && validate_change (insn, &SET_DEST (set), dest, 0))
805 success = 1;
808 /* REG_EQUAL may get simplified into register.
809 We don't allow that. Remove that note. This code ought
810 not to happen, because previous code ought to synthesize
811 reg-reg move, but be on the safe side. */
812 if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
813 remove_note (insn, note);
815 return success;
818 /* Find a set of REGNOs that are available on entry to INSN's block. Return
819 NULL no such set is found. */
821 static struct cprop_expr *
822 find_avail_set (int regno, rtx_insn *insn)
824 /* SET1 contains the last set found that can be returned to the caller for
825 use in a substitution. */
826 struct cprop_expr *set1 = 0;
828 /* Loops are not possible here. To get a loop we would need two sets
829 available at the start of the block containing INSN. i.e. we would
830 need two sets like this available at the start of the block:
832 (set (reg X) (reg Y))
833 (set (reg Y) (reg X))
835 This can not happen since the set of (reg Y) would have killed the
836 set of (reg X) making it unavailable at the start of this block. */
837 while (1)
839 rtx src;
840 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
842 /* Find a set that is available at the start of the block
843 which contains INSN. */
844 while (set)
846 if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
847 set->bitmap_index))
848 break;
849 set = next_set (regno, set);
852 /* If no available set was found we've reached the end of the
853 (possibly empty) copy chain. */
854 if (set == 0)
855 break;
857 src = set->src;
859 /* We know the set is available.
860 Now check that SRC is locally anticipatable (i.e. none of the
861 source operands have changed since the start of the block).
863 If the source operand changed, we may still use it for the next
864 iteration of this loop, but we may not use it for substitutions. */
866 if (cprop_constant_p (src) || reg_not_set_p (src, insn))
867 set1 = set;
869 /* If the source of the set is anything except a register, then
870 we have reached the end of the copy chain. */
871 if (! REG_P (src))
872 break;
874 /* Follow the copy chain, i.e. start another iteration of the loop
875 and see if we have an available copy into SRC. */
876 regno = REGNO (src);
879 /* SET1 holds the last set that was available and anticipatable at
880 INSN. */
881 return set1;
884 /* Subroutine of cprop_insn that tries to propagate constants into
885 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
886 it is the instruction that immediately precedes JUMP, and must be a
887 single SET of a register. FROM is what we will try to replace,
888 SRC is the constant we will try to substitute for it. Return nonzero
889 if a change was made. */
891 static int
892 cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src)
894 rtx new_rtx, set_src, note_src;
895 rtx set = pc_set (jump);
896 rtx note = find_reg_equal_equiv_note (jump);
898 if (note)
900 note_src = XEXP (note, 0);
901 if (GET_CODE (note_src) == EXPR_LIST)
902 note_src = NULL_RTX;
904 else note_src = NULL_RTX;
906 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
907 set_src = note_src ? note_src : SET_SRC (set);
909 /* First substitute the SETCC condition into the JUMP instruction,
910 then substitute that given values into this expanded JUMP. */
911 if (setcc != NULL_RTX
912 && !modified_between_p (from, setcc, jump)
913 && !modified_between_p (src, setcc, jump))
915 rtx setcc_src;
916 rtx setcc_set = single_set (setcc);
917 rtx setcc_note = find_reg_equal_equiv_note (setcc);
918 setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
919 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
920 set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
921 setcc_src);
923 else
924 setcc = NULL;
926 new_rtx = simplify_replace_rtx (set_src, from, src);
928 /* If no simplification can be made, then try the next register. */
929 if (rtx_equal_p (new_rtx, SET_SRC (set)))
930 return 0;
932 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
933 if (new_rtx == pc_rtx)
934 delete_insn (jump);
935 else
937 /* Ensure the value computed inside the jump insn to be equivalent
938 to one computed by setcc. */
939 if (setcc && modified_in_p (new_rtx, setcc))
940 return 0;
941 if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
943 /* When (some) constants are not valid in a comparison, and there
944 are two registers to be replaced by constants before the entire
945 comparison can be folded into a constant, we need to keep
946 intermediate information in REG_EQUAL notes. For targets with
947 separate compare insns, such notes are added by try_replace_reg.
948 When we have a combined compare-and-branch instruction, however,
949 we need to attach a note to the branch itself to make this
950 optimization work. */
952 if (!rtx_equal_p (new_rtx, note_src))
953 set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
954 return 0;
957 /* Remove REG_EQUAL note after simplification. */
958 if (note_src)
959 remove_note (jump, note);
962 #ifdef HAVE_cc0
963 /* Delete the cc0 setter. */
964 if (setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
965 delete_insn (setcc);
966 #endif
968 global_const_prop_count++;
969 if (dump_file != NULL)
971 fprintf (dump_file,
972 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with"
973 "constant ", REGNO (from), INSN_UID (jump));
974 print_rtl (dump_file, src);
975 fprintf (dump_file, "\n");
977 purge_dead_edges (bb);
979 /* If a conditional jump has been changed into unconditional jump, remove
980 the jump and make the edge fallthru - this is always called in
981 cfglayout mode. */
982 if (new_rtx != pc_rtx && simplejump_p (jump))
984 edge e;
985 edge_iterator ei;
987 FOR_EACH_EDGE (e, ei, bb->succs)
988 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
989 && BB_HEAD (e->dest) == JUMP_LABEL (jump))
991 e->flags |= EDGE_FALLTHRU;
992 break;
994 delete_insn (jump);
997 return 1;
1000 /* Subroutine of cprop_insn that tries to propagate constants. FROM is what
1001 we will try to replace, SRC is the constant we will try to substitute for
1002 it and INSN is the instruction where this will be happening. */
1004 static int
1005 constprop_register (rtx from, rtx src, rtx_insn *insn)
1007 rtx sset;
1009 /* Check for reg or cc0 setting instructions followed by
1010 conditional branch instructions first. */
1011 if ((sset = single_set (insn)) != NULL
1012 && NEXT_INSN (insn)
1013 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
1015 rtx dest = SET_DEST (sset);
1016 if ((REG_P (dest) || CC0_P (dest))
1017 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
1018 from, src))
1019 return 1;
1022 /* Handle normal insns next. */
1023 if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
1024 return 1;
1026 /* Try to propagate a CONST_INT into a conditional jump.
1027 We're pretty specific about what we will handle in this
1028 code, we can extend this as necessary over time.
1030 Right now the insn in question must look like
1031 (set (pc) (if_then_else ...)) */
1032 else if (any_condjump_p (insn) && onlyjump_p (insn))
1033 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
1034 return 0;
1037 /* Perform constant and copy propagation on INSN.
1038 Return nonzero if a change was made. */
1040 static int
1041 cprop_insn (rtx_insn *insn)
1043 unsigned i;
1044 int changed = 0, changed_this_round;
1045 rtx note;
1047 retry:
1048 changed_this_round = 0;
1049 reg_use_count = 0;
1050 note_uses (&PATTERN (insn), find_used_regs, NULL);
1052 /* We may win even when propagating constants into notes. */
1053 note = find_reg_equal_equiv_note (insn);
1054 if (note)
1055 find_used_regs (&XEXP (note, 0), NULL);
1057 for (i = 0; i < reg_use_count; i++)
1059 rtx reg_used = reg_use_table[i];
1060 unsigned int regno = REGNO (reg_used);
1061 rtx src;
1062 struct cprop_expr *set;
1064 /* If the register has already been set in this block, there's
1065 nothing we can do. */
1066 if (! reg_not_set_p (reg_used, insn))
1067 continue;
1069 /* Find an assignment that sets reg_used and is available
1070 at the start of the block. */
1071 set = find_avail_set (regno, insn);
1072 if (! set)
1073 continue;
1075 src = set->src;
1077 /* Constant propagation. */
1078 if (cprop_constant_p (src))
1080 if (constprop_register (reg_used, src, insn))
1082 changed_this_round = changed = 1;
1083 global_const_prop_count++;
1084 if (dump_file != NULL)
1086 fprintf (dump_file,
1087 "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
1088 fprintf (dump_file, "insn %d with constant ",
1089 INSN_UID (insn));
1090 print_rtl (dump_file, src);
1091 fprintf (dump_file, "\n");
1093 if (insn->deleted ())
1094 return 1;
1097 else if (REG_P (src)
1098 && REGNO (src) >= FIRST_PSEUDO_REGISTER
1099 && REGNO (src) != regno)
1101 if (try_replace_reg (reg_used, src, insn))
1103 changed_this_round = changed = 1;
1104 global_copy_prop_count++;
1105 if (dump_file != NULL)
1107 fprintf (dump_file,
1108 "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1109 regno, INSN_UID (insn));
1110 fprintf (dump_file, " with reg %d\n", REGNO (src));
1113 /* The original insn setting reg_used may or may not now be
1114 deletable. We leave the deletion to DCE. */
1115 /* FIXME: If it turns out that the insn isn't deletable,
1116 then we may have unnecessarily extended register lifetimes
1117 and made things worse. */
1121 /* If try_replace_reg simplified the insn, the regs found
1122 by find_used_regs may not be valid anymore. Start over. */
1123 if (changed_this_round)
1124 goto retry;
1127 if (changed && DEBUG_INSN_P (insn))
1128 return 0;
1130 return changed;
1133 /* Like find_used_regs, but avoid recording uses that appear in
1134 input-output contexts such as zero_extract or pre_dec. This
1135 restricts the cases we consider to those for which local cprop
1136 can legitimately make replacements. */
1138 static void
1139 local_cprop_find_used_regs (rtx *xptr, void *data)
1141 rtx x = *xptr;
1143 if (x == 0)
1144 return;
1146 switch (GET_CODE (x))
1148 case ZERO_EXTRACT:
1149 case SIGN_EXTRACT:
1150 case STRICT_LOW_PART:
1151 return;
1153 case PRE_DEC:
1154 case PRE_INC:
1155 case POST_DEC:
1156 case POST_INC:
1157 case PRE_MODIFY:
1158 case POST_MODIFY:
1159 /* Can only legitimately appear this early in the context of
1160 stack pushes for function arguments, but handle all of the
1161 codes nonetheless. */
1162 return;
1164 case SUBREG:
1165 /* Setting a subreg of a register larger than word_mode leaves
1166 the non-written words unchanged. */
1167 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
1168 return;
1169 break;
1171 default:
1172 break;
1175 find_used_regs (xptr, data);
1178 /* Try to perform local const/copy propagation on X in INSN. */
1180 static bool
1181 do_local_cprop (rtx x, rtx_insn *insn)
1183 rtx newreg = NULL, newcnst = NULL;
1185 /* Rule out USE instructions and ASM statements as we don't want to
1186 change the hard registers mentioned. */
1187 if (REG_P (x)
1188 && (REGNO (x) >= FIRST_PSEUDO_REGISTER
1189 || (GET_CODE (PATTERN (insn)) != USE
1190 && asm_noperands (PATTERN (insn)) < 0)))
1192 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
1193 struct elt_loc_list *l;
1195 if (!val)
1196 return false;
1197 for (l = val->locs; l; l = l->next)
1199 rtx this_rtx = l->loc;
1200 rtx note;
1202 if (cprop_constant_p (this_rtx))
1203 newcnst = this_rtx;
1204 if (REG_P (this_rtx) && REGNO (this_rtx) >= FIRST_PSEUDO_REGISTER
1205 /* Don't copy propagate if it has attached REG_EQUIV note.
1206 At this point this only function parameters should have
1207 REG_EQUIV notes and if the argument slot is used somewhere
1208 explicitly, it means address of parameter has been taken,
1209 so we should not extend the lifetime of the pseudo. */
1210 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
1211 || ! MEM_P (XEXP (note, 0))))
1212 newreg = this_rtx;
1214 if (newcnst && constprop_register (x, newcnst, insn))
1216 if (dump_file != NULL)
1218 fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
1219 REGNO (x));
1220 fprintf (dump_file, "insn %d with constant ",
1221 INSN_UID (insn));
1222 print_rtl (dump_file, newcnst);
1223 fprintf (dump_file, "\n");
1225 local_const_prop_count++;
1226 return true;
1228 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
1230 if (dump_file != NULL)
1232 fprintf (dump_file,
1233 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
1234 REGNO (x), INSN_UID (insn));
1235 fprintf (dump_file, " with reg %d\n", REGNO (newreg));
1237 local_copy_prop_count++;
1238 return true;
1241 return false;
1244 /* Do local const/copy propagation (i.e. within each basic block). */
1246 static int
1247 local_cprop_pass (void)
1249 basic_block bb;
1250 rtx_insn *insn;
1251 bool changed = false;
1252 unsigned i;
1254 cselib_init (0);
1255 FOR_EACH_BB_FN (bb, cfun)
1257 FOR_BB_INSNS (bb, insn)
1259 if (INSN_P (insn))
1261 rtx note = find_reg_equal_equiv_note (insn);
1264 reg_use_count = 0;
1265 note_uses (&PATTERN (insn), local_cprop_find_used_regs,
1266 NULL);
1267 if (note)
1268 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
1270 for (i = 0; i < reg_use_count; i++)
1272 if (do_local_cprop (reg_use_table[i], insn))
1274 if (!DEBUG_INSN_P (insn))
1275 changed = true;
1276 break;
1279 if (insn->deleted ())
1280 break;
1282 while (i < reg_use_count);
1284 cselib_process_insn (insn);
1287 /* Forget everything at the end of a basic block. */
1288 cselib_clear_table ();
1291 cselib_finish ();
1293 return changed;
1296 /* Similar to get_condition, only the resulting condition must be
1297 valid at JUMP, instead of at EARLIEST.
1299 This differs from noce_get_condition in ifcvt.c in that we prefer not to
1300 settle for the condition variable in the jump instruction being integral.
1301 We prefer to be able to record the value of a user variable, rather than
1302 the value of a temporary used in a condition. This could be solved by
1303 recording the value of *every* register scanned by canonicalize_condition,
1304 but this would require some code reorganization. */
1307 fis_get_condition (rtx_insn *jump)
1309 return get_condition (jump, NULL, false, true);
1312 /* Check the comparison COND to see if we can safely form an implicit
1313 set from it. */
1315 static bool
1316 implicit_set_cond_p (const_rtx cond)
1318 machine_mode mode;
1319 rtx cst;
1321 /* COND must be either an EQ or NE comparison. */
1322 if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
1323 return false;
1325 /* The first operand of COND must be a pseudo-reg. */
1326 if (! REG_P (XEXP (cond, 0))
1327 || HARD_REGISTER_P (XEXP (cond, 0)))
1328 return false;
1330 /* The second operand of COND must be a suitable constant. */
1331 mode = GET_MODE (XEXP (cond, 0));
1332 cst = XEXP (cond, 1);
1334 /* We can't perform this optimization if either operand might be or might
1335 contain a signed zero. */
1336 if (HONOR_SIGNED_ZEROS (mode))
1338 /* It is sufficient to check if CST is or contains a zero. We must
1339 handle float, complex, and vector. If any subpart is a zero, then
1340 the optimization can't be performed. */
1341 /* ??? The complex and vector checks are not implemented yet. We just
1342 always return zero for them. */
1343 if (CONST_DOUBLE_AS_FLOAT_P (cst))
1345 REAL_VALUE_TYPE d;
1346 REAL_VALUE_FROM_CONST_DOUBLE (d, cst);
1347 if (REAL_VALUES_EQUAL (d, dconst0))
1348 return 0;
1350 else
1351 return 0;
1354 return cprop_constant_p (cst);
1357 /* Find the implicit sets of a function. An "implicit set" is a constraint
1358 on the value of a variable, implied by a conditional jump. For example,
1359 following "if (x == 2)", the then branch may be optimized as though the
1360 conditional performed an "explicit set", in this example, "x = 2". This
1361 function records the set patterns that are implicit at the start of each
1362 basic block.
1364 If an implicit set is found but the set is implicit on a critical edge,
1365 this critical edge is split.
1367 Return true if the CFG was modified, false otherwise. */
1369 static bool
1370 find_implicit_sets (void)
1372 basic_block bb, dest;
1373 rtx cond, new_rtx;
1374 unsigned int count = 0;
1375 bool edges_split = false;
1376 size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10;
1378 implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1380 FOR_EACH_BB_FN (bb, cfun)
1382 /* Check for more than one successor. */
1383 if (EDGE_COUNT (bb->succs) <= 1)
1384 continue;
1386 cond = fis_get_condition (BB_END (bb));
1388 /* If no condition is found or if it isn't of a suitable form,
1389 ignore it. */
1390 if (! cond || ! implicit_set_cond_p (cond))
1391 continue;
1393 dest = GET_CODE (cond) == EQ
1394 ? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
1396 /* If DEST doesn't go anywhere, ignore it. */
1397 if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1398 continue;
1400 /* We have found a suitable implicit set. Try to record it now as
1401 a SET in DEST. If DEST has more than one predecessor, the edge
1402 between BB and DEST is a critical edge and we must split it,
1403 because we can only record one implicit set per DEST basic block. */
1404 if (! single_pred_p (dest))
1406 dest = split_edge (find_edge (bb, dest));
1407 edges_split = true;
1410 if (implicit_sets_size <= (size_t) dest->index)
1412 size_t old_implicit_sets_size = implicit_sets_size;
1413 implicit_sets_size *= 2;
1414 implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
1415 memset (implicit_sets + old_implicit_sets_size, 0,
1416 (implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1419 new_rtx = gen_rtx_SET (VOIDmode, XEXP (cond, 0),
1420 XEXP (cond, 1));
1421 implicit_sets[dest->index] = new_rtx;
1422 if (dump_file)
1424 fprintf (dump_file, "Implicit set of reg %d in ",
1425 REGNO (XEXP (cond, 0)));
1426 fprintf (dump_file, "basic block %d\n", dest->index);
1428 count++;
1431 if (dump_file)
1432 fprintf (dump_file, "Found %d implicit sets\n", count);
1434 /* Confess our sins. */
1435 return edges_split;
1438 /* Bypass conditional jumps. */
1440 /* The value of last_basic_block at the beginning of the jump_bypass
1441 pass. The use of redirect_edge_and_branch_force may introduce new
1442 basic blocks, but the data flow analysis is only valid for basic
1443 block indices less than bypass_last_basic_block. */
1445 static int bypass_last_basic_block;
1447 /* Find a set of REGNO to a constant that is available at the end of basic
1448 block BB. Return NULL if no such set is found. Based heavily upon
1449 find_avail_set. */
1451 static struct cprop_expr *
1452 find_bypass_set (int regno, int bb)
1454 struct cprop_expr *result = 0;
1456 for (;;)
1458 rtx src;
1459 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
1461 while (set)
1463 if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
1464 break;
1465 set = next_set (regno, set);
1468 if (set == 0)
1469 break;
1471 src = set->src;
1472 if (cprop_constant_p (src))
1473 result = set;
1475 if (! REG_P (src))
1476 break;
1478 regno = REGNO (src);
1480 return result;
1483 /* Subroutine of bypass_block that checks whether a pseudo is killed by
1484 any of the instructions inserted on an edge. Jump bypassing places
1485 condition code setters on CFG edges using insert_insn_on_edge. This
1486 function is required to check that our data flow analysis is still
1487 valid prior to commit_edge_insertions. */
1489 static bool
1490 reg_killed_on_edge (const_rtx reg, const_edge e)
1492 rtx_insn *insn;
1494 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
1495 if (INSN_P (insn) && reg_set_p (reg, insn))
1496 return true;
1498 return false;
1501 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
1502 basic block BB which has more than one predecessor. If not NULL, SETCC
1503 is the first instruction of BB, which is immediately followed by JUMP_INSN
1504 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
1505 Returns nonzero if a change was made.
1507 During the jump bypassing pass, we may place copies of SETCC instructions
1508 on CFG edges. The following routine must be careful to pay attention to
1509 these inserted insns when performing its transformations. */
1511 static int
1512 bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump)
1514 rtx_insn *insn;
1515 rtx note;
1516 edge e, edest;
1517 int change;
1518 int may_be_loop_header = false;
1519 unsigned removed_p;
1520 unsigned i;
1521 edge_iterator ei;
1523 insn = (setcc != NULL) ? setcc : jump;
1525 /* Determine set of register uses in INSN. */
1526 reg_use_count = 0;
1527 note_uses (&PATTERN (insn), find_used_regs, NULL);
1528 note = find_reg_equal_equiv_note (insn);
1529 if (note)
1530 find_used_regs (&XEXP (note, 0), NULL);
1532 if (current_loops)
1534 /* If we are to preserve loop structure then do not bypass
1535 a loop header. This will either rotate the loop, create
1536 multiple entry loops or even irreducible regions. */
1537 if (bb == bb->loop_father->header)
1538 return 0;
1540 else
1542 FOR_EACH_EDGE (e, ei, bb->preds)
1543 if (e->flags & EDGE_DFS_BACK)
1545 may_be_loop_header = true;
1546 break;
1550 change = 0;
1551 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1553 removed_p = 0;
1555 if (e->flags & EDGE_COMPLEX)
1557 ei_next (&ei);
1558 continue;
1561 /* We can't redirect edges from new basic blocks. */
1562 if (e->src->index >= bypass_last_basic_block)
1564 ei_next (&ei);
1565 continue;
1568 /* The irreducible loops created by redirecting of edges entering the
1569 loop from outside would decrease effectiveness of some of the
1570 following optimizations, so prevent this. */
1571 if (may_be_loop_header
1572 && !(e->flags & EDGE_DFS_BACK))
1574 ei_next (&ei);
1575 continue;
1578 for (i = 0; i < reg_use_count; i++)
1580 rtx reg_used = reg_use_table[i];
1581 unsigned int regno = REGNO (reg_used);
1582 basic_block dest, old_dest;
1583 struct cprop_expr *set;
1584 rtx src, new_rtx;
1586 set = find_bypass_set (regno, e->src->index);
1588 if (! set)
1589 continue;
1591 /* Check the data flow is valid after edge insertions. */
1592 if (e->insns.r && reg_killed_on_edge (reg_used, e))
1593 continue;
1595 src = SET_SRC (pc_set (jump));
1597 if (setcc != NULL)
1598 src = simplify_replace_rtx (src,
1599 SET_DEST (PATTERN (setcc)),
1600 SET_SRC (PATTERN (setcc)));
1602 new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1604 /* Jump bypassing may have already placed instructions on
1605 edges of the CFG. We can't bypass an outgoing edge that
1606 has instructions associated with it, as these insns won't
1607 get executed if the incoming edge is redirected. */
1608 if (new_rtx == pc_rtx)
1610 edest = FALLTHRU_EDGE (bb);
1611 dest = edest->insns.r ? NULL : edest->dest;
1613 else if (GET_CODE (new_rtx) == LABEL_REF)
1615 dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
1616 /* Don't bypass edges containing instructions. */
1617 edest = find_edge (bb, dest);
1618 if (edest && edest->insns.r)
1619 dest = NULL;
1621 else
1622 dest = NULL;
1624 /* Avoid unification of the edge with other edges from original
1625 branch. We would end up emitting the instruction on "both"
1626 edges. */
1627 if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
1628 && find_edge (e->src, dest))
1629 dest = NULL;
1631 old_dest = e->dest;
1632 if (dest != NULL
1633 && dest != old_dest
1634 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1636 redirect_edge_and_branch_force (e, dest);
1638 /* Copy the register setter to the redirected edge.
1639 Don't copy CC0 setters, as CC0 is dead after jump. */
1640 if (setcc)
1642 rtx pat = PATTERN (setcc);
1643 if (!CC0_P (SET_DEST (pat)))
1644 insert_insn_on_edge (copy_insn (pat), e);
1647 if (dump_file != NULL)
1649 fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
1650 "in jump_insn %d equals constant ",
1651 regno, INSN_UID (jump));
1652 print_rtl (dump_file, set->src);
1653 fprintf (dump_file, "\n\t when BB %d is entered from "
1654 "BB %d. Redirect edge %d->%d to %d.\n",
1655 old_dest->index, e->src->index, e->src->index,
1656 old_dest->index, dest->index);
1658 change = 1;
1659 removed_p = 1;
1660 break;
1663 if (!removed_p)
1664 ei_next (&ei);
1666 return change;
1669 /* Find basic blocks with more than one predecessor that only contain a
1670 single conditional jump. If the result of the comparison is known at
1671 compile-time from any incoming edge, redirect that edge to the
1672 appropriate target. Return nonzero if a change was made.
1674 This function is now mis-named, because we also handle indirect jumps. */
1676 static int
1677 bypass_conditional_jumps (void)
1679 basic_block bb;
1680 int changed;
1681 rtx_insn *setcc;
1682 rtx_insn *insn;
1683 rtx dest;
1685 /* Note we start at block 1. */
1686 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1687 return 0;
1689 bypass_last_basic_block = last_basic_block_for_fn (cfun);
1690 mark_dfs_back_edges ();
1692 changed = 0;
1693 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1694 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1696 /* Check for more than one predecessor. */
1697 if (!single_pred_p (bb))
1699 setcc = NULL;
1700 FOR_BB_INSNS (bb, insn)
1701 if (DEBUG_INSN_P (insn))
1702 continue;
1703 else if (NONJUMP_INSN_P (insn))
1705 if (setcc)
1706 break;
1707 if (GET_CODE (PATTERN (insn)) != SET)
1708 break;
1710 dest = SET_DEST (PATTERN (insn));
1711 if (REG_P (dest) || CC0_P (dest))
1712 setcc = insn;
1713 else
1714 break;
1716 else if (JUMP_P (insn))
1718 if ((any_condjump_p (insn) || computed_jump_p (insn))
1719 && onlyjump_p (insn))
1720 changed |= bypass_block (bb, setcc, insn);
1721 break;
1723 else if (INSN_P (insn))
1724 break;
1728 /* If we bypassed any register setting insns, we inserted a
1729 copy on the redirected edge. These need to be committed. */
1730 if (changed)
1731 commit_edge_insertions ();
1733 return changed;
1736 /* Return true if the graph is too expensive to optimize. PASS is the
1737 optimization about to be performed. */
1739 static bool
1740 is_too_expensive (const char *pass)
1742 /* Trying to perform global optimizations on flow graphs which have
1743 a high connectivity will take a long time and is unlikely to be
1744 particularly useful.
1746 In normal circumstances a cfg should have about twice as many
1747 edges as blocks. But we do not want to punish small functions
1748 which have a couple switch statements. Rather than simply
1749 threshold the number of blocks, uses something with a more
1750 graceful degradation. */
1751 if (n_edges_for_fn (cfun) > 20000 + n_basic_blocks_for_fn (cfun) * 4)
1753 warning (OPT_Wdisabled_optimization,
1754 "%s: %d basic blocks and %d edges/basic block",
1755 pass, n_basic_blocks_for_fn (cfun),
1756 n_edges_for_fn (cfun) / n_basic_blocks_for_fn (cfun));
1758 return true;
1761 /* If allocating memory for the cprop bitmap would take up too much
1762 storage it's better just to disable the optimization. */
1763 if ((n_basic_blocks_for_fn (cfun)
1764 * SBITMAP_SET_SIZE (max_reg_num ())
1765 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
1767 warning (OPT_Wdisabled_optimization,
1768 "%s: %d basic blocks and %d registers",
1769 pass, n_basic_blocks_for_fn (cfun), max_reg_num ());
1771 return true;
1774 return false;
1777 /* Main function for the CPROP pass. */
1779 static int
1780 one_cprop_pass (void)
1782 int i;
1783 int changed = 0;
1785 /* Return if there's nothing to do, or it is too expensive. */
1786 if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
1787 || is_too_expensive (_ ("const/copy propagation disabled")))
1788 return 0;
1790 global_const_prop_count = local_const_prop_count = 0;
1791 global_copy_prop_count = local_copy_prop_count = 0;
1793 bytes_used = 0;
1794 gcc_obstack_init (&cprop_obstack);
1796 /* Do a local const/copy propagation pass first. The global pass
1797 only handles global opportunities.
1798 If the local pass changes something, remove any unreachable blocks
1799 because the CPROP global dataflow analysis may get into infinite
1800 loops for CFGs with unreachable blocks.
1802 FIXME: This local pass should not be necessary after CSE (but for
1803 some reason it still is). It is also (proven) not necessary
1804 to run the local pass right after FWPWOP.
1806 FIXME: The global analysis would not get into infinite loops if it
1807 would use the DF solver (via df_simple_dataflow) instead of
1808 the solver implemented in this file. */
1809 changed |= local_cprop_pass ();
1810 if (changed)
1811 delete_unreachable_blocks ();
1813 /* Determine implicit sets. This may change the CFG (split critical
1814 edges if that exposes an implicit set).
1815 Note that find_implicit_sets() does not rely on up-to-date DF caches
1816 so that we do not have to re-run df_analyze() even if local CPROP
1817 changed something.
1818 ??? This could run earlier so that any uncovered implicit sets
1819 sets could be exploited in local_cprop_pass() also. Later. */
1820 changed |= find_implicit_sets ();
1822 /* If local_cprop_pass() or find_implicit_sets() changed something,
1823 run df_analyze() to bring all insn caches up-to-date, and to take
1824 new basic blocks from edge splitting on the DF radar.
1825 NB: This also runs the fast DCE pass, because execute_rtl_cprop
1826 sets DF_LR_RUN_DCE. */
1827 if (changed)
1828 df_analyze ();
1830 /* Initialize implicit_set_indexes array. */
1831 implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun));
1832 for (i = 0; i < last_basic_block_for_fn (cfun); i++)
1833 implicit_set_indexes[i] = -1;
1835 alloc_hash_table (&set_hash_table);
1836 compute_hash_table (&set_hash_table);
1838 /* Free implicit_sets before peak usage. */
1839 free (implicit_sets);
1840 implicit_sets = NULL;
1842 if (dump_file)
1843 dump_hash_table (dump_file, "SET", &set_hash_table);
1844 if (set_hash_table.n_elems > 0)
1846 basic_block bb;
1847 rtx_insn *insn;
1849 alloc_cprop_mem (last_basic_block_for_fn (cfun),
1850 set_hash_table.n_elems);
1851 compute_cprop_data ();
1853 free (implicit_set_indexes);
1854 implicit_set_indexes = NULL;
1856 /* Allocate vars to track sets of regs. */
1857 reg_set_bitmap = ALLOC_REG_SET (NULL);
1859 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1860 EXIT_BLOCK_PTR_FOR_FN (cfun),
1861 next_bb)
1863 /* Reset tables used to keep track of what's still valid [since
1864 the start of the block]. */
1865 reset_opr_set_tables ();
1867 FOR_BB_INSNS (bb, insn)
1868 if (INSN_P (insn))
1870 changed |= cprop_insn (insn);
1872 /* Keep track of everything modified by this insn. */
1873 /* ??? Need to be careful w.r.t. mods done to INSN.
1874 Don't call mark_oprs_set if we turned the
1875 insn into a NOTE, or deleted the insn. */
1876 if (! NOTE_P (insn) && ! insn->deleted ())
1877 mark_oprs_set (insn);
1881 changed |= bypass_conditional_jumps ();
1883 FREE_REG_SET (reg_set_bitmap);
1884 free_cprop_mem ();
1886 else
1888 free (implicit_set_indexes);
1889 implicit_set_indexes = NULL;
1892 free_hash_table (&set_hash_table);
1893 obstack_free (&cprop_obstack, NULL);
1895 if (dump_file)
1897 fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1898 current_function_name (), n_basic_blocks_for_fn (cfun),
1899 bytes_used);
1900 fprintf (dump_file, "%d local const props, %d local copy props, ",
1901 local_const_prop_count, local_copy_prop_count);
1902 fprintf (dump_file, "%d global const props, %d global copy props\n\n",
1903 global_const_prop_count, global_copy_prop_count);
1906 return changed;
1909 /* All the passes implemented in this file. Each pass has its
1910 own gate and execute function, and at the end of the file a
1911 pass definition for passes.c.
1913 We do not construct an accurate cfg in functions which call
1914 setjmp, so none of these passes runs if the function calls
1915 setjmp.
1916 FIXME: Should just handle setjmp via REG_SETJMP notes. */
1918 static unsigned int
1919 execute_rtl_cprop (void)
1921 int changed;
1922 delete_unreachable_blocks ();
1923 df_set_flags (DF_LR_RUN_DCE);
1924 df_analyze ();
1925 changed = one_cprop_pass ();
1926 flag_rerun_cse_after_global_opts |= changed;
1927 if (changed)
1928 cleanup_cfg (CLEANUP_CFG_CHANGED);
1929 return 0;
1932 namespace {
1934 const pass_data pass_data_rtl_cprop =
1936 RTL_PASS, /* type */
1937 "cprop", /* name */
1938 OPTGROUP_NONE, /* optinfo_flags */
1939 TV_CPROP, /* tv_id */
1940 PROP_cfglayout, /* properties_required */
1941 0, /* properties_provided */
1942 0, /* properties_destroyed */
1943 0, /* todo_flags_start */
1944 TODO_df_finish, /* todo_flags_finish */
1947 class pass_rtl_cprop : public rtl_opt_pass
1949 public:
1950 pass_rtl_cprop (gcc::context *ctxt)
1951 : rtl_opt_pass (pass_data_rtl_cprop, ctxt)
1954 /* opt_pass methods: */
1955 opt_pass * clone () { return new pass_rtl_cprop (m_ctxt); }
1956 virtual bool gate (function *fun)
1958 return optimize > 0 && flag_gcse
1959 && !fun->calls_setjmp
1960 && dbg_cnt (cprop);
1963 virtual unsigned int execute (function *) { return execute_rtl_cprop (); }
1965 }; // class pass_rtl_cprop
1967 } // anon namespace
1969 rtl_opt_pass *
1970 make_pass_rtl_cprop (gcc::context *ctxt)
1972 return new pass_rtl_cprop (ctxt);