Re: Refactor copying decl section names
[official-gcc.git] / gcc / tree-ssa-pre.c
blob90877e3c68e330af285cb76912347fbe37027576
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 unsigned value_id;
260 location_t loc;
261 pre_expr_union u;
263 /* hash_table support. */
264 static inline hashval_t hash (const pre_expr_d *);
265 static inline int equal (const pre_expr_d *, const pre_expr_d *);
266 } *pre_expr;
268 #define PRE_EXPR_NAME(e) (e)->u.name
269 #define PRE_EXPR_NARY(e) (e)->u.nary
270 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
271 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
273 /* Compare E1 and E1 for equality. */
275 inline int
276 pre_expr_d::equal (const pre_expr_d *e1, const pre_expr_d *e2)
278 if (e1->kind != e2->kind)
279 return false;
281 switch (e1->kind)
283 case CONSTANT:
284 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
285 PRE_EXPR_CONSTANT (e2));
286 case NAME:
287 return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
288 case NARY:
289 return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
290 case REFERENCE:
291 return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
292 PRE_EXPR_REFERENCE (e2));
293 default:
294 gcc_unreachable ();
298 /* Hash E. */
300 inline hashval_t
301 pre_expr_d::hash (const pre_expr_d *e)
303 switch (e->kind)
305 case CONSTANT:
306 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
307 case NAME:
308 return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
309 case NARY:
310 return PRE_EXPR_NARY (e)->hashcode;
311 case REFERENCE:
312 return PRE_EXPR_REFERENCE (e)->hashcode;
313 default:
314 gcc_unreachable ();
318 /* Next global expression id number. */
319 static unsigned int next_expression_id;
321 /* Mapping from expression to id number we can use in bitmap sets. */
322 static vec<pre_expr> expressions;
323 static hash_table<pre_expr_d> *expression_to_id;
324 static vec<unsigned> name_to_id;
326 /* Allocate an expression id for EXPR. */
328 static inline unsigned int
329 alloc_expression_id (pre_expr expr)
331 struct pre_expr_d **slot;
332 /* Make sure we won't overflow. */
333 gcc_assert (next_expression_id + 1 > next_expression_id);
334 expr->id = next_expression_id++;
335 expressions.safe_push (expr);
336 if (expr->kind == NAME)
338 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
339 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
340 re-allocations by using vec::reserve upfront. */
341 unsigned old_len = name_to_id.length ();
342 name_to_id.reserve (num_ssa_names - old_len);
343 name_to_id.quick_grow_cleared (num_ssa_names);
344 gcc_assert (name_to_id[version] == 0);
345 name_to_id[version] = expr->id;
347 else
349 slot = expression_to_id->find_slot (expr, INSERT);
350 gcc_assert (!*slot);
351 *slot = expr;
353 return next_expression_id - 1;
356 /* Return the expression id for tree EXPR. */
358 static inline unsigned int
359 get_expression_id (const pre_expr expr)
361 return expr->id;
364 static inline unsigned int
365 lookup_expression_id (const pre_expr expr)
367 struct pre_expr_d **slot;
369 if (expr->kind == NAME)
371 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
372 if (name_to_id.length () <= version)
373 return 0;
374 return name_to_id[version];
376 else
378 slot = expression_to_id->find_slot (expr, NO_INSERT);
379 if (!slot)
380 return 0;
381 return ((pre_expr)*slot)->id;
385 /* Return the existing expression id for EXPR, or create one if one
386 does not exist yet. */
388 static inline unsigned int
389 get_or_alloc_expression_id (pre_expr expr)
391 unsigned int id = lookup_expression_id (expr);
392 if (id == 0)
393 return alloc_expression_id (expr);
394 return expr->id = id;
397 /* Return the expression that has expression id ID */
399 static inline pre_expr
400 expression_for_id (unsigned int id)
402 return expressions[id];
405 static object_allocator<pre_expr_d> pre_expr_pool ("pre_expr nodes");
407 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
409 static pre_expr
410 get_or_alloc_expr_for_name (tree name)
412 struct pre_expr_d expr;
413 pre_expr result;
414 unsigned int result_id;
416 expr.kind = NAME;
417 expr.id = 0;
418 PRE_EXPR_NAME (&expr) = name;
419 result_id = lookup_expression_id (&expr);
420 if (result_id != 0)
421 return expression_for_id (result_id);
423 result = pre_expr_pool.allocate ();
424 result->kind = NAME;
425 result->loc = UNKNOWN_LOCATION;
426 result->value_id = VN_INFO (name)->value_id;
427 PRE_EXPR_NAME (result) = name;
428 alloc_expression_id (result);
429 return result;
432 /* Given an NARY, get or create a pre_expr to represent it. */
434 static pre_expr
435 get_or_alloc_expr_for_nary (vn_nary_op_t nary,
436 location_t loc = UNKNOWN_LOCATION)
438 struct pre_expr_d expr;
439 pre_expr result;
440 unsigned int result_id;
442 expr.kind = NARY;
443 expr.id = 0;
444 PRE_EXPR_NARY (&expr) = nary;
445 result_id = lookup_expression_id (&expr);
446 if (result_id != 0)
447 return expression_for_id (result_id);
449 result = pre_expr_pool.allocate ();
450 result->kind = NARY;
451 result->loc = loc;
452 result->value_id = nary->value_id;
453 PRE_EXPR_NARY (result) = nary;
454 alloc_expression_id (result);
455 return result;
458 /* Given an REFERENCE, get or create a pre_expr to represent it. */
460 static pre_expr
461 get_or_alloc_expr_for_reference (vn_reference_t reference,
462 location_t loc = UNKNOWN_LOCATION)
464 struct pre_expr_d expr;
465 pre_expr result;
466 unsigned int result_id;
468 expr.kind = REFERENCE;
469 expr.id = 0;
470 PRE_EXPR_REFERENCE (&expr) = reference;
471 result_id = lookup_expression_id (&expr);
472 if (result_id != 0)
473 return expression_for_id (result_id);
475 result = pre_expr_pool.allocate ();
476 result->kind = REFERENCE;
477 result->loc = loc;
478 result->value_id = reference->value_id;
479 PRE_EXPR_REFERENCE (result) = reference;
480 alloc_expression_id (result);
481 return result;
485 /* An unordered bitmap set. One bitmap tracks values, the other,
486 expressions. */
487 typedef class bitmap_set
489 public:
490 bitmap_head expressions;
491 bitmap_head values;
492 } *bitmap_set_t;
494 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
495 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
497 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
498 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
500 /* Mapping from value id to expressions with that value_id. */
501 static vec<bitmap> value_expressions;
502 /* We just record a single expression for each constant value,
503 one of kind CONSTANT. */
504 static vec<pre_expr> constant_value_expressions;
507 /* This structure is used to keep track of statistics on what
508 optimization PRE was able to perform. */
509 static struct
511 /* The number of new expressions/temporaries generated by PRE. */
512 int insertions;
514 /* The number of inserts found due to partial anticipation */
515 int pa_insert;
517 /* The number of inserts made for code hoisting. */
518 int hoist_insert;
520 /* The number of new PHI nodes added by PRE. */
521 int phis;
522 } pre_stats;
524 static bool do_partial_partial;
525 static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
526 static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
527 static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
528 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
529 static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
530 static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
531 static bitmap_set_t bitmap_set_new (void);
532 static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
533 tree);
534 static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
535 static unsigned int get_expr_value_id (pre_expr);
537 /* We can add and remove elements and entries to and from sets
538 and hash tables, so we use alloc pools for them. */
540 static object_allocator<bitmap_set> bitmap_set_pool ("Bitmap sets");
541 static bitmap_obstack grand_bitmap_obstack;
543 /* A three tuple {e, pred, v} used to cache phi translations in the
544 phi_translate_table. */
546 typedef struct expr_pred_trans_d : public typed_noop_remove <expr_pred_trans_d>
548 typedef expr_pred_trans_d value_type;
549 typedef expr_pred_trans_d compare_type;
551 /* The expression ID. */
552 unsigned e;
554 /* The value expression ID that resulted from the translation. */
555 unsigned v;
557 /* hash_table support. */
558 static inline void mark_empty (expr_pred_trans_d &);
559 static inline bool is_empty (const expr_pred_trans_d &);
560 static inline void mark_deleted (expr_pred_trans_d &);
561 static inline bool is_deleted (const expr_pred_trans_d &);
562 static const bool empty_zero_p = true;
563 static inline hashval_t hash (const expr_pred_trans_d &);
564 static inline int equal (const expr_pred_trans_d &, const expr_pred_trans_d &);
565 } *expr_pred_trans_t;
566 typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
568 inline bool
569 expr_pred_trans_d::is_empty (const expr_pred_trans_d &e)
571 return e.e == 0;
574 inline bool
575 expr_pred_trans_d::is_deleted (const expr_pred_trans_d &e)
577 return e.e == -1u;
580 inline void
581 expr_pred_trans_d::mark_empty (expr_pred_trans_d &e)
583 e.e = 0;
586 inline void
587 expr_pred_trans_d::mark_deleted (expr_pred_trans_d &e)
589 e.e = -1u;
592 inline hashval_t
593 expr_pred_trans_d::hash (const expr_pred_trans_d &e)
595 return e.e;
598 inline int
599 expr_pred_trans_d::equal (const expr_pred_trans_d &ve1,
600 const expr_pred_trans_d &ve2)
602 return ve1.e == ve2.e;
605 /* Sets that we need to keep track of. */
606 typedef struct bb_bitmap_sets
608 /* The EXP_GEN set, which represents expressions/values generated in
609 a basic block. */
610 bitmap_set_t exp_gen;
612 /* The PHI_GEN set, which represents PHI results generated in a
613 basic block. */
614 bitmap_set_t phi_gen;
616 /* The TMP_GEN set, which represents results/temporaries generated
617 in a basic block. IE the LHS of an expression. */
618 bitmap_set_t tmp_gen;
620 /* The AVAIL_OUT set, which represents which values are available in
621 a given basic block. */
622 bitmap_set_t avail_out;
624 /* The ANTIC_IN set, which represents which values are anticipatable
625 in a given basic block. */
626 bitmap_set_t antic_in;
628 /* The PA_IN set, which represents which values are
629 partially anticipatable in a given basic block. */
630 bitmap_set_t pa_in;
632 /* The NEW_SETS set, which is used during insertion to augment the
633 AVAIL_OUT set of blocks with the new insertions performed during
634 the current iteration. */
635 bitmap_set_t new_sets;
637 /* A cache for value_dies_in_block_x. */
638 bitmap expr_dies;
640 /* The live virtual operand on successor edges. */
641 tree vop_on_exit;
643 /* PHI translate cache for the single successor edge. */
644 hash_table<expr_pred_trans_d> *phi_translate_table;
646 /* True if we have visited this block during ANTIC calculation. */
647 unsigned int visited : 1;
649 /* True when the block contains a call that might not return. */
650 unsigned int contains_may_not_return_call : 1;
651 } *bb_value_sets_t;
653 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
654 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
655 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
656 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
657 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
658 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
659 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
660 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
661 #define PHI_TRANS_TABLE(BB) ((bb_value_sets_t) ((BB)->aux))->phi_translate_table
662 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
663 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
664 #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
667 /* Add the tuple mapping from {expression E, basic block PRED} to
668 the phi translation table and return whether it pre-existed. */
670 static inline bool
671 phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
673 if (!PHI_TRANS_TABLE (pred))
674 PHI_TRANS_TABLE (pred) = new hash_table<expr_pred_trans_d> (11);
676 expr_pred_trans_t slot;
677 expr_pred_trans_d tem;
678 unsigned id = get_expression_id (e);
679 tem.e = id;
680 slot = PHI_TRANS_TABLE (pred)->find_slot_with_hash (tem, id, INSERT);
681 if (slot->e)
683 *entry = slot;
684 return true;
687 *entry = slot;
688 slot->e = id;
689 return false;
693 /* Add expression E to the expression set of value id V. */
695 static void
696 add_to_value (unsigned int v, pre_expr e)
698 gcc_checking_assert (get_expr_value_id (e) == v);
700 if (value_id_constant_p (v))
702 if (e->kind != CONSTANT)
703 return;
705 if (-v >= constant_value_expressions.length ())
706 constant_value_expressions.safe_grow_cleared (-v + 1);
708 pre_expr leader = constant_value_expressions[-v];
709 if (!leader)
710 constant_value_expressions[-v] = e;
712 else
714 if (v >= value_expressions.length ())
715 value_expressions.safe_grow_cleared (v + 1);
717 bitmap set = value_expressions[v];
718 if (!set)
720 set = BITMAP_ALLOC (&grand_bitmap_obstack);
721 value_expressions[v] = set;
723 bitmap_set_bit (set, get_or_alloc_expression_id (e));
727 /* Create a new bitmap set and return it. */
729 static bitmap_set_t
730 bitmap_set_new (void)
732 bitmap_set_t ret = bitmap_set_pool.allocate ();
733 bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
734 bitmap_initialize (&ret->values, &grand_bitmap_obstack);
735 return ret;
738 /* Return the value id for a PRE expression EXPR. */
740 static unsigned int
741 get_expr_value_id (pre_expr expr)
743 /* ??? We cannot assert that expr has a value-id (it can be 0), because
744 we assign value-ids only to expressions that have a result
745 in set_hashtable_value_ids. */
746 return expr->value_id;
749 /* Return a VN valnum (SSA name or constant) for the PRE value-id VAL. */
751 static tree
752 vn_valnum_from_value_id (unsigned int val)
754 if (value_id_constant_p (val))
756 pre_expr vexpr = constant_value_expressions[-val];
757 if (vexpr)
758 return PRE_EXPR_CONSTANT (vexpr);
759 return NULL_TREE;
762 bitmap exprset = value_expressions[val];
763 bitmap_iterator bi;
764 unsigned int i;
765 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
767 pre_expr vexpr = expression_for_id (i);
768 if (vexpr->kind == NAME)
769 return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
771 return NULL_TREE;
774 /* Insert an expression EXPR into a bitmapped set. */
776 static void
777 bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
779 unsigned int val = get_expr_value_id (expr);
780 if (! value_id_constant_p (val))
782 /* Note this is the only function causing multiple expressions
783 for the same value to appear in a set. This is needed for
784 TMP_GEN, PHI_GEN and NEW_SETs. */
785 bitmap_set_bit (&set->values, val);
786 bitmap_set_bit (&set->expressions, get_or_alloc_expression_id (expr));
790 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
792 static void
793 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
795 bitmap_copy (&dest->expressions, &orig->expressions);
796 bitmap_copy (&dest->values, &orig->values);
800 /* Free memory used up by SET. */
801 static void
802 bitmap_set_free (bitmap_set_t set)
804 bitmap_clear (&set->expressions);
805 bitmap_clear (&set->values);
809 /* Generate an topological-ordered array of bitmap set SET. */
811 static vec<pre_expr>
812 sorted_array_from_bitmap_set (bitmap_set_t set)
814 unsigned int i, j;
815 bitmap_iterator bi, bj;
816 vec<pre_expr> result;
818 /* Pre-allocate enough space for the array. */
819 result.create (bitmap_count_bits (&set->expressions));
821 FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
823 /* The number of expressions having a given value is usually
824 relatively small. Thus, rather than making a vector of all
825 the expressions and sorting it by value-id, we walk the values
826 and check in the reverse mapping that tells us what expressions
827 have a given value, to filter those in our set. As a result,
828 the expressions are inserted in value-id order, which means
829 topological order.
831 If this is somehow a significant lose for some cases, we can
832 choose which set to walk based on the set size. */
833 bitmap exprset = value_expressions[i];
834 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj)
836 if (bitmap_bit_p (&set->expressions, j))
837 result.quick_push (expression_for_id (j));
841 return result;
844 /* Subtract all expressions contained in ORIG from DEST. */
846 static bitmap_set_t
847 bitmap_set_subtract_expressions (bitmap_set_t dest, bitmap_set_t orig)
849 bitmap_set_t result = bitmap_set_new ();
850 bitmap_iterator bi;
851 unsigned int i;
853 bitmap_and_compl (&result->expressions, &dest->expressions,
854 &orig->expressions);
856 FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
858 pre_expr expr = expression_for_id (i);
859 unsigned int value_id = get_expr_value_id (expr);
860 bitmap_set_bit (&result->values, value_id);
863 return result;
866 /* Subtract all values in bitmap set B from bitmap set A. */
868 static void
869 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
871 unsigned int i;
872 bitmap_iterator bi;
873 unsigned to_remove = -1U;
874 bitmap_and_compl_into (&a->values, &b->values);
875 FOR_EACH_EXPR_ID_IN_SET (a, i, bi)
877 if (to_remove != -1U)
879 bitmap_clear_bit (&a->expressions, to_remove);
880 to_remove = -1U;
882 pre_expr expr = expression_for_id (i);
883 if (! bitmap_bit_p (&a->values, get_expr_value_id (expr)))
884 to_remove = i;
886 if (to_remove != -1U)
887 bitmap_clear_bit (&a->expressions, to_remove);
891 /* Return true if bitmapped set SET contains the value VALUE_ID. */
893 static bool
894 bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
896 if (value_id_constant_p (value_id))
897 return true;
899 return bitmap_bit_p (&set->values, value_id);
902 /* Return true if two bitmap sets are equal. */
904 static bool
905 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
907 return bitmap_equal_p (&a->values, &b->values);
910 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
911 and add it otherwise. */
913 static void
914 bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
916 unsigned int val = get_expr_value_id (expr);
917 if (value_id_constant_p (val))
918 return;
920 if (bitmap_set_contains_value (set, val))
922 /* The number of expressions having a given value is usually
923 significantly less than the total number of expressions in SET.
924 Thus, rather than check, for each expression in SET, whether it
925 has the value LOOKFOR, we walk the reverse mapping that tells us
926 what expressions have a given value, and see if any of those
927 expressions are in our set. For large testcases, this is about
928 5-10x faster than walking the bitmap. If this is somehow a
929 significant lose for some cases, we can choose which set to walk
930 based on the set size. */
931 unsigned int i;
932 bitmap_iterator bi;
933 bitmap exprset = value_expressions[val];
934 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
936 if (bitmap_clear_bit (&set->expressions, i))
938 bitmap_set_bit (&set->expressions, get_expression_id (expr));
939 return;
942 gcc_unreachable ();
944 else
945 bitmap_insert_into_set (set, expr);
948 /* Insert EXPR into SET if EXPR's value is not already present in
949 SET. */
951 static void
952 bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
954 unsigned int val = get_expr_value_id (expr);
956 gcc_checking_assert (expr->id == get_or_alloc_expression_id (expr));
958 /* Constant values are always considered to be part of the set. */
959 if (value_id_constant_p (val))
960 return;
962 /* If the value membership changed, add the expression. */
963 if (bitmap_set_bit (&set->values, val))
964 bitmap_set_bit (&set->expressions, expr->id);
967 /* Print out EXPR to outfile. */
969 static void
970 print_pre_expr (FILE *outfile, const pre_expr expr)
972 if (! expr)
974 fprintf (outfile, "NULL");
975 return;
977 switch (expr->kind)
979 case CONSTANT:
980 print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr));
981 break;
982 case NAME:
983 print_generic_expr (outfile, PRE_EXPR_NAME (expr));
984 break;
985 case NARY:
987 unsigned int i;
988 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
989 fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
990 for (i = 0; i < nary->length; i++)
992 print_generic_expr (outfile, nary->op[i]);
993 if (i != (unsigned) nary->length - 1)
994 fprintf (outfile, ",");
996 fprintf (outfile, "}");
998 break;
1000 case REFERENCE:
1002 vn_reference_op_t vro;
1003 unsigned int i;
1004 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1005 fprintf (outfile, "{");
1006 for (i = 0;
1007 ref->operands.iterate (i, &vro);
1008 i++)
1010 bool closebrace = false;
1011 if (vro->opcode != SSA_NAME
1012 && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
1014 fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
1015 if (vro->op0)
1017 fprintf (outfile, "<");
1018 closebrace = true;
1021 if (vro->op0)
1023 print_generic_expr (outfile, vro->op0);
1024 if (vro->op1)
1026 fprintf (outfile, ",");
1027 print_generic_expr (outfile, vro->op1);
1029 if (vro->op2)
1031 fprintf (outfile, ",");
1032 print_generic_expr (outfile, vro->op2);
1035 if (closebrace)
1036 fprintf (outfile, ">");
1037 if (i != ref->operands.length () - 1)
1038 fprintf (outfile, ",");
1040 fprintf (outfile, "}");
1041 if (ref->vuse)
1043 fprintf (outfile, "@");
1044 print_generic_expr (outfile, ref->vuse);
1047 break;
1050 void debug_pre_expr (pre_expr);
1052 /* Like print_pre_expr but always prints to stderr. */
1053 DEBUG_FUNCTION void
1054 debug_pre_expr (pre_expr e)
1056 print_pre_expr (stderr, e);
1057 fprintf (stderr, "\n");
1060 /* Print out SET to OUTFILE. */
1062 static void
1063 print_bitmap_set (FILE *outfile, bitmap_set_t set,
1064 const char *setname, int blockindex)
1066 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
1067 if (set)
1069 bool first = true;
1070 unsigned i;
1071 bitmap_iterator bi;
1073 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1075 const pre_expr expr = expression_for_id (i);
1077 if (!first)
1078 fprintf (outfile, ", ");
1079 first = false;
1080 print_pre_expr (outfile, expr);
1082 fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1085 fprintf (outfile, " }\n");
1088 void debug_bitmap_set (bitmap_set_t);
1090 DEBUG_FUNCTION void
1091 debug_bitmap_set (bitmap_set_t set)
1093 print_bitmap_set (stderr, set, "debug", 0);
1096 void debug_bitmap_sets_for (basic_block);
1098 DEBUG_FUNCTION void
1099 debug_bitmap_sets_for (basic_block bb)
1101 print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1102 print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1103 print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1104 print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1105 print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1106 if (do_partial_partial)
1107 print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1108 print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1111 /* Print out the expressions that have VAL to OUTFILE. */
1113 static void
1114 print_value_expressions (FILE *outfile, unsigned int val)
1116 bitmap set = value_expressions[val];
1117 if (set)
1119 bitmap_set x;
1120 char s[10];
1121 sprintf (s, "%04d", val);
1122 x.expressions = *set;
1123 print_bitmap_set (outfile, &x, s, 0);
1128 DEBUG_FUNCTION void
1129 debug_value_expressions (unsigned int val)
1131 print_value_expressions (stderr, val);
1134 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1135 represent it. */
1137 static pre_expr
1138 get_or_alloc_expr_for_constant (tree constant)
1140 unsigned int result_id;
1141 struct pre_expr_d expr;
1142 pre_expr newexpr;
1144 expr.kind = CONSTANT;
1145 PRE_EXPR_CONSTANT (&expr) = constant;
1146 result_id = lookup_expression_id (&expr);
1147 if (result_id != 0)
1148 return expression_for_id (result_id);
1150 newexpr = pre_expr_pool.allocate ();
1151 newexpr->kind = CONSTANT;
1152 newexpr->loc = UNKNOWN_LOCATION;
1153 PRE_EXPR_CONSTANT (newexpr) = constant;
1154 alloc_expression_id (newexpr);
1155 newexpr->value_id = get_or_alloc_constant_value_id (constant);
1156 add_to_value (newexpr->value_id, newexpr);
1157 return newexpr;
1160 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1161 Currently only supports constants and SSA_NAMES. */
1162 static pre_expr
1163 get_or_alloc_expr_for (tree t)
1165 if (TREE_CODE (t) == SSA_NAME)
1166 return get_or_alloc_expr_for_name (t);
1167 else if (is_gimple_min_invariant (t))
1168 return get_or_alloc_expr_for_constant (t);
1169 gcc_unreachable ();
1172 /* Return the folded version of T if T, when folded, is a gimple
1173 min_invariant or an SSA name. Otherwise, return T. */
1175 static pre_expr
1176 fully_constant_expression (pre_expr e)
1178 switch (e->kind)
1180 case CONSTANT:
1181 return e;
1182 case NARY:
1184 vn_nary_op_t nary = PRE_EXPR_NARY (e);
1185 tree res = vn_nary_simplify (nary);
1186 if (!res)
1187 return e;
1188 if (is_gimple_min_invariant (res))
1189 return get_or_alloc_expr_for_constant (res);
1190 if (TREE_CODE (res) == SSA_NAME)
1191 return get_or_alloc_expr_for_name (res);
1192 return e;
1194 case REFERENCE:
1196 vn_reference_t ref = PRE_EXPR_REFERENCE (e);
1197 tree folded;
1198 if ((folded = fully_constant_vn_reference_p (ref)))
1199 return get_or_alloc_expr_for_constant (folded);
1200 return e;
1202 default:
1203 return e;
1205 return e;
1208 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1209 it has the value it would have in BLOCK. Set *SAME_VALID to true
1210 in case the new vuse doesn't change the value id of the OPERANDS. */
1212 static tree
1213 translate_vuse_through_block (vec<vn_reference_op_s> operands,
1214 alias_set_type set, alias_set_type base_set,
1215 tree type, tree vuse,
1216 basic_block phiblock,
1217 basic_block block, bool *same_valid)
1219 gimple *phi = SSA_NAME_DEF_STMT (vuse);
1220 ao_ref ref;
1221 edge e = NULL;
1222 bool use_oracle;
1224 if (same_valid)
1225 *same_valid = true;
1227 if (gimple_bb (phi) != phiblock)
1228 return vuse;
1230 unsigned int cnt = param_sccvn_max_alias_queries_per_access;
1231 use_oracle = ao_ref_init_from_vn_reference (&ref, set, base_set,
1232 type, operands);
1234 /* Use the alias-oracle to find either the PHI node in this block,
1235 the first VUSE used in this block that is equivalent to vuse or
1236 the first VUSE which definition in this block kills the value. */
1237 if (gimple_code (phi) == GIMPLE_PHI)
1238 e = find_edge (block, phiblock);
1239 else if (use_oracle)
1240 while (cnt > 0
1241 && !stmt_may_clobber_ref_p_1 (phi, &ref))
1243 --cnt;
1244 vuse = gimple_vuse (phi);
1245 phi = SSA_NAME_DEF_STMT (vuse);
1246 if (gimple_bb (phi) != phiblock)
1247 return vuse;
1248 if (gimple_code (phi) == GIMPLE_PHI)
1250 e = find_edge (block, phiblock);
1251 break;
1254 else
1255 return NULL_TREE;
1257 if (e)
1259 if (use_oracle && same_valid)
1261 bitmap visited = NULL;
1262 /* Try to find a vuse that dominates this phi node by skipping
1263 non-clobbering statements. */
1264 vuse = get_continuation_for_phi (phi, &ref, true,
1265 cnt, &visited, false, NULL, NULL);
1266 if (visited)
1267 BITMAP_FREE (visited);
1269 else
1270 vuse = NULL_TREE;
1271 /* If we didn't find any, the value ID can't stay the same. */
1272 if (!vuse && same_valid)
1273 *same_valid = false;
1274 /* ??? We would like to return vuse here as this is the canonical
1275 upmost vdef that this reference is associated with. But during
1276 insertion of the references into the hash tables we only ever
1277 directly insert with their direct gimple_vuse, hence returning
1278 something else would make us not find the other expression. */
1279 return PHI_ARG_DEF (phi, e->dest_idx);
1282 return NULL_TREE;
1285 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1286 SET2 *or* SET3. This is used to avoid making a set consisting of the union
1287 of PA_IN and ANTIC_IN during insert and phi-translation. */
1289 static inline pre_expr
1290 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
1291 bitmap_set_t set3 = NULL)
1293 pre_expr result = NULL;
1295 if (set1)
1296 result = bitmap_find_leader (set1, val);
1297 if (!result && set2)
1298 result = bitmap_find_leader (set2, val);
1299 if (!result && set3)
1300 result = bitmap_find_leader (set3, val);
1301 return result;
1304 /* Get the tree type for our PRE expression e. */
1306 static tree
1307 get_expr_type (const pre_expr e)
1309 switch (e->kind)
1311 case NAME:
1312 return TREE_TYPE (PRE_EXPR_NAME (e));
1313 case CONSTANT:
1314 return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1315 case REFERENCE:
1316 return PRE_EXPR_REFERENCE (e)->type;
1317 case NARY:
1318 return PRE_EXPR_NARY (e)->type;
1320 gcc_unreachable ();
1323 /* Get a representative SSA_NAME for a given expression that is available in B.
1324 Since all of our sub-expressions are treated as values, we require
1325 them to be SSA_NAME's for simplicity.
1326 Prior versions of GVNPRE used to use "value handles" here, so that
1327 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1328 either case, the operands are really values (IE we do not expect
1329 them to be usable without finding leaders). */
1331 static tree
1332 get_representative_for (const pre_expr e, basic_block b = NULL)
1334 tree name, valnum = NULL_TREE;
1335 unsigned int value_id = get_expr_value_id (e);
1337 switch (e->kind)
1339 case NAME:
1340 return PRE_EXPR_NAME (e);
1341 case CONSTANT:
1342 return PRE_EXPR_CONSTANT (e);
1343 case NARY:
1344 case REFERENCE:
1346 /* Go through all of the expressions representing this value
1347 and pick out an SSA_NAME. */
1348 unsigned int i;
1349 bitmap_iterator bi;
1350 bitmap exprs = value_expressions[value_id];
1351 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1353 pre_expr rep = expression_for_id (i);
1354 if (rep->kind == NAME)
1356 tree name = PRE_EXPR_NAME (rep);
1357 valnum = VN_INFO (name)->valnum;
1358 gimple *def = SSA_NAME_DEF_STMT (name);
1359 /* We have to return either a new representative or one
1360 that can be used for expression simplification and thus
1361 is available in B. */
1362 if (! b
1363 || gimple_nop_p (def)
1364 || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
1365 return name;
1367 else if (rep->kind == CONSTANT)
1368 return PRE_EXPR_CONSTANT (rep);
1371 break;
1374 /* If we reached here we couldn't find an SSA_NAME. This can
1375 happen when we've discovered a value that has never appeared in
1376 the program as set to an SSA_NAME, as the result of phi translation.
1377 Create one here.
1378 ??? We should be able to re-use this when we insert the statement
1379 to compute it. */
1380 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1381 vn_ssa_aux_t vn_info = VN_INFO (name);
1382 vn_info->value_id = value_id;
1383 vn_info->valnum = valnum ? valnum : name;
1384 /* ??? For now mark this SSA name for release by VN. */
1385 vn_info->needs_insertion = true;
1386 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1387 if (dump_file && (dump_flags & TDF_DETAILS))
1389 fprintf (dump_file, "Created SSA_NAME representative ");
1390 print_generic_expr (dump_file, name);
1391 fprintf (dump_file, " for expression:");
1392 print_pre_expr (dump_file, e);
1393 fprintf (dump_file, " (%04d)\n", value_id);
1396 return name;
1400 static pre_expr
1401 phi_translate (bitmap_set_t, pre_expr, bitmap_set_t, bitmap_set_t, edge);
1403 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1404 the phis in PRED. Return NULL if we can't find a leader for each part
1405 of the translated expression. */
1407 static pre_expr
1408 phi_translate_1 (bitmap_set_t dest,
1409 pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
1411 basic_block pred = e->src;
1412 basic_block phiblock = e->dest;
1413 location_t expr_loc = expr->loc;
1414 switch (expr->kind)
1416 case NARY:
1418 unsigned int i;
1419 bool changed = false;
1420 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1421 vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1422 sizeof_vn_nary_op (nary->length));
1423 memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1425 for (i = 0; i < newnary->length; i++)
1427 if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1428 continue;
1429 else
1431 pre_expr leader, result;
1432 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1433 leader = find_leader_in_sets (op_val_id, set1, set2);
1434 result = phi_translate (dest, leader, set1, set2, e);
1435 if (result && result != leader)
1436 /* If op has a leader in the sets we translate make
1437 sure to use the value of the translated expression.
1438 We might need a new representative for that. */
1439 newnary->op[i] = get_representative_for (result, pred);
1440 else if (!result)
1441 return NULL;
1443 changed |= newnary->op[i] != nary->op[i];
1446 if (changed)
1448 pre_expr constant;
1449 unsigned int new_val_id;
1451 PRE_EXPR_NARY (expr) = newnary;
1452 constant = fully_constant_expression (expr);
1453 PRE_EXPR_NARY (expr) = nary;
1454 if (constant != expr)
1456 /* For non-CONSTANTs we have to make sure we can eventually
1457 insert the expression. Which means we need to have a
1458 leader for it. */
1459 if (constant->kind != CONSTANT)
1461 /* Do not allow simplifications to non-constants over
1462 backedges as this will likely result in a loop PHI node
1463 to be inserted and increased register pressure.
1464 See PR77498 - this avoids doing predcoms work in
1465 a less efficient way. */
1466 if (e->flags & EDGE_DFS_BACK)
1468 else
1470 unsigned value_id = get_expr_value_id (constant);
1471 /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1472 dest has what we computed into ANTIC_OUT sofar
1473 so pick from that - since topological sorting
1474 by sorted_array_from_bitmap_set isn't perfect
1475 we may lose some cases here. */
1476 constant = find_leader_in_sets (value_id, dest,
1477 AVAIL_OUT (pred));
1478 if (constant)
1480 if (dump_file && (dump_flags & TDF_DETAILS))
1482 fprintf (dump_file, "simplifying ");
1483 print_pre_expr (dump_file, expr);
1484 fprintf (dump_file, " translated %d -> %d to ",
1485 phiblock->index, pred->index);
1486 PRE_EXPR_NARY (expr) = newnary;
1487 print_pre_expr (dump_file, expr);
1488 PRE_EXPR_NARY (expr) = nary;
1489 fprintf (dump_file, " to ");
1490 print_pre_expr (dump_file, constant);
1491 fprintf (dump_file, "\n");
1493 return constant;
1497 else
1498 return constant;
1501 /* vn_nary_* do not valueize operands. */
1502 for (i = 0; i < newnary->length; ++i)
1503 if (TREE_CODE (newnary->op[i]) == SSA_NAME)
1504 newnary->op[i] = VN_INFO (newnary->op[i])->valnum;
1505 tree result = vn_nary_op_lookup_pieces (newnary->length,
1506 newnary->opcode,
1507 newnary->type,
1508 &newnary->op[0],
1509 &nary);
1510 if (result && is_gimple_min_invariant (result))
1511 return get_or_alloc_expr_for_constant (result);
1513 if (!nary || nary->predicated_values)
1515 new_val_id = get_next_value_id ();
1516 nary = vn_nary_op_insert_pieces (newnary->length,
1517 newnary->opcode,
1518 newnary->type,
1519 &newnary->op[0],
1520 result, new_val_id);
1522 expr = get_or_alloc_expr_for_nary (nary, expr_loc);
1523 add_to_value (get_expr_value_id (expr), expr);
1525 return expr;
1527 break;
1529 case REFERENCE:
1531 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1532 vec<vn_reference_op_s> operands = ref->operands;
1533 tree vuse = ref->vuse;
1534 tree newvuse = vuse;
1535 vec<vn_reference_op_s> newoperands = vNULL;
1536 bool changed = false, same_valid = true;
1537 unsigned int i, n;
1538 vn_reference_op_t operand;
1539 vn_reference_t newref;
1541 for (i = 0; operands.iterate (i, &operand); i++)
1543 pre_expr opresult;
1544 pre_expr leader;
1545 tree op[3];
1546 tree type = operand->type;
1547 vn_reference_op_s newop = *operand;
1548 op[0] = operand->op0;
1549 op[1] = operand->op1;
1550 op[2] = operand->op2;
1551 for (n = 0; n < 3; ++n)
1553 unsigned int op_val_id;
1554 if (!op[n])
1555 continue;
1556 if (TREE_CODE (op[n]) != SSA_NAME)
1558 /* We can't possibly insert these. */
1559 if (n != 0
1560 && !is_gimple_min_invariant (op[n]))
1561 break;
1562 continue;
1564 op_val_id = VN_INFO (op[n])->value_id;
1565 leader = find_leader_in_sets (op_val_id, set1, set2);
1566 opresult = phi_translate (dest, leader, set1, set2, e);
1567 if (opresult && opresult != leader)
1569 tree name = get_representative_for (opresult);
1570 changed |= name != op[n];
1571 op[n] = name;
1573 else if (!opresult)
1574 break;
1576 if (n != 3)
1578 newoperands.release ();
1579 return NULL;
1581 if (!changed)
1582 continue;
1583 if (!newoperands.exists ())
1584 newoperands = operands.copy ();
1585 /* We may have changed from an SSA_NAME to a constant */
1586 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1587 newop.opcode = TREE_CODE (op[0]);
1588 newop.type = type;
1589 newop.op0 = op[0];
1590 newop.op1 = op[1];
1591 newop.op2 = op[2];
1592 newoperands[i] = newop;
1594 gcc_checking_assert (i == operands.length ());
1596 if (vuse)
1598 newvuse = translate_vuse_through_block (newoperands.exists ()
1599 ? newoperands : operands,
1600 ref->set, ref->base_set,
1601 ref->type,
1602 vuse, phiblock, pred,
1603 changed
1604 ? NULL : &same_valid);
1605 if (newvuse == NULL_TREE)
1607 newoperands.release ();
1608 return NULL;
1612 if (changed || newvuse != vuse)
1614 unsigned int new_val_id;
1616 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1617 ref->base_set,
1618 ref->type,
1619 newoperands.exists ()
1620 ? newoperands : operands,
1621 &newref, VN_WALK);
1622 if (result)
1623 newoperands.release ();
1625 /* We can always insert constants, so if we have a partial
1626 redundant constant load of another type try to translate it
1627 to a constant of appropriate type. */
1628 if (result && is_gimple_min_invariant (result))
1630 tree tem = result;
1631 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1633 tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1634 if (tem && !is_gimple_min_invariant (tem))
1635 tem = NULL_TREE;
1637 if (tem)
1638 return get_or_alloc_expr_for_constant (tem);
1641 /* If we'd have to convert things we would need to validate
1642 if we can insert the translated expression. So fail
1643 here for now - we cannot insert an alias with a different
1644 type in the VN tables either, as that would assert. */
1645 if (result
1646 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1647 return NULL;
1648 else if (!result && newref
1649 && !useless_type_conversion_p (ref->type, newref->type))
1651 newoperands.release ();
1652 return NULL;
1655 if (newref)
1656 new_val_id = newref->value_id;
1657 else
1659 if (changed || !same_valid)
1660 new_val_id = get_next_value_id ();
1661 else
1662 new_val_id = ref->value_id;
1663 if (!newoperands.exists ())
1664 newoperands = operands.copy ();
1665 newref = vn_reference_insert_pieces (newvuse, ref->set,
1666 ref->base_set, ref->type,
1667 newoperands,
1668 result, new_val_id);
1669 newoperands = vNULL;
1671 expr = get_or_alloc_expr_for_reference (newref, expr_loc);
1672 add_to_value (new_val_id, expr);
1674 newoperands.release ();
1675 return expr;
1677 break;
1679 case NAME:
1681 tree name = PRE_EXPR_NAME (expr);
1682 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1683 /* If the SSA name is defined by a PHI node in this block,
1684 translate it. */
1685 if (gimple_code (def_stmt) == GIMPLE_PHI
1686 && gimple_bb (def_stmt) == phiblock)
1688 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1690 /* Handle constant. */
1691 if (is_gimple_min_invariant (def))
1692 return get_or_alloc_expr_for_constant (def);
1694 return get_or_alloc_expr_for_name (def);
1696 /* Otherwise return it unchanged - it will get removed if its
1697 value is not available in PREDs AVAIL_OUT set of expressions
1698 by the subtraction of TMP_GEN. */
1699 return expr;
1702 default:
1703 gcc_unreachable ();
1707 /* Wrapper around phi_translate_1 providing caching functionality. */
1709 static pre_expr
1710 phi_translate (bitmap_set_t dest, pre_expr expr,
1711 bitmap_set_t set1, bitmap_set_t set2, edge e)
1713 expr_pred_trans_t slot = NULL;
1714 pre_expr phitrans;
1716 if (!expr)
1717 return NULL;
1719 /* Constants contain no values that need translation. */
1720 if (expr->kind == CONSTANT)
1721 return expr;
1723 if (value_id_constant_p (get_expr_value_id (expr)))
1724 return expr;
1726 /* Don't add translations of NAMEs as those are cheap to translate. */
1727 if (expr->kind != NAME)
1729 if (phi_trans_add (&slot, expr, e->src))
1730 return slot->v == 0 ? NULL : expression_for_id (slot->v);
1731 /* Store NULL for the value we want to return in the case of
1732 recursing. */
1733 slot->v = 0;
1736 /* Translate. */
1737 basic_block saved_valueize_bb = vn_context_bb;
1738 vn_context_bb = e->src;
1739 phitrans = phi_translate_1 (dest, expr, set1, set2, e);
1740 vn_context_bb = saved_valueize_bb;
1742 if (slot)
1744 /* We may have reallocated. */
1745 phi_trans_add (&slot, expr, e->src);
1746 if (phitrans)
1747 slot->v = get_expression_id (phitrans);
1748 else
1749 /* Remove failed translations again, they cause insert
1750 iteration to not pick up new opportunities reliably. */
1751 PHI_TRANS_TABLE (e->src)->clear_slot (slot);
1754 return phitrans;
1758 /* For each expression in SET, translate the values through phi nodes
1759 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1760 expressions in DEST. */
1762 static void
1763 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
1765 vec<pre_expr> exprs;
1766 pre_expr expr;
1767 int i;
1769 if (gimple_seq_empty_p (phi_nodes (e->dest)))
1771 bitmap_set_copy (dest, set);
1772 return;
1775 exprs = sorted_array_from_bitmap_set (set);
1776 /* Allocate the phi-translation cache where we have an idea about
1777 its size. hash-table implementation internals tell us that
1778 allocating the table to fit twice the number of elements will
1779 make sure we do not usually re-allocate. */
1780 if (!PHI_TRANS_TABLE (e->src))
1781 PHI_TRANS_TABLE (e->src)
1782 = new hash_table<expr_pred_trans_d> (2 * exprs.length ());
1783 FOR_EACH_VEC_ELT (exprs, i, expr)
1785 pre_expr translated;
1786 translated = phi_translate (dest, expr, set, NULL, e);
1787 if (!translated)
1788 continue;
1790 bitmap_insert_into_set (dest, translated);
1792 exprs.release ();
1795 /* Find the leader for a value (i.e., the name representing that
1796 value) in a given set, and return it. Return NULL if no leader
1797 is found. */
1799 static pre_expr
1800 bitmap_find_leader (bitmap_set_t set, unsigned int val)
1802 if (value_id_constant_p (val))
1803 return constant_value_expressions[-val];
1805 if (bitmap_set_contains_value (set, val))
1807 /* Rather than walk the entire bitmap of expressions, and see
1808 whether any of them has the value we are looking for, we look
1809 at the reverse mapping, which tells us the set of expressions
1810 that have a given value (IE value->expressions with that
1811 value) and see if any of those expressions are in our set.
1812 The number of expressions per value is usually significantly
1813 less than the number of expressions in the set. In fact, for
1814 large testcases, doing it this way is roughly 5-10x faster
1815 than walking the bitmap.
1816 If this is somehow a significant lose for some cases, we can
1817 choose which set to walk based on which set is smaller. */
1818 unsigned int i;
1819 bitmap_iterator bi;
1820 bitmap exprset = value_expressions[val];
1822 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1823 return expression_for_id (i);
1825 return NULL;
1828 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1829 BLOCK by seeing if it is not killed in the block. Note that we are
1830 only determining whether there is a store that kills it. Because
1831 of the order in which clean iterates over values, we are guaranteed
1832 that altered operands will have caused us to be eliminated from the
1833 ANTIC_IN set already. */
1835 static bool
1836 value_dies_in_block_x (pre_expr expr, basic_block block)
1838 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1839 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1840 gimple *def;
1841 gimple_stmt_iterator gsi;
1842 unsigned id = get_expression_id (expr);
1843 bool res = false;
1844 ao_ref ref;
1846 if (!vuse)
1847 return false;
1849 /* Lookup a previously calculated result. */
1850 if (EXPR_DIES (block)
1851 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1852 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1854 /* A memory expression {e, VUSE} dies in the block if there is a
1855 statement that may clobber e. If, starting statement walk from the
1856 top of the basic block, a statement uses VUSE there can be no kill
1857 inbetween that use and the original statement that loaded {e, VUSE},
1858 so we can stop walking. */
1859 ref.base = NULL_TREE;
1860 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1862 tree def_vuse, def_vdef;
1863 def = gsi_stmt (gsi);
1864 def_vuse = gimple_vuse (def);
1865 def_vdef = gimple_vdef (def);
1867 /* Not a memory statement. */
1868 if (!def_vuse)
1869 continue;
1871 /* Not a may-def. */
1872 if (!def_vdef)
1874 /* A load with the same VUSE, we're done. */
1875 if (def_vuse == vuse)
1876 break;
1878 continue;
1881 /* Init ref only if we really need it. */
1882 if (ref.base == NULL_TREE
1883 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->base_set,
1884 refx->type, refx->operands))
1886 res = true;
1887 break;
1889 /* If the statement may clobber expr, it dies. */
1890 if (stmt_may_clobber_ref_p_1 (def, &ref))
1892 res = true;
1893 break;
1897 /* Remember the result. */
1898 if (!EXPR_DIES (block))
1899 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1900 bitmap_set_bit (EXPR_DIES (block), id * 2);
1901 if (res)
1902 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1904 return res;
1908 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1909 contains its value-id. */
1911 static bool
1912 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1914 if (op && TREE_CODE (op) == SSA_NAME)
1916 unsigned int value_id = VN_INFO (op)->value_id;
1917 if (!(bitmap_set_contains_value (set1, value_id)
1918 || (set2 && bitmap_set_contains_value (set2, value_id))))
1919 return false;
1921 return true;
1924 /* Determine if the expression EXPR is valid in SET1 U SET2.
1925 ONLY SET2 CAN BE NULL.
1926 This means that we have a leader for each part of the expression
1927 (if it consists of values), or the expression is an SSA_NAME.
1928 For loads/calls, we also see if the vuse is killed in this block. */
1930 static bool
1931 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
1933 switch (expr->kind)
1935 case NAME:
1936 /* By construction all NAMEs are available. Non-available
1937 NAMEs are removed by subtracting TMP_GEN from the sets. */
1938 return true;
1939 case NARY:
1941 unsigned int i;
1942 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1943 for (i = 0; i < nary->length; i++)
1944 if (!op_valid_in_sets (set1, set2, nary->op[i]))
1945 return false;
1946 return true;
1948 break;
1949 case REFERENCE:
1951 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1952 vn_reference_op_t vro;
1953 unsigned int i;
1955 FOR_EACH_VEC_ELT (ref->operands, i, vro)
1957 if (!op_valid_in_sets (set1, set2, vro->op0)
1958 || !op_valid_in_sets (set1, set2, vro->op1)
1959 || !op_valid_in_sets (set1, set2, vro->op2))
1960 return false;
1962 return true;
1964 default:
1965 gcc_unreachable ();
1969 /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
1970 This means expressions that are made up of values we have no leaders for
1971 in SET1 or SET2. */
1973 static void
1974 clean (bitmap_set_t set1, bitmap_set_t set2 = NULL)
1976 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
1977 pre_expr expr;
1978 int i;
1980 FOR_EACH_VEC_ELT (exprs, i, expr)
1982 if (!valid_in_sets (set1, set2, expr))
1984 unsigned int val = get_expr_value_id (expr);
1985 bitmap_clear_bit (&set1->expressions, get_expression_id (expr));
1986 /* We are entered with possibly multiple expressions for a value
1987 so before removing a value from the set see if there's an
1988 expression for it left. */
1989 if (! bitmap_find_leader (set1, val))
1990 bitmap_clear_bit (&set1->values, val);
1993 exprs.release ();
1996 /* Clean the set of expressions that are no longer valid in SET because
1997 they are clobbered in BLOCK or because they trap and may not be executed. */
1999 static void
2000 prune_clobbered_mems (bitmap_set_t set, basic_block block)
2002 bitmap_iterator bi;
2003 unsigned i;
2004 unsigned to_remove = -1U;
2005 bool any_removed = false;
2007 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2009 /* Remove queued expr. */
2010 if (to_remove != -1U)
2012 bitmap_clear_bit (&set->expressions, to_remove);
2013 any_removed = true;
2014 to_remove = -1U;
2017 pre_expr expr = expression_for_id (i);
2018 if (expr->kind == REFERENCE)
2020 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2021 if (ref->vuse)
2023 gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2024 if (!gimple_nop_p (def_stmt)
2025 && ((gimple_bb (def_stmt) != block
2026 && !dominated_by_p (CDI_DOMINATORS,
2027 block, gimple_bb (def_stmt)))
2028 || (gimple_bb (def_stmt) == block
2029 && value_dies_in_block_x (expr, block))))
2030 to_remove = i;
2033 else if (expr->kind == NARY)
2035 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2036 /* If the NARY may trap make sure the block does not contain
2037 a possible exit point.
2038 ??? This is overly conservative if we translate AVAIL_OUT
2039 as the available expression might be after the exit point. */
2040 if (BB_MAY_NOTRETURN (block)
2041 && vn_nary_may_trap (nary))
2042 to_remove = i;
2046 /* Remove queued expr. */
2047 if (to_remove != -1U)
2049 bitmap_clear_bit (&set->expressions, to_remove);
2050 any_removed = true;
2053 /* Above we only removed expressions, now clean the set of values
2054 which no longer have any corresponding expression. We cannot
2055 clear the value at the time we remove an expression since there
2056 may be multiple expressions per value.
2057 If we'd queue possibly to be removed values we could use
2058 the bitmap_find_leader way to see if there's still an expression
2059 for it. For some ratio of to be removed values and number of
2060 values/expressions in the set this might be faster than rebuilding
2061 the value-set. */
2062 if (any_removed)
2064 bitmap_clear (&set->values);
2065 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2067 pre_expr expr = expression_for_id (i);
2068 unsigned int value_id = get_expr_value_id (expr);
2069 bitmap_set_bit (&set->values, value_id);
2074 /* Compute the ANTIC set for BLOCK.
2076 If succs(BLOCK) > 1 then
2077 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2078 else if succs(BLOCK) == 1 then
2079 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2081 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2083 Note that clean() is deferred until after the iteration. */
2085 static bool
2086 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2088 bitmap_set_t S, old, ANTIC_OUT;
2089 edge e;
2090 edge_iterator ei;
2092 bool was_visited = BB_VISITED (block);
2093 bool changed = ! BB_VISITED (block);
2094 BB_VISITED (block) = 1;
2095 old = ANTIC_OUT = S = NULL;
2097 /* If any edges from predecessors are abnormal, antic_in is empty,
2098 so do nothing. */
2099 if (block_has_abnormal_pred_edge)
2100 goto maybe_dump_sets;
2102 old = ANTIC_IN (block);
2103 ANTIC_OUT = bitmap_set_new ();
2105 /* If the block has no successors, ANTIC_OUT is empty. */
2106 if (EDGE_COUNT (block->succs) == 0)
2108 /* If we have one successor, we could have some phi nodes to
2109 translate through. */
2110 else if (single_succ_p (block))
2112 e = single_succ_edge (block);
2113 gcc_assert (BB_VISITED (e->dest));
2114 phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
2116 /* If we have multiple successors, we take the intersection of all of
2117 them. Note that in the case of loop exit phi nodes, we may have
2118 phis to translate through. */
2119 else
2121 size_t i;
2122 edge first = NULL;
2124 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2125 FOR_EACH_EDGE (e, ei, block->succs)
2127 if (!first
2128 && BB_VISITED (e->dest))
2129 first = e;
2130 else if (BB_VISITED (e->dest))
2131 worklist.quick_push (e);
2132 else
2134 /* Unvisited successors get their ANTIC_IN replaced by the
2135 maximal set to arrive at a maximum ANTIC_IN solution.
2136 We can ignore them in the intersection operation and thus
2137 need not explicitely represent that maximum solution. */
2138 if (dump_file && (dump_flags & TDF_DETAILS))
2139 fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n",
2140 e->src->index, e->dest->index);
2144 /* Of multiple successors we have to have visited one already
2145 which is guaranteed by iteration order. */
2146 gcc_assert (first != NULL);
2148 phi_translate_set (ANTIC_OUT, ANTIC_IN (first->dest), first);
2150 /* If we have multiple successors we need to intersect the ANTIC_OUT
2151 sets. For values that's a simple intersection but for
2152 expressions it is a union. Given we want to have a single
2153 expression per value in our sets we have to canonicalize.
2154 Avoid randomness and running into cycles like for PR82129 and
2155 canonicalize the expression we choose to the one with the
2156 lowest id. This requires we actually compute the union first. */
2157 FOR_EACH_VEC_ELT (worklist, i, e)
2159 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2161 bitmap_set_t tmp = bitmap_set_new ();
2162 phi_translate_set (tmp, ANTIC_IN (e->dest), e);
2163 bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
2164 bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
2165 bitmap_set_free (tmp);
2167 else
2169 bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
2170 bitmap_ior_into (&ANTIC_OUT->expressions,
2171 &ANTIC_IN (e->dest)->expressions);
2174 if (! worklist.is_empty ())
2176 /* Prune expressions not in the value set. */
2177 bitmap_iterator bi;
2178 unsigned int i;
2179 unsigned int to_clear = -1U;
2180 FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
2182 if (to_clear != -1U)
2184 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2185 to_clear = -1U;
2187 pre_expr expr = expression_for_id (i);
2188 unsigned int value_id = get_expr_value_id (expr);
2189 if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
2190 to_clear = i;
2192 if (to_clear != -1U)
2193 bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2197 /* Prune expressions that are clobbered in block and thus become
2198 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2199 prune_clobbered_mems (ANTIC_OUT, block);
2201 /* Generate ANTIC_OUT - TMP_GEN. */
2202 S = bitmap_set_subtract_expressions (ANTIC_OUT, TMP_GEN (block));
2204 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2205 ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
2206 TMP_GEN (block));
2208 /* Then union in the ANTIC_OUT - TMP_GEN values,
2209 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2210 bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
2211 bitmap_ior_into (&ANTIC_IN (block)->expressions, &S->expressions);
2213 /* clean (ANTIC_IN (block)) is defered to after the iteration converged
2214 because it can cause non-convergence, see for example PR81181. */
2216 /* Intersect ANTIC_IN with the old ANTIC_IN. This is required until
2217 we properly represent the maximum expression set, thus not prune
2218 values without expressions during the iteration. */
2219 if (was_visited
2220 && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
2222 if (dump_file && (dump_flags & TDF_DETAILS))
2223 fprintf (dump_file, "warning: intersecting with old ANTIC_IN "
2224 "shrinks the set\n");
2225 /* Prune expressions not in the value set. */
2226 bitmap_iterator bi;
2227 unsigned int i;
2228 unsigned int to_clear = -1U;
2229 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
2231 if (to_clear != -1U)
2233 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2234 to_clear = -1U;
2236 pre_expr expr = expression_for_id (i);
2237 unsigned int value_id = get_expr_value_id (expr);
2238 if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
2239 to_clear = i;
2241 if (to_clear != -1U)
2242 bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2245 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2246 changed = true;
2248 maybe_dump_sets:
2249 if (dump_file && (dump_flags & TDF_DETAILS))
2251 if (ANTIC_OUT)
2252 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2254 if (changed)
2255 fprintf (dump_file, "[changed] ");
2256 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2257 block->index);
2259 if (S)
2260 print_bitmap_set (dump_file, S, "S", block->index);
2262 if (old)
2263 bitmap_set_free (old);
2264 if (S)
2265 bitmap_set_free (S);
2266 if (ANTIC_OUT)
2267 bitmap_set_free (ANTIC_OUT);
2268 return changed;
2271 /* Compute PARTIAL_ANTIC for BLOCK.
2273 If succs(BLOCK) > 1 then
2274 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2275 in ANTIC_OUT for all succ(BLOCK)
2276 else if succs(BLOCK) == 1 then
2277 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2279 PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
2282 static void
2283 compute_partial_antic_aux (basic_block block,
2284 bool block_has_abnormal_pred_edge)
2286 bitmap_set_t old_PA_IN;
2287 bitmap_set_t PA_OUT;
2288 edge e;
2289 edge_iterator ei;
2290 unsigned long max_pa = param_max_partial_antic_length;
2292 old_PA_IN = PA_OUT = NULL;
2294 /* If any edges from predecessors are abnormal, antic_in is empty,
2295 so do nothing. */
2296 if (block_has_abnormal_pred_edge)
2297 goto maybe_dump_sets;
2299 /* If there are too many partially anticipatable values in the
2300 block, phi_translate_set can take an exponential time: stop
2301 before the translation starts. */
2302 if (max_pa
2303 && single_succ_p (block)
2304 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2305 goto maybe_dump_sets;
2307 old_PA_IN = PA_IN (block);
2308 PA_OUT = bitmap_set_new ();
2310 /* If the block has no successors, ANTIC_OUT is empty. */
2311 if (EDGE_COUNT (block->succs) == 0)
2313 /* If we have one successor, we could have some phi nodes to
2314 translate through. Note that we can't phi translate across DFS
2315 back edges in partial antic, because it uses a union operation on
2316 the successors. For recurrences like IV's, we will end up
2317 generating a new value in the set on each go around (i + 3 (VH.1)
2318 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2319 else if (single_succ_p (block))
2321 e = single_succ_edge (block);
2322 if (!(e->flags & EDGE_DFS_BACK))
2323 phi_translate_set (PA_OUT, PA_IN (e->dest), e);
2325 /* If we have multiple successors, we take the union of all of
2326 them. */
2327 else
2329 size_t i;
2331 auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2332 FOR_EACH_EDGE (e, ei, block->succs)
2334 if (e->flags & EDGE_DFS_BACK)
2335 continue;
2336 worklist.quick_push (e);
2338 if (worklist.length () > 0)
2340 FOR_EACH_VEC_ELT (worklist, i, e)
2342 unsigned int i;
2343 bitmap_iterator bi;
2345 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
2346 bitmap_value_insert_into_set (PA_OUT,
2347 expression_for_id (i));
2348 if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2350 bitmap_set_t pa_in = bitmap_set_new ();
2351 phi_translate_set (pa_in, PA_IN (e->dest), e);
2352 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2353 bitmap_value_insert_into_set (PA_OUT,
2354 expression_for_id (i));
2355 bitmap_set_free (pa_in);
2357 else
2358 FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
2359 bitmap_value_insert_into_set (PA_OUT,
2360 expression_for_id (i));
2365 /* Prune expressions that are clobbered in block and thus become
2366 invalid if translated from PA_OUT to PA_IN. */
2367 prune_clobbered_mems (PA_OUT, block);
2369 /* PA_IN starts with PA_OUT - TMP_GEN.
2370 Then we subtract things from ANTIC_IN. */
2371 PA_IN (block) = bitmap_set_subtract_expressions (PA_OUT, TMP_GEN (block));
2373 /* For partial antic, we want to put back in the phi results, since
2374 we will properly avoid making them partially antic over backedges. */
2375 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2376 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2378 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2379 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2381 clean (PA_IN (block), ANTIC_IN (block));
2383 maybe_dump_sets:
2384 if (dump_file && (dump_flags & TDF_DETAILS))
2386 if (PA_OUT)
2387 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2389 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2391 if (old_PA_IN)
2392 bitmap_set_free (old_PA_IN);
2393 if (PA_OUT)
2394 bitmap_set_free (PA_OUT);
2397 /* Compute ANTIC and partial ANTIC sets. */
2399 static void
2400 compute_antic (void)
2402 bool changed = true;
2403 int num_iterations = 0;
2404 basic_block block;
2405 int i;
2406 edge_iterator ei;
2407 edge e;
2409 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2410 We pre-build the map of blocks with incoming abnormal edges here. */
2411 auto_sbitmap has_abnormal_preds (last_basic_block_for_fn (cfun));
2412 bitmap_clear (has_abnormal_preds);
2414 FOR_ALL_BB_FN (block, cfun)
2416 BB_VISITED (block) = 0;
2418 FOR_EACH_EDGE (e, ei, block->preds)
2419 if (e->flags & EDGE_ABNORMAL)
2421 bitmap_set_bit (has_abnormal_preds, block->index);
2422 break;
2425 /* While we are here, give empty ANTIC_IN sets to each block. */
2426 ANTIC_IN (block) = bitmap_set_new ();
2427 if (do_partial_partial)
2428 PA_IN (block) = bitmap_set_new ();
2431 /* At the exit block we anticipate nothing. */
2432 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2434 /* For ANTIC computation we need a postorder that also guarantees that
2435 a block with a single successor is visited after its successor.
2436 RPO on the inverted CFG has this property. */
2437 auto_vec<int, 20> postorder;
2438 inverted_post_order_compute (&postorder);
2440 auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
2441 bitmap_clear (worklist);
2442 FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2443 bitmap_set_bit (worklist, e->src->index);
2444 while (changed)
2446 if (dump_file && (dump_flags & TDF_DETAILS))
2447 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2448 /* ??? We need to clear our PHI translation cache here as the
2449 ANTIC sets shrink and we restrict valid translations to
2450 those having operands with leaders in ANTIC. Same below
2451 for PA ANTIC computation. */
2452 num_iterations++;
2453 changed = false;
2454 for (i = postorder.length () - 1; i >= 0; i--)
2456 if (bitmap_bit_p (worklist, postorder[i]))
2458 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2459 bitmap_clear_bit (worklist, block->index);
2460 if (compute_antic_aux (block,
2461 bitmap_bit_p (has_abnormal_preds,
2462 block->index)))
2464 FOR_EACH_EDGE (e, ei, block->preds)
2465 bitmap_set_bit (worklist, e->src->index);
2466 changed = true;
2470 /* Theoretically possible, but *highly* unlikely. */
2471 gcc_checking_assert (num_iterations < 500);
2474 /* We have to clean after the dataflow problem converged as cleaning
2475 can cause non-convergence because it is based on expressions
2476 rather than values. */
2477 FOR_EACH_BB_FN (block, cfun)
2478 clean (ANTIC_IN (block));
2480 statistics_histogram_event (cfun, "compute_antic iterations",
2481 num_iterations);
2483 if (do_partial_partial)
2485 /* For partial antic we ignore backedges and thus we do not need
2486 to perform any iteration when we process blocks in postorder. */
2487 for (i = postorder.length () - 1; i >= 0; i--)
2489 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2490 compute_partial_antic_aux (block,
2491 bitmap_bit_p (has_abnormal_preds,
2492 block->index));
2498 /* Inserted expressions are placed onto this worklist, which is used
2499 for performing quick dead code elimination of insertions we made
2500 that didn't turn out to be necessary. */
2501 static bitmap inserted_exprs;
2503 /* The actual worker for create_component_ref_by_pieces. */
2505 static tree
2506 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2507 unsigned int *operand, gimple_seq *stmts)
2509 vn_reference_op_t currop = &ref->operands[*operand];
2510 tree genop;
2511 ++*operand;
2512 switch (currop->opcode)
2514 case CALL_EXPR:
2515 gcc_unreachable ();
2517 case MEM_REF:
2519 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2520 stmts);
2521 if (!baseop)
2522 return NULL_TREE;
2523 tree offset = currop->op0;
2524 if (TREE_CODE (baseop) == ADDR_EXPR
2525 && handled_component_p (TREE_OPERAND (baseop, 0)))
2527 poly_int64 off;
2528 tree base;
2529 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2530 &off);
2531 gcc_assert (base);
2532 offset = int_const_binop (PLUS_EXPR, offset,
2533 build_int_cst (TREE_TYPE (offset),
2534 off));
2535 baseop = build_fold_addr_expr (base);
2537 genop = build2 (MEM_REF, currop->type, baseop, offset);
2538 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2539 MR_DEPENDENCE_BASE (genop) = currop->base;
2540 REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
2541 return genop;
2544 case TARGET_MEM_REF:
2546 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2547 vn_reference_op_t nextop = &ref->operands[(*operand)++];
2548 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2549 stmts);
2550 if (!baseop)
2551 return NULL_TREE;
2552 if (currop->op0)
2554 genop0 = find_or_generate_expression (block, currop->op0, stmts);
2555 if (!genop0)
2556 return NULL_TREE;
2558 if (nextop->op0)
2560 genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2561 if (!genop1)
2562 return NULL_TREE;
2564 genop = build5 (TARGET_MEM_REF, currop->type,
2565 baseop, currop->op2, genop0, currop->op1, genop1);
2567 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2568 MR_DEPENDENCE_BASE (genop) = currop->base;
2569 return genop;
2572 case ADDR_EXPR:
2573 if (currop->op0)
2575 gcc_assert (is_gimple_min_invariant (currop->op0));
2576 return currop->op0;
2578 /* Fallthrough. */
2579 case REALPART_EXPR:
2580 case IMAGPART_EXPR:
2581 case VIEW_CONVERT_EXPR:
2583 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2584 stmts);
2585 if (!genop0)
2586 return NULL_TREE;
2587 return fold_build1 (currop->opcode, currop->type, genop0);
2590 case WITH_SIZE_EXPR:
2592 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2593 stmts);
2594 if (!genop0)
2595 return NULL_TREE;
2596 tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2597 if (!genop1)
2598 return NULL_TREE;
2599 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2602 case BIT_FIELD_REF:
2604 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2605 stmts);
2606 if (!genop0)
2607 return NULL_TREE;
2608 tree op1 = currop->op0;
2609 tree op2 = currop->op1;
2610 tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2611 REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
2612 return fold (t);
2615 /* For array ref vn_reference_op's, operand 1 of the array ref
2616 is op0 of the reference op and operand 3 of the array ref is
2617 op1. */
2618 case ARRAY_RANGE_REF:
2619 case ARRAY_REF:
2621 tree genop0;
2622 tree genop1 = currop->op0;
2623 tree genop2 = currop->op1;
2624 tree genop3 = currop->op2;
2625 genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2626 stmts);
2627 if (!genop0)
2628 return NULL_TREE;
2629 genop1 = find_or_generate_expression (block, genop1, stmts);
2630 if (!genop1)
2631 return NULL_TREE;
2632 if (genop2)
2634 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2635 /* Drop zero minimum index if redundant. */
2636 if (integer_zerop (genop2)
2637 && (!domain_type
2638 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2639 genop2 = NULL_TREE;
2640 else
2642 genop2 = find_or_generate_expression (block, genop2, stmts);
2643 if (!genop2)
2644 return NULL_TREE;
2647 if (genop3)
2649 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2650 /* We can't always put a size in units of the element alignment
2651 here as the element alignment may be not visible. See
2652 PR43783. Simply drop the element size for constant
2653 sizes. */
2654 if (TREE_CODE (genop3) == INTEGER_CST
2655 && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
2656 && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
2657 (wi::to_offset (genop3)
2658 * vn_ref_op_align_unit (currop))))
2659 genop3 = NULL_TREE;
2660 else
2662 genop3 = find_or_generate_expression (block, genop3, stmts);
2663 if (!genop3)
2664 return NULL_TREE;
2667 return build4 (currop->opcode, currop->type, genop0, genop1,
2668 genop2, genop3);
2670 case COMPONENT_REF:
2672 tree op0;
2673 tree op1;
2674 tree genop2 = currop->op1;
2675 op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2676 if (!op0)
2677 return NULL_TREE;
2678 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2679 op1 = currop->op0;
2680 if (genop2)
2682 genop2 = find_or_generate_expression (block, genop2, stmts);
2683 if (!genop2)
2684 return NULL_TREE;
2686 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2689 case SSA_NAME:
2691 genop = find_or_generate_expression (block, currop->op0, stmts);
2692 return genop;
2694 case STRING_CST:
2695 case INTEGER_CST:
2696 case POLY_INT_CST:
2697 case COMPLEX_CST:
2698 case VECTOR_CST:
2699 case REAL_CST:
2700 case CONSTRUCTOR:
2701 case VAR_DECL:
2702 case PARM_DECL:
2703 case CONST_DECL:
2704 case RESULT_DECL:
2705 case FUNCTION_DECL:
2706 return currop->op0;
2708 default:
2709 gcc_unreachable ();
2713 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2714 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2715 trying to rename aggregates into ssa form directly, which is a no no.
2717 Thus, this routine doesn't create temporaries, it just builds a
2718 single access expression for the array, calling
2719 find_or_generate_expression to build the innermost pieces.
2721 This function is a subroutine of create_expression_by_pieces, and
2722 should not be called on it's own unless you really know what you
2723 are doing. */
2725 static tree
2726 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2727 gimple_seq *stmts)
2729 unsigned int op = 0;
2730 return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2733 /* Find a simple leader for an expression, or generate one using
2734 create_expression_by_pieces from a NARY expression for the value.
2735 BLOCK is the basic_block we are looking for leaders in.
2736 OP is the tree expression to find a leader for or generate.
2737 Returns the leader or NULL_TREE on failure. */
2739 static tree
2740 find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2742 pre_expr expr = get_or_alloc_expr_for (op);
2743 unsigned int lookfor = get_expr_value_id (expr);
2744 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2745 if (leader)
2747 if (leader->kind == NAME)
2748 return PRE_EXPR_NAME (leader);
2749 else if (leader->kind == CONSTANT)
2750 return PRE_EXPR_CONSTANT (leader);
2752 /* Defer. */
2753 return NULL_TREE;
2756 /* It must be a complex expression, so generate it recursively. Note
2757 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2758 where the insert algorithm fails to insert a required expression. */
2759 bitmap exprset = value_expressions[lookfor];
2760 bitmap_iterator bi;
2761 unsigned int i;
2762 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2764 pre_expr temp = expression_for_id (i);
2765 /* We cannot insert random REFERENCE expressions at arbitrary
2766 places. We can insert NARYs which eventually re-materializes
2767 its operand values. */
2768 if (temp->kind == NARY)
2769 return create_expression_by_pieces (block, temp, stmts,
2770 get_expr_type (expr));
2773 /* Defer. */
2774 return NULL_TREE;
2777 /* Create an expression in pieces, so that we can handle very complex
2778 expressions that may be ANTIC, but not necessary GIMPLE.
2779 BLOCK is the basic block the expression will be inserted into,
2780 EXPR is the expression to insert (in value form)
2781 STMTS is a statement list to append the necessary insertions into.
2783 This function will die if we hit some value that shouldn't be
2784 ANTIC but is (IE there is no leader for it, or its components).
2785 The function returns NULL_TREE in case a different antic expression
2786 has to be inserted first.
2787 This function may also generate expressions that are themselves
2788 partially or fully redundant. Those that are will be either made
2789 fully redundant during the next iteration of insert (for partially
2790 redundant ones), or eliminated by eliminate (for fully redundant
2791 ones). */
2793 static tree
2794 create_expression_by_pieces (basic_block block, pre_expr expr,
2795 gimple_seq *stmts, tree type)
2797 tree name;
2798 tree folded;
2799 gimple_seq forced_stmts = NULL;
2800 unsigned int value_id;
2801 gimple_stmt_iterator gsi;
2802 tree exprtype = type ? type : get_expr_type (expr);
2803 pre_expr nameexpr;
2804 gassign *newstmt;
2806 switch (expr->kind)
2808 /* We may hit the NAME/CONSTANT case if we have to convert types
2809 that value numbering saw through. */
2810 case NAME:
2811 folded = PRE_EXPR_NAME (expr);
2812 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
2813 return NULL_TREE;
2814 if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2815 return folded;
2816 break;
2817 case CONSTANT:
2819 folded = PRE_EXPR_CONSTANT (expr);
2820 tree tem = fold_convert (exprtype, folded);
2821 if (is_gimple_min_invariant (tem))
2822 return tem;
2823 break;
2825 case REFERENCE:
2826 if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
2828 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2829 unsigned int operand = 1;
2830 vn_reference_op_t currop = &ref->operands[0];
2831 tree sc = NULL_TREE;
2832 tree fn = find_or_generate_expression (block, currop->op0, stmts);
2833 if (!fn)
2834 return NULL_TREE;
2835 if (currop->op1)
2837 sc = find_or_generate_expression (block, currop->op1, stmts);
2838 if (!sc)
2839 return NULL_TREE;
2841 auto_vec<tree> args (ref->operands.length () - 1);
2842 while (operand < ref->operands.length ())
2844 tree arg = create_component_ref_by_pieces_1 (block, ref,
2845 &operand, stmts);
2846 if (!arg)
2847 return NULL_TREE;
2848 args.quick_push (arg);
2850 gcall *call = gimple_build_call_vec (fn, args);
2851 gimple_set_location (call, expr->loc);
2852 gimple_call_set_fntype (call, currop->type);
2853 if (sc)
2854 gimple_call_set_chain (call, sc);
2855 tree forcedname = make_ssa_name (TREE_TYPE (currop->type));
2856 gimple_call_set_lhs (call, forcedname);
2857 /* There's no CCP pass after PRE which would re-compute alignment
2858 information so make sure we re-materialize this here. */
2859 if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED)
2860 && args.length () - 2 <= 1
2861 && tree_fits_uhwi_p (args[1])
2862 && (args.length () != 3 || tree_fits_uhwi_p (args[2])))
2864 unsigned HOST_WIDE_INT halign = tree_to_uhwi (args[1]);
2865 unsigned HOST_WIDE_INT hmisalign
2866 = args.length () == 3 ? tree_to_uhwi (args[2]) : 0;
2867 if ((halign & (halign - 1)) == 0
2868 && (hmisalign & ~(halign - 1)) == 0
2869 && (unsigned int)halign != 0)
2870 set_ptr_info_alignment (get_ptr_info (forcedname),
2871 halign, hmisalign);
2873 gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
2874 gimple_seq_add_stmt_without_update (&forced_stmts, call);
2875 folded = forcedname;
2877 else
2879 folded = create_component_ref_by_pieces (block,
2880 PRE_EXPR_REFERENCE (expr),
2881 stmts);
2882 if (!folded)
2883 return NULL_TREE;
2884 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2885 newstmt = gimple_build_assign (name, folded);
2886 gimple_set_location (newstmt, expr->loc);
2887 gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2888 gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
2889 folded = name;
2891 break;
2892 case NARY:
2894 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2895 tree *genop = XALLOCAVEC (tree, nary->length);
2896 unsigned i;
2897 for (i = 0; i < nary->length; ++i)
2899 genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2900 if (!genop[i])
2901 return NULL_TREE;
2902 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2903 may have conversions stripped. */
2904 if (nary->opcode == POINTER_PLUS_EXPR)
2906 if (i == 0)
2907 genop[i] = gimple_convert (&forced_stmts,
2908 nary->type, genop[i]);
2909 else if (i == 1)
2910 genop[i] = gimple_convert (&forced_stmts,
2911 sizetype, genop[i]);
2913 else
2914 genop[i] = gimple_convert (&forced_stmts,
2915 TREE_TYPE (nary->op[i]), genop[i]);
2917 if (nary->opcode == CONSTRUCTOR)
2919 vec<constructor_elt, va_gc> *elts = NULL;
2920 for (i = 0; i < nary->length; ++i)
2921 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2922 folded = build_constructor (nary->type, elts);
2923 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2924 newstmt = gimple_build_assign (name, folded);
2925 gimple_set_location (newstmt, expr->loc);
2926 gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2927 folded = name;
2929 else
2931 switch (nary->length)
2933 case 1:
2934 folded = gimple_build (&forced_stmts, expr->loc,
2935 nary->opcode, nary->type, genop[0]);
2936 break;
2937 case 2:
2938 folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
2939 nary->type, genop[0], genop[1]);
2940 break;
2941 case 3:
2942 folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
2943 nary->type, genop[0], genop[1],
2944 genop[2]);
2945 break;
2946 default:
2947 gcc_unreachable ();
2951 break;
2952 default:
2953 gcc_unreachable ();
2956 folded = gimple_convert (&forced_stmts, exprtype, folded);
2958 /* If there is nothing to insert, return the simplified result. */
2959 if (gimple_seq_empty_p (forced_stmts))
2960 return folded;
2961 /* If we simplified to a constant return it and discard eventually
2962 built stmts. */
2963 if (is_gimple_min_invariant (folded))
2965 gimple_seq_discard (forced_stmts);
2966 return folded;
2968 /* Likewise if we simplified to sth not queued for insertion. */
2969 bool found = false;
2970 gsi = gsi_last (forced_stmts);
2971 for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2973 gimple *stmt = gsi_stmt (gsi);
2974 tree forcedname = gimple_get_lhs (stmt);
2975 if (forcedname == folded)
2977 found = true;
2978 break;
2981 if (! found)
2983 gimple_seq_discard (forced_stmts);
2984 return folded;
2986 gcc_assert (TREE_CODE (folded) == SSA_NAME);
2988 /* If we have any intermediate expressions to the value sets, add them
2989 to the value sets and chain them in the instruction stream. */
2990 if (forced_stmts)
2992 gsi = gsi_start (forced_stmts);
2993 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2995 gimple *stmt = gsi_stmt (gsi);
2996 tree forcedname = gimple_get_lhs (stmt);
2997 pre_expr nameexpr;
2999 if (forcedname != folded)
3001 vn_ssa_aux_t vn_info = VN_INFO (forcedname);
3002 vn_info->valnum = forcedname;
3003 vn_info->value_id = get_next_value_id ();
3004 nameexpr = get_or_alloc_expr_for_name (forcedname);
3005 add_to_value (vn_info->value_id, nameexpr);
3006 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3007 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3010 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
3012 gimple_seq_add_seq (stmts, forced_stmts);
3015 name = folded;
3017 /* Fold the last statement. */
3018 gsi = gsi_last (*stmts);
3019 if (fold_stmt_inplace (&gsi))
3020 update_stmt (gsi_stmt (gsi));
3022 /* Add a value number to the temporary.
3023 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
3024 we are creating the expression by pieces, and this particular piece of
3025 the expression may have been represented. There is no harm in replacing
3026 here. */
3027 value_id = get_expr_value_id (expr);
3028 vn_ssa_aux_t vn_info = VN_INFO (name);
3029 vn_info->value_id = value_id;
3030 vn_info->valnum = vn_valnum_from_value_id (value_id);
3031 if (vn_info->valnum == NULL_TREE)
3032 vn_info->valnum = name;
3033 gcc_assert (vn_info->valnum != NULL_TREE);
3034 nameexpr = get_or_alloc_expr_for_name (name);
3035 add_to_value (value_id, nameexpr);
3036 if (NEW_SETS (block))
3037 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3038 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3040 pre_stats.insertions++;
3041 if (dump_file && (dump_flags & TDF_DETAILS))
3043 fprintf (dump_file, "Inserted ");
3044 print_gimple_stmt (dump_file, gsi_stmt (gsi_last (*stmts)), 0);
3045 fprintf (dump_file, " in predecessor %d (%04d)\n",
3046 block->index, value_id);
3049 return name;
3053 /* Insert the to-be-made-available values of expression EXPRNUM for each
3054 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3055 merge the result with a phi node, given the same value number as
3056 NODE. Return true if we have inserted new stuff. */
3058 static bool
3059 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3060 vec<pre_expr> avail)
3062 pre_expr expr = expression_for_id (exprnum);
3063 pre_expr newphi;
3064 unsigned int val = get_expr_value_id (expr);
3065 edge pred;
3066 bool insertions = false;
3067 bool nophi = false;
3068 basic_block bprime;
3069 pre_expr eprime;
3070 edge_iterator ei;
3071 tree type = get_expr_type (expr);
3072 tree temp;
3073 gphi *phi;
3075 /* Make sure we aren't creating an induction variable. */
3076 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3078 bool firstinsideloop = false;
3079 bool secondinsideloop = false;
3080 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3081 EDGE_PRED (block, 0)->src);
3082 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3083 EDGE_PRED (block, 1)->src);
3084 /* Induction variables only have one edge inside the loop. */
3085 if ((firstinsideloop ^ secondinsideloop)
3086 && expr->kind != REFERENCE)
3088 if (dump_file && (dump_flags & TDF_DETAILS))
3089 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3090 nophi = true;
3094 /* Make the necessary insertions. */
3095 FOR_EACH_EDGE (pred, ei, block->preds)
3097 gimple_seq stmts = NULL;
3098 tree builtexpr;
3099 bprime = pred->src;
3100 eprime = avail[pred->dest_idx];
3101 builtexpr = create_expression_by_pieces (bprime, eprime,
3102 &stmts, type);
3103 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3104 if (!gimple_seq_empty_p (stmts))
3106 basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
3107 gcc_assert (! new_bb);
3108 insertions = true;
3110 if (!builtexpr)
3112 /* We cannot insert a PHI node if we failed to insert
3113 on one edge. */
3114 nophi = true;
3115 continue;
3117 if (is_gimple_min_invariant (builtexpr))
3118 avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
3119 else
3120 avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3122 /* If we didn't want a phi node, and we made insertions, we still have
3123 inserted new stuff, and thus return true. If we didn't want a phi node,
3124 and didn't make insertions, we haven't added anything new, so return
3125 false. */
3126 if (nophi && insertions)
3127 return true;
3128 else if (nophi && !insertions)
3129 return false;
3131 /* Now build a phi for the new variable. */
3132 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3133 phi = create_phi_node (temp, block);
3135 vn_ssa_aux_t vn_info = VN_INFO (temp);
3136 vn_info->value_id = val;
3137 vn_info->valnum = vn_valnum_from_value_id (val);
3138 if (vn_info->valnum == NULL_TREE)
3139 vn_info->valnum = temp;
3140 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3141 FOR_EACH_EDGE (pred, ei, block->preds)
3143 pre_expr ae = avail[pred->dest_idx];
3144 gcc_assert (get_expr_type (ae) == type
3145 || useless_type_conversion_p (type, get_expr_type (ae)));
3146 if (ae->kind == CONSTANT)
3147 add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3148 pred, UNKNOWN_LOCATION);
3149 else
3150 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3153 newphi = get_or_alloc_expr_for_name (temp);
3154 add_to_value (val, newphi);
3156 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3157 this insertion, since we test for the existence of this value in PHI_GEN
3158 before proceeding with the partial redundancy checks in insert_aux.
3160 The value may exist in AVAIL_OUT, in particular, it could be represented
3161 by the expression we are trying to eliminate, in which case we want the
3162 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3163 inserted there.
3165 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3166 this block, because if it did, it would have existed in our dominator's
3167 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3170 bitmap_insert_into_set (PHI_GEN (block), newphi);
3171 bitmap_value_replace_in_set (AVAIL_OUT (block),
3172 newphi);
3173 bitmap_insert_into_set (NEW_SETS (block),
3174 newphi);
3176 /* If we insert a PHI node for a conversion of another PHI node
3177 in the same basic-block try to preserve range information.
3178 This is important so that followup loop passes receive optimal
3179 number of iteration analysis results. See PR61743. */
3180 if (expr->kind == NARY
3181 && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3182 && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3183 && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3184 && INTEGRAL_TYPE_P (type)
3185 && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3186 && (TYPE_PRECISION (type)
3187 >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3188 && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3190 wide_int min, max;
3191 if (get_range_info (expr->u.nary->op[0], &min, &max) == VR_RANGE
3192 && !wi::neg_p (min, SIGNED)
3193 && !wi::neg_p (max, SIGNED))
3194 /* Just handle extension and sign-changes of all-positive ranges. */
3195 set_range_info (temp,
3196 SSA_NAME_RANGE_TYPE (expr->u.nary->op[0]),
3197 wide_int_storage::from (min, TYPE_PRECISION (type),
3198 TYPE_SIGN (type)),
3199 wide_int_storage::from (max, TYPE_PRECISION (type),
3200 TYPE_SIGN (type)));
3203 if (dump_file && (dump_flags & TDF_DETAILS))
3205 fprintf (dump_file, "Created phi ");
3206 print_gimple_stmt (dump_file, phi, 0);
3207 fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3209 pre_stats.phis++;
3210 return true;
3215 /* Perform insertion of partially redundant or hoistable values.
3216 For BLOCK, do the following:
3217 1. Propagate the NEW_SETS of the dominator into the current block.
3218 If the block has multiple predecessors,
3219 2a. Iterate over the ANTIC expressions for the block to see if
3220 any of them are partially redundant.
3221 2b. If so, insert them into the necessary predecessors to make
3222 the expression fully redundant.
3223 2c. Insert a new PHI merging the values of the predecessors.
3224 2d. Insert the new PHI, and the new expressions, into the
3225 NEW_SETS set.
3226 If the block has multiple successors,
3227 3a. Iterate over the ANTIC values for the block to see if
3228 any of them are good candidates for hoisting.
3229 3b. If so, insert expressions computing the values in BLOCK,
3230 and add the new expressions into the NEW_SETS set.
3231 4. Recursively call ourselves on the dominator children of BLOCK.
3233 Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3234 do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3235 done in do_hoist_insertion.
3238 static bool
3239 do_pre_regular_insertion (basic_block block, basic_block dom)
3241 bool new_stuff = false;
3242 vec<pre_expr> exprs;
3243 pre_expr expr;
3244 auto_vec<pre_expr, 2> avail;
3245 int i;
3247 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3248 avail.safe_grow (EDGE_COUNT (block->preds), true);
3250 FOR_EACH_VEC_ELT (exprs, i, expr)
3252 if (expr->kind == NARY
3253 || expr->kind == REFERENCE)
3255 unsigned int val;
3256 bool by_some = false;
3257 bool cant_insert = false;
3258 bool all_same = true;
3259 pre_expr first_s = NULL;
3260 edge pred;
3261 basic_block bprime;
3262 pre_expr eprime = NULL;
3263 edge_iterator ei;
3264 pre_expr edoubleprime = NULL;
3265 bool do_insertion = false;
3267 val = get_expr_value_id (expr);
3268 if (bitmap_set_contains_value (PHI_GEN (block), val))
3269 continue;
3270 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3272 if (dump_file && (dump_flags & TDF_DETAILS))
3274 fprintf (dump_file, "Found fully redundant value: ");
3275 print_pre_expr (dump_file, expr);
3276 fprintf (dump_file, "\n");
3278 continue;
3281 FOR_EACH_EDGE (pred, ei, block->preds)
3283 unsigned int vprime;
3285 /* We should never run insertion for the exit block
3286 and so not come across fake pred edges. */
3287 gcc_assert (!(pred->flags & EDGE_FAKE));
3288 bprime = pred->src;
3289 /* We are looking at ANTIC_OUT of bprime. */
3290 eprime = phi_translate (NULL, expr, ANTIC_IN (block), NULL, pred);
3292 /* eprime will generally only be NULL if the
3293 value of the expression, translated
3294 through the PHI for this predecessor, is
3295 undefined. If that is the case, we can't
3296 make the expression fully redundant,
3297 because its value is undefined along a
3298 predecessor path. We can thus break out
3299 early because it doesn't matter what the
3300 rest of the results are. */
3301 if (eprime == NULL)
3303 avail[pred->dest_idx] = NULL;
3304 cant_insert = true;
3305 break;
3308 vprime = get_expr_value_id (eprime);
3309 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3310 vprime);
3311 if (edoubleprime == NULL)
3313 avail[pred->dest_idx] = eprime;
3314 all_same = false;
3316 else
3318 avail[pred->dest_idx] = edoubleprime;
3319 by_some = true;
3320 /* We want to perform insertions to remove a redundancy on
3321 a path in the CFG we want to optimize for speed. */
3322 if (optimize_edge_for_speed_p (pred))
3323 do_insertion = true;
3324 if (first_s == NULL)
3325 first_s = edoubleprime;
3326 else if (!pre_expr_d::equal (first_s, edoubleprime))
3327 all_same = false;
3330 /* If we can insert it, it's not the same value
3331 already existing along every predecessor, and
3332 it's defined by some predecessor, it is
3333 partially redundant. */
3334 if (!cant_insert && !all_same && by_some)
3336 if (!do_insertion)
3338 if (dump_file && (dump_flags & TDF_DETAILS))
3340 fprintf (dump_file, "Skipping partial redundancy for "
3341 "expression ");
3342 print_pre_expr (dump_file, expr);
3343 fprintf (dump_file, " (%04d), no redundancy on to be "
3344 "optimized for speed edge\n", val);
3347 else if (dbg_cnt (treepre_insert))
3349 if (dump_file && (dump_flags & TDF_DETAILS))
3351 fprintf (dump_file, "Found partial redundancy for "
3352 "expression ");
3353 print_pre_expr (dump_file, expr);
3354 fprintf (dump_file, " (%04d)\n",
3355 get_expr_value_id (expr));
3357 if (insert_into_preds_of_block (block,
3358 get_expression_id (expr),
3359 avail))
3360 new_stuff = true;
3363 /* If all edges produce the same value and that value is
3364 an invariant, then the PHI has the same value on all
3365 edges. Note this. */
3366 else if (!cant_insert && all_same)
3368 gcc_assert (edoubleprime->kind == CONSTANT
3369 || edoubleprime->kind == NAME);
3371 tree temp = make_temp_ssa_name (get_expr_type (expr),
3372 NULL, "pretmp");
3373 gassign *assign
3374 = gimple_build_assign (temp,
3375 edoubleprime->kind == CONSTANT ?
3376 PRE_EXPR_CONSTANT (edoubleprime) :
3377 PRE_EXPR_NAME (edoubleprime));
3378 gimple_stmt_iterator gsi = gsi_after_labels (block);
3379 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3381 vn_ssa_aux_t vn_info = VN_INFO (temp);
3382 vn_info->value_id = val;
3383 vn_info->valnum = vn_valnum_from_value_id (val);
3384 if (vn_info->valnum == NULL_TREE)
3385 vn_info->valnum = temp;
3386 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3387 pre_expr newe = get_or_alloc_expr_for_name (temp);
3388 add_to_value (val, newe);
3389 bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3390 bitmap_insert_into_set (NEW_SETS (block), newe);
3395 exprs.release ();
3396 return new_stuff;
3400 /* Perform insertion for partially anticipatable expressions. There
3401 is only one case we will perform insertion for these. This case is
3402 if the expression is partially anticipatable, and fully available.
3403 In this case, we know that putting it earlier will enable us to
3404 remove the later computation. */
3406 static bool
3407 do_pre_partial_partial_insertion (basic_block block, basic_block dom)
3409 bool new_stuff = false;
3410 vec<pre_expr> exprs;
3411 pre_expr expr;
3412 auto_vec<pre_expr, 2> avail;
3413 int i;
3415 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3416 avail.safe_grow (EDGE_COUNT (block->preds), true);
3418 FOR_EACH_VEC_ELT (exprs, i, expr)
3420 if (expr->kind == NARY
3421 || expr->kind == REFERENCE)
3423 unsigned int val;
3424 bool by_all = true;
3425 bool cant_insert = false;
3426 edge pred;
3427 basic_block bprime;
3428 pre_expr eprime = NULL;
3429 edge_iterator ei;
3431 val = get_expr_value_id (expr);
3432 if (bitmap_set_contains_value (PHI_GEN (block), val))
3433 continue;
3434 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3435 continue;
3437 FOR_EACH_EDGE (pred, ei, block->preds)
3439 unsigned int vprime;
3440 pre_expr edoubleprime;
3442 /* We should never run insertion for the exit block
3443 and so not come across fake pred edges. */
3444 gcc_assert (!(pred->flags & EDGE_FAKE));
3445 bprime = pred->src;
3446 eprime = phi_translate (NULL, expr, ANTIC_IN (block),
3447 PA_IN (block), pred);
3449 /* eprime will generally only be NULL if the
3450 value of the expression, translated
3451 through the PHI for this predecessor, is
3452 undefined. If that is the case, we can't
3453 make the expression fully redundant,
3454 because its value is undefined along a
3455 predecessor path. We can thus break out
3456 early because it doesn't matter what the
3457 rest of the results are. */
3458 if (eprime == NULL)
3460 avail[pred->dest_idx] = NULL;
3461 cant_insert = true;
3462 break;
3465 vprime = get_expr_value_id (eprime);
3466 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3467 avail[pred->dest_idx] = edoubleprime;
3468 if (edoubleprime == NULL)
3470 by_all = false;
3471 break;
3475 /* If we can insert it, it's not the same value
3476 already existing along every predecessor, and
3477 it's defined by some predecessor, it is
3478 partially redundant. */
3479 if (!cant_insert && by_all)
3481 edge succ;
3482 bool do_insertion = false;
3484 /* Insert only if we can remove a later expression on a path
3485 that we want to optimize for speed.
3486 The phi node that we will be inserting in BLOCK is not free,
3487 and inserting it for the sake of !optimize_for_speed successor
3488 may cause regressions on the speed path. */
3489 FOR_EACH_EDGE (succ, ei, block->succs)
3491 if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3492 || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3494 if (optimize_edge_for_speed_p (succ))
3495 do_insertion = true;
3499 if (!do_insertion)
3501 if (dump_file && (dump_flags & TDF_DETAILS))
3503 fprintf (dump_file, "Skipping partial partial redundancy "
3504 "for expression ");
3505 print_pre_expr (dump_file, expr);
3506 fprintf (dump_file, " (%04d), not (partially) anticipated "
3507 "on any to be optimized for speed edges\n", val);
3510 else if (dbg_cnt (treepre_insert))
3512 pre_stats.pa_insert++;
3513 if (dump_file && (dump_flags & TDF_DETAILS))
3515 fprintf (dump_file, "Found partial partial redundancy "
3516 "for expression ");
3517 print_pre_expr (dump_file, expr);
3518 fprintf (dump_file, " (%04d)\n",
3519 get_expr_value_id (expr));
3521 if (insert_into_preds_of_block (block,
3522 get_expression_id (expr),
3523 avail))
3524 new_stuff = true;
3530 exprs.release ();
3531 return new_stuff;
3534 /* Insert expressions in BLOCK to compute hoistable values up.
3535 Return TRUE if something was inserted, otherwise return FALSE.
3536 The caller has to make sure that BLOCK has at least two successors. */
3538 static bool
3539 do_hoist_insertion (basic_block block)
3541 edge e;
3542 edge_iterator ei;
3543 bool new_stuff = false;
3544 unsigned i;
3545 gimple_stmt_iterator last;
3547 /* At least two successors, or else... */
3548 gcc_assert (EDGE_COUNT (block->succs) >= 2);
3550 /* Check that all successors of BLOCK are dominated by block.
3551 We could use dominated_by_p() for this, but actually there is a much
3552 quicker check: any successor that is dominated by BLOCK can't have
3553 more than one predecessor edge. */
3554 FOR_EACH_EDGE (e, ei, block->succs)
3555 if (! single_pred_p (e->dest))
3556 return false;
3558 /* Determine the insertion point. If we cannot safely insert before
3559 the last stmt if we'd have to, bail out. */
3560 last = gsi_last_bb (block);
3561 if (!gsi_end_p (last)
3562 && !is_ctrl_stmt (gsi_stmt (last))
3563 && stmt_ends_bb_p (gsi_stmt (last)))
3564 return false;
3566 /* Compute the set of hoistable expressions from ANTIC_IN. First compute
3567 hoistable values. */
3568 bitmap_set hoistable_set;
3570 /* A hoistable value must be in ANTIC_IN(block)
3571 but not in AVAIL_OUT(BLOCK). */
3572 bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
3573 bitmap_and_compl (&hoistable_set.values,
3574 &ANTIC_IN (block)->values, &AVAIL_OUT (block)->values);
3576 /* Short-cut for a common case: hoistable_set is empty. */
3577 if (bitmap_empty_p (&hoistable_set.values))
3578 return false;
3580 /* Compute which of the hoistable values is in AVAIL_OUT of
3581 at least one of the successors of BLOCK. */
3582 bitmap_head availout_in_some;
3583 bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
3584 FOR_EACH_EDGE (e, ei, block->succs)
3585 /* Do not consider expressions solely because their availability
3586 on loop exits. They'd be ANTIC-IN throughout the whole loop
3587 and thus effectively hoisted across loops by combination of
3588 PRE and hoisting. */
3589 if (! loop_exit_edge_p (block->loop_father, e))
3590 bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
3591 &AVAIL_OUT (e->dest)->values);
3592 bitmap_clear (&hoistable_set.values);
3594 /* Short-cut for a common case: availout_in_some is empty. */
3595 if (bitmap_empty_p (&availout_in_some))
3596 return false;
3598 /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */
3599 bitmap_move (&hoistable_set.values, &availout_in_some);
3600 hoistable_set.expressions = ANTIC_IN (block)->expressions;
3602 /* Now finally construct the topological-ordered expression set. */
3603 vec<pre_expr> exprs = sorted_array_from_bitmap_set (&hoistable_set);
3605 bitmap_clear (&hoistable_set.values);
3607 /* If there are candidate values for hoisting, insert expressions
3608 strategically to make the hoistable expressions fully redundant. */
3609 pre_expr expr;
3610 FOR_EACH_VEC_ELT (exprs, i, expr)
3612 /* While we try to sort expressions topologically above the
3613 sorting doesn't work out perfectly. Catch expressions we
3614 already inserted. */
3615 unsigned int value_id = get_expr_value_id (expr);
3616 if (bitmap_set_contains_value (AVAIL_OUT (block), value_id))
3618 if (dump_file && (dump_flags & TDF_DETAILS))
3620 fprintf (dump_file,
3621 "Already inserted expression for ");
3622 print_pre_expr (dump_file, expr);
3623 fprintf (dump_file, " (%04d)\n", value_id);
3625 continue;
3628 /* If we end up with a punned expression representation and this
3629 happens to be a float typed one give up - we can't know for
3630 sure whether all paths perform the floating-point load we are
3631 about to insert and on some targets this can cause correctness
3632 issues. See PR88240. */
3633 if (expr->kind == REFERENCE
3634 && PRE_EXPR_REFERENCE (expr)->punned
3635 && FLOAT_TYPE_P (get_expr_type (expr)))
3636 continue;
3638 /* OK, we should hoist this value. Perform the transformation. */
3639 pre_stats.hoist_insert++;
3640 if (dump_file && (dump_flags & TDF_DETAILS))
3642 fprintf (dump_file,
3643 "Inserting expression in block %d for code hoisting: ",
3644 block->index);
3645 print_pre_expr (dump_file, expr);
3646 fprintf (dump_file, " (%04d)\n", value_id);
3649 gimple_seq stmts = NULL;
3650 tree res = create_expression_by_pieces (block, expr, &stmts,
3651 get_expr_type (expr));
3653 /* Do not return true if expression creation ultimately
3654 did not insert any statements. */
3655 if (gimple_seq_empty_p (stmts))
3656 res = NULL_TREE;
3657 else
3659 if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
3660 gsi_insert_seq_before (&last, stmts, GSI_SAME_STMT);
3661 else
3662 gsi_insert_seq_after (&last, stmts, GSI_NEW_STMT);
3665 /* Make sure to not return true if expression creation ultimately
3666 failed but also make sure to insert any stmts produced as they
3667 are tracked in inserted_exprs. */
3668 if (! res)
3669 continue;
3671 new_stuff = true;
3674 exprs.release ();
3676 return new_stuff;
3679 /* Perform insertion of partially redundant and hoistable values. */
3681 static void
3682 insert (void)
3684 basic_block bb;
3686 FOR_ALL_BB_FN (bb, cfun)
3687 NEW_SETS (bb) = bitmap_set_new ();
3689 int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3690 int rpo_num = pre_and_rev_post_order_compute (NULL, rpo, false);
3692 int num_iterations = 0;
3693 bool changed;
3696 num_iterations++;
3697 if (dump_file && dump_flags & TDF_DETAILS)
3698 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3700 changed = false;
3701 /* Insert expressions for hoisting. Do a backward walk here since
3702 inserting into BLOCK exposes new opportunities in its predecessors.
3703 Since PRE and hoist insertions can cause back-to-back iteration
3704 limit that on the hoist side. */
3705 if (flag_code_hoisting
3706 && num_iterations <= param_max_pre_hoist_insert_iterations)
3707 for (int idx = rpo_num - 1; idx >= 0; --idx)
3709 basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
3710 if (EDGE_COUNT (block->succs) >= 2)
3711 changed |= do_hoist_insertion (block);
3713 for (int idx = 0; idx < rpo_num; ++idx)
3715 basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
3716 basic_block dom = get_immediate_dominator (CDI_DOMINATORS, block);
3717 if (dom)
3719 unsigned i;
3720 bitmap_iterator bi;
3721 bitmap_set_t newset;
3723 /* First, update the AVAIL_OUT set with anything we may have
3724 inserted higher up in the dominator tree. */
3725 newset = NEW_SETS (dom);
3726 if (newset)
3728 /* Note that we need to value_replace both NEW_SETS, and
3729 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3730 represented by some non-simple expression here that we want
3731 to replace it with. */
3732 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3734 pre_expr expr = expression_for_id (i);
3735 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3736 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3740 /* Insert expressions for partial redundancies. */
3741 if (flag_tree_pre && !single_pred_p (block))
3743 changed |= do_pre_regular_insertion (block, dom);
3744 if (do_partial_partial)
3745 changed |= do_pre_partial_partial_insertion (block, dom);
3750 /* Clear the NEW sets before the next iteration. We have already
3751 fully propagated its contents. */
3752 if (changed)
3753 FOR_ALL_BB_FN (bb, cfun)
3754 bitmap_set_free (NEW_SETS (bb));
3756 while (changed);
3758 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3760 free (rpo);
3764 /* Compute the AVAIL set for all basic blocks.
3766 This function performs value numbering of the statements in each basic
3767 block. The AVAIL sets are built from information we glean while doing
3768 this value numbering, since the AVAIL sets contain only one entry per
3769 value.
3771 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3772 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3774 static void
3775 compute_avail (void)
3778 basic_block block, son;
3779 basic_block *worklist;
3780 size_t sp = 0;
3781 unsigned i;
3782 tree name;
3784 /* We pretend that default definitions are defined in the entry block.
3785 This includes function arguments and the static chain decl. */
3786 FOR_EACH_SSA_NAME (i, name, cfun)
3788 pre_expr e;
3789 if (!SSA_NAME_IS_DEFAULT_DEF (name)
3790 || has_zero_uses (name)
3791 || virtual_operand_p (name))
3792 continue;
3794 e = get_or_alloc_expr_for_name (name);
3795 add_to_value (get_expr_value_id (e), e);
3796 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), e);
3797 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3801 if (dump_file && (dump_flags & TDF_DETAILS))
3803 print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3804 "tmp_gen", ENTRY_BLOCK);
3805 print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3806 "avail_out", ENTRY_BLOCK);
3809 /* Allocate the worklist. */
3810 worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
3812 /* Seed the algorithm by putting the dominator children of the entry
3813 block on the worklist. */
3814 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun));
3815 son;
3816 son = next_dom_son (CDI_DOMINATORS, son))
3817 worklist[sp++] = son;
3819 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun))
3820 = ssa_default_def (cfun, gimple_vop (cfun));
3822 /* Loop until the worklist is empty. */
3823 while (sp)
3825 gimple *stmt;
3826 basic_block dom;
3828 /* Pick a block from the worklist. */
3829 block = worklist[--sp];
3830 vn_context_bb = block;
3832 /* Initially, the set of available values in BLOCK is that of
3833 its immediate dominator. */
3834 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3835 if (dom)
3837 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3838 BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
3841 /* Generate values for PHI nodes. */
3842 for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
3843 gsi_next (&gsi))
3845 tree result = gimple_phi_result (gsi.phi ());
3847 /* We have no need for virtual phis, as they don't represent
3848 actual computations. */
3849 if (virtual_operand_p (result))
3851 BB_LIVE_VOP_ON_EXIT (block) = result;
3852 continue;
3855 pre_expr e = get_or_alloc_expr_for_name (result);
3856 add_to_value (get_expr_value_id (e), e);
3857 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3858 bitmap_insert_into_set (PHI_GEN (block), e);
3861 BB_MAY_NOTRETURN (block) = 0;
3863 /* Now compute value numbers and populate value sets with all
3864 the expressions computed in BLOCK. */
3865 for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
3866 gsi_next (&gsi))
3868 ssa_op_iter iter;
3869 tree op;
3871 stmt = gsi_stmt (gsi);
3873 /* Cache whether the basic-block has any non-visible side-effect
3874 or control flow.
3875 If this isn't a call or it is the last stmt in the
3876 basic-block then the CFG represents things correctly. */
3877 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3879 /* Non-looping const functions always return normally.
3880 Otherwise the call might not return or have side-effects
3881 that forbids hoisting possibly trapping expressions
3882 before it. */
3883 int flags = gimple_call_flags (stmt);
3884 if (!(flags & ECF_CONST)
3885 || (flags & ECF_LOOPING_CONST_OR_PURE))
3886 BB_MAY_NOTRETURN (block) = 1;
3889 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3891 pre_expr e = get_or_alloc_expr_for_name (op);
3893 add_to_value (get_expr_value_id (e), e);
3894 bitmap_insert_into_set (TMP_GEN (block), e);
3895 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3898 if (gimple_vdef (stmt))
3899 BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
3901 if (gimple_has_side_effects (stmt)
3902 || stmt_could_throw_p (cfun, stmt)
3903 || is_gimple_debug (stmt))
3904 continue;
3906 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3908 if (ssa_undefined_value_p (op))
3909 continue;
3910 pre_expr e = get_or_alloc_expr_for_name (op);
3911 bitmap_value_insert_into_set (EXP_GEN (block), e);
3914 switch (gimple_code (stmt))
3916 case GIMPLE_RETURN:
3917 continue;
3919 case GIMPLE_CALL:
3921 vn_reference_t ref;
3922 vn_reference_s ref1;
3923 pre_expr result = NULL;
3925 /* We can value number only calls to real functions. */
3926 if (gimple_call_internal_p (stmt))
3927 continue;
3929 vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
3930 if (!ref)
3931 continue;
3933 /* If the value of the call is not invalidated in
3934 this block until it is computed, add the expression
3935 to EXP_GEN. */
3936 if (!gimple_vuse (stmt)
3937 || gimple_code
3938 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3939 || gimple_bb (SSA_NAME_DEF_STMT
3940 (gimple_vuse (stmt))) != block)
3942 result = get_or_alloc_expr_for_reference
3943 (ref, gimple_location (stmt));
3944 add_to_value (get_expr_value_id (result), result);
3945 bitmap_value_insert_into_set (EXP_GEN (block), result);
3947 continue;
3950 case GIMPLE_ASSIGN:
3952 pre_expr result = NULL;
3953 switch (vn_get_stmt_kind (stmt))
3955 case VN_NARY:
3957 enum tree_code code = gimple_assign_rhs_code (stmt);
3958 vn_nary_op_t nary;
3960 /* COND_EXPR and VEC_COND_EXPR are awkward in
3961 that they contain an embedded complex expression.
3962 Don't even try to shove those through PRE. */
3963 if (code == COND_EXPR
3964 || code == VEC_COND_EXPR)
3965 continue;
3967 vn_nary_op_lookup_stmt (stmt, &nary);
3968 if (!nary || nary->predicated_values)
3969 continue;
3971 /* If the NARY traps and there was a preceding
3972 point in the block that might not return avoid
3973 adding the nary to EXP_GEN. */
3974 if (BB_MAY_NOTRETURN (block)
3975 && vn_nary_may_trap (nary))
3976 continue;
3978 result = get_or_alloc_expr_for_nary
3979 (nary, gimple_location (stmt));
3980 break;
3983 case VN_REFERENCE:
3985 tree rhs1 = gimple_assign_rhs1 (stmt);
3986 ao_ref rhs1_ref;
3987 ao_ref_init (&rhs1_ref, rhs1);
3988 alias_set_type set = ao_ref_alias_set (&rhs1_ref);
3989 alias_set_type base_set
3990 = ao_ref_base_alias_set (&rhs1_ref);
3991 vec<vn_reference_op_s> operands
3992 = vn_reference_operands_for_lookup (rhs1);
3993 vn_reference_t ref;
3994 vn_reference_lookup_pieces (gimple_vuse (stmt), set,
3995 base_set, TREE_TYPE (rhs1),
3996 operands, &ref, VN_WALK);
3997 if (!ref)
3999 operands.release ();
4000 continue;
4003 /* If the REFERENCE traps and there was a preceding
4004 point in the block that might not return avoid
4005 adding the reference to EXP_GEN. */
4006 if (BB_MAY_NOTRETURN (block)
4007 && vn_reference_may_trap (ref))
4009 operands.release ();
4010 continue;
4013 /* If the value of the reference is not invalidated in
4014 this block until it is computed, add the expression
4015 to EXP_GEN. */
4016 if (gimple_vuse (stmt))
4018 gimple *def_stmt;
4019 bool ok = true;
4020 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
4021 while (!gimple_nop_p (def_stmt)
4022 && gimple_code (def_stmt) != GIMPLE_PHI
4023 && gimple_bb (def_stmt) == block)
4025 if (stmt_may_clobber_ref_p
4026 (def_stmt, gimple_assign_rhs1 (stmt)))
4028 ok = false;
4029 break;
4031 def_stmt
4032 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
4034 if (!ok)
4036 operands.release ();
4037 continue;
4041 /* If the load was value-numbered to another
4042 load make sure we do not use its expression
4043 for insertion if it wouldn't be a valid
4044 replacement. */
4045 /* At the momemt we have a testcase
4046 for hoist insertion of aligned vs. misaligned
4047 variants in gcc.dg/torture/pr65270-1.c thus
4048 with just alignment to be considered we can
4049 simply replace the expression in the hashtable
4050 with the most conservative one. */
4051 vn_reference_op_t ref1 = &ref->operands.last ();
4052 while (ref1->opcode != TARGET_MEM_REF
4053 && ref1->opcode != MEM_REF
4054 && ref1 != &ref->operands[0])
4055 --ref1;
4056 vn_reference_op_t ref2 = &operands.last ();
4057 while (ref2->opcode != TARGET_MEM_REF
4058 && ref2->opcode != MEM_REF
4059 && ref2 != &operands[0])
4060 --ref2;
4061 if ((ref1->opcode == TARGET_MEM_REF
4062 || ref1->opcode == MEM_REF)
4063 && (TYPE_ALIGN (ref1->type)
4064 > TYPE_ALIGN (ref2->type)))
4065 ref1->type
4066 = build_aligned_type (ref1->type,
4067 TYPE_ALIGN (ref2->type));
4068 /* TBAA behavior is an obvious part so make sure
4069 that the hashtable one covers this as well
4070 by adjusting the ref alias set and its base. */
4071 if (ref->set == set
4072 || alias_set_subset_of (set, ref->set))
4074 else if (alias_set_subset_of (ref->set, set))
4076 ref->set = set;
4077 if (ref1->opcode == MEM_REF)
4078 ref1->op0
4079 = wide_int_to_tree (TREE_TYPE (ref2->op0),
4080 wi::to_wide (ref1->op0));
4081 else
4082 ref1->op2
4083 = wide_int_to_tree (TREE_TYPE (ref2->op2),
4084 wi::to_wide (ref1->op2));
4086 else
4088 ref->set = 0;
4089 if (ref1->opcode == MEM_REF)
4090 ref1->op0
4091 = wide_int_to_tree (ptr_type_node,
4092 wi::to_wide (ref1->op0));
4093 else
4094 ref1->op2
4095 = wide_int_to_tree (ptr_type_node,
4096 wi::to_wide (ref1->op2));
4098 operands.release ();
4100 result = get_or_alloc_expr_for_reference
4101 (ref, gimple_location (stmt));
4102 break;
4105 default:
4106 continue;
4109 add_to_value (get_expr_value_id (result), result);
4110 bitmap_value_insert_into_set (EXP_GEN (block), result);
4111 continue;
4113 default:
4114 break;
4118 if (dump_file && (dump_flags & TDF_DETAILS))
4120 print_bitmap_set (dump_file, EXP_GEN (block),
4121 "exp_gen", block->index);
4122 print_bitmap_set (dump_file, PHI_GEN (block),
4123 "phi_gen", block->index);
4124 print_bitmap_set (dump_file, TMP_GEN (block),
4125 "tmp_gen", block->index);
4126 print_bitmap_set (dump_file, AVAIL_OUT (block),
4127 "avail_out", block->index);
4130 /* Put the dominator children of BLOCK on the worklist of blocks
4131 to compute available sets for. */
4132 for (son = first_dom_son (CDI_DOMINATORS, block);
4133 son;
4134 son = next_dom_son (CDI_DOMINATORS, son))
4135 worklist[sp++] = son;
4137 vn_context_bb = NULL;
4139 free (worklist);
4143 /* Initialize data structures used by PRE. */
4145 static void
4146 init_pre (void)
4148 basic_block bb;
4150 next_expression_id = 1;
4151 expressions.create (0);
4152 expressions.safe_push (NULL);
4153 value_expressions.create (get_max_value_id () + 1);
4154 value_expressions.quick_grow_cleared (get_max_value_id () + 1);
4155 constant_value_expressions.create (get_max_constant_value_id () + 1);
4156 constant_value_expressions.quick_grow_cleared (get_max_constant_value_id () + 1);
4157 name_to_id.create (0);
4159 inserted_exprs = BITMAP_ALLOC (NULL);
4161 connect_infinite_loops_to_exit ();
4162 memset (&pre_stats, 0, sizeof (pre_stats));
4164 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4166 calculate_dominance_info (CDI_DOMINATORS);
4168 bitmap_obstack_initialize (&grand_bitmap_obstack);
4169 expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4170 FOR_ALL_BB_FN (bb, cfun)
4172 EXP_GEN (bb) = bitmap_set_new ();
4173 PHI_GEN (bb) = bitmap_set_new ();
4174 TMP_GEN (bb) = bitmap_set_new ();
4175 AVAIL_OUT (bb) = bitmap_set_new ();
4176 PHI_TRANS_TABLE (bb) = NULL;
4181 /* Deallocate data structures used by PRE. */
4183 static void
4184 fini_pre ()
4186 value_expressions.release ();
4187 constant_value_expressions.release ();
4188 expressions.release ();
4189 BITMAP_FREE (inserted_exprs);
4190 bitmap_obstack_release (&grand_bitmap_obstack);
4191 bitmap_set_pool.release ();
4192 pre_expr_pool.release ();
4193 delete expression_to_id;
4194 expression_to_id = NULL;
4195 name_to_id.release ();
4197 basic_block bb;
4198 FOR_ALL_BB_FN (bb, cfun)
4199 if (bb->aux && PHI_TRANS_TABLE (bb))
4200 delete PHI_TRANS_TABLE (bb);
4201 free_aux_for_blocks ();
4204 namespace {
4206 const pass_data pass_data_pre =
4208 GIMPLE_PASS, /* type */
4209 "pre", /* name */
4210 OPTGROUP_NONE, /* optinfo_flags */
4211 TV_TREE_PRE, /* tv_id */
4212 ( PROP_cfg | PROP_ssa ), /* properties_required */
4213 0, /* properties_provided */
4214 0, /* properties_destroyed */
4215 TODO_rebuild_alias, /* todo_flags_start */
4216 0, /* todo_flags_finish */
4219 class pass_pre : public gimple_opt_pass
4221 public:
4222 pass_pre (gcc::context *ctxt)
4223 : gimple_opt_pass (pass_data_pre, ctxt)
4226 /* opt_pass methods: */
4227 virtual bool gate (function *)
4228 { return flag_tree_pre != 0 || flag_code_hoisting != 0; }
4229 virtual unsigned int execute (function *);
4231 }; // class pass_pre
4233 /* Valueization hook for RPO VN when we are calling back to it
4234 at ANTIC compute time. */
4236 static tree
4237 pre_valueize (tree name)
4239 if (TREE_CODE (name) == SSA_NAME)
4241 tree tem = VN_INFO (name)->valnum;
4242 if (tem != VN_TOP && tem != name)
4244 if (TREE_CODE (tem) != SSA_NAME
4245 || SSA_NAME_IS_DEFAULT_DEF (tem))
4246 return tem;
4247 /* We create temporary SSA names for representatives that
4248 do not have a definition (yet) but are not default defs either
4249 assume they are fine to use. */
4250 basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (tem));
4251 if (! def_bb
4252 || dominated_by_p (CDI_DOMINATORS, vn_context_bb, def_bb))
4253 return tem;
4254 /* ??? Now we could look for a leader. Ideally we'd somehow
4255 expose RPO VN leaders and get rid of AVAIL_OUT as well... */
4258 return name;
4261 unsigned int
4262 pass_pre::execute (function *fun)
4264 unsigned int todo = 0;
4266 do_partial_partial =
4267 flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4269 /* This has to happen before VN runs because
4270 loop_optimizer_init may create new phis, etc. */
4271 loop_optimizer_init (LOOPS_NORMAL);
4272 split_edges_for_insertion ();
4273 scev_initialize ();
4274 calculate_dominance_info (CDI_DOMINATORS);
4276 run_rpo_vn (VN_WALK);
4278 init_pre ();
4280 vn_valueize = pre_valueize;
4282 /* Insert can get quite slow on an incredibly large number of basic
4283 blocks due to some quadratic behavior. Until this behavior is
4284 fixed, don't run it when he have an incredibly large number of
4285 bb's. If we aren't going to run insert, there is no point in
4286 computing ANTIC, either, even though it's plenty fast nor do
4287 we require AVAIL. */
4288 if (n_basic_blocks_for_fn (fun) < 4000)
4290 compute_avail ();
4291 compute_antic ();
4292 insert ();
4295 /* Make sure to remove fake edges before committing our inserts.
4296 This makes sure we don't end up with extra critical edges that
4297 we would need to split. */
4298 remove_fake_exit_edges ();
4299 gsi_commit_edge_inserts ();
4301 /* Eliminate folds statements which might (should not...) end up
4302 not keeping virtual operands up-to-date. */
4303 gcc_assert (!need_ssa_update_p (fun));
4305 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4306 statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4307 statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
4308 statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4310 todo |= eliminate_with_rpo_vn (inserted_exprs);
4312 vn_valueize = NULL;
4314 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4315 to insert PHI nodes sometimes, and because value numbering of casts isn't
4316 perfect, we sometimes end up inserting dead code. This simple DCE-like
4317 pass removes any insertions we made that weren't actually used. */
4318 simple_dce_from_worklist (inserted_exprs);
4320 fini_pre ();
4322 scev_finalize ();
4323 loop_optimizer_finalize ();
4325 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4326 case we can merge the block with the remaining predecessor of the block.
4327 It should either:
4328 - call merge_blocks after each tail merge iteration
4329 - call merge_blocks after all tail merge iterations
4330 - mark TODO_cleanup_cfg when necessary
4331 - share the cfg cleanup with fini_pre. */
4332 todo |= tail_merge_optimize (todo);
4334 free_rpo_vn ();
4336 /* Tail merging invalidates the virtual SSA web, together with
4337 cfg-cleanup opportunities exposed by PRE this will wreck the
4338 SSA updating machinery. So make sure to run update-ssa
4339 manually, before eventually scheduling cfg-cleanup as part of
4340 the todo. */
4341 update_ssa (TODO_update_ssa_only_virtuals);
4343 return todo;
4346 } // anon namespace
4348 gimple_opt_pass *
4349 make_pass_pre (gcc::context *ctxt)
4351 return new pass_pre (ctxt);