[PR 70965] Schedule extra rebuild_cgraph_edges
[official-gcc.git] / gcc / cprop.c
blob31868a5c6bb4f5110276eb285d0ace01a61fb820
1 /* Global constant/copy propagation for RTL.
2 Copyright (C) 1997-2016 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 "backend.h"
24 #include "rtl.h"
25 #include "cfghooks.h"
26 #include "df.h"
27 #include "insn-config.h"
28 #include "memmodel.h"
29 #include "emit-rtl.h"
30 #include "recog.h"
31 #include "diagnostic-core.h"
32 #include "toplev.h"
33 #include "cfgrtl.h"
34 #include "cfganal.h"
35 #include "lcm.h"
36 #include "cfgcleanup.h"
37 #include "params.h"
38 #include "cselib.h"
39 #include "intl.h"
40 #include "tree-pass.h"
41 #include "dbgcnt.h"
42 #include "cfgloop.h"
43 #include "gcse.h"
46 /* An obstack for our working variables. */
47 static struct obstack cprop_obstack;
49 /* Occurrence of an expression.
50 There is one per basic block. If a pattern appears more than once the
51 last appearance is used. */
53 struct cprop_occr
55 /* Next occurrence of this expression. */
56 struct cprop_occr *next;
57 /* The insn that computes the expression. */
58 rtx_insn *insn;
61 /* Hash table entry for assignment expressions. */
63 struct cprop_expr
65 /* The expression (DEST := SRC). */
66 rtx dest;
67 rtx src;
69 /* Index in the available expression bitmaps. */
70 int bitmap_index;
71 /* Next entry with the same hash. */
72 struct cprop_expr *next_same_hash;
73 /* List of available occurrence in basic blocks in the function.
74 An "available occurrence" is one that is the last occurrence in the
75 basic block and whose operands are not modified by following statements
76 in the basic block [including this insn]. */
77 struct cprop_occr *avail_occr;
80 /* Hash table for copy propagation expressions.
81 Each hash table is an array of buckets.
82 ??? It is known that if it were an array of entries, structure elements
83 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
84 not clear whether in the final analysis a sufficient amount of memory would
85 be saved as the size of the available expression bitmaps would be larger
86 [one could build a mapping table without holes afterwards though].
87 Someday I'll perform the computation and figure it out. */
89 struct hash_table_d
91 /* The table itself.
92 This is an array of `set_hash_table_size' elements. */
93 struct cprop_expr **table;
95 /* Size of the hash table, in elements. */
96 unsigned int size;
98 /* Number of hash table elements. */
99 unsigned int n_elems;
102 /* Copy propagation hash table. */
103 static struct hash_table_d set_hash_table;
105 /* Array of implicit set patterns indexed by basic block index. */
106 static rtx *implicit_sets;
108 /* Array of indexes of expressions for implicit set patterns indexed by basic
109 block index. In other words, implicit_set_indexes[i] is the bitmap_index
110 of the expression whose RTX is implicit_sets[i]. */
111 static int *implicit_set_indexes;
113 /* Bitmap containing one bit for each register in the program.
114 Used when performing GCSE to track which registers have been set since
115 the start or end of the basic block while traversing that block. */
116 static regset reg_set_bitmap;
118 /* Various variables for statistics gathering. */
120 /* Memory used in a pass.
121 This isn't intended to be absolutely precise. Its intent is only
122 to keep an eye on memory usage. */
123 static int bytes_used;
125 /* Number of local constants propagated. */
126 static int local_const_prop_count;
127 /* Number of local copies propagated. */
128 static int local_copy_prop_count;
129 /* Number of global constants propagated. */
130 static int global_const_prop_count;
131 /* Number of global copies propagated. */
132 static int global_copy_prop_count;
134 #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T)))
135 #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S)))
137 /* Cover function to obstack_alloc. */
139 static void *
140 cprop_alloc (unsigned long size)
142 bytes_used += size;
143 return obstack_alloc (&cprop_obstack, size);
146 /* Return nonzero if register X is unchanged from INSN to the end
147 of INSN's basic block. */
149 static int
150 reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
152 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
155 /* Hash a set of register REGNO.
157 Sets are hashed on the register that is set. This simplifies the PRE copy
158 propagation code.
160 ??? May need to make things more elaborate. Later, as necessary. */
162 static unsigned int
163 hash_mod (int regno, int hash_table_size)
165 return (unsigned) regno % hash_table_size;
168 /* Insert assignment DEST:=SET from INSN in the hash table.
169 DEST is a register and SET is a register or a suitable constant.
170 If the assignment is already present in the table, record it as
171 the last occurrence in INSN's basic block.
172 IMPLICIT is true if it's an implicit set, false otherwise. */
174 static void
175 insert_set_in_table (rtx dest, rtx src, rtx_insn *insn,
176 struct hash_table_d *table, bool implicit)
178 bool found = false;
179 unsigned int hash;
180 struct cprop_expr *cur_expr, *last_expr = NULL;
181 struct cprop_occr *cur_occr;
183 hash = hash_mod (REGNO (dest), table->size);
185 for (cur_expr = table->table[hash]; cur_expr;
186 cur_expr = cur_expr->next_same_hash)
188 if (dest == cur_expr->dest
189 && src == cur_expr->src)
191 found = true;
192 break;
194 last_expr = cur_expr;
197 if (! found)
199 cur_expr = GOBNEW (struct cprop_expr);
200 bytes_used += sizeof (struct cprop_expr);
201 if (table->table[hash] == NULL)
202 /* This is the first pattern that hashed to this index. */
203 table->table[hash] = cur_expr;
204 else
205 /* Add EXPR to end of this hash chain. */
206 last_expr->next_same_hash = cur_expr;
208 /* Set the fields of the expr element.
209 We must copy X because it can be modified when copy propagation is
210 performed on its operands. */
211 cur_expr->dest = copy_rtx (dest);
212 cur_expr->src = copy_rtx (src);
213 cur_expr->bitmap_index = table->n_elems++;
214 cur_expr->next_same_hash = NULL;
215 cur_expr->avail_occr = NULL;
218 /* Now record the occurrence. */
219 cur_occr = cur_expr->avail_occr;
221 if (cur_occr
222 && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
224 /* Found another instance of the expression in the same basic block.
225 Prefer this occurrence to the currently recorded one. We want
226 the last one in the block and the block is scanned from start
227 to end. */
228 cur_occr->insn = insn;
230 else
232 /* First occurrence of this expression in this basic block. */
233 cur_occr = GOBNEW (struct cprop_occr);
234 bytes_used += sizeof (struct cprop_occr);
235 cur_occr->insn = insn;
236 cur_occr->next = cur_expr->avail_occr;
237 cur_expr->avail_occr = cur_occr;
240 /* Record bitmap_index of the implicit set in implicit_set_indexes. */
241 if (implicit)
242 implicit_set_indexes[BLOCK_FOR_INSN (insn)->index]
243 = cur_expr->bitmap_index;
246 /* Determine whether the rtx X should be treated as a constant for CPROP.
247 Since X might be inserted more than once we have to take care that it
248 is sharable. */
250 static bool
251 cprop_constant_p (const_rtx x)
253 return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
256 /* Determine whether the rtx X should be treated as a register that can
257 be propagated. Any pseudo-register is fine. */
259 static bool
260 cprop_reg_p (const_rtx x)
262 return REG_P (x) && !HARD_REGISTER_P (x);
265 /* Scan SET present in INSN and add an entry to the hash TABLE.
266 IMPLICIT is true if it's an implicit set, false otherwise. */
268 static void
269 hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table,
270 bool implicit)
272 rtx src = SET_SRC (set);
273 rtx dest = SET_DEST (set);
275 if (cprop_reg_p (dest)
276 && reg_available_p (dest, insn)
277 && can_copy_p (GET_MODE (dest)))
279 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
281 This allows us to do a single CPROP pass and still eliminate
282 redundant constants, addresses or other expressions that are
283 constructed with multiple instructions.
285 However, keep the original SRC if INSN is a simple reg-reg move. In
286 In this case, there will almost always be a REG_EQUAL note on the
287 insn that sets SRC. By recording the REG_EQUAL value here as SRC
288 for INSN, we miss copy propagation opportunities.
290 Note that this does not impede profitable constant propagations. We
291 "look through" reg-reg sets in lookup_set. */
292 rtx note = find_reg_equal_equiv_note (insn);
293 if (note != 0
294 && REG_NOTE_KIND (note) == REG_EQUAL
295 && !REG_P (src)
296 && cprop_constant_p (XEXP (note, 0)))
297 src = XEXP (note, 0), set = gen_rtx_SET (dest, src);
299 /* Record sets for constant/copy propagation. */
300 if ((cprop_reg_p (src)
301 && src != dest
302 && reg_available_p (src, insn))
303 || cprop_constant_p (src))
304 insert_set_in_table (dest, src, insn, table, implicit);
308 /* Process INSN and add hash table entries as appropriate. */
310 static void
311 hash_scan_insn (rtx_insn *insn, struct hash_table_d *table)
313 rtx pat = PATTERN (insn);
314 int i;
316 /* Pick out the sets of INSN and for other forms of instructions record
317 what's been modified. */
319 if (GET_CODE (pat) == SET)
320 hash_scan_set (pat, insn, table, false);
321 else if (GET_CODE (pat) == PARALLEL)
322 for (i = 0; i < XVECLEN (pat, 0); i++)
324 rtx x = XVECEXP (pat, 0, i);
326 if (GET_CODE (x) == SET)
327 hash_scan_set (x, insn, table, false);
331 /* Dump the hash table TABLE to file FILE under the name NAME. */
333 static void
334 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
336 int i;
337 /* Flattened out table, so it's printed in proper order. */
338 struct cprop_expr **flat_table;
339 unsigned int *hash_val;
340 struct cprop_expr *expr;
342 flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems);
343 hash_val = XNEWVEC (unsigned int, table->n_elems);
345 for (i = 0; i < (int) table->size; i++)
346 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
348 flat_table[expr->bitmap_index] = expr;
349 hash_val[expr->bitmap_index] = i;
352 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
353 name, table->size, table->n_elems);
355 for (i = 0; i < (int) table->n_elems; i++)
356 if (flat_table[i] != 0)
358 expr = flat_table[i];
359 fprintf (file, "Index %d (hash value %d)\n ",
360 expr->bitmap_index, hash_val[i]);
361 print_rtl (file, expr->dest);
362 fprintf (file, " := ");
363 print_rtl (file, expr->src);
364 fprintf (file, "\n");
367 fprintf (file, "\n");
369 free (flat_table);
370 free (hash_val);
373 /* Record as unavailable all registers that are DEF operands of INSN. */
375 static void
376 make_set_regs_unavailable (rtx_insn *insn)
378 df_ref def;
380 FOR_EACH_INSN_DEF (def, insn)
381 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
384 /* Top level function to create an assignment hash table.
386 Assignment entries are placed in the hash table if
387 - they are of the form (set (pseudo-reg) src),
388 - src is something we want to perform const/copy propagation on,
389 - none of the operands or target are subsequently modified in the block
391 Currently src must be a pseudo-reg or a const_int.
393 TABLE is the table computed. */
395 static void
396 compute_hash_table_work (struct hash_table_d *table)
398 basic_block bb;
400 /* Allocate vars to track sets of regs. */
401 reg_set_bitmap = ALLOC_REG_SET (NULL);
403 FOR_EACH_BB_FN (bb, cfun)
405 rtx_insn *insn;
407 /* Reset tables used to keep track of what's not yet invalid [since
408 the end of the block]. */
409 CLEAR_REG_SET (reg_set_bitmap);
411 /* Go over all insns from the last to the first. This is convenient
412 for tracking available registers, i.e. not set between INSN and
413 the end of the basic block BB. */
414 FOR_BB_INSNS_REVERSE (bb, insn)
416 /* Only real insns are interesting. */
417 if (!NONDEBUG_INSN_P (insn))
418 continue;
420 /* Record interesting sets from INSN in the hash table. */
421 hash_scan_insn (insn, table);
423 /* Any registers set in INSN will make SETs above it not AVAIL. */
424 make_set_regs_unavailable (insn);
427 /* Insert implicit sets in the hash table, pretending they appear as
428 insns at the head of the basic block. */
429 if (implicit_sets[bb->index] != NULL_RTX)
430 hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
433 FREE_REG_SET (reg_set_bitmap);
436 /* Allocate space for the set/expr hash TABLE.
437 It is used to determine the number of buckets to use. */
439 static void
440 alloc_hash_table (struct hash_table_d *table)
442 int n;
444 n = get_max_insn_count ();
446 table->size = n / 4;
447 if (table->size < 11)
448 table->size = 11;
450 /* Attempt to maintain efficient use of hash table.
451 Making it an odd number is simplest for now.
452 ??? Later take some measurements. */
453 table->size |= 1;
454 n = table->size * sizeof (struct cprop_expr *);
455 table->table = XNEWVAR (struct cprop_expr *, n);
458 /* Free things allocated by alloc_hash_table. */
460 static void
461 free_hash_table (struct hash_table_d *table)
463 free (table->table);
466 /* Compute the hash TABLE for doing copy/const propagation or
467 expression hash table. */
469 static void
470 compute_hash_table (struct hash_table_d *table)
472 /* Initialize count of number of entries in hash table. */
473 table->n_elems = 0;
474 memset (table->table, 0, table->size * sizeof (struct cprop_expr *));
476 compute_hash_table_work (table);
479 /* Expression tracking support. */
481 /* Lookup REGNO in the set TABLE. The result is a pointer to the
482 table entry, or NULL if not found. */
484 static struct cprop_expr *
485 lookup_set (unsigned int regno, struct hash_table_d *table)
487 unsigned int hash = hash_mod (regno, table->size);
488 struct cprop_expr *expr;
490 expr = table->table[hash];
492 while (expr && REGNO (expr->dest) != regno)
493 expr = expr->next_same_hash;
495 return expr;
498 /* Return the next entry for REGNO in list EXPR. */
500 static struct cprop_expr *
501 next_set (unsigned int regno, struct cprop_expr *expr)
504 expr = expr->next_same_hash;
505 while (expr && REGNO (expr->dest) != regno);
507 return expr;
510 /* Reset tables used to keep track of what's still available [since the
511 start of the block]. */
513 static void
514 reset_opr_set_tables (void)
516 /* Maintain a bitmap of which regs have been set since beginning of
517 the block. */
518 CLEAR_REG_SET (reg_set_bitmap);
521 /* Return nonzero if the register X has not been set yet [since the
522 start of the basic block containing INSN]. */
524 static int
525 reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
527 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
530 /* Record things set by INSN.
531 This data is used by reg_not_set_p. */
533 static void
534 mark_oprs_set (rtx_insn *insn)
536 df_ref def;
538 FOR_EACH_INSN_DEF (def, insn)
539 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
542 /* Compute copy/constant propagation working variables. */
544 /* Local properties of assignments. */
545 static sbitmap *cprop_avloc;
546 static sbitmap *cprop_kill;
548 /* Global properties of assignments (computed from the local properties). */
549 static sbitmap *cprop_avin;
550 static sbitmap *cprop_avout;
552 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
553 basic blocks. N_SETS is the number of sets. */
555 static void
556 alloc_cprop_mem (int n_blocks, int n_sets)
558 cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
559 cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
561 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
562 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
565 /* Free vars used by copy/const propagation. */
567 static void
568 free_cprop_mem (void)
570 sbitmap_vector_free (cprop_avloc);
571 sbitmap_vector_free (cprop_kill);
572 sbitmap_vector_free (cprop_avin);
573 sbitmap_vector_free (cprop_avout);
576 /* Compute the local properties of each recorded expression.
578 Local properties are those that are defined by the block, irrespective of
579 other blocks.
581 An expression is killed in a block if its operands, either DEST or SRC, are
582 modified in the block.
584 An expression is computed (locally available) in a block if it is computed
585 at least once and expression would contain the same value if the
586 computation was moved to the end of the block.
588 KILL and COMP are destination sbitmaps for recording local properties. */
590 static void
591 compute_local_properties (sbitmap *kill, sbitmap *comp,
592 struct hash_table_d *table)
594 unsigned int i;
596 /* Initialize the bitmaps that were passed in. */
597 bitmap_vector_clear (kill, last_basic_block_for_fn (cfun));
598 bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
600 for (i = 0; i < table->size; i++)
602 struct cprop_expr *expr;
604 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
606 int indx = expr->bitmap_index;
607 df_ref def;
608 struct cprop_occr *occr;
610 /* For each definition of the destination pseudo-reg, the expression
611 is killed in the block where the definition is. */
612 for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
613 def; def = DF_REF_NEXT_REG (def))
614 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
616 /* If the source is a pseudo-reg, for each definition of the source,
617 the expression is killed in the block where the definition is. */
618 if (REG_P (expr->src))
619 for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
620 def; def = DF_REF_NEXT_REG (def))
621 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
623 /* The occurrences recorded in avail_occr are exactly those that
624 are locally available in the block where they are. */
625 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
627 bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
633 /* Hash table support. */
635 /* Top level routine to do the dataflow analysis needed by copy/const
636 propagation. */
638 static void
639 compute_cprop_data (void)
641 basic_block bb;
643 compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
644 compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
646 /* Merge implicit sets into CPROP_AVIN. They are always available at the
647 entry of their basic block. We need to do this because 1) implicit sets
648 aren't recorded for the local pass so they cannot be propagated within
649 their basic block by this pass and 2) the global pass would otherwise
650 propagate them only in the successors of their basic block. */
651 FOR_EACH_BB_FN (bb, cfun)
653 int index = implicit_set_indexes[bb->index];
654 if (index != -1)
655 bitmap_set_bit (cprop_avin[bb->index], index);
659 /* Copy/constant propagation. */
661 /* Maximum number of register uses in an insn that we handle. */
662 #define MAX_USES 8
664 /* Table of uses (registers, both hard and pseudo) found in an insn.
665 Allocated statically to avoid alloc/free complexity and overhead. */
666 static rtx reg_use_table[MAX_USES];
668 /* Index into `reg_use_table' while building it. */
669 static unsigned reg_use_count;
671 /* Set up a list of register numbers used in INSN. The found uses are stored
672 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
673 and contains the number of uses in the table upon exit.
675 ??? If a register appears multiple times we will record it multiple times.
676 This doesn't hurt anything but it will slow things down. */
678 static void
679 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
681 int i, j;
682 enum rtx_code code;
683 const char *fmt;
684 rtx x = *xptr;
686 /* repeat is used to turn tail-recursion into iteration since GCC
687 can't do it when there's no return value. */
688 repeat:
689 if (x == 0)
690 return;
692 code = GET_CODE (x);
693 if (REG_P (x))
695 if (reg_use_count == MAX_USES)
696 return;
698 reg_use_table[reg_use_count] = x;
699 reg_use_count++;
702 /* Recursively scan the operands of this expression. */
704 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
706 if (fmt[i] == 'e')
708 /* If we are about to do the last recursive call
709 needed at this level, change it into iteration.
710 This function is called enough to be worth it. */
711 if (i == 0)
713 x = XEXP (x, 0);
714 goto repeat;
717 find_used_regs (&XEXP (x, i), data);
719 else if (fmt[i] == 'E')
720 for (j = 0; j < XVECLEN (x, i); j++)
721 find_used_regs (&XVECEXP (x, i, j), data);
725 /* Try to replace all uses of FROM in INSN with TO.
726 Return nonzero if successful. */
728 static int
729 try_replace_reg (rtx from, rtx to, rtx_insn *insn)
731 rtx note = find_reg_equal_equiv_note (insn);
732 rtx src = 0;
733 int success = 0;
734 rtx set = single_set (insn);
736 bool check_rtx_costs = true;
737 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
738 int old_cost = set ? set_rtx_cost (set, speed) : 0;
740 if (!set
741 || CONSTANT_P (SET_SRC (set))
742 || (note != 0
743 && REG_NOTE_KIND (note) == REG_EQUAL
744 && (GET_CODE (XEXP (note, 0)) == CONST
745 || CONSTANT_P (XEXP (note, 0)))))
746 check_rtx_costs = false;
748 /* Usually we substitute easy stuff, so we won't copy everything.
749 We however need to take care to not duplicate non-trivial CONST
750 expressions. */
751 to = copy_rtx (to);
753 validate_replace_src_group (from, to, insn);
755 /* If TO is a constant, check the cost of the set after propagation
756 to the cost of the set before the propagation. If the cost is
757 higher, then do not replace FROM with TO. */
759 if (check_rtx_costs
760 && CONSTANT_P (to)
761 && set_rtx_cost (set, speed) > old_cost)
763 cancel_changes (0);
764 return false;
768 if (num_changes_pending () && apply_change_group ())
769 success = 1;
771 /* Try to simplify SET_SRC if we have substituted a constant. */
772 if (success && set && CONSTANT_P (to))
774 src = simplify_rtx (SET_SRC (set));
776 if (src)
777 validate_change (insn, &SET_SRC (set), src, 0);
780 /* If there is already a REG_EQUAL note, update the expression in it
781 with our replacement. */
782 if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
783 set_unique_reg_note (insn, REG_EQUAL,
784 simplify_replace_rtx (XEXP (note, 0), from, to));
785 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
787 /* If above failed and this is a single set, try to simplify the source
788 of the set given our substitution. We could perhaps try this for
789 multiple SETs, but it probably won't buy us anything. */
790 src = simplify_replace_rtx (SET_SRC (set), from, to);
792 if (!rtx_equal_p (src, SET_SRC (set))
793 && validate_change (insn, &SET_SRC (set), src, 0))
794 success = 1;
796 /* If we've failed perform the replacement, have a single SET to
797 a REG destination and don't yet have a note, add a REG_EQUAL note
798 to not lose information. */
799 if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
800 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
803 if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
805 /* Registers can also appear as uses in SET_DEST if it is a MEM.
806 We could perhaps try this for multiple SETs, but it probably
807 won't buy us anything. */
808 rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
810 if (!rtx_equal_p (dest, SET_DEST (set))
811 && validate_change (insn, &SET_DEST (set), dest, 0))
812 success = 1;
815 /* REG_EQUAL may get simplified into register.
816 We don't allow that. Remove that note. This code ought
817 not to happen, because previous code ought to synthesize
818 reg-reg move, but be on the safe side. */
819 if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
820 remove_note (insn, note);
822 return success;
825 /* Find a set of REGNOs that are available on entry to INSN's block. If found,
826 SET_RET[0] will be assigned a set with a register source and SET_RET[1] a
827 set with a constant source. If not found the corresponding entry is set to
828 NULL. */
830 static void
831 find_avail_set (int regno, rtx_insn *insn, struct cprop_expr *set_ret[2])
833 set_ret[0] = set_ret[1] = NULL;
835 /* Loops are not possible here. To get a loop we would need two sets
836 available at the start of the block containing INSN. i.e. we would
837 need two sets like this available at the start of the block:
839 (set (reg X) (reg Y))
840 (set (reg Y) (reg X))
842 This can not happen since the set of (reg Y) would have killed the
843 set of (reg X) making it unavailable at the start of this block. */
844 while (1)
846 rtx src;
847 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
849 /* Find a set that is available at the start of the block
850 which contains INSN. */
851 while (set)
853 if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
854 set->bitmap_index))
855 break;
856 set = next_set (regno, set);
859 /* If no available set was found we've reached the end of the
860 (possibly empty) copy chain. */
861 if (set == 0)
862 break;
864 src = set->src;
866 /* We know the set is available.
867 Now check that SRC is locally anticipatable (i.e. none of the
868 source operands have changed since the start of the block).
870 If the source operand changed, we may still use it for the next
871 iteration of this loop, but we may not use it for substitutions. */
873 if (cprop_constant_p (src))
874 set_ret[1] = set;
875 else if (reg_not_set_p (src, insn))
876 set_ret[0] = set;
878 /* If the source of the set is anything except a register, then
879 we have reached the end of the copy chain. */
880 if (! REG_P (src))
881 break;
883 /* Follow the copy chain, i.e. start another iteration of the loop
884 and see if we have an available copy into SRC. */
885 regno = REGNO (src);
889 /* Subroutine of cprop_insn that tries to propagate constants into
890 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
891 it is the instruction that immediately precedes JUMP, and must be a
892 single SET of a register. FROM is what we will try to replace,
893 SRC is the constant we will try to substitute for it. Return nonzero
894 if a change was made. */
896 static int
897 cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src)
899 rtx new_rtx, set_src, note_src;
900 rtx set = pc_set (jump);
901 rtx note = find_reg_equal_equiv_note (jump);
903 if (note)
905 note_src = XEXP (note, 0);
906 if (GET_CODE (note_src) == EXPR_LIST)
907 note_src = NULL_RTX;
909 else note_src = NULL_RTX;
911 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
912 set_src = note_src ? note_src : SET_SRC (set);
914 /* First substitute the SETCC condition into the JUMP instruction,
915 then substitute that given values into this expanded JUMP. */
916 if (setcc != NULL_RTX
917 && !modified_between_p (from, setcc, jump)
918 && !modified_between_p (src, setcc, jump))
920 rtx setcc_src;
921 rtx setcc_set = single_set (setcc);
922 rtx setcc_note = find_reg_equal_equiv_note (setcc);
923 setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
924 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
925 set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
926 setcc_src);
928 else
929 setcc = NULL;
931 new_rtx = simplify_replace_rtx (set_src, from, src);
933 /* If no simplification can be made, then try the next register. */
934 if (rtx_equal_p (new_rtx, SET_SRC (set)))
935 return 0;
937 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
938 if (new_rtx == pc_rtx)
939 delete_insn (jump);
940 else
942 /* Ensure the value computed inside the jump insn to be equivalent
943 to one computed by setcc. */
944 if (setcc && modified_in_p (new_rtx, setcc))
945 return 0;
946 if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
948 /* When (some) constants are not valid in a comparison, and there
949 are two registers to be replaced by constants before the entire
950 comparison can be folded into a constant, we need to keep
951 intermediate information in REG_EQUAL notes. For targets with
952 separate compare insns, such notes are added by try_replace_reg.
953 When we have a combined compare-and-branch instruction, however,
954 we need to attach a note to the branch itself to make this
955 optimization work. */
957 if (!rtx_equal_p (new_rtx, note_src))
958 set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
959 return 0;
962 /* Remove REG_EQUAL note after simplification. */
963 if (note_src)
964 remove_note (jump, note);
967 /* Delete the cc0 setter. */
968 if (HAVE_cc0 && setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
969 delete_insn (setcc);
971 global_const_prop_count++;
972 if (dump_file != NULL)
974 fprintf (dump_file,
975 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with"
976 "constant ", REGNO (from), INSN_UID (jump));
977 print_rtl (dump_file, src);
978 fprintf (dump_file, "\n");
980 purge_dead_edges (bb);
982 /* If a conditional jump has been changed into unconditional jump, remove
983 the jump and make the edge fallthru - this is always called in
984 cfglayout mode. */
985 if (new_rtx != pc_rtx && simplejump_p (jump))
987 edge e;
988 edge_iterator ei;
990 FOR_EACH_EDGE (e, ei, bb->succs)
991 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
992 && BB_HEAD (e->dest) == JUMP_LABEL (jump))
994 e->flags |= EDGE_FALLTHRU;
995 break;
997 delete_insn (jump);
1000 return 1;
1003 /* Subroutine of cprop_insn that tries to propagate constants. FROM is what
1004 we will try to replace, SRC is the constant we will try to substitute for
1005 it and INSN is the instruction where this will be happening. */
1007 static int
1008 constprop_register (rtx from, rtx src, rtx_insn *insn)
1010 rtx sset;
1012 /* Check for reg or cc0 setting instructions followed by
1013 conditional branch instructions first. */
1014 if ((sset = single_set (insn)) != NULL
1015 && NEXT_INSN (insn)
1016 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
1018 rtx dest = SET_DEST (sset);
1019 if ((REG_P (dest) || CC0_P (dest))
1020 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
1021 from, src))
1022 return 1;
1025 /* Handle normal insns next. */
1026 if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
1027 return 1;
1029 /* Try to propagate a CONST_INT into a conditional jump.
1030 We're pretty specific about what we will handle in this
1031 code, we can extend this as necessary over time.
1033 Right now the insn in question must look like
1034 (set (pc) (if_then_else ...)) */
1035 else if (any_condjump_p (insn) && onlyjump_p (insn))
1036 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
1037 return 0;
1040 /* Perform constant and copy propagation on INSN.
1041 Return nonzero if a change was made. */
1043 static int
1044 cprop_insn (rtx_insn *insn)
1046 unsigned i;
1047 int changed = 0, changed_this_round;
1048 rtx note;
1052 changed_this_round = 0;
1053 reg_use_count = 0;
1054 note_uses (&PATTERN (insn), find_used_regs, NULL);
1056 /* We may win even when propagating constants into notes. */
1057 note = find_reg_equal_equiv_note (insn);
1058 if (note)
1059 find_used_regs (&XEXP (note, 0), NULL);
1061 for (i = 0; i < reg_use_count; i++)
1063 rtx reg_used = reg_use_table[i];
1064 unsigned int regno = REGNO (reg_used);
1065 rtx src_cst = NULL, src_reg = NULL;
1066 struct cprop_expr *set[2];
1068 /* If the register has already been set in this block, there's
1069 nothing we can do. */
1070 if (! reg_not_set_p (reg_used, insn))
1071 continue;
1073 /* Find an assignment that sets reg_used and is available
1074 at the start of the block. */
1075 find_avail_set (regno, insn, set);
1076 if (set[0])
1077 src_reg = set[0]->src;
1078 if (set[1])
1079 src_cst = set[1]->src;
1081 /* Constant propagation. */
1082 if (src_cst && cprop_constant_p (src_cst)
1083 && constprop_register (reg_used, src_cst, insn))
1085 changed_this_round = changed = 1;
1086 global_const_prop_count++;
1087 if (dump_file != NULL)
1089 fprintf (dump_file,
1090 "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
1091 fprintf (dump_file, "insn %d with constant ",
1092 INSN_UID (insn));
1093 print_rtl (dump_file, src_cst);
1094 fprintf (dump_file, "\n");
1096 if (insn->deleted ())
1097 return 1;
1099 /* Copy propagation. */
1100 else if (src_reg && cprop_reg_p (src_reg)
1101 && REGNO (src_reg) != regno
1102 && try_replace_reg (reg_used, src_reg, insn))
1104 changed_this_round = changed = 1;
1105 global_copy_prop_count++;
1106 if (dump_file != NULL)
1108 fprintf (dump_file,
1109 "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1110 regno, INSN_UID (insn));
1111 fprintf (dump_file, " with reg %d\n", REGNO (src_reg));
1114 /* The original insn setting reg_used may or may not now be
1115 deletable. We leave the deletion to DCE. */
1116 /* FIXME: If it turns out that the insn isn't deletable,
1117 then we may have unnecessarily extended register lifetimes
1118 and made things worse. */
1122 /* If try_replace_reg simplified the insn, the regs found by find_used_regs
1123 may not be valid anymore. Start over. */
1124 while (changed_this_round);
1126 if (changed && DEBUG_INSN_P (insn))
1127 return 0;
1129 return changed;
1132 /* Like find_used_regs, but avoid recording uses that appear in
1133 input-output contexts such as zero_extract or pre_dec. This
1134 restricts the cases we consider to those for which local cprop
1135 can legitimately make replacements. */
1137 static void
1138 local_cprop_find_used_regs (rtx *xptr, void *data)
1140 rtx x = *xptr;
1142 if (x == 0)
1143 return;
1145 switch (GET_CODE (x))
1147 case ZERO_EXTRACT:
1148 case SIGN_EXTRACT:
1149 case STRICT_LOW_PART:
1150 return;
1152 case PRE_DEC:
1153 case PRE_INC:
1154 case POST_DEC:
1155 case POST_INC:
1156 case PRE_MODIFY:
1157 case POST_MODIFY:
1158 /* Can only legitimately appear this early in the context of
1159 stack pushes for function arguments, but handle all of the
1160 codes nonetheless. */
1161 return;
1163 case SUBREG:
1164 if (df_read_modify_subreg_p (x))
1165 return;
1166 break;
1168 default:
1169 break;
1172 find_used_regs (xptr, data);
1175 /* Try to perform local const/copy propagation on X in INSN. */
1177 static bool
1178 do_local_cprop (rtx x, rtx_insn *insn)
1180 rtx newreg = NULL, newcnst = NULL;
1182 /* Rule out USE instructions and ASM statements as we don't want to
1183 change the hard registers mentioned. */
1184 if (REG_P (x)
1185 && (cprop_reg_p (x)
1186 || (GET_CODE (PATTERN (insn)) != USE
1187 && asm_noperands (PATTERN (insn)) < 0)))
1189 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
1190 struct elt_loc_list *l;
1192 if (!val)
1193 return false;
1194 for (l = val->locs; l; l = l->next)
1196 rtx this_rtx = l->loc;
1197 rtx note;
1199 if (cprop_constant_p (this_rtx))
1200 newcnst = this_rtx;
1201 if (cprop_reg_p (this_rtx)
1202 /* Don't copy propagate if it has attached REG_EQUIV note.
1203 At this point this only function parameters should have
1204 REG_EQUIV notes and if the argument slot is used somewhere
1205 explicitly, it means address of parameter has been taken,
1206 so we should not extend the lifetime of the pseudo. */
1207 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
1208 || ! MEM_P (XEXP (note, 0))))
1209 newreg = this_rtx;
1211 if (newcnst && constprop_register (x, newcnst, insn))
1213 if (dump_file != NULL)
1215 fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
1216 REGNO (x));
1217 fprintf (dump_file, "insn %d with constant ",
1218 INSN_UID (insn));
1219 print_rtl (dump_file, newcnst);
1220 fprintf (dump_file, "\n");
1222 local_const_prop_count++;
1223 return true;
1225 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
1227 if (dump_file != NULL)
1229 fprintf (dump_file,
1230 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
1231 REGNO (x), INSN_UID (insn));
1232 fprintf (dump_file, " with reg %d\n", REGNO (newreg));
1234 local_copy_prop_count++;
1235 return true;
1238 return false;
1241 /* Do local const/copy propagation (i.e. within each basic block). */
1243 static int
1244 local_cprop_pass (void)
1246 basic_block bb;
1247 rtx_insn *insn;
1248 bool changed = false;
1249 unsigned i;
1251 cselib_init (0);
1252 FOR_EACH_BB_FN (bb, cfun)
1254 FOR_BB_INSNS (bb, insn)
1256 if (INSN_P (insn))
1258 rtx note = find_reg_equal_equiv_note (insn);
1261 reg_use_count = 0;
1262 note_uses (&PATTERN (insn), local_cprop_find_used_regs,
1263 NULL);
1264 if (note)
1265 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
1267 for (i = 0; i < reg_use_count; i++)
1269 if (do_local_cprop (reg_use_table[i], insn))
1271 if (!DEBUG_INSN_P (insn))
1272 changed = true;
1273 break;
1276 if (insn->deleted ())
1277 break;
1279 while (i < reg_use_count);
1281 cselib_process_insn (insn);
1284 /* Forget everything at the end of a basic block. */
1285 cselib_clear_table ();
1288 cselib_finish ();
1290 return changed;
1293 /* Similar to get_condition, only the resulting condition must be
1294 valid at JUMP, instead of at EARLIEST.
1296 This differs from noce_get_condition in ifcvt.c in that we prefer not to
1297 settle for the condition variable in the jump instruction being integral.
1298 We prefer to be able to record the value of a user variable, rather than
1299 the value of a temporary used in a condition. This could be solved by
1300 recording the value of *every* register scanned by canonicalize_condition,
1301 but this would require some code reorganization. */
1304 fis_get_condition (rtx_insn *jump)
1306 return get_condition (jump, NULL, false, true);
1309 /* Check the comparison COND to see if we can safely form an implicit
1310 set from it. */
1312 static bool
1313 implicit_set_cond_p (const_rtx cond)
1315 machine_mode mode;
1316 rtx cst;
1318 /* COND must be either an EQ or NE comparison. */
1319 if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
1320 return false;
1322 /* The first operand of COND must be a register we can propagate. */
1323 if (!cprop_reg_p (XEXP (cond, 0)))
1324 return false;
1326 /* The second operand of COND must be a suitable constant. */
1327 mode = GET_MODE (XEXP (cond, 0));
1328 cst = XEXP (cond, 1);
1330 /* We can't perform this optimization if either operand might be or might
1331 contain a signed zero. */
1332 if (HONOR_SIGNED_ZEROS (mode))
1334 /* It is sufficient to check if CST is or contains a zero. We must
1335 handle float, complex, and vector. If any subpart is a zero, then
1336 the optimization can't be performed. */
1337 /* ??? The complex and vector checks are not implemented yet. We just
1338 always return zero for them. */
1339 if (CONST_DOUBLE_AS_FLOAT_P (cst)
1340 && real_equal (CONST_DOUBLE_REAL_VALUE (cst), &dconst0))
1341 return 0;
1342 else
1343 return 0;
1346 return cprop_constant_p (cst);
1349 /* Find the implicit sets of a function. An "implicit set" is a constraint
1350 on the value of a variable, implied by a conditional jump. For example,
1351 following "if (x == 2)", the then branch may be optimized as though the
1352 conditional performed an "explicit set", in this example, "x = 2". This
1353 function records the set patterns that are implicit at the start of each
1354 basic block.
1356 If an implicit set is found but the set is implicit on a critical edge,
1357 this critical edge is split.
1359 Return true if the CFG was modified, false otherwise. */
1361 static bool
1362 find_implicit_sets (void)
1364 basic_block bb, dest;
1365 rtx cond, new_rtx;
1366 unsigned int count = 0;
1367 bool edges_split = false;
1368 size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10;
1370 implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1372 FOR_EACH_BB_FN (bb, cfun)
1374 /* Check for more than one successor. */
1375 if (EDGE_COUNT (bb->succs) <= 1)
1376 continue;
1378 cond = fis_get_condition (BB_END (bb));
1380 /* If no condition is found or if it isn't of a suitable form,
1381 ignore it. */
1382 if (! cond || ! implicit_set_cond_p (cond))
1383 continue;
1385 dest = GET_CODE (cond) == EQ
1386 ? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
1388 /* If DEST doesn't go anywhere, ignore it. */
1389 if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1390 continue;
1392 /* We have found a suitable implicit set. Try to record it now as
1393 a SET in DEST. If DEST has more than one predecessor, the edge
1394 between BB and DEST is a critical edge and we must split it,
1395 because we can only record one implicit set per DEST basic block. */
1396 if (! single_pred_p (dest))
1398 dest = split_edge (find_edge (bb, dest));
1399 edges_split = true;
1402 if (implicit_sets_size <= (size_t) dest->index)
1404 size_t old_implicit_sets_size = implicit_sets_size;
1405 implicit_sets_size *= 2;
1406 implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
1407 memset (implicit_sets + old_implicit_sets_size, 0,
1408 (implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1411 new_rtx = gen_rtx_SET (XEXP (cond, 0), XEXP (cond, 1));
1412 implicit_sets[dest->index] = new_rtx;
1413 if (dump_file)
1415 fprintf (dump_file, "Implicit set of reg %d in ",
1416 REGNO (XEXP (cond, 0)));
1417 fprintf (dump_file, "basic block %d\n", dest->index);
1419 count++;
1422 if (dump_file)
1423 fprintf (dump_file, "Found %d implicit sets\n", count);
1425 /* Confess our sins. */
1426 return edges_split;
1429 /* Bypass conditional jumps. */
1431 /* The value of last_basic_block at the beginning of the jump_bypass
1432 pass. The use of redirect_edge_and_branch_force may introduce new
1433 basic blocks, but the data flow analysis is only valid for basic
1434 block indices less than bypass_last_basic_block. */
1436 static int bypass_last_basic_block;
1438 /* Find a set of REGNO to a constant that is available at the end of basic
1439 block BB. Return NULL if no such set is found. Based heavily upon
1440 find_avail_set. */
1442 static struct cprop_expr *
1443 find_bypass_set (int regno, int bb)
1445 struct cprop_expr *result = 0;
1447 for (;;)
1449 rtx src;
1450 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
1452 while (set)
1454 if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
1455 break;
1456 set = next_set (regno, set);
1459 if (set == 0)
1460 break;
1462 src = set->src;
1463 if (cprop_constant_p (src))
1464 result = set;
1466 if (! REG_P (src))
1467 break;
1469 regno = REGNO (src);
1471 return result;
1474 /* Subroutine of bypass_block that checks whether a pseudo is killed by
1475 any of the instructions inserted on an edge. Jump bypassing places
1476 condition code setters on CFG edges using insert_insn_on_edge. This
1477 function is required to check that our data flow analysis is still
1478 valid prior to commit_edge_insertions. */
1480 static bool
1481 reg_killed_on_edge (const_rtx reg, const_edge e)
1483 rtx_insn *insn;
1485 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
1486 if (INSN_P (insn) && reg_set_p (reg, insn))
1487 return true;
1489 return false;
1492 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
1493 basic block BB which has more than one predecessor. If not NULL, SETCC
1494 is the first instruction of BB, which is immediately followed by JUMP_INSN
1495 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
1496 Returns nonzero if a change was made.
1498 During the jump bypassing pass, we may place copies of SETCC instructions
1499 on CFG edges. The following routine must be careful to pay attention to
1500 these inserted insns when performing its transformations. */
1502 static int
1503 bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump)
1505 rtx_insn *insn;
1506 rtx note;
1507 edge e, edest;
1508 int change;
1509 int may_be_loop_header = false;
1510 unsigned removed_p;
1511 unsigned i;
1512 edge_iterator ei;
1514 insn = (setcc != NULL) ? setcc : jump;
1516 /* Determine set of register uses in INSN. */
1517 reg_use_count = 0;
1518 note_uses (&PATTERN (insn), find_used_regs, NULL);
1519 note = find_reg_equal_equiv_note (insn);
1520 if (note)
1521 find_used_regs (&XEXP (note, 0), NULL);
1523 if (current_loops)
1525 /* If we are to preserve loop structure then do not bypass
1526 a loop header. This will either rotate the loop, create
1527 multiple entry loops or even irreducible regions. */
1528 if (bb == bb->loop_father->header)
1529 return 0;
1531 else
1533 FOR_EACH_EDGE (e, ei, bb->preds)
1534 if (e->flags & EDGE_DFS_BACK)
1536 may_be_loop_header = true;
1537 break;
1541 change = 0;
1542 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1544 removed_p = 0;
1546 if (e->flags & EDGE_COMPLEX)
1548 ei_next (&ei);
1549 continue;
1552 /* We can't redirect edges from new basic blocks. */
1553 if (e->src->index >= bypass_last_basic_block)
1555 ei_next (&ei);
1556 continue;
1559 /* The irreducible loops created by redirecting of edges entering the
1560 loop from outside would decrease effectiveness of some of the
1561 following optimizations, so prevent this. */
1562 if (may_be_loop_header
1563 && !(e->flags & EDGE_DFS_BACK))
1565 ei_next (&ei);
1566 continue;
1569 for (i = 0; i < reg_use_count; i++)
1571 rtx reg_used = reg_use_table[i];
1572 unsigned int regno = REGNO (reg_used);
1573 basic_block dest, old_dest;
1574 struct cprop_expr *set;
1575 rtx src, new_rtx;
1577 set = find_bypass_set (regno, e->src->index);
1579 if (! set)
1580 continue;
1582 /* Check the data flow is valid after edge insertions. */
1583 if (e->insns.r && reg_killed_on_edge (reg_used, e))
1584 continue;
1586 src = SET_SRC (pc_set (jump));
1588 if (setcc != NULL)
1589 src = simplify_replace_rtx (src,
1590 SET_DEST (PATTERN (setcc)),
1591 SET_SRC (PATTERN (setcc)));
1593 new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1595 /* Jump bypassing may have already placed instructions on
1596 edges of the CFG. We can't bypass an outgoing edge that
1597 has instructions associated with it, as these insns won't
1598 get executed if the incoming edge is redirected. */
1599 if (new_rtx == pc_rtx)
1601 edest = FALLTHRU_EDGE (bb);
1602 dest = edest->insns.r ? NULL : edest->dest;
1604 else if (GET_CODE (new_rtx) == LABEL_REF)
1606 dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
1607 /* Don't bypass edges containing instructions. */
1608 edest = find_edge (bb, dest);
1609 if (edest && edest->insns.r)
1610 dest = NULL;
1612 else
1613 dest = NULL;
1615 /* Avoid unification of the edge with other edges from original
1616 branch. We would end up emitting the instruction on "both"
1617 edges. */
1618 if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
1619 && find_edge (e->src, dest))
1620 dest = NULL;
1622 old_dest = e->dest;
1623 if (dest != NULL
1624 && dest != old_dest
1625 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1627 redirect_edge_and_branch_force (e, dest);
1629 /* Copy the register setter to the redirected edge.
1630 Don't copy CC0 setters, as CC0 is dead after jump. */
1631 if (setcc)
1633 rtx pat = PATTERN (setcc);
1634 if (!CC0_P (SET_DEST (pat)))
1635 insert_insn_on_edge (copy_insn (pat), e);
1638 if (dump_file != NULL)
1640 fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
1641 "in jump_insn %d equals constant ",
1642 regno, INSN_UID (jump));
1643 print_rtl (dump_file, set->src);
1644 fprintf (dump_file, "\n\t when BB %d is entered from "
1645 "BB %d. Redirect edge %d->%d to %d.\n",
1646 old_dest->index, e->src->index, e->src->index,
1647 old_dest->index, dest->index);
1649 change = 1;
1650 removed_p = 1;
1651 break;
1654 if (!removed_p)
1655 ei_next (&ei);
1657 return change;
1660 /* Find basic blocks with more than one predecessor that only contain a
1661 single conditional jump. If the result of the comparison is known at
1662 compile-time from any incoming edge, redirect that edge to the
1663 appropriate target. Return nonzero if a change was made.
1665 This function is now mis-named, because we also handle indirect jumps. */
1667 static int
1668 bypass_conditional_jumps (void)
1670 basic_block bb;
1671 int changed;
1672 rtx_insn *setcc;
1673 rtx_insn *insn;
1674 rtx dest;
1676 /* Note we start at block 1. */
1677 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1678 return 0;
1680 bypass_last_basic_block = last_basic_block_for_fn (cfun);
1681 mark_dfs_back_edges ();
1683 changed = 0;
1684 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1685 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1687 /* Check for more than one predecessor. */
1688 if (!single_pred_p (bb))
1690 setcc = NULL;
1691 FOR_BB_INSNS (bb, insn)
1692 if (DEBUG_INSN_P (insn))
1693 continue;
1694 else if (NONJUMP_INSN_P (insn))
1696 if (setcc)
1697 break;
1698 if (GET_CODE (PATTERN (insn)) != SET)
1699 break;
1701 dest = SET_DEST (PATTERN (insn));
1702 if (REG_P (dest) || CC0_P (dest))
1703 setcc = insn;
1704 else
1705 break;
1707 else if (JUMP_P (insn))
1709 if ((any_condjump_p (insn) || computed_jump_p (insn))
1710 && onlyjump_p (insn))
1711 changed |= bypass_block (bb, setcc, insn);
1712 break;
1714 else if (INSN_P (insn))
1715 break;
1719 /* If we bypassed any register setting insns, we inserted a
1720 copy on the redirected edge. These need to be committed. */
1721 if (changed)
1722 commit_edge_insertions ();
1724 return changed;
1727 /* Main function for the CPROP pass. */
1729 static int
1730 one_cprop_pass (void)
1732 int i;
1733 int changed = 0;
1735 /* Return if there's nothing to do, or it is too expensive. */
1736 if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
1737 || gcse_or_cprop_is_too_expensive (_ ("const/copy propagation disabled")))
1738 return 0;
1740 global_const_prop_count = local_const_prop_count = 0;
1741 global_copy_prop_count = local_copy_prop_count = 0;
1743 bytes_used = 0;
1744 gcc_obstack_init (&cprop_obstack);
1746 /* Do a local const/copy propagation pass first. The global pass
1747 only handles global opportunities.
1748 If the local pass changes something, remove any unreachable blocks
1749 because the CPROP global dataflow analysis may get into infinite
1750 loops for CFGs with unreachable blocks.
1752 FIXME: This local pass should not be necessary after CSE (but for
1753 some reason it still is). It is also (proven) not necessary
1754 to run the local pass right after FWPWOP.
1756 FIXME: The global analysis would not get into infinite loops if it
1757 would use the DF solver (via df_simple_dataflow) instead of
1758 the solver implemented in this file. */
1759 changed |= local_cprop_pass ();
1760 if (changed)
1761 delete_unreachable_blocks ();
1763 /* Determine implicit sets. This may change the CFG (split critical
1764 edges if that exposes an implicit set).
1765 Note that find_implicit_sets() does not rely on up-to-date DF caches
1766 so that we do not have to re-run df_analyze() even if local CPROP
1767 changed something.
1768 ??? This could run earlier so that any uncovered implicit sets
1769 sets could be exploited in local_cprop_pass() also. Later. */
1770 changed |= find_implicit_sets ();
1772 /* If local_cprop_pass() or find_implicit_sets() changed something,
1773 run df_analyze() to bring all insn caches up-to-date, and to take
1774 new basic blocks from edge splitting on the DF radar.
1775 NB: This also runs the fast DCE pass, because execute_rtl_cprop
1776 sets DF_LR_RUN_DCE. */
1777 if (changed)
1778 df_analyze ();
1780 /* Initialize implicit_set_indexes array. */
1781 implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun));
1782 for (i = 0; i < last_basic_block_for_fn (cfun); i++)
1783 implicit_set_indexes[i] = -1;
1785 alloc_hash_table (&set_hash_table);
1786 compute_hash_table (&set_hash_table);
1788 /* Free implicit_sets before peak usage. */
1789 free (implicit_sets);
1790 implicit_sets = NULL;
1792 if (dump_file)
1793 dump_hash_table (dump_file, "SET", &set_hash_table);
1794 if (set_hash_table.n_elems > 0)
1796 basic_block bb;
1797 rtx_insn *insn;
1799 alloc_cprop_mem (last_basic_block_for_fn (cfun),
1800 set_hash_table.n_elems);
1801 compute_cprop_data ();
1803 free (implicit_set_indexes);
1804 implicit_set_indexes = NULL;
1806 /* Allocate vars to track sets of regs. */
1807 reg_set_bitmap = ALLOC_REG_SET (NULL);
1809 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1810 EXIT_BLOCK_PTR_FOR_FN (cfun),
1811 next_bb)
1813 /* Reset tables used to keep track of what's still valid [since
1814 the start of the block]. */
1815 reset_opr_set_tables ();
1817 FOR_BB_INSNS (bb, insn)
1818 if (INSN_P (insn))
1820 changed |= cprop_insn (insn);
1822 /* Keep track of everything modified by this insn. */
1823 /* ??? Need to be careful w.r.t. mods done to INSN.
1824 Don't call mark_oprs_set if we turned the
1825 insn into a NOTE, or deleted the insn. */
1826 if (! NOTE_P (insn) && ! insn->deleted ())
1827 mark_oprs_set (insn);
1831 changed |= bypass_conditional_jumps ();
1833 FREE_REG_SET (reg_set_bitmap);
1834 free_cprop_mem ();
1836 else
1838 free (implicit_set_indexes);
1839 implicit_set_indexes = NULL;
1842 free_hash_table (&set_hash_table);
1843 obstack_free (&cprop_obstack, NULL);
1845 if (dump_file)
1847 fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1848 current_function_name (), n_basic_blocks_for_fn (cfun),
1849 bytes_used);
1850 fprintf (dump_file, "%d local const props, %d local copy props, ",
1851 local_const_prop_count, local_copy_prop_count);
1852 fprintf (dump_file, "%d global const props, %d global copy props\n\n",
1853 global_const_prop_count, global_copy_prop_count);
1856 return changed;
1859 /* All the passes implemented in this file. Each pass has its
1860 own gate and execute function, and at the end of the file a
1861 pass definition for passes.c.
1863 We do not construct an accurate cfg in functions which call
1864 setjmp, so none of these passes runs if the function calls
1865 setjmp.
1866 FIXME: Should just handle setjmp via REG_SETJMP notes. */
1868 static unsigned int
1869 execute_rtl_cprop (void)
1871 int changed;
1872 delete_unreachable_blocks ();
1873 df_set_flags (DF_LR_RUN_DCE);
1874 df_analyze ();
1875 changed = one_cprop_pass ();
1876 flag_rerun_cse_after_global_opts |= changed;
1877 if (changed)
1878 cleanup_cfg (CLEANUP_CFG_CHANGED);
1879 return 0;
1882 namespace {
1884 const pass_data pass_data_rtl_cprop =
1886 RTL_PASS, /* type */
1887 "cprop", /* name */
1888 OPTGROUP_NONE, /* optinfo_flags */
1889 TV_CPROP, /* tv_id */
1890 PROP_cfglayout, /* properties_required */
1891 0, /* properties_provided */
1892 0, /* properties_destroyed */
1893 0, /* todo_flags_start */
1894 TODO_df_finish, /* todo_flags_finish */
1897 class pass_rtl_cprop : public rtl_opt_pass
1899 public:
1900 pass_rtl_cprop (gcc::context *ctxt)
1901 : rtl_opt_pass (pass_data_rtl_cprop, ctxt)
1904 /* opt_pass methods: */
1905 opt_pass * clone () { return new pass_rtl_cprop (m_ctxt); }
1906 virtual bool gate (function *fun)
1908 return optimize > 0 && flag_gcse
1909 && !fun->calls_setjmp
1910 && dbg_cnt (cprop);
1913 virtual unsigned int execute (function *) { return execute_rtl_cprop (); }
1915 }; // class pass_rtl_cprop
1917 } // anon namespace
1919 rtl_opt_pass *
1920 make_pass_rtl_cprop (gcc::context *ctxt)
1922 return new pass_rtl_cprop (ctxt);