PR middle-end/66633
[official-gcc.git] / gcc / cprop.c
blob259686892859ef8c52b34a481738767df21a0d35
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 "alias.h"
28 #include "symtab.h"
29 #include "tree.h"
30 #include "tm_p.h"
31 #include "regs.h"
32 #include "hard-reg-set.h"
33 #include "flags.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "predict.h"
37 #include "function.h"
38 #include "dominance.h"
39 #include "cfg.h"
40 #include "cfgrtl.h"
41 #include "cfganal.h"
42 #include "lcm.h"
43 #include "cfgcleanup.h"
44 #include "basic-block.h"
45 #include "expmed.h"
46 #include "dojump.h"
47 #include "explow.h"
48 #include "calls.h"
49 #include "emit-rtl.h"
50 #include "varasm.h"
51 #include "stmt.h"
52 #include "expr.h"
53 #include "except.h"
54 #include "params.h"
55 #include "alloc-pool.h"
56 #include "cselib.h"
57 #include "intl.h"
58 #include "obstack.h"
59 #include "tree-pass.h"
60 #include "df.h"
61 #include "dbgcnt.h"
62 #include "target.h"
63 #include "cfgloop.h"
66 /* An obstack for our working variables. */
67 static struct obstack cprop_obstack;
69 /* Occurrence of an expression.
70 There is one per basic block. If a pattern appears more than once the
71 last appearance is used. */
73 struct cprop_occr
75 /* Next occurrence of this expression. */
76 struct cprop_occr *next;
77 /* The insn that computes the expression. */
78 rtx_insn *insn;
81 typedef struct cprop_occr *occr_t;
83 /* Hash table entry for assignment expressions. */
85 struct cprop_expr
87 /* The expression (DEST := SRC). */
88 rtx dest;
89 rtx src;
91 /* Index in the available expression bitmaps. */
92 int bitmap_index;
93 /* Next entry with the same hash. */
94 struct cprop_expr *next_same_hash;
95 /* List of available occurrence in basic blocks in the function.
96 An "available occurrence" is one that is the last occurrence in the
97 basic block and whose operands are not modified by following statements
98 in the basic block [including this insn]. */
99 struct cprop_occr *avail_occr;
102 /* Hash table for copy propagation expressions.
103 Each hash table is an array of buckets.
104 ??? It is known that if it were an array of entries, structure elements
105 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
106 not clear whether in the final analysis a sufficient amount of memory would
107 be saved as the size of the available expression bitmaps would be larger
108 [one could build a mapping table without holes afterwards though].
109 Someday I'll perform the computation and figure it out. */
111 struct hash_table_d
113 /* The table itself.
114 This is an array of `set_hash_table_size' elements. */
115 struct cprop_expr **table;
117 /* Size of the hash table, in elements. */
118 unsigned int size;
120 /* Number of hash table elements. */
121 unsigned int n_elems;
124 /* Copy propagation hash table. */
125 static struct hash_table_d set_hash_table;
127 /* Array of implicit set patterns indexed by basic block index. */
128 static rtx *implicit_sets;
130 /* Array of indexes of expressions for implicit set patterns indexed by basic
131 block index. In other words, implicit_set_indexes[i] is the bitmap_index
132 of the expression whose RTX is implicit_sets[i]. */
133 static int *implicit_set_indexes;
135 /* Bitmap containing one bit for each register in the program.
136 Used when performing GCSE to track which registers have been set since
137 the start or end of the basic block while traversing that block. */
138 static regset reg_set_bitmap;
140 /* Various variables for statistics gathering. */
142 /* Memory used in a pass.
143 This isn't intended to be absolutely precise. Its intent is only
144 to keep an eye on memory usage. */
145 static int bytes_used;
147 /* Number of local constants propagated. */
148 static int local_const_prop_count;
149 /* Number of local copies propagated. */
150 static int local_copy_prop_count;
151 /* Number of global constants propagated. */
152 static int global_const_prop_count;
153 /* Number of global copies propagated. */
154 static int global_copy_prop_count;
156 #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T)))
157 #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S)))
159 /* Cover function to obstack_alloc. */
161 static void *
162 cprop_alloc (unsigned long size)
164 bytes_used += size;
165 return obstack_alloc (&cprop_obstack, size);
168 /* Return nonzero if register X is unchanged from INSN to the end
169 of INSN's basic block. */
171 static int
172 reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
174 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
177 /* Hash a set of register REGNO.
179 Sets are hashed on the register that is set. This simplifies the PRE copy
180 propagation code.
182 ??? May need to make things more elaborate. Later, as necessary. */
184 static unsigned int
185 hash_mod (int regno, int hash_table_size)
187 return (unsigned) regno % hash_table_size;
190 /* Insert assignment DEST:=SET from INSN in the hash table.
191 DEST is a register and SET is a register or a suitable constant.
192 If the assignment is already present in the table, record it as
193 the last occurrence in INSN's basic block.
194 IMPLICIT is true if it's an implicit set, false otherwise. */
196 static void
197 insert_set_in_table (rtx dest, rtx src, rtx_insn *insn,
198 struct hash_table_d *table, bool implicit)
200 bool found = false;
201 unsigned int hash;
202 struct cprop_expr *cur_expr, *last_expr = NULL;
203 struct cprop_occr *cur_occr;
205 hash = hash_mod (REGNO (dest), table->size);
207 for (cur_expr = table->table[hash]; cur_expr;
208 cur_expr = cur_expr->next_same_hash)
210 if (dest == cur_expr->dest
211 && src == cur_expr->src)
213 found = true;
214 break;
216 last_expr = cur_expr;
219 if (! found)
221 cur_expr = GOBNEW (struct cprop_expr);
222 bytes_used += sizeof (struct cprop_expr);
223 if (table->table[hash] == NULL)
224 /* This is the first pattern that hashed to this index. */
225 table->table[hash] = cur_expr;
226 else
227 /* Add EXPR to end of this hash chain. */
228 last_expr->next_same_hash = cur_expr;
230 /* Set the fields of the expr element.
231 We must copy X because it can be modified when copy propagation is
232 performed on its operands. */
233 cur_expr->dest = copy_rtx (dest);
234 cur_expr->src = copy_rtx (src);
235 cur_expr->bitmap_index = table->n_elems++;
236 cur_expr->next_same_hash = NULL;
237 cur_expr->avail_occr = NULL;
240 /* Now record the occurrence. */
241 cur_occr = cur_expr->avail_occr;
243 if (cur_occr
244 && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
246 /* Found another instance of the expression in the same basic block.
247 Prefer this occurrence to the currently recorded one. We want
248 the last one in the block and the block is scanned from start
249 to end. */
250 cur_occr->insn = insn;
252 else
254 /* First occurrence of this expression in this basic block. */
255 cur_occr = GOBNEW (struct cprop_occr);
256 bytes_used += sizeof (struct cprop_occr);
257 cur_occr->insn = insn;
258 cur_occr->next = cur_expr->avail_occr;
259 cur_expr->avail_occr = cur_occr;
262 /* Record bitmap_index of the implicit set in implicit_set_indexes. */
263 if (implicit)
264 implicit_set_indexes[BLOCK_FOR_INSN (insn)->index]
265 = cur_expr->bitmap_index;
268 /* Determine whether the rtx X should be treated as a constant for CPROP.
269 Since X might be inserted more than once we have to take care that it
270 is sharable. */
272 static bool
273 cprop_constant_p (const_rtx x)
275 return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
278 /* Determine whether the rtx X should be treated as a register that can
279 be propagated. Any pseudo-register is fine. */
281 static bool
282 cprop_reg_p (const_rtx x)
284 return REG_P (x) && !HARD_REGISTER_P (x);
287 /* Scan SET present in INSN and add an entry to the hash TABLE.
288 IMPLICIT is true if it's an implicit set, false otherwise. */
290 static void
291 hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table,
292 bool implicit)
294 rtx src = SET_SRC (set);
295 rtx dest = SET_DEST (set);
297 if (cprop_reg_p (dest)
298 && reg_available_p (dest, insn)
299 && can_copy_p (GET_MODE (dest)))
301 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
303 This allows us to do a single CPROP pass and still eliminate
304 redundant constants, addresses or other expressions that are
305 constructed with multiple instructions.
307 However, keep the original SRC if INSN is a simple reg-reg move. In
308 In this case, there will almost always be a REG_EQUAL note on the
309 insn that sets SRC. By recording the REG_EQUAL value here as SRC
310 for INSN, we miss copy propagation opportunities.
312 Note that this does not impede profitable constant propagations. We
313 "look through" reg-reg sets in lookup_set. */
314 rtx note = find_reg_equal_equiv_note (insn);
315 if (note != 0
316 && REG_NOTE_KIND (note) == REG_EQUAL
317 && !REG_P (src)
318 && cprop_constant_p (XEXP (note, 0)))
319 src = XEXP (note, 0), set = gen_rtx_SET (dest, src);
321 /* Record sets for constant/copy propagation. */
322 if ((cprop_reg_p (src)
323 && src != dest
324 && reg_available_p (src, insn))
325 || cprop_constant_p (src))
326 insert_set_in_table (dest, src, insn, table, implicit);
330 /* Process INSN and add hash table entries as appropriate. */
332 static void
333 hash_scan_insn (rtx_insn *insn, struct hash_table_d *table)
335 rtx pat = PATTERN (insn);
336 int i;
338 /* Pick out the sets of INSN and for other forms of instructions record
339 what's been modified. */
341 if (GET_CODE (pat) == SET)
342 hash_scan_set (pat, insn, table, false);
343 else if (GET_CODE (pat) == PARALLEL)
344 for (i = 0; i < XVECLEN (pat, 0); i++)
346 rtx x = XVECEXP (pat, 0, i);
348 if (GET_CODE (x) == SET)
349 hash_scan_set (x, insn, table, false);
353 /* Dump the hash table TABLE to file FILE under the name NAME. */
355 static void
356 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
358 int i;
359 /* Flattened out table, so it's printed in proper order. */
360 struct cprop_expr **flat_table;
361 unsigned int *hash_val;
362 struct cprop_expr *expr;
364 flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems);
365 hash_val = XNEWVEC (unsigned int, table->n_elems);
367 for (i = 0; i < (int) table->size; i++)
368 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
370 flat_table[expr->bitmap_index] = expr;
371 hash_val[expr->bitmap_index] = i;
374 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
375 name, table->size, table->n_elems);
377 for (i = 0; i < (int) table->n_elems; i++)
378 if (flat_table[i] != 0)
380 expr = flat_table[i];
381 fprintf (file, "Index %d (hash value %d)\n ",
382 expr->bitmap_index, hash_val[i]);
383 print_rtl (file, expr->dest);
384 fprintf (file, " := ");
385 print_rtl (file, expr->src);
386 fprintf (file, "\n");
389 fprintf (file, "\n");
391 free (flat_table);
392 free (hash_val);
395 /* Record as unavailable all registers that are DEF operands of INSN. */
397 static void
398 make_set_regs_unavailable (rtx_insn *insn)
400 df_ref def;
402 FOR_EACH_INSN_DEF (def, insn)
403 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
406 /* Top level function to create an assignment hash table.
408 Assignment entries are placed in the hash table if
409 - they are of the form (set (pseudo-reg) src),
410 - src is something we want to perform const/copy propagation on,
411 - none of the operands or target are subsequently modified in the block
413 Currently src must be a pseudo-reg or a const_int.
415 TABLE is the table computed. */
417 static void
418 compute_hash_table_work (struct hash_table_d *table)
420 basic_block bb;
422 /* Allocate vars to track sets of regs. */
423 reg_set_bitmap = ALLOC_REG_SET (NULL);
425 FOR_EACH_BB_FN (bb, cfun)
427 rtx_insn *insn;
429 /* Reset tables used to keep track of what's not yet invalid [since
430 the end of the block]. */
431 CLEAR_REG_SET (reg_set_bitmap);
433 /* Go over all insns from the last to the first. This is convenient
434 for tracking available registers, i.e. not set between INSN and
435 the end of the basic block BB. */
436 FOR_BB_INSNS_REVERSE (bb, insn)
438 /* Only real insns are interesting. */
439 if (!NONDEBUG_INSN_P (insn))
440 continue;
442 /* Record interesting sets from INSN in the hash table. */
443 hash_scan_insn (insn, table);
445 /* Any registers set in INSN will make SETs above it not AVAIL. */
446 make_set_regs_unavailable (insn);
449 /* Insert implicit sets in the hash table, pretending they appear as
450 insns at the head of the basic block. */
451 if (implicit_sets[bb->index] != NULL_RTX)
452 hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
455 FREE_REG_SET (reg_set_bitmap);
458 /* Allocate space for the set/expr hash TABLE.
459 It is used to determine the number of buckets to use. */
461 static void
462 alloc_hash_table (struct hash_table_d *table)
464 int n;
466 n = get_max_insn_count ();
468 table->size = n / 4;
469 if (table->size < 11)
470 table->size = 11;
472 /* Attempt to maintain efficient use of hash table.
473 Making it an odd number is simplest for now.
474 ??? Later take some measurements. */
475 table->size |= 1;
476 n = table->size * sizeof (struct cprop_expr *);
477 table->table = XNEWVAR (struct cprop_expr *, n);
480 /* Free things allocated by alloc_hash_table. */
482 static void
483 free_hash_table (struct hash_table_d *table)
485 free (table->table);
488 /* Compute the hash TABLE for doing copy/const propagation or
489 expression hash table. */
491 static void
492 compute_hash_table (struct hash_table_d *table)
494 /* Initialize count of number of entries in hash table. */
495 table->n_elems = 0;
496 memset (table->table, 0, table->size * sizeof (struct cprop_expr *));
498 compute_hash_table_work (table);
501 /* Expression tracking support. */
503 /* Lookup REGNO in the set TABLE. The result is a pointer to the
504 table entry, or NULL if not found. */
506 static struct cprop_expr *
507 lookup_set (unsigned int regno, struct hash_table_d *table)
509 unsigned int hash = hash_mod (regno, table->size);
510 struct cprop_expr *expr;
512 expr = table->table[hash];
514 while (expr && REGNO (expr->dest) != regno)
515 expr = expr->next_same_hash;
517 return expr;
520 /* Return the next entry for REGNO in list EXPR. */
522 static struct cprop_expr *
523 next_set (unsigned int regno, struct cprop_expr *expr)
526 expr = expr->next_same_hash;
527 while (expr && REGNO (expr->dest) != regno);
529 return expr;
532 /* Reset tables used to keep track of what's still available [since the
533 start of the block]. */
535 static void
536 reset_opr_set_tables (void)
538 /* Maintain a bitmap of which regs have been set since beginning of
539 the block. */
540 CLEAR_REG_SET (reg_set_bitmap);
543 /* Return nonzero if the register X has not been set yet [since the
544 start of the basic block containing INSN]. */
546 static int
547 reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
549 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
552 /* Record things set by INSN.
553 This data is used by reg_not_set_p. */
555 static void
556 mark_oprs_set (rtx_insn *insn)
558 df_ref def;
560 FOR_EACH_INSN_DEF (def, insn)
561 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
564 /* Compute copy/constant propagation working variables. */
566 /* Local properties of assignments. */
567 static sbitmap *cprop_avloc;
568 static sbitmap *cprop_kill;
570 /* Global properties of assignments (computed from the local properties). */
571 static sbitmap *cprop_avin;
572 static sbitmap *cprop_avout;
574 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
575 basic blocks. N_SETS is the number of sets. */
577 static void
578 alloc_cprop_mem (int n_blocks, int n_sets)
580 cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
581 cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
583 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
584 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
587 /* Free vars used by copy/const propagation. */
589 static void
590 free_cprop_mem (void)
592 sbitmap_vector_free (cprop_avloc);
593 sbitmap_vector_free (cprop_kill);
594 sbitmap_vector_free (cprop_avin);
595 sbitmap_vector_free (cprop_avout);
598 /* Compute the local properties of each recorded expression.
600 Local properties are those that are defined by the block, irrespective of
601 other blocks.
603 An expression is killed in a block if its operands, either DEST or SRC, are
604 modified in the block.
606 An expression is computed (locally available) in a block if it is computed
607 at least once and expression would contain the same value if the
608 computation was moved to the end of the block.
610 KILL and COMP are destination sbitmaps for recording local properties. */
612 static void
613 compute_local_properties (sbitmap *kill, sbitmap *comp,
614 struct hash_table_d *table)
616 unsigned int i;
618 /* Initialize the bitmaps that were passed in. */
619 bitmap_vector_clear (kill, last_basic_block_for_fn (cfun));
620 bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
622 for (i = 0; i < table->size; i++)
624 struct cprop_expr *expr;
626 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
628 int indx = expr->bitmap_index;
629 df_ref def;
630 struct cprop_occr *occr;
632 /* For each definition of the destination pseudo-reg, the expression
633 is killed in the block where the definition is. */
634 for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
635 def; def = DF_REF_NEXT_REG (def))
636 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
638 /* If the source is a pseudo-reg, for each definition of the source,
639 the expression is killed in the block where the definition is. */
640 if (REG_P (expr->src))
641 for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
642 def; def = DF_REF_NEXT_REG (def))
643 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
645 /* The occurrences recorded in avail_occr are exactly those that
646 are locally available in the block where they are. */
647 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
649 bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
655 /* Hash table support. */
657 /* Top level routine to do the dataflow analysis needed by copy/const
658 propagation. */
660 static void
661 compute_cprop_data (void)
663 basic_block bb;
665 compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
666 compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
668 /* Merge implicit sets into CPROP_AVIN. They are always available at the
669 entry of their basic block. We need to do this because 1) implicit sets
670 aren't recorded for the local pass so they cannot be propagated within
671 their basic block by this pass and 2) the global pass would otherwise
672 propagate them only in the successors of their basic block. */
673 FOR_EACH_BB_FN (bb, cfun)
675 int index = implicit_set_indexes[bb->index];
676 if (index != -1)
677 bitmap_set_bit (cprop_avin[bb->index], index);
681 /* Copy/constant propagation. */
683 /* Maximum number of register uses in an insn that we handle. */
684 #define MAX_USES 8
686 /* Table of uses (registers, both hard and pseudo) found in an insn.
687 Allocated statically to avoid alloc/free complexity and overhead. */
688 static rtx reg_use_table[MAX_USES];
690 /* Index into `reg_use_table' while building it. */
691 static unsigned reg_use_count;
693 /* Set up a list of register numbers used in INSN. The found uses are stored
694 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
695 and contains the number of uses in the table upon exit.
697 ??? If a register appears multiple times we will record it multiple times.
698 This doesn't hurt anything but it will slow things down. */
700 static void
701 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
703 int i, j;
704 enum rtx_code code;
705 const char *fmt;
706 rtx x = *xptr;
708 /* repeat is used to turn tail-recursion into iteration since GCC
709 can't do it when there's no return value. */
710 repeat:
711 if (x == 0)
712 return;
714 code = GET_CODE (x);
715 if (REG_P (x))
717 if (reg_use_count == MAX_USES)
718 return;
720 reg_use_table[reg_use_count] = x;
721 reg_use_count++;
724 /* Recursively scan the operands of this expression. */
726 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
728 if (fmt[i] == 'e')
730 /* If we are about to do the last recursive call
731 needed at this level, change it into iteration.
732 This function is called enough to be worth it. */
733 if (i == 0)
735 x = XEXP (x, 0);
736 goto repeat;
739 find_used_regs (&XEXP (x, i), data);
741 else if (fmt[i] == 'E')
742 for (j = 0; j < XVECLEN (x, i); j++)
743 find_used_regs (&XVECEXP (x, i, j), data);
747 /* Try to replace all uses of FROM in INSN with TO.
748 Return nonzero if successful. */
750 static int
751 try_replace_reg (rtx from, rtx to, rtx_insn *insn)
753 rtx note = find_reg_equal_equiv_note (insn);
754 rtx src = 0;
755 int success = 0;
756 rtx set = single_set (insn);
758 bool check_rtx_costs = true;
759 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
760 int old_cost = set ? set_rtx_cost (set, speed) : 0;
762 if ((note != 0
763 && REG_NOTE_KIND (note) == REG_EQUAL
764 && (GET_CODE (XEXP (note, 0)) == CONST
765 || CONSTANT_P (XEXP (note, 0))))
766 || (set && CONSTANT_P (SET_SRC (set))))
767 check_rtx_costs = false;
769 /* Usually we substitute easy stuff, so we won't copy everything.
770 We however need to take care to not duplicate non-trivial CONST
771 expressions. */
772 to = copy_rtx (to);
774 validate_replace_src_group (from, to, insn);
776 /* If TO is a constant, check the cost of the set after propagation
777 to the cost of the set before the propagation. If the cost is
778 higher, then do not replace FROM with TO. */
780 if (check_rtx_costs
781 && CONSTANT_P (to)
782 && (set_rtx_cost (set, speed) > old_cost))
784 cancel_changes (0);
785 return false;
789 if (num_changes_pending () && apply_change_group ())
790 success = 1;
792 /* Try to simplify SET_SRC if we have substituted a constant. */
793 if (success && set && CONSTANT_P (to))
795 src = simplify_rtx (SET_SRC (set));
797 if (src)
798 validate_change (insn, &SET_SRC (set), src, 0);
801 /* If there is already a REG_EQUAL note, update the expression in it
802 with our replacement. */
803 if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
804 set_unique_reg_note (insn, REG_EQUAL,
805 simplify_replace_rtx (XEXP (note, 0), from, to));
806 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
808 /* If above failed and this is a single set, try to simplify the source
809 of the set given our substitution. We could perhaps try this for
810 multiple SETs, but it probably won't buy us anything. */
811 src = simplify_replace_rtx (SET_SRC (set), from, to);
813 if (!rtx_equal_p (src, SET_SRC (set))
814 && validate_change (insn, &SET_SRC (set), src, 0))
815 success = 1;
817 /* If we've failed perform the replacement, have a single SET to
818 a REG destination and don't yet have a note, add a REG_EQUAL note
819 to not lose information. */
820 if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
821 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
824 if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
826 /* Registers can also appear as uses in SET_DEST if it is a MEM.
827 We could perhaps try this for multiple SETs, but it probably
828 won't buy us anything. */
829 rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
831 if (!rtx_equal_p (dest, SET_DEST (set))
832 && validate_change (insn, &SET_DEST (set), dest, 0))
833 success = 1;
836 /* REG_EQUAL may get simplified into register.
837 We don't allow that. Remove that note. This code ought
838 not to happen, because previous code ought to synthesize
839 reg-reg move, but be on the safe side. */
840 if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
841 remove_note (insn, note);
843 return success;
846 /* Find a set of REGNOs that are available on entry to INSN's block. If found,
847 SET_RET[0] will be assigned a set with a register source and SET_RET[1] a
848 set with a constant source. If not found the corresponding entry is set to
849 NULL. */
851 static void
852 find_avail_set (int regno, rtx_insn *insn, struct cprop_expr *set_ret[2])
854 set_ret[0] = set_ret[1] = NULL;
856 /* Loops are not possible here. To get a loop we would need two sets
857 available at the start of the block containing INSN. i.e. we would
858 need two sets like this available at the start of the block:
860 (set (reg X) (reg Y))
861 (set (reg Y) (reg X))
863 This can not happen since the set of (reg Y) would have killed the
864 set of (reg X) making it unavailable at the start of this block. */
865 while (1)
867 rtx src;
868 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
870 /* Find a set that is available at the start of the block
871 which contains INSN. */
872 while (set)
874 if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
875 set->bitmap_index))
876 break;
877 set = next_set (regno, set);
880 /* If no available set was found we've reached the end of the
881 (possibly empty) copy chain. */
882 if (set == 0)
883 break;
885 src = set->src;
887 /* We know the set is available.
888 Now check that SRC is locally anticipatable (i.e. none of the
889 source operands have changed since the start of the block).
891 If the source operand changed, we may still use it for the next
892 iteration of this loop, but we may not use it for substitutions. */
894 if (cprop_constant_p (src))
895 set_ret[1] = set;
896 else if (reg_not_set_p (src, insn))
897 set_ret[0] = set;
899 /* If the source of the set is anything except a register, then
900 we have reached the end of the copy chain. */
901 if (! REG_P (src))
902 break;
904 /* Follow the copy chain, i.e. start another iteration of the loop
905 and see if we have an available copy into SRC. */
906 regno = REGNO (src);
910 /* Subroutine of cprop_insn that tries to propagate constants into
911 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
912 it is the instruction that immediately precedes JUMP, and must be a
913 single SET of a register. FROM is what we will try to replace,
914 SRC is the constant we will try to substitute for it. Return nonzero
915 if a change was made. */
917 static int
918 cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src)
920 rtx new_rtx, set_src, note_src;
921 rtx set = pc_set (jump);
922 rtx note = find_reg_equal_equiv_note (jump);
924 if (note)
926 note_src = XEXP (note, 0);
927 if (GET_CODE (note_src) == EXPR_LIST)
928 note_src = NULL_RTX;
930 else note_src = NULL_RTX;
932 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
933 set_src = note_src ? note_src : SET_SRC (set);
935 /* First substitute the SETCC condition into the JUMP instruction,
936 then substitute that given values into this expanded JUMP. */
937 if (setcc != NULL_RTX
938 && !modified_between_p (from, setcc, jump)
939 && !modified_between_p (src, setcc, jump))
941 rtx setcc_src;
942 rtx setcc_set = single_set (setcc);
943 rtx setcc_note = find_reg_equal_equiv_note (setcc);
944 setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
945 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
946 set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
947 setcc_src);
949 else
950 setcc = NULL;
952 new_rtx = simplify_replace_rtx (set_src, from, src);
954 /* If no simplification can be made, then try the next register. */
955 if (rtx_equal_p (new_rtx, SET_SRC (set)))
956 return 0;
958 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
959 if (new_rtx == pc_rtx)
960 delete_insn (jump);
961 else
963 /* Ensure the value computed inside the jump insn to be equivalent
964 to one computed by setcc. */
965 if (setcc && modified_in_p (new_rtx, setcc))
966 return 0;
967 if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
969 /* When (some) constants are not valid in a comparison, and there
970 are two registers to be replaced by constants before the entire
971 comparison can be folded into a constant, we need to keep
972 intermediate information in REG_EQUAL notes. For targets with
973 separate compare insns, such notes are added by try_replace_reg.
974 When we have a combined compare-and-branch instruction, however,
975 we need to attach a note to the branch itself to make this
976 optimization work. */
978 if (!rtx_equal_p (new_rtx, note_src))
979 set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
980 return 0;
983 /* Remove REG_EQUAL note after simplification. */
984 if (note_src)
985 remove_note (jump, note);
988 /* Delete the cc0 setter. */
989 if (HAVE_cc0 && setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
990 delete_insn (setcc);
992 global_const_prop_count++;
993 if (dump_file != NULL)
995 fprintf (dump_file,
996 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with"
997 "constant ", REGNO (from), INSN_UID (jump));
998 print_rtl (dump_file, src);
999 fprintf (dump_file, "\n");
1001 purge_dead_edges (bb);
1003 /* If a conditional jump has been changed into unconditional jump, remove
1004 the jump and make the edge fallthru - this is always called in
1005 cfglayout mode. */
1006 if (new_rtx != pc_rtx && simplejump_p (jump))
1008 edge e;
1009 edge_iterator ei;
1011 FOR_EACH_EDGE (e, ei, bb->succs)
1012 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1013 && BB_HEAD (e->dest) == JUMP_LABEL (jump))
1015 e->flags |= EDGE_FALLTHRU;
1016 break;
1018 delete_insn (jump);
1021 return 1;
1024 /* Subroutine of cprop_insn that tries to propagate constants. FROM is what
1025 we will try to replace, SRC is the constant we will try to substitute for
1026 it and INSN is the instruction where this will be happening. */
1028 static int
1029 constprop_register (rtx from, rtx src, rtx_insn *insn)
1031 rtx sset;
1033 /* Check for reg or cc0 setting instructions followed by
1034 conditional branch instructions first. */
1035 if ((sset = single_set (insn)) != NULL
1036 && NEXT_INSN (insn)
1037 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
1039 rtx dest = SET_DEST (sset);
1040 if ((REG_P (dest) || CC0_P (dest))
1041 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
1042 from, src))
1043 return 1;
1046 /* Handle normal insns next. */
1047 if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
1048 return 1;
1050 /* Try to propagate a CONST_INT into a conditional jump.
1051 We're pretty specific about what we will handle in this
1052 code, we can extend this as necessary over time.
1054 Right now the insn in question must look like
1055 (set (pc) (if_then_else ...)) */
1056 else if (any_condjump_p (insn) && onlyjump_p (insn))
1057 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
1058 return 0;
1061 /* Perform constant and copy propagation on INSN.
1062 Return nonzero if a change was made. */
1064 static int
1065 cprop_insn (rtx_insn *insn)
1067 unsigned i;
1068 int changed = 0, changed_this_round;
1069 rtx note;
1073 changed_this_round = 0;
1074 reg_use_count = 0;
1075 note_uses (&PATTERN (insn), find_used_regs, NULL);
1077 /* We may win even when propagating constants into notes. */
1078 note = find_reg_equal_equiv_note (insn);
1079 if (note)
1080 find_used_regs (&XEXP (note, 0), NULL);
1082 for (i = 0; i < reg_use_count; i++)
1084 rtx reg_used = reg_use_table[i];
1085 unsigned int regno = REGNO (reg_used);
1086 rtx src_cst = NULL, src_reg = NULL;
1087 struct cprop_expr *set[2];
1089 /* If the register has already been set in this block, there's
1090 nothing we can do. */
1091 if (! reg_not_set_p (reg_used, insn))
1092 continue;
1094 /* Find an assignment that sets reg_used and is available
1095 at the start of the block. */
1096 find_avail_set (regno, insn, set);
1097 if (set[0])
1098 src_reg = set[0]->src;
1099 if (set[1])
1100 src_cst = set[1]->src;
1102 /* Constant propagation. */
1103 if (src_cst && cprop_constant_p (src_cst)
1104 && constprop_register (reg_used, src_cst, insn))
1106 changed_this_round = changed = 1;
1107 global_const_prop_count++;
1108 if (dump_file != NULL)
1110 fprintf (dump_file,
1111 "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
1112 fprintf (dump_file, "insn %d with constant ",
1113 INSN_UID (insn));
1114 print_rtl (dump_file, src_cst);
1115 fprintf (dump_file, "\n");
1117 if (insn->deleted ())
1118 return 1;
1120 /* Copy propagation. */
1121 else if (src_reg && cprop_reg_p (src_reg)
1122 && REGNO (src_reg) != regno
1123 && try_replace_reg (reg_used, src_reg, insn))
1125 changed_this_round = changed = 1;
1126 global_copy_prop_count++;
1127 if (dump_file != NULL)
1129 fprintf (dump_file,
1130 "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1131 regno, INSN_UID (insn));
1132 fprintf (dump_file, " with reg %d\n", REGNO (src_reg));
1135 /* The original insn setting reg_used may or may not now be
1136 deletable. We leave the deletion to DCE. */
1137 /* FIXME: If it turns out that the insn isn't deletable,
1138 then we may have unnecessarily extended register lifetimes
1139 and made things worse. */
1143 /* If try_replace_reg simplified the insn, the regs found by find_used_regs
1144 may not be valid anymore. Start over. */
1145 while (changed_this_round);
1147 if (changed && DEBUG_INSN_P (insn))
1148 return 0;
1150 return changed;
1153 /* Like find_used_regs, but avoid recording uses that appear in
1154 input-output contexts such as zero_extract or pre_dec. This
1155 restricts the cases we consider to those for which local cprop
1156 can legitimately make replacements. */
1158 static void
1159 local_cprop_find_used_regs (rtx *xptr, void *data)
1161 rtx x = *xptr;
1163 if (x == 0)
1164 return;
1166 switch (GET_CODE (x))
1168 case ZERO_EXTRACT:
1169 case SIGN_EXTRACT:
1170 case STRICT_LOW_PART:
1171 return;
1173 case PRE_DEC:
1174 case PRE_INC:
1175 case POST_DEC:
1176 case POST_INC:
1177 case PRE_MODIFY:
1178 case POST_MODIFY:
1179 /* Can only legitimately appear this early in the context of
1180 stack pushes for function arguments, but handle all of the
1181 codes nonetheless. */
1182 return;
1184 case SUBREG:
1185 /* Setting a subreg of a register larger than word_mode leaves
1186 the non-written words unchanged. */
1187 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
1188 return;
1189 break;
1191 default:
1192 break;
1195 find_used_regs (xptr, data);
1198 /* Try to perform local const/copy propagation on X in INSN. */
1200 static bool
1201 do_local_cprop (rtx x, rtx_insn *insn)
1203 rtx newreg = NULL, newcnst = NULL;
1205 /* Rule out USE instructions and ASM statements as we don't want to
1206 change the hard registers mentioned. */
1207 if (REG_P (x)
1208 && (cprop_reg_p (x)
1209 || (GET_CODE (PATTERN (insn)) != USE
1210 && asm_noperands (PATTERN (insn)) < 0)))
1212 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
1213 struct elt_loc_list *l;
1215 if (!val)
1216 return false;
1217 for (l = val->locs; l; l = l->next)
1219 rtx this_rtx = l->loc;
1220 rtx note;
1222 if (cprop_constant_p (this_rtx))
1223 newcnst = this_rtx;
1224 if (cprop_reg_p (this_rtx)
1225 /* Don't copy propagate if it has attached REG_EQUIV note.
1226 At this point this only function parameters should have
1227 REG_EQUIV notes and if the argument slot is used somewhere
1228 explicitly, it means address of parameter has been taken,
1229 so we should not extend the lifetime of the pseudo. */
1230 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
1231 || ! MEM_P (XEXP (note, 0))))
1232 newreg = this_rtx;
1234 if (newcnst && constprop_register (x, newcnst, insn))
1236 if (dump_file != NULL)
1238 fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
1239 REGNO (x));
1240 fprintf (dump_file, "insn %d with constant ",
1241 INSN_UID (insn));
1242 print_rtl (dump_file, newcnst);
1243 fprintf (dump_file, "\n");
1245 local_const_prop_count++;
1246 return true;
1248 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
1250 if (dump_file != NULL)
1252 fprintf (dump_file,
1253 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
1254 REGNO (x), INSN_UID (insn));
1255 fprintf (dump_file, " with reg %d\n", REGNO (newreg));
1257 local_copy_prop_count++;
1258 return true;
1261 return false;
1264 /* Do local const/copy propagation (i.e. within each basic block). */
1266 static int
1267 local_cprop_pass (void)
1269 basic_block bb;
1270 rtx_insn *insn;
1271 bool changed = false;
1272 unsigned i;
1274 cselib_init (0);
1275 FOR_EACH_BB_FN (bb, cfun)
1277 FOR_BB_INSNS (bb, insn)
1279 if (INSN_P (insn))
1281 rtx note = find_reg_equal_equiv_note (insn);
1284 reg_use_count = 0;
1285 note_uses (&PATTERN (insn), local_cprop_find_used_regs,
1286 NULL);
1287 if (note)
1288 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
1290 for (i = 0; i < reg_use_count; i++)
1292 if (do_local_cprop (reg_use_table[i], insn))
1294 if (!DEBUG_INSN_P (insn))
1295 changed = true;
1296 break;
1299 if (insn->deleted ())
1300 break;
1302 while (i < reg_use_count);
1304 cselib_process_insn (insn);
1307 /* Forget everything at the end of a basic block. */
1308 cselib_clear_table ();
1311 cselib_finish ();
1313 return changed;
1316 /* Similar to get_condition, only the resulting condition must be
1317 valid at JUMP, instead of at EARLIEST.
1319 This differs from noce_get_condition in ifcvt.c in that we prefer not to
1320 settle for the condition variable in the jump instruction being integral.
1321 We prefer to be able to record the value of a user variable, rather than
1322 the value of a temporary used in a condition. This could be solved by
1323 recording the value of *every* register scanned by canonicalize_condition,
1324 but this would require some code reorganization. */
1327 fis_get_condition (rtx_insn *jump)
1329 return get_condition (jump, NULL, false, true);
1332 /* Check the comparison COND to see if we can safely form an implicit
1333 set from it. */
1335 static bool
1336 implicit_set_cond_p (const_rtx cond)
1338 machine_mode mode;
1339 rtx cst;
1341 /* COND must be either an EQ or NE comparison. */
1342 if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
1343 return false;
1345 /* The first operand of COND must be a register we can propagate. */
1346 if (!cprop_reg_p (XEXP (cond, 0)))
1347 return false;
1349 /* The second operand of COND must be a suitable constant. */
1350 mode = GET_MODE (XEXP (cond, 0));
1351 cst = XEXP (cond, 1);
1353 /* We can't perform this optimization if either operand might be or might
1354 contain a signed zero. */
1355 if (HONOR_SIGNED_ZEROS (mode))
1357 /* It is sufficient to check if CST is or contains a zero. We must
1358 handle float, complex, and vector. If any subpart is a zero, then
1359 the optimization can't be performed. */
1360 /* ??? The complex and vector checks are not implemented yet. We just
1361 always return zero for them. */
1362 if (CONST_DOUBLE_AS_FLOAT_P (cst))
1364 REAL_VALUE_TYPE d;
1365 REAL_VALUE_FROM_CONST_DOUBLE (d, cst);
1366 if (REAL_VALUES_EQUAL (d, dconst0))
1367 return 0;
1369 else
1370 return 0;
1373 return cprop_constant_p (cst);
1376 /* Find the implicit sets of a function. An "implicit set" is a constraint
1377 on the value of a variable, implied by a conditional jump. For example,
1378 following "if (x == 2)", the then branch may be optimized as though the
1379 conditional performed an "explicit set", in this example, "x = 2". This
1380 function records the set patterns that are implicit at the start of each
1381 basic block.
1383 If an implicit set is found but the set is implicit on a critical edge,
1384 this critical edge is split.
1386 Return true if the CFG was modified, false otherwise. */
1388 static bool
1389 find_implicit_sets (void)
1391 basic_block bb, dest;
1392 rtx cond, new_rtx;
1393 unsigned int count = 0;
1394 bool edges_split = false;
1395 size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10;
1397 implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1399 FOR_EACH_BB_FN (bb, cfun)
1401 /* Check for more than one successor. */
1402 if (EDGE_COUNT (bb->succs) <= 1)
1403 continue;
1405 cond = fis_get_condition (BB_END (bb));
1407 /* If no condition is found or if it isn't of a suitable form,
1408 ignore it. */
1409 if (! cond || ! implicit_set_cond_p (cond))
1410 continue;
1412 dest = GET_CODE (cond) == EQ
1413 ? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
1415 /* If DEST doesn't go anywhere, ignore it. */
1416 if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1417 continue;
1419 /* We have found a suitable implicit set. Try to record it now as
1420 a SET in DEST. If DEST has more than one predecessor, the edge
1421 between BB and DEST is a critical edge and we must split it,
1422 because we can only record one implicit set per DEST basic block. */
1423 if (! single_pred_p (dest))
1425 dest = split_edge (find_edge (bb, dest));
1426 edges_split = true;
1429 if (implicit_sets_size <= (size_t) dest->index)
1431 size_t old_implicit_sets_size = implicit_sets_size;
1432 implicit_sets_size *= 2;
1433 implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
1434 memset (implicit_sets + old_implicit_sets_size, 0,
1435 (implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1438 new_rtx = gen_rtx_SET (XEXP (cond, 0), XEXP (cond, 1));
1439 implicit_sets[dest->index] = new_rtx;
1440 if (dump_file)
1442 fprintf (dump_file, "Implicit set of reg %d in ",
1443 REGNO (XEXP (cond, 0)));
1444 fprintf (dump_file, "basic block %d\n", dest->index);
1446 count++;
1449 if (dump_file)
1450 fprintf (dump_file, "Found %d implicit sets\n", count);
1452 /* Confess our sins. */
1453 return edges_split;
1456 /* Bypass conditional jumps. */
1458 /* The value of last_basic_block at the beginning of the jump_bypass
1459 pass. The use of redirect_edge_and_branch_force may introduce new
1460 basic blocks, but the data flow analysis is only valid for basic
1461 block indices less than bypass_last_basic_block. */
1463 static int bypass_last_basic_block;
1465 /* Find a set of REGNO to a constant that is available at the end of basic
1466 block BB. Return NULL if no such set is found. Based heavily upon
1467 find_avail_set. */
1469 static struct cprop_expr *
1470 find_bypass_set (int regno, int bb)
1472 struct cprop_expr *result = 0;
1474 for (;;)
1476 rtx src;
1477 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
1479 while (set)
1481 if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
1482 break;
1483 set = next_set (regno, set);
1486 if (set == 0)
1487 break;
1489 src = set->src;
1490 if (cprop_constant_p (src))
1491 result = set;
1493 if (! REG_P (src))
1494 break;
1496 regno = REGNO (src);
1498 return result;
1501 /* Subroutine of bypass_block that checks whether a pseudo is killed by
1502 any of the instructions inserted on an edge. Jump bypassing places
1503 condition code setters on CFG edges using insert_insn_on_edge. This
1504 function is required to check that our data flow analysis is still
1505 valid prior to commit_edge_insertions. */
1507 static bool
1508 reg_killed_on_edge (const_rtx reg, const_edge e)
1510 rtx_insn *insn;
1512 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
1513 if (INSN_P (insn) && reg_set_p (reg, insn))
1514 return true;
1516 return false;
1519 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
1520 basic block BB which has more than one predecessor. If not NULL, SETCC
1521 is the first instruction of BB, which is immediately followed by JUMP_INSN
1522 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
1523 Returns nonzero if a change was made.
1525 During the jump bypassing pass, we may place copies of SETCC instructions
1526 on CFG edges. The following routine must be careful to pay attention to
1527 these inserted insns when performing its transformations. */
1529 static int
1530 bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump)
1532 rtx_insn *insn;
1533 rtx note;
1534 edge e, edest;
1535 int change;
1536 int may_be_loop_header = false;
1537 unsigned removed_p;
1538 unsigned i;
1539 edge_iterator ei;
1541 insn = (setcc != NULL) ? setcc : jump;
1543 /* Determine set of register uses in INSN. */
1544 reg_use_count = 0;
1545 note_uses (&PATTERN (insn), find_used_regs, NULL);
1546 note = find_reg_equal_equiv_note (insn);
1547 if (note)
1548 find_used_regs (&XEXP (note, 0), NULL);
1550 if (current_loops)
1552 /* If we are to preserve loop structure then do not bypass
1553 a loop header. This will either rotate the loop, create
1554 multiple entry loops or even irreducible regions. */
1555 if (bb == bb->loop_father->header)
1556 return 0;
1558 else
1560 FOR_EACH_EDGE (e, ei, bb->preds)
1561 if (e->flags & EDGE_DFS_BACK)
1563 may_be_loop_header = true;
1564 break;
1568 change = 0;
1569 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1571 removed_p = 0;
1573 if (e->flags & EDGE_COMPLEX)
1575 ei_next (&ei);
1576 continue;
1579 /* We can't redirect edges from new basic blocks. */
1580 if (e->src->index >= bypass_last_basic_block)
1582 ei_next (&ei);
1583 continue;
1586 /* The irreducible loops created by redirecting of edges entering the
1587 loop from outside would decrease effectiveness of some of the
1588 following optimizations, so prevent this. */
1589 if (may_be_loop_header
1590 && !(e->flags & EDGE_DFS_BACK))
1592 ei_next (&ei);
1593 continue;
1596 for (i = 0; i < reg_use_count; i++)
1598 rtx reg_used = reg_use_table[i];
1599 unsigned int regno = REGNO (reg_used);
1600 basic_block dest, old_dest;
1601 struct cprop_expr *set;
1602 rtx src, new_rtx;
1604 set = find_bypass_set (regno, e->src->index);
1606 if (! set)
1607 continue;
1609 /* Check the data flow is valid after edge insertions. */
1610 if (e->insns.r && reg_killed_on_edge (reg_used, e))
1611 continue;
1613 src = SET_SRC (pc_set (jump));
1615 if (setcc != NULL)
1616 src = simplify_replace_rtx (src,
1617 SET_DEST (PATTERN (setcc)),
1618 SET_SRC (PATTERN (setcc)));
1620 new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1622 /* Jump bypassing may have already placed instructions on
1623 edges of the CFG. We can't bypass an outgoing edge that
1624 has instructions associated with it, as these insns won't
1625 get executed if the incoming edge is redirected. */
1626 if (new_rtx == pc_rtx)
1628 edest = FALLTHRU_EDGE (bb);
1629 dest = edest->insns.r ? NULL : edest->dest;
1631 else if (GET_CODE (new_rtx) == LABEL_REF)
1633 dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
1634 /* Don't bypass edges containing instructions. */
1635 edest = find_edge (bb, dest);
1636 if (edest && edest->insns.r)
1637 dest = NULL;
1639 else
1640 dest = NULL;
1642 /* Avoid unification of the edge with other edges from original
1643 branch. We would end up emitting the instruction on "both"
1644 edges. */
1645 if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
1646 && find_edge (e->src, dest))
1647 dest = NULL;
1649 old_dest = e->dest;
1650 if (dest != NULL
1651 && dest != old_dest
1652 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1654 redirect_edge_and_branch_force (e, dest);
1656 /* Copy the register setter to the redirected edge.
1657 Don't copy CC0 setters, as CC0 is dead after jump. */
1658 if (setcc)
1660 rtx pat = PATTERN (setcc);
1661 if (!CC0_P (SET_DEST (pat)))
1662 insert_insn_on_edge (copy_insn (pat), e);
1665 if (dump_file != NULL)
1667 fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
1668 "in jump_insn %d equals constant ",
1669 regno, INSN_UID (jump));
1670 print_rtl (dump_file, set->src);
1671 fprintf (dump_file, "\n\t when BB %d is entered from "
1672 "BB %d. Redirect edge %d->%d to %d.\n",
1673 old_dest->index, e->src->index, e->src->index,
1674 old_dest->index, dest->index);
1676 change = 1;
1677 removed_p = 1;
1678 break;
1681 if (!removed_p)
1682 ei_next (&ei);
1684 return change;
1687 /* Find basic blocks with more than one predecessor that only contain a
1688 single conditional jump. If the result of the comparison is known at
1689 compile-time from any incoming edge, redirect that edge to the
1690 appropriate target. Return nonzero if a change was made.
1692 This function is now mis-named, because we also handle indirect jumps. */
1694 static int
1695 bypass_conditional_jumps (void)
1697 basic_block bb;
1698 int changed;
1699 rtx_insn *setcc;
1700 rtx_insn *insn;
1701 rtx dest;
1703 /* Note we start at block 1. */
1704 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1705 return 0;
1707 bypass_last_basic_block = last_basic_block_for_fn (cfun);
1708 mark_dfs_back_edges ();
1710 changed = 0;
1711 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1712 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1714 /* Check for more than one predecessor. */
1715 if (!single_pred_p (bb))
1717 setcc = NULL;
1718 FOR_BB_INSNS (bb, insn)
1719 if (DEBUG_INSN_P (insn))
1720 continue;
1721 else if (NONJUMP_INSN_P (insn))
1723 if (setcc)
1724 break;
1725 if (GET_CODE (PATTERN (insn)) != SET)
1726 break;
1728 dest = SET_DEST (PATTERN (insn));
1729 if (REG_P (dest) || CC0_P (dest))
1730 setcc = insn;
1731 else
1732 break;
1734 else if (JUMP_P (insn))
1736 if ((any_condjump_p (insn) || computed_jump_p (insn))
1737 && onlyjump_p (insn))
1738 changed |= bypass_block (bb, setcc, insn);
1739 break;
1741 else if (INSN_P (insn))
1742 break;
1746 /* If we bypassed any register setting insns, we inserted a
1747 copy on the redirected edge. These need to be committed. */
1748 if (changed)
1749 commit_edge_insertions ();
1751 return changed;
1754 /* Return true if the graph is too expensive to optimize. PASS is the
1755 optimization about to be performed. */
1757 static bool
1758 is_too_expensive (const char *pass)
1760 /* Trying to perform global optimizations on flow graphs which have
1761 a high connectivity will take a long time and is unlikely to be
1762 particularly useful.
1764 In normal circumstances a cfg should have about twice as many
1765 edges as blocks. But we do not want to punish small functions
1766 which have a couple switch statements. Rather than simply
1767 threshold the number of blocks, uses something with a more
1768 graceful degradation. */
1769 if (n_edges_for_fn (cfun) > 20000 + n_basic_blocks_for_fn (cfun) * 4)
1771 warning (OPT_Wdisabled_optimization,
1772 "%s: %d basic blocks and %d edges/basic block",
1773 pass, n_basic_blocks_for_fn (cfun),
1774 n_edges_for_fn (cfun) / n_basic_blocks_for_fn (cfun));
1776 return true;
1779 /* If allocating memory for the cprop bitmap would take up too much
1780 storage it's better just to disable the optimization. */
1781 if ((n_basic_blocks_for_fn (cfun)
1782 * SBITMAP_SET_SIZE (max_reg_num ())
1783 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
1785 warning (OPT_Wdisabled_optimization,
1786 "%s: %d basic blocks and %d registers",
1787 pass, n_basic_blocks_for_fn (cfun), max_reg_num ());
1789 return true;
1792 return false;
1795 /* Main function for the CPROP pass. */
1797 static int
1798 one_cprop_pass (void)
1800 int i;
1801 int changed = 0;
1803 /* Return if there's nothing to do, or it is too expensive. */
1804 if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
1805 || is_too_expensive (_ ("const/copy propagation disabled")))
1806 return 0;
1808 global_const_prop_count = local_const_prop_count = 0;
1809 global_copy_prop_count = local_copy_prop_count = 0;
1811 bytes_used = 0;
1812 gcc_obstack_init (&cprop_obstack);
1814 /* Do a local const/copy propagation pass first. The global pass
1815 only handles global opportunities.
1816 If the local pass changes something, remove any unreachable blocks
1817 because the CPROP global dataflow analysis may get into infinite
1818 loops for CFGs with unreachable blocks.
1820 FIXME: This local pass should not be necessary after CSE (but for
1821 some reason it still is). It is also (proven) not necessary
1822 to run the local pass right after FWPWOP.
1824 FIXME: The global analysis would not get into infinite loops if it
1825 would use the DF solver (via df_simple_dataflow) instead of
1826 the solver implemented in this file. */
1827 changed |= local_cprop_pass ();
1828 if (changed)
1829 delete_unreachable_blocks ();
1831 /* Determine implicit sets. This may change the CFG (split critical
1832 edges if that exposes an implicit set).
1833 Note that find_implicit_sets() does not rely on up-to-date DF caches
1834 so that we do not have to re-run df_analyze() even if local CPROP
1835 changed something.
1836 ??? This could run earlier so that any uncovered implicit sets
1837 sets could be exploited in local_cprop_pass() also. Later. */
1838 changed |= find_implicit_sets ();
1840 /* If local_cprop_pass() or find_implicit_sets() changed something,
1841 run df_analyze() to bring all insn caches up-to-date, and to take
1842 new basic blocks from edge splitting on the DF radar.
1843 NB: This also runs the fast DCE pass, because execute_rtl_cprop
1844 sets DF_LR_RUN_DCE. */
1845 if (changed)
1846 df_analyze ();
1848 /* Initialize implicit_set_indexes array. */
1849 implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun));
1850 for (i = 0; i < last_basic_block_for_fn (cfun); i++)
1851 implicit_set_indexes[i] = -1;
1853 alloc_hash_table (&set_hash_table);
1854 compute_hash_table (&set_hash_table);
1856 /* Free implicit_sets before peak usage. */
1857 free (implicit_sets);
1858 implicit_sets = NULL;
1860 if (dump_file)
1861 dump_hash_table (dump_file, "SET", &set_hash_table);
1862 if (set_hash_table.n_elems > 0)
1864 basic_block bb;
1865 rtx_insn *insn;
1867 alloc_cprop_mem (last_basic_block_for_fn (cfun),
1868 set_hash_table.n_elems);
1869 compute_cprop_data ();
1871 free (implicit_set_indexes);
1872 implicit_set_indexes = NULL;
1874 /* Allocate vars to track sets of regs. */
1875 reg_set_bitmap = ALLOC_REG_SET (NULL);
1877 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1878 EXIT_BLOCK_PTR_FOR_FN (cfun),
1879 next_bb)
1881 /* Reset tables used to keep track of what's still valid [since
1882 the start of the block]. */
1883 reset_opr_set_tables ();
1885 FOR_BB_INSNS (bb, insn)
1886 if (INSN_P (insn))
1888 changed |= cprop_insn (insn);
1890 /* Keep track of everything modified by this insn. */
1891 /* ??? Need to be careful w.r.t. mods done to INSN.
1892 Don't call mark_oprs_set if we turned the
1893 insn into a NOTE, or deleted the insn. */
1894 if (! NOTE_P (insn) && ! insn->deleted ())
1895 mark_oprs_set (insn);
1899 changed |= bypass_conditional_jumps ();
1901 FREE_REG_SET (reg_set_bitmap);
1902 free_cprop_mem ();
1904 else
1906 free (implicit_set_indexes);
1907 implicit_set_indexes = NULL;
1910 free_hash_table (&set_hash_table);
1911 obstack_free (&cprop_obstack, NULL);
1913 if (dump_file)
1915 fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1916 current_function_name (), n_basic_blocks_for_fn (cfun),
1917 bytes_used);
1918 fprintf (dump_file, "%d local const props, %d local copy props, ",
1919 local_const_prop_count, local_copy_prop_count);
1920 fprintf (dump_file, "%d global const props, %d global copy props\n\n",
1921 global_const_prop_count, global_copy_prop_count);
1924 return changed;
1927 /* All the passes implemented in this file. Each pass has its
1928 own gate and execute function, and at the end of the file a
1929 pass definition for passes.c.
1931 We do not construct an accurate cfg in functions which call
1932 setjmp, so none of these passes runs if the function calls
1933 setjmp.
1934 FIXME: Should just handle setjmp via REG_SETJMP notes. */
1936 static unsigned int
1937 execute_rtl_cprop (void)
1939 int changed;
1940 delete_unreachable_blocks ();
1941 df_set_flags (DF_LR_RUN_DCE);
1942 df_analyze ();
1943 changed = one_cprop_pass ();
1944 flag_rerun_cse_after_global_opts |= changed;
1945 if (changed)
1946 cleanup_cfg (CLEANUP_CFG_CHANGED);
1947 return 0;
1950 namespace {
1952 const pass_data pass_data_rtl_cprop =
1954 RTL_PASS, /* type */
1955 "cprop", /* name */
1956 OPTGROUP_NONE, /* optinfo_flags */
1957 TV_CPROP, /* tv_id */
1958 PROP_cfglayout, /* properties_required */
1959 0, /* properties_provided */
1960 0, /* properties_destroyed */
1961 0, /* todo_flags_start */
1962 TODO_df_finish, /* todo_flags_finish */
1965 class pass_rtl_cprop : public rtl_opt_pass
1967 public:
1968 pass_rtl_cprop (gcc::context *ctxt)
1969 : rtl_opt_pass (pass_data_rtl_cprop, ctxt)
1972 /* opt_pass methods: */
1973 opt_pass * clone () { return new pass_rtl_cprop (m_ctxt); }
1974 virtual bool gate (function *fun)
1976 return optimize > 0 && flag_gcse
1977 && !fun->calls_setjmp
1978 && dbg_cnt (cprop);
1981 virtual unsigned int execute (function *) { return execute_rtl_cprop (); }
1983 }; // class pass_rtl_cprop
1985 } // anon namespace
1987 rtl_opt_pass *
1988 make_pass_rtl_cprop (gcc::context *ctxt)
1990 return new pass_rtl_cprop (ctxt);