Daily bump.
[official-gcc.git] / gcc / tree-ssa-pre.c
blob63f3a81e94c2056f8a548e580e8f99c5bb12511b
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, true);
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, alias_set_type base_set,
1144 tree type, tree vuse,
1145 basic_block phiblock,
1146 basic_block block, bool *same_valid)
1148 gimple *phi = SSA_NAME_DEF_STMT (vuse);
1149 ao_ref ref;
1150 edge e = NULL;
1151 bool use_oracle;
1153 if (same_valid)
1154 *same_valid = true;
1156 if (gimple_bb (phi) != phiblock)
1157 return vuse;
1159 unsigned int cnt = param_sccvn_max_alias_queries_per_access;
1160 use_oracle = ao_ref_init_from_vn_reference (&ref, set, base_set,
1161 type, operands);
1163 /* Use the alias-oracle to find either the PHI node in this block,
1164 the first VUSE used in this block that is equivalent to vuse or
1165 the first VUSE which definition in this block kills the value. */
1166 if (gimple_code (phi) == GIMPLE_PHI)
1167 e = find_edge (block, phiblock);
1168 else if (use_oracle)
1169 while (cnt > 0
1170 && !stmt_may_clobber_ref_p_1 (phi, &ref))
1172 --cnt;
1173 vuse = gimple_vuse (phi);
1174 phi = SSA_NAME_DEF_STMT (vuse);
1175 if (gimple_bb (phi) != phiblock)
1176 return vuse;
1177 if (gimple_code (phi) == GIMPLE_PHI)
1179 e = find_edge (block, phiblock);
1180 break;
1183 else
1184 return NULL_TREE;
1186 if (e)
1188 if (use_oracle && same_valid)
1190 bitmap visited = NULL;
1191 /* Try to find a vuse that dominates this phi node by skipping
1192 non-clobbering statements. */
1193 vuse = get_continuation_for_phi (phi, &ref, true,
1194 cnt, &visited, false, NULL, NULL);
1195 if (visited)
1196 BITMAP_FREE (visited);
1198 else
1199 vuse = NULL_TREE;
1200 /* If we didn't find any, the value ID can't stay the same. */
1201 if (!vuse && same_valid)
1202 *same_valid = false;
1203 /* ??? We would like to return vuse here as this is the canonical
1204 upmost vdef that this reference is associated with. But during
1205 insertion of the references into the hash tables we only ever
1206 directly insert with their direct gimple_vuse, hence returning
1207 something else would make us not find the other expression. */
1208 return PHI_ARG_DEF (phi, e->dest_idx);
1211 return NULL_TREE;
1214 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1215 SET2 *or* SET3. This is used to avoid making a set consisting of the union
1216 of PA_IN and ANTIC_IN during insert and phi-translation. */
1218 static inline pre_expr
1219 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
1220 bitmap_set_t set3 = NULL)
1222 pre_expr result = NULL;
1224 if (set1)
1225 result = bitmap_find_leader (set1, val);
1226 if (!result && set2)
1227 result = bitmap_find_leader (set2, val);
1228 if (!result && set3)
1229 result = bitmap_find_leader (set3, val);
1230 return result;
1233 /* Get the tree type for our PRE expression e. */
1235 static tree
1236 get_expr_type (const pre_expr e)
1238 switch (e->kind)
1240 case NAME:
1241 return TREE_TYPE (PRE_EXPR_NAME (e));
1242 case CONSTANT:
1243 return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1244 case REFERENCE:
1245 return PRE_EXPR_REFERENCE (e)->type;
1246 case NARY:
1247 return PRE_EXPR_NARY (e)->type;
1249 gcc_unreachable ();
1252 /* Get a representative SSA_NAME for a given expression that is available in B.
1253 Since all of our sub-expressions are treated as values, we require
1254 them to be SSA_NAME's for simplicity.
1255 Prior versions of GVNPRE used to use "value handles" here, so that
1256 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1257 either case, the operands are really values (IE we do not expect
1258 them to be usable without finding leaders). */
1260 static tree
1261 get_representative_for (const pre_expr e, basic_block b = NULL)
1263 tree name, valnum = NULL_TREE;
1264 unsigned int value_id = get_expr_value_id (e);
1266 switch (e->kind)
1268 case NAME:
1269 return PRE_EXPR_NAME (e);
1270 case CONSTANT:
1271 return PRE_EXPR_CONSTANT (e);
1272 case NARY:
1273 case REFERENCE:
1275 /* Go through all of the expressions representing this value
1276 and pick out an SSA_NAME. */
1277 unsigned int i;
1278 bitmap_iterator bi;
1279 bitmap exprs = value_expressions[value_id];
1280 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1282 pre_expr rep = expression_for_id (i);
1283 if (rep->kind == NAME)
1285 tree name = PRE_EXPR_NAME (rep);
1286 valnum = VN_INFO (name)->valnum;
1287 gimple *def = SSA_NAME_DEF_STMT (name);
1288 /* We have to return either a new representative or one
1289 that can be used for expression simplification and thus
1290 is available in B. */
1291 if (! b
1292 || gimple_nop_p (def)
1293 || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
1294 return name;
1296 else if (rep->kind == CONSTANT)
1297 return PRE_EXPR_CONSTANT (rep);
1300 break;
1303 /* If we reached here we couldn't find an SSA_NAME. This can
1304 happen when we've discovered a value that has never appeared in
1305 the program as set to an SSA_NAME, as the result of phi translation.
1306 Create one here.
1307 ??? We should be able to re-use this when we insert the statement
1308 to compute it. */
1309 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1310 VN_INFO (name)->value_id = value_id;
1311 VN_INFO (name)->valnum = valnum ? valnum : name;
1312 /* ??? For now mark this SSA name for release by VN. */
1313 VN_INFO (name)->needs_insertion = true;
1314 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1315 if (dump_file && (dump_flags & TDF_DETAILS))
1317 fprintf (dump_file, "Created SSA_NAME representative ");
1318 print_generic_expr (dump_file, name);
1319 fprintf (dump_file, " for expression:");
1320 print_pre_expr (dump_file, e);
1321 fprintf (dump_file, " (%04d)\n", value_id);
1324 return name;
1328 static pre_expr
1329 phi_translate (bitmap_set_t, pre_expr, bitmap_set_t, bitmap_set_t, edge);
1331 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1332 the phis in PRED. Return NULL if we can't find a leader for each part
1333 of the translated expression. */
1335 static pre_expr
1336 phi_translate_1 (bitmap_set_t dest,
1337 pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
1339 basic_block pred = e->src;
1340 basic_block phiblock = e->dest;
1341 location_t expr_loc = expr->loc;
1342 switch (expr->kind)
1344 case NARY:
1346 unsigned int i;
1347 bool changed = false;
1348 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1349 vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1350 sizeof_vn_nary_op (nary->length));
1351 memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1353 for (i = 0; i < newnary->length; i++)
1355 if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1356 continue;
1357 else
1359 pre_expr leader, result;
1360 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1361 leader = find_leader_in_sets (op_val_id, set1, set2);
1362 result = phi_translate (dest, leader, set1, set2, e);
1363 if (result && result != leader)
1364 /* If op has a leader in the sets we translate make
1365 sure to use the value of the translated expression.
1366 We might need a new representative for that. */
1367 newnary->op[i] = get_representative_for (result, pred);
1368 else if (!result)
1369 return NULL;
1371 changed |= newnary->op[i] != nary->op[i];
1374 if (changed)
1376 pre_expr constant;
1377 unsigned int new_val_id;
1379 PRE_EXPR_NARY (expr) = newnary;
1380 constant = fully_constant_expression (expr);
1381 PRE_EXPR_NARY (expr) = nary;
1382 if (constant != expr)
1384 /* For non-CONSTANTs we have to make sure we can eventually
1385 insert the expression. Which means we need to have a
1386 leader for it. */
1387 if (constant->kind != CONSTANT)
1389 /* Do not allow simplifications to non-constants over
1390 backedges as this will likely result in a loop PHI node
1391 to be inserted and increased register pressure.
1392 See PR77498 - this avoids doing predcoms work in
1393 a less efficient way. */
1394 if (e->flags & EDGE_DFS_BACK)
1396 else
1398 unsigned value_id = get_expr_value_id (constant);
1399 /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1400 dest has what we computed into ANTIC_OUT sofar
1401 so pick from that - since topological sorting
1402 by sorted_array_from_bitmap_set isn't perfect
1403 we may lose some cases here. */
1404 constant = find_leader_in_sets (value_id, dest,
1405 AVAIL_OUT (pred));
1406 if (constant)
1408 if (dump_file && (dump_flags & TDF_DETAILS))
1410 fprintf (dump_file, "simplifying ");
1411 print_pre_expr (dump_file, expr);
1412 fprintf (dump_file, " translated %d -> %d to ",
1413 phiblock->index, pred->index);
1414 PRE_EXPR_NARY (expr) = newnary;
1415 print_pre_expr (dump_file, expr);
1416 PRE_EXPR_NARY (expr) = nary;
1417 fprintf (dump_file, " to ");
1418 print_pre_expr (dump_file, constant);
1419 fprintf (dump_file, "\n");
1421 return constant;
1425 else
1426 return constant;
1429 /* vn_nary_* do not valueize operands. */
1430 for (i = 0; i < newnary->length; ++i)
1431 if (TREE_CODE (newnary->op[i]) == SSA_NAME)
1432 newnary->op[i] = VN_INFO (newnary->op[i])->valnum;
1433 tree result = vn_nary_op_lookup_pieces (newnary->length,
1434 newnary->opcode,
1435 newnary->type,
1436 &newnary->op[0],
1437 &nary);
1438 if (result && is_gimple_min_invariant (result))
1439 return get_or_alloc_expr_for_constant (result);
1441 expr = pre_expr_pool.allocate ();
1442 expr->kind = NARY;
1443 expr->id = 0;
1444 expr->loc = expr_loc;
1445 if (nary && !nary->predicated_values)
1447 PRE_EXPR_NARY (expr) = nary;
1448 new_val_id = nary->value_id;
1449 get_or_alloc_expression_id (expr);
1451 else
1453 new_val_id = get_next_value_id ();
1454 value_expressions.safe_grow_cleared (get_max_value_id () + 1,
1455 true);
1456 nary = vn_nary_op_insert_pieces (newnary->length,
1457 newnary->opcode,
1458 newnary->type,
1459 &newnary->op[0],
1460 result, new_val_id);
1461 PRE_EXPR_NARY (expr) = nary;
1462 get_or_alloc_expression_id (expr);
1464 add_to_value (new_val_id, expr);
1466 return expr;
1468 break;
1470 case REFERENCE:
1472 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1473 vec<vn_reference_op_s> operands = ref->operands;
1474 tree vuse = ref->vuse;
1475 tree newvuse = vuse;
1476 vec<vn_reference_op_s> newoperands = vNULL;
1477 bool changed = false, same_valid = true;
1478 unsigned int i, n;
1479 vn_reference_op_t operand;
1480 vn_reference_t newref;
1482 for (i = 0; operands.iterate (i, &operand); i++)
1484 pre_expr opresult;
1485 pre_expr leader;
1486 tree op[3];
1487 tree type = operand->type;
1488 vn_reference_op_s newop = *operand;
1489 op[0] = operand->op0;
1490 op[1] = operand->op1;
1491 op[2] = operand->op2;
1492 for (n = 0; n < 3; ++n)
1494 unsigned int op_val_id;
1495 if (!op[n])
1496 continue;
1497 if (TREE_CODE (op[n]) != SSA_NAME)
1499 /* We can't possibly insert these. */
1500 if (n != 0
1501 && !is_gimple_min_invariant (op[n]))
1502 break;
1503 continue;
1505 op_val_id = VN_INFO (op[n])->value_id;
1506 leader = find_leader_in_sets (op_val_id, set1, set2);
1507 opresult = phi_translate (dest, leader, set1, set2, e);
1508 if (opresult && opresult != leader)
1510 tree name = get_representative_for (opresult);
1511 changed |= name != op[n];
1512 op[n] = name;
1514 else if (!opresult)
1515 break;
1517 if (n != 3)
1519 newoperands.release ();
1520 return NULL;
1522 if (!changed)
1523 continue;
1524 if (!newoperands.exists ())
1525 newoperands = operands.copy ();
1526 /* We may have changed from an SSA_NAME to a constant */
1527 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1528 newop.opcode = TREE_CODE (op[0]);
1529 newop.type = type;
1530 newop.op0 = op[0];
1531 newop.op1 = op[1];
1532 newop.op2 = op[2];
1533 newoperands[i] = newop;
1535 gcc_checking_assert (i == operands.length ());
1537 if (vuse)
1539 newvuse = translate_vuse_through_block (newoperands.exists ()
1540 ? newoperands : operands,
1541 ref->set, ref->base_set,
1542 ref->type,
1543 vuse, phiblock, pred,
1544 changed
1545 ? NULL : &same_valid);
1546 if (newvuse == NULL_TREE)
1548 newoperands.release ();
1549 return NULL;
1553 if (changed || newvuse != vuse)
1555 unsigned int new_val_id;
1557 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1558 ref->base_set,
1559 ref->type,
1560 newoperands.exists ()
1561 ? newoperands : operands,
1562 &newref, VN_WALK);
1563 if (result)
1564 newoperands.release ();
1566 /* We can always insert constants, so if we have a partial
1567 redundant constant load of another type try to translate it
1568 to a constant of appropriate type. */
1569 if (result && is_gimple_min_invariant (result))
1571 tree tem = result;
1572 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1574 tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1575 if (tem && !is_gimple_min_invariant (tem))
1576 tem = NULL_TREE;
1578 if (tem)
1579 return get_or_alloc_expr_for_constant (tem);
1582 /* If we'd have to convert things we would need to validate
1583 if we can insert the translated expression. So fail
1584 here for now - we cannot insert an alias with a different
1585 type in the VN tables either, as that would assert. */
1586 if (result
1587 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1588 return NULL;
1589 else if (!result && newref
1590 && !useless_type_conversion_p (ref->type, newref->type))
1592 newoperands.release ();
1593 return NULL;
1596 expr = pre_expr_pool.allocate ();
1597 expr->kind = REFERENCE;
1598 expr->id = 0;
1599 expr->loc = expr_loc;
1601 if (newref)
1602 new_val_id = newref->value_id;
1603 else
1605 if (changed || !same_valid)
1607 new_val_id = get_next_value_id ();
1608 value_expressions.safe_grow_cleared
1609 (get_max_value_id () + 1, true);
1611 else
1612 new_val_id = ref->value_id;
1613 if (!newoperands.exists ())
1614 newoperands = operands.copy ();
1615 newref = vn_reference_insert_pieces (newvuse, ref->set,
1616 ref->base_set, ref->type,
1617 newoperands,
1618 result, new_val_id);
1619 newoperands = vNULL;
1621 PRE_EXPR_REFERENCE (expr) = newref;
1622 get_or_alloc_expression_id (expr);
1623 add_to_value (new_val_id, expr);
1625 newoperands.release ();
1626 return expr;
1628 break;
1630 case NAME:
1632 tree name = PRE_EXPR_NAME (expr);
1633 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1634 /* If the SSA name is defined by a PHI node in this block,
1635 translate it. */
1636 if (gimple_code (def_stmt) == GIMPLE_PHI
1637 && gimple_bb (def_stmt) == phiblock)
1639 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1641 /* Handle constant. */
1642 if (is_gimple_min_invariant (def))
1643 return get_or_alloc_expr_for_constant (def);
1645 return get_or_alloc_expr_for_name (def);
1647 /* Otherwise return it unchanged - it will get removed if its
1648 value is not available in PREDs AVAIL_OUT set of expressions
1649 by the subtraction of TMP_GEN. */
1650 return expr;
1653 default:
1654 gcc_unreachable ();
1658 /* Wrapper around phi_translate_1 providing caching functionality. */
1660 static pre_expr
1661 phi_translate (bitmap_set_t dest, pre_expr expr,
1662 bitmap_set_t set1, bitmap_set_t set2, edge e)
1664 expr_pred_trans_t slot = NULL;
1665 pre_expr phitrans;
1667 if (!expr)
1668 return NULL;
1670 /* Constants contain no values that need translation. */
1671 if (expr->kind == CONSTANT)
1672 return expr;
1674 if (value_id_constant_p (get_expr_value_id (expr)))
1675 return expr;
1677 /* Don't add translations of NAMEs as those are cheap to translate. */
1678 if (expr->kind != NAME)
1680 if (phi_trans_add (&slot, expr, e->src))
1681 return slot->v;
1682 /* Store NULL for the value we want to return in the case of
1683 recursing. */
1684 slot->v = NULL;
1687 /* Translate. */
1688 basic_block saved_valueize_bb = vn_context_bb;
1689 vn_context_bb = e->src;
1690 phitrans = phi_translate_1 (dest, expr, set1, set2, e);
1691 vn_context_bb = saved_valueize_bb;
1693 if (slot)
1695 if (phitrans)
1696 slot->v = phitrans;
1697 else
1698 /* Remove failed translations again, they cause insert
1699 iteration to not pick up new opportunities reliably. */
1700 phi_translate_table->remove_elt_with_hash (slot, slot->hashcode);
1703 return phitrans;
1707 /* For each expression in SET, translate the values through phi nodes
1708 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1709 expressions in DEST. */
1711 static void
1712 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
1714 vec<pre_expr> exprs;
1715 pre_expr expr;
1716 int i;
1718 if (gimple_seq_empty_p (phi_nodes (e->dest)))
1720 bitmap_set_copy (dest, set);
1721 return;
1724 exprs = sorted_array_from_bitmap_set (set);
1725 FOR_EACH_VEC_ELT (exprs, i, expr)
1727 pre_expr translated;
1728 translated = phi_translate (dest, expr, set, NULL, e);
1729 if (!translated)
1730 continue;
1732 bitmap_insert_into_set (dest, translated);
1734 exprs.release ();
1737 /* Find the leader for a value (i.e., the name representing that
1738 value) in a given set, and return it. Return NULL if no leader
1739 is found. */
1741 static pre_expr
1742 bitmap_find_leader (bitmap_set_t set, unsigned int val)
1744 if (value_id_constant_p (val))
1746 unsigned int i;
1747 bitmap_iterator bi;
1748 bitmap exprset = value_expressions[val];
1750 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1752 pre_expr expr = expression_for_id (i);
1753 if (expr->kind == CONSTANT)
1754 return expr;
1757 if (bitmap_set_contains_value (set, val))
1759 /* Rather than walk the entire bitmap of expressions, and see
1760 whether any of them has the value we are looking for, we look
1761 at the reverse mapping, which tells us the set of expressions
1762 that have a given value (IE value->expressions with that
1763 value) and see if any of those expressions are in our set.
1764 The number of expressions per value is usually significantly
1765 less than the number of expressions in the set. In fact, for
1766 large testcases, doing it this way is roughly 5-10x faster
1767 than walking the bitmap.
1768 If this is somehow a significant lose for some cases, we can
1769 choose which set to walk based on which set is smaller. */
1770 unsigned int i;
1771 bitmap_iterator bi;
1772 bitmap exprset = value_expressions[val];
1774 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1775 return expression_for_id (i);
1777 return NULL;
1780 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1781 BLOCK by seeing if it is not killed in the block. Note that we are
1782 only determining whether there is a store that kills it. Because
1783 of the order in which clean iterates over values, we are guaranteed
1784 that altered operands will have caused us to be eliminated from the
1785 ANTIC_IN set already. */
1787 static bool
1788 value_dies_in_block_x (pre_expr expr, basic_block block)
1790 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1791 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1792 gimple *def;
1793 gimple_stmt_iterator gsi;
1794 unsigned id = get_expression_id (expr);
1795 bool res = false;
1796 ao_ref ref;
1798 if (!vuse)
1799 return false;
1801 /* Lookup a previously calculated result. */
1802 if (EXPR_DIES (block)
1803 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1804 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1806 /* A memory expression {e, VUSE} dies in the block if there is a
1807 statement that may clobber e. If, starting statement walk from the
1808 top of the basic block, a statement uses VUSE there can be no kill
1809 inbetween that use and the original statement that loaded {e, VUSE},
1810 so we can stop walking. */
1811 ref.base = NULL_TREE;
1812 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1814 tree def_vuse, def_vdef;
1815 def = gsi_stmt (gsi);
1816 def_vuse = gimple_vuse (def);
1817 def_vdef = gimple_vdef (def);
1819 /* Not a memory statement. */
1820 if (!def_vuse)
1821 continue;
1823 /* Not a may-def. */
1824 if (!def_vdef)
1826 /* A load with the same VUSE, we're done. */
1827 if (def_vuse == vuse)
1828 break;
1830 continue;
1833 /* Init ref only if we really need it. */
1834 if (ref.base == NULL_TREE
1835 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->base_set,
1836 refx->type, refx->operands))
1838 res = true;
1839 break;
1841 /* If the statement may clobber expr, it dies. */
1842 if (stmt_may_clobber_ref_p_1 (def, &ref))
1844 res = true;
1845 break;
1849 /* Remember the result. */
1850 if (!EXPR_DIES (block))
1851 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1852 bitmap_set_bit (EXPR_DIES (block), id * 2);
1853 if (res)
1854 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1856 return res;
1860 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1861 contains its value-id. */
1863 static bool
1864 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1866 if (op && TREE_CODE (op) == SSA_NAME)
1868 unsigned int value_id = VN_INFO (op)->value_id;
1869 if (!(bitmap_set_contains_value (set1, value_id)
1870 || (set2 && bitmap_set_contains_value (set2, value_id))))
1871 return false;
1873 return true;
1876 /* Determine if the expression EXPR is valid in SET1 U SET2.
1877 ONLY SET2 CAN BE NULL.
1878 This means that we have a leader for each part of the expression
1879 (if it consists of values), or the expression is an SSA_NAME.
1880 For loads/calls, we also see if the vuse is killed in this block. */
1882 static bool
1883 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
1885 switch (expr->kind)
1887 case NAME:
1888 /* By construction all NAMEs are available. Non-available
1889 NAMEs are removed by subtracting TMP_GEN from the sets. */
1890 return true;
1891 case NARY:
1893 unsigned int i;
1894 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1895 for (i = 0; i < nary->length; i++)
1896 if (!op_valid_in_sets (set1, set2, nary->op[i]))
1897 return false;
1898 return true;
1900 break;
1901 case REFERENCE:
1903 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1904 vn_reference_op_t vro;
1905 unsigned int i;
1907 FOR_EACH_VEC_ELT (ref->operands, i, vro)
1909 if (!op_valid_in_sets (set1, set2, vro->op0)
1910 || !op_valid_in_sets (set1, set2, vro->op1)
1911 || !op_valid_in_sets (set1, set2, vro->op2))
1912 return false;
1914 return true;
1916 default:
1917 gcc_unreachable ();
1921 /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
1922 This means expressions that are made up of values we have no leaders for
1923 in SET1 or SET2. */
1925 static void
1926 clean (bitmap_set_t set1, bitmap_set_t set2 = NULL)
1928 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
1929 pre_expr expr;
1930 int i;
1932 FOR_EACH_VEC_ELT (exprs, i, expr)
1934 if (!valid_in_sets (set1, set2, expr))
1936 unsigned int val = get_expr_value_id (expr);
1937 bitmap_clear_bit (&set1->expressions, get_expression_id (expr));
1938 /* We are entered with possibly multiple expressions for a value
1939 so before removing a value from the set see if there's an
1940 expression for it left. */
1941 if (! bitmap_find_leader (set1, val))
1942 bitmap_clear_bit (&set1->values, val);
1945 exprs.release ();
1948 /* Clean the set of expressions that are no longer valid in SET because
1949 they are clobbered in BLOCK or because they trap and may not be executed. */
1951 static void
1952 prune_clobbered_mems (bitmap_set_t set, basic_block block)
1954 bitmap_iterator bi;
1955 unsigned i;
1956 unsigned to_remove = -1U;
1957 bool any_removed = false;
1959 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1961 /* Remove queued expr. */
1962 if (to_remove != -1U)
1964 bitmap_clear_bit (&set->expressions, to_remove);
1965 any_removed = true;
1966 to_remove = -1U;
1969 pre_expr expr = expression_for_id (i);
1970 if (expr->kind == REFERENCE)
1972 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1973 if (ref->vuse)
1975 gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
1976 if (!gimple_nop_p (def_stmt)
1977 && ((gimple_bb (def_stmt) != block
1978 && !dominated_by_p (CDI_DOMINATORS,
1979 block, gimple_bb (def_stmt)))
1980 || (gimple_bb (def_stmt) == block
1981 && value_dies_in_block_x (expr, block))))
1982 to_remove = i;
1985 else if (expr->kind == NARY)
1987 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1988 /* If the NARY may trap make sure the block does not contain
1989 a possible exit point.
1990 ??? This is overly conservative if we translate AVAIL_OUT
1991 as the available expression might be after the exit point. */
1992 if (BB_MAY_NOTRETURN (block)
1993 && vn_nary_may_trap (nary))
1994 to_remove = i;
1998 /* Remove queued expr. */
1999 if (to_remove != -1U)
2001 bitmap_clear_bit (&set->expressions, to_remove);
2002 any_removed = true;
2005 /* Above we only removed expressions, now clean the set of values
2006 which no longer have any corresponding expression. We cannot
2007 clear the value at the time we remove an expression since there
2008 may be multiple expressions per value.
2009 If we'd queue possibly to be removed values we could use
2010 the bitmap_find_leader way to see if there's still an expression
2011 for it. For some ratio of to be removed values and number of
2012 values/expressions in the set this might be faster than rebuilding
2013 the value-set. */
2014 if (any_removed)
2016 bitmap_clear (&set->values);
2017 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2019 pre_expr expr = expression_for_id (i);
2020 unsigned int value_id = get_expr_value_id (expr);
2021 bitmap_set_bit (&set->values, value_id);
2026 /* Compute the ANTIC set for BLOCK.
2028 If succs(BLOCK) > 1 then
2029 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2030 else if succs(BLOCK) == 1 then
2031 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2033 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2035 Note that clean() is deferred until after the iteration. */
2037 static bool
2038 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2040 bitmap_set_t S, old, ANTIC_OUT;
2041 edge e;
2042 edge_iterator ei;
2044 bool was_visited = BB_VISITED (block);
2045 bool changed = ! BB_VISITED (block);
2046 BB_VISITED (block) = 1;
2047 old = ANTIC_OUT = S = NULL;
2049 /* If any edges from predecessors are abnormal, antic_in is empty,
2050 so do nothing. */
2051 if (block_has_abnormal_pred_edge)
2052 goto maybe_dump_sets;
2054 old = ANTIC_IN (block);
2055 ANTIC_OUT = bitmap_set_new ();
2057 /* If the block has no successors, ANTIC_OUT is empty. */
2058 if (EDGE_COUNT (block->succs) == 0)
2060 /* If we have one successor, we could have some phi nodes to
2061 translate through. */
2062 else if (single_succ_p (block))
2064 e = single_succ_edge (block);
2065 gcc_assert (BB_VISITED (e->dest));
2066 phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
2068 /* If we have multiple successors, we take the intersection of all of
2069 them. Note that in the case of loop exit phi nodes, we may have
2070 phis to translate through. */
2071 else
2073 size_t i;
2074 edge first = NULL;
2076 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2077 FOR_EACH_EDGE (e, ei, block->succs)
2079 if (!first
2080 && BB_VISITED (e->dest))
2081 first = e;
2082 else if (BB_VISITED (e->dest))
2083 worklist.quick_push (e);
2084 else
2086 /* Unvisited successors get their ANTIC_IN replaced by the
2087 maximal set to arrive at a maximum ANTIC_IN solution.
2088 We can ignore them in the intersection operation and thus
2089 need not explicitely represent that maximum solution. */
2090 if (dump_file && (dump_flags & TDF_DETAILS))
2091 fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n",
2092 e->src->index, e->dest->index);
2096 /* Of multiple successors we have to have visited one already
2097 which is guaranteed by iteration order. */
2098 gcc_assert (first != NULL);
2100 phi_translate_set (ANTIC_OUT, ANTIC_IN (first->dest), first);
2102 /* If we have multiple successors we need to intersect the ANTIC_OUT
2103 sets. For values that's a simple intersection but for
2104 expressions it is a union. Given we want to have a single
2105 expression per value in our sets we have to canonicalize.
2106 Avoid randomness and running into cycles like for PR82129 and
2107 canonicalize the expression we choose to the one with the
2108 lowest id. This requires we actually compute the union first. */
2109 FOR_EACH_VEC_ELT (worklist, i, e)
2111 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2113 bitmap_set_t tmp = bitmap_set_new ();
2114 phi_translate_set (tmp, ANTIC_IN (e->dest), e);
2115 bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
2116 bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
2117 bitmap_set_free (tmp);
2119 else
2121 bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
2122 bitmap_ior_into (&ANTIC_OUT->expressions,
2123 &ANTIC_IN (e->dest)->expressions);
2126 if (! worklist.is_empty ())
2128 /* Prune expressions not in the value set. */
2129 bitmap_iterator bi;
2130 unsigned int i;
2131 unsigned int to_clear = -1U;
2132 FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
2134 if (to_clear != -1U)
2136 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2137 to_clear = -1U;
2139 pre_expr expr = expression_for_id (i);
2140 unsigned int value_id = get_expr_value_id (expr);
2141 if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
2142 to_clear = i;
2144 if (to_clear != -1U)
2145 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2149 /* Prune expressions that are clobbered in block and thus become
2150 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2151 prune_clobbered_mems (ANTIC_OUT, block);
2153 /* Generate ANTIC_OUT - TMP_GEN. */
2154 S = bitmap_set_subtract_expressions (ANTIC_OUT, TMP_GEN (block));
2156 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2157 ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
2158 TMP_GEN (block));
2160 /* Then union in the ANTIC_OUT - TMP_GEN values,
2161 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2162 bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
2163 bitmap_ior_into (&ANTIC_IN (block)->expressions, &S->expressions);
2165 /* clean (ANTIC_IN (block)) is defered to after the iteration converged
2166 because it can cause non-convergence, see for example PR81181. */
2168 /* Intersect ANTIC_IN with the old ANTIC_IN. This is required until
2169 we properly represent the maximum expression set, thus not prune
2170 values without expressions during the iteration. */
2171 if (was_visited
2172 && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
2174 if (dump_file && (dump_flags & TDF_DETAILS))
2175 fprintf (dump_file, "warning: intersecting with old ANTIC_IN "
2176 "shrinks the set\n");
2177 /* Prune expressions not in the value set. */
2178 bitmap_iterator bi;
2179 unsigned int i;
2180 unsigned int to_clear = -1U;
2181 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
2183 if (to_clear != -1U)
2185 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2186 to_clear = -1U;
2188 pre_expr expr = expression_for_id (i);
2189 unsigned int value_id = get_expr_value_id (expr);
2190 if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
2191 to_clear = i;
2193 if (to_clear != -1U)
2194 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2197 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2198 changed = true;
2200 maybe_dump_sets:
2201 if (dump_file && (dump_flags & TDF_DETAILS))
2203 if (ANTIC_OUT)
2204 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2206 if (changed)
2207 fprintf (dump_file, "[changed] ");
2208 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2209 block->index);
2211 if (S)
2212 print_bitmap_set (dump_file, S, "S", block->index);
2214 if (old)
2215 bitmap_set_free (old);
2216 if (S)
2217 bitmap_set_free (S);
2218 if (ANTIC_OUT)
2219 bitmap_set_free (ANTIC_OUT);
2220 return changed;
2223 /* Compute PARTIAL_ANTIC for BLOCK.
2225 If succs(BLOCK) > 1 then
2226 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2227 in ANTIC_OUT for all succ(BLOCK)
2228 else if succs(BLOCK) == 1 then
2229 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2231 PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
2234 static void
2235 compute_partial_antic_aux (basic_block block,
2236 bool block_has_abnormal_pred_edge)
2238 bitmap_set_t old_PA_IN;
2239 bitmap_set_t PA_OUT;
2240 edge e;
2241 edge_iterator ei;
2242 unsigned long max_pa = param_max_partial_antic_length;
2244 old_PA_IN = PA_OUT = NULL;
2246 /* If any edges from predecessors are abnormal, antic_in is empty,
2247 so do nothing. */
2248 if (block_has_abnormal_pred_edge)
2249 goto maybe_dump_sets;
2251 /* If there are too many partially anticipatable values in the
2252 block, phi_translate_set can take an exponential time: stop
2253 before the translation starts. */
2254 if (max_pa
2255 && single_succ_p (block)
2256 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2257 goto maybe_dump_sets;
2259 old_PA_IN = PA_IN (block);
2260 PA_OUT = bitmap_set_new ();
2262 /* If the block has no successors, ANTIC_OUT is empty. */
2263 if (EDGE_COUNT (block->succs) == 0)
2265 /* If we have one successor, we could have some phi nodes to
2266 translate through. Note that we can't phi translate across DFS
2267 back edges in partial antic, because it uses a union operation on
2268 the successors. For recurrences like IV's, we will end up
2269 generating a new value in the set on each go around (i + 3 (VH.1)
2270 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2271 else if (single_succ_p (block))
2273 e = single_succ_edge (block);
2274 if (!(e->flags & EDGE_DFS_BACK))
2275 phi_translate_set (PA_OUT, PA_IN (e->dest), e);
2277 /* If we have multiple successors, we take the union of all of
2278 them. */
2279 else
2281 size_t i;
2283 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2284 FOR_EACH_EDGE (e, ei, block->succs)
2286 if (e->flags & EDGE_DFS_BACK)
2287 continue;
2288 worklist.quick_push (e);
2290 if (worklist.length () > 0)
2292 FOR_EACH_VEC_ELT (worklist, i, e)
2294 unsigned int i;
2295 bitmap_iterator bi;
2297 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
2298 bitmap_value_insert_into_set (PA_OUT,
2299 expression_for_id (i));
2300 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2302 bitmap_set_t pa_in = bitmap_set_new ();
2303 phi_translate_set (pa_in, PA_IN (e->dest), e);
2304 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2305 bitmap_value_insert_into_set (PA_OUT,
2306 expression_for_id (i));
2307 bitmap_set_free (pa_in);
2309 else
2310 FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
2311 bitmap_value_insert_into_set (PA_OUT,
2312 expression_for_id (i));
2317 /* Prune expressions that are clobbered in block and thus become
2318 invalid if translated from PA_OUT to PA_IN. */
2319 prune_clobbered_mems (PA_OUT, block);
2321 /* PA_IN starts with PA_OUT - TMP_GEN.
2322 Then we subtract things from ANTIC_IN. */
2323 PA_IN (block) = bitmap_set_subtract_expressions (PA_OUT, TMP_GEN (block));
2325 /* For partial antic, we want to put back in the phi results, since
2326 we will properly avoid making them partially antic over backedges. */
2327 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2328 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2330 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2331 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2333 clean (PA_IN (block), ANTIC_IN (block));
2335 maybe_dump_sets:
2336 if (dump_file && (dump_flags & TDF_DETAILS))
2338 if (PA_OUT)
2339 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2341 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2343 if (old_PA_IN)
2344 bitmap_set_free (old_PA_IN);
2345 if (PA_OUT)
2346 bitmap_set_free (PA_OUT);
2349 /* Compute ANTIC and partial ANTIC sets. */
2351 static void
2352 compute_antic (void)
2354 bool changed = true;
2355 int num_iterations = 0;
2356 basic_block block;
2357 int i;
2358 edge_iterator ei;
2359 edge e;
2361 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2362 We pre-build the map of blocks with incoming abnormal edges here. */
2363 auto_sbitmap has_abnormal_preds (last_basic_block_for_fn (cfun));
2364 bitmap_clear (has_abnormal_preds);
2366 FOR_ALL_BB_FN (block, cfun)
2368 BB_VISITED (block) = 0;
2370 FOR_EACH_EDGE (e, ei, block->preds)
2371 if (e->flags & EDGE_ABNORMAL)
2373 bitmap_set_bit (has_abnormal_preds, block->index);
2374 break;
2377 /* While we are here, give empty ANTIC_IN sets to each block. */
2378 ANTIC_IN (block) = bitmap_set_new ();
2379 if (do_partial_partial)
2380 PA_IN (block) = bitmap_set_new ();
2383 /* At the exit block we anticipate nothing. */
2384 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2386 /* For ANTIC computation we need a postorder that also guarantees that
2387 a block with a single successor is visited after its successor.
2388 RPO on the inverted CFG has this property. */
2389 auto_vec<int, 20> postorder;
2390 inverted_post_order_compute (&postorder);
2392 auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
2393 bitmap_clear (worklist);
2394 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2395 bitmap_set_bit (worklist, e->src->index);
2396 while (changed)
2398 if (dump_file && (dump_flags & TDF_DETAILS))
2399 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2400 /* ??? We need to clear our PHI translation cache here as the
2401 ANTIC sets shrink and we restrict valid translations to
2402 those having operands with leaders in ANTIC. Same below
2403 for PA ANTIC computation. */
2404 num_iterations++;
2405 changed = false;
2406 for (i = postorder.length () - 1; i >= 0; i--)
2408 if (bitmap_bit_p (worklist, postorder[i]))
2410 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2411 bitmap_clear_bit (worklist, block->index);
2412 if (compute_antic_aux (block,
2413 bitmap_bit_p (has_abnormal_preds,
2414 block->index)))
2416 FOR_EACH_EDGE (e, ei, block->preds)
2417 bitmap_set_bit (worklist, e->src->index);
2418 changed = true;
2422 /* Theoretically possible, but *highly* unlikely. */
2423 gcc_checking_assert (num_iterations < 500);
2426 /* We have to clean after the dataflow problem converged as cleaning
2427 can cause non-convergence because it is based on expressions
2428 rather than values. */
2429 FOR_EACH_BB_FN (block, cfun)
2430 clean (ANTIC_IN (block));
2432 statistics_histogram_event (cfun, "compute_antic iterations",
2433 num_iterations);
2435 if (do_partial_partial)
2437 /* For partial antic we ignore backedges and thus we do not need
2438 to perform any iteration when we process blocks in postorder. */
2439 for (i = postorder.length () - 1; i >= 0; i--)
2441 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2442 compute_partial_antic_aux (block,
2443 bitmap_bit_p (has_abnormal_preds,
2444 block->index));
2450 /* Inserted expressions are placed onto this worklist, which is used
2451 for performing quick dead code elimination of insertions we made
2452 that didn't turn out to be necessary. */
2453 static bitmap inserted_exprs;
2455 /* The actual worker for create_component_ref_by_pieces. */
2457 static tree
2458 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2459 unsigned int *operand, gimple_seq *stmts)
2461 vn_reference_op_t currop = &ref->operands[*operand];
2462 tree genop;
2463 ++*operand;
2464 switch (currop->opcode)
2466 case CALL_EXPR:
2467 gcc_unreachable ();
2469 case MEM_REF:
2471 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2472 stmts);
2473 if (!baseop)
2474 return NULL_TREE;
2475 tree offset = currop->op0;
2476 if (TREE_CODE (baseop) == ADDR_EXPR
2477 && handled_component_p (TREE_OPERAND (baseop, 0)))
2479 poly_int64 off;
2480 tree base;
2481 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2482 &off);
2483 gcc_assert (base);
2484 offset = int_const_binop (PLUS_EXPR, offset,
2485 build_int_cst (TREE_TYPE (offset),
2486 off));
2487 baseop = build_fold_addr_expr (base);
2489 genop = build2 (MEM_REF, currop->type, baseop, offset);
2490 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2491 MR_DEPENDENCE_BASE (genop) = currop->base;
2492 REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
2493 return genop;
2496 case TARGET_MEM_REF:
2498 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2499 vn_reference_op_t nextop = &ref->operands[(*operand)++];
2500 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2501 stmts);
2502 if (!baseop)
2503 return NULL_TREE;
2504 if (currop->op0)
2506 genop0 = find_or_generate_expression (block, currop->op0, stmts);
2507 if (!genop0)
2508 return NULL_TREE;
2510 if (nextop->op0)
2512 genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2513 if (!genop1)
2514 return NULL_TREE;
2516 genop = build5 (TARGET_MEM_REF, currop->type,
2517 baseop, currop->op2, genop0, currop->op1, genop1);
2519 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2520 MR_DEPENDENCE_BASE (genop) = currop->base;
2521 return genop;
2524 case ADDR_EXPR:
2525 if (currop->op0)
2527 gcc_assert (is_gimple_min_invariant (currop->op0));
2528 return currop->op0;
2530 /* Fallthrough. */
2531 case REALPART_EXPR:
2532 case IMAGPART_EXPR:
2533 case VIEW_CONVERT_EXPR:
2535 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2536 stmts);
2537 if (!genop0)
2538 return NULL_TREE;
2539 return fold_build1 (currop->opcode, currop->type, genop0);
2542 case WITH_SIZE_EXPR:
2544 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2545 stmts);
2546 if (!genop0)
2547 return NULL_TREE;
2548 tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2549 if (!genop1)
2550 return NULL_TREE;
2551 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2554 case BIT_FIELD_REF:
2556 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2557 stmts);
2558 if (!genop0)
2559 return NULL_TREE;
2560 tree op1 = currop->op0;
2561 tree op2 = currop->op1;
2562 tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2563 REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
2564 return fold (t);
2567 /* For array ref vn_reference_op's, operand 1 of the array ref
2568 is op0 of the reference op and operand 3 of the array ref is
2569 op1. */
2570 case ARRAY_RANGE_REF:
2571 case ARRAY_REF:
2573 tree genop0;
2574 tree genop1 = currop->op0;
2575 tree genop2 = currop->op1;
2576 tree genop3 = currop->op2;
2577 genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2578 stmts);
2579 if (!genop0)
2580 return NULL_TREE;
2581 genop1 = find_or_generate_expression (block, genop1, stmts);
2582 if (!genop1)
2583 return NULL_TREE;
2584 if (genop2)
2586 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2587 /* Drop zero minimum index if redundant. */
2588 if (integer_zerop (genop2)
2589 && (!domain_type
2590 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2591 genop2 = NULL_TREE;
2592 else
2594 genop2 = find_or_generate_expression (block, genop2, stmts);
2595 if (!genop2)
2596 return NULL_TREE;
2599 if (genop3)
2601 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2602 /* We can't always put a size in units of the element alignment
2603 here as the element alignment may be not visible. See
2604 PR43783. Simply drop the element size for constant
2605 sizes. */
2606 if (TREE_CODE (genop3) == INTEGER_CST
2607 && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
2608 && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
2609 (wi::to_offset (genop3)
2610 * vn_ref_op_align_unit (currop))))
2611 genop3 = NULL_TREE;
2612 else
2614 genop3 = find_or_generate_expression (block, genop3, stmts);
2615 if (!genop3)
2616 return NULL_TREE;
2619 return build4 (currop->opcode, currop->type, genop0, genop1,
2620 genop2, genop3);
2622 case COMPONENT_REF:
2624 tree op0;
2625 tree op1;
2626 tree genop2 = currop->op1;
2627 op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2628 if (!op0)
2629 return NULL_TREE;
2630 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2631 op1 = currop->op0;
2632 if (genop2)
2634 genop2 = find_or_generate_expression (block, genop2, stmts);
2635 if (!genop2)
2636 return NULL_TREE;
2638 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2641 case SSA_NAME:
2643 genop = find_or_generate_expression (block, currop->op0, stmts);
2644 return genop;
2646 case STRING_CST:
2647 case INTEGER_CST:
2648 case POLY_INT_CST:
2649 case COMPLEX_CST:
2650 case VECTOR_CST:
2651 case REAL_CST:
2652 case CONSTRUCTOR:
2653 case VAR_DECL:
2654 case PARM_DECL:
2655 case CONST_DECL:
2656 case RESULT_DECL:
2657 case FUNCTION_DECL:
2658 return currop->op0;
2660 default:
2661 gcc_unreachable ();
2665 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2666 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2667 trying to rename aggregates into ssa form directly, which is a no no.
2669 Thus, this routine doesn't create temporaries, it just builds a
2670 single access expression for the array, calling
2671 find_or_generate_expression to build the innermost pieces.
2673 This function is a subroutine of create_expression_by_pieces, and
2674 should not be called on it's own unless you really know what you
2675 are doing. */
2677 static tree
2678 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2679 gimple_seq *stmts)
2681 unsigned int op = 0;
2682 return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2685 /* Find a simple leader for an expression, or generate one using
2686 create_expression_by_pieces from a NARY expression for the value.
2687 BLOCK is the basic_block we are looking for leaders in.
2688 OP is the tree expression to find a leader for or generate.
2689 Returns the leader or NULL_TREE on failure. */
2691 static tree
2692 find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2694 pre_expr expr = get_or_alloc_expr_for (op);
2695 unsigned int lookfor = get_expr_value_id (expr);
2696 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2697 if (leader)
2699 if (leader->kind == NAME)
2700 return PRE_EXPR_NAME (leader);
2701 else if (leader->kind == CONSTANT)
2702 return PRE_EXPR_CONSTANT (leader);
2704 /* Defer. */
2705 return NULL_TREE;
2708 /* It must be a complex expression, so generate it recursively. Note
2709 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2710 where the insert algorithm fails to insert a required expression. */
2711 bitmap exprset = value_expressions[lookfor];
2712 bitmap_iterator bi;
2713 unsigned int i;
2714 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2716 pre_expr temp = expression_for_id (i);
2717 /* We cannot insert random REFERENCE expressions at arbitrary
2718 places. We can insert NARYs which eventually re-materializes
2719 its operand values. */
2720 if (temp->kind == NARY)
2721 return create_expression_by_pieces (block, temp, stmts,
2722 get_expr_type (expr));
2725 /* Defer. */
2726 return NULL_TREE;
2729 /* Create an expression in pieces, so that we can handle very complex
2730 expressions that may be ANTIC, but not necessary GIMPLE.
2731 BLOCK is the basic block the expression will be inserted into,
2732 EXPR is the expression to insert (in value form)
2733 STMTS is a statement list to append the necessary insertions into.
2735 This function will die if we hit some value that shouldn't be
2736 ANTIC but is (IE there is no leader for it, or its components).
2737 The function returns NULL_TREE in case a different antic expression
2738 has to be inserted first.
2739 This function may also generate expressions that are themselves
2740 partially or fully redundant. Those that are will be either made
2741 fully redundant during the next iteration of insert (for partially
2742 redundant ones), or eliminated by eliminate (for fully redundant
2743 ones). */
2745 static tree
2746 create_expression_by_pieces (basic_block block, pre_expr expr,
2747 gimple_seq *stmts, tree type)
2749 tree name;
2750 tree folded;
2751 gimple_seq forced_stmts = NULL;
2752 unsigned int value_id;
2753 gimple_stmt_iterator gsi;
2754 tree exprtype = type ? type : get_expr_type (expr);
2755 pre_expr nameexpr;
2756 gassign *newstmt;
2758 switch (expr->kind)
2760 /* We may hit the NAME/CONSTANT case if we have to convert types
2761 that value numbering saw through. */
2762 case NAME:
2763 folded = PRE_EXPR_NAME (expr);
2764 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
2765 return NULL_TREE;
2766 if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2767 return folded;
2768 break;
2769 case CONSTANT:
2771 folded = PRE_EXPR_CONSTANT (expr);
2772 tree tem = fold_convert (exprtype, folded);
2773 if (is_gimple_min_invariant (tem))
2774 return tem;
2775 break;
2777 case REFERENCE:
2778 if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
2780 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2781 unsigned int operand = 1;
2782 vn_reference_op_t currop = &ref->operands[0];
2783 tree sc = NULL_TREE;
2784 tree fn = find_or_generate_expression (block, currop->op0, stmts);
2785 if (!fn)
2786 return NULL_TREE;
2787 if (currop->op1)
2789 sc = find_or_generate_expression (block, currop->op1, stmts);
2790 if (!sc)
2791 return NULL_TREE;
2793 auto_vec<tree> args (ref->operands.length () - 1);
2794 while (operand < ref->operands.length ())
2796 tree arg = create_component_ref_by_pieces_1 (block, ref,
2797 &operand, stmts);
2798 if (!arg)
2799 return NULL_TREE;
2800 args.quick_push (arg);
2802 gcall *call = gimple_build_call_vec (fn, args);
2803 gimple_set_location (call, expr->loc);
2804 gimple_call_set_fntype (call, currop->type);
2805 if (sc)
2806 gimple_call_set_chain (call, sc);
2807 tree forcedname = make_ssa_name (TREE_TYPE (currop->type));
2808 gimple_call_set_lhs (call, forcedname);
2809 /* There's no CCP pass after PRE which would re-compute alignment
2810 information so make sure we re-materialize this here. */
2811 if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED)
2812 && args.length () - 2 <= 1
2813 && tree_fits_uhwi_p (args[1])
2814 && (args.length () != 3 || tree_fits_uhwi_p (args[2])))
2816 unsigned HOST_WIDE_INT halign = tree_to_uhwi (args[1]);
2817 unsigned HOST_WIDE_INT hmisalign
2818 = args.length () == 3 ? tree_to_uhwi (args[2]) : 0;
2819 if ((halign & (halign - 1)) == 0
2820 && (hmisalign & ~(halign - 1)) == 0
2821 && (unsigned int)halign != 0)
2822 set_ptr_info_alignment (get_ptr_info (forcedname),
2823 halign, hmisalign);
2825 gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
2826 gimple_seq_add_stmt_without_update (&forced_stmts, call);
2827 folded = forcedname;
2829 else
2831 folded = create_component_ref_by_pieces (block,
2832 PRE_EXPR_REFERENCE (expr),
2833 stmts);
2834 if (!folded)
2835 return NULL_TREE;
2836 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2837 newstmt = gimple_build_assign (name, folded);
2838 gimple_set_location (newstmt, expr->loc);
2839 gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2840 gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
2841 folded = name;
2843 break;
2844 case NARY:
2846 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2847 tree *genop = XALLOCAVEC (tree, nary->length);
2848 unsigned i;
2849 for (i = 0; i < nary->length; ++i)
2851 genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2852 if (!genop[i])
2853 return NULL_TREE;
2854 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2855 may have conversions stripped. */
2856 if (nary->opcode == POINTER_PLUS_EXPR)
2858 if (i == 0)
2859 genop[i] = gimple_convert (&forced_stmts,
2860 nary->type, genop[i]);
2861 else if (i == 1)
2862 genop[i] = gimple_convert (&forced_stmts,
2863 sizetype, genop[i]);
2865 else
2866 genop[i] = gimple_convert (&forced_stmts,
2867 TREE_TYPE (nary->op[i]), genop[i]);
2869 if (nary->opcode == CONSTRUCTOR)
2871 vec<constructor_elt, va_gc> *elts = NULL;
2872 for (i = 0; i < nary->length; ++i)
2873 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2874 folded = build_constructor (nary->type, elts);
2875 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2876 newstmt = gimple_build_assign (name, folded);
2877 gimple_set_location (newstmt, expr->loc);
2878 gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2879 folded = name;
2881 else
2883 switch (nary->length)
2885 case 1:
2886 folded = gimple_build (&forced_stmts, expr->loc,
2887 nary->opcode, nary->type, genop[0]);
2888 break;
2889 case 2:
2890 folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
2891 nary->type, genop[0], genop[1]);
2892 break;
2893 case 3:
2894 folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
2895 nary->type, genop[0], genop[1],
2896 genop[2]);
2897 break;
2898 default:
2899 gcc_unreachable ();
2903 break;
2904 default:
2905 gcc_unreachable ();
2908 folded = gimple_convert (&forced_stmts, exprtype, folded);
2910 /* If there is nothing to insert, return the simplified result. */
2911 if (gimple_seq_empty_p (forced_stmts))
2912 return folded;
2913 /* If we simplified to a constant return it and discard eventually
2914 built stmts. */
2915 if (is_gimple_min_invariant (folded))
2917 gimple_seq_discard (forced_stmts);
2918 return folded;
2920 /* Likewise if we simplified to sth not queued for insertion. */
2921 bool found = false;
2922 gsi = gsi_last (forced_stmts);
2923 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2925 gimple *stmt = gsi_stmt (gsi);
2926 tree forcedname = gimple_get_lhs (stmt);
2927 if (forcedname == folded)
2929 found = true;
2930 break;
2933 if (! found)
2935 gimple_seq_discard (forced_stmts);
2936 return folded;
2938 gcc_assert (TREE_CODE (folded) == SSA_NAME);
2940 /* If we have any intermediate expressions to the value sets, add them
2941 to the value sets and chain them in the instruction stream. */
2942 if (forced_stmts)
2944 gsi = gsi_start (forced_stmts);
2945 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2947 gimple *stmt = gsi_stmt (gsi);
2948 tree forcedname = gimple_get_lhs (stmt);
2949 pre_expr nameexpr;
2951 if (forcedname != folded)
2953 VN_INFO (forcedname)->valnum = forcedname;
2954 VN_INFO (forcedname)->value_id = get_next_value_id ();
2955 nameexpr = get_or_alloc_expr_for_name (forcedname);
2956 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
2957 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2958 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2961 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
2963 gimple_seq_add_seq (stmts, forced_stmts);
2966 name = folded;
2968 /* Fold the last statement. */
2969 gsi = gsi_last (*stmts);
2970 if (fold_stmt_inplace (&gsi))
2971 update_stmt (gsi_stmt (gsi));
2973 /* Add a value number to the temporary.
2974 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2975 we are creating the expression by pieces, and this particular piece of
2976 the expression may have been represented. There is no harm in replacing
2977 here. */
2978 value_id = get_expr_value_id (expr);
2979 VN_INFO (name)->value_id = value_id;
2980 VN_INFO (name)->valnum = vn_valnum_from_value_id (value_id);
2981 if (VN_INFO (name)->valnum == NULL_TREE)
2982 VN_INFO (name)->valnum = name;
2983 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
2984 nameexpr = get_or_alloc_expr_for_name (name);
2985 add_to_value (value_id, nameexpr);
2986 if (NEW_SETS (block))
2987 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2988 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2990 pre_stats.insertions++;
2991 if (dump_file && (dump_flags & TDF_DETAILS))
2993 fprintf (dump_file, "Inserted ");
2994 print_gimple_stmt (dump_file, gsi_stmt (gsi_last (*stmts)), 0);
2995 fprintf (dump_file, " in predecessor %d (%04d)\n",
2996 block->index, value_id);
2999 return name;
3003 /* Insert the to-be-made-available values of expression EXPRNUM for each
3004 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3005 merge the result with a phi node, given the same value number as
3006 NODE. Return true if we have inserted new stuff. */
3008 static bool
3009 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3010 vec<pre_expr> avail)
3012 pre_expr expr = expression_for_id (exprnum);
3013 pre_expr newphi;
3014 unsigned int val = get_expr_value_id (expr);
3015 edge pred;
3016 bool insertions = false;
3017 bool nophi = false;
3018 basic_block bprime;
3019 pre_expr eprime;
3020 edge_iterator ei;
3021 tree type = get_expr_type (expr);
3022 tree temp;
3023 gphi *phi;
3025 /* Make sure we aren't creating an induction variable. */
3026 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3028 bool firstinsideloop = false;
3029 bool secondinsideloop = false;
3030 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3031 EDGE_PRED (block, 0)->src);
3032 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3033 EDGE_PRED (block, 1)->src);
3034 /* Induction variables only have one edge inside the loop. */
3035 if ((firstinsideloop ^ secondinsideloop)
3036 && expr->kind != REFERENCE)
3038 if (dump_file && (dump_flags & TDF_DETAILS))
3039 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3040 nophi = true;
3044 /* Make the necessary insertions. */
3045 FOR_EACH_EDGE (pred, ei, block->preds)
3047 gimple_seq stmts = NULL;
3048 tree builtexpr;
3049 bprime = pred->src;
3050 eprime = avail[pred->dest_idx];
3051 builtexpr = create_expression_by_pieces (bprime, eprime,
3052 &stmts, type);
3053 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3054 if (!gimple_seq_empty_p (stmts))
3056 basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
3057 gcc_assert (! new_bb);
3058 insertions = true;
3060 if (!builtexpr)
3062 /* We cannot insert a PHI node if we failed to insert
3063 on one edge. */
3064 nophi = true;
3065 continue;
3067 if (is_gimple_min_invariant (builtexpr))
3068 avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
3069 else
3070 avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3072 /* If we didn't want a phi node, and we made insertions, we still have
3073 inserted new stuff, and thus return true. If we didn't want a phi node,
3074 and didn't make insertions, we haven't added anything new, so return
3075 false. */
3076 if (nophi && insertions)
3077 return true;
3078 else if (nophi && !insertions)
3079 return false;
3081 /* Now build a phi for the new variable. */
3082 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3083 phi = create_phi_node (temp, block);
3085 VN_INFO (temp)->value_id = val;
3086 VN_INFO (temp)->valnum = vn_valnum_from_value_id (val);
3087 if (VN_INFO (temp)->valnum == NULL_TREE)
3088 VN_INFO (temp)->valnum = temp;
3089 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3090 FOR_EACH_EDGE (pred, ei, block->preds)
3092 pre_expr ae = avail[pred->dest_idx];
3093 gcc_assert (get_expr_type (ae) == type
3094 || useless_type_conversion_p (type, get_expr_type (ae)));
3095 if (ae->kind == CONSTANT)
3096 add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3097 pred, UNKNOWN_LOCATION);
3098 else
3099 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3102 newphi = get_or_alloc_expr_for_name (temp);
3103 add_to_value (val, newphi);
3105 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3106 this insertion, since we test for the existence of this value in PHI_GEN
3107 before proceeding with the partial redundancy checks in insert_aux.
3109 The value may exist in AVAIL_OUT, in particular, it could be represented
3110 by the expression we are trying to eliminate, in which case we want the
3111 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3112 inserted there.
3114 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3115 this block, because if it did, it would have existed in our dominator's
3116 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3119 bitmap_insert_into_set (PHI_GEN (block), newphi);
3120 bitmap_value_replace_in_set (AVAIL_OUT (block),
3121 newphi);
3122 bitmap_insert_into_set (NEW_SETS (block),
3123 newphi);
3125 /* If we insert a PHI node for a conversion of another PHI node
3126 in the same basic-block try to preserve range information.
3127 This is important so that followup loop passes receive optimal
3128 number of iteration analysis results. See PR61743. */
3129 if (expr->kind == NARY
3130 && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3131 && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3132 && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3133 && INTEGRAL_TYPE_P (type)
3134 && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3135 && (TYPE_PRECISION (type)
3136 >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3137 && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3139 wide_int min, max;
3140 if (get_range_info (expr->u.nary->op[0], &min, &max) == VR_RANGE
3141 && !wi::neg_p (min, SIGNED)
3142 && !wi::neg_p (max, SIGNED))
3143 /* Just handle extension and sign-changes of all-positive ranges. */
3144 set_range_info (temp,
3145 SSA_NAME_RANGE_TYPE (expr->u.nary->op[0]),
3146 wide_int_storage::from (min, TYPE_PRECISION (type),
3147 TYPE_SIGN (type)),
3148 wide_int_storage::from (max, TYPE_PRECISION (type),
3149 TYPE_SIGN (type)));
3152 if (dump_file && (dump_flags & TDF_DETAILS))
3154 fprintf (dump_file, "Created phi ");
3155 print_gimple_stmt (dump_file, phi, 0);
3156 fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3158 pre_stats.phis++;
3159 return true;
3164 /* Perform insertion of partially redundant or hoistable values.
3165 For BLOCK, do the following:
3166 1. Propagate the NEW_SETS of the dominator into the current block.
3167 If the block has multiple predecessors,
3168 2a. Iterate over the ANTIC expressions for the block to see if
3169 any of them are partially redundant.
3170 2b. If so, insert them into the necessary predecessors to make
3171 the expression fully redundant.
3172 2c. Insert a new PHI merging the values of the predecessors.
3173 2d. Insert the new PHI, and the new expressions, into the
3174 NEW_SETS set.
3175 If the block has multiple successors,
3176 3a. Iterate over the ANTIC values for the block to see if
3177 any of them are good candidates for hoisting.
3178 3b. If so, insert expressions computing the values in BLOCK,
3179 and add the new expressions into the NEW_SETS set.
3180 4. Recursively call ourselves on the dominator children of BLOCK.
3182 Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3183 do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3184 done in do_hoist_insertion.
3187 static bool
3188 do_pre_regular_insertion (basic_block block, basic_block dom)
3190 bool new_stuff = false;
3191 vec<pre_expr> exprs;
3192 pre_expr expr;
3193 auto_vec<pre_expr> avail;
3194 int i;
3196 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3197 avail.safe_grow (EDGE_COUNT (block->preds), true);
3199 FOR_EACH_VEC_ELT (exprs, i, expr)
3201 if (expr->kind == NARY
3202 || expr->kind == REFERENCE)
3204 unsigned int val;
3205 bool by_some = false;
3206 bool cant_insert = false;
3207 bool all_same = true;
3208 pre_expr first_s = NULL;
3209 edge pred;
3210 basic_block bprime;
3211 pre_expr eprime = NULL;
3212 edge_iterator ei;
3213 pre_expr edoubleprime = NULL;
3214 bool do_insertion = false;
3216 val = get_expr_value_id (expr);
3217 if (bitmap_set_contains_value (PHI_GEN (block), val))
3218 continue;
3219 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3221 if (dump_file && (dump_flags & TDF_DETAILS))
3223 fprintf (dump_file, "Found fully redundant value: ");
3224 print_pre_expr (dump_file, expr);
3225 fprintf (dump_file, "\n");
3227 continue;
3230 FOR_EACH_EDGE (pred, ei, block->preds)
3232 unsigned int vprime;
3234 /* We should never run insertion for the exit block
3235 and so not come across fake pred edges. */
3236 gcc_assert (!(pred->flags & EDGE_FAKE));
3237 bprime = pred->src;
3238 /* We are looking at ANTIC_OUT of bprime. */
3239 eprime = phi_translate (NULL, expr, ANTIC_IN (block), NULL, pred);
3241 /* eprime will generally only be NULL if the
3242 value of the expression, translated
3243 through the PHI for this predecessor, is
3244 undefined. If that is the case, we can't
3245 make the expression fully redundant,
3246 because its value is undefined along a
3247 predecessor path. We can thus break out
3248 early because it doesn't matter what the
3249 rest of the results are. */
3250 if (eprime == NULL)
3252 avail[pred->dest_idx] = NULL;
3253 cant_insert = true;
3254 break;
3257 vprime = get_expr_value_id (eprime);
3258 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3259 vprime);
3260 if (edoubleprime == NULL)
3262 avail[pred->dest_idx] = eprime;
3263 all_same = false;
3265 else
3267 avail[pred->dest_idx] = edoubleprime;
3268 by_some = true;
3269 /* We want to perform insertions to remove a redundancy on
3270 a path in the CFG we want to optimize for speed. */
3271 if (optimize_edge_for_speed_p (pred))
3272 do_insertion = true;
3273 if (first_s == NULL)
3274 first_s = edoubleprime;
3275 else if (!pre_expr_d::equal (first_s, edoubleprime))
3276 all_same = false;
3279 /* If we can insert it, it's not the same value
3280 already existing along every predecessor, and
3281 it's defined by some predecessor, it is
3282 partially redundant. */
3283 if (!cant_insert && !all_same && by_some)
3285 if (!do_insertion)
3287 if (dump_file && (dump_flags & TDF_DETAILS))
3289 fprintf (dump_file, "Skipping partial redundancy for "
3290 "expression ");
3291 print_pre_expr (dump_file, expr);
3292 fprintf (dump_file, " (%04d), no redundancy on to be "
3293 "optimized for speed edge\n", val);
3296 else if (dbg_cnt (treepre_insert))
3298 if (dump_file && (dump_flags & TDF_DETAILS))
3300 fprintf (dump_file, "Found partial redundancy for "
3301 "expression ");
3302 print_pre_expr (dump_file, expr);
3303 fprintf (dump_file, " (%04d)\n",
3304 get_expr_value_id (expr));
3306 if (insert_into_preds_of_block (block,
3307 get_expression_id (expr),
3308 avail))
3309 new_stuff = true;
3312 /* If all edges produce the same value and that value is
3313 an invariant, then the PHI has the same value on all
3314 edges. Note this. */
3315 else if (!cant_insert && all_same)
3317 gcc_assert (edoubleprime->kind == CONSTANT
3318 || edoubleprime->kind == NAME);
3320 tree temp = make_temp_ssa_name (get_expr_type (expr),
3321 NULL, "pretmp");
3322 gassign *assign
3323 = gimple_build_assign (temp,
3324 edoubleprime->kind == CONSTANT ?
3325 PRE_EXPR_CONSTANT (edoubleprime) :
3326 PRE_EXPR_NAME (edoubleprime));
3327 gimple_stmt_iterator gsi = gsi_after_labels (block);
3328 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3330 VN_INFO (temp)->value_id = val;
3331 VN_INFO (temp)->valnum = vn_valnum_from_value_id (val);
3332 if (VN_INFO (temp)->valnum == NULL_TREE)
3333 VN_INFO (temp)->valnum = temp;
3334 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3335 pre_expr newe = get_or_alloc_expr_for_name (temp);
3336 add_to_value (val, newe);
3337 bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3338 bitmap_insert_into_set (NEW_SETS (block), newe);
3343 exprs.release ();
3344 return new_stuff;
3348 /* Perform insertion for partially anticipatable expressions. There
3349 is only one case we will perform insertion for these. This case is
3350 if the expression is partially anticipatable, and fully available.
3351 In this case, we know that putting it earlier will enable us to
3352 remove the later computation. */
3354 static bool
3355 do_pre_partial_partial_insertion (basic_block block, basic_block dom)
3357 bool new_stuff = false;
3358 vec<pre_expr> exprs;
3359 pre_expr expr;
3360 auto_vec<pre_expr> avail;
3361 int i;
3363 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3364 avail.safe_grow (EDGE_COUNT (block->preds), true);
3366 FOR_EACH_VEC_ELT (exprs, i, expr)
3368 if (expr->kind == NARY
3369 || expr->kind == REFERENCE)
3371 unsigned int val;
3372 bool by_all = true;
3373 bool cant_insert = false;
3374 edge pred;
3375 basic_block bprime;
3376 pre_expr eprime = NULL;
3377 edge_iterator ei;
3379 val = get_expr_value_id (expr);
3380 if (bitmap_set_contains_value (PHI_GEN (block), val))
3381 continue;
3382 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3383 continue;
3385 FOR_EACH_EDGE (pred, ei, block->preds)
3387 unsigned int vprime;
3388 pre_expr edoubleprime;
3390 /* We should never run insertion for the exit block
3391 and so not come across fake pred edges. */
3392 gcc_assert (!(pred->flags & EDGE_FAKE));
3393 bprime = pred->src;
3394 eprime = phi_translate (NULL, expr, ANTIC_IN (block),
3395 PA_IN (block), pred);
3397 /* eprime will generally only be NULL if the
3398 value of the expression, translated
3399 through the PHI for this predecessor, is
3400 undefined. If that is the case, we can't
3401 make the expression fully redundant,
3402 because its value is undefined along a
3403 predecessor path. We can thus break out
3404 early because it doesn't matter what the
3405 rest of the results are. */
3406 if (eprime == NULL)
3408 avail[pred->dest_idx] = NULL;
3409 cant_insert = true;
3410 break;
3413 vprime = get_expr_value_id (eprime);
3414 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3415 avail[pred->dest_idx] = edoubleprime;
3416 if (edoubleprime == NULL)
3418 by_all = false;
3419 break;
3423 /* If we can insert it, it's not the same value
3424 already existing along every predecessor, and
3425 it's defined by some predecessor, it is
3426 partially redundant. */
3427 if (!cant_insert && by_all)
3429 edge succ;
3430 bool do_insertion = false;
3432 /* Insert only if we can remove a later expression on a path
3433 that we want to optimize for speed.
3434 The phi node that we will be inserting in BLOCK is not free,
3435 and inserting it for the sake of !optimize_for_speed successor
3436 may cause regressions on the speed path. */
3437 FOR_EACH_EDGE (succ, ei, block->succs)
3439 if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3440 || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3442 if (optimize_edge_for_speed_p (succ))
3443 do_insertion = true;
3447 if (!do_insertion)
3449 if (dump_file && (dump_flags & TDF_DETAILS))
3451 fprintf (dump_file, "Skipping partial partial redundancy "
3452 "for expression ");
3453 print_pre_expr (dump_file, expr);
3454 fprintf (dump_file, " (%04d), not (partially) anticipated "
3455 "on any to be optimized for speed edges\n", val);
3458 else if (dbg_cnt (treepre_insert))
3460 pre_stats.pa_insert++;
3461 if (dump_file && (dump_flags & TDF_DETAILS))
3463 fprintf (dump_file, "Found partial partial redundancy "
3464 "for expression ");
3465 print_pre_expr (dump_file, expr);
3466 fprintf (dump_file, " (%04d)\n",
3467 get_expr_value_id (expr));
3469 if (insert_into_preds_of_block (block,
3470 get_expression_id (expr),
3471 avail))
3472 new_stuff = true;
3478 exprs.release ();
3479 return new_stuff;
3482 /* Insert expressions in BLOCK to compute hoistable values up.
3483 Return TRUE if something was inserted, otherwise return FALSE.
3484 The caller has to make sure that BLOCK has at least two successors. */
3486 static bool
3487 do_hoist_insertion (basic_block block)
3489 edge e;
3490 edge_iterator ei;
3491 bool new_stuff = false;
3492 unsigned i;
3493 gimple_stmt_iterator last;
3495 /* At least two successors, or else... */
3496 gcc_assert (EDGE_COUNT (block->succs) >= 2);
3498 /* Check that all successors of BLOCK are dominated by block.
3499 We could use dominated_by_p() for this, but actually there is a much
3500 quicker check: any successor that is dominated by BLOCK can't have
3501 more than one predecessor edge. */
3502 FOR_EACH_EDGE (e, ei, block->succs)
3503 if (! single_pred_p (e->dest))
3504 return false;
3506 /* Determine the insertion point. If we cannot safely insert before
3507 the last stmt if we'd have to, bail out. */
3508 last = gsi_last_bb (block);
3509 if (!gsi_end_p (last)
3510 && !is_ctrl_stmt (gsi_stmt (last))
3511 && stmt_ends_bb_p (gsi_stmt (last)))
3512 return false;
3514 /* Compute the set of hoistable expressions from ANTIC_IN. First compute
3515 hoistable values. */
3516 bitmap_set hoistable_set;
3518 /* A hoistable value must be in ANTIC_IN(block)
3519 but not in AVAIL_OUT(BLOCK). */
3520 bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
3521 bitmap_and_compl (&hoistable_set.values,
3522 &ANTIC_IN (block)->values, &AVAIL_OUT (block)->values);
3524 /* Short-cut for a common case: hoistable_set is empty. */
3525 if (bitmap_empty_p (&hoistable_set.values))
3526 return false;
3528 /* Compute which of the hoistable values is in AVAIL_OUT of
3529 at least one of the successors of BLOCK. */
3530 bitmap_head availout_in_some;
3531 bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
3532 FOR_EACH_EDGE (e, ei, block->succs)
3533 /* Do not consider expressions solely because their availability
3534 on loop exits. They'd be ANTIC-IN throughout the whole loop
3535 and thus effectively hoisted across loops by combination of
3536 PRE and hoisting. */
3537 if (! loop_exit_edge_p (block->loop_father, e))
3538 bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
3539 &AVAIL_OUT (e->dest)->values);
3540 bitmap_clear (&hoistable_set.values);
3542 /* Short-cut for a common case: availout_in_some is empty. */
3543 if (bitmap_empty_p (&availout_in_some))
3544 return false;
3546 /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */
3547 bitmap_move (&hoistable_set.values, &availout_in_some);
3548 hoistable_set.expressions = ANTIC_IN (block)->expressions;
3550 /* Now finally construct the topological-ordered expression set. */
3551 vec<pre_expr> exprs = sorted_array_from_bitmap_set (&hoistable_set);
3553 bitmap_clear (&hoistable_set.values);
3555 /* If there are candidate values for hoisting, insert expressions
3556 strategically to make the hoistable expressions fully redundant. */
3557 pre_expr expr;
3558 FOR_EACH_VEC_ELT (exprs, i, expr)
3560 /* While we try to sort expressions topologically above the
3561 sorting doesn't work out perfectly. Catch expressions we
3562 already inserted. */
3563 unsigned int value_id = get_expr_value_id (expr);
3564 if (bitmap_set_contains_value (AVAIL_OUT (block), value_id))
3566 if (dump_file && (dump_flags & TDF_DETAILS))
3568 fprintf (dump_file,
3569 "Already inserted expression for ");
3570 print_pre_expr (dump_file, expr);
3571 fprintf (dump_file, " (%04d)\n", value_id);
3573 continue;
3576 /* If we end up with a punned expression representation and this
3577 happens to be a float typed one give up - we can't know for
3578 sure whether all paths perform the floating-point load we are
3579 about to insert and on some targets this can cause correctness
3580 issues. See PR88240. */
3581 if (expr->kind == REFERENCE
3582 && PRE_EXPR_REFERENCE (expr)->punned
3583 && FLOAT_TYPE_P (get_expr_type (expr)))
3584 continue;
3586 /* OK, we should hoist this value. Perform the transformation. */
3587 pre_stats.hoist_insert++;
3588 if (dump_file && (dump_flags & TDF_DETAILS))
3590 fprintf (dump_file,
3591 "Inserting expression in block %d for code hoisting: ",
3592 block->index);
3593 print_pre_expr (dump_file, expr);
3594 fprintf (dump_file, " (%04d)\n", value_id);
3597 gimple_seq stmts = NULL;
3598 tree res = create_expression_by_pieces (block, expr, &stmts,
3599 get_expr_type (expr));
3601 /* Do not return true if expression creation ultimately
3602 did not insert any statements. */
3603 if (gimple_seq_empty_p (stmts))
3604 res = NULL_TREE;
3605 else
3607 if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
3608 gsi_insert_seq_before (&last, stmts, GSI_SAME_STMT);
3609 else
3610 gsi_insert_seq_after (&last, stmts, GSI_NEW_STMT);
3613 /* Make sure to not return true if expression creation ultimately
3614 failed but also make sure to insert any stmts produced as they
3615 are tracked in inserted_exprs. */
3616 if (! res)
3617 continue;
3619 new_stuff = true;
3622 exprs.release ();
3624 return new_stuff;
3627 /* Perform insertion of partially redundant and hoistable values. */
3629 static void
3630 insert (void)
3632 basic_block bb;
3634 FOR_ALL_BB_FN (bb, cfun)
3635 NEW_SETS (bb) = bitmap_set_new ();
3637 int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3638 int rpo_num = pre_and_rev_post_order_compute (NULL, rpo, false);
3640 int num_iterations = 0;
3641 bool changed;
3644 num_iterations++;
3645 if (dump_file && dump_flags & TDF_DETAILS)
3646 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3648 changed = false;
3649 for (int idx = 0; idx < rpo_num; ++idx)
3651 basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
3652 basic_block dom = get_immediate_dominator (CDI_DOMINATORS, block);
3653 if (dom)
3655 unsigned i;
3656 bitmap_iterator bi;
3657 bitmap_set_t newset;
3659 /* First, update the AVAIL_OUT set with anything we may have
3660 inserted higher up in the dominator tree. */
3661 newset = NEW_SETS (dom);
3662 if (newset)
3664 /* Note that we need to value_replace both NEW_SETS, and
3665 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3666 represented by some non-simple expression here that we want
3667 to replace it with. */
3668 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3670 pre_expr expr = expression_for_id (i);
3671 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3672 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3676 /* Insert expressions for partial redundancies. */
3677 if (flag_tree_pre && !single_pred_p (block))
3679 changed |= do_pre_regular_insertion (block, dom);
3680 if (do_partial_partial)
3681 changed |= do_pre_partial_partial_insertion (block, dom);
3684 /* Insert expressions for hoisting. */
3685 if (flag_code_hoisting && EDGE_COUNT (block->succs) >= 2)
3686 changed |= do_hoist_insertion (block);
3690 /* Clear the NEW sets before the next iteration. We have already
3691 fully propagated its contents. */
3692 if (changed)
3693 FOR_ALL_BB_FN (bb, cfun)
3694 bitmap_set_free (NEW_SETS (bb));
3696 while (changed);
3698 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3700 free (rpo);
3704 /* Compute the AVAIL set for all basic blocks.
3706 This function performs value numbering of the statements in each basic
3707 block. The AVAIL sets are built from information we glean while doing
3708 this value numbering, since the AVAIL sets contain only one entry per
3709 value.
3711 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3712 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3714 static void
3715 compute_avail (void)
3718 basic_block block, son;
3719 basic_block *worklist;
3720 size_t sp = 0;
3721 unsigned i;
3722 tree name;
3724 /* We pretend that default definitions are defined in the entry block.
3725 This includes function arguments and the static chain decl. */
3726 FOR_EACH_SSA_NAME (i, name, cfun)
3728 pre_expr e;
3729 if (!SSA_NAME_IS_DEFAULT_DEF (name)
3730 || has_zero_uses (name)
3731 || virtual_operand_p (name))
3732 continue;
3734 e = get_or_alloc_expr_for_name (name);
3735 add_to_value (get_expr_value_id (e), e);
3736 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), e);
3737 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3741 if (dump_file && (dump_flags & TDF_DETAILS))
3743 print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3744 "tmp_gen", ENTRY_BLOCK);
3745 print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3746 "avail_out", ENTRY_BLOCK);
3749 /* Allocate the worklist. */
3750 worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
3752 /* Seed the algorithm by putting the dominator children of the entry
3753 block on the worklist. */
3754 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun));
3755 son;
3756 son = next_dom_son (CDI_DOMINATORS, son))
3757 worklist[sp++] = son;
3759 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun))
3760 = ssa_default_def (cfun, gimple_vop (cfun));
3762 /* Loop until the worklist is empty. */
3763 while (sp)
3765 gimple *stmt;
3766 basic_block dom;
3768 /* Pick a block from the worklist. */
3769 block = worklist[--sp];
3770 vn_context_bb = block;
3772 /* Initially, the set of available values in BLOCK is that of
3773 its immediate dominator. */
3774 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3775 if (dom)
3777 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3778 BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
3781 /* Generate values for PHI nodes. */
3782 for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
3783 gsi_next (&gsi))
3785 tree result = gimple_phi_result (gsi.phi ());
3787 /* We have no need for virtual phis, as they don't represent
3788 actual computations. */
3789 if (virtual_operand_p (result))
3791 BB_LIVE_VOP_ON_EXIT (block) = result;
3792 continue;
3795 pre_expr e = get_or_alloc_expr_for_name (result);
3796 add_to_value (get_expr_value_id (e), e);
3797 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3798 bitmap_insert_into_set (PHI_GEN (block), e);
3801 BB_MAY_NOTRETURN (block) = 0;
3803 /* Now compute value numbers and populate value sets with all
3804 the expressions computed in BLOCK. */
3805 for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
3806 gsi_next (&gsi))
3808 ssa_op_iter iter;
3809 tree op;
3811 stmt = gsi_stmt (gsi);
3813 /* Cache whether the basic-block has any non-visible side-effect
3814 or control flow.
3815 If this isn't a call or it is the last stmt in the
3816 basic-block then the CFG represents things correctly. */
3817 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3819 /* Non-looping const functions always return normally.
3820 Otherwise the call might not return or have side-effects
3821 that forbids hoisting possibly trapping expressions
3822 before it. */
3823 int flags = gimple_call_flags (stmt);
3824 if (!(flags & ECF_CONST)
3825 || (flags & ECF_LOOPING_CONST_OR_PURE))
3826 BB_MAY_NOTRETURN (block) = 1;
3829 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3831 pre_expr e = get_or_alloc_expr_for_name (op);
3833 add_to_value (get_expr_value_id (e), e);
3834 bitmap_insert_into_set (TMP_GEN (block), e);
3835 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3838 if (gimple_vdef (stmt))
3839 BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
3841 if (gimple_has_side_effects (stmt)
3842 || stmt_could_throw_p (cfun, stmt)
3843 || is_gimple_debug (stmt))
3844 continue;
3846 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3848 if (ssa_undefined_value_p (op))
3849 continue;
3850 pre_expr e = get_or_alloc_expr_for_name (op);
3851 bitmap_value_insert_into_set (EXP_GEN (block), e);
3854 switch (gimple_code (stmt))
3856 case GIMPLE_RETURN:
3857 continue;
3859 case GIMPLE_CALL:
3861 vn_reference_t ref;
3862 vn_reference_s ref1;
3863 pre_expr result = NULL;
3865 /* We can value number only calls to real functions. */
3866 if (gimple_call_internal_p (stmt))
3867 continue;
3869 vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
3870 if (!ref)
3871 continue;
3873 /* If the value of the call is not invalidated in
3874 this block until it is computed, add the expression
3875 to EXP_GEN. */
3876 if (!gimple_vuse (stmt)
3877 || gimple_code
3878 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3879 || gimple_bb (SSA_NAME_DEF_STMT
3880 (gimple_vuse (stmt))) != block)
3882 result = pre_expr_pool.allocate ();
3883 result->kind = REFERENCE;
3884 result->id = 0;
3885 result->loc = gimple_location (stmt);
3886 PRE_EXPR_REFERENCE (result) = ref;
3888 get_or_alloc_expression_id (result);
3889 add_to_value (get_expr_value_id (result), result);
3890 bitmap_value_insert_into_set (EXP_GEN (block), result);
3892 continue;
3895 case GIMPLE_ASSIGN:
3897 pre_expr result = NULL;
3898 switch (vn_get_stmt_kind (stmt))
3900 case VN_NARY:
3902 enum tree_code code = gimple_assign_rhs_code (stmt);
3903 vn_nary_op_t nary;
3905 /* COND_EXPR and VEC_COND_EXPR are awkward in
3906 that they contain an embedded complex expression.
3907 Don't even try to shove those through PRE. */
3908 if (code == COND_EXPR
3909 || code == VEC_COND_EXPR)
3910 continue;
3912 vn_nary_op_lookup_stmt (stmt, &nary);
3913 if (!nary || nary->predicated_values)
3914 continue;
3916 /* If the NARY traps and there was a preceding
3917 point in the block that might not return avoid
3918 adding the nary to EXP_GEN. */
3919 if (BB_MAY_NOTRETURN (block)
3920 && vn_nary_may_trap (nary))
3921 continue;
3923 result = pre_expr_pool.allocate ();
3924 result->kind = NARY;
3925 result->id = 0;
3926 result->loc = gimple_location (stmt);
3927 PRE_EXPR_NARY (result) = nary;
3928 break;
3931 case VN_REFERENCE:
3933 tree rhs1 = gimple_assign_rhs1 (stmt);
3934 ao_ref rhs1_ref;
3935 ao_ref_init (&rhs1_ref, rhs1);
3936 alias_set_type set = ao_ref_alias_set (&rhs1_ref);
3937 alias_set_type base_set
3938 = ao_ref_base_alias_set (&rhs1_ref);
3939 vec<vn_reference_op_s> operands
3940 = vn_reference_operands_for_lookup (rhs1);
3941 vn_reference_t ref;
3942 vn_reference_lookup_pieces (gimple_vuse (stmt), set,
3943 base_set, TREE_TYPE (rhs1),
3944 operands, &ref, VN_WALK);
3945 if (!ref)
3947 operands.release ();
3948 continue;
3951 /* If the REFERENCE traps and there was a preceding
3952 point in the block that might not return avoid
3953 adding the reference to EXP_GEN. */
3954 if (BB_MAY_NOTRETURN (block)
3955 && vn_reference_may_trap (ref))
3956 continue;
3958 /* If the value of the reference is not invalidated in
3959 this block until it is computed, add the expression
3960 to EXP_GEN. */
3961 if (gimple_vuse (stmt))
3963 gimple *def_stmt;
3964 bool ok = true;
3965 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
3966 while (!gimple_nop_p (def_stmt)
3967 && gimple_code (def_stmt) != GIMPLE_PHI
3968 && gimple_bb (def_stmt) == block)
3970 if (stmt_may_clobber_ref_p
3971 (def_stmt, gimple_assign_rhs1 (stmt)))
3973 ok = false;
3974 break;
3976 def_stmt
3977 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
3979 if (!ok)
3981 operands.release ();
3982 continue;
3986 /* If the load was value-numbered to another
3987 load make sure we do not use its expression
3988 for insertion if it wouldn't be a valid
3989 replacement. */
3990 /* At the momemt we have a testcase
3991 for hoist insertion of aligned vs. misaligned
3992 variants in gcc.dg/torture/pr65270-1.c thus
3993 with just alignment to be considered we can
3994 simply replace the expression in the hashtable
3995 with the most conservative one. */
3996 vn_reference_op_t ref1 = &ref->operands.last ();
3997 while (ref1->opcode != TARGET_MEM_REF
3998 && ref1->opcode != MEM_REF
3999 && ref1 != &ref->operands[0])
4000 --ref1;
4001 vn_reference_op_t ref2 = &operands.last ();
4002 while (ref2->opcode != TARGET_MEM_REF
4003 && ref2->opcode != MEM_REF
4004 && ref2 != &operands[0])
4005 --ref2;
4006 if ((ref1->opcode == TARGET_MEM_REF
4007 || ref1->opcode == MEM_REF)
4008 && (TYPE_ALIGN (ref1->type)
4009 > TYPE_ALIGN (ref2->type)))
4010 ref1->type
4011 = build_aligned_type (ref1->type,
4012 TYPE_ALIGN (ref2->type));
4013 /* TBAA behavior is an obvious part so make sure
4014 that the hashtable one covers this as well
4015 by adjusting the ref alias set and its base. */
4016 if (ref->set == set
4017 || alias_set_subset_of (set, ref->set))
4019 else if (alias_set_subset_of (ref->set, set))
4021 ref->set = set;
4022 if (ref1->opcode == MEM_REF)
4023 ref1->op0
4024 = wide_int_to_tree (TREE_TYPE (ref2->op0),
4025 wi::to_wide (ref1->op0));
4026 else
4027 ref1->op2
4028 = wide_int_to_tree (TREE_TYPE (ref2->op2),
4029 wi::to_wide (ref1->op2));
4031 else
4033 ref->set = 0;
4034 if (ref1->opcode == MEM_REF)
4035 ref1->op0
4036 = wide_int_to_tree (ptr_type_node,
4037 wi::to_wide (ref1->op0));
4038 else
4039 ref1->op2
4040 = wide_int_to_tree (ptr_type_node,
4041 wi::to_wide (ref1->op2));
4043 operands.release ();
4045 result = pre_expr_pool.allocate ();
4046 result->kind = REFERENCE;
4047 result->id = 0;
4048 result->loc = gimple_location (stmt);
4049 PRE_EXPR_REFERENCE (result) = ref;
4050 break;
4053 default:
4054 continue;
4057 get_or_alloc_expression_id (result);
4058 add_to_value (get_expr_value_id (result), result);
4059 bitmap_value_insert_into_set (EXP_GEN (block), result);
4060 continue;
4062 default:
4063 break;
4067 if (dump_file && (dump_flags & TDF_DETAILS))
4069 print_bitmap_set (dump_file, EXP_GEN (block),
4070 "exp_gen", block->index);
4071 print_bitmap_set (dump_file, PHI_GEN (block),
4072 "phi_gen", block->index);
4073 print_bitmap_set (dump_file, TMP_GEN (block),
4074 "tmp_gen", block->index);
4075 print_bitmap_set (dump_file, AVAIL_OUT (block),
4076 "avail_out", block->index);
4079 /* Put the dominator children of BLOCK on the worklist of blocks
4080 to compute available sets for. */
4081 for (son = first_dom_son (CDI_DOMINATORS, block);
4082 son;
4083 son = next_dom_son (CDI_DOMINATORS, son))
4084 worklist[sp++] = son;
4086 vn_context_bb = NULL;
4088 free (worklist);
4092 /* Initialize data structures used by PRE. */
4094 static void
4095 init_pre (void)
4097 basic_block bb;
4099 next_expression_id = 1;
4100 expressions.create (0);
4101 expressions.safe_push (NULL);
4102 value_expressions.create (get_max_value_id () + 1);
4103 value_expressions.safe_grow_cleared (get_max_value_id () + 1, true);
4104 name_to_id.create (0);
4106 inserted_exprs = BITMAP_ALLOC (NULL);
4108 connect_infinite_loops_to_exit ();
4109 memset (&pre_stats, 0, sizeof (pre_stats));
4111 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4113 calculate_dominance_info (CDI_DOMINATORS);
4115 bitmap_obstack_initialize (&grand_bitmap_obstack);
4116 phi_translate_table = new hash_table<expr_pred_trans_d> (5110);
4117 expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4118 FOR_ALL_BB_FN (bb, cfun)
4120 EXP_GEN (bb) = bitmap_set_new ();
4121 PHI_GEN (bb) = bitmap_set_new ();
4122 TMP_GEN (bb) = bitmap_set_new ();
4123 AVAIL_OUT (bb) = bitmap_set_new ();
4128 /* Deallocate data structures used by PRE. */
4130 static void
4131 fini_pre ()
4133 value_expressions.release ();
4134 expressions.release ();
4135 BITMAP_FREE (inserted_exprs);
4136 bitmap_obstack_release (&grand_bitmap_obstack);
4137 bitmap_set_pool.release ();
4138 pre_expr_pool.release ();
4139 delete phi_translate_table;
4140 phi_translate_table = NULL;
4141 delete expression_to_id;
4142 expression_to_id = NULL;
4143 name_to_id.release ();
4145 free_aux_for_blocks ();
4148 namespace {
4150 const pass_data pass_data_pre =
4152 GIMPLE_PASS, /* type */
4153 "pre", /* name */
4154 OPTGROUP_NONE, /* optinfo_flags */
4155 TV_TREE_PRE, /* tv_id */
4156 ( PROP_cfg | PROP_ssa ), /* properties_required */
4157 0, /* properties_provided */
4158 0, /* properties_destroyed */
4159 TODO_rebuild_alias, /* todo_flags_start */
4160 0, /* todo_flags_finish */
4163 class pass_pre : public gimple_opt_pass
4165 public:
4166 pass_pre (gcc::context *ctxt)
4167 : gimple_opt_pass (pass_data_pre, ctxt)
4170 /* opt_pass methods: */
4171 virtual bool gate (function *)
4172 { return flag_tree_pre != 0 || flag_code_hoisting != 0; }
4173 virtual unsigned int execute (function *);
4175 }; // class pass_pre
4177 /* Valueization hook for RPO VN when we are calling back to it
4178 at ANTIC compute time. */
4180 static tree
4181 pre_valueize (tree name)
4183 if (TREE_CODE (name) == SSA_NAME)
4185 tree tem = VN_INFO (name)->valnum;
4186 if (tem != VN_TOP && tem != name)
4188 if (TREE_CODE (tem) != SSA_NAME
4189 || SSA_NAME_IS_DEFAULT_DEF (tem))
4190 return tem;
4191 /* We create temporary SSA names for representatives that
4192 do not have a definition (yet) but are not default defs either
4193 assume they are fine to use. */
4194 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (tem));
4195 if (! def_bb
4196 || dominated_by_p (CDI_DOMINATORS, vn_context_bb, def_bb))
4197 return tem;
4198 /* ??? Now we could look for a leader. Ideally we'd somehow
4199 expose RPO VN leaders and get rid of AVAIL_OUT as well... */
4202 return name;
4205 unsigned int
4206 pass_pre::execute (function *fun)
4208 unsigned int todo = 0;
4210 do_partial_partial =
4211 flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4213 /* This has to happen before VN runs because
4214 loop_optimizer_init may create new phis, etc. */
4215 loop_optimizer_init (LOOPS_NORMAL);
4216 split_edges_for_insertion ();
4217 scev_initialize ();
4218 calculate_dominance_info (CDI_DOMINATORS);
4220 run_rpo_vn (VN_WALK);
4222 init_pre ();
4224 vn_valueize = pre_valueize;
4226 /* Insert can get quite slow on an incredibly large number of basic
4227 blocks due to some quadratic behavior. Until this behavior is
4228 fixed, don't run it when he have an incredibly large number of
4229 bb's. If we aren't going to run insert, there is no point in
4230 computing ANTIC, either, even though it's plenty fast nor do
4231 we require AVAIL. */
4232 if (n_basic_blocks_for_fn (fun) < 4000)
4234 compute_avail ();
4235 compute_antic ();
4236 insert ();
4239 /* Make sure to remove fake edges before committing our inserts.
4240 This makes sure we don't end up with extra critical edges that
4241 we would need to split. */
4242 remove_fake_exit_edges ();
4243 gsi_commit_edge_inserts ();
4245 /* Eliminate folds statements which might (should not...) end up
4246 not keeping virtual operands up-to-date. */
4247 gcc_assert (!need_ssa_update_p (fun));
4249 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4250 statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4251 statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
4252 statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4254 todo |= eliminate_with_rpo_vn (inserted_exprs);
4256 vn_valueize = NULL;
4258 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4259 to insert PHI nodes sometimes, and because value numbering of casts isn't
4260 perfect, we sometimes end up inserting dead code. This simple DCE-like
4261 pass removes any insertions we made that weren't actually used. */
4262 simple_dce_from_worklist (inserted_exprs);
4264 fini_pre ();
4266 scev_finalize ();
4267 loop_optimizer_finalize ();
4269 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4270 case we can merge the block with the remaining predecessor of the block.
4271 It should either:
4272 - call merge_blocks after each tail merge iteration
4273 - call merge_blocks after all tail merge iterations
4274 - mark TODO_cleanup_cfg when necessary
4275 - share the cfg cleanup with fini_pre. */
4276 todo |= tail_merge_optimize (todo);
4278 free_rpo_vn ();
4280 /* Tail merging invalidates the virtual SSA web, together with
4281 cfg-cleanup opportunities exposed by PRE this will wreck the
4282 SSA updating machinery. So make sure to run update-ssa
4283 manually, before eventually scheduling cfg-cleanup as part of
4284 the todo. */
4285 update_ssa (TODO_update_ssa_only_virtuals);
4287 return todo;
4290 } // anon namespace
4292 gimple_opt_pass *
4293 make_pass_pre (gcc::context *ctxt)
4295 return new pass_pre (ctxt);