Use gimple_phi in many more places.
[official-gcc.git] / gcc / tree-ssa-pre.c
blob96f2bea9e6741d86928ad54a12712d3e255c586e
1 /* SSA-PRE for trees.
2 Copyright (C) 2001-2014 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 "tm.h"
26 #include "tree.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-inline.h"
30 #include "inchash.h"
31 #include "hash-table.h"
32 #include "tree-ssa-alias.h"
33 #include "internal-fn.h"
34 #include "gimple-fold.h"
35 #include "tree-eh.h"
36 #include "gimple-expr.h"
37 #include "is-a.h"
38 #include "gimple.h"
39 #include "gimplify.h"
40 #include "gimple-iterator.h"
41 #include "gimplify-me.h"
42 #include "gimple-ssa.h"
43 #include "tree-cfg.h"
44 #include "tree-phinodes.h"
45 #include "ssa-iterators.h"
46 #include "stringpool.h"
47 #include "tree-ssanames.h"
48 #include "tree-ssa-loop.h"
49 #include "tree-into-ssa.h"
50 #include "expr.h"
51 #include "tree-dfa.h"
52 #include "tree-ssa.h"
53 #include "tree-iterator.h"
54 #include "alloc-pool.h"
55 #include "obstack.h"
56 #include "tree-pass.h"
57 #include "flags.h"
58 #include "langhooks.h"
59 #include "cfgloop.h"
60 #include "tree-ssa-sccvn.h"
61 #include "tree-scalar-evolution.h"
62 #include "params.h"
63 #include "dbgcnt.h"
64 #include "domwalk.h"
65 #include "ipa-prop.h"
66 #include "tree-ssa-propagate.h"
67 #include "ipa-utils.h"
69 /* TODO:
71 1. Avail sets can be shared by making an avail_find_leader that
72 walks up the dominator tree and looks in those avail sets.
73 This might affect code optimality, it's unclear right now.
74 2. Strength reduction can be performed by anticipating expressions
75 we can repair later on.
76 3. We can do back-substitution or smarter value numbering to catch
77 commutative expressions split up over multiple statements.
80 /* For ease of terminology, "expression node" in the below refers to
81 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
82 represent the actual statement containing the expressions we care about,
83 and we cache the value number by putting it in the expression. */
85 /* Basic algorithm
87 First we walk the statements to generate the AVAIL sets, the
88 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
89 generation of values/expressions by a given block. We use them
90 when computing the ANTIC sets. The AVAIL sets consist of
91 SSA_NAME's that represent values, so we know what values are
92 available in what blocks. AVAIL is a forward dataflow problem. In
93 SSA, values are never killed, so we don't need a kill set, or a
94 fixpoint iteration, in order to calculate the AVAIL sets. In
95 traditional parlance, AVAIL sets tell us the downsafety of the
96 expressions/values.
98 Next, we generate the ANTIC sets. These sets represent the
99 anticipatable expressions. ANTIC is a backwards dataflow
100 problem. An expression is anticipatable in a given block if it could
101 be generated in that block. This means that if we had to perform
102 an insertion in that block, of the value of that expression, we
103 could. Calculating the ANTIC sets requires phi translation of
104 expressions, because the flow goes backwards through phis. We must
105 iterate to a fixpoint of the ANTIC sets, because we have a kill
106 set. Even in SSA form, values are not live over the entire
107 function, only from their definition point onwards. So we have to
108 remove values from the ANTIC set once we go past the definition
109 point of the leaders that make them up.
110 compute_antic/compute_antic_aux performs this computation.
112 Third, we perform insertions to make partially redundant
113 expressions fully redundant.
115 An expression is partially redundant (excluding partial
116 anticipation) if:
118 1. It is AVAIL in some, but not all, of the predecessors of a
119 given block.
120 2. It is ANTIC in all the predecessors.
122 In order to make it fully redundant, we insert the expression into
123 the predecessors where it is not available, but is ANTIC.
125 For the partial anticipation case, we only perform insertion if it
126 is partially anticipated in some block, and fully available in all
127 of the predecessors.
129 insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
130 performs these steps.
132 Fourth, we eliminate fully redundant expressions.
133 This is a simple statement walk that replaces redundant
134 calculations with the now available values. */
136 /* Representations of value numbers:
138 Value numbers are represented by a representative SSA_NAME. We
139 will create fake SSA_NAME's in situations where we need a
140 representative but do not have one (because it is a complex
141 expression). In order to facilitate storing the value numbers in
142 bitmaps, and keep the number of wasted SSA_NAME's down, we also
143 associate a value_id with each value number, and create full blown
144 ssa_name's only where we actually need them (IE in operands of
145 existing expressions).
147 Theoretically you could replace all the value_id's with
148 SSA_NAME_VERSION, but this would allocate a large number of
149 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
150 It would also require an additional indirection at each point we
151 use the value id. */
153 /* Representation of expressions on value numbers:
155 Expressions consisting of value numbers are represented the same
156 way as our VN internally represents them, with an additional
157 "pre_expr" wrapping around them in order to facilitate storing all
158 of the expressions in the same sets. */
160 /* Representation of sets:
162 The dataflow sets do not need to be sorted in any particular order
163 for the majority of their lifetime, are simply represented as two
164 bitmaps, one that keeps track of values present in the set, and one
165 that keeps track of expressions present in the set.
167 When we need them in topological order, we produce it on demand by
168 transforming the bitmap into an array and sorting it into topo
169 order. */
171 /* Type of expression, used to know which member of the PRE_EXPR union
172 is valid. */
174 enum pre_expr_kind
176 NAME,
177 NARY,
178 REFERENCE,
179 CONSTANT
182 typedef union pre_expr_union_d
184 tree name;
185 tree constant;
186 vn_nary_op_t nary;
187 vn_reference_t reference;
188 } pre_expr_union;
190 typedef struct pre_expr_d : typed_noop_remove <pre_expr_d>
192 enum pre_expr_kind kind;
193 unsigned int id;
194 pre_expr_union u;
196 /* hash_table support. */
197 typedef pre_expr_d value_type;
198 typedef pre_expr_d compare_type;
199 static inline hashval_t hash (const pre_expr_d *);
200 static inline int equal (const pre_expr_d *, const pre_expr_d *);
201 } *pre_expr;
203 #define PRE_EXPR_NAME(e) (e)->u.name
204 #define PRE_EXPR_NARY(e) (e)->u.nary
205 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
206 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
208 /* Compare E1 and E1 for equality. */
210 inline int
211 pre_expr_d::equal (const value_type *e1, const compare_type *e2)
213 if (e1->kind != e2->kind)
214 return false;
216 switch (e1->kind)
218 case CONSTANT:
219 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
220 PRE_EXPR_CONSTANT (e2));
221 case NAME:
222 return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
223 case NARY:
224 return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
225 case REFERENCE:
226 return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
227 PRE_EXPR_REFERENCE (e2));
228 default:
229 gcc_unreachable ();
233 /* Hash E. */
235 inline hashval_t
236 pre_expr_d::hash (const value_type *e)
238 switch (e->kind)
240 case CONSTANT:
241 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
242 case NAME:
243 return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
244 case NARY:
245 return PRE_EXPR_NARY (e)->hashcode;
246 case REFERENCE:
247 return PRE_EXPR_REFERENCE (e)->hashcode;
248 default:
249 gcc_unreachable ();
253 /* Next global expression id number. */
254 static unsigned int next_expression_id;
256 /* Mapping from expression to id number we can use in bitmap sets. */
257 static vec<pre_expr> expressions;
258 static hash_table<pre_expr_d> *expression_to_id;
259 static vec<unsigned> name_to_id;
261 /* Allocate an expression id for EXPR. */
263 static inline unsigned int
264 alloc_expression_id (pre_expr expr)
266 struct pre_expr_d **slot;
267 /* Make sure we won't overflow. */
268 gcc_assert (next_expression_id + 1 > next_expression_id);
269 expr->id = next_expression_id++;
270 expressions.safe_push (expr);
271 if (expr->kind == NAME)
273 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
274 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
275 re-allocations by using vec::reserve upfront. */
276 unsigned old_len = name_to_id.length ();
277 name_to_id.reserve (num_ssa_names - old_len);
278 name_to_id.quick_grow_cleared (num_ssa_names);
279 gcc_assert (name_to_id[version] == 0);
280 name_to_id[version] = expr->id;
282 else
284 slot = expression_to_id->find_slot (expr, INSERT);
285 gcc_assert (!*slot);
286 *slot = expr;
288 return next_expression_id - 1;
291 /* Return the expression id for tree EXPR. */
293 static inline unsigned int
294 get_expression_id (const pre_expr expr)
296 return expr->id;
299 static inline unsigned int
300 lookup_expression_id (const pre_expr expr)
302 struct pre_expr_d **slot;
304 if (expr->kind == NAME)
306 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
307 if (name_to_id.length () <= version)
308 return 0;
309 return name_to_id[version];
311 else
313 slot = expression_to_id->find_slot (expr, NO_INSERT);
314 if (!slot)
315 return 0;
316 return ((pre_expr)*slot)->id;
320 /* Return the existing expression id for EXPR, or create one if one
321 does not exist yet. */
323 static inline unsigned int
324 get_or_alloc_expression_id (pre_expr expr)
326 unsigned int id = lookup_expression_id (expr);
327 if (id == 0)
328 return alloc_expression_id (expr);
329 return expr->id = id;
332 /* Return the expression that has expression id ID */
334 static inline pre_expr
335 expression_for_id (unsigned int id)
337 return expressions[id];
340 /* Free the expression id field in all of our expressions,
341 and then destroy the expressions array. */
343 static void
344 clear_expression_ids (void)
346 expressions.release ();
349 static alloc_pool pre_expr_pool;
351 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
353 static pre_expr
354 get_or_alloc_expr_for_name (tree name)
356 struct pre_expr_d expr;
357 pre_expr result;
358 unsigned int result_id;
360 expr.kind = NAME;
361 expr.id = 0;
362 PRE_EXPR_NAME (&expr) = name;
363 result_id = lookup_expression_id (&expr);
364 if (result_id != 0)
365 return expression_for_id (result_id);
367 result = (pre_expr) pool_alloc (pre_expr_pool);
368 result->kind = NAME;
369 PRE_EXPR_NAME (result) = name;
370 alloc_expression_id (result);
371 return result;
374 /* An unordered bitmap set. One bitmap tracks values, the other,
375 expressions. */
376 typedef struct bitmap_set
378 bitmap_head expressions;
379 bitmap_head values;
380 } *bitmap_set_t;
382 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
383 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
385 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
386 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
388 /* Mapping from value id to expressions with that value_id. */
389 static vec<bitmap> value_expressions;
391 /* Sets that we need to keep track of. */
392 typedef struct bb_bitmap_sets
394 /* The EXP_GEN set, which represents expressions/values generated in
395 a basic block. */
396 bitmap_set_t exp_gen;
398 /* The PHI_GEN set, which represents PHI results generated in a
399 basic block. */
400 bitmap_set_t phi_gen;
402 /* The TMP_GEN set, which represents results/temporaries generated
403 in a basic block. IE the LHS of an expression. */
404 bitmap_set_t tmp_gen;
406 /* The AVAIL_OUT set, which represents which values are available in
407 a given basic block. */
408 bitmap_set_t avail_out;
410 /* The ANTIC_IN set, which represents which values are anticipatable
411 in a given basic block. */
412 bitmap_set_t antic_in;
414 /* The PA_IN set, which represents which values are
415 partially anticipatable in a given basic block. */
416 bitmap_set_t pa_in;
418 /* The NEW_SETS set, which is used during insertion to augment the
419 AVAIL_OUT set of blocks with the new insertions performed during
420 the current iteration. */
421 bitmap_set_t new_sets;
423 /* A cache for value_dies_in_block_x. */
424 bitmap expr_dies;
426 /* The live virtual operand on successor edges. */
427 tree vop_on_exit;
429 /* True if we have visited this block during ANTIC calculation. */
430 unsigned int visited : 1;
432 /* True when the block contains a call that might not return. */
433 unsigned int contains_may_not_return_call : 1;
434 } *bb_value_sets_t;
436 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
437 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
438 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
439 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
440 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
441 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
442 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
443 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
444 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
445 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
446 #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
449 /* Basic block list in postorder. */
450 static int *postorder;
451 static int postorder_num;
453 /* This structure is used to keep track of statistics on what
454 optimization PRE was able to perform. */
455 static struct
457 /* The number of RHS computations eliminated by PRE. */
458 int eliminations;
460 /* The number of new expressions/temporaries generated by PRE. */
461 int insertions;
463 /* The number of inserts found due to partial anticipation */
464 int pa_insert;
466 /* The number of new PHI nodes added by PRE. */
467 int phis;
468 } pre_stats;
470 static bool do_partial_partial;
471 static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
472 static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
473 static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
474 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
475 static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
476 static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
477 static void bitmap_insert_into_set_1 (bitmap_set_t, pre_expr,
478 unsigned int, bool);
479 static bitmap_set_t bitmap_set_new (void);
480 static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
481 tree);
482 static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
483 static unsigned int get_expr_value_id (pre_expr);
485 /* We can add and remove elements and entries to and from sets
486 and hash tables, so we use alloc pools for them. */
488 static alloc_pool bitmap_set_pool;
489 static bitmap_obstack grand_bitmap_obstack;
491 /* Set of blocks with statements that have had their EH properties changed. */
492 static bitmap need_eh_cleanup;
494 /* Set of blocks with statements that have had their AB properties changed. */
495 static bitmap need_ab_cleanup;
497 /* A three tuple {e, pred, v} used to cache phi translations in the
498 phi_translate_table. */
500 typedef struct expr_pred_trans_d : typed_free_remove<expr_pred_trans_d>
502 /* The expression. */
503 pre_expr e;
505 /* The predecessor block along which we translated the expression. */
506 basic_block pred;
508 /* The value that resulted from the translation. */
509 pre_expr v;
511 /* The hashcode for the expression, pred pair. This is cached for
512 speed reasons. */
513 hashval_t hashcode;
515 /* hash_table support. */
516 typedef expr_pred_trans_d value_type;
517 typedef expr_pred_trans_d compare_type;
518 static inline hashval_t hash (const value_type *);
519 static inline int equal (const value_type *, const compare_type *);
520 } *expr_pred_trans_t;
521 typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
523 inline hashval_t
524 expr_pred_trans_d::hash (const expr_pred_trans_d *e)
526 return e->hashcode;
529 inline int
530 expr_pred_trans_d::equal (const value_type *ve1,
531 const compare_type *ve2)
533 basic_block b1 = ve1->pred;
534 basic_block b2 = ve2->pred;
536 /* If they are not translations for the same basic block, they can't
537 be equal. */
538 if (b1 != b2)
539 return false;
540 return pre_expr_d::equal (ve1->e, ve2->e);
543 /* The phi_translate_table caches phi translations for a given
544 expression and predecessor. */
545 static hash_table<expr_pred_trans_d> *phi_translate_table;
547 /* Add the tuple mapping from {expression E, basic block PRED} to
548 the phi translation table and return whether it pre-existed. */
550 static inline bool
551 phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
553 expr_pred_trans_t *slot;
554 expr_pred_trans_d tem;
555 hashval_t hash = iterative_hash_hashval_t (pre_expr_d::hash (e),
556 pred->index);
557 tem.e = e;
558 tem.pred = pred;
559 tem.hashcode = hash;
560 slot = phi_translate_table->find_slot_with_hash (&tem, hash, INSERT);
561 if (*slot)
563 *entry = *slot;
564 return true;
567 *entry = *slot = XNEW (struct expr_pred_trans_d);
568 (*entry)->e = e;
569 (*entry)->pred = pred;
570 (*entry)->hashcode = hash;
571 return false;
575 /* Add expression E to the expression set of value id V. */
577 static void
578 add_to_value (unsigned int v, pre_expr e)
580 bitmap set;
582 gcc_checking_assert (get_expr_value_id (e) == v);
584 if (v >= value_expressions.length ())
586 value_expressions.safe_grow_cleared (v + 1);
589 set = value_expressions[v];
590 if (!set)
592 set = BITMAP_ALLOC (&grand_bitmap_obstack);
593 value_expressions[v] = set;
596 bitmap_set_bit (set, get_or_alloc_expression_id (e));
599 /* Create a new bitmap set and return it. */
601 static bitmap_set_t
602 bitmap_set_new (void)
604 bitmap_set_t ret = (bitmap_set_t) pool_alloc (bitmap_set_pool);
605 bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
606 bitmap_initialize (&ret->values, &grand_bitmap_obstack);
607 return ret;
610 /* Return the value id for a PRE expression EXPR. */
612 static unsigned int
613 get_expr_value_id (pre_expr expr)
615 unsigned int id;
616 switch (expr->kind)
618 case CONSTANT:
619 id = get_constant_value_id (PRE_EXPR_CONSTANT (expr));
620 break;
621 case NAME:
622 id = VN_INFO (PRE_EXPR_NAME (expr))->value_id;
623 break;
624 case NARY:
625 id = PRE_EXPR_NARY (expr)->value_id;
626 break;
627 case REFERENCE:
628 id = PRE_EXPR_REFERENCE (expr)->value_id;
629 break;
630 default:
631 gcc_unreachable ();
633 /* ??? We cannot assert that expr has a value-id (it can be 0), because
634 we assign value-ids only to expressions that have a result
635 in set_hashtable_value_ids. */
636 return id;
639 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
641 static tree
642 sccvn_valnum_from_value_id (unsigned int val)
644 bitmap_iterator bi;
645 unsigned int i;
646 bitmap exprset = value_expressions[val];
647 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
649 pre_expr vexpr = expression_for_id (i);
650 if (vexpr->kind == NAME)
651 return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
652 else if (vexpr->kind == CONSTANT)
653 return PRE_EXPR_CONSTANT (vexpr);
655 return NULL_TREE;
658 /* Remove an expression EXPR from a bitmapped set. */
660 static void
661 bitmap_remove_from_set (bitmap_set_t set, pre_expr expr)
663 unsigned int val = get_expr_value_id (expr);
664 if (!value_id_constant_p (val))
666 bitmap_clear_bit (&set->values, val);
667 bitmap_clear_bit (&set->expressions, get_expression_id (expr));
671 static void
672 bitmap_insert_into_set_1 (bitmap_set_t set, pre_expr expr,
673 unsigned int val, bool allow_constants)
675 if (allow_constants || !value_id_constant_p (val))
677 /* We specifically expect this and only this function to be able to
678 insert constants into a set. */
679 bitmap_set_bit (&set->values, val);
680 bitmap_set_bit (&set->expressions, get_or_alloc_expression_id (expr));
684 /* Insert an expression EXPR into a bitmapped set. */
686 static void
687 bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
689 bitmap_insert_into_set_1 (set, expr, get_expr_value_id (expr), false);
692 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
694 static void
695 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
697 bitmap_copy (&dest->expressions, &orig->expressions);
698 bitmap_copy (&dest->values, &orig->values);
702 /* Free memory used up by SET. */
703 static void
704 bitmap_set_free (bitmap_set_t set)
706 bitmap_clear (&set->expressions);
707 bitmap_clear (&set->values);
711 /* Generate an topological-ordered array of bitmap set SET. */
713 static vec<pre_expr>
714 sorted_array_from_bitmap_set (bitmap_set_t set)
716 unsigned int i, j;
717 bitmap_iterator bi, bj;
718 vec<pre_expr> result;
720 /* Pre-allocate enough space for the array. */
721 result.create (bitmap_count_bits (&set->expressions));
723 FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
725 /* The number of expressions having a given value is usually
726 relatively small. Thus, rather than making a vector of all
727 the expressions and sorting it by value-id, we walk the values
728 and check in the reverse mapping that tells us what expressions
729 have a given value, to filter those in our set. As a result,
730 the expressions are inserted in value-id order, which means
731 topological order.
733 If this is somehow a significant lose for some cases, we can
734 choose which set to walk based on the set size. */
735 bitmap exprset = value_expressions[i];
736 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj)
738 if (bitmap_bit_p (&set->expressions, j))
739 result.quick_push (expression_for_id (j));
743 return result;
746 /* Perform bitmapped set operation DEST &= ORIG. */
748 static void
749 bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig)
751 bitmap_iterator bi;
752 unsigned int i;
754 if (dest != orig)
756 bitmap_head temp;
757 bitmap_initialize (&temp, &grand_bitmap_obstack);
759 bitmap_and_into (&dest->values, &orig->values);
760 bitmap_copy (&temp, &dest->expressions);
761 EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
763 pre_expr expr = expression_for_id (i);
764 unsigned int value_id = get_expr_value_id (expr);
765 if (!bitmap_bit_p (&dest->values, value_id))
766 bitmap_clear_bit (&dest->expressions, i);
768 bitmap_clear (&temp);
772 /* Subtract all values and expressions contained in ORIG from DEST. */
774 static bitmap_set_t
775 bitmap_set_subtract (bitmap_set_t dest, bitmap_set_t orig)
777 bitmap_set_t result = bitmap_set_new ();
778 bitmap_iterator bi;
779 unsigned int i;
781 bitmap_and_compl (&result->expressions, &dest->expressions,
782 &orig->expressions);
784 FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
786 pre_expr expr = expression_for_id (i);
787 unsigned int value_id = get_expr_value_id (expr);
788 bitmap_set_bit (&result->values, value_id);
791 return result;
794 /* Subtract all the values in bitmap set B from bitmap set A. */
796 static void
797 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
799 unsigned int i;
800 bitmap_iterator bi;
801 bitmap_head temp;
803 bitmap_initialize (&temp, &grand_bitmap_obstack);
805 bitmap_copy (&temp, &a->expressions);
806 EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
808 pre_expr expr = expression_for_id (i);
809 if (bitmap_set_contains_value (b, get_expr_value_id (expr)))
810 bitmap_remove_from_set (a, expr);
812 bitmap_clear (&temp);
816 /* Return true if bitmapped set SET contains the value VALUE_ID. */
818 static bool
819 bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
821 if (value_id_constant_p (value_id))
822 return true;
824 if (!set || bitmap_empty_p (&set->expressions))
825 return false;
827 return bitmap_bit_p (&set->values, value_id);
830 static inline bool
831 bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr)
833 return bitmap_bit_p (&set->expressions, get_expression_id (expr));
836 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
838 static void
839 bitmap_set_replace_value (bitmap_set_t set, unsigned int lookfor,
840 const pre_expr expr)
842 bitmap exprset;
843 unsigned int i;
844 bitmap_iterator bi;
846 if (value_id_constant_p (lookfor))
847 return;
849 if (!bitmap_set_contains_value (set, lookfor))
850 return;
852 /* The number of expressions having a given value is usually
853 significantly less than the total number of expressions in SET.
854 Thus, rather than check, for each expression in SET, whether it
855 has the value LOOKFOR, we walk the reverse mapping that tells us
856 what expressions have a given value, and see if any of those
857 expressions are in our set. For large testcases, this is about
858 5-10x faster than walking the bitmap. If this is somehow a
859 significant lose for some cases, we can choose which set to walk
860 based on the set size. */
861 exprset = value_expressions[lookfor];
862 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
864 if (bitmap_clear_bit (&set->expressions, i))
866 bitmap_set_bit (&set->expressions, get_expression_id (expr));
867 return;
871 gcc_unreachable ();
874 /* Return true if two bitmap sets are equal. */
876 static bool
877 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
879 return bitmap_equal_p (&a->values, &b->values);
882 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
883 and add it otherwise. */
885 static void
886 bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
888 unsigned int val = get_expr_value_id (expr);
890 if (bitmap_set_contains_value (set, val))
891 bitmap_set_replace_value (set, val, expr);
892 else
893 bitmap_insert_into_set (set, expr);
896 /* Insert EXPR into SET if EXPR's value is not already present in
897 SET. */
899 static void
900 bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
902 unsigned int val = get_expr_value_id (expr);
904 gcc_checking_assert (expr->id == get_or_alloc_expression_id (expr));
906 /* Constant values are always considered to be part of the set. */
907 if (value_id_constant_p (val))
908 return;
910 /* If the value membership changed, add the expression. */
911 if (bitmap_set_bit (&set->values, val))
912 bitmap_set_bit (&set->expressions, expr->id);
915 /* Print out EXPR to outfile. */
917 static void
918 print_pre_expr (FILE *outfile, const pre_expr expr)
920 switch (expr->kind)
922 case CONSTANT:
923 print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr), 0);
924 break;
925 case NAME:
926 print_generic_expr (outfile, PRE_EXPR_NAME (expr), 0);
927 break;
928 case NARY:
930 unsigned int i;
931 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
932 fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
933 for (i = 0; i < nary->length; i++)
935 print_generic_expr (outfile, nary->op[i], 0);
936 if (i != (unsigned) nary->length - 1)
937 fprintf (outfile, ",");
939 fprintf (outfile, "}");
941 break;
943 case REFERENCE:
945 vn_reference_op_t vro;
946 unsigned int i;
947 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
948 fprintf (outfile, "{");
949 for (i = 0;
950 ref->operands.iterate (i, &vro);
951 i++)
953 bool closebrace = false;
954 if (vro->opcode != SSA_NAME
955 && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
957 fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
958 if (vro->op0)
960 fprintf (outfile, "<");
961 closebrace = true;
964 if (vro->op0)
966 print_generic_expr (outfile, vro->op0, 0);
967 if (vro->op1)
969 fprintf (outfile, ",");
970 print_generic_expr (outfile, vro->op1, 0);
972 if (vro->op2)
974 fprintf (outfile, ",");
975 print_generic_expr (outfile, vro->op2, 0);
978 if (closebrace)
979 fprintf (outfile, ">");
980 if (i != ref->operands.length () - 1)
981 fprintf (outfile, ",");
983 fprintf (outfile, "}");
984 if (ref->vuse)
986 fprintf (outfile, "@");
987 print_generic_expr (outfile, ref->vuse, 0);
990 break;
993 void debug_pre_expr (pre_expr);
995 /* Like print_pre_expr but always prints to stderr. */
996 DEBUG_FUNCTION void
997 debug_pre_expr (pre_expr e)
999 print_pre_expr (stderr, e);
1000 fprintf (stderr, "\n");
1003 /* Print out SET to OUTFILE. */
1005 static void
1006 print_bitmap_set (FILE *outfile, bitmap_set_t set,
1007 const char *setname, int blockindex)
1009 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
1010 if (set)
1012 bool first = true;
1013 unsigned i;
1014 bitmap_iterator bi;
1016 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1018 const pre_expr expr = expression_for_id (i);
1020 if (!first)
1021 fprintf (outfile, ", ");
1022 first = false;
1023 print_pre_expr (outfile, expr);
1025 fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1028 fprintf (outfile, " }\n");
1031 void debug_bitmap_set (bitmap_set_t);
1033 DEBUG_FUNCTION void
1034 debug_bitmap_set (bitmap_set_t set)
1036 print_bitmap_set (stderr, set, "debug", 0);
1039 void debug_bitmap_sets_for (basic_block);
1041 DEBUG_FUNCTION void
1042 debug_bitmap_sets_for (basic_block bb)
1044 print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1045 print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1046 print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1047 print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1048 print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1049 if (do_partial_partial)
1050 print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1051 print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1054 /* Print out the expressions that have VAL to OUTFILE. */
1056 static void
1057 print_value_expressions (FILE *outfile, unsigned int val)
1059 bitmap set = value_expressions[val];
1060 if (set)
1062 bitmap_set x;
1063 char s[10];
1064 sprintf (s, "%04d", val);
1065 x.expressions = *set;
1066 print_bitmap_set (outfile, &x, s, 0);
1071 DEBUG_FUNCTION void
1072 debug_value_expressions (unsigned int val)
1074 print_value_expressions (stderr, val);
1077 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1078 represent it. */
1080 static pre_expr
1081 get_or_alloc_expr_for_constant (tree constant)
1083 unsigned int result_id;
1084 unsigned int value_id;
1085 struct pre_expr_d expr;
1086 pre_expr newexpr;
1088 expr.kind = CONSTANT;
1089 PRE_EXPR_CONSTANT (&expr) = constant;
1090 result_id = lookup_expression_id (&expr);
1091 if (result_id != 0)
1092 return expression_for_id (result_id);
1094 newexpr = (pre_expr) pool_alloc (pre_expr_pool);
1095 newexpr->kind = CONSTANT;
1096 PRE_EXPR_CONSTANT (newexpr) = constant;
1097 alloc_expression_id (newexpr);
1098 value_id = get_or_alloc_constant_value_id (constant);
1099 add_to_value (value_id, newexpr);
1100 return newexpr;
1103 /* Given a value id V, find the actual tree representing the constant
1104 value if there is one, and return it. Return NULL if we can't find
1105 a constant. */
1107 static tree
1108 get_constant_for_value_id (unsigned int v)
1110 if (value_id_constant_p (v))
1112 unsigned int i;
1113 bitmap_iterator bi;
1114 bitmap exprset = value_expressions[v];
1116 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1118 pre_expr expr = expression_for_id (i);
1119 if (expr->kind == CONSTANT)
1120 return PRE_EXPR_CONSTANT (expr);
1123 return NULL;
1126 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1127 Currently only supports constants and SSA_NAMES. */
1128 static pre_expr
1129 get_or_alloc_expr_for (tree t)
1131 if (TREE_CODE (t) == SSA_NAME)
1132 return get_or_alloc_expr_for_name (t);
1133 else if (is_gimple_min_invariant (t))
1134 return get_or_alloc_expr_for_constant (t);
1135 else
1137 /* More complex expressions can result from SCCVN expression
1138 simplification that inserts values for them. As they all
1139 do not have VOPs the get handled by the nary ops struct. */
1140 vn_nary_op_t result;
1141 unsigned int result_id;
1142 vn_nary_op_lookup (t, &result);
1143 if (result != NULL)
1145 pre_expr e = (pre_expr) pool_alloc (pre_expr_pool);
1146 e->kind = NARY;
1147 PRE_EXPR_NARY (e) = result;
1148 result_id = lookup_expression_id (e);
1149 if (result_id != 0)
1151 pool_free (pre_expr_pool, e);
1152 e = expression_for_id (result_id);
1153 return e;
1155 alloc_expression_id (e);
1156 return e;
1159 return NULL;
1162 /* Return the folded version of T if T, when folded, is a gimple
1163 min_invariant. Otherwise, return T. */
1165 static pre_expr
1166 fully_constant_expression (pre_expr e)
1168 switch (e->kind)
1170 case CONSTANT:
1171 return e;
1172 case NARY:
1174 vn_nary_op_t nary = PRE_EXPR_NARY (e);
1175 switch (TREE_CODE_CLASS (nary->opcode))
1177 case tcc_binary:
1178 case tcc_comparison:
1180 /* We have to go from trees to pre exprs to value ids to
1181 constants. */
1182 tree naryop0 = nary->op[0];
1183 tree naryop1 = nary->op[1];
1184 tree result;
1185 if (!is_gimple_min_invariant (naryop0))
1187 pre_expr rep0 = get_or_alloc_expr_for (naryop0);
1188 unsigned int vrep0 = get_expr_value_id (rep0);
1189 tree const0 = get_constant_for_value_id (vrep0);
1190 if (const0)
1191 naryop0 = fold_convert (TREE_TYPE (naryop0), const0);
1193 if (!is_gimple_min_invariant (naryop1))
1195 pre_expr rep1 = get_or_alloc_expr_for (naryop1);
1196 unsigned int vrep1 = get_expr_value_id (rep1);
1197 tree const1 = get_constant_for_value_id (vrep1);
1198 if (const1)
1199 naryop1 = fold_convert (TREE_TYPE (naryop1), const1);
1201 result = fold_binary (nary->opcode, nary->type,
1202 naryop0, naryop1);
1203 if (result && is_gimple_min_invariant (result))
1204 return get_or_alloc_expr_for_constant (result);
1205 /* We might have simplified the expression to a
1206 SSA_NAME for example from x_1 * 1. But we cannot
1207 insert a PHI for x_1 unconditionally as x_1 might
1208 not be available readily. */
1209 return e;
1211 case tcc_reference:
1212 if (nary->opcode != REALPART_EXPR
1213 && nary->opcode != IMAGPART_EXPR
1214 && nary->opcode != VIEW_CONVERT_EXPR)
1215 return e;
1216 /* Fallthrough. */
1217 case tcc_unary:
1219 /* We have to go from trees to pre exprs to value ids to
1220 constants. */
1221 tree naryop0 = nary->op[0];
1222 tree const0, result;
1223 if (is_gimple_min_invariant (naryop0))
1224 const0 = naryop0;
1225 else
1227 pre_expr rep0 = get_or_alloc_expr_for (naryop0);
1228 unsigned int vrep0 = get_expr_value_id (rep0);
1229 const0 = get_constant_for_value_id (vrep0);
1231 result = NULL;
1232 if (const0)
1234 tree type1 = TREE_TYPE (nary->op[0]);
1235 const0 = fold_convert (type1, const0);
1236 result = fold_unary (nary->opcode, nary->type, const0);
1238 if (result && is_gimple_min_invariant (result))
1239 return get_or_alloc_expr_for_constant (result);
1240 return e;
1242 default:
1243 return e;
1246 case REFERENCE:
1248 vn_reference_t ref = PRE_EXPR_REFERENCE (e);
1249 tree folded;
1250 if ((folded = fully_constant_vn_reference_p (ref)))
1251 return get_or_alloc_expr_for_constant (folded);
1252 return e;
1254 default:
1255 return e;
1257 return e;
1260 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1261 it has the value it would have in BLOCK. Set *SAME_VALID to true
1262 in case the new vuse doesn't change the value id of the OPERANDS. */
1264 static tree
1265 translate_vuse_through_block (vec<vn_reference_op_s> operands,
1266 alias_set_type set, tree type, tree vuse,
1267 basic_block phiblock,
1268 basic_block block, bool *same_valid)
1270 gimple phi = SSA_NAME_DEF_STMT (vuse);
1271 ao_ref ref;
1272 edge e = NULL;
1273 bool use_oracle;
1275 *same_valid = true;
1277 if (gimple_bb (phi) != phiblock)
1278 return vuse;
1280 use_oracle = ao_ref_init_from_vn_reference (&ref, set, type, operands);
1282 /* Use the alias-oracle to find either the PHI node in this block,
1283 the first VUSE used in this block that is equivalent to vuse or
1284 the first VUSE which definition in this block kills the value. */
1285 if (gimple_code (phi) == GIMPLE_PHI)
1286 e = find_edge (block, phiblock);
1287 else if (use_oracle)
1288 while (!stmt_may_clobber_ref_p_1 (phi, &ref))
1290 vuse = gimple_vuse (phi);
1291 phi = SSA_NAME_DEF_STMT (vuse);
1292 if (gimple_bb (phi) != phiblock)
1293 return vuse;
1294 if (gimple_code (phi) == GIMPLE_PHI)
1296 e = find_edge (block, phiblock);
1297 break;
1300 else
1301 return NULL_TREE;
1303 if (e)
1305 if (use_oracle)
1307 bitmap visited = NULL;
1308 unsigned int cnt;
1309 /* Try to find a vuse that dominates this phi node by skipping
1310 non-clobbering statements. */
1311 vuse = get_continuation_for_phi (phi, &ref, &cnt, &visited, false,
1312 NULL, NULL);
1313 if (visited)
1314 BITMAP_FREE (visited);
1316 else
1317 vuse = NULL_TREE;
1318 if (!vuse)
1320 /* If we didn't find any, the value ID can't stay the same,
1321 but return the translated vuse. */
1322 *same_valid = false;
1323 vuse = PHI_ARG_DEF (phi, e->dest_idx);
1325 /* ??? We would like to return vuse here as this is the canonical
1326 upmost vdef that this reference is associated with. But during
1327 insertion of the references into the hash tables we only ever
1328 directly insert with their direct gimple_vuse, hence returning
1329 something else would make us not find the other expression. */
1330 return PHI_ARG_DEF (phi, e->dest_idx);
1333 return NULL_TREE;
1336 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1337 SET2. This is used to avoid making a set consisting of the union
1338 of PA_IN and ANTIC_IN during insert. */
1340 static inline pre_expr
1341 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2)
1343 pre_expr result;
1345 result = bitmap_find_leader (set1, val);
1346 if (!result && set2)
1347 result = bitmap_find_leader (set2, val);
1348 return result;
1351 /* Get the tree type for our PRE expression e. */
1353 static tree
1354 get_expr_type (const pre_expr e)
1356 switch (e->kind)
1358 case NAME:
1359 return TREE_TYPE (PRE_EXPR_NAME (e));
1360 case CONSTANT:
1361 return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1362 case REFERENCE:
1363 return PRE_EXPR_REFERENCE (e)->type;
1364 case NARY:
1365 return PRE_EXPR_NARY (e)->type;
1367 gcc_unreachable ();
1370 /* Get a representative SSA_NAME for a given expression.
1371 Since all of our sub-expressions are treated as values, we require
1372 them to be SSA_NAME's for simplicity.
1373 Prior versions of GVNPRE used to use "value handles" here, so that
1374 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1375 either case, the operands are really values (IE we do not expect
1376 them to be usable without finding leaders). */
1378 static tree
1379 get_representative_for (const pre_expr e)
1381 tree name;
1382 unsigned int value_id = get_expr_value_id (e);
1384 switch (e->kind)
1386 case NAME:
1387 return PRE_EXPR_NAME (e);
1388 case CONSTANT:
1389 return PRE_EXPR_CONSTANT (e);
1390 case NARY:
1391 case REFERENCE:
1393 /* Go through all of the expressions representing this value
1394 and pick out an SSA_NAME. */
1395 unsigned int i;
1396 bitmap_iterator bi;
1397 bitmap exprs = value_expressions[value_id];
1398 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1400 pre_expr rep = expression_for_id (i);
1401 if (rep->kind == NAME)
1402 return PRE_EXPR_NAME (rep);
1403 else if (rep->kind == CONSTANT)
1404 return PRE_EXPR_CONSTANT (rep);
1407 break;
1410 /* If we reached here we couldn't find an SSA_NAME. This can
1411 happen when we've discovered a value that has never appeared in
1412 the program as set to an SSA_NAME, as the result of phi translation.
1413 Create one here.
1414 ??? We should be able to re-use this when we insert the statement
1415 to compute it. */
1416 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1417 VN_INFO_GET (name)->value_id = value_id;
1418 VN_INFO (name)->valnum = name;
1419 /* ??? For now mark this SSA name for release by SCCVN. */
1420 VN_INFO (name)->needs_insertion = true;
1421 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1422 if (dump_file && (dump_flags & TDF_DETAILS))
1424 fprintf (dump_file, "Created SSA_NAME representative ");
1425 print_generic_expr (dump_file, name, 0);
1426 fprintf (dump_file, " for expression:");
1427 print_pre_expr (dump_file, e);
1428 fprintf (dump_file, " (%04d)\n", value_id);
1431 return name;
1436 static pre_expr
1437 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1438 basic_block pred, basic_block phiblock);
1440 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1441 the phis in PRED. Return NULL if we can't find a leader for each part
1442 of the translated expression. */
1444 static pre_expr
1445 phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1446 basic_block pred, basic_block phiblock)
1448 switch (expr->kind)
1450 case NARY:
1452 unsigned int i;
1453 bool changed = false;
1454 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1455 vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1456 sizeof_vn_nary_op (nary->length));
1457 memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1459 for (i = 0; i < newnary->length; i++)
1461 if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1462 continue;
1463 else
1465 pre_expr leader, result;
1466 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1467 leader = find_leader_in_sets (op_val_id, set1, set2);
1468 result = phi_translate (leader, set1, set2, pred, phiblock);
1469 if (result && result != leader)
1471 tree name = get_representative_for (result);
1472 if (!name)
1473 return NULL;
1474 newnary->op[i] = name;
1476 else if (!result)
1477 return NULL;
1479 changed |= newnary->op[i] != nary->op[i];
1482 if (changed)
1484 pre_expr constant;
1485 unsigned int new_val_id;
1487 tree result = vn_nary_op_lookup_pieces (newnary->length,
1488 newnary->opcode,
1489 newnary->type,
1490 &newnary->op[0],
1491 &nary);
1492 if (result && is_gimple_min_invariant (result))
1493 return get_or_alloc_expr_for_constant (result);
1495 expr = (pre_expr) pool_alloc (pre_expr_pool);
1496 expr->kind = NARY;
1497 expr->id = 0;
1498 if (nary)
1500 PRE_EXPR_NARY (expr) = nary;
1501 constant = fully_constant_expression (expr);
1502 if (constant != expr)
1503 return constant;
1505 new_val_id = nary->value_id;
1506 get_or_alloc_expression_id (expr);
1508 else
1510 new_val_id = get_next_value_id ();
1511 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
1512 nary = vn_nary_op_insert_pieces (newnary->length,
1513 newnary->opcode,
1514 newnary->type,
1515 &newnary->op[0],
1516 result, new_val_id);
1517 PRE_EXPR_NARY (expr) = nary;
1518 constant = fully_constant_expression (expr);
1519 if (constant != expr)
1520 return constant;
1521 get_or_alloc_expression_id (expr);
1523 add_to_value (new_val_id, 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 if (!leader)
1567 break;
1568 opresult = phi_translate (leader, set1, set2, pred, phiblock);
1569 if (!opresult)
1570 break;
1571 if (opresult != leader)
1573 tree name = get_representative_for (opresult);
1574 if (!name)
1575 break;
1576 changed |= name != op[n];
1577 op[n] = name;
1580 if (n != 3)
1582 newoperands.release ();
1583 return NULL;
1585 if (!changed)
1586 continue;
1587 if (!newoperands.exists ())
1588 newoperands = operands.copy ();
1589 /* We may have changed from an SSA_NAME to a constant */
1590 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1591 newop.opcode = TREE_CODE (op[0]);
1592 newop.type = type;
1593 newop.op0 = op[0];
1594 newop.op1 = op[1];
1595 newop.op2 = op[2];
1596 newoperands[i] = newop;
1598 gcc_checking_assert (i == operands.length ());
1600 if (vuse)
1602 newvuse = translate_vuse_through_block (newoperands.exists ()
1603 ? newoperands : operands,
1604 ref->set, ref->type,
1605 vuse, phiblock, pred,
1606 &same_valid);
1607 if (newvuse == NULL_TREE)
1609 newoperands.release ();
1610 return NULL;
1614 if (changed || newvuse != vuse)
1616 unsigned int new_val_id;
1617 pre_expr constant;
1619 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1620 ref->type,
1621 newoperands.exists ()
1622 ? newoperands : operands,
1623 &newref, VN_WALK);
1624 if (result)
1625 newoperands.release ();
1627 /* We can always insert constants, so if we have a partial
1628 redundant constant load of another type try to translate it
1629 to a constant of appropriate type. */
1630 if (result && is_gimple_min_invariant (result))
1632 tree tem = result;
1633 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1635 tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1636 if (tem && !is_gimple_min_invariant (tem))
1637 tem = NULL_TREE;
1639 if (tem)
1640 return get_or_alloc_expr_for_constant (tem);
1643 /* If we'd have to convert things we would need to validate
1644 if we can insert the translated expression. So fail
1645 here for now - we cannot insert an alias with a different
1646 type in the VN tables either, as that would assert. */
1647 if (result
1648 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1649 return NULL;
1650 else if (!result && newref
1651 && !useless_type_conversion_p (ref->type, newref->type))
1653 newoperands.release ();
1654 return NULL;
1657 expr = (pre_expr) pool_alloc (pre_expr_pool);
1658 expr->kind = REFERENCE;
1659 expr->id = 0;
1661 if (newref)
1663 PRE_EXPR_REFERENCE (expr) = newref;
1664 constant = fully_constant_expression (expr);
1665 if (constant != expr)
1666 return constant;
1668 new_val_id = newref->value_id;
1669 get_or_alloc_expression_id (expr);
1671 else
1673 if (changed || !same_valid)
1675 new_val_id = get_next_value_id ();
1676 value_expressions.safe_grow_cleared
1677 (get_max_value_id () + 1);
1679 else
1680 new_val_id = ref->value_id;
1681 if (!newoperands.exists ())
1682 newoperands = operands.copy ();
1683 newref = vn_reference_insert_pieces (newvuse, ref->set,
1684 ref->type,
1685 newoperands,
1686 result, new_val_id);
1687 newoperands = vNULL;
1688 PRE_EXPR_REFERENCE (expr) = newref;
1689 constant = fully_constant_expression (expr);
1690 if (constant != expr)
1691 return constant;
1692 get_or_alloc_expression_id (expr);
1694 add_to_value (new_val_id, expr);
1696 newoperands.release ();
1697 return expr;
1699 break;
1701 case NAME:
1703 tree name = PRE_EXPR_NAME (expr);
1704 gimple def_stmt = SSA_NAME_DEF_STMT (name);
1705 /* If the SSA name is defined by a PHI node in this block,
1706 translate it. */
1707 if (gimple_code (def_stmt) == GIMPLE_PHI
1708 && gimple_bb (def_stmt) == phiblock)
1710 edge e = find_edge (pred, gimple_bb (def_stmt));
1711 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1713 /* Handle constant. */
1714 if (is_gimple_min_invariant (def))
1715 return get_or_alloc_expr_for_constant (def);
1717 return get_or_alloc_expr_for_name (def);
1719 /* Otherwise return it unchanged - it will get removed if its
1720 value is not available in PREDs AVAIL_OUT set of expressions
1721 by the subtraction of TMP_GEN. */
1722 return expr;
1725 default:
1726 gcc_unreachable ();
1730 /* Wrapper around phi_translate_1 providing caching functionality. */
1732 static pre_expr
1733 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1734 basic_block pred, basic_block phiblock)
1736 expr_pred_trans_t slot = NULL;
1737 pre_expr phitrans;
1739 if (!expr)
1740 return NULL;
1742 /* Constants contain no values that need translation. */
1743 if (expr->kind == CONSTANT)
1744 return expr;
1746 if (value_id_constant_p (get_expr_value_id (expr)))
1747 return expr;
1749 /* Don't add translations of NAMEs as those are cheap to translate. */
1750 if (expr->kind != NAME)
1752 if (phi_trans_add (&slot, expr, pred))
1753 return slot->v;
1754 /* Store NULL for the value we want to return in the case of
1755 recursing. */
1756 slot->v = NULL;
1759 /* Translate. */
1760 phitrans = phi_translate_1 (expr, set1, set2, pred, phiblock);
1762 if (slot)
1764 if (phitrans)
1765 slot->v = phitrans;
1766 else
1767 /* Remove failed translations again, they cause insert
1768 iteration to not pick up new opportunities reliably. */
1769 phi_translate_table->remove_elt_with_hash (slot, slot->hashcode);
1772 return phitrans;
1776 /* For each expression in SET, translate the values through phi nodes
1777 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1778 expressions in DEST. */
1780 static void
1781 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
1782 basic_block phiblock)
1784 vec<pre_expr> exprs;
1785 pre_expr expr;
1786 int i;
1788 if (gimple_seq_empty_p (phi_nodes (phiblock)))
1790 bitmap_set_copy (dest, set);
1791 return;
1794 exprs = sorted_array_from_bitmap_set (set);
1795 FOR_EACH_VEC_ELT (exprs, i, expr)
1797 pre_expr translated;
1798 translated = phi_translate (expr, set, NULL, pred, phiblock);
1799 if (!translated)
1800 continue;
1802 /* We might end up with multiple expressions from SET being
1803 translated to the same value. In this case we do not want
1804 to retain the NARY or REFERENCE expression but prefer a NAME
1805 which would be the leader. */
1806 if (translated->kind == NAME)
1807 bitmap_value_replace_in_set (dest, translated);
1808 else
1809 bitmap_value_insert_into_set (dest, translated);
1811 exprs.release ();
1814 /* Find the leader for a value (i.e., the name representing that
1815 value) in a given set, and return it. Return NULL if no leader
1816 is found. */
1818 static pre_expr
1819 bitmap_find_leader (bitmap_set_t set, unsigned int val)
1821 if (value_id_constant_p (val))
1823 unsigned int i;
1824 bitmap_iterator bi;
1825 bitmap exprset = value_expressions[val];
1827 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1829 pre_expr expr = expression_for_id (i);
1830 if (expr->kind == CONSTANT)
1831 return expr;
1834 if (bitmap_set_contains_value (set, val))
1836 /* Rather than walk the entire bitmap of expressions, and see
1837 whether any of them has the value we are looking for, we look
1838 at the reverse mapping, which tells us the set of expressions
1839 that have a given value (IE value->expressions with that
1840 value) and see if any of those expressions are in our set.
1841 The number of expressions per value is usually significantly
1842 less than the number of expressions in the set. In fact, for
1843 large testcases, doing it this way is roughly 5-10x faster
1844 than walking the bitmap.
1845 If this is somehow a significant lose for some cases, we can
1846 choose which set to walk based on which set is smaller. */
1847 unsigned int i;
1848 bitmap_iterator bi;
1849 bitmap exprset = value_expressions[val];
1851 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1852 return expression_for_id (i);
1854 return NULL;
1857 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1858 BLOCK by seeing if it is not killed in the block. Note that we are
1859 only determining whether there is a store that kills it. Because
1860 of the order in which clean iterates over values, we are guaranteed
1861 that altered operands will have caused us to be eliminated from the
1862 ANTIC_IN set already. */
1864 static bool
1865 value_dies_in_block_x (pre_expr expr, basic_block block)
1867 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1868 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1869 gimple def;
1870 gimple_stmt_iterator gsi;
1871 unsigned id = get_expression_id (expr);
1872 bool res = false;
1873 ao_ref ref;
1875 if (!vuse)
1876 return false;
1878 /* Lookup a previously calculated result. */
1879 if (EXPR_DIES (block)
1880 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1881 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1883 /* A memory expression {e, VUSE} dies in the block if there is a
1884 statement that may clobber e. If, starting statement walk from the
1885 top of the basic block, a statement uses VUSE there can be no kill
1886 inbetween that use and the original statement that loaded {e, VUSE},
1887 so we can stop walking. */
1888 ref.base = NULL_TREE;
1889 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1891 tree def_vuse, def_vdef;
1892 def = gsi_stmt (gsi);
1893 def_vuse = gimple_vuse (def);
1894 def_vdef = gimple_vdef (def);
1896 /* Not a memory statement. */
1897 if (!def_vuse)
1898 continue;
1900 /* Not a may-def. */
1901 if (!def_vdef)
1903 /* A load with the same VUSE, we're done. */
1904 if (def_vuse == vuse)
1905 break;
1907 continue;
1910 /* Init ref only if we really need it. */
1911 if (ref.base == NULL_TREE
1912 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type,
1913 refx->operands))
1915 res = true;
1916 break;
1918 /* If the statement may clobber expr, it dies. */
1919 if (stmt_may_clobber_ref_p_1 (def, &ref))
1921 res = true;
1922 break;
1926 /* Remember the result. */
1927 if (!EXPR_DIES (block))
1928 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1929 bitmap_set_bit (EXPR_DIES (block), id * 2);
1930 if (res)
1931 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1933 return res;
1937 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1938 contains its value-id. */
1940 static bool
1941 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1943 if (op && TREE_CODE (op) == SSA_NAME)
1945 unsigned int value_id = VN_INFO (op)->value_id;
1946 if (!(bitmap_set_contains_value (set1, value_id)
1947 || (set2 && bitmap_set_contains_value (set2, value_id))))
1948 return false;
1950 return true;
1953 /* Determine if the expression EXPR is valid in SET1 U SET2.
1954 ONLY SET2 CAN BE NULL.
1955 This means that we have a leader for each part of the expression
1956 (if it consists of values), or the expression is an SSA_NAME.
1957 For loads/calls, we also see if the vuse is killed in this block. */
1959 static bool
1960 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
1962 switch (expr->kind)
1964 case NAME:
1965 /* By construction all NAMEs are available. Non-available
1966 NAMEs are removed by subtracting TMP_GEN from the sets. */
1967 return true;
1968 case NARY:
1970 unsigned int i;
1971 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1972 for (i = 0; i < nary->length; i++)
1973 if (!op_valid_in_sets (set1, set2, nary->op[i]))
1974 return false;
1975 return true;
1977 break;
1978 case REFERENCE:
1980 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1981 vn_reference_op_t vro;
1982 unsigned int i;
1984 FOR_EACH_VEC_ELT (ref->operands, i, vro)
1986 if (!op_valid_in_sets (set1, set2, vro->op0)
1987 || !op_valid_in_sets (set1, set2, vro->op1)
1988 || !op_valid_in_sets (set1, set2, vro->op2))
1989 return false;
1991 return true;
1993 default:
1994 gcc_unreachable ();
1998 /* Clean the set of expressions that are no longer valid in SET1 or
1999 SET2. This means expressions that are made up of values we have no
2000 leaders for in SET1 or SET2. This version is used for partial
2001 anticipation, which means it is not valid in either ANTIC_IN or
2002 PA_IN. */
2004 static void
2005 dependent_clean (bitmap_set_t set1, bitmap_set_t set2)
2007 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
2008 pre_expr expr;
2009 int i;
2011 FOR_EACH_VEC_ELT (exprs, i, expr)
2013 if (!valid_in_sets (set1, set2, expr))
2014 bitmap_remove_from_set (set1, expr);
2016 exprs.release ();
2019 /* Clean the set of expressions that are no longer valid in SET. This
2020 means expressions that are made up of values we have no leaders for
2021 in SET. */
2023 static void
2024 clean (bitmap_set_t set)
2026 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set);
2027 pre_expr expr;
2028 int i;
2030 FOR_EACH_VEC_ELT (exprs, i, expr)
2032 if (!valid_in_sets (set, NULL, expr))
2033 bitmap_remove_from_set (set, expr);
2035 exprs.release ();
2038 /* Clean the set of expressions that are no longer valid in SET because
2039 they are clobbered in BLOCK or because they trap and may not be executed. */
2041 static void
2042 prune_clobbered_mems (bitmap_set_t set, basic_block block)
2044 bitmap_iterator bi;
2045 unsigned i;
2047 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2049 pre_expr expr = expression_for_id (i);
2050 if (expr->kind == REFERENCE)
2052 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2053 if (ref->vuse)
2055 gimple def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2056 if (!gimple_nop_p (def_stmt)
2057 && ((gimple_bb (def_stmt) != block
2058 && !dominated_by_p (CDI_DOMINATORS,
2059 block, gimple_bb (def_stmt)))
2060 || (gimple_bb (def_stmt) == block
2061 && value_dies_in_block_x (expr, block))))
2062 bitmap_remove_from_set (set, expr);
2065 else if (expr->kind == NARY)
2067 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2068 /* If the NARY may trap make sure the block does not contain
2069 a possible exit point.
2070 ??? This is overly conservative if we translate AVAIL_OUT
2071 as the available expression might be after the exit point. */
2072 if (BB_MAY_NOTRETURN (block)
2073 && vn_nary_may_trap (nary))
2074 bitmap_remove_from_set (set, expr);
2079 static sbitmap has_abnormal_preds;
2081 /* List of blocks that may have changed during ANTIC computation and
2082 thus need to be iterated over. */
2084 static sbitmap changed_blocks;
2086 /* Compute the ANTIC set for BLOCK.
2088 If succs(BLOCK) > 1 then
2089 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2090 else if succs(BLOCK) == 1 then
2091 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2093 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2096 static bool
2097 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2099 bool changed = false;
2100 bitmap_set_t S, old, ANTIC_OUT;
2101 bitmap_iterator bi;
2102 unsigned int bii;
2103 edge e;
2104 edge_iterator ei;
2106 old = ANTIC_OUT = S = NULL;
2107 BB_VISITED (block) = 1;
2109 /* If any edges from predecessors are abnormal, antic_in is empty,
2110 so do nothing. */
2111 if (block_has_abnormal_pred_edge)
2112 goto maybe_dump_sets;
2114 old = ANTIC_IN (block);
2115 ANTIC_OUT = bitmap_set_new ();
2117 /* If the block has no successors, ANTIC_OUT is empty. */
2118 if (EDGE_COUNT (block->succs) == 0)
2120 /* If we have one successor, we could have some phi nodes to
2121 translate through. */
2122 else if (single_succ_p (block))
2124 basic_block succ_bb = single_succ (block);
2125 gcc_assert (BB_VISITED (succ_bb));
2126 phi_translate_set (ANTIC_OUT, ANTIC_IN (succ_bb), block, succ_bb);
2128 /* If we have multiple successors, we take the intersection of all of
2129 them. Note that in the case of loop exit phi nodes, we may have
2130 phis to translate through. */
2131 else
2133 size_t i;
2134 basic_block bprime, first = NULL;
2136 auto_vec<basic_block> worklist (EDGE_COUNT (block->succs));
2137 FOR_EACH_EDGE (e, ei, block->succs)
2139 if (!first
2140 && BB_VISITED (e->dest))
2141 first = e->dest;
2142 else if (BB_VISITED (e->dest))
2143 worklist.quick_push (e->dest);
2146 /* Of multiple successors we have to have visited one already
2147 which is guaranteed by iteration order. */
2148 gcc_assert (first != NULL);
2150 phi_translate_set (ANTIC_OUT, ANTIC_IN (first), block, first);
2152 FOR_EACH_VEC_ELT (worklist, i, bprime)
2154 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2156 bitmap_set_t tmp = bitmap_set_new ();
2157 phi_translate_set (tmp, ANTIC_IN (bprime), block, bprime);
2158 bitmap_set_and (ANTIC_OUT, tmp);
2159 bitmap_set_free (tmp);
2161 else
2162 bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime));
2166 /* Prune expressions that are clobbered in block and thus become
2167 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2168 prune_clobbered_mems (ANTIC_OUT, block);
2170 /* Generate ANTIC_OUT - TMP_GEN. */
2171 S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block));
2173 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2174 ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block),
2175 TMP_GEN (block));
2177 /* Then union in the ANTIC_OUT - TMP_GEN values,
2178 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2179 FOR_EACH_EXPR_ID_IN_SET (S, bii, bi)
2180 bitmap_value_insert_into_set (ANTIC_IN (block),
2181 expression_for_id (bii));
2183 clean (ANTIC_IN (block));
2185 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2187 changed = true;
2188 bitmap_set_bit (changed_blocks, block->index);
2189 FOR_EACH_EDGE (e, ei, block->preds)
2190 bitmap_set_bit (changed_blocks, e->src->index);
2192 else
2193 bitmap_clear_bit (changed_blocks, block->index);
2195 maybe_dump_sets:
2196 if (dump_file && (dump_flags & TDF_DETAILS))
2198 if (ANTIC_OUT)
2199 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2201 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2202 block->index);
2204 if (S)
2205 print_bitmap_set (dump_file, S, "S", block->index);
2207 if (old)
2208 bitmap_set_free (old);
2209 if (S)
2210 bitmap_set_free (S);
2211 if (ANTIC_OUT)
2212 bitmap_set_free (ANTIC_OUT);
2213 return changed;
2216 /* Compute PARTIAL_ANTIC for BLOCK.
2218 If succs(BLOCK) > 1 then
2219 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2220 in ANTIC_OUT for all succ(BLOCK)
2221 else if succs(BLOCK) == 1 then
2222 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2224 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2225 - ANTIC_IN[BLOCK])
2228 static bool
2229 compute_partial_antic_aux (basic_block block,
2230 bool block_has_abnormal_pred_edge)
2232 bool changed = false;
2233 bitmap_set_t old_PA_IN;
2234 bitmap_set_t PA_OUT;
2235 edge e;
2236 edge_iterator ei;
2237 unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH);
2239 old_PA_IN = PA_OUT = NULL;
2241 /* If any edges from predecessors are abnormal, antic_in is empty,
2242 so do nothing. */
2243 if (block_has_abnormal_pred_edge)
2244 goto maybe_dump_sets;
2246 /* If there are too many partially anticipatable values in the
2247 block, phi_translate_set can take an exponential time: stop
2248 before the translation starts. */
2249 if (max_pa
2250 && single_succ_p (block)
2251 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2252 goto maybe_dump_sets;
2254 old_PA_IN = PA_IN (block);
2255 PA_OUT = bitmap_set_new ();
2257 /* If the block has no successors, ANTIC_OUT is empty. */
2258 if (EDGE_COUNT (block->succs) == 0)
2260 /* If we have one successor, we could have some phi nodes to
2261 translate through. Note that we can't phi translate across DFS
2262 back edges in partial antic, because it uses a union operation on
2263 the successors. For recurrences like IV's, we will end up
2264 generating a new value in the set on each go around (i + 3 (VH.1)
2265 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2266 else if (single_succ_p (block))
2268 basic_block succ = single_succ (block);
2269 if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK))
2270 phi_translate_set (PA_OUT, PA_IN (succ), block, succ);
2272 /* If we have multiple successors, we take the union of all of
2273 them. */
2274 else
2276 size_t i;
2277 basic_block bprime;
2279 auto_vec<basic_block> worklist (EDGE_COUNT (block->succs));
2280 FOR_EACH_EDGE (e, ei, block->succs)
2282 if (e->flags & EDGE_DFS_BACK)
2283 continue;
2284 worklist.quick_push (e->dest);
2286 if (worklist.length () > 0)
2288 FOR_EACH_VEC_ELT (worklist, i, bprime)
2290 unsigned int i;
2291 bitmap_iterator bi;
2293 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi)
2294 bitmap_value_insert_into_set (PA_OUT,
2295 expression_for_id (i));
2296 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2298 bitmap_set_t pa_in = bitmap_set_new ();
2299 phi_translate_set (pa_in, PA_IN (bprime), block, bprime);
2300 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2301 bitmap_value_insert_into_set (PA_OUT,
2302 expression_for_id (i));
2303 bitmap_set_free (pa_in);
2305 else
2306 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi)
2307 bitmap_value_insert_into_set (PA_OUT,
2308 expression_for_id (i));
2313 /* Prune expressions that are clobbered in block and thus become
2314 invalid if translated from PA_OUT to PA_IN. */
2315 prune_clobbered_mems (PA_OUT, block);
2317 /* PA_IN starts with PA_OUT - TMP_GEN.
2318 Then we subtract things from ANTIC_IN. */
2319 PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block));
2321 /* For partial antic, we want to put back in the phi results, since
2322 we will properly avoid making them partially antic over backedges. */
2323 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2324 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2326 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2327 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2329 dependent_clean (PA_IN (block), ANTIC_IN (block));
2331 if (!bitmap_set_equal (old_PA_IN, PA_IN (block)))
2333 changed = true;
2334 bitmap_set_bit (changed_blocks, block->index);
2335 FOR_EACH_EDGE (e, ei, block->preds)
2336 bitmap_set_bit (changed_blocks, e->src->index);
2338 else
2339 bitmap_clear_bit (changed_blocks, block->index);
2341 maybe_dump_sets:
2342 if (dump_file && (dump_flags & TDF_DETAILS))
2344 if (PA_OUT)
2345 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2347 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2349 if (old_PA_IN)
2350 bitmap_set_free (old_PA_IN);
2351 if (PA_OUT)
2352 bitmap_set_free (PA_OUT);
2353 return changed;
2356 /* Compute ANTIC and partial ANTIC sets. */
2358 static void
2359 compute_antic (void)
2361 bool changed = true;
2362 int num_iterations = 0;
2363 basic_block block;
2364 int i;
2366 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2367 We pre-build the map of blocks with incoming abnormal edges here. */
2368 has_abnormal_preds = sbitmap_alloc (last_basic_block_for_fn (cfun));
2369 bitmap_clear (has_abnormal_preds);
2371 FOR_ALL_BB_FN (block, cfun)
2373 edge_iterator ei;
2374 edge e;
2376 FOR_EACH_EDGE (e, ei, block->preds)
2378 e->flags &= ~EDGE_DFS_BACK;
2379 if (e->flags & EDGE_ABNORMAL)
2381 bitmap_set_bit (has_abnormal_preds, block->index);
2382 break;
2386 BB_VISITED (block) = 0;
2388 /* While we are here, give empty ANTIC_IN sets to each block. */
2389 ANTIC_IN (block) = bitmap_set_new ();
2390 PA_IN (block) = bitmap_set_new ();
2393 /* At the exit block we anticipate nothing. */
2394 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2396 changed_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun) + 1);
2397 bitmap_ones (changed_blocks);
2398 while (changed)
2400 if (dump_file && (dump_flags & TDF_DETAILS))
2401 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2402 /* ??? We need to clear our PHI translation cache here as the
2403 ANTIC sets shrink and we restrict valid translations to
2404 those having operands with leaders in ANTIC. Same below
2405 for PA ANTIC computation. */
2406 num_iterations++;
2407 changed = false;
2408 for (i = postorder_num - 1; i >= 0; i--)
2410 if (bitmap_bit_p (changed_blocks, postorder[i]))
2412 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2413 changed |= compute_antic_aux (block,
2414 bitmap_bit_p (has_abnormal_preds,
2415 block->index));
2418 /* Theoretically possible, but *highly* unlikely. */
2419 gcc_checking_assert (num_iterations < 500);
2422 statistics_histogram_event (cfun, "compute_antic iterations",
2423 num_iterations);
2425 if (do_partial_partial)
2427 bitmap_ones (changed_blocks);
2428 mark_dfs_back_edges ();
2429 num_iterations = 0;
2430 changed = true;
2431 while (changed)
2433 if (dump_file && (dump_flags & TDF_DETAILS))
2434 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2435 num_iterations++;
2436 changed = false;
2437 for (i = postorder_num - 1 ; i >= 0; i--)
2439 if (bitmap_bit_p (changed_blocks, postorder[i]))
2441 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2442 changed
2443 |= compute_partial_antic_aux (block,
2444 bitmap_bit_p (has_abnormal_preds,
2445 block->index));
2448 /* Theoretically possible, but *highly* unlikely. */
2449 gcc_checking_assert (num_iterations < 500);
2451 statistics_histogram_event (cfun, "compute_partial_antic iterations",
2452 num_iterations);
2454 sbitmap_free (has_abnormal_preds);
2455 sbitmap_free (changed_blocks);
2459 /* Inserted expressions are placed onto this worklist, which is used
2460 for performing quick dead code elimination of insertions we made
2461 that didn't turn out to be necessary. */
2462 static bitmap inserted_exprs;
2464 /* The actual worker for create_component_ref_by_pieces. */
2466 static tree
2467 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2468 unsigned int *operand, gimple_seq *stmts)
2470 vn_reference_op_t currop = &ref->operands[*operand];
2471 tree genop;
2472 ++*operand;
2473 switch (currop->opcode)
2475 case CALL_EXPR:
2477 tree folded, sc = NULL_TREE;
2478 unsigned int nargs = 0;
2479 tree fn, *args;
2480 if (TREE_CODE (currop->op0) == FUNCTION_DECL)
2481 fn = currop->op0;
2482 else
2483 fn = find_or_generate_expression (block, currop->op0, stmts);
2484 if (!fn)
2485 return NULL_TREE;
2486 if (currop->op1)
2488 sc = find_or_generate_expression (block, currop->op1, stmts);
2489 if (!sc)
2490 return NULL_TREE;
2492 args = XNEWVEC (tree, ref->operands.length () - 1);
2493 while (*operand < ref->operands.length ())
2495 args[nargs] = create_component_ref_by_pieces_1 (block, ref,
2496 operand, stmts);
2497 if (!args[nargs])
2498 return NULL_TREE;
2499 nargs++;
2501 folded = build_call_array (currop->type,
2502 (TREE_CODE (fn) == FUNCTION_DECL
2503 ? build_fold_addr_expr (fn) : fn),
2504 nargs, args);
2505 free (args);
2506 if (sc)
2507 CALL_EXPR_STATIC_CHAIN (folded) = sc;
2508 return folded;
2511 case MEM_REF:
2513 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2514 stmts);
2515 if (!baseop)
2516 return NULL_TREE;
2517 tree offset = currop->op0;
2518 if (TREE_CODE (baseop) == ADDR_EXPR
2519 && handled_component_p (TREE_OPERAND (baseop, 0)))
2521 HOST_WIDE_INT off;
2522 tree base;
2523 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2524 &off);
2525 gcc_assert (base);
2526 offset = int_const_binop (PLUS_EXPR, offset,
2527 build_int_cst (TREE_TYPE (offset),
2528 off));
2529 baseop = build_fold_addr_expr (base);
2531 return fold_build2 (MEM_REF, currop->type, baseop, offset);
2534 case TARGET_MEM_REF:
2536 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2537 vn_reference_op_t nextop = &ref->operands[++*operand];
2538 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2539 stmts);
2540 if (!baseop)
2541 return NULL_TREE;
2542 if (currop->op0)
2544 genop0 = find_or_generate_expression (block, currop->op0, stmts);
2545 if (!genop0)
2546 return NULL_TREE;
2548 if (nextop->op0)
2550 genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2551 if (!genop1)
2552 return NULL_TREE;
2554 return build5 (TARGET_MEM_REF, currop->type,
2555 baseop, currop->op2, genop0, currop->op1, genop1);
2558 case ADDR_EXPR:
2559 if (currop->op0)
2561 gcc_assert (is_gimple_min_invariant (currop->op0));
2562 return currop->op0;
2564 /* Fallthrough. */
2565 case REALPART_EXPR:
2566 case IMAGPART_EXPR:
2567 case VIEW_CONVERT_EXPR:
2569 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2570 stmts);
2571 if (!genop0)
2572 return NULL_TREE;
2573 return fold_build1 (currop->opcode, currop->type, genop0);
2576 case WITH_SIZE_EXPR:
2578 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2579 stmts);
2580 if (!genop0)
2581 return NULL_TREE;
2582 tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2583 if (!genop1)
2584 return NULL_TREE;
2585 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2588 case BIT_FIELD_REF:
2590 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2591 stmts);
2592 if (!genop0)
2593 return NULL_TREE;
2594 tree op1 = currop->op0;
2595 tree op2 = currop->op1;
2596 return fold_build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2599 /* For array ref vn_reference_op's, operand 1 of the array ref
2600 is op0 of the reference op and operand 3 of the array ref is
2601 op1. */
2602 case ARRAY_RANGE_REF:
2603 case ARRAY_REF:
2605 tree genop0;
2606 tree genop1 = currop->op0;
2607 tree genop2 = currop->op1;
2608 tree genop3 = currop->op2;
2609 genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2610 stmts);
2611 if (!genop0)
2612 return NULL_TREE;
2613 genop1 = find_or_generate_expression (block, genop1, stmts);
2614 if (!genop1)
2615 return NULL_TREE;
2616 if (genop2)
2618 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2619 /* Drop zero minimum index if redundant. */
2620 if (integer_zerop (genop2)
2621 && (!domain_type
2622 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2623 genop2 = NULL_TREE;
2624 else
2626 genop2 = find_or_generate_expression (block, genop2, stmts);
2627 if (!genop2)
2628 return NULL_TREE;
2631 if (genop3)
2633 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2634 /* We can't always put a size in units of the element alignment
2635 here as the element alignment may be not visible. See
2636 PR43783. Simply drop the element size for constant
2637 sizes. */
2638 if (tree_int_cst_equal (genop3, TYPE_SIZE_UNIT (elmt_type)))
2639 genop3 = NULL_TREE;
2640 else
2642 genop3 = size_binop (EXACT_DIV_EXPR, genop3,
2643 size_int (TYPE_ALIGN_UNIT (elmt_type)));
2644 genop3 = find_or_generate_expression (block, genop3, stmts);
2645 if (!genop3)
2646 return NULL_TREE;
2649 return build4 (currop->opcode, currop->type, genop0, genop1,
2650 genop2, genop3);
2652 case COMPONENT_REF:
2654 tree op0;
2655 tree op1;
2656 tree genop2 = currop->op1;
2657 op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2658 if (!op0)
2659 return NULL_TREE;
2660 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2661 op1 = currop->op0;
2662 if (genop2)
2664 genop2 = find_or_generate_expression (block, genop2, stmts);
2665 if (!genop2)
2666 return NULL_TREE;
2668 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2671 case SSA_NAME:
2673 genop = find_or_generate_expression (block, currop->op0, stmts);
2674 return genop;
2676 case STRING_CST:
2677 case INTEGER_CST:
2678 case COMPLEX_CST:
2679 case VECTOR_CST:
2680 case REAL_CST:
2681 case CONSTRUCTOR:
2682 case VAR_DECL:
2683 case PARM_DECL:
2684 case CONST_DECL:
2685 case RESULT_DECL:
2686 case FUNCTION_DECL:
2687 return currop->op0;
2689 default:
2690 gcc_unreachable ();
2694 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2695 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2696 trying to rename aggregates into ssa form directly, which is a no no.
2698 Thus, this routine doesn't create temporaries, it just builds a
2699 single access expression for the array, calling
2700 find_or_generate_expression to build the innermost pieces.
2702 This function is a subroutine of create_expression_by_pieces, and
2703 should not be called on it's own unless you really know what you
2704 are doing. */
2706 static tree
2707 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2708 gimple_seq *stmts)
2710 unsigned int op = 0;
2711 return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2714 /* Find a simple leader for an expression, or generate one using
2715 create_expression_by_pieces from a NARY expression for the value.
2716 BLOCK is the basic_block we are looking for leaders in.
2717 OP is the tree expression to find a leader for or generate.
2718 Returns the leader or NULL_TREE on failure. */
2720 static tree
2721 find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2723 pre_expr expr = get_or_alloc_expr_for (op);
2724 unsigned int lookfor = get_expr_value_id (expr);
2725 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2726 if (leader)
2728 if (leader->kind == NAME)
2729 return PRE_EXPR_NAME (leader);
2730 else if (leader->kind == CONSTANT)
2731 return PRE_EXPR_CONSTANT (leader);
2733 /* Defer. */
2734 return NULL_TREE;
2737 /* It must be a complex expression, so generate it recursively. Note
2738 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2739 where the insert algorithm fails to insert a required expression. */
2740 bitmap exprset = value_expressions[lookfor];
2741 bitmap_iterator bi;
2742 unsigned int i;
2743 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2745 pre_expr temp = expression_for_id (i);
2746 /* We cannot insert random REFERENCE expressions at arbitrary
2747 places. We can insert NARYs which eventually re-materializes
2748 its operand values. */
2749 if (temp->kind == NARY)
2750 return create_expression_by_pieces (block, temp, stmts,
2751 get_expr_type (expr));
2754 /* Defer. */
2755 return NULL_TREE;
2758 #define NECESSARY GF_PLF_1
2760 /* Create an expression in pieces, so that we can handle very complex
2761 expressions that may be ANTIC, but not necessary GIMPLE.
2762 BLOCK is the basic block the expression will be inserted into,
2763 EXPR is the expression to insert (in value form)
2764 STMTS is a statement list to append the necessary insertions into.
2766 This function will die if we hit some value that shouldn't be
2767 ANTIC but is (IE there is no leader for it, or its components).
2768 The function returns NULL_TREE in case a different antic expression
2769 has to be inserted first.
2770 This function may also generate expressions that are themselves
2771 partially or fully redundant. Those that are will be either made
2772 fully redundant during the next iteration of insert (for partially
2773 redundant ones), or eliminated by eliminate (for fully redundant
2774 ones). */
2776 static tree
2777 create_expression_by_pieces (basic_block block, pre_expr expr,
2778 gimple_seq *stmts, tree type)
2780 tree name;
2781 tree folded;
2782 gimple_seq forced_stmts = NULL;
2783 unsigned int value_id;
2784 gimple_stmt_iterator gsi;
2785 tree exprtype = type ? type : get_expr_type (expr);
2786 pre_expr nameexpr;
2787 gimple_assign newstmt;
2789 switch (expr->kind)
2791 /* We may hit the NAME/CONSTANT case if we have to convert types
2792 that value numbering saw through. */
2793 case NAME:
2794 folded = PRE_EXPR_NAME (expr);
2795 break;
2796 case CONSTANT:
2797 folded = PRE_EXPR_CONSTANT (expr);
2798 break;
2799 case REFERENCE:
2801 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2802 folded = create_component_ref_by_pieces (block, ref, stmts);
2803 if (!folded)
2804 return NULL_TREE;
2806 break;
2807 case NARY:
2809 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2810 tree *genop = XALLOCAVEC (tree, nary->length);
2811 unsigned i;
2812 for (i = 0; i < nary->length; ++i)
2814 genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2815 if (!genop[i])
2816 return NULL_TREE;
2817 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2818 may have conversions stripped. */
2819 if (nary->opcode == POINTER_PLUS_EXPR)
2821 if (i == 0)
2822 genop[i] = gimple_convert (&forced_stmts,
2823 nary->type, genop[i]);
2824 else if (i == 1)
2825 genop[i] = gimple_convert (&forced_stmts,
2826 sizetype, genop[i]);
2828 else
2829 genop[i] = gimple_convert (&forced_stmts,
2830 TREE_TYPE (nary->op[i]), genop[i]);
2832 if (nary->opcode == CONSTRUCTOR)
2834 vec<constructor_elt, va_gc> *elts = NULL;
2835 for (i = 0; i < nary->length; ++i)
2836 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2837 folded = build_constructor (nary->type, elts);
2839 else
2841 switch (nary->length)
2843 case 1:
2844 folded = fold_build1 (nary->opcode, nary->type,
2845 genop[0]);
2846 break;
2847 case 2:
2848 folded = fold_build2 (nary->opcode, nary->type,
2849 genop[0], genop[1]);
2850 break;
2851 case 3:
2852 folded = fold_build3 (nary->opcode, nary->type,
2853 genop[0], genop[1], genop[2]);
2854 break;
2855 default:
2856 gcc_unreachable ();
2860 break;
2861 default:
2862 gcc_unreachable ();
2865 if (!useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2866 folded = fold_convert (exprtype, folded);
2868 /* Force the generated expression to be a sequence of GIMPLE
2869 statements.
2870 We have to call unshare_expr because force_gimple_operand may
2871 modify the tree we pass to it. */
2872 gimple_seq tem = NULL;
2873 folded = force_gimple_operand (unshare_expr (folded), &tem,
2874 false, NULL);
2875 gimple_seq_add_seq_without_update (&forced_stmts, tem);
2877 /* If we have any intermediate expressions to the value sets, add them
2878 to the value sets and chain them in the instruction stream. */
2879 if (forced_stmts)
2881 gsi = gsi_start (forced_stmts);
2882 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2884 gimple stmt = gsi_stmt (gsi);
2885 tree forcedname = gimple_get_lhs (stmt);
2886 pre_expr nameexpr;
2888 if (TREE_CODE (forcedname) == SSA_NAME)
2890 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
2891 VN_INFO_GET (forcedname)->valnum = forcedname;
2892 VN_INFO (forcedname)->value_id = get_next_value_id ();
2893 nameexpr = get_or_alloc_expr_for_name (forcedname);
2894 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
2895 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2896 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2899 gimple_set_vuse (stmt, BB_LIVE_VOP_ON_EXIT (block));
2901 gimple_seq_add_seq (stmts, forced_stmts);
2904 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2905 newstmt = gimple_build_assign (name, folded);
2906 gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
2907 gimple_set_plf (newstmt, NECESSARY, false);
2909 gimple_seq_add_stmt (stmts, newstmt);
2910 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (name));
2912 /* Fold the last statement. */
2913 gsi = gsi_last (*stmts);
2914 if (fold_stmt_inplace (&gsi))
2915 update_stmt (gsi_stmt (gsi));
2917 /* Add a value number to the temporary.
2918 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2919 we are creating the expression by pieces, and this particular piece of
2920 the expression may have been represented. There is no harm in replacing
2921 here. */
2922 value_id = get_expr_value_id (expr);
2923 VN_INFO_GET (name)->value_id = value_id;
2924 VN_INFO (name)->valnum = sccvn_valnum_from_value_id (value_id);
2925 if (VN_INFO (name)->valnum == NULL_TREE)
2926 VN_INFO (name)->valnum = name;
2927 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
2928 nameexpr = get_or_alloc_expr_for_name (name);
2929 add_to_value (value_id, nameexpr);
2930 if (NEW_SETS (block))
2931 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2932 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2934 pre_stats.insertions++;
2935 if (dump_file && (dump_flags & TDF_DETAILS))
2937 fprintf (dump_file, "Inserted ");
2938 print_gimple_stmt (dump_file, newstmt, 0, 0);
2939 fprintf (dump_file, " in predecessor %d (%04d)\n",
2940 block->index, value_id);
2943 return name;
2947 /* Insert the to-be-made-available values of expression EXPRNUM for each
2948 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2949 merge the result with a phi node, given the same value number as
2950 NODE. Return true if we have inserted new stuff. */
2952 static bool
2953 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
2954 vec<pre_expr> avail)
2956 pre_expr expr = expression_for_id (exprnum);
2957 pre_expr newphi;
2958 unsigned int val = get_expr_value_id (expr);
2959 edge pred;
2960 bool insertions = false;
2961 bool nophi = false;
2962 basic_block bprime;
2963 pre_expr eprime;
2964 edge_iterator ei;
2965 tree type = get_expr_type (expr);
2966 tree temp;
2967 gimple_phi phi;
2969 /* Make sure we aren't creating an induction variable. */
2970 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
2972 bool firstinsideloop = false;
2973 bool secondinsideloop = false;
2974 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
2975 EDGE_PRED (block, 0)->src);
2976 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
2977 EDGE_PRED (block, 1)->src);
2978 /* Induction variables only have one edge inside the loop. */
2979 if ((firstinsideloop ^ secondinsideloop)
2980 && expr->kind != REFERENCE)
2982 if (dump_file && (dump_flags & TDF_DETAILS))
2983 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
2984 nophi = true;
2988 /* Make the necessary insertions. */
2989 FOR_EACH_EDGE (pred, ei, block->preds)
2991 gimple_seq stmts = NULL;
2992 tree builtexpr;
2993 bprime = pred->src;
2994 eprime = avail[pred->dest_idx];
2996 if (eprime->kind != NAME && eprime->kind != CONSTANT)
2998 builtexpr = create_expression_by_pieces (bprime, eprime,
2999 &stmts, type);
3000 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3001 gsi_insert_seq_on_edge (pred, stmts);
3002 if (!builtexpr)
3004 /* We cannot insert a PHI node if we failed to insert
3005 on one edge. */
3006 nophi = true;
3007 continue;
3009 avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3010 insertions = true;
3012 else if (eprime->kind == CONSTANT)
3014 /* Constants may not have the right type, fold_convert
3015 should give us back a constant with the right type. */
3016 tree constant = PRE_EXPR_CONSTANT (eprime);
3017 if (!useless_type_conversion_p (type, TREE_TYPE (constant)))
3019 tree builtexpr = fold_convert (type, constant);
3020 if (!is_gimple_min_invariant (builtexpr))
3022 tree forcedexpr = force_gimple_operand (builtexpr,
3023 &stmts, true,
3024 NULL);
3025 if (!is_gimple_min_invariant (forcedexpr))
3027 if (forcedexpr != builtexpr)
3029 VN_INFO_GET (forcedexpr)->valnum = PRE_EXPR_CONSTANT (eprime);
3030 VN_INFO (forcedexpr)->value_id = get_expr_value_id (eprime);
3032 if (stmts)
3034 gimple_stmt_iterator gsi;
3035 gsi = gsi_start (stmts);
3036 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3038 gimple stmt = gsi_stmt (gsi);
3039 tree lhs = gimple_get_lhs (stmt);
3040 if (TREE_CODE (lhs) == SSA_NAME)
3041 bitmap_set_bit (inserted_exprs,
3042 SSA_NAME_VERSION (lhs));
3043 gimple_set_plf (stmt, NECESSARY, false);
3045 gsi_insert_seq_on_edge (pred, stmts);
3047 avail[pred->dest_idx]
3048 = get_or_alloc_expr_for_name (forcedexpr);
3051 else
3052 avail[pred->dest_idx]
3053 = get_or_alloc_expr_for_constant (builtexpr);
3056 else if (eprime->kind == NAME)
3058 /* We may have to do a conversion because our value
3059 numbering can look through types in certain cases, but
3060 our IL requires all operands of a phi node have the same
3061 type. */
3062 tree name = PRE_EXPR_NAME (eprime);
3063 if (!useless_type_conversion_p (type, TREE_TYPE (name)))
3065 tree builtexpr;
3066 tree forcedexpr;
3067 builtexpr = fold_convert (type, name);
3068 forcedexpr = force_gimple_operand (builtexpr,
3069 &stmts, true,
3070 NULL);
3072 if (forcedexpr != name)
3074 VN_INFO_GET (forcedexpr)->valnum = VN_INFO (name)->valnum;
3075 VN_INFO (forcedexpr)->value_id = VN_INFO (name)->value_id;
3078 if (stmts)
3080 gimple_stmt_iterator gsi;
3081 gsi = gsi_start (stmts);
3082 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3084 gimple stmt = gsi_stmt (gsi);
3085 tree lhs = gimple_get_lhs (stmt);
3086 if (TREE_CODE (lhs) == SSA_NAME)
3087 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
3088 gimple_set_plf (stmt, NECESSARY, false);
3090 gsi_insert_seq_on_edge (pred, stmts);
3092 avail[pred->dest_idx] = get_or_alloc_expr_for_name (forcedexpr);
3096 /* If we didn't want a phi node, and we made insertions, we still have
3097 inserted new stuff, and thus return true. If we didn't want a phi node,
3098 and didn't make insertions, we haven't added anything new, so return
3099 false. */
3100 if (nophi && insertions)
3101 return true;
3102 else if (nophi && !insertions)
3103 return false;
3105 /* Now build a phi for the new variable. */
3106 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3107 phi = create_phi_node (temp, block);
3109 gimple_set_plf (phi, NECESSARY, false);
3110 VN_INFO_GET (temp)->value_id = val;
3111 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3112 if (VN_INFO (temp)->valnum == NULL_TREE)
3113 VN_INFO (temp)->valnum = temp;
3114 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3115 FOR_EACH_EDGE (pred, ei, block->preds)
3117 pre_expr ae = avail[pred->dest_idx];
3118 gcc_assert (get_expr_type (ae) == type
3119 || useless_type_conversion_p (type, get_expr_type (ae)));
3120 if (ae->kind == CONSTANT)
3121 add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3122 pred, UNKNOWN_LOCATION);
3123 else
3124 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3127 newphi = get_or_alloc_expr_for_name (temp);
3128 add_to_value (val, newphi);
3130 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3131 this insertion, since we test for the existence of this value in PHI_GEN
3132 before proceeding with the partial redundancy checks in insert_aux.
3134 The value may exist in AVAIL_OUT, in particular, it could be represented
3135 by the expression we are trying to eliminate, in which case we want the
3136 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3137 inserted there.
3139 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3140 this block, because if it did, it would have existed in our dominator's
3141 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3144 bitmap_insert_into_set (PHI_GEN (block), newphi);
3145 bitmap_value_replace_in_set (AVAIL_OUT (block),
3146 newphi);
3147 bitmap_insert_into_set (NEW_SETS (block),
3148 newphi);
3150 if (dump_file && (dump_flags & TDF_DETAILS))
3152 fprintf (dump_file, "Created phi ");
3153 print_gimple_stmt (dump_file, phi, 0, 0);
3154 fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3156 pre_stats.phis++;
3157 return true;
3162 /* Perform insertion of partially redundant values.
3163 For BLOCK, do the following:
3164 1. Propagate the NEW_SETS of the dominator into the current block.
3165 If the block has multiple predecessors,
3166 2a. Iterate over the ANTIC expressions for the block to see if
3167 any of them are partially redundant.
3168 2b. If so, insert them into the necessary predecessors to make
3169 the expression fully redundant.
3170 2c. Insert a new PHI merging the values of the predecessors.
3171 2d. Insert the new PHI, and the new expressions, into the
3172 NEW_SETS set.
3173 3. Recursively call ourselves on the dominator children of BLOCK.
3175 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
3176 do_regular_insertion and do_partial_insertion.
3180 static bool
3181 do_regular_insertion (basic_block block, basic_block dom)
3183 bool new_stuff = false;
3184 vec<pre_expr> exprs;
3185 pre_expr expr;
3186 vec<pre_expr> avail = vNULL;
3187 int i;
3189 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3190 avail.safe_grow (EDGE_COUNT (block->preds));
3192 FOR_EACH_VEC_ELT (exprs, i, expr)
3194 if (expr->kind == NARY
3195 || expr->kind == REFERENCE)
3197 unsigned int val;
3198 bool by_some = false;
3199 bool cant_insert = false;
3200 bool all_same = true;
3201 pre_expr first_s = NULL;
3202 edge pred;
3203 basic_block bprime;
3204 pre_expr eprime = NULL;
3205 edge_iterator ei;
3206 pre_expr edoubleprime = NULL;
3207 bool do_insertion = false;
3209 val = get_expr_value_id (expr);
3210 if (bitmap_set_contains_value (PHI_GEN (block), val))
3211 continue;
3212 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3214 if (dump_file && (dump_flags & TDF_DETAILS))
3216 fprintf (dump_file, "Found fully redundant value: ");
3217 print_pre_expr (dump_file, expr);
3218 fprintf (dump_file, "\n");
3220 continue;
3223 FOR_EACH_EDGE (pred, ei, block->preds)
3225 unsigned int vprime;
3227 /* We should never run insertion for the exit block
3228 and so not come across fake pred edges. */
3229 gcc_assert (!(pred->flags & EDGE_FAKE));
3230 bprime = pred->src;
3231 eprime = phi_translate (expr, ANTIC_IN (block), NULL,
3232 bprime, block);
3234 /* eprime will generally only be NULL if the
3235 value of the expression, translated
3236 through the PHI for this predecessor, is
3237 undefined. If that is the case, we can't
3238 make the expression fully redundant,
3239 because its value is undefined along a
3240 predecessor path. We can thus break out
3241 early because it doesn't matter what the
3242 rest of the results are. */
3243 if (eprime == NULL)
3245 avail[pred->dest_idx] = NULL;
3246 cant_insert = true;
3247 break;
3250 eprime = fully_constant_expression (eprime);
3251 vprime = get_expr_value_id (eprime);
3252 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3253 vprime);
3254 if (edoubleprime == NULL)
3256 avail[pred->dest_idx] = eprime;
3257 all_same = false;
3259 else
3261 avail[pred->dest_idx] = edoubleprime;
3262 by_some = true;
3263 /* We want to perform insertions to remove a redundancy on
3264 a path in the CFG we want to optimize for speed. */
3265 if (optimize_edge_for_speed_p (pred))
3266 do_insertion = true;
3267 if (first_s == NULL)
3268 first_s = edoubleprime;
3269 else if (!pre_expr_d::equal (first_s, edoubleprime))
3270 all_same = false;
3273 /* If we can insert it, it's not the same value
3274 already existing along every predecessor, and
3275 it's defined by some predecessor, it is
3276 partially redundant. */
3277 if (!cant_insert && !all_same && by_some)
3279 if (!do_insertion)
3281 if (dump_file && (dump_flags & TDF_DETAILS))
3283 fprintf (dump_file, "Skipping partial redundancy for "
3284 "expression ");
3285 print_pre_expr (dump_file, expr);
3286 fprintf (dump_file, " (%04d), no redundancy on to be "
3287 "optimized for speed edge\n", val);
3290 else if (dbg_cnt (treepre_insert))
3292 if (dump_file && (dump_flags & TDF_DETAILS))
3294 fprintf (dump_file, "Found partial redundancy for "
3295 "expression ");
3296 print_pre_expr (dump_file, expr);
3297 fprintf (dump_file, " (%04d)\n",
3298 get_expr_value_id (expr));
3300 if (insert_into_preds_of_block (block,
3301 get_expression_id (expr),
3302 avail))
3303 new_stuff = true;
3306 /* If all edges produce the same value and that value is
3307 an invariant, then the PHI has the same value on all
3308 edges. Note this. */
3309 else if (!cant_insert && all_same)
3311 gcc_assert (edoubleprime->kind == CONSTANT
3312 || edoubleprime->kind == NAME);
3314 tree temp = make_temp_ssa_name (get_expr_type (expr),
3315 NULL, "pretmp");
3316 gimple_assign assign =
3317 gimple_build_assign (temp,
3318 edoubleprime->kind == CONSTANT ?
3319 PRE_EXPR_CONSTANT (edoubleprime) :
3320 PRE_EXPR_NAME (edoubleprime));
3321 gimple_stmt_iterator gsi = gsi_after_labels (block);
3322 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3324 gimple_set_plf (assign, NECESSARY, false);
3325 VN_INFO_GET (temp)->value_id = val;
3326 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3327 if (VN_INFO (temp)->valnum == NULL_TREE)
3328 VN_INFO (temp)->valnum = temp;
3329 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3330 pre_expr newe = get_or_alloc_expr_for_name (temp);
3331 add_to_value (val, newe);
3332 bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3333 bitmap_insert_into_set (NEW_SETS (block), newe);
3338 exprs.release ();
3339 return new_stuff;
3343 /* Perform insertion for partially anticipatable expressions. There
3344 is only one case we will perform insertion for these. This case is
3345 if the expression is partially anticipatable, and fully available.
3346 In this case, we know that putting it earlier will enable us to
3347 remove the later computation. */
3350 static bool
3351 do_partial_partial_insertion (basic_block block, basic_block dom)
3353 bool new_stuff = false;
3354 vec<pre_expr> exprs;
3355 pre_expr expr;
3356 auto_vec<pre_expr> avail;
3357 int i;
3359 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3360 avail.safe_grow (EDGE_COUNT (block->preds));
3362 FOR_EACH_VEC_ELT (exprs, i, expr)
3364 if (expr->kind == NARY
3365 || expr->kind == REFERENCE)
3367 unsigned int val;
3368 bool by_all = true;
3369 bool cant_insert = false;
3370 edge pred;
3371 basic_block bprime;
3372 pre_expr eprime = NULL;
3373 edge_iterator ei;
3375 val = get_expr_value_id (expr);
3376 if (bitmap_set_contains_value (PHI_GEN (block), val))
3377 continue;
3378 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3379 continue;
3381 FOR_EACH_EDGE (pred, ei, block->preds)
3383 unsigned int vprime;
3384 pre_expr edoubleprime;
3386 /* We should never run insertion for the exit block
3387 and so not come across fake pred edges. */
3388 gcc_assert (!(pred->flags & EDGE_FAKE));
3389 bprime = pred->src;
3390 eprime = phi_translate (expr, ANTIC_IN (block),
3391 PA_IN (block),
3392 bprime, block);
3394 /* eprime will generally only be NULL if the
3395 value of the expression, translated
3396 through the PHI for this predecessor, is
3397 undefined. If that is the case, we can't
3398 make the expression fully redundant,
3399 because its value is undefined along a
3400 predecessor path. We can thus break out
3401 early because it doesn't matter what the
3402 rest of the results are. */
3403 if (eprime == NULL)
3405 avail[pred->dest_idx] = NULL;
3406 cant_insert = true;
3407 break;
3410 eprime = fully_constant_expression (eprime);
3411 vprime = get_expr_value_id (eprime);
3412 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3413 avail[pred->dest_idx] = edoubleprime;
3414 if (edoubleprime == NULL)
3416 by_all = false;
3417 break;
3421 /* If we can insert it, it's not the same value
3422 already existing along every predecessor, and
3423 it's defined by some predecessor, it is
3424 partially redundant. */
3425 if (!cant_insert && by_all)
3427 edge succ;
3428 bool do_insertion = false;
3430 /* Insert only if we can remove a later expression on a path
3431 that we want to optimize for speed.
3432 The phi node that we will be inserting in BLOCK is not free,
3433 and inserting it for the sake of !optimize_for_speed successor
3434 may cause regressions on the speed path. */
3435 FOR_EACH_EDGE (succ, ei, block->succs)
3437 if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3438 || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3440 if (optimize_edge_for_speed_p (succ))
3441 do_insertion = true;
3445 if (!do_insertion)
3447 if (dump_file && (dump_flags & TDF_DETAILS))
3449 fprintf (dump_file, "Skipping partial partial redundancy "
3450 "for expression ");
3451 print_pre_expr (dump_file, expr);
3452 fprintf (dump_file, " (%04d), not (partially) anticipated "
3453 "on any to be optimized for speed edges\n", val);
3456 else if (dbg_cnt (treepre_insert))
3458 pre_stats.pa_insert++;
3459 if (dump_file && (dump_flags & TDF_DETAILS))
3461 fprintf (dump_file, "Found partial partial redundancy "
3462 "for expression ");
3463 print_pre_expr (dump_file, expr);
3464 fprintf (dump_file, " (%04d)\n",
3465 get_expr_value_id (expr));
3467 if (insert_into_preds_of_block (block,
3468 get_expression_id (expr),
3469 avail))
3470 new_stuff = true;
3476 exprs.release ();
3477 return new_stuff;
3480 static bool
3481 insert_aux (basic_block block)
3483 basic_block son;
3484 bool new_stuff = false;
3486 if (block)
3488 basic_block dom;
3489 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3490 if (dom)
3492 unsigned i;
3493 bitmap_iterator bi;
3494 bitmap_set_t newset = NEW_SETS (dom);
3495 if (newset)
3497 /* Note that we need to value_replace both NEW_SETS, and
3498 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3499 represented by some non-simple expression here that we want
3500 to replace it with. */
3501 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3503 pre_expr expr = expression_for_id (i);
3504 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3505 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3508 if (!single_pred_p (block))
3510 new_stuff |= do_regular_insertion (block, dom);
3511 if (do_partial_partial)
3512 new_stuff |= do_partial_partial_insertion (block, dom);
3516 for (son = first_dom_son (CDI_DOMINATORS, block);
3517 son;
3518 son = next_dom_son (CDI_DOMINATORS, son))
3520 new_stuff |= insert_aux (son);
3523 return new_stuff;
3526 /* Perform insertion of partially redundant values. */
3528 static void
3529 insert (void)
3531 bool new_stuff = true;
3532 basic_block bb;
3533 int num_iterations = 0;
3535 FOR_ALL_BB_FN (bb, cfun)
3536 NEW_SETS (bb) = bitmap_set_new ();
3538 while (new_stuff)
3540 num_iterations++;
3541 if (dump_file && dump_flags & TDF_DETAILS)
3542 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3543 new_stuff = insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3545 /* Clear the NEW sets before the next iteration. We have already
3546 fully propagated its contents. */
3547 if (new_stuff)
3548 FOR_ALL_BB_FN (bb, cfun)
3549 bitmap_set_free (NEW_SETS (bb));
3551 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3555 /* Compute the AVAIL set for all basic blocks.
3557 This function performs value numbering of the statements in each basic
3558 block. The AVAIL sets are built from information we glean while doing
3559 this value numbering, since the AVAIL sets contain only one entry per
3560 value.
3562 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3563 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3565 static void
3566 compute_avail (void)
3569 basic_block block, son;
3570 basic_block *worklist;
3571 size_t sp = 0;
3572 unsigned i;
3574 /* We pretend that default definitions are defined in the entry block.
3575 This includes function arguments and the static chain decl. */
3576 for (i = 1; i < num_ssa_names; ++i)
3578 tree name = ssa_name (i);
3579 pre_expr e;
3580 if (!name
3581 || !SSA_NAME_IS_DEFAULT_DEF (name)
3582 || has_zero_uses (name)
3583 || virtual_operand_p (name))
3584 continue;
3586 e = get_or_alloc_expr_for_name (name);
3587 add_to_value (get_expr_value_id (e), e);
3588 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), e);
3589 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3593 if (dump_file && (dump_flags & TDF_DETAILS))
3595 print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3596 "tmp_gen", ENTRY_BLOCK);
3597 print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3598 "avail_out", ENTRY_BLOCK);
3601 /* Allocate the worklist. */
3602 worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
3604 /* Seed the algorithm by putting the dominator children of the entry
3605 block on the worklist. */
3606 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun));
3607 son;
3608 son = next_dom_son (CDI_DOMINATORS, son))
3609 worklist[sp++] = son;
3611 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun))
3612 = ssa_default_def (cfun, gimple_vop (cfun));
3614 /* Loop until the worklist is empty. */
3615 while (sp)
3617 gimple stmt;
3618 basic_block dom;
3620 /* Pick a block from the worklist. */
3621 block = worklist[--sp];
3623 /* Initially, the set of available values in BLOCK is that of
3624 its immediate dominator. */
3625 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3626 if (dom)
3628 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3629 BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
3632 /* Generate values for PHI nodes. */
3633 for (gimple_phi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
3634 gsi_next (&gsi))
3636 tree result = gimple_phi_result (gsi.phi ());
3638 /* We have no need for virtual phis, as they don't represent
3639 actual computations. */
3640 if (virtual_operand_p (result))
3642 BB_LIVE_VOP_ON_EXIT (block) = result;
3643 continue;
3646 pre_expr e = get_or_alloc_expr_for_name (result);
3647 add_to_value (get_expr_value_id (e), e);
3648 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3649 bitmap_insert_into_set (PHI_GEN (block), e);
3652 BB_MAY_NOTRETURN (block) = 0;
3654 /* Now compute value numbers and populate value sets with all
3655 the expressions computed in BLOCK. */
3656 for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
3657 gsi_next (&gsi))
3659 ssa_op_iter iter;
3660 tree op;
3662 stmt = gsi_stmt (gsi);
3664 /* Cache whether the basic-block has any non-visible side-effect
3665 or control flow.
3666 If this isn't a call or it is the last stmt in the
3667 basic-block then the CFG represents things correctly. */
3668 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3670 /* Non-looping const functions always return normally.
3671 Otherwise the call might not return or have side-effects
3672 that forbids hoisting possibly trapping expressions
3673 before it. */
3674 int flags = gimple_call_flags (stmt);
3675 if (!(flags & ECF_CONST)
3676 || (flags & ECF_LOOPING_CONST_OR_PURE))
3677 BB_MAY_NOTRETURN (block) = 1;
3680 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3682 pre_expr e = get_or_alloc_expr_for_name (op);
3684 add_to_value (get_expr_value_id (e), e);
3685 bitmap_insert_into_set (TMP_GEN (block), e);
3686 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3689 if (gimple_vdef (stmt))
3690 BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
3692 if (gimple_has_side_effects (stmt)
3693 || stmt_could_throw_p (stmt)
3694 || is_gimple_debug (stmt))
3695 continue;
3697 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3699 if (ssa_undefined_value_p (op))
3700 continue;
3701 pre_expr e = get_or_alloc_expr_for_name (op);
3702 bitmap_value_insert_into_set (EXP_GEN (block), e);
3705 switch (gimple_code (stmt))
3707 case GIMPLE_RETURN:
3708 continue;
3710 case GIMPLE_CALL:
3712 vn_reference_t ref;
3713 vn_reference_s ref1;
3714 pre_expr result = NULL;
3716 /* We can value number only calls to real functions. */
3717 if (gimple_call_internal_p (stmt))
3718 continue;
3720 vn_reference_lookup_call (as_a <gimple_call> (stmt), &ref, &ref1);
3721 if (!ref)
3722 continue;
3724 /* If the value of the call is not invalidated in
3725 this block until it is computed, add the expression
3726 to EXP_GEN. */
3727 if (!gimple_vuse (stmt)
3728 || gimple_code
3729 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3730 || gimple_bb (SSA_NAME_DEF_STMT
3731 (gimple_vuse (stmt))) != block)
3733 result = (pre_expr) pool_alloc (pre_expr_pool);
3734 result->kind = REFERENCE;
3735 result->id = 0;
3736 PRE_EXPR_REFERENCE (result) = ref;
3738 get_or_alloc_expression_id (result);
3739 add_to_value (get_expr_value_id (result), result);
3740 bitmap_value_insert_into_set (EXP_GEN (block), result);
3742 continue;
3745 case GIMPLE_ASSIGN:
3747 pre_expr result = NULL;
3748 switch (vn_get_stmt_kind (stmt))
3750 case VN_NARY:
3752 enum tree_code code = gimple_assign_rhs_code (stmt);
3753 vn_nary_op_t nary;
3755 /* COND_EXPR and VEC_COND_EXPR are awkward in
3756 that they contain an embedded complex expression.
3757 Don't even try to shove those through PRE. */
3758 if (code == COND_EXPR
3759 || code == VEC_COND_EXPR)
3760 continue;
3762 vn_nary_op_lookup_stmt (stmt, &nary);
3763 if (!nary)
3764 continue;
3766 /* If the NARY traps and there was a preceding
3767 point in the block that might not return avoid
3768 adding the nary to EXP_GEN. */
3769 if (BB_MAY_NOTRETURN (block)
3770 && vn_nary_may_trap (nary))
3771 continue;
3773 result = (pre_expr) pool_alloc (pre_expr_pool);
3774 result->kind = NARY;
3775 result->id = 0;
3776 PRE_EXPR_NARY (result) = nary;
3777 break;
3780 case VN_REFERENCE:
3782 vn_reference_t ref;
3783 vn_reference_lookup (gimple_assign_rhs1 (stmt),
3784 gimple_vuse (stmt),
3785 VN_WALK, &ref);
3786 if (!ref)
3787 continue;
3789 /* If the value of the reference is not invalidated in
3790 this block until it is computed, add the expression
3791 to EXP_GEN. */
3792 if (gimple_vuse (stmt))
3794 gimple def_stmt;
3795 bool ok = true;
3796 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
3797 while (!gimple_nop_p (def_stmt)
3798 && gimple_code (def_stmt) != GIMPLE_PHI
3799 && gimple_bb (def_stmt) == block)
3801 if (stmt_may_clobber_ref_p
3802 (def_stmt, gimple_assign_rhs1 (stmt)))
3804 ok = false;
3805 break;
3807 def_stmt
3808 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
3810 if (!ok)
3811 continue;
3814 result = (pre_expr) pool_alloc (pre_expr_pool);
3815 result->kind = REFERENCE;
3816 result->id = 0;
3817 PRE_EXPR_REFERENCE (result) = ref;
3818 break;
3821 default:
3822 continue;
3825 get_or_alloc_expression_id (result);
3826 add_to_value (get_expr_value_id (result), result);
3827 bitmap_value_insert_into_set (EXP_GEN (block), result);
3828 continue;
3830 default:
3831 break;
3835 if (dump_file && (dump_flags & TDF_DETAILS))
3837 print_bitmap_set (dump_file, EXP_GEN (block),
3838 "exp_gen", block->index);
3839 print_bitmap_set (dump_file, PHI_GEN (block),
3840 "phi_gen", block->index);
3841 print_bitmap_set (dump_file, TMP_GEN (block),
3842 "tmp_gen", block->index);
3843 print_bitmap_set (dump_file, AVAIL_OUT (block),
3844 "avail_out", block->index);
3847 /* Put the dominator children of BLOCK on the worklist of blocks
3848 to compute available sets for. */
3849 for (son = first_dom_son (CDI_DOMINATORS, block);
3850 son;
3851 son = next_dom_son (CDI_DOMINATORS, son))
3852 worklist[sp++] = son;
3855 free (worklist);
3859 /* Local state for the eliminate domwalk. */
3860 static vec<gimple> el_to_remove;
3861 static unsigned int el_todo;
3862 static vec<tree> el_avail;
3863 static vec<tree> el_avail_stack;
3865 /* Return a leader for OP that is available at the current point of the
3866 eliminate domwalk. */
3868 static tree
3869 eliminate_avail (tree op)
3871 tree valnum = VN_INFO (op)->valnum;
3872 if (TREE_CODE (valnum) == SSA_NAME)
3874 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
3875 return valnum;
3876 if (el_avail.length () > SSA_NAME_VERSION (valnum))
3877 return el_avail[SSA_NAME_VERSION (valnum)];
3879 else if (is_gimple_min_invariant (valnum))
3880 return valnum;
3881 return NULL_TREE;
3884 /* At the current point of the eliminate domwalk make OP available. */
3886 static void
3887 eliminate_push_avail (tree op)
3889 tree valnum = VN_INFO (op)->valnum;
3890 if (TREE_CODE (valnum) == SSA_NAME)
3892 if (el_avail.length () <= SSA_NAME_VERSION (valnum))
3893 el_avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1);
3894 el_avail[SSA_NAME_VERSION (valnum)] = op;
3895 el_avail_stack.safe_push (op);
3899 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
3900 the leader for the expression if insertion was successful. */
3902 static tree
3903 eliminate_insert (gimple_stmt_iterator *gsi, tree val)
3905 tree expr = vn_get_expr_for (val);
3906 if (!CONVERT_EXPR_P (expr)
3907 && TREE_CODE (expr) != VIEW_CONVERT_EXPR)
3908 return NULL_TREE;
3910 tree op = TREE_OPERAND (expr, 0);
3911 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (op) : op;
3912 if (!leader)
3913 return NULL_TREE;
3915 tree res = make_temp_ssa_name (TREE_TYPE (val), NULL, "pretmp");
3916 gimple_assign tem =
3917 gimple_build_assign (res,
3918 fold_build1 (TREE_CODE (expr),
3919 TREE_TYPE (expr), leader));
3920 gsi_insert_before (gsi, tem, GSI_SAME_STMT);
3921 VN_INFO_GET (res)->valnum = val;
3923 if (TREE_CODE (leader) == SSA_NAME)
3924 gimple_set_plf (SSA_NAME_DEF_STMT (leader), NECESSARY, true);
3926 pre_stats.insertions++;
3927 if (dump_file && (dump_flags & TDF_DETAILS))
3929 fprintf (dump_file, "Inserted ");
3930 print_gimple_stmt (dump_file, tem, 0, 0);
3933 return res;
3936 class eliminate_dom_walker : public dom_walker
3938 public:
3939 eliminate_dom_walker (cdi_direction direction, bool do_pre_)
3940 : dom_walker (direction), do_pre (do_pre_) {}
3942 virtual void before_dom_children (basic_block);
3943 virtual void after_dom_children (basic_block);
3945 bool do_pre;
3948 /* Perform elimination for the basic-block B during the domwalk. */
3950 void
3951 eliminate_dom_walker::before_dom_children (basic_block b)
3953 /* Mark new bb. */
3954 el_avail_stack.safe_push (NULL_TREE);
3956 /* ??? If we do nothing for unreachable blocks then this will confuse
3957 tailmerging. Eventually we can reduce its reliance on SCCVN now
3958 that we fully copy/constant-propagate (most) things. */
3960 for (gimple_phi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
3962 gimple_phi phi = gsi.phi ();
3963 tree res = PHI_RESULT (phi);
3965 if (virtual_operand_p (res))
3967 gsi_next (&gsi);
3968 continue;
3971 tree sprime = eliminate_avail (res);
3972 if (sprime
3973 && sprime != res)
3975 if (dump_file && (dump_flags & TDF_DETAILS))
3977 fprintf (dump_file, "Replaced redundant PHI node defining ");
3978 print_generic_expr (dump_file, res, 0);
3979 fprintf (dump_file, " with ");
3980 print_generic_expr (dump_file, sprime, 0);
3981 fprintf (dump_file, "\n");
3984 /* If we inserted this PHI node ourself, it's not an elimination. */
3985 if (inserted_exprs
3986 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
3987 pre_stats.phis--;
3988 else
3989 pre_stats.eliminations++;
3991 /* If we will propagate into all uses don't bother to do
3992 anything. */
3993 if (may_propagate_copy (res, sprime))
3995 /* Mark the PHI for removal. */
3996 el_to_remove.safe_push (phi);
3997 gsi_next (&gsi);
3998 continue;
4001 remove_phi_node (&gsi, false);
4003 if (inserted_exprs
4004 && !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res))
4005 && TREE_CODE (sprime) == SSA_NAME)
4006 gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
4008 if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
4009 sprime = fold_convert (TREE_TYPE (res), sprime);
4010 gimple stmt = gimple_build_assign (res, sprime);
4011 /* ??? It cannot yet be necessary (DOM walk). */
4012 gimple_set_plf (stmt, NECESSARY, gimple_plf (phi, NECESSARY));
4014 gimple_stmt_iterator gsi2 = gsi_after_labels (b);
4015 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
4016 continue;
4019 eliminate_push_avail (res);
4020 gsi_next (&gsi);
4023 for (gimple_stmt_iterator gsi = gsi_start_bb (b);
4024 !gsi_end_p (gsi);
4025 gsi_next (&gsi))
4027 tree sprime = NULL_TREE;
4028 gimple stmt = gsi_stmt (gsi);
4029 tree lhs = gimple_get_lhs (stmt);
4030 if (lhs && TREE_CODE (lhs) == SSA_NAME
4031 && !gimple_has_volatile_ops (stmt)
4032 /* See PR43491. Do not replace a global register variable when
4033 it is a the RHS of an assignment. Do replace local register
4034 variables since gcc does not guarantee a local variable will
4035 be allocated in register.
4036 ??? The fix isn't effective here. This should instead
4037 be ensured by not value-numbering them the same but treating
4038 them like volatiles? */
4039 && !(gimple_assign_single_p (stmt)
4040 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
4041 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
4042 && is_global_var (gimple_assign_rhs1 (stmt)))))
4044 sprime = eliminate_avail (lhs);
4045 if (!sprime)
4047 /* If there is no existing usable leader but SCCVN thinks
4048 it has an expression it wants to use as replacement,
4049 insert that. */
4050 tree val = VN_INFO (lhs)->valnum;
4051 if (val != VN_TOP
4052 && TREE_CODE (val) == SSA_NAME
4053 && VN_INFO (val)->needs_insertion
4054 && VN_INFO (val)->expr != NULL_TREE
4055 && (sprime = eliminate_insert (&gsi, val)) != NULL_TREE)
4056 eliminate_push_avail (sprime);
4059 /* If this now constitutes a copy duplicate points-to
4060 and range info appropriately. This is especially
4061 important for inserted code. See tree-ssa-copy.c
4062 for similar code. */
4063 if (sprime
4064 && TREE_CODE (sprime) == SSA_NAME)
4066 basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime));
4067 if (POINTER_TYPE_P (TREE_TYPE (lhs))
4068 && SSA_NAME_PTR_INFO (lhs)
4069 && !SSA_NAME_PTR_INFO (sprime))
4071 duplicate_ssa_name_ptr_info (sprime,
4072 SSA_NAME_PTR_INFO (lhs));
4073 if (b != sprime_b)
4074 mark_ptr_info_alignment_unknown
4075 (SSA_NAME_PTR_INFO (sprime));
4077 else if (!POINTER_TYPE_P (TREE_TYPE (lhs))
4078 && SSA_NAME_RANGE_INFO (lhs)
4079 && !SSA_NAME_RANGE_INFO (sprime)
4080 && b == sprime_b)
4081 duplicate_ssa_name_range_info (sprime,
4082 SSA_NAME_RANGE_TYPE (lhs),
4083 SSA_NAME_RANGE_INFO (lhs));
4086 /* Inhibit the use of an inserted PHI on a loop header when
4087 the address of the memory reference is a simple induction
4088 variable. In other cases the vectorizer won't do anything
4089 anyway (either it's loop invariant or a complicated
4090 expression). */
4091 if (sprime
4092 && TREE_CODE (sprime) == SSA_NAME
4093 && do_pre
4094 && flag_tree_loop_vectorize
4095 && loop_outer (b->loop_father)
4096 && has_zero_uses (sprime)
4097 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
4098 && gimple_assign_load_p (stmt))
4100 gimple def_stmt = SSA_NAME_DEF_STMT (sprime);
4101 basic_block def_bb = gimple_bb (def_stmt);
4102 if (gimple_code (def_stmt) == GIMPLE_PHI
4103 && b->loop_father->header == def_bb)
4105 ssa_op_iter iter;
4106 tree op;
4107 bool found = false;
4108 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
4110 affine_iv iv;
4111 def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
4112 if (def_bb
4113 && flow_bb_inside_loop_p (b->loop_father, def_bb)
4114 && simple_iv (b->loop_father,
4115 b->loop_father, op, &iv, true))
4117 found = true;
4118 break;
4121 if (found)
4123 if (dump_file && (dump_flags & TDF_DETAILS))
4125 fprintf (dump_file, "Not replacing ");
4126 print_gimple_expr (dump_file, stmt, 0, 0);
4127 fprintf (dump_file, " with ");
4128 print_generic_expr (dump_file, sprime, 0);
4129 fprintf (dump_file, " which would add a loop"
4130 " carried dependence to loop %d\n",
4131 b->loop_father->num);
4133 /* Don't keep sprime available. */
4134 sprime = NULL_TREE;
4139 if (sprime)
4141 /* If we can propagate the value computed for LHS into
4142 all uses don't bother doing anything with this stmt. */
4143 if (may_propagate_copy (lhs, sprime))
4145 /* Mark it for removal. */
4146 el_to_remove.safe_push (stmt);
4148 /* ??? Don't count copy/constant propagations. */
4149 if (gimple_assign_single_p (stmt)
4150 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
4151 || gimple_assign_rhs1 (stmt) == sprime))
4152 continue;
4154 if (dump_file && (dump_flags & TDF_DETAILS))
4156 fprintf (dump_file, "Replaced ");
4157 print_gimple_expr (dump_file, stmt, 0, 0);
4158 fprintf (dump_file, " with ");
4159 print_generic_expr (dump_file, sprime, 0);
4160 fprintf (dump_file, " in all uses of ");
4161 print_gimple_stmt (dump_file, stmt, 0, 0);
4164 pre_stats.eliminations++;
4165 continue;
4168 /* If this is an assignment from our leader (which
4169 happens in the case the value-number is a constant)
4170 then there is nothing to do. */
4171 if (gimple_assign_single_p (stmt)
4172 && sprime == gimple_assign_rhs1 (stmt))
4173 continue;
4175 /* Else replace its RHS. */
4176 bool can_make_abnormal_goto
4177 = is_gimple_call (stmt)
4178 && stmt_can_make_abnormal_goto (stmt);
4180 if (dump_file && (dump_flags & TDF_DETAILS))
4182 fprintf (dump_file, "Replaced ");
4183 print_gimple_expr (dump_file, stmt, 0, 0);
4184 fprintf (dump_file, " with ");
4185 print_generic_expr (dump_file, sprime, 0);
4186 fprintf (dump_file, " in ");
4187 print_gimple_stmt (dump_file, stmt, 0, 0);
4190 if (TREE_CODE (sprime) == SSA_NAME)
4191 gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
4192 NECESSARY, true);
4194 pre_stats.eliminations++;
4195 gimple orig_stmt = stmt;
4196 if (!useless_type_conversion_p (TREE_TYPE (lhs),
4197 TREE_TYPE (sprime)))
4198 sprime = fold_convert (TREE_TYPE (lhs), sprime);
4199 tree vdef = gimple_vdef (stmt);
4200 tree vuse = gimple_vuse (stmt);
4201 propagate_tree_value_into_stmt (&gsi, sprime);
4202 stmt = gsi_stmt (gsi);
4203 update_stmt (stmt);
4204 if (vdef != gimple_vdef (stmt))
4205 VN_INFO (vdef)->valnum = vuse;
4207 /* If we removed EH side-effects from the statement, clean
4208 its EH information. */
4209 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4211 bitmap_set_bit (need_eh_cleanup,
4212 gimple_bb (stmt)->index);
4213 if (dump_file && (dump_flags & TDF_DETAILS))
4214 fprintf (dump_file, " Removed EH side-effects.\n");
4217 /* Likewise for AB side-effects. */
4218 if (can_make_abnormal_goto
4219 && !stmt_can_make_abnormal_goto (stmt))
4221 bitmap_set_bit (need_ab_cleanup,
4222 gimple_bb (stmt)->index);
4223 if (dump_file && (dump_flags & TDF_DETAILS))
4224 fprintf (dump_file, " Removed AB side-effects.\n");
4227 continue;
4231 /* If the statement is a scalar store, see if the expression
4232 has the same value number as its rhs. If so, the store is
4233 dead. */
4234 if (gimple_assign_single_p (stmt)
4235 && !gimple_has_volatile_ops (stmt)
4236 && !is_gimple_reg (gimple_assign_lhs (stmt))
4237 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
4238 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
4240 tree val;
4241 tree rhs = gimple_assign_rhs1 (stmt);
4242 val = vn_reference_lookup (gimple_assign_lhs (stmt),
4243 gimple_vuse (stmt), VN_WALK, NULL);
4244 if (TREE_CODE (rhs) == SSA_NAME)
4245 rhs = VN_INFO (rhs)->valnum;
4246 if (val
4247 && operand_equal_p (val, rhs, 0))
4249 if (dump_file && (dump_flags & TDF_DETAILS))
4251 fprintf (dump_file, "Deleted redundant store ");
4252 print_gimple_stmt (dump_file, stmt, 0, 0);
4255 /* Queue stmt for removal. */
4256 el_to_remove.safe_push (stmt);
4257 continue;
4261 bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
4262 bool was_noreturn = (is_gimple_call (stmt)
4263 && gimple_call_noreturn_p (stmt));
4264 tree vdef = gimple_vdef (stmt);
4265 tree vuse = gimple_vuse (stmt);
4267 /* If we didn't replace the whole stmt (or propagate the result
4268 into all uses), replace all uses on this stmt with their
4269 leaders. */
4270 use_operand_p use_p;
4271 ssa_op_iter iter;
4272 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
4274 tree use = USE_FROM_PTR (use_p);
4275 /* ??? The call code above leaves stmt operands un-updated. */
4276 if (TREE_CODE (use) != SSA_NAME)
4277 continue;
4278 tree sprime = eliminate_avail (use);
4279 if (sprime && sprime != use
4280 && may_propagate_copy (use, sprime)
4281 /* We substitute into debug stmts to avoid excessive
4282 debug temporaries created by removed stmts, but we need
4283 to avoid doing so for inserted sprimes as we never want
4284 to create debug temporaries for them. */
4285 && (!inserted_exprs
4286 || TREE_CODE (sprime) != SSA_NAME
4287 || !is_gimple_debug (stmt)
4288 || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
4290 propagate_value (use_p, sprime);
4291 gimple_set_modified (stmt, true);
4292 if (TREE_CODE (sprime) == SSA_NAME
4293 && !is_gimple_debug (stmt))
4294 gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
4295 NECESSARY, true);
4299 /* Visit indirect calls and turn them into direct calls if
4300 possible using the devirtualization machinery. */
4301 if (gimple_call call_stmt = dyn_cast <gimple_call> (stmt))
4303 tree fn = gimple_call_fn (call_stmt);
4304 if (fn
4305 && flag_devirtualize
4306 && virtual_method_call_p (fn))
4308 tree otr_type = obj_type_ref_class (fn);
4309 tree instance;
4310 ipa_polymorphic_call_context context (current_function_decl, fn, stmt, &instance);
4311 bool final;
4313 context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn), otr_type, stmt);
4315 vec <cgraph_node *>targets
4316 = possible_polymorphic_call_targets (obj_type_ref_class (fn),
4317 tree_to_uhwi
4318 (OBJ_TYPE_REF_TOKEN (fn)),
4319 context,
4320 &final);
4321 if (dump_enabled_p ())
4322 dump_possible_polymorphic_call_targets (dump_file,
4323 obj_type_ref_class (fn),
4324 tree_to_uhwi
4325 (OBJ_TYPE_REF_TOKEN (fn)),
4326 context);
4327 if (final && targets.length () <= 1 && dbg_cnt (devirt))
4329 tree fn;
4330 if (targets.length () == 1)
4331 fn = targets[0]->decl;
4332 else
4333 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
4334 if (dump_enabled_p ())
4336 location_t loc = gimple_location_safe (stmt);
4337 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
4338 "converting indirect call to "
4339 "function %s\n",
4340 cgraph_node::get (fn)->name ());
4342 gimple_call_set_fndecl (call_stmt, fn);
4343 gimple_set_modified (stmt, true);
4348 if (gimple_modified_p (stmt))
4350 /* If a formerly non-invariant ADDR_EXPR is turned into an
4351 invariant one it was on a separate stmt. */
4352 if (gimple_assign_single_p (stmt)
4353 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
4354 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
4355 gimple old_stmt = stmt;
4356 if (is_gimple_call (stmt))
4358 /* ??? Only fold calls inplace for now, this may create new
4359 SSA names which in turn will confuse free_scc_vn SSA name
4360 release code. */
4361 fold_stmt_inplace (&gsi);
4362 /* When changing a call into a noreturn call, cfg cleanup
4363 is needed to fix up the noreturn call. */
4364 if (!was_noreturn && gimple_call_noreturn_p (stmt))
4365 el_todo |= TODO_cleanup_cfg;
4367 else
4369 fold_stmt (&gsi);
4370 stmt = gsi_stmt (gsi);
4371 if ((gimple_code (stmt) == GIMPLE_COND
4372 && (gimple_cond_true_p (as_a <gimple_cond> (stmt))
4373 || gimple_cond_false_p (as_a <gimple_cond> (stmt))))
4374 || (gimple_code (stmt) == GIMPLE_SWITCH
4375 && TREE_CODE (gimple_switch_index (
4376 as_a <gimple_switch> (stmt)))
4377 == INTEGER_CST))
4378 el_todo |= TODO_cleanup_cfg;
4380 /* If we removed EH side-effects from the statement, clean
4381 its EH information. */
4382 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
4384 bitmap_set_bit (need_eh_cleanup,
4385 gimple_bb (stmt)->index);
4386 if (dump_file && (dump_flags & TDF_DETAILS))
4387 fprintf (dump_file, " Removed EH side-effects.\n");
4389 /* Likewise for AB side-effects. */
4390 if (can_make_abnormal_goto
4391 && !stmt_can_make_abnormal_goto (stmt))
4393 bitmap_set_bit (need_ab_cleanup,
4394 gimple_bb (stmt)->index);
4395 if (dump_file && (dump_flags & TDF_DETAILS))
4396 fprintf (dump_file, " Removed AB side-effects.\n");
4398 update_stmt (stmt);
4399 if (vdef != gimple_vdef (stmt))
4400 VN_INFO (vdef)->valnum = vuse;
4403 /* Make new values available - for fully redundant LHS we
4404 continue with the next stmt above and skip this. */
4405 def_operand_p defp;
4406 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
4407 eliminate_push_avail (DEF_FROM_PTR (defp));
4410 /* Replace destination PHI arguments. */
4411 edge_iterator ei;
4412 edge e;
4413 FOR_EACH_EDGE (e, ei, b->succs)
4415 for (gimple_phi_iterator gsi = gsi_start_phis (e->dest);
4416 !gsi_end_p (gsi);
4417 gsi_next (&gsi))
4419 gimple_phi phi = gsi.phi ();
4420 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
4421 tree arg = USE_FROM_PTR (use_p);
4422 if (TREE_CODE (arg) != SSA_NAME
4423 || virtual_operand_p (arg))
4424 continue;
4425 tree sprime = eliminate_avail (arg);
4426 if (sprime && may_propagate_copy (arg, sprime))
4428 propagate_value (use_p, sprime);
4429 if (TREE_CODE (sprime) == SSA_NAME)
4430 gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
4436 /* Make no longer available leaders no longer available. */
4438 void
4439 eliminate_dom_walker::after_dom_children (basic_block)
4441 tree entry;
4442 while ((entry = el_avail_stack.pop ()) != NULL_TREE)
4443 el_avail[SSA_NAME_VERSION (VN_INFO (entry)->valnum)] = NULL_TREE;
4446 /* Eliminate fully redundant computations. */
4448 static unsigned int
4449 eliminate (bool do_pre)
4451 gimple_stmt_iterator gsi;
4452 gimple stmt;
4454 need_eh_cleanup = BITMAP_ALLOC (NULL);
4455 need_ab_cleanup = BITMAP_ALLOC (NULL);
4457 el_to_remove.create (0);
4458 el_todo = 0;
4459 el_avail.create (num_ssa_names);
4460 el_avail_stack.create (0);
4462 eliminate_dom_walker (CDI_DOMINATORS,
4463 do_pre).walk (cfun->cfg->x_entry_block_ptr);
4465 el_avail.release ();
4466 el_avail_stack.release ();
4468 /* We cannot remove stmts during BB walk, especially not release SSA
4469 names there as this confuses the VN machinery. The stmts ending
4470 up in el_to_remove are either stores or simple copies.
4471 Remove stmts in reverse order to make debug stmt creation possible. */
4472 while (!el_to_remove.is_empty ())
4474 stmt = el_to_remove.pop ();
4476 if (dump_file && (dump_flags & TDF_DETAILS))
4478 fprintf (dump_file, "Removing dead stmt ");
4479 print_gimple_stmt (dump_file, stmt, 0, 0);
4482 tree lhs;
4483 if (gimple_code (stmt) == GIMPLE_PHI)
4484 lhs = gimple_phi_result (stmt);
4485 else
4486 lhs = gimple_get_lhs (stmt);
4488 if (inserted_exprs
4489 && TREE_CODE (lhs) == SSA_NAME)
4490 bitmap_clear_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
4492 gsi = gsi_for_stmt (stmt);
4493 if (gimple_code (stmt) == GIMPLE_PHI)
4494 remove_phi_node (&gsi, true);
4495 else
4497 basic_block bb = gimple_bb (stmt);
4498 unlink_stmt_vdef (stmt);
4499 if (gsi_remove (&gsi, true))
4500 bitmap_set_bit (need_eh_cleanup, bb->index);
4501 release_defs (stmt);
4504 /* Removing a stmt may expose a forwarder block. */
4505 el_todo |= TODO_cleanup_cfg;
4507 el_to_remove.release ();
4509 return el_todo;
4512 /* Perform CFG cleanups made necessary by elimination. */
4514 static unsigned
4515 fini_eliminate (void)
4517 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
4518 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
4520 if (do_eh_cleanup)
4521 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
4523 if (do_ab_cleanup)
4524 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
4526 BITMAP_FREE (need_eh_cleanup);
4527 BITMAP_FREE (need_ab_cleanup);
4529 if (do_eh_cleanup || do_ab_cleanup)
4530 return TODO_cleanup_cfg;
4531 return 0;
4534 /* Borrow a bit of tree-ssa-dce.c for the moment.
4535 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4536 this may be a bit faster, and we may want critical edges kept split. */
4538 /* If OP's defining statement has not already been determined to be necessary,
4539 mark that statement necessary. Return the stmt, if it is newly
4540 necessary. */
4542 static inline gimple
4543 mark_operand_necessary (tree op)
4545 gimple stmt;
4547 gcc_assert (op);
4549 if (TREE_CODE (op) != SSA_NAME)
4550 return NULL;
4552 stmt = SSA_NAME_DEF_STMT (op);
4553 gcc_assert (stmt);
4555 if (gimple_plf (stmt, NECESSARY)
4556 || gimple_nop_p (stmt))
4557 return NULL;
4559 gimple_set_plf (stmt, NECESSARY, true);
4560 return stmt;
4563 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4564 to insert PHI nodes sometimes, and because value numbering of casts isn't
4565 perfect, we sometimes end up inserting dead code. This simple DCE-like
4566 pass removes any insertions we made that weren't actually used. */
4568 static void
4569 remove_dead_inserted_code (void)
4571 bitmap worklist;
4572 unsigned i;
4573 bitmap_iterator bi;
4574 gimple t;
4576 worklist = BITMAP_ALLOC (NULL);
4577 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4579 t = SSA_NAME_DEF_STMT (ssa_name (i));
4580 if (gimple_plf (t, NECESSARY))
4581 bitmap_set_bit (worklist, i);
4583 while (!bitmap_empty_p (worklist))
4585 i = bitmap_first_set_bit (worklist);
4586 bitmap_clear_bit (worklist, i);
4587 t = SSA_NAME_DEF_STMT (ssa_name (i));
4589 /* PHI nodes are somewhat special in that each PHI alternative has
4590 data and control dependencies. All the statements feeding the
4591 PHI node's arguments are always necessary. */
4592 if (gimple_code (t) == GIMPLE_PHI)
4594 unsigned k;
4596 for (k = 0; k < gimple_phi_num_args (t); k++)
4598 tree arg = PHI_ARG_DEF (t, k);
4599 if (TREE_CODE (arg) == SSA_NAME)
4601 gimple n = mark_operand_necessary (arg);
4602 if (n)
4603 bitmap_set_bit (worklist, SSA_NAME_VERSION (arg));
4607 else
4609 /* Propagate through the operands. Examine all the USE, VUSE and
4610 VDEF operands in this statement. Mark all the statements
4611 which feed this statement's uses as necessary. */
4612 ssa_op_iter iter;
4613 tree use;
4615 /* The operands of VDEF expressions are also needed as they
4616 represent potential definitions that may reach this
4617 statement (VDEF operands allow us to follow def-def
4618 links). */
4620 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
4622 gimple n = mark_operand_necessary (use);
4623 if (n)
4624 bitmap_set_bit (worklist, SSA_NAME_VERSION (use));
4629 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4631 t = SSA_NAME_DEF_STMT (ssa_name (i));
4632 if (!gimple_plf (t, NECESSARY))
4634 gimple_stmt_iterator gsi;
4636 if (dump_file && (dump_flags & TDF_DETAILS))
4638 fprintf (dump_file, "Removing unnecessary insertion:");
4639 print_gimple_stmt (dump_file, t, 0, 0);
4642 gsi = gsi_for_stmt (t);
4643 if (gimple_code (t) == GIMPLE_PHI)
4644 remove_phi_node (&gsi, true);
4645 else
4647 gsi_remove (&gsi, true);
4648 release_defs (t);
4652 BITMAP_FREE (worklist);
4656 /* Initialize data structures used by PRE. */
4658 static void
4659 init_pre (void)
4661 basic_block bb;
4663 next_expression_id = 1;
4664 expressions.create (0);
4665 expressions.safe_push (NULL);
4666 value_expressions.create (get_max_value_id () + 1);
4667 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
4668 name_to_id.create (0);
4670 inserted_exprs = BITMAP_ALLOC (NULL);
4672 connect_infinite_loops_to_exit ();
4673 memset (&pre_stats, 0, sizeof (pre_stats));
4675 postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
4676 postorder_num = inverted_post_order_compute (postorder);
4678 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4680 calculate_dominance_info (CDI_POST_DOMINATORS);
4681 calculate_dominance_info (CDI_DOMINATORS);
4683 bitmap_obstack_initialize (&grand_bitmap_obstack);
4684 phi_translate_table = new hash_table<expr_pred_trans_d> (5110);
4685 expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4686 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
4687 sizeof (struct bitmap_set), 30);
4688 pre_expr_pool = create_alloc_pool ("pre_expr nodes",
4689 sizeof (struct pre_expr_d), 30);
4690 FOR_ALL_BB_FN (bb, cfun)
4692 EXP_GEN (bb) = bitmap_set_new ();
4693 PHI_GEN (bb) = bitmap_set_new ();
4694 TMP_GEN (bb) = bitmap_set_new ();
4695 AVAIL_OUT (bb) = bitmap_set_new ();
4700 /* Deallocate data structures used by PRE. */
4702 static void
4703 fini_pre ()
4705 free (postorder);
4706 value_expressions.release ();
4707 BITMAP_FREE (inserted_exprs);
4708 bitmap_obstack_release (&grand_bitmap_obstack);
4709 free_alloc_pool (bitmap_set_pool);
4710 free_alloc_pool (pre_expr_pool);
4711 delete phi_translate_table;
4712 phi_translate_table = NULL;
4713 delete expression_to_id;
4714 expression_to_id = NULL;
4715 name_to_id.release ();
4717 free_aux_for_blocks ();
4719 free_dominance_info (CDI_POST_DOMINATORS);
4722 namespace {
4724 const pass_data pass_data_pre =
4726 GIMPLE_PASS, /* type */
4727 "pre", /* name */
4728 OPTGROUP_NONE, /* optinfo_flags */
4729 TV_TREE_PRE, /* tv_id */
4730 /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before
4731 pass_pre. */
4732 ( PROP_no_crit_edges | PROP_cfg | PROP_ssa ), /* properties_required */
4733 0, /* properties_provided */
4734 PROP_no_crit_edges, /* properties_destroyed */
4735 TODO_rebuild_alias, /* todo_flags_start */
4736 0, /* todo_flags_finish */
4739 class pass_pre : public gimple_opt_pass
4741 public:
4742 pass_pre (gcc::context *ctxt)
4743 : gimple_opt_pass (pass_data_pre, ctxt)
4746 /* opt_pass methods: */
4747 virtual bool gate (function *) { return flag_tree_pre != 0; }
4748 virtual unsigned int execute (function *);
4750 }; // class pass_pre
4752 unsigned int
4753 pass_pre::execute (function *fun)
4755 unsigned int todo = 0;
4757 do_partial_partial =
4758 flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4760 /* This has to happen before SCCVN runs because
4761 loop_optimizer_init may create new phis, etc. */
4762 loop_optimizer_init (LOOPS_NORMAL);
4764 if (!run_scc_vn (VN_WALK))
4766 loop_optimizer_finalize ();
4767 return 0;
4770 init_pre ();
4771 scev_initialize ();
4773 /* Collect and value number expressions computed in each basic block. */
4774 compute_avail ();
4776 /* Insert can get quite slow on an incredibly large number of basic
4777 blocks due to some quadratic behavior. Until this behavior is
4778 fixed, don't run it when he have an incredibly large number of
4779 bb's. If we aren't going to run insert, there is no point in
4780 computing ANTIC, either, even though it's plenty fast. */
4781 if (n_basic_blocks_for_fn (fun) < 4000)
4783 compute_antic ();
4784 insert ();
4787 /* Make sure to remove fake edges before committing our inserts.
4788 This makes sure we don't end up with extra critical edges that
4789 we would need to split. */
4790 remove_fake_exit_edges ();
4791 gsi_commit_edge_inserts ();
4793 /* Eliminate folds statements which might (should not...) end up
4794 not keeping virtual operands up-to-date. */
4795 gcc_assert (!need_ssa_update_p (fun));
4797 /* Remove all the redundant expressions. */
4798 todo |= eliminate (true);
4800 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4801 statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4802 statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4803 statistics_counter_event (fun, "Eliminated", pre_stats.eliminations);
4805 clear_expression_ids ();
4806 remove_dead_inserted_code ();
4808 scev_finalize ();
4809 fini_pre ();
4810 todo |= fini_eliminate ();
4811 loop_optimizer_finalize ();
4813 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4814 case we can merge the block with the remaining predecessor of the block.
4815 It should either:
4816 - call merge_blocks after each tail merge iteration
4817 - call merge_blocks after all tail merge iterations
4818 - mark TODO_cleanup_cfg when necessary
4819 - share the cfg cleanup with fini_pre. */
4820 todo |= tail_merge_optimize (todo);
4822 free_scc_vn ();
4824 /* Tail merging invalidates the virtual SSA web, together with
4825 cfg-cleanup opportunities exposed by PRE this will wreck the
4826 SSA updating machinery. So make sure to run update-ssa
4827 manually, before eventually scheduling cfg-cleanup as part of
4828 the todo. */
4829 update_ssa (TODO_update_ssa_only_virtuals);
4831 return todo;
4834 } // anon namespace
4836 gimple_opt_pass *
4837 make_pass_pre (gcc::context *ctxt)
4839 return new pass_pre (ctxt);
4842 namespace {
4844 const pass_data pass_data_fre =
4846 GIMPLE_PASS, /* type */
4847 "fre", /* name */
4848 OPTGROUP_NONE, /* optinfo_flags */
4849 TV_TREE_FRE, /* tv_id */
4850 ( PROP_cfg | PROP_ssa ), /* properties_required */
4851 0, /* properties_provided */
4852 0, /* properties_destroyed */
4853 0, /* todo_flags_start */
4854 0, /* todo_flags_finish */
4857 class pass_fre : public gimple_opt_pass
4859 public:
4860 pass_fre (gcc::context *ctxt)
4861 : gimple_opt_pass (pass_data_fre, ctxt)
4864 /* opt_pass methods: */
4865 opt_pass * clone () { return new pass_fre (m_ctxt); }
4866 virtual bool gate (function *) { return flag_tree_fre != 0; }
4867 virtual unsigned int execute (function *);
4869 }; // class pass_fre
4871 unsigned int
4872 pass_fre::execute (function *fun)
4874 unsigned int todo = 0;
4876 if (!run_scc_vn (VN_WALKREWRITE))
4877 return 0;
4879 memset (&pre_stats, 0, sizeof (pre_stats));
4881 /* Remove all the redundant expressions. */
4882 todo |= eliminate (false);
4884 todo |= fini_eliminate ();
4886 free_scc_vn ();
4888 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4889 statistics_counter_event (fun, "Eliminated", pre_stats.eliminations);
4891 return todo;
4894 } // anon namespace
4896 gimple_opt_pass *
4897 make_pass_fre (gcc::context *ctxt)
4899 return new pass_fre (ctxt);