make __stl_prime_list in comdat
[official-gcc.git] / gcc / cprop.c
blobd90f769e546ee7aa378daf176dc4bbd9665e9f1e
1 /* Global constant/copy propagation for RTL.
2 Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "tm.h"
25 #include "diagnostic-core.h"
26 #include "toplev.h"
28 #include "rtl.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 "basic-block.h"
37 #include "output.h"
38 #include "function.h"
39 #include "expr.h"
40 #include "except.h"
41 #include "params.h"
42 #include "cselib.h"
43 #include "intl.h"
44 #include "obstack.h"
45 #include "timevar.h"
46 #include "tree-pass.h"
47 #include "hashtab.h"
48 #include "df.h"
49 #include "dbgcnt.h"
50 #include "target.h"
53 /* An obstack for our working variables. */
54 static struct obstack cprop_obstack;
56 /* Occurrence of an expression.
57 There is one per basic block. If a pattern appears more than once the
58 last appearance is used. */
60 struct occr
62 /* Next occurrence of this expression. */
63 struct occr *next;
64 /* The insn that computes the expression. */
65 rtx insn;
68 typedef struct occr *occr_t;
69 DEF_VEC_P (occr_t);
70 DEF_VEC_ALLOC_P (occr_t, heap);
72 /* Hash table entry for an assignment expressions. */
74 struct expr
76 /* The expression (DEST := SRC). */
77 rtx dest;
78 rtx src;
80 /* Index in the available expression bitmaps. */
81 int bitmap_index;
82 /* Next entry with the same hash. */
83 struct expr *next_same_hash;
84 /* List of available occurrence in basic blocks in the function.
85 An "available occurrence" is one that is the last occurrence in the
86 basic block and the operands are not modified by following statements in
87 the basic block [including this insn]. */
88 struct occr *avail_occr;
91 /* Hash table for copy propagation expressions.
92 Each hash table is an array of buckets.
93 ??? It is known that if it were an array of entries, structure elements
94 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
95 not clear whether in the final analysis a sufficient amount of memory would
96 be saved as the size of the available expression bitmaps would be larger
97 [one could build a mapping table without holes afterwards though].
98 Someday I'll perform the computation and figure it out. */
100 struct hash_table_d
102 /* The table itself.
103 This is an array of `set_hash_table_size' elements. */
104 struct expr **table;
106 /* Size of the hash table, in elements. */
107 unsigned int size;
109 /* Number of hash table elements. */
110 unsigned int n_elems;
113 /* Copy propagation hash table. */
114 static struct hash_table_d set_hash_table;
116 /* Array of implicit set patterns indexed by basic block index. */
117 static rtx *implicit_sets;
119 /* Bitmap containing one bit for each register in the program.
120 Used when performing GCSE to track which registers have been set since
121 the start or end of the basic block while traversing that block. */
122 static regset reg_set_bitmap;
124 /* Various variables for statistics gathering. */
126 /* Memory used in a pass.
127 This isn't intended to be absolutely precise. Its intent is only
128 to keep an eye on memory usage. */
129 static int bytes_used;
131 /* Number of local constants propagated. */
132 static int local_const_prop_count;
133 /* Number of local copies propagated. */
134 static int local_copy_prop_count;
135 /* Number of global constants propagated. */
136 static int global_const_prop_count;
137 /* Number of global copies propagated. */
138 static int global_copy_prop_count;
141 #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T)))
142 #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S)))
144 /* Cover function to obstack_alloc. */
146 static void *
147 cprop_alloc (unsigned long size)
149 bytes_used += size;
150 return obstack_alloc (&cprop_obstack, size);
153 /* Return nonzero if register X is unchanged from INSN to the end
154 of INSN's basic block. */
156 static int
157 reg_available_p (const_rtx x, const_rtx insn ATTRIBUTE_UNUSED)
159 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
162 /* Hash a set of register REGNO.
164 Sets are hashed on the register that is set. This simplifies the PRE copy
165 propagation code.
167 ??? May need to make things more elaborate. Later, as necessary. */
169 static unsigned int
170 hash_set (int regno, int hash_table_size)
172 unsigned int hash;
174 hash = regno;
175 return hash % hash_table_size;
178 /* Insert assignment DEST:=SET from INSN in the hash table.
179 DEST is a register and SET is a register or a suitable constant.
180 If the assignment is already present in the table, record it as
181 the last occurrence in INSN's basic block. */
183 static void
184 insert_set_in_table (rtx dest, rtx src, rtx insn, struct hash_table_d *table)
186 bool found = false;
187 unsigned int hash;
188 struct expr *cur_expr, *last_expr = NULL;
189 struct occr *cur_occr;
191 hash = hash_set (REGNO (dest), table->size);
193 for (cur_expr = table->table[hash]; cur_expr;
194 cur_expr = cur_expr->next_same_hash)
196 if (dest == cur_expr->dest
197 && src == cur_expr->src)
199 found = true;
200 break;
202 last_expr = cur_expr;
205 if (! found)
207 cur_expr = GOBNEW (struct expr);
208 bytes_used += sizeof (struct expr);
209 if (table->table[hash] == NULL)
210 /* This is the first pattern that hashed to this index. */
211 table->table[hash] = cur_expr;
212 else
213 /* Add EXPR to end of this hash chain. */
214 last_expr->next_same_hash = cur_expr;
216 /* Set the fields of the expr element.
217 We must copy X because it can be modified when copy propagation is
218 performed on its operands. */
219 cur_expr->dest = copy_rtx (dest);
220 cur_expr->src = copy_rtx (src);
221 cur_expr->bitmap_index = table->n_elems++;
222 cur_expr->next_same_hash = NULL;
223 cur_expr->avail_occr = NULL;
226 /* Now record the occurrence. */
227 cur_occr = cur_expr->avail_occr;
229 if (cur_occr
230 && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
232 /* Found another instance of the expression in the same basic block.
233 Prefer this occurrence to the currently recorded one. We want
234 the last one in the block and the block is scanned from start
235 to end. */
236 cur_occr->insn = insn;
238 else
240 /* First occurrence of this expression in this basic block. */
241 cur_occr = GOBNEW (struct occr);
242 bytes_used += sizeof (struct occr);
243 cur_occr->insn = insn;
244 cur_occr->next = cur_expr->avail_occr;
245 cur_expr->avail_occr = cur_occr;
249 /* Determine whether the rtx X should be treated as a constant for CPROP.
250 Since X might be inserted more than once we have to take care that it
251 is sharable. */
253 static bool
254 cprop_constant_p (const_rtx x)
256 return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
259 /* Scan pattern PAT of INSN and add an entry to the hash TABLE (set or
260 expression one). */
262 static void
263 hash_scan_set (rtx pat, rtx insn, struct hash_table_d *table)
265 rtx src = SET_SRC (pat);
266 rtx dest = SET_DEST (pat);
268 if (REG_P (dest)
269 && ! HARD_REGISTER_P (dest)
270 && reg_available_p (dest, insn)
271 && can_copy_p (GET_MODE (dest)))
273 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
275 This allows us to do a single CPROP pass and still eliminate
276 redundant constants, addresses or other expressions that are
277 constructed with multiple instructions.
279 However, keep the original SRC if INSN is a simple reg-reg move. In
280 In this case, there will almost always be a REG_EQUAL note on the
281 insn that sets SRC. By recording the REG_EQUAL value here as SRC
282 for INSN, we miss copy propagation opportunities.
284 Note that this does not impede profitable constant propagations. We
285 "look through" reg-reg sets in lookup_set. */
286 rtx note = find_reg_equal_equiv_note (insn);
287 if (note != 0
288 && REG_NOTE_KIND (note) == REG_EQUAL
289 && !REG_P (src)
290 && cprop_constant_p (XEXP (note, 0)))
291 src = XEXP (note, 0), pat = gen_rtx_SET (VOIDmode, dest, src);
293 /* Record sets for constant/copy propagation. */
294 if ((REG_P (src)
295 && src != dest
296 && ! HARD_REGISTER_P (src)
297 && reg_available_p (src, insn))
298 || cprop_constant_p (src))
299 insert_set_in_table (dest, src, insn, table);
303 /* Process INSN and add hash table entries as appropriate.
305 Only available expressions that set a single pseudo-reg are recorded.
307 Single sets in a PARALLEL could be handled, but it's an extra complication
308 that isn't dealt with right now. The trick is handling the CLOBBERs that
309 are also in the PARALLEL. Later.
311 If SET_P is nonzero, this is for the assignment hash table,
312 otherwise it is for the expression hash table. */
314 static void
315 hash_scan_insn (rtx insn, struct hash_table_d *table)
317 rtx pat = PATTERN (insn);
318 int i;
320 /* Pick out the sets of INSN and for other forms of instructions record
321 what's been modified. */
323 if (GET_CODE (pat) == SET)
324 hash_scan_set (pat, insn, table);
325 else if (GET_CODE (pat) == PARALLEL)
326 for (i = 0; i < XVECLEN (pat, 0); i++)
328 rtx x = XVECEXP (pat, 0, i);
330 if (GET_CODE (x) == SET)
331 hash_scan_set (x, insn, table);
335 static void
336 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
338 int i;
339 /* Flattened out table, so it's printed in proper order. */
340 struct expr **flat_table;
341 unsigned int *hash_val;
342 struct expr *expr;
344 flat_table = XCNEWVEC (struct expr *, table->n_elems);
345 hash_val = XNEWVEC (unsigned int, table->n_elems);
347 for (i = 0; i < (int) table->size; i++)
348 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
350 flat_table[expr->bitmap_index] = expr;
351 hash_val[expr->bitmap_index] = i;
354 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
355 name, table->size, table->n_elems);
357 for (i = 0; i < (int) table->n_elems; i++)
358 if (flat_table[i] != 0)
360 expr = flat_table[i];
361 fprintf (file, "Index %d (hash value %d)\n ",
362 expr->bitmap_index, hash_val[i]);
363 print_rtl (file, expr->dest);
364 fprintf (file, " := ");
365 print_rtl (file, expr->src);
366 fprintf (file, "\n");
369 fprintf (file, "\n");
371 free (flat_table);
372 free (hash_val);
375 /* Record as unavailable all registers that are DEF operands of INSN. */
376 static void
377 make_set_regs_unavailable (rtx insn)
379 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
380 df_ref *def_rec;
382 for (def_rec = DF_INSN_INFO_DEFS (insn_info); *def_rec; def_rec++)
383 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (*def_rec));
386 /* Top level function to create an assignments hash table.
388 Assignment entries are placed in the hash table if
389 - they are of the form (set (pseudo-reg) src),
390 - src is something we want to perform const/copy propagation on,
391 - none of the operands or target are subsequently modified in the block
393 Currently src must be a pseudo-reg or a const_int.
395 TABLE is the table computed. */
397 static void
398 compute_hash_table_work (struct hash_table_d *table)
400 basic_block bb;
402 /* Allocate vars to track sets of regs. */
403 reg_set_bitmap = ALLOC_REG_SET (NULL);
405 FOR_EACH_BB (bb)
407 rtx insn;
409 /* Reset tables used to keep track of what's not yet invalid [since
410 the end of the block]. */
411 CLEAR_REG_SET (reg_set_bitmap);
413 /* Go over all insns from the last to the first. This is convenient
414 for tracking available registers, i.e. not set between INSN and
415 the end of the basic block BB. */
416 FOR_BB_INSNS_REVERSE (bb, insn)
418 /* Only real insns are interesting. */
419 if (!NONDEBUG_INSN_P (insn))
420 continue;
422 /* Record interesting sets from INSN in the hash table. */
423 hash_scan_insn (insn, table);
425 /* Any registers set in INSN will make SETs above it not AVAIL. */
426 make_set_regs_unavailable (insn);
429 /* Insert implicit sets in the hash table, pretending they appear as
430 insns at the head of the basic block. */
431 if (implicit_sets[bb->index] != NULL_RTX)
432 hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table);
435 FREE_REG_SET (reg_set_bitmap);
438 /* Allocate space for the set/expr hash TABLE.
439 It is used to determine the number of buckets to use. */
441 static void
442 alloc_hash_table (struct hash_table_d *table)
444 int n;
446 n = get_max_insn_count ();
448 table->size = n / 4;
449 if (table->size < 11)
450 table->size = 11;
452 /* Attempt to maintain efficient use of hash table.
453 Making it an odd number is simplest for now.
454 ??? Later take some measurements. */
455 table->size |= 1;
456 n = table->size * sizeof (struct expr *);
457 table->table = XNEWVAR (struct expr *, n);
460 /* Free things allocated by alloc_hash_table. */
462 static void
463 free_hash_table (struct hash_table_d *table)
465 free (table->table);
468 /* Compute the hash TABLE for doing copy/const propagation or
469 expression hash table. */
471 static void
472 compute_hash_table (struct hash_table_d *table)
474 /* Initialize count of number of entries in hash table. */
475 table->n_elems = 0;
476 memset (table->table, 0, table->size * sizeof (struct expr *));
478 compute_hash_table_work (table);
481 /* Expression tracking support. */
483 /* Lookup REGNO in the set TABLE. The result is a pointer to the
484 table entry, or NULL if not found. */
486 static struct expr *
487 lookup_set (unsigned int regno, struct hash_table_d *table)
489 unsigned int hash = hash_set (regno, table->size);
490 struct expr *expr;
492 expr = table->table[hash];
494 while (expr && REGNO (expr->dest) != regno)
495 expr = expr->next_same_hash;
497 return expr;
500 /* Return the next entry for REGNO in list EXPR. */
502 static struct expr *
503 next_set (unsigned int regno, struct expr *expr)
506 expr = expr->next_same_hash;
507 while (expr && REGNO (expr->dest) != regno);
509 return expr;
512 /* Reset tables used to keep track of what's still available [since the
513 start of the block]. */
515 static void
516 reset_opr_set_tables (void)
518 /* Maintain a bitmap of which regs have been set since beginning of
519 the block. */
520 CLEAR_REG_SET (reg_set_bitmap);
523 /* Return nonzero if the register X has not been set yet [since the
524 start of the basic block containing INSN]. */
526 static int
527 reg_not_set_p (const_rtx x, const_rtx insn ATTRIBUTE_UNUSED)
529 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
532 /* Record things set by INSN.
533 This data is used by reg_not_set_p. */
535 static void
536 mark_oprs_set (rtx insn)
538 struct df_insn_info *insn_info = DF_INSN_INFO_GET (insn);
539 df_ref *def_rec;
541 for (def_rec = DF_INSN_INFO_DEFS (insn_info); *def_rec; def_rec++)
542 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (*def_rec));
546 /* Compute copy/constant propagation working variables. */
548 /* Local properties of assignments. */
549 static sbitmap *cprop_pavloc;
550 static sbitmap *cprop_absaltered;
552 /* Global properties of assignments (computed from the local properties). */
553 static sbitmap *cprop_avin;
554 static sbitmap *cprop_avout;
556 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
557 basic blocks. N_SETS is the number of sets. */
559 static void
560 alloc_cprop_mem (int n_blocks, int n_sets)
562 cprop_pavloc = sbitmap_vector_alloc (n_blocks, n_sets);
563 cprop_absaltered = sbitmap_vector_alloc (n_blocks, n_sets);
565 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
566 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
569 /* Free vars used by copy/const propagation. */
571 static void
572 free_cprop_mem (void)
574 sbitmap_vector_free (cprop_pavloc);
575 sbitmap_vector_free (cprop_absaltered);
576 sbitmap_vector_free (cprop_avin);
577 sbitmap_vector_free (cprop_avout);
580 /* Compute the local properties of each recorded expression.
582 Local properties are those that are defined by the block, irrespective of
583 other blocks.
585 An expression is transparent in a block if its operands are not modified
586 in the block.
588 An expression is computed (locally available) in a block if it is computed
589 at least once and expression would contain the same value if the
590 computation was moved to the end of the block.
592 TRANSP and COMP are destination sbitmaps for recording local properties. */
594 static void
595 compute_local_properties (sbitmap *transp, sbitmap *comp,
596 struct hash_table_d *table)
598 unsigned int i;
600 /* Initialize the bitmaps that were passed in. */
601 sbitmap_vector_zero (transp, last_basic_block);
602 sbitmap_vector_zero (comp, last_basic_block);
604 for (i = 0; i < table->size; i++)
606 struct expr *expr;
608 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
610 int indx = expr->bitmap_index;
611 df_ref def;
612 struct occr *occr;
614 /* The expression is transparent in a block if it is not killed,
615 i.e. DEST and SRC are not set or clobbered in the block.
616 We start by assuming all are transparent [none are killed],
617 and then set the bits for those that are. */
618 for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
619 def; def = DF_REF_NEXT_REG (def))
620 SET_BIT (transp[DF_REF_BB (def)->index], indx);
621 if (REG_P (expr->src))
622 for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
623 def; def = DF_REF_NEXT_REG (def))
624 SET_BIT (transp[DF_REF_BB (def)->index], indx);
626 /* The occurrences recorded in avail_occr are exactly those that
627 we want to set to nonzero in COMP. */
628 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
630 SET_BIT (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
636 /* Hash table support. */
638 /* Top level routine to do the dataflow analysis needed by copy/const
639 propagation. */
641 static void
642 compute_cprop_data (void)
644 compute_local_properties (cprop_absaltered, cprop_pavloc, &set_hash_table);
645 compute_available (cprop_pavloc, cprop_absaltered,
646 cprop_avout, cprop_avin);
649 /* Copy/constant propagation. */
651 /* Maximum number of register uses in an insn that we handle. */
652 #define MAX_USES 8
654 /* Table of uses (registers, both hard and pseudo) found in an insn.
655 Allocated statically to avoid alloc/free complexity and overhead. */
656 static rtx reg_use_table[MAX_USES];
658 /* Index into `reg_use_table' while building it. */
659 static unsigned reg_use_count;
661 /* Set up a list of register numbers used in INSN. The found uses are stored
662 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
663 and contains the number of uses in the table upon exit.
665 ??? If a register appears multiple times we will record it multiple times.
666 This doesn't hurt anything but it will slow things down. */
668 static void
669 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
671 int i, j;
672 enum rtx_code code;
673 const char *fmt;
674 rtx x = *xptr;
676 /* repeat is used to turn tail-recursion into iteration since GCC
677 can't do it when there's no return value. */
678 repeat:
679 if (x == 0)
680 return;
682 code = GET_CODE (x);
683 if (REG_P (x))
685 if (reg_use_count == MAX_USES)
686 return;
688 reg_use_table[reg_use_count] = x;
689 reg_use_count++;
692 /* Recursively scan the operands of this expression. */
694 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
696 if (fmt[i] == 'e')
698 /* If we are about to do the last recursive call
699 needed at this level, change it into iteration.
700 This function is called enough to be worth it. */
701 if (i == 0)
703 x = XEXP (x, 0);
704 goto repeat;
707 find_used_regs (&XEXP (x, i), data);
709 else if (fmt[i] == 'E')
710 for (j = 0; j < XVECLEN (x, i); j++)
711 find_used_regs (&XVECEXP (x, i, j), data);
715 /* Try to replace all uses of FROM in INSN with TO.
716 Return nonzero if successful. */
718 static int
719 try_replace_reg (rtx from, rtx to, rtx insn)
721 rtx note = find_reg_equal_equiv_note (insn);
722 rtx src = 0;
723 int success = 0;
724 rtx set = single_set (insn);
726 /* Usually we substitute easy stuff, so we won't copy everything.
727 We however need to take care to not duplicate non-trivial CONST
728 expressions. */
729 to = copy_rtx (to);
731 validate_replace_src_group (from, to, insn);
732 if (num_changes_pending () && apply_change_group ())
733 success = 1;
735 /* Try to simplify SET_SRC if we have substituted a constant. */
736 if (success && set && CONSTANT_P (to))
738 src = simplify_rtx (SET_SRC (set));
740 if (src)
741 validate_change (insn, &SET_SRC (set), src, 0);
744 /* If there is already a REG_EQUAL note, update the expression in it
745 with our replacement. */
746 if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
747 set_unique_reg_note (insn, REG_EQUAL,
748 simplify_replace_rtx (XEXP (note, 0), from, to));
749 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
751 /* If above failed and this is a single set, try to simplify the source of
752 the set given our substitution. We could perhaps try this for multiple
753 SETs, but it probably won't buy us anything. */
754 src = simplify_replace_rtx (SET_SRC (set), from, to);
756 if (!rtx_equal_p (src, SET_SRC (set))
757 && validate_change (insn, &SET_SRC (set), src, 0))
758 success = 1;
760 /* If we've failed perform the replacement, have a single SET to
761 a REG destination and don't yet have a note, add a REG_EQUAL note
762 to not lose information. */
763 if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
764 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
767 if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
769 /* Registers can also appear as uses in SET_DEST if it is a MEM.
770 We could perhaps try this for multiple SETs, but it probably
771 won't buy us anything. */
772 rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
774 if (!rtx_equal_p (dest, SET_DEST (set))
775 && validate_change (insn, &SET_DEST (set), dest, 0))
776 success = 1;
779 /* REG_EQUAL may get simplified into register.
780 We don't allow that. Remove that note. This code ought
781 not to happen, because previous code ought to synthesize
782 reg-reg move, but be on the safe side. */
783 if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
784 remove_note (insn, note);
786 return success;
789 /* Find a set of REGNOs that are available on entry to INSN's block. Returns
790 NULL no such set is found. */
792 static struct expr *
793 find_avail_set (int regno, rtx insn)
795 /* SET1 contains the last set found that can be returned to the caller for
796 use in a substitution. */
797 struct expr *set1 = 0;
799 /* Loops are not possible here. To get a loop we would need two sets
800 available at the start of the block containing INSN. i.e. we would
801 need two sets like this available at the start of the block:
803 (set (reg X) (reg Y))
804 (set (reg Y) (reg X))
806 This can not happen since the set of (reg Y) would have killed the
807 set of (reg X) making it unavailable at the start of this block. */
808 while (1)
810 rtx src;
811 struct expr *set = lookup_set (regno, &set_hash_table);
813 /* Find a set that is available at the start of the block
814 which contains INSN. */
815 while (set)
817 if (TEST_BIT (cprop_avin[BLOCK_FOR_INSN (insn)->index],
818 set->bitmap_index))
819 break;
820 set = next_set (regno, set);
823 /* If no available set was found we've reached the end of the
824 (possibly empty) copy chain. */
825 if (set == 0)
826 break;
828 src = set->src;
830 /* We know the set is available.
831 Now check that SRC is locally anticipatable (i.e. none of the
832 source operands have changed since the start of the block).
834 If the source operand changed, we may still use it for the next
835 iteration of this loop, but we may not use it for substitutions. */
837 if (cprop_constant_p (src) || reg_not_set_p (src, insn))
838 set1 = set;
840 /* If the source of the set is anything except a register, then
841 we have reached the end of the copy chain. */
842 if (! REG_P (src))
843 break;
845 /* Follow the copy chain, i.e. start another iteration of the loop
846 and see if we have an available copy into SRC. */
847 regno = REGNO (src);
850 /* SET1 holds the last set that was available and anticipatable at
851 INSN. */
852 return set1;
855 /* Subroutine of cprop_insn that tries to propagate constants into
856 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
857 it is the instruction that immediately precedes JUMP, and must be a
858 single SET of a register. FROM is what we will try to replace,
859 SRC is the constant we will try to substitute for it. Returns nonzero
860 if a change was made. */
862 static int
863 cprop_jump (basic_block bb, rtx setcc, rtx jump, rtx from, rtx src)
865 rtx new_rtx, set_src, note_src;
866 rtx set = pc_set (jump);
867 rtx note = find_reg_equal_equiv_note (jump);
869 if (note)
871 note_src = XEXP (note, 0);
872 if (GET_CODE (note_src) == EXPR_LIST)
873 note_src = NULL_RTX;
875 else note_src = NULL_RTX;
877 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
878 set_src = note_src ? note_src : SET_SRC (set);
880 /* First substitute the SETCC condition into the JUMP instruction,
881 then substitute that given values into this expanded JUMP. */
882 if (setcc != NULL_RTX
883 && !modified_between_p (from, setcc, jump)
884 && !modified_between_p (src, setcc, jump))
886 rtx setcc_src;
887 rtx setcc_set = single_set (setcc);
888 rtx setcc_note = find_reg_equal_equiv_note (setcc);
889 setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
890 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
891 set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
892 setcc_src);
894 else
895 setcc = NULL_RTX;
897 new_rtx = simplify_replace_rtx (set_src, from, src);
899 /* If no simplification can be made, then try the next register. */
900 if (rtx_equal_p (new_rtx, SET_SRC (set)))
901 return 0;
903 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
904 if (new_rtx == pc_rtx)
905 delete_insn (jump);
906 else
908 /* Ensure the value computed inside the jump insn to be equivalent
909 to one computed by setcc. */
910 if (setcc && modified_in_p (new_rtx, setcc))
911 return 0;
912 if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
914 /* When (some) constants are not valid in a comparison, and there
915 are two registers to be replaced by constants before the entire
916 comparison can be folded into a constant, we need to keep
917 intermediate information in REG_EQUAL notes. For targets with
918 separate compare insns, such notes are added by try_replace_reg.
919 When we have a combined compare-and-branch instruction, however,
920 we need to attach a note to the branch itself to make this
921 optimization work. */
923 if (!rtx_equal_p (new_rtx, note_src))
924 set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
925 return 0;
928 /* Remove REG_EQUAL note after simplification. */
929 if (note_src)
930 remove_note (jump, note);
933 #ifdef HAVE_cc0
934 /* Delete the cc0 setter. */
935 if (setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
936 delete_insn (setcc);
937 #endif
939 global_const_prop_count++;
940 if (dump_file != NULL)
942 fprintf (dump_file,
943 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with constant ",
944 REGNO (from), INSN_UID (jump));
945 print_rtl (dump_file, src);
946 fprintf (dump_file, "\n");
948 purge_dead_edges (bb);
950 /* If a conditional jump has been changed into unconditional jump, remove
951 the jump and make the edge fallthru - this is always called in
952 cfglayout mode. */
953 if (new_rtx != pc_rtx && simplejump_p (jump))
955 edge e;
956 edge_iterator ei;
958 FOR_EACH_EDGE (e, ei, bb->succs)
959 if (e->dest != EXIT_BLOCK_PTR
960 && BB_HEAD (e->dest) == JUMP_LABEL (jump))
962 e->flags |= EDGE_FALLTHRU;
963 break;
965 delete_insn (jump);
968 return 1;
971 static bool
972 constprop_register (rtx insn, rtx from, rtx to)
974 rtx sset;
976 /* Check for reg or cc0 setting instructions followed by
977 conditional branch instructions first. */
978 if ((sset = single_set (insn)) != NULL
979 && NEXT_INSN (insn)
980 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
982 rtx dest = SET_DEST (sset);
983 if ((REG_P (dest) || CC0_P (dest))
984 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn), from, to))
985 return 1;
988 /* Handle normal insns next. */
989 if (NONJUMP_INSN_P (insn)
990 && try_replace_reg (from, to, insn))
991 return 1;
993 /* Try to propagate a CONST_INT into a conditional jump.
994 We're pretty specific about what we will handle in this
995 code, we can extend this as necessary over time.
997 Right now the insn in question must look like
998 (set (pc) (if_then_else ...)) */
999 else if (any_condjump_p (insn) && onlyjump_p (insn))
1000 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, to);
1001 return 0;
1004 /* Perform constant and copy propagation on INSN.
1005 The result is nonzero if a change was made. */
1007 static int
1008 cprop_insn (rtx insn)
1010 unsigned i;
1011 int changed = 0, changed_this_round;
1012 rtx note;
1014 retry:
1015 changed_this_round = 0;
1016 reg_use_count = 0;
1017 note_uses (&PATTERN (insn), find_used_regs, NULL);
1019 /* We may win even when propagating constants into notes. */
1020 note = find_reg_equal_equiv_note (insn);
1021 if (note)
1022 find_used_regs (&XEXP (note, 0), NULL);
1024 for (i = 0; i < reg_use_count; i++)
1026 rtx reg_used = reg_use_table[i];
1027 unsigned int regno = REGNO (reg_used);
1028 rtx src;
1029 struct expr *set;
1031 /* If the register has already been set in this block, there's
1032 nothing we can do. */
1033 if (! reg_not_set_p (reg_used, insn))
1034 continue;
1036 /* Find an assignment that sets reg_used and is available
1037 at the start of the block. */
1038 set = find_avail_set (regno, insn);
1039 if (! set)
1040 continue;
1042 src = set->src;
1044 /* Constant propagation. */
1045 if (cprop_constant_p (src))
1047 if (constprop_register (insn, reg_used, src))
1049 changed_this_round = changed = 1;
1050 global_const_prop_count++;
1051 if (dump_file != NULL)
1053 fprintf (dump_file, "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
1054 fprintf (dump_file, "insn %d with constant ", INSN_UID (insn));
1055 print_rtl (dump_file, src);
1056 fprintf (dump_file, "\n");
1058 if (INSN_DELETED_P (insn))
1059 return 1;
1062 else if (REG_P (src)
1063 && REGNO (src) >= FIRST_PSEUDO_REGISTER
1064 && REGNO (src) != regno)
1066 if (try_replace_reg (reg_used, src, insn))
1068 changed_this_round = changed = 1;
1069 global_copy_prop_count++;
1070 if (dump_file != NULL)
1072 fprintf (dump_file, "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1073 regno, INSN_UID (insn));
1074 fprintf (dump_file, " with reg %d\n", REGNO (src));
1077 /* The original insn setting reg_used may or may not now be
1078 deletable. We leave the deletion to DCE. */
1079 /* FIXME: If it turns out that the insn isn't deletable,
1080 then we may have unnecessarily extended register lifetimes
1081 and made things worse. */
1085 /* If try_replace_reg simplified the insn, the regs found
1086 by find_used_regs may not be valid anymore. Start over. */
1087 if (changed_this_round)
1088 goto retry;
1091 if (changed && DEBUG_INSN_P (insn))
1092 return 0;
1094 return changed;
1097 /* Like find_used_regs, but avoid recording uses that appear in
1098 input-output contexts such as zero_extract or pre_dec. This
1099 restricts the cases we consider to those for which local cprop
1100 can legitimately make replacements. */
1102 static void
1103 local_cprop_find_used_regs (rtx *xptr, void *data)
1105 rtx x = *xptr;
1107 if (x == 0)
1108 return;
1110 switch (GET_CODE (x))
1112 case ZERO_EXTRACT:
1113 case SIGN_EXTRACT:
1114 case STRICT_LOW_PART:
1115 return;
1117 case PRE_DEC:
1118 case PRE_INC:
1119 case POST_DEC:
1120 case POST_INC:
1121 case PRE_MODIFY:
1122 case POST_MODIFY:
1123 /* Can only legitimately appear this early in the context of
1124 stack pushes for function arguments, but handle all of the
1125 codes nonetheless. */
1126 return;
1128 case SUBREG:
1129 /* Setting a subreg of a register larger than word_mode leaves
1130 the non-written words unchanged. */
1131 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
1132 return;
1133 break;
1135 default:
1136 break;
1139 find_used_regs (xptr, data);
1142 /* Try to perform local const/copy propagation on X in INSN. */
1144 static bool
1145 do_local_cprop (rtx x, rtx insn)
1147 rtx newreg = NULL, newcnst = NULL;
1149 /* Rule out USE instructions and ASM statements as we don't want to
1150 change the hard registers mentioned. */
1151 if (REG_P (x)
1152 && (REGNO (x) >= FIRST_PSEUDO_REGISTER
1153 || (GET_CODE (PATTERN (insn)) != USE
1154 && asm_noperands (PATTERN (insn)) < 0)))
1156 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
1157 struct elt_loc_list *l;
1159 if (!val)
1160 return false;
1161 for (l = val->locs; l; l = l->next)
1163 rtx this_rtx = l->loc;
1164 rtx note;
1166 if (cprop_constant_p (this_rtx))
1167 newcnst = this_rtx;
1168 if (REG_P (this_rtx) && REGNO (this_rtx) >= FIRST_PSEUDO_REGISTER
1169 /* Don't copy propagate if it has attached REG_EQUIV note.
1170 At this point this only function parameters should have
1171 REG_EQUIV notes and if the argument slot is used somewhere
1172 explicitly, it means address of parameter has been taken,
1173 so we should not extend the lifetime of the pseudo. */
1174 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
1175 || ! MEM_P (XEXP (note, 0))))
1176 newreg = this_rtx;
1178 if (newcnst && constprop_register (insn, x, newcnst))
1180 if (dump_file != NULL)
1182 fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
1183 REGNO (x));
1184 fprintf (dump_file, "insn %d with constant ",
1185 INSN_UID (insn));
1186 print_rtl (dump_file, newcnst);
1187 fprintf (dump_file, "\n");
1189 local_const_prop_count++;
1190 return true;
1192 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
1194 if (dump_file != NULL)
1196 fprintf (dump_file,
1197 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
1198 REGNO (x), INSN_UID (insn));
1199 fprintf (dump_file, " with reg %d\n", REGNO (newreg));
1201 local_copy_prop_count++;
1202 return true;
1205 return false;
1208 /* Do local const/copy propagation (i.e. within each basic block). */
1210 static int
1211 local_cprop_pass (void)
1213 basic_block bb;
1214 rtx insn;
1215 bool changed = false;
1216 unsigned i;
1218 cselib_init (0);
1219 FOR_EACH_BB (bb)
1221 FOR_BB_INSNS (bb, insn)
1223 if (INSN_P (insn))
1225 rtx note = find_reg_equal_equiv_note (insn);
1228 reg_use_count = 0;
1229 note_uses (&PATTERN (insn), local_cprop_find_used_regs,
1230 NULL);
1231 if (note)
1232 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
1234 for (i = 0; i < reg_use_count; i++)
1236 if (do_local_cprop (reg_use_table[i], insn))
1238 if (!DEBUG_INSN_P (insn))
1239 changed = true;
1240 break;
1243 if (INSN_DELETED_P (insn))
1244 break;
1246 while (i < reg_use_count);
1248 cselib_process_insn (insn);
1251 /* Forget everything at the end of a basic block. */
1252 cselib_clear_table ();
1255 cselib_finish ();
1257 return changed;
1260 /* Similar to get_condition, only the resulting condition must be
1261 valid at JUMP, instead of at EARLIEST.
1263 This differs from noce_get_condition in ifcvt.c in that we prefer not to
1264 settle for the condition variable in the jump instruction being integral.
1265 We prefer to be able to record the value of a user variable, rather than
1266 the value of a temporary used in a condition. This could be solved by
1267 recording the value of *every* register scanned by canonicalize_condition,
1268 but this would require some code reorganization. */
1271 fis_get_condition (rtx jump)
1273 return get_condition (jump, NULL, false, true);
1276 /* Check the comparison COND to see if we can safely form an implicit
1277 set from it. */
1279 static bool
1280 implicit_set_cond_p (const_rtx cond)
1282 enum machine_mode mode;
1283 rtx cst;
1285 /* COND must be either an EQ or NE comparison. */
1286 if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
1287 return false;
1289 /* The first operand of COND must be a pseudo-reg. */
1290 if (! REG_P (XEXP (cond, 0))
1291 || HARD_REGISTER_P (XEXP (cond, 0)))
1292 return false;
1294 /* The second operand of COND must be a suitable constant. */
1295 mode = GET_MODE (XEXP (cond, 0));
1296 cst = XEXP (cond, 1);
1298 /* We can't perform this optimization if either operand might be or might
1299 contain a signed zero. */
1300 if (HONOR_SIGNED_ZEROS (mode))
1302 /* It is sufficient to check if CST is or contains a zero. We must
1303 handle float, complex, and vector. If any subpart is a zero, then
1304 the optimization can't be performed. */
1305 /* ??? The complex and vector checks are not implemented yet. We just
1306 always return zero for them. */
1307 if (GET_CODE (cst) == CONST_DOUBLE)
1309 REAL_VALUE_TYPE d;
1310 REAL_VALUE_FROM_CONST_DOUBLE (d, cst);
1311 if (REAL_VALUES_EQUAL (d, dconst0))
1312 return 0;
1314 else
1315 return 0;
1318 return cprop_constant_p (cst);
1321 /* Find the implicit sets of a function. An "implicit set" is a constraint
1322 on the value of a variable, implied by a conditional jump. For example,
1323 following "if (x == 2)", the then branch may be optimized as though the
1324 conditional performed an "explicit set", in this example, "x = 2". This
1325 function records the set patterns that are implicit at the start of each
1326 basic block.
1328 If an implicit set is found but the set is implicit on a critical edge,
1329 this critical edge is split.
1331 Return true if the CFG was modified, false otherwise. */
1333 static bool
1334 find_implicit_sets (void)
1336 basic_block bb, dest;
1337 rtx cond, new_rtx;
1338 unsigned int count = 0;
1339 bool edges_split = false;
1340 size_t implicit_sets_size = last_basic_block + 10;
1342 implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1344 FOR_EACH_BB (bb)
1346 /* Check for more than one successor. */
1347 if (EDGE_COUNT (bb->succs) <= 1)
1348 continue;
1350 cond = fis_get_condition (BB_END (bb));
1352 /* If no condition is found or if it isn't of a suitable form,
1353 ignore it. */
1354 if (! cond || ! implicit_set_cond_p (cond))
1355 continue;
1357 dest = GET_CODE (cond) == EQ
1358 ? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
1360 /* If DEST doesn't go anywhere, ignore it. */
1361 if (! dest || dest == EXIT_BLOCK_PTR)
1362 continue;
1364 /* We have found a suitable implicit set. Try to record it now as
1365 a SET in DEST. If DEST has more than one predecessor, the edge
1366 between BB and DEST is a critical edge and we must split it,
1367 because we can only record one implicit set per DEST basic block. */
1368 if (! single_pred_p (dest))
1370 dest = split_edge (find_edge (bb, dest));
1371 edges_split = true;
1374 if (implicit_sets_size <= (size_t) dest->index)
1376 size_t old_implicit_sets_size = implicit_sets_size;
1377 implicit_sets_size *= 2;
1378 implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
1379 memset (implicit_sets + old_implicit_sets_size, 0,
1380 (implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1383 new_rtx = gen_rtx_SET (VOIDmode, XEXP (cond, 0),
1384 XEXP (cond, 1));
1385 implicit_sets[dest->index] = new_rtx;
1386 if (dump_file)
1388 fprintf(dump_file, "Implicit set of reg %d in ",
1389 REGNO (XEXP (cond, 0)));
1390 fprintf(dump_file, "basic block %d\n", dest->index);
1392 count++;
1395 if (dump_file)
1396 fprintf (dump_file, "Found %d implicit sets\n", count);
1398 /* Confess our sins. */
1399 return edges_split;
1402 /* Bypass conditional jumps. */
1404 /* The value of last_basic_block at the beginning of the jump_bypass
1405 pass. The use of redirect_edge_and_branch_force may introduce new
1406 basic blocks, but the data flow analysis is only valid for basic
1407 block indices less than bypass_last_basic_block. */
1409 static int bypass_last_basic_block;
1411 /* Find a set of REGNO to a constant that is available at the end of basic
1412 block BB. Returns NULL if no such set is found. Based heavily upon
1413 find_avail_set. */
1415 static struct expr *
1416 find_bypass_set (int regno, int bb)
1418 struct expr *result = 0;
1420 for (;;)
1422 rtx src;
1423 struct expr *set = lookup_set (regno, &set_hash_table);
1425 while (set)
1427 if (TEST_BIT (cprop_avout[bb], set->bitmap_index))
1428 break;
1429 set = next_set (regno, set);
1432 if (set == 0)
1433 break;
1435 src = set->src;
1436 if (cprop_constant_p (src))
1437 result = set;
1439 if (! REG_P (src))
1440 break;
1442 regno = REGNO (src);
1444 return result;
1448 /* Subroutine of bypass_block that checks whether a pseudo is killed by
1449 any of the instructions inserted on an edge. Jump bypassing places
1450 condition code setters on CFG edges using insert_insn_on_edge. This
1451 function is required to check that our data flow analysis is still
1452 valid prior to commit_edge_insertions. */
1454 static bool
1455 reg_killed_on_edge (const_rtx reg, const_edge e)
1457 rtx insn;
1459 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
1460 if (INSN_P (insn) && reg_set_p (reg, insn))
1461 return true;
1463 return false;
1466 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
1467 basic block BB which has more than one predecessor. If not NULL, SETCC
1468 is the first instruction of BB, which is immediately followed by JUMP_INSN
1469 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
1470 Returns nonzero if a change was made.
1472 During the jump bypassing pass, we may place copies of SETCC instructions
1473 on CFG edges. The following routine must be careful to pay attention to
1474 these inserted insns when performing its transformations. */
1476 static int
1477 bypass_block (basic_block bb, rtx setcc, rtx jump)
1479 rtx insn, note;
1480 edge e, edest;
1481 int change;
1482 int may_be_loop_header;
1483 unsigned removed_p;
1484 unsigned i;
1485 edge_iterator ei;
1487 insn = (setcc != NULL) ? setcc : jump;
1489 /* Determine set of register uses in INSN. */
1490 reg_use_count = 0;
1491 note_uses (&PATTERN (insn), find_used_regs, NULL);
1492 note = find_reg_equal_equiv_note (insn);
1493 if (note)
1494 find_used_regs (&XEXP (note, 0), NULL);
1496 may_be_loop_header = false;
1497 FOR_EACH_EDGE (e, ei, bb->preds)
1498 if (e->flags & EDGE_DFS_BACK)
1500 may_be_loop_header = true;
1501 break;
1504 change = 0;
1505 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1507 removed_p = 0;
1509 if (e->flags & EDGE_COMPLEX)
1511 ei_next (&ei);
1512 continue;
1515 /* We can't redirect edges from new basic blocks. */
1516 if (e->src->index >= bypass_last_basic_block)
1518 ei_next (&ei);
1519 continue;
1522 /* The irreducible loops created by redirecting of edges entering the
1523 loop from outside would decrease effectiveness of some of the following
1524 optimizations, so prevent this. */
1525 if (may_be_loop_header
1526 && !(e->flags & EDGE_DFS_BACK))
1528 ei_next (&ei);
1529 continue;
1532 for (i = 0; i < reg_use_count; i++)
1534 rtx reg_used = reg_use_table[i];
1535 unsigned int regno = REGNO (reg_used);
1536 basic_block dest, old_dest;
1537 struct expr *set;
1538 rtx src, new_rtx;
1540 set = find_bypass_set (regno, e->src->index);
1542 if (! set)
1543 continue;
1545 /* Check the data flow is valid after edge insertions. */
1546 if (e->insns.r && reg_killed_on_edge (reg_used, e))
1547 continue;
1549 src = SET_SRC (pc_set (jump));
1551 if (setcc != NULL)
1552 src = simplify_replace_rtx (src,
1553 SET_DEST (PATTERN (setcc)),
1554 SET_SRC (PATTERN (setcc)));
1556 new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1558 /* Jump bypassing may have already placed instructions on
1559 edges of the CFG. We can't bypass an outgoing edge that
1560 has instructions associated with it, as these insns won't
1561 get executed if the incoming edge is redirected. */
1563 if (new_rtx == pc_rtx)
1565 edest = FALLTHRU_EDGE (bb);
1566 dest = edest->insns.r ? NULL : edest->dest;
1568 else if (GET_CODE (new_rtx) == LABEL_REF)
1570 dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
1571 /* Don't bypass edges containing instructions. */
1572 edest = find_edge (bb, dest);
1573 if (edest && edest->insns.r)
1574 dest = NULL;
1576 else
1577 dest = NULL;
1579 /* Avoid unification of the edge with other edges from original
1580 branch. We would end up emitting the instruction on "both"
1581 edges. */
1583 if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
1584 && find_edge (e->src, dest))
1585 dest = NULL;
1587 old_dest = e->dest;
1588 if (dest != NULL
1589 && dest != old_dest
1590 && dest != EXIT_BLOCK_PTR)
1592 redirect_edge_and_branch_force (e, dest);
1594 /* Copy the register setter to the redirected edge.
1595 Don't copy CC0 setters, as CC0 is dead after jump. */
1596 if (setcc)
1598 rtx pat = PATTERN (setcc);
1599 if (!CC0_P (SET_DEST (pat)))
1600 insert_insn_on_edge (copy_insn (pat), e);
1603 if (dump_file != NULL)
1605 fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
1606 "in jump_insn %d equals constant ",
1607 regno, INSN_UID (jump));
1608 print_rtl (dump_file, set->src);
1609 fprintf (dump_file, "\nBypass edge from %d->%d to %d\n",
1610 e->src->index, old_dest->index, dest->index);
1612 change = 1;
1613 removed_p = 1;
1614 break;
1617 if (!removed_p)
1618 ei_next (&ei);
1620 return change;
1623 /* Find basic blocks with more than one predecessor that only contain a
1624 single conditional jump. If the result of the comparison is known at
1625 compile-time from any incoming edge, redirect that edge to the
1626 appropriate target. Returns nonzero if a change was made.
1628 This function is now mis-named, because we also handle indirect jumps. */
1630 static int
1631 bypass_conditional_jumps (void)
1633 basic_block bb;
1634 int changed;
1635 rtx setcc;
1636 rtx insn;
1637 rtx dest;
1639 /* Note we start at block 1. */
1640 if (ENTRY_BLOCK_PTR->next_bb == EXIT_BLOCK_PTR)
1641 return 0;
1643 bypass_last_basic_block = last_basic_block;
1644 mark_dfs_back_edges ();
1646 changed = 0;
1647 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb,
1648 EXIT_BLOCK_PTR, next_bb)
1650 /* Check for more than one predecessor. */
1651 if (!single_pred_p (bb))
1653 setcc = NULL_RTX;
1654 FOR_BB_INSNS (bb, insn)
1655 if (DEBUG_INSN_P (insn))
1656 continue;
1657 else if (NONJUMP_INSN_P (insn))
1659 if (setcc)
1660 break;
1661 if (GET_CODE (PATTERN (insn)) != SET)
1662 break;
1664 dest = SET_DEST (PATTERN (insn));
1665 if (REG_P (dest) || CC0_P (dest))
1666 setcc = insn;
1667 else
1668 break;
1670 else if (JUMP_P (insn))
1672 if ((any_condjump_p (insn) || computed_jump_p (insn))
1673 && onlyjump_p (insn))
1674 changed |= bypass_block (bb, setcc, insn);
1675 break;
1677 else if (INSN_P (insn))
1678 break;
1682 /* If we bypassed any register setting insns, we inserted a
1683 copy on the redirected edge. These need to be committed. */
1684 if (changed)
1685 commit_edge_insertions ();
1687 return changed;
1690 /* Return true if the graph is too expensive to optimize. PASS is the
1691 optimization about to be performed. */
1693 static bool
1694 is_too_expensive (const char *pass)
1696 /* Trying to perform global optimizations on flow graphs which have
1697 a high connectivity will take a long time and is unlikely to be
1698 particularly useful.
1700 In normal circumstances a cfg should have about twice as many
1701 edges as blocks. But we do not want to punish small functions
1702 which have a couple switch statements. Rather than simply
1703 threshold the number of blocks, uses something with a more
1704 graceful degradation. */
1705 if (n_edges > 20000 + n_basic_blocks * 4)
1707 warning (OPT_Wdisabled_optimization,
1708 "%s: %d basic blocks and %d edges/basic block",
1709 pass, n_basic_blocks, n_edges / n_basic_blocks);
1711 return true;
1714 /* If allocating memory for the cprop bitmap would take up too much
1715 storage it's better just to disable the optimization. */
1716 if ((n_basic_blocks
1717 * SBITMAP_SET_SIZE (max_reg_num ())
1718 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
1720 warning (OPT_Wdisabled_optimization,
1721 "%s: %d basic blocks and %d registers",
1722 pass, n_basic_blocks, max_reg_num ());
1724 return true;
1727 return false;
1731 /* Main function for the CPROP pass. */
1733 static int
1734 one_cprop_pass (void)
1736 int changed = 0;
1738 /* Return if there's nothing to do, or it is too expensive. */
1739 if (n_basic_blocks <= NUM_FIXED_BLOCKS + 1
1740 || is_too_expensive (_ ("const/copy propagation disabled")))
1741 return 0;
1743 global_const_prop_count = local_const_prop_count = 0;
1744 global_copy_prop_count = local_copy_prop_count = 0;
1746 bytes_used = 0;
1747 gcc_obstack_init (&cprop_obstack);
1749 /* Do a local const/copy propagation pass first. The global pass
1750 only handles global opportunities.
1751 If the local pass changes something, remove any unreachable blocks
1752 because the CPROP global dataflow analysis may get into infinite
1753 loops for CFGs with unreachable blocks.
1755 FIXME: This local pass should not be necessary after CSE (but for
1756 some reason it still is). It is also (proven) not necessary
1757 to run the local pass right after FWPWOP.
1759 FIXME: The global analysis would not get into infinite loops if it
1760 would use the DF solver (via df_simple_dataflow) instead of
1761 the solver implemented in this file. */
1762 changed |= local_cprop_pass ();
1763 if (changed)
1764 delete_unreachable_blocks ();
1766 /* Determine implicit sets. This may change the CFG (split critical
1767 edges if that exposes an implicit set).
1768 Note that find_implicit_sets() does not rely on up-to-date DF caches
1769 so that we do not have to re-run df_analyze() even if local CPROP
1770 changed something.
1771 ??? This could run earlier so that any uncovered implicit sets
1772 sets could be exploited in local_cprop_pass() also. Later. */
1773 changed |= find_implicit_sets ();
1775 /* If local_cprop_pass() or find_implicit_sets() changed something,
1776 run df_analyze() to bring all insn caches up-to-date, and to take
1777 new basic blocks from edge splitting on the DF radar.
1778 NB: This also runs the fast DCE pass, because execute_rtl_cprop
1779 sets DF_LR_RUN_DCE. */
1780 if (changed)
1781 df_analyze ();
1783 alloc_hash_table (&set_hash_table);
1784 compute_hash_table (&set_hash_table);
1786 /* Free implicit_sets before peak usage. */
1787 free (implicit_sets);
1788 implicit_sets = NULL;
1790 if (dump_file)
1791 dump_hash_table (dump_file, "SET", &set_hash_table);
1792 if (set_hash_table.n_elems > 0)
1794 basic_block bb;
1795 rtx insn;
1797 alloc_cprop_mem (last_basic_block, set_hash_table.n_elems);
1798 compute_cprop_data ();
1800 /* Allocate vars to track sets of regs. */
1801 reg_set_bitmap = ALLOC_REG_SET (NULL);
1803 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR->next_bb->next_bb, EXIT_BLOCK_PTR, next_bb)
1805 /* Reset tables used to keep track of what's still valid [since
1806 the start of the block]. */
1807 reset_opr_set_tables ();
1809 FOR_BB_INSNS (bb, insn)
1810 if (INSN_P (insn))
1812 changed |= cprop_insn (insn);
1814 /* Keep track of everything modified by this insn. */
1815 /* ??? Need to be careful w.r.t. mods done to INSN.
1816 Don't call mark_oprs_set if we turned the
1817 insn into a NOTE, or deleted the insn. */
1818 if (! NOTE_P (insn) && ! INSN_DELETED_P (insn))
1819 mark_oprs_set (insn);
1823 changed |= bypass_conditional_jumps ();
1825 FREE_REG_SET (reg_set_bitmap);
1826 free_cprop_mem ();
1829 free_hash_table (&set_hash_table);
1830 obstack_free (&cprop_obstack, NULL);
1832 if (dump_file)
1834 fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1835 current_function_name (), n_basic_blocks, bytes_used);
1836 fprintf (dump_file, "%d local const props, %d local copy props, ",
1837 local_const_prop_count, local_copy_prop_count);
1838 fprintf (dump_file, "%d global const props, %d global copy props\n\n",
1839 global_const_prop_count, global_copy_prop_count);
1842 return changed;
1846 /* All the passes implemented in this file. Each pass has its
1847 own gate and execute function, and at the end of the file a
1848 pass definition for passes.c.
1850 We do not construct an accurate cfg in functions which call
1851 setjmp, so none of these passes runs if the function calls
1852 setjmp.
1853 FIXME: Should just handle setjmp via REG_SETJMP notes. */
1855 static bool
1856 gate_rtl_cprop (void)
1858 return optimize > 0 && flag_gcse
1859 && !cfun->calls_setjmp
1860 && dbg_cnt (cprop);
1863 static unsigned int
1864 execute_rtl_cprop (void)
1866 int changed;
1867 delete_unreachable_blocks ();
1868 df_set_flags (DF_LR_RUN_DCE);
1869 df_analyze ();
1870 changed = one_cprop_pass ();
1871 flag_rerun_cse_after_global_opts |= changed;
1872 if (changed)
1873 cleanup_cfg (0);
1874 return 0;
1877 struct rtl_opt_pass pass_rtl_cprop =
1880 RTL_PASS,
1881 "cprop", /* name */
1882 gate_rtl_cprop, /* gate */
1883 execute_rtl_cprop, /* execute */
1884 NULL, /* sub */
1885 NULL, /* next */
1886 0, /* static_pass_number */
1887 TV_CPROP, /* tv_id */
1888 PROP_cfglayout, /* properties_required */
1889 0, /* properties_provided */
1890 0, /* properties_destroyed */
1891 0, /* todo_flags_start */
1892 TODO_df_finish | TODO_verify_rtl_sharing |
1893 TODO_verify_flow | TODO_ggc_collect /* todo_flags_finish */