1 /* Full and partial redundancy elimination and code hoisting on SSA GIMPLE.
2 Copyright (C) 2001-2022 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
30 #include "alloc-pool.h"
31 #include "tree-pass.h"
34 #include "gimple-pretty-print.h"
35 #include "fold-const.h"
37 #include "gimple-fold.h"
40 #include "gimple-iterator.h"
42 #include "tree-into-ssa.h"
46 #include "tree-ssa-sccvn.h"
47 #include "tree-scalar-evolution.h"
50 #include "tree-ssa-propagate.h"
51 #include "tree-ssa-dce.h"
52 #include "tree-cfgcleanup.h"
54 #include "gimple-range.h"
56 /* Even though this file is called tree-ssa-pre.cc, we actually
57 implement a bit more than just PRE here. All of them piggy-back
58 on GVN which is implemented in tree-ssa-sccvn.cc.
60 1. Full Redundancy Elimination (FRE)
61 This is the elimination phase of GVN.
63 2. Partial Redundancy Elimination (PRE)
64 This is adds computation of AVAIL_OUT and ANTIC_IN and
65 doing expression insertion to form GVN-PRE.
68 This optimization uses the ANTIC_IN sets computed for PRE
69 to move expressions further up than PRE would do, to make
70 multiple computations of the same value fully redundant.
71 This pass is explained below (after the explanation of the
72 basic algorithm for PRE).
77 1. Avail sets can be shared by making an avail_find_leader that
78 walks up the dominator tree and looks in those avail sets.
79 This might affect code optimality, it's unclear right now.
80 Currently the AVAIL_OUT sets are the remaining quadraticness in
82 2. Strength reduction can be performed by anticipating expressions
83 we can repair later on.
84 3. We can do back-substitution or smarter value numbering to catch
85 commutative expressions split up over multiple statements.
88 /* For ease of terminology, "expression node" in the below refers to
89 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
90 represent the actual statement containing the expressions we care about,
91 and we cache the value number by putting it in the expression. */
93 /* Basic algorithm for Partial Redundancy Elimination:
95 First we walk the statements to generate the AVAIL sets, the
96 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
97 generation of values/expressions by a given block. We use them
98 when computing the ANTIC sets. The AVAIL sets consist of
99 SSA_NAME's that represent values, so we know what values are
100 available in what blocks. AVAIL is a forward dataflow problem. In
101 SSA, values are never killed, so we don't need a kill set, or a
102 fixpoint iteration, in order to calculate the AVAIL sets. In
103 traditional parlance, AVAIL sets tell us the downsafety of the
106 Next, we generate the ANTIC sets. These sets represent the
107 anticipatable expressions. ANTIC is a backwards dataflow
108 problem. An expression is anticipatable in a given block if it could
109 be generated in that block. This means that if we had to perform
110 an insertion in that block, of the value of that expression, we
111 could. Calculating the ANTIC sets requires phi translation of
112 expressions, because the flow goes backwards through phis. We must
113 iterate to a fixpoint of the ANTIC sets, because we have a kill
114 set. Even in SSA form, values are not live over the entire
115 function, only from their definition point onwards. So we have to
116 remove values from the ANTIC set once we go past the definition
117 point of the leaders that make them up.
118 compute_antic/compute_antic_aux performs this computation.
120 Third, we perform insertions to make partially redundant
121 expressions fully redundant.
123 An expression is partially redundant (excluding partial
126 1. It is AVAIL in some, but not all, of the predecessors of a
128 2. It is ANTIC in all the predecessors.
130 In order to make it fully redundant, we insert the expression into
131 the predecessors where it is not available, but is ANTIC.
133 When optimizing for size, we only eliminate the partial redundancy
134 if we need to insert in only one predecessor. This avoids almost
135 completely the code size increase that PRE usually causes.
137 For the partial anticipation case, we only perform insertion if it
138 is partially anticipated in some block, and fully available in all
141 do_pre_regular_insertion/do_pre_partial_partial_insertion
142 performs these steps, driven by insert/insert_aux.
144 Fourth, we eliminate fully redundant expressions.
145 This is a simple statement walk that replaces redundant
146 calculations with the now available values. */
148 /* Basic algorithm for Code Hoisting:
150 Code hoisting is: Moving value computations up in the control flow
151 graph to make multiple copies redundant. Typically this is a size
152 optimization, but there are cases where it also is helpful for speed.
154 A simple code hoisting algorithm is implemented that piggy-backs on
155 the PRE infrastructure. For code hoisting, we have to know ANTIC_OUT
156 which is effectively ANTIC_IN - AVAIL_OUT. The latter two have to be
157 computed for PRE, and we can use them to perform a limited version of
160 For the purpose of this implementation, a value is hoistable to a basic
161 block B if the following properties are met:
163 1. The value is in ANTIC_IN(B) -- the value will be computed on all
164 paths from B to function exit and it can be computed in B);
166 2. The value is not in AVAIL_OUT(B) -- there would be no need to
167 compute the value again and make it available twice;
169 3. All successors of B are dominated by B -- makes sure that inserting
170 a computation of the value in B will make the remaining
171 computations fully redundant;
173 4. At least one successor has the value in AVAIL_OUT -- to avoid
174 hoisting values up too far;
176 5. There are at least two successors of B -- hoisting in straight
177 line code is pointless.
179 The third condition is not strictly necessary, but it would complicate
180 the hoisting pass a lot. In fact, I don't know of any code hoisting
181 algorithm that does not have this requirement. Fortunately, experiments
182 have show that most candidate hoistable values are in regions that meet
183 this condition (e.g. diamond-shape regions).
185 The forth condition is necessary to avoid hoisting things up too far
186 away from the uses of the value. Nothing else limits the algorithm
187 from hoisting everything up as far as ANTIC_IN allows. Experiments
188 with SPEC and CSiBE have shown that hoisting up too far results in more
189 spilling, less benefits for code size, and worse benchmark scores.
190 Fortunately, in practice most of the interesting hoisting opportunities
191 are caught despite this limitation.
193 For hoistable values that meet all conditions, expressions are inserted
194 to make the calculation of the hoistable value fully redundant. We
195 perform code hoisting insertions after each round of PRE insertions,
196 because code hoisting never exposes new PRE opportunities, but PRE can
197 create new code hoisting opportunities.
199 The code hoisting algorithm is implemented in do_hoist_insert, driven
200 by insert/insert_aux. */
202 /* Representations of value numbers:
204 Value numbers are represented by a representative SSA_NAME. We
205 will create fake SSA_NAME's in situations where we need a
206 representative but do not have one (because it is a complex
207 expression). In order to facilitate storing the value numbers in
208 bitmaps, and keep the number of wasted SSA_NAME's down, we also
209 associate a value_id with each value number, and create full blown
210 ssa_name's only where we actually need them (IE in operands of
211 existing expressions).
213 Theoretically you could replace all the value_id's with
214 SSA_NAME_VERSION, but this would allocate a large number of
215 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
216 It would also require an additional indirection at each point we
219 /* Representation of expressions on value numbers:
221 Expressions consisting of value numbers are represented the same
222 way as our VN internally represents them, with an additional
223 "pre_expr" wrapping around them in order to facilitate storing all
224 of the expressions in the same sets. */
226 /* Representation of sets:
228 The dataflow sets do not need to be sorted in any particular order
229 for the majority of their lifetime, are simply represented as two
230 bitmaps, one that keeps track of values present in the set, and one
231 that keeps track of expressions present in the set.
233 When we need them in topological order, we produce it on demand by
234 transforming the bitmap into an array and sorting it into topo
237 /* Type of expression, used to know which member of the PRE_EXPR union
253 vn_reference_t reference
;
256 typedef struct pre_expr_d
: nofree_ptr_hash
<pre_expr_d
>
258 enum pre_expr_kind kind
;
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
;
326 static obstack pre_expr_obstack
;
328 /* Allocate an expression id for EXPR. */
330 static inline unsigned int
331 alloc_expression_id (pre_expr expr
)
333 struct pre_expr_d
**slot
;
334 /* Make sure we won't overflow. */
335 gcc_assert (next_expression_id
+ 1 > next_expression_id
);
336 expr
->id
= next_expression_id
++;
337 expressions
.safe_push (expr
);
338 if (expr
->kind
== NAME
)
340 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
341 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
342 re-allocations by using vec::reserve upfront. */
343 unsigned old_len
= name_to_id
.length ();
344 name_to_id
.reserve (num_ssa_names
- old_len
);
345 name_to_id
.quick_grow_cleared (num_ssa_names
);
346 gcc_assert (name_to_id
[version
] == 0);
347 name_to_id
[version
] = expr
->id
;
351 slot
= expression_to_id
->find_slot (expr
, INSERT
);
355 return next_expression_id
- 1;
358 /* Return the expression id for tree EXPR. */
360 static inline unsigned int
361 get_expression_id (const pre_expr expr
)
366 static inline unsigned int
367 lookup_expression_id (const pre_expr expr
)
369 struct pre_expr_d
**slot
;
371 if (expr
->kind
== NAME
)
373 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
374 if (name_to_id
.length () <= version
)
376 return name_to_id
[version
];
380 slot
= expression_to_id
->find_slot (expr
, NO_INSERT
);
383 return ((pre_expr
)*slot
)->id
;
387 /* Return the existing expression id for EXPR, or create one if one
388 does not exist yet. */
390 static inline unsigned int
391 get_or_alloc_expression_id (pre_expr expr
)
393 unsigned int id
= lookup_expression_id (expr
);
395 return alloc_expression_id (expr
);
396 return expr
->id
= id
;
399 /* Return the expression that has expression id ID */
401 static inline pre_expr
402 expression_for_id (unsigned int id
)
404 return expressions
[id
];
407 static object_allocator
<pre_expr_d
> pre_expr_pool ("pre_expr nodes");
409 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
412 get_or_alloc_expr_for_name (tree name
)
414 struct pre_expr_d expr
;
416 unsigned int result_id
;
420 PRE_EXPR_NAME (&expr
) = name
;
421 result_id
= lookup_expression_id (&expr
);
423 return expression_for_id (result_id
);
425 result
= pre_expr_pool
.allocate ();
427 result
->loc
= UNKNOWN_LOCATION
;
428 result
->value_id
= VN_INFO (name
)->value_id
;
429 PRE_EXPR_NAME (result
) = name
;
430 alloc_expression_id (result
);
434 /* Given an NARY, get or create a pre_expr to represent it. Assign
435 VALUE_ID to it or allocate a new value-id if it is zero. Record
436 LOC as the original location of the expression. */
439 get_or_alloc_expr_for_nary (vn_nary_op_t nary
, unsigned value_id
,
440 location_t loc
= UNKNOWN_LOCATION
)
442 struct pre_expr_d expr
;
444 unsigned int result_id
;
446 gcc_assert (value_id
== 0 || !value_id_constant_p (value_id
));
450 nary
->hashcode
= vn_nary_op_compute_hash (nary
);
451 PRE_EXPR_NARY (&expr
) = nary
;
452 result_id
= lookup_expression_id (&expr
);
454 return expression_for_id (result_id
);
456 result
= pre_expr_pool
.allocate ();
459 result
->value_id
= value_id
? value_id
: get_next_value_id ();
460 PRE_EXPR_NARY (result
)
461 = alloc_vn_nary_op_noinit (nary
->length
, &pre_expr_obstack
);
462 memcpy (PRE_EXPR_NARY (result
), nary
, sizeof_vn_nary_op (nary
->length
));
463 alloc_expression_id (result
);
467 /* Given an REFERENCE, get or create a pre_expr to represent it. */
470 get_or_alloc_expr_for_reference (vn_reference_t reference
,
471 location_t loc
= UNKNOWN_LOCATION
)
473 struct pre_expr_d expr
;
475 unsigned int result_id
;
477 expr
.kind
= REFERENCE
;
479 PRE_EXPR_REFERENCE (&expr
) = reference
;
480 result_id
= lookup_expression_id (&expr
);
482 return expression_for_id (result_id
);
484 result
= pre_expr_pool
.allocate ();
485 result
->kind
= REFERENCE
;
487 result
->value_id
= reference
->value_id
;
488 PRE_EXPR_REFERENCE (result
) = reference
;
489 alloc_expression_id (result
);
494 /* An unordered bitmap set. One bitmap tracks values, the other,
496 typedef class bitmap_set
499 bitmap_head expressions
;
503 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
504 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
506 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
507 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
509 /* Mapping from value id to expressions with that value_id. */
510 static vec
<bitmap
> value_expressions
;
511 /* We just record a single expression for each constant value,
512 one of kind CONSTANT. */
513 static vec
<pre_expr
> constant_value_expressions
;
516 /* This structure is used to keep track of statistics on what
517 optimization PRE was able to perform. */
520 /* The number of new expressions/temporaries generated by PRE. */
523 /* The number of inserts found due to partial anticipation */
526 /* The number of inserts made for code hoisting. */
529 /* The number of new PHI nodes added by PRE. */
533 static bool do_partial_partial
;
534 static pre_expr
bitmap_find_leader (bitmap_set_t
, unsigned int);
535 static void bitmap_value_insert_into_set (bitmap_set_t
, pre_expr
);
536 static bool bitmap_value_replace_in_set (bitmap_set_t
, pre_expr
);
537 static void bitmap_set_copy (bitmap_set_t
, bitmap_set_t
);
538 static bool bitmap_set_contains_value (bitmap_set_t
, unsigned int);
539 static void bitmap_insert_into_set (bitmap_set_t
, pre_expr
);
540 static bitmap_set_t
bitmap_set_new (void);
541 static tree
create_expression_by_pieces (basic_block
, pre_expr
, gimple_seq
*,
543 static tree
find_or_generate_expression (basic_block
, tree
, gimple_seq
*);
544 static unsigned int get_expr_value_id (pre_expr
);
546 /* We can add and remove elements and entries to and from sets
547 and hash tables, so we use alloc pools for them. */
549 static object_allocator
<bitmap_set
> bitmap_set_pool ("Bitmap sets");
550 static bitmap_obstack grand_bitmap_obstack
;
552 /* A three tuple {e, pred, v} used to cache phi translations in the
553 phi_translate_table. */
555 typedef struct expr_pred_trans_d
: public typed_noop_remove
<expr_pred_trans_d
>
557 typedef expr_pred_trans_d value_type
;
558 typedef expr_pred_trans_d compare_type
;
560 /* The expression ID. */
563 /* The value expression ID that resulted from the translation. */
566 /* hash_table support. */
567 static inline void mark_empty (expr_pred_trans_d
&);
568 static inline bool is_empty (const expr_pred_trans_d
&);
569 static inline void mark_deleted (expr_pred_trans_d
&);
570 static inline bool is_deleted (const expr_pred_trans_d
&);
571 static const bool empty_zero_p
= true;
572 static inline hashval_t
hash (const expr_pred_trans_d
&);
573 static inline int equal (const expr_pred_trans_d
&, const expr_pred_trans_d
&);
574 } *expr_pred_trans_t
;
575 typedef const struct expr_pred_trans_d
*const_expr_pred_trans_t
;
578 expr_pred_trans_d::is_empty (const expr_pred_trans_d
&e
)
584 expr_pred_trans_d::is_deleted (const expr_pred_trans_d
&e
)
590 expr_pred_trans_d::mark_empty (expr_pred_trans_d
&e
)
596 expr_pred_trans_d::mark_deleted (expr_pred_trans_d
&e
)
602 expr_pred_trans_d::hash (const expr_pred_trans_d
&e
)
608 expr_pred_trans_d::equal (const expr_pred_trans_d
&ve1
,
609 const expr_pred_trans_d
&ve2
)
611 return ve1
.e
== ve2
.e
;
614 /* Sets that we need to keep track of. */
615 typedef struct bb_bitmap_sets
617 /* The EXP_GEN set, which represents expressions/values generated in
619 bitmap_set_t exp_gen
;
621 /* The PHI_GEN set, which represents PHI results generated in a
623 bitmap_set_t phi_gen
;
625 /* The TMP_GEN set, which represents results/temporaries generated
626 in a basic block. IE the LHS of an expression. */
627 bitmap_set_t tmp_gen
;
629 /* The AVAIL_OUT set, which represents which values are available in
630 a given basic block. */
631 bitmap_set_t avail_out
;
633 /* The ANTIC_IN set, which represents which values are anticipatable
634 in a given basic block. */
635 bitmap_set_t antic_in
;
637 /* The PA_IN set, which represents which values are
638 partially anticipatable in a given basic block. */
641 /* The NEW_SETS set, which is used during insertion to augment the
642 AVAIL_OUT set of blocks with the new insertions performed during
643 the current iteration. */
644 bitmap_set_t new_sets
;
646 /* A cache for value_dies_in_block_x. */
649 /* The live virtual operand on successor edges. */
652 /* PHI translate cache for the single successor edge. */
653 hash_table
<expr_pred_trans_d
> *phi_translate_table
;
655 /* True if we have visited this block during ANTIC calculation. */
656 unsigned int visited
: 1;
658 /* True when the block contains a call that might not return. */
659 unsigned int contains_may_not_return_call
: 1;
662 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
663 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
664 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
665 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
666 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
667 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
668 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
669 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
670 #define PHI_TRANS_TABLE(BB) ((bb_value_sets_t) ((BB)->aux))->phi_translate_table
671 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
672 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
673 #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
676 /* Add the tuple mapping from {expression E, basic block PRED} to
677 the phi translation table and return whether it pre-existed. */
680 phi_trans_add (expr_pred_trans_t
*entry
, pre_expr e
, basic_block pred
)
682 if (!PHI_TRANS_TABLE (pred
))
683 PHI_TRANS_TABLE (pred
) = new hash_table
<expr_pred_trans_d
> (11);
685 expr_pred_trans_t slot
;
686 expr_pred_trans_d tem
;
687 unsigned id
= get_expression_id (e
);
689 slot
= PHI_TRANS_TABLE (pred
)->find_slot_with_hash (tem
, id
, INSERT
);
702 /* Add expression E to the expression set of value id V. */
705 add_to_value (unsigned int v
, pre_expr e
)
707 gcc_checking_assert (get_expr_value_id (e
) == v
);
709 if (value_id_constant_p (v
))
711 if (e
->kind
!= CONSTANT
)
714 if (-v
>= constant_value_expressions
.length ())
715 constant_value_expressions
.safe_grow_cleared (-v
+ 1);
717 pre_expr leader
= constant_value_expressions
[-v
];
719 constant_value_expressions
[-v
] = e
;
723 if (v
>= value_expressions
.length ())
724 value_expressions
.safe_grow_cleared (v
+ 1);
726 bitmap set
= value_expressions
[v
];
729 set
= BITMAP_ALLOC (&grand_bitmap_obstack
);
730 value_expressions
[v
] = set
;
732 bitmap_set_bit (set
, get_or_alloc_expression_id (e
));
736 /* Create a new bitmap set and return it. */
739 bitmap_set_new (void)
741 bitmap_set_t ret
= bitmap_set_pool
.allocate ();
742 bitmap_initialize (&ret
->expressions
, &grand_bitmap_obstack
);
743 bitmap_initialize (&ret
->values
, &grand_bitmap_obstack
);
747 /* Return the value id for a PRE expression EXPR. */
750 get_expr_value_id (pre_expr expr
)
752 /* ??? We cannot assert that expr has a value-id (it can be 0), because
753 we assign value-ids only to expressions that have a result
754 in set_hashtable_value_ids. */
755 return expr
->value_id
;
758 /* Return a VN valnum (SSA name or constant) for the PRE value-id VAL. */
761 vn_valnum_from_value_id (unsigned int val
)
763 if (value_id_constant_p (val
))
765 pre_expr vexpr
= constant_value_expressions
[-val
];
767 return PRE_EXPR_CONSTANT (vexpr
);
771 bitmap exprset
= value_expressions
[val
];
774 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
776 pre_expr vexpr
= expression_for_id (i
);
777 if (vexpr
->kind
== NAME
)
778 return VN_INFO (PRE_EXPR_NAME (vexpr
))->valnum
;
783 /* Insert an expression EXPR into a bitmapped set. */
786 bitmap_insert_into_set (bitmap_set_t set
, pre_expr expr
)
788 unsigned int val
= get_expr_value_id (expr
);
789 if (! value_id_constant_p (val
))
791 /* Note this is the only function causing multiple expressions
792 for the same value to appear in a set. This is needed for
793 TMP_GEN, PHI_GEN and NEW_SETs. */
794 bitmap_set_bit (&set
->values
, val
);
795 bitmap_set_bit (&set
->expressions
, get_or_alloc_expression_id (expr
));
799 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
802 bitmap_set_copy (bitmap_set_t dest
, bitmap_set_t orig
)
804 bitmap_copy (&dest
->expressions
, &orig
->expressions
);
805 bitmap_copy (&dest
->values
, &orig
->values
);
809 /* Free memory used up by SET. */
811 bitmap_set_free (bitmap_set_t set
)
813 bitmap_clear (&set
->expressions
);
814 bitmap_clear (&set
->values
);
818 pre_expr_DFS (pre_expr expr
, bitmap_set_t set
, bitmap val_visited
,
819 vec
<pre_expr
> &post
);
821 /* DFS walk leaders of VAL to their operands with leaders in SET, collecting
822 expressions in SET in postorder into POST. */
825 pre_expr_DFS (unsigned val
, bitmap_set_t set
, bitmap val_visited
,
831 /* Iterate over all leaders and DFS recurse. Borrowed from
832 bitmap_find_leader. */
833 bitmap exprset
= value_expressions
[val
];
834 if (!exprset
->first
->next
)
836 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
837 if (bitmap_bit_p (&set
->expressions
, i
))
838 pre_expr_DFS (expression_for_id (i
), set
, val_visited
, post
);
842 EXECUTE_IF_AND_IN_BITMAP (exprset
, &set
->expressions
, 0, i
, bi
)
843 pre_expr_DFS (expression_for_id (i
), set
, val_visited
, post
);
846 /* DFS walk EXPR to its operands with leaders in SET, collecting
847 expressions in SET in postorder into POST. */
850 pre_expr_DFS (pre_expr expr
, bitmap_set_t set
, bitmap val_visited
,
857 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
858 for (unsigned i
= 0; i
< nary
->length
; i
++)
860 if (TREE_CODE (nary
->op
[i
]) != SSA_NAME
)
862 unsigned int op_val_id
= VN_INFO (nary
->op
[i
])->value_id
;
863 /* If we already found a leader for the value we've
864 recursed already. Avoid the costly bitmap_find_leader. */
865 if (bitmap_bit_p (&set
->values
, op_val_id
)
866 && bitmap_set_bit (val_visited
, op_val_id
))
867 pre_expr_DFS (op_val_id
, set
, val_visited
, post
);
873 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
874 vec
<vn_reference_op_s
> operands
= ref
->operands
;
875 vn_reference_op_t operand
;
876 for (unsigned i
= 0; operands
.iterate (i
, &operand
); i
++)
879 op
[0] = operand
->op0
;
880 op
[1] = operand
->op1
;
881 op
[2] = operand
->op2
;
882 for (unsigned n
= 0; n
< 3; ++n
)
884 if (!op
[n
] || TREE_CODE (op
[n
]) != SSA_NAME
)
886 unsigned op_val_id
= VN_INFO (op
[n
])->value_id
;
887 if (bitmap_bit_p (&set
->values
, op_val_id
)
888 && bitmap_set_bit (val_visited
, op_val_id
))
889 pre_expr_DFS (op_val_id
, set
, val_visited
, post
);
896 post
.quick_push (expr
);
899 /* Generate an topological-ordered array of bitmap set SET. */
902 sorted_array_from_bitmap_set (bitmap_set_t set
)
906 vec
<pre_expr
> result
;
908 /* Pre-allocate enough space for the array. */
909 result
.create (bitmap_count_bits (&set
->expressions
));
911 auto_bitmap
val_visited (&grand_bitmap_obstack
);
912 bitmap_tree_view (val_visited
);
913 FOR_EACH_VALUE_ID_IN_SET (set
, i
, bi
)
914 if (bitmap_set_bit (val_visited
, i
))
915 pre_expr_DFS (i
, set
, val_visited
, result
);
920 /* Subtract all expressions contained in ORIG from DEST. */
923 bitmap_set_subtract_expressions (bitmap_set_t dest
, bitmap_set_t orig
)
925 bitmap_set_t result
= bitmap_set_new ();
929 bitmap_and_compl (&result
->expressions
, &dest
->expressions
,
932 FOR_EACH_EXPR_ID_IN_SET (result
, i
, bi
)
934 pre_expr expr
= expression_for_id (i
);
935 unsigned int value_id
= get_expr_value_id (expr
);
936 bitmap_set_bit (&result
->values
, value_id
);
942 /* Subtract all values in bitmap set B from bitmap set A. */
945 bitmap_set_subtract_values (bitmap_set_t a
, bitmap_set_t b
)
949 unsigned to_remove
= -1U;
950 bitmap_and_compl_into (&a
->values
, &b
->values
);
951 FOR_EACH_EXPR_ID_IN_SET (a
, i
, bi
)
953 if (to_remove
!= -1U)
955 bitmap_clear_bit (&a
->expressions
, to_remove
);
958 pre_expr expr
= expression_for_id (i
);
959 if (! bitmap_bit_p (&a
->values
, get_expr_value_id (expr
)))
962 if (to_remove
!= -1U)
963 bitmap_clear_bit (&a
->expressions
, to_remove
);
967 /* Return true if bitmapped set SET contains the value VALUE_ID. */
970 bitmap_set_contains_value (bitmap_set_t set
, unsigned int value_id
)
972 if (value_id_constant_p (value_id
))
975 return bitmap_bit_p (&set
->values
, value_id
);
978 /* Return true if two bitmap sets are equal. */
981 bitmap_set_equal (bitmap_set_t a
, bitmap_set_t b
)
983 return bitmap_equal_p (&a
->values
, &b
->values
);
986 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
987 and add it otherwise. Return true if any changes were made. */
990 bitmap_value_replace_in_set (bitmap_set_t set
, pre_expr expr
)
992 unsigned int val
= get_expr_value_id (expr
);
993 if (value_id_constant_p (val
))
996 if (bitmap_set_contains_value (set
, val
))
998 /* The number of expressions having a given value is usually
999 significantly less than the total number of expressions in SET.
1000 Thus, rather than check, for each expression in SET, whether it
1001 has the value LOOKFOR, we walk the reverse mapping that tells us
1002 what expressions have a given value, and see if any of those
1003 expressions are in our set. For large testcases, this is about
1004 5-10x faster than walking the bitmap. If this is somehow a
1005 significant lose for some cases, we can choose which set to walk
1006 based on the set size. */
1009 bitmap exprset
= value_expressions
[val
];
1010 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1012 if (bitmap_clear_bit (&set
->expressions
, i
))
1014 bitmap_set_bit (&set
->expressions
, get_expression_id (expr
));
1015 return i
!= get_expression_id (expr
);
1021 bitmap_insert_into_set (set
, expr
);
1025 /* Insert EXPR into SET if EXPR's value is not already present in
1029 bitmap_value_insert_into_set (bitmap_set_t set
, pre_expr expr
)
1031 unsigned int val
= get_expr_value_id (expr
);
1033 gcc_checking_assert (expr
->id
== get_or_alloc_expression_id (expr
));
1035 /* Constant values are always considered to be part of the set. */
1036 if (value_id_constant_p (val
))
1039 /* If the value membership changed, add the expression. */
1040 if (bitmap_set_bit (&set
->values
, val
))
1041 bitmap_set_bit (&set
->expressions
, expr
->id
);
1044 /* Print out EXPR to outfile. */
1047 print_pre_expr (FILE *outfile
, const pre_expr expr
)
1051 fprintf (outfile
, "NULL");
1057 print_generic_expr (outfile
, PRE_EXPR_CONSTANT (expr
));
1060 print_generic_expr (outfile
, PRE_EXPR_NAME (expr
));
1065 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1066 fprintf (outfile
, "{%s,", get_tree_code_name (nary
->opcode
));
1067 for (i
= 0; i
< nary
->length
; i
++)
1069 print_generic_expr (outfile
, nary
->op
[i
]);
1070 if (i
!= (unsigned) nary
->length
- 1)
1071 fprintf (outfile
, ",");
1073 fprintf (outfile
, "}");
1079 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1080 print_vn_reference_ops (outfile
, ref
->operands
);
1083 fprintf (outfile
, "@");
1084 print_generic_expr (outfile
, ref
->vuse
);
1090 void debug_pre_expr (pre_expr
);
1092 /* Like print_pre_expr but always prints to stderr. */
1094 debug_pre_expr (pre_expr e
)
1096 print_pre_expr (stderr
, e
);
1097 fprintf (stderr
, "\n");
1100 /* Print out SET to OUTFILE. */
1103 print_bitmap_set (FILE *outfile
, bitmap_set_t set
,
1104 const char *setname
, int blockindex
)
1106 fprintf (outfile
, "%s[%d] := { ", setname
, blockindex
);
1113 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
1115 const pre_expr expr
= expression_for_id (i
);
1118 fprintf (outfile
, ", ");
1120 print_pre_expr (outfile
, expr
);
1122 fprintf (outfile
, " (%04d)", get_expr_value_id (expr
));
1125 fprintf (outfile
, " }\n");
1128 void debug_bitmap_set (bitmap_set_t
);
1131 debug_bitmap_set (bitmap_set_t set
)
1133 print_bitmap_set (stderr
, set
, "debug", 0);
1136 void debug_bitmap_sets_for (basic_block
);
1139 debug_bitmap_sets_for (basic_block bb
)
1141 print_bitmap_set (stderr
, AVAIL_OUT (bb
), "avail_out", bb
->index
);
1142 print_bitmap_set (stderr
, EXP_GEN (bb
), "exp_gen", bb
->index
);
1143 print_bitmap_set (stderr
, PHI_GEN (bb
), "phi_gen", bb
->index
);
1144 print_bitmap_set (stderr
, TMP_GEN (bb
), "tmp_gen", bb
->index
);
1145 print_bitmap_set (stderr
, ANTIC_IN (bb
), "antic_in", bb
->index
);
1146 if (do_partial_partial
)
1147 print_bitmap_set (stderr
, PA_IN (bb
), "pa_in", bb
->index
);
1148 print_bitmap_set (stderr
, NEW_SETS (bb
), "new_sets", bb
->index
);
1151 /* Print out the expressions that have VAL to OUTFILE. */
1154 print_value_expressions (FILE *outfile
, unsigned int val
)
1156 bitmap set
= value_expressions
[val
];
1161 sprintf (s
, "%04d", val
);
1162 x
.expressions
= *set
;
1163 print_bitmap_set (outfile
, &x
, s
, 0);
1169 debug_value_expressions (unsigned int val
)
1171 print_value_expressions (stderr
, val
);
1174 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1178 get_or_alloc_expr_for_constant (tree constant
)
1180 unsigned int result_id
;
1181 struct pre_expr_d expr
;
1184 expr
.kind
= CONSTANT
;
1185 PRE_EXPR_CONSTANT (&expr
) = constant
;
1186 result_id
= lookup_expression_id (&expr
);
1188 return expression_for_id (result_id
);
1190 newexpr
= pre_expr_pool
.allocate ();
1191 newexpr
->kind
= CONSTANT
;
1192 newexpr
->loc
= UNKNOWN_LOCATION
;
1193 PRE_EXPR_CONSTANT (newexpr
) = constant
;
1194 alloc_expression_id (newexpr
);
1195 newexpr
->value_id
= get_or_alloc_constant_value_id (constant
);
1196 add_to_value (newexpr
->value_id
, newexpr
);
1200 /* Return the folded version of T if T, when folded, is a gimple
1201 min_invariant or an SSA name. Otherwise, return T. */
1204 fully_constant_expression (pre_expr e
)
1212 vn_nary_op_t nary
= PRE_EXPR_NARY (e
);
1213 tree res
= vn_nary_simplify (nary
);
1216 if (is_gimple_min_invariant (res
))
1217 return get_or_alloc_expr_for_constant (res
);
1218 if (TREE_CODE (res
) == SSA_NAME
)
1219 return get_or_alloc_expr_for_name (res
);
1224 vn_reference_t ref
= PRE_EXPR_REFERENCE (e
);
1226 if ((folded
= fully_constant_vn_reference_p (ref
)))
1227 return get_or_alloc_expr_for_constant (folded
);
1235 /* Translate the VUSE backwards through phi nodes in E->dest, so that
1236 it has the value it would have in E->src. Set *SAME_VALID to true
1237 in case the new vuse doesn't change the value id of the OPERANDS. */
1240 translate_vuse_through_block (vec
<vn_reference_op_s
> operands
,
1241 alias_set_type set
, alias_set_type base_set
,
1242 tree type
, tree vuse
, edge e
, bool *same_valid
)
1244 basic_block phiblock
= e
->dest
;
1245 gimple
*phi
= SSA_NAME_DEF_STMT (vuse
);
1251 if (gimple_bb (phi
) != phiblock
)
1254 /* We have pruned expressions that are killed in PHIBLOCK via
1255 prune_clobbered_mems but we have not rewritten the VUSE to the one
1256 live at the start of the block. If there is no virtual PHI to translate
1257 through return the VUSE live at entry. Otherwise the VUSE to translate
1258 is the def of the virtual PHI node. */
1259 phi
= get_virtual_phi (phiblock
);
1261 return BB_LIVE_VOP_ON_EXIT
1262 (get_immediate_dominator (CDI_DOMINATORS
, phiblock
));
1265 && ao_ref_init_from_vn_reference (&ref
, set
, base_set
, type
, operands
))
1267 bitmap visited
= NULL
;
1268 /* Try to find a vuse that dominates this phi node by skipping
1269 non-clobbering statements. */
1270 unsigned int cnt
= param_sccvn_max_alias_queries_per_access
;
1271 vuse
= get_continuation_for_phi (phi
, &ref
, true,
1272 cnt
, &visited
, false, NULL
, NULL
);
1274 BITMAP_FREE (visited
);
1278 /* If we didn't find any, the value ID can't stay the same. */
1279 if (!vuse
&& same_valid
)
1280 *same_valid
= false;
1282 /* ??? We would like to return vuse here as this is the canonical
1283 upmost vdef that this reference is associated with. But during
1284 insertion of the references into the hash tables we only ever
1285 directly insert with their direct gimple_vuse, hence returning
1286 something else would make us not find the other expression. */
1287 return PHI_ARG_DEF (phi
, e
->dest_idx
);
1290 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1291 SET2 *or* SET3. This is used to avoid making a set consisting of the union
1292 of PA_IN and ANTIC_IN during insert and phi-translation. */
1294 static inline pre_expr
1295 find_leader_in_sets (unsigned int val
, bitmap_set_t set1
, bitmap_set_t set2
,
1296 bitmap_set_t set3
= NULL
)
1298 pre_expr result
= NULL
;
1301 result
= bitmap_find_leader (set1
, val
);
1302 if (!result
&& set2
)
1303 result
= bitmap_find_leader (set2
, val
);
1304 if (!result
&& set3
)
1305 result
= bitmap_find_leader (set3
, val
);
1309 /* Get the tree type for our PRE expression e. */
1312 get_expr_type (const pre_expr e
)
1317 return TREE_TYPE (PRE_EXPR_NAME (e
));
1319 return TREE_TYPE (PRE_EXPR_CONSTANT (e
));
1321 return PRE_EXPR_REFERENCE (e
)->type
;
1323 return PRE_EXPR_NARY (e
)->type
;
1328 /* Get a representative SSA_NAME for a given expression that is available in B.
1329 Since all of our sub-expressions are treated as values, we require
1330 them to be SSA_NAME's for simplicity.
1331 Prior versions of GVNPRE used to use "value handles" here, so that
1332 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1333 either case, the operands are really values (IE we do not expect
1334 them to be usable without finding leaders). */
1337 get_representative_for (const pre_expr e
, basic_block b
= NULL
)
1339 tree name
, valnum
= NULL_TREE
;
1340 unsigned int value_id
= get_expr_value_id (e
);
1345 return PRE_EXPR_NAME (e
);
1347 return PRE_EXPR_CONSTANT (e
);
1351 /* Go through all of the expressions representing this value
1352 and pick out an SSA_NAME. */
1355 bitmap exprs
= value_expressions
[value_id
];
1356 EXECUTE_IF_SET_IN_BITMAP (exprs
, 0, i
, bi
)
1358 pre_expr rep
= expression_for_id (i
);
1359 if (rep
->kind
== NAME
)
1361 tree name
= PRE_EXPR_NAME (rep
);
1362 valnum
= VN_INFO (name
)->valnum
;
1363 gimple
*def
= SSA_NAME_DEF_STMT (name
);
1364 /* We have to return either a new representative or one
1365 that can be used for expression simplification and thus
1366 is available in B. */
1368 || gimple_nop_p (def
)
1369 || dominated_by_p (CDI_DOMINATORS
, b
, gimple_bb (def
)))
1372 else if (rep
->kind
== CONSTANT
)
1373 return PRE_EXPR_CONSTANT (rep
);
1379 /* If we reached here we couldn't find an SSA_NAME. This can
1380 happen when we've discovered a value that has never appeared in
1381 the program as set to an SSA_NAME, as the result of phi translation.
1383 ??? We should be able to re-use this when we insert the statement
1385 name
= make_temp_ssa_name (get_expr_type (e
), gimple_build_nop (), "pretmp");
1386 vn_ssa_aux_t vn_info
= VN_INFO (name
);
1387 vn_info
->value_id
= value_id
;
1388 vn_info
->valnum
= valnum
? valnum
: name
;
1389 vn_info
->visited
= true;
1390 /* ??? For now mark this SSA name for release by VN. */
1391 vn_info
->needs_insertion
= true;
1392 add_to_value (value_id
, get_or_alloc_expr_for_name (name
));
1393 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1395 fprintf (dump_file
, "Created SSA_NAME representative ");
1396 print_generic_expr (dump_file
, name
);
1397 fprintf (dump_file
, " for expression:");
1398 print_pre_expr (dump_file
, e
);
1399 fprintf (dump_file
, " (%04d)\n", value_id
);
1407 phi_translate (bitmap_set_t
, pre_expr
, bitmap_set_t
, bitmap_set_t
, edge
);
1409 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1410 the phis in PRED. Return NULL if we can't find a leader for each part
1411 of the translated expression. */
1414 phi_translate_1 (bitmap_set_t dest
,
1415 pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
, edge e
)
1417 basic_block pred
= e
->src
;
1418 basic_block phiblock
= e
->dest
;
1419 location_t expr_loc
= expr
->loc
;
1425 bool changed
= false;
1426 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1427 vn_nary_op_t newnary
= XALLOCAVAR (struct vn_nary_op_s
,
1428 sizeof_vn_nary_op (nary
->length
));
1429 memcpy (newnary
, nary
, sizeof_vn_nary_op (nary
->length
));
1431 for (i
= 0; i
< newnary
->length
; i
++)
1433 if (TREE_CODE (newnary
->op
[i
]) != SSA_NAME
)
1437 pre_expr leader
, result
;
1438 unsigned int op_val_id
= VN_INFO (newnary
->op
[i
])->value_id
;
1439 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1440 result
= phi_translate (dest
, leader
, set1
, set2
, e
);
1441 if (result
&& result
!= leader
)
1442 /* If op has a leader in the sets we translate make
1443 sure to use the value of the translated expression.
1444 We might need a new representative for that. */
1445 newnary
->op
[i
] = get_representative_for (result
, pred
);
1449 changed
|= newnary
->op
[i
] != nary
->op
[i
];
1455 unsigned int new_val_id
;
1457 PRE_EXPR_NARY (expr
) = newnary
;
1458 constant
= fully_constant_expression (expr
);
1459 PRE_EXPR_NARY (expr
) = nary
;
1460 if (constant
!= expr
)
1462 /* For non-CONSTANTs we have to make sure we can eventually
1463 insert the expression. Which means we need to have a
1465 if (constant
->kind
!= CONSTANT
)
1467 /* Do not allow simplifications to non-constants over
1468 backedges as this will likely result in a loop PHI node
1469 to be inserted and increased register pressure.
1470 See PR77498 - this avoids doing predcoms work in
1471 a less efficient way. */
1472 if (e
->flags
& EDGE_DFS_BACK
)
1476 unsigned value_id
= get_expr_value_id (constant
);
1477 /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1478 dest has what we computed into ANTIC_OUT sofar
1479 so pick from that - since topological sorting
1480 by sorted_array_from_bitmap_set isn't perfect
1481 we may lose some cases here. */
1482 constant
= find_leader_in_sets (value_id
, dest
,
1486 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1488 fprintf (dump_file
, "simplifying ");
1489 print_pre_expr (dump_file
, expr
);
1490 fprintf (dump_file
, " translated %d -> %d to ",
1491 phiblock
->index
, pred
->index
);
1492 PRE_EXPR_NARY (expr
) = newnary
;
1493 print_pre_expr (dump_file
, expr
);
1494 PRE_EXPR_NARY (expr
) = nary
;
1495 fprintf (dump_file
, " to ");
1496 print_pre_expr (dump_file
, constant
);
1497 fprintf (dump_file
, "\n");
1507 tree result
= vn_nary_op_lookup_pieces (newnary
->length
,
1512 if (result
&& is_gimple_min_invariant (result
))
1513 return get_or_alloc_expr_for_constant (result
);
1515 if (!nary
|| nary
->predicated_values
)
1518 new_val_id
= nary
->value_id
;
1519 expr
= get_or_alloc_expr_for_nary (newnary
, new_val_id
, expr_loc
);
1520 add_to_value (get_expr_value_id (expr
), expr
);
1528 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1529 vec
<vn_reference_op_s
> operands
= ref
->operands
;
1530 tree vuse
= ref
->vuse
;
1531 tree newvuse
= vuse
;
1532 vec
<vn_reference_op_s
> newoperands
= vNULL
;
1533 bool changed
= false, same_valid
= true;
1535 vn_reference_op_t operand
;
1536 vn_reference_t newref
;
1538 for (i
= 0; operands
.iterate (i
, &operand
); i
++)
1543 tree type
= operand
->type
;
1544 vn_reference_op_s newop
= *operand
;
1545 op
[0] = operand
->op0
;
1546 op
[1] = operand
->op1
;
1547 op
[2] = operand
->op2
;
1548 for (n
= 0; n
< 3; ++n
)
1550 unsigned int op_val_id
;
1553 if (TREE_CODE (op
[n
]) != SSA_NAME
)
1555 /* We can't possibly insert these. */
1557 && !is_gimple_min_invariant (op
[n
]))
1561 op_val_id
= VN_INFO (op
[n
])->value_id
;
1562 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1563 opresult
= phi_translate (dest
, leader
, set1
, set2
, e
);
1564 if (opresult
&& opresult
!= leader
)
1566 tree name
= get_representative_for (opresult
);
1567 changed
|= name
!= op
[n
];
1575 newoperands
.release ();
1578 /* When we translate a MEM_REF across a backedge and we have
1579 restrict info that's not from our functions parameters
1580 we have to remap it since we now may deal with a different
1581 instance where the dependence info is no longer valid.
1582 See PR102970. Note instead of keeping a remapping table
1583 per backedge we simply throw away restrict info. */
1584 if ((newop
.opcode
== MEM_REF
1585 || newop
.opcode
== TARGET_MEM_REF
)
1587 && (e
->flags
& EDGE_DFS_BACK
))
1595 if (!newoperands
.exists ())
1596 newoperands
= operands
.copy ();
1597 /* We may have changed from an SSA_NAME to a constant */
1598 if (newop
.opcode
== SSA_NAME
&& TREE_CODE (op
[0]) != SSA_NAME
)
1599 newop
.opcode
= TREE_CODE (op
[0]);
1604 newoperands
[i
] = newop
;
1606 gcc_checking_assert (i
== operands
.length ());
1610 newvuse
= translate_vuse_through_block (newoperands
.exists ()
1611 ? newoperands
: operands
,
1612 ref
->set
, ref
->base_set
,
1615 ? NULL
: &same_valid
);
1616 if (newvuse
== NULL_TREE
)
1618 newoperands
.release ();
1623 if (changed
|| newvuse
!= vuse
)
1625 unsigned int new_val_id
;
1627 tree result
= vn_reference_lookup_pieces (newvuse
, ref
->set
,
1630 newoperands
.exists ()
1631 ? newoperands
: operands
,
1634 newoperands
.release ();
1636 /* We can always insert constants, so if we have a partial
1637 redundant constant load of another type try to translate it
1638 to a constant of appropriate type. */
1639 if (result
&& is_gimple_min_invariant (result
))
1642 if (!useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1644 tem
= fold_unary (VIEW_CONVERT_EXPR
, ref
->type
, result
);
1645 if (tem
&& !is_gimple_min_invariant (tem
))
1649 return get_or_alloc_expr_for_constant (tem
);
1652 /* If we'd have to convert things we would need to validate
1653 if we can insert the translated expression. So fail
1654 here for now - we cannot insert an alias with a different
1655 type in the VN tables either, as that would assert. */
1657 && !useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1659 else if (!result
&& newref
1660 && !useless_type_conversion_p (ref
->type
, newref
->type
))
1662 newoperands
.release ();
1667 new_val_id
= newref
->value_id
;
1670 if (changed
|| !same_valid
)
1671 new_val_id
= get_next_value_id ();
1673 new_val_id
= ref
->value_id
;
1674 if (!newoperands
.exists ())
1675 newoperands
= operands
.copy ();
1676 newref
= vn_reference_insert_pieces (newvuse
, ref
->set
,
1677 ref
->base_set
, ref
->type
,
1679 result
, new_val_id
);
1680 newoperands
= vNULL
;
1682 expr
= get_or_alloc_expr_for_reference (newref
, expr_loc
);
1683 add_to_value (new_val_id
, expr
);
1685 newoperands
.release ();
1692 tree name
= PRE_EXPR_NAME (expr
);
1693 gimple
*def_stmt
= SSA_NAME_DEF_STMT (name
);
1694 /* If the SSA name is defined by a PHI node in this block,
1696 if (gimple_code (def_stmt
) == GIMPLE_PHI
1697 && gimple_bb (def_stmt
) == phiblock
)
1699 tree def
= PHI_ARG_DEF (def_stmt
, e
->dest_idx
);
1701 /* Handle constant. */
1702 if (is_gimple_min_invariant (def
))
1703 return get_or_alloc_expr_for_constant (def
);
1705 return get_or_alloc_expr_for_name (def
);
1707 /* Otherwise return it unchanged - it will get removed if its
1708 value is not available in PREDs AVAIL_OUT set of expressions
1709 by the subtraction of TMP_GEN. */
1718 /* Wrapper around phi_translate_1 providing caching functionality. */
1721 phi_translate (bitmap_set_t dest
, pre_expr expr
,
1722 bitmap_set_t set1
, bitmap_set_t set2
, edge e
)
1724 expr_pred_trans_t slot
= NULL
;
1730 /* Constants contain no values that need translation. */
1731 if (expr
->kind
== CONSTANT
)
1734 if (value_id_constant_p (get_expr_value_id (expr
)))
1737 /* Don't add translations of NAMEs as those are cheap to translate. */
1738 if (expr
->kind
!= NAME
)
1740 if (phi_trans_add (&slot
, expr
, e
->src
))
1741 return slot
->v
== 0 ? NULL
: expression_for_id (slot
->v
);
1742 /* Store NULL for the value we want to return in the case of
1748 basic_block saved_valueize_bb
= vn_context_bb
;
1749 vn_context_bb
= e
->src
;
1750 phitrans
= phi_translate_1 (dest
, expr
, set1
, set2
, e
);
1751 vn_context_bb
= saved_valueize_bb
;
1755 /* We may have reallocated. */
1756 phi_trans_add (&slot
, expr
, e
->src
);
1758 slot
->v
= get_expression_id (phitrans
);
1760 /* Remove failed translations again, they cause insert
1761 iteration to not pick up new opportunities reliably. */
1762 PHI_TRANS_TABLE (e
->src
)->clear_slot (slot
);
1769 /* For each expression in SET, translate the values through phi nodes
1770 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1771 expressions in DEST. */
1774 phi_translate_set (bitmap_set_t dest
, bitmap_set_t set
, edge e
)
1779 if (gimple_seq_empty_p (phi_nodes (e
->dest
)))
1781 bitmap_set_copy (dest
, set
);
1785 /* Allocate the phi-translation cache where we have an idea about
1786 its size. hash-table implementation internals tell us that
1787 allocating the table to fit twice the number of elements will
1788 make sure we do not usually re-allocate. */
1789 if (!PHI_TRANS_TABLE (e
->src
))
1790 PHI_TRANS_TABLE (e
->src
) = new hash_table
<expr_pred_trans_d
>
1791 (2 * bitmap_count_bits (&set
->expressions
));
1792 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
1794 pre_expr expr
= expression_for_id (i
);
1795 pre_expr translated
= phi_translate (dest
, expr
, set
, NULL
, e
);
1799 bitmap_insert_into_set (dest
, translated
);
1803 /* Find the leader for a value (i.e., the name representing that
1804 value) in a given set, and return it. Return NULL if no leader
1808 bitmap_find_leader (bitmap_set_t set
, unsigned int val
)
1810 if (value_id_constant_p (val
))
1811 return constant_value_expressions
[-val
];
1813 if (bitmap_set_contains_value (set
, val
))
1815 /* Rather than walk the entire bitmap of expressions, and see
1816 whether any of them has the value we are looking for, we look
1817 at the reverse mapping, which tells us the set of expressions
1818 that have a given value (IE value->expressions with that
1819 value) and see if any of those expressions are in our set.
1820 The number of expressions per value is usually significantly
1821 less than the number of expressions in the set. In fact, for
1822 large testcases, doing it this way is roughly 5-10x faster
1823 than walking the bitmap.
1824 If this is somehow a significant lose for some cases, we can
1825 choose which set to walk based on which set is smaller. */
1828 bitmap exprset
= value_expressions
[val
];
1830 if (!exprset
->first
->next
)
1831 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1832 if (bitmap_bit_p (&set
->expressions
, i
))
1833 return expression_for_id (i
);
1835 EXECUTE_IF_AND_IN_BITMAP (exprset
, &set
->expressions
, 0, i
, bi
)
1836 return expression_for_id (i
);
1841 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1842 BLOCK by seeing if it is not killed in the block. Note that we are
1843 only determining whether there is a store that kills it. Because
1844 of the order in which clean iterates over values, we are guaranteed
1845 that altered operands will have caused us to be eliminated from the
1846 ANTIC_IN set already. */
1849 value_dies_in_block_x (pre_expr expr
, basic_block block
)
1851 tree vuse
= PRE_EXPR_REFERENCE (expr
)->vuse
;
1852 vn_reference_t refx
= PRE_EXPR_REFERENCE (expr
);
1854 gimple_stmt_iterator gsi
;
1855 unsigned id
= get_expression_id (expr
);
1862 /* Lookup a previously calculated result. */
1863 if (EXPR_DIES (block
)
1864 && bitmap_bit_p (EXPR_DIES (block
), id
* 2))
1865 return bitmap_bit_p (EXPR_DIES (block
), id
* 2 + 1);
1867 /* A memory expression {e, VUSE} dies in the block if there is a
1868 statement that may clobber e. If, starting statement walk from the
1869 top of the basic block, a statement uses VUSE there can be no kill
1870 inbetween that use and the original statement that loaded {e, VUSE},
1871 so we can stop walking. */
1872 ref
.base
= NULL_TREE
;
1873 for (gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1875 tree def_vuse
, def_vdef
;
1876 def
= gsi_stmt (gsi
);
1877 def_vuse
= gimple_vuse (def
);
1878 def_vdef
= gimple_vdef (def
);
1880 /* Not a memory statement. */
1884 /* Not a may-def. */
1887 /* A load with the same VUSE, we're done. */
1888 if (def_vuse
== vuse
)
1894 /* Init ref only if we really need it. */
1895 if (ref
.base
== NULL_TREE
1896 && !ao_ref_init_from_vn_reference (&ref
, refx
->set
, refx
->base_set
,
1897 refx
->type
, refx
->operands
))
1902 /* If the statement may clobber expr, it dies. */
1903 if (stmt_may_clobber_ref_p_1 (def
, &ref
))
1910 /* Remember the result. */
1911 if (!EXPR_DIES (block
))
1912 EXPR_DIES (block
) = BITMAP_ALLOC (&grand_bitmap_obstack
);
1913 bitmap_set_bit (EXPR_DIES (block
), id
* 2);
1915 bitmap_set_bit (EXPR_DIES (block
), id
* 2 + 1);
1921 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1922 contains its value-id. */
1925 op_valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, tree op
)
1927 if (op
&& TREE_CODE (op
) == SSA_NAME
)
1929 unsigned int value_id
= VN_INFO (op
)->value_id
;
1930 if (!(bitmap_set_contains_value (set1
, value_id
)
1931 || (set2
&& bitmap_set_contains_value (set2
, value_id
))))
1937 /* Determine if the expression EXPR is valid in SET1 U SET2.
1938 ONLY SET2 CAN BE NULL.
1939 This means that we have a leader for each part of the expression
1940 (if it consists of values), or the expression is an SSA_NAME.
1941 For loads/calls, we also see if the vuse is killed in this block. */
1944 valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, pre_expr expr
)
1949 /* By construction all NAMEs are available. Non-available
1950 NAMEs are removed by subtracting TMP_GEN from the sets. */
1955 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1956 for (i
= 0; i
< nary
->length
; i
++)
1957 if (!op_valid_in_sets (set1
, set2
, nary
->op
[i
]))
1964 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1965 vn_reference_op_t vro
;
1968 FOR_EACH_VEC_ELT (ref
->operands
, i
, vro
)
1970 if (!op_valid_in_sets (set1
, set2
, vro
->op0
)
1971 || !op_valid_in_sets (set1
, set2
, vro
->op1
)
1972 || !op_valid_in_sets (set1
, set2
, vro
->op2
))
1982 /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
1983 This means expressions that are made up of values we have no leaders for
1987 clean (bitmap_set_t set1
, bitmap_set_t set2
= NULL
)
1989 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set1
);
1993 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
1995 if (!valid_in_sets (set1
, set2
, expr
))
1997 unsigned int val
= get_expr_value_id (expr
);
1998 bitmap_clear_bit (&set1
->expressions
, get_expression_id (expr
));
1999 /* We are entered with possibly multiple expressions for a value
2000 so before removing a value from the set see if there's an
2001 expression for it left. */
2002 if (! bitmap_find_leader (set1
, val
))
2003 bitmap_clear_bit (&set1
->values
, val
);
2012 FOR_EACH_EXPR_ID_IN_SET (set1
, j
, bi
)
2013 gcc_assert (valid_in_sets (set1
, set2
, expression_for_id (j
)));
2017 /* Clean the set of expressions that are no longer valid in SET because
2018 they are clobbered in BLOCK or because they trap and may not be executed. */
2021 prune_clobbered_mems (bitmap_set_t set
, basic_block block
)
2025 unsigned to_remove
= -1U;
2026 bool any_removed
= false;
2028 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
2030 /* Remove queued expr. */
2031 if (to_remove
!= -1U)
2033 bitmap_clear_bit (&set
->expressions
, to_remove
);
2038 pre_expr expr
= expression_for_id (i
);
2039 if (expr
->kind
== REFERENCE
)
2041 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2044 gimple
*def_stmt
= SSA_NAME_DEF_STMT (ref
->vuse
);
2045 if (!gimple_nop_p (def_stmt
)
2046 && ((gimple_bb (def_stmt
) != block
2047 && !dominated_by_p (CDI_DOMINATORS
,
2048 block
, gimple_bb (def_stmt
)))
2049 || (gimple_bb (def_stmt
) == block
2050 && value_dies_in_block_x (expr
, block
))))
2053 /* If the REFERENCE may trap make sure the block does not contain
2054 a possible exit point.
2055 ??? This is overly conservative if we translate AVAIL_OUT
2056 as the available expression might be after the exit point. */
2057 if (BB_MAY_NOTRETURN (block
)
2058 && vn_reference_may_trap (ref
))
2061 else if (expr
->kind
== NARY
)
2063 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2064 /* If the NARY may trap make sure the block does not contain
2065 a possible exit point.
2066 ??? This is overly conservative if we translate AVAIL_OUT
2067 as the available expression might be after the exit point. */
2068 if (BB_MAY_NOTRETURN (block
)
2069 && vn_nary_may_trap (nary
))
2074 /* Remove queued expr. */
2075 if (to_remove
!= -1U)
2077 bitmap_clear_bit (&set
->expressions
, to_remove
);
2081 /* Above we only removed expressions, now clean the set of values
2082 which no longer have any corresponding expression. We cannot
2083 clear the value at the time we remove an expression since there
2084 may be multiple expressions per value.
2085 If we'd queue possibly to be removed values we could use
2086 the bitmap_find_leader way to see if there's still an expression
2087 for it. For some ratio of to be removed values and number of
2088 values/expressions in the set this might be faster than rebuilding
2092 bitmap_clear (&set
->values
);
2093 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
2095 pre_expr expr
= expression_for_id (i
);
2096 unsigned int value_id
= get_expr_value_id (expr
);
2097 bitmap_set_bit (&set
->values
, value_id
);
2102 /* Compute the ANTIC set for BLOCK.
2104 If succs(BLOCK) > 1 then
2105 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2106 else if succs(BLOCK) == 1 then
2107 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2109 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2111 Note that clean() is deferred until after the iteration. */
2114 compute_antic_aux (basic_block block
, bool block_has_abnormal_pred_edge
)
2116 bitmap_set_t S
, old
, ANTIC_OUT
;
2120 bool was_visited
= BB_VISITED (block
);
2121 bool changed
= ! BB_VISITED (block
);
2122 BB_VISITED (block
) = 1;
2123 old
= ANTIC_OUT
= S
= NULL
;
2125 /* If any edges from predecessors are abnormal, antic_in is empty,
2127 if (block_has_abnormal_pred_edge
)
2128 goto maybe_dump_sets
;
2130 old
= ANTIC_IN (block
);
2131 ANTIC_OUT
= bitmap_set_new ();
2133 /* If the block has no successors, ANTIC_OUT is empty. */
2134 if (EDGE_COUNT (block
->succs
) == 0)
2136 /* If we have one successor, we could have some phi nodes to
2137 translate through. */
2138 else if (single_succ_p (block
))
2140 e
= single_succ_edge (block
);
2141 gcc_assert (BB_VISITED (e
->dest
));
2142 phi_translate_set (ANTIC_OUT
, ANTIC_IN (e
->dest
), e
);
2144 /* If we have multiple successors, we take the intersection of all of
2145 them. Note that in the case of loop exit phi nodes, we may have
2146 phis to translate through. */
2152 auto_vec
<edge
> worklist (EDGE_COUNT (block
->succs
));
2153 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2156 && BB_VISITED (e
->dest
))
2158 else if (BB_VISITED (e
->dest
))
2159 worklist
.quick_push (e
);
2162 /* Unvisited successors get their ANTIC_IN replaced by the
2163 maximal set to arrive at a maximum ANTIC_IN solution.
2164 We can ignore them in the intersection operation and thus
2165 need not explicitely represent that maximum solution. */
2166 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2167 fprintf (dump_file
, "ANTIC_IN is MAX on %d->%d\n",
2168 e
->src
->index
, e
->dest
->index
);
2172 /* Of multiple successors we have to have visited one already
2173 which is guaranteed by iteration order. */
2174 gcc_assert (first
!= NULL
);
2176 phi_translate_set (ANTIC_OUT
, ANTIC_IN (first
->dest
), first
);
2178 /* If we have multiple successors we need to intersect the ANTIC_OUT
2179 sets. For values that's a simple intersection but for
2180 expressions it is a union. Given we want to have a single
2181 expression per value in our sets we have to canonicalize.
2182 Avoid randomness and running into cycles like for PR82129 and
2183 canonicalize the expression we choose to the one with the
2184 lowest id. This requires we actually compute the union first. */
2185 FOR_EACH_VEC_ELT (worklist
, i
, e
)
2187 if (!gimple_seq_empty_p (phi_nodes (e
->dest
)))
2189 bitmap_set_t tmp
= bitmap_set_new ();
2190 phi_translate_set (tmp
, ANTIC_IN (e
->dest
), e
);
2191 bitmap_and_into (&ANTIC_OUT
->values
, &tmp
->values
);
2192 bitmap_ior_into (&ANTIC_OUT
->expressions
, &tmp
->expressions
);
2193 bitmap_set_free (tmp
);
2197 bitmap_and_into (&ANTIC_OUT
->values
, &ANTIC_IN (e
->dest
)->values
);
2198 bitmap_ior_into (&ANTIC_OUT
->expressions
,
2199 &ANTIC_IN (e
->dest
)->expressions
);
2202 if (! worklist
.is_empty ())
2204 /* Prune expressions not in the value set. */
2207 unsigned int to_clear
= -1U;
2208 FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT
, i
, bi
)
2210 if (to_clear
!= -1U)
2212 bitmap_clear_bit (&ANTIC_OUT
->expressions
, to_clear
);
2215 pre_expr expr
= expression_for_id (i
);
2216 unsigned int value_id
= get_expr_value_id (expr
);
2217 if (!bitmap_bit_p (&ANTIC_OUT
->values
, value_id
))
2220 if (to_clear
!= -1U)
2221 bitmap_clear_bit (&ANTIC_OUT
->expressions
, to_clear
);
2225 /* Prune expressions that are clobbered in block and thus become
2226 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2227 prune_clobbered_mems (ANTIC_OUT
, block
);
2229 /* Generate ANTIC_OUT - TMP_GEN. */
2230 S
= bitmap_set_subtract_expressions (ANTIC_OUT
, TMP_GEN (block
));
2232 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2233 ANTIC_IN (block
) = bitmap_set_subtract_expressions (EXP_GEN (block
),
2236 /* Then union in the ANTIC_OUT - TMP_GEN values,
2237 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2238 bitmap_ior_into (&ANTIC_IN (block
)->values
, &S
->values
);
2239 bitmap_ior_into (&ANTIC_IN (block
)->expressions
, &S
->expressions
);
2241 /* clean (ANTIC_IN (block)) is defered to after the iteration converged
2242 because it can cause non-convergence, see for example PR81181. */
2244 /* Intersect ANTIC_IN with the old ANTIC_IN. This is required until
2245 we properly represent the maximum expression set, thus not prune
2246 values without expressions during the iteration. */
2248 && bitmap_and_into (&ANTIC_IN (block
)->values
, &old
->values
))
2250 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2251 fprintf (dump_file
, "warning: intersecting with old ANTIC_IN "
2252 "shrinks the set\n");
2253 /* Prune expressions not in the value set. */
2256 unsigned int to_clear
= -1U;
2257 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block
), i
, bi
)
2259 if (to_clear
!= -1U)
2261 bitmap_clear_bit (&ANTIC_IN (block
)->expressions
, to_clear
);
2264 pre_expr expr
= expression_for_id (i
);
2265 unsigned int value_id
= get_expr_value_id (expr
);
2266 if (!bitmap_bit_p (&ANTIC_IN (block
)->values
, value_id
))
2269 if (to_clear
!= -1U)
2270 bitmap_clear_bit (&ANTIC_IN (block
)->expressions
, to_clear
);
2273 if (!bitmap_set_equal (old
, ANTIC_IN (block
)))
2277 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2280 print_bitmap_set (dump_file
, ANTIC_OUT
, "ANTIC_OUT", block
->index
);
2283 fprintf (dump_file
, "[changed] ");
2284 print_bitmap_set (dump_file
, ANTIC_IN (block
), "ANTIC_IN",
2288 print_bitmap_set (dump_file
, S
, "S", block
->index
);
2291 bitmap_set_free (old
);
2293 bitmap_set_free (S
);
2295 bitmap_set_free (ANTIC_OUT
);
2299 /* Compute PARTIAL_ANTIC for BLOCK.
2301 If succs(BLOCK) > 1 then
2302 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2303 in ANTIC_OUT for all succ(BLOCK)
2304 else if succs(BLOCK) == 1 then
2305 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2307 PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
2311 compute_partial_antic_aux (basic_block block
,
2312 bool block_has_abnormal_pred_edge
)
2314 bitmap_set_t old_PA_IN
;
2315 bitmap_set_t PA_OUT
;
2318 unsigned long max_pa
= param_max_partial_antic_length
;
2320 old_PA_IN
= PA_OUT
= NULL
;
2322 /* If any edges from predecessors are abnormal, antic_in is empty,
2324 if (block_has_abnormal_pred_edge
)
2325 goto maybe_dump_sets
;
2327 /* If there are too many partially anticipatable values in the
2328 block, phi_translate_set can take an exponential time: stop
2329 before the translation starts. */
2331 && single_succ_p (block
)
2332 && bitmap_count_bits (&PA_IN (single_succ (block
))->values
) > max_pa
)
2333 goto maybe_dump_sets
;
2335 old_PA_IN
= PA_IN (block
);
2336 PA_OUT
= bitmap_set_new ();
2338 /* If the block has no successors, ANTIC_OUT is empty. */
2339 if (EDGE_COUNT (block
->succs
) == 0)
2341 /* If we have one successor, we could have some phi nodes to
2342 translate through. Note that we can't phi translate across DFS
2343 back edges in partial antic, because it uses a union operation on
2344 the successors. For recurrences like IV's, we will end up
2345 generating a new value in the set on each go around (i + 3 (VH.1)
2346 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2347 else if (single_succ_p (block
))
2349 e
= single_succ_edge (block
);
2350 if (!(e
->flags
& EDGE_DFS_BACK
))
2351 phi_translate_set (PA_OUT
, PA_IN (e
->dest
), e
);
2353 /* If we have multiple successors, we take the union of all of
2359 auto_vec
<edge
> worklist (EDGE_COUNT (block
->succs
));
2360 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2362 if (e
->flags
& EDGE_DFS_BACK
)
2364 worklist
.quick_push (e
);
2366 if (worklist
.length () > 0)
2368 FOR_EACH_VEC_ELT (worklist
, i
, e
)
2373 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e
->dest
), i
, bi
)
2374 bitmap_value_insert_into_set (PA_OUT
,
2375 expression_for_id (i
));
2376 if (!gimple_seq_empty_p (phi_nodes (e
->dest
)))
2378 bitmap_set_t pa_in
= bitmap_set_new ();
2379 phi_translate_set (pa_in
, PA_IN (e
->dest
), e
);
2380 FOR_EACH_EXPR_ID_IN_SET (pa_in
, i
, bi
)
2381 bitmap_value_insert_into_set (PA_OUT
,
2382 expression_for_id (i
));
2383 bitmap_set_free (pa_in
);
2386 FOR_EACH_EXPR_ID_IN_SET (PA_IN (e
->dest
), i
, bi
)
2387 bitmap_value_insert_into_set (PA_OUT
,
2388 expression_for_id (i
));
2393 /* Prune expressions that are clobbered in block and thus become
2394 invalid if translated from PA_OUT to PA_IN. */
2395 prune_clobbered_mems (PA_OUT
, block
);
2397 /* PA_IN starts with PA_OUT - TMP_GEN.
2398 Then we subtract things from ANTIC_IN. */
2399 PA_IN (block
) = bitmap_set_subtract_expressions (PA_OUT
, TMP_GEN (block
));
2401 /* For partial antic, we want to put back in the phi results, since
2402 we will properly avoid making them partially antic over backedges. */
2403 bitmap_ior_into (&PA_IN (block
)->values
, &PHI_GEN (block
)->values
);
2404 bitmap_ior_into (&PA_IN (block
)->expressions
, &PHI_GEN (block
)->expressions
);
2406 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2407 bitmap_set_subtract_values (PA_IN (block
), ANTIC_IN (block
));
2409 clean (PA_IN (block
), ANTIC_IN (block
));
2412 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2415 print_bitmap_set (dump_file
, PA_OUT
, "PA_OUT", block
->index
);
2417 print_bitmap_set (dump_file
, PA_IN (block
), "PA_IN", block
->index
);
2420 bitmap_set_free (old_PA_IN
);
2422 bitmap_set_free (PA_OUT
);
2425 /* Compute ANTIC and partial ANTIC sets. */
2428 compute_antic (void)
2430 bool changed
= true;
2431 int num_iterations
= 0;
2437 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2438 We pre-build the map of blocks with incoming abnormal edges here. */
2439 auto_sbitmap
has_abnormal_preds (last_basic_block_for_fn (cfun
));
2440 bitmap_clear (has_abnormal_preds
);
2442 FOR_ALL_BB_FN (block
, cfun
)
2444 BB_VISITED (block
) = 0;
2446 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2447 if (e
->flags
& EDGE_ABNORMAL
)
2449 bitmap_set_bit (has_abnormal_preds
, block
->index
);
2453 /* While we are here, give empty ANTIC_IN sets to each block. */
2454 ANTIC_IN (block
) = bitmap_set_new ();
2455 if (do_partial_partial
)
2456 PA_IN (block
) = bitmap_set_new ();
2459 /* At the exit block we anticipate nothing. */
2460 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun
)) = 1;
2462 /* For ANTIC computation we need a postorder that also guarantees that
2463 a block with a single successor is visited after its successor.
2464 RPO on the inverted CFG has this property. */
2465 auto_vec
<int, 20> postorder
;
2466 inverted_post_order_compute (&postorder
);
2468 auto_sbitmap
worklist (last_basic_block_for_fn (cfun
) + 1);
2469 bitmap_clear (worklist
);
2470 FOR_EACH_EDGE (e
, ei
, EXIT_BLOCK_PTR_FOR_FN (cfun
)->preds
)
2471 bitmap_set_bit (worklist
, e
->src
->index
);
2474 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2475 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2476 /* ??? We need to clear our PHI translation cache here as the
2477 ANTIC sets shrink and we restrict valid translations to
2478 those having operands with leaders in ANTIC. Same below
2479 for PA ANTIC computation. */
2482 for (i
= postorder
.length () - 1; i
>= 0; i
--)
2484 if (bitmap_bit_p (worklist
, postorder
[i
]))
2486 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2487 bitmap_clear_bit (worklist
, block
->index
);
2488 if (compute_antic_aux (block
,
2489 bitmap_bit_p (has_abnormal_preds
,
2492 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2493 bitmap_set_bit (worklist
, e
->src
->index
);
2498 /* Theoretically possible, but *highly* unlikely. */
2499 gcc_checking_assert (num_iterations
< 500);
2502 /* We have to clean after the dataflow problem converged as cleaning
2503 can cause non-convergence because it is based on expressions
2504 rather than values. */
2505 FOR_EACH_BB_FN (block
, cfun
)
2506 clean (ANTIC_IN (block
));
2508 statistics_histogram_event (cfun
, "compute_antic iterations",
2511 if (do_partial_partial
)
2513 /* For partial antic we ignore backedges and thus we do not need
2514 to perform any iteration when we process blocks in postorder. */
2515 for (i
= postorder
.length () - 1; i
>= 0; i
--)
2517 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2518 compute_partial_antic_aux (block
,
2519 bitmap_bit_p (has_abnormal_preds
,
2526 /* Inserted expressions are placed onto this worklist, which is used
2527 for performing quick dead code elimination of insertions we made
2528 that didn't turn out to be necessary. */
2529 static bitmap inserted_exprs
;
2531 /* The actual worker for create_component_ref_by_pieces. */
2534 create_component_ref_by_pieces_1 (basic_block block
, vn_reference_t ref
,
2535 unsigned int *operand
, gimple_seq
*stmts
)
2537 vn_reference_op_t currop
= &ref
->operands
[*operand
];
2540 switch (currop
->opcode
)
2547 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2551 tree offset
= currop
->op0
;
2552 if (TREE_CODE (baseop
) == ADDR_EXPR
2553 && handled_component_p (TREE_OPERAND (baseop
, 0)))
2557 base
= get_addr_base_and_unit_offset (TREE_OPERAND (baseop
, 0),
2560 offset
= int_const_binop (PLUS_EXPR
, offset
,
2561 build_int_cst (TREE_TYPE (offset
),
2563 baseop
= build_fold_addr_expr (base
);
2565 genop
= build2 (MEM_REF
, currop
->type
, baseop
, offset
);
2566 MR_DEPENDENCE_CLIQUE (genop
) = currop
->clique
;
2567 MR_DEPENDENCE_BASE (genop
) = currop
->base
;
2568 REF_REVERSE_STORAGE_ORDER (genop
) = currop
->reverse
;
2572 case TARGET_MEM_REF
:
2574 tree genop0
= NULL_TREE
, genop1
= NULL_TREE
;
2575 vn_reference_op_t nextop
= &ref
->operands
[(*operand
)++];
2576 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2582 genop0
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2588 genop1
= find_or_generate_expression (block
, nextop
->op0
, stmts
);
2592 genop
= build5 (TARGET_MEM_REF
, currop
->type
,
2593 baseop
, currop
->op2
, genop0
, currop
->op1
, genop1
);
2595 MR_DEPENDENCE_CLIQUE (genop
) = currop
->clique
;
2596 MR_DEPENDENCE_BASE (genop
) = currop
->base
;
2603 gcc_assert (is_gimple_min_invariant (currop
->op0
));
2609 case VIEW_CONVERT_EXPR
:
2611 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2615 return fold_build1 (currop
->opcode
, currop
->type
, genop0
);
2618 case WITH_SIZE_EXPR
:
2620 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2624 tree genop1
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2627 return fold_build2 (currop
->opcode
, currop
->type
, genop0
, genop1
);
2632 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2636 tree op1
= currop
->op0
;
2637 tree op2
= currop
->op1
;
2638 tree t
= build3 (BIT_FIELD_REF
, currop
->type
, genop0
, op1
, op2
);
2639 REF_REVERSE_STORAGE_ORDER (t
) = currop
->reverse
;
2643 /* For array ref vn_reference_op's, operand 1 of the array ref
2644 is op0 of the reference op and operand 3 of the array ref is
2646 case ARRAY_RANGE_REF
:
2650 tree genop1
= currop
->op0
;
2651 tree genop2
= currop
->op1
;
2652 tree genop3
= currop
->op2
;
2653 genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2657 genop1
= find_or_generate_expression (block
, genop1
, stmts
);
2662 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (genop0
));
2663 /* Drop zero minimum index if redundant. */
2664 if (integer_zerop (genop2
)
2666 || integer_zerop (TYPE_MIN_VALUE (domain_type
))))
2670 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2677 tree elmt_type
= TREE_TYPE (TREE_TYPE (genop0
));
2678 /* We can't always put a size in units of the element alignment
2679 here as the element alignment may be not visible. See
2680 PR43783. Simply drop the element size for constant
2682 if (TREE_CODE (genop3
) == INTEGER_CST
2683 && TREE_CODE (TYPE_SIZE_UNIT (elmt_type
)) == INTEGER_CST
2684 && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type
)),
2685 (wi::to_offset (genop3
)
2686 * vn_ref_op_align_unit (currop
))))
2690 genop3
= find_or_generate_expression (block
, genop3
, stmts
);
2695 return build4 (currop
->opcode
, currop
->type
, genop0
, genop1
,
2702 tree genop2
= currop
->op1
;
2703 op0
= create_component_ref_by_pieces_1 (block
, ref
, operand
, stmts
);
2706 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2710 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2714 return fold_build3 (COMPONENT_REF
, TREE_TYPE (op1
), op0
, op1
, genop2
);
2719 genop
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2741 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2742 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2743 trying to rename aggregates into ssa form directly, which is a no no.
2745 Thus, this routine doesn't create temporaries, it just builds a
2746 single access expression for the array, calling
2747 find_or_generate_expression to build the innermost pieces.
2749 This function is a subroutine of create_expression_by_pieces, and
2750 should not be called on it's own unless you really know what you
2754 create_component_ref_by_pieces (basic_block block
, vn_reference_t ref
,
2757 unsigned int op
= 0;
2758 return create_component_ref_by_pieces_1 (block
, ref
, &op
, stmts
);
2761 /* Find a simple leader for an expression, or generate one using
2762 create_expression_by_pieces from a NARY expression for the value.
2763 BLOCK is the basic_block we are looking for leaders in.
2764 OP is the tree expression to find a leader for or generate.
2765 Returns the leader or NULL_TREE on failure. */
2768 find_or_generate_expression (basic_block block
, tree op
, gimple_seq
*stmts
)
2770 /* Constants are always leaders. */
2771 if (is_gimple_min_invariant (op
))
2774 gcc_assert (TREE_CODE (op
) == SSA_NAME
);
2775 vn_ssa_aux_t info
= VN_INFO (op
);
2776 unsigned int lookfor
= info
->value_id
;
2777 if (value_id_constant_p (lookfor
))
2778 return info
->valnum
;
2780 pre_expr leader
= bitmap_find_leader (AVAIL_OUT (block
), lookfor
);
2783 if (leader
->kind
== NAME
)
2784 return PRE_EXPR_NAME (leader
);
2785 else if (leader
->kind
== CONSTANT
)
2786 return PRE_EXPR_CONSTANT (leader
);
2791 gcc_assert (!value_id_constant_p (lookfor
));
2793 /* It must be a complex expression, so generate it recursively. Note
2794 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2795 where the insert algorithm fails to insert a required expression. */
2796 bitmap exprset
= value_expressions
[lookfor
];
2799 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
2801 pre_expr temp
= expression_for_id (i
);
2802 /* We cannot insert random REFERENCE expressions at arbitrary
2803 places. We can insert NARYs which eventually re-materializes
2804 its operand values. */
2805 if (temp
->kind
== NARY
)
2806 return create_expression_by_pieces (block
, temp
, stmts
,
2814 /* Create an expression in pieces, so that we can handle very complex
2815 expressions that may be ANTIC, but not necessary GIMPLE.
2816 BLOCK is the basic block the expression will be inserted into,
2817 EXPR is the expression to insert (in value form)
2818 STMTS is a statement list to append the necessary insertions into.
2820 This function will die if we hit some value that shouldn't be
2821 ANTIC but is (IE there is no leader for it, or its components).
2822 The function returns NULL_TREE in case a different antic expression
2823 has to be inserted first.
2824 This function may also generate expressions that are themselves
2825 partially or fully redundant. Those that are will be either made
2826 fully redundant during the next iteration of insert (for partially
2827 redundant ones), or eliminated by eliminate (for fully redundant
2831 create_expression_by_pieces (basic_block block
, pre_expr expr
,
2832 gimple_seq
*stmts
, tree type
)
2836 gimple_seq forced_stmts
= NULL
;
2837 unsigned int value_id
;
2838 gimple_stmt_iterator gsi
;
2839 tree exprtype
= type
? type
: get_expr_type (expr
);
2845 /* We may hit the NAME/CONSTANT case if we have to convert types
2846 that value numbering saw through. */
2848 folded
= PRE_EXPR_NAME (expr
);
2849 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded
))
2851 if (useless_type_conversion_p (exprtype
, TREE_TYPE (folded
)))
2856 folded
= PRE_EXPR_CONSTANT (expr
);
2857 tree tem
= fold_convert (exprtype
, folded
);
2858 if (is_gimple_min_invariant (tem
))
2863 if (PRE_EXPR_REFERENCE (expr
)->operands
[0].opcode
== CALL_EXPR
)
2865 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2866 unsigned int operand
= 1;
2867 vn_reference_op_t currop
= &ref
->operands
[0];
2868 tree sc
= NULL_TREE
;
2869 tree fn
= NULL_TREE
;
2872 fn
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2878 sc
= find_or_generate_expression (block
, currop
->op1
, stmts
);
2882 auto_vec
<tree
> args (ref
->operands
.length () - 1);
2883 while (operand
< ref
->operands
.length ())
2885 tree arg
= create_component_ref_by_pieces_1 (block
, ref
,
2889 args
.quick_push (arg
);
2894 call
= gimple_build_call_vec (fn
, args
);
2895 gimple_call_set_fntype (call
, currop
->type
);
2898 call
= gimple_build_call_internal_vec ((internal_fn
)currop
->clique
,
2900 gimple_set_location (call
, expr
->loc
);
2902 gimple_call_set_chain (call
, sc
);
2903 tree forcedname
= make_ssa_name (ref
->type
);
2904 gimple_call_set_lhs (call
, forcedname
);
2905 /* There's no CCP pass after PRE which would re-compute alignment
2906 information so make sure we re-materialize this here. */
2907 if (gimple_call_builtin_p (call
, BUILT_IN_ASSUME_ALIGNED
)
2908 && args
.length () - 2 <= 1
2909 && tree_fits_uhwi_p (args
[1])
2910 && (args
.length () != 3 || tree_fits_uhwi_p (args
[2])))
2912 unsigned HOST_WIDE_INT halign
= tree_to_uhwi (args
[1]);
2913 unsigned HOST_WIDE_INT hmisalign
2914 = args
.length () == 3 ? tree_to_uhwi (args
[2]) : 0;
2915 if ((halign
& (halign
- 1)) == 0
2916 && (hmisalign
& ~(halign
- 1)) == 0
2917 && (unsigned int)halign
!= 0)
2918 set_ptr_info_alignment (get_ptr_info (forcedname
),
2921 gimple_set_vuse (call
, BB_LIVE_VOP_ON_EXIT (block
));
2922 gimple_seq_add_stmt_without_update (&forced_stmts
, call
);
2923 folded
= forcedname
;
2927 folded
= create_component_ref_by_pieces (block
,
2928 PRE_EXPR_REFERENCE (expr
),
2932 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2933 newstmt
= gimple_build_assign (name
, folded
);
2934 gimple_set_location (newstmt
, expr
->loc
);
2935 gimple_seq_add_stmt_without_update (&forced_stmts
, newstmt
);
2936 gimple_set_vuse (newstmt
, BB_LIVE_VOP_ON_EXIT (block
));
2942 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2943 tree
*genop
= XALLOCAVEC (tree
, nary
->length
);
2945 for (i
= 0; i
< nary
->length
; ++i
)
2947 genop
[i
] = find_or_generate_expression (block
, nary
->op
[i
], stmts
);
2950 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2951 may have conversions stripped. */
2952 if (nary
->opcode
== POINTER_PLUS_EXPR
)
2955 genop
[i
] = gimple_convert (&forced_stmts
,
2956 nary
->type
, genop
[i
]);
2958 genop
[i
] = gimple_convert (&forced_stmts
,
2959 sizetype
, genop
[i
]);
2962 genop
[i
] = gimple_convert (&forced_stmts
,
2963 TREE_TYPE (nary
->op
[i
]), genop
[i
]);
2965 if (nary
->opcode
== CONSTRUCTOR
)
2967 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
2968 for (i
= 0; i
< nary
->length
; ++i
)
2969 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
, genop
[i
]);
2970 folded
= build_constructor (nary
->type
, elts
);
2971 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2972 newstmt
= gimple_build_assign (name
, folded
);
2973 gimple_set_location (newstmt
, expr
->loc
);
2974 gimple_seq_add_stmt_without_update (&forced_stmts
, newstmt
);
2979 switch (nary
->length
)
2982 folded
= gimple_build (&forced_stmts
, expr
->loc
,
2983 nary
->opcode
, nary
->type
, genop
[0]);
2986 folded
= gimple_build (&forced_stmts
, expr
->loc
, nary
->opcode
,
2987 nary
->type
, genop
[0], genop
[1]);
2990 folded
= gimple_build (&forced_stmts
, expr
->loc
, nary
->opcode
,
2991 nary
->type
, genop
[0], genop
[1],
3004 folded
= gimple_convert (&forced_stmts
, exprtype
, folded
);
3006 /* If there is nothing to insert, return the simplified result. */
3007 if (gimple_seq_empty_p (forced_stmts
))
3009 /* If we simplified to a constant return it and discard eventually
3011 if (is_gimple_min_invariant (folded
))
3013 gimple_seq_discard (forced_stmts
);
3016 /* Likewise if we simplified to sth not queued for insertion. */
3018 gsi
= gsi_last (forced_stmts
);
3019 for (; !gsi_end_p (gsi
); gsi_prev (&gsi
))
3021 gimple
*stmt
= gsi_stmt (gsi
);
3022 tree forcedname
= gimple_get_lhs (stmt
);
3023 if (forcedname
== folded
)
3031 gimple_seq_discard (forced_stmts
);
3034 gcc_assert (TREE_CODE (folded
) == SSA_NAME
);
3036 /* If we have any intermediate expressions to the value sets, add them
3037 to the value sets and chain them in the instruction stream. */
3040 gsi
= gsi_start (forced_stmts
);
3041 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
3043 gimple
*stmt
= gsi_stmt (gsi
);
3044 tree forcedname
= gimple_get_lhs (stmt
);
3047 if (forcedname
!= folded
)
3049 vn_ssa_aux_t vn_info
= VN_INFO (forcedname
);
3050 vn_info
->valnum
= forcedname
;
3051 vn_info
->value_id
= get_next_value_id ();
3052 nameexpr
= get_or_alloc_expr_for_name (forcedname
);
3053 add_to_value (vn_info
->value_id
, nameexpr
);
3054 if (NEW_SETS (block
))
3055 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
3056 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
3059 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (forcedname
));
3061 gimple_seq_add_seq (stmts
, forced_stmts
);
3066 /* Fold the last statement. */
3067 gsi
= gsi_last (*stmts
);
3068 if (fold_stmt_inplace (&gsi
))
3069 update_stmt (gsi_stmt (gsi
));
3071 /* Add a value number to the temporary.
3072 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
3073 we are creating the expression by pieces, and this particular piece of
3074 the expression may have been represented. There is no harm in replacing
3076 value_id
= get_expr_value_id (expr
);
3077 vn_ssa_aux_t vn_info
= VN_INFO (name
);
3078 vn_info
->value_id
= value_id
;
3079 vn_info
->valnum
= vn_valnum_from_value_id (value_id
);
3080 if (vn_info
->valnum
== NULL_TREE
)
3081 vn_info
->valnum
= name
;
3082 gcc_assert (vn_info
->valnum
!= NULL_TREE
);
3083 nameexpr
= get_or_alloc_expr_for_name (name
);
3084 add_to_value (value_id
, nameexpr
);
3085 if (NEW_SETS (block
))
3086 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
3087 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
3089 pre_stats
.insertions
++;
3090 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3092 fprintf (dump_file
, "Inserted ");
3093 print_gimple_stmt (dump_file
, gsi_stmt (gsi_last (*stmts
)), 0);
3094 fprintf (dump_file
, " in predecessor %d (%04d)\n",
3095 block
->index
, value_id
);
3102 /* Insert the to-be-made-available values of expression EXPRNUM for each
3103 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3104 merge the result with a phi node, given the same value number as
3105 NODE. Return true if we have inserted new stuff. */
3108 insert_into_preds_of_block (basic_block block
, unsigned int exprnum
,
3109 vec
<pre_expr
> &avail
)
3111 pre_expr expr
= expression_for_id (exprnum
);
3113 unsigned int val
= get_expr_value_id (expr
);
3115 bool insertions
= false;
3120 tree type
= get_expr_type (expr
);
3124 /* Make sure we aren't creating an induction variable. */
3125 if (bb_loop_depth (block
) > 0 && EDGE_COUNT (block
->preds
) == 2)
3127 bool firstinsideloop
= false;
3128 bool secondinsideloop
= false;
3129 firstinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3130 EDGE_PRED (block
, 0)->src
);
3131 secondinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3132 EDGE_PRED (block
, 1)->src
);
3133 /* Induction variables only have one edge inside the loop. */
3134 if ((firstinsideloop
^ secondinsideloop
)
3135 && expr
->kind
!= REFERENCE
)
3137 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3138 fprintf (dump_file
, "Skipping insertion of phi for partial "
3139 "redundancy: Looks like an induction variable\n");
3144 /* Make the necessary insertions. */
3145 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3147 /* When we are not inserting a PHI node do not bother inserting
3148 into places that do not dominate the anticipated computations. */
3149 if (nophi
&& !dominated_by_p (CDI_DOMINATORS
, block
, pred
->src
))
3151 gimple_seq stmts
= NULL
;
3154 eprime
= avail
[pred
->dest_idx
];
3155 builtexpr
= create_expression_by_pieces (bprime
, eprime
,
3157 gcc_assert (!(pred
->flags
& EDGE_ABNORMAL
));
3158 if (!gimple_seq_empty_p (stmts
))
3160 basic_block new_bb
= gsi_insert_seq_on_edge_immediate (pred
, stmts
);
3161 gcc_assert (! new_bb
);
3166 /* We cannot insert a PHI node if we failed to insert
3171 if (is_gimple_min_invariant (builtexpr
))
3172 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_constant (builtexpr
);
3174 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_name (builtexpr
);
3176 /* If we didn't want a phi node, and we made insertions, we still have
3177 inserted new stuff, and thus return true. If we didn't want a phi node,
3178 and didn't make insertions, we haven't added anything new, so return
3180 if (nophi
&& insertions
)
3182 else if (nophi
&& !insertions
)
3185 /* Now build a phi for the new variable. */
3186 temp
= make_temp_ssa_name (type
, NULL
, "prephitmp");
3187 phi
= create_phi_node (temp
, block
);
3189 vn_ssa_aux_t vn_info
= VN_INFO (temp
);
3190 vn_info
->value_id
= val
;
3191 vn_info
->valnum
= vn_valnum_from_value_id (val
);
3192 if (vn_info
->valnum
== NULL_TREE
)
3193 vn_info
->valnum
= temp
;
3194 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3195 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3197 pre_expr ae
= avail
[pred
->dest_idx
];
3198 gcc_assert (get_expr_type (ae
) == type
3199 || useless_type_conversion_p (type
, get_expr_type (ae
)));
3200 if (ae
->kind
== CONSTANT
)
3201 add_phi_arg (phi
, unshare_expr (PRE_EXPR_CONSTANT (ae
)),
3202 pred
, UNKNOWN_LOCATION
);
3204 add_phi_arg (phi
, PRE_EXPR_NAME (ae
), pred
, UNKNOWN_LOCATION
);
3207 newphi
= get_or_alloc_expr_for_name (temp
);
3208 add_to_value (val
, newphi
);
3210 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3211 this insertion, since we test for the existence of this value in PHI_GEN
3212 before proceeding with the partial redundancy checks in insert_aux.
3214 The value may exist in AVAIL_OUT, in particular, it could be represented
3215 by the expression we are trying to eliminate, in which case we want the
3216 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3219 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3220 this block, because if it did, it would have existed in our dominator's
3221 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3224 bitmap_insert_into_set (PHI_GEN (block
), newphi
);
3225 bitmap_value_replace_in_set (AVAIL_OUT (block
),
3227 if (NEW_SETS (block
))
3228 bitmap_insert_into_set (NEW_SETS (block
), newphi
);
3230 /* If we insert a PHI node for a conversion of another PHI node
3231 in the same basic-block try to preserve range information.
3232 This is important so that followup loop passes receive optimal
3233 number of iteration analysis results. See PR61743. */
3234 if (expr
->kind
== NARY
3235 && CONVERT_EXPR_CODE_P (expr
->u
.nary
->opcode
)
3236 && TREE_CODE (expr
->u
.nary
->op
[0]) == SSA_NAME
3237 && gimple_bb (SSA_NAME_DEF_STMT (expr
->u
.nary
->op
[0])) == block
3238 && INTEGRAL_TYPE_P (type
)
3239 && INTEGRAL_TYPE_P (TREE_TYPE (expr
->u
.nary
->op
[0]))
3240 && (TYPE_PRECISION (type
)
3241 >= TYPE_PRECISION (TREE_TYPE (expr
->u
.nary
->op
[0])))
3242 && SSA_NAME_RANGE_INFO (expr
->u
.nary
->op
[0]))
3245 if (get_range_query (cfun
)->range_of_expr (r
, expr
->u
.nary
->op
[0])
3246 && r
.kind () == VR_RANGE
3247 && !wi::neg_p (r
.lower_bound (), SIGNED
)
3248 && !wi::neg_p (r
.upper_bound (), SIGNED
))
3249 /* Just handle extension and sign-changes of all-positive ranges. */
3250 set_range_info (temp
, VR_RANGE
,
3251 wide_int_storage::from (r
.lower_bound (),
3252 TYPE_PRECISION (type
),
3254 wide_int_storage::from (r
.upper_bound (),
3255 TYPE_PRECISION (type
),
3259 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3261 fprintf (dump_file
, "Created phi ");
3262 print_gimple_stmt (dump_file
, phi
, 0);
3263 fprintf (dump_file
, " in block %d (%04d)\n", block
->index
, val
);
3271 /* Perform insertion of partially redundant or hoistable values.
3272 For BLOCK, do the following:
3273 1. Propagate the NEW_SETS of the dominator into the current block.
3274 If the block has multiple predecessors,
3275 2a. Iterate over the ANTIC expressions for the block to see if
3276 any of them are partially redundant.
3277 2b. If so, insert them into the necessary predecessors to make
3278 the expression fully redundant.
3279 2c. Insert a new PHI merging the values of the predecessors.
3280 2d. Insert the new PHI, and the new expressions, into the
3282 If the block has multiple successors,
3283 3a. Iterate over the ANTIC values for the block to see if
3284 any of them are good candidates for hoisting.
3285 3b. If so, insert expressions computing the values in BLOCK,
3286 and add the new expressions into the NEW_SETS set.
3287 4. Recursively call ourselves on the dominator children of BLOCK.
3289 Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3290 do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3291 done in do_hoist_insertion.
3295 do_pre_regular_insertion (basic_block block
, basic_block dom
,
3296 vec
<pre_expr
> exprs
)
3298 bool new_stuff
= false;
3300 auto_vec
<pre_expr
, 2> avail
;
3303 avail
.safe_grow (EDGE_COUNT (block
->preds
), true);
3305 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3307 if (expr
->kind
== NARY
3308 || expr
->kind
== REFERENCE
)
3311 bool by_some
= false;
3312 bool cant_insert
= false;
3313 bool all_same
= true;
3314 pre_expr first_s
= NULL
;
3317 pre_expr eprime
= NULL
;
3319 pre_expr edoubleprime
= NULL
;
3320 bool do_insertion
= false;
3322 val
= get_expr_value_id (expr
);
3323 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3325 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3327 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3329 fprintf (dump_file
, "Found fully redundant value: ");
3330 print_pre_expr (dump_file
, expr
);
3331 fprintf (dump_file
, "\n");
3336 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3338 unsigned int vprime
;
3340 /* We should never run insertion for the exit block
3341 and so not come across fake pred edges. */
3342 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3344 /* We are looking at ANTIC_OUT of bprime. */
3345 eprime
= phi_translate (NULL
, expr
, ANTIC_IN (block
), NULL
, pred
);
3347 /* eprime will generally only be NULL if the
3348 value of the expression, translated
3349 through the PHI for this predecessor, is
3350 undefined. If that is the case, we can't
3351 make the expression fully redundant,
3352 because its value is undefined along a
3353 predecessor path. We can thus break out
3354 early because it doesn't matter what the
3355 rest of the results are. */
3358 avail
[pred
->dest_idx
] = NULL
;
3363 vprime
= get_expr_value_id (eprime
);
3364 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
),
3366 if (edoubleprime
== NULL
)
3368 avail
[pred
->dest_idx
] = eprime
;
3373 avail
[pred
->dest_idx
] = edoubleprime
;
3375 /* We want to perform insertions to remove a redundancy on
3376 a path in the CFG we want to optimize for speed. */
3377 if (optimize_edge_for_speed_p (pred
))
3378 do_insertion
= true;
3379 if (first_s
== NULL
)
3380 first_s
= edoubleprime
;
3381 else if (!pre_expr_d::equal (first_s
, edoubleprime
))
3385 /* If we can insert it, it's not the same value
3386 already existing along every predecessor, and
3387 it's defined by some predecessor, it is
3388 partially redundant. */
3389 if (!cant_insert
&& !all_same
&& by_some
)
3393 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3395 fprintf (dump_file
, "Skipping partial redundancy for "
3397 print_pre_expr (dump_file
, expr
);
3398 fprintf (dump_file
, " (%04d), no redundancy on to be "
3399 "optimized for speed edge\n", val
);
3402 else if (dbg_cnt (treepre_insert
))
3404 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3406 fprintf (dump_file
, "Found partial redundancy for "
3408 print_pre_expr (dump_file
, expr
);
3409 fprintf (dump_file
, " (%04d)\n",
3410 get_expr_value_id (expr
));
3412 if (insert_into_preds_of_block (block
,
3413 get_expression_id (expr
),
3418 /* If all edges produce the same value and that value is
3419 an invariant, then the PHI has the same value on all
3420 edges. Note this. */
3421 else if (!cant_insert
3423 && (edoubleprime
->kind
!= NAME
3424 || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI
3425 (PRE_EXPR_NAME (edoubleprime
))))
3427 gcc_assert (edoubleprime
->kind
== CONSTANT
3428 || edoubleprime
->kind
== NAME
);
3430 tree temp
= make_temp_ssa_name (get_expr_type (expr
),
3433 = gimple_build_assign (temp
,
3434 edoubleprime
->kind
== CONSTANT
?
3435 PRE_EXPR_CONSTANT (edoubleprime
) :
3436 PRE_EXPR_NAME (edoubleprime
));
3437 gimple_stmt_iterator gsi
= gsi_after_labels (block
);
3438 gsi_insert_before (&gsi
, assign
, GSI_NEW_STMT
);
3440 vn_ssa_aux_t vn_info
= VN_INFO (temp
);
3441 vn_info
->value_id
= val
;
3442 vn_info
->valnum
= vn_valnum_from_value_id (val
);
3443 if (vn_info
->valnum
== NULL_TREE
)
3444 vn_info
->valnum
= temp
;
3445 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3446 pre_expr newe
= get_or_alloc_expr_for_name (temp
);
3447 add_to_value (val
, newe
);
3448 bitmap_value_replace_in_set (AVAIL_OUT (block
), newe
);
3449 bitmap_insert_into_set (NEW_SETS (block
), newe
);
3450 bitmap_insert_into_set (PHI_GEN (block
), newe
);
3459 /* Perform insertion for partially anticipatable expressions. There
3460 is only one case we will perform insertion for these. This case is
3461 if the expression is partially anticipatable, and fully available.
3462 In this case, we know that putting it earlier will enable us to
3463 remove the later computation. */
3466 do_pre_partial_partial_insertion (basic_block block
, basic_block dom
,
3467 vec
<pre_expr
> exprs
)
3469 bool new_stuff
= false;
3471 auto_vec
<pre_expr
, 2> avail
;
3474 avail
.safe_grow (EDGE_COUNT (block
->preds
), true);
3476 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3478 if (expr
->kind
== NARY
3479 || expr
->kind
== REFERENCE
)
3483 bool cant_insert
= false;
3486 pre_expr eprime
= NULL
;
3489 val
= get_expr_value_id (expr
);
3490 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3492 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3495 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3497 unsigned int vprime
;
3498 pre_expr edoubleprime
;
3500 /* We should never run insertion for the exit block
3501 and so not come across fake pred edges. */
3502 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3504 eprime
= phi_translate (NULL
, expr
, ANTIC_IN (block
),
3505 PA_IN (block
), pred
);
3507 /* eprime will generally only be NULL if the
3508 value of the expression, translated
3509 through the PHI for this predecessor, is
3510 undefined. If that is the case, we can't
3511 make the expression fully redundant,
3512 because its value is undefined along a
3513 predecessor path. We can thus break out
3514 early because it doesn't matter what the
3515 rest of the results are. */
3518 avail
[pred
->dest_idx
] = NULL
;
3523 vprime
= get_expr_value_id (eprime
);
3524 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
), vprime
);
3525 avail
[pred
->dest_idx
] = edoubleprime
;
3526 if (edoubleprime
== NULL
)
3533 /* If we can insert it, it's not the same value
3534 already existing along every predecessor, and
3535 it's defined by some predecessor, it is
3536 partially redundant. */
3537 if (!cant_insert
&& by_all
)
3540 bool do_insertion
= false;
3542 /* Insert only if we can remove a later expression on a path
3543 that we want to optimize for speed.
3544 The phi node that we will be inserting in BLOCK is not free,
3545 and inserting it for the sake of !optimize_for_speed successor
3546 may cause regressions on the speed path. */
3547 FOR_EACH_EDGE (succ
, ei
, block
->succs
)
3549 if (bitmap_set_contains_value (PA_IN (succ
->dest
), val
)
3550 || bitmap_set_contains_value (ANTIC_IN (succ
->dest
), val
))
3552 if (optimize_edge_for_speed_p (succ
))
3553 do_insertion
= true;
3559 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3561 fprintf (dump_file
, "Skipping partial partial redundancy "
3563 print_pre_expr (dump_file
, expr
);
3564 fprintf (dump_file
, " (%04d), not (partially) anticipated "
3565 "on any to be optimized for speed edges\n", val
);
3568 else if (dbg_cnt (treepre_insert
))
3570 pre_stats
.pa_insert
++;
3571 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3573 fprintf (dump_file
, "Found partial partial redundancy "
3575 print_pre_expr (dump_file
, expr
);
3576 fprintf (dump_file
, " (%04d)\n",
3577 get_expr_value_id (expr
));
3579 if (insert_into_preds_of_block (block
,
3580 get_expression_id (expr
),
3591 /* Insert expressions in BLOCK to compute hoistable values up.
3592 Return TRUE if something was inserted, otherwise return FALSE.
3593 The caller has to make sure that BLOCK has at least two successors. */
3596 do_hoist_insertion (basic_block block
)
3600 bool new_stuff
= false;
3602 gimple_stmt_iterator last
;
3604 /* At least two successors, or else... */
3605 gcc_assert (EDGE_COUNT (block
->succs
) >= 2);
3607 /* Check that all successors of BLOCK are dominated by block.
3608 We could use dominated_by_p() for this, but actually there is a much
3609 quicker check: any successor that is dominated by BLOCK can't have
3610 more than one predecessor edge. */
3611 FOR_EACH_EDGE (e
, ei
, block
->succs
)
3612 if (! single_pred_p (e
->dest
))
3615 /* Determine the insertion point. If we cannot safely insert before
3616 the last stmt if we'd have to, bail out. */
3617 last
= gsi_last_bb (block
);
3618 if (!gsi_end_p (last
)
3619 && !is_ctrl_stmt (gsi_stmt (last
))
3620 && stmt_ends_bb_p (gsi_stmt (last
)))
3623 /* Compute the set of hoistable expressions from ANTIC_IN. First compute
3624 hoistable values. */
3625 bitmap_set hoistable_set
;
3627 /* A hoistable value must be in ANTIC_IN(block)
3628 but not in AVAIL_OUT(BLOCK). */
3629 bitmap_initialize (&hoistable_set
.values
, &grand_bitmap_obstack
);
3630 bitmap_and_compl (&hoistable_set
.values
,
3631 &ANTIC_IN (block
)->values
, &AVAIL_OUT (block
)->values
);
3633 /* Short-cut for a common case: hoistable_set is empty. */
3634 if (bitmap_empty_p (&hoistable_set
.values
))
3637 /* Compute which of the hoistable values is in AVAIL_OUT of
3638 at least one of the successors of BLOCK. */
3639 bitmap_head availout_in_some
;
3640 bitmap_initialize (&availout_in_some
, &grand_bitmap_obstack
);
3641 FOR_EACH_EDGE (e
, ei
, block
->succs
)
3642 /* Do not consider expressions solely because their availability
3643 on loop exits. They'd be ANTIC-IN throughout the whole loop
3644 and thus effectively hoisted across loops by combination of
3645 PRE and hoisting. */
3646 if (! loop_exit_edge_p (block
->loop_father
, e
))
3647 bitmap_ior_and_into (&availout_in_some
, &hoistable_set
.values
,
3648 &AVAIL_OUT (e
->dest
)->values
);
3649 bitmap_clear (&hoistable_set
.values
);
3651 /* Short-cut for a common case: availout_in_some is empty. */
3652 if (bitmap_empty_p (&availout_in_some
))
3655 /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */
3656 bitmap_move (&hoistable_set
.values
, &availout_in_some
);
3657 hoistable_set
.expressions
= ANTIC_IN (block
)->expressions
;
3659 /* Now finally construct the topological-ordered expression set. */
3660 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (&hoistable_set
);
3662 bitmap_clear (&hoistable_set
.values
);
3664 /* If there are candidate values for hoisting, insert expressions
3665 strategically to make the hoistable expressions fully redundant. */
3667 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3669 /* While we try to sort expressions topologically above the
3670 sorting doesn't work out perfectly. Catch expressions we
3671 already inserted. */
3672 unsigned int value_id
= get_expr_value_id (expr
);
3673 if (bitmap_set_contains_value (AVAIL_OUT (block
), value_id
))
3675 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3678 "Already inserted expression for ");
3679 print_pre_expr (dump_file
, expr
);
3680 fprintf (dump_file
, " (%04d)\n", value_id
);
3685 /* If we end up with a punned expression representation and this
3686 happens to be a float typed one give up - we can't know for
3687 sure whether all paths perform the floating-point load we are
3688 about to insert and on some targets this can cause correctness
3689 issues. See PR88240. */
3690 if (expr
->kind
== REFERENCE
3691 && PRE_EXPR_REFERENCE (expr
)->punned
3692 && FLOAT_TYPE_P (get_expr_type (expr
)))
3695 /* OK, we should hoist this value. Perform the transformation. */
3696 pre_stats
.hoist_insert
++;
3697 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3700 "Inserting expression in block %d for code hoisting: ",
3702 print_pre_expr (dump_file
, expr
);
3703 fprintf (dump_file
, " (%04d)\n", value_id
);
3706 gimple_seq stmts
= NULL
;
3707 tree res
= create_expression_by_pieces (block
, expr
, &stmts
,
3708 get_expr_type (expr
));
3710 /* Do not return true if expression creation ultimately
3711 did not insert any statements. */
3712 if (gimple_seq_empty_p (stmts
))
3716 if (gsi_end_p (last
) || is_ctrl_stmt (gsi_stmt (last
)))
3717 gsi_insert_seq_before (&last
, stmts
, GSI_SAME_STMT
);
3719 gsi_insert_seq_after (&last
, stmts
, GSI_NEW_STMT
);
3722 /* Make sure to not return true if expression creation ultimately
3723 failed but also make sure to insert any stmts produced as they
3724 are tracked in inserted_exprs. */
3736 /* Perform insertion of partially redundant and hoistable values. */
3743 FOR_ALL_BB_FN (bb
, cfun
)
3744 NEW_SETS (bb
) = bitmap_set_new ();
3746 int *rpo
= XNEWVEC (int, n_basic_blocks_for_fn (cfun
));
3747 int *bb_rpo
= XNEWVEC (int, last_basic_block_for_fn (cfun
) + 1);
3748 int rpo_num
= pre_and_rev_post_order_compute (NULL
, rpo
, false);
3749 for (int i
= 0; i
< rpo_num
; ++i
)
3752 int num_iterations
= 0;
3757 if (dump_file
&& dump_flags
& TDF_DETAILS
)
3758 fprintf (dump_file
, "Starting insert iteration %d\n", num_iterations
);
3761 for (int idx
= 0; idx
< rpo_num
; ++idx
)
3763 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[idx
]);
3764 basic_block dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3769 bitmap_set_t newset
;
3771 /* First, update the AVAIL_OUT set with anything we may have
3772 inserted higher up in the dominator tree. */
3773 newset
= NEW_SETS (dom
);
3775 /* Note that we need to value_replace both NEW_SETS, and
3776 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3777 represented by some non-simple expression here that we want
3778 to replace it with. */
3779 bool avail_out_changed
= false;
3780 FOR_EACH_EXPR_ID_IN_SET (newset
, i
, bi
)
3782 pre_expr expr
= expression_for_id (i
);
3783 bitmap_value_replace_in_set (NEW_SETS (block
), expr
);
3785 |= bitmap_value_replace_in_set (AVAIL_OUT (block
), expr
);
3787 /* We need to iterate if AVAIL_OUT of an already processed
3788 block source changed. */
3789 if (avail_out_changed
&& !changed
)
3793 FOR_EACH_EDGE (e
, ei
, block
->succs
)
3794 if (e
->dest
->index
!= EXIT_BLOCK
3795 && bb_rpo
[e
->dest
->index
] < idx
)
3799 /* Insert expressions for partial redundancies. */
3800 if (flag_tree_pre
&& !single_pred_p (block
))
3803 = sorted_array_from_bitmap_set (ANTIC_IN (block
));
3804 /* Sorting is not perfect, iterate locally. */
3805 while (do_pre_regular_insertion (block
, dom
, exprs
))
3808 if (do_partial_partial
)
3810 exprs
= sorted_array_from_bitmap_set (PA_IN (block
));
3811 while (do_pre_partial_partial_insertion (block
, dom
,
3820 /* Clear the NEW sets before the next iteration. We have already
3821 fully propagated its contents. */
3823 FOR_ALL_BB_FN (bb
, cfun
)
3824 bitmap_set_free (NEW_SETS (bb
));
3828 statistics_histogram_event (cfun
, "insert iterations", num_iterations
);
3830 /* AVAIL_OUT is not needed after insertion so we don't have to
3831 propagate NEW_SETS from hoist insertion. */
3832 FOR_ALL_BB_FN (bb
, cfun
)
3834 bitmap_set_free (NEW_SETS (bb
));
3835 bitmap_set_pool
.remove (NEW_SETS (bb
));
3836 NEW_SETS (bb
) = NULL
;
3839 /* Insert expressions for hoisting. Do a backward walk here since
3840 inserting into BLOCK exposes new opportunities in its predecessors.
3841 Since PRE and hoist insertions can cause back-to-back iteration
3842 and we are interested in PRE insertion exposed hoisting opportunities
3843 but not in hoisting exposed PRE ones do hoist insertion only after
3844 PRE insertion iteration finished and do not iterate it. */
3845 if (flag_code_hoisting
)
3846 for (int idx
= rpo_num
- 1; idx
>= 0; --idx
)
3848 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, rpo
[idx
]);
3849 if (EDGE_COUNT (block
->succs
) >= 2)
3850 changed
|= do_hoist_insertion (block
);
3858 /* Compute the AVAIL set for all basic blocks.
3860 This function performs value numbering of the statements in each basic
3861 block. The AVAIL sets are built from information we glean while doing
3862 this value numbering, since the AVAIL sets contain only one entry per
3865 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3866 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3869 compute_avail (function
*fun
)
3872 basic_block block
, son
;
3873 basic_block
*worklist
;
3878 /* We pretend that default definitions are defined in the entry block.
3879 This includes function arguments and the static chain decl. */
3880 FOR_EACH_SSA_NAME (i
, name
, fun
)
3883 if (!SSA_NAME_IS_DEFAULT_DEF (name
)
3884 || has_zero_uses (name
)
3885 || virtual_operand_p (name
))
3888 e
= get_or_alloc_expr_for_name (name
);
3889 add_to_value (get_expr_value_id (e
), e
);
3890 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun
)), e
);
3891 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun
)),
3895 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3897 print_bitmap_set (dump_file
, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun
)),
3898 "tmp_gen", ENTRY_BLOCK
);
3899 print_bitmap_set (dump_file
, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun
)),
3900 "avail_out", ENTRY_BLOCK
);
3903 /* Allocate the worklist. */
3904 worklist
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (fun
));
3906 /* Seed the algorithm by putting the dominator children of the entry
3907 block on the worklist. */
3908 for (son
= first_dom_son (CDI_DOMINATORS
, ENTRY_BLOCK_PTR_FOR_FN (fun
));
3910 son
= next_dom_son (CDI_DOMINATORS
, son
))
3911 worklist
[sp
++] = son
;
3913 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (fun
))
3914 = ssa_default_def (fun
, gimple_vop (fun
));
3916 /* Loop until the worklist is empty. */
3922 /* Pick a block from the worklist. */
3923 block
= worklist
[--sp
];
3924 vn_context_bb
= block
;
3926 /* Initially, the set of available values in BLOCK is that of
3927 its immediate dominator. */
3928 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3931 bitmap_set_copy (AVAIL_OUT (block
), AVAIL_OUT (dom
));
3932 BB_LIVE_VOP_ON_EXIT (block
) = BB_LIVE_VOP_ON_EXIT (dom
);
3935 /* Generate values for PHI nodes. */
3936 for (gphi_iterator gsi
= gsi_start_phis (block
); !gsi_end_p (gsi
);
3939 tree result
= gimple_phi_result (gsi
.phi ());
3941 /* We have no need for virtual phis, as they don't represent
3942 actual computations. */
3943 if (virtual_operand_p (result
))
3945 BB_LIVE_VOP_ON_EXIT (block
) = result
;
3949 pre_expr e
= get_or_alloc_expr_for_name (result
);
3950 add_to_value (get_expr_value_id (e
), e
);
3951 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3952 bitmap_insert_into_set (PHI_GEN (block
), e
);
3955 BB_MAY_NOTRETURN (block
) = 0;
3957 /* Now compute value numbers and populate value sets with all
3958 the expressions computed in BLOCK. */
3959 bool set_bb_may_notreturn
= false;
3960 for (gimple_stmt_iterator gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
);
3966 stmt
= gsi_stmt (gsi
);
3968 if (set_bb_may_notreturn
)
3970 BB_MAY_NOTRETURN (block
) = 1;
3971 set_bb_may_notreturn
= false;
3974 /* Cache whether the basic-block has any non-visible side-effect
3976 If this isn't a call or it is the last stmt in the
3977 basic-block then the CFG represents things correctly. */
3978 if (is_gimple_call (stmt
) && !stmt_ends_bb_p (stmt
))
3980 /* Non-looping const functions always return normally.
3981 Otherwise the call might not return or have side-effects
3982 that forbids hoisting possibly trapping expressions
3984 int flags
= gimple_call_flags (stmt
);
3985 if (!(flags
& (ECF_CONST
|ECF_PURE
))
3986 || (flags
& ECF_LOOPING_CONST_OR_PURE
)
3987 || stmt_can_throw_external (fun
, stmt
))
3988 /* Defer setting of BB_MAY_NOTRETURN to avoid it
3989 influencing the processing of the call itself. */
3990 set_bb_may_notreturn
= true;
3993 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
3995 pre_expr e
= get_or_alloc_expr_for_name (op
);
3996 add_to_value (get_expr_value_id (e
), e
);
3997 bitmap_insert_into_set (TMP_GEN (block
), e
);
3998 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
4001 if (gimple_vdef (stmt
))
4002 BB_LIVE_VOP_ON_EXIT (block
) = gimple_vdef (stmt
);
4004 if (gimple_has_side_effects (stmt
)
4005 || stmt_could_throw_p (fun
, stmt
)
4006 || is_gimple_debug (stmt
))
4009 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
4011 if (ssa_undefined_value_p (op
))
4013 pre_expr e
= get_or_alloc_expr_for_name (op
);
4014 bitmap_value_insert_into_set (EXP_GEN (block
), e
);
4017 switch (gimple_code (stmt
))
4025 vn_reference_s ref1
;
4026 pre_expr result
= NULL
;
4028 vn_reference_lookup_call (as_a
<gcall
*> (stmt
), &ref
, &ref1
);
4029 /* There is no point to PRE a call without a value. */
4030 if (!ref
|| !ref
->result
)
4033 /* If the value of the call is not invalidated in
4034 this block until it is computed, add the expression
4036 if ((!gimple_vuse (stmt
)
4038 (SSA_NAME_DEF_STMT (gimple_vuse (stmt
))) == GIMPLE_PHI
4039 || gimple_bb (SSA_NAME_DEF_STMT
4040 (gimple_vuse (stmt
))) != block
)
4041 /* If the REFERENCE traps and there was a preceding
4042 point in the block that might not return avoid
4043 adding the reference to EXP_GEN. */
4044 && (!BB_MAY_NOTRETURN (block
)
4045 || !vn_reference_may_trap (ref
)))
4047 result
= get_or_alloc_expr_for_reference
4048 (ref
, gimple_location (stmt
));
4049 add_to_value (get_expr_value_id (result
), result
);
4050 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
4057 pre_expr result
= NULL
;
4058 switch (vn_get_stmt_kind (stmt
))
4062 enum tree_code code
= gimple_assign_rhs_code (stmt
);
4065 /* COND_EXPR is awkward in that it contains an
4066 embedded complex expression.
4067 Don't even try to shove it through PRE. */
4068 if (code
== COND_EXPR
)
4071 vn_nary_op_lookup_stmt (stmt
, &nary
);
4072 if (!nary
|| nary
->predicated_values
)
4075 unsigned value_id
= nary
->value_id
;
4076 if (value_id_constant_p (value_id
))
4079 /* Record the un-valueized expression for EXP_GEN. */
4080 nary
= XALLOCAVAR (struct vn_nary_op_s
,
4082 (vn_nary_length_from_stmt (stmt
)));
4083 init_vn_nary_op_from_stmt (nary
, as_a
<gassign
*> (stmt
));
4085 /* If the NARY traps and there was a preceding
4086 point in the block that might not return avoid
4087 adding the nary to EXP_GEN. */
4088 if (BB_MAY_NOTRETURN (block
)
4089 && vn_nary_may_trap (nary
))
4092 result
= get_or_alloc_expr_for_nary
4093 (nary
, value_id
, gimple_location (stmt
));
4099 tree rhs1
= gimple_assign_rhs1 (stmt
);
4101 ao_ref_init (&rhs1_ref
, rhs1
);
4102 alias_set_type set
= ao_ref_alias_set (&rhs1_ref
);
4103 alias_set_type base_set
4104 = ao_ref_base_alias_set (&rhs1_ref
);
4105 vec
<vn_reference_op_s
> operands
4106 = vn_reference_operands_for_lookup (rhs1
);
4108 vn_reference_lookup_pieces (gimple_vuse (stmt
), set
,
4109 base_set
, TREE_TYPE (rhs1
),
4110 operands
, &ref
, VN_WALK
);
4113 operands
.release ();
4117 /* If the REFERENCE traps and there was a preceding
4118 point in the block that might not return avoid
4119 adding the reference to EXP_GEN. */
4120 if (BB_MAY_NOTRETURN (block
)
4121 && vn_reference_may_trap (ref
))
4123 operands
.release ();
4127 /* If the value of the reference is not invalidated in
4128 this block until it is computed, add the expression
4130 if (gimple_vuse (stmt
))
4134 def_stmt
= SSA_NAME_DEF_STMT (gimple_vuse (stmt
));
4135 while (!gimple_nop_p (def_stmt
)
4136 && gimple_code (def_stmt
) != GIMPLE_PHI
4137 && gimple_bb (def_stmt
) == block
)
4139 if (stmt_may_clobber_ref_p
4140 (def_stmt
, gimple_assign_rhs1 (stmt
)))
4146 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt
));
4150 operands
.release ();
4155 /* If the load was value-numbered to another
4156 load make sure we do not use its expression
4157 for insertion if it wouldn't be a valid
4159 /* At the momemt we have a testcase
4160 for hoist insertion of aligned vs. misaligned
4161 variants in gcc.dg/torture/pr65270-1.c thus
4162 with just alignment to be considered we can
4163 simply replace the expression in the hashtable
4164 with the most conservative one. */
4165 vn_reference_op_t ref1
= &ref
->operands
.last ();
4166 while (ref1
->opcode
!= TARGET_MEM_REF
4167 && ref1
->opcode
!= MEM_REF
4168 && ref1
!= &ref
->operands
[0])
4170 vn_reference_op_t ref2
= &operands
.last ();
4171 while (ref2
->opcode
!= TARGET_MEM_REF
4172 && ref2
->opcode
!= MEM_REF
4173 && ref2
!= &operands
[0])
4175 if ((ref1
->opcode
== TARGET_MEM_REF
4176 || ref1
->opcode
== MEM_REF
)
4177 && (TYPE_ALIGN (ref1
->type
)
4178 > TYPE_ALIGN (ref2
->type
)))
4180 = build_aligned_type (ref1
->type
,
4181 TYPE_ALIGN (ref2
->type
));
4182 /* TBAA behavior is an obvious part so make sure
4183 that the hashtable one covers this as well
4184 by adjusting the ref alias set and its base. */
4186 || alias_set_subset_of (set
, ref
->set
))
4188 else if (ref1
->opcode
!= ref2
->opcode
4189 || (ref1
->opcode
!= MEM_REF
4190 && ref1
->opcode
!= TARGET_MEM_REF
))
4192 /* With mismatching base opcodes or bases
4193 other than MEM_REF or TARGET_MEM_REF we
4194 can't do any easy TBAA adjustment. */
4195 operands
.release ();
4198 else if (alias_set_subset_of (ref
->set
, set
))
4201 if (ref1
->opcode
== MEM_REF
)
4203 = wide_int_to_tree (TREE_TYPE (ref2
->op0
),
4204 wi::to_wide (ref1
->op0
));
4207 = wide_int_to_tree (TREE_TYPE (ref2
->op2
),
4208 wi::to_wide (ref1
->op2
));
4213 if (ref1
->opcode
== MEM_REF
)
4215 = wide_int_to_tree (ptr_type_node
,
4216 wi::to_wide (ref1
->op0
));
4219 = wide_int_to_tree (ptr_type_node
,
4220 wi::to_wide (ref1
->op2
));
4222 operands
.release ();
4224 result
= get_or_alloc_expr_for_reference
4225 (ref
, gimple_location (stmt
));
4233 add_to_value (get_expr_value_id (result
), result
);
4234 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
4241 if (set_bb_may_notreturn
)
4243 BB_MAY_NOTRETURN (block
) = 1;
4244 set_bb_may_notreturn
= false;
4247 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4249 print_bitmap_set (dump_file
, EXP_GEN (block
),
4250 "exp_gen", block
->index
);
4251 print_bitmap_set (dump_file
, PHI_GEN (block
),
4252 "phi_gen", block
->index
);
4253 print_bitmap_set (dump_file
, TMP_GEN (block
),
4254 "tmp_gen", block
->index
);
4255 print_bitmap_set (dump_file
, AVAIL_OUT (block
),
4256 "avail_out", block
->index
);
4259 /* Put the dominator children of BLOCK on the worklist of blocks
4260 to compute available sets for. */
4261 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
4263 son
= next_dom_son (CDI_DOMINATORS
, son
))
4264 worklist
[sp
++] = son
;
4266 vn_context_bb
= NULL
;
4272 /* Initialize data structures used by PRE. */
4279 next_expression_id
= 1;
4280 expressions
.create (0);
4281 expressions
.safe_push (NULL
);
4282 value_expressions
.create (get_max_value_id () + 1);
4283 value_expressions
.quick_grow_cleared (get_max_value_id () + 1);
4284 constant_value_expressions
.create (get_max_constant_value_id () + 1);
4285 constant_value_expressions
.quick_grow_cleared (get_max_constant_value_id () + 1);
4286 name_to_id
.create (0);
4287 gcc_obstack_init (&pre_expr_obstack
);
4289 inserted_exprs
= BITMAP_ALLOC (NULL
);
4291 connect_infinite_loops_to_exit ();
4292 memset (&pre_stats
, 0, sizeof (pre_stats
));
4294 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets
));
4296 calculate_dominance_info (CDI_DOMINATORS
);
4298 bitmap_obstack_initialize (&grand_bitmap_obstack
);
4299 expression_to_id
= new hash_table
<pre_expr_d
> (num_ssa_names
* 3);
4300 FOR_ALL_BB_FN (bb
, cfun
)
4302 EXP_GEN (bb
) = bitmap_set_new ();
4303 PHI_GEN (bb
) = bitmap_set_new ();
4304 TMP_GEN (bb
) = bitmap_set_new ();
4305 AVAIL_OUT (bb
) = bitmap_set_new ();
4306 PHI_TRANS_TABLE (bb
) = NULL
;
4311 /* Deallocate data structures used by PRE. */
4316 value_expressions
.release ();
4317 constant_value_expressions
.release ();
4318 expressions
.release ();
4319 bitmap_obstack_release (&grand_bitmap_obstack
);
4320 bitmap_set_pool
.release ();
4321 pre_expr_pool
.release ();
4322 delete expression_to_id
;
4323 expression_to_id
= NULL
;
4324 name_to_id
.release ();
4325 obstack_free (&pre_expr_obstack
, NULL
);
4328 FOR_ALL_BB_FN (bb
, cfun
)
4329 if (bb
->aux
&& PHI_TRANS_TABLE (bb
))
4330 delete PHI_TRANS_TABLE (bb
);
4331 free_aux_for_blocks ();
4336 const pass_data pass_data_pre
=
4338 GIMPLE_PASS
, /* type */
4340 OPTGROUP_NONE
, /* optinfo_flags */
4341 TV_TREE_PRE
, /* tv_id */
4342 ( PROP_cfg
| PROP_ssa
), /* properties_required */
4343 0, /* properties_provided */
4344 0, /* properties_destroyed */
4345 TODO_rebuild_alias
, /* todo_flags_start */
4346 0, /* todo_flags_finish */
4349 class pass_pre
: public gimple_opt_pass
4352 pass_pre (gcc::context
*ctxt
)
4353 : gimple_opt_pass (pass_data_pre
, ctxt
)
4356 /* opt_pass methods: */
4357 virtual bool gate (function
*)
4358 { return flag_tree_pre
!= 0 || flag_code_hoisting
!= 0; }
4359 virtual unsigned int execute (function
*);
4361 }; // class pass_pre
4363 /* Valueization hook for RPO VN when we are calling back to it
4364 at ANTIC compute time. */
4367 pre_valueize (tree name
)
4369 if (TREE_CODE (name
) == SSA_NAME
)
4371 tree tem
= VN_INFO (name
)->valnum
;
4372 if (tem
!= VN_TOP
&& tem
!= name
)
4374 if (TREE_CODE (tem
) != SSA_NAME
4375 || SSA_NAME_IS_DEFAULT_DEF (tem
))
4377 /* We create temporary SSA names for representatives that
4378 do not have a definition (yet) but are not default defs either
4379 assume they are fine to use. */
4380 basic_block def_bb
= gimple_bb (SSA_NAME_DEF_STMT (tem
));
4382 || dominated_by_p (CDI_DOMINATORS
, vn_context_bb
, def_bb
))
4384 /* ??? Now we could look for a leader. Ideally we'd somehow
4385 expose RPO VN leaders and get rid of AVAIL_OUT as well... */
4392 pass_pre::execute (function
*fun
)
4394 unsigned int todo
= 0;
4396 do_partial_partial
=
4397 flag_tree_partial_pre
&& optimize_function_for_speed_p (fun
);
4399 /* This has to happen before VN runs because
4400 loop_optimizer_init may create new phis, etc. */
4401 loop_optimizer_init (LOOPS_NORMAL
);
4402 split_edges_for_insertion ();
4404 calculate_dominance_info (CDI_DOMINATORS
);
4406 run_rpo_vn (VN_WALK
);
4410 vn_valueize
= pre_valueize
;
4412 /* Insert can get quite slow on an incredibly large number of basic
4413 blocks due to some quadratic behavior. Until this behavior is
4414 fixed, don't run it when he have an incredibly large number of
4415 bb's. If we aren't going to run insert, there is no point in
4416 computing ANTIC, either, even though it's plenty fast nor do
4417 we require AVAIL. */
4418 if (n_basic_blocks_for_fn (fun
) < 4000)
4420 compute_avail (fun
);
4425 /* Make sure to remove fake edges before committing our inserts.
4426 This makes sure we don't end up with extra critical edges that
4427 we would need to split. */
4428 remove_fake_exit_edges ();
4429 gsi_commit_edge_inserts ();
4431 /* Eliminate folds statements which might (should not...) end up
4432 not keeping virtual operands up-to-date. */
4433 gcc_assert (!need_ssa_update_p (fun
));
4435 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
4436 statistics_counter_event (fun
, "PA inserted", pre_stats
.pa_insert
);
4437 statistics_counter_event (fun
, "HOIST inserted", pre_stats
.hoist_insert
);
4438 statistics_counter_event (fun
, "New PHIs", pre_stats
.phis
);
4440 todo
|= eliminate_with_rpo_vn (inserted_exprs
);
4447 loop_optimizer_finalize ();
4449 /* Perform a CFG cleanup before we run simple_dce_from_worklist since
4450 unreachable code regions will have not up-to-date SSA form which
4452 bool need_crit_edge_split
= false;
4453 if (todo
& TODO_cleanup_cfg
)
4455 cleanup_tree_cfg ();
4456 need_crit_edge_split
= true;
4459 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4460 to insert PHI nodes sometimes, and because value numbering of casts isn't
4461 perfect, we sometimes end up inserting dead code. This simple DCE-like
4462 pass removes any insertions we made that weren't actually used. */
4463 simple_dce_from_worklist (inserted_exprs
);
4464 BITMAP_FREE (inserted_exprs
);
4466 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4467 case we can merge the block with the remaining predecessor of the block.
4469 - call merge_blocks after each tail merge iteration
4470 - call merge_blocks after all tail merge iterations
4471 - mark TODO_cleanup_cfg when necessary. */
4472 todo
|= tail_merge_optimize (need_crit_edge_split
);
4476 /* Tail merging invalidates the virtual SSA web, together with
4477 cfg-cleanup opportunities exposed by PRE this will wreck the
4478 SSA updating machinery. So make sure to run update-ssa
4479 manually, before eventually scheduling cfg-cleanup as part of
4481 update_ssa (TODO_update_ssa_only_virtuals
);
4489 make_pass_pre (gcc::context
*ctxt
)
4491 return new pass_pre (ctxt
);