1 /* Full and partial redundancy elimination and code hoisting on SSA GIMPLE.
2 Copyright (C) 2001-2017 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-ssa-loop.h"
43 #include "tree-into-ssa.h"
47 #include "tree-ssa-sccvn.h"
48 #include "tree-scalar-evolution.h"
52 #include "tree-ssa-propagate.h"
53 #include "ipa-utils.h"
54 #include "tree-cfgcleanup.h"
55 #include "langhooks.h"
58 /* Even though this file is called tree-ssa-pre.c, we actually
59 implement a bit more than just PRE here. All of them piggy-back
60 on GVN which is implemented in tree-ssa-sccvn.c.
62 1. Full Redundancy Elimination (FRE)
63 This is the elimination phase of GVN.
65 2. Partial Redundancy Elimination (PRE)
66 This is adds computation of AVAIL_OUT and ANTIC_IN and
67 doing expression insertion to form GVN-PRE.
70 This optimization uses the ANTIC_IN sets computed for PRE
71 to move expressions further up than PRE would do, to make
72 multiple computations of the same value fully redundant.
73 This pass is explained below (after the explanation of the
74 basic algorithm for PRE).
79 1. Avail sets can be shared by making an avail_find_leader that
80 walks up the dominator tree and looks in those avail sets.
81 This might affect code optimality, it's unclear right now.
82 Currently the AVAIL_OUT sets are the remaining quadraticness in
84 2. Strength reduction can be performed by anticipating expressions
85 we can repair later on.
86 3. We can do back-substitution or smarter value numbering to catch
87 commutative expressions split up over multiple statements.
90 /* For ease of terminology, "expression node" in the below refers to
91 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
92 represent the actual statement containing the expressions we care about,
93 and we cache the value number by putting it in the expression. */
95 /* Basic algorithm for Partial Redundancy Elimination:
97 First we walk the statements to generate the AVAIL sets, the
98 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
99 generation of values/expressions by a given block. We use them
100 when computing the ANTIC sets. The AVAIL sets consist of
101 SSA_NAME's that represent values, so we know what values are
102 available in what blocks. AVAIL is a forward dataflow problem. In
103 SSA, values are never killed, so we don't need a kill set, or a
104 fixpoint iteration, in order to calculate the AVAIL sets. In
105 traditional parlance, AVAIL sets tell us the downsafety of the
108 Next, we generate the ANTIC sets. These sets represent the
109 anticipatable expressions. ANTIC is a backwards dataflow
110 problem. An expression is anticipatable in a given block if it could
111 be generated in that block. This means that if we had to perform
112 an insertion in that block, of the value of that expression, we
113 could. Calculating the ANTIC sets requires phi translation of
114 expressions, because the flow goes backwards through phis. We must
115 iterate to a fixpoint of the ANTIC sets, because we have a kill
116 set. Even in SSA form, values are not live over the entire
117 function, only from their definition point onwards. So we have to
118 remove values from the ANTIC set once we go past the definition
119 point of the leaders that make them up.
120 compute_antic/compute_antic_aux performs this computation.
122 Third, we perform insertions to make partially redundant
123 expressions fully redundant.
125 An expression is partially redundant (excluding partial
128 1. It is AVAIL in some, but not all, of the predecessors of a
130 2. It is ANTIC in all the predecessors.
132 In order to make it fully redundant, we insert the expression into
133 the predecessors where it is not available, but is ANTIC.
135 When optimizing for size, we only eliminate the partial redundancy
136 if we need to insert in only one predecessor. This avoids almost
137 completely the code size increase that PRE usually causes.
139 For the partial anticipation case, we only perform insertion if it
140 is partially anticipated in some block, and fully available in all
143 do_pre_regular_insertion/do_pre_partial_partial_insertion
144 performs these steps, driven by insert/insert_aux.
146 Fourth, we eliminate fully redundant expressions.
147 This is a simple statement walk that replaces redundant
148 calculations with the now available values. */
150 /* Basic algorithm for Code Hoisting:
152 Code hoisting is: Moving value computations up in the control flow
153 graph to make multiple copies redundant. Typically this is a size
154 optimization, but there are cases where it also is helpful for speed.
156 A simple code hoisting algorithm is implemented that piggy-backs on
157 the PRE infrastructure. For code hoisting, we have to know ANTIC_OUT
158 which is effectively ANTIC_IN - AVAIL_OUT. The latter two have to be
159 computed for PRE, and we can use them to perform a limited version of
162 For the purpose of this implementation, a value is hoistable to a basic
163 block B if the following properties are met:
165 1. The value is in ANTIC_IN(B) -- the value will be computed on all
166 paths from B to function exit and it can be computed in B);
168 2. The value is not in AVAIL_OUT(B) -- there would be no need to
169 compute the value again and make it available twice;
171 3. All successors of B are dominated by B -- makes sure that inserting
172 a computation of the value in B will make the remaining
173 computations fully redundant;
175 4. At least one successor has the value in AVAIL_OUT -- to avoid
176 hoisting values up too far;
178 5. There are at least two successors of B -- hoisting in straight
179 line code is pointless.
181 The third condition is not strictly necessary, but it would complicate
182 the hoisting pass a lot. In fact, I don't know of any code hoisting
183 algorithm that does not have this requirement. Fortunately, experiments
184 have show that most candidate hoistable values are in regions that meet
185 this condition (e.g. diamond-shape regions).
187 The forth condition is necessary to avoid hoisting things up too far
188 away from the uses of the value. Nothing else limits the algorithm
189 from hoisting everything up as far as ANTIC_IN allows. Experiments
190 with SPEC and CSiBE have shown that hoisting up too far results in more
191 spilling, less benefits for code size, and worse benchmark scores.
192 Fortunately, in practice most of the interesting hoisting opportunities
193 are caught despite this limitation.
195 For hoistable values that meet all conditions, expressions are inserted
196 to make the calculation of the hoistable value fully redundant. We
197 perform code hoisting insertions after each round of PRE insertions,
198 because code hoisting never exposes new PRE opportunities, but PRE can
199 create new code hoisting opportunities.
201 The code hoisting algorithm is implemented in do_hoist_insert, driven
202 by insert/insert_aux. */
204 /* Representations of value numbers:
206 Value numbers are represented by a representative SSA_NAME. We
207 will create fake SSA_NAME's in situations where we need a
208 representative but do not have one (because it is a complex
209 expression). In order to facilitate storing the value numbers in
210 bitmaps, and keep the number of wasted SSA_NAME's down, we also
211 associate a value_id with each value number, and create full blown
212 ssa_name's only where we actually need them (IE in operands of
213 existing expressions).
215 Theoretically you could replace all the value_id's with
216 SSA_NAME_VERSION, but this would allocate a large number of
217 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
218 It would also require an additional indirection at each point we
221 /* Representation of expressions on value numbers:
223 Expressions consisting of value numbers are represented the same
224 way as our VN internally represents them, with an additional
225 "pre_expr" wrapping around them in order to facilitate storing all
226 of the expressions in the same sets. */
228 /* Representation of sets:
230 The dataflow sets do not need to be sorted in any particular order
231 for the majority of their lifetime, are simply represented as two
232 bitmaps, one that keeps track of values present in the set, and one
233 that keeps track of expressions present in the set.
235 When we need them in topological order, we produce it on demand by
236 transforming the bitmap into an array and sorting it into topo
239 /* Type of expression, used to know which member of the PRE_EXPR union
255 vn_reference_t reference
;
258 typedef struct pre_expr_d
: nofree_ptr_hash
<pre_expr_d
>
260 enum pre_expr_kind kind
;
264 /* hash_table support. */
265 static inline hashval_t
hash (const pre_expr_d
*);
266 static inline int equal (const pre_expr_d
*, const pre_expr_d
*);
269 #define PRE_EXPR_NAME(e) (e)->u.name
270 #define PRE_EXPR_NARY(e) (e)->u.nary
271 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
272 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
274 /* Compare E1 and E1 for equality. */
277 pre_expr_d::equal (const pre_expr_d
*e1
, const pre_expr_d
*e2
)
279 if (e1
->kind
!= e2
->kind
)
285 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1
),
286 PRE_EXPR_CONSTANT (e2
));
288 return PRE_EXPR_NAME (e1
) == PRE_EXPR_NAME (e2
);
290 return vn_nary_op_eq (PRE_EXPR_NARY (e1
), PRE_EXPR_NARY (e2
));
292 return vn_reference_eq (PRE_EXPR_REFERENCE (e1
),
293 PRE_EXPR_REFERENCE (e2
));
302 pre_expr_d::hash (const pre_expr_d
*e
)
307 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e
));
309 return SSA_NAME_VERSION (PRE_EXPR_NAME (e
));
311 return PRE_EXPR_NARY (e
)->hashcode
;
313 return PRE_EXPR_REFERENCE (e
)->hashcode
;
319 /* Next global expression id number. */
320 static unsigned int next_expression_id
;
322 /* Mapping from expression to id number we can use in bitmap sets. */
323 static vec
<pre_expr
> expressions
;
324 static hash_table
<pre_expr_d
> *expression_to_id
;
325 static vec
<unsigned> name_to_id
;
327 /* Allocate an expression id for EXPR. */
329 static inline unsigned int
330 alloc_expression_id (pre_expr expr
)
332 struct pre_expr_d
**slot
;
333 /* Make sure we won't overflow. */
334 gcc_assert (next_expression_id
+ 1 > next_expression_id
);
335 expr
->id
= next_expression_id
++;
336 expressions
.safe_push (expr
);
337 if (expr
->kind
== NAME
)
339 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
340 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
341 re-allocations by using vec::reserve upfront. */
342 unsigned old_len
= name_to_id
.length ();
343 name_to_id
.reserve (num_ssa_names
- old_len
);
344 name_to_id
.quick_grow_cleared (num_ssa_names
);
345 gcc_assert (name_to_id
[version
] == 0);
346 name_to_id
[version
] = expr
->id
;
350 slot
= expression_to_id
->find_slot (expr
, INSERT
);
354 return next_expression_id
- 1;
357 /* Return the expression id for tree EXPR. */
359 static inline unsigned int
360 get_expression_id (const pre_expr expr
)
365 static inline unsigned int
366 lookup_expression_id (const pre_expr expr
)
368 struct pre_expr_d
**slot
;
370 if (expr
->kind
== NAME
)
372 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
373 if (name_to_id
.length () <= version
)
375 return name_to_id
[version
];
379 slot
= expression_to_id
->find_slot (expr
, NO_INSERT
);
382 return ((pre_expr
)*slot
)->id
;
386 /* Return the existing expression id for EXPR, or create one if one
387 does not exist yet. */
389 static inline unsigned int
390 get_or_alloc_expression_id (pre_expr expr
)
392 unsigned int id
= lookup_expression_id (expr
);
394 return alloc_expression_id (expr
);
395 return expr
->id
= id
;
398 /* Return the expression that has expression id ID */
400 static inline pre_expr
401 expression_for_id (unsigned int id
)
403 return expressions
[id
];
406 /* Free the expression id field in all of our expressions,
407 and then destroy the expressions array. */
410 clear_expression_ids (void)
412 expressions
.release ();
415 static object_allocator
<pre_expr_d
> pre_expr_pool ("pre_expr nodes");
417 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
420 get_or_alloc_expr_for_name (tree name
)
422 struct pre_expr_d expr
;
424 unsigned int result_id
;
428 PRE_EXPR_NAME (&expr
) = name
;
429 result_id
= lookup_expression_id (&expr
);
431 return expression_for_id (result_id
);
433 result
= pre_expr_pool
.allocate ();
435 PRE_EXPR_NAME (result
) = name
;
436 alloc_expression_id (result
);
440 /* An unordered bitmap set. One bitmap tracks values, the other,
442 typedef struct bitmap_set
444 bitmap_head expressions
;
448 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
449 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
451 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
452 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
454 /* Mapping from value id to expressions with that value_id. */
455 static vec
<bitmap
> value_expressions
;
457 /* Sets that we need to keep track of. */
458 typedef struct bb_bitmap_sets
460 /* The EXP_GEN set, which represents expressions/values generated in
462 bitmap_set_t exp_gen
;
464 /* The PHI_GEN set, which represents PHI results generated in a
466 bitmap_set_t phi_gen
;
468 /* The TMP_GEN set, which represents results/temporaries generated
469 in a basic block. IE the LHS of an expression. */
470 bitmap_set_t tmp_gen
;
472 /* The AVAIL_OUT set, which represents which values are available in
473 a given basic block. */
474 bitmap_set_t avail_out
;
476 /* The ANTIC_IN set, which represents which values are anticipatable
477 in a given basic block. */
478 bitmap_set_t antic_in
;
480 /* The PA_IN set, which represents which values are
481 partially anticipatable in a given basic block. */
484 /* The NEW_SETS set, which is used during insertion to augment the
485 AVAIL_OUT set of blocks with the new insertions performed during
486 the current iteration. */
487 bitmap_set_t new_sets
;
489 /* A cache for value_dies_in_block_x. */
492 /* The live virtual operand on successor edges. */
495 /* True if we have visited this block during ANTIC calculation. */
496 unsigned int visited
: 1;
498 /* True when the block contains a call that might not return. */
499 unsigned int contains_may_not_return_call
: 1;
502 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
503 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
504 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
505 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
506 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
507 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
508 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
509 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
510 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
511 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
512 #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
515 /* This structure is used to keep track of statistics on what
516 optimization PRE was able to perform. */
519 /* The number of RHS computations eliminated by PRE. */
522 /* The number of new expressions/temporaries generated by PRE. */
525 /* The number of inserts found due to partial anticipation */
528 /* The number of inserts made for code hoisting. */
531 /* The number of new PHI nodes added by PRE. */
535 static bool do_partial_partial
;
536 static pre_expr
bitmap_find_leader (bitmap_set_t
, unsigned int);
537 static void bitmap_value_insert_into_set (bitmap_set_t
, pre_expr
);
538 static void bitmap_value_replace_in_set (bitmap_set_t
, pre_expr
);
539 static void bitmap_set_copy (bitmap_set_t
, bitmap_set_t
);
540 static void bitmap_set_and (bitmap_set_t
, bitmap_set_t
);
541 static bool bitmap_set_contains_value (bitmap_set_t
, unsigned int);
542 static void bitmap_insert_into_set (bitmap_set_t
, pre_expr
);
543 static void bitmap_insert_into_set_1 (bitmap_set_t
, pre_expr
,
545 static bitmap_set_t
bitmap_set_new (void);
546 static tree
create_expression_by_pieces (basic_block
, pre_expr
, gimple_seq
*,
548 static tree
find_or_generate_expression (basic_block
, tree
, gimple_seq
*);
549 static unsigned int get_expr_value_id (pre_expr
);
551 /* We can add and remove elements and entries to and from sets
552 and hash tables, so we use alloc pools for them. */
554 static object_allocator
<bitmap_set
> bitmap_set_pool ("Bitmap sets");
555 static bitmap_obstack grand_bitmap_obstack
;
557 /* Set of blocks with statements that have had their EH properties changed. */
558 static bitmap need_eh_cleanup
;
560 /* Set of blocks with statements that have had their AB properties changed. */
561 static bitmap need_ab_cleanup
;
563 /* A three tuple {e, pred, v} used to cache phi translations in the
564 phi_translate_table. */
566 typedef struct expr_pred_trans_d
: free_ptr_hash
<expr_pred_trans_d
>
568 /* The expression. */
571 /* The predecessor block along which we translated the expression. */
574 /* The value that resulted from the translation. */
577 /* The hashcode for the expression, pred pair. This is cached for
581 /* hash_table support. */
582 static inline hashval_t
hash (const expr_pred_trans_d
*);
583 static inline int equal (const expr_pred_trans_d
*, const expr_pred_trans_d
*);
584 } *expr_pred_trans_t
;
585 typedef const struct expr_pred_trans_d
*const_expr_pred_trans_t
;
588 expr_pred_trans_d::hash (const expr_pred_trans_d
*e
)
594 expr_pred_trans_d::equal (const expr_pred_trans_d
*ve1
,
595 const expr_pred_trans_d
*ve2
)
597 basic_block b1
= ve1
->pred
;
598 basic_block b2
= ve2
->pred
;
600 /* If they are not translations for the same basic block, they can't
604 return pre_expr_d::equal (ve1
->e
, ve2
->e
);
607 /* The phi_translate_table caches phi translations for a given
608 expression and predecessor. */
609 static hash_table
<expr_pred_trans_d
> *phi_translate_table
;
611 /* Add the tuple mapping from {expression E, basic block PRED} to
612 the phi translation table and return whether it pre-existed. */
615 phi_trans_add (expr_pred_trans_t
*entry
, pre_expr e
, basic_block pred
)
617 expr_pred_trans_t
*slot
;
618 expr_pred_trans_d tem
;
619 hashval_t hash
= iterative_hash_hashval_t (pre_expr_d::hash (e
),
624 slot
= phi_translate_table
->find_slot_with_hash (&tem
, hash
, INSERT
);
631 *entry
= *slot
= XNEW (struct expr_pred_trans_d
);
633 (*entry
)->pred
= pred
;
634 (*entry
)->hashcode
= hash
;
639 /* Add expression E to the expression set of value id V. */
642 add_to_value (unsigned int v
, pre_expr e
)
646 gcc_checking_assert (get_expr_value_id (e
) == v
);
648 if (v
>= value_expressions
.length ())
650 value_expressions
.safe_grow_cleared (v
+ 1);
653 set
= value_expressions
[v
];
656 set
= BITMAP_ALLOC (&grand_bitmap_obstack
);
657 value_expressions
[v
] = set
;
660 bitmap_set_bit (set
, get_or_alloc_expression_id (e
));
663 /* Create a new bitmap set and return it. */
666 bitmap_set_new (void)
668 bitmap_set_t ret
= bitmap_set_pool
.allocate ();
669 bitmap_initialize (&ret
->expressions
, &grand_bitmap_obstack
);
670 bitmap_initialize (&ret
->values
, &grand_bitmap_obstack
);
674 /* Return the value id for a PRE expression EXPR. */
677 get_expr_value_id (pre_expr expr
)
683 id
= get_constant_value_id (PRE_EXPR_CONSTANT (expr
));
686 id
= VN_INFO (PRE_EXPR_NAME (expr
))->value_id
;
689 id
= PRE_EXPR_NARY (expr
)->value_id
;
692 id
= PRE_EXPR_REFERENCE (expr
)->value_id
;
697 /* ??? We cannot assert that expr has a value-id (it can be 0), because
698 we assign value-ids only to expressions that have a result
699 in set_hashtable_value_ids. */
703 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
706 sccvn_valnum_from_value_id (unsigned int val
)
710 bitmap exprset
= value_expressions
[val
];
711 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
713 pre_expr vexpr
= expression_for_id (i
);
714 if (vexpr
->kind
== NAME
)
715 return VN_INFO (PRE_EXPR_NAME (vexpr
))->valnum
;
716 else if (vexpr
->kind
== CONSTANT
)
717 return PRE_EXPR_CONSTANT (vexpr
);
722 /* Remove an expression EXPR from a bitmapped set. */
725 bitmap_remove_from_set (bitmap_set_t set
, pre_expr expr
)
727 unsigned int val
= get_expr_value_id (expr
);
728 if (!value_id_constant_p (val
))
730 bitmap_clear_bit (&set
->values
, val
);
731 bitmap_clear_bit (&set
->expressions
, get_expression_id (expr
));
736 bitmap_insert_into_set_1 (bitmap_set_t set
, pre_expr expr
,
737 unsigned int val
, bool allow_constants
)
739 if (allow_constants
|| !value_id_constant_p (val
))
741 /* We specifically expect this and only this function to be able to
742 insert constants into a set. */
743 bitmap_set_bit (&set
->values
, val
);
744 bitmap_set_bit (&set
->expressions
, get_or_alloc_expression_id (expr
));
748 /* Insert an expression EXPR into a bitmapped set. */
751 bitmap_insert_into_set (bitmap_set_t set
, pre_expr expr
)
753 bitmap_insert_into_set_1 (set
, expr
, get_expr_value_id (expr
), false);
756 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
759 bitmap_set_copy (bitmap_set_t dest
, bitmap_set_t orig
)
761 bitmap_copy (&dest
->expressions
, &orig
->expressions
);
762 bitmap_copy (&dest
->values
, &orig
->values
);
766 /* Free memory used up by SET. */
768 bitmap_set_free (bitmap_set_t set
)
770 bitmap_clear (&set
->expressions
);
771 bitmap_clear (&set
->values
);
775 /* Generate an topological-ordered array of bitmap set SET. */
778 sorted_array_from_bitmap_set (bitmap_set_t set
)
781 bitmap_iterator bi
, bj
;
782 vec
<pre_expr
> result
;
784 /* Pre-allocate enough space for the array. */
785 result
.create (bitmap_count_bits (&set
->expressions
));
787 FOR_EACH_VALUE_ID_IN_SET (set
, i
, bi
)
789 /* The number of expressions having a given value is usually
790 relatively small. Thus, rather than making a vector of all
791 the expressions and sorting it by value-id, we walk the values
792 and check in the reverse mapping that tells us what expressions
793 have a given value, to filter those in our set. As a result,
794 the expressions are inserted in value-id order, which means
797 If this is somehow a significant lose for some cases, we can
798 choose which set to walk based on the set size. */
799 bitmap exprset
= value_expressions
[i
];
800 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, j
, bj
)
802 if (bitmap_bit_p (&set
->expressions
, j
))
803 result
.quick_push (expression_for_id (j
));
810 /* Perform bitmapped set operation DEST &= ORIG. */
813 bitmap_set_and (bitmap_set_t dest
, bitmap_set_t orig
)
821 bitmap_initialize (&temp
, &grand_bitmap_obstack
);
823 bitmap_and_into (&dest
->values
, &orig
->values
);
824 bitmap_copy (&temp
, &dest
->expressions
);
825 EXECUTE_IF_SET_IN_BITMAP (&temp
, 0, i
, bi
)
827 pre_expr expr
= expression_for_id (i
);
828 unsigned int value_id
= get_expr_value_id (expr
);
829 if (!bitmap_bit_p (&dest
->values
, value_id
))
830 bitmap_clear_bit (&dest
->expressions
, i
);
832 bitmap_clear (&temp
);
836 /* Subtract all values and expressions contained in ORIG from DEST. */
839 bitmap_set_subtract (bitmap_set_t dest
, bitmap_set_t orig
)
841 bitmap_set_t result
= bitmap_set_new ();
845 bitmap_and_compl (&result
->expressions
, &dest
->expressions
,
848 FOR_EACH_EXPR_ID_IN_SET (result
, i
, bi
)
850 pre_expr expr
= expression_for_id (i
);
851 unsigned int value_id
= get_expr_value_id (expr
);
852 bitmap_set_bit (&result
->values
, value_id
);
858 /* Subtract all the values in bitmap set B from bitmap set A. */
861 bitmap_set_subtract_values (bitmap_set_t a
, bitmap_set_t b
)
867 bitmap_initialize (&temp
, &grand_bitmap_obstack
);
869 bitmap_copy (&temp
, &a
->expressions
);
870 EXECUTE_IF_SET_IN_BITMAP (&temp
, 0, i
, bi
)
872 pre_expr expr
= expression_for_id (i
);
873 if (bitmap_set_contains_value (b
, get_expr_value_id (expr
)))
874 bitmap_remove_from_set (a
, expr
);
876 bitmap_clear (&temp
);
880 /* Return true if bitmapped set SET contains the value VALUE_ID. */
883 bitmap_set_contains_value (bitmap_set_t set
, unsigned int value_id
)
885 if (value_id_constant_p (value_id
))
888 if (!set
|| bitmap_empty_p (&set
->expressions
))
891 return bitmap_bit_p (&set
->values
, value_id
);
895 bitmap_set_contains_expr (bitmap_set_t set
, const pre_expr expr
)
897 return bitmap_bit_p (&set
->expressions
, get_expression_id (expr
));
900 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
903 bitmap_set_replace_value (bitmap_set_t set
, unsigned int lookfor
,
910 if (value_id_constant_p (lookfor
))
913 if (!bitmap_set_contains_value (set
, lookfor
))
916 /* The number of expressions having a given value is usually
917 significantly less than the total number of expressions in SET.
918 Thus, rather than check, for each expression in SET, whether it
919 has the value LOOKFOR, we walk the reverse mapping that tells us
920 what expressions have a given value, and see if any of those
921 expressions are in our set. For large testcases, this is about
922 5-10x faster than walking the bitmap. If this is somehow a
923 significant lose for some cases, we can choose which set to walk
924 based on the set size. */
925 exprset
= value_expressions
[lookfor
];
926 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
928 if (bitmap_clear_bit (&set
->expressions
, i
))
930 bitmap_set_bit (&set
->expressions
, get_expression_id (expr
));
938 /* Return true if two bitmap sets are equal. */
941 bitmap_set_equal (bitmap_set_t a
, bitmap_set_t b
)
943 return bitmap_equal_p (&a
->values
, &b
->values
);
946 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
947 and add it otherwise. */
950 bitmap_value_replace_in_set (bitmap_set_t set
, pre_expr expr
)
952 unsigned int val
= get_expr_value_id (expr
);
954 if (bitmap_set_contains_value (set
, val
))
955 bitmap_set_replace_value (set
, val
, expr
);
957 bitmap_insert_into_set (set
, expr
);
960 /* Insert EXPR into SET if EXPR's value is not already present in
964 bitmap_value_insert_into_set (bitmap_set_t set
, pre_expr expr
)
966 unsigned int val
= get_expr_value_id (expr
);
968 gcc_checking_assert (expr
->id
== get_or_alloc_expression_id (expr
));
970 /* Constant values are always considered to be part of the set. */
971 if (value_id_constant_p (val
))
974 /* If the value membership changed, add the expression. */
975 if (bitmap_set_bit (&set
->values
, val
))
976 bitmap_set_bit (&set
->expressions
, expr
->id
);
979 /* Print out EXPR to outfile. */
982 print_pre_expr (FILE *outfile
, const pre_expr expr
)
987 print_generic_expr (outfile
, PRE_EXPR_CONSTANT (expr
), 0);
990 print_generic_expr (outfile
, PRE_EXPR_NAME (expr
), 0);
995 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
996 fprintf (outfile
, "{%s,", get_tree_code_name (nary
->opcode
));
997 for (i
= 0; i
< nary
->length
; i
++)
999 print_generic_expr (outfile
, nary
->op
[i
], 0);
1000 if (i
!= (unsigned) nary
->length
- 1)
1001 fprintf (outfile
, ",");
1003 fprintf (outfile
, "}");
1009 vn_reference_op_t vro
;
1011 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1012 fprintf (outfile
, "{");
1014 ref
->operands
.iterate (i
, &vro
);
1017 bool closebrace
= false;
1018 if (vro
->opcode
!= SSA_NAME
1019 && TREE_CODE_CLASS (vro
->opcode
) != tcc_declaration
)
1021 fprintf (outfile
, "%s", get_tree_code_name (vro
->opcode
));
1024 fprintf (outfile
, "<");
1030 print_generic_expr (outfile
, vro
->op0
, 0);
1033 fprintf (outfile
, ",");
1034 print_generic_expr (outfile
, vro
->op1
, 0);
1038 fprintf (outfile
, ",");
1039 print_generic_expr (outfile
, vro
->op2
, 0);
1043 fprintf (outfile
, ">");
1044 if (i
!= ref
->operands
.length () - 1)
1045 fprintf (outfile
, ",");
1047 fprintf (outfile
, "}");
1050 fprintf (outfile
, "@");
1051 print_generic_expr (outfile
, ref
->vuse
, 0);
1057 void debug_pre_expr (pre_expr
);
1059 /* Like print_pre_expr but always prints to stderr. */
1061 debug_pre_expr (pre_expr e
)
1063 print_pre_expr (stderr
, e
);
1064 fprintf (stderr
, "\n");
1067 /* Print out SET to OUTFILE. */
1070 print_bitmap_set (FILE *outfile
, bitmap_set_t set
,
1071 const char *setname
, int blockindex
)
1073 fprintf (outfile
, "%s[%d] := { ", setname
, blockindex
);
1080 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
1082 const pre_expr expr
= expression_for_id (i
);
1085 fprintf (outfile
, ", ");
1087 print_pre_expr (outfile
, expr
);
1089 fprintf (outfile
, " (%04d)", get_expr_value_id (expr
));
1092 fprintf (outfile
, " }\n");
1095 void debug_bitmap_set (bitmap_set_t
);
1098 debug_bitmap_set (bitmap_set_t set
)
1100 print_bitmap_set (stderr
, set
, "debug", 0);
1103 void debug_bitmap_sets_for (basic_block
);
1106 debug_bitmap_sets_for (basic_block bb
)
1108 print_bitmap_set (stderr
, AVAIL_OUT (bb
), "avail_out", bb
->index
);
1109 print_bitmap_set (stderr
, EXP_GEN (bb
), "exp_gen", bb
->index
);
1110 print_bitmap_set (stderr
, PHI_GEN (bb
), "phi_gen", bb
->index
);
1111 print_bitmap_set (stderr
, TMP_GEN (bb
), "tmp_gen", bb
->index
);
1112 print_bitmap_set (stderr
, ANTIC_IN (bb
), "antic_in", bb
->index
);
1113 if (do_partial_partial
)
1114 print_bitmap_set (stderr
, PA_IN (bb
), "pa_in", bb
->index
);
1115 print_bitmap_set (stderr
, NEW_SETS (bb
), "new_sets", bb
->index
);
1118 /* Print out the expressions that have VAL to OUTFILE. */
1121 print_value_expressions (FILE *outfile
, unsigned int val
)
1123 bitmap set
= value_expressions
[val
];
1128 sprintf (s
, "%04d", val
);
1129 x
.expressions
= *set
;
1130 print_bitmap_set (outfile
, &x
, s
, 0);
1136 debug_value_expressions (unsigned int val
)
1138 print_value_expressions (stderr
, val
);
1141 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1145 get_or_alloc_expr_for_constant (tree constant
)
1147 unsigned int result_id
;
1148 unsigned int value_id
;
1149 struct pre_expr_d expr
;
1152 expr
.kind
= CONSTANT
;
1153 PRE_EXPR_CONSTANT (&expr
) = constant
;
1154 result_id
= lookup_expression_id (&expr
);
1156 return expression_for_id (result_id
);
1158 newexpr
= pre_expr_pool
.allocate ();
1159 newexpr
->kind
= CONSTANT
;
1160 PRE_EXPR_CONSTANT (newexpr
) = constant
;
1161 alloc_expression_id (newexpr
);
1162 value_id
= get_or_alloc_constant_value_id (constant
);
1163 add_to_value (value_id
, newexpr
);
1167 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1168 Currently only supports constants and SSA_NAMES. */
1170 get_or_alloc_expr_for (tree t
)
1172 if (TREE_CODE (t
) == SSA_NAME
)
1173 return get_or_alloc_expr_for_name (t
);
1174 else if (is_gimple_min_invariant (t
))
1175 return get_or_alloc_expr_for_constant (t
);
1178 /* More complex expressions can result from SCCVN expression
1179 simplification that inserts values for them. As they all
1180 do not have VOPs the get handled by the nary ops struct. */
1181 vn_nary_op_t result
;
1182 unsigned int result_id
;
1183 vn_nary_op_lookup (t
, &result
);
1186 pre_expr e
= pre_expr_pool
.allocate ();
1188 PRE_EXPR_NARY (e
) = result
;
1189 result_id
= lookup_expression_id (e
);
1192 pre_expr_pool
.remove (e
);
1193 e
= expression_for_id (result_id
);
1196 alloc_expression_id (e
);
1203 /* Return the folded version of T if T, when folded, is a gimple
1204 min_invariant or an SSA name. Otherwise, return T. */
1207 fully_constant_expression (pre_expr e
)
1215 vn_nary_op_t nary
= PRE_EXPR_NARY (e
);
1216 tree res
= vn_nary_simplify (nary
);
1219 if (is_gimple_min_invariant (res
))
1220 return get_or_alloc_expr_for_constant (res
);
1221 if (TREE_CODE (res
) == SSA_NAME
)
1222 return get_or_alloc_expr_for_name (res
);
1227 vn_reference_t ref
= PRE_EXPR_REFERENCE (e
);
1229 if ((folded
= fully_constant_vn_reference_p (ref
)))
1230 return get_or_alloc_expr_for_constant (folded
);
1239 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1240 it has the value it would have in BLOCK. Set *SAME_VALID to true
1241 in case the new vuse doesn't change the value id of the OPERANDS. */
1244 translate_vuse_through_block (vec
<vn_reference_op_s
> operands
,
1245 alias_set_type set
, tree type
, tree vuse
,
1246 basic_block phiblock
,
1247 basic_block block
, bool *same_valid
)
1249 gimple
*phi
= SSA_NAME_DEF_STMT (vuse
);
1256 if (gimple_bb (phi
) != phiblock
)
1259 use_oracle
= ao_ref_init_from_vn_reference (&ref
, set
, type
, operands
);
1261 /* Use the alias-oracle to find either the PHI node in this block,
1262 the first VUSE used in this block that is equivalent to vuse or
1263 the first VUSE which definition in this block kills the value. */
1264 if (gimple_code (phi
) == GIMPLE_PHI
)
1265 e
= find_edge (block
, phiblock
);
1266 else if (use_oracle
)
1267 while (!stmt_may_clobber_ref_p_1 (phi
, &ref
))
1269 vuse
= gimple_vuse (phi
);
1270 phi
= SSA_NAME_DEF_STMT (vuse
);
1271 if (gimple_bb (phi
) != phiblock
)
1273 if (gimple_code (phi
) == GIMPLE_PHI
)
1275 e
= find_edge (block
, phiblock
);
1286 bitmap visited
= NULL
;
1288 /* Try to find a vuse that dominates this phi node by skipping
1289 non-clobbering statements. */
1290 vuse
= get_continuation_for_phi (phi
, &ref
, &cnt
, &visited
, false,
1293 BITMAP_FREE (visited
);
1299 /* If we didn't find any, the value ID can't stay the same,
1300 but return the translated vuse. */
1301 *same_valid
= false;
1302 vuse
= PHI_ARG_DEF (phi
, e
->dest_idx
);
1304 /* ??? We would like to return vuse here as this is the canonical
1305 upmost vdef that this reference is associated with. But during
1306 insertion of the references into the hash tables we only ever
1307 directly insert with their direct gimple_vuse, hence returning
1308 something else would make us not find the other expression. */
1309 return PHI_ARG_DEF (phi
, e
->dest_idx
);
1315 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1316 SET2 *or* SET3. This is used to avoid making a set consisting of the union
1317 of PA_IN and ANTIC_IN during insert and phi-translation. */
1319 static inline pre_expr
1320 find_leader_in_sets (unsigned int val
, bitmap_set_t set1
, bitmap_set_t set2
,
1321 bitmap_set_t set3
= NULL
)
1325 result
= bitmap_find_leader (set1
, val
);
1326 if (!result
&& set2
)
1327 result
= bitmap_find_leader (set2
, val
);
1328 if (!result
&& set3
)
1329 result
= bitmap_find_leader (set3
, val
);
1333 /* Get the tree type for our PRE expression e. */
1336 get_expr_type (const pre_expr e
)
1341 return TREE_TYPE (PRE_EXPR_NAME (e
));
1343 return TREE_TYPE (PRE_EXPR_CONSTANT (e
));
1345 return PRE_EXPR_REFERENCE (e
)->type
;
1347 return PRE_EXPR_NARY (e
)->type
;
1352 /* Get a representative SSA_NAME for a given expression.
1353 Since all of our sub-expressions are treated as values, we require
1354 them to be SSA_NAME's for simplicity.
1355 Prior versions of GVNPRE used to use "value handles" here, so that
1356 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1357 either case, the operands are really values (IE we do not expect
1358 them to be usable without finding leaders). */
1361 get_representative_for (const pre_expr e
)
1364 unsigned int value_id
= get_expr_value_id (e
);
1369 return VN_INFO (PRE_EXPR_NAME (e
))->valnum
;
1371 return PRE_EXPR_CONSTANT (e
);
1375 /* Go through all of the expressions representing this value
1376 and pick out an SSA_NAME. */
1379 bitmap exprs
= value_expressions
[value_id
];
1380 EXECUTE_IF_SET_IN_BITMAP (exprs
, 0, i
, bi
)
1382 pre_expr rep
= expression_for_id (i
);
1383 if (rep
->kind
== NAME
)
1384 return VN_INFO (PRE_EXPR_NAME (rep
))->valnum
;
1385 else if (rep
->kind
== CONSTANT
)
1386 return PRE_EXPR_CONSTANT (rep
);
1392 /* If we reached here we couldn't find an SSA_NAME. This can
1393 happen when we've discovered a value that has never appeared in
1394 the program as set to an SSA_NAME, as the result of phi translation.
1396 ??? We should be able to re-use this when we insert the statement
1398 name
= make_temp_ssa_name (get_expr_type (e
), gimple_build_nop (), "pretmp");
1399 VN_INFO_GET (name
)->value_id
= value_id
;
1400 VN_INFO (name
)->valnum
= name
;
1401 /* ??? For now mark this SSA name for release by SCCVN. */
1402 VN_INFO (name
)->needs_insertion
= true;
1403 add_to_value (value_id
, get_or_alloc_expr_for_name (name
));
1404 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1406 fprintf (dump_file
, "Created SSA_NAME representative ");
1407 print_generic_expr (dump_file
, name
, 0);
1408 fprintf (dump_file
, " for expression:");
1409 print_pre_expr (dump_file
, e
);
1410 fprintf (dump_file
, " (%04d)\n", value_id
);
1419 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1420 basic_block pred
, basic_block phiblock
);
1422 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1423 the phis in PRED. Return NULL if we can't find a leader for each part
1424 of the translated expression. */
1427 phi_translate_1 (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1428 basic_block pred
, basic_block phiblock
)
1435 bool changed
= false;
1436 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1437 vn_nary_op_t newnary
= XALLOCAVAR (struct vn_nary_op_s
,
1438 sizeof_vn_nary_op (nary
->length
));
1439 memcpy (newnary
, nary
, sizeof_vn_nary_op (nary
->length
));
1441 for (i
= 0; i
< newnary
->length
; i
++)
1443 if (TREE_CODE (newnary
->op
[i
]) != SSA_NAME
)
1447 pre_expr leader
, result
;
1448 unsigned int op_val_id
= VN_INFO (newnary
->op
[i
])->value_id
;
1449 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1450 result
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1451 if (result
&& result
!= leader
)
1452 newnary
->op
[i
] = get_representative_for (result
);
1456 changed
|= newnary
->op
[i
] != nary
->op
[i
];
1462 unsigned int new_val_id
;
1464 PRE_EXPR_NARY (expr
) = newnary
;
1465 constant
= fully_constant_expression (expr
);
1466 PRE_EXPR_NARY (expr
) = nary
;
1467 if (constant
!= expr
)
1469 /* For non-CONSTANTs we have to make sure we can eventually
1470 insert the expression. Which means we need to have a
1472 if (constant
->kind
!= CONSTANT
)
1474 /* Do not allow simplifications to non-constants over
1475 backedges as this will likely result in a loop PHI node
1476 to be inserted and increased register pressure.
1477 See PR77498 - this avoids doing predcoms work in
1478 a less efficient way. */
1479 if (find_edge (pred
, phiblock
)->flags
& EDGE_DFS_BACK
)
1483 unsigned value_id
= get_expr_value_id (constant
);
1484 constant
= find_leader_in_sets (value_id
, set1
, set2
,
1494 tree result
= vn_nary_op_lookup_pieces (newnary
->length
,
1499 if (result
&& is_gimple_min_invariant (result
))
1500 return get_or_alloc_expr_for_constant (result
);
1502 expr
= pre_expr_pool
.allocate ();
1507 PRE_EXPR_NARY (expr
) = nary
;
1508 new_val_id
= nary
->value_id
;
1509 get_or_alloc_expression_id (expr
);
1513 new_val_id
= get_next_value_id ();
1514 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
1515 nary
= vn_nary_op_insert_pieces (newnary
->length
,
1519 result
, new_val_id
);
1520 PRE_EXPR_NARY (expr
) = nary
;
1521 get_or_alloc_expression_id (expr
);
1523 add_to_value (new_val_id
, expr
);
1531 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1532 vec
<vn_reference_op_s
> operands
= ref
->operands
;
1533 tree vuse
= ref
->vuse
;
1534 tree newvuse
= vuse
;
1535 vec
<vn_reference_op_s
> newoperands
= vNULL
;
1536 bool changed
= false, same_valid
= true;
1538 vn_reference_op_t operand
;
1539 vn_reference_t newref
;
1541 for (i
= 0; operands
.iterate (i
, &operand
); i
++)
1546 tree type
= operand
->type
;
1547 vn_reference_op_s newop
= *operand
;
1548 op
[0] = operand
->op0
;
1549 op
[1] = operand
->op1
;
1550 op
[2] = operand
->op2
;
1551 for (n
= 0; n
< 3; ++n
)
1553 unsigned int op_val_id
;
1556 if (TREE_CODE (op
[n
]) != SSA_NAME
)
1558 /* We can't possibly insert these. */
1560 && !is_gimple_min_invariant (op
[n
]))
1564 op_val_id
= VN_INFO (op
[n
])->value_id
;
1565 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1566 opresult
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1567 if (opresult
&& opresult
!= leader
)
1569 tree name
= get_representative_for (opresult
);
1570 changed
|= name
!= op
[n
];
1578 newoperands
.release ();
1583 if (!newoperands
.exists ())
1584 newoperands
= operands
.copy ();
1585 /* We may have changed from an SSA_NAME to a constant */
1586 if (newop
.opcode
== SSA_NAME
&& TREE_CODE (op
[0]) != SSA_NAME
)
1587 newop
.opcode
= TREE_CODE (op
[0]);
1592 newoperands
[i
] = newop
;
1594 gcc_checking_assert (i
== operands
.length ());
1598 newvuse
= translate_vuse_through_block (newoperands
.exists ()
1599 ? newoperands
: operands
,
1600 ref
->set
, ref
->type
,
1601 vuse
, phiblock
, pred
,
1603 if (newvuse
== NULL_TREE
)
1605 newoperands
.release ();
1610 if (changed
|| newvuse
!= vuse
)
1612 unsigned int new_val_id
;
1615 tree result
= vn_reference_lookup_pieces (newvuse
, ref
->set
,
1617 newoperands
.exists ()
1618 ? newoperands
: operands
,
1621 newoperands
.release ();
1623 /* We can always insert constants, so if we have a partial
1624 redundant constant load of another type try to translate it
1625 to a constant of appropriate type. */
1626 if (result
&& is_gimple_min_invariant (result
))
1629 if (!useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1631 tem
= fold_unary (VIEW_CONVERT_EXPR
, ref
->type
, result
);
1632 if (tem
&& !is_gimple_min_invariant (tem
))
1636 return get_or_alloc_expr_for_constant (tem
);
1639 /* If we'd have to convert things we would need to validate
1640 if we can insert the translated expression. So fail
1641 here for now - we cannot insert an alias with a different
1642 type in the VN tables either, as that would assert. */
1644 && !useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1646 else if (!result
&& newref
1647 && !useless_type_conversion_p (ref
->type
, newref
->type
))
1649 newoperands
.release ();
1653 expr
= pre_expr_pool
.allocate ();
1654 expr
->kind
= REFERENCE
;
1659 PRE_EXPR_REFERENCE (expr
) = newref
;
1660 constant
= fully_constant_expression (expr
);
1661 if (constant
!= expr
)
1664 new_val_id
= newref
->value_id
;
1665 get_or_alloc_expression_id (expr
);
1669 if (changed
|| !same_valid
)
1671 new_val_id
= get_next_value_id ();
1672 value_expressions
.safe_grow_cleared
1673 (get_max_value_id () + 1);
1676 new_val_id
= ref
->value_id
;
1677 if (!newoperands
.exists ())
1678 newoperands
= operands
.copy ();
1679 newref
= vn_reference_insert_pieces (newvuse
, ref
->set
,
1682 result
, new_val_id
);
1683 newoperands
= vNULL
;
1684 PRE_EXPR_REFERENCE (expr
) = newref
;
1685 constant
= fully_constant_expression (expr
);
1686 if (constant
!= expr
)
1688 get_or_alloc_expression_id (expr
);
1690 add_to_value (new_val_id
, expr
);
1692 newoperands
.release ();
1699 tree name
= PRE_EXPR_NAME (expr
);
1700 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1701 /* If the SSA name is defined by a PHI node in this block,
1703 if (gimple_code (def_stmt
) == GIMPLE_PHI
1704 && gimple_bb (def_stmt
) == phiblock
)
1706 edge e
= find_edge (pred
, gimple_bb (def_stmt
));
1707 tree def
= PHI_ARG_DEF (def_stmt
, e
->dest_idx
);
1709 /* Handle constant. */
1710 if (is_gimple_min_invariant (def
))
1711 return get_or_alloc_expr_for_constant (def
);
1713 return get_or_alloc_expr_for_name (def
);
1715 /* Otherwise return it unchanged - it will get removed if its
1716 value is not available in PREDs AVAIL_OUT set of expressions
1717 by the subtraction of TMP_GEN. */
1726 /* Wrapper around phi_translate_1 providing caching functionality. */
1729 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1730 basic_block pred
, basic_block phiblock
)
1732 expr_pred_trans_t slot
= NULL
;
1738 /* Constants contain no values that need translation. */
1739 if (expr
->kind
== CONSTANT
)
1742 if (value_id_constant_p (get_expr_value_id (expr
)))
1745 /* Don't add translations of NAMEs as those are cheap to translate. */
1746 if (expr
->kind
!= NAME
)
1748 if (phi_trans_add (&slot
, expr
, pred
))
1750 /* Store NULL for the value we want to return in the case of
1756 phitrans
= phi_translate_1 (expr
, set1
, set2
, pred
, phiblock
);
1763 /* Remove failed translations again, they cause insert
1764 iteration to not pick up new opportunities reliably. */
1765 phi_translate_table
->remove_elt_with_hash (slot
, slot
->hashcode
);
1772 /* For each expression in SET, translate the values through phi nodes
1773 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1774 expressions in DEST. */
1777 phi_translate_set (bitmap_set_t dest
, bitmap_set_t set
, basic_block pred
,
1778 basic_block phiblock
)
1780 vec
<pre_expr
> exprs
;
1784 if (gimple_seq_empty_p (phi_nodes (phiblock
)))
1786 bitmap_set_copy (dest
, set
);
1790 exprs
= sorted_array_from_bitmap_set (set
);
1791 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
1793 pre_expr translated
;
1794 translated
= phi_translate (expr
, set
, NULL
, pred
, phiblock
);
1798 /* We might end up with multiple expressions from SET being
1799 translated to the same value. In this case we do not want
1800 to retain the NARY or REFERENCE expression but prefer a NAME
1801 which would be the leader. */
1802 if (translated
->kind
== NAME
)
1803 bitmap_value_replace_in_set (dest
, translated
);
1805 bitmap_value_insert_into_set (dest
, translated
);
1810 /* Find the leader for a value (i.e., the name representing that
1811 value) in a given set, and return it. Return NULL if no leader
1815 bitmap_find_leader (bitmap_set_t set
, unsigned int val
)
1817 if (value_id_constant_p (val
))
1821 bitmap exprset
= value_expressions
[val
];
1823 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1825 pre_expr expr
= expression_for_id (i
);
1826 if (expr
->kind
== CONSTANT
)
1830 if (bitmap_set_contains_value (set
, val
))
1832 /* Rather than walk the entire bitmap of expressions, and see
1833 whether any of them has the value we are looking for, we look
1834 at the reverse mapping, which tells us the set of expressions
1835 that have a given value (IE value->expressions with that
1836 value) and see if any of those expressions are in our set.
1837 The number of expressions per value is usually significantly
1838 less than the number of expressions in the set. In fact, for
1839 large testcases, doing it this way is roughly 5-10x faster
1840 than walking the bitmap.
1841 If this is somehow a significant lose for some cases, we can
1842 choose which set to walk based on which set is smaller. */
1845 bitmap exprset
= value_expressions
[val
];
1847 EXECUTE_IF_AND_IN_BITMAP (exprset
, &set
->expressions
, 0, i
, bi
)
1848 return expression_for_id (i
);
1853 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1854 BLOCK by seeing if it is not killed in the block. Note that we are
1855 only determining whether there is a store that kills it. Because
1856 of the order in which clean iterates over values, we are guaranteed
1857 that altered operands will have caused us to be eliminated from the
1858 ANTIC_IN set already. */
1861 value_dies_in_block_x (pre_expr expr
, basic_block block
)
1863 tree vuse
= PRE_EXPR_REFERENCE (expr
)->vuse
;
1864 vn_reference_t refx
= PRE_EXPR_REFERENCE (expr
);
1866 gimple_stmt_iterator gsi
;
1867 unsigned id
= get_expression_id (expr
);
1874 /* Lookup a previously calculated result. */
1875 if (EXPR_DIES (block
)
1876 && bitmap_bit_p (EXPR_DIES (block
), id
* 2))
1877 return bitmap_bit_p (EXPR_DIES (block
), id
* 2 + 1);
1879 /* A memory expression {e, VUSE} dies in the block if there is a
1880 statement that may clobber e. If, starting statement walk from the
1881 top of the basic block, a statement uses VUSE there can be no kill
1882 inbetween that use and the original statement that loaded {e, VUSE},
1883 so we can stop walking. */
1884 ref
.base
= NULL_TREE
;
1885 for (gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1887 tree def_vuse
, def_vdef
;
1888 def
= gsi_stmt (gsi
);
1889 def_vuse
= gimple_vuse (def
);
1890 def_vdef
= gimple_vdef (def
);
1892 /* Not a memory statement. */
1896 /* Not a may-def. */
1899 /* A load with the same VUSE, we're done. */
1900 if (def_vuse
== vuse
)
1906 /* Init ref only if we really need it. */
1907 if (ref
.base
== NULL_TREE
1908 && !ao_ref_init_from_vn_reference (&ref
, refx
->set
, refx
->type
,
1914 /* If the statement may clobber expr, it dies. */
1915 if (stmt_may_clobber_ref_p_1 (def
, &ref
))
1922 /* Remember the result. */
1923 if (!EXPR_DIES (block
))
1924 EXPR_DIES (block
) = BITMAP_ALLOC (&grand_bitmap_obstack
);
1925 bitmap_set_bit (EXPR_DIES (block
), id
* 2);
1927 bitmap_set_bit (EXPR_DIES (block
), id
* 2 + 1);
1933 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1934 contains its value-id. */
1937 op_valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, tree op
)
1939 if (op
&& TREE_CODE (op
) == SSA_NAME
)
1941 unsigned int value_id
= VN_INFO (op
)->value_id
;
1942 if (!(bitmap_set_contains_value (set1
, value_id
)
1943 || (set2
&& bitmap_set_contains_value (set2
, value_id
))))
1949 /* Determine if the expression EXPR is valid in SET1 U SET2.
1950 ONLY SET2 CAN BE NULL.
1951 This means that we have a leader for each part of the expression
1952 (if it consists of values), or the expression is an SSA_NAME.
1953 For loads/calls, we also see if the vuse is killed in this block. */
1956 valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, pre_expr expr
)
1961 /* By construction all NAMEs are available. Non-available
1962 NAMEs are removed by subtracting TMP_GEN from the sets. */
1967 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1968 for (i
= 0; i
< nary
->length
; i
++)
1969 if (!op_valid_in_sets (set1
, set2
, nary
->op
[i
]))
1976 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1977 vn_reference_op_t vro
;
1980 FOR_EACH_VEC_ELT (ref
->operands
, i
, vro
)
1982 if (!op_valid_in_sets (set1
, set2
, vro
->op0
)
1983 || !op_valid_in_sets (set1
, set2
, vro
->op1
)
1984 || !op_valid_in_sets (set1
, set2
, vro
->op2
))
1994 /* Clean the set of expressions that are no longer valid in SET1 or
1995 SET2. This means expressions that are made up of values we have no
1996 leaders for in SET1 or SET2. This version is used for partial
1997 anticipation, which means it is not valid in either ANTIC_IN or
2001 dependent_clean (bitmap_set_t set1
, bitmap_set_t set2
)
2003 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set1
);
2007 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2009 if (!valid_in_sets (set1
, set2
, expr
))
2010 bitmap_remove_from_set (set1
, expr
);
2015 /* Clean the set of expressions that are no longer valid in SET. This
2016 means expressions that are made up of values we have no leaders for
2020 clean (bitmap_set_t set
)
2022 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set
);
2026 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2028 if (!valid_in_sets (set
, NULL
, expr
))
2029 bitmap_remove_from_set (set
, expr
);
2034 /* Clean the set of expressions that are no longer valid in SET because
2035 they are clobbered in BLOCK or because they trap and may not be executed. */
2038 prune_clobbered_mems (bitmap_set_t set
, basic_block block
)
2042 pre_expr to_remove
= NULL
;
2044 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
2046 /* Remove queued expr. */
2049 bitmap_remove_from_set (set
, to_remove
);
2053 pre_expr expr
= expression_for_id (i
);
2054 if (expr
->kind
== REFERENCE
)
2056 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2059 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ref
->vuse
);
2060 if (!gimple_nop_p (def_stmt
)
2061 && ((gimple_bb (def_stmt
) != block
2062 && !dominated_by_p (CDI_DOMINATORS
,
2063 block
, gimple_bb (def_stmt
)))
2064 || (gimple_bb (def_stmt
) == block
2065 && value_dies_in_block_x (expr
, block
))))
2069 else if (expr
->kind
== NARY
)
2071 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2072 /* If the NARY may trap make sure the block does not contain
2073 a possible exit point.
2074 ??? This is overly conservative if we translate AVAIL_OUT
2075 as the available expression might be after the exit point. */
2076 if (BB_MAY_NOTRETURN (block
)
2077 && vn_nary_may_trap (nary
))
2082 /* Remove queued expr. */
2084 bitmap_remove_from_set (set
, to_remove
);
2087 static sbitmap has_abnormal_preds
;
2089 /* Compute the ANTIC set for BLOCK.
2091 If succs(BLOCK) > 1 then
2092 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2093 else if succs(BLOCK) == 1 then
2094 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2096 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2100 compute_antic_aux (basic_block block
, bool block_has_abnormal_pred_edge
)
2102 bool changed
= false;
2103 bitmap_set_t S
, old
, ANTIC_OUT
;
2108 bool was_visited
= BB_VISITED (block
);
2110 old
= ANTIC_OUT
= S
= NULL
;
2111 BB_VISITED (block
) = 1;
2113 /* If any edges from predecessors are abnormal, antic_in is empty,
2115 if (block_has_abnormal_pred_edge
)
2116 goto maybe_dump_sets
;
2118 old
= ANTIC_IN (block
);
2119 ANTIC_OUT
= bitmap_set_new ();
2121 /* If the block has no successors, ANTIC_OUT is empty. */
2122 if (EDGE_COUNT (block
->succs
) == 0)
2124 /* If we have one successor, we could have some phi nodes to
2125 translate through. */
2126 else if (single_succ_p (block
))
2128 basic_block succ_bb
= single_succ (block
);
2129 gcc_assert (BB_VISITED (succ_bb
));
2130 phi_translate_set (ANTIC_OUT
, ANTIC_IN (succ_bb
), block
, succ_bb
);
2132 /* If we have multiple successors, we take the intersection of all of
2133 them. Note that in the case of loop exit phi nodes, we may have
2134 phis to translate through. */
2138 basic_block bprime
, first
= NULL
;
2140 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2141 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2144 && BB_VISITED (e
->dest
))
2146 else if (BB_VISITED (e
->dest
))
2147 worklist
.quick_push (e
->dest
);
2150 /* Unvisited successors get their ANTIC_IN replaced by the
2151 maximal set to arrive at a maximum ANTIC_IN solution.
2152 We can ignore them in the intersection operation and thus
2153 need not explicitely represent that maximum solution. */
2154 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2155 fprintf (dump_file
, "ANTIC_IN is MAX on %d->%d\n",
2156 e
->src
->index
, e
->dest
->index
);
2160 /* Of multiple successors we have to have visited one already
2161 which is guaranteed by iteration order. */
2162 gcc_assert (first
!= NULL
);
2164 phi_translate_set (ANTIC_OUT
, ANTIC_IN (first
), block
, first
);
2166 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2168 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2170 bitmap_set_t tmp
= bitmap_set_new ();
2171 phi_translate_set (tmp
, ANTIC_IN (bprime
), block
, bprime
);
2172 bitmap_set_and (ANTIC_OUT
, tmp
);
2173 bitmap_set_free (tmp
);
2176 bitmap_set_and (ANTIC_OUT
, ANTIC_IN (bprime
));
2180 /* Prune expressions that are clobbered in block and thus become
2181 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2182 prune_clobbered_mems (ANTIC_OUT
, block
);
2184 /* Generate ANTIC_OUT - TMP_GEN. */
2185 S
= bitmap_set_subtract (ANTIC_OUT
, TMP_GEN (block
));
2187 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2188 ANTIC_IN (block
) = bitmap_set_subtract (EXP_GEN (block
),
2191 /* Then union in the ANTIC_OUT - TMP_GEN values,
2192 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2193 FOR_EACH_EXPR_ID_IN_SET (S
, bii
, bi
)
2194 bitmap_value_insert_into_set (ANTIC_IN (block
),
2195 expression_for_id (bii
));
2197 clean (ANTIC_IN (block
));
2199 if (!was_visited
|| !bitmap_set_equal (old
, ANTIC_IN (block
)))
2203 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2206 print_bitmap_set (dump_file
, ANTIC_OUT
, "ANTIC_OUT", block
->index
);
2209 fprintf (dump_file
, "[changed] ");
2210 print_bitmap_set (dump_file
, ANTIC_IN (block
), "ANTIC_IN",
2214 print_bitmap_set (dump_file
, S
, "S", block
->index
);
2217 bitmap_set_free (old
);
2219 bitmap_set_free (S
);
2221 bitmap_set_free (ANTIC_OUT
);
2225 /* Compute PARTIAL_ANTIC for BLOCK.
2227 If succs(BLOCK) > 1 then
2228 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2229 in ANTIC_OUT for all succ(BLOCK)
2230 else if succs(BLOCK) == 1 then
2231 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2233 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2238 compute_partial_antic_aux (basic_block block
,
2239 bool block_has_abnormal_pred_edge
)
2241 bitmap_set_t old_PA_IN
;
2242 bitmap_set_t PA_OUT
;
2245 unsigned long max_pa
= PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH
);
2247 old_PA_IN
= PA_OUT
= NULL
;
2249 /* If any edges from predecessors are abnormal, antic_in is empty,
2251 if (block_has_abnormal_pred_edge
)
2252 goto maybe_dump_sets
;
2254 /* If there are too many partially anticipatable values in the
2255 block, phi_translate_set can take an exponential time: stop
2256 before the translation starts. */
2258 && single_succ_p (block
)
2259 && bitmap_count_bits (&PA_IN (single_succ (block
))->values
) > max_pa
)
2260 goto maybe_dump_sets
;
2262 old_PA_IN
= PA_IN (block
);
2263 PA_OUT
= bitmap_set_new ();
2265 /* If the block has no successors, ANTIC_OUT is empty. */
2266 if (EDGE_COUNT (block
->succs
) == 0)
2268 /* If we have one successor, we could have some phi nodes to
2269 translate through. Note that we can't phi translate across DFS
2270 back edges in partial antic, because it uses a union operation on
2271 the successors. For recurrences like IV's, we will end up
2272 generating a new value in the set on each go around (i + 3 (VH.1)
2273 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2274 else if (single_succ_p (block
))
2276 basic_block succ
= single_succ (block
);
2277 if (!(single_succ_edge (block
)->flags
& EDGE_DFS_BACK
))
2278 phi_translate_set (PA_OUT
, PA_IN (succ
), block
, succ
);
2280 /* If we have multiple successors, we take the union of all of
2287 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2288 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2290 if (e
->flags
& EDGE_DFS_BACK
)
2292 worklist
.quick_push (e
->dest
);
2294 if (worklist
.length () > 0)
2296 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2301 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime
), i
, bi
)
2302 bitmap_value_insert_into_set (PA_OUT
,
2303 expression_for_id (i
));
2304 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2306 bitmap_set_t pa_in
= bitmap_set_new ();
2307 phi_translate_set (pa_in
, PA_IN (bprime
), block
, bprime
);
2308 FOR_EACH_EXPR_ID_IN_SET (pa_in
, i
, bi
)
2309 bitmap_value_insert_into_set (PA_OUT
,
2310 expression_for_id (i
));
2311 bitmap_set_free (pa_in
);
2314 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime
), i
, bi
)
2315 bitmap_value_insert_into_set (PA_OUT
,
2316 expression_for_id (i
));
2321 /* Prune expressions that are clobbered in block and thus become
2322 invalid if translated from PA_OUT to PA_IN. */
2323 prune_clobbered_mems (PA_OUT
, block
);
2325 /* PA_IN starts with PA_OUT - TMP_GEN.
2326 Then we subtract things from ANTIC_IN. */
2327 PA_IN (block
) = bitmap_set_subtract (PA_OUT
, TMP_GEN (block
));
2329 /* For partial antic, we want to put back in the phi results, since
2330 we will properly avoid making them partially antic over backedges. */
2331 bitmap_ior_into (&PA_IN (block
)->values
, &PHI_GEN (block
)->values
);
2332 bitmap_ior_into (&PA_IN (block
)->expressions
, &PHI_GEN (block
)->expressions
);
2334 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2335 bitmap_set_subtract_values (PA_IN (block
), ANTIC_IN (block
));
2337 dependent_clean (PA_IN (block
), ANTIC_IN (block
));
2340 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2343 print_bitmap_set (dump_file
, PA_OUT
, "PA_OUT", block
->index
);
2345 print_bitmap_set (dump_file
, PA_IN (block
), "PA_IN", block
->index
);
2348 bitmap_set_free (old_PA_IN
);
2350 bitmap_set_free (PA_OUT
);
2353 /* Compute ANTIC and partial ANTIC sets. */
2356 compute_antic (void)
2358 bool changed
= true;
2359 int num_iterations
= 0;
2365 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2366 We pre-build the map of blocks with incoming abnormal edges here. */
2367 has_abnormal_preds
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
2368 bitmap_clear (has_abnormal_preds
);
2370 FOR_ALL_BB_FN (block
, cfun
)
2372 BB_VISITED (block
) = 0;
2374 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2375 if (e
->flags
& EDGE_ABNORMAL
)
2377 bitmap_set_bit (has_abnormal_preds
, block
->index
);
2379 /* We also anticipate nothing. */
2380 BB_VISITED (block
) = 1;
2384 /* While we are here, give empty ANTIC_IN sets to each block. */
2385 ANTIC_IN (block
) = bitmap_set_new ();
2386 if (do_partial_partial
)
2387 PA_IN (block
) = bitmap_set_new ();
2390 /* At the exit block we anticipate nothing. */
2391 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun
)) = 1;
2393 /* For ANTIC computation we need a postorder that also guarantees that
2394 a block with a single successor is visited after its successor.
2395 RPO on the inverted CFG has this property. */
2396 int *postorder
= XNEWVEC (int, n_basic_blocks_for_fn (cfun
));
2397 int postorder_num
= inverted_post_order_compute (postorder
);
2399 auto_sbitmap
worklist (last_basic_block_for_fn (cfun
) + 1);
2400 bitmap_ones (worklist
);
2403 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2404 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2405 /* ??? We need to clear our PHI translation cache here as the
2406 ANTIC sets shrink and we restrict valid translations to
2407 those having operands with leaders in ANTIC. Same below
2408 for PA ANTIC computation. */
2411 for (i
= postorder_num
- 1; i
>= 0; i
--)
2413 if (bitmap_bit_p (worklist
, postorder
[i
]))
2415 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2416 bitmap_clear_bit (worklist
, block
->index
);
2417 if (compute_antic_aux (block
,
2418 bitmap_bit_p (has_abnormal_preds
,
2421 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2422 bitmap_set_bit (worklist
, e
->src
->index
);
2427 /* Theoretically possible, but *highly* unlikely. */
2428 gcc_checking_assert (num_iterations
< 500);
2431 statistics_histogram_event (cfun
, "compute_antic iterations",
2434 if (do_partial_partial
)
2436 /* For partial antic we ignore backedges and thus we do not need
2437 to perform any iteration when we process blocks in postorder. */
2438 postorder_num
= pre_and_rev_post_order_compute (NULL
, postorder
, false);
2439 for (i
= postorder_num
- 1 ; i
>= 0; i
--)
2441 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2442 compute_partial_antic_aux (block
,
2443 bitmap_bit_p (has_abnormal_preds
,
2448 sbitmap_free (has_abnormal_preds
);
2453 /* Inserted expressions are placed onto this worklist, which is used
2454 for performing quick dead code elimination of insertions we made
2455 that didn't turn out to be necessary. */
2456 static bitmap inserted_exprs
;
2458 /* The actual worker for create_component_ref_by_pieces. */
2461 create_component_ref_by_pieces_1 (basic_block block
, vn_reference_t ref
,
2462 unsigned int *operand
, gimple_seq
*stmts
)
2464 vn_reference_op_t currop
= &ref
->operands
[*operand
];
2467 switch (currop
->opcode
)
2474 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2478 tree offset
= currop
->op0
;
2479 if (TREE_CODE (baseop
) == ADDR_EXPR
2480 && handled_component_p (TREE_OPERAND (baseop
, 0)))
2484 base
= get_addr_base_and_unit_offset (TREE_OPERAND (baseop
, 0),
2487 offset
= int_const_binop (PLUS_EXPR
, offset
,
2488 build_int_cst (TREE_TYPE (offset
),
2490 baseop
= build_fold_addr_expr (base
);
2492 genop
= build2 (MEM_REF
, currop
->type
, baseop
, offset
);
2493 MR_DEPENDENCE_CLIQUE (genop
) = currop
->clique
;
2494 MR_DEPENDENCE_BASE (genop
) = currop
->base
;
2495 REF_REVERSE_STORAGE_ORDER (genop
) = currop
->reverse
;
2499 case TARGET_MEM_REF
:
2501 tree genop0
= NULL_TREE
, genop1
= NULL_TREE
;
2502 vn_reference_op_t nextop
= &ref
->operands
[++*operand
];
2503 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2509 genop0
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2515 genop1
= find_or_generate_expression (block
, nextop
->op0
, stmts
);
2519 genop
= build5 (TARGET_MEM_REF
, currop
->type
,
2520 baseop
, currop
->op2
, genop0
, currop
->op1
, genop1
);
2522 MR_DEPENDENCE_CLIQUE (genop
) = currop
->clique
;
2523 MR_DEPENDENCE_BASE (genop
) = currop
->base
;
2530 gcc_assert (is_gimple_min_invariant (currop
->op0
));
2536 case VIEW_CONVERT_EXPR
:
2538 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2542 return fold_build1 (currop
->opcode
, currop
->type
, genop0
);
2545 case WITH_SIZE_EXPR
:
2547 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2551 tree genop1
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2554 return fold_build2 (currop
->opcode
, currop
->type
, genop0
, genop1
);
2559 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2563 tree op1
= currop
->op0
;
2564 tree op2
= currop
->op1
;
2565 tree t
= build3 (BIT_FIELD_REF
, currop
->type
, genop0
, op1
, op2
);
2566 REF_REVERSE_STORAGE_ORDER (t
) = currop
->reverse
;
2570 /* For array ref vn_reference_op's, operand 1 of the array ref
2571 is op0 of the reference op and operand 3 of the array ref is
2573 case ARRAY_RANGE_REF
:
2577 tree genop1
= currop
->op0
;
2578 tree genop2
= currop
->op1
;
2579 tree genop3
= currop
->op2
;
2580 genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2584 genop1
= find_or_generate_expression (block
, genop1
, stmts
);
2589 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (genop0
));
2590 /* Drop zero minimum index if redundant. */
2591 if (integer_zerop (genop2
)
2593 || integer_zerop (TYPE_MIN_VALUE (domain_type
))))
2597 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2604 tree elmt_type
= TREE_TYPE (TREE_TYPE (genop0
));
2605 /* We can't always put a size in units of the element alignment
2606 here as the element alignment may be not visible. See
2607 PR43783. Simply drop the element size for constant
2609 if (TREE_CODE (genop3
) == INTEGER_CST
2610 && TREE_CODE (TYPE_SIZE_UNIT (elmt_type
)) == INTEGER_CST
2611 && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type
)),
2612 (wi::to_offset (genop3
)
2613 * vn_ref_op_align_unit (currop
))))
2617 genop3
= find_or_generate_expression (block
, genop3
, stmts
);
2622 return build4 (currop
->opcode
, currop
->type
, genop0
, genop1
,
2629 tree genop2
= currop
->op1
;
2630 op0
= create_component_ref_by_pieces_1 (block
, ref
, operand
, stmts
);
2633 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2637 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2641 return fold_build3 (COMPONENT_REF
, TREE_TYPE (op1
), op0
, op1
, genop2
);
2646 genop
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2667 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2668 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2669 trying to rename aggregates into ssa form directly, which is a no no.
2671 Thus, this routine doesn't create temporaries, it just builds a
2672 single access expression for the array, calling
2673 find_or_generate_expression to build the innermost pieces.
2675 This function is a subroutine of create_expression_by_pieces, and
2676 should not be called on it's own unless you really know what you
2680 create_component_ref_by_pieces (basic_block block
, vn_reference_t ref
,
2683 unsigned int op
= 0;
2684 return create_component_ref_by_pieces_1 (block
, ref
, &op
, stmts
);
2687 /* Find a simple leader for an expression, or generate one using
2688 create_expression_by_pieces from a NARY expression for the value.
2689 BLOCK is the basic_block we are looking for leaders in.
2690 OP is the tree expression to find a leader for or generate.
2691 Returns the leader or NULL_TREE on failure. */
2694 find_or_generate_expression (basic_block block
, tree op
, gimple_seq
*stmts
)
2696 pre_expr expr
= get_or_alloc_expr_for (op
);
2697 unsigned int lookfor
= get_expr_value_id (expr
);
2698 pre_expr leader
= bitmap_find_leader (AVAIL_OUT (block
), lookfor
);
2701 if (leader
->kind
== NAME
)
2702 return PRE_EXPR_NAME (leader
);
2703 else if (leader
->kind
== CONSTANT
)
2704 return PRE_EXPR_CONSTANT (leader
);
2710 /* It must be a complex expression, so generate it recursively. Note
2711 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2712 where the insert algorithm fails to insert a required expression. */
2713 bitmap exprset
= value_expressions
[lookfor
];
2716 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
2718 pre_expr temp
= expression_for_id (i
);
2719 /* We cannot insert random REFERENCE expressions at arbitrary
2720 places. We can insert NARYs which eventually re-materializes
2721 its operand values. */
2722 if (temp
->kind
== NARY
)
2723 return create_expression_by_pieces (block
, temp
, stmts
,
2724 get_expr_type (expr
));
2731 #define NECESSARY GF_PLF_1
2733 /* Create an expression in pieces, so that we can handle very complex
2734 expressions that may be ANTIC, but not necessary GIMPLE.
2735 BLOCK is the basic block the expression will be inserted into,
2736 EXPR is the expression to insert (in value form)
2737 STMTS is a statement list to append the necessary insertions into.
2739 This function will die if we hit some value that shouldn't be
2740 ANTIC but is (IE there is no leader for it, or its components).
2741 The function returns NULL_TREE in case a different antic expression
2742 has to be inserted first.
2743 This function may also generate expressions that are themselves
2744 partially or fully redundant. Those that are will be either made
2745 fully redundant during the next iteration of insert (for partially
2746 redundant ones), or eliminated by eliminate (for fully redundant
2750 create_expression_by_pieces (basic_block block
, pre_expr expr
,
2751 gimple_seq
*stmts
, tree type
)
2755 gimple_seq forced_stmts
= NULL
;
2756 unsigned int value_id
;
2757 gimple_stmt_iterator gsi
;
2758 tree exprtype
= type
? type
: get_expr_type (expr
);
2764 /* We may hit the NAME/CONSTANT case if we have to convert types
2765 that value numbering saw through. */
2767 folded
= PRE_EXPR_NAME (expr
);
2768 if (useless_type_conversion_p (exprtype
, TREE_TYPE (folded
)))
2773 folded
= PRE_EXPR_CONSTANT (expr
);
2774 tree tem
= fold_convert (exprtype
, folded
);
2775 if (is_gimple_min_invariant (tem
))
2780 if (PRE_EXPR_REFERENCE (expr
)->operands
[0].opcode
== CALL_EXPR
)
2782 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2783 unsigned int operand
= 1;
2784 vn_reference_op_t currop
= &ref
->operands
[0];
2785 tree sc
= NULL_TREE
;
2787 if (TREE_CODE (currop
->op0
) == FUNCTION_DECL
)
2790 fn
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2795 sc
= find_or_generate_expression (block
, currop
->op1
, stmts
);
2799 auto_vec
<tree
> args (ref
->operands
.length () - 1);
2800 while (operand
< ref
->operands
.length ())
2802 tree arg
= create_component_ref_by_pieces_1 (block
, ref
,
2806 args
.quick_push (arg
);
2809 = gimple_build_call_vec ((TREE_CODE (fn
) == FUNCTION_DECL
2810 ? build_fold_addr_expr (fn
) : fn
), args
);
2811 gimple_call_set_with_bounds (call
, currop
->with_bounds
);
2813 gimple_call_set_chain (call
, sc
);
2814 tree forcedname
= make_ssa_name (currop
->type
);
2815 gimple_call_set_lhs (call
, forcedname
);
2816 gimple_set_vuse (call
, BB_LIVE_VOP_ON_EXIT (block
));
2817 gimple_seq_add_stmt_without_update (&forced_stmts
, call
);
2818 folded
= forcedname
;
2822 folded
= create_component_ref_by_pieces (block
,
2823 PRE_EXPR_REFERENCE (expr
),
2827 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2828 newstmt
= gimple_build_assign (name
, folded
);
2829 gimple_seq_add_stmt_without_update (&forced_stmts
, newstmt
);
2830 gimple_set_vuse (newstmt
, BB_LIVE_VOP_ON_EXIT (block
));
2836 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2837 tree
*genop
= XALLOCAVEC (tree
, nary
->length
);
2839 for (i
= 0; i
< nary
->length
; ++i
)
2841 genop
[i
] = find_or_generate_expression (block
, nary
->op
[i
], stmts
);
2844 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2845 may have conversions stripped. */
2846 if (nary
->opcode
== POINTER_PLUS_EXPR
)
2849 genop
[i
] = gimple_convert (&forced_stmts
,
2850 nary
->type
, genop
[i
]);
2852 genop
[i
] = gimple_convert (&forced_stmts
,
2853 sizetype
, genop
[i
]);
2856 genop
[i
] = gimple_convert (&forced_stmts
,
2857 TREE_TYPE (nary
->op
[i
]), genop
[i
]);
2859 if (nary
->opcode
== CONSTRUCTOR
)
2861 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
2862 for (i
= 0; i
< nary
->length
; ++i
)
2863 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
, genop
[i
]);
2864 folded
= build_constructor (nary
->type
, elts
);
2865 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2866 newstmt
= gimple_build_assign (name
, folded
);
2867 gimple_seq_add_stmt_without_update (&forced_stmts
, newstmt
);
2872 switch (nary
->length
)
2875 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2879 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2880 genop
[0], genop
[1]);
2883 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2884 genop
[0], genop
[1], genop
[2]);
2896 folded
= gimple_convert (&forced_stmts
, exprtype
, folded
);
2898 /* If there is nothing to insert, return the simplified result. */
2899 if (gimple_seq_empty_p (forced_stmts
))
2901 /* If we simplified to a constant return it and discard eventually
2903 if (is_gimple_min_invariant (folded
))
2905 gimple_seq_discard (forced_stmts
);
2908 /* Likewise if we simplified to sth not queued for insertion. */
2910 gsi
= gsi_last (forced_stmts
);
2911 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
2913 gimple
*stmt
= gsi_stmt (gsi
);
2914 tree forcedname
= gimple_get_lhs (stmt
);
2915 if (forcedname
== folded
)
2923 gimple_seq_discard (forced_stmts
);
2926 gcc_assert (TREE_CODE (folded
) == SSA_NAME
);
2928 /* If we have any intermediate expressions to the value sets, add them
2929 to the value sets and chain them in the instruction stream. */
2932 gsi
= gsi_start (forced_stmts
);
2933 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
2935 gimple
*stmt
= gsi_stmt (gsi
);
2936 tree forcedname
= gimple_get_lhs (stmt
);
2939 if (forcedname
!= folded
)
2941 VN_INFO_GET (forcedname
)->valnum
= forcedname
;
2942 VN_INFO (forcedname
)->value_id
= get_next_value_id ();
2943 nameexpr
= get_or_alloc_expr_for_name (forcedname
);
2944 add_to_value (VN_INFO (forcedname
)->value_id
, nameexpr
);
2945 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2946 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2949 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (forcedname
));
2950 gimple_set_plf (stmt
, NECESSARY
, false);
2952 gimple_seq_add_seq (stmts
, forced_stmts
);
2957 /* Fold the last statement. */
2958 gsi
= gsi_last (*stmts
);
2959 if (fold_stmt_inplace (&gsi
))
2960 update_stmt (gsi_stmt (gsi
));
2962 /* Add a value number to the temporary.
2963 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2964 we are creating the expression by pieces, and this particular piece of
2965 the expression may have been represented. There is no harm in replacing
2967 value_id
= get_expr_value_id (expr
);
2968 VN_INFO_GET (name
)->value_id
= value_id
;
2969 VN_INFO (name
)->valnum
= sccvn_valnum_from_value_id (value_id
);
2970 if (VN_INFO (name
)->valnum
== NULL_TREE
)
2971 VN_INFO (name
)->valnum
= name
;
2972 gcc_assert (VN_INFO (name
)->valnum
!= NULL_TREE
);
2973 nameexpr
= get_or_alloc_expr_for_name (name
);
2974 add_to_value (value_id
, nameexpr
);
2975 if (NEW_SETS (block
))
2976 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2977 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2979 pre_stats
.insertions
++;
2980 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2982 fprintf (dump_file
, "Inserted ");
2983 print_gimple_stmt (dump_file
, gsi_stmt (gsi_last (*stmts
)), 0, 0);
2984 fprintf (dump_file
, " in predecessor %d (%04d)\n",
2985 block
->index
, value_id
);
2992 /* Insert the to-be-made-available values of expression EXPRNUM for each
2993 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2994 merge the result with a phi node, given the same value number as
2995 NODE. Return true if we have inserted new stuff. */
2998 insert_into_preds_of_block (basic_block block
, unsigned int exprnum
,
2999 vec
<pre_expr
> avail
)
3001 pre_expr expr
= expression_for_id (exprnum
);
3003 unsigned int val
= get_expr_value_id (expr
);
3005 bool insertions
= false;
3010 tree type
= get_expr_type (expr
);
3014 /* Make sure we aren't creating an induction variable. */
3015 if (bb_loop_depth (block
) > 0 && EDGE_COUNT (block
->preds
) == 2)
3017 bool firstinsideloop
= false;
3018 bool secondinsideloop
= false;
3019 firstinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3020 EDGE_PRED (block
, 0)->src
);
3021 secondinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3022 EDGE_PRED (block
, 1)->src
);
3023 /* Induction variables only have one edge inside the loop. */
3024 if ((firstinsideloop
^ secondinsideloop
)
3025 && expr
->kind
!= REFERENCE
)
3027 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3028 fprintf (dump_file
, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3033 /* Make the necessary insertions. */
3034 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3036 gimple_seq stmts
= NULL
;
3039 eprime
= avail
[pred
->dest_idx
];
3040 builtexpr
= create_expression_by_pieces (bprime
, eprime
,
3042 gcc_assert (!(pred
->flags
& EDGE_ABNORMAL
));
3043 if (!gimple_seq_empty_p (stmts
))
3045 gsi_insert_seq_on_edge (pred
, stmts
);
3050 /* We cannot insert a PHI node if we failed to insert
3055 if (is_gimple_min_invariant (builtexpr
))
3056 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_constant (builtexpr
);
3058 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_name (builtexpr
);
3060 /* If we didn't want a phi node, and we made insertions, we still have
3061 inserted new stuff, and thus return true. If we didn't want a phi node,
3062 and didn't make insertions, we haven't added anything new, so return
3064 if (nophi
&& insertions
)
3066 else if (nophi
&& !insertions
)
3069 /* Now build a phi for the new variable. */
3070 temp
= make_temp_ssa_name (type
, NULL
, "prephitmp");
3071 phi
= create_phi_node (temp
, block
);
3073 gimple_set_plf (phi
, NECESSARY
, false);
3074 VN_INFO_GET (temp
)->value_id
= val
;
3075 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3076 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3077 VN_INFO (temp
)->valnum
= temp
;
3078 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3079 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3081 pre_expr ae
= avail
[pred
->dest_idx
];
3082 gcc_assert (get_expr_type (ae
) == type
3083 || useless_type_conversion_p (type
, get_expr_type (ae
)));
3084 if (ae
->kind
== CONSTANT
)
3085 add_phi_arg (phi
, unshare_expr (PRE_EXPR_CONSTANT (ae
)),
3086 pred
, UNKNOWN_LOCATION
);
3088 add_phi_arg (phi
, PRE_EXPR_NAME (ae
), pred
, UNKNOWN_LOCATION
);
3091 newphi
= get_or_alloc_expr_for_name (temp
);
3092 add_to_value (val
, newphi
);
3094 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3095 this insertion, since we test for the existence of this value in PHI_GEN
3096 before proceeding with the partial redundancy checks in insert_aux.
3098 The value may exist in AVAIL_OUT, in particular, it could be represented
3099 by the expression we are trying to eliminate, in which case we want the
3100 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3103 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3104 this block, because if it did, it would have existed in our dominator's
3105 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3108 bitmap_insert_into_set (PHI_GEN (block
), newphi
);
3109 bitmap_value_replace_in_set (AVAIL_OUT (block
),
3111 bitmap_insert_into_set (NEW_SETS (block
),
3114 /* If we insert a PHI node for a conversion of another PHI node
3115 in the same basic-block try to preserve range information.
3116 This is important so that followup loop passes receive optimal
3117 number of iteration analysis results. See PR61743. */
3118 if (expr
->kind
== NARY
3119 && CONVERT_EXPR_CODE_P (expr
->u
.nary
->opcode
)
3120 && TREE_CODE (expr
->u
.nary
->op
[0]) == SSA_NAME
3121 && gimple_bb (SSA_NAME_DEF_STMT (expr
->u
.nary
->op
[0])) == block
3122 && INTEGRAL_TYPE_P (type
)
3123 && INTEGRAL_TYPE_P (TREE_TYPE (expr
->u
.nary
->op
[0]))
3124 && (TYPE_PRECISION (type
)
3125 >= TYPE_PRECISION (TREE_TYPE (expr
->u
.nary
->op
[0])))
3126 && SSA_NAME_RANGE_INFO (expr
->u
.nary
->op
[0]))
3129 if (get_range_info (expr
->u
.nary
->op
[0], &min
, &max
) == VR_RANGE
3130 && !wi::neg_p (min
, SIGNED
)
3131 && !wi::neg_p (max
, SIGNED
))
3132 /* Just handle extension and sign-changes of all-positive ranges. */
3133 set_range_info (temp
,
3134 SSA_NAME_RANGE_TYPE (expr
->u
.nary
->op
[0]),
3135 wide_int_storage::from (min
, TYPE_PRECISION (type
),
3137 wide_int_storage::from (max
, TYPE_PRECISION (type
),
3141 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3143 fprintf (dump_file
, "Created phi ");
3144 print_gimple_stmt (dump_file
, phi
, 0, 0);
3145 fprintf (dump_file
, " in block %d (%04d)\n", block
->index
, val
);
3153 /* Perform insertion of partially redundant or hoistable values.
3154 For BLOCK, do the following:
3155 1. Propagate the NEW_SETS of the dominator into the current block.
3156 If the block has multiple predecessors,
3157 2a. Iterate over the ANTIC expressions for the block to see if
3158 any of them are partially redundant.
3159 2b. If so, insert them into the necessary predecessors to make
3160 the expression fully redundant.
3161 2c. Insert a new PHI merging the values of the predecessors.
3162 2d. Insert the new PHI, and the new expressions, into the
3164 If the block has multiple successors,
3165 3a. Iterate over the ANTIC values for the block to see if
3166 any of them are good candidates for hoisting.
3167 3b. If so, insert expressions computing the values in BLOCK,
3168 and add the new expressions into the NEW_SETS set.
3169 4. Recursively call ourselves on the dominator children of BLOCK.
3171 Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3172 do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3173 done in do_hoist_insertion.
3177 do_pre_regular_insertion (basic_block block
, basic_block dom
)
3179 bool new_stuff
= false;
3180 vec
<pre_expr
> exprs
;
3182 auto_vec
<pre_expr
> avail
;
3185 exprs
= sorted_array_from_bitmap_set (ANTIC_IN (block
));
3186 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3188 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3190 if (expr
->kind
== NARY
3191 || expr
->kind
== REFERENCE
)
3194 bool by_some
= false;
3195 bool cant_insert
= false;
3196 bool all_same
= true;
3197 pre_expr first_s
= NULL
;
3200 pre_expr eprime
= NULL
;
3202 pre_expr edoubleprime
= NULL
;
3203 bool do_insertion
= false;
3205 val
= get_expr_value_id (expr
);
3206 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3208 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3210 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3212 fprintf (dump_file
, "Found fully redundant value: ");
3213 print_pre_expr (dump_file
, expr
);
3214 fprintf (dump_file
, "\n");
3219 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3221 unsigned int vprime
;
3223 /* We should never run insertion for the exit block
3224 and so not come across fake pred edges. */
3225 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3227 /* We are looking at ANTIC_OUT of bprime. */
3228 eprime
= phi_translate (expr
, ANTIC_IN (block
), NULL
,
3231 /* eprime will generally only be NULL if the
3232 value of the expression, translated
3233 through the PHI for this predecessor, is
3234 undefined. If that is the case, we can't
3235 make the expression fully redundant,
3236 because its value is undefined along a
3237 predecessor path. We can thus break out
3238 early because it doesn't matter what the
3239 rest of the results are. */
3242 avail
[pred
->dest_idx
] = NULL
;
3247 vprime
= get_expr_value_id (eprime
);
3248 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
),
3250 if (edoubleprime
== NULL
)
3252 avail
[pred
->dest_idx
] = eprime
;
3257 avail
[pred
->dest_idx
] = edoubleprime
;
3259 /* We want to perform insertions to remove a redundancy on
3260 a path in the CFG we want to optimize for speed. */
3261 if (optimize_edge_for_speed_p (pred
))
3262 do_insertion
= true;
3263 if (first_s
== NULL
)
3264 first_s
= edoubleprime
;
3265 else if (!pre_expr_d::equal (first_s
, edoubleprime
))
3269 /* If we can insert it, it's not the same value
3270 already existing along every predecessor, and
3271 it's defined by some predecessor, it is
3272 partially redundant. */
3273 if (!cant_insert
&& !all_same
&& by_some
)
3277 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3279 fprintf (dump_file
, "Skipping partial redundancy for "
3281 print_pre_expr (dump_file
, expr
);
3282 fprintf (dump_file
, " (%04d), no redundancy on to be "
3283 "optimized for speed edge\n", val
);
3286 else if (dbg_cnt (treepre_insert
))
3288 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3290 fprintf (dump_file
, "Found partial redundancy for "
3292 print_pre_expr (dump_file
, expr
);
3293 fprintf (dump_file
, " (%04d)\n",
3294 get_expr_value_id (expr
));
3296 if (insert_into_preds_of_block (block
,
3297 get_expression_id (expr
),
3302 /* If all edges produce the same value and that value is
3303 an invariant, then the PHI has the same value on all
3304 edges. Note this. */
3305 else if (!cant_insert
&& all_same
)
3307 gcc_assert (edoubleprime
->kind
== CONSTANT
3308 || edoubleprime
->kind
== NAME
);
3310 tree temp
= make_temp_ssa_name (get_expr_type (expr
),
3313 = gimple_build_assign (temp
,
3314 edoubleprime
->kind
== CONSTANT
?
3315 PRE_EXPR_CONSTANT (edoubleprime
) :
3316 PRE_EXPR_NAME (edoubleprime
));
3317 gimple_stmt_iterator gsi
= gsi_after_labels (block
);
3318 gsi_insert_before (&gsi
, assign
, GSI_NEW_STMT
);
3320 gimple_set_plf (assign
, NECESSARY
, false);
3321 VN_INFO_GET (temp
)->value_id
= val
;
3322 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3323 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3324 VN_INFO (temp
)->valnum
= temp
;
3325 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3326 pre_expr newe
= get_or_alloc_expr_for_name (temp
);
3327 add_to_value (val
, newe
);
3328 bitmap_value_replace_in_set (AVAIL_OUT (block
), newe
);
3329 bitmap_insert_into_set (NEW_SETS (block
), newe
);
3339 /* Perform insertion for partially anticipatable expressions. There
3340 is only one case we will perform insertion for these. This case is
3341 if the expression is partially anticipatable, and fully available.
3342 In this case, we know that putting it earlier will enable us to
3343 remove the later computation. */
3346 do_pre_partial_partial_insertion (basic_block block
, basic_block dom
)
3348 bool new_stuff
= false;
3349 vec
<pre_expr
> exprs
;
3351 auto_vec
<pre_expr
> avail
;
3354 exprs
= sorted_array_from_bitmap_set (PA_IN (block
));
3355 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3357 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3359 if (expr
->kind
== NARY
3360 || expr
->kind
== REFERENCE
)
3364 bool cant_insert
= false;
3367 pre_expr eprime
= NULL
;
3370 val
= get_expr_value_id (expr
);
3371 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3373 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3376 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3378 unsigned int vprime
;
3379 pre_expr edoubleprime
;
3381 /* We should never run insertion for the exit block
3382 and so not come across fake pred edges. */
3383 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3385 eprime
= phi_translate (expr
, ANTIC_IN (block
),
3389 /* eprime will generally only be NULL if the
3390 value of the expression, translated
3391 through the PHI for this predecessor, is
3392 undefined. If that is the case, we can't
3393 make the expression fully redundant,
3394 because its value is undefined along a
3395 predecessor path. We can thus break out
3396 early because it doesn't matter what the
3397 rest of the results are. */
3400 avail
[pred
->dest_idx
] = NULL
;
3405 vprime
= get_expr_value_id (eprime
);
3406 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
), vprime
);
3407 avail
[pred
->dest_idx
] = edoubleprime
;
3408 if (edoubleprime
== NULL
)
3415 /* If we can insert it, it's not the same value
3416 already existing along every predecessor, and
3417 it's defined by some predecessor, it is
3418 partially redundant. */
3419 if (!cant_insert
&& by_all
)
3422 bool do_insertion
= false;
3424 /* Insert only if we can remove a later expression on a path
3425 that we want to optimize for speed.
3426 The phi node that we will be inserting in BLOCK is not free,
3427 and inserting it for the sake of !optimize_for_speed successor
3428 may cause regressions on the speed path. */
3429 FOR_EACH_EDGE (succ
, ei
, block
->succs
)
3431 if (bitmap_set_contains_value (PA_IN (succ
->dest
), val
)
3432 || bitmap_set_contains_value (ANTIC_IN (succ
->dest
), val
))
3434 if (optimize_edge_for_speed_p (succ
))
3435 do_insertion
= true;
3441 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3443 fprintf (dump_file
, "Skipping partial partial redundancy "
3445 print_pre_expr (dump_file
, expr
);
3446 fprintf (dump_file
, " (%04d), not (partially) anticipated "
3447 "on any to be optimized for speed edges\n", val
);
3450 else if (dbg_cnt (treepre_insert
))
3452 pre_stats
.pa_insert
++;
3453 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3455 fprintf (dump_file
, "Found partial partial redundancy "
3457 print_pre_expr (dump_file
, expr
);
3458 fprintf (dump_file
, " (%04d)\n",
3459 get_expr_value_id (expr
));
3461 if (insert_into_preds_of_block (block
,
3462 get_expression_id (expr
),
3474 /* Insert expressions in BLOCK to compute hoistable values up.
3475 Return TRUE if something was inserted, otherwise return FALSE.
3476 The caller has to make sure that BLOCK has at least two successors. */
3479 do_hoist_insertion (basic_block block
)
3483 bool new_stuff
= false;
3485 gimple_stmt_iterator last
;
3487 /* At least two successors, or else... */
3488 gcc_assert (EDGE_COUNT (block
->succs
) >= 2);
3490 /* Check that all successors of BLOCK are dominated by block.
3491 We could use dominated_by_p() for this, but actually there is a much
3492 quicker check: any successor that is dominated by BLOCK can't have
3493 more than one predecessor edge. */
3494 FOR_EACH_EDGE (e
, ei
, block
->succs
)
3495 if (! single_pred_p (e
->dest
))
3498 /* Determine the insertion point. If we cannot safely insert before
3499 the last stmt if we'd have to, bail out. */
3500 last
= gsi_last_bb (block
);
3501 if (!gsi_end_p (last
)
3502 && !is_ctrl_stmt (gsi_stmt (last
))
3503 && stmt_ends_bb_p (gsi_stmt (last
)))
3506 /* Compute the set of hoistable expressions from ANTIC_IN. First compute
3507 hoistable values. */
3508 bitmap_set hoistable_set
;
3510 /* A hoistable value must be in ANTIC_IN(block)
3511 but not in AVAIL_OUT(BLOCK). */
3512 bitmap_initialize (&hoistable_set
.values
, &grand_bitmap_obstack
);
3513 bitmap_and_compl (&hoistable_set
.values
,
3514 &ANTIC_IN (block
)->values
, &AVAIL_OUT (block
)->values
);
3516 /* Short-cut for a common case: hoistable_set is empty. */
3517 if (bitmap_empty_p (&hoistable_set
.values
))
3520 /* Compute which of the hoistable values is in AVAIL_OUT of
3521 at least one of the successors of BLOCK. */
3522 bitmap_head availout_in_some
;
3523 bitmap_initialize (&availout_in_some
, &grand_bitmap_obstack
);
3524 FOR_EACH_EDGE (e
, ei
, block
->succs
)
3525 /* Do not consider expressions solely because their availability
3526 on loop exits. They'd be ANTIC-IN throughout the whole loop
3527 and thus effectively hoisted across loops by combination of
3528 PRE and hoisting. */
3529 if (! loop_exit_edge_p (block
->loop_father
, e
))
3530 bitmap_ior_and_into (&availout_in_some
, &hoistable_set
.values
,
3531 &AVAIL_OUT (e
->dest
)->values
);
3532 bitmap_clear (&hoistable_set
.values
);
3534 /* Short-cut for a common case: availout_in_some is empty. */
3535 if (bitmap_empty_p (&availout_in_some
))
3538 /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */
3539 hoistable_set
.values
= availout_in_some
;
3540 hoistable_set
.expressions
= ANTIC_IN (block
)->expressions
;
3542 /* Now finally construct the topological-ordered expression set. */
3543 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (&hoistable_set
);
3545 bitmap_clear (&hoistable_set
.values
);
3547 /* If there are candidate values for hoisting, insert expressions
3548 strategically to make the hoistable expressions fully redundant. */
3550 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3552 /* While we try to sort expressions topologically above the
3553 sorting doesn't work out perfectly. Catch expressions we
3554 already inserted. */
3555 unsigned int value_id
= get_expr_value_id (expr
);
3556 if (bitmap_set_contains_value (AVAIL_OUT (block
), value_id
))
3558 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3561 "Already inserted expression for ");
3562 print_pre_expr (dump_file
, expr
);
3563 fprintf (dump_file
, " (%04d)\n", value_id
);
3568 /* OK, we should hoist this value. Perform the transformation. */
3569 pre_stats
.hoist_insert
++;
3570 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3573 "Inserting expression in block %d for code hoisting: ",
3575 print_pre_expr (dump_file
, expr
);
3576 fprintf (dump_file
, " (%04d)\n", value_id
);
3579 gimple_seq stmts
= NULL
;
3580 tree res
= create_expression_by_pieces (block
, expr
, &stmts
,
3581 get_expr_type (expr
));
3583 /* Do not return true if expression creation ultimately
3584 did not insert any statements. */
3585 if (gimple_seq_empty_p (stmts
))
3589 if (gsi_end_p (last
) || is_ctrl_stmt (gsi_stmt (last
)))
3590 gsi_insert_seq_before (&last
, stmts
, GSI_SAME_STMT
);
3592 gsi_insert_seq_after (&last
, stmts
, GSI_NEW_STMT
);
3595 /* Make sure to not return true if expression creation ultimately
3596 failed but also make sure to insert any stmts produced as they
3597 are tracked in inserted_exprs. */
3609 /* Do a dominator walk on the control flow graph, and insert computations
3610 of values as necessary for PRE and hoisting. */
3613 insert_aux (basic_block block
, bool do_pre
, bool do_hoist
)
3616 bool new_stuff
= false;
3621 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3626 bitmap_set_t newset
;
3628 /* First, update the AVAIL_OUT set with anything we may have
3629 inserted higher up in the dominator tree. */
3630 newset
= NEW_SETS (dom
);
3633 /* Note that we need to value_replace both NEW_SETS, and
3634 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3635 represented by some non-simple expression here that we want
3636 to replace it with. */
3637 FOR_EACH_EXPR_ID_IN_SET (newset
, i
, bi
)
3639 pre_expr expr
= expression_for_id (i
);
3640 bitmap_value_replace_in_set (NEW_SETS (block
), expr
);
3641 bitmap_value_replace_in_set (AVAIL_OUT (block
), expr
);
3645 /* Insert expressions for partial redundancies. */
3646 if (do_pre
&& !single_pred_p (block
))
3648 new_stuff
|= do_pre_regular_insertion (block
, dom
);
3649 if (do_partial_partial
)
3650 new_stuff
|= do_pre_partial_partial_insertion (block
, dom
);
3653 /* Insert expressions for hoisting. */
3654 if (do_hoist
&& EDGE_COUNT (block
->succs
) >= 2)
3655 new_stuff
|= do_hoist_insertion (block
);
3658 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
3660 son
= next_dom_son (CDI_DOMINATORS
, son
))
3662 new_stuff
|= insert_aux (son
, do_pre
, do_hoist
);
3668 /* Perform insertion of partially redundant and hoistable values. */
3673 bool new_stuff
= true;
3675 int num_iterations
= 0;
3677 FOR_ALL_BB_FN (bb
, cfun
)
3678 NEW_SETS (bb
) = bitmap_set_new ();
3683 if (dump_file
&& dump_flags
& TDF_DETAILS
)
3684 fprintf (dump_file
, "Starting insert iteration %d\n", num_iterations
);
3685 new_stuff
= insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun
), flag_tree_pre
,
3686 flag_code_hoisting
);
3688 /* Clear the NEW sets before the next iteration. We have already
3689 fully propagated its contents. */
3691 FOR_ALL_BB_FN (bb
, cfun
)
3692 bitmap_set_free (NEW_SETS (bb
));
3694 statistics_histogram_event (cfun
, "insert iterations", num_iterations
);
3698 /* Compute the AVAIL set for all basic blocks.
3700 This function performs value numbering of the statements in each basic
3701 block. The AVAIL sets are built from information we glean while doing
3702 this value numbering, since the AVAIL sets contain only one entry per
3705 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3706 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3709 compute_avail (void)
3712 basic_block block
, son
;
3713 basic_block
*worklist
;
3718 /* We pretend that default definitions are defined in the entry block.
3719 This includes function arguments and the static chain decl. */
3720 FOR_EACH_SSA_NAME (i
, name
, cfun
)
3723 if (!SSA_NAME_IS_DEFAULT_DEF (name
)
3724 || has_zero_uses (name
)
3725 || virtual_operand_p (name
))
3728 e
= get_or_alloc_expr_for_name (name
);
3729 add_to_value (get_expr_value_id (e
), e
);
3730 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)), e
);
3731 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3735 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3737 print_bitmap_set (dump_file
, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3738 "tmp_gen", ENTRY_BLOCK
);
3739 print_bitmap_set (dump_file
, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3740 "avail_out", ENTRY_BLOCK
);
3743 /* Allocate the worklist. */
3744 worklist
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
3746 /* Seed the algorithm by putting the dominator children of the entry
3747 block on the worklist. */
3748 for (son
= first_dom_son (CDI_DOMINATORS
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3750 son
= next_dom_son (CDI_DOMINATORS
, son
))
3751 worklist
[sp
++] = son
;
3753 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun
))
3754 = ssa_default_def (cfun
, gimple_vop (cfun
));
3756 /* Loop until the worklist is empty. */
3762 /* Pick a block from the worklist. */
3763 block
= worklist
[--sp
];
3765 /* Initially, the set of available values in BLOCK is that of
3766 its immediate dominator. */
3767 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3770 bitmap_set_copy (AVAIL_OUT (block
), AVAIL_OUT (dom
));
3771 BB_LIVE_VOP_ON_EXIT (block
) = BB_LIVE_VOP_ON_EXIT (dom
);
3774 /* Generate values for PHI nodes. */
3775 for (gphi_iterator gsi
= gsi_start_phis (block
); !gsi_end_p (gsi
);
3778 tree result
= gimple_phi_result (gsi
.phi ());
3780 /* We have no need for virtual phis, as they don't represent
3781 actual computations. */
3782 if (virtual_operand_p (result
))
3784 BB_LIVE_VOP_ON_EXIT (block
) = result
;
3788 pre_expr e
= get_or_alloc_expr_for_name (result
);
3789 add_to_value (get_expr_value_id (e
), e
);
3790 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3791 bitmap_insert_into_set (PHI_GEN (block
), e
);
3794 BB_MAY_NOTRETURN (block
) = 0;
3796 /* Now compute value numbers and populate value sets with all
3797 the expressions computed in BLOCK. */
3798 for (gimple_stmt_iterator gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
);
3804 stmt
= gsi_stmt (gsi
);
3806 /* Cache whether the basic-block has any non-visible side-effect
3808 If this isn't a call or it is the last stmt in the
3809 basic-block then the CFG represents things correctly. */
3810 if (is_gimple_call (stmt
) && !stmt_ends_bb_p (stmt
))
3812 /* Non-looping const functions always return normally.
3813 Otherwise the call might not return or have side-effects
3814 that forbids hoisting possibly trapping expressions
3816 int flags
= gimple_call_flags (stmt
);
3817 if (!(flags
& ECF_CONST
)
3818 || (flags
& ECF_LOOPING_CONST_OR_PURE
))
3819 BB_MAY_NOTRETURN (block
) = 1;
3822 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
3824 pre_expr e
= get_or_alloc_expr_for_name (op
);
3826 add_to_value (get_expr_value_id (e
), e
);
3827 bitmap_insert_into_set (TMP_GEN (block
), e
);
3828 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3831 if (gimple_vdef (stmt
))
3832 BB_LIVE_VOP_ON_EXIT (block
) = gimple_vdef (stmt
);
3834 if (gimple_has_side_effects (stmt
)
3835 || stmt_could_throw_p (stmt
)
3836 || is_gimple_debug (stmt
))
3839 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
3841 if (ssa_undefined_value_p (op
))
3843 pre_expr e
= get_or_alloc_expr_for_name (op
);
3844 bitmap_value_insert_into_set (EXP_GEN (block
), e
);
3847 switch (gimple_code (stmt
))
3855 vn_reference_s ref1
;
3856 pre_expr result
= NULL
;
3858 /* We can value number only calls to real functions. */
3859 if (gimple_call_internal_p (stmt
))
3862 vn_reference_lookup_call (as_a
<gcall
*> (stmt
), &ref
, &ref1
);
3866 /* If the value of the call is not invalidated in
3867 this block until it is computed, add the expression
3869 if (!gimple_vuse (stmt
)
3871 (SSA_NAME_DEF_STMT (gimple_vuse (stmt
))) == GIMPLE_PHI
3872 || gimple_bb (SSA_NAME_DEF_STMT
3873 (gimple_vuse (stmt
))) != block
)
3875 result
= pre_expr_pool
.allocate ();
3876 result
->kind
= REFERENCE
;
3878 PRE_EXPR_REFERENCE (result
) = ref
;
3880 get_or_alloc_expression_id (result
);
3881 add_to_value (get_expr_value_id (result
), result
);
3882 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
3889 pre_expr result
= NULL
;
3890 switch (vn_get_stmt_kind (stmt
))
3894 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3897 /* COND_EXPR and VEC_COND_EXPR are awkward in
3898 that they contain an embedded complex expression.
3899 Don't even try to shove those through PRE. */
3900 if (code
== COND_EXPR
3901 || code
== VEC_COND_EXPR
)
3904 vn_nary_op_lookup_stmt (stmt
, &nary
);
3908 /* If the NARY traps and there was a preceding
3909 point in the block that might not return avoid
3910 adding the nary to EXP_GEN. */
3911 if (BB_MAY_NOTRETURN (block
)
3912 && vn_nary_may_trap (nary
))
3915 result
= pre_expr_pool
.allocate ();
3916 result
->kind
= NARY
;
3918 PRE_EXPR_NARY (result
) = nary
;
3924 tree rhs1
= gimple_assign_rhs1 (stmt
);
3925 alias_set_type set
= get_alias_set (rhs1
);
3926 vec
<vn_reference_op_s
> operands
3927 = vn_reference_operands_for_lookup (rhs1
);
3929 vn_reference_lookup_pieces (gimple_vuse (stmt
), set
,
3931 operands
, &ref
, VN_WALK
);
3934 operands
.release ();
3938 /* If the value of the reference is not invalidated in
3939 this block until it is computed, add the expression
3941 if (gimple_vuse (stmt
))
3945 def_stmt
= SSA_NAME_DEF_STMT (gimple_vuse (stmt
));
3946 while (!gimple_nop_p (def_stmt
)
3947 && gimple_code (def_stmt
) != GIMPLE_PHI
3948 && gimple_bb (def_stmt
) == block
)
3950 if (stmt_may_clobber_ref_p
3951 (def_stmt
, gimple_assign_rhs1 (stmt
)))
3957 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt
));
3961 operands
.release ();
3966 /* If the load was value-numbered to another
3967 load make sure we do not use its expression
3968 for insertion if it wouldn't be a valid
3970 /* At the momemt we have a testcase
3971 for hoist insertion of aligned vs. misaligned
3972 variants in gcc.dg/torture/pr65270-1.c thus
3973 with just alignment to be considered we can
3974 simply replace the expression in the hashtable
3975 with the most conservative one. */
3976 vn_reference_op_t ref1
= &ref
->operands
.last ();
3977 while (ref1
->opcode
!= TARGET_MEM_REF
3978 && ref1
->opcode
!= MEM_REF
3979 && ref1
!= &ref
->operands
[0])
3981 vn_reference_op_t ref2
= &operands
.last ();
3982 while (ref2
->opcode
!= TARGET_MEM_REF
3983 && ref2
->opcode
!= MEM_REF
3984 && ref2
!= &operands
[0])
3986 if ((ref1
->opcode
== TARGET_MEM_REF
3987 || ref1
->opcode
== MEM_REF
)
3988 && (TYPE_ALIGN (ref1
->type
)
3989 > TYPE_ALIGN (ref2
->type
)))
3991 = build_aligned_type (ref1
->type
,
3992 TYPE_ALIGN (ref2
->type
));
3993 /* TBAA behavior is an obvious part so make sure
3994 that the hashtable one covers this as well
3995 by adjusting the ref alias set and its base. */
3997 || alias_set_subset_of (set
, ref
->set
))
3999 else if (alias_set_subset_of (ref
->set
, set
))
4002 if (ref1
->opcode
== MEM_REF
)
4003 ref1
->op0
= wide_int_to_tree (TREE_TYPE (ref2
->op0
),
4006 ref1
->op2
= wide_int_to_tree (TREE_TYPE (ref2
->op2
),
4012 if (ref1
->opcode
== MEM_REF
)
4013 ref1
->op0
= wide_int_to_tree (ptr_type_node
,
4016 ref1
->op2
= wide_int_to_tree (ptr_type_node
,
4019 operands
.release ();
4021 result
= pre_expr_pool
.allocate ();
4022 result
->kind
= REFERENCE
;
4024 PRE_EXPR_REFERENCE (result
) = ref
;
4032 get_or_alloc_expression_id (result
);
4033 add_to_value (get_expr_value_id (result
), result
);
4034 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
4042 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4044 print_bitmap_set (dump_file
, EXP_GEN (block
),
4045 "exp_gen", block
->index
);
4046 print_bitmap_set (dump_file
, PHI_GEN (block
),
4047 "phi_gen", block
->index
);
4048 print_bitmap_set (dump_file
, TMP_GEN (block
),
4049 "tmp_gen", block
->index
);
4050 print_bitmap_set (dump_file
, AVAIL_OUT (block
),
4051 "avail_out", block
->index
);
4054 /* Put the dominator children of BLOCK on the worklist of blocks
4055 to compute available sets for. */
4056 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
4058 son
= next_dom_son (CDI_DOMINATORS
, son
))
4059 worklist
[sp
++] = son
;
4066 /* Local state for the eliminate domwalk. */
4067 static vec
<gimple
*> el_to_remove
;
4068 static vec
<gimple
*> el_to_fixup
;
4069 static unsigned int el_todo
;
4070 static vec
<tree
> el_avail
;
4071 static vec
<tree
> el_avail_stack
;
4073 /* Return a leader for OP that is available at the current point of the
4074 eliminate domwalk. */
4077 eliminate_avail (tree op
)
4079 tree valnum
= VN_INFO (op
)->valnum
;
4080 if (TREE_CODE (valnum
) == SSA_NAME
)
4082 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
4084 if (el_avail
.length () > SSA_NAME_VERSION (valnum
))
4085 return el_avail
[SSA_NAME_VERSION (valnum
)];
4087 else if (is_gimple_min_invariant (valnum
))
4092 /* At the current point of the eliminate domwalk make OP available. */
4095 eliminate_push_avail (tree op
)
4097 tree valnum
= VN_INFO (op
)->valnum
;
4098 if (TREE_CODE (valnum
) == SSA_NAME
)
4100 if (el_avail
.length () <= SSA_NAME_VERSION (valnum
))
4101 el_avail
.safe_grow_cleared (SSA_NAME_VERSION (valnum
) + 1);
4103 if (el_avail
[SSA_NAME_VERSION (valnum
)])
4104 pushop
= el_avail
[SSA_NAME_VERSION (valnum
)];
4105 el_avail_stack
.safe_push (pushop
);
4106 el_avail
[SSA_NAME_VERSION (valnum
)] = op
;
4110 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
4111 the leader for the expression if insertion was successful. */
4114 eliminate_insert (gimple_stmt_iterator
*gsi
, tree val
)
4116 /* We can insert a sequence with a single assignment only. */
4117 gimple_seq stmts
= VN_INFO (val
)->expr
;
4118 if (!gimple_seq_singleton_p (stmts
))
4120 gassign
*stmt
= dyn_cast
<gassign
*> (gimple_seq_first_stmt (stmts
));
4122 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt
))
4123 && gimple_assign_rhs_code (stmt
) != VIEW_CONVERT_EXPR
4124 && gimple_assign_rhs_code (stmt
) != BIT_FIELD_REF
4125 && (gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
4126 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)))
4129 tree op
= gimple_assign_rhs1 (stmt
);
4130 if (gimple_assign_rhs_code (stmt
) == VIEW_CONVERT_EXPR
4131 || gimple_assign_rhs_code (stmt
) == BIT_FIELD_REF
)
4132 op
= TREE_OPERAND (op
, 0);
4133 tree leader
= TREE_CODE (op
) == SSA_NAME
? eliminate_avail (op
) : op
;
4139 if (gimple_assign_rhs_code (stmt
) == BIT_FIELD_REF
)
4140 res
= gimple_build (&stmts
, BIT_FIELD_REF
,
4141 TREE_TYPE (val
), leader
,
4142 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 1),
4143 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 2));
4144 else if (gimple_assign_rhs_code (stmt
) == BIT_AND_EXPR
)
4145 res
= gimple_build (&stmts
, BIT_AND_EXPR
,
4146 TREE_TYPE (val
), leader
, gimple_assign_rhs2 (stmt
));
4148 res
= gimple_build (&stmts
, gimple_assign_rhs_code (stmt
),
4149 TREE_TYPE (val
), leader
);
4150 if (TREE_CODE (res
) != SSA_NAME
4151 || SSA_NAME_IS_DEFAULT_DEF (res
)
4152 || gimple_bb (SSA_NAME_DEF_STMT (res
)))
4154 gimple_seq_discard (stmts
);
4156 /* During propagation we have to treat SSA info conservatively
4157 and thus we can end up simplifying the inserted expression
4158 at elimination time to sth not defined in stmts. */
4159 /* But then this is a redundancy we failed to detect. Which means
4160 res now has two values. That doesn't play well with how
4161 we track availability here, so give up. */
4162 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4164 if (TREE_CODE (res
) == SSA_NAME
)
4165 res
= eliminate_avail (res
);
4168 fprintf (dump_file
, "Failed to insert expression for value ");
4169 print_generic_expr (dump_file
, val
, 0);
4170 fprintf (dump_file
, " which is really fully redundant to ");
4171 print_generic_expr (dump_file
, res
, 0);
4172 fprintf (dump_file
, "\n");
4180 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
4181 VN_INFO_GET (res
)->valnum
= val
;
4183 if (TREE_CODE (leader
) == SSA_NAME
)
4184 gimple_set_plf (SSA_NAME_DEF_STMT (leader
), NECESSARY
, true);
4187 pre_stats
.insertions
++;
4188 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4190 fprintf (dump_file
, "Inserted ");
4191 print_gimple_stmt (dump_file
, SSA_NAME_DEF_STMT (res
), 0, 0);
4197 class eliminate_dom_walker
: public dom_walker
4200 eliminate_dom_walker (cdi_direction direction
, bool do_pre_
)
4201 : dom_walker (direction
), do_pre (do_pre_
) {}
4203 virtual edge
before_dom_children (basic_block
);
4204 virtual void after_dom_children (basic_block
);
4209 /* Perform elimination for the basic-block B during the domwalk. */
4212 eliminate_dom_walker::before_dom_children (basic_block b
)
4215 el_avail_stack
.safe_push (NULL_TREE
);
4217 /* ??? If we do nothing for unreachable blocks then this will confuse
4218 tailmerging. Eventually we can reduce its reliance on SCCVN now
4219 that we fully copy/constant-propagate (most) things. */
4221 for (gphi_iterator gsi
= gsi_start_phis (b
); !gsi_end_p (gsi
);)
4223 gphi
*phi
= gsi
.phi ();
4224 tree res
= PHI_RESULT (phi
);
4226 if (virtual_operand_p (res
))
4232 tree sprime
= eliminate_avail (res
);
4236 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4238 fprintf (dump_file
, "Replaced redundant PHI node defining ");
4239 print_generic_expr (dump_file
, res
, 0);
4240 fprintf (dump_file
, " with ");
4241 print_generic_expr (dump_file
, sprime
, 0);
4242 fprintf (dump_file
, "\n");
4245 /* If we inserted this PHI node ourself, it's not an elimination. */
4247 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
)))
4250 pre_stats
.eliminations
++;
4252 /* If we will propagate into all uses don't bother to do
4254 if (may_propagate_copy (res
, sprime
))
4256 /* Mark the PHI for removal. */
4257 el_to_remove
.safe_push (phi
);
4262 remove_phi_node (&gsi
, false);
4265 && !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
))
4266 && TREE_CODE (sprime
) == SSA_NAME
)
4267 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
4269 if (!useless_type_conversion_p (TREE_TYPE (res
), TREE_TYPE (sprime
)))
4270 sprime
= fold_convert (TREE_TYPE (res
), sprime
);
4271 gimple
*stmt
= gimple_build_assign (res
, sprime
);
4272 /* ??? It cannot yet be necessary (DOM walk). */
4273 gimple_set_plf (stmt
, NECESSARY
, gimple_plf (phi
, NECESSARY
));
4275 gimple_stmt_iterator gsi2
= gsi_after_labels (b
);
4276 gsi_insert_before (&gsi2
, stmt
, GSI_NEW_STMT
);
4280 eliminate_push_avail (res
);
4284 for (gimple_stmt_iterator gsi
= gsi_start_bb (b
);
4288 tree sprime
= NULL_TREE
;
4289 gimple
*stmt
= gsi_stmt (gsi
);
4290 tree lhs
= gimple_get_lhs (stmt
);
4291 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
4292 && !gimple_has_volatile_ops (stmt
)
4293 /* See PR43491. Do not replace a global register variable when
4294 it is a the RHS of an assignment. Do replace local register
4295 variables since gcc does not guarantee a local variable will
4296 be allocated in register.
4297 ??? The fix isn't effective here. This should instead
4298 be ensured by not value-numbering them the same but treating
4299 them like volatiles? */
4300 && !(gimple_assign_single_p (stmt
)
4301 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == VAR_DECL
4302 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
))
4303 && is_global_var (gimple_assign_rhs1 (stmt
)))))
4305 sprime
= eliminate_avail (lhs
);
4308 /* If there is no existing usable leader but SCCVN thinks
4309 it has an expression it wants to use as replacement,
4311 tree val
= VN_INFO (lhs
)->valnum
;
4313 && TREE_CODE (val
) == SSA_NAME
4314 && VN_INFO (val
)->needs_insertion
4315 && VN_INFO (val
)->expr
!= NULL
4316 && (sprime
= eliminate_insert (&gsi
, val
)) != NULL_TREE
)
4317 eliminate_push_avail (sprime
);
4320 /* If this now constitutes a copy duplicate points-to
4321 and range info appropriately. This is especially
4322 important for inserted code. See tree-ssa-copy.c
4323 for similar code. */
4325 && TREE_CODE (sprime
) == SSA_NAME
)
4327 basic_block sprime_b
= gimple_bb (SSA_NAME_DEF_STMT (sprime
));
4328 if (POINTER_TYPE_P (TREE_TYPE (lhs
))
4329 && VN_INFO_PTR_INFO (lhs
)
4330 && ! VN_INFO_PTR_INFO (sprime
))
4332 duplicate_ssa_name_ptr_info (sprime
,
4333 VN_INFO_PTR_INFO (lhs
));
4335 mark_ptr_info_alignment_unknown
4336 (SSA_NAME_PTR_INFO (sprime
));
4338 else if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
4339 && VN_INFO_RANGE_INFO (lhs
)
4340 && ! VN_INFO_RANGE_INFO (sprime
)
4342 duplicate_ssa_name_range_info (sprime
,
4343 VN_INFO_RANGE_TYPE (lhs
),
4344 VN_INFO_RANGE_INFO (lhs
));
4347 /* Inhibit the use of an inserted PHI on a loop header when
4348 the address of the memory reference is a simple induction
4349 variable. In other cases the vectorizer won't do anything
4350 anyway (either it's loop invariant or a complicated
4353 && TREE_CODE (sprime
) == SSA_NAME
4355 && (flag_tree_loop_vectorize
|| flag_tree_parallelize_loops
> 1)
4356 && loop_outer (b
->loop_father
)
4357 && has_zero_uses (sprime
)
4358 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))
4359 && gimple_assign_load_p (stmt
))
4361 gimple
*def_stmt
= SSA_NAME_DEF_STMT (sprime
);
4362 basic_block def_bb
= gimple_bb (def_stmt
);
4363 if (gimple_code (def_stmt
) == GIMPLE_PHI
4364 && def_bb
->loop_father
->header
== def_bb
)
4366 loop_p loop
= def_bb
->loop_father
;
4370 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
4373 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (op
));
4375 && flow_bb_inside_loop_p (loop
, def_bb
)
4376 && simple_iv (loop
, loop
, op
, &iv
, true))
4384 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4386 fprintf (dump_file
, "Not replacing ");
4387 print_gimple_expr (dump_file
, stmt
, 0, 0);
4388 fprintf (dump_file
, " with ");
4389 print_generic_expr (dump_file
, sprime
, 0);
4390 fprintf (dump_file
, " which would add a loop"
4391 " carried dependence to loop %d\n",
4394 /* Don't keep sprime available. */
4402 /* If we can propagate the value computed for LHS into
4403 all uses don't bother doing anything with this stmt. */
4404 if (may_propagate_copy (lhs
, sprime
))
4406 /* Mark it for removal. */
4407 el_to_remove
.safe_push (stmt
);
4409 /* ??? Don't count copy/constant propagations. */
4410 if (gimple_assign_single_p (stmt
)
4411 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4412 || gimple_assign_rhs1 (stmt
) == sprime
))
4415 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4417 fprintf (dump_file
, "Replaced ");
4418 print_gimple_expr (dump_file
, stmt
, 0, 0);
4419 fprintf (dump_file
, " with ");
4420 print_generic_expr (dump_file
, sprime
, 0);
4421 fprintf (dump_file
, " in all uses of ");
4422 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4425 pre_stats
.eliminations
++;
4429 /* If this is an assignment from our leader (which
4430 happens in the case the value-number is a constant)
4431 then there is nothing to do. */
4432 if (gimple_assign_single_p (stmt
)
4433 && sprime
== gimple_assign_rhs1 (stmt
))
4436 /* Else replace its RHS. */
4437 bool can_make_abnormal_goto
4438 = is_gimple_call (stmt
)
4439 && stmt_can_make_abnormal_goto (stmt
);
4441 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4443 fprintf (dump_file
, "Replaced ");
4444 print_gimple_expr (dump_file
, stmt
, 0, 0);
4445 fprintf (dump_file
, " with ");
4446 print_generic_expr (dump_file
, sprime
, 0);
4447 fprintf (dump_file
, " in ");
4448 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4451 if (TREE_CODE (sprime
) == SSA_NAME
)
4452 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4455 pre_stats
.eliminations
++;
4456 gimple
*orig_stmt
= stmt
;
4457 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
4458 TREE_TYPE (sprime
)))
4459 sprime
= fold_convert (TREE_TYPE (lhs
), sprime
);
4460 tree vdef
= gimple_vdef (stmt
);
4461 tree vuse
= gimple_vuse (stmt
);
4462 propagate_tree_value_into_stmt (&gsi
, sprime
);
4463 stmt
= gsi_stmt (gsi
);
4465 if (vdef
!= gimple_vdef (stmt
))
4466 VN_INFO (vdef
)->valnum
= vuse
;
4468 /* If we removed EH side-effects from the statement, clean
4469 its EH information. */
4470 if (maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
))
4472 bitmap_set_bit (need_eh_cleanup
,
4473 gimple_bb (stmt
)->index
);
4474 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4475 fprintf (dump_file
, " Removed EH side-effects.\n");
4478 /* Likewise for AB side-effects. */
4479 if (can_make_abnormal_goto
4480 && !stmt_can_make_abnormal_goto (stmt
))
4482 bitmap_set_bit (need_ab_cleanup
,
4483 gimple_bb (stmt
)->index
);
4484 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4485 fprintf (dump_file
, " Removed AB side-effects.\n");
4492 /* If the statement is a scalar store, see if the expression
4493 has the same value number as its rhs. If so, the store is
4495 if (gimple_assign_single_p (stmt
)
4496 && !gimple_has_volatile_ops (stmt
)
4497 && !is_gimple_reg (gimple_assign_lhs (stmt
))
4498 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4499 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
))))
4502 tree rhs
= gimple_assign_rhs1 (stmt
);
4503 vn_reference_t vnresult
;
4504 val
= vn_reference_lookup (lhs
, gimple_vuse (stmt
), VN_WALKREWRITE
,
4506 if (TREE_CODE (rhs
) == SSA_NAME
)
4507 rhs
= VN_INFO (rhs
)->valnum
;
4509 && operand_equal_p (val
, rhs
, 0))
4511 /* We can only remove the later store if the former aliases
4512 at least all accesses the later one does or if the store
4513 was to readonly memory storing the same value. */
4514 alias_set_type set
= get_alias_set (lhs
);
4516 || vnresult
->set
== set
4517 || alias_set_subset_of (set
, vnresult
->set
))
4519 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4521 fprintf (dump_file
, "Deleted redundant store ");
4522 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4525 /* Queue stmt for removal. */
4526 el_to_remove
.safe_push (stmt
);
4532 /* If this is a control statement value numbering left edges
4533 unexecuted on force the condition in a way consistent with
4535 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
4537 if ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
)
4538 ^ (EDGE_SUCC (b
, 1)->flags
& EDGE_EXECUTABLE
))
4540 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4542 fprintf (dump_file
, "Removing unexecutable edge from ");
4543 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4545 if (((EDGE_SUCC (b
, 0)->flags
& EDGE_TRUE_VALUE
) != 0)
4546 == ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
) != 0))
4547 gimple_cond_make_true (cond
);
4549 gimple_cond_make_false (cond
);
4551 el_todo
|= TODO_cleanup_cfg
;
4556 bool can_make_abnormal_goto
= stmt_can_make_abnormal_goto (stmt
);
4557 bool was_noreturn
= (is_gimple_call (stmt
)
4558 && gimple_call_noreturn_p (stmt
));
4559 tree vdef
= gimple_vdef (stmt
);
4560 tree vuse
= gimple_vuse (stmt
);
4562 /* If we didn't replace the whole stmt (or propagate the result
4563 into all uses), replace all uses on this stmt with their
4565 use_operand_p use_p
;
4567 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
4569 tree use
= USE_FROM_PTR (use_p
);
4570 /* ??? The call code above leaves stmt operands un-updated. */
4571 if (TREE_CODE (use
) != SSA_NAME
)
4573 tree sprime
= eliminate_avail (use
);
4574 if (sprime
&& sprime
!= use
4575 && may_propagate_copy (use
, sprime
)
4576 /* We substitute into debug stmts to avoid excessive
4577 debug temporaries created by removed stmts, but we need
4578 to avoid doing so for inserted sprimes as we never want
4579 to create debug temporaries for them. */
4581 || TREE_CODE (sprime
) != SSA_NAME
4582 || !is_gimple_debug (stmt
)
4583 || !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))))
4585 propagate_value (use_p
, sprime
);
4586 gimple_set_modified (stmt
, true);
4587 if (TREE_CODE (sprime
) == SSA_NAME
4588 && !is_gimple_debug (stmt
))
4589 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4594 /* Visit indirect calls and turn them into direct calls if
4595 possible using the devirtualization machinery. */
4596 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
4598 tree fn
= gimple_call_fn (call_stmt
);
4600 && flag_devirtualize
4601 && virtual_method_call_p (fn
))
4603 tree otr_type
= obj_type_ref_class (fn
);
4605 ipa_polymorphic_call_context
context (current_function_decl
, fn
, stmt
, &instance
);
4608 context
.get_dynamic_type (instance
, OBJ_TYPE_REF_OBJECT (fn
), otr_type
, stmt
);
4610 vec
<cgraph_node
*>targets
4611 = possible_polymorphic_call_targets (obj_type_ref_class (fn
),
4613 (OBJ_TYPE_REF_TOKEN (fn
)),
4617 dump_possible_polymorphic_call_targets (dump_file
,
4618 obj_type_ref_class (fn
),
4620 (OBJ_TYPE_REF_TOKEN (fn
)),
4622 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
4625 if (targets
.length () == 1)
4626 fn
= targets
[0]->decl
;
4628 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
4629 if (dump_enabled_p ())
4631 location_t loc
= gimple_location_safe (stmt
);
4632 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
4633 "converting indirect call to "
4635 lang_hooks
.decl_printable_name (fn
, 2));
4637 gimple_call_set_fndecl (call_stmt
, fn
);
4638 /* If changing the call to __builtin_unreachable
4639 or similar noreturn function, adjust gimple_call_fntype
4641 if (gimple_call_noreturn_p (call_stmt
)
4642 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn
)))
4643 && TYPE_ARG_TYPES (TREE_TYPE (fn
))
4644 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4646 gimple_call_set_fntype (call_stmt
, TREE_TYPE (fn
));
4647 maybe_remove_unused_call_args (cfun
, call_stmt
);
4648 gimple_set_modified (stmt
, true);
4653 if (gimple_modified_p (stmt
))
4655 /* If a formerly non-invariant ADDR_EXPR is turned into an
4656 invariant one it was on a separate stmt. */
4657 if (gimple_assign_single_p (stmt
)
4658 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
4659 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
4660 gimple
*old_stmt
= stmt
;
4661 if (is_gimple_call (stmt
))
4663 /* ??? Only fold calls inplace for now, this may create new
4664 SSA names which in turn will confuse free_scc_vn SSA name
4666 fold_stmt_inplace (&gsi
);
4667 /* When changing a call into a noreturn call, cfg cleanup
4668 is needed to fix up the noreturn call. */
4669 if (!was_noreturn
&& gimple_call_noreturn_p (stmt
))
4670 el_to_fixup
.safe_push (stmt
);
4675 stmt
= gsi_stmt (gsi
);
4676 if ((gimple_code (stmt
) == GIMPLE_COND
4677 && (gimple_cond_true_p (as_a
<gcond
*> (stmt
))
4678 || gimple_cond_false_p (as_a
<gcond
*> (stmt
))))
4679 || (gimple_code (stmt
) == GIMPLE_SWITCH
4680 && TREE_CODE (gimple_switch_index (
4681 as_a
<gswitch
*> (stmt
)))
4683 el_todo
|= TODO_cleanup_cfg
;
4685 /* If we removed EH side-effects from the statement, clean
4686 its EH information. */
4687 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
4689 bitmap_set_bit (need_eh_cleanup
,
4690 gimple_bb (stmt
)->index
);
4691 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4692 fprintf (dump_file
, " Removed EH side-effects.\n");
4694 /* Likewise for AB side-effects. */
4695 if (can_make_abnormal_goto
4696 && !stmt_can_make_abnormal_goto (stmt
))
4698 bitmap_set_bit (need_ab_cleanup
,
4699 gimple_bb (stmt
)->index
);
4700 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4701 fprintf (dump_file
, " Removed AB side-effects.\n");
4704 if (vdef
!= gimple_vdef (stmt
))
4705 VN_INFO (vdef
)->valnum
= vuse
;
4708 /* Make new values available - for fully redundant LHS we
4709 continue with the next stmt above and skip this. */
4711 FOR_EACH_SSA_DEF_OPERAND (defp
, stmt
, iter
, SSA_OP_DEF
)
4712 eliminate_push_avail (DEF_FROM_PTR (defp
));
4715 /* Replace destination PHI arguments. */
4718 FOR_EACH_EDGE (e
, ei
, b
->succs
)
4720 for (gphi_iterator gsi
= gsi_start_phis (e
->dest
);
4724 gphi
*phi
= gsi
.phi ();
4725 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
4726 tree arg
= USE_FROM_PTR (use_p
);
4727 if (TREE_CODE (arg
) != SSA_NAME
4728 || virtual_operand_p (arg
))
4730 tree sprime
= eliminate_avail (arg
);
4731 if (sprime
&& may_propagate_copy (arg
, sprime
))
4733 propagate_value (use_p
, sprime
);
4734 if (TREE_CODE (sprime
) == SSA_NAME
)
4735 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
4742 /* Make no longer available leaders no longer available. */
4745 eliminate_dom_walker::after_dom_children (basic_block
)
4748 while ((entry
= el_avail_stack
.pop ()) != NULL_TREE
)
4750 tree valnum
= VN_INFO (entry
)->valnum
;
4751 tree old
= el_avail
[SSA_NAME_VERSION (valnum
)];
4753 el_avail
[SSA_NAME_VERSION (valnum
)] = NULL_TREE
;
4755 el_avail
[SSA_NAME_VERSION (valnum
)] = entry
;
4759 /* Eliminate fully redundant computations. */
4762 eliminate (bool do_pre
)
4764 gimple_stmt_iterator gsi
;
4767 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
4768 need_ab_cleanup
= BITMAP_ALLOC (NULL
);
4770 el_to_remove
.create (0);
4771 el_to_fixup
.create (0);
4773 el_avail
.create (num_ssa_names
);
4774 el_avail_stack
.create (0);
4776 eliminate_dom_walker (CDI_DOMINATORS
,
4777 do_pre
).walk (cfun
->cfg
->x_entry_block_ptr
);
4779 el_avail
.release ();
4780 el_avail_stack
.release ();
4782 /* We cannot remove stmts during BB walk, especially not release SSA
4783 names there as this confuses the VN machinery. The stmts ending
4784 up in el_to_remove are either stores or simple copies.
4785 Remove stmts in reverse order to make debug stmt creation possible. */
4786 while (!el_to_remove
.is_empty ())
4788 stmt
= el_to_remove
.pop ();
4790 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4792 fprintf (dump_file
, "Removing dead stmt ");
4793 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4797 if (gimple_code (stmt
) == GIMPLE_PHI
)
4798 lhs
= gimple_phi_result (stmt
);
4800 lhs
= gimple_get_lhs (stmt
);
4803 && TREE_CODE (lhs
) == SSA_NAME
)
4804 bitmap_clear_bit (inserted_exprs
, SSA_NAME_VERSION (lhs
));
4806 gsi
= gsi_for_stmt (stmt
);
4807 if (gimple_code (stmt
) == GIMPLE_PHI
)
4808 remove_phi_node (&gsi
, true);
4811 basic_block bb
= gimple_bb (stmt
);
4812 unlink_stmt_vdef (stmt
);
4813 if (gsi_remove (&gsi
, true))
4814 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
4815 if (is_gimple_call (stmt
) && stmt_can_make_abnormal_goto (stmt
))
4816 bitmap_set_bit (need_ab_cleanup
, bb
->index
);
4817 release_defs (stmt
);
4820 /* Removing a stmt may expose a forwarder block. */
4821 el_todo
|= TODO_cleanup_cfg
;
4823 el_to_remove
.release ();
4825 /* Fixup stmts that became noreturn calls. This may require splitting
4826 blocks and thus isn't possible during the dominator walk. Do this
4827 in reverse order so we don't inadvertedly remove a stmt we want to
4828 fixup by visiting a dominating now noreturn call first. */
4829 while (!el_to_fixup
.is_empty ())
4831 stmt
= el_to_fixup
.pop ();
4833 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4835 fprintf (dump_file
, "Fixing up noreturn call ");
4836 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4839 if (fixup_noreturn_call (stmt
))
4840 el_todo
|= TODO_cleanup_cfg
;
4842 el_to_fixup
.release ();
4847 /* Perform CFG cleanups made necessary by elimination. */
4850 fini_eliminate (void)
4852 bool do_eh_cleanup
= !bitmap_empty_p (need_eh_cleanup
);
4853 bool do_ab_cleanup
= !bitmap_empty_p (need_ab_cleanup
);
4856 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
4859 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup
);
4861 BITMAP_FREE (need_eh_cleanup
);
4862 BITMAP_FREE (need_ab_cleanup
);
4864 if (do_eh_cleanup
|| do_ab_cleanup
)
4865 return TODO_cleanup_cfg
;
4869 /* Borrow a bit of tree-ssa-dce.c for the moment.
4870 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4871 this may be a bit faster, and we may want critical edges kept split. */
4873 /* If OP's defining statement has not already been determined to be necessary,
4874 mark that statement necessary. Return the stmt, if it is newly
4877 static inline gimple
*
4878 mark_operand_necessary (tree op
)
4884 if (TREE_CODE (op
) != SSA_NAME
)
4887 stmt
= SSA_NAME_DEF_STMT (op
);
4890 if (gimple_plf (stmt
, NECESSARY
)
4891 || gimple_nop_p (stmt
))
4894 gimple_set_plf (stmt
, NECESSARY
, true);
4898 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4899 to insert PHI nodes sometimes, and because value numbering of casts isn't
4900 perfect, we sometimes end up inserting dead code. This simple DCE-like
4901 pass removes any insertions we made that weren't actually used. */
4904 remove_dead_inserted_code (void)
4911 worklist
= BITMAP_ALLOC (NULL
);
4912 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4914 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4915 if (gimple_plf (t
, NECESSARY
))
4916 bitmap_set_bit (worklist
, i
);
4918 while (!bitmap_empty_p (worklist
))
4920 i
= bitmap_first_set_bit (worklist
);
4921 bitmap_clear_bit (worklist
, i
);
4922 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4924 /* PHI nodes are somewhat special in that each PHI alternative has
4925 data and control dependencies. All the statements feeding the
4926 PHI node's arguments are always necessary. */
4927 if (gimple_code (t
) == GIMPLE_PHI
)
4931 for (k
= 0; k
< gimple_phi_num_args (t
); k
++)
4933 tree arg
= PHI_ARG_DEF (t
, k
);
4934 if (TREE_CODE (arg
) == SSA_NAME
)
4936 gimple
*n
= mark_operand_necessary (arg
);
4938 bitmap_set_bit (worklist
, SSA_NAME_VERSION (arg
));
4944 /* Propagate through the operands. Examine all the USE, VUSE and
4945 VDEF operands in this statement. Mark all the statements
4946 which feed this statement's uses as necessary. */
4950 /* The operands of VDEF expressions are also needed as they
4951 represent potential definitions that may reach this
4952 statement (VDEF operands allow us to follow def-def
4955 FOR_EACH_SSA_TREE_OPERAND (use
, t
, iter
, SSA_OP_ALL_USES
)
4957 gimple
*n
= mark_operand_necessary (use
);
4959 bitmap_set_bit (worklist
, SSA_NAME_VERSION (use
));
4964 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4966 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4967 if (!gimple_plf (t
, NECESSARY
))
4969 gimple_stmt_iterator gsi
;
4971 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4973 fprintf (dump_file
, "Removing unnecessary insertion:");
4974 print_gimple_stmt (dump_file
, t
, 0, 0);
4977 gsi
= gsi_for_stmt (t
);
4978 if (gimple_code (t
) == GIMPLE_PHI
)
4979 remove_phi_node (&gsi
, true);
4982 gsi_remove (&gsi
, true);
4987 BITMAP_FREE (worklist
);
4991 /* Initialize data structures used by PRE. */
4998 next_expression_id
= 1;
4999 expressions
.create (0);
5000 expressions
.safe_push (NULL
);
5001 value_expressions
.create (get_max_value_id () + 1);
5002 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
5003 name_to_id
.create (0);
5005 inserted_exprs
= BITMAP_ALLOC (NULL
);
5007 connect_infinite_loops_to_exit ();
5008 memset (&pre_stats
, 0, sizeof (pre_stats
));
5010 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets
));
5012 calculate_dominance_info (CDI_DOMINATORS
);
5014 bitmap_obstack_initialize (&grand_bitmap_obstack
);
5015 phi_translate_table
= new hash_table
<expr_pred_trans_d
> (5110);
5016 expression_to_id
= new hash_table
<pre_expr_d
> (num_ssa_names
* 3);
5017 FOR_ALL_BB_FN (bb
, cfun
)
5019 EXP_GEN (bb
) = bitmap_set_new ();
5020 PHI_GEN (bb
) = bitmap_set_new ();
5021 TMP_GEN (bb
) = bitmap_set_new ();
5022 AVAIL_OUT (bb
) = bitmap_set_new ();
5027 /* Deallocate data structures used by PRE. */
5032 value_expressions
.release ();
5033 BITMAP_FREE (inserted_exprs
);
5034 bitmap_obstack_release (&grand_bitmap_obstack
);
5035 bitmap_set_pool
.release ();
5036 pre_expr_pool
.release ();
5037 delete phi_translate_table
;
5038 phi_translate_table
= NULL
;
5039 delete expression_to_id
;
5040 expression_to_id
= NULL
;
5041 name_to_id
.release ();
5043 free_aux_for_blocks ();
5048 const pass_data pass_data_pre
=
5050 GIMPLE_PASS
, /* type */
5052 OPTGROUP_NONE
, /* optinfo_flags */
5053 TV_TREE_PRE
, /* tv_id */
5054 /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before
5056 ( PROP_no_crit_edges
| PROP_cfg
| PROP_ssa
), /* properties_required */
5057 0, /* properties_provided */
5058 PROP_no_crit_edges
, /* properties_destroyed */
5059 TODO_rebuild_alias
, /* todo_flags_start */
5060 0, /* todo_flags_finish */
5063 class pass_pre
: public gimple_opt_pass
5066 pass_pre (gcc::context
*ctxt
)
5067 : gimple_opt_pass (pass_data_pre
, ctxt
)
5070 /* opt_pass methods: */
5071 virtual bool gate (function
*)
5072 { return flag_tree_pre
!= 0 || flag_code_hoisting
!= 0; }
5073 virtual unsigned int execute (function
*);
5075 }; // class pass_pre
5078 pass_pre::execute (function
*fun
)
5080 unsigned int todo
= 0;
5082 do_partial_partial
=
5083 flag_tree_partial_pre
&& optimize_function_for_speed_p (fun
);
5085 /* This has to happen before SCCVN runs because
5086 loop_optimizer_init may create new phis, etc. */
5087 loop_optimizer_init (LOOPS_NORMAL
);
5089 run_scc_vn (VN_WALK
);
5094 /* Collect and value number expressions computed in each basic block. */
5097 /* Insert can get quite slow on an incredibly large number of basic
5098 blocks due to some quadratic behavior. Until this behavior is
5099 fixed, don't run it when he have an incredibly large number of
5100 bb's. If we aren't going to run insert, there is no point in
5101 computing ANTIC, either, even though it's plenty fast. */
5102 if (n_basic_blocks_for_fn (fun
) < 4000)
5108 /* Make sure to remove fake edges before committing our inserts.
5109 This makes sure we don't end up with extra critical edges that
5110 we would need to split. */
5111 remove_fake_exit_edges ();
5112 gsi_commit_edge_inserts ();
5114 /* Eliminate folds statements which might (should not...) end up
5115 not keeping virtual operands up-to-date. */
5116 gcc_assert (!need_ssa_update_p (fun
));
5118 /* Remove all the redundant expressions. */
5119 todo
|= eliminate (true);
5121 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
5122 statistics_counter_event (fun
, "PA inserted", pre_stats
.pa_insert
);
5123 statistics_counter_event (fun
, "HOIST inserted", pre_stats
.hoist_insert
);
5124 statistics_counter_event (fun
, "New PHIs", pre_stats
.phis
);
5125 statistics_counter_event (fun
, "Eliminated", pre_stats
.eliminations
);
5127 clear_expression_ids ();
5128 remove_dead_inserted_code ();
5132 todo
|= fini_eliminate ();
5133 loop_optimizer_finalize ();
5135 /* Restore SSA info before tail-merging as that resets it as well. */
5136 scc_vn_restore_ssa_info ();
5138 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
5139 case we can merge the block with the remaining predecessor of the block.
5141 - call merge_blocks after each tail merge iteration
5142 - call merge_blocks after all tail merge iterations
5143 - mark TODO_cleanup_cfg when necessary
5144 - share the cfg cleanup with fini_pre. */
5145 todo
|= tail_merge_optimize (todo
);
5149 /* Tail merging invalidates the virtual SSA web, together with
5150 cfg-cleanup opportunities exposed by PRE this will wreck the
5151 SSA updating machinery. So make sure to run update-ssa
5152 manually, before eventually scheduling cfg-cleanup as part of
5154 update_ssa (TODO_update_ssa_only_virtuals
);
5162 make_pass_pre (gcc::context
*ctxt
)
5164 return new pass_pre (ctxt
);
5169 const pass_data pass_data_fre
=
5171 GIMPLE_PASS
, /* type */
5173 OPTGROUP_NONE
, /* optinfo_flags */
5174 TV_TREE_FRE
, /* tv_id */
5175 ( PROP_cfg
| PROP_ssa
), /* properties_required */
5176 0, /* properties_provided */
5177 0, /* properties_destroyed */
5178 0, /* todo_flags_start */
5179 0, /* todo_flags_finish */
5182 class pass_fre
: public gimple_opt_pass
5185 pass_fre (gcc::context
*ctxt
)
5186 : gimple_opt_pass (pass_data_fre
, ctxt
)
5189 /* opt_pass methods: */
5190 opt_pass
* clone () { return new pass_fre (m_ctxt
); }
5191 virtual bool gate (function
*) { return flag_tree_fre
!= 0; }
5192 virtual unsigned int execute (function
*);
5194 }; // class pass_fre
5197 pass_fre::execute (function
*fun
)
5199 unsigned int todo
= 0;
5201 run_scc_vn (VN_WALKREWRITE
);
5203 memset (&pre_stats
, 0, sizeof (pre_stats
));
5205 /* Remove all the redundant expressions. */
5206 todo
|= eliminate (false);
5208 todo
|= fini_eliminate ();
5210 scc_vn_restore_ssa_info ();
5213 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
5214 statistics_counter_event (fun
, "Eliminated", pre_stats
.eliminations
);
5222 make_pass_fre (gcc::context
*ctxt
)
5224 return new pass_fre (ctxt
);