* config/pa/linux-atomic.c (__kernel_cmpxchg): Reorder arguments to
[official-gcc.git] / gcc / tree-ssa-pre.c
blobb4e34774f183ac75d1925995ab36cf977127be94
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 "alias.h"
27 #include "symtab.h"
28 #include "tree.h"
29 #include "fold-const.h"
30 #include "predict.h"
31 #include "hard-reg-set.h"
32 #include "function.h"
33 #include "dominance.h"
34 #include "cfg.h"
35 #include "cfganal.h"
36 #include "basic-block.h"
37 #include "gimple-pretty-print.h"
38 #include "tree-inline.h"
39 #include "tree-ssa-alias.h"
40 #include "internal-fn.h"
41 #include "gimple-fold.h"
42 #include "tree-eh.h"
43 #include "gimple-expr.h"
44 #include "gimple.h"
45 #include "gimplify.h"
46 #include "gimple-iterator.h"
47 #include "gimplify-me.h"
48 #include "gimple-ssa.h"
49 #include "tree-cfg.h"
50 #include "tree-phinodes.h"
51 #include "ssa-iterators.h"
52 #include "stringpool.h"
53 #include "tree-ssanames.h"
54 #include "tree-ssa-loop.h"
55 #include "tree-into-ssa.h"
56 #include "rtl.h"
57 #include "flags.h"
58 #include "insn-config.h"
59 #include "expmed.h"
60 #include "dojump.h"
61 #include "explow.h"
62 #include "calls.h"
63 #include "emit-rtl.h"
64 #include "varasm.h"
65 #include "stmt.h"
66 #include "expr.h"
67 #include "tree-dfa.h"
68 #include "tree-ssa.h"
69 #include "tree-iterator.h"
70 #include "alloc-pool.h"
71 #include "obstack.h"
72 #include "tree-pass.h"
73 #include "langhooks.h"
74 #include "cfgloop.h"
75 #include "tree-ssa-sccvn.h"
76 #include "tree-scalar-evolution.h"
77 #include "params.h"
78 #include "dbgcnt.h"
79 #include "domwalk.h"
80 #include "cgraph.h"
81 #include "symbol-summary.h"
82 #include "ipa-prop.h"
83 #include "tree-ssa-propagate.h"
84 #include "ipa-utils.h"
85 #include "tree-cfgcleanup.h"
87 /* TODO:
89 1. Avail sets can be shared by making an avail_find_leader that
90 walks up the dominator tree and looks in those avail sets.
91 This might affect code optimality, it's unclear right now.
92 2. Strength reduction can be performed by anticipating expressions
93 we can repair later on.
94 3. We can do back-substitution or smarter value numbering to catch
95 commutative expressions split up over multiple statements.
98 /* For ease of terminology, "expression node" in the below refers to
99 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
100 represent the actual statement containing the expressions we care about,
101 and we cache the value number by putting it in the expression. */
103 /* Basic algorithm
105 First we walk the statements to generate the AVAIL sets, the
106 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
107 generation of values/expressions by a given block. We use them
108 when computing the ANTIC sets. The AVAIL sets consist of
109 SSA_NAME's that represent values, so we know what values are
110 available in what blocks. AVAIL is a forward dataflow problem. In
111 SSA, values are never killed, so we don't need a kill set, or a
112 fixpoint iteration, in order to calculate the AVAIL sets. In
113 traditional parlance, AVAIL sets tell us the downsafety of the
114 expressions/values.
116 Next, we generate the ANTIC sets. These sets represent the
117 anticipatable expressions. ANTIC is a backwards dataflow
118 problem. An expression is anticipatable in a given block if it could
119 be generated in that block. This means that if we had to perform
120 an insertion in that block, of the value of that expression, we
121 could. Calculating the ANTIC sets requires phi translation of
122 expressions, because the flow goes backwards through phis. We must
123 iterate to a fixpoint of the ANTIC sets, because we have a kill
124 set. Even in SSA form, values are not live over the entire
125 function, only from their definition point onwards. So we have to
126 remove values from the ANTIC set once we go past the definition
127 point of the leaders that make them up.
128 compute_antic/compute_antic_aux performs this computation.
130 Third, we perform insertions to make partially redundant
131 expressions fully redundant.
133 An expression is partially redundant (excluding partial
134 anticipation) if:
136 1. It is AVAIL in some, but not all, of the predecessors of a
137 given block.
138 2. It is ANTIC in all the predecessors.
140 In order to make it fully redundant, we insert the expression into
141 the predecessors where it is not available, but is ANTIC.
143 For the partial anticipation case, we only perform insertion if it
144 is partially anticipated in some block, and fully available in all
145 of the predecessors.
147 insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
148 performs these steps.
150 Fourth, we eliminate fully redundant expressions.
151 This is a simple statement walk that replaces redundant
152 calculations with the now available values. */
154 /* Representations of value numbers:
156 Value numbers are represented by a representative SSA_NAME. We
157 will create fake SSA_NAME's in situations where we need a
158 representative but do not have one (because it is a complex
159 expression). In order to facilitate storing the value numbers in
160 bitmaps, and keep the number of wasted SSA_NAME's down, we also
161 associate a value_id with each value number, and create full blown
162 ssa_name's only where we actually need them (IE in operands of
163 existing expressions).
165 Theoretically you could replace all the value_id's with
166 SSA_NAME_VERSION, but this would allocate a large number of
167 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
168 It would also require an additional indirection at each point we
169 use the value id. */
171 /* Representation of expressions on value numbers:
173 Expressions consisting of value numbers are represented the same
174 way as our VN internally represents them, with an additional
175 "pre_expr" wrapping around them in order to facilitate storing all
176 of the expressions in the same sets. */
178 /* Representation of sets:
180 The dataflow sets do not need to be sorted in any particular order
181 for the majority of their lifetime, are simply represented as two
182 bitmaps, one that keeps track of values present in the set, and one
183 that keeps track of expressions present in the set.
185 When we need them in topological order, we produce it on demand by
186 transforming the bitmap into an array and sorting it into topo
187 order. */
189 /* Type of expression, used to know which member of the PRE_EXPR union
190 is valid. */
192 enum pre_expr_kind
194 NAME,
195 NARY,
196 REFERENCE,
197 CONSTANT
200 typedef union pre_expr_union_d
202 tree name;
203 tree constant;
204 vn_nary_op_t nary;
205 vn_reference_t reference;
206 } pre_expr_union;
208 typedef struct pre_expr_d : nofree_ptr_hash <pre_expr_d>
210 enum pre_expr_kind kind;
211 unsigned int id;
212 pre_expr_union u;
214 /* hash_table support. */
215 static inline hashval_t hash (const pre_expr_d *);
216 static inline int equal (const pre_expr_d *, const pre_expr_d *);
217 } *pre_expr;
219 #define PRE_EXPR_NAME(e) (e)->u.name
220 #define PRE_EXPR_NARY(e) (e)->u.nary
221 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
222 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
224 /* Compare E1 and E1 for equality. */
226 inline int
227 pre_expr_d::equal (const pre_expr_d *e1, const pre_expr_d *e2)
229 if (e1->kind != e2->kind)
230 return false;
232 switch (e1->kind)
234 case CONSTANT:
235 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
236 PRE_EXPR_CONSTANT (e2));
237 case NAME:
238 return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
239 case NARY:
240 return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
241 case REFERENCE:
242 return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
243 PRE_EXPR_REFERENCE (e2));
244 default:
245 gcc_unreachable ();
249 /* Hash E. */
251 inline hashval_t
252 pre_expr_d::hash (const pre_expr_d *e)
254 switch (e->kind)
256 case CONSTANT:
257 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
258 case NAME:
259 return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
260 case NARY:
261 return PRE_EXPR_NARY (e)->hashcode;
262 case REFERENCE:
263 return PRE_EXPR_REFERENCE (e)->hashcode;
264 default:
265 gcc_unreachable ();
269 /* Next global expression id number. */
270 static unsigned int next_expression_id;
272 /* Mapping from expression to id number we can use in bitmap sets. */
273 static vec<pre_expr> expressions;
274 static hash_table<pre_expr_d> *expression_to_id;
275 static vec<unsigned> name_to_id;
277 /* Allocate an expression id for EXPR. */
279 static inline unsigned int
280 alloc_expression_id (pre_expr expr)
282 struct pre_expr_d **slot;
283 /* Make sure we won't overflow. */
284 gcc_assert (next_expression_id + 1 > next_expression_id);
285 expr->id = next_expression_id++;
286 expressions.safe_push (expr);
287 if (expr->kind == NAME)
289 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
290 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
291 re-allocations by using vec::reserve upfront. */
292 unsigned old_len = name_to_id.length ();
293 name_to_id.reserve (num_ssa_names - old_len);
294 name_to_id.quick_grow_cleared (num_ssa_names);
295 gcc_assert (name_to_id[version] == 0);
296 name_to_id[version] = expr->id;
298 else
300 slot = expression_to_id->find_slot (expr, INSERT);
301 gcc_assert (!*slot);
302 *slot = expr;
304 return next_expression_id - 1;
307 /* Return the expression id for tree EXPR. */
309 static inline unsigned int
310 get_expression_id (const pre_expr expr)
312 return expr->id;
315 static inline unsigned int
316 lookup_expression_id (const pre_expr expr)
318 struct pre_expr_d **slot;
320 if (expr->kind == NAME)
322 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
323 if (name_to_id.length () <= version)
324 return 0;
325 return name_to_id[version];
327 else
329 slot = expression_to_id->find_slot (expr, NO_INSERT);
330 if (!slot)
331 return 0;
332 return ((pre_expr)*slot)->id;
336 /* Return the existing expression id for EXPR, or create one if one
337 does not exist yet. */
339 static inline unsigned int
340 get_or_alloc_expression_id (pre_expr expr)
342 unsigned int id = lookup_expression_id (expr);
343 if (id == 0)
344 return alloc_expression_id (expr);
345 return expr->id = id;
348 /* Return the expression that has expression id ID */
350 static inline pre_expr
351 expression_for_id (unsigned int id)
353 return expressions[id];
356 /* Free the expression id field in all of our expressions,
357 and then destroy the expressions array. */
359 static void
360 clear_expression_ids (void)
362 expressions.release ();
365 static pool_allocator<pre_expr_d> pre_expr_pool ("pre_expr nodes", 30);
367 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
369 static pre_expr
370 get_or_alloc_expr_for_name (tree name)
372 struct pre_expr_d expr;
373 pre_expr result;
374 unsigned int result_id;
376 expr.kind = NAME;
377 expr.id = 0;
378 PRE_EXPR_NAME (&expr) = name;
379 result_id = lookup_expression_id (&expr);
380 if (result_id != 0)
381 return expression_for_id (result_id);
383 result = pre_expr_pool.allocate ();
384 result->kind = NAME;
385 PRE_EXPR_NAME (result) = name;
386 alloc_expression_id (result);
387 return result;
390 /* An unordered bitmap set. One bitmap tracks values, the other,
391 expressions. */
392 typedef struct bitmap_set
394 bitmap_head expressions;
395 bitmap_head values;
396 } *bitmap_set_t;
398 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
399 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
401 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
402 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
404 /* Mapping from value id to expressions with that value_id. */
405 static vec<bitmap> value_expressions;
407 /* Sets that we need to keep track of. */
408 typedef struct bb_bitmap_sets
410 /* The EXP_GEN set, which represents expressions/values generated in
411 a basic block. */
412 bitmap_set_t exp_gen;
414 /* The PHI_GEN set, which represents PHI results generated in a
415 basic block. */
416 bitmap_set_t phi_gen;
418 /* The TMP_GEN set, which represents results/temporaries generated
419 in a basic block. IE the LHS of an expression. */
420 bitmap_set_t tmp_gen;
422 /* The AVAIL_OUT set, which represents which values are available in
423 a given basic block. */
424 bitmap_set_t avail_out;
426 /* The ANTIC_IN set, which represents which values are anticipatable
427 in a given basic block. */
428 bitmap_set_t antic_in;
430 /* The PA_IN set, which represents which values are
431 partially anticipatable in a given basic block. */
432 bitmap_set_t pa_in;
434 /* The NEW_SETS set, which is used during insertion to augment the
435 AVAIL_OUT set of blocks with the new insertions performed during
436 the current iteration. */
437 bitmap_set_t new_sets;
439 /* A cache for value_dies_in_block_x. */
440 bitmap expr_dies;
442 /* The live virtual operand on successor edges. */
443 tree vop_on_exit;
445 /* True if we have visited this block during ANTIC calculation. */
446 unsigned int visited : 1;
448 /* True when the block contains a call that might not return. */
449 unsigned int contains_may_not_return_call : 1;
450 } *bb_value_sets_t;
452 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
453 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
454 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
455 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
456 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
457 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
458 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
459 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
460 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
461 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
462 #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
465 /* Basic block list in postorder. */
466 static int *postorder;
467 static int postorder_num;
469 /* This structure is used to keep track of statistics on what
470 optimization PRE was able to perform. */
471 static struct
473 /* The number of RHS computations eliminated by PRE. */
474 int eliminations;
476 /* The number of new expressions/temporaries generated by PRE. */
477 int insertions;
479 /* The number of inserts found due to partial anticipation */
480 int pa_insert;
482 /* The number of new PHI nodes added by PRE. */
483 int phis;
484 } pre_stats;
486 static bool do_partial_partial;
487 static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
488 static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
489 static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
490 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
491 static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
492 static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
493 static void bitmap_insert_into_set_1 (bitmap_set_t, pre_expr,
494 unsigned int, bool);
495 static bitmap_set_t bitmap_set_new (void);
496 static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
497 tree);
498 static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
499 static unsigned int get_expr_value_id (pre_expr);
501 /* We can add and remove elements and entries to and from sets
502 and hash tables, so we use alloc pools for them. */
504 static pool_allocator<bitmap_set> bitmap_set_pool ("Bitmap sets", 30);
505 static bitmap_obstack grand_bitmap_obstack;
507 /* Set of blocks with statements that have had their EH properties changed. */
508 static bitmap need_eh_cleanup;
510 /* Set of blocks with statements that have had their AB properties changed. */
511 static bitmap need_ab_cleanup;
513 /* A three tuple {e, pred, v} used to cache phi translations in the
514 phi_translate_table. */
516 typedef struct expr_pred_trans_d : free_ptr_hash<expr_pred_trans_d>
518 /* The expression. */
519 pre_expr e;
521 /* The predecessor block along which we translated the expression. */
522 basic_block pred;
524 /* The value that resulted from the translation. */
525 pre_expr v;
527 /* The hashcode for the expression, pred pair. This is cached for
528 speed reasons. */
529 hashval_t hashcode;
531 /* hash_table support. */
532 static inline hashval_t hash (const expr_pred_trans_d *);
533 static inline int equal (const expr_pred_trans_d *, const expr_pred_trans_d *);
534 } *expr_pred_trans_t;
535 typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
537 inline hashval_t
538 expr_pred_trans_d::hash (const expr_pred_trans_d *e)
540 return e->hashcode;
543 inline int
544 expr_pred_trans_d::equal (const expr_pred_trans_d *ve1,
545 const expr_pred_trans_d *ve2)
547 basic_block b1 = ve1->pred;
548 basic_block b2 = ve2->pred;
550 /* If they are not translations for the same basic block, they can't
551 be equal. */
552 if (b1 != b2)
553 return false;
554 return pre_expr_d::equal (ve1->e, ve2->e);
557 /* The phi_translate_table caches phi translations for a given
558 expression and predecessor. */
559 static hash_table<expr_pred_trans_d> *phi_translate_table;
561 /* Add the tuple mapping from {expression E, basic block PRED} to
562 the phi translation table and return whether it pre-existed. */
564 static inline bool
565 phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
567 expr_pred_trans_t *slot;
568 expr_pred_trans_d tem;
569 hashval_t hash = iterative_hash_hashval_t (pre_expr_d::hash (e),
570 pred->index);
571 tem.e = e;
572 tem.pred = pred;
573 tem.hashcode = hash;
574 slot = phi_translate_table->find_slot_with_hash (&tem, hash, INSERT);
575 if (*slot)
577 *entry = *slot;
578 return true;
581 *entry = *slot = XNEW (struct expr_pred_trans_d);
582 (*entry)->e = e;
583 (*entry)->pred = pred;
584 (*entry)->hashcode = hash;
585 return false;
589 /* Add expression E to the expression set of value id V. */
591 static void
592 add_to_value (unsigned int v, pre_expr e)
594 bitmap set;
596 gcc_checking_assert (get_expr_value_id (e) == v);
598 if (v >= value_expressions.length ())
600 value_expressions.safe_grow_cleared (v + 1);
603 set = value_expressions[v];
604 if (!set)
606 set = BITMAP_ALLOC (&grand_bitmap_obstack);
607 value_expressions[v] = set;
610 bitmap_set_bit (set, get_or_alloc_expression_id (e));
613 /* Create a new bitmap set and return it. */
615 static bitmap_set_t
616 bitmap_set_new (void)
618 bitmap_set_t ret = bitmap_set_pool.allocate ();
619 bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
620 bitmap_initialize (&ret->values, &grand_bitmap_obstack);
621 return ret;
624 /* Return the value id for a PRE expression EXPR. */
626 static unsigned int
627 get_expr_value_id (pre_expr expr)
629 unsigned int id;
630 switch (expr->kind)
632 case CONSTANT:
633 id = get_constant_value_id (PRE_EXPR_CONSTANT (expr));
634 break;
635 case NAME:
636 id = VN_INFO (PRE_EXPR_NAME (expr))->value_id;
637 break;
638 case NARY:
639 id = PRE_EXPR_NARY (expr)->value_id;
640 break;
641 case REFERENCE:
642 id = PRE_EXPR_REFERENCE (expr)->value_id;
643 break;
644 default:
645 gcc_unreachable ();
647 /* ??? We cannot assert that expr has a value-id (it can be 0), because
648 we assign value-ids only to expressions that have a result
649 in set_hashtable_value_ids. */
650 return id;
653 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
655 static tree
656 sccvn_valnum_from_value_id (unsigned int val)
658 bitmap_iterator bi;
659 unsigned int i;
660 bitmap exprset = value_expressions[val];
661 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
663 pre_expr vexpr = expression_for_id (i);
664 if (vexpr->kind == NAME)
665 return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
666 else if (vexpr->kind == CONSTANT)
667 return PRE_EXPR_CONSTANT (vexpr);
669 return NULL_TREE;
672 /* Remove an expression EXPR from a bitmapped set. */
674 static void
675 bitmap_remove_from_set (bitmap_set_t set, pre_expr expr)
677 unsigned int val = get_expr_value_id (expr);
678 if (!value_id_constant_p (val))
680 bitmap_clear_bit (&set->values, val);
681 bitmap_clear_bit (&set->expressions, get_expression_id (expr));
685 static void
686 bitmap_insert_into_set_1 (bitmap_set_t set, pre_expr expr,
687 unsigned int val, bool allow_constants)
689 if (allow_constants || !value_id_constant_p (val))
691 /* We specifically expect this and only this function to be able to
692 insert constants into a set. */
693 bitmap_set_bit (&set->values, val);
694 bitmap_set_bit (&set->expressions, get_or_alloc_expression_id (expr));
698 /* Insert an expression EXPR into a bitmapped set. */
700 static void
701 bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
703 bitmap_insert_into_set_1 (set, expr, get_expr_value_id (expr), false);
706 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
708 static void
709 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
711 bitmap_copy (&dest->expressions, &orig->expressions);
712 bitmap_copy (&dest->values, &orig->values);
716 /* Free memory used up by SET. */
717 static void
718 bitmap_set_free (bitmap_set_t set)
720 bitmap_clear (&set->expressions);
721 bitmap_clear (&set->values);
725 /* Generate an topological-ordered array of bitmap set SET. */
727 static vec<pre_expr>
728 sorted_array_from_bitmap_set (bitmap_set_t set)
730 unsigned int i, j;
731 bitmap_iterator bi, bj;
732 vec<pre_expr> result;
734 /* Pre-allocate enough space for the array. */
735 result.create (bitmap_count_bits (&set->expressions));
737 FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
739 /* The number of expressions having a given value is usually
740 relatively small. Thus, rather than making a vector of all
741 the expressions and sorting it by value-id, we walk the values
742 and check in the reverse mapping that tells us what expressions
743 have a given value, to filter those in our set. As a result,
744 the expressions are inserted in value-id order, which means
745 topological order.
747 If this is somehow a significant lose for some cases, we can
748 choose which set to walk based on the set size. */
749 bitmap exprset = value_expressions[i];
750 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj)
752 if (bitmap_bit_p (&set->expressions, j))
753 result.quick_push (expression_for_id (j));
757 return result;
760 /* Perform bitmapped set operation DEST &= ORIG. */
762 static void
763 bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig)
765 bitmap_iterator bi;
766 unsigned int i;
768 if (dest != orig)
770 bitmap_head temp;
771 bitmap_initialize (&temp, &grand_bitmap_obstack);
773 bitmap_and_into (&dest->values, &orig->values);
774 bitmap_copy (&temp, &dest->expressions);
775 EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
777 pre_expr expr = expression_for_id (i);
778 unsigned int value_id = get_expr_value_id (expr);
779 if (!bitmap_bit_p (&dest->values, value_id))
780 bitmap_clear_bit (&dest->expressions, i);
782 bitmap_clear (&temp);
786 /* Subtract all values and expressions contained in ORIG from DEST. */
788 static bitmap_set_t
789 bitmap_set_subtract (bitmap_set_t dest, bitmap_set_t orig)
791 bitmap_set_t result = bitmap_set_new ();
792 bitmap_iterator bi;
793 unsigned int i;
795 bitmap_and_compl (&result->expressions, &dest->expressions,
796 &orig->expressions);
798 FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
800 pre_expr expr = expression_for_id (i);
801 unsigned int value_id = get_expr_value_id (expr);
802 bitmap_set_bit (&result->values, value_id);
805 return result;
808 /* Subtract all the values in bitmap set B from bitmap set A. */
810 static void
811 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
813 unsigned int i;
814 bitmap_iterator bi;
815 bitmap_head temp;
817 bitmap_initialize (&temp, &grand_bitmap_obstack);
819 bitmap_copy (&temp, &a->expressions);
820 EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
822 pre_expr expr = expression_for_id (i);
823 if (bitmap_set_contains_value (b, get_expr_value_id (expr)))
824 bitmap_remove_from_set (a, expr);
826 bitmap_clear (&temp);
830 /* Return true if bitmapped set SET contains the value VALUE_ID. */
832 static bool
833 bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
835 if (value_id_constant_p (value_id))
836 return true;
838 if (!set || bitmap_empty_p (&set->expressions))
839 return false;
841 return bitmap_bit_p (&set->values, value_id);
844 static inline bool
845 bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr)
847 return bitmap_bit_p (&set->expressions, get_expression_id (expr));
850 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
852 static void
853 bitmap_set_replace_value (bitmap_set_t set, unsigned int lookfor,
854 const pre_expr expr)
856 bitmap exprset;
857 unsigned int i;
858 bitmap_iterator bi;
860 if (value_id_constant_p (lookfor))
861 return;
863 if (!bitmap_set_contains_value (set, lookfor))
864 return;
866 /* The number of expressions having a given value is usually
867 significantly less than the total number of expressions in SET.
868 Thus, rather than check, for each expression in SET, whether it
869 has the value LOOKFOR, we walk the reverse mapping that tells us
870 what expressions have a given value, and see if any of those
871 expressions are in our set. For large testcases, this is about
872 5-10x faster than walking the bitmap. If this is somehow a
873 significant lose for some cases, we can choose which set to walk
874 based on the set size. */
875 exprset = value_expressions[lookfor];
876 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
878 if (bitmap_clear_bit (&set->expressions, i))
880 bitmap_set_bit (&set->expressions, get_expression_id (expr));
881 return;
885 gcc_unreachable ();
888 /* Return true if two bitmap sets are equal. */
890 static bool
891 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
893 return bitmap_equal_p (&a->values, &b->values);
896 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
897 and add it otherwise. */
899 static void
900 bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
902 unsigned int val = get_expr_value_id (expr);
904 if (bitmap_set_contains_value (set, val))
905 bitmap_set_replace_value (set, val, expr);
906 else
907 bitmap_insert_into_set (set, expr);
910 /* Insert EXPR into SET if EXPR's value is not already present in
911 SET. */
913 static void
914 bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
916 unsigned int val = get_expr_value_id (expr);
918 gcc_checking_assert (expr->id == get_or_alloc_expression_id (expr));
920 /* Constant values are always considered to be part of the set. */
921 if (value_id_constant_p (val))
922 return;
924 /* If the value membership changed, add the expression. */
925 if (bitmap_set_bit (&set->values, val))
926 bitmap_set_bit (&set->expressions, expr->id);
929 /* Print out EXPR to outfile. */
931 static void
932 print_pre_expr (FILE *outfile, const pre_expr expr)
934 switch (expr->kind)
936 case CONSTANT:
937 print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr), 0);
938 break;
939 case NAME:
940 print_generic_expr (outfile, PRE_EXPR_NAME (expr), 0);
941 break;
942 case NARY:
944 unsigned int i;
945 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
946 fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
947 for (i = 0; i < nary->length; i++)
949 print_generic_expr (outfile, nary->op[i], 0);
950 if (i != (unsigned) nary->length - 1)
951 fprintf (outfile, ",");
953 fprintf (outfile, "}");
955 break;
957 case REFERENCE:
959 vn_reference_op_t vro;
960 unsigned int i;
961 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
962 fprintf (outfile, "{");
963 for (i = 0;
964 ref->operands.iterate (i, &vro);
965 i++)
967 bool closebrace = false;
968 if (vro->opcode != SSA_NAME
969 && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
971 fprintf (outfile, "%s", get_tree_code_name (vro->opcode));
972 if (vro->op0)
974 fprintf (outfile, "<");
975 closebrace = true;
978 if (vro->op0)
980 print_generic_expr (outfile, vro->op0, 0);
981 if (vro->op1)
983 fprintf (outfile, ",");
984 print_generic_expr (outfile, vro->op1, 0);
986 if (vro->op2)
988 fprintf (outfile, ",");
989 print_generic_expr (outfile, vro->op2, 0);
992 if (closebrace)
993 fprintf (outfile, ">");
994 if (i != ref->operands.length () - 1)
995 fprintf (outfile, ",");
997 fprintf (outfile, "}");
998 if (ref->vuse)
1000 fprintf (outfile, "@");
1001 print_generic_expr (outfile, ref->vuse, 0);
1004 break;
1007 void debug_pre_expr (pre_expr);
1009 /* Like print_pre_expr but always prints to stderr. */
1010 DEBUG_FUNCTION void
1011 debug_pre_expr (pre_expr e)
1013 print_pre_expr (stderr, e);
1014 fprintf (stderr, "\n");
1017 /* Print out SET to OUTFILE. */
1019 static void
1020 print_bitmap_set (FILE *outfile, bitmap_set_t set,
1021 const char *setname, int blockindex)
1023 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
1024 if (set)
1026 bool first = true;
1027 unsigned i;
1028 bitmap_iterator bi;
1030 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1032 const pre_expr expr = expression_for_id (i);
1034 if (!first)
1035 fprintf (outfile, ", ");
1036 first = false;
1037 print_pre_expr (outfile, expr);
1039 fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1042 fprintf (outfile, " }\n");
1045 void debug_bitmap_set (bitmap_set_t);
1047 DEBUG_FUNCTION void
1048 debug_bitmap_set (bitmap_set_t set)
1050 print_bitmap_set (stderr, set, "debug", 0);
1053 void debug_bitmap_sets_for (basic_block);
1055 DEBUG_FUNCTION void
1056 debug_bitmap_sets_for (basic_block bb)
1058 print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1059 print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1060 print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1061 print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1062 print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1063 if (do_partial_partial)
1064 print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1065 print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1068 /* Print out the expressions that have VAL to OUTFILE. */
1070 static void
1071 print_value_expressions (FILE *outfile, unsigned int val)
1073 bitmap set = value_expressions[val];
1074 if (set)
1076 bitmap_set x;
1077 char s[10];
1078 sprintf (s, "%04d", val);
1079 x.expressions = *set;
1080 print_bitmap_set (outfile, &x, s, 0);
1085 DEBUG_FUNCTION void
1086 debug_value_expressions (unsigned int val)
1088 print_value_expressions (stderr, val);
1091 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1092 represent it. */
1094 static pre_expr
1095 get_or_alloc_expr_for_constant (tree constant)
1097 unsigned int result_id;
1098 unsigned int value_id;
1099 struct pre_expr_d expr;
1100 pre_expr newexpr;
1102 expr.kind = CONSTANT;
1103 PRE_EXPR_CONSTANT (&expr) = constant;
1104 result_id = lookup_expression_id (&expr);
1105 if (result_id != 0)
1106 return expression_for_id (result_id);
1108 newexpr = pre_expr_pool.allocate ();
1109 newexpr->kind = CONSTANT;
1110 PRE_EXPR_CONSTANT (newexpr) = constant;
1111 alloc_expression_id (newexpr);
1112 value_id = get_or_alloc_constant_value_id (constant);
1113 add_to_value (value_id, newexpr);
1114 return newexpr;
1117 /* Given a value id V, find the actual tree representing the constant
1118 value if there is one, and return it. Return NULL if we can't find
1119 a constant. */
1121 static tree
1122 get_constant_for_value_id (unsigned int v)
1124 if (value_id_constant_p (v))
1126 unsigned int i;
1127 bitmap_iterator bi;
1128 bitmap exprset = value_expressions[v];
1130 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1132 pre_expr expr = expression_for_id (i);
1133 if (expr->kind == CONSTANT)
1134 return PRE_EXPR_CONSTANT (expr);
1137 return NULL;
1140 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1141 Currently only supports constants and SSA_NAMES. */
1142 static pre_expr
1143 get_or_alloc_expr_for (tree t)
1145 if (TREE_CODE (t) == SSA_NAME)
1146 return get_or_alloc_expr_for_name (t);
1147 else if (is_gimple_min_invariant (t))
1148 return get_or_alloc_expr_for_constant (t);
1149 else
1151 /* More complex expressions can result from SCCVN expression
1152 simplification that inserts values for them. As they all
1153 do not have VOPs the get handled by the nary ops struct. */
1154 vn_nary_op_t result;
1155 unsigned int result_id;
1156 vn_nary_op_lookup (t, &result);
1157 if (result != NULL)
1159 pre_expr e = pre_expr_pool.allocate ();
1160 e->kind = NARY;
1161 PRE_EXPR_NARY (e) = result;
1162 result_id = lookup_expression_id (e);
1163 if (result_id != 0)
1165 pre_expr_pool.remove (e);
1166 e = expression_for_id (result_id);
1167 return e;
1169 alloc_expression_id (e);
1170 return e;
1173 return NULL;
1176 /* Return the folded version of T if T, when folded, is a gimple
1177 min_invariant. Otherwise, return T. */
1179 static pre_expr
1180 fully_constant_expression (pre_expr e)
1182 switch (e->kind)
1184 case CONSTANT:
1185 return e;
1186 case NARY:
1188 vn_nary_op_t nary = PRE_EXPR_NARY (e);
1189 switch (TREE_CODE_CLASS (nary->opcode))
1191 case tcc_binary:
1192 case tcc_comparison:
1194 /* We have to go from trees to pre exprs to value ids to
1195 constants. */
1196 tree naryop0 = nary->op[0];
1197 tree naryop1 = nary->op[1];
1198 tree result;
1199 if (!is_gimple_min_invariant (naryop0))
1201 pre_expr rep0 = get_or_alloc_expr_for (naryop0);
1202 unsigned int vrep0 = get_expr_value_id (rep0);
1203 tree const0 = get_constant_for_value_id (vrep0);
1204 if (const0)
1205 naryop0 = fold_convert (TREE_TYPE (naryop0), const0);
1207 if (!is_gimple_min_invariant (naryop1))
1209 pre_expr rep1 = get_or_alloc_expr_for (naryop1);
1210 unsigned int vrep1 = get_expr_value_id (rep1);
1211 tree const1 = get_constant_for_value_id (vrep1);
1212 if (const1)
1213 naryop1 = fold_convert (TREE_TYPE (naryop1), const1);
1215 result = fold_binary (nary->opcode, nary->type,
1216 naryop0, naryop1);
1217 if (result && is_gimple_min_invariant (result))
1218 return get_or_alloc_expr_for_constant (result);
1219 /* We might have simplified the expression to a
1220 SSA_NAME for example from x_1 * 1. But we cannot
1221 insert a PHI for x_1 unconditionally as x_1 might
1222 not be available readily. */
1223 return e;
1225 case tcc_reference:
1226 if (nary->opcode != REALPART_EXPR
1227 && nary->opcode != IMAGPART_EXPR
1228 && nary->opcode != VIEW_CONVERT_EXPR)
1229 return e;
1230 /* Fallthrough. */
1231 case tcc_unary:
1233 /* We have to go from trees to pre exprs to value ids to
1234 constants. */
1235 tree naryop0 = nary->op[0];
1236 tree const0, result;
1237 if (is_gimple_min_invariant (naryop0))
1238 const0 = naryop0;
1239 else
1241 pre_expr rep0 = get_or_alloc_expr_for (naryop0);
1242 unsigned int vrep0 = get_expr_value_id (rep0);
1243 const0 = get_constant_for_value_id (vrep0);
1245 result = NULL;
1246 if (const0)
1248 tree type1 = TREE_TYPE (nary->op[0]);
1249 const0 = fold_convert (type1, const0);
1250 result = fold_unary (nary->opcode, nary->type, const0);
1252 if (result && is_gimple_min_invariant (result))
1253 return get_or_alloc_expr_for_constant (result);
1254 return e;
1256 default:
1257 return e;
1260 case REFERENCE:
1262 vn_reference_t ref = PRE_EXPR_REFERENCE (e);
1263 tree folded;
1264 if ((folded = fully_constant_vn_reference_p (ref)))
1265 return get_or_alloc_expr_for_constant (folded);
1266 return e;
1268 default:
1269 return e;
1271 return e;
1274 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1275 it has the value it would have in BLOCK. Set *SAME_VALID to true
1276 in case the new vuse doesn't change the value id of the OPERANDS. */
1278 static tree
1279 translate_vuse_through_block (vec<vn_reference_op_s> operands,
1280 alias_set_type set, tree type, tree vuse,
1281 basic_block phiblock,
1282 basic_block block, bool *same_valid)
1284 gimple phi = SSA_NAME_DEF_STMT (vuse);
1285 ao_ref ref;
1286 edge e = NULL;
1287 bool use_oracle;
1289 *same_valid = true;
1291 if (gimple_bb (phi) != phiblock)
1292 return vuse;
1294 use_oracle = ao_ref_init_from_vn_reference (&ref, set, type, operands);
1296 /* Use the alias-oracle to find either the PHI node in this block,
1297 the first VUSE used in this block that is equivalent to vuse or
1298 the first VUSE which definition in this block kills the value. */
1299 if (gimple_code (phi) == GIMPLE_PHI)
1300 e = find_edge (block, phiblock);
1301 else if (use_oracle)
1302 while (!stmt_may_clobber_ref_p_1 (phi, &ref))
1304 vuse = gimple_vuse (phi);
1305 phi = SSA_NAME_DEF_STMT (vuse);
1306 if (gimple_bb (phi) != phiblock)
1307 return vuse;
1308 if (gimple_code (phi) == GIMPLE_PHI)
1310 e = find_edge (block, phiblock);
1311 break;
1314 else
1315 return NULL_TREE;
1317 if (e)
1319 if (use_oracle)
1321 bitmap visited = NULL;
1322 unsigned int cnt;
1323 /* Try to find a vuse that dominates this phi node by skipping
1324 non-clobbering statements. */
1325 vuse = get_continuation_for_phi (phi, &ref, &cnt, &visited, false,
1326 NULL, NULL);
1327 if (visited)
1328 BITMAP_FREE (visited);
1330 else
1331 vuse = NULL_TREE;
1332 if (!vuse)
1334 /* If we didn't find any, the value ID can't stay the same,
1335 but return the translated vuse. */
1336 *same_valid = false;
1337 vuse = PHI_ARG_DEF (phi, e->dest_idx);
1339 /* ??? We would like to return vuse here as this is the canonical
1340 upmost vdef that this reference is associated with. But during
1341 insertion of the references into the hash tables we only ever
1342 directly insert with their direct gimple_vuse, hence returning
1343 something else would make us not find the other expression. */
1344 return PHI_ARG_DEF (phi, e->dest_idx);
1347 return NULL_TREE;
1350 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1351 SET2. This is used to avoid making a set consisting of the union
1352 of PA_IN and ANTIC_IN during insert. */
1354 static inline pre_expr
1355 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2)
1357 pre_expr result;
1359 result = bitmap_find_leader (set1, val);
1360 if (!result && set2)
1361 result = bitmap_find_leader (set2, val);
1362 return result;
1365 /* Get the tree type for our PRE expression e. */
1367 static tree
1368 get_expr_type (const pre_expr e)
1370 switch (e->kind)
1372 case NAME:
1373 return TREE_TYPE (PRE_EXPR_NAME (e));
1374 case CONSTANT:
1375 return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1376 case REFERENCE:
1377 return PRE_EXPR_REFERENCE (e)->type;
1378 case NARY:
1379 return PRE_EXPR_NARY (e)->type;
1381 gcc_unreachable ();
1384 /* Get a representative SSA_NAME for a given expression.
1385 Since all of our sub-expressions are treated as values, we require
1386 them to be SSA_NAME's for simplicity.
1387 Prior versions of GVNPRE used to use "value handles" here, so that
1388 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1389 either case, the operands are really values (IE we do not expect
1390 them to be usable without finding leaders). */
1392 static tree
1393 get_representative_for (const pre_expr e)
1395 tree name;
1396 unsigned int value_id = get_expr_value_id (e);
1398 switch (e->kind)
1400 case NAME:
1401 return PRE_EXPR_NAME (e);
1402 case CONSTANT:
1403 return PRE_EXPR_CONSTANT (e);
1404 case NARY:
1405 case REFERENCE:
1407 /* Go through all of the expressions representing this value
1408 and pick out an SSA_NAME. */
1409 unsigned int i;
1410 bitmap_iterator bi;
1411 bitmap exprs = value_expressions[value_id];
1412 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1414 pre_expr rep = expression_for_id (i);
1415 if (rep->kind == NAME)
1416 return PRE_EXPR_NAME (rep);
1417 else if (rep->kind == CONSTANT)
1418 return PRE_EXPR_CONSTANT (rep);
1421 break;
1424 /* If we reached here we couldn't find an SSA_NAME. This can
1425 happen when we've discovered a value that has never appeared in
1426 the program as set to an SSA_NAME, as the result of phi translation.
1427 Create one here.
1428 ??? We should be able to re-use this when we insert the statement
1429 to compute it. */
1430 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1431 VN_INFO_GET (name)->value_id = value_id;
1432 VN_INFO (name)->valnum = name;
1433 /* ??? For now mark this SSA name for release by SCCVN. */
1434 VN_INFO (name)->needs_insertion = true;
1435 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1436 if (dump_file && (dump_flags & TDF_DETAILS))
1438 fprintf (dump_file, "Created SSA_NAME representative ");
1439 print_generic_expr (dump_file, name, 0);
1440 fprintf (dump_file, " for expression:");
1441 print_pre_expr (dump_file, e);
1442 fprintf (dump_file, " (%04d)\n", value_id);
1445 return name;
1450 static pre_expr
1451 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1452 basic_block pred, basic_block phiblock);
1454 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1455 the phis in PRED. Return NULL if we can't find a leader for each part
1456 of the translated expression. */
1458 static pre_expr
1459 phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1460 basic_block pred, basic_block phiblock)
1462 switch (expr->kind)
1464 case NARY:
1466 unsigned int i;
1467 bool changed = false;
1468 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1469 vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1470 sizeof_vn_nary_op (nary->length));
1471 memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1473 for (i = 0; i < newnary->length; i++)
1475 if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1476 continue;
1477 else
1479 pre_expr leader, result;
1480 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1481 leader = find_leader_in_sets (op_val_id, set1, set2);
1482 result = phi_translate (leader, set1, set2, pred, phiblock);
1483 if (result && result != leader)
1485 tree name = get_representative_for (result);
1486 if (!name)
1487 return NULL;
1488 newnary->op[i] = name;
1490 else if (!result)
1491 return NULL;
1493 changed |= newnary->op[i] != nary->op[i];
1496 if (changed)
1498 pre_expr constant;
1499 unsigned int new_val_id;
1501 tree result = vn_nary_op_lookup_pieces (newnary->length,
1502 newnary->opcode,
1503 newnary->type,
1504 &newnary->op[0],
1505 &nary);
1506 if (result && is_gimple_min_invariant (result))
1507 return get_or_alloc_expr_for_constant (result);
1509 expr = pre_expr_pool.allocate ();
1510 expr->kind = NARY;
1511 expr->id = 0;
1512 if (nary)
1514 PRE_EXPR_NARY (expr) = nary;
1515 constant = fully_constant_expression (expr);
1516 if (constant != expr)
1517 return constant;
1519 new_val_id = nary->value_id;
1520 get_or_alloc_expression_id (expr);
1522 else
1524 new_val_id = get_next_value_id ();
1525 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
1526 nary = vn_nary_op_insert_pieces (newnary->length,
1527 newnary->opcode,
1528 newnary->type,
1529 &newnary->op[0],
1530 result, new_val_id);
1531 PRE_EXPR_NARY (expr) = nary;
1532 constant = fully_constant_expression (expr);
1533 if (constant != expr)
1534 return constant;
1535 get_or_alloc_expression_id (expr);
1537 add_to_value (new_val_id, expr);
1539 return expr;
1541 break;
1543 case REFERENCE:
1545 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1546 vec<vn_reference_op_s> operands = ref->operands;
1547 tree vuse = ref->vuse;
1548 tree newvuse = vuse;
1549 vec<vn_reference_op_s> newoperands = vNULL;
1550 bool changed = false, same_valid = true;
1551 unsigned int i, n;
1552 vn_reference_op_t operand;
1553 vn_reference_t newref;
1555 for (i = 0; operands.iterate (i, &operand); i++)
1557 pre_expr opresult;
1558 pre_expr leader;
1559 tree op[3];
1560 tree type = operand->type;
1561 vn_reference_op_s newop = *operand;
1562 op[0] = operand->op0;
1563 op[1] = operand->op1;
1564 op[2] = operand->op2;
1565 for (n = 0; n < 3; ++n)
1567 unsigned int op_val_id;
1568 if (!op[n])
1569 continue;
1570 if (TREE_CODE (op[n]) != SSA_NAME)
1572 /* We can't possibly insert these. */
1573 if (n != 0
1574 && !is_gimple_min_invariant (op[n]))
1575 break;
1576 continue;
1578 op_val_id = VN_INFO (op[n])->value_id;
1579 leader = find_leader_in_sets (op_val_id, set1, set2);
1580 if (!leader)
1581 break;
1582 opresult = phi_translate (leader, set1, set2, pred, phiblock);
1583 if (!opresult)
1584 break;
1585 if (opresult != leader)
1587 tree name = get_representative_for (opresult);
1588 if (!name)
1589 break;
1590 changed |= name != op[n];
1591 op[n] = name;
1594 if (n != 3)
1596 newoperands.release ();
1597 return NULL;
1599 if (!changed)
1600 continue;
1601 if (!newoperands.exists ())
1602 newoperands = operands.copy ();
1603 /* We may have changed from an SSA_NAME to a constant */
1604 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1605 newop.opcode = TREE_CODE (op[0]);
1606 newop.type = type;
1607 newop.op0 = op[0];
1608 newop.op1 = op[1];
1609 newop.op2 = op[2];
1610 newoperands[i] = newop;
1612 gcc_checking_assert (i == operands.length ());
1614 if (vuse)
1616 newvuse = translate_vuse_through_block (newoperands.exists ()
1617 ? newoperands : operands,
1618 ref->set, ref->type,
1619 vuse, phiblock, pred,
1620 &same_valid);
1621 if (newvuse == NULL_TREE)
1623 newoperands.release ();
1624 return NULL;
1628 if (changed || newvuse != vuse)
1630 unsigned int new_val_id;
1631 pre_expr constant;
1633 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1634 ref->type,
1635 newoperands.exists ()
1636 ? newoperands : operands,
1637 &newref, VN_WALK);
1638 if (result)
1639 newoperands.release ();
1641 /* We can always insert constants, so if we have a partial
1642 redundant constant load of another type try to translate it
1643 to a constant of appropriate type. */
1644 if (result && is_gimple_min_invariant (result))
1646 tree tem = result;
1647 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1649 tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1650 if (tem && !is_gimple_min_invariant (tem))
1651 tem = NULL_TREE;
1653 if (tem)
1654 return get_or_alloc_expr_for_constant (tem);
1657 /* If we'd have to convert things we would need to validate
1658 if we can insert the translated expression. So fail
1659 here for now - we cannot insert an alias with a different
1660 type in the VN tables either, as that would assert. */
1661 if (result
1662 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1663 return NULL;
1664 else if (!result && newref
1665 && !useless_type_conversion_p (ref->type, newref->type))
1667 newoperands.release ();
1668 return NULL;
1671 expr = pre_expr_pool.allocate ();
1672 expr->kind = REFERENCE;
1673 expr->id = 0;
1675 if (newref)
1677 PRE_EXPR_REFERENCE (expr) = newref;
1678 constant = fully_constant_expression (expr);
1679 if (constant != expr)
1680 return constant;
1682 new_val_id = newref->value_id;
1683 get_or_alloc_expression_id (expr);
1685 else
1687 if (changed || !same_valid)
1689 new_val_id = get_next_value_id ();
1690 value_expressions.safe_grow_cleared
1691 (get_max_value_id () + 1);
1693 else
1694 new_val_id = ref->value_id;
1695 if (!newoperands.exists ())
1696 newoperands = operands.copy ();
1697 newref = vn_reference_insert_pieces (newvuse, ref->set,
1698 ref->type,
1699 newoperands,
1700 result, new_val_id);
1701 newoperands = vNULL;
1702 PRE_EXPR_REFERENCE (expr) = newref;
1703 constant = fully_constant_expression (expr);
1704 if (constant != expr)
1705 return constant;
1706 get_or_alloc_expression_id (expr);
1708 add_to_value (new_val_id, expr);
1710 newoperands.release ();
1711 return expr;
1713 break;
1715 case NAME:
1717 tree name = PRE_EXPR_NAME (expr);
1718 gimple def_stmt = SSA_NAME_DEF_STMT (name);
1719 /* If the SSA name is defined by a PHI node in this block,
1720 translate it. */
1721 if (gimple_code (def_stmt) == GIMPLE_PHI
1722 && gimple_bb (def_stmt) == phiblock)
1724 edge e = find_edge (pred, gimple_bb (def_stmt));
1725 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1727 /* Handle constant. */
1728 if (is_gimple_min_invariant (def))
1729 return get_or_alloc_expr_for_constant (def);
1731 return get_or_alloc_expr_for_name (def);
1733 /* Otherwise return it unchanged - it will get removed if its
1734 value is not available in PREDs AVAIL_OUT set of expressions
1735 by the subtraction of TMP_GEN. */
1736 return expr;
1739 default:
1740 gcc_unreachable ();
1744 /* Wrapper around phi_translate_1 providing caching functionality. */
1746 static pre_expr
1747 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1748 basic_block pred, basic_block phiblock)
1750 expr_pred_trans_t slot = NULL;
1751 pre_expr phitrans;
1753 if (!expr)
1754 return NULL;
1756 /* Constants contain no values that need translation. */
1757 if (expr->kind == CONSTANT)
1758 return expr;
1760 if (value_id_constant_p (get_expr_value_id (expr)))
1761 return expr;
1763 /* Don't add translations of NAMEs as those are cheap to translate. */
1764 if (expr->kind != NAME)
1766 if (phi_trans_add (&slot, expr, pred))
1767 return slot->v;
1768 /* Store NULL for the value we want to return in the case of
1769 recursing. */
1770 slot->v = NULL;
1773 /* Translate. */
1774 phitrans = phi_translate_1 (expr, set1, set2, pred, phiblock);
1776 if (slot)
1778 if (phitrans)
1779 slot->v = phitrans;
1780 else
1781 /* Remove failed translations again, they cause insert
1782 iteration to not pick up new opportunities reliably. */
1783 phi_translate_table->remove_elt_with_hash (slot, slot->hashcode);
1786 return phitrans;
1790 /* For each expression in SET, translate the values through phi nodes
1791 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1792 expressions in DEST. */
1794 static void
1795 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
1796 basic_block phiblock)
1798 vec<pre_expr> exprs;
1799 pre_expr expr;
1800 int i;
1802 if (gimple_seq_empty_p (phi_nodes (phiblock)))
1804 bitmap_set_copy (dest, set);
1805 return;
1808 exprs = sorted_array_from_bitmap_set (set);
1809 FOR_EACH_VEC_ELT (exprs, i, expr)
1811 pre_expr translated;
1812 translated = phi_translate (expr, set, NULL, pred, phiblock);
1813 if (!translated)
1814 continue;
1816 /* We might end up with multiple expressions from SET being
1817 translated to the same value. In this case we do not want
1818 to retain the NARY or REFERENCE expression but prefer a NAME
1819 which would be the leader. */
1820 if (translated->kind == NAME)
1821 bitmap_value_replace_in_set (dest, translated);
1822 else
1823 bitmap_value_insert_into_set (dest, translated);
1825 exprs.release ();
1828 /* Find the leader for a value (i.e., the name representing that
1829 value) in a given set, and return it. Return NULL if no leader
1830 is found. */
1832 static pre_expr
1833 bitmap_find_leader (bitmap_set_t set, unsigned int val)
1835 if (value_id_constant_p (val))
1837 unsigned int i;
1838 bitmap_iterator bi;
1839 bitmap exprset = value_expressions[val];
1841 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1843 pre_expr expr = expression_for_id (i);
1844 if (expr->kind == CONSTANT)
1845 return expr;
1848 if (bitmap_set_contains_value (set, val))
1850 /* Rather than walk the entire bitmap of expressions, and see
1851 whether any of them has the value we are looking for, we look
1852 at the reverse mapping, which tells us the set of expressions
1853 that have a given value (IE value->expressions with that
1854 value) and see if any of those expressions are in our set.
1855 The number of expressions per value is usually significantly
1856 less than the number of expressions in the set. In fact, for
1857 large testcases, doing it this way is roughly 5-10x faster
1858 than walking the bitmap.
1859 If this is somehow a significant lose for some cases, we can
1860 choose which set to walk based on which set is smaller. */
1861 unsigned int i;
1862 bitmap_iterator bi;
1863 bitmap exprset = value_expressions[val];
1865 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1866 return expression_for_id (i);
1868 return NULL;
1871 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1872 BLOCK by seeing if it is not killed in the block. Note that we are
1873 only determining whether there is a store that kills it. Because
1874 of the order in which clean iterates over values, we are guaranteed
1875 that altered operands will have caused us to be eliminated from the
1876 ANTIC_IN set already. */
1878 static bool
1879 value_dies_in_block_x (pre_expr expr, basic_block block)
1881 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1882 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1883 gimple def;
1884 gimple_stmt_iterator gsi;
1885 unsigned id = get_expression_id (expr);
1886 bool res = false;
1887 ao_ref ref;
1889 if (!vuse)
1890 return false;
1892 /* Lookup a previously calculated result. */
1893 if (EXPR_DIES (block)
1894 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1895 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1897 /* A memory expression {e, VUSE} dies in the block if there is a
1898 statement that may clobber e. If, starting statement walk from the
1899 top of the basic block, a statement uses VUSE there can be no kill
1900 inbetween that use and the original statement that loaded {e, VUSE},
1901 so we can stop walking. */
1902 ref.base = NULL_TREE;
1903 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1905 tree def_vuse, def_vdef;
1906 def = gsi_stmt (gsi);
1907 def_vuse = gimple_vuse (def);
1908 def_vdef = gimple_vdef (def);
1910 /* Not a memory statement. */
1911 if (!def_vuse)
1912 continue;
1914 /* Not a may-def. */
1915 if (!def_vdef)
1917 /* A load with the same VUSE, we're done. */
1918 if (def_vuse == vuse)
1919 break;
1921 continue;
1924 /* Init ref only if we really need it. */
1925 if (ref.base == NULL_TREE
1926 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type,
1927 refx->operands))
1929 res = true;
1930 break;
1932 /* If the statement may clobber expr, it dies. */
1933 if (stmt_may_clobber_ref_p_1 (def, &ref))
1935 res = true;
1936 break;
1940 /* Remember the result. */
1941 if (!EXPR_DIES (block))
1942 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1943 bitmap_set_bit (EXPR_DIES (block), id * 2);
1944 if (res)
1945 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1947 return res;
1951 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1952 contains its value-id. */
1954 static bool
1955 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1957 if (op && TREE_CODE (op) == SSA_NAME)
1959 unsigned int value_id = VN_INFO (op)->value_id;
1960 if (!(bitmap_set_contains_value (set1, value_id)
1961 || (set2 && bitmap_set_contains_value (set2, value_id))))
1962 return false;
1964 return true;
1967 /* Determine if the expression EXPR is valid in SET1 U SET2.
1968 ONLY SET2 CAN BE NULL.
1969 This means that we have a leader for each part of the expression
1970 (if it consists of values), or the expression is an SSA_NAME.
1971 For loads/calls, we also see if the vuse is killed in this block. */
1973 static bool
1974 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
1976 switch (expr->kind)
1978 case NAME:
1979 /* By construction all NAMEs are available. Non-available
1980 NAMEs are removed by subtracting TMP_GEN from the sets. */
1981 return true;
1982 case NARY:
1984 unsigned int i;
1985 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1986 for (i = 0; i < nary->length; i++)
1987 if (!op_valid_in_sets (set1, set2, nary->op[i]))
1988 return false;
1989 return true;
1991 break;
1992 case REFERENCE:
1994 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1995 vn_reference_op_t vro;
1996 unsigned int i;
1998 FOR_EACH_VEC_ELT (ref->operands, i, vro)
2000 if (!op_valid_in_sets (set1, set2, vro->op0)
2001 || !op_valid_in_sets (set1, set2, vro->op1)
2002 || !op_valid_in_sets (set1, set2, vro->op2))
2003 return false;
2005 return true;
2007 default:
2008 gcc_unreachable ();
2012 /* Clean the set of expressions that are no longer valid in SET1 or
2013 SET2. This means expressions that are made up of values we have no
2014 leaders for in SET1 or SET2. This version is used for partial
2015 anticipation, which means it is not valid in either ANTIC_IN or
2016 PA_IN. */
2018 static void
2019 dependent_clean (bitmap_set_t set1, bitmap_set_t set2)
2021 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
2022 pre_expr expr;
2023 int i;
2025 FOR_EACH_VEC_ELT (exprs, i, expr)
2027 if (!valid_in_sets (set1, set2, expr))
2028 bitmap_remove_from_set (set1, expr);
2030 exprs.release ();
2033 /* Clean the set of expressions that are no longer valid in SET. This
2034 means expressions that are made up of values we have no leaders for
2035 in SET. */
2037 static void
2038 clean (bitmap_set_t set)
2040 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set);
2041 pre_expr expr;
2042 int i;
2044 FOR_EACH_VEC_ELT (exprs, i, expr)
2046 if (!valid_in_sets (set, NULL, expr))
2047 bitmap_remove_from_set (set, expr);
2049 exprs.release ();
2052 /* Clean the set of expressions that are no longer valid in SET because
2053 they are clobbered in BLOCK or because they trap and may not be executed. */
2055 static void
2056 prune_clobbered_mems (bitmap_set_t set, basic_block block)
2058 bitmap_iterator bi;
2059 unsigned i;
2061 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2063 pre_expr expr = expression_for_id (i);
2064 if (expr->kind == REFERENCE)
2066 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2067 if (ref->vuse)
2069 gimple def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2070 if (!gimple_nop_p (def_stmt)
2071 && ((gimple_bb (def_stmt) != block
2072 && !dominated_by_p (CDI_DOMINATORS,
2073 block, gimple_bb (def_stmt)))
2074 || (gimple_bb (def_stmt) == block
2075 && value_dies_in_block_x (expr, block))))
2076 bitmap_remove_from_set (set, expr);
2079 else if (expr->kind == NARY)
2081 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2082 /* If the NARY may trap make sure the block does not contain
2083 a possible exit point.
2084 ??? This is overly conservative if we translate AVAIL_OUT
2085 as the available expression might be after the exit point. */
2086 if (BB_MAY_NOTRETURN (block)
2087 && vn_nary_may_trap (nary))
2088 bitmap_remove_from_set (set, expr);
2093 static sbitmap has_abnormal_preds;
2095 /* List of blocks that may have changed during ANTIC computation and
2096 thus need to be iterated over. */
2098 static sbitmap changed_blocks;
2100 /* Compute the ANTIC set for BLOCK.
2102 If succs(BLOCK) > 1 then
2103 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2104 else if succs(BLOCK) == 1 then
2105 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2107 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2110 static bool
2111 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2113 bool changed = false;
2114 bitmap_set_t S, old, ANTIC_OUT;
2115 bitmap_iterator bi;
2116 unsigned int bii;
2117 edge e;
2118 edge_iterator ei;
2120 old = ANTIC_OUT = S = NULL;
2121 BB_VISITED (block) = 1;
2123 /* If any edges from predecessors are abnormal, antic_in is empty,
2124 so do nothing. */
2125 if (block_has_abnormal_pred_edge)
2126 goto maybe_dump_sets;
2128 old = ANTIC_IN (block);
2129 ANTIC_OUT = bitmap_set_new ();
2131 /* If the block has no successors, ANTIC_OUT is empty. */
2132 if (EDGE_COUNT (block->succs) == 0)
2134 /* If we have one successor, we could have some phi nodes to
2135 translate through. */
2136 else if (single_succ_p (block))
2138 basic_block succ_bb = single_succ (block);
2139 gcc_assert (BB_VISITED (succ_bb));
2140 phi_translate_set (ANTIC_OUT, ANTIC_IN (succ_bb), block, succ_bb);
2142 /* If we have multiple successors, we take the intersection of all of
2143 them. Note that in the case of loop exit phi nodes, we may have
2144 phis to translate through. */
2145 else
2147 size_t i;
2148 basic_block bprime, first = NULL;
2150 auto_vec<basic_block> worklist (EDGE_COUNT (block->succs));
2151 FOR_EACH_EDGE (e, ei, block->succs)
2153 if (!first
2154 && BB_VISITED (e->dest))
2155 first = e->dest;
2156 else if (BB_VISITED (e->dest))
2157 worklist.quick_push (e->dest);
2160 /* Of multiple successors we have to have visited one already
2161 which is guaranteed by iteration order. */
2162 gcc_assert (first != NULL);
2164 phi_translate_set (ANTIC_OUT, ANTIC_IN (first), block, first);
2166 FOR_EACH_VEC_ELT (worklist, i, bprime)
2168 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2170 bitmap_set_t tmp = bitmap_set_new ();
2171 phi_translate_set (tmp, ANTIC_IN (bprime), block, bprime);
2172 bitmap_set_and (ANTIC_OUT, tmp);
2173 bitmap_set_free (tmp);
2175 else
2176 bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime));
2180 /* Prune expressions that are clobbered in block and thus become
2181 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2182 prune_clobbered_mems (ANTIC_OUT, block);
2184 /* Generate ANTIC_OUT - TMP_GEN. */
2185 S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block));
2187 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2188 ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block),
2189 TMP_GEN (block));
2191 /* Then union in the ANTIC_OUT - TMP_GEN values,
2192 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2193 FOR_EACH_EXPR_ID_IN_SET (S, bii, bi)
2194 bitmap_value_insert_into_set (ANTIC_IN (block),
2195 expression_for_id (bii));
2197 clean (ANTIC_IN (block));
2199 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2201 changed = true;
2202 bitmap_set_bit (changed_blocks, block->index);
2203 FOR_EACH_EDGE (e, ei, block->preds)
2204 bitmap_set_bit (changed_blocks, e->src->index);
2206 else
2207 bitmap_clear_bit (changed_blocks, block->index);
2209 maybe_dump_sets:
2210 if (dump_file && (dump_flags & TDF_DETAILS))
2212 if (ANTIC_OUT)
2213 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2215 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2216 block->index);
2218 if (S)
2219 print_bitmap_set (dump_file, S, "S", block->index);
2221 if (old)
2222 bitmap_set_free (old);
2223 if (S)
2224 bitmap_set_free (S);
2225 if (ANTIC_OUT)
2226 bitmap_set_free (ANTIC_OUT);
2227 return changed;
2230 /* Compute PARTIAL_ANTIC for BLOCK.
2232 If succs(BLOCK) > 1 then
2233 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2234 in ANTIC_OUT for all succ(BLOCK)
2235 else if succs(BLOCK) == 1 then
2236 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2238 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2239 - ANTIC_IN[BLOCK])
2242 static bool
2243 compute_partial_antic_aux (basic_block block,
2244 bool block_has_abnormal_pred_edge)
2246 bool changed = false;
2247 bitmap_set_t old_PA_IN;
2248 bitmap_set_t PA_OUT;
2249 edge e;
2250 edge_iterator ei;
2251 unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH);
2253 old_PA_IN = PA_OUT = NULL;
2255 /* If any edges from predecessors are abnormal, antic_in is empty,
2256 so do nothing. */
2257 if (block_has_abnormal_pred_edge)
2258 goto maybe_dump_sets;
2260 /* If there are too many partially anticipatable values in the
2261 block, phi_translate_set can take an exponential time: stop
2262 before the translation starts. */
2263 if (max_pa
2264 && single_succ_p (block)
2265 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2266 goto maybe_dump_sets;
2268 old_PA_IN = PA_IN (block);
2269 PA_OUT = bitmap_set_new ();
2271 /* If the block has no successors, ANTIC_OUT is empty. */
2272 if (EDGE_COUNT (block->succs) == 0)
2274 /* If we have one successor, we could have some phi nodes to
2275 translate through. Note that we can't phi translate across DFS
2276 back edges in partial antic, because it uses a union operation on
2277 the successors. For recurrences like IV's, we will end up
2278 generating a new value in the set on each go around (i + 3 (VH.1)
2279 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2280 else if (single_succ_p (block))
2282 basic_block succ = single_succ (block);
2283 if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK))
2284 phi_translate_set (PA_OUT, PA_IN (succ), block, succ);
2286 /* If we have multiple successors, we take the union of all of
2287 them. */
2288 else
2290 size_t i;
2291 basic_block bprime;
2293 auto_vec<basic_block> worklist (EDGE_COUNT (block->succs));
2294 FOR_EACH_EDGE (e, ei, block->succs)
2296 if (e->flags & EDGE_DFS_BACK)
2297 continue;
2298 worklist.quick_push (e->dest);
2300 if (worklist.length () > 0)
2302 FOR_EACH_VEC_ELT (worklist, i, bprime)
2304 unsigned int i;
2305 bitmap_iterator bi;
2307 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi)
2308 bitmap_value_insert_into_set (PA_OUT,
2309 expression_for_id (i));
2310 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2312 bitmap_set_t pa_in = bitmap_set_new ();
2313 phi_translate_set (pa_in, PA_IN (bprime), block, bprime);
2314 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2315 bitmap_value_insert_into_set (PA_OUT,
2316 expression_for_id (i));
2317 bitmap_set_free (pa_in);
2319 else
2320 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi)
2321 bitmap_value_insert_into_set (PA_OUT,
2322 expression_for_id (i));
2327 /* Prune expressions that are clobbered in block and thus become
2328 invalid if translated from PA_OUT to PA_IN. */
2329 prune_clobbered_mems (PA_OUT, block);
2331 /* PA_IN starts with PA_OUT - TMP_GEN.
2332 Then we subtract things from ANTIC_IN. */
2333 PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block));
2335 /* For partial antic, we want to put back in the phi results, since
2336 we will properly avoid making them partially antic over backedges. */
2337 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2338 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2340 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2341 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2343 dependent_clean (PA_IN (block), ANTIC_IN (block));
2345 if (!bitmap_set_equal (old_PA_IN, PA_IN (block)))
2347 changed = true;
2348 bitmap_set_bit (changed_blocks, block->index);
2349 FOR_EACH_EDGE (e, ei, block->preds)
2350 bitmap_set_bit (changed_blocks, e->src->index);
2352 else
2353 bitmap_clear_bit (changed_blocks, block->index);
2355 maybe_dump_sets:
2356 if (dump_file && (dump_flags & TDF_DETAILS))
2358 if (PA_OUT)
2359 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2361 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2363 if (old_PA_IN)
2364 bitmap_set_free (old_PA_IN);
2365 if (PA_OUT)
2366 bitmap_set_free (PA_OUT);
2367 return changed;
2370 /* Compute ANTIC and partial ANTIC sets. */
2372 static void
2373 compute_antic (void)
2375 bool changed = true;
2376 int num_iterations = 0;
2377 basic_block block;
2378 int i;
2380 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2381 We pre-build the map of blocks with incoming abnormal edges here. */
2382 has_abnormal_preds = sbitmap_alloc (last_basic_block_for_fn (cfun));
2383 bitmap_clear (has_abnormal_preds);
2385 FOR_ALL_BB_FN (block, cfun)
2387 edge_iterator ei;
2388 edge e;
2390 FOR_EACH_EDGE (e, ei, block->preds)
2392 e->flags &= ~EDGE_DFS_BACK;
2393 if (e->flags & EDGE_ABNORMAL)
2395 bitmap_set_bit (has_abnormal_preds, block->index);
2396 break;
2400 BB_VISITED (block) = 0;
2402 /* While we are here, give empty ANTIC_IN sets to each block. */
2403 ANTIC_IN (block) = bitmap_set_new ();
2404 PA_IN (block) = bitmap_set_new ();
2407 /* At the exit block we anticipate nothing. */
2408 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2410 changed_blocks = sbitmap_alloc (last_basic_block_for_fn (cfun) + 1);
2411 bitmap_ones (changed_blocks);
2412 while (changed)
2414 if (dump_file && (dump_flags & TDF_DETAILS))
2415 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2416 /* ??? We need to clear our PHI translation cache here as the
2417 ANTIC sets shrink and we restrict valid translations to
2418 those having operands with leaders in ANTIC. Same below
2419 for PA ANTIC computation. */
2420 num_iterations++;
2421 changed = false;
2422 for (i = postorder_num - 1; i >= 0; i--)
2424 if (bitmap_bit_p (changed_blocks, postorder[i]))
2426 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2427 changed |= compute_antic_aux (block,
2428 bitmap_bit_p (has_abnormal_preds,
2429 block->index));
2432 /* Theoretically possible, but *highly* unlikely. */
2433 gcc_checking_assert (num_iterations < 500);
2436 statistics_histogram_event (cfun, "compute_antic iterations",
2437 num_iterations);
2439 if (do_partial_partial)
2441 bitmap_ones (changed_blocks);
2442 mark_dfs_back_edges ();
2443 num_iterations = 0;
2444 changed = true;
2445 while (changed)
2447 if (dump_file && (dump_flags & TDF_DETAILS))
2448 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2449 num_iterations++;
2450 changed = false;
2451 for (i = postorder_num - 1 ; i >= 0; i--)
2453 if (bitmap_bit_p (changed_blocks, postorder[i]))
2455 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2456 changed
2457 |= compute_partial_antic_aux (block,
2458 bitmap_bit_p (has_abnormal_preds,
2459 block->index));
2462 /* Theoretically possible, but *highly* unlikely. */
2463 gcc_checking_assert (num_iterations < 500);
2465 statistics_histogram_event (cfun, "compute_partial_antic iterations",
2466 num_iterations);
2468 sbitmap_free (has_abnormal_preds);
2469 sbitmap_free (changed_blocks);
2473 /* Inserted expressions are placed onto this worklist, which is used
2474 for performing quick dead code elimination of insertions we made
2475 that didn't turn out to be necessary. */
2476 static bitmap inserted_exprs;
2478 /* The actual worker for create_component_ref_by_pieces. */
2480 static tree
2481 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2482 unsigned int *operand, gimple_seq *stmts)
2484 vn_reference_op_t currop = &ref->operands[*operand];
2485 tree genop;
2486 ++*operand;
2487 switch (currop->opcode)
2489 case CALL_EXPR:
2491 tree folded, sc = NULL_TREE;
2492 unsigned int nargs = 0;
2493 tree fn, *args;
2494 if (TREE_CODE (currop->op0) == FUNCTION_DECL)
2495 fn = currop->op0;
2496 else
2497 fn = find_or_generate_expression (block, currop->op0, stmts);
2498 if (!fn)
2499 return NULL_TREE;
2500 if (currop->op1)
2502 sc = find_or_generate_expression (block, currop->op1, stmts);
2503 if (!sc)
2504 return NULL_TREE;
2506 args = XNEWVEC (tree, ref->operands.length () - 1);
2507 while (*operand < ref->operands.length ())
2509 args[nargs] = create_component_ref_by_pieces_1 (block, ref,
2510 operand, stmts);
2511 if (!args[nargs])
2512 return NULL_TREE;
2513 nargs++;
2515 folded = build_call_array (currop->type,
2516 (TREE_CODE (fn) == FUNCTION_DECL
2517 ? build_fold_addr_expr (fn) : fn),
2518 nargs, args);
2519 if (currop->with_bounds)
2520 CALL_WITH_BOUNDS_P (folded) = true;
2521 free (args);
2522 if (sc)
2523 CALL_EXPR_STATIC_CHAIN (folded) = sc;
2524 return folded;
2527 case MEM_REF:
2529 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2530 stmts);
2531 if (!baseop)
2532 return NULL_TREE;
2533 tree offset = currop->op0;
2534 if (TREE_CODE (baseop) == ADDR_EXPR
2535 && handled_component_p (TREE_OPERAND (baseop, 0)))
2537 HOST_WIDE_INT off;
2538 tree base;
2539 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2540 &off);
2541 gcc_assert (base);
2542 offset = int_const_binop (PLUS_EXPR, offset,
2543 build_int_cst (TREE_TYPE (offset),
2544 off));
2545 baseop = build_fold_addr_expr (base);
2547 return fold_build2 (MEM_REF, currop->type, baseop, offset);
2550 case TARGET_MEM_REF:
2552 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2553 vn_reference_op_t nextop = &ref->operands[++*operand];
2554 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2555 stmts);
2556 if (!baseop)
2557 return NULL_TREE;
2558 if (currop->op0)
2560 genop0 = find_or_generate_expression (block, currop->op0, stmts);
2561 if (!genop0)
2562 return NULL_TREE;
2564 if (nextop->op0)
2566 genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2567 if (!genop1)
2568 return NULL_TREE;
2570 return build5 (TARGET_MEM_REF, currop->type,
2571 baseop, currop->op2, genop0, currop->op1, genop1);
2574 case ADDR_EXPR:
2575 if (currop->op0)
2577 gcc_assert (is_gimple_min_invariant (currop->op0));
2578 return currop->op0;
2580 /* Fallthrough. */
2581 case REALPART_EXPR:
2582 case IMAGPART_EXPR:
2583 case VIEW_CONVERT_EXPR:
2585 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2586 stmts);
2587 if (!genop0)
2588 return NULL_TREE;
2589 return fold_build1 (currop->opcode, currop->type, genop0);
2592 case WITH_SIZE_EXPR:
2594 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2595 stmts);
2596 if (!genop0)
2597 return NULL_TREE;
2598 tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2599 if (!genop1)
2600 return NULL_TREE;
2601 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2604 case BIT_FIELD_REF:
2606 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2607 stmts);
2608 if (!genop0)
2609 return NULL_TREE;
2610 tree op1 = currop->op0;
2611 tree op2 = currop->op1;
2612 return fold_build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2615 /* For array ref vn_reference_op's, operand 1 of the array ref
2616 is op0 of the reference op and operand 3 of the array ref is
2617 op1. */
2618 case ARRAY_RANGE_REF:
2619 case ARRAY_REF:
2621 tree genop0;
2622 tree genop1 = currop->op0;
2623 tree genop2 = currop->op1;
2624 tree genop3 = currop->op2;
2625 genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2626 stmts);
2627 if (!genop0)
2628 return NULL_TREE;
2629 genop1 = find_or_generate_expression (block, genop1, stmts);
2630 if (!genop1)
2631 return NULL_TREE;
2632 if (genop2)
2634 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2635 /* Drop zero minimum index if redundant. */
2636 if (integer_zerop (genop2)
2637 && (!domain_type
2638 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2639 genop2 = NULL_TREE;
2640 else
2642 genop2 = find_or_generate_expression (block, genop2, stmts);
2643 if (!genop2)
2644 return NULL_TREE;
2647 if (genop3)
2649 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2650 /* We can't always put a size in units of the element alignment
2651 here as the element alignment may be not visible. See
2652 PR43783. Simply drop the element size for constant
2653 sizes. */
2654 if (tree_int_cst_equal (genop3, TYPE_SIZE_UNIT (elmt_type)))
2655 genop3 = NULL_TREE;
2656 else
2658 genop3 = size_binop (EXACT_DIV_EXPR, genop3,
2659 size_int (TYPE_ALIGN_UNIT (elmt_type)));
2660 genop3 = find_or_generate_expression (block, genop3, stmts);
2661 if (!genop3)
2662 return NULL_TREE;
2665 return build4 (currop->opcode, currop->type, genop0, genop1,
2666 genop2, genop3);
2668 case COMPONENT_REF:
2670 tree op0;
2671 tree op1;
2672 tree genop2 = currop->op1;
2673 op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2674 if (!op0)
2675 return NULL_TREE;
2676 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2677 op1 = currop->op0;
2678 if (genop2)
2680 genop2 = find_or_generate_expression (block, genop2, stmts);
2681 if (!genop2)
2682 return NULL_TREE;
2684 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2687 case SSA_NAME:
2689 genop = find_or_generate_expression (block, currop->op0, stmts);
2690 return genop;
2692 case STRING_CST:
2693 case INTEGER_CST:
2694 case COMPLEX_CST:
2695 case VECTOR_CST:
2696 case REAL_CST:
2697 case CONSTRUCTOR:
2698 case VAR_DECL:
2699 case PARM_DECL:
2700 case CONST_DECL:
2701 case RESULT_DECL:
2702 case FUNCTION_DECL:
2703 return currop->op0;
2705 default:
2706 gcc_unreachable ();
2710 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2711 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2712 trying to rename aggregates into ssa form directly, which is a no no.
2714 Thus, this routine doesn't create temporaries, it just builds a
2715 single access expression for the array, calling
2716 find_or_generate_expression to build the innermost pieces.
2718 This function is a subroutine of create_expression_by_pieces, and
2719 should not be called on it's own unless you really know what you
2720 are doing. */
2722 static tree
2723 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2724 gimple_seq *stmts)
2726 unsigned int op = 0;
2727 return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2730 /* Find a simple leader for an expression, or generate one using
2731 create_expression_by_pieces from a NARY expression for the value.
2732 BLOCK is the basic_block we are looking for leaders in.
2733 OP is the tree expression to find a leader for or generate.
2734 Returns the leader or NULL_TREE on failure. */
2736 static tree
2737 find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2739 pre_expr expr = get_or_alloc_expr_for (op);
2740 unsigned int lookfor = get_expr_value_id (expr);
2741 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2742 if (leader)
2744 if (leader->kind == NAME)
2745 return PRE_EXPR_NAME (leader);
2746 else if (leader->kind == CONSTANT)
2747 return PRE_EXPR_CONSTANT (leader);
2749 /* Defer. */
2750 return NULL_TREE;
2753 /* It must be a complex expression, so generate it recursively. Note
2754 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2755 where the insert algorithm fails to insert a required expression. */
2756 bitmap exprset = value_expressions[lookfor];
2757 bitmap_iterator bi;
2758 unsigned int i;
2759 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2761 pre_expr temp = expression_for_id (i);
2762 /* We cannot insert random REFERENCE expressions at arbitrary
2763 places. We can insert NARYs which eventually re-materializes
2764 its operand values. */
2765 if (temp->kind == NARY)
2766 return create_expression_by_pieces (block, temp, stmts,
2767 get_expr_type (expr));
2770 /* Defer. */
2771 return NULL_TREE;
2774 #define NECESSARY GF_PLF_1
2776 /* Create an expression in pieces, so that we can handle very complex
2777 expressions that may be ANTIC, but not necessary GIMPLE.
2778 BLOCK is the basic block the expression will be inserted into,
2779 EXPR is the expression to insert (in value form)
2780 STMTS is a statement list to append the necessary insertions into.
2782 This function will die if we hit some value that shouldn't be
2783 ANTIC but is (IE there is no leader for it, or its components).
2784 The function returns NULL_TREE in case a different antic expression
2785 has to be inserted first.
2786 This function may also generate expressions that are themselves
2787 partially or fully redundant. Those that are will be either made
2788 fully redundant during the next iteration of insert (for partially
2789 redundant ones), or eliminated by eliminate (for fully redundant
2790 ones). */
2792 static tree
2793 create_expression_by_pieces (basic_block block, pre_expr expr,
2794 gimple_seq *stmts, tree type)
2796 tree name;
2797 tree folded;
2798 gimple_seq forced_stmts = NULL;
2799 unsigned int value_id;
2800 gimple_stmt_iterator gsi;
2801 tree exprtype = type ? type : get_expr_type (expr);
2802 pre_expr nameexpr;
2803 gassign *newstmt;
2805 switch (expr->kind)
2807 /* We may hit the NAME/CONSTANT case if we have to convert types
2808 that value numbering saw through. */
2809 case NAME:
2810 folded = PRE_EXPR_NAME (expr);
2811 break;
2812 case CONSTANT:
2813 folded = PRE_EXPR_CONSTANT (expr);
2814 break;
2815 case REFERENCE:
2817 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2818 folded = create_component_ref_by_pieces (block, ref, stmts);
2819 if (!folded)
2820 return NULL_TREE;
2822 break;
2823 case NARY:
2825 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2826 tree *genop = XALLOCAVEC (tree, nary->length);
2827 unsigned i;
2828 for (i = 0; i < nary->length; ++i)
2830 genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2831 if (!genop[i])
2832 return NULL_TREE;
2833 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2834 may have conversions stripped. */
2835 if (nary->opcode == POINTER_PLUS_EXPR)
2837 if (i == 0)
2838 genop[i] = gimple_convert (&forced_stmts,
2839 nary->type, genop[i]);
2840 else if (i == 1)
2841 genop[i] = gimple_convert (&forced_stmts,
2842 sizetype, genop[i]);
2844 else
2845 genop[i] = gimple_convert (&forced_stmts,
2846 TREE_TYPE (nary->op[i]), genop[i]);
2848 if (nary->opcode == CONSTRUCTOR)
2850 vec<constructor_elt, va_gc> *elts = NULL;
2851 for (i = 0; i < nary->length; ++i)
2852 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2853 folded = build_constructor (nary->type, elts);
2855 else
2857 switch (nary->length)
2859 case 1:
2860 folded = fold_build1 (nary->opcode, nary->type,
2861 genop[0]);
2862 break;
2863 case 2:
2864 folded = fold_build2 (nary->opcode, nary->type,
2865 genop[0], genop[1]);
2866 break;
2867 case 3:
2868 folded = fold_build3 (nary->opcode, nary->type,
2869 genop[0], genop[1], genop[2]);
2870 break;
2871 default:
2872 gcc_unreachable ();
2876 break;
2877 default:
2878 gcc_unreachable ();
2881 if (!useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2882 folded = fold_convert (exprtype, folded);
2884 /* Force the generated expression to be a sequence of GIMPLE
2885 statements.
2886 We have to call unshare_expr because force_gimple_operand may
2887 modify the tree we pass to it. */
2888 gimple_seq tem = NULL;
2889 folded = force_gimple_operand (unshare_expr (folded), &tem,
2890 false, NULL);
2891 gimple_seq_add_seq_without_update (&forced_stmts, tem);
2893 /* If we have any intermediate expressions to the value sets, add them
2894 to the value sets and chain them in the instruction stream. */
2895 if (forced_stmts)
2897 gsi = gsi_start (forced_stmts);
2898 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2900 gimple stmt = gsi_stmt (gsi);
2901 tree forcedname = gimple_get_lhs (stmt);
2902 pre_expr nameexpr;
2904 if (TREE_CODE (forcedname) == SSA_NAME)
2906 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
2907 VN_INFO_GET (forcedname)->valnum = forcedname;
2908 VN_INFO (forcedname)->value_id = get_next_value_id ();
2909 nameexpr = get_or_alloc_expr_for_name (forcedname);
2910 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
2911 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2912 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2915 gimple_set_vuse (stmt, BB_LIVE_VOP_ON_EXIT (block));
2916 gimple_set_modified (stmt, true);
2918 gimple_seq_add_seq (stmts, forced_stmts);
2921 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2922 newstmt = gimple_build_assign (name, folded);
2923 gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
2924 gimple_set_modified (newstmt, true);
2925 gimple_set_plf (newstmt, NECESSARY, false);
2927 gimple_seq_add_stmt (stmts, newstmt);
2928 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (name));
2930 /* Fold the last statement. */
2931 gsi = gsi_last (*stmts);
2932 if (fold_stmt_inplace (&gsi))
2933 update_stmt (gsi_stmt (gsi));
2935 /* Add a value number to the temporary.
2936 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2937 we are creating the expression by pieces, and this particular piece of
2938 the expression may have been represented. There is no harm in replacing
2939 here. */
2940 value_id = get_expr_value_id (expr);
2941 VN_INFO_GET (name)->value_id = value_id;
2942 VN_INFO (name)->valnum = sccvn_valnum_from_value_id (value_id);
2943 if (VN_INFO (name)->valnum == NULL_TREE)
2944 VN_INFO (name)->valnum = name;
2945 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
2946 nameexpr = get_or_alloc_expr_for_name (name);
2947 add_to_value (value_id, nameexpr);
2948 if (NEW_SETS (block))
2949 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2950 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2952 pre_stats.insertions++;
2953 if (dump_file && (dump_flags & TDF_DETAILS))
2955 fprintf (dump_file, "Inserted ");
2956 print_gimple_stmt (dump_file, newstmt, 0, 0);
2957 fprintf (dump_file, " in predecessor %d (%04d)\n",
2958 block->index, value_id);
2961 return name;
2965 /* Insert the to-be-made-available values of expression EXPRNUM for each
2966 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2967 merge the result with a phi node, given the same value number as
2968 NODE. Return true if we have inserted new stuff. */
2970 static bool
2971 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
2972 vec<pre_expr> avail)
2974 pre_expr expr = expression_for_id (exprnum);
2975 pre_expr newphi;
2976 unsigned int val = get_expr_value_id (expr);
2977 edge pred;
2978 bool insertions = false;
2979 bool nophi = false;
2980 basic_block bprime;
2981 pre_expr eprime;
2982 edge_iterator ei;
2983 tree type = get_expr_type (expr);
2984 tree temp;
2985 gphi *phi;
2987 /* Make sure we aren't creating an induction variable. */
2988 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
2990 bool firstinsideloop = false;
2991 bool secondinsideloop = false;
2992 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
2993 EDGE_PRED (block, 0)->src);
2994 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
2995 EDGE_PRED (block, 1)->src);
2996 /* Induction variables only have one edge inside the loop. */
2997 if ((firstinsideloop ^ secondinsideloop)
2998 && expr->kind != REFERENCE)
3000 if (dump_file && (dump_flags & TDF_DETAILS))
3001 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3002 nophi = true;
3006 /* Make the necessary insertions. */
3007 FOR_EACH_EDGE (pred, ei, block->preds)
3009 gimple_seq stmts = NULL;
3010 tree builtexpr;
3011 bprime = pred->src;
3012 eprime = avail[pred->dest_idx];
3014 if (eprime->kind != NAME && eprime->kind != CONSTANT)
3016 builtexpr = create_expression_by_pieces (bprime, eprime,
3017 &stmts, type);
3018 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3019 gsi_insert_seq_on_edge (pred, stmts);
3020 if (!builtexpr)
3022 /* We cannot insert a PHI node if we failed to insert
3023 on one edge. */
3024 nophi = true;
3025 continue;
3027 avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3028 insertions = true;
3030 else if (eprime->kind == CONSTANT)
3032 /* Constants may not have the right type, fold_convert
3033 should give us back a constant with the right type. */
3034 tree constant = PRE_EXPR_CONSTANT (eprime);
3035 if (!useless_type_conversion_p (type, TREE_TYPE (constant)))
3037 tree builtexpr = fold_convert (type, constant);
3038 if (!is_gimple_min_invariant (builtexpr))
3040 tree forcedexpr = force_gimple_operand (builtexpr,
3041 &stmts, true,
3042 NULL);
3043 if (!is_gimple_min_invariant (forcedexpr))
3045 if (forcedexpr != builtexpr)
3047 VN_INFO_GET (forcedexpr)->valnum = PRE_EXPR_CONSTANT (eprime);
3048 VN_INFO (forcedexpr)->value_id = get_expr_value_id (eprime);
3050 if (stmts)
3052 gimple_stmt_iterator gsi;
3053 gsi = gsi_start (stmts);
3054 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3056 gimple stmt = gsi_stmt (gsi);
3057 tree lhs = gimple_get_lhs (stmt);
3058 if (TREE_CODE (lhs) == SSA_NAME)
3059 bitmap_set_bit (inserted_exprs,
3060 SSA_NAME_VERSION (lhs));
3061 gimple_set_plf (stmt, NECESSARY, false);
3063 gsi_insert_seq_on_edge (pred, stmts);
3065 avail[pred->dest_idx]
3066 = get_or_alloc_expr_for_name (forcedexpr);
3069 else
3070 avail[pred->dest_idx]
3071 = get_or_alloc_expr_for_constant (builtexpr);
3074 else if (eprime->kind == NAME)
3076 /* We may have to do a conversion because our value
3077 numbering can look through types in certain cases, but
3078 our IL requires all operands of a phi node have the same
3079 type. */
3080 tree name = PRE_EXPR_NAME (eprime);
3081 if (!useless_type_conversion_p (type, TREE_TYPE (name)))
3083 tree builtexpr;
3084 tree forcedexpr;
3085 builtexpr = fold_convert (type, name);
3086 forcedexpr = force_gimple_operand (builtexpr,
3087 &stmts, true,
3088 NULL);
3090 if (forcedexpr != name)
3092 VN_INFO_GET (forcedexpr)->valnum = VN_INFO (name)->valnum;
3093 VN_INFO (forcedexpr)->value_id = VN_INFO (name)->value_id;
3096 if (stmts)
3098 gimple_stmt_iterator gsi;
3099 gsi = gsi_start (stmts);
3100 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3102 gimple stmt = gsi_stmt (gsi);
3103 tree lhs = gimple_get_lhs (stmt);
3104 if (TREE_CODE (lhs) == SSA_NAME)
3105 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
3106 gimple_set_plf (stmt, NECESSARY, false);
3108 gsi_insert_seq_on_edge (pred, stmts);
3110 avail[pred->dest_idx] = get_or_alloc_expr_for_name (forcedexpr);
3114 /* If we didn't want a phi node, and we made insertions, we still have
3115 inserted new stuff, and thus return true. If we didn't want a phi node,
3116 and didn't make insertions, we haven't added anything new, so return
3117 false. */
3118 if (nophi && insertions)
3119 return true;
3120 else if (nophi && !insertions)
3121 return false;
3123 /* Now build a phi for the new variable. */
3124 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3125 phi = create_phi_node (temp, block);
3127 gimple_set_plf (phi, NECESSARY, false);
3128 VN_INFO_GET (temp)->value_id = val;
3129 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3130 if (VN_INFO (temp)->valnum == NULL_TREE)
3131 VN_INFO (temp)->valnum = temp;
3132 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3133 FOR_EACH_EDGE (pred, ei, block->preds)
3135 pre_expr ae = avail[pred->dest_idx];
3136 gcc_assert (get_expr_type (ae) == type
3137 || useless_type_conversion_p (type, get_expr_type (ae)));
3138 if (ae->kind == CONSTANT)
3139 add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3140 pred, UNKNOWN_LOCATION);
3141 else
3142 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3145 newphi = get_or_alloc_expr_for_name (temp);
3146 add_to_value (val, newphi);
3148 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3149 this insertion, since we test for the existence of this value in PHI_GEN
3150 before proceeding with the partial redundancy checks in insert_aux.
3152 The value may exist in AVAIL_OUT, in particular, it could be represented
3153 by the expression we are trying to eliminate, in which case we want the
3154 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3155 inserted there.
3157 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3158 this block, because if it did, it would have existed in our dominator's
3159 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3162 bitmap_insert_into_set (PHI_GEN (block), newphi);
3163 bitmap_value_replace_in_set (AVAIL_OUT (block),
3164 newphi);
3165 bitmap_insert_into_set (NEW_SETS (block),
3166 newphi);
3168 /* If we insert a PHI node for a conversion of another PHI node
3169 in the same basic-block try to preserve range information.
3170 This is important so that followup loop passes receive optimal
3171 number of iteration analysis results. See PR61743. */
3172 if (expr->kind == NARY
3173 && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3174 && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3175 && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3176 && INTEGRAL_TYPE_P (type)
3177 && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3178 && (TYPE_PRECISION (type)
3179 >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3180 && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3182 wide_int min, max;
3183 if (get_range_info (expr->u.nary->op[0], &min, &max) == VR_RANGE
3184 && !wi::neg_p (min, SIGNED)
3185 && !wi::neg_p (max, SIGNED))
3186 /* Just handle extension and sign-changes of all-positive ranges. */
3187 set_range_info (temp,
3188 SSA_NAME_RANGE_TYPE (expr->u.nary->op[0]),
3189 wide_int_storage::from (min, TYPE_PRECISION (type),
3190 TYPE_SIGN (type)),
3191 wide_int_storage::from (max, TYPE_PRECISION (type),
3192 TYPE_SIGN (type)));
3195 if (dump_file && (dump_flags & TDF_DETAILS))
3197 fprintf (dump_file, "Created phi ");
3198 print_gimple_stmt (dump_file, phi, 0, 0);
3199 fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3201 pre_stats.phis++;
3202 return true;
3207 /* Perform insertion of partially redundant values.
3208 For BLOCK, do the following:
3209 1. Propagate the NEW_SETS of the dominator into the current block.
3210 If the block has multiple predecessors,
3211 2a. Iterate over the ANTIC expressions for the block to see if
3212 any of them are partially redundant.
3213 2b. If so, insert them into the necessary predecessors to make
3214 the expression fully redundant.
3215 2c. Insert a new PHI merging the values of the predecessors.
3216 2d. Insert the new PHI, and the new expressions, into the
3217 NEW_SETS set.
3218 3. Recursively call ourselves on the dominator children of BLOCK.
3220 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
3221 do_regular_insertion and do_partial_insertion.
3225 static bool
3226 do_regular_insertion (basic_block block, basic_block dom)
3228 bool new_stuff = false;
3229 vec<pre_expr> exprs;
3230 pre_expr expr;
3231 auto_vec<pre_expr> avail;
3232 int i;
3234 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3235 avail.safe_grow (EDGE_COUNT (block->preds));
3237 FOR_EACH_VEC_ELT (exprs, i, expr)
3239 if (expr->kind == NARY
3240 || expr->kind == REFERENCE)
3242 unsigned int val;
3243 bool by_some = false;
3244 bool cant_insert = false;
3245 bool all_same = true;
3246 pre_expr first_s = NULL;
3247 edge pred;
3248 basic_block bprime;
3249 pre_expr eprime = NULL;
3250 edge_iterator ei;
3251 pre_expr edoubleprime = NULL;
3252 bool do_insertion = false;
3254 val = get_expr_value_id (expr);
3255 if (bitmap_set_contains_value (PHI_GEN (block), val))
3256 continue;
3257 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3259 if (dump_file && (dump_flags & TDF_DETAILS))
3261 fprintf (dump_file, "Found fully redundant value: ");
3262 print_pre_expr (dump_file, expr);
3263 fprintf (dump_file, "\n");
3265 continue;
3268 FOR_EACH_EDGE (pred, ei, block->preds)
3270 unsigned int vprime;
3272 /* We should never run insertion for the exit block
3273 and so not come across fake pred edges. */
3274 gcc_assert (!(pred->flags & EDGE_FAKE));
3275 bprime = pred->src;
3276 eprime = phi_translate (expr, ANTIC_IN (block), NULL,
3277 bprime, block);
3279 /* eprime will generally only be NULL if the
3280 value of the expression, translated
3281 through the PHI for this predecessor, is
3282 undefined. If that is the case, we can't
3283 make the expression fully redundant,
3284 because its value is undefined along a
3285 predecessor path. We can thus break out
3286 early because it doesn't matter what the
3287 rest of the results are. */
3288 if (eprime == NULL)
3290 avail[pred->dest_idx] = NULL;
3291 cant_insert = true;
3292 break;
3295 eprime = fully_constant_expression (eprime);
3296 vprime = get_expr_value_id (eprime);
3297 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3298 vprime);
3299 if (edoubleprime == NULL)
3301 avail[pred->dest_idx] = eprime;
3302 all_same = false;
3304 else
3306 avail[pred->dest_idx] = edoubleprime;
3307 by_some = true;
3308 /* We want to perform insertions to remove a redundancy on
3309 a path in the CFG we want to optimize for speed. */
3310 if (optimize_edge_for_speed_p (pred))
3311 do_insertion = true;
3312 if (first_s == NULL)
3313 first_s = edoubleprime;
3314 else if (!pre_expr_d::equal (first_s, edoubleprime))
3315 all_same = false;
3318 /* If we can insert it, it's not the same value
3319 already existing along every predecessor, and
3320 it's defined by some predecessor, it is
3321 partially redundant. */
3322 if (!cant_insert && !all_same && by_some)
3324 if (!do_insertion)
3326 if (dump_file && (dump_flags & TDF_DETAILS))
3328 fprintf (dump_file, "Skipping partial redundancy for "
3329 "expression ");
3330 print_pre_expr (dump_file, expr);
3331 fprintf (dump_file, " (%04d), no redundancy on to be "
3332 "optimized for speed edge\n", val);
3335 else if (dbg_cnt (treepre_insert))
3337 if (dump_file && (dump_flags & TDF_DETAILS))
3339 fprintf (dump_file, "Found partial redundancy for "
3340 "expression ");
3341 print_pre_expr (dump_file, expr);
3342 fprintf (dump_file, " (%04d)\n",
3343 get_expr_value_id (expr));
3345 if (insert_into_preds_of_block (block,
3346 get_expression_id (expr),
3347 avail))
3348 new_stuff = true;
3351 /* If all edges produce the same value and that value is
3352 an invariant, then the PHI has the same value on all
3353 edges. Note this. */
3354 else if (!cant_insert && all_same)
3356 gcc_assert (edoubleprime->kind == CONSTANT
3357 || edoubleprime->kind == NAME);
3359 tree temp = make_temp_ssa_name (get_expr_type (expr),
3360 NULL, "pretmp");
3361 gassign *assign
3362 = gimple_build_assign (temp,
3363 edoubleprime->kind == CONSTANT ?
3364 PRE_EXPR_CONSTANT (edoubleprime) :
3365 PRE_EXPR_NAME (edoubleprime));
3366 gimple_stmt_iterator gsi = gsi_after_labels (block);
3367 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3369 gimple_set_plf (assign, NECESSARY, false);
3370 VN_INFO_GET (temp)->value_id = val;
3371 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3372 if (VN_INFO (temp)->valnum == NULL_TREE)
3373 VN_INFO (temp)->valnum = temp;
3374 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3375 pre_expr newe = get_or_alloc_expr_for_name (temp);
3376 add_to_value (val, newe);
3377 bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3378 bitmap_insert_into_set (NEW_SETS (block), newe);
3383 exprs.release ();
3384 return new_stuff;
3388 /* Perform insertion for partially anticipatable expressions. There
3389 is only one case we will perform insertion for these. This case is
3390 if the expression is partially anticipatable, and fully available.
3391 In this case, we know that putting it earlier will enable us to
3392 remove the later computation. */
3395 static bool
3396 do_partial_partial_insertion (basic_block block, basic_block dom)
3398 bool new_stuff = false;
3399 vec<pre_expr> exprs;
3400 pre_expr expr;
3401 auto_vec<pre_expr> avail;
3402 int i;
3404 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3405 avail.safe_grow (EDGE_COUNT (block->preds));
3407 FOR_EACH_VEC_ELT (exprs, i, expr)
3409 if (expr->kind == NARY
3410 || expr->kind == REFERENCE)
3412 unsigned int val;
3413 bool by_all = true;
3414 bool cant_insert = false;
3415 edge pred;
3416 basic_block bprime;
3417 pre_expr eprime = NULL;
3418 edge_iterator ei;
3420 val = get_expr_value_id (expr);
3421 if (bitmap_set_contains_value (PHI_GEN (block), val))
3422 continue;
3423 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3424 continue;
3426 FOR_EACH_EDGE (pred, ei, block->preds)
3428 unsigned int vprime;
3429 pre_expr edoubleprime;
3431 /* We should never run insertion for the exit block
3432 and so not come across fake pred edges. */
3433 gcc_assert (!(pred->flags & EDGE_FAKE));
3434 bprime = pred->src;
3435 eprime = phi_translate (expr, ANTIC_IN (block),
3436 PA_IN (block),
3437 bprime, block);
3439 /* eprime will generally only be NULL if the
3440 value of the expression, translated
3441 through the PHI for this predecessor, is
3442 undefined. If that is the case, we can't
3443 make the expression fully redundant,
3444 because its value is undefined along a
3445 predecessor path. We can thus break out
3446 early because it doesn't matter what the
3447 rest of the results are. */
3448 if (eprime == NULL)
3450 avail[pred->dest_idx] = NULL;
3451 cant_insert = true;
3452 break;
3455 eprime = fully_constant_expression (eprime);
3456 vprime = get_expr_value_id (eprime);
3457 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3458 avail[pred->dest_idx] = edoubleprime;
3459 if (edoubleprime == NULL)
3461 by_all = false;
3462 break;
3466 /* If we can insert it, it's not the same value
3467 already existing along every predecessor, and
3468 it's defined by some predecessor, it is
3469 partially redundant. */
3470 if (!cant_insert && by_all)
3472 edge succ;
3473 bool do_insertion = false;
3475 /* Insert only if we can remove a later expression on a path
3476 that we want to optimize for speed.
3477 The phi node that we will be inserting in BLOCK is not free,
3478 and inserting it for the sake of !optimize_for_speed successor
3479 may cause regressions on the speed path. */
3480 FOR_EACH_EDGE (succ, ei, block->succs)
3482 if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3483 || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3485 if (optimize_edge_for_speed_p (succ))
3486 do_insertion = true;
3490 if (!do_insertion)
3492 if (dump_file && (dump_flags & TDF_DETAILS))
3494 fprintf (dump_file, "Skipping partial partial redundancy "
3495 "for expression ");
3496 print_pre_expr (dump_file, expr);
3497 fprintf (dump_file, " (%04d), not (partially) anticipated "
3498 "on any to be optimized for speed edges\n", val);
3501 else if (dbg_cnt (treepre_insert))
3503 pre_stats.pa_insert++;
3504 if (dump_file && (dump_flags & TDF_DETAILS))
3506 fprintf (dump_file, "Found partial partial redundancy "
3507 "for expression ");
3508 print_pre_expr (dump_file, expr);
3509 fprintf (dump_file, " (%04d)\n",
3510 get_expr_value_id (expr));
3512 if (insert_into_preds_of_block (block,
3513 get_expression_id (expr),
3514 avail))
3515 new_stuff = true;
3521 exprs.release ();
3522 return new_stuff;
3525 static bool
3526 insert_aux (basic_block block)
3528 basic_block son;
3529 bool new_stuff = false;
3531 if (block)
3533 basic_block dom;
3534 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3535 if (dom)
3537 unsigned i;
3538 bitmap_iterator bi;
3539 bitmap_set_t newset = NEW_SETS (dom);
3540 if (newset)
3542 /* Note that we need to value_replace both NEW_SETS, and
3543 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3544 represented by some non-simple expression here that we want
3545 to replace it with. */
3546 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3548 pre_expr expr = expression_for_id (i);
3549 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3550 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3553 if (!single_pred_p (block))
3555 new_stuff |= do_regular_insertion (block, dom);
3556 if (do_partial_partial)
3557 new_stuff |= do_partial_partial_insertion (block, dom);
3561 for (son = first_dom_son (CDI_DOMINATORS, block);
3562 son;
3563 son = next_dom_son (CDI_DOMINATORS, son))
3565 new_stuff |= insert_aux (son);
3568 return new_stuff;
3571 /* Perform insertion of partially redundant values. */
3573 static void
3574 insert (void)
3576 bool new_stuff = true;
3577 basic_block bb;
3578 int num_iterations = 0;
3580 FOR_ALL_BB_FN (bb, cfun)
3581 NEW_SETS (bb) = bitmap_set_new ();
3583 while (new_stuff)
3585 num_iterations++;
3586 if (dump_file && dump_flags & TDF_DETAILS)
3587 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3588 new_stuff = insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun));
3590 /* Clear the NEW sets before the next iteration. We have already
3591 fully propagated its contents. */
3592 if (new_stuff)
3593 FOR_ALL_BB_FN (bb, cfun)
3594 bitmap_set_free (NEW_SETS (bb));
3596 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3600 /* Compute the AVAIL set for all basic blocks.
3602 This function performs value numbering of the statements in each basic
3603 block. The AVAIL sets are built from information we glean while doing
3604 this value numbering, since the AVAIL sets contain only one entry per
3605 value.
3607 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3608 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3610 static void
3611 compute_avail (void)
3614 basic_block block, son;
3615 basic_block *worklist;
3616 size_t sp = 0;
3617 unsigned i;
3619 /* We pretend that default definitions are defined in the entry block.
3620 This includes function arguments and the static chain decl. */
3621 for (i = 1; i < num_ssa_names; ++i)
3623 tree name = ssa_name (i);
3624 pre_expr e;
3625 if (!name
3626 || !SSA_NAME_IS_DEFAULT_DEF (name)
3627 || has_zero_uses (name)
3628 || virtual_operand_p (name))
3629 continue;
3631 e = get_or_alloc_expr_for_name (name);
3632 add_to_value (get_expr_value_id (e), e);
3633 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), e);
3634 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3638 if (dump_file && (dump_flags & TDF_DETAILS))
3640 print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3641 "tmp_gen", ENTRY_BLOCK);
3642 print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3643 "avail_out", ENTRY_BLOCK);
3646 /* Allocate the worklist. */
3647 worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
3649 /* Seed the algorithm by putting the dominator children of the entry
3650 block on the worklist. */
3651 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun));
3652 son;
3653 son = next_dom_son (CDI_DOMINATORS, son))
3654 worklist[sp++] = son;
3656 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun))
3657 = ssa_default_def (cfun, gimple_vop (cfun));
3659 /* Loop until the worklist is empty. */
3660 while (sp)
3662 gimple stmt;
3663 basic_block dom;
3665 /* Pick a block from the worklist. */
3666 block = worklist[--sp];
3668 /* Initially, the set of available values in BLOCK is that of
3669 its immediate dominator. */
3670 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3671 if (dom)
3673 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3674 BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
3677 /* Generate values for PHI nodes. */
3678 for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
3679 gsi_next (&gsi))
3681 tree result = gimple_phi_result (gsi.phi ());
3683 /* We have no need for virtual phis, as they don't represent
3684 actual computations. */
3685 if (virtual_operand_p (result))
3687 BB_LIVE_VOP_ON_EXIT (block) = result;
3688 continue;
3691 pre_expr e = get_or_alloc_expr_for_name (result);
3692 add_to_value (get_expr_value_id (e), e);
3693 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3694 bitmap_insert_into_set (PHI_GEN (block), e);
3697 BB_MAY_NOTRETURN (block) = 0;
3699 /* Now compute value numbers and populate value sets with all
3700 the expressions computed in BLOCK. */
3701 for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
3702 gsi_next (&gsi))
3704 ssa_op_iter iter;
3705 tree op;
3707 stmt = gsi_stmt (gsi);
3709 /* Cache whether the basic-block has any non-visible side-effect
3710 or control flow.
3711 If this isn't a call or it is the last stmt in the
3712 basic-block then the CFG represents things correctly. */
3713 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3715 /* Non-looping const functions always return normally.
3716 Otherwise the call might not return or have side-effects
3717 that forbids hoisting possibly trapping expressions
3718 before it. */
3719 int flags = gimple_call_flags (stmt);
3720 if (!(flags & ECF_CONST)
3721 || (flags & ECF_LOOPING_CONST_OR_PURE))
3722 BB_MAY_NOTRETURN (block) = 1;
3725 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3727 pre_expr e = get_or_alloc_expr_for_name (op);
3729 add_to_value (get_expr_value_id (e), e);
3730 bitmap_insert_into_set (TMP_GEN (block), e);
3731 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3734 if (gimple_vdef (stmt))
3735 BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
3737 if (gimple_has_side_effects (stmt)
3738 || stmt_could_throw_p (stmt)
3739 || is_gimple_debug (stmt))
3740 continue;
3742 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3744 if (ssa_undefined_value_p (op))
3745 continue;
3746 pre_expr e = get_or_alloc_expr_for_name (op);
3747 bitmap_value_insert_into_set (EXP_GEN (block), e);
3750 switch (gimple_code (stmt))
3752 case GIMPLE_RETURN:
3753 continue;
3755 case GIMPLE_CALL:
3757 vn_reference_t ref;
3758 vn_reference_s ref1;
3759 pre_expr result = NULL;
3761 /* We can value number only calls to real functions. */
3762 if (gimple_call_internal_p (stmt))
3763 continue;
3765 vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
3766 if (!ref)
3767 continue;
3769 /* If the value of the call is not invalidated in
3770 this block until it is computed, add the expression
3771 to EXP_GEN. */
3772 if (!gimple_vuse (stmt)
3773 || gimple_code
3774 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3775 || gimple_bb (SSA_NAME_DEF_STMT
3776 (gimple_vuse (stmt))) != block)
3778 result = pre_expr_pool.allocate ();
3779 result->kind = REFERENCE;
3780 result->id = 0;
3781 PRE_EXPR_REFERENCE (result) = ref;
3783 get_or_alloc_expression_id (result);
3784 add_to_value (get_expr_value_id (result), result);
3785 bitmap_value_insert_into_set (EXP_GEN (block), result);
3787 continue;
3790 case GIMPLE_ASSIGN:
3792 pre_expr result = NULL;
3793 switch (vn_get_stmt_kind (stmt))
3795 case VN_NARY:
3797 enum tree_code code = gimple_assign_rhs_code (stmt);
3798 vn_nary_op_t nary;
3800 /* COND_EXPR and VEC_COND_EXPR are awkward in
3801 that they contain an embedded complex expression.
3802 Don't even try to shove those through PRE. */
3803 if (code == COND_EXPR
3804 || code == VEC_COND_EXPR)
3805 continue;
3807 vn_nary_op_lookup_stmt (stmt, &nary);
3808 if (!nary)
3809 continue;
3811 /* If the NARY traps and there was a preceding
3812 point in the block that might not return avoid
3813 adding the nary to EXP_GEN. */
3814 if (BB_MAY_NOTRETURN (block)
3815 && vn_nary_may_trap (nary))
3816 continue;
3818 result = pre_expr_pool.allocate ();
3819 result->kind = NARY;
3820 result->id = 0;
3821 PRE_EXPR_NARY (result) = nary;
3822 break;
3825 case VN_REFERENCE:
3827 vn_reference_t ref;
3828 vn_reference_lookup (gimple_assign_rhs1 (stmt),
3829 gimple_vuse (stmt),
3830 VN_WALK, &ref);
3831 if (!ref)
3832 continue;
3834 /* If the value of the reference is not invalidated in
3835 this block until it is computed, add the expression
3836 to EXP_GEN. */
3837 if (gimple_vuse (stmt))
3839 gimple def_stmt;
3840 bool ok = true;
3841 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
3842 while (!gimple_nop_p (def_stmt)
3843 && gimple_code (def_stmt) != GIMPLE_PHI
3844 && gimple_bb (def_stmt) == block)
3846 if (stmt_may_clobber_ref_p
3847 (def_stmt, gimple_assign_rhs1 (stmt)))
3849 ok = false;
3850 break;
3852 def_stmt
3853 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
3855 if (!ok)
3856 continue;
3859 result = pre_expr_pool.allocate ();
3860 result->kind = REFERENCE;
3861 result->id = 0;
3862 PRE_EXPR_REFERENCE (result) = ref;
3863 break;
3866 default:
3867 continue;
3870 get_or_alloc_expression_id (result);
3871 add_to_value (get_expr_value_id (result), result);
3872 bitmap_value_insert_into_set (EXP_GEN (block), result);
3873 continue;
3875 default:
3876 break;
3880 if (dump_file && (dump_flags & TDF_DETAILS))
3882 print_bitmap_set (dump_file, EXP_GEN (block),
3883 "exp_gen", block->index);
3884 print_bitmap_set (dump_file, PHI_GEN (block),
3885 "phi_gen", block->index);
3886 print_bitmap_set (dump_file, TMP_GEN (block),
3887 "tmp_gen", block->index);
3888 print_bitmap_set (dump_file, AVAIL_OUT (block),
3889 "avail_out", block->index);
3892 /* Put the dominator children of BLOCK on the worklist of blocks
3893 to compute available sets for. */
3894 for (son = first_dom_son (CDI_DOMINATORS, block);
3895 son;
3896 son = next_dom_son (CDI_DOMINATORS, son))
3897 worklist[sp++] = son;
3900 free (worklist);
3904 /* Local state for the eliminate domwalk. */
3905 static vec<gimple> el_to_remove;
3906 static vec<gimple> el_to_fixup;
3907 static unsigned int el_todo;
3908 static vec<tree> el_avail;
3909 static vec<tree> el_avail_stack;
3911 /* Return a leader for OP that is available at the current point of the
3912 eliminate domwalk. */
3914 static tree
3915 eliminate_avail (tree op)
3917 tree valnum = VN_INFO (op)->valnum;
3918 if (TREE_CODE (valnum) == SSA_NAME)
3920 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
3921 return valnum;
3922 if (el_avail.length () > SSA_NAME_VERSION (valnum))
3923 return el_avail[SSA_NAME_VERSION (valnum)];
3925 else if (is_gimple_min_invariant (valnum))
3926 return valnum;
3927 return NULL_TREE;
3930 /* At the current point of the eliminate domwalk make OP available. */
3932 static void
3933 eliminate_push_avail (tree op)
3935 tree valnum = VN_INFO (op)->valnum;
3936 if (TREE_CODE (valnum) == SSA_NAME)
3938 if (el_avail.length () <= SSA_NAME_VERSION (valnum))
3939 el_avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1);
3940 tree pushop = op;
3941 if (el_avail[SSA_NAME_VERSION (valnum)])
3942 pushop = el_avail[SSA_NAME_VERSION (valnum)];
3943 el_avail_stack.safe_push (pushop);
3944 el_avail[SSA_NAME_VERSION (valnum)] = op;
3948 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
3949 the leader for the expression if insertion was successful. */
3951 static tree
3952 eliminate_insert (gimple_stmt_iterator *gsi, tree val)
3954 tree expr = vn_get_expr_for (val);
3955 if (!CONVERT_EXPR_P (expr)
3956 && TREE_CODE (expr) != VIEW_CONVERT_EXPR)
3957 return NULL_TREE;
3959 tree op = TREE_OPERAND (expr, 0);
3960 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (op) : op;
3961 if (!leader)
3962 return NULL_TREE;
3964 tree res = make_temp_ssa_name (TREE_TYPE (val), NULL, "pretmp");
3965 gassign *tem = gimple_build_assign (res,
3966 fold_build1 (TREE_CODE (expr),
3967 TREE_TYPE (expr), leader));
3968 gsi_insert_before (gsi, tem, GSI_SAME_STMT);
3969 VN_INFO_GET (res)->valnum = val;
3971 if (TREE_CODE (leader) == SSA_NAME)
3972 gimple_set_plf (SSA_NAME_DEF_STMT (leader), NECESSARY, true);
3974 pre_stats.insertions++;
3975 if (dump_file && (dump_flags & TDF_DETAILS))
3977 fprintf (dump_file, "Inserted ");
3978 print_gimple_stmt (dump_file, tem, 0, 0);
3981 return res;
3984 class eliminate_dom_walker : public dom_walker
3986 public:
3987 eliminate_dom_walker (cdi_direction direction, bool do_pre_)
3988 : dom_walker (direction), do_pre (do_pre_) {}
3990 virtual void before_dom_children (basic_block);
3991 virtual void after_dom_children (basic_block);
3993 bool do_pre;
3996 /* Perform elimination for the basic-block B during the domwalk. */
3998 void
3999 eliminate_dom_walker::before_dom_children (basic_block b)
4001 /* Mark new bb. */
4002 el_avail_stack.safe_push (NULL_TREE);
4004 /* ??? If we do nothing for unreachable blocks then this will confuse
4005 tailmerging. Eventually we can reduce its reliance on SCCVN now
4006 that we fully copy/constant-propagate (most) things. */
4008 for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
4010 gphi *phi = gsi.phi ();
4011 tree res = PHI_RESULT (phi);
4013 if (virtual_operand_p (res))
4015 gsi_next (&gsi);
4016 continue;
4019 tree sprime = eliminate_avail (res);
4020 if (sprime
4021 && sprime != res)
4023 if (dump_file && (dump_flags & TDF_DETAILS))
4025 fprintf (dump_file, "Replaced redundant PHI node defining ");
4026 print_generic_expr (dump_file, res, 0);
4027 fprintf (dump_file, " with ");
4028 print_generic_expr (dump_file, sprime, 0);
4029 fprintf (dump_file, "\n");
4032 /* If we inserted this PHI node ourself, it's not an elimination. */
4033 if (inserted_exprs
4034 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
4035 pre_stats.phis--;
4036 else
4037 pre_stats.eliminations++;
4039 /* If we will propagate into all uses don't bother to do
4040 anything. */
4041 if (may_propagate_copy (res, sprime))
4043 /* Mark the PHI for removal. */
4044 el_to_remove.safe_push (phi);
4045 gsi_next (&gsi);
4046 continue;
4049 remove_phi_node (&gsi, false);
4051 if (inserted_exprs
4052 && !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res))
4053 && TREE_CODE (sprime) == SSA_NAME)
4054 gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
4056 if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
4057 sprime = fold_convert (TREE_TYPE (res), sprime);
4058 gimple stmt = gimple_build_assign (res, sprime);
4059 /* ??? It cannot yet be necessary (DOM walk). */
4060 gimple_set_plf (stmt, NECESSARY, gimple_plf (phi, NECESSARY));
4062 gimple_stmt_iterator gsi2 = gsi_after_labels (b);
4063 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
4064 continue;
4067 eliminate_push_avail (res);
4068 gsi_next (&gsi);
4071 for (gimple_stmt_iterator gsi = gsi_start_bb (b);
4072 !gsi_end_p (gsi);
4073 gsi_next (&gsi))
4075 tree sprime = NULL_TREE;
4076 gimple stmt = gsi_stmt (gsi);
4077 tree lhs = gimple_get_lhs (stmt);
4078 if (lhs && TREE_CODE (lhs) == SSA_NAME
4079 && !gimple_has_volatile_ops (stmt)
4080 /* See PR43491. Do not replace a global register variable when
4081 it is a the RHS of an assignment. Do replace local register
4082 variables since gcc does not guarantee a local variable will
4083 be allocated in register.
4084 ??? The fix isn't effective here. This should instead
4085 be ensured by not value-numbering them the same but treating
4086 them like volatiles? */
4087 && !(gimple_assign_single_p (stmt)
4088 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
4089 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
4090 && is_global_var (gimple_assign_rhs1 (stmt)))))
4092 sprime = eliminate_avail (lhs);
4093 if (!sprime)
4095 /* If there is no existing usable leader but SCCVN thinks
4096 it has an expression it wants to use as replacement,
4097 insert that. */
4098 tree val = VN_INFO (lhs)->valnum;
4099 if (val != VN_TOP
4100 && TREE_CODE (val) == SSA_NAME
4101 && VN_INFO (val)->needs_insertion
4102 && VN_INFO (val)->expr != NULL_TREE
4103 && (sprime = eliminate_insert (&gsi, val)) != NULL_TREE)
4104 eliminate_push_avail (sprime);
4107 /* If this now constitutes a copy duplicate points-to
4108 and range info appropriately. This is especially
4109 important for inserted code. See tree-ssa-copy.c
4110 for similar code. */
4111 if (sprime
4112 && TREE_CODE (sprime) == SSA_NAME)
4114 basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime));
4115 if (POINTER_TYPE_P (TREE_TYPE (lhs))
4116 && SSA_NAME_PTR_INFO (lhs)
4117 && !SSA_NAME_PTR_INFO (sprime))
4119 duplicate_ssa_name_ptr_info (sprime,
4120 SSA_NAME_PTR_INFO (lhs));
4121 if (b != sprime_b)
4122 mark_ptr_info_alignment_unknown
4123 (SSA_NAME_PTR_INFO (sprime));
4125 else if (!POINTER_TYPE_P (TREE_TYPE (lhs))
4126 && SSA_NAME_RANGE_INFO (lhs)
4127 && !SSA_NAME_RANGE_INFO (sprime)
4128 && b == sprime_b)
4129 duplicate_ssa_name_range_info (sprime,
4130 SSA_NAME_RANGE_TYPE (lhs),
4131 SSA_NAME_RANGE_INFO (lhs));
4134 /* Inhibit the use of an inserted PHI on a loop header when
4135 the address of the memory reference is a simple induction
4136 variable. In other cases the vectorizer won't do anything
4137 anyway (either it's loop invariant or a complicated
4138 expression). */
4139 if (sprime
4140 && TREE_CODE (sprime) == SSA_NAME
4141 && do_pre
4142 && flag_tree_loop_vectorize
4143 && loop_outer (b->loop_father)
4144 && has_zero_uses (sprime)
4145 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
4146 && gimple_assign_load_p (stmt))
4148 gimple def_stmt = SSA_NAME_DEF_STMT (sprime);
4149 basic_block def_bb = gimple_bb (def_stmt);
4150 if (gimple_code (def_stmt) == GIMPLE_PHI
4151 && b->loop_father->header == def_bb)
4153 ssa_op_iter iter;
4154 tree op;
4155 bool found = false;
4156 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
4158 affine_iv iv;
4159 def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
4160 if (def_bb
4161 && flow_bb_inside_loop_p (b->loop_father, def_bb)
4162 && simple_iv (b->loop_father,
4163 b->loop_father, op, &iv, true))
4165 found = true;
4166 break;
4169 if (found)
4171 if (dump_file && (dump_flags & TDF_DETAILS))
4173 fprintf (dump_file, "Not replacing ");
4174 print_gimple_expr (dump_file, stmt, 0, 0);
4175 fprintf (dump_file, " with ");
4176 print_generic_expr (dump_file, sprime, 0);
4177 fprintf (dump_file, " which would add a loop"
4178 " carried dependence to loop %d\n",
4179 b->loop_father->num);
4181 /* Don't keep sprime available. */
4182 sprime = NULL_TREE;
4187 if (sprime)
4189 /* If we can propagate the value computed for LHS into
4190 all uses don't bother doing anything with this stmt. */
4191 if (may_propagate_copy (lhs, sprime))
4193 /* Mark it for removal. */
4194 el_to_remove.safe_push (stmt);
4196 /* ??? Don't count copy/constant propagations. */
4197 if (gimple_assign_single_p (stmt)
4198 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
4199 || gimple_assign_rhs1 (stmt) == sprime))
4200 continue;
4202 if (dump_file && (dump_flags & TDF_DETAILS))
4204 fprintf (dump_file, "Replaced ");
4205 print_gimple_expr (dump_file, stmt, 0, 0);
4206 fprintf (dump_file, " with ");
4207 print_generic_expr (dump_file, sprime, 0);
4208 fprintf (dump_file, " in all uses of ");
4209 print_gimple_stmt (dump_file, stmt, 0, 0);
4212 pre_stats.eliminations++;
4213 continue;
4216 /* If this is an assignment from our leader (which
4217 happens in the case the value-number is a constant)
4218 then there is nothing to do. */
4219 if (gimple_assign_single_p (stmt)
4220 && sprime == gimple_assign_rhs1 (stmt))
4221 continue;
4223 /* Else replace its RHS. */
4224 bool can_make_abnormal_goto
4225 = is_gimple_call (stmt)
4226 && stmt_can_make_abnormal_goto (stmt);
4228 if (dump_file && (dump_flags & TDF_DETAILS))
4230 fprintf (dump_file, "Replaced ");
4231 print_gimple_expr (dump_file, stmt, 0, 0);
4232 fprintf (dump_file, " with ");
4233 print_generic_expr (dump_file, sprime, 0);
4234 fprintf (dump_file, " in ");
4235 print_gimple_stmt (dump_file, stmt, 0, 0);
4238 if (TREE_CODE (sprime) == SSA_NAME)
4239 gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
4240 NECESSARY, true);
4242 pre_stats.eliminations++;
4243 gimple orig_stmt = stmt;
4244 if (!useless_type_conversion_p (TREE_TYPE (lhs),
4245 TREE_TYPE (sprime)))
4246 sprime = fold_convert (TREE_TYPE (lhs), sprime);
4247 tree vdef = gimple_vdef (stmt);
4248 tree vuse = gimple_vuse (stmt);
4249 propagate_tree_value_into_stmt (&gsi, sprime);
4250 stmt = gsi_stmt (gsi);
4251 update_stmt (stmt);
4252 if (vdef != gimple_vdef (stmt))
4253 VN_INFO (vdef)->valnum = vuse;
4255 /* If we removed EH side-effects from the statement, clean
4256 its EH information. */
4257 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4259 bitmap_set_bit (need_eh_cleanup,
4260 gimple_bb (stmt)->index);
4261 if (dump_file && (dump_flags & TDF_DETAILS))
4262 fprintf (dump_file, " Removed EH side-effects.\n");
4265 /* Likewise for AB side-effects. */
4266 if (can_make_abnormal_goto
4267 && !stmt_can_make_abnormal_goto (stmt))
4269 bitmap_set_bit (need_ab_cleanup,
4270 gimple_bb (stmt)->index);
4271 if (dump_file && (dump_flags & TDF_DETAILS))
4272 fprintf (dump_file, " Removed AB side-effects.\n");
4275 continue;
4279 /* If the statement is a scalar store, see if the expression
4280 has the same value number as its rhs. If so, the store is
4281 dead. */
4282 if (gimple_assign_single_p (stmt)
4283 && !gimple_has_volatile_ops (stmt)
4284 && !is_gimple_reg (gimple_assign_lhs (stmt))
4285 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
4286 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
4288 tree val;
4289 tree rhs = gimple_assign_rhs1 (stmt);
4290 val = vn_reference_lookup (gimple_assign_lhs (stmt),
4291 gimple_vuse (stmt), VN_WALK, NULL);
4292 if (TREE_CODE (rhs) == SSA_NAME)
4293 rhs = VN_INFO (rhs)->valnum;
4294 if (val
4295 && operand_equal_p (val, rhs, 0))
4297 if (dump_file && (dump_flags & TDF_DETAILS))
4299 fprintf (dump_file, "Deleted redundant store ");
4300 print_gimple_stmt (dump_file, stmt, 0, 0);
4303 /* Queue stmt for removal. */
4304 el_to_remove.safe_push (stmt);
4305 continue;
4309 bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
4310 bool was_noreturn = (is_gimple_call (stmt)
4311 && gimple_call_noreturn_p (stmt));
4312 tree vdef = gimple_vdef (stmt);
4313 tree vuse = gimple_vuse (stmt);
4315 /* If we didn't replace the whole stmt (or propagate the result
4316 into all uses), replace all uses on this stmt with their
4317 leaders. */
4318 use_operand_p use_p;
4319 ssa_op_iter iter;
4320 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
4322 tree use = USE_FROM_PTR (use_p);
4323 /* ??? The call code above leaves stmt operands un-updated. */
4324 if (TREE_CODE (use) != SSA_NAME)
4325 continue;
4326 tree sprime = eliminate_avail (use);
4327 if (sprime && sprime != use
4328 && may_propagate_copy (use, sprime)
4329 /* We substitute into debug stmts to avoid excessive
4330 debug temporaries created by removed stmts, but we need
4331 to avoid doing so for inserted sprimes as we never want
4332 to create debug temporaries for them. */
4333 && (!inserted_exprs
4334 || TREE_CODE (sprime) != SSA_NAME
4335 || !is_gimple_debug (stmt)
4336 || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
4338 propagate_value (use_p, sprime);
4339 gimple_set_modified (stmt, true);
4340 if (TREE_CODE (sprime) == SSA_NAME
4341 && !is_gimple_debug (stmt))
4342 gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
4343 NECESSARY, true);
4347 /* Visit indirect calls and turn them into direct calls if
4348 possible using the devirtualization machinery. */
4349 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
4351 tree fn = gimple_call_fn (call_stmt);
4352 if (fn
4353 && flag_devirtualize
4354 && virtual_method_call_p (fn))
4356 tree otr_type = obj_type_ref_class (fn);
4357 tree instance;
4358 ipa_polymorphic_call_context context (current_function_decl, fn, stmt, &instance);
4359 bool final;
4361 context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn), otr_type, stmt);
4363 vec <cgraph_node *>targets
4364 = possible_polymorphic_call_targets (obj_type_ref_class (fn),
4365 tree_to_uhwi
4366 (OBJ_TYPE_REF_TOKEN (fn)),
4367 context,
4368 &final);
4369 if (dump_file)
4370 dump_possible_polymorphic_call_targets (dump_file,
4371 obj_type_ref_class (fn),
4372 tree_to_uhwi
4373 (OBJ_TYPE_REF_TOKEN (fn)),
4374 context);
4375 if (final && targets.length () <= 1 && dbg_cnt (devirt))
4377 tree fn;
4378 if (targets.length () == 1)
4379 fn = targets[0]->decl;
4380 else
4381 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
4382 if (dump_enabled_p ())
4384 location_t loc = gimple_location_safe (stmt);
4385 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
4386 "converting indirect call to "
4387 "function %s\n",
4388 cgraph_node::get (fn)->name ());
4390 gimple_call_set_fndecl (call_stmt, fn);
4391 maybe_remove_unused_call_args (cfun, call_stmt);
4392 gimple_set_modified (stmt, true);
4397 if (gimple_modified_p (stmt))
4399 /* If a formerly non-invariant ADDR_EXPR is turned into an
4400 invariant one it was on a separate stmt. */
4401 if (gimple_assign_single_p (stmt)
4402 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
4403 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
4404 gimple old_stmt = stmt;
4405 if (is_gimple_call (stmt))
4407 /* ??? Only fold calls inplace for now, this may create new
4408 SSA names which in turn will confuse free_scc_vn SSA name
4409 release code. */
4410 fold_stmt_inplace (&gsi);
4411 /* When changing a call into a noreturn call, cfg cleanup
4412 is needed to fix up the noreturn call. */
4413 if (!was_noreturn && gimple_call_noreturn_p (stmt))
4414 el_to_fixup.safe_push (stmt);
4416 else
4418 fold_stmt (&gsi);
4419 stmt = gsi_stmt (gsi);
4420 if ((gimple_code (stmt) == GIMPLE_COND
4421 && (gimple_cond_true_p (as_a <gcond *> (stmt))
4422 || gimple_cond_false_p (as_a <gcond *> (stmt))))
4423 || (gimple_code (stmt) == GIMPLE_SWITCH
4424 && TREE_CODE (gimple_switch_index (
4425 as_a <gswitch *> (stmt)))
4426 == INTEGER_CST))
4427 el_todo |= TODO_cleanup_cfg;
4429 /* If we removed EH side-effects from the statement, clean
4430 its EH information. */
4431 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
4433 bitmap_set_bit (need_eh_cleanup,
4434 gimple_bb (stmt)->index);
4435 if (dump_file && (dump_flags & TDF_DETAILS))
4436 fprintf (dump_file, " Removed EH side-effects.\n");
4438 /* Likewise for AB side-effects. */
4439 if (can_make_abnormal_goto
4440 && !stmt_can_make_abnormal_goto (stmt))
4442 bitmap_set_bit (need_ab_cleanup,
4443 gimple_bb (stmt)->index);
4444 if (dump_file && (dump_flags & TDF_DETAILS))
4445 fprintf (dump_file, " Removed AB side-effects.\n");
4447 update_stmt (stmt);
4448 if (vdef != gimple_vdef (stmt))
4449 VN_INFO (vdef)->valnum = vuse;
4452 /* Make new values available - for fully redundant LHS we
4453 continue with the next stmt above and skip this. */
4454 def_operand_p defp;
4455 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
4456 eliminate_push_avail (DEF_FROM_PTR (defp));
4459 /* Replace destination PHI arguments. */
4460 edge_iterator ei;
4461 edge e;
4462 FOR_EACH_EDGE (e, ei, b->succs)
4464 for (gphi_iterator gsi = gsi_start_phis (e->dest);
4465 !gsi_end_p (gsi);
4466 gsi_next (&gsi))
4468 gphi *phi = gsi.phi ();
4469 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
4470 tree arg = USE_FROM_PTR (use_p);
4471 if (TREE_CODE (arg) != SSA_NAME
4472 || virtual_operand_p (arg))
4473 continue;
4474 tree sprime = eliminate_avail (arg);
4475 if (sprime && may_propagate_copy (arg, sprime))
4477 propagate_value (use_p, sprime);
4478 if (TREE_CODE (sprime) == SSA_NAME)
4479 gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
4485 /* Make no longer available leaders no longer available. */
4487 void
4488 eliminate_dom_walker::after_dom_children (basic_block)
4490 tree entry;
4491 while ((entry = el_avail_stack.pop ()) != NULL_TREE)
4493 tree valnum = VN_INFO (entry)->valnum;
4494 tree old = el_avail[SSA_NAME_VERSION (valnum)];
4495 if (old == entry)
4496 el_avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
4497 else
4498 el_avail[SSA_NAME_VERSION (valnum)] = entry;
4502 /* Eliminate fully redundant computations. */
4504 static unsigned int
4505 eliminate (bool do_pre)
4507 gimple_stmt_iterator gsi;
4508 gimple stmt;
4510 need_eh_cleanup = BITMAP_ALLOC (NULL);
4511 need_ab_cleanup = BITMAP_ALLOC (NULL);
4513 el_to_remove.create (0);
4514 el_to_fixup.create (0);
4515 el_todo = 0;
4516 el_avail.create (num_ssa_names);
4517 el_avail_stack.create (0);
4519 eliminate_dom_walker (CDI_DOMINATORS,
4520 do_pre).walk (cfun->cfg->x_entry_block_ptr);
4522 el_avail.release ();
4523 el_avail_stack.release ();
4525 /* We cannot remove stmts during BB walk, especially not release SSA
4526 names there as this confuses the VN machinery. The stmts ending
4527 up in el_to_remove are either stores or simple copies.
4528 Remove stmts in reverse order to make debug stmt creation possible. */
4529 while (!el_to_remove.is_empty ())
4531 stmt = el_to_remove.pop ();
4533 if (dump_file && (dump_flags & TDF_DETAILS))
4535 fprintf (dump_file, "Removing dead stmt ");
4536 print_gimple_stmt (dump_file, stmt, 0, 0);
4539 tree lhs;
4540 if (gimple_code (stmt) == GIMPLE_PHI)
4541 lhs = gimple_phi_result (stmt);
4542 else
4543 lhs = gimple_get_lhs (stmt);
4545 if (inserted_exprs
4546 && TREE_CODE (lhs) == SSA_NAME)
4547 bitmap_clear_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
4549 gsi = gsi_for_stmt (stmt);
4550 if (gimple_code (stmt) == GIMPLE_PHI)
4551 remove_phi_node (&gsi, true);
4552 else
4554 basic_block bb = gimple_bb (stmt);
4555 unlink_stmt_vdef (stmt);
4556 if (gsi_remove (&gsi, true))
4557 bitmap_set_bit (need_eh_cleanup, bb->index);
4558 release_defs (stmt);
4561 /* Removing a stmt may expose a forwarder block. */
4562 el_todo |= TODO_cleanup_cfg;
4564 el_to_remove.release ();
4566 /* Fixup stmts that became noreturn calls. This may require splitting
4567 blocks and thus isn't possible during the dominator walk. Do this
4568 in reverse order so we don't inadvertedly remove a stmt we want to
4569 fixup by visiting a dominating now noreturn call first. */
4570 while (!el_to_fixup.is_empty ())
4572 stmt = el_to_fixup.pop ();
4574 if (dump_file && (dump_flags & TDF_DETAILS))
4576 fprintf (dump_file, "Fixing up noreturn call ");
4577 print_gimple_stmt (dump_file, stmt, 0, 0);
4580 if (fixup_noreturn_call (stmt))
4581 el_todo |= TODO_cleanup_cfg;
4583 el_to_fixup.release ();
4585 return el_todo;
4588 /* Perform CFG cleanups made necessary by elimination. */
4590 static unsigned
4591 fini_eliminate (void)
4593 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
4594 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
4596 if (do_eh_cleanup)
4597 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
4599 if (do_ab_cleanup)
4600 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
4602 BITMAP_FREE (need_eh_cleanup);
4603 BITMAP_FREE (need_ab_cleanup);
4605 if (do_eh_cleanup || do_ab_cleanup)
4606 return TODO_cleanup_cfg;
4607 return 0;
4610 /* Borrow a bit of tree-ssa-dce.c for the moment.
4611 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4612 this may be a bit faster, and we may want critical edges kept split. */
4614 /* If OP's defining statement has not already been determined to be necessary,
4615 mark that statement necessary. Return the stmt, if it is newly
4616 necessary. */
4618 static inline gimple
4619 mark_operand_necessary (tree op)
4621 gimple stmt;
4623 gcc_assert (op);
4625 if (TREE_CODE (op) != SSA_NAME)
4626 return NULL;
4628 stmt = SSA_NAME_DEF_STMT (op);
4629 gcc_assert (stmt);
4631 if (gimple_plf (stmt, NECESSARY)
4632 || gimple_nop_p (stmt))
4633 return NULL;
4635 gimple_set_plf (stmt, NECESSARY, true);
4636 return stmt;
4639 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4640 to insert PHI nodes sometimes, and because value numbering of casts isn't
4641 perfect, we sometimes end up inserting dead code. This simple DCE-like
4642 pass removes any insertions we made that weren't actually used. */
4644 static void
4645 remove_dead_inserted_code (void)
4647 bitmap worklist;
4648 unsigned i;
4649 bitmap_iterator bi;
4650 gimple t;
4652 worklist = BITMAP_ALLOC (NULL);
4653 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4655 t = SSA_NAME_DEF_STMT (ssa_name (i));
4656 if (gimple_plf (t, NECESSARY))
4657 bitmap_set_bit (worklist, i);
4659 while (!bitmap_empty_p (worklist))
4661 i = bitmap_first_set_bit (worklist);
4662 bitmap_clear_bit (worklist, i);
4663 t = SSA_NAME_DEF_STMT (ssa_name (i));
4665 /* PHI nodes are somewhat special in that each PHI alternative has
4666 data and control dependencies. All the statements feeding the
4667 PHI node's arguments are always necessary. */
4668 if (gimple_code (t) == GIMPLE_PHI)
4670 unsigned k;
4672 for (k = 0; k < gimple_phi_num_args (t); k++)
4674 tree arg = PHI_ARG_DEF (t, k);
4675 if (TREE_CODE (arg) == SSA_NAME)
4677 gimple n = mark_operand_necessary (arg);
4678 if (n)
4679 bitmap_set_bit (worklist, SSA_NAME_VERSION (arg));
4683 else
4685 /* Propagate through the operands. Examine all the USE, VUSE and
4686 VDEF operands in this statement. Mark all the statements
4687 which feed this statement's uses as necessary. */
4688 ssa_op_iter iter;
4689 tree use;
4691 /* The operands of VDEF expressions are also needed as they
4692 represent potential definitions that may reach this
4693 statement (VDEF operands allow us to follow def-def
4694 links). */
4696 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
4698 gimple n = mark_operand_necessary (use);
4699 if (n)
4700 bitmap_set_bit (worklist, SSA_NAME_VERSION (use));
4705 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4707 t = SSA_NAME_DEF_STMT (ssa_name (i));
4708 if (!gimple_plf (t, NECESSARY))
4710 gimple_stmt_iterator gsi;
4712 if (dump_file && (dump_flags & TDF_DETAILS))
4714 fprintf (dump_file, "Removing unnecessary insertion:");
4715 print_gimple_stmt (dump_file, t, 0, 0);
4718 gsi = gsi_for_stmt (t);
4719 if (gimple_code (t) == GIMPLE_PHI)
4720 remove_phi_node (&gsi, true);
4721 else
4723 gsi_remove (&gsi, true);
4724 release_defs (t);
4728 BITMAP_FREE (worklist);
4732 /* Initialize data structures used by PRE. */
4734 static void
4735 init_pre (void)
4737 basic_block bb;
4739 next_expression_id = 1;
4740 expressions.create (0);
4741 expressions.safe_push (NULL);
4742 value_expressions.create (get_max_value_id () + 1);
4743 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
4744 name_to_id.create (0);
4746 inserted_exprs = BITMAP_ALLOC (NULL);
4748 connect_infinite_loops_to_exit ();
4749 memset (&pre_stats, 0, sizeof (pre_stats));
4751 postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
4752 postorder_num = inverted_post_order_compute (postorder);
4754 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4756 calculate_dominance_info (CDI_POST_DOMINATORS);
4757 calculate_dominance_info (CDI_DOMINATORS);
4759 bitmap_obstack_initialize (&grand_bitmap_obstack);
4760 phi_translate_table = new hash_table<expr_pred_trans_d> (5110);
4761 expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4762 FOR_ALL_BB_FN (bb, cfun)
4764 EXP_GEN (bb) = bitmap_set_new ();
4765 PHI_GEN (bb) = bitmap_set_new ();
4766 TMP_GEN (bb) = bitmap_set_new ();
4767 AVAIL_OUT (bb) = bitmap_set_new ();
4772 /* Deallocate data structures used by PRE. */
4774 static void
4775 fini_pre ()
4777 free (postorder);
4778 value_expressions.release ();
4779 BITMAP_FREE (inserted_exprs);
4780 bitmap_obstack_release (&grand_bitmap_obstack);
4781 bitmap_set_pool.release ();
4782 pre_expr_pool.release ();
4783 delete phi_translate_table;
4784 phi_translate_table = NULL;
4785 delete expression_to_id;
4786 expression_to_id = NULL;
4787 name_to_id.release ();
4789 free_aux_for_blocks ();
4791 free_dominance_info (CDI_POST_DOMINATORS);
4794 namespace {
4796 const pass_data pass_data_pre =
4798 GIMPLE_PASS, /* type */
4799 "pre", /* name */
4800 OPTGROUP_NONE, /* optinfo_flags */
4801 TV_TREE_PRE, /* tv_id */
4802 /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before
4803 pass_pre. */
4804 ( PROP_no_crit_edges | PROP_cfg | PROP_ssa ), /* properties_required */
4805 0, /* properties_provided */
4806 PROP_no_crit_edges, /* properties_destroyed */
4807 TODO_rebuild_alias, /* todo_flags_start */
4808 0, /* todo_flags_finish */
4811 class pass_pre : public gimple_opt_pass
4813 public:
4814 pass_pre (gcc::context *ctxt)
4815 : gimple_opt_pass (pass_data_pre, ctxt)
4818 /* opt_pass methods: */
4819 virtual bool gate (function *) { return flag_tree_pre != 0; }
4820 virtual unsigned int execute (function *);
4822 }; // class pass_pre
4824 unsigned int
4825 pass_pre::execute (function *fun)
4827 unsigned int todo = 0;
4829 do_partial_partial =
4830 flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4832 /* This has to happen before SCCVN runs because
4833 loop_optimizer_init may create new phis, etc. */
4834 loop_optimizer_init (LOOPS_NORMAL);
4836 if (!run_scc_vn (VN_WALK))
4838 loop_optimizer_finalize ();
4839 return 0;
4842 init_pre ();
4843 scev_initialize ();
4845 /* Collect and value number expressions computed in each basic block. */
4846 compute_avail ();
4848 /* Insert can get quite slow on an incredibly large number of basic
4849 blocks due to some quadratic behavior. Until this behavior is
4850 fixed, don't run it when he have an incredibly large number of
4851 bb's. If we aren't going to run insert, there is no point in
4852 computing ANTIC, either, even though it's plenty fast. */
4853 if (n_basic_blocks_for_fn (fun) < 4000)
4855 compute_antic ();
4856 insert ();
4859 /* Make sure to remove fake edges before committing our inserts.
4860 This makes sure we don't end up with extra critical edges that
4861 we would need to split. */
4862 remove_fake_exit_edges ();
4863 gsi_commit_edge_inserts ();
4865 /* Eliminate folds statements which might (should not...) end up
4866 not keeping virtual operands up-to-date. */
4867 gcc_assert (!need_ssa_update_p (fun));
4869 /* Remove all the redundant expressions. */
4870 todo |= eliminate (true);
4872 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4873 statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4874 statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4875 statistics_counter_event (fun, "Eliminated", pre_stats.eliminations);
4877 clear_expression_ids ();
4878 remove_dead_inserted_code ();
4880 scev_finalize ();
4881 fini_pre ();
4882 todo |= fini_eliminate ();
4883 loop_optimizer_finalize ();
4885 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4886 case we can merge the block with the remaining predecessor of the block.
4887 It should either:
4888 - call merge_blocks after each tail merge iteration
4889 - call merge_blocks after all tail merge iterations
4890 - mark TODO_cleanup_cfg when necessary
4891 - share the cfg cleanup with fini_pre. */
4892 todo |= tail_merge_optimize (todo);
4894 free_scc_vn ();
4896 /* Tail merging invalidates the virtual SSA web, together with
4897 cfg-cleanup opportunities exposed by PRE this will wreck the
4898 SSA updating machinery. So make sure to run update-ssa
4899 manually, before eventually scheduling cfg-cleanup as part of
4900 the todo. */
4901 update_ssa (TODO_update_ssa_only_virtuals);
4903 return todo;
4906 } // anon namespace
4908 gimple_opt_pass *
4909 make_pass_pre (gcc::context *ctxt)
4911 return new pass_pre (ctxt);
4914 namespace {
4916 const pass_data pass_data_fre =
4918 GIMPLE_PASS, /* type */
4919 "fre", /* name */
4920 OPTGROUP_NONE, /* optinfo_flags */
4921 TV_TREE_FRE, /* tv_id */
4922 ( PROP_cfg | PROP_ssa ), /* properties_required */
4923 0, /* properties_provided */
4924 0, /* properties_destroyed */
4925 0, /* todo_flags_start */
4926 0, /* todo_flags_finish */
4929 class pass_fre : public gimple_opt_pass
4931 public:
4932 pass_fre (gcc::context *ctxt)
4933 : gimple_opt_pass (pass_data_fre, ctxt)
4936 /* opt_pass methods: */
4937 opt_pass * clone () { return new pass_fre (m_ctxt); }
4938 virtual bool gate (function *) { return flag_tree_fre != 0; }
4939 virtual unsigned int execute (function *);
4941 }; // class pass_fre
4943 unsigned int
4944 pass_fre::execute (function *fun)
4946 unsigned int todo = 0;
4948 if (!run_scc_vn (VN_WALKREWRITE))
4949 return 0;
4951 memset (&pre_stats, 0, sizeof (pre_stats));
4953 /* Remove all the redundant expressions. */
4954 todo |= eliminate (false);
4956 todo |= fini_eliminate ();
4958 free_scc_vn ();
4960 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4961 statistics_counter_event (fun, "Eliminated", pre_stats.eliminations);
4963 return todo;
4966 } // anon namespace
4968 gimple_opt_pass *
4969 make_pass_fre (gcc::context *ctxt)
4971 return new pass_fre (ctxt);