1 /* Full and partial redundancy elimination and code hoisting on SSA GIMPLE.
2 Copyright (C) 2001-2018 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"
30 #include "alloc-pool.h"
31 #include "tree-pass.h"
34 #include "gimple-pretty-print.h"
35 #include "fold-const.h"
37 #include "gimple-fold.h"
40 #include "gimple-iterator.h"
42 #include "tree-into-ssa.h"
46 #include "tree-ssa-sccvn.h"
47 #include "tree-scalar-evolution.h"
51 #include "tree-ssa-propagate.h"
52 #include "tree-ssa-dce.h"
53 #include "tree-cfgcleanup.h"
56 /* Even though this file is called tree-ssa-pre.c, we actually
57 implement a bit more than just PRE here. All of them piggy-back
58 on GVN which is implemented in tree-ssa-sccvn.c.
60 1. Full Redundancy Elimination (FRE)
61 This is the elimination phase of GVN.
63 2. Partial Redundancy Elimination (PRE)
64 This is adds computation of AVAIL_OUT and ANTIC_IN and
65 doing expression insertion to form GVN-PRE.
68 This optimization uses the ANTIC_IN sets computed for PRE
69 to move expressions further up than PRE would do, to make
70 multiple computations of the same value fully redundant.
71 This pass is explained below (after the explanation of the
72 basic algorithm for PRE).
77 1. Avail sets can be shared by making an avail_find_leader that
78 walks up the dominator tree and looks in those avail sets.
79 This might affect code optimality, it's unclear right now.
80 Currently the AVAIL_OUT sets are the remaining quadraticness in
82 2. Strength reduction can be performed by anticipating expressions
83 we can repair later on.
84 3. We can do back-substitution or smarter value numbering to catch
85 commutative expressions split up over multiple statements.
88 /* For ease of terminology, "expression node" in the below refers to
89 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
90 represent the actual statement containing the expressions we care about,
91 and we cache the value number by putting it in the expression. */
93 /* Basic algorithm for Partial Redundancy Elimination:
95 First we walk the statements to generate the AVAIL sets, the
96 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
97 generation of values/expressions by a given block. We use them
98 when computing the ANTIC sets. The AVAIL sets consist of
99 SSA_NAME's that represent values, so we know what values are
100 available in what blocks. AVAIL is a forward dataflow problem. In
101 SSA, values are never killed, so we don't need a kill set, or a
102 fixpoint iteration, in order to calculate the AVAIL sets. In
103 traditional parlance, AVAIL sets tell us the downsafety of the
106 Next, we generate the ANTIC sets. These sets represent the
107 anticipatable expressions. ANTIC is a backwards dataflow
108 problem. An expression is anticipatable in a given block if it could
109 be generated in that block. This means that if we had to perform
110 an insertion in that block, of the value of that expression, we
111 could. Calculating the ANTIC sets requires phi translation of
112 expressions, because the flow goes backwards through phis. We must
113 iterate to a fixpoint of the ANTIC sets, because we have a kill
114 set. Even in SSA form, values are not live over the entire
115 function, only from their definition point onwards. So we have to
116 remove values from the ANTIC set once we go past the definition
117 point of the leaders that make them up.
118 compute_antic/compute_antic_aux performs this computation.
120 Third, we perform insertions to make partially redundant
121 expressions fully redundant.
123 An expression is partially redundant (excluding partial
126 1. It is AVAIL in some, but not all, of the predecessors of a
128 2. It is ANTIC in all the predecessors.
130 In order to make it fully redundant, we insert the expression into
131 the predecessors where it is not available, but is ANTIC.
133 When optimizing for size, we only eliminate the partial redundancy
134 if we need to insert in only one predecessor. This avoids almost
135 completely the code size increase that PRE usually causes.
137 For the partial anticipation case, we only perform insertion if it
138 is partially anticipated in some block, and fully available in all
141 do_pre_regular_insertion/do_pre_partial_partial_insertion
142 performs these steps, driven by insert/insert_aux.
144 Fourth, we eliminate fully redundant expressions.
145 This is a simple statement walk that replaces redundant
146 calculations with the now available values. */
148 /* Basic algorithm for Code Hoisting:
150 Code hoisting is: Moving value computations up in the control flow
151 graph to make multiple copies redundant. Typically this is a size
152 optimization, but there are cases where it also is helpful for speed.
154 A simple code hoisting algorithm is implemented that piggy-backs on
155 the PRE infrastructure. For code hoisting, we have to know ANTIC_OUT
156 which is effectively ANTIC_IN - AVAIL_OUT. The latter two have to be
157 computed for PRE, and we can use them to perform a limited version of
160 For the purpose of this implementation, a value is hoistable to a basic
161 block B if the following properties are met:
163 1. The value is in ANTIC_IN(B) -- the value will be computed on all
164 paths from B to function exit and it can be computed in B);
166 2. The value is not in AVAIL_OUT(B) -- there would be no need to
167 compute the value again and make it available twice;
169 3. All successors of B are dominated by B -- makes sure that inserting
170 a computation of the value in B will make the remaining
171 computations fully redundant;
173 4. At least one successor has the value in AVAIL_OUT -- to avoid
174 hoisting values up too far;
176 5. There are at least two successors of B -- hoisting in straight
177 line code is pointless.
179 The third condition is not strictly necessary, but it would complicate
180 the hoisting pass a lot. In fact, I don't know of any code hoisting
181 algorithm that does not have this requirement. Fortunately, experiments
182 have show that most candidate hoistable values are in regions that meet
183 this condition (e.g. diamond-shape regions).
185 The forth condition is necessary to avoid hoisting things up too far
186 away from the uses of the value. Nothing else limits the algorithm
187 from hoisting everything up as far as ANTIC_IN allows. Experiments
188 with SPEC and CSiBE have shown that hoisting up too far results in more
189 spilling, less benefits for code size, and worse benchmark scores.
190 Fortunately, in practice most of the interesting hoisting opportunities
191 are caught despite this limitation.
193 For hoistable values that meet all conditions, expressions are inserted
194 to make the calculation of the hoistable value fully redundant. We
195 perform code hoisting insertions after each round of PRE insertions,
196 because code hoisting never exposes new PRE opportunities, but PRE can
197 create new code hoisting opportunities.
199 The code hoisting algorithm is implemented in do_hoist_insert, driven
200 by insert/insert_aux. */
202 /* Representations of value numbers:
204 Value numbers are represented by a representative SSA_NAME. We
205 will create fake SSA_NAME's in situations where we need a
206 representative but do not have one (because it is a complex
207 expression). In order to facilitate storing the value numbers in
208 bitmaps, and keep the number of wasted SSA_NAME's down, we also
209 associate a value_id with each value number, and create full blown
210 ssa_name's only where we actually need them (IE in operands of
211 existing expressions).
213 Theoretically you could replace all the value_id's with
214 SSA_NAME_VERSION, but this would allocate a large number of
215 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
216 It would also require an additional indirection at each point we
219 /* Representation of expressions on value numbers:
221 Expressions consisting of value numbers are represented the same
222 way as our VN internally represents them, with an additional
223 "pre_expr" wrapping around them in order to facilitate storing all
224 of the expressions in the same sets. */
226 /* Representation of sets:
228 The dataflow sets do not need to be sorted in any particular order
229 for the majority of their lifetime, are simply represented as two
230 bitmaps, one that keeps track of values present in the set, and one
231 that keeps track of expressions present in the set.
233 When we need them in topological order, we produce it on demand by
234 transforming the bitmap into an array and sorting it into topo
237 /* Type of expression, used to know which member of the PRE_EXPR union
253 vn_reference_t reference
;
256 typedef struct pre_expr_d
: nofree_ptr_hash
<pre_expr_d
>
258 enum pre_expr_kind kind
;
262 /* hash_table support. */
263 static inline hashval_t
hash (const pre_expr_d
*);
264 static inline int equal (const pre_expr_d
*, const pre_expr_d
*);
267 #define PRE_EXPR_NAME(e) (e)->u.name
268 #define PRE_EXPR_NARY(e) (e)->u.nary
269 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
270 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
272 /* Compare E1 and E1 for equality. */
275 pre_expr_d::equal (const pre_expr_d
*e1
, const pre_expr_d
*e2
)
277 if (e1
->kind
!= e2
->kind
)
283 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1
),
284 PRE_EXPR_CONSTANT (e2
));
286 return PRE_EXPR_NAME (e1
) == PRE_EXPR_NAME (e2
);
288 return vn_nary_op_eq (PRE_EXPR_NARY (e1
), PRE_EXPR_NARY (e2
));
290 return vn_reference_eq (PRE_EXPR_REFERENCE (e1
),
291 PRE_EXPR_REFERENCE (e2
));
300 pre_expr_d::hash (const pre_expr_d
*e
)
305 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e
));
307 return SSA_NAME_VERSION (PRE_EXPR_NAME (e
));
309 return PRE_EXPR_NARY (e
)->hashcode
;
311 return PRE_EXPR_REFERENCE (e
)->hashcode
;
317 /* Next global expression id number. */
318 static unsigned int next_expression_id
;
320 /* Mapping from expression to id number we can use in bitmap sets. */
321 static vec
<pre_expr
> expressions
;
322 static hash_table
<pre_expr_d
> *expression_to_id
;
323 static vec
<unsigned> name_to_id
;
325 /* Allocate an expression id for EXPR. */
327 static inline unsigned int
328 alloc_expression_id (pre_expr expr
)
330 struct pre_expr_d
**slot
;
331 /* Make sure we won't overflow. */
332 gcc_assert (next_expression_id
+ 1 > next_expression_id
);
333 expr
->id
= next_expression_id
++;
334 expressions
.safe_push (expr
);
335 if (expr
->kind
== NAME
)
337 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
338 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
339 re-allocations by using vec::reserve upfront. */
340 unsigned old_len
= name_to_id
.length ();
341 name_to_id
.reserve (num_ssa_names
- old_len
);
342 name_to_id
.quick_grow_cleared (num_ssa_names
);
343 gcc_assert (name_to_id
[version
] == 0);
344 name_to_id
[version
] = expr
->id
;
348 slot
= expression_to_id
->find_slot (expr
, INSERT
);
352 return next_expression_id
- 1;
355 /* Return the expression id for tree EXPR. */
357 static inline unsigned int
358 get_expression_id (const pre_expr expr
)
363 static inline unsigned int
364 lookup_expression_id (const pre_expr expr
)
366 struct pre_expr_d
**slot
;
368 if (expr
->kind
== NAME
)
370 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
371 if (name_to_id
.length () <= version
)
373 return name_to_id
[version
];
377 slot
= expression_to_id
->find_slot (expr
, NO_INSERT
);
380 return ((pre_expr
)*slot
)->id
;
384 /* Return the existing expression id for EXPR, or create one if one
385 does not exist yet. */
387 static inline unsigned int
388 get_or_alloc_expression_id (pre_expr expr
)
390 unsigned int id
= lookup_expression_id (expr
);
392 return alloc_expression_id (expr
);
393 return expr
->id
= id
;
396 /* Return the expression that has expression id ID */
398 static inline pre_expr
399 expression_for_id (unsigned int id
)
401 return expressions
[id
];
404 static object_allocator
<pre_expr_d
> pre_expr_pool ("pre_expr nodes");
406 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
409 get_or_alloc_expr_for_name (tree name
)
411 struct pre_expr_d expr
;
413 unsigned int result_id
;
417 PRE_EXPR_NAME (&expr
) = name
;
418 result_id
= lookup_expression_id (&expr
);
420 return expression_for_id (result_id
);
422 result
= pre_expr_pool
.allocate ();
424 PRE_EXPR_NAME (result
) = name
;
425 alloc_expression_id (result
);
429 /* An unordered bitmap set. One bitmap tracks values, the other,
431 typedef struct bitmap_set
433 bitmap_head expressions
;
437 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
438 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
440 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
441 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
443 /* Mapping from value id to expressions with that value_id. */
444 static vec
<bitmap
> value_expressions
;
446 /* Sets that we need to keep track of. */
447 typedef struct bb_bitmap_sets
449 /* The EXP_GEN set, which represents expressions/values generated in
451 bitmap_set_t exp_gen
;
453 /* The PHI_GEN set, which represents PHI results generated in a
455 bitmap_set_t phi_gen
;
457 /* The TMP_GEN set, which represents results/temporaries generated
458 in a basic block. IE the LHS of an expression. */
459 bitmap_set_t tmp_gen
;
461 /* The AVAIL_OUT set, which represents which values are available in
462 a given basic block. */
463 bitmap_set_t avail_out
;
465 /* The ANTIC_IN set, which represents which values are anticipatable
466 in a given basic block. */
467 bitmap_set_t antic_in
;
469 /* The PA_IN set, which represents which values are
470 partially anticipatable in a given basic block. */
473 /* The NEW_SETS set, which is used during insertion to augment the
474 AVAIL_OUT set of blocks with the new insertions performed during
475 the current iteration. */
476 bitmap_set_t new_sets
;
478 /* A cache for value_dies_in_block_x. */
481 /* The live virtual operand on successor edges. */
484 /* True if we have visited this block during ANTIC calculation. */
485 unsigned int visited
: 1;
487 /* True when the block contains a call that might not return. */
488 unsigned int contains_may_not_return_call
: 1;
491 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
492 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
493 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
494 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
495 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
496 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
497 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
498 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
499 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
500 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
501 #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
504 /* This structure is used to keep track of statistics on what
505 optimization PRE was able to perform. */
508 /* The number of new expressions/temporaries generated by PRE. */
511 /* The number of inserts found due to partial anticipation */
514 /* The number of inserts made for code hoisting. */
517 /* The number of new PHI nodes added by PRE. */
521 static bool do_partial_partial
;
522 static pre_expr
bitmap_find_leader (bitmap_set_t
, unsigned int);
523 static void bitmap_value_insert_into_set (bitmap_set_t
, pre_expr
);
524 static void bitmap_value_replace_in_set (bitmap_set_t
, pre_expr
);
525 static void bitmap_set_copy (bitmap_set_t
, bitmap_set_t
);
526 static bool bitmap_set_contains_value (bitmap_set_t
, unsigned int);
527 static void bitmap_insert_into_set (bitmap_set_t
, pre_expr
);
528 static bitmap_set_t
bitmap_set_new (void);
529 static tree
create_expression_by_pieces (basic_block
, pre_expr
, gimple_seq
*,
531 static tree
find_or_generate_expression (basic_block
, tree
, gimple_seq
*);
532 static unsigned int get_expr_value_id (pre_expr
);
534 /* We can add and remove elements and entries to and from sets
535 and hash tables, so we use alloc pools for them. */
537 static object_allocator
<bitmap_set
> bitmap_set_pool ("Bitmap sets");
538 static bitmap_obstack grand_bitmap_obstack
;
540 /* A three tuple {e, pred, v} used to cache phi translations in the
541 phi_translate_table. */
543 typedef struct expr_pred_trans_d
: free_ptr_hash
<expr_pred_trans_d
>
545 /* The expression. */
548 /* The predecessor block along which we translated the expression. */
551 /* The value that resulted from the translation. */
554 /* The hashcode for the expression, pred pair. This is cached for
558 /* hash_table support. */
559 static inline hashval_t
hash (const expr_pred_trans_d
*);
560 static inline int equal (const expr_pred_trans_d
*, const expr_pred_trans_d
*);
561 } *expr_pred_trans_t
;
562 typedef const struct expr_pred_trans_d
*const_expr_pred_trans_t
;
565 expr_pred_trans_d::hash (const expr_pred_trans_d
*e
)
571 expr_pred_trans_d::equal (const expr_pred_trans_d
*ve1
,
572 const expr_pred_trans_d
*ve2
)
574 basic_block b1
= ve1
->pred
;
575 basic_block b2
= ve2
->pred
;
577 /* If they are not translations for the same basic block, they can't
581 return pre_expr_d::equal (ve1
->e
, ve2
->e
);
584 /* The phi_translate_table caches phi translations for a given
585 expression and predecessor. */
586 static hash_table
<expr_pred_trans_d
> *phi_translate_table
;
588 /* Add the tuple mapping from {expression E, basic block PRED} to
589 the phi translation table and return whether it pre-existed. */
592 phi_trans_add (expr_pred_trans_t
*entry
, pre_expr e
, basic_block pred
)
594 expr_pred_trans_t
*slot
;
595 expr_pred_trans_d tem
;
596 hashval_t hash
= iterative_hash_hashval_t (pre_expr_d::hash (e
),
601 slot
= phi_translate_table
->find_slot_with_hash (&tem
, hash
, INSERT
);
608 *entry
= *slot
= XNEW (struct expr_pred_trans_d
);
610 (*entry
)->pred
= pred
;
611 (*entry
)->hashcode
= hash
;
616 /* Add expression E to the expression set of value id V. */
619 add_to_value (unsigned int v
, pre_expr e
)
623 gcc_checking_assert (get_expr_value_id (e
) == v
);
625 if (v
>= value_expressions
.length ())
627 value_expressions
.safe_grow_cleared (v
+ 1);
630 set
= value_expressions
[v
];
633 set
= BITMAP_ALLOC (&grand_bitmap_obstack
);
634 value_expressions
[v
] = set
;
637 bitmap_set_bit (set
, get_or_alloc_expression_id (e
));
640 /* Create a new bitmap set and return it. */
643 bitmap_set_new (void)
645 bitmap_set_t ret
= bitmap_set_pool
.allocate ();
646 bitmap_initialize (&ret
->expressions
, &grand_bitmap_obstack
);
647 bitmap_initialize (&ret
->values
, &grand_bitmap_obstack
);
651 /* Return the value id for a PRE expression EXPR. */
654 get_expr_value_id (pre_expr expr
)
660 id
= get_constant_value_id (PRE_EXPR_CONSTANT (expr
));
663 id
= VN_INFO (PRE_EXPR_NAME (expr
))->value_id
;
666 id
= PRE_EXPR_NARY (expr
)->value_id
;
669 id
= PRE_EXPR_REFERENCE (expr
)->value_id
;
674 /* ??? We cannot assert that expr has a value-id (it can be 0), because
675 we assign value-ids only to expressions that have a result
676 in set_hashtable_value_ids. */
680 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
683 sccvn_valnum_from_value_id (unsigned int val
)
687 bitmap exprset
= value_expressions
[val
];
688 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
690 pre_expr vexpr
= expression_for_id (i
);
691 if (vexpr
->kind
== NAME
)
692 return VN_INFO (PRE_EXPR_NAME (vexpr
))->valnum
;
693 else if (vexpr
->kind
== CONSTANT
)
694 return PRE_EXPR_CONSTANT (vexpr
);
699 /* Insert an expression EXPR into a bitmapped set. */
702 bitmap_insert_into_set (bitmap_set_t set
, pre_expr expr
)
704 unsigned int val
= get_expr_value_id (expr
);
705 if (! value_id_constant_p (val
))
707 /* Note this is the only function causing multiple expressions
708 for the same value to appear in a set. This is needed for
709 TMP_GEN, PHI_GEN and NEW_SETs. */
710 bitmap_set_bit (&set
->values
, val
);
711 bitmap_set_bit (&set
->expressions
, get_or_alloc_expression_id (expr
));
715 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
718 bitmap_set_copy (bitmap_set_t dest
, bitmap_set_t orig
)
720 bitmap_copy (&dest
->expressions
, &orig
->expressions
);
721 bitmap_copy (&dest
->values
, &orig
->values
);
725 /* Free memory used up by SET. */
727 bitmap_set_free (bitmap_set_t set
)
729 bitmap_clear (&set
->expressions
);
730 bitmap_clear (&set
->values
);
734 /* Generate an topological-ordered array of bitmap set SET. */
737 sorted_array_from_bitmap_set (bitmap_set_t set
)
740 bitmap_iterator bi
, bj
;
741 vec
<pre_expr
> result
;
743 /* Pre-allocate enough space for the array. */
744 result
.create (bitmap_count_bits (&set
->expressions
));
746 FOR_EACH_VALUE_ID_IN_SET (set
, i
, bi
)
748 /* The number of expressions having a given value is usually
749 relatively small. Thus, rather than making a vector of all
750 the expressions and sorting it by value-id, we walk the values
751 and check in the reverse mapping that tells us what expressions
752 have a given value, to filter those in our set. As a result,
753 the expressions are inserted in value-id order, which means
756 If this is somehow a significant lose for some cases, we can
757 choose which set to walk based on the set size. */
758 bitmap exprset
= value_expressions
[i
];
759 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, j
, bj
)
761 if (bitmap_bit_p (&set
->expressions
, j
))
762 result
.quick_push (expression_for_id (j
));
769 /* Subtract all expressions contained in ORIG from DEST. */
772 bitmap_set_subtract_expressions (bitmap_set_t dest
, bitmap_set_t orig
)
774 bitmap_set_t result
= bitmap_set_new ();
778 bitmap_and_compl (&result
->expressions
, &dest
->expressions
,
781 FOR_EACH_EXPR_ID_IN_SET (result
, i
, bi
)
783 pre_expr expr
= expression_for_id (i
);
784 unsigned int value_id
= get_expr_value_id (expr
);
785 bitmap_set_bit (&result
->values
, value_id
);
791 /* Subtract all values in bitmap set B from bitmap set A. */
794 bitmap_set_subtract_values (bitmap_set_t a
, bitmap_set_t b
)
798 unsigned to_remove
= -1U;
799 bitmap_and_compl_into (&a
->values
, &b
->values
);
800 FOR_EACH_EXPR_ID_IN_SET (a
, i
, bi
)
802 if (to_remove
!= -1U)
804 bitmap_clear_bit (&a
->expressions
, to_remove
);
807 pre_expr expr
= expression_for_id (i
);
808 if (! bitmap_bit_p (&a
->values
, get_expr_value_id (expr
)))
811 if (to_remove
!= -1U)
812 bitmap_clear_bit (&a
->expressions
, to_remove
);
816 /* Return true if bitmapped set SET contains the value VALUE_ID. */
819 bitmap_set_contains_value (bitmap_set_t set
, unsigned int value_id
)
821 if (value_id_constant_p (value_id
))
824 return bitmap_bit_p (&set
->values
, value_id
);
828 bitmap_set_contains_expr (bitmap_set_t set
, const pre_expr expr
)
830 return bitmap_bit_p (&set
->expressions
, get_expression_id (expr
));
833 /* Return true if two bitmap sets are equal. */
836 bitmap_set_equal (bitmap_set_t a
, bitmap_set_t b
)
838 return bitmap_equal_p (&a
->values
, &b
->values
);
841 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
842 and add it otherwise. */
845 bitmap_value_replace_in_set (bitmap_set_t set
, pre_expr expr
)
847 unsigned int val
= get_expr_value_id (expr
);
848 if (value_id_constant_p (val
))
851 if (bitmap_set_contains_value (set
, val
))
853 /* The number of expressions having a given value is usually
854 significantly less than the total number of expressions in SET.
855 Thus, rather than check, for each expression in SET, whether it
856 has the value LOOKFOR, we walk the reverse mapping that tells us
857 what expressions have a given value, and see if any of those
858 expressions are in our set. For large testcases, this is about
859 5-10x faster than walking the bitmap. If this is somehow a
860 significant lose for some cases, we can choose which set to walk
861 based on the set size. */
864 bitmap exprset
= value_expressions
[val
];
865 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
867 if (bitmap_clear_bit (&set
->expressions
, i
))
869 bitmap_set_bit (&set
->expressions
, get_expression_id (expr
));
876 bitmap_insert_into_set (set
, expr
);
879 /* Insert EXPR into SET if EXPR's value is not already present in
883 bitmap_value_insert_into_set (bitmap_set_t set
, pre_expr expr
)
885 unsigned int val
= get_expr_value_id (expr
);
887 gcc_checking_assert (expr
->id
== get_or_alloc_expression_id (expr
));
889 /* Constant values are always considered to be part of the set. */
890 if (value_id_constant_p (val
))
893 /* If the value membership changed, add the expression. */
894 if (bitmap_set_bit (&set
->values
, val
))
895 bitmap_set_bit (&set
->expressions
, expr
->id
);
898 /* Print out EXPR to outfile. */
901 print_pre_expr (FILE *outfile
, const pre_expr expr
)
905 fprintf (outfile
, "NULL");
911 print_generic_expr (outfile
, PRE_EXPR_CONSTANT (expr
));
914 print_generic_expr (outfile
, PRE_EXPR_NAME (expr
));
919 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
920 fprintf (outfile
, "{%s,", get_tree_code_name (nary
->opcode
));
921 for (i
= 0; i
< nary
->length
; i
++)
923 print_generic_expr (outfile
, nary
->op
[i
]);
924 if (i
!= (unsigned) nary
->length
- 1)
925 fprintf (outfile
, ",");
927 fprintf (outfile
, "}");
933 vn_reference_op_t vro
;
935 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
936 fprintf (outfile
, "{");
938 ref
->operands
.iterate (i
, &vro
);
941 bool closebrace
= false;
942 if (vro
->opcode
!= SSA_NAME
943 && TREE_CODE_CLASS (vro
->opcode
) != tcc_declaration
)
945 fprintf (outfile
, "%s", get_tree_code_name (vro
->opcode
));
948 fprintf (outfile
, "<");
954 print_generic_expr (outfile
, vro
->op0
);
957 fprintf (outfile
, ",");
958 print_generic_expr (outfile
, vro
->op1
);
962 fprintf (outfile
, ",");
963 print_generic_expr (outfile
, vro
->op2
);
967 fprintf (outfile
, ">");
968 if (i
!= ref
->operands
.length () - 1)
969 fprintf (outfile
, ",");
971 fprintf (outfile
, "}");
974 fprintf (outfile
, "@");
975 print_generic_expr (outfile
, ref
->vuse
);
981 void debug_pre_expr (pre_expr
);
983 /* Like print_pre_expr but always prints to stderr. */
985 debug_pre_expr (pre_expr e
)
987 print_pre_expr (stderr
, e
);
988 fprintf (stderr
, "\n");
991 /* Print out SET to OUTFILE. */
994 print_bitmap_set (FILE *outfile
, bitmap_set_t set
,
995 const char *setname
, int blockindex
)
997 fprintf (outfile
, "%s[%d] := { ", setname
, blockindex
);
1004 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
1006 const pre_expr expr
= expression_for_id (i
);
1009 fprintf (outfile
, ", ");
1011 print_pre_expr (outfile
, expr
);
1013 fprintf (outfile
, " (%04d)", get_expr_value_id (expr
));
1016 fprintf (outfile
, " }\n");
1019 void debug_bitmap_set (bitmap_set_t
);
1022 debug_bitmap_set (bitmap_set_t set
)
1024 print_bitmap_set (stderr
, set
, "debug", 0);
1027 void debug_bitmap_sets_for (basic_block
);
1030 debug_bitmap_sets_for (basic_block bb
)
1032 print_bitmap_set (stderr
, AVAIL_OUT (bb
), "avail_out", bb
->index
);
1033 print_bitmap_set (stderr
, EXP_GEN (bb
), "exp_gen", bb
->index
);
1034 print_bitmap_set (stderr
, PHI_GEN (bb
), "phi_gen", bb
->index
);
1035 print_bitmap_set (stderr
, TMP_GEN (bb
), "tmp_gen", bb
->index
);
1036 print_bitmap_set (stderr
, ANTIC_IN (bb
), "antic_in", bb
->index
);
1037 if (do_partial_partial
)
1038 print_bitmap_set (stderr
, PA_IN (bb
), "pa_in", bb
->index
);
1039 print_bitmap_set (stderr
, NEW_SETS (bb
), "new_sets", bb
->index
);
1042 /* Print out the expressions that have VAL to OUTFILE. */
1045 print_value_expressions (FILE *outfile
, unsigned int val
)
1047 bitmap set
= value_expressions
[val
];
1052 sprintf (s
, "%04d", val
);
1053 x
.expressions
= *set
;
1054 print_bitmap_set (outfile
, &x
, s
, 0);
1060 debug_value_expressions (unsigned int val
)
1062 print_value_expressions (stderr
, val
);
1065 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1069 get_or_alloc_expr_for_constant (tree constant
)
1071 unsigned int result_id
;
1072 unsigned int value_id
;
1073 struct pre_expr_d expr
;
1076 expr
.kind
= CONSTANT
;
1077 PRE_EXPR_CONSTANT (&expr
) = constant
;
1078 result_id
= lookup_expression_id (&expr
);
1080 return expression_for_id (result_id
);
1082 newexpr
= pre_expr_pool
.allocate ();
1083 newexpr
->kind
= CONSTANT
;
1084 PRE_EXPR_CONSTANT (newexpr
) = constant
;
1085 alloc_expression_id (newexpr
);
1086 value_id
= get_or_alloc_constant_value_id (constant
);
1087 add_to_value (value_id
, newexpr
);
1091 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1092 Currently only supports constants and SSA_NAMES. */
1094 get_or_alloc_expr_for (tree t
)
1096 if (TREE_CODE (t
) == SSA_NAME
)
1097 return get_or_alloc_expr_for_name (t
);
1098 else if (is_gimple_min_invariant (t
))
1099 return get_or_alloc_expr_for_constant (t
);
1103 /* Return the folded version of T if T, when folded, is a gimple
1104 min_invariant or an SSA name. Otherwise, return T. */
1107 fully_constant_expression (pre_expr e
)
1115 vn_nary_op_t nary
= PRE_EXPR_NARY (e
);
1116 tree res
= vn_nary_simplify (nary
);
1119 if (is_gimple_min_invariant (res
))
1120 return get_or_alloc_expr_for_constant (res
);
1121 if (TREE_CODE (res
) == SSA_NAME
)
1122 return get_or_alloc_expr_for_name (res
);
1127 vn_reference_t ref
= PRE_EXPR_REFERENCE (e
);
1129 if ((folded
= fully_constant_vn_reference_p (ref
)))
1130 return get_or_alloc_expr_for_constant (folded
);
1139 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1140 it has the value it would have in BLOCK. Set *SAME_VALID to true
1141 in case the new vuse doesn't change the value id of the OPERANDS. */
1144 translate_vuse_through_block (vec
<vn_reference_op_s
> operands
,
1145 alias_set_type set
, tree type
, tree vuse
,
1146 basic_block phiblock
,
1147 basic_block block
, bool *same_valid
)
1149 gimple
*phi
= SSA_NAME_DEF_STMT (vuse
);
1156 if (gimple_bb (phi
) != phiblock
)
1159 use_oracle
= ao_ref_init_from_vn_reference (&ref
, set
, type
, operands
);
1161 /* Use the alias-oracle to find either the PHI node in this block,
1162 the first VUSE used in this block that is equivalent to vuse or
1163 the first VUSE which definition in this block kills the value. */
1164 if (gimple_code (phi
) == GIMPLE_PHI
)
1165 e
= find_edge (block
, phiblock
);
1166 else if (use_oracle
)
1167 while (!stmt_may_clobber_ref_p_1 (phi
, &ref
))
1169 vuse
= gimple_vuse (phi
);
1170 phi
= SSA_NAME_DEF_STMT (vuse
);
1171 if (gimple_bb (phi
) != phiblock
)
1173 if (gimple_code (phi
) == GIMPLE_PHI
)
1175 e
= find_edge (block
, phiblock
);
1186 bitmap visited
= NULL
;
1188 /* Try to find a vuse that dominates this phi node by skipping
1189 non-clobbering statements. */
1190 vuse
= get_continuation_for_phi (phi
, &ref
, &cnt
, &visited
, false,
1193 BITMAP_FREE (visited
);
1199 /* If we didn't find any, the value ID can't stay the same,
1200 but return the translated vuse. */
1201 *same_valid
= false;
1202 vuse
= PHI_ARG_DEF (phi
, e
->dest_idx
);
1204 /* ??? We would like to return vuse here as this is the canonical
1205 upmost vdef that this reference is associated with. But during
1206 insertion of the references into the hash tables we only ever
1207 directly insert with their direct gimple_vuse, hence returning
1208 something else would make us not find the other expression. */
1209 return PHI_ARG_DEF (phi
, e
->dest_idx
);
1215 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1216 SET2 *or* SET3. This is used to avoid making a set consisting of the union
1217 of PA_IN and ANTIC_IN during insert and phi-translation. */
1219 static inline pre_expr
1220 find_leader_in_sets (unsigned int val
, bitmap_set_t set1
, bitmap_set_t set2
,
1221 bitmap_set_t set3
= NULL
)
1223 pre_expr result
= NULL
;
1226 result
= bitmap_find_leader (set1
, val
);
1227 if (!result
&& set2
)
1228 result
= bitmap_find_leader (set2
, val
);
1229 if (!result
&& set3
)
1230 result
= bitmap_find_leader (set3
, val
);
1234 /* Get the tree type for our PRE expression e. */
1237 get_expr_type (const pre_expr e
)
1242 return TREE_TYPE (PRE_EXPR_NAME (e
));
1244 return TREE_TYPE (PRE_EXPR_CONSTANT (e
));
1246 return PRE_EXPR_REFERENCE (e
)->type
;
1248 return PRE_EXPR_NARY (e
)->type
;
1253 /* Get a representative SSA_NAME for a given expression that is available in B.
1254 Since all of our sub-expressions are treated as values, we require
1255 them to be SSA_NAME's for simplicity.
1256 Prior versions of GVNPRE used to use "value handles" here, so that
1257 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1258 either case, the operands are really values (IE we do not expect
1259 them to be usable without finding leaders). */
1262 get_representative_for (const pre_expr e
, basic_block b
= NULL
)
1264 tree name
, valnum
= NULL_TREE
;
1265 unsigned int value_id
= get_expr_value_id (e
);
1270 return VN_INFO (PRE_EXPR_NAME (e
))->valnum
;
1272 return PRE_EXPR_CONSTANT (e
);
1276 /* Go through all of the expressions representing this value
1277 and pick out an SSA_NAME. */
1280 bitmap exprs
= value_expressions
[value_id
];
1281 EXECUTE_IF_SET_IN_BITMAP (exprs
, 0, i
, bi
)
1283 pre_expr rep
= expression_for_id (i
);
1284 if (rep
->kind
== NAME
)
1286 tree name
= PRE_EXPR_NAME (rep
);
1287 valnum
= VN_INFO (name
)->valnum
;
1288 gimple
*def
= SSA_NAME_DEF_STMT (name
);
1289 /* We have to return either a new representative or one
1290 that can be used for expression simplification and thus
1291 is available in B. */
1293 || gimple_nop_p (def
)
1294 || dominated_by_p (CDI_DOMINATORS
, b
, gimple_bb (def
)))
1297 else if (rep
->kind
== CONSTANT
)
1298 return PRE_EXPR_CONSTANT (rep
);
1304 /* If we reached here we couldn't find an SSA_NAME. This can
1305 happen when we've discovered a value that has never appeared in
1306 the program as set to an SSA_NAME, as the result of phi translation.
1308 ??? We should be able to re-use this when we insert the statement
1310 name
= make_temp_ssa_name (get_expr_type (e
), gimple_build_nop (), "pretmp");
1311 VN_INFO_GET (name
)->value_id
= value_id
;
1312 VN_INFO (name
)->valnum
= valnum
? valnum
: name
;
1313 /* ??? For now mark this SSA name for release by SCCVN. */
1314 VN_INFO (name
)->needs_insertion
= true;
1315 add_to_value (value_id
, get_or_alloc_expr_for_name (name
));
1316 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1318 fprintf (dump_file
, "Created SSA_NAME representative ");
1319 print_generic_expr (dump_file
, name
);
1320 fprintf (dump_file
, " for expression:");
1321 print_pre_expr (dump_file
, e
);
1322 fprintf (dump_file
, " (%04d)\n", value_id
);
1330 phi_translate (bitmap_set_t
, pre_expr
, bitmap_set_t
, bitmap_set_t
, edge
);
1332 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1333 the phis in PRED. Return NULL if we can't find a leader for each part
1334 of the translated expression. */
1337 phi_translate_1 (bitmap_set_t dest
,
1338 pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
, edge e
)
1340 basic_block pred
= e
->src
;
1341 basic_block phiblock
= e
->dest
;
1347 bool changed
= false;
1348 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1349 vn_nary_op_t newnary
= XALLOCAVAR (struct vn_nary_op_s
,
1350 sizeof_vn_nary_op (nary
->length
));
1351 memcpy (newnary
, nary
, sizeof_vn_nary_op (nary
->length
));
1353 for (i
= 0; i
< newnary
->length
; i
++)
1355 if (TREE_CODE (newnary
->op
[i
]) != SSA_NAME
)
1359 pre_expr leader
, result
;
1360 unsigned int op_val_id
= VN_INFO (newnary
->op
[i
])->value_id
;
1361 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1362 result
= phi_translate (dest
, leader
, set1
, set2
, e
);
1363 if (result
&& result
!= leader
)
1364 /* If op has a leader in the sets we translate make
1365 sure to use the value of the translated expression.
1366 We might need a new representative for that. */
1367 newnary
->op
[i
] = get_representative_for (result
, pred
);
1371 changed
|= newnary
->op
[i
] != nary
->op
[i
];
1377 unsigned int new_val_id
;
1379 PRE_EXPR_NARY (expr
) = newnary
;
1380 constant
= fully_constant_expression (expr
);
1381 PRE_EXPR_NARY (expr
) = nary
;
1382 if (constant
!= expr
)
1384 /* For non-CONSTANTs we have to make sure we can eventually
1385 insert the expression. Which means we need to have a
1387 if (constant
->kind
!= CONSTANT
)
1389 /* Do not allow simplifications to non-constants over
1390 backedges as this will likely result in a loop PHI node
1391 to be inserted and increased register pressure.
1392 See PR77498 - this avoids doing predcoms work in
1393 a less efficient way. */
1394 if (e
->flags
& EDGE_DFS_BACK
)
1398 unsigned value_id
= get_expr_value_id (constant
);
1399 /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1400 dest has what we computed into ANTIC_OUT sofar
1401 so pick from that - since topological sorting
1402 by sorted_array_from_bitmap_set isn't perfect
1403 we may lose some cases here. */
1404 constant
= find_leader_in_sets (value_id
, dest
,
1414 /* vn_nary_* do not valueize operands. */
1415 for (i
= 0; i
< newnary
->length
; ++i
)
1416 if (TREE_CODE (newnary
->op
[i
]) == SSA_NAME
)
1417 newnary
->op
[i
] = VN_INFO (newnary
->op
[i
])->valnum
;
1418 tree result
= vn_nary_op_lookup_pieces (newnary
->length
,
1423 if (result
&& is_gimple_min_invariant (result
))
1424 return get_or_alloc_expr_for_constant (result
);
1426 expr
= pre_expr_pool
.allocate ();
1431 PRE_EXPR_NARY (expr
) = nary
;
1432 new_val_id
= nary
->value_id
;
1433 get_or_alloc_expression_id (expr
);
1437 new_val_id
= get_next_value_id ();
1438 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
1439 nary
= vn_nary_op_insert_pieces (newnary
->length
,
1443 result
, new_val_id
);
1444 PRE_EXPR_NARY (expr
) = nary
;
1445 get_or_alloc_expression_id (expr
);
1447 add_to_value (new_val_id
, expr
);
1455 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1456 vec
<vn_reference_op_s
> operands
= ref
->operands
;
1457 tree vuse
= ref
->vuse
;
1458 tree newvuse
= vuse
;
1459 vec
<vn_reference_op_s
> newoperands
= vNULL
;
1460 bool changed
= false, same_valid
= true;
1462 vn_reference_op_t operand
;
1463 vn_reference_t newref
;
1465 for (i
= 0; operands
.iterate (i
, &operand
); i
++)
1470 tree type
= operand
->type
;
1471 vn_reference_op_s newop
= *operand
;
1472 op
[0] = operand
->op0
;
1473 op
[1] = operand
->op1
;
1474 op
[2] = operand
->op2
;
1475 for (n
= 0; n
< 3; ++n
)
1477 unsigned int op_val_id
;
1480 if (TREE_CODE (op
[n
]) != SSA_NAME
)
1482 /* We can't possibly insert these. */
1484 && !is_gimple_min_invariant (op
[n
]))
1488 op_val_id
= VN_INFO (op
[n
])->value_id
;
1489 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1490 opresult
= phi_translate (dest
, leader
, set1
, set2
, e
);
1491 if (opresult
&& opresult
!= leader
)
1493 tree name
= get_representative_for (opresult
);
1494 changed
|= name
!= op
[n
];
1502 newoperands
.release ();
1507 if (!newoperands
.exists ())
1508 newoperands
= operands
.copy ();
1509 /* We may have changed from an SSA_NAME to a constant */
1510 if (newop
.opcode
== SSA_NAME
&& TREE_CODE (op
[0]) != SSA_NAME
)
1511 newop
.opcode
= TREE_CODE (op
[0]);
1516 newoperands
[i
] = newop
;
1518 gcc_checking_assert (i
== operands
.length ());
1522 newvuse
= translate_vuse_through_block (newoperands
.exists ()
1523 ? newoperands
: operands
,
1524 ref
->set
, ref
->type
,
1525 vuse
, phiblock
, pred
,
1527 if (newvuse
== NULL_TREE
)
1529 newoperands
.release ();
1534 if (changed
|| newvuse
!= vuse
)
1536 unsigned int new_val_id
;
1538 tree result
= vn_reference_lookup_pieces (newvuse
, ref
->set
,
1540 newoperands
.exists ()
1541 ? newoperands
: operands
,
1544 newoperands
.release ();
1546 /* We can always insert constants, so if we have a partial
1547 redundant constant load of another type try to translate it
1548 to a constant of appropriate type. */
1549 if (result
&& is_gimple_min_invariant (result
))
1552 if (!useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1554 tem
= fold_unary (VIEW_CONVERT_EXPR
, ref
->type
, result
);
1555 if (tem
&& !is_gimple_min_invariant (tem
))
1559 return get_or_alloc_expr_for_constant (tem
);
1562 /* If we'd have to convert things we would need to validate
1563 if we can insert the translated expression. So fail
1564 here for now - we cannot insert an alias with a different
1565 type in the VN tables either, as that would assert. */
1567 && !useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1569 else if (!result
&& newref
1570 && !useless_type_conversion_p (ref
->type
, newref
->type
))
1572 newoperands
.release ();
1576 expr
= pre_expr_pool
.allocate ();
1577 expr
->kind
= REFERENCE
;
1581 new_val_id
= newref
->value_id
;
1584 if (changed
|| !same_valid
)
1586 new_val_id
= get_next_value_id ();
1587 value_expressions
.safe_grow_cleared
1588 (get_max_value_id () + 1);
1591 new_val_id
= ref
->value_id
;
1592 if (!newoperands
.exists ())
1593 newoperands
= operands
.copy ();
1594 newref
= vn_reference_insert_pieces (newvuse
, ref
->set
,
1597 result
, new_val_id
);
1598 newoperands
= vNULL
;
1600 PRE_EXPR_REFERENCE (expr
) = newref
;
1601 get_or_alloc_expression_id (expr
);
1602 add_to_value (new_val_id
, expr
);
1604 newoperands
.release ();
1611 tree name
= PRE_EXPR_NAME (expr
);
1612 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1613 /* If the SSA name is defined by a PHI node in this block,
1615 if (gimple_code (def_stmt
) == GIMPLE_PHI
1616 && gimple_bb (def_stmt
) == phiblock
)
1618 tree def
= PHI_ARG_DEF (def_stmt
, e
->dest_idx
);
1620 /* Handle constant. */
1621 if (is_gimple_min_invariant (def
))
1622 return get_or_alloc_expr_for_constant (def
);
1624 return get_or_alloc_expr_for_name (def
);
1626 /* Otherwise return it unchanged - it will get removed if its
1627 value is not available in PREDs AVAIL_OUT set of expressions
1628 by the subtraction of TMP_GEN. */
1637 /* Wrapper around phi_translate_1 providing caching functionality. */
1640 phi_translate (bitmap_set_t dest
, pre_expr expr
,
1641 bitmap_set_t set1
, bitmap_set_t set2
, edge e
)
1643 expr_pred_trans_t slot
= NULL
;
1649 /* Constants contain no values that need translation. */
1650 if (expr
->kind
== CONSTANT
)
1653 if (value_id_constant_p (get_expr_value_id (expr
)))
1656 /* Don't add translations of NAMEs as those are cheap to translate. */
1657 if (expr
->kind
!= NAME
)
1659 if (phi_trans_add (&slot
, expr
, e
->src
))
1661 /* Store NULL for the value we want to return in the case of
1667 phitrans
= phi_translate_1 (dest
, expr
, set1
, set2
, e
);
1674 /* Remove failed translations again, they cause insert
1675 iteration to not pick up new opportunities reliably. */
1676 phi_translate_table
->remove_elt_with_hash (slot
, slot
->hashcode
);
1683 /* For each expression in SET, translate the values through phi nodes
1684 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1685 expressions in DEST. */
1688 phi_translate_set (bitmap_set_t dest
, bitmap_set_t set
, edge e
)
1690 vec
<pre_expr
> exprs
;
1694 if (gimple_seq_empty_p (phi_nodes (e
->dest
)))
1696 bitmap_set_copy (dest
, set
);
1700 exprs
= sorted_array_from_bitmap_set (set
);
1701 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
1703 pre_expr translated
;
1704 translated
= phi_translate (dest
, expr
, set
, NULL
, e
);
1708 bitmap_insert_into_set (dest
, translated
);
1713 /* Find the leader for a value (i.e., the name representing that
1714 value) in a given set, and return it. Return NULL if no leader
1718 bitmap_find_leader (bitmap_set_t set
, unsigned int val
)
1720 if (value_id_constant_p (val
))
1724 bitmap exprset
= value_expressions
[val
];
1726 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1728 pre_expr expr
= expression_for_id (i
);
1729 if (expr
->kind
== CONSTANT
)
1733 if (bitmap_set_contains_value (set
, val
))
1735 /* Rather than walk the entire bitmap of expressions, and see
1736 whether any of them has the value we are looking for, we look
1737 at the reverse mapping, which tells us the set of expressions
1738 that have a given value (IE value->expressions with that
1739 value) and see if any of those expressions are in our set.
1740 The number of expressions per value is usually significantly
1741 less than the number of expressions in the set. In fact, for
1742 large testcases, doing it this way is roughly 5-10x faster
1743 than walking the bitmap.
1744 If this is somehow a significant lose for some cases, we can
1745 choose which set to walk based on which set is smaller. */
1748 bitmap exprset
= value_expressions
[val
];
1750 EXECUTE_IF_AND_IN_BITMAP (exprset
, &set
->expressions
, 0, i
, bi
)
1751 return expression_for_id (i
);
1756 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1757 BLOCK by seeing if it is not killed in the block. Note that we are
1758 only determining whether there is a store that kills it. Because
1759 of the order in which clean iterates over values, we are guaranteed
1760 that altered operands will have caused us to be eliminated from the
1761 ANTIC_IN set already. */
1764 value_dies_in_block_x (pre_expr expr
, basic_block block
)
1766 tree vuse
= PRE_EXPR_REFERENCE (expr
)->vuse
;
1767 vn_reference_t refx
= PRE_EXPR_REFERENCE (expr
);
1769 gimple_stmt_iterator gsi
;
1770 unsigned id
= get_expression_id (expr
);
1777 /* Lookup a previously calculated result. */
1778 if (EXPR_DIES (block
)
1779 && bitmap_bit_p (EXPR_DIES (block
), id
* 2))
1780 return bitmap_bit_p (EXPR_DIES (block
), id
* 2 + 1);
1782 /* A memory expression {e, VUSE} dies in the block if there is a
1783 statement that may clobber e. If, starting statement walk from the
1784 top of the basic block, a statement uses VUSE there can be no kill
1785 inbetween that use and the original statement that loaded {e, VUSE},
1786 so we can stop walking. */
1787 ref
.base
= NULL_TREE
;
1788 for (gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1790 tree def_vuse
, def_vdef
;
1791 def
= gsi_stmt (gsi
);
1792 def_vuse
= gimple_vuse (def
);
1793 def_vdef
= gimple_vdef (def
);
1795 /* Not a memory statement. */
1799 /* Not a may-def. */
1802 /* A load with the same VUSE, we're done. */
1803 if (def_vuse
== vuse
)
1809 /* Init ref only if we really need it. */
1810 if (ref
.base
== NULL_TREE
1811 && !ao_ref_init_from_vn_reference (&ref
, refx
->set
, refx
->type
,
1817 /* If the statement may clobber expr, it dies. */
1818 if (stmt_may_clobber_ref_p_1 (def
, &ref
))
1825 /* Remember the result. */
1826 if (!EXPR_DIES (block
))
1827 EXPR_DIES (block
) = BITMAP_ALLOC (&grand_bitmap_obstack
);
1828 bitmap_set_bit (EXPR_DIES (block
), id
* 2);
1830 bitmap_set_bit (EXPR_DIES (block
), id
* 2 + 1);
1836 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1837 contains its value-id. */
1840 op_valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, tree op
)
1842 if (op
&& TREE_CODE (op
) == SSA_NAME
)
1844 unsigned int value_id
= VN_INFO (op
)->value_id
;
1845 if (!(bitmap_set_contains_value (set1
, value_id
)
1846 || (set2
&& bitmap_set_contains_value (set2
, value_id
))))
1852 /* Determine if the expression EXPR is valid in SET1 U SET2.
1853 ONLY SET2 CAN BE NULL.
1854 This means that we have a leader for each part of the expression
1855 (if it consists of values), or the expression is an SSA_NAME.
1856 For loads/calls, we also see if the vuse is killed in this block. */
1859 valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, pre_expr expr
)
1864 /* By construction all NAMEs are available. Non-available
1865 NAMEs are removed by subtracting TMP_GEN from the sets. */
1870 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1871 for (i
= 0; i
< nary
->length
; i
++)
1872 if (!op_valid_in_sets (set1
, set2
, nary
->op
[i
]))
1879 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1880 vn_reference_op_t vro
;
1883 FOR_EACH_VEC_ELT (ref
->operands
, i
, vro
)
1885 if (!op_valid_in_sets (set1
, set2
, vro
->op0
)
1886 || !op_valid_in_sets (set1
, set2
, vro
->op1
)
1887 || !op_valid_in_sets (set1
, set2
, vro
->op2
))
1897 /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
1898 This means expressions that are made up of values we have no leaders for
1902 clean (bitmap_set_t set1
, bitmap_set_t set2
= NULL
)
1904 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set1
);
1908 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
1910 if (!valid_in_sets (set1
, set2
, expr
))
1912 unsigned int val
= get_expr_value_id (expr
);
1913 bitmap_clear_bit (&set1
->expressions
, get_expression_id (expr
));
1914 /* We are entered with possibly multiple expressions for a value
1915 so before removing a value from the set see if there's an
1916 expression for it left. */
1917 if (! bitmap_find_leader (set1
, val
))
1918 bitmap_clear_bit (&set1
->values
, val
);
1924 /* Clean the set of expressions that are no longer valid in SET because
1925 they are clobbered in BLOCK or because they trap and may not be executed. */
1928 prune_clobbered_mems (bitmap_set_t set
, basic_block block
)
1932 unsigned to_remove
= -1U;
1933 bool any_removed
= false;
1935 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
1937 /* Remove queued expr. */
1938 if (to_remove
!= -1U)
1940 bitmap_clear_bit (&set
->expressions
, to_remove
);
1945 pre_expr expr
= expression_for_id (i
);
1946 if (expr
->kind
== REFERENCE
)
1948 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1951 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ref
->vuse
);
1952 if (!gimple_nop_p (def_stmt
)
1953 && ((gimple_bb (def_stmt
) != block
1954 && !dominated_by_p (CDI_DOMINATORS
,
1955 block
, gimple_bb (def_stmt
)))
1956 || (gimple_bb (def_stmt
) == block
1957 && value_dies_in_block_x (expr
, block
))))
1961 else if (expr
->kind
== NARY
)
1963 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1964 /* If the NARY may trap make sure the block does not contain
1965 a possible exit point.
1966 ??? This is overly conservative if we translate AVAIL_OUT
1967 as the available expression might be after the exit point. */
1968 if (BB_MAY_NOTRETURN (block
)
1969 && vn_nary_may_trap (nary
))
1974 /* Remove queued expr. */
1975 if (to_remove
!= -1U)
1977 bitmap_clear_bit (&set
->expressions
, to_remove
);
1981 /* Above we only removed expressions, now clean the set of values
1982 which no longer have any corresponding expression. We cannot
1983 clear the value at the time we remove an expression since there
1984 may be multiple expressions per value.
1985 If we'd queue possibly to be removed values we could use
1986 the bitmap_find_leader way to see if there's still an expression
1987 for it. For some ratio of to be removed values and number of
1988 values/expressions in the set this might be faster than rebuilding
1992 bitmap_clear (&set
->values
);
1993 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
1995 pre_expr expr
= expression_for_id (i
);
1996 unsigned int value_id
= get_expr_value_id (expr
);
1997 bitmap_set_bit (&set
->values
, value_id
);
2002 static sbitmap has_abnormal_preds
;
2004 /* Compute the ANTIC set for BLOCK.
2006 If succs(BLOCK) > 1 then
2007 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2008 else if succs(BLOCK) == 1 then
2009 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2011 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2013 Note that clean() is deferred until after the iteration. */
2016 compute_antic_aux (basic_block block
, bool block_has_abnormal_pred_edge
)
2018 bitmap_set_t S
, old
, ANTIC_OUT
;
2022 bool was_visited
= BB_VISITED (block
);
2023 bool changed
= ! BB_VISITED (block
);
2024 BB_VISITED (block
) = 1;
2025 old
= ANTIC_OUT
= S
= NULL
;
2027 /* If any edges from predecessors are abnormal, antic_in is empty,
2029 if (block_has_abnormal_pred_edge
)
2030 goto maybe_dump_sets
;
2032 old
= ANTIC_IN (block
);
2033 ANTIC_OUT
= bitmap_set_new ();
2035 /* If the block has no successors, ANTIC_OUT is empty. */
2036 if (EDGE_COUNT (block
->succs
) == 0)
2038 /* If we have one successor, we could have some phi nodes to
2039 translate through. */
2040 else if (single_succ_p (block
))
2042 e
= single_succ_edge (block
);
2043 gcc_assert (BB_VISITED (e
->dest
));
2044 phi_translate_set (ANTIC_OUT
, ANTIC_IN (e
->dest
), e
);
2046 /* If we have multiple successors, we take the intersection of all of
2047 them. Note that in the case of loop exit phi nodes, we may have
2048 phis to translate through. */
2054 auto_vec
<edge
> worklist (EDGE_COUNT (block
->succs
));
2055 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2058 && BB_VISITED (e
->dest
))
2060 else if (BB_VISITED (e
->dest
))
2061 worklist
.quick_push (e
);
2064 /* Unvisited successors get their ANTIC_IN replaced by the
2065 maximal set to arrive at a maximum ANTIC_IN solution.
2066 We can ignore them in the intersection operation and thus
2067 need not explicitely represent that maximum solution. */
2068 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2069 fprintf (dump_file
, "ANTIC_IN is MAX on %d->%d\n",
2070 e
->src
->index
, e
->dest
->index
);
2074 /* Of multiple successors we have to have visited one already
2075 which is guaranteed by iteration order. */
2076 gcc_assert (first
!= NULL
);
2078 phi_translate_set (ANTIC_OUT
, ANTIC_IN (first
->dest
), first
);
2080 /* If we have multiple successors we need to intersect the ANTIC_OUT
2081 sets. For values that's a simple intersection but for
2082 expressions it is a union. Given we want to have a single
2083 expression per value in our sets we have to canonicalize.
2084 Avoid randomness and running into cycles like for PR82129 and
2085 canonicalize the expression we choose to the one with the
2086 lowest id. This requires we actually compute the union first. */
2087 FOR_EACH_VEC_ELT (worklist
, i
, e
)
2089 if (!gimple_seq_empty_p (phi_nodes (e
->dest
)))
2091 bitmap_set_t tmp
= bitmap_set_new ();
2092 phi_translate_set (tmp
, ANTIC_IN (e
->dest
), e
);
2093 bitmap_and_into (&ANTIC_OUT
->values
, &tmp
->values
);
2094 bitmap_ior_into (&ANTIC_OUT
->expressions
, &tmp
->expressions
);
2095 bitmap_set_free (tmp
);
2099 bitmap_and_into (&ANTIC_OUT
->values
, &ANTIC_IN (e
->dest
)->values
);
2100 bitmap_ior_into (&ANTIC_OUT
->expressions
,
2101 &ANTIC_IN (e
->dest
)->expressions
);
2104 if (! worklist
.is_empty ())
2106 /* Prune expressions not in the value set. */
2109 unsigned int to_clear
= -1U;
2110 FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT
, i
, bi
)
2112 if (to_clear
!= -1U)
2114 bitmap_clear_bit (&ANTIC_OUT
->expressions
, to_clear
);
2117 pre_expr expr
= expression_for_id (i
);
2118 unsigned int value_id
= get_expr_value_id (expr
);
2119 if (!bitmap_bit_p (&ANTIC_OUT
->values
, value_id
))
2122 if (to_clear
!= -1U)
2123 bitmap_clear_bit (&ANTIC_OUT
->expressions
, to_clear
);
2127 /* Prune expressions that are clobbered in block and thus become
2128 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2129 prune_clobbered_mems (ANTIC_OUT
, block
);
2131 /* Generate ANTIC_OUT - TMP_GEN. */
2132 S
= bitmap_set_subtract_expressions (ANTIC_OUT
, TMP_GEN (block
));
2134 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2135 ANTIC_IN (block
) = bitmap_set_subtract_expressions (EXP_GEN (block
),
2138 /* Then union in the ANTIC_OUT - TMP_GEN values,
2139 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2140 bitmap_ior_into (&ANTIC_IN (block
)->values
, &S
->values
);
2141 bitmap_ior_into (&ANTIC_IN (block
)->expressions
, &S
->expressions
);
2143 /* clean (ANTIC_IN (block)) is defered to after the iteration converged
2144 because it can cause non-convergence, see for example PR81181. */
2146 /* Intersect ANTIC_IN with the old ANTIC_IN. This is required until
2147 we properly represent the maximum expression set, thus not prune
2148 values without expressions during the iteration. */
2150 && bitmap_and_into (&ANTIC_IN (block
)->values
, &old
->values
))
2152 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2153 fprintf (dump_file
, "warning: intersecting with old ANTIC_IN "
2154 "shrinks the set\n");
2155 /* Prune expressions not in the value set. */
2158 unsigned int to_clear
= -1U;
2159 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block
), i
, bi
)
2161 if (to_clear
!= -1U)
2163 bitmap_clear_bit (&ANTIC_IN (block
)->expressions
, to_clear
);
2166 pre_expr expr
= expression_for_id (i
);
2167 unsigned int value_id
= get_expr_value_id (expr
);
2168 if (!bitmap_bit_p (&ANTIC_IN (block
)->values
, value_id
))
2171 if (to_clear
!= -1U)
2172 bitmap_clear_bit (&ANTIC_IN (block
)->expressions
, to_clear
);
2175 if (!bitmap_set_equal (old
, ANTIC_IN (block
)))
2179 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2182 print_bitmap_set (dump_file
, ANTIC_OUT
, "ANTIC_OUT", block
->index
);
2185 fprintf (dump_file
, "[changed] ");
2186 print_bitmap_set (dump_file
, ANTIC_IN (block
), "ANTIC_IN",
2190 print_bitmap_set (dump_file
, S
, "S", block
->index
);
2193 bitmap_set_free (old
);
2195 bitmap_set_free (S
);
2197 bitmap_set_free (ANTIC_OUT
);
2201 /* Compute PARTIAL_ANTIC for BLOCK.
2203 If succs(BLOCK) > 1 then
2204 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2205 in ANTIC_OUT for all succ(BLOCK)
2206 else if succs(BLOCK) == 1 then
2207 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2209 PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
2213 compute_partial_antic_aux (basic_block block
,
2214 bool block_has_abnormal_pred_edge
)
2216 bitmap_set_t old_PA_IN
;
2217 bitmap_set_t PA_OUT
;
2220 unsigned long max_pa
= PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH
);
2222 old_PA_IN
= PA_OUT
= NULL
;
2224 /* If any edges from predecessors are abnormal, antic_in is empty,
2226 if (block_has_abnormal_pred_edge
)
2227 goto maybe_dump_sets
;
2229 /* If there are too many partially anticipatable values in the
2230 block, phi_translate_set can take an exponential time: stop
2231 before the translation starts. */
2233 && single_succ_p (block
)
2234 && bitmap_count_bits (&PA_IN (single_succ (block
))->values
) > max_pa
)
2235 goto maybe_dump_sets
;
2237 old_PA_IN
= PA_IN (block
);
2238 PA_OUT
= bitmap_set_new ();
2240 /* If the block has no successors, ANTIC_OUT is empty. */
2241 if (EDGE_COUNT (block
->succs
) == 0)
2243 /* If we have one successor, we could have some phi nodes to
2244 translate through. Note that we can't phi translate across DFS
2245 back edges in partial antic, because it uses a union operation on
2246 the successors. For recurrences like IV's, we will end up
2247 generating a new value in the set on each go around (i + 3 (VH.1)
2248 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2249 else if (single_succ_p (block
))
2251 e
= single_succ_edge (block
);
2252 if (!(e
->flags
& EDGE_DFS_BACK
))
2253 phi_translate_set (PA_OUT
, PA_IN (e
->dest
), e
);
2255 /* If we have multiple successors, we take the union of all of
2261 auto_vec
<edge
> worklist (EDGE_COUNT (block
->succs
));
2262 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2264 if (e
->flags
& EDGE_DFS_BACK
)
2266 worklist
.quick_push (e
);
2268 if (worklist
.length () > 0)
2270 FOR_EACH_VEC_ELT (worklist
, i
, e
)
2275 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e
->dest
), i
, bi
)
2276 bitmap_value_insert_into_set (PA_OUT
,
2277 expression_for_id (i
));
2278 if (!gimple_seq_empty_p (phi_nodes (e
->dest
)))
2280 bitmap_set_t pa_in
= bitmap_set_new ();
2281 phi_translate_set (pa_in
, PA_IN (e
->dest
), e
);
2282 FOR_EACH_EXPR_ID_IN_SET (pa_in
, i
, bi
)
2283 bitmap_value_insert_into_set (PA_OUT
,
2284 expression_for_id (i
));
2285 bitmap_set_free (pa_in
);
2288 FOR_EACH_EXPR_ID_IN_SET (PA_IN (e
->dest
), i
, bi
)
2289 bitmap_value_insert_into_set (PA_OUT
,
2290 expression_for_id (i
));
2295 /* Prune expressions that are clobbered in block and thus become
2296 invalid if translated from PA_OUT to PA_IN. */
2297 prune_clobbered_mems (PA_OUT
, block
);
2299 /* PA_IN starts with PA_OUT - TMP_GEN.
2300 Then we subtract things from ANTIC_IN. */
2301 PA_IN (block
) = bitmap_set_subtract_expressions (PA_OUT
, TMP_GEN (block
));
2303 /* For partial antic, we want to put back in the phi results, since
2304 we will properly avoid making them partially antic over backedges. */
2305 bitmap_ior_into (&PA_IN (block
)->values
, &PHI_GEN (block
)->values
);
2306 bitmap_ior_into (&PA_IN (block
)->expressions
, &PHI_GEN (block
)->expressions
);
2308 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2309 bitmap_set_subtract_values (PA_IN (block
), ANTIC_IN (block
));
2311 clean (PA_IN (block
), ANTIC_IN (block
));
2314 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2317 print_bitmap_set (dump_file
, PA_OUT
, "PA_OUT", block
->index
);
2319 print_bitmap_set (dump_file
, PA_IN (block
), "PA_IN", block
->index
);
2322 bitmap_set_free (old_PA_IN
);
2324 bitmap_set_free (PA_OUT
);
2327 /* Compute ANTIC and partial ANTIC sets. */
2330 compute_antic (void)
2332 bool changed
= true;
2333 int num_iterations
= 0;
2339 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2340 We pre-build the map of blocks with incoming abnormal edges here. */
2341 has_abnormal_preds
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
2342 bitmap_clear (has_abnormal_preds
);
2344 FOR_ALL_BB_FN (block
, cfun
)
2346 BB_VISITED (block
) = 0;
2348 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2349 if (e
->flags
& EDGE_ABNORMAL
)
2351 bitmap_set_bit (has_abnormal_preds
, block
->index
);
2355 /* While we are here, give empty ANTIC_IN sets to each block. */
2356 ANTIC_IN (block
) = bitmap_set_new ();
2357 if (do_partial_partial
)
2358 PA_IN (block
) = bitmap_set_new ();
2361 /* At the exit block we anticipate nothing. */
2362 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun
)) = 1;
2364 /* For ANTIC computation we need a postorder that also guarantees that
2365 a block with a single successor is visited after its successor.
2366 RPO on the inverted CFG has this property. */
2367 auto_vec
<int, 20> postorder
;
2368 inverted_post_order_compute (&postorder
);
2370 auto_sbitmap
worklist (last_basic_block_for_fn (cfun
) + 1);
2371 bitmap_clear (worklist
);
2372 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
2373 bitmap_set_bit (worklist
, e
->src
->index
);
2376 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2377 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2378 /* ??? We need to clear our PHI translation cache here as the
2379 ANTIC sets shrink and we restrict valid translations to
2380 those having operands with leaders in ANTIC. Same below
2381 for PA ANTIC computation. */
2384 for (i
= postorder
.length () - 1; i
>= 0; i
--)
2386 if (bitmap_bit_p (worklist
, postorder
[i
]))
2388 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2389 bitmap_clear_bit (worklist
, block
->index
);
2390 if (compute_antic_aux (block
,
2391 bitmap_bit_p (has_abnormal_preds
,
2394 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2395 bitmap_set_bit (worklist
, e
->src
->index
);
2400 /* Theoretically possible, but *highly* unlikely. */
2401 gcc_checking_assert (num_iterations
< 500);
2404 /* We have to clean after the dataflow problem converged as cleaning
2405 can cause non-convergence because it is based on expressions
2406 rather than values. */
2407 FOR_EACH_BB_FN (block
, cfun
)
2408 clean (ANTIC_IN (block
));
2410 statistics_histogram_event (cfun
, "compute_antic iterations",
2413 if (do_partial_partial
)
2415 /* For partial antic we ignore backedges and thus we do not need
2416 to perform any iteration when we process blocks in postorder. */
2418 = pre_and_rev_post_order_compute (NULL
, postorder
.address (), false);
2419 for (i
= postorder_num
- 1 ; i
>= 0; i
--)
2421 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2422 compute_partial_antic_aux (block
,
2423 bitmap_bit_p (has_abnormal_preds
,
2428 sbitmap_free (has_abnormal_preds
);
2432 /* Inserted expressions are placed onto this worklist, which is used
2433 for performing quick dead code elimination of insertions we made
2434 that didn't turn out to be necessary. */
2435 static bitmap inserted_exprs
;
2437 /* The actual worker for create_component_ref_by_pieces. */
2440 create_component_ref_by_pieces_1 (basic_block block
, vn_reference_t ref
,
2441 unsigned int *operand
, gimple_seq
*stmts
)
2443 vn_reference_op_t currop
= &ref
->operands
[*operand
];
2446 switch (currop
->opcode
)
2453 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2457 tree offset
= currop
->op0
;
2458 if (TREE_CODE (baseop
) == ADDR_EXPR
2459 && handled_component_p (TREE_OPERAND (baseop
, 0)))
2463 base
= get_addr_base_and_unit_offset (TREE_OPERAND (baseop
, 0),
2466 offset
= int_const_binop (PLUS_EXPR
, offset
,
2467 build_int_cst (TREE_TYPE (offset
),
2469 baseop
= build_fold_addr_expr (base
);
2471 genop
= build2 (MEM_REF
, currop
->type
, baseop
, offset
);
2472 MR_DEPENDENCE_CLIQUE (genop
) = currop
->clique
;
2473 MR_DEPENDENCE_BASE (genop
) = currop
->base
;
2474 REF_REVERSE_STORAGE_ORDER (genop
) = currop
->reverse
;
2478 case TARGET_MEM_REF
:
2480 tree genop0
= NULL_TREE
, genop1
= NULL_TREE
;
2481 vn_reference_op_t nextop
= &ref
->operands
[++*operand
];
2482 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2488 genop0
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2494 genop1
= find_or_generate_expression (block
, nextop
->op0
, stmts
);
2498 genop
= build5 (TARGET_MEM_REF
, currop
->type
,
2499 baseop
, currop
->op2
, genop0
, currop
->op1
, genop1
);
2501 MR_DEPENDENCE_CLIQUE (genop
) = currop
->clique
;
2502 MR_DEPENDENCE_BASE (genop
) = currop
->base
;
2509 gcc_assert (is_gimple_min_invariant (currop
->op0
));
2515 case VIEW_CONVERT_EXPR
:
2517 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2521 return fold_build1 (currop
->opcode
, currop
->type
, genop0
);
2524 case WITH_SIZE_EXPR
:
2526 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2530 tree genop1
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2533 return fold_build2 (currop
->opcode
, currop
->type
, genop0
, genop1
);
2538 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2542 tree op1
= currop
->op0
;
2543 tree op2
= currop
->op1
;
2544 tree t
= build3 (BIT_FIELD_REF
, currop
->type
, genop0
, op1
, op2
);
2545 REF_REVERSE_STORAGE_ORDER (t
) = currop
->reverse
;
2549 /* For array ref vn_reference_op's, operand 1 of the array ref
2550 is op0 of the reference op and operand 3 of the array ref is
2552 case ARRAY_RANGE_REF
:
2556 tree genop1
= currop
->op0
;
2557 tree genop2
= currop
->op1
;
2558 tree genop3
= currop
->op2
;
2559 genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2563 genop1
= find_or_generate_expression (block
, genop1
, stmts
);
2568 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (genop0
));
2569 /* Drop zero minimum index if redundant. */
2570 if (integer_zerop (genop2
)
2572 || integer_zerop (TYPE_MIN_VALUE (domain_type
))))
2576 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2583 tree elmt_type
= TREE_TYPE (TREE_TYPE (genop0
));
2584 /* We can't always put a size in units of the element alignment
2585 here as the element alignment may be not visible. See
2586 PR43783. Simply drop the element size for constant
2588 if (TREE_CODE (genop3
) == INTEGER_CST
2589 && TREE_CODE (TYPE_SIZE_UNIT (elmt_type
)) == INTEGER_CST
2590 && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type
)),
2591 (wi::to_offset (genop3
)
2592 * vn_ref_op_align_unit (currop
))))
2596 genop3
= find_or_generate_expression (block
, genop3
, stmts
);
2601 return build4 (currop
->opcode
, currop
->type
, genop0
, genop1
,
2608 tree genop2
= currop
->op1
;
2609 op0
= create_component_ref_by_pieces_1 (block
, ref
, operand
, stmts
);
2612 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2616 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2620 return fold_build3 (COMPONENT_REF
, TREE_TYPE (op1
), op0
, op1
, genop2
);
2625 genop
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2646 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2647 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2648 trying to rename aggregates into ssa form directly, which is a no no.
2650 Thus, this routine doesn't create temporaries, it just builds a
2651 single access expression for the array, calling
2652 find_or_generate_expression to build the innermost pieces.
2654 This function is a subroutine of create_expression_by_pieces, and
2655 should not be called on it's own unless you really know what you
2659 create_component_ref_by_pieces (basic_block block
, vn_reference_t ref
,
2662 unsigned int op
= 0;
2663 return create_component_ref_by_pieces_1 (block
, ref
, &op
, stmts
);
2666 /* Find a simple leader for an expression, or generate one using
2667 create_expression_by_pieces from a NARY expression for the value.
2668 BLOCK is the basic_block we are looking for leaders in.
2669 OP is the tree expression to find a leader for or generate.
2670 Returns the leader or NULL_TREE on failure. */
2673 find_or_generate_expression (basic_block block
, tree op
, gimple_seq
*stmts
)
2675 pre_expr expr
= get_or_alloc_expr_for (op
);
2676 unsigned int lookfor
= get_expr_value_id (expr
);
2677 pre_expr leader
= bitmap_find_leader (AVAIL_OUT (block
), lookfor
);
2680 if (leader
->kind
== NAME
)
2681 return PRE_EXPR_NAME (leader
);
2682 else if (leader
->kind
== CONSTANT
)
2683 return PRE_EXPR_CONSTANT (leader
);
2689 /* It must be a complex expression, so generate it recursively. Note
2690 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2691 where the insert algorithm fails to insert a required expression. */
2692 bitmap exprset
= value_expressions
[lookfor
];
2695 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
2697 pre_expr temp
= expression_for_id (i
);
2698 /* We cannot insert random REFERENCE expressions at arbitrary
2699 places. We can insert NARYs which eventually re-materializes
2700 its operand values. */
2701 if (temp
->kind
== NARY
)
2702 return create_expression_by_pieces (block
, temp
, stmts
,
2703 get_expr_type (expr
));
2710 /* Create an expression in pieces, so that we can handle very complex
2711 expressions that may be ANTIC, but not necessary GIMPLE.
2712 BLOCK is the basic block the expression will be inserted into,
2713 EXPR is the expression to insert (in value form)
2714 STMTS is a statement list to append the necessary insertions into.
2716 This function will die if we hit some value that shouldn't be
2717 ANTIC but is (IE there is no leader for it, or its components).
2718 The function returns NULL_TREE in case a different antic expression
2719 has to be inserted first.
2720 This function may also generate expressions that are themselves
2721 partially or fully redundant. Those that are will be either made
2722 fully redundant during the next iteration of insert (for partially
2723 redundant ones), or eliminated by eliminate (for fully redundant
2727 create_expression_by_pieces (basic_block block
, pre_expr expr
,
2728 gimple_seq
*stmts
, tree type
)
2732 gimple_seq forced_stmts
= NULL
;
2733 unsigned int value_id
;
2734 gimple_stmt_iterator gsi
;
2735 tree exprtype
= type
? type
: get_expr_type (expr
);
2741 /* We may hit the NAME/CONSTANT case if we have to convert types
2742 that value numbering saw through. */
2744 folded
= PRE_EXPR_NAME (expr
);
2745 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded
))
2747 if (useless_type_conversion_p (exprtype
, TREE_TYPE (folded
)))
2752 folded
= PRE_EXPR_CONSTANT (expr
);
2753 tree tem
= fold_convert (exprtype
, folded
);
2754 if (is_gimple_min_invariant (tem
))
2759 if (PRE_EXPR_REFERENCE (expr
)->operands
[0].opcode
== CALL_EXPR
)
2761 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2762 unsigned int operand
= 1;
2763 vn_reference_op_t currop
= &ref
->operands
[0];
2764 tree sc
= NULL_TREE
;
2765 tree fn
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2770 sc
= find_or_generate_expression (block
, currop
->op1
, stmts
);
2774 auto_vec
<tree
> args (ref
->operands
.length () - 1);
2775 while (operand
< ref
->operands
.length ())
2777 tree arg
= create_component_ref_by_pieces_1 (block
, ref
,
2781 args
.quick_push (arg
);
2783 gcall
*call
= gimple_build_call_vec (fn
, args
);
2785 gimple_call_set_chain (call
, sc
);
2786 tree forcedname
= make_ssa_name (currop
->type
);
2787 gimple_call_set_lhs (call
, forcedname
);
2788 /* There's no CCP pass after PRE which would re-compute alignment
2789 information so make sure we re-materialize this here. */
2790 if (gimple_call_builtin_p (call
, BUILT_IN_ASSUME_ALIGNED
)
2791 && args
.length () - 2 <= 1
2792 && tree_fits_uhwi_p (args
[1])
2793 && (args
.length () != 3 || tree_fits_uhwi_p (args
[2])))
2795 unsigned HOST_WIDE_INT halign
= tree_to_uhwi (args
[1]);
2796 unsigned HOST_WIDE_INT hmisalign
2797 = args
.length () == 3 ? tree_to_uhwi (args
[2]) : 0;
2798 if ((halign
& (halign
- 1)) == 0
2799 && (hmisalign
& ~(halign
- 1)) == 0)
2800 set_ptr_info_alignment (get_ptr_info (forcedname
),
2803 gimple_set_vuse (call
, BB_LIVE_VOP_ON_EXIT (block
));
2804 gimple_seq_add_stmt_without_update (&forced_stmts
, call
);
2805 folded
= forcedname
;
2809 folded
= create_component_ref_by_pieces (block
,
2810 PRE_EXPR_REFERENCE (expr
),
2814 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2815 newstmt
= gimple_build_assign (name
, folded
);
2816 gimple_seq_add_stmt_without_update (&forced_stmts
, newstmt
);
2817 gimple_set_vuse (newstmt
, BB_LIVE_VOP_ON_EXIT (block
));
2823 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2824 tree
*genop
= XALLOCAVEC (tree
, nary
->length
);
2826 for (i
= 0; i
< nary
->length
; ++i
)
2828 genop
[i
] = find_or_generate_expression (block
, nary
->op
[i
], stmts
);
2831 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2832 may have conversions stripped. */
2833 if (nary
->opcode
== POINTER_PLUS_EXPR
)
2836 genop
[i
] = gimple_convert (&forced_stmts
,
2837 nary
->type
, genop
[i
]);
2839 genop
[i
] = gimple_convert (&forced_stmts
,
2840 sizetype
, genop
[i
]);
2843 genop
[i
] = gimple_convert (&forced_stmts
,
2844 TREE_TYPE (nary
->op
[i
]), genop
[i
]);
2846 if (nary
->opcode
== CONSTRUCTOR
)
2848 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
2849 for (i
= 0; i
< nary
->length
; ++i
)
2850 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
, genop
[i
]);
2851 folded
= build_constructor (nary
->type
, elts
);
2852 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2853 newstmt
= gimple_build_assign (name
, folded
);
2854 gimple_seq_add_stmt_without_update (&forced_stmts
, newstmt
);
2859 switch (nary
->length
)
2862 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2866 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2867 genop
[0], genop
[1]);
2870 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2871 genop
[0], genop
[1], genop
[2]);
2883 folded
= gimple_convert (&forced_stmts
, exprtype
, folded
);
2885 /* If there is nothing to insert, return the simplified result. */
2886 if (gimple_seq_empty_p (forced_stmts
))
2888 /* If we simplified to a constant return it and discard eventually
2890 if (is_gimple_min_invariant (folded
))
2892 gimple_seq_discard (forced_stmts
);
2895 /* Likewise if we simplified to sth not queued for insertion. */
2897 gsi
= gsi_last (forced_stmts
);
2898 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
2900 gimple
*stmt
= gsi_stmt (gsi
);
2901 tree forcedname
= gimple_get_lhs (stmt
);
2902 if (forcedname
== folded
)
2910 gimple_seq_discard (forced_stmts
);
2913 gcc_assert (TREE_CODE (folded
) == SSA_NAME
);
2915 /* If we have any intermediate expressions to the value sets, add them
2916 to the value sets and chain them in the instruction stream. */
2919 gsi
= gsi_start (forced_stmts
);
2920 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
2922 gimple
*stmt
= gsi_stmt (gsi
);
2923 tree forcedname
= gimple_get_lhs (stmt
);
2926 if (forcedname
!= folded
)
2928 VN_INFO_GET (forcedname
)->valnum
= forcedname
;
2929 VN_INFO (forcedname
)->value_id
= get_next_value_id ();
2930 nameexpr
= get_or_alloc_expr_for_name (forcedname
);
2931 add_to_value (VN_INFO (forcedname
)->value_id
, nameexpr
);
2932 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2933 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2936 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (forcedname
));
2938 gimple_seq_add_seq (stmts
, forced_stmts
);
2943 /* Fold the last statement. */
2944 gsi
= gsi_last (*stmts
);
2945 if (fold_stmt_inplace (&gsi
))
2946 update_stmt (gsi_stmt (gsi
));
2948 /* Add a value number to the temporary.
2949 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2950 we are creating the expression by pieces, and this particular piece of
2951 the expression may have been represented. There is no harm in replacing
2953 value_id
= get_expr_value_id (expr
);
2954 VN_INFO_GET (name
)->value_id
= value_id
;
2955 VN_INFO (name
)->valnum
= sccvn_valnum_from_value_id (value_id
);
2956 if (VN_INFO (name
)->valnum
== NULL_TREE
)
2957 VN_INFO (name
)->valnum
= name
;
2958 gcc_assert (VN_INFO (name
)->valnum
!= NULL_TREE
);
2959 nameexpr
= get_or_alloc_expr_for_name (name
);
2960 add_to_value (value_id
, nameexpr
);
2961 if (NEW_SETS (block
))
2962 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2963 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2965 pre_stats
.insertions
++;
2966 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2968 fprintf (dump_file
, "Inserted ");
2969 print_gimple_stmt (dump_file
, gsi_stmt (gsi_last (*stmts
)), 0);
2970 fprintf (dump_file
, " in predecessor %d (%04d)\n",
2971 block
->index
, value_id
);
2978 /* Insert the to-be-made-available values of expression EXPRNUM for each
2979 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2980 merge the result with a phi node, given the same value number as
2981 NODE. Return true if we have inserted new stuff. */
2984 insert_into_preds_of_block (basic_block block
, unsigned int exprnum
,
2985 vec
<pre_expr
> avail
)
2987 pre_expr expr
= expression_for_id (exprnum
);
2989 unsigned int val
= get_expr_value_id (expr
);
2991 bool insertions
= false;
2996 tree type
= get_expr_type (expr
);
3000 /* Make sure we aren't creating an induction variable. */
3001 if (bb_loop_depth (block
) > 0 && EDGE_COUNT (block
->preds
) == 2)
3003 bool firstinsideloop
= false;
3004 bool secondinsideloop
= false;
3005 firstinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3006 EDGE_PRED (block
, 0)->src
);
3007 secondinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3008 EDGE_PRED (block
, 1)->src
);
3009 /* Induction variables only have one edge inside the loop. */
3010 if ((firstinsideloop
^ secondinsideloop
)
3011 && expr
->kind
!= REFERENCE
)
3013 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3014 fprintf (dump_file
, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3019 /* Make the necessary insertions. */
3020 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3022 gimple_seq stmts
= NULL
;
3025 eprime
= avail
[pred
->dest_idx
];
3026 builtexpr
= create_expression_by_pieces (bprime
, eprime
,
3028 gcc_assert (!(pred
->flags
& EDGE_ABNORMAL
));
3029 if (!gimple_seq_empty_p (stmts
))
3031 basic_block new_bb
= gsi_insert_seq_on_edge_immediate (pred
, stmts
);
3032 gcc_assert (! new_bb
);
3037 /* We cannot insert a PHI node if we failed to insert
3042 if (is_gimple_min_invariant (builtexpr
))
3043 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_constant (builtexpr
);
3045 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_name (builtexpr
);
3047 /* If we didn't want a phi node, and we made insertions, we still have
3048 inserted new stuff, and thus return true. If we didn't want a phi node,
3049 and didn't make insertions, we haven't added anything new, so return
3051 if (nophi
&& insertions
)
3053 else if (nophi
&& !insertions
)
3056 /* Now build a phi for the new variable. */
3057 temp
= make_temp_ssa_name (type
, NULL
, "prephitmp");
3058 phi
= create_phi_node (temp
, block
);
3060 VN_INFO_GET (temp
)->value_id
= val
;
3061 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3062 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3063 VN_INFO (temp
)->valnum
= temp
;
3064 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3065 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3067 pre_expr ae
= avail
[pred
->dest_idx
];
3068 gcc_assert (get_expr_type (ae
) == type
3069 || useless_type_conversion_p (type
, get_expr_type (ae
)));
3070 if (ae
->kind
== CONSTANT
)
3071 add_phi_arg (phi
, unshare_expr (PRE_EXPR_CONSTANT (ae
)),
3072 pred
, UNKNOWN_LOCATION
);
3074 add_phi_arg (phi
, PRE_EXPR_NAME (ae
), pred
, UNKNOWN_LOCATION
);
3077 newphi
= get_or_alloc_expr_for_name (temp
);
3078 add_to_value (val
, newphi
);
3080 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3081 this insertion, since we test for the existence of this value in PHI_GEN
3082 before proceeding with the partial redundancy checks in insert_aux.
3084 The value may exist in AVAIL_OUT, in particular, it could be represented
3085 by the expression we are trying to eliminate, in which case we want the
3086 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3089 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3090 this block, because if it did, it would have existed in our dominator's
3091 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3094 bitmap_insert_into_set (PHI_GEN (block
), newphi
);
3095 bitmap_value_replace_in_set (AVAIL_OUT (block
),
3097 bitmap_insert_into_set (NEW_SETS (block
),
3100 /* If we insert a PHI node for a conversion of another PHI node
3101 in the same basic-block try to preserve range information.
3102 This is important so that followup loop passes receive optimal
3103 number of iteration analysis results. See PR61743. */
3104 if (expr
->kind
== NARY
3105 && CONVERT_EXPR_CODE_P (expr
->u
.nary
->opcode
)
3106 && TREE_CODE (expr
->u
.nary
->op
[0]) == SSA_NAME
3107 && gimple_bb (SSA_NAME_DEF_STMT (expr
->u
.nary
->op
[0])) == block
3108 && INTEGRAL_TYPE_P (type
)
3109 && INTEGRAL_TYPE_P (TREE_TYPE (expr
->u
.nary
->op
[0]))
3110 && (TYPE_PRECISION (type
)
3111 >= TYPE_PRECISION (TREE_TYPE (expr
->u
.nary
->op
[0])))
3112 && SSA_NAME_RANGE_INFO (expr
->u
.nary
->op
[0]))
3115 if (get_range_info (expr
->u
.nary
->op
[0], &min
, &max
) == VR_RANGE
3116 && !wi::neg_p (min
, SIGNED
)
3117 && !wi::neg_p (max
, SIGNED
))
3118 /* Just handle extension and sign-changes of all-positive ranges. */
3119 set_range_info (temp
,
3120 SSA_NAME_RANGE_TYPE (expr
->u
.nary
->op
[0]),
3121 wide_int_storage::from (min
, TYPE_PRECISION (type
),
3123 wide_int_storage::from (max
, TYPE_PRECISION (type
),
3127 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3129 fprintf (dump_file
, "Created phi ");
3130 print_gimple_stmt (dump_file
, phi
, 0);
3131 fprintf (dump_file
, " in block %d (%04d)\n", block
->index
, val
);
3139 /* Perform insertion of partially redundant or hoistable values.
3140 For BLOCK, do the following:
3141 1. Propagate the NEW_SETS of the dominator into the current block.
3142 If the block has multiple predecessors,
3143 2a. Iterate over the ANTIC expressions for the block to see if
3144 any of them are partially redundant.
3145 2b. If so, insert them into the necessary predecessors to make
3146 the expression fully redundant.
3147 2c. Insert a new PHI merging the values of the predecessors.
3148 2d. Insert the new PHI, and the new expressions, into the
3150 If the block has multiple successors,
3151 3a. Iterate over the ANTIC values for the block to see if
3152 any of them are good candidates for hoisting.
3153 3b. If so, insert expressions computing the values in BLOCK,
3154 and add the new expressions into the NEW_SETS set.
3155 4. Recursively call ourselves on the dominator children of BLOCK.
3157 Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3158 do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3159 done in do_hoist_insertion.
3163 do_pre_regular_insertion (basic_block block
, basic_block dom
)
3165 bool new_stuff
= false;
3166 vec
<pre_expr
> exprs
;
3168 auto_vec
<pre_expr
> avail
;
3171 exprs
= sorted_array_from_bitmap_set (ANTIC_IN (block
));
3172 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3174 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3176 if (expr
->kind
== NARY
3177 || expr
->kind
== REFERENCE
)
3180 bool by_some
= false;
3181 bool cant_insert
= false;
3182 bool all_same
= true;
3183 pre_expr first_s
= NULL
;
3186 pre_expr eprime
= NULL
;
3188 pre_expr edoubleprime
= NULL
;
3189 bool do_insertion
= false;
3191 val
= get_expr_value_id (expr
);
3192 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3194 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3196 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3198 fprintf (dump_file
, "Found fully redundant value: ");
3199 print_pre_expr (dump_file
, expr
);
3200 fprintf (dump_file
, "\n");
3205 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3207 unsigned int vprime
;
3209 /* We should never run insertion for the exit block
3210 and so not come across fake pred edges. */
3211 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3213 /* We are looking at ANTIC_OUT of bprime. */
3214 eprime
= phi_translate (NULL
, expr
, ANTIC_IN (block
), NULL
, pred
);
3216 /* eprime will generally only be NULL if the
3217 value of the expression, translated
3218 through the PHI for this predecessor, is
3219 undefined. If that is the case, we can't
3220 make the expression fully redundant,
3221 because its value is undefined along a
3222 predecessor path. We can thus break out
3223 early because it doesn't matter what the
3224 rest of the results are. */
3227 avail
[pred
->dest_idx
] = NULL
;
3232 vprime
= get_expr_value_id (eprime
);
3233 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
),
3235 if (edoubleprime
== NULL
)
3237 avail
[pred
->dest_idx
] = eprime
;
3242 avail
[pred
->dest_idx
] = edoubleprime
;
3244 /* We want to perform insertions to remove a redundancy on
3245 a path in the CFG we want to optimize for speed. */
3246 if (optimize_edge_for_speed_p (pred
))
3247 do_insertion
= true;
3248 if (first_s
== NULL
)
3249 first_s
= edoubleprime
;
3250 else if (!pre_expr_d::equal (first_s
, edoubleprime
))
3254 /* If we can insert it, it's not the same value
3255 already existing along every predecessor, and
3256 it's defined by some predecessor, it is
3257 partially redundant. */
3258 if (!cant_insert
&& !all_same
&& by_some
)
3262 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3264 fprintf (dump_file
, "Skipping partial redundancy for "
3266 print_pre_expr (dump_file
, expr
);
3267 fprintf (dump_file
, " (%04d), no redundancy on to be "
3268 "optimized for speed edge\n", val
);
3271 else if (dbg_cnt (treepre_insert
))
3273 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3275 fprintf (dump_file
, "Found partial redundancy for "
3277 print_pre_expr (dump_file
, expr
);
3278 fprintf (dump_file
, " (%04d)\n",
3279 get_expr_value_id (expr
));
3281 if (insert_into_preds_of_block (block
,
3282 get_expression_id (expr
),
3287 /* If all edges produce the same value and that value is
3288 an invariant, then the PHI has the same value on all
3289 edges. Note this. */
3290 else if (!cant_insert
&& all_same
)
3292 gcc_assert (edoubleprime
->kind
== CONSTANT
3293 || edoubleprime
->kind
== NAME
);
3295 tree temp
= make_temp_ssa_name (get_expr_type (expr
),
3298 = gimple_build_assign (temp
,
3299 edoubleprime
->kind
== CONSTANT
?
3300 PRE_EXPR_CONSTANT (edoubleprime
) :
3301 PRE_EXPR_NAME (edoubleprime
));
3302 gimple_stmt_iterator gsi
= gsi_after_labels (block
);
3303 gsi_insert_before (&gsi
, assign
, GSI_NEW_STMT
);
3305 VN_INFO_GET (temp
)->value_id
= val
;
3306 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3307 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3308 VN_INFO (temp
)->valnum
= temp
;
3309 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3310 pre_expr newe
= get_or_alloc_expr_for_name (temp
);
3311 add_to_value (val
, newe
);
3312 bitmap_value_replace_in_set (AVAIL_OUT (block
), newe
);
3313 bitmap_insert_into_set (NEW_SETS (block
), newe
);
3323 /* Perform insertion for partially anticipatable expressions. There
3324 is only one case we will perform insertion for these. This case is
3325 if the expression is partially anticipatable, and fully available.
3326 In this case, we know that putting it earlier will enable us to
3327 remove the later computation. */
3330 do_pre_partial_partial_insertion (basic_block block
, basic_block dom
)
3332 bool new_stuff
= false;
3333 vec
<pre_expr
> exprs
;
3335 auto_vec
<pre_expr
> avail
;
3338 exprs
= sorted_array_from_bitmap_set (PA_IN (block
));
3339 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3341 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3343 if (expr
->kind
== NARY
3344 || expr
->kind
== REFERENCE
)
3348 bool cant_insert
= false;
3351 pre_expr eprime
= NULL
;
3354 val
= get_expr_value_id (expr
);
3355 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3357 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3360 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3362 unsigned int vprime
;
3363 pre_expr edoubleprime
;
3365 /* We should never run insertion for the exit block
3366 and so not come across fake pred edges. */
3367 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3369 eprime
= phi_translate (NULL
, expr
, ANTIC_IN (block
),
3370 PA_IN (block
), pred
);
3372 /* eprime will generally only be NULL if the
3373 value of the expression, translated
3374 through the PHI for this predecessor, is
3375 undefined. If that is the case, we can't
3376 make the expression fully redundant,
3377 because its value is undefined along a
3378 predecessor path. We can thus break out
3379 early because it doesn't matter what the
3380 rest of the results are. */
3383 avail
[pred
->dest_idx
] = NULL
;
3388 vprime
= get_expr_value_id (eprime
);
3389 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
), vprime
);
3390 avail
[pred
->dest_idx
] = edoubleprime
;
3391 if (edoubleprime
== NULL
)
3398 /* If we can insert it, it's not the same value
3399 already existing along every predecessor, and
3400 it's defined by some predecessor, it is
3401 partially redundant. */
3402 if (!cant_insert
&& by_all
)
3405 bool do_insertion
= false;
3407 /* Insert only if we can remove a later expression on a path
3408 that we want to optimize for speed.
3409 The phi node that we will be inserting in BLOCK is not free,
3410 and inserting it for the sake of !optimize_for_speed successor
3411 may cause regressions on the speed path. */
3412 FOR_EACH_EDGE (succ
, ei
, block
->succs
)
3414 if (bitmap_set_contains_value (PA_IN (succ
->dest
), val
)
3415 || bitmap_set_contains_value (ANTIC_IN (succ
->dest
), val
))
3417 if (optimize_edge_for_speed_p (succ
))
3418 do_insertion
= true;
3424 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3426 fprintf (dump_file
, "Skipping partial partial redundancy "
3428 print_pre_expr (dump_file
, expr
);
3429 fprintf (dump_file
, " (%04d), not (partially) anticipated "
3430 "on any to be optimized for speed edges\n", val
);
3433 else if (dbg_cnt (treepre_insert
))
3435 pre_stats
.pa_insert
++;
3436 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3438 fprintf (dump_file
, "Found partial partial redundancy "
3440 print_pre_expr (dump_file
, expr
);
3441 fprintf (dump_file
, " (%04d)\n",
3442 get_expr_value_id (expr
));
3444 if (insert_into_preds_of_block (block
,
3445 get_expression_id (expr
),
3457 /* Insert expressions in BLOCK to compute hoistable values up.
3458 Return TRUE if something was inserted, otherwise return FALSE.
3459 The caller has to make sure that BLOCK has at least two successors. */
3462 do_hoist_insertion (basic_block block
)
3466 bool new_stuff
= false;
3468 gimple_stmt_iterator last
;
3470 /* At least two successors, or else... */
3471 gcc_assert (EDGE_COUNT (block
->succs
) >= 2);
3473 /* Check that all successors of BLOCK are dominated by block.
3474 We could use dominated_by_p() for this, but actually there is a much
3475 quicker check: any successor that is dominated by BLOCK can't have
3476 more than one predecessor edge. */
3477 FOR_EACH_EDGE (e
, ei
, block
->succs
)
3478 if (! single_pred_p (e
->dest
))
3481 /* Determine the insertion point. If we cannot safely insert before
3482 the last stmt if we'd have to, bail out. */
3483 last
= gsi_last_bb (block
);
3484 if (!gsi_end_p (last
)
3485 && !is_ctrl_stmt (gsi_stmt (last
))
3486 && stmt_ends_bb_p (gsi_stmt (last
)))
3489 /* Compute the set of hoistable expressions from ANTIC_IN. First compute
3490 hoistable values. */
3491 bitmap_set hoistable_set
;
3493 /* A hoistable value must be in ANTIC_IN(block)
3494 but not in AVAIL_OUT(BLOCK). */
3495 bitmap_initialize (&hoistable_set
.values
, &grand_bitmap_obstack
);
3496 bitmap_and_compl (&hoistable_set
.values
,
3497 &ANTIC_IN (block
)->values
, &AVAIL_OUT (block
)->values
);
3499 /* Short-cut for a common case: hoistable_set is empty. */
3500 if (bitmap_empty_p (&hoistable_set
.values
))
3503 /* Compute which of the hoistable values is in AVAIL_OUT of
3504 at least one of the successors of BLOCK. */
3505 bitmap_head availout_in_some
;
3506 bitmap_initialize (&availout_in_some
, &grand_bitmap_obstack
);
3507 FOR_EACH_EDGE (e
, ei
, block
->succs
)
3508 /* Do not consider expressions solely because their availability
3509 on loop exits. They'd be ANTIC-IN throughout the whole loop
3510 and thus effectively hoisted across loops by combination of
3511 PRE and hoisting. */
3512 if (! loop_exit_edge_p (block
->loop_father
, e
))
3513 bitmap_ior_and_into (&availout_in_some
, &hoistable_set
.values
,
3514 &AVAIL_OUT (e
->dest
)->values
);
3515 bitmap_clear (&hoistable_set
.values
);
3517 /* Short-cut for a common case: availout_in_some is empty. */
3518 if (bitmap_empty_p (&availout_in_some
))
3521 /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */
3522 hoistable_set
.values
= availout_in_some
;
3523 hoistable_set
.expressions
= ANTIC_IN (block
)->expressions
;
3525 /* Now finally construct the topological-ordered expression set. */
3526 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (&hoistable_set
);
3528 bitmap_clear (&hoistable_set
.values
);
3530 /* If there are candidate values for hoisting, insert expressions
3531 strategically to make the hoistable expressions fully redundant. */
3533 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3535 /* While we try to sort expressions topologically above the
3536 sorting doesn't work out perfectly. Catch expressions we
3537 already inserted. */
3538 unsigned int value_id
= get_expr_value_id (expr
);
3539 if (bitmap_set_contains_value (AVAIL_OUT (block
), value_id
))
3541 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3544 "Already inserted expression for ");
3545 print_pre_expr (dump_file
, expr
);
3546 fprintf (dump_file
, " (%04d)\n", value_id
);
3551 /* OK, we should hoist this value. Perform the transformation. */
3552 pre_stats
.hoist_insert
++;
3553 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3556 "Inserting expression in block %d for code hoisting: ",
3558 print_pre_expr (dump_file
, expr
);
3559 fprintf (dump_file
, " (%04d)\n", value_id
);
3562 gimple_seq stmts
= NULL
;
3563 tree res
= create_expression_by_pieces (block
, expr
, &stmts
,
3564 get_expr_type (expr
));
3566 /* Do not return true if expression creation ultimately
3567 did not insert any statements. */
3568 if (gimple_seq_empty_p (stmts
))
3572 if (gsi_end_p (last
) || is_ctrl_stmt (gsi_stmt (last
)))
3573 gsi_insert_seq_before (&last
, stmts
, GSI_SAME_STMT
);
3575 gsi_insert_seq_after (&last
, stmts
, GSI_NEW_STMT
);
3578 /* Make sure to not return true if expression creation ultimately
3579 failed but also make sure to insert any stmts produced as they
3580 are tracked in inserted_exprs. */
3592 /* Do a dominator walk on the control flow graph, and insert computations
3593 of values as necessary for PRE and hoisting. */
3596 insert_aux (basic_block block
, bool do_pre
, bool do_hoist
)
3599 bool new_stuff
= false;
3604 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3609 bitmap_set_t newset
;
3611 /* First, update the AVAIL_OUT set with anything we may have
3612 inserted higher up in the dominator tree. */
3613 newset
= NEW_SETS (dom
);
3616 /* Note that we need to value_replace both NEW_SETS, and
3617 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3618 represented by some non-simple expression here that we want
3619 to replace it with. */
3620 FOR_EACH_EXPR_ID_IN_SET (newset
, i
, bi
)
3622 pre_expr expr
= expression_for_id (i
);
3623 bitmap_value_replace_in_set (NEW_SETS (block
), expr
);
3624 bitmap_value_replace_in_set (AVAIL_OUT (block
), expr
);
3628 /* Insert expressions for partial redundancies. */
3629 if (do_pre
&& !single_pred_p (block
))
3631 new_stuff
|= do_pre_regular_insertion (block
, dom
);
3632 if (do_partial_partial
)
3633 new_stuff
|= do_pre_partial_partial_insertion (block
, dom
);
3636 /* Insert expressions for hoisting. */
3637 if (do_hoist
&& EDGE_COUNT (block
->succs
) >= 2)
3638 new_stuff
|= do_hoist_insertion (block
);
3641 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
3643 son
= next_dom_son (CDI_DOMINATORS
, son
))
3645 new_stuff
|= insert_aux (son
, do_pre
, do_hoist
);
3651 /* Perform insertion of partially redundant and hoistable values. */
3656 bool new_stuff
= true;
3658 int num_iterations
= 0;
3660 FOR_ALL_BB_FN (bb
, cfun
)
3661 NEW_SETS (bb
) = bitmap_set_new ();
3666 if (dump_file
&& dump_flags
& TDF_DETAILS
)
3667 fprintf (dump_file
, "Starting insert iteration %d\n", num_iterations
);
3668 new_stuff
= insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun
), flag_tree_pre
,
3669 flag_code_hoisting
);
3671 /* Clear the NEW sets before the next iteration. We have already
3672 fully propagated its contents. */
3674 FOR_ALL_BB_FN (bb
, cfun
)
3675 bitmap_set_free (NEW_SETS (bb
));
3677 statistics_histogram_event (cfun
, "insert iterations", num_iterations
);
3681 /* Compute the AVAIL set for all basic blocks.
3683 This function performs value numbering of the statements in each basic
3684 block. The AVAIL sets are built from information we glean while doing
3685 this value numbering, since the AVAIL sets contain only one entry per
3688 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3689 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3692 compute_avail (void)
3695 basic_block block
, son
;
3696 basic_block
*worklist
;
3701 /* We pretend that default definitions are defined in the entry block.
3702 This includes function arguments and the static chain decl. */
3703 FOR_EACH_SSA_NAME (i
, name
, cfun
)
3706 if (!SSA_NAME_IS_DEFAULT_DEF (name
)
3707 || has_zero_uses (name
)
3708 || virtual_operand_p (name
))
3711 e
= get_or_alloc_expr_for_name (name
);
3712 add_to_value (get_expr_value_id (e
), e
);
3713 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)), e
);
3714 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3718 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3720 print_bitmap_set (dump_file
, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3721 "tmp_gen", ENTRY_BLOCK
);
3722 print_bitmap_set (dump_file
, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3723 "avail_out", ENTRY_BLOCK
);
3726 /* Allocate the worklist. */
3727 worklist
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
3729 /* Seed the algorithm by putting the dominator children of the entry
3730 block on the worklist. */
3731 for (son
= first_dom_son (CDI_DOMINATORS
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3733 son
= next_dom_son (CDI_DOMINATORS
, son
))
3734 worklist
[sp
++] = son
;
3736 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun
))
3737 = ssa_default_def (cfun
, gimple_vop (cfun
));
3739 /* Loop until the worklist is empty. */
3745 /* Pick a block from the worklist. */
3746 block
= worklist
[--sp
];
3748 /* Initially, the set of available values in BLOCK is that of
3749 its immediate dominator. */
3750 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3753 bitmap_set_copy (AVAIL_OUT (block
), AVAIL_OUT (dom
));
3754 BB_LIVE_VOP_ON_EXIT (block
) = BB_LIVE_VOP_ON_EXIT (dom
);
3757 /* Generate values for PHI nodes. */
3758 for (gphi_iterator gsi
= gsi_start_phis (block
); !gsi_end_p (gsi
);
3761 tree result
= gimple_phi_result (gsi
.phi ());
3763 /* We have no need for virtual phis, as they don't represent
3764 actual computations. */
3765 if (virtual_operand_p (result
))
3767 BB_LIVE_VOP_ON_EXIT (block
) = result
;
3771 pre_expr e
= get_or_alloc_expr_for_name (result
);
3772 add_to_value (get_expr_value_id (e
), e
);
3773 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3774 bitmap_insert_into_set (PHI_GEN (block
), e
);
3777 BB_MAY_NOTRETURN (block
) = 0;
3779 /* Now compute value numbers and populate value sets with all
3780 the expressions computed in BLOCK. */
3781 for (gimple_stmt_iterator gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
);
3787 stmt
= gsi_stmt (gsi
);
3789 /* Cache whether the basic-block has any non-visible side-effect
3791 If this isn't a call or it is the last stmt in the
3792 basic-block then the CFG represents things correctly. */
3793 if (is_gimple_call (stmt
) && !stmt_ends_bb_p (stmt
))
3795 /* Non-looping const functions always return normally.
3796 Otherwise the call might not return or have side-effects
3797 that forbids hoisting possibly trapping expressions
3799 int flags
= gimple_call_flags (stmt
);
3800 if (!(flags
& ECF_CONST
)
3801 || (flags
& ECF_LOOPING_CONST_OR_PURE
))
3802 BB_MAY_NOTRETURN (block
) = 1;
3805 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
3807 pre_expr e
= get_or_alloc_expr_for_name (op
);
3809 add_to_value (get_expr_value_id (e
), e
);
3810 bitmap_insert_into_set (TMP_GEN (block
), e
);
3811 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3814 if (gimple_vdef (stmt
))
3815 BB_LIVE_VOP_ON_EXIT (block
) = gimple_vdef (stmt
);
3817 if (gimple_has_side_effects (stmt
)
3818 || stmt_could_throw_p (stmt
)
3819 || is_gimple_debug (stmt
))
3822 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
3824 if (ssa_undefined_value_p (op
))
3826 pre_expr e
= get_or_alloc_expr_for_name (op
);
3827 bitmap_value_insert_into_set (EXP_GEN (block
), e
);
3830 switch (gimple_code (stmt
))
3838 vn_reference_s ref1
;
3839 pre_expr result
= NULL
;
3841 /* We can value number only calls to real functions. */
3842 if (gimple_call_internal_p (stmt
))
3845 vn_reference_lookup_call (as_a
<gcall
*> (stmt
), &ref
, &ref1
);
3849 /* If the value of the call is not invalidated in
3850 this block until it is computed, add the expression
3852 if (!gimple_vuse (stmt
)
3854 (SSA_NAME_DEF_STMT (gimple_vuse (stmt
))) == GIMPLE_PHI
3855 || gimple_bb (SSA_NAME_DEF_STMT
3856 (gimple_vuse (stmt
))) != block
)
3858 result
= pre_expr_pool
.allocate ();
3859 result
->kind
= REFERENCE
;
3861 PRE_EXPR_REFERENCE (result
) = ref
;
3863 get_or_alloc_expression_id (result
);
3864 add_to_value (get_expr_value_id (result
), result
);
3865 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
3872 pre_expr result
= NULL
;
3873 switch (vn_get_stmt_kind (stmt
))
3877 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3880 /* COND_EXPR and VEC_COND_EXPR are awkward in
3881 that they contain an embedded complex expression.
3882 Don't even try to shove those through PRE. */
3883 if (code
== COND_EXPR
3884 || code
== VEC_COND_EXPR
)
3887 vn_nary_op_lookup_stmt (stmt
, &nary
);
3891 /* If the NARY traps and there was a preceding
3892 point in the block that might not return avoid
3893 adding the nary to EXP_GEN. */
3894 if (BB_MAY_NOTRETURN (block
)
3895 && vn_nary_may_trap (nary
))
3898 result
= pre_expr_pool
.allocate ();
3899 result
->kind
= NARY
;
3901 PRE_EXPR_NARY (result
) = nary
;
3907 tree rhs1
= gimple_assign_rhs1 (stmt
);
3908 alias_set_type set
= get_alias_set (rhs1
);
3909 vec
<vn_reference_op_s
> operands
3910 = vn_reference_operands_for_lookup (rhs1
);
3912 vn_reference_lookup_pieces (gimple_vuse (stmt
), set
,
3914 operands
, &ref
, VN_WALK
);
3917 operands
.release ();
3921 /* If the value of the reference is not invalidated in
3922 this block until it is computed, add the expression
3924 if (gimple_vuse (stmt
))
3928 def_stmt
= SSA_NAME_DEF_STMT (gimple_vuse (stmt
));
3929 while (!gimple_nop_p (def_stmt
)
3930 && gimple_code (def_stmt
) != GIMPLE_PHI
3931 && gimple_bb (def_stmt
) == block
)
3933 if (stmt_may_clobber_ref_p
3934 (def_stmt
, gimple_assign_rhs1 (stmt
)))
3940 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt
));
3944 operands
.release ();
3949 /* If the load was value-numbered to another
3950 load make sure we do not use its expression
3951 for insertion if it wouldn't be a valid
3953 /* At the momemt we have a testcase
3954 for hoist insertion of aligned vs. misaligned
3955 variants in gcc.dg/torture/pr65270-1.c thus
3956 with just alignment to be considered we can
3957 simply replace the expression in the hashtable
3958 with the most conservative one. */
3959 vn_reference_op_t ref1
= &ref
->operands
.last ();
3960 while (ref1
->opcode
!= TARGET_MEM_REF
3961 && ref1
->opcode
!= MEM_REF
3962 && ref1
!= &ref
->operands
[0])
3964 vn_reference_op_t ref2
= &operands
.last ();
3965 while (ref2
->opcode
!= TARGET_MEM_REF
3966 && ref2
->opcode
!= MEM_REF
3967 && ref2
!= &operands
[0])
3969 if ((ref1
->opcode
== TARGET_MEM_REF
3970 || ref1
->opcode
== MEM_REF
)
3971 && (TYPE_ALIGN (ref1
->type
)
3972 > TYPE_ALIGN (ref2
->type
)))
3974 = build_aligned_type (ref1
->type
,
3975 TYPE_ALIGN (ref2
->type
));
3976 /* TBAA behavior is an obvious part so make sure
3977 that the hashtable one covers this as well
3978 by adjusting the ref alias set and its base. */
3980 || alias_set_subset_of (set
, ref
->set
))
3982 else if (alias_set_subset_of (ref
->set
, set
))
3985 if (ref1
->opcode
== MEM_REF
)
3987 = wide_int_to_tree (TREE_TYPE (ref2
->op0
),
3988 wi::to_wide (ref1
->op0
));
3991 = wide_int_to_tree (TREE_TYPE (ref2
->op2
),
3992 wi::to_wide (ref1
->op2
));
3997 if (ref1
->opcode
== MEM_REF
)
3999 = wide_int_to_tree (ptr_type_node
,
4000 wi::to_wide (ref1
->op0
));
4003 = wide_int_to_tree (ptr_type_node
,
4004 wi::to_wide (ref1
->op2
));
4006 operands
.release ();
4008 result
= pre_expr_pool
.allocate ();
4009 result
->kind
= REFERENCE
;
4011 PRE_EXPR_REFERENCE (result
) = ref
;
4019 get_or_alloc_expression_id (result
);
4020 add_to_value (get_expr_value_id (result
), result
);
4021 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
4029 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4031 print_bitmap_set (dump_file
, EXP_GEN (block
),
4032 "exp_gen", block
->index
);
4033 print_bitmap_set (dump_file
, PHI_GEN (block
),
4034 "phi_gen", block
->index
);
4035 print_bitmap_set (dump_file
, TMP_GEN (block
),
4036 "tmp_gen", block
->index
);
4037 print_bitmap_set (dump_file
, AVAIL_OUT (block
),
4038 "avail_out", block
->index
);
4041 /* Put the dominator children of BLOCK on the worklist of blocks
4042 to compute available sets for. */
4043 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
4045 son
= next_dom_son (CDI_DOMINATORS
, son
))
4046 worklist
[sp
++] = son
;
4053 /* Initialize data structures used by PRE. */
4060 next_expression_id
= 1;
4061 expressions
.create (0);
4062 expressions
.safe_push (NULL
);
4063 value_expressions
.create (get_max_value_id () + 1);
4064 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
4065 name_to_id
.create (0);
4067 inserted_exprs
= BITMAP_ALLOC (NULL
);
4069 connect_infinite_loops_to_exit ();
4070 memset (&pre_stats
, 0, sizeof (pre_stats
));
4072 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets
));
4074 calculate_dominance_info (CDI_DOMINATORS
);
4076 bitmap_obstack_initialize (&grand_bitmap_obstack
);
4077 phi_translate_table
= new hash_table
<expr_pred_trans_d
> (5110);
4078 expression_to_id
= new hash_table
<pre_expr_d
> (num_ssa_names
* 3);
4079 FOR_ALL_BB_FN (bb
, cfun
)
4081 EXP_GEN (bb
) = bitmap_set_new ();
4082 PHI_GEN (bb
) = bitmap_set_new ();
4083 TMP_GEN (bb
) = bitmap_set_new ();
4084 AVAIL_OUT (bb
) = bitmap_set_new ();
4089 /* Deallocate data structures used by PRE. */
4094 value_expressions
.release ();
4095 expressions
.release ();
4096 BITMAP_FREE (inserted_exprs
);
4097 bitmap_obstack_release (&grand_bitmap_obstack
);
4098 bitmap_set_pool
.release ();
4099 pre_expr_pool
.release ();
4100 delete phi_translate_table
;
4101 phi_translate_table
= NULL
;
4102 delete expression_to_id
;
4103 expression_to_id
= NULL
;
4104 name_to_id
.release ();
4106 free_aux_for_blocks ();
4111 const pass_data pass_data_pre
=
4113 GIMPLE_PASS
, /* type */
4115 OPTGROUP_NONE
, /* optinfo_flags */
4116 TV_TREE_PRE
, /* tv_id */
4117 ( PROP_cfg
| PROP_ssa
), /* properties_required */
4118 0, /* properties_provided */
4119 0, /* properties_destroyed */
4120 TODO_rebuild_alias
, /* todo_flags_start */
4121 0, /* todo_flags_finish */
4124 class pass_pre
: public gimple_opt_pass
4127 pass_pre (gcc::context
*ctxt
)
4128 : gimple_opt_pass (pass_data_pre
, ctxt
)
4131 /* opt_pass methods: */
4132 virtual bool gate (function
*)
4133 { return flag_tree_pre
!= 0 || flag_code_hoisting
!= 0; }
4134 virtual unsigned int execute (function
*);
4136 }; // class pass_pre
4139 pass_pre::execute (function
*fun
)
4141 unsigned int todo
= 0;
4143 do_partial_partial
=
4144 flag_tree_partial_pre
&& optimize_function_for_speed_p (fun
);
4146 /* This has to happen before SCCVN runs because
4147 loop_optimizer_init may create new phis, etc. */
4148 loop_optimizer_init (LOOPS_NORMAL
);
4149 split_critical_edges ();
4152 run_scc_vn (VN_WALK
);
4156 /* Insert can get quite slow on an incredibly large number of basic
4157 blocks due to some quadratic behavior. Until this behavior is
4158 fixed, don't run it when he have an incredibly large number of
4159 bb's. If we aren't going to run insert, there is no point in
4160 computing ANTIC, either, even though it's plenty fast nor do
4161 we require AVAIL. */
4162 if (n_basic_blocks_for_fn (fun
) < 4000)
4169 /* Make sure to remove fake edges before committing our inserts.
4170 This makes sure we don't end up with extra critical edges that
4171 we would need to split. */
4172 remove_fake_exit_edges ();
4173 gsi_commit_edge_inserts ();
4175 /* Eliminate folds statements which might (should not...) end up
4176 not keeping virtual operands up-to-date. */
4177 gcc_assert (!need_ssa_update_p (fun
));
4179 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
4180 statistics_counter_event (fun
, "PA inserted", pre_stats
.pa_insert
);
4181 statistics_counter_event (fun
, "HOIST inserted", pre_stats
.hoist_insert
);
4182 statistics_counter_event (fun
, "New PHIs", pre_stats
.phis
);
4184 /* Remove all the redundant expressions. */
4185 todo
|= vn_eliminate (inserted_exprs
);
4187 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4188 to insert PHI nodes sometimes, and because value numbering of casts isn't
4189 perfect, we sometimes end up inserting dead code. This simple DCE-like
4190 pass removes any insertions we made that weren't actually used. */
4191 simple_dce_from_worklist (inserted_exprs
);
4196 loop_optimizer_finalize ();
4198 /* Restore SSA info before tail-merging as that resets it as well. */
4199 scc_vn_restore_ssa_info ();
4201 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4202 case we can merge the block with the remaining predecessor of the block.
4204 - call merge_blocks after each tail merge iteration
4205 - call merge_blocks after all tail merge iterations
4206 - mark TODO_cleanup_cfg when necessary
4207 - share the cfg cleanup with fini_pre. */
4208 todo
|= tail_merge_optimize (todo
);
4212 /* Tail merging invalidates the virtual SSA web, together with
4213 cfg-cleanup opportunities exposed by PRE this will wreck the
4214 SSA updating machinery. So make sure to run update-ssa
4215 manually, before eventually scheduling cfg-cleanup as part of
4217 update_ssa (TODO_update_ssa_only_virtuals
);
4225 make_pass_pre (gcc::context
*ctxt
)
4227 return new pass_pre (ctxt
);