2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
32 #include "fold-const.h"
34 #include "gimple-pretty-print.h"
35 #include "tree-inline.h"
36 #include "internal-fn.h"
37 #include "gimple-fold.h"
40 #include "gimple-iterator.h"
41 #include "gimplify-me.h"
43 #include "tree-ssa-loop.h"
44 #include "tree-into-ssa.h"
46 #include "insn-config.h"
57 #include "tree-iterator.h"
58 #include "alloc-pool.h"
59 #include "tree-pass.h"
60 #include "langhooks.h"
62 #include "tree-ssa-sccvn.h"
63 #include "tree-scalar-evolution.h"
68 #include "symbol-summary.h"
70 #include "tree-ssa-propagate.h"
71 #include "ipa-utils.h"
72 #include "tree-cfgcleanup.h"
76 1. Avail sets can be shared by making an avail_find_leader that
77 walks up the dominator tree and looks in those avail sets.
78 This might affect code optimality, it's unclear right now.
79 2. Strength reduction can be performed by anticipating expressions
80 we can repair later on.
81 3. We can do back-substitution or smarter value numbering to catch
82 commutative expressions split up over multiple statements.
85 /* For ease of terminology, "expression node" in the below refers to
86 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
87 represent the actual statement containing the expressions we care about,
88 and we cache the value number by putting it in the expression. */
92 First we walk the statements to generate the AVAIL sets, the
93 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
94 generation of values/expressions by a given block. We use them
95 when computing the ANTIC sets. The AVAIL sets consist of
96 SSA_NAME's that represent values, so we know what values are
97 available in what blocks. AVAIL is a forward dataflow problem. In
98 SSA, values are never killed, so we don't need a kill set, or a
99 fixpoint iteration, in order to calculate the AVAIL sets. In
100 traditional parlance, AVAIL sets tell us the downsafety of the
103 Next, we generate the ANTIC sets. These sets represent the
104 anticipatable expressions. ANTIC is a backwards dataflow
105 problem. An expression is anticipatable in a given block if it could
106 be generated in that block. This means that if we had to perform
107 an insertion in that block, of the value of that expression, we
108 could. Calculating the ANTIC sets requires phi translation of
109 expressions, because the flow goes backwards through phis. We must
110 iterate to a fixpoint of the ANTIC sets, because we have a kill
111 set. Even in SSA form, values are not live over the entire
112 function, only from their definition point onwards. So we have to
113 remove values from the ANTIC set once we go past the definition
114 point of the leaders that make them up.
115 compute_antic/compute_antic_aux performs this computation.
117 Third, we perform insertions to make partially redundant
118 expressions fully redundant.
120 An expression is partially redundant (excluding partial
123 1. It is AVAIL in some, but not all, of the predecessors of a
125 2. It is ANTIC in all the predecessors.
127 In order to make it fully redundant, we insert the expression into
128 the predecessors where it is not available, but is ANTIC.
130 For the partial anticipation case, we only perform insertion if it
131 is partially anticipated in some block, and fully available in all
134 insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
135 performs these steps.
137 Fourth, we eliminate fully redundant expressions.
138 This is a simple statement walk that replaces redundant
139 calculations with the now available values. */
141 /* Representations of value numbers:
143 Value numbers are represented by a representative SSA_NAME. We
144 will create fake SSA_NAME's in situations where we need a
145 representative but do not have one (because it is a complex
146 expression). In order to facilitate storing the value numbers in
147 bitmaps, and keep the number of wasted SSA_NAME's down, we also
148 associate a value_id with each value number, and create full blown
149 ssa_name's only where we actually need them (IE in operands of
150 existing expressions).
152 Theoretically you could replace all the value_id's with
153 SSA_NAME_VERSION, but this would allocate a large number of
154 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
155 It would also require an additional indirection at each point we
158 /* Representation of expressions on value numbers:
160 Expressions consisting of value numbers are represented the same
161 way as our VN internally represents them, with an additional
162 "pre_expr" wrapping around them in order to facilitate storing all
163 of the expressions in the same sets. */
165 /* Representation of sets:
167 The dataflow sets do not need to be sorted in any particular order
168 for the majority of their lifetime, are simply represented as two
169 bitmaps, one that keeps track of values present in the set, and one
170 that keeps track of expressions present in the set.
172 When we need them in topological order, we produce it on demand by
173 transforming the bitmap into an array and sorting it into topo
176 /* Type of expression, used to know which member of the PRE_EXPR union
192 vn_reference_t reference
;
195 typedef struct pre_expr_d
: nofree_ptr_hash
<pre_expr_d
>
197 enum pre_expr_kind kind
;
201 /* hash_table support. */
202 static inline hashval_t
hash (const pre_expr_d
*);
203 static inline int equal (const pre_expr_d
*, const pre_expr_d
*);
206 #define PRE_EXPR_NAME(e) (e)->u.name
207 #define PRE_EXPR_NARY(e) (e)->u.nary
208 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
209 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
211 /* Compare E1 and E1 for equality. */
214 pre_expr_d::equal (const pre_expr_d
*e1
, const pre_expr_d
*e2
)
216 if (e1
->kind
!= e2
->kind
)
222 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1
),
223 PRE_EXPR_CONSTANT (e2
));
225 return PRE_EXPR_NAME (e1
) == PRE_EXPR_NAME (e2
);
227 return vn_nary_op_eq (PRE_EXPR_NARY (e1
), PRE_EXPR_NARY (e2
));
229 return vn_reference_eq (PRE_EXPR_REFERENCE (e1
),
230 PRE_EXPR_REFERENCE (e2
));
239 pre_expr_d::hash (const pre_expr_d
*e
)
244 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e
));
246 return SSA_NAME_VERSION (PRE_EXPR_NAME (e
));
248 return PRE_EXPR_NARY (e
)->hashcode
;
250 return PRE_EXPR_REFERENCE (e
)->hashcode
;
256 /* Next global expression id number. */
257 static unsigned int next_expression_id
;
259 /* Mapping from expression to id number we can use in bitmap sets. */
260 static vec
<pre_expr
> expressions
;
261 static hash_table
<pre_expr_d
> *expression_to_id
;
262 static vec
<unsigned> name_to_id
;
264 /* Allocate an expression id for EXPR. */
266 static inline unsigned int
267 alloc_expression_id (pre_expr expr
)
269 struct pre_expr_d
**slot
;
270 /* Make sure we won't overflow. */
271 gcc_assert (next_expression_id
+ 1 > next_expression_id
);
272 expr
->id
= next_expression_id
++;
273 expressions
.safe_push (expr
);
274 if (expr
->kind
== NAME
)
276 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
277 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
278 re-allocations by using vec::reserve upfront. */
279 unsigned old_len
= name_to_id
.length ();
280 name_to_id
.reserve (num_ssa_names
- old_len
);
281 name_to_id
.quick_grow_cleared (num_ssa_names
);
282 gcc_assert (name_to_id
[version
] == 0);
283 name_to_id
[version
] = expr
->id
;
287 slot
= expression_to_id
->find_slot (expr
, INSERT
);
291 return next_expression_id
- 1;
294 /* Return the expression id for tree EXPR. */
296 static inline unsigned int
297 get_expression_id (const pre_expr expr
)
302 static inline unsigned int
303 lookup_expression_id (const pre_expr expr
)
305 struct pre_expr_d
**slot
;
307 if (expr
->kind
== NAME
)
309 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
310 if (name_to_id
.length () <= version
)
312 return name_to_id
[version
];
316 slot
= expression_to_id
->find_slot (expr
, NO_INSERT
);
319 return ((pre_expr
)*slot
)->id
;
323 /* Return the existing expression id for EXPR, or create one if one
324 does not exist yet. */
326 static inline unsigned int
327 get_or_alloc_expression_id (pre_expr expr
)
329 unsigned int id
= lookup_expression_id (expr
);
331 return alloc_expression_id (expr
);
332 return expr
->id
= id
;
335 /* Return the expression that has expression id ID */
337 static inline pre_expr
338 expression_for_id (unsigned int id
)
340 return expressions
[id
];
343 /* Free the expression id field in all of our expressions,
344 and then destroy the expressions array. */
347 clear_expression_ids (void)
349 expressions
.release ();
352 static object_allocator
<pre_expr_d
> pre_expr_pool ("pre_expr nodes");
354 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
357 get_or_alloc_expr_for_name (tree name
)
359 struct pre_expr_d expr
;
361 unsigned int result_id
;
365 PRE_EXPR_NAME (&expr
) = name
;
366 result_id
= lookup_expression_id (&expr
);
368 return expression_for_id (result_id
);
370 result
= pre_expr_pool
.allocate ();
372 PRE_EXPR_NAME (result
) = name
;
373 alloc_expression_id (result
);
377 /* An unordered bitmap set. One bitmap tracks values, the other,
379 typedef struct bitmap_set
381 bitmap_head expressions
;
385 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
386 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
388 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
389 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
391 /* Mapping from value id to expressions with that value_id. */
392 static vec
<bitmap
> value_expressions
;
394 /* Sets that we need to keep track of. */
395 typedef struct bb_bitmap_sets
397 /* The EXP_GEN set, which represents expressions/values generated in
399 bitmap_set_t exp_gen
;
401 /* The PHI_GEN set, which represents PHI results generated in a
403 bitmap_set_t phi_gen
;
405 /* The TMP_GEN set, which represents results/temporaries generated
406 in a basic block. IE the LHS of an expression. */
407 bitmap_set_t tmp_gen
;
409 /* The AVAIL_OUT set, which represents which values are available in
410 a given basic block. */
411 bitmap_set_t avail_out
;
413 /* The ANTIC_IN set, which represents which values are anticipatable
414 in a given basic block. */
415 bitmap_set_t antic_in
;
417 /* The PA_IN set, which represents which values are
418 partially anticipatable in a given basic block. */
421 /* The NEW_SETS set, which is used during insertion to augment the
422 AVAIL_OUT set of blocks with the new insertions performed during
423 the current iteration. */
424 bitmap_set_t new_sets
;
426 /* A cache for value_dies_in_block_x. */
429 /* The live virtual operand on successor edges. */
432 /* True if we have visited this block during ANTIC calculation. */
433 unsigned int visited
: 1;
435 /* True when the block contains a call that might not return. */
436 unsigned int contains_may_not_return_call
: 1;
439 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
440 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
441 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
442 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
443 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
444 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
445 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
446 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
447 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
448 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
449 #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
452 /* Basic block list in postorder. */
453 static int *postorder
;
454 static int postorder_num
;
456 /* This structure is used to keep track of statistics on what
457 optimization PRE was able to perform. */
460 /* The number of RHS computations eliminated by PRE. */
463 /* The number of new expressions/temporaries generated by PRE. */
466 /* The number of inserts found due to partial anticipation */
469 /* The number of new PHI nodes added by PRE. */
473 static bool do_partial_partial
;
474 static pre_expr
bitmap_find_leader (bitmap_set_t
, unsigned int);
475 static void bitmap_value_insert_into_set (bitmap_set_t
, pre_expr
);
476 static void bitmap_value_replace_in_set (bitmap_set_t
, pre_expr
);
477 static void bitmap_set_copy (bitmap_set_t
, bitmap_set_t
);
478 static bool bitmap_set_contains_value (bitmap_set_t
, unsigned int);
479 static void bitmap_insert_into_set (bitmap_set_t
, pre_expr
);
480 static void bitmap_insert_into_set_1 (bitmap_set_t
, pre_expr
,
482 static bitmap_set_t
bitmap_set_new (void);
483 static tree
create_expression_by_pieces (basic_block
, pre_expr
, gimple_seq
*,
485 static tree
find_or_generate_expression (basic_block
, tree
, gimple_seq
*);
486 static unsigned int get_expr_value_id (pre_expr
);
488 /* We can add and remove elements and entries to and from sets
489 and hash tables, so we use alloc pools for them. */
491 static object_allocator
<bitmap_set
> bitmap_set_pool ("Bitmap sets");
492 static bitmap_obstack grand_bitmap_obstack
;
494 /* Set of blocks with statements that have had their EH properties changed. */
495 static bitmap need_eh_cleanup
;
497 /* Set of blocks with statements that have had their AB properties changed. */
498 static bitmap need_ab_cleanup
;
500 /* A three tuple {e, pred, v} used to cache phi translations in the
501 phi_translate_table. */
503 typedef struct expr_pred_trans_d
: free_ptr_hash
<expr_pred_trans_d
>
505 /* The expression. */
508 /* The predecessor block along which we translated the expression. */
511 /* The value that resulted from the translation. */
514 /* The hashcode for the expression, pred pair. This is cached for
518 /* hash_table support. */
519 static inline hashval_t
hash (const expr_pred_trans_d
*);
520 static inline int equal (const expr_pred_trans_d
*, const expr_pred_trans_d
*);
521 } *expr_pred_trans_t
;
522 typedef const struct expr_pred_trans_d
*const_expr_pred_trans_t
;
525 expr_pred_trans_d::hash (const expr_pred_trans_d
*e
)
531 expr_pred_trans_d::equal (const expr_pred_trans_d
*ve1
,
532 const expr_pred_trans_d
*ve2
)
534 basic_block b1
= ve1
->pred
;
535 basic_block b2
= ve2
->pred
;
537 /* If they are not translations for the same basic block, they can't
541 return pre_expr_d::equal (ve1
->e
, ve2
->e
);
544 /* The phi_translate_table caches phi translations for a given
545 expression and predecessor. */
546 static hash_table
<expr_pred_trans_d
> *phi_translate_table
;
548 /* Add the tuple mapping from {expression E, basic block PRED} to
549 the phi translation table and return whether it pre-existed. */
552 phi_trans_add (expr_pred_trans_t
*entry
, pre_expr e
, basic_block pred
)
554 expr_pred_trans_t
*slot
;
555 expr_pred_trans_d tem
;
556 hashval_t hash
= iterative_hash_hashval_t (pre_expr_d::hash (e
),
561 slot
= phi_translate_table
->find_slot_with_hash (&tem
, hash
, INSERT
);
568 *entry
= *slot
= XNEW (struct expr_pred_trans_d
);
570 (*entry
)->pred
= pred
;
571 (*entry
)->hashcode
= hash
;
576 /* Add expression E to the expression set of value id V. */
579 add_to_value (unsigned int v
, pre_expr e
)
583 gcc_checking_assert (get_expr_value_id (e
) == v
);
585 if (v
>= value_expressions
.length ())
587 value_expressions
.safe_grow_cleared (v
+ 1);
590 set
= value_expressions
[v
];
593 set
= BITMAP_ALLOC (&grand_bitmap_obstack
);
594 value_expressions
[v
] = set
;
597 bitmap_set_bit (set
, get_or_alloc_expression_id (e
));
600 /* Create a new bitmap set and return it. */
603 bitmap_set_new (void)
605 bitmap_set_t ret
= bitmap_set_pool
.allocate ();
606 bitmap_initialize (&ret
->expressions
, &grand_bitmap_obstack
);
607 bitmap_initialize (&ret
->values
, &grand_bitmap_obstack
);
611 /* Return the value id for a PRE expression EXPR. */
614 get_expr_value_id (pre_expr expr
)
620 id
= get_constant_value_id (PRE_EXPR_CONSTANT (expr
));
623 id
= VN_INFO (PRE_EXPR_NAME (expr
))->value_id
;
626 id
= PRE_EXPR_NARY (expr
)->value_id
;
629 id
= PRE_EXPR_REFERENCE (expr
)->value_id
;
634 /* ??? We cannot assert that expr has a value-id (it can be 0), because
635 we assign value-ids only to expressions that have a result
636 in set_hashtable_value_ids. */
640 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
643 sccvn_valnum_from_value_id (unsigned int val
)
647 bitmap exprset
= value_expressions
[val
];
648 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
650 pre_expr vexpr
= expression_for_id (i
);
651 if (vexpr
->kind
== NAME
)
652 return VN_INFO (PRE_EXPR_NAME (vexpr
))->valnum
;
653 else if (vexpr
->kind
== CONSTANT
)
654 return PRE_EXPR_CONSTANT (vexpr
);
659 /* Remove an expression EXPR from a bitmapped set. */
662 bitmap_remove_from_set (bitmap_set_t set
, pre_expr expr
)
664 unsigned int val
= get_expr_value_id (expr
);
665 if (!value_id_constant_p (val
))
667 bitmap_clear_bit (&set
->values
, val
);
668 bitmap_clear_bit (&set
->expressions
, get_expression_id (expr
));
673 bitmap_insert_into_set_1 (bitmap_set_t set
, pre_expr expr
,
674 unsigned int val
, bool allow_constants
)
676 if (allow_constants
|| !value_id_constant_p (val
))
678 /* We specifically expect this and only this function to be able to
679 insert constants into a set. */
680 bitmap_set_bit (&set
->values
, val
);
681 bitmap_set_bit (&set
->expressions
, get_or_alloc_expression_id (expr
));
685 /* Insert an expression EXPR into a bitmapped set. */
688 bitmap_insert_into_set (bitmap_set_t set
, pre_expr expr
)
690 bitmap_insert_into_set_1 (set
, expr
, get_expr_value_id (expr
), false);
693 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
696 bitmap_set_copy (bitmap_set_t dest
, bitmap_set_t orig
)
698 bitmap_copy (&dest
->expressions
, &orig
->expressions
);
699 bitmap_copy (&dest
->values
, &orig
->values
);
703 /* Free memory used up by SET. */
705 bitmap_set_free (bitmap_set_t set
)
707 bitmap_clear (&set
->expressions
);
708 bitmap_clear (&set
->values
);
712 /* Generate an topological-ordered array of bitmap set SET. */
715 sorted_array_from_bitmap_set (bitmap_set_t set
)
718 bitmap_iterator bi
, bj
;
719 vec
<pre_expr
> result
;
721 /* Pre-allocate enough space for the array. */
722 result
.create (bitmap_count_bits (&set
->expressions
));
724 FOR_EACH_VALUE_ID_IN_SET (set
, i
, bi
)
726 /* The number of expressions having a given value is usually
727 relatively small. Thus, rather than making a vector of all
728 the expressions and sorting it by value-id, we walk the values
729 and check in the reverse mapping that tells us what expressions
730 have a given value, to filter those in our set. As a result,
731 the expressions are inserted in value-id order, which means
734 If this is somehow a significant lose for some cases, we can
735 choose which set to walk based on the set size. */
736 bitmap exprset
= value_expressions
[i
];
737 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, j
, bj
)
739 if (bitmap_bit_p (&set
->expressions
, j
))
740 result
.quick_push (expression_for_id (j
));
747 /* Perform bitmapped set operation DEST &= ORIG. */
750 bitmap_set_and (bitmap_set_t dest
, bitmap_set_t orig
)
758 bitmap_initialize (&temp
, &grand_bitmap_obstack
);
760 bitmap_and_into (&dest
->values
, &orig
->values
);
761 bitmap_copy (&temp
, &dest
->expressions
);
762 EXECUTE_IF_SET_IN_BITMAP (&temp
, 0, i
, bi
)
764 pre_expr expr
= expression_for_id (i
);
765 unsigned int value_id
= get_expr_value_id (expr
);
766 if (!bitmap_bit_p (&dest
->values
, value_id
))
767 bitmap_clear_bit (&dest
->expressions
, i
);
769 bitmap_clear (&temp
);
773 /* Subtract all values and expressions contained in ORIG from DEST. */
776 bitmap_set_subtract (bitmap_set_t dest
, bitmap_set_t orig
)
778 bitmap_set_t result
= bitmap_set_new ();
782 bitmap_and_compl (&result
->expressions
, &dest
->expressions
,
785 FOR_EACH_EXPR_ID_IN_SET (result
, i
, bi
)
787 pre_expr expr
= expression_for_id (i
);
788 unsigned int value_id
= get_expr_value_id (expr
);
789 bitmap_set_bit (&result
->values
, value_id
);
795 /* Subtract all the values in bitmap set B from bitmap set A. */
798 bitmap_set_subtract_values (bitmap_set_t a
, bitmap_set_t b
)
804 bitmap_initialize (&temp
, &grand_bitmap_obstack
);
806 bitmap_copy (&temp
, &a
->expressions
);
807 EXECUTE_IF_SET_IN_BITMAP (&temp
, 0, i
, bi
)
809 pre_expr expr
= expression_for_id (i
);
810 if (bitmap_set_contains_value (b
, get_expr_value_id (expr
)))
811 bitmap_remove_from_set (a
, expr
);
813 bitmap_clear (&temp
);
817 /* Return true if bitmapped set SET contains the value VALUE_ID. */
820 bitmap_set_contains_value (bitmap_set_t set
, unsigned int value_id
)
822 if (value_id_constant_p (value_id
))
825 if (!set
|| bitmap_empty_p (&set
->expressions
))
828 return bitmap_bit_p (&set
->values
, value_id
);
832 bitmap_set_contains_expr (bitmap_set_t set
, const pre_expr expr
)
834 return bitmap_bit_p (&set
->expressions
, get_expression_id (expr
));
837 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
840 bitmap_set_replace_value (bitmap_set_t set
, unsigned int lookfor
,
847 if (value_id_constant_p (lookfor
))
850 if (!bitmap_set_contains_value (set
, lookfor
))
853 /* The number of expressions having a given value is usually
854 significantly less than the total number of expressions in SET.
855 Thus, rather than check, for each expression in SET, whether it
856 has the value LOOKFOR, we walk the reverse mapping that tells us
857 what expressions have a given value, and see if any of those
858 expressions are in our set. For large testcases, this is about
859 5-10x faster than walking the bitmap. If this is somehow a
860 significant lose for some cases, we can choose which set to walk
861 based on the set size. */
862 exprset
= value_expressions
[lookfor
];
863 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
865 if (bitmap_clear_bit (&set
->expressions
, i
))
867 bitmap_set_bit (&set
->expressions
, get_expression_id (expr
));
875 /* Return true if two bitmap sets are equal. */
878 bitmap_set_equal (bitmap_set_t a
, bitmap_set_t b
)
880 return bitmap_equal_p (&a
->values
, &b
->values
);
883 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
884 and add it otherwise. */
887 bitmap_value_replace_in_set (bitmap_set_t set
, pre_expr expr
)
889 unsigned int val
= get_expr_value_id (expr
);
891 if (bitmap_set_contains_value (set
, val
))
892 bitmap_set_replace_value (set
, val
, expr
);
894 bitmap_insert_into_set (set
, expr
);
897 /* Insert EXPR into SET if EXPR's value is not already present in
901 bitmap_value_insert_into_set (bitmap_set_t set
, pre_expr expr
)
903 unsigned int val
= get_expr_value_id (expr
);
905 gcc_checking_assert (expr
->id
== get_or_alloc_expression_id (expr
));
907 /* Constant values are always considered to be part of the set. */
908 if (value_id_constant_p (val
))
911 /* If the value membership changed, add the expression. */
912 if (bitmap_set_bit (&set
->values
, val
))
913 bitmap_set_bit (&set
->expressions
, expr
->id
);
916 /* Print out EXPR to outfile. */
919 print_pre_expr (FILE *outfile
, const pre_expr expr
)
924 print_generic_expr (outfile
, PRE_EXPR_CONSTANT (expr
), 0);
927 print_generic_expr (outfile
, PRE_EXPR_NAME (expr
), 0);
932 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
933 fprintf (outfile
, "{%s,", get_tree_code_name (nary
->opcode
));
934 for (i
= 0; i
< nary
->length
; i
++)
936 print_generic_expr (outfile
, nary
->op
[i
], 0);
937 if (i
!= (unsigned) nary
->length
- 1)
938 fprintf (outfile
, ",");
940 fprintf (outfile
, "}");
946 vn_reference_op_t vro
;
948 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
949 fprintf (outfile
, "{");
951 ref
->operands
.iterate (i
, &vro
);
954 bool closebrace
= false;
955 if (vro
->opcode
!= SSA_NAME
956 && TREE_CODE_CLASS (vro
->opcode
) != tcc_declaration
)
958 fprintf (outfile
, "%s", get_tree_code_name (vro
->opcode
));
961 fprintf (outfile
, "<");
967 print_generic_expr (outfile
, vro
->op0
, 0);
970 fprintf (outfile
, ",");
971 print_generic_expr (outfile
, vro
->op1
, 0);
975 fprintf (outfile
, ",");
976 print_generic_expr (outfile
, vro
->op2
, 0);
980 fprintf (outfile
, ">");
981 if (i
!= ref
->operands
.length () - 1)
982 fprintf (outfile
, ",");
984 fprintf (outfile
, "}");
987 fprintf (outfile
, "@");
988 print_generic_expr (outfile
, ref
->vuse
, 0);
994 void debug_pre_expr (pre_expr
);
996 /* Like print_pre_expr but always prints to stderr. */
998 debug_pre_expr (pre_expr e
)
1000 print_pre_expr (stderr
, e
);
1001 fprintf (stderr
, "\n");
1004 /* Print out SET to OUTFILE. */
1007 print_bitmap_set (FILE *outfile
, bitmap_set_t set
,
1008 const char *setname
, int blockindex
)
1010 fprintf (outfile
, "%s[%d] := { ", setname
, blockindex
);
1017 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
1019 const pre_expr expr
= expression_for_id (i
);
1022 fprintf (outfile
, ", ");
1024 print_pre_expr (outfile
, expr
);
1026 fprintf (outfile
, " (%04d)", get_expr_value_id (expr
));
1029 fprintf (outfile
, " }\n");
1032 void debug_bitmap_set (bitmap_set_t
);
1035 debug_bitmap_set (bitmap_set_t set
)
1037 print_bitmap_set (stderr
, set
, "debug", 0);
1040 void debug_bitmap_sets_for (basic_block
);
1043 debug_bitmap_sets_for (basic_block bb
)
1045 print_bitmap_set (stderr
, AVAIL_OUT (bb
), "avail_out", bb
->index
);
1046 print_bitmap_set (stderr
, EXP_GEN (bb
), "exp_gen", bb
->index
);
1047 print_bitmap_set (stderr
, PHI_GEN (bb
), "phi_gen", bb
->index
);
1048 print_bitmap_set (stderr
, TMP_GEN (bb
), "tmp_gen", bb
->index
);
1049 print_bitmap_set (stderr
, ANTIC_IN (bb
), "antic_in", bb
->index
);
1050 if (do_partial_partial
)
1051 print_bitmap_set (stderr
, PA_IN (bb
), "pa_in", bb
->index
);
1052 print_bitmap_set (stderr
, NEW_SETS (bb
), "new_sets", bb
->index
);
1055 /* Print out the expressions that have VAL to OUTFILE. */
1058 print_value_expressions (FILE *outfile
, unsigned int val
)
1060 bitmap set
= value_expressions
[val
];
1065 sprintf (s
, "%04d", val
);
1066 x
.expressions
= *set
;
1067 print_bitmap_set (outfile
, &x
, s
, 0);
1073 debug_value_expressions (unsigned int val
)
1075 print_value_expressions (stderr
, val
);
1078 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1082 get_or_alloc_expr_for_constant (tree constant
)
1084 unsigned int result_id
;
1085 unsigned int value_id
;
1086 struct pre_expr_d expr
;
1089 expr
.kind
= CONSTANT
;
1090 PRE_EXPR_CONSTANT (&expr
) = constant
;
1091 result_id
= lookup_expression_id (&expr
);
1093 return expression_for_id (result_id
);
1095 newexpr
= pre_expr_pool
.allocate ();
1096 newexpr
->kind
= CONSTANT
;
1097 PRE_EXPR_CONSTANT (newexpr
) = constant
;
1098 alloc_expression_id (newexpr
);
1099 value_id
= get_or_alloc_constant_value_id (constant
);
1100 add_to_value (value_id
, newexpr
);
1104 /* Given a value id V, find the actual tree representing the constant
1105 value if there is one, and return it. Return NULL if we can't find
1109 get_constant_for_value_id (unsigned int v
)
1111 if (value_id_constant_p (v
))
1115 bitmap exprset
= value_expressions
[v
];
1117 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1119 pre_expr expr
= expression_for_id (i
);
1120 if (expr
->kind
== CONSTANT
)
1121 return PRE_EXPR_CONSTANT (expr
);
1127 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1128 Currently only supports constants and SSA_NAMES. */
1130 get_or_alloc_expr_for (tree t
)
1132 if (TREE_CODE (t
) == SSA_NAME
)
1133 return get_or_alloc_expr_for_name (t
);
1134 else if (is_gimple_min_invariant (t
))
1135 return get_or_alloc_expr_for_constant (t
);
1138 /* More complex expressions can result from SCCVN expression
1139 simplification that inserts values for them. As they all
1140 do not have VOPs the get handled by the nary ops struct. */
1141 vn_nary_op_t result
;
1142 unsigned int result_id
;
1143 vn_nary_op_lookup (t
, &result
);
1146 pre_expr e
= pre_expr_pool
.allocate ();
1148 PRE_EXPR_NARY (e
) = result
;
1149 result_id
= lookup_expression_id (e
);
1152 pre_expr_pool
.remove (e
);
1153 e
= expression_for_id (result_id
);
1156 alloc_expression_id (e
);
1163 /* Return the folded version of T if T, when folded, is a gimple
1164 min_invariant. Otherwise, return T. */
1167 fully_constant_expression (pre_expr e
)
1175 vn_nary_op_t nary
= PRE_EXPR_NARY (e
);
1176 switch (TREE_CODE_CLASS (nary
->opcode
))
1179 case tcc_comparison
:
1181 /* We have to go from trees to pre exprs to value ids to
1183 tree naryop0
= nary
->op
[0];
1184 tree naryop1
= nary
->op
[1];
1186 if (!is_gimple_min_invariant (naryop0
))
1188 pre_expr rep0
= get_or_alloc_expr_for (naryop0
);
1189 unsigned int vrep0
= get_expr_value_id (rep0
);
1190 tree const0
= get_constant_for_value_id (vrep0
);
1192 naryop0
= fold_convert (TREE_TYPE (naryop0
), const0
);
1194 if (!is_gimple_min_invariant (naryop1
))
1196 pre_expr rep1
= get_or_alloc_expr_for (naryop1
);
1197 unsigned int vrep1
= get_expr_value_id (rep1
);
1198 tree const1
= get_constant_for_value_id (vrep1
);
1200 naryop1
= fold_convert (TREE_TYPE (naryop1
), const1
);
1202 result
= fold_binary (nary
->opcode
, nary
->type
,
1204 if (result
&& is_gimple_min_invariant (result
))
1205 return get_or_alloc_expr_for_constant (result
);
1206 /* We might have simplified the expression to a
1207 SSA_NAME for example from x_1 * 1. But we cannot
1208 insert a PHI for x_1 unconditionally as x_1 might
1209 not be available readily. */
1213 if (nary
->opcode
!= REALPART_EXPR
1214 && nary
->opcode
!= IMAGPART_EXPR
1215 && nary
->opcode
!= VIEW_CONVERT_EXPR
)
1220 /* We have to go from trees to pre exprs to value ids to
1222 tree naryop0
= nary
->op
[0];
1223 tree const0
, result
;
1224 if (is_gimple_min_invariant (naryop0
))
1228 pre_expr rep0
= get_or_alloc_expr_for (naryop0
);
1229 unsigned int vrep0
= get_expr_value_id (rep0
);
1230 const0
= get_constant_for_value_id (vrep0
);
1235 tree type1
= TREE_TYPE (nary
->op
[0]);
1236 const0
= fold_convert (type1
, const0
);
1237 result
= fold_unary (nary
->opcode
, nary
->type
, const0
);
1239 if (result
&& is_gimple_min_invariant (result
))
1240 return get_or_alloc_expr_for_constant (result
);
1249 vn_reference_t ref
= PRE_EXPR_REFERENCE (e
);
1251 if ((folded
= fully_constant_vn_reference_p (ref
)))
1252 return get_or_alloc_expr_for_constant (folded
);
1261 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1262 it has the value it would have in BLOCK. Set *SAME_VALID to true
1263 in case the new vuse doesn't change the value id of the OPERANDS. */
1266 translate_vuse_through_block (vec
<vn_reference_op_s
> operands
,
1267 alias_set_type set
, tree type
, tree vuse
,
1268 basic_block phiblock
,
1269 basic_block block
, bool *same_valid
)
1271 gimple
*phi
= SSA_NAME_DEF_STMT (vuse
);
1278 if (gimple_bb (phi
) != phiblock
)
1281 use_oracle
= ao_ref_init_from_vn_reference (&ref
, set
, type
, operands
);
1283 /* Use the alias-oracle to find either the PHI node in this block,
1284 the first VUSE used in this block that is equivalent to vuse or
1285 the first VUSE which definition in this block kills the value. */
1286 if (gimple_code (phi
) == GIMPLE_PHI
)
1287 e
= find_edge (block
, phiblock
);
1288 else if (use_oracle
)
1289 while (!stmt_may_clobber_ref_p_1 (phi
, &ref
))
1291 vuse
= gimple_vuse (phi
);
1292 phi
= SSA_NAME_DEF_STMT (vuse
);
1293 if (gimple_bb (phi
) != phiblock
)
1295 if (gimple_code (phi
) == GIMPLE_PHI
)
1297 e
= find_edge (block
, phiblock
);
1308 bitmap visited
= NULL
;
1310 /* Try to find a vuse that dominates this phi node by skipping
1311 non-clobbering statements. */
1312 vuse
= get_continuation_for_phi (phi
, &ref
, &cnt
, &visited
, false,
1315 BITMAP_FREE (visited
);
1321 /* If we didn't find any, the value ID can't stay the same,
1322 but return the translated vuse. */
1323 *same_valid
= false;
1324 vuse
= PHI_ARG_DEF (phi
, e
->dest_idx
);
1326 /* ??? We would like to return vuse here as this is the canonical
1327 upmost vdef that this reference is associated with. But during
1328 insertion of the references into the hash tables we only ever
1329 directly insert with their direct gimple_vuse, hence returning
1330 something else would make us not find the other expression. */
1331 return PHI_ARG_DEF (phi
, e
->dest_idx
);
1337 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1338 SET2. This is used to avoid making a set consisting of the union
1339 of PA_IN and ANTIC_IN during insert. */
1341 static inline pre_expr
1342 find_leader_in_sets (unsigned int val
, bitmap_set_t set1
, bitmap_set_t set2
)
1346 result
= bitmap_find_leader (set1
, val
);
1347 if (!result
&& set2
)
1348 result
= bitmap_find_leader (set2
, val
);
1352 /* Get the tree type for our PRE expression e. */
1355 get_expr_type (const pre_expr e
)
1360 return TREE_TYPE (PRE_EXPR_NAME (e
));
1362 return TREE_TYPE (PRE_EXPR_CONSTANT (e
));
1364 return PRE_EXPR_REFERENCE (e
)->type
;
1366 return PRE_EXPR_NARY (e
)->type
;
1371 /* Get a representative SSA_NAME for a given expression.
1372 Since all of our sub-expressions are treated as values, we require
1373 them to be SSA_NAME's for simplicity.
1374 Prior versions of GVNPRE used to use "value handles" here, so that
1375 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1376 either case, the operands are really values (IE we do not expect
1377 them to be usable without finding leaders). */
1380 get_representative_for (const pre_expr e
)
1383 unsigned int value_id
= get_expr_value_id (e
);
1388 return PRE_EXPR_NAME (e
);
1390 return PRE_EXPR_CONSTANT (e
);
1394 /* Go through all of the expressions representing this value
1395 and pick out an SSA_NAME. */
1398 bitmap exprs
= value_expressions
[value_id
];
1399 EXECUTE_IF_SET_IN_BITMAP (exprs
, 0, i
, bi
)
1401 pre_expr rep
= expression_for_id (i
);
1402 if (rep
->kind
== NAME
)
1403 return PRE_EXPR_NAME (rep
);
1404 else if (rep
->kind
== CONSTANT
)
1405 return PRE_EXPR_CONSTANT (rep
);
1411 /* If we reached here we couldn't find an SSA_NAME. This can
1412 happen when we've discovered a value that has never appeared in
1413 the program as set to an SSA_NAME, as the result of phi translation.
1415 ??? We should be able to re-use this when we insert the statement
1417 name
= make_temp_ssa_name (get_expr_type (e
), gimple_build_nop (), "pretmp");
1418 VN_INFO_GET (name
)->value_id
= value_id
;
1419 VN_INFO (name
)->valnum
= name
;
1420 /* ??? For now mark this SSA name for release by SCCVN. */
1421 VN_INFO (name
)->needs_insertion
= true;
1422 add_to_value (value_id
, get_or_alloc_expr_for_name (name
));
1423 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1425 fprintf (dump_file
, "Created SSA_NAME representative ");
1426 print_generic_expr (dump_file
, name
, 0);
1427 fprintf (dump_file
, " for expression:");
1428 print_pre_expr (dump_file
, e
);
1429 fprintf (dump_file
, " (%04d)\n", value_id
);
1438 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1439 basic_block pred
, basic_block phiblock
);
1441 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1442 the phis in PRED. Return NULL if we can't find a leader for each part
1443 of the translated expression. */
1446 phi_translate_1 (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1447 basic_block pred
, basic_block phiblock
)
1454 bool changed
= false;
1455 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1456 vn_nary_op_t newnary
= XALLOCAVAR (struct vn_nary_op_s
,
1457 sizeof_vn_nary_op (nary
->length
));
1458 memcpy (newnary
, nary
, sizeof_vn_nary_op (nary
->length
));
1460 for (i
= 0; i
< newnary
->length
; i
++)
1462 if (TREE_CODE (newnary
->op
[i
]) != SSA_NAME
)
1466 pre_expr leader
, result
;
1467 unsigned int op_val_id
= VN_INFO (newnary
->op
[i
])->value_id
;
1468 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1469 result
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1470 if (result
&& result
!= leader
)
1472 tree name
= get_representative_for (result
);
1475 newnary
->op
[i
] = name
;
1480 changed
|= newnary
->op
[i
] != nary
->op
[i
];
1486 unsigned int new_val_id
;
1488 tree result
= vn_nary_op_lookup_pieces (newnary
->length
,
1493 if (result
&& is_gimple_min_invariant (result
))
1494 return get_or_alloc_expr_for_constant (result
);
1496 expr
= pre_expr_pool
.allocate ();
1501 PRE_EXPR_NARY (expr
) = nary
;
1502 constant
= fully_constant_expression (expr
);
1503 if (constant
!= expr
)
1506 new_val_id
= nary
->value_id
;
1507 get_or_alloc_expression_id (expr
);
1511 new_val_id
= get_next_value_id ();
1512 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
1513 nary
= vn_nary_op_insert_pieces (newnary
->length
,
1517 result
, new_val_id
);
1518 PRE_EXPR_NARY (expr
) = nary
;
1519 constant
= fully_constant_expression (expr
);
1520 if (constant
!= expr
)
1522 get_or_alloc_expression_id (expr
);
1524 add_to_value (new_val_id
, expr
);
1532 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1533 vec
<vn_reference_op_s
> operands
= ref
->operands
;
1534 tree vuse
= ref
->vuse
;
1535 tree newvuse
= vuse
;
1536 vec
<vn_reference_op_s
> newoperands
= vNULL
;
1537 bool changed
= false, same_valid
= true;
1539 vn_reference_op_t operand
;
1540 vn_reference_t newref
;
1542 for (i
= 0; operands
.iterate (i
, &operand
); i
++)
1547 tree type
= operand
->type
;
1548 vn_reference_op_s newop
= *operand
;
1549 op
[0] = operand
->op0
;
1550 op
[1] = operand
->op1
;
1551 op
[2] = operand
->op2
;
1552 for (n
= 0; n
< 3; ++n
)
1554 unsigned int op_val_id
;
1557 if (TREE_CODE (op
[n
]) != SSA_NAME
)
1559 /* We can't possibly insert these. */
1561 && !is_gimple_min_invariant (op
[n
]))
1565 op_val_id
= VN_INFO (op
[n
])->value_id
;
1566 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1569 opresult
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1572 if (opresult
!= leader
)
1574 tree name
= get_representative_for (opresult
);
1577 changed
|= name
!= op
[n
];
1583 newoperands
.release ();
1588 if (!newoperands
.exists ())
1589 newoperands
= operands
.copy ();
1590 /* We may have changed from an SSA_NAME to a constant */
1591 if (newop
.opcode
== SSA_NAME
&& TREE_CODE (op
[0]) != SSA_NAME
)
1592 newop
.opcode
= TREE_CODE (op
[0]);
1597 newoperands
[i
] = newop
;
1599 gcc_checking_assert (i
== operands
.length ());
1603 newvuse
= translate_vuse_through_block (newoperands
.exists ()
1604 ? newoperands
: operands
,
1605 ref
->set
, ref
->type
,
1606 vuse
, phiblock
, pred
,
1608 if (newvuse
== NULL_TREE
)
1610 newoperands
.release ();
1615 if (changed
|| newvuse
!= vuse
)
1617 unsigned int new_val_id
;
1620 tree result
= vn_reference_lookup_pieces (newvuse
, ref
->set
,
1622 newoperands
.exists ()
1623 ? newoperands
: operands
,
1626 newoperands
.release ();
1628 /* We can always insert constants, so if we have a partial
1629 redundant constant load of another type try to translate it
1630 to a constant of appropriate type. */
1631 if (result
&& is_gimple_min_invariant (result
))
1634 if (!useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1636 tem
= fold_unary (VIEW_CONVERT_EXPR
, ref
->type
, result
);
1637 if (tem
&& !is_gimple_min_invariant (tem
))
1641 return get_or_alloc_expr_for_constant (tem
);
1644 /* If we'd have to convert things we would need to validate
1645 if we can insert the translated expression. So fail
1646 here for now - we cannot insert an alias with a different
1647 type in the VN tables either, as that would assert. */
1649 && !useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1651 else if (!result
&& newref
1652 && !useless_type_conversion_p (ref
->type
, newref
->type
))
1654 newoperands
.release ();
1658 expr
= pre_expr_pool
.allocate ();
1659 expr
->kind
= REFERENCE
;
1664 PRE_EXPR_REFERENCE (expr
) = newref
;
1665 constant
= fully_constant_expression (expr
);
1666 if (constant
!= expr
)
1669 new_val_id
= newref
->value_id
;
1670 get_or_alloc_expression_id (expr
);
1674 if (changed
|| !same_valid
)
1676 new_val_id
= get_next_value_id ();
1677 value_expressions
.safe_grow_cleared
1678 (get_max_value_id () + 1);
1681 new_val_id
= ref
->value_id
;
1682 if (!newoperands
.exists ())
1683 newoperands
= operands
.copy ();
1684 newref
= vn_reference_insert_pieces (newvuse
, ref
->set
,
1687 result
, new_val_id
);
1688 newoperands
= vNULL
;
1689 PRE_EXPR_REFERENCE (expr
) = newref
;
1690 constant
= fully_constant_expression (expr
);
1691 if (constant
!= expr
)
1693 get_or_alloc_expression_id (expr
);
1695 add_to_value (new_val_id
, expr
);
1697 newoperands
.release ();
1704 tree name
= PRE_EXPR_NAME (expr
);
1705 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1706 /* If the SSA name is defined by a PHI node in this block,
1708 if (gimple_code (def_stmt
) == GIMPLE_PHI
1709 && gimple_bb (def_stmt
) == phiblock
)
1711 edge e
= find_edge (pred
, gimple_bb (def_stmt
));
1712 tree def
= PHI_ARG_DEF (def_stmt
, e
->dest_idx
);
1714 /* Handle constant. */
1715 if (is_gimple_min_invariant (def
))
1716 return get_or_alloc_expr_for_constant (def
);
1718 return get_or_alloc_expr_for_name (def
);
1720 /* Otherwise return it unchanged - it will get removed if its
1721 value is not available in PREDs AVAIL_OUT set of expressions
1722 by the subtraction of TMP_GEN. */
1731 /* Wrapper around phi_translate_1 providing caching functionality. */
1734 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1735 basic_block pred
, basic_block phiblock
)
1737 expr_pred_trans_t slot
= NULL
;
1743 /* Constants contain no values that need translation. */
1744 if (expr
->kind
== CONSTANT
)
1747 if (value_id_constant_p (get_expr_value_id (expr
)))
1750 /* Don't add translations of NAMEs as those are cheap to translate. */
1751 if (expr
->kind
!= NAME
)
1753 if (phi_trans_add (&slot
, expr
, pred
))
1755 /* Store NULL for the value we want to return in the case of
1761 phitrans
= phi_translate_1 (expr
, set1
, set2
, pred
, phiblock
);
1768 /* Remove failed translations again, they cause insert
1769 iteration to not pick up new opportunities reliably. */
1770 phi_translate_table
->remove_elt_with_hash (slot
, slot
->hashcode
);
1777 /* For each expression in SET, translate the values through phi nodes
1778 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1779 expressions in DEST. */
1782 phi_translate_set (bitmap_set_t dest
, bitmap_set_t set
, basic_block pred
,
1783 basic_block phiblock
)
1785 vec
<pre_expr
> exprs
;
1789 if (gimple_seq_empty_p (phi_nodes (phiblock
)))
1791 bitmap_set_copy (dest
, set
);
1795 exprs
= sorted_array_from_bitmap_set (set
);
1796 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
1798 pre_expr translated
;
1799 translated
= phi_translate (expr
, set
, NULL
, pred
, phiblock
);
1803 /* We might end up with multiple expressions from SET being
1804 translated to the same value. In this case we do not want
1805 to retain the NARY or REFERENCE expression but prefer a NAME
1806 which would be the leader. */
1807 if (translated
->kind
== NAME
)
1808 bitmap_value_replace_in_set (dest
, translated
);
1810 bitmap_value_insert_into_set (dest
, translated
);
1815 /* Find the leader for a value (i.e., the name representing that
1816 value) in a given set, and return it. Return NULL if no leader
1820 bitmap_find_leader (bitmap_set_t set
, unsigned int val
)
1822 if (value_id_constant_p (val
))
1826 bitmap exprset
= value_expressions
[val
];
1828 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1830 pre_expr expr
= expression_for_id (i
);
1831 if (expr
->kind
== CONSTANT
)
1835 if (bitmap_set_contains_value (set
, val
))
1837 /* Rather than walk the entire bitmap of expressions, and see
1838 whether any of them has the value we are looking for, we look
1839 at the reverse mapping, which tells us the set of expressions
1840 that have a given value (IE value->expressions with that
1841 value) and see if any of those expressions are in our set.
1842 The number of expressions per value is usually significantly
1843 less than the number of expressions in the set. In fact, for
1844 large testcases, doing it this way is roughly 5-10x faster
1845 than walking the bitmap.
1846 If this is somehow a significant lose for some cases, we can
1847 choose which set to walk based on which set is smaller. */
1850 bitmap exprset
= value_expressions
[val
];
1852 EXECUTE_IF_AND_IN_BITMAP (exprset
, &set
->expressions
, 0, i
, bi
)
1853 return expression_for_id (i
);
1858 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1859 BLOCK by seeing if it is not killed in the block. Note that we are
1860 only determining whether there is a store that kills it. Because
1861 of the order in which clean iterates over values, we are guaranteed
1862 that altered operands will have caused us to be eliminated from the
1863 ANTIC_IN set already. */
1866 value_dies_in_block_x (pre_expr expr
, basic_block block
)
1868 tree vuse
= PRE_EXPR_REFERENCE (expr
)->vuse
;
1869 vn_reference_t refx
= PRE_EXPR_REFERENCE (expr
);
1871 gimple_stmt_iterator gsi
;
1872 unsigned id
= get_expression_id (expr
);
1879 /* Lookup a previously calculated result. */
1880 if (EXPR_DIES (block
)
1881 && bitmap_bit_p (EXPR_DIES (block
), id
* 2))
1882 return bitmap_bit_p (EXPR_DIES (block
), id
* 2 + 1);
1884 /* A memory expression {e, VUSE} dies in the block if there is a
1885 statement that may clobber e. If, starting statement walk from the
1886 top of the basic block, a statement uses VUSE there can be no kill
1887 inbetween that use and the original statement that loaded {e, VUSE},
1888 so we can stop walking. */
1889 ref
.base
= NULL_TREE
;
1890 for (gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1892 tree def_vuse
, def_vdef
;
1893 def
= gsi_stmt (gsi
);
1894 def_vuse
= gimple_vuse (def
);
1895 def_vdef
= gimple_vdef (def
);
1897 /* Not a memory statement. */
1901 /* Not a may-def. */
1904 /* A load with the same VUSE, we're done. */
1905 if (def_vuse
== vuse
)
1911 /* Init ref only if we really need it. */
1912 if (ref
.base
== NULL_TREE
1913 && !ao_ref_init_from_vn_reference (&ref
, refx
->set
, refx
->type
,
1919 /* If the statement may clobber expr, it dies. */
1920 if (stmt_may_clobber_ref_p_1 (def
, &ref
))
1927 /* Remember the result. */
1928 if (!EXPR_DIES (block
))
1929 EXPR_DIES (block
) = BITMAP_ALLOC (&grand_bitmap_obstack
);
1930 bitmap_set_bit (EXPR_DIES (block
), id
* 2);
1932 bitmap_set_bit (EXPR_DIES (block
), id
* 2 + 1);
1938 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1939 contains its value-id. */
1942 op_valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, tree op
)
1944 if (op
&& TREE_CODE (op
) == SSA_NAME
)
1946 unsigned int value_id
= VN_INFO (op
)->value_id
;
1947 if (!(bitmap_set_contains_value (set1
, value_id
)
1948 || (set2
&& bitmap_set_contains_value (set2
, value_id
))))
1954 /* Determine if the expression EXPR is valid in SET1 U SET2.
1955 ONLY SET2 CAN BE NULL.
1956 This means that we have a leader for each part of the expression
1957 (if it consists of values), or the expression is an SSA_NAME.
1958 For loads/calls, we also see if the vuse is killed in this block. */
1961 valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, pre_expr expr
)
1966 /* By construction all NAMEs are available. Non-available
1967 NAMEs are removed by subtracting TMP_GEN from the sets. */
1972 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1973 for (i
= 0; i
< nary
->length
; i
++)
1974 if (!op_valid_in_sets (set1
, set2
, nary
->op
[i
]))
1981 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1982 vn_reference_op_t vro
;
1985 FOR_EACH_VEC_ELT (ref
->operands
, i
, vro
)
1987 if (!op_valid_in_sets (set1
, set2
, vro
->op0
)
1988 || !op_valid_in_sets (set1
, set2
, vro
->op1
)
1989 || !op_valid_in_sets (set1
, set2
, vro
->op2
))
1999 /* Clean the set of expressions that are no longer valid in SET1 or
2000 SET2. This means expressions that are made up of values we have no
2001 leaders for in SET1 or SET2. This version is used for partial
2002 anticipation, which means it is not valid in either ANTIC_IN or
2006 dependent_clean (bitmap_set_t set1
, bitmap_set_t set2
)
2008 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set1
);
2012 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2014 if (!valid_in_sets (set1
, set2
, expr
))
2015 bitmap_remove_from_set (set1
, expr
);
2020 /* Clean the set of expressions that are no longer valid in SET. This
2021 means expressions that are made up of values we have no leaders for
2025 clean (bitmap_set_t set
)
2027 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set
);
2031 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2033 if (!valid_in_sets (set
, NULL
, expr
))
2034 bitmap_remove_from_set (set
, expr
);
2039 /* Clean the set of expressions that are no longer valid in SET because
2040 they are clobbered in BLOCK or because they trap and may not be executed. */
2043 prune_clobbered_mems (bitmap_set_t set
, basic_block block
)
2048 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
2050 pre_expr expr
= expression_for_id (i
);
2051 if (expr
->kind
== REFERENCE
)
2053 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2056 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ref
->vuse
);
2057 if (!gimple_nop_p (def_stmt
)
2058 && ((gimple_bb (def_stmt
) != block
2059 && !dominated_by_p (CDI_DOMINATORS
,
2060 block
, gimple_bb (def_stmt
)))
2061 || (gimple_bb (def_stmt
) == block
2062 && value_dies_in_block_x (expr
, block
))))
2063 bitmap_remove_from_set (set
, expr
);
2066 else if (expr
->kind
== NARY
)
2068 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2069 /* If the NARY may trap make sure the block does not contain
2070 a possible exit point.
2071 ??? This is overly conservative if we translate AVAIL_OUT
2072 as the available expression might be after the exit point. */
2073 if (BB_MAY_NOTRETURN (block
)
2074 && vn_nary_may_trap (nary
))
2075 bitmap_remove_from_set (set
, expr
);
2080 static sbitmap has_abnormal_preds
;
2082 /* List of blocks that may have changed during ANTIC computation and
2083 thus need to be iterated over. */
2085 static sbitmap changed_blocks
;
2087 /* Compute the ANTIC set for BLOCK.
2089 If succs(BLOCK) > 1 then
2090 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2091 else if succs(BLOCK) == 1 then
2092 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2094 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2098 compute_antic_aux (basic_block block
, bool block_has_abnormal_pred_edge
)
2100 bool changed
= false;
2101 bitmap_set_t S
, old
, ANTIC_OUT
;
2107 old
= ANTIC_OUT
= S
= NULL
;
2108 BB_VISITED (block
) = 1;
2110 /* If any edges from predecessors are abnormal, antic_in is empty,
2112 if (block_has_abnormal_pred_edge
)
2113 goto maybe_dump_sets
;
2115 old
= ANTIC_IN (block
);
2116 ANTIC_OUT
= bitmap_set_new ();
2118 /* If the block has no successors, ANTIC_OUT is empty. */
2119 if (EDGE_COUNT (block
->succs
) == 0)
2121 /* If we have one successor, we could have some phi nodes to
2122 translate through. */
2123 else if (single_succ_p (block
))
2125 basic_block succ_bb
= single_succ (block
);
2126 gcc_assert (BB_VISITED (succ_bb
));
2127 phi_translate_set (ANTIC_OUT
, ANTIC_IN (succ_bb
), block
, succ_bb
);
2129 /* If we have multiple successors, we take the intersection of all of
2130 them. Note that in the case of loop exit phi nodes, we may have
2131 phis to translate through. */
2135 basic_block bprime
, first
= NULL
;
2137 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2138 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2141 && BB_VISITED (e
->dest
))
2143 else if (BB_VISITED (e
->dest
))
2144 worklist
.quick_push (e
->dest
);
2147 /* Of multiple successors we have to have visited one already
2148 which is guaranteed by iteration order. */
2149 gcc_assert (first
!= NULL
);
2151 phi_translate_set (ANTIC_OUT
, ANTIC_IN (first
), block
, first
);
2153 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2155 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2157 bitmap_set_t tmp
= bitmap_set_new ();
2158 phi_translate_set (tmp
, ANTIC_IN (bprime
), block
, bprime
);
2159 bitmap_set_and (ANTIC_OUT
, tmp
);
2160 bitmap_set_free (tmp
);
2163 bitmap_set_and (ANTIC_OUT
, ANTIC_IN (bprime
));
2167 /* Prune expressions that are clobbered in block and thus become
2168 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2169 prune_clobbered_mems (ANTIC_OUT
, block
);
2171 /* Generate ANTIC_OUT - TMP_GEN. */
2172 S
= bitmap_set_subtract (ANTIC_OUT
, TMP_GEN (block
));
2174 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2175 ANTIC_IN (block
) = bitmap_set_subtract (EXP_GEN (block
),
2178 /* Then union in the ANTIC_OUT - TMP_GEN values,
2179 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2180 FOR_EACH_EXPR_ID_IN_SET (S
, bii
, bi
)
2181 bitmap_value_insert_into_set (ANTIC_IN (block
),
2182 expression_for_id (bii
));
2184 clean (ANTIC_IN (block
));
2186 if (!bitmap_set_equal (old
, ANTIC_IN (block
)))
2189 bitmap_set_bit (changed_blocks
, block
->index
);
2190 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2191 bitmap_set_bit (changed_blocks
, e
->src
->index
);
2194 bitmap_clear_bit (changed_blocks
, block
->index
);
2197 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2200 print_bitmap_set (dump_file
, ANTIC_OUT
, "ANTIC_OUT", block
->index
);
2202 print_bitmap_set (dump_file
, ANTIC_IN (block
), "ANTIC_IN",
2206 print_bitmap_set (dump_file
, S
, "S", block
->index
);
2209 bitmap_set_free (old
);
2211 bitmap_set_free (S
);
2213 bitmap_set_free (ANTIC_OUT
);
2217 /* Compute PARTIAL_ANTIC for BLOCK.
2219 If succs(BLOCK) > 1 then
2220 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2221 in ANTIC_OUT for all succ(BLOCK)
2222 else if succs(BLOCK) == 1 then
2223 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2225 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2230 compute_partial_antic_aux (basic_block block
,
2231 bool block_has_abnormal_pred_edge
)
2233 bool changed
= false;
2234 bitmap_set_t old_PA_IN
;
2235 bitmap_set_t PA_OUT
;
2238 unsigned long max_pa
= PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH
);
2240 old_PA_IN
= PA_OUT
= NULL
;
2242 /* If any edges from predecessors are abnormal, antic_in is empty,
2244 if (block_has_abnormal_pred_edge
)
2245 goto maybe_dump_sets
;
2247 /* If there are too many partially anticipatable values in the
2248 block, phi_translate_set can take an exponential time: stop
2249 before the translation starts. */
2251 && single_succ_p (block
)
2252 && bitmap_count_bits (&PA_IN (single_succ (block
))->values
) > max_pa
)
2253 goto maybe_dump_sets
;
2255 old_PA_IN
= PA_IN (block
);
2256 PA_OUT
= bitmap_set_new ();
2258 /* If the block has no successors, ANTIC_OUT is empty. */
2259 if (EDGE_COUNT (block
->succs
) == 0)
2261 /* If we have one successor, we could have some phi nodes to
2262 translate through. Note that we can't phi translate across DFS
2263 back edges in partial antic, because it uses a union operation on
2264 the successors. For recurrences like IV's, we will end up
2265 generating a new value in the set on each go around (i + 3 (VH.1)
2266 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2267 else if (single_succ_p (block
))
2269 basic_block succ
= single_succ (block
);
2270 if (!(single_succ_edge (block
)->flags
& EDGE_DFS_BACK
))
2271 phi_translate_set (PA_OUT
, PA_IN (succ
), block
, succ
);
2273 /* If we have multiple successors, we take the union of all of
2280 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2281 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2283 if (e
->flags
& EDGE_DFS_BACK
)
2285 worklist
.quick_push (e
->dest
);
2287 if (worklist
.length () > 0)
2289 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2294 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime
), i
, bi
)
2295 bitmap_value_insert_into_set (PA_OUT
,
2296 expression_for_id (i
));
2297 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2299 bitmap_set_t pa_in
= bitmap_set_new ();
2300 phi_translate_set (pa_in
, PA_IN (bprime
), block
, bprime
);
2301 FOR_EACH_EXPR_ID_IN_SET (pa_in
, i
, bi
)
2302 bitmap_value_insert_into_set (PA_OUT
,
2303 expression_for_id (i
));
2304 bitmap_set_free (pa_in
);
2307 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime
), i
, bi
)
2308 bitmap_value_insert_into_set (PA_OUT
,
2309 expression_for_id (i
));
2314 /* Prune expressions that are clobbered in block and thus become
2315 invalid if translated from PA_OUT to PA_IN. */
2316 prune_clobbered_mems (PA_OUT
, block
);
2318 /* PA_IN starts with PA_OUT - TMP_GEN.
2319 Then we subtract things from ANTIC_IN. */
2320 PA_IN (block
) = bitmap_set_subtract (PA_OUT
, TMP_GEN (block
));
2322 /* For partial antic, we want to put back in the phi results, since
2323 we will properly avoid making them partially antic over backedges. */
2324 bitmap_ior_into (&PA_IN (block
)->values
, &PHI_GEN (block
)->values
);
2325 bitmap_ior_into (&PA_IN (block
)->expressions
, &PHI_GEN (block
)->expressions
);
2327 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2328 bitmap_set_subtract_values (PA_IN (block
), ANTIC_IN (block
));
2330 dependent_clean (PA_IN (block
), ANTIC_IN (block
));
2332 if (!bitmap_set_equal (old_PA_IN
, PA_IN (block
)))
2335 bitmap_set_bit (changed_blocks
, block
->index
);
2336 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2337 bitmap_set_bit (changed_blocks
, e
->src
->index
);
2340 bitmap_clear_bit (changed_blocks
, block
->index
);
2343 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2346 print_bitmap_set (dump_file
, PA_OUT
, "PA_OUT", block
->index
);
2348 print_bitmap_set (dump_file
, PA_IN (block
), "PA_IN", block
->index
);
2351 bitmap_set_free (old_PA_IN
);
2353 bitmap_set_free (PA_OUT
);
2357 /* Compute ANTIC and partial ANTIC sets. */
2360 compute_antic (void)
2362 bool changed
= true;
2363 int num_iterations
= 0;
2367 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2368 We pre-build the map of blocks with incoming abnormal edges here. */
2369 has_abnormal_preds
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
2370 bitmap_clear (has_abnormal_preds
);
2372 FOR_ALL_BB_FN (block
, cfun
)
2377 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2379 e
->flags
&= ~EDGE_DFS_BACK
;
2380 if (e
->flags
& EDGE_ABNORMAL
)
2382 bitmap_set_bit (has_abnormal_preds
, block
->index
);
2387 BB_VISITED (block
) = 0;
2389 /* While we are here, give empty ANTIC_IN sets to each block. */
2390 ANTIC_IN (block
) = bitmap_set_new ();
2391 PA_IN (block
) = bitmap_set_new ();
2394 /* At the exit block we anticipate nothing. */
2395 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun
)) = 1;
2397 changed_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
) + 1);
2398 bitmap_ones (changed_blocks
);
2401 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2402 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2403 /* ??? We need to clear our PHI translation cache here as the
2404 ANTIC sets shrink and we restrict valid translations to
2405 those having operands with leaders in ANTIC. Same below
2406 for PA ANTIC computation. */
2409 for (i
= postorder_num
- 1; i
>= 0; i
--)
2411 if (bitmap_bit_p (changed_blocks
, postorder
[i
]))
2413 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2414 changed
|= compute_antic_aux (block
,
2415 bitmap_bit_p (has_abnormal_preds
,
2419 /* Theoretically possible, but *highly* unlikely. */
2420 gcc_checking_assert (num_iterations
< 500);
2423 statistics_histogram_event (cfun
, "compute_antic iterations",
2426 if (do_partial_partial
)
2428 bitmap_ones (changed_blocks
);
2429 mark_dfs_back_edges ();
2434 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2435 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2438 for (i
= postorder_num
- 1 ; i
>= 0; i
--)
2440 if (bitmap_bit_p (changed_blocks
, postorder
[i
]))
2442 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2444 |= compute_partial_antic_aux (block
,
2445 bitmap_bit_p (has_abnormal_preds
,
2449 /* Theoretically possible, but *highly* unlikely. */
2450 gcc_checking_assert (num_iterations
< 500);
2452 statistics_histogram_event (cfun
, "compute_partial_antic iterations",
2455 sbitmap_free (has_abnormal_preds
);
2456 sbitmap_free (changed_blocks
);
2460 /* Inserted expressions are placed onto this worklist, which is used
2461 for performing quick dead code elimination of insertions we made
2462 that didn't turn out to be necessary. */
2463 static bitmap inserted_exprs
;
2465 /* The actual worker for create_component_ref_by_pieces. */
2468 create_component_ref_by_pieces_1 (basic_block block
, vn_reference_t ref
,
2469 unsigned int *operand
, gimple_seq
*stmts
)
2471 vn_reference_op_t currop
= &ref
->operands
[*operand
];
2474 switch (currop
->opcode
)
2481 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2485 tree offset
= currop
->op0
;
2486 if (TREE_CODE (baseop
) == ADDR_EXPR
2487 && handled_component_p (TREE_OPERAND (baseop
, 0)))
2491 base
= get_addr_base_and_unit_offset (TREE_OPERAND (baseop
, 0),
2494 offset
= int_const_binop (PLUS_EXPR
, offset
,
2495 build_int_cst (TREE_TYPE (offset
),
2497 baseop
= build_fold_addr_expr (base
);
2499 genop
= build2 (MEM_REF
, currop
->type
, baseop
, offset
);
2500 MR_DEPENDENCE_CLIQUE (genop
) = currop
->clique
;
2501 MR_DEPENDENCE_BASE (genop
) = currop
->base
;
2505 case TARGET_MEM_REF
:
2507 tree genop0
= NULL_TREE
, genop1
= NULL_TREE
;
2508 vn_reference_op_t nextop
= &ref
->operands
[++*operand
];
2509 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2515 genop0
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2521 genop1
= find_or_generate_expression (block
, nextop
->op0
, stmts
);
2525 genop
= build5 (TARGET_MEM_REF
, currop
->type
,
2526 baseop
, currop
->op2
, genop0
, currop
->op1
, genop1
);
2528 MR_DEPENDENCE_CLIQUE (genop
) = currop
->clique
;
2529 MR_DEPENDENCE_BASE (genop
) = currop
->base
;
2536 gcc_assert (is_gimple_min_invariant (currop
->op0
));
2542 case VIEW_CONVERT_EXPR
:
2544 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2548 return fold_build1 (currop
->opcode
, currop
->type
, genop0
);
2551 case WITH_SIZE_EXPR
:
2553 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2557 tree genop1
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2560 return fold_build2 (currop
->opcode
, currop
->type
, genop0
, genop1
);
2565 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2569 tree op1
= currop
->op0
;
2570 tree op2
= currop
->op1
;
2571 return fold_build3 (BIT_FIELD_REF
, currop
->type
, genop0
, op1
, op2
);
2574 /* For array ref vn_reference_op's, operand 1 of the array ref
2575 is op0 of the reference op and operand 3 of the array ref is
2577 case ARRAY_RANGE_REF
:
2581 tree genop1
= currop
->op0
;
2582 tree genop2
= currop
->op1
;
2583 tree genop3
= currop
->op2
;
2584 genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2588 genop1
= find_or_generate_expression (block
, genop1
, stmts
);
2593 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (genop0
));
2594 /* Drop zero minimum index if redundant. */
2595 if (integer_zerop (genop2
)
2597 || integer_zerop (TYPE_MIN_VALUE (domain_type
))))
2601 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2608 tree elmt_type
= TREE_TYPE (TREE_TYPE (genop0
));
2609 /* We can't always put a size in units of the element alignment
2610 here as the element alignment may be not visible. See
2611 PR43783. Simply drop the element size for constant
2613 if (tree_int_cst_equal (genop3
, TYPE_SIZE_UNIT (elmt_type
)))
2617 genop3
= size_binop (EXACT_DIV_EXPR
, genop3
,
2618 size_int (TYPE_ALIGN_UNIT (elmt_type
)));
2619 genop3
= find_or_generate_expression (block
, genop3
, stmts
);
2624 return build4 (currop
->opcode
, currop
->type
, genop0
, genop1
,
2631 tree genop2
= currop
->op1
;
2632 op0
= create_component_ref_by_pieces_1 (block
, ref
, operand
, stmts
);
2635 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2639 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2643 return fold_build3 (COMPONENT_REF
, TREE_TYPE (op1
), op0
, op1
, genop2
);
2648 genop
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2669 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2670 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2671 trying to rename aggregates into ssa form directly, which is a no no.
2673 Thus, this routine doesn't create temporaries, it just builds a
2674 single access expression for the array, calling
2675 find_or_generate_expression to build the innermost pieces.
2677 This function is a subroutine of create_expression_by_pieces, and
2678 should not be called on it's own unless you really know what you
2682 create_component_ref_by_pieces (basic_block block
, vn_reference_t ref
,
2685 unsigned int op
= 0;
2686 return create_component_ref_by_pieces_1 (block
, ref
, &op
, stmts
);
2689 /* Find a simple leader for an expression, or generate one using
2690 create_expression_by_pieces from a NARY expression for the value.
2691 BLOCK is the basic_block we are looking for leaders in.
2692 OP is the tree expression to find a leader for or generate.
2693 Returns the leader or NULL_TREE on failure. */
2696 find_or_generate_expression (basic_block block
, tree op
, gimple_seq
*stmts
)
2698 pre_expr expr
= get_or_alloc_expr_for (op
);
2699 unsigned int lookfor
= get_expr_value_id (expr
);
2700 pre_expr leader
= bitmap_find_leader (AVAIL_OUT (block
), lookfor
);
2703 if (leader
->kind
== NAME
)
2704 return PRE_EXPR_NAME (leader
);
2705 else if (leader
->kind
== CONSTANT
)
2706 return PRE_EXPR_CONSTANT (leader
);
2712 /* It must be a complex expression, so generate it recursively. Note
2713 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2714 where the insert algorithm fails to insert a required expression. */
2715 bitmap exprset
= value_expressions
[lookfor
];
2718 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
2720 pre_expr temp
= expression_for_id (i
);
2721 /* We cannot insert random REFERENCE expressions at arbitrary
2722 places. We can insert NARYs which eventually re-materializes
2723 its operand values. */
2724 if (temp
->kind
== NARY
)
2725 return create_expression_by_pieces (block
, temp
, stmts
,
2726 get_expr_type (expr
));
2733 #define NECESSARY GF_PLF_1
2735 /* Create an expression in pieces, so that we can handle very complex
2736 expressions that may be ANTIC, but not necessary GIMPLE.
2737 BLOCK is the basic block the expression will be inserted into,
2738 EXPR is the expression to insert (in value form)
2739 STMTS is a statement list to append the necessary insertions into.
2741 This function will die if we hit some value that shouldn't be
2742 ANTIC but is (IE there is no leader for it, or its components).
2743 The function returns NULL_TREE in case a different antic expression
2744 has to be inserted first.
2745 This function may also generate expressions that are themselves
2746 partially or fully redundant. Those that are will be either made
2747 fully redundant during the next iteration of insert (for partially
2748 redundant ones), or eliminated by eliminate (for fully redundant
2752 create_expression_by_pieces (basic_block block
, pre_expr expr
,
2753 gimple_seq
*stmts
, tree type
)
2757 gimple_seq forced_stmts
= NULL
;
2758 unsigned int value_id
;
2759 gimple_stmt_iterator gsi
;
2760 tree exprtype
= type
? type
: get_expr_type (expr
);
2766 /* We may hit the NAME/CONSTANT case if we have to convert types
2767 that value numbering saw through. */
2769 folded
= PRE_EXPR_NAME (expr
);
2770 if (useless_type_conversion_p (exprtype
, TREE_TYPE (folded
)))
2775 folded
= PRE_EXPR_CONSTANT (expr
);
2776 tree tem
= fold_convert (exprtype
, folded
);
2777 if (is_gimple_min_invariant (tem
))
2782 if (PRE_EXPR_REFERENCE (expr
)->operands
[0].opcode
== CALL_EXPR
)
2784 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2785 unsigned int operand
= 1;
2786 vn_reference_op_t currop
= &ref
->operands
[0];
2787 tree sc
= NULL_TREE
;
2789 if (TREE_CODE (currop
->op0
) == FUNCTION_DECL
)
2792 fn
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2797 sc
= find_or_generate_expression (block
, currop
->op1
, stmts
);
2801 auto_vec
<tree
> args (ref
->operands
.length () - 1);
2802 while (operand
< ref
->operands
.length ())
2804 tree arg
= create_component_ref_by_pieces_1 (block
, ref
,
2808 args
.quick_push (arg
);
2811 = gimple_build_call_vec ((TREE_CODE (fn
) == FUNCTION_DECL
2812 ? build_fold_addr_expr (fn
) : fn
), args
);
2813 gimple_call_set_with_bounds (call
, currop
->with_bounds
);
2815 gimple_call_set_chain (call
, sc
);
2816 tree forcedname
= make_ssa_name (currop
->type
);
2817 gimple_call_set_lhs (call
, forcedname
);
2818 gimple_set_vuse (call
, BB_LIVE_VOP_ON_EXIT (block
));
2819 gimple_seq_add_stmt_without_update (&forced_stmts
, call
);
2820 folded
= forcedname
;
2824 folded
= create_component_ref_by_pieces (block
,
2825 PRE_EXPR_REFERENCE (expr
),
2829 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2830 newstmt
= gimple_build_assign (name
, folded
);
2831 gimple_seq_add_stmt_without_update (&forced_stmts
, newstmt
);
2832 gimple_set_vuse (newstmt
, BB_LIVE_VOP_ON_EXIT (block
));
2838 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2839 tree
*genop
= XALLOCAVEC (tree
, nary
->length
);
2841 for (i
= 0; i
< nary
->length
; ++i
)
2843 genop
[i
] = find_or_generate_expression (block
, nary
->op
[i
], stmts
);
2846 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2847 may have conversions stripped. */
2848 if (nary
->opcode
== POINTER_PLUS_EXPR
)
2851 genop
[i
] = gimple_convert (&forced_stmts
,
2852 nary
->type
, genop
[i
]);
2854 genop
[i
] = gimple_convert (&forced_stmts
,
2855 sizetype
, genop
[i
]);
2858 genop
[i
] = gimple_convert (&forced_stmts
,
2859 TREE_TYPE (nary
->op
[i
]), genop
[i
]);
2861 if (nary
->opcode
== CONSTRUCTOR
)
2863 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
2864 for (i
= 0; i
< nary
->length
; ++i
)
2865 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
, genop
[i
]);
2866 folded
= build_constructor (nary
->type
, elts
);
2867 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2868 newstmt
= gimple_build_assign (name
, folded
);
2869 gimple_seq_add_stmt_without_update (&forced_stmts
, newstmt
);
2874 switch (nary
->length
)
2877 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2881 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2882 genop
[0], genop
[1]);
2885 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2886 genop
[0], genop
[1], genop
[2]);
2898 folded
= gimple_convert (&forced_stmts
, exprtype
, folded
);
2900 /* If everything simplified to an exisiting SSA name or constant just
2902 if (gimple_seq_empty_p (forced_stmts
)
2903 || is_gimple_min_invariant (folded
))
2906 gcc_assert (TREE_CODE (folded
) == SSA_NAME
);
2908 /* If we have any intermediate expressions to the value sets, add them
2909 to the value sets and chain them in the instruction stream. */
2912 gsi
= gsi_start (forced_stmts
);
2913 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
2915 gimple
*stmt
= gsi_stmt (gsi
);
2916 tree forcedname
= gimple_get_lhs (stmt
);
2919 if (forcedname
!= folded
)
2921 VN_INFO_GET (forcedname
)->valnum
= forcedname
;
2922 VN_INFO (forcedname
)->value_id
= get_next_value_id ();
2923 nameexpr
= get_or_alloc_expr_for_name (forcedname
);
2924 add_to_value (VN_INFO (forcedname
)->value_id
, nameexpr
);
2925 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2926 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2929 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (forcedname
));
2930 gimple_set_plf (stmt
, NECESSARY
, false);
2932 gimple_seq_add_seq (stmts
, forced_stmts
);
2937 /* Fold the last statement. */
2938 gsi
= gsi_last (*stmts
);
2939 if (fold_stmt_inplace (&gsi
))
2940 update_stmt (gsi_stmt (gsi
));
2942 /* Add a value number to the temporary.
2943 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2944 we are creating the expression by pieces, and this particular piece of
2945 the expression may have been represented. There is no harm in replacing
2947 value_id
= get_expr_value_id (expr
);
2948 VN_INFO_GET (name
)->value_id
= value_id
;
2949 VN_INFO (name
)->valnum
= sccvn_valnum_from_value_id (value_id
);
2950 if (VN_INFO (name
)->valnum
== NULL_TREE
)
2951 VN_INFO (name
)->valnum
= name
;
2952 gcc_assert (VN_INFO (name
)->valnum
!= NULL_TREE
);
2953 nameexpr
= get_or_alloc_expr_for_name (name
);
2954 add_to_value (value_id
, nameexpr
);
2955 if (NEW_SETS (block
))
2956 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2957 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2959 pre_stats
.insertions
++;
2960 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2962 fprintf (dump_file
, "Inserted ");
2963 print_gimple_stmt (dump_file
, gsi_stmt (gsi_last (*stmts
)), 0, 0);
2964 fprintf (dump_file
, " in predecessor %d (%04d)\n",
2965 block
->index
, value_id
);
2972 /* Insert the to-be-made-available values of expression EXPRNUM for each
2973 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2974 merge the result with a phi node, given the same value number as
2975 NODE. Return true if we have inserted new stuff. */
2978 insert_into_preds_of_block (basic_block block
, unsigned int exprnum
,
2979 vec
<pre_expr
> avail
)
2981 pre_expr expr
= expression_for_id (exprnum
);
2983 unsigned int val
= get_expr_value_id (expr
);
2985 bool insertions
= false;
2990 tree type
= get_expr_type (expr
);
2994 /* Make sure we aren't creating an induction variable. */
2995 if (bb_loop_depth (block
) > 0 && EDGE_COUNT (block
->preds
) == 2)
2997 bool firstinsideloop
= false;
2998 bool secondinsideloop
= false;
2999 firstinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3000 EDGE_PRED (block
, 0)->src
);
3001 secondinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3002 EDGE_PRED (block
, 1)->src
);
3003 /* Induction variables only have one edge inside the loop. */
3004 if ((firstinsideloop
^ secondinsideloop
)
3005 && expr
->kind
!= REFERENCE
)
3007 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3008 fprintf (dump_file
, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3013 /* Make the necessary insertions. */
3014 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3016 gimple_seq stmts
= NULL
;
3019 eprime
= avail
[pred
->dest_idx
];
3020 builtexpr
= create_expression_by_pieces (bprime
, eprime
,
3022 gcc_assert (!(pred
->flags
& EDGE_ABNORMAL
));
3023 if (!gimple_seq_empty_p (stmts
))
3025 gsi_insert_seq_on_edge (pred
, stmts
);
3030 /* We cannot insert a PHI node if we failed to insert
3035 if (is_gimple_min_invariant (builtexpr
))
3036 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_constant (builtexpr
);
3038 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_name (builtexpr
);
3040 /* If we didn't want a phi node, and we made insertions, we still have
3041 inserted new stuff, and thus return true. If we didn't want a phi node,
3042 and didn't make insertions, we haven't added anything new, so return
3044 if (nophi
&& insertions
)
3046 else if (nophi
&& !insertions
)
3049 /* Now build a phi for the new variable. */
3050 temp
= make_temp_ssa_name (type
, NULL
, "prephitmp");
3051 phi
= create_phi_node (temp
, block
);
3053 gimple_set_plf (phi
, NECESSARY
, false);
3054 VN_INFO_GET (temp
)->value_id
= val
;
3055 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3056 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3057 VN_INFO (temp
)->valnum
= temp
;
3058 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3059 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3061 pre_expr ae
= avail
[pred
->dest_idx
];
3062 gcc_assert (get_expr_type (ae
) == type
3063 || useless_type_conversion_p (type
, get_expr_type (ae
)));
3064 if (ae
->kind
== CONSTANT
)
3065 add_phi_arg (phi
, unshare_expr (PRE_EXPR_CONSTANT (ae
)),
3066 pred
, UNKNOWN_LOCATION
);
3068 add_phi_arg (phi
, PRE_EXPR_NAME (ae
), pred
, UNKNOWN_LOCATION
);
3071 newphi
= get_or_alloc_expr_for_name (temp
);
3072 add_to_value (val
, newphi
);
3074 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3075 this insertion, since we test for the existence of this value in PHI_GEN
3076 before proceeding with the partial redundancy checks in insert_aux.
3078 The value may exist in AVAIL_OUT, in particular, it could be represented
3079 by the expression we are trying to eliminate, in which case we want the
3080 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3083 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3084 this block, because if it did, it would have existed in our dominator's
3085 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3088 bitmap_insert_into_set (PHI_GEN (block
), newphi
);
3089 bitmap_value_replace_in_set (AVAIL_OUT (block
),
3091 bitmap_insert_into_set (NEW_SETS (block
),
3094 /* If we insert a PHI node for a conversion of another PHI node
3095 in the same basic-block try to preserve range information.
3096 This is important so that followup loop passes receive optimal
3097 number of iteration analysis results. See PR61743. */
3098 if (expr
->kind
== NARY
3099 && CONVERT_EXPR_CODE_P (expr
->u
.nary
->opcode
)
3100 && TREE_CODE (expr
->u
.nary
->op
[0]) == SSA_NAME
3101 && gimple_bb (SSA_NAME_DEF_STMT (expr
->u
.nary
->op
[0])) == block
3102 && INTEGRAL_TYPE_P (type
)
3103 && INTEGRAL_TYPE_P (TREE_TYPE (expr
->u
.nary
->op
[0]))
3104 && (TYPE_PRECISION (type
)
3105 >= TYPE_PRECISION (TREE_TYPE (expr
->u
.nary
->op
[0])))
3106 && SSA_NAME_RANGE_INFO (expr
->u
.nary
->op
[0]))
3109 if (get_range_info (expr
->u
.nary
->op
[0], &min
, &max
) == VR_RANGE
3110 && !wi::neg_p (min
, SIGNED
)
3111 && !wi::neg_p (max
, SIGNED
))
3112 /* Just handle extension and sign-changes of all-positive ranges. */
3113 set_range_info (temp
,
3114 SSA_NAME_RANGE_TYPE (expr
->u
.nary
->op
[0]),
3115 wide_int_storage::from (min
, TYPE_PRECISION (type
),
3117 wide_int_storage::from (max
, TYPE_PRECISION (type
),
3121 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3123 fprintf (dump_file
, "Created phi ");
3124 print_gimple_stmt (dump_file
, phi
, 0, 0);
3125 fprintf (dump_file
, " in block %d (%04d)\n", block
->index
, val
);
3133 /* Perform insertion of partially redundant values.
3134 For BLOCK, do the following:
3135 1. Propagate the NEW_SETS of the dominator into the current block.
3136 If the block has multiple predecessors,
3137 2a. Iterate over the ANTIC expressions for the block to see if
3138 any of them are partially redundant.
3139 2b. If so, insert them into the necessary predecessors to make
3140 the expression fully redundant.
3141 2c. Insert a new PHI merging the values of the predecessors.
3142 2d. Insert the new PHI, and the new expressions, into the
3144 3. Recursively call ourselves on the dominator children of BLOCK.
3146 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
3147 do_regular_insertion and do_partial_insertion.
3152 do_regular_insertion (basic_block block
, basic_block dom
)
3154 bool new_stuff
= false;
3155 vec
<pre_expr
> exprs
;
3157 auto_vec
<pre_expr
> avail
;
3160 exprs
= sorted_array_from_bitmap_set (ANTIC_IN (block
));
3161 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3163 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3165 if (expr
->kind
== NARY
3166 || expr
->kind
== REFERENCE
)
3169 bool by_some
= false;
3170 bool cant_insert
= false;
3171 bool all_same
= true;
3172 pre_expr first_s
= NULL
;
3175 pre_expr eprime
= NULL
;
3177 pre_expr edoubleprime
= NULL
;
3178 bool do_insertion
= false;
3180 val
= get_expr_value_id (expr
);
3181 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3183 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3185 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3187 fprintf (dump_file
, "Found fully redundant value: ");
3188 print_pre_expr (dump_file
, expr
);
3189 fprintf (dump_file
, "\n");
3194 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3196 unsigned int vprime
;
3198 /* We should never run insertion for the exit block
3199 and so not come across fake pred edges. */
3200 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3202 /* We are looking at ANTIC_OUT of bprime. */
3203 eprime
= phi_translate (expr
, ANTIC_IN (block
), NULL
,
3206 /* eprime will generally only be NULL if the
3207 value of the expression, translated
3208 through the PHI for this predecessor, is
3209 undefined. If that is the case, we can't
3210 make the expression fully redundant,
3211 because its value is undefined along a
3212 predecessor path. We can thus break out
3213 early because it doesn't matter what the
3214 rest of the results are. */
3217 avail
[pred
->dest_idx
] = NULL
;
3222 eprime
= fully_constant_expression (eprime
);
3223 vprime
= get_expr_value_id (eprime
);
3224 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
),
3226 if (edoubleprime
== NULL
)
3228 avail
[pred
->dest_idx
] = eprime
;
3233 avail
[pred
->dest_idx
] = edoubleprime
;
3235 /* We want to perform insertions to remove a redundancy on
3236 a path in the CFG we want to optimize for speed. */
3237 if (optimize_edge_for_speed_p (pred
))
3238 do_insertion
= true;
3239 if (first_s
== NULL
)
3240 first_s
= edoubleprime
;
3241 else if (!pre_expr_d::equal (first_s
, edoubleprime
))
3245 /* If we can insert it, it's not the same value
3246 already existing along every predecessor, and
3247 it's defined by some predecessor, it is
3248 partially redundant. */
3249 if (!cant_insert
&& !all_same
&& by_some
)
3253 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3255 fprintf (dump_file
, "Skipping partial redundancy for "
3257 print_pre_expr (dump_file
, expr
);
3258 fprintf (dump_file
, " (%04d), no redundancy on to be "
3259 "optimized for speed edge\n", val
);
3262 else if (dbg_cnt (treepre_insert
))
3264 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3266 fprintf (dump_file
, "Found partial redundancy for "
3268 print_pre_expr (dump_file
, expr
);
3269 fprintf (dump_file
, " (%04d)\n",
3270 get_expr_value_id (expr
));
3272 if (insert_into_preds_of_block (block
,
3273 get_expression_id (expr
),
3278 /* If all edges produce the same value and that value is
3279 an invariant, then the PHI has the same value on all
3280 edges. Note this. */
3281 else if (!cant_insert
&& all_same
)
3283 gcc_assert (edoubleprime
->kind
== CONSTANT
3284 || edoubleprime
->kind
== NAME
);
3286 tree temp
= make_temp_ssa_name (get_expr_type (expr
),
3289 = gimple_build_assign (temp
,
3290 edoubleprime
->kind
== CONSTANT
?
3291 PRE_EXPR_CONSTANT (edoubleprime
) :
3292 PRE_EXPR_NAME (edoubleprime
));
3293 gimple_stmt_iterator gsi
= gsi_after_labels (block
);
3294 gsi_insert_before (&gsi
, assign
, GSI_NEW_STMT
);
3296 gimple_set_plf (assign
, NECESSARY
, false);
3297 VN_INFO_GET (temp
)->value_id
= val
;
3298 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3299 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3300 VN_INFO (temp
)->valnum
= temp
;
3301 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3302 pre_expr newe
= get_or_alloc_expr_for_name (temp
);
3303 add_to_value (val
, newe
);
3304 bitmap_value_replace_in_set (AVAIL_OUT (block
), newe
);
3305 bitmap_insert_into_set (NEW_SETS (block
), newe
);
3315 /* Perform insertion for partially anticipatable expressions. There
3316 is only one case we will perform insertion for these. This case is
3317 if the expression is partially anticipatable, and fully available.
3318 In this case, we know that putting it earlier will enable us to
3319 remove the later computation. */
3323 do_partial_partial_insertion (basic_block block
, basic_block dom
)
3325 bool new_stuff
= false;
3326 vec
<pre_expr
> exprs
;
3328 auto_vec
<pre_expr
> avail
;
3331 exprs
= sorted_array_from_bitmap_set (PA_IN (block
));
3332 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3334 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3336 if (expr
->kind
== NARY
3337 || expr
->kind
== REFERENCE
)
3341 bool cant_insert
= false;
3344 pre_expr eprime
= NULL
;
3347 val
= get_expr_value_id (expr
);
3348 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3350 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3353 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3355 unsigned int vprime
;
3356 pre_expr edoubleprime
;
3358 /* We should never run insertion for the exit block
3359 and so not come across fake pred edges. */
3360 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3362 eprime
= phi_translate (expr
, ANTIC_IN (block
),
3366 /* eprime will generally only be NULL if the
3367 value of the expression, translated
3368 through the PHI for this predecessor, is
3369 undefined. If that is the case, we can't
3370 make the expression fully redundant,
3371 because its value is undefined along a
3372 predecessor path. We can thus break out
3373 early because it doesn't matter what the
3374 rest of the results are. */
3377 avail
[pred
->dest_idx
] = NULL
;
3382 eprime
= fully_constant_expression (eprime
);
3383 vprime
= get_expr_value_id (eprime
);
3384 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
), vprime
);
3385 avail
[pred
->dest_idx
] = edoubleprime
;
3386 if (edoubleprime
== NULL
)
3393 /* If we can insert it, it's not the same value
3394 already existing along every predecessor, and
3395 it's defined by some predecessor, it is
3396 partially redundant. */
3397 if (!cant_insert
&& by_all
)
3400 bool do_insertion
= false;
3402 /* Insert only if we can remove a later expression on a path
3403 that we want to optimize for speed.
3404 The phi node that we will be inserting in BLOCK is not free,
3405 and inserting it for the sake of !optimize_for_speed successor
3406 may cause regressions on the speed path. */
3407 FOR_EACH_EDGE (succ
, ei
, block
->succs
)
3409 if (bitmap_set_contains_value (PA_IN (succ
->dest
), val
)
3410 || bitmap_set_contains_value (ANTIC_IN (succ
->dest
), val
))
3412 if (optimize_edge_for_speed_p (succ
))
3413 do_insertion
= true;
3419 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3421 fprintf (dump_file
, "Skipping partial partial redundancy "
3423 print_pre_expr (dump_file
, expr
);
3424 fprintf (dump_file
, " (%04d), not (partially) anticipated "
3425 "on any to be optimized for speed edges\n", val
);
3428 else if (dbg_cnt (treepre_insert
))
3430 pre_stats
.pa_insert
++;
3431 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3433 fprintf (dump_file
, "Found partial partial redundancy "
3435 print_pre_expr (dump_file
, expr
);
3436 fprintf (dump_file
, " (%04d)\n",
3437 get_expr_value_id (expr
));
3439 if (insert_into_preds_of_block (block
,
3440 get_expression_id (expr
),
3453 insert_aux (basic_block block
)
3456 bool new_stuff
= false;
3461 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3466 bitmap_set_t newset
= NEW_SETS (dom
);
3469 /* Note that we need to value_replace both NEW_SETS, and
3470 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3471 represented by some non-simple expression here that we want
3472 to replace it with. */
3473 FOR_EACH_EXPR_ID_IN_SET (newset
, i
, bi
)
3475 pre_expr expr
= expression_for_id (i
);
3476 bitmap_value_replace_in_set (NEW_SETS (block
), expr
);
3477 bitmap_value_replace_in_set (AVAIL_OUT (block
), expr
);
3480 if (!single_pred_p (block
))
3482 new_stuff
|= do_regular_insertion (block
, dom
);
3483 if (do_partial_partial
)
3484 new_stuff
|= do_partial_partial_insertion (block
, dom
);
3488 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
3490 son
= next_dom_son (CDI_DOMINATORS
, son
))
3492 new_stuff
|= insert_aux (son
);
3498 /* Perform insertion of partially redundant values. */
3503 bool new_stuff
= true;
3505 int num_iterations
= 0;
3507 FOR_ALL_BB_FN (bb
, cfun
)
3508 NEW_SETS (bb
) = bitmap_set_new ();
3513 if (dump_file
&& dump_flags
& TDF_DETAILS
)
3514 fprintf (dump_file
, "Starting insert iteration %d\n", num_iterations
);
3515 new_stuff
= insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3517 /* Clear the NEW sets before the next iteration. We have already
3518 fully propagated its contents. */
3520 FOR_ALL_BB_FN (bb
, cfun
)
3521 bitmap_set_free (NEW_SETS (bb
));
3523 statistics_histogram_event (cfun
, "insert iterations", num_iterations
);
3527 /* Compute the AVAIL set for all basic blocks.
3529 This function performs value numbering of the statements in each basic
3530 block. The AVAIL sets are built from information we glean while doing
3531 this value numbering, since the AVAIL sets contain only one entry per
3534 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3535 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3538 compute_avail (void)
3541 basic_block block
, son
;
3542 basic_block
*worklist
;
3546 /* We pretend that default definitions are defined in the entry block.
3547 This includes function arguments and the static chain decl. */
3548 for (i
= 1; i
< num_ssa_names
; ++i
)
3550 tree name
= ssa_name (i
);
3553 || !SSA_NAME_IS_DEFAULT_DEF (name
)
3554 || has_zero_uses (name
)
3555 || virtual_operand_p (name
))
3558 e
= get_or_alloc_expr_for_name (name
);
3559 add_to_value (get_expr_value_id (e
), e
);
3560 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)), e
);
3561 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3565 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3567 print_bitmap_set (dump_file
, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3568 "tmp_gen", ENTRY_BLOCK
);
3569 print_bitmap_set (dump_file
, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3570 "avail_out", ENTRY_BLOCK
);
3573 /* Allocate the worklist. */
3574 worklist
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
3576 /* Seed the algorithm by putting the dominator children of the entry
3577 block on the worklist. */
3578 for (son
= first_dom_son (CDI_DOMINATORS
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3580 son
= next_dom_son (CDI_DOMINATORS
, son
))
3581 worklist
[sp
++] = son
;
3583 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun
))
3584 = ssa_default_def (cfun
, gimple_vop (cfun
));
3586 /* Loop until the worklist is empty. */
3592 /* Pick a block from the worklist. */
3593 block
= worklist
[--sp
];
3595 /* Initially, the set of available values in BLOCK is that of
3596 its immediate dominator. */
3597 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3600 bitmap_set_copy (AVAIL_OUT (block
), AVAIL_OUT (dom
));
3601 BB_LIVE_VOP_ON_EXIT (block
) = BB_LIVE_VOP_ON_EXIT (dom
);
3604 /* Generate values for PHI nodes. */
3605 for (gphi_iterator gsi
= gsi_start_phis (block
); !gsi_end_p (gsi
);
3608 tree result
= gimple_phi_result (gsi
.phi ());
3610 /* We have no need for virtual phis, as they don't represent
3611 actual computations. */
3612 if (virtual_operand_p (result
))
3614 BB_LIVE_VOP_ON_EXIT (block
) = result
;
3618 pre_expr e
= get_or_alloc_expr_for_name (result
);
3619 add_to_value (get_expr_value_id (e
), e
);
3620 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3621 bitmap_insert_into_set (PHI_GEN (block
), e
);
3624 BB_MAY_NOTRETURN (block
) = 0;
3626 /* Now compute value numbers and populate value sets with all
3627 the expressions computed in BLOCK. */
3628 for (gimple_stmt_iterator gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
);
3634 stmt
= gsi_stmt (gsi
);
3636 /* Cache whether the basic-block has any non-visible side-effect
3638 If this isn't a call or it is the last stmt in the
3639 basic-block then the CFG represents things correctly. */
3640 if (is_gimple_call (stmt
) && !stmt_ends_bb_p (stmt
))
3642 /* Non-looping const functions always return normally.
3643 Otherwise the call might not return or have side-effects
3644 that forbids hoisting possibly trapping expressions
3646 int flags
= gimple_call_flags (stmt
);
3647 if (!(flags
& ECF_CONST
)
3648 || (flags
& ECF_LOOPING_CONST_OR_PURE
))
3649 BB_MAY_NOTRETURN (block
) = 1;
3652 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
3654 pre_expr e
= get_or_alloc_expr_for_name (op
);
3656 add_to_value (get_expr_value_id (e
), e
);
3657 bitmap_insert_into_set (TMP_GEN (block
), e
);
3658 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3661 if (gimple_vdef (stmt
))
3662 BB_LIVE_VOP_ON_EXIT (block
) = gimple_vdef (stmt
);
3664 if (gimple_has_side_effects (stmt
)
3665 || stmt_could_throw_p (stmt
)
3666 || is_gimple_debug (stmt
))
3669 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
3671 if (ssa_undefined_value_p (op
))
3673 pre_expr e
= get_or_alloc_expr_for_name (op
);
3674 bitmap_value_insert_into_set (EXP_GEN (block
), e
);
3677 switch (gimple_code (stmt
))
3685 vn_reference_s ref1
;
3686 pre_expr result
= NULL
;
3688 /* We can value number only calls to real functions. */
3689 if (gimple_call_internal_p (stmt
))
3692 vn_reference_lookup_call (as_a
<gcall
*> (stmt
), &ref
, &ref1
);
3696 /* If the value of the call is not invalidated in
3697 this block until it is computed, add the expression
3699 if (!gimple_vuse (stmt
)
3701 (SSA_NAME_DEF_STMT (gimple_vuse (stmt
))) == GIMPLE_PHI
3702 || gimple_bb (SSA_NAME_DEF_STMT
3703 (gimple_vuse (stmt
))) != block
)
3705 result
= pre_expr_pool
.allocate ();
3706 result
->kind
= REFERENCE
;
3708 PRE_EXPR_REFERENCE (result
) = ref
;
3710 get_or_alloc_expression_id (result
);
3711 add_to_value (get_expr_value_id (result
), result
);
3712 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
3719 pre_expr result
= NULL
;
3720 switch (vn_get_stmt_kind (stmt
))
3724 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3727 /* COND_EXPR and VEC_COND_EXPR are awkward in
3728 that they contain an embedded complex expression.
3729 Don't even try to shove those through PRE. */
3730 if (code
== COND_EXPR
3731 || code
== VEC_COND_EXPR
)
3734 vn_nary_op_lookup_stmt (stmt
, &nary
);
3738 /* If the NARY traps and there was a preceding
3739 point in the block that might not return avoid
3740 adding the nary to EXP_GEN. */
3741 if (BB_MAY_NOTRETURN (block
)
3742 && vn_nary_may_trap (nary
))
3745 result
= pre_expr_pool
.allocate ();
3746 result
->kind
= NARY
;
3748 PRE_EXPR_NARY (result
) = nary
;
3755 vn_reference_lookup (gimple_assign_rhs1 (stmt
),
3761 /* If the value of the reference is not invalidated in
3762 this block until it is computed, add the expression
3764 if (gimple_vuse (stmt
))
3768 def_stmt
= SSA_NAME_DEF_STMT (gimple_vuse (stmt
));
3769 while (!gimple_nop_p (def_stmt
)
3770 && gimple_code (def_stmt
) != GIMPLE_PHI
3771 && gimple_bb (def_stmt
) == block
)
3773 if (stmt_may_clobber_ref_p
3774 (def_stmt
, gimple_assign_rhs1 (stmt
)))
3780 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt
));
3786 result
= pre_expr_pool
.allocate ();
3787 result
->kind
= REFERENCE
;
3789 PRE_EXPR_REFERENCE (result
) = ref
;
3797 get_or_alloc_expression_id (result
);
3798 add_to_value (get_expr_value_id (result
), result
);
3799 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
3807 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3809 print_bitmap_set (dump_file
, EXP_GEN (block
),
3810 "exp_gen", block
->index
);
3811 print_bitmap_set (dump_file
, PHI_GEN (block
),
3812 "phi_gen", block
->index
);
3813 print_bitmap_set (dump_file
, TMP_GEN (block
),
3814 "tmp_gen", block
->index
);
3815 print_bitmap_set (dump_file
, AVAIL_OUT (block
),
3816 "avail_out", block
->index
);
3819 /* Put the dominator children of BLOCK on the worklist of blocks
3820 to compute available sets for. */
3821 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
3823 son
= next_dom_son (CDI_DOMINATORS
, son
))
3824 worklist
[sp
++] = son
;
3831 /* Local state for the eliminate domwalk. */
3832 static vec
<gimple
*> el_to_remove
;
3833 static vec
<gimple
*> el_to_fixup
;
3834 static unsigned int el_todo
;
3835 static vec
<tree
> el_avail
;
3836 static vec
<tree
> el_avail_stack
;
3838 /* Return a leader for OP that is available at the current point of the
3839 eliminate domwalk. */
3842 eliminate_avail (tree op
)
3844 tree valnum
= VN_INFO (op
)->valnum
;
3845 if (TREE_CODE (valnum
) == SSA_NAME
)
3847 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
3849 if (el_avail
.length () > SSA_NAME_VERSION (valnum
))
3850 return el_avail
[SSA_NAME_VERSION (valnum
)];
3852 else if (is_gimple_min_invariant (valnum
))
3857 /* At the current point of the eliminate domwalk make OP available. */
3860 eliminate_push_avail (tree op
)
3862 tree valnum
= VN_INFO (op
)->valnum
;
3863 if (TREE_CODE (valnum
) == SSA_NAME
)
3865 if (el_avail
.length () <= SSA_NAME_VERSION (valnum
))
3866 el_avail
.safe_grow_cleared (SSA_NAME_VERSION (valnum
) + 1);
3868 if (el_avail
[SSA_NAME_VERSION (valnum
)])
3869 pushop
= el_avail
[SSA_NAME_VERSION (valnum
)];
3870 el_avail_stack
.safe_push (pushop
);
3871 el_avail
[SSA_NAME_VERSION (valnum
)] = op
;
3875 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
3876 the leader for the expression if insertion was successful. */
3879 eliminate_insert (gimple_stmt_iterator
*gsi
, tree val
)
3881 gimple
*stmt
= gimple_seq_first_stmt (VN_INFO (val
)->expr
);
3882 if (!is_gimple_assign (stmt
)
3883 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt
))
3884 && gimple_assign_rhs_code (stmt
) != VIEW_CONVERT_EXPR
))
3887 tree op
= gimple_assign_rhs1 (stmt
);
3888 if (gimple_assign_rhs_code (stmt
) == VIEW_CONVERT_EXPR
)
3889 op
= TREE_OPERAND (op
, 0);
3890 tree leader
= TREE_CODE (op
) == SSA_NAME
? eliminate_avail (op
) : op
;
3894 gimple_seq stmts
= NULL
;
3895 tree res
= gimple_build (&stmts
, gimple_assign_rhs_code (stmt
),
3896 TREE_TYPE (val
), leader
);
3897 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
3898 VN_INFO_GET (res
)->valnum
= val
;
3900 if (TREE_CODE (leader
) == SSA_NAME
)
3901 gimple_set_plf (SSA_NAME_DEF_STMT (leader
), NECESSARY
, true);
3903 pre_stats
.insertions
++;
3904 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3906 fprintf (dump_file
, "Inserted ");
3907 print_gimple_stmt (dump_file
, SSA_NAME_DEF_STMT (res
), 0, 0);
3913 class eliminate_dom_walker
: public dom_walker
3916 eliminate_dom_walker (cdi_direction direction
, bool do_pre_
)
3917 : dom_walker (direction
), do_pre (do_pre_
) {}
3919 virtual void before_dom_children (basic_block
);
3920 virtual void after_dom_children (basic_block
);
3925 /* Perform elimination for the basic-block B during the domwalk. */
3928 eliminate_dom_walker::before_dom_children (basic_block b
)
3931 el_avail_stack
.safe_push (NULL_TREE
);
3933 /* ??? If we do nothing for unreachable blocks then this will confuse
3934 tailmerging. Eventually we can reduce its reliance on SCCVN now
3935 that we fully copy/constant-propagate (most) things. */
3937 for (gphi_iterator gsi
= gsi_start_phis (b
); !gsi_end_p (gsi
);)
3939 gphi
*phi
= gsi
.phi ();
3940 tree res
= PHI_RESULT (phi
);
3942 if (virtual_operand_p (res
))
3948 tree sprime
= eliminate_avail (res
);
3952 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3954 fprintf (dump_file
, "Replaced redundant PHI node defining ");
3955 print_generic_expr (dump_file
, res
, 0);
3956 fprintf (dump_file
, " with ");
3957 print_generic_expr (dump_file
, sprime
, 0);
3958 fprintf (dump_file
, "\n");
3961 /* If we inserted this PHI node ourself, it's not an elimination. */
3963 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
)))
3966 pre_stats
.eliminations
++;
3968 /* If we will propagate into all uses don't bother to do
3970 if (may_propagate_copy (res
, sprime
))
3972 /* Mark the PHI for removal. */
3973 el_to_remove
.safe_push (phi
);
3978 remove_phi_node (&gsi
, false);
3981 && !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
))
3982 && TREE_CODE (sprime
) == SSA_NAME
)
3983 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
3985 if (!useless_type_conversion_p (TREE_TYPE (res
), TREE_TYPE (sprime
)))
3986 sprime
= fold_convert (TREE_TYPE (res
), sprime
);
3987 gimple
*stmt
= gimple_build_assign (res
, sprime
);
3988 /* ??? It cannot yet be necessary (DOM walk). */
3989 gimple_set_plf (stmt
, NECESSARY
, gimple_plf (phi
, NECESSARY
));
3991 gimple_stmt_iterator gsi2
= gsi_after_labels (b
);
3992 gsi_insert_before (&gsi2
, stmt
, GSI_NEW_STMT
);
3996 eliminate_push_avail (res
);
4000 for (gimple_stmt_iterator gsi
= gsi_start_bb (b
);
4004 tree sprime
= NULL_TREE
;
4005 gimple
*stmt
= gsi_stmt (gsi
);
4006 tree lhs
= gimple_get_lhs (stmt
);
4007 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
4008 && !gimple_has_volatile_ops (stmt
)
4009 /* See PR43491. Do not replace a global register variable when
4010 it is a the RHS of an assignment. Do replace local register
4011 variables since gcc does not guarantee a local variable will
4012 be allocated in register.
4013 ??? The fix isn't effective here. This should instead
4014 be ensured by not value-numbering them the same but treating
4015 them like volatiles? */
4016 && !(gimple_assign_single_p (stmt
)
4017 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == VAR_DECL
4018 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
))
4019 && is_global_var (gimple_assign_rhs1 (stmt
)))))
4021 sprime
= eliminate_avail (lhs
);
4024 /* If there is no existing usable leader but SCCVN thinks
4025 it has an expression it wants to use as replacement,
4027 tree val
= VN_INFO (lhs
)->valnum
;
4029 && TREE_CODE (val
) == SSA_NAME
4030 && VN_INFO (val
)->needs_insertion
4031 && VN_INFO (val
)->expr
!= NULL
4032 && (sprime
= eliminate_insert (&gsi
, val
)) != NULL_TREE
)
4033 eliminate_push_avail (sprime
);
4036 /* If this now constitutes a copy duplicate points-to
4037 and range info appropriately. This is especially
4038 important for inserted code. See tree-ssa-copy.c
4039 for similar code. */
4041 && TREE_CODE (sprime
) == SSA_NAME
)
4043 basic_block sprime_b
= gimple_bb (SSA_NAME_DEF_STMT (sprime
));
4044 if (POINTER_TYPE_P (TREE_TYPE (lhs
))
4045 && SSA_NAME_PTR_INFO (lhs
)
4046 && !SSA_NAME_PTR_INFO (sprime
))
4048 duplicate_ssa_name_ptr_info (sprime
,
4049 SSA_NAME_PTR_INFO (lhs
));
4051 mark_ptr_info_alignment_unknown
4052 (SSA_NAME_PTR_INFO (sprime
));
4054 else if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
4055 && SSA_NAME_RANGE_INFO (lhs
)
4056 && !SSA_NAME_RANGE_INFO (sprime
)
4058 duplicate_ssa_name_range_info (sprime
,
4059 SSA_NAME_RANGE_TYPE (lhs
),
4060 SSA_NAME_RANGE_INFO (lhs
));
4063 /* Inhibit the use of an inserted PHI on a loop header when
4064 the address of the memory reference is a simple induction
4065 variable. In other cases the vectorizer won't do anything
4066 anyway (either it's loop invariant or a complicated
4069 && TREE_CODE (sprime
) == SSA_NAME
4071 && flag_tree_loop_vectorize
4072 && loop_outer (b
->loop_father
)
4073 && has_zero_uses (sprime
)
4074 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))
4075 && gimple_assign_load_p (stmt
))
4077 gimple
*def_stmt
= SSA_NAME_DEF_STMT (sprime
);
4078 basic_block def_bb
= gimple_bb (def_stmt
);
4079 if (gimple_code (def_stmt
) == GIMPLE_PHI
4080 && b
->loop_father
->header
== def_bb
)
4085 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
4088 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (op
));
4090 && flow_bb_inside_loop_p (b
->loop_father
, def_bb
)
4091 && simple_iv (b
->loop_father
,
4092 b
->loop_father
, op
, &iv
, true))
4100 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4102 fprintf (dump_file
, "Not replacing ");
4103 print_gimple_expr (dump_file
, stmt
, 0, 0);
4104 fprintf (dump_file
, " with ");
4105 print_generic_expr (dump_file
, sprime
, 0);
4106 fprintf (dump_file
, " which would add a loop"
4107 " carried dependence to loop %d\n",
4108 b
->loop_father
->num
);
4110 /* Don't keep sprime available. */
4118 /* If we can propagate the value computed for LHS into
4119 all uses don't bother doing anything with this stmt. */
4120 if (may_propagate_copy (lhs
, sprime
))
4122 /* Mark it for removal. */
4123 el_to_remove
.safe_push (stmt
);
4125 /* ??? Don't count copy/constant propagations. */
4126 if (gimple_assign_single_p (stmt
)
4127 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4128 || gimple_assign_rhs1 (stmt
) == sprime
))
4131 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4133 fprintf (dump_file
, "Replaced ");
4134 print_gimple_expr (dump_file
, stmt
, 0, 0);
4135 fprintf (dump_file
, " with ");
4136 print_generic_expr (dump_file
, sprime
, 0);
4137 fprintf (dump_file
, " in all uses of ");
4138 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4141 pre_stats
.eliminations
++;
4145 /* If this is an assignment from our leader (which
4146 happens in the case the value-number is a constant)
4147 then there is nothing to do. */
4148 if (gimple_assign_single_p (stmt
)
4149 && sprime
== gimple_assign_rhs1 (stmt
))
4152 /* Else replace its RHS. */
4153 bool can_make_abnormal_goto
4154 = is_gimple_call (stmt
)
4155 && stmt_can_make_abnormal_goto (stmt
);
4157 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4159 fprintf (dump_file
, "Replaced ");
4160 print_gimple_expr (dump_file
, stmt
, 0, 0);
4161 fprintf (dump_file
, " with ");
4162 print_generic_expr (dump_file
, sprime
, 0);
4163 fprintf (dump_file
, " in ");
4164 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4167 if (TREE_CODE (sprime
) == SSA_NAME
)
4168 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4171 pre_stats
.eliminations
++;
4172 gimple
*orig_stmt
= stmt
;
4173 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
4174 TREE_TYPE (sprime
)))
4175 sprime
= fold_convert (TREE_TYPE (lhs
), sprime
);
4176 tree vdef
= gimple_vdef (stmt
);
4177 tree vuse
= gimple_vuse (stmt
);
4178 propagate_tree_value_into_stmt (&gsi
, sprime
);
4179 stmt
= gsi_stmt (gsi
);
4181 if (vdef
!= gimple_vdef (stmt
))
4182 VN_INFO (vdef
)->valnum
= vuse
;
4184 /* If we removed EH side-effects from the statement, clean
4185 its EH information. */
4186 if (maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
))
4188 bitmap_set_bit (need_eh_cleanup
,
4189 gimple_bb (stmt
)->index
);
4190 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4191 fprintf (dump_file
, " Removed EH side-effects.\n");
4194 /* Likewise for AB side-effects. */
4195 if (can_make_abnormal_goto
4196 && !stmt_can_make_abnormal_goto (stmt
))
4198 bitmap_set_bit (need_ab_cleanup
,
4199 gimple_bb (stmt
)->index
);
4200 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4201 fprintf (dump_file
, " Removed AB side-effects.\n");
4208 /* If the statement is a scalar store, see if the expression
4209 has the same value number as its rhs. If so, the store is
4211 if (gimple_assign_single_p (stmt
)
4212 && !gimple_has_volatile_ops (stmt
)
4213 && !is_gimple_reg (gimple_assign_lhs (stmt
))
4214 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4215 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
))))
4218 tree rhs
= gimple_assign_rhs1 (stmt
);
4219 val
= vn_reference_lookup (gimple_assign_lhs (stmt
),
4220 gimple_vuse (stmt
), VN_WALK
, NULL
);
4221 if (TREE_CODE (rhs
) == SSA_NAME
)
4222 rhs
= VN_INFO (rhs
)->valnum
;
4224 && operand_equal_p (val
, rhs
, 0))
4226 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4228 fprintf (dump_file
, "Deleted redundant store ");
4229 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4232 /* Queue stmt for removal. */
4233 el_to_remove
.safe_push (stmt
);
4238 /* If this is a control statement value numbering left edges
4239 unexecuted on force the condition in a way consistent with
4241 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
4243 if ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
)
4244 ^ (EDGE_SUCC (b
, 1)->flags
& EDGE_EXECUTABLE
))
4246 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4248 fprintf (dump_file
, "Removing unexecutable edge from ");
4249 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4251 if (((EDGE_SUCC (b
, 0)->flags
& EDGE_TRUE_VALUE
) != 0)
4252 == ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
) != 0))
4253 gimple_cond_make_true (cond
);
4255 gimple_cond_make_false (cond
);
4257 el_todo
|= TODO_cleanup_cfg
;
4262 bool can_make_abnormal_goto
= stmt_can_make_abnormal_goto (stmt
);
4263 bool was_noreturn
= (is_gimple_call (stmt
)
4264 && gimple_call_noreturn_p (stmt
));
4265 tree vdef
= gimple_vdef (stmt
);
4266 tree vuse
= gimple_vuse (stmt
);
4268 /* If we didn't replace the whole stmt (or propagate the result
4269 into all uses), replace all uses on this stmt with their
4271 use_operand_p use_p
;
4273 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
4275 tree use
= USE_FROM_PTR (use_p
);
4276 /* ??? The call code above leaves stmt operands un-updated. */
4277 if (TREE_CODE (use
) != SSA_NAME
)
4279 tree sprime
= eliminate_avail (use
);
4280 if (sprime
&& sprime
!= use
4281 && may_propagate_copy (use
, sprime
)
4282 /* We substitute into debug stmts to avoid excessive
4283 debug temporaries created by removed stmts, but we need
4284 to avoid doing so for inserted sprimes as we never want
4285 to create debug temporaries for them. */
4287 || TREE_CODE (sprime
) != SSA_NAME
4288 || !is_gimple_debug (stmt
)
4289 || !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))))
4291 propagate_value (use_p
, sprime
);
4292 gimple_set_modified (stmt
, true);
4293 if (TREE_CODE (sprime
) == SSA_NAME
4294 && !is_gimple_debug (stmt
))
4295 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4300 /* Visit indirect calls and turn them into direct calls if
4301 possible using the devirtualization machinery. */
4302 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
4304 tree fn
= gimple_call_fn (call_stmt
);
4306 && flag_devirtualize
4307 && virtual_method_call_p (fn
))
4309 tree otr_type
= obj_type_ref_class (fn
);
4311 ipa_polymorphic_call_context
context (current_function_decl
, fn
, stmt
, &instance
);
4314 context
.get_dynamic_type (instance
, OBJ_TYPE_REF_OBJECT (fn
), otr_type
, stmt
);
4316 vec
<cgraph_node
*>targets
4317 = possible_polymorphic_call_targets (obj_type_ref_class (fn
),
4319 (OBJ_TYPE_REF_TOKEN (fn
)),
4323 dump_possible_polymorphic_call_targets (dump_file
,
4324 obj_type_ref_class (fn
),
4326 (OBJ_TYPE_REF_TOKEN (fn
)),
4328 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
4331 if (targets
.length () == 1)
4332 fn
= targets
[0]->decl
;
4334 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
4335 if (dump_enabled_p ())
4337 location_t loc
= gimple_location_safe (stmt
);
4338 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
4339 "converting indirect call to "
4341 cgraph_node::get (fn
)->name ());
4343 gimple_call_set_fndecl (call_stmt
, fn
);
4344 maybe_remove_unused_call_args (cfun
, call_stmt
);
4345 gimple_set_modified (stmt
, true);
4350 if (gimple_modified_p (stmt
))
4352 /* If a formerly non-invariant ADDR_EXPR is turned into an
4353 invariant one it was on a separate stmt. */
4354 if (gimple_assign_single_p (stmt
)
4355 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
4356 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
4357 gimple
*old_stmt
= stmt
;
4358 if (is_gimple_call (stmt
))
4360 /* ??? Only fold calls inplace for now, this may create new
4361 SSA names which in turn will confuse free_scc_vn SSA name
4363 fold_stmt_inplace (&gsi
);
4364 /* When changing a call into a noreturn call, cfg cleanup
4365 is needed to fix up the noreturn call. */
4366 if (!was_noreturn
&& gimple_call_noreturn_p (stmt
))
4367 el_to_fixup
.safe_push (stmt
);
4372 stmt
= gsi_stmt (gsi
);
4373 if ((gimple_code (stmt
) == GIMPLE_COND
4374 && (gimple_cond_true_p (as_a
<gcond
*> (stmt
))
4375 || gimple_cond_false_p (as_a
<gcond
*> (stmt
))))
4376 || (gimple_code (stmt
) == GIMPLE_SWITCH
4377 && TREE_CODE (gimple_switch_index (
4378 as_a
<gswitch
*> (stmt
)))
4380 el_todo
|= TODO_cleanup_cfg
;
4382 /* If we removed EH side-effects from the statement, clean
4383 its EH information. */
4384 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
4386 bitmap_set_bit (need_eh_cleanup
,
4387 gimple_bb (stmt
)->index
);
4388 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4389 fprintf (dump_file
, " Removed EH side-effects.\n");
4391 /* Likewise for AB side-effects. */
4392 if (can_make_abnormal_goto
4393 && !stmt_can_make_abnormal_goto (stmt
))
4395 bitmap_set_bit (need_ab_cleanup
,
4396 gimple_bb (stmt
)->index
);
4397 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4398 fprintf (dump_file
, " Removed AB side-effects.\n");
4401 if (vdef
!= gimple_vdef (stmt
))
4402 VN_INFO (vdef
)->valnum
= vuse
;
4405 /* Make new values available - for fully redundant LHS we
4406 continue with the next stmt above and skip this. */
4408 FOR_EACH_SSA_DEF_OPERAND (defp
, stmt
, iter
, SSA_OP_DEF
)
4409 eliminate_push_avail (DEF_FROM_PTR (defp
));
4412 /* Replace destination PHI arguments. */
4415 FOR_EACH_EDGE (e
, ei
, b
->succs
)
4417 for (gphi_iterator gsi
= gsi_start_phis (e
->dest
);
4421 gphi
*phi
= gsi
.phi ();
4422 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
4423 tree arg
= USE_FROM_PTR (use_p
);
4424 if (TREE_CODE (arg
) != SSA_NAME
4425 || virtual_operand_p (arg
))
4427 tree sprime
= eliminate_avail (arg
);
4428 if (sprime
&& may_propagate_copy (arg
, sprime
))
4430 propagate_value (use_p
, sprime
);
4431 if (TREE_CODE (sprime
) == SSA_NAME
)
4432 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
4438 /* Make no longer available leaders no longer available. */
4441 eliminate_dom_walker::after_dom_children (basic_block
)
4444 while ((entry
= el_avail_stack
.pop ()) != NULL_TREE
)
4446 tree valnum
= VN_INFO (entry
)->valnum
;
4447 tree old
= el_avail
[SSA_NAME_VERSION (valnum
)];
4449 el_avail
[SSA_NAME_VERSION (valnum
)] = NULL_TREE
;
4451 el_avail
[SSA_NAME_VERSION (valnum
)] = entry
;
4455 /* Eliminate fully redundant computations. */
4458 eliminate (bool do_pre
)
4460 gimple_stmt_iterator gsi
;
4463 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
4464 need_ab_cleanup
= BITMAP_ALLOC (NULL
);
4466 el_to_remove
.create (0);
4467 el_to_fixup
.create (0);
4469 el_avail
.create (num_ssa_names
);
4470 el_avail_stack
.create (0);
4472 eliminate_dom_walker (CDI_DOMINATORS
,
4473 do_pre
).walk (cfun
->cfg
->x_entry_block_ptr
);
4475 el_avail
.release ();
4476 el_avail_stack
.release ();
4478 /* We cannot remove stmts during BB walk, especially not release SSA
4479 names there as this confuses the VN machinery. The stmts ending
4480 up in el_to_remove are either stores or simple copies.
4481 Remove stmts in reverse order to make debug stmt creation possible. */
4482 while (!el_to_remove
.is_empty ())
4484 stmt
= el_to_remove
.pop ();
4486 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4488 fprintf (dump_file
, "Removing dead stmt ");
4489 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4493 if (gimple_code (stmt
) == GIMPLE_PHI
)
4494 lhs
= gimple_phi_result (stmt
);
4496 lhs
= gimple_get_lhs (stmt
);
4499 && TREE_CODE (lhs
) == SSA_NAME
)
4500 bitmap_clear_bit (inserted_exprs
, SSA_NAME_VERSION (lhs
));
4502 gsi
= gsi_for_stmt (stmt
);
4503 if (gimple_code (stmt
) == GIMPLE_PHI
)
4504 remove_phi_node (&gsi
, true);
4507 basic_block bb
= gimple_bb (stmt
);
4508 unlink_stmt_vdef (stmt
);
4509 if (gsi_remove (&gsi
, true))
4510 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
4511 release_defs (stmt
);
4514 /* Removing a stmt may expose a forwarder block. */
4515 el_todo
|= TODO_cleanup_cfg
;
4517 el_to_remove
.release ();
4519 /* Fixup stmts that became noreturn calls. This may require splitting
4520 blocks and thus isn't possible during the dominator walk. Do this
4521 in reverse order so we don't inadvertedly remove a stmt we want to
4522 fixup by visiting a dominating now noreturn call first. */
4523 while (!el_to_fixup
.is_empty ())
4525 stmt
= el_to_fixup
.pop ();
4527 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4529 fprintf (dump_file
, "Fixing up noreturn call ");
4530 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4533 if (fixup_noreturn_call (stmt
))
4534 el_todo
|= TODO_cleanup_cfg
;
4536 el_to_fixup
.release ();
4541 /* Perform CFG cleanups made necessary by elimination. */
4544 fini_eliminate (void)
4546 bool do_eh_cleanup
= !bitmap_empty_p (need_eh_cleanup
);
4547 bool do_ab_cleanup
= !bitmap_empty_p (need_ab_cleanup
);
4550 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
4553 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup
);
4555 BITMAP_FREE (need_eh_cleanup
);
4556 BITMAP_FREE (need_ab_cleanup
);
4558 if (do_eh_cleanup
|| do_ab_cleanup
)
4559 return TODO_cleanup_cfg
;
4563 /* Borrow a bit of tree-ssa-dce.c for the moment.
4564 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4565 this may be a bit faster, and we may want critical edges kept split. */
4567 /* If OP's defining statement has not already been determined to be necessary,
4568 mark that statement necessary. Return the stmt, if it is newly
4571 static inline gimple
*
4572 mark_operand_necessary (tree op
)
4578 if (TREE_CODE (op
) != SSA_NAME
)
4581 stmt
= SSA_NAME_DEF_STMT (op
);
4584 if (gimple_plf (stmt
, NECESSARY
)
4585 || gimple_nop_p (stmt
))
4588 gimple_set_plf (stmt
, NECESSARY
, true);
4592 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4593 to insert PHI nodes sometimes, and because value numbering of casts isn't
4594 perfect, we sometimes end up inserting dead code. This simple DCE-like
4595 pass removes any insertions we made that weren't actually used. */
4598 remove_dead_inserted_code (void)
4605 worklist
= BITMAP_ALLOC (NULL
);
4606 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4608 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4609 if (gimple_plf (t
, NECESSARY
))
4610 bitmap_set_bit (worklist
, i
);
4612 while (!bitmap_empty_p (worklist
))
4614 i
= bitmap_first_set_bit (worklist
);
4615 bitmap_clear_bit (worklist
, i
);
4616 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4618 /* PHI nodes are somewhat special in that each PHI alternative has
4619 data and control dependencies. All the statements feeding the
4620 PHI node's arguments are always necessary. */
4621 if (gimple_code (t
) == GIMPLE_PHI
)
4625 for (k
= 0; k
< gimple_phi_num_args (t
); k
++)
4627 tree arg
= PHI_ARG_DEF (t
, k
);
4628 if (TREE_CODE (arg
) == SSA_NAME
)
4630 gimple
*n
= mark_operand_necessary (arg
);
4632 bitmap_set_bit (worklist
, SSA_NAME_VERSION (arg
));
4638 /* Propagate through the operands. Examine all the USE, VUSE and
4639 VDEF operands in this statement. Mark all the statements
4640 which feed this statement's uses as necessary. */
4644 /* The operands of VDEF expressions are also needed as they
4645 represent potential definitions that may reach this
4646 statement (VDEF operands allow us to follow def-def
4649 FOR_EACH_SSA_TREE_OPERAND (use
, t
, iter
, SSA_OP_ALL_USES
)
4651 gimple
*n
= mark_operand_necessary (use
);
4653 bitmap_set_bit (worklist
, SSA_NAME_VERSION (use
));
4658 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4660 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4661 if (!gimple_plf (t
, NECESSARY
))
4663 gimple_stmt_iterator gsi
;
4665 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4667 fprintf (dump_file
, "Removing unnecessary insertion:");
4668 print_gimple_stmt (dump_file
, t
, 0, 0);
4671 gsi
= gsi_for_stmt (t
);
4672 if (gimple_code (t
) == GIMPLE_PHI
)
4673 remove_phi_node (&gsi
, true);
4676 gsi_remove (&gsi
, true);
4681 BITMAP_FREE (worklist
);
4685 /* Initialize data structures used by PRE. */
4692 next_expression_id
= 1;
4693 expressions
.create (0);
4694 expressions
.safe_push (NULL
);
4695 value_expressions
.create (get_max_value_id () + 1);
4696 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
4697 name_to_id
.create (0);
4699 inserted_exprs
= BITMAP_ALLOC (NULL
);
4701 connect_infinite_loops_to_exit ();
4702 memset (&pre_stats
, 0, sizeof (pre_stats
));
4704 postorder
= XNEWVEC (int, n_basic_blocks_for_fn (cfun
));
4705 postorder_num
= inverted_post_order_compute (postorder
);
4707 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets
));
4709 calculate_dominance_info (CDI_POST_DOMINATORS
);
4710 calculate_dominance_info (CDI_DOMINATORS
);
4712 bitmap_obstack_initialize (&grand_bitmap_obstack
);
4713 phi_translate_table
= new hash_table
<expr_pred_trans_d
> (5110);
4714 expression_to_id
= new hash_table
<pre_expr_d
> (num_ssa_names
* 3);
4715 FOR_ALL_BB_FN (bb
, cfun
)
4717 EXP_GEN (bb
) = bitmap_set_new ();
4718 PHI_GEN (bb
) = bitmap_set_new ();
4719 TMP_GEN (bb
) = bitmap_set_new ();
4720 AVAIL_OUT (bb
) = bitmap_set_new ();
4725 /* Deallocate data structures used by PRE. */
4731 value_expressions
.release ();
4732 BITMAP_FREE (inserted_exprs
);
4733 bitmap_obstack_release (&grand_bitmap_obstack
);
4734 bitmap_set_pool
.release ();
4735 pre_expr_pool
.release ();
4736 delete phi_translate_table
;
4737 phi_translate_table
= NULL
;
4738 delete expression_to_id
;
4739 expression_to_id
= NULL
;
4740 name_to_id
.release ();
4742 free_aux_for_blocks ();
4744 free_dominance_info (CDI_POST_DOMINATORS
);
4749 const pass_data pass_data_pre
=
4751 GIMPLE_PASS
, /* type */
4753 OPTGROUP_NONE
, /* optinfo_flags */
4754 TV_TREE_PRE
, /* tv_id */
4755 /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before
4757 ( PROP_no_crit_edges
| PROP_cfg
| PROP_ssa
), /* properties_required */
4758 0, /* properties_provided */
4759 PROP_no_crit_edges
, /* properties_destroyed */
4760 TODO_rebuild_alias
, /* todo_flags_start */
4761 0, /* todo_flags_finish */
4764 class pass_pre
: public gimple_opt_pass
4767 pass_pre (gcc::context
*ctxt
)
4768 : gimple_opt_pass (pass_data_pre
, ctxt
)
4771 /* opt_pass methods: */
4772 virtual bool gate (function
*) { return flag_tree_pre
!= 0; }
4773 virtual unsigned int execute (function
*);
4775 }; // class pass_pre
4778 pass_pre::execute (function
*fun
)
4780 unsigned int todo
= 0;
4782 do_partial_partial
=
4783 flag_tree_partial_pre
&& optimize_function_for_speed_p (fun
);
4785 /* This has to happen before SCCVN runs because
4786 loop_optimizer_init may create new phis, etc. */
4787 loop_optimizer_init (LOOPS_NORMAL
);
4789 if (!run_scc_vn (VN_WALK
))
4791 loop_optimizer_finalize ();
4798 /* Collect and value number expressions computed in each basic block. */
4801 /* Insert can get quite slow on an incredibly large number of basic
4802 blocks due to some quadratic behavior. Until this behavior is
4803 fixed, don't run it when he have an incredibly large number of
4804 bb's. If we aren't going to run insert, there is no point in
4805 computing ANTIC, either, even though it's plenty fast. */
4806 if (n_basic_blocks_for_fn (fun
) < 4000)
4812 /* Make sure to remove fake edges before committing our inserts.
4813 This makes sure we don't end up with extra critical edges that
4814 we would need to split. */
4815 remove_fake_exit_edges ();
4816 gsi_commit_edge_inserts ();
4818 /* Eliminate folds statements which might (should not...) end up
4819 not keeping virtual operands up-to-date. */
4820 gcc_assert (!need_ssa_update_p (fun
));
4822 /* Remove all the redundant expressions. */
4823 todo
|= eliminate (true);
4825 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
4826 statistics_counter_event (fun
, "PA inserted", pre_stats
.pa_insert
);
4827 statistics_counter_event (fun
, "New PHIs", pre_stats
.phis
);
4828 statistics_counter_event (fun
, "Eliminated", pre_stats
.eliminations
);
4830 clear_expression_ids ();
4831 remove_dead_inserted_code ();
4835 todo
|= fini_eliminate ();
4836 loop_optimizer_finalize ();
4838 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4839 case we can merge the block with the remaining predecessor of the block.
4841 - call merge_blocks after each tail merge iteration
4842 - call merge_blocks after all tail merge iterations
4843 - mark TODO_cleanup_cfg when necessary
4844 - share the cfg cleanup with fini_pre. */
4845 todo
|= tail_merge_optimize (todo
);
4849 /* Tail merging invalidates the virtual SSA web, together with
4850 cfg-cleanup opportunities exposed by PRE this will wreck the
4851 SSA updating machinery. So make sure to run update-ssa
4852 manually, before eventually scheduling cfg-cleanup as part of
4854 update_ssa (TODO_update_ssa_only_virtuals
);
4862 make_pass_pre (gcc::context
*ctxt
)
4864 return new pass_pre (ctxt
);
4869 const pass_data pass_data_fre
=
4871 GIMPLE_PASS
, /* type */
4873 OPTGROUP_NONE
, /* optinfo_flags */
4874 TV_TREE_FRE
, /* tv_id */
4875 ( PROP_cfg
| PROP_ssa
), /* properties_required */
4876 0, /* properties_provided */
4877 0, /* properties_destroyed */
4878 0, /* todo_flags_start */
4879 0, /* todo_flags_finish */
4882 class pass_fre
: public gimple_opt_pass
4885 pass_fre (gcc::context
*ctxt
)
4886 : gimple_opt_pass (pass_data_fre
, ctxt
)
4889 /* opt_pass methods: */
4890 opt_pass
* clone () { return new pass_fre (m_ctxt
); }
4891 virtual bool gate (function
*) { return flag_tree_fre
!= 0; }
4892 virtual unsigned int execute (function
*);
4894 }; // class pass_fre
4897 pass_fre::execute (function
*fun
)
4899 unsigned int todo
= 0;
4901 if (!run_scc_vn (VN_WALKREWRITE
))
4904 memset (&pre_stats
, 0, sizeof (pre_stats
));
4906 /* Remove all the redundant expressions. */
4907 todo
|= eliminate (false);
4909 todo
|= fini_eliminate ();
4913 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
4914 statistics_counter_event (fun
, "Eliminated", pre_stats
.eliminations
);
4922 make_pass_fre (gcc::context
*ctxt
)
4924 return new pass_fre (ctxt
);