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. This is used to avoid making a set consisting of the union
1317 of PA_IN and ANTIC_IN during insert. */
1319 static inline pre_expr
1320 find_leader_in_sets (unsigned int val
, bitmap_set_t set1
, bitmap_set_t set2
)
1324 result
= bitmap_find_leader (set1
, val
);
1325 if (!result
&& set2
)
1326 result
= bitmap_find_leader (set2
, val
);
1330 /* Get the tree type for our PRE expression e. */
1333 get_expr_type (const pre_expr e
)
1338 return TREE_TYPE (PRE_EXPR_NAME (e
));
1340 return TREE_TYPE (PRE_EXPR_CONSTANT (e
));
1342 return PRE_EXPR_REFERENCE (e
)->type
;
1344 return PRE_EXPR_NARY (e
)->type
;
1349 /* Get a representative SSA_NAME for a given expression.
1350 Since all of our sub-expressions are treated as values, we require
1351 them to be SSA_NAME's for simplicity.
1352 Prior versions of GVNPRE used to use "value handles" here, so that
1353 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1354 either case, the operands are really values (IE we do not expect
1355 them to be usable without finding leaders). */
1358 get_representative_for (const pre_expr e
)
1361 unsigned int value_id
= get_expr_value_id (e
);
1366 return VN_INFO (PRE_EXPR_NAME (e
))->valnum
;
1368 return PRE_EXPR_CONSTANT (e
);
1372 /* Go through all of the expressions representing this value
1373 and pick out an SSA_NAME. */
1376 bitmap exprs
= value_expressions
[value_id
];
1377 EXECUTE_IF_SET_IN_BITMAP (exprs
, 0, i
, bi
)
1379 pre_expr rep
= expression_for_id (i
);
1380 if (rep
->kind
== NAME
)
1381 return VN_INFO (PRE_EXPR_NAME (rep
))->valnum
;
1382 else if (rep
->kind
== CONSTANT
)
1383 return PRE_EXPR_CONSTANT (rep
);
1389 /* If we reached here we couldn't find an SSA_NAME. This can
1390 happen when we've discovered a value that has never appeared in
1391 the program as set to an SSA_NAME, as the result of phi translation.
1393 ??? We should be able to re-use this when we insert the statement
1395 name
= make_temp_ssa_name (get_expr_type (e
), gimple_build_nop (), "pretmp");
1396 VN_INFO_GET (name
)->value_id
= value_id
;
1397 VN_INFO (name
)->valnum
= name
;
1398 /* ??? For now mark this SSA name for release by SCCVN. */
1399 VN_INFO (name
)->needs_insertion
= true;
1400 add_to_value (value_id
, get_or_alloc_expr_for_name (name
));
1401 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1403 fprintf (dump_file
, "Created SSA_NAME representative ");
1404 print_generic_expr (dump_file
, name
, 0);
1405 fprintf (dump_file
, " for expression:");
1406 print_pre_expr (dump_file
, e
);
1407 fprintf (dump_file
, " (%04d)\n", value_id
);
1416 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1417 basic_block pred
, basic_block phiblock
);
1419 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1420 the phis in PRED. Return NULL if we can't find a leader for each part
1421 of the translated expression. */
1424 phi_translate_1 (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1425 basic_block pred
, basic_block phiblock
)
1432 bool changed
= false;
1433 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1434 vn_nary_op_t newnary
= XALLOCAVAR (struct vn_nary_op_s
,
1435 sizeof_vn_nary_op (nary
->length
));
1436 memcpy (newnary
, nary
, sizeof_vn_nary_op (nary
->length
));
1438 for (i
= 0; i
< newnary
->length
; i
++)
1440 if (TREE_CODE (newnary
->op
[i
]) != SSA_NAME
)
1444 pre_expr leader
, result
;
1445 unsigned int op_val_id
= VN_INFO (newnary
->op
[i
])->value_id
;
1446 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1447 result
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1448 if (result
&& result
!= leader
)
1449 newnary
->op
[i
] = get_representative_for (result
);
1453 changed
|= newnary
->op
[i
] != nary
->op
[i
];
1459 unsigned int new_val_id
;
1461 PRE_EXPR_NARY (expr
) = newnary
;
1462 constant
= fully_constant_expression (expr
);
1463 PRE_EXPR_NARY (expr
) = nary
;
1464 if (constant
!= expr
)
1466 /* For non-CONSTANTs we have to make sure we can eventually
1467 insert the expression. Which means we need to have a
1469 if (constant
->kind
!= CONSTANT
)
1471 /* Do not allow simplifications to non-constants over
1472 backedges as this will likely result in a loop PHI node
1473 to be inserted and increased register pressure.
1474 See PR77498 - this avoids doing predcoms work in
1475 a less efficient way. */
1476 if (find_edge (pred
, phiblock
)->flags
& EDGE_DFS_BACK
)
1480 unsigned value_id
= get_expr_value_id (constant
);
1481 constant
= find_leader_in_sets (value_id
, set1
, set2
);
1490 tree result
= vn_nary_op_lookup_pieces (newnary
->length
,
1495 if (result
&& is_gimple_min_invariant (result
))
1496 return get_or_alloc_expr_for_constant (result
);
1498 expr
= pre_expr_pool
.allocate ();
1503 PRE_EXPR_NARY (expr
) = nary
;
1504 new_val_id
= nary
->value_id
;
1505 get_or_alloc_expression_id (expr
);
1509 new_val_id
= get_next_value_id ();
1510 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
1511 nary
= vn_nary_op_insert_pieces (newnary
->length
,
1515 result
, new_val_id
);
1516 PRE_EXPR_NARY (expr
) = nary
;
1517 get_or_alloc_expression_id (expr
);
1519 add_to_value (new_val_id
, expr
);
1527 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1528 vec
<vn_reference_op_s
> operands
= ref
->operands
;
1529 tree vuse
= ref
->vuse
;
1530 tree newvuse
= vuse
;
1531 vec
<vn_reference_op_s
> newoperands
= vNULL
;
1532 bool changed
= false, same_valid
= true;
1534 vn_reference_op_t operand
;
1535 vn_reference_t newref
;
1537 for (i
= 0; operands
.iterate (i
, &operand
); i
++)
1542 tree type
= operand
->type
;
1543 vn_reference_op_s newop
= *operand
;
1544 op
[0] = operand
->op0
;
1545 op
[1] = operand
->op1
;
1546 op
[2] = operand
->op2
;
1547 for (n
= 0; n
< 3; ++n
)
1549 unsigned int op_val_id
;
1552 if (TREE_CODE (op
[n
]) != SSA_NAME
)
1554 /* We can't possibly insert these. */
1556 && !is_gimple_min_invariant (op
[n
]))
1560 op_val_id
= VN_INFO (op
[n
])->value_id
;
1561 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1562 opresult
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1563 if (opresult
&& opresult
!= leader
)
1565 tree name
= get_representative_for (opresult
);
1566 changed
|= name
!= op
[n
];
1574 newoperands
.release ();
1579 if (!newoperands
.exists ())
1580 newoperands
= operands
.copy ();
1581 /* We may have changed from an SSA_NAME to a constant */
1582 if (newop
.opcode
== SSA_NAME
&& TREE_CODE (op
[0]) != SSA_NAME
)
1583 newop
.opcode
= TREE_CODE (op
[0]);
1588 newoperands
[i
] = newop
;
1590 gcc_checking_assert (i
== operands
.length ());
1594 newvuse
= translate_vuse_through_block (newoperands
.exists ()
1595 ? newoperands
: operands
,
1596 ref
->set
, ref
->type
,
1597 vuse
, phiblock
, pred
,
1599 if (newvuse
== NULL_TREE
)
1601 newoperands
.release ();
1606 if (changed
|| newvuse
!= vuse
)
1608 unsigned int new_val_id
;
1611 tree result
= vn_reference_lookup_pieces (newvuse
, ref
->set
,
1613 newoperands
.exists ()
1614 ? newoperands
: operands
,
1617 newoperands
.release ();
1619 /* We can always insert constants, so if we have a partial
1620 redundant constant load of another type try to translate it
1621 to a constant of appropriate type. */
1622 if (result
&& is_gimple_min_invariant (result
))
1625 if (!useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1627 tem
= fold_unary (VIEW_CONVERT_EXPR
, ref
->type
, result
);
1628 if (tem
&& !is_gimple_min_invariant (tem
))
1632 return get_or_alloc_expr_for_constant (tem
);
1635 /* If we'd have to convert things we would need to validate
1636 if we can insert the translated expression. So fail
1637 here for now - we cannot insert an alias with a different
1638 type in the VN tables either, as that would assert. */
1640 && !useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1642 else if (!result
&& newref
1643 && !useless_type_conversion_p (ref
->type
, newref
->type
))
1645 newoperands
.release ();
1649 expr
= pre_expr_pool
.allocate ();
1650 expr
->kind
= REFERENCE
;
1655 PRE_EXPR_REFERENCE (expr
) = newref
;
1656 constant
= fully_constant_expression (expr
);
1657 if (constant
!= expr
)
1660 new_val_id
= newref
->value_id
;
1661 get_or_alloc_expression_id (expr
);
1665 if (changed
|| !same_valid
)
1667 new_val_id
= get_next_value_id ();
1668 value_expressions
.safe_grow_cleared
1669 (get_max_value_id () + 1);
1672 new_val_id
= ref
->value_id
;
1673 if (!newoperands
.exists ())
1674 newoperands
= operands
.copy ();
1675 newref
= vn_reference_insert_pieces (newvuse
, ref
->set
,
1678 result
, new_val_id
);
1679 newoperands
= vNULL
;
1680 PRE_EXPR_REFERENCE (expr
) = newref
;
1681 constant
= fully_constant_expression (expr
);
1682 if (constant
!= expr
)
1684 get_or_alloc_expression_id (expr
);
1686 add_to_value (new_val_id
, expr
);
1688 newoperands
.release ();
1695 tree name
= PRE_EXPR_NAME (expr
);
1696 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1697 /* If the SSA name is defined by a PHI node in this block,
1699 if (gimple_code (def_stmt
) == GIMPLE_PHI
1700 && gimple_bb (def_stmt
) == phiblock
)
1702 edge e
= find_edge (pred
, gimple_bb (def_stmt
));
1703 tree def
= PHI_ARG_DEF (def_stmt
, e
->dest_idx
);
1705 /* Handle constant. */
1706 if (is_gimple_min_invariant (def
))
1707 return get_or_alloc_expr_for_constant (def
);
1709 return get_or_alloc_expr_for_name (def
);
1711 /* Otherwise return it unchanged - it will get removed if its
1712 value is not available in PREDs AVAIL_OUT set of expressions
1713 by the subtraction of TMP_GEN. */
1722 /* Wrapper around phi_translate_1 providing caching functionality. */
1725 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1726 basic_block pred
, basic_block phiblock
)
1728 expr_pred_trans_t slot
= NULL
;
1734 /* Constants contain no values that need translation. */
1735 if (expr
->kind
== CONSTANT
)
1738 if (value_id_constant_p (get_expr_value_id (expr
)))
1741 /* Don't add translations of NAMEs as those are cheap to translate. */
1742 if (expr
->kind
!= NAME
)
1744 if (phi_trans_add (&slot
, expr
, pred
))
1746 /* Store NULL for the value we want to return in the case of
1752 phitrans
= phi_translate_1 (expr
, set1
, set2
, pred
, phiblock
);
1759 /* Remove failed translations again, they cause insert
1760 iteration to not pick up new opportunities reliably. */
1761 phi_translate_table
->remove_elt_with_hash (slot
, slot
->hashcode
);
1768 /* For each expression in SET, translate the values through phi nodes
1769 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1770 expressions in DEST. */
1773 phi_translate_set (bitmap_set_t dest
, bitmap_set_t set
, basic_block pred
,
1774 basic_block phiblock
)
1776 vec
<pre_expr
> exprs
;
1780 if (gimple_seq_empty_p (phi_nodes (phiblock
)))
1782 bitmap_set_copy (dest
, set
);
1786 exprs
= sorted_array_from_bitmap_set (set
);
1787 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
1789 pre_expr translated
;
1790 translated
= phi_translate (expr
, set
, NULL
, pred
, phiblock
);
1794 /* We might end up with multiple expressions from SET being
1795 translated to the same value. In this case we do not want
1796 to retain the NARY or REFERENCE expression but prefer a NAME
1797 which would be the leader. */
1798 if (translated
->kind
== NAME
)
1799 bitmap_value_replace_in_set (dest
, translated
);
1801 bitmap_value_insert_into_set (dest
, translated
);
1806 /* Find the leader for a value (i.e., the name representing that
1807 value) in a given set, and return it. Return NULL if no leader
1811 bitmap_find_leader (bitmap_set_t set
, unsigned int val
)
1813 if (value_id_constant_p (val
))
1817 bitmap exprset
= value_expressions
[val
];
1819 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1821 pre_expr expr
= expression_for_id (i
);
1822 if (expr
->kind
== CONSTANT
)
1826 if (bitmap_set_contains_value (set
, val
))
1828 /* Rather than walk the entire bitmap of expressions, and see
1829 whether any of them has the value we are looking for, we look
1830 at the reverse mapping, which tells us the set of expressions
1831 that have a given value (IE value->expressions with that
1832 value) and see if any of those expressions are in our set.
1833 The number of expressions per value is usually significantly
1834 less than the number of expressions in the set. In fact, for
1835 large testcases, doing it this way is roughly 5-10x faster
1836 than walking the bitmap.
1837 If this is somehow a significant lose for some cases, we can
1838 choose which set to walk based on which set is smaller. */
1841 bitmap exprset
= value_expressions
[val
];
1843 EXECUTE_IF_AND_IN_BITMAP (exprset
, &set
->expressions
, 0, i
, bi
)
1844 return expression_for_id (i
);
1849 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1850 BLOCK by seeing if it is not killed in the block. Note that we are
1851 only determining whether there is a store that kills it. Because
1852 of the order in which clean iterates over values, we are guaranteed
1853 that altered operands will have caused us to be eliminated from the
1854 ANTIC_IN set already. */
1857 value_dies_in_block_x (pre_expr expr
, basic_block block
)
1859 tree vuse
= PRE_EXPR_REFERENCE (expr
)->vuse
;
1860 vn_reference_t refx
= PRE_EXPR_REFERENCE (expr
);
1862 gimple_stmt_iterator gsi
;
1863 unsigned id
= get_expression_id (expr
);
1870 /* Lookup a previously calculated result. */
1871 if (EXPR_DIES (block
)
1872 && bitmap_bit_p (EXPR_DIES (block
), id
* 2))
1873 return bitmap_bit_p (EXPR_DIES (block
), id
* 2 + 1);
1875 /* A memory expression {e, VUSE} dies in the block if there is a
1876 statement that may clobber e. If, starting statement walk from the
1877 top of the basic block, a statement uses VUSE there can be no kill
1878 inbetween that use and the original statement that loaded {e, VUSE},
1879 so we can stop walking. */
1880 ref
.base
= NULL_TREE
;
1881 for (gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1883 tree def_vuse
, def_vdef
;
1884 def
= gsi_stmt (gsi
);
1885 def_vuse
= gimple_vuse (def
);
1886 def_vdef
= gimple_vdef (def
);
1888 /* Not a memory statement. */
1892 /* Not a may-def. */
1895 /* A load with the same VUSE, we're done. */
1896 if (def_vuse
== vuse
)
1902 /* Init ref only if we really need it. */
1903 if (ref
.base
== NULL_TREE
1904 && !ao_ref_init_from_vn_reference (&ref
, refx
->set
, refx
->type
,
1910 /* If the statement may clobber expr, it dies. */
1911 if (stmt_may_clobber_ref_p_1 (def
, &ref
))
1918 /* Remember the result. */
1919 if (!EXPR_DIES (block
))
1920 EXPR_DIES (block
) = BITMAP_ALLOC (&grand_bitmap_obstack
);
1921 bitmap_set_bit (EXPR_DIES (block
), id
* 2);
1923 bitmap_set_bit (EXPR_DIES (block
), id
* 2 + 1);
1929 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1930 contains its value-id. */
1933 op_valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, tree op
)
1935 if (op
&& TREE_CODE (op
) == SSA_NAME
)
1937 unsigned int value_id
= VN_INFO (op
)->value_id
;
1938 if (!(bitmap_set_contains_value (set1
, value_id
)
1939 || (set2
&& bitmap_set_contains_value (set2
, value_id
))))
1945 /* Determine if the expression EXPR is valid in SET1 U SET2.
1946 ONLY SET2 CAN BE NULL.
1947 This means that we have a leader for each part of the expression
1948 (if it consists of values), or the expression is an SSA_NAME.
1949 For loads/calls, we also see if the vuse is killed in this block. */
1952 valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, pre_expr expr
)
1957 /* By construction all NAMEs are available. Non-available
1958 NAMEs are removed by subtracting TMP_GEN from the sets. */
1963 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1964 for (i
= 0; i
< nary
->length
; i
++)
1965 if (!op_valid_in_sets (set1
, set2
, nary
->op
[i
]))
1972 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1973 vn_reference_op_t vro
;
1976 FOR_EACH_VEC_ELT (ref
->operands
, i
, vro
)
1978 if (!op_valid_in_sets (set1
, set2
, vro
->op0
)
1979 || !op_valid_in_sets (set1
, set2
, vro
->op1
)
1980 || !op_valid_in_sets (set1
, set2
, vro
->op2
))
1990 /* Clean the set of expressions that are no longer valid in SET1 or
1991 SET2. This means expressions that are made up of values we have no
1992 leaders for in SET1 or SET2. This version is used for partial
1993 anticipation, which means it is not valid in either ANTIC_IN or
1997 dependent_clean (bitmap_set_t set1
, bitmap_set_t set2
)
1999 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set1
);
2003 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2005 if (!valid_in_sets (set1
, set2
, expr
))
2006 bitmap_remove_from_set (set1
, expr
);
2011 /* Clean the set of expressions that are no longer valid in SET. This
2012 means expressions that are made up of values we have no leaders for
2016 clean (bitmap_set_t set
)
2018 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set
);
2022 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2024 if (!valid_in_sets (set
, NULL
, expr
))
2025 bitmap_remove_from_set (set
, expr
);
2030 /* Clean the set of expressions that are no longer valid in SET because
2031 they are clobbered in BLOCK or because they trap and may not be executed. */
2034 prune_clobbered_mems (bitmap_set_t set
, basic_block block
)
2038 pre_expr to_remove
= NULL
;
2040 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
2042 /* Remove queued expr. */
2045 bitmap_remove_from_set (set
, to_remove
);
2049 pre_expr expr
= expression_for_id (i
);
2050 if (expr
->kind
== REFERENCE
)
2052 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2055 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ref
->vuse
);
2056 if (!gimple_nop_p (def_stmt
)
2057 && ((gimple_bb (def_stmt
) != block
2058 && !dominated_by_p (CDI_DOMINATORS
,
2059 block
, gimple_bb (def_stmt
)))
2060 || (gimple_bb (def_stmt
) == block
2061 && value_dies_in_block_x (expr
, block
))))
2065 else if (expr
->kind
== NARY
)
2067 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2068 /* If the NARY may trap make sure the block does not contain
2069 a possible exit point.
2070 ??? This is overly conservative if we translate AVAIL_OUT
2071 as the available expression might be after the exit point. */
2072 if (BB_MAY_NOTRETURN (block
)
2073 && vn_nary_may_trap (nary
))
2078 /* Remove queued expr. */
2080 bitmap_remove_from_set (set
, to_remove
);
2083 static sbitmap has_abnormal_preds
;
2085 /* Compute the ANTIC set for BLOCK.
2087 If succs(BLOCK) > 1 then
2088 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2089 else if succs(BLOCK) == 1 then
2090 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2092 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2096 compute_antic_aux (basic_block block
, bool block_has_abnormal_pred_edge
)
2098 bool changed
= false;
2099 bitmap_set_t S
, old
, ANTIC_OUT
;
2104 bool was_visited
= BB_VISITED (block
);
2106 old
= ANTIC_OUT
= S
= NULL
;
2107 BB_VISITED (block
) = 1;
2109 /* If any edges from predecessors are abnormal, antic_in is empty,
2111 if (block_has_abnormal_pred_edge
)
2112 goto maybe_dump_sets
;
2114 old
= ANTIC_IN (block
);
2115 ANTIC_OUT
= bitmap_set_new ();
2117 /* If the block has no successors, ANTIC_OUT is empty. */
2118 if (EDGE_COUNT (block
->succs
) == 0)
2120 /* If we have one successor, we could have some phi nodes to
2121 translate through. */
2122 else if (single_succ_p (block
))
2124 basic_block succ_bb
= single_succ (block
);
2125 gcc_assert (BB_VISITED (succ_bb
));
2126 phi_translate_set (ANTIC_OUT
, ANTIC_IN (succ_bb
), block
, succ_bb
);
2128 /* If we have multiple successors, we take the intersection of all of
2129 them. Note that in the case of loop exit phi nodes, we may have
2130 phis to translate through. */
2134 basic_block bprime
, first
= NULL
;
2136 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2137 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2140 && BB_VISITED (e
->dest
))
2142 else if (BB_VISITED (e
->dest
))
2143 worklist
.quick_push (e
->dest
);
2146 /* Unvisited successors get their ANTIC_IN replaced by the
2147 maximal set to arrive at a maximum ANTIC_IN solution.
2148 We can ignore them in the intersection operation and thus
2149 need not explicitely represent that maximum solution. */
2150 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2151 fprintf (dump_file
, "ANTIC_IN is MAX on %d->%d\n",
2152 e
->src
->index
, e
->dest
->index
);
2156 /* Of multiple successors we have to have visited one already
2157 which is guaranteed by iteration order. */
2158 gcc_assert (first
!= NULL
);
2160 phi_translate_set (ANTIC_OUT
, ANTIC_IN (first
), block
, first
);
2162 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2164 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2166 bitmap_set_t tmp
= bitmap_set_new ();
2167 phi_translate_set (tmp
, ANTIC_IN (bprime
), block
, bprime
);
2168 bitmap_set_and (ANTIC_OUT
, tmp
);
2169 bitmap_set_free (tmp
);
2172 bitmap_set_and (ANTIC_OUT
, ANTIC_IN (bprime
));
2176 /* Prune expressions that are clobbered in block and thus become
2177 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2178 prune_clobbered_mems (ANTIC_OUT
, block
);
2180 /* Generate ANTIC_OUT - TMP_GEN. */
2181 S
= bitmap_set_subtract (ANTIC_OUT
, TMP_GEN (block
));
2183 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2184 ANTIC_IN (block
) = bitmap_set_subtract (EXP_GEN (block
),
2187 /* Then union in the ANTIC_OUT - TMP_GEN values,
2188 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2189 FOR_EACH_EXPR_ID_IN_SET (S
, bii
, bi
)
2190 bitmap_value_insert_into_set (ANTIC_IN (block
),
2191 expression_for_id (bii
));
2193 clean (ANTIC_IN (block
));
2195 if (!was_visited
|| !bitmap_set_equal (old
, ANTIC_IN (block
)))
2199 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2202 print_bitmap_set (dump_file
, ANTIC_OUT
, "ANTIC_OUT", block
->index
);
2205 fprintf (dump_file
, "[changed] ");
2206 print_bitmap_set (dump_file
, ANTIC_IN (block
), "ANTIC_IN",
2210 print_bitmap_set (dump_file
, S
, "S", block
->index
);
2213 bitmap_set_free (old
);
2215 bitmap_set_free (S
);
2217 bitmap_set_free (ANTIC_OUT
);
2221 /* Compute PARTIAL_ANTIC for BLOCK.
2223 If succs(BLOCK) > 1 then
2224 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2225 in ANTIC_OUT for all succ(BLOCK)
2226 else if succs(BLOCK) == 1 then
2227 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2229 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2234 compute_partial_antic_aux (basic_block block
,
2235 bool block_has_abnormal_pred_edge
)
2237 bitmap_set_t old_PA_IN
;
2238 bitmap_set_t PA_OUT
;
2241 unsigned long max_pa
= PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH
);
2243 old_PA_IN
= PA_OUT
= NULL
;
2245 /* If any edges from predecessors are abnormal, antic_in is empty,
2247 if (block_has_abnormal_pred_edge
)
2248 goto maybe_dump_sets
;
2250 /* If there are too many partially anticipatable values in the
2251 block, phi_translate_set can take an exponential time: stop
2252 before the translation starts. */
2254 && single_succ_p (block
)
2255 && bitmap_count_bits (&PA_IN (single_succ (block
))->values
) > max_pa
)
2256 goto maybe_dump_sets
;
2258 old_PA_IN
= PA_IN (block
);
2259 PA_OUT
= bitmap_set_new ();
2261 /* If the block has no successors, ANTIC_OUT is empty. */
2262 if (EDGE_COUNT (block
->succs
) == 0)
2264 /* If we have one successor, we could have some phi nodes to
2265 translate through. Note that we can't phi translate across DFS
2266 back edges in partial antic, because it uses a union operation on
2267 the successors. For recurrences like IV's, we will end up
2268 generating a new value in the set on each go around (i + 3 (VH.1)
2269 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2270 else if (single_succ_p (block
))
2272 basic_block succ
= single_succ (block
);
2273 if (!(single_succ_edge (block
)->flags
& EDGE_DFS_BACK
))
2274 phi_translate_set (PA_OUT
, PA_IN (succ
), block
, succ
);
2276 /* If we have multiple successors, we take the union of all of
2283 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2284 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2286 if (e
->flags
& EDGE_DFS_BACK
)
2288 worklist
.quick_push (e
->dest
);
2290 if (worklist
.length () > 0)
2292 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2297 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime
), i
, bi
)
2298 bitmap_value_insert_into_set (PA_OUT
,
2299 expression_for_id (i
));
2300 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2302 bitmap_set_t pa_in
= bitmap_set_new ();
2303 phi_translate_set (pa_in
, PA_IN (bprime
), block
, bprime
);
2304 FOR_EACH_EXPR_ID_IN_SET (pa_in
, i
, bi
)
2305 bitmap_value_insert_into_set (PA_OUT
,
2306 expression_for_id (i
));
2307 bitmap_set_free (pa_in
);
2310 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime
), i
, bi
)
2311 bitmap_value_insert_into_set (PA_OUT
,
2312 expression_for_id (i
));
2317 /* Prune expressions that are clobbered in block and thus become
2318 invalid if translated from PA_OUT to PA_IN. */
2319 prune_clobbered_mems (PA_OUT
, block
);
2321 /* PA_IN starts with PA_OUT - TMP_GEN.
2322 Then we subtract things from ANTIC_IN. */
2323 PA_IN (block
) = bitmap_set_subtract (PA_OUT
, TMP_GEN (block
));
2325 /* For partial antic, we want to put back in the phi results, since
2326 we will properly avoid making them partially antic over backedges. */
2327 bitmap_ior_into (&PA_IN (block
)->values
, &PHI_GEN (block
)->values
);
2328 bitmap_ior_into (&PA_IN (block
)->expressions
, &PHI_GEN (block
)->expressions
);
2330 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2331 bitmap_set_subtract_values (PA_IN (block
), ANTIC_IN (block
));
2333 dependent_clean (PA_IN (block
), ANTIC_IN (block
));
2336 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2339 print_bitmap_set (dump_file
, PA_OUT
, "PA_OUT", block
->index
);
2341 print_bitmap_set (dump_file
, PA_IN (block
), "PA_IN", block
->index
);
2344 bitmap_set_free (old_PA_IN
);
2346 bitmap_set_free (PA_OUT
);
2349 /* Compute ANTIC and partial ANTIC sets. */
2352 compute_antic (void)
2354 bool changed
= true;
2355 int num_iterations
= 0;
2361 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2362 We pre-build the map of blocks with incoming abnormal edges here. */
2363 has_abnormal_preds
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
2364 bitmap_clear (has_abnormal_preds
);
2366 FOR_ALL_BB_FN (block
, cfun
)
2368 BB_VISITED (block
) = 0;
2370 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2371 if (e
->flags
& EDGE_ABNORMAL
)
2373 bitmap_set_bit (has_abnormal_preds
, block
->index
);
2375 /* We also anticipate nothing. */
2376 BB_VISITED (block
) = 1;
2380 /* While we are here, give empty ANTIC_IN sets to each block. */
2381 ANTIC_IN (block
) = bitmap_set_new ();
2382 if (do_partial_partial
)
2383 PA_IN (block
) = bitmap_set_new ();
2386 /* At the exit block we anticipate nothing. */
2387 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun
)) = 1;
2389 /* For ANTIC computation we need a postorder that also guarantees that
2390 a block with a single successor is visited after its successor.
2391 RPO on the inverted CFG has this property. */
2392 int *postorder
= XNEWVEC (int, n_basic_blocks_for_fn (cfun
));
2393 int postorder_num
= inverted_post_order_compute (postorder
);
2395 auto_sbitmap
worklist (last_basic_block_for_fn (cfun
) + 1);
2396 bitmap_ones (worklist
);
2399 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2400 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2401 /* ??? We need to clear our PHI translation cache here as the
2402 ANTIC sets shrink and we restrict valid translations to
2403 those having operands with leaders in ANTIC. Same below
2404 for PA ANTIC computation. */
2407 for (i
= postorder_num
- 1; i
>= 0; i
--)
2409 if (bitmap_bit_p (worklist
, postorder
[i
]))
2411 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2412 bitmap_clear_bit (worklist
, block
->index
);
2413 if (compute_antic_aux (block
,
2414 bitmap_bit_p (has_abnormal_preds
,
2417 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2418 bitmap_set_bit (worklist
, e
->src
->index
);
2423 /* Theoretically possible, but *highly* unlikely. */
2424 gcc_checking_assert (num_iterations
< 500);
2427 statistics_histogram_event (cfun
, "compute_antic iterations",
2430 if (do_partial_partial
)
2432 /* For partial antic we ignore backedges and thus we do not need
2433 to perform any iteration when we process blocks in postorder. */
2434 postorder_num
= pre_and_rev_post_order_compute (NULL
, postorder
, false);
2435 for (i
= postorder_num
- 1 ; i
>= 0; i
--)
2437 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2438 compute_partial_antic_aux (block
,
2439 bitmap_bit_p (has_abnormal_preds
,
2444 sbitmap_free (has_abnormal_preds
);
2449 /* Inserted expressions are placed onto this worklist, which is used
2450 for performing quick dead code elimination of insertions we made
2451 that didn't turn out to be necessary. */
2452 static bitmap inserted_exprs
;
2454 /* The actual worker for create_component_ref_by_pieces. */
2457 create_component_ref_by_pieces_1 (basic_block block
, vn_reference_t ref
,
2458 unsigned int *operand
, gimple_seq
*stmts
)
2460 vn_reference_op_t currop
= &ref
->operands
[*operand
];
2463 switch (currop
->opcode
)
2470 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2474 tree offset
= currop
->op0
;
2475 if (TREE_CODE (baseop
) == ADDR_EXPR
2476 && handled_component_p (TREE_OPERAND (baseop
, 0)))
2480 base
= get_addr_base_and_unit_offset (TREE_OPERAND (baseop
, 0),
2483 offset
= int_const_binop (PLUS_EXPR
, offset
,
2484 build_int_cst (TREE_TYPE (offset
),
2486 baseop
= build_fold_addr_expr (base
);
2488 genop
= build2 (MEM_REF
, currop
->type
, baseop
, offset
);
2489 MR_DEPENDENCE_CLIQUE (genop
) = currop
->clique
;
2490 MR_DEPENDENCE_BASE (genop
) = currop
->base
;
2491 REF_REVERSE_STORAGE_ORDER (genop
) = currop
->reverse
;
2495 case TARGET_MEM_REF
:
2497 tree genop0
= NULL_TREE
, genop1
= NULL_TREE
;
2498 vn_reference_op_t nextop
= &ref
->operands
[++*operand
];
2499 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2505 genop0
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2511 genop1
= find_or_generate_expression (block
, nextop
->op0
, stmts
);
2515 genop
= build5 (TARGET_MEM_REF
, currop
->type
,
2516 baseop
, currop
->op2
, genop0
, currop
->op1
, genop1
);
2518 MR_DEPENDENCE_CLIQUE (genop
) = currop
->clique
;
2519 MR_DEPENDENCE_BASE (genop
) = currop
->base
;
2526 gcc_assert (is_gimple_min_invariant (currop
->op0
));
2532 case VIEW_CONVERT_EXPR
:
2534 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2538 return fold_build1 (currop
->opcode
, currop
->type
, genop0
);
2541 case WITH_SIZE_EXPR
:
2543 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2547 tree genop1
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2550 return fold_build2 (currop
->opcode
, currop
->type
, genop0
, genop1
);
2555 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2559 tree op1
= currop
->op0
;
2560 tree op2
= currop
->op1
;
2561 tree t
= build3 (BIT_FIELD_REF
, currop
->type
, genop0
, op1
, op2
);
2562 REF_REVERSE_STORAGE_ORDER (t
) = currop
->reverse
;
2566 /* For array ref vn_reference_op's, operand 1 of the array ref
2567 is op0 of the reference op and operand 3 of the array ref is
2569 case ARRAY_RANGE_REF
:
2573 tree genop1
= currop
->op0
;
2574 tree genop2
= currop
->op1
;
2575 tree genop3
= currop
->op2
;
2576 genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2580 genop1
= find_or_generate_expression (block
, genop1
, stmts
);
2585 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (genop0
));
2586 /* Drop zero minimum index if redundant. */
2587 if (integer_zerop (genop2
)
2589 || integer_zerop (TYPE_MIN_VALUE (domain_type
))))
2593 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2600 tree elmt_type
= TREE_TYPE (TREE_TYPE (genop0
));
2601 /* We can't always put a size in units of the element alignment
2602 here as the element alignment may be not visible. See
2603 PR43783. Simply drop the element size for constant
2605 if (TREE_CODE (genop3
) == INTEGER_CST
2606 && TREE_CODE (TYPE_SIZE_UNIT (elmt_type
)) == INTEGER_CST
2607 && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type
)),
2608 (wi::to_offset (genop3
)
2609 * vn_ref_op_align_unit (currop
))))
2613 genop3
= find_or_generate_expression (block
, genop3
, stmts
);
2618 return build4 (currop
->opcode
, currop
->type
, genop0
, genop1
,
2625 tree genop2
= currop
->op1
;
2626 op0
= create_component_ref_by_pieces_1 (block
, ref
, operand
, stmts
);
2629 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2633 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2637 return fold_build3 (COMPONENT_REF
, TREE_TYPE (op1
), op0
, op1
, genop2
);
2642 genop
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2663 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2664 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2665 trying to rename aggregates into ssa form directly, which is a no no.
2667 Thus, this routine doesn't create temporaries, it just builds a
2668 single access expression for the array, calling
2669 find_or_generate_expression to build the innermost pieces.
2671 This function is a subroutine of create_expression_by_pieces, and
2672 should not be called on it's own unless you really know what you
2676 create_component_ref_by_pieces (basic_block block
, vn_reference_t ref
,
2679 unsigned int op
= 0;
2680 return create_component_ref_by_pieces_1 (block
, ref
, &op
, stmts
);
2683 /* Find a simple leader for an expression, or generate one using
2684 create_expression_by_pieces from a NARY expression for the value.
2685 BLOCK is the basic_block we are looking for leaders in.
2686 OP is the tree expression to find a leader for or generate.
2687 Returns the leader or NULL_TREE on failure. */
2690 find_or_generate_expression (basic_block block
, tree op
, gimple_seq
*stmts
)
2692 pre_expr expr
= get_or_alloc_expr_for (op
);
2693 unsigned int lookfor
= get_expr_value_id (expr
);
2694 pre_expr leader
= bitmap_find_leader (AVAIL_OUT (block
), lookfor
);
2697 if (leader
->kind
== NAME
)
2698 return PRE_EXPR_NAME (leader
);
2699 else if (leader
->kind
== CONSTANT
)
2700 return PRE_EXPR_CONSTANT (leader
);
2706 /* It must be a complex expression, so generate it recursively. Note
2707 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2708 where the insert algorithm fails to insert a required expression. */
2709 bitmap exprset
= value_expressions
[lookfor
];
2712 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
2714 pre_expr temp
= expression_for_id (i
);
2715 /* We cannot insert random REFERENCE expressions at arbitrary
2716 places. We can insert NARYs which eventually re-materializes
2717 its operand values. */
2718 if (temp
->kind
== NARY
)
2719 return create_expression_by_pieces (block
, temp
, stmts
,
2720 get_expr_type (expr
));
2727 #define NECESSARY GF_PLF_1
2729 /* Create an expression in pieces, so that we can handle very complex
2730 expressions that may be ANTIC, but not necessary GIMPLE.
2731 BLOCK is the basic block the expression will be inserted into,
2732 EXPR is the expression to insert (in value form)
2733 STMTS is a statement list to append the necessary insertions into.
2735 This function will die if we hit some value that shouldn't be
2736 ANTIC but is (IE there is no leader for it, or its components).
2737 The function returns NULL_TREE in case a different antic expression
2738 has to be inserted first.
2739 This function may also generate expressions that are themselves
2740 partially or fully redundant. Those that are will be either made
2741 fully redundant during the next iteration of insert (for partially
2742 redundant ones), or eliminated by eliminate (for fully redundant
2746 create_expression_by_pieces (basic_block block
, pre_expr expr
,
2747 gimple_seq
*stmts
, tree type
)
2751 gimple_seq forced_stmts
= NULL
;
2752 unsigned int value_id
;
2753 gimple_stmt_iterator gsi
;
2754 tree exprtype
= type
? type
: get_expr_type (expr
);
2760 /* We may hit the NAME/CONSTANT case if we have to convert types
2761 that value numbering saw through. */
2763 folded
= PRE_EXPR_NAME (expr
);
2764 if (useless_type_conversion_p (exprtype
, TREE_TYPE (folded
)))
2769 folded
= PRE_EXPR_CONSTANT (expr
);
2770 tree tem
= fold_convert (exprtype
, folded
);
2771 if (is_gimple_min_invariant (tem
))
2776 if (PRE_EXPR_REFERENCE (expr
)->operands
[0].opcode
== CALL_EXPR
)
2778 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2779 unsigned int operand
= 1;
2780 vn_reference_op_t currop
= &ref
->operands
[0];
2781 tree sc
= NULL_TREE
;
2783 if (TREE_CODE (currop
->op0
) == FUNCTION_DECL
)
2786 fn
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2791 sc
= find_or_generate_expression (block
, currop
->op1
, stmts
);
2795 auto_vec
<tree
> args (ref
->operands
.length () - 1);
2796 while (operand
< ref
->operands
.length ())
2798 tree arg
= create_component_ref_by_pieces_1 (block
, ref
,
2802 args
.quick_push (arg
);
2805 = gimple_build_call_vec ((TREE_CODE (fn
) == FUNCTION_DECL
2806 ? build_fold_addr_expr (fn
) : fn
), args
);
2807 gimple_call_set_with_bounds (call
, currop
->with_bounds
);
2809 gimple_call_set_chain (call
, sc
);
2810 tree forcedname
= make_ssa_name (currop
->type
);
2811 gimple_call_set_lhs (call
, forcedname
);
2812 gimple_set_vuse (call
, BB_LIVE_VOP_ON_EXIT (block
));
2813 gimple_seq_add_stmt_without_update (&forced_stmts
, call
);
2814 folded
= forcedname
;
2818 folded
= create_component_ref_by_pieces (block
,
2819 PRE_EXPR_REFERENCE (expr
),
2823 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2824 newstmt
= gimple_build_assign (name
, folded
);
2825 gimple_seq_add_stmt_without_update (&forced_stmts
, newstmt
);
2826 gimple_set_vuse (newstmt
, BB_LIVE_VOP_ON_EXIT (block
));
2832 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2833 tree
*genop
= XALLOCAVEC (tree
, nary
->length
);
2835 for (i
= 0; i
< nary
->length
; ++i
)
2837 genop
[i
] = find_or_generate_expression (block
, nary
->op
[i
], stmts
);
2840 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2841 may have conversions stripped. */
2842 if (nary
->opcode
== POINTER_PLUS_EXPR
)
2845 genop
[i
] = gimple_convert (&forced_stmts
,
2846 nary
->type
, genop
[i
]);
2848 genop
[i
] = gimple_convert (&forced_stmts
,
2849 sizetype
, genop
[i
]);
2852 genop
[i
] = gimple_convert (&forced_stmts
,
2853 TREE_TYPE (nary
->op
[i
]), genop
[i
]);
2855 if (nary
->opcode
== CONSTRUCTOR
)
2857 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
2858 for (i
= 0; i
< nary
->length
; ++i
)
2859 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
, genop
[i
]);
2860 folded
= build_constructor (nary
->type
, elts
);
2861 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2862 newstmt
= gimple_build_assign (name
, folded
);
2863 gimple_seq_add_stmt_without_update (&forced_stmts
, newstmt
);
2868 switch (nary
->length
)
2871 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2875 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2876 genop
[0], genop
[1]);
2879 folded
= gimple_build (&forced_stmts
, nary
->opcode
, nary
->type
,
2880 genop
[0], genop
[1], genop
[2]);
2892 folded
= gimple_convert (&forced_stmts
, exprtype
, folded
);
2894 /* If there is nothing to insert, return the simplified result. */
2895 if (gimple_seq_empty_p (forced_stmts
))
2897 /* If we simplified to a constant return it and discard eventually
2899 if (is_gimple_min_invariant (folded
))
2901 gimple_seq_discard (forced_stmts
);
2904 /* Likewise if we simplified to sth not queued for insertion. */
2906 gsi
= gsi_last (forced_stmts
);
2907 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
2909 gimple
*stmt
= gsi_stmt (gsi
);
2910 tree forcedname
= gimple_get_lhs (stmt
);
2911 if (forcedname
== folded
)
2919 gimple_seq_discard (forced_stmts
);
2922 gcc_assert (TREE_CODE (folded
) == SSA_NAME
);
2924 /* If we have any intermediate expressions to the value sets, add them
2925 to the value sets and chain them in the instruction stream. */
2928 gsi
= gsi_start (forced_stmts
);
2929 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
2931 gimple
*stmt
= gsi_stmt (gsi
);
2932 tree forcedname
= gimple_get_lhs (stmt
);
2935 if (forcedname
!= folded
)
2937 VN_INFO_GET (forcedname
)->valnum
= forcedname
;
2938 VN_INFO (forcedname
)->value_id
= get_next_value_id ();
2939 nameexpr
= get_or_alloc_expr_for_name (forcedname
);
2940 add_to_value (VN_INFO (forcedname
)->value_id
, nameexpr
);
2941 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2942 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2945 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (forcedname
));
2946 gimple_set_plf (stmt
, NECESSARY
, false);
2948 gimple_seq_add_seq (stmts
, forced_stmts
);
2953 /* Fold the last statement. */
2954 gsi
= gsi_last (*stmts
);
2955 if (fold_stmt_inplace (&gsi
))
2956 update_stmt (gsi_stmt (gsi
));
2958 /* Add a value number to the temporary.
2959 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2960 we are creating the expression by pieces, and this particular piece of
2961 the expression may have been represented. There is no harm in replacing
2963 value_id
= get_expr_value_id (expr
);
2964 VN_INFO_GET (name
)->value_id
= value_id
;
2965 VN_INFO (name
)->valnum
= sccvn_valnum_from_value_id (value_id
);
2966 if (VN_INFO (name
)->valnum
== NULL_TREE
)
2967 VN_INFO (name
)->valnum
= name
;
2968 gcc_assert (VN_INFO (name
)->valnum
!= NULL_TREE
);
2969 nameexpr
= get_or_alloc_expr_for_name (name
);
2970 add_to_value (value_id
, nameexpr
);
2971 if (NEW_SETS (block
))
2972 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2973 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2975 pre_stats
.insertions
++;
2976 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2978 fprintf (dump_file
, "Inserted ");
2979 print_gimple_stmt (dump_file
, gsi_stmt (gsi_last (*stmts
)), 0, 0);
2980 fprintf (dump_file
, " in predecessor %d (%04d)\n",
2981 block
->index
, value_id
);
2988 /* Insert the to-be-made-available values of expression EXPRNUM for each
2989 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2990 merge the result with a phi node, given the same value number as
2991 NODE. Return true if we have inserted new stuff. */
2994 insert_into_preds_of_block (basic_block block
, unsigned int exprnum
,
2995 vec
<pre_expr
> avail
)
2997 pre_expr expr
= expression_for_id (exprnum
);
2999 unsigned int val
= get_expr_value_id (expr
);
3001 bool insertions
= false;
3006 tree type
= get_expr_type (expr
);
3010 /* Make sure we aren't creating an induction variable. */
3011 if (bb_loop_depth (block
) > 0 && EDGE_COUNT (block
->preds
) == 2)
3013 bool firstinsideloop
= false;
3014 bool secondinsideloop
= false;
3015 firstinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3016 EDGE_PRED (block
, 0)->src
);
3017 secondinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3018 EDGE_PRED (block
, 1)->src
);
3019 /* Induction variables only have one edge inside the loop. */
3020 if ((firstinsideloop
^ secondinsideloop
)
3021 && expr
->kind
!= REFERENCE
)
3023 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3024 fprintf (dump_file
, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3029 /* Make the necessary insertions. */
3030 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3032 gimple_seq stmts
= NULL
;
3035 eprime
= avail
[pred
->dest_idx
];
3036 builtexpr
= create_expression_by_pieces (bprime
, eprime
,
3038 gcc_assert (!(pred
->flags
& EDGE_ABNORMAL
));
3039 if (!gimple_seq_empty_p (stmts
))
3041 gsi_insert_seq_on_edge (pred
, stmts
);
3046 /* We cannot insert a PHI node if we failed to insert
3051 if (is_gimple_min_invariant (builtexpr
))
3052 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_constant (builtexpr
);
3054 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_name (builtexpr
);
3056 /* If we didn't want a phi node, and we made insertions, we still have
3057 inserted new stuff, and thus return true. If we didn't want a phi node,
3058 and didn't make insertions, we haven't added anything new, so return
3060 if (nophi
&& insertions
)
3062 else if (nophi
&& !insertions
)
3065 /* Now build a phi for the new variable. */
3066 temp
= make_temp_ssa_name (type
, NULL
, "prephitmp");
3067 phi
= create_phi_node (temp
, block
);
3069 gimple_set_plf (phi
, NECESSARY
, false);
3070 VN_INFO_GET (temp
)->value_id
= val
;
3071 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3072 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3073 VN_INFO (temp
)->valnum
= temp
;
3074 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3075 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3077 pre_expr ae
= avail
[pred
->dest_idx
];
3078 gcc_assert (get_expr_type (ae
) == type
3079 || useless_type_conversion_p (type
, get_expr_type (ae
)));
3080 if (ae
->kind
== CONSTANT
)
3081 add_phi_arg (phi
, unshare_expr (PRE_EXPR_CONSTANT (ae
)),
3082 pred
, UNKNOWN_LOCATION
);
3084 add_phi_arg (phi
, PRE_EXPR_NAME (ae
), pred
, UNKNOWN_LOCATION
);
3087 newphi
= get_or_alloc_expr_for_name (temp
);
3088 add_to_value (val
, newphi
);
3090 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3091 this insertion, since we test for the existence of this value in PHI_GEN
3092 before proceeding with the partial redundancy checks in insert_aux.
3094 The value may exist in AVAIL_OUT, in particular, it could be represented
3095 by the expression we are trying to eliminate, in which case we want the
3096 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3099 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3100 this block, because if it did, it would have existed in our dominator's
3101 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3104 bitmap_insert_into_set (PHI_GEN (block
), newphi
);
3105 bitmap_value_replace_in_set (AVAIL_OUT (block
),
3107 bitmap_insert_into_set (NEW_SETS (block
),
3110 /* If we insert a PHI node for a conversion of another PHI node
3111 in the same basic-block try to preserve range information.
3112 This is important so that followup loop passes receive optimal
3113 number of iteration analysis results. See PR61743. */
3114 if (expr
->kind
== NARY
3115 && CONVERT_EXPR_CODE_P (expr
->u
.nary
->opcode
)
3116 && TREE_CODE (expr
->u
.nary
->op
[0]) == SSA_NAME
3117 && gimple_bb (SSA_NAME_DEF_STMT (expr
->u
.nary
->op
[0])) == block
3118 && INTEGRAL_TYPE_P (type
)
3119 && INTEGRAL_TYPE_P (TREE_TYPE (expr
->u
.nary
->op
[0]))
3120 && (TYPE_PRECISION (type
)
3121 >= TYPE_PRECISION (TREE_TYPE (expr
->u
.nary
->op
[0])))
3122 && SSA_NAME_RANGE_INFO (expr
->u
.nary
->op
[0]))
3125 if (get_range_info (expr
->u
.nary
->op
[0], &min
, &max
) == VR_RANGE
3126 && !wi::neg_p (min
, SIGNED
)
3127 && !wi::neg_p (max
, SIGNED
))
3128 /* Just handle extension and sign-changes of all-positive ranges. */
3129 set_range_info (temp
,
3130 SSA_NAME_RANGE_TYPE (expr
->u
.nary
->op
[0]),
3131 wide_int_storage::from (min
, TYPE_PRECISION (type
),
3133 wide_int_storage::from (max
, TYPE_PRECISION (type
),
3137 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3139 fprintf (dump_file
, "Created phi ");
3140 print_gimple_stmt (dump_file
, phi
, 0, 0);
3141 fprintf (dump_file
, " in block %d (%04d)\n", block
->index
, val
);
3149 /* Perform insertion of partially redundant or hoistable values.
3150 For BLOCK, do the following:
3151 1. Propagate the NEW_SETS of the dominator into the current block.
3152 If the block has multiple predecessors,
3153 2a. Iterate over the ANTIC expressions for the block to see if
3154 any of them are partially redundant.
3155 2b. If so, insert them into the necessary predecessors to make
3156 the expression fully redundant.
3157 2c. Insert a new PHI merging the values of the predecessors.
3158 2d. Insert the new PHI, and the new expressions, into the
3160 If the block has multiple successors,
3161 3a. Iterate over the ANTIC values for the block to see if
3162 any of them are good candidates for hoisting.
3163 3b. If so, insert expressions computing the values in BLOCK,
3164 and add the new expressions into the NEW_SETS set.
3165 4. Recursively call ourselves on the dominator children of BLOCK.
3167 Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3168 do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3169 done in do_hoist_insertion.
3173 do_pre_regular_insertion (basic_block block
, basic_block dom
)
3175 bool new_stuff
= false;
3176 vec
<pre_expr
> exprs
;
3178 auto_vec
<pre_expr
> avail
;
3181 exprs
= sorted_array_from_bitmap_set (ANTIC_IN (block
));
3182 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3184 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3186 if (expr
->kind
== NARY
3187 || expr
->kind
== REFERENCE
)
3190 bool by_some
= false;
3191 bool cant_insert
= false;
3192 bool all_same
= true;
3193 pre_expr first_s
= NULL
;
3196 pre_expr eprime
= NULL
;
3198 pre_expr edoubleprime
= NULL
;
3199 bool do_insertion
= false;
3201 val
= get_expr_value_id (expr
);
3202 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3204 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3206 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3208 fprintf (dump_file
, "Found fully redundant value: ");
3209 print_pre_expr (dump_file
, expr
);
3210 fprintf (dump_file
, "\n");
3215 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3217 unsigned int vprime
;
3219 /* We should never run insertion for the exit block
3220 and so not come across fake pred edges. */
3221 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3223 /* We are looking at ANTIC_OUT of bprime. */
3224 eprime
= phi_translate (expr
, ANTIC_IN (block
), NULL
,
3227 /* eprime will generally only be NULL if the
3228 value of the expression, translated
3229 through the PHI for this predecessor, is
3230 undefined. If that is the case, we can't
3231 make the expression fully redundant,
3232 because its value is undefined along a
3233 predecessor path. We can thus break out
3234 early because it doesn't matter what the
3235 rest of the results are. */
3238 avail
[pred
->dest_idx
] = NULL
;
3243 vprime
= get_expr_value_id (eprime
);
3244 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
),
3246 if (edoubleprime
== NULL
)
3248 avail
[pred
->dest_idx
] = eprime
;
3253 avail
[pred
->dest_idx
] = edoubleprime
;
3255 /* We want to perform insertions to remove a redundancy on
3256 a path in the CFG we want to optimize for speed. */
3257 if (optimize_edge_for_speed_p (pred
))
3258 do_insertion
= true;
3259 if (first_s
== NULL
)
3260 first_s
= edoubleprime
;
3261 else if (!pre_expr_d::equal (first_s
, edoubleprime
))
3265 /* If we can insert it, it's not the same value
3266 already existing along every predecessor, and
3267 it's defined by some predecessor, it is
3268 partially redundant. */
3269 if (!cant_insert
&& !all_same
&& by_some
)
3273 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3275 fprintf (dump_file
, "Skipping partial redundancy for "
3277 print_pre_expr (dump_file
, expr
);
3278 fprintf (dump_file
, " (%04d), no redundancy on to be "
3279 "optimized for speed edge\n", val
);
3282 else if (dbg_cnt (treepre_insert
))
3284 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3286 fprintf (dump_file
, "Found partial redundancy for "
3288 print_pre_expr (dump_file
, expr
);
3289 fprintf (dump_file
, " (%04d)\n",
3290 get_expr_value_id (expr
));
3292 if (insert_into_preds_of_block (block
,
3293 get_expression_id (expr
),
3298 /* If all edges produce the same value and that value is
3299 an invariant, then the PHI has the same value on all
3300 edges. Note this. */
3301 else if (!cant_insert
&& all_same
)
3303 gcc_assert (edoubleprime
->kind
== CONSTANT
3304 || edoubleprime
->kind
== NAME
);
3306 tree temp
= make_temp_ssa_name (get_expr_type (expr
),
3309 = gimple_build_assign (temp
,
3310 edoubleprime
->kind
== CONSTANT
?
3311 PRE_EXPR_CONSTANT (edoubleprime
) :
3312 PRE_EXPR_NAME (edoubleprime
));
3313 gimple_stmt_iterator gsi
= gsi_after_labels (block
);
3314 gsi_insert_before (&gsi
, assign
, GSI_NEW_STMT
);
3316 gimple_set_plf (assign
, NECESSARY
, false);
3317 VN_INFO_GET (temp
)->value_id
= val
;
3318 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3319 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3320 VN_INFO (temp
)->valnum
= temp
;
3321 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3322 pre_expr newe
= get_or_alloc_expr_for_name (temp
);
3323 add_to_value (val
, newe
);
3324 bitmap_value_replace_in_set (AVAIL_OUT (block
), newe
);
3325 bitmap_insert_into_set (NEW_SETS (block
), newe
);
3335 /* Perform insertion for partially anticipatable expressions. There
3336 is only one case we will perform insertion for these. This case is
3337 if the expression is partially anticipatable, and fully available.
3338 In this case, we know that putting it earlier will enable us to
3339 remove the later computation. */
3342 do_pre_partial_partial_insertion (basic_block block
, basic_block dom
)
3344 bool new_stuff
= false;
3345 vec
<pre_expr
> exprs
;
3347 auto_vec
<pre_expr
> avail
;
3350 exprs
= sorted_array_from_bitmap_set (PA_IN (block
));
3351 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3353 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3355 if (expr
->kind
== NARY
3356 || expr
->kind
== REFERENCE
)
3360 bool cant_insert
= false;
3363 pre_expr eprime
= NULL
;
3366 val
= get_expr_value_id (expr
);
3367 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3369 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3372 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3374 unsigned int vprime
;
3375 pre_expr edoubleprime
;
3377 /* We should never run insertion for the exit block
3378 and so not come across fake pred edges. */
3379 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3381 eprime
= phi_translate (expr
, ANTIC_IN (block
),
3385 /* eprime will generally only be NULL if the
3386 value of the expression, translated
3387 through the PHI for this predecessor, is
3388 undefined. If that is the case, we can't
3389 make the expression fully redundant,
3390 because its value is undefined along a
3391 predecessor path. We can thus break out
3392 early because it doesn't matter what the
3393 rest of the results are. */
3396 avail
[pred
->dest_idx
] = NULL
;
3401 vprime
= get_expr_value_id (eprime
);
3402 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
), vprime
);
3403 avail
[pred
->dest_idx
] = edoubleprime
;
3404 if (edoubleprime
== NULL
)
3411 /* If we can insert it, it's not the same value
3412 already existing along every predecessor, and
3413 it's defined by some predecessor, it is
3414 partially redundant. */
3415 if (!cant_insert
&& by_all
)
3418 bool do_insertion
= false;
3420 /* Insert only if we can remove a later expression on a path
3421 that we want to optimize for speed.
3422 The phi node that we will be inserting in BLOCK is not free,
3423 and inserting it for the sake of !optimize_for_speed successor
3424 may cause regressions on the speed path. */
3425 FOR_EACH_EDGE (succ
, ei
, block
->succs
)
3427 if (bitmap_set_contains_value (PA_IN (succ
->dest
), val
)
3428 || bitmap_set_contains_value (ANTIC_IN (succ
->dest
), val
))
3430 if (optimize_edge_for_speed_p (succ
))
3431 do_insertion
= true;
3437 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3439 fprintf (dump_file
, "Skipping partial partial redundancy "
3441 print_pre_expr (dump_file
, expr
);
3442 fprintf (dump_file
, " (%04d), not (partially) anticipated "
3443 "on any to be optimized for speed edges\n", val
);
3446 else if (dbg_cnt (treepre_insert
))
3448 pre_stats
.pa_insert
++;
3449 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3451 fprintf (dump_file
, "Found partial partial redundancy "
3453 print_pre_expr (dump_file
, expr
);
3454 fprintf (dump_file
, " (%04d)\n",
3455 get_expr_value_id (expr
));
3457 if (insert_into_preds_of_block (block
,
3458 get_expression_id (expr
),
3470 /* Insert expressions in BLOCK to compute hoistable values up.
3471 Return TRUE if something was inserted, otherwise return FALSE.
3472 The caller has to make sure that BLOCK has at least two successors. */
3475 do_hoist_insertion (basic_block block
)
3479 bool new_stuff
= false;
3481 gimple_stmt_iterator last
;
3483 /* At least two successors, or else... */
3484 gcc_assert (EDGE_COUNT (block
->succs
) >= 2);
3486 /* Check that all successors of BLOCK are dominated by block.
3487 We could use dominated_by_p() for this, but actually there is a much
3488 quicker check: any successor that is dominated by BLOCK can't have
3489 more than one predecessor edge. */
3490 FOR_EACH_EDGE (e
, ei
, block
->succs
)
3491 if (! single_pred_p (e
->dest
))
3494 /* Determine the insertion point. If we cannot safely insert before
3495 the last stmt if we'd have to, bail out. */
3496 last
= gsi_last_bb (block
);
3497 if (!gsi_end_p (last
)
3498 && !is_ctrl_stmt (gsi_stmt (last
))
3499 && stmt_ends_bb_p (gsi_stmt (last
)))
3502 /* Compute the set of hoistable expressions from ANTIC_IN. First compute
3503 hoistable values. */
3504 bitmap_set hoistable_set
;
3506 /* A hoistable value must be in ANTIC_IN(block)
3507 but not in AVAIL_OUT(BLOCK). */
3508 bitmap_initialize (&hoistable_set
.values
, &grand_bitmap_obstack
);
3509 bitmap_and_compl (&hoistable_set
.values
,
3510 &ANTIC_IN (block
)->values
, &AVAIL_OUT (block
)->values
);
3512 /* Short-cut for a common case: hoistable_set is empty. */
3513 if (bitmap_empty_p (&hoistable_set
.values
))
3516 /* Compute which of the hoistable values is in AVAIL_OUT of
3517 at least one of the successors of BLOCK. */
3518 bitmap_head availout_in_some
;
3519 bitmap_initialize (&availout_in_some
, &grand_bitmap_obstack
);
3520 FOR_EACH_EDGE (e
, ei
, block
->succs
)
3521 /* Do not consider expressions solely because their availability
3522 on loop exits. They'd be ANTIC-IN throughout the whole loop
3523 and thus effectively hoisted across loops by combination of
3524 PRE and hoisting. */
3525 if (! loop_exit_edge_p (block
->loop_father
, e
))
3526 bitmap_ior_and_into (&availout_in_some
, &hoistable_set
.values
,
3527 &AVAIL_OUT (e
->dest
)->values
);
3528 bitmap_clear (&hoistable_set
.values
);
3530 /* Short-cut for a common case: availout_in_some is empty. */
3531 if (bitmap_empty_p (&availout_in_some
))
3534 /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */
3535 hoistable_set
.values
= availout_in_some
;
3536 hoistable_set
.expressions
= ANTIC_IN (block
)->expressions
;
3538 /* Now finally construct the topological-ordered expression set. */
3539 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (&hoistable_set
);
3541 bitmap_clear (&hoistable_set
.values
);
3543 /* If there are candidate values for hoisting, insert expressions
3544 strategically to make the hoistable expressions fully redundant. */
3546 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3548 /* While we try to sort expressions topologically above the
3549 sorting doesn't work out perfectly. Catch expressions we
3550 already inserted. */
3551 unsigned int value_id
= get_expr_value_id (expr
);
3552 if (bitmap_set_contains_value (AVAIL_OUT (block
), value_id
))
3554 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3557 "Already inserted expression for ");
3558 print_pre_expr (dump_file
, expr
);
3559 fprintf (dump_file
, " (%04d)\n", value_id
);
3564 /* OK, we should hoist this value. Perform the transformation. */
3565 pre_stats
.hoist_insert
++;
3566 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3569 "Inserting expression in block %d for code hoisting: ",
3571 print_pre_expr (dump_file
, expr
);
3572 fprintf (dump_file
, " (%04d)\n", value_id
);
3575 gimple_seq stmts
= NULL
;
3576 tree res
= create_expression_by_pieces (block
, expr
, &stmts
,
3577 get_expr_type (expr
));
3579 /* Do not return true if expression creation ultimately
3580 did not insert any statements. */
3581 if (gimple_seq_empty_p (stmts
))
3585 if (gsi_end_p (last
) || is_ctrl_stmt (gsi_stmt (last
)))
3586 gsi_insert_seq_before (&last
, stmts
, GSI_SAME_STMT
);
3588 gsi_insert_seq_after (&last
, stmts
, GSI_NEW_STMT
);
3591 /* Make sure to not return true if expression creation ultimately
3592 failed but also make sure to insert any stmts produced as they
3593 are tracked in inserted_exprs. */
3605 /* Do a dominator walk on the control flow graph, and insert computations
3606 of values as necessary for PRE and hoisting. */
3609 insert_aux (basic_block block
, bool do_pre
, bool do_hoist
)
3612 bool new_stuff
= false;
3617 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3622 bitmap_set_t newset
;
3624 /* First, update the AVAIL_OUT set with anything we may have
3625 inserted higher up in the dominator tree. */
3626 newset
= NEW_SETS (dom
);
3629 /* Note that we need to value_replace both NEW_SETS, and
3630 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3631 represented by some non-simple expression here that we want
3632 to replace it with. */
3633 FOR_EACH_EXPR_ID_IN_SET (newset
, i
, bi
)
3635 pre_expr expr
= expression_for_id (i
);
3636 bitmap_value_replace_in_set (NEW_SETS (block
), expr
);
3637 bitmap_value_replace_in_set (AVAIL_OUT (block
), expr
);
3641 /* Insert expressions for partial redundancies. */
3642 if (do_pre
&& !single_pred_p (block
))
3644 new_stuff
|= do_pre_regular_insertion (block
, dom
);
3645 if (do_partial_partial
)
3646 new_stuff
|= do_pre_partial_partial_insertion (block
, dom
);
3649 /* Insert expressions for hoisting. */
3650 if (do_hoist
&& EDGE_COUNT (block
->succs
) >= 2)
3651 new_stuff
|= do_hoist_insertion (block
);
3654 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
3656 son
= next_dom_son (CDI_DOMINATORS
, son
))
3658 new_stuff
|= insert_aux (son
, do_pre
, do_hoist
);
3664 /* Perform insertion of partially redundant and hoistable values. */
3669 bool new_stuff
= true;
3671 int num_iterations
= 0;
3673 FOR_ALL_BB_FN (bb
, cfun
)
3674 NEW_SETS (bb
) = bitmap_set_new ();
3679 if (dump_file
&& dump_flags
& TDF_DETAILS
)
3680 fprintf (dump_file
, "Starting insert iteration %d\n", num_iterations
);
3681 new_stuff
= insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun
), flag_tree_pre
,
3682 flag_code_hoisting
);
3684 /* Clear the NEW sets before the next iteration. We have already
3685 fully propagated its contents. */
3687 FOR_ALL_BB_FN (bb
, cfun
)
3688 bitmap_set_free (NEW_SETS (bb
));
3690 statistics_histogram_event (cfun
, "insert iterations", num_iterations
);
3694 /* Compute the AVAIL set for all basic blocks.
3696 This function performs value numbering of the statements in each basic
3697 block. The AVAIL sets are built from information we glean while doing
3698 this value numbering, since the AVAIL sets contain only one entry per
3701 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3702 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3705 compute_avail (void)
3708 basic_block block
, son
;
3709 basic_block
*worklist
;
3714 /* We pretend that default definitions are defined in the entry block.
3715 This includes function arguments and the static chain decl. */
3716 FOR_EACH_SSA_NAME (i
, name
, cfun
)
3719 if (!SSA_NAME_IS_DEFAULT_DEF (name
)
3720 || has_zero_uses (name
)
3721 || virtual_operand_p (name
))
3724 e
= get_or_alloc_expr_for_name (name
);
3725 add_to_value (get_expr_value_id (e
), e
);
3726 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)), e
);
3727 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3731 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3733 print_bitmap_set (dump_file
, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3734 "tmp_gen", ENTRY_BLOCK
);
3735 print_bitmap_set (dump_file
, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3736 "avail_out", ENTRY_BLOCK
);
3739 /* Allocate the worklist. */
3740 worklist
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
3742 /* Seed the algorithm by putting the dominator children of the entry
3743 block on the worklist. */
3744 for (son
= first_dom_son (CDI_DOMINATORS
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3746 son
= next_dom_son (CDI_DOMINATORS
, son
))
3747 worklist
[sp
++] = son
;
3749 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun
))
3750 = ssa_default_def (cfun
, gimple_vop (cfun
));
3752 /* Loop until the worklist is empty. */
3758 /* Pick a block from the worklist. */
3759 block
= worklist
[--sp
];
3761 /* Initially, the set of available values in BLOCK is that of
3762 its immediate dominator. */
3763 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3766 bitmap_set_copy (AVAIL_OUT (block
), AVAIL_OUT (dom
));
3767 BB_LIVE_VOP_ON_EXIT (block
) = BB_LIVE_VOP_ON_EXIT (dom
);
3770 /* Generate values for PHI nodes. */
3771 for (gphi_iterator gsi
= gsi_start_phis (block
); !gsi_end_p (gsi
);
3774 tree result
= gimple_phi_result (gsi
.phi ());
3776 /* We have no need for virtual phis, as they don't represent
3777 actual computations. */
3778 if (virtual_operand_p (result
))
3780 BB_LIVE_VOP_ON_EXIT (block
) = result
;
3784 pre_expr e
= get_or_alloc_expr_for_name (result
);
3785 add_to_value (get_expr_value_id (e
), e
);
3786 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3787 bitmap_insert_into_set (PHI_GEN (block
), e
);
3790 BB_MAY_NOTRETURN (block
) = 0;
3792 /* Now compute value numbers and populate value sets with all
3793 the expressions computed in BLOCK. */
3794 for (gimple_stmt_iterator gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
);
3800 stmt
= gsi_stmt (gsi
);
3802 /* Cache whether the basic-block has any non-visible side-effect
3804 If this isn't a call or it is the last stmt in the
3805 basic-block then the CFG represents things correctly. */
3806 if (is_gimple_call (stmt
) && !stmt_ends_bb_p (stmt
))
3808 /* Non-looping const functions always return normally.
3809 Otherwise the call might not return or have side-effects
3810 that forbids hoisting possibly trapping expressions
3812 int flags
= gimple_call_flags (stmt
);
3813 if (!(flags
& ECF_CONST
)
3814 || (flags
& ECF_LOOPING_CONST_OR_PURE
))
3815 BB_MAY_NOTRETURN (block
) = 1;
3818 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
3820 pre_expr e
= get_or_alloc_expr_for_name (op
);
3822 add_to_value (get_expr_value_id (e
), e
);
3823 bitmap_insert_into_set (TMP_GEN (block
), e
);
3824 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3827 if (gimple_vdef (stmt
))
3828 BB_LIVE_VOP_ON_EXIT (block
) = gimple_vdef (stmt
);
3830 if (gimple_has_side_effects (stmt
)
3831 || stmt_could_throw_p (stmt
)
3832 || is_gimple_debug (stmt
))
3835 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
3837 if (ssa_undefined_value_p (op
))
3839 pre_expr e
= get_or_alloc_expr_for_name (op
);
3840 bitmap_value_insert_into_set (EXP_GEN (block
), e
);
3843 switch (gimple_code (stmt
))
3851 vn_reference_s ref1
;
3852 pre_expr result
= NULL
;
3854 /* We can value number only calls to real functions. */
3855 if (gimple_call_internal_p (stmt
))
3858 vn_reference_lookup_call (as_a
<gcall
*> (stmt
), &ref
, &ref1
);
3862 /* If the value of the call is not invalidated in
3863 this block until it is computed, add the expression
3865 if (!gimple_vuse (stmt
)
3867 (SSA_NAME_DEF_STMT (gimple_vuse (stmt
))) == GIMPLE_PHI
3868 || gimple_bb (SSA_NAME_DEF_STMT
3869 (gimple_vuse (stmt
))) != block
)
3871 result
= pre_expr_pool
.allocate ();
3872 result
->kind
= REFERENCE
;
3874 PRE_EXPR_REFERENCE (result
) = ref
;
3876 get_or_alloc_expression_id (result
);
3877 add_to_value (get_expr_value_id (result
), result
);
3878 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
3885 pre_expr result
= NULL
;
3886 switch (vn_get_stmt_kind (stmt
))
3890 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3893 /* COND_EXPR and VEC_COND_EXPR are awkward in
3894 that they contain an embedded complex expression.
3895 Don't even try to shove those through PRE. */
3896 if (code
== COND_EXPR
3897 || code
== VEC_COND_EXPR
)
3900 vn_nary_op_lookup_stmt (stmt
, &nary
);
3904 /* If the NARY traps and there was a preceding
3905 point in the block that might not return avoid
3906 adding the nary to EXP_GEN. */
3907 if (BB_MAY_NOTRETURN (block
)
3908 && vn_nary_may_trap (nary
))
3911 result
= pre_expr_pool
.allocate ();
3912 result
->kind
= NARY
;
3914 PRE_EXPR_NARY (result
) = nary
;
3920 tree rhs1
= gimple_assign_rhs1 (stmt
);
3921 alias_set_type set
= get_alias_set (rhs1
);
3922 vec
<vn_reference_op_s
> operands
3923 = vn_reference_operands_for_lookup (rhs1
);
3925 vn_reference_lookup_pieces (gimple_vuse (stmt
), set
,
3927 operands
, &ref
, VN_WALK
);
3930 operands
.release ();
3934 /* If the value of the reference is not invalidated in
3935 this block until it is computed, add the expression
3937 if (gimple_vuse (stmt
))
3941 def_stmt
= SSA_NAME_DEF_STMT (gimple_vuse (stmt
));
3942 while (!gimple_nop_p (def_stmt
)
3943 && gimple_code (def_stmt
) != GIMPLE_PHI
3944 && gimple_bb (def_stmt
) == block
)
3946 if (stmt_may_clobber_ref_p
3947 (def_stmt
, gimple_assign_rhs1 (stmt
)))
3953 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt
));
3957 operands
.release ();
3962 /* If the load was value-numbered to another
3963 load make sure we do not use its expression
3964 for insertion if it wouldn't be a valid
3966 /* At the momemt we have a testcase
3967 for hoist insertion of aligned vs. misaligned
3968 variants in gcc.dg/torture/pr65270-1.c thus
3969 with just alignment to be considered we can
3970 simply replace the expression in the hashtable
3971 with the most conservative one. */
3972 vn_reference_op_t ref1
= &ref
->operands
.last ();
3973 while (ref1
->opcode
!= TARGET_MEM_REF
3974 && ref1
->opcode
!= MEM_REF
3975 && ref1
!= &ref
->operands
[0])
3977 vn_reference_op_t ref2
= &operands
.last ();
3978 while (ref2
->opcode
!= TARGET_MEM_REF
3979 && ref2
->opcode
!= MEM_REF
3980 && ref2
!= &operands
[0])
3982 if ((ref1
->opcode
== TARGET_MEM_REF
3983 || ref1
->opcode
== MEM_REF
)
3984 && (TYPE_ALIGN (ref1
->type
)
3985 > TYPE_ALIGN (ref2
->type
)))
3987 = build_aligned_type (ref1
->type
,
3988 TYPE_ALIGN (ref2
->type
));
3989 /* TBAA behavior is an obvious part so make sure
3990 that the hashtable one covers this as well
3991 by adjusting the ref alias set and its base. */
3993 || alias_set_subset_of (set
, ref
->set
))
3995 else if (alias_set_subset_of (ref
->set
, set
))
3998 if (ref1
->opcode
== MEM_REF
)
3999 ref1
->op0
= wide_int_to_tree (TREE_TYPE (ref2
->op0
),
4002 ref1
->op2
= wide_int_to_tree (TREE_TYPE (ref2
->op2
),
4008 if (ref1
->opcode
== MEM_REF
)
4009 ref1
->op0
= wide_int_to_tree (ptr_type_node
,
4012 ref1
->op2
= wide_int_to_tree (ptr_type_node
,
4015 operands
.release ();
4017 result
= pre_expr_pool
.allocate ();
4018 result
->kind
= REFERENCE
;
4020 PRE_EXPR_REFERENCE (result
) = ref
;
4028 get_or_alloc_expression_id (result
);
4029 add_to_value (get_expr_value_id (result
), result
);
4030 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
4038 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4040 print_bitmap_set (dump_file
, EXP_GEN (block
),
4041 "exp_gen", block
->index
);
4042 print_bitmap_set (dump_file
, PHI_GEN (block
),
4043 "phi_gen", block
->index
);
4044 print_bitmap_set (dump_file
, TMP_GEN (block
),
4045 "tmp_gen", block
->index
);
4046 print_bitmap_set (dump_file
, AVAIL_OUT (block
),
4047 "avail_out", block
->index
);
4050 /* Put the dominator children of BLOCK on the worklist of blocks
4051 to compute available sets for. */
4052 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
4054 son
= next_dom_son (CDI_DOMINATORS
, son
))
4055 worklist
[sp
++] = son
;
4062 /* Local state for the eliminate domwalk. */
4063 static vec
<gimple
*> el_to_remove
;
4064 static vec
<gimple
*> el_to_fixup
;
4065 static unsigned int el_todo
;
4066 static vec
<tree
> el_avail
;
4067 static vec
<tree
> el_avail_stack
;
4069 /* Return a leader for OP that is available at the current point of the
4070 eliminate domwalk. */
4073 eliminate_avail (tree op
)
4075 tree valnum
= VN_INFO (op
)->valnum
;
4076 if (TREE_CODE (valnum
) == SSA_NAME
)
4078 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
4080 if (el_avail
.length () > SSA_NAME_VERSION (valnum
))
4081 return el_avail
[SSA_NAME_VERSION (valnum
)];
4083 else if (is_gimple_min_invariant (valnum
))
4088 /* At the current point of the eliminate domwalk make OP available. */
4091 eliminate_push_avail (tree op
)
4093 tree valnum
= VN_INFO (op
)->valnum
;
4094 if (TREE_CODE (valnum
) == SSA_NAME
)
4096 if (el_avail
.length () <= SSA_NAME_VERSION (valnum
))
4097 el_avail
.safe_grow_cleared (SSA_NAME_VERSION (valnum
) + 1);
4099 if (el_avail
[SSA_NAME_VERSION (valnum
)])
4100 pushop
= el_avail
[SSA_NAME_VERSION (valnum
)];
4101 el_avail_stack
.safe_push (pushop
);
4102 el_avail
[SSA_NAME_VERSION (valnum
)] = op
;
4106 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
4107 the leader for the expression if insertion was successful. */
4110 eliminate_insert (gimple_stmt_iterator
*gsi
, tree val
)
4112 /* We can insert a sequence with a single assignment only. */
4113 gimple_seq stmts
= VN_INFO (val
)->expr
;
4114 if (!gimple_seq_singleton_p (stmts
))
4116 gassign
*stmt
= dyn_cast
<gassign
*> (gimple_seq_first_stmt (stmts
));
4118 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt
))
4119 && gimple_assign_rhs_code (stmt
) != VIEW_CONVERT_EXPR
4120 && gimple_assign_rhs_code (stmt
) != BIT_FIELD_REF
4121 && (gimple_assign_rhs_code (stmt
) != BIT_AND_EXPR
4122 || TREE_CODE (gimple_assign_rhs2 (stmt
)) != INTEGER_CST
)))
4125 tree op
= gimple_assign_rhs1 (stmt
);
4126 if (gimple_assign_rhs_code (stmt
) == VIEW_CONVERT_EXPR
4127 || gimple_assign_rhs_code (stmt
) == BIT_FIELD_REF
)
4128 op
= TREE_OPERAND (op
, 0);
4129 tree leader
= TREE_CODE (op
) == SSA_NAME
? eliminate_avail (op
) : op
;
4135 if (gimple_assign_rhs_code (stmt
) == BIT_FIELD_REF
)
4136 res
= gimple_build (&stmts
, BIT_FIELD_REF
,
4137 TREE_TYPE (val
), leader
,
4138 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 1),
4139 TREE_OPERAND (gimple_assign_rhs1 (stmt
), 2));
4140 else if (gimple_assign_rhs_code (stmt
) == BIT_AND_EXPR
)
4141 res
= gimple_build (&stmts
, BIT_AND_EXPR
,
4142 TREE_TYPE (val
), leader
, gimple_assign_rhs2 (stmt
));
4144 res
= gimple_build (&stmts
, gimple_assign_rhs_code (stmt
),
4145 TREE_TYPE (val
), leader
);
4146 if (TREE_CODE (res
) != SSA_NAME
4147 || SSA_NAME_IS_DEFAULT_DEF (res
)
4148 || gimple_bb (SSA_NAME_DEF_STMT (res
)))
4150 gimple_seq_discard (stmts
);
4152 /* During propagation we have to treat SSA info conservatively
4153 and thus we can end up simplifying the inserted expression
4154 at elimination time to sth not defined in stmts. */
4155 /* But then this is a redundancy we failed to detect. Which means
4156 res now has two values. That doesn't play well with how
4157 we track availability here, so give up. */
4158 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4160 if (TREE_CODE (res
) == SSA_NAME
)
4161 res
= eliminate_avail (res
);
4164 fprintf (dump_file
, "Failed to insert expression for value ");
4165 print_generic_expr (dump_file
, val
, 0);
4166 fprintf (dump_file
, " which is really fully redundant to ");
4167 print_generic_expr (dump_file
, res
, 0);
4168 fprintf (dump_file
, "\n");
4176 gsi_insert_seq_before (gsi
, stmts
, GSI_SAME_STMT
);
4177 VN_INFO_GET (res
)->valnum
= val
;
4179 if (TREE_CODE (leader
) == SSA_NAME
)
4180 gimple_set_plf (SSA_NAME_DEF_STMT (leader
), NECESSARY
, true);
4183 pre_stats
.insertions
++;
4184 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4186 fprintf (dump_file
, "Inserted ");
4187 print_gimple_stmt (dump_file
, SSA_NAME_DEF_STMT (res
), 0, 0);
4193 class eliminate_dom_walker
: public dom_walker
4196 eliminate_dom_walker (cdi_direction direction
, bool do_pre_
)
4197 : dom_walker (direction
), do_pre (do_pre_
) {}
4199 virtual edge
before_dom_children (basic_block
);
4200 virtual void after_dom_children (basic_block
);
4205 /* Perform elimination for the basic-block B during the domwalk. */
4208 eliminate_dom_walker::before_dom_children (basic_block b
)
4211 el_avail_stack
.safe_push (NULL_TREE
);
4213 /* ??? If we do nothing for unreachable blocks then this will confuse
4214 tailmerging. Eventually we can reduce its reliance on SCCVN now
4215 that we fully copy/constant-propagate (most) things. */
4217 for (gphi_iterator gsi
= gsi_start_phis (b
); !gsi_end_p (gsi
);)
4219 gphi
*phi
= gsi
.phi ();
4220 tree res
= PHI_RESULT (phi
);
4222 if (virtual_operand_p (res
))
4228 tree sprime
= eliminate_avail (res
);
4232 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4234 fprintf (dump_file
, "Replaced redundant PHI node defining ");
4235 print_generic_expr (dump_file
, res
, 0);
4236 fprintf (dump_file
, " with ");
4237 print_generic_expr (dump_file
, sprime
, 0);
4238 fprintf (dump_file
, "\n");
4241 /* If we inserted this PHI node ourself, it's not an elimination. */
4243 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
)))
4246 pre_stats
.eliminations
++;
4248 /* If we will propagate into all uses don't bother to do
4250 if (may_propagate_copy (res
, sprime
))
4252 /* Mark the PHI for removal. */
4253 el_to_remove
.safe_push (phi
);
4258 remove_phi_node (&gsi
, false);
4261 && !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
))
4262 && TREE_CODE (sprime
) == SSA_NAME
)
4263 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
4265 if (!useless_type_conversion_p (TREE_TYPE (res
), TREE_TYPE (sprime
)))
4266 sprime
= fold_convert (TREE_TYPE (res
), sprime
);
4267 gimple
*stmt
= gimple_build_assign (res
, sprime
);
4268 /* ??? It cannot yet be necessary (DOM walk). */
4269 gimple_set_plf (stmt
, NECESSARY
, gimple_plf (phi
, NECESSARY
));
4271 gimple_stmt_iterator gsi2
= gsi_after_labels (b
);
4272 gsi_insert_before (&gsi2
, stmt
, GSI_NEW_STMT
);
4276 eliminate_push_avail (res
);
4280 for (gimple_stmt_iterator gsi
= gsi_start_bb (b
);
4284 tree sprime
= NULL_TREE
;
4285 gimple
*stmt
= gsi_stmt (gsi
);
4286 tree lhs
= gimple_get_lhs (stmt
);
4287 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
4288 && !gimple_has_volatile_ops (stmt
)
4289 /* See PR43491. Do not replace a global register variable when
4290 it is a the RHS of an assignment. Do replace local register
4291 variables since gcc does not guarantee a local variable will
4292 be allocated in register.
4293 ??? The fix isn't effective here. This should instead
4294 be ensured by not value-numbering them the same but treating
4295 them like volatiles? */
4296 && !(gimple_assign_single_p (stmt
)
4297 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == VAR_DECL
4298 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
))
4299 && is_global_var (gimple_assign_rhs1 (stmt
)))))
4301 sprime
= eliminate_avail (lhs
);
4304 /* If there is no existing usable leader but SCCVN thinks
4305 it has an expression it wants to use as replacement,
4307 tree val
= VN_INFO (lhs
)->valnum
;
4309 && TREE_CODE (val
) == SSA_NAME
4310 && VN_INFO (val
)->needs_insertion
4311 && VN_INFO (val
)->expr
!= NULL
4312 && (sprime
= eliminate_insert (&gsi
, val
)) != NULL_TREE
)
4313 eliminate_push_avail (sprime
);
4316 /* If this now constitutes a copy duplicate points-to
4317 and range info appropriately. This is especially
4318 important for inserted code. See tree-ssa-copy.c
4319 for similar code. */
4321 && TREE_CODE (sprime
) == SSA_NAME
)
4323 basic_block sprime_b
= gimple_bb (SSA_NAME_DEF_STMT (sprime
));
4324 if (POINTER_TYPE_P (TREE_TYPE (lhs
))
4325 && VN_INFO_PTR_INFO (lhs
)
4326 && ! VN_INFO_PTR_INFO (sprime
))
4328 duplicate_ssa_name_ptr_info (sprime
,
4329 VN_INFO_PTR_INFO (lhs
));
4331 mark_ptr_info_alignment_unknown
4332 (SSA_NAME_PTR_INFO (sprime
));
4334 else if (INTEGRAL_TYPE_P (TREE_TYPE (lhs
))
4335 && VN_INFO_RANGE_INFO (lhs
)
4336 && ! VN_INFO_RANGE_INFO (sprime
)
4338 duplicate_ssa_name_range_info (sprime
,
4339 VN_INFO_RANGE_TYPE (lhs
),
4340 VN_INFO_RANGE_INFO (lhs
));
4343 /* Inhibit the use of an inserted PHI on a loop header when
4344 the address of the memory reference is a simple induction
4345 variable. In other cases the vectorizer won't do anything
4346 anyway (either it's loop invariant or a complicated
4349 && TREE_CODE (sprime
) == SSA_NAME
4351 && (flag_tree_loop_vectorize
|| flag_tree_parallelize_loops
> 1)
4352 && loop_outer (b
->loop_father
)
4353 && has_zero_uses (sprime
)
4354 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))
4355 && gimple_assign_load_p (stmt
))
4357 gimple
*def_stmt
= SSA_NAME_DEF_STMT (sprime
);
4358 basic_block def_bb
= gimple_bb (def_stmt
);
4359 if (gimple_code (def_stmt
) == GIMPLE_PHI
4360 && def_bb
->loop_father
->header
== def_bb
)
4362 loop_p loop
= def_bb
->loop_father
;
4366 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
4369 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (op
));
4371 && flow_bb_inside_loop_p (loop
, def_bb
)
4372 && simple_iv (loop
, loop
, op
, &iv
, true))
4380 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4382 fprintf (dump_file
, "Not replacing ");
4383 print_gimple_expr (dump_file
, stmt
, 0, 0);
4384 fprintf (dump_file
, " with ");
4385 print_generic_expr (dump_file
, sprime
, 0);
4386 fprintf (dump_file
, " which would add a loop"
4387 " carried dependence to loop %d\n",
4390 /* Don't keep sprime available. */
4398 /* If we can propagate the value computed for LHS into
4399 all uses don't bother doing anything with this stmt. */
4400 if (may_propagate_copy (lhs
, sprime
))
4402 /* Mark it for removal. */
4403 el_to_remove
.safe_push (stmt
);
4405 /* ??? Don't count copy/constant propagations. */
4406 if (gimple_assign_single_p (stmt
)
4407 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4408 || gimple_assign_rhs1 (stmt
) == sprime
))
4411 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4413 fprintf (dump_file
, "Replaced ");
4414 print_gimple_expr (dump_file
, stmt
, 0, 0);
4415 fprintf (dump_file
, " with ");
4416 print_generic_expr (dump_file
, sprime
, 0);
4417 fprintf (dump_file
, " in all uses of ");
4418 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4421 pre_stats
.eliminations
++;
4425 /* If this is an assignment from our leader (which
4426 happens in the case the value-number is a constant)
4427 then there is nothing to do. */
4428 if (gimple_assign_single_p (stmt
)
4429 && sprime
== gimple_assign_rhs1 (stmt
))
4432 /* Else replace its RHS. */
4433 bool can_make_abnormal_goto
4434 = is_gimple_call (stmt
)
4435 && stmt_can_make_abnormal_goto (stmt
);
4437 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4439 fprintf (dump_file
, "Replaced ");
4440 print_gimple_expr (dump_file
, stmt
, 0, 0);
4441 fprintf (dump_file
, " with ");
4442 print_generic_expr (dump_file
, sprime
, 0);
4443 fprintf (dump_file
, " in ");
4444 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4447 if (TREE_CODE (sprime
) == SSA_NAME
)
4448 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4451 pre_stats
.eliminations
++;
4452 gimple
*orig_stmt
= stmt
;
4453 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
4454 TREE_TYPE (sprime
)))
4455 sprime
= fold_convert (TREE_TYPE (lhs
), sprime
);
4456 tree vdef
= gimple_vdef (stmt
);
4457 tree vuse
= gimple_vuse (stmt
);
4458 propagate_tree_value_into_stmt (&gsi
, sprime
);
4459 stmt
= gsi_stmt (gsi
);
4461 if (vdef
!= gimple_vdef (stmt
))
4462 VN_INFO (vdef
)->valnum
= vuse
;
4464 /* If we removed EH side-effects from the statement, clean
4465 its EH information. */
4466 if (maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
))
4468 bitmap_set_bit (need_eh_cleanup
,
4469 gimple_bb (stmt
)->index
);
4470 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4471 fprintf (dump_file
, " Removed EH side-effects.\n");
4474 /* Likewise for AB side-effects. */
4475 if (can_make_abnormal_goto
4476 && !stmt_can_make_abnormal_goto (stmt
))
4478 bitmap_set_bit (need_ab_cleanup
,
4479 gimple_bb (stmt
)->index
);
4480 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4481 fprintf (dump_file
, " Removed AB side-effects.\n");
4488 /* If the statement is a scalar store, see if the expression
4489 has the same value number as its rhs. If so, the store is
4491 if (gimple_assign_single_p (stmt
)
4492 && !gimple_has_volatile_ops (stmt
)
4493 && !is_gimple_reg (gimple_assign_lhs (stmt
))
4494 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4495 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
))))
4498 tree rhs
= gimple_assign_rhs1 (stmt
);
4499 vn_reference_t vnresult
;
4500 val
= vn_reference_lookup (lhs
, gimple_vuse (stmt
), VN_WALKREWRITE
,
4502 if (TREE_CODE (rhs
) == SSA_NAME
)
4503 rhs
= VN_INFO (rhs
)->valnum
;
4505 && operand_equal_p (val
, rhs
, 0))
4507 /* We can only remove the later store if the former aliases
4508 at least all accesses the later one does or if the store
4509 was to readonly memory storing the same value. */
4510 alias_set_type set
= get_alias_set (lhs
);
4512 || vnresult
->set
== set
4513 || alias_set_subset_of (set
, vnresult
->set
))
4515 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4517 fprintf (dump_file
, "Deleted redundant store ");
4518 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4521 /* Queue stmt for removal. */
4522 el_to_remove
.safe_push (stmt
);
4528 /* If this is a control statement value numbering left edges
4529 unexecuted on force the condition in a way consistent with
4531 if (gcond
*cond
= dyn_cast
<gcond
*> (stmt
))
4533 if ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
)
4534 ^ (EDGE_SUCC (b
, 1)->flags
& EDGE_EXECUTABLE
))
4536 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4538 fprintf (dump_file
, "Removing unexecutable edge from ");
4539 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4541 if (((EDGE_SUCC (b
, 0)->flags
& EDGE_TRUE_VALUE
) != 0)
4542 == ((EDGE_SUCC (b
, 0)->flags
& EDGE_EXECUTABLE
) != 0))
4543 gimple_cond_make_true (cond
);
4545 gimple_cond_make_false (cond
);
4547 el_todo
|= TODO_cleanup_cfg
;
4552 bool can_make_abnormal_goto
= stmt_can_make_abnormal_goto (stmt
);
4553 bool was_noreturn
= (is_gimple_call (stmt
)
4554 && gimple_call_noreturn_p (stmt
));
4555 tree vdef
= gimple_vdef (stmt
);
4556 tree vuse
= gimple_vuse (stmt
);
4558 /* If we didn't replace the whole stmt (or propagate the result
4559 into all uses), replace all uses on this stmt with their
4561 use_operand_p use_p
;
4563 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
4565 tree use
= USE_FROM_PTR (use_p
);
4566 /* ??? The call code above leaves stmt operands un-updated. */
4567 if (TREE_CODE (use
) != SSA_NAME
)
4569 tree sprime
= eliminate_avail (use
);
4570 if (sprime
&& sprime
!= use
4571 && may_propagate_copy (use
, sprime
)
4572 /* We substitute into debug stmts to avoid excessive
4573 debug temporaries created by removed stmts, but we need
4574 to avoid doing so for inserted sprimes as we never want
4575 to create debug temporaries for them. */
4577 || TREE_CODE (sprime
) != SSA_NAME
4578 || !is_gimple_debug (stmt
)
4579 || !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))))
4581 propagate_value (use_p
, sprime
);
4582 gimple_set_modified (stmt
, true);
4583 if (TREE_CODE (sprime
) == SSA_NAME
4584 && !is_gimple_debug (stmt
))
4585 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4590 /* Visit indirect calls and turn them into direct calls if
4591 possible using the devirtualization machinery. */
4592 if (gcall
*call_stmt
= dyn_cast
<gcall
*> (stmt
))
4594 tree fn
= gimple_call_fn (call_stmt
);
4596 && flag_devirtualize
4597 && virtual_method_call_p (fn
))
4599 tree otr_type
= obj_type_ref_class (fn
);
4601 ipa_polymorphic_call_context
context (current_function_decl
, fn
, stmt
, &instance
);
4604 context
.get_dynamic_type (instance
, OBJ_TYPE_REF_OBJECT (fn
), otr_type
, stmt
);
4606 vec
<cgraph_node
*>targets
4607 = possible_polymorphic_call_targets (obj_type_ref_class (fn
),
4609 (OBJ_TYPE_REF_TOKEN (fn
)),
4613 dump_possible_polymorphic_call_targets (dump_file
,
4614 obj_type_ref_class (fn
),
4616 (OBJ_TYPE_REF_TOKEN (fn
)),
4618 if (final
&& targets
.length () <= 1 && dbg_cnt (devirt
))
4621 if (targets
.length () == 1)
4622 fn
= targets
[0]->decl
;
4624 fn
= builtin_decl_implicit (BUILT_IN_UNREACHABLE
);
4625 if (dump_enabled_p ())
4627 location_t loc
= gimple_location_safe (stmt
);
4628 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
4629 "converting indirect call to "
4631 lang_hooks
.decl_printable_name (fn
, 2));
4633 gimple_call_set_fndecl (call_stmt
, fn
);
4634 /* If changing the call to __builtin_unreachable
4635 or similar noreturn function, adjust gimple_call_fntype
4637 if (gimple_call_noreturn_p (call_stmt
)
4638 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn
)))
4639 && TYPE_ARG_TYPES (TREE_TYPE (fn
))
4640 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn
)))
4642 gimple_call_set_fntype (call_stmt
, TREE_TYPE (fn
));
4643 maybe_remove_unused_call_args (cfun
, call_stmt
);
4644 gimple_set_modified (stmt
, true);
4649 if (gimple_modified_p (stmt
))
4651 /* If a formerly non-invariant ADDR_EXPR is turned into an
4652 invariant one it was on a separate stmt. */
4653 if (gimple_assign_single_p (stmt
)
4654 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
4655 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
4656 gimple
*old_stmt
= stmt
;
4657 if (is_gimple_call (stmt
))
4659 /* ??? Only fold calls inplace for now, this may create new
4660 SSA names which in turn will confuse free_scc_vn SSA name
4662 fold_stmt_inplace (&gsi
);
4663 /* When changing a call into a noreturn call, cfg cleanup
4664 is needed to fix up the noreturn call. */
4665 if (!was_noreturn
&& gimple_call_noreturn_p (stmt
))
4666 el_to_fixup
.safe_push (stmt
);
4671 stmt
= gsi_stmt (gsi
);
4672 if ((gimple_code (stmt
) == GIMPLE_COND
4673 && (gimple_cond_true_p (as_a
<gcond
*> (stmt
))
4674 || gimple_cond_false_p (as_a
<gcond
*> (stmt
))))
4675 || (gimple_code (stmt
) == GIMPLE_SWITCH
4676 && TREE_CODE (gimple_switch_index (
4677 as_a
<gswitch
*> (stmt
)))
4679 el_todo
|= TODO_cleanup_cfg
;
4681 /* If we removed EH side-effects from the statement, clean
4682 its EH information. */
4683 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
4685 bitmap_set_bit (need_eh_cleanup
,
4686 gimple_bb (stmt
)->index
);
4687 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4688 fprintf (dump_file
, " Removed EH side-effects.\n");
4690 /* Likewise for AB side-effects. */
4691 if (can_make_abnormal_goto
4692 && !stmt_can_make_abnormal_goto (stmt
))
4694 bitmap_set_bit (need_ab_cleanup
,
4695 gimple_bb (stmt
)->index
);
4696 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4697 fprintf (dump_file
, " Removed AB side-effects.\n");
4700 if (vdef
!= gimple_vdef (stmt
))
4701 VN_INFO (vdef
)->valnum
= vuse
;
4704 /* Make new values available - for fully redundant LHS we
4705 continue with the next stmt above and skip this. */
4707 FOR_EACH_SSA_DEF_OPERAND (defp
, stmt
, iter
, SSA_OP_DEF
)
4708 eliminate_push_avail (DEF_FROM_PTR (defp
));
4711 /* Replace destination PHI arguments. */
4714 FOR_EACH_EDGE (e
, ei
, b
->succs
)
4716 for (gphi_iterator gsi
= gsi_start_phis (e
->dest
);
4720 gphi
*phi
= gsi
.phi ();
4721 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
4722 tree arg
= USE_FROM_PTR (use_p
);
4723 if (TREE_CODE (arg
) != SSA_NAME
4724 || virtual_operand_p (arg
))
4726 tree sprime
= eliminate_avail (arg
);
4727 if (sprime
&& may_propagate_copy (arg
, sprime
))
4729 propagate_value (use_p
, sprime
);
4730 if (TREE_CODE (sprime
) == SSA_NAME
)
4731 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
4738 /* Make no longer available leaders no longer available. */
4741 eliminate_dom_walker::after_dom_children (basic_block
)
4744 while ((entry
= el_avail_stack
.pop ()) != NULL_TREE
)
4746 tree valnum
= VN_INFO (entry
)->valnum
;
4747 tree old
= el_avail
[SSA_NAME_VERSION (valnum
)];
4749 el_avail
[SSA_NAME_VERSION (valnum
)] = NULL_TREE
;
4751 el_avail
[SSA_NAME_VERSION (valnum
)] = entry
;
4755 /* Eliminate fully redundant computations. */
4758 eliminate (bool do_pre
)
4760 gimple_stmt_iterator gsi
;
4763 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
4764 need_ab_cleanup
= BITMAP_ALLOC (NULL
);
4766 el_to_remove
.create (0);
4767 el_to_fixup
.create (0);
4769 el_avail
.create (num_ssa_names
);
4770 el_avail_stack
.create (0);
4772 eliminate_dom_walker (CDI_DOMINATORS
,
4773 do_pre
).walk (cfun
->cfg
->x_entry_block_ptr
);
4775 el_avail
.release ();
4776 el_avail_stack
.release ();
4778 /* We cannot remove stmts during BB walk, especially not release SSA
4779 names there as this confuses the VN machinery. The stmts ending
4780 up in el_to_remove are either stores or simple copies.
4781 Remove stmts in reverse order to make debug stmt creation possible. */
4782 while (!el_to_remove
.is_empty ())
4784 stmt
= el_to_remove
.pop ();
4786 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4788 fprintf (dump_file
, "Removing dead stmt ");
4789 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4793 if (gimple_code (stmt
) == GIMPLE_PHI
)
4794 lhs
= gimple_phi_result (stmt
);
4796 lhs
= gimple_get_lhs (stmt
);
4799 && TREE_CODE (lhs
) == SSA_NAME
)
4800 bitmap_clear_bit (inserted_exprs
, SSA_NAME_VERSION (lhs
));
4802 gsi
= gsi_for_stmt (stmt
);
4803 if (gimple_code (stmt
) == GIMPLE_PHI
)
4804 remove_phi_node (&gsi
, true);
4807 basic_block bb
= gimple_bb (stmt
);
4808 unlink_stmt_vdef (stmt
);
4809 if (gsi_remove (&gsi
, true))
4810 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
4811 if (is_gimple_call (stmt
) && stmt_can_make_abnormal_goto (stmt
))
4812 bitmap_set_bit (need_ab_cleanup
, bb
->index
);
4813 release_defs (stmt
);
4816 /* Removing a stmt may expose a forwarder block. */
4817 el_todo
|= TODO_cleanup_cfg
;
4819 el_to_remove
.release ();
4821 /* Fixup stmts that became noreturn calls. This may require splitting
4822 blocks and thus isn't possible during the dominator walk. Do this
4823 in reverse order so we don't inadvertedly remove a stmt we want to
4824 fixup by visiting a dominating now noreturn call first. */
4825 while (!el_to_fixup
.is_empty ())
4827 stmt
= el_to_fixup
.pop ();
4829 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4831 fprintf (dump_file
, "Fixing up noreturn call ");
4832 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4835 if (fixup_noreturn_call (stmt
))
4836 el_todo
|= TODO_cleanup_cfg
;
4838 el_to_fixup
.release ();
4843 /* Perform CFG cleanups made necessary by elimination. */
4846 fini_eliminate (void)
4848 bool do_eh_cleanup
= !bitmap_empty_p (need_eh_cleanup
);
4849 bool do_ab_cleanup
= !bitmap_empty_p (need_ab_cleanup
);
4852 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
4855 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup
);
4857 BITMAP_FREE (need_eh_cleanup
);
4858 BITMAP_FREE (need_ab_cleanup
);
4860 if (do_eh_cleanup
|| do_ab_cleanup
)
4861 return TODO_cleanup_cfg
;
4865 /* Borrow a bit of tree-ssa-dce.c for the moment.
4866 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4867 this may be a bit faster, and we may want critical edges kept split. */
4869 /* If OP's defining statement has not already been determined to be necessary,
4870 mark that statement necessary. Return the stmt, if it is newly
4873 static inline gimple
*
4874 mark_operand_necessary (tree op
)
4880 if (TREE_CODE (op
) != SSA_NAME
)
4883 stmt
= SSA_NAME_DEF_STMT (op
);
4886 if (gimple_plf (stmt
, NECESSARY
)
4887 || gimple_nop_p (stmt
))
4890 gimple_set_plf (stmt
, NECESSARY
, true);
4894 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4895 to insert PHI nodes sometimes, and because value numbering of casts isn't
4896 perfect, we sometimes end up inserting dead code. This simple DCE-like
4897 pass removes any insertions we made that weren't actually used. */
4900 remove_dead_inserted_code (void)
4907 worklist
= BITMAP_ALLOC (NULL
);
4908 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4910 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4911 if (gimple_plf (t
, NECESSARY
))
4912 bitmap_set_bit (worklist
, i
);
4914 while (!bitmap_empty_p (worklist
))
4916 i
= bitmap_first_set_bit (worklist
);
4917 bitmap_clear_bit (worklist
, i
);
4918 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4920 /* PHI nodes are somewhat special in that each PHI alternative has
4921 data and control dependencies. All the statements feeding the
4922 PHI node's arguments are always necessary. */
4923 if (gimple_code (t
) == GIMPLE_PHI
)
4927 for (k
= 0; k
< gimple_phi_num_args (t
); k
++)
4929 tree arg
= PHI_ARG_DEF (t
, k
);
4930 if (TREE_CODE (arg
) == SSA_NAME
)
4932 gimple
*n
= mark_operand_necessary (arg
);
4934 bitmap_set_bit (worklist
, SSA_NAME_VERSION (arg
));
4940 /* Propagate through the operands. Examine all the USE, VUSE and
4941 VDEF operands in this statement. Mark all the statements
4942 which feed this statement's uses as necessary. */
4946 /* The operands of VDEF expressions are also needed as they
4947 represent potential definitions that may reach this
4948 statement (VDEF operands allow us to follow def-def
4951 FOR_EACH_SSA_TREE_OPERAND (use
, t
, iter
, SSA_OP_ALL_USES
)
4953 gimple
*n
= mark_operand_necessary (use
);
4955 bitmap_set_bit (worklist
, SSA_NAME_VERSION (use
));
4960 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4962 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4963 if (!gimple_plf (t
, NECESSARY
))
4965 gimple_stmt_iterator gsi
;
4967 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4969 fprintf (dump_file
, "Removing unnecessary insertion:");
4970 print_gimple_stmt (dump_file
, t
, 0, 0);
4973 gsi
= gsi_for_stmt (t
);
4974 if (gimple_code (t
) == GIMPLE_PHI
)
4975 remove_phi_node (&gsi
, true);
4978 gsi_remove (&gsi
, true);
4983 BITMAP_FREE (worklist
);
4987 /* Initialize data structures used by PRE. */
4994 next_expression_id
= 1;
4995 expressions
.create (0);
4996 expressions
.safe_push (NULL
);
4997 value_expressions
.create (get_max_value_id () + 1);
4998 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
4999 name_to_id
.create (0);
5001 inserted_exprs
= BITMAP_ALLOC (NULL
);
5003 connect_infinite_loops_to_exit ();
5004 memset (&pre_stats
, 0, sizeof (pre_stats
));
5006 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets
));
5008 calculate_dominance_info (CDI_DOMINATORS
);
5010 bitmap_obstack_initialize (&grand_bitmap_obstack
);
5011 phi_translate_table
= new hash_table
<expr_pred_trans_d
> (5110);
5012 expression_to_id
= new hash_table
<pre_expr_d
> (num_ssa_names
* 3);
5013 FOR_ALL_BB_FN (bb
, cfun
)
5015 EXP_GEN (bb
) = bitmap_set_new ();
5016 PHI_GEN (bb
) = bitmap_set_new ();
5017 TMP_GEN (bb
) = bitmap_set_new ();
5018 AVAIL_OUT (bb
) = bitmap_set_new ();
5023 /* Deallocate data structures used by PRE. */
5028 value_expressions
.release ();
5029 BITMAP_FREE (inserted_exprs
);
5030 bitmap_obstack_release (&grand_bitmap_obstack
);
5031 bitmap_set_pool
.release ();
5032 pre_expr_pool
.release ();
5033 delete phi_translate_table
;
5034 phi_translate_table
= NULL
;
5035 delete expression_to_id
;
5036 expression_to_id
= NULL
;
5037 name_to_id
.release ();
5039 free_aux_for_blocks ();
5044 const pass_data pass_data_pre
=
5046 GIMPLE_PASS
, /* type */
5048 OPTGROUP_NONE
, /* optinfo_flags */
5049 TV_TREE_PRE
, /* tv_id */
5050 /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before
5052 ( PROP_no_crit_edges
| PROP_cfg
| PROP_ssa
), /* properties_required */
5053 0, /* properties_provided */
5054 PROP_no_crit_edges
, /* properties_destroyed */
5055 TODO_rebuild_alias
, /* todo_flags_start */
5056 0, /* todo_flags_finish */
5059 class pass_pre
: public gimple_opt_pass
5062 pass_pre (gcc::context
*ctxt
)
5063 : gimple_opt_pass (pass_data_pre
, ctxt
)
5066 /* opt_pass methods: */
5067 virtual bool gate (function
*)
5068 { return flag_tree_pre
!= 0 || flag_code_hoisting
!= 0; }
5069 virtual unsigned int execute (function
*);
5071 }; // class pass_pre
5074 pass_pre::execute (function
*fun
)
5076 unsigned int todo
= 0;
5078 do_partial_partial
=
5079 flag_tree_partial_pre
&& optimize_function_for_speed_p (fun
);
5081 /* This has to happen before SCCVN runs because
5082 loop_optimizer_init may create new phis, etc. */
5083 loop_optimizer_init (LOOPS_NORMAL
);
5085 if (!run_scc_vn (VN_WALK
))
5087 loop_optimizer_finalize ();
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 if (!run_scc_vn (VN_WALKREWRITE
))
5204 memset (&pre_stats
, 0, sizeof (pre_stats
));
5206 /* Remove all the redundant expressions. */
5207 todo
|= eliminate (false);
5209 todo
|= fini_eliminate ();
5211 scc_vn_restore_ssa_info ();
5214 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
5215 statistics_counter_event (fun
, "Eliminated", pre_stats
.eliminations
);
5223 make_pass_fre (gcc::context
*ctxt
)
5225 return new pass_fre (ctxt
);