Implement TARGET_IRA_CHANGE_PSEUDO_ALLOCNO_CLASS hook.
[official-gcc.git] / gcc / tree-ssa-pre.c
blobd2184927953273ac35762166032f1d692de8ccc0
1 /* SSA-PRE for trees.
2 Copyright (C) 2001-2015 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 "input.h"
27 #include "alias.h"
28 #include "symtab.h"
29 #include "tree.h"
30 #include "fold-const.h"
31 #include "predict.h"
32 #include "hard-reg-set.h"
33 #include "function.h"
34 #include "dominance.h"
35 #include "cfg.h"
36 #include "cfganal.h"
37 #include "basic-block.h"
38 #include "gimple-pretty-print.h"
39 #include "tree-inline.h"
40 #include "tree-ssa-alias.h"
41 #include "internal-fn.h"
42 #include "gimple-fold.h"
43 #include "tree-eh.h"
44 #include "gimple-expr.h"
45 #include "is-a.h"
46 #include "gimple.h"
47 #include "gimplify.h"
48 #include "gimple-iterator.h"
49 #include "gimplify-me.h"
50 #include "gimple-ssa.h"
51 #include "tree-cfg.h"
52 #include "tree-phinodes.h"
53 #include "ssa-iterators.h"
54 #include "stringpool.h"
55 #include "tree-ssanames.h"
56 #include "tree-ssa-loop.h"
57 #include "tree-into-ssa.h"
58 #include "rtl.h"
59 #include "flags.h"
60 #include "insn-config.h"
61 #include "expmed.h"
62 #include "dojump.h"
63 #include "explow.h"
64 #include "calls.h"
65 #include "emit-rtl.h"
66 #include "varasm.h"
67 #include "stmt.h"
68 #include "expr.h"
69 #include "tree-dfa.h"
70 #include "tree-ssa.h"
71 #include "tree-iterator.h"
72 #include "alloc-pool.h"
73 #include "obstack.h"
74 #include "tree-pass.h"
75 #include "langhooks.h"
76 #include "cfgloop.h"
77 #include "tree-ssa-sccvn.h"
78 #include "tree-scalar-evolution.h"
79 #include "params.h"
80 #include "dbgcnt.h"
81 #include "domwalk.h"
82 #include "plugin-api.h"
83 #include "ipa-ref.h"
84 #include "cgraph.h"
85 #include "symbol-summary.h"
86 #include "ipa-prop.h"
87 #include "tree-ssa-propagate.h"
88 #include "ipa-utils.h"
89 #include "tree-cfgcleanup.h"
91 /* TODO:
93 1. Avail sets can be shared by making an avail_find_leader that
94 walks up the dominator tree and looks in those avail sets.
95 This might affect code optimality, it's unclear right now.
96 2. Strength reduction can be performed by anticipating expressions
97 we can repair later on.
98 3. We can do back-substitution or smarter value numbering to catch
99 commutative expressions split up over multiple statements.
102 /* For ease of terminology, "expression node" in the below refers to
103 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
104 represent the actual statement containing the expressions we care about,
105 and we cache the value number by putting it in the expression. */
107 /* Basic algorithm
109 First we walk the statements to generate the AVAIL sets, the
110 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
111 generation of values/expressions by a given block. We use them
112 when computing the ANTIC sets. The AVAIL sets consist of
113 SSA_NAME's that represent values, so we know what values are
114 available in what blocks. AVAIL is a forward dataflow problem. In
115 SSA, values are never killed, so we don't need a kill set, or a
116 fixpoint iteration, in order to calculate the AVAIL sets. In
117 traditional parlance, AVAIL sets tell us the downsafety of the
118 expressions/values.
120 Next, we generate the ANTIC sets. These sets represent the
121 anticipatable expressions. ANTIC is a backwards dataflow
122 problem. An expression is anticipatable in a given block if it could
123 be generated in that block. This means that if we had to perform
124 an insertion in that block, of the value of that expression, we
125 could. Calculating the ANTIC sets requires phi translation of
126 expressions, because the flow goes backwards through phis. We must
127 iterate to a fixpoint of the ANTIC sets, because we have a kill
128 set. Even in SSA form, values are not live over the entire
129 function, only from their definition point onwards. So we have to
130 remove values from the ANTIC set once we go past the definition
131 point of the leaders that make them up.
132 compute_antic/compute_antic_aux performs this computation.
134 Third, we perform insertions to make partially redundant
135 expressions fully redundant.
137 An expression is partially redundant (excluding partial
138 anticipation) if:
140 1. It is AVAIL in some, but not all, of the predecessors of a
141 given block.
142 2. It is ANTIC in all the predecessors.
144 In order to make it fully redundant, we insert the expression into
145 the predecessors where it is not available, but is ANTIC.
147 For the partial anticipation case, we only perform insertion if it
148 is partially anticipated in some block, and fully available in all
149 of the predecessors.
151 insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
152 performs these steps.
154 Fourth, we eliminate fully redundant expressions.
155 This is a simple statement walk that replaces redundant
156 calculations with the now available values. */
158 /* Representations of value numbers:
160 Value numbers are represented by a representative SSA_NAME. We
161 will create fake SSA_NAME's in situations where we need a
162 representative but do not have one (because it is a complex
163 expression). In order to facilitate storing the value numbers in
164 bitmaps, and keep the number of wasted SSA_NAME's down, we also
165 associate a value_id with each value number, and create full blown
166 ssa_name's only where we actually need them (IE in operands of
167 existing expressions).
169 Theoretically you could replace all the value_id's with
170 SSA_NAME_VERSION, but this would allocate a large number of
171 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
172 It would also require an additional indirection at each point we
173 use the value id. */
175 /* Representation of expressions on value numbers:
177 Expressions consisting of value numbers are represented the same
178 way as our VN internally represents them, with an additional
179 "pre_expr" wrapping around them in order to facilitate storing all
180 of the expressions in the same sets. */
182 /* Representation of sets:
184 The dataflow sets do not need to be sorted in any particular order
185 for the majority of their lifetime, are simply represented as two
186 bitmaps, one that keeps track of values present in the set, and one
187 that keeps track of expressions present in the set.
189 When we need them in topological order, we produce it on demand by
190 transforming the bitmap into an array and sorting it into topo
191 order. */
193 /* Type of expression, used to know which member of the PRE_EXPR union
194 is valid. */
196 enum pre_expr_kind
198 NAME,
199 NARY,
200 REFERENCE,
201 CONSTANT
204 typedef union pre_expr_union_d
206 tree name;
207 tree constant;
208 vn_nary_op_t nary;
209 vn_reference_t reference;
210 } pre_expr_union;
212 typedef struct pre_expr_d : typed_noop_remove <pre_expr_d>
214 enum pre_expr_kind kind;
215 unsigned int id;
216 pre_expr_union u;
218 /* hash_table support. */
219 typedef pre_expr_d *value_type;
220 typedef pre_expr_d *compare_type;
221 static inline hashval_t hash (const pre_expr_d *);
222 static inline int equal (const pre_expr_d *, const pre_expr_d *);
223 } *pre_expr;
225 #define PRE_EXPR_NAME(e) (e)->u.name
226 #define PRE_EXPR_NARY(e) (e)->u.nary
227 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
228 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
230 /* Compare E1 and E1 for equality. */
232 inline int
233 pre_expr_d::equal (const pre_expr_d *e1, const pre_expr_d *e2)
235 if (e1->kind != e2->kind)
236 return false;
238 switch (e1->kind)
240 case CONSTANT:
241 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
242 PRE_EXPR_CONSTANT (e2));
243 case NAME:
244 return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
245 case NARY:
246 return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
247 case REFERENCE:
248 return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
249 PRE_EXPR_REFERENCE (e2));
250 default:
251 gcc_unreachable ();
255 /* Hash E. */
257 inline hashval_t
258 pre_expr_d::hash (const pre_expr_d *e)
260 switch (e->kind)
262 case CONSTANT:
263 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
264 case NAME:
265 return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
266 case NARY:
267 return PRE_EXPR_NARY (e)->hashcode;
268 case REFERENCE:
269 return PRE_EXPR_REFERENCE (e)->hashcode;
270 default:
271 gcc_unreachable ();
275 /* Next global expression id number. */
276 static unsigned int next_expression_id;
278 /* Mapping from expression to id number we can use in bitmap sets. */
279 static vec<pre_expr> expressions;
280 static hash_table<pre_expr_d> *expression_to_id;
281 static vec<unsigned> name_to_id;
283 /* Allocate an expression id for EXPR. */
285 static inline unsigned int
286 alloc_expression_id (pre_expr expr)
288 struct pre_expr_d **slot;
289 /* Make sure we won't overflow. */
290 gcc_assert (next_expression_id + 1 > next_expression_id);
291 expr->id = next_expression_id++;
292 expressions.safe_push (expr);
293 if (expr->kind == NAME)
295 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
296 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
297 re-allocations by using vec::reserve upfront. */
298 unsigned old_len = name_to_id.length ();
299 name_to_id.reserve (num_ssa_names - old_len);
300 name_to_id.quick_grow_cleared (num_ssa_names);
301 gcc_assert (name_to_id[version] == 0);
302 name_to_id[version] = expr->id;
304 else
306 slot = expression_to_id->find_slot (expr, INSERT);
307 gcc_assert (!*slot);
308 *slot = expr;
310 return next_expression_id - 1;
313 /* Return the expression id for tree EXPR. */
315 static inline unsigned int
316 get_expression_id (const pre_expr expr)
318 return expr->id;
321 static inline unsigned int
322 lookup_expression_id (const pre_expr expr)
324 struct pre_expr_d **slot;
326 if (expr->kind == NAME)
328 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
329 if (name_to_id.length () <= version)
330 return 0;
331 return name_to_id[version];
333 else
335 slot = expression_to_id->find_slot (expr, NO_INSERT);
336 if (!slot)
337 return 0;
338 return ((pre_expr)*slot)->id;
342 /* Return the existing expression id for EXPR, or create one if one
343 does not exist yet. */
345 static inline unsigned int
346 get_or_alloc_expression_id (pre_expr expr)
348 unsigned int id = lookup_expression_id (expr);
349 if (id == 0)
350 return alloc_expression_id (expr);
351 return expr->id = id;
354 /* Return the expression that has expression id ID */
356 static inline pre_expr
357 expression_for_id (unsigned int id)
359 return expressions[id];
362 /* Free the expression id field in all of our expressions,
363 and then destroy the expressions array. */
365 static void
366 clear_expression_ids (void)
368 expressions.release ();
371 static pool_allocator<pre_expr_d> pre_expr_pool ("pre_expr nodes", 30);
373 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
375 static pre_expr
376 get_or_alloc_expr_for_name (tree name)
378 struct pre_expr_d expr;
379 pre_expr result;
380 unsigned int result_id;
382 expr.kind = NAME;
383 expr.id = 0;
384 PRE_EXPR_NAME (&expr) = name;
385 result_id = lookup_expression_id (&expr);
386 if (result_id != 0)
387 return expression_for_id (result_id);
389 result = pre_expr_pool.allocate ();
390 result->kind = NAME;
391 PRE_EXPR_NAME (result) = name;
392 alloc_expression_id (result);
393 return result;
396 /* An unordered bitmap set. One bitmap tracks values, the other,
397 expressions. */
398 typedef struct bitmap_set
400 bitmap_head expressions;
401 bitmap_head values;
402 } *bitmap_set_t;
404 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
405 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
407 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
408 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
410 /* Mapping from value id to expressions with that value_id. */
411 static vec<bitmap> value_expressions;
413 /* Sets that we need to keep track of. */
414 typedef struct bb_bitmap_sets
416 /* The EXP_GEN set, which represents expressions/values generated in
417 a basic block. */
418 bitmap_set_t exp_gen;
420 /* The PHI_GEN set, which represents PHI results generated in a
421 basic block. */
422 bitmap_set_t phi_gen;
424 /* The TMP_GEN set, which represents results/temporaries generated
425 in a basic block. IE the LHS of an expression. */
426 bitmap_set_t tmp_gen;
428 /* The AVAIL_OUT set, which represents which values are available in
429 a given basic block. */
430 bitmap_set_t avail_out;
432 /* The ANTIC_IN set, which represents which values are anticipatable
433 in a given basic block. */
434 bitmap_set_t antic_in;
436 /* The PA_IN set, which represents which values are
437 partially anticipatable in a given basic block. */
438 bitmap_set_t pa_in;
440 /* The NEW_SETS set, which is used during insertion to augment the
441 AVAIL_OUT set of blocks with the new insertions performed during
442 the current iteration. */
443 bitmap_set_t new_sets;
445 /* A cache for value_dies_in_block_x. */
446 bitmap expr_dies;
448 /* The live virtual operand on successor edges. */
449 tree vop_on_exit;
451 /* True if we have visited this block during ANTIC calculation. */
452 unsigned int visited : 1;
454 /* True when the block contains a call that might not return. */
455 unsigned int contains_may_not_return_call : 1;
456 } *bb_value_sets_t;
458 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
459 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
460 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
461 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
462 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
463 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
464 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
465 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
466 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
467 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
468 #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
471 /* Basic block list in postorder. */
472 static int *postorder;
473 static int postorder_num;
475 /* This structure is used to keep track of statistics on what
476 optimization PRE was able to perform. */
477 static struct
479 /* The number of RHS computations eliminated by PRE. */
480 int eliminations;
482 /* The number of new expressions/temporaries generated by PRE. */
483 int insertions;
485 /* The number of inserts found due to partial anticipation */
486 int pa_insert;
488 /* The number of new PHI nodes added by PRE. */
489 int phis;
490 } pre_stats;
492 static bool do_partial_partial;
493 static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
494 static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
495 static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
496 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
497 static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
498 static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
499 static void bitmap_insert_into_set_1 (bitmap_set_t, pre_expr,
500 unsigned int, bool);
501 static bitmap_set_t bitmap_set_new (void);
502 static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
503 tree);
504 static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
505 static unsigned int get_expr_value_id (pre_expr);
507 /* We can add and remove elements and entries to and from sets
508 and hash tables, so we use alloc pools for them. */
510 static pool_allocator<bitmap_set> bitmap_set_pool ("Bitmap sets", 30);
511 static bitmap_obstack grand_bitmap_obstack;
513 /* Set of blocks with statements that have had their EH properties changed. */
514 static bitmap need_eh_cleanup;
516 /* Set of blocks with statements that have had their AB properties changed. */
517 static bitmap need_ab_cleanup;
519 /* A three tuple {e, pred, v} used to cache phi translations in the
520 phi_translate_table. */
522 typedef struct expr_pred_trans_d : typed_free_remove<expr_pred_trans_d>
524 /* The expression. */
525 pre_expr e;
527 /* The predecessor block along which we translated the expression. */
528 basic_block pred;
530 /* The value that resulted from the translation. */
531 pre_expr v;
533 /* The hashcode for the expression, pred pair. This is cached for
534 speed reasons. */
535 hashval_t hashcode;
537 /* hash_table support. */
538 typedef expr_pred_trans_d *value_type;
539 typedef expr_pred_trans_d *compare_type;
540 static inline hashval_t hash (const expr_pred_trans_d *);
541 static inline int equal (const expr_pred_trans_d *, const expr_pred_trans_d *);
542 } *expr_pred_trans_t;
543 typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
545 inline hashval_t
546 expr_pred_trans_d::hash (const expr_pred_trans_d *e)
548 return e->hashcode;
551 inline int
552 expr_pred_trans_d::equal (const expr_pred_trans_d *ve1,
553 const expr_pred_trans_d *ve2)
555 basic_block b1 = ve1->pred;
556 basic_block b2 = ve2->pred;
558 /* If they are not translations for the same basic block, they can't
559 be equal. */
560 if (b1 != b2)
561 return false;
562 return pre_expr_d::equal (ve1->e, ve2->e);
565 /* The phi_translate_table caches phi translations for a given
566 expression and predecessor. */
567 static hash_table<expr_pred_trans_d> *phi_translate_table;
569 /* Add the tuple mapping from {expression E, basic block PRED} to
570 the phi translation table and return whether it pre-existed. */
572 static inline bool
573 phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
575 expr_pred_trans_t *slot;
576 expr_pred_trans_d tem;
577 hashval_t hash = iterative_hash_hashval_t (pre_expr_d::hash (e),
578 pred->index);
579 tem.e = e;
580 tem.pred = pred;
581 tem.hashcode = hash;
582 slot = phi_translate_table->find_slot_with_hash (&tem, hash, INSERT);
583 if (*slot)
585 *entry = *slot;
586 return true;
589 *entry = *slot = XNEW (struct expr_pred_trans_d);
590 (*entry)->e = e;
591 (*entry)->pred = pred;
592 (*entry)->hashcode = hash;
593 return false;
597 /* Add expression E to the expression set of value id V. */
599 static void
600 add_to_value (unsigned int v, pre_expr e)
602 bitmap set;
604 gcc_checking_assert (get_expr_value_id (e) == v);
606 if (v >= value_expressions.length ())
608 value_expressions.safe_grow_cleared (v + 1);
611 set = value_expressions[v];
612 if (!set)
614 set = BITMAP_ALLOC (&grand_bitmap_obstack);
615 value_expressions[v] = set;
618 bitmap_set_bit (set, get_or_alloc_expression_id (e));
621 /* Create a new bitmap set and return it. */
623 static bitmap_set_t
624 bitmap_set_new (void)
626 bitmap_set_t ret = bitmap_set_pool.allocate ();
627 bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
628 bitmap_initialize (&ret->values, &grand_bitmap_obstack);
629 return ret;
632 /* Return the value id for a PRE expression EXPR. */
634 static unsigned int
635 get_expr_value_id (pre_expr expr)
637 unsigned int id;
638 switch (expr->kind)
640 case CONSTANT:
641 id = get_constant_value_id (PRE_EXPR_CONSTANT (expr));
642 break;
643 case NAME:
644 id = VN_INFO (PRE_EXPR_NAME (expr))->value_id;
645 break;
646 case NARY:
647 id = PRE_EXPR_NARY (expr)->value_id;
648 break;
649 case REFERENCE:
650 id = PRE_EXPR_REFERENCE (expr)->value_id;
651 break;
652 default:
653 gcc_unreachable ();
655 /* ??? We cannot assert that expr has a value-id (it can be 0), because
656 we assign value-ids only to expressions that have a result
657 in set_hashtable_value_ids. */
658 return id;
661 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
663 static tree
664 sccvn_valnum_from_value_id (unsigned int val)
666 bitmap_iterator bi;
667 unsigned int i;
668 bitmap exprset = value_expressions[val];
669 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
671 pre_expr vexpr = expression_for_id (i);
672 if (vexpr->kind == NAME)
673 return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
674 else if (vexpr->kind == CONSTANT)
675 return PRE_EXPR_CONSTANT (vexpr);
677 return NULL_TREE;
680 /* Remove an expression EXPR from a bitmapped set. */
682 static void
683 bitmap_remove_from_set (bitmap_set_t set, pre_expr expr)
685 unsigned int val = get_expr_value_id (expr);
686 if (!value_id_constant_p (val))
688 bitmap_clear_bit (&set->values, val);
689 bitmap_clear_bit (&set->expressions, get_expression_id (expr));
693 static void
694 bitmap_insert_into_set_1 (bitmap_set_t set, pre_expr expr,
695 unsigned int val, bool allow_constants)
697 if (allow_constants || !value_id_constant_p (val))
699 /* We specifically expect this and only this function to be able to
700 insert constants into a set. */
701 bitmap_set_bit (&set->values, val);
702 bitmap_set_bit (&set->expressions, get_or_alloc_expression_id (expr));
706 /* Insert an expression EXPR into a bitmapped set. */
708 static void
709 bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
711 bitmap_insert_into_set_1 (set, expr, get_expr_value_id (expr), false);
714 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
716 static void
717 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
719 bitmap_copy (&dest->expressions, &orig->expressions);
720 bitmap_copy (&dest->values, &orig->values);
724 /* Free memory used up by SET. */
725 static void
726 bitmap_set_free (bitmap_set_t set)
728 bitmap_clear (&set->expressions);
729 bitmap_clear (&set->values);
733 /* Generate an topological-ordered array of bitmap set SET. */
735 static vec<pre_expr>
736 sorted_array_from_bitmap_set (bitmap_set_t set)
738 unsigned int i, j;
739 bitmap_iterator bi, bj;
740 vec<pre_expr> result;
742 /* Pre-allocate enough space for the array. */
743 result.create (bitmap_count_bits (&set->expressions));
745 FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
747 /* The number of expressions having a given value is usually
748 relatively small. Thus, rather than making a vector of all
749 the expressions and sorting it by value-id, we walk the values
750 and check in the reverse mapping that tells us what expressions
751 have a given value, to filter those in our set. As a result,
752 the expressions are inserted in value-id order, which means
753 topological order.
755 If this is somehow a significant lose for some cases, we can
756 choose which set to walk based on the set size. */
757 bitmap exprset = value_expressions[i];
758 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj)
760 if (bitmap_bit_p (&set->expressions, j))
761 result.quick_push (expression_for_id (j));
765 return result;
768 /* Perform bitmapped set operation DEST &= ORIG. */
770 static void
771 bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig)
773 bitmap_iterator bi;
774 unsigned int i;
776 if (dest != orig)
778 bitmap_head temp;
779 bitmap_initialize (&temp, &grand_bitmap_obstack);
781 bitmap_and_into (&dest->values, &orig->values);
782 bitmap_copy (&temp, &dest->expressions);
783 EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
785 pre_expr expr = expression_for_id (i);
786 unsigned int value_id = get_expr_value_id (expr);
787 if (!bitmap_bit_p (&dest->values, value_id))
788 bitmap_clear_bit (&dest->expressions, i);
790 bitmap_clear (&temp);
794 /* Subtract all values and expressions contained in ORIG from DEST. */
796 static bitmap_set_t
797 bitmap_set_subtract (bitmap_set_t dest, bitmap_set_t orig)
799 bitmap_set_t result = bitmap_set_new ();
800 bitmap_iterator bi;
801 unsigned int i;
803 bitmap_and_compl (&result->expressions, &dest->expressions,
804 &orig->expressions);
806 FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
808 pre_expr expr = expression_for_id (i);
809 unsigned int value_id = get_expr_value_id (expr);
810 bitmap_set_bit (&result->values, value_id);
813 return result;
816 /* Subtract all the values in bitmap set B from bitmap set A. */
818 static void
819 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
821 unsigned int i;
822 bitmap_iterator bi;
823 bitmap_head temp;
825 bitmap_initialize (&temp, &grand_bitmap_obstack);
827 bitmap_copy (&temp, &a->expressions);
828 EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
830 pre_expr expr = expression_for_id (i);
831 if (bitmap_set_contains_value (b, get_expr_value_id (expr)))
832 bitmap_remove_from_set (a, expr);
834 bitmap_clear (&temp);
838 /* Return true if bitmapped set SET contains the value VALUE_ID. */
840 static bool
841 bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
843 if (value_id_constant_p (value_id))
844 return true;
846 if (!set || bitmap_empty_p (&set->expressions))
847 return false;
849 return bitmap_bit_p (&set->values, value_id);
852 static inline bool
853 bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr)
855 return bitmap_bit_p (&set->expressions, get_expression_id (expr));
858 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
860 static void
861 bitmap_set_replace_value (bitmap_set_t set, unsigned int lookfor,
862 const pre_expr expr)
864 bitmap exprset;
865 unsigned int i;
866 bitmap_iterator bi;
868 if (value_id_constant_p (lookfor))
869 return;
871 if (!bitmap_set_contains_value (set, lookfor))
872 return;
874 /* The number of expressions having a given value is usually
875 significantly less than the total number of expressions in SET.
876 Thus, rather than check, for each expression in SET, whether it
877 has the value LOOKFOR, we walk the reverse mapping that tells us
878 what expressions have a given value, and see if any of those
879 expressions are in our set. For large testcases, this is about
880 5-10x faster than walking the bitmap. If this is somehow a
881 significant lose for some cases, we can choose which set to walk
882 based on the set size. */
883 exprset = value_expressions[lookfor];
884 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
886 if (bitmap_clear_bit (&set->expressions, i))
888 bitmap_set_bit (&set->expressions, get_expression_id (expr));
889 return;
893 gcc_unreachable ();
896 /* Return true if two bitmap sets are equal. */
898 static bool
899 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
901 return bitmap_equal_p (&a->values, &b->values);
904 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
905 and add it otherwise. */
907 static void
908 bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
910 unsigned int val = get_expr_value_id (expr);
912 if (bitmap_set_contains_value (set, val))
913 bitmap_set_replace_value (set, val, expr);
914 else
915 bitmap_insert_into_set (set, expr);
918 /* Insert EXPR into SET if EXPR's value is not already present in
919 SET. */
921 static void
922 bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
924 unsigned int val = get_expr_value_id (expr);
926 gcc_checking_assert (expr->id == get_or_alloc_expression_id (expr));
928 /* Constant values are always considered to be part of the set. */
929 if (value_id_constant_p (val))
930 return;
932 /* If the value membership changed, add the expression. */
933 if (bitmap_set_bit (&set->values, val))
934 bitmap_set_bit (&set->expressions, expr->id);
937 /* Print out EXPR to outfile. */
939 static void
940 print_pre_expr (FILE *outfile, const pre_expr expr)
942 switch (expr->kind)
944 case CONSTANT:
945 print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr), 0);
946 break;
947 case NAME:
948 print_generic_expr (outfile, PRE_EXPR_NAME (expr), 0);
949 break;
950 case NARY:
952 unsigned int i;
953 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
954 fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
955 for (i = 0; i < nary->length; i++)
957 print_generic_expr (outfile, nary->op[i], 0);
958 if (i != (unsigned) nary->length - 1)
959 fprintf (outfile, ",");
961 fprintf (outfile, "}");
963 break;
965 case REFERENCE:
967 vn_reference_op_t vro;
968 unsigned int i;
969 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
970 fprintf (outfile, "{");
971 for (i = 0;
972 ref->operands.iterate (i, &vro);
973 i++)
975 bool closebrace = false;
976 if (vro->opcode != SSA_NAME
977 && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
979 fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
980 if (vro->op0)
982 fprintf (outfile, "<");
983 closebrace = true;
986 if (vro->op0)
988 print_generic_expr (outfile, vro->op0, 0);
989 if (vro->op1)
991 fprintf (outfile, ",");
992 print_generic_expr (outfile, vro->op1, 0);
994 if (vro->op2)
996 fprintf (outfile, ",");
997 print_generic_expr (outfile, vro->op2, 0);
1000 if (closebrace)
1001 fprintf (outfile, ">");
1002 if (i != ref->operands.length () - 1)
1003 fprintf (outfile, ",");
1005 fprintf (outfile, "}");
1006 if (ref->vuse)
1008 fprintf (outfile, "@");
1009 print_generic_expr (outfile, ref->vuse, 0);
1012 break;
1015 void debug_pre_expr (pre_expr);
1017 /* Like print_pre_expr but always prints to stderr. */
1018 DEBUG_FUNCTION void
1019 debug_pre_expr (pre_expr e)
1021 print_pre_expr (stderr, e);
1022 fprintf (stderr, "\n");
1025 /* Print out SET to OUTFILE. */
1027 static void
1028 print_bitmap_set (FILE *outfile, bitmap_set_t set,
1029 const char *setname, int blockindex)
1031 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
1032 if (set)
1034 bool first = true;
1035 unsigned i;
1036 bitmap_iterator bi;
1038 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1040 const pre_expr expr = expression_for_id (i);
1042 if (!first)
1043 fprintf (outfile, ", ");
1044 first = false;
1045 print_pre_expr (outfile, expr);
1047 fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1050 fprintf (outfile, " }\n");
1053 void debug_bitmap_set (bitmap_set_t);
1055 DEBUG_FUNCTION void
1056 debug_bitmap_set (bitmap_set_t set)
1058 print_bitmap_set (stderr, set, "debug", 0);
1061 void debug_bitmap_sets_for (basic_block);
1063 DEBUG_FUNCTION void
1064 debug_bitmap_sets_for (basic_block bb)
1066 print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1067 print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1068 print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1069 print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1070 print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1071 if (do_partial_partial)
1072 print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1073 print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1076 /* Print out the expressions that have VAL to OUTFILE. */
1078 static void
1079 print_value_expressions (FILE *outfile, unsigned int val)
1081 bitmap set = value_expressions[val];
1082 if (set)
1084 bitmap_set x;
1085 char s[10];
1086 sprintf (s, "%04d", val);
1087 x.expressions = *set;
1088 print_bitmap_set (outfile, &x, s, 0);
1093 DEBUG_FUNCTION void
1094 debug_value_expressions (unsigned int val)
1096 print_value_expressions (stderr, val);
1099 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1100 represent it. */
1102 static pre_expr
1103 get_or_alloc_expr_for_constant (tree constant)
1105 unsigned int result_id;
1106 unsigned int value_id;
1107 struct pre_expr_d expr;
1108 pre_expr newexpr;
1110 expr.kind = CONSTANT;
1111 PRE_EXPR_CONSTANT (&expr) = constant;
1112 result_id = lookup_expression_id (&expr);
1113 if (result_id != 0)
1114 return expression_for_id (result_id);
1116 newexpr = pre_expr_pool.allocate ();
1117 newexpr->kind = CONSTANT;
1118 PRE_EXPR_CONSTANT (newexpr) = constant;
1119 alloc_expression_id (newexpr);
1120 value_id = get_or_alloc_constant_value_id (constant);
1121 add_to_value (value_id, newexpr);
1122 return newexpr;
1125 /* Given a value id V, find the actual tree representing the constant
1126 value if there is one, and return it. Return NULL if we can't find
1127 a constant. */
1129 static tree
1130 get_constant_for_value_id (unsigned int v)
1132 if (value_id_constant_p (v))
1134 unsigned int i;
1135 bitmap_iterator bi;
1136 bitmap exprset = value_expressions[v];
1138 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1140 pre_expr expr = expression_for_id (i);
1141 if (expr->kind == CONSTANT)
1142 return PRE_EXPR_CONSTANT (expr);
1145 return NULL;
1148 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1149 Currently only supports constants and SSA_NAMES. */
1150 static pre_expr
1151 get_or_alloc_expr_for (tree t)
1153 if (TREE_CODE (t) == SSA_NAME)
1154 return get_or_alloc_expr_for_name (t);
1155 else if (is_gimple_min_invariant (t))
1156 return get_or_alloc_expr_for_constant (t);
1157 else
1159 /* More complex expressions can result from SCCVN expression
1160 simplification that inserts values for them. As they all
1161 do not have VOPs the get handled by the nary ops struct. */
1162 vn_nary_op_t result;
1163 unsigned int result_id;
1164 vn_nary_op_lookup (t, &result);
1165 if (result != NULL)
1167 pre_expr e = pre_expr_pool.allocate ();
1168 e->kind = NARY;
1169 PRE_EXPR_NARY (e) = result;
1170 result_id = lookup_expression_id (e);
1171 if (result_id != 0)
1173 pre_expr_pool.remove (e);
1174 e = expression_for_id (result_id);
1175 return e;
1177 alloc_expression_id (e);
1178 return e;
1181 return NULL;
1184 /* Return the folded version of T if T, when folded, is a gimple
1185 min_invariant. Otherwise, return T. */
1187 static pre_expr
1188 fully_constant_expression (pre_expr e)
1190 switch (e->kind)
1192 case CONSTANT:
1193 return e;
1194 case NARY:
1196 vn_nary_op_t nary = PRE_EXPR_NARY (e);
1197 switch (TREE_CODE_CLASS (nary->opcode))
1199 case tcc_binary:
1200 case tcc_comparison:
1202 /* We have to go from trees to pre exprs to value ids to
1203 constants. */
1204 tree naryop0 = nary->op[0];
1205 tree naryop1 = nary->op[1];
1206 tree result;
1207 if (!is_gimple_min_invariant (naryop0))
1209 pre_expr rep0 = get_or_alloc_expr_for (naryop0);
1210 unsigned int vrep0 = get_expr_value_id (rep0);
1211 tree const0 = get_constant_for_value_id (vrep0);
1212 if (const0)
1213 naryop0 = fold_convert (TREE_TYPE (naryop0), const0);
1215 if (!is_gimple_min_invariant (naryop1))
1217 pre_expr rep1 = get_or_alloc_expr_for (naryop1);
1218 unsigned int vrep1 = get_expr_value_id (rep1);
1219 tree const1 = get_constant_for_value_id (vrep1);
1220 if (const1)
1221 naryop1 = fold_convert (TREE_TYPE (naryop1), const1);
1223 result = fold_binary (nary->opcode, nary->type,
1224 naryop0, naryop1);
1225 if (result && is_gimple_min_invariant (result))
1226 return get_or_alloc_expr_for_constant (result);
1227 /* We might have simplified the expression to a
1228 SSA_NAME for example from x_1 * 1. But we cannot
1229 insert a PHI for x_1 unconditionally as x_1 might
1230 not be available readily. */
1231 return e;
1233 case tcc_reference:
1234 if (nary->opcode != REALPART_EXPR
1235 && nary->opcode != IMAGPART_EXPR
1236 && nary->opcode != VIEW_CONVERT_EXPR)
1237 return e;
1238 /* Fallthrough. */
1239 case tcc_unary:
1241 /* We have to go from trees to pre exprs to value ids to
1242 constants. */
1243 tree naryop0 = nary->op[0];
1244 tree const0, result;
1245 if (is_gimple_min_invariant (naryop0))
1246 const0 = naryop0;
1247 else
1249 pre_expr rep0 = get_or_alloc_expr_for (naryop0);
1250 unsigned int vrep0 = get_expr_value_id (rep0);
1251 const0 = get_constant_for_value_id (vrep0);
1253 result = NULL;
1254 if (const0)
1256 tree type1 = TREE_TYPE (nary->op[0]);
1257 const0 = fold_convert (type1, const0);
1258 result = fold_unary (nary->opcode, nary->type, const0);
1260 if (result && is_gimple_min_invariant (result))
1261 return get_or_alloc_expr_for_constant (result);
1262 return e;
1264 default:
1265 return e;
1268 case REFERENCE:
1270 vn_reference_t ref = PRE_EXPR_REFERENCE (e);
1271 tree folded;
1272 if ((folded = fully_constant_vn_reference_p (ref)))
1273 return get_or_alloc_expr_for_constant (folded);
1274 return e;
1276 default:
1277 return e;
1279 return e;
1282 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1283 it has the value it would have in BLOCK. Set *SAME_VALID to true
1284 in case the new vuse doesn't change the value id of the OPERANDS. */
1286 static tree
1287 translate_vuse_through_block (vec<vn_reference_op_s> operands,
1288 alias_set_type set, tree type, tree vuse,
1289 basic_block phiblock,
1290 basic_block block, bool *same_valid)
1292 gimple phi = SSA_NAME_DEF_STMT (vuse);
1293 ao_ref ref;
1294 edge e = NULL;
1295 bool use_oracle;
1297 *same_valid = true;
1299 if (gimple_bb (phi) != phiblock)
1300 return vuse;
1302 use_oracle = ao_ref_init_from_vn_reference (&ref, set, type, operands);
1304 /* Use the alias-oracle to find either the PHI node in this block,
1305 the first VUSE used in this block that is equivalent to vuse or
1306 the first VUSE which definition in this block kills the value. */
1307 if (gimple_code (phi) == GIMPLE_PHI)
1308 e = find_edge (block, phiblock);
1309 else if (use_oracle)
1310 while (!stmt_may_clobber_ref_p_1 (phi, &ref))
1312 vuse = gimple_vuse (phi);
1313 phi = SSA_NAME_DEF_STMT (vuse);
1314 if (gimple_bb (phi) != phiblock)
1315 return vuse;
1316 if (gimple_code (phi) == GIMPLE_PHI)
1318 e = find_edge (block, phiblock);
1319 break;
1322 else
1323 return NULL_TREE;
1325 if (e)
1327 if (use_oracle)
1329 bitmap visited = NULL;
1330 unsigned int cnt;
1331 /* Try to find a vuse that dominates this phi node by skipping
1332 non-clobbering statements. */
1333 vuse = get_continuation_for_phi (phi, &ref, &cnt, &visited, false,
1334 NULL, NULL);
1335 if (visited)
1336 BITMAP_FREE (visited);
1338 else
1339 vuse = NULL_TREE;
1340 if (!vuse)
1342 /* If we didn't find any, the value ID can't stay the same,
1343 but return the translated vuse. */
1344 *same_valid = false;
1345 vuse = PHI_ARG_DEF (phi, e->dest_idx);
1347 /* ??? We would like to return vuse here as this is the canonical
1348 upmost vdef that this reference is associated with. But during
1349 insertion of the references into the hash tables we only ever
1350 directly insert with their direct gimple_vuse, hence returning
1351 something else would make us not find the other expression. */
1352 return PHI_ARG_DEF (phi, e->dest_idx);
1355 return NULL_TREE;
1358 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1359 SET2. This is used to avoid making a set consisting of the union
1360 of PA_IN and ANTIC_IN during insert. */
1362 static inline pre_expr
1363 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2)
1365 pre_expr result;
1367 result = bitmap_find_leader (set1, val);
1368 if (!result && set2)
1369 result = bitmap_find_leader (set2, val);
1370 return result;
1373 /* Get the tree type for our PRE expression e. */
1375 static tree
1376 get_expr_type (const pre_expr e)
1378 switch (e->kind)
1380 case NAME:
1381 return TREE_TYPE (PRE_EXPR_NAME (e));
1382 case CONSTANT:
1383 return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1384 case REFERENCE:
1385 return PRE_EXPR_REFERENCE (e)->type;
1386 case NARY:
1387 return PRE_EXPR_NARY (e)->type;
1389 gcc_unreachable ();
1392 /* Get a representative SSA_NAME for a given expression.
1393 Since all of our sub-expressions are treated as values, we require
1394 them to be SSA_NAME's for simplicity.
1395 Prior versions of GVNPRE used to use "value handles" here, so that
1396 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1397 either case, the operands are really values (IE we do not expect
1398 them to be usable without finding leaders). */
1400 static tree
1401 get_representative_for (const pre_expr e)
1403 tree name;
1404 unsigned int value_id = get_expr_value_id (e);
1406 switch (e->kind)
1408 case NAME:
1409 return PRE_EXPR_NAME (e);
1410 case CONSTANT:
1411 return PRE_EXPR_CONSTANT (e);
1412 case NARY:
1413 case REFERENCE:
1415 /* Go through all of the expressions representing this value
1416 and pick out an SSA_NAME. */
1417 unsigned int i;
1418 bitmap_iterator bi;
1419 bitmap exprs = value_expressions[value_id];
1420 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1422 pre_expr rep = expression_for_id (i);
1423 if (rep->kind == NAME)
1424 return PRE_EXPR_NAME (rep);
1425 else if (rep->kind == CONSTANT)
1426 return PRE_EXPR_CONSTANT (rep);
1429 break;
1432 /* If we reached here we couldn't find an SSA_NAME. This can
1433 happen when we've discovered a value that has never appeared in
1434 the program as set to an SSA_NAME, as the result of phi translation.
1435 Create one here.
1436 ??? We should be able to re-use this when we insert the statement
1437 to compute it. */
1438 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1439 VN_INFO_GET (name)->value_id = value_id;
1440 VN_INFO (name)->valnum = name;
1441 /* ??? For now mark this SSA name for release by SCCVN. */
1442 VN_INFO (name)->needs_insertion = true;
1443 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1444 if (dump_file && (dump_flags & TDF_DETAILS))
1446 fprintf (dump_file, "Created SSA_NAME representative ");
1447 print_generic_expr (dump_file, name, 0);
1448 fprintf (dump_file, " for expression:");
1449 print_pre_expr (dump_file, e);
1450 fprintf (dump_file, " (%04d)\n", value_id);
1453 return name;
1458 static pre_expr
1459 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1460 basic_block pred, basic_block phiblock);
1462 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1463 the phis in PRED. Return NULL if we can't find a leader for each part
1464 of the translated expression. */
1466 static pre_expr
1467 phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1468 basic_block pred, basic_block phiblock)
1470 switch (expr->kind)
1472 case NARY:
1474 unsigned int i;
1475 bool changed = false;
1476 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1477 vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1478 sizeof_vn_nary_op (nary->length));
1479 memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1481 for (i = 0; i < newnary->length; i++)
1483 if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1484 continue;
1485 else
1487 pre_expr leader, result;
1488 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1489 leader = find_leader_in_sets (op_val_id, set1, set2);
1490 result = phi_translate (leader, set1, set2, pred, phiblock);
1491 if (result && result != leader)
1493 tree name = get_representative_for (result);
1494 if (!name)
1495 return NULL;
1496 newnary->op[i] = name;
1498 else if (!result)
1499 return NULL;
1501 changed |= newnary->op[i] != nary->op[i];
1504 if (changed)
1506 pre_expr constant;
1507 unsigned int new_val_id;
1509 tree result = vn_nary_op_lookup_pieces (newnary->length,
1510 newnary->opcode,
1511 newnary->type,
1512 &newnary->op[0],
1513 &nary);
1514 if (result && is_gimple_min_invariant (result))
1515 return get_or_alloc_expr_for_constant (result);
1517 expr = pre_expr_pool.allocate ();
1518 expr->kind = NARY;
1519 expr->id = 0;
1520 if (nary)
1522 PRE_EXPR_NARY (expr) = nary;
1523 constant = fully_constant_expression (expr);
1524 if (constant != expr)
1525 return constant;
1527 new_val_id = nary->value_id;
1528 get_or_alloc_expression_id (expr);
1530 else
1532 new_val_id = get_next_value_id ();
1533 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
1534 nary = vn_nary_op_insert_pieces (newnary->length,
1535 newnary->opcode,
1536 newnary->type,
1537 &newnary->op[0],
1538 result, new_val_id);
1539 PRE_EXPR_NARY (expr) = nary;
1540 constant = fully_constant_expression (expr);
1541 if (constant != expr)
1542 return constant;
1543 get_or_alloc_expression_id (expr);
1545 add_to_value (new_val_id, expr);
1547 return expr;
1549 break;
1551 case REFERENCE:
1553 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1554 vec<vn_reference_op_s> operands = ref->operands;
1555 tree vuse = ref->vuse;
1556 tree newvuse = vuse;
1557 vec<vn_reference_op_s> newoperands = vNULL;
1558 bool changed = false, same_valid = true;
1559 unsigned int i, n;
1560 vn_reference_op_t operand;
1561 vn_reference_t newref;
1563 for (i = 0; operands.iterate (i, &operand); i++)
1565 pre_expr opresult;
1566 pre_expr leader;
1567 tree op[3];
1568 tree type = operand->type;
1569 vn_reference_op_s newop = *operand;
1570 op[0] = operand->op0;
1571 op[1] = operand->op1;
1572 op[2] = operand->op2;
1573 for (n = 0; n < 3; ++n)
1575 unsigned int op_val_id;
1576 if (!op[n])
1577 continue;
1578 if (TREE_CODE (op[n]) != SSA_NAME)
1580 /* We can't possibly insert these. */
1581 if (n != 0
1582 && !is_gimple_min_invariant (op[n]))
1583 break;
1584 continue;
1586 op_val_id = VN_INFO (op[n])->value_id;
1587 leader = find_leader_in_sets (op_val_id, set1, set2);
1588 if (!leader)
1589 break;
1590 opresult = phi_translate (leader, set1, set2, pred, phiblock);
1591 if (!opresult)
1592 break;
1593 if (opresult != leader)
1595 tree name = get_representative_for (opresult);
1596 if (!name)
1597 break;
1598 changed |= name != op[n];
1599 op[n] = name;
1602 if (n != 3)
1604 newoperands.release ();
1605 return NULL;
1607 if (!changed)
1608 continue;
1609 if (!newoperands.exists ())
1610 newoperands = operands.copy ();
1611 /* We may have changed from an SSA_NAME to a constant */
1612 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1613 newop.opcode = TREE_CODE (op[0]);
1614 newop.type = type;
1615 newop.op0 = op[0];
1616 newop.op1 = op[1];
1617 newop.op2 = op[2];
1618 newoperands[i] = newop;
1620 gcc_checking_assert (i == operands.length ());
1622 if (vuse)
1624 newvuse = translate_vuse_through_block (newoperands.exists ()
1625 ? newoperands : operands,
1626 ref->set, ref->type,
1627 vuse, phiblock, pred,
1628 &same_valid);
1629 if (newvuse == NULL_TREE)
1631 newoperands.release ();
1632 return NULL;
1636 if (changed || newvuse != vuse)
1638 unsigned int new_val_id;
1639 pre_expr constant;
1641 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1642 ref->type,
1643 newoperands.exists ()
1644 ? newoperands : operands,
1645 &newref, VN_WALK);
1646 if (result)
1647 newoperands.release ();
1649 /* We can always insert constants, so if we have a partial
1650 redundant constant load of another type try to translate it
1651 to a constant of appropriate type. */
1652 if (result && is_gimple_min_invariant (result))
1654 tree tem = result;
1655 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1657 tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1658 if (tem && !is_gimple_min_invariant (tem))
1659 tem = NULL_TREE;
1661 if (tem)
1662 return get_or_alloc_expr_for_constant (tem);
1665 /* If we'd have to convert things we would need to validate
1666 if we can insert the translated expression. So fail
1667 here for now - we cannot insert an alias with a different
1668 type in the VN tables either, as that would assert. */
1669 if (result
1670 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1671 return NULL;
1672 else if (!result && newref
1673 && !useless_type_conversion_p (ref->type, newref->type))
1675 newoperands.release ();
1676 return NULL;
1679 expr = pre_expr_pool.allocate ();
1680 expr->kind = REFERENCE;
1681 expr->id = 0;
1683 if (newref)
1685 PRE_EXPR_REFERENCE (expr) = newref;
1686 constant = fully_constant_expression (expr);
1687 if (constant != expr)
1688 return constant;
1690 new_val_id = newref->value_id;
1691 get_or_alloc_expression_id (expr);
1693 else
1695 if (changed || !same_valid)
1697 new_val_id = get_next_value_id ();
1698 value_expressions.safe_grow_cleared
1699 (get_max_value_id () + 1);
1701 else
1702 new_val_id = ref->value_id;
1703 if (!newoperands.exists ())
1704 newoperands = operands.copy ();
1705 newref = vn_reference_insert_pieces (newvuse, ref->set,
1706 ref->type,
1707 newoperands,
1708 result, new_val_id);
1709 newoperands = vNULL;
1710 PRE_EXPR_REFERENCE (expr) = newref;
1711 constant = fully_constant_expression (expr);
1712 if (constant != expr)
1713 return constant;
1714 get_or_alloc_expression_id (expr);
1716 add_to_value (new_val_id, expr);
1718 newoperands.release ();
1719 return expr;
1721 break;
1723 case NAME:
1725 tree name = PRE_EXPR_NAME (expr);
1726 gimple def_stmt = SSA_NAME_DEF_STMT (name);
1727 /* If the SSA name is defined by a PHI node in this block,
1728 translate it. */
1729 if (gimple_code (def_stmt) == GIMPLE_PHI
1730 && gimple_bb (def_stmt) == phiblock)
1732 edge e = find_edge (pred, gimple_bb (def_stmt));
1733 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1735 /* Handle constant. */
1736 if (is_gimple_min_invariant (def))
1737 return get_or_alloc_expr_for_constant (def);
1739 return get_or_alloc_expr_for_name (def);
1741 /* Otherwise return it unchanged - it will get removed if its
1742 value is not available in PREDs AVAIL_OUT set of expressions
1743 by the subtraction of TMP_GEN. */
1744 return expr;
1747 default:
1748 gcc_unreachable ();
1752 /* Wrapper around phi_translate_1 providing caching functionality. */
1754 static pre_expr
1755 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1756 basic_block pred, basic_block phiblock)
1758 expr_pred_trans_t slot = NULL;
1759 pre_expr phitrans;
1761 if (!expr)
1762 return NULL;
1764 /* Constants contain no values that need translation. */
1765 if (expr->kind == CONSTANT)
1766 return expr;
1768 if (value_id_constant_p (get_expr_value_id (expr)))
1769 return expr;
1771 /* Don't add translations of NAMEs as those are cheap to translate. */
1772 if (expr->kind != NAME)
1774 if (phi_trans_add (&slot, expr, pred))
1775 return slot->v;
1776 /* Store NULL for the value we want to return in the case of
1777 recursing. */
1778 slot->v = NULL;
1781 /* Translate. */
1782 phitrans = phi_translate_1 (expr, set1, set2, pred, phiblock);
1784 if (slot)
1786 if (phitrans)
1787 slot->v = phitrans;
1788 else
1789 /* Remove failed translations again, they cause insert
1790 iteration to not pick up new opportunities reliably. */
1791 phi_translate_table->remove_elt_with_hash (slot, slot->hashcode);
1794 return phitrans;
1798 /* For each expression in SET, translate the values through phi nodes
1799 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1800 expressions in DEST. */
1802 static void
1803 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
1804 basic_block phiblock)
1806 vec<pre_expr> exprs;
1807 pre_expr expr;
1808 int i;
1810 if (gimple_seq_empty_p (phi_nodes (phiblock)))
1812 bitmap_set_copy (dest, set);
1813 return;
1816 exprs = sorted_array_from_bitmap_set (set);
1817 FOR_EACH_VEC_ELT (exprs, i, expr)
1819 pre_expr translated;
1820 translated = phi_translate (expr, set, NULL, pred, phiblock);
1821 if (!translated)
1822 continue;
1824 /* We might end up with multiple expressions from SET being
1825 translated to the same value. In this case we do not want
1826 to retain the NARY or REFERENCE expression but prefer a NAME
1827 which would be the leader. */
1828 if (translated->kind == NAME)
1829 bitmap_value_replace_in_set (dest, translated);
1830 else
1831 bitmap_value_insert_into_set (dest, translated);
1833 exprs.release ();
1836 /* Find the leader for a value (i.e., the name representing that
1837 value) in a given set, and return it. Return NULL if no leader
1838 is found. */
1840 static pre_expr
1841 bitmap_find_leader (bitmap_set_t set, unsigned int val)
1843 if (value_id_constant_p (val))
1845 unsigned int i;
1846 bitmap_iterator bi;
1847 bitmap exprset = value_expressions[val];
1849 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1851 pre_expr expr = expression_for_id (i);
1852 if (expr->kind == CONSTANT)
1853 return expr;
1856 if (bitmap_set_contains_value (set, val))
1858 /* Rather than walk the entire bitmap of expressions, and see
1859 whether any of them has the value we are looking for, we look
1860 at the reverse mapping, which tells us the set of expressions
1861 that have a given value (IE value->expressions with that
1862 value) and see if any of those expressions are in our set.
1863 The number of expressions per value is usually significantly
1864 less than the number of expressions in the set. In fact, for
1865 large testcases, doing it this way is roughly 5-10x faster
1866 than walking the bitmap.
1867 If this is somehow a significant lose for some cases, we can
1868 choose which set to walk based on which set is smaller. */
1869 unsigned int i;
1870 bitmap_iterator bi;
1871 bitmap exprset = value_expressions[val];
1873 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1874 return expression_for_id (i);
1876 return NULL;
1879 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1880 BLOCK by seeing if it is not killed in the block. Note that we are
1881 only determining whether there is a store that kills it. Because
1882 of the order in which clean iterates over values, we are guaranteed
1883 that altered operands will have caused us to be eliminated from the
1884 ANTIC_IN set already. */
1886 static bool
1887 value_dies_in_block_x (pre_expr expr, basic_block block)
1889 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1890 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1891 gimple def;
1892 gimple_stmt_iterator gsi;
1893 unsigned id = get_expression_id (expr);
1894 bool res = false;
1895 ao_ref ref;
1897 if (!vuse)
1898 return false;
1900 /* Lookup a previously calculated result. */
1901 if (EXPR_DIES (block)
1902 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1903 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1905 /* A memory expression {e, VUSE} dies in the block if there is a
1906 statement that may clobber e. If, starting statement walk from the
1907 top of the basic block, a statement uses VUSE there can be no kill
1908 inbetween that use and the original statement that loaded {e, VUSE},
1909 so we can stop walking. */
1910 ref.base = NULL_TREE;
1911 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1913 tree def_vuse, def_vdef;
1914 def = gsi_stmt (gsi);
1915 def_vuse = gimple_vuse (def);
1916 def_vdef = gimple_vdef (def);
1918 /* Not a memory statement. */
1919 if (!def_vuse)
1920 continue;
1922 /* Not a may-def. */
1923 if (!def_vdef)
1925 /* A load with the same VUSE, we're done. */
1926 if (def_vuse == vuse)
1927 break;
1929 continue;
1932 /* Init ref only if we really need it. */
1933 if (ref.base == NULL_TREE
1934 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type,
1935 refx->operands))
1937 res = true;
1938 break;
1940 /* If the statement may clobber expr, it dies. */
1941 if (stmt_may_clobber_ref_p_1 (def, &ref))
1943 res = true;
1944 break;
1948 /* Remember the result. */
1949 if (!EXPR_DIES (block))
1950 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1951 bitmap_set_bit (EXPR_DIES (block), id * 2);
1952 if (res)
1953 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1955 return res;
1959 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1960 contains its value-id. */
1962 static bool
1963 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1965 if (op && TREE_CODE (op) == SSA_NAME)
1967 unsigned int value_id = VN_INFO (op)->value_id;
1968 if (!(bitmap_set_contains_value (set1, value_id)
1969 || (set2 && bitmap_set_contains_value (set2, value_id))))
1970 return false;
1972 return true;
1975 /* Determine if the expression EXPR is valid in SET1 U SET2.
1976 ONLY SET2 CAN BE NULL.
1977 This means that we have a leader for each part of the expression
1978 (if it consists of values), or the expression is an SSA_NAME.
1979 For loads/calls, we also see if the vuse is killed in this block. */
1981 static bool
1982 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
1984 switch (expr->kind)
1986 case NAME:
1987 /* By construction all NAMEs are available. Non-available
1988 NAMEs are removed by subtracting TMP_GEN from the sets. */
1989 return true;
1990 case NARY:
1992 unsigned int i;
1993 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1994 for (i = 0; i < nary->length; i++)
1995 if (!op_valid_in_sets (set1, set2, nary->op[i]))
1996 return false;
1997 return true;
1999 break;
2000 case REFERENCE:
2002 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2003 vn_reference_op_t vro;
2004 unsigned int i;
2006 FOR_EACH_VEC_ELT (ref->operands, i, vro)
2008 if (!op_valid_in_sets (set1, set2, vro->op0)
2009 || !op_valid_in_sets (set1, set2, vro->op1)
2010 || !op_valid_in_sets (set1, set2, vro->op2))
2011 return false;
2013 return true;
2015 default:
2016 gcc_unreachable ();
2020 /* Clean the set of expressions that are no longer valid in SET1 or
2021 SET2. This means expressions that are made up of values we have no
2022 leaders for in SET1 or SET2. This version is used for partial
2023 anticipation, which means it is not valid in either ANTIC_IN or
2024 PA_IN. */
2026 static void
2027 dependent_clean (bitmap_set_t set1, bitmap_set_t set2)
2029 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
2030 pre_expr expr;
2031 int i;
2033 FOR_EACH_VEC_ELT (exprs, i, expr)
2035 if (!valid_in_sets (set1, set2, expr))
2036 bitmap_remove_from_set (set1, expr);
2038 exprs.release ();
2041 /* Clean the set of expressions that are no longer valid in SET. This
2042 means expressions that are made up of values we have no leaders for
2043 in SET. */
2045 static void
2046 clean (bitmap_set_t set)
2048 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set);
2049 pre_expr expr;
2050 int i;
2052 FOR_EACH_VEC_ELT (exprs, i, expr)
2054 if (!valid_in_sets (set, NULL, expr))
2055 bitmap_remove_from_set (set, expr);
2057 exprs.release ();
2060 /* Clean the set of expressions that are no longer valid in SET because
2061 they are clobbered in BLOCK or because they trap and may not be executed. */
2063 static void
2064 prune_clobbered_mems (bitmap_set_t set, basic_block block)
2066 bitmap_iterator bi;
2067 unsigned i;
2069 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2071 pre_expr expr = expression_for_id (i);
2072 if (expr->kind == REFERENCE)
2074 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2075 if (ref->vuse)
2077 gimple def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2078 if (!gimple_nop_p (def_stmt)
2079 && ((gimple_bb (def_stmt) != block
2080 && !dominated_by_p (CDI_DOMINATORS,
2081 block, gimple_bb (def_stmt)))
2082 || (gimple_bb (def_stmt) == block
2083 && value_dies_in_block_x (expr, block))))
2084 bitmap_remove_from_set (set, expr);
2087 else if (expr->kind == NARY)
2089 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2090 /* If the NARY may trap make sure the block does not contain
2091 a possible exit point.
2092 ??? This is overly conservative if we translate AVAIL_OUT
2093 as the available expression might be after the exit point. */
2094 if (BB_MAY_NOTRETURN (block)
2095 && vn_nary_may_trap (nary))
2096 bitmap_remove_from_set (set, expr);
2101 static sbitmap has_abnormal_preds;
2103 /* List of blocks that may have changed during ANTIC computation and
2104 thus need to be iterated over. */
2106 static sbitmap changed_blocks;
2108 /* Compute the ANTIC set for BLOCK.
2110 If succs(BLOCK) > 1 then
2111 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2112 else if succs(BLOCK) == 1 then
2113 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2115 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2118 static bool
2119 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2121 bool changed = false;
2122 bitmap_set_t S, old, ANTIC_OUT;
2123 bitmap_iterator bi;
2124 unsigned int bii;
2125 edge e;
2126 edge_iterator ei;
2128 old = ANTIC_OUT = S = NULL;
2129 BB_VISITED (block) = 1;
2131 /* If any edges from predecessors are abnormal, antic_in is empty,
2132 so do nothing. */
2133 if (block_has_abnormal_pred_edge)
2134 goto maybe_dump_sets;
2136 old = ANTIC_IN (block);
2137 ANTIC_OUT = bitmap_set_new ();
2139 /* If the block has no successors, ANTIC_OUT is empty. */
2140 if (EDGE_COUNT (block->succs) == 0)
2142 /* If we have one successor, we could have some phi nodes to
2143 translate through. */
2144 else if (single_succ_p (block))
2146 basic_block succ_bb = single_succ (block);
2147 gcc_assert (BB_VISITED (succ_bb));
2148 phi_translate_set (ANTIC_OUT, ANTIC_IN (succ_bb), block, succ_bb);
2150 /* If we have multiple successors, we take the intersection of all of
2151 them. Note that in the case of loop exit phi nodes, we may have
2152 phis to translate through. */
2153 else
2155 size_t i;
2156 basic_block bprime, first = NULL;
2158 auto_vec<basic_block> worklist (EDGE_COUNT (block->succs));
2159 FOR_EACH_EDGE (e, ei, block->succs)
2161 if (!first
2162 && BB_VISITED (e->dest))
2163 first = e->dest;
2164 else if (BB_VISITED (e->dest))
2165 worklist.quick_push (e->dest);
2168 /* Of multiple successors we have to have visited one already
2169 which is guaranteed by iteration order. */
2170 gcc_assert (first != NULL);
2172 phi_translate_set (ANTIC_OUT, ANTIC_IN (first), block, first);
2174 FOR_EACH_VEC_ELT (worklist, i, bprime)
2176 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2178 bitmap_set_t tmp = bitmap_set_new ();
2179 phi_translate_set (tmp, ANTIC_IN (bprime), block, bprime);
2180 bitmap_set_and (ANTIC_OUT, tmp);
2181 bitmap_set_free (tmp);
2183 else
2184 bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime));
2188 /* Prune expressions that are clobbered in block and thus become
2189 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2190 prune_clobbered_mems (ANTIC_OUT, block);
2192 /* Generate ANTIC_OUT - TMP_GEN. */
2193 S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block));
2195 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2196 ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block),
2197 TMP_GEN (block));
2199 /* Then union in the ANTIC_OUT - TMP_GEN values,
2200 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2201 FOR_EACH_EXPR_ID_IN_SET (S, bii, bi)
2202 bitmap_value_insert_into_set (ANTIC_IN (block),
2203 expression_for_id (bii));
2205 clean (ANTIC_IN (block));
2207 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2209 changed = true;
2210 bitmap_set_bit (changed_blocks, block->index);
2211 FOR_EACH_EDGE (e, ei, block->preds)
2212 bitmap_set_bit (changed_blocks, e->src->index);
2214 else
2215 bitmap_clear_bit (changed_blocks, block->index);
2217 maybe_dump_sets:
2218 if (dump_file && (dump_flags & TDF_DETAILS))
2220 if (ANTIC_OUT)
2221 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2223 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2224 block->index);
2226 if (S)
2227 print_bitmap_set (dump_file, S, "S", block->index);
2229 if (old)
2230 bitmap_set_free (old);
2231 if (S)
2232 bitmap_set_free (S);
2233 if (ANTIC_OUT)
2234 bitmap_set_free (ANTIC_OUT);
2235 return changed;
2238 /* Compute PARTIAL_ANTIC for BLOCK.
2240 If succs(BLOCK) > 1 then
2241 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2242 in ANTIC_OUT for all succ(BLOCK)
2243 else if succs(BLOCK) == 1 then
2244 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2246 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2247 - ANTIC_IN[BLOCK])
2250 static bool
2251 compute_partial_antic_aux (basic_block block,
2252 bool block_has_abnormal_pred_edge)
2254 bool changed = false;
2255 bitmap_set_t old_PA_IN;
2256 bitmap_set_t PA_OUT;
2257 edge e;
2258 edge_iterator ei;
2259 unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH);
2261 old_PA_IN = PA_OUT = NULL;
2263 /* If any edges from predecessors are abnormal, antic_in is empty,
2264 so do nothing. */
2265 if (block_has_abnormal_pred_edge)
2266 goto maybe_dump_sets;
2268 /* If there are too many partially anticipatable values in the
2269 block, phi_translate_set can take an exponential time: stop
2270 before the translation starts. */
2271 if (max_pa
2272 && single_succ_p (block)
2273 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2274 goto maybe_dump_sets;
2276 old_PA_IN = PA_IN (block);
2277 PA_OUT = bitmap_set_new ();
2279 /* If the block has no successors, ANTIC_OUT is empty. */
2280 if (EDGE_COUNT (block->succs) == 0)
2282 /* If we have one successor, we could have some phi nodes to
2283 translate through. Note that we can't phi translate across DFS
2284 back edges in partial antic, because it uses a union operation on
2285 the successors. For recurrences like IV's, we will end up
2286 generating a new value in the set on each go around (i + 3 (VH.1)
2287 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2288 else if (single_succ_p (block))
2290 basic_block succ = single_succ (block);
2291 if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK))
2292 phi_translate_set (PA_OUT, PA_IN (succ), block, succ);
2294 /* If we have multiple successors, we take the union of all of
2295 them. */
2296 else
2298 size_t i;
2299 basic_block bprime;
2301 auto_vec<basic_block> worklist (EDGE_COUNT (block->succs));
2302 FOR_EACH_EDGE (e, ei, block->succs)
2304 if (e->flags & EDGE_DFS_BACK)
2305 continue;
2306 worklist.quick_push (e->dest);
2308 if (worklist.length () > 0)
2310 FOR_EACH_VEC_ELT (worklist, i, bprime)
2312 unsigned int i;
2313 bitmap_iterator bi;
2315 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi)
2316 bitmap_value_insert_into_set (PA_OUT,
2317 expression_for_id (i));
2318 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2320 bitmap_set_t pa_in = bitmap_set_new ();
2321 phi_translate_set (pa_in, PA_IN (bprime), block, bprime);
2322 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2323 bitmap_value_insert_into_set (PA_OUT,
2324 expression_for_id (i));
2325 bitmap_set_free (pa_in);
2327 else
2328 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi)
2329 bitmap_value_insert_into_set (PA_OUT,
2330 expression_for_id (i));
2335 /* Prune expressions that are clobbered in block and thus become
2336 invalid if translated from PA_OUT to PA_IN. */
2337 prune_clobbered_mems (PA_OUT, block);
2339 /* PA_IN starts with PA_OUT - TMP_GEN.
2340 Then we subtract things from ANTIC_IN. */
2341 PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block));
2343 /* For partial antic, we want to put back in the phi results, since
2344 we will properly avoid making them partially antic over backedges. */
2345 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2346 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2348 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2349 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2351 dependent_clean (PA_IN (block), ANTIC_IN (block));
2353 if (!bitmap_set_equal (old_PA_IN, PA_IN (block)))
2355 changed = true;
2356 bitmap_set_bit (changed_blocks, block->index);
2357 FOR_EACH_EDGE (e, ei, block->preds)
2358 bitmap_set_bit (changed_blocks, e->src->index);
2360 else
2361 bitmap_clear_bit (changed_blocks, block->index);
2363 maybe_dump_sets:
2364 if (dump_file && (dump_flags & TDF_DETAILS))
2366 if (PA_OUT)
2367 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2369 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2371 if (old_PA_IN)
2372 bitmap_set_free (old_PA_IN);
2373 if (PA_OUT)
2374 bitmap_set_free (PA_OUT);
2375 return changed;
2378 /* Compute ANTIC and partial ANTIC sets. */
2380 static void
2381 compute_antic (void)
2383 bool changed = true;
2384 int num_iterations = 0;
2385 basic_block block;
2386 int i;
2388 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2389 We pre-build the map of blocks with incoming abnormal edges here. */
2390 has_abnormal_preds = sbitmap_alloc (last_basic_block_for_fn (cfun));
2391 bitmap_clear (has_abnormal_preds);
2393 FOR_ALL_BB_FN (block, cfun)
2395 edge_iterator ei;
2396 edge e;
2398 FOR_EACH_EDGE (e, ei, block->preds)
2400 e->flags &= ~EDGE_DFS_BACK;
2401 if (e->flags & EDGE_ABNORMAL)
2403 bitmap_set_bit (has_abnormal_preds, block->index);
2404 break;
2408 BB_VISITED (block) = 0;
2410 /* While we are here, give empty ANTIC_IN sets to each block. */
2411 ANTIC_IN (block) = bitmap_set_new ();
2412 PA_IN (block) = bitmap_set_new ();
2415 /* At the exit block we anticipate nothing. */
2416 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2418 changed_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun) + 1);
2419 bitmap_ones (changed_blocks);
2420 while (changed)
2422 if (dump_file && (dump_flags & TDF_DETAILS))
2423 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2424 /* ??? We need to clear our PHI translation cache here as the
2425 ANTIC sets shrink and we restrict valid translations to
2426 those having operands with leaders in ANTIC. Same below
2427 for PA ANTIC computation. */
2428 num_iterations++;
2429 changed = false;
2430 for (i = postorder_num - 1; i >= 0; i--)
2432 if (bitmap_bit_p (changed_blocks, postorder[i]))
2434 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2435 changed |= compute_antic_aux (block,
2436 bitmap_bit_p (has_abnormal_preds,
2437 block->index));
2440 /* Theoretically possible, but *highly* unlikely. */
2441 gcc_checking_assert (num_iterations < 500);
2444 statistics_histogram_event (cfun, "compute_antic iterations",
2445 num_iterations);
2447 if (do_partial_partial)
2449 bitmap_ones (changed_blocks);
2450 mark_dfs_back_edges ();
2451 num_iterations = 0;
2452 changed = true;
2453 while (changed)
2455 if (dump_file && (dump_flags & TDF_DETAILS))
2456 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2457 num_iterations++;
2458 changed = false;
2459 for (i = postorder_num - 1 ; i >= 0; i--)
2461 if (bitmap_bit_p (changed_blocks, postorder[i]))
2463 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2464 changed
2465 |= compute_partial_antic_aux (block,
2466 bitmap_bit_p (has_abnormal_preds,
2467 block->index));
2470 /* Theoretically possible, but *highly* unlikely. */
2471 gcc_checking_assert (num_iterations < 500);
2473 statistics_histogram_event (cfun, "compute_partial_antic iterations",
2474 num_iterations);
2476 sbitmap_free (has_abnormal_preds);
2477 sbitmap_free (changed_blocks);
2481 /* Inserted expressions are placed onto this worklist, which is used
2482 for performing quick dead code elimination of insertions we made
2483 that didn't turn out to be necessary. */
2484 static bitmap inserted_exprs;
2486 /* The actual worker for create_component_ref_by_pieces. */
2488 static tree
2489 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2490 unsigned int *operand, gimple_seq *stmts)
2492 vn_reference_op_t currop = &ref->operands[*operand];
2493 tree genop;
2494 ++*operand;
2495 switch (currop->opcode)
2497 case CALL_EXPR:
2499 tree folded, sc = NULL_TREE;
2500 unsigned int nargs = 0;
2501 tree fn, *args;
2502 if (TREE_CODE (currop->op0) == FUNCTION_DECL)
2503 fn = currop->op0;
2504 else
2505 fn = find_or_generate_expression (block, currop->op0, stmts);
2506 if (!fn)
2507 return NULL_TREE;
2508 if (currop->op1)
2510 sc = find_or_generate_expression (block, currop->op1, stmts);
2511 if (!sc)
2512 return NULL_TREE;
2514 args = XNEWVEC (tree, ref->operands.length () - 1);
2515 while (*operand < ref->operands.length ())
2517 args[nargs] = create_component_ref_by_pieces_1 (block, ref,
2518 operand, stmts);
2519 if (!args[nargs])
2520 return NULL_TREE;
2521 nargs++;
2523 folded = build_call_array (currop->type,
2524 (TREE_CODE (fn) == FUNCTION_DECL
2525 ? build_fold_addr_expr (fn) : fn),
2526 nargs, args);
2527 if (currop->with_bounds)
2528 CALL_WITH_BOUNDS_P (folded) = true;
2529 free (args);
2530 if (sc)
2531 CALL_EXPR_STATIC_CHAIN (folded) = sc;
2532 return folded;
2535 case MEM_REF:
2537 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2538 stmts);
2539 if (!baseop)
2540 return NULL_TREE;
2541 tree offset = currop->op0;
2542 if (TREE_CODE (baseop) == ADDR_EXPR
2543 && handled_component_p (TREE_OPERAND (baseop, 0)))
2545 HOST_WIDE_INT off;
2546 tree base;
2547 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2548 &off);
2549 gcc_assert (base);
2550 offset = int_const_binop (PLUS_EXPR, offset,
2551 build_int_cst (TREE_TYPE (offset),
2552 off));
2553 baseop = build_fold_addr_expr (base);
2555 return fold_build2 (MEM_REF, currop->type, baseop, offset);
2558 case TARGET_MEM_REF:
2560 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2561 vn_reference_op_t nextop = &ref->operands[++*operand];
2562 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2563 stmts);
2564 if (!baseop)
2565 return NULL_TREE;
2566 if (currop->op0)
2568 genop0 = find_or_generate_expression (block, currop->op0, stmts);
2569 if (!genop0)
2570 return NULL_TREE;
2572 if (nextop->op0)
2574 genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2575 if (!genop1)
2576 return NULL_TREE;
2578 return build5 (TARGET_MEM_REF, currop->type,
2579 baseop, currop->op2, genop0, currop->op1, genop1);
2582 case ADDR_EXPR:
2583 if (currop->op0)
2585 gcc_assert (is_gimple_min_invariant (currop->op0));
2586 return currop->op0;
2588 /* Fallthrough. */
2589 case REALPART_EXPR:
2590 case IMAGPART_EXPR:
2591 case VIEW_CONVERT_EXPR:
2593 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2594 stmts);
2595 if (!genop0)
2596 return NULL_TREE;
2597 return fold_build1 (currop->opcode, currop->type, genop0);
2600 case WITH_SIZE_EXPR:
2602 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2603 stmts);
2604 if (!genop0)
2605 return NULL_TREE;
2606 tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2607 if (!genop1)
2608 return NULL_TREE;
2609 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2612 case BIT_FIELD_REF:
2614 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2615 stmts);
2616 if (!genop0)
2617 return NULL_TREE;
2618 tree op1 = currop->op0;
2619 tree op2 = currop->op1;
2620 return fold_build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2623 /* For array ref vn_reference_op's, operand 1 of the array ref
2624 is op0 of the reference op and operand 3 of the array ref is
2625 op1. */
2626 case ARRAY_RANGE_REF:
2627 case ARRAY_REF:
2629 tree genop0;
2630 tree genop1 = currop->op0;
2631 tree genop2 = currop->op1;
2632 tree genop3 = currop->op2;
2633 genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2634 stmts);
2635 if (!genop0)
2636 return NULL_TREE;
2637 genop1 = find_or_generate_expression (block, genop1, stmts);
2638 if (!genop1)
2639 return NULL_TREE;
2640 if (genop2)
2642 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2643 /* Drop zero minimum index if redundant. */
2644 if (integer_zerop (genop2)
2645 && (!domain_type
2646 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2647 genop2 = NULL_TREE;
2648 else
2650 genop2 = find_or_generate_expression (block, genop2, stmts);
2651 if (!genop2)
2652 return NULL_TREE;
2655 if (genop3)
2657 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2658 /* We can't always put a size in units of the element alignment
2659 here as the element alignment may be not visible. See
2660 PR43783. Simply drop the element size for constant
2661 sizes. */
2662 if (tree_int_cst_equal (genop3, TYPE_SIZE_UNIT (elmt_type)))
2663 genop3 = NULL_TREE;
2664 else
2666 genop3 = size_binop (EXACT_DIV_EXPR, genop3,
2667 size_int (TYPE_ALIGN_UNIT (elmt_type)));
2668 genop3 = find_or_generate_expression (block, genop3, stmts);
2669 if (!genop3)
2670 return NULL_TREE;
2673 return build4 (currop->opcode, currop->type, genop0, genop1,
2674 genop2, genop3);
2676 case COMPONENT_REF:
2678 tree op0;
2679 tree op1;
2680 tree genop2 = currop->op1;
2681 op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2682 if (!op0)
2683 return NULL_TREE;
2684 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2685 op1 = currop->op0;
2686 if (genop2)
2688 genop2 = find_or_generate_expression (block, genop2, stmts);
2689 if (!genop2)
2690 return NULL_TREE;
2692 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2695 case SSA_NAME:
2697 genop = find_or_generate_expression (block, currop->op0, stmts);
2698 return genop;
2700 case STRING_CST:
2701 case INTEGER_CST:
2702 case COMPLEX_CST:
2703 case VECTOR_CST:
2704 case REAL_CST:
2705 case CONSTRUCTOR:
2706 case VAR_DECL:
2707 case PARM_DECL:
2708 case CONST_DECL:
2709 case RESULT_DECL:
2710 case FUNCTION_DECL:
2711 return currop->op0;
2713 default:
2714 gcc_unreachable ();
2718 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2719 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2720 trying to rename aggregates into ssa form directly, which is a no no.
2722 Thus, this routine doesn't create temporaries, it just builds a
2723 single access expression for the array, calling
2724 find_or_generate_expression to build the innermost pieces.
2726 This function is a subroutine of create_expression_by_pieces, and
2727 should not be called on it's own unless you really know what you
2728 are doing. */
2730 static tree
2731 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2732 gimple_seq *stmts)
2734 unsigned int op = 0;
2735 return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2738 /* Find a simple leader for an expression, or generate one using
2739 create_expression_by_pieces from a NARY expression for the value.
2740 BLOCK is the basic_block we are looking for leaders in.
2741 OP is the tree expression to find a leader for or generate.
2742 Returns the leader or NULL_TREE on failure. */
2744 static tree
2745 find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2747 pre_expr expr = get_or_alloc_expr_for (op);
2748 unsigned int lookfor = get_expr_value_id (expr);
2749 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2750 if (leader)
2752 if (leader->kind == NAME)
2753 return PRE_EXPR_NAME (leader);
2754 else if (leader->kind == CONSTANT)
2755 return PRE_EXPR_CONSTANT (leader);
2757 /* Defer. */
2758 return NULL_TREE;
2761 /* It must be a complex expression, so generate it recursively. Note
2762 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2763 where the insert algorithm fails to insert a required expression. */
2764 bitmap exprset = value_expressions[lookfor];
2765 bitmap_iterator bi;
2766 unsigned int i;
2767 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2769 pre_expr temp = expression_for_id (i);
2770 /* We cannot insert random REFERENCE expressions at arbitrary
2771 places. We can insert NARYs which eventually re-materializes
2772 its operand values. */
2773 if (temp->kind == NARY)
2774 return create_expression_by_pieces (block, temp, stmts,
2775 get_expr_type (expr));
2778 /* Defer. */
2779 return NULL_TREE;
2782 #define NECESSARY GF_PLF_1
2784 /* Create an expression in pieces, so that we can handle very complex
2785 expressions that may be ANTIC, but not necessary GIMPLE.
2786 BLOCK is the basic block the expression will be inserted into,
2787 EXPR is the expression to insert (in value form)
2788 STMTS is a statement list to append the necessary insertions into.
2790 This function will die if we hit some value that shouldn't be
2791 ANTIC but is (IE there is no leader for it, or its components).
2792 The function returns NULL_TREE in case a different antic expression
2793 has to be inserted first.
2794 This function may also generate expressions that are themselves
2795 partially or fully redundant. Those that are will be either made
2796 fully redundant during the next iteration of insert (for partially
2797 redundant ones), or eliminated by eliminate (for fully redundant
2798 ones). */
2800 static tree
2801 create_expression_by_pieces (basic_block block, pre_expr expr,
2802 gimple_seq *stmts, tree type)
2804 tree name;
2805 tree folded;
2806 gimple_seq forced_stmts = NULL;
2807 unsigned int value_id;
2808 gimple_stmt_iterator gsi;
2809 tree exprtype = type ? type : get_expr_type (expr);
2810 pre_expr nameexpr;
2811 gassign *newstmt;
2813 switch (expr->kind)
2815 /* We may hit the NAME/CONSTANT case if we have to convert types
2816 that value numbering saw through. */
2817 case NAME:
2818 folded = PRE_EXPR_NAME (expr);
2819 break;
2820 case CONSTANT:
2821 folded = PRE_EXPR_CONSTANT (expr);
2822 break;
2823 case REFERENCE:
2825 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2826 folded = create_component_ref_by_pieces (block, ref, stmts);
2827 if (!folded)
2828 return NULL_TREE;
2830 break;
2831 case NARY:
2833 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2834 tree *genop = XALLOCAVEC (tree, nary->length);
2835 unsigned i;
2836 for (i = 0; i < nary->length; ++i)
2838 genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2839 if (!genop[i])
2840 return NULL_TREE;
2841 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2842 may have conversions stripped. */
2843 if (nary->opcode == POINTER_PLUS_EXPR)
2845 if (i == 0)
2846 genop[i] = gimple_convert (&forced_stmts,
2847 nary->type, genop[i]);
2848 else if (i == 1)
2849 genop[i] = gimple_convert (&forced_stmts,
2850 sizetype, genop[i]);
2852 else
2853 genop[i] = gimple_convert (&forced_stmts,
2854 TREE_TYPE (nary->op[i]), genop[i]);
2856 if (nary->opcode == CONSTRUCTOR)
2858 vec<constructor_elt, va_gc> *elts = NULL;
2859 for (i = 0; i < nary->length; ++i)
2860 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2861 folded = build_constructor (nary->type, elts);
2863 else
2865 switch (nary->length)
2867 case 1:
2868 folded = fold_build1 (nary->opcode, nary->type,
2869 genop[0]);
2870 break;
2871 case 2:
2872 folded = fold_build2 (nary->opcode, nary->type,
2873 genop[0], genop[1]);
2874 break;
2875 case 3:
2876 folded = fold_build3 (nary->opcode, nary->type,
2877 genop[0], genop[1], genop[2]);
2878 break;
2879 default:
2880 gcc_unreachable ();
2884 break;
2885 default:
2886 gcc_unreachable ();
2889 if (!useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2890 folded = fold_convert (exprtype, folded);
2892 /* Force the generated expression to be a sequence of GIMPLE
2893 statements.
2894 We have to call unshare_expr because force_gimple_operand may
2895 modify the tree we pass to it. */
2896 gimple_seq tem = NULL;
2897 folded = force_gimple_operand (unshare_expr (folded), &tem,
2898 false, NULL);
2899 gimple_seq_add_seq_without_update (&forced_stmts, tem);
2901 /* If we have any intermediate expressions to the value sets, add them
2902 to the value sets and chain them in the instruction stream. */
2903 if (forced_stmts)
2905 gsi = gsi_start (forced_stmts);
2906 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2908 gimple stmt = gsi_stmt (gsi);
2909 tree forcedname = gimple_get_lhs (stmt);
2910 pre_expr nameexpr;
2912 if (TREE_CODE (forcedname) == SSA_NAME)
2914 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
2915 VN_INFO_GET (forcedname)->valnum = forcedname;
2916 VN_INFO (forcedname)->value_id = get_next_value_id ();
2917 nameexpr = get_or_alloc_expr_for_name (forcedname);
2918 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
2919 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2920 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2923 gimple_set_vuse (stmt, BB_LIVE_VOP_ON_EXIT (block));
2924 gimple_set_modified (stmt, true);
2926 gimple_seq_add_seq (stmts, forced_stmts);
2929 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2930 newstmt = gimple_build_assign (name, folded);
2931 gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
2932 gimple_set_modified (newstmt, true);
2933 gimple_set_plf (newstmt, NECESSARY, false);
2935 gimple_seq_add_stmt (stmts, newstmt);
2936 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (name));
2938 /* Fold the last statement. */
2939 gsi = gsi_last (*stmts);
2940 if (fold_stmt_inplace (&gsi))
2941 update_stmt (gsi_stmt (gsi));
2943 /* Add a value number to the temporary.
2944 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2945 we are creating the expression by pieces, and this particular piece of
2946 the expression may have been represented. There is no harm in replacing
2947 here. */
2948 value_id = get_expr_value_id (expr);
2949 VN_INFO_GET (name)->value_id = value_id;
2950 VN_INFO (name)->valnum = sccvn_valnum_from_value_id (value_id);
2951 if (VN_INFO (name)->valnum == NULL_TREE)
2952 VN_INFO (name)->valnum = name;
2953 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
2954 nameexpr = get_or_alloc_expr_for_name (name);
2955 add_to_value (value_id, nameexpr);
2956 if (NEW_SETS (block))
2957 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2958 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2960 pre_stats.insertions++;
2961 if (dump_file && (dump_flags & TDF_DETAILS))
2963 fprintf (dump_file, "Inserted ");
2964 print_gimple_stmt (dump_file, newstmt, 0, 0);
2965 fprintf (dump_file, " in predecessor %d (%04d)\n",
2966 block->index, value_id);
2969 return name;
2973 /* Insert the to-be-made-available values of expression EXPRNUM for each
2974 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2975 merge the result with a phi node, given the same value number as
2976 NODE. Return true if we have inserted new stuff. */
2978 static bool
2979 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
2980 vec<pre_expr> avail)
2982 pre_expr expr = expression_for_id (exprnum);
2983 pre_expr newphi;
2984 unsigned int val = get_expr_value_id (expr);
2985 edge pred;
2986 bool insertions = false;
2987 bool nophi = false;
2988 basic_block bprime;
2989 pre_expr eprime;
2990 edge_iterator ei;
2991 tree type = get_expr_type (expr);
2992 tree temp;
2993 gphi *phi;
2995 /* Make sure we aren't creating an induction variable. */
2996 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
2998 bool firstinsideloop = false;
2999 bool secondinsideloop = false;
3000 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3001 EDGE_PRED (block, 0)->src);
3002 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3003 EDGE_PRED (block, 1)->src);
3004 /* Induction variables only have one edge inside the loop. */
3005 if ((firstinsideloop ^ secondinsideloop)
3006 && expr->kind != REFERENCE)
3008 if (dump_file && (dump_flags & TDF_DETAILS))
3009 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3010 nophi = true;
3014 /* Make the necessary insertions. */
3015 FOR_EACH_EDGE (pred, ei, block->preds)
3017 gimple_seq stmts = NULL;
3018 tree builtexpr;
3019 bprime = pred->src;
3020 eprime = avail[pred->dest_idx];
3022 if (eprime->kind != NAME && eprime->kind != CONSTANT)
3024 builtexpr = create_expression_by_pieces (bprime, eprime,
3025 &stmts, type);
3026 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3027 gsi_insert_seq_on_edge (pred, stmts);
3028 if (!builtexpr)
3030 /* We cannot insert a PHI node if we failed to insert
3031 on one edge. */
3032 nophi = true;
3033 continue;
3035 avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3036 insertions = true;
3038 else if (eprime->kind == CONSTANT)
3040 /* Constants may not have the right type, fold_convert
3041 should give us back a constant with the right type. */
3042 tree constant = PRE_EXPR_CONSTANT (eprime);
3043 if (!useless_type_conversion_p (type, TREE_TYPE (constant)))
3045 tree builtexpr = fold_convert (type, constant);
3046 if (!is_gimple_min_invariant (builtexpr))
3048 tree forcedexpr = force_gimple_operand (builtexpr,
3049 &stmts, true,
3050 NULL);
3051 if (!is_gimple_min_invariant (forcedexpr))
3053 if (forcedexpr != builtexpr)
3055 VN_INFO_GET (forcedexpr)->valnum = PRE_EXPR_CONSTANT (eprime);
3056 VN_INFO (forcedexpr)->value_id = get_expr_value_id (eprime);
3058 if (stmts)
3060 gimple_stmt_iterator gsi;
3061 gsi = gsi_start (stmts);
3062 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3064 gimple stmt = gsi_stmt (gsi);
3065 tree lhs = gimple_get_lhs (stmt);
3066 if (TREE_CODE (lhs) == SSA_NAME)
3067 bitmap_set_bit (inserted_exprs,
3068 SSA_NAME_VERSION (lhs));
3069 gimple_set_plf (stmt, NECESSARY, false);
3071 gsi_insert_seq_on_edge (pred, stmts);
3073 avail[pred->dest_idx]
3074 = get_or_alloc_expr_for_name (forcedexpr);
3077 else
3078 avail[pred->dest_idx]
3079 = get_or_alloc_expr_for_constant (builtexpr);
3082 else if (eprime->kind == NAME)
3084 /* We may have to do a conversion because our value
3085 numbering can look through types in certain cases, but
3086 our IL requires all operands of a phi node have the same
3087 type. */
3088 tree name = PRE_EXPR_NAME (eprime);
3089 if (!useless_type_conversion_p (type, TREE_TYPE (name)))
3091 tree builtexpr;
3092 tree forcedexpr;
3093 builtexpr = fold_convert (type, name);
3094 forcedexpr = force_gimple_operand (builtexpr,
3095 &stmts, true,
3096 NULL);
3098 if (forcedexpr != name)
3100 VN_INFO_GET (forcedexpr)->valnum = VN_INFO (name)->valnum;
3101 VN_INFO (forcedexpr)->value_id = VN_INFO (name)->value_id;
3104 if (stmts)
3106 gimple_stmt_iterator gsi;
3107 gsi = gsi_start (stmts);
3108 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3110 gimple stmt = gsi_stmt (gsi);
3111 tree lhs = gimple_get_lhs (stmt);
3112 if (TREE_CODE (lhs) == SSA_NAME)
3113 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
3114 gimple_set_plf (stmt, NECESSARY, false);
3116 gsi_insert_seq_on_edge (pred, stmts);
3118 avail[pred->dest_idx] = get_or_alloc_expr_for_name (forcedexpr);
3122 /* If we didn't want a phi node, and we made insertions, we still have
3123 inserted new stuff, and thus return true. If we didn't want a phi node,
3124 and didn't make insertions, we haven't added anything new, so return
3125 false. */
3126 if (nophi && insertions)
3127 return true;
3128 else if (nophi && !insertions)
3129 return false;
3131 /* Now build a phi for the new variable. */
3132 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3133 phi = create_phi_node (temp, block);
3135 gimple_set_plf (phi, NECESSARY, false);
3136 VN_INFO_GET (temp)->value_id = val;
3137 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3138 if (VN_INFO (temp)->valnum == NULL_TREE)
3139 VN_INFO (temp)->valnum = temp;
3140 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3141 FOR_EACH_EDGE (pred, ei, block->preds)
3143 pre_expr ae = avail[pred->dest_idx];
3144 gcc_assert (get_expr_type (ae) == type
3145 || useless_type_conversion_p (type, get_expr_type (ae)));
3146 if (ae->kind == CONSTANT)
3147 add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3148 pred, UNKNOWN_LOCATION);
3149 else
3150 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3153 newphi = get_or_alloc_expr_for_name (temp);
3154 add_to_value (val, newphi);
3156 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3157 this insertion, since we test for the existence of this value in PHI_GEN
3158 before proceeding with the partial redundancy checks in insert_aux.
3160 The value may exist in AVAIL_OUT, in particular, it could be represented
3161 by the expression we are trying to eliminate, in which case we want the
3162 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3163 inserted there.
3165 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3166 this block, because if it did, it would have existed in our dominator's
3167 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3170 bitmap_insert_into_set (PHI_GEN (block), newphi);
3171 bitmap_value_replace_in_set (AVAIL_OUT (block),
3172 newphi);
3173 bitmap_insert_into_set (NEW_SETS (block),
3174 newphi);
3176 /* If we insert a PHI node for a conversion of another PHI node
3177 in the same basic-block try to preserve range information.
3178 This is important so that followup loop passes receive optimal
3179 number of iteration analysis results. See PR61743. */
3180 if (expr->kind == NARY
3181 && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3182 && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3183 && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3184 && INTEGRAL_TYPE_P (type)
3185 && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3186 && (TYPE_PRECISION (type)
3187 >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3188 && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3190 wide_int min, max;
3191 if (get_range_info (expr->u.nary->op[0], &min, &max) == VR_RANGE
3192 && !wi::neg_p (min, SIGNED)
3193 && !wi::neg_p (max, SIGNED))
3194 /* Just handle extension and sign-changes of all-positive ranges. */
3195 set_range_info (temp,
3196 SSA_NAME_RANGE_TYPE (expr->u.nary->op[0]),
3197 wide_int_storage::from (min, TYPE_PRECISION (type),
3198 TYPE_SIGN (type)),
3199 wide_int_storage::from (max, TYPE_PRECISION (type),
3200 TYPE_SIGN (type)));
3203 if (dump_file && (dump_flags & TDF_DETAILS))
3205 fprintf (dump_file, "Created phi ");
3206 print_gimple_stmt (dump_file, phi, 0, 0);
3207 fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3209 pre_stats.phis++;
3210 return true;
3215 /* Perform insertion of partially redundant values.
3216 For BLOCK, do the following:
3217 1. Propagate the NEW_SETS of the dominator into the current block.
3218 If the block has multiple predecessors,
3219 2a. Iterate over the ANTIC expressions for the block to see if
3220 any of them are partially redundant.
3221 2b. If so, insert them into the necessary predecessors to make
3222 the expression fully redundant.
3223 2c. Insert a new PHI merging the values of the predecessors.
3224 2d. Insert the new PHI, and the new expressions, into the
3225 NEW_SETS set.
3226 3. Recursively call ourselves on the dominator children of BLOCK.
3228 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
3229 do_regular_insertion and do_partial_insertion.
3233 static bool
3234 do_regular_insertion (basic_block block, basic_block dom)
3236 bool new_stuff = false;
3237 vec<pre_expr> exprs;
3238 pre_expr expr;
3239 auto_vec<pre_expr> avail;
3240 int i;
3242 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3243 avail.safe_grow (EDGE_COUNT (block->preds));
3245 FOR_EACH_VEC_ELT (exprs, i, expr)
3247 if (expr->kind == NARY
3248 || expr->kind == REFERENCE)
3250 unsigned int val;
3251 bool by_some = false;
3252 bool cant_insert = false;
3253 bool all_same = true;
3254 pre_expr first_s = NULL;
3255 edge pred;
3256 basic_block bprime;
3257 pre_expr eprime = NULL;
3258 edge_iterator ei;
3259 pre_expr edoubleprime = NULL;
3260 bool do_insertion = false;
3262 val = get_expr_value_id (expr);
3263 if (bitmap_set_contains_value (PHI_GEN (block), val))
3264 continue;
3265 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3267 if (dump_file && (dump_flags & TDF_DETAILS))
3269 fprintf (dump_file, "Found fully redundant value: ");
3270 print_pre_expr (dump_file, expr);
3271 fprintf (dump_file, "\n");
3273 continue;
3276 FOR_EACH_EDGE (pred, ei, block->preds)
3278 unsigned int vprime;
3280 /* We should never run insertion for the exit block
3281 and so not come across fake pred edges. */
3282 gcc_assert (!(pred->flags & EDGE_FAKE));
3283 bprime = pred->src;
3284 eprime = phi_translate (expr, ANTIC_IN (block), NULL,
3285 bprime, block);
3287 /* eprime will generally only be NULL if the
3288 value of the expression, translated
3289 through the PHI for this predecessor, is
3290 undefined. If that is the case, we can't
3291 make the expression fully redundant,
3292 because its value is undefined along a
3293 predecessor path. We can thus break out
3294 early because it doesn't matter what the
3295 rest of the results are. */
3296 if (eprime == NULL)
3298 avail[pred->dest_idx] = NULL;
3299 cant_insert = true;
3300 break;
3303 eprime = fully_constant_expression (eprime);
3304 vprime = get_expr_value_id (eprime);
3305 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3306 vprime);
3307 if (edoubleprime == NULL)
3309 avail[pred->dest_idx] = eprime;
3310 all_same = false;
3312 else
3314 avail[pred->dest_idx] = edoubleprime;
3315 by_some = true;
3316 /* We want to perform insertions to remove a redundancy on
3317 a path in the CFG we want to optimize for speed. */
3318 if (optimize_edge_for_speed_p (pred))
3319 do_insertion = true;
3320 if (first_s == NULL)
3321 first_s = edoubleprime;
3322 else if (!pre_expr_d::equal (first_s, edoubleprime))
3323 all_same = false;
3326 /* If we can insert it, it's not the same value
3327 already existing along every predecessor, and
3328 it's defined by some predecessor, it is
3329 partially redundant. */
3330 if (!cant_insert && !all_same && by_some)
3332 if (!do_insertion)
3334 if (dump_file && (dump_flags & TDF_DETAILS))
3336 fprintf (dump_file, "Skipping partial redundancy for "
3337 "expression ");
3338 print_pre_expr (dump_file, expr);
3339 fprintf (dump_file, " (%04d), no redundancy on to be "
3340 "optimized for speed edge\n", val);
3343 else if (dbg_cnt (treepre_insert))
3345 if (dump_file && (dump_flags & TDF_DETAILS))
3347 fprintf (dump_file, "Found partial redundancy for "
3348 "expression ");
3349 print_pre_expr (dump_file, expr);
3350 fprintf (dump_file, " (%04d)\n",
3351 get_expr_value_id (expr));
3353 if (insert_into_preds_of_block (block,
3354 get_expression_id (expr),
3355 avail))
3356 new_stuff = true;
3359 /* If all edges produce the same value and that value is
3360 an invariant, then the PHI has the same value on all
3361 edges. Note this. */
3362 else if (!cant_insert && all_same)
3364 gcc_assert (edoubleprime->kind == CONSTANT
3365 || edoubleprime->kind == NAME);
3367 tree temp = make_temp_ssa_name (get_expr_type (expr),
3368 NULL, "pretmp");
3369 gassign *assign
3370 = gimple_build_assign (temp,
3371 edoubleprime->kind == CONSTANT ?
3372 PRE_EXPR_CONSTANT (edoubleprime) :
3373 PRE_EXPR_NAME (edoubleprime));
3374 gimple_stmt_iterator gsi = gsi_after_labels (block);
3375 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3377 gimple_set_plf (assign, NECESSARY, false);
3378 VN_INFO_GET (temp)->value_id = val;
3379 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3380 if (VN_INFO (temp)->valnum == NULL_TREE)
3381 VN_INFO (temp)->valnum = temp;
3382 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3383 pre_expr newe = get_or_alloc_expr_for_name (temp);
3384 add_to_value (val, newe);
3385 bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3386 bitmap_insert_into_set (NEW_SETS (block), newe);
3391 exprs.release ();
3392 return new_stuff;
3396 /* Perform insertion for partially anticipatable expressions. There
3397 is only one case we will perform insertion for these. This case is
3398 if the expression is partially anticipatable, and fully available.
3399 In this case, we know that putting it earlier will enable us to
3400 remove the later computation. */
3403 static bool
3404 do_partial_partial_insertion (basic_block block, basic_block dom)
3406 bool new_stuff = false;
3407 vec<pre_expr> exprs;
3408 pre_expr expr;
3409 auto_vec<pre_expr> avail;
3410 int i;
3412 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3413 avail.safe_grow (EDGE_COUNT (block->preds));
3415 FOR_EACH_VEC_ELT (exprs, i, expr)
3417 if (expr->kind == NARY
3418 || expr->kind == REFERENCE)
3420 unsigned int val;
3421 bool by_all = true;
3422 bool cant_insert = false;
3423 edge pred;
3424 basic_block bprime;
3425 pre_expr eprime = NULL;
3426 edge_iterator ei;
3428 val = get_expr_value_id (expr);
3429 if (bitmap_set_contains_value (PHI_GEN (block), val))
3430 continue;
3431 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3432 continue;
3434 FOR_EACH_EDGE (pred, ei, block->preds)
3436 unsigned int vprime;
3437 pre_expr edoubleprime;
3439 /* We should never run insertion for the exit block
3440 and so not come across fake pred edges. */
3441 gcc_assert (!(pred->flags & EDGE_FAKE));
3442 bprime = pred->src;
3443 eprime = phi_translate (expr, ANTIC_IN (block),
3444 PA_IN (block),
3445 bprime, block);
3447 /* eprime will generally only be NULL if the
3448 value of the expression, translated
3449 through the PHI for this predecessor, is
3450 undefined. If that is the case, we can't
3451 make the expression fully redundant,
3452 because its value is undefined along a
3453 predecessor path. We can thus break out
3454 early because it doesn't matter what the
3455 rest of the results are. */
3456 if (eprime == NULL)
3458 avail[pred->dest_idx] = NULL;
3459 cant_insert = true;
3460 break;
3463 eprime = fully_constant_expression (eprime);
3464 vprime = get_expr_value_id (eprime);
3465 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3466 avail[pred->dest_idx] = edoubleprime;
3467 if (edoubleprime == NULL)
3469 by_all = false;
3470 break;
3474 /* If we can insert it, it's not the same value
3475 already existing along every predecessor, and
3476 it's defined by some predecessor, it is
3477 partially redundant. */
3478 if (!cant_insert && by_all)
3480 edge succ;
3481 bool do_insertion = false;
3483 /* Insert only if we can remove a later expression on a path
3484 that we want to optimize for speed.
3485 The phi node that we will be inserting in BLOCK is not free,
3486 and inserting it for the sake of !optimize_for_speed successor
3487 may cause regressions on the speed path. */
3488 FOR_EACH_EDGE (succ, ei, block->succs)
3490 if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3491 || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3493 if (optimize_edge_for_speed_p (succ))
3494 do_insertion = true;
3498 if (!do_insertion)
3500 if (dump_file && (dump_flags & TDF_DETAILS))
3502 fprintf (dump_file, "Skipping partial partial redundancy "
3503 "for expression ");
3504 print_pre_expr (dump_file, expr);
3505 fprintf (dump_file, " (%04d), not (partially) anticipated "
3506 "on any to be optimized for speed edges\n", val);
3509 else if (dbg_cnt (treepre_insert))
3511 pre_stats.pa_insert++;
3512 if (dump_file && (dump_flags & TDF_DETAILS))
3514 fprintf (dump_file, "Found partial partial redundancy "
3515 "for expression ");
3516 print_pre_expr (dump_file, expr);
3517 fprintf (dump_file, " (%04d)\n",
3518 get_expr_value_id (expr));
3520 if (insert_into_preds_of_block (block,
3521 get_expression_id (expr),
3522 avail))
3523 new_stuff = true;
3529 exprs.release ();
3530 return new_stuff;
3533 static bool
3534 insert_aux (basic_block block)
3536 basic_block son;
3537 bool new_stuff = false;
3539 if (block)
3541 basic_block dom;
3542 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3543 if (dom)
3545 unsigned i;
3546 bitmap_iterator bi;
3547 bitmap_set_t newset = NEW_SETS (dom);
3548 if (newset)
3550 /* Note that we need to value_replace both NEW_SETS, and
3551 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3552 represented by some non-simple expression here that we want
3553 to replace it with. */
3554 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3556 pre_expr expr = expression_for_id (i);
3557 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3558 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3561 if (!single_pred_p (block))
3563 new_stuff |= do_regular_insertion (block, dom);
3564 if (do_partial_partial)
3565 new_stuff |= do_partial_partial_insertion (block, dom);
3569 for (son = first_dom_son (CDI_DOMINATORS, block);
3570 son;
3571 son = next_dom_son (CDI_DOMINATORS, son))
3573 new_stuff |= insert_aux (son);
3576 return new_stuff;
3579 /* Perform insertion of partially redundant values. */
3581 static void
3582 insert (void)
3584 bool new_stuff = true;
3585 basic_block bb;
3586 int num_iterations = 0;
3588 FOR_ALL_BB_FN (bb, cfun)
3589 NEW_SETS (bb) = bitmap_set_new ();
3591 while (new_stuff)
3593 num_iterations++;
3594 if (dump_file && dump_flags & TDF_DETAILS)
3595 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3596 new_stuff = insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3598 /* Clear the NEW sets before the next iteration. We have already
3599 fully propagated its contents. */
3600 if (new_stuff)
3601 FOR_ALL_BB_FN (bb, cfun)
3602 bitmap_set_free (NEW_SETS (bb));
3604 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3608 /* Compute the AVAIL set for all basic blocks.
3610 This function performs value numbering of the statements in each basic
3611 block. The AVAIL sets are built from information we glean while doing
3612 this value numbering, since the AVAIL sets contain only one entry per
3613 value.
3615 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3616 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3618 static void
3619 compute_avail (void)
3622 basic_block block, son;
3623 basic_block *worklist;
3624 size_t sp = 0;
3625 unsigned i;
3627 /* We pretend that default definitions are defined in the entry block.
3628 This includes function arguments and the static chain decl. */
3629 for (i = 1; i < num_ssa_names; ++i)
3631 tree name = ssa_name (i);
3632 pre_expr e;
3633 if (!name
3634 || !SSA_NAME_IS_DEFAULT_DEF (name)
3635 || has_zero_uses (name)
3636 || virtual_operand_p (name))
3637 continue;
3639 e = get_or_alloc_expr_for_name (name);
3640 add_to_value (get_expr_value_id (e), e);
3641 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), e);
3642 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3646 if (dump_file && (dump_flags & TDF_DETAILS))
3648 print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3649 "tmp_gen", ENTRY_BLOCK);
3650 print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3651 "avail_out", ENTRY_BLOCK);
3654 /* Allocate the worklist. */
3655 worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
3657 /* Seed the algorithm by putting the dominator children of the entry
3658 block on the worklist. */
3659 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun));
3660 son;
3661 son = next_dom_son (CDI_DOMINATORS, son))
3662 worklist[sp++] = son;
3664 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun))
3665 = ssa_default_def (cfun, gimple_vop (cfun));
3667 /* Loop until the worklist is empty. */
3668 while (sp)
3670 gimple stmt;
3671 basic_block dom;
3673 /* Pick a block from the worklist. */
3674 block = worklist[--sp];
3676 /* Initially, the set of available values in BLOCK is that of
3677 its immediate dominator. */
3678 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3679 if (dom)
3681 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3682 BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
3685 /* Generate values for PHI nodes. */
3686 for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
3687 gsi_next (&gsi))
3689 tree result = gimple_phi_result (gsi.phi ());
3691 /* We have no need for virtual phis, as they don't represent
3692 actual computations. */
3693 if (virtual_operand_p (result))
3695 BB_LIVE_VOP_ON_EXIT (block) = result;
3696 continue;
3699 pre_expr e = get_or_alloc_expr_for_name (result);
3700 add_to_value (get_expr_value_id (e), e);
3701 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3702 bitmap_insert_into_set (PHI_GEN (block), e);
3705 BB_MAY_NOTRETURN (block) = 0;
3707 /* Now compute value numbers and populate value sets with all
3708 the expressions computed in BLOCK. */
3709 for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
3710 gsi_next (&gsi))
3712 ssa_op_iter iter;
3713 tree op;
3715 stmt = gsi_stmt (gsi);
3717 /* Cache whether the basic-block has any non-visible side-effect
3718 or control flow.
3719 If this isn't a call or it is the last stmt in the
3720 basic-block then the CFG represents things correctly. */
3721 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3723 /* Non-looping const functions always return normally.
3724 Otherwise the call might not return or have side-effects
3725 that forbids hoisting possibly trapping expressions
3726 before it. */
3727 int flags = gimple_call_flags (stmt);
3728 if (!(flags & ECF_CONST)
3729 || (flags & ECF_LOOPING_CONST_OR_PURE))
3730 BB_MAY_NOTRETURN (block) = 1;
3733 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3735 pre_expr e = get_or_alloc_expr_for_name (op);
3737 add_to_value (get_expr_value_id (e), e);
3738 bitmap_insert_into_set (TMP_GEN (block), e);
3739 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3742 if (gimple_vdef (stmt))
3743 BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
3745 if (gimple_has_side_effects (stmt)
3746 || stmt_could_throw_p (stmt)
3747 || is_gimple_debug (stmt))
3748 continue;
3750 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3752 if (ssa_undefined_value_p (op))
3753 continue;
3754 pre_expr e = get_or_alloc_expr_for_name (op);
3755 bitmap_value_insert_into_set (EXP_GEN (block), e);
3758 switch (gimple_code (stmt))
3760 case GIMPLE_RETURN:
3761 continue;
3763 case GIMPLE_CALL:
3765 vn_reference_t ref;
3766 vn_reference_s ref1;
3767 pre_expr result = NULL;
3769 /* We can value number only calls to real functions. */
3770 if (gimple_call_internal_p (stmt))
3771 continue;
3773 vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
3774 if (!ref)
3775 continue;
3777 /* If the value of the call is not invalidated in
3778 this block until it is computed, add the expression
3779 to EXP_GEN. */
3780 if (!gimple_vuse (stmt)
3781 || gimple_code
3782 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3783 || gimple_bb (SSA_NAME_DEF_STMT
3784 (gimple_vuse (stmt))) != block)
3786 result = pre_expr_pool.allocate ();
3787 result->kind = REFERENCE;
3788 result->id = 0;
3789 PRE_EXPR_REFERENCE (result) = ref;
3791 get_or_alloc_expression_id (result);
3792 add_to_value (get_expr_value_id (result), result);
3793 bitmap_value_insert_into_set (EXP_GEN (block), result);
3795 continue;
3798 case GIMPLE_ASSIGN:
3800 pre_expr result = NULL;
3801 switch (vn_get_stmt_kind (stmt))
3803 case VN_NARY:
3805 enum tree_code code = gimple_assign_rhs_code (stmt);
3806 vn_nary_op_t nary;
3808 /* COND_EXPR and VEC_COND_EXPR are awkward in
3809 that they contain an embedded complex expression.
3810 Don't even try to shove those through PRE. */
3811 if (code == COND_EXPR
3812 || code == VEC_COND_EXPR)
3813 continue;
3815 vn_nary_op_lookup_stmt (stmt, &nary);
3816 if (!nary)
3817 continue;
3819 /* If the NARY traps and there was a preceding
3820 point in the block that might not return avoid
3821 adding the nary to EXP_GEN. */
3822 if (BB_MAY_NOTRETURN (block)
3823 && vn_nary_may_trap (nary))
3824 continue;
3826 result = pre_expr_pool.allocate ();
3827 result->kind = NARY;
3828 result->id = 0;
3829 PRE_EXPR_NARY (result) = nary;
3830 break;
3833 case VN_REFERENCE:
3835 vn_reference_t ref;
3836 vn_reference_lookup (gimple_assign_rhs1 (stmt),
3837 gimple_vuse (stmt),
3838 VN_WALK, &ref);
3839 if (!ref)
3840 continue;
3842 /* If the value of the reference is not invalidated in
3843 this block until it is computed, add the expression
3844 to EXP_GEN. */
3845 if (gimple_vuse (stmt))
3847 gimple def_stmt;
3848 bool ok = true;
3849 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
3850 while (!gimple_nop_p (def_stmt)
3851 && gimple_code (def_stmt) != GIMPLE_PHI
3852 && gimple_bb (def_stmt) == block)
3854 if (stmt_may_clobber_ref_p
3855 (def_stmt, gimple_assign_rhs1 (stmt)))
3857 ok = false;
3858 break;
3860 def_stmt
3861 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
3863 if (!ok)
3864 continue;
3867 result = pre_expr_pool.allocate ();
3868 result->kind = REFERENCE;
3869 result->id = 0;
3870 PRE_EXPR_REFERENCE (result) = ref;
3871 break;
3874 default:
3875 continue;
3878 get_or_alloc_expression_id (result);
3879 add_to_value (get_expr_value_id (result), result);
3880 bitmap_value_insert_into_set (EXP_GEN (block), result);
3881 continue;
3883 default:
3884 break;
3888 if (dump_file && (dump_flags & TDF_DETAILS))
3890 print_bitmap_set (dump_file, EXP_GEN (block),
3891 "exp_gen", block->index);
3892 print_bitmap_set (dump_file, PHI_GEN (block),
3893 "phi_gen", block->index);
3894 print_bitmap_set (dump_file, TMP_GEN (block),
3895 "tmp_gen", block->index);
3896 print_bitmap_set (dump_file, AVAIL_OUT (block),
3897 "avail_out", block->index);
3900 /* Put the dominator children of BLOCK on the worklist of blocks
3901 to compute available sets for. */
3902 for (son = first_dom_son (CDI_DOMINATORS, block);
3903 son;
3904 son = next_dom_son (CDI_DOMINATORS, son))
3905 worklist[sp++] = son;
3908 free (worklist);
3912 /* Local state for the eliminate domwalk. */
3913 static vec<gimple> el_to_remove;
3914 static vec<gimple> el_to_fixup;
3915 static unsigned int el_todo;
3916 static vec<tree> el_avail;
3917 static vec<tree> el_avail_stack;
3919 /* Return a leader for OP that is available at the current point of the
3920 eliminate domwalk. */
3922 static tree
3923 eliminate_avail (tree op)
3925 tree valnum = VN_INFO (op)->valnum;
3926 if (TREE_CODE (valnum) == SSA_NAME)
3928 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
3929 return valnum;
3930 if (el_avail.length () > SSA_NAME_VERSION (valnum))
3931 return el_avail[SSA_NAME_VERSION (valnum)];
3933 else if (is_gimple_min_invariant (valnum))
3934 return valnum;
3935 return NULL_TREE;
3938 /* At the current point of the eliminate domwalk make OP available. */
3940 static void
3941 eliminate_push_avail (tree op)
3943 tree valnum = VN_INFO (op)->valnum;
3944 if (TREE_CODE (valnum) == SSA_NAME)
3946 if (el_avail.length () <= SSA_NAME_VERSION (valnum))
3947 el_avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1);
3948 tree pushop = op;
3949 if (el_avail[SSA_NAME_VERSION (valnum)])
3950 pushop = el_avail[SSA_NAME_VERSION (valnum)];
3951 el_avail_stack.safe_push (pushop);
3952 el_avail[SSA_NAME_VERSION (valnum)] = op;
3956 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
3957 the leader for the expression if insertion was successful. */
3959 static tree
3960 eliminate_insert (gimple_stmt_iterator *gsi, tree val)
3962 tree expr = vn_get_expr_for (val);
3963 if (!CONVERT_EXPR_P (expr)
3964 && TREE_CODE (expr) != VIEW_CONVERT_EXPR)
3965 return NULL_TREE;
3967 tree op = TREE_OPERAND (expr, 0);
3968 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (op) : op;
3969 if (!leader)
3970 return NULL_TREE;
3972 tree res = make_temp_ssa_name (TREE_TYPE (val), NULL, "pretmp");
3973 gassign *tem = gimple_build_assign (res,
3974 fold_build1 (TREE_CODE (expr),
3975 TREE_TYPE (expr), leader));
3976 gsi_insert_before (gsi, tem, GSI_SAME_STMT);
3977 VN_INFO_GET (res)->valnum = val;
3979 if (TREE_CODE (leader) == SSA_NAME)
3980 gimple_set_plf (SSA_NAME_DEF_STMT (leader), NECESSARY, true);
3982 pre_stats.insertions++;
3983 if (dump_file && (dump_flags & TDF_DETAILS))
3985 fprintf (dump_file, "Inserted ");
3986 print_gimple_stmt (dump_file, tem, 0, 0);
3989 return res;
3992 class eliminate_dom_walker : public dom_walker
3994 public:
3995 eliminate_dom_walker (cdi_direction direction, bool do_pre_)
3996 : dom_walker (direction), do_pre (do_pre_) {}
3998 virtual void before_dom_children (basic_block);
3999 virtual void after_dom_children (basic_block);
4001 bool do_pre;
4004 /* Perform elimination for the basic-block B during the domwalk. */
4006 void
4007 eliminate_dom_walker::before_dom_children (basic_block b)
4009 /* Mark new bb. */
4010 el_avail_stack.safe_push (NULL_TREE);
4012 /* ??? If we do nothing for unreachable blocks then this will confuse
4013 tailmerging. Eventually we can reduce its reliance on SCCVN now
4014 that we fully copy/constant-propagate (most) things. */
4016 for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
4018 gphi *phi = gsi.phi ();
4019 tree res = PHI_RESULT (phi);
4021 if (virtual_operand_p (res))
4023 gsi_next (&gsi);
4024 continue;
4027 tree sprime = eliminate_avail (res);
4028 if (sprime
4029 && sprime != res)
4031 if (dump_file && (dump_flags & TDF_DETAILS))
4033 fprintf (dump_file, "Replaced redundant PHI node defining ");
4034 print_generic_expr (dump_file, res, 0);
4035 fprintf (dump_file, " with ");
4036 print_generic_expr (dump_file, sprime, 0);
4037 fprintf (dump_file, "\n");
4040 /* If we inserted this PHI node ourself, it's not an elimination. */
4041 if (inserted_exprs
4042 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
4043 pre_stats.phis--;
4044 else
4045 pre_stats.eliminations++;
4047 /* If we will propagate into all uses don't bother to do
4048 anything. */
4049 if (may_propagate_copy (res, sprime))
4051 /* Mark the PHI for removal. */
4052 el_to_remove.safe_push (phi);
4053 gsi_next (&gsi);
4054 continue;
4057 remove_phi_node (&gsi, false);
4059 if (inserted_exprs
4060 && !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res))
4061 && TREE_CODE (sprime) == SSA_NAME)
4062 gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
4064 if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
4065 sprime = fold_convert (TREE_TYPE (res), sprime);
4066 gimple stmt = gimple_build_assign (res, sprime);
4067 /* ??? It cannot yet be necessary (DOM walk). */
4068 gimple_set_plf (stmt, NECESSARY, gimple_plf (phi, NECESSARY));
4070 gimple_stmt_iterator gsi2 = gsi_after_labels (b);
4071 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
4072 continue;
4075 eliminate_push_avail (res);
4076 gsi_next (&gsi);
4079 for (gimple_stmt_iterator gsi = gsi_start_bb (b);
4080 !gsi_end_p (gsi);
4081 gsi_next (&gsi))
4083 tree sprime = NULL_TREE;
4084 gimple stmt = gsi_stmt (gsi);
4085 tree lhs = gimple_get_lhs (stmt);
4086 if (lhs && TREE_CODE (lhs) == SSA_NAME
4087 && !gimple_has_volatile_ops (stmt)
4088 /* See PR43491. Do not replace a global register variable when
4089 it is a the RHS of an assignment. Do replace local register
4090 variables since gcc does not guarantee a local variable will
4091 be allocated in register.
4092 ??? The fix isn't effective here. This should instead
4093 be ensured by not value-numbering them the same but treating
4094 them like volatiles? */
4095 && !(gimple_assign_single_p (stmt)
4096 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
4097 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
4098 && is_global_var (gimple_assign_rhs1 (stmt)))))
4100 sprime = eliminate_avail (lhs);
4101 if (!sprime)
4103 /* If there is no existing usable leader but SCCVN thinks
4104 it has an expression it wants to use as replacement,
4105 insert that. */
4106 tree val = VN_INFO (lhs)->valnum;
4107 if (val != VN_TOP
4108 && TREE_CODE (val) == SSA_NAME
4109 && VN_INFO (val)->needs_insertion
4110 && VN_INFO (val)->expr != NULL_TREE
4111 && (sprime = eliminate_insert (&gsi, val)) != NULL_TREE)
4112 eliminate_push_avail (sprime);
4115 /* If this now constitutes a copy duplicate points-to
4116 and range info appropriately. This is especially
4117 important for inserted code. See tree-ssa-copy.c
4118 for similar code. */
4119 if (sprime
4120 && TREE_CODE (sprime) == SSA_NAME)
4122 basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime));
4123 if (POINTER_TYPE_P (TREE_TYPE (lhs))
4124 && SSA_NAME_PTR_INFO (lhs)
4125 && !SSA_NAME_PTR_INFO (sprime))
4127 duplicate_ssa_name_ptr_info (sprime,
4128 SSA_NAME_PTR_INFO (lhs));
4129 if (b != sprime_b)
4130 mark_ptr_info_alignment_unknown
4131 (SSA_NAME_PTR_INFO (sprime));
4133 else if (!POINTER_TYPE_P (TREE_TYPE (lhs))
4134 && SSA_NAME_RANGE_INFO (lhs)
4135 && !SSA_NAME_RANGE_INFO (sprime)
4136 && b == sprime_b)
4137 duplicate_ssa_name_range_info (sprime,
4138 SSA_NAME_RANGE_TYPE (lhs),
4139 SSA_NAME_RANGE_INFO (lhs));
4142 /* Inhibit the use of an inserted PHI on a loop header when
4143 the address of the memory reference is a simple induction
4144 variable. In other cases the vectorizer won't do anything
4145 anyway (either it's loop invariant or a complicated
4146 expression). */
4147 if (sprime
4148 && TREE_CODE (sprime) == SSA_NAME
4149 && do_pre
4150 && flag_tree_loop_vectorize
4151 && loop_outer (b->loop_father)
4152 && has_zero_uses (sprime)
4153 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
4154 && gimple_assign_load_p (stmt))
4156 gimple def_stmt = SSA_NAME_DEF_STMT (sprime);
4157 basic_block def_bb = gimple_bb (def_stmt);
4158 if (gimple_code (def_stmt) == GIMPLE_PHI
4159 && b->loop_father->header == def_bb)
4161 ssa_op_iter iter;
4162 tree op;
4163 bool found = false;
4164 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
4166 affine_iv iv;
4167 def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
4168 if (def_bb
4169 && flow_bb_inside_loop_p (b->loop_father, def_bb)
4170 && simple_iv (b->loop_father,
4171 b->loop_father, op, &iv, true))
4173 found = true;
4174 break;
4177 if (found)
4179 if (dump_file && (dump_flags & TDF_DETAILS))
4181 fprintf (dump_file, "Not replacing ");
4182 print_gimple_expr (dump_file, stmt, 0, 0);
4183 fprintf (dump_file, " with ");
4184 print_generic_expr (dump_file, sprime, 0);
4185 fprintf (dump_file, " which would add a loop"
4186 " carried dependence to loop %d\n",
4187 b->loop_father->num);
4189 /* Don't keep sprime available. */
4190 sprime = NULL_TREE;
4195 if (sprime)
4197 /* If we can propagate the value computed for LHS into
4198 all uses don't bother doing anything with this stmt. */
4199 if (may_propagate_copy (lhs, sprime))
4201 /* Mark it for removal. */
4202 el_to_remove.safe_push (stmt);
4204 /* ??? Don't count copy/constant propagations. */
4205 if (gimple_assign_single_p (stmt)
4206 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
4207 || gimple_assign_rhs1 (stmt) == sprime))
4208 continue;
4210 if (dump_file && (dump_flags & TDF_DETAILS))
4212 fprintf (dump_file, "Replaced ");
4213 print_gimple_expr (dump_file, stmt, 0, 0);
4214 fprintf (dump_file, " with ");
4215 print_generic_expr (dump_file, sprime, 0);
4216 fprintf (dump_file, " in all uses of ");
4217 print_gimple_stmt (dump_file, stmt, 0, 0);
4220 pre_stats.eliminations++;
4221 continue;
4224 /* If this is an assignment from our leader (which
4225 happens in the case the value-number is a constant)
4226 then there is nothing to do. */
4227 if (gimple_assign_single_p (stmt)
4228 && sprime == gimple_assign_rhs1 (stmt))
4229 continue;
4231 /* Else replace its RHS. */
4232 bool can_make_abnormal_goto
4233 = is_gimple_call (stmt)
4234 && stmt_can_make_abnormal_goto (stmt);
4236 if (dump_file && (dump_flags & TDF_DETAILS))
4238 fprintf (dump_file, "Replaced ");
4239 print_gimple_expr (dump_file, stmt, 0, 0);
4240 fprintf (dump_file, " with ");
4241 print_generic_expr (dump_file, sprime, 0);
4242 fprintf (dump_file, " in ");
4243 print_gimple_stmt (dump_file, stmt, 0, 0);
4246 if (TREE_CODE (sprime) == SSA_NAME)
4247 gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
4248 NECESSARY, true);
4250 pre_stats.eliminations++;
4251 gimple orig_stmt = stmt;
4252 if (!useless_type_conversion_p (TREE_TYPE (lhs),
4253 TREE_TYPE (sprime)))
4254 sprime = fold_convert (TREE_TYPE (lhs), sprime);
4255 tree vdef = gimple_vdef (stmt);
4256 tree vuse = gimple_vuse (stmt);
4257 propagate_tree_value_into_stmt (&gsi, sprime);
4258 stmt = gsi_stmt (gsi);
4259 update_stmt (stmt);
4260 if (vdef != gimple_vdef (stmt))
4261 VN_INFO (vdef)->valnum = vuse;
4263 /* If we removed EH side-effects from the statement, clean
4264 its EH information. */
4265 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4267 bitmap_set_bit (need_eh_cleanup,
4268 gimple_bb (stmt)->index);
4269 if (dump_file && (dump_flags & TDF_DETAILS))
4270 fprintf (dump_file, " Removed EH side-effects.\n");
4273 /* Likewise for AB side-effects. */
4274 if (can_make_abnormal_goto
4275 && !stmt_can_make_abnormal_goto (stmt))
4277 bitmap_set_bit (need_ab_cleanup,
4278 gimple_bb (stmt)->index);
4279 if (dump_file && (dump_flags & TDF_DETAILS))
4280 fprintf (dump_file, " Removed AB side-effects.\n");
4283 continue;
4287 /* If the statement is a scalar store, see if the expression
4288 has the same value number as its rhs. If so, the store is
4289 dead. */
4290 if (gimple_assign_single_p (stmt)
4291 && !gimple_has_volatile_ops (stmt)
4292 && !is_gimple_reg (gimple_assign_lhs (stmt))
4293 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
4294 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
4296 tree val;
4297 tree rhs = gimple_assign_rhs1 (stmt);
4298 val = vn_reference_lookup (gimple_assign_lhs (stmt),
4299 gimple_vuse (stmt), VN_WALK, NULL);
4300 if (TREE_CODE (rhs) == SSA_NAME)
4301 rhs = VN_INFO (rhs)->valnum;
4302 if (val
4303 && operand_equal_p (val, rhs, 0))
4305 if (dump_file && (dump_flags & TDF_DETAILS))
4307 fprintf (dump_file, "Deleted redundant store ");
4308 print_gimple_stmt (dump_file, stmt, 0, 0);
4311 /* Queue stmt for removal. */
4312 el_to_remove.safe_push (stmt);
4313 continue;
4317 bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
4318 bool was_noreturn = (is_gimple_call (stmt)
4319 && gimple_call_noreturn_p (stmt));
4320 tree vdef = gimple_vdef (stmt);
4321 tree vuse = gimple_vuse (stmt);
4323 /* If we didn't replace the whole stmt (or propagate the result
4324 into all uses), replace all uses on this stmt with their
4325 leaders. */
4326 use_operand_p use_p;
4327 ssa_op_iter iter;
4328 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
4330 tree use = USE_FROM_PTR (use_p);
4331 /* ??? The call code above leaves stmt operands un-updated. */
4332 if (TREE_CODE (use) != SSA_NAME)
4333 continue;
4334 tree sprime = eliminate_avail (use);
4335 if (sprime && sprime != use
4336 && may_propagate_copy (use, sprime)
4337 /* We substitute into debug stmts to avoid excessive
4338 debug temporaries created by removed stmts, but we need
4339 to avoid doing so for inserted sprimes as we never want
4340 to create debug temporaries for them. */
4341 && (!inserted_exprs
4342 || TREE_CODE (sprime) != SSA_NAME
4343 || !is_gimple_debug (stmt)
4344 || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
4346 propagate_value (use_p, sprime);
4347 gimple_set_modified (stmt, true);
4348 if (TREE_CODE (sprime) == SSA_NAME
4349 && !is_gimple_debug (stmt))
4350 gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
4351 NECESSARY, true);
4355 /* Visit indirect calls and turn them into direct calls if
4356 possible using the devirtualization machinery. */
4357 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
4359 tree fn = gimple_call_fn (call_stmt);
4360 if (fn
4361 && flag_devirtualize
4362 && virtual_method_call_p (fn))
4364 tree otr_type = obj_type_ref_class (fn);
4365 tree instance;
4366 ipa_polymorphic_call_context context (current_function_decl, fn, stmt, &instance);
4367 bool final;
4369 context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn), otr_type, stmt);
4371 vec <cgraph_node *>targets
4372 = possible_polymorphic_call_targets (obj_type_ref_class (fn),
4373 tree_to_uhwi
4374 (OBJ_TYPE_REF_TOKEN (fn)),
4375 context,
4376 &final);
4377 if (dump_enabled_p ())
4378 dump_possible_polymorphic_call_targets (dump_file,
4379 obj_type_ref_class (fn),
4380 tree_to_uhwi
4381 (OBJ_TYPE_REF_TOKEN (fn)),
4382 context);
4383 if (final && targets.length () <= 1 && dbg_cnt (devirt))
4385 tree fn;
4386 if (targets.length () == 1)
4387 fn = targets[0]->decl;
4388 else
4389 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
4390 if (dump_enabled_p ())
4392 location_t loc = gimple_location_safe (stmt);
4393 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
4394 "converting indirect call to "
4395 "function %s\n",
4396 cgraph_node::get (fn)->name ());
4398 gimple_call_set_fndecl (call_stmt, fn);
4399 maybe_remove_unused_call_args (cfun, call_stmt);
4400 gimple_set_modified (stmt, true);
4405 if (gimple_modified_p (stmt))
4407 /* If a formerly non-invariant ADDR_EXPR is turned into an
4408 invariant one it was on a separate stmt. */
4409 if (gimple_assign_single_p (stmt)
4410 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
4411 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
4412 gimple old_stmt = stmt;
4413 if (is_gimple_call (stmt))
4415 /* ??? Only fold calls inplace for now, this may create new
4416 SSA names which in turn will confuse free_scc_vn SSA name
4417 release code. */
4418 fold_stmt_inplace (&gsi);
4419 /* When changing a call into a noreturn call, cfg cleanup
4420 is needed to fix up the noreturn call. */
4421 if (!was_noreturn && gimple_call_noreturn_p (stmt))
4422 el_to_fixup.safe_push (stmt);
4424 else
4426 fold_stmt (&gsi);
4427 stmt = gsi_stmt (gsi);
4428 if ((gimple_code (stmt) == GIMPLE_COND
4429 && (gimple_cond_true_p (as_a <gcond *> (stmt))
4430 || gimple_cond_false_p (as_a <gcond *> (stmt))))
4431 || (gimple_code (stmt) == GIMPLE_SWITCH
4432 && TREE_CODE (gimple_switch_index (
4433 as_a <gswitch *> (stmt)))
4434 == INTEGER_CST))
4435 el_todo |= TODO_cleanup_cfg;
4437 /* If we removed EH side-effects from the statement, clean
4438 its EH information. */
4439 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
4441 bitmap_set_bit (need_eh_cleanup,
4442 gimple_bb (stmt)->index);
4443 if (dump_file && (dump_flags & TDF_DETAILS))
4444 fprintf (dump_file, " Removed EH side-effects.\n");
4446 /* Likewise for AB side-effects. */
4447 if (can_make_abnormal_goto
4448 && !stmt_can_make_abnormal_goto (stmt))
4450 bitmap_set_bit (need_ab_cleanup,
4451 gimple_bb (stmt)->index);
4452 if (dump_file && (dump_flags & TDF_DETAILS))
4453 fprintf (dump_file, " Removed AB side-effects.\n");
4455 update_stmt (stmt);
4456 if (vdef != gimple_vdef (stmt))
4457 VN_INFO (vdef)->valnum = vuse;
4460 /* Make new values available - for fully redundant LHS we
4461 continue with the next stmt above and skip this. */
4462 def_operand_p defp;
4463 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
4464 eliminate_push_avail (DEF_FROM_PTR (defp));
4467 /* Replace destination PHI arguments. */
4468 edge_iterator ei;
4469 edge e;
4470 FOR_EACH_EDGE (e, ei, b->succs)
4472 for (gphi_iterator gsi = gsi_start_phis (e->dest);
4473 !gsi_end_p (gsi);
4474 gsi_next (&gsi))
4476 gphi *phi = gsi.phi ();
4477 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
4478 tree arg = USE_FROM_PTR (use_p);
4479 if (TREE_CODE (arg) != SSA_NAME
4480 || virtual_operand_p (arg))
4481 continue;
4482 tree sprime = eliminate_avail (arg);
4483 if (sprime && may_propagate_copy (arg, sprime))
4485 propagate_value (use_p, sprime);
4486 if (TREE_CODE (sprime) == SSA_NAME)
4487 gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
4493 /* Make no longer available leaders no longer available. */
4495 void
4496 eliminate_dom_walker::after_dom_children (basic_block)
4498 tree entry;
4499 while ((entry = el_avail_stack.pop ()) != NULL_TREE)
4501 tree valnum = VN_INFO (entry)->valnum;
4502 tree old = el_avail[SSA_NAME_VERSION (valnum)];
4503 if (old == entry)
4504 el_avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
4505 else
4506 el_avail[SSA_NAME_VERSION (valnum)] = entry;
4510 /* Eliminate fully redundant computations. */
4512 static unsigned int
4513 eliminate (bool do_pre)
4515 gimple_stmt_iterator gsi;
4516 gimple stmt;
4518 need_eh_cleanup = BITMAP_ALLOC (NULL);
4519 need_ab_cleanup = BITMAP_ALLOC (NULL);
4521 el_to_remove.create (0);
4522 el_to_fixup.create (0);
4523 el_todo = 0;
4524 el_avail.create (num_ssa_names);
4525 el_avail_stack.create (0);
4527 eliminate_dom_walker (CDI_DOMINATORS,
4528 do_pre).walk (cfun->cfg->x_entry_block_ptr);
4530 el_avail.release ();
4531 el_avail_stack.release ();
4533 /* We cannot remove stmts during BB walk, especially not release SSA
4534 names there as this confuses the VN machinery. The stmts ending
4535 up in el_to_remove are either stores or simple copies.
4536 Remove stmts in reverse order to make debug stmt creation possible. */
4537 while (!el_to_remove.is_empty ())
4539 stmt = el_to_remove.pop ();
4541 if (dump_file && (dump_flags & TDF_DETAILS))
4543 fprintf (dump_file, "Removing dead stmt ");
4544 print_gimple_stmt (dump_file, stmt, 0, 0);
4547 tree lhs;
4548 if (gimple_code (stmt) == GIMPLE_PHI)
4549 lhs = gimple_phi_result (stmt);
4550 else
4551 lhs = gimple_get_lhs (stmt);
4553 if (inserted_exprs
4554 && TREE_CODE (lhs) == SSA_NAME)
4555 bitmap_clear_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
4557 gsi = gsi_for_stmt (stmt);
4558 if (gimple_code (stmt) == GIMPLE_PHI)
4559 remove_phi_node (&gsi, true);
4560 else
4562 basic_block bb = gimple_bb (stmt);
4563 unlink_stmt_vdef (stmt);
4564 if (gsi_remove (&gsi, true))
4565 bitmap_set_bit (need_eh_cleanup, bb->index);
4566 release_defs (stmt);
4569 /* Removing a stmt may expose a forwarder block. */
4570 el_todo |= TODO_cleanup_cfg;
4572 el_to_remove.release ();
4574 /* Fixup stmts that became noreturn calls. This may require splitting
4575 blocks and thus isn't possible during the dominator walk. Do this
4576 in reverse order so we don't inadvertedly remove a stmt we want to
4577 fixup by visiting a dominating now noreturn call first. */
4578 while (!el_to_fixup.is_empty ())
4580 stmt = el_to_fixup.pop ();
4582 if (dump_file && (dump_flags & TDF_DETAILS))
4584 fprintf (dump_file, "Fixing up noreturn call ");
4585 print_gimple_stmt (dump_file, stmt, 0, 0);
4588 if (fixup_noreturn_call (stmt))
4589 el_todo |= TODO_cleanup_cfg;
4591 el_to_fixup.release ();
4593 return el_todo;
4596 /* Perform CFG cleanups made necessary by elimination. */
4598 static unsigned
4599 fini_eliminate (void)
4601 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
4602 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
4604 if (do_eh_cleanup)
4605 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
4607 if (do_ab_cleanup)
4608 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
4610 BITMAP_FREE (need_eh_cleanup);
4611 BITMAP_FREE (need_ab_cleanup);
4613 if (do_eh_cleanup || do_ab_cleanup)
4614 return TODO_cleanup_cfg;
4615 return 0;
4618 /* Borrow a bit of tree-ssa-dce.c for the moment.
4619 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4620 this may be a bit faster, and we may want critical edges kept split. */
4622 /* If OP's defining statement has not already been determined to be necessary,
4623 mark that statement necessary. Return the stmt, if it is newly
4624 necessary. */
4626 static inline gimple
4627 mark_operand_necessary (tree op)
4629 gimple stmt;
4631 gcc_assert (op);
4633 if (TREE_CODE (op) != SSA_NAME)
4634 return NULL;
4636 stmt = SSA_NAME_DEF_STMT (op);
4637 gcc_assert (stmt);
4639 if (gimple_plf (stmt, NECESSARY)
4640 || gimple_nop_p (stmt))
4641 return NULL;
4643 gimple_set_plf (stmt, NECESSARY, true);
4644 return stmt;
4647 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4648 to insert PHI nodes sometimes, and because value numbering of casts isn't
4649 perfect, we sometimes end up inserting dead code. This simple DCE-like
4650 pass removes any insertions we made that weren't actually used. */
4652 static void
4653 remove_dead_inserted_code (void)
4655 bitmap worklist;
4656 unsigned i;
4657 bitmap_iterator bi;
4658 gimple t;
4660 worklist = BITMAP_ALLOC (NULL);
4661 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4663 t = SSA_NAME_DEF_STMT (ssa_name (i));
4664 if (gimple_plf (t, NECESSARY))
4665 bitmap_set_bit (worklist, i);
4667 while (!bitmap_empty_p (worklist))
4669 i = bitmap_first_set_bit (worklist);
4670 bitmap_clear_bit (worklist, i);
4671 t = SSA_NAME_DEF_STMT (ssa_name (i));
4673 /* PHI nodes are somewhat special in that each PHI alternative has
4674 data and control dependencies. All the statements feeding the
4675 PHI node's arguments are always necessary. */
4676 if (gimple_code (t) == GIMPLE_PHI)
4678 unsigned k;
4680 for (k = 0; k < gimple_phi_num_args (t); k++)
4682 tree arg = PHI_ARG_DEF (t, k);
4683 if (TREE_CODE (arg) == SSA_NAME)
4685 gimple n = mark_operand_necessary (arg);
4686 if (n)
4687 bitmap_set_bit (worklist, SSA_NAME_VERSION (arg));
4691 else
4693 /* Propagate through the operands. Examine all the USE, VUSE and
4694 VDEF operands in this statement. Mark all the statements
4695 which feed this statement's uses as necessary. */
4696 ssa_op_iter iter;
4697 tree use;
4699 /* The operands of VDEF expressions are also needed as they
4700 represent potential definitions that may reach this
4701 statement (VDEF operands allow us to follow def-def
4702 links). */
4704 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
4706 gimple n = mark_operand_necessary (use);
4707 if (n)
4708 bitmap_set_bit (worklist, SSA_NAME_VERSION (use));
4713 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4715 t = SSA_NAME_DEF_STMT (ssa_name (i));
4716 if (!gimple_plf (t, NECESSARY))
4718 gimple_stmt_iterator gsi;
4720 if (dump_file && (dump_flags & TDF_DETAILS))
4722 fprintf (dump_file, "Removing unnecessary insertion:");
4723 print_gimple_stmt (dump_file, t, 0, 0);
4726 gsi = gsi_for_stmt (t);
4727 if (gimple_code (t) == GIMPLE_PHI)
4728 remove_phi_node (&gsi, true);
4729 else
4731 gsi_remove (&gsi, true);
4732 release_defs (t);
4736 BITMAP_FREE (worklist);
4740 /* Initialize data structures used by PRE. */
4742 static void
4743 init_pre (void)
4745 basic_block bb;
4747 next_expression_id = 1;
4748 expressions.create (0);
4749 expressions.safe_push (NULL);
4750 value_expressions.create (get_max_value_id () + 1);
4751 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
4752 name_to_id.create (0);
4754 inserted_exprs = BITMAP_ALLOC (NULL);
4756 connect_infinite_loops_to_exit ();
4757 memset (&pre_stats, 0, sizeof (pre_stats));
4759 postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
4760 postorder_num = inverted_post_order_compute (postorder);
4762 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4764 calculate_dominance_info (CDI_POST_DOMINATORS);
4765 calculate_dominance_info (CDI_DOMINATORS);
4767 bitmap_obstack_initialize (&grand_bitmap_obstack);
4768 phi_translate_table = new hash_table<expr_pred_trans_d> (5110);
4769 expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4770 FOR_ALL_BB_FN (bb, cfun)
4772 EXP_GEN (bb) = bitmap_set_new ();
4773 PHI_GEN (bb) = bitmap_set_new ();
4774 TMP_GEN (bb) = bitmap_set_new ();
4775 AVAIL_OUT (bb) = bitmap_set_new ();
4780 /* Deallocate data structures used by PRE. */
4782 static void
4783 fini_pre ()
4785 free (postorder);
4786 value_expressions.release ();
4787 BITMAP_FREE (inserted_exprs);
4788 bitmap_obstack_release (&grand_bitmap_obstack);
4789 bitmap_set_pool.release ();
4790 pre_expr_pool.release ();
4791 delete phi_translate_table;
4792 phi_translate_table = NULL;
4793 delete expression_to_id;
4794 expression_to_id = NULL;
4795 name_to_id.release ();
4797 free_aux_for_blocks ();
4799 free_dominance_info (CDI_POST_DOMINATORS);
4802 namespace {
4804 const pass_data pass_data_pre =
4806 GIMPLE_PASS, /* type */
4807 "pre", /* name */
4808 OPTGROUP_NONE, /* optinfo_flags */
4809 TV_TREE_PRE, /* tv_id */
4810 /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before
4811 pass_pre. */
4812 ( PROP_no_crit_edges | PROP_cfg | PROP_ssa ), /* properties_required */
4813 0, /* properties_provided */
4814 PROP_no_crit_edges, /* properties_destroyed */
4815 TODO_rebuild_alias, /* todo_flags_start */
4816 0, /* todo_flags_finish */
4819 class pass_pre : public gimple_opt_pass
4821 public:
4822 pass_pre (gcc::context *ctxt)
4823 : gimple_opt_pass (pass_data_pre, ctxt)
4826 /* opt_pass methods: */
4827 virtual bool gate (function *) { return flag_tree_pre != 0; }
4828 virtual unsigned int execute (function *);
4830 }; // class pass_pre
4832 unsigned int
4833 pass_pre::execute (function *fun)
4835 unsigned int todo = 0;
4837 do_partial_partial =
4838 flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4840 /* This has to happen before SCCVN runs because
4841 loop_optimizer_init may create new phis, etc. */
4842 loop_optimizer_init (LOOPS_NORMAL);
4844 if (!run_scc_vn (VN_WALK))
4846 loop_optimizer_finalize ();
4847 return 0;
4850 init_pre ();
4851 scev_initialize ();
4853 /* Collect and value number expressions computed in each basic block. */
4854 compute_avail ();
4856 /* Insert can get quite slow on an incredibly large number of basic
4857 blocks due to some quadratic behavior. Until this behavior is
4858 fixed, don't run it when he have an incredibly large number of
4859 bb's. If we aren't going to run insert, there is no point in
4860 computing ANTIC, either, even though it's plenty fast. */
4861 if (n_basic_blocks_for_fn (fun) < 4000)
4863 compute_antic ();
4864 insert ();
4867 /* Make sure to remove fake edges before committing our inserts.
4868 This makes sure we don't end up with extra critical edges that
4869 we would need to split. */
4870 remove_fake_exit_edges ();
4871 gsi_commit_edge_inserts ();
4873 /* Eliminate folds statements which might (should not...) end up
4874 not keeping virtual operands up-to-date. */
4875 gcc_assert (!need_ssa_update_p (fun));
4877 /* Remove all the redundant expressions. */
4878 todo |= eliminate (true);
4880 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4881 statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4882 statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4883 statistics_counter_event (fun, "Eliminated", pre_stats.eliminations);
4885 clear_expression_ids ();
4886 remove_dead_inserted_code ();
4888 scev_finalize ();
4889 fini_pre ();
4890 todo |= fini_eliminate ();
4891 loop_optimizer_finalize ();
4893 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4894 case we can merge the block with the remaining predecessor of the block.
4895 It should either:
4896 - call merge_blocks after each tail merge iteration
4897 - call merge_blocks after all tail merge iterations
4898 - mark TODO_cleanup_cfg when necessary
4899 - share the cfg cleanup with fini_pre. */
4900 todo |= tail_merge_optimize (todo);
4902 free_scc_vn ();
4904 /* Tail merging invalidates the virtual SSA web, together with
4905 cfg-cleanup opportunities exposed by PRE this will wreck the
4906 SSA updating machinery. So make sure to run update-ssa
4907 manually, before eventually scheduling cfg-cleanup as part of
4908 the todo. */
4909 update_ssa (TODO_update_ssa_only_virtuals);
4911 return todo;
4914 } // anon namespace
4916 gimple_opt_pass *
4917 make_pass_pre (gcc::context *ctxt)
4919 return new pass_pre (ctxt);
4922 namespace {
4924 const pass_data pass_data_fre =
4926 GIMPLE_PASS, /* type */
4927 "fre", /* name */
4928 OPTGROUP_NONE, /* optinfo_flags */
4929 TV_TREE_FRE, /* tv_id */
4930 ( PROP_cfg | PROP_ssa ), /* properties_required */
4931 0, /* properties_provided */
4932 0, /* properties_destroyed */
4933 0, /* todo_flags_start */
4934 0, /* todo_flags_finish */
4937 class pass_fre : public gimple_opt_pass
4939 public:
4940 pass_fre (gcc::context *ctxt)
4941 : gimple_opt_pass (pass_data_fre, ctxt)
4944 /* opt_pass methods: */
4945 opt_pass * clone () { return new pass_fre (m_ctxt); }
4946 virtual bool gate (function *) { return flag_tree_fre != 0; }
4947 virtual unsigned int execute (function *);
4949 }; // class pass_fre
4951 unsigned int
4952 pass_fre::execute (function *fun)
4954 unsigned int todo = 0;
4956 if (!run_scc_vn (VN_WALKREWRITE))
4957 return 0;
4959 memset (&pre_stats, 0, sizeof (pre_stats));
4961 /* Remove all the redundant expressions. */
4962 todo |= eliminate (false);
4964 todo |= fini_eliminate ();
4966 free_scc_vn ();
4968 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4969 statistics_counter_event (fun, "Eliminated", pre_stats.eliminations);
4971 return todo;
4974 } // anon namespace
4976 gimple_opt_pass *
4977 make_pass_fre (gcc::context *ctxt)
4979 return new pass_fre (ctxt);