2012-12-01 Alessandro Fanfarillo <alessandro.fanfarillo@gmail.com>
[official-gcc.git] / gcc / tree-ssa-pre.c
blob9c95ef6a2d303b6967f05a91498ee2a7aac61521
1 /* SSA-PRE for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
5 <stevenb@suse.de>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3. If not see
21 <http://www.gnu.org/licenses/>. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "tree.h"
28 #include "basic-block.h"
29 #include "gimple-pretty-print.h"
30 #include "tree-inline.h"
31 #include "tree-flow.h"
32 #include "gimple.h"
33 #include "hash-table.h"
34 #include "tree-iterator.h"
35 #include "alloc-pool.h"
36 #include "obstack.h"
37 #include "tree-pass.h"
38 #include "flags.h"
39 #include "bitmap.h"
40 #include "langhooks.h"
41 #include "cfgloop.h"
42 #include "tree-ssa-sccvn.h"
43 #include "tree-scalar-evolution.h"
44 #include "params.h"
45 #include "dbgcnt.h"
46 #include "domwalk.h"
48 /* TODO:
50 1. Avail sets can be shared by making an avail_find_leader that
51 walks up the dominator tree and looks in those avail sets.
52 This might affect code optimality, it's unclear right now.
53 2. Strength reduction can be performed by anticipating expressions
54 we can repair later on.
55 3. We can do back-substitution or smarter value numbering to catch
56 commutative expressions split up over multiple statements.
59 /* For ease of terminology, "expression node" in the below refers to
60 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
61 represent the actual statement containing the expressions we care about,
62 and we cache the value number by putting it in the expression. */
64 /* Basic algorithm
66 First we walk the statements to generate the AVAIL sets, the
67 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
68 generation of values/expressions by a given block. We use them
69 when computing the ANTIC sets. The AVAIL sets consist of
70 SSA_NAME's that represent values, so we know what values are
71 available in what blocks. AVAIL is a forward dataflow problem. In
72 SSA, values are never killed, so we don't need a kill set, or a
73 fixpoint iteration, in order to calculate the AVAIL sets. In
74 traditional parlance, AVAIL sets tell us the downsafety of the
75 expressions/values.
77 Next, we generate the ANTIC sets. These sets represent the
78 anticipatable expressions. ANTIC is a backwards dataflow
79 problem. An expression is anticipatable in a given block if it could
80 be generated in that block. This means that if we had to perform
81 an insertion in that block, of the value of that expression, we
82 could. Calculating the ANTIC sets requires phi translation of
83 expressions, because the flow goes backwards through phis. We must
84 iterate to a fixpoint of the ANTIC sets, because we have a kill
85 set. Even in SSA form, values are not live over the entire
86 function, only from their definition point onwards. So we have to
87 remove values from the ANTIC set once we go past the definition
88 point of the leaders that make them up.
89 compute_antic/compute_antic_aux performs this computation.
91 Third, we perform insertions to make partially redundant
92 expressions fully redundant.
94 An expression is partially redundant (excluding partial
95 anticipation) if:
97 1. It is AVAIL in some, but not all, of the predecessors of a
98 given block.
99 2. It is ANTIC in all the predecessors.
101 In order to make it fully redundant, we insert the expression into
102 the predecessors where it is not available, but is ANTIC.
104 For the partial anticipation case, we only perform insertion if it
105 is partially anticipated in some block, and fully available in all
106 of the predecessors.
108 insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
109 performs these steps.
111 Fourth, we eliminate fully redundant expressions.
112 This is a simple statement walk that replaces redundant
113 calculations with the now available values. */
115 /* Representations of value numbers:
117 Value numbers are represented by a representative SSA_NAME. We
118 will create fake SSA_NAME's in situations where we need a
119 representative but do not have one (because it is a complex
120 expression). In order to facilitate storing the value numbers in
121 bitmaps, and keep the number of wasted SSA_NAME's down, we also
122 associate a value_id with each value number, and create full blown
123 ssa_name's only where we actually need them (IE in operands of
124 existing expressions).
126 Theoretically you could replace all the value_id's with
127 SSA_NAME_VERSION, but this would allocate a large number of
128 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
129 It would also require an additional indirection at each point we
130 use the value id. */
132 /* Representation of expressions on value numbers:
134 Expressions consisting of value numbers are represented the same
135 way as our VN internally represents them, with an additional
136 "pre_expr" wrapping around them in order to facilitate storing all
137 of the expressions in the same sets. */
139 /* Representation of sets:
141 The dataflow sets do not need to be sorted in any particular order
142 for the majority of their lifetime, are simply represented as two
143 bitmaps, one that keeps track of values present in the set, and one
144 that keeps track of expressions present in the set.
146 When we need them in topological order, we produce it on demand by
147 transforming the bitmap into an array and sorting it into topo
148 order. */
150 /* Type of expression, used to know which member of the PRE_EXPR union
151 is valid. */
153 enum pre_expr_kind
155 NAME,
156 NARY,
157 REFERENCE,
158 CONSTANT
161 typedef union pre_expr_union_d
163 tree name;
164 tree constant;
165 vn_nary_op_t nary;
166 vn_reference_t reference;
167 } pre_expr_union;
169 typedef struct pre_expr_d : typed_noop_remove <pre_expr_d>
171 enum pre_expr_kind kind;
172 unsigned int id;
173 pre_expr_union u;
175 /* hash_table support. */
176 typedef pre_expr_d value_type;
177 typedef pre_expr_d compare_type;
178 static inline hashval_t hash (const pre_expr_d *);
179 static inline int equal (const pre_expr_d *, const pre_expr_d *);
180 } *pre_expr;
182 #define PRE_EXPR_NAME(e) (e)->u.name
183 #define PRE_EXPR_NARY(e) (e)->u.nary
184 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
185 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
187 /* Compare E1 and E1 for equality. */
189 inline int
190 pre_expr_d::equal (const value_type *e1, const compare_type *e2)
192 if (e1->kind != e2->kind)
193 return false;
195 switch (e1->kind)
197 case CONSTANT:
198 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
199 PRE_EXPR_CONSTANT (e2));
200 case NAME:
201 return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
202 case NARY:
203 return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
204 case REFERENCE:
205 return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
206 PRE_EXPR_REFERENCE (e2));
207 default:
208 gcc_unreachable ();
212 /* Hash E. */
214 inline hashval_t
215 pre_expr_d::hash (const value_type *e)
217 switch (e->kind)
219 case CONSTANT:
220 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
221 case NAME:
222 return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
223 case NARY:
224 return PRE_EXPR_NARY (e)->hashcode;
225 case REFERENCE:
226 return PRE_EXPR_REFERENCE (e)->hashcode;
227 default:
228 gcc_unreachable ();
232 /* Next global expression id number. */
233 static unsigned int next_expression_id;
235 /* Mapping from expression to id number we can use in bitmap sets. */
236 static vec<pre_expr> expressions;
237 static hash_table <pre_expr_d> expression_to_id;
238 static vec<unsigned> name_to_id;
240 /* Allocate an expression id for EXPR. */
242 static inline unsigned int
243 alloc_expression_id (pre_expr expr)
245 struct pre_expr_d **slot;
246 /* Make sure we won't overflow. */
247 gcc_assert (next_expression_id + 1 > next_expression_id);
248 expr->id = next_expression_id++;
249 expressions.safe_push (expr);
250 if (expr->kind == NAME)
252 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
253 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
254 re-allocations by using vec::reserve upfront. There is no
255 vec::quick_grow_cleared unfortunately. */
256 unsigned old_len = name_to_id.length ();
257 name_to_id.reserve (num_ssa_names - old_len);
258 name_to_id.safe_grow_cleared (num_ssa_names);
259 gcc_assert (name_to_id[version] == 0);
260 name_to_id[version] = expr->id;
262 else
264 slot = expression_to_id.find_slot (expr, INSERT);
265 gcc_assert (!*slot);
266 *slot = expr;
268 return next_expression_id - 1;
271 /* Return the expression id for tree EXPR. */
273 static inline unsigned int
274 get_expression_id (const pre_expr expr)
276 return expr->id;
279 static inline unsigned int
280 lookup_expression_id (const pre_expr expr)
282 struct pre_expr_d **slot;
284 if (expr->kind == NAME)
286 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
287 if (name_to_id.length () <= version)
288 return 0;
289 return name_to_id[version];
291 else
293 slot = expression_to_id.find_slot (expr, NO_INSERT);
294 if (!slot)
295 return 0;
296 return ((pre_expr)*slot)->id;
300 /* Return the existing expression id for EXPR, or create one if one
301 does not exist yet. */
303 static inline unsigned int
304 get_or_alloc_expression_id (pre_expr expr)
306 unsigned int id = lookup_expression_id (expr);
307 if (id == 0)
308 return alloc_expression_id (expr);
309 return expr->id = id;
312 /* Return the expression that has expression id ID */
314 static inline pre_expr
315 expression_for_id (unsigned int id)
317 return expressions[id];
320 /* Free the expression id field in all of our expressions,
321 and then destroy the expressions array. */
323 static void
324 clear_expression_ids (void)
326 expressions.release ();
329 static alloc_pool pre_expr_pool;
331 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
333 static pre_expr
334 get_or_alloc_expr_for_name (tree name)
336 struct pre_expr_d expr;
337 pre_expr result;
338 unsigned int result_id;
340 expr.kind = NAME;
341 expr.id = 0;
342 PRE_EXPR_NAME (&expr) = name;
343 result_id = lookup_expression_id (&expr);
344 if (result_id != 0)
345 return expression_for_id (result_id);
347 result = (pre_expr) pool_alloc (pre_expr_pool);
348 result->kind = NAME;
349 PRE_EXPR_NAME (result) = name;
350 alloc_expression_id (result);
351 return result;
354 /* An unordered bitmap set. One bitmap tracks values, the other,
355 expressions. */
356 typedef struct bitmap_set
358 bitmap_head expressions;
359 bitmap_head values;
360 } *bitmap_set_t;
362 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
363 EXECUTE_IF_SET_IN_BITMAP(&(set)->expressions, 0, (id), (bi))
365 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
366 EXECUTE_IF_SET_IN_BITMAP(&(set)->values, 0, (id), (bi))
368 /* Mapping from value id to expressions with that value_id. */
369 static vec<bitmap> value_expressions;
371 /* Sets that we need to keep track of. */
372 typedef struct bb_bitmap_sets
374 /* The EXP_GEN set, which represents expressions/values generated in
375 a basic block. */
376 bitmap_set_t exp_gen;
378 /* The PHI_GEN set, which represents PHI results generated in a
379 basic block. */
380 bitmap_set_t phi_gen;
382 /* The TMP_GEN set, which represents results/temporaries generated
383 in a basic block. IE the LHS of an expression. */
384 bitmap_set_t tmp_gen;
386 /* The AVAIL_OUT set, which represents which values are available in
387 a given basic block. */
388 bitmap_set_t avail_out;
390 /* The ANTIC_IN set, which represents which values are anticipatable
391 in a given basic block. */
392 bitmap_set_t antic_in;
394 /* The PA_IN set, which represents which values are
395 partially anticipatable in a given basic block. */
396 bitmap_set_t pa_in;
398 /* The NEW_SETS set, which is used during insertion to augment the
399 AVAIL_OUT set of blocks with the new insertions performed during
400 the current iteration. */
401 bitmap_set_t new_sets;
403 /* A cache for value_dies_in_block_x. */
404 bitmap expr_dies;
406 /* True if we have visited this block during ANTIC calculation. */
407 unsigned int visited : 1;
409 /* True we have deferred processing this block during ANTIC
410 calculation until its successor is processed. */
411 unsigned int deferred : 1;
413 /* True when the block contains a call that might not return. */
414 unsigned int contains_may_not_return_call : 1;
415 } *bb_value_sets_t;
417 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
418 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
419 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
420 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
421 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
422 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
423 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
424 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
425 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
426 #define BB_DEFERRED(BB) ((bb_value_sets_t) ((BB)->aux))->deferred
427 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
430 /* Basic block list in postorder. */
431 static int *postorder;
432 static int postorder_num;
434 /* This structure is used to keep track of statistics on what
435 optimization PRE was able to perform. */
436 static struct
438 /* The number of RHS computations eliminated by PRE. */
439 int eliminations;
441 /* The number of new expressions/temporaries generated by PRE. */
442 int insertions;
444 /* The number of inserts found due to partial anticipation */
445 int pa_insert;
447 /* The number of new PHI nodes added by PRE. */
448 int phis;
450 /* The number of values found constant. */
451 int constified;
453 } pre_stats;
455 static bool do_partial_partial;
456 static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
457 static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
458 static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
459 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
460 static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
461 static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
462 static void bitmap_insert_into_set_1 (bitmap_set_t, pre_expr,
463 unsigned int, bool);
464 static bitmap_set_t bitmap_set_new (void);
465 static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
466 tree);
467 static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
468 static unsigned int get_expr_value_id (pre_expr);
470 /* We can add and remove elements and entries to and from sets
471 and hash tables, so we use alloc pools for them. */
473 static alloc_pool bitmap_set_pool;
474 static bitmap_obstack grand_bitmap_obstack;
476 /* Set of blocks with statements that have had their EH properties changed. */
477 static bitmap need_eh_cleanup;
479 /* Set of blocks with statements that have had their AB properties changed. */
480 static bitmap need_ab_cleanup;
482 /* A three tuple {e, pred, v} used to cache phi translations in the
483 phi_translate_table. */
485 typedef struct expr_pred_trans_d : typed_free_remove<expr_pred_trans_d>
487 /* The expression. */
488 pre_expr e;
490 /* The predecessor block along which we translated the expression. */
491 basic_block pred;
493 /* The value that resulted from the translation. */
494 pre_expr v;
496 /* The hashcode for the expression, pred pair. This is cached for
497 speed reasons. */
498 hashval_t hashcode;
500 /* hash_table support. */
501 typedef expr_pred_trans_d value_type;
502 typedef expr_pred_trans_d compare_type;
503 static inline hashval_t hash (const value_type *);
504 static inline int equal (const value_type *, const compare_type *);
505 } *expr_pred_trans_t;
506 typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
508 inline hashval_t
509 expr_pred_trans_d::hash (const expr_pred_trans_d *e)
511 return e->hashcode;
514 inline int
515 expr_pred_trans_d::equal (const value_type *ve1,
516 const compare_type *ve2)
518 basic_block b1 = ve1->pred;
519 basic_block b2 = ve2->pred;
521 /* If they are not translations for the same basic block, they can't
522 be equal. */
523 if (b1 != b2)
524 return false;
525 return pre_expr_d::equal (ve1->e, ve2->e);
528 /* The phi_translate_table caches phi translations for a given
529 expression and predecessor. */
530 static hash_table <expr_pred_trans_d> phi_translate_table;
532 /* Search in the phi translation table for the translation of
533 expression E in basic block PRED.
534 Return the translated value, if found, NULL otherwise. */
536 static inline pre_expr
537 phi_trans_lookup (pre_expr e, basic_block pred)
539 expr_pred_trans_t *slot;
540 struct expr_pred_trans_d ept;
542 ept.e = e;
543 ept.pred = pred;
544 ept.hashcode = iterative_hash_hashval_t (pre_expr_d::hash (e), pred->index);
545 slot = phi_translate_table.find_slot_with_hash (&ept, ept.hashcode,
546 NO_INSERT);
547 if (!slot)
548 return NULL;
549 else
550 return (*slot)->v;
554 /* Add the tuple mapping from {expression E, basic block PRED} to
555 value V, to the phi translation table. */
557 static inline void
558 phi_trans_add (pre_expr e, pre_expr v, basic_block pred)
560 expr_pred_trans_t *slot;
561 expr_pred_trans_t new_pair = XNEW (struct expr_pred_trans_d);
562 new_pair->e = e;
563 new_pair->pred = pred;
564 new_pair->v = v;
565 new_pair->hashcode = iterative_hash_hashval_t (pre_expr_d::hash (e),
566 pred->index);
568 slot = phi_translate_table.find_slot_with_hash (new_pair,
569 new_pair->hashcode, INSERT);
570 free (*slot);
571 *slot = new_pair;
575 /* Add expression E to the expression set of value id V. */
577 static void
578 add_to_value (unsigned int v, pre_expr e)
580 bitmap set;
582 gcc_checking_assert (get_expr_value_id (e) == v);
584 if (v >= value_expressions.length ())
586 value_expressions.safe_grow_cleared (v + 1);
589 set = value_expressions[v];
590 if (!set)
592 set = BITMAP_ALLOC (&grand_bitmap_obstack);
593 value_expressions[v] = set;
596 bitmap_set_bit (set, get_or_alloc_expression_id (e));
599 /* Create a new bitmap set and return it. */
601 static bitmap_set_t
602 bitmap_set_new (void)
604 bitmap_set_t ret = (bitmap_set_t) pool_alloc (bitmap_set_pool);
605 bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
606 bitmap_initialize (&ret->values, &grand_bitmap_obstack);
607 return ret;
610 /* Return the value id for a PRE expression EXPR. */
612 static unsigned int
613 get_expr_value_id (pre_expr expr)
615 unsigned int id;
616 switch (expr->kind)
618 case CONSTANT:
619 id = get_constant_value_id (PRE_EXPR_CONSTANT (expr));
620 break;
621 case NAME:
622 id = VN_INFO (PRE_EXPR_NAME (expr))->value_id;
623 break;
624 case NARY:
625 id = PRE_EXPR_NARY (expr)->value_id;
626 break;
627 case REFERENCE:
628 id = PRE_EXPR_REFERENCE (expr)->value_id;
629 break;
630 default:
631 gcc_unreachable ();
633 /* ??? We cannot assert that expr has a value-id (it can be 0), because
634 we assign value-ids only to expressions that have a result
635 in set_hashtable_value_ids. */
636 return id;
639 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
641 static tree
642 sccvn_valnum_from_value_id (unsigned int val)
644 bitmap_iterator bi;
645 unsigned int i;
646 bitmap exprset = value_expressions[val];
647 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
649 pre_expr vexpr = expression_for_id (i);
650 if (vexpr->kind == NAME)
651 return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
652 else if (vexpr->kind == CONSTANT)
653 return PRE_EXPR_CONSTANT (vexpr);
655 return NULL_TREE;
658 /* Remove an expression EXPR from a bitmapped set. */
660 static void
661 bitmap_remove_from_set (bitmap_set_t set, pre_expr expr)
663 unsigned int val = get_expr_value_id (expr);
664 if (!value_id_constant_p (val))
666 bitmap_clear_bit (&set->values, val);
667 bitmap_clear_bit (&set->expressions, get_expression_id (expr));
671 static void
672 bitmap_insert_into_set_1 (bitmap_set_t set, pre_expr expr,
673 unsigned int val, bool allow_constants)
675 if (allow_constants || !value_id_constant_p (val))
677 /* We specifically expect this and only this function to be able to
678 insert constants into a set. */
679 bitmap_set_bit (&set->values, val);
680 bitmap_set_bit (&set->expressions, get_or_alloc_expression_id (expr));
684 /* Insert an expression EXPR into a bitmapped set. */
686 static void
687 bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
689 bitmap_insert_into_set_1 (set, expr, get_expr_value_id (expr), false);
692 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
694 static void
695 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
697 bitmap_copy (&dest->expressions, &orig->expressions);
698 bitmap_copy (&dest->values, &orig->values);
702 /* Free memory used up by SET. */
703 static void
704 bitmap_set_free (bitmap_set_t set)
706 bitmap_clear (&set->expressions);
707 bitmap_clear (&set->values);
711 /* Generate an topological-ordered array of bitmap set SET. */
713 static vec<pre_expr>
714 sorted_array_from_bitmap_set (bitmap_set_t set)
716 unsigned int i, j;
717 bitmap_iterator bi, bj;
718 vec<pre_expr> result;
720 /* Pre-allocate roughly enough space for the array. */
721 result.create (bitmap_count_bits (&set->values));
723 FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
725 /* The number of expressions having a given value is usually
726 relatively small. Thus, rather than making a vector of all
727 the expressions and sorting it by value-id, we walk the values
728 and check in the reverse mapping that tells us what expressions
729 have a given value, to filter those in our set. As a result,
730 the expressions are inserted in value-id order, which means
731 topological order.
733 If this is somehow a significant lose for some cases, we can
734 choose which set to walk based on the set size. */
735 bitmap exprset = value_expressions[i];
736 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj)
738 if (bitmap_bit_p (&set->expressions, j))
739 result.safe_push (expression_for_id (j));
743 return result;
746 /* Perform bitmapped set operation DEST &= ORIG. */
748 static void
749 bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig)
751 bitmap_iterator bi;
752 unsigned int i;
754 if (dest != orig)
756 bitmap_head temp;
757 bitmap_initialize (&temp, &grand_bitmap_obstack);
759 bitmap_and_into (&dest->values, &orig->values);
760 bitmap_copy (&temp, &dest->expressions);
761 EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
763 pre_expr expr = expression_for_id (i);
764 unsigned int value_id = get_expr_value_id (expr);
765 if (!bitmap_bit_p (&dest->values, value_id))
766 bitmap_clear_bit (&dest->expressions, i);
768 bitmap_clear (&temp);
772 /* Subtract all values and expressions contained in ORIG from DEST. */
774 static bitmap_set_t
775 bitmap_set_subtract (bitmap_set_t dest, bitmap_set_t orig)
777 bitmap_set_t result = bitmap_set_new ();
778 bitmap_iterator bi;
779 unsigned int i;
781 bitmap_and_compl (&result->expressions, &dest->expressions,
782 &orig->expressions);
784 FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
786 pre_expr expr = expression_for_id (i);
787 unsigned int value_id = get_expr_value_id (expr);
788 bitmap_set_bit (&result->values, value_id);
791 return result;
794 /* Subtract all the values in bitmap set B from bitmap set A. */
796 static void
797 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
799 unsigned int i;
800 bitmap_iterator bi;
801 bitmap_head temp;
803 bitmap_initialize (&temp, &grand_bitmap_obstack);
805 bitmap_copy (&temp, &a->expressions);
806 EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
808 pre_expr expr = expression_for_id (i);
809 if (bitmap_set_contains_value (b, get_expr_value_id (expr)))
810 bitmap_remove_from_set (a, expr);
812 bitmap_clear (&temp);
816 /* Return true if bitmapped set SET contains the value VALUE_ID. */
818 static bool
819 bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
821 if (value_id_constant_p (value_id))
822 return true;
824 if (!set || bitmap_empty_p (&set->expressions))
825 return false;
827 return bitmap_bit_p (&set->values, value_id);
830 static inline bool
831 bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr)
833 return bitmap_bit_p (&set->expressions, get_expression_id (expr));
836 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
838 static void
839 bitmap_set_replace_value (bitmap_set_t set, unsigned int lookfor,
840 const pre_expr expr)
842 bitmap exprset;
843 unsigned int i;
844 bitmap_iterator bi;
846 if (value_id_constant_p (lookfor))
847 return;
849 if (!bitmap_set_contains_value (set, lookfor))
850 return;
852 /* The number of expressions having a given value is usually
853 significantly less than the total number of expressions in SET.
854 Thus, rather than check, for each expression in SET, whether it
855 has the value LOOKFOR, we walk the reverse mapping that tells us
856 what expressions have a given value, and see if any of those
857 expressions are in our set. For large testcases, this is about
858 5-10x faster than walking the bitmap. If this is somehow a
859 significant lose for some cases, we can choose which set to walk
860 based on the set size. */
861 exprset = value_expressions[lookfor];
862 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
864 if (bitmap_clear_bit (&set->expressions, i))
866 bitmap_set_bit (&set->expressions, get_expression_id (expr));
867 return;
872 /* Return true if two bitmap sets are equal. */
874 static bool
875 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
877 return bitmap_equal_p (&a->values, &b->values);
880 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
881 and add it otherwise. */
883 static void
884 bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
886 unsigned int val = get_expr_value_id (expr);
888 if (bitmap_set_contains_value (set, val))
889 bitmap_set_replace_value (set, val, expr);
890 else
891 bitmap_insert_into_set (set, expr);
894 /* Insert EXPR into SET if EXPR's value is not already present in
895 SET. */
897 static void
898 bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
900 unsigned int val = get_expr_value_id (expr);
902 gcc_checking_assert (expr->id == get_or_alloc_expression_id (expr));
904 /* Constant values are always considered to be part of the set. */
905 if (value_id_constant_p (val))
906 return;
908 /* If the value membership changed, add the expression. */
909 if (bitmap_set_bit (&set->values, val))
910 bitmap_set_bit (&set->expressions, expr->id);
913 /* Print out EXPR to outfile. */
915 static void
916 print_pre_expr (FILE *outfile, const pre_expr expr)
918 switch (expr->kind)
920 case CONSTANT:
921 print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr), 0);
922 break;
923 case NAME:
924 print_generic_expr (outfile, PRE_EXPR_NAME (expr), 0);
925 break;
926 case NARY:
928 unsigned int i;
929 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
930 fprintf (outfile, "{%s,", tree_code_name [nary->opcode]);
931 for (i = 0; i < nary->length; i++)
933 print_generic_expr (outfile, nary->op[i], 0);
934 if (i != (unsigned) nary->length - 1)
935 fprintf (outfile, ",");
937 fprintf (outfile, "}");
939 break;
941 case REFERENCE:
943 vn_reference_op_t vro;
944 unsigned int i;
945 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
946 fprintf (outfile, "{");
947 for (i = 0;
948 ref->operands.iterate (i, &vro);
949 i++)
951 bool closebrace = false;
952 if (vro->opcode != SSA_NAME
953 && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
955 fprintf (outfile, "%s", tree_code_name [vro->opcode]);
956 if (vro->op0)
958 fprintf (outfile, "<");
959 closebrace = true;
962 if (vro->op0)
964 print_generic_expr (outfile, vro->op0, 0);
965 if (vro->op1)
967 fprintf (outfile, ",");
968 print_generic_expr (outfile, vro->op1, 0);
970 if (vro->op2)
972 fprintf (outfile, ",");
973 print_generic_expr (outfile, vro->op2, 0);
976 if (closebrace)
977 fprintf (outfile, ">");
978 if (i != ref->operands.length () - 1)
979 fprintf (outfile, ",");
981 fprintf (outfile, "}");
982 if (ref->vuse)
984 fprintf (outfile, "@");
985 print_generic_expr (outfile, ref->vuse, 0);
988 break;
991 void debug_pre_expr (pre_expr);
993 /* Like print_pre_expr but always prints to stderr. */
994 DEBUG_FUNCTION void
995 debug_pre_expr (pre_expr e)
997 print_pre_expr (stderr, e);
998 fprintf (stderr, "\n");
1001 /* Print out SET to OUTFILE. */
1003 static void
1004 print_bitmap_set (FILE *outfile, bitmap_set_t set,
1005 const char *setname, int blockindex)
1007 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
1008 if (set)
1010 bool first = true;
1011 unsigned i;
1012 bitmap_iterator bi;
1014 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1016 const pre_expr expr = expression_for_id (i);
1018 if (!first)
1019 fprintf (outfile, ", ");
1020 first = false;
1021 print_pre_expr (outfile, expr);
1023 fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1026 fprintf (outfile, " }\n");
1029 void debug_bitmap_set (bitmap_set_t);
1031 DEBUG_FUNCTION void
1032 debug_bitmap_set (bitmap_set_t set)
1034 print_bitmap_set (stderr, set, "debug", 0);
1037 void debug_bitmap_sets_for (basic_block);
1039 DEBUG_FUNCTION void
1040 debug_bitmap_sets_for (basic_block bb)
1042 print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1043 print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1044 print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1045 print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1046 print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1047 if (do_partial_partial)
1048 print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1049 print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1052 /* Print out the expressions that have VAL to OUTFILE. */
1054 static void
1055 print_value_expressions (FILE *outfile, unsigned int val)
1057 bitmap set = value_expressions[val];
1058 if (set)
1060 bitmap_set x;
1061 char s[10];
1062 sprintf (s, "%04d", val);
1063 x.expressions = *set;
1064 print_bitmap_set (outfile, &x, s, 0);
1069 DEBUG_FUNCTION void
1070 debug_value_expressions (unsigned int val)
1072 print_value_expressions (stderr, val);
1075 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1076 represent it. */
1078 static pre_expr
1079 get_or_alloc_expr_for_constant (tree constant)
1081 unsigned int result_id;
1082 unsigned int value_id;
1083 struct pre_expr_d expr;
1084 pre_expr newexpr;
1086 expr.kind = CONSTANT;
1087 PRE_EXPR_CONSTANT (&expr) = constant;
1088 result_id = lookup_expression_id (&expr);
1089 if (result_id != 0)
1090 return expression_for_id (result_id);
1092 newexpr = (pre_expr) pool_alloc (pre_expr_pool);
1093 newexpr->kind = CONSTANT;
1094 PRE_EXPR_CONSTANT (newexpr) = constant;
1095 alloc_expression_id (newexpr);
1096 value_id = get_or_alloc_constant_value_id (constant);
1097 add_to_value (value_id, newexpr);
1098 return newexpr;
1101 /* Given a value id V, find the actual tree representing the constant
1102 value if there is one, and return it. Return NULL if we can't find
1103 a constant. */
1105 static tree
1106 get_constant_for_value_id (unsigned int v)
1108 if (value_id_constant_p (v))
1110 unsigned int i;
1111 bitmap_iterator bi;
1112 bitmap exprset = value_expressions[v];
1114 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1116 pre_expr expr = expression_for_id (i);
1117 if (expr->kind == CONSTANT)
1118 return PRE_EXPR_CONSTANT (expr);
1121 return NULL;
1124 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1125 Currently only supports constants and SSA_NAMES. */
1126 static pre_expr
1127 get_or_alloc_expr_for (tree t)
1129 if (TREE_CODE (t) == SSA_NAME)
1130 return get_or_alloc_expr_for_name (t);
1131 else if (is_gimple_min_invariant (t))
1132 return get_or_alloc_expr_for_constant (t);
1133 else
1135 /* More complex expressions can result from SCCVN expression
1136 simplification that inserts values for them. As they all
1137 do not have VOPs the get handled by the nary ops struct. */
1138 vn_nary_op_t result;
1139 unsigned int result_id;
1140 vn_nary_op_lookup (t, &result);
1141 if (result != NULL)
1143 pre_expr e = (pre_expr) pool_alloc (pre_expr_pool);
1144 e->kind = NARY;
1145 PRE_EXPR_NARY (e) = result;
1146 result_id = lookup_expression_id (e);
1147 if (result_id != 0)
1149 pool_free (pre_expr_pool, e);
1150 e = expression_for_id (result_id);
1151 return e;
1153 alloc_expression_id (e);
1154 return e;
1157 return NULL;
1160 /* Return the folded version of T if T, when folded, is a gimple
1161 min_invariant. Otherwise, return T. */
1163 static pre_expr
1164 fully_constant_expression (pre_expr e)
1166 switch (e->kind)
1168 case CONSTANT:
1169 return e;
1170 case NARY:
1172 vn_nary_op_t nary = PRE_EXPR_NARY (e);
1173 switch (TREE_CODE_CLASS (nary->opcode))
1175 case tcc_binary:
1176 case tcc_comparison:
1178 /* We have to go from trees to pre exprs to value ids to
1179 constants. */
1180 tree naryop0 = nary->op[0];
1181 tree naryop1 = nary->op[1];
1182 tree result;
1183 if (!is_gimple_min_invariant (naryop0))
1185 pre_expr rep0 = get_or_alloc_expr_for (naryop0);
1186 unsigned int vrep0 = get_expr_value_id (rep0);
1187 tree const0 = get_constant_for_value_id (vrep0);
1188 if (const0)
1189 naryop0 = fold_convert (TREE_TYPE (naryop0), const0);
1191 if (!is_gimple_min_invariant (naryop1))
1193 pre_expr rep1 = get_or_alloc_expr_for (naryop1);
1194 unsigned int vrep1 = get_expr_value_id (rep1);
1195 tree const1 = get_constant_for_value_id (vrep1);
1196 if (const1)
1197 naryop1 = fold_convert (TREE_TYPE (naryop1), const1);
1199 result = fold_binary (nary->opcode, nary->type,
1200 naryop0, naryop1);
1201 if (result && is_gimple_min_invariant (result))
1202 return get_or_alloc_expr_for_constant (result);
1203 /* We might have simplified the expression to a
1204 SSA_NAME for example from x_1 * 1. But we cannot
1205 insert a PHI for x_1 unconditionally as x_1 might
1206 not be available readily. */
1207 return e;
1209 case tcc_reference:
1210 if (nary->opcode != REALPART_EXPR
1211 && nary->opcode != IMAGPART_EXPR
1212 && nary->opcode != VIEW_CONVERT_EXPR)
1213 return e;
1214 /* Fallthrough. */
1215 case tcc_unary:
1217 /* We have to go from trees to pre exprs to value ids to
1218 constants. */
1219 tree naryop0 = nary->op[0];
1220 tree const0, result;
1221 if (is_gimple_min_invariant (naryop0))
1222 const0 = naryop0;
1223 else
1225 pre_expr rep0 = get_or_alloc_expr_for (naryop0);
1226 unsigned int vrep0 = get_expr_value_id (rep0);
1227 const0 = get_constant_for_value_id (vrep0);
1229 result = NULL;
1230 if (const0)
1232 tree type1 = TREE_TYPE (nary->op[0]);
1233 const0 = fold_convert (type1, const0);
1234 result = fold_unary (nary->opcode, nary->type, const0);
1236 if (result && is_gimple_min_invariant (result))
1237 return get_or_alloc_expr_for_constant (result);
1238 return e;
1240 default:
1241 return e;
1244 case REFERENCE:
1246 vn_reference_t ref = PRE_EXPR_REFERENCE (e);
1247 tree folded;
1248 if ((folded = fully_constant_vn_reference_p (ref)))
1249 return get_or_alloc_expr_for_constant (folded);
1250 return e;
1252 default:
1253 return e;
1255 return e;
1258 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1259 it has the value it would have in BLOCK. Set *SAME_VALID to true
1260 in case the new vuse doesn't change the value id of the OPERANDS. */
1262 static tree
1263 translate_vuse_through_block (vec<vn_reference_op_s> operands,
1264 alias_set_type set, tree type, tree vuse,
1265 basic_block phiblock,
1266 basic_block block, bool *same_valid)
1268 gimple phi = SSA_NAME_DEF_STMT (vuse);
1269 ao_ref ref;
1270 edge e = NULL;
1271 bool use_oracle;
1273 *same_valid = true;
1275 if (gimple_bb (phi) != phiblock)
1276 return vuse;
1278 use_oracle = ao_ref_init_from_vn_reference (&ref, set, type, operands);
1280 /* Use the alias-oracle to find either the PHI node in this block,
1281 the first VUSE used in this block that is equivalent to vuse or
1282 the first VUSE which definition in this block kills the value. */
1283 if (gimple_code (phi) == GIMPLE_PHI)
1284 e = find_edge (block, phiblock);
1285 else if (use_oracle)
1286 while (!stmt_may_clobber_ref_p_1 (phi, &ref))
1288 vuse = gimple_vuse (phi);
1289 phi = SSA_NAME_DEF_STMT (vuse);
1290 if (gimple_bb (phi) != phiblock)
1291 return vuse;
1292 if (gimple_code (phi) == GIMPLE_PHI)
1294 e = find_edge (block, phiblock);
1295 break;
1298 else
1299 return NULL_TREE;
1301 if (e)
1303 if (use_oracle)
1305 bitmap visited = NULL;
1306 unsigned int cnt;
1307 /* Try to find a vuse that dominates this phi node by skipping
1308 non-clobbering statements. */
1309 vuse = get_continuation_for_phi (phi, &ref, &cnt, &visited, false);
1310 if (visited)
1311 BITMAP_FREE (visited);
1313 else
1314 vuse = NULL_TREE;
1315 if (!vuse)
1317 /* If we didn't find any, the value ID can't stay the same,
1318 but return the translated vuse. */
1319 *same_valid = false;
1320 vuse = PHI_ARG_DEF (phi, e->dest_idx);
1322 /* ??? We would like to return vuse here as this is the canonical
1323 upmost vdef that this reference is associated with. But during
1324 insertion of the references into the hash tables we only ever
1325 directly insert with their direct gimple_vuse, hence returning
1326 something else would make us not find the other expression. */
1327 return PHI_ARG_DEF (phi, e->dest_idx);
1330 return NULL_TREE;
1333 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1334 SET2. This is used to avoid making a set consisting of the union
1335 of PA_IN and ANTIC_IN during insert. */
1337 static inline pre_expr
1338 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2)
1340 pre_expr result;
1342 result = bitmap_find_leader (set1, val);
1343 if (!result && set2)
1344 result = bitmap_find_leader (set2, val);
1345 return result;
1348 /* Get the tree type for our PRE expression e. */
1350 static tree
1351 get_expr_type (const pre_expr e)
1353 switch (e->kind)
1355 case NAME:
1356 return TREE_TYPE (PRE_EXPR_NAME (e));
1357 case CONSTANT:
1358 return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1359 case REFERENCE:
1360 return PRE_EXPR_REFERENCE (e)->type;
1361 case NARY:
1362 return PRE_EXPR_NARY (e)->type;
1364 gcc_unreachable();
1367 /* Get a representative SSA_NAME for a given expression.
1368 Since all of our sub-expressions are treated as values, we require
1369 them to be SSA_NAME's for simplicity.
1370 Prior versions of GVNPRE used to use "value handles" here, so that
1371 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1372 either case, the operands are really values (IE we do not expect
1373 them to be usable without finding leaders). */
1375 static tree
1376 get_representative_for (const pre_expr e)
1378 tree name;
1379 unsigned int value_id = get_expr_value_id (e);
1381 switch (e->kind)
1383 case NAME:
1384 return PRE_EXPR_NAME (e);
1385 case CONSTANT:
1386 return PRE_EXPR_CONSTANT (e);
1387 case NARY:
1388 case REFERENCE:
1390 /* Go through all of the expressions representing this value
1391 and pick out an SSA_NAME. */
1392 unsigned int i;
1393 bitmap_iterator bi;
1394 bitmap exprs = value_expressions[value_id];
1395 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1397 pre_expr rep = expression_for_id (i);
1398 if (rep->kind == NAME)
1399 return PRE_EXPR_NAME (rep);
1402 break;
1404 /* If we reached here we couldn't find an SSA_NAME. This can
1405 happen when we've discovered a value that has never appeared in
1406 the program as set to an SSA_NAME, most likely as the result of
1407 phi translation. */
1408 if (dump_file)
1410 fprintf (dump_file,
1411 "Could not find SSA_NAME representative for expression:");
1412 print_pre_expr (dump_file, e);
1413 fprintf (dump_file, "\n");
1416 /* Build and insert the assignment of the end result to the temporary
1417 that we will return. */
1418 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1419 VN_INFO_GET (name)->value_id = value_id;
1420 VN_INFO (name)->valnum = sccvn_valnum_from_value_id (value_id);
1421 if (VN_INFO (name)->valnum == NULL_TREE)
1422 VN_INFO (name)->valnum = name;
1423 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1424 if (dump_file)
1426 fprintf (dump_file, "Created SSA_NAME representative ");
1427 print_generic_expr (dump_file, name, 0);
1428 fprintf (dump_file, " for expression:");
1429 print_pre_expr (dump_file, e);
1430 fprintf (dump_file, "\n");
1433 return name;
1438 static pre_expr
1439 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1440 basic_block pred, basic_block phiblock);
1442 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1443 the phis in PRED. Return NULL if we can't find a leader for each part
1444 of the translated expression. */
1446 static pre_expr
1447 phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1448 basic_block pred, basic_block phiblock)
1450 switch (expr->kind)
1452 case NARY:
1454 unsigned int i;
1455 bool changed = false;
1456 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1457 vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1458 sizeof_vn_nary_op (nary->length));
1459 memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1461 for (i = 0; i < newnary->length; i++)
1463 if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1464 continue;
1465 else
1467 pre_expr leader, result;
1468 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1469 leader = find_leader_in_sets (op_val_id, set1, set2);
1470 result = phi_translate (leader, set1, set2, pred, phiblock);
1471 if (result && result != leader)
1473 tree name = get_representative_for (result);
1474 if (!name)
1475 return NULL;
1476 newnary->op[i] = name;
1478 else if (!result)
1479 return NULL;
1481 changed |= newnary->op[i] != nary->op[i];
1484 if (changed)
1486 pre_expr constant;
1487 unsigned int new_val_id;
1489 tree result = vn_nary_op_lookup_pieces (newnary->length,
1490 newnary->opcode,
1491 newnary->type,
1492 &newnary->op[0],
1493 &nary);
1494 if (result && is_gimple_min_invariant (result))
1495 return get_or_alloc_expr_for_constant (result);
1497 expr = (pre_expr) pool_alloc (pre_expr_pool);
1498 expr->kind = NARY;
1499 expr->id = 0;
1500 if (nary)
1502 PRE_EXPR_NARY (expr) = nary;
1503 constant = fully_constant_expression (expr);
1504 if (constant != expr)
1505 return constant;
1507 new_val_id = nary->value_id;
1508 get_or_alloc_expression_id (expr);
1510 else
1512 new_val_id = get_next_value_id ();
1513 value_expressions.safe_grow_cleared (get_max_value_id() + 1);
1514 nary = vn_nary_op_insert_pieces (newnary->length,
1515 newnary->opcode,
1516 newnary->type,
1517 &newnary->op[0],
1518 result, new_val_id);
1519 PRE_EXPR_NARY (expr) = nary;
1520 constant = fully_constant_expression (expr);
1521 if (constant != expr)
1522 return constant;
1523 get_or_alloc_expression_id (expr);
1525 add_to_value (new_val_id, expr);
1527 return expr;
1529 break;
1531 case REFERENCE:
1533 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1534 vec<vn_reference_op_s> operands = ref->operands;
1535 tree vuse = ref->vuse;
1536 tree newvuse = vuse;
1537 vec<vn_reference_op_s> newoperands = vNULL;
1538 bool changed = false, same_valid = true;
1539 unsigned int i, j, n;
1540 vn_reference_op_t operand;
1541 vn_reference_t newref;
1543 for (i = 0, j = 0;
1544 operands.iterate (i, &operand); i++, j++)
1546 pre_expr opresult;
1547 pre_expr leader;
1548 tree op[3];
1549 tree type = operand->type;
1550 vn_reference_op_s newop = *operand;
1551 op[0] = operand->op0;
1552 op[1] = operand->op1;
1553 op[2] = operand->op2;
1554 for (n = 0; n < 3; ++n)
1556 unsigned int op_val_id;
1557 if (!op[n])
1558 continue;
1559 if (TREE_CODE (op[n]) != SSA_NAME)
1561 /* We can't possibly insert these. */
1562 if (n != 0
1563 && !is_gimple_min_invariant (op[n]))
1564 break;
1565 continue;
1567 op_val_id = VN_INFO (op[n])->value_id;
1568 leader = find_leader_in_sets (op_val_id, set1, set2);
1569 if (!leader)
1570 break;
1571 /* Make sure we do not recursively translate ourselves
1572 like for translating a[n_1] with the leader for
1573 n_1 being a[n_1]. */
1574 if (get_expression_id (leader) != get_expression_id (expr))
1576 opresult = phi_translate (leader, set1, set2,
1577 pred, phiblock);
1578 if (!opresult)
1579 break;
1580 if (opresult != leader)
1582 tree name = get_representative_for (opresult);
1583 if (!name)
1584 break;
1585 changed |= name != op[n];
1586 op[n] = name;
1590 if (n != 3)
1592 newoperands.release ();
1593 return NULL;
1595 if (!newoperands.exists ())
1596 newoperands = operands.copy ();
1597 /* We may have changed from an SSA_NAME to a constant */
1598 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1599 newop.opcode = TREE_CODE (op[0]);
1600 newop.type = type;
1601 newop.op0 = op[0];
1602 newop.op1 = op[1];
1603 newop.op2 = op[2];
1604 /* If it transforms a non-constant ARRAY_REF into a constant
1605 one, adjust the constant offset. */
1606 if (newop.opcode == ARRAY_REF
1607 && newop.off == -1
1608 && TREE_CODE (op[0]) == INTEGER_CST
1609 && TREE_CODE (op[1]) == INTEGER_CST
1610 && TREE_CODE (op[2]) == INTEGER_CST)
1612 double_int off = tree_to_double_int (op[0]);
1613 off += -tree_to_double_int (op[1]);
1614 off *= tree_to_double_int (op[2]);
1615 if (off.fits_shwi ())
1616 newop.off = off.low;
1618 newoperands[j] = newop;
1619 /* If it transforms from an SSA_NAME to an address, fold with
1620 a preceding indirect reference. */
1621 if (j > 0 && op[0] && TREE_CODE (op[0]) == ADDR_EXPR
1622 && newoperands[j - 1].opcode == MEM_REF)
1623 vn_reference_fold_indirect (&newoperands, &j);
1625 if (i != operands.length ())
1627 newoperands.release ();
1628 return NULL;
1631 if (vuse)
1633 newvuse = translate_vuse_through_block (newoperands,
1634 ref->set, ref->type,
1635 vuse, phiblock, pred,
1636 &same_valid);
1637 if (newvuse == NULL_TREE)
1639 newoperands.release ();
1640 return NULL;
1644 if (changed || newvuse != vuse)
1646 unsigned int new_val_id;
1647 pre_expr constant;
1649 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1650 ref->type,
1651 newoperands,
1652 &newref, VN_WALK);
1653 if (result)
1654 newoperands.release ();
1656 /* We can always insert constants, so if we have a partial
1657 redundant constant load of another type try to translate it
1658 to a constant of appropriate type. */
1659 if (result && is_gimple_min_invariant (result))
1661 tree tem = result;
1662 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1664 tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1665 if (tem && !is_gimple_min_invariant (tem))
1666 tem = NULL_TREE;
1668 if (tem)
1669 return get_or_alloc_expr_for_constant (tem);
1672 /* If we'd have to convert things we would need to validate
1673 if we can insert the translated expression. So fail
1674 here for now - we cannot insert an alias with a different
1675 type in the VN tables either, as that would assert. */
1676 if (result
1677 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1678 return NULL;
1679 else if (!result && newref
1680 && !useless_type_conversion_p (ref->type, newref->type))
1682 newoperands.release ();
1683 return NULL;
1686 expr = (pre_expr) pool_alloc (pre_expr_pool);
1687 expr->kind = REFERENCE;
1688 expr->id = 0;
1690 if (newref)
1692 PRE_EXPR_REFERENCE (expr) = newref;
1693 constant = fully_constant_expression (expr);
1694 if (constant != expr)
1695 return constant;
1697 new_val_id = newref->value_id;
1698 get_or_alloc_expression_id (expr);
1700 else
1702 if (changed || !same_valid)
1704 new_val_id = get_next_value_id ();
1705 value_expressions.safe_grow_cleared(get_max_value_id() + 1);
1707 else
1708 new_val_id = ref->value_id;
1709 newref = vn_reference_insert_pieces (newvuse, ref->set,
1710 ref->type,
1711 newoperands,
1712 result, new_val_id);
1713 newoperands.create (0);
1714 PRE_EXPR_REFERENCE (expr) = newref;
1715 constant = fully_constant_expression (expr);
1716 if (constant != expr)
1717 return constant;
1718 get_or_alloc_expression_id (expr);
1720 add_to_value (new_val_id, expr);
1722 newoperands.release ();
1723 return expr;
1725 break;
1727 case NAME:
1729 tree name = PRE_EXPR_NAME (expr);
1730 gimple def_stmt = SSA_NAME_DEF_STMT (name);
1731 /* If the SSA name is defined by a PHI node in this block,
1732 translate it. */
1733 if (gimple_code (def_stmt) == GIMPLE_PHI
1734 && gimple_bb (def_stmt) == phiblock)
1736 edge e = find_edge (pred, gimple_bb (def_stmt));
1737 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1739 /* Handle constant. */
1740 if (is_gimple_min_invariant (def))
1741 return get_or_alloc_expr_for_constant (def);
1743 return get_or_alloc_expr_for_name (def);
1745 /* Otherwise return it unchanged - it will get cleaned if its
1746 value is not available in PREDs AVAIL_OUT set of expressions. */
1747 return expr;
1750 default:
1751 gcc_unreachable ();
1755 /* Wrapper around phi_translate_1 providing caching functionality. */
1757 static pre_expr
1758 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1759 basic_block pred, basic_block phiblock)
1761 pre_expr phitrans;
1763 if (!expr)
1764 return NULL;
1766 /* Constants contain no values that need translation. */
1767 if (expr->kind == CONSTANT)
1768 return expr;
1770 if (value_id_constant_p (get_expr_value_id (expr)))
1771 return expr;
1773 if (expr->kind != NAME)
1775 phitrans = phi_trans_lookup (expr, pred);
1776 if (phitrans)
1777 return phitrans;
1780 /* Translate. */
1781 phitrans = phi_translate_1 (expr, set1, set2, pred, phiblock);
1783 /* Don't add empty translations to the cache. Neither add
1784 translations of NAMEs as those are cheap to translate. */
1785 if (phitrans
1786 && expr->kind != NAME)
1787 phi_trans_add (expr, phitrans, pred);
1789 return phitrans;
1793 /* For each expression in SET, translate the values through phi nodes
1794 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1795 expressions in DEST. */
1797 static void
1798 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
1799 basic_block phiblock)
1801 vec<pre_expr> exprs;
1802 pre_expr expr;
1803 int i;
1805 if (gimple_seq_empty_p (phi_nodes (phiblock)))
1807 bitmap_set_copy (dest, set);
1808 return;
1811 exprs = sorted_array_from_bitmap_set (set);
1812 FOR_EACH_VEC_ELT (exprs, i, expr)
1814 pre_expr translated;
1815 translated = phi_translate (expr, set, NULL, pred, phiblock);
1816 if (!translated)
1817 continue;
1819 /* We might end up with multiple expressions from SET being
1820 translated to the same value. In this case we do not want
1821 to retain the NARY or REFERENCE expression but prefer a NAME
1822 which would be the leader. */
1823 if (translated->kind == NAME)
1824 bitmap_value_replace_in_set (dest, translated);
1825 else
1826 bitmap_value_insert_into_set (dest, translated);
1828 exprs.release ();
1831 /* Find the leader for a value (i.e., the name representing that
1832 value) in a given set, and return it. If STMT is non-NULL it
1833 makes sure the defining statement for the leader dominates it.
1834 Return NULL if no leader is found. */
1836 static pre_expr
1837 bitmap_find_leader (bitmap_set_t set, unsigned int val)
1839 if (value_id_constant_p (val))
1841 unsigned int i;
1842 bitmap_iterator bi;
1843 bitmap exprset = value_expressions[val];
1845 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1847 pre_expr expr = expression_for_id (i);
1848 if (expr->kind == CONSTANT)
1849 return expr;
1852 if (bitmap_set_contains_value (set, val))
1854 /* Rather than walk the entire bitmap of expressions, and see
1855 whether any of them has the value we are looking for, we look
1856 at the reverse mapping, which tells us the set of expressions
1857 that have a given value (IE value->expressions with that
1858 value) and see if any of those expressions are in our set.
1859 The number of expressions per value is usually significantly
1860 less than the number of expressions in the set. In fact, for
1861 large testcases, doing it this way is roughly 5-10x faster
1862 than walking the bitmap.
1863 If this is somehow a significant lose for some cases, we can
1864 choose which set to walk based on which set is smaller. */
1865 unsigned int i;
1866 bitmap_iterator bi;
1867 bitmap exprset = value_expressions[val];
1869 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1870 return expression_for_id (i);
1872 return NULL;
1875 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1876 BLOCK by seeing if it is not killed in the block. Note that we are
1877 only determining whether there is a store that kills it. Because
1878 of the order in which clean iterates over values, we are guaranteed
1879 that altered operands will have caused us to be eliminated from the
1880 ANTIC_IN set already. */
1882 static bool
1883 value_dies_in_block_x (pre_expr expr, basic_block block)
1885 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1886 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1887 gimple def;
1888 gimple_stmt_iterator gsi;
1889 unsigned id = get_expression_id (expr);
1890 bool res = false;
1891 ao_ref ref;
1893 if (!vuse)
1894 return false;
1896 /* Lookup a previously calculated result. */
1897 if (EXPR_DIES (block)
1898 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1899 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1901 /* A memory expression {e, VUSE} dies in the block if there is a
1902 statement that may clobber e. If, starting statement walk from the
1903 top of the basic block, a statement uses VUSE there can be no kill
1904 inbetween that use and the original statement that loaded {e, VUSE},
1905 so we can stop walking. */
1906 ref.base = NULL_TREE;
1907 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1909 tree def_vuse, def_vdef;
1910 def = gsi_stmt (gsi);
1911 def_vuse = gimple_vuse (def);
1912 def_vdef = gimple_vdef (def);
1914 /* Not a memory statement. */
1915 if (!def_vuse)
1916 continue;
1918 /* Not a may-def. */
1919 if (!def_vdef)
1921 /* A load with the same VUSE, we're done. */
1922 if (def_vuse == vuse)
1923 break;
1925 continue;
1928 /* Init ref only if we really need it. */
1929 if (ref.base == NULL_TREE
1930 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type,
1931 refx->operands))
1933 res = true;
1934 break;
1936 /* If the statement may clobber expr, it dies. */
1937 if (stmt_may_clobber_ref_p_1 (def, &ref))
1939 res = true;
1940 break;
1944 /* Remember the result. */
1945 if (!EXPR_DIES (block))
1946 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1947 bitmap_set_bit (EXPR_DIES (block), id * 2);
1948 if (res)
1949 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1951 return res;
1955 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1956 contains its value-id. */
1958 static bool
1959 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1961 if (op && TREE_CODE (op) == SSA_NAME)
1963 unsigned int value_id = VN_INFO (op)->value_id;
1964 if (!(bitmap_set_contains_value (set1, value_id)
1965 || (set2 && bitmap_set_contains_value (set2, value_id))))
1966 return false;
1968 return true;
1971 /* Determine if the expression EXPR is valid in SET1 U SET2.
1972 ONLY SET2 CAN BE NULL.
1973 This means that we have a leader for each part of the expression
1974 (if it consists of values), or the expression is an SSA_NAME.
1975 For loads/calls, we also see if the vuse is killed in this block. */
1977 static bool
1978 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr,
1979 basic_block block)
1981 switch (expr->kind)
1983 case NAME:
1984 return bitmap_set_contains_expr (AVAIL_OUT (block), expr);
1985 case NARY:
1987 unsigned int i;
1988 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1989 for (i = 0; i < nary->length; i++)
1990 if (!op_valid_in_sets (set1, set2, nary->op[i]))
1991 return false;
1992 return true;
1994 break;
1995 case REFERENCE:
1997 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1998 vn_reference_op_t vro;
1999 unsigned int i;
2001 FOR_EACH_VEC_ELT (ref->operands, i, vro)
2003 if (!op_valid_in_sets (set1, set2, vro->op0)
2004 || !op_valid_in_sets (set1, set2, vro->op1)
2005 || !op_valid_in_sets (set1, set2, vro->op2))
2006 return false;
2008 return true;
2010 default:
2011 gcc_unreachable ();
2015 /* Clean the set of expressions that are no longer valid in SET1 or
2016 SET2. This means expressions that are made up of values we have no
2017 leaders for in SET1 or SET2. This version is used for partial
2018 anticipation, which means it is not valid in either ANTIC_IN or
2019 PA_IN. */
2021 static void
2022 dependent_clean (bitmap_set_t set1, bitmap_set_t set2, basic_block block)
2024 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
2025 pre_expr expr;
2026 int i;
2028 FOR_EACH_VEC_ELT (exprs, i, expr)
2030 if (!valid_in_sets (set1, set2, expr, block))
2031 bitmap_remove_from_set (set1, expr);
2033 exprs.release ();
2036 /* Clean the set of expressions that are no longer valid in SET. This
2037 means expressions that are made up of values we have no leaders for
2038 in SET. */
2040 static void
2041 clean (bitmap_set_t set, basic_block block)
2043 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set);
2044 pre_expr expr;
2045 int i;
2047 FOR_EACH_VEC_ELT (exprs, i, expr)
2049 if (!valid_in_sets (set, NULL, expr, block))
2050 bitmap_remove_from_set (set, expr);
2052 exprs.release ();
2055 /* Clean the set of expressions that are no longer valid in SET because
2056 they are clobbered in BLOCK or because they trap and may not be executed. */
2058 static void
2059 prune_clobbered_mems (bitmap_set_t set, basic_block block)
2061 bitmap_iterator bi;
2062 unsigned i;
2064 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2066 pre_expr expr = expression_for_id (i);
2067 if (expr->kind == REFERENCE)
2069 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2070 if (ref->vuse)
2072 gimple def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2073 if (!gimple_nop_p (def_stmt)
2074 && ((gimple_bb (def_stmt) != block
2075 && !dominated_by_p (CDI_DOMINATORS,
2076 block, gimple_bb (def_stmt)))
2077 || (gimple_bb (def_stmt) == block
2078 && value_dies_in_block_x (expr, block))))
2079 bitmap_remove_from_set (set, expr);
2082 else if (expr->kind == NARY)
2084 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2085 /* If the NARY may trap make sure the block does not contain
2086 a possible exit point.
2087 ??? This is overly conservative if we translate AVAIL_OUT
2088 as the available expression might be after the exit point. */
2089 if (BB_MAY_NOTRETURN (block)
2090 && vn_nary_may_trap (nary))
2091 bitmap_remove_from_set (set, expr);
2096 static sbitmap has_abnormal_preds;
2098 /* List of blocks that may have changed during ANTIC computation and
2099 thus need to be iterated over. */
2101 static sbitmap changed_blocks;
2103 /* Decide whether to defer a block for a later iteration, or PHI
2104 translate SOURCE to DEST using phis in PHIBLOCK. Return false if we
2105 should defer the block, and true if we processed it. */
2107 static bool
2108 defer_or_phi_translate_block (bitmap_set_t dest, bitmap_set_t source,
2109 basic_block block, basic_block phiblock)
2111 if (!BB_VISITED (phiblock))
2113 bitmap_set_bit (changed_blocks, block->index);
2114 BB_VISITED (block) = 0;
2115 BB_DEFERRED (block) = 1;
2116 return false;
2118 else
2119 phi_translate_set (dest, source, block, phiblock);
2120 return true;
2123 /* Compute the ANTIC set for BLOCK.
2125 If succs(BLOCK) > 1 then
2126 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2127 else if succs(BLOCK) == 1 then
2128 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2130 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2133 static bool
2134 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2136 bool changed = false;
2137 bitmap_set_t S, old, ANTIC_OUT;
2138 bitmap_iterator bi;
2139 unsigned int bii;
2140 edge e;
2141 edge_iterator ei;
2143 old = ANTIC_OUT = S = NULL;
2144 BB_VISITED (block) = 1;
2146 /* If any edges from predecessors are abnormal, antic_in is empty,
2147 so do nothing. */
2148 if (block_has_abnormal_pred_edge)
2149 goto maybe_dump_sets;
2151 old = ANTIC_IN (block);
2152 ANTIC_OUT = bitmap_set_new ();
2154 /* If the block has no successors, ANTIC_OUT is empty. */
2155 if (EDGE_COUNT (block->succs) == 0)
2157 /* If we have one successor, we could have some phi nodes to
2158 translate through. */
2159 else if (single_succ_p (block))
2161 basic_block succ_bb = single_succ (block);
2163 /* We trade iterations of the dataflow equations for having to
2164 phi translate the maximal set, which is incredibly slow
2165 (since the maximal set often has 300+ members, even when you
2166 have a small number of blocks).
2167 Basically, we defer the computation of ANTIC for this block
2168 until we have processed it's successor, which will inevitably
2169 have a *much* smaller set of values to phi translate once
2170 clean has been run on it.
2171 The cost of doing this is that we technically perform more
2172 iterations, however, they are lower cost iterations.
2174 Timings for PRE on tramp3d-v4:
2175 without maximal set fix: 11 seconds
2176 with maximal set fix/without deferring: 26 seconds
2177 with maximal set fix/with deferring: 11 seconds
2180 if (!defer_or_phi_translate_block (ANTIC_OUT, ANTIC_IN (succ_bb),
2181 block, succ_bb))
2183 changed = true;
2184 goto maybe_dump_sets;
2187 /* If we have multiple successors, we take the intersection of all of
2188 them. Note that in the case of loop exit phi nodes, we may have
2189 phis to translate through. */
2190 else
2192 vec<basic_block> worklist;
2193 size_t i;
2194 basic_block bprime, first = NULL;
2196 worklist.create (EDGE_COUNT (block->succs));
2197 FOR_EACH_EDGE (e, ei, block->succs)
2199 if (!first
2200 && BB_VISITED (e->dest))
2201 first = e->dest;
2202 else if (BB_VISITED (e->dest))
2203 worklist.quick_push (e->dest);
2206 /* Of multiple successors we have to have visited one already. */
2207 if (!first)
2209 bitmap_set_bit (changed_blocks, block->index);
2210 BB_VISITED (block) = 0;
2211 BB_DEFERRED (block) = 1;
2212 changed = true;
2213 worklist.release ();
2214 goto maybe_dump_sets;
2217 if (!gimple_seq_empty_p (phi_nodes (first)))
2218 phi_translate_set (ANTIC_OUT, ANTIC_IN (first), block, first);
2219 else
2220 bitmap_set_copy (ANTIC_OUT, ANTIC_IN (first));
2222 FOR_EACH_VEC_ELT (worklist, i, bprime)
2224 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2226 bitmap_set_t tmp = bitmap_set_new ();
2227 phi_translate_set (tmp, ANTIC_IN (bprime), block, bprime);
2228 bitmap_set_and (ANTIC_OUT, tmp);
2229 bitmap_set_free (tmp);
2231 else
2232 bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime));
2234 worklist.release ();
2237 /* Prune expressions that are clobbered in block and thus become
2238 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2239 prune_clobbered_mems (ANTIC_OUT, block);
2241 /* Generate ANTIC_OUT - TMP_GEN. */
2242 S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block));
2244 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2245 ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block),
2246 TMP_GEN (block));
2248 /* Then union in the ANTIC_OUT - TMP_GEN values,
2249 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2250 FOR_EACH_EXPR_ID_IN_SET (S, bii, bi)
2251 bitmap_value_insert_into_set (ANTIC_IN (block),
2252 expression_for_id (bii));
2254 clean (ANTIC_IN (block), block);
2256 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2258 changed = true;
2259 bitmap_set_bit (changed_blocks, block->index);
2260 FOR_EACH_EDGE (e, ei, block->preds)
2261 bitmap_set_bit (changed_blocks, e->src->index);
2263 else
2264 bitmap_clear_bit (changed_blocks, block->index);
2266 maybe_dump_sets:
2267 if (dump_file && (dump_flags & TDF_DETAILS))
2269 if (!BB_DEFERRED (block) || BB_VISITED (block))
2271 if (ANTIC_OUT)
2272 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2274 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2275 block->index);
2277 if (S)
2278 print_bitmap_set (dump_file, S, "S", block->index);
2280 else
2282 fprintf (dump_file,
2283 "Block %d was deferred for a future iteration.\n",
2284 block->index);
2287 if (old)
2288 bitmap_set_free (old);
2289 if (S)
2290 bitmap_set_free (S);
2291 if (ANTIC_OUT)
2292 bitmap_set_free (ANTIC_OUT);
2293 return changed;
2296 /* Compute PARTIAL_ANTIC for BLOCK.
2298 If succs(BLOCK) > 1 then
2299 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2300 in ANTIC_OUT for all succ(BLOCK)
2301 else if succs(BLOCK) == 1 then
2302 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2304 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2305 - ANTIC_IN[BLOCK])
2308 static bool
2309 compute_partial_antic_aux (basic_block block,
2310 bool block_has_abnormal_pred_edge)
2312 bool changed = false;
2313 bitmap_set_t old_PA_IN;
2314 bitmap_set_t PA_OUT;
2315 edge e;
2316 edge_iterator ei;
2317 unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH);
2319 old_PA_IN = PA_OUT = NULL;
2321 /* If any edges from predecessors are abnormal, antic_in is empty,
2322 so do nothing. */
2323 if (block_has_abnormal_pred_edge)
2324 goto maybe_dump_sets;
2326 /* If there are too many partially anticipatable values in the
2327 block, phi_translate_set can take an exponential time: stop
2328 before the translation starts. */
2329 if (max_pa
2330 && single_succ_p (block)
2331 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2332 goto maybe_dump_sets;
2334 old_PA_IN = PA_IN (block);
2335 PA_OUT = bitmap_set_new ();
2337 /* If the block has no successors, ANTIC_OUT is empty. */
2338 if (EDGE_COUNT (block->succs) == 0)
2340 /* If we have one successor, we could have some phi nodes to
2341 translate through. Note that we can't phi translate across DFS
2342 back edges in partial antic, because it uses a union operation on
2343 the successors. For recurrences like IV's, we will end up
2344 generating a new value in the set on each go around (i + 3 (VH.1)
2345 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2346 else if (single_succ_p (block))
2348 basic_block succ = single_succ (block);
2349 if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK))
2350 phi_translate_set (PA_OUT, PA_IN (succ), block, succ);
2352 /* If we have multiple successors, we take the union of all of
2353 them. */
2354 else
2356 vec<basic_block> worklist;
2357 size_t i;
2358 basic_block bprime;
2360 worklist.create (EDGE_COUNT (block->succs));
2361 FOR_EACH_EDGE (e, ei, block->succs)
2363 if (e->flags & EDGE_DFS_BACK)
2364 continue;
2365 worklist.quick_push (e->dest);
2367 if (worklist.length () > 0)
2369 FOR_EACH_VEC_ELT (worklist, i, bprime)
2371 unsigned int i;
2372 bitmap_iterator bi;
2374 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi)
2375 bitmap_value_insert_into_set (PA_OUT,
2376 expression_for_id (i));
2377 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2379 bitmap_set_t pa_in = bitmap_set_new ();
2380 phi_translate_set (pa_in, PA_IN (bprime), block, bprime);
2381 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2382 bitmap_value_insert_into_set (PA_OUT,
2383 expression_for_id (i));
2384 bitmap_set_free (pa_in);
2386 else
2387 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi)
2388 bitmap_value_insert_into_set (PA_OUT,
2389 expression_for_id (i));
2392 worklist.release ();
2395 /* Prune expressions that are clobbered in block and thus become
2396 invalid if translated from PA_OUT to PA_IN. */
2397 prune_clobbered_mems (PA_OUT, block);
2399 /* PA_IN starts with PA_OUT - TMP_GEN.
2400 Then we subtract things from ANTIC_IN. */
2401 PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block));
2403 /* For partial antic, we want to put back in the phi results, since
2404 we will properly avoid making them partially antic over backedges. */
2405 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2406 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2408 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2409 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2411 dependent_clean (PA_IN (block), ANTIC_IN (block), block);
2413 if (!bitmap_set_equal (old_PA_IN, PA_IN (block)))
2415 changed = true;
2416 bitmap_set_bit (changed_blocks, block->index);
2417 FOR_EACH_EDGE (e, ei, block->preds)
2418 bitmap_set_bit (changed_blocks, e->src->index);
2420 else
2421 bitmap_clear_bit (changed_blocks, block->index);
2423 maybe_dump_sets:
2424 if (dump_file && (dump_flags & TDF_DETAILS))
2426 if (PA_OUT)
2427 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2429 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2431 if (old_PA_IN)
2432 bitmap_set_free (old_PA_IN);
2433 if (PA_OUT)
2434 bitmap_set_free (PA_OUT);
2435 return changed;
2438 /* Compute ANTIC and partial ANTIC sets. */
2440 static void
2441 compute_antic (void)
2443 bool changed = true;
2444 int num_iterations = 0;
2445 basic_block block;
2446 int i;
2448 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2449 We pre-build the map of blocks with incoming abnormal edges here. */
2450 has_abnormal_preds = sbitmap_alloc (last_basic_block);
2451 bitmap_clear (has_abnormal_preds);
2453 FOR_ALL_BB (block)
2455 edge_iterator ei;
2456 edge e;
2458 FOR_EACH_EDGE (e, ei, block->preds)
2460 e->flags &= ~EDGE_DFS_BACK;
2461 if (e->flags & EDGE_ABNORMAL)
2463 bitmap_set_bit (has_abnormal_preds, block->index);
2464 break;
2468 BB_VISITED (block) = 0;
2469 BB_DEFERRED (block) = 0;
2471 /* While we are here, give empty ANTIC_IN sets to each block. */
2472 ANTIC_IN (block) = bitmap_set_new ();
2473 PA_IN (block) = bitmap_set_new ();
2476 /* At the exit block we anticipate nothing. */
2477 BB_VISITED (EXIT_BLOCK_PTR) = 1;
2479 changed_blocks = sbitmap_alloc (last_basic_block + 1);
2480 bitmap_ones (changed_blocks);
2481 while (changed)
2483 if (dump_file && (dump_flags & TDF_DETAILS))
2484 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2485 /* ??? We need to clear our PHI translation cache here as the
2486 ANTIC sets shrink and we restrict valid translations to
2487 those having operands with leaders in ANTIC. Same below
2488 for PA ANTIC computation. */
2489 num_iterations++;
2490 changed = false;
2491 for (i = postorder_num - 1; i >= 0; i--)
2493 if (bitmap_bit_p (changed_blocks, postorder[i]))
2495 basic_block block = BASIC_BLOCK (postorder[i]);
2496 changed |= compute_antic_aux (block,
2497 bitmap_bit_p (has_abnormal_preds,
2498 block->index));
2501 /* Theoretically possible, but *highly* unlikely. */
2502 gcc_checking_assert (num_iterations < 500);
2505 statistics_histogram_event (cfun, "compute_antic iterations",
2506 num_iterations);
2508 if (do_partial_partial)
2510 bitmap_ones (changed_blocks);
2511 mark_dfs_back_edges ();
2512 num_iterations = 0;
2513 changed = true;
2514 while (changed)
2516 if (dump_file && (dump_flags & TDF_DETAILS))
2517 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2518 num_iterations++;
2519 changed = false;
2520 for (i = postorder_num - 1 ; i >= 0; i--)
2522 if (bitmap_bit_p (changed_blocks, postorder[i]))
2524 basic_block block = BASIC_BLOCK (postorder[i]);
2525 changed
2526 |= compute_partial_antic_aux (block,
2527 bitmap_bit_p (has_abnormal_preds,
2528 block->index));
2531 /* Theoretically possible, but *highly* unlikely. */
2532 gcc_checking_assert (num_iterations < 500);
2534 statistics_histogram_event (cfun, "compute_partial_antic iterations",
2535 num_iterations);
2537 sbitmap_free (has_abnormal_preds);
2538 sbitmap_free (changed_blocks);
2542 /* Inserted expressions are placed onto this worklist, which is used
2543 for performing quick dead code elimination of insertions we made
2544 that didn't turn out to be necessary. */
2545 static bitmap inserted_exprs;
2547 /* The actual worker for create_component_ref_by_pieces. */
2549 static tree
2550 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2551 unsigned int *operand, gimple_seq *stmts)
2553 vn_reference_op_t currop = &ref->operands[*operand];
2554 tree genop;
2555 ++*operand;
2556 switch (currop->opcode)
2558 case CALL_EXPR:
2560 tree folded, sc = NULL_TREE;
2561 unsigned int nargs = 0;
2562 tree fn, *args;
2563 if (TREE_CODE (currop->op0) == FUNCTION_DECL)
2564 fn = currop->op0;
2565 else
2566 fn = find_or_generate_expression (block, currop->op0, stmts);
2567 if (currop->op1)
2568 sc = find_or_generate_expression (block, currop->op1, stmts);
2569 args = XNEWVEC (tree, ref->operands.length () - 1);
2570 while (*operand < ref->operands.length ())
2572 args[nargs] = create_component_ref_by_pieces_1 (block, ref,
2573 operand, stmts);
2574 nargs++;
2576 folded = build_call_array (currop->type,
2577 (TREE_CODE (fn) == FUNCTION_DECL
2578 ? build_fold_addr_expr (fn) : fn),
2579 nargs, args);
2580 free (args);
2581 if (sc)
2582 CALL_EXPR_STATIC_CHAIN (folded) = sc;
2583 return folded;
2586 case MEM_REF:
2588 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2589 stmts);
2590 tree offset = currop->op0;
2591 if (TREE_CODE (baseop) == ADDR_EXPR
2592 && handled_component_p (TREE_OPERAND (baseop, 0)))
2594 HOST_WIDE_INT off;
2595 tree base;
2596 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2597 &off);
2598 gcc_assert (base);
2599 offset = int_const_binop (PLUS_EXPR, offset,
2600 build_int_cst (TREE_TYPE (offset),
2601 off));
2602 baseop = build_fold_addr_expr (base);
2604 return fold_build2 (MEM_REF, currop->type, baseop, offset);
2607 case TARGET_MEM_REF:
2609 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2610 vn_reference_op_t nextop = &ref->operands[++*operand];
2611 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2612 stmts);
2613 if (currop->op0)
2614 genop0 = find_or_generate_expression (block, currop->op0, stmts);
2615 if (nextop->op0)
2616 genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2617 return build5 (TARGET_MEM_REF, currop->type,
2618 baseop, currop->op2, genop0, currop->op1, genop1);
2621 case ADDR_EXPR:
2622 if (currop->op0)
2624 gcc_assert (is_gimple_min_invariant (currop->op0));
2625 return currop->op0;
2627 /* Fallthrough. */
2628 case REALPART_EXPR:
2629 case IMAGPART_EXPR:
2630 case VIEW_CONVERT_EXPR:
2632 tree genop0 = create_component_ref_by_pieces_1 (block, ref,
2633 operand, stmts);
2634 return fold_build1 (currop->opcode, currop->type, genop0);
2637 case WITH_SIZE_EXPR:
2639 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2640 stmts);
2641 tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2642 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2645 case BIT_FIELD_REF:
2647 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2648 stmts);
2649 tree op1 = currop->op0;
2650 tree op2 = currop->op1;
2651 return fold_build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2654 /* For array ref vn_reference_op's, operand 1 of the array ref
2655 is op0 of the reference op and operand 3 of the array ref is
2656 op1. */
2657 case ARRAY_RANGE_REF:
2658 case ARRAY_REF:
2660 tree genop0;
2661 tree genop1 = currop->op0;
2662 tree genop2 = currop->op1;
2663 tree genop3 = currop->op2;
2664 genop0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2665 genop1 = find_or_generate_expression (block, genop1, stmts);
2666 if (genop2)
2668 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2669 /* Drop zero minimum index if redundant. */
2670 if (integer_zerop (genop2)
2671 && (!domain_type
2672 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2673 genop2 = NULL_TREE;
2674 else
2675 genop2 = find_or_generate_expression (block, genop2, stmts);
2677 if (genop3)
2679 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2680 /* We can't always put a size in units of the element alignment
2681 here as the element alignment may be not visible. See
2682 PR43783. Simply drop the element size for constant
2683 sizes. */
2684 if (tree_int_cst_equal (genop3, TYPE_SIZE_UNIT (elmt_type)))
2685 genop3 = NULL_TREE;
2686 else
2688 genop3 = size_binop (EXACT_DIV_EXPR, genop3,
2689 size_int (TYPE_ALIGN_UNIT (elmt_type)));
2690 genop3 = find_or_generate_expression (block, genop3, stmts);
2693 return build4 (currop->opcode, currop->type, genop0, genop1,
2694 genop2, genop3);
2696 case COMPONENT_REF:
2698 tree op0;
2699 tree op1;
2700 tree genop2 = currop->op1;
2701 op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2702 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2703 op1 = currop->op0;
2704 if (genop2)
2705 genop2 = find_or_generate_expression (block, genop2, stmts);
2706 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2709 case SSA_NAME:
2711 genop = find_or_generate_expression (block, currop->op0, stmts);
2712 return genop;
2714 case STRING_CST:
2715 case INTEGER_CST:
2716 case COMPLEX_CST:
2717 case VECTOR_CST:
2718 case REAL_CST:
2719 case CONSTRUCTOR:
2720 case VAR_DECL:
2721 case PARM_DECL:
2722 case CONST_DECL:
2723 case RESULT_DECL:
2724 case FUNCTION_DECL:
2725 return currop->op0;
2727 default:
2728 gcc_unreachable ();
2732 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2733 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2734 trying to rename aggregates into ssa form directly, which is a no no.
2736 Thus, this routine doesn't create temporaries, it just builds a
2737 single access expression for the array, calling
2738 find_or_generate_expression to build the innermost pieces.
2740 This function is a subroutine of create_expression_by_pieces, and
2741 should not be called on it's own unless you really know what you
2742 are doing. */
2744 static tree
2745 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2746 gimple_seq *stmts)
2748 unsigned int op = 0;
2749 return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2752 /* Find a leader for an expression, or generate one using
2753 create_expression_by_pieces if it's ANTIC but
2754 complex.
2755 BLOCK is the basic_block we are looking for leaders in.
2756 OP is the tree expression to find a leader for or generate.
2757 STMTS is the statement list to put the inserted expressions on.
2758 Returns the SSA_NAME of the LHS of the generated expression or the
2759 leader.
2760 DOMSTMT if non-NULL is a statement that should be dominated by
2761 all uses in the generated expression. If DOMSTMT is non-NULL this
2762 routine can fail and return NULL_TREE. Otherwise it will assert
2763 on failure. */
2765 static tree
2766 find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2768 pre_expr expr = get_or_alloc_expr_for (op);
2769 unsigned int lookfor = get_expr_value_id (expr);
2770 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2771 if (leader)
2773 if (leader->kind == NAME)
2774 return PRE_EXPR_NAME (leader);
2775 else if (leader->kind == CONSTANT)
2776 return PRE_EXPR_CONSTANT (leader);
2779 /* It must be a complex expression, so generate it recursively. */
2780 bitmap exprset = value_expressions[lookfor];
2781 bitmap_iterator bi;
2782 unsigned int i;
2783 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2785 pre_expr temp = expression_for_id (i);
2786 if (temp->kind != NAME)
2787 return create_expression_by_pieces (block, temp, stmts,
2788 get_expr_type (expr));
2791 gcc_unreachable ();
2794 #define NECESSARY GF_PLF_1
2796 /* Create an expression in pieces, so that we can handle very complex
2797 expressions that may be ANTIC, but not necessary GIMPLE.
2798 BLOCK is the basic block the expression will be inserted into,
2799 EXPR is the expression to insert (in value form)
2800 STMTS is a statement list to append the necessary insertions into.
2802 This function will die if we hit some value that shouldn't be
2803 ANTIC but is (IE there is no leader for it, or its components).
2804 This function may also generate expressions that are themselves
2805 partially or fully redundant. Those that are will be either made
2806 fully redundant during the next iteration of insert (for partially
2807 redundant ones), or eliminated by eliminate (for fully redundant
2808 ones).
2810 If DOMSTMT is non-NULL then we make sure that all uses in the
2811 expressions dominate that statement. In this case the function
2812 can return NULL_TREE to signal failure. */
2814 static tree
2815 create_expression_by_pieces (basic_block block, pre_expr expr,
2816 gimple_seq *stmts, tree type)
2818 tree name;
2819 tree folded;
2820 gimple_seq forced_stmts = NULL;
2821 unsigned int value_id;
2822 gimple_stmt_iterator gsi;
2823 tree exprtype = type ? type : get_expr_type (expr);
2824 pre_expr nameexpr;
2825 gimple newstmt;
2827 switch (expr->kind)
2829 /* We may hit the NAME/CONSTANT case if we have to convert types
2830 that value numbering saw through. */
2831 case NAME:
2832 folded = PRE_EXPR_NAME (expr);
2833 break;
2834 case CONSTANT:
2835 folded = PRE_EXPR_CONSTANT (expr);
2836 break;
2837 case REFERENCE:
2839 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2840 folded = create_component_ref_by_pieces (block, ref, stmts);
2842 break;
2843 case NARY:
2845 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2846 tree *genop = XALLOCAVEC (tree, nary->length);
2847 unsigned i;
2848 for (i = 0; i < nary->length; ++i)
2850 genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2851 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2852 may have conversions stripped. */
2853 if (nary->opcode == POINTER_PLUS_EXPR)
2855 if (i == 0)
2856 genop[i] = fold_convert (nary->type, genop[i]);
2857 else if (i == 1)
2858 genop[i] = convert_to_ptrofftype (genop[i]);
2860 else
2861 genop[i] = fold_convert (TREE_TYPE (nary->op[i]), genop[i]);
2863 if (nary->opcode == CONSTRUCTOR)
2865 vec<constructor_elt, va_gc> *elts = NULL;
2866 for (i = 0; i < nary->length; ++i)
2867 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2868 folded = build_constructor (nary->type, elts);
2870 else
2872 switch (nary->length)
2874 case 1:
2875 folded = fold_build1 (nary->opcode, nary->type,
2876 genop[0]);
2877 break;
2878 case 2:
2879 folded = fold_build2 (nary->opcode, nary->type,
2880 genop[0], genop[1]);
2881 break;
2882 case 3:
2883 folded = fold_build3 (nary->opcode, nary->type,
2884 genop[0], genop[1], genop[3]);
2885 break;
2886 default:
2887 gcc_unreachable ();
2891 break;
2892 default:
2893 gcc_unreachable ();
2896 if (!useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2897 folded = fold_convert (exprtype, folded);
2899 /* Force the generated expression to be a sequence of GIMPLE
2900 statements.
2901 We have to call unshare_expr because force_gimple_operand may
2902 modify the tree we pass to it. */
2903 folded = force_gimple_operand (unshare_expr (folded), &forced_stmts,
2904 false, NULL);
2906 /* If we have any intermediate expressions to the value sets, add them
2907 to the value sets and chain them in the instruction stream. */
2908 if (forced_stmts)
2910 gsi = gsi_start (forced_stmts);
2911 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2913 gimple stmt = gsi_stmt (gsi);
2914 tree forcedname = gimple_get_lhs (stmt);
2915 pre_expr nameexpr;
2917 if (TREE_CODE (forcedname) == SSA_NAME)
2919 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
2920 VN_INFO_GET (forcedname)->valnum = forcedname;
2921 VN_INFO (forcedname)->value_id = get_next_value_id ();
2922 nameexpr = get_or_alloc_expr_for_name (forcedname);
2923 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
2924 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2925 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2928 gimple_seq_add_seq (stmts, forced_stmts);
2931 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2932 newstmt = gimple_build_assign (name, folded);
2933 gimple_set_plf (newstmt, NECESSARY, false);
2935 gimple_seq_add_stmt (stmts, newstmt);
2936 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (name));
2938 /* Fold the last statement. */
2939 gsi = gsi_last (*stmts);
2940 if (fold_stmt_inplace (&gsi))
2941 update_stmt (gsi_stmt (gsi));
2943 /* Add a value number to the temporary.
2944 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2945 we are creating the expression by pieces, and this particular piece of
2946 the expression may have been represented. There is no harm in replacing
2947 here. */
2948 value_id = get_expr_value_id (expr);
2949 VN_INFO_GET (name)->value_id = value_id;
2950 VN_INFO (name)->valnum = sccvn_valnum_from_value_id (value_id);
2951 if (VN_INFO (name)->valnum == NULL_TREE)
2952 VN_INFO (name)->valnum = name;
2953 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
2954 nameexpr = get_or_alloc_expr_for_name (name);
2955 add_to_value (value_id, nameexpr);
2956 if (NEW_SETS (block))
2957 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2958 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2960 pre_stats.insertions++;
2961 if (dump_file && (dump_flags & TDF_DETAILS))
2963 fprintf (dump_file, "Inserted ");
2964 print_gimple_stmt (dump_file, newstmt, 0, 0);
2965 fprintf (dump_file, " in predecessor %d\n", block->index);
2968 return name;
2972 /* Returns true if we want to inhibit the insertions of PHI nodes
2973 for the given EXPR for basic block BB (a member of a loop).
2974 We want to do this, when we fear that the induction variable we
2975 create might inhibit vectorization. */
2977 static bool
2978 inhibit_phi_insertion (basic_block bb, pre_expr expr)
2980 vn_reference_t vr = PRE_EXPR_REFERENCE (expr);
2981 vec<vn_reference_op_s> ops = vr->operands;
2982 vn_reference_op_t op;
2983 unsigned i;
2985 /* If we aren't going to vectorize we don't inhibit anything. */
2986 if (!flag_tree_vectorize)
2987 return false;
2989 /* Otherwise we inhibit the insertion when the address of the
2990 memory reference is a simple induction variable. In other
2991 cases the vectorizer won't do anything anyway (either it's
2992 loop invariant or a complicated expression). */
2993 FOR_EACH_VEC_ELT (ops, i, op)
2995 switch (op->opcode)
2997 case CALL_EXPR:
2998 /* Calls are not a problem. */
2999 return false;
3001 case ARRAY_REF:
3002 case ARRAY_RANGE_REF:
3003 if (TREE_CODE (op->op0) != SSA_NAME)
3004 break;
3005 /* Fallthru. */
3006 case SSA_NAME:
3008 basic_block defbb = gimple_bb (SSA_NAME_DEF_STMT (op->op0));
3009 affine_iv iv;
3010 /* Default defs are loop invariant. */
3011 if (!defbb)
3012 break;
3013 /* Defined outside this loop, also loop invariant. */
3014 if (!flow_bb_inside_loop_p (bb->loop_father, defbb))
3015 break;
3016 /* If it's a simple induction variable inhibit insertion,
3017 the vectorizer might be interested in this one. */
3018 if (simple_iv (bb->loop_father, bb->loop_father,
3019 op->op0, &iv, true))
3020 return true;
3021 /* No simple IV, vectorizer can't do anything, hence no
3022 reason to inhibit the transformation for this operand. */
3023 break;
3025 default:
3026 break;
3029 return false;
3032 /* Insert the to-be-made-available values of expression EXPRNUM for each
3033 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3034 merge the result with a phi node, given the same value number as
3035 NODE. Return true if we have inserted new stuff. */
3037 static bool
3038 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3039 vec<pre_expr> avail)
3041 pre_expr expr = expression_for_id (exprnum);
3042 pre_expr newphi;
3043 unsigned int val = get_expr_value_id (expr);
3044 edge pred;
3045 bool insertions = false;
3046 bool nophi = false;
3047 basic_block bprime;
3048 pre_expr eprime;
3049 edge_iterator ei;
3050 tree type = get_expr_type (expr);
3051 tree temp;
3052 gimple phi;
3054 /* Make sure we aren't creating an induction variable. */
3055 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3057 bool firstinsideloop = false;
3058 bool secondinsideloop = false;
3059 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3060 EDGE_PRED (block, 0)->src);
3061 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3062 EDGE_PRED (block, 1)->src);
3063 /* Induction variables only have one edge inside the loop. */
3064 if ((firstinsideloop ^ secondinsideloop)
3065 && (expr->kind != REFERENCE
3066 || inhibit_phi_insertion (block, expr)))
3068 if (dump_file && (dump_flags & TDF_DETAILS))
3069 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3070 nophi = true;
3074 /* Make the necessary insertions. */
3075 FOR_EACH_EDGE (pred, ei, block->preds)
3077 gimple_seq stmts = NULL;
3078 tree builtexpr;
3079 bprime = pred->src;
3080 eprime = avail[pred->dest_idx];
3082 if (eprime->kind != NAME && eprime->kind != CONSTANT)
3084 builtexpr = create_expression_by_pieces (bprime, eprime,
3085 &stmts, type);
3086 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3087 gsi_insert_seq_on_edge (pred, stmts);
3088 avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3089 insertions = true;
3091 else if (eprime->kind == CONSTANT)
3093 /* Constants may not have the right type, fold_convert
3094 should give us back a constant with the right type. */
3095 tree constant = PRE_EXPR_CONSTANT (eprime);
3096 if (!useless_type_conversion_p (type, TREE_TYPE (constant)))
3098 tree builtexpr = fold_convert (type, constant);
3099 if (!is_gimple_min_invariant (builtexpr))
3101 tree forcedexpr = force_gimple_operand (builtexpr,
3102 &stmts, true,
3103 NULL);
3104 if (!is_gimple_min_invariant (forcedexpr))
3106 if (forcedexpr != builtexpr)
3108 VN_INFO_GET (forcedexpr)->valnum = PRE_EXPR_CONSTANT (eprime);
3109 VN_INFO (forcedexpr)->value_id = get_expr_value_id (eprime);
3111 if (stmts)
3113 gimple_stmt_iterator gsi;
3114 gsi = gsi_start (stmts);
3115 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3117 gimple stmt = gsi_stmt (gsi);
3118 tree lhs = gimple_get_lhs (stmt);
3119 if (TREE_CODE (lhs) == SSA_NAME)
3120 bitmap_set_bit (inserted_exprs,
3121 SSA_NAME_VERSION (lhs));
3122 gimple_set_plf (stmt, NECESSARY, false);
3124 gsi_insert_seq_on_edge (pred, stmts);
3126 avail[pred->dest_idx]
3127 = get_or_alloc_expr_for_name (forcedexpr);
3130 else
3131 avail[pred->dest_idx]
3132 = get_or_alloc_expr_for_constant (builtexpr);
3135 else if (eprime->kind == NAME)
3137 /* We may have to do a conversion because our value
3138 numbering can look through types in certain cases, but
3139 our IL requires all operands of a phi node have the same
3140 type. */
3141 tree name = PRE_EXPR_NAME (eprime);
3142 if (!useless_type_conversion_p (type, TREE_TYPE (name)))
3144 tree builtexpr;
3145 tree forcedexpr;
3146 builtexpr = fold_convert (type, name);
3147 forcedexpr = force_gimple_operand (builtexpr,
3148 &stmts, true,
3149 NULL);
3151 if (forcedexpr != name)
3153 VN_INFO_GET (forcedexpr)->valnum = VN_INFO (name)->valnum;
3154 VN_INFO (forcedexpr)->value_id = VN_INFO (name)->value_id;
3157 if (stmts)
3159 gimple_stmt_iterator gsi;
3160 gsi = gsi_start (stmts);
3161 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3163 gimple stmt = gsi_stmt (gsi);
3164 tree lhs = gimple_get_lhs (stmt);
3165 if (TREE_CODE (lhs) == SSA_NAME)
3166 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
3167 gimple_set_plf (stmt, NECESSARY, false);
3169 gsi_insert_seq_on_edge (pred, stmts);
3171 avail[pred->dest_idx] = get_or_alloc_expr_for_name (forcedexpr);
3175 /* If we didn't want a phi node, and we made insertions, we still have
3176 inserted new stuff, and thus return true. If we didn't want a phi node,
3177 and didn't make insertions, we haven't added anything new, so return
3178 false. */
3179 if (nophi && insertions)
3180 return true;
3181 else if (nophi && !insertions)
3182 return false;
3184 /* Now build a phi for the new variable. */
3185 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3186 phi = create_phi_node (temp, block);
3188 gimple_set_plf (phi, NECESSARY, false);
3189 VN_INFO_GET (temp)->value_id = val;
3190 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3191 if (VN_INFO (temp)->valnum == NULL_TREE)
3192 VN_INFO (temp)->valnum = temp;
3193 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3194 FOR_EACH_EDGE (pred, ei, block->preds)
3196 pre_expr ae = avail[pred->dest_idx];
3197 gcc_assert (get_expr_type (ae) == type
3198 || useless_type_conversion_p (type, get_expr_type (ae)));
3199 if (ae->kind == CONSTANT)
3200 add_phi_arg (phi, PRE_EXPR_CONSTANT (ae), pred, UNKNOWN_LOCATION);
3201 else
3202 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3205 newphi = get_or_alloc_expr_for_name (temp);
3206 add_to_value (val, newphi);
3208 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3209 this insertion, since we test for the existence of this value in PHI_GEN
3210 before proceeding with the partial redundancy checks in insert_aux.
3212 The value may exist in AVAIL_OUT, in particular, it could be represented
3213 by the expression we are trying to eliminate, in which case we want the
3214 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3215 inserted there.
3217 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3218 this block, because if it did, it would have existed in our dominator's
3219 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3222 bitmap_insert_into_set (PHI_GEN (block), newphi);
3223 bitmap_value_replace_in_set (AVAIL_OUT (block),
3224 newphi);
3225 bitmap_insert_into_set (NEW_SETS (block),
3226 newphi);
3228 if (dump_file && (dump_flags & TDF_DETAILS))
3230 fprintf (dump_file, "Created phi ");
3231 print_gimple_stmt (dump_file, phi, 0, 0);
3232 fprintf (dump_file, " in block %d\n", block->index);
3234 pre_stats.phis++;
3235 return true;
3240 /* Perform insertion of partially redundant values.
3241 For BLOCK, do the following:
3242 1. Propagate the NEW_SETS of the dominator into the current block.
3243 If the block has multiple predecessors,
3244 2a. Iterate over the ANTIC expressions for the block to see if
3245 any of them are partially redundant.
3246 2b. If so, insert them into the necessary predecessors to make
3247 the expression fully redundant.
3248 2c. Insert a new PHI merging the values of the predecessors.
3249 2d. Insert the new PHI, and the new expressions, into the
3250 NEW_SETS set.
3251 3. Recursively call ourselves on the dominator children of BLOCK.
3253 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
3254 do_regular_insertion and do_partial_insertion.
3258 static bool
3259 do_regular_insertion (basic_block block, basic_block dom)
3261 bool new_stuff = false;
3262 vec<pre_expr> exprs;
3263 pre_expr expr;
3264 vec<pre_expr> avail = vNULL;
3265 int i;
3267 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3268 avail.safe_grow (EDGE_COUNT (block->preds));
3270 FOR_EACH_VEC_ELT (exprs, i, expr)
3272 if (expr->kind != NAME)
3274 unsigned int val;
3275 bool by_some = false;
3276 bool cant_insert = false;
3277 bool all_same = true;
3278 pre_expr first_s = NULL;
3279 edge pred;
3280 basic_block bprime;
3281 pre_expr eprime = NULL;
3282 edge_iterator ei;
3283 pre_expr edoubleprime = NULL;
3284 bool do_insertion = false;
3286 val = get_expr_value_id (expr);
3287 if (bitmap_set_contains_value (PHI_GEN (block), val))
3288 continue;
3289 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3291 if (dump_file && (dump_flags & TDF_DETAILS))
3292 fprintf (dump_file, "Found fully redundant value\n");
3293 continue;
3296 FOR_EACH_EDGE (pred, ei, block->preds)
3298 unsigned int vprime;
3300 /* We should never run insertion for the exit block
3301 and so not come across fake pred edges. */
3302 gcc_assert (!(pred->flags & EDGE_FAKE));
3303 bprime = pred->src;
3304 eprime = phi_translate (expr, ANTIC_IN (block), NULL,
3305 bprime, block);
3307 /* eprime will generally only be NULL if the
3308 value of the expression, translated
3309 through the PHI for this predecessor, is
3310 undefined. If that is the case, we can't
3311 make the expression fully redundant,
3312 because its value is undefined along a
3313 predecessor path. We can thus break out
3314 early because it doesn't matter what the
3315 rest of the results are. */
3316 if (eprime == NULL)
3318 avail[pred->dest_idx] = NULL;
3319 cant_insert = true;
3320 break;
3323 eprime = fully_constant_expression (eprime);
3324 vprime = get_expr_value_id (eprime);
3325 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3326 vprime);
3327 if (edoubleprime == NULL)
3329 avail[pred->dest_idx] = eprime;
3330 all_same = false;
3332 else
3334 avail[pred->dest_idx] = edoubleprime;
3335 by_some = true;
3336 /* We want to perform insertions to remove a redundancy on
3337 a path in the CFG we want to optimize for speed. */
3338 if (optimize_edge_for_speed_p (pred))
3339 do_insertion = true;
3340 if (first_s == NULL)
3341 first_s = edoubleprime;
3342 else if (!pre_expr_d::equal (first_s, edoubleprime))
3343 all_same = false;
3346 /* If we can insert it, it's not the same value
3347 already existing along every predecessor, and
3348 it's defined by some predecessor, it is
3349 partially redundant. */
3350 if (!cant_insert && !all_same && by_some)
3352 if (!do_insertion)
3354 if (dump_file && (dump_flags & TDF_DETAILS))
3356 fprintf (dump_file, "Skipping partial redundancy for "
3357 "expression ");
3358 print_pre_expr (dump_file, expr);
3359 fprintf (dump_file, " (%04d), no redundancy on to be "
3360 "optimized for speed edge\n", val);
3363 else if (dbg_cnt (treepre_insert))
3365 if (dump_file && (dump_flags & TDF_DETAILS))
3367 fprintf (dump_file, "Found partial redundancy for "
3368 "expression ");
3369 print_pre_expr (dump_file, expr);
3370 fprintf (dump_file, " (%04d)\n",
3371 get_expr_value_id (expr));
3373 if (insert_into_preds_of_block (block,
3374 get_expression_id (expr),
3375 avail))
3376 new_stuff = true;
3379 /* If all edges produce the same value and that value is
3380 an invariant, then the PHI has the same value on all
3381 edges. Note this. */
3382 else if (!cant_insert && all_same && eprime
3383 && (edoubleprime->kind == CONSTANT
3384 || edoubleprime->kind == NAME)
3385 && !value_id_constant_p (val))
3387 unsigned int j;
3388 bitmap_iterator bi;
3389 bitmap exprset = value_expressions[val];
3391 unsigned int new_val = get_expr_value_id (edoubleprime);
3392 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bi)
3394 pre_expr expr = expression_for_id (j);
3396 if (expr->kind == NAME)
3398 vn_ssa_aux_t info = VN_INFO (PRE_EXPR_NAME (expr));
3399 /* Just reset the value id and valnum so it is
3400 the same as the constant we have discovered. */
3401 if (edoubleprime->kind == CONSTANT)
3403 info->valnum = PRE_EXPR_CONSTANT (edoubleprime);
3404 pre_stats.constified++;
3406 else
3407 info->valnum = VN_INFO (PRE_EXPR_NAME (edoubleprime))->valnum;
3408 info->value_id = new_val;
3415 exprs.release ();
3416 avail.release ();
3417 return new_stuff;
3421 /* Perform insertion for partially anticipatable expressions. There
3422 is only one case we will perform insertion for these. This case is
3423 if the expression is partially anticipatable, and fully available.
3424 In this case, we know that putting it earlier will enable us to
3425 remove the later computation. */
3428 static bool
3429 do_partial_partial_insertion (basic_block block, basic_block dom)
3431 bool new_stuff = false;
3432 vec<pre_expr> exprs;
3433 pre_expr expr;
3434 vec<pre_expr> avail = vNULL;
3435 int i;
3437 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3438 avail.safe_grow (EDGE_COUNT (block->preds));
3440 FOR_EACH_VEC_ELT (exprs, i, expr)
3442 if (expr->kind != NAME)
3444 unsigned int val;
3445 bool by_all = true;
3446 bool cant_insert = false;
3447 edge pred;
3448 basic_block bprime;
3449 pre_expr eprime = NULL;
3450 edge_iterator ei;
3452 val = get_expr_value_id (expr);
3453 if (bitmap_set_contains_value (PHI_GEN (block), val))
3454 continue;
3455 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3456 continue;
3458 FOR_EACH_EDGE (pred, ei, block->preds)
3460 unsigned int vprime;
3461 pre_expr edoubleprime;
3463 /* We should never run insertion for the exit block
3464 and so not come across fake pred edges. */
3465 gcc_assert (!(pred->flags & EDGE_FAKE));
3466 bprime = pred->src;
3467 eprime = phi_translate (expr, ANTIC_IN (block),
3468 PA_IN (block),
3469 bprime, block);
3471 /* eprime will generally only be NULL if the
3472 value of the expression, translated
3473 through the PHI for this predecessor, is
3474 undefined. If that is the case, we can't
3475 make the expression fully redundant,
3476 because its value is undefined along a
3477 predecessor path. We can thus break out
3478 early because it doesn't matter what the
3479 rest of the results are. */
3480 if (eprime == NULL)
3482 avail[pred->dest_idx] = NULL;
3483 cant_insert = true;
3484 break;
3487 eprime = fully_constant_expression (eprime);
3488 vprime = get_expr_value_id (eprime);
3489 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3490 avail[pred->dest_idx] = edoubleprime;
3491 if (edoubleprime == NULL)
3493 by_all = false;
3494 break;
3498 /* If we can insert it, it's not the same value
3499 already existing along every predecessor, and
3500 it's defined by some predecessor, it is
3501 partially redundant. */
3502 if (!cant_insert && by_all)
3504 edge succ;
3505 bool do_insertion = false;
3507 /* Insert only if we can remove a later expression on a path
3508 that we want to optimize for speed.
3509 The phi node that we will be inserting in BLOCK is not free,
3510 and inserting it for the sake of !optimize_for_speed successor
3511 may cause regressions on the speed path. */
3512 FOR_EACH_EDGE (succ, ei, block->succs)
3514 if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3515 || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3517 if (optimize_edge_for_speed_p (succ))
3518 do_insertion = true;
3522 if (!do_insertion)
3524 if (dump_file && (dump_flags & TDF_DETAILS))
3526 fprintf (dump_file, "Skipping partial partial redundancy "
3527 "for expression ");
3528 print_pre_expr (dump_file, expr);
3529 fprintf (dump_file, " (%04d), not (partially) anticipated "
3530 "on any to be optimized for speed edges\n", val);
3533 else if (dbg_cnt (treepre_insert))
3535 pre_stats.pa_insert++;
3536 if (dump_file && (dump_flags & TDF_DETAILS))
3538 fprintf (dump_file, "Found partial partial redundancy "
3539 "for expression ");
3540 print_pre_expr (dump_file, expr);
3541 fprintf (dump_file, " (%04d)\n",
3542 get_expr_value_id (expr));
3544 if (insert_into_preds_of_block (block,
3545 get_expression_id (expr),
3546 avail))
3547 new_stuff = true;
3553 exprs.release ();
3554 avail.release ();
3555 return new_stuff;
3558 static bool
3559 insert_aux (basic_block block)
3561 basic_block son;
3562 bool new_stuff = false;
3564 if (block)
3566 basic_block dom;
3567 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3568 if (dom)
3570 unsigned i;
3571 bitmap_iterator bi;
3572 bitmap_set_t newset = NEW_SETS (dom);
3573 if (newset)
3575 /* Note that we need to value_replace both NEW_SETS, and
3576 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3577 represented by some non-simple expression here that we want
3578 to replace it with. */
3579 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3581 pre_expr expr = expression_for_id (i);
3582 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3583 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3586 if (!single_pred_p (block))
3588 new_stuff |= do_regular_insertion (block, dom);
3589 if (do_partial_partial)
3590 new_stuff |= do_partial_partial_insertion (block, dom);
3594 for (son = first_dom_son (CDI_DOMINATORS, block);
3595 son;
3596 son = next_dom_son (CDI_DOMINATORS, son))
3598 new_stuff |= insert_aux (son);
3601 return new_stuff;
3604 /* Perform insertion of partially redundant values. */
3606 static void
3607 insert (void)
3609 bool new_stuff = true;
3610 basic_block bb;
3611 int num_iterations = 0;
3613 FOR_ALL_BB (bb)
3614 NEW_SETS (bb) = bitmap_set_new ();
3616 while (new_stuff)
3618 num_iterations++;
3619 if (dump_file && dump_flags & TDF_DETAILS)
3620 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3621 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
3623 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3627 /* Compute the AVAIL set for all basic blocks.
3629 This function performs value numbering of the statements in each basic
3630 block. The AVAIL sets are built from information we glean while doing
3631 this value numbering, since the AVAIL sets contain only one entry per
3632 value.
3634 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3635 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3637 static void
3638 compute_avail (void)
3641 basic_block block, son;
3642 basic_block *worklist;
3643 size_t sp = 0;
3644 unsigned i;
3646 /* We pretend that default definitions are defined in the entry block.
3647 This includes function arguments and the static chain decl. */
3648 for (i = 1; i < num_ssa_names; ++i)
3650 tree name = ssa_name (i);
3651 pre_expr e;
3652 if (!name
3653 || !SSA_NAME_IS_DEFAULT_DEF (name)
3654 || has_zero_uses (name)
3655 || virtual_operand_p (name))
3656 continue;
3658 e = get_or_alloc_expr_for_name (name);
3659 add_to_value (get_expr_value_id (e), e);
3660 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), e);
3661 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), e);
3664 if (dump_file && (dump_flags & TDF_DETAILS))
3666 print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR),
3667 "tmp_gen", ENTRY_BLOCK);
3668 print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR),
3669 "avail_out", ENTRY_BLOCK);
3672 /* Allocate the worklist. */
3673 worklist = XNEWVEC (basic_block, n_basic_blocks);
3675 /* Seed the algorithm by putting the dominator children of the entry
3676 block on the worklist. */
3677 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR);
3678 son;
3679 son = next_dom_son (CDI_DOMINATORS, son))
3680 worklist[sp++] = son;
3682 /* Loop until the worklist is empty. */
3683 while (sp)
3685 gimple_stmt_iterator gsi;
3686 gimple stmt;
3687 basic_block dom;
3689 /* Pick a block from the worklist. */
3690 block = worklist[--sp];
3692 /* Initially, the set of available values in BLOCK is that of
3693 its immediate dominator. */
3694 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3695 if (dom)
3696 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3698 /* Generate values for PHI nodes. */
3699 for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
3701 tree result = gimple_phi_result (gsi_stmt (gsi));
3703 /* We have no need for virtual phis, as they don't represent
3704 actual computations. */
3705 if (virtual_operand_p (result))
3706 continue;
3708 pre_expr e = get_or_alloc_expr_for_name (result);
3709 add_to_value (get_expr_value_id (e), e);
3710 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3711 bitmap_insert_into_set (PHI_GEN (block), e);
3714 BB_MAY_NOTRETURN (block) = 0;
3716 /* Now compute value numbers and populate value sets with all
3717 the expressions computed in BLOCK. */
3718 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
3720 ssa_op_iter iter;
3721 tree op;
3723 stmt = gsi_stmt (gsi);
3725 /* Cache whether the basic-block has any non-visible side-effect
3726 or control flow.
3727 If this isn't a call or it is the last stmt in the
3728 basic-block then the CFG represents things correctly. */
3729 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3731 /* Non-looping const functions always return normally.
3732 Otherwise the call might not return or have side-effects
3733 that forbids hoisting possibly trapping expressions
3734 before it. */
3735 int flags = gimple_call_flags (stmt);
3736 if (!(flags & ECF_CONST)
3737 || (flags & ECF_LOOPING_CONST_OR_PURE))
3738 BB_MAY_NOTRETURN (block) = 1;
3741 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3743 pre_expr e = get_or_alloc_expr_for_name (op);
3745 add_to_value (get_expr_value_id (e), e);
3746 bitmap_insert_into_set (TMP_GEN (block), e);
3747 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3750 if (gimple_has_side_effects (stmt)
3751 || stmt_could_throw_p (stmt)
3752 || is_gimple_debug (stmt))
3753 continue;
3755 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3757 if (ssa_undefined_value_p (op))
3758 continue;
3759 pre_expr e = get_or_alloc_expr_for_name (op);
3760 bitmap_value_insert_into_set (EXP_GEN (block), e);
3763 switch (gimple_code (stmt))
3765 case GIMPLE_RETURN:
3766 continue;
3768 case GIMPLE_CALL:
3770 vn_reference_t ref;
3771 pre_expr result = NULL;
3772 vec<vn_reference_op_s> ops = vNULL;
3774 /* We can value number only calls to real functions. */
3775 if (gimple_call_internal_p (stmt))
3776 continue;
3778 copy_reference_ops_from_call (stmt, &ops);
3779 vn_reference_lookup_pieces (gimple_vuse (stmt), 0,
3780 gimple_expr_type (stmt),
3781 ops, &ref, VN_NOWALK);
3782 ops.release ();
3783 if (!ref)
3784 continue;
3786 /* If the value of the call is not invalidated in
3787 this block until it is computed, add the expression
3788 to EXP_GEN. */
3789 if (!gimple_vuse (stmt)
3790 || gimple_code
3791 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3792 || gimple_bb (SSA_NAME_DEF_STMT
3793 (gimple_vuse (stmt))) != block)
3795 result = (pre_expr) pool_alloc (pre_expr_pool);
3796 result->kind = REFERENCE;
3797 result->id = 0;
3798 PRE_EXPR_REFERENCE (result) = ref;
3800 get_or_alloc_expression_id (result);
3801 add_to_value (get_expr_value_id (result), result);
3802 bitmap_value_insert_into_set (EXP_GEN (block), result);
3804 continue;
3807 case GIMPLE_ASSIGN:
3809 pre_expr result = NULL;
3810 switch (vn_get_stmt_kind (stmt))
3812 case VN_NARY:
3814 enum tree_code code = gimple_assign_rhs_code (stmt);
3815 vn_nary_op_t nary;
3817 /* COND_EXPR and VEC_COND_EXPR are awkward in
3818 that they contain an embedded complex expression.
3819 Don't even try to shove those through PRE. */
3820 if (code == COND_EXPR
3821 || code == VEC_COND_EXPR)
3822 continue;
3824 vn_nary_op_lookup_stmt (stmt, &nary);
3825 if (!nary)
3826 continue;
3828 /* If the NARY traps and there was a preceding
3829 point in the block that might not return avoid
3830 adding the nary to EXP_GEN. */
3831 if (BB_MAY_NOTRETURN (block)
3832 && vn_nary_may_trap (nary))
3833 continue;
3835 result = (pre_expr) pool_alloc (pre_expr_pool);
3836 result->kind = NARY;
3837 result->id = 0;
3838 PRE_EXPR_NARY (result) = nary;
3839 break;
3842 case VN_REFERENCE:
3844 vn_reference_t ref;
3845 vn_reference_lookup (gimple_assign_rhs1 (stmt),
3846 gimple_vuse (stmt),
3847 VN_WALK, &ref);
3848 if (!ref)
3849 continue;
3851 /* If the value of the reference is not invalidated in
3852 this block until it is computed, add the expression
3853 to EXP_GEN. */
3854 if (gimple_vuse (stmt))
3856 gimple def_stmt;
3857 bool ok = true;
3858 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
3859 while (!gimple_nop_p (def_stmt)
3860 && gimple_code (def_stmt) != GIMPLE_PHI
3861 && gimple_bb (def_stmt) == block)
3863 if (stmt_may_clobber_ref_p
3864 (def_stmt, gimple_assign_rhs1 (stmt)))
3866 ok = false;
3867 break;
3869 def_stmt
3870 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
3872 if (!ok)
3873 continue;
3876 result = (pre_expr) pool_alloc (pre_expr_pool);
3877 result->kind = REFERENCE;
3878 result->id = 0;
3879 PRE_EXPR_REFERENCE (result) = ref;
3880 break;
3883 default:
3884 continue;
3887 get_or_alloc_expression_id (result);
3888 add_to_value (get_expr_value_id (result), result);
3889 bitmap_value_insert_into_set (EXP_GEN (block), result);
3890 continue;
3892 default:
3893 break;
3897 if (dump_file && (dump_flags & TDF_DETAILS))
3899 print_bitmap_set (dump_file, EXP_GEN (block),
3900 "exp_gen", block->index);
3901 print_bitmap_set (dump_file, PHI_GEN (block),
3902 "phi_gen", block->index);
3903 print_bitmap_set (dump_file, TMP_GEN (block),
3904 "tmp_gen", block->index);
3905 print_bitmap_set (dump_file, AVAIL_OUT (block),
3906 "avail_out", block->index);
3909 /* Put the dominator children of BLOCK on the worklist of blocks
3910 to compute available sets for. */
3911 for (son = first_dom_son (CDI_DOMINATORS, block);
3912 son;
3913 son = next_dom_son (CDI_DOMINATORS, son))
3914 worklist[sp++] = son;
3917 free (worklist);
3921 /* Local state for the eliminate domwalk. */
3922 static vec<gimple> el_to_remove;
3923 static vec<gimple> el_to_update;
3924 static unsigned int el_todo;
3925 static vec<tree> el_avail;
3926 static vec<tree> el_avail_stack;
3928 /* Return a leader for OP that is available at the current point of the
3929 eliminate domwalk. */
3931 static tree
3932 eliminate_avail (tree op)
3934 tree valnum = VN_INFO (op)->valnum;
3935 if (TREE_CODE (valnum) == SSA_NAME)
3937 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
3938 return valnum;
3939 if (el_avail.length () > SSA_NAME_VERSION (valnum))
3940 return el_avail[SSA_NAME_VERSION (valnum)];
3942 else if (is_gimple_min_invariant (valnum))
3943 return valnum;
3944 return NULL_TREE;
3947 /* At the current point of the eliminate domwalk make OP available. */
3949 static void
3950 eliminate_push_avail (tree op)
3952 tree valnum = VN_INFO (op)->valnum;
3953 if (TREE_CODE (valnum) == SSA_NAME)
3955 if (el_avail.length () <= SSA_NAME_VERSION (valnum))
3956 el_avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1);
3957 el_avail[SSA_NAME_VERSION (valnum)] = op;
3958 el_avail_stack.safe_push (op);
3962 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
3963 the leader for the expression if insertion was successful. */
3965 static tree
3966 eliminate_insert (gimple_stmt_iterator *gsi, tree val)
3968 tree expr = vn_get_expr_for (val);
3969 if (!CONVERT_EXPR_P (expr)
3970 && TREE_CODE (expr) != VIEW_CONVERT_EXPR)
3971 return NULL_TREE;
3973 tree op = TREE_OPERAND (expr, 0);
3974 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (op) : op;
3975 if (!leader)
3976 return NULL_TREE;
3978 tree res = make_temp_ssa_name (TREE_TYPE (val), NULL, "pretmp");
3979 gimple tem = gimple_build_assign (res,
3980 fold_build1 (TREE_CODE (expr),
3981 TREE_TYPE (expr), leader));
3982 gsi_insert_before (gsi, tem, GSI_SAME_STMT);
3983 VN_INFO_GET (res)->valnum = val;
3985 if (TREE_CODE (leader) == SSA_NAME)
3986 gimple_set_plf (SSA_NAME_DEF_STMT (leader), NECESSARY, true);
3988 pre_stats.insertions++;
3989 if (dump_file && (dump_flags & TDF_DETAILS))
3991 fprintf (dump_file, "Inserted ");
3992 print_gimple_stmt (dump_file, tem, 0, 0);
3995 return res;
3998 /* Perform elimination for the basic-block B during the domwalk. */
4000 static void
4001 eliminate_bb (dom_walk_data *, basic_block b)
4003 gimple_stmt_iterator gsi;
4004 gimple stmt;
4006 /* Mark new bb. */
4007 el_avail_stack.safe_push (NULL_TREE);
4009 for (gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
4011 gimple stmt, phi = gsi_stmt (gsi);
4012 tree sprime = NULL_TREE, res = PHI_RESULT (phi);
4013 gimple_stmt_iterator gsi2;
4015 /* We want to perform redundant PHI elimination. Do so by
4016 replacing the PHI with a single copy if possible.
4017 Do not touch inserted, single-argument or virtual PHIs. */
4018 if (gimple_phi_num_args (phi) == 1
4019 || virtual_operand_p (res))
4021 gsi_next (&gsi);
4022 continue;
4025 sprime = eliminate_avail (res);
4026 if (!sprime
4027 || sprime == res)
4029 eliminate_push_avail (res);
4030 gsi_next (&gsi);
4031 continue;
4033 else if (is_gimple_min_invariant (sprime))
4035 if (!useless_type_conversion_p (TREE_TYPE (res),
4036 TREE_TYPE (sprime)))
4037 sprime = fold_convert (TREE_TYPE (res), sprime);
4040 if (dump_file && (dump_flags & TDF_DETAILS))
4042 fprintf (dump_file, "Replaced redundant PHI node defining ");
4043 print_generic_expr (dump_file, res, 0);
4044 fprintf (dump_file, " with ");
4045 print_generic_expr (dump_file, sprime, 0);
4046 fprintf (dump_file, "\n");
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 stmt = gimple_build_assign (res, sprime);
4059 SSA_NAME_DEF_STMT (res) = stmt;
4060 gimple_set_plf (stmt, NECESSARY, gimple_plf (phi, NECESSARY));
4062 gsi2 = gsi_after_labels (b);
4063 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
4064 /* Queue the copy for eventual removal. */
4065 el_to_remove.safe_push (stmt);
4066 /* If we inserted this PHI node ourself, it's not an elimination. */
4067 if (inserted_exprs
4068 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
4069 pre_stats.phis--;
4070 else
4071 pre_stats.eliminations++;
4074 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi); gsi_next (&gsi))
4076 tree lhs = NULL_TREE;
4077 tree rhs = NULL_TREE;
4079 stmt = gsi_stmt (gsi);
4081 if (gimple_has_lhs (stmt))
4082 lhs = gimple_get_lhs (stmt);
4084 if (gimple_assign_single_p (stmt))
4085 rhs = gimple_assign_rhs1 (stmt);
4087 /* Lookup the RHS of the expression, see if we have an
4088 available computation for it. If so, replace the RHS with
4089 the available computation.
4091 See PR43491.
4092 We don't replace global register variable when it is a the RHS of
4093 a single assign. We do replace local register variable since gcc
4094 does not guarantee local variable will be allocated in register. */
4095 if (gimple_has_lhs (stmt)
4096 && TREE_CODE (lhs) == SSA_NAME
4097 && !gimple_assign_ssa_name_copy_p (stmt)
4098 && (!gimple_assign_single_p (stmt)
4099 || (!is_gimple_min_invariant (rhs)
4100 && (gimple_assign_rhs_code (stmt) != VAR_DECL
4101 || !is_global_var (rhs)
4102 || !DECL_HARD_REGISTER (rhs))))
4103 && !gimple_has_volatile_ops (stmt))
4105 tree sprime;
4106 gimple orig_stmt = stmt;
4108 sprime = eliminate_avail (lhs);
4109 if (!sprime)
4111 /* If there is no existing usable leader but SCCVN thinks
4112 it has an expression it wants to use as replacement,
4113 insert that. */
4114 tree val = VN_INFO (lhs)->valnum;
4115 if (val != VN_TOP
4116 && TREE_CODE (val) == SSA_NAME
4117 && VN_INFO (val)->needs_insertion
4118 && (sprime = eliminate_insert (&gsi, val)) != NULL_TREE)
4119 eliminate_push_avail (sprime);
4121 else if (is_gimple_min_invariant (sprime))
4123 /* If there is no existing leader but SCCVN knows this
4124 value is constant, use that constant. */
4125 if (!useless_type_conversion_p (TREE_TYPE (lhs),
4126 TREE_TYPE (sprime)))
4127 sprime = fold_convert (TREE_TYPE (lhs), sprime);
4129 if (dump_file && (dump_flags & TDF_DETAILS))
4131 fprintf (dump_file, "Replaced ");
4132 print_gimple_expr (dump_file, stmt, 0, 0);
4133 fprintf (dump_file, " with ");
4134 print_generic_expr (dump_file, sprime, 0);
4135 fprintf (dump_file, " in ");
4136 print_gimple_stmt (dump_file, stmt, 0, 0);
4138 pre_stats.eliminations++;
4139 propagate_tree_value_into_stmt (&gsi, sprime);
4140 stmt = gsi_stmt (gsi);
4141 update_stmt (stmt);
4143 /* If we removed EH side-effects from the statement, clean
4144 its EH information. */
4145 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4147 bitmap_set_bit (need_eh_cleanup,
4148 gimple_bb (stmt)->index);
4149 if (dump_file && (dump_flags & TDF_DETAILS))
4150 fprintf (dump_file, " Removed EH side-effects.\n");
4152 continue;
4155 /* If there is no usable leader mark lhs as leader for its value. */
4156 if (!sprime)
4157 eliminate_push_avail (lhs);
4159 if (sprime
4160 && sprime != lhs
4161 && (rhs == NULL_TREE
4162 || TREE_CODE (rhs) != SSA_NAME
4163 || may_propagate_copy (rhs, sprime)))
4165 bool can_make_abnormal_goto
4166 = is_gimple_call (stmt)
4167 && stmt_can_make_abnormal_goto (stmt);
4169 gcc_assert (sprime != rhs);
4171 if (dump_file && (dump_flags & TDF_DETAILS))
4173 fprintf (dump_file, "Replaced ");
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, " in ");
4178 print_gimple_stmt (dump_file, stmt, 0, 0);
4181 if (TREE_CODE (sprime) == SSA_NAME)
4182 gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
4183 NECESSARY, true);
4184 /* We need to make sure the new and old types actually match,
4185 which may require adding a simple cast, which fold_convert
4186 will do for us. */
4187 if ((!rhs || TREE_CODE (rhs) != SSA_NAME)
4188 && !useless_type_conversion_p (gimple_expr_type (stmt),
4189 TREE_TYPE (sprime)))
4190 sprime = fold_convert (gimple_expr_type (stmt), sprime);
4192 pre_stats.eliminations++;
4193 propagate_tree_value_into_stmt (&gsi, sprime);
4194 stmt = gsi_stmt (gsi);
4195 update_stmt (stmt);
4197 /* If we removed EH side-effects from the statement, clean
4198 its EH information. */
4199 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4201 bitmap_set_bit (need_eh_cleanup,
4202 gimple_bb (stmt)->index);
4203 if (dump_file && (dump_flags & TDF_DETAILS))
4204 fprintf (dump_file, " Removed EH side-effects.\n");
4207 /* Likewise for AB side-effects. */
4208 if (can_make_abnormal_goto
4209 && !stmt_can_make_abnormal_goto (stmt))
4211 bitmap_set_bit (need_ab_cleanup,
4212 gimple_bb (stmt)->index);
4213 if (dump_file && (dump_flags & TDF_DETAILS))
4214 fprintf (dump_file, " Removed AB side-effects.\n");
4218 /* If the statement is a scalar store, see if the expression
4219 has the same value number as its rhs. If so, the store is
4220 dead. */
4221 else if (gimple_assign_single_p (stmt)
4222 && !gimple_has_volatile_ops (stmt)
4223 && !is_gimple_reg (gimple_assign_lhs (stmt))
4224 && (TREE_CODE (rhs) == SSA_NAME
4225 || is_gimple_min_invariant (rhs)))
4227 tree val;
4228 val = vn_reference_lookup (gimple_assign_lhs (stmt),
4229 gimple_vuse (stmt), VN_WALK, NULL);
4230 if (TREE_CODE (rhs) == SSA_NAME)
4231 rhs = VN_INFO (rhs)->valnum;
4232 if (val
4233 && operand_equal_p (val, rhs, 0))
4235 if (dump_file && (dump_flags & TDF_DETAILS))
4237 fprintf (dump_file, "Deleted redundant store ");
4238 print_gimple_stmt (dump_file, stmt, 0, 0);
4241 /* Queue stmt for removal. */
4242 el_to_remove.safe_push (stmt);
4245 /* Visit COND_EXPRs and fold the comparison with the
4246 available value-numbers. */
4247 else if (gimple_code (stmt) == GIMPLE_COND)
4249 tree op0 = gimple_cond_lhs (stmt);
4250 tree op1 = gimple_cond_rhs (stmt);
4251 tree result;
4253 if (TREE_CODE (op0) == SSA_NAME)
4254 op0 = VN_INFO (op0)->valnum;
4255 if (TREE_CODE (op1) == SSA_NAME)
4256 op1 = VN_INFO (op1)->valnum;
4257 result = fold_binary (gimple_cond_code (stmt), boolean_type_node,
4258 op0, op1);
4259 if (result && TREE_CODE (result) == INTEGER_CST)
4261 if (integer_zerop (result))
4262 gimple_cond_make_false (stmt);
4263 else
4264 gimple_cond_make_true (stmt);
4265 update_stmt (stmt);
4266 el_todo = TODO_cleanup_cfg;
4269 /* Visit indirect calls and turn them into direct calls if
4270 possible. */
4271 if (is_gimple_call (stmt))
4273 tree orig_fn = gimple_call_fn (stmt);
4274 tree fn;
4275 if (!orig_fn)
4276 continue;
4277 if (TREE_CODE (orig_fn) == SSA_NAME)
4278 fn = VN_INFO (orig_fn)->valnum;
4279 else if (TREE_CODE (orig_fn) == OBJ_TYPE_REF
4280 && TREE_CODE (OBJ_TYPE_REF_EXPR (orig_fn)) == SSA_NAME)
4281 fn = VN_INFO (OBJ_TYPE_REF_EXPR (orig_fn))->valnum;
4282 else
4283 continue;
4284 if (gimple_call_addr_fndecl (fn) != NULL_TREE
4285 && useless_type_conversion_p (TREE_TYPE (orig_fn),
4286 TREE_TYPE (fn)))
4288 bool can_make_abnormal_goto
4289 = stmt_can_make_abnormal_goto (stmt);
4290 bool was_noreturn = gimple_call_noreturn_p (stmt);
4292 if (dump_file && (dump_flags & TDF_DETAILS))
4294 fprintf (dump_file, "Replacing call target with ");
4295 print_generic_expr (dump_file, fn, 0);
4296 fprintf (dump_file, " in ");
4297 print_gimple_stmt (dump_file, stmt, 0, 0);
4300 gimple_call_set_fn (stmt, fn);
4301 el_to_update.safe_push (stmt);
4303 /* When changing a call into a noreturn call, cfg cleanup
4304 is needed to fix up the noreturn call. */
4305 if (!was_noreturn && gimple_call_noreturn_p (stmt))
4306 el_todo |= TODO_cleanup_cfg;
4308 /* If we removed EH side-effects from the statement, clean
4309 its EH information. */
4310 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
4312 bitmap_set_bit (need_eh_cleanup,
4313 gimple_bb (stmt)->index);
4314 if (dump_file && (dump_flags & TDF_DETAILS))
4315 fprintf (dump_file, " Removed EH side-effects.\n");
4318 /* Likewise for AB side-effects. */
4319 if (can_make_abnormal_goto
4320 && !stmt_can_make_abnormal_goto (stmt))
4322 bitmap_set_bit (need_ab_cleanup,
4323 gimple_bb (stmt)->index);
4324 if (dump_file && (dump_flags & TDF_DETAILS))
4325 fprintf (dump_file, " Removed AB side-effects.\n");
4328 /* Changing an indirect call to a direct call may
4329 have exposed different semantics. This may
4330 require an SSA update. */
4331 el_todo |= TODO_update_ssa_only_virtuals;
4337 /* Make no longer available leaders no longer available. */
4339 static void
4340 eliminate_leave_block (dom_walk_data *, basic_block)
4342 tree entry;
4343 while ((entry = el_avail_stack.pop ()) != NULL_TREE)
4344 el_avail[SSA_NAME_VERSION (VN_INFO (entry)->valnum)] = NULL_TREE;
4347 /* Eliminate fully redundant computations. */
4349 static unsigned int
4350 eliminate (void)
4352 struct dom_walk_data walk_data;
4353 gimple_stmt_iterator gsi;
4354 gimple stmt;
4355 unsigned i;
4357 need_eh_cleanup = BITMAP_ALLOC (NULL);
4358 need_ab_cleanup = BITMAP_ALLOC (NULL);
4360 el_to_remove.create (0);
4361 el_to_update.create (0);
4362 el_todo = 0;
4363 el_avail.create (0);
4364 el_avail_stack.create (0);
4366 walk_data.dom_direction = CDI_DOMINATORS;
4367 walk_data.initialize_block_local_data = NULL;
4368 walk_data.before_dom_children = eliminate_bb;
4369 walk_data.after_dom_children = eliminate_leave_block;
4370 walk_data.global_data = NULL;
4371 walk_data.block_local_data_size = 0;
4372 init_walk_dominator_tree (&walk_data);
4373 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
4374 fini_walk_dominator_tree (&walk_data);
4376 el_avail.release ();
4377 el_avail_stack.release ();
4379 /* We cannot remove stmts during BB walk, especially not release SSA
4380 names there as this confuses the VN machinery. The stmts ending
4381 up in el_to_remove are either stores or simple copies. */
4382 FOR_EACH_VEC_ELT (el_to_remove, i, stmt)
4384 tree lhs = gimple_assign_lhs (stmt);
4385 tree rhs = gimple_assign_rhs1 (stmt);
4386 use_operand_p use_p;
4387 gimple use_stmt;
4389 /* If there is a single use only, propagate the equivalency
4390 instead of keeping the copy. */
4391 if (TREE_CODE (lhs) == SSA_NAME
4392 && TREE_CODE (rhs) == SSA_NAME
4393 && single_imm_use (lhs, &use_p, &use_stmt)
4394 && may_propagate_copy (USE_FROM_PTR (use_p), rhs))
4396 SET_USE (use_p, rhs);
4397 update_stmt (use_stmt);
4398 if (inserted_exprs
4399 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (lhs))
4400 && TREE_CODE (rhs) == SSA_NAME)
4401 gimple_set_plf (SSA_NAME_DEF_STMT (rhs), NECESSARY, true);
4404 /* If this is a store or a now unused copy, remove it. */
4405 if (TREE_CODE (lhs) != SSA_NAME
4406 || has_zero_uses (lhs))
4408 basic_block bb = gimple_bb (stmt);
4409 gsi = gsi_for_stmt (stmt);
4410 unlink_stmt_vdef (stmt);
4411 if (gsi_remove (&gsi, true))
4412 bitmap_set_bit (need_eh_cleanup, bb->index);
4413 if (inserted_exprs
4414 && TREE_CODE (lhs) == SSA_NAME)
4415 bitmap_clear_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
4416 release_defs (stmt);
4419 el_to_remove.release ();
4421 /* We cannot update call statements with virtual operands during
4422 SSA walk. This might remove them which in turn makes our
4423 VN lattice invalid. */
4424 FOR_EACH_VEC_ELT (el_to_update, i, stmt)
4425 update_stmt (stmt);
4426 el_to_update.release ();
4428 return el_todo;
4431 /* Perform CFG cleanups made necessary by elimination. */
4433 static unsigned
4434 fini_eliminate (void)
4436 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
4437 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
4439 if (do_eh_cleanup)
4440 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
4442 if (do_ab_cleanup)
4443 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
4445 BITMAP_FREE (need_eh_cleanup);
4446 BITMAP_FREE (need_ab_cleanup);
4448 if (do_eh_cleanup || do_ab_cleanup)
4449 return TODO_cleanup_cfg;
4450 return 0;
4453 /* Borrow a bit of tree-ssa-dce.c for the moment.
4454 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4455 this may be a bit faster, and we may want critical edges kept split. */
4457 /* If OP's defining statement has not already been determined to be necessary,
4458 mark that statement necessary. Return the stmt, if it is newly
4459 necessary. */
4461 static inline gimple
4462 mark_operand_necessary (tree op)
4464 gimple stmt;
4466 gcc_assert (op);
4468 if (TREE_CODE (op) != SSA_NAME)
4469 return NULL;
4471 stmt = SSA_NAME_DEF_STMT (op);
4472 gcc_assert (stmt);
4474 if (gimple_plf (stmt, NECESSARY)
4475 || gimple_nop_p (stmt))
4476 return NULL;
4478 gimple_set_plf (stmt, NECESSARY, true);
4479 return stmt;
4482 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4483 to insert PHI nodes sometimes, and because value numbering of casts isn't
4484 perfect, we sometimes end up inserting dead code. This simple DCE-like
4485 pass removes any insertions we made that weren't actually used. */
4487 static void
4488 remove_dead_inserted_code (void)
4490 bitmap worklist;
4491 unsigned i;
4492 bitmap_iterator bi;
4493 gimple t;
4495 worklist = BITMAP_ALLOC (NULL);
4496 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4498 t = SSA_NAME_DEF_STMT (ssa_name (i));
4499 if (gimple_plf (t, NECESSARY))
4500 bitmap_set_bit (worklist, i);
4502 while (!bitmap_empty_p (worklist))
4504 i = bitmap_first_set_bit (worklist);
4505 bitmap_clear_bit (worklist, i);
4506 t = SSA_NAME_DEF_STMT (ssa_name (i));
4508 /* PHI nodes are somewhat special in that each PHI alternative has
4509 data and control dependencies. All the statements feeding the
4510 PHI node's arguments are always necessary. */
4511 if (gimple_code (t) == GIMPLE_PHI)
4513 unsigned k;
4515 for (k = 0; k < gimple_phi_num_args (t); k++)
4517 tree arg = PHI_ARG_DEF (t, k);
4518 if (TREE_CODE (arg) == SSA_NAME)
4520 gimple n = mark_operand_necessary (arg);
4521 if (n)
4522 bitmap_set_bit (worklist, SSA_NAME_VERSION (arg));
4526 else
4528 /* Propagate through the operands. Examine all the USE, VUSE and
4529 VDEF operands in this statement. Mark all the statements
4530 which feed this statement's uses as necessary. */
4531 ssa_op_iter iter;
4532 tree use;
4534 /* The operands of VDEF expressions are also needed as they
4535 represent potential definitions that may reach this
4536 statement (VDEF operands allow us to follow def-def
4537 links). */
4539 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
4541 gimple n = mark_operand_necessary (use);
4542 if (n)
4543 bitmap_set_bit (worklist, SSA_NAME_VERSION (use));
4548 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4550 t = SSA_NAME_DEF_STMT (ssa_name (i));
4551 if (!gimple_plf (t, NECESSARY))
4553 gimple_stmt_iterator gsi;
4555 if (dump_file && (dump_flags & TDF_DETAILS))
4557 fprintf (dump_file, "Removing unnecessary insertion:");
4558 print_gimple_stmt (dump_file, t, 0, 0);
4561 gsi = gsi_for_stmt (t);
4562 if (gimple_code (t) == GIMPLE_PHI)
4563 remove_phi_node (&gsi, true);
4564 else
4566 gsi_remove (&gsi, true);
4567 release_defs (t);
4571 BITMAP_FREE (worklist);
4575 /* Initialize data structures used by PRE. */
4577 static void
4578 init_pre (void)
4580 basic_block bb;
4582 next_expression_id = 1;
4583 expressions.create (0);
4584 expressions.safe_push (NULL);
4585 value_expressions.create (get_max_value_id () + 1);
4586 value_expressions.safe_grow_cleared (get_max_value_id() + 1);
4587 name_to_id.create (0);
4589 inserted_exprs = BITMAP_ALLOC (NULL);
4591 connect_infinite_loops_to_exit ();
4592 memset (&pre_stats, 0, sizeof (pre_stats));
4594 postorder = XNEWVEC (int, n_basic_blocks);
4595 postorder_num = inverted_post_order_compute (postorder);
4597 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4599 calculate_dominance_info (CDI_POST_DOMINATORS);
4600 calculate_dominance_info (CDI_DOMINATORS);
4602 bitmap_obstack_initialize (&grand_bitmap_obstack);
4603 phi_translate_table.create (5110);
4604 expression_to_id.create (num_ssa_names * 3);
4605 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
4606 sizeof (struct bitmap_set), 30);
4607 pre_expr_pool = create_alloc_pool ("pre_expr nodes",
4608 sizeof (struct pre_expr_d), 30);
4609 FOR_ALL_BB (bb)
4611 EXP_GEN (bb) = bitmap_set_new ();
4612 PHI_GEN (bb) = bitmap_set_new ();
4613 TMP_GEN (bb) = bitmap_set_new ();
4614 AVAIL_OUT (bb) = bitmap_set_new ();
4619 /* Deallocate data structures used by PRE. */
4621 static void
4622 fini_pre ()
4624 free (postorder);
4625 value_expressions.release ();
4626 BITMAP_FREE (inserted_exprs);
4627 bitmap_obstack_release (&grand_bitmap_obstack);
4628 free_alloc_pool (bitmap_set_pool);
4629 free_alloc_pool (pre_expr_pool);
4630 phi_translate_table.dispose ();
4631 expression_to_id.dispose ();
4632 name_to_id.release ();
4634 free_aux_for_blocks ();
4636 free_dominance_info (CDI_POST_DOMINATORS);
4639 /* Gate and execute functions for PRE. */
4641 static unsigned int
4642 do_pre (void)
4644 unsigned int todo = 0;
4646 do_partial_partial =
4647 flag_tree_partial_pre && optimize_function_for_speed_p (cfun);
4649 /* This has to happen before SCCVN runs because
4650 loop_optimizer_init may create new phis, etc. */
4651 loop_optimizer_init (LOOPS_NORMAL);
4653 if (!run_scc_vn (VN_WALK))
4655 loop_optimizer_finalize ();
4656 return 0;
4659 init_pre ();
4660 scev_initialize ();
4662 /* Collect and value number expressions computed in each basic block. */
4663 compute_avail ();
4665 /* Insert can get quite slow on an incredibly large number of basic
4666 blocks due to some quadratic behavior. Until this behavior is
4667 fixed, don't run it when he have an incredibly large number of
4668 bb's. If we aren't going to run insert, there is no point in
4669 computing ANTIC, either, even though it's plenty fast. */
4670 if (n_basic_blocks < 4000)
4672 compute_antic ();
4673 insert ();
4676 /* Make sure to remove fake edges before committing our inserts.
4677 This makes sure we don't end up with extra critical edges that
4678 we would need to split. */
4679 remove_fake_exit_edges ();
4680 gsi_commit_edge_inserts ();
4682 /* Remove all the redundant expressions. */
4683 todo |= eliminate ();
4685 statistics_counter_event (cfun, "Insertions", pre_stats.insertions);
4686 statistics_counter_event (cfun, "PA inserted", pre_stats.pa_insert);
4687 statistics_counter_event (cfun, "New PHIs", pre_stats.phis);
4688 statistics_counter_event (cfun, "Eliminated", pre_stats.eliminations);
4689 statistics_counter_event (cfun, "Constified", pre_stats.constified);
4691 clear_expression_ids ();
4692 remove_dead_inserted_code ();
4693 todo |= TODO_verify_flow;
4695 scev_finalize ();
4696 fini_pre ();
4697 todo |= fini_eliminate ();
4698 loop_optimizer_finalize ();
4700 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4701 case we can merge the block with the remaining predecessor of the block.
4702 It should either:
4703 - call merge_blocks after each tail merge iteration
4704 - call merge_blocks after all tail merge iterations
4705 - mark TODO_cleanup_cfg when necessary
4706 - share the cfg cleanup with fini_pre. */
4707 todo |= tail_merge_optimize (todo);
4709 free_scc_vn ();
4711 /* Tail merging invalidates the virtual SSA web, together with
4712 cfg-cleanup opportunities exposed by PRE this will wreck the
4713 SSA updating machinery. So make sure to run update-ssa
4714 manually, before eventually scheduling cfg-cleanup as part of
4715 the todo. */
4716 update_ssa (TODO_update_ssa_only_virtuals);
4718 return todo;
4721 static bool
4722 gate_pre (void)
4724 return flag_tree_pre != 0;
4727 struct gimple_opt_pass pass_pre =
4730 GIMPLE_PASS,
4731 "pre", /* name */
4732 OPTGROUP_NONE, /* optinfo_flags */
4733 gate_pre, /* gate */
4734 do_pre, /* execute */
4735 NULL, /* sub */
4736 NULL, /* next */
4737 0, /* static_pass_number */
4738 TV_TREE_PRE, /* tv_id */
4739 PROP_no_crit_edges | PROP_cfg
4740 | PROP_ssa, /* properties_required */
4741 0, /* properties_provided */
4742 0, /* properties_destroyed */
4743 TODO_rebuild_alias, /* todo_flags_start */
4744 TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
4749 /* Gate and execute functions for FRE. */
4751 static unsigned int
4752 execute_fre (void)
4754 unsigned int todo = 0;
4756 if (!run_scc_vn (VN_WALKREWRITE))
4757 return 0;
4759 memset (&pre_stats, 0, sizeof (pre_stats));
4761 /* Remove all the redundant expressions. */
4762 todo |= eliminate ();
4764 todo |= fini_eliminate ();
4766 free_scc_vn ();
4768 statistics_counter_event (cfun, "Insertions", pre_stats.insertions);
4769 statistics_counter_event (cfun, "Eliminated", pre_stats.eliminations);
4770 statistics_counter_event (cfun, "Constified", pre_stats.constified);
4772 return todo;
4775 static bool
4776 gate_fre (void)
4778 return flag_tree_fre != 0;
4781 struct gimple_opt_pass pass_fre =
4784 GIMPLE_PASS,
4785 "fre", /* name */
4786 OPTGROUP_NONE, /* optinfo_flags */
4787 gate_fre, /* gate */
4788 execute_fre, /* execute */
4789 NULL, /* sub */
4790 NULL, /* next */
4791 0, /* static_pass_number */
4792 TV_TREE_FRE, /* tv_id */
4793 PROP_cfg | PROP_ssa, /* properties_required */
4794 0, /* properties_provided */
4795 0, /* properties_destroyed */
4796 0, /* todo_flags_start */
4797 TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */