2012-09-15 Tom de Vries <tom@codesourcery.com>
[official-gcc.git] / gcc / tree-ssa-pre.c
blobab9f2f467c307a0036d0d959f3ff4c5adfa8ee8a
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 T;
177 static inline hashval_t hash (const pre_expr_d *);
178 static inline int equal (const pre_expr_d *, const pre_expr_d *);
179 } *pre_expr;
181 #define PRE_EXPR_NAME(e) (e)->u.name
182 #define PRE_EXPR_NARY(e) (e)->u.nary
183 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
184 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
186 /* Compare E1 and E1 for equality. */
188 inline int
189 pre_expr_d::equal (const struct pre_expr_d *e1, const struct pre_expr_d *e2)
191 if (e1->kind != e2->kind)
192 return false;
194 switch (e1->kind)
196 case CONSTANT:
197 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
198 PRE_EXPR_CONSTANT (e2));
199 case NAME:
200 return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
201 case NARY:
202 return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
203 case REFERENCE:
204 return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
205 PRE_EXPR_REFERENCE (e2));
206 default:
207 gcc_unreachable ();
211 /* Hash E. */
213 inline hashval_t
214 pre_expr_d::hash (const struct pre_expr_d *e)
216 switch (e->kind)
218 case CONSTANT:
219 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
220 case NAME:
221 return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
222 case NARY:
223 return PRE_EXPR_NARY (e)->hashcode;
224 case REFERENCE:
225 return PRE_EXPR_REFERENCE (e)->hashcode;
226 default:
227 gcc_unreachable ();
231 /* Next global expression id number. */
232 static unsigned int next_expression_id;
234 /* Mapping from expression to id number we can use in bitmap sets. */
235 DEF_VEC_P (pre_expr);
236 DEF_VEC_ALLOC_P (pre_expr, heap);
237 static VEC(pre_expr, heap) *expressions;
238 static hash_table <pre_expr_d> expression_to_id;
239 static VEC(unsigned, heap) *name_to_id;
241 /* Allocate an expression id for EXPR. */
243 static inline unsigned int
244 alloc_expression_id (pre_expr expr)
246 struct pre_expr_d **slot;
247 /* Make sure we won't overflow. */
248 gcc_assert (next_expression_id + 1 > next_expression_id);
249 expr->id = next_expression_id++;
250 VEC_safe_push (pre_expr, heap, expressions, expr);
251 if (expr->kind == NAME)
253 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
254 /* VEC_safe_grow_cleared allocates no headroom. Avoid frequent
255 re-allocations by using VEC_reserve upfront. There is no
256 VEC_quick_grow_cleared unfortunately. */
257 unsigned old_len = VEC_length (unsigned, name_to_id);
258 VEC_reserve (unsigned, heap, name_to_id, num_ssa_names - old_len);
259 VEC_safe_grow_cleared (unsigned, heap, name_to_id, num_ssa_names);
260 gcc_assert (VEC_index (unsigned, name_to_id, version) == 0);
261 VEC_replace (unsigned, name_to_id, version, expr->id);
263 else
265 slot = expression_to_id.find_slot (expr, INSERT);
266 gcc_assert (!*slot);
267 *slot = expr;
269 return next_expression_id - 1;
272 /* Return the expression id for tree EXPR. */
274 static inline unsigned int
275 get_expression_id (const pre_expr expr)
277 return expr->id;
280 static inline unsigned int
281 lookup_expression_id (const pre_expr expr)
283 struct pre_expr_d **slot;
285 if (expr->kind == NAME)
287 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
288 if (VEC_length (unsigned, name_to_id) <= version)
289 return 0;
290 return VEC_index (unsigned, name_to_id, version);
292 else
294 slot = expression_to_id.find_slot (expr, NO_INSERT);
295 if (!slot)
296 return 0;
297 return ((pre_expr)*slot)->id;
301 /* Return the existing expression id for EXPR, or create one if one
302 does not exist yet. */
304 static inline unsigned int
305 get_or_alloc_expression_id (pre_expr expr)
307 unsigned int id = lookup_expression_id (expr);
308 if (id == 0)
309 return alloc_expression_id (expr);
310 return expr->id = id;
313 /* Return the expression that has expression id ID */
315 static inline pre_expr
316 expression_for_id (unsigned int id)
318 return VEC_index (pre_expr, expressions, id);
321 /* Free the expression id field in all of our expressions,
322 and then destroy the expressions array. */
324 static void
325 clear_expression_ids (void)
327 VEC_free (pre_expr, heap, expressions);
330 static alloc_pool pre_expr_pool;
332 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
334 static pre_expr
335 get_or_alloc_expr_for_name (tree name)
337 struct pre_expr_d expr;
338 pre_expr result;
339 unsigned int result_id;
341 expr.kind = NAME;
342 expr.id = 0;
343 PRE_EXPR_NAME (&expr) = name;
344 result_id = lookup_expression_id (&expr);
345 if (result_id != 0)
346 return expression_for_id (result_id);
348 result = (pre_expr) pool_alloc (pre_expr_pool);
349 result->kind = NAME;
350 PRE_EXPR_NAME (result) = name;
351 alloc_expression_id (result);
352 return result;
355 /* An unordered bitmap set. One bitmap tracks values, the other,
356 expressions. */
357 typedef struct bitmap_set
359 bitmap_head expressions;
360 bitmap_head values;
361 } *bitmap_set_t;
363 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
364 EXECUTE_IF_SET_IN_BITMAP(&(set)->expressions, 0, (id), (bi))
366 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
367 EXECUTE_IF_SET_IN_BITMAP(&(set)->values, 0, (id), (bi))
369 /* Mapping from value id to expressions with that value_id. */
370 static VEC(bitmap, heap) *value_expressions;
372 /* Sets that we need to keep track of. */
373 typedef struct bb_bitmap_sets
375 /* The EXP_GEN set, which represents expressions/values generated in
376 a basic block. */
377 bitmap_set_t exp_gen;
379 /* The PHI_GEN set, which represents PHI results generated in a
380 basic block. */
381 bitmap_set_t phi_gen;
383 /* The TMP_GEN set, which represents results/temporaries generated
384 in a basic block. IE the LHS of an expression. */
385 bitmap_set_t tmp_gen;
387 /* The AVAIL_OUT set, which represents which values are available in
388 a given basic block. */
389 bitmap_set_t avail_out;
391 /* The ANTIC_IN set, which represents which values are anticipatable
392 in a given basic block. */
393 bitmap_set_t antic_in;
395 /* The PA_IN set, which represents which values are
396 partially anticipatable in a given basic block. */
397 bitmap_set_t pa_in;
399 /* The NEW_SETS set, which is used during insertion to augment the
400 AVAIL_OUT set of blocks with the new insertions performed during
401 the current iteration. */
402 bitmap_set_t new_sets;
404 /* A cache for value_dies_in_block_x. */
405 bitmap expr_dies;
407 /* True if we have visited this block during ANTIC calculation. */
408 unsigned int visited : 1;
410 /* True we have deferred processing this block during ANTIC
411 calculation until its successor is processed. */
412 unsigned int deferred : 1;
414 /* True when the block contains a call that might not return. */
415 unsigned int contains_may_not_return_call : 1;
416 } *bb_value_sets_t;
418 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
419 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
420 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
421 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
422 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
423 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
424 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
425 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
426 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
427 #define BB_DEFERRED(BB) ((bb_value_sets_t) ((BB)->aux))->deferred
428 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
431 /* Basic block list in postorder. */
432 static int *postorder;
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, gimple);
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 gimple, tree);
467 static tree find_or_generate_expression (basic_block, pre_expr, gimple_seq *,
468 gimple);
469 static unsigned int get_expr_value_id (pre_expr);
471 /* We can add and remove elements and entries to and from sets
472 and hash tables, so we use alloc pools for them. */
474 static alloc_pool bitmap_set_pool;
475 static bitmap_obstack grand_bitmap_obstack;
477 /* Set of blocks with statements that have had their EH properties changed. */
478 static bitmap need_eh_cleanup;
480 /* Set of blocks with statements that have had their AB properties changed. */
481 static bitmap need_ab_cleanup;
483 /* A three tuple {e, pred, v} used to cache phi translations in the
484 phi_translate_table. */
486 typedef struct expr_pred_trans_d : typed_free_remove<expr_pred_trans_d>
488 /* The expression. */
489 pre_expr e;
491 /* The predecessor block along which we translated the expression. */
492 basic_block pred;
494 /* The value that resulted from the translation. */
495 pre_expr v;
497 /* The hashcode for the expression, pred pair. This is cached for
498 speed reasons. */
499 hashval_t hashcode;
501 /* hash_table support. */
502 typedef expr_pred_trans_d T;
503 static inline hashval_t hash (const expr_pred_trans_d *);
504 static inline int equal (const expr_pred_trans_d *, const expr_pred_trans_d *);
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 expr_pred_trans_d *ve1,
516 const expr_pred_trans_d *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 >= VEC_length (bitmap, value_expressions))
586 VEC_safe_grow_cleared (bitmap, heap, value_expressions, v + 1);
589 set = VEC_index (bitmap, value_expressions, v);
590 if (!set)
592 set = BITMAP_ALLOC (&grand_bitmap_obstack);
593 VEC_replace (bitmap, 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 switch (expr->kind)
617 case CONSTANT:
619 unsigned int id;
620 id = get_constant_value_id (PRE_EXPR_CONSTANT (expr));
621 if (id == 0)
623 id = get_or_alloc_constant_value_id (PRE_EXPR_CONSTANT (expr));
624 add_to_value (id, expr);
626 return id;
628 case NAME:
629 return VN_INFO (PRE_EXPR_NAME (expr))->value_id;
630 case NARY:
631 return PRE_EXPR_NARY (expr)->value_id;
632 case REFERENCE:
633 return PRE_EXPR_REFERENCE (expr)->value_id;
634 default:
635 gcc_unreachable ();
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 = VEC_index (bitmap, 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, heap) *
714 sorted_array_from_bitmap_set (bitmap_set_t set)
716 unsigned int i, j;
717 bitmap_iterator bi, bj;
718 VEC(pre_expr, heap) *result;
720 /* Pre-allocate roughly enough space for the array. */
721 result = VEC_alloc (pre_expr, heap, 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 = VEC_index (bitmap, value_expressions, i);
736 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj)
738 if (bitmap_bit_p (&set->expressions, j))
739 VEC_safe_push (pre_expr, heap, result, 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 = VEC_index (bitmap, 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 VEC_iterate (vn_reference_op_s, ref->operands, 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 != VEC_length (vn_reference_op_s, ref->operands) - 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 = VEC_index (bitmap, 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 = VEC_index (bitmap, 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, heap) *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, NULL);
1343 if (!result && set2)
1344 result = bitmap_find_leader (set2, val, NULL);
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 = VEC_index (bitmap, 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 VEC_safe_grow_cleared (bitmap, heap,
1514 value_expressions,
1515 get_max_value_id() + 1);
1516 nary = vn_nary_op_insert_pieces (newnary->length,
1517 newnary->opcode,
1518 newnary->type,
1519 &newnary->op[0],
1520 result, new_val_id);
1521 PRE_EXPR_NARY (expr) = nary;
1522 constant = fully_constant_expression (expr);
1523 if (constant != expr)
1524 return constant;
1525 get_or_alloc_expression_id (expr);
1527 add_to_value (new_val_id, expr);
1529 return expr;
1531 break;
1533 case REFERENCE:
1535 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1536 VEC (vn_reference_op_s, heap) *operands = ref->operands;
1537 tree vuse = ref->vuse;
1538 tree newvuse = vuse;
1539 VEC (vn_reference_op_s, heap) *newoperands = NULL;
1540 bool changed = false, same_valid = true;
1541 unsigned int i, j, n;
1542 vn_reference_op_t operand;
1543 vn_reference_t newref;
1545 for (i = 0, j = 0;
1546 VEC_iterate (vn_reference_op_s, operands, i, operand); i++, j++)
1548 pre_expr opresult;
1549 pre_expr leader;
1550 tree op[3];
1551 tree type = operand->type;
1552 vn_reference_op_s newop = *operand;
1553 op[0] = operand->op0;
1554 op[1] = operand->op1;
1555 op[2] = operand->op2;
1556 for (n = 0; n < 3; ++n)
1558 unsigned int op_val_id;
1559 if (!op[n])
1560 continue;
1561 if (TREE_CODE (op[n]) != SSA_NAME)
1563 /* We can't possibly insert these. */
1564 if (n != 0
1565 && !is_gimple_min_invariant (op[n]))
1566 break;
1567 continue;
1569 op_val_id = VN_INFO (op[n])->value_id;
1570 leader = find_leader_in_sets (op_val_id, set1, set2);
1571 if (!leader)
1572 break;
1573 /* Make sure we do not recursively translate ourselves
1574 like for translating a[n_1] with the leader for
1575 n_1 being a[n_1]. */
1576 if (get_expression_id (leader) != get_expression_id (expr))
1578 opresult = phi_translate (leader, set1, set2,
1579 pred, phiblock);
1580 if (!opresult)
1581 break;
1582 if (opresult != leader)
1584 tree name = get_representative_for (opresult);
1585 if (!name)
1586 break;
1587 changed |= name != op[n];
1588 op[n] = name;
1592 if (n != 3)
1594 if (newoperands)
1595 VEC_free (vn_reference_op_s, heap, newoperands);
1596 return NULL;
1598 if (!newoperands)
1599 newoperands = VEC_copy (vn_reference_op_s, heap, operands);
1600 /* We may have changed from an SSA_NAME to a constant */
1601 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1602 newop.opcode = TREE_CODE (op[0]);
1603 newop.type = type;
1604 newop.op0 = op[0];
1605 newop.op1 = op[1];
1606 newop.op2 = op[2];
1607 /* If it transforms a non-constant ARRAY_REF into a constant
1608 one, adjust the constant offset. */
1609 if (newop.opcode == ARRAY_REF
1610 && newop.off == -1
1611 && TREE_CODE (op[0]) == INTEGER_CST
1612 && TREE_CODE (op[1]) == INTEGER_CST
1613 && TREE_CODE (op[2]) == INTEGER_CST)
1615 double_int off = tree_to_double_int (op[0]);
1616 off += -tree_to_double_int (op[1]);
1617 off *= tree_to_double_int (op[2]);
1618 if (off.fits_shwi ())
1619 newop.off = off.low;
1621 VEC_replace (vn_reference_op_s, newoperands, j, newop);
1622 /* If it transforms from an SSA_NAME to an address, fold with
1623 a preceding indirect reference. */
1624 if (j > 0 && op[0] && TREE_CODE (op[0]) == ADDR_EXPR
1625 && VEC_index (vn_reference_op_s,
1626 newoperands, j - 1).opcode == MEM_REF)
1627 vn_reference_fold_indirect (&newoperands, &j);
1629 if (i != VEC_length (vn_reference_op_s, operands))
1631 if (newoperands)
1632 VEC_free (vn_reference_op_s, heap, newoperands);
1633 return NULL;
1636 if (vuse)
1638 newvuse = translate_vuse_through_block (newoperands,
1639 ref->set, ref->type,
1640 vuse, phiblock, pred,
1641 &same_valid);
1642 if (newvuse == NULL_TREE)
1644 VEC_free (vn_reference_op_s, heap, newoperands);
1645 return NULL;
1649 if (changed || newvuse != vuse)
1651 unsigned int new_val_id;
1652 pre_expr constant;
1654 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1655 ref->type,
1656 newoperands,
1657 &newref, VN_WALK);
1658 if (result)
1659 VEC_free (vn_reference_op_s, heap, newoperands);
1661 /* We can always insert constants, so if we have a partial
1662 redundant constant load of another type try to translate it
1663 to a constant of appropriate type. */
1664 if (result && is_gimple_min_invariant (result))
1666 tree tem = result;
1667 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1669 tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1670 if (tem && !is_gimple_min_invariant (tem))
1671 tem = NULL_TREE;
1673 if (tem)
1674 return get_or_alloc_expr_for_constant (tem);
1677 /* If we'd have to convert things we would need to validate
1678 if we can insert the translated expression. So fail
1679 here for now - we cannot insert an alias with a different
1680 type in the VN tables either, as that would assert. */
1681 if (result
1682 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1683 return NULL;
1684 else if (!result && newref
1685 && !useless_type_conversion_p (ref->type, newref->type))
1687 VEC_free (vn_reference_op_s, heap, newoperands);
1688 return NULL;
1691 expr = (pre_expr) pool_alloc (pre_expr_pool);
1692 expr->kind = REFERENCE;
1693 expr->id = 0;
1695 if (newref)
1697 PRE_EXPR_REFERENCE (expr) = newref;
1698 constant = fully_constant_expression (expr);
1699 if (constant != expr)
1700 return constant;
1702 new_val_id = newref->value_id;
1703 get_or_alloc_expression_id (expr);
1705 else
1707 if (changed || !same_valid)
1709 new_val_id = get_next_value_id ();
1710 VEC_safe_grow_cleared (bitmap, heap,
1711 value_expressions,
1712 get_max_value_id() + 1);
1714 else
1715 new_val_id = ref->value_id;
1716 newref = vn_reference_insert_pieces (newvuse, ref->set,
1717 ref->type,
1718 newoperands,
1719 result, new_val_id);
1720 newoperands = NULL;
1721 PRE_EXPR_REFERENCE (expr) = newref;
1722 constant = fully_constant_expression (expr);
1723 if (constant != expr)
1724 return constant;
1725 get_or_alloc_expression_id (expr);
1727 add_to_value (new_val_id, expr);
1729 VEC_free (vn_reference_op_s, heap, newoperands);
1730 return expr;
1732 break;
1734 case NAME:
1736 gimple phi = NULL;
1737 edge e;
1738 gimple def_stmt;
1739 tree name = PRE_EXPR_NAME (expr);
1741 def_stmt = SSA_NAME_DEF_STMT (name);
1742 if (gimple_code (def_stmt) == GIMPLE_PHI
1743 && gimple_bb (def_stmt) == phiblock)
1744 phi = def_stmt;
1745 else
1746 return expr;
1748 e = find_edge (pred, gimple_bb (phi));
1749 if (e)
1751 tree def = PHI_ARG_DEF (phi, e->dest_idx);
1752 pre_expr newexpr;
1754 if (TREE_CODE (def) == SSA_NAME)
1755 def = VN_INFO (def)->valnum;
1757 /* Handle constant. */
1758 if (is_gimple_min_invariant (def))
1759 return get_or_alloc_expr_for_constant (def);
1761 if (TREE_CODE (def) == SSA_NAME && ssa_undefined_value_p (def))
1762 return NULL;
1764 newexpr = get_or_alloc_expr_for_name (def);
1765 return newexpr;
1768 return expr;
1770 default:
1771 gcc_unreachable ();
1775 /* Wrapper around phi_translate_1 providing caching functionality. */
1777 static pre_expr
1778 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1779 basic_block pred, basic_block phiblock)
1781 pre_expr phitrans;
1783 if (!expr)
1784 return NULL;
1786 /* Constants contain no values that need translation. */
1787 if (expr->kind == CONSTANT)
1788 return expr;
1790 if (value_id_constant_p (get_expr_value_id (expr)))
1791 return expr;
1793 if (expr->kind != NAME)
1795 phitrans = phi_trans_lookup (expr, pred);
1796 if (phitrans)
1797 return phitrans;
1800 /* Translate. */
1801 phitrans = phi_translate_1 (expr, set1, set2, pred, phiblock);
1803 /* Don't add empty translations to the cache. Neither add
1804 translations of NAMEs as those are cheap to translate. */
1805 if (phitrans
1806 && expr->kind != NAME)
1807 phi_trans_add (expr, phitrans, pred);
1809 return phitrans;
1813 /* For each expression in SET, translate the values through phi nodes
1814 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1815 expressions in DEST. */
1817 static void
1818 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
1819 basic_block phiblock)
1821 VEC (pre_expr, heap) *exprs;
1822 pre_expr expr;
1823 int i;
1825 if (gimple_seq_empty_p (phi_nodes (phiblock)))
1827 bitmap_set_copy (dest, set);
1828 return;
1831 exprs = sorted_array_from_bitmap_set (set);
1832 FOR_EACH_VEC_ELT (pre_expr, exprs, i, expr)
1834 pre_expr translated;
1835 translated = phi_translate (expr, set, NULL, pred, phiblock);
1836 if (!translated)
1837 continue;
1839 /* We might end up with multiple expressions from SET being
1840 translated to the same value. In this case we do not want
1841 to retain the NARY or REFERENCE expression but prefer a NAME
1842 which would be the leader. */
1843 if (translated->kind == NAME)
1844 bitmap_value_replace_in_set (dest, translated);
1845 else
1846 bitmap_value_insert_into_set (dest, translated);
1848 VEC_free (pre_expr, heap, exprs);
1851 /* Find the leader for a value (i.e., the name representing that
1852 value) in a given set, and return it. If STMT is non-NULL it
1853 makes sure the defining statement for the leader dominates it.
1854 Return NULL if no leader is found. */
1856 static pre_expr
1857 bitmap_find_leader (bitmap_set_t set, unsigned int val, gimple stmt)
1859 if (value_id_constant_p (val))
1861 unsigned int i;
1862 bitmap_iterator bi;
1863 bitmap exprset = VEC_index (bitmap, value_expressions, val);
1865 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1867 pre_expr expr = expression_for_id (i);
1868 if (expr->kind == CONSTANT)
1869 return expr;
1872 if (bitmap_set_contains_value (set, val))
1874 /* Rather than walk the entire bitmap of expressions, and see
1875 whether any of them has the value we are looking for, we look
1876 at the reverse mapping, which tells us the set of expressions
1877 that have a given value (IE value->expressions with that
1878 value) and see if any of those expressions are in our set.
1879 The number of expressions per value is usually significantly
1880 less than the number of expressions in the set. In fact, for
1881 large testcases, doing it this way is roughly 5-10x faster
1882 than walking the bitmap.
1883 If this is somehow a significant lose for some cases, we can
1884 choose which set to walk based on which set is smaller. */
1885 unsigned int i;
1886 bitmap_iterator bi;
1887 bitmap exprset = VEC_index (bitmap, value_expressions, val);
1889 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1891 pre_expr val = expression_for_id (i);
1892 /* At the point where stmt is not null, there should always
1893 be an SSA_NAME first in the list of expressions. */
1894 if (stmt)
1896 gimple def_stmt = SSA_NAME_DEF_STMT (PRE_EXPR_NAME (val));
1897 if (gimple_code (def_stmt) != GIMPLE_PHI
1898 && gimple_bb (def_stmt) == gimple_bb (stmt)
1899 /* PRE insertions are at the end of the basic-block
1900 and have UID 0. */
1901 && (gimple_uid (def_stmt) == 0
1902 || gimple_uid (def_stmt) >= gimple_uid (stmt)))
1903 continue;
1905 return val;
1908 return NULL;
1911 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1912 BLOCK by seeing if it is not killed in the block. Note that we are
1913 only determining whether there is a store that kills it. Because
1914 of the order in which clean iterates over values, we are guaranteed
1915 that altered operands will have caused us to be eliminated from the
1916 ANTIC_IN set already. */
1918 static bool
1919 value_dies_in_block_x (pre_expr expr, basic_block block)
1921 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1922 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1923 gimple def;
1924 gimple_stmt_iterator gsi;
1925 unsigned id = get_expression_id (expr);
1926 bool res = false;
1927 ao_ref ref;
1929 if (!vuse)
1930 return false;
1932 /* Lookup a previously calculated result. */
1933 if (EXPR_DIES (block)
1934 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1935 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1937 /* A memory expression {e, VUSE} dies in the block if there is a
1938 statement that may clobber e. If, starting statement walk from the
1939 top of the basic block, a statement uses VUSE there can be no kill
1940 inbetween that use and the original statement that loaded {e, VUSE},
1941 so we can stop walking. */
1942 ref.base = NULL_TREE;
1943 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1945 tree def_vuse, def_vdef;
1946 def = gsi_stmt (gsi);
1947 def_vuse = gimple_vuse (def);
1948 def_vdef = gimple_vdef (def);
1950 /* Not a memory statement. */
1951 if (!def_vuse)
1952 continue;
1954 /* Not a may-def. */
1955 if (!def_vdef)
1957 /* A load with the same VUSE, we're done. */
1958 if (def_vuse == vuse)
1959 break;
1961 continue;
1964 /* Init ref only if we really need it. */
1965 if (ref.base == NULL_TREE
1966 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type,
1967 refx->operands))
1969 res = true;
1970 break;
1972 /* If the statement may clobber expr, it dies. */
1973 if (stmt_may_clobber_ref_p_1 (def, &ref))
1975 res = true;
1976 break;
1980 /* Remember the result. */
1981 if (!EXPR_DIES (block))
1982 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1983 bitmap_set_bit (EXPR_DIES (block), id * 2);
1984 if (res)
1985 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1987 return res;
1991 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1992 contains its value-id. */
1994 static bool
1995 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1997 if (op && TREE_CODE (op) == SSA_NAME)
1999 unsigned int value_id = VN_INFO (op)->value_id;
2000 if (!(bitmap_set_contains_value (set1, value_id)
2001 || (set2 && bitmap_set_contains_value (set2, value_id))))
2002 return false;
2004 return true;
2007 /* Determine if the expression EXPR is valid in SET1 U SET2.
2008 ONLY SET2 CAN BE NULL.
2009 This means that we have a leader for each part of the expression
2010 (if it consists of values), or the expression is an SSA_NAME.
2011 For loads/calls, we also see if the vuse is killed in this block. */
2013 static bool
2014 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr,
2015 basic_block block)
2017 switch (expr->kind)
2019 case NAME:
2020 return bitmap_set_contains_expr (AVAIL_OUT (block), expr);
2021 case NARY:
2023 unsigned int i;
2024 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2025 for (i = 0; i < nary->length; i++)
2026 if (!op_valid_in_sets (set1, set2, nary->op[i]))
2027 return false;
2028 return true;
2030 break;
2031 case REFERENCE:
2033 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2034 vn_reference_op_t vro;
2035 unsigned int i;
2037 FOR_EACH_VEC_ELT (vn_reference_op_s, ref->operands, i, vro)
2039 if (!op_valid_in_sets (set1, set2, vro->op0)
2040 || !op_valid_in_sets (set1, set2, vro->op1)
2041 || !op_valid_in_sets (set1, set2, vro->op2))
2042 return false;
2044 return true;
2046 default:
2047 gcc_unreachable ();
2051 /* Clean the set of expressions that are no longer valid in SET1 or
2052 SET2. This means expressions that are made up of values we have no
2053 leaders for in SET1 or SET2. This version is used for partial
2054 anticipation, which means it is not valid in either ANTIC_IN or
2055 PA_IN. */
2057 static void
2058 dependent_clean (bitmap_set_t set1, bitmap_set_t set2, basic_block block)
2060 VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (set1);
2061 pre_expr expr;
2062 int i;
2064 FOR_EACH_VEC_ELT (pre_expr, exprs, i, expr)
2066 if (!valid_in_sets (set1, set2, expr, block))
2067 bitmap_remove_from_set (set1, expr);
2069 VEC_free (pre_expr, heap, exprs);
2072 /* Clean the set of expressions that are no longer valid in SET. This
2073 means expressions that are made up of values we have no leaders for
2074 in SET. */
2076 static void
2077 clean (bitmap_set_t set, basic_block block)
2079 VEC (pre_expr, heap) *exprs = sorted_array_from_bitmap_set (set);
2080 pre_expr expr;
2081 int i;
2083 FOR_EACH_VEC_ELT (pre_expr, exprs, i, expr)
2085 if (!valid_in_sets (set, NULL, expr, block))
2086 bitmap_remove_from_set (set, expr);
2088 VEC_free (pre_expr, heap, exprs);
2091 /* Clean the set of expressions that are no longer valid in SET because
2092 they are clobbered in BLOCK or because they trap and may not be executed. */
2094 static void
2095 prune_clobbered_mems (bitmap_set_t set, basic_block block)
2097 bitmap_iterator bi;
2098 unsigned i;
2100 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2102 pre_expr expr = expression_for_id (i);
2103 if (expr->kind == REFERENCE)
2105 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2106 if (ref->vuse)
2108 gimple def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2109 if (!gimple_nop_p (def_stmt)
2110 && ((gimple_bb (def_stmt) != block
2111 && !dominated_by_p (CDI_DOMINATORS,
2112 block, gimple_bb (def_stmt)))
2113 || (gimple_bb (def_stmt) == block
2114 && value_dies_in_block_x (expr, block))))
2115 bitmap_remove_from_set (set, expr);
2118 else if (expr->kind == NARY)
2120 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2121 /* If the NARY may trap make sure the block does not contain
2122 a possible exit point.
2123 ??? This is overly conservative if we translate AVAIL_OUT
2124 as the available expression might be after the exit point. */
2125 if (BB_MAY_NOTRETURN (block)
2126 && vn_nary_may_trap (nary))
2127 bitmap_remove_from_set (set, expr);
2132 static sbitmap has_abnormal_preds;
2134 /* List of blocks that may have changed during ANTIC computation and
2135 thus need to be iterated over. */
2137 static sbitmap changed_blocks;
2139 /* Decide whether to defer a block for a later iteration, or PHI
2140 translate SOURCE to DEST using phis in PHIBLOCK. Return false if we
2141 should defer the block, and true if we processed it. */
2143 static bool
2144 defer_or_phi_translate_block (bitmap_set_t dest, bitmap_set_t source,
2145 basic_block block, basic_block phiblock)
2147 if (!BB_VISITED (phiblock))
2149 SET_BIT (changed_blocks, block->index);
2150 BB_VISITED (block) = 0;
2151 BB_DEFERRED (block) = 1;
2152 return false;
2154 else
2155 phi_translate_set (dest, source, block, phiblock);
2156 return true;
2159 /* Compute the ANTIC set for BLOCK.
2161 If succs(BLOCK) > 1 then
2162 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2163 else if succs(BLOCK) == 1 then
2164 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2166 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2169 static bool
2170 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2172 bool changed = false;
2173 bitmap_set_t S, old, ANTIC_OUT;
2174 bitmap_iterator bi;
2175 unsigned int bii;
2176 edge e;
2177 edge_iterator ei;
2179 old = ANTIC_OUT = S = NULL;
2180 BB_VISITED (block) = 1;
2182 /* If any edges from predecessors are abnormal, antic_in is empty,
2183 so do nothing. */
2184 if (block_has_abnormal_pred_edge)
2185 goto maybe_dump_sets;
2187 old = ANTIC_IN (block);
2188 ANTIC_OUT = bitmap_set_new ();
2190 /* If the block has no successors, ANTIC_OUT is empty. */
2191 if (EDGE_COUNT (block->succs) == 0)
2193 /* If we have one successor, we could have some phi nodes to
2194 translate through. */
2195 else if (single_succ_p (block))
2197 basic_block succ_bb = single_succ (block);
2199 /* We trade iterations of the dataflow equations for having to
2200 phi translate the maximal set, which is incredibly slow
2201 (since the maximal set often has 300+ members, even when you
2202 have a small number of blocks).
2203 Basically, we defer the computation of ANTIC for this block
2204 until we have processed it's successor, which will inevitably
2205 have a *much* smaller set of values to phi translate once
2206 clean has been run on it.
2207 The cost of doing this is that we technically perform more
2208 iterations, however, they are lower cost iterations.
2210 Timings for PRE on tramp3d-v4:
2211 without maximal set fix: 11 seconds
2212 with maximal set fix/without deferring: 26 seconds
2213 with maximal set fix/with deferring: 11 seconds
2216 if (!defer_or_phi_translate_block (ANTIC_OUT, ANTIC_IN (succ_bb),
2217 block, succ_bb))
2219 changed = true;
2220 goto maybe_dump_sets;
2223 /* If we have multiple successors, we take the intersection of all of
2224 them. Note that in the case of loop exit phi nodes, we may have
2225 phis to translate through. */
2226 else
2228 VEC(basic_block, heap) * worklist;
2229 size_t i;
2230 basic_block bprime, first = NULL;
2232 worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
2233 FOR_EACH_EDGE (e, ei, block->succs)
2235 if (!first
2236 && BB_VISITED (e->dest))
2237 first = e->dest;
2238 else if (BB_VISITED (e->dest))
2239 VEC_quick_push (basic_block, worklist, e->dest);
2242 /* Of multiple successors we have to have visited one already. */
2243 if (!first)
2245 SET_BIT (changed_blocks, block->index);
2246 BB_VISITED (block) = 0;
2247 BB_DEFERRED (block) = 1;
2248 changed = true;
2249 VEC_free (basic_block, heap, worklist);
2250 goto maybe_dump_sets;
2253 if (!gimple_seq_empty_p (phi_nodes (first)))
2254 phi_translate_set (ANTIC_OUT, ANTIC_IN (first), block, first);
2255 else
2256 bitmap_set_copy (ANTIC_OUT, ANTIC_IN (first));
2258 FOR_EACH_VEC_ELT (basic_block, worklist, i, bprime)
2260 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2262 bitmap_set_t tmp = bitmap_set_new ();
2263 phi_translate_set (tmp, ANTIC_IN (bprime), block, bprime);
2264 bitmap_set_and (ANTIC_OUT, tmp);
2265 bitmap_set_free (tmp);
2267 else
2268 bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime));
2270 VEC_free (basic_block, heap, worklist);
2273 /* Prune expressions that are clobbered in block and thus become
2274 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2275 prune_clobbered_mems (ANTIC_OUT, block);
2277 /* Generate ANTIC_OUT - TMP_GEN. */
2278 S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block));
2280 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2281 ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block),
2282 TMP_GEN (block));
2284 /* Then union in the ANTIC_OUT - TMP_GEN values,
2285 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2286 FOR_EACH_EXPR_ID_IN_SET (S, bii, bi)
2287 bitmap_value_insert_into_set (ANTIC_IN (block),
2288 expression_for_id (bii));
2290 clean (ANTIC_IN (block), block);
2292 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2294 changed = true;
2295 SET_BIT (changed_blocks, block->index);
2296 FOR_EACH_EDGE (e, ei, block->preds)
2297 SET_BIT (changed_blocks, e->src->index);
2299 else
2300 RESET_BIT (changed_blocks, block->index);
2302 maybe_dump_sets:
2303 if (dump_file && (dump_flags & TDF_DETAILS))
2305 if (!BB_DEFERRED (block) || BB_VISITED (block))
2307 if (ANTIC_OUT)
2308 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2310 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2311 block->index);
2313 if (S)
2314 print_bitmap_set (dump_file, S, "S", block->index);
2316 else
2318 fprintf (dump_file,
2319 "Block %d was deferred for a future iteration.\n",
2320 block->index);
2323 if (old)
2324 bitmap_set_free (old);
2325 if (S)
2326 bitmap_set_free (S);
2327 if (ANTIC_OUT)
2328 bitmap_set_free (ANTIC_OUT);
2329 return changed;
2332 /* Compute PARTIAL_ANTIC for BLOCK.
2334 If succs(BLOCK) > 1 then
2335 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2336 in ANTIC_OUT for all succ(BLOCK)
2337 else if succs(BLOCK) == 1 then
2338 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2340 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2341 - ANTIC_IN[BLOCK])
2344 static bool
2345 compute_partial_antic_aux (basic_block block,
2346 bool block_has_abnormal_pred_edge)
2348 bool changed = false;
2349 bitmap_set_t old_PA_IN;
2350 bitmap_set_t PA_OUT;
2351 edge e;
2352 edge_iterator ei;
2353 unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH);
2355 old_PA_IN = PA_OUT = NULL;
2357 /* If any edges from predecessors are abnormal, antic_in is empty,
2358 so do nothing. */
2359 if (block_has_abnormal_pred_edge)
2360 goto maybe_dump_sets;
2362 /* If there are too many partially anticipatable values in the
2363 block, phi_translate_set can take an exponential time: stop
2364 before the translation starts. */
2365 if (max_pa
2366 && single_succ_p (block)
2367 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2368 goto maybe_dump_sets;
2370 old_PA_IN = PA_IN (block);
2371 PA_OUT = bitmap_set_new ();
2373 /* If the block has no successors, ANTIC_OUT is empty. */
2374 if (EDGE_COUNT (block->succs) == 0)
2376 /* If we have one successor, we could have some phi nodes to
2377 translate through. Note that we can't phi translate across DFS
2378 back edges in partial antic, because it uses a union operation on
2379 the successors. For recurrences like IV's, we will end up
2380 generating a new value in the set on each go around (i + 3 (VH.1)
2381 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2382 else if (single_succ_p (block))
2384 basic_block succ = single_succ (block);
2385 if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK))
2386 phi_translate_set (PA_OUT, PA_IN (succ), block, succ);
2388 /* If we have multiple successors, we take the union of all of
2389 them. */
2390 else
2392 VEC(basic_block, heap) * worklist;
2393 size_t i;
2394 basic_block bprime;
2396 worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
2397 FOR_EACH_EDGE (e, ei, block->succs)
2399 if (e->flags & EDGE_DFS_BACK)
2400 continue;
2401 VEC_quick_push (basic_block, worklist, e->dest);
2403 if (VEC_length (basic_block, worklist) > 0)
2405 FOR_EACH_VEC_ELT (basic_block, worklist, i, bprime)
2407 unsigned int i;
2408 bitmap_iterator bi;
2410 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi)
2411 bitmap_value_insert_into_set (PA_OUT,
2412 expression_for_id (i));
2413 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2415 bitmap_set_t pa_in = bitmap_set_new ();
2416 phi_translate_set (pa_in, PA_IN (bprime), block, bprime);
2417 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2418 bitmap_value_insert_into_set (PA_OUT,
2419 expression_for_id (i));
2420 bitmap_set_free (pa_in);
2422 else
2423 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi)
2424 bitmap_value_insert_into_set (PA_OUT,
2425 expression_for_id (i));
2428 VEC_free (basic_block, heap, worklist);
2431 /* Prune expressions that are clobbered in block and thus become
2432 invalid if translated from PA_OUT to PA_IN. */
2433 prune_clobbered_mems (PA_OUT, block);
2435 /* PA_IN starts with PA_OUT - TMP_GEN.
2436 Then we subtract things from ANTIC_IN. */
2437 PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block));
2439 /* For partial antic, we want to put back in the phi results, since
2440 we will properly avoid making them partially antic over backedges. */
2441 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2442 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2444 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2445 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2447 dependent_clean (PA_IN (block), ANTIC_IN (block), block);
2449 if (!bitmap_set_equal (old_PA_IN, PA_IN (block)))
2451 changed = true;
2452 SET_BIT (changed_blocks, block->index);
2453 FOR_EACH_EDGE (e, ei, block->preds)
2454 SET_BIT (changed_blocks, e->src->index);
2456 else
2457 RESET_BIT (changed_blocks, block->index);
2459 maybe_dump_sets:
2460 if (dump_file && (dump_flags & TDF_DETAILS))
2462 if (PA_OUT)
2463 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2465 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2467 if (old_PA_IN)
2468 bitmap_set_free (old_PA_IN);
2469 if (PA_OUT)
2470 bitmap_set_free (PA_OUT);
2471 return changed;
2474 /* Compute ANTIC and partial ANTIC sets. */
2476 static void
2477 compute_antic (void)
2479 bool changed = true;
2480 int num_iterations = 0;
2481 basic_block block;
2482 int i;
2484 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2485 We pre-build the map of blocks with incoming abnormal edges here. */
2486 has_abnormal_preds = sbitmap_alloc (last_basic_block);
2487 sbitmap_zero (has_abnormal_preds);
2489 FOR_EACH_BB (block)
2491 edge_iterator ei;
2492 edge e;
2494 FOR_EACH_EDGE (e, ei, block->preds)
2496 e->flags &= ~EDGE_DFS_BACK;
2497 if (e->flags & EDGE_ABNORMAL)
2499 SET_BIT (has_abnormal_preds, block->index);
2500 break;
2504 BB_VISITED (block) = 0;
2505 BB_DEFERRED (block) = 0;
2507 /* While we are here, give empty ANTIC_IN sets to each block. */
2508 ANTIC_IN (block) = bitmap_set_new ();
2509 PA_IN (block) = bitmap_set_new ();
2512 /* At the exit block we anticipate nothing. */
2513 ANTIC_IN (EXIT_BLOCK_PTR) = bitmap_set_new ();
2514 BB_VISITED (EXIT_BLOCK_PTR) = 1;
2515 PA_IN (EXIT_BLOCK_PTR) = bitmap_set_new ();
2517 changed_blocks = sbitmap_alloc (last_basic_block + 1);
2518 sbitmap_ones (changed_blocks);
2519 while (changed)
2521 if (dump_file && (dump_flags & TDF_DETAILS))
2522 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2523 /* ??? We need to clear our PHI translation cache here as the
2524 ANTIC sets shrink and we restrict valid translations to
2525 those having operands with leaders in ANTIC. Same below
2526 for PA ANTIC computation. */
2527 num_iterations++;
2528 changed = false;
2529 for (i = n_basic_blocks - NUM_FIXED_BLOCKS - 1; i >= 0; i--)
2531 if (TEST_BIT (changed_blocks, postorder[i]))
2533 basic_block block = BASIC_BLOCK (postorder[i]);
2534 changed |= compute_antic_aux (block,
2535 TEST_BIT (has_abnormal_preds,
2536 block->index));
2539 /* Theoretically possible, but *highly* unlikely. */
2540 gcc_checking_assert (num_iterations < 500);
2543 statistics_histogram_event (cfun, "compute_antic iterations",
2544 num_iterations);
2546 if (do_partial_partial)
2548 sbitmap_ones (changed_blocks);
2549 mark_dfs_back_edges ();
2550 num_iterations = 0;
2551 changed = true;
2552 while (changed)
2554 if (dump_file && (dump_flags & TDF_DETAILS))
2555 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2556 num_iterations++;
2557 changed = false;
2558 for (i = n_basic_blocks - NUM_FIXED_BLOCKS - 1 ; i >= 0; i--)
2560 if (TEST_BIT (changed_blocks, postorder[i]))
2562 basic_block block = BASIC_BLOCK (postorder[i]);
2563 changed
2564 |= compute_partial_antic_aux (block,
2565 TEST_BIT (has_abnormal_preds,
2566 block->index));
2569 /* Theoretically possible, but *highly* unlikely. */
2570 gcc_checking_assert (num_iterations < 500);
2572 statistics_histogram_event (cfun, "compute_partial_antic iterations",
2573 num_iterations);
2575 sbitmap_free (has_abnormal_preds);
2576 sbitmap_free (changed_blocks);
2580 /* Inserted expressions are placed onto this worklist, which is used
2581 for performing quick dead code elimination of insertions we made
2582 that didn't turn out to be necessary. */
2583 static bitmap inserted_exprs;
2585 /* The actual worker for create_component_ref_by_pieces. */
2587 static tree
2588 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2589 unsigned int *operand, gimple_seq *stmts,
2590 gimple domstmt)
2592 vn_reference_op_t currop = &VEC_index (vn_reference_op_s, ref->operands,
2593 *operand);
2594 tree genop;
2595 ++*operand;
2596 switch (currop->opcode)
2598 case CALL_EXPR:
2600 tree folded, sc = NULL_TREE;
2601 unsigned int nargs = 0;
2602 tree fn, *args;
2603 if (TREE_CODE (currop->op0) == FUNCTION_DECL)
2604 fn = currop->op0;
2605 else
2607 pre_expr op0 = get_or_alloc_expr_for (currop->op0);
2608 fn = find_or_generate_expression (block, op0, stmts, domstmt);
2609 if (!fn)
2610 return NULL_TREE;
2612 if (currop->op1)
2614 pre_expr scexpr = get_or_alloc_expr_for (currop->op1);
2615 sc = find_or_generate_expression (block, scexpr, stmts, domstmt);
2616 if (!sc)
2617 return NULL_TREE;
2619 args = XNEWVEC (tree, VEC_length (vn_reference_op_s,
2620 ref->operands) - 1);
2621 while (*operand < VEC_length (vn_reference_op_s, ref->operands))
2623 args[nargs] = create_component_ref_by_pieces_1 (block, ref,
2624 operand, stmts,
2625 domstmt);
2626 if (!args[nargs])
2628 free (args);
2629 return NULL_TREE;
2631 nargs++;
2633 folded = build_call_array (currop->type,
2634 (TREE_CODE (fn) == FUNCTION_DECL
2635 ? build_fold_addr_expr (fn) : fn),
2636 nargs, args);
2637 free (args);
2638 if (sc)
2639 CALL_EXPR_STATIC_CHAIN (folded) = sc;
2640 return folded;
2643 case MEM_REF:
2645 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2646 stmts, domstmt);
2647 tree offset = currop->op0;
2648 if (!baseop)
2649 return NULL_TREE;
2650 if (TREE_CODE (baseop) == ADDR_EXPR
2651 && handled_component_p (TREE_OPERAND (baseop, 0)))
2653 HOST_WIDE_INT off;
2654 tree base;
2655 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2656 &off);
2657 gcc_assert (base);
2658 offset = int_const_binop (PLUS_EXPR, offset,
2659 build_int_cst (TREE_TYPE (offset),
2660 off));
2661 baseop = build_fold_addr_expr (base);
2663 return fold_build2 (MEM_REF, currop->type, baseop, offset);
2666 case TARGET_MEM_REF:
2668 pre_expr op0expr, op1expr;
2669 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2670 vn_reference_op_t nextop = &VEC_index (vn_reference_op_s, ref->operands,
2671 ++*operand);
2672 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2673 stmts, domstmt);
2674 if (!baseop)
2675 return NULL_TREE;
2676 if (currop->op0)
2678 op0expr = get_or_alloc_expr_for (currop->op0);
2679 genop0 = find_or_generate_expression (block, op0expr,
2680 stmts, domstmt);
2681 if (!genop0)
2682 return NULL_TREE;
2684 if (nextop->op0)
2686 op1expr = get_or_alloc_expr_for (nextop->op0);
2687 genop1 = find_or_generate_expression (block, op1expr,
2688 stmts, domstmt);
2689 if (!genop1)
2690 return NULL_TREE;
2692 return build5 (TARGET_MEM_REF, currop->type,
2693 baseop, currop->op2, genop0, currop->op1, genop1);
2696 case ADDR_EXPR:
2697 if (currop->op0)
2699 gcc_assert (is_gimple_min_invariant (currop->op0));
2700 return currop->op0;
2702 /* Fallthrough. */
2703 case REALPART_EXPR:
2704 case IMAGPART_EXPR:
2705 case VIEW_CONVERT_EXPR:
2707 tree genop0 = create_component_ref_by_pieces_1 (block, ref,
2708 operand,
2709 stmts, domstmt);
2710 if (!genop0)
2711 return NULL_TREE;
2713 return fold_build1 (currop->opcode, currop->type, genop0);
2716 case WITH_SIZE_EXPR:
2718 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2719 stmts, domstmt);
2720 pre_expr op1expr = get_or_alloc_expr_for (currop->op0);
2721 tree genop1;
2723 if (!genop0)
2724 return NULL_TREE;
2726 genop1 = find_or_generate_expression (block, op1expr, stmts, domstmt);
2727 if (!genop1)
2728 return NULL_TREE;
2730 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2733 case BIT_FIELD_REF:
2735 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2736 stmts, domstmt);
2737 tree op1 = currop->op0;
2738 tree op2 = currop->op1;
2740 if (!genop0)
2741 return NULL_TREE;
2743 return fold_build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2746 /* For array ref vn_reference_op's, operand 1 of the array ref
2747 is op0 of the reference op and operand 3 of the array ref is
2748 op1. */
2749 case ARRAY_RANGE_REF:
2750 case ARRAY_REF:
2752 tree genop0;
2753 tree genop1 = currop->op0;
2754 pre_expr op1expr;
2755 tree genop2 = currop->op1;
2756 pre_expr op2expr;
2757 tree genop3 = currop->op2;
2758 pre_expr op3expr;
2759 genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2760 stmts, domstmt);
2761 if (!genop0)
2762 return NULL_TREE;
2763 op1expr = get_or_alloc_expr_for (genop1);
2764 genop1 = find_or_generate_expression (block, op1expr, stmts, domstmt);
2765 if (!genop1)
2766 return NULL_TREE;
2767 if (genop2)
2769 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2770 /* Drop zero minimum index if redundant. */
2771 if (integer_zerop (genop2)
2772 && (!domain_type
2773 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2774 genop2 = NULL_TREE;
2775 else
2777 op2expr = get_or_alloc_expr_for (genop2);
2778 genop2 = find_or_generate_expression (block, op2expr, stmts,
2779 domstmt);
2780 if (!genop2)
2781 return NULL_TREE;
2784 if (genop3)
2786 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2787 /* We can't always put a size in units of the element alignment
2788 here as the element alignment may be not visible. See
2789 PR43783. Simply drop the element size for constant
2790 sizes. */
2791 if (tree_int_cst_equal (genop3, TYPE_SIZE_UNIT (elmt_type)))
2792 genop3 = NULL_TREE;
2793 else
2795 genop3 = size_binop (EXACT_DIV_EXPR, genop3,
2796 size_int (TYPE_ALIGN_UNIT (elmt_type)));
2797 op3expr = get_or_alloc_expr_for (genop3);
2798 genop3 = find_or_generate_expression (block, op3expr, stmts,
2799 domstmt);
2800 if (!genop3)
2801 return NULL_TREE;
2804 return build4 (currop->opcode, currop->type, genop0, genop1,
2805 genop2, genop3);
2807 case COMPONENT_REF:
2809 tree op0;
2810 tree op1;
2811 tree genop2 = currop->op1;
2812 pre_expr op2expr;
2813 op0 = create_component_ref_by_pieces_1 (block, ref, operand,
2814 stmts, domstmt);
2815 if (!op0)
2816 return NULL_TREE;
2817 /* op1 should be a FIELD_DECL, which are represented by
2818 themselves. */
2819 op1 = currop->op0;
2820 if (genop2)
2822 op2expr = get_or_alloc_expr_for (genop2);
2823 genop2 = find_or_generate_expression (block, op2expr, stmts,
2824 domstmt);
2825 if (!genop2)
2826 return NULL_TREE;
2829 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2832 case SSA_NAME:
2834 pre_expr op0expr = get_or_alloc_expr_for (currop->op0);
2835 genop = find_or_generate_expression (block, op0expr, stmts, domstmt);
2836 return genop;
2838 case STRING_CST:
2839 case INTEGER_CST:
2840 case COMPLEX_CST:
2841 case VECTOR_CST:
2842 case REAL_CST:
2843 case CONSTRUCTOR:
2844 case VAR_DECL:
2845 case PARM_DECL:
2846 case CONST_DECL:
2847 case RESULT_DECL:
2848 case FUNCTION_DECL:
2849 return currop->op0;
2851 default:
2852 gcc_unreachable ();
2856 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2857 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2858 trying to rename aggregates into ssa form directly, which is a no no.
2860 Thus, this routine doesn't create temporaries, it just builds a
2861 single access expression for the array, calling
2862 find_or_generate_expression to build the innermost pieces.
2864 This function is a subroutine of create_expression_by_pieces, and
2865 should not be called on it's own unless you really know what you
2866 are doing. */
2868 static tree
2869 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2870 gimple_seq *stmts, gimple domstmt)
2872 unsigned int op = 0;
2873 return create_component_ref_by_pieces_1 (block, ref, &op, stmts, domstmt);
2876 /* Find a leader for an expression, or generate one using
2877 create_expression_by_pieces if it's ANTIC but
2878 complex.
2879 BLOCK is the basic_block we are looking for leaders in.
2880 EXPR is the expression to find a leader or generate for.
2881 STMTS is the statement list to put the inserted expressions on.
2882 Returns the SSA_NAME of the LHS of the generated expression or the
2883 leader.
2884 DOMSTMT if non-NULL is a statement that should be dominated by
2885 all uses in the generated expression. If DOMSTMT is non-NULL this
2886 routine can fail and return NULL_TREE. Otherwise it will assert
2887 on failure. */
2889 static tree
2890 find_or_generate_expression (basic_block block, pre_expr expr,
2891 gimple_seq *stmts, gimple domstmt)
2893 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block),
2894 get_expr_value_id (expr), domstmt);
2895 tree genop = NULL;
2896 if (leader)
2898 if (leader->kind == NAME)
2899 genop = PRE_EXPR_NAME (leader);
2900 else if (leader->kind == CONSTANT)
2901 genop = PRE_EXPR_CONSTANT (leader);
2904 /* If it's still NULL, it must be a complex expression, so generate
2905 it recursively. Not so if inserting expressions for values generated
2906 by SCCVN. */
2907 if (genop == NULL
2908 && !domstmt)
2910 bitmap exprset;
2911 unsigned int lookfor = get_expr_value_id (expr);
2912 bool handled = false;
2913 bitmap_iterator bi;
2914 unsigned int i;
2916 exprset = VEC_index (bitmap, value_expressions, lookfor);
2917 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2919 pre_expr temp = expression_for_id (i);
2920 if (temp->kind != NAME)
2922 handled = true;
2923 genop = create_expression_by_pieces (block, temp, stmts,
2924 domstmt,
2925 get_expr_type (expr));
2926 break;
2929 if (!handled && domstmt)
2930 return NULL_TREE;
2932 gcc_assert (handled);
2934 return genop;
2937 #define NECESSARY GF_PLF_1
2939 /* Create an expression in pieces, so that we can handle very complex
2940 expressions that may be ANTIC, but not necessary GIMPLE.
2941 BLOCK is the basic block the expression will be inserted into,
2942 EXPR is the expression to insert (in value form)
2943 STMTS is a statement list to append the necessary insertions into.
2945 This function will die if we hit some value that shouldn't be
2946 ANTIC but is (IE there is no leader for it, or its components).
2947 This function may also generate expressions that are themselves
2948 partially or fully redundant. Those that are will be either made
2949 fully redundant during the next iteration of insert (for partially
2950 redundant ones), or eliminated by eliminate (for fully redundant
2951 ones).
2953 If DOMSTMT is non-NULL then we make sure that all uses in the
2954 expressions dominate that statement. In this case the function
2955 can return NULL_TREE to signal failure. */
2957 static tree
2958 create_expression_by_pieces (basic_block block, pre_expr expr,
2959 gimple_seq *stmts, gimple domstmt, tree type)
2961 tree name;
2962 tree folded;
2963 gimple_seq forced_stmts = NULL;
2964 unsigned int value_id;
2965 gimple_stmt_iterator gsi;
2966 tree exprtype = type ? type : get_expr_type (expr);
2967 pre_expr nameexpr;
2968 gimple newstmt;
2970 switch (expr->kind)
2972 /* We may hit the NAME/CONSTANT case if we have to convert types
2973 that value numbering saw through. */
2974 case NAME:
2975 folded = PRE_EXPR_NAME (expr);
2976 break;
2977 case CONSTANT:
2978 folded = PRE_EXPR_CONSTANT (expr);
2979 break;
2980 case REFERENCE:
2982 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2983 folded = create_component_ref_by_pieces (block, ref, stmts, domstmt);
2985 break;
2986 case NARY:
2988 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2989 tree genop[4];
2990 unsigned i;
2991 for (i = 0; i < nary->length; ++i)
2993 pre_expr op = get_or_alloc_expr_for (nary->op[i]);
2994 genop[i] = find_or_generate_expression (block, op,
2995 stmts, domstmt);
2996 if (!genop[i])
2997 return NULL_TREE;
2998 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2999 may have conversions stripped. */
3000 if (nary->opcode == POINTER_PLUS_EXPR)
3002 if (i == 0)
3003 genop[i] = fold_convert (nary->type, genop[i]);
3004 else if (i == 1)
3005 genop[i] = convert_to_ptrofftype (genop[i]);
3007 else
3008 genop[i] = fold_convert (TREE_TYPE (nary->op[i]), genop[i]);
3010 if (nary->opcode == CONSTRUCTOR)
3012 VEC(constructor_elt,gc) *elts = NULL;
3013 for (i = 0; i < nary->length; ++i)
3014 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
3015 folded = build_constructor (nary->type, elts);
3017 else
3019 switch (nary->length)
3021 case 1:
3022 folded = fold_build1 (nary->opcode, nary->type,
3023 genop[0]);
3024 break;
3025 case 2:
3026 folded = fold_build2 (nary->opcode, nary->type,
3027 genop[0], genop[1]);
3028 break;
3029 case 3:
3030 folded = fold_build3 (nary->opcode, nary->type,
3031 genop[0], genop[1], genop[3]);
3032 break;
3033 default:
3034 gcc_unreachable ();
3038 break;
3039 default:
3040 return NULL_TREE;
3043 if (!useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
3044 folded = fold_convert (exprtype, folded);
3046 /* Force the generated expression to be a sequence of GIMPLE
3047 statements.
3048 We have to call unshare_expr because force_gimple_operand may
3049 modify the tree we pass to it. */
3050 folded = force_gimple_operand (unshare_expr (folded), &forced_stmts,
3051 false, NULL);
3053 /* If we have any intermediate expressions to the value sets, add them
3054 to the value sets and chain them in the instruction stream. */
3055 if (forced_stmts)
3057 gsi = gsi_start (forced_stmts);
3058 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3060 gimple stmt = gsi_stmt (gsi);
3061 tree forcedname = gimple_get_lhs (stmt);
3062 pre_expr nameexpr;
3064 if (TREE_CODE (forcedname) == SSA_NAME)
3066 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
3067 VN_INFO_GET (forcedname)->valnum = forcedname;
3068 VN_INFO (forcedname)->value_id = get_next_value_id ();
3069 nameexpr = get_or_alloc_expr_for_name (forcedname);
3070 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
3071 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3072 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3075 gimple_seq_add_seq (stmts, forced_stmts);
3078 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
3079 newstmt = gimple_build_assign (name, folded);
3080 gimple_set_plf (newstmt, NECESSARY, false);
3082 gimple_seq_add_stmt (stmts, newstmt);
3083 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (name));
3085 /* Fold the last statement. */
3086 gsi = gsi_last (*stmts);
3087 if (fold_stmt_inplace (&gsi))
3088 update_stmt (gsi_stmt (gsi));
3090 /* Add a value number to the temporary.
3091 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
3092 we are creating the expression by pieces, and this particular piece of
3093 the expression may have been represented. There is no harm in replacing
3094 here. */
3095 value_id = get_expr_value_id (expr);
3096 VN_INFO_GET (name)->value_id = value_id;
3097 VN_INFO (name)->valnum = sccvn_valnum_from_value_id (value_id);
3098 if (VN_INFO (name)->valnum == NULL_TREE)
3099 VN_INFO (name)->valnum = name;
3100 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
3101 nameexpr = get_or_alloc_expr_for_name (name);
3102 add_to_value (value_id, nameexpr);
3103 if (NEW_SETS (block))
3104 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3105 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3107 pre_stats.insertions++;
3108 if (dump_file && (dump_flags & TDF_DETAILS))
3110 fprintf (dump_file, "Inserted ");
3111 print_gimple_stmt (dump_file, newstmt, 0, 0);
3112 fprintf (dump_file, " in predecessor %d\n", block->index);
3115 return name;
3119 /* Returns true if we want to inhibit the insertions of PHI nodes
3120 for the given EXPR for basic block BB (a member of a loop).
3121 We want to do this, when we fear that the induction variable we
3122 create might inhibit vectorization. */
3124 static bool
3125 inhibit_phi_insertion (basic_block bb, pre_expr expr)
3127 vn_reference_t vr = PRE_EXPR_REFERENCE (expr);
3128 VEC (vn_reference_op_s, heap) *ops = vr->operands;
3129 vn_reference_op_t op;
3130 unsigned i;
3132 /* If we aren't going to vectorize we don't inhibit anything. */
3133 if (!flag_tree_vectorize)
3134 return false;
3136 /* Otherwise we inhibit the insertion when the address of the
3137 memory reference is a simple induction variable. In other
3138 cases the vectorizer won't do anything anyway (either it's
3139 loop invariant or a complicated expression). */
3140 FOR_EACH_VEC_ELT (vn_reference_op_s, ops, i, op)
3142 switch (op->opcode)
3144 case CALL_EXPR:
3145 /* Calls are not a problem. */
3146 return false;
3148 case ARRAY_REF:
3149 case ARRAY_RANGE_REF:
3150 if (TREE_CODE (op->op0) != SSA_NAME)
3151 break;
3152 /* Fallthru. */
3153 case SSA_NAME:
3155 basic_block defbb = gimple_bb (SSA_NAME_DEF_STMT (op->op0));
3156 affine_iv iv;
3157 /* Default defs are loop invariant. */
3158 if (!defbb)
3159 break;
3160 /* Defined outside this loop, also loop invariant. */
3161 if (!flow_bb_inside_loop_p (bb->loop_father, defbb))
3162 break;
3163 /* If it's a simple induction variable inhibit insertion,
3164 the vectorizer might be interested in this one. */
3165 if (simple_iv (bb->loop_father, bb->loop_father,
3166 op->op0, &iv, true))
3167 return true;
3168 /* No simple IV, vectorizer can't do anything, hence no
3169 reason to inhibit the transformation for this operand. */
3170 break;
3172 default:
3173 break;
3176 return false;
3179 /* Insert the to-be-made-available values of expression EXPRNUM for each
3180 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3181 merge the result with a phi node, given the same value number as
3182 NODE. Return true if we have inserted new stuff. */
3184 static bool
3185 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3186 VEC(pre_expr, heap) *avail)
3188 pre_expr expr = expression_for_id (exprnum);
3189 pre_expr newphi;
3190 unsigned int val = get_expr_value_id (expr);
3191 edge pred;
3192 bool insertions = false;
3193 bool nophi = false;
3194 basic_block bprime;
3195 pre_expr eprime;
3196 edge_iterator ei;
3197 tree type = get_expr_type (expr);
3198 tree temp;
3199 gimple phi;
3201 /* Make sure we aren't creating an induction variable. */
3202 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3204 bool firstinsideloop = false;
3205 bool secondinsideloop = false;
3206 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3207 EDGE_PRED (block, 0)->src);
3208 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3209 EDGE_PRED (block, 1)->src);
3210 /* Induction variables only have one edge inside the loop. */
3211 if ((firstinsideloop ^ secondinsideloop)
3212 && (expr->kind != REFERENCE
3213 || inhibit_phi_insertion (block, expr)))
3215 if (dump_file && (dump_flags & TDF_DETAILS))
3216 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3217 nophi = true;
3221 /* Make the necessary insertions. */
3222 FOR_EACH_EDGE (pred, ei, block->preds)
3224 gimple_seq stmts = NULL;
3225 tree builtexpr;
3226 bprime = pred->src;
3227 eprime = VEC_index (pre_expr, avail, pred->dest_idx);
3229 if (eprime->kind != NAME && eprime->kind != CONSTANT)
3231 builtexpr = create_expression_by_pieces (bprime,
3232 eprime,
3233 &stmts, NULL,
3234 type);
3235 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3236 gsi_insert_seq_on_edge (pred, stmts);
3237 VEC_replace (pre_expr, avail, pred->dest_idx,
3238 get_or_alloc_expr_for_name (builtexpr));
3239 insertions = true;
3241 else if (eprime->kind == CONSTANT)
3243 /* Constants may not have the right type, fold_convert
3244 should give us back a constant with the right type. */
3245 tree constant = PRE_EXPR_CONSTANT (eprime);
3246 if (!useless_type_conversion_p (type, TREE_TYPE (constant)))
3248 tree builtexpr = fold_convert (type, constant);
3249 if (!is_gimple_min_invariant (builtexpr))
3251 tree forcedexpr = force_gimple_operand (builtexpr,
3252 &stmts, true,
3253 NULL);
3254 if (!is_gimple_min_invariant (forcedexpr))
3256 if (forcedexpr != builtexpr)
3258 VN_INFO_GET (forcedexpr)->valnum = PRE_EXPR_CONSTANT (eprime);
3259 VN_INFO (forcedexpr)->value_id = get_expr_value_id (eprime);
3261 if (stmts)
3263 gimple_stmt_iterator gsi;
3264 gsi = gsi_start (stmts);
3265 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3267 gimple stmt = gsi_stmt (gsi);
3268 tree lhs = gimple_get_lhs (stmt);
3269 if (TREE_CODE (lhs) == SSA_NAME)
3270 bitmap_set_bit (inserted_exprs,
3271 SSA_NAME_VERSION (lhs));
3272 gimple_set_plf (stmt, NECESSARY, false);
3274 gsi_insert_seq_on_edge (pred, stmts);
3276 VEC_replace (pre_expr, avail, pred->dest_idx,
3277 get_or_alloc_expr_for_name (forcedexpr));
3280 else
3281 VEC_replace (pre_expr, avail, pred->dest_idx,
3282 get_or_alloc_expr_for_constant (builtexpr));
3285 else if (eprime->kind == NAME)
3287 /* We may have to do a conversion because our value
3288 numbering can look through types in certain cases, but
3289 our IL requires all operands of a phi node have the same
3290 type. */
3291 tree name = PRE_EXPR_NAME (eprime);
3292 if (!useless_type_conversion_p (type, TREE_TYPE (name)))
3294 tree builtexpr;
3295 tree forcedexpr;
3296 builtexpr = fold_convert (type, name);
3297 forcedexpr = force_gimple_operand (builtexpr,
3298 &stmts, true,
3299 NULL);
3301 if (forcedexpr != name)
3303 VN_INFO_GET (forcedexpr)->valnum = VN_INFO (name)->valnum;
3304 VN_INFO (forcedexpr)->value_id = VN_INFO (name)->value_id;
3307 if (stmts)
3309 gimple_stmt_iterator gsi;
3310 gsi = gsi_start (stmts);
3311 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3313 gimple stmt = gsi_stmt (gsi);
3314 tree lhs = gimple_get_lhs (stmt);
3315 if (TREE_CODE (lhs) == SSA_NAME)
3316 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
3317 gimple_set_plf (stmt, NECESSARY, false);
3319 gsi_insert_seq_on_edge (pred, stmts);
3321 VEC_replace (pre_expr, avail, pred->dest_idx,
3322 get_or_alloc_expr_for_name (forcedexpr));
3326 /* If we didn't want a phi node, and we made insertions, we still have
3327 inserted new stuff, and thus return true. If we didn't want a phi node,
3328 and didn't make insertions, we haven't added anything new, so return
3329 false. */
3330 if (nophi && insertions)
3331 return true;
3332 else if (nophi && !insertions)
3333 return false;
3335 /* Now build a phi for the new variable. */
3336 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3337 phi = create_phi_node (temp, block);
3339 gimple_set_plf (phi, NECESSARY, false);
3340 VN_INFO_GET (temp)->value_id = val;
3341 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3342 if (VN_INFO (temp)->valnum == NULL_TREE)
3343 VN_INFO (temp)->valnum = temp;
3344 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3345 FOR_EACH_EDGE (pred, ei, block->preds)
3347 pre_expr ae = VEC_index (pre_expr, avail, pred->dest_idx);
3348 gcc_assert (get_expr_type (ae) == type
3349 || useless_type_conversion_p (type, get_expr_type (ae)));
3350 if (ae->kind == CONSTANT)
3351 add_phi_arg (phi, PRE_EXPR_CONSTANT (ae), pred, UNKNOWN_LOCATION);
3352 else
3353 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3356 newphi = get_or_alloc_expr_for_name (temp);
3357 add_to_value (val, newphi);
3359 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3360 this insertion, since we test for the existence of this value in PHI_GEN
3361 before proceeding with the partial redundancy checks in insert_aux.
3363 The value may exist in AVAIL_OUT, in particular, it could be represented
3364 by the expression we are trying to eliminate, in which case we want the
3365 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3366 inserted there.
3368 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3369 this block, because if it did, it would have existed in our dominator's
3370 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3373 bitmap_insert_into_set (PHI_GEN (block), newphi);
3374 bitmap_value_replace_in_set (AVAIL_OUT (block),
3375 newphi);
3376 bitmap_insert_into_set (NEW_SETS (block),
3377 newphi);
3379 if (dump_file && (dump_flags & TDF_DETAILS))
3381 fprintf (dump_file, "Created phi ");
3382 print_gimple_stmt (dump_file, phi, 0, 0);
3383 fprintf (dump_file, " in block %d\n", block->index);
3385 pre_stats.phis++;
3386 return true;
3391 /* Perform insertion of partially redundant values.
3392 For BLOCK, do the following:
3393 1. Propagate the NEW_SETS of the dominator into the current block.
3394 If the block has multiple predecessors,
3395 2a. Iterate over the ANTIC expressions for the block to see if
3396 any of them are partially redundant.
3397 2b. If so, insert them into the necessary predecessors to make
3398 the expression fully redundant.
3399 2c. Insert a new PHI merging the values of the predecessors.
3400 2d. Insert the new PHI, and the new expressions, into the
3401 NEW_SETS set.
3402 3. Recursively call ourselves on the dominator children of BLOCK.
3404 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
3405 do_regular_insertion and do_partial_insertion.
3409 static bool
3410 do_regular_insertion (basic_block block, basic_block dom)
3412 bool new_stuff = false;
3413 VEC (pre_expr, heap) *exprs;
3414 pre_expr expr;
3415 VEC (pre_expr, heap) *avail = NULL;
3416 int i;
3418 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3419 VEC_safe_grow (pre_expr, heap, avail, EDGE_COUNT (block->preds));
3421 FOR_EACH_VEC_ELT (pre_expr, exprs, i, expr)
3423 if (expr->kind != NAME)
3425 unsigned int val;
3426 bool by_some = false;
3427 bool cant_insert = false;
3428 bool all_same = true;
3429 pre_expr first_s = NULL;
3430 edge pred;
3431 basic_block bprime;
3432 pre_expr eprime = NULL;
3433 edge_iterator ei;
3434 pre_expr edoubleprime = NULL;
3435 bool do_insertion = false;
3437 val = get_expr_value_id (expr);
3438 if (bitmap_set_contains_value (PHI_GEN (block), val))
3439 continue;
3440 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3442 if (dump_file && (dump_flags & TDF_DETAILS))
3443 fprintf (dump_file, "Found fully redundant value\n");
3444 continue;
3447 FOR_EACH_EDGE (pred, ei, block->preds)
3449 unsigned int vprime;
3451 /* We should never run insertion for the exit block
3452 and so not come across fake pred edges. */
3453 gcc_assert (!(pred->flags & EDGE_FAKE));
3454 bprime = pred->src;
3455 eprime = phi_translate (expr, ANTIC_IN (block), NULL,
3456 bprime, block);
3458 /* eprime will generally only be NULL if the
3459 value of the expression, translated
3460 through the PHI for this predecessor, is
3461 undefined. If that is the case, we can't
3462 make the expression fully redundant,
3463 because its value is undefined along a
3464 predecessor path. We can thus break out
3465 early because it doesn't matter what the
3466 rest of the results are. */
3467 if (eprime == NULL)
3469 VEC_replace (pre_expr, avail, pred->dest_idx, NULL);
3470 cant_insert = true;
3471 break;
3474 eprime = fully_constant_expression (eprime);
3475 vprime = get_expr_value_id (eprime);
3476 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3477 vprime, NULL);
3478 if (edoubleprime == NULL)
3480 VEC_replace (pre_expr, avail, pred->dest_idx, eprime);
3481 all_same = false;
3483 else
3485 VEC_replace (pre_expr, avail, pred->dest_idx, edoubleprime);
3486 by_some = true;
3487 /* We want to perform insertions to remove a redundancy on
3488 a path in the CFG we want to optimize for speed. */
3489 if (optimize_edge_for_speed_p (pred))
3490 do_insertion = true;
3491 if (first_s == NULL)
3492 first_s = edoubleprime;
3493 else if (!pre_expr_d::equal (first_s, edoubleprime))
3494 all_same = false;
3497 /* If we can insert it, it's not the same value
3498 already existing along every predecessor, and
3499 it's defined by some predecessor, it is
3500 partially redundant. */
3501 if (!cant_insert && !all_same && by_some)
3503 if (!do_insertion)
3505 if (dump_file && (dump_flags & TDF_DETAILS))
3507 fprintf (dump_file, "Skipping partial redundancy for "
3508 "expression ");
3509 print_pre_expr (dump_file, expr);
3510 fprintf (dump_file, " (%04d), no redundancy on to be "
3511 "optimized for speed edge\n", val);
3514 else if (dbg_cnt (treepre_insert))
3516 if (dump_file && (dump_flags & TDF_DETAILS))
3518 fprintf (dump_file, "Found partial redundancy for "
3519 "expression ");
3520 print_pre_expr (dump_file, expr);
3521 fprintf (dump_file, " (%04d)\n",
3522 get_expr_value_id (expr));
3524 if (insert_into_preds_of_block (block,
3525 get_expression_id (expr),
3526 avail))
3527 new_stuff = true;
3530 /* If all edges produce the same value and that value is
3531 an invariant, then the PHI has the same value on all
3532 edges. Note this. */
3533 else if (!cant_insert && all_same && eprime
3534 && (edoubleprime->kind == CONSTANT
3535 || edoubleprime->kind == NAME)
3536 && !value_id_constant_p (val))
3538 unsigned int j;
3539 bitmap_iterator bi;
3540 bitmap exprset = VEC_index (bitmap, value_expressions, val);
3542 unsigned int new_val = get_expr_value_id (edoubleprime);
3543 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bi)
3545 pre_expr expr = expression_for_id (j);
3547 if (expr->kind == NAME)
3549 vn_ssa_aux_t info = VN_INFO (PRE_EXPR_NAME (expr));
3550 /* Just reset the value id and valnum so it is
3551 the same as the constant we have discovered. */
3552 if (edoubleprime->kind == CONSTANT)
3554 info->valnum = PRE_EXPR_CONSTANT (edoubleprime);
3555 pre_stats.constified++;
3557 else
3558 info->valnum = VN_INFO (PRE_EXPR_NAME (edoubleprime))->valnum;
3559 info->value_id = new_val;
3566 VEC_free (pre_expr, heap, exprs);
3567 VEC_free (pre_expr, heap, avail);
3568 return new_stuff;
3572 /* Perform insertion for partially anticipatable expressions. There
3573 is only one case we will perform insertion for these. This case is
3574 if the expression is partially anticipatable, and fully available.
3575 In this case, we know that putting it earlier will enable us to
3576 remove the later computation. */
3579 static bool
3580 do_partial_partial_insertion (basic_block block, basic_block dom)
3582 bool new_stuff = false;
3583 VEC (pre_expr, heap) *exprs;
3584 pre_expr expr;
3585 VEC (pre_expr, heap) *avail = NULL;
3586 int i;
3588 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3589 VEC_safe_grow (pre_expr, heap, avail, EDGE_COUNT (block->preds));
3591 FOR_EACH_VEC_ELT (pre_expr, exprs, i, expr)
3593 if (expr->kind != NAME)
3595 unsigned int val;
3596 bool by_all = true;
3597 bool cant_insert = false;
3598 edge pred;
3599 basic_block bprime;
3600 pre_expr eprime = NULL;
3601 edge_iterator ei;
3603 val = get_expr_value_id (expr);
3604 if (bitmap_set_contains_value (PHI_GEN (block), val))
3605 continue;
3606 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3607 continue;
3609 FOR_EACH_EDGE (pred, ei, block->preds)
3611 unsigned int vprime;
3612 pre_expr edoubleprime;
3614 /* We should never run insertion for the exit block
3615 and so not come across fake pred edges. */
3616 gcc_assert (!(pred->flags & EDGE_FAKE));
3617 bprime = pred->src;
3618 eprime = phi_translate (expr, ANTIC_IN (block),
3619 PA_IN (block),
3620 bprime, block);
3622 /* eprime will generally only be NULL if the
3623 value of the expression, translated
3624 through the PHI for this predecessor, is
3625 undefined. If that is the case, we can't
3626 make the expression fully redundant,
3627 because its value is undefined along a
3628 predecessor path. We can thus break out
3629 early because it doesn't matter what the
3630 rest of the results are. */
3631 if (eprime == NULL)
3633 VEC_replace (pre_expr, avail, pred->dest_idx, NULL);
3634 cant_insert = true;
3635 break;
3638 eprime = fully_constant_expression (eprime);
3639 vprime = get_expr_value_id (eprime);
3640 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3641 vprime, NULL);
3642 VEC_replace (pre_expr, avail, pred->dest_idx, edoubleprime);
3643 if (edoubleprime == NULL)
3645 by_all = false;
3646 break;
3650 /* If we can insert it, it's not the same value
3651 already existing along every predecessor, and
3652 it's defined by some predecessor, it is
3653 partially redundant. */
3654 if (!cant_insert && by_all)
3656 edge succ;
3657 bool do_insertion = false;
3659 /* Insert only if we can remove a later expression on a path
3660 that we want to optimize for speed.
3661 The phi node that we will be inserting in BLOCK is not free,
3662 and inserting it for the sake of !optimize_for_speed successor
3663 may cause regressions on the speed path. */
3664 FOR_EACH_EDGE (succ, ei, block->succs)
3666 if (bitmap_set_contains_value (PA_IN (succ->dest), val))
3668 if (optimize_edge_for_speed_p (succ))
3669 do_insertion = true;
3673 if (!do_insertion)
3675 if (dump_file && (dump_flags & TDF_DETAILS))
3677 fprintf (dump_file, "Skipping partial partial redundancy "
3678 "for expression ");
3679 print_pre_expr (dump_file, expr);
3680 fprintf (dump_file, " (%04d), not partially anticipated "
3681 "on any to be optimized for speed edges\n", val);
3684 else if (dbg_cnt (treepre_insert))
3686 pre_stats.pa_insert++;
3687 if (dump_file && (dump_flags & TDF_DETAILS))
3689 fprintf (dump_file, "Found partial partial redundancy "
3690 "for expression ");
3691 print_pre_expr (dump_file, expr);
3692 fprintf (dump_file, " (%04d)\n",
3693 get_expr_value_id (expr));
3695 if (insert_into_preds_of_block (block,
3696 get_expression_id (expr),
3697 avail))
3698 new_stuff = true;
3704 VEC_free (pre_expr, heap, exprs);
3705 VEC_free (pre_expr, heap, avail);
3706 return new_stuff;
3709 static bool
3710 insert_aux (basic_block block)
3712 basic_block son;
3713 bool new_stuff = false;
3715 if (block)
3717 basic_block dom;
3718 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3719 if (dom)
3721 unsigned i;
3722 bitmap_iterator bi;
3723 bitmap_set_t newset = NEW_SETS (dom);
3724 if (newset)
3726 /* Note that we need to value_replace both NEW_SETS, and
3727 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3728 represented by some non-simple expression here that we want
3729 to replace it with. */
3730 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3732 pre_expr expr = expression_for_id (i);
3733 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3734 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3737 if (!single_pred_p (block))
3739 new_stuff |= do_regular_insertion (block, dom);
3740 if (do_partial_partial)
3741 new_stuff |= do_partial_partial_insertion (block, dom);
3745 for (son = first_dom_son (CDI_DOMINATORS, block);
3746 son;
3747 son = next_dom_son (CDI_DOMINATORS, son))
3749 new_stuff |= insert_aux (son);
3752 return new_stuff;
3755 /* Perform insertion of partially redundant values. */
3757 static void
3758 insert (void)
3760 bool new_stuff = true;
3761 basic_block bb;
3762 int num_iterations = 0;
3764 FOR_ALL_BB (bb)
3765 NEW_SETS (bb) = bitmap_set_new ();
3767 while (new_stuff)
3769 num_iterations++;
3770 if (dump_file && dump_flags & TDF_DETAILS)
3771 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3772 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
3774 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3778 /* Add OP to EXP_GEN (block), and possibly to the maximal set. */
3780 static void
3781 add_to_exp_gen (basic_block block, tree op)
3783 pre_expr result;
3785 if (TREE_CODE (op) == SSA_NAME && ssa_undefined_value_p (op))
3786 return;
3788 result = get_or_alloc_expr_for_name (op);
3789 bitmap_value_insert_into_set (EXP_GEN (block), result);
3792 /* Create value ids for PHI in BLOCK. */
3794 static void
3795 make_values_for_phi (gimple phi, basic_block block)
3797 tree result = gimple_phi_result (phi);
3798 unsigned i;
3800 /* We have no need for virtual phis, as they don't represent
3801 actual computations. */
3802 if (virtual_operand_p (result))
3803 return;
3805 pre_expr e = get_or_alloc_expr_for_name (result);
3806 add_to_value (get_expr_value_id (e), e);
3807 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3808 bitmap_insert_into_set (PHI_GEN (block), e);
3809 for (i = 0; i < gimple_phi_num_args (phi); ++i)
3811 tree arg = gimple_phi_arg_def (phi, i);
3812 if (TREE_CODE (arg) == SSA_NAME)
3814 e = get_or_alloc_expr_for_name (arg);
3815 add_to_value (get_expr_value_id (e), e);
3820 /* Compute the AVAIL set for all basic blocks.
3822 This function performs value numbering of the statements in each basic
3823 block. The AVAIL sets are built from information we glean while doing
3824 this value numbering, since the AVAIL sets contain only one entry per
3825 value.
3827 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3828 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3830 static void
3831 compute_avail (void)
3834 basic_block block, son;
3835 basic_block *worklist;
3836 size_t sp = 0;
3837 unsigned i;
3839 /* We pretend that default definitions are defined in the entry block.
3840 This includes function arguments and the static chain decl. */
3841 for (i = 1; i < num_ssa_names; ++i)
3843 tree name = ssa_name (i);
3844 pre_expr e;
3845 if (!name
3846 || !SSA_NAME_IS_DEFAULT_DEF (name)
3847 || has_zero_uses (name)
3848 || virtual_operand_p (name))
3849 continue;
3851 e = get_or_alloc_expr_for_name (name);
3852 add_to_value (get_expr_value_id (e), e);
3853 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), e);
3854 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), e);
3857 /* Allocate the worklist. */
3858 worklist = XNEWVEC (basic_block, n_basic_blocks);
3860 /* Seed the algorithm by putting the dominator children of the entry
3861 block on the worklist. */
3862 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR);
3863 son;
3864 son = next_dom_son (CDI_DOMINATORS, son))
3865 worklist[sp++] = son;
3867 /* Loop until the worklist is empty. */
3868 while (sp)
3870 gimple_stmt_iterator gsi;
3871 gimple stmt;
3872 basic_block dom;
3873 unsigned int stmt_uid = 1;
3875 /* Pick a block from the worklist. */
3876 block = worklist[--sp];
3878 /* Initially, the set of available values in BLOCK is that of
3879 its immediate dominator. */
3880 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3881 if (dom)
3882 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3884 /* Generate values for PHI nodes. */
3885 for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
3886 make_values_for_phi (gsi_stmt (gsi), block);
3888 BB_MAY_NOTRETURN (block) = 0;
3890 /* Now compute value numbers and populate value sets with all
3891 the expressions computed in BLOCK. */
3892 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
3894 ssa_op_iter iter;
3895 tree op;
3897 stmt = gsi_stmt (gsi);
3898 gimple_set_uid (stmt, stmt_uid++);
3900 /* Cache whether the basic-block has any non-visible side-effect
3901 or control flow.
3902 If this isn't a call or it is the last stmt in the
3903 basic-block then the CFG represents things correctly. */
3904 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3906 /* Non-looping const functions always return normally.
3907 Otherwise the call might not return or have side-effects
3908 that forbids hoisting possibly trapping expressions
3909 before it. */
3910 int flags = gimple_call_flags (stmt);
3911 if (!(flags & ECF_CONST)
3912 || (flags & ECF_LOOPING_CONST_OR_PURE))
3913 BB_MAY_NOTRETURN (block) = 1;
3916 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3918 pre_expr e = get_or_alloc_expr_for_name (op);
3920 add_to_value (get_expr_value_id (e), e);
3921 bitmap_insert_into_set (TMP_GEN (block), e);
3922 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3925 if (gimple_has_side_effects (stmt)
3926 || stmt_could_throw_p (stmt)
3927 || is_gimple_debug (stmt))
3928 continue;
3930 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3931 add_to_exp_gen (block, op);
3933 switch (gimple_code (stmt))
3935 case GIMPLE_RETURN:
3936 continue;
3938 case GIMPLE_CALL:
3940 vn_reference_t ref;
3941 pre_expr result = NULL;
3942 VEC(vn_reference_op_s, heap) *ops = NULL;
3944 /* We can value number only calls to real functions. */
3945 if (gimple_call_internal_p (stmt))
3946 continue;
3948 copy_reference_ops_from_call (stmt, &ops);
3949 vn_reference_lookup_pieces (gimple_vuse (stmt), 0,
3950 gimple_expr_type (stmt),
3951 ops, &ref, VN_NOWALK);
3952 VEC_free (vn_reference_op_s, heap, ops);
3953 if (!ref)
3954 continue;
3956 /* If the value of the call is not invalidated in
3957 this block until it is computed, add the expression
3958 to EXP_GEN. */
3959 if (!gimple_vuse (stmt)
3960 || gimple_code
3961 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3962 || gimple_bb (SSA_NAME_DEF_STMT
3963 (gimple_vuse (stmt))) != block)
3965 result = (pre_expr) pool_alloc (pre_expr_pool);
3966 result->kind = REFERENCE;
3967 result->id = 0;
3968 PRE_EXPR_REFERENCE (result) = ref;
3970 get_or_alloc_expression_id (result);
3971 add_to_value (get_expr_value_id (result), result);
3972 bitmap_value_insert_into_set (EXP_GEN (block), result);
3974 continue;
3977 case GIMPLE_ASSIGN:
3979 pre_expr result = NULL;
3980 switch (vn_get_stmt_kind (stmt))
3982 case VN_NARY:
3984 vn_nary_op_t nary;
3985 vn_nary_op_lookup_pieces (gimple_num_ops (stmt) - 1,
3986 gimple_assign_rhs_code (stmt),
3987 gimple_expr_type (stmt),
3988 gimple_assign_rhs1_ptr (stmt),
3989 &nary);
3990 if (!nary)
3991 continue;
3993 /* If the NARY traps and there was a preceding
3994 point in the block that might not return avoid
3995 adding the nary to EXP_GEN. */
3996 if (BB_MAY_NOTRETURN (block)
3997 && vn_nary_may_trap (nary))
3998 continue;
4000 result = (pre_expr) pool_alloc (pre_expr_pool);
4001 result->kind = NARY;
4002 result->id = 0;
4003 PRE_EXPR_NARY (result) = nary;
4004 break;
4007 case VN_REFERENCE:
4009 vn_reference_t ref;
4010 vn_reference_lookup (gimple_assign_rhs1 (stmt),
4011 gimple_vuse (stmt),
4012 VN_WALK, &ref);
4013 if (!ref)
4014 continue;
4016 /* If the value of the reference is not invalidated in
4017 this block until it is computed, add the expression
4018 to EXP_GEN. */
4019 if (gimple_vuse (stmt))
4021 gimple def_stmt;
4022 bool ok = true;
4023 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
4024 while (!gimple_nop_p (def_stmt)
4025 && gimple_code (def_stmt) != GIMPLE_PHI
4026 && gimple_bb (def_stmt) == block)
4028 if (stmt_may_clobber_ref_p
4029 (def_stmt, gimple_assign_rhs1 (stmt)))
4031 ok = false;
4032 break;
4034 def_stmt
4035 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
4037 if (!ok)
4038 continue;
4041 result = (pre_expr) pool_alloc (pre_expr_pool);
4042 result->kind = REFERENCE;
4043 result->id = 0;
4044 PRE_EXPR_REFERENCE (result) = ref;
4045 break;
4048 default:
4049 continue;
4052 get_or_alloc_expression_id (result);
4053 add_to_value (get_expr_value_id (result), result);
4054 bitmap_value_insert_into_set (EXP_GEN (block), result);
4055 continue;
4057 default:
4058 break;
4062 /* Put the dominator children of BLOCK on the worklist of blocks
4063 to compute available sets for. */
4064 for (son = first_dom_son (CDI_DOMINATORS, block);
4065 son;
4066 son = next_dom_son (CDI_DOMINATORS, son))
4067 worklist[sp++] = son;
4070 free (worklist);
4074 /* Local state for the eliminate domwalk. */
4075 static VEC (gimple, heap) *el_to_remove;
4076 static VEC (gimple, heap) *el_to_update;
4077 static unsigned int el_todo;
4078 static VEC (tree, heap) *el_avail;
4079 static VEC (tree, heap) *el_avail_stack;
4081 /* Return a leader for OP that is available at the current point of the
4082 eliminate domwalk. */
4084 static tree
4085 eliminate_avail (tree op)
4087 tree valnum = VN_INFO (op)->valnum;
4088 if (TREE_CODE (valnum) == SSA_NAME)
4090 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
4091 return valnum;
4092 if (VEC_length (tree, el_avail) > SSA_NAME_VERSION (valnum))
4093 return VEC_index (tree, el_avail, SSA_NAME_VERSION (valnum));
4095 else if (is_gimple_min_invariant (valnum))
4096 return valnum;
4097 return NULL_TREE;
4100 /* At the current point of the eliminate domwalk make OP available. */
4102 static void
4103 eliminate_push_avail (tree op)
4105 tree valnum = VN_INFO (op)->valnum;
4106 if (TREE_CODE (valnum) == SSA_NAME)
4108 if (VEC_length (tree, el_avail) <= SSA_NAME_VERSION (valnum))
4109 VEC_safe_grow_cleared (tree, heap,
4110 el_avail, SSA_NAME_VERSION (valnum) + 1);
4111 VEC_replace (tree, el_avail, SSA_NAME_VERSION (valnum), op);
4112 VEC_safe_push (tree, heap, el_avail_stack, op);
4116 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
4117 the leader for the expression if insertion was successful. */
4119 static tree
4120 eliminate_insert (gimple_stmt_iterator *gsi, tree val)
4122 tree expr = vn_get_expr_for (val);
4123 if (!CONVERT_EXPR_P (expr)
4124 && TREE_CODE (expr) != VIEW_CONVERT_EXPR)
4125 return NULL_TREE;
4127 tree op = TREE_OPERAND (expr, 0);
4128 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (op) : op;
4129 if (!leader)
4130 return NULL_TREE;
4132 tree res = make_temp_ssa_name (TREE_TYPE (val), NULL, "pretmp");
4133 gimple tem = gimple_build_assign (res,
4134 build1 (TREE_CODE (expr),
4135 TREE_TYPE (expr), leader));
4136 gsi_insert_before (gsi, tem, GSI_SAME_STMT);
4137 VN_INFO_GET (res)->valnum = val;
4139 if (TREE_CODE (leader) == SSA_NAME)
4140 gimple_set_plf (SSA_NAME_DEF_STMT (leader), NECESSARY, true);
4142 pre_stats.insertions++;
4143 if (dump_file && (dump_flags & TDF_DETAILS))
4145 fprintf (dump_file, "Inserted ");
4146 print_gimple_stmt (dump_file, tem, 0, 0);
4149 return res;
4152 /* Perform elimination for the basic-block B during the domwalk. */
4154 static void
4155 eliminate_bb (dom_walk_data *, basic_block b)
4157 gimple_stmt_iterator gsi;
4158 gimple stmt;
4160 /* Mark new bb. */
4161 VEC_safe_push (tree, heap, el_avail_stack, NULL_TREE);
4163 for (gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
4165 gimple stmt, phi = gsi_stmt (gsi);
4166 tree sprime = NULL_TREE, res = PHI_RESULT (phi);
4167 gimple_stmt_iterator gsi2;
4169 /* We want to perform redundant PHI elimination. Do so by
4170 replacing the PHI with a single copy if possible.
4171 Do not touch inserted, single-argument or virtual PHIs. */
4172 if (gimple_phi_num_args (phi) == 1
4173 || virtual_operand_p (res))
4175 gsi_next (&gsi);
4176 continue;
4179 sprime = eliminate_avail (res);
4180 if (!sprime
4181 || sprime == res)
4183 eliminate_push_avail (res);
4184 gsi_next (&gsi);
4185 continue;
4187 else if (is_gimple_min_invariant (sprime))
4189 if (!useless_type_conversion_p (TREE_TYPE (res),
4190 TREE_TYPE (sprime)))
4191 sprime = fold_convert (TREE_TYPE (res), sprime);
4194 if (dump_file && (dump_flags & TDF_DETAILS))
4196 fprintf (dump_file, "Replaced redundant PHI node defining ");
4197 print_generic_expr (dump_file, res, 0);
4198 fprintf (dump_file, " with ");
4199 print_generic_expr (dump_file, sprime, 0);
4200 fprintf (dump_file, "\n");
4203 remove_phi_node (&gsi, false);
4205 if (inserted_exprs
4206 && !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res))
4207 && TREE_CODE (sprime) == SSA_NAME)
4208 gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
4210 if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
4211 sprime = fold_convert (TREE_TYPE (res), sprime);
4212 stmt = gimple_build_assign (res, sprime);
4213 SSA_NAME_DEF_STMT (res) = stmt;
4214 gimple_set_plf (stmt, NECESSARY, gimple_plf (phi, NECESSARY));
4216 gsi2 = gsi_after_labels (b);
4217 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
4218 /* Queue the copy for eventual removal. */
4219 VEC_safe_push (gimple, heap, el_to_remove, stmt);
4220 /* If we inserted this PHI node ourself, it's not an elimination. */
4221 if (inserted_exprs
4222 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
4223 pre_stats.phis--;
4224 else
4225 pre_stats.eliminations++;
4228 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi); gsi_next (&gsi))
4230 tree lhs = NULL_TREE;
4231 tree rhs = NULL_TREE;
4233 stmt = gsi_stmt (gsi);
4235 if (gimple_has_lhs (stmt))
4236 lhs = gimple_get_lhs (stmt);
4238 if (gimple_assign_single_p (stmt))
4239 rhs = gimple_assign_rhs1 (stmt);
4241 /* Lookup the RHS of the expression, see if we have an
4242 available computation for it. If so, replace the RHS with
4243 the available computation.
4245 See PR43491.
4246 We don't replace global register variable when it is a the RHS of
4247 a single assign. We do replace local register variable since gcc
4248 does not guarantee local variable will be allocated in register. */
4249 if (gimple_has_lhs (stmt)
4250 && TREE_CODE (lhs) == SSA_NAME
4251 && !gimple_assign_ssa_name_copy_p (stmt)
4252 && (!gimple_assign_single_p (stmt)
4253 || (!is_gimple_min_invariant (rhs)
4254 && (gimple_assign_rhs_code (stmt) != VAR_DECL
4255 || !is_global_var (rhs)
4256 || !DECL_HARD_REGISTER (rhs))))
4257 && !gimple_has_volatile_ops (stmt))
4259 tree sprime;
4260 gimple orig_stmt = stmt;
4262 sprime = eliminate_avail (lhs);
4263 if (!sprime)
4265 /* If there is no existing usable leader but SCCVN thinks
4266 it has an expression it wants to use as replacement,
4267 insert that. */
4268 tree val = VN_INFO (lhs)->valnum;
4269 if (val != VN_TOP
4270 && TREE_CODE (val) == SSA_NAME
4271 && VN_INFO (val)->needs_insertion
4272 && (sprime = eliminate_insert (&gsi, val)) != NULL_TREE)
4273 eliminate_push_avail (sprime);
4275 else if (is_gimple_min_invariant (sprime))
4277 /* If there is no existing leader but SCCVN knows this
4278 value is constant, use that constant. */
4279 if (!useless_type_conversion_p (TREE_TYPE (lhs),
4280 TREE_TYPE (sprime)))
4281 sprime = fold_convert (TREE_TYPE (lhs), sprime);
4283 if (dump_file && (dump_flags & TDF_DETAILS))
4285 fprintf (dump_file, "Replaced ");
4286 print_gimple_expr (dump_file, stmt, 0, 0);
4287 fprintf (dump_file, " with ");
4288 print_generic_expr (dump_file, sprime, 0);
4289 fprintf (dump_file, " in ");
4290 print_gimple_stmt (dump_file, stmt, 0, 0);
4292 pre_stats.eliminations++;
4293 propagate_tree_value_into_stmt (&gsi, sprime);
4294 stmt = gsi_stmt (gsi);
4295 update_stmt (stmt);
4297 /* If we removed EH side-effects from the statement, clean
4298 its EH information. */
4299 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4301 bitmap_set_bit (need_eh_cleanup,
4302 gimple_bb (stmt)->index);
4303 if (dump_file && (dump_flags & TDF_DETAILS))
4304 fprintf (dump_file, " Removed EH side-effects.\n");
4306 continue;
4309 /* If there is no usable leader mark lhs as leader for its value. */
4310 if (!sprime)
4311 eliminate_push_avail (lhs);
4313 if (sprime
4314 && sprime != lhs
4315 && (rhs == NULL_TREE
4316 || TREE_CODE (rhs) != SSA_NAME
4317 || may_propagate_copy (rhs, sprime)))
4319 bool can_make_abnormal_goto
4320 = is_gimple_call (stmt)
4321 && stmt_can_make_abnormal_goto (stmt);
4323 gcc_assert (sprime != rhs);
4325 if (dump_file && (dump_flags & TDF_DETAILS))
4327 fprintf (dump_file, "Replaced ");
4328 print_gimple_expr (dump_file, stmt, 0, 0);
4329 fprintf (dump_file, " with ");
4330 print_generic_expr (dump_file, sprime, 0);
4331 fprintf (dump_file, " in ");
4332 print_gimple_stmt (dump_file, stmt, 0, 0);
4335 if (TREE_CODE (sprime) == SSA_NAME)
4336 gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
4337 NECESSARY, true);
4338 /* We need to make sure the new and old types actually match,
4339 which may require adding a simple cast, which fold_convert
4340 will do for us. */
4341 if ((!rhs || TREE_CODE (rhs) != SSA_NAME)
4342 && !useless_type_conversion_p (gimple_expr_type (stmt),
4343 TREE_TYPE (sprime)))
4344 sprime = fold_convert (gimple_expr_type (stmt), sprime);
4346 pre_stats.eliminations++;
4347 propagate_tree_value_into_stmt (&gsi, sprime);
4348 stmt = gsi_stmt (gsi);
4349 update_stmt (stmt);
4351 /* If we removed EH side-effects from the statement, clean
4352 its EH information. */
4353 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4355 bitmap_set_bit (need_eh_cleanup,
4356 gimple_bb (stmt)->index);
4357 if (dump_file && (dump_flags & TDF_DETAILS))
4358 fprintf (dump_file, " Removed EH side-effects.\n");
4361 /* Likewise for AB side-effects. */
4362 if (can_make_abnormal_goto
4363 && !stmt_can_make_abnormal_goto (stmt))
4365 bitmap_set_bit (need_ab_cleanup,
4366 gimple_bb (stmt)->index);
4367 if (dump_file && (dump_flags & TDF_DETAILS))
4368 fprintf (dump_file, " Removed AB side-effects.\n");
4372 /* If the statement is a scalar store, see if the expression
4373 has the same value number as its rhs. If so, the store is
4374 dead. */
4375 else if (gimple_assign_single_p (stmt)
4376 && !gimple_has_volatile_ops (stmt)
4377 && !is_gimple_reg (gimple_assign_lhs (stmt))
4378 && (TREE_CODE (rhs) == SSA_NAME
4379 || is_gimple_min_invariant (rhs)))
4381 tree val;
4382 val = vn_reference_lookup (gimple_assign_lhs (stmt),
4383 gimple_vuse (stmt), VN_WALK, NULL);
4384 if (TREE_CODE (rhs) == SSA_NAME)
4385 rhs = VN_INFO (rhs)->valnum;
4386 if (val
4387 && operand_equal_p (val, rhs, 0))
4389 if (dump_file && (dump_flags & TDF_DETAILS))
4391 fprintf (dump_file, "Deleted redundant store ");
4392 print_gimple_stmt (dump_file, stmt, 0, 0);
4395 /* Queue stmt for removal. */
4396 VEC_safe_push (gimple, heap, el_to_remove, stmt);
4399 /* Visit COND_EXPRs and fold the comparison with the
4400 available value-numbers. */
4401 else if (gimple_code (stmt) == GIMPLE_COND)
4403 tree op0 = gimple_cond_lhs (stmt);
4404 tree op1 = gimple_cond_rhs (stmt);
4405 tree result;
4407 if (TREE_CODE (op0) == SSA_NAME)
4408 op0 = VN_INFO (op0)->valnum;
4409 if (TREE_CODE (op1) == SSA_NAME)
4410 op1 = VN_INFO (op1)->valnum;
4411 result = fold_binary (gimple_cond_code (stmt), boolean_type_node,
4412 op0, op1);
4413 if (result && TREE_CODE (result) == INTEGER_CST)
4415 if (integer_zerop (result))
4416 gimple_cond_make_false (stmt);
4417 else
4418 gimple_cond_make_true (stmt);
4419 update_stmt (stmt);
4420 el_todo = TODO_cleanup_cfg;
4423 /* Visit indirect calls and turn them into direct calls if
4424 possible. */
4425 if (is_gimple_call (stmt))
4427 tree orig_fn = gimple_call_fn (stmt);
4428 tree fn;
4429 if (!orig_fn)
4430 continue;
4431 if (TREE_CODE (orig_fn) == SSA_NAME)
4432 fn = VN_INFO (orig_fn)->valnum;
4433 else if (TREE_CODE (orig_fn) == OBJ_TYPE_REF
4434 && TREE_CODE (OBJ_TYPE_REF_EXPR (orig_fn)) == SSA_NAME)
4435 fn = VN_INFO (OBJ_TYPE_REF_EXPR (orig_fn))->valnum;
4436 else
4437 continue;
4438 if (gimple_call_addr_fndecl (fn) != NULL_TREE
4439 && useless_type_conversion_p (TREE_TYPE (orig_fn),
4440 TREE_TYPE (fn)))
4442 bool can_make_abnormal_goto
4443 = stmt_can_make_abnormal_goto (stmt);
4444 bool was_noreturn = gimple_call_noreturn_p (stmt);
4446 if (dump_file && (dump_flags & TDF_DETAILS))
4448 fprintf (dump_file, "Replacing call target with ");
4449 print_generic_expr (dump_file, fn, 0);
4450 fprintf (dump_file, " in ");
4451 print_gimple_stmt (dump_file, stmt, 0, 0);
4454 gimple_call_set_fn (stmt, fn);
4455 VEC_safe_push (gimple, heap, el_to_update, stmt);
4457 /* When changing a call into a noreturn call, cfg cleanup
4458 is needed to fix up the noreturn call. */
4459 if (!was_noreturn && gimple_call_noreturn_p (stmt))
4460 el_todo |= TODO_cleanup_cfg;
4462 /* If we removed EH side-effects from the statement, clean
4463 its EH information. */
4464 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
4466 bitmap_set_bit (need_eh_cleanup,
4467 gimple_bb (stmt)->index);
4468 if (dump_file && (dump_flags & TDF_DETAILS))
4469 fprintf (dump_file, " Removed EH side-effects.\n");
4472 /* Likewise for AB side-effects. */
4473 if (can_make_abnormal_goto
4474 && !stmt_can_make_abnormal_goto (stmt))
4476 bitmap_set_bit (need_ab_cleanup,
4477 gimple_bb (stmt)->index);
4478 if (dump_file && (dump_flags & TDF_DETAILS))
4479 fprintf (dump_file, " Removed AB side-effects.\n");
4482 /* Changing an indirect call to a direct call may
4483 have exposed different semantics. This may
4484 require an SSA update. */
4485 el_todo |= TODO_update_ssa_only_virtuals;
4491 /* Make no longer available leaders no longer available. */
4493 static void
4494 eliminate_leave_block (dom_walk_data *, basic_block)
4496 tree entry;
4497 while ((entry = VEC_pop (tree, el_avail_stack)) != NULL_TREE)
4498 VEC_replace (tree, el_avail,
4499 SSA_NAME_VERSION (VN_INFO (entry)->valnum), NULL_TREE);
4502 /* Eliminate fully redundant computations. */
4504 static unsigned int
4505 eliminate (void)
4507 struct dom_walk_data walk_data;
4508 gimple_stmt_iterator gsi;
4509 gimple stmt;
4510 unsigned i;
4512 need_eh_cleanup = BITMAP_ALLOC (NULL);
4513 need_ab_cleanup = BITMAP_ALLOC (NULL);
4515 el_to_remove = NULL;
4516 el_to_update = NULL;
4517 el_todo = 0;
4518 el_avail = NULL;
4519 el_avail_stack = NULL;
4521 walk_data.dom_direction = CDI_DOMINATORS;
4522 walk_data.initialize_block_local_data = NULL;
4523 walk_data.before_dom_children = eliminate_bb;
4524 walk_data.after_dom_children = eliminate_leave_block;
4525 walk_data.global_data = NULL;
4526 walk_data.block_local_data_size = 0;
4527 init_walk_dominator_tree (&walk_data);
4528 walk_dominator_tree (&walk_data, ENTRY_BLOCK_PTR);
4529 fini_walk_dominator_tree (&walk_data);
4531 VEC_free (tree, heap, el_avail);
4532 VEC_free (tree, heap, el_avail_stack);
4534 /* We cannot remove stmts during BB walk, especially not release SSA
4535 names there as this confuses the VN machinery. The stmts ending
4536 up in el_to_remove are either stores or simple copies. */
4537 FOR_EACH_VEC_ELT (gimple, el_to_remove, i, stmt)
4539 tree lhs = gimple_assign_lhs (stmt);
4540 tree rhs = gimple_assign_rhs1 (stmt);
4541 use_operand_p use_p;
4542 gimple use_stmt;
4544 /* If there is a single use only, propagate the equivalency
4545 instead of keeping the copy. */
4546 if (TREE_CODE (lhs) == SSA_NAME
4547 && TREE_CODE (rhs) == SSA_NAME
4548 && single_imm_use (lhs, &use_p, &use_stmt)
4549 && may_propagate_copy (USE_FROM_PTR (use_p), rhs))
4551 SET_USE (use_p, rhs);
4552 update_stmt (use_stmt);
4553 if (inserted_exprs
4554 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (lhs))
4555 && TREE_CODE (rhs) == SSA_NAME)
4556 gimple_set_plf (SSA_NAME_DEF_STMT (rhs), NECESSARY, true);
4559 /* If this is a store or a now unused copy, remove it. */
4560 if (TREE_CODE (lhs) != SSA_NAME
4561 || has_zero_uses (lhs))
4563 basic_block bb = gimple_bb (stmt);
4564 gsi = gsi_for_stmt (stmt);
4565 unlink_stmt_vdef (stmt);
4566 if (gsi_remove (&gsi, true))
4567 bitmap_set_bit (need_eh_cleanup, bb->index);
4568 if (inserted_exprs
4569 && TREE_CODE (lhs) == SSA_NAME)
4570 bitmap_clear_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
4571 release_defs (stmt);
4574 VEC_free (gimple, heap, el_to_remove);
4576 /* We cannot update call statements with virtual operands during
4577 SSA walk. This might remove them which in turn makes our
4578 VN lattice invalid. */
4579 FOR_EACH_VEC_ELT (gimple, el_to_update, i, stmt)
4580 update_stmt (stmt);
4581 VEC_free (gimple, heap, el_to_update);
4583 return el_todo;
4586 /* Perform CFG cleanups made necessary by elimination. */
4588 static void
4589 fini_eliminate (void)
4591 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
4592 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
4594 if (do_eh_cleanup)
4595 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
4597 if (do_ab_cleanup)
4598 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
4600 BITMAP_FREE (need_eh_cleanup);
4601 BITMAP_FREE (need_ab_cleanup);
4603 if (do_eh_cleanup || do_ab_cleanup)
4604 cleanup_tree_cfg ();
4607 /* Borrow a bit of tree-ssa-dce.c for the moment.
4608 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4609 this may be a bit faster, and we may want critical edges kept split. */
4611 /* If OP's defining statement has not already been determined to be necessary,
4612 mark that statement necessary. Return the stmt, if it is newly
4613 necessary. */
4615 static inline gimple
4616 mark_operand_necessary (tree op)
4618 gimple stmt;
4620 gcc_assert (op);
4622 if (TREE_CODE (op) != SSA_NAME)
4623 return NULL;
4625 stmt = SSA_NAME_DEF_STMT (op);
4626 gcc_assert (stmt);
4628 if (gimple_plf (stmt, NECESSARY)
4629 || gimple_nop_p (stmt))
4630 return NULL;
4632 gimple_set_plf (stmt, NECESSARY, true);
4633 return stmt;
4636 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4637 to insert PHI nodes sometimes, and because value numbering of casts isn't
4638 perfect, we sometimes end up inserting dead code. This simple DCE-like
4639 pass removes any insertions we made that weren't actually used. */
4641 static void
4642 remove_dead_inserted_code (void)
4644 bitmap worklist;
4645 unsigned i;
4646 bitmap_iterator bi;
4647 gimple t;
4649 worklist = BITMAP_ALLOC (NULL);
4650 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4652 t = SSA_NAME_DEF_STMT (ssa_name (i));
4653 if (gimple_plf (t, NECESSARY))
4654 bitmap_set_bit (worklist, i);
4656 while (!bitmap_empty_p (worklist))
4658 i = bitmap_first_set_bit (worklist);
4659 bitmap_clear_bit (worklist, i);
4660 t = SSA_NAME_DEF_STMT (ssa_name (i));
4662 /* PHI nodes are somewhat special in that each PHI alternative has
4663 data and control dependencies. All the statements feeding the
4664 PHI node's arguments are always necessary. */
4665 if (gimple_code (t) == GIMPLE_PHI)
4667 unsigned k;
4669 for (k = 0; k < gimple_phi_num_args (t); k++)
4671 tree arg = PHI_ARG_DEF (t, k);
4672 if (TREE_CODE (arg) == SSA_NAME)
4674 gimple n = mark_operand_necessary (arg);
4675 if (n)
4676 bitmap_set_bit (worklist, SSA_NAME_VERSION (arg));
4680 else
4682 /* Propagate through the operands. Examine all the USE, VUSE and
4683 VDEF operands in this statement. Mark all the statements
4684 which feed this statement's uses as necessary. */
4685 ssa_op_iter iter;
4686 tree use;
4688 /* The operands of VDEF expressions are also needed as they
4689 represent potential definitions that may reach this
4690 statement (VDEF operands allow us to follow def-def
4691 links). */
4693 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
4695 gimple n = mark_operand_necessary (use);
4696 if (n)
4697 bitmap_set_bit (worklist, SSA_NAME_VERSION (use));
4702 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4704 t = SSA_NAME_DEF_STMT (ssa_name (i));
4705 if (!gimple_plf (t, NECESSARY))
4707 gimple_stmt_iterator gsi;
4709 if (dump_file && (dump_flags & TDF_DETAILS))
4711 fprintf (dump_file, "Removing unnecessary insertion:");
4712 print_gimple_stmt (dump_file, t, 0, 0);
4715 gsi = gsi_for_stmt (t);
4716 if (gimple_code (t) == GIMPLE_PHI)
4717 remove_phi_node (&gsi, true);
4718 else
4720 gsi_remove (&gsi, true);
4721 release_defs (t);
4725 BITMAP_FREE (worklist);
4728 /* Compute a reverse post-order in *POST_ORDER. If INCLUDE_ENTRY_EXIT is
4729 true, then then ENTRY_BLOCK and EXIT_BLOCK are included. Returns
4730 the number of visited blocks. */
4732 static int
4733 my_rev_post_order_compute (int *post_order, bool include_entry_exit)
4735 edge_iterator *stack;
4736 int sp;
4737 int post_order_num = 0;
4738 sbitmap visited;
4740 if (include_entry_exit)
4741 post_order[post_order_num++] = EXIT_BLOCK;
4743 /* Allocate stack for back-tracking up CFG. */
4744 stack = XNEWVEC (edge_iterator, n_basic_blocks + 1);
4745 sp = 0;
4747 /* Allocate bitmap to track nodes that have been visited. */
4748 visited = sbitmap_alloc (last_basic_block);
4750 /* None of the nodes in the CFG have been visited yet. */
4751 sbitmap_zero (visited);
4753 /* Push the last edge on to the stack. */
4754 stack[sp++] = ei_start (EXIT_BLOCK_PTR->preds);
4756 while (sp)
4758 edge_iterator ei;
4759 basic_block src;
4760 basic_block dest;
4762 /* Look at the edge on the top of the stack. */
4763 ei = stack[sp - 1];
4764 src = ei_edge (ei)->src;
4765 dest = ei_edge (ei)->dest;
4767 /* Check if the edge source has been visited yet. */
4768 if (src != ENTRY_BLOCK_PTR && ! TEST_BIT (visited, src->index))
4770 /* Mark that we have visited the destination. */
4771 SET_BIT (visited, src->index);
4773 if (EDGE_COUNT (src->preds) > 0)
4774 /* Since the SRC node has been visited for the first
4775 time, check its predecessors. */
4776 stack[sp++] = ei_start (src->preds);
4777 else
4778 post_order[post_order_num++] = src->index;
4780 else
4782 if (ei_one_before_end_p (ei) && dest != EXIT_BLOCK_PTR)
4783 post_order[post_order_num++] = dest->index;
4785 if (!ei_one_before_end_p (ei))
4786 ei_next (&stack[sp - 1]);
4787 else
4788 sp--;
4792 if (include_entry_exit)
4793 post_order[post_order_num++] = ENTRY_BLOCK;
4795 free (stack);
4796 sbitmap_free (visited);
4797 return post_order_num;
4801 /* Initialize data structures used by PRE. */
4803 static void
4804 init_pre (void)
4806 basic_block bb;
4808 next_expression_id = 1;
4809 expressions = NULL;
4810 VEC_safe_push (pre_expr, heap, expressions, NULL);
4811 value_expressions = VEC_alloc (bitmap, heap, get_max_value_id () + 1);
4812 VEC_safe_grow_cleared (bitmap, heap, value_expressions,
4813 get_max_value_id() + 1);
4814 name_to_id = NULL;
4816 inserted_exprs = BITMAP_ALLOC (NULL);
4818 connect_infinite_loops_to_exit ();
4819 memset (&pre_stats, 0, sizeof (pre_stats));
4822 postorder = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
4823 my_rev_post_order_compute (postorder, false);
4825 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4827 calculate_dominance_info (CDI_POST_DOMINATORS);
4828 calculate_dominance_info (CDI_DOMINATORS);
4830 bitmap_obstack_initialize (&grand_bitmap_obstack);
4831 phi_translate_table.create (5110);
4832 expression_to_id.create (num_ssa_names * 3);
4833 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
4834 sizeof (struct bitmap_set), 30);
4835 pre_expr_pool = create_alloc_pool ("pre_expr nodes",
4836 sizeof (struct pre_expr_d), 30);
4837 FOR_ALL_BB (bb)
4839 EXP_GEN (bb) = bitmap_set_new ();
4840 PHI_GEN (bb) = bitmap_set_new ();
4841 TMP_GEN (bb) = bitmap_set_new ();
4842 AVAIL_OUT (bb) = bitmap_set_new ();
4847 /* Deallocate data structures used by PRE. */
4849 static void
4850 fini_pre ()
4852 free (postorder);
4853 VEC_free (bitmap, heap, value_expressions);
4854 BITMAP_FREE (inserted_exprs);
4855 bitmap_obstack_release (&grand_bitmap_obstack);
4856 free_alloc_pool (bitmap_set_pool);
4857 free_alloc_pool (pre_expr_pool);
4858 phi_translate_table.dispose ();
4859 expression_to_id.dispose ();
4860 VEC_free (unsigned, heap, name_to_id);
4862 free_aux_for_blocks ();
4864 free_dominance_info (CDI_POST_DOMINATORS);
4867 /* Gate and execute functions for PRE. */
4869 static unsigned int
4870 do_pre (void)
4872 unsigned int todo = 0;
4874 do_partial_partial =
4875 flag_tree_partial_pre && optimize_function_for_speed_p (cfun);
4877 /* This has to happen before SCCVN runs because
4878 loop_optimizer_init may create new phis, etc. */
4879 loop_optimizer_init (LOOPS_NORMAL);
4881 if (!run_scc_vn (VN_WALK))
4883 loop_optimizer_finalize ();
4884 return 0;
4887 init_pre ();
4888 scev_initialize ();
4890 /* Collect and value number expressions computed in each basic block. */
4891 compute_avail ();
4893 if (dump_file && (dump_flags & TDF_DETAILS))
4895 basic_block bb;
4896 FOR_ALL_BB (bb)
4898 print_bitmap_set (dump_file, EXP_GEN (bb),
4899 "exp_gen", bb->index);
4900 print_bitmap_set (dump_file, PHI_GEN (bb),
4901 "phi_gen", bb->index);
4902 print_bitmap_set (dump_file, TMP_GEN (bb),
4903 "tmp_gen", bb->index);
4904 print_bitmap_set (dump_file, AVAIL_OUT (bb),
4905 "avail_out", bb->index);
4909 /* Insert can get quite slow on an incredibly large number of basic
4910 blocks due to some quadratic behavior. Until this behavior is
4911 fixed, don't run it when he have an incredibly large number of
4912 bb's. If we aren't going to run insert, there is no point in
4913 computing ANTIC, either, even though it's plenty fast. */
4914 if (n_basic_blocks < 4000)
4916 compute_antic ();
4917 insert ();
4920 /* Make sure to remove fake edges before committing our inserts.
4921 This makes sure we don't end up with extra critical edges that
4922 we would need to split. */
4923 remove_fake_exit_edges ();
4924 gsi_commit_edge_inserts ();
4926 /* Remove all the redundant expressions. */
4927 todo |= eliminate ();
4929 statistics_counter_event (cfun, "Insertions", pre_stats.insertions);
4930 statistics_counter_event (cfun, "PA inserted", pre_stats.pa_insert);
4931 statistics_counter_event (cfun, "New PHIs", pre_stats.phis);
4932 statistics_counter_event (cfun, "Eliminated", pre_stats.eliminations);
4933 statistics_counter_event (cfun, "Constified", pre_stats.constified);
4935 clear_expression_ids ();
4936 remove_dead_inserted_code ();
4937 todo |= TODO_verify_flow;
4939 scev_finalize ();
4940 fini_pre ();
4941 fini_eliminate ();
4942 loop_optimizer_finalize ();
4944 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4945 case we can merge the block with the remaining predecessor of the block.
4946 It should either:
4947 - call merge_blocks after each tail merge iteration
4948 - call merge_blocks after all tail merge iterations
4949 - mark TODO_cleanup_cfg when necessary
4950 - share the cfg cleanup with fini_pre. */
4951 todo |= tail_merge_optimize (todo);
4953 free_scc_vn ();
4955 return todo;
4958 static bool
4959 gate_pre (void)
4961 return flag_tree_pre != 0;
4964 struct gimple_opt_pass pass_pre =
4967 GIMPLE_PASS,
4968 "pre", /* name */
4969 gate_pre, /* gate */
4970 do_pre, /* execute */
4971 NULL, /* sub */
4972 NULL, /* next */
4973 0, /* static_pass_number */
4974 TV_TREE_PRE, /* tv_id */
4975 PROP_no_crit_edges | PROP_cfg
4976 | PROP_ssa, /* properties_required */
4977 0, /* properties_provided */
4978 0, /* properties_destroyed */
4979 TODO_rebuild_alias, /* todo_flags_start */
4980 TODO_update_ssa_only_virtuals | TODO_ggc_collect
4981 | TODO_verify_ssa /* todo_flags_finish */
4986 /* Gate and execute functions for FRE. */
4988 static unsigned int
4989 execute_fre (void)
4991 unsigned int todo = 0;
4993 if (!run_scc_vn (VN_WALKREWRITE))
4994 return 0;
4996 memset (&pre_stats, 0, sizeof (pre_stats));
4998 /* Remove all the redundant expressions. */
4999 todo |= eliminate ();
5001 fini_eliminate ();
5003 free_scc_vn ();
5005 statistics_counter_event (cfun, "Insertions", pre_stats.insertions);
5006 statistics_counter_event (cfun, "Eliminated", pre_stats.eliminations);
5007 statistics_counter_event (cfun, "Constified", pre_stats.constified);
5009 return todo;
5012 static bool
5013 gate_fre (void)
5015 return flag_tree_fre != 0;
5018 struct gimple_opt_pass pass_fre =
5021 GIMPLE_PASS,
5022 "fre", /* name */
5023 gate_fre, /* gate */
5024 execute_fre, /* execute */
5025 NULL, /* sub */
5026 NULL, /* next */
5027 0, /* static_pass_number */
5028 TV_TREE_FRE, /* tv_id */
5029 PROP_cfg | PROP_ssa, /* properties_required */
5030 0, /* properties_provided */
5031 0, /* properties_destroyed */
5032 0, /* todo_flags_start */
5033 TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */