2 Copyright (C) 2001-2014 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"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-inline.h"
31 #include "hash-table.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-fold.h"
36 #include "gimple-expr.h"
40 #include "gimple-iterator.h"
41 #include "gimplify-me.h"
42 #include "gimple-ssa.h"
44 #include "tree-phinodes.h"
45 #include "ssa-iterators.h"
46 #include "stringpool.h"
47 #include "tree-ssanames.h"
48 #include "tree-ssa-loop.h"
49 #include "tree-into-ssa.h"
53 #include "tree-iterator.h"
54 #include "alloc-pool.h"
56 #include "tree-pass.h"
58 #include "langhooks.h"
60 #include "tree-ssa-sccvn.h"
61 #include "tree-scalar-evolution.h"
66 #include "tree-ssa-propagate.h"
67 #include "ipa-utils.h"
71 1. Avail sets can be shared by making an avail_find_leader that
72 walks up the dominator tree and looks in those avail sets.
73 This might affect code optimality, it's unclear right now.
74 2. Strength reduction can be performed by anticipating expressions
75 we can repair later on.
76 3. We can do back-substitution or smarter value numbering to catch
77 commutative expressions split up over multiple statements.
80 /* For ease of terminology, "expression node" in the below refers to
81 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
82 represent the actual statement containing the expressions we care about,
83 and we cache the value number by putting it in the expression. */
87 First we walk the statements to generate the AVAIL sets, the
88 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
89 generation of values/expressions by a given block. We use them
90 when computing the ANTIC sets. The AVAIL sets consist of
91 SSA_NAME's that represent values, so we know what values are
92 available in what blocks. AVAIL is a forward dataflow problem. In
93 SSA, values are never killed, so we don't need a kill set, or a
94 fixpoint iteration, in order to calculate the AVAIL sets. In
95 traditional parlance, AVAIL sets tell us the downsafety of the
98 Next, we generate the ANTIC sets. These sets represent the
99 anticipatable expressions. ANTIC is a backwards dataflow
100 problem. An expression is anticipatable in a given block if it could
101 be generated in that block. This means that if we had to perform
102 an insertion in that block, of the value of that expression, we
103 could. Calculating the ANTIC sets requires phi translation of
104 expressions, because the flow goes backwards through phis. We must
105 iterate to a fixpoint of the ANTIC sets, because we have a kill
106 set. Even in SSA form, values are not live over the entire
107 function, only from their definition point onwards. So we have to
108 remove values from the ANTIC set once we go past the definition
109 point of the leaders that make them up.
110 compute_antic/compute_antic_aux performs this computation.
112 Third, we perform insertions to make partially redundant
113 expressions fully redundant.
115 An expression is partially redundant (excluding partial
118 1. It is AVAIL in some, but not all, of the predecessors of a
120 2. It is ANTIC in all the predecessors.
122 In order to make it fully redundant, we insert the expression into
123 the predecessors where it is not available, but is ANTIC.
125 For the partial anticipation case, we only perform insertion if it
126 is partially anticipated in some block, and fully available in all
129 insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
130 performs these steps.
132 Fourth, we eliminate fully redundant expressions.
133 This is a simple statement walk that replaces redundant
134 calculations with the now available values. */
136 /* Representations of value numbers:
138 Value numbers are represented by a representative SSA_NAME. We
139 will create fake SSA_NAME's in situations where we need a
140 representative but do not have one (because it is a complex
141 expression). In order to facilitate storing the value numbers in
142 bitmaps, and keep the number of wasted SSA_NAME's down, we also
143 associate a value_id with each value number, and create full blown
144 ssa_name's only where we actually need them (IE in operands of
145 existing expressions).
147 Theoretically you could replace all the value_id's with
148 SSA_NAME_VERSION, but this would allocate a large number of
149 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
150 It would also require an additional indirection at each point we
153 /* Representation of expressions on value numbers:
155 Expressions consisting of value numbers are represented the same
156 way as our VN internally represents them, with an additional
157 "pre_expr" wrapping around them in order to facilitate storing all
158 of the expressions in the same sets. */
160 /* Representation of sets:
162 The dataflow sets do not need to be sorted in any particular order
163 for the majority of their lifetime, are simply represented as two
164 bitmaps, one that keeps track of values present in the set, and one
165 that keeps track of expressions present in the set.
167 When we need them in topological order, we produce it on demand by
168 transforming the bitmap into an array and sorting it into topo
171 /* Type of expression, used to know which member of the PRE_EXPR union
182 typedef union pre_expr_union_d
187 vn_reference_t reference
;
190 typedef struct pre_expr_d
: typed_noop_remove
<pre_expr_d
>
192 enum pre_expr_kind kind
;
196 /* hash_table support. */
197 typedef pre_expr_d value_type
;
198 typedef pre_expr_d compare_type
;
199 static inline hashval_t
hash (const pre_expr_d
*);
200 static inline int equal (const pre_expr_d
*, const pre_expr_d
*);
203 #define PRE_EXPR_NAME(e) (e)->u.name
204 #define PRE_EXPR_NARY(e) (e)->u.nary
205 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
206 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
208 /* Compare E1 and E1 for equality. */
211 pre_expr_d::equal (const value_type
*e1
, const compare_type
*e2
)
213 if (e1
->kind
!= e2
->kind
)
219 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1
),
220 PRE_EXPR_CONSTANT (e2
));
222 return PRE_EXPR_NAME (e1
) == PRE_EXPR_NAME (e2
);
224 return vn_nary_op_eq (PRE_EXPR_NARY (e1
), PRE_EXPR_NARY (e2
));
226 return vn_reference_eq (PRE_EXPR_REFERENCE (e1
),
227 PRE_EXPR_REFERENCE (e2
));
236 pre_expr_d::hash (const value_type
*e
)
241 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e
));
243 return SSA_NAME_VERSION (PRE_EXPR_NAME (e
));
245 return PRE_EXPR_NARY (e
)->hashcode
;
247 return PRE_EXPR_REFERENCE (e
)->hashcode
;
253 /* Next global expression id number. */
254 static unsigned int next_expression_id
;
256 /* Mapping from expression to id number we can use in bitmap sets. */
257 static vec
<pre_expr
> expressions
;
258 static hash_table
<pre_expr_d
> *expression_to_id
;
259 static vec
<unsigned> name_to_id
;
261 /* Allocate an expression id for EXPR. */
263 static inline unsigned int
264 alloc_expression_id (pre_expr expr
)
266 struct pre_expr_d
**slot
;
267 /* Make sure we won't overflow. */
268 gcc_assert (next_expression_id
+ 1 > next_expression_id
);
269 expr
->id
= next_expression_id
++;
270 expressions
.safe_push (expr
);
271 if (expr
->kind
== NAME
)
273 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
274 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
275 re-allocations by using vec::reserve upfront. There is no
276 vec::quick_grow_cleared unfortunately. */
277 unsigned old_len
= name_to_id
.length ();
278 name_to_id
.reserve (num_ssa_names
- old_len
);
279 name_to_id
.safe_grow_cleared (num_ssa_names
);
280 gcc_assert (name_to_id
[version
] == 0);
281 name_to_id
[version
] = expr
->id
;
285 slot
= expression_to_id
->find_slot (expr
, INSERT
);
289 return next_expression_id
- 1;
292 /* Return the expression id for tree EXPR. */
294 static inline unsigned int
295 get_expression_id (const pre_expr expr
)
300 static inline unsigned int
301 lookup_expression_id (const pre_expr expr
)
303 struct pre_expr_d
**slot
;
305 if (expr
->kind
== NAME
)
307 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
308 if (name_to_id
.length () <= version
)
310 return name_to_id
[version
];
314 slot
= expression_to_id
->find_slot (expr
, NO_INSERT
);
317 return ((pre_expr
)*slot
)->id
;
321 /* Return the existing expression id for EXPR, or create one if one
322 does not exist yet. */
324 static inline unsigned int
325 get_or_alloc_expression_id (pre_expr expr
)
327 unsigned int id
= lookup_expression_id (expr
);
329 return alloc_expression_id (expr
);
330 return expr
->id
= id
;
333 /* Return the expression that has expression id ID */
335 static inline pre_expr
336 expression_for_id (unsigned int id
)
338 return expressions
[id
];
341 /* Free the expression id field in all of our expressions,
342 and then destroy the expressions array. */
345 clear_expression_ids (void)
347 expressions
.release ();
350 static alloc_pool pre_expr_pool
;
352 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
355 get_or_alloc_expr_for_name (tree name
)
357 struct pre_expr_d expr
;
359 unsigned int result_id
;
363 PRE_EXPR_NAME (&expr
) = name
;
364 result_id
= lookup_expression_id (&expr
);
366 return expression_for_id (result_id
);
368 result
= (pre_expr
) pool_alloc (pre_expr_pool
);
370 PRE_EXPR_NAME (result
) = name
;
371 alloc_expression_id (result
);
375 /* An unordered bitmap set. One bitmap tracks values, the other,
377 typedef struct bitmap_set
379 bitmap_head expressions
;
383 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
384 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
386 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
387 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
389 /* Mapping from value id to expressions with that value_id. */
390 static vec
<bitmap
> value_expressions
;
392 /* Sets that we need to keep track of. */
393 typedef struct bb_bitmap_sets
395 /* The EXP_GEN set, which represents expressions/values generated in
397 bitmap_set_t exp_gen
;
399 /* The PHI_GEN set, which represents PHI results generated in a
401 bitmap_set_t phi_gen
;
403 /* The TMP_GEN set, which represents results/temporaries generated
404 in a basic block. IE the LHS of an expression. */
405 bitmap_set_t tmp_gen
;
407 /* The AVAIL_OUT set, which represents which values are available in
408 a given basic block. */
409 bitmap_set_t avail_out
;
411 /* The ANTIC_IN set, which represents which values are anticipatable
412 in a given basic block. */
413 bitmap_set_t antic_in
;
415 /* The PA_IN set, which represents which values are
416 partially anticipatable in a given basic block. */
419 /* The NEW_SETS set, which is used during insertion to augment the
420 AVAIL_OUT set of blocks with the new insertions performed during
421 the current iteration. */
422 bitmap_set_t new_sets
;
424 /* A cache for value_dies_in_block_x. */
427 /* True if we have visited this block during ANTIC calculation. */
428 unsigned int visited
: 1;
430 /* True we have deferred processing this block during ANTIC
431 calculation until its successor is processed. */
432 unsigned int deferred
: 1;
434 /* True when the block contains a call that might not return. */
435 unsigned int contains_may_not_return_call
: 1;
438 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
439 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
440 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
441 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
442 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
443 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
444 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
445 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
446 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
447 #define BB_DEFERRED(BB) ((bb_value_sets_t) ((BB)->aux))->deferred
448 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
451 /* Basic block list in postorder. */
452 static int *postorder
;
453 static int postorder_num
;
455 /* This structure is used to keep track of statistics on what
456 optimization PRE was able to perform. */
459 /* The number of RHS computations eliminated by PRE. */
462 /* The number of new expressions/temporaries generated by PRE. */
465 /* The number of inserts found due to partial anticipation */
468 /* The number of new PHI nodes added by PRE. */
472 static bool do_partial_partial
;
473 static pre_expr
bitmap_find_leader (bitmap_set_t
, unsigned int);
474 static void bitmap_value_insert_into_set (bitmap_set_t
, pre_expr
);
475 static void bitmap_value_replace_in_set (bitmap_set_t
, pre_expr
);
476 static void bitmap_set_copy (bitmap_set_t
, bitmap_set_t
);
477 static bool bitmap_set_contains_value (bitmap_set_t
, unsigned int);
478 static void bitmap_insert_into_set (bitmap_set_t
, pre_expr
);
479 static void bitmap_insert_into_set_1 (bitmap_set_t
, pre_expr
,
481 static bitmap_set_t
bitmap_set_new (void);
482 static tree
create_expression_by_pieces (basic_block
, pre_expr
, gimple_seq
*,
484 static tree
find_or_generate_expression (basic_block
, tree
, gimple_seq
*);
485 static unsigned int get_expr_value_id (pre_expr
);
487 /* We can add and remove elements and entries to and from sets
488 and hash tables, so we use alloc pools for them. */
490 static alloc_pool bitmap_set_pool
;
491 static bitmap_obstack grand_bitmap_obstack
;
493 /* Set of blocks with statements that have had their EH properties changed. */
494 static bitmap need_eh_cleanup
;
496 /* Set of blocks with statements that have had their AB properties changed. */
497 static bitmap need_ab_cleanup
;
499 /* A three tuple {e, pred, v} used to cache phi translations in the
500 phi_translate_table. */
502 typedef struct expr_pred_trans_d
: typed_free_remove
<expr_pred_trans_d
>
504 /* The expression. */
507 /* The predecessor block along which we translated the expression. */
510 /* The value that resulted from the translation. */
513 /* The hashcode for the expression, pred pair. This is cached for
517 /* hash_table support. */
518 typedef expr_pred_trans_d value_type
;
519 typedef expr_pred_trans_d compare_type
;
520 static inline hashval_t
hash (const value_type
*);
521 static inline int equal (const value_type
*, const compare_type
*);
522 } *expr_pred_trans_t
;
523 typedef const struct expr_pred_trans_d
*const_expr_pred_trans_t
;
526 expr_pred_trans_d::hash (const expr_pred_trans_d
*e
)
532 expr_pred_trans_d::equal (const value_type
*ve1
,
533 const compare_type
*ve2
)
535 basic_block b1
= ve1
->pred
;
536 basic_block b2
= ve2
->pred
;
538 /* If they are not translations for the same basic block, they can't
542 return pre_expr_d::equal (ve1
->e
, ve2
->e
);
545 /* The phi_translate_table caches phi translations for a given
546 expression and predecessor. */
547 static hash_table
<expr_pred_trans_d
> *phi_translate_table
;
549 /* Add the tuple mapping from {expression E, basic block PRED} to
550 the phi translation table and return whether it pre-existed. */
553 phi_trans_add (expr_pred_trans_t
*entry
, pre_expr e
, basic_block pred
)
555 expr_pred_trans_t
*slot
;
556 expr_pred_trans_d tem
;
557 hashval_t hash
= iterative_hash_hashval_t (pre_expr_d::hash (e
),
562 slot
= phi_translate_table
->find_slot_with_hash (&tem
, hash
, INSERT
);
569 *entry
= *slot
= XNEW (struct expr_pred_trans_d
);
571 (*entry
)->pred
= pred
;
572 (*entry
)->hashcode
= hash
;
577 /* Add expression E to the expression set of value id V. */
580 add_to_value (unsigned int v
, pre_expr e
)
584 gcc_checking_assert (get_expr_value_id (e
) == v
);
586 if (v
>= value_expressions
.length ())
588 value_expressions
.safe_grow_cleared (v
+ 1);
591 set
= value_expressions
[v
];
594 set
= BITMAP_ALLOC (&grand_bitmap_obstack
);
595 value_expressions
[v
] = set
;
598 bitmap_set_bit (set
, get_or_alloc_expression_id (e
));
601 /* Create a new bitmap set and return it. */
604 bitmap_set_new (void)
606 bitmap_set_t ret
= (bitmap_set_t
) pool_alloc (bitmap_set_pool
);
607 bitmap_initialize (&ret
->expressions
, &grand_bitmap_obstack
);
608 bitmap_initialize (&ret
->values
, &grand_bitmap_obstack
);
612 /* Return the value id for a PRE expression EXPR. */
615 get_expr_value_id (pre_expr expr
)
621 id
= get_constant_value_id (PRE_EXPR_CONSTANT (expr
));
624 id
= VN_INFO (PRE_EXPR_NAME (expr
))->value_id
;
627 id
= PRE_EXPR_NARY (expr
)->value_id
;
630 id
= PRE_EXPR_REFERENCE (expr
)->value_id
;
635 /* ??? We cannot assert that expr has a value-id (it can be 0), because
636 we assign value-ids only to expressions that have a result
637 in set_hashtable_value_ids. */
641 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
644 sccvn_valnum_from_value_id (unsigned int val
)
648 bitmap exprset
= value_expressions
[val
];
649 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
651 pre_expr vexpr
= expression_for_id (i
);
652 if (vexpr
->kind
== NAME
)
653 return VN_INFO (PRE_EXPR_NAME (vexpr
))->valnum
;
654 else if (vexpr
->kind
== CONSTANT
)
655 return PRE_EXPR_CONSTANT (vexpr
);
660 /* Remove an expression EXPR from a bitmapped set. */
663 bitmap_remove_from_set (bitmap_set_t set
, pre_expr expr
)
665 unsigned int val
= get_expr_value_id (expr
);
666 if (!value_id_constant_p (val
))
668 bitmap_clear_bit (&set
->values
, val
);
669 bitmap_clear_bit (&set
->expressions
, get_expression_id (expr
));
674 bitmap_insert_into_set_1 (bitmap_set_t set
, pre_expr expr
,
675 unsigned int val
, bool allow_constants
)
677 if (allow_constants
|| !value_id_constant_p (val
))
679 /* We specifically expect this and only this function to be able to
680 insert constants into a set. */
681 bitmap_set_bit (&set
->values
, val
);
682 bitmap_set_bit (&set
->expressions
, get_or_alloc_expression_id (expr
));
686 /* Insert an expression EXPR into a bitmapped set. */
689 bitmap_insert_into_set (bitmap_set_t set
, pre_expr expr
)
691 bitmap_insert_into_set_1 (set
, expr
, get_expr_value_id (expr
), false);
694 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
697 bitmap_set_copy (bitmap_set_t dest
, bitmap_set_t orig
)
699 bitmap_copy (&dest
->expressions
, &orig
->expressions
);
700 bitmap_copy (&dest
->values
, &orig
->values
);
704 /* Free memory used up by SET. */
706 bitmap_set_free (bitmap_set_t set
)
708 bitmap_clear (&set
->expressions
);
709 bitmap_clear (&set
->values
);
713 /* Generate an topological-ordered array of bitmap set SET. */
716 sorted_array_from_bitmap_set (bitmap_set_t set
)
719 bitmap_iterator bi
, bj
;
720 vec
<pre_expr
> result
;
722 /* Pre-allocate enough space for the array. */
723 result
.create (bitmap_count_bits (&set
->expressions
));
725 FOR_EACH_VALUE_ID_IN_SET (set
, i
, bi
)
727 /* The number of expressions having a given value is usually
728 relatively small. Thus, rather than making a vector of all
729 the expressions and sorting it by value-id, we walk the values
730 and check in the reverse mapping that tells us what expressions
731 have a given value, to filter those in our set. As a result,
732 the expressions are inserted in value-id order, which means
735 If this is somehow a significant lose for some cases, we can
736 choose which set to walk based on the set size. */
737 bitmap exprset
= value_expressions
[i
];
738 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, j
, bj
)
740 if (bitmap_bit_p (&set
->expressions
, j
))
741 result
.quick_push (expression_for_id (j
));
748 /* Perform bitmapped set operation DEST &= ORIG. */
751 bitmap_set_and (bitmap_set_t dest
, bitmap_set_t orig
)
759 bitmap_initialize (&temp
, &grand_bitmap_obstack
);
761 bitmap_and_into (&dest
->values
, &orig
->values
);
762 bitmap_copy (&temp
, &dest
->expressions
);
763 EXECUTE_IF_SET_IN_BITMAP (&temp
, 0, i
, bi
)
765 pre_expr expr
= expression_for_id (i
);
766 unsigned int value_id
= get_expr_value_id (expr
);
767 if (!bitmap_bit_p (&dest
->values
, value_id
))
768 bitmap_clear_bit (&dest
->expressions
, i
);
770 bitmap_clear (&temp
);
774 /* Subtract all values and expressions contained in ORIG from DEST. */
777 bitmap_set_subtract (bitmap_set_t dest
, bitmap_set_t orig
)
779 bitmap_set_t result
= bitmap_set_new ();
783 bitmap_and_compl (&result
->expressions
, &dest
->expressions
,
786 FOR_EACH_EXPR_ID_IN_SET (result
, i
, bi
)
788 pre_expr expr
= expression_for_id (i
);
789 unsigned int value_id
= get_expr_value_id (expr
);
790 bitmap_set_bit (&result
->values
, value_id
);
796 /* Subtract all the values in bitmap set B from bitmap set A. */
799 bitmap_set_subtract_values (bitmap_set_t a
, bitmap_set_t b
)
805 bitmap_initialize (&temp
, &grand_bitmap_obstack
);
807 bitmap_copy (&temp
, &a
->expressions
);
808 EXECUTE_IF_SET_IN_BITMAP (&temp
, 0, i
, bi
)
810 pre_expr expr
= expression_for_id (i
);
811 if (bitmap_set_contains_value (b
, get_expr_value_id (expr
)))
812 bitmap_remove_from_set (a
, expr
);
814 bitmap_clear (&temp
);
818 /* Return true if bitmapped set SET contains the value VALUE_ID. */
821 bitmap_set_contains_value (bitmap_set_t set
, unsigned int value_id
)
823 if (value_id_constant_p (value_id
))
826 if (!set
|| bitmap_empty_p (&set
->expressions
))
829 return bitmap_bit_p (&set
->values
, value_id
);
833 bitmap_set_contains_expr (bitmap_set_t set
, const pre_expr expr
)
835 return bitmap_bit_p (&set
->expressions
, get_expression_id (expr
));
838 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
841 bitmap_set_replace_value (bitmap_set_t set
, unsigned int lookfor
,
848 if (value_id_constant_p (lookfor
))
851 if (!bitmap_set_contains_value (set
, lookfor
))
854 /* The number of expressions having a given value is usually
855 significantly less than the total number of expressions in SET.
856 Thus, rather than check, for each expression in SET, whether it
857 has the value LOOKFOR, we walk the reverse mapping that tells us
858 what expressions have a given value, and see if any of those
859 expressions are in our set. For large testcases, this is about
860 5-10x faster than walking the bitmap. If this is somehow a
861 significant lose for some cases, we can choose which set to walk
862 based on the set size. */
863 exprset
= value_expressions
[lookfor
];
864 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
866 if (bitmap_clear_bit (&set
->expressions
, i
))
868 bitmap_set_bit (&set
->expressions
, get_expression_id (expr
));
876 /* Return true if two bitmap sets are equal. */
879 bitmap_set_equal (bitmap_set_t a
, bitmap_set_t b
)
881 return bitmap_equal_p (&a
->values
, &b
->values
);
884 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
885 and add it otherwise. */
888 bitmap_value_replace_in_set (bitmap_set_t set
, pre_expr expr
)
890 unsigned int val
= get_expr_value_id (expr
);
892 if (bitmap_set_contains_value (set
, val
))
893 bitmap_set_replace_value (set
, val
, expr
);
895 bitmap_insert_into_set (set
, expr
);
898 /* Insert EXPR into SET if EXPR's value is not already present in
902 bitmap_value_insert_into_set (bitmap_set_t set
, pre_expr expr
)
904 unsigned int val
= get_expr_value_id (expr
);
906 gcc_checking_assert (expr
->id
== get_or_alloc_expression_id (expr
));
908 /* Constant values are always considered to be part of the set. */
909 if (value_id_constant_p (val
))
912 /* If the value membership changed, add the expression. */
913 if (bitmap_set_bit (&set
->values
, val
))
914 bitmap_set_bit (&set
->expressions
, expr
->id
);
917 /* Print out EXPR to outfile. */
920 print_pre_expr (FILE *outfile
, const pre_expr expr
)
925 print_generic_expr (outfile
, PRE_EXPR_CONSTANT (expr
), 0);
928 print_generic_expr (outfile
, PRE_EXPR_NAME (expr
), 0);
933 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
934 fprintf (outfile
, "{%s,", get_tree_code_name (nary
->opcode
));
935 for (i
= 0; i
< nary
->length
; i
++)
937 print_generic_expr (outfile
, nary
->op
[i
], 0);
938 if (i
!= (unsigned) nary
->length
- 1)
939 fprintf (outfile
, ",");
941 fprintf (outfile
, "}");
947 vn_reference_op_t vro
;
949 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
950 fprintf (outfile
, "{");
952 ref
->operands
.iterate (i
, &vro
);
955 bool closebrace
= false;
956 if (vro
->opcode
!= SSA_NAME
957 && TREE_CODE_CLASS (vro
->opcode
) != tcc_declaration
)
959 fprintf (outfile
, "%s", get_tree_code_name (vro
->opcode
));
962 fprintf (outfile
, "<");
968 print_generic_expr (outfile
, vro
->op0
, 0);
971 fprintf (outfile
, ",");
972 print_generic_expr (outfile
, vro
->op1
, 0);
976 fprintf (outfile
, ",");
977 print_generic_expr (outfile
, vro
->op2
, 0);
981 fprintf (outfile
, ">");
982 if (i
!= ref
->operands
.length () - 1)
983 fprintf (outfile
, ",");
985 fprintf (outfile
, "}");
988 fprintf (outfile
, "@");
989 print_generic_expr (outfile
, ref
->vuse
, 0);
995 void debug_pre_expr (pre_expr
);
997 /* Like print_pre_expr but always prints to stderr. */
999 debug_pre_expr (pre_expr e
)
1001 print_pre_expr (stderr
, e
);
1002 fprintf (stderr
, "\n");
1005 /* Print out SET to OUTFILE. */
1008 print_bitmap_set (FILE *outfile
, bitmap_set_t set
,
1009 const char *setname
, int blockindex
)
1011 fprintf (outfile
, "%s[%d] := { ", setname
, blockindex
);
1018 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
1020 const pre_expr expr
= expression_for_id (i
);
1023 fprintf (outfile
, ", ");
1025 print_pre_expr (outfile
, expr
);
1027 fprintf (outfile
, " (%04d)", get_expr_value_id (expr
));
1030 fprintf (outfile
, " }\n");
1033 void debug_bitmap_set (bitmap_set_t
);
1036 debug_bitmap_set (bitmap_set_t set
)
1038 print_bitmap_set (stderr
, set
, "debug", 0);
1041 void debug_bitmap_sets_for (basic_block
);
1044 debug_bitmap_sets_for (basic_block bb
)
1046 print_bitmap_set (stderr
, AVAIL_OUT (bb
), "avail_out", bb
->index
);
1047 print_bitmap_set (stderr
, EXP_GEN (bb
), "exp_gen", bb
->index
);
1048 print_bitmap_set (stderr
, PHI_GEN (bb
), "phi_gen", bb
->index
);
1049 print_bitmap_set (stderr
, TMP_GEN (bb
), "tmp_gen", bb
->index
);
1050 print_bitmap_set (stderr
, ANTIC_IN (bb
), "antic_in", bb
->index
);
1051 if (do_partial_partial
)
1052 print_bitmap_set (stderr
, PA_IN (bb
), "pa_in", bb
->index
);
1053 print_bitmap_set (stderr
, NEW_SETS (bb
), "new_sets", bb
->index
);
1056 /* Print out the expressions that have VAL to OUTFILE. */
1059 print_value_expressions (FILE *outfile
, unsigned int val
)
1061 bitmap set
= value_expressions
[val
];
1066 sprintf (s
, "%04d", val
);
1067 x
.expressions
= *set
;
1068 print_bitmap_set (outfile
, &x
, s
, 0);
1074 debug_value_expressions (unsigned int val
)
1076 print_value_expressions (stderr
, val
);
1079 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1083 get_or_alloc_expr_for_constant (tree constant
)
1085 unsigned int result_id
;
1086 unsigned int value_id
;
1087 struct pre_expr_d expr
;
1090 expr
.kind
= CONSTANT
;
1091 PRE_EXPR_CONSTANT (&expr
) = constant
;
1092 result_id
= lookup_expression_id (&expr
);
1094 return expression_for_id (result_id
);
1096 newexpr
= (pre_expr
) pool_alloc (pre_expr_pool
);
1097 newexpr
->kind
= CONSTANT
;
1098 PRE_EXPR_CONSTANT (newexpr
) = constant
;
1099 alloc_expression_id (newexpr
);
1100 value_id
= get_or_alloc_constant_value_id (constant
);
1101 add_to_value (value_id
, newexpr
);
1105 /* Given a value id V, find the actual tree representing the constant
1106 value if there is one, and return it. Return NULL if we can't find
1110 get_constant_for_value_id (unsigned int v
)
1112 if (value_id_constant_p (v
))
1116 bitmap exprset
= value_expressions
[v
];
1118 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1120 pre_expr expr
= expression_for_id (i
);
1121 if (expr
->kind
== CONSTANT
)
1122 return PRE_EXPR_CONSTANT (expr
);
1128 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1129 Currently only supports constants and SSA_NAMES. */
1131 get_or_alloc_expr_for (tree t
)
1133 if (TREE_CODE (t
) == SSA_NAME
)
1134 return get_or_alloc_expr_for_name (t
);
1135 else if (is_gimple_min_invariant (t
))
1136 return get_or_alloc_expr_for_constant (t
);
1139 /* More complex expressions can result from SCCVN expression
1140 simplification that inserts values for them. As they all
1141 do not have VOPs the get handled by the nary ops struct. */
1142 vn_nary_op_t result
;
1143 unsigned int result_id
;
1144 vn_nary_op_lookup (t
, &result
);
1147 pre_expr e
= (pre_expr
) pool_alloc (pre_expr_pool
);
1149 PRE_EXPR_NARY (e
) = result
;
1150 result_id
= lookup_expression_id (e
);
1153 pool_free (pre_expr_pool
, e
);
1154 e
= expression_for_id (result_id
);
1157 alloc_expression_id (e
);
1164 /* Return the folded version of T if T, when folded, is a gimple
1165 min_invariant. Otherwise, return T. */
1168 fully_constant_expression (pre_expr e
)
1176 vn_nary_op_t nary
= PRE_EXPR_NARY (e
);
1177 switch (TREE_CODE_CLASS (nary
->opcode
))
1180 case tcc_comparison
:
1182 /* We have to go from trees to pre exprs to value ids to
1184 tree naryop0
= nary
->op
[0];
1185 tree naryop1
= nary
->op
[1];
1187 if (!is_gimple_min_invariant (naryop0
))
1189 pre_expr rep0
= get_or_alloc_expr_for (naryop0
);
1190 unsigned int vrep0
= get_expr_value_id (rep0
);
1191 tree const0
= get_constant_for_value_id (vrep0
);
1193 naryop0
= fold_convert (TREE_TYPE (naryop0
), const0
);
1195 if (!is_gimple_min_invariant (naryop1
))
1197 pre_expr rep1
= get_or_alloc_expr_for (naryop1
);
1198 unsigned int vrep1
= get_expr_value_id (rep1
);
1199 tree const1
= get_constant_for_value_id (vrep1
);
1201 naryop1
= fold_convert (TREE_TYPE (naryop1
), const1
);
1203 result
= fold_binary (nary
->opcode
, nary
->type
,
1205 if (result
&& is_gimple_min_invariant (result
))
1206 return get_or_alloc_expr_for_constant (result
);
1207 /* We might have simplified the expression to a
1208 SSA_NAME for example from x_1 * 1. But we cannot
1209 insert a PHI for x_1 unconditionally as x_1 might
1210 not be available readily. */
1214 if (nary
->opcode
!= REALPART_EXPR
1215 && nary
->opcode
!= IMAGPART_EXPR
1216 && nary
->opcode
!= VIEW_CONVERT_EXPR
)
1221 /* We have to go from trees to pre exprs to value ids to
1223 tree naryop0
= nary
->op
[0];
1224 tree const0
, result
;
1225 if (is_gimple_min_invariant (naryop0
))
1229 pre_expr rep0
= get_or_alloc_expr_for (naryop0
);
1230 unsigned int vrep0
= get_expr_value_id (rep0
);
1231 const0
= get_constant_for_value_id (vrep0
);
1236 tree type1
= TREE_TYPE (nary
->op
[0]);
1237 const0
= fold_convert (type1
, const0
);
1238 result
= fold_unary (nary
->opcode
, nary
->type
, const0
);
1240 if (result
&& is_gimple_min_invariant (result
))
1241 return get_or_alloc_expr_for_constant (result
);
1250 vn_reference_t ref
= PRE_EXPR_REFERENCE (e
);
1252 if ((folded
= fully_constant_vn_reference_p (ref
)))
1253 return get_or_alloc_expr_for_constant (folded
);
1262 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1263 it has the value it would have in BLOCK. Set *SAME_VALID to true
1264 in case the new vuse doesn't change the value id of the OPERANDS. */
1267 translate_vuse_through_block (vec
<vn_reference_op_s
> operands
,
1268 alias_set_type set
, tree type
, tree vuse
,
1269 basic_block phiblock
,
1270 basic_block block
, bool *same_valid
)
1272 gimple phi
= SSA_NAME_DEF_STMT (vuse
);
1279 if (gimple_bb (phi
) != phiblock
)
1282 use_oracle
= ao_ref_init_from_vn_reference (&ref
, set
, type
, operands
);
1284 /* Use the alias-oracle to find either the PHI node in this block,
1285 the first VUSE used in this block that is equivalent to vuse or
1286 the first VUSE which definition in this block kills the value. */
1287 if (gimple_code (phi
) == GIMPLE_PHI
)
1288 e
= find_edge (block
, phiblock
);
1289 else if (use_oracle
)
1290 while (!stmt_may_clobber_ref_p_1 (phi
, &ref
))
1292 vuse
= gimple_vuse (phi
);
1293 phi
= SSA_NAME_DEF_STMT (vuse
);
1294 if (gimple_bb (phi
) != phiblock
)
1296 if (gimple_code (phi
) == GIMPLE_PHI
)
1298 e
= find_edge (block
, phiblock
);
1309 bitmap visited
= NULL
;
1311 /* Try to find a vuse that dominates this phi node by skipping
1312 non-clobbering statements. */
1313 vuse
= get_continuation_for_phi (phi
, &ref
, &cnt
, &visited
, false,
1316 BITMAP_FREE (visited
);
1322 /* If we didn't find any, the value ID can't stay the same,
1323 but return the translated vuse. */
1324 *same_valid
= false;
1325 vuse
= PHI_ARG_DEF (phi
, e
->dest_idx
);
1327 /* ??? We would like to return vuse here as this is the canonical
1328 upmost vdef that this reference is associated with. But during
1329 insertion of the references into the hash tables we only ever
1330 directly insert with their direct gimple_vuse, hence returning
1331 something else would make us not find the other expression. */
1332 return PHI_ARG_DEF (phi
, e
->dest_idx
);
1338 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1339 SET2. This is used to avoid making a set consisting of the union
1340 of PA_IN and ANTIC_IN during insert. */
1342 static inline pre_expr
1343 find_leader_in_sets (unsigned int val
, bitmap_set_t set1
, bitmap_set_t set2
)
1347 result
= bitmap_find_leader (set1
, val
);
1348 if (!result
&& set2
)
1349 result
= bitmap_find_leader (set2
, val
);
1353 /* Get the tree type for our PRE expression e. */
1356 get_expr_type (const pre_expr e
)
1361 return TREE_TYPE (PRE_EXPR_NAME (e
));
1363 return TREE_TYPE (PRE_EXPR_CONSTANT (e
));
1365 return PRE_EXPR_REFERENCE (e
)->type
;
1367 return PRE_EXPR_NARY (e
)->type
;
1372 /* Get a representative SSA_NAME for a given expression.
1373 Since all of our sub-expressions are treated as values, we require
1374 them to be SSA_NAME's for simplicity.
1375 Prior versions of GVNPRE used to use "value handles" here, so that
1376 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1377 either case, the operands are really values (IE we do not expect
1378 them to be usable without finding leaders). */
1381 get_representative_for (const pre_expr e
)
1384 unsigned int value_id
= get_expr_value_id (e
);
1389 return PRE_EXPR_NAME (e
);
1391 return PRE_EXPR_CONSTANT (e
);
1395 /* Go through all of the expressions representing this value
1396 and pick out an SSA_NAME. */
1399 bitmap exprs
= value_expressions
[value_id
];
1400 EXECUTE_IF_SET_IN_BITMAP (exprs
, 0, i
, bi
)
1402 pre_expr rep
= expression_for_id (i
);
1403 if (rep
->kind
== NAME
)
1404 return PRE_EXPR_NAME (rep
);
1405 else if (rep
->kind
== CONSTANT
)
1406 return PRE_EXPR_CONSTANT (rep
);
1412 /* If we reached here we couldn't find an SSA_NAME. This can
1413 happen when we've discovered a value that has never appeared in
1414 the program as set to an SSA_NAME, as the result of phi translation.
1416 ??? We should be able to re-use this when we insert the statement
1418 name
= make_temp_ssa_name (get_expr_type (e
), gimple_build_nop (), "pretmp");
1419 VN_INFO_GET (name
)->value_id
= value_id
;
1420 VN_INFO (name
)->valnum
= name
;
1421 /* ??? For now mark this SSA name for release by SCCVN. */
1422 VN_INFO (name
)->needs_insertion
= true;
1423 add_to_value (value_id
, get_or_alloc_expr_for_name (name
));
1424 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1426 fprintf (dump_file
, "Created SSA_NAME representative ");
1427 print_generic_expr (dump_file
, name
, 0);
1428 fprintf (dump_file
, " for expression:");
1429 print_pre_expr (dump_file
, e
);
1430 fprintf (dump_file
, " (%04d)\n", value_id
);
1439 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1440 basic_block pred
, basic_block phiblock
);
1442 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1443 the phis in PRED. Return NULL if we can't find a leader for each part
1444 of the translated expression. */
1447 phi_translate_1 (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1448 basic_block pred
, basic_block phiblock
)
1455 bool changed
= false;
1456 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1457 vn_nary_op_t newnary
= XALLOCAVAR (struct vn_nary_op_s
,
1458 sizeof_vn_nary_op (nary
->length
));
1459 memcpy (newnary
, nary
, sizeof_vn_nary_op (nary
->length
));
1461 for (i
= 0; i
< newnary
->length
; i
++)
1463 if (TREE_CODE (newnary
->op
[i
]) != SSA_NAME
)
1467 pre_expr leader
, result
;
1468 unsigned int op_val_id
= VN_INFO (newnary
->op
[i
])->value_id
;
1469 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1470 result
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1471 if (result
&& result
!= leader
)
1473 tree name
= get_representative_for (result
);
1476 newnary
->op
[i
] = name
;
1481 changed
|= newnary
->op
[i
] != nary
->op
[i
];
1487 unsigned int new_val_id
;
1489 tree result
= vn_nary_op_lookup_pieces (newnary
->length
,
1494 if (result
&& is_gimple_min_invariant (result
))
1495 return get_or_alloc_expr_for_constant (result
);
1497 expr
= (pre_expr
) pool_alloc (pre_expr_pool
);
1502 PRE_EXPR_NARY (expr
) = nary
;
1503 constant
= fully_constant_expression (expr
);
1504 if (constant
!= expr
)
1507 new_val_id
= nary
->value_id
;
1508 get_or_alloc_expression_id (expr
);
1512 new_val_id
= get_next_value_id ();
1513 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
1514 nary
= vn_nary_op_insert_pieces (newnary
->length
,
1518 result
, new_val_id
);
1519 PRE_EXPR_NARY (expr
) = nary
;
1520 constant
= fully_constant_expression (expr
);
1521 if (constant
!= expr
)
1523 get_or_alloc_expression_id (expr
);
1525 add_to_value (new_val_id
, expr
);
1533 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1534 vec
<vn_reference_op_s
> operands
= ref
->operands
;
1535 tree vuse
= ref
->vuse
;
1536 tree newvuse
= vuse
;
1537 vec
<vn_reference_op_s
> newoperands
= vNULL
;
1538 bool changed
= false, same_valid
= true;
1540 vn_reference_op_t operand
;
1541 vn_reference_t newref
;
1543 for (i
= 0; operands
.iterate (i
, &operand
); i
++)
1548 tree type
= operand
->type
;
1549 vn_reference_op_s newop
= *operand
;
1550 op
[0] = operand
->op0
;
1551 op
[1] = operand
->op1
;
1552 op
[2] = operand
->op2
;
1553 for (n
= 0; n
< 3; ++n
)
1555 unsigned int op_val_id
;
1558 if (TREE_CODE (op
[n
]) != SSA_NAME
)
1560 /* We can't possibly insert these. */
1562 && !is_gimple_min_invariant (op
[n
]))
1566 op_val_id
= VN_INFO (op
[n
])->value_id
;
1567 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1570 opresult
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1573 if (opresult
!= leader
)
1575 tree name
= get_representative_for (opresult
);
1578 changed
|= name
!= op
[n
];
1584 newoperands
.release ();
1589 if (!newoperands
.exists ())
1590 newoperands
= operands
.copy ();
1591 /* We may have changed from an SSA_NAME to a constant */
1592 if (newop
.opcode
== SSA_NAME
&& TREE_CODE (op
[0]) != SSA_NAME
)
1593 newop
.opcode
= TREE_CODE (op
[0]);
1598 newoperands
[i
] = newop
;
1600 gcc_checking_assert (i
== operands
.length ());
1604 newvuse
= translate_vuse_through_block (newoperands
.exists ()
1605 ? newoperands
: operands
,
1606 ref
->set
, ref
->type
,
1607 vuse
, phiblock
, pred
,
1609 if (newvuse
== NULL_TREE
)
1611 newoperands
.release ();
1616 if (changed
|| newvuse
!= vuse
)
1618 unsigned int new_val_id
;
1621 tree result
= vn_reference_lookup_pieces (newvuse
, ref
->set
,
1623 newoperands
.exists ()
1624 ? newoperands
: operands
,
1627 newoperands
.release ();
1629 /* We can always insert constants, so if we have a partial
1630 redundant constant load of another type try to translate it
1631 to a constant of appropriate type. */
1632 if (result
&& is_gimple_min_invariant (result
))
1635 if (!useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1637 tem
= fold_unary (VIEW_CONVERT_EXPR
, ref
->type
, result
);
1638 if (tem
&& !is_gimple_min_invariant (tem
))
1642 return get_or_alloc_expr_for_constant (tem
);
1645 /* If we'd have to convert things we would need to validate
1646 if we can insert the translated expression. So fail
1647 here for now - we cannot insert an alias with a different
1648 type in the VN tables either, as that would assert. */
1650 && !useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1652 else if (!result
&& newref
1653 && !useless_type_conversion_p (ref
->type
, newref
->type
))
1655 newoperands
.release ();
1659 expr
= (pre_expr
) pool_alloc (pre_expr_pool
);
1660 expr
->kind
= REFERENCE
;
1665 PRE_EXPR_REFERENCE (expr
) = newref
;
1666 constant
= fully_constant_expression (expr
);
1667 if (constant
!= expr
)
1670 new_val_id
= newref
->value_id
;
1671 get_or_alloc_expression_id (expr
);
1675 if (changed
|| !same_valid
)
1677 new_val_id
= get_next_value_id ();
1678 value_expressions
.safe_grow_cleared
1679 (get_max_value_id () + 1);
1682 new_val_id
= ref
->value_id
;
1683 if (!newoperands
.exists ())
1684 newoperands
= operands
.copy ();
1685 newref
= vn_reference_insert_pieces (newvuse
, ref
->set
,
1688 result
, new_val_id
);
1689 newoperands
= vNULL
;
1690 PRE_EXPR_REFERENCE (expr
) = newref
;
1691 constant
= fully_constant_expression (expr
);
1692 if (constant
!= expr
)
1694 get_or_alloc_expression_id (expr
);
1696 add_to_value (new_val_id
, expr
);
1698 newoperands
.release ();
1705 tree name
= PRE_EXPR_NAME (expr
);
1706 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
1707 /* If the SSA name is defined by a PHI node in this block,
1709 if (gimple_code (def_stmt
) == GIMPLE_PHI
1710 && gimple_bb (def_stmt
) == phiblock
)
1712 edge e
= find_edge (pred
, gimple_bb (def_stmt
));
1713 tree def
= PHI_ARG_DEF (def_stmt
, e
->dest_idx
);
1715 /* Handle constant. */
1716 if (is_gimple_min_invariant (def
))
1717 return get_or_alloc_expr_for_constant (def
);
1719 return get_or_alloc_expr_for_name (def
);
1721 /* Otherwise return it unchanged - it will get removed if its
1722 value is not available in PREDs AVAIL_OUT set of expressions
1723 by the subtraction of TMP_GEN. */
1732 /* Wrapper around phi_translate_1 providing caching functionality. */
1735 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1736 basic_block pred
, basic_block phiblock
)
1738 expr_pred_trans_t slot
= NULL
;
1744 /* Constants contain no values that need translation. */
1745 if (expr
->kind
== CONSTANT
)
1748 if (value_id_constant_p (get_expr_value_id (expr
)))
1751 /* Don't add translations of NAMEs as those are cheap to translate. */
1752 if (expr
->kind
!= NAME
)
1754 if (phi_trans_add (&slot
, expr
, pred
))
1756 /* Store NULL for the value we want to return in the case of
1762 phitrans
= phi_translate_1 (expr
, set1
, set2
, pred
, phiblock
);
1769 /* Remove failed translations again, they cause insert
1770 iteration to not pick up new opportunities reliably. */
1771 phi_translate_table
->remove_elt_with_hash (slot
, slot
->hashcode
);
1778 /* For each expression in SET, translate the values through phi nodes
1779 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1780 expressions in DEST. */
1783 phi_translate_set (bitmap_set_t dest
, bitmap_set_t set
, basic_block pred
,
1784 basic_block phiblock
)
1786 vec
<pre_expr
> exprs
;
1790 if (gimple_seq_empty_p (phi_nodes (phiblock
)))
1792 bitmap_set_copy (dest
, set
);
1796 exprs
= sorted_array_from_bitmap_set (set
);
1797 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
1799 pre_expr translated
;
1800 translated
= phi_translate (expr
, set
, NULL
, pred
, phiblock
);
1804 /* We might end up with multiple expressions from SET being
1805 translated to the same value. In this case we do not want
1806 to retain the NARY or REFERENCE expression but prefer a NAME
1807 which would be the leader. */
1808 if (translated
->kind
== NAME
)
1809 bitmap_value_replace_in_set (dest
, translated
);
1811 bitmap_value_insert_into_set (dest
, translated
);
1816 /* Find the leader for a value (i.e., the name representing that
1817 value) in a given set, and return it. Return NULL if no leader
1821 bitmap_find_leader (bitmap_set_t set
, unsigned int val
)
1823 if (value_id_constant_p (val
))
1827 bitmap exprset
= value_expressions
[val
];
1829 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1831 pre_expr expr
= expression_for_id (i
);
1832 if (expr
->kind
== CONSTANT
)
1836 if (bitmap_set_contains_value (set
, val
))
1838 /* Rather than walk the entire bitmap of expressions, and see
1839 whether any of them has the value we are looking for, we look
1840 at the reverse mapping, which tells us the set of expressions
1841 that have a given value (IE value->expressions with that
1842 value) and see if any of those expressions are in our set.
1843 The number of expressions per value is usually significantly
1844 less than the number of expressions in the set. In fact, for
1845 large testcases, doing it this way is roughly 5-10x faster
1846 than walking the bitmap.
1847 If this is somehow a significant lose for some cases, we can
1848 choose which set to walk based on which set is smaller. */
1851 bitmap exprset
= value_expressions
[val
];
1853 EXECUTE_IF_AND_IN_BITMAP (exprset
, &set
->expressions
, 0, i
, bi
)
1854 return expression_for_id (i
);
1859 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1860 BLOCK by seeing if it is not killed in the block. Note that we are
1861 only determining whether there is a store that kills it. Because
1862 of the order in which clean iterates over values, we are guaranteed
1863 that altered operands will have caused us to be eliminated from the
1864 ANTIC_IN set already. */
1867 value_dies_in_block_x (pre_expr expr
, basic_block block
)
1869 tree vuse
= PRE_EXPR_REFERENCE (expr
)->vuse
;
1870 vn_reference_t refx
= PRE_EXPR_REFERENCE (expr
);
1872 gimple_stmt_iterator gsi
;
1873 unsigned id
= get_expression_id (expr
);
1880 /* Lookup a previously calculated result. */
1881 if (EXPR_DIES (block
)
1882 && bitmap_bit_p (EXPR_DIES (block
), id
* 2))
1883 return bitmap_bit_p (EXPR_DIES (block
), id
* 2 + 1);
1885 /* A memory expression {e, VUSE} dies in the block if there is a
1886 statement that may clobber e. If, starting statement walk from the
1887 top of the basic block, a statement uses VUSE there can be no kill
1888 inbetween that use and the original statement that loaded {e, VUSE},
1889 so we can stop walking. */
1890 ref
.base
= NULL_TREE
;
1891 for (gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1893 tree def_vuse
, def_vdef
;
1894 def
= gsi_stmt (gsi
);
1895 def_vuse
= gimple_vuse (def
);
1896 def_vdef
= gimple_vdef (def
);
1898 /* Not a memory statement. */
1902 /* Not a may-def. */
1905 /* A load with the same VUSE, we're done. */
1906 if (def_vuse
== vuse
)
1912 /* Init ref only if we really need it. */
1913 if (ref
.base
== NULL_TREE
1914 && !ao_ref_init_from_vn_reference (&ref
, refx
->set
, refx
->type
,
1920 /* If the statement may clobber expr, it dies. */
1921 if (stmt_may_clobber_ref_p_1 (def
, &ref
))
1928 /* Remember the result. */
1929 if (!EXPR_DIES (block
))
1930 EXPR_DIES (block
) = BITMAP_ALLOC (&grand_bitmap_obstack
);
1931 bitmap_set_bit (EXPR_DIES (block
), id
* 2);
1933 bitmap_set_bit (EXPR_DIES (block
), id
* 2 + 1);
1939 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1940 contains its value-id. */
1943 op_valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, tree op
)
1945 if (op
&& TREE_CODE (op
) == SSA_NAME
)
1947 unsigned int value_id
= VN_INFO (op
)->value_id
;
1948 if (!(bitmap_set_contains_value (set1
, value_id
)
1949 || (set2
&& bitmap_set_contains_value (set2
, value_id
))))
1955 /* Determine if the expression EXPR is valid in SET1 U SET2.
1956 ONLY SET2 CAN BE NULL.
1957 This means that we have a leader for each part of the expression
1958 (if it consists of values), or the expression is an SSA_NAME.
1959 For loads/calls, we also see if the vuse is killed in this block. */
1962 valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, pre_expr expr
)
1967 /* By construction all NAMEs are available. Non-available
1968 NAMEs are removed by subtracting TMP_GEN from the sets. */
1973 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1974 for (i
= 0; i
< nary
->length
; i
++)
1975 if (!op_valid_in_sets (set1
, set2
, nary
->op
[i
]))
1982 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1983 vn_reference_op_t vro
;
1986 FOR_EACH_VEC_ELT (ref
->operands
, i
, vro
)
1988 if (!op_valid_in_sets (set1
, set2
, vro
->op0
)
1989 || !op_valid_in_sets (set1
, set2
, vro
->op1
)
1990 || !op_valid_in_sets (set1
, set2
, vro
->op2
))
2000 /* Clean the set of expressions that are no longer valid in SET1 or
2001 SET2. This means expressions that are made up of values we have no
2002 leaders for in SET1 or SET2. This version is used for partial
2003 anticipation, which means it is not valid in either ANTIC_IN or
2007 dependent_clean (bitmap_set_t set1
, bitmap_set_t set2
)
2009 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set1
);
2013 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2015 if (!valid_in_sets (set1
, set2
, expr
))
2016 bitmap_remove_from_set (set1
, expr
);
2021 /* Clean the set of expressions that are no longer valid in SET. This
2022 means expressions that are made up of values we have no leaders for
2026 clean (bitmap_set_t set
)
2028 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set
);
2032 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2034 if (!valid_in_sets (set
, NULL
, expr
))
2035 bitmap_remove_from_set (set
, expr
);
2040 /* Clean the set of expressions that are no longer valid in SET because
2041 they are clobbered in BLOCK or because they trap and may not be executed. */
2044 prune_clobbered_mems (bitmap_set_t set
, basic_block block
)
2049 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
2051 pre_expr expr
= expression_for_id (i
);
2052 if (expr
->kind
== REFERENCE
)
2054 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2057 gimple def_stmt
= SSA_NAME_DEF_STMT (ref
->vuse
);
2058 if (!gimple_nop_p (def_stmt
)
2059 && ((gimple_bb (def_stmt
) != block
2060 && !dominated_by_p (CDI_DOMINATORS
,
2061 block
, gimple_bb (def_stmt
)))
2062 || (gimple_bb (def_stmt
) == block
2063 && value_dies_in_block_x (expr
, block
))))
2064 bitmap_remove_from_set (set
, expr
);
2067 else if (expr
->kind
== NARY
)
2069 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2070 /* If the NARY may trap make sure the block does not contain
2071 a possible exit point.
2072 ??? This is overly conservative if we translate AVAIL_OUT
2073 as the available expression might be after the exit point. */
2074 if (BB_MAY_NOTRETURN (block
)
2075 && vn_nary_may_trap (nary
))
2076 bitmap_remove_from_set (set
, expr
);
2081 static sbitmap has_abnormal_preds
;
2083 /* List of blocks that may have changed during ANTIC computation and
2084 thus need to be iterated over. */
2086 static sbitmap changed_blocks
;
2088 /* Decide whether to defer a block for a later iteration, or PHI
2089 translate SOURCE to DEST using phis in PHIBLOCK. Return false if we
2090 should defer the block, and true if we processed it. */
2093 defer_or_phi_translate_block (bitmap_set_t dest
, bitmap_set_t source
,
2094 basic_block block
, basic_block phiblock
)
2096 if (!BB_VISITED (phiblock
))
2098 bitmap_set_bit (changed_blocks
, block
->index
);
2099 BB_VISITED (block
) = 0;
2100 BB_DEFERRED (block
) = 1;
2104 phi_translate_set (dest
, source
, block
, phiblock
);
2108 /* Compute the ANTIC set for BLOCK.
2110 If succs(BLOCK) > 1 then
2111 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2112 else if succs(BLOCK) == 1 then
2113 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2115 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2119 compute_antic_aux (basic_block block
, bool block_has_abnormal_pred_edge
)
2121 bool changed
= false;
2122 bitmap_set_t S
, old
, ANTIC_OUT
;
2128 old
= ANTIC_OUT
= S
= NULL
;
2129 BB_VISITED (block
) = 1;
2131 /* If any edges from predecessors are abnormal, antic_in is empty,
2133 if (block_has_abnormal_pred_edge
)
2134 goto maybe_dump_sets
;
2136 old
= ANTIC_IN (block
);
2137 ANTIC_OUT
= bitmap_set_new ();
2139 /* If the block has no successors, ANTIC_OUT is empty. */
2140 if (EDGE_COUNT (block
->succs
) == 0)
2142 /* If we have one successor, we could have some phi nodes to
2143 translate through. */
2144 else if (single_succ_p (block
))
2146 basic_block succ_bb
= single_succ (block
);
2148 /* We trade iterations of the dataflow equations for having to
2149 phi translate the maximal set, which is incredibly slow
2150 (since the maximal set often has 300+ members, even when you
2151 have a small number of blocks).
2152 Basically, we defer the computation of ANTIC for this block
2153 until we have processed it's successor, which will inevitably
2154 have a *much* smaller set of values to phi translate once
2155 clean has been run on it.
2156 The cost of doing this is that we technically perform more
2157 iterations, however, they are lower cost iterations.
2159 Timings for PRE on tramp3d-v4:
2160 without maximal set fix: 11 seconds
2161 with maximal set fix/without deferring: 26 seconds
2162 with maximal set fix/with deferring: 11 seconds
2165 if (!defer_or_phi_translate_block (ANTIC_OUT
, ANTIC_IN (succ_bb
),
2169 goto maybe_dump_sets
;
2172 /* If we have multiple successors, we take the intersection of all of
2173 them. Note that in the case of loop exit phi nodes, we may have
2174 phis to translate through. */
2178 basic_block bprime
, first
= NULL
;
2180 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2181 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2184 && BB_VISITED (e
->dest
))
2186 else if (BB_VISITED (e
->dest
))
2187 worklist
.quick_push (e
->dest
);
2190 /* Of multiple successors we have to have visited one already. */
2193 bitmap_set_bit (changed_blocks
, block
->index
);
2194 BB_VISITED (block
) = 0;
2195 BB_DEFERRED (block
) = 1;
2197 goto maybe_dump_sets
;
2200 if (!gimple_seq_empty_p (phi_nodes (first
)))
2201 phi_translate_set (ANTIC_OUT
, ANTIC_IN (first
), block
, first
);
2203 bitmap_set_copy (ANTIC_OUT
, ANTIC_IN (first
));
2205 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2207 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2209 bitmap_set_t tmp
= bitmap_set_new ();
2210 phi_translate_set (tmp
, ANTIC_IN (bprime
), block
, bprime
);
2211 bitmap_set_and (ANTIC_OUT
, tmp
);
2212 bitmap_set_free (tmp
);
2215 bitmap_set_and (ANTIC_OUT
, ANTIC_IN (bprime
));
2219 /* Prune expressions that are clobbered in block and thus become
2220 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2221 prune_clobbered_mems (ANTIC_OUT
, block
);
2223 /* Generate ANTIC_OUT - TMP_GEN. */
2224 S
= bitmap_set_subtract (ANTIC_OUT
, TMP_GEN (block
));
2226 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2227 ANTIC_IN (block
) = bitmap_set_subtract (EXP_GEN (block
),
2230 /* Then union in the ANTIC_OUT - TMP_GEN values,
2231 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2232 FOR_EACH_EXPR_ID_IN_SET (S
, bii
, bi
)
2233 bitmap_value_insert_into_set (ANTIC_IN (block
),
2234 expression_for_id (bii
));
2236 clean (ANTIC_IN (block
));
2238 if (!bitmap_set_equal (old
, ANTIC_IN (block
)))
2241 bitmap_set_bit (changed_blocks
, block
->index
);
2242 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2243 bitmap_set_bit (changed_blocks
, e
->src
->index
);
2246 bitmap_clear_bit (changed_blocks
, block
->index
);
2249 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2251 if (!BB_DEFERRED (block
) || BB_VISITED (block
))
2254 print_bitmap_set (dump_file
, ANTIC_OUT
, "ANTIC_OUT", block
->index
);
2256 print_bitmap_set (dump_file
, ANTIC_IN (block
), "ANTIC_IN",
2260 print_bitmap_set (dump_file
, S
, "S", block
->index
);
2265 "Block %d was deferred for a future iteration.\n",
2270 bitmap_set_free (old
);
2272 bitmap_set_free (S
);
2274 bitmap_set_free (ANTIC_OUT
);
2278 /* Compute PARTIAL_ANTIC for BLOCK.
2280 If succs(BLOCK) > 1 then
2281 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2282 in ANTIC_OUT for all succ(BLOCK)
2283 else if succs(BLOCK) == 1 then
2284 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2286 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2291 compute_partial_antic_aux (basic_block block
,
2292 bool block_has_abnormal_pred_edge
)
2294 bool changed
= false;
2295 bitmap_set_t old_PA_IN
;
2296 bitmap_set_t PA_OUT
;
2299 unsigned long max_pa
= PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH
);
2301 old_PA_IN
= PA_OUT
= NULL
;
2303 /* If any edges from predecessors are abnormal, antic_in is empty,
2305 if (block_has_abnormal_pred_edge
)
2306 goto maybe_dump_sets
;
2308 /* If there are too many partially anticipatable values in the
2309 block, phi_translate_set can take an exponential time: stop
2310 before the translation starts. */
2312 && single_succ_p (block
)
2313 && bitmap_count_bits (&PA_IN (single_succ (block
))->values
) > max_pa
)
2314 goto maybe_dump_sets
;
2316 old_PA_IN
= PA_IN (block
);
2317 PA_OUT
= bitmap_set_new ();
2319 /* If the block has no successors, ANTIC_OUT is empty. */
2320 if (EDGE_COUNT (block
->succs
) == 0)
2322 /* If we have one successor, we could have some phi nodes to
2323 translate through. Note that we can't phi translate across DFS
2324 back edges in partial antic, because it uses a union operation on
2325 the successors. For recurrences like IV's, we will end up
2326 generating a new value in the set on each go around (i + 3 (VH.1)
2327 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2328 else if (single_succ_p (block
))
2330 basic_block succ
= single_succ (block
);
2331 if (!(single_succ_edge (block
)->flags
& EDGE_DFS_BACK
))
2332 phi_translate_set (PA_OUT
, PA_IN (succ
), block
, succ
);
2334 /* If we have multiple successors, we take the union of all of
2341 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2342 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2344 if (e
->flags
& EDGE_DFS_BACK
)
2346 worklist
.quick_push (e
->dest
);
2348 if (worklist
.length () > 0)
2350 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2355 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime
), i
, bi
)
2356 bitmap_value_insert_into_set (PA_OUT
,
2357 expression_for_id (i
));
2358 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2360 bitmap_set_t pa_in
= bitmap_set_new ();
2361 phi_translate_set (pa_in
, PA_IN (bprime
), block
, bprime
);
2362 FOR_EACH_EXPR_ID_IN_SET (pa_in
, i
, bi
)
2363 bitmap_value_insert_into_set (PA_OUT
,
2364 expression_for_id (i
));
2365 bitmap_set_free (pa_in
);
2368 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime
), i
, bi
)
2369 bitmap_value_insert_into_set (PA_OUT
,
2370 expression_for_id (i
));
2375 /* Prune expressions that are clobbered in block and thus become
2376 invalid if translated from PA_OUT to PA_IN. */
2377 prune_clobbered_mems (PA_OUT
, block
);
2379 /* PA_IN starts with PA_OUT - TMP_GEN.
2380 Then we subtract things from ANTIC_IN. */
2381 PA_IN (block
) = bitmap_set_subtract (PA_OUT
, TMP_GEN (block
));
2383 /* For partial antic, we want to put back in the phi results, since
2384 we will properly avoid making them partially antic over backedges. */
2385 bitmap_ior_into (&PA_IN (block
)->values
, &PHI_GEN (block
)->values
);
2386 bitmap_ior_into (&PA_IN (block
)->expressions
, &PHI_GEN (block
)->expressions
);
2388 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2389 bitmap_set_subtract_values (PA_IN (block
), ANTIC_IN (block
));
2391 dependent_clean (PA_IN (block
), ANTIC_IN (block
));
2393 if (!bitmap_set_equal (old_PA_IN
, PA_IN (block
)))
2396 bitmap_set_bit (changed_blocks
, block
->index
);
2397 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2398 bitmap_set_bit (changed_blocks
, e
->src
->index
);
2401 bitmap_clear_bit (changed_blocks
, block
->index
);
2404 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2407 print_bitmap_set (dump_file
, PA_OUT
, "PA_OUT", block
->index
);
2409 print_bitmap_set (dump_file
, PA_IN (block
), "PA_IN", block
->index
);
2412 bitmap_set_free (old_PA_IN
);
2414 bitmap_set_free (PA_OUT
);
2418 /* Compute ANTIC and partial ANTIC sets. */
2421 compute_antic (void)
2423 bool changed
= true;
2424 int num_iterations
= 0;
2428 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2429 We pre-build the map of blocks with incoming abnormal edges here. */
2430 has_abnormal_preds
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
2431 bitmap_clear (has_abnormal_preds
);
2433 FOR_ALL_BB_FN (block
, cfun
)
2438 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2440 e
->flags
&= ~EDGE_DFS_BACK
;
2441 if (e
->flags
& EDGE_ABNORMAL
)
2443 bitmap_set_bit (has_abnormal_preds
, block
->index
);
2448 BB_VISITED (block
) = 0;
2449 BB_DEFERRED (block
) = 0;
2451 /* While we are here, give empty ANTIC_IN sets to each block. */
2452 ANTIC_IN (block
) = bitmap_set_new ();
2453 PA_IN (block
) = bitmap_set_new ();
2456 /* At the exit block we anticipate nothing. */
2457 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun
)) = 1;
2459 changed_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
) + 1);
2460 bitmap_ones (changed_blocks
);
2463 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2464 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2465 /* ??? We need to clear our PHI translation cache here as the
2466 ANTIC sets shrink and we restrict valid translations to
2467 those having operands with leaders in ANTIC. Same below
2468 for PA ANTIC computation. */
2471 for (i
= postorder_num
- 1; i
>= 0; i
--)
2473 if (bitmap_bit_p (changed_blocks
, postorder
[i
]))
2475 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2476 changed
|= compute_antic_aux (block
,
2477 bitmap_bit_p (has_abnormal_preds
,
2481 /* Theoretically possible, but *highly* unlikely. */
2482 gcc_checking_assert (num_iterations
< 500);
2485 statistics_histogram_event (cfun
, "compute_antic iterations",
2488 if (do_partial_partial
)
2490 bitmap_ones (changed_blocks
);
2491 mark_dfs_back_edges ();
2496 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2497 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2500 for (i
= postorder_num
- 1 ; i
>= 0; i
--)
2502 if (bitmap_bit_p (changed_blocks
, postorder
[i
]))
2504 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2506 |= compute_partial_antic_aux (block
,
2507 bitmap_bit_p (has_abnormal_preds
,
2511 /* Theoretically possible, but *highly* unlikely. */
2512 gcc_checking_assert (num_iterations
< 500);
2514 statistics_histogram_event (cfun
, "compute_partial_antic iterations",
2517 sbitmap_free (has_abnormal_preds
);
2518 sbitmap_free (changed_blocks
);
2522 /* Inserted expressions are placed onto this worklist, which is used
2523 for performing quick dead code elimination of insertions we made
2524 that didn't turn out to be necessary. */
2525 static bitmap inserted_exprs
;
2527 /* The actual worker for create_component_ref_by_pieces. */
2530 create_component_ref_by_pieces_1 (basic_block block
, vn_reference_t ref
,
2531 unsigned int *operand
, gimple_seq
*stmts
)
2533 vn_reference_op_t currop
= &ref
->operands
[*operand
];
2536 switch (currop
->opcode
)
2540 tree folded
, sc
= NULL_TREE
;
2541 unsigned int nargs
= 0;
2543 if (TREE_CODE (currop
->op0
) == FUNCTION_DECL
)
2546 fn
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2551 sc
= find_or_generate_expression (block
, currop
->op1
, stmts
);
2555 args
= XNEWVEC (tree
, ref
->operands
.length () - 1);
2556 while (*operand
< ref
->operands
.length ())
2558 args
[nargs
] = create_component_ref_by_pieces_1 (block
, ref
,
2564 folded
= build_call_array (currop
->type
,
2565 (TREE_CODE (fn
) == FUNCTION_DECL
2566 ? build_fold_addr_expr (fn
) : fn
),
2570 CALL_EXPR_STATIC_CHAIN (folded
) = sc
;
2576 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2580 tree offset
= currop
->op0
;
2581 if (TREE_CODE (baseop
) == ADDR_EXPR
2582 && handled_component_p (TREE_OPERAND (baseop
, 0)))
2586 base
= get_addr_base_and_unit_offset (TREE_OPERAND (baseop
, 0),
2589 offset
= int_const_binop (PLUS_EXPR
, offset
,
2590 build_int_cst (TREE_TYPE (offset
),
2592 baseop
= build_fold_addr_expr (base
);
2594 return fold_build2 (MEM_REF
, currop
->type
, baseop
, offset
);
2597 case TARGET_MEM_REF
:
2599 tree genop0
= NULL_TREE
, genop1
= NULL_TREE
;
2600 vn_reference_op_t nextop
= &ref
->operands
[++*operand
];
2601 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2607 genop0
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2613 genop1
= find_or_generate_expression (block
, nextop
->op0
, stmts
);
2617 return build5 (TARGET_MEM_REF
, currop
->type
,
2618 baseop
, currop
->op2
, genop0
, currop
->op1
, genop1
);
2624 gcc_assert (is_gimple_min_invariant (currop
->op0
));
2630 case VIEW_CONVERT_EXPR
:
2632 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2636 return fold_build1 (currop
->opcode
, currop
->type
, genop0
);
2639 case WITH_SIZE_EXPR
:
2641 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2645 tree genop1
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2648 return fold_build2 (currop
->opcode
, currop
->type
, genop0
, genop1
);
2653 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2657 tree op1
= currop
->op0
;
2658 tree op2
= currop
->op1
;
2659 return fold_build3 (BIT_FIELD_REF
, currop
->type
, genop0
, op1
, op2
);
2662 /* For array ref vn_reference_op's, operand 1 of the array ref
2663 is op0 of the reference op and operand 3 of the array ref is
2665 case ARRAY_RANGE_REF
:
2669 tree genop1
= currop
->op0
;
2670 tree genop2
= currop
->op1
;
2671 tree genop3
= currop
->op2
;
2672 genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2676 genop1
= find_or_generate_expression (block
, genop1
, stmts
);
2681 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (genop0
));
2682 /* Drop zero minimum index if redundant. */
2683 if (integer_zerop (genop2
)
2685 || integer_zerop (TYPE_MIN_VALUE (domain_type
))))
2689 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2696 tree elmt_type
= TREE_TYPE (TREE_TYPE (genop0
));
2697 /* We can't always put a size in units of the element alignment
2698 here as the element alignment may be not visible. See
2699 PR43783. Simply drop the element size for constant
2701 if (tree_int_cst_equal (genop3
, TYPE_SIZE_UNIT (elmt_type
)))
2705 genop3
= size_binop (EXACT_DIV_EXPR
, genop3
,
2706 size_int (TYPE_ALIGN_UNIT (elmt_type
)));
2707 genop3
= find_or_generate_expression (block
, genop3
, stmts
);
2712 return build4 (currop
->opcode
, currop
->type
, genop0
, genop1
,
2719 tree genop2
= currop
->op1
;
2720 op0
= create_component_ref_by_pieces_1 (block
, ref
, operand
, stmts
);
2723 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2727 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2731 return fold_build3 (COMPONENT_REF
, TREE_TYPE (op1
), op0
, op1
, genop2
);
2736 genop
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2757 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2758 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2759 trying to rename aggregates into ssa form directly, which is a no no.
2761 Thus, this routine doesn't create temporaries, it just builds a
2762 single access expression for the array, calling
2763 find_or_generate_expression to build the innermost pieces.
2765 This function is a subroutine of create_expression_by_pieces, and
2766 should not be called on it's own unless you really know what you
2770 create_component_ref_by_pieces (basic_block block
, vn_reference_t ref
,
2773 unsigned int op
= 0;
2774 return create_component_ref_by_pieces_1 (block
, ref
, &op
, stmts
);
2777 /* Find a simple leader for an expression, or generate one using
2778 create_expression_by_pieces from a NARY expression for the value.
2779 BLOCK is the basic_block we are looking for leaders in.
2780 OP is the tree expression to find a leader for or generate.
2781 Returns the leader or NULL_TREE on failure. */
2784 find_or_generate_expression (basic_block block
, tree op
, gimple_seq
*stmts
)
2786 pre_expr expr
= get_or_alloc_expr_for (op
);
2787 unsigned int lookfor
= get_expr_value_id (expr
);
2788 pre_expr leader
= bitmap_find_leader (AVAIL_OUT (block
), lookfor
);
2791 if (leader
->kind
== NAME
)
2792 return PRE_EXPR_NAME (leader
);
2793 else if (leader
->kind
== CONSTANT
)
2794 return PRE_EXPR_CONSTANT (leader
);
2800 /* It must be a complex expression, so generate it recursively. Note
2801 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2802 where the insert algorithm fails to insert a required expression. */
2803 bitmap exprset
= value_expressions
[lookfor
];
2806 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
2808 pre_expr temp
= expression_for_id (i
);
2809 /* We cannot insert random REFERENCE expressions at arbitrary
2810 places. We can insert NARYs which eventually re-materializes
2811 its operand values. */
2812 if (temp
->kind
== NARY
)
2813 return create_expression_by_pieces (block
, temp
, stmts
,
2814 get_expr_type (expr
));
2821 #define NECESSARY GF_PLF_1
2823 /* Create an expression in pieces, so that we can handle very complex
2824 expressions that may be ANTIC, but not necessary GIMPLE.
2825 BLOCK is the basic block the expression will be inserted into,
2826 EXPR is the expression to insert (in value form)
2827 STMTS is a statement list to append the necessary insertions into.
2829 This function will die if we hit some value that shouldn't be
2830 ANTIC but is (IE there is no leader for it, or its components).
2831 The function returns NULL_TREE in case a different antic expression
2832 has to be inserted first.
2833 This function may also generate expressions that are themselves
2834 partially or fully redundant. Those that are will be either made
2835 fully redundant during the next iteration of insert (for partially
2836 redundant ones), or eliminated by eliminate (for fully redundant
2840 create_expression_by_pieces (basic_block block
, pre_expr expr
,
2841 gimple_seq
*stmts
, tree type
)
2845 gimple_seq forced_stmts
= NULL
;
2846 unsigned int value_id
;
2847 gimple_stmt_iterator gsi
;
2848 tree exprtype
= type
? type
: get_expr_type (expr
);
2854 /* We may hit the NAME/CONSTANT case if we have to convert types
2855 that value numbering saw through. */
2857 folded
= PRE_EXPR_NAME (expr
);
2860 folded
= PRE_EXPR_CONSTANT (expr
);
2864 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2865 folded
= create_component_ref_by_pieces (block
, ref
, stmts
);
2872 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2873 tree
*genop
= XALLOCAVEC (tree
, nary
->length
);
2875 for (i
= 0; i
< nary
->length
; ++i
)
2877 genop
[i
] = find_or_generate_expression (block
, nary
->op
[i
], stmts
);
2880 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2881 may have conversions stripped. */
2882 if (nary
->opcode
== POINTER_PLUS_EXPR
)
2885 genop
[i
] = fold_convert (nary
->type
, genop
[i
]);
2887 genop
[i
] = convert_to_ptrofftype (genop
[i
]);
2890 genop
[i
] = fold_convert (TREE_TYPE (nary
->op
[i
]), genop
[i
]);
2892 if (nary
->opcode
== CONSTRUCTOR
)
2894 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
2895 for (i
= 0; i
< nary
->length
; ++i
)
2896 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
, genop
[i
]);
2897 folded
= build_constructor (nary
->type
, elts
);
2901 switch (nary
->length
)
2904 folded
= fold_build1 (nary
->opcode
, nary
->type
,
2908 folded
= fold_build2 (nary
->opcode
, nary
->type
,
2909 genop
[0], genop
[1]);
2912 folded
= fold_build3 (nary
->opcode
, nary
->type
,
2913 genop
[0], genop
[1], genop
[2]);
2925 if (!useless_type_conversion_p (exprtype
, TREE_TYPE (folded
)))
2926 folded
= fold_convert (exprtype
, folded
);
2928 /* Force the generated expression to be a sequence of GIMPLE
2930 We have to call unshare_expr because force_gimple_operand may
2931 modify the tree we pass to it. */
2932 folded
= force_gimple_operand (unshare_expr (folded
), &forced_stmts
,
2935 /* If we have any intermediate expressions to the value sets, add them
2936 to the value sets and chain them in the instruction stream. */
2939 gsi
= gsi_start (forced_stmts
);
2940 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
2942 gimple stmt
= gsi_stmt (gsi
);
2943 tree forcedname
= gimple_get_lhs (stmt
);
2946 if (TREE_CODE (forcedname
) == SSA_NAME
)
2948 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (forcedname
));
2949 VN_INFO_GET (forcedname
)->valnum
= forcedname
;
2950 VN_INFO (forcedname
)->value_id
= get_next_value_id ();
2951 nameexpr
= get_or_alloc_expr_for_name (forcedname
);
2952 add_to_value (VN_INFO (forcedname
)->value_id
, nameexpr
);
2953 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2954 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2957 gimple_seq_add_seq (stmts
, forced_stmts
);
2960 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2961 newstmt
= gimple_build_assign (name
, folded
);
2962 gimple_set_plf (newstmt
, NECESSARY
, false);
2964 gimple_seq_add_stmt (stmts
, newstmt
);
2965 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (name
));
2967 /* Fold the last statement. */
2968 gsi
= gsi_last (*stmts
);
2969 if (fold_stmt_inplace (&gsi
))
2970 update_stmt (gsi_stmt (gsi
));
2972 /* Add a value number to the temporary.
2973 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2974 we are creating the expression by pieces, and this particular piece of
2975 the expression may have been represented. There is no harm in replacing
2977 value_id
= get_expr_value_id (expr
);
2978 VN_INFO_GET (name
)->value_id
= value_id
;
2979 VN_INFO (name
)->valnum
= sccvn_valnum_from_value_id (value_id
);
2980 if (VN_INFO (name
)->valnum
== NULL_TREE
)
2981 VN_INFO (name
)->valnum
= name
;
2982 gcc_assert (VN_INFO (name
)->valnum
!= NULL_TREE
);
2983 nameexpr
= get_or_alloc_expr_for_name (name
);
2984 add_to_value (value_id
, nameexpr
);
2985 if (NEW_SETS (block
))
2986 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2987 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2989 pre_stats
.insertions
++;
2990 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2992 fprintf (dump_file
, "Inserted ");
2993 print_gimple_stmt (dump_file
, newstmt
, 0, 0);
2994 fprintf (dump_file
, " in predecessor %d (%04d)\n",
2995 block
->index
, value_id
);
3002 /* Insert the to-be-made-available values of expression EXPRNUM for each
3003 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3004 merge the result with a phi node, given the same value number as
3005 NODE. Return true if we have inserted new stuff. */
3008 insert_into_preds_of_block (basic_block block
, unsigned int exprnum
,
3009 vec
<pre_expr
> avail
)
3011 pre_expr expr
= expression_for_id (exprnum
);
3013 unsigned int val
= get_expr_value_id (expr
);
3015 bool insertions
= false;
3020 tree type
= get_expr_type (expr
);
3024 /* Make sure we aren't creating an induction variable. */
3025 if (bb_loop_depth (block
) > 0 && EDGE_COUNT (block
->preds
) == 2)
3027 bool firstinsideloop
= false;
3028 bool secondinsideloop
= false;
3029 firstinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3030 EDGE_PRED (block
, 0)->src
);
3031 secondinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3032 EDGE_PRED (block
, 1)->src
);
3033 /* Induction variables only have one edge inside the loop. */
3034 if ((firstinsideloop
^ secondinsideloop
)
3035 && expr
->kind
!= REFERENCE
)
3037 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3038 fprintf (dump_file
, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3043 /* Make the necessary insertions. */
3044 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3046 gimple_seq stmts
= NULL
;
3049 eprime
= avail
[pred
->dest_idx
];
3051 if (eprime
->kind
!= NAME
&& eprime
->kind
!= CONSTANT
)
3053 builtexpr
= create_expression_by_pieces (bprime
, eprime
,
3055 gcc_assert (!(pred
->flags
& EDGE_ABNORMAL
));
3056 gsi_insert_seq_on_edge (pred
, stmts
);
3059 /* We cannot insert a PHI node if we failed to insert
3064 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_name (builtexpr
);
3067 else if (eprime
->kind
== CONSTANT
)
3069 /* Constants may not have the right type, fold_convert
3070 should give us back a constant with the right type. */
3071 tree constant
= PRE_EXPR_CONSTANT (eprime
);
3072 if (!useless_type_conversion_p (type
, TREE_TYPE (constant
)))
3074 tree builtexpr
= fold_convert (type
, constant
);
3075 if (!is_gimple_min_invariant (builtexpr
))
3077 tree forcedexpr
= force_gimple_operand (builtexpr
,
3080 if (!is_gimple_min_invariant (forcedexpr
))
3082 if (forcedexpr
!= builtexpr
)
3084 VN_INFO_GET (forcedexpr
)->valnum
= PRE_EXPR_CONSTANT (eprime
);
3085 VN_INFO (forcedexpr
)->value_id
= get_expr_value_id (eprime
);
3089 gimple_stmt_iterator gsi
;
3090 gsi
= gsi_start (stmts
);
3091 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
3093 gimple stmt
= gsi_stmt (gsi
);
3094 tree lhs
= gimple_get_lhs (stmt
);
3095 if (TREE_CODE (lhs
) == SSA_NAME
)
3096 bitmap_set_bit (inserted_exprs
,
3097 SSA_NAME_VERSION (lhs
));
3098 gimple_set_plf (stmt
, NECESSARY
, false);
3100 gsi_insert_seq_on_edge (pred
, stmts
);
3102 avail
[pred
->dest_idx
]
3103 = get_or_alloc_expr_for_name (forcedexpr
);
3107 avail
[pred
->dest_idx
]
3108 = get_or_alloc_expr_for_constant (builtexpr
);
3111 else if (eprime
->kind
== NAME
)
3113 /* We may have to do a conversion because our value
3114 numbering can look through types in certain cases, but
3115 our IL requires all operands of a phi node have the same
3117 tree name
= PRE_EXPR_NAME (eprime
);
3118 if (!useless_type_conversion_p (type
, TREE_TYPE (name
)))
3122 builtexpr
= fold_convert (type
, name
);
3123 forcedexpr
= force_gimple_operand (builtexpr
,
3127 if (forcedexpr
!= name
)
3129 VN_INFO_GET (forcedexpr
)->valnum
= VN_INFO (name
)->valnum
;
3130 VN_INFO (forcedexpr
)->value_id
= VN_INFO (name
)->value_id
;
3135 gimple_stmt_iterator gsi
;
3136 gsi
= gsi_start (stmts
);
3137 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
3139 gimple stmt
= gsi_stmt (gsi
);
3140 tree lhs
= gimple_get_lhs (stmt
);
3141 if (TREE_CODE (lhs
) == SSA_NAME
)
3142 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (lhs
));
3143 gimple_set_plf (stmt
, NECESSARY
, false);
3145 gsi_insert_seq_on_edge (pred
, stmts
);
3147 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_name (forcedexpr
);
3151 /* If we didn't want a phi node, and we made insertions, we still have
3152 inserted new stuff, and thus return true. If we didn't want a phi node,
3153 and didn't make insertions, we haven't added anything new, so return
3155 if (nophi
&& insertions
)
3157 else if (nophi
&& !insertions
)
3160 /* Now build a phi for the new variable. */
3161 temp
= make_temp_ssa_name (type
, NULL
, "prephitmp");
3162 phi
= create_phi_node (temp
, block
);
3164 gimple_set_plf (phi
, NECESSARY
, false);
3165 VN_INFO_GET (temp
)->value_id
= val
;
3166 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3167 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3168 VN_INFO (temp
)->valnum
= temp
;
3169 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3170 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3172 pre_expr ae
= avail
[pred
->dest_idx
];
3173 gcc_assert (get_expr_type (ae
) == type
3174 || useless_type_conversion_p (type
, get_expr_type (ae
)));
3175 if (ae
->kind
== CONSTANT
)
3176 add_phi_arg (phi
, unshare_expr (PRE_EXPR_CONSTANT (ae
)),
3177 pred
, UNKNOWN_LOCATION
);
3179 add_phi_arg (phi
, PRE_EXPR_NAME (ae
), pred
, UNKNOWN_LOCATION
);
3182 newphi
= get_or_alloc_expr_for_name (temp
);
3183 add_to_value (val
, newphi
);
3185 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3186 this insertion, since we test for the existence of this value in PHI_GEN
3187 before proceeding with the partial redundancy checks in insert_aux.
3189 The value may exist in AVAIL_OUT, in particular, it could be represented
3190 by the expression we are trying to eliminate, in which case we want the
3191 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3194 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3195 this block, because if it did, it would have existed in our dominator's
3196 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3199 bitmap_insert_into_set (PHI_GEN (block
), newphi
);
3200 bitmap_value_replace_in_set (AVAIL_OUT (block
),
3202 bitmap_insert_into_set (NEW_SETS (block
),
3205 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3207 fprintf (dump_file
, "Created phi ");
3208 print_gimple_stmt (dump_file
, phi
, 0, 0);
3209 fprintf (dump_file
, " in block %d (%04d)\n", block
->index
, val
);
3217 /* Perform insertion of partially redundant values.
3218 For BLOCK, do the following:
3219 1. Propagate the NEW_SETS of the dominator into the current block.
3220 If the block has multiple predecessors,
3221 2a. Iterate over the ANTIC expressions for the block to see if
3222 any of them are partially redundant.
3223 2b. If so, insert them into the necessary predecessors to make
3224 the expression fully redundant.
3225 2c. Insert a new PHI merging the values of the predecessors.
3226 2d. Insert the new PHI, and the new expressions, into the
3228 3. Recursively call ourselves on the dominator children of BLOCK.
3230 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
3231 do_regular_insertion and do_partial_insertion.
3236 do_regular_insertion (basic_block block
, basic_block dom
)
3238 bool new_stuff
= false;
3239 vec
<pre_expr
> exprs
;
3241 vec
<pre_expr
> avail
= vNULL
;
3244 exprs
= sorted_array_from_bitmap_set (ANTIC_IN (block
));
3245 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3247 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3249 if (expr
->kind
== NARY
3250 || expr
->kind
== REFERENCE
)
3253 bool by_some
= false;
3254 bool cant_insert
= false;
3255 bool all_same
= true;
3256 pre_expr first_s
= NULL
;
3259 pre_expr eprime
= NULL
;
3261 pre_expr edoubleprime
= NULL
;
3262 bool do_insertion
= false;
3264 val
= get_expr_value_id (expr
);
3265 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3267 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3269 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3271 fprintf (dump_file
, "Found fully redundant value: ");
3272 print_pre_expr (dump_file
, expr
);
3273 fprintf (dump_file
, "\n");
3278 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3280 unsigned int vprime
;
3282 /* We should never run insertion for the exit block
3283 and so not come across fake pred edges. */
3284 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3286 eprime
= phi_translate (expr
, ANTIC_IN (block
), NULL
,
3289 /* eprime will generally only be NULL if the
3290 value of the expression, translated
3291 through the PHI for this predecessor, is
3292 undefined. If that is the case, we can't
3293 make the expression fully redundant,
3294 because its value is undefined along a
3295 predecessor path. We can thus break out
3296 early because it doesn't matter what the
3297 rest of the results are. */
3300 avail
[pred
->dest_idx
] = NULL
;
3305 eprime
= fully_constant_expression (eprime
);
3306 vprime
= get_expr_value_id (eprime
);
3307 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
),
3309 if (edoubleprime
== NULL
)
3311 avail
[pred
->dest_idx
] = eprime
;
3316 avail
[pred
->dest_idx
] = edoubleprime
;
3318 /* We want to perform insertions to remove a redundancy on
3319 a path in the CFG we want to optimize for speed. */
3320 if (optimize_edge_for_speed_p (pred
))
3321 do_insertion
= true;
3322 if (first_s
== NULL
)
3323 first_s
= edoubleprime
;
3324 else if (!pre_expr_d::equal (first_s
, edoubleprime
))
3328 /* If we can insert it, it's not the same value
3329 already existing along every predecessor, and
3330 it's defined by some predecessor, it is
3331 partially redundant. */
3332 if (!cant_insert
&& !all_same
&& by_some
)
3336 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3338 fprintf (dump_file
, "Skipping partial redundancy for "
3340 print_pre_expr (dump_file
, expr
);
3341 fprintf (dump_file
, " (%04d), no redundancy on to be "
3342 "optimized for speed edge\n", val
);
3345 else if (dbg_cnt (treepre_insert
))
3347 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3349 fprintf (dump_file
, "Found partial redundancy for "
3351 print_pre_expr (dump_file
, expr
);
3352 fprintf (dump_file
, " (%04d)\n",
3353 get_expr_value_id (expr
));
3355 if (insert_into_preds_of_block (block
,
3356 get_expression_id (expr
),
3361 /* If all edges produce the same value and that value is
3362 an invariant, then the PHI has the same value on all
3363 edges. Note this. */
3364 else if (!cant_insert
&& all_same
)
3366 gcc_assert (edoubleprime
->kind
== CONSTANT
3367 || edoubleprime
->kind
== NAME
);
3369 tree temp
= make_temp_ssa_name (get_expr_type (expr
),
3371 gimple assign
= gimple_build_assign (temp
,
3372 edoubleprime
->kind
== CONSTANT
? PRE_EXPR_CONSTANT (edoubleprime
) : PRE_EXPR_NAME (edoubleprime
));
3373 gimple_stmt_iterator gsi
= gsi_after_labels (block
);
3374 gsi_insert_before (&gsi
, assign
, GSI_NEW_STMT
);
3376 gimple_set_plf (assign
, NECESSARY
, false);
3377 VN_INFO_GET (temp
)->value_id
= val
;
3378 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3379 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3380 VN_INFO (temp
)->valnum
= temp
;
3381 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3382 pre_expr newe
= get_or_alloc_expr_for_name (temp
);
3383 add_to_value (val
, newe
);
3384 bitmap_value_replace_in_set (AVAIL_OUT (block
), newe
);
3385 bitmap_insert_into_set (NEW_SETS (block
), newe
);
3395 /* Perform insertion for partially anticipatable expressions. There
3396 is only one case we will perform insertion for these. This case is
3397 if the expression is partially anticipatable, and fully available.
3398 In this case, we know that putting it earlier will enable us to
3399 remove the later computation. */
3403 do_partial_partial_insertion (basic_block block
, basic_block dom
)
3405 bool new_stuff
= false;
3406 vec
<pre_expr
> exprs
;
3408 auto_vec
<pre_expr
> avail
;
3411 exprs
= sorted_array_from_bitmap_set (PA_IN (block
));
3412 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3414 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3416 if (expr
->kind
== NARY
3417 || expr
->kind
== REFERENCE
)
3421 bool cant_insert
= false;
3424 pre_expr eprime
= NULL
;
3427 val
= get_expr_value_id (expr
);
3428 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3430 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3433 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3435 unsigned int vprime
;
3436 pre_expr edoubleprime
;
3438 /* We should never run insertion for the exit block
3439 and so not come across fake pred edges. */
3440 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3442 eprime
= phi_translate (expr
, ANTIC_IN (block
),
3446 /* eprime will generally only be NULL if the
3447 value of the expression, translated
3448 through the PHI for this predecessor, is
3449 undefined. If that is the case, we can't
3450 make the expression fully redundant,
3451 because its value is undefined along a
3452 predecessor path. We can thus break out
3453 early because it doesn't matter what the
3454 rest of the results are. */
3457 avail
[pred
->dest_idx
] = NULL
;
3462 eprime
= fully_constant_expression (eprime
);
3463 vprime
= get_expr_value_id (eprime
);
3464 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
), vprime
);
3465 avail
[pred
->dest_idx
] = edoubleprime
;
3466 if (edoubleprime
== NULL
)
3473 /* If we can insert it, it's not the same value
3474 already existing along every predecessor, and
3475 it's defined by some predecessor, it is
3476 partially redundant. */
3477 if (!cant_insert
&& by_all
)
3480 bool do_insertion
= false;
3482 /* Insert only if we can remove a later expression on a path
3483 that we want to optimize for speed.
3484 The phi node that we will be inserting in BLOCK is not free,
3485 and inserting it for the sake of !optimize_for_speed successor
3486 may cause regressions on the speed path. */
3487 FOR_EACH_EDGE (succ
, ei
, block
->succs
)
3489 if (bitmap_set_contains_value (PA_IN (succ
->dest
), val
)
3490 || bitmap_set_contains_value (ANTIC_IN (succ
->dest
), val
))
3492 if (optimize_edge_for_speed_p (succ
))
3493 do_insertion
= true;
3499 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3501 fprintf (dump_file
, "Skipping partial partial redundancy "
3503 print_pre_expr (dump_file
, expr
);
3504 fprintf (dump_file
, " (%04d), not (partially) anticipated "
3505 "on any to be optimized for speed edges\n", val
);
3508 else if (dbg_cnt (treepre_insert
))
3510 pre_stats
.pa_insert
++;
3511 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3513 fprintf (dump_file
, "Found partial partial redundancy "
3515 print_pre_expr (dump_file
, expr
);
3516 fprintf (dump_file
, " (%04d)\n",
3517 get_expr_value_id (expr
));
3519 if (insert_into_preds_of_block (block
,
3520 get_expression_id (expr
),
3533 insert_aux (basic_block block
)
3536 bool new_stuff
= false;
3541 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3546 bitmap_set_t newset
= NEW_SETS (dom
);
3549 /* Note that we need to value_replace both NEW_SETS, and
3550 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3551 represented by some non-simple expression here that we want
3552 to replace it with. */
3553 FOR_EACH_EXPR_ID_IN_SET (newset
, i
, bi
)
3555 pre_expr expr
= expression_for_id (i
);
3556 bitmap_value_replace_in_set (NEW_SETS (block
), expr
);
3557 bitmap_value_replace_in_set (AVAIL_OUT (block
), expr
);
3560 if (!single_pred_p (block
))
3562 new_stuff
|= do_regular_insertion (block
, dom
);
3563 if (do_partial_partial
)
3564 new_stuff
|= do_partial_partial_insertion (block
, dom
);
3568 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
3570 son
= next_dom_son (CDI_DOMINATORS
, son
))
3572 new_stuff
|= insert_aux (son
);
3578 /* Perform insertion of partially redundant values. */
3583 bool new_stuff
= true;
3585 int num_iterations
= 0;
3587 FOR_ALL_BB_FN (bb
, cfun
)
3588 NEW_SETS (bb
) = bitmap_set_new ();
3593 if (dump_file
&& dump_flags
& TDF_DETAILS
)
3594 fprintf (dump_file
, "Starting insert iteration %d\n", num_iterations
);
3595 new_stuff
= insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3597 /* Clear the NEW sets before the next iteration. We have already
3598 fully propagated its contents. */
3600 FOR_ALL_BB_FN (bb
, cfun
)
3601 bitmap_set_free (NEW_SETS (bb
));
3603 statistics_histogram_event (cfun
, "insert iterations", num_iterations
);
3607 /* Compute the AVAIL set for all basic blocks.
3609 This function performs value numbering of the statements in each basic
3610 block. The AVAIL sets are built from information we glean while doing
3611 this value numbering, since the AVAIL sets contain only one entry per
3614 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3615 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3618 compute_avail (void)
3621 basic_block block
, son
;
3622 basic_block
*worklist
;
3626 /* We pretend that default definitions are defined in the entry block.
3627 This includes function arguments and the static chain decl. */
3628 for (i
= 1; i
< num_ssa_names
; ++i
)
3630 tree name
= ssa_name (i
);
3633 || !SSA_NAME_IS_DEFAULT_DEF (name
)
3634 || has_zero_uses (name
)
3635 || virtual_operand_p (name
))
3638 e
= get_or_alloc_expr_for_name (name
);
3639 add_to_value (get_expr_value_id (e
), e
);
3640 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)), e
);
3641 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3645 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3647 print_bitmap_set (dump_file
, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3648 "tmp_gen", ENTRY_BLOCK
);
3649 print_bitmap_set (dump_file
, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3650 "avail_out", ENTRY_BLOCK
);
3653 /* Allocate the worklist. */
3654 worklist
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
3656 /* Seed the algorithm by putting the dominator children of the entry
3657 block on the worklist. */
3658 for (son
= first_dom_son (CDI_DOMINATORS
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3660 son
= next_dom_son (CDI_DOMINATORS
, son
))
3661 worklist
[sp
++] = son
;
3663 /* Loop until the worklist is empty. */
3666 gimple_stmt_iterator gsi
;
3670 /* Pick a block from the worklist. */
3671 block
= worklist
[--sp
];
3673 /* Initially, the set of available values in BLOCK is that of
3674 its immediate dominator. */
3675 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3677 bitmap_set_copy (AVAIL_OUT (block
), AVAIL_OUT (dom
));
3679 /* Generate values for PHI nodes. */
3680 for (gsi
= gsi_start_phis (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3682 tree result
= gimple_phi_result (gsi_stmt (gsi
));
3684 /* We have no need for virtual phis, as they don't represent
3685 actual computations. */
3686 if (virtual_operand_p (result
))
3689 pre_expr e
= get_or_alloc_expr_for_name (result
);
3690 add_to_value (get_expr_value_id (e
), e
);
3691 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3692 bitmap_insert_into_set (PHI_GEN (block
), e
);
3695 BB_MAY_NOTRETURN (block
) = 0;
3697 /* Now compute value numbers and populate value sets with all
3698 the expressions computed in BLOCK. */
3699 for (gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3704 stmt
= gsi_stmt (gsi
);
3706 /* Cache whether the basic-block has any non-visible side-effect
3708 If this isn't a call or it is the last stmt in the
3709 basic-block then the CFG represents things correctly. */
3710 if (is_gimple_call (stmt
) && !stmt_ends_bb_p (stmt
))
3712 /* Non-looping const functions always return normally.
3713 Otherwise the call might not return or have side-effects
3714 that forbids hoisting possibly trapping expressions
3716 int flags
= gimple_call_flags (stmt
);
3717 if (!(flags
& ECF_CONST
)
3718 || (flags
& ECF_LOOPING_CONST_OR_PURE
))
3719 BB_MAY_NOTRETURN (block
) = 1;
3722 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
3724 pre_expr e
= get_or_alloc_expr_for_name (op
);
3726 add_to_value (get_expr_value_id (e
), e
);
3727 bitmap_insert_into_set (TMP_GEN (block
), e
);
3728 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3731 if (gimple_has_side_effects (stmt
)
3732 || stmt_could_throw_p (stmt
)
3733 || is_gimple_debug (stmt
))
3736 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
3738 if (ssa_undefined_value_p (op
))
3740 pre_expr e
= get_or_alloc_expr_for_name (op
);
3741 bitmap_value_insert_into_set (EXP_GEN (block
), e
);
3744 switch (gimple_code (stmt
))
3752 vn_reference_s ref1
;
3753 pre_expr result
= NULL
;
3755 /* We can value number only calls to real functions. */
3756 if (gimple_call_internal_p (stmt
))
3759 vn_reference_lookup_call (stmt
, &ref
, &ref1
);
3763 /* If the value of the call is not invalidated in
3764 this block until it is computed, add the expression
3766 if (!gimple_vuse (stmt
)
3768 (SSA_NAME_DEF_STMT (gimple_vuse (stmt
))) == GIMPLE_PHI
3769 || gimple_bb (SSA_NAME_DEF_STMT
3770 (gimple_vuse (stmt
))) != block
)
3772 result
= (pre_expr
) pool_alloc (pre_expr_pool
);
3773 result
->kind
= REFERENCE
;
3775 PRE_EXPR_REFERENCE (result
) = ref
;
3777 get_or_alloc_expression_id (result
);
3778 add_to_value (get_expr_value_id (result
), result
);
3779 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
3786 pre_expr result
= NULL
;
3787 switch (vn_get_stmt_kind (stmt
))
3791 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3794 /* COND_EXPR and VEC_COND_EXPR are awkward in
3795 that they contain an embedded complex expression.
3796 Don't even try to shove those through PRE. */
3797 if (code
== COND_EXPR
3798 || code
== VEC_COND_EXPR
)
3801 vn_nary_op_lookup_stmt (stmt
, &nary
);
3805 /* If the NARY traps and there was a preceding
3806 point in the block that might not return avoid
3807 adding the nary to EXP_GEN. */
3808 if (BB_MAY_NOTRETURN (block
)
3809 && vn_nary_may_trap (nary
))
3812 result
= (pre_expr
) pool_alloc (pre_expr_pool
);
3813 result
->kind
= NARY
;
3815 PRE_EXPR_NARY (result
) = nary
;
3822 vn_reference_lookup (gimple_assign_rhs1 (stmt
),
3828 /* If the value of the reference is not invalidated in
3829 this block until it is computed, add the expression
3831 if (gimple_vuse (stmt
))
3835 def_stmt
= SSA_NAME_DEF_STMT (gimple_vuse (stmt
));
3836 while (!gimple_nop_p (def_stmt
)
3837 && gimple_code (def_stmt
) != GIMPLE_PHI
3838 && gimple_bb (def_stmt
) == block
)
3840 if (stmt_may_clobber_ref_p
3841 (def_stmt
, gimple_assign_rhs1 (stmt
)))
3847 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt
));
3853 result
= (pre_expr
) pool_alloc (pre_expr_pool
);
3854 result
->kind
= REFERENCE
;
3856 PRE_EXPR_REFERENCE (result
) = ref
;
3864 get_or_alloc_expression_id (result
);
3865 add_to_value (get_expr_value_id (result
), result
);
3866 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
3874 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3876 print_bitmap_set (dump_file
, EXP_GEN (block
),
3877 "exp_gen", block
->index
);
3878 print_bitmap_set (dump_file
, PHI_GEN (block
),
3879 "phi_gen", block
->index
);
3880 print_bitmap_set (dump_file
, TMP_GEN (block
),
3881 "tmp_gen", block
->index
);
3882 print_bitmap_set (dump_file
, AVAIL_OUT (block
),
3883 "avail_out", block
->index
);
3886 /* Put the dominator children of BLOCK on the worklist of blocks
3887 to compute available sets for. */
3888 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
3890 son
= next_dom_son (CDI_DOMINATORS
, son
))
3891 worklist
[sp
++] = son
;
3898 /* Local state for the eliminate domwalk. */
3899 static vec
<gimple
> el_to_remove
;
3900 static unsigned int el_todo
;
3901 static vec
<tree
> el_avail
;
3902 static vec
<tree
> el_avail_stack
;
3904 /* Return a leader for OP that is available at the current point of the
3905 eliminate domwalk. */
3908 eliminate_avail (tree op
)
3910 tree valnum
= VN_INFO (op
)->valnum
;
3911 if (TREE_CODE (valnum
) == SSA_NAME
)
3913 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
3915 if (el_avail
.length () > SSA_NAME_VERSION (valnum
))
3916 return el_avail
[SSA_NAME_VERSION (valnum
)];
3918 else if (is_gimple_min_invariant (valnum
))
3923 /* At the current point of the eliminate domwalk make OP available. */
3926 eliminate_push_avail (tree op
)
3928 tree valnum
= VN_INFO (op
)->valnum
;
3929 if (TREE_CODE (valnum
) == SSA_NAME
)
3931 if (el_avail
.length () <= SSA_NAME_VERSION (valnum
))
3932 el_avail
.safe_grow_cleared (SSA_NAME_VERSION (valnum
) + 1);
3933 el_avail
[SSA_NAME_VERSION (valnum
)] = op
;
3934 el_avail_stack
.safe_push (op
);
3938 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
3939 the leader for the expression if insertion was successful. */
3942 eliminate_insert (gimple_stmt_iterator
*gsi
, tree val
)
3944 tree expr
= vn_get_expr_for (val
);
3945 if (!CONVERT_EXPR_P (expr
)
3946 && TREE_CODE (expr
) != VIEW_CONVERT_EXPR
)
3949 tree op
= TREE_OPERAND (expr
, 0);
3950 tree leader
= TREE_CODE (op
) == SSA_NAME
? eliminate_avail (op
) : op
;
3954 tree res
= make_temp_ssa_name (TREE_TYPE (val
), NULL
, "pretmp");
3955 gimple tem
= gimple_build_assign (res
,
3956 fold_build1 (TREE_CODE (expr
),
3957 TREE_TYPE (expr
), leader
));
3958 gsi_insert_before (gsi
, tem
, GSI_SAME_STMT
);
3959 VN_INFO_GET (res
)->valnum
= val
;
3961 if (TREE_CODE (leader
) == SSA_NAME
)
3962 gimple_set_plf (SSA_NAME_DEF_STMT (leader
), NECESSARY
, true);
3964 pre_stats
.insertions
++;
3965 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3967 fprintf (dump_file
, "Inserted ");
3968 print_gimple_stmt (dump_file
, tem
, 0, 0);
3974 class eliminate_dom_walker
: public dom_walker
3977 eliminate_dom_walker (cdi_direction direction
, bool do_pre_
)
3978 : dom_walker (direction
), do_pre (do_pre_
) {}
3980 virtual void before_dom_children (basic_block
);
3981 virtual void after_dom_children (basic_block
);
3986 /* Perform elimination for the basic-block B during the domwalk. */
3989 eliminate_dom_walker::before_dom_children (basic_block b
)
3991 gimple_stmt_iterator gsi
;
3995 el_avail_stack
.safe_push (NULL_TREE
);
3997 /* ??? If we do nothing for unreachable blocks then this will confuse
3998 tailmerging. Eventually we can reduce its reliance on SCCVN now
3999 that we fully copy/constant-propagate (most) things. */
4001 for (gsi
= gsi_start_phis (b
); !gsi_end_p (gsi
);)
4003 gimple phi
= gsi_stmt (gsi
);
4004 tree res
= PHI_RESULT (phi
);
4006 if (virtual_operand_p (res
))
4012 tree sprime
= eliminate_avail (res
);
4016 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4018 fprintf (dump_file
, "Replaced redundant PHI node defining ");
4019 print_generic_expr (dump_file
, res
, 0);
4020 fprintf (dump_file
, " with ");
4021 print_generic_expr (dump_file
, sprime
, 0);
4022 fprintf (dump_file
, "\n");
4025 /* If we inserted this PHI node ourself, it's not an elimination. */
4027 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
)))
4030 pre_stats
.eliminations
++;
4032 /* If we will propagate into all uses don't bother to do
4034 if (may_propagate_copy (res
, sprime
))
4036 /* Mark the PHI for removal. */
4037 el_to_remove
.safe_push (phi
);
4042 remove_phi_node (&gsi
, false);
4045 && !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
))
4046 && TREE_CODE (sprime
) == SSA_NAME
)
4047 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
4049 if (!useless_type_conversion_p (TREE_TYPE (res
), TREE_TYPE (sprime
)))
4050 sprime
= fold_convert (TREE_TYPE (res
), sprime
);
4051 gimple stmt
= gimple_build_assign (res
, sprime
);
4052 /* ??? It cannot yet be necessary (DOM walk). */
4053 gimple_set_plf (stmt
, NECESSARY
, gimple_plf (phi
, NECESSARY
));
4055 gimple_stmt_iterator gsi2
= gsi_after_labels (b
);
4056 gsi_insert_before (&gsi2
, stmt
, GSI_NEW_STMT
);
4060 eliminate_push_avail (res
);
4064 for (gsi
= gsi_start_bb (b
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4066 tree sprime
= NULL_TREE
;
4067 stmt
= gsi_stmt (gsi
);
4068 tree lhs
= gimple_get_lhs (stmt
);
4069 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
4070 && !gimple_has_volatile_ops (stmt
)
4071 /* See PR43491. Do not replace a global register variable when
4072 it is a the RHS of an assignment. Do replace local register
4073 variables since gcc does not guarantee a local variable will
4074 be allocated in register.
4075 ??? The fix isn't effective here. This should instead
4076 be ensured by not value-numbering them the same but treating
4077 them like volatiles? */
4078 && !(gimple_assign_single_p (stmt
)
4079 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == VAR_DECL
4080 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
))
4081 && is_global_var (gimple_assign_rhs1 (stmt
)))))
4083 sprime
= eliminate_avail (lhs
);
4086 /* If there is no existing usable leader but SCCVN thinks
4087 it has an expression it wants to use as replacement,
4089 tree val
= VN_INFO (lhs
)->valnum
;
4091 && TREE_CODE (val
) == SSA_NAME
4092 && VN_INFO (val
)->needs_insertion
4093 && VN_INFO (val
)->expr
!= NULL_TREE
4094 && (sprime
= eliminate_insert (&gsi
, val
)) != NULL_TREE
)
4095 eliminate_push_avail (sprime
);
4098 /* If this now constitutes a copy duplicate points-to
4099 and range info appropriately. This is especially
4100 important for inserted code. See tree-ssa-copy.c
4101 for similar code. */
4103 && TREE_CODE (sprime
) == SSA_NAME
)
4105 basic_block sprime_b
= gimple_bb (SSA_NAME_DEF_STMT (sprime
));
4106 if (POINTER_TYPE_P (TREE_TYPE (lhs
))
4107 && SSA_NAME_PTR_INFO (lhs
)
4108 && !SSA_NAME_PTR_INFO (sprime
))
4110 duplicate_ssa_name_ptr_info (sprime
,
4111 SSA_NAME_PTR_INFO (lhs
));
4113 mark_ptr_info_alignment_unknown
4114 (SSA_NAME_PTR_INFO (sprime
));
4116 else if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
4117 && SSA_NAME_RANGE_INFO (lhs
)
4118 && !SSA_NAME_RANGE_INFO (sprime
)
4120 duplicate_ssa_name_range_info (sprime
,
4121 SSA_NAME_RANGE_TYPE (lhs
),
4122 SSA_NAME_RANGE_INFO (lhs
));
4125 /* Inhibit the use of an inserted PHI on a loop header when
4126 the address of the memory reference is a simple induction
4127 variable. In other cases the vectorizer won't do anything
4128 anyway (either it's loop invariant or a complicated
4131 && TREE_CODE (sprime
) == SSA_NAME
4133 && flag_tree_loop_vectorize
4134 && loop_outer (b
->loop_father
)
4135 && has_zero_uses (sprime
)
4136 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))
4137 && gimple_assign_load_p (stmt
))
4139 gimple def_stmt
= SSA_NAME_DEF_STMT (sprime
);
4140 basic_block def_bb
= gimple_bb (def_stmt
);
4141 if (gimple_code (def_stmt
) == GIMPLE_PHI
4142 && b
->loop_father
->header
== def_bb
)
4147 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
4150 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (op
));
4152 && flow_bb_inside_loop_p (b
->loop_father
, def_bb
)
4153 && simple_iv (b
->loop_father
,
4154 b
->loop_father
, op
, &iv
, true))
4162 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4164 fprintf (dump_file
, "Not replacing ");
4165 print_gimple_expr (dump_file
, stmt
, 0, 0);
4166 fprintf (dump_file
, " with ");
4167 print_generic_expr (dump_file
, sprime
, 0);
4168 fprintf (dump_file
, " which would add a loop"
4169 " carried dependence to loop %d\n",
4170 b
->loop_father
->num
);
4172 /* Don't keep sprime available. */
4180 /* If we can propagate the value computed for LHS into
4181 all uses don't bother doing anything with this stmt. */
4182 if (may_propagate_copy (lhs
, sprime
))
4184 /* Mark it for removal. */
4185 el_to_remove
.safe_push (stmt
);
4187 /* ??? Don't count copy/constant propagations. */
4188 if (gimple_assign_single_p (stmt
)
4189 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4190 || gimple_assign_rhs1 (stmt
) == sprime
))
4193 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4195 fprintf (dump_file
, "Replaced ");
4196 print_gimple_expr (dump_file
, stmt
, 0, 0);
4197 fprintf (dump_file
, " with ");
4198 print_generic_expr (dump_file
, sprime
, 0);
4199 fprintf (dump_file
, " in all uses of ");
4200 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4203 pre_stats
.eliminations
++;
4207 /* If this is an assignment from our leader (which
4208 happens in the case the value-number is a constant)
4209 then there is nothing to do. */
4210 if (gimple_assign_single_p (stmt
)
4211 && sprime
== gimple_assign_rhs1 (stmt
))
4214 /* Else replace its RHS. */
4215 bool can_make_abnormal_goto
4216 = is_gimple_call (stmt
)
4217 && stmt_can_make_abnormal_goto (stmt
);
4219 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4221 fprintf (dump_file
, "Replaced ");
4222 print_gimple_expr (dump_file
, stmt
, 0, 0);
4223 fprintf (dump_file
, " with ");
4224 print_generic_expr (dump_file
, sprime
, 0);
4225 fprintf (dump_file
, " in ");
4226 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4229 if (TREE_CODE (sprime
) == SSA_NAME
)
4230 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4233 pre_stats
.eliminations
++;
4234 gimple orig_stmt
= stmt
;
4235 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
4236 TREE_TYPE (sprime
)))
4237 sprime
= fold_convert (TREE_TYPE (lhs
), sprime
);
4238 tree vdef
= gimple_vdef (stmt
);
4239 tree vuse
= gimple_vuse (stmt
);
4240 propagate_tree_value_into_stmt (&gsi
, sprime
);
4241 stmt
= gsi_stmt (gsi
);
4243 if (vdef
!= gimple_vdef (stmt
))
4244 VN_INFO (vdef
)->valnum
= vuse
;
4246 /* If we removed EH side-effects from the statement, clean
4247 its EH information. */
4248 if (maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
))
4250 bitmap_set_bit (need_eh_cleanup
,
4251 gimple_bb (stmt
)->index
);
4252 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4253 fprintf (dump_file
, " Removed EH side-effects.\n");
4256 /* Likewise for AB side-effects. */
4257 if (can_make_abnormal_goto
4258 && !stmt_can_make_abnormal_goto (stmt
))
4260 bitmap_set_bit (need_ab_cleanup
,
4261 gimple_bb (stmt
)->index
);
4262 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4263 fprintf (dump_file
, " Removed AB side-effects.\n");
4270 /* If the statement is a scalar store, see if the expression
4271 has the same value number as its rhs. If so, the store is
4273 if (gimple_assign_single_p (stmt
)
4274 && !gimple_has_volatile_ops (stmt
)
4275 && !is_gimple_reg (gimple_assign_lhs (stmt
))
4276 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4277 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
))))
4280 tree rhs
= gimple_assign_rhs1 (stmt
);
4281 val
= vn_reference_lookup (gimple_assign_lhs (stmt
),
4282 gimple_vuse (stmt
), VN_WALK
, NULL
);
4283 if (TREE_CODE (rhs
) == SSA_NAME
)
4284 rhs
= VN_INFO (rhs
)->valnum
;
4286 && operand_equal_p (val
, rhs
, 0))
4288 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4290 fprintf (dump_file
, "Deleted redundant store ");
4291 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4294 /* Queue stmt for removal. */
4295 el_to_remove
.safe_push (stmt
);
4300 bool can_make_abnormal_goto
= stmt_can_make_abnormal_goto (stmt
);
4301 bool was_noreturn
= (is_gimple_call (stmt
)
4302 && gimple_call_noreturn_p (stmt
));
4303 tree vdef
= gimple_vdef (stmt
);
4304 tree vuse
= gimple_vuse (stmt
);
4306 /* If we didn't replace the whole stmt (or propagate the result
4307 into all uses), replace all uses on this stmt with their
4309 use_operand_p use_p
;
4311 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
4313 tree use
= USE_FROM_PTR (use_p
);
4314 /* ??? The call code above leaves stmt operands un-updated. */
4315 if (TREE_CODE (use
) != SSA_NAME
)
4317 tree sprime
= eliminate_avail (use
);
4318 if (sprime
&& sprime
!= use
4319 && may_propagate_copy (use
, sprime
)
4320 /* We substitute into debug stmts to avoid excessive
4321 debug temporaries created by removed stmts, but we need
4322 to avoid doing so for inserted sprimes as we never want
4323 to create debug temporaries for them. */
4325 || TREE_CODE (sprime
) != SSA_NAME
4326 || !is_gimple_debug (stmt
)
4327 || !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))))
4329 propagate_value (use_p
, sprime
);
4330 gimple_set_modified (stmt
, true);
4331 if (TREE_CODE (sprime
) == SSA_NAME
4332 && !is_gimple_debug (stmt
))
4333 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4338 /* Visit indirect calls and turn them into direct calls if
4339 possible using the devirtualization machinery. */
4340 if (is_gimple_call (stmt
))
4342 tree fn
= gimple_call_fn (stmt
);
4344 && flag_devirtualize
4345 && virtual_method_call_p (fn
))
4348 HOST_WIDE_INT otr_token
;
4349 ipa_polymorphic_call_context context
;
4353 instance
= get_polymorphic_call_info (current_function_decl
,
4355 &otr_type
, &otr_token
, &context
, stmt
);
4357 context
.get_dynamic_type (instance
, OBJ_TYPE_REF_OBJECT (fn
), otr_type
, stmt
);
4359 vec
<cgraph_node
*>targets
4360 = possible_polymorphic_call_targets (obj_type_ref_class (fn
),
4362 (OBJ_TYPE_REF_TOKEN (fn
)),
4365 if (dump_enabled_p ())
4366 dump_possible_polymorphic_call_targets (dump_file
,
4367 obj_type_ref_class (fn
),
4369 (OBJ_TYPE_REF_TOKEN (fn
)),
4371 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
4374 if (targets
.length () == 1)
4375 fn
= targets
[0]->decl
;
4377 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
4378 if (dump_enabled_p ())
4380 location_t loc
= gimple_location_safe (stmt
);
4381 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
4382 "converting indirect call to "
4384 cgraph_node::get (fn
)->name ());
4386 gimple_call_set_fndecl (stmt
, fn
);
4387 gimple_set_modified (stmt
, true);
4390 gcc_assert (!ipa_intraprocedural_devirtualization (stmt
));
4394 if (gimple_modified_p (stmt
))
4396 /* If a formerly non-invariant ADDR_EXPR is turned into an
4397 invariant one it was on a separate stmt. */
4398 if (gimple_assign_single_p (stmt
)
4399 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
4400 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
4401 gimple old_stmt
= stmt
;
4402 if (is_gimple_call (stmt
))
4404 /* ??? Only fold calls inplace for now, this may create new
4405 SSA names which in turn will confuse free_scc_vn SSA name
4407 fold_stmt_inplace (&gsi
);
4408 /* When changing a call into a noreturn call, cfg cleanup
4409 is needed to fix up the noreturn call. */
4410 if (!was_noreturn
&& gimple_call_noreturn_p (stmt
))
4411 el_todo
|= TODO_cleanup_cfg
;
4416 stmt
= gsi_stmt (gsi
);
4417 if ((gimple_code (stmt
) == GIMPLE_COND
4418 && (gimple_cond_true_p (stmt
)
4419 || gimple_cond_false_p (stmt
)))
4420 || (gimple_code (stmt
) == GIMPLE_SWITCH
4421 && TREE_CODE (gimple_switch_index (stmt
)) == INTEGER_CST
))
4422 el_todo
|= TODO_cleanup_cfg
;
4424 /* If we removed EH side-effects from the statement, clean
4425 its EH information. */
4426 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
4428 bitmap_set_bit (need_eh_cleanup
,
4429 gimple_bb (stmt
)->index
);
4430 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4431 fprintf (dump_file
, " Removed EH side-effects.\n");
4433 /* Likewise for AB side-effects. */
4434 if (can_make_abnormal_goto
4435 && !stmt_can_make_abnormal_goto (stmt
))
4437 bitmap_set_bit (need_ab_cleanup
,
4438 gimple_bb (stmt
)->index
);
4439 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4440 fprintf (dump_file
, " Removed AB side-effects.\n");
4443 if (vdef
!= gimple_vdef (stmt
))
4444 VN_INFO (vdef
)->valnum
= vuse
;
4447 /* Make new values available - for fully redundant LHS we
4448 continue with the next stmt above and skip this. */
4450 FOR_EACH_SSA_DEF_OPERAND (defp
, stmt
, iter
, SSA_OP_DEF
)
4451 eliminate_push_avail (DEF_FROM_PTR (defp
));
4454 /* Replace destination PHI arguments. */
4457 FOR_EACH_EDGE (e
, ei
, b
->succs
)
4459 for (gsi
= gsi_start_phis (e
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4461 gimple phi
= gsi_stmt (gsi
);
4462 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
4463 tree arg
= USE_FROM_PTR (use_p
);
4464 if (TREE_CODE (arg
) != SSA_NAME
4465 || virtual_operand_p (arg
))
4467 tree sprime
= eliminate_avail (arg
);
4468 if (sprime
&& may_propagate_copy (arg
, sprime
))
4470 propagate_value (use_p
, sprime
);
4471 if (TREE_CODE (sprime
) == SSA_NAME
)
4472 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
4478 /* Make no longer available leaders no longer available. */
4481 eliminate_dom_walker::after_dom_children (basic_block
)
4484 while ((entry
= el_avail_stack
.pop ()) != NULL_TREE
)
4485 el_avail
[SSA_NAME_VERSION (VN_INFO (entry
)->valnum
)] = NULL_TREE
;
4488 /* Eliminate fully redundant computations. */
4491 eliminate (bool do_pre
)
4493 gimple_stmt_iterator gsi
;
4496 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
4497 need_ab_cleanup
= BITMAP_ALLOC (NULL
);
4499 el_to_remove
.create (0);
4501 el_avail
.create (0);
4502 el_avail_stack
.create (0);
4504 eliminate_dom_walker (CDI_DOMINATORS
,
4505 do_pre
).walk (cfun
->cfg
->x_entry_block_ptr
);
4507 el_avail
.release ();
4508 el_avail_stack
.release ();
4510 /* We cannot remove stmts during BB walk, especially not release SSA
4511 names there as this confuses the VN machinery. The stmts ending
4512 up in el_to_remove are either stores or simple copies.
4513 Remove stmts in reverse order to make debug stmt creation possible. */
4514 while (!el_to_remove
.is_empty ())
4516 stmt
= el_to_remove
.pop ();
4518 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4520 fprintf (dump_file
, "Removing dead stmt ");
4521 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4525 if (gimple_code (stmt
) == GIMPLE_PHI
)
4526 lhs
= gimple_phi_result (stmt
);
4528 lhs
= gimple_get_lhs (stmt
);
4531 && TREE_CODE (lhs
) == SSA_NAME
)
4532 bitmap_clear_bit (inserted_exprs
, SSA_NAME_VERSION (lhs
));
4534 gsi
= gsi_for_stmt (stmt
);
4535 if (gimple_code (stmt
) == GIMPLE_PHI
)
4536 remove_phi_node (&gsi
, true);
4539 basic_block bb
= gimple_bb (stmt
);
4540 unlink_stmt_vdef (stmt
);
4541 if (gsi_remove (&gsi
, true))
4542 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
4543 release_defs (stmt
);
4546 /* Removing a stmt may expose a forwarder block. */
4547 el_todo
|= TODO_cleanup_cfg
;
4549 el_to_remove
.release ();
4554 /* Perform CFG cleanups made necessary by elimination. */
4557 fini_eliminate (void)
4559 bool do_eh_cleanup
= !bitmap_empty_p (need_eh_cleanup
);
4560 bool do_ab_cleanup
= !bitmap_empty_p (need_ab_cleanup
);
4563 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
4566 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup
);
4568 BITMAP_FREE (need_eh_cleanup
);
4569 BITMAP_FREE (need_ab_cleanup
);
4571 if (do_eh_cleanup
|| do_ab_cleanup
)
4572 return TODO_cleanup_cfg
;
4576 /* Borrow a bit of tree-ssa-dce.c for the moment.
4577 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4578 this may be a bit faster, and we may want critical edges kept split. */
4580 /* If OP's defining statement has not already been determined to be necessary,
4581 mark that statement necessary. Return the stmt, if it is newly
4584 static inline gimple
4585 mark_operand_necessary (tree op
)
4591 if (TREE_CODE (op
) != SSA_NAME
)
4594 stmt
= SSA_NAME_DEF_STMT (op
);
4597 if (gimple_plf (stmt
, NECESSARY
)
4598 || gimple_nop_p (stmt
))
4601 gimple_set_plf (stmt
, NECESSARY
, true);
4605 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4606 to insert PHI nodes sometimes, and because value numbering of casts isn't
4607 perfect, we sometimes end up inserting dead code. This simple DCE-like
4608 pass removes any insertions we made that weren't actually used. */
4611 remove_dead_inserted_code (void)
4618 worklist
= BITMAP_ALLOC (NULL
);
4619 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4621 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4622 if (gimple_plf (t
, NECESSARY
))
4623 bitmap_set_bit (worklist
, i
);
4625 while (!bitmap_empty_p (worklist
))
4627 i
= bitmap_first_set_bit (worklist
);
4628 bitmap_clear_bit (worklist
, i
);
4629 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4631 /* PHI nodes are somewhat special in that each PHI alternative has
4632 data and control dependencies. All the statements feeding the
4633 PHI node's arguments are always necessary. */
4634 if (gimple_code (t
) == GIMPLE_PHI
)
4638 for (k
= 0; k
< gimple_phi_num_args (t
); k
++)
4640 tree arg
= PHI_ARG_DEF (t
, k
);
4641 if (TREE_CODE (arg
) == SSA_NAME
)
4643 gimple n
= mark_operand_necessary (arg
);
4645 bitmap_set_bit (worklist
, SSA_NAME_VERSION (arg
));
4651 /* Propagate through the operands. Examine all the USE, VUSE and
4652 VDEF operands in this statement. Mark all the statements
4653 which feed this statement's uses as necessary. */
4657 /* The operands of VDEF expressions are also needed as they
4658 represent potential definitions that may reach this
4659 statement (VDEF operands allow us to follow def-def
4662 FOR_EACH_SSA_TREE_OPERAND (use
, t
, iter
, SSA_OP_ALL_USES
)
4664 gimple n
= mark_operand_necessary (use
);
4666 bitmap_set_bit (worklist
, SSA_NAME_VERSION (use
));
4671 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4673 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4674 if (!gimple_plf (t
, NECESSARY
))
4676 gimple_stmt_iterator gsi
;
4678 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4680 fprintf (dump_file
, "Removing unnecessary insertion:");
4681 print_gimple_stmt (dump_file
, t
, 0, 0);
4684 gsi
= gsi_for_stmt (t
);
4685 if (gimple_code (t
) == GIMPLE_PHI
)
4686 remove_phi_node (&gsi
, true);
4689 gsi_remove (&gsi
, true);
4694 BITMAP_FREE (worklist
);
4698 /* Initialize data structures used by PRE. */
4705 next_expression_id
= 1;
4706 expressions
.create (0);
4707 expressions
.safe_push (NULL
);
4708 value_expressions
.create (get_max_value_id () + 1);
4709 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
4710 name_to_id
.create (0);
4712 inserted_exprs
= BITMAP_ALLOC (NULL
);
4714 connect_infinite_loops_to_exit ();
4715 memset (&pre_stats
, 0, sizeof (pre_stats
));
4717 postorder
= XNEWVEC (int, n_basic_blocks_for_fn (cfun
));
4718 postorder_num
= inverted_post_order_compute (postorder
);
4720 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets
));
4722 calculate_dominance_info (CDI_POST_DOMINATORS
);
4723 calculate_dominance_info (CDI_DOMINATORS
);
4725 bitmap_obstack_initialize (&grand_bitmap_obstack
);
4726 phi_translate_table
= new hash_table
<expr_pred_trans_d
> (5110);
4727 expression_to_id
= new hash_table
<pre_expr_d
> (num_ssa_names
* 3);
4728 bitmap_set_pool
= create_alloc_pool ("Bitmap sets",
4729 sizeof (struct bitmap_set
), 30);
4730 pre_expr_pool
= create_alloc_pool ("pre_expr nodes",
4731 sizeof (struct pre_expr_d
), 30);
4732 FOR_ALL_BB_FN (bb
, cfun
)
4734 EXP_GEN (bb
) = bitmap_set_new ();
4735 PHI_GEN (bb
) = bitmap_set_new ();
4736 TMP_GEN (bb
) = bitmap_set_new ();
4737 AVAIL_OUT (bb
) = bitmap_set_new ();
4742 /* Deallocate data structures used by PRE. */
4748 value_expressions
.release ();
4749 BITMAP_FREE (inserted_exprs
);
4750 bitmap_obstack_release (&grand_bitmap_obstack
);
4751 free_alloc_pool (bitmap_set_pool
);
4752 free_alloc_pool (pre_expr_pool
);
4753 delete phi_translate_table
;
4754 phi_translate_table
= NULL
;
4755 delete expression_to_id
;
4756 expression_to_id
= NULL
;
4757 name_to_id
.release ();
4759 free_aux_for_blocks ();
4761 free_dominance_info (CDI_POST_DOMINATORS
);
4766 const pass_data pass_data_pre
=
4768 GIMPLE_PASS
, /* type */
4770 OPTGROUP_NONE
, /* optinfo_flags */
4771 TV_TREE_PRE
, /* tv_id */
4772 /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before
4774 ( PROP_no_crit_edges
| PROP_cfg
| PROP_ssa
), /* properties_required */
4775 0, /* properties_provided */
4776 PROP_no_crit_edges
, /* properties_destroyed */
4777 TODO_rebuild_alias
, /* todo_flags_start */
4778 0, /* todo_flags_finish */
4781 class pass_pre
: public gimple_opt_pass
4784 pass_pre (gcc::context
*ctxt
)
4785 : gimple_opt_pass (pass_data_pre
, ctxt
)
4788 /* opt_pass methods: */
4789 virtual bool gate (function
*) { return flag_tree_pre
!= 0; }
4790 virtual unsigned int execute (function
*);
4792 }; // class pass_pre
4795 pass_pre::execute (function
*fun
)
4797 unsigned int todo
= 0;
4799 do_partial_partial
=
4800 flag_tree_partial_pre
&& optimize_function_for_speed_p (fun
);
4802 /* This has to happen before SCCVN runs because
4803 loop_optimizer_init may create new phis, etc. */
4804 loop_optimizer_init (LOOPS_NORMAL
);
4806 if (!run_scc_vn (VN_WALK
))
4808 loop_optimizer_finalize ();
4815 /* Collect and value number expressions computed in each basic block. */
4818 /* Insert can get quite slow on an incredibly large number of basic
4819 blocks due to some quadratic behavior. Until this behavior is
4820 fixed, don't run it when he have an incredibly large number of
4821 bb's. If we aren't going to run insert, there is no point in
4822 computing ANTIC, either, even though it's plenty fast. */
4823 if (n_basic_blocks_for_fn (fun
) < 4000)
4829 /* Make sure to remove fake edges before committing our inserts.
4830 This makes sure we don't end up with extra critical edges that
4831 we would need to split. */
4832 remove_fake_exit_edges ();
4833 gsi_commit_edge_inserts ();
4835 /* Remove all the redundant expressions. */
4836 todo
|= eliminate (true);
4838 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
4839 statistics_counter_event (fun
, "PA inserted", pre_stats
.pa_insert
);
4840 statistics_counter_event (fun
, "New PHIs", pre_stats
.phis
);
4841 statistics_counter_event (fun
, "Eliminated", pre_stats
.eliminations
);
4843 clear_expression_ids ();
4844 remove_dead_inserted_code ();
4848 todo
|= fini_eliminate ();
4849 loop_optimizer_finalize ();
4851 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4852 case we can merge the block with the remaining predecessor of the block.
4854 - call merge_blocks after each tail merge iteration
4855 - call merge_blocks after all tail merge iterations
4856 - mark TODO_cleanup_cfg when necessary
4857 - share the cfg cleanup with fini_pre. */
4858 todo
|= tail_merge_optimize (todo
);
4862 /* Tail merging invalidates the virtual SSA web, together with
4863 cfg-cleanup opportunities exposed by PRE this will wreck the
4864 SSA updating machinery. So make sure to run update-ssa
4865 manually, before eventually scheduling cfg-cleanup as part of
4867 update_ssa (TODO_update_ssa_only_virtuals
);
4875 make_pass_pre (gcc::context
*ctxt
)
4877 return new pass_pre (ctxt
);
4882 const pass_data pass_data_fre
=
4884 GIMPLE_PASS
, /* type */
4886 OPTGROUP_NONE
, /* optinfo_flags */
4887 TV_TREE_FRE
, /* tv_id */
4888 ( PROP_cfg
| PROP_ssa
), /* properties_required */
4889 0, /* properties_provided */
4890 0, /* properties_destroyed */
4891 0, /* todo_flags_start */
4892 0, /* todo_flags_finish */
4895 class pass_fre
: public gimple_opt_pass
4898 pass_fre (gcc::context
*ctxt
)
4899 : gimple_opt_pass (pass_data_fre
, ctxt
)
4902 /* opt_pass methods: */
4903 opt_pass
* clone () { return new pass_fre (m_ctxt
); }
4904 virtual bool gate (function
*) { return flag_tree_fre
!= 0; }
4905 virtual unsigned int execute (function
*);
4907 }; // class pass_fre
4910 pass_fre::execute (function
*fun
)
4912 unsigned int todo
= 0;
4914 if (!run_scc_vn (VN_WALKREWRITE
))
4917 memset (&pre_stats
, 0, sizeof (pre_stats
));
4919 /* Remove all the redundant expressions. */
4920 todo
|= eliminate (false);
4922 todo
|= fini_eliminate ();
4926 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
4927 statistics_counter_event (fun
, "Eliminated", pre_stats
.eliminations
);
4935 make_pass_fre (gcc::context
*ctxt
)
4937 return new pass_fre (ctxt
);