2 Copyright (C) 2001-2015 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
29 #include "fold-const.h"
31 #include "hard-reg-set.h"
33 #include "dominance.h"
36 #include "basic-block.h"
37 #include "gimple-pretty-print.h"
38 #include "tree-inline.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-fold.h"
43 #include "gimple-expr.h"
46 #include "gimple-iterator.h"
47 #include "gimplify-me.h"
48 #include "gimple-ssa.h"
50 #include "tree-phinodes.h"
51 #include "ssa-iterators.h"
52 #include "stringpool.h"
53 #include "tree-ssanames.h"
54 #include "tree-ssa-loop.h"
55 #include "tree-into-ssa.h"
58 #include "insn-config.h"
69 #include "tree-iterator.h"
70 #include "alloc-pool.h"
72 #include "tree-pass.h"
73 #include "langhooks.h"
75 #include "tree-ssa-sccvn.h"
76 #include "tree-scalar-evolution.h"
80 #include "plugin-api.h"
83 #include "symbol-summary.h"
85 #include "tree-ssa-propagate.h"
86 #include "ipa-utils.h"
87 #include "tree-cfgcleanup.h"
91 1. Avail sets can be shared by making an avail_find_leader that
92 walks up the dominator tree and looks in those avail sets.
93 This might affect code optimality, it's unclear right now.
94 2. Strength reduction can be performed by anticipating expressions
95 we can repair later on.
96 3. We can do back-substitution or smarter value numbering to catch
97 commutative expressions split up over multiple statements.
100 /* For ease of terminology, "expression node" in the below refers to
101 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
102 represent the actual statement containing the expressions we care about,
103 and we cache the value number by putting it in the expression. */
107 First we walk the statements to generate the AVAIL sets, the
108 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
109 generation of values/expressions by a given block. We use them
110 when computing the ANTIC sets. The AVAIL sets consist of
111 SSA_NAME's that represent values, so we know what values are
112 available in what blocks. AVAIL is a forward dataflow problem. In
113 SSA, values are never killed, so we don't need a kill set, or a
114 fixpoint iteration, in order to calculate the AVAIL sets. In
115 traditional parlance, AVAIL sets tell us the downsafety of the
118 Next, we generate the ANTIC sets. These sets represent the
119 anticipatable expressions. ANTIC is a backwards dataflow
120 problem. An expression is anticipatable in a given block if it could
121 be generated in that block. This means that if we had to perform
122 an insertion in that block, of the value of that expression, we
123 could. Calculating the ANTIC sets requires phi translation of
124 expressions, because the flow goes backwards through phis. We must
125 iterate to a fixpoint of the ANTIC sets, because we have a kill
126 set. Even in SSA form, values are not live over the entire
127 function, only from their definition point onwards. So we have to
128 remove values from the ANTIC set once we go past the definition
129 point of the leaders that make them up.
130 compute_antic/compute_antic_aux performs this computation.
132 Third, we perform insertions to make partially redundant
133 expressions fully redundant.
135 An expression is partially redundant (excluding partial
138 1. It is AVAIL in some, but not all, of the predecessors of a
140 2. It is ANTIC in all the predecessors.
142 In order to make it fully redundant, we insert the expression into
143 the predecessors where it is not available, but is ANTIC.
145 For the partial anticipation case, we only perform insertion if it
146 is partially anticipated in some block, and fully available in all
149 insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
150 performs these steps.
152 Fourth, we eliminate fully redundant expressions.
153 This is a simple statement walk that replaces redundant
154 calculations with the now available values. */
156 /* Representations of value numbers:
158 Value numbers are represented by a representative SSA_NAME. We
159 will create fake SSA_NAME's in situations where we need a
160 representative but do not have one (because it is a complex
161 expression). In order to facilitate storing the value numbers in
162 bitmaps, and keep the number of wasted SSA_NAME's down, we also
163 associate a value_id with each value number, and create full blown
164 ssa_name's only where we actually need them (IE in operands of
165 existing expressions).
167 Theoretically you could replace all the value_id's with
168 SSA_NAME_VERSION, but this would allocate a large number of
169 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
170 It would also require an additional indirection at each point we
173 /* Representation of expressions on value numbers:
175 Expressions consisting of value numbers are represented the same
176 way as our VN internally represents them, with an additional
177 "pre_expr" wrapping around them in order to facilitate storing all
178 of the expressions in the same sets. */
180 /* Representation of sets:
182 The dataflow sets do not need to be sorted in any particular order
183 for the majority of their lifetime, are simply represented as two
184 bitmaps, one that keeps track of values present in the set, and one
185 that keeps track of expressions present in the set.
187 When we need them in topological order, we produce it on demand by
188 transforming the bitmap into an array and sorting it into topo
191 /* Type of expression, used to know which member of the PRE_EXPR union
202 typedef union pre_expr_union_d
207 vn_reference_t reference
;
210 typedef struct pre_expr_d
: typed_noop_remove
<pre_expr_d
>
212 enum pre_expr_kind kind
;
216 /* hash_table support. */
217 typedef pre_expr_d
*value_type
;
218 typedef pre_expr_d
*compare_type
;
219 static inline hashval_t
hash (const pre_expr_d
*);
220 static inline int equal (const pre_expr_d
*, const pre_expr_d
*);
223 #define PRE_EXPR_NAME(e) (e)->u.name
224 #define PRE_EXPR_NARY(e) (e)->u.nary
225 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
226 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
228 /* Compare E1 and E1 for equality. */
231 pre_expr_d::equal (const pre_expr_d
*e1
, const pre_expr_d
*e2
)
233 if (e1
->kind
!= e2
->kind
)
239 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1
),
240 PRE_EXPR_CONSTANT (e2
));
242 return PRE_EXPR_NAME (e1
) == PRE_EXPR_NAME (e2
);
244 return vn_nary_op_eq (PRE_EXPR_NARY (e1
), PRE_EXPR_NARY (e2
));
246 return vn_reference_eq (PRE_EXPR_REFERENCE (e1
),
247 PRE_EXPR_REFERENCE (e2
));
256 pre_expr_d::hash (const pre_expr_d
*e
)
261 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e
));
263 return SSA_NAME_VERSION (PRE_EXPR_NAME (e
));
265 return PRE_EXPR_NARY (e
)->hashcode
;
267 return PRE_EXPR_REFERENCE (e
)->hashcode
;
273 /* Next global expression id number. */
274 static unsigned int next_expression_id
;
276 /* Mapping from expression to id number we can use in bitmap sets. */
277 static vec
<pre_expr
> expressions
;
278 static hash_table
<pre_expr_d
> *expression_to_id
;
279 static vec
<unsigned> name_to_id
;
281 /* Allocate an expression id for EXPR. */
283 static inline unsigned int
284 alloc_expression_id (pre_expr expr
)
286 struct pre_expr_d
**slot
;
287 /* Make sure we won't overflow. */
288 gcc_assert (next_expression_id
+ 1 > next_expression_id
);
289 expr
->id
= next_expression_id
++;
290 expressions
.safe_push (expr
);
291 if (expr
->kind
== NAME
)
293 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
294 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
295 re-allocations by using vec::reserve upfront. */
296 unsigned old_len
= name_to_id
.length ();
297 name_to_id
.reserve (num_ssa_names
- old_len
);
298 name_to_id
.quick_grow_cleared (num_ssa_names
);
299 gcc_assert (name_to_id
[version
] == 0);
300 name_to_id
[version
] = expr
->id
;
304 slot
= expression_to_id
->find_slot (expr
, INSERT
);
308 return next_expression_id
- 1;
311 /* Return the expression id for tree EXPR. */
313 static inline unsigned int
314 get_expression_id (const pre_expr expr
)
319 static inline unsigned int
320 lookup_expression_id (const pre_expr expr
)
322 struct pre_expr_d
**slot
;
324 if (expr
->kind
== NAME
)
326 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
327 if (name_to_id
.length () <= version
)
329 return name_to_id
[version
];
333 slot
= expression_to_id
->find_slot (expr
, NO_INSERT
);
336 return ((pre_expr
)*slot
)->id
;
340 /* Return the existing expression id for EXPR, or create one if one
341 does not exist yet. */
343 static inline unsigned int
344 get_or_alloc_expression_id (pre_expr expr
)
346 unsigned int id
= lookup_expression_id (expr
);
348 return alloc_expression_id (expr
);
349 return expr
->id
= id
;
352 /* Return the expression that has expression id ID */
354 static inline pre_expr
355 expression_for_id (unsigned int id
)
357 return expressions
[id
];
360 /* Free the expression id field in all of our expressions,
361 and then destroy the expressions array. */
364 clear_expression_ids (void)
366 expressions
.release ();
369 static pool_allocator
<pre_expr_d
> pre_expr_pool ("pre_expr nodes", 30);
371 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
374 get_or_alloc_expr_for_name (tree name
)
376 struct pre_expr_d expr
;
378 unsigned int result_id
;
382 PRE_EXPR_NAME (&expr
) = name
;
383 result_id
= lookup_expression_id (&expr
);
385 return expression_for_id (result_id
);
387 result
= pre_expr_pool
.allocate ();
389 PRE_EXPR_NAME (result
) = name
;
390 alloc_expression_id (result
);
394 /* An unordered bitmap set. One bitmap tracks values, the other,
396 typedef struct bitmap_set
398 bitmap_head expressions
;
402 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
403 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
405 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
406 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
408 /* Mapping from value id to expressions with that value_id. */
409 static vec
<bitmap
> value_expressions
;
411 /* Sets that we need to keep track of. */
412 typedef struct bb_bitmap_sets
414 /* The EXP_GEN set, which represents expressions/values generated in
416 bitmap_set_t exp_gen
;
418 /* The PHI_GEN set, which represents PHI results generated in a
420 bitmap_set_t phi_gen
;
422 /* The TMP_GEN set, which represents results/temporaries generated
423 in a basic block. IE the LHS of an expression. */
424 bitmap_set_t tmp_gen
;
426 /* The AVAIL_OUT set, which represents which values are available in
427 a given basic block. */
428 bitmap_set_t avail_out
;
430 /* The ANTIC_IN set, which represents which values are anticipatable
431 in a given basic block. */
432 bitmap_set_t antic_in
;
434 /* The PA_IN set, which represents which values are
435 partially anticipatable in a given basic block. */
438 /* The NEW_SETS set, which is used during insertion to augment the
439 AVAIL_OUT set of blocks with the new insertions performed during
440 the current iteration. */
441 bitmap_set_t new_sets
;
443 /* A cache for value_dies_in_block_x. */
446 /* The live virtual operand on successor edges. */
449 /* True if we have visited this block during ANTIC calculation. */
450 unsigned int visited
: 1;
452 /* True when the block contains a call that might not return. */
453 unsigned int contains_may_not_return_call
: 1;
456 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
457 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
458 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
459 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
460 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
461 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
462 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
463 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
464 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
465 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
466 #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
469 /* Basic block list in postorder. */
470 static int *postorder
;
471 static int postorder_num
;
473 /* This structure is used to keep track of statistics on what
474 optimization PRE was able to perform. */
477 /* The number of RHS computations eliminated by PRE. */
480 /* The number of new expressions/temporaries generated by PRE. */
483 /* The number of inserts found due to partial anticipation */
486 /* The number of new PHI nodes added by PRE. */
490 static bool do_partial_partial
;
491 static pre_expr
bitmap_find_leader (bitmap_set_t
, unsigned int);
492 static void bitmap_value_insert_into_set (bitmap_set_t
, pre_expr
);
493 static void bitmap_value_replace_in_set (bitmap_set_t
, pre_expr
);
494 static void bitmap_set_copy (bitmap_set_t
, bitmap_set_t
);
495 static bool bitmap_set_contains_value (bitmap_set_t
, unsigned int);
496 static void bitmap_insert_into_set (bitmap_set_t
, pre_expr
);
497 static void bitmap_insert_into_set_1 (bitmap_set_t
, pre_expr
,
499 static bitmap_set_t
bitmap_set_new (void);
500 static tree
create_expression_by_pieces (basic_block
, pre_expr
, gimple_seq
*,
502 static tree
find_or_generate_expression (basic_block
, tree
, gimple_seq
*);
503 static unsigned int get_expr_value_id (pre_expr
);
505 /* We can add and remove elements and entries to and from sets
506 and hash tables, so we use alloc pools for them. */
508 static pool_allocator
<bitmap_set
> bitmap_set_pool ("Bitmap sets", 30);
509 static bitmap_obstack grand_bitmap_obstack
;
511 /* Set of blocks with statements that have had their EH properties changed. */
512 static bitmap need_eh_cleanup
;
514 /* Set of blocks with statements that have had their AB properties changed. */
515 static bitmap need_ab_cleanup
;
517 /* A three tuple {e, pred, v} used to cache phi translations in the
518 phi_translate_table. */
520 typedef struct expr_pred_trans_d
: typed_free_remove
<expr_pred_trans_d
>
522 /* The expression. */
525 /* The predecessor block along which we translated the expression. */
528 /* The value that resulted from the translation. */
531 /* The hashcode for the expression, pred pair. This is cached for
535 /* hash_table support. */
536 typedef expr_pred_trans_d
*value_type
;
537 typedef expr_pred_trans_d
*compare_type
;
538 static inline hashval_t
hash (const expr_pred_trans_d
*);
539 static inline int equal (const expr_pred_trans_d
*, const expr_pred_trans_d
*);
540 } *expr_pred_trans_t
;
541 typedef const struct expr_pred_trans_d
*const_expr_pred_trans_t
;
544 expr_pred_trans_d::hash (const expr_pred_trans_d
*e
)
550 expr_pred_trans_d::equal (const expr_pred_trans_d
*ve1
,
551 const expr_pred_trans_d
*ve2
)
553 basic_block b1
= ve1
->pred
;
554 basic_block b2
= ve2
->pred
;
556 /* If they are not translations for the same basic block, they can't
560 return pre_expr_d::equal (ve1
->e
, ve2
->e
);
563 /* The phi_translate_table caches phi translations for a given
564 expression and predecessor. */
565 static hash_table
<expr_pred_trans_d
> *phi_translate_table
;
567 /* Add the tuple mapping from {expression E, basic block PRED} to
568 the phi translation table and return whether it pre-existed. */
571 phi_trans_add (expr_pred_trans_t
*entry
, pre_expr e
, basic_block pred
)
573 expr_pred_trans_t
*slot
;
574 expr_pred_trans_d tem
;
575 hashval_t hash
= iterative_hash_hashval_t (pre_expr_d::hash (e
),
580 slot
= phi_translate_table
->find_slot_with_hash (&tem
, hash
, INSERT
);
587 *entry
= *slot
= XNEW (struct expr_pred_trans_d
);
589 (*entry
)->pred
= pred
;
590 (*entry
)->hashcode
= hash
;
595 /* Add expression E to the expression set of value id V. */
598 add_to_value (unsigned int v
, pre_expr e
)
602 gcc_checking_assert (get_expr_value_id (e
) == v
);
604 if (v
>= value_expressions
.length ())
606 value_expressions
.safe_grow_cleared (v
+ 1);
609 set
= value_expressions
[v
];
612 set
= BITMAP_ALLOC (&grand_bitmap_obstack
);
613 value_expressions
[v
] = set
;
616 bitmap_set_bit (set
, get_or_alloc_expression_id (e
));
619 /* Create a new bitmap set and return it. */
622 bitmap_set_new (void)
624 bitmap_set_t ret
= bitmap_set_pool
.allocate ();
625 bitmap_initialize (&ret
->expressions
, &grand_bitmap_obstack
);
626 bitmap_initialize (&ret
->values
, &grand_bitmap_obstack
);
630 /* Return the value id for a PRE expression EXPR. */
633 get_expr_value_id (pre_expr expr
)
639 id
= get_constant_value_id (PRE_EXPR_CONSTANT (expr
));
642 id
= VN_INFO (PRE_EXPR_NAME (expr
))->value_id
;
645 id
= PRE_EXPR_NARY (expr
)->value_id
;
648 id
= PRE_EXPR_REFERENCE (expr
)->value_id
;
653 /* ??? We cannot assert that expr has a value-id (it can be 0), because
654 we assign value-ids only to expressions that have a result
655 in set_hashtable_value_ids. */
659 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
662 sccvn_valnum_from_value_id (unsigned int val
)
666 bitmap exprset
= value_expressions
[val
];
667 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
669 pre_expr vexpr
= expression_for_id (i
);
670 if (vexpr
->kind
== NAME
)
671 return VN_INFO (PRE_EXPR_NAME (vexpr
))->valnum
;
672 else if (vexpr
->kind
== CONSTANT
)
673 return PRE_EXPR_CONSTANT (vexpr
);
678 /* Remove an expression EXPR from a bitmapped set. */
681 bitmap_remove_from_set (bitmap_set_t set
, pre_expr expr
)
683 unsigned int val
= get_expr_value_id (expr
);
684 if (!value_id_constant_p (val
))
686 bitmap_clear_bit (&set
->values
, val
);
687 bitmap_clear_bit (&set
->expressions
, get_expression_id (expr
));
692 bitmap_insert_into_set_1 (bitmap_set_t set
, pre_expr expr
,
693 unsigned int val
, bool allow_constants
)
695 if (allow_constants
|| !value_id_constant_p (val
))
697 /* We specifically expect this and only this function to be able to
698 insert constants into a set. */
699 bitmap_set_bit (&set
->values
, val
);
700 bitmap_set_bit (&set
->expressions
, get_or_alloc_expression_id (expr
));
704 /* Insert an expression EXPR into a bitmapped set. */
707 bitmap_insert_into_set (bitmap_set_t set
, pre_expr expr
)
709 bitmap_insert_into_set_1 (set
, expr
, get_expr_value_id (expr
), false);
712 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
715 bitmap_set_copy (bitmap_set_t dest
, bitmap_set_t orig
)
717 bitmap_copy (&dest
->expressions
, &orig
->expressions
);
718 bitmap_copy (&dest
->values
, &orig
->values
);
722 /* Free memory used up by SET. */
724 bitmap_set_free (bitmap_set_t set
)
726 bitmap_clear (&set
->expressions
);
727 bitmap_clear (&set
->values
);
731 /* Generate an topological-ordered array of bitmap set SET. */
734 sorted_array_from_bitmap_set (bitmap_set_t set
)
737 bitmap_iterator bi
, bj
;
738 vec
<pre_expr
> result
;
740 /* Pre-allocate enough space for the array. */
741 result
.create (bitmap_count_bits (&set
->expressions
));
743 FOR_EACH_VALUE_ID_IN_SET (set
, i
, bi
)
745 /* The number of expressions having a given value is usually
746 relatively small. Thus, rather than making a vector of all
747 the expressions and sorting it by value-id, we walk the values
748 and check in the reverse mapping that tells us what expressions
749 have a given value, to filter those in our set. As a result,
750 the expressions are inserted in value-id order, which means
753 If this is somehow a significant lose for some cases, we can
754 choose which set to walk based on the set size. */
755 bitmap exprset
= value_expressions
[i
];
756 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, j
, bj
)
758 if (bitmap_bit_p (&set
->expressions
, j
))
759 result
.quick_push (expression_for_id (j
));
766 /* Perform bitmapped set operation DEST &= ORIG. */
769 bitmap_set_and (bitmap_set_t dest
, bitmap_set_t orig
)
777 bitmap_initialize (&temp
, &grand_bitmap_obstack
);
779 bitmap_and_into (&dest
->values
, &orig
->values
);
780 bitmap_copy (&temp
, &dest
->expressions
);
781 EXECUTE_IF_SET_IN_BITMAP (&temp
, 0, i
, bi
)
783 pre_expr expr
= expression_for_id (i
);
784 unsigned int value_id
= get_expr_value_id (expr
);
785 if (!bitmap_bit_p (&dest
->values
, value_id
))
786 bitmap_clear_bit (&dest
->expressions
, i
);
788 bitmap_clear (&temp
);
792 /* Subtract all values and expressions contained in ORIG from DEST. */
795 bitmap_set_subtract (bitmap_set_t dest
, bitmap_set_t orig
)
797 bitmap_set_t result
= bitmap_set_new ();
801 bitmap_and_compl (&result
->expressions
, &dest
->expressions
,
804 FOR_EACH_EXPR_ID_IN_SET (result
, i
, bi
)
806 pre_expr expr
= expression_for_id (i
);
807 unsigned int value_id
= get_expr_value_id (expr
);
808 bitmap_set_bit (&result
->values
, value_id
);
814 /* Subtract all the values in bitmap set B from bitmap set A. */
817 bitmap_set_subtract_values (bitmap_set_t a
, bitmap_set_t b
)
823 bitmap_initialize (&temp
, &grand_bitmap_obstack
);
825 bitmap_copy (&temp
, &a
->expressions
);
826 EXECUTE_IF_SET_IN_BITMAP (&temp
, 0, i
, bi
)
828 pre_expr expr
= expression_for_id (i
);
829 if (bitmap_set_contains_value (b
, get_expr_value_id (expr
)))
830 bitmap_remove_from_set (a
, expr
);
832 bitmap_clear (&temp
);
836 /* Return true if bitmapped set SET contains the value VALUE_ID. */
839 bitmap_set_contains_value (bitmap_set_t set
, unsigned int value_id
)
841 if (value_id_constant_p (value_id
))
844 if (!set
|| bitmap_empty_p (&set
->expressions
))
847 return bitmap_bit_p (&set
->values
, value_id
);
851 bitmap_set_contains_expr (bitmap_set_t set
, const pre_expr expr
)
853 return bitmap_bit_p (&set
->expressions
, get_expression_id (expr
));
856 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
859 bitmap_set_replace_value (bitmap_set_t set
, unsigned int lookfor
,
866 if (value_id_constant_p (lookfor
))
869 if (!bitmap_set_contains_value (set
, lookfor
))
872 /* The number of expressions having a given value is usually
873 significantly less than the total number of expressions in SET.
874 Thus, rather than check, for each expression in SET, whether it
875 has the value LOOKFOR, we walk the reverse mapping that tells us
876 what expressions have a given value, and see if any of those
877 expressions are in our set. For large testcases, this is about
878 5-10x faster than walking the bitmap. If this is somehow a
879 significant lose for some cases, we can choose which set to walk
880 based on the set size. */
881 exprset
= value_expressions
[lookfor
];
882 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
884 if (bitmap_clear_bit (&set
->expressions
, i
))
886 bitmap_set_bit (&set
->expressions
, get_expression_id (expr
));
894 /* Return true if two bitmap sets are equal. */
897 bitmap_set_equal (bitmap_set_t a
, bitmap_set_t b
)
899 return bitmap_equal_p (&a
->values
, &b
->values
);
902 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
903 and add it otherwise. */
906 bitmap_value_replace_in_set (bitmap_set_t set
, pre_expr expr
)
908 unsigned int val
= get_expr_value_id (expr
);
910 if (bitmap_set_contains_value (set
, val
))
911 bitmap_set_replace_value (set
, val
, expr
);
913 bitmap_insert_into_set (set
, expr
);
916 /* Insert EXPR into SET if EXPR's value is not already present in
920 bitmap_value_insert_into_set (bitmap_set_t set
, pre_expr expr
)
922 unsigned int val
= get_expr_value_id (expr
);
924 gcc_checking_assert (expr
->id
== get_or_alloc_expression_id (expr
));
926 /* Constant values are always considered to be part of the set. */
927 if (value_id_constant_p (val
))
930 /* If the value membership changed, add the expression. */
931 if (bitmap_set_bit (&set
->values
, val
))
932 bitmap_set_bit (&set
->expressions
, expr
->id
);
935 /* Print out EXPR to outfile. */
938 print_pre_expr (FILE *outfile
, const pre_expr expr
)
943 print_generic_expr (outfile
, PRE_EXPR_CONSTANT (expr
), 0);
946 print_generic_expr (outfile
, PRE_EXPR_NAME (expr
), 0);
951 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
952 fprintf (outfile
, "{%s,", get_tree_code_name (nary
->opcode
));
953 for (i
= 0; i
< nary
->length
; i
++)
955 print_generic_expr (outfile
, nary
->op
[i
], 0);
956 if (i
!= (unsigned) nary
->length
- 1)
957 fprintf (outfile
, ",");
959 fprintf (outfile
, "}");
965 vn_reference_op_t vro
;
967 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
968 fprintf (outfile
, "{");
970 ref
->operands
.iterate (i
, &vro
);
973 bool closebrace
= false;
974 if (vro
->opcode
!= SSA_NAME
975 && TREE_CODE_CLASS (vro
->opcode
) != tcc_declaration
)
977 fprintf (outfile
, "%s", get_tree_code_name (vro
->opcode
));
980 fprintf (outfile
, "<");
986 print_generic_expr (outfile
, vro
->op0
, 0);
989 fprintf (outfile
, ",");
990 print_generic_expr (outfile
, vro
->op1
, 0);
994 fprintf (outfile
, ",");
995 print_generic_expr (outfile
, vro
->op2
, 0);
999 fprintf (outfile
, ">");
1000 if (i
!= ref
->operands
.length () - 1)
1001 fprintf (outfile
, ",");
1003 fprintf (outfile
, "}");
1006 fprintf (outfile
, "@");
1007 print_generic_expr (outfile
, ref
->vuse
, 0);
1013 void debug_pre_expr (pre_expr
);
1015 /* Like print_pre_expr but always prints to stderr. */
1017 debug_pre_expr (pre_expr e
)
1019 print_pre_expr (stderr
, e
);
1020 fprintf (stderr
, "\n");
1023 /* Print out SET to OUTFILE. */
1026 print_bitmap_set (FILE *outfile
, bitmap_set_t set
,
1027 const char *setname
, int blockindex
)
1029 fprintf (outfile
, "%s[%d] := { ", setname
, blockindex
);
1036 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
1038 const pre_expr expr
= expression_for_id (i
);
1041 fprintf (outfile
, ", ");
1043 print_pre_expr (outfile
, expr
);
1045 fprintf (outfile
, " (%04d)", get_expr_value_id (expr
));
1048 fprintf (outfile
, " }\n");
1051 void debug_bitmap_set (bitmap_set_t
);
1054 debug_bitmap_set (bitmap_set_t set
)
1056 print_bitmap_set (stderr
, set
, "debug", 0);
1059 void debug_bitmap_sets_for (basic_block
);
1062 debug_bitmap_sets_for (basic_block bb
)
1064 print_bitmap_set (stderr
, AVAIL_OUT (bb
), "avail_out", bb
->index
);
1065 print_bitmap_set (stderr
, EXP_GEN (bb
), "exp_gen", bb
->index
);
1066 print_bitmap_set (stderr
, PHI_GEN (bb
), "phi_gen", bb
->index
);
1067 print_bitmap_set (stderr
, TMP_GEN (bb
), "tmp_gen", bb
->index
);
1068 print_bitmap_set (stderr
, ANTIC_IN (bb
), "antic_in", bb
->index
);
1069 if (do_partial_partial
)
1070 print_bitmap_set (stderr
, PA_IN (bb
), "pa_in", bb
->index
);
1071 print_bitmap_set (stderr
, NEW_SETS (bb
), "new_sets", bb
->index
);
1074 /* Print out the expressions that have VAL to OUTFILE. */
1077 print_value_expressions (FILE *outfile
, unsigned int val
)
1079 bitmap set
= value_expressions
[val
];
1084 sprintf (s
, "%04d", val
);
1085 x
.expressions
= *set
;
1086 print_bitmap_set (outfile
, &x
, s
, 0);
1092 debug_value_expressions (unsigned int val
)
1094 print_value_expressions (stderr
, val
);
1097 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1101 get_or_alloc_expr_for_constant (tree constant
)
1103 unsigned int result_id
;
1104 unsigned int value_id
;
1105 struct pre_expr_d expr
;
1108 expr
.kind
= CONSTANT
;
1109 PRE_EXPR_CONSTANT (&expr
) = constant
;
1110 result_id
= lookup_expression_id (&expr
);
1112 return expression_for_id (result_id
);
1114 newexpr
= pre_expr_pool
.allocate ();
1115 newexpr
->kind
= CONSTANT
;
1116 PRE_EXPR_CONSTANT (newexpr
) = constant
;
1117 alloc_expression_id (newexpr
);
1118 value_id
= get_or_alloc_constant_value_id (constant
);
1119 add_to_value (value_id
, newexpr
);
1123 /* Given a value id V, find the actual tree representing the constant
1124 value if there is one, and return it. Return NULL if we can't find
1128 get_constant_for_value_id (unsigned int v
)
1130 if (value_id_constant_p (v
))
1134 bitmap exprset
= value_expressions
[v
];
1136 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1138 pre_expr expr
= expression_for_id (i
);
1139 if (expr
->kind
== CONSTANT
)
1140 return PRE_EXPR_CONSTANT (expr
);
1146 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1147 Currently only supports constants and SSA_NAMES. */
1149 get_or_alloc_expr_for (tree t
)
1151 if (TREE_CODE (t
) == SSA_NAME
)
1152 return get_or_alloc_expr_for_name (t
);
1153 else if (is_gimple_min_invariant (t
))
1154 return get_or_alloc_expr_for_constant (t
);
1157 /* More complex expressions can result from SCCVN expression
1158 simplification that inserts values for them. As they all
1159 do not have VOPs the get handled by the nary ops struct. */
1160 vn_nary_op_t result
;
1161 unsigned int result_id
;
1162 vn_nary_op_lookup (t
, &result
);
1165 pre_expr e
= pre_expr_pool
.allocate ();
1167 PRE_EXPR_NARY (e
) = result
;
1168 result_id
= lookup_expression_id (e
);
1171 pre_expr_pool
.remove (e
);
1172 e
= expression_for_id (result_id
);
1175 alloc_expression_id (e
);
1182 /* Return the folded version of T if T, when folded, is a gimple
1183 min_invariant. Otherwise, return T. */
1186 fully_constant_expression (pre_expr e
)
1194 vn_nary_op_t nary
= PRE_EXPR_NARY (e
);
1195 switch (TREE_CODE_CLASS (nary
->opcode
))
1198 case tcc_comparison
:
1200 /* We have to go from trees to pre exprs to value ids to
1202 tree naryop0
= nary
->op
[0];
1203 tree naryop1
= nary
->op
[1];
1205 if (!is_gimple_min_invariant (naryop0
))
1207 pre_expr rep0
= get_or_alloc_expr_for (naryop0
);
1208 unsigned int vrep0
= get_expr_value_id (rep0
);
1209 tree const0
= get_constant_for_value_id (vrep0
);
1211 naryop0
= fold_convert (TREE_TYPE (naryop0
), const0
);
1213 if (!is_gimple_min_invariant (naryop1
))
1215 pre_expr rep1
= get_or_alloc_expr_for (naryop1
);
1216 unsigned int vrep1
= get_expr_value_id (rep1
);
1217 tree const1
= get_constant_for_value_id (vrep1
);
1219 naryop1
= fold_convert (TREE_TYPE (naryop1
), const1
);
1221 result
= fold_binary (nary
->opcode
, nary
->type
,
1223 if (result
&& is_gimple_min_invariant (result
))
1224 return get_or_alloc_expr_for_constant (result
);
1225 /* We might have simplified the expression to a
1226 SSA_NAME for example from x_1 * 1. But we cannot
1227 insert a PHI for x_1 unconditionally as x_1 might
1228 not be available readily. */
1232 if (nary
->opcode
!= REALPART_EXPR
1233 && nary
->opcode
!= IMAGPART_EXPR
1234 && nary
->opcode
!= VIEW_CONVERT_EXPR
)
1239 /* We have to go from trees to pre exprs to value ids to
1241 tree naryop0
= nary
->op
[0];
1242 tree const0
, result
;
1243 if (is_gimple_min_invariant (naryop0
))
1247 pre_expr rep0
= get_or_alloc_expr_for (naryop0
);
1248 unsigned int vrep0
= get_expr_value_id (rep0
);
1249 const0
= get_constant_for_value_id (vrep0
);
1254 tree type1
= TREE_TYPE (nary
->op
[0]);
1255 const0
= fold_convert (type1
, const0
);
1256 result
= fold_unary (nary
->opcode
, nary
->type
, const0
);
1258 if (result
&& is_gimple_min_invariant (result
))
1259 return get_or_alloc_expr_for_constant (result
);
1268 vn_reference_t ref
= PRE_EXPR_REFERENCE (e
);
1270 if ((folded
= fully_constant_vn_reference_p (ref
)))
1271 return get_or_alloc_expr_for_constant (folded
);
1280 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1281 it has the value it would have in BLOCK. Set *SAME_VALID to true
1282 in case the new vuse doesn't change the value id of the OPERANDS. */
1285 translate_vuse_through_block (vec
<vn_reference_op_s
> operands
,
1286 alias_set_type set
, tree type
, tree vuse
,
1287 basic_block phiblock
,
1288 basic_block block
, bool *same_valid
)
1290 gimple phi
= SSA_NAME_DEF_STMT (vuse
);
1297 if (gimple_bb (phi
) != phiblock
)
1300 use_oracle
= ao_ref_init_from_vn_reference (&ref
, set
, type
, operands
);
1302 /* Use the alias-oracle to find either the PHI node in this block,
1303 the first VUSE used in this block that is equivalent to vuse or
1304 the first VUSE which definition in this block kills the value. */
1305 if (gimple_code (phi
) == GIMPLE_PHI
)
1306 e
= find_edge (block
, phiblock
);
1307 else if (use_oracle
)
1308 while (!stmt_may_clobber_ref_p_1 (phi
, &ref
))
1310 vuse
= gimple_vuse (phi
);
1311 phi
= SSA_NAME_DEF_STMT (vuse
);
1312 if (gimple_bb (phi
) != phiblock
)
1314 if (gimple_code (phi
) == GIMPLE_PHI
)
1316 e
= find_edge (block
, phiblock
);
1327 bitmap visited
= NULL
;
1329 /* Try to find a vuse that dominates this phi node by skipping
1330 non-clobbering statements. */
1331 vuse
= get_continuation_for_phi (phi
, &ref
, &cnt
, &visited
, false,
1334 BITMAP_FREE (visited
);
1340 /* If we didn't find any, the value ID can't stay the same,
1341 but return the translated vuse. */
1342 *same_valid
= false;
1343 vuse
= PHI_ARG_DEF (phi
, e
->dest_idx
);
1345 /* ??? We would like to return vuse here as this is the canonical
1346 upmost vdef that this reference is associated with. But during
1347 insertion of the references into the hash tables we only ever
1348 directly insert with their direct gimple_vuse, hence returning
1349 something else would make us not find the other expression. */
1350 return PHI_ARG_DEF (phi
, e
->dest_idx
);
1356 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1357 SET2. This is used to avoid making a set consisting of the union
1358 of PA_IN and ANTIC_IN during insert. */
1360 static inline pre_expr
1361 find_leader_in_sets (unsigned int val
, bitmap_set_t set1
, bitmap_set_t set2
)
1365 result
= bitmap_find_leader (set1
, val
);
1366 if (!result
&& set2
)
1367 result
= bitmap_find_leader (set2
, val
);
1371 /* Get the tree type for our PRE expression e. */
1374 get_expr_type (const pre_expr e
)
1379 return TREE_TYPE (PRE_EXPR_NAME (e
));
1381 return TREE_TYPE (PRE_EXPR_CONSTANT (e
));
1383 return PRE_EXPR_REFERENCE (e
)->type
;
1385 return PRE_EXPR_NARY (e
)->type
;
1390 /* Get a representative SSA_NAME for a given expression.
1391 Since all of our sub-expressions are treated as values, we require
1392 them to be SSA_NAME's for simplicity.
1393 Prior versions of GVNPRE used to use "value handles" here, so that
1394 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1395 either case, the operands are really values (IE we do not expect
1396 them to be usable without finding leaders). */
1399 get_representative_for (const pre_expr e
)
1402 unsigned int value_id
= get_expr_value_id (e
);
1407 return PRE_EXPR_NAME (e
);
1409 return PRE_EXPR_CONSTANT (e
);
1413 /* Go through all of the expressions representing this value
1414 and pick out an SSA_NAME. */
1417 bitmap exprs
= value_expressions
[value_id
];
1418 EXECUTE_IF_SET_IN_BITMAP (exprs
, 0, i
, bi
)
1420 pre_expr rep
= expression_for_id (i
);
1421 if (rep
->kind
== NAME
)
1422 return PRE_EXPR_NAME (rep
);
1423 else if (rep
->kind
== CONSTANT
)
1424 return PRE_EXPR_CONSTANT (rep
);
1430 /* If we reached here we couldn't find an SSA_NAME. This can
1431 happen when we've discovered a value that has never appeared in
1432 the program as set to an SSA_NAME, as the result of phi translation.
1434 ??? We should be able to re-use this when we insert the statement
1436 name
= make_temp_ssa_name (get_expr_type (e
), gimple_build_nop (), "pretmp");
1437 VN_INFO_GET (name
)->value_id
= value_id
;
1438 VN_INFO (name
)->valnum
= name
;
1439 /* ??? For now mark this SSA name for release by SCCVN. */
1440 VN_INFO (name
)->needs_insertion
= true;
1441 add_to_value (value_id
, get_or_alloc_expr_for_name (name
));
1442 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1444 fprintf (dump_file
, "Created SSA_NAME representative ");
1445 print_generic_expr (dump_file
, name
, 0);
1446 fprintf (dump_file
, " for expression:");
1447 print_pre_expr (dump_file
, e
);
1448 fprintf (dump_file
, " (%04d)\n", value_id
);
1457 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1458 basic_block pred
, basic_block phiblock
);
1460 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1461 the phis in PRED. Return NULL if we can't find a leader for each part
1462 of the translated expression. */
1465 phi_translate_1 (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1466 basic_block pred
, basic_block phiblock
)
1473 bool changed
= false;
1474 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1475 vn_nary_op_t newnary
= XALLOCAVAR (struct vn_nary_op_s
,
1476 sizeof_vn_nary_op (nary
->length
));
1477 memcpy (newnary
, nary
, sizeof_vn_nary_op (nary
->length
));
1479 for (i
= 0; i
< newnary
->length
; i
++)
1481 if (TREE_CODE (newnary
->op
[i
]) != SSA_NAME
)
1485 pre_expr leader
, result
;
1486 unsigned int op_val_id
= VN_INFO (newnary
->op
[i
])->value_id
;
1487 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1488 result
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1489 if (result
&& result
!= leader
)
1491 tree name
= get_representative_for (result
);
1494 newnary
->op
[i
] = name
;
1499 changed
|= newnary
->op
[i
] != nary
->op
[i
];
1505 unsigned int new_val_id
;
1507 tree result
= vn_nary_op_lookup_pieces (newnary
->length
,
1512 if (result
&& is_gimple_min_invariant (result
))
1513 return get_or_alloc_expr_for_constant (result
);
1515 expr
= pre_expr_pool
.allocate ();
1520 PRE_EXPR_NARY (expr
) = nary
;
1521 constant
= fully_constant_expression (expr
);
1522 if (constant
!= expr
)
1525 new_val_id
= nary
->value_id
;
1526 get_or_alloc_expression_id (expr
);
1530 new_val_id
= get_next_value_id ();
1531 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
1532 nary
= vn_nary_op_insert_pieces (newnary
->length
,
1536 result
, new_val_id
);
1537 PRE_EXPR_NARY (expr
) = nary
;
1538 constant
= fully_constant_expression (expr
);
1539 if (constant
!= expr
)
1541 get_or_alloc_expression_id (expr
);
1543 add_to_value (new_val_id
, expr
);
1551 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1552 vec
<vn_reference_op_s
> operands
= ref
->operands
;
1553 tree vuse
= ref
->vuse
;
1554 tree newvuse
= vuse
;
1555 vec
<vn_reference_op_s
> newoperands
= vNULL
;
1556 bool changed
= false, same_valid
= true;
1558 vn_reference_op_t operand
;
1559 vn_reference_t newref
;
1561 for (i
= 0; operands
.iterate (i
, &operand
); i
++)
1566 tree type
= operand
->type
;
1567 vn_reference_op_s newop
= *operand
;
1568 op
[0] = operand
->op0
;
1569 op
[1] = operand
->op1
;
1570 op
[2] = operand
->op2
;
1571 for (n
= 0; n
< 3; ++n
)
1573 unsigned int op_val_id
;
1576 if (TREE_CODE (op
[n
]) != SSA_NAME
)
1578 /* We can't possibly insert these. */
1580 && !is_gimple_min_invariant (op
[n
]))
1584 op_val_id
= VN_INFO (op
[n
])->value_id
;
1585 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1588 opresult
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1591 if (opresult
!= leader
)
1593 tree name
= get_representative_for (opresult
);
1596 changed
|= name
!= op
[n
];
1602 newoperands
.release ();
1607 if (!newoperands
.exists ())
1608 newoperands
= operands
.copy ();
1609 /* We may have changed from an SSA_NAME to a constant */
1610 if (newop
.opcode
== SSA_NAME
&& TREE_CODE (op
[0]) != SSA_NAME
)
1611 newop
.opcode
= TREE_CODE (op
[0]);
1616 newoperands
[i
] = newop
;
1618 gcc_checking_assert (i
== operands
.length ());
1622 newvuse
= translate_vuse_through_block (newoperands
.exists ()
1623 ? newoperands
: operands
,
1624 ref
->set
, ref
->type
,
1625 vuse
, phiblock
, pred
,
1627 if (newvuse
== NULL_TREE
)
1629 newoperands
.release ();
1634 if (changed
|| newvuse
!= vuse
)
1636 unsigned int new_val_id
;
1639 tree result
= vn_reference_lookup_pieces (newvuse
, ref
->set
,
1641 newoperands
.exists ()
1642 ? newoperands
: operands
,
1645 newoperands
.release ();
1647 /* We can always insert constants, so if we have a partial
1648 redundant constant load of another type try to translate it
1649 to a constant of appropriate type. */
1650 if (result
&& is_gimple_min_invariant (result
))
1653 if (!useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1655 tem
= fold_unary (VIEW_CONVERT_EXPR
, ref
->type
, result
);
1656 if (tem
&& !is_gimple_min_invariant (tem
))
1660 return get_or_alloc_expr_for_constant (tem
);
1663 /* If we'd have to convert things we would need to validate
1664 if we can insert the translated expression. So fail
1665 here for now - we cannot insert an alias with a different
1666 type in the VN tables either, as that would assert. */
1668 && !useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1670 else if (!result
&& newref
1671 && !useless_type_conversion_p (ref
->type
, newref
->type
))
1673 newoperands
.release ();
1677 expr
= pre_expr_pool
.allocate ();
1678 expr
->kind
= REFERENCE
;
1683 PRE_EXPR_REFERENCE (expr
) = newref
;
1684 constant
= fully_constant_expression (expr
);
1685 if (constant
!= expr
)
1688 new_val_id
= newref
->value_id
;
1689 get_or_alloc_expression_id (expr
);
1693 if (changed
|| !same_valid
)
1695 new_val_id
= get_next_value_id ();
1696 value_expressions
.safe_grow_cleared
1697 (get_max_value_id () + 1);
1700 new_val_id
= ref
->value_id
;
1701 if (!newoperands
.exists ())
1702 newoperands
= operands
.copy ();
1703 newref
= vn_reference_insert_pieces (newvuse
, ref
->set
,
1706 result
, new_val_id
);
1707 newoperands
= vNULL
;
1708 PRE_EXPR_REFERENCE (expr
) = newref
;
1709 constant
= fully_constant_expression (expr
);
1710 if (constant
!= expr
)
1712 get_or_alloc_expression_id (expr
);
1714 add_to_value (new_val_id
, expr
);
1716 newoperands
.release ();
1723 tree name
= PRE_EXPR_NAME (expr
);
1724 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
1725 /* If the SSA name is defined by a PHI node in this block,
1727 if (gimple_code (def_stmt
) == GIMPLE_PHI
1728 && gimple_bb (def_stmt
) == phiblock
)
1730 edge e
= find_edge (pred
, gimple_bb (def_stmt
));
1731 tree def
= PHI_ARG_DEF (def_stmt
, e
->dest_idx
);
1733 /* Handle constant. */
1734 if (is_gimple_min_invariant (def
))
1735 return get_or_alloc_expr_for_constant (def
);
1737 return get_or_alloc_expr_for_name (def
);
1739 /* Otherwise return it unchanged - it will get removed if its
1740 value is not available in PREDs AVAIL_OUT set of expressions
1741 by the subtraction of TMP_GEN. */
1750 /* Wrapper around phi_translate_1 providing caching functionality. */
1753 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1754 basic_block pred
, basic_block phiblock
)
1756 expr_pred_trans_t slot
= NULL
;
1762 /* Constants contain no values that need translation. */
1763 if (expr
->kind
== CONSTANT
)
1766 if (value_id_constant_p (get_expr_value_id (expr
)))
1769 /* Don't add translations of NAMEs as those are cheap to translate. */
1770 if (expr
->kind
!= NAME
)
1772 if (phi_trans_add (&slot
, expr
, pred
))
1774 /* Store NULL for the value we want to return in the case of
1780 phitrans
= phi_translate_1 (expr
, set1
, set2
, pred
, phiblock
);
1787 /* Remove failed translations again, they cause insert
1788 iteration to not pick up new opportunities reliably. */
1789 phi_translate_table
->remove_elt_with_hash (slot
, slot
->hashcode
);
1796 /* For each expression in SET, translate the values through phi nodes
1797 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1798 expressions in DEST. */
1801 phi_translate_set (bitmap_set_t dest
, bitmap_set_t set
, basic_block pred
,
1802 basic_block phiblock
)
1804 vec
<pre_expr
> exprs
;
1808 if (gimple_seq_empty_p (phi_nodes (phiblock
)))
1810 bitmap_set_copy (dest
, set
);
1814 exprs
= sorted_array_from_bitmap_set (set
);
1815 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
1817 pre_expr translated
;
1818 translated
= phi_translate (expr
, set
, NULL
, pred
, phiblock
);
1822 /* We might end up with multiple expressions from SET being
1823 translated to the same value. In this case we do not want
1824 to retain the NARY or REFERENCE expression but prefer a NAME
1825 which would be the leader. */
1826 if (translated
->kind
== NAME
)
1827 bitmap_value_replace_in_set (dest
, translated
);
1829 bitmap_value_insert_into_set (dest
, translated
);
1834 /* Find the leader for a value (i.e., the name representing that
1835 value) in a given set, and return it. Return NULL if no leader
1839 bitmap_find_leader (bitmap_set_t set
, unsigned int val
)
1841 if (value_id_constant_p (val
))
1845 bitmap exprset
= value_expressions
[val
];
1847 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1849 pre_expr expr
= expression_for_id (i
);
1850 if (expr
->kind
== CONSTANT
)
1854 if (bitmap_set_contains_value (set
, val
))
1856 /* Rather than walk the entire bitmap of expressions, and see
1857 whether any of them has the value we are looking for, we look
1858 at the reverse mapping, which tells us the set of expressions
1859 that have a given value (IE value->expressions with that
1860 value) and see if any of those expressions are in our set.
1861 The number of expressions per value is usually significantly
1862 less than the number of expressions in the set. In fact, for
1863 large testcases, doing it this way is roughly 5-10x faster
1864 than walking the bitmap.
1865 If this is somehow a significant lose for some cases, we can
1866 choose which set to walk based on which set is smaller. */
1869 bitmap exprset
= value_expressions
[val
];
1871 EXECUTE_IF_AND_IN_BITMAP (exprset
, &set
->expressions
, 0, i
, bi
)
1872 return expression_for_id (i
);
1877 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1878 BLOCK by seeing if it is not killed in the block. Note that we are
1879 only determining whether there is a store that kills it. Because
1880 of the order in which clean iterates over values, we are guaranteed
1881 that altered operands will have caused us to be eliminated from the
1882 ANTIC_IN set already. */
1885 value_dies_in_block_x (pre_expr expr
, basic_block block
)
1887 tree vuse
= PRE_EXPR_REFERENCE (expr
)->vuse
;
1888 vn_reference_t refx
= PRE_EXPR_REFERENCE (expr
);
1890 gimple_stmt_iterator gsi
;
1891 unsigned id
= get_expression_id (expr
);
1898 /* Lookup a previously calculated result. */
1899 if (EXPR_DIES (block
)
1900 && bitmap_bit_p (EXPR_DIES (block
), id
* 2))
1901 return bitmap_bit_p (EXPR_DIES (block
), id
* 2 + 1);
1903 /* A memory expression {e, VUSE} dies in the block if there is a
1904 statement that may clobber e. If, starting statement walk from the
1905 top of the basic block, a statement uses VUSE there can be no kill
1906 inbetween that use and the original statement that loaded {e, VUSE},
1907 so we can stop walking. */
1908 ref
.base
= NULL_TREE
;
1909 for (gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1911 tree def_vuse
, def_vdef
;
1912 def
= gsi_stmt (gsi
);
1913 def_vuse
= gimple_vuse (def
);
1914 def_vdef
= gimple_vdef (def
);
1916 /* Not a memory statement. */
1920 /* Not a may-def. */
1923 /* A load with the same VUSE, we're done. */
1924 if (def_vuse
== vuse
)
1930 /* Init ref only if we really need it. */
1931 if (ref
.base
== NULL_TREE
1932 && !ao_ref_init_from_vn_reference (&ref
, refx
->set
, refx
->type
,
1938 /* If the statement may clobber expr, it dies. */
1939 if (stmt_may_clobber_ref_p_1 (def
, &ref
))
1946 /* Remember the result. */
1947 if (!EXPR_DIES (block
))
1948 EXPR_DIES (block
) = BITMAP_ALLOC (&grand_bitmap_obstack
);
1949 bitmap_set_bit (EXPR_DIES (block
), id
* 2);
1951 bitmap_set_bit (EXPR_DIES (block
), id
* 2 + 1);
1957 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1958 contains its value-id. */
1961 op_valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, tree op
)
1963 if (op
&& TREE_CODE (op
) == SSA_NAME
)
1965 unsigned int value_id
= VN_INFO (op
)->value_id
;
1966 if (!(bitmap_set_contains_value (set1
, value_id
)
1967 || (set2
&& bitmap_set_contains_value (set2
, value_id
))))
1973 /* Determine if the expression EXPR is valid in SET1 U SET2.
1974 ONLY SET2 CAN BE NULL.
1975 This means that we have a leader for each part of the expression
1976 (if it consists of values), or the expression is an SSA_NAME.
1977 For loads/calls, we also see if the vuse is killed in this block. */
1980 valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, pre_expr expr
)
1985 /* By construction all NAMEs are available. Non-available
1986 NAMEs are removed by subtracting TMP_GEN from the sets. */
1991 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1992 for (i
= 0; i
< nary
->length
; i
++)
1993 if (!op_valid_in_sets (set1
, set2
, nary
->op
[i
]))
2000 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2001 vn_reference_op_t vro
;
2004 FOR_EACH_VEC_ELT (ref
->operands
, i
, vro
)
2006 if (!op_valid_in_sets (set1
, set2
, vro
->op0
)
2007 || !op_valid_in_sets (set1
, set2
, vro
->op1
)
2008 || !op_valid_in_sets (set1
, set2
, vro
->op2
))
2018 /* Clean the set of expressions that are no longer valid in SET1 or
2019 SET2. This means expressions that are made up of values we have no
2020 leaders for in SET1 or SET2. This version is used for partial
2021 anticipation, which means it is not valid in either ANTIC_IN or
2025 dependent_clean (bitmap_set_t set1
, bitmap_set_t set2
)
2027 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set1
);
2031 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2033 if (!valid_in_sets (set1
, set2
, expr
))
2034 bitmap_remove_from_set (set1
, expr
);
2039 /* Clean the set of expressions that are no longer valid in SET. This
2040 means expressions that are made up of values we have no leaders for
2044 clean (bitmap_set_t set
)
2046 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set
);
2050 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2052 if (!valid_in_sets (set
, NULL
, expr
))
2053 bitmap_remove_from_set (set
, expr
);
2058 /* Clean the set of expressions that are no longer valid in SET because
2059 they are clobbered in BLOCK or because they trap and may not be executed. */
2062 prune_clobbered_mems (bitmap_set_t set
, basic_block block
)
2067 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
2069 pre_expr expr
= expression_for_id (i
);
2070 if (expr
->kind
== REFERENCE
)
2072 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2075 gimple def_stmt
= SSA_NAME_DEF_STMT (ref
->vuse
);
2076 if (!gimple_nop_p (def_stmt
)
2077 && ((gimple_bb (def_stmt
) != block
2078 && !dominated_by_p (CDI_DOMINATORS
,
2079 block
, gimple_bb (def_stmt
)))
2080 || (gimple_bb (def_stmt
) == block
2081 && value_dies_in_block_x (expr
, block
))))
2082 bitmap_remove_from_set (set
, expr
);
2085 else if (expr
->kind
== NARY
)
2087 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2088 /* If the NARY may trap make sure the block does not contain
2089 a possible exit point.
2090 ??? This is overly conservative if we translate AVAIL_OUT
2091 as the available expression might be after the exit point. */
2092 if (BB_MAY_NOTRETURN (block
)
2093 && vn_nary_may_trap (nary
))
2094 bitmap_remove_from_set (set
, expr
);
2099 static sbitmap has_abnormal_preds
;
2101 /* List of blocks that may have changed during ANTIC computation and
2102 thus need to be iterated over. */
2104 static sbitmap changed_blocks
;
2106 /* Compute the ANTIC set for BLOCK.
2108 If succs(BLOCK) > 1 then
2109 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2110 else if succs(BLOCK) == 1 then
2111 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2113 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2117 compute_antic_aux (basic_block block
, bool block_has_abnormal_pred_edge
)
2119 bool changed
= false;
2120 bitmap_set_t S
, old
, ANTIC_OUT
;
2126 old
= ANTIC_OUT
= S
= NULL
;
2127 BB_VISITED (block
) = 1;
2129 /* If any edges from predecessors are abnormal, antic_in is empty,
2131 if (block_has_abnormal_pred_edge
)
2132 goto maybe_dump_sets
;
2134 old
= ANTIC_IN (block
);
2135 ANTIC_OUT
= bitmap_set_new ();
2137 /* If the block has no successors, ANTIC_OUT is empty. */
2138 if (EDGE_COUNT (block
->succs
) == 0)
2140 /* If we have one successor, we could have some phi nodes to
2141 translate through. */
2142 else if (single_succ_p (block
))
2144 basic_block succ_bb
= single_succ (block
);
2145 gcc_assert (BB_VISITED (succ_bb
));
2146 phi_translate_set (ANTIC_OUT
, ANTIC_IN (succ_bb
), block
, succ_bb
);
2148 /* If we have multiple successors, we take the intersection of all of
2149 them. Note that in the case of loop exit phi nodes, we may have
2150 phis to translate through. */
2154 basic_block bprime
, first
= NULL
;
2156 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2157 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2160 && BB_VISITED (e
->dest
))
2162 else if (BB_VISITED (e
->dest
))
2163 worklist
.quick_push (e
->dest
);
2166 /* Of multiple successors we have to have visited one already
2167 which is guaranteed by iteration order. */
2168 gcc_assert (first
!= NULL
);
2170 phi_translate_set (ANTIC_OUT
, ANTIC_IN (first
), block
, first
);
2172 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2174 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2176 bitmap_set_t tmp
= bitmap_set_new ();
2177 phi_translate_set (tmp
, ANTIC_IN (bprime
), block
, bprime
);
2178 bitmap_set_and (ANTIC_OUT
, tmp
);
2179 bitmap_set_free (tmp
);
2182 bitmap_set_and (ANTIC_OUT
, ANTIC_IN (bprime
));
2186 /* Prune expressions that are clobbered in block and thus become
2187 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2188 prune_clobbered_mems (ANTIC_OUT
, block
);
2190 /* Generate ANTIC_OUT - TMP_GEN. */
2191 S
= bitmap_set_subtract (ANTIC_OUT
, TMP_GEN (block
));
2193 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2194 ANTIC_IN (block
) = bitmap_set_subtract (EXP_GEN (block
),
2197 /* Then union in the ANTIC_OUT - TMP_GEN values,
2198 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2199 FOR_EACH_EXPR_ID_IN_SET (S
, bii
, bi
)
2200 bitmap_value_insert_into_set (ANTIC_IN (block
),
2201 expression_for_id (bii
));
2203 clean (ANTIC_IN (block
));
2205 if (!bitmap_set_equal (old
, ANTIC_IN (block
)))
2208 bitmap_set_bit (changed_blocks
, block
->index
);
2209 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2210 bitmap_set_bit (changed_blocks
, e
->src
->index
);
2213 bitmap_clear_bit (changed_blocks
, block
->index
);
2216 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2219 print_bitmap_set (dump_file
, ANTIC_OUT
, "ANTIC_OUT", block
->index
);
2221 print_bitmap_set (dump_file
, ANTIC_IN (block
), "ANTIC_IN",
2225 print_bitmap_set (dump_file
, S
, "S", block
->index
);
2228 bitmap_set_free (old
);
2230 bitmap_set_free (S
);
2232 bitmap_set_free (ANTIC_OUT
);
2236 /* Compute PARTIAL_ANTIC for BLOCK.
2238 If succs(BLOCK) > 1 then
2239 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2240 in ANTIC_OUT for all succ(BLOCK)
2241 else if succs(BLOCK) == 1 then
2242 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2244 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2249 compute_partial_antic_aux (basic_block block
,
2250 bool block_has_abnormal_pred_edge
)
2252 bool changed
= false;
2253 bitmap_set_t old_PA_IN
;
2254 bitmap_set_t PA_OUT
;
2257 unsigned long max_pa
= PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH
);
2259 old_PA_IN
= PA_OUT
= NULL
;
2261 /* If any edges from predecessors are abnormal, antic_in is empty,
2263 if (block_has_abnormal_pred_edge
)
2264 goto maybe_dump_sets
;
2266 /* If there are too many partially anticipatable values in the
2267 block, phi_translate_set can take an exponential time: stop
2268 before the translation starts. */
2270 && single_succ_p (block
)
2271 && bitmap_count_bits (&PA_IN (single_succ (block
))->values
) > max_pa
)
2272 goto maybe_dump_sets
;
2274 old_PA_IN
= PA_IN (block
);
2275 PA_OUT
= bitmap_set_new ();
2277 /* If the block has no successors, ANTIC_OUT is empty. */
2278 if (EDGE_COUNT (block
->succs
) == 0)
2280 /* If we have one successor, we could have some phi nodes to
2281 translate through. Note that we can't phi translate across DFS
2282 back edges in partial antic, because it uses a union operation on
2283 the successors. For recurrences like IV's, we will end up
2284 generating a new value in the set on each go around (i + 3 (VH.1)
2285 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2286 else if (single_succ_p (block
))
2288 basic_block succ
= single_succ (block
);
2289 if (!(single_succ_edge (block
)->flags
& EDGE_DFS_BACK
))
2290 phi_translate_set (PA_OUT
, PA_IN (succ
), block
, succ
);
2292 /* If we have multiple successors, we take the union of all of
2299 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2300 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2302 if (e
->flags
& EDGE_DFS_BACK
)
2304 worklist
.quick_push (e
->dest
);
2306 if (worklist
.length () > 0)
2308 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2313 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime
), i
, bi
)
2314 bitmap_value_insert_into_set (PA_OUT
,
2315 expression_for_id (i
));
2316 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2318 bitmap_set_t pa_in
= bitmap_set_new ();
2319 phi_translate_set (pa_in
, PA_IN (bprime
), block
, bprime
);
2320 FOR_EACH_EXPR_ID_IN_SET (pa_in
, i
, bi
)
2321 bitmap_value_insert_into_set (PA_OUT
,
2322 expression_for_id (i
));
2323 bitmap_set_free (pa_in
);
2326 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime
), i
, bi
)
2327 bitmap_value_insert_into_set (PA_OUT
,
2328 expression_for_id (i
));
2333 /* Prune expressions that are clobbered in block and thus become
2334 invalid if translated from PA_OUT to PA_IN. */
2335 prune_clobbered_mems (PA_OUT
, block
);
2337 /* PA_IN starts with PA_OUT - TMP_GEN.
2338 Then we subtract things from ANTIC_IN. */
2339 PA_IN (block
) = bitmap_set_subtract (PA_OUT
, TMP_GEN (block
));
2341 /* For partial antic, we want to put back in the phi results, since
2342 we will properly avoid making them partially antic over backedges. */
2343 bitmap_ior_into (&PA_IN (block
)->values
, &PHI_GEN (block
)->values
);
2344 bitmap_ior_into (&PA_IN (block
)->expressions
, &PHI_GEN (block
)->expressions
);
2346 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2347 bitmap_set_subtract_values (PA_IN (block
), ANTIC_IN (block
));
2349 dependent_clean (PA_IN (block
), ANTIC_IN (block
));
2351 if (!bitmap_set_equal (old_PA_IN
, PA_IN (block
)))
2354 bitmap_set_bit (changed_blocks
, block
->index
);
2355 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2356 bitmap_set_bit (changed_blocks
, e
->src
->index
);
2359 bitmap_clear_bit (changed_blocks
, block
->index
);
2362 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2365 print_bitmap_set (dump_file
, PA_OUT
, "PA_OUT", block
->index
);
2367 print_bitmap_set (dump_file
, PA_IN (block
), "PA_IN", block
->index
);
2370 bitmap_set_free (old_PA_IN
);
2372 bitmap_set_free (PA_OUT
);
2376 /* Compute ANTIC and partial ANTIC sets. */
2379 compute_antic (void)
2381 bool changed
= true;
2382 int num_iterations
= 0;
2386 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2387 We pre-build the map of blocks with incoming abnormal edges here. */
2388 has_abnormal_preds
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
2389 bitmap_clear (has_abnormal_preds
);
2391 FOR_ALL_BB_FN (block
, cfun
)
2396 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2398 e
->flags
&= ~EDGE_DFS_BACK
;
2399 if (e
->flags
& EDGE_ABNORMAL
)
2401 bitmap_set_bit (has_abnormal_preds
, block
->index
);
2406 BB_VISITED (block
) = 0;
2408 /* While we are here, give empty ANTIC_IN sets to each block. */
2409 ANTIC_IN (block
) = bitmap_set_new ();
2410 PA_IN (block
) = bitmap_set_new ();
2413 /* At the exit block we anticipate nothing. */
2414 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun
)) = 1;
2416 changed_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
) + 1);
2417 bitmap_ones (changed_blocks
);
2420 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2421 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2422 /* ??? We need to clear our PHI translation cache here as the
2423 ANTIC sets shrink and we restrict valid translations to
2424 those having operands with leaders in ANTIC. Same below
2425 for PA ANTIC computation. */
2428 for (i
= postorder_num
- 1; i
>= 0; i
--)
2430 if (bitmap_bit_p (changed_blocks
, postorder
[i
]))
2432 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2433 changed
|= compute_antic_aux (block
,
2434 bitmap_bit_p (has_abnormal_preds
,
2438 /* Theoretically possible, but *highly* unlikely. */
2439 gcc_checking_assert (num_iterations
< 500);
2442 statistics_histogram_event (cfun
, "compute_antic iterations",
2445 if (do_partial_partial
)
2447 bitmap_ones (changed_blocks
);
2448 mark_dfs_back_edges ();
2453 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2454 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2457 for (i
= postorder_num
- 1 ; i
>= 0; i
--)
2459 if (bitmap_bit_p (changed_blocks
, postorder
[i
]))
2461 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2463 |= compute_partial_antic_aux (block
,
2464 bitmap_bit_p (has_abnormal_preds
,
2468 /* Theoretically possible, but *highly* unlikely. */
2469 gcc_checking_assert (num_iterations
< 500);
2471 statistics_histogram_event (cfun
, "compute_partial_antic iterations",
2474 sbitmap_free (has_abnormal_preds
);
2475 sbitmap_free (changed_blocks
);
2479 /* Inserted expressions are placed onto this worklist, which is used
2480 for performing quick dead code elimination of insertions we made
2481 that didn't turn out to be necessary. */
2482 static bitmap inserted_exprs
;
2484 /* The actual worker for create_component_ref_by_pieces. */
2487 create_component_ref_by_pieces_1 (basic_block block
, vn_reference_t ref
,
2488 unsigned int *operand
, gimple_seq
*stmts
)
2490 vn_reference_op_t currop
= &ref
->operands
[*operand
];
2493 switch (currop
->opcode
)
2497 tree folded
, sc
= NULL_TREE
;
2498 unsigned int nargs
= 0;
2500 if (TREE_CODE (currop
->op0
) == FUNCTION_DECL
)
2503 fn
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2508 sc
= find_or_generate_expression (block
, currop
->op1
, stmts
);
2512 args
= XNEWVEC (tree
, ref
->operands
.length () - 1);
2513 while (*operand
< ref
->operands
.length ())
2515 args
[nargs
] = create_component_ref_by_pieces_1 (block
, ref
,
2521 folded
= build_call_array (currop
->type
,
2522 (TREE_CODE (fn
) == FUNCTION_DECL
2523 ? build_fold_addr_expr (fn
) : fn
),
2525 if (currop
->with_bounds
)
2526 CALL_WITH_BOUNDS_P (folded
) = true;
2529 CALL_EXPR_STATIC_CHAIN (folded
) = sc
;
2535 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2539 tree offset
= currop
->op0
;
2540 if (TREE_CODE (baseop
) == ADDR_EXPR
2541 && handled_component_p (TREE_OPERAND (baseop
, 0)))
2545 base
= get_addr_base_and_unit_offset (TREE_OPERAND (baseop
, 0),
2548 offset
= int_const_binop (PLUS_EXPR
, offset
,
2549 build_int_cst (TREE_TYPE (offset
),
2551 baseop
= build_fold_addr_expr (base
);
2553 return fold_build2 (MEM_REF
, currop
->type
, baseop
, offset
);
2556 case TARGET_MEM_REF
:
2558 tree genop0
= NULL_TREE
, genop1
= NULL_TREE
;
2559 vn_reference_op_t nextop
= &ref
->operands
[++*operand
];
2560 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2566 genop0
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2572 genop1
= find_or_generate_expression (block
, nextop
->op0
, stmts
);
2576 return build5 (TARGET_MEM_REF
, currop
->type
,
2577 baseop
, currop
->op2
, genop0
, currop
->op1
, genop1
);
2583 gcc_assert (is_gimple_min_invariant (currop
->op0
));
2589 case VIEW_CONVERT_EXPR
:
2591 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2595 return fold_build1 (currop
->opcode
, currop
->type
, genop0
);
2598 case WITH_SIZE_EXPR
:
2600 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2604 tree genop1
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2607 return fold_build2 (currop
->opcode
, currop
->type
, genop0
, genop1
);
2612 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2616 tree op1
= currop
->op0
;
2617 tree op2
= currop
->op1
;
2618 return fold_build3 (BIT_FIELD_REF
, currop
->type
, genop0
, op1
, op2
);
2621 /* For array ref vn_reference_op's, operand 1 of the array ref
2622 is op0 of the reference op and operand 3 of the array ref is
2624 case ARRAY_RANGE_REF
:
2628 tree genop1
= currop
->op0
;
2629 tree genop2
= currop
->op1
;
2630 tree genop3
= currop
->op2
;
2631 genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2635 genop1
= find_or_generate_expression (block
, genop1
, stmts
);
2640 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (genop0
));
2641 /* Drop zero minimum index if redundant. */
2642 if (integer_zerop (genop2
)
2644 || integer_zerop (TYPE_MIN_VALUE (domain_type
))))
2648 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2655 tree elmt_type
= TREE_TYPE (TREE_TYPE (genop0
));
2656 /* We can't always put a size in units of the element alignment
2657 here as the element alignment may be not visible. See
2658 PR43783. Simply drop the element size for constant
2660 if (tree_int_cst_equal (genop3
, TYPE_SIZE_UNIT (elmt_type
)))
2664 genop3
= size_binop (EXACT_DIV_EXPR
, genop3
,
2665 size_int (TYPE_ALIGN_UNIT (elmt_type
)));
2666 genop3
= find_or_generate_expression (block
, genop3
, stmts
);
2671 return build4 (currop
->opcode
, currop
->type
, genop0
, genop1
,
2678 tree genop2
= currop
->op1
;
2679 op0
= create_component_ref_by_pieces_1 (block
, ref
, operand
, stmts
);
2682 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2686 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2690 return fold_build3 (COMPONENT_REF
, TREE_TYPE (op1
), op0
, op1
, genop2
);
2695 genop
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2716 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2717 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2718 trying to rename aggregates into ssa form directly, which is a no no.
2720 Thus, this routine doesn't create temporaries, it just builds a
2721 single access expression for the array, calling
2722 find_or_generate_expression to build the innermost pieces.
2724 This function is a subroutine of create_expression_by_pieces, and
2725 should not be called on it's own unless you really know what you
2729 create_component_ref_by_pieces (basic_block block
, vn_reference_t ref
,
2732 unsigned int op
= 0;
2733 return create_component_ref_by_pieces_1 (block
, ref
, &op
, stmts
);
2736 /* Find a simple leader for an expression, or generate one using
2737 create_expression_by_pieces from a NARY expression for the value.
2738 BLOCK is the basic_block we are looking for leaders in.
2739 OP is the tree expression to find a leader for or generate.
2740 Returns the leader or NULL_TREE on failure. */
2743 find_or_generate_expression (basic_block block
, tree op
, gimple_seq
*stmts
)
2745 pre_expr expr
= get_or_alloc_expr_for (op
);
2746 unsigned int lookfor
= get_expr_value_id (expr
);
2747 pre_expr leader
= bitmap_find_leader (AVAIL_OUT (block
), lookfor
);
2750 if (leader
->kind
== NAME
)
2751 return PRE_EXPR_NAME (leader
);
2752 else if (leader
->kind
== CONSTANT
)
2753 return PRE_EXPR_CONSTANT (leader
);
2759 /* It must be a complex expression, so generate it recursively. Note
2760 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2761 where the insert algorithm fails to insert a required expression. */
2762 bitmap exprset
= value_expressions
[lookfor
];
2765 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
2767 pre_expr temp
= expression_for_id (i
);
2768 /* We cannot insert random REFERENCE expressions at arbitrary
2769 places. We can insert NARYs which eventually re-materializes
2770 its operand values. */
2771 if (temp
->kind
== NARY
)
2772 return create_expression_by_pieces (block
, temp
, stmts
,
2773 get_expr_type (expr
));
2780 #define NECESSARY GF_PLF_1
2782 /* Create an expression in pieces, so that we can handle very complex
2783 expressions that may be ANTIC, but not necessary GIMPLE.
2784 BLOCK is the basic block the expression will be inserted into,
2785 EXPR is the expression to insert (in value form)
2786 STMTS is a statement list to append the necessary insertions into.
2788 This function will die if we hit some value that shouldn't be
2789 ANTIC but is (IE there is no leader for it, or its components).
2790 The function returns NULL_TREE in case a different antic expression
2791 has to be inserted first.
2792 This function may also generate expressions that are themselves
2793 partially or fully redundant. Those that are will be either made
2794 fully redundant during the next iteration of insert (for partially
2795 redundant ones), or eliminated by eliminate (for fully redundant
2799 create_expression_by_pieces (basic_block block
, pre_expr expr
,
2800 gimple_seq
*stmts
, tree type
)
2804 gimple_seq forced_stmts
= NULL
;
2805 unsigned int value_id
;
2806 gimple_stmt_iterator gsi
;
2807 tree exprtype
= type
? type
: get_expr_type (expr
);
2813 /* We may hit the NAME/CONSTANT case if we have to convert types
2814 that value numbering saw through. */
2816 folded
= PRE_EXPR_NAME (expr
);
2819 folded
= PRE_EXPR_CONSTANT (expr
);
2823 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2824 folded
= create_component_ref_by_pieces (block
, ref
, stmts
);
2831 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2832 tree
*genop
= XALLOCAVEC (tree
, nary
->length
);
2834 for (i
= 0; i
< nary
->length
; ++i
)
2836 genop
[i
] = find_or_generate_expression (block
, nary
->op
[i
], stmts
);
2839 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2840 may have conversions stripped. */
2841 if (nary
->opcode
== POINTER_PLUS_EXPR
)
2844 genop
[i
] = gimple_convert (&forced_stmts
,
2845 nary
->type
, genop
[i
]);
2847 genop
[i
] = gimple_convert (&forced_stmts
,
2848 sizetype
, genop
[i
]);
2851 genop
[i
] = gimple_convert (&forced_stmts
,
2852 TREE_TYPE (nary
->op
[i
]), genop
[i
]);
2854 if (nary
->opcode
== CONSTRUCTOR
)
2856 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
2857 for (i
= 0; i
< nary
->length
; ++i
)
2858 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
, genop
[i
]);
2859 folded
= build_constructor (nary
->type
, elts
);
2863 switch (nary
->length
)
2866 folded
= fold_build1 (nary
->opcode
, nary
->type
,
2870 folded
= fold_build2 (nary
->opcode
, nary
->type
,
2871 genop
[0], genop
[1]);
2874 folded
= fold_build3 (nary
->opcode
, nary
->type
,
2875 genop
[0], genop
[1], genop
[2]);
2887 if (!useless_type_conversion_p (exprtype
, TREE_TYPE (folded
)))
2888 folded
= fold_convert (exprtype
, folded
);
2890 /* Force the generated expression to be a sequence of GIMPLE
2892 We have to call unshare_expr because force_gimple_operand may
2893 modify the tree we pass to it. */
2894 gimple_seq tem
= NULL
;
2895 folded
= force_gimple_operand (unshare_expr (folded
), &tem
,
2897 gimple_seq_add_seq_without_update (&forced_stmts
, tem
);
2899 /* If we have any intermediate expressions to the value sets, add them
2900 to the value sets and chain them in the instruction stream. */
2903 gsi
= gsi_start (forced_stmts
);
2904 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
2906 gimple stmt
= gsi_stmt (gsi
);
2907 tree forcedname
= gimple_get_lhs (stmt
);
2910 if (TREE_CODE (forcedname
) == SSA_NAME
)
2912 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (forcedname
));
2913 VN_INFO_GET (forcedname
)->valnum
= forcedname
;
2914 VN_INFO (forcedname
)->value_id
= get_next_value_id ();
2915 nameexpr
= get_or_alloc_expr_for_name (forcedname
);
2916 add_to_value (VN_INFO (forcedname
)->value_id
, nameexpr
);
2917 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2918 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2921 gimple_set_vuse (stmt
, BB_LIVE_VOP_ON_EXIT (block
));
2922 gimple_set_modified (stmt
, true);
2924 gimple_seq_add_seq (stmts
, forced_stmts
);
2927 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2928 newstmt
= gimple_build_assign (name
, folded
);
2929 gimple_set_vuse (newstmt
, BB_LIVE_VOP_ON_EXIT (block
));
2930 gimple_set_modified (newstmt
, true);
2931 gimple_set_plf (newstmt
, NECESSARY
, false);
2933 gimple_seq_add_stmt (stmts
, newstmt
);
2934 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (name
));
2936 /* Fold the last statement. */
2937 gsi
= gsi_last (*stmts
);
2938 if (fold_stmt_inplace (&gsi
))
2939 update_stmt (gsi_stmt (gsi
));
2941 /* Add a value number to the temporary.
2942 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2943 we are creating the expression by pieces, and this particular piece of
2944 the expression may have been represented. There is no harm in replacing
2946 value_id
= get_expr_value_id (expr
);
2947 VN_INFO_GET (name
)->value_id
= value_id
;
2948 VN_INFO (name
)->valnum
= sccvn_valnum_from_value_id (value_id
);
2949 if (VN_INFO (name
)->valnum
== NULL_TREE
)
2950 VN_INFO (name
)->valnum
= name
;
2951 gcc_assert (VN_INFO (name
)->valnum
!= NULL_TREE
);
2952 nameexpr
= get_or_alloc_expr_for_name (name
);
2953 add_to_value (value_id
, nameexpr
);
2954 if (NEW_SETS (block
))
2955 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2956 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2958 pre_stats
.insertions
++;
2959 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2961 fprintf (dump_file
, "Inserted ");
2962 print_gimple_stmt (dump_file
, newstmt
, 0, 0);
2963 fprintf (dump_file
, " in predecessor %d (%04d)\n",
2964 block
->index
, value_id
);
2971 /* Insert the to-be-made-available values of expression EXPRNUM for each
2972 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2973 merge the result with a phi node, given the same value number as
2974 NODE. Return true if we have inserted new stuff. */
2977 insert_into_preds_of_block (basic_block block
, unsigned int exprnum
,
2978 vec
<pre_expr
> avail
)
2980 pre_expr expr
= expression_for_id (exprnum
);
2982 unsigned int val
= get_expr_value_id (expr
);
2984 bool insertions
= false;
2989 tree type
= get_expr_type (expr
);
2993 /* Make sure we aren't creating an induction variable. */
2994 if (bb_loop_depth (block
) > 0 && EDGE_COUNT (block
->preds
) == 2)
2996 bool firstinsideloop
= false;
2997 bool secondinsideloop
= false;
2998 firstinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
2999 EDGE_PRED (block
, 0)->src
);
3000 secondinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3001 EDGE_PRED (block
, 1)->src
);
3002 /* Induction variables only have one edge inside the loop. */
3003 if ((firstinsideloop
^ secondinsideloop
)
3004 && expr
->kind
!= REFERENCE
)
3006 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3007 fprintf (dump_file
, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3012 /* Make the necessary insertions. */
3013 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3015 gimple_seq stmts
= NULL
;
3018 eprime
= avail
[pred
->dest_idx
];
3020 if (eprime
->kind
!= NAME
&& eprime
->kind
!= CONSTANT
)
3022 builtexpr
= create_expression_by_pieces (bprime
, eprime
,
3024 gcc_assert (!(pred
->flags
& EDGE_ABNORMAL
));
3025 gsi_insert_seq_on_edge (pred
, stmts
);
3028 /* We cannot insert a PHI node if we failed to insert
3033 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_name (builtexpr
);
3036 else if (eprime
->kind
== CONSTANT
)
3038 /* Constants may not have the right type, fold_convert
3039 should give us back a constant with the right type. */
3040 tree constant
= PRE_EXPR_CONSTANT (eprime
);
3041 if (!useless_type_conversion_p (type
, TREE_TYPE (constant
)))
3043 tree builtexpr
= fold_convert (type
, constant
);
3044 if (!is_gimple_min_invariant (builtexpr
))
3046 tree forcedexpr
= force_gimple_operand (builtexpr
,
3049 if (!is_gimple_min_invariant (forcedexpr
))
3051 if (forcedexpr
!= builtexpr
)
3053 VN_INFO_GET (forcedexpr
)->valnum
= PRE_EXPR_CONSTANT (eprime
);
3054 VN_INFO (forcedexpr
)->value_id
= get_expr_value_id (eprime
);
3058 gimple_stmt_iterator gsi
;
3059 gsi
= gsi_start (stmts
);
3060 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
3062 gimple stmt
= gsi_stmt (gsi
);
3063 tree lhs
= gimple_get_lhs (stmt
);
3064 if (TREE_CODE (lhs
) == SSA_NAME
)
3065 bitmap_set_bit (inserted_exprs
,
3066 SSA_NAME_VERSION (lhs
));
3067 gimple_set_plf (stmt
, NECESSARY
, false);
3069 gsi_insert_seq_on_edge (pred
, stmts
);
3071 avail
[pred
->dest_idx
]
3072 = get_or_alloc_expr_for_name (forcedexpr
);
3076 avail
[pred
->dest_idx
]
3077 = get_or_alloc_expr_for_constant (builtexpr
);
3080 else if (eprime
->kind
== NAME
)
3082 /* We may have to do a conversion because our value
3083 numbering can look through types in certain cases, but
3084 our IL requires all operands of a phi node have the same
3086 tree name
= PRE_EXPR_NAME (eprime
);
3087 if (!useless_type_conversion_p (type
, TREE_TYPE (name
)))
3091 builtexpr
= fold_convert (type
, name
);
3092 forcedexpr
= force_gimple_operand (builtexpr
,
3096 if (forcedexpr
!= name
)
3098 VN_INFO_GET (forcedexpr
)->valnum
= VN_INFO (name
)->valnum
;
3099 VN_INFO (forcedexpr
)->value_id
= VN_INFO (name
)->value_id
;
3104 gimple_stmt_iterator gsi
;
3105 gsi
= gsi_start (stmts
);
3106 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
3108 gimple stmt
= gsi_stmt (gsi
);
3109 tree lhs
= gimple_get_lhs (stmt
);
3110 if (TREE_CODE (lhs
) == SSA_NAME
)
3111 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (lhs
));
3112 gimple_set_plf (stmt
, NECESSARY
, false);
3114 gsi_insert_seq_on_edge (pred
, stmts
);
3116 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_name (forcedexpr
);
3120 /* If we didn't want a phi node, and we made insertions, we still have
3121 inserted new stuff, and thus return true. If we didn't want a phi node,
3122 and didn't make insertions, we haven't added anything new, so return
3124 if (nophi
&& insertions
)
3126 else if (nophi
&& !insertions
)
3129 /* Now build a phi for the new variable. */
3130 temp
= make_temp_ssa_name (type
, NULL
, "prephitmp");
3131 phi
= create_phi_node (temp
, block
);
3133 gimple_set_plf (phi
, NECESSARY
, false);
3134 VN_INFO_GET (temp
)->value_id
= val
;
3135 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3136 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3137 VN_INFO (temp
)->valnum
= temp
;
3138 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3139 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3141 pre_expr ae
= avail
[pred
->dest_idx
];
3142 gcc_assert (get_expr_type (ae
) == type
3143 || useless_type_conversion_p (type
, get_expr_type (ae
)));
3144 if (ae
->kind
== CONSTANT
)
3145 add_phi_arg (phi
, unshare_expr (PRE_EXPR_CONSTANT (ae
)),
3146 pred
, UNKNOWN_LOCATION
);
3148 add_phi_arg (phi
, PRE_EXPR_NAME (ae
), pred
, UNKNOWN_LOCATION
);
3151 newphi
= get_or_alloc_expr_for_name (temp
);
3152 add_to_value (val
, newphi
);
3154 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3155 this insertion, since we test for the existence of this value in PHI_GEN
3156 before proceeding with the partial redundancy checks in insert_aux.
3158 The value may exist in AVAIL_OUT, in particular, it could be represented
3159 by the expression we are trying to eliminate, in which case we want the
3160 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3163 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3164 this block, because if it did, it would have existed in our dominator's
3165 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3168 bitmap_insert_into_set (PHI_GEN (block
), newphi
);
3169 bitmap_value_replace_in_set (AVAIL_OUT (block
),
3171 bitmap_insert_into_set (NEW_SETS (block
),
3174 /* If we insert a PHI node for a conversion of another PHI node
3175 in the same basic-block try to preserve range information.
3176 This is important so that followup loop passes receive optimal
3177 number of iteration analysis results. See PR61743. */
3178 if (expr
->kind
== NARY
3179 && CONVERT_EXPR_CODE_P (expr
->u
.nary
->opcode
)
3180 && TREE_CODE (expr
->u
.nary
->op
[0]) == SSA_NAME
3181 && gimple_bb (SSA_NAME_DEF_STMT (expr
->u
.nary
->op
[0])) == block
3182 && INTEGRAL_TYPE_P (type
)
3183 && INTEGRAL_TYPE_P (TREE_TYPE (expr
->u
.nary
->op
[0]))
3184 && (TYPE_PRECISION (type
)
3185 >= TYPE_PRECISION (TREE_TYPE (expr
->u
.nary
->op
[0])))
3186 && SSA_NAME_RANGE_INFO (expr
->u
.nary
->op
[0]))
3189 if (get_range_info (expr
->u
.nary
->op
[0], &min
, &max
) == VR_RANGE
3190 && !wi::neg_p (min
, SIGNED
)
3191 && !wi::neg_p (max
, SIGNED
))
3192 /* Just handle extension and sign-changes of all-positive ranges. */
3193 set_range_info (temp
,
3194 SSA_NAME_RANGE_TYPE (expr
->u
.nary
->op
[0]),
3195 wide_int_storage::from (min
, TYPE_PRECISION (type
),
3197 wide_int_storage::from (max
, TYPE_PRECISION (type
),
3201 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3203 fprintf (dump_file
, "Created phi ");
3204 print_gimple_stmt (dump_file
, phi
, 0, 0);
3205 fprintf (dump_file
, " in block %d (%04d)\n", block
->index
, val
);
3213 /* Perform insertion of partially redundant values.
3214 For BLOCK, do the following:
3215 1. Propagate the NEW_SETS of the dominator into the current block.
3216 If the block has multiple predecessors,
3217 2a. Iterate over the ANTIC expressions for the block to see if
3218 any of them are partially redundant.
3219 2b. If so, insert them into the necessary predecessors to make
3220 the expression fully redundant.
3221 2c. Insert a new PHI merging the values of the predecessors.
3222 2d. Insert the new PHI, and the new expressions, into the
3224 3. Recursively call ourselves on the dominator children of BLOCK.
3226 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
3227 do_regular_insertion and do_partial_insertion.
3232 do_regular_insertion (basic_block block
, basic_block dom
)
3234 bool new_stuff
= false;
3235 vec
<pre_expr
> exprs
;
3237 auto_vec
<pre_expr
> avail
;
3240 exprs
= sorted_array_from_bitmap_set (ANTIC_IN (block
));
3241 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3243 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3245 if (expr
->kind
== NARY
3246 || expr
->kind
== REFERENCE
)
3249 bool by_some
= false;
3250 bool cant_insert
= false;
3251 bool all_same
= true;
3252 pre_expr first_s
= NULL
;
3255 pre_expr eprime
= NULL
;
3257 pre_expr edoubleprime
= NULL
;
3258 bool do_insertion
= false;
3260 val
= get_expr_value_id (expr
);
3261 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3263 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3265 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3267 fprintf (dump_file
, "Found fully redundant value: ");
3268 print_pre_expr (dump_file
, expr
);
3269 fprintf (dump_file
, "\n");
3274 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3276 unsigned int vprime
;
3278 /* We should never run insertion for the exit block
3279 and so not come across fake pred edges. */
3280 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3282 eprime
= phi_translate (expr
, ANTIC_IN (block
), NULL
,
3285 /* eprime will generally only be NULL if the
3286 value of the expression, translated
3287 through the PHI for this predecessor, is
3288 undefined. If that is the case, we can't
3289 make the expression fully redundant,
3290 because its value is undefined along a
3291 predecessor path. We can thus break out
3292 early because it doesn't matter what the
3293 rest of the results are. */
3296 avail
[pred
->dest_idx
] = NULL
;
3301 eprime
= fully_constant_expression (eprime
);
3302 vprime
= get_expr_value_id (eprime
);
3303 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
),
3305 if (edoubleprime
== NULL
)
3307 avail
[pred
->dest_idx
] = eprime
;
3312 avail
[pred
->dest_idx
] = edoubleprime
;
3314 /* We want to perform insertions to remove a redundancy on
3315 a path in the CFG we want to optimize for speed. */
3316 if (optimize_edge_for_speed_p (pred
))
3317 do_insertion
= true;
3318 if (first_s
== NULL
)
3319 first_s
= edoubleprime
;
3320 else if (!pre_expr_d::equal (first_s
, edoubleprime
))
3324 /* If we can insert it, it's not the same value
3325 already existing along every predecessor, and
3326 it's defined by some predecessor, it is
3327 partially redundant. */
3328 if (!cant_insert
&& !all_same
&& by_some
)
3332 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3334 fprintf (dump_file
, "Skipping partial redundancy for "
3336 print_pre_expr (dump_file
, expr
);
3337 fprintf (dump_file
, " (%04d), no redundancy on to be "
3338 "optimized for speed edge\n", val
);
3341 else if (dbg_cnt (treepre_insert
))
3343 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3345 fprintf (dump_file
, "Found partial redundancy for "
3347 print_pre_expr (dump_file
, expr
);
3348 fprintf (dump_file
, " (%04d)\n",
3349 get_expr_value_id (expr
));
3351 if (insert_into_preds_of_block (block
,
3352 get_expression_id (expr
),
3357 /* If all edges produce the same value and that value is
3358 an invariant, then the PHI has the same value on all
3359 edges. Note this. */
3360 else if (!cant_insert
&& all_same
)
3362 gcc_assert (edoubleprime
->kind
== CONSTANT
3363 || edoubleprime
->kind
== NAME
);
3365 tree temp
= make_temp_ssa_name (get_expr_type (expr
),
3368 = gimple_build_assign (temp
,
3369 edoubleprime
->kind
== CONSTANT
?
3370 PRE_EXPR_CONSTANT (edoubleprime
) :
3371 PRE_EXPR_NAME (edoubleprime
));
3372 gimple_stmt_iterator gsi
= gsi_after_labels (block
);
3373 gsi_insert_before (&gsi
, assign
, GSI_NEW_STMT
);
3375 gimple_set_plf (assign
, NECESSARY
, false);
3376 VN_INFO_GET (temp
)->value_id
= val
;
3377 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3378 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3379 VN_INFO (temp
)->valnum
= temp
;
3380 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3381 pre_expr newe
= get_or_alloc_expr_for_name (temp
);
3382 add_to_value (val
, newe
);
3383 bitmap_value_replace_in_set (AVAIL_OUT (block
), newe
);
3384 bitmap_insert_into_set (NEW_SETS (block
), newe
);
3394 /* Perform insertion for partially anticipatable expressions. There
3395 is only one case we will perform insertion for these. This case is
3396 if the expression is partially anticipatable, and fully available.
3397 In this case, we know that putting it earlier will enable us to
3398 remove the later computation. */
3402 do_partial_partial_insertion (basic_block block
, basic_block dom
)
3404 bool new_stuff
= false;
3405 vec
<pre_expr
> exprs
;
3407 auto_vec
<pre_expr
> avail
;
3410 exprs
= sorted_array_from_bitmap_set (PA_IN (block
));
3411 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3413 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3415 if (expr
->kind
== NARY
3416 || expr
->kind
== REFERENCE
)
3420 bool cant_insert
= false;
3423 pre_expr eprime
= NULL
;
3426 val
= get_expr_value_id (expr
);
3427 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3429 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3432 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3434 unsigned int vprime
;
3435 pre_expr edoubleprime
;
3437 /* We should never run insertion for the exit block
3438 and so not come across fake pred edges. */
3439 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3441 eprime
= phi_translate (expr
, ANTIC_IN (block
),
3445 /* eprime will generally only be NULL if the
3446 value of the expression, translated
3447 through the PHI for this predecessor, is
3448 undefined. If that is the case, we can't
3449 make the expression fully redundant,
3450 because its value is undefined along a
3451 predecessor path. We can thus break out
3452 early because it doesn't matter what the
3453 rest of the results are. */
3456 avail
[pred
->dest_idx
] = NULL
;
3461 eprime
= fully_constant_expression (eprime
);
3462 vprime
= get_expr_value_id (eprime
);
3463 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
), vprime
);
3464 avail
[pred
->dest_idx
] = edoubleprime
;
3465 if (edoubleprime
== NULL
)
3472 /* If we can insert it, it's not the same value
3473 already existing along every predecessor, and
3474 it's defined by some predecessor, it is
3475 partially redundant. */
3476 if (!cant_insert
&& by_all
)
3479 bool do_insertion
= false;
3481 /* Insert only if we can remove a later expression on a path
3482 that we want to optimize for speed.
3483 The phi node that we will be inserting in BLOCK is not free,
3484 and inserting it for the sake of !optimize_for_speed successor
3485 may cause regressions on the speed path. */
3486 FOR_EACH_EDGE (succ
, ei
, block
->succs
)
3488 if (bitmap_set_contains_value (PA_IN (succ
->dest
), val
)
3489 || bitmap_set_contains_value (ANTIC_IN (succ
->dest
), val
))
3491 if (optimize_edge_for_speed_p (succ
))
3492 do_insertion
= true;
3498 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3500 fprintf (dump_file
, "Skipping partial partial redundancy "
3502 print_pre_expr (dump_file
, expr
);
3503 fprintf (dump_file
, " (%04d), not (partially) anticipated "
3504 "on any to be optimized for speed edges\n", val
);
3507 else if (dbg_cnt (treepre_insert
))
3509 pre_stats
.pa_insert
++;
3510 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3512 fprintf (dump_file
, "Found partial partial redundancy "
3514 print_pre_expr (dump_file
, expr
);
3515 fprintf (dump_file
, " (%04d)\n",
3516 get_expr_value_id (expr
));
3518 if (insert_into_preds_of_block (block
,
3519 get_expression_id (expr
),
3532 insert_aux (basic_block block
)
3535 bool new_stuff
= false;
3540 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3545 bitmap_set_t newset
= NEW_SETS (dom
);
3548 /* Note that we need to value_replace both NEW_SETS, and
3549 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3550 represented by some non-simple expression here that we want
3551 to replace it with. */
3552 FOR_EACH_EXPR_ID_IN_SET (newset
, i
, bi
)
3554 pre_expr expr
= expression_for_id (i
);
3555 bitmap_value_replace_in_set (NEW_SETS (block
), expr
);
3556 bitmap_value_replace_in_set (AVAIL_OUT (block
), expr
);
3559 if (!single_pred_p (block
))
3561 new_stuff
|= do_regular_insertion (block
, dom
);
3562 if (do_partial_partial
)
3563 new_stuff
|= do_partial_partial_insertion (block
, dom
);
3567 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
3569 son
= next_dom_son (CDI_DOMINATORS
, son
))
3571 new_stuff
|= insert_aux (son
);
3577 /* Perform insertion of partially redundant values. */
3582 bool new_stuff
= true;
3584 int num_iterations
= 0;
3586 FOR_ALL_BB_FN (bb
, cfun
)
3587 NEW_SETS (bb
) = bitmap_set_new ();
3592 if (dump_file
&& dump_flags
& TDF_DETAILS
)
3593 fprintf (dump_file
, "Starting insert iteration %d\n", num_iterations
);
3594 new_stuff
= insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3596 /* Clear the NEW sets before the next iteration. We have already
3597 fully propagated its contents. */
3599 FOR_ALL_BB_FN (bb
, cfun
)
3600 bitmap_set_free (NEW_SETS (bb
));
3602 statistics_histogram_event (cfun
, "insert iterations", num_iterations
);
3606 /* Compute the AVAIL set for all basic blocks.
3608 This function performs value numbering of the statements in each basic
3609 block. The AVAIL sets are built from information we glean while doing
3610 this value numbering, since the AVAIL sets contain only one entry per
3613 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3614 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3617 compute_avail (void)
3620 basic_block block
, son
;
3621 basic_block
*worklist
;
3625 /* We pretend that default definitions are defined in the entry block.
3626 This includes function arguments and the static chain decl. */
3627 for (i
= 1; i
< num_ssa_names
; ++i
)
3629 tree name
= ssa_name (i
);
3632 || !SSA_NAME_IS_DEFAULT_DEF (name
)
3633 || has_zero_uses (name
)
3634 || virtual_operand_p (name
))
3637 e
= get_or_alloc_expr_for_name (name
);
3638 add_to_value (get_expr_value_id (e
), e
);
3639 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)), e
);
3640 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3644 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3646 print_bitmap_set (dump_file
, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3647 "tmp_gen", ENTRY_BLOCK
);
3648 print_bitmap_set (dump_file
, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3649 "avail_out", ENTRY_BLOCK
);
3652 /* Allocate the worklist. */
3653 worklist
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
3655 /* Seed the algorithm by putting the dominator children of the entry
3656 block on the worklist. */
3657 for (son
= first_dom_son (CDI_DOMINATORS
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3659 son
= next_dom_son (CDI_DOMINATORS
, son
))
3660 worklist
[sp
++] = son
;
3662 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun
))
3663 = ssa_default_def (cfun
, gimple_vop (cfun
));
3665 /* Loop until the worklist is empty. */
3671 /* Pick a block from the worklist. */
3672 block
= worklist
[--sp
];
3674 /* Initially, the set of available values in BLOCK is that of
3675 its immediate dominator. */
3676 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3679 bitmap_set_copy (AVAIL_OUT (block
), AVAIL_OUT (dom
));
3680 BB_LIVE_VOP_ON_EXIT (block
) = BB_LIVE_VOP_ON_EXIT (dom
);
3683 /* Generate values for PHI nodes. */
3684 for (gphi_iterator gsi
= gsi_start_phis (block
); !gsi_end_p (gsi
);
3687 tree result
= gimple_phi_result (gsi
.phi ());
3689 /* We have no need for virtual phis, as they don't represent
3690 actual computations. */
3691 if (virtual_operand_p (result
))
3693 BB_LIVE_VOP_ON_EXIT (block
) = result
;
3697 pre_expr e
= get_or_alloc_expr_for_name (result
);
3698 add_to_value (get_expr_value_id (e
), e
);
3699 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3700 bitmap_insert_into_set (PHI_GEN (block
), e
);
3703 BB_MAY_NOTRETURN (block
) = 0;
3705 /* Now compute value numbers and populate value sets with all
3706 the expressions computed in BLOCK. */
3707 for (gimple_stmt_iterator gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
);
3713 stmt
= gsi_stmt (gsi
);
3715 /* Cache whether the basic-block has any non-visible side-effect
3717 If this isn't a call or it is the last stmt in the
3718 basic-block then the CFG represents things correctly. */
3719 if (is_gimple_call (stmt
) && !stmt_ends_bb_p (stmt
))
3721 /* Non-looping const functions always return normally.
3722 Otherwise the call might not return or have side-effects
3723 that forbids hoisting possibly trapping expressions
3725 int flags
= gimple_call_flags (stmt
);
3726 if (!(flags
& ECF_CONST
)
3727 || (flags
& ECF_LOOPING_CONST_OR_PURE
))
3728 BB_MAY_NOTRETURN (block
) = 1;
3731 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
3733 pre_expr e
= get_or_alloc_expr_for_name (op
);
3735 add_to_value (get_expr_value_id (e
), e
);
3736 bitmap_insert_into_set (TMP_GEN (block
), e
);
3737 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3740 if (gimple_vdef (stmt
))
3741 BB_LIVE_VOP_ON_EXIT (block
) = gimple_vdef (stmt
);
3743 if (gimple_has_side_effects (stmt
)
3744 || stmt_could_throw_p (stmt
)
3745 || is_gimple_debug (stmt
))
3748 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
3750 if (ssa_undefined_value_p (op
))
3752 pre_expr e
= get_or_alloc_expr_for_name (op
);
3753 bitmap_value_insert_into_set (EXP_GEN (block
), e
);
3756 switch (gimple_code (stmt
))
3764 vn_reference_s ref1
;
3765 pre_expr result
= NULL
;
3767 /* We can value number only calls to real functions. */
3768 if (gimple_call_internal_p (stmt
))
3771 vn_reference_lookup_call (as_a
<gcall
*> (stmt
), &ref
, &ref1
);
3775 /* If the value of the call is not invalidated in
3776 this block until it is computed, add the expression
3778 if (!gimple_vuse (stmt
)
3780 (SSA_NAME_DEF_STMT (gimple_vuse (stmt
))) == GIMPLE_PHI
3781 || gimple_bb (SSA_NAME_DEF_STMT
3782 (gimple_vuse (stmt
))) != block
)
3784 result
= pre_expr_pool
.allocate ();
3785 result
->kind
= REFERENCE
;
3787 PRE_EXPR_REFERENCE (result
) = ref
;
3789 get_or_alloc_expression_id (result
);
3790 add_to_value (get_expr_value_id (result
), result
);
3791 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
3798 pre_expr result
= NULL
;
3799 switch (vn_get_stmt_kind (stmt
))
3803 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3806 /* COND_EXPR and VEC_COND_EXPR are awkward in
3807 that they contain an embedded complex expression.
3808 Don't even try to shove those through PRE. */
3809 if (code
== COND_EXPR
3810 || code
== VEC_COND_EXPR
)
3813 vn_nary_op_lookup_stmt (stmt
, &nary
);
3817 /* If the NARY traps and there was a preceding
3818 point in the block that might not return avoid
3819 adding the nary to EXP_GEN. */
3820 if (BB_MAY_NOTRETURN (block
)
3821 && vn_nary_may_trap (nary
))
3824 result
= pre_expr_pool
.allocate ();
3825 result
->kind
= NARY
;
3827 PRE_EXPR_NARY (result
) = nary
;
3834 vn_reference_lookup (gimple_assign_rhs1 (stmt
),
3840 /* If the value of the reference is not invalidated in
3841 this block until it is computed, add the expression
3843 if (gimple_vuse (stmt
))
3847 def_stmt
= SSA_NAME_DEF_STMT (gimple_vuse (stmt
));
3848 while (!gimple_nop_p (def_stmt
)
3849 && gimple_code (def_stmt
) != GIMPLE_PHI
3850 && gimple_bb (def_stmt
) == block
)
3852 if (stmt_may_clobber_ref_p
3853 (def_stmt
, gimple_assign_rhs1 (stmt
)))
3859 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt
));
3865 result
= pre_expr_pool
.allocate ();
3866 result
->kind
= REFERENCE
;
3868 PRE_EXPR_REFERENCE (result
) = ref
;
3876 get_or_alloc_expression_id (result
);
3877 add_to_value (get_expr_value_id (result
), result
);
3878 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
3886 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3888 print_bitmap_set (dump_file
, EXP_GEN (block
),
3889 "exp_gen", block
->index
);
3890 print_bitmap_set (dump_file
, PHI_GEN (block
),
3891 "phi_gen", block
->index
);
3892 print_bitmap_set (dump_file
, TMP_GEN (block
),
3893 "tmp_gen", block
->index
);
3894 print_bitmap_set (dump_file
, AVAIL_OUT (block
),
3895 "avail_out", block
->index
);
3898 /* Put the dominator children of BLOCK on the worklist of blocks
3899 to compute available sets for. */
3900 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
3902 son
= next_dom_son (CDI_DOMINATORS
, son
))
3903 worklist
[sp
++] = son
;
3910 /* Local state for the eliminate domwalk. */
3911 static vec
<gimple
> el_to_remove
;
3912 static vec
<gimple
> el_to_fixup
;
3913 static unsigned int el_todo
;
3914 static vec
<tree
> el_avail
;
3915 static vec
<tree
> el_avail_stack
;
3917 /* Return a leader for OP that is available at the current point of the
3918 eliminate domwalk. */
3921 eliminate_avail (tree op
)
3923 tree valnum
= VN_INFO (op
)->valnum
;
3924 if (TREE_CODE (valnum
) == SSA_NAME
)
3926 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
3928 if (el_avail
.length () > SSA_NAME_VERSION (valnum
))
3929 return el_avail
[SSA_NAME_VERSION (valnum
)];
3931 else if (is_gimple_min_invariant (valnum
))
3936 /* At the current point of the eliminate domwalk make OP available. */
3939 eliminate_push_avail (tree op
)
3941 tree valnum
= VN_INFO (op
)->valnum
;
3942 if (TREE_CODE (valnum
) == SSA_NAME
)
3944 if (el_avail
.length () <= SSA_NAME_VERSION (valnum
))
3945 el_avail
.safe_grow_cleared (SSA_NAME_VERSION (valnum
) + 1);
3947 if (el_avail
[SSA_NAME_VERSION (valnum
)])
3948 pushop
= el_avail
[SSA_NAME_VERSION (valnum
)];
3949 el_avail_stack
.safe_push (pushop
);
3950 el_avail
[SSA_NAME_VERSION (valnum
)] = op
;
3954 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
3955 the leader for the expression if insertion was successful. */
3958 eliminate_insert (gimple_stmt_iterator
*gsi
, tree val
)
3960 tree expr
= vn_get_expr_for (val
);
3961 if (!CONVERT_EXPR_P (expr
)
3962 && TREE_CODE (expr
) != VIEW_CONVERT_EXPR
)
3965 tree op
= TREE_OPERAND (expr
, 0);
3966 tree leader
= TREE_CODE (op
) == SSA_NAME
? eliminate_avail (op
) : op
;
3970 tree res
= make_temp_ssa_name (TREE_TYPE (val
), NULL
, "pretmp");
3971 gassign
*tem
= gimple_build_assign (res
,
3972 fold_build1 (TREE_CODE (expr
),
3973 TREE_TYPE (expr
), leader
));
3974 gsi_insert_before (gsi
, tem
, GSI_SAME_STMT
);
3975 VN_INFO_GET (res
)->valnum
= val
;
3977 if (TREE_CODE (leader
) == SSA_NAME
)
3978 gimple_set_plf (SSA_NAME_DEF_STMT (leader
), NECESSARY
, true);
3980 pre_stats
.insertions
++;
3981 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3983 fprintf (dump_file
, "Inserted ");
3984 print_gimple_stmt (dump_file
, tem
, 0, 0);
3990 class eliminate_dom_walker
: public dom_walker
3993 eliminate_dom_walker (cdi_direction direction
, bool do_pre_
)
3994 : dom_walker (direction
), do_pre (do_pre_
) {}
3996 virtual void before_dom_children (basic_block
);
3997 virtual void after_dom_children (basic_block
);
4002 /* Perform elimination for the basic-block B during the domwalk. */
4005 eliminate_dom_walker::before_dom_children (basic_block b
)
4008 el_avail_stack
.safe_push (NULL_TREE
);
4010 /* ??? If we do nothing for unreachable blocks then this will confuse
4011 tailmerging. Eventually we can reduce its reliance on SCCVN now
4012 that we fully copy/constant-propagate (most) things. */
4014 for (gphi_iterator gsi
= gsi_start_phis (b
); !gsi_end_p (gsi
);)
4016 gphi
*phi
= gsi
.phi ();
4017 tree res
= PHI_RESULT (phi
);
4019 if (virtual_operand_p (res
))
4025 tree sprime
= eliminate_avail (res
);
4029 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4031 fprintf (dump_file
, "Replaced redundant PHI node defining ");
4032 print_generic_expr (dump_file
, res
, 0);
4033 fprintf (dump_file
, " with ");
4034 print_generic_expr (dump_file
, sprime
, 0);
4035 fprintf (dump_file
, "\n");
4038 /* If we inserted this PHI node ourself, it's not an elimination. */
4040 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
)))
4043 pre_stats
.eliminations
++;
4045 /* If we will propagate into all uses don't bother to do
4047 if (may_propagate_copy (res
, sprime
))
4049 /* Mark the PHI for removal. */
4050 el_to_remove
.safe_push (phi
);
4055 remove_phi_node (&gsi
, false);
4058 && !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
))
4059 && TREE_CODE (sprime
) == SSA_NAME
)
4060 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
4062 if (!useless_type_conversion_p (TREE_TYPE (res
), TREE_TYPE (sprime
)))
4063 sprime
= fold_convert (TREE_TYPE (res
), sprime
);
4064 gimple stmt
= gimple_build_assign (res
, sprime
);
4065 /* ??? It cannot yet be necessary (DOM walk). */
4066 gimple_set_plf (stmt
, NECESSARY
, gimple_plf (phi
, NECESSARY
));
4068 gimple_stmt_iterator gsi2
= gsi_after_labels (b
);
4069 gsi_insert_before (&gsi2
, stmt
, GSI_NEW_STMT
);
4073 eliminate_push_avail (res
);
4077 for (gimple_stmt_iterator gsi
= gsi_start_bb (b
);
4081 tree sprime
= NULL_TREE
;
4082 gimple stmt
= gsi_stmt (gsi
);
4083 tree lhs
= gimple_get_lhs (stmt
);
4084 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
4085 && !gimple_has_volatile_ops (stmt
)
4086 /* See PR43491. Do not replace a global register variable when
4087 it is a the RHS of an assignment. Do replace local register
4088 variables since gcc does not guarantee a local variable will
4089 be allocated in register.
4090 ??? The fix isn't effective here. This should instead
4091 be ensured by not value-numbering them the same but treating
4092 them like volatiles? */
4093 && !(gimple_assign_single_p (stmt
)
4094 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == VAR_DECL
4095 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
))
4096 && is_global_var (gimple_assign_rhs1 (stmt
)))))
4098 sprime
= eliminate_avail (lhs
);
4101 /* If there is no existing usable leader but SCCVN thinks
4102 it has an expression it wants to use as replacement,
4104 tree val
= VN_INFO (lhs
)->valnum
;
4106 && TREE_CODE (val
) == SSA_NAME
4107 && VN_INFO (val
)->needs_insertion
4108 && VN_INFO (val
)->expr
!= NULL_TREE
4109 && (sprime
= eliminate_insert (&gsi
, val
)) != NULL_TREE
)
4110 eliminate_push_avail (sprime
);
4113 /* If this now constitutes a copy duplicate points-to
4114 and range info appropriately. This is especially
4115 important for inserted code. See tree-ssa-copy.c
4116 for similar code. */
4118 && TREE_CODE (sprime
) == SSA_NAME
)
4120 basic_block sprime_b
= gimple_bb (SSA_NAME_DEF_STMT (sprime
));
4121 if (POINTER_TYPE_P (TREE_TYPE (lhs
))
4122 && SSA_NAME_PTR_INFO (lhs
)
4123 && !SSA_NAME_PTR_INFO (sprime
))
4125 duplicate_ssa_name_ptr_info (sprime
,
4126 SSA_NAME_PTR_INFO (lhs
));
4128 mark_ptr_info_alignment_unknown
4129 (SSA_NAME_PTR_INFO (sprime
));
4131 else if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
4132 && SSA_NAME_RANGE_INFO (lhs
)
4133 && !SSA_NAME_RANGE_INFO (sprime
)
4135 duplicate_ssa_name_range_info (sprime
,
4136 SSA_NAME_RANGE_TYPE (lhs
),
4137 SSA_NAME_RANGE_INFO (lhs
));
4140 /* Inhibit the use of an inserted PHI on a loop header when
4141 the address of the memory reference is a simple induction
4142 variable. In other cases the vectorizer won't do anything
4143 anyway (either it's loop invariant or a complicated
4146 && TREE_CODE (sprime
) == SSA_NAME
4148 && flag_tree_loop_vectorize
4149 && loop_outer (b
->loop_father
)
4150 && has_zero_uses (sprime
)
4151 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))
4152 && gimple_assign_load_p (stmt
))
4154 gimple def_stmt
= SSA_NAME_DEF_STMT (sprime
);
4155 basic_block def_bb
= gimple_bb (def_stmt
);
4156 if (gimple_code (def_stmt
) == GIMPLE_PHI
4157 && b
->loop_father
->header
== def_bb
)
4162 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
4165 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (op
));
4167 && flow_bb_inside_loop_p (b
->loop_father
, def_bb
)
4168 && simple_iv (b
->loop_father
,
4169 b
->loop_father
, op
, &iv
, true))
4177 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4179 fprintf (dump_file
, "Not replacing ");
4180 print_gimple_expr (dump_file
, stmt
, 0, 0);
4181 fprintf (dump_file
, " with ");
4182 print_generic_expr (dump_file
, sprime
, 0);
4183 fprintf (dump_file
, " which would add a loop"
4184 " carried dependence to loop %d\n",
4185 b
->loop_father
->num
);
4187 /* Don't keep sprime available. */
4195 /* If we can propagate the value computed for LHS into
4196 all uses don't bother doing anything with this stmt. */
4197 if (may_propagate_copy (lhs
, sprime
))
4199 /* Mark it for removal. */
4200 el_to_remove
.safe_push (stmt
);
4202 /* ??? Don't count copy/constant propagations. */
4203 if (gimple_assign_single_p (stmt
)
4204 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4205 || gimple_assign_rhs1 (stmt
) == sprime
))
4208 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4210 fprintf (dump_file
, "Replaced ");
4211 print_gimple_expr (dump_file
, stmt
, 0, 0);
4212 fprintf (dump_file
, " with ");
4213 print_generic_expr (dump_file
, sprime
, 0);
4214 fprintf (dump_file
, " in all uses of ");
4215 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4218 pre_stats
.eliminations
++;
4222 /* If this is an assignment from our leader (which
4223 happens in the case the value-number is a constant)
4224 then there is nothing to do. */
4225 if (gimple_assign_single_p (stmt
)
4226 && sprime
== gimple_assign_rhs1 (stmt
))
4229 /* Else replace its RHS. */
4230 bool can_make_abnormal_goto
4231 = is_gimple_call (stmt
)
4232 && stmt_can_make_abnormal_goto (stmt
);
4234 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4236 fprintf (dump_file
, "Replaced ");
4237 print_gimple_expr (dump_file
, stmt
, 0, 0);
4238 fprintf (dump_file
, " with ");
4239 print_generic_expr (dump_file
, sprime
, 0);
4240 fprintf (dump_file
, " in ");
4241 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4244 if (TREE_CODE (sprime
) == SSA_NAME
)
4245 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4248 pre_stats
.eliminations
++;
4249 gimple orig_stmt
= stmt
;
4250 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
4251 TREE_TYPE (sprime
)))
4252 sprime
= fold_convert (TREE_TYPE (lhs
), sprime
);
4253 tree vdef
= gimple_vdef (stmt
);
4254 tree vuse
= gimple_vuse (stmt
);
4255 propagate_tree_value_into_stmt (&gsi
, sprime
);
4256 stmt
= gsi_stmt (gsi
);
4258 if (vdef
!= gimple_vdef (stmt
))
4259 VN_INFO (vdef
)->valnum
= vuse
;
4261 /* If we removed EH side-effects from the statement, clean
4262 its EH information. */
4263 if (maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
))
4265 bitmap_set_bit (need_eh_cleanup
,
4266 gimple_bb (stmt
)->index
);
4267 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4268 fprintf (dump_file
, " Removed EH side-effects.\n");
4271 /* Likewise for AB side-effects. */
4272 if (can_make_abnormal_goto
4273 && !stmt_can_make_abnormal_goto (stmt
))
4275 bitmap_set_bit (need_ab_cleanup
,
4276 gimple_bb (stmt
)->index
);
4277 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4278 fprintf (dump_file
, " Removed AB side-effects.\n");
4285 /* If the statement is a scalar store, see if the expression
4286 has the same value number as its rhs. If so, the store is
4288 if (gimple_assign_single_p (stmt
)
4289 && !gimple_has_volatile_ops (stmt
)
4290 && !is_gimple_reg (gimple_assign_lhs (stmt
))
4291 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4292 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
))))
4295 tree rhs
= gimple_assign_rhs1 (stmt
);
4296 val
= vn_reference_lookup (gimple_assign_lhs (stmt
),
4297 gimple_vuse (stmt
), VN_WALK
, NULL
);
4298 if (TREE_CODE (rhs
) == SSA_NAME
)
4299 rhs
= VN_INFO (rhs
)->valnum
;
4301 && operand_equal_p (val
, rhs
, 0))
4303 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4305 fprintf (dump_file
, "Deleted redundant store ");
4306 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4309 /* Queue stmt for removal. */
4310 el_to_remove
.safe_push (stmt
);
4315 bool can_make_abnormal_goto
= stmt_can_make_abnormal_goto (stmt
);
4316 bool was_noreturn
= (is_gimple_call (stmt
)
4317 && gimple_call_noreturn_p (stmt
));
4318 tree vdef
= gimple_vdef (stmt
);
4319 tree vuse
= gimple_vuse (stmt
);
4321 /* If we didn't replace the whole stmt (or propagate the result
4322 into all uses), replace all uses on this stmt with their
4324 use_operand_p use_p
;
4326 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
4328 tree use
= USE_FROM_PTR (use_p
);
4329 /* ??? The call code above leaves stmt operands un-updated. */
4330 if (TREE_CODE (use
) != SSA_NAME
)
4332 tree sprime
= eliminate_avail (use
);
4333 if (sprime
&& sprime
!= use
4334 && may_propagate_copy (use
, sprime
)
4335 /* We substitute into debug stmts to avoid excessive
4336 debug temporaries created by removed stmts, but we need
4337 to avoid doing so for inserted sprimes as we never want
4338 to create debug temporaries for them. */
4340 || TREE_CODE (sprime
) != SSA_NAME
4341 || !is_gimple_debug (stmt
)
4342 || !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))))
4344 propagate_value (use_p
, sprime
);
4345 gimple_set_modified (stmt
, true);
4346 if (TREE_CODE (sprime
) == SSA_NAME
4347 && !is_gimple_debug (stmt
))
4348 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4353 /* Visit indirect calls and turn them into direct calls if
4354 possible using the devirtualization machinery. */
4355 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
4357 tree fn
= gimple_call_fn (call_stmt
);
4359 && flag_devirtualize
4360 && virtual_method_call_p (fn
))
4362 tree otr_type
= obj_type_ref_class (fn
);
4364 ipa_polymorphic_call_context
context (current_function_decl
, fn
, stmt
, &instance
);
4367 context
.get_dynamic_type (instance
, OBJ_TYPE_REF_OBJECT (fn
), otr_type
, stmt
);
4369 vec
<cgraph_node
*>targets
4370 = possible_polymorphic_call_targets (obj_type_ref_class (fn
),
4372 (OBJ_TYPE_REF_TOKEN (fn
)),
4375 if (dump_enabled_p ())
4376 dump_possible_polymorphic_call_targets (dump_file
,
4377 obj_type_ref_class (fn
),
4379 (OBJ_TYPE_REF_TOKEN (fn
)),
4381 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
4384 if (targets
.length () == 1)
4385 fn
= targets
[0]->decl
;
4387 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
4388 if (dump_enabled_p ())
4390 location_t loc
= gimple_location_safe (stmt
);
4391 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
4392 "converting indirect call to "
4394 cgraph_node::get (fn
)->name ());
4396 gimple_call_set_fndecl (call_stmt
, fn
);
4397 maybe_remove_unused_call_args (cfun
, call_stmt
);
4398 gimple_set_modified (stmt
, true);
4403 if (gimple_modified_p (stmt
))
4405 /* If a formerly non-invariant ADDR_EXPR is turned into an
4406 invariant one it was on a separate stmt. */
4407 if (gimple_assign_single_p (stmt
)
4408 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
4409 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
4410 gimple old_stmt
= stmt
;
4411 if (is_gimple_call (stmt
))
4413 /* ??? Only fold calls inplace for now, this may create new
4414 SSA names which in turn will confuse free_scc_vn SSA name
4416 fold_stmt_inplace (&gsi
);
4417 /* When changing a call into a noreturn call, cfg cleanup
4418 is needed to fix up the noreturn call. */
4419 if (!was_noreturn
&& gimple_call_noreturn_p (stmt
))
4420 el_to_fixup
.safe_push (stmt
);
4425 stmt
= gsi_stmt (gsi
);
4426 if ((gimple_code (stmt
) == GIMPLE_COND
4427 && (gimple_cond_true_p (as_a
<gcond
*> (stmt
))
4428 || gimple_cond_false_p (as_a
<gcond
*> (stmt
))))
4429 || (gimple_code (stmt
) == GIMPLE_SWITCH
4430 && TREE_CODE (gimple_switch_index (
4431 as_a
<gswitch
*> (stmt
)))
4433 el_todo
|= TODO_cleanup_cfg
;
4435 /* If we removed EH side-effects from the statement, clean
4436 its EH information. */
4437 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
4439 bitmap_set_bit (need_eh_cleanup
,
4440 gimple_bb (stmt
)->index
);
4441 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4442 fprintf (dump_file
, " Removed EH side-effects.\n");
4444 /* Likewise for AB side-effects. */
4445 if (can_make_abnormal_goto
4446 && !stmt_can_make_abnormal_goto (stmt
))
4448 bitmap_set_bit (need_ab_cleanup
,
4449 gimple_bb (stmt
)->index
);
4450 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4451 fprintf (dump_file
, " Removed AB side-effects.\n");
4454 if (vdef
!= gimple_vdef (stmt
))
4455 VN_INFO (vdef
)->valnum
= vuse
;
4458 /* Make new values available - for fully redundant LHS we
4459 continue with the next stmt above and skip this. */
4461 FOR_EACH_SSA_DEF_OPERAND (defp
, stmt
, iter
, SSA_OP_DEF
)
4462 eliminate_push_avail (DEF_FROM_PTR (defp
));
4465 /* Replace destination PHI arguments. */
4468 FOR_EACH_EDGE (e
, ei
, b
->succs
)
4470 for (gphi_iterator gsi
= gsi_start_phis (e
->dest
);
4474 gphi
*phi
= gsi
.phi ();
4475 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
4476 tree arg
= USE_FROM_PTR (use_p
);
4477 if (TREE_CODE (arg
) != SSA_NAME
4478 || virtual_operand_p (arg
))
4480 tree sprime
= eliminate_avail (arg
);
4481 if (sprime
&& may_propagate_copy (arg
, sprime
))
4483 propagate_value (use_p
, sprime
);
4484 if (TREE_CODE (sprime
) == SSA_NAME
)
4485 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
4491 /* Make no longer available leaders no longer available. */
4494 eliminate_dom_walker::after_dom_children (basic_block
)
4497 while ((entry
= el_avail_stack
.pop ()) != NULL_TREE
)
4499 tree valnum
= VN_INFO (entry
)->valnum
;
4500 tree old
= el_avail
[SSA_NAME_VERSION (valnum
)];
4502 el_avail
[SSA_NAME_VERSION (valnum
)] = NULL_TREE
;
4504 el_avail
[SSA_NAME_VERSION (valnum
)] = entry
;
4508 /* Eliminate fully redundant computations. */
4511 eliminate (bool do_pre
)
4513 gimple_stmt_iterator gsi
;
4516 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
4517 need_ab_cleanup
= BITMAP_ALLOC (NULL
);
4519 el_to_remove
.create (0);
4520 el_to_fixup
.create (0);
4522 el_avail
.create (num_ssa_names
);
4523 el_avail_stack
.create (0);
4525 eliminate_dom_walker (CDI_DOMINATORS
,
4526 do_pre
).walk (cfun
->cfg
->x_entry_block_ptr
);
4528 el_avail
.release ();
4529 el_avail_stack
.release ();
4531 /* We cannot remove stmts during BB walk, especially not release SSA
4532 names there as this confuses the VN machinery. The stmts ending
4533 up in el_to_remove are either stores or simple copies.
4534 Remove stmts in reverse order to make debug stmt creation possible. */
4535 while (!el_to_remove
.is_empty ())
4537 stmt
= el_to_remove
.pop ();
4539 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4541 fprintf (dump_file
, "Removing dead stmt ");
4542 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4546 if (gimple_code (stmt
) == GIMPLE_PHI
)
4547 lhs
= gimple_phi_result (stmt
);
4549 lhs
= gimple_get_lhs (stmt
);
4552 && TREE_CODE (lhs
) == SSA_NAME
)
4553 bitmap_clear_bit (inserted_exprs
, SSA_NAME_VERSION (lhs
));
4555 gsi
= gsi_for_stmt (stmt
);
4556 if (gimple_code (stmt
) == GIMPLE_PHI
)
4557 remove_phi_node (&gsi
, true);
4560 basic_block bb
= gimple_bb (stmt
);
4561 unlink_stmt_vdef (stmt
);
4562 if (gsi_remove (&gsi
, true))
4563 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
4564 release_defs (stmt
);
4567 /* Removing a stmt may expose a forwarder block. */
4568 el_todo
|= TODO_cleanup_cfg
;
4570 el_to_remove
.release ();
4572 /* Fixup stmts that became noreturn calls. This may require splitting
4573 blocks and thus isn't possible during the dominator walk. Do this
4574 in reverse order so we don't inadvertedly remove a stmt we want to
4575 fixup by visiting a dominating now noreturn call first. */
4576 while (!el_to_fixup
.is_empty ())
4578 stmt
= el_to_fixup
.pop ();
4580 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4582 fprintf (dump_file
, "Fixing up noreturn call ");
4583 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4586 if (fixup_noreturn_call (stmt
))
4587 el_todo
|= TODO_cleanup_cfg
;
4589 el_to_fixup
.release ();
4594 /* Perform CFG cleanups made necessary by elimination. */
4597 fini_eliminate (void)
4599 bool do_eh_cleanup
= !bitmap_empty_p (need_eh_cleanup
);
4600 bool do_ab_cleanup
= !bitmap_empty_p (need_ab_cleanup
);
4603 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
4606 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup
);
4608 BITMAP_FREE (need_eh_cleanup
);
4609 BITMAP_FREE (need_ab_cleanup
);
4611 if (do_eh_cleanup
|| do_ab_cleanup
)
4612 return TODO_cleanup_cfg
;
4616 /* Borrow a bit of tree-ssa-dce.c for the moment.
4617 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4618 this may be a bit faster, and we may want critical edges kept split. */
4620 /* If OP's defining statement has not already been determined to be necessary,
4621 mark that statement necessary. Return the stmt, if it is newly
4624 static inline gimple
4625 mark_operand_necessary (tree op
)
4631 if (TREE_CODE (op
) != SSA_NAME
)
4634 stmt
= SSA_NAME_DEF_STMT (op
);
4637 if (gimple_plf (stmt
, NECESSARY
)
4638 || gimple_nop_p (stmt
))
4641 gimple_set_plf (stmt
, NECESSARY
, true);
4645 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4646 to insert PHI nodes sometimes, and because value numbering of casts isn't
4647 perfect, we sometimes end up inserting dead code. This simple DCE-like
4648 pass removes any insertions we made that weren't actually used. */
4651 remove_dead_inserted_code (void)
4658 worklist
= BITMAP_ALLOC (NULL
);
4659 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4661 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4662 if (gimple_plf (t
, NECESSARY
))
4663 bitmap_set_bit (worklist
, i
);
4665 while (!bitmap_empty_p (worklist
))
4667 i
= bitmap_first_set_bit (worklist
);
4668 bitmap_clear_bit (worklist
, i
);
4669 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4671 /* PHI nodes are somewhat special in that each PHI alternative has
4672 data and control dependencies. All the statements feeding the
4673 PHI node's arguments are always necessary. */
4674 if (gimple_code (t
) == GIMPLE_PHI
)
4678 for (k
= 0; k
< gimple_phi_num_args (t
); k
++)
4680 tree arg
= PHI_ARG_DEF (t
, k
);
4681 if (TREE_CODE (arg
) == SSA_NAME
)
4683 gimple n
= mark_operand_necessary (arg
);
4685 bitmap_set_bit (worklist
, SSA_NAME_VERSION (arg
));
4691 /* Propagate through the operands. Examine all the USE, VUSE and
4692 VDEF operands in this statement. Mark all the statements
4693 which feed this statement's uses as necessary. */
4697 /* The operands of VDEF expressions are also needed as they
4698 represent potential definitions that may reach this
4699 statement (VDEF operands allow us to follow def-def
4702 FOR_EACH_SSA_TREE_OPERAND (use
, t
, iter
, SSA_OP_ALL_USES
)
4704 gimple n
= mark_operand_necessary (use
);
4706 bitmap_set_bit (worklist
, SSA_NAME_VERSION (use
));
4711 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4713 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4714 if (!gimple_plf (t
, NECESSARY
))
4716 gimple_stmt_iterator gsi
;
4718 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4720 fprintf (dump_file
, "Removing unnecessary insertion:");
4721 print_gimple_stmt (dump_file
, t
, 0, 0);
4724 gsi
= gsi_for_stmt (t
);
4725 if (gimple_code (t
) == GIMPLE_PHI
)
4726 remove_phi_node (&gsi
, true);
4729 gsi_remove (&gsi
, true);
4734 BITMAP_FREE (worklist
);
4738 /* Initialize data structures used by PRE. */
4745 next_expression_id
= 1;
4746 expressions
.create (0);
4747 expressions
.safe_push (NULL
);
4748 value_expressions
.create (get_max_value_id () + 1);
4749 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
4750 name_to_id
.create (0);
4752 inserted_exprs
= BITMAP_ALLOC (NULL
);
4754 connect_infinite_loops_to_exit ();
4755 memset (&pre_stats
, 0, sizeof (pre_stats
));
4757 postorder
= XNEWVEC (int, n_basic_blocks_for_fn (cfun
));
4758 postorder_num
= inverted_post_order_compute (postorder
);
4760 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets
));
4762 calculate_dominance_info (CDI_POST_DOMINATORS
);
4763 calculate_dominance_info (CDI_DOMINATORS
);
4765 bitmap_obstack_initialize (&grand_bitmap_obstack
);
4766 phi_translate_table
= new hash_table
<expr_pred_trans_d
> (5110);
4767 expression_to_id
= new hash_table
<pre_expr_d
> (num_ssa_names
* 3);
4768 FOR_ALL_BB_FN (bb
, cfun
)
4770 EXP_GEN (bb
) = bitmap_set_new ();
4771 PHI_GEN (bb
) = bitmap_set_new ();
4772 TMP_GEN (bb
) = bitmap_set_new ();
4773 AVAIL_OUT (bb
) = bitmap_set_new ();
4778 /* Deallocate data structures used by PRE. */
4784 value_expressions
.release ();
4785 BITMAP_FREE (inserted_exprs
);
4786 bitmap_obstack_release (&grand_bitmap_obstack
);
4787 bitmap_set_pool
.release ();
4788 pre_expr_pool
.release ();
4789 delete phi_translate_table
;
4790 phi_translate_table
= NULL
;
4791 delete expression_to_id
;
4792 expression_to_id
= NULL
;
4793 name_to_id
.release ();
4795 free_aux_for_blocks ();
4797 free_dominance_info (CDI_POST_DOMINATORS
);
4802 const pass_data pass_data_pre
=
4804 GIMPLE_PASS
, /* type */
4806 OPTGROUP_NONE
, /* optinfo_flags */
4807 TV_TREE_PRE
, /* tv_id */
4808 /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before
4810 ( PROP_no_crit_edges
| PROP_cfg
| PROP_ssa
), /* properties_required */
4811 0, /* properties_provided */
4812 PROP_no_crit_edges
, /* properties_destroyed */
4813 TODO_rebuild_alias
, /* todo_flags_start */
4814 0, /* todo_flags_finish */
4817 class pass_pre
: public gimple_opt_pass
4820 pass_pre (gcc::context
*ctxt
)
4821 : gimple_opt_pass (pass_data_pre
, ctxt
)
4824 /* opt_pass methods: */
4825 virtual bool gate (function
*) { return flag_tree_pre
!= 0; }
4826 virtual unsigned int execute (function
*);
4828 }; // class pass_pre
4831 pass_pre::execute (function
*fun
)
4833 unsigned int todo
= 0;
4835 do_partial_partial
=
4836 flag_tree_partial_pre
&& optimize_function_for_speed_p (fun
);
4838 /* This has to happen before SCCVN runs because
4839 loop_optimizer_init may create new phis, etc. */
4840 loop_optimizer_init (LOOPS_NORMAL
);
4842 if (!run_scc_vn (VN_WALK
))
4844 loop_optimizer_finalize ();
4851 /* Collect and value number expressions computed in each basic block. */
4854 /* Insert can get quite slow on an incredibly large number of basic
4855 blocks due to some quadratic behavior. Until this behavior is
4856 fixed, don't run it when he have an incredibly large number of
4857 bb's. If we aren't going to run insert, there is no point in
4858 computing ANTIC, either, even though it's plenty fast. */
4859 if (n_basic_blocks_for_fn (fun
) < 4000)
4865 /* Make sure to remove fake edges before committing our inserts.
4866 This makes sure we don't end up with extra critical edges that
4867 we would need to split. */
4868 remove_fake_exit_edges ();
4869 gsi_commit_edge_inserts ();
4871 /* Eliminate folds statements which might (should not...) end up
4872 not keeping virtual operands up-to-date. */
4873 gcc_assert (!need_ssa_update_p (fun
));
4875 /* Remove all the redundant expressions. */
4876 todo
|= eliminate (true);
4878 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
4879 statistics_counter_event (fun
, "PA inserted", pre_stats
.pa_insert
);
4880 statistics_counter_event (fun
, "New PHIs", pre_stats
.phis
);
4881 statistics_counter_event (fun
, "Eliminated", pre_stats
.eliminations
);
4883 clear_expression_ids ();
4884 remove_dead_inserted_code ();
4888 todo
|= fini_eliminate ();
4889 loop_optimizer_finalize ();
4891 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4892 case we can merge the block with the remaining predecessor of the block.
4894 - call merge_blocks after each tail merge iteration
4895 - call merge_blocks after all tail merge iterations
4896 - mark TODO_cleanup_cfg when necessary
4897 - share the cfg cleanup with fini_pre. */
4898 todo
|= tail_merge_optimize (todo
);
4902 /* Tail merging invalidates the virtual SSA web, together with
4903 cfg-cleanup opportunities exposed by PRE this will wreck the
4904 SSA updating machinery. So make sure to run update-ssa
4905 manually, before eventually scheduling cfg-cleanup as part of
4907 update_ssa (TODO_update_ssa_only_virtuals
);
4915 make_pass_pre (gcc::context
*ctxt
)
4917 return new pass_pre (ctxt
);
4922 const pass_data pass_data_fre
=
4924 GIMPLE_PASS
, /* type */
4926 OPTGROUP_NONE
, /* optinfo_flags */
4927 TV_TREE_FRE
, /* tv_id */
4928 ( PROP_cfg
| PROP_ssa
), /* properties_required */
4929 0, /* properties_provided */
4930 0, /* properties_destroyed */
4931 0, /* todo_flags_start */
4932 0, /* todo_flags_finish */
4935 class pass_fre
: public gimple_opt_pass
4938 pass_fre (gcc::context
*ctxt
)
4939 : gimple_opt_pass (pass_data_fre
, ctxt
)
4942 /* opt_pass methods: */
4943 opt_pass
* clone () { return new pass_fre (m_ctxt
); }
4944 virtual bool gate (function
*) { return flag_tree_fre
!= 0; }
4945 virtual unsigned int execute (function
*);
4947 }; // class pass_fre
4950 pass_fre::execute (function
*fun
)
4952 unsigned int todo
= 0;
4954 if (!run_scc_vn (VN_WALKREWRITE
))
4957 memset (&pre_stats
, 0, sizeof (pre_stats
));
4959 /* Remove all the redundant expressions. */
4960 todo
|= eliminate (false);
4962 todo
|= fini_eliminate ();
4966 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
4967 statistics_counter_event (fun
, "Eliminated", pre_stats
.eliminations
);
4975 make_pass_fre (gcc::context
*ctxt
)
4977 return new pass_fre (ctxt
);