[PATCH] [PR testsuite/67959]Minor cleanup for ssa-thread-13.c
[official-gcc.git] / gcc / cprop.c
blob4cb8586902ab2351870cb2a5be642b1021a81eed
1 /* Global constant/copy propagation for RTL.
2 Copyright (C) 1997-2015 Free Software Foundation, Inc.
4 This file is part of GCC.
6 GCC is free software; you can redistribute it and/or modify it under
7 the terms of the GNU General Public License as published by the Free
8 Software Foundation; either version 3, or (at your option) any later
9 version.
11 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
12 WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 for more details.
16 You should have received a copy of the GNU General Public License
17 along with GCC; see the file COPYING3. If not see
18 <http://www.gnu.org/licenses/>. */
20 #include "config.h"
21 #include "system.h"
22 #include "coretypes.h"
23 #include "backend.h"
24 #include "cfghooks.h"
25 #include "tree.h"
26 #include "rtl.h"
27 #include "df.h"
28 #include "diagnostic-core.h"
29 #include "toplev.h"
30 #include "alias.h"
31 #include "tm_p.h"
32 #include "regs.h"
33 #include "flags.h"
34 #include "insn-config.h"
35 #include "recog.h"
36 #include "cfgrtl.h"
37 #include "cfganal.h"
38 #include "lcm.h"
39 #include "cfgcleanup.h"
40 #include "expmed.h"
41 #include "dojump.h"
42 #include "explow.h"
43 #include "calls.h"
44 #include "emit-rtl.h"
45 #include "varasm.h"
46 #include "stmt.h"
47 #include "expr.h"
48 #include "except.h"
49 #include "params.h"
50 #include "alloc-pool.h"
51 #include "cselib.h"
52 #include "intl.h"
53 #include "tree-pass.h"
54 #include "dbgcnt.h"
55 #include "target.h"
56 #include "cfgloop.h"
59 /* An obstack for our working variables. */
60 static struct obstack cprop_obstack;
62 /* Occurrence of an expression.
63 There is one per basic block. If a pattern appears more than once the
64 last appearance is used. */
66 struct cprop_occr
68 /* Next occurrence of this expression. */
69 struct cprop_occr *next;
70 /* The insn that computes the expression. */
71 rtx_insn *insn;
74 /* Hash table entry for assignment expressions. */
76 struct cprop_expr
78 /* The expression (DEST := SRC). */
79 rtx dest;
80 rtx src;
82 /* Index in the available expression bitmaps. */
83 int bitmap_index;
84 /* Next entry with the same hash. */
85 struct cprop_expr *next_same_hash;
86 /* List of available occurrence in basic blocks in the function.
87 An "available occurrence" is one that is the last occurrence in the
88 basic block and whose operands are not modified by following statements
89 in the basic block [including this insn]. */
90 struct cprop_occr *avail_occr;
93 /* Hash table for copy propagation expressions.
94 Each hash table is an array of buckets.
95 ??? It is known that if it were an array of entries, structure elements
96 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
97 not clear whether in the final analysis a sufficient amount of memory would
98 be saved as the size of the available expression bitmaps would be larger
99 [one could build a mapping table without holes afterwards though].
100 Someday I'll perform the computation and figure it out. */
102 struct hash_table_d
104 /* The table itself.
105 This is an array of `set_hash_table_size' elements. */
106 struct cprop_expr **table;
108 /* Size of the hash table, in elements. */
109 unsigned int size;
111 /* Number of hash table elements. */
112 unsigned int n_elems;
115 /* Copy propagation hash table. */
116 static struct hash_table_d set_hash_table;
118 /* Array of implicit set patterns indexed by basic block index. */
119 static rtx *implicit_sets;
121 /* Array of indexes of expressions for implicit set patterns indexed by basic
122 block index. In other words, implicit_set_indexes[i] is the bitmap_index
123 of the expression whose RTX is implicit_sets[i]. */
124 static int *implicit_set_indexes;
126 /* Bitmap containing one bit for each register in the program.
127 Used when performing GCSE to track which registers have been set since
128 the start or end of the basic block while traversing that block. */
129 static regset reg_set_bitmap;
131 /* Various variables for statistics gathering. */
133 /* Memory used in a pass.
134 This isn't intended to be absolutely precise. Its intent is only
135 to keep an eye on memory usage. */
136 static int bytes_used;
138 /* Number of local constants propagated. */
139 static int local_const_prop_count;
140 /* Number of local copies propagated. */
141 static int local_copy_prop_count;
142 /* Number of global constants propagated. */
143 static int global_const_prop_count;
144 /* Number of global copies propagated. */
145 static int global_copy_prop_count;
147 #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T)))
148 #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S)))
150 /* Cover function to obstack_alloc. */
152 static void *
153 cprop_alloc (unsigned long size)
155 bytes_used += size;
156 return obstack_alloc (&cprop_obstack, size);
159 /* Return nonzero if register X is unchanged from INSN to the end
160 of INSN's basic block. */
162 static int
163 reg_available_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
165 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
168 /* Hash a set of register REGNO.
170 Sets are hashed on the register that is set. This simplifies the PRE copy
171 propagation code.
173 ??? May need to make things more elaborate. Later, as necessary. */
175 static unsigned int
176 hash_mod (int regno, int hash_table_size)
178 return (unsigned) regno % hash_table_size;
181 /* Insert assignment DEST:=SET from INSN in the hash table.
182 DEST is a register and SET is a register or a suitable constant.
183 If the assignment is already present in the table, record it as
184 the last occurrence in INSN's basic block.
185 IMPLICIT is true if it's an implicit set, false otherwise. */
187 static void
188 insert_set_in_table (rtx dest, rtx src, rtx_insn *insn,
189 struct hash_table_d *table, bool implicit)
191 bool found = false;
192 unsigned int hash;
193 struct cprop_expr *cur_expr, *last_expr = NULL;
194 struct cprop_occr *cur_occr;
196 hash = hash_mod (REGNO (dest), table->size);
198 for (cur_expr = table->table[hash]; cur_expr;
199 cur_expr = cur_expr->next_same_hash)
201 if (dest == cur_expr->dest
202 && src == cur_expr->src)
204 found = true;
205 break;
207 last_expr = cur_expr;
210 if (! found)
212 cur_expr = GOBNEW (struct cprop_expr);
213 bytes_used += sizeof (struct cprop_expr);
214 if (table->table[hash] == NULL)
215 /* This is the first pattern that hashed to this index. */
216 table->table[hash] = cur_expr;
217 else
218 /* Add EXPR to end of this hash chain. */
219 last_expr->next_same_hash = cur_expr;
221 /* Set the fields of the expr element.
222 We must copy X because it can be modified when copy propagation is
223 performed on its operands. */
224 cur_expr->dest = copy_rtx (dest);
225 cur_expr->src = copy_rtx (src);
226 cur_expr->bitmap_index = table->n_elems++;
227 cur_expr->next_same_hash = NULL;
228 cur_expr->avail_occr = NULL;
231 /* Now record the occurrence. */
232 cur_occr = cur_expr->avail_occr;
234 if (cur_occr
235 && BLOCK_FOR_INSN (cur_occr->insn) == BLOCK_FOR_INSN (insn))
237 /* Found another instance of the expression in the same basic block.
238 Prefer this occurrence to the currently recorded one. We want
239 the last one in the block and the block is scanned from start
240 to end. */
241 cur_occr->insn = insn;
243 else
245 /* First occurrence of this expression in this basic block. */
246 cur_occr = GOBNEW (struct cprop_occr);
247 bytes_used += sizeof (struct cprop_occr);
248 cur_occr->insn = insn;
249 cur_occr->next = cur_expr->avail_occr;
250 cur_expr->avail_occr = cur_occr;
253 /* Record bitmap_index of the implicit set in implicit_set_indexes. */
254 if (implicit)
255 implicit_set_indexes[BLOCK_FOR_INSN (insn)->index]
256 = cur_expr->bitmap_index;
259 /* Determine whether the rtx X should be treated as a constant for CPROP.
260 Since X might be inserted more than once we have to take care that it
261 is sharable. */
263 static bool
264 cprop_constant_p (const_rtx x)
266 return CONSTANT_P (x) && (GET_CODE (x) != CONST || shared_const_p (x));
269 /* Determine whether the rtx X should be treated as a register that can
270 be propagated. Any pseudo-register is fine. */
272 static bool
273 cprop_reg_p (const_rtx x)
275 return REG_P (x) && !HARD_REGISTER_P (x);
278 /* Scan SET present in INSN and add an entry to the hash TABLE.
279 IMPLICIT is true if it's an implicit set, false otherwise. */
281 static void
282 hash_scan_set (rtx set, rtx_insn *insn, struct hash_table_d *table,
283 bool implicit)
285 rtx src = SET_SRC (set);
286 rtx dest = SET_DEST (set);
288 if (cprop_reg_p (dest)
289 && reg_available_p (dest, insn)
290 && can_copy_p (GET_MODE (dest)))
292 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
294 This allows us to do a single CPROP pass and still eliminate
295 redundant constants, addresses or other expressions that are
296 constructed with multiple instructions.
298 However, keep the original SRC if INSN is a simple reg-reg move. In
299 In this case, there will almost always be a REG_EQUAL note on the
300 insn that sets SRC. By recording the REG_EQUAL value here as SRC
301 for INSN, we miss copy propagation opportunities.
303 Note that this does not impede profitable constant propagations. We
304 "look through" reg-reg sets in lookup_set. */
305 rtx note = find_reg_equal_equiv_note (insn);
306 if (note != 0
307 && REG_NOTE_KIND (note) == REG_EQUAL
308 && !REG_P (src)
309 && cprop_constant_p (XEXP (note, 0)))
310 src = XEXP (note, 0), set = gen_rtx_SET (dest, src);
312 /* Record sets for constant/copy propagation. */
313 if ((cprop_reg_p (src)
314 && src != dest
315 && reg_available_p (src, insn))
316 || cprop_constant_p (src))
317 insert_set_in_table (dest, src, insn, table, implicit);
321 /* Process INSN and add hash table entries as appropriate. */
323 static void
324 hash_scan_insn (rtx_insn *insn, struct hash_table_d *table)
326 rtx pat = PATTERN (insn);
327 int i;
329 /* Pick out the sets of INSN and for other forms of instructions record
330 what's been modified. */
332 if (GET_CODE (pat) == SET)
333 hash_scan_set (pat, insn, table, false);
334 else if (GET_CODE (pat) == PARALLEL)
335 for (i = 0; i < XVECLEN (pat, 0); i++)
337 rtx x = XVECEXP (pat, 0, i);
339 if (GET_CODE (x) == SET)
340 hash_scan_set (x, insn, table, false);
344 /* Dump the hash table TABLE to file FILE under the name NAME. */
346 static void
347 dump_hash_table (FILE *file, const char *name, struct hash_table_d *table)
349 int i;
350 /* Flattened out table, so it's printed in proper order. */
351 struct cprop_expr **flat_table;
352 unsigned int *hash_val;
353 struct cprop_expr *expr;
355 flat_table = XCNEWVEC (struct cprop_expr *, table->n_elems);
356 hash_val = XNEWVEC (unsigned int, table->n_elems);
358 for (i = 0; i < (int) table->size; i++)
359 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
361 flat_table[expr->bitmap_index] = expr;
362 hash_val[expr->bitmap_index] = i;
365 fprintf (file, "%s hash table (%d buckets, %d entries)\n",
366 name, table->size, table->n_elems);
368 for (i = 0; i < (int) table->n_elems; i++)
369 if (flat_table[i] != 0)
371 expr = flat_table[i];
372 fprintf (file, "Index %d (hash value %d)\n ",
373 expr->bitmap_index, hash_val[i]);
374 print_rtl (file, expr->dest);
375 fprintf (file, " := ");
376 print_rtl (file, expr->src);
377 fprintf (file, "\n");
380 fprintf (file, "\n");
382 free (flat_table);
383 free (hash_val);
386 /* Record as unavailable all registers that are DEF operands of INSN. */
388 static void
389 make_set_regs_unavailable (rtx_insn *insn)
391 df_ref def;
393 FOR_EACH_INSN_DEF (def, insn)
394 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
397 /* Top level function to create an assignment hash table.
399 Assignment entries are placed in the hash table if
400 - they are of the form (set (pseudo-reg) src),
401 - src is something we want to perform const/copy propagation on,
402 - none of the operands or target are subsequently modified in the block
404 Currently src must be a pseudo-reg or a const_int.
406 TABLE is the table computed. */
408 static void
409 compute_hash_table_work (struct hash_table_d *table)
411 basic_block bb;
413 /* Allocate vars to track sets of regs. */
414 reg_set_bitmap = ALLOC_REG_SET (NULL);
416 FOR_EACH_BB_FN (bb, cfun)
418 rtx_insn *insn;
420 /* Reset tables used to keep track of what's not yet invalid [since
421 the end of the block]. */
422 CLEAR_REG_SET (reg_set_bitmap);
424 /* Go over all insns from the last to the first. This is convenient
425 for tracking available registers, i.e. not set between INSN and
426 the end of the basic block BB. */
427 FOR_BB_INSNS_REVERSE (bb, insn)
429 /* Only real insns are interesting. */
430 if (!NONDEBUG_INSN_P (insn))
431 continue;
433 /* Record interesting sets from INSN in the hash table. */
434 hash_scan_insn (insn, table);
436 /* Any registers set in INSN will make SETs above it not AVAIL. */
437 make_set_regs_unavailable (insn);
440 /* Insert implicit sets in the hash table, pretending they appear as
441 insns at the head of the basic block. */
442 if (implicit_sets[bb->index] != NULL_RTX)
443 hash_scan_set (implicit_sets[bb->index], BB_HEAD (bb), table, true);
446 FREE_REG_SET (reg_set_bitmap);
449 /* Allocate space for the set/expr hash TABLE.
450 It is used to determine the number of buckets to use. */
452 static void
453 alloc_hash_table (struct hash_table_d *table)
455 int n;
457 n = get_max_insn_count ();
459 table->size = n / 4;
460 if (table->size < 11)
461 table->size = 11;
463 /* Attempt to maintain efficient use of hash table.
464 Making it an odd number is simplest for now.
465 ??? Later take some measurements. */
466 table->size |= 1;
467 n = table->size * sizeof (struct cprop_expr *);
468 table->table = XNEWVAR (struct cprop_expr *, n);
471 /* Free things allocated by alloc_hash_table. */
473 static void
474 free_hash_table (struct hash_table_d *table)
476 free (table->table);
479 /* Compute the hash TABLE for doing copy/const propagation or
480 expression hash table. */
482 static void
483 compute_hash_table (struct hash_table_d *table)
485 /* Initialize count of number of entries in hash table. */
486 table->n_elems = 0;
487 memset (table->table, 0, table->size * sizeof (struct cprop_expr *));
489 compute_hash_table_work (table);
492 /* Expression tracking support. */
494 /* Lookup REGNO in the set TABLE. The result is a pointer to the
495 table entry, or NULL if not found. */
497 static struct cprop_expr *
498 lookup_set (unsigned int regno, struct hash_table_d *table)
500 unsigned int hash = hash_mod (regno, table->size);
501 struct cprop_expr *expr;
503 expr = table->table[hash];
505 while (expr && REGNO (expr->dest) != regno)
506 expr = expr->next_same_hash;
508 return expr;
511 /* Return the next entry for REGNO in list EXPR. */
513 static struct cprop_expr *
514 next_set (unsigned int regno, struct cprop_expr *expr)
517 expr = expr->next_same_hash;
518 while (expr && REGNO (expr->dest) != regno);
520 return expr;
523 /* Reset tables used to keep track of what's still available [since the
524 start of the block]. */
526 static void
527 reset_opr_set_tables (void)
529 /* Maintain a bitmap of which regs have been set since beginning of
530 the block. */
531 CLEAR_REG_SET (reg_set_bitmap);
534 /* Return nonzero if the register X has not been set yet [since the
535 start of the basic block containing INSN]. */
537 static int
538 reg_not_set_p (const_rtx x, const rtx_insn *insn ATTRIBUTE_UNUSED)
540 return ! REGNO_REG_SET_P (reg_set_bitmap, REGNO (x));
543 /* Record things set by INSN.
544 This data is used by reg_not_set_p. */
546 static void
547 mark_oprs_set (rtx_insn *insn)
549 df_ref def;
551 FOR_EACH_INSN_DEF (def, insn)
552 SET_REGNO_REG_SET (reg_set_bitmap, DF_REF_REGNO (def));
555 /* Compute copy/constant propagation working variables. */
557 /* Local properties of assignments. */
558 static sbitmap *cprop_avloc;
559 static sbitmap *cprop_kill;
561 /* Global properties of assignments (computed from the local properties). */
562 static sbitmap *cprop_avin;
563 static sbitmap *cprop_avout;
565 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
566 basic blocks. N_SETS is the number of sets. */
568 static void
569 alloc_cprop_mem (int n_blocks, int n_sets)
571 cprop_avloc = sbitmap_vector_alloc (n_blocks, n_sets);
572 cprop_kill = sbitmap_vector_alloc (n_blocks, n_sets);
574 cprop_avin = sbitmap_vector_alloc (n_blocks, n_sets);
575 cprop_avout = sbitmap_vector_alloc (n_blocks, n_sets);
578 /* Free vars used by copy/const propagation. */
580 static void
581 free_cprop_mem (void)
583 sbitmap_vector_free (cprop_avloc);
584 sbitmap_vector_free (cprop_kill);
585 sbitmap_vector_free (cprop_avin);
586 sbitmap_vector_free (cprop_avout);
589 /* Compute the local properties of each recorded expression.
591 Local properties are those that are defined by the block, irrespective of
592 other blocks.
594 An expression is killed in a block if its operands, either DEST or SRC, are
595 modified in the block.
597 An expression is computed (locally available) in a block if it is computed
598 at least once and expression would contain the same value if the
599 computation was moved to the end of the block.
601 KILL and COMP are destination sbitmaps for recording local properties. */
603 static void
604 compute_local_properties (sbitmap *kill, sbitmap *comp,
605 struct hash_table_d *table)
607 unsigned int i;
609 /* Initialize the bitmaps that were passed in. */
610 bitmap_vector_clear (kill, last_basic_block_for_fn (cfun));
611 bitmap_vector_clear (comp, last_basic_block_for_fn (cfun));
613 for (i = 0; i < table->size; i++)
615 struct cprop_expr *expr;
617 for (expr = table->table[i]; expr != NULL; expr = expr->next_same_hash)
619 int indx = expr->bitmap_index;
620 df_ref def;
621 struct cprop_occr *occr;
623 /* For each definition of the destination pseudo-reg, the expression
624 is killed in the block where the definition is. */
625 for (def = DF_REG_DEF_CHAIN (REGNO (expr->dest));
626 def; def = DF_REF_NEXT_REG (def))
627 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
629 /* If the source is a pseudo-reg, for each definition of the source,
630 the expression is killed in the block where the definition is. */
631 if (REG_P (expr->src))
632 for (def = DF_REG_DEF_CHAIN (REGNO (expr->src));
633 def; def = DF_REF_NEXT_REG (def))
634 bitmap_set_bit (kill[DF_REF_BB (def)->index], indx);
636 /* The occurrences recorded in avail_occr are exactly those that
637 are locally available in the block where they are. */
638 for (occr = expr->avail_occr; occr != NULL; occr = occr->next)
640 bitmap_set_bit (comp[BLOCK_FOR_INSN (occr->insn)->index], indx);
646 /* Hash table support. */
648 /* Top level routine to do the dataflow analysis needed by copy/const
649 propagation. */
651 static void
652 compute_cprop_data (void)
654 basic_block bb;
656 compute_local_properties (cprop_kill, cprop_avloc, &set_hash_table);
657 compute_available (cprop_avloc, cprop_kill, cprop_avout, cprop_avin);
659 /* Merge implicit sets into CPROP_AVIN. They are always available at the
660 entry of their basic block. We need to do this because 1) implicit sets
661 aren't recorded for the local pass so they cannot be propagated within
662 their basic block by this pass and 2) the global pass would otherwise
663 propagate them only in the successors of their basic block. */
664 FOR_EACH_BB_FN (bb, cfun)
666 int index = implicit_set_indexes[bb->index];
667 if (index != -1)
668 bitmap_set_bit (cprop_avin[bb->index], index);
672 /* Copy/constant propagation. */
674 /* Maximum number of register uses in an insn that we handle. */
675 #define MAX_USES 8
677 /* Table of uses (registers, both hard and pseudo) found in an insn.
678 Allocated statically to avoid alloc/free complexity and overhead. */
679 static rtx reg_use_table[MAX_USES];
681 /* Index into `reg_use_table' while building it. */
682 static unsigned reg_use_count;
684 /* Set up a list of register numbers used in INSN. The found uses are stored
685 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
686 and contains the number of uses in the table upon exit.
688 ??? If a register appears multiple times we will record it multiple times.
689 This doesn't hurt anything but it will slow things down. */
691 static void
692 find_used_regs (rtx *xptr, void *data ATTRIBUTE_UNUSED)
694 int i, j;
695 enum rtx_code code;
696 const char *fmt;
697 rtx x = *xptr;
699 /* repeat is used to turn tail-recursion into iteration since GCC
700 can't do it when there's no return value. */
701 repeat:
702 if (x == 0)
703 return;
705 code = GET_CODE (x);
706 if (REG_P (x))
708 if (reg_use_count == MAX_USES)
709 return;
711 reg_use_table[reg_use_count] = x;
712 reg_use_count++;
715 /* Recursively scan the operands of this expression. */
717 for (i = GET_RTX_LENGTH (code) - 1, fmt = GET_RTX_FORMAT (code); i >= 0; i--)
719 if (fmt[i] == 'e')
721 /* If we are about to do the last recursive call
722 needed at this level, change it into iteration.
723 This function is called enough to be worth it. */
724 if (i == 0)
726 x = XEXP (x, 0);
727 goto repeat;
730 find_used_regs (&XEXP (x, i), data);
732 else if (fmt[i] == 'E')
733 for (j = 0; j < XVECLEN (x, i); j++)
734 find_used_regs (&XVECEXP (x, i, j), data);
738 /* Try to replace all uses of FROM in INSN with TO.
739 Return nonzero if successful. */
741 static int
742 try_replace_reg (rtx from, rtx to, rtx_insn *insn)
744 rtx note = find_reg_equal_equiv_note (insn);
745 rtx src = 0;
746 int success = 0;
747 rtx set = single_set (insn);
749 bool check_rtx_costs = true;
750 bool speed = optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn));
751 int old_cost = set ? set_rtx_cost (set, speed) : 0;
753 if (!set
754 || CONSTANT_P (SET_SRC (set))
755 || (note != 0
756 && REG_NOTE_KIND (note) == REG_EQUAL
757 && (GET_CODE (XEXP (note, 0)) == CONST
758 || CONSTANT_P (XEXP (note, 0)))))
759 check_rtx_costs = false;
761 /* Usually we substitute easy stuff, so we won't copy everything.
762 We however need to take care to not duplicate non-trivial CONST
763 expressions. */
764 to = copy_rtx (to);
766 validate_replace_src_group (from, to, insn);
768 /* If TO is a constant, check the cost of the set after propagation
769 to the cost of the set before the propagation. If the cost is
770 higher, then do not replace FROM with TO. */
772 if (check_rtx_costs
773 && CONSTANT_P (to)
774 && set_rtx_cost (set, speed) > old_cost)
776 cancel_changes (0);
777 return false;
781 if (num_changes_pending () && apply_change_group ())
782 success = 1;
784 /* Try to simplify SET_SRC if we have substituted a constant. */
785 if (success && set && CONSTANT_P (to))
787 src = simplify_rtx (SET_SRC (set));
789 if (src)
790 validate_change (insn, &SET_SRC (set), src, 0);
793 /* If there is already a REG_EQUAL note, update the expression in it
794 with our replacement. */
795 if (note != 0 && REG_NOTE_KIND (note) == REG_EQUAL)
796 set_unique_reg_note (insn, REG_EQUAL,
797 simplify_replace_rtx (XEXP (note, 0), from, to));
798 if (!success && set && reg_mentioned_p (from, SET_SRC (set)))
800 /* If above failed and this is a single set, try to simplify the source
801 of the set given our substitution. We could perhaps try this for
802 multiple SETs, but it probably won't buy us anything. */
803 src = simplify_replace_rtx (SET_SRC (set), from, to);
805 if (!rtx_equal_p (src, SET_SRC (set))
806 && validate_change (insn, &SET_SRC (set), src, 0))
807 success = 1;
809 /* If we've failed perform the replacement, have a single SET to
810 a REG destination and don't yet have a note, add a REG_EQUAL note
811 to not lose information. */
812 if (!success && note == 0 && set != 0 && REG_P (SET_DEST (set)))
813 note = set_unique_reg_note (insn, REG_EQUAL, copy_rtx (src));
816 if (set && MEM_P (SET_DEST (set)) && reg_mentioned_p (from, SET_DEST (set)))
818 /* Registers can also appear as uses in SET_DEST if it is a MEM.
819 We could perhaps try this for multiple SETs, but it probably
820 won't buy us anything. */
821 rtx dest = simplify_replace_rtx (SET_DEST (set), from, to);
823 if (!rtx_equal_p (dest, SET_DEST (set))
824 && validate_change (insn, &SET_DEST (set), dest, 0))
825 success = 1;
828 /* REG_EQUAL may get simplified into register.
829 We don't allow that. Remove that note. This code ought
830 not to happen, because previous code ought to synthesize
831 reg-reg move, but be on the safe side. */
832 if (note && REG_NOTE_KIND (note) == REG_EQUAL && REG_P (XEXP (note, 0)))
833 remove_note (insn, note);
835 return success;
838 /* Find a set of REGNOs that are available on entry to INSN's block. If found,
839 SET_RET[0] will be assigned a set with a register source and SET_RET[1] a
840 set with a constant source. If not found the corresponding entry is set to
841 NULL. */
843 static void
844 find_avail_set (int regno, rtx_insn *insn, struct cprop_expr *set_ret[2])
846 set_ret[0] = set_ret[1] = NULL;
848 /* Loops are not possible here. To get a loop we would need two sets
849 available at the start of the block containing INSN. i.e. we would
850 need two sets like this available at the start of the block:
852 (set (reg X) (reg Y))
853 (set (reg Y) (reg X))
855 This can not happen since the set of (reg Y) would have killed the
856 set of (reg X) making it unavailable at the start of this block. */
857 while (1)
859 rtx src;
860 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
862 /* Find a set that is available at the start of the block
863 which contains INSN. */
864 while (set)
866 if (bitmap_bit_p (cprop_avin[BLOCK_FOR_INSN (insn)->index],
867 set->bitmap_index))
868 break;
869 set = next_set (regno, set);
872 /* If no available set was found we've reached the end of the
873 (possibly empty) copy chain. */
874 if (set == 0)
875 break;
877 src = set->src;
879 /* We know the set is available.
880 Now check that SRC is locally anticipatable (i.e. none of the
881 source operands have changed since the start of the block).
883 If the source operand changed, we may still use it for the next
884 iteration of this loop, but we may not use it for substitutions. */
886 if (cprop_constant_p (src))
887 set_ret[1] = set;
888 else if (reg_not_set_p (src, insn))
889 set_ret[0] = set;
891 /* If the source of the set is anything except a register, then
892 we have reached the end of the copy chain. */
893 if (! REG_P (src))
894 break;
896 /* Follow the copy chain, i.e. start another iteration of the loop
897 and see if we have an available copy into SRC. */
898 regno = REGNO (src);
902 /* Subroutine of cprop_insn that tries to propagate constants into
903 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
904 it is the instruction that immediately precedes JUMP, and must be a
905 single SET of a register. FROM is what we will try to replace,
906 SRC is the constant we will try to substitute for it. Return nonzero
907 if a change was made. */
909 static int
910 cprop_jump (basic_block bb, rtx_insn *setcc, rtx_insn *jump, rtx from, rtx src)
912 rtx new_rtx, set_src, note_src;
913 rtx set = pc_set (jump);
914 rtx note = find_reg_equal_equiv_note (jump);
916 if (note)
918 note_src = XEXP (note, 0);
919 if (GET_CODE (note_src) == EXPR_LIST)
920 note_src = NULL_RTX;
922 else note_src = NULL_RTX;
924 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
925 set_src = note_src ? note_src : SET_SRC (set);
927 /* First substitute the SETCC condition into the JUMP instruction,
928 then substitute that given values into this expanded JUMP. */
929 if (setcc != NULL_RTX
930 && !modified_between_p (from, setcc, jump)
931 && !modified_between_p (src, setcc, jump))
933 rtx setcc_src;
934 rtx setcc_set = single_set (setcc);
935 rtx setcc_note = find_reg_equal_equiv_note (setcc);
936 setcc_src = (setcc_note && GET_CODE (XEXP (setcc_note, 0)) != EXPR_LIST)
937 ? XEXP (setcc_note, 0) : SET_SRC (setcc_set);
938 set_src = simplify_replace_rtx (set_src, SET_DEST (setcc_set),
939 setcc_src);
941 else
942 setcc = NULL;
944 new_rtx = simplify_replace_rtx (set_src, from, src);
946 /* If no simplification can be made, then try the next register. */
947 if (rtx_equal_p (new_rtx, SET_SRC (set)))
948 return 0;
950 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
951 if (new_rtx == pc_rtx)
952 delete_insn (jump);
953 else
955 /* Ensure the value computed inside the jump insn to be equivalent
956 to one computed by setcc. */
957 if (setcc && modified_in_p (new_rtx, setcc))
958 return 0;
959 if (! validate_unshare_change (jump, &SET_SRC (set), new_rtx, 0))
961 /* When (some) constants are not valid in a comparison, and there
962 are two registers to be replaced by constants before the entire
963 comparison can be folded into a constant, we need to keep
964 intermediate information in REG_EQUAL notes. For targets with
965 separate compare insns, such notes are added by try_replace_reg.
966 When we have a combined compare-and-branch instruction, however,
967 we need to attach a note to the branch itself to make this
968 optimization work. */
970 if (!rtx_equal_p (new_rtx, note_src))
971 set_unique_reg_note (jump, REG_EQUAL, copy_rtx (new_rtx));
972 return 0;
975 /* Remove REG_EQUAL note after simplification. */
976 if (note_src)
977 remove_note (jump, note);
980 /* Delete the cc0 setter. */
981 if (HAVE_cc0 && setcc != NULL && CC0_P (SET_DEST (single_set (setcc))))
982 delete_insn (setcc);
984 global_const_prop_count++;
985 if (dump_file != NULL)
987 fprintf (dump_file,
988 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with"
989 "constant ", REGNO (from), INSN_UID (jump));
990 print_rtl (dump_file, src);
991 fprintf (dump_file, "\n");
993 purge_dead_edges (bb);
995 /* If a conditional jump has been changed into unconditional jump, remove
996 the jump and make the edge fallthru - this is always called in
997 cfglayout mode. */
998 if (new_rtx != pc_rtx && simplejump_p (jump))
1000 edge e;
1001 edge_iterator ei;
1003 FOR_EACH_EDGE (e, ei, bb->succs)
1004 if (e->dest != EXIT_BLOCK_PTR_FOR_FN (cfun)
1005 && BB_HEAD (e->dest) == JUMP_LABEL (jump))
1007 e->flags |= EDGE_FALLTHRU;
1008 break;
1010 delete_insn (jump);
1013 return 1;
1016 /* Subroutine of cprop_insn that tries to propagate constants. FROM is what
1017 we will try to replace, SRC is the constant we will try to substitute for
1018 it and INSN is the instruction where this will be happening. */
1020 static int
1021 constprop_register (rtx from, rtx src, rtx_insn *insn)
1023 rtx sset;
1025 /* Check for reg or cc0 setting instructions followed by
1026 conditional branch instructions first. */
1027 if ((sset = single_set (insn)) != NULL
1028 && NEXT_INSN (insn)
1029 && any_condjump_p (NEXT_INSN (insn)) && onlyjump_p (NEXT_INSN (insn)))
1031 rtx dest = SET_DEST (sset);
1032 if ((REG_P (dest) || CC0_P (dest))
1033 && cprop_jump (BLOCK_FOR_INSN (insn), insn, NEXT_INSN (insn),
1034 from, src))
1035 return 1;
1038 /* Handle normal insns next. */
1039 if (NONJUMP_INSN_P (insn) && try_replace_reg (from, src, insn))
1040 return 1;
1042 /* Try to propagate a CONST_INT into a conditional jump.
1043 We're pretty specific about what we will handle in this
1044 code, we can extend this as necessary over time.
1046 Right now the insn in question must look like
1047 (set (pc) (if_then_else ...)) */
1048 else if (any_condjump_p (insn) && onlyjump_p (insn))
1049 return cprop_jump (BLOCK_FOR_INSN (insn), NULL, insn, from, src);
1050 return 0;
1053 /* Perform constant and copy propagation on INSN.
1054 Return nonzero if a change was made. */
1056 static int
1057 cprop_insn (rtx_insn *insn)
1059 unsigned i;
1060 int changed = 0, changed_this_round;
1061 rtx note;
1065 changed_this_round = 0;
1066 reg_use_count = 0;
1067 note_uses (&PATTERN (insn), find_used_regs, NULL);
1069 /* We may win even when propagating constants into notes. */
1070 note = find_reg_equal_equiv_note (insn);
1071 if (note)
1072 find_used_regs (&XEXP (note, 0), NULL);
1074 for (i = 0; i < reg_use_count; i++)
1076 rtx reg_used = reg_use_table[i];
1077 unsigned int regno = REGNO (reg_used);
1078 rtx src_cst = NULL, src_reg = NULL;
1079 struct cprop_expr *set[2];
1081 /* If the register has already been set in this block, there's
1082 nothing we can do. */
1083 if (! reg_not_set_p (reg_used, insn))
1084 continue;
1086 /* Find an assignment that sets reg_used and is available
1087 at the start of the block. */
1088 find_avail_set (regno, insn, set);
1089 if (set[0])
1090 src_reg = set[0]->src;
1091 if (set[1])
1092 src_cst = set[1]->src;
1094 /* Constant propagation. */
1095 if (src_cst && cprop_constant_p (src_cst)
1096 && constprop_register (reg_used, src_cst, insn))
1098 changed_this_round = changed = 1;
1099 global_const_prop_count++;
1100 if (dump_file != NULL)
1102 fprintf (dump_file,
1103 "GLOBAL CONST-PROP: Replacing reg %d in ", regno);
1104 fprintf (dump_file, "insn %d with constant ",
1105 INSN_UID (insn));
1106 print_rtl (dump_file, src_cst);
1107 fprintf (dump_file, "\n");
1109 if (insn->deleted ())
1110 return 1;
1112 /* Copy propagation. */
1113 else if (src_reg && cprop_reg_p (src_reg)
1114 && REGNO (src_reg) != regno
1115 && try_replace_reg (reg_used, src_reg, insn))
1117 changed_this_round = changed = 1;
1118 global_copy_prop_count++;
1119 if (dump_file != NULL)
1121 fprintf (dump_file,
1122 "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1123 regno, INSN_UID (insn));
1124 fprintf (dump_file, " with reg %d\n", REGNO (src_reg));
1127 /* The original insn setting reg_used may or may not now be
1128 deletable. We leave the deletion to DCE. */
1129 /* FIXME: If it turns out that the insn isn't deletable,
1130 then we may have unnecessarily extended register lifetimes
1131 and made things worse. */
1135 /* If try_replace_reg simplified the insn, the regs found by find_used_regs
1136 may not be valid anymore. Start over. */
1137 while (changed_this_round);
1139 if (changed && DEBUG_INSN_P (insn))
1140 return 0;
1142 return changed;
1145 /* Like find_used_regs, but avoid recording uses that appear in
1146 input-output contexts such as zero_extract or pre_dec. This
1147 restricts the cases we consider to those for which local cprop
1148 can legitimately make replacements. */
1150 static void
1151 local_cprop_find_used_regs (rtx *xptr, void *data)
1153 rtx x = *xptr;
1155 if (x == 0)
1156 return;
1158 switch (GET_CODE (x))
1160 case ZERO_EXTRACT:
1161 case SIGN_EXTRACT:
1162 case STRICT_LOW_PART:
1163 return;
1165 case PRE_DEC:
1166 case PRE_INC:
1167 case POST_DEC:
1168 case POST_INC:
1169 case PRE_MODIFY:
1170 case POST_MODIFY:
1171 /* Can only legitimately appear this early in the context of
1172 stack pushes for function arguments, but handle all of the
1173 codes nonetheless. */
1174 return;
1176 case SUBREG:
1177 /* Setting a subreg of a register larger than word_mode leaves
1178 the non-written words unchanged. */
1179 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x))) > BITS_PER_WORD)
1180 return;
1181 break;
1183 default:
1184 break;
1187 find_used_regs (xptr, data);
1190 /* Try to perform local const/copy propagation on X in INSN. */
1192 static bool
1193 do_local_cprop (rtx x, rtx_insn *insn)
1195 rtx newreg = NULL, newcnst = NULL;
1197 /* Rule out USE instructions and ASM statements as we don't want to
1198 change the hard registers mentioned. */
1199 if (REG_P (x)
1200 && (cprop_reg_p (x)
1201 || (GET_CODE (PATTERN (insn)) != USE
1202 && asm_noperands (PATTERN (insn)) < 0)))
1204 cselib_val *val = cselib_lookup (x, GET_MODE (x), 0, VOIDmode);
1205 struct elt_loc_list *l;
1207 if (!val)
1208 return false;
1209 for (l = val->locs; l; l = l->next)
1211 rtx this_rtx = l->loc;
1212 rtx note;
1214 if (cprop_constant_p (this_rtx))
1215 newcnst = this_rtx;
1216 if (cprop_reg_p (this_rtx)
1217 /* Don't copy propagate if it has attached REG_EQUIV note.
1218 At this point this only function parameters should have
1219 REG_EQUIV notes and if the argument slot is used somewhere
1220 explicitly, it means address of parameter has been taken,
1221 so we should not extend the lifetime of the pseudo. */
1222 && (!(note = find_reg_note (l->setting_insn, REG_EQUIV, NULL_RTX))
1223 || ! MEM_P (XEXP (note, 0))))
1224 newreg = this_rtx;
1226 if (newcnst && constprop_register (x, newcnst, insn))
1228 if (dump_file != NULL)
1230 fprintf (dump_file, "LOCAL CONST-PROP: Replacing reg %d in ",
1231 REGNO (x));
1232 fprintf (dump_file, "insn %d with constant ",
1233 INSN_UID (insn));
1234 print_rtl (dump_file, newcnst);
1235 fprintf (dump_file, "\n");
1237 local_const_prop_count++;
1238 return true;
1240 else if (newreg && newreg != x && try_replace_reg (x, newreg, insn))
1242 if (dump_file != NULL)
1244 fprintf (dump_file,
1245 "LOCAL COPY-PROP: Replacing reg %d in insn %d",
1246 REGNO (x), INSN_UID (insn));
1247 fprintf (dump_file, " with reg %d\n", REGNO (newreg));
1249 local_copy_prop_count++;
1250 return true;
1253 return false;
1256 /* Do local const/copy propagation (i.e. within each basic block). */
1258 static int
1259 local_cprop_pass (void)
1261 basic_block bb;
1262 rtx_insn *insn;
1263 bool changed = false;
1264 unsigned i;
1266 cselib_init (0);
1267 FOR_EACH_BB_FN (bb, cfun)
1269 FOR_BB_INSNS (bb, insn)
1271 if (INSN_P (insn))
1273 rtx note = find_reg_equal_equiv_note (insn);
1276 reg_use_count = 0;
1277 note_uses (&PATTERN (insn), local_cprop_find_used_regs,
1278 NULL);
1279 if (note)
1280 local_cprop_find_used_regs (&XEXP (note, 0), NULL);
1282 for (i = 0; i < reg_use_count; i++)
1284 if (do_local_cprop (reg_use_table[i], insn))
1286 if (!DEBUG_INSN_P (insn))
1287 changed = true;
1288 break;
1291 if (insn->deleted ())
1292 break;
1294 while (i < reg_use_count);
1296 cselib_process_insn (insn);
1299 /* Forget everything at the end of a basic block. */
1300 cselib_clear_table ();
1303 cselib_finish ();
1305 return changed;
1308 /* Similar to get_condition, only the resulting condition must be
1309 valid at JUMP, instead of at EARLIEST.
1311 This differs from noce_get_condition in ifcvt.c in that we prefer not to
1312 settle for the condition variable in the jump instruction being integral.
1313 We prefer to be able to record the value of a user variable, rather than
1314 the value of a temporary used in a condition. This could be solved by
1315 recording the value of *every* register scanned by canonicalize_condition,
1316 but this would require some code reorganization. */
1319 fis_get_condition (rtx_insn *jump)
1321 return get_condition (jump, NULL, false, true);
1324 /* Check the comparison COND to see if we can safely form an implicit
1325 set from it. */
1327 static bool
1328 implicit_set_cond_p (const_rtx cond)
1330 machine_mode mode;
1331 rtx cst;
1333 /* COND must be either an EQ or NE comparison. */
1334 if (GET_CODE (cond) != EQ && GET_CODE (cond) != NE)
1335 return false;
1337 /* The first operand of COND must be a register we can propagate. */
1338 if (!cprop_reg_p (XEXP (cond, 0)))
1339 return false;
1341 /* The second operand of COND must be a suitable constant. */
1342 mode = GET_MODE (XEXP (cond, 0));
1343 cst = XEXP (cond, 1);
1345 /* We can't perform this optimization if either operand might be or might
1346 contain a signed zero. */
1347 if (HONOR_SIGNED_ZEROS (mode))
1349 /* It is sufficient to check if CST is or contains a zero. We must
1350 handle float, complex, and vector. If any subpart is a zero, then
1351 the optimization can't be performed. */
1352 /* ??? The complex and vector checks are not implemented yet. We just
1353 always return zero for them. */
1354 if (CONST_DOUBLE_AS_FLOAT_P (cst)
1355 && real_equal (CONST_DOUBLE_REAL_VALUE (cst), &dconst0))
1356 return 0;
1357 else
1358 return 0;
1361 return cprop_constant_p (cst);
1364 /* Find the implicit sets of a function. An "implicit set" is a constraint
1365 on the value of a variable, implied by a conditional jump. For example,
1366 following "if (x == 2)", the then branch may be optimized as though the
1367 conditional performed an "explicit set", in this example, "x = 2". This
1368 function records the set patterns that are implicit at the start of each
1369 basic block.
1371 If an implicit set is found but the set is implicit on a critical edge,
1372 this critical edge is split.
1374 Return true if the CFG was modified, false otherwise. */
1376 static bool
1377 find_implicit_sets (void)
1379 basic_block bb, dest;
1380 rtx cond, new_rtx;
1381 unsigned int count = 0;
1382 bool edges_split = false;
1383 size_t implicit_sets_size = last_basic_block_for_fn (cfun) + 10;
1385 implicit_sets = XCNEWVEC (rtx, implicit_sets_size);
1387 FOR_EACH_BB_FN (bb, cfun)
1389 /* Check for more than one successor. */
1390 if (EDGE_COUNT (bb->succs) <= 1)
1391 continue;
1393 cond = fis_get_condition (BB_END (bb));
1395 /* If no condition is found or if it isn't of a suitable form,
1396 ignore it. */
1397 if (! cond || ! implicit_set_cond_p (cond))
1398 continue;
1400 dest = GET_CODE (cond) == EQ
1401 ? BRANCH_EDGE (bb)->dest : FALLTHRU_EDGE (bb)->dest;
1403 /* If DEST doesn't go anywhere, ignore it. */
1404 if (! dest || dest == EXIT_BLOCK_PTR_FOR_FN (cfun))
1405 continue;
1407 /* We have found a suitable implicit set. Try to record it now as
1408 a SET in DEST. If DEST has more than one predecessor, the edge
1409 between BB and DEST is a critical edge and we must split it,
1410 because we can only record one implicit set per DEST basic block. */
1411 if (! single_pred_p (dest))
1413 dest = split_edge (find_edge (bb, dest));
1414 edges_split = true;
1417 if (implicit_sets_size <= (size_t) dest->index)
1419 size_t old_implicit_sets_size = implicit_sets_size;
1420 implicit_sets_size *= 2;
1421 implicit_sets = XRESIZEVEC (rtx, implicit_sets, implicit_sets_size);
1422 memset (implicit_sets + old_implicit_sets_size, 0,
1423 (implicit_sets_size - old_implicit_sets_size) * sizeof (rtx));
1426 new_rtx = gen_rtx_SET (XEXP (cond, 0), XEXP (cond, 1));
1427 implicit_sets[dest->index] = new_rtx;
1428 if (dump_file)
1430 fprintf (dump_file, "Implicit set of reg %d in ",
1431 REGNO (XEXP (cond, 0)));
1432 fprintf (dump_file, "basic block %d\n", dest->index);
1434 count++;
1437 if (dump_file)
1438 fprintf (dump_file, "Found %d implicit sets\n", count);
1440 /* Confess our sins. */
1441 return edges_split;
1444 /* Bypass conditional jumps. */
1446 /* The value of last_basic_block at the beginning of the jump_bypass
1447 pass. The use of redirect_edge_and_branch_force may introduce new
1448 basic blocks, but the data flow analysis is only valid for basic
1449 block indices less than bypass_last_basic_block. */
1451 static int bypass_last_basic_block;
1453 /* Find a set of REGNO to a constant that is available at the end of basic
1454 block BB. Return NULL if no such set is found. Based heavily upon
1455 find_avail_set. */
1457 static struct cprop_expr *
1458 find_bypass_set (int regno, int bb)
1460 struct cprop_expr *result = 0;
1462 for (;;)
1464 rtx src;
1465 struct cprop_expr *set = lookup_set (regno, &set_hash_table);
1467 while (set)
1469 if (bitmap_bit_p (cprop_avout[bb], set->bitmap_index))
1470 break;
1471 set = next_set (regno, set);
1474 if (set == 0)
1475 break;
1477 src = set->src;
1478 if (cprop_constant_p (src))
1479 result = set;
1481 if (! REG_P (src))
1482 break;
1484 regno = REGNO (src);
1486 return result;
1489 /* Subroutine of bypass_block that checks whether a pseudo is killed by
1490 any of the instructions inserted on an edge. Jump bypassing places
1491 condition code setters on CFG edges using insert_insn_on_edge. This
1492 function is required to check that our data flow analysis is still
1493 valid prior to commit_edge_insertions. */
1495 static bool
1496 reg_killed_on_edge (const_rtx reg, const_edge e)
1498 rtx_insn *insn;
1500 for (insn = e->insns.r; insn; insn = NEXT_INSN (insn))
1501 if (INSN_P (insn) && reg_set_p (reg, insn))
1502 return true;
1504 return false;
1507 /* Subroutine of bypass_conditional_jumps that attempts to bypass the given
1508 basic block BB which has more than one predecessor. If not NULL, SETCC
1509 is the first instruction of BB, which is immediately followed by JUMP_INSN
1510 JUMP. Otherwise, SETCC is NULL, and JUMP is the first insn of BB.
1511 Returns nonzero if a change was made.
1513 During the jump bypassing pass, we may place copies of SETCC instructions
1514 on CFG edges. The following routine must be careful to pay attention to
1515 these inserted insns when performing its transformations. */
1517 static int
1518 bypass_block (basic_block bb, rtx_insn *setcc, rtx_insn *jump)
1520 rtx_insn *insn;
1521 rtx note;
1522 edge e, edest;
1523 int change;
1524 int may_be_loop_header = false;
1525 unsigned removed_p;
1526 unsigned i;
1527 edge_iterator ei;
1529 insn = (setcc != NULL) ? setcc : jump;
1531 /* Determine set of register uses in INSN. */
1532 reg_use_count = 0;
1533 note_uses (&PATTERN (insn), find_used_regs, NULL);
1534 note = find_reg_equal_equiv_note (insn);
1535 if (note)
1536 find_used_regs (&XEXP (note, 0), NULL);
1538 if (current_loops)
1540 /* If we are to preserve loop structure then do not bypass
1541 a loop header. This will either rotate the loop, create
1542 multiple entry loops or even irreducible regions. */
1543 if (bb == bb->loop_father->header)
1544 return 0;
1546 else
1548 FOR_EACH_EDGE (e, ei, bb->preds)
1549 if (e->flags & EDGE_DFS_BACK)
1551 may_be_loop_header = true;
1552 break;
1556 change = 0;
1557 for (ei = ei_start (bb->preds); (e = ei_safe_edge (ei)); )
1559 removed_p = 0;
1561 if (e->flags & EDGE_COMPLEX)
1563 ei_next (&ei);
1564 continue;
1567 /* We can't redirect edges from new basic blocks. */
1568 if (e->src->index >= bypass_last_basic_block)
1570 ei_next (&ei);
1571 continue;
1574 /* The irreducible loops created by redirecting of edges entering the
1575 loop from outside would decrease effectiveness of some of the
1576 following optimizations, so prevent this. */
1577 if (may_be_loop_header
1578 && !(e->flags & EDGE_DFS_BACK))
1580 ei_next (&ei);
1581 continue;
1584 for (i = 0; i < reg_use_count; i++)
1586 rtx reg_used = reg_use_table[i];
1587 unsigned int regno = REGNO (reg_used);
1588 basic_block dest, old_dest;
1589 struct cprop_expr *set;
1590 rtx src, new_rtx;
1592 set = find_bypass_set (regno, e->src->index);
1594 if (! set)
1595 continue;
1597 /* Check the data flow is valid after edge insertions. */
1598 if (e->insns.r && reg_killed_on_edge (reg_used, e))
1599 continue;
1601 src = SET_SRC (pc_set (jump));
1603 if (setcc != NULL)
1604 src = simplify_replace_rtx (src,
1605 SET_DEST (PATTERN (setcc)),
1606 SET_SRC (PATTERN (setcc)));
1608 new_rtx = simplify_replace_rtx (src, reg_used, set->src);
1610 /* Jump bypassing may have already placed instructions on
1611 edges of the CFG. We can't bypass an outgoing edge that
1612 has instructions associated with it, as these insns won't
1613 get executed if the incoming edge is redirected. */
1614 if (new_rtx == pc_rtx)
1616 edest = FALLTHRU_EDGE (bb);
1617 dest = edest->insns.r ? NULL : edest->dest;
1619 else if (GET_CODE (new_rtx) == LABEL_REF)
1621 dest = BLOCK_FOR_INSN (XEXP (new_rtx, 0));
1622 /* Don't bypass edges containing instructions. */
1623 edest = find_edge (bb, dest);
1624 if (edest && edest->insns.r)
1625 dest = NULL;
1627 else
1628 dest = NULL;
1630 /* Avoid unification of the edge with other edges from original
1631 branch. We would end up emitting the instruction on "both"
1632 edges. */
1633 if (dest && setcc && !CC0_P (SET_DEST (PATTERN (setcc)))
1634 && find_edge (e->src, dest))
1635 dest = NULL;
1637 old_dest = e->dest;
1638 if (dest != NULL
1639 && dest != old_dest
1640 && dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
1642 redirect_edge_and_branch_force (e, dest);
1644 /* Copy the register setter to the redirected edge.
1645 Don't copy CC0 setters, as CC0 is dead after jump. */
1646 if (setcc)
1648 rtx pat = PATTERN (setcc);
1649 if (!CC0_P (SET_DEST (pat)))
1650 insert_insn_on_edge (copy_insn (pat), e);
1653 if (dump_file != NULL)
1655 fprintf (dump_file, "JUMP-BYPASS: Proved reg %d "
1656 "in jump_insn %d equals constant ",
1657 regno, INSN_UID (jump));
1658 print_rtl (dump_file, set->src);
1659 fprintf (dump_file, "\n\t when BB %d is entered from "
1660 "BB %d. Redirect edge %d->%d to %d.\n",
1661 old_dest->index, e->src->index, e->src->index,
1662 old_dest->index, dest->index);
1664 change = 1;
1665 removed_p = 1;
1666 break;
1669 if (!removed_p)
1670 ei_next (&ei);
1672 return change;
1675 /* Find basic blocks with more than one predecessor that only contain a
1676 single conditional jump. If the result of the comparison is known at
1677 compile-time from any incoming edge, redirect that edge to the
1678 appropriate target. Return nonzero if a change was made.
1680 This function is now mis-named, because we also handle indirect jumps. */
1682 static int
1683 bypass_conditional_jumps (void)
1685 basic_block bb;
1686 int changed;
1687 rtx_insn *setcc;
1688 rtx_insn *insn;
1689 rtx dest;
1691 /* Note we start at block 1. */
1692 if (ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb == EXIT_BLOCK_PTR_FOR_FN (cfun))
1693 return 0;
1695 bypass_last_basic_block = last_basic_block_for_fn (cfun);
1696 mark_dfs_back_edges ();
1698 changed = 0;
1699 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1700 EXIT_BLOCK_PTR_FOR_FN (cfun), next_bb)
1702 /* Check for more than one predecessor. */
1703 if (!single_pred_p (bb))
1705 setcc = NULL;
1706 FOR_BB_INSNS (bb, insn)
1707 if (DEBUG_INSN_P (insn))
1708 continue;
1709 else if (NONJUMP_INSN_P (insn))
1711 if (setcc)
1712 break;
1713 if (GET_CODE (PATTERN (insn)) != SET)
1714 break;
1716 dest = SET_DEST (PATTERN (insn));
1717 if (REG_P (dest) || CC0_P (dest))
1718 setcc = insn;
1719 else
1720 break;
1722 else if (JUMP_P (insn))
1724 if ((any_condjump_p (insn) || computed_jump_p (insn))
1725 && onlyjump_p (insn))
1726 changed |= bypass_block (bb, setcc, insn);
1727 break;
1729 else if (INSN_P (insn))
1730 break;
1734 /* If we bypassed any register setting insns, we inserted a
1735 copy on the redirected edge. These need to be committed. */
1736 if (changed)
1737 commit_edge_insertions ();
1739 return changed;
1742 /* Return true if the graph is too expensive to optimize. PASS is the
1743 optimization about to be performed. */
1745 static bool
1746 is_too_expensive (const char *pass)
1748 /* Trying to perform global optimizations on flow graphs which have
1749 a high connectivity will take a long time and is unlikely to be
1750 particularly useful.
1752 In normal circumstances a cfg should have about twice as many
1753 edges as blocks. But we do not want to punish small functions
1754 which have a couple switch statements. Rather than simply
1755 threshold the number of blocks, uses something with a more
1756 graceful degradation. */
1757 if (n_edges_for_fn (cfun) > 20000 + n_basic_blocks_for_fn (cfun) * 4)
1759 warning (OPT_Wdisabled_optimization,
1760 "%s: %d basic blocks and %d edges/basic block",
1761 pass, n_basic_blocks_for_fn (cfun),
1762 n_edges_for_fn (cfun) / n_basic_blocks_for_fn (cfun));
1764 return true;
1767 /* If allocating memory for the cprop bitmap would take up too much
1768 storage it's better just to disable the optimization. */
1769 if ((n_basic_blocks_for_fn (cfun)
1770 * SBITMAP_SET_SIZE (max_reg_num ())
1771 * sizeof (SBITMAP_ELT_TYPE)) > MAX_GCSE_MEMORY)
1773 warning (OPT_Wdisabled_optimization,
1774 "%s: %d basic blocks and %d registers",
1775 pass, n_basic_blocks_for_fn (cfun), max_reg_num ());
1777 return true;
1780 return false;
1783 /* Main function for the CPROP pass. */
1785 static int
1786 one_cprop_pass (void)
1788 int i;
1789 int changed = 0;
1791 /* Return if there's nothing to do, or it is too expensive. */
1792 if (n_basic_blocks_for_fn (cfun) <= NUM_FIXED_BLOCKS + 1
1793 || is_too_expensive (_ ("const/copy propagation disabled")))
1794 return 0;
1796 global_const_prop_count = local_const_prop_count = 0;
1797 global_copy_prop_count = local_copy_prop_count = 0;
1799 bytes_used = 0;
1800 gcc_obstack_init (&cprop_obstack);
1802 /* Do a local const/copy propagation pass first. The global pass
1803 only handles global opportunities.
1804 If the local pass changes something, remove any unreachable blocks
1805 because the CPROP global dataflow analysis may get into infinite
1806 loops for CFGs with unreachable blocks.
1808 FIXME: This local pass should not be necessary after CSE (but for
1809 some reason it still is). It is also (proven) not necessary
1810 to run the local pass right after FWPWOP.
1812 FIXME: The global analysis would not get into infinite loops if it
1813 would use the DF solver (via df_simple_dataflow) instead of
1814 the solver implemented in this file. */
1815 changed |= local_cprop_pass ();
1816 if (changed)
1817 delete_unreachable_blocks ();
1819 /* Determine implicit sets. This may change the CFG (split critical
1820 edges if that exposes an implicit set).
1821 Note that find_implicit_sets() does not rely on up-to-date DF caches
1822 so that we do not have to re-run df_analyze() even if local CPROP
1823 changed something.
1824 ??? This could run earlier so that any uncovered implicit sets
1825 sets could be exploited in local_cprop_pass() also. Later. */
1826 changed |= find_implicit_sets ();
1828 /* If local_cprop_pass() or find_implicit_sets() changed something,
1829 run df_analyze() to bring all insn caches up-to-date, and to take
1830 new basic blocks from edge splitting on the DF radar.
1831 NB: This also runs the fast DCE pass, because execute_rtl_cprop
1832 sets DF_LR_RUN_DCE. */
1833 if (changed)
1834 df_analyze ();
1836 /* Initialize implicit_set_indexes array. */
1837 implicit_set_indexes = XNEWVEC (int, last_basic_block_for_fn (cfun));
1838 for (i = 0; i < last_basic_block_for_fn (cfun); i++)
1839 implicit_set_indexes[i] = -1;
1841 alloc_hash_table (&set_hash_table);
1842 compute_hash_table (&set_hash_table);
1844 /* Free implicit_sets before peak usage. */
1845 free (implicit_sets);
1846 implicit_sets = NULL;
1848 if (dump_file)
1849 dump_hash_table (dump_file, "SET", &set_hash_table);
1850 if (set_hash_table.n_elems > 0)
1852 basic_block bb;
1853 rtx_insn *insn;
1855 alloc_cprop_mem (last_basic_block_for_fn (cfun),
1856 set_hash_table.n_elems);
1857 compute_cprop_data ();
1859 free (implicit_set_indexes);
1860 implicit_set_indexes = NULL;
1862 /* Allocate vars to track sets of regs. */
1863 reg_set_bitmap = ALLOC_REG_SET (NULL);
1865 FOR_BB_BETWEEN (bb, ENTRY_BLOCK_PTR_FOR_FN (cfun)->next_bb->next_bb,
1866 EXIT_BLOCK_PTR_FOR_FN (cfun),
1867 next_bb)
1869 /* Reset tables used to keep track of what's still valid [since
1870 the start of the block]. */
1871 reset_opr_set_tables ();
1873 FOR_BB_INSNS (bb, insn)
1874 if (INSN_P (insn))
1876 changed |= cprop_insn (insn);
1878 /* Keep track of everything modified by this insn. */
1879 /* ??? Need to be careful w.r.t. mods done to INSN.
1880 Don't call mark_oprs_set if we turned the
1881 insn into a NOTE, or deleted the insn. */
1882 if (! NOTE_P (insn) && ! insn->deleted ())
1883 mark_oprs_set (insn);
1887 changed |= bypass_conditional_jumps ();
1889 FREE_REG_SET (reg_set_bitmap);
1890 free_cprop_mem ();
1892 else
1894 free (implicit_set_indexes);
1895 implicit_set_indexes = NULL;
1898 free_hash_table (&set_hash_table);
1899 obstack_free (&cprop_obstack, NULL);
1901 if (dump_file)
1903 fprintf (dump_file, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1904 current_function_name (), n_basic_blocks_for_fn (cfun),
1905 bytes_used);
1906 fprintf (dump_file, "%d local const props, %d local copy props, ",
1907 local_const_prop_count, local_copy_prop_count);
1908 fprintf (dump_file, "%d global const props, %d global copy props\n\n",
1909 global_const_prop_count, global_copy_prop_count);
1912 return changed;
1915 /* All the passes implemented in this file. Each pass has its
1916 own gate and execute function, and at the end of the file a
1917 pass definition for passes.c.
1919 We do not construct an accurate cfg in functions which call
1920 setjmp, so none of these passes runs if the function calls
1921 setjmp.
1922 FIXME: Should just handle setjmp via REG_SETJMP notes. */
1924 static unsigned int
1925 execute_rtl_cprop (void)
1927 int changed;
1928 delete_unreachable_blocks ();
1929 df_set_flags (DF_LR_RUN_DCE);
1930 df_analyze ();
1931 changed = one_cprop_pass ();
1932 flag_rerun_cse_after_global_opts |= changed;
1933 if (changed)
1934 cleanup_cfg (CLEANUP_CFG_CHANGED);
1935 return 0;
1938 namespace {
1940 const pass_data pass_data_rtl_cprop =
1942 RTL_PASS, /* type */
1943 "cprop", /* name */
1944 OPTGROUP_NONE, /* optinfo_flags */
1945 TV_CPROP, /* tv_id */
1946 PROP_cfglayout, /* properties_required */
1947 0, /* properties_provided */
1948 0, /* properties_destroyed */
1949 0, /* todo_flags_start */
1950 TODO_df_finish, /* todo_flags_finish */
1953 class pass_rtl_cprop : public rtl_opt_pass
1955 public:
1956 pass_rtl_cprop (gcc::context *ctxt)
1957 : rtl_opt_pass (pass_data_rtl_cprop, ctxt)
1960 /* opt_pass methods: */
1961 opt_pass * clone () { return new pass_rtl_cprop (m_ctxt); }
1962 virtual bool gate (function *fun)
1964 return optimize > 0 && flag_gcse
1965 && !fun->calls_setjmp
1966 && dbg_cnt (cprop);
1969 virtual unsigned int execute (function *) { return execute_rtl_cprop (); }
1971 }; // class pass_rtl_cprop
1973 } // anon namespace
1975 rtl_opt_pass *
1976 make_pass_rtl_cprop (gcc::context *ctxt)
1978 return new pass_rtl_cprop (ctxt);