Emit .note.GNU-stack for hard-float linux targets.
[official-gcc.git] / gcc / tree-ssa-pre.c
blobeaed4b16ed83f2914aef10a2ccbf05a680e9c094
1 /* Full and partial redundancy elimination and code hoisting on SSA GIMPLE.
2 Copyright (C) 2001-2020 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 "dbgcnt.h"
49 #include "domwalk.h"
50 #include "tree-ssa-propagate.h"
51 #include "tree-ssa-dce.h"
52 #include "tree-cfgcleanup.h"
53 #include "alias.h"
55 /* Even though this file is called tree-ssa-pre.c, we actually
56 implement a bit more than just PRE here. All of them piggy-back
57 on GVN which is implemented in tree-ssa-sccvn.c.
59 1. Full Redundancy Elimination (FRE)
60 This is the elimination phase of GVN.
62 2. Partial Redundancy Elimination (PRE)
63 This is adds computation of AVAIL_OUT and ANTIC_IN and
64 doing expression insertion to form GVN-PRE.
66 3. Code hoisting
67 This optimization uses the ANTIC_IN sets computed for PRE
68 to move expressions further up than PRE would do, to make
69 multiple computations of the same value fully redundant.
70 This pass is explained below (after the explanation of the
71 basic algorithm for PRE).
74 /* TODO:
76 1. Avail sets can be shared by making an avail_find_leader that
77 walks up the dominator tree and looks in those avail sets.
78 This might affect code optimality, it's unclear right now.
79 Currently the AVAIL_OUT sets are the remaining quadraticness in
80 memory of GVN-PRE.
81 2. Strength reduction can be performed by anticipating expressions
82 we can repair later on.
83 3. We can do back-substitution or smarter value numbering to catch
84 commutative expressions split up over multiple statements.
87 /* For ease of terminology, "expression node" in the below refers to
88 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
89 represent the actual statement containing the expressions we care about,
90 and we cache the value number by putting it in the expression. */
92 /* Basic algorithm for Partial Redundancy Elimination:
94 First we walk the statements to generate the AVAIL sets, the
95 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
96 generation of values/expressions by a given block. We use them
97 when computing the ANTIC sets. The AVAIL sets consist of
98 SSA_NAME's that represent values, so we know what values are
99 available in what blocks. AVAIL is a forward dataflow problem. In
100 SSA, values are never killed, so we don't need a kill set, or a
101 fixpoint iteration, in order to calculate the AVAIL sets. In
102 traditional parlance, AVAIL sets tell us the downsafety of the
103 expressions/values.
105 Next, we generate the ANTIC sets. These sets represent the
106 anticipatable expressions. ANTIC is a backwards dataflow
107 problem. An expression is anticipatable in a given block if it could
108 be generated in that block. This means that if we had to perform
109 an insertion in that block, of the value of that expression, we
110 could. Calculating the ANTIC sets requires phi translation of
111 expressions, because the flow goes backwards through phis. We must
112 iterate to a fixpoint of the ANTIC sets, because we have a kill
113 set. Even in SSA form, values are not live over the entire
114 function, only from their definition point onwards. So we have to
115 remove values from the ANTIC set once we go past the definition
116 point of the leaders that make them up.
117 compute_antic/compute_antic_aux performs this computation.
119 Third, we perform insertions to make partially redundant
120 expressions fully redundant.
122 An expression is partially redundant (excluding partial
123 anticipation) if:
125 1. It is AVAIL in some, but not all, of the predecessors of a
126 given block.
127 2. It is ANTIC in all the predecessors.
129 In order to make it fully redundant, we insert the expression into
130 the predecessors where it is not available, but is ANTIC.
132 When optimizing for size, we only eliminate the partial redundancy
133 if we need to insert in only one predecessor. This avoids almost
134 completely the code size increase that PRE usually causes.
136 For the partial anticipation case, we only perform insertion if it
137 is partially anticipated in some block, and fully available in all
138 of the predecessors.
140 do_pre_regular_insertion/do_pre_partial_partial_insertion
141 performs these steps, driven by insert/insert_aux.
143 Fourth, we eliminate fully redundant expressions.
144 This is a simple statement walk that replaces redundant
145 calculations with the now available values. */
147 /* Basic algorithm for Code Hoisting:
149 Code hoisting is: Moving value computations up in the control flow
150 graph to make multiple copies redundant. Typically this is a size
151 optimization, but there are cases where it also is helpful for speed.
153 A simple code hoisting algorithm is implemented that piggy-backs on
154 the PRE infrastructure. For code hoisting, we have to know ANTIC_OUT
155 which is effectively ANTIC_IN - AVAIL_OUT. The latter two have to be
156 computed for PRE, and we can use them to perform a limited version of
157 code hoisting, too.
159 For the purpose of this implementation, a value is hoistable to a basic
160 block B if the following properties are met:
162 1. The value is in ANTIC_IN(B) -- the value will be computed on all
163 paths from B to function exit and it can be computed in B);
165 2. The value is not in AVAIL_OUT(B) -- there would be no need to
166 compute the value again and make it available twice;
168 3. All successors of B are dominated by B -- makes sure that inserting
169 a computation of the value in B will make the remaining
170 computations fully redundant;
172 4. At least one successor has the value in AVAIL_OUT -- to avoid
173 hoisting values up too far;
175 5. There are at least two successors of B -- hoisting in straight
176 line code is pointless.
178 The third condition is not strictly necessary, but it would complicate
179 the hoisting pass a lot. In fact, I don't know of any code hoisting
180 algorithm that does not have this requirement. Fortunately, experiments
181 have show that most candidate hoistable values are in regions that meet
182 this condition (e.g. diamond-shape regions).
184 The forth condition is necessary to avoid hoisting things up too far
185 away from the uses of the value. Nothing else limits the algorithm
186 from hoisting everything up as far as ANTIC_IN allows. Experiments
187 with SPEC and CSiBE have shown that hoisting up too far results in more
188 spilling, less benefits for code size, and worse benchmark scores.
189 Fortunately, in practice most of the interesting hoisting opportunities
190 are caught despite this limitation.
192 For hoistable values that meet all conditions, expressions are inserted
193 to make the calculation of the hoistable value fully redundant. We
194 perform code hoisting insertions after each round of PRE insertions,
195 because code hoisting never exposes new PRE opportunities, but PRE can
196 create new code hoisting opportunities.
198 The code hoisting algorithm is implemented in do_hoist_insert, driven
199 by insert/insert_aux. */
201 /* Representations of value numbers:
203 Value numbers are represented by a representative SSA_NAME. We
204 will create fake SSA_NAME's in situations where we need a
205 representative but do not have one (because it is a complex
206 expression). In order to facilitate storing the value numbers in
207 bitmaps, and keep the number of wasted SSA_NAME's down, we also
208 associate a value_id with each value number, and create full blown
209 ssa_name's only where we actually need them (IE in operands of
210 existing expressions).
212 Theoretically you could replace all the value_id's with
213 SSA_NAME_VERSION, but this would allocate a large number of
214 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
215 It would also require an additional indirection at each point we
216 use the value id. */
218 /* Representation of expressions on value numbers:
220 Expressions consisting of value numbers are represented the same
221 way as our VN internally represents them, with an additional
222 "pre_expr" wrapping around them in order to facilitate storing all
223 of the expressions in the same sets. */
225 /* Representation of sets:
227 The dataflow sets do not need to be sorted in any particular order
228 for the majority of their lifetime, are simply represented as two
229 bitmaps, one that keeps track of values present in the set, and one
230 that keeps track of expressions present in the set.
232 When we need them in topological order, we produce it on demand by
233 transforming the bitmap into an array and sorting it into topo
234 order. */
236 /* Type of expression, used to know which member of the PRE_EXPR union
237 is valid. */
239 enum pre_expr_kind
241 NAME,
242 NARY,
243 REFERENCE,
244 CONSTANT
247 union pre_expr_union
249 tree name;
250 tree constant;
251 vn_nary_op_t nary;
252 vn_reference_t reference;
255 typedef struct pre_expr_d : nofree_ptr_hash <pre_expr_d>
257 enum pre_expr_kind kind;
258 unsigned int id;
259 location_t loc;
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 result->loc = UNKNOWN_LOCATION;
425 PRE_EXPR_NAME (result) = name;
426 alloc_expression_id (result);
427 return result;
430 /* An unordered bitmap set. One bitmap tracks values, the other,
431 expressions. */
432 typedef class bitmap_set
434 public:
435 bitmap_head expressions;
436 bitmap_head values;
437 } *bitmap_set_t;
439 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
440 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
442 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
443 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
445 /* Mapping from value id to expressions with that value_id. */
446 static vec<bitmap> value_expressions;
448 /* Sets that we need to keep track of. */
449 typedef struct bb_bitmap_sets
451 /* The EXP_GEN set, which represents expressions/values generated in
452 a basic block. */
453 bitmap_set_t exp_gen;
455 /* The PHI_GEN set, which represents PHI results generated in a
456 basic block. */
457 bitmap_set_t phi_gen;
459 /* The TMP_GEN set, which represents results/temporaries generated
460 in a basic block. IE the LHS of an expression. */
461 bitmap_set_t tmp_gen;
463 /* The AVAIL_OUT set, which represents which values are available in
464 a given basic block. */
465 bitmap_set_t avail_out;
467 /* The ANTIC_IN set, which represents which values are anticipatable
468 in a given basic block. */
469 bitmap_set_t antic_in;
471 /* The PA_IN set, which represents which values are
472 partially anticipatable in a given basic block. */
473 bitmap_set_t pa_in;
475 /* The NEW_SETS set, which is used during insertion to augment the
476 AVAIL_OUT set of blocks with the new insertions performed during
477 the current iteration. */
478 bitmap_set_t new_sets;
480 /* A cache for value_dies_in_block_x. */
481 bitmap expr_dies;
483 /* The live virtual operand on successor edges. */
484 tree vop_on_exit;
486 /* True if we have visited this block during ANTIC calculation. */
487 unsigned int visited : 1;
489 /* True when the block contains a call that might not return. */
490 unsigned int contains_may_not_return_call : 1;
491 } *bb_value_sets_t;
493 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
494 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
495 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
496 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
497 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
498 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
499 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
500 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
501 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
502 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
503 #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
506 /* This structure is used to keep track of statistics on what
507 optimization PRE was able to perform. */
508 static struct
510 /* The number of new expressions/temporaries generated by PRE. */
511 int insertions;
513 /* The number of inserts found due to partial anticipation */
514 int pa_insert;
516 /* The number of inserts made for code hoisting. */
517 int hoist_insert;
519 /* The number of new PHI nodes added by PRE. */
520 int phis;
521 } pre_stats;
523 static bool do_partial_partial;
524 static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
525 static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
526 static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
527 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
528 static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
529 static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
530 static bitmap_set_t bitmap_set_new (void);
531 static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
532 tree);
533 static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
534 static unsigned int get_expr_value_id (pre_expr);
536 /* We can add and remove elements and entries to and from sets
537 and hash tables, so we use alloc pools for them. */
539 static object_allocator<bitmap_set> bitmap_set_pool ("Bitmap sets");
540 static bitmap_obstack grand_bitmap_obstack;
542 /* A three tuple {e, pred, v} used to cache phi translations in the
543 phi_translate_table. */
545 typedef struct expr_pred_trans_d : free_ptr_hash<expr_pred_trans_d>
547 /* The expression. */
548 pre_expr e;
550 /* The predecessor block along which we translated the expression. */
551 basic_block pred;
553 /* The value that resulted from the translation. */
554 pre_expr v;
556 /* The hashcode for the expression, pred pair. This is cached for
557 speed reasons. */
558 hashval_t hashcode;
560 /* hash_table support. */
561 static inline hashval_t hash (const expr_pred_trans_d *);
562 static inline int equal (const expr_pred_trans_d *, const expr_pred_trans_d *);
563 } *expr_pred_trans_t;
564 typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
566 inline hashval_t
567 expr_pred_trans_d::hash (const expr_pred_trans_d *e)
569 return e->hashcode;
572 inline int
573 expr_pred_trans_d::equal (const expr_pred_trans_d *ve1,
574 const expr_pred_trans_d *ve2)
576 basic_block b1 = ve1->pred;
577 basic_block b2 = ve2->pred;
579 /* If they are not translations for the same basic block, they can't
580 be equal. */
581 if (b1 != b2)
582 return false;
583 return pre_expr_d::equal (ve1->e, ve2->e);
586 /* The phi_translate_table caches phi translations for a given
587 expression and predecessor. */
588 static hash_table<expr_pred_trans_d> *phi_translate_table;
590 /* Add the tuple mapping from {expression E, basic block PRED} to
591 the phi translation table and return whether it pre-existed. */
593 static inline bool
594 phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
596 expr_pred_trans_t *slot;
597 expr_pred_trans_d tem;
598 hashval_t hash = iterative_hash_hashval_t (pre_expr_d::hash (e),
599 pred->index);
600 tem.e = e;
601 tem.pred = pred;
602 tem.hashcode = hash;
603 slot = phi_translate_table->find_slot_with_hash (&tem, hash, INSERT);
604 if (*slot)
606 *entry = *slot;
607 return true;
610 *entry = *slot = XNEW (struct expr_pred_trans_d);
611 (*entry)->e = e;
612 (*entry)->pred = pred;
613 (*entry)->hashcode = hash;
614 return false;
618 /* Add expression E to the expression set of value id V. */
620 static void
621 add_to_value (unsigned int v, pre_expr e)
623 bitmap set;
625 gcc_checking_assert (get_expr_value_id (e) == v);
627 if (v >= value_expressions.length ())
629 value_expressions.safe_grow_cleared (v + 1);
632 set = value_expressions[v];
633 if (!set)
635 set = BITMAP_ALLOC (&grand_bitmap_obstack);
636 value_expressions[v] = set;
639 bitmap_set_bit (set, get_or_alloc_expression_id (e));
642 /* Create a new bitmap set and return it. */
644 static bitmap_set_t
645 bitmap_set_new (void)
647 bitmap_set_t ret = bitmap_set_pool.allocate ();
648 bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
649 bitmap_initialize (&ret->values, &grand_bitmap_obstack);
650 return ret;
653 /* Return the value id for a PRE expression EXPR. */
655 static unsigned int
656 get_expr_value_id (pre_expr expr)
658 unsigned int id;
659 switch (expr->kind)
661 case CONSTANT:
662 id = get_constant_value_id (PRE_EXPR_CONSTANT (expr));
663 break;
664 case NAME:
665 id = VN_INFO (PRE_EXPR_NAME (expr))->value_id;
666 break;
667 case NARY:
668 gcc_assert (!PRE_EXPR_NARY (expr)->predicated_values);
669 id = PRE_EXPR_NARY (expr)->value_id;
670 break;
671 case REFERENCE:
672 id = PRE_EXPR_REFERENCE (expr)->value_id;
673 break;
674 default:
675 gcc_unreachable ();
677 /* ??? We cannot assert that expr has a value-id (it can be 0), because
678 we assign value-ids only to expressions that have a result
679 in set_hashtable_value_ids. */
680 return id;
683 /* Return a VN valnum (SSA name or constant) for the PRE value-id VAL. */
685 static tree
686 vn_valnum_from_value_id (unsigned int val)
688 bitmap_iterator bi;
689 unsigned int i;
690 bitmap exprset = value_expressions[val];
691 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
693 pre_expr vexpr = expression_for_id (i);
694 if (vexpr->kind == NAME)
695 return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
696 else if (vexpr->kind == CONSTANT)
697 return PRE_EXPR_CONSTANT (vexpr);
699 return NULL_TREE;
702 /* Insert an expression EXPR into a bitmapped set. */
704 static void
705 bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
707 unsigned int val = get_expr_value_id (expr);
708 if (! value_id_constant_p (val))
710 /* Note this is the only function causing multiple expressions
711 for the same value to appear in a set. This is needed for
712 TMP_GEN, PHI_GEN and NEW_SETs. */
713 bitmap_set_bit (&set->values, val);
714 bitmap_set_bit (&set->expressions, get_or_alloc_expression_id (expr));
718 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
720 static void
721 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
723 bitmap_copy (&dest->expressions, &orig->expressions);
724 bitmap_copy (&dest->values, &orig->values);
728 /* Free memory used up by SET. */
729 static void
730 bitmap_set_free (bitmap_set_t set)
732 bitmap_clear (&set->expressions);
733 bitmap_clear (&set->values);
737 /* Generate an topological-ordered array of bitmap set SET. */
739 static vec<pre_expr>
740 sorted_array_from_bitmap_set (bitmap_set_t set)
742 unsigned int i, j;
743 bitmap_iterator bi, bj;
744 vec<pre_expr> result;
746 /* Pre-allocate enough space for the array. */
747 result.create (bitmap_count_bits (&set->expressions));
749 FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
751 /* The number of expressions having a given value is usually
752 relatively small. Thus, rather than making a vector of all
753 the expressions and sorting it by value-id, we walk the values
754 and check in the reverse mapping that tells us what expressions
755 have a given value, to filter those in our set. As a result,
756 the expressions are inserted in value-id order, which means
757 topological order.
759 If this is somehow a significant lose for some cases, we can
760 choose which set to walk based on the set size. */
761 bitmap exprset = value_expressions[i];
762 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj)
764 if (bitmap_bit_p (&set->expressions, j))
765 result.quick_push (expression_for_id (j));
769 return result;
772 /* Subtract all expressions contained in ORIG from DEST. */
774 static bitmap_set_t
775 bitmap_set_subtract_expressions (bitmap_set_t dest, bitmap_set_t orig)
777 bitmap_set_t result = bitmap_set_new ();
778 bitmap_iterator bi;
779 unsigned int i;
781 bitmap_and_compl (&result->expressions, &dest->expressions,
782 &orig->expressions);
784 FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
786 pre_expr expr = expression_for_id (i);
787 unsigned int value_id = get_expr_value_id (expr);
788 bitmap_set_bit (&result->values, value_id);
791 return result;
794 /* Subtract all values in bitmap set B from bitmap set A. */
796 static void
797 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
799 unsigned int i;
800 bitmap_iterator bi;
801 unsigned to_remove = -1U;
802 bitmap_and_compl_into (&a->values, &b->values);
803 FOR_EACH_EXPR_ID_IN_SET (a, i, bi)
805 if (to_remove != -1U)
807 bitmap_clear_bit (&a->expressions, to_remove);
808 to_remove = -1U;
810 pre_expr expr = expression_for_id (i);
811 if (! bitmap_bit_p (&a->values, get_expr_value_id (expr)))
812 to_remove = i;
814 if (to_remove != -1U)
815 bitmap_clear_bit (&a->expressions, to_remove);
819 /* Return true if bitmapped set SET contains the value VALUE_ID. */
821 static bool
822 bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
824 if (value_id_constant_p (value_id))
825 return true;
827 return bitmap_bit_p (&set->values, value_id);
830 /* Return true if two bitmap sets are equal. */
832 static bool
833 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
835 return bitmap_equal_p (&a->values, &b->values);
838 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
839 and add it otherwise. */
841 static void
842 bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
844 unsigned int val = get_expr_value_id (expr);
845 if (value_id_constant_p (val))
846 return;
848 if (bitmap_set_contains_value (set, val))
850 /* The number of expressions having a given value is usually
851 significantly less than the total number of expressions in SET.
852 Thus, rather than check, for each expression in SET, whether it
853 has the value LOOKFOR, we walk the reverse mapping that tells us
854 what expressions have a given value, and see if any of those
855 expressions are in our set. For large testcases, this is about
856 5-10x faster than walking the bitmap. If this is somehow a
857 significant lose for some cases, we can choose which set to walk
858 based on the set size. */
859 unsigned int i;
860 bitmap_iterator bi;
861 bitmap exprset = value_expressions[val];
862 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
864 if (bitmap_clear_bit (&set->expressions, i))
866 bitmap_set_bit (&set->expressions, get_expression_id (expr));
867 return;
870 gcc_unreachable ();
872 else
873 bitmap_insert_into_set (set, expr);
876 /* Insert EXPR into SET if EXPR's value is not already present in
877 SET. */
879 static void
880 bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
882 unsigned int val = get_expr_value_id (expr);
884 gcc_checking_assert (expr->id == get_or_alloc_expression_id (expr));
886 /* Constant values are always considered to be part of the set. */
887 if (value_id_constant_p (val))
888 return;
890 /* If the value membership changed, add the expression. */
891 if (bitmap_set_bit (&set->values, val))
892 bitmap_set_bit (&set->expressions, expr->id);
895 /* Print out EXPR to outfile. */
897 static void
898 print_pre_expr (FILE *outfile, const pre_expr expr)
900 if (! expr)
902 fprintf (outfile, "NULL");
903 return;
905 switch (expr->kind)
907 case CONSTANT:
908 print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr));
909 break;
910 case NAME:
911 print_generic_expr (outfile, PRE_EXPR_NAME (expr));
912 break;
913 case NARY:
915 unsigned int i;
916 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
917 fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
918 for (i = 0; i < nary->length; i++)
920 print_generic_expr (outfile, nary->op[i]);
921 if (i != (unsigned) nary->length - 1)
922 fprintf (outfile, ",");
924 fprintf (outfile, "}");
926 break;
928 case REFERENCE:
930 vn_reference_op_t vro;
931 unsigned int i;
932 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
933 fprintf (outfile, "{");
934 for (i = 0;
935 ref->operands.iterate (i, &vro);
936 i++)
938 bool closebrace = false;
939 if (vro->opcode != SSA_NAME
940 && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
942 fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
943 if (vro->op0)
945 fprintf (outfile, "<");
946 closebrace = true;
949 if (vro->op0)
951 print_generic_expr (outfile, vro->op0);
952 if (vro->op1)
954 fprintf (outfile, ",");
955 print_generic_expr (outfile, vro->op1);
957 if (vro->op2)
959 fprintf (outfile, ",");
960 print_generic_expr (outfile, vro->op2);
963 if (closebrace)
964 fprintf (outfile, ">");
965 if (i != ref->operands.length () - 1)
966 fprintf (outfile, ",");
968 fprintf (outfile, "}");
969 if (ref->vuse)
971 fprintf (outfile, "@");
972 print_generic_expr (outfile, ref->vuse);
975 break;
978 void debug_pre_expr (pre_expr);
980 /* Like print_pre_expr but always prints to stderr. */
981 DEBUG_FUNCTION void
982 debug_pre_expr (pre_expr e)
984 print_pre_expr (stderr, e);
985 fprintf (stderr, "\n");
988 /* Print out SET to OUTFILE. */
990 static void
991 print_bitmap_set (FILE *outfile, bitmap_set_t set,
992 const char *setname, int blockindex)
994 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
995 if (set)
997 bool first = true;
998 unsigned i;
999 bitmap_iterator bi;
1001 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1003 const pre_expr expr = expression_for_id (i);
1005 if (!first)
1006 fprintf (outfile, ", ");
1007 first = false;
1008 print_pre_expr (outfile, expr);
1010 fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1013 fprintf (outfile, " }\n");
1016 void debug_bitmap_set (bitmap_set_t);
1018 DEBUG_FUNCTION void
1019 debug_bitmap_set (bitmap_set_t set)
1021 print_bitmap_set (stderr, set, "debug", 0);
1024 void debug_bitmap_sets_for (basic_block);
1026 DEBUG_FUNCTION void
1027 debug_bitmap_sets_for (basic_block bb)
1029 print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1030 print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1031 print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1032 print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1033 print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1034 if (do_partial_partial)
1035 print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1036 print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1039 /* Print out the expressions that have VAL to OUTFILE. */
1041 static void
1042 print_value_expressions (FILE *outfile, unsigned int val)
1044 bitmap set = value_expressions[val];
1045 if (set)
1047 bitmap_set x;
1048 char s[10];
1049 sprintf (s, "%04d", val);
1050 x.expressions = *set;
1051 print_bitmap_set (outfile, &x, s, 0);
1056 DEBUG_FUNCTION void
1057 debug_value_expressions (unsigned int val)
1059 print_value_expressions (stderr, val);
1062 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1063 represent it. */
1065 static pre_expr
1066 get_or_alloc_expr_for_constant (tree constant)
1068 unsigned int result_id;
1069 unsigned int value_id;
1070 struct pre_expr_d expr;
1071 pre_expr newexpr;
1073 expr.kind = CONSTANT;
1074 PRE_EXPR_CONSTANT (&expr) = constant;
1075 result_id = lookup_expression_id (&expr);
1076 if (result_id != 0)
1077 return expression_for_id (result_id);
1079 newexpr = pre_expr_pool.allocate ();
1080 newexpr->kind = CONSTANT;
1081 newexpr->loc = UNKNOWN_LOCATION;
1082 PRE_EXPR_CONSTANT (newexpr) = constant;
1083 alloc_expression_id (newexpr);
1084 value_id = get_or_alloc_constant_value_id (constant);
1085 add_to_value (value_id, newexpr);
1086 return newexpr;
1089 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1090 Currently only supports constants and SSA_NAMES. */
1091 static pre_expr
1092 get_or_alloc_expr_for (tree t)
1094 if (TREE_CODE (t) == SSA_NAME)
1095 return get_or_alloc_expr_for_name (t);
1096 else if (is_gimple_min_invariant (t))
1097 return get_or_alloc_expr_for_constant (t);
1098 gcc_unreachable ();
1101 /* Return the folded version of T if T, when folded, is a gimple
1102 min_invariant or an SSA name. Otherwise, return T. */
1104 static pre_expr
1105 fully_constant_expression (pre_expr e)
1107 switch (e->kind)
1109 case CONSTANT:
1110 return e;
1111 case NARY:
1113 vn_nary_op_t nary = PRE_EXPR_NARY (e);
1114 tree res = vn_nary_simplify (nary);
1115 if (!res)
1116 return e;
1117 if (is_gimple_min_invariant (res))
1118 return get_or_alloc_expr_for_constant (res);
1119 if (TREE_CODE (res) == SSA_NAME)
1120 return get_or_alloc_expr_for_name (res);
1121 return e;
1123 case REFERENCE:
1125 vn_reference_t ref = PRE_EXPR_REFERENCE (e);
1126 tree folded;
1127 if ((folded = fully_constant_vn_reference_p (ref)))
1128 return get_or_alloc_expr_for_constant (folded);
1129 return e;
1131 default:
1132 return e;
1134 return e;
1137 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1138 it has the value it would have in BLOCK. Set *SAME_VALID to true
1139 in case the new vuse doesn't change the value id of the OPERANDS. */
1141 static tree
1142 translate_vuse_through_block (vec<vn_reference_op_s> operands,
1143 alias_set_type set, tree type, tree vuse,
1144 basic_block phiblock,
1145 basic_block block, bool *same_valid)
1147 gimple *phi = SSA_NAME_DEF_STMT (vuse);
1148 ao_ref ref;
1149 edge e = NULL;
1150 bool use_oracle;
1152 if (same_valid)
1153 *same_valid = true;
1155 if (gimple_bb (phi) != phiblock)
1156 return vuse;
1158 unsigned int cnt = param_sccvn_max_alias_queries_per_access;
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 (cnt > 0
1168 && !stmt_may_clobber_ref_p_1 (phi, &ref))
1170 --cnt;
1171 vuse = gimple_vuse (phi);
1172 phi = SSA_NAME_DEF_STMT (vuse);
1173 if (gimple_bb (phi) != phiblock)
1174 return vuse;
1175 if (gimple_code (phi) == GIMPLE_PHI)
1177 e = find_edge (block, phiblock);
1178 break;
1181 else
1182 return NULL_TREE;
1184 if (e)
1186 if (use_oracle && same_valid)
1188 bitmap visited = NULL;
1189 /* Try to find a vuse that dominates this phi node by skipping
1190 non-clobbering statements. */
1191 vuse = get_continuation_for_phi (phi, &ref, true,
1192 cnt, &visited, false, NULL, NULL);
1193 if (visited)
1194 BITMAP_FREE (visited);
1196 else
1197 vuse = NULL_TREE;
1198 /* If we didn't find any, the value ID can't stay the same. */
1199 if (!vuse && same_valid)
1200 *same_valid = false;
1201 /* ??? We would like to return vuse here as this is the canonical
1202 upmost vdef that this reference is associated with. But during
1203 insertion of the references into the hash tables we only ever
1204 directly insert with their direct gimple_vuse, hence returning
1205 something else would make us not find the other expression. */
1206 return PHI_ARG_DEF (phi, e->dest_idx);
1209 return NULL_TREE;
1212 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1213 SET2 *or* SET3. This is used to avoid making a set consisting of the union
1214 of PA_IN and ANTIC_IN during insert and phi-translation. */
1216 static inline pre_expr
1217 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
1218 bitmap_set_t set3 = NULL)
1220 pre_expr result = NULL;
1222 if (set1)
1223 result = bitmap_find_leader (set1, val);
1224 if (!result && set2)
1225 result = bitmap_find_leader (set2, val);
1226 if (!result && set3)
1227 result = bitmap_find_leader (set3, val);
1228 return result;
1231 /* Get the tree type for our PRE expression e. */
1233 static tree
1234 get_expr_type (const pre_expr e)
1236 switch (e->kind)
1238 case NAME:
1239 return TREE_TYPE (PRE_EXPR_NAME (e));
1240 case CONSTANT:
1241 return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1242 case REFERENCE:
1243 return PRE_EXPR_REFERENCE (e)->type;
1244 case NARY:
1245 return PRE_EXPR_NARY (e)->type;
1247 gcc_unreachable ();
1250 /* Get a representative SSA_NAME for a given expression that is available in B.
1251 Since all of our sub-expressions are treated as values, we require
1252 them to be SSA_NAME's for simplicity.
1253 Prior versions of GVNPRE used to use "value handles" here, so that
1254 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1255 either case, the operands are really values (IE we do not expect
1256 them to be usable without finding leaders). */
1258 static tree
1259 get_representative_for (const pre_expr e, basic_block b = NULL)
1261 tree name, valnum = NULL_TREE;
1262 unsigned int value_id = get_expr_value_id (e);
1264 switch (e->kind)
1266 case NAME:
1267 return PRE_EXPR_NAME (e);
1268 case CONSTANT:
1269 return PRE_EXPR_CONSTANT (e);
1270 case NARY:
1271 case REFERENCE:
1273 /* Go through all of the expressions representing this value
1274 and pick out an SSA_NAME. */
1275 unsigned int i;
1276 bitmap_iterator bi;
1277 bitmap exprs = value_expressions[value_id];
1278 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1280 pre_expr rep = expression_for_id (i);
1281 if (rep->kind == NAME)
1283 tree name = PRE_EXPR_NAME (rep);
1284 valnum = VN_INFO (name)->valnum;
1285 gimple *def = SSA_NAME_DEF_STMT (name);
1286 /* We have to return either a new representative or one
1287 that can be used for expression simplification and thus
1288 is available in B. */
1289 if (! b
1290 || gimple_nop_p (def)
1291 || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
1292 return name;
1294 else if (rep->kind == CONSTANT)
1295 return PRE_EXPR_CONSTANT (rep);
1298 break;
1301 /* If we reached here we couldn't find an SSA_NAME. This can
1302 happen when we've discovered a value that has never appeared in
1303 the program as set to an SSA_NAME, as the result of phi translation.
1304 Create one here.
1305 ??? We should be able to re-use this when we insert the statement
1306 to compute it. */
1307 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1308 VN_INFO (name)->value_id = value_id;
1309 VN_INFO (name)->valnum = valnum ? valnum : name;
1310 /* ??? For now mark this SSA name for release by VN. */
1311 VN_INFO (name)->needs_insertion = true;
1312 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1313 if (dump_file && (dump_flags & TDF_DETAILS))
1315 fprintf (dump_file, "Created SSA_NAME representative ");
1316 print_generic_expr (dump_file, name);
1317 fprintf (dump_file, " for expression:");
1318 print_pre_expr (dump_file, e);
1319 fprintf (dump_file, " (%04d)\n", value_id);
1322 return name;
1326 static pre_expr
1327 phi_translate (bitmap_set_t, pre_expr, bitmap_set_t, bitmap_set_t, edge);
1329 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1330 the phis in PRED. Return NULL if we can't find a leader for each part
1331 of the translated expression. */
1333 static pre_expr
1334 phi_translate_1 (bitmap_set_t dest,
1335 pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
1337 basic_block pred = e->src;
1338 basic_block phiblock = e->dest;
1339 location_t expr_loc = expr->loc;
1340 switch (expr->kind)
1342 case NARY:
1344 unsigned int i;
1345 bool changed = false;
1346 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1347 vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1348 sizeof_vn_nary_op (nary->length));
1349 memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1351 for (i = 0; i < newnary->length; i++)
1353 if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1354 continue;
1355 else
1357 pre_expr leader, result;
1358 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1359 leader = find_leader_in_sets (op_val_id, set1, set2);
1360 result = phi_translate (dest, leader, set1, set2, e);
1361 if (result && result != leader)
1362 /* If op has a leader in the sets we translate make
1363 sure to use the value of the translated expression.
1364 We might need a new representative for that. */
1365 newnary->op[i] = get_representative_for (result, pred);
1366 else if (!result)
1367 return NULL;
1369 changed |= newnary->op[i] != nary->op[i];
1372 if (changed)
1374 pre_expr constant;
1375 unsigned int new_val_id;
1377 PRE_EXPR_NARY (expr) = newnary;
1378 constant = fully_constant_expression (expr);
1379 PRE_EXPR_NARY (expr) = nary;
1380 if (constant != expr)
1382 /* For non-CONSTANTs we have to make sure we can eventually
1383 insert the expression. Which means we need to have a
1384 leader for it. */
1385 if (constant->kind != CONSTANT)
1387 /* Do not allow simplifications to non-constants over
1388 backedges as this will likely result in a loop PHI node
1389 to be inserted and increased register pressure.
1390 See PR77498 - this avoids doing predcoms work in
1391 a less efficient way. */
1392 if (e->flags & EDGE_DFS_BACK)
1394 else
1396 unsigned value_id = get_expr_value_id (constant);
1397 /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1398 dest has what we computed into ANTIC_OUT sofar
1399 so pick from that - since topological sorting
1400 by sorted_array_from_bitmap_set isn't perfect
1401 we may lose some cases here. */
1402 constant = find_leader_in_sets (value_id, dest,
1403 AVAIL_OUT (pred));
1404 if (constant)
1406 if (dump_file && (dump_flags & TDF_DETAILS))
1408 fprintf (dump_file, "simplifying ");
1409 print_pre_expr (dump_file, expr);
1410 fprintf (dump_file, " translated %d -> %d to ",
1411 phiblock->index, pred->index);
1412 PRE_EXPR_NARY (expr) = newnary;
1413 print_pre_expr (dump_file, expr);
1414 PRE_EXPR_NARY (expr) = nary;
1415 fprintf (dump_file, " to ");
1416 print_pre_expr (dump_file, constant);
1417 fprintf (dump_file, "\n");
1419 return constant;
1423 else
1424 return constant;
1427 /* vn_nary_* do not valueize operands. */
1428 for (i = 0; i < newnary->length; ++i)
1429 if (TREE_CODE (newnary->op[i]) == SSA_NAME)
1430 newnary->op[i] = VN_INFO (newnary->op[i])->valnum;
1431 tree result = vn_nary_op_lookup_pieces (newnary->length,
1432 newnary->opcode,
1433 newnary->type,
1434 &newnary->op[0],
1435 &nary);
1436 if (result && is_gimple_min_invariant (result))
1437 return get_or_alloc_expr_for_constant (result);
1439 expr = pre_expr_pool.allocate ();
1440 expr->kind = NARY;
1441 expr->id = 0;
1442 expr->loc = expr_loc;
1443 if (nary && !nary->predicated_values)
1445 PRE_EXPR_NARY (expr) = nary;
1446 new_val_id = nary->value_id;
1447 get_or_alloc_expression_id (expr);
1449 else
1451 new_val_id = get_next_value_id ();
1452 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
1453 nary = vn_nary_op_insert_pieces (newnary->length,
1454 newnary->opcode,
1455 newnary->type,
1456 &newnary->op[0],
1457 result, new_val_id);
1458 PRE_EXPR_NARY (expr) = nary;
1459 get_or_alloc_expression_id (expr);
1461 add_to_value (new_val_id, expr);
1463 return expr;
1465 break;
1467 case REFERENCE:
1469 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1470 vec<vn_reference_op_s> operands = ref->operands;
1471 tree vuse = ref->vuse;
1472 tree newvuse = vuse;
1473 vec<vn_reference_op_s> newoperands = vNULL;
1474 bool changed = false, same_valid = true;
1475 unsigned int i, n;
1476 vn_reference_op_t operand;
1477 vn_reference_t newref;
1479 for (i = 0; operands.iterate (i, &operand); i++)
1481 pre_expr opresult;
1482 pre_expr leader;
1483 tree op[3];
1484 tree type = operand->type;
1485 vn_reference_op_s newop = *operand;
1486 op[0] = operand->op0;
1487 op[1] = operand->op1;
1488 op[2] = operand->op2;
1489 for (n = 0; n < 3; ++n)
1491 unsigned int op_val_id;
1492 if (!op[n])
1493 continue;
1494 if (TREE_CODE (op[n]) != SSA_NAME)
1496 /* We can't possibly insert these. */
1497 if (n != 0
1498 && !is_gimple_min_invariant (op[n]))
1499 break;
1500 continue;
1502 op_val_id = VN_INFO (op[n])->value_id;
1503 leader = find_leader_in_sets (op_val_id, set1, set2);
1504 opresult = phi_translate (dest, leader, set1, set2, e);
1505 if (opresult && opresult != leader)
1507 tree name = get_representative_for (opresult);
1508 changed |= name != op[n];
1509 op[n] = name;
1511 else if (!opresult)
1512 break;
1514 if (n != 3)
1516 newoperands.release ();
1517 return NULL;
1519 if (!changed)
1520 continue;
1521 if (!newoperands.exists ())
1522 newoperands = operands.copy ();
1523 /* We may have changed from an SSA_NAME to a constant */
1524 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1525 newop.opcode = TREE_CODE (op[0]);
1526 newop.type = type;
1527 newop.op0 = op[0];
1528 newop.op1 = op[1];
1529 newop.op2 = op[2];
1530 newoperands[i] = newop;
1532 gcc_checking_assert (i == operands.length ());
1534 if (vuse)
1536 newvuse = translate_vuse_through_block (newoperands.exists ()
1537 ? newoperands : operands,
1538 ref->set, ref->type,
1539 vuse, phiblock, pred,
1540 changed
1541 ? NULL : &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;
1594 expr->loc = expr_loc;
1596 if (newref)
1597 new_val_id = newref->value_id;
1598 else
1600 if (changed || !same_valid)
1602 new_val_id = get_next_value_id ();
1603 value_expressions.safe_grow_cleared
1604 (get_max_value_id () + 1);
1606 else
1607 new_val_id = ref->value_id;
1608 if (!newoperands.exists ())
1609 newoperands = operands.copy ();
1610 newref = vn_reference_insert_pieces (newvuse, ref->set,
1611 ref->type,
1612 newoperands,
1613 result, new_val_id);
1614 newoperands = vNULL;
1616 PRE_EXPR_REFERENCE (expr) = newref;
1617 get_or_alloc_expression_id (expr);
1618 add_to_value (new_val_id, expr);
1620 newoperands.release ();
1621 return expr;
1623 break;
1625 case NAME:
1627 tree name = PRE_EXPR_NAME (expr);
1628 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1629 /* If the SSA name is defined by a PHI node in this block,
1630 translate it. */
1631 if (gimple_code (def_stmt) == GIMPLE_PHI
1632 && gimple_bb (def_stmt) == phiblock)
1634 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1636 /* Handle constant. */
1637 if (is_gimple_min_invariant (def))
1638 return get_or_alloc_expr_for_constant (def);
1640 return get_or_alloc_expr_for_name (def);
1642 /* Otherwise return it unchanged - it will get removed if its
1643 value is not available in PREDs AVAIL_OUT set of expressions
1644 by the subtraction of TMP_GEN. */
1645 return expr;
1648 default:
1649 gcc_unreachable ();
1653 /* Wrapper around phi_translate_1 providing caching functionality. */
1655 static pre_expr
1656 phi_translate (bitmap_set_t dest, pre_expr expr,
1657 bitmap_set_t set1, bitmap_set_t set2, edge e)
1659 expr_pred_trans_t slot = NULL;
1660 pre_expr phitrans;
1662 if (!expr)
1663 return NULL;
1665 /* Constants contain no values that need translation. */
1666 if (expr->kind == CONSTANT)
1667 return expr;
1669 if (value_id_constant_p (get_expr_value_id (expr)))
1670 return expr;
1672 /* Don't add translations of NAMEs as those are cheap to translate. */
1673 if (expr->kind != NAME)
1675 if (phi_trans_add (&slot, expr, e->src))
1676 return slot->v;
1677 /* Store NULL for the value we want to return in the case of
1678 recursing. */
1679 slot->v = NULL;
1682 /* Translate. */
1683 basic_block saved_valueize_bb = vn_context_bb;
1684 vn_context_bb = e->src;
1685 phitrans = phi_translate_1 (dest, expr, set1, set2, e);
1686 vn_context_bb = saved_valueize_bb;
1688 if (slot)
1690 if (phitrans)
1691 slot->v = phitrans;
1692 else
1693 /* Remove failed translations again, they cause insert
1694 iteration to not pick up new opportunities reliably. */
1695 phi_translate_table->remove_elt_with_hash (slot, slot->hashcode);
1698 return phitrans;
1702 /* For each expression in SET, translate the values through phi nodes
1703 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1704 expressions in DEST. */
1706 static void
1707 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
1709 vec<pre_expr> exprs;
1710 pre_expr expr;
1711 int i;
1713 if (gimple_seq_empty_p (phi_nodes (e->dest)))
1715 bitmap_set_copy (dest, set);
1716 return;
1719 exprs = sorted_array_from_bitmap_set (set);
1720 FOR_EACH_VEC_ELT (exprs, i, expr)
1722 pre_expr translated;
1723 translated = phi_translate (dest, expr, set, NULL, e);
1724 if (!translated)
1725 continue;
1727 bitmap_insert_into_set (dest, translated);
1729 exprs.release ();
1732 /* Find the leader for a value (i.e., the name representing that
1733 value) in a given set, and return it. Return NULL if no leader
1734 is found. */
1736 static pre_expr
1737 bitmap_find_leader (bitmap_set_t set, unsigned int val)
1739 if (value_id_constant_p (val))
1741 unsigned int i;
1742 bitmap_iterator bi;
1743 bitmap exprset = value_expressions[val];
1745 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1747 pre_expr expr = expression_for_id (i);
1748 if (expr->kind == CONSTANT)
1749 return expr;
1752 if (bitmap_set_contains_value (set, val))
1754 /* Rather than walk the entire bitmap of expressions, and see
1755 whether any of them has the value we are looking for, we look
1756 at the reverse mapping, which tells us the set of expressions
1757 that have a given value (IE value->expressions with that
1758 value) and see if any of those expressions are in our set.
1759 The number of expressions per value is usually significantly
1760 less than the number of expressions in the set. In fact, for
1761 large testcases, doing it this way is roughly 5-10x faster
1762 than walking the bitmap.
1763 If this is somehow a significant lose for some cases, we can
1764 choose which set to walk based on which set is smaller. */
1765 unsigned int i;
1766 bitmap_iterator bi;
1767 bitmap exprset = value_expressions[val];
1769 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1770 return expression_for_id (i);
1772 return NULL;
1775 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1776 BLOCK by seeing if it is not killed in the block. Note that we are
1777 only determining whether there is a store that kills it. Because
1778 of the order in which clean iterates over values, we are guaranteed
1779 that altered operands will have caused us to be eliminated from the
1780 ANTIC_IN set already. */
1782 static bool
1783 value_dies_in_block_x (pre_expr expr, basic_block block)
1785 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1786 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1787 gimple *def;
1788 gimple_stmt_iterator gsi;
1789 unsigned id = get_expression_id (expr);
1790 bool res = false;
1791 ao_ref ref;
1793 if (!vuse)
1794 return false;
1796 /* Lookup a previously calculated result. */
1797 if (EXPR_DIES (block)
1798 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1799 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1801 /* A memory expression {e, VUSE} dies in the block if there is a
1802 statement that may clobber e. If, starting statement walk from the
1803 top of the basic block, a statement uses VUSE there can be no kill
1804 inbetween that use and the original statement that loaded {e, VUSE},
1805 so we can stop walking. */
1806 ref.base = NULL_TREE;
1807 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1809 tree def_vuse, def_vdef;
1810 def = gsi_stmt (gsi);
1811 def_vuse = gimple_vuse (def);
1812 def_vdef = gimple_vdef (def);
1814 /* Not a memory statement. */
1815 if (!def_vuse)
1816 continue;
1818 /* Not a may-def. */
1819 if (!def_vdef)
1821 /* A load with the same VUSE, we're done. */
1822 if (def_vuse == vuse)
1823 break;
1825 continue;
1828 /* Init ref only if we really need it. */
1829 if (ref.base == NULL_TREE
1830 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type,
1831 refx->operands))
1833 res = true;
1834 break;
1836 /* If the statement may clobber expr, it dies. */
1837 if (stmt_may_clobber_ref_p_1 (def, &ref))
1839 res = true;
1840 break;
1844 /* Remember the result. */
1845 if (!EXPR_DIES (block))
1846 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1847 bitmap_set_bit (EXPR_DIES (block), id * 2);
1848 if (res)
1849 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1851 return res;
1855 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1856 contains its value-id. */
1858 static bool
1859 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1861 if (op && TREE_CODE (op) == SSA_NAME)
1863 unsigned int value_id = VN_INFO (op)->value_id;
1864 if (!(bitmap_set_contains_value (set1, value_id)
1865 || (set2 && bitmap_set_contains_value (set2, value_id))))
1866 return false;
1868 return true;
1871 /* Determine if the expression EXPR is valid in SET1 U SET2.
1872 ONLY SET2 CAN BE NULL.
1873 This means that we have a leader for each part of the expression
1874 (if it consists of values), or the expression is an SSA_NAME.
1875 For loads/calls, we also see if the vuse is killed in this block. */
1877 static bool
1878 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
1880 switch (expr->kind)
1882 case NAME:
1883 /* By construction all NAMEs are available. Non-available
1884 NAMEs are removed by subtracting TMP_GEN from the sets. */
1885 return true;
1886 case NARY:
1888 unsigned int i;
1889 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1890 for (i = 0; i < nary->length; i++)
1891 if (!op_valid_in_sets (set1, set2, nary->op[i]))
1892 return false;
1893 return true;
1895 break;
1896 case REFERENCE:
1898 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1899 vn_reference_op_t vro;
1900 unsigned int i;
1902 FOR_EACH_VEC_ELT (ref->operands, i, vro)
1904 if (!op_valid_in_sets (set1, set2, vro->op0)
1905 || !op_valid_in_sets (set1, set2, vro->op1)
1906 || !op_valid_in_sets (set1, set2, vro->op2))
1907 return false;
1909 return true;
1911 default:
1912 gcc_unreachable ();
1916 /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
1917 This means expressions that are made up of values we have no leaders for
1918 in SET1 or SET2. */
1920 static void
1921 clean (bitmap_set_t set1, bitmap_set_t set2 = NULL)
1923 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
1924 pre_expr expr;
1925 int i;
1927 FOR_EACH_VEC_ELT (exprs, i, expr)
1929 if (!valid_in_sets (set1, set2, expr))
1931 unsigned int val = get_expr_value_id (expr);
1932 bitmap_clear_bit (&set1->expressions, get_expression_id (expr));
1933 /* We are entered with possibly multiple expressions for a value
1934 so before removing a value from the set see if there's an
1935 expression for it left. */
1936 if (! bitmap_find_leader (set1, val))
1937 bitmap_clear_bit (&set1->values, val);
1940 exprs.release ();
1943 /* Clean the set of expressions that are no longer valid in SET because
1944 they are clobbered in BLOCK or because they trap and may not be executed. */
1946 static void
1947 prune_clobbered_mems (bitmap_set_t set, basic_block block)
1949 bitmap_iterator bi;
1950 unsigned i;
1951 unsigned to_remove = -1U;
1952 bool any_removed = false;
1954 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1956 /* Remove queued expr. */
1957 if (to_remove != -1U)
1959 bitmap_clear_bit (&set->expressions, to_remove);
1960 any_removed = true;
1961 to_remove = -1U;
1964 pre_expr expr = expression_for_id (i);
1965 if (expr->kind == REFERENCE)
1967 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1968 if (ref->vuse)
1970 gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
1971 if (!gimple_nop_p (def_stmt)
1972 && ((gimple_bb (def_stmt) != block
1973 && !dominated_by_p (CDI_DOMINATORS,
1974 block, gimple_bb (def_stmt)))
1975 || (gimple_bb (def_stmt) == block
1976 && value_dies_in_block_x (expr, block))))
1977 to_remove = i;
1980 else if (expr->kind == NARY)
1982 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1983 /* If the NARY may trap make sure the block does not contain
1984 a possible exit point.
1985 ??? This is overly conservative if we translate AVAIL_OUT
1986 as the available expression might be after the exit point. */
1987 if (BB_MAY_NOTRETURN (block)
1988 && vn_nary_may_trap (nary))
1989 to_remove = i;
1993 /* Remove queued expr. */
1994 if (to_remove != -1U)
1996 bitmap_clear_bit (&set->expressions, to_remove);
1997 any_removed = true;
2000 /* Above we only removed expressions, now clean the set of values
2001 which no longer have any corresponding expression. We cannot
2002 clear the value at the time we remove an expression since there
2003 may be multiple expressions per value.
2004 If we'd queue possibly to be removed values we could use
2005 the bitmap_find_leader way to see if there's still an expression
2006 for it. For some ratio of to be removed values and number of
2007 values/expressions in the set this might be faster than rebuilding
2008 the value-set. */
2009 if (any_removed)
2011 bitmap_clear (&set->values);
2012 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2014 pre_expr expr = expression_for_id (i);
2015 unsigned int value_id = get_expr_value_id (expr);
2016 bitmap_set_bit (&set->values, value_id);
2021 /* Compute the ANTIC set for BLOCK.
2023 If succs(BLOCK) > 1 then
2024 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2025 else if succs(BLOCK) == 1 then
2026 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2028 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2030 Note that clean() is deferred until after the iteration. */
2032 static bool
2033 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2035 bitmap_set_t S, old, ANTIC_OUT;
2036 edge e;
2037 edge_iterator ei;
2039 bool was_visited = BB_VISITED (block);
2040 bool changed = ! BB_VISITED (block);
2041 BB_VISITED (block) = 1;
2042 old = ANTIC_OUT = S = NULL;
2044 /* If any edges from predecessors are abnormal, antic_in is empty,
2045 so do nothing. */
2046 if (block_has_abnormal_pred_edge)
2047 goto maybe_dump_sets;
2049 old = ANTIC_IN (block);
2050 ANTIC_OUT = bitmap_set_new ();
2052 /* If the block has no successors, ANTIC_OUT is empty. */
2053 if (EDGE_COUNT (block->succs) == 0)
2055 /* If we have one successor, we could have some phi nodes to
2056 translate through. */
2057 else if (single_succ_p (block))
2059 e = single_succ_edge (block);
2060 gcc_assert (BB_VISITED (e->dest));
2061 phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
2063 /* If we have multiple successors, we take the intersection of all of
2064 them. Note that in the case of loop exit phi nodes, we may have
2065 phis to translate through. */
2066 else
2068 size_t i;
2069 edge first = NULL;
2071 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2072 FOR_EACH_EDGE (e, ei, block->succs)
2074 if (!first
2075 && BB_VISITED (e->dest))
2076 first = e;
2077 else if (BB_VISITED (e->dest))
2078 worklist.quick_push (e);
2079 else
2081 /* Unvisited successors get their ANTIC_IN replaced by the
2082 maximal set to arrive at a maximum ANTIC_IN solution.
2083 We can ignore them in the intersection operation and thus
2084 need not explicitely represent that maximum solution. */
2085 if (dump_file && (dump_flags & TDF_DETAILS))
2086 fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n",
2087 e->src->index, e->dest->index);
2091 /* Of multiple successors we have to have visited one already
2092 which is guaranteed by iteration order. */
2093 gcc_assert (first != NULL);
2095 phi_translate_set (ANTIC_OUT, ANTIC_IN (first->dest), first);
2097 /* If we have multiple successors we need to intersect the ANTIC_OUT
2098 sets. For values that's a simple intersection but for
2099 expressions it is a union. Given we want to have a single
2100 expression per value in our sets we have to canonicalize.
2101 Avoid randomness and running into cycles like for PR82129 and
2102 canonicalize the expression we choose to the one with the
2103 lowest id. This requires we actually compute the union first. */
2104 FOR_EACH_VEC_ELT (worklist, i, e)
2106 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2108 bitmap_set_t tmp = bitmap_set_new ();
2109 phi_translate_set (tmp, ANTIC_IN (e->dest), e);
2110 bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
2111 bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
2112 bitmap_set_free (tmp);
2114 else
2116 bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
2117 bitmap_ior_into (&ANTIC_OUT->expressions,
2118 &ANTIC_IN (e->dest)->expressions);
2121 if (! worklist.is_empty ())
2123 /* Prune expressions not in the value set. */
2124 bitmap_iterator bi;
2125 unsigned int i;
2126 unsigned int to_clear = -1U;
2127 FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
2129 if (to_clear != -1U)
2131 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2132 to_clear = -1U;
2134 pre_expr expr = expression_for_id (i);
2135 unsigned int value_id = get_expr_value_id (expr);
2136 if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
2137 to_clear = i;
2139 if (to_clear != -1U)
2140 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2144 /* Prune expressions that are clobbered in block and thus become
2145 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2146 prune_clobbered_mems (ANTIC_OUT, block);
2148 /* Generate ANTIC_OUT - TMP_GEN. */
2149 S = bitmap_set_subtract_expressions (ANTIC_OUT, TMP_GEN (block));
2151 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2152 ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
2153 TMP_GEN (block));
2155 /* Then union in the ANTIC_OUT - TMP_GEN values,
2156 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2157 bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
2158 bitmap_ior_into (&ANTIC_IN (block)->expressions, &S->expressions);
2160 /* clean (ANTIC_IN (block)) is defered to after the iteration converged
2161 because it can cause non-convergence, see for example PR81181. */
2163 /* Intersect ANTIC_IN with the old ANTIC_IN. This is required until
2164 we properly represent the maximum expression set, thus not prune
2165 values without expressions during the iteration. */
2166 if (was_visited
2167 && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
2169 if (dump_file && (dump_flags & TDF_DETAILS))
2170 fprintf (dump_file, "warning: intersecting with old ANTIC_IN "
2171 "shrinks the set\n");
2172 /* Prune expressions not in the value set. */
2173 bitmap_iterator bi;
2174 unsigned int i;
2175 unsigned int to_clear = -1U;
2176 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
2178 if (to_clear != -1U)
2180 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2181 to_clear = -1U;
2183 pre_expr expr = expression_for_id (i);
2184 unsigned int value_id = get_expr_value_id (expr);
2185 if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
2186 to_clear = i;
2188 if (to_clear != -1U)
2189 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2192 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2193 changed = true;
2195 maybe_dump_sets:
2196 if (dump_file && (dump_flags & TDF_DETAILS))
2198 if (ANTIC_OUT)
2199 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2201 if (changed)
2202 fprintf (dump_file, "[changed] ");
2203 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2204 block->index);
2206 if (S)
2207 print_bitmap_set (dump_file, S, "S", block->index);
2209 if (old)
2210 bitmap_set_free (old);
2211 if (S)
2212 bitmap_set_free (S);
2213 if (ANTIC_OUT)
2214 bitmap_set_free (ANTIC_OUT);
2215 return changed;
2218 /* Compute PARTIAL_ANTIC for BLOCK.
2220 If succs(BLOCK) > 1 then
2221 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2222 in ANTIC_OUT for all succ(BLOCK)
2223 else if succs(BLOCK) == 1 then
2224 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2226 PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
2229 static void
2230 compute_partial_antic_aux (basic_block block,
2231 bool block_has_abnormal_pred_edge)
2233 bitmap_set_t old_PA_IN;
2234 bitmap_set_t PA_OUT;
2235 edge e;
2236 edge_iterator ei;
2237 unsigned long max_pa = param_max_partial_antic_length;
2239 old_PA_IN = PA_OUT = NULL;
2241 /* If any edges from predecessors are abnormal, antic_in is empty,
2242 so do nothing. */
2243 if (block_has_abnormal_pred_edge)
2244 goto maybe_dump_sets;
2246 /* If there are too many partially anticipatable values in the
2247 block, phi_translate_set can take an exponential time: stop
2248 before the translation starts. */
2249 if (max_pa
2250 && single_succ_p (block)
2251 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2252 goto maybe_dump_sets;
2254 old_PA_IN = PA_IN (block);
2255 PA_OUT = bitmap_set_new ();
2257 /* If the block has no successors, ANTIC_OUT is empty. */
2258 if (EDGE_COUNT (block->succs) == 0)
2260 /* If we have one successor, we could have some phi nodes to
2261 translate through. Note that we can't phi translate across DFS
2262 back edges in partial antic, because it uses a union operation on
2263 the successors. For recurrences like IV's, we will end up
2264 generating a new value in the set on each go around (i + 3 (VH.1)
2265 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2266 else if (single_succ_p (block))
2268 e = single_succ_edge (block);
2269 if (!(e->flags & EDGE_DFS_BACK))
2270 phi_translate_set (PA_OUT, PA_IN (e->dest), e);
2272 /* If we have multiple successors, we take the union of all of
2273 them. */
2274 else
2276 size_t i;
2278 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2279 FOR_EACH_EDGE (e, ei, block->succs)
2281 if (e->flags & EDGE_DFS_BACK)
2282 continue;
2283 worklist.quick_push (e);
2285 if (worklist.length () > 0)
2287 FOR_EACH_VEC_ELT (worklist, i, e)
2289 unsigned int i;
2290 bitmap_iterator bi;
2292 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
2293 bitmap_value_insert_into_set (PA_OUT,
2294 expression_for_id (i));
2295 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2297 bitmap_set_t pa_in = bitmap_set_new ();
2298 phi_translate_set (pa_in, PA_IN (e->dest), e);
2299 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2300 bitmap_value_insert_into_set (PA_OUT,
2301 expression_for_id (i));
2302 bitmap_set_free (pa_in);
2304 else
2305 FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
2306 bitmap_value_insert_into_set (PA_OUT,
2307 expression_for_id (i));
2312 /* Prune expressions that are clobbered in block and thus become
2313 invalid if translated from PA_OUT to PA_IN. */
2314 prune_clobbered_mems (PA_OUT, block);
2316 /* PA_IN starts with PA_OUT - TMP_GEN.
2317 Then we subtract things from ANTIC_IN. */
2318 PA_IN (block) = bitmap_set_subtract_expressions (PA_OUT, TMP_GEN (block));
2320 /* For partial antic, we want to put back in the phi results, since
2321 we will properly avoid making them partially antic over backedges. */
2322 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2323 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2325 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2326 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2328 clean (PA_IN (block), ANTIC_IN (block));
2330 maybe_dump_sets:
2331 if (dump_file && (dump_flags & TDF_DETAILS))
2333 if (PA_OUT)
2334 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2336 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2338 if (old_PA_IN)
2339 bitmap_set_free (old_PA_IN);
2340 if (PA_OUT)
2341 bitmap_set_free (PA_OUT);
2344 /* Compute ANTIC and partial ANTIC sets. */
2346 static void
2347 compute_antic (void)
2349 bool changed = true;
2350 int num_iterations = 0;
2351 basic_block block;
2352 int i;
2353 edge_iterator ei;
2354 edge e;
2356 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2357 We pre-build the map of blocks with incoming abnormal edges here. */
2358 auto_sbitmap has_abnormal_preds (last_basic_block_for_fn (cfun));
2359 bitmap_clear (has_abnormal_preds);
2361 FOR_ALL_BB_FN (block, cfun)
2363 BB_VISITED (block) = 0;
2365 FOR_EACH_EDGE (e, ei, block->preds)
2366 if (e->flags & EDGE_ABNORMAL)
2368 bitmap_set_bit (has_abnormal_preds, block->index);
2369 break;
2372 /* While we are here, give empty ANTIC_IN sets to each block. */
2373 ANTIC_IN (block) = bitmap_set_new ();
2374 if (do_partial_partial)
2375 PA_IN (block) = bitmap_set_new ();
2378 /* At the exit block we anticipate nothing. */
2379 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2381 /* For ANTIC computation we need a postorder that also guarantees that
2382 a block with a single successor is visited after its successor.
2383 RPO on the inverted CFG has this property. */
2384 auto_vec<int, 20> postorder;
2385 inverted_post_order_compute (&postorder);
2387 auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
2388 bitmap_clear (worklist);
2389 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2390 bitmap_set_bit (worklist, e->src->index);
2391 while (changed)
2393 if (dump_file && (dump_flags & TDF_DETAILS))
2394 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2395 /* ??? We need to clear our PHI translation cache here as the
2396 ANTIC sets shrink and we restrict valid translations to
2397 those having operands with leaders in ANTIC. Same below
2398 for PA ANTIC computation. */
2399 num_iterations++;
2400 changed = false;
2401 for (i = postorder.length () - 1; i >= 0; i--)
2403 if (bitmap_bit_p (worklist, postorder[i]))
2405 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2406 bitmap_clear_bit (worklist, block->index);
2407 if (compute_antic_aux (block,
2408 bitmap_bit_p (has_abnormal_preds,
2409 block->index)))
2411 FOR_EACH_EDGE (e, ei, block->preds)
2412 bitmap_set_bit (worklist, e->src->index);
2413 changed = true;
2417 /* Theoretically possible, but *highly* unlikely. */
2418 gcc_checking_assert (num_iterations < 500);
2421 /* We have to clean after the dataflow problem converged as cleaning
2422 can cause non-convergence because it is based on expressions
2423 rather than values. */
2424 FOR_EACH_BB_FN (block, cfun)
2425 clean (ANTIC_IN (block));
2427 statistics_histogram_event (cfun, "compute_antic iterations",
2428 num_iterations);
2430 if (do_partial_partial)
2432 /* For partial antic we ignore backedges and thus we do not need
2433 to perform any iteration when we process blocks in postorder. */
2434 for (i = postorder.length () - 1; i >= 0; i--)
2436 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2437 compute_partial_antic_aux (block,
2438 bitmap_bit_p (has_abnormal_preds,
2439 block->index));
2445 /* Inserted expressions are placed onto this worklist, which is used
2446 for performing quick dead code elimination of insertions we made
2447 that didn't turn out to be necessary. */
2448 static bitmap inserted_exprs;
2450 /* The actual worker for create_component_ref_by_pieces. */
2452 static tree
2453 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2454 unsigned int *operand, gimple_seq *stmts)
2456 vn_reference_op_t currop = &ref->operands[*operand];
2457 tree genop;
2458 ++*operand;
2459 switch (currop->opcode)
2461 case CALL_EXPR:
2462 gcc_unreachable ();
2464 case MEM_REF:
2466 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2467 stmts);
2468 if (!baseop)
2469 return NULL_TREE;
2470 tree offset = currop->op0;
2471 if (TREE_CODE (baseop) == ADDR_EXPR
2472 && handled_component_p (TREE_OPERAND (baseop, 0)))
2474 poly_int64 off;
2475 tree base;
2476 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2477 &off);
2478 gcc_assert (base);
2479 offset = int_const_binop (PLUS_EXPR, offset,
2480 build_int_cst (TREE_TYPE (offset),
2481 off));
2482 baseop = build_fold_addr_expr (base);
2484 genop = build2 (MEM_REF, currop->type, baseop, offset);
2485 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2486 MR_DEPENDENCE_BASE (genop) = currop->base;
2487 REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
2488 return genop;
2491 case TARGET_MEM_REF:
2493 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2494 vn_reference_op_t nextop = &ref->operands[(*operand)++];
2495 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2496 stmts);
2497 if (!baseop)
2498 return NULL_TREE;
2499 if (currop->op0)
2501 genop0 = find_or_generate_expression (block, currop->op0, stmts);
2502 if (!genop0)
2503 return NULL_TREE;
2505 if (nextop->op0)
2507 genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2508 if (!genop1)
2509 return NULL_TREE;
2511 genop = build5 (TARGET_MEM_REF, currop->type,
2512 baseop, currop->op2, genop0, currop->op1, genop1);
2514 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2515 MR_DEPENDENCE_BASE (genop) = currop->base;
2516 return genop;
2519 case ADDR_EXPR:
2520 if (currop->op0)
2522 gcc_assert (is_gimple_min_invariant (currop->op0));
2523 return currop->op0;
2525 /* Fallthrough. */
2526 case REALPART_EXPR:
2527 case IMAGPART_EXPR:
2528 case VIEW_CONVERT_EXPR:
2530 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2531 stmts);
2532 if (!genop0)
2533 return NULL_TREE;
2534 return fold_build1 (currop->opcode, currop->type, genop0);
2537 case WITH_SIZE_EXPR:
2539 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2540 stmts);
2541 if (!genop0)
2542 return NULL_TREE;
2543 tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2544 if (!genop1)
2545 return NULL_TREE;
2546 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2549 case BIT_FIELD_REF:
2551 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2552 stmts);
2553 if (!genop0)
2554 return NULL_TREE;
2555 tree op1 = currop->op0;
2556 tree op2 = currop->op1;
2557 tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2558 REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
2559 return fold (t);
2562 /* For array ref vn_reference_op's, operand 1 of the array ref
2563 is op0 of the reference op and operand 3 of the array ref is
2564 op1. */
2565 case ARRAY_RANGE_REF:
2566 case ARRAY_REF:
2568 tree genop0;
2569 tree genop1 = currop->op0;
2570 tree genop2 = currop->op1;
2571 tree genop3 = currop->op2;
2572 genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2573 stmts);
2574 if (!genop0)
2575 return NULL_TREE;
2576 genop1 = find_or_generate_expression (block, genop1, stmts);
2577 if (!genop1)
2578 return NULL_TREE;
2579 if (genop2)
2581 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2582 /* Drop zero minimum index if redundant. */
2583 if (integer_zerop (genop2)
2584 && (!domain_type
2585 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2586 genop2 = NULL_TREE;
2587 else
2589 genop2 = find_or_generate_expression (block, genop2, stmts);
2590 if (!genop2)
2591 return NULL_TREE;
2594 if (genop3)
2596 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2597 /* We can't always put a size in units of the element alignment
2598 here as the element alignment may be not visible. See
2599 PR43783. Simply drop the element size for constant
2600 sizes. */
2601 if (TREE_CODE (genop3) == INTEGER_CST
2602 && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
2603 && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
2604 (wi::to_offset (genop3)
2605 * vn_ref_op_align_unit (currop))))
2606 genop3 = NULL_TREE;
2607 else
2609 genop3 = find_or_generate_expression (block, genop3, stmts);
2610 if (!genop3)
2611 return NULL_TREE;
2614 return build4 (currop->opcode, currop->type, genop0, genop1,
2615 genop2, genop3);
2617 case COMPONENT_REF:
2619 tree op0;
2620 tree op1;
2621 tree genop2 = currop->op1;
2622 op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2623 if (!op0)
2624 return NULL_TREE;
2625 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2626 op1 = currop->op0;
2627 if (genop2)
2629 genop2 = find_or_generate_expression (block, genop2, stmts);
2630 if (!genop2)
2631 return NULL_TREE;
2633 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2636 case SSA_NAME:
2638 genop = find_or_generate_expression (block, currop->op0, stmts);
2639 return genop;
2641 case STRING_CST:
2642 case INTEGER_CST:
2643 case COMPLEX_CST:
2644 case VECTOR_CST:
2645 case REAL_CST:
2646 case CONSTRUCTOR:
2647 case VAR_DECL:
2648 case PARM_DECL:
2649 case CONST_DECL:
2650 case RESULT_DECL:
2651 case FUNCTION_DECL:
2652 return currop->op0;
2654 default:
2655 gcc_unreachable ();
2659 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2660 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2661 trying to rename aggregates into ssa form directly, which is a no no.
2663 Thus, this routine doesn't create temporaries, it just builds a
2664 single access expression for the array, calling
2665 find_or_generate_expression to build the innermost pieces.
2667 This function is a subroutine of create_expression_by_pieces, and
2668 should not be called on it's own unless you really know what you
2669 are doing. */
2671 static tree
2672 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2673 gimple_seq *stmts)
2675 unsigned int op = 0;
2676 return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2679 /* Find a simple leader for an expression, or generate one using
2680 create_expression_by_pieces from a NARY expression for the value.
2681 BLOCK is the basic_block we are looking for leaders in.
2682 OP is the tree expression to find a leader for or generate.
2683 Returns the leader or NULL_TREE on failure. */
2685 static tree
2686 find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2688 pre_expr expr = get_or_alloc_expr_for (op);
2689 unsigned int lookfor = get_expr_value_id (expr);
2690 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2691 if (leader)
2693 if (leader->kind == NAME)
2694 return PRE_EXPR_NAME (leader);
2695 else if (leader->kind == CONSTANT)
2696 return PRE_EXPR_CONSTANT (leader);
2698 /* Defer. */
2699 return NULL_TREE;
2702 /* It must be a complex expression, so generate it recursively. Note
2703 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2704 where the insert algorithm fails to insert a required expression. */
2705 bitmap exprset = value_expressions[lookfor];
2706 bitmap_iterator bi;
2707 unsigned int i;
2708 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2710 pre_expr temp = expression_for_id (i);
2711 /* We cannot insert random REFERENCE expressions at arbitrary
2712 places. We can insert NARYs which eventually re-materializes
2713 its operand values. */
2714 if (temp->kind == NARY)
2715 return create_expression_by_pieces (block, temp, stmts,
2716 get_expr_type (expr));
2719 /* Defer. */
2720 return NULL_TREE;
2723 /* Create an expression in pieces, so that we can handle very complex
2724 expressions that may be ANTIC, but not necessary GIMPLE.
2725 BLOCK is the basic block the expression will be inserted into,
2726 EXPR is the expression to insert (in value form)
2727 STMTS is a statement list to append the necessary insertions into.
2729 This function will die if we hit some value that shouldn't be
2730 ANTIC but is (IE there is no leader for it, or its components).
2731 The function returns NULL_TREE in case a different antic expression
2732 has to be inserted first.
2733 This function may also generate expressions that are themselves
2734 partially or fully redundant. Those that are will be either made
2735 fully redundant during the next iteration of insert (for partially
2736 redundant ones), or eliminated by eliminate (for fully redundant
2737 ones). */
2739 static tree
2740 create_expression_by_pieces (basic_block block, pre_expr expr,
2741 gimple_seq *stmts, tree type)
2743 tree name;
2744 tree folded;
2745 gimple_seq forced_stmts = NULL;
2746 unsigned int value_id;
2747 gimple_stmt_iterator gsi;
2748 tree exprtype = type ? type : get_expr_type (expr);
2749 pre_expr nameexpr;
2750 gassign *newstmt;
2752 switch (expr->kind)
2754 /* We may hit the NAME/CONSTANT case if we have to convert types
2755 that value numbering saw through. */
2756 case NAME:
2757 folded = PRE_EXPR_NAME (expr);
2758 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
2759 return NULL_TREE;
2760 if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2761 return folded;
2762 break;
2763 case CONSTANT:
2765 folded = PRE_EXPR_CONSTANT (expr);
2766 tree tem = fold_convert (exprtype, folded);
2767 if (is_gimple_min_invariant (tem))
2768 return tem;
2769 break;
2771 case REFERENCE:
2772 if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
2774 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2775 unsigned int operand = 1;
2776 vn_reference_op_t currop = &ref->operands[0];
2777 tree sc = NULL_TREE;
2778 tree fn = find_or_generate_expression (block, currop->op0, stmts);
2779 if (!fn)
2780 return NULL_TREE;
2781 if (currop->op1)
2783 sc = find_or_generate_expression (block, currop->op1, stmts);
2784 if (!sc)
2785 return NULL_TREE;
2787 auto_vec<tree> args (ref->operands.length () - 1);
2788 while (operand < ref->operands.length ())
2790 tree arg = create_component_ref_by_pieces_1 (block, ref,
2791 &operand, stmts);
2792 if (!arg)
2793 return NULL_TREE;
2794 args.quick_push (arg);
2796 gcall *call = gimple_build_call_vec (fn, args);
2797 gimple_set_location (call, expr->loc);
2798 gimple_call_set_fntype (call, currop->type);
2799 if (sc)
2800 gimple_call_set_chain (call, sc);
2801 tree forcedname = make_ssa_name (TREE_TYPE (currop->type));
2802 gimple_call_set_lhs (call, forcedname);
2803 /* There's no CCP pass after PRE which would re-compute alignment
2804 information so make sure we re-materialize this here. */
2805 if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED)
2806 && args.length () - 2 <= 1
2807 && tree_fits_uhwi_p (args[1])
2808 && (args.length () != 3 || tree_fits_uhwi_p (args[2])))
2810 unsigned HOST_WIDE_INT halign = tree_to_uhwi (args[1]);
2811 unsigned HOST_WIDE_INT hmisalign
2812 = args.length () == 3 ? tree_to_uhwi (args[2]) : 0;
2813 if ((halign & (halign - 1)) == 0
2814 && (hmisalign & ~(halign - 1)) == 0)
2815 set_ptr_info_alignment (get_ptr_info (forcedname),
2816 halign, hmisalign);
2818 gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
2819 gimple_seq_add_stmt_without_update (&forced_stmts, call);
2820 folded = forcedname;
2822 else
2824 folded = create_component_ref_by_pieces (block,
2825 PRE_EXPR_REFERENCE (expr),
2826 stmts);
2827 if (!folded)
2828 return NULL_TREE;
2829 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2830 newstmt = gimple_build_assign (name, folded);
2831 gimple_set_location (newstmt, expr->loc);
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_set_location (newstmt, expr->loc);
2871 gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2872 folded = name;
2874 else
2876 switch (nary->length)
2878 case 1:
2879 folded = gimple_build (&forced_stmts, expr->loc,
2880 nary->opcode, nary->type, genop[0]);
2881 break;
2882 case 2:
2883 folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
2884 nary->type, genop[0], genop[1]);
2885 break;
2886 case 3:
2887 folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
2888 nary->type, genop[0], genop[1],
2889 genop[2]);
2890 break;
2891 default:
2892 gcc_unreachable ();
2896 break;
2897 default:
2898 gcc_unreachable ();
2901 folded = gimple_convert (&forced_stmts, exprtype, folded);
2903 /* If there is nothing to insert, return the simplified result. */
2904 if (gimple_seq_empty_p (forced_stmts))
2905 return folded;
2906 /* If we simplified to a constant return it and discard eventually
2907 built stmts. */
2908 if (is_gimple_min_invariant (folded))
2910 gimple_seq_discard (forced_stmts);
2911 return folded;
2913 /* Likewise if we simplified to sth not queued for insertion. */
2914 bool found = false;
2915 gsi = gsi_last (forced_stmts);
2916 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2918 gimple *stmt = gsi_stmt (gsi);
2919 tree forcedname = gimple_get_lhs (stmt);
2920 if (forcedname == folded)
2922 found = true;
2923 break;
2926 if (! found)
2928 gimple_seq_discard (forced_stmts);
2929 return folded;
2931 gcc_assert (TREE_CODE (folded) == SSA_NAME);
2933 /* If we have any intermediate expressions to the value sets, add them
2934 to the value sets and chain them in the instruction stream. */
2935 if (forced_stmts)
2937 gsi = gsi_start (forced_stmts);
2938 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2940 gimple *stmt = gsi_stmt (gsi);
2941 tree forcedname = gimple_get_lhs (stmt);
2942 pre_expr nameexpr;
2944 if (forcedname != folded)
2946 VN_INFO (forcedname)->valnum = forcedname;
2947 VN_INFO (forcedname)->value_id = get_next_value_id ();
2948 nameexpr = get_or_alloc_expr_for_name (forcedname);
2949 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
2950 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2951 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2954 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
2956 gimple_seq_add_seq (stmts, forced_stmts);
2959 name = folded;
2961 /* Fold the last statement. */
2962 gsi = gsi_last (*stmts);
2963 if (fold_stmt_inplace (&gsi))
2964 update_stmt (gsi_stmt (gsi));
2966 /* Add a value number to the temporary.
2967 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2968 we are creating the expression by pieces, and this particular piece of
2969 the expression may have been represented. There is no harm in replacing
2970 here. */
2971 value_id = get_expr_value_id (expr);
2972 VN_INFO (name)->value_id = value_id;
2973 VN_INFO (name)->valnum = vn_valnum_from_value_id (value_id);
2974 if (VN_INFO (name)->valnum == NULL_TREE)
2975 VN_INFO (name)->valnum = name;
2976 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
2977 nameexpr = get_or_alloc_expr_for_name (name);
2978 add_to_value (value_id, nameexpr);
2979 if (NEW_SETS (block))
2980 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2981 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2983 pre_stats.insertions++;
2984 if (dump_file && (dump_flags & TDF_DETAILS))
2986 fprintf (dump_file, "Inserted ");
2987 print_gimple_stmt (dump_file, gsi_stmt (gsi_last (*stmts)), 0);
2988 fprintf (dump_file, " in predecessor %d (%04d)\n",
2989 block->index, value_id);
2992 return name;
2996 /* Insert the to-be-made-available values of expression EXPRNUM for each
2997 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2998 merge the result with a phi node, given the same value number as
2999 NODE. Return true if we have inserted new stuff. */
3001 static bool
3002 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3003 vec<pre_expr> avail)
3005 pre_expr expr = expression_for_id (exprnum);
3006 pre_expr newphi;
3007 unsigned int val = get_expr_value_id (expr);
3008 edge pred;
3009 bool insertions = false;
3010 bool nophi = false;
3011 basic_block bprime;
3012 pre_expr eprime;
3013 edge_iterator ei;
3014 tree type = get_expr_type (expr);
3015 tree temp;
3016 gphi *phi;
3018 /* Make sure we aren't creating an induction variable. */
3019 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3021 bool firstinsideloop = false;
3022 bool secondinsideloop = false;
3023 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3024 EDGE_PRED (block, 0)->src);
3025 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3026 EDGE_PRED (block, 1)->src);
3027 /* Induction variables only have one edge inside the loop. */
3028 if ((firstinsideloop ^ secondinsideloop)
3029 && expr->kind != REFERENCE)
3031 if (dump_file && (dump_flags & TDF_DETAILS))
3032 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3033 nophi = true;
3037 /* Make the necessary insertions. */
3038 FOR_EACH_EDGE (pred, ei, block->preds)
3040 gimple_seq stmts = NULL;
3041 tree builtexpr;
3042 bprime = pred->src;
3043 eprime = avail[pred->dest_idx];
3044 builtexpr = create_expression_by_pieces (bprime, eprime,
3045 &stmts, type);
3046 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3047 if (!gimple_seq_empty_p (stmts))
3049 basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
3050 gcc_assert (! new_bb);
3051 insertions = true;
3053 if (!builtexpr)
3055 /* We cannot insert a PHI node if we failed to insert
3056 on one edge. */
3057 nophi = true;
3058 continue;
3060 if (is_gimple_min_invariant (builtexpr))
3061 avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
3062 else
3063 avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3065 /* If we didn't want a phi node, and we made insertions, we still have
3066 inserted new stuff, and thus return true. If we didn't want a phi node,
3067 and didn't make insertions, we haven't added anything new, so return
3068 false. */
3069 if (nophi && insertions)
3070 return true;
3071 else if (nophi && !insertions)
3072 return false;
3074 /* Now build a phi for the new variable. */
3075 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3076 phi = create_phi_node (temp, block);
3078 VN_INFO (temp)->value_id = val;
3079 VN_INFO (temp)->valnum = vn_valnum_from_value_id (val);
3080 if (VN_INFO (temp)->valnum == NULL_TREE)
3081 VN_INFO (temp)->valnum = temp;
3082 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3083 FOR_EACH_EDGE (pred, ei, block->preds)
3085 pre_expr ae = avail[pred->dest_idx];
3086 gcc_assert (get_expr_type (ae) == type
3087 || useless_type_conversion_p (type, get_expr_type (ae)));
3088 if (ae->kind == CONSTANT)
3089 add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3090 pred, UNKNOWN_LOCATION);
3091 else
3092 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3095 newphi = get_or_alloc_expr_for_name (temp);
3096 add_to_value (val, newphi);
3098 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3099 this insertion, since we test for the existence of this value in PHI_GEN
3100 before proceeding with the partial redundancy checks in insert_aux.
3102 The value may exist in AVAIL_OUT, in particular, it could be represented
3103 by the expression we are trying to eliminate, in which case we want the
3104 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3105 inserted there.
3107 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3108 this block, because if it did, it would have existed in our dominator's
3109 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3112 bitmap_insert_into_set (PHI_GEN (block), newphi);
3113 bitmap_value_replace_in_set (AVAIL_OUT (block),
3114 newphi);
3115 bitmap_insert_into_set (NEW_SETS (block),
3116 newphi);
3118 /* If we insert a PHI node for a conversion of another PHI node
3119 in the same basic-block try to preserve range information.
3120 This is important so that followup loop passes receive optimal
3121 number of iteration analysis results. See PR61743. */
3122 if (expr->kind == NARY
3123 && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3124 && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3125 && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3126 && INTEGRAL_TYPE_P (type)
3127 && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3128 && (TYPE_PRECISION (type)
3129 >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3130 && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3132 wide_int min, max;
3133 if (get_range_info (expr->u.nary->op[0], &min, &max) == VR_RANGE
3134 && !wi::neg_p (min, SIGNED)
3135 && !wi::neg_p (max, SIGNED))
3136 /* Just handle extension and sign-changes of all-positive ranges. */
3137 set_range_info (temp,
3138 SSA_NAME_RANGE_TYPE (expr->u.nary->op[0]),
3139 wide_int_storage::from (min, TYPE_PRECISION (type),
3140 TYPE_SIGN (type)),
3141 wide_int_storage::from (max, TYPE_PRECISION (type),
3142 TYPE_SIGN (type)));
3145 if (dump_file && (dump_flags & TDF_DETAILS))
3147 fprintf (dump_file, "Created phi ");
3148 print_gimple_stmt (dump_file, phi, 0);
3149 fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3151 pre_stats.phis++;
3152 return true;
3157 /* Perform insertion of partially redundant or hoistable values.
3158 For BLOCK, do the following:
3159 1. Propagate the NEW_SETS of the dominator into the current block.
3160 If the block has multiple predecessors,
3161 2a. Iterate over the ANTIC expressions for the block to see if
3162 any of them are partially redundant.
3163 2b. If so, insert them into the necessary predecessors to make
3164 the expression fully redundant.
3165 2c. Insert a new PHI merging the values of the predecessors.
3166 2d. Insert the new PHI, and the new expressions, into the
3167 NEW_SETS set.
3168 If the block has multiple successors,
3169 3a. Iterate over the ANTIC values for the block to see if
3170 any of them are good candidates for hoisting.
3171 3b. If so, insert expressions computing the values in BLOCK,
3172 and add the new expressions into the NEW_SETS set.
3173 4. Recursively call ourselves on the dominator children of BLOCK.
3175 Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3176 do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3177 done in do_hoist_insertion.
3180 static bool
3181 do_pre_regular_insertion (basic_block block, basic_block dom)
3183 bool new_stuff = false;
3184 vec<pre_expr> exprs;
3185 pre_expr expr;
3186 auto_vec<pre_expr> avail;
3187 int i;
3189 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3190 avail.safe_grow (EDGE_COUNT (block->preds));
3192 FOR_EACH_VEC_ELT (exprs, i, expr)
3194 if (expr->kind == NARY
3195 || expr->kind == REFERENCE)
3197 unsigned int val;
3198 bool by_some = false;
3199 bool cant_insert = false;
3200 bool all_same = true;
3201 pre_expr first_s = NULL;
3202 edge pred;
3203 basic_block bprime;
3204 pre_expr eprime = NULL;
3205 edge_iterator ei;
3206 pre_expr edoubleprime = NULL;
3207 bool do_insertion = false;
3209 val = get_expr_value_id (expr);
3210 if (bitmap_set_contains_value (PHI_GEN (block), val))
3211 continue;
3212 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3214 if (dump_file && (dump_flags & TDF_DETAILS))
3216 fprintf (dump_file, "Found fully redundant value: ");
3217 print_pre_expr (dump_file, expr);
3218 fprintf (dump_file, "\n");
3220 continue;
3223 FOR_EACH_EDGE (pred, ei, block->preds)
3225 unsigned int vprime;
3227 /* We should never run insertion for the exit block
3228 and so not come across fake pred edges. */
3229 gcc_assert (!(pred->flags & EDGE_FAKE));
3230 bprime = pred->src;
3231 /* We are looking at ANTIC_OUT of bprime. */
3232 eprime = phi_translate (NULL, expr, ANTIC_IN (block), NULL, pred);
3234 /* eprime will generally only be NULL if the
3235 value of the expression, translated
3236 through the PHI for this predecessor, is
3237 undefined. If that is the case, we can't
3238 make the expression fully redundant,
3239 because its value is undefined along a
3240 predecessor path. We can thus break out
3241 early because it doesn't matter what the
3242 rest of the results are. */
3243 if (eprime == NULL)
3245 avail[pred->dest_idx] = NULL;
3246 cant_insert = true;
3247 break;
3250 vprime = get_expr_value_id (eprime);
3251 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3252 vprime);
3253 if (edoubleprime == NULL)
3255 avail[pred->dest_idx] = eprime;
3256 all_same = false;
3258 else
3260 avail[pred->dest_idx] = edoubleprime;
3261 by_some = true;
3262 /* We want to perform insertions to remove a redundancy on
3263 a path in the CFG we want to optimize for speed. */
3264 if (optimize_edge_for_speed_p (pred))
3265 do_insertion = true;
3266 if (first_s == NULL)
3267 first_s = edoubleprime;
3268 else if (!pre_expr_d::equal (first_s, edoubleprime))
3269 all_same = false;
3272 /* If we can insert it, it's not the same value
3273 already existing along every predecessor, and
3274 it's defined by some predecessor, it is
3275 partially redundant. */
3276 if (!cant_insert && !all_same && by_some)
3278 if (!do_insertion)
3280 if (dump_file && (dump_flags & TDF_DETAILS))
3282 fprintf (dump_file, "Skipping partial redundancy for "
3283 "expression ");
3284 print_pre_expr (dump_file, expr);
3285 fprintf (dump_file, " (%04d), no redundancy on to be "
3286 "optimized for speed edge\n", val);
3289 else if (dbg_cnt (treepre_insert))
3291 if (dump_file && (dump_flags & TDF_DETAILS))
3293 fprintf (dump_file, "Found partial redundancy for "
3294 "expression ");
3295 print_pre_expr (dump_file, expr);
3296 fprintf (dump_file, " (%04d)\n",
3297 get_expr_value_id (expr));
3299 if (insert_into_preds_of_block (block,
3300 get_expression_id (expr),
3301 avail))
3302 new_stuff = true;
3305 /* If all edges produce the same value and that value is
3306 an invariant, then the PHI has the same value on all
3307 edges. Note this. */
3308 else if (!cant_insert && all_same)
3310 gcc_assert (edoubleprime->kind == CONSTANT
3311 || edoubleprime->kind == NAME);
3313 tree temp = make_temp_ssa_name (get_expr_type (expr),
3314 NULL, "pretmp");
3315 gassign *assign
3316 = gimple_build_assign (temp,
3317 edoubleprime->kind == CONSTANT ?
3318 PRE_EXPR_CONSTANT (edoubleprime) :
3319 PRE_EXPR_NAME (edoubleprime));
3320 gimple_stmt_iterator gsi = gsi_after_labels (block);
3321 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3323 VN_INFO (temp)->value_id = val;
3324 VN_INFO (temp)->valnum = vn_valnum_from_value_id (val);
3325 if (VN_INFO (temp)->valnum == NULL_TREE)
3326 VN_INFO (temp)->valnum = temp;
3327 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3328 pre_expr newe = get_or_alloc_expr_for_name (temp);
3329 add_to_value (val, newe);
3330 bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3331 bitmap_insert_into_set (NEW_SETS (block), newe);
3336 exprs.release ();
3337 return new_stuff;
3341 /* Perform insertion for partially anticipatable expressions. There
3342 is only one case we will perform insertion for these. This case is
3343 if the expression is partially anticipatable, and fully available.
3344 In this case, we know that putting it earlier will enable us to
3345 remove the later computation. */
3347 static bool
3348 do_pre_partial_partial_insertion (basic_block block, basic_block dom)
3350 bool new_stuff = false;
3351 vec<pre_expr> exprs;
3352 pre_expr expr;
3353 auto_vec<pre_expr> avail;
3354 int i;
3356 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3357 avail.safe_grow (EDGE_COUNT (block->preds));
3359 FOR_EACH_VEC_ELT (exprs, i, expr)
3361 if (expr->kind == NARY
3362 || expr->kind == REFERENCE)
3364 unsigned int val;
3365 bool by_all = true;
3366 bool cant_insert = false;
3367 edge pred;
3368 basic_block bprime;
3369 pre_expr eprime = NULL;
3370 edge_iterator ei;
3372 val = get_expr_value_id (expr);
3373 if (bitmap_set_contains_value (PHI_GEN (block), val))
3374 continue;
3375 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3376 continue;
3378 FOR_EACH_EDGE (pred, ei, block->preds)
3380 unsigned int vprime;
3381 pre_expr edoubleprime;
3383 /* We should never run insertion for the exit block
3384 and so not come across fake pred edges. */
3385 gcc_assert (!(pred->flags & EDGE_FAKE));
3386 bprime = pred->src;
3387 eprime = phi_translate (NULL, expr, ANTIC_IN (block),
3388 PA_IN (block), pred);
3390 /* eprime will generally only be NULL if the
3391 value of the expression, translated
3392 through the PHI for this predecessor, is
3393 undefined. If that is the case, we can't
3394 make the expression fully redundant,
3395 because its value is undefined along a
3396 predecessor path. We can thus break out
3397 early because it doesn't matter what the
3398 rest of the results are. */
3399 if (eprime == NULL)
3401 avail[pred->dest_idx] = NULL;
3402 cant_insert = true;
3403 break;
3406 vprime = get_expr_value_id (eprime);
3407 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3408 avail[pred->dest_idx] = edoubleprime;
3409 if (edoubleprime == NULL)
3411 by_all = false;
3412 break;
3416 /* If we can insert it, it's not the same value
3417 already existing along every predecessor, and
3418 it's defined by some predecessor, it is
3419 partially redundant. */
3420 if (!cant_insert && by_all)
3422 edge succ;
3423 bool do_insertion = false;
3425 /* Insert only if we can remove a later expression on a path
3426 that we want to optimize for speed.
3427 The phi node that we will be inserting in BLOCK is not free,
3428 and inserting it for the sake of !optimize_for_speed successor
3429 may cause regressions on the speed path. */
3430 FOR_EACH_EDGE (succ, ei, block->succs)
3432 if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3433 || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3435 if (optimize_edge_for_speed_p (succ))
3436 do_insertion = true;
3440 if (!do_insertion)
3442 if (dump_file && (dump_flags & TDF_DETAILS))
3444 fprintf (dump_file, "Skipping partial partial redundancy "
3445 "for expression ");
3446 print_pre_expr (dump_file, expr);
3447 fprintf (dump_file, " (%04d), not (partially) anticipated "
3448 "on any to be optimized for speed edges\n", val);
3451 else if (dbg_cnt (treepre_insert))
3453 pre_stats.pa_insert++;
3454 if (dump_file && (dump_flags & TDF_DETAILS))
3456 fprintf (dump_file, "Found partial partial redundancy "
3457 "for expression ");
3458 print_pre_expr (dump_file, expr);
3459 fprintf (dump_file, " (%04d)\n",
3460 get_expr_value_id (expr));
3462 if (insert_into_preds_of_block (block,
3463 get_expression_id (expr),
3464 avail))
3465 new_stuff = true;
3471 exprs.release ();
3472 return new_stuff;
3475 /* Insert expressions in BLOCK to compute hoistable values up.
3476 Return TRUE if something was inserted, otherwise return FALSE.
3477 The caller has to make sure that BLOCK has at least two successors. */
3479 static bool
3480 do_hoist_insertion (basic_block block)
3482 edge e;
3483 edge_iterator ei;
3484 bool new_stuff = false;
3485 unsigned i;
3486 gimple_stmt_iterator last;
3488 /* At least two successors, or else... */
3489 gcc_assert (EDGE_COUNT (block->succs) >= 2);
3491 /* Check that all successors of BLOCK are dominated by block.
3492 We could use dominated_by_p() for this, but actually there is a much
3493 quicker check: any successor that is dominated by BLOCK can't have
3494 more than one predecessor edge. */
3495 FOR_EACH_EDGE (e, ei, block->succs)
3496 if (! single_pred_p (e->dest))
3497 return false;
3499 /* Determine the insertion point. If we cannot safely insert before
3500 the last stmt if we'd have to, bail out. */
3501 last = gsi_last_bb (block);
3502 if (!gsi_end_p (last)
3503 && !is_ctrl_stmt (gsi_stmt (last))
3504 && stmt_ends_bb_p (gsi_stmt (last)))
3505 return false;
3507 /* Compute the set of hoistable expressions from ANTIC_IN. First compute
3508 hoistable values. */
3509 bitmap_set hoistable_set;
3511 /* A hoistable value must be in ANTIC_IN(block)
3512 but not in AVAIL_OUT(BLOCK). */
3513 bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
3514 bitmap_and_compl (&hoistable_set.values,
3515 &ANTIC_IN (block)->values, &AVAIL_OUT (block)->values);
3517 /* Short-cut for a common case: hoistable_set is empty. */
3518 if (bitmap_empty_p (&hoistable_set.values))
3519 return false;
3521 /* Compute which of the hoistable values is in AVAIL_OUT of
3522 at least one of the successors of BLOCK. */
3523 bitmap_head availout_in_some;
3524 bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
3525 FOR_EACH_EDGE (e, ei, block->succs)
3526 /* Do not consider expressions solely because their availability
3527 on loop exits. They'd be ANTIC-IN throughout the whole loop
3528 and thus effectively hoisted across loops by combination of
3529 PRE and hoisting. */
3530 if (! loop_exit_edge_p (block->loop_father, e))
3531 bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
3532 &AVAIL_OUT (e->dest)->values);
3533 bitmap_clear (&hoistable_set.values);
3535 /* Short-cut for a common case: availout_in_some is empty. */
3536 if (bitmap_empty_p (&availout_in_some))
3537 return false;
3539 /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */
3540 bitmap_move (&hoistable_set.values, &availout_in_some);
3541 hoistable_set.expressions = ANTIC_IN (block)->expressions;
3543 /* Now finally construct the topological-ordered expression set. */
3544 vec<pre_expr> exprs = sorted_array_from_bitmap_set (&hoistable_set);
3546 bitmap_clear (&hoistable_set.values);
3548 /* If there are candidate values for hoisting, insert expressions
3549 strategically to make the hoistable expressions fully redundant. */
3550 pre_expr expr;
3551 FOR_EACH_VEC_ELT (exprs, i, expr)
3553 /* While we try to sort expressions topologically above the
3554 sorting doesn't work out perfectly. Catch expressions we
3555 already inserted. */
3556 unsigned int value_id = get_expr_value_id (expr);
3557 if (bitmap_set_contains_value (AVAIL_OUT (block), value_id))
3559 if (dump_file && (dump_flags & TDF_DETAILS))
3561 fprintf (dump_file,
3562 "Already inserted expression for ");
3563 print_pre_expr (dump_file, expr);
3564 fprintf (dump_file, " (%04d)\n", value_id);
3566 continue;
3569 /* OK, we should hoist this value. Perform the transformation. */
3570 pre_stats.hoist_insert++;
3571 if (dump_file && (dump_flags & TDF_DETAILS))
3573 fprintf (dump_file,
3574 "Inserting expression in block %d for code hoisting: ",
3575 block->index);
3576 print_pre_expr (dump_file, expr);
3577 fprintf (dump_file, " (%04d)\n", value_id);
3580 gimple_seq stmts = NULL;
3581 tree res = create_expression_by_pieces (block, expr, &stmts,
3582 get_expr_type (expr));
3584 /* Do not return true if expression creation ultimately
3585 did not insert any statements. */
3586 if (gimple_seq_empty_p (stmts))
3587 res = NULL_TREE;
3588 else
3590 if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
3591 gsi_insert_seq_before (&last, stmts, GSI_SAME_STMT);
3592 else
3593 gsi_insert_seq_after (&last, stmts, GSI_NEW_STMT);
3596 /* Make sure to not return true if expression creation ultimately
3597 failed but also make sure to insert any stmts produced as they
3598 are tracked in inserted_exprs. */
3599 if (! res)
3600 continue;
3602 new_stuff = true;
3605 exprs.release ();
3607 return new_stuff;
3610 /* Perform insertion of partially redundant and hoistable values. */
3612 static void
3613 insert (void)
3615 basic_block bb;
3617 FOR_ALL_BB_FN (bb, cfun)
3618 NEW_SETS (bb) = bitmap_set_new ();
3620 int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3621 int rpo_num = pre_and_rev_post_order_compute (NULL, rpo, false);
3623 int num_iterations = 0;
3624 bool changed;
3627 num_iterations++;
3628 if (dump_file && dump_flags & TDF_DETAILS)
3629 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3631 changed = false;
3632 for (int idx = 0; idx < rpo_num; ++idx)
3634 basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
3635 basic_block dom = get_immediate_dominator (CDI_DOMINATORS, block);
3636 if (dom)
3638 unsigned i;
3639 bitmap_iterator bi;
3640 bitmap_set_t newset;
3642 /* First, update the AVAIL_OUT set with anything we may have
3643 inserted higher up in the dominator tree. */
3644 newset = NEW_SETS (dom);
3645 if (newset)
3647 /* Note that we need to value_replace both NEW_SETS, and
3648 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3649 represented by some non-simple expression here that we want
3650 to replace it with. */
3651 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3653 pre_expr expr = expression_for_id (i);
3654 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3655 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3659 /* Insert expressions for partial redundancies. */
3660 if (flag_tree_pre && !single_pred_p (block))
3662 changed |= do_pre_regular_insertion (block, dom);
3663 if (do_partial_partial)
3664 changed |= do_pre_partial_partial_insertion (block, dom);
3667 /* Insert expressions for hoisting. */
3668 if (flag_code_hoisting && EDGE_COUNT (block->succs) >= 2)
3669 changed |= do_hoist_insertion (block);
3673 /* Clear the NEW sets before the next iteration. We have already
3674 fully propagated its contents. */
3675 if (changed)
3676 FOR_ALL_BB_FN (bb, cfun)
3677 bitmap_set_free (NEW_SETS (bb));
3679 while (changed);
3681 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3683 free (rpo);
3687 /* Compute the AVAIL set for all basic blocks.
3689 This function performs value numbering of the statements in each basic
3690 block. The AVAIL sets are built from information we glean while doing
3691 this value numbering, since the AVAIL sets contain only one entry per
3692 value.
3694 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3695 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3697 static void
3698 compute_avail (void)
3701 basic_block block, son;
3702 basic_block *worklist;
3703 size_t sp = 0;
3704 unsigned i;
3705 tree name;
3707 /* We pretend that default definitions are defined in the entry block.
3708 This includes function arguments and the static chain decl. */
3709 FOR_EACH_SSA_NAME (i, name, cfun)
3711 pre_expr e;
3712 if (!SSA_NAME_IS_DEFAULT_DEF (name)
3713 || has_zero_uses (name)
3714 || virtual_operand_p (name))
3715 continue;
3717 e = get_or_alloc_expr_for_name (name);
3718 add_to_value (get_expr_value_id (e), e);
3719 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), e);
3720 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3724 if (dump_file && (dump_flags & TDF_DETAILS))
3726 print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3727 "tmp_gen", ENTRY_BLOCK);
3728 print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3729 "avail_out", ENTRY_BLOCK);
3732 /* Allocate the worklist. */
3733 worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
3735 /* Seed the algorithm by putting the dominator children of the entry
3736 block on the worklist. */
3737 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun));
3738 son;
3739 son = next_dom_son (CDI_DOMINATORS, son))
3740 worklist[sp++] = son;
3742 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun))
3743 = ssa_default_def (cfun, gimple_vop (cfun));
3745 /* Loop until the worklist is empty. */
3746 while (sp)
3748 gimple *stmt;
3749 basic_block dom;
3751 /* Pick a block from the worklist. */
3752 block = worklist[--sp];
3753 vn_context_bb = block;
3755 /* Initially, the set of available values in BLOCK is that of
3756 its immediate dominator. */
3757 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3758 if (dom)
3760 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3761 BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
3764 /* Generate values for PHI nodes. */
3765 for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
3766 gsi_next (&gsi))
3768 tree result = gimple_phi_result (gsi.phi ());
3770 /* We have no need for virtual phis, as they don't represent
3771 actual computations. */
3772 if (virtual_operand_p (result))
3774 BB_LIVE_VOP_ON_EXIT (block) = result;
3775 continue;
3778 pre_expr e = get_or_alloc_expr_for_name (result);
3779 add_to_value (get_expr_value_id (e), e);
3780 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3781 bitmap_insert_into_set (PHI_GEN (block), e);
3784 BB_MAY_NOTRETURN (block) = 0;
3786 /* Now compute value numbers and populate value sets with all
3787 the expressions computed in BLOCK. */
3788 for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
3789 gsi_next (&gsi))
3791 ssa_op_iter iter;
3792 tree op;
3794 stmt = gsi_stmt (gsi);
3796 /* Cache whether the basic-block has any non-visible side-effect
3797 or control flow.
3798 If this isn't a call or it is the last stmt in the
3799 basic-block then the CFG represents things correctly. */
3800 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3802 /* Non-looping const functions always return normally.
3803 Otherwise the call might not return or have side-effects
3804 that forbids hoisting possibly trapping expressions
3805 before it. */
3806 int flags = gimple_call_flags (stmt);
3807 if (!(flags & ECF_CONST)
3808 || (flags & ECF_LOOPING_CONST_OR_PURE))
3809 BB_MAY_NOTRETURN (block) = 1;
3812 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3814 pre_expr e = get_or_alloc_expr_for_name (op);
3816 add_to_value (get_expr_value_id (e), e);
3817 bitmap_insert_into_set (TMP_GEN (block), e);
3818 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3821 if (gimple_vdef (stmt))
3822 BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
3824 if (gimple_has_side_effects (stmt)
3825 || stmt_could_throw_p (cfun, stmt)
3826 || is_gimple_debug (stmt))
3827 continue;
3829 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3831 if (ssa_undefined_value_p (op))
3832 continue;
3833 pre_expr e = get_or_alloc_expr_for_name (op);
3834 bitmap_value_insert_into_set (EXP_GEN (block), e);
3837 switch (gimple_code (stmt))
3839 case GIMPLE_RETURN:
3840 continue;
3842 case GIMPLE_CALL:
3844 vn_reference_t ref;
3845 vn_reference_s ref1;
3846 pre_expr result = NULL;
3848 /* We can value number only calls to real functions. */
3849 if (gimple_call_internal_p (stmt))
3850 continue;
3852 vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
3853 if (!ref)
3854 continue;
3856 /* If the value of the call is not invalidated in
3857 this block until it is computed, add the expression
3858 to EXP_GEN. */
3859 if (!gimple_vuse (stmt)
3860 || gimple_code
3861 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3862 || gimple_bb (SSA_NAME_DEF_STMT
3863 (gimple_vuse (stmt))) != block)
3865 result = pre_expr_pool.allocate ();
3866 result->kind = REFERENCE;
3867 result->id = 0;
3868 result->loc = gimple_location (stmt);
3869 PRE_EXPR_REFERENCE (result) = ref;
3871 get_or_alloc_expression_id (result);
3872 add_to_value (get_expr_value_id (result), result);
3873 bitmap_value_insert_into_set (EXP_GEN (block), result);
3875 continue;
3878 case GIMPLE_ASSIGN:
3880 pre_expr result = NULL;
3881 switch (vn_get_stmt_kind (stmt))
3883 case VN_NARY:
3885 enum tree_code code = gimple_assign_rhs_code (stmt);
3886 vn_nary_op_t nary;
3888 /* COND_EXPR and VEC_COND_EXPR are awkward in
3889 that they contain an embedded complex expression.
3890 Don't even try to shove those through PRE. */
3891 if (code == COND_EXPR
3892 || code == VEC_COND_EXPR)
3893 continue;
3895 vn_nary_op_lookup_stmt (stmt, &nary);
3896 if (!nary || nary->predicated_values)
3897 continue;
3899 /* If the NARY traps and there was a preceding
3900 point in the block that might not return avoid
3901 adding the nary to EXP_GEN. */
3902 if (BB_MAY_NOTRETURN (block)
3903 && vn_nary_may_trap (nary))
3904 continue;
3906 result = pre_expr_pool.allocate ();
3907 result->kind = NARY;
3908 result->id = 0;
3909 result->loc = gimple_location (stmt);
3910 PRE_EXPR_NARY (result) = nary;
3911 break;
3914 case VN_REFERENCE:
3916 tree rhs1 = gimple_assign_rhs1 (stmt);
3917 alias_set_type set = get_alias_set (rhs1);
3918 vec<vn_reference_op_s> operands
3919 = vn_reference_operands_for_lookup (rhs1);
3920 vn_reference_t ref;
3921 vn_reference_lookup_pieces (gimple_vuse (stmt), set,
3922 TREE_TYPE (rhs1),
3923 operands, &ref, VN_WALK);
3924 if (!ref)
3926 operands.release ();
3927 continue;
3930 /* If the REFERENCE traps and there was a preceding
3931 point in the block that might not return avoid
3932 adding the reference to EXP_GEN. */
3933 if (BB_MAY_NOTRETURN (block)
3934 && vn_reference_may_trap (ref))
3935 continue;
3937 /* If the value of the reference is not invalidated in
3938 this block until it is computed, add the expression
3939 to EXP_GEN. */
3940 if (gimple_vuse (stmt))
3942 gimple *def_stmt;
3943 bool ok = true;
3944 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
3945 while (!gimple_nop_p (def_stmt)
3946 && gimple_code (def_stmt) != GIMPLE_PHI
3947 && gimple_bb (def_stmt) == block)
3949 if (stmt_may_clobber_ref_p
3950 (def_stmt, gimple_assign_rhs1 (stmt)))
3952 ok = false;
3953 break;
3955 def_stmt
3956 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
3958 if (!ok)
3960 operands.release ();
3961 continue;
3965 /* If the load was value-numbered to another
3966 load make sure we do not use its expression
3967 for insertion if it wouldn't be a valid
3968 replacement. */
3969 /* At the momemt we have a testcase
3970 for hoist insertion of aligned vs. misaligned
3971 variants in gcc.dg/torture/pr65270-1.c thus
3972 with just alignment to be considered we can
3973 simply replace the expression in the hashtable
3974 with the most conservative one. */
3975 vn_reference_op_t ref1 = &ref->operands.last ();
3976 while (ref1->opcode != TARGET_MEM_REF
3977 && ref1->opcode != MEM_REF
3978 && ref1 != &ref->operands[0])
3979 --ref1;
3980 vn_reference_op_t ref2 = &operands.last ();
3981 while (ref2->opcode != TARGET_MEM_REF
3982 && ref2->opcode != MEM_REF
3983 && ref2 != &operands[0])
3984 --ref2;
3985 if ((ref1->opcode == TARGET_MEM_REF
3986 || ref1->opcode == MEM_REF)
3987 && (TYPE_ALIGN (ref1->type)
3988 > TYPE_ALIGN (ref2->type)))
3989 ref1->type
3990 = build_aligned_type (ref1->type,
3991 TYPE_ALIGN (ref2->type));
3992 /* TBAA behavior is an obvious part so make sure
3993 that the hashtable one covers this as well
3994 by adjusting the ref alias set and its base. */
3995 if (ref->set == set
3996 || alias_set_subset_of (set, ref->set))
3998 else if (alias_set_subset_of (ref->set, set))
4000 ref->set = set;
4001 if (ref1->opcode == MEM_REF)
4002 ref1->op0
4003 = wide_int_to_tree (TREE_TYPE (ref2->op0),
4004 wi::to_wide (ref1->op0));
4005 else
4006 ref1->op2
4007 = wide_int_to_tree (TREE_TYPE (ref2->op2),
4008 wi::to_wide (ref1->op2));
4010 else
4012 ref->set = 0;
4013 if (ref1->opcode == MEM_REF)
4014 ref1->op0
4015 = wide_int_to_tree (ptr_type_node,
4016 wi::to_wide (ref1->op0));
4017 else
4018 ref1->op2
4019 = wide_int_to_tree (ptr_type_node,
4020 wi::to_wide (ref1->op2));
4022 operands.release ();
4024 result = pre_expr_pool.allocate ();
4025 result->kind = REFERENCE;
4026 result->id = 0;
4027 result->loc = gimple_location (stmt);
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_edges_for_insertion ();
4196 scev_initialize ();
4197 calculate_dominance_info (CDI_DOMINATORS);
4199 run_rpo_vn (VN_WALK);
4201 init_pre ();
4203 vn_valueize = pre_valueize;
4205 /* Insert can get quite slow on an incredibly large number of basic
4206 blocks due to some quadratic behavior. Until this behavior is
4207 fixed, don't run it when he have an incredibly large number of
4208 bb's. If we aren't going to run insert, there is no point in
4209 computing ANTIC, either, even though it's plenty fast nor do
4210 we require AVAIL. */
4211 if (n_basic_blocks_for_fn (fun) < 4000)
4213 compute_avail ();
4214 compute_antic ();
4215 insert ();
4218 /* Make sure to remove fake edges before committing our inserts.
4219 This makes sure we don't end up with extra critical edges that
4220 we would need to split. */
4221 remove_fake_exit_edges ();
4222 gsi_commit_edge_inserts ();
4224 /* Eliminate folds statements which might (should not...) end up
4225 not keeping virtual operands up-to-date. */
4226 gcc_assert (!need_ssa_update_p (fun));
4228 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4229 statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4230 statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
4231 statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4233 todo |= eliminate_with_rpo_vn (inserted_exprs);
4235 vn_valueize = NULL;
4237 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4238 to insert PHI nodes sometimes, and because value numbering of casts isn't
4239 perfect, we sometimes end up inserting dead code. This simple DCE-like
4240 pass removes any insertions we made that weren't actually used. */
4241 simple_dce_from_worklist (inserted_exprs);
4243 fini_pre ();
4245 scev_finalize ();
4246 loop_optimizer_finalize ();
4248 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4249 case we can merge the block with the remaining predecessor of the block.
4250 It should either:
4251 - call merge_blocks after each tail merge iteration
4252 - call merge_blocks after all tail merge iterations
4253 - mark TODO_cleanup_cfg when necessary
4254 - share the cfg cleanup with fini_pre. */
4255 todo |= tail_merge_optimize (todo);
4257 free_rpo_vn ();
4259 /* Tail merging invalidates the virtual SSA web, together with
4260 cfg-cleanup opportunities exposed by PRE this will wreck the
4261 SSA updating machinery. So make sure to run update-ssa
4262 manually, before eventually scheduling cfg-cleanup as part of
4263 the todo. */
4264 update_ssa (TODO_update_ssa_only_virtuals);
4266 return todo;
4269 } // anon namespace
4271 gimple_opt_pass *
4272 make_pass_pre (gcc::context *ctxt)
4274 return new pass_pre (ctxt);