C++: fix-it hint for missing "typename" (PR c++/63392)
[official-gcc.git] / gcc / tree-ssa-pre.c
blob2dce88bd85e4d486d4f0dbb125d6a10e3cc7d050
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 id = PRE_EXPR_NARY (expr)->value_id;
667 break;
668 case REFERENCE:
669 id = PRE_EXPR_REFERENCE (expr)->value_id;
670 break;
671 default:
672 gcc_unreachable ();
674 /* ??? We cannot assert that expr has a value-id (it can be 0), because
675 we assign value-ids only to expressions that have a result
676 in set_hashtable_value_ids. */
677 return id;
680 /* Return a VN valnum (SSA name or constant) for the PRE value-id VAL. */
682 static tree
683 vn_valnum_from_value_id (unsigned int val)
685 bitmap_iterator bi;
686 unsigned int i;
687 bitmap exprset = value_expressions[val];
688 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
690 pre_expr vexpr = expression_for_id (i);
691 if (vexpr->kind == NAME)
692 return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
693 else if (vexpr->kind == CONSTANT)
694 return PRE_EXPR_CONSTANT (vexpr);
696 return NULL_TREE;
699 /* Insert an expression EXPR into a bitmapped set. */
701 static void
702 bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
704 unsigned int val = get_expr_value_id (expr);
705 if (! value_id_constant_p (val))
707 /* Note this is the only function causing multiple expressions
708 for the same value to appear in a set. This is needed for
709 TMP_GEN, PHI_GEN and NEW_SETs. */
710 bitmap_set_bit (&set->values, val);
711 bitmap_set_bit (&set->expressions, get_or_alloc_expression_id (expr));
715 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
717 static void
718 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
720 bitmap_copy (&dest->expressions, &orig->expressions);
721 bitmap_copy (&dest->values, &orig->values);
725 /* Free memory used up by SET. */
726 static void
727 bitmap_set_free (bitmap_set_t set)
729 bitmap_clear (&set->expressions);
730 bitmap_clear (&set->values);
734 /* Generate an topological-ordered array of bitmap set SET. */
736 static vec<pre_expr>
737 sorted_array_from_bitmap_set (bitmap_set_t set)
739 unsigned int i, j;
740 bitmap_iterator bi, bj;
741 vec<pre_expr> result;
743 /* Pre-allocate enough space for the array. */
744 result.create (bitmap_count_bits (&set->expressions));
746 FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
748 /* The number of expressions having a given value is usually
749 relatively small. Thus, rather than making a vector of all
750 the expressions and sorting it by value-id, we walk the values
751 and check in the reverse mapping that tells us what expressions
752 have a given value, to filter those in our set. As a result,
753 the expressions are inserted in value-id order, which means
754 topological order.
756 If this is somehow a significant lose for some cases, we can
757 choose which set to walk based on the set size. */
758 bitmap exprset = value_expressions[i];
759 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj)
761 if (bitmap_bit_p (&set->expressions, j))
762 result.quick_push (expression_for_id (j));
766 return result;
769 /* Subtract all expressions contained in ORIG from DEST. */
771 static bitmap_set_t
772 bitmap_set_subtract_expressions (bitmap_set_t dest, bitmap_set_t orig)
774 bitmap_set_t result = bitmap_set_new ();
775 bitmap_iterator bi;
776 unsigned int i;
778 bitmap_and_compl (&result->expressions, &dest->expressions,
779 &orig->expressions);
781 FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
783 pre_expr expr = expression_for_id (i);
784 unsigned int value_id = get_expr_value_id (expr);
785 bitmap_set_bit (&result->values, value_id);
788 return result;
791 /* Subtract all values in bitmap set B from bitmap set A. */
793 static void
794 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
796 unsigned int i;
797 bitmap_iterator bi;
798 unsigned to_remove = -1U;
799 bitmap_and_compl_into (&a->values, &b->values);
800 FOR_EACH_EXPR_ID_IN_SET (a, i, bi)
802 if (to_remove != -1U)
804 bitmap_clear_bit (&a->expressions, to_remove);
805 to_remove = -1U;
807 pre_expr expr = expression_for_id (i);
808 if (! bitmap_bit_p (&a->values, get_expr_value_id (expr)))
809 to_remove = i;
811 if (to_remove != -1U)
812 bitmap_clear_bit (&a->expressions, to_remove);
816 /* Return true if bitmapped set SET contains the value VALUE_ID. */
818 static bool
819 bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
821 if (value_id_constant_p (value_id))
822 return true;
824 return bitmap_bit_p (&set->values, value_id);
827 static inline bool
828 bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr)
830 return bitmap_bit_p (&set->expressions, get_expression_id (expr));
833 /* Return true if two bitmap sets are equal. */
835 static bool
836 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
838 return bitmap_equal_p (&a->values, &b->values);
841 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
842 and add it otherwise. */
844 static void
845 bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
847 unsigned int val = get_expr_value_id (expr);
848 if (value_id_constant_p (val))
849 return;
851 if (bitmap_set_contains_value (set, val))
853 /* The number of expressions having a given value is usually
854 significantly less than the total number of expressions in SET.
855 Thus, rather than check, for each expression in SET, whether it
856 has the value LOOKFOR, we walk the reverse mapping that tells us
857 what expressions have a given value, and see if any of those
858 expressions are in our set. For large testcases, this is about
859 5-10x faster than walking the bitmap. If this is somehow a
860 significant lose for some cases, we can choose which set to walk
861 based on the set size. */
862 unsigned int i;
863 bitmap_iterator bi;
864 bitmap exprset = value_expressions[val];
865 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
867 if (bitmap_clear_bit (&set->expressions, i))
869 bitmap_set_bit (&set->expressions, get_expression_id (expr));
870 return;
873 gcc_unreachable ();
875 else
876 bitmap_insert_into_set (set, expr);
879 /* Insert EXPR into SET if EXPR's value is not already present in
880 SET. */
882 static void
883 bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
885 unsigned int val = get_expr_value_id (expr);
887 gcc_checking_assert (expr->id == get_or_alloc_expression_id (expr));
889 /* Constant values are always considered to be part of the set. */
890 if (value_id_constant_p (val))
891 return;
893 /* If the value membership changed, add the expression. */
894 if (bitmap_set_bit (&set->values, val))
895 bitmap_set_bit (&set->expressions, expr->id);
898 /* Print out EXPR to outfile. */
900 static void
901 print_pre_expr (FILE *outfile, const pre_expr expr)
903 if (! expr)
905 fprintf (outfile, "NULL");
906 return;
908 switch (expr->kind)
910 case CONSTANT:
911 print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr));
912 break;
913 case NAME:
914 print_generic_expr (outfile, PRE_EXPR_NAME (expr));
915 break;
916 case NARY:
918 unsigned int i;
919 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
920 fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
921 for (i = 0; i < nary->length; i++)
923 print_generic_expr (outfile, nary->op[i]);
924 if (i != (unsigned) nary->length - 1)
925 fprintf (outfile, ",");
927 fprintf (outfile, "}");
929 break;
931 case REFERENCE:
933 vn_reference_op_t vro;
934 unsigned int i;
935 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
936 fprintf (outfile, "{");
937 for (i = 0;
938 ref->operands.iterate (i, &vro);
939 i++)
941 bool closebrace = false;
942 if (vro->opcode != SSA_NAME
943 && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
945 fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
946 if (vro->op0)
948 fprintf (outfile, "<");
949 closebrace = true;
952 if (vro->op0)
954 print_generic_expr (outfile, vro->op0);
955 if (vro->op1)
957 fprintf (outfile, ",");
958 print_generic_expr (outfile, vro->op1);
960 if (vro->op2)
962 fprintf (outfile, ",");
963 print_generic_expr (outfile, vro->op2);
966 if (closebrace)
967 fprintf (outfile, ">");
968 if (i != ref->operands.length () - 1)
969 fprintf (outfile, ",");
971 fprintf (outfile, "}");
972 if (ref->vuse)
974 fprintf (outfile, "@");
975 print_generic_expr (outfile, ref->vuse);
978 break;
981 void debug_pre_expr (pre_expr);
983 /* Like print_pre_expr but always prints to stderr. */
984 DEBUG_FUNCTION void
985 debug_pre_expr (pre_expr e)
987 print_pre_expr (stderr, e);
988 fprintf (stderr, "\n");
991 /* Print out SET to OUTFILE. */
993 static void
994 print_bitmap_set (FILE *outfile, bitmap_set_t set,
995 const char *setname, int blockindex)
997 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
998 if (set)
1000 bool first = true;
1001 unsigned i;
1002 bitmap_iterator bi;
1004 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1006 const pre_expr expr = expression_for_id (i);
1008 if (!first)
1009 fprintf (outfile, ", ");
1010 first = false;
1011 print_pre_expr (outfile, expr);
1013 fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1016 fprintf (outfile, " }\n");
1019 void debug_bitmap_set (bitmap_set_t);
1021 DEBUG_FUNCTION void
1022 debug_bitmap_set (bitmap_set_t set)
1024 print_bitmap_set (stderr, set, "debug", 0);
1027 void debug_bitmap_sets_for (basic_block);
1029 DEBUG_FUNCTION void
1030 debug_bitmap_sets_for (basic_block bb)
1032 print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1033 print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1034 print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1035 print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1036 print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1037 if (do_partial_partial)
1038 print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1039 print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1042 /* Print out the expressions that have VAL to OUTFILE. */
1044 static void
1045 print_value_expressions (FILE *outfile, unsigned int val)
1047 bitmap set = value_expressions[val];
1048 if (set)
1050 bitmap_set x;
1051 char s[10];
1052 sprintf (s, "%04d", val);
1053 x.expressions = *set;
1054 print_bitmap_set (outfile, &x, s, 0);
1059 DEBUG_FUNCTION void
1060 debug_value_expressions (unsigned int val)
1062 print_value_expressions (stderr, val);
1065 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1066 represent it. */
1068 static pre_expr
1069 get_or_alloc_expr_for_constant (tree constant)
1071 unsigned int result_id;
1072 unsigned int value_id;
1073 struct pre_expr_d expr;
1074 pre_expr newexpr;
1076 expr.kind = CONSTANT;
1077 PRE_EXPR_CONSTANT (&expr) = constant;
1078 result_id = lookup_expression_id (&expr);
1079 if (result_id != 0)
1080 return expression_for_id (result_id);
1082 newexpr = pre_expr_pool.allocate ();
1083 newexpr->kind = CONSTANT;
1084 PRE_EXPR_CONSTANT (newexpr) = constant;
1085 alloc_expression_id (newexpr);
1086 value_id = get_or_alloc_constant_value_id (constant);
1087 add_to_value (value_id, newexpr);
1088 return newexpr;
1091 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1092 Currently only supports constants and SSA_NAMES. */
1093 static pre_expr
1094 get_or_alloc_expr_for (tree t)
1096 if (TREE_CODE (t) == SSA_NAME)
1097 return get_or_alloc_expr_for_name (t);
1098 else if (is_gimple_min_invariant (t))
1099 return get_or_alloc_expr_for_constant (t);
1100 gcc_unreachable ();
1103 /* Return the folded version of T if T, when folded, is a gimple
1104 min_invariant or an SSA name. Otherwise, return T. */
1106 static pre_expr
1107 fully_constant_expression (pre_expr e)
1109 switch (e->kind)
1111 case CONSTANT:
1112 return e;
1113 case NARY:
1115 vn_nary_op_t nary = PRE_EXPR_NARY (e);
1116 tree res = vn_nary_simplify (nary);
1117 if (!res)
1118 return e;
1119 if (is_gimple_min_invariant (res))
1120 return get_or_alloc_expr_for_constant (res);
1121 if (TREE_CODE (res) == SSA_NAME)
1122 return get_or_alloc_expr_for_name (res);
1123 return e;
1125 case REFERENCE:
1127 vn_reference_t ref = PRE_EXPR_REFERENCE (e);
1128 tree folded;
1129 if ((folded = fully_constant_vn_reference_p (ref)))
1130 return get_or_alloc_expr_for_constant (folded);
1131 return e;
1133 default:
1134 return e;
1136 return e;
1139 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1140 it has the value it would have in BLOCK. Set *SAME_VALID to true
1141 in case the new vuse doesn't change the value id of the OPERANDS. */
1143 static tree
1144 translate_vuse_through_block (vec<vn_reference_op_s> operands,
1145 alias_set_type set, tree type, tree vuse,
1146 basic_block phiblock,
1147 basic_block block, bool *same_valid)
1149 gimple *phi = SSA_NAME_DEF_STMT (vuse);
1150 ao_ref ref;
1151 edge e = NULL;
1152 bool use_oracle;
1154 *same_valid = true;
1156 if (gimple_bb (phi) != phiblock)
1157 return vuse;
1159 use_oracle = ao_ref_init_from_vn_reference (&ref, set, type, operands);
1161 /* Use the alias-oracle to find either the PHI node in this block,
1162 the first VUSE used in this block that is equivalent to vuse or
1163 the first VUSE which definition in this block kills the value. */
1164 if (gimple_code (phi) == GIMPLE_PHI)
1165 e = find_edge (block, phiblock);
1166 else if (use_oracle)
1167 while (!stmt_may_clobber_ref_p_1 (phi, &ref))
1169 vuse = gimple_vuse (phi);
1170 phi = SSA_NAME_DEF_STMT (vuse);
1171 if (gimple_bb (phi) != phiblock)
1172 return vuse;
1173 if (gimple_code (phi) == GIMPLE_PHI)
1175 e = find_edge (block, phiblock);
1176 break;
1179 else
1180 return NULL_TREE;
1182 if (e)
1184 if (use_oracle)
1186 bitmap visited = NULL;
1187 unsigned int cnt;
1188 /* Try to find a vuse that dominates this phi node by skipping
1189 non-clobbering statements. */
1190 vuse = get_continuation_for_phi (phi, &ref, &cnt, &visited, false,
1191 NULL, NULL);
1192 if (visited)
1193 BITMAP_FREE (visited);
1195 else
1196 vuse = NULL_TREE;
1197 if (!vuse)
1199 /* If we didn't find any, the value ID can't stay the same,
1200 but return the translated vuse. */
1201 *same_valid = false;
1202 vuse = PHI_ARG_DEF (phi, e->dest_idx);
1204 /* ??? We would like to return vuse here as this is the canonical
1205 upmost vdef that this reference is associated with. But during
1206 insertion of the references into the hash tables we only ever
1207 directly insert with their direct gimple_vuse, hence returning
1208 something else would make us not find the other expression. */
1209 return PHI_ARG_DEF (phi, e->dest_idx);
1212 return NULL_TREE;
1215 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1216 SET2 *or* SET3. This is used to avoid making a set consisting of the union
1217 of PA_IN and ANTIC_IN during insert and phi-translation. */
1219 static inline pre_expr
1220 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
1221 bitmap_set_t set3 = NULL)
1223 pre_expr result = NULL;
1225 if (set1)
1226 result = bitmap_find_leader (set1, val);
1227 if (!result && set2)
1228 result = bitmap_find_leader (set2, val);
1229 if (!result && set3)
1230 result = bitmap_find_leader (set3, val);
1231 return result;
1234 /* Get the tree type for our PRE expression e. */
1236 static tree
1237 get_expr_type (const pre_expr e)
1239 switch (e->kind)
1241 case NAME:
1242 return TREE_TYPE (PRE_EXPR_NAME (e));
1243 case CONSTANT:
1244 return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1245 case REFERENCE:
1246 return PRE_EXPR_REFERENCE (e)->type;
1247 case NARY:
1248 return PRE_EXPR_NARY (e)->type;
1250 gcc_unreachable ();
1253 /* Get a representative SSA_NAME for a given expression that is available in B.
1254 Since all of our sub-expressions are treated as values, we require
1255 them to be SSA_NAME's for simplicity.
1256 Prior versions of GVNPRE used to use "value handles" here, so that
1257 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1258 either case, the operands are really values (IE we do not expect
1259 them to be usable without finding leaders). */
1261 static tree
1262 get_representative_for (const pre_expr e, basic_block b = NULL)
1264 tree name, valnum = NULL_TREE;
1265 unsigned int value_id = get_expr_value_id (e);
1267 switch (e->kind)
1269 case NAME:
1270 return VN_INFO (PRE_EXPR_NAME (e))->valnum;
1271 case CONSTANT:
1272 return PRE_EXPR_CONSTANT (e);
1273 case NARY:
1274 case REFERENCE:
1276 /* Go through all of the expressions representing this value
1277 and pick out an SSA_NAME. */
1278 unsigned int i;
1279 bitmap_iterator bi;
1280 bitmap exprs = value_expressions[value_id];
1281 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1283 pre_expr rep = expression_for_id (i);
1284 if (rep->kind == NAME)
1286 tree name = PRE_EXPR_NAME (rep);
1287 valnum = VN_INFO (name)->valnum;
1288 gimple *def = SSA_NAME_DEF_STMT (name);
1289 /* We have to return either a new representative or one
1290 that can be used for expression simplification and thus
1291 is available in B. */
1292 if (! b
1293 || gimple_nop_p (def)
1294 || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
1295 return name;
1297 else if (rep->kind == CONSTANT)
1298 return PRE_EXPR_CONSTANT (rep);
1301 break;
1304 /* If we reached here we couldn't find an SSA_NAME. This can
1305 happen when we've discovered a value that has never appeared in
1306 the program as set to an SSA_NAME, as the result of phi translation.
1307 Create one here.
1308 ??? We should be able to re-use this when we insert the statement
1309 to compute it. */
1310 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1311 VN_INFO (name)->value_id = value_id;
1312 VN_INFO (name)->valnum = valnum ? valnum : name;
1313 /* ??? For now mark this SSA name for release by VN. */
1314 VN_INFO (name)->needs_insertion = true;
1315 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1316 if (dump_file && (dump_flags & TDF_DETAILS))
1318 fprintf (dump_file, "Created SSA_NAME representative ");
1319 print_generic_expr (dump_file, name);
1320 fprintf (dump_file, " for expression:");
1321 print_pre_expr (dump_file, e);
1322 fprintf (dump_file, " (%04d)\n", value_id);
1325 return name;
1329 static pre_expr
1330 phi_translate (bitmap_set_t, pre_expr, bitmap_set_t, bitmap_set_t, edge);
1332 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1333 the phis in PRED. Return NULL if we can't find a leader for each part
1334 of the translated expression. */
1336 static pre_expr
1337 phi_translate_1 (bitmap_set_t dest,
1338 pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
1340 basic_block pred = e->src;
1341 basic_block phiblock = e->dest;
1342 switch (expr->kind)
1344 case NARY:
1346 unsigned int i;
1347 bool changed = false;
1348 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1349 vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1350 sizeof_vn_nary_op (nary->length));
1351 memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1353 for (i = 0; i < newnary->length; i++)
1355 if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1356 continue;
1357 else
1359 pre_expr leader, result;
1360 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1361 leader = find_leader_in_sets (op_val_id, set1, set2);
1362 result = phi_translate (dest, leader, set1, set2, e);
1363 if (result && result != leader)
1364 /* If op has a leader in the sets we translate make
1365 sure to use the value of the translated expression.
1366 We might need a new representative for that. */
1367 newnary->op[i] = get_representative_for (result, pred);
1368 else if (!result)
1369 return NULL;
1371 changed |= newnary->op[i] != nary->op[i];
1374 if (changed)
1376 pre_expr constant;
1377 unsigned int new_val_id;
1379 PRE_EXPR_NARY (expr) = newnary;
1380 constant = fully_constant_expression (expr);
1381 PRE_EXPR_NARY (expr) = nary;
1382 if (constant != expr)
1384 /* For non-CONSTANTs we have to make sure we can eventually
1385 insert the expression. Which means we need to have a
1386 leader for it. */
1387 if (constant->kind != CONSTANT)
1389 /* Do not allow simplifications to non-constants over
1390 backedges as this will likely result in a loop PHI node
1391 to be inserted and increased register pressure.
1392 See PR77498 - this avoids doing predcoms work in
1393 a less efficient way. */
1394 if (e->flags & EDGE_DFS_BACK)
1396 else
1398 unsigned value_id = get_expr_value_id (constant);
1399 /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1400 dest has what we computed into ANTIC_OUT sofar
1401 so pick from that - since topological sorting
1402 by sorted_array_from_bitmap_set isn't perfect
1403 we may lose some cases here. */
1404 constant = find_leader_in_sets (value_id, dest,
1405 AVAIL_OUT (pred));
1406 if (constant)
1408 if (dump_file && (dump_flags & TDF_DETAILS))
1410 fprintf (dump_file, "simplifying ");
1411 print_pre_expr (dump_file, expr);
1412 fprintf (dump_file, " translated %d -> %d to ",
1413 phiblock->index, pred->index);
1414 PRE_EXPR_NARY (expr) = newnary;
1415 print_pre_expr (dump_file, expr);
1416 PRE_EXPR_NARY (expr) = nary;
1417 fprintf (dump_file, " to ");
1418 print_pre_expr (dump_file, constant);
1419 fprintf (dump_file, "\n");
1421 return constant;
1425 else
1426 return constant;
1429 /* vn_nary_* do not valueize operands. */
1430 for (i = 0; i < newnary->length; ++i)
1431 if (TREE_CODE (newnary->op[i]) == SSA_NAME)
1432 newnary->op[i] = VN_INFO (newnary->op[i])->valnum;
1433 tree result = vn_nary_op_lookup_pieces (newnary->length,
1434 newnary->opcode,
1435 newnary->type,
1436 &newnary->op[0],
1437 &nary);
1438 if (result && is_gimple_min_invariant (result))
1439 return get_or_alloc_expr_for_constant (result);
1441 expr = pre_expr_pool.allocate ();
1442 expr->kind = NARY;
1443 expr->id = 0;
1444 if (nary && !nary->predicated_values)
1446 PRE_EXPR_NARY (expr) = nary;
1447 new_val_id = nary->value_id;
1448 get_or_alloc_expression_id (expr);
1450 else
1452 new_val_id = get_next_value_id ();
1453 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
1454 nary = vn_nary_op_insert_pieces (newnary->length,
1455 newnary->opcode,
1456 newnary->type,
1457 &newnary->op[0],
1458 result, new_val_id);
1459 PRE_EXPR_NARY (expr) = nary;
1460 get_or_alloc_expression_id (expr);
1462 add_to_value (new_val_id, expr);
1464 return expr;
1466 break;
1468 case REFERENCE:
1470 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1471 vec<vn_reference_op_s> operands = ref->operands;
1472 tree vuse = ref->vuse;
1473 tree newvuse = vuse;
1474 vec<vn_reference_op_s> newoperands = vNULL;
1475 bool changed = false, same_valid = true;
1476 unsigned int i, n;
1477 vn_reference_op_t operand;
1478 vn_reference_t newref;
1480 for (i = 0; operands.iterate (i, &operand); i++)
1482 pre_expr opresult;
1483 pre_expr leader;
1484 tree op[3];
1485 tree type = operand->type;
1486 vn_reference_op_s newop = *operand;
1487 op[0] = operand->op0;
1488 op[1] = operand->op1;
1489 op[2] = operand->op2;
1490 for (n = 0; n < 3; ++n)
1492 unsigned int op_val_id;
1493 if (!op[n])
1494 continue;
1495 if (TREE_CODE (op[n]) != SSA_NAME)
1497 /* We can't possibly insert these. */
1498 if (n != 0
1499 && !is_gimple_min_invariant (op[n]))
1500 break;
1501 continue;
1503 op_val_id = VN_INFO (op[n])->value_id;
1504 leader = find_leader_in_sets (op_val_id, set1, set2);
1505 opresult = phi_translate (dest, leader, set1, set2, e);
1506 if (opresult && opresult != leader)
1508 tree name = get_representative_for (opresult);
1509 changed |= name != op[n];
1510 op[n] = name;
1512 else if (!opresult)
1513 break;
1515 if (n != 3)
1517 newoperands.release ();
1518 return NULL;
1520 if (!changed)
1521 continue;
1522 if (!newoperands.exists ())
1523 newoperands = operands.copy ();
1524 /* We may have changed from an SSA_NAME to a constant */
1525 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1526 newop.opcode = TREE_CODE (op[0]);
1527 newop.type = type;
1528 newop.op0 = op[0];
1529 newop.op1 = op[1];
1530 newop.op2 = op[2];
1531 newoperands[i] = newop;
1533 gcc_checking_assert (i == operands.length ());
1535 if (vuse)
1537 newvuse = translate_vuse_through_block (newoperands.exists ()
1538 ? newoperands : operands,
1539 ref->set, ref->type,
1540 vuse, phiblock, pred,
1541 &same_valid);
1542 if (newvuse == NULL_TREE)
1544 newoperands.release ();
1545 return NULL;
1549 if (changed || newvuse != vuse)
1551 unsigned int new_val_id;
1553 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1554 ref->type,
1555 newoperands.exists ()
1556 ? newoperands : operands,
1557 &newref, VN_WALK);
1558 if (result)
1559 newoperands.release ();
1561 /* We can always insert constants, so if we have a partial
1562 redundant constant load of another type try to translate it
1563 to a constant of appropriate type. */
1564 if (result && is_gimple_min_invariant (result))
1566 tree tem = result;
1567 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1569 tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1570 if (tem && !is_gimple_min_invariant (tem))
1571 tem = NULL_TREE;
1573 if (tem)
1574 return get_or_alloc_expr_for_constant (tem);
1577 /* If we'd have to convert things we would need to validate
1578 if we can insert the translated expression. So fail
1579 here for now - we cannot insert an alias with a different
1580 type in the VN tables either, as that would assert. */
1581 if (result
1582 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1583 return NULL;
1584 else if (!result && newref
1585 && !useless_type_conversion_p (ref->type, newref->type))
1587 newoperands.release ();
1588 return NULL;
1591 expr = pre_expr_pool.allocate ();
1592 expr->kind = REFERENCE;
1593 expr->id = 0;
1595 if (newref)
1596 new_val_id = newref->value_id;
1597 else
1599 if (changed || !same_valid)
1601 new_val_id = get_next_value_id ();
1602 value_expressions.safe_grow_cleared
1603 (get_max_value_id () + 1);
1605 else
1606 new_val_id = ref->value_id;
1607 if (!newoperands.exists ())
1608 newoperands = operands.copy ();
1609 newref = vn_reference_insert_pieces (newvuse, ref->set,
1610 ref->type,
1611 newoperands,
1612 result, new_val_id);
1613 newoperands = vNULL;
1615 PRE_EXPR_REFERENCE (expr) = newref;
1616 get_or_alloc_expression_id (expr);
1617 add_to_value (new_val_id, expr);
1619 newoperands.release ();
1620 return expr;
1622 break;
1624 case NAME:
1626 tree name = PRE_EXPR_NAME (expr);
1627 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1628 /* If the SSA name is defined by a PHI node in this block,
1629 translate it. */
1630 if (gimple_code (def_stmt) == GIMPLE_PHI
1631 && gimple_bb (def_stmt) == phiblock)
1633 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1635 /* Handle constant. */
1636 if (is_gimple_min_invariant (def))
1637 return get_or_alloc_expr_for_constant (def);
1639 return get_or_alloc_expr_for_name (def);
1641 /* Otherwise return it unchanged - it will get removed if its
1642 value is not available in PREDs AVAIL_OUT set of expressions
1643 by the subtraction of TMP_GEN. */
1644 return expr;
1647 default:
1648 gcc_unreachable ();
1652 /* Wrapper around phi_translate_1 providing caching functionality. */
1654 static pre_expr
1655 phi_translate (bitmap_set_t dest, pre_expr expr,
1656 bitmap_set_t set1, bitmap_set_t set2, edge e)
1658 expr_pred_trans_t slot = NULL;
1659 pre_expr phitrans;
1661 if (!expr)
1662 return NULL;
1664 /* Constants contain no values that need translation. */
1665 if (expr->kind == CONSTANT)
1666 return expr;
1668 if (value_id_constant_p (get_expr_value_id (expr)))
1669 return expr;
1671 /* Don't add translations of NAMEs as those are cheap to translate. */
1672 if (expr->kind != NAME)
1674 if (phi_trans_add (&slot, expr, e->src))
1675 return slot->v;
1676 /* Store NULL for the value we want to return in the case of
1677 recursing. */
1678 slot->v = NULL;
1681 /* Translate. */
1682 basic_block saved_valueize_bb = vn_context_bb;
1683 vn_context_bb = e->src;
1684 phitrans = phi_translate_1 (dest, expr, set1, set2, e);
1685 vn_context_bb = saved_valueize_bb;
1687 if (slot)
1689 if (phitrans)
1690 slot->v = phitrans;
1691 else
1692 /* Remove failed translations again, they cause insert
1693 iteration to not pick up new opportunities reliably. */
1694 phi_translate_table->remove_elt_with_hash (slot, slot->hashcode);
1697 return phitrans;
1701 /* For each expression in SET, translate the values through phi nodes
1702 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1703 expressions in DEST. */
1705 static void
1706 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
1708 vec<pre_expr> exprs;
1709 pre_expr expr;
1710 int i;
1712 if (gimple_seq_empty_p (phi_nodes (e->dest)))
1714 bitmap_set_copy (dest, set);
1715 return;
1718 exprs = sorted_array_from_bitmap_set (set);
1719 FOR_EACH_VEC_ELT (exprs, i, expr)
1721 pre_expr translated;
1722 translated = phi_translate (dest, expr, set, NULL, e);
1723 if (!translated)
1724 continue;
1726 bitmap_insert_into_set (dest, translated);
1728 exprs.release ();
1731 /* Find the leader for a value (i.e., the name representing that
1732 value) in a given set, and return it. Return NULL if no leader
1733 is found. */
1735 static pre_expr
1736 bitmap_find_leader (bitmap_set_t set, unsigned int val)
1738 if (value_id_constant_p (val))
1740 unsigned int i;
1741 bitmap_iterator bi;
1742 bitmap exprset = value_expressions[val];
1744 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1746 pre_expr expr = expression_for_id (i);
1747 if (expr->kind == CONSTANT)
1748 return expr;
1751 if (bitmap_set_contains_value (set, val))
1753 /* Rather than walk the entire bitmap of expressions, and see
1754 whether any of them has the value we are looking for, we look
1755 at the reverse mapping, which tells us the set of expressions
1756 that have a given value (IE value->expressions with that
1757 value) and see if any of those expressions are in our set.
1758 The number of expressions per value is usually significantly
1759 less than the number of expressions in the set. In fact, for
1760 large testcases, doing it this way is roughly 5-10x faster
1761 than walking the bitmap.
1762 If this is somehow a significant lose for some cases, we can
1763 choose which set to walk based on which set is smaller. */
1764 unsigned int i;
1765 bitmap_iterator bi;
1766 bitmap exprset = value_expressions[val];
1768 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1769 return expression_for_id (i);
1771 return NULL;
1774 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1775 BLOCK by seeing if it is not killed in the block. Note that we are
1776 only determining whether there is a store that kills it. Because
1777 of the order in which clean iterates over values, we are guaranteed
1778 that altered operands will have caused us to be eliminated from the
1779 ANTIC_IN set already. */
1781 static bool
1782 value_dies_in_block_x (pre_expr expr, basic_block block)
1784 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1785 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1786 gimple *def;
1787 gimple_stmt_iterator gsi;
1788 unsigned id = get_expression_id (expr);
1789 bool res = false;
1790 ao_ref ref;
1792 if (!vuse)
1793 return false;
1795 /* Lookup a previously calculated result. */
1796 if (EXPR_DIES (block)
1797 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1798 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1800 /* A memory expression {e, VUSE} dies in the block if there is a
1801 statement that may clobber e. If, starting statement walk from the
1802 top of the basic block, a statement uses VUSE there can be no kill
1803 inbetween that use and the original statement that loaded {e, VUSE},
1804 so we can stop walking. */
1805 ref.base = NULL_TREE;
1806 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1808 tree def_vuse, def_vdef;
1809 def = gsi_stmt (gsi);
1810 def_vuse = gimple_vuse (def);
1811 def_vdef = gimple_vdef (def);
1813 /* Not a memory statement. */
1814 if (!def_vuse)
1815 continue;
1817 /* Not a may-def. */
1818 if (!def_vdef)
1820 /* A load with the same VUSE, we're done. */
1821 if (def_vuse == vuse)
1822 break;
1824 continue;
1827 /* Init ref only if we really need it. */
1828 if (ref.base == NULL_TREE
1829 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type,
1830 refx->operands))
1832 res = true;
1833 break;
1835 /* If the statement may clobber expr, it dies. */
1836 if (stmt_may_clobber_ref_p_1 (def, &ref))
1838 res = true;
1839 break;
1843 /* Remember the result. */
1844 if (!EXPR_DIES (block))
1845 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1846 bitmap_set_bit (EXPR_DIES (block), id * 2);
1847 if (res)
1848 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1850 return res;
1854 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1855 contains its value-id. */
1857 static bool
1858 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1860 if (op && TREE_CODE (op) == SSA_NAME)
1862 unsigned int value_id = VN_INFO (op)->value_id;
1863 if (!(bitmap_set_contains_value (set1, value_id)
1864 || (set2 && bitmap_set_contains_value (set2, value_id))))
1865 return false;
1867 return true;
1870 /* Determine if the expression EXPR is valid in SET1 U SET2.
1871 ONLY SET2 CAN BE NULL.
1872 This means that we have a leader for each part of the expression
1873 (if it consists of values), or the expression is an SSA_NAME.
1874 For loads/calls, we also see if the vuse is killed in this block. */
1876 static bool
1877 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
1879 switch (expr->kind)
1881 case NAME:
1882 /* By construction all NAMEs are available. Non-available
1883 NAMEs are removed by subtracting TMP_GEN from the sets. */
1884 return true;
1885 case NARY:
1887 unsigned int i;
1888 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1889 for (i = 0; i < nary->length; i++)
1890 if (!op_valid_in_sets (set1, set2, nary->op[i]))
1891 return false;
1892 return true;
1894 break;
1895 case REFERENCE:
1897 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1898 vn_reference_op_t vro;
1899 unsigned int i;
1901 FOR_EACH_VEC_ELT (ref->operands, i, vro)
1903 if (!op_valid_in_sets (set1, set2, vro->op0)
1904 || !op_valid_in_sets (set1, set2, vro->op1)
1905 || !op_valid_in_sets (set1, set2, vro->op2))
1906 return false;
1908 return true;
1910 default:
1911 gcc_unreachable ();
1915 /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
1916 This means expressions that are made up of values we have no leaders for
1917 in SET1 or SET2. */
1919 static void
1920 clean (bitmap_set_t set1, bitmap_set_t set2 = NULL)
1922 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
1923 pre_expr expr;
1924 int i;
1926 FOR_EACH_VEC_ELT (exprs, i, expr)
1928 if (!valid_in_sets (set1, set2, expr))
1930 unsigned int val = get_expr_value_id (expr);
1931 bitmap_clear_bit (&set1->expressions, get_expression_id (expr));
1932 /* We are entered with possibly multiple expressions for a value
1933 so before removing a value from the set see if there's an
1934 expression for it left. */
1935 if (! bitmap_find_leader (set1, val))
1936 bitmap_clear_bit (&set1->values, val);
1939 exprs.release ();
1942 /* Clean the set of expressions that are no longer valid in SET because
1943 they are clobbered in BLOCK or because they trap and may not be executed. */
1945 static void
1946 prune_clobbered_mems (bitmap_set_t set, basic_block block)
1948 bitmap_iterator bi;
1949 unsigned i;
1950 unsigned to_remove = -1U;
1951 bool any_removed = false;
1953 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1955 /* Remove queued expr. */
1956 if (to_remove != -1U)
1958 bitmap_clear_bit (&set->expressions, to_remove);
1959 any_removed = true;
1960 to_remove = -1U;
1963 pre_expr expr = expression_for_id (i);
1964 if (expr->kind == REFERENCE)
1966 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1967 if (ref->vuse)
1969 gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
1970 if (!gimple_nop_p (def_stmt)
1971 && ((gimple_bb (def_stmt) != block
1972 && !dominated_by_p (CDI_DOMINATORS,
1973 block, gimple_bb (def_stmt)))
1974 || (gimple_bb (def_stmt) == block
1975 && value_dies_in_block_x (expr, block))))
1976 to_remove = i;
1979 else if (expr->kind == NARY)
1981 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1982 /* If the NARY may trap make sure the block does not contain
1983 a possible exit point.
1984 ??? This is overly conservative if we translate AVAIL_OUT
1985 as the available expression might be after the exit point. */
1986 if (BB_MAY_NOTRETURN (block)
1987 && vn_nary_may_trap (nary))
1988 to_remove = i;
1992 /* Remove queued expr. */
1993 if (to_remove != -1U)
1995 bitmap_clear_bit (&set->expressions, to_remove);
1996 any_removed = true;
1999 /* Above we only removed expressions, now clean the set of values
2000 which no longer have any corresponding expression. We cannot
2001 clear the value at the time we remove an expression since there
2002 may be multiple expressions per value.
2003 If we'd queue possibly to be removed values we could use
2004 the bitmap_find_leader way to see if there's still an expression
2005 for it. For some ratio of to be removed values and number of
2006 values/expressions in the set this might be faster than rebuilding
2007 the value-set. */
2008 if (any_removed)
2010 bitmap_clear (&set->values);
2011 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2013 pre_expr expr = expression_for_id (i);
2014 unsigned int value_id = get_expr_value_id (expr);
2015 bitmap_set_bit (&set->values, value_id);
2020 static sbitmap has_abnormal_preds;
2022 /* Compute the ANTIC set for BLOCK.
2024 If succs(BLOCK) > 1 then
2025 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2026 else if succs(BLOCK) == 1 then
2027 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2029 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2031 Note that clean() is deferred until after the iteration. */
2033 static bool
2034 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2036 bitmap_set_t S, old, ANTIC_OUT;
2037 edge e;
2038 edge_iterator ei;
2040 bool was_visited = BB_VISITED (block);
2041 bool changed = ! BB_VISITED (block);
2042 BB_VISITED (block) = 1;
2043 old = ANTIC_OUT = S = NULL;
2045 /* If any edges from predecessors are abnormal, antic_in is empty,
2046 so do nothing. */
2047 if (block_has_abnormal_pred_edge)
2048 goto maybe_dump_sets;
2050 old = ANTIC_IN (block);
2051 ANTIC_OUT = bitmap_set_new ();
2053 /* If the block has no successors, ANTIC_OUT is empty. */
2054 if (EDGE_COUNT (block->succs) == 0)
2056 /* If we have one successor, we could have some phi nodes to
2057 translate through. */
2058 else if (single_succ_p (block))
2060 e = single_succ_edge (block);
2061 gcc_assert (BB_VISITED (e->dest));
2062 phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
2064 /* If we have multiple successors, we take the intersection of all of
2065 them. Note that in the case of loop exit phi nodes, we may have
2066 phis to translate through. */
2067 else
2069 size_t i;
2070 edge first = NULL;
2072 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2073 FOR_EACH_EDGE (e, ei, block->succs)
2075 if (!first
2076 && BB_VISITED (e->dest))
2077 first = e;
2078 else if (BB_VISITED (e->dest))
2079 worklist.quick_push (e);
2080 else
2082 /* Unvisited successors get their ANTIC_IN replaced by the
2083 maximal set to arrive at a maximum ANTIC_IN solution.
2084 We can ignore them in the intersection operation and thus
2085 need not explicitely represent that maximum solution. */
2086 if (dump_file && (dump_flags & TDF_DETAILS))
2087 fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n",
2088 e->src->index, e->dest->index);
2092 /* Of multiple successors we have to have visited one already
2093 which is guaranteed by iteration order. */
2094 gcc_assert (first != NULL);
2096 phi_translate_set (ANTIC_OUT, ANTIC_IN (first->dest), first);
2098 /* If we have multiple successors we need to intersect the ANTIC_OUT
2099 sets. For values that's a simple intersection but for
2100 expressions it is a union. Given we want to have a single
2101 expression per value in our sets we have to canonicalize.
2102 Avoid randomness and running into cycles like for PR82129 and
2103 canonicalize the expression we choose to the one with the
2104 lowest id. This requires we actually compute the union first. */
2105 FOR_EACH_VEC_ELT (worklist, i, e)
2107 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2109 bitmap_set_t tmp = bitmap_set_new ();
2110 phi_translate_set (tmp, ANTIC_IN (e->dest), e);
2111 bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
2112 bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
2113 bitmap_set_free (tmp);
2115 else
2117 bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
2118 bitmap_ior_into (&ANTIC_OUT->expressions,
2119 &ANTIC_IN (e->dest)->expressions);
2122 if (! worklist.is_empty ())
2124 /* Prune expressions not in the value set. */
2125 bitmap_iterator bi;
2126 unsigned int i;
2127 unsigned int to_clear = -1U;
2128 FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
2130 if (to_clear != -1U)
2132 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2133 to_clear = -1U;
2135 pre_expr expr = expression_for_id (i);
2136 unsigned int value_id = get_expr_value_id (expr);
2137 if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
2138 to_clear = i;
2140 if (to_clear != -1U)
2141 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2145 /* Prune expressions that are clobbered in block and thus become
2146 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2147 prune_clobbered_mems (ANTIC_OUT, block);
2149 /* Generate ANTIC_OUT - TMP_GEN. */
2150 S = bitmap_set_subtract_expressions (ANTIC_OUT, TMP_GEN (block));
2152 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2153 ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
2154 TMP_GEN (block));
2156 /* Then union in the ANTIC_OUT - TMP_GEN values,
2157 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2158 bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
2159 bitmap_ior_into (&ANTIC_IN (block)->expressions, &S->expressions);
2161 /* clean (ANTIC_IN (block)) is defered to after the iteration converged
2162 because it can cause non-convergence, see for example PR81181. */
2164 /* Intersect ANTIC_IN with the old ANTIC_IN. This is required until
2165 we properly represent the maximum expression set, thus not prune
2166 values without expressions during the iteration. */
2167 if (was_visited
2168 && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
2170 if (dump_file && (dump_flags & TDF_DETAILS))
2171 fprintf (dump_file, "warning: intersecting with old ANTIC_IN "
2172 "shrinks the set\n");
2173 /* Prune expressions not in the value set. */
2174 bitmap_iterator bi;
2175 unsigned int i;
2176 unsigned int to_clear = -1U;
2177 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
2179 if (to_clear != -1U)
2181 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2182 to_clear = -1U;
2184 pre_expr expr = expression_for_id (i);
2185 unsigned int value_id = get_expr_value_id (expr);
2186 if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
2187 to_clear = i;
2189 if (to_clear != -1U)
2190 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2193 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2194 changed = true;
2196 maybe_dump_sets:
2197 if (dump_file && (dump_flags & TDF_DETAILS))
2199 if (ANTIC_OUT)
2200 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2202 if (changed)
2203 fprintf (dump_file, "[changed] ");
2204 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2205 block->index);
2207 if (S)
2208 print_bitmap_set (dump_file, S, "S", block->index);
2210 if (old)
2211 bitmap_set_free (old);
2212 if (S)
2213 bitmap_set_free (S);
2214 if (ANTIC_OUT)
2215 bitmap_set_free (ANTIC_OUT);
2216 return changed;
2219 /* Compute PARTIAL_ANTIC for BLOCK.
2221 If succs(BLOCK) > 1 then
2222 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2223 in ANTIC_OUT for all succ(BLOCK)
2224 else if succs(BLOCK) == 1 then
2225 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2227 PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
2230 static void
2231 compute_partial_antic_aux (basic_block block,
2232 bool block_has_abnormal_pred_edge)
2234 bitmap_set_t old_PA_IN;
2235 bitmap_set_t PA_OUT;
2236 edge e;
2237 edge_iterator ei;
2238 unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH);
2240 old_PA_IN = PA_OUT = NULL;
2242 /* If any edges from predecessors are abnormal, antic_in is empty,
2243 so do nothing. */
2244 if (block_has_abnormal_pred_edge)
2245 goto maybe_dump_sets;
2247 /* If there are too many partially anticipatable values in the
2248 block, phi_translate_set can take an exponential time: stop
2249 before the translation starts. */
2250 if (max_pa
2251 && single_succ_p (block)
2252 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2253 goto maybe_dump_sets;
2255 old_PA_IN = PA_IN (block);
2256 PA_OUT = bitmap_set_new ();
2258 /* If the block has no successors, ANTIC_OUT is empty. */
2259 if (EDGE_COUNT (block->succs) == 0)
2261 /* If we have one successor, we could have some phi nodes to
2262 translate through. Note that we can't phi translate across DFS
2263 back edges in partial antic, because it uses a union operation on
2264 the successors. For recurrences like IV's, we will end up
2265 generating a new value in the set on each go around (i + 3 (VH.1)
2266 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2267 else if (single_succ_p (block))
2269 e = single_succ_edge (block);
2270 if (!(e->flags & EDGE_DFS_BACK))
2271 phi_translate_set (PA_OUT, PA_IN (e->dest), e);
2273 /* If we have multiple successors, we take the union of all of
2274 them. */
2275 else
2277 size_t i;
2279 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2280 FOR_EACH_EDGE (e, ei, block->succs)
2282 if (e->flags & EDGE_DFS_BACK)
2283 continue;
2284 worklist.quick_push (e);
2286 if (worklist.length () > 0)
2288 FOR_EACH_VEC_ELT (worklist, i, e)
2290 unsigned int i;
2291 bitmap_iterator bi;
2293 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
2294 bitmap_value_insert_into_set (PA_OUT,
2295 expression_for_id (i));
2296 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2298 bitmap_set_t pa_in = bitmap_set_new ();
2299 phi_translate_set (pa_in, PA_IN (e->dest), e);
2300 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2301 bitmap_value_insert_into_set (PA_OUT,
2302 expression_for_id (i));
2303 bitmap_set_free (pa_in);
2305 else
2306 FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
2307 bitmap_value_insert_into_set (PA_OUT,
2308 expression_for_id (i));
2313 /* Prune expressions that are clobbered in block and thus become
2314 invalid if translated from PA_OUT to PA_IN. */
2315 prune_clobbered_mems (PA_OUT, block);
2317 /* PA_IN starts with PA_OUT - TMP_GEN.
2318 Then we subtract things from ANTIC_IN. */
2319 PA_IN (block) = bitmap_set_subtract_expressions (PA_OUT, TMP_GEN (block));
2321 /* For partial antic, we want to put back in the phi results, since
2322 we will properly avoid making them partially antic over backedges. */
2323 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2324 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2326 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2327 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2329 clean (PA_IN (block), ANTIC_IN (block));
2331 maybe_dump_sets:
2332 if (dump_file && (dump_flags & TDF_DETAILS))
2334 if (PA_OUT)
2335 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2337 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2339 if (old_PA_IN)
2340 bitmap_set_free (old_PA_IN);
2341 if (PA_OUT)
2342 bitmap_set_free (PA_OUT);
2345 /* Compute ANTIC and partial ANTIC sets. */
2347 static void
2348 compute_antic (void)
2350 bool changed = true;
2351 int num_iterations = 0;
2352 basic_block block;
2353 int i;
2354 edge_iterator ei;
2355 edge e;
2357 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2358 We pre-build the map of blocks with incoming abnormal edges here. */
2359 has_abnormal_preds = sbitmap_alloc (last_basic_block_for_fn (cfun));
2360 bitmap_clear (has_abnormal_preds);
2362 FOR_ALL_BB_FN (block, cfun)
2364 BB_VISITED (block) = 0;
2366 FOR_EACH_EDGE (e, ei, block->preds)
2367 if (e->flags & EDGE_ABNORMAL)
2369 bitmap_set_bit (has_abnormal_preds, block->index);
2370 break;
2373 /* While we are here, give empty ANTIC_IN sets to each block. */
2374 ANTIC_IN (block) = bitmap_set_new ();
2375 if (do_partial_partial)
2376 PA_IN (block) = bitmap_set_new ();
2379 /* At the exit block we anticipate nothing. */
2380 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2382 /* For ANTIC computation we need a postorder that also guarantees that
2383 a block with a single successor is visited after its successor.
2384 RPO on the inverted CFG has this property. */
2385 auto_vec<int, 20> postorder;
2386 inverted_post_order_compute (&postorder);
2388 auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
2389 bitmap_clear (worklist);
2390 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2391 bitmap_set_bit (worklist, e->src->index);
2392 while (changed)
2394 if (dump_file && (dump_flags & TDF_DETAILS))
2395 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2396 /* ??? We need to clear our PHI translation cache here as the
2397 ANTIC sets shrink and we restrict valid translations to
2398 those having operands with leaders in ANTIC. Same below
2399 for PA ANTIC computation. */
2400 num_iterations++;
2401 changed = false;
2402 for (i = postorder.length () - 1; i >= 0; i--)
2404 if (bitmap_bit_p (worklist, postorder[i]))
2406 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2407 bitmap_clear_bit (worklist, block->index);
2408 if (compute_antic_aux (block,
2409 bitmap_bit_p (has_abnormal_preds,
2410 block->index)))
2412 FOR_EACH_EDGE (e, ei, block->preds)
2413 bitmap_set_bit (worklist, e->src->index);
2414 changed = true;
2418 /* Theoretically possible, but *highly* unlikely. */
2419 gcc_checking_assert (num_iterations < 500);
2422 /* We have to clean after the dataflow problem converged as cleaning
2423 can cause non-convergence because it is based on expressions
2424 rather than values. */
2425 FOR_EACH_BB_FN (block, cfun)
2426 clean (ANTIC_IN (block));
2428 statistics_histogram_event (cfun, "compute_antic iterations",
2429 num_iterations);
2431 if (do_partial_partial)
2433 /* For partial antic we ignore backedges and thus we do not need
2434 to perform any iteration when we process blocks in postorder. */
2435 for (i = postorder.length () - 1; i >= 0; i--)
2437 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2438 compute_partial_antic_aux (block,
2439 bitmap_bit_p (has_abnormal_preds,
2440 block->index));
2444 sbitmap_free (has_abnormal_preds);
2448 /* Inserted expressions are placed onto this worklist, which is used
2449 for performing quick dead code elimination of insertions we made
2450 that didn't turn out to be necessary. */
2451 static bitmap inserted_exprs;
2453 /* The actual worker for create_component_ref_by_pieces. */
2455 static tree
2456 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2457 unsigned int *operand, gimple_seq *stmts)
2459 vn_reference_op_t currop = &ref->operands[*operand];
2460 tree genop;
2461 ++*operand;
2462 switch (currop->opcode)
2464 case CALL_EXPR:
2465 gcc_unreachable ();
2467 case MEM_REF:
2469 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2470 stmts);
2471 if (!baseop)
2472 return NULL_TREE;
2473 tree offset = currop->op0;
2474 if (TREE_CODE (baseop) == ADDR_EXPR
2475 && handled_component_p (TREE_OPERAND (baseop, 0)))
2477 poly_int64 off;
2478 tree base;
2479 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2480 &off);
2481 gcc_assert (base);
2482 offset = int_const_binop (PLUS_EXPR, offset,
2483 build_int_cst (TREE_TYPE (offset),
2484 off));
2485 baseop = build_fold_addr_expr (base);
2487 genop = build2 (MEM_REF, currop->type, baseop, offset);
2488 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2489 MR_DEPENDENCE_BASE (genop) = currop->base;
2490 REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
2491 return genop;
2494 case TARGET_MEM_REF:
2496 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2497 vn_reference_op_t nextop = &ref->operands[++*operand];
2498 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2499 stmts);
2500 if (!baseop)
2501 return NULL_TREE;
2502 if (currop->op0)
2504 genop0 = find_or_generate_expression (block, currop->op0, stmts);
2505 if (!genop0)
2506 return NULL_TREE;
2508 if (nextop->op0)
2510 genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2511 if (!genop1)
2512 return NULL_TREE;
2514 genop = build5 (TARGET_MEM_REF, currop->type,
2515 baseop, currop->op2, genop0, currop->op1, genop1);
2517 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2518 MR_DEPENDENCE_BASE (genop) = currop->base;
2519 return genop;
2522 case ADDR_EXPR:
2523 if (currop->op0)
2525 gcc_assert (is_gimple_min_invariant (currop->op0));
2526 return currop->op0;
2528 /* Fallthrough. */
2529 case REALPART_EXPR:
2530 case IMAGPART_EXPR:
2531 case VIEW_CONVERT_EXPR:
2533 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2534 stmts);
2535 if (!genop0)
2536 return NULL_TREE;
2537 return fold_build1 (currop->opcode, currop->type, genop0);
2540 case WITH_SIZE_EXPR:
2542 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2543 stmts);
2544 if (!genop0)
2545 return NULL_TREE;
2546 tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2547 if (!genop1)
2548 return NULL_TREE;
2549 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2552 case BIT_FIELD_REF:
2554 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2555 stmts);
2556 if (!genop0)
2557 return NULL_TREE;
2558 tree op1 = currop->op0;
2559 tree op2 = currop->op1;
2560 tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2561 REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
2562 return fold (t);
2565 /* For array ref vn_reference_op's, operand 1 of the array ref
2566 is op0 of the reference op and operand 3 of the array ref is
2567 op1. */
2568 case ARRAY_RANGE_REF:
2569 case ARRAY_REF:
2571 tree genop0;
2572 tree genop1 = currop->op0;
2573 tree genop2 = currop->op1;
2574 tree genop3 = currop->op2;
2575 genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2576 stmts);
2577 if (!genop0)
2578 return NULL_TREE;
2579 genop1 = find_or_generate_expression (block, genop1, stmts);
2580 if (!genop1)
2581 return NULL_TREE;
2582 if (genop2)
2584 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2585 /* Drop zero minimum index if redundant. */
2586 if (integer_zerop (genop2)
2587 && (!domain_type
2588 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2589 genop2 = NULL_TREE;
2590 else
2592 genop2 = find_or_generate_expression (block, genop2, stmts);
2593 if (!genop2)
2594 return NULL_TREE;
2597 if (genop3)
2599 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2600 /* We can't always put a size in units of the element alignment
2601 here as the element alignment may be not visible. See
2602 PR43783. Simply drop the element size for constant
2603 sizes. */
2604 if (TREE_CODE (genop3) == INTEGER_CST
2605 && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
2606 && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
2607 (wi::to_offset (genop3)
2608 * vn_ref_op_align_unit (currop))))
2609 genop3 = NULL_TREE;
2610 else
2612 genop3 = find_or_generate_expression (block, genop3, stmts);
2613 if (!genop3)
2614 return NULL_TREE;
2617 return build4 (currop->opcode, currop->type, genop0, genop1,
2618 genop2, genop3);
2620 case COMPONENT_REF:
2622 tree op0;
2623 tree op1;
2624 tree genop2 = currop->op1;
2625 op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2626 if (!op0)
2627 return NULL_TREE;
2628 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2629 op1 = currop->op0;
2630 if (genop2)
2632 genop2 = find_or_generate_expression (block, genop2, stmts);
2633 if (!genop2)
2634 return NULL_TREE;
2636 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2639 case SSA_NAME:
2641 genop = find_or_generate_expression (block, currop->op0, stmts);
2642 return genop;
2644 case STRING_CST:
2645 case INTEGER_CST:
2646 case COMPLEX_CST:
2647 case VECTOR_CST:
2648 case REAL_CST:
2649 case CONSTRUCTOR:
2650 case VAR_DECL:
2651 case PARM_DECL:
2652 case CONST_DECL:
2653 case RESULT_DECL:
2654 case FUNCTION_DECL:
2655 return currop->op0;
2657 default:
2658 gcc_unreachable ();
2662 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2663 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2664 trying to rename aggregates into ssa form directly, which is a no no.
2666 Thus, this routine doesn't create temporaries, it just builds a
2667 single access expression for the array, calling
2668 find_or_generate_expression to build the innermost pieces.
2670 This function is a subroutine of create_expression_by_pieces, and
2671 should not be called on it's own unless you really know what you
2672 are doing. */
2674 static tree
2675 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2676 gimple_seq *stmts)
2678 unsigned int op = 0;
2679 return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2682 /* Find a simple leader for an expression, or generate one using
2683 create_expression_by_pieces from a NARY expression for the value.
2684 BLOCK is the basic_block we are looking for leaders in.
2685 OP is the tree expression to find a leader for or generate.
2686 Returns the leader or NULL_TREE on failure. */
2688 static tree
2689 find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2691 pre_expr expr = get_or_alloc_expr_for (op);
2692 unsigned int lookfor = get_expr_value_id (expr);
2693 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2694 if (leader)
2696 if (leader->kind == NAME)
2697 return PRE_EXPR_NAME (leader);
2698 else if (leader->kind == CONSTANT)
2699 return PRE_EXPR_CONSTANT (leader);
2701 /* Defer. */
2702 return NULL_TREE;
2705 /* It must be a complex expression, so generate it recursively. Note
2706 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2707 where the insert algorithm fails to insert a required expression. */
2708 bitmap exprset = value_expressions[lookfor];
2709 bitmap_iterator bi;
2710 unsigned int i;
2711 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2713 pre_expr temp = expression_for_id (i);
2714 /* We cannot insert random REFERENCE expressions at arbitrary
2715 places. We can insert NARYs which eventually re-materializes
2716 its operand values. */
2717 if (temp->kind == NARY)
2718 return create_expression_by_pieces (block, temp, stmts,
2719 get_expr_type (expr));
2722 /* Defer. */
2723 return NULL_TREE;
2726 /* Create an expression in pieces, so that we can handle very complex
2727 expressions that may be ANTIC, but not necessary GIMPLE.
2728 BLOCK is the basic block the expression will be inserted into,
2729 EXPR is the expression to insert (in value form)
2730 STMTS is a statement list to append the necessary insertions into.
2732 This function will die if we hit some value that shouldn't be
2733 ANTIC but is (IE there is no leader for it, or its components).
2734 The function returns NULL_TREE in case a different antic expression
2735 has to be inserted first.
2736 This function may also generate expressions that are themselves
2737 partially or fully redundant. Those that are will be either made
2738 fully redundant during the next iteration of insert (for partially
2739 redundant ones), or eliminated by eliminate (for fully redundant
2740 ones). */
2742 static tree
2743 create_expression_by_pieces (basic_block block, pre_expr expr,
2744 gimple_seq *stmts, tree type)
2746 tree name;
2747 tree folded;
2748 gimple_seq forced_stmts = NULL;
2749 unsigned int value_id;
2750 gimple_stmt_iterator gsi;
2751 tree exprtype = type ? type : get_expr_type (expr);
2752 pre_expr nameexpr;
2753 gassign *newstmt;
2755 switch (expr->kind)
2757 /* We may hit the NAME/CONSTANT case if we have to convert types
2758 that value numbering saw through. */
2759 case NAME:
2760 folded = PRE_EXPR_NAME (expr);
2761 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
2762 return NULL_TREE;
2763 if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2764 return folded;
2765 break;
2766 case CONSTANT:
2768 folded = PRE_EXPR_CONSTANT (expr);
2769 tree tem = fold_convert (exprtype, folded);
2770 if (is_gimple_min_invariant (tem))
2771 return tem;
2772 break;
2774 case REFERENCE:
2775 if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
2777 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2778 unsigned int operand = 1;
2779 vn_reference_op_t currop = &ref->operands[0];
2780 tree sc = NULL_TREE;
2781 tree fn = find_or_generate_expression (block, currop->op0, stmts);
2782 if (!fn)
2783 return NULL_TREE;
2784 if (currop->op1)
2786 sc = find_or_generate_expression (block, currop->op1, stmts);
2787 if (!sc)
2788 return NULL_TREE;
2790 auto_vec<tree> args (ref->operands.length () - 1);
2791 while (operand < ref->operands.length ())
2793 tree arg = create_component_ref_by_pieces_1 (block, ref,
2794 &operand, stmts);
2795 if (!arg)
2796 return NULL_TREE;
2797 args.quick_push (arg);
2799 gcall *call = gimple_build_call_vec (fn, args);
2800 if (sc)
2801 gimple_call_set_chain (call, sc);
2802 tree forcedname = make_ssa_name (currop->type);
2803 gimple_call_set_lhs (call, forcedname);
2804 /* There's no CCP pass after PRE which would re-compute alignment
2805 information so make sure we re-materialize this here. */
2806 if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED)
2807 && args.length () - 2 <= 1
2808 && tree_fits_uhwi_p (args[1])
2809 && (args.length () != 3 || tree_fits_uhwi_p (args[2])))
2811 unsigned HOST_WIDE_INT halign = tree_to_uhwi (args[1]);
2812 unsigned HOST_WIDE_INT hmisalign
2813 = args.length () == 3 ? tree_to_uhwi (args[2]) : 0;
2814 if ((halign & (halign - 1)) == 0
2815 && (hmisalign & ~(halign - 1)) == 0)
2816 set_ptr_info_alignment (get_ptr_info (forcedname),
2817 halign, hmisalign);
2819 gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
2820 gimple_seq_add_stmt_without_update (&forced_stmts, call);
2821 folded = forcedname;
2823 else
2825 folded = create_component_ref_by_pieces (block,
2826 PRE_EXPR_REFERENCE (expr),
2827 stmts);
2828 if (!folded)
2829 return NULL_TREE;
2830 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2831 newstmt = gimple_build_assign (name, folded);
2832 gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2833 gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
2834 folded = name;
2836 break;
2837 case NARY:
2839 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2840 tree *genop = XALLOCAVEC (tree, nary->length);
2841 unsigned i;
2842 for (i = 0; i < nary->length; ++i)
2844 genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2845 if (!genop[i])
2846 return NULL_TREE;
2847 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2848 may have conversions stripped. */
2849 if (nary->opcode == POINTER_PLUS_EXPR)
2851 if (i == 0)
2852 genop[i] = gimple_convert (&forced_stmts,
2853 nary->type, genop[i]);
2854 else if (i == 1)
2855 genop[i] = gimple_convert (&forced_stmts,
2856 sizetype, genop[i]);
2858 else
2859 genop[i] = gimple_convert (&forced_stmts,
2860 TREE_TYPE (nary->op[i]), genop[i]);
2862 if (nary->opcode == CONSTRUCTOR)
2864 vec<constructor_elt, va_gc> *elts = NULL;
2865 for (i = 0; i < nary->length; ++i)
2866 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2867 folded = build_constructor (nary->type, elts);
2868 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2869 newstmt = gimple_build_assign (name, folded);
2870 gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2871 folded = name;
2873 else
2875 switch (nary->length)
2877 case 1:
2878 folded = gimple_build (&forced_stmts, nary->opcode, nary->type,
2879 genop[0]);
2880 break;
2881 case 2:
2882 folded = gimple_build (&forced_stmts, nary->opcode, nary->type,
2883 genop[0], genop[1]);
2884 break;
2885 case 3:
2886 folded = gimple_build (&forced_stmts, nary->opcode, nary->type,
2887 genop[0], genop[1], genop[2]);
2888 break;
2889 default:
2890 gcc_unreachable ();
2894 break;
2895 default:
2896 gcc_unreachable ();
2899 folded = gimple_convert (&forced_stmts, exprtype, folded);
2901 /* If there is nothing to insert, return the simplified result. */
2902 if (gimple_seq_empty_p (forced_stmts))
2903 return folded;
2904 /* If we simplified to a constant return it and discard eventually
2905 built stmts. */
2906 if (is_gimple_min_invariant (folded))
2908 gimple_seq_discard (forced_stmts);
2909 return folded;
2911 /* Likewise if we simplified to sth not queued for insertion. */
2912 bool found = false;
2913 gsi = gsi_last (forced_stmts);
2914 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2916 gimple *stmt = gsi_stmt (gsi);
2917 tree forcedname = gimple_get_lhs (stmt);
2918 if (forcedname == folded)
2920 found = true;
2921 break;
2924 if (! found)
2926 gimple_seq_discard (forced_stmts);
2927 return folded;
2929 gcc_assert (TREE_CODE (folded) == SSA_NAME);
2931 /* If we have any intermediate expressions to the value sets, add them
2932 to the value sets and chain them in the instruction stream. */
2933 if (forced_stmts)
2935 gsi = gsi_start (forced_stmts);
2936 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2938 gimple *stmt = gsi_stmt (gsi);
2939 tree forcedname = gimple_get_lhs (stmt);
2940 pre_expr nameexpr;
2942 if (forcedname != folded)
2944 VN_INFO (forcedname)->valnum = forcedname;
2945 VN_INFO (forcedname)->value_id = get_next_value_id ();
2946 nameexpr = get_or_alloc_expr_for_name (forcedname);
2947 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
2948 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2949 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2952 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
2954 gimple_seq_add_seq (stmts, forced_stmts);
2957 name = folded;
2959 /* Fold the last statement. */
2960 gsi = gsi_last (*stmts);
2961 if (fold_stmt_inplace (&gsi))
2962 update_stmt (gsi_stmt (gsi));
2964 /* Add a value number to the temporary.
2965 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2966 we are creating the expression by pieces, and this particular piece of
2967 the expression may have been represented. There is no harm in replacing
2968 here. */
2969 value_id = get_expr_value_id (expr);
2970 VN_INFO (name)->value_id = value_id;
2971 VN_INFO (name)->valnum = vn_valnum_from_value_id (value_id);
2972 if (VN_INFO (name)->valnum == NULL_TREE)
2973 VN_INFO (name)->valnum = name;
2974 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
2975 nameexpr = get_or_alloc_expr_for_name (name);
2976 add_to_value (value_id, nameexpr);
2977 if (NEW_SETS (block))
2978 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2979 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2981 pre_stats.insertions++;
2982 if (dump_file && (dump_flags & TDF_DETAILS))
2984 fprintf (dump_file, "Inserted ");
2985 print_gimple_stmt (dump_file, gsi_stmt (gsi_last (*stmts)), 0);
2986 fprintf (dump_file, " in predecessor %d (%04d)\n",
2987 block->index, value_id);
2990 return name;
2994 /* Insert the to-be-made-available values of expression EXPRNUM for each
2995 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2996 merge the result with a phi node, given the same value number as
2997 NODE. Return true if we have inserted new stuff. */
2999 static bool
3000 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3001 vec<pre_expr> avail)
3003 pre_expr expr = expression_for_id (exprnum);
3004 pre_expr newphi;
3005 unsigned int val = get_expr_value_id (expr);
3006 edge pred;
3007 bool insertions = false;
3008 bool nophi = false;
3009 basic_block bprime;
3010 pre_expr eprime;
3011 edge_iterator ei;
3012 tree type = get_expr_type (expr);
3013 tree temp;
3014 gphi *phi;
3016 /* Make sure we aren't creating an induction variable. */
3017 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3019 bool firstinsideloop = false;
3020 bool secondinsideloop = false;
3021 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3022 EDGE_PRED (block, 0)->src);
3023 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3024 EDGE_PRED (block, 1)->src);
3025 /* Induction variables only have one edge inside the loop. */
3026 if ((firstinsideloop ^ secondinsideloop)
3027 && expr->kind != REFERENCE)
3029 if (dump_file && (dump_flags & TDF_DETAILS))
3030 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3031 nophi = true;
3035 /* Make the necessary insertions. */
3036 FOR_EACH_EDGE (pred, ei, block->preds)
3038 gimple_seq stmts = NULL;
3039 tree builtexpr;
3040 bprime = pred->src;
3041 eprime = avail[pred->dest_idx];
3042 builtexpr = create_expression_by_pieces (bprime, eprime,
3043 &stmts, type);
3044 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3045 if (!gimple_seq_empty_p (stmts))
3047 basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
3048 gcc_assert (! new_bb);
3049 insertions = true;
3051 if (!builtexpr)
3053 /* We cannot insert a PHI node if we failed to insert
3054 on one edge. */
3055 nophi = true;
3056 continue;
3058 if (is_gimple_min_invariant (builtexpr))
3059 avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
3060 else
3061 avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3063 /* If we didn't want a phi node, and we made insertions, we still have
3064 inserted new stuff, and thus return true. If we didn't want a phi node,
3065 and didn't make insertions, we haven't added anything new, so return
3066 false. */
3067 if (nophi && insertions)
3068 return true;
3069 else if (nophi && !insertions)
3070 return false;
3072 /* Now build a phi for the new variable. */
3073 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3074 phi = create_phi_node (temp, block);
3076 VN_INFO (temp)->value_id = val;
3077 VN_INFO (temp)->valnum = vn_valnum_from_value_id (val);
3078 if (VN_INFO (temp)->valnum == NULL_TREE)
3079 VN_INFO (temp)->valnum = temp;
3080 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3081 FOR_EACH_EDGE (pred, ei, block->preds)
3083 pre_expr ae = avail[pred->dest_idx];
3084 gcc_assert (get_expr_type (ae) == type
3085 || useless_type_conversion_p (type, get_expr_type (ae)));
3086 if (ae->kind == CONSTANT)
3087 add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3088 pred, UNKNOWN_LOCATION);
3089 else
3090 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3093 newphi = get_or_alloc_expr_for_name (temp);
3094 add_to_value (val, newphi);
3096 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3097 this insertion, since we test for the existence of this value in PHI_GEN
3098 before proceeding with the partial redundancy checks in insert_aux.
3100 The value may exist in AVAIL_OUT, in particular, it could be represented
3101 by the expression we are trying to eliminate, in which case we want the
3102 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3103 inserted there.
3105 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3106 this block, because if it did, it would have existed in our dominator's
3107 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3110 bitmap_insert_into_set (PHI_GEN (block), newphi);
3111 bitmap_value_replace_in_set (AVAIL_OUT (block),
3112 newphi);
3113 bitmap_insert_into_set (NEW_SETS (block),
3114 newphi);
3116 /* If we insert a PHI node for a conversion of another PHI node
3117 in the same basic-block try to preserve range information.
3118 This is important so that followup loop passes receive optimal
3119 number of iteration analysis results. See PR61743. */
3120 if (expr->kind == NARY
3121 && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3122 && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3123 && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3124 && INTEGRAL_TYPE_P (type)
3125 && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3126 && (TYPE_PRECISION (type)
3127 >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3128 && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3130 wide_int min, max;
3131 if (get_range_info (expr->u.nary->op[0], &min, &max) == VR_RANGE
3132 && !wi::neg_p (min, SIGNED)
3133 && !wi::neg_p (max, SIGNED))
3134 /* Just handle extension and sign-changes of all-positive ranges. */
3135 set_range_info (temp,
3136 SSA_NAME_RANGE_TYPE (expr->u.nary->op[0]),
3137 wide_int_storage::from (min, TYPE_PRECISION (type),
3138 TYPE_SIGN (type)),
3139 wide_int_storage::from (max, TYPE_PRECISION (type),
3140 TYPE_SIGN (type)));
3143 if (dump_file && (dump_flags & TDF_DETAILS))
3145 fprintf (dump_file, "Created phi ");
3146 print_gimple_stmt (dump_file, phi, 0);
3147 fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3149 pre_stats.phis++;
3150 return true;
3155 /* Perform insertion of partially redundant or hoistable values.
3156 For BLOCK, do the following:
3157 1. Propagate the NEW_SETS of the dominator into the current block.
3158 If the block has multiple predecessors,
3159 2a. Iterate over the ANTIC expressions for the block to see if
3160 any of them are partially redundant.
3161 2b. If so, insert them into the necessary predecessors to make
3162 the expression fully redundant.
3163 2c. Insert a new PHI merging the values of the predecessors.
3164 2d. Insert the new PHI, and the new expressions, into the
3165 NEW_SETS set.
3166 If the block has multiple successors,
3167 3a. Iterate over the ANTIC values for the block to see if
3168 any of them are good candidates for hoisting.
3169 3b. If so, insert expressions computing the values in BLOCK,
3170 and add the new expressions into the NEW_SETS set.
3171 4. Recursively call ourselves on the dominator children of BLOCK.
3173 Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3174 do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3175 done in do_hoist_insertion.
3178 static bool
3179 do_pre_regular_insertion (basic_block block, basic_block dom)
3181 bool new_stuff = false;
3182 vec<pre_expr> exprs;
3183 pre_expr expr;
3184 auto_vec<pre_expr> avail;
3185 int i;
3187 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3188 avail.safe_grow (EDGE_COUNT (block->preds));
3190 FOR_EACH_VEC_ELT (exprs, i, expr)
3192 if (expr->kind == NARY
3193 || expr->kind == REFERENCE)
3195 unsigned int val;
3196 bool by_some = false;
3197 bool cant_insert = false;
3198 bool all_same = true;
3199 pre_expr first_s = NULL;
3200 edge pred;
3201 basic_block bprime;
3202 pre_expr eprime = NULL;
3203 edge_iterator ei;
3204 pre_expr edoubleprime = NULL;
3205 bool do_insertion = false;
3207 val = get_expr_value_id (expr);
3208 if (bitmap_set_contains_value (PHI_GEN (block), val))
3209 continue;
3210 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3212 if (dump_file && (dump_flags & TDF_DETAILS))
3214 fprintf (dump_file, "Found fully redundant value: ");
3215 print_pre_expr (dump_file, expr);
3216 fprintf (dump_file, "\n");
3218 continue;
3221 FOR_EACH_EDGE (pred, ei, block->preds)
3223 unsigned int vprime;
3225 /* We should never run insertion for the exit block
3226 and so not come across fake pred edges. */
3227 gcc_assert (!(pred->flags & EDGE_FAKE));
3228 bprime = pred->src;
3229 /* We are looking at ANTIC_OUT of bprime. */
3230 eprime = phi_translate (NULL, expr, ANTIC_IN (block), NULL, pred);
3232 /* eprime will generally only be NULL if the
3233 value of the expression, translated
3234 through the PHI for this predecessor, is
3235 undefined. If that is the case, we can't
3236 make the expression fully redundant,
3237 because its value is undefined along a
3238 predecessor path. We can thus break out
3239 early because it doesn't matter what the
3240 rest of the results are. */
3241 if (eprime == NULL)
3243 avail[pred->dest_idx] = NULL;
3244 cant_insert = true;
3245 break;
3248 vprime = get_expr_value_id (eprime);
3249 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3250 vprime);
3251 if (edoubleprime == NULL)
3253 avail[pred->dest_idx] = eprime;
3254 all_same = false;
3256 else
3258 avail[pred->dest_idx] = edoubleprime;
3259 by_some = true;
3260 /* We want to perform insertions to remove a redundancy on
3261 a path in the CFG we want to optimize for speed. */
3262 if (optimize_edge_for_speed_p (pred))
3263 do_insertion = true;
3264 if (first_s == NULL)
3265 first_s = edoubleprime;
3266 else if (!pre_expr_d::equal (first_s, edoubleprime))
3267 all_same = false;
3270 /* If we can insert it, it's not the same value
3271 already existing along every predecessor, and
3272 it's defined by some predecessor, it is
3273 partially redundant. */
3274 if (!cant_insert && !all_same && by_some)
3276 if (!do_insertion)
3278 if (dump_file && (dump_flags & TDF_DETAILS))
3280 fprintf (dump_file, "Skipping partial redundancy for "
3281 "expression ");
3282 print_pre_expr (dump_file, expr);
3283 fprintf (dump_file, " (%04d), no redundancy on to be "
3284 "optimized for speed edge\n", val);
3287 else if (dbg_cnt (treepre_insert))
3289 if (dump_file && (dump_flags & TDF_DETAILS))
3291 fprintf (dump_file, "Found partial redundancy for "
3292 "expression ");
3293 print_pre_expr (dump_file, expr);
3294 fprintf (dump_file, " (%04d)\n",
3295 get_expr_value_id (expr));
3297 if (insert_into_preds_of_block (block,
3298 get_expression_id (expr),
3299 avail))
3300 new_stuff = true;
3303 /* If all edges produce the same value and that value is
3304 an invariant, then the PHI has the same value on all
3305 edges. Note this. */
3306 else if (!cant_insert && all_same)
3308 gcc_assert (edoubleprime->kind == CONSTANT
3309 || edoubleprime->kind == NAME);
3311 tree temp = make_temp_ssa_name (get_expr_type (expr),
3312 NULL, "pretmp");
3313 gassign *assign
3314 = gimple_build_assign (temp,
3315 edoubleprime->kind == CONSTANT ?
3316 PRE_EXPR_CONSTANT (edoubleprime) :
3317 PRE_EXPR_NAME (edoubleprime));
3318 gimple_stmt_iterator gsi = gsi_after_labels (block);
3319 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3321 VN_INFO (temp)->value_id = val;
3322 VN_INFO (temp)->valnum = vn_valnum_from_value_id (val);
3323 if (VN_INFO (temp)->valnum == NULL_TREE)
3324 VN_INFO (temp)->valnum = temp;
3325 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3326 pre_expr newe = get_or_alloc_expr_for_name (temp);
3327 add_to_value (val, newe);
3328 bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3329 bitmap_insert_into_set (NEW_SETS (block), newe);
3334 exprs.release ();
3335 return new_stuff;
3339 /* Perform insertion for partially anticipatable expressions. There
3340 is only one case we will perform insertion for these. This case is
3341 if the expression is partially anticipatable, and fully available.
3342 In this case, we know that putting it earlier will enable us to
3343 remove the later computation. */
3345 static bool
3346 do_pre_partial_partial_insertion (basic_block block, basic_block dom)
3348 bool new_stuff = false;
3349 vec<pre_expr> exprs;
3350 pre_expr expr;
3351 auto_vec<pre_expr> avail;
3352 int i;
3354 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3355 avail.safe_grow (EDGE_COUNT (block->preds));
3357 FOR_EACH_VEC_ELT (exprs, i, expr)
3359 if (expr->kind == NARY
3360 || expr->kind == REFERENCE)
3362 unsigned int val;
3363 bool by_all = true;
3364 bool cant_insert = false;
3365 edge pred;
3366 basic_block bprime;
3367 pre_expr eprime = NULL;
3368 edge_iterator ei;
3370 val = get_expr_value_id (expr);
3371 if (bitmap_set_contains_value (PHI_GEN (block), val))
3372 continue;
3373 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3374 continue;
3376 FOR_EACH_EDGE (pred, ei, block->preds)
3378 unsigned int vprime;
3379 pre_expr edoubleprime;
3381 /* We should never run insertion for the exit block
3382 and so not come across fake pred edges. */
3383 gcc_assert (!(pred->flags & EDGE_FAKE));
3384 bprime = pred->src;
3385 eprime = phi_translate (NULL, expr, ANTIC_IN (block),
3386 PA_IN (block), pred);
3388 /* eprime will generally only be NULL if the
3389 value of the expression, translated
3390 through the PHI for this predecessor, is
3391 undefined. If that is the case, we can't
3392 make the expression fully redundant,
3393 because its value is undefined along a
3394 predecessor path. We can thus break out
3395 early because it doesn't matter what the
3396 rest of the results are. */
3397 if (eprime == NULL)
3399 avail[pred->dest_idx] = NULL;
3400 cant_insert = true;
3401 break;
3404 vprime = get_expr_value_id (eprime);
3405 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3406 avail[pred->dest_idx] = edoubleprime;
3407 if (edoubleprime == NULL)
3409 by_all = false;
3410 break;
3414 /* If we can insert it, it's not the same value
3415 already existing along every predecessor, and
3416 it's defined by some predecessor, it is
3417 partially redundant. */
3418 if (!cant_insert && by_all)
3420 edge succ;
3421 bool do_insertion = false;
3423 /* Insert only if we can remove a later expression on a path
3424 that we want to optimize for speed.
3425 The phi node that we will be inserting in BLOCK is not free,
3426 and inserting it for the sake of !optimize_for_speed successor
3427 may cause regressions on the speed path. */
3428 FOR_EACH_EDGE (succ, ei, block->succs)
3430 if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3431 || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3433 if (optimize_edge_for_speed_p (succ))
3434 do_insertion = true;
3438 if (!do_insertion)
3440 if (dump_file && (dump_flags & TDF_DETAILS))
3442 fprintf (dump_file, "Skipping partial partial redundancy "
3443 "for expression ");
3444 print_pre_expr (dump_file, expr);
3445 fprintf (dump_file, " (%04d), not (partially) anticipated "
3446 "on any to be optimized for speed edges\n", val);
3449 else if (dbg_cnt (treepre_insert))
3451 pre_stats.pa_insert++;
3452 if (dump_file && (dump_flags & TDF_DETAILS))
3454 fprintf (dump_file, "Found partial partial redundancy "
3455 "for expression ");
3456 print_pre_expr (dump_file, expr);
3457 fprintf (dump_file, " (%04d)\n",
3458 get_expr_value_id (expr));
3460 if (insert_into_preds_of_block (block,
3461 get_expression_id (expr),
3462 avail))
3463 new_stuff = true;
3469 exprs.release ();
3470 return new_stuff;
3473 /* Insert expressions in BLOCK to compute hoistable values up.
3474 Return TRUE if something was inserted, otherwise return FALSE.
3475 The caller has to make sure that BLOCK has at least two successors. */
3477 static bool
3478 do_hoist_insertion (basic_block block)
3480 edge e;
3481 edge_iterator ei;
3482 bool new_stuff = false;
3483 unsigned i;
3484 gimple_stmt_iterator last;
3486 /* At least two successors, or else... */
3487 gcc_assert (EDGE_COUNT (block->succs) >= 2);
3489 /* Check that all successors of BLOCK are dominated by block.
3490 We could use dominated_by_p() for this, but actually there is a much
3491 quicker check: any successor that is dominated by BLOCK can't have
3492 more than one predecessor edge. */
3493 FOR_EACH_EDGE (e, ei, block->succs)
3494 if (! single_pred_p (e->dest))
3495 return false;
3497 /* Determine the insertion point. If we cannot safely insert before
3498 the last stmt if we'd have to, bail out. */
3499 last = gsi_last_bb (block);
3500 if (!gsi_end_p (last)
3501 && !is_ctrl_stmt (gsi_stmt (last))
3502 && stmt_ends_bb_p (gsi_stmt (last)))
3503 return false;
3505 /* Compute the set of hoistable expressions from ANTIC_IN. First compute
3506 hoistable values. */
3507 bitmap_set hoistable_set;
3509 /* A hoistable value must be in ANTIC_IN(block)
3510 but not in AVAIL_OUT(BLOCK). */
3511 bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
3512 bitmap_and_compl (&hoistable_set.values,
3513 &ANTIC_IN (block)->values, &AVAIL_OUT (block)->values);
3515 /* Short-cut for a common case: hoistable_set is empty. */
3516 if (bitmap_empty_p (&hoistable_set.values))
3517 return false;
3519 /* Compute which of the hoistable values is in AVAIL_OUT of
3520 at least one of the successors of BLOCK. */
3521 bitmap_head availout_in_some;
3522 bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
3523 FOR_EACH_EDGE (e, ei, block->succs)
3524 /* Do not consider expressions solely because their availability
3525 on loop exits. They'd be ANTIC-IN throughout the whole loop
3526 and thus effectively hoisted across loops by combination of
3527 PRE and hoisting. */
3528 if (! loop_exit_edge_p (block->loop_father, e))
3529 bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
3530 &AVAIL_OUT (e->dest)->values);
3531 bitmap_clear (&hoistable_set.values);
3533 /* Short-cut for a common case: availout_in_some is empty. */
3534 if (bitmap_empty_p (&availout_in_some))
3535 return false;
3537 /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */
3538 hoistable_set.values = availout_in_some;
3539 hoistable_set.expressions = ANTIC_IN (block)->expressions;
3541 /* Now finally construct the topological-ordered expression set. */
3542 vec<pre_expr> exprs = sorted_array_from_bitmap_set (&hoistable_set);
3544 bitmap_clear (&hoistable_set.values);
3546 /* If there are candidate values for hoisting, insert expressions
3547 strategically to make the hoistable expressions fully redundant. */
3548 pre_expr expr;
3549 FOR_EACH_VEC_ELT (exprs, i, expr)
3551 /* While we try to sort expressions topologically above the
3552 sorting doesn't work out perfectly. Catch expressions we
3553 already inserted. */
3554 unsigned int value_id = get_expr_value_id (expr);
3555 if (bitmap_set_contains_value (AVAIL_OUT (block), value_id))
3557 if (dump_file && (dump_flags & TDF_DETAILS))
3559 fprintf (dump_file,
3560 "Already inserted expression for ");
3561 print_pre_expr (dump_file, expr);
3562 fprintf (dump_file, " (%04d)\n", value_id);
3564 continue;
3567 /* OK, we should hoist this value. Perform the transformation. */
3568 pre_stats.hoist_insert++;
3569 if (dump_file && (dump_flags & TDF_DETAILS))
3571 fprintf (dump_file,
3572 "Inserting expression in block %d for code hoisting: ",
3573 block->index);
3574 print_pre_expr (dump_file, expr);
3575 fprintf (dump_file, " (%04d)\n", value_id);
3578 gimple_seq stmts = NULL;
3579 tree res = create_expression_by_pieces (block, expr, &stmts,
3580 get_expr_type (expr));
3582 /* Do not return true if expression creation ultimately
3583 did not insert any statements. */
3584 if (gimple_seq_empty_p (stmts))
3585 res = NULL_TREE;
3586 else
3588 if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
3589 gsi_insert_seq_before (&last, stmts, GSI_SAME_STMT);
3590 else
3591 gsi_insert_seq_after (&last, stmts, GSI_NEW_STMT);
3594 /* Make sure to not return true if expression creation ultimately
3595 failed but also make sure to insert any stmts produced as they
3596 are tracked in inserted_exprs. */
3597 if (! res)
3598 continue;
3600 new_stuff = true;
3603 exprs.release ();
3605 return new_stuff;
3608 /* Do a dominator walk on the control flow graph, and insert computations
3609 of values as necessary for PRE and hoisting. */
3611 static bool
3612 insert_aux (basic_block block, bool do_pre, bool do_hoist)
3614 basic_block son;
3615 bool new_stuff = false;
3617 if (block)
3619 basic_block dom;
3620 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3621 if (dom)
3623 unsigned i;
3624 bitmap_iterator bi;
3625 bitmap_set_t newset;
3627 /* First, update the AVAIL_OUT set with anything we may have
3628 inserted higher up in the dominator tree. */
3629 newset = NEW_SETS (dom);
3630 if (newset)
3632 /* Note that we need to value_replace both NEW_SETS, and
3633 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3634 represented by some non-simple expression here that we want
3635 to replace it with. */
3636 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3638 pre_expr expr = expression_for_id (i);
3639 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3640 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3644 /* Insert expressions for partial redundancies. */
3645 if (do_pre && !single_pred_p (block))
3647 new_stuff |= do_pre_regular_insertion (block, dom);
3648 if (do_partial_partial)
3649 new_stuff |= do_pre_partial_partial_insertion (block, dom);
3652 /* Insert expressions for hoisting. */
3653 if (do_hoist && EDGE_COUNT (block->succs) >= 2)
3654 new_stuff |= do_hoist_insertion (block);
3657 for (son = first_dom_son (CDI_DOMINATORS, block);
3658 son;
3659 son = next_dom_son (CDI_DOMINATORS, son))
3661 new_stuff |= insert_aux (son, do_pre, do_hoist);
3664 return new_stuff;
3667 /* Perform insertion of partially redundant and hoistable values. */
3669 static void
3670 insert (void)
3672 bool new_stuff = true;
3673 basic_block bb;
3674 int num_iterations = 0;
3676 FOR_ALL_BB_FN (bb, cfun)
3677 NEW_SETS (bb) = bitmap_set_new ();
3679 while (new_stuff)
3681 num_iterations++;
3682 if (dump_file && dump_flags & TDF_DETAILS)
3683 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3684 new_stuff = insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun), flag_tree_pre,
3685 flag_code_hoisting);
3687 /* Clear the NEW sets before the next iteration. We have already
3688 fully propagated its contents. */
3689 if (new_stuff)
3690 FOR_ALL_BB_FN (bb, cfun)
3691 bitmap_set_free (NEW_SETS (bb));
3693 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3697 /* Compute the AVAIL set for all basic blocks.
3699 This function performs value numbering of the statements in each basic
3700 block. The AVAIL sets are built from information we glean while doing
3701 this value numbering, since the AVAIL sets contain only one entry per
3702 value.
3704 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3705 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3707 static void
3708 compute_avail (void)
3711 basic_block block, son;
3712 basic_block *worklist;
3713 size_t sp = 0;
3714 unsigned i;
3715 tree name;
3717 /* We pretend that default definitions are defined in the entry block.
3718 This includes function arguments and the static chain decl. */
3719 FOR_EACH_SSA_NAME (i, name, cfun)
3721 pre_expr e;
3722 if (!SSA_NAME_IS_DEFAULT_DEF (name)
3723 || has_zero_uses (name)
3724 || virtual_operand_p (name))
3725 continue;
3727 e = get_or_alloc_expr_for_name (name);
3728 add_to_value (get_expr_value_id (e), e);
3729 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), e);
3730 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3734 if (dump_file && (dump_flags & TDF_DETAILS))
3736 print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3737 "tmp_gen", ENTRY_BLOCK);
3738 print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3739 "avail_out", ENTRY_BLOCK);
3742 /* Allocate the worklist. */
3743 worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
3745 /* Seed the algorithm by putting the dominator children of the entry
3746 block on the worklist. */
3747 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun));
3748 son;
3749 son = next_dom_son (CDI_DOMINATORS, son))
3750 worklist[sp++] = son;
3752 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun))
3753 = ssa_default_def (cfun, gimple_vop (cfun));
3755 /* Loop until the worklist is empty. */
3756 while (sp)
3758 gimple *stmt;
3759 basic_block dom;
3761 /* Pick a block from the worklist. */
3762 block = worklist[--sp];
3763 vn_context_bb = block;
3765 /* Initially, the set of available values in BLOCK is that of
3766 its immediate dominator. */
3767 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3768 if (dom)
3770 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3771 BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
3774 /* Generate values for PHI nodes. */
3775 for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
3776 gsi_next (&gsi))
3778 tree result = gimple_phi_result (gsi.phi ());
3780 /* We have no need for virtual phis, as they don't represent
3781 actual computations. */
3782 if (virtual_operand_p (result))
3784 BB_LIVE_VOP_ON_EXIT (block) = result;
3785 continue;
3788 pre_expr e = get_or_alloc_expr_for_name (result);
3789 add_to_value (get_expr_value_id (e), e);
3790 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3791 bitmap_insert_into_set (PHI_GEN (block), e);
3794 BB_MAY_NOTRETURN (block) = 0;
3796 /* Now compute value numbers and populate value sets with all
3797 the expressions computed in BLOCK. */
3798 for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
3799 gsi_next (&gsi))
3801 ssa_op_iter iter;
3802 tree op;
3804 stmt = gsi_stmt (gsi);
3806 /* Cache whether the basic-block has any non-visible side-effect
3807 or control flow.
3808 If this isn't a call or it is the last stmt in the
3809 basic-block then the CFG represents things correctly. */
3810 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3812 /* Non-looping const functions always return normally.
3813 Otherwise the call might not return or have side-effects
3814 that forbids hoisting possibly trapping expressions
3815 before it. */
3816 int flags = gimple_call_flags (stmt);
3817 if (!(flags & ECF_CONST)
3818 || (flags & ECF_LOOPING_CONST_OR_PURE))
3819 BB_MAY_NOTRETURN (block) = 1;
3822 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3824 pre_expr e = get_or_alloc_expr_for_name (op);
3826 add_to_value (get_expr_value_id (e), e);
3827 bitmap_insert_into_set (TMP_GEN (block), e);
3828 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3831 if (gimple_vdef (stmt))
3832 BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
3834 if (gimple_has_side_effects (stmt)
3835 || stmt_could_throw_p (stmt)
3836 || is_gimple_debug (stmt))
3837 continue;
3839 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3841 if (ssa_undefined_value_p (op))
3842 continue;
3843 pre_expr e = get_or_alloc_expr_for_name (op);
3844 bitmap_value_insert_into_set (EXP_GEN (block), e);
3847 switch (gimple_code (stmt))
3849 case GIMPLE_RETURN:
3850 continue;
3852 case GIMPLE_CALL:
3854 vn_reference_t ref;
3855 vn_reference_s ref1;
3856 pre_expr result = NULL;
3858 /* We can value number only calls to real functions. */
3859 if (gimple_call_internal_p (stmt))
3860 continue;
3862 vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
3863 if (!ref)
3864 continue;
3866 /* If the value of the call is not invalidated in
3867 this block until it is computed, add the expression
3868 to EXP_GEN. */
3869 if (!gimple_vuse (stmt)
3870 || gimple_code
3871 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3872 || gimple_bb (SSA_NAME_DEF_STMT
3873 (gimple_vuse (stmt))) != block)
3875 result = pre_expr_pool.allocate ();
3876 result->kind = REFERENCE;
3877 result->id = 0;
3878 PRE_EXPR_REFERENCE (result) = ref;
3880 get_or_alloc_expression_id (result);
3881 add_to_value (get_expr_value_id (result), result);
3882 bitmap_value_insert_into_set (EXP_GEN (block), result);
3884 continue;
3887 case GIMPLE_ASSIGN:
3889 pre_expr result = NULL;
3890 switch (vn_get_stmt_kind (stmt))
3892 case VN_NARY:
3894 enum tree_code code = gimple_assign_rhs_code (stmt);
3895 vn_nary_op_t nary;
3897 /* COND_EXPR and VEC_COND_EXPR are awkward in
3898 that they contain an embedded complex expression.
3899 Don't even try to shove those through PRE. */
3900 if (code == COND_EXPR
3901 || code == VEC_COND_EXPR)
3902 continue;
3904 vn_nary_op_lookup_stmt (stmt, &nary);
3905 if (!nary)
3906 continue;
3908 /* If the NARY traps and there was a preceding
3909 point in the block that might not return avoid
3910 adding the nary to EXP_GEN. */
3911 if (BB_MAY_NOTRETURN (block)
3912 && vn_nary_may_trap (nary))
3913 continue;
3915 result = pre_expr_pool.allocate ();
3916 result->kind = NARY;
3917 result->id = 0;
3918 PRE_EXPR_NARY (result) = nary;
3919 break;
3922 case VN_REFERENCE:
3924 tree rhs1 = gimple_assign_rhs1 (stmt);
3925 alias_set_type set = get_alias_set (rhs1);
3926 vec<vn_reference_op_s> operands
3927 = vn_reference_operands_for_lookup (rhs1);
3928 vn_reference_t ref;
3929 vn_reference_lookup_pieces (gimple_vuse (stmt), set,
3930 TREE_TYPE (rhs1),
3931 operands, &ref, VN_WALK);
3932 if (!ref)
3934 operands.release ();
3935 continue;
3938 /* If the value of the reference is not invalidated in
3939 this block until it is computed, add the expression
3940 to EXP_GEN. */
3941 if (gimple_vuse (stmt))
3943 gimple *def_stmt;
3944 bool ok = true;
3945 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
3946 while (!gimple_nop_p (def_stmt)
3947 && gimple_code (def_stmt) != GIMPLE_PHI
3948 && gimple_bb (def_stmt) == block)
3950 if (stmt_may_clobber_ref_p
3951 (def_stmt, gimple_assign_rhs1 (stmt)))
3953 ok = false;
3954 break;
3956 def_stmt
3957 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
3959 if (!ok)
3961 operands.release ();
3962 continue;
3966 /* If the load was value-numbered to another
3967 load make sure we do not use its expression
3968 for insertion if it wouldn't be a valid
3969 replacement. */
3970 /* At the momemt we have a testcase
3971 for hoist insertion of aligned vs. misaligned
3972 variants in gcc.dg/torture/pr65270-1.c thus
3973 with just alignment to be considered we can
3974 simply replace the expression in the hashtable
3975 with the most conservative one. */
3976 vn_reference_op_t ref1 = &ref->operands.last ();
3977 while (ref1->opcode != TARGET_MEM_REF
3978 && ref1->opcode != MEM_REF
3979 && ref1 != &ref->operands[0])
3980 --ref1;
3981 vn_reference_op_t ref2 = &operands.last ();
3982 while (ref2->opcode != TARGET_MEM_REF
3983 && ref2->opcode != MEM_REF
3984 && ref2 != &operands[0])
3985 --ref2;
3986 if ((ref1->opcode == TARGET_MEM_REF
3987 || ref1->opcode == MEM_REF)
3988 && (TYPE_ALIGN (ref1->type)
3989 > TYPE_ALIGN (ref2->type)))
3990 ref1->type
3991 = build_aligned_type (ref1->type,
3992 TYPE_ALIGN (ref2->type));
3993 /* TBAA behavior is an obvious part so make sure
3994 that the hashtable one covers this as well
3995 by adjusting the ref alias set and its base. */
3996 if (ref->set == set
3997 || alias_set_subset_of (set, ref->set))
3999 else if (alias_set_subset_of (ref->set, set))
4001 ref->set = set;
4002 if (ref1->opcode == MEM_REF)
4003 ref1->op0
4004 = wide_int_to_tree (TREE_TYPE (ref2->op0),
4005 wi::to_wide (ref1->op0));
4006 else
4007 ref1->op2
4008 = wide_int_to_tree (TREE_TYPE (ref2->op2),
4009 wi::to_wide (ref1->op2));
4011 else
4013 ref->set = 0;
4014 if (ref1->opcode == MEM_REF)
4015 ref1->op0
4016 = wide_int_to_tree (ptr_type_node,
4017 wi::to_wide (ref1->op0));
4018 else
4019 ref1->op2
4020 = wide_int_to_tree (ptr_type_node,
4021 wi::to_wide (ref1->op2));
4023 operands.release ();
4025 result = pre_expr_pool.allocate ();
4026 result->kind = REFERENCE;
4027 result->id = 0;
4028 PRE_EXPR_REFERENCE (result) = ref;
4029 break;
4032 default:
4033 continue;
4036 get_or_alloc_expression_id (result);
4037 add_to_value (get_expr_value_id (result), result);
4038 bitmap_value_insert_into_set (EXP_GEN (block), result);
4039 continue;
4041 default:
4042 break;
4046 if (dump_file && (dump_flags & TDF_DETAILS))
4048 print_bitmap_set (dump_file, EXP_GEN (block),
4049 "exp_gen", block->index);
4050 print_bitmap_set (dump_file, PHI_GEN (block),
4051 "phi_gen", block->index);
4052 print_bitmap_set (dump_file, TMP_GEN (block),
4053 "tmp_gen", block->index);
4054 print_bitmap_set (dump_file, AVAIL_OUT (block),
4055 "avail_out", block->index);
4058 /* Put the dominator children of BLOCK on the worklist of blocks
4059 to compute available sets for. */
4060 for (son = first_dom_son (CDI_DOMINATORS, block);
4061 son;
4062 son = next_dom_son (CDI_DOMINATORS, son))
4063 worklist[sp++] = son;
4065 vn_context_bb = NULL;
4067 free (worklist);
4071 /* Initialize data structures used by PRE. */
4073 static void
4074 init_pre (void)
4076 basic_block bb;
4078 next_expression_id = 1;
4079 expressions.create (0);
4080 expressions.safe_push (NULL);
4081 value_expressions.create (get_max_value_id () + 1);
4082 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
4083 name_to_id.create (0);
4085 inserted_exprs = BITMAP_ALLOC (NULL);
4087 connect_infinite_loops_to_exit ();
4088 memset (&pre_stats, 0, sizeof (pre_stats));
4090 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4092 calculate_dominance_info (CDI_DOMINATORS);
4094 bitmap_obstack_initialize (&grand_bitmap_obstack);
4095 phi_translate_table = new hash_table<expr_pred_trans_d> (5110);
4096 expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4097 FOR_ALL_BB_FN (bb, cfun)
4099 EXP_GEN (bb) = bitmap_set_new ();
4100 PHI_GEN (bb) = bitmap_set_new ();
4101 TMP_GEN (bb) = bitmap_set_new ();
4102 AVAIL_OUT (bb) = bitmap_set_new ();
4107 /* Deallocate data structures used by PRE. */
4109 static void
4110 fini_pre ()
4112 value_expressions.release ();
4113 expressions.release ();
4114 BITMAP_FREE (inserted_exprs);
4115 bitmap_obstack_release (&grand_bitmap_obstack);
4116 bitmap_set_pool.release ();
4117 pre_expr_pool.release ();
4118 delete phi_translate_table;
4119 phi_translate_table = NULL;
4120 delete expression_to_id;
4121 expression_to_id = NULL;
4122 name_to_id.release ();
4124 free_aux_for_blocks ();
4127 namespace {
4129 const pass_data pass_data_pre =
4131 GIMPLE_PASS, /* type */
4132 "pre", /* name */
4133 OPTGROUP_NONE, /* optinfo_flags */
4134 TV_TREE_PRE, /* tv_id */
4135 ( PROP_cfg | PROP_ssa ), /* properties_required */
4136 0, /* properties_provided */
4137 0, /* properties_destroyed */
4138 TODO_rebuild_alias, /* todo_flags_start */
4139 0, /* todo_flags_finish */
4142 class pass_pre : public gimple_opt_pass
4144 public:
4145 pass_pre (gcc::context *ctxt)
4146 : gimple_opt_pass (pass_data_pre, ctxt)
4149 /* opt_pass methods: */
4150 virtual bool gate (function *)
4151 { return flag_tree_pre != 0 || flag_code_hoisting != 0; }
4152 virtual unsigned int execute (function *);
4154 }; // class pass_pre
4156 /* Valueization hook for RPO VN when we are calling back to it
4157 at ANTIC compute time. */
4159 static tree
4160 pre_valueize (tree name)
4162 if (TREE_CODE (name) == SSA_NAME)
4164 tree tem = VN_INFO (name)->valnum;
4165 if (tem != VN_TOP && tem != name)
4167 if (TREE_CODE (tem) != SSA_NAME
4168 || SSA_NAME_IS_DEFAULT_DEF (tem))
4169 return tem;
4170 /* We create temporary SSA names for representatives that
4171 do not have a definition (yet) but are not default defs either
4172 assume they are fine to use. */
4173 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (tem));
4174 if (! def_bb
4175 || dominated_by_p (CDI_DOMINATORS, vn_context_bb, def_bb))
4176 return tem;
4177 /* ??? Now we could look for a leader. Ideally we'd somehow
4178 expose RPO VN leaders and get rid of AVAIL_OUT as well... */
4181 return name;
4184 unsigned int
4185 pass_pre::execute (function *fun)
4187 unsigned int todo = 0;
4189 do_partial_partial =
4190 flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4192 /* This has to happen before VN runs because
4193 loop_optimizer_init may create new phis, etc. */
4194 loop_optimizer_init (LOOPS_NORMAL);
4195 split_critical_edges ();
4196 scev_initialize ();
4198 run_rpo_vn (VN_WALK);
4200 init_pre ();
4202 vn_valueize = pre_valueize;
4204 /* Insert can get quite slow on an incredibly large number of basic
4205 blocks due to some quadratic behavior. Until this behavior is
4206 fixed, don't run it when he have an incredibly large number of
4207 bb's. If we aren't going to run insert, there is no point in
4208 computing ANTIC, either, even though it's plenty fast nor do
4209 we require AVAIL. */
4210 if (n_basic_blocks_for_fn (fun) < 4000)
4212 compute_avail ();
4213 compute_antic ();
4214 insert ();
4217 /* Make sure to remove fake edges before committing our inserts.
4218 This makes sure we don't end up with extra critical edges that
4219 we would need to split. */
4220 remove_fake_exit_edges ();
4221 gsi_commit_edge_inserts ();
4223 /* Eliminate folds statements which might (should not...) end up
4224 not keeping virtual operands up-to-date. */
4225 gcc_assert (!need_ssa_update_p (fun));
4227 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4228 statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4229 statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
4230 statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4232 todo |= eliminate_with_rpo_vn (inserted_exprs);
4234 vn_valueize = NULL;
4236 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4237 to insert PHI nodes sometimes, and because value numbering of casts isn't
4238 perfect, we sometimes end up inserting dead code. This simple DCE-like
4239 pass removes any insertions we made that weren't actually used. */
4240 simple_dce_from_worklist (inserted_exprs);
4242 fini_pre ();
4244 scev_finalize ();
4245 loop_optimizer_finalize ();
4247 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4248 case we can merge the block with the remaining predecessor of the block.
4249 It should either:
4250 - call merge_blocks after each tail merge iteration
4251 - call merge_blocks after all tail merge iterations
4252 - mark TODO_cleanup_cfg when necessary
4253 - share the cfg cleanup with fini_pre. */
4254 todo |= tail_merge_optimize (todo);
4256 free_rpo_vn ();
4258 /* Tail merging invalidates the virtual SSA web, together with
4259 cfg-cleanup opportunities exposed by PRE this will wreck the
4260 SSA updating machinery. So make sure to run update-ssa
4261 manually, before eventually scheduling cfg-cleanup as part of
4262 the todo. */
4263 update_ssa (TODO_update_ssa_only_virtuals);
4265 return todo;
4268 } // anon namespace
4270 gimple_opt_pass *
4271 make_pass_pre (gcc::context *ctxt)
4273 return new pass_pre (ctxt);