re PR other/63387 (Optimize pairs of isnan() calls into a single isunordered())
[official-gcc.git] / gcc / cprop.c
blob57c44ef58b19f18f3ad60cea63d931a09f57bc9c
1 /* Global constant/copy propagation for RTL.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "tm.h"
24 #include "diagnostic-core.h"
25 #include "toplev.h"
26 #include "rtl.h"
27 #include "hash-set.h"
28 #include "machmode.h"
29 #include "vec.h"
30 #include "double-int.h"
31 #include "input.h"
32 #include "alias.h"
33 #include "symtab.h"
34 #include "wide-int.h"
35 #include "inchash.h"
36 #include "tree.h"
37 #include "tm_p.h"
38 #include "regs.h"
39 #include "hard-reg-set.h"
40 #include "flags.h"
41 #include "insn-config.h"
42 #include "recog.h"
43 #include "predict.h"
44 #include "hashtab.h"
45 #include "function.h"
46 #include "dominance.h"
47 #include "cfg.h"
48 #include "cfgrtl.h"
49 #include "cfganal.h"
50 #include "lcm.h"
51 #include "cfgcleanup.h"
52 #include "basic-block.h"
53 #include "statistics.h"
54 #include "real.h"
55 #include "fixed-value.h"
56 #include "expmed.h"
57 #include "dojump.h"
58 #include "explow.h"
59 #include "calls.h"
60 #include "emit-rtl.h"
61 #include "varasm.h"
62 #include "stmt.h"
63 #include "expr.h"
64 #include "except.h"
65 #include "params.h"
66 #include "cselib.h"
67 #include "intl.h"
68 #include "obstack.h"
69 #include "tree-pass.h"
70 #include "df.h"
71 #include "dbgcnt.h"
72 #include "target.h"
73 #include "cfgloop.h"
76 /* An obstack for our working variables. */
77 static struct obstack cprop_obstack;
79 /* Occurrence of an expression.
80 There is one per basic block. If a pattern appears more than once the
81 last appearance is used. */
83 struct cprop_occr
85 /* Next occurrence of this expression. */
86 struct cprop_occr *next;
87 /* The insn that computes the expression. */
88 rtx_insn *insn;
91 typedef struct cprop_occr *occr_t;
93 /* Hash table entry for assignment expressions. */
95 struct cprop_expr
97 /* The expression (DEST := SRC). */
98 rtx dest;
99 rtx src;
101 /* Index in the available expression bitmaps. */
102 int bitmap_index;
103 /* Next entry with the same hash. */
104 struct cprop_expr *next_same_hash;
105 /* List of available occurrence in basic blocks in the function.
106 An "available occurrence" is one that is the last occurrence in the
107 basic block and whose operands are not modified by following statements
108 in the basic block [including this insn]. */
109 struct cprop_occr *avail_occr;
112 /* Hash table for copy propagation expressions.
113 Each hash table is an array of buckets.
114 ??? It is known that if it were an array of entries, structure elements
115 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
116 not clear whether in the final analysis a sufficient amount of memory would
117 be saved as the size of the available expression bitmaps would be larger
118 [one could build a mapping table without holes afterwards though].
119 Someday I'll perform the computation and figure it out. */
121 struct hash_table_d
123 /* The table itself.
124 This is an array of `set_hash_table_size' elements. */
125 struct cprop_expr **table;
127 /* Size of the hash table, in elements. */
128 unsigned int size;
130 /* Number of hash table elements. */
131 unsigned int n_elems;
134 /* Copy propagation hash table. */
135 static struct hash_table_d set_hash_table;
137 /* Array of implicit set patterns indexed by basic block index. */
138 static rtx *implicit_sets;
140 /* Array of indexes of expressions for implicit set patterns indexed by basic
141 block index. In other words, implicit_set_indexes[i] is the bitmap_index
142 of the expression whose RTX is implicit_sets[i]. */
143 static int *implicit_set_indexes;
145 /* Bitmap containing one bit for each register in the program.
146 Used when performing GCSE to track which registers have been set since
147 the start or end of the basic block while traversing that block. */
148 static regset reg_set_bitmap;
150 /* Various variables for statistics gathering. */
152 /* Memory used in a pass.
153 This isn't intended to be absolutely precise. Its intent is only
154 to keep an eye on memory usage. */
155 static int bytes_used;
157 /* Number of local constants propagated. */
158 static int local_const_prop_count;
159 /* Number of local copies propagated. */
160 static int local_copy_prop_count;
161 /* Number of global constants propagated. */
162 static int global_const_prop_count;
163 /* Number of global copies propagated. */
164 static int global_copy_prop_count;
166 #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T)))
167 #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S)))
169 /* Cover function to obstack_alloc. */
171 static void *
172 cprop_alloc (unsigned long size)
174 bytes_used += size;
175 return obstack_alloc (&cprop_obstack, size);
178 /* Return nonzero if register X is unchanged from INSN to the end
179 of INSN's basic block. */
181 static int
182 reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
184 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
187 /* Hash a set of register REGNO.
189 Sets are hashed on the register that is set. This simplifies the PRE copy
190 propagation code.
192 ??? May need to make things more elaborate. Later, as necessary. */
194 static unsigned int
195 hash_mod (int regno, int hash_table_size)
197 return (unsigned) regno % hash_table_size;
200 /* Insert assignment DEST:=SET from INSN in the hash table.
201 DEST is a register and SET is a register or a suitable constant.
202 If the assignment is already present in the table, record it as
203 the last occurrence in INSN's basic block.
204 IMPLICIT is true if it's an implicit set, false otherwise. */
206 static void
207 insert_set_in_table (rtx dest, rtx src, rtx_insn *insn,
208 struct hash_table_d *table, bool implicit)
210 bool found = false;
211 unsigned int hash;
212 struct cprop_expr *cur_expr, *last_expr = NULL;
213 struct cprop_occr *cur_occr;
215 hash = hash_mod (REGNO (dest), table->size);
217 for (cur_expr = table->table[hash]; cur_expr;
218 cur_expr = cur_expr->next_same_hash)
220 if (dest == cur_expr->dest
221 && src == cur_expr->src)
223 found = true;
224 break;
226 last_expr = cur_expr;
229 if (! found)
231 cur_expr = GOBNEW (struct cprop_expr);
232 bytes_used += sizeof (struct cprop_expr);
233 if (table->table[hash] == NULL)
234 /* This is the first pattern that hashed to this index. */
235 table->table[hash] = cur_expr;
236 else
237 /* Add EXPR to end of this hash chain. */
238 last_expr->next_same_hash = cur_expr;
240 /* Set the fields of the expr element.
241 We must copy X because it can be modified when copy propagation is
242 performed on its operands. */
243 cur_expr->dest = copy_rtx (dest);
244 cur_expr->src = copy_rtx (src);
245 cur_expr->bitmap_index = table->n_elems++;
246 cur_expr->next_same_hash = NULL;
247 cur_expr->avail_occr = NULL;
250 /* Now record the occurrence. */
251 cur_occr = cur_expr->avail_occr;
253 if (cur_occr
254 && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
256 /* Found another instance of the expression in the same basic block.
257 Prefer this occurrence to the currently recorded one. We want
258 the last one in the block and the block is scanned from start
259 to end. */
260 cur_occr->insn = insn;
262 else
264 /* First occurrence of this expression in this basic block. */
265 cur_occr = GOBNEW (struct cprop_occr);
266 bytes_used += sizeof (struct cprop_occr);
267 cur_occr->insn = insn;
268 cur_occr->next = cur_expr->avail_occr;
269 cur_expr->avail_occr = cur_occr;
272 /* Record bitmap_index of the implicit set in implicit_set_indexes. */
273 if (implicit)
274 implicit_set_indexes[BLOCK_FOR_INSN (insn)->index]
275 = cur_expr->bitmap_index;
278 /* Determine whether the rtx X should be treated as a constant for CPROP.
279 Since X might be inserted more than once we have to take care that it
280 is sharable. */
282 static bool
283 cprop_constant_p (const_rtx x)
285 return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
288 /* Determine whether the rtx X should be treated as a register that can
289 be propagated. Any pseudo-register is fine. */
291 static bool
292 cprop_reg_p (const_rtx x)
294 return REG_P (x) && !HARD_REGISTER_P (x);
297 /* Scan SET present in INSN and add an entry to the hash TABLE.
298 IMPLICIT is true if it's an implicit set, false otherwise. */
300 static void
301 hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table,
302 bool implicit)
304 rtx src = SET_SRC (set);
305 rtx dest = SET_DEST (set);
307 if (cprop_reg_p (dest)
308 && reg_available_p (dest, insn)
309 && can_copy_p (GET_MODE (dest)))
311 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
313 This allows us to do a single CPROP pass and still eliminate
314 redundant constants, addresses or other expressions that are
315 constructed with multiple instructions.
317 However, keep the original SRC if INSN is a simple reg-reg move. In
318 In this case, there will almost always be a REG_EQUAL note on the
319 insn that sets SRC. By recording the REG_EQUAL value here as SRC
320 for INSN, we miss copy propagation opportunities.
322 Note that this does not impede profitable constant propagations. We
323 "look through" reg-reg sets in lookup_set. */
324 rtx note = find_reg_equal_equiv_note (insn);
325 if (note != 0
326 && REG_NOTE_KIND (note) == REG_EQUAL
327 && !REG_P (src)
328 && cprop_constant_p (XEXP (note, 0)))
329 src = XEXP (note, 0), set = gen_rtx_SET (dest, src);
331 /* Record sets for constant/copy propagation. */
332 if ((cprop_reg_p (src)
333 && src != dest
334 && reg_available_p (src, insn))
335 || cprop_constant_p (src))
336 insert_set_in_table (dest, src, insn, table, implicit);
340 /* Process INSN and add hash table entries as appropriate. */
342 static void
343 hash_scan_insn (rtx_insn *insn, struct hash_table_d *table)
345 rtx pat = PATTERN (insn);
346 int i;
348 /* Pick out the sets of INSN and for other forms of instructions record
349 what's been modified. */
351 if (GET_CODE (pat) == SET)
352 hash_scan_set (pat, insn, table, false);
353 else if (GET_CODE (pat) == PARALLEL)
354 for (i = 0; i < XVECLEN (pat, 0); i++)
356 rtx x = XVECEXP (pat, 0, i);
358 if (GET_CODE (x) == SET)
359 hash_scan_set (x, insn, table, false);
363 /* Dump the hash table TABLE to file FILE under the name NAME. */
365 static void
366 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
368 int i;
369 /* Flattened out table, so it's printed in proper order. */
370 struct cprop_expr **flat_table;
371 unsigned int *hash_val;
372 struct cprop_expr *expr;
374 flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems);
375 hash_val = XNEWVEC (unsigned int, table->n_elems);
377 for (i = 0; i < (int) table->size; i++)
378 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
380 flat_table[expr->bitmap_index] = expr;
381 hash_val[expr->bitmap_index] = i;
384 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
385 name, table->size, table->n_elems);
387 for (i = 0; i < (int) table->n_elems; i++)
388 if (flat_table[i] != 0)
390 expr = flat_table[i];
391 fprintf (file, "Index %d (hash value %d)\n ",
392 expr->bitmap_index, hash_val[i]);
393 print_rtl (file, expr->dest);
394 fprintf (file, " := ");
395 print_rtl (file, expr->src);
396 fprintf (file, "\n");
399 fprintf (file, "\n");
401 free (flat_table);
402 free (hash_val);
405 /* Record as unavailable all registers that are DEF operands of INSN. */
407 static void
408 make_set_regs_unavailable (rtx_insn *insn)
410 df_ref def;
412 FOR_EACH_INSN_DEF (def, insn)
413 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
416 /* Top level function to create an assignment hash table.
418 Assignment entries are placed in the hash table if
419 - they are of the form (set (pseudo-reg) src),
420 - src is something we want to perform const/copy propagation on,
421 - none of the operands or target are subsequently modified in the block
423 Currently src must be a pseudo-reg or a const_int.
425 TABLE is the table computed. */
427 static void
428 compute_hash_table_work (struct hash_table_d *table)
430 basic_block bb;
432 /* Allocate vars to track sets of regs. */
433 reg_set_bitmap = ALLOC_REG_SET (NULL);
435 FOR_EACH_BB_FN (bb, cfun)
437 rtx_insn *insn;
439 /* Reset tables used to keep track of what's not yet invalid [since
440 the end of the block]. */
441 CLEAR_REG_SET (reg_set_bitmap);
443 /* Go over all insns from the last to the first. This is convenient
444 for tracking available registers, i.e. not set between INSN and
445 the end of the basic block BB. */
446 FOR_BB_INSNS_REVERSE (bb, insn)
448 /* Only real insns are interesting. */
449 if (!NONDEBUG_INSN_P (insn))
450 continue;
452 /* Record interesting sets from INSN in the hash table. */
453 hash_scan_insn (insn, table);
455 /* Any registers set in INSN will make SETs above it not AVAIL. */
456 make_set_regs_unavailable (insn);
459 /* Insert implicit sets in the hash table, pretending they appear as
460 insns at the head of the basic block. */
461 if (implicit_sets[bb->index] != NULL_RTX)
462 hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
465 FREE_REG_SET (reg_set_bitmap);
468 /* Allocate space for the set/expr hash TABLE.
469 It is used to determine the number of buckets to use. */
471 static void
472 alloc_hash_table (struct hash_table_d *table)
474 int n;
476 n = get_max_insn_count ();
478 table->size = n / 4;
479 if (table->size < 11)
480 table->size = 11;
482 /* Attempt to maintain efficient use of hash table.
483 Making it an odd number is simplest for now.
484 ??? Later take some measurements. */
485 table->size |= 1;
486 n = table->size * sizeof (struct cprop_expr *);
487 table->table = XNEWVAR (struct cprop_expr *, n);
490 /* Free things allocated by alloc_hash_table. */
492 static void
493 free_hash_table (struct hash_table_d *table)
495 free (table->table);
498 /* Compute the hash TABLE for doing copy/const propagation or
499 expression hash table. */
501 static void
502 compute_hash_table (struct hash_table_d *table)
504 /* Initialize count of number of entries in hash table. */
505 table->n_elems = 0;
506 memset (table->table, 0, table->size * sizeof (struct cprop_expr *));
508 compute_hash_table_work (table);
511 /* Expression tracking support. */
513 /* Lookup REGNO in the set TABLE. The result is a pointer to the
514 table entry, or NULL if not found. */
516 static struct cprop_expr *
517 lookup_set (unsigned int regno, struct hash_table_d *table)
519 unsigned int hash = hash_mod (regno, table->size);
520 struct cprop_expr *expr;
522 expr = table->table[hash];
524 while (expr && REGNO (expr->dest) != regno)
525 expr = expr->next_same_hash;
527 return expr;
530 /* Return the next entry for REGNO in list EXPR. */
532 static struct cprop_expr *
533 next_set (unsigned int regno, struct cprop_expr *expr)
536 expr = expr->next_same_hash;
537 while (expr && REGNO (expr->dest) != regno);
539 return expr;
542 /* Reset tables used to keep track of what's still available [since the
543 start of the block]. */
545 static void
546 reset_opr_set_tables (void)
548 /* Maintain a bitmap of which regs have been set since beginning of
549 the block. */
550 CLEAR_REG_SET (reg_set_bitmap);
553 /* Return nonzero if the register X has not been set yet [since the
554 start of the basic block containing INSN]. */
556 static int
557 reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
559 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
562 /* Record things set by INSN.
563 This data is used by reg_not_set_p. */
565 static void
566 mark_oprs_set (rtx_insn *insn)
568 df_ref def;
570 FOR_EACH_INSN_DEF (def, insn)
571 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
574 /* Compute copy/constant propagation working variables. */
576 /* Local properties of assignments. */
577 static sbitmap *cprop_avloc;
578 static sbitmap *cprop_kill;
580 /* Global properties of assignments (computed from the local properties). */
581 static sbitmap *cprop_avin;
582 static sbitmap *cprop_avout;
584 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
585 basic blocks. N_SETS is the number of sets. */
587 static void
588 alloc_cprop_mem (int n_blocks, int n_sets)
590 cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
591 cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
593 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
594 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
597 /* Free vars used by copy/const propagation. */
599 static void
600 free_cprop_mem (void)
602 sbitmap_vector_free (cprop_avloc);
603 sbitmap_vector_free (cprop_kill);
604 sbitmap_vector_free (cprop_avin);
605 sbitmap_vector_free (cprop_avout);
608 /* Compute the local properties of each recorded expression.
610 Local properties are those that are defined by the block, irrespective of
611 other blocks.
613 An expression is killed in a block if its operands, either DEST or SRC, are
614 modified in the block.
616 An expression is computed (locally available) in a block if it is computed
617 at least once and expression would contain the same value if the
618 computation was moved to the end of the block.
620 KILL and COMP are destination sbitmaps for recording local properties. */
622 static void
623 compute_local_properties (sbitmap *kill, sbitmap *comp,
624 struct hash_table_d *table)
626 unsigned int i;
628 /* Initialize the bitmaps that were passed in. */
629 bitmap_vector_clear (kill, last_basic_block_for_fn (cfun));
630 bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
632 for (i = 0; i < table->size; i++)
634 struct cprop_expr *expr;
636 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
638 int indx = expr->bitmap_index;
639 df_ref def;
640 struct cprop_occr *occr;
642 /* For each definition of the destination pseudo-reg, the expression
643 is killed in the block where the definition is. */
644 for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
645 def; def = DF_REF_NEXT_REG (def))
646 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
648 /* If the source is a pseudo-reg, for each definition of the source,
649 the expression is killed in the block where the definition is. */
650 if (REG_P (expr->src))
651 for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
652 def; def = DF_REF_NEXT_REG (def))
653 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
655 /* The occurrences recorded in avail_occr are exactly those that
656 are locally available in the block where they are. */
657 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
659 bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
665 /* Hash table support. */
667 /* Top level routine to do the dataflow analysis needed by copy/const
668 propagation. */
670 static void
671 compute_cprop_data (void)
673 basic_block bb;
675 compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
676 compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
678 /* Merge implicit sets into CPROP_AVIN. They are always available at the
679 entry of their basic block. We need to do this because 1) implicit sets
680 aren't recorded for the local pass so they cannot be propagated within
681 their basic block by this pass and 2) the global pass would otherwise
682 propagate them only in the successors of their basic block. */
683 FOR_EACH_BB_FN (bb, cfun)
685 int index = implicit_set_indexes[bb->index];
686 if (index != -1)
687 bitmap_set_bit (cprop_avin[bb->index], index);
691 /* Copy/constant propagation. */
693 /* Maximum number of register uses in an insn that we handle. */
694 #define MAX_USES 8
696 /* Table of uses (registers, both hard and pseudo) found in an insn.
697 Allocated statically to avoid alloc/free complexity and overhead. */
698 static rtx reg_use_table[MAX_USES];
700 /* Index into `reg_use_table' while building it. */
701 static unsigned reg_use_count;
703 /* Set up a list of register numbers used in INSN. The found uses are stored
704 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
705 and contains the number of uses in the table upon exit.
707 ??? If a register appears multiple times we will record it multiple times.
708 This doesn't hurt anything but it will slow things down. */
710 static void
711 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
713 int i, j;
714 enum rtx_code code;
715 const char *fmt;
716 rtx x = *xptr;
718 /* repeat is used to turn tail-recursion into iteration since GCC
719 can't do it when there's no return value. */
720 repeat:
721 if (x == 0)
722 return;
724 code = GET_CODE (x);
725 if (REG_P (x))
727 if (reg_use_count == MAX_USES)
728 return;
730 reg_use_table[reg_use_count] = x;
731 reg_use_count++;
734 /* Recursively scan the operands of this expression. */
736 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
738 if (fmt[i] == 'e')
740 /* If we are about to do the last recursive call
741 needed at this level, change it into iteration.
742 This function is called enough to be worth it. */
743 if (i == 0)
745 x = XEXP (x, 0);
746 goto repeat;
749 find_used_regs (&XEXP (x, i), data);
751 else if (fmt[i] == 'E')
752 for (j = 0; j < XVECLEN (x, i); j++)
753 find_used_regs (&XVECEXP (x, i, j), data);
757 /* Try to replace all uses of FROM in INSN with TO.
758 Return nonzero if successful. */
760 static int
761 try_replace_reg (rtx from, rtx to, rtx_insn *insn)
763 rtx note = find_reg_equal_equiv_note (insn);
764 rtx src = 0;
765 int success = 0;
766 rtx set = single_set (insn);
768 /* Usually we substitute easy stuff, so we won't copy everything.
769 We however need to take care to not duplicate non-trivial CONST
770 expressions. */
771 to = copy_rtx (to);
773 validate_replace_src_group (from, to, insn);
774 if (num_changes_pending () && apply_change_group ())
775 success = 1;
777 /* Try to simplify SET_SRC if we have substituted a constant. */
778 if (success && set && CONSTANT_P (to))
780 src = simplify_rtx (SET_SRC (set));
782 if (src)
783 validate_change (insn, &SET_SRC (set), src, 0);
786 /* If there is already a REG_EQUAL note, update the expression in it
787 with our replacement. */
788 if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
789 set_unique_reg_note (insn, REG_EQUAL,
790 simplify_replace_rtx (XEXP (note, 0), from, to));
791 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
793 /* If above failed and this is a single set, try to simplify the source
794 of the set given our substitution. We could perhaps try this for
795 multiple SETs, but it probably won't buy us anything. */
796 src = simplify_replace_rtx (SET_SRC (set), from, to);
798 if (!rtx_equal_p (src, SET_SRC (set))
799 && validate_change (insn, &SET_SRC (set), src, 0))
800 success = 1;
802 /* If we've failed perform the replacement, have a single SET to
803 a REG destination and don't yet have a note, add a REG_EQUAL note
804 to not lose information. */
805 if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
806 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
809 if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
811 /* Registers can also appear as uses in SET_DEST if it is a MEM.
812 We could perhaps try this for multiple SETs, but it probably
813 won't buy us anything. */
814 rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
816 if (!rtx_equal_p (dest, SET_DEST (set))
817 && validate_change (insn, &SET_DEST (set), dest, 0))
818 success = 1;
821 /* REG_EQUAL may get simplified into register.
822 We don't allow that. Remove that note. This code ought
823 not to happen, because previous code ought to synthesize
824 reg-reg move, but be on the safe side. */
825 if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
826 remove_note (insn, note);
828 return success;
831 /* Find a set of REGNOs that are available on entry to INSN's block. If found,
832 SET_RET[0] will be assigned a set with a register source and SET_RET[1] a
833 set with a constant source. If not found the corresponding entry is set to
834 NULL. */
836 static void
837 find_avail_set (int regno, rtx_insn *insn, struct cprop_expr *set_ret[2])
839 set_ret[0] = set_ret[1] = NULL;
841 /* Loops are not possible here. To get a loop we would need two sets
842 available at the start of the block containing INSN. i.e. we would
843 need two sets like this available at the start of the block:
845 (set (reg X) (reg Y))
846 (set (reg Y) (reg X))
848 This can not happen since the set of (reg Y) would have killed the
849 set of (reg X) making it unavailable at the start of this block. */
850 while (1)
852 rtx src;
853 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
855 /* Find a set that is available at the start of the block
856 which contains INSN. */
857 while (set)
859 if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
860 set->bitmap_index))
861 break;
862 set = next_set (regno, set);
865 /* If no available set was found we've reached the end of the
866 (possibly empty) copy chain. */
867 if (set == 0)
868 break;
870 src = set->src;
872 /* We know the set is available.
873 Now check that SRC is locally anticipatable (i.e. none of the
874 source operands have changed since the start of the block).
876 If the source operand changed, we may still use it for the next
877 iteration of this loop, but we may not use it for substitutions. */
879 if (cprop_constant_p (src))
880 set_ret[1] = set;
881 else if (reg_not_set_p (src, insn))
882 set_ret[0] = set;
884 /* If the source of the set is anything except a register, then
885 we have reached the end of the copy chain. */
886 if (! REG_P (src))
887 break;
889 /* Follow the copy chain, i.e. start another iteration of the loop
890 and see if we have an available copy into SRC. */
891 regno = REGNO (src);
895 /* Subroutine of cprop_insn that tries to propagate constants into
896 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
897 it is the instruction that immediately precedes JUMP, and must be a
898 single SET of a register. FROM is what we will try to replace,
899 SRC is the constant we will try to substitute for it. Return nonzero
900 if a change was made. */
902 static int
903 cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src)
905 rtx new_rtx, set_src, note_src;
906 rtx set = pc_set (jump);
907 rtx note = find_reg_equal_equiv_note (jump);
909 if (note)
911 note_src = XEXP (note, 0);
912 if (GET_CODE (note_src) == EXPR_LIST)
913 note_src = NULL_RTX;
915 else note_src = NULL_RTX;
917 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
918 set_src = note_src ? note_src : SET_SRC (set);
920 /* First substitute the SETCC condition into the JUMP instruction,
921 then substitute that given values into this expanded JUMP. */
922 if (setcc != NULL_RTX
923 && !modified_between_p (from, setcc, jump)
924 && !modified_between_p (src, setcc, jump))
926 rtx setcc_src;
927 rtx setcc_set = single_set (setcc);
928 rtx setcc_note = find_reg_equal_equiv_note (setcc);
929 setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
930 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
931 set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
932 setcc_src);
934 else
935 setcc = NULL;
937 new_rtx = simplify_replace_rtx (set_src, from, src);
939 /* If no simplification can be made, then try the next register. */
940 if (rtx_equal_p (new_rtx, SET_SRC (set)))
941 return 0;
943 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
944 if (new_rtx == pc_rtx)
945 delete_insn (jump);
946 else
948 /* Ensure the value computed inside the jump insn to be equivalent
949 to one computed by setcc. */
950 if (setcc && modified_in_p (new_rtx, setcc))
951 return 0;
952 if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
954 /* When (some) constants are not valid in a comparison, and there
955 are two registers to be replaced by constants before the entire
956 comparison can be folded into a constant, we need to keep
957 intermediate information in REG_EQUAL notes. For targets with
958 separate compare insns, such notes are added by try_replace_reg.
959 When we have a combined compare-and-branch instruction, however,
960 we need to attach a note to the branch itself to make this
961 optimization work. */
963 if (!rtx_equal_p (new_rtx, note_src))
964 set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
965 return 0;
968 /* Remove REG_EQUAL note after simplification. */
969 if (note_src)
970 remove_note (jump, note);
973 /* Delete the cc0 setter. */
974 if (HAVE_cc0 && setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
975 delete_insn (setcc);
977 global_const_prop_count++;
978 if (dump_file != NULL)
980 fprintf (dump_file,
981 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with"
982 "constant ", REGNO (from), INSN_UID (jump));
983 print_rtl (dump_file, src);
984 fprintf (dump_file, "\n");
986 purge_dead_edges (bb);
988 /* If a conditional jump has been changed into unconditional jump, remove
989 the jump and make the edge fallthru - this is always called in
990 cfglayout mode. */
991 if (new_rtx != pc_rtx && simplejump_p (jump))
993 edge e;
994 edge_iterator ei;
996 FOR_EACH_EDGE (e, ei, bb->succs)
997 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
998 && BB_HEAD (e->dest) == JUMP_LABEL (jump))
1000 e->flags |= EDGE_FALLTHRU;
1001 break;
1003 delete_insn (jump);
1006 return 1;
1009 /* Subroutine of cprop_insn that tries to propagate constants. FROM is what
1010 we will try to replace, SRC is the constant we will try to substitute for
1011 it and INSN is the instruction where this will be happening. */
1013 static int
1014 constprop_register (rtx from, rtx src, rtx_insn *insn)
1016 rtx sset;
1018 /* Check for reg or cc0 setting instructions followed by
1019 conditional branch instructions first. */
1020 if ((sset = single_set (insn)) != NULL
1021 && NEXT_INSN (insn)
1022 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
1024 rtx dest = SET_DEST (sset);
1025 if ((REG_P (dest) || CC0_P (dest))
1026 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
1027 from, src))
1028 return 1;
1031 /* Handle normal insns next. */
1032 if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
1033 return 1;
1035 /* Try to propagate a CONST_INT into a conditional jump.
1036 We're pretty specific about what we will handle in this
1037 code, we can extend this as necessary over time.
1039 Right now the insn in question must look like
1040 (set (pc) (if_then_else ...)) */
1041 else if (any_condjump_p (insn) && onlyjump_p (insn))
1042 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
1043 return 0;
1046 /* Perform constant and copy propagation on INSN.
1047 Return nonzero if a change was made. */
1049 static int
1050 cprop_insn (rtx_insn *insn)
1052 unsigned i;
1053 int changed = 0, changed_this_round;
1054 rtx note;
1058 changed_this_round = 0;
1059 reg_use_count = 0;
1060 note_uses (&PATTERN (insn), find_used_regs, NULL);
1062 /* We may win even when propagating constants into notes. */
1063 note = find_reg_equal_equiv_note (insn);
1064 if (note)
1065 find_used_regs (&XEXP (note, 0), NULL);
1067 for (i = 0; i < reg_use_count; i++)
1069 rtx reg_used = reg_use_table[i];
1070 unsigned int regno = REGNO (reg_used);
1071 rtx src_cst = NULL, src_reg = NULL;
1072 struct cprop_expr *set[2];
1074 /* If the register has already been set in this block, there's
1075 nothing we can do. */
1076 if (! reg_not_set_p (reg_used, insn))
1077 continue;
1079 /* Find an assignment that sets reg_used and is available
1080 at the start of the block. */
1081 find_avail_set (regno, insn, set);
1082 if (set[0])
1083 src_reg = set[0]->src;
1084 if (set[1])
1085 src_cst = set[1]->src;
1087 /* Constant propagation. */
1088 if (src_cst && cprop_constant_p (src_cst)
1089 && constprop_register (reg_used, src_cst, insn))
1091 changed_this_round = changed = 1;
1092 global_const_prop_count++;
1093 if (dump_file != NULL)
1095 fprintf (dump_file,
1096 "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
1097 fprintf (dump_file, "insn %d with constant ",
1098 INSN_UID (insn));
1099 print_rtl (dump_file, src_cst);
1100 fprintf (dump_file, "\n");
1102 if (insn->deleted ())
1103 return 1;
1105 /* Copy propagation. */
1106 else if (src_reg && cprop_reg_p (src_reg)
1107 && REGNO (src_reg) != regno
1108 && try_replace_reg (reg_used, src_reg, insn))
1110 changed_this_round = changed = 1;
1111 global_copy_prop_count++;
1112 if (dump_file != NULL)
1114 fprintf (dump_file,
1115 "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1116 regno, INSN_UID (insn));
1117 fprintf (dump_file, " with reg %d\n", REGNO (src_reg));
1120 /* The original insn setting reg_used may or may not now be
1121 deletable. We leave the deletion to DCE. */
1122 /* FIXME: If it turns out that the insn isn't deletable,
1123 then we may have unnecessarily extended register lifetimes
1124 and made things worse. */
1128 /* If try_replace_reg simplified the insn, the regs found by find_used_regs
1129 may not be valid anymore. Start over. */
1130 while (changed_this_round);
1132 if (changed && DEBUG_INSN_P (insn))
1133 return 0;
1135 return changed;
1138 /* Like find_used_regs, but avoid recording uses that appear in
1139 input-output contexts such as zero_extract or pre_dec. This
1140 restricts the cases we consider to those for which local cprop
1141 can legitimately make replacements. */
1143 static void
1144 local_cprop_find_used_regs (rtx *xptr, void *data)
1146 rtx x = *xptr;
1148 if (x == 0)
1149 return;
1151 switch (GET_CODE (x))
1153 case ZERO_EXTRACT:
1154 case SIGN_EXTRACT:
1155 case STRICT_LOW_PART:
1156 return;
1158 case PRE_DEC:
1159 case PRE_INC:
1160 case POST_DEC:
1161 case POST_INC:
1162 case PRE_MODIFY:
1163 case POST_MODIFY:
1164 /* Can only legitimately appear this early in the context of
1165 stack pushes for function arguments, but handle all of the
1166 codes nonetheless. */
1167 return;
1169 case SUBREG:
1170 /* Setting a subreg of a register larger than word_mode leaves
1171 the non-written words unchanged. */
1172 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
1173 return;
1174 break;
1176 default:
1177 break;
1180 find_used_regs (xptr, data);
1183 /* Try to perform local const/copy propagation on X in INSN. */
1185 static bool
1186 do_local_cprop (rtx x, rtx_insn *insn)
1188 rtx newreg = NULL, newcnst = NULL;
1190 /* Rule out USE instructions and ASM statements as we don't want to
1191 change the hard registers mentioned. */
1192 if (REG_P (x)
1193 && (cprop_reg_p (x)
1194 || (GET_CODE (PATTERN (insn)) != USE
1195 && asm_noperands (PATTERN (insn)) < 0)))
1197 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
1198 struct elt_loc_list *l;
1200 if (!val)
1201 return false;
1202 for (l = val->locs; l; l = l->next)
1204 rtx this_rtx = l->loc;
1205 rtx note;
1207 if (cprop_constant_p (this_rtx))
1208 newcnst = this_rtx;
1209 if (cprop_reg_p (this_rtx)
1210 /* Don't copy propagate if it has attached REG_EQUIV note.
1211 At this point this only function parameters should have
1212 REG_EQUIV notes and if the argument slot is used somewhere
1213 explicitly, it means address of parameter has been taken,
1214 so we should not extend the lifetime of the pseudo. */
1215 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
1216 || ! MEM_P (XEXP (note, 0))))
1217 newreg = this_rtx;
1219 if (newcnst && constprop_register (x, newcnst, insn))
1221 if (dump_file != NULL)
1223 fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
1224 REGNO (x));
1225 fprintf (dump_file, "insn %d with constant ",
1226 INSN_UID (insn));
1227 print_rtl (dump_file, newcnst);
1228 fprintf (dump_file, "\n");
1230 local_const_prop_count++;
1231 return true;
1233 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
1235 if (dump_file != NULL)
1237 fprintf (dump_file,
1238 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
1239 REGNO (x), INSN_UID (insn));
1240 fprintf (dump_file, " with reg %d\n", REGNO (newreg));
1242 local_copy_prop_count++;
1243 return true;
1246 return false;
1249 /* Do local const/copy propagation (i.e. within each basic block). */
1251 static int
1252 local_cprop_pass (void)
1254 basic_block bb;
1255 rtx_insn *insn;
1256 bool changed = false;
1257 unsigned i;
1259 cselib_init (0);
1260 FOR_EACH_BB_FN (bb, cfun)
1262 FOR_BB_INSNS (bb, insn)
1264 if (INSN_P (insn))
1266 rtx note = find_reg_equal_equiv_note (insn);
1269 reg_use_count = 0;
1270 note_uses (&PATTERN (insn), local_cprop_find_used_regs,
1271 NULL);
1272 if (note)
1273 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
1275 for (i = 0; i < reg_use_count; i++)
1277 if (do_local_cprop (reg_use_table[i], insn))
1279 if (!DEBUG_INSN_P (insn))
1280 changed = true;
1281 break;
1284 if (insn->deleted ())
1285 break;
1287 while (i < reg_use_count);
1289 cselib_process_insn (insn);
1292 /* Forget everything at the end of a basic block. */
1293 cselib_clear_table ();
1296 cselib_finish ();
1298 return changed;
1301 /* Similar to get_condition, only the resulting condition must be
1302 valid at JUMP, instead of at EARLIEST.
1304 This differs from noce_get_condition in ifcvt.c in that we prefer not to
1305 settle for the condition variable in the jump instruction being integral.
1306 We prefer to be able to record the value of a user variable, rather than
1307 the value of a temporary used in a condition. This could be solved by
1308 recording the value of *every* register scanned by canonicalize_condition,
1309 but this would require some code reorganization. */
1312 fis_get_condition (rtx_insn *jump)
1314 return get_condition (jump, NULL, false, true);
1317 /* Check the comparison COND to see if we can safely form an implicit
1318 set from it. */
1320 static bool
1321 implicit_set_cond_p (const_rtx cond)
1323 machine_mode mode;
1324 rtx cst;
1326 /* COND must be either an EQ or NE comparison. */
1327 if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
1328 return false;
1330 /* The first operand of COND must be a register we can propagate. */
1331 if (!cprop_reg_p (XEXP (cond, 0)))
1332 return false;
1334 /* The second operand of COND must be a suitable constant. */
1335 mode = GET_MODE (XEXP (cond, 0));
1336 cst = XEXP (cond, 1);
1338 /* We can't perform this optimization if either operand might be or might
1339 contain a signed zero. */
1340 if (HONOR_SIGNED_ZEROS (mode))
1342 /* It is sufficient to check if CST is or contains a zero. We must
1343 handle float, complex, and vector. If any subpart is a zero, then
1344 the optimization can't be performed. */
1345 /* ??? The complex and vector checks are not implemented yet. We just
1346 always return zero for them. */
1347 if (CONST_DOUBLE_AS_FLOAT_P (cst))
1349 REAL_VALUE_TYPE d;
1350 REAL_VALUE_FROM_CONST_DOUBLE (d, cst);
1351 if (REAL_VALUES_EQUAL (d, dconst0))
1352 return 0;
1354 else
1355 return 0;
1358 return cprop_constant_p (cst);
1361 /* Find the implicit sets of a function. An "implicit set" is a constraint
1362 on the value of a variable, implied by a conditional jump. For example,
1363 following "if (x == 2)", the then branch may be optimized as though the
1364 conditional performed an "explicit set", in this example, "x = 2". This
1365 function records the set patterns that are implicit at the start of each
1366 basic block.
1368 If an implicit set is found but the set is implicit on a critical edge,
1369 this critical edge is split.
1371 Return true if the CFG was modified, false otherwise. */
1373 static bool
1374 find_implicit_sets (void)
1376 basic_block bb, dest;
1377 rtx cond, new_rtx;
1378 unsigned int count = 0;
1379 bool edges_split = false;
1380 size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10;
1382 implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1384 FOR_EACH_BB_FN (bb, cfun)
1386 /* Check for more than one successor. */
1387 if (EDGE_COUNT (bb->succs) <= 1)
1388 continue;
1390 cond = fis_get_condition (BB_END (bb));
1392 /* If no condition is found or if it isn't of a suitable form,
1393 ignore it. */
1394 if (! cond || ! implicit_set_cond_p (cond))
1395 continue;
1397 dest = GET_CODE (cond) == EQ
1398 ? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
1400 /* If DEST doesn't go anywhere, ignore it. */
1401 if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1402 continue;
1404 /* We have found a suitable implicit set. Try to record it now as
1405 a SET in DEST. If DEST has more than one predecessor, the edge
1406 between BB and DEST is a critical edge and we must split it,
1407 because we can only record one implicit set per DEST basic block. */
1408 if (! single_pred_p (dest))
1410 dest = split_edge (find_edge (bb, dest));
1411 edges_split = true;
1414 if (implicit_sets_size <= (size_t) dest->index)
1416 size_t old_implicit_sets_size = implicit_sets_size;
1417 implicit_sets_size *= 2;
1418 implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
1419 memset (implicit_sets + old_implicit_sets_size, 0,
1420 (implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1423 new_rtx = gen_rtx_SET (XEXP (cond, 0), XEXP (cond, 1));
1424 implicit_sets[dest->index] = new_rtx;
1425 if (dump_file)
1427 fprintf (dump_file, "Implicit set of reg %d in ",
1428 REGNO (XEXP (cond, 0)));
1429 fprintf (dump_file, "basic block %d\n", dest->index);
1431 count++;
1434 if (dump_file)
1435 fprintf (dump_file, "Found %d implicit sets\n", count);
1437 /* Confess our sins. */
1438 return edges_split;
1441 /* Bypass conditional jumps. */
1443 /* The value of last_basic_block at the beginning of the jump_bypass
1444 pass. The use of redirect_edge_and_branch_force may introduce new
1445 basic blocks, but the data flow analysis is only valid for basic
1446 block indices less than bypass_last_basic_block. */
1448 static int bypass_last_basic_block;
1450 /* Find a set of REGNO to a constant that is available at the end of basic
1451 block BB. Return NULL if no such set is found. Based heavily upon
1452 find_avail_set. */
1454 static struct cprop_expr *
1455 find_bypass_set (int regno, int bb)
1457 struct cprop_expr *result = 0;
1459 for (;;)
1461 rtx src;
1462 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
1464 while (set)
1466 if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
1467 break;
1468 set = next_set (regno, set);
1471 if (set == 0)
1472 break;
1474 src = set->src;
1475 if (cprop_constant_p (src))
1476 result = set;
1478 if (! REG_P (src))
1479 break;
1481 regno = REGNO (src);
1483 return result;
1486 /* Subroutine of bypass_block that checks whether a pseudo is killed by
1487 any of the instructions inserted on an edge. Jump bypassing places
1488 condition code setters on CFG edges using insert_insn_on_edge. This
1489 function is required to check that our data flow analysis is still
1490 valid prior to commit_edge_insertions. */
1492 static bool
1493 reg_killed_on_edge (const_rtx reg, const_edge e)
1495 rtx_insn *insn;
1497 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
1498 if (INSN_P (insn) && reg_set_p (reg, insn))
1499 return true;
1501 return false;
1504 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
1505 basic block BB which has more than one predecessor. If not NULL, SETCC
1506 is the first instruction of BB, which is immediately followed by JUMP_INSN
1507 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
1508 Returns nonzero if a change was made.
1510 During the jump bypassing pass, we may place copies of SETCC instructions
1511 on CFG edges. The following routine must be careful to pay attention to
1512 these inserted insns when performing its transformations. */
1514 static int
1515 bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump)
1517 rtx_insn *insn;
1518 rtx note;
1519 edge e, edest;
1520 int change;
1521 int may_be_loop_header = false;
1522 unsigned removed_p;
1523 unsigned i;
1524 edge_iterator ei;
1526 insn = (setcc != NULL) ? setcc : jump;
1528 /* Determine set of register uses in INSN. */
1529 reg_use_count = 0;
1530 note_uses (&PATTERN (insn), find_used_regs, NULL);
1531 note = find_reg_equal_equiv_note (insn);
1532 if (note)
1533 find_used_regs (&XEXP (note, 0), NULL);
1535 if (current_loops)
1537 /* If we are to preserve loop structure then do not bypass
1538 a loop header. This will either rotate the loop, create
1539 multiple entry loops or even irreducible regions. */
1540 if (bb == bb->loop_father->header)
1541 return 0;
1543 else
1545 FOR_EACH_EDGE (e, ei, bb->preds)
1546 if (e->flags & EDGE_DFS_BACK)
1548 may_be_loop_header = true;
1549 break;
1553 change = 0;
1554 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1556 removed_p = 0;
1558 if (e->flags & EDGE_COMPLEX)
1560 ei_next (&ei);
1561 continue;
1564 /* We can't redirect edges from new basic blocks. */
1565 if (e->src->index >= bypass_last_basic_block)
1567 ei_next (&ei);
1568 continue;
1571 /* The irreducible loops created by redirecting of edges entering the
1572 loop from outside would decrease effectiveness of some of the
1573 following optimizations, so prevent this. */
1574 if (may_be_loop_header
1575 && !(e->flags & EDGE_DFS_BACK))
1577 ei_next (&ei);
1578 continue;
1581 for (i = 0; i < reg_use_count; i++)
1583 rtx reg_used = reg_use_table[i];
1584 unsigned int regno = REGNO (reg_used);
1585 basic_block dest, old_dest;
1586 struct cprop_expr *set;
1587 rtx src, new_rtx;
1589 set = find_bypass_set (regno, e->src->index);
1591 if (! set)
1592 continue;
1594 /* Check the data flow is valid after edge insertions. */
1595 if (e->insns.r && reg_killed_on_edge (reg_used, e))
1596 continue;
1598 src = SET_SRC (pc_set (jump));
1600 if (setcc != NULL)
1601 src = simplify_replace_rtx (src,
1602 SET_DEST (PATTERN (setcc)),
1603 SET_SRC (PATTERN (setcc)));
1605 new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1607 /* Jump bypassing may have already placed instructions on
1608 edges of the CFG. We can't bypass an outgoing edge that
1609 has instructions associated with it, as these insns won't
1610 get executed if the incoming edge is redirected. */
1611 if (new_rtx == pc_rtx)
1613 edest = FALLTHRU_EDGE (bb);
1614 dest = edest->insns.r ? NULL : edest->dest;
1616 else if (GET_CODE (new_rtx) == LABEL_REF)
1618 dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
1619 /* Don't bypass edges containing instructions. */
1620 edest = find_edge (bb, dest);
1621 if (edest && edest->insns.r)
1622 dest = NULL;
1624 else
1625 dest = NULL;
1627 /* Avoid unification of the edge with other edges from original
1628 branch. We would end up emitting the instruction on "both"
1629 edges. */
1630 if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
1631 && find_edge (e->src, dest))
1632 dest = NULL;
1634 old_dest = e->dest;
1635 if (dest != NULL
1636 && dest != old_dest
1637 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1639 redirect_edge_and_branch_force (e, dest);
1641 /* Copy the register setter to the redirected edge.
1642 Don't copy CC0 setters, as CC0 is dead after jump. */
1643 if (setcc)
1645 rtx pat = PATTERN (setcc);
1646 if (!CC0_P (SET_DEST (pat)))
1647 insert_insn_on_edge (copy_insn (pat), e);
1650 if (dump_file != NULL)
1652 fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
1653 "in jump_insn %d equals constant ",
1654 regno, INSN_UID (jump));
1655 print_rtl (dump_file, set->src);
1656 fprintf (dump_file, "\n\t when BB %d is entered from "
1657 "BB %d. Redirect edge %d->%d to %d.\n",
1658 old_dest->index, e->src->index, e->src->index,
1659 old_dest->index, dest->index);
1661 change = 1;
1662 removed_p = 1;
1663 break;
1666 if (!removed_p)
1667 ei_next (&ei);
1669 return change;
1672 /* Find basic blocks with more than one predecessor that only contain a
1673 single conditional jump. If the result of the comparison is known at
1674 compile-time from any incoming edge, redirect that edge to the
1675 appropriate target. Return nonzero if a change was made.
1677 This function is now mis-named, because we also handle indirect jumps. */
1679 static int
1680 bypass_conditional_jumps (void)
1682 basic_block bb;
1683 int changed;
1684 rtx_insn *setcc;
1685 rtx_insn *insn;
1686 rtx dest;
1688 /* Note we start at block 1. */
1689 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1690 return 0;
1692 bypass_last_basic_block = last_basic_block_for_fn (cfun);
1693 mark_dfs_back_edges ();
1695 changed = 0;
1696 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1697 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1699 /* Check for more than one predecessor. */
1700 if (!single_pred_p (bb))
1702 setcc = NULL;
1703 FOR_BB_INSNS (bb, insn)
1704 if (DEBUG_INSN_P (insn))
1705 continue;
1706 else if (NONJUMP_INSN_P (insn))
1708 if (setcc)
1709 break;
1710 if (GET_CODE (PATTERN (insn)) != SET)
1711 break;
1713 dest = SET_DEST (PATTERN (insn));
1714 if (REG_P (dest) || CC0_P (dest))
1715 setcc = insn;
1716 else
1717 break;
1719 else if (JUMP_P (insn))
1721 if ((any_condjump_p (insn) || computed_jump_p (insn))
1722 && onlyjump_p (insn))
1723 changed |= bypass_block (bb, setcc, insn);
1724 break;
1726 else if (INSN_P (insn))
1727 break;
1731 /* If we bypassed any register setting insns, we inserted a
1732 copy on the redirected edge. These need to be committed. */
1733 if (changed)
1734 commit_edge_insertions ();
1736 return changed;
1739 /* Return true if the graph is too expensive to optimize. PASS is the
1740 optimization about to be performed. */
1742 static bool
1743 is_too_expensive (const char *pass)
1745 /* Trying to perform global optimizations on flow graphs which have
1746 a high connectivity will take a long time and is unlikely to be
1747 particularly useful.
1749 In normal circumstances a cfg should have about twice as many
1750 edges as blocks. But we do not want to punish small functions
1751 which have a couple switch statements. Rather than simply
1752 threshold the number of blocks, uses something with a more
1753 graceful degradation. */
1754 if (n_edges_for_fn (cfun) > 20000 + n_basic_blocks_for_fn (cfun) * 4)
1756 warning (OPT_Wdisabled_optimization,
1757 "%s: %d basic blocks and %d edges/basic block",
1758 pass, n_basic_blocks_for_fn (cfun),
1759 n_edges_for_fn (cfun) / n_basic_blocks_for_fn (cfun));
1761 return true;
1764 /* If allocating memory for the cprop bitmap would take up too much
1765 storage it's better just to disable the optimization. */
1766 if ((n_basic_blocks_for_fn (cfun)
1767 * SBITMAP_SET_SIZE (max_reg_num ())
1768 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
1770 warning (OPT_Wdisabled_optimization,
1771 "%s: %d basic blocks and %d registers",
1772 pass, n_basic_blocks_for_fn (cfun), max_reg_num ());
1774 return true;
1777 return false;
1780 /* Main function for the CPROP pass. */
1782 static int
1783 one_cprop_pass (void)
1785 int i;
1786 int changed = 0;
1788 /* Return if there's nothing to do, or it is too expensive. */
1789 if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
1790 || is_too_expensive (_ ("const/copy propagation disabled")))
1791 return 0;
1793 global_const_prop_count = local_const_prop_count = 0;
1794 global_copy_prop_count = local_copy_prop_count = 0;
1796 bytes_used = 0;
1797 gcc_obstack_init (&cprop_obstack);
1799 /* Do a local const/copy propagation pass first. The global pass
1800 only handles global opportunities.
1801 If the local pass changes something, remove any unreachable blocks
1802 because the CPROP global dataflow analysis may get into infinite
1803 loops for CFGs with unreachable blocks.
1805 FIXME: This local pass should not be necessary after CSE (but for
1806 some reason it still is). It is also (proven) not necessary
1807 to run the local pass right after FWPWOP.
1809 FIXME: The global analysis would not get into infinite loops if it
1810 would use the DF solver (via df_simple_dataflow) instead of
1811 the solver implemented in this file. */
1812 changed |= local_cprop_pass ();
1813 if (changed)
1814 delete_unreachable_blocks ();
1816 /* Determine implicit sets. This may change the CFG (split critical
1817 edges if that exposes an implicit set).
1818 Note that find_implicit_sets() does not rely on up-to-date DF caches
1819 so that we do not have to re-run df_analyze() even if local CPROP
1820 changed something.
1821 ??? This could run earlier so that any uncovered implicit sets
1822 sets could be exploited in local_cprop_pass() also. Later. */
1823 changed |= find_implicit_sets ();
1825 /* If local_cprop_pass() or find_implicit_sets() changed something,
1826 run df_analyze() to bring all insn caches up-to-date, and to take
1827 new basic blocks from edge splitting on the DF radar.
1828 NB: This also runs the fast DCE pass, because execute_rtl_cprop
1829 sets DF_LR_RUN_DCE. */
1830 if (changed)
1831 df_analyze ();
1833 /* Initialize implicit_set_indexes array. */
1834 implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun));
1835 for (i = 0; i < last_basic_block_for_fn (cfun); i++)
1836 implicit_set_indexes[i] = -1;
1838 alloc_hash_table (&set_hash_table);
1839 compute_hash_table (&set_hash_table);
1841 /* Free implicit_sets before peak usage. */
1842 free (implicit_sets);
1843 implicit_sets = NULL;
1845 if (dump_file)
1846 dump_hash_table (dump_file, "SET", &set_hash_table);
1847 if (set_hash_table.n_elems > 0)
1849 basic_block bb;
1850 rtx_insn *insn;
1852 alloc_cprop_mem (last_basic_block_for_fn (cfun),
1853 set_hash_table.n_elems);
1854 compute_cprop_data ();
1856 free (implicit_set_indexes);
1857 implicit_set_indexes = NULL;
1859 /* Allocate vars to track sets of regs. */
1860 reg_set_bitmap = ALLOC_REG_SET (NULL);
1862 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1863 EXIT_BLOCK_PTR_FOR_FN (cfun),
1864 next_bb)
1866 /* Reset tables used to keep track of what's still valid [since
1867 the start of the block]. */
1868 reset_opr_set_tables ();
1870 FOR_BB_INSNS (bb, insn)
1871 if (INSN_P (insn))
1873 changed |= cprop_insn (insn);
1875 /* Keep track of everything modified by this insn. */
1876 /* ??? Need to be careful w.r.t. mods done to INSN.
1877 Don't call mark_oprs_set if we turned the
1878 insn into a NOTE, or deleted the insn. */
1879 if (! NOTE_P (insn) && ! insn->deleted ())
1880 mark_oprs_set (insn);
1884 changed |= bypass_conditional_jumps ();
1886 FREE_REG_SET (reg_set_bitmap);
1887 free_cprop_mem ();
1889 else
1891 free (implicit_set_indexes);
1892 implicit_set_indexes = NULL;
1895 free_hash_table (&set_hash_table);
1896 obstack_free (&cprop_obstack, NULL);
1898 if (dump_file)
1900 fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1901 current_function_name (), n_basic_blocks_for_fn (cfun),
1902 bytes_used);
1903 fprintf (dump_file, "%d local const props, %d local copy props, ",
1904 local_const_prop_count, local_copy_prop_count);
1905 fprintf (dump_file, "%d global const props, %d global copy props\n\n",
1906 global_const_prop_count, global_copy_prop_count);
1909 return changed;
1912 /* All the passes implemented in this file. Each pass has its
1913 own gate and execute function, and at the end of the file a
1914 pass definition for passes.c.
1916 We do not construct an accurate cfg in functions which call
1917 setjmp, so none of these passes runs if the function calls
1918 setjmp.
1919 FIXME: Should just handle setjmp via REG_SETJMP notes. */
1921 static unsigned int
1922 execute_rtl_cprop (void)
1924 int changed;
1925 delete_unreachable_blocks ();
1926 df_set_flags (DF_LR_RUN_DCE);
1927 df_analyze ();
1928 changed = one_cprop_pass ();
1929 flag_rerun_cse_after_global_opts |= changed;
1930 if (changed)
1931 cleanup_cfg (CLEANUP_CFG_CHANGED);
1932 return 0;
1935 namespace {
1937 const pass_data pass_data_rtl_cprop =
1939 RTL_PASS, /* type */
1940 "cprop", /* name */
1941 OPTGROUP_NONE, /* optinfo_flags */
1942 TV_CPROP, /* tv_id */
1943 PROP_cfglayout, /* properties_required */
1944 0, /* properties_provided */
1945 0, /* properties_destroyed */
1946 0, /* todo_flags_start */
1947 TODO_df_finish, /* todo_flags_finish */
1950 class pass_rtl_cprop : public rtl_opt_pass
1952 public:
1953 pass_rtl_cprop (gcc::context *ctxt)
1954 : rtl_opt_pass (pass_data_rtl_cprop, ctxt)
1957 /* opt_pass methods: */
1958 opt_pass * clone () { return new pass_rtl_cprop (m_ctxt); }
1959 virtual bool gate (function *fun)
1961 return optimize > 0 && flag_gcse
1962 && !fun->calls_setjmp
1963 && dbg_cnt (cprop);
1966 virtual unsigned int execute (function *) { return execute_rtl_cprop (); }
1968 }; // class pass_rtl_cprop
1970 } // anon namespace
1972 rtl_opt_pass *
1973 make_pass_rtl_cprop (gcc::context *ctxt)
1975 return new pass_rtl_cprop (ctxt);