PR c++/87582
[official-gcc.git] / gcc / tree-ssa-pre.c
blobd1b0cc0c26f94421d06a1a98049121fddf1eeaae
1 /* Full and partial redundancy elimination and code hoisting on SSA GIMPLE.
2 Copyright (C) 2001-2018 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
4 <stevenb@suse.de>
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)
11 any later version.
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/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "backend.h"
26 #include "rtl.h"
27 #include "tree.h"
28 #include "gimple.h"
29 #include "predict.h"
30 #include "alloc-pool.h"
31 #include "tree-pass.h"
32 #include "ssa.h"
33 #include "cgraph.h"
34 #include "gimple-pretty-print.h"
35 #include "fold-const.h"
36 #include "cfganal.h"
37 #include "gimple-fold.h"
38 #include "tree-eh.h"
39 #include "gimplify.h"
40 #include "gimple-iterator.h"
41 #include "tree-cfg.h"
42 #include "tree-into-ssa.h"
43 #include "tree-dfa.h"
44 #include "tree-ssa.h"
45 #include "cfgloop.h"
46 #include "tree-ssa-sccvn.h"
47 #include "tree-scalar-evolution.h"
48 #include "params.h"
49 #include "dbgcnt.h"
50 #include "domwalk.h"
51 #include "tree-ssa-propagate.h"
52 #include "tree-ssa-dce.h"
53 #include "tree-cfgcleanup.h"
54 #include "alias.h"
56 /* Even though this file is called tree-ssa-pre.c, we actually
57 implement a bit more than just PRE here. All of them piggy-back
58 on GVN which is implemented in tree-ssa-sccvn.c.
60 1. Full Redundancy Elimination (FRE)
61 This is the elimination phase of GVN.
63 2. Partial Redundancy Elimination (PRE)
64 This is adds computation of AVAIL_OUT and ANTIC_IN and
65 doing expression insertion to form GVN-PRE.
67 3. Code hoisting
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).
75 /* TODO:
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
81 memory of GVN-PRE.
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
104 expressions/values.
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
124 anticipation) if:
126 1. It is AVAIL in some, but not all, of the predecessors of a
127 given block.
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
139 of the predecessors.
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
158 code hoisting, too.
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
217 use the value id. */
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
235 order. */
237 /* Type of expression, used to know which member of the PRE_EXPR union
238 is valid. */
240 enum pre_expr_kind
242 NAME,
243 NARY,
244 REFERENCE,
245 CONSTANT
248 union pre_expr_union
250 tree name;
251 tree constant;
252 vn_nary_op_t nary;
253 vn_reference_t reference;
256 typedef struct pre_expr_d : nofree_ptr_hash <pre_expr_d>
258 enum pre_expr_kind kind;
259 unsigned int id;
260 pre_expr_union u;
262 /* hash_table support. */
263 static inline hashval_t hash (const pre_expr_d *);
264 static inline int equal (const pre_expr_d *, const pre_expr_d *);
265 } *pre_expr;
267 #define PRE_EXPR_NAME(e) (e)->u.name
268 #define PRE_EXPR_NARY(e) (e)->u.nary
269 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
270 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
272 /* Compare E1 and E1 for equality. */
274 inline int
275 pre_expr_d::equal (const pre_expr_d *e1, const pre_expr_d *e2)
277 if (e1->kind != e2->kind)
278 return false;
280 switch (e1->kind)
282 case CONSTANT:
283 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
284 PRE_EXPR_CONSTANT (e2));
285 case NAME:
286 return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
287 case NARY:
288 return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
289 case REFERENCE:
290 return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
291 PRE_EXPR_REFERENCE (e2));
292 default:
293 gcc_unreachable ();
297 /* Hash E. */
299 inline hashval_t
300 pre_expr_d::hash (const pre_expr_d *e)
302 switch (e->kind)
304 case CONSTANT:
305 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
306 case NAME:
307 return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
308 case NARY:
309 return PRE_EXPR_NARY (e)->hashcode;
310 case REFERENCE:
311 return PRE_EXPR_REFERENCE (e)->hashcode;
312 default:
313 gcc_unreachable ();
317 /* Next global expression id number. */
318 static unsigned int next_expression_id;
320 /* Mapping from expression to id number we can use in bitmap sets. */
321 static vec<pre_expr> expressions;
322 static hash_table<pre_expr_d> *expression_to_id;
323 static vec<unsigned> name_to_id;
325 /* Allocate an expression id for EXPR. */
327 static inline unsigned int
328 alloc_expression_id (pre_expr expr)
330 struct pre_expr_d **slot;
331 /* Make sure we won't overflow. */
332 gcc_assert (next_expression_id + 1 > next_expression_id);
333 expr->id = next_expression_id++;
334 expressions.safe_push (expr);
335 if (expr->kind == NAME)
337 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
338 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
339 re-allocations by using vec::reserve upfront. */
340 unsigned old_len = name_to_id.length ();
341 name_to_id.reserve (num_ssa_names - old_len);
342 name_to_id.quick_grow_cleared (num_ssa_names);
343 gcc_assert (name_to_id[version] == 0);
344 name_to_id[version] = expr->id;
346 else
348 slot = expression_to_id->find_slot (expr, INSERT);
349 gcc_assert (!*slot);
350 *slot = expr;
352 return next_expression_id - 1;
355 /* Return the expression id for tree EXPR. */
357 static inline unsigned int
358 get_expression_id (const pre_expr expr)
360 return expr->id;
363 static inline unsigned int
364 lookup_expression_id (const pre_expr expr)
366 struct pre_expr_d **slot;
368 if (expr->kind == NAME)
370 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
371 if (name_to_id.length () <= version)
372 return 0;
373 return name_to_id[version];
375 else
377 slot = expression_to_id->find_slot (expr, NO_INSERT);
378 if (!slot)
379 return 0;
380 return ((pre_expr)*slot)->id;
384 /* Return the existing expression id for EXPR, or create one if one
385 does not exist yet. */
387 static inline unsigned int
388 get_or_alloc_expression_id (pre_expr expr)
390 unsigned int id = lookup_expression_id (expr);
391 if (id == 0)
392 return alloc_expression_id (expr);
393 return expr->id = id;
396 /* Return the expression that has expression id ID */
398 static inline pre_expr
399 expression_for_id (unsigned int id)
401 return expressions[id];
404 static object_allocator<pre_expr_d> pre_expr_pool ("pre_expr nodes");
406 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
408 static pre_expr
409 get_or_alloc_expr_for_name (tree name)
411 struct pre_expr_d expr;
412 pre_expr result;
413 unsigned int result_id;
415 expr.kind = NAME;
416 expr.id = 0;
417 PRE_EXPR_NAME (&expr) = name;
418 result_id = lookup_expression_id (&expr);
419 if (result_id != 0)
420 return expression_for_id (result_id);
422 result = pre_expr_pool.allocate ();
423 result->kind = NAME;
424 PRE_EXPR_NAME (result) = name;
425 alloc_expression_id (result);
426 return result;
429 /* An unordered bitmap set. One bitmap tracks values, the other,
430 expressions. */
431 typedef struct bitmap_set
433 bitmap_head expressions;
434 bitmap_head values;
435 } *bitmap_set_t;
437 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
438 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
440 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
441 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
443 /* Mapping from value id to expressions with that value_id. */
444 static vec<bitmap> value_expressions;
446 /* Sets that we need to keep track of. */
447 typedef struct bb_bitmap_sets
449 /* The EXP_GEN set, which represents expressions/values generated in
450 a basic block. */
451 bitmap_set_t exp_gen;
453 /* The PHI_GEN set, which represents PHI results generated in a
454 basic block. */
455 bitmap_set_t phi_gen;
457 /* The TMP_GEN set, which represents results/temporaries generated
458 in a basic block. IE the LHS of an expression. */
459 bitmap_set_t tmp_gen;
461 /* The AVAIL_OUT set, which represents which values are available in
462 a given basic block. */
463 bitmap_set_t avail_out;
465 /* The ANTIC_IN set, which represents which values are anticipatable
466 in a given basic block. */
467 bitmap_set_t antic_in;
469 /* The PA_IN set, which represents which values are
470 partially anticipatable in a given basic block. */
471 bitmap_set_t pa_in;
473 /* The NEW_SETS set, which is used during insertion to augment the
474 AVAIL_OUT set of blocks with the new insertions performed during
475 the current iteration. */
476 bitmap_set_t new_sets;
478 /* A cache for value_dies_in_block_x. */
479 bitmap expr_dies;
481 /* The live virtual operand on successor edges. */
482 tree vop_on_exit;
484 /* True if we have visited this block during ANTIC calculation. */
485 unsigned int visited : 1;
487 /* True when the block contains a call that might not return. */
488 unsigned int contains_may_not_return_call : 1;
489 } *bb_value_sets_t;
491 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
492 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
493 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
494 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
495 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
496 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
497 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
498 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
499 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
500 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
501 #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
504 /* This structure is used to keep track of statistics on what
505 optimization PRE was able to perform. */
506 static struct
508 /* The number of new expressions/temporaries generated by PRE. */
509 int insertions;
511 /* The number of inserts found due to partial anticipation */
512 int pa_insert;
514 /* The number of inserts made for code hoisting. */
515 int hoist_insert;
517 /* The number of new PHI nodes added by PRE. */
518 int phis;
519 } pre_stats;
521 static bool do_partial_partial;
522 static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
523 static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
524 static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
525 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
526 static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
527 static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
528 static bitmap_set_t bitmap_set_new (void);
529 static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
530 tree);
531 static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
532 static unsigned int get_expr_value_id (pre_expr);
534 /* We can add and remove elements and entries to and from sets
535 and hash tables, so we use alloc pools for them. */
537 static object_allocator<bitmap_set> bitmap_set_pool ("Bitmap sets");
538 static bitmap_obstack grand_bitmap_obstack;
540 /* A three tuple {e, pred, v} used to cache phi translations in the
541 phi_translate_table. */
543 typedef struct expr_pred_trans_d : free_ptr_hash<expr_pred_trans_d>
545 /* The expression. */
546 pre_expr e;
548 /* The predecessor block along which we translated the expression. */
549 basic_block pred;
551 /* The value that resulted from the translation. */
552 pre_expr v;
554 /* The hashcode for the expression, pred pair. This is cached for
555 speed reasons. */
556 hashval_t hashcode;
558 /* hash_table support. */
559 static inline hashval_t hash (const expr_pred_trans_d *);
560 static inline int equal (const expr_pred_trans_d *, const expr_pred_trans_d *);
561 } *expr_pred_trans_t;
562 typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
564 inline hashval_t
565 expr_pred_trans_d::hash (const expr_pred_trans_d *e)
567 return e->hashcode;
570 inline int
571 expr_pred_trans_d::equal (const expr_pred_trans_d *ve1,
572 const expr_pred_trans_d *ve2)
574 basic_block b1 = ve1->pred;
575 basic_block b2 = ve2->pred;
577 /* If they are not translations for the same basic block, they can't
578 be equal. */
579 if (b1 != b2)
580 return false;
581 return pre_expr_d::equal (ve1->e, ve2->e);
584 /* The phi_translate_table caches phi translations for a given
585 expression and predecessor. */
586 static hash_table<expr_pred_trans_d> *phi_translate_table;
588 /* Add the tuple mapping from {expression E, basic block PRED} to
589 the phi translation table and return whether it pre-existed. */
591 static inline bool
592 phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
594 expr_pred_trans_t *slot;
595 expr_pred_trans_d tem;
596 hashval_t hash = iterative_hash_hashval_t (pre_expr_d::hash (e),
597 pred->index);
598 tem.e = e;
599 tem.pred = pred;
600 tem.hashcode = hash;
601 slot = phi_translate_table->find_slot_with_hash (&tem, hash, INSERT);
602 if (*slot)
604 *entry = *slot;
605 return true;
608 *entry = *slot = XNEW (struct expr_pred_trans_d);
609 (*entry)->e = e;
610 (*entry)->pred = pred;
611 (*entry)->hashcode = hash;
612 return false;
616 /* Add expression E to the expression set of value id V. */
618 static void
619 add_to_value (unsigned int v, pre_expr e)
621 bitmap set;
623 gcc_checking_assert (get_expr_value_id (e) == v);
625 if (v >= value_expressions.length ())
627 value_expressions.safe_grow_cleared (v + 1);
630 set = value_expressions[v];
631 if (!set)
633 set = BITMAP_ALLOC (&grand_bitmap_obstack);
634 value_expressions[v] = set;
637 bitmap_set_bit (set, get_or_alloc_expression_id (e));
640 /* Create a new bitmap set and return it. */
642 static bitmap_set_t
643 bitmap_set_new (void)
645 bitmap_set_t ret = bitmap_set_pool.allocate ();
646 bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
647 bitmap_initialize (&ret->values, &grand_bitmap_obstack);
648 return ret;
651 /* Return the value id for a PRE expression EXPR. */
653 static unsigned int
654 get_expr_value_id (pre_expr expr)
656 unsigned int id;
657 switch (expr->kind)
659 case CONSTANT:
660 id = get_constant_value_id (PRE_EXPR_CONSTANT (expr));
661 break;
662 case NAME:
663 id = VN_INFO (PRE_EXPR_NAME (expr))->value_id;
664 break;
665 case NARY:
666 gcc_assert (!PRE_EXPR_NARY (expr)->predicated_values);
667 id = PRE_EXPR_NARY (expr)->value_id;
668 break;
669 case REFERENCE:
670 id = PRE_EXPR_REFERENCE (expr)->value_id;
671 break;
672 default:
673 gcc_unreachable ();
675 /* ??? We cannot assert that expr has a value-id (it can be 0), because
676 we assign value-ids only to expressions that have a result
677 in set_hashtable_value_ids. */
678 return id;
681 /* Return a VN valnum (SSA name or constant) for the PRE value-id VAL. */
683 static tree
684 vn_valnum_from_value_id (unsigned int val)
686 bitmap_iterator bi;
687 unsigned int i;
688 bitmap exprset = value_expressions[val];
689 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
691 pre_expr vexpr = expression_for_id (i);
692 if (vexpr->kind == NAME)
693 return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
694 else if (vexpr->kind == CONSTANT)
695 return PRE_EXPR_CONSTANT (vexpr);
697 return NULL_TREE;
700 /* Insert an expression EXPR into a bitmapped set. */
702 static void
703 bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
705 unsigned int val = get_expr_value_id (expr);
706 if (! value_id_constant_p (val))
708 /* Note this is the only function causing multiple expressions
709 for the same value to appear in a set. This is needed for
710 TMP_GEN, PHI_GEN and NEW_SETs. */
711 bitmap_set_bit (&set->values, val);
712 bitmap_set_bit (&set->expressions, get_or_alloc_expression_id (expr));
716 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
718 static void
719 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
721 bitmap_copy (&dest->expressions, &orig->expressions);
722 bitmap_copy (&dest->values, &orig->values);
726 /* Free memory used up by SET. */
727 static void
728 bitmap_set_free (bitmap_set_t set)
730 bitmap_clear (&set->expressions);
731 bitmap_clear (&set->values);
735 /* Generate an topological-ordered array of bitmap set SET. */
737 static vec<pre_expr>
738 sorted_array_from_bitmap_set (bitmap_set_t set)
740 unsigned int i, j;
741 bitmap_iterator bi, bj;
742 vec<pre_expr> result;
744 /* Pre-allocate enough space for the array. */
745 result.create (bitmap_count_bits (&set->expressions));
747 FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
749 /* The number of expressions having a given value is usually
750 relatively small. Thus, rather than making a vector of all
751 the expressions and sorting it by value-id, we walk the values
752 and check in the reverse mapping that tells us what expressions
753 have a given value, to filter those in our set. As a result,
754 the expressions are inserted in value-id order, which means
755 topological order.
757 If this is somehow a significant lose for some cases, we can
758 choose which set to walk based on the set size. */
759 bitmap exprset = value_expressions[i];
760 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj)
762 if (bitmap_bit_p (&set->expressions, j))
763 result.quick_push (expression_for_id (j));
767 return result;
770 /* Subtract all expressions contained in ORIG from DEST. */
772 static bitmap_set_t
773 bitmap_set_subtract_expressions (bitmap_set_t dest, bitmap_set_t orig)
775 bitmap_set_t result = bitmap_set_new ();
776 bitmap_iterator bi;
777 unsigned int i;
779 bitmap_and_compl (&result->expressions, &dest->expressions,
780 &orig->expressions);
782 FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
784 pre_expr expr = expression_for_id (i);
785 unsigned int value_id = get_expr_value_id (expr);
786 bitmap_set_bit (&result->values, value_id);
789 return result;
792 /* Subtract all values in bitmap set B from bitmap set A. */
794 static void
795 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
797 unsigned int i;
798 bitmap_iterator bi;
799 unsigned to_remove = -1U;
800 bitmap_and_compl_into (&a->values, &b->values);
801 FOR_EACH_EXPR_ID_IN_SET (a, i, bi)
803 if (to_remove != -1U)
805 bitmap_clear_bit (&a->expressions, to_remove);
806 to_remove = -1U;
808 pre_expr expr = expression_for_id (i);
809 if (! bitmap_bit_p (&a->values, get_expr_value_id (expr)))
810 to_remove = i;
812 if (to_remove != -1U)
813 bitmap_clear_bit (&a->expressions, to_remove);
817 /* Return true if bitmapped set SET contains the value VALUE_ID. */
819 static bool
820 bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
822 if (value_id_constant_p (value_id))
823 return true;
825 return bitmap_bit_p (&set->values, value_id);
828 /* Return true if two bitmap sets are equal. */
830 static bool
831 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
833 return bitmap_equal_p (&a->values, &b->values);
836 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
837 and add it otherwise. */
839 static void
840 bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
842 unsigned int val = get_expr_value_id (expr);
843 if (value_id_constant_p (val))
844 return;
846 if (bitmap_set_contains_value (set, val))
848 /* The number of expressions having a given value is usually
849 significantly less than the total number of expressions in SET.
850 Thus, rather than check, for each expression in SET, whether it
851 has the value LOOKFOR, we walk the reverse mapping that tells us
852 what expressions have a given value, and see if any of those
853 expressions are in our set. For large testcases, this is about
854 5-10x faster than walking the bitmap. If this is somehow a
855 significant lose for some cases, we can choose which set to walk
856 based on the set size. */
857 unsigned int i;
858 bitmap_iterator bi;
859 bitmap exprset = value_expressions[val];
860 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
862 if (bitmap_clear_bit (&set->expressions, i))
864 bitmap_set_bit (&set->expressions, get_expression_id (expr));
865 return;
868 gcc_unreachable ();
870 else
871 bitmap_insert_into_set (set, expr);
874 /* Insert EXPR into SET if EXPR's value is not already present in
875 SET. */
877 static void
878 bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
880 unsigned int val = get_expr_value_id (expr);
882 gcc_checking_assert (expr->id == get_or_alloc_expression_id (expr));
884 /* Constant values are always considered to be part of the set. */
885 if (value_id_constant_p (val))
886 return;
888 /* If the value membership changed, add the expression. */
889 if (bitmap_set_bit (&set->values, val))
890 bitmap_set_bit (&set->expressions, expr->id);
893 /* Print out EXPR to outfile. */
895 static void
896 print_pre_expr (FILE *outfile, const pre_expr expr)
898 if (! expr)
900 fprintf (outfile, "NULL");
901 return;
903 switch (expr->kind)
905 case CONSTANT:
906 print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr));
907 break;
908 case NAME:
909 print_generic_expr (outfile, PRE_EXPR_NAME (expr));
910 break;
911 case NARY:
913 unsigned int i;
914 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
915 fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
916 for (i = 0; i < nary->length; i++)
918 print_generic_expr (outfile, nary->op[i]);
919 if (i != (unsigned) nary->length - 1)
920 fprintf (outfile, ",");
922 fprintf (outfile, "}");
924 break;
926 case REFERENCE:
928 vn_reference_op_t vro;
929 unsigned int i;
930 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
931 fprintf (outfile, "{");
932 for (i = 0;
933 ref->operands.iterate (i, &vro);
934 i++)
936 bool closebrace = false;
937 if (vro->opcode != SSA_NAME
938 && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
940 fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
941 if (vro->op0)
943 fprintf (outfile, "<");
944 closebrace = true;
947 if (vro->op0)
949 print_generic_expr (outfile, vro->op0);
950 if (vro->op1)
952 fprintf (outfile, ",");
953 print_generic_expr (outfile, vro->op1);
955 if (vro->op2)
957 fprintf (outfile, ",");
958 print_generic_expr (outfile, vro->op2);
961 if (closebrace)
962 fprintf (outfile, ">");
963 if (i != ref->operands.length () - 1)
964 fprintf (outfile, ",");
966 fprintf (outfile, "}");
967 if (ref->vuse)
969 fprintf (outfile, "@");
970 print_generic_expr (outfile, ref->vuse);
973 break;
976 void debug_pre_expr (pre_expr);
978 /* Like print_pre_expr but always prints to stderr. */
979 DEBUG_FUNCTION void
980 debug_pre_expr (pre_expr e)
982 print_pre_expr (stderr, e);
983 fprintf (stderr, "\n");
986 /* Print out SET to OUTFILE. */
988 static void
989 print_bitmap_set (FILE *outfile, bitmap_set_t set,
990 const char *setname, int blockindex)
992 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
993 if (set)
995 bool first = true;
996 unsigned i;
997 bitmap_iterator bi;
999 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1001 const pre_expr expr = expression_for_id (i);
1003 if (!first)
1004 fprintf (outfile, ", ");
1005 first = false;
1006 print_pre_expr (outfile, expr);
1008 fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1011 fprintf (outfile, " }\n");
1014 void debug_bitmap_set (bitmap_set_t);
1016 DEBUG_FUNCTION void
1017 debug_bitmap_set (bitmap_set_t set)
1019 print_bitmap_set (stderr, set, "debug", 0);
1022 void debug_bitmap_sets_for (basic_block);
1024 DEBUG_FUNCTION void
1025 debug_bitmap_sets_for (basic_block bb)
1027 print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1028 print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1029 print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1030 print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1031 print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1032 if (do_partial_partial)
1033 print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1034 print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1037 /* Print out the expressions that have VAL to OUTFILE. */
1039 static void
1040 print_value_expressions (FILE *outfile, unsigned int val)
1042 bitmap set = value_expressions[val];
1043 if (set)
1045 bitmap_set x;
1046 char s[10];
1047 sprintf (s, "%04d", val);
1048 x.expressions = *set;
1049 print_bitmap_set (outfile, &x, s, 0);
1054 DEBUG_FUNCTION void
1055 debug_value_expressions (unsigned int val)
1057 print_value_expressions (stderr, val);
1060 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1061 represent it. */
1063 static pre_expr
1064 get_or_alloc_expr_for_constant (tree constant)
1066 unsigned int result_id;
1067 unsigned int value_id;
1068 struct pre_expr_d expr;
1069 pre_expr newexpr;
1071 expr.kind = CONSTANT;
1072 PRE_EXPR_CONSTANT (&expr) = constant;
1073 result_id = lookup_expression_id (&expr);
1074 if (result_id != 0)
1075 return expression_for_id (result_id);
1077 newexpr = pre_expr_pool.allocate ();
1078 newexpr->kind = CONSTANT;
1079 PRE_EXPR_CONSTANT (newexpr) = constant;
1080 alloc_expression_id (newexpr);
1081 value_id = get_or_alloc_constant_value_id (constant);
1082 add_to_value (value_id, newexpr);
1083 return newexpr;
1086 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1087 Currently only supports constants and SSA_NAMES. */
1088 static pre_expr
1089 get_or_alloc_expr_for (tree t)
1091 if (TREE_CODE (t) == SSA_NAME)
1092 return get_or_alloc_expr_for_name (t);
1093 else if (is_gimple_min_invariant (t))
1094 return get_or_alloc_expr_for_constant (t);
1095 gcc_unreachable ();
1098 /* Return the folded version of T if T, when folded, is a gimple
1099 min_invariant or an SSA name. Otherwise, return T. */
1101 static pre_expr
1102 fully_constant_expression (pre_expr e)
1104 switch (e->kind)
1106 case CONSTANT:
1107 return e;
1108 case NARY:
1110 vn_nary_op_t nary = PRE_EXPR_NARY (e);
1111 tree res = vn_nary_simplify (nary);
1112 if (!res)
1113 return e;
1114 if (is_gimple_min_invariant (res))
1115 return get_or_alloc_expr_for_constant (res);
1116 if (TREE_CODE (res) == SSA_NAME)
1117 return get_or_alloc_expr_for_name (res);
1118 return e;
1120 case REFERENCE:
1122 vn_reference_t ref = PRE_EXPR_REFERENCE (e);
1123 tree folded;
1124 if ((folded = fully_constant_vn_reference_p (ref)))
1125 return get_or_alloc_expr_for_constant (folded);
1126 return e;
1128 default:
1129 return e;
1131 return e;
1134 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1135 it has the value it would have in BLOCK. Set *SAME_VALID to true
1136 in case the new vuse doesn't change the value id of the OPERANDS. */
1138 static tree
1139 translate_vuse_through_block (vec<vn_reference_op_s> operands,
1140 alias_set_type set, tree type, tree vuse,
1141 basic_block phiblock,
1142 basic_block block, bool *same_valid)
1144 gimple *phi = SSA_NAME_DEF_STMT (vuse);
1145 ao_ref ref;
1146 edge e = NULL;
1147 bool use_oracle;
1149 *same_valid = true;
1151 if (gimple_bb (phi) != phiblock)
1152 return vuse;
1154 use_oracle = ao_ref_init_from_vn_reference (&ref, set, type, operands);
1156 /* Use the alias-oracle to find either the PHI node in this block,
1157 the first VUSE used in this block that is equivalent to vuse or
1158 the first VUSE which definition in this block kills the value. */
1159 if (gimple_code (phi) == GIMPLE_PHI)
1160 e = find_edge (block, phiblock);
1161 else if (use_oracle)
1162 while (!stmt_may_clobber_ref_p_1 (phi, &ref))
1164 vuse = gimple_vuse (phi);
1165 phi = SSA_NAME_DEF_STMT (vuse);
1166 if (gimple_bb (phi) != phiblock)
1167 return vuse;
1168 if (gimple_code (phi) == GIMPLE_PHI)
1170 e = find_edge (block, phiblock);
1171 break;
1174 else
1175 return NULL_TREE;
1177 if (e)
1179 if (use_oracle)
1181 bitmap visited = NULL;
1182 unsigned int cnt;
1183 /* Try to find a vuse that dominates this phi node by skipping
1184 non-clobbering statements. */
1185 vuse = get_continuation_for_phi (phi, &ref, &cnt, &visited, false,
1186 NULL, NULL);
1187 if (visited)
1188 BITMAP_FREE (visited);
1190 else
1191 vuse = NULL_TREE;
1192 if (!vuse)
1194 /* If we didn't find any, the value ID can't stay the same,
1195 but return the translated vuse. */
1196 *same_valid = false;
1197 vuse = PHI_ARG_DEF (phi, e->dest_idx);
1199 /* ??? We would like to return vuse here as this is the canonical
1200 upmost vdef that this reference is associated with. But during
1201 insertion of the references into the hash tables we only ever
1202 directly insert with their direct gimple_vuse, hence returning
1203 something else would make us not find the other expression. */
1204 return PHI_ARG_DEF (phi, e->dest_idx);
1207 return NULL_TREE;
1210 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1211 SET2 *or* SET3. This is used to avoid making a set consisting of the union
1212 of PA_IN and ANTIC_IN during insert and phi-translation. */
1214 static inline pre_expr
1215 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
1216 bitmap_set_t set3 = NULL)
1218 pre_expr result = NULL;
1220 if (set1)
1221 result = bitmap_find_leader (set1, val);
1222 if (!result && set2)
1223 result = bitmap_find_leader (set2, val);
1224 if (!result && set3)
1225 result = bitmap_find_leader (set3, val);
1226 return result;
1229 /* Get the tree type for our PRE expression e. */
1231 static tree
1232 get_expr_type (const pre_expr e)
1234 switch (e->kind)
1236 case NAME:
1237 return TREE_TYPE (PRE_EXPR_NAME (e));
1238 case CONSTANT:
1239 return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1240 case REFERENCE:
1241 return PRE_EXPR_REFERENCE (e)->type;
1242 case NARY:
1243 return PRE_EXPR_NARY (e)->type;
1245 gcc_unreachable ();
1248 /* Get a representative SSA_NAME for a given expression that is available in B.
1249 Since all of our sub-expressions are treated as values, we require
1250 them to be SSA_NAME's for simplicity.
1251 Prior versions of GVNPRE used to use "value handles" here, so that
1252 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1253 either case, the operands are really values (IE we do not expect
1254 them to be usable without finding leaders). */
1256 static tree
1257 get_representative_for (const pre_expr e, basic_block b = NULL)
1259 tree name, valnum = NULL_TREE;
1260 unsigned int value_id = get_expr_value_id (e);
1262 switch (e->kind)
1264 case NAME:
1265 return VN_INFO (PRE_EXPR_NAME (e))->valnum;
1266 case CONSTANT:
1267 return PRE_EXPR_CONSTANT (e);
1268 case NARY:
1269 case REFERENCE:
1271 /* Go through all of the expressions representing this value
1272 and pick out an SSA_NAME. */
1273 unsigned int i;
1274 bitmap_iterator bi;
1275 bitmap exprs = value_expressions[value_id];
1276 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1278 pre_expr rep = expression_for_id (i);
1279 if (rep->kind == NAME)
1281 tree name = PRE_EXPR_NAME (rep);
1282 valnum = VN_INFO (name)->valnum;
1283 gimple *def = SSA_NAME_DEF_STMT (name);
1284 /* We have to return either a new representative or one
1285 that can be used for expression simplification and thus
1286 is available in B. */
1287 if (! b
1288 || gimple_nop_p (def)
1289 || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
1290 return name;
1292 else if (rep->kind == CONSTANT)
1293 return PRE_EXPR_CONSTANT (rep);
1296 break;
1299 /* If we reached here we couldn't find an SSA_NAME. This can
1300 happen when we've discovered a value that has never appeared in
1301 the program as set to an SSA_NAME, as the result of phi translation.
1302 Create one here.
1303 ??? We should be able to re-use this when we insert the statement
1304 to compute it. */
1305 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1306 VN_INFO (name)->value_id = value_id;
1307 VN_INFO (name)->valnum = valnum ? valnum : name;
1308 /* ??? For now mark this SSA name for release by VN. */
1309 VN_INFO (name)->needs_insertion = true;
1310 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1311 if (dump_file && (dump_flags & TDF_DETAILS))
1313 fprintf (dump_file, "Created SSA_NAME representative ");
1314 print_generic_expr (dump_file, name);
1315 fprintf (dump_file, " for expression:");
1316 print_pre_expr (dump_file, e);
1317 fprintf (dump_file, " (%04d)\n", value_id);
1320 return name;
1324 static pre_expr
1325 phi_translate (bitmap_set_t, pre_expr, bitmap_set_t, bitmap_set_t, edge);
1327 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1328 the phis in PRED. Return NULL if we can't find a leader for each part
1329 of the translated expression. */
1331 static pre_expr
1332 phi_translate_1 (bitmap_set_t dest,
1333 pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
1335 basic_block pred = e->src;
1336 basic_block phiblock = e->dest;
1337 switch (expr->kind)
1339 case NARY:
1341 unsigned int i;
1342 bool changed = false;
1343 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1344 vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1345 sizeof_vn_nary_op (nary->length));
1346 memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1348 for (i = 0; i < newnary->length; i++)
1350 if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1351 continue;
1352 else
1354 pre_expr leader, result;
1355 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1356 leader = find_leader_in_sets (op_val_id, set1, set2);
1357 result = phi_translate (dest, leader, set1, set2, e);
1358 if (result && result != leader)
1359 /* If op has a leader in the sets we translate make
1360 sure to use the value of the translated expression.
1361 We might need a new representative for that. */
1362 newnary->op[i] = get_representative_for (result, pred);
1363 else if (!result)
1364 return NULL;
1366 changed |= newnary->op[i] != nary->op[i];
1369 if (changed)
1371 pre_expr constant;
1372 unsigned int new_val_id;
1374 PRE_EXPR_NARY (expr) = newnary;
1375 constant = fully_constant_expression (expr);
1376 PRE_EXPR_NARY (expr) = nary;
1377 if (constant != expr)
1379 /* For non-CONSTANTs we have to make sure we can eventually
1380 insert the expression. Which means we need to have a
1381 leader for it. */
1382 if (constant->kind != CONSTANT)
1384 /* Do not allow simplifications to non-constants over
1385 backedges as this will likely result in a loop PHI node
1386 to be inserted and increased register pressure.
1387 See PR77498 - this avoids doing predcoms work in
1388 a less efficient way. */
1389 if (e->flags & EDGE_DFS_BACK)
1391 else
1393 unsigned value_id = get_expr_value_id (constant);
1394 /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1395 dest has what we computed into ANTIC_OUT sofar
1396 so pick from that - since topological sorting
1397 by sorted_array_from_bitmap_set isn't perfect
1398 we may lose some cases here. */
1399 constant = find_leader_in_sets (value_id, dest,
1400 AVAIL_OUT (pred));
1401 if (constant)
1403 if (dump_file && (dump_flags & TDF_DETAILS))
1405 fprintf (dump_file, "simplifying ");
1406 print_pre_expr (dump_file, expr);
1407 fprintf (dump_file, " translated %d -> %d to ",
1408 phiblock->index, pred->index);
1409 PRE_EXPR_NARY (expr) = newnary;
1410 print_pre_expr (dump_file, expr);
1411 PRE_EXPR_NARY (expr) = nary;
1412 fprintf (dump_file, " to ");
1413 print_pre_expr (dump_file, constant);
1414 fprintf (dump_file, "\n");
1416 return constant;
1420 else
1421 return constant;
1424 /* vn_nary_* do not valueize operands. */
1425 for (i = 0; i < newnary->length; ++i)
1426 if (TREE_CODE (newnary->op[i]) == SSA_NAME)
1427 newnary->op[i] = VN_INFO (newnary->op[i])->valnum;
1428 tree result = vn_nary_op_lookup_pieces (newnary->length,
1429 newnary->opcode,
1430 newnary->type,
1431 &newnary->op[0],
1432 &nary);
1433 if (result && is_gimple_min_invariant (result))
1434 return get_or_alloc_expr_for_constant (result);
1436 expr = pre_expr_pool.allocate ();
1437 expr->kind = NARY;
1438 expr->id = 0;
1439 if (nary && !nary->predicated_values)
1441 PRE_EXPR_NARY (expr) = nary;
1442 new_val_id = nary->value_id;
1443 get_or_alloc_expression_id (expr);
1445 else
1447 new_val_id = get_next_value_id ();
1448 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
1449 nary = vn_nary_op_insert_pieces (newnary->length,
1450 newnary->opcode,
1451 newnary->type,
1452 &newnary->op[0],
1453 result, new_val_id);
1454 PRE_EXPR_NARY (expr) = nary;
1455 get_or_alloc_expression_id (expr);
1457 add_to_value (new_val_id, expr);
1459 return expr;
1461 break;
1463 case REFERENCE:
1465 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1466 vec<vn_reference_op_s> operands = ref->operands;
1467 tree vuse = ref->vuse;
1468 tree newvuse = vuse;
1469 vec<vn_reference_op_s> newoperands = vNULL;
1470 bool changed = false, same_valid = true;
1471 unsigned int i, n;
1472 vn_reference_op_t operand;
1473 vn_reference_t newref;
1475 for (i = 0; operands.iterate (i, &operand); i++)
1477 pre_expr opresult;
1478 pre_expr leader;
1479 tree op[3];
1480 tree type = operand->type;
1481 vn_reference_op_s newop = *operand;
1482 op[0] = operand->op0;
1483 op[1] = operand->op1;
1484 op[2] = operand->op2;
1485 for (n = 0; n < 3; ++n)
1487 unsigned int op_val_id;
1488 if (!op[n])
1489 continue;
1490 if (TREE_CODE (op[n]) != SSA_NAME)
1492 /* We can't possibly insert these. */
1493 if (n != 0
1494 && !is_gimple_min_invariant (op[n]))
1495 break;
1496 continue;
1498 op_val_id = VN_INFO (op[n])->value_id;
1499 leader = find_leader_in_sets (op_val_id, set1, set2);
1500 opresult = phi_translate (dest, leader, set1, set2, e);
1501 if (opresult && opresult != leader)
1503 tree name = get_representative_for (opresult);
1504 changed |= name != op[n];
1505 op[n] = name;
1507 else if (!opresult)
1508 break;
1510 if (n != 3)
1512 newoperands.release ();
1513 return NULL;
1515 if (!changed)
1516 continue;
1517 if (!newoperands.exists ())
1518 newoperands = operands.copy ();
1519 /* We may have changed from an SSA_NAME to a constant */
1520 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1521 newop.opcode = TREE_CODE (op[0]);
1522 newop.type = type;
1523 newop.op0 = op[0];
1524 newop.op1 = op[1];
1525 newop.op2 = op[2];
1526 newoperands[i] = newop;
1528 gcc_checking_assert (i == operands.length ());
1530 if (vuse)
1532 newvuse = translate_vuse_through_block (newoperands.exists ()
1533 ? newoperands : operands,
1534 ref->set, ref->type,
1535 vuse, phiblock, pred,
1536 &same_valid);
1537 if (newvuse == NULL_TREE)
1539 newoperands.release ();
1540 return NULL;
1544 if (changed || newvuse != vuse)
1546 unsigned int new_val_id;
1548 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1549 ref->type,
1550 newoperands.exists ()
1551 ? newoperands : operands,
1552 &newref, VN_WALK);
1553 if (result)
1554 newoperands.release ();
1556 /* We can always insert constants, so if we have a partial
1557 redundant constant load of another type try to translate it
1558 to a constant of appropriate type. */
1559 if (result && is_gimple_min_invariant (result))
1561 tree tem = result;
1562 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1564 tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1565 if (tem && !is_gimple_min_invariant (tem))
1566 tem = NULL_TREE;
1568 if (tem)
1569 return get_or_alloc_expr_for_constant (tem);
1572 /* If we'd have to convert things we would need to validate
1573 if we can insert the translated expression. So fail
1574 here for now - we cannot insert an alias with a different
1575 type in the VN tables either, as that would assert. */
1576 if (result
1577 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1578 return NULL;
1579 else if (!result && newref
1580 && !useless_type_conversion_p (ref->type, newref->type))
1582 newoperands.release ();
1583 return NULL;
1586 expr = pre_expr_pool.allocate ();
1587 expr->kind = REFERENCE;
1588 expr->id = 0;
1590 if (newref)
1591 new_val_id = newref->value_id;
1592 else
1594 if (changed || !same_valid)
1596 new_val_id = get_next_value_id ();
1597 value_expressions.safe_grow_cleared
1598 (get_max_value_id () + 1);
1600 else
1601 new_val_id = ref->value_id;
1602 if (!newoperands.exists ())
1603 newoperands = operands.copy ();
1604 newref = vn_reference_insert_pieces (newvuse, ref->set,
1605 ref->type,
1606 newoperands,
1607 result, new_val_id);
1608 newoperands = vNULL;
1610 PRE_EXPR_REFERENCE (expr) = newref;
1611 get_or_alloc_expression_id (expr);
1612 add_to_value (new_val_id, expr);
1614 newoperands.release ();
1615 return expr;
1617 break;
1619 case NAME:
1621 tree name = PRE_EXPR_NAME (expr);
1622 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1623 /* If the SSA name is defined by a PHI node in this block,
1624 translate it. */
1625 if (gimple_code (def_stmt) == GIMPLE_PHI
1626 && gimple_bb (def_stmt) == phiblock)
1628 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1630 /* Handle constant. */
1631 if (is_gimple_min_invariant (def))
1632 return get_or_alloc_expr_for_constant (def);
1634 return get_or_alloc_expr_for_name (def);
1636 /* Otherwise return it unchanged - it will get removed if its
1637 value is not available in PREDs AVAIL_OUT set of expressions
1638 by the subtraction of TMP_GEN. */
1639 return expr;
1642 default:
1643 gcc_unreachable ();
1647 /* Wrapper around phi_translate_1 providing caching functionality. */
1649 static pre_expr
1650 phi_translate (bitmap_set_t dest, pre_expr expr,
1651 bitmap_set_t set1, bitmap_set_t set2, edge e)
1653 expr_pred_trans_t slot = NULL;
1654 pre_expr phitrans;
1656 if (!expr)
1657 return NULL;
1659 /* Constants contain no values that need translation. */
1660 if (expr->kind == CONSTANT)
1661 return expr;
1663 if (value_id_constant_p (get_expr_value_id (expr)))
1664 return expr;
1666 /* Don't add translations of NAMEs as those are cheap to translate. */
1667 if (expr->kind != NAME)
1669 if (phi_trans_add (&slot, expr, e->src))
1670 return slot->v;
1671 /* Store NULL for the value we want to return in the case of
1672 recursing. */
1673 slot->v = NULL;
1676 /* Translate. */
1677 basic_block saved_valueize_bb = vn_context_bb;
1678 vn_context_bb = e->src;
1679 phitrans = phi_translate_1 (dest, expr, set1, set2, e);
1680 vn_context_bb = saved_valueize_bb;
1682 if (slot)
1684 if (phitrans)
1685 slot->v = phitrans;
1686 else
1687 /* Remove failed translations again, they cause insert
1688 iteration to not pick up new opportunities reliably. */
1689 phi_translate_table->remove_elt_with_hash (slot, slot->hashcode);
1692 return phitrans;
1696 /* For each expression in SET, translate the values through phi nodes
1697 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1698 expressions in DEST. */
1700 static void
1701 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
1703 vec<pre_expr> exprs;
1704 pre_expr expr;
1705 int i;
1707 if (gimple_seq_empty_p (phi_nodes (e->dest)))
1709 bitmap_set_copy (dest, set);
1710 return;
1713 exprs = sorted_array_from_bitmap_set (set);
1714 FOR_EACH_VEC_ELT (exprs, i, expr)
1716 pre_expr translated;
1717 translated = phi_translate (dest, expr, set, NULL, e);
1718 if (!translated)
1719 continue;
1721 bitmap_insert_into_set (dest, translated);
1723 exprs.release ();
1726 /* Find the leader for a value (i.e., the name representing that
1727 value) in a given set, and return it. Return NULL if no leader
1728 is found. */
1730 static pre_expr
1731 bitmap_find_leader (bitmap_set_t set, unsigned int val)
1733 if (value_id_constant_p (val))
1735 unsigned int i;
1736 bitmap_iterator bi;
1737 bitmap exprset = value_expressions[val];
1739 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1741 pre_expr expr = expression_for_id (i);
1742 if (expr->kind == CONSTANT)
1743 return expr;
1746 if (bitmap_set_contains_value (set, val))
1748 /* Rather than walk the entire bitmap of expressions, and see
1749 whether any of them has the value we are looking for, we look
1750 at the reverse mapping, which tells us the set of expressions
1751 that have a given value (IE value->expressions with that
1752 value) and see if any of those expressions are in our set.
1753 The number of expressions per value is usually significantly
1754 less than the number of expressions in the set. In fact, for
1755 large testcases, doing it this way is roughly 5-10x faster
1756 than walking the bitmap.
1757 If this is somehow a significant lose for some cases, we can
1758 choose which set to walk based on which set is smaller. */
1759 unsigned int i;
1760 bitmap_iterator bi;
1761 bitmap exprset = value_expressions[val];
1763 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1764 return expression_for_id (i);
1766 return NULL;
1769 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1770 BLOCK by seeing if it is not killed in the block. Note that we are
1771 only determining whether there is a store that kills it. Because
1772 of the order in which clean iterates over values, we are guaranteed
1773 that altered operands will have caused us to be eliminated from the
1774 ANTIC_IN set already. */
1776 static bool
1777 value_dies_in_block_x (pre_expr expr, basic_block block)
1779 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1780 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1781 gimple *def;
1782 gimple_stmt_iterator gsi;
1783 unsigned id = get_expression_id (expr);
1784 bool res = false;
1785 ao_ref ref;
1787 if (!vuse)
1788 return false;
1790 /* Lookup a previously calculated result. */
1791 if (EXPR_DIES (block)
1792 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1793 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1795 /* A memory expression {e, VUSE} dies in the block if there is a
1796 statement that may clobber e. If, starting statement walk from the
1797 top of the basic block, a statement uses VUSE there can be no kill
1798 inbetween that use and the original statement that loaded {e, VUSE},
1799 so we can stop walking. */
1800 ref.base = NULL_TREE;
1801 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1803 tree def_vuse, def_vdef;
1804 def = gsi_stmt (gsi);
1805 def_vuse = gimple_vuse (def);
1806 def_vdef = gimple_vdef (def);
1808 /* Not a memory statement. */
1809 if (!def_vuse)
1810 continue;
1812 /* Not a may-def. */
1813 if (!def_vdef)
1815 /* A load with the same VUSE, we're done. */
1816 if (def_vuse == vuse)
1817 break;
1819 continue;
1822 /* Init ref only if we really need it. */
1823 if (ref.base == NULL_TREE
1824 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type,
1825 refx->operands))
1827 res = true;
1828 break;
1830 /* If the statement may clobber expr, it dies. */
1831 if (stmt_may_clobber_ref_p_1 (def, &ref))
1833 res = true;
1834 break;
1838 /* Remember the result. */
1839 if (!EXPR_DIES (block))
1840 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1841 bitmap_set_bit (EXPR_DIES (block), id * 2);
1842 if (res)
1843 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1845 return res;
1849 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1850 contains its value-id. */
1852 static bool
1853 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1855 if (op && TREE_CODE (op) == SSA_NAME)
1857 unsigned int value_id = VN_INFO (op)->value_id;
1858 if (!(bitmap_set_contains_value (set1, value_id)
1859 || (set2 && bitmap_set_contains_value (set2, value_id))))
1860 return false;
1862 return true;
1865 /* Determine if the expression EXPR is valid in SET1 U SET2.
1866 ONLY SET2 CAN BE NULL.
1867 This means that we have a leader for each part of the expression
1868 (if it consists of values), or the expression is an SSA_NAME.
1869 For loads/calls, we also see if the vuse is killed in this block. */
1871 static bool
1872 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
1874 switch (expr->kind)
1876 case NAME:
1877 /* By construction all NAMEs are available. Non-available
1878 NAMEs are removed by subtracting TMP_GEN from the sets. */
1879 return true;
1880 case NARY:
1882 unsigned int i;
1883 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1884 for (i = 0; i < nary->length; i++)
1885 if (!op_valid_in_sets (set1, set2, nary->op[i]))
1886 return false;
1887 return true;
1889 break;
1890 case REFERENCE:
1892 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1893 vn_reference_op_t vro;
1894 unsigned int i;
1896 FOR_EACH_VEC_ELT (ref->operands, i, vro)
1898 if (!op_valid_in_sets (set1, set2, vro->op0)
1899 || !op_valid_in_sets (set1, set2, vro->op1)
1900 || !op_valid_in_sets (set1, set2, vro->op2))
1901 return false;
1903 return true;
1905 default:
1906 gcc_unreachable ();
1910 /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
1911 This means expressions that are made up of values we have no leaders for
1912 in SET1 or SET2. */
1914 static void
1915 clean (bitmap_set_t set1, bitmap_set_t set2 = NULL)
1917 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
1918 pre_expr expr;
1919 int i;
1921 FOR_EACH_VEC_ELT (exprs, i, expr)
1923 if (!valid_in_sets (set1, set2, expr))
1925 unsigned int val = get_expr_value_id (expr);
1926 bitmap_clear_bit (&set1->expressions, get_expression_id (expr));
1927 /* We are entered with possibly multiple expressions for a value
1928 so before removing a value from the set see if there's an
1929 expression for it left. */
1930 if (! bitmap_find_leader (set1, val))
1931 bitmap_clear_bit (&set1->values, val);
1934 exprs.release ();
1937 /* Clean the set of expressions that are no longer valid in SET because
1938 they are clobbered in BLOCK or because they trap and may not be executed. */
1940 static void
1941 prune_clobbered_mems (bitmap_set_t set, basic_block block)
1943 bitmap_iterator bi;
1944 unsigned i;
1945 unsigned to_remove = -1U;
1946 bool any_removed = false;
1948 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1950 /* Remove queued expr. */
1951 if (to_remove != -1U)
1953 bitmap_clear_bit (&set->expressions, to_remove);
1954 any_removed = true;
1955 to_remove = -1U;
1958 pre_expr expr = expression_for_id (i);
1959 if (expr->kind == REFERENCE)
1961 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1962 if (ref->vuse)
1964 gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
1965 if (!gimple_nop_p (def_stmt)
1966 && ((gimple_bb (def_stmt) != block
1967 && !dominated_by_p (CDI_DOMINATORS,
1968 block, gimple_bb (def_stmt)))
1969 || (gimple_bb (def_stmt) == block
1970 && value_dies_in_block_x (expr, block))))
1971 to_remove = i;
1974 else if (expr->kind == NARY)
1976 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1977 /* If the NARY may trap make sure the block does not contain
1978 a possible exit point.
1979 ??? This is overly conservative if we translate AVAIL_OUT
1980 as the available expression might be after the exit point. */
1981 if (BB_MAY_NOTRETURN (block)
1982 && vn_nary_may_trap (nary))
1983 to_remove = i;
1987 /* Remove queued expr. */
1988 if (to_remove != -1U)
1990 bitmap_clear_bit (&set->expressions, to_remove);
1991 any_removed = true;
1994 /* Above we only removed expressions, now clean the set of values
1995 which no longer have any corresponding expression. We cannot
1996 clear the value at the time we remove an expression since there
1997 may be multiple expressions per value.
1998 If we'd queue possibly to be removed values we could use
1999 the bitmap_find_leader way to see if there's still an expression
2000 for it. For some ratio of to be removed values and number of
2001 values/expressions in the set this might be faster than rebuilding
2002 the value-set. */
2003 if (any_removed)
2005 bitmap_clear (&set->values);
2006 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2008 pre_expr expr = expression_for_id (i);
2009 unsigned int value_id = get_expr_value_id (expr);
2010 bitmap_set_bit (&set->values, value_id);
2015 static sbitmap has_abnormal_preds;
2017 /* Compute the ANTIC set for BLOCK.
2019 If succs(BLOCK) > 1 then
2020 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2021 else if succs(BLOCK) == 1 then
2022 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2024 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2026 Note that clean() is deferred until after the iteration. */
2028 static bool
2029 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2031 bitmap_set_t S, old, ANTIC_OUT;
2032 edge e;
2033 edge_iterator ei;
2035 bool was_visited = BB_VISITED (block);
2036 bool changed = ! BB_VISITED (block);
2037 BB_VISITED (block) = 1;
2038 old = ANTIC_OUT = S = NULL;
2040 /* If any edges from predecessors are abnormal, antic_in is empty,
2041 so do nothing. */
2042 if (block_has_abnormal_pred_edge)
2043 goto maybe_dump_sets;
2045 old = ANTIC_IN (block);
2046 ANTIC_OUT = bitmap_set_new ();
2048 /* If the block has no successors, ANTIC_OUT is empty. */
2049 if (EDGE_COUNT (block->succs) == 0)
2051 /* If we have one successor, we could have some phi nodes to
2052 translate through. */
2053 else if (single_succ_p (block))
2055 e = single_succ_edge (block);
2056 gcc_assert (BB_VISITED (e->dest));
2057 phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
2059 /* If we have multiple successors, we take the intersection of all of
2060 them. Note that in the case of loop exit phi nodes, we may have
2061 phis to translate through. */
2062 else
2064 size_t i;
2065 edge first = NULL;
2067 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2068 FOR_EACH_EDGE (e, ei, block->succs)
2070 if (!first
2071 && BB_VISITED (e->dest))
2072 first = e;
2073 else if (BB_VISITED (e->dest))
2074 worklist.quick_push (e);
2075 else
2077 /* Unvisited successors get their ANTIC_IN replaced by the
2078 maximal set to arrive at a maximum ANTIC_IN solution.
2079 We can ignore them in the intersection operation and thus
2080 need not explicitely represent that maximum solution. */
2081 if (dump_file && (dump_flags & TDF_DETAILS))
2082 fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n",
2083 e->src->index, e->dest->index);
2087 /* Of multiple successors we have to have visited one already
2088 which is guaranteed by iteration order. */
2089 gcc_assert (first != NULL);
2091 phi_translate_set (ANTIC_OUT, ANTIC_IN (first->dest), first);
2093 /* If we have multiple successors we need to intersect the ANTIC_OUT
2094 sets. For values that's a simple intersection but for
2095 expressions it is a union. Given we want to have a single
2096 expression per value in our sets we have to canonicalize.
2097 Avoid randomness and running into cycles like for PR82129 and
2098 canonicalize the expression we choose to the one with the
2099 lowest id. This requires we actually compute the union first. */
2100 FOR_EACH_VEC_ELT (worklist, i, e)
2102 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2104 bitmap_set_t tmp = bitmap_set_new ();
2105 phi_translate_set (tmp, ANTIC_IN (e->dest), e);
2106 bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
2107 bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
2108 bitmap_set_free (tmp);
2110 else
2112 bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
2113 bitmap_ior_into (&ANTIC_OUT->expressions,
2114 &ANTIC_IN (e->dest)->expressions);
2117 if (! worklist.is_empty ())
2119 /* Prune expressions not in the value set. */
2120 bitmap_iterator bi;
2121 unsigned int i;
2122 unsigned int to_clear = -1U;
2123 FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
2125 if (to_clear != -1U)
2127 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2128 to_clear = -1U;
2130 pre_expr expr = expression_for_id (i);
2131 unsigned int value_id = get_expr_value_id (expr);
2132 if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
2133 to_clear = i;
2135 if (to_clear != -1U)
2136 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2140 /* Prune expressions that are clobbered in block and thus become
2141 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2142 prune_clobbered_mems (ANTIC_OUT, block);
2144 /* Generate ANTIC_OUT - TMP_GEN. */
2145 S = bitmap_set_subtract_expressions (ANTIC_OUT, TMP_GEN (block));
2147 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2148 ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
2149 TMP_GEN (block));
2151 /* Then union in the ANTIC_OUT - TMP_GEN values,
2152 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2153 bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
2154 bitmap_ior_into (&ANTIC_IN (block)->expressions, &S->expressions);
2156 /* clean (ANTIC_IN (block)) is defered to after the iteration converged
2157 because it can cause non-convergence, see for example PR81181. */
2159 /* Intersect ANTIC_IN with the old ANTIC_IN. This is required until
2160 we properly represent the maximum expression set, thus not prune
2161 values without expressions during the iteration. */
2162 if (was_visited
2163 && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
2165 if (dump_file && (dump_flags & TDF_DETAILS))
2166 fprintf (dump_file, "warning: intersecting with old ANTIC_IN "
2167 "shrinks the set\n");
2168 /* Prune expressions not in the value set. */
2169 bitmap_iterator bi;
2170 unsigned int i;
2171 unsigned int to_clear = -1U;
2172 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
2174 if (to_clear != -1U)
2176 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2177 to_clear = -1U;
2179 pre_expr expr = expression_for_id (i);
2180 unsigned int value_id = get_expr_value_id (expr);
2181 if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
2182 to_clear = i;
2184 if (to_clear != -1U)
2185 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2188 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2189 changed = true;
2191 maybe_dump_sets:
2192 if (dump_file && (dump_flags & TDF_DETAILS))
2194 if (ANTIC_OUT)
2195 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2197 if (changed)
2198 fprintf (dump_file, "[changed] ");
2199 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2200 block->index);
2202 if (S)
2203 print_bitmap_set (dump_file, S, "S", block->index);
2205 if (old)
2206 bitmap_set_free (old);
2207 if (S)
2208 bitmap_set_free (S);
2209 if (ANTIC_OUT)
2210 bitmap_set_free (ANTIC_OUT);
2211 return changed;
2214 /* Compute PARTIAL_ANTIC for BLOCK.
2216 If succs(BLOCK) > 1 then
2217 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2218 in ANTIC_OUT for all succ(BLOCK)
2219 else if succs(BLOCK) == 1 then
2220 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2222 PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
2225 static void
2226 compute_partial_antic_aux (basic_block block,
2227 bool block_has_abnormal_pred_edge)
2229 bitmap_set_t old_PA_IN;
2230 bitmap_set_t PA_OUT;
2231 edge e;
2232 edge_iterator ei;
2233 unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH);
2235 old_PA_IN = PA_OUT = NULL;
2237 /* If any edges from predecessors are abnormal, antic_in is empty,
2238 so do nothing. */
2239 if (block_has_abnormal_pred_edge)
2240 goto maybe_dump_sets;
2242 /* If there are too many partially anticipatable values in the
2243 block, phi_translate_set can take an exponential time: stop
2244 before the translation starts. */
2245 if (max_pa
2246 && single_succ_p (block)
2247 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2248 goto maybe_dump_sets;
2250 old_PA_IN = PA_IN (block);
2251 PA_OUT = bitmap_set_new ();
2253 /* If the block has no successors, ANTIC_OUT is empty. */
2254 if (EDGE_COUNT (block->succs) == 0)
2256 /* If we have one successor, we could have some phi nodes to
2257 translate through. Note that we can't phi translate across DFS
2258 back edges in partial antic, because it uses a union operation on
2259 the successors. For recurrences like IV's, we will end up
2260 generating a new value in the set on each go around (i + 3 (VH.1)
2261 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2262 else if (single_succ_p (block))
2264 e = single_succ_edge (block);
2265 if (!(e->flags & EDGE_DFS_BACK))
2266 phi_translate_set (PA_OUT, PA_IN (e->dest), e);
2268 /* If we have multiple successors, we take the union of all of
2269 them. */
2270 else
2272 size_t i;
2274 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2275 FOR_EACH_EDGE (e, ei, block->succs)
2277 if (e->flags & EDGE_DFS_BACK)
2278 continue;
2279 worklist.quick_push (e);
2281 if (worklist.length () > 0)
2283 FOR_EACH_VEC_ELT (worklist, i, e)
2285 unsigned int i;
2286 bitmap_iterator bi;
2288 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
2289 bitmap_value_insert_into_set (PA_OUT,
2290 expression_for_id (i));
2291 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2293 bitmap_set_t pa_in = bitmap_set_new ();
2294 phi_translate_set (pa_in, PA_IN (e->dest), e);
2295 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2296 bitmap_value_insert_into_set (PA_OUT,
2297 expression_for_id (i));
2298 bitmap_set_free (pa_in);
2300 else
2301 FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
2302 bitmap_value_insert_into_set (PA_OUT,
2303 expression_for_id (i));
2308 /* Prune expressions that are clobbered in block and thus become
2309 invalid if translated from PA_OUT to PA_IN. */
2310 prune_clobbered_mems (PA_OUT, block);
2312 /* PA_IN starts with PA_OUT - TMP_GEN.
2313 Then we subtract things from ANTIC_IN. */
2314 PA_IN (block) = bitmap_set_subtract_expressions (PA_OUT, TMP_GEN (block));
2316 /* For partial antic, we want to put back in the phi results, since
2317 we will properly avoid making them partially antic over backedges. */
2318 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2319 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2321 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2322 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2324 clean (PA_IN (block), ANTIC_IN (block));
2326 maybe_dump_sets:
2327 if (dump_file && (dump_flags & TDF_DETAILS))
2329 if (PA_OUT)
2330 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2332 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2334 if (old_PA_IN)
2335 bitmap_set_free (old_PA_IN);
2336 if (PA_OUT)
2337 bitmap_set_free (PA_OUT);
2340 /* Compute ANTIC and partial ANTIC sets. */
2342 static void
2343 compute_antic (void)
2345 bool changed = true;
2346 int num_iterations = 0;
2347 basic_block block;
2348 int i;
2349 edge_iterator ei;
2350 edge e;
2352 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2353 We pre-build the map of blocks with incoming abnormal edges here. */
2354 has_abnormal_preds = sbitmap_alloc (last_basic_block_for_fn (cfun));
2355 bitmap_clear (has_abnormal_preds);
2357 FOR_ALL_BB_FN (block, cfun)
2359 BB_VISITED (block) = 0;
2361 FOR_EACH_EDGE (e, ei, block->preds)
2362 if (e->flags & EDGE_ABNORMAL)
2364 bitmap_set_bit (has_abnormal_preds, block->index);
2365 break;
2368 /* While we are here, give empty ANTIC_IN sets to each block. */
2369 ANTIC_IN (block) = bitmap_set_new ();
2370 if (do_partial_partial)
2371 PA_IN (block) = bitmap_set_new ();
2374 /* At the exit block we anticipate nothing. */
2375 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2377 /* For ANTIC computation we need a postorder that also guarantees that
2378 a block with a single successor is visited after its successor.
2379 RPO on the inverted CFG has this property. */
2380 auto_vec<int, 20> postorder;
2381 inverted_post_order_compute (&postorder);
2383 auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
2384 bitmap_clear (worklist);
2385 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2386 bitmap_set_bit (worklist, e->src->index);
2387 while (changed)
2389 if (dump_file && (dump_flags & TDF_DETAILS))
2390 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2391 /* ??? We need to clear our PHI translation cache here as the
2392 ANTIC sets shrink and we restrict valid translations to
2393 those having operands with leaders in ANTIC. Same below
2394 for PA ANTIC computation. */
2395 num_iterations++;
2396 changed = false;
2397 for (i = postorder.length () - 1; i >= 0; i--)
2399 if (bitmap_bit_p (worklist, postorder[i]))
2401 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2402 bitmap_clear_bit (worklist, block->index);
2403 if (compute_antic_aux (block,
2404 bitmap_bit_p (has_abnormal_preds,
2405 block->index)))
2407 FOR_EACH_EDGE (e, ei, block->preds)
2408 bitmap_set_bit (worklist, e->src->index);
2409 changed = true;
2413 /* Theoretically possible, but *highly* unlikely. */
2414 gcc_checking_assert (num_iterations < 500);
2417 /* We have to clean after the dataflow problem converged as cleaning
2418 can cause non-convergence because it is based on expressions
2419 rather than values. */
2420 FOR_EACH_BB_FN (block, cfun)
2421 clean (ANTIC_IN (block));
2423 statistics_histogram_event (cfun, "compute_antic iterations",
2424 num_iterations);
2426 if (do_partial_partial)
2428 /* For partial antic we ignore backedges and thus we do not need
2429 to perform any iteration when we process blocks in postorder. */
2430 for (i = postorder.length () - 1; i >= 0; i--)
2432 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2433 compute_partial_antic_aux (block,
2434 bitmap_bit_p (has_abnormal_preds,
2435 block->index));
2439 sbitmap_free (has_abnormal_preds);
2443 /* Inserted expressions are placed onto this worklist, which is used
2444 for performing quick dead code elimination of insertions we made
2445 that didn't turn out to be necessary. */
2446 static bitmap inserted_exprs;
2448 /* The actual worker for create_component_ref_by_pieces. */
2450 static tree
2451 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2452 unsigned int *operand, gimple_seq *stmts)
2454 vn_reference_op_t currop = &ref->operands[*operand];
2455 tree genop;
2456 ++*operand;
2457 switch (currop->opcode)
2459 case CALL_EXPR:
2460 gcc_unreachable ();
2462 case MEM_REF:
2464 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2465 stmts);
2466 if (!baseop)
2467 return NULL_TREE;
2468 tree offset = currop->op0;
2469 if (TREE_CODE (baseop) == ADDR_EXPR
2470 && handled_component_p (TREE_OPERAND (baseop, 0)))
2472 poly_int64 off;
2473 tree base;
2474 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2475 &off);
2476 gcc_assert (base);
2477 offset = int_const_binop (PLUS_EXPR, offset,
2478 build_int_cst (TREE_TYPE (offset),
2479 off));
2480 baseop = build_fold_addr_expr (base);
2482 genop = build2 (MEM_REF, currop->type, baseop, offset);
2483 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2484 MR_DEPENDENCE_BASE (genop) = currop->base;
2485 REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
2486 return genop;
2489 case TARGET_MEM_REF:
2491 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2492 vn_reference_op_t nextop = &ref->operands[++*operand];
2493 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2494 stmts);
2495 if (!baseop)
2496 return NULL_TREE;
2497 if (currop->op0)
2499 genop0 = find_or_generate_expression (block, currop->op0, stmts);
2500 if (!genop0)
2501 return NULL_TREE;
2503 if (nextop->op0)
2505 genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2506 if (!genop1)
2507 return NULL_TREE;
2509 genop = build5 (TARGET_MEM_REF, currop->type,
2510 baseop, currop->op2, genop0, currop->op1, genop1);
2512 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2513 MR_DEPENDENCE_BASE (genop) = currop->base;
2514 return genop;
2517 case ADDR_EXPR:
2518 if (currop->op0)
2520 gcc_assert (is_gimple_min_invariant (currop->op0));
2521 return currop->op0;
2523 /* Fallthrough. */
2524 case REALPART_EXPR:
2525 case IMAGPART_EXPR:
2526 case VIEW_CONVERT_EXPR:
2528 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2529 stmts);
2530 if (!genop0)
2531 return NULL_TREE;
2532 return fold_build1 (currop->opcode, currop->type, genop0);
2535 case WITH_SIZE_EXPR:
2537 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2538 stmts);
2539 if (!genop0)
2540 return NULL_TREE;
2541 tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2542 if (!genop1)
2543 return NULL_TREE;
2544 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2547 case BIT_FIELD_REF:
2549 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2550 stmts);
2551 if (!genop0)
2552 return NULL_TREE;
2553 tree op1 = currop->op0;
2554 tree op2 = currop->op1;
2555 tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2556 REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
2557 return fold (t);
2560 /* For array ref vn_reference_op's, operand 1 of the array ref
2561 is op0 of the reference op and operand 3 of the array ref is
2562 op1. */
2563 case ARRAY_RANGE_REF:
2564 case ARRAY_REF:
2566 tree genop0;
2567 tree genop1 = currop->op0;
2568 tree genop2 = currop->op1;
2569 tree genop3 = currop->op2;
2570 genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2571 stmts);
2572 if (!genop0)
2573 return NULL_TREE;
2574 genop1 = find_or_generate_expression (block, genop1, stmts);
2575 if (!genop1)
2576 return NULL_TREE;
2577 if (genop2)
2579 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2580 /* Drop zero minimum index if redundant. */
2581 if (integer_zerop (genop2)
2582 && (!domain_type
2583 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2584 genop2 = NULL_TREE;
2585 else
2587 genop2 = find_or_generate_expression (block, genop2, stmts);
2588 if (!genop2)
2589 return NULL_TREE;
2592 if (genop3)
2594 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2595 /* We can't always put a size in units of the element alignment
2596 here as the element alignment may be not visible. See
2597 PR43783. Simply drop the element size for constant
2598 sizes. */
2599 if (TREE_CODE (genop3) == INTEGER_CST
2600 && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
2601 && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
2602 (wi::to_offset (genop3)
2603 * vn_ref_op_align_unit (currop))))
2604 genop3 = NULL_TREE;
2605 else
2607 genop3 = find_or_generate_expression (block, genop3, stmts);
2608 if (!genop3)
2609 return NULL_TREE;
2612 return build4 (currop->opcode, currop->type, genop0, genop1,
2613 genop2, genop3);
2615 case COMPONENT_REF:
2617 tree op0;
2618 tree op1;
2619 tree genop2 = currop->op1;
2620 op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2621 if (!op0)
2622 return NULL_TREE;
2623 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2624 op1 = currop->op0;
2625 if (genop2)
2627 genop2 = find_or_generate_expression (block, genop2, stmts);
2628 if (!genop2)
2629 return NULL_TREE;
2631 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2634 case SSA_NAME:
2636 genop = find_or_generate_expression (block, currop->op0, stmts);
2637 return genop;
2639 case STRING_CST:
2640 case INTEGER_CST:
2641 case COMPLEX_CST:
2642 case VECTOR_CST:
2643 case REAL_CST:
2644 case CONSTRUCTOR:
2645 case VAR_DECL:
2646 case PARM_DECL:
2647 case CONST_DECL:
2648 case RESULT_DECL:
2649 case FUNCTION_DECL:
2650 return currop->op0;
2652 default:
2653 gcc_unreachable ();
2657 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2658 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2659 trying to rename aggregates into ssa form directly, which is a no no.
2661 Thus, this routine doesn't create temporaries, it just builds a
2662 single access expression for the array, calling
2663 find_or_generate_expression to build the innermost pieces.
2665 This function is a subroutine of create_expression_by_pieces, and
2666 should not be called on it's own unless you really know what you
2667 are doing. */
2669 static tree
2670 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2671 gimple_seq *stmts)
2673 unsigned int op = 0;
2674 return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2677 /* Find a simple leader for an expression, or generate one using
2678 create_expression_by_pieces from a NARY expression for the value.
2679 BLOCK is the basic_block we are looking for leaders in.
2680 OP is the tree expression to find a leader for or generate.
2681 Returns the leader or NULL_TREE on failure. */
2683 static tree
2684 find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2686 pre_expr expr = get_or_alloc_expr_for (op);
2687 unsigned int lookfor = get_expr_value_id (expr);
2688 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2689 if (leader)
2691 if (leader->kind == NAME)
2692 return PRE_EXPR_NAME (leader);
2693 else if (leader->kind == CONSTANT)
2694 return PRE_EXPR_CONSTANT (leader);
2696 /* Defer. */
2697 return NULL_TREE;
2700 /* It must be a complex expression, so generate it recursively. Note
2701 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2702 where the insert algorithm fails to insert a required expression. */
2703 bitmap exprset = value_expressions[lookfor];
2704 bitmap_iterator bi;
2705 unsigned int i;
2706 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2708 pre_expr temp = expression_for_id (i);
2709 /* We cannot insert random REFERENCE expressions at arbitrary
2710 places. We can insert NARYs which eventually re-materializes
2711 its operand values. */
2712 if (temp->kind == NARY)
2713 return create_expression_by_pieces (block, temp, stmts,
2714 get_expr_type (expr));
2717 /* Defer. */
2718 return NULL_TREE;
2721 /* Create an expression in pieces, so that we can handle very complex
2722 expressions that may be ANTIC, but not necessary GIMPLE.
2723 BLOCK is the basic block the expression will be inserted into,
2724 EXPR is the expression to insert (in value form)
2725 STMTS is a statement list to append the necessary insertions into.
2727 This function will die if we hit some value that shouldn't be
2728 ANTIC but is (IE there is no leader for it, or its components).
2729 The function returns NULL_TREE in case a different antic expression
2730 has to be inserted first.
2731 This function may also generate expressions that are themselves
2732 partially or fully redundant. Those that are will be either made
2733 fully redundant during the next iteration of insert (for partially
2734 redundant ones), or eliminated by eliminate (for fully redundant
2735 ones). */
2737 static tree
2738 create_expression_by_pieces (basic_block block, pre_expr expr,
2739 gimple_seq *stmts, tree type)
2741 tree name;
2742 tree folded;
2743 gimple_seq forced_stmts = NULL;
2744 unsigned int value_id;
2745 gimple_stmt_iterator gsi;
2746 tree exprtype = type ? type : get_expr_type (expr);
2747 pre_expr nameexpr;
2748 gassign *newstmt;
2750 switch (expr->kind)
2752 /* We may hit the NAME/CONSTANT case if we have to convert types
2753 that value numbering saw through. */
2754 case NAME:
2755 folded = PRE_EXPR_NAME (expr);
2756 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
2757 return NULL_TREE;
2758 if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2759 return folded;
2760 break;
2761 case CONSTANT:
2763 folded = PRE_EXPR_CONSTANT (expr);
2764 tree tem = fold_convert (exprtype, folded);
2765 if (is_gimple_min_invariant (tem))
2766 return tem;
2767 break;
2769 case REFERENCE:
2770 if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
2772 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2773 unsigned int operand = 1;
2774 vn_reference_op_t currop = &ref->operands[0];
2775 tree sc = NULL_TREE;
2776 tree fn = find_or_generate_expression (block, currop->op0, stmts);
2777 if (!fn)
2778 return NULL_TREE;
2779 if (currop->op1)
2781 sc = find_or_generate_expression (block, currop->op1, stmts);
2782 if (!sc)
2783 return NULL_TREE;
2785 auto_vec<tree> args (ref->operands.length () - 1);
2786 while (operand < ref->operands.length ())
2788 tree arg = create_component_ref_by_pieces_1 (block, ref,
2789 &operand, stmts);
2790 if (!arg)
2791 return NULL_TREE;
2792 args.quick_push (arg);
2794 gcall *call = gimple_build_call_vec (fn, args);
2795 if (sc)
2796 gimple_call_set_chain (call, sc);
2797 tree forcedname = make_ssa_name (currop->type);
2798 gimple_call_set_lhs (call, forcedname);
2799 /* There's no CCP pass after PRE which would re-compute alignment
2800 information so make sure we re-materialize this here. */
2801 if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED)
2802 && args.length () - 2 <= 1
2803 && tree_fits_uhwi_p (args[1])
2804 && (args.length () != 3 || tree_fits_uhwi_p (args[2])))
2806 unsigned HOST_WIDE_INT halign = tree_to_uhwi (args[1]);
2807 unsigned HOST_WIDE_INT hmisalign
2808 = args.length () == 3 ? tree_to_uhwi (args[2]) : 0;
2809 if ((halign & (halign - 1)) == 0
2810 && (hmisalign & ~(halign - 1)) == 0)
2811 set_ptr_info_alignment (get_ptr_info (forcedname),
2812 halign, hmisalign);
2814 gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
2815 gimple_seq_add_stmt_without_update (&forced_stmts, call);
2816 folded = forcedname;
2818 else
2820 folded = create_component_ref_by_pieces (block,
2821 PRE_EXPR_REFERENCE (expr),
2822 stmts);
2823 if (!folded)
2824 return NULL_TREE;
2825 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2826 newstmt = gimple_build_assign (name, folded);
2827 gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2828 gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
2829 folded = name;
2831 break;
2832 case NARY:
2834 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2835 tree *genop = XALLOCAVEC (tree, nary->length);
2836 unsigned i;
2837 for (i = 0; i < nary->length; ++i)
2839 genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2840 if (!genop[i])
2841 return NULL_TREE;
2842 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2843 may have conversions stripped. */
2844 if (nary->opcode == POINTER_PLUS_EXPR)
2846 if (i == 0)
2847 genop[i] = gimple_convert (&forced_stmts,
2848 nary->type, genop[i]);
2849 else if (i == 1)
2850 genop[i] = gimple_convert (&forced_stmts,
2851 sizetype, genop[i]);
2853 else
2854 genop[i] = gimple_convert (&forced_stmts,
2855 TREE_TYPE (nary->op[i]), genop[i]);
2857 if (nary->opcode == CONSTRUCTOR)
2859 vec<constructor_elt, va_gc> *elts = NULL;
2860 for (i = 0; i < nary->length; ++i)
2861 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2862 folded = build_constructor (nary->type, elts);
2863 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2864 newstmt = gimple_build_assign (name, folded);
2865 gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2866 folded = name;
2868 else
2870 switch (nary->length)
2872 case 1:
2873 folded = gimple_build (&forced_stmts, nary->opcode, nary->type,
2874 genop[0]);
2875 break;
2876 case 2:
2877 folded = gimple_build (&forced_stmts, nary->opcode, nary->type,
2878 genop[0], genop[1]);
2879 break;
2880 case 3:
2881 folded = gimple_build (&forced_stmts, nary->opcode, nary->type,
2882 genop[0], genop[1], genop[2]);
2883 break;
2884 default:
2885 gcc_unreachable ();
2889 break;
2890 default:
2891 gcc_unreachable ();
2894 folded = gimple_convert (&forced_stmts, exprtype, folded);
2896 /* If there is nothing to insert, return the simplified result. */
2897 if (gimple_seq_empty_p (forced_stmts))
2898 return folded;
2899 /* If we simplified to a constant return it and discard eventually
2900 built stmts. */
2901 if (is_gimple_min_invariant (folded))
2903 gimple_seq_discard (forced_stmts);
2904 return folded;
2906 /* Likewise if we simplified to sth not queued for insertion. */
2907 bool found = false;
2908 gsi = gsi_last (forced_stmts);
2909 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2911 gimple *stmt = gsi_stmt (gsi);
2912 tree forcedname = gimple_get_lhs (stmt);
2913 if (forcedname == folded)
2915 found = true;
2916 break;
2919 if (! found)
2921 gimple_seq_discard (forced_stmts);
2922 return folded;
2924 gcc_assert (TREE_CODE (folded) == SSA_NAME);
2926 /* If we have any intermediate expressions to the value sets, add them
2927 to the value sets and chain them in the instruction stream. */
2928 if (forced_stmts)
2930 gsi = gsi_start (forced_stmts);
2931 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2933 gimple *stmt = gsi_stmt (gsi);
2934 tree forcedname = gimple_get_lhs (stmt);
2935 pre_expr nameexpr;
2937 if (forcedname != folded)
2939 VN_INFO (forcedname)->valnum = forcedname;
2940 VN_INFO (forcedname)->value_id = get_next_value_id ();
2941 nameexpr = get_or_alloc_expr_for_name (forcedname);
2942 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
2943 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2944 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2947 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
2949 gimple_seq_add_seq (stmts, forced_stmts);
2952 name = folded;
2954 /* Fold the last statement. */
2955 gsi = gsi_last (*stmts);
2956 if (fold_stmt_inplace (&gsi))
2957 update_stmt (gsi_stmt (gsi));
2959 /* Add a value number to the temporary.
2960 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2961 we are creating the expression by pieces, and this particular piece of
2962 the expression may have been represented. There is no harm in replacing
2963 here. */
2964 value_id = get_expr_value_id (expr);
2965 VN_INFO (name)->value_id = value_id;
2966 VN_INFO (name)->valnum = vn_valnum_from_value_id (value_id);
2967 if (VN_INFO (name)->valnum == NULL_TREE)
2968 VN_INFO (name)->valnum = name;
2969 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
2970 nameexpr = get_or_alloc_expr_for_name (name);
2971 add_to_value (value_id, nameexpr);
2972 if (NEW_SETS (block))
2973 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2974 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2976 pre_stats.insertions++;
2977 if (dump_file && (dump_flags & TDF_DETAILS))
2979 fprintf (dump_file, "Inserted ");
2980 print_gimple_stmt (dump_file, gsi_stmt (gsi_last (*stmts)), 0);
2981 fprintf (dump_file, " in predecessor %d (%04d)\n",
2982 block->index, value_id);
2985 return name;
2989 /* Insert the to-be-made-available values of expression EXPRNUM for each
2990 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2991 merge the result with a phi node, given the same value number as
2992 NODE. Return true if we have inserted new stuff. */
2994 static bool
2995 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
2996 vec<pre_expr> avail)
2998 pre_expr expr = expression_for_id (exprnum);
2999 pre_expr newphi;
3000 unsigned int val = get_expr_value_id (expr);
3001 edge pred;
3002 bool insertions = false;
3003 bool nophi = false;
3004 basic_block bprime;
3005 pre_expr eprime;
3006 edge_iterator ei;
3007 tree type = get_expr_type (expr);
3008 tree temp;
3009 gphi *phi;
3011 /* Make sure we aren't creating an induction variable. */
3012 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3014 bool firstinsideloop = false;
3015 bool secondinsideloop = false;
3016 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3017 EDGE_PRED (block, 0)->src);
3018 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3019 EDGE_PRED (block, 1)->src);
3020 /* Induction variables only have one edge inside the loop. */
3021 if ((firstinsideloop ^ secondinsideloop)
3022 && expr->kind != REFERENCE)
3024 if (dump_file && (dump_flags & TDF_DETAILS))
3025 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3026 nophi = true;
3030 /* Make the necessary insertions. */
3031 FOR_EACH_EDGE (pred, ei, block->preds)
3033 gimple_seq stmts = NULL;
3034 tree builtexpr;
3035 bprime = pred->src;
3036 eprime = avail[pred->dest_idx];
3037 builtexpr = create_expression_by_pieces (bprime, eprime,
3038 &stmts, type);
3039 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3040 if (!gimple_seq_empty_p (stmts))
3042 basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
3043 gcc_assert (! new_bb);
3044 insertions = true;
3046 if (!builtexpr)
3048 /* We cannot insert a PHI node if we failed to insert
3049 on one edge. */
3050 nophi = true;
3051 continue;
3053 if (is_gimple_min_invariant (builtexpr))
3054 avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
3055 else
3056 avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3058 /* If we didn't want a phi node, and we made insertions, we still have
3059 inserted new stuff, and thus return true. If we didn't want a phi node,
3060 and didn't make insertions, we haven't added anything new, so return
3061 false. */
3062 if (nophi && insertions)
3063 return true;
3064 else if (nophi && !insertions)
3065 return false;
3067 /* Now build a phi for the new variable. */
3068 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3069 phi = create_phi_node (temp, block);
3071 VN_INFO (temp)->value_id = val;
3072 VN_INFO (temp)->valnum = vn_valnum_from_value_id (val);
3073 if (VN_INFO (temp)->valnum == NULL_TREE)
3074 VN_INFO (temp)->valnum = temp;
3075 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3076 FOR_EACH_EDGE (pred, ei, block->preds)
3078 pre_expr ae = avail[pred->dest_idx];
3079 gcc_assert (get_expr_type (ae) == type
3080 || useless_type_conversion_p (type, get_expr_type (ae)));
3081 if (ae->kind == CONSTANT)
3082 add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3083 pred, UNKNOWN_LOCATION);
3084 else
3085 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3088 newphi = get_or_alloc_expr_for_name (temp);
3089 add_to_value (val, newphi);
3091 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3092 this insertion, since we test for the existence of this value in PHI_GEN
3093 before proceeding with the partial redundancy checks in insert_aux.
3095 The value may exist in AVAIL_OUT, in particular, it could be represented
3096 by the expression we are trying to eliminate, in which case we want the
3097 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3098 inserted there.
3100 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3101 this block, because if it did, it would have existed in our dominator's
3102 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3105 bitmap_insert_into_set (PHI_GEN (block), newphi);
3106 bitmap_value_replace_in_set (AVAIL_OUT (block),
3107 newphi);
3108 bitmap_insert_into_set (NEW_SETS (block),
3109 newphi);
3111 /* If we insert a PHI node for a conversion of another PHI node
3112 in the same basic-block try to preserve range information.
3113 This is important so that followup loop passes receive optimal
3114 number of iteration analysis results. See PR61743. */
3115 if (expr->kind == NARY
3116 && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3117 && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3118 && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3119 && INTEGRAL_TYPE_P (type)
3120 && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3121 && (TYPE_PRECISION (type)
3122 >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3123 && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3125 wide_int min, max;
3126 if (get_range_info (expr->u.nary->op[0], &min, &max) == VR_RANGE
3127 && !wi::neg_p (min, SIGNED)
3128 && !wi::neg_p (max, SIGNED))
3129 /* Just handle extension and sign-changes of all-positive ranges. */
3130 set_range_info (temp,
3131 SSA_NAME_RANGE_TYPE (expr->u.nary->op[0]),
3132 wide_int_storage::from (min, TYPE_PRECISION (type),
3133 TYPE_SIGN (type)),
3134 wide_int_storage::from (max, TYPE_PRECISION (type),
3135 TYPE_SIGN (type)));
3138 if (dump_file && (dump_flags & TDF_DETAILS))
3140 fprintf (dump_file, "Created phi ");
3141 print_gimple_stmt (dump_file, phi, 0);
3142 fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3144 pre_stats.phis++;
3145 return true;
3150 /* Perform insertion of partially redundant or hoistable values.
3151 For BLOCK, do the following:
3152 1. Propagate the NEW_SETS of the dominator into the current block.
3153 If the block has multiple predecessors,
3154 2a. Iterate over the ANTIC expressions for the block to see if
3155 any of them are partially redundant.
3156 2b. If so, insert them into the necessary predecessors to make
3157 the expression fully redundant.
3158 2c. Insert a new PHI merging the values of the predecessors.
3159 2d. Insert the new PHI, and the new expressions, into the
3160 NEW_SETS set.
3161 If the block has multiple successors,
3162 3a. Iterate over the ANTIC values for the block to see if
3163 any of them are good candidates for hoisting.
3164 3b. If so, insert expressions computing the values in BLOCK,
3165 and add the new expressions into the NEW_SETS set.
3166 4. Recursively call ourselves on the dominator children of BLOCK.
3168 Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3169 do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3170 done in do_hoist_insertion.
3173 static bool
3174 do_pre_regular_insertion (basic_block block, basic_block dom)
3176 bool new_stuff = false;
3177 vec<pre_expr> exprs;
3178 pre_expr expr;
3179 auto_vec<pre_expr> avail;
3180 int i;
3182 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3183 avail.safe_grow (EDGE_COUNT (block->preds));
3185 FOR_EACH_VEC_ELT (exprs, i, expr)
3187 if (expr->kind == NARY
3188 || expr->kind == REFERENCE)
3190 unsigned int val;
3191 bool by_some = false;
3192 bool cant_insert = false;
3193 bool all_same = true;
3194 pre_expr first_s = NULL;
3195 edge pred;
3196 basic_block bprime;
3197 pre_expr eprime = NULL;
3198 edge_iterator ei;
3199 pre_expr edoubleprime = NULL;
3200 bool do_insertion = false;
3202 val = get_expr_value_id (expr);
3203 if (bitmap_set_contains_value (PHI_GEN (block), val))
3204 continue;
3205 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3207 if (dump_file && (dump_flags & TDF_DETAILS))
3209 fprintf (dump_file, "Found fully redundant value: ");
3210 print_pre_expr (dump_file, expr);
3211 fprintf (dump_file, "\n");
3213 continue;
3216 FOR_EACH_EDGE (pred, ei, block->preds)
3218 unsigned int vprime;
3220 /* We should never run insertion for the exit block
3221 and so not come across fake pred edges. */
3222 gcc_assert (!(pred->flags & EDGE_FAKE));
3223 bprime = pred->src;
3224 /* We are looking at ANTIC_OUT of bprime. */
3225 eprime = phi_translate (NULL, expr, ANTIC_IN (block), NULL, pred);
3227 /* eprime will generally only be NULL if the
3228 value of the expression, translated
3229 through the PHI for this predecessor, is
3230 undefined. If that is the case, we can't
3231 make the expression fully redundant,
3232 because its value is undefined along a
3233 predecessor path. We can thus break out
3234 early because it doesn't matter what the
3235 rest of the results are. */
3236 if (eprime == NULL)
3238 avail[pred->dest_idx] = NULL;
3239 cant_insert = true;
3240 break;
3243 vprime = get_expr_value_id (eprime);
3244 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3245 vprime);
3246 if (edoubleprime == NULL)
3248 avail[pred->dest_idx] = eprime;
3249 all_same = false;
3251 else
3253 avail[pred->dest_idx] = edoubleprime;
3254 by_some = true;
3255 /* We want to perform insertions to remove a redundancy on
3256 a path in the CFG we want to optimize for speed. */
3257 if (optimize_edge_for_speed_p (pred))
3258 do_insertion = true;
3259 if (first_s == NULL)
3260 first_s = edoubleprime;
3261 else if (!pre_expr_d::equal (first_s, edoubleprime))
3262 all_same = false;
3265 /* If we can insert it, it's not the same value
3266 already existing along every predecessor, and
3267 it's defined by some predecessor, it is
3268 partially redundant. */
3269 if (!cant_insert && !all_same && by_some)
3271 if (!do_insertion)
3273 if (dump_file && (dump_flags & TDF_DETAILS))
3275 fprintf (dump_file, "Skipping partial redundancy for "
3276 "expression ");
3277 print_pre_expr (dump_file, expr);
3278 fprintf (dump_file, " (%04d), no redundancy on to be "
3279 "optimized for speed edge\n", val);
3282 else if (dbg_cnt (treepre_insert))
3284 if (dump_file && (dump_flags & TDF_DETAILS))
3286 fprintf (dump_file, "Found partial redundancy for "
3287 "expression ");
3288 print_pre_expr (dump_file, expr);
3289 fprintf (dump_file, " (%04d)\n",
3290 get_expr_value_id (expr));
3292 if (insert_into_preds_of_block (block,
3293 get_expression_id (expr),
3294 avail))
3295 new_stuff = true;
3298 /* If all edges produce the same value and that value is
3299 an invariant, then the PHI has the same value on all
3300 edges. Note this. */
3301 else if (!cant_insert && all_same)
3303 gcc_assert (edoubleprime->kind == CONSTANT
3304 || edoubleprime->kind == NAME);
3306 tree temp = make_temp_ssa_name (get_expr_type (expr),
3307 NULL, "pretmp");
3308 gassign *assign
3309 = gimple_build_assign (temp,
3310 edoubleprime->kind == CONSTANT ?
3311 PRE_EXPR_CONSTANT (edoubleprime) :
3312 PRE_EXPR_NAME (edoubleprime));
3313 gimple_stmt_iterator gsi = gsi_after_labels (block);
3314 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3316 VN_INFO (temp)->value_id = val;
3317 VN_INFO (temp)->valnum = vn_valnum_from_value_id (val);
3318 if (VN_INFO (temp)->valnum == NULL_TREE)
3319 VN_INFO (temp)->valnum = temp;
3320 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3321 pre_expr newe = get_or_alloc_expr_for_name (temp);
3322 add_to_value (val, newe);
3323 bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3324 bitmap_insert_into_set (NEW_SETS (block), newe);
3329 exprs.release ();
3330 return new_stuff;
3334 /* Perform insertion for partially anticipatable expressions. There
3335 is only one case we will perform insertion for these. This case is
3336 if the expression is partially anticipatable, and fully available.
3337 In this case, we know that putting it earlier will enable us to
3338 remove the later computation. */
3340 static bool
3341 do_pre_partial_partial_insertion (basic_block block, basic_block dom)
3343 bool new_stuff = false;
3344 vec<pre_expr> exprs;
3345 pre_expr expr;
3346 auto_vec<pre_expr> avail;
3347 int i;
3349 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3350 avail.safe_grow (EDGE_COUNT (block->preds));
3352 FOR_EACH_VEC_ELT (exprs, i, expr)
3354 if (expr->kind == NARY
3355 || expr->kind == REFERENCE)
3357 unsigned int val;
3358 bool by_all = true;
3359 bool cant_insert = false;
3360 edge pred;
3361 basic_block bprime;
3362 pre_expr eprime = NULL;
3363 edge_iterator ei;
3365 val = get_expr_value_id (expr);
3366 if (bitmap_set_contains_value (PHI_GEN (block), val))
3367 continue;
3368 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3369 continue;
3371 FOR_EACH_EDGE (pred, ei, block->preds)
3373 unsigned int vprime;
3374 pre_expr edoubleprime;
3376 /* We should never run insertion for the exit block
3377 and so not come across fake pred edges. */
3378 gcc_assert (!(pred->flags & EDGE_FAKE));
3379 bprime = pred->src;
3380 eprime = phi_translate (NULL, expr, ANTIC_IN (block),
3381 PA_IN (block), pred);
3383 /* eprime will generally only be NULL if the
3384 value of the expression, translated
3385 through the PHI for this predecessor, is
3386 undefined. If that is the case, we can't
3387 make the expression fully redundant,
3388 because its value is undefined along a
3389 predecessor path. We can thus break out
3390 early because it doesn't matter what the
3391 rest of the results are. */
3392 if (eprime == NULL)
3394 avail[pred->dest_idx] = NULL;
3395 cant_insert = true;
3396 break;
3399 vprime = get_expr_value_id (eprime);
3400 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3401 avail[pred->dest_idx] = edoubleprime;
3402 if (edoubleprime == NULL)
3404 by_all = false;
3405 break;
3409 /* If we can insert it, it's not the same value
3410 already existing along every predecessor, and
3411 it's defined by some predecessor, it is
3412 partially redundant. */
3413 if (!cant_insert && by_all)
3415 edge succ;
3416 bool do_insertion = false;
3418 /* Insert only if we can remove a later expression on a path
3419 that we want to optimize for speed.
3420 The phi node that we will be inserting in BLOCK is not free,
3421 and inserting it for the sake of !optimize_for_speed successor
3422 may cause regressions on the speed path. */
3423 FOR_EACH_EDGE (succ, ei, block->succs)
3425 if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3426 || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3428 if (optimize_edge_for_speed_p (succ))
3429 do_insertion = true;
3433 if (!do_insertion)
3435 if (dump_file && (dump_flags & TDF_DETAILS))
3437 fprintf (dump_file, "Skipping partial partial redundancy "
3438 "for expression ");
3439 print_pre_expr (dump_file, expr);
3440 fprintf (dump_file, " (%04d), not (partially) anticipated "
3441 "on any to be optimized for speed edges\n", val);
3444 else if (dbg_cnt (treepre_insert))
3446 pre_stats.pa_insert++;
3447 if (dump_file && (dump_flags & TDF_DETAILS))
3449 fprintf (dump_file, "Found partial partial redundancy "
3450 "for expression ");
3451 print_pre_expr (dump_file, expr);
3452 fprintf (dump_file, " (%04d)\n",
3453 get_expr_value_id (expr));
3455 if (insert_into_preds_of_block (block,
3456 get_expression_id (expr),
3457 avail))
3458 new_stuff = true;
3464 exprs.release ();
3465 return new_stuff;
3468 /* Insert expressions in BLOCK to compute hoistable values up.
3469 Return TRUE if something was inserted, otherwise return FALSE.
3470 The caller has to make sure that BLOCK has at least two successors. */
3472 static bool
3473 do_hoist_insertion (basic_block block)
3475 edge e;
3476 edge_iterator ei;
3477 bool new_stuff = false;
3478 unsigned i;
3479 gimple_stmt_iterator last;
3481 /* At least two successors, or else... */
3482 gcc_assert (EDGE_COUNT (block->succs) >= 2);
3484 /* Check that all successors of BLOCK are dominated by block.
3485 We could use dominated_by_p() for this, but actually there is a much
3486 quicker check: any successor that is dominated by BLOCK can't have
3487 more than one predecessor edge. */
3488 FOR_EACH_EDGE (e, ei, block->succs)
3489 if (! single_pred_p (e->dest))
3490 return false;
3492 /* Determine the insertion point. If we cannot safely insert before
3493 the last stmt if we'd have to, bail out. */
3494 last = gsi_last_bb (block);
3495 if (!gsi_end_p (last)
3496 && !is_ctrl_stmt (gsi_stmt (last))
3497 && stmt_ends_bb_p (gsi_stmt (last)))
3498 return false;
3500 /* Compute the set of hoistable expressions from ANTIC_IN. First compute
3501 hoistable values. */
3502 bitmap_set hoistable_set;
3504 /* A hoistable value must be in ANTIC_IN(block)
3505 but not in AVAIL_OUT(BLOCK). */
3506 bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
3507 bitmap_and_compl (&hoistable_set.values,
3508 &ANTIC_IN (block)->values, &AVAIL_OUT (block)->values);
3510 /* Short-cut for a common case: hoistable_set is empty. */
3511 if (bitmap_empty_p (&hoistable_set.values))
3512 return false;
3514 /* Compute which of the hoistable values is in AVAIL_OUT of
3515 at least one of the successors of BLOCK. */
3516 bitmap_head availout_in_some;
3517 bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
3518 FOR_EACH_EDGE (e, ei, block->succs)
3519 /* Do not consider expressions solely because their availability
3520 on loop exits. They'd be ANTIC-IN throughout the whole loop
3521 and thus effectively hoisted across loops by combination of
3522 PRE and hoisting. */
3523 if (! loop_exit_edge_p (block->loop_father, e))
3524 bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
3525 &AVAIL_OUT (e->dest)->values);
3526 bitmap_clear (&hoistable_set.values);
3528 /* Short-cut for a common case: availout_in_some is empty. */
3529 if (bitmap_empty_p (&availout_in_some))
3530 return false;
3532 /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */
3533 hoistable_set.values = availout_in_some;
3534 hoistable_set.expressions = ANTIC_IN (block)->expressions;
3536 /* Now finally construct the topological-ordered expression set. */
3537 vec<pre_expr> exprs = sorted_array_from_bitmap_set (&hoistable_set);
3539 bitmap_clear (&hoistable_set.values);
3541 /* If there are candidate values for hoisting, insert expressions
3542 strategically to make the hoistable expressions fully redundant. */
3543 pre_expr expr;
3544 FOR_EACH_VEC_ELT (exprs, i, expr)
3546 /* While we try to sort expressions topologically above the
3547 sorting doesn't work out perfectly. Catch expressions we
3548 already inserted. */
3549 unsigned int value_id = get_expr_value_id (expr);
3550 if (bitmap_set_contains_value (AVAIL_OUT (block), value_id))
3552 if (dump_file && (dump_flags & TDF_DETAILS))
3554 fprintf (dump_file,
3555 "Already inserted expression for ");
3556 print_pre_expr (dump_file, expr);
3557 fprintf (dump_file, " (%04d)\n", value_id);
3559 continue;
3562 /* OK, we should hoist this value. Perform the transformation. */
3563 pre_stats.hoist_insert++;
3564 if (dump_file && (dump_flags & TDF_DETAILS))
3566 fprintf (dump_file,
3567 "Inserting expression in block %d for code hoisting: ",
3568 block->index);
3569 print_pre_expr (dump_file, expr);
3570 fprintf (dump_file, " (%04d)\n", value_id);
3573 gimple_seq stmts = NULL;
3574 tree res = create_expression_by_pieces (block, expr, &stmts,
3575 get_expr_type (expr));
3577 /* Do not return true if expression creation ultimately
3578 did not insert any statements. */
3579 if (gimple_seq_empty_p (stmts))
3580 res = NULL_TREE;
3581 else
3583 if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
3584 gsi_insert_seq_before (&last, stmts, GSI_SAME_STMT);
3585 else
3586 gsi_insert_seq_after (&last, stmts, GSI_NEW_STMT);
3589 /* Make sure to not return true if expression creation ultimately
3590 failed but also make sure to insert any stmts produced as they
3591 are tracked in inserted_exprs. */
3592 if (! res)
3593 continue;
3595 new_stuff = true;
3598 exprs.release ();
3600 return new_stuff;
3603 /* Do a dominator walk on the control flow graph, and insert computations
3604 of values as necessary for PRE and hoisting. */
3606 static bool
3607 insert_aux (basic_block block, bool do_pre, bool do_hoist)
3609 basic_block son;
3610 bool new_stuff = false;
3612 if (block)
3614 basic_block dom;
3615 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3616 if (dom)
3618 unsigned i;
3619 bitmap_iterator bi;
3620 bitmap_set_t newset;
3622 /* First, update the AVAIL_OUT set with anything we may have
3623 inserted higher up in the dominator tree. */
3624 newset = NEW_SETS (dom);
3625 if (newset)
3627 /* Note that we need to value_replace both NEW_SETS, and
3628 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3629 represented by some non-simple expression here that we want
3630 to replace it with. */
3631 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3633 pre_expr expr = expression_for_id (i);
3634 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3635 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3639 /* Insert expressions for partial redundancies. */
3640 if (do_pre && !single_pred_p (block))
3642 new_stuff |= do_pre_regular_insertion (block, dom);
3643 if (do_partial_partial)
3644 new_stuff |= do_pre_partial_partial_insertion (block, dom);
3647 /* Insert expressions for hoisting. */
3648 if (do_hoist && EDGE_COUNT (block->succs) >= 2)
3649 new_stuff |= do_hoist_insertion (block);
3652 for (son = first_dom_son (CDI_DOMINATORS, block);
3653 son;
3654 son = next_dom_son (CDI_DOMINATORS, son))
3656 new_stuff |= insert_aux (son, do_pre, do_hoist);
3659 return new_stuff;
3662 /* Perform insertion of partially redundant and hoistable values. */
3664 static void
3665 insert (void)
3667 bool new_stuff = true;
3668 basic_block bb;
3669 int num_iterations = 0;
3671 FOR_ALL_BB_FN (bb, cfun)
3672 NEW_SETS (bb) = bitmap_set_new ();
3674 while (new_stuff)
3676 num_iterations++;
3677 if (dump_file && dump_flags & TDF_DETAILS)
3678 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3679 new_stuff = insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun), flag_tree_pre,
3680 flag_code_hoisting);
3682 /* Clear the NEW sets before the next iteration. We have already
3683 fully propagated its contents. */
3684 if (new_stuff)
3685 FOR_ALL_BB_FN (bb, cfun)
3686 bitmap_set_free (NEW_SETS (bb));
3688 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3692 /* Compute the AVAIL set for all basic blocks.
3694 This function performs value numbering of the statements in each basic
3695 block. The AVAIL sets are built from information we glean while doing
3696 this value numbering, since the AVAIL sets contain only one entry per
3697 value.
3699 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3700 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3702 static void
3703 compute_avail (void)
3706 basic_block block, son;
3707 basic_block *worklist;
3708 size_t sp = 0;
3709 unsigned i;
3710 tree name;
3712 /* We pretend that default definitions are defined in the entry block.
3713 This includes function arguments and the static chain decl. */
3714 FOR_EACH_SSA_NAME (i, name, cfun)
3716 pre_expr e;
3717 if (!SSA_NAME_IS_DEFAULT_DEF (name)
3718 || has_zero_uses (name)
3719 || virtual_operand_p (name))
3720 continue;
3722 e = get_or_alloc_expr_for_name (name);
3723 add_to_value (get_expr_value_id (e), e);
3724 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), e);
3725 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3729 if (dump_file && (dump_flags & TDF_DETAILS))
3731 print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3732 "tmp_gen", ENTRY_BLOCK);
3733 print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3734 "avail_out", ENTRY_BLOCK);
3737 /* Allocate the worklist. */
3738 worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
3740 /* Seed the algorithm by putting the dominator children of the entry
3741 block on the worklist. */
3742 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun));
3743 son;
3744 son = next_dom_son (CDI_DOMINATORS, son))
3745 worklist[sp++] = son;
3747 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun))
3748 = ssa_default_def (cfun, gimple_vop (cfun));
3750 /* Loop until the worklist is empty. */
3751 while (sp)
3753 gimple *stmt;
3754 basic_block dom;
3756 /* Pick a block from the worklist. */
3757 block = worklist[--sp];
3758 vn_context_bb = block;
3760 /* Initially, the set of available values in BLOCK is that of
3761 its immediate dominator. */
3762 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3763 if (dom)
3765 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3766 BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
3769 /* Generate values for PHI nodes. */
3770 for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
3771 gsi_next (&gsi))
3773 tree result = gimple_phi_result (gsi.phi ());
3775 /* We have no need for virtual phis, as they don't represent
3776 actual computations. */
3777 if (virtual_operand_p (result))
3779 BB_LIVE_VOP_ON_EXIT (block) = result;
3780 continue;
3783 pre_expr e = get_or_alloc_expr_for_name (result);
3784 add_to_value (get_expr_value_id (e), e);
3785 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3786 bitmap_insert_into_set (PHI_GEN (block), e);
3789 BB_MAY_NOTRETURN (block) = 0;
3791 /* Now compute value numbers and populate value sets with all
3792 the expressions computed in BLOCK. */
3793 for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
3794 gsi_next (&gsi))
3796 ssa_op_iter iter;
3797 tree op;
3799 stmt = gsi_stmt (gsi);
3801 /* Cache whether the basic-block has any non-visible side-effect
3802 or control flow.
3803 If this isn't a call or it is the last stmt in the
3804 basic-block then the CFG represents things correctly. */
3805 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3807 /* Non-looping const functions always return normally.
3808 Otherwise the call might not return or have side-effects
3809 that forbids hoisting possibly trapping expressions
3810 before it. */
3811 int flags = gimple_call_flags (stmt);
3812 if (!(flags & ECF_CONST)
3813 || (flags & ECF_LOOPING_CONST_OR_PURE))
3814 BB_MAY_NOTRETURN (block) = 1;
3817 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3819 pre_expr e = get_or_alloc_expr_for_name (op);
3821 add_to_value (get_expr_value_id (e), e);
3822 bitmap_insert_into_set (TMP_GEN (block), e);
3823 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3826 if (gimple_vdef (stmt))
3827 BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
3829 if (gimple_has_side_effects (stmt)
3830 || stmt_could_throw_p (stmt)
3831 || is_gimple_debug (stmt))
3832 continue;
3834 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3836 if (ssa_undefined_value_p (op))
3837 continue;
3838 pre_expr e = get_or_alloc_expr_for_name (op);
3839 bitmap_value_insert_into_set (EXP_GEN (block), e);
3842 switch (gimple_code (stmt))
3844 case GIMPLE_RETURN:
3845 continue;
3847 case GIMPLE_CALL:
3849 vn_reference_t ref;
3850 vn_reference_s ref1;
3851 pre_expr result = NULL;
3853 /* We can value number only calls to real functions. */
3854 if (gimple_call_internal_p (stmt))
3855 continue;
3857 vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
3858 if (!ref)
3859 continue;
3861 /* If the value of the call is not invalidated in
3862 this block until it is computed, add the expression
3863 to EXP_GEN. */
3864 if (!gimple_vuse (stmt)
3865 || gimple_code
3866 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3867 || gimple_bb (SSA_NAME_DEF_STMT
3868 (gimple_vuse (stmt))) != block)
3870 result = pre_expr_pool.allocate ();
3871 result->kind = REFERENCE;
3872 result->id = 0;
3873 PRE_EXPR_REFERENCE (result) = ref;
3875 get_or_alloc_expression_id (result);
3876 add_to_value (get_expr_value_id (result), result);
3877 bitmap_value_insert_into_set (EXP_GEN (block), result);
3879 continue;
3882 case GIMPLE_ASSIGN:
3884 pre_expr result = NULL;
3885 switch (vn_get_stmt_kind (stmt))
3887 case VN_NARY:
3889 enum tree_code code = gimple_assign_rhs_code (stmt);
3890 vn_nary_op_t nary;
3892 /* COND_EXPR and VEC_COND_EXPR are awkward in
3893 that they contain an embedded complex expression.
3894 Don't even try to shove those through PRE. */
3895 if (code == COND_EXPR
3896 || code == VEC_COND_EXPR)
3897 continue;
3899 vn_nary_op_lookup_stmt (stmt, &nary);
3900 if (!nary || nary->predicated_values)
3901 continue;
3903 /* If the NARY traps and there was a preceding
3904 point in the block that might not return avoid
3905 adding the nary to EXP_GEN. */
3906 if (BB_MAY_NOTRETURN (block)
3907 && vn_nary_may_trap (nary))
3908 continue;
3910 result = pre_expr_pool.allocate ();
3911 result->kind = NARY;
3912 result->id = 0;
3913 PRE_EXPR_NARY (result) = nary;
3914 break;
3917 case VN_REFERENCE:
3919 tree rhs1 = gimple_assign_rhs1 (stmt);
3920 alias_set_type set = get_alias_set (rhs1);
3921 vec<vn_reference_op_s> operands
3922 = vn_reference_operands_for_lookup (rhs1);
3923 vn_reference_t ref;
3924 vn_reference_lookup_pieces (gimple_vuse (stmt), set,
3925 TREE_TYPE (rhs1),
3926 operands, &ref, VN_WALK);
3927 if (!ref)
3929 operands.release ();
3930 continue;
3933 /* If the value of the reference is not invalidated in
3934 this block until it is computed, add the expression
3935 to EXP_GEN. */
3936 if (gimple_vuse (stmt))
3938 gimple *def_stmt;
3939 bool ok = true;
3940 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
3941 while (!gimple_nop_p (def_stmt)
3942 && gimple_code (def_stmt) != GIMPLE_PHI
3943 && gimple_bb (def_stmt) == block)
3945 if (stmt_may_clobber_ref_p
3946 (def_stmt, gimple_assign_rhs1 (stmt)))
3948 ok = false;
3949 break;
3951 def_stmt
3952 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
3954 if (!ok)
3956 operands.release ();
3957 continue;
3961 /* If the load was value-numbered to another
3962 load make sure we do not use its expression
3963 for insertion if it wouldn't be a valid
3964 replacement. */
3965 /* At the momemt we have a testcase
3966 for hoist insertion of aligned vs. misaligned
3967 variants in gcc.dg/torture/pr65270-1.c thus
3968 with just alignment to be considered we can
3969 simply replace the expression in the hashtable
3970 with the most conservative one. */
3971 vn_reference_op_t ref1 = &ref->operands.last ();
3972 while (ref1->opcode != TARGET_MEM_REF
3973 && ref1->opcode != MEM_REF
3974 && ref1 != &ref->operands[0])
3975 --ref1;
3976 vn_reference_op_t ref2 = &operands.last ();
3977 while (ref2->opcode != TARGET_MEM_REF
3978 && ref2->opcode != MEM_REF
3979 && ref2 != &operands[0])
3980 --ref2;
3981 if ((ref1->opcode == TARGET_MEM_REF
3982 || ref1->opcode == MEM_REF)
3983 && (TYPE_ALIGN (ref1->type)
3984 > TYPE_ALIGN (ref2->type)))
3985 ref1->type
3986 = build_aligned_type (ref1->type,
3987 TYPE_ALIGN (ref2->type));
3988 /* TBAA behavior is an obvious part so make sure
3989 that the hashtable one covers this as well
3990 by adjusting the ref alias set and its base. */
3991 if (ref->set == set
3992 || alias_set_subset_of (set, ref->set))
3994 else if (alias_set_subset_of (ref->set, set))
3996 ref->set = set;
3997 if (ref1->opcode == MEM_REF)
3998 ref1->op0
3999 = wide_int_to_tree (TREE_TYPE (ref2->op0),
4000 wi::to_wide (ref1->op0));
4001 else
4002 ref1->op2
4003 = wide_int_to_tree (TREE_TYPE (ref2->op2),
4004 wi::to_wide (ref1->op2));
4006 else
4008 ref->set = 0;
4009 if (ref1->opcode == MEM_REF)
4010 ref1->op0
4011 = wide_int_to_tree (ptr_type_node,
4012 wi::to_wide (ref1->op0));
4013 else
4014 ref1->op2
4015 = wide_int_to_tree (ptr_type_node,
4016 wi::to_wide (ref1->op2));
4018 operands.release ();
4020 result = pre_expr_pool.allocate ();
4021 result->kind = REFERENCE;
4022 result->id = 0;
4023 PRE_EXPR_REFERENCE (result) = ref;
4024 break;
4027 default:
4028 continue;
4031 get_or_alloc_expression_id (result);
4032 add_to_value (get_expr_value_id (result), result);
4033 bitmap_value_insert_into_set (EXP_GEN (block), result);
4034 continue;
4036 default:
4037 break;
4041 if (dump_file && (dump_flags & TDF_DETAILS))
4043 print_bitmap_set (dump_file, EXP_GEN (block),
4044 "exp_gen", block->index);
4045 print_bitmap_set (dump_file, PHI_GEN (block),
4046 "phi_gen", block->index);
4047 print_bitmap_set (dump_file, TMP_GEN (block),
4048 "tmp_gen", block->index);
4049 print_bitmap_set (dump_file, AVAIL_OUT (block),
4050 "avail_out", block->index);
4053 /* Put the dominator children of BLOCK on the worklist of blocks
4054 to compute available sets for. */
4055 for (son = first_dom_son (CDI_DOMINATORS, block);
4056 son;
4057 son = next_dom_son (CDI_DOMINATORS, son))
4058 worklist[sp++] = son;
4060 vn_context_bb = NULL;
4062 free (worklist);
4066 /* Initialize data structures used by PRE. */
4068 static void
4069 init_pre (void)
4071 basic_block bb;
4073 next_expression_id = 1;
4074 expressions.create (0);
4075 expressions.safe_push (NULL);
4076 value_expressions.create (get_max_value_id () + 1);
4077 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
4078 name_to_id.create (0);
4080 inserted_exprs = BITMAP_ALLOC (NULL);
4082 connect_infinite_loops_to_exit ();
4083 memset (&pre_stats, 0, sizeof (pre_stats));
4085 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4087 calculate_dominance_info (CDI_DOMINATORS);
4089 bitmap_obstack_initialize (&grand_bitmap_obstack);
4090 phi_translate_table = new hash_table<expr_pred_trans_d> (5110);
4091 expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4092 FOR_ALL_BB_FN (bb, cfun)
4094 EXP_GEN (bb) = bitmap_set_new ();
4095 PHI_GEN (bb) = bitmap_set_new ();
4096 TMP_GEN (bb) = bitmap_set_new ();
4097 AVAIL_OUT (bb) = bitmap_set_new ();
4102 /* Deallocate data structures used by PRE. */
4104 static void
4105 fini_pre ()
4107 value_expressions.release ();
4108 expressions.release ();
4109 BITMAP_FREE (inserted_exprs);
4110 bitmap_obstack_release (&grand_bitmap_obstack);
4111 bitmap_set_pool.release ();
4112 pre_expr_pool.release ();
4113 delete phi_translate_table;
4114 phi_translate_table = NULL;
4115 delete expression_to_id;
4116 expression_to_id = NULL;
4117 name_to_id.release ();
4119 free_aux_for_blocks ();
4122 namespace {
4124 const pass_data pass_data_pre =
4126 GIMPLE_PASS, /* type */
4127 "pre", /* name */
4128 OPTGROUP_NONE, /* optinfo_flags */
4129 TV_TREE_PRE, /* tv_id */
4130 ( PROP_cfg | PROP_ssa ), /* properties_required */
4131 0, /* properties_provided */
4132 0, /* properties_destroyed */
4133 TODO_rebuild_alias, /* todo_flags_start */
4134 0, /* todo_flags_finish */
4137 class pass_pre : public gimple_opt_pass
4139 public:
4140 pass_pre (gcc::context *ctxt)
4141 : gimple_opt_pass (pass_data_pre, ctxt)
4144 /* opt_pass methods: */
4145 virtual bool gate (function *)
4146 { return flag_tree_pre != 0 || flag_code_hoisting != 0; }
4147 virtual unsigned int execute (function *);
4149 }; // class pass_pre
4151 /* Valueization hook for RPO VN when we are calling back to it
4152 at ANTIC compute time. */
4154 static tree
4155 pre_valueize (tree name)
4157 if (TREE_CODE (name) == SSA_NAME)
4159 tree tem = VN_INFO (name)->valnum;
4160 if (tem != VN_TOP && tem != name)
4162 if (TREE_CODE (tem) != SSA_NAME
4163 || SSA_NAME_IS_DEFAULT_DEF (tem))
4164 return tem;
4165 /* We create temporary SSA names for representatives that
4166 do not have a definition (yet) but are not default defs either
4167 assume they are fine to use. */
4168 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (tem));
4169 if (! def_bb
4170 || dominated_by_p (CDI_DOMINATORS, vn_context_bb, def_bb))
4171 return tem;
4172 /* ??? Now we could look for a leader. Ideally we'd somehow
4173 expose RPO VN leaders and get rid of AVAIL_OUT as well... */
4176 return name;
4179 unsigned int
4180 pass_pre::execute (function *fun)
4182 unsigned int todo = 0;
4184 do_partial_partial =
4185 flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4187 /* This has to happen before VN runs because
4188 loop_optimizer_init may create new phis, etc. */
4189 loop_optimizer_init (LOOPS_NORMAL);
4190 split_critical_edges ();
4191 scev_initialize ();
4193 run_rpo_vn (VN_WALK);
4195 init_pre ();
4197 vn_valueize = pre_valueize;
4199 /* Insert can get quite slow on an incredibly large number of basic
4200 blocks due to some quadratic behavior. Until this behavior is
4201 fixed, don't run it when he have an incredibly large number of
4202 bb's. If we aren't going to run insert, there is no point in
4203 computing ANTIC, either, even though it's plenty fast nor do
4204 we require AVAIL. */
4205 if (n_basic_blocks_for_fn (fun) < 4000)
4207 compute_avail ();
4208 compute_antic ();
4209 insert ();
4212 /* Make sure to remove fake edges before committing our inserts.
4213 This makes sure we don't end up with extra critical edges that
4214 we would need to split. */
4215 remove_fake_exit_edges ();
4216 gsi_commit_edge_inserts ();
4218 /* Eliminate folds statements which might (should not...) end up
4219 not keeping virtual operands up-to-date. */
4220 gcc_assert (!need_ssa_update_p (fun));
4222 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4223 statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4224 statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
4225 statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4227 todo |= eliminate_with_rpo_vn (inserted_exprs);
4229 vn_valueize = NULL;
4231 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4232 to insert PHI nodes sometimes, and because value numbering of casts isn't
4233 perfect, we sometimes end up inserting dead code. This simple DCE-like
4234 pass removes any insertions we made that weren't actually used. */
4235 simple_dce_from_worklist (inserted_exprs);
4237 fini_pre ();
4239 scev_finalize ();
4240 loop_optimizer_finalize ();
4242 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4243 case we can merge the block with the remaining predecessor of the block.
4244 It should either:
4245 - call merge_blocks after each tail merge iteration
4246 - call merge_blocks after all tail merge iterations
4247 - mark TODO_cleanup_cfg when necessary
4248 - share the cfg cleanup with fini_pre. */
4249 todo |= tail_merge_optimize (todo);
4251 free_rpo_vn ();
4253 /* Tail merging invalidates the virtual SSA web, together with
4254 cfg-cleanup opportunities exposed by PRE this will wreck the
4255 SSA updating machinery. So make sure to run update-ssa
4256 manually, before eventually scheduling cfg-cleanup as part of
4257 the todo. */
4258 update_ssa (TODO_update_ssa_only_virtuals);
4260 return todo;
4263 } // anon namespace
4265 gimple_opt_pass *
4266 make_pass_pre (gcc::context *ctxt)
4268 return new pass_pre (ctxt);