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
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
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/>. */
22 #include "coretypes.h"
27 #include "insn-config.h"
30 #include "diagnostic-core.h"
35 #include "cfgcleanup.h"
39 #include "tree-pass.h"
44 /* An obstack for our working variables. */
45 static struct obstack cprop_obstack
;
47 /* Occurrence of an expression.
48 There is one per basic block. If a pattern appears more than once the
49 last appearance is used. */
53 /* Next occurrence of this expression. */
54 struct cprop_occr
*next
;
55 /* The insn that computes the expression. */
59 /* Hash table entry for assignment expressions. */
63 /* The expression (DEST := SRC). */
67 /* Index in the available expression bitmaps. */
69 /* Next entry with the same hash. */
70 struct cprop_expr
*next_same_hash
;
71 /* List of available occurrence in basic blocks in the function.
72 An "available occurrence" is one that is the last occurrence in the
73 basic block and whose operands are not modified by following statements
74 in the basic block [including this insn]. */
75 struct cprop_occr
*avail_occr
;
78 /* Hash table for copy propagation expressions.
79 Each hash table is an array of buckets.
80 ??? It is known that if it were an array of entries, structure elements
81 `next_same_hash' and `bitmap_index' wouldn't be necessary. However, it is
82 not clear whether in the final analysis a sufficient amount of memory would
83 be saved as the size of the available expression bitmaps would be larger
84 [one could build a mapping table without holes afterwards though].
85 Someday I'll perform the computation and figure it out. */
90 This is an array of `set_hash_table_size' elements. */
91 struct cprop_expr
**table
;
93 /* Size of the hash table, in elements. */
96 /* Number of hash table elements. */
100 /* Copy propagation hash table. */
101 static struct hash_table_d set_hash_table
;
103 /* Array of implicit set patterns indexed by basic block index. */
104 static rtx
*implicit_sets
;
106 /* Array of indexes of expressions for implicit set patterns indexed by basic
107 block index. In other words, implicit_set_indexes[i] is the bitmap_index
108 of the expression whose RTX is implicit_sets[i]. */
109 static int *implicit_set_indexes
;
111 /* Bitmap containing one bit for each register in the program.
112 Used when performing GCSE to track which registers have been set since
113 the start or end of the basic block while traversing that block. */
114 static regset reg_set_bitmap
;
116 /* Various variables for statistics gathering. */
118 /* Memory used in a pass.
119 This isn't intended to be absolutely precise. Its intent is only
120 to keep an eye on memory usage. */
121 static int bytes_used
;
123 /* Number of local constants propagated. */
124 static int local_const_prop_count
;
125 /* Number of local copies propagated. */
126 static int local_copy_prop_count
;
127 /* Number of global constants propagated. */
128 static int global_const_prop_count
;
129 /* Number of global copies propagated. */
130 static int global_copy_prop_count
;
132 #define GOBNEW(T) ((T *) cprop_alloc (sizeof (T)))
133 #define GOBNEWVAR(T, S) ((T *) cprop_alloc ((S)))
135 /* Cover function to obstack_alloc. */
138 cprop_alloc (unsigned long size
)
141 return obstack_alloc (&cprop_obstack
, size
);
144 /* Return nonzero if register X is unchanged from INSN to the end
145 of INSN's basic block. */
148 reg_available_p (const_rtx x
, const rtx_insn
*insn ATTRIBUTE_UNUSED
)
150 return ! REGNO_REG_SET_P (reg_set_bitmap
, REGNO (x
));
153 /* Hash a set of register REGNO.
155 Sets are hashed on the register that is set. This simplifies the PRE copy
158 ??? May need to make things more elaborate. Later, as necessary. */
161 hash_mod (int regno
, int hash_table_size
)
163 return (unsigned) regno
% hash_table_size
;
166 /* Insert assignment DEST:=SET from INSN in the hash table.
167 DEST is a register and SET is a register or a suitable constant.
168 If the assignment is already present in the table, record it as
169 the last occurrence in INSN's basic block.
170 IMPLICIT is true if it's an implicit set, false otherwise. */
173 insert_set_in_table (rtx dest
, rtx src
, rtx_insn
*insn
,
174 struct hash_table_d
*table
, bool implicit
)
178 struct cprop_expr
*cur_expr
, *last_expr
= NULL
;
179 struct cprop_occr
*cur_occr
;
181 hash
= hash_mod (REGNO (dest
), table
->size
);
183 for (cur_expr
= table
->table
[hash
]; cur_expr
;
184 cur_expr
= cur_expr
->next_same_hash
)
186 if (dest
== cur_expr
->dest
187 && src
== cur_expr
->src
)
192 last_expr
= cur_expr
;
197 cur_expr
= GOBNEW (struct cprop_expr
);
198 bytes_used
+= sizeof (struct cprop_expr
);
199 if (table
->table
[hash
] == NULL
)
200 /* This is the first pattern that hashed to this index. */
201 table
->table
[hash
] = cur_expr
;
203 /* Add EXPR to end of this hash chain. */
204 last_expr
->next_same_hash
= cur_expr
;
206 /* Set the fields of the expr element.
207 We must copy X because it can be modified when copy propagation is
208 performed on its operands. */
209 cur_expr
->dest
= copy_rtx (dest
);
210 cur_expr
->src
= copy_rtx (src
);
211 cur_expr
->bitmap_index
= table
->n_elems
++;
212 cur_expr
->next_same_hash
= NULL
;
213 cur_expr
->avail_occr
= NULL
;
216 /* Now record the occurrence. */
217 cur_occr
= cur_expr
->avail_occr
;
220 && BLOCK_FOR_INSN (cur_occr
->insn
) == BLOCK_FOR_INSN (insn
))
222 /* Found another instance of the expression in the same basic block.
223 Prefer this occurrence to the currently recorded one. We want
224 the last one in the block and the block is scanned from start
226 cur_occr
->insn
= insn
;
230 /* First occurrence of this expression in this basic block. */
231 cur_occr
= GOBNEW (struct cprop_occr
);
232 bytes_used
+= sizeof (struct cprop_occr
);
233 cur_occr
->insn
= insn
;
234 cur_occr
->next
= cur_expr
->avail_occr
;
235 cur_expr
->avail_occr
= cur_occr
;
238 /* Record bitmap_index of the implicit set in implicit_set_indexes. */
240 implicit_set_indexes
[BLOCK_FOR_INSN (insn
)->index
]
241 = cur_expr
->bitmap_index
;
244 /* Determine whether the rtx X should be treated as a constant for CPROP.
245 Since X might be inserted more than once we have to take care that it
249 cprop_constant_p (const_rtx x
)
251 return CONSTANT_P (x
) && (GET_CODE (x
) != CONST
|| shared_const_p (x
));
254 /* Determine whether the rtx X should be treated as a register that can
255 be propagated. Any pseudo-register is fine. */
258 cprop_reg_p (const_rtx x
)
260 return REG_P (x
) && !HARD_REGISTER_P (x
);
263 /* Scan SET present in INSN and add an entry to the hash TABLE.
264 IMPLICIT is true if it's an implicit set, false otherwise. */
267 hash_scan_set (rtx set
, rtx_insn
*insn
, struct hash_table_d
*table
,
270 rtx src
= SET_SRC (set
);
271 rtx dest
= SET_DEST (set
);
273 if (cprop_reg_p (dest
)
274 && reg_available_p (dest
, insn
)
275 && can_copy_p (GET_MODE (dest
)))
277 /* See if a REG_EQUAL note shows this equivalent to a simpler expression.
279 This allows us to do a single CPROP pass and still eliminate
280 redundant constants, addresses or other expressions that are
281 constructed with multiple instructions.
283 However, keep the original SRC if INSN is a simple reg-reg move. In
284 In this case, there will almost always be a REG_EQUAL note on the
285 insn that sets SRC. By recording the REG_EQUAL value here as SRC
286 for INSN, we miss copy propagation opportunities.
288 Note that this does not impede profitable constant propagations. We
289 "look through" reg-reg sets in lookup_set. */
290 rtx note
= find_reg_equal_equiv_note (insn
);
292 && REG_NOTE_KIND (note
) == REG_EQUAL
294 && cprop_constant_p (XEXP (note
, 0)))
295 src
= XEXP (note
, 0), set
= gen_rtx_SET (dest
, src
);
297 /* Record sets for constant/copy propagation. */
298 if ((cprop_reg_p (src
)
300 && reg_available_p (src
, insn
))
301 || cprop_constant_p (src
))
302 insert_set_in_table (dest
, src
, insn
, table
, implicit
);
306 /* Process INSN and add hash table entries as appropriate. */
309 hash_scan_insn (rtx_insn
*insn
, struct hash_table_d
*table
)
311 rtx pat
= PATTERN (insn
);
314 /* Pick out the sets of INSN and for other forms of instructions record
315 what's been modified. */
317 if (GET_CODE (pat
) == SET
)
318 hash_scan_set (pat
, insn
, table
, false);
319 else if (GET_CODE (pat
) == PARALLEL
)
320 for (i
= 0; i
< XVECLEN (pat
, 0); i
++)
322 rtx x
= XVECEXP (pat
, 0, i
);
324 if (GET_CODE (x
) == SET
)
325 hash_scan_set (x
, insn
, table
, false);
329 /* Dump the hash table TABLE to file FILE under the name NAME. */
332 dump_hash_table (FILE *file
, const char *name
, struct hash_table_d
*table
)
335 /* Flattened out table, so it's printed in proper order. */
336 struct cprop_expr
**flat_table
;
337 unsigned int *hash_val
;
338 struct cprop_expr
*expr
;
340 flat_table
= XCNEWVEC (struct cprop_expr
*, table
->n_elems
);
341 hash_val
= XNEWVEC (unsigned int, table
->n_elems
);
343 for (i
= 0; i
< (int) table
->size
; i
++)
344 for (expr
= table
->table
[i
]; expr
!= NULL
; expr
= expr
->next_same_hash
)
346 flat_table
[expr
->bitmap_index
] = expr
;
347 hash_val
[expr
->bitmap_index
] = i
;
350 fprintf (file
, "%s hash table (%d buckets, %d entries)\n",
351 name
, table
->size
, table
->n_elems
);
353 for (i
= 0; i
< (int) table
->n_elems
; i
++)
354 if (flat_table
[i
] != 0)
356 expr
= flat_table
[i
];
357 fprintf (file
, "Index %d (hash value %d)\n ",
358 expr
->bitmap_index
, hash_val
[i
]);
359 print_rtl (file
, expr
->dest
);
360 fprintf (file
, " := ");
361 print_rtl (file
, expr
->src
);
362 fprintf (file
, "\n");
365 fprintf (file
, "\n");
371 /* Record as unavailable all registers that are DEF operands of INSN. */
374 make_set_regs_unavailable (rtx_insn
*insn
)
378 FOR_EACH_INSN_DEF (def
, insn
)
379 SET_REGNO_REG_SET (reg_set_bitmap
, DF_REF_REGNO (def
));
382 /* Top level function to create an assignment hash table.
384 Assignment entries are placed in the hash table if
385 - they are of the form (set (pseudo-reg) src),
386 - src is something we want to perform const/copy propagation on,
387 - none of the operands or target are subsequently modified in the block
389 Currently src must be a pseudo-reg or a const_int.
391 TABLE is the table computed. */
394 compute_hash_table_work (struct hash_table_d
*table
)
398 /* Allocate vars to track sets of regs. */
399 reg_set_bitmap
= ALLOC_REG_SET (NULL
);
401 FOR_EACH_BB_FN (bb
, cfun
)
405 /* Reset tables used to keep track of what's not yet invalid [since
406 the end of the block]. */
407 CLEAR_REG_SET (reg_set_bitmap
);
409 /* Go over all insns from the last to the first. This is convenient
410 for tracking available registers, i.e. not set between INSN and
411 the end of the basic block BB. */
412 FOR_BB_INSNS_REVERSE (bb
, insn
)
414 /* Only real insns are interesting. */
415 if (!NONDEBUG_INSN_P (insn
))
418 /* Record interesting sets from INSN in the hash table. */
419 hash_scan_insn (insn
, table
);
421 /* Any registers set in INSN will make SETs above it not AVAIL. */
422 make_set_regs_unavailable (insn
);
425 /* Insert implicit sets in the hash table, pretending they appear as
426 insns at the head of the basic block. */
427 if (implicit_sets
[bb
->index
] != NULL_RTX
)
428 hash_scan_set (implicit_sets
[bb
->index
], BB_HEAD (bb
), table
, true);
431 FREE_REG_SET (reg_set_bitmap
);
434 /* Allocate space for the set/expr hash TABLE.
435 It is used to determine the number of buckets to use. */
438 alloc_hash_table (struct hash_table_d
*table
)
442 n
= get_max_insn_count ();
445 if (table
->size
< 11)
448 /* Attempt to maintain efficient use of hash table.
449 Making it an odd number is simplest for now.
450 ??? Later take some measurements. */
452 n
= table
->size
* sizeof (struct cprop_expr
*);
453 table
->table
= XNEWVAR (struct cprop_expr
*, n
);
456 /* Free things allocated by alloc_hash_table. */
459 free_hash_table (struct hash_table_d
*table
)
464 /* Compute the hash TABLE for doing copy/const propagation or
465 expression hash table. */
468 compute_hash_table (struct hash_table_d
*table
)
470 /* Initialize count of number of entries in hash table. */
472 memset (table
->table
, 0, table
->size
* sizeof (struct cprop_expr
*));
474 compute_hash_table_work (table
);
477 /* Expression tracking support. */
479 /* Lookup REGNO in the set TABLE. The result is a pointer to the
480 table entry, or NULL if not found. */
482 static struct cprop_expr
*
483 lookup_set (unsigned int regno
, struct hash_table_d
*table
)
485 unsigned int hash
= hash_mod (regno
, table
->size
);
486 struct cprop_expr
*expr
;
488 expr
= table
->table
[hash
];
490 while (expr
&& REGNO (expr
->dest
) != regno
)
491 expr
= expr
->next_same_hash
;
496 /* Return the next entry for REGNO in list EXPR. */
498 static struct cprop_expr
*
499 next_set (unsigned int regno
, struct cprop_expr
*expr
)
502 expr
= expr
->next_same_hash
;
503 while (expr
&& REGNO (expr
->dest
) != regno
);
508 /* Reset tables used to keep track of what's still available [since the
509 start of the block]. */
512 reset_opr_set_tables (void)
514 /* Maintain a bitmap of which regs have been set since beginning of
516 CLEAR_REG_SET (reg_set_bitmap
);
519 /* Return nonzero if the register X has not been set yet [since the
520 start of the basic block containing INSN]. */
523 reg_not_set_p (const_rtx x
, const rtx_insn
*insn ATTRIBUTE_UNUSED
)
525 return ! REGNO_REG_SET_P (reg_set_bitmap
, REGNO (x
));
528 /* Record things set by INSN.
529 This data is used by reg_not_set_p. */
532 mark_oprs_set (rtx_insn
*insn
)
536 FOR_EACH_INSN_DEF (def
, insn
)
537 SET_REGNO_REG_SET (reg_set_bitmap
, DF_REF_REGNO (def
));
540 /* Compute copy/constant propagation working variables. */
542 /* Local properties of assignments. */
543 static sbitmap
*cprop_avloc
;
544 static sbitmap
*cprop_kill
;
546 /* Global properties of assignments (computed from the local properties). */
547 static sbitmap
*cprop_avin
;
548 static sbitmap
*cprop_avout
;
550 /* Allocate vars used for copy/const propagation. N_BLOCKS is the number of
551 basic blocks. N_SETS is the number of sets. */
554 alloc_cprop_mem (int n_blocks
, int n_sets
)
556 cprop_avloc
= sbitmap_vector_alloc (n_blocks
, n_sets
);
557 cprop_kill
= sbitmap_vector_alloc (n_blocks
, n_sets
);
559 cprop_avin
= sbitmap_vector_alloc (n_blocks
, n_sets
);
560 cprop_avout
= sbitmap_vector_alloc (n_blocks
, n_sets
);
563 /* Free vars used by copy/const propagation. */
566 free_cprop_mem (void)
568 sbitmap_vector_free (cprop_avloc
);
569 sbitmap_vector_free (cprop_kill
);
570 sbitmap_vector_free (cprop_avin
);
571 sbitmap_vector_free (cprop_avout
);
574 /* Compute the local properties of each recorded expression.
576 Local properties are those that are defined by the block, irrespective of
579 An expression is killed in a block if its operands, either DEST or SRC, are
580 modified in the block.
582 An expression is computed (locally available) in a block if it is computed
583 at least once and expression would contain the same value if the
584 computation was moved to the end of the block.
586 KILL and COMP are destination sbitmaps for recording local properties. */
589 compute_local_properties (sbitmap
*kill
, sbitmap
*comp
,
590 struct hash_table_d
*table
)
594 /* Initialize the bitmaps that were passed in. */
595 bitmap_vector_clear (kill
, last_basic_block_for_fn (cfun
));
596 bitmap_vector_clear (comp
, last_basic_block_for_fn (cfun
));
598 for (i
= 0; i
< table
->size
; i
++)
600 struct cprop_expr
*expr
;
602 for (expr
= table
->table
[i
]; expr
!= NULL
; expr
= expr
->next_same_hash
)
604 int indx
= expr
->bitmap_index
;
606 struct cprop_occr
*occr
;
608 /* For each definition of the destination pseudo-reg, the expression
609 is killed in the block where the definition is. */
610 for (def
= DF_REG_DEF_CHAIN (REGNO (expr
->dest
));
611 def
; def
= DF_REF_NEXT_REG (def
))
612 bitmap_set_bit (kill
[DF_REF_BB (def
)->index
], indx
);
614 /* If the source is a pseudo-reg, for each definition of the source,
615 the expression is killed in the block where the definition is. */
616 if (REG_P (expr
->src
))
617 for (def
= DF_REG_DEF_CHAIN (REGNO (expr
->src
));
618 def
; def
= DF_REF_NEXT_REG (def
))
619 bitmap_set_bit (kill
[DF_REF_BB (def
)->index
], indx
);
621 /* The occurrences recorded in avail_occr are exactly those that
622 are locally available in the block where they are. */
623 for (occr
= expr
->avail_occr
; occr
!= NULL
; occr
= occr
->next
)
625 bitmap_set_bit (comp
[BLOCK_FOR_INSN (occr
->insn
)->index
], indx
);
631 /* Hash table support. */
633 /* Top level routine to do the dataflow analysis needed by copy/const
637 compute_cprop_data (void)
641 compute_local_properties (cprop_kill
, cprop_avloc
, &set_hash_table
);
642 compute_available (cprop_avloc
, cprop_kill
, cprop_avout
, cprop_avin
);
644 /* Merge implicit sets into CPROP_AVIN. They are always available at the
645 entry of their basic block. We need to do this because 1) implicit sets
646 aren't recorded for the local pass so they cannot be propagated within
647 their basic block by this pass and 2) the global pass would otherwise
648 propagate them only in the successors of their basic block. */
649 FOR_EACH_BB_FN (bb
, cfun
)
651 int index
= implicit_set_indexes
[bb
->index
];
653 bitmap_set_bit (cprop_avin
[bb
->index
], index
);
657 /* Copy/constant propagation. */
659 /* Maximum number of register uses in an insn that we handle. */
662 /* Table of uses (registers, both hard and pseudo) found in an insn.
663 Allocated statically to avoid alloc/free complexity and overhead. */
664 static rtx reg_use_table
[MAX_USES
];
666 /* Index into `reg_use_table' while building it. */
667 static unsigned reg_use_count
;
669 /* Set up a list of register numbers used in INSN. The found uses are stored
670 in `reg_use_table'. `reg_use_count' is initialized to zero before entry,
671 and contains the number of uses in the table upon exit.
673 ??? If a register appears multiple times we will record it multiple times.
674 This doesn't hurt anything but it will slow things down. */
677 find_used_regs (rtx
*xptr
, void *data ATTRIBUTE_UNUSED
)
684 /* repeat is used to turn tail-recursion into iteration since GCC
685 can't do it when there's no return value. */
693 if (reg_use_count
== MAX_USES
)
696 reg_use_table
[reg_use_count
] = x
;
700 /* Recursively scan the operands of this expression. */
702 for (i
= GET_RTX_LENGTH (code
) - 1, fmt
= GET_RTX_FORMAT (code
); i
>= 0; i
--)
706 /* If we are about to do the last recursive call
707 needed at this level, change it into iteration.
708 This function is called enough to be worth it. */
715 find_used_regs (&XEXP (x
, i
), data
);
717 else if (fmt
[i
] == 'E')
718 for (j
= 0; j
< XVECLEN (x
, i
); j
++)
719 find_used_regs (&XVECEXP (x
, i
, j
), data
);
723 /* Try to replace all uses of FROM in INSN with TO.
724 Return nonzero if successful. */
727 try_replace_reg (rtx from
, rtx to
, rtx_insn
*insn
)
729 rtx note
= find_reg_equal_equiv_note (insn
);
732 rtx set
= single_set (insn
);
734 bool check_rtx_costs
= true;
735 bool speed
= optimize_bb_for_speed_p (BLOCK_FOR_INSN (insn
));
736 int old_cost
= set
? set_rtx_cost (set
, speed
) : 0;
739 || CONSTANT_P (SET_SRC (set
))
741 && REG_NOTE_KIND (note
) == REG_EQUAL
742 && (GET_CODE (XEXP (note
, 0)) == CONST
743 || CONSTANT_P (XEXP (note
, 0)))))
744 check_rtx_costs
= false;
746 /* Usually we substitute easy stuff, so we won't copy everything.
747 We however need to take care to not duplicate non-trivial CONST
751 validate_replace_src_group (from
, to
, insn
);
753 /* If TO is a constant, check the cost of the set after propagation
754 to the cost of the set before the propagation. If the cost is
755 higher, then do not replace FROM with TO. */
759 && set_rtx_cost (set
, speed
) > old_cost
)
766 if (num_changes_pending () && apply_change_group ())
769 /* Try to simplify SET_SRC if we have substituted a constant. */
770 if (success
&& set
&& CONSTANT_P (to
))
772 src
= simplify_rtx (SET_SRC (set
));
775 validate_change (insn
, &SET_SRC (set
), src
, 0);
778 /* If there is already a REG_EQUAL note, update the expression in it
779 with our replacement. */
780 if (note
!= 0 && REG_NOTE_KIND (note
) == REG_EQUAL
)
781 set_unique_reg_note (insn
, REG_EQUAL
,
782 simplify_replace_rtx (XEXP (note
, 0), from
, to
));
783 if (!success
&& set
&& reg_mentioned_p (from
, SET_SRC (set
)))
785 /* If above failed and this is a single set, try to simplify the source
786 of the set given our substitution. We could perhaps try this for
787 multiple SETs, but it probably won't buy us anything. */
788 src
= simplify_replace_rtx (SET_SRC (set
), from
, to
);
790 if (!rtx_equal_p (src
, SET_SRC (set
))
791 && validate_change (insn
, &SET_SRC (set
), src
, 0))
794 /* If we've failed perform the replacement, have a single SET to
795 a REG destination and don't yet have a note, add a REG_EQUAL note
796 to not lose information. */
797 if (!success
&& note
== 0 && set
!= 0 && REG_P (SET_DEST (set
)))
798 note
= set_unique_reg_note (insn
, REG_EQUAL
, copy_rtx (src
));
801 if (set
&& MEM_P (SET_DEST (set
)) && reg_mentioned_p (from
, SET_DEST (set
)))
803 /* Registers can also appear as uses in SET_DEST if it is a MEM.
804 We could perhaps try this for multiple SETs, but it probably
805 won't buy us anything. */
806 rtx dest
= simplify_replace_rtx (SET_DEST (set
), from
, to
);
808 if (!rtx_equal_p (dest
, SET_DEST (set
))
809 && validate_change (insn
, &SET_DEST (set
), dest
, 0))
813 /* REG_EQUAL may get simplified into register.
814 We don't allow that. Remove that note. This code ought
815 not to happen, because previous code ought to synthesize
816 reg-reg move, but be on the safe side. */
817 if (note
&& REG_NOTE_KIND (note
) == REG_EQUAL
&& REG_P (XEXP (note
, 0)))
818 remove_note (insn
, note
);
823 /* Find a set of REGNOs that are available on entry to INSN's block. If found,
824 SET_RET[0] will be assigned a set with a register source and SET_RET[1] a
825 set with a constant source. If not found the corresponding entry is set to
829 find_avail_set (int regno
, rtx_insn
*insn
, struct cprop_expr
*set_ret
[2])
831 set_ret
[0] = set_ret
[1] = NULL
;
833 /* Loops are not possible here. To get a loop we would need two sets
834 available at the start of the block containing INSN. i.e. we would
835 need two sets like this available at the start of the block:
837 (set (reg X) (reg Y))
838 (set (reg Y) (reg X))
840 This can not happen since the set of (reg Y) would have killed the
841 set of (reg X) making it unavailable at the start of this block. */
845 struct cprop_expr
*set
= lookup_set (regno
, &set_hash_table
);
847 /* Find a set that is available at the start of the block
848 which contains INSN. */
851 if (bitmap_bit_p (cprop_avin
[BLOCK_FOR_INSN (insn
)->index
],
854 set
= next_set (regno
, set
);
857 /* If no available set was found we've reached the end of the
858 (possibly empty) copy chain. */
864 /* We know the set is available.
865 Now check that SRC is locally anticipatable (i.e. none of the
866 source operands have changed since the start of the block).
868 If the source operand changed, we may still use it for the next
869 iteration of this loop, but we may not use it for substitutions. */
871 if (cprop_constant_p (src
))
873 else if (reg_not_set_p (src
, insn
))
876 /* If the source of the set is anything except a register, then
877 we have reached the end of the copy chain. */
881 /* Follow the copy chain, i.e. start another iteration of the loop
882 and see if we have an available copy into SRC. */
887 /* Subroutine of cprop_insn that tries to propagate constants into
888 JUMP_INSNS. JUMP must be a conditional jump. If SETCC is non-NULL
889 it is the instruction that immediately precedes JUMP, and must be a
890 single SET of a register. FROM is what we will try to replace,
891 SRC is the constant we will try to substitute for it. Return nonzero
892 if a change was made. */
895 cprop_jump (basic_block bb
, rtx_insn
*setcc
, rtx_insn
*jump
, rtx from
, rtx src
)
897 rtx new_rtx
, set_src
, note_src
;
898 rtx set
= pc_set (jump
);
899 rtx note
= find_reg_equal_equiv_note (jump
);
903 note_src
= XEXP (note
, 0);
904 if (GET_CODE (note_src
) == EXPR_LIST
)
907 else note_src
= NULL_RTX
;
909 /* Prefer REG_EQUAL notes except those containing EXPR_LISTs. */
910 set_src
= note_src
? note_src
: SET_SRC (set
);
912 /* First substitute the SETCC condition into the JUMP instruction,
913 then substitute that given values into this expanded JUMP. */
914 if (setcc
!= NULL_RTX
915 && !modified_between_p (from
, setcc
, jump
)
916 && !modified_between_p (src
, setcc
, jump
))
919 rtx setcc_set
= single_set (setcc
);
920 rtx setcc_note
= find_reg_equal_equiv_note (setcc
);
921 setcc_src
= (setcc_note
&& GET_CODE (XEXP (setcc_note
, 0)) != EXPR_LIST
)
922 ? XEXP (setcc_note
, 0) : SET_SRC (setcc_set
);
923 set_src
= simplify_replace_rtx (set_src
, SET_DEST (setcc_set
),
929 new_rtx
= simplify_replace_rtx (set_src
, from
, src
);
931 /* If no simplification can be made, then try the next register. */
932 if (rtx_equal_p (new_rtx
, SET_SRC (set
)))
935 /* If this is now a no-op delete it, otherwise this must be a valid insn. */
936 if (new_rtx
== pc_rtx
)
940 /* Ensure the value computed inside the jump insn to be equivalent
941 to one computed by setcc. */
942 if (setcc
&& modified_in_p (new_rtx
, setcc
))
944 if (! validate_unshare_change (jump
, &SET_SRC (set
), new_rtx
, 0))
946 /* When (some) constants are not valid in a comparison, and there
947 are two registers to be replaced by constants before the entire
948 comparison can be folded into a constant, we need to keep
949 intermediate information in REG_EQUAL notes. For targets with
950 separate compare insns, such notes are added by try_replace_reg.
951 When we have a combined compare-and-branch instruction, however,
952 we need to attach a note to the branch itself to make this
953 optimization work. */
955 if (!rtx_equal_p (new_rtx
, note_src
))
956 set_unique_reg_note (jump
, REG_EQUAL
, copy_rtx (new_rtx
));
960 /* Remove REG_EQUAL note after simplification. */
962 remove_note (jump
, note
);
965 /* Delete the cc0 setter. */
966 if (HAVE_cc0
&& setcc
!= NULL
&& CC0_P (SET_DEST (single_set (setcc
))))
969 global_const_prop_count
++;
970 if (dump_file
!= NULL
)
973 "GLOBAL CONST-PROP: Replacing reg %d in jump_insn %d with"
974 "constant ", REGNO (from
), INSN_UID (jump
));
975 print_rtl (dump_file
, src
);
976 fprintf (dump_file
, "\n");
978 purge_dead_edges (bb
);
980 /* If a conditional jump has been changed into unconditional jump, remove
981 the jump and make the edge fallthru - this is always called in
983 if (new_rtx
!= pc_rtx
&& simplejump_p (jump
))
988 FOR_EACH_EDGE (e
, ei
, bb
->succs
)
989 if (e
->dest
!= EXIT_BLOCK_PTR_FOR_FN (cfun
)
990 && BB_HEAD (e
->dest
) == JUMP_LABEL (jump
))
992 e
->flags
|= EDGE_FALLTHRU
;
1001 /* Subroutine of cprop_insn that tries to propagate constants. FROM is what
1002 we will try to replace, SRC is the constant we will try to substitute for
1003 it and INSN is the instruction where this will be happening. */
1006 constprop_register (rtx from
, rtx src
, rtx_insn
*insn
)
1010 /* Check for reg or cc0 setting instructions followed by
1011 conditional branch instructions first. */
1012 if ((sset
= single_set (insn
)) != NULL
1014 && any_condjump_p (NEXT_INSN (insn
)) && onlyjump_p (NEXT_INSN (insn
)))
1016 rtx dest
= SET_DEST (sset
);
1017 if ((REG_P (dest
) || CC0_P (dest
))
1018 && cprop_jump (BLOCK_FOR_INSN (insn
), insn
, NEXT_INSN (insn
),
1023 /* Handle normal insns next. */
1024 if (NONJUMP_INSN_P (insn
) && try_replace_reg (from
, src
, insn
))
1027 /* Try to propagate a CONST_INT into a conditional jump.
1028 We're pretty specific about what we will handle in this
1029 code, we can extend this as necessary over time.
1031 Right now the insn in question must look like
1032 (set (pc) (if_then_else ...)) */
1033 else if (any_condjump_p (insn
) && onlyjump_p (insn
))
1034 return cprop_jump (BLOCK_FOR_INSN (insn
), NULL
, insn
, from
, src
);
1038 /* Perform constant and copy propagation on INSN.
1039 Return nonzero if a change was made. */
1042 cprop_insn (rtx_insn
*insn
)
1045 int changed
= 0, changed_this_round
;
1050 changed_this_round
= 0;
1052 note_uses (&PATTERN (insn
), find_used_regs
, NULL
);
1054 /* We may win even when propagating constants into notes. */
1055 note
= find_reg_equal_equiv_note (insn
);
1057 find_used_regs (&XEXP (note
, 0), NULL
);
1059 for (i
= 0; i
< reg_use_count
; i
++)
1061 rtx reg_used
= reg_use_table
[i
];
1062 unsigned int regno
= REGNO (reg_used
);
1063 rtx src_cst
= NULL
, src_reg
= NULL
;
1064 struct cprop_expr
*set
[2];
1066 /* If the register has already been set in this block, there's
1067 nothing we can do. */
1068 if (! reg_not_set_p (reg_used
, insn
))
1071 /* Find an assignment that sets reg_used and is available
1072 at the start of the block. */
1073 find_avail_set (regno
, insn
, set
);
1075 src_reg
= set
[0]->src
;
1077 src_cst
= set
[1]->src
;
1079 /* Constant propagation. */
1080 if (src_cst
&& cprop_constant_p (src_cst
)
1081 && constprop_register (reg_used
, src_cst
, insn
))
1083 changed_this_round
= changed
= 1;
1084 global_const_prop_count
++;
1085 if (dump_file
!= NULL
)
1088 "GLOBAL CONST-PROP: Replacing reg %d in ", regno
);
1089 fprintf (dump_file
, "insn %d with constant ",
1091 print_rtl (dump_file
, src_cst
);
1092 fprintf (dump_file
, "\n");
1094 if (insn
->deleted ())
1097 /* Copy propagation. */
1098 else if (src_reg
&& cprop_reg_p (src_reg
)
1099 && REGNO (src_reg
) != regno
1100 && try_replace_reg (reg_used
, src_reg
, insn
))
1102 changed_this_round
= changed
= 1;
1103 global_copy_prop_count
++;
1104 if (dump_file
!= NULL
)
1107 "GLOBAL COPY-PROP: Replacing reg %d in insn %d",
1108 regno
, INSN_UID (insn
));
1109 fprintf (dump_file
, " with reg %d\n", REGNO (src_reg
));
1112 /* The original insn setting reg_used may or may not now be
1113 deletable. We leave the deletion to DCE. */
1114 /* FIXME: If it turns out that the insn isn't deletable,
1115 then we may have unnecessarily extended register lifetimes
1116 and made things worse. */
1120 /* If try_replace_reg simplified the insn, the regs found by find_used_regs
1121 may not be valid anymore. Start over. */
1122 while (changed_this_round
);
1124 if (changed
&& DEBUG_INSN_P (insn
))
1130 /* Like find_used_regs, but avoid recording uses that appear in
1131 input-output contexts such as zero_extract or pre_dec. This
1132 restricts the cases we consider to those for which local cprop
1133 can legitimately make replacements. */
1136 local_cprop_find_used_regs (rtx
*xptr
, void *data
)
1143 switch (GET_CODE (x
))
1147 case STRICT_LOW_PART
:
1156 /* Can only legitimately appear this early in the context of
1157 stack pushes for function arguments, but handle all of the
1158 codes nonetheless. */
1162 /* Setting a subreg of a register larger than word_mode leaves
1163 the non-written words unchanged. */
1164 if (GET_MODE_BITSIZE (GET_MODE (SUBREG_REG (x
))) > BITS_PER_WORD
)
1172 find_used_regs (xptr
, data
);
1175 /* Try to perform local const/copy propagation on X in INSN. */
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. */
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
;
1194 for (l
= val
->locs
; l
; l
= l
->next
)
1196 rtx this_rtx
= l
->loc
;
1199 if (cprop_constant_p (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))))
1211 if (newcnst
&& constprop_register (x
, newcnst
, insn
))
1213 if (dump_file
!= NULL
)
1215 fprintf (dump_file
, "LOCAL CONST-PROP: Replacing reg %d in ",
1217 fprintf (dump_file
, "insn %d with constant ",
1219 print_rtl (dump_file
, newcnst
);
1220 fprintf (dump_file
, "\n");
1222 local_const_prop_count
++;
1225 else if (newreg
&& newreg
!= x
&& try_replace_reg (x
, newreg
, insn
))
1227 if (dump_file
!= NULL
)
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
++;
1241 /* Do local const/copy propagation (i.e. within each basic block). */
1244 local_cprop_pass (void)
1248 bool changed
= false;
1252 FOR_EACH_BB_FN (bb
, cfun
)
1254 FOR_BB_INSNS (bb
, insn
)
1258 rtx note
= find_reg_equal_equiv_note (insn
);
1262 note_uses (&PATTERN (insn
), local_cprop_find_used_regs
,
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
))
1276 if (insn
->deleted ())
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 ();
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
1313 implicit_set_cond_p (const_rtx cond
)
1318 /* COND must be either an EQ or NE comparison. */
1319 if (GET_CODE (cond
) != EQ
&& GET_CODE (cond
) != NE
)
1322 /* The first operand of COND must be a register we can propagate. */
1323 if (!cprop_reg_p (XEXP (cond
, 0)))
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
))
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
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. */
1362 find_implicit_sets (void)
1364 basic_block bb
, dest
;
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)
1378 cond
= fis_get_condition (BB_END (bb
));
1380 /* If no condition is found or if it isn't of a suitable form,
1382 if (! cond
|| ! implicit_set_cond_p (cond
))
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
))
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
));
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
;
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
);
1423 fprintf (dump_file
, "Found %d implicit sets\n", count
);
1425 /* Confess our sins. */
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
1442 static struct cprop_expr
*
1443 find_bypass_set (int regno
, int bb
)
1445 struct cprop_expr
*result
= 0;
1450 struct cprop_expr
*set
= lookup_set (regno
, &set_hash_table
);
1454 if (bitmap_bit_p (cprop_avout
[bb
], set
->bitmap_index
))
1456 set
= next_set (regno
, set
);
1463 if (cprop_constant_p (src
))
1469 regno
= REGNO (src
);
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. */
1481 reg_killed_on_edge (const_rtx reg
, const_edge e
)
1485 for (insn
= e
->insns
.r
; insn
; insn
= NEXT_INSN (insn
))
1486 if (INSN_P (insn
) && reg_set_p (reg
, insn
))
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. */
1503 bypass_block (basic_block bb
, rtx_insn
*setcc
, rtx_insn
*jump
)
1509 int may_be_loop_header
= false;
1514 insn
= (setcc
!= NULL
) ? setcc
: jump
;
1516 /* Determine set of register uses in INSN. */
1518 note_uses (&PATTERN (insn
), find_used_regs
, NULL
);
1519 note
= find_reg_equal_equiv_note (insn
);
1521 find_used_regs (&XEXP (note
, 0), NULL
);
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
)
1533 FOR_EACH_EDGE (e
, ei
, bb
->preds
)
1534 if (e
->flags
& EDGE_DFS_BACK
)
1536 may_be_loop_header
= true;
1542 for (ei
= ei_start (bb
->preds
); (e
= ei_safe_edge (ei
)); )
1546 if (e
->flags
& EDGE_COMPLEX
)
1552 /* We can't redirect edges from new basic blocks. */
1553 if (e
->src
->index
>= bypass_last_basic_block
)
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
))
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
;
1577 set
= find_bypass_set (regno
, e
->src
->index
);
1582 /* Check the data flow is valid after edge insertions. */
1583 if (e
->insns
.r
&& reg_killed_on_edge (reg_used
, e
))
1586 src
= SET_SRC (pc_set (jump
));
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
)
1615 /* Avoid unification of the edge with other edges from original
1616 branch. We would end up emitting the instruction on "both"
1618 if (dest
&& setcc
&& !CC0_P (SET_DEST (PATTERN (setcc
)))
1619 && find_edge (e
->src
, 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. */
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
);
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. */
1668 bypass_conditional_jumps (void)
1676 /* Note we start at block 1. */
1677 if (ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
== EXIT_BLOCK_PTR_FOR_FN (cfun
))
1680 bypass_last_basic_block
= last_basic_block_for_fn (cfun
);
1681 mark_dfs_back_edges ();
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
))
1691 FOR_BB_INSNS (bb
, insn
)
1692 if (DEBUG_INSN_P (insn
))
1694 else if (NONJUMP_INSN_P (insn
))
1698 if (GET_CODE (PATTERN (insn
)) != SET
)
1701 dest
= SET_DEST (PATTERN (insn
));
1702 if (REG_P (dest
) || CC0_P (dest
))
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
);
1714 else if (INSN_P (insn
))
1719 /* If we bypassed any register setting insns, we inserted a
1720 copy on the redirected edge. These need to be committed. */
1722 commit_edge_insertions ();
1727 /* Return true if the graph is too expensive to optimize. PASS is the
1728 optimization about to be performed. */
1731 is_too_expensive (const char *pass
)
1733 /* Trying to perform global optimizations on flow graphs which have
1734 a high connectivity will take a long time and is unlikely to be
1735 particularly useful.
1737 In normal circumstances a cfg should have about twice as many
1738 edges as blocks. But we do not want to punish small functions
1739 which have a couple switch statements. Rather than simply
1740 threshold the number of blocks, uses something with a more
1741 graceful degradation. */
1742 if (n_edges_for_fn (cfun
) > 20000 + n_basic_blocks_for_fn (cfun
) * 4)
1744 warning (OPT_Wdisabled_optimization
,
1745 "%s: %d basic blocks and %d edges/basic block",
1746 pass
, n_basic_blocks_for_fn (cfun
),
1747 n_edges_for_fn (cfun
) / n_basic_blocks_for_fn (cfun
));
1752 /* If allocating memory for the cprop bitmap would take up too much
1753 storage it's better just to disable the optimization. */
1754 if ((n_basic_blocks_for_fn (cfun
)
1755 * SBITMAP_SET_SIZE (max_reg_num ())
1756 * sizeof (SBITMAP_ELT_TYPE
)) > MAX_GCSE_MEMORY
)
1758 warning (OPT_Wdisabled_optimization
,
1759 "%s: %d basic blocks and %d registers",
1760 pass
, n_basic_blocks_for_fn (cfun
), max_reg_num ());
1768 /* Main function for the CPROP pass. */
1771 one_cprop_pass (void)
1776 /* Return if there's nothing to do, or it is too expensive. */
1777 if (n_basic_blocks_for_fn (cfun
) <= NUM_FIXED_BLOCKS
+ 1
1778 || is_too_expensive (_ ("const/copy propagation disabled")))
1781 global_const_prop_count
= local_const_prop_count
= 0;
1782 global_copy_prop_count
= local_copy_prop_count
= 0;
1785 gcc_obstack_init (&cprop_obstack
);
1787 /* Do a local const/copy propagation pass first. The global pass
1788 only handles global opportunities.
1789 If the local pass changes something, remove any unreachable blocks
1790 because the CPROP global dataflow analysis may get into infinite
1791 loops for CFGs with unreachable blocks.
1793 FIXME: This local pass should not be necessary after CSE (but for
1794 some reason it still is). It is also (proven) not necessary
1795 to run the local pass right after FWPWOP.
1797 FIXME: The global analysis would not get into infinite loops if it
1798 would use the DF solver (via df_simple_dataflow) instead of
1799 the solver implemented in this file. */
1800 changed
|= local_cprop_pass ();
1802 delete_unreachable_blocks ();
1804 /* Determine implicit sets. This may change the CFG (split critical
1805 edges if that exposes an implicit set).
1806 Note that find_implicit_sets() does not rely on up-to-date DF caches
1807 so that we do not have to re-run df_analyze() even if local CPROP
1809 ??? This could run earlier so that any uncovered implicit sets
1810 sets could be exploited in local_cprop_pass() also. Later. */
1811 changed
|= find_implicit_sets ();
1813 /* If local_cprop_pass() or find_implicit_sets() changed something,
1814 run df_analyze() to bring all insn caches up-to-date, and to take
1815 new basic blocks from edge splitting on the DF radar.
1816 NB: This also runs the fast DCE pass, because execute_rtl_cprop
1817 sets DF_LR_RUN_DCE. */
1821 /* Initialize implicit_set_indexes array. */
1822 implicit_set_indexes
= XNEWVEC (int, last_basic_block_for_fn (cfun
));
1823 for (i
= 0; i
< last_basic_block_for_fn (cfun
); i
++)
1824 implicit_set_indexes
[i
] = -1;
1826 alloc_hash_table (&set_hash_table
);
1827 compute_hash_table (&set_hash_table
);
1829 /* Free implicit_sets before peak usage. */
1830 free (implicit_sets
);
1831 implicit_sets
= NULL
;
1834 dump_hash_table (dump_file
, "SET", &set_hash_table
);
1835 if (set_hash_table
.n_elems
> 0)
1840 alloc_cprop_mem (last_basic_block_for_fn (cfun
),
1841 set_hash_table
.n_elems
);
1842 compute_cprop_data ();
1844 free (implicit_set_indexes
);
1845 implicit_set_indexes
= NULL
;
1847 /* Allocate vars to track sets of regs. */
1848 reg_set_bitmap
= ALLOC_REG_SET (NULL
);
1850 FOR_BB_BETWEEN (bb
, ENTRY_BLOCK_PTR_FOR_FN (cfun
)->next_bb
->next_bb
,
1851 EXIT_BLOCK_PTR_FOR_FN (cfun
),
1854 /* Reset tables used to keep track of what's still valid [since
1855 the start of the block]. */
1856 reset_opr_set_tables ();
1858 FOR_BB_INSNS (bb
, insn
)
1861 changed
|= cprop_insn (insn
);
1863 /* Keep track of everything modified by this insn. */
1864 /* ??? Need to be careful w.r.t. mods done to INSN.
1865 Don't call mark_oprs_set if we turned the
1866 insn into a NOTE, or deleted the insn. */
1867 if (! NOTE_P (insn
) && ! insn
->deleted ())
1868 mark_oprs_set (insn
);
1872 changed
|= bypass_conditional_jumps ();
1874 FREE_REG_SET (reg_set_bitmap
);
1879 free (implicit_set_indexes
);
1880 implicit_set_indexes
= NULL
;
1883 free_hash_table (&set_hash_table
);
1884 obstack_free (&cprop_obstack
, NULL
);
1888 fprintf (dump_file
, "CPROP of %s, %d basic blocks, %d bytes needed, ",
1889 current_function_name (), n_basic_blocks_for_fn (cfun
),
1891 fprintf (dump_file
, "%d local const props, %d local copy props, ",
1892 local_const_prop_count
, local_copy_prop_count
);
1893 fprintf (dump_file
, "%d global const props, %d global copy props\n\n",
1894 global_const_prop_count
, global_copy_prop_count
);
1900 /* All the passes implemented in this file. Each pass has its
1901 own gate and execute function, and at the end of the file a
1902 pass definition for passes.c.
1904 We do not construct an accurate cfg in functions which call
1905 setjmp, so none of these passes runs if the function calls
1907 FIXME: Should just handle setjmp via REG_SETJMP notes. */
1910 execute_rtl_cprop (void)
1913 delete_unreachable_blocks ();
1914 df_set_flags (DF_LR_RUN_DCE
);
1916 changed
= one_cprop_pass ();
1917 flag_rerun_cse_after_global_opts
|= changed
;
1919 cleanup_cfg (CLEANUP_CFG_CHANGED
);
1925 const pass_data pass_data_rtl_cprop
=
1927 RTL_PASS
, /* type */
1929 OPTGROUP_NONE
, /* optinfo_flags */
1930 TV_CPROP
, /* tv_id */
1931 PROP_cfglayout
, /* properties_required */
1932 0, /* properties_provided */
1933 0, /* properties_destroyed */
1934 0, /* todo_flags_start */
1935 TODO_df_finish
, /* todo_flags_finish */
1938 class pass_rtl_cprop
: public rtl_opt_pass
1941 pass_rtl_cprop (gcc::context
*ctxt
)
1942 : rtl_opt_pass (pass_data_rtl_cprop
, ctxt
)
1945 /* opt_pass methods: */
1946 opt_pass
* clone () { return new pass_rtl_cprop (m_ctxt
); }
1947 virtual bool gate (function
*fun
)
1949 return optimize
> 0 && flag_gcse
1950 && !fun
->calls_setjmp
1954 virtual unsigned int execute (function
*) { return execute_rtl_cprop (); }
1956 }; // class pass_rtl_cprop
1961 make_pass_rtl_cprop (gcc::context
*ctxt
)
1963 return new pass_rtl_cprop (ctxt
);