2 Copyright (C) 2001-2014 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
24 #include "coretypes.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-inline.h"
30 #include "hash-table.h"
31 #include "tree-ssa-alias.h"
32 #include "internal-fn.h"
33 #include "gimple-fold.h"
35 #include "gimple-expr.h"
39 #include "gimple-iterator.h"
40 #include "gimplify-me.h"
41 #include "gimple-ssa.h"
43 #include "tree-phinodes.h"
44 #include "ssa-iterators.h"
45 #include "stringpool.h"
46 #include "tree-ssanames.h"
47 #include "tree-ssa-loop.h"
48 #include "tree-into-ssa.h"
52 #include "tree-iterator.h"
53 #include "alloc-pool.h"
55 #include "tree-pass.h"
57 #include "langhooks.h"
59 #include "tree-ssa-sccvn.h"
60 #include "tree-scalar-evolution.h"
65 #include "tree-ssa-propagate.h"
69 1. Avail sets can be shared by making an avail_find_leader that
70 walks up the dominator tree and looks in those avail sets.
71 This might affect code optimality, it's unclear right now.
72 2. Strength reduction can be performed by anticipating expressions
73 we can repair later on.
74 3. We can do back-substitution or smarter value numbering to catch
75 commutative expressions split up over multiple statements.
78 /* For ease of terminology, "expression node" in the below refers to
79 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
80 represent the actual statement containing the expressions we care about,
81 and we cache the value number by putting it in the expression. */
85 First we walk the statements to generate the AVAIL sets, the
86 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
87 generation of values/expressions by a given block. We use them
88 when computing the ANTIC sets. The AVAIL sets consist of
89 SSA_NAME's that represent values, so we know what values are
90 available in what blocks. AVAIL is a forward dataflow problem. In
91 SSA, values are never killed, so we don't need a kill set, or a
92 fixpoint iteration, in order to calculate the AVAIL sets. In
93 traditional parlance, AVAIL sets tell us the downsafety of the
96 Next, we generate the ANTIC sets. These sets represent the
97 anticipatable expressions. ANTIC is a backwards dataflow
98 problem. An expression is anticipatable in a given block if it could
99 be generated in that block. This means that if we had to perform
100 an insertion in that block, of the value of that expression, we
101 could. Calculating the ANTIC sets requires phi translation of
102 expressions, because the flow goes backwards through phis. We must
103 iterate to a fixpoint of the ANTIC sets, because we have a kill
104 set. Even in SSA form, values are not live over the entire
105 function, only from their definition point onwards. So we have to
106 remove values from the ANTIC set once we go past the definition
107 point of the leaders that make them up.
108 compute_antic/compute_antic_aux performs this computation.
110 Third, we perform insertions to make partially redundant
111 expressions fully redundant.
113 An expression is partially redundant (excluding partial
116 1. It is AVAIL in some, but not all, of the predecessors of a
118 2. It is ANTIC in all the predecessors.
120 In order to make it fully redundant, we insert the expression into
121 the predecessors where it is not available, but is ANTIC.
123 For the partial anticipation case, we only perform insertion if it
124 is partially anticipated in some block, and fully available in all
127 insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
128 performs these steps.
130 Fourth, we eliminate fully redundant expressions.
131 This is a simple statement walk that replaces redundant
132 calculations with the now available values. */
134 /* Representations of value numbers:
136 Value numbers are represented by a representative SSA_NAME. We
137 will create fake SSA_NAME's in situations where we need a
138 representative but do not have one (because it is a complex
139 expression). In order to facilitate storing the value numbers in
140 bitmaps, and keep the number of wasted SSA_NAME's down, we also
141 associate a value_id with each value number, and create full blown
142 ssa_name's only where we actually need them (IE in operands of
143 existing expressions).
145 Theoretically you could replace all the value_id's with
146 SSA_NAME_VERSION, but this would allocate a large number of
147 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
148 It would also require an additional indirection at each point we
151 /* Representation of expressions on value numbers:
153 Expressions consisting of value numbers are represented the same
154 way as our VN internally represents them, with an additional
155 "pre_expr" wrapping around them in order to facilitate storing all
156 of the expressions in the same sets. */
158 /* Representation of sets:
160 The dataflow sets do not need to be sorted in any particular order
161 for the majority of their lifetime, are simply represented as two
162 bitmaps, one that keeps track of values present in the set, and one
163 that keeps track of expressions present in the set.
165 When we need them in topological order, we produce it on demand by
166 transforming the bitmap into an array and sorting it into topo
169 /* Type of expression, used to know which member of the PRE_EXPR union
180 typedef union pre_expr_union_d
185 vn_reference_t reference
;
188 typedef struct pre_expr_d
: typed_noop_remove
<pre_expr_d
>
190 enum pre_expr_kind kind
;
194 /* hash_table support. */
195 typedef pre_expr_d value_type
;
196 typedef pre_expr_d compare_type
;
197 static inline hashval_t
hash (const pre_expr_d
*);
198 static inline int equal (const pre_expr_d
*, const pre_expr_d
*);
201 #define PRE_EXPR_NAME(e) (e)->u.name
202 #define PRE_EXPR_NARY(e) (e)->u.nary
203 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
204 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
206 /* Compare E1 and E1 for equality. */
209 pre_expr_d::equal (const value_type
*e1
, const compare_type
*e2
)
211 if (e1
->kind
!= e2
->kind
)
217 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1
),
218 PRE_EXPR_CONSTANT (e2
));
220 return PRE_EXPR_NAME (e1
) == PRE_EXPR_NAME (e2
);
222 return vn_nary_op_eq (PRE_EXPR_NARY (e1
), PRE_EXPR_NARY (e2
));
224 return vn_reference_eq (PRE_EXPR_REFERENCE (e1
),
225 PRE_EXPR_REFERENCE (e2
));
234 pre_expr_d::hash (const value_type
*e
)
239 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e
));
241 return SSA_NAME_VERSION (PRE_EXPR_NAME (e
));
243 return PRE_EXPR_NARY (e
)->hashcode
;
245 return PRE_EXPR_REFERENCE (e
)->hashcode
;
251 /* Next global expression id number. */
252 static unsigned int next_expression_id
;
254 /* Mapping from expression to id number we can use in bitmap sets. */
255 static vec
<pre_expr
> expressions
;
256 static hash_table
<pre_expr_d
> *expression_to_id
;
257 static vec
<unsigned> name_to_id
;
259 /* Allocate an expression id for EXPR. */
261 static inline unsigned int
262 alloc_expression_id (pre_expr expr
)
264 struct pre_expr_d
**slot
;
265 /* Make sure we won't overflow. */
266 gcc_assert (next_expression_id
+ 1 > next_expression_id
);
267 expr
->id
= next_expression_id
++;
268 expressions
.safe_push (expr
);
269 if (expr
->kind
== NAME
)
271 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
272 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
273 re-allocations by using vec::reserve upfront. There is no
274 vec::quick_grow_cleared unfortunately. */
275 unsigned old_len
= name_to_id
.length ();
276 name_to_id
.reserve (num_ssa_names
- old_len
);
277 name_to_id
.safe_grow_cleared (num_ssa_names
);
278 gcc_assert (name_to_id
[version
] == 0);
279 name_to_id
[version
] = expr
->id
;
283 slot
= expression_to_id
->find_slot (expr
, INSERT
);
287 return next_expression_id
- 1;
290 /* Return the expression id for tree EXPR. */
292 static inline unsigned int
293 get_expression_id (const pre_expr expr
)
298 static inline unsigned int
299 lookup_expression_id (const pre_expr expr
)
301 struct pre_expr_d
**slot
;
303 if (expr
->kind
== NAME
)
305 unsigned version
= SSA_NAME_VERSION (PRE_EXPR_NAME (expr
));
306 if (name_to_id
.length () <= version
)
308 return name_to_id
[version
];
312 slot
= expression_to_id
->find_slot (expr
, NO_INSERT
);
315 return ((pre_expr
)*slot
)->id
;
319 /* Return the existing expression id for EXPR, or create one if one
320 does not exist yet. */
322 static inline unsigned int
323 get_or_alloc_expression_id (pre_expr expr
)
325 unsigned int id
= lookup_expression_id (expr
);
327 return alloc_expression_id (expr
);
328 return expr
->id
= id
;
331 /* Return the expression that has expression id ID */
333 static inline pre_expr
334 expression_for_id (unsigned int id
)
336 return expressions
[id
];
339 /* Free the expression id field in all of our expressions,
340 and then destroy the expressions array. */
343 clear_expression_ids (void)
345 expressions
.release ();
348 static alloc_pool pre_expr_pool
;
350 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
353 get_or_alloc_expr_for_name (tree name
)
355 struct pre_expr_d expr
;
357 unsigned int result_id
;
361 PRE_EXPR_NAME (&expr
) = name
;
362 result_id
= lookup_expression_id (&expr
);
364 return expression_for_id (result_id
);
366 result
= (pre_expr
) pool_alloc (pre_expr_pool
);
368 PRE_EXPR_NAME (result
) = name
;
369 alloc_expression_id (result
);
373 /* An unordered bitmap set. One bitmap tracks values, the other,
375 typedef struct bitmap_set
377 bitmap_head expressions
;
381 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
382 EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
384 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
385 EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
387 /* Mapping from value id to expressions with that value_id. */
388 static vec
<bitmap
> value_expressions
;
390 /* Sets that we need to keep track of. */
391 typedef struct bb_bitmap_sets
393 /* The EXP_GEN set, which represents expressions/values generated in
395 bitmap_set_t exp_gen
;
397 /* The PHI_GEN set, which represents PHI results generated in a
399 bitmap_set_t phi_gen
;
401 /* The TMP_GEN set, which represents results/temporaries generated
402 in a basic block. IE the LHS of an expression. */
403 bitmap_set_t tmp_gen
;
405 /* The AVAIL_OUT set, which represents which values are available in
406 a given basic block. */
407 bitmap_set_t avail_out
;
409 /* The ANTIC_IN set, which represents which values are anticipatable
410 in a given basic block. */
411 bitmap_set_t antic_in
;
413 /* The PA_IN set, which represents which values are
414 partially anticipatable in a given basic block. */
417 /* The NEW_SETS set, which is used during insertion to augment the
418 AVAIL_OUT set of blocks with the new insertions performed during
419 the current iteration. */
420 bitmap_set_t new_sets
;
422 /* A cache for value_dies_in_block_x. */
425 /* True if we have visited this block during ANTIC calculation. */
426 unsigned int visited
: 1;
428 /* True we have deferred processing this block during ANTIC
429 calculation until its successor is processed. */
430 unsigned int deferred
: 1;
432 /* True when the block contains a call that might not return. */
433 unsigned int contains_may_not_return_call
: 1;
436 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
437 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
438 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
439 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
440 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
441 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
442 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
443 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
444 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
445 #define BB_DEFERRED(BB) ((bb_value_sets_t) ((BB)->aux))->deferred
446 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
449 /* Basic block list in postorder. */
450 static int *postorder
;
451 static int postorder_num
;
453 /* This structure is used to keep track of statistics on what
454 optimization PRE was able to perform. */
457 /* The number of RHS computations eliminated by PRE. */
460 /* The number of new expressions/temporaries generated by PRE. */
463 /* The number of inserts found due to partial anticipation */
466 /* The number of new PHI nodes added by PRE. */
470 static bool do_partial_partial
;
471 static pre_expr
bitmap_find_leader (bitmap_set_t
, unsigned int);
472 static void bitmap_value_insert_into_set (bitmap_set_t
, pre_expr
);
473 static void bitmap_value_replace_in_set (bitmap_set_t
, pre_expr
);
474 static void bitmap_set_copy (bitmap_set_t
, bitmap_set_t
);
475 static bool bitmap_set_contains_value (bitmap_set_t
, unsigned int);
476 static void bitmap_insert_into_set (bitmap_set_t
, pre_expr
);
477 static void bitmap_insert_into_set_1 (bitmap_set_t
, pre_expr
,
479 static bitmap_set_t
bitmap_set_new (void);
480 static tree
create_expression_by_pieces (basic_block
, pre_expr
, gimple_seq
*,
482 static tree
find_or_generate_expression (basic_block
, tree
, gimple_seq
*);
483 static unsigned int get_expr_value_id (pre_expr
);
485 /* We can add and remove elements and entries to and from sets
486 and hash tables, so we use alloc pools for them. */
488 static alloc_pool bitmap_set_pool
;
489 static bitmap_obstack grand_bitmap_obstack
;
491 /* Set of blocks with statements that have had their EH properties changed. */
492 static bitmap need_eh_cleanup
;
494 /* Set of blocks with statements that have had their AB properties changed. */
495 static bitmap need_ab_cleanup
;
497 /* A three tuple {e, pred, v} used to cache phi translations in the
498 phi_translate_table. */
500 typedef struct expr_pred_trans_d
: typed_free_remove
<expr_pred_trans_d
>
502 /* The expression. */
505 /* The predecessor block along which we translated the expression. */
508 /* The value that resulted from the translation. */
511 /* The hashcode for the expression, pred pair. This is cached for
515 /* hash_table support. */
516 typedef expr_pred_trans_d value_type
;
517 typedef expr_pred_trans_d compare_type
;
518 static inline hashval_t
hash (const value_type
*);
519 static inline int equal (const value_type
*, const compare_type
*);
520 } *expr_pred_trans_t
;
521 typedef const struct expr_pred_trans_d
*const_expr_pred_trans_t
;
524 expr_pred_trans_d::hash (const expr_pred_trans_d
*e
)
530 expr_pred_trans_d::equal (const value_type
*ve1
,
531 const compare_type
*ve2
)
533 basic_block b1
= ve1
->pred
;
534 basic_block b2
= ve2
->pred
;
536 /* If they are not translations for the same basic block, they can't
540 return pre_expr_d::equal (ve1
->e
, ve2
->e
);
543 /* The phi_translate_table caches phi translations for a given
544 expression and predecessor. */
545 static hash_table
<expr_pred_trans_d
> *phi_translate_table
;
547 /* Add the tuple mapping from {expression E, basic block PRED} to
548 the phi translation table and return whether it pre-existed. */
551 phi_trans_add (expr_pred_trans_t
*entry
, pre_expr e
, basic_block pred
)
553 expr_pred_trans_t
*slot
;
554 expr_pred_trans_d tem
;
555 hashval_t hash
= iterative_hash_hashval_t (pre_expr_d::hash (e
),
560 slot
= phi_translate_table
->find_slot_with_hash (&tem
, hash
, INSERT
);
567 *entry
= *slot
= XNEW (struct expr_pred_trans_d
);
569 (*entry
)->pred
= pred
;
570 (*entry
)->hashcode
= hash
;
575 /* Add expression E to the expression set of value id V. */
578 add_to_value (unsigned int v
, pre_expr e
)
582 gcc_checking_assert (get_expr_value_id (e
) == v
);
584 if (v
>= value_expressions
.length ())
586 value_expressions
.safe_grow_cleared (v
+ 1);
589 set
= value_expressions
[v
];
592 set
= BITMAP_ALLOC (&grand_bitmap_obstack
);
593 value_expressions
[v
] = set
;
596 bitmap_set_bit (set
, get_or_alloc_expression_id (e
));
599 /* Create a new bitmap set and return it. */
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
);
610 /* Return the value id for a PRE expression EXPR. */
613 get_expr_value_id (pre_expr expr
)
619 id
= get_constant_value_id (PRE_EXPR_CONSTANT (expr
));
622 id
= VN_INFO (PRE_EXPR_NAME (expr
))->value_id
;
625 id
= PRE_EXPR_NARY (expr
)->value_id
;
628 id
= PRE_EXPR_REFERENCE (expr
)->value_id
;
633 /* ??? We cannot assert that expr has a value-id (it can be 0), because
634 we assign value-ids only to expressions that have a result
635 in set_hashtable_value_ids. */
639 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
642 sccvn_valnum_from_value_id (unsigned int val
)
646 bitmap exprset
= value_expressions
[val
];
647 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
649 pre_expr vexpr
= expression_for_id (i
);
650 if (vexpr
->kind
== NAME
)
651 return VN_INFO (PRE_EXPR_NAME (vexpr
))->valnum
;
652 else if (vexpr
->kind
== CONSTANT
)
653 return PRE_EXPR_CONSTANT (vexpr
);
658 /* Remove an expression EXPR from a bitmapped set. */
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
));
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. */
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. */
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. */
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. */
714 sorted_array_from_bitmap_set (bitmap_set_t set
)
717 bitmap_iterator bi
, bj
;
718 vec
<pre_expr
> result
;
720 /* Pre-allocate roughly enough space for the array. */
721 result
.create (bitmap_count_bits (&set
->values
));
723 FOR_EACH_VALUE_ID_IN_SET (set
, i
, bi
)
725 /* The number of expressions having a given value is usually
726 relatively small. Thus, rather than making a vector of all
727 the expressions and sorting it by value-id, we walk the values
728 and check in the reverse mapping that tells us what expressions
729 have a given value, to filter those in our set. As a result,
730 the expressions are inserted in value-id order, which means
733 If this is somehow a significant lose for some cases, we can
734 choose which set to walk based on the set size. */
735 bitmap exprset
= value_expressions
[i
];
736 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, j
, bj
)
738 if (bitmap_bit_p (&set
->expressions
, j
))
739 result
.safe_push (expression_for_id (j
));
746 /* Perform bitmapped set operation DEST &= ORIG. */
749 bitmap_set_and (bitmap_set_t dest
, bitmap_set_t orig
)
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. */
775 bitmap_set_subtract (bitmap_set_t dest
, bitmap_set_t orig
)
777 bitmap_set_t result
= bitmap_set_new ();
781 bitmap_and_compl (&result
->expressions
, &dest
->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
);
794 /* Subtract all the values in bitmap set B from bitmap set A. */
797 bitmap_set_subtract_values (bitmap_set_t a
, bitmap_set_t b
)
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. */
819 bitmap_set_contains_value (bitmap_set_t set
, unsigned int value_id
)
821 if (value_id_constant_p (value_id
))
824 if (!set
|| bitmap_empty_p (&set
->expressions
))
827 return bitmap_bit_p (&set
->values
, value_id
);
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. */
839 bitmap_set_replace_value (bitmap_set_t set
, unsigned int lookfor
,
846 if (value_id_constant_p (lookfor
))
849 if (!bitmap_set_contains_value (set
, lookfor
))
852 /* The number of expressions having a given value is usually
853 significantly less than the total number of expressions in SET.
854 Thus, rather than check, for each expression in SET, whether it
855 has the value LOOKFOR, we walk the reverse mapping that tells us
856 what expressions have a given value, and see if any of those
857 expressions are in our set. For large testcases, this is about
858 5-10x faster than walking the bitmap. If this is somehow a
859 significant lose for some cases, we can choose which set to walk
860 based on the set size. */
861 exprset
= value_expressions
[lookfor
];
862 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
864 if (bitmap_clear_bit (&set
->expressions
, i
))
866 bitmap_set_bit (&set
->expressions
, get_expression_id (expr
));
874 /* Return true if two bitmap sets are equal. */
877 bitmap_set_equal (bitmap_set_t a
, bitmap_set_t b
)
879 return bitmap_equal_p (&a
->values
, &b
->values
);
882 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
883 and add it otherwise. */
886 bitmap_value_replace_in_set (bitmap_set_t set
, pre_expr expr
)
888 unsigned int val
= get_expr_value_id (expr
);
890 if (bitmap_set_contains_value (set
, val
))
891 bitmap_set_replace_value (set
, val
, expr
);
893 bitmap_insert_into_set (set
, expr
);
896 /* Insert EXPR into SET if EXPR's value is not already present in
900 bitmap_value_insert_into_set (bitmap_set_t set
, pre_expr expr
)
902 unsigned int val
= get_expr_value_id (expr
);
904 gcc_checking_assert (expr
->id
== get_or_alloc_expression_id (expr
));
906 /* Constant values are always considered to be part of the set. */
907 if (value_id_constant_p (val
))
910 /* If the value membership changed, add the expression. */
911 if (bitmap_set_bit (&set
->values
, val
))
912 bitmap_set_bit (&set
->expressions
, expr
->id
);
915 /* Print out EXPR to outfile. */
918 print_pre_expr (FILE *outfile
, const pre_expr expr
)
923 print_generic_expr (outfile
, PRE_EXPR_CONSTANT (expr
), 0);
926 print_generic_expr (outfile
, PRE_EXPR_NAME (expr
), 0);
931 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
932 fprintf (outfile
, "{%s,", get_tree_code_name (nary
->opcode
));
933 for (i
= 0; i
< nary
->length
; i
++)
935 print_generic_expr (outfile
, nary
->op
[i
], 0);
936 if (i
!= (unsigned) nary
->length
- 1)
937 fprintf (outfile
, ",");
939 fprintf (outfile
, "}");
945 vn_reference_op_t vro
;
947 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
948 fprintf (outfile
, "{");
950 ref
->operands
.iterate (i
, &vro
);
953 bool closebrace
= false;
954 if (vro
->opcode
!= SSA_NAME
955 && TREE_CODE_CLASS (vro
->opcode
) != tcc_declaration
)
957 fprintf (outfile
, "%s", get_tree_code_name (vro
->opcode
));
960 fprintf (outfile
, "<");
966 print_generic_expr (outfile
, vro
->op0
, 0);
969 fprintf (outfile
, ",");
970 print_generic_expr (outfile
, vro
->op1
, 0);
974 fprintf (outfile
, ",");
975 print_generic_expr (outfile
, vro
->op2
, 0);
979 fprintf (outfile
, ">");
980 if (i
!= ref
->operands
.length () - 1)
981 fprintf (outfile
, ",");
983 fprintf (outfile
, "}");
986 fprintf (outfile
, "@");
987 print_generic_expr (outfile
, ref
->vuse
, 0);
993 void debug_pre_expr (pre_expr
);
995 /* Like print_pre_expr but always prints to stderr. */
997 debug_pre_expr (pre_expr e
)
999 print_pre_expr (stderr
, e
);
1000 fprintf (stderr
, "\n");
1003 /* Print out SET to OUTFILE. */
1006 print_bitmap_set (FILE *outfile
, bitmap_set_t set
,
1007 const char *setname
, int blockindex
)
1009 fprintf (outfile
, "%s[%d] := { ", setname
, blockindex
);
1016 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
1018 const pre_expr expr
= expression_for_id (i
);
1021 fprintf (outfile
, ", ");
1023 print_pre_expr (outfile
, expr
);
1025 fprintf (outfile
, " (%04d)", get_expr_value_id (expr
));
1028 fprintf (outfile
, " }\n");
1031 void debug_bitmap_set (bitmap_set_t
);
1034 debug_bitmap_set (bitmap_set_t set
)
1036 print_bitmap_set (stderr
, set
, "debug", 0);
1039 void debug_bitmap_sets_for (basic_block
);
1042 debug_bitmap_sets_for (basic_block bb
)
1044 print_bitmap_set (stderr
, AVAIL_OUT (bb
), "avail_out", bb
->index
);
1045 print_bitmap_set (stderr
, EXP_GEN (bb
), "exp_gen", bb
->index
);
1046 print_bitmap_set (stderr
, PHI_GEN (bb
), "phi_gen", bb
->index
);
1047 print_bitmap_set (stderr
, TMP_GEN (bb
), "tmp_gen", bb
->index
);
1048 print_bitmap_set (stderr
, ANTIC_IN (bb
), "antic_in", bb
->index
);
1049 if (do_partial_partial
)
1050 print_bitmap_set (stderr
, PA_IN (bb
), "pa_in", bb
->index
);
1051 print_bitmap_set (stderr
, NEW_SETS (bb
), "new_sets", bb
->index
);
1054 /* Print out the expressions that have VAL to OUTFILE. */
1057 print_value_expressions (FILE *outfile
, unsigned int val
)
1059 bitmap set
= value_expressions
[val
];
1064 sprintf (s
, "%04d", val
);
1065 x
.expressions
= *set
;
1066 print_bitmap_set (outfile
, &x
, s
, 0);
1072 debug_value_expressions (unsigned int val
)
1074 print_value_expressions (stderr
, val
);
1077 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1081 get_or_alloc_expr_for_constant (tree constant
)
1083 unsigned int result_id
;
1084 unsigned int value_id
;
1085 struct pre_expr_d expr
;
1088 expr
.kind
= CONSTANT
;
1089 PRE_EXPR_CONSTANT (&expr
) = constant
;
1090 result_id
= lookup_expression_id (&expr
);
1092 return expression_for_id (result_id
);
1094 newexpr
= (pre_expr
) pool_alloc (pre_expr_pool
);
1095 newexpr
->kind
= CONSTANT
;
1096 PRE_EXPR_CONSTANT (newexpr
) = constant
;
1097 alloc_expression_id (newexpr
);
1098 value_id
= get_or_alloc_constant_value_id (constant
);
1099 add_to_value (value_id
, newexpr
);
1103 /* Given a value id V, find the actual tree representing the constant
1104 value if there is one, and return it. Return NULL if we can't find
1108 get_constant_for_value_id (unsigned int v
)
1110 if (value_id_constant_p (v
))
1114 bitmap exprset
= value_expressions
[v
];
1116 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1118 pre_expr expr
= expression_for_id (i
);
1119 if (expr
->kind
== CONSTANT
)
1120 return PRE_EXPR_CONSTANT (expr
);
1126 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1127 Currently only supports constants and SSA_NAMES. */
1129 get_or_alloc_expr_for (tree t
)
1131 if (TREE_CODE (t
) == SSA_NAME
)
1132 return get_or_alloc_expr_for_name (t
);
1133 else if (is_gimple_min_invariant (t
))
1134 return get_or_alloc_expr_for_constant (t
);
1137 /* More complex expressions can result from SCCVN expression
1138 simplification that inserts values for them. As they all
1139 do not have VOPs the get handled by the nary ops struct. */
1140 vn_nary_op_t result
;
1141 unsigned int result_id
;
1142 vn_nary_op_lookup (t
, &result
);
1145 pre_expr e
= (pre_expr
) pool_alloc (pre_expr_pool
);
1147 PRE_EXPR_NARY (e
) = result
;
1148 result_id
= lookup_expression_id (e
);
1151 pool_free (pre_expr_pool
, e
);
1152 e
= expression_for_id (result_id
);
1155 alloc_expression_id (e
);
1162 /* Return the folded version of T if T, when folded, is a gimple
1163 min_invariant. Otherwise, return T. */
1166 fully_constant_expression (pre_expr e
)
1174 vn_nary_op_t nary
= PRE_EXPR_NARY (e
);
1175 switch (TREE_CODE_CLASS (nary
->opcode
))
1178 case tcc_comparison
:
1180 /* We have to go from trees to pre exprs to value ids to
1182 tree naryop0
= nary
->op
[0];
1183 tree naryop1
= nary
->op
[1];
1185 if (!is_gimple_min_invariant (naryop0
))
1187 pre_expr rep0
= get_or_alloc_expr_for (naryop0
);
1188 unsigned int vrep0
= get_expr_value_id (rep0
);
1189 tree const0
= get_constant_for_value_id (vrep0
);
1191 naryop0
= fold_convert (TREE_TYPE (naryop0
), const0
);
1193 if (!is_gimple_min_invariant (naryop1
))
1195 pre_expr rep1
= get_or_alloc_expr_for (naryop1
);
1196 unsigned int vrep1
= get_expr_value_id (rep1
);
1197 tree const1
= get_constant_for_value_id (vrep1
);
1199 naryop1
= fold_convert (TREE_TYPE (naryop1
), const1
);
1201 result
= fold_binary (nary
->opcode
, nary
->type
,
1203 if (result
&& is_gimple_min_invariant (result
))
1204 return get_or_alloc_expr_for_constant (result
);
1205 /* We might have simplified the expression to a
1206 SSA_NAME for example from x_1 * 1. But we cannot
1207 insert a PHI for x_1 unconditionally as x_1 might
1208 not be available readily. */
1212 if (nary
->opcode
!= REALPART_EXPR
1213 && nary
->opcode
!= IMAGPART_EXPR
1214 && nary
->opcode
!= VIEW_CONVERT_EXPR
)
1219 /* We have to go from trees to pre exprs to value ids to
1221 tree naryop0
= nary
->op
[0];
1222 tree const0
, result
;
1223 if (is_gimple_min_invariant (naryop0
))
1227 pre_expr rep0
= get_or_alloc_expr_for (naryop0
);
1228 unsigned int vrep0
= get_expr_value_id (rep0
);
1229 const0
= get_constant_for_value_id (vrep0
);
1234 tree type1
= TREE_TYPE (nary
->op
[0]);
1235 const0
= fold_convert (type1
, const0
);
1236 result
= fold_unary (nary
->opcode
, nary
->type
, const0
);
1238 if (result
&& is_gimple_min_invariant (result
))
1239 return get_or_alloc_expr_for_constant (result
);
1248 vn_reference_t ref
= PRE_EXPR_REFERENCE (e
);
1250 if ((folded
= fully_constant_vn_reference_p (ref
)))
1251 return get_or_alloc_expr_for_constant (folded
);
1260 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1261 it has the value it would have in BLOCK. Set *SAME_VALID to true
1262 in case the new vuse doesn't change the value id of the OPERANDS. */
1265 translate_vuse_through_block (vec
<vn_reference_op_s
> operands
,
1266 alias_set_type set
, tree type
, tree vuse
,
1267 basic_block phiblock
,
1268 basic_block block
, bool *same_valid
)
1270 gimple phi
= SSA_NAME_DEF_STMT (vuse
);
1277 if (gimple_bb (phi
) != phiblock
)
1280 use_oracle
= ao_ref_init_from_vn_reference (&ref
, set
, type
, operands
);
1282 /* Use the alias-oracle to find either the PHI node in this block,
1283 the first VUSE used in this block that is equivalent to vuse or
1284 the first VUSE which definition in this block kills the value. */
1285 if (gimple_code (phi
) == GIMPLE_PHI
)
1286 e
= find_edge (block
, phiblock
);
1287 else if (use_oracle
)
1288 while (!stmt_may_clobber_ref_p_1 (phi
, &ref
))
1290 vuse
= gimple_vuse (phi
);
1291 phi
= SSA_NAME_DEF_STMT (vuse
);
1292 if (gimple_bb (phi
) != phiblock
)
1294 if (gimple_code (phi
) == GIMPLE_PHI
)
1296 e
= find_edge (block
, phiblock
);
1307 bitmap visited
= NULL
;
1309 /* Try to find a vuse that dominates this phi node by skipping
1310 non-clobbering statements. */
1311 vuse
= get_continuation_for_phi (phi
, &ref
, &cnt
, &visited
, false,
1314 BITMAP_FREE (visited
);
1320 /* If we didn't find any, the value ID can't stay the same,
1321 but return the translated vuse. */
1322 *same_valid
= false;
1323 vuse
= PHI_ARG_DEF (phi
, e
->dest_idx
);
1325 /* ??? We would like to return vuse here as this is the canonical
1326 upmost vdef that this reference is associated with. But during
1327 insertion of the references into the hash tables we only ever
1328 directly insert with their direct gimple_vuse, hence returning
1329 something else would make us not find the other expression. */
1330 return PHI_ARG_DEF (phi
, e
->dest_idx
);
1336 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1337 SET2. This is used to avoid making a set consisting of the union
1338 of PA_IN and ANTIC_IN during insert. */
1340 static inline pre_expr
1341 find_leader_in_sets (unsigned int val
, bitmap_set_t set1
, bitmap_set_t set2
)
1345 result
= bitmap_find_leader (set1
, val
);
1346 if (!result
&& set2
)
1347 result
= bitmap_find_leader (set2
, val
);
1351 /* Get the tree type for our PRE expression e. */
1354 get_expr_type (const pre_expr e
)
1359 return TREE_TYPE (PRE_EXPR_NAME (e
));
1361 return TREE_TYPE (PRE_EXPR_CONSTANT (e
));
1363 return PRE_EXPR_REFERENCE (e
)->type
;
1365 return PRE_EXPR_NARY (e
)->type
;
1370 /* Get a representative SSA_NAME for a given expression.
1371 Since all of our sub-expressions are treated as values, we require
1372 them to be SSA_NAME's for simplicity.
1373 Prior versions of GVNPRE used to use "value handles" here, so that
1374 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1375 either case, the operands are really values (IE we do not expect
1376 them to be usable without finding leaders). */
1379 get_representative_for (const pre_expr e
)
1382 unsigned int value_id
= get_expr_value_id (e
);
1387 return PRE_EXPR_NAME (e
);
1389 return PRE_EXPR_CONSTANT (e
);
1393 /* Go through all of the expressions representing this value
1394 and pick out an SSA_NAME. */
1397 bitmap exprs
= value_expressions
[value_id
];
1398 EXECUTE_IF_SET_IN_BITMAP (exprs
, 0, i
, bi
)
1400 pre_expr rep
= expression_for_id (i
);
1401 if (rep
->kind
== NAME
)
1402 return PRE_EXPR_NAME (rep
);
1403 else if (rep
->kind
== CONSTANT
)
1404 return PRE_EXPR_CONSTANT (rep
);
1410 /* If we reached here we couldn't find an SSA_NAME. This can
1411 happen when we've discovered a value that has never appeared in
1412 the program as set to an SSA_NAME, as the result of phi translation.
1414 ??? We should be able to re-use this when we insert the statement
1416 name
= make_temp_ssa_name (get_expr_type (e
), gimple_build_nop (), "pretmp");
1417 VN_INFO_GET (name
)->value_id
= value_id
;
1418 VN_INFO (name
)->valnum
= name
;
1419 /* ??? For now mark this SSA name for release by SCCVN. */
1420 VN_INFO (name
)->needs_insertion
= true;
1421 add_to_value (value_id
, get_or_alloc_expr_for_name (name
));
1422 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
1424 fprintf (dump_file
, "Created SSA_NAME representative ");
1425 print_generic_expr (dump_file
, name
, 0);
1426 fprintf (dump_file
, " for expression:");
1427 print_pre_expr (dump_file
, e
);
1428 fprintf (dump_file
, " (%04d)\n", value_id
);
1437 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1438 basic_block pred
, basic_block phiblock
);
1440 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1441 the phis in PRED. Return NULL if we can't find a leader for each part
1442 of the translated expression. */
1445 phi_translate_1 (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1446 basic_block pred
, basic_block phiblock
)
1453 bool changed
= false;
1454 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1455 vn_nary_op_t newnary
= XALLOCAVAR (struct vn_nary_op_s
,
1456 sizeof_vn_nary_op (nary
->length
));
1457 memcpy (newnary
, nary
, sizeof_vn_nary_op (nary
->length
));
1459 for (i
= 0; i
< newnary
->length
; i
++)
1461 if (TREE_CODE (newnary
->op
[i
]) != SSA_NAME
)
1465 pre_expr leader
, result
;
1466 unsigned int op_val_id
= VN_INFO (newnary
->op
[i
])->value_id
;
1467 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1468 result
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1469 if (result
&& result
!= leader
)
1471 tree name
= get_representative_for (result
);
1474 newnary
->op
[i
] = name
;
1479 changed
|= newnary
->op
[i
] != nary
->op
[i
];
1485 unsigned int new_val_id
;
1487 tree result
= vn_nary_op_lookup_pieces (newnary
->length
,
1492 if (result
&& is_gimple_min_invariant (result
))
1493 return get_or_alloc_expr_for_constant (result
);
1495 expr
= (pre_expr
) pool_alloc (pre_expr_pool
);
1500 PRE_EXPR_NARY (expr
) = nary
;
1501 constant
= fully_constant_expression (expr
);
1502 if (constant
!= expr
)
1505 new_val_id
= nary
->value_id
;
1506 get_or_alloc_expression_id (expr
);
1510 new_val_id
= get_next_value_id ();
1511 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
1512 nary
= vn_nary_op_insert_pieces (newnary
->length
,
1516 result
, new_val_id
);
1517 PRE_EXPR_NARY (expr
) = nary
;
1518 constant
= fully_constant_expression (expr
);
1519 if (constant
!= expr
)
1521 get_or_alloc_expression_id (expr
);
1523 add_to_value (new_val_id
, expr
);
1531 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1532 vec
<vn_reference_op_s
> operands
= ref
->operands
;
1533 tree vuse
= ref
->vuse
;
1534 tree newvuse
= vuse
;
1535 vec
<vn_reference_op_s
> newoperands
= vNULL
;
1536 bool changed
= false, same_valid
= true;
1537 unsigned int i
, j
, n
;
1538 vn_reference_op_t operand
;
1539 vn_reference_t newref
;
1542 operands
.iterate (i
, &operand
); i
++, j
++)
1547 tree type
= operand
->type
;
1548 vn_reference_op_s newop
= *operand
;
1549 op
[0] = operand
->op0
;
1550 op
[1] = operand
->op1
;
1551 op
[2] = operand
->op2
;
1552 for (n
= 0; n
< 3; ++n
)
1554 unsigned int op_val_id
;
1557 if (TREE_CODE (op
[n
]) != SSA_NAME
)
1559 /* We can't possibly insert these. */
1561 && !is_gimple_min_invariant (op
[n
]))
1565 op_val_id
= VN_INFO (op
[n
])->value_id
;
1566 leader
= find_leader_in_sets (op_val_id
, set1
, set2
);
1569 opresult
= phi_translate (leader
, set1
, set2
, pred
, phiblock
);
1572 if (opresult
!= leader
)
1574 tree name
= get_representative_for (opresult
);
1577 changed
|= name
!= op
[n
];
1583 newoperands
.release ();
1586 if (!newoperands
.exists ())
1587 newoperands
= operands
.copy ();
1588 /* We may have changed from an SSA_NAME to a constant */
1589 if (newop
.opcode
== SSA_NAME
&& TREE_CODE (op
[0]) != SSA_NAME
)
1590 newop
.opcode
= TREE_CODE (op
[0]);
1595 /* If it transforms a non-constant ARRAY_REF into a constant
1596 one, adjust the constant offset. */
1597 if (newop
.opcode
== ARRAY_REF
1599 && TREE_CODE (op
[0]) == INTEGER_CST
1600 && TREE_CODE (op
[1]) == INTEGER_CST
1601 && TREE_CODE (op
[2]) == INTEGER_CST
)
1603 offset_int off
= ((wi::to_offset (op
[0])
1604 - wi::to_offset (op
[1]))
1605 * wi::to_offset (op
[2]));
1606 if (wi::fits_shwi_p (off
))
1607 newop
.off
= off
.to_shwi ();
1609 newoperands
[j
] = newop
;
1610 /* If it transforms from an SSA_NAME to an address, fold with
1611 a preceding indirect reference. */
1612 if (j
> 0 && op
[0] && TREE_CODE (op
[0]) == ADDR_EXPR
1613 && newoperands
[j
- 1].opcode
== MEM_REF
)
1614 vn_reference_fold_indirect (&newoperands
, &j
);
1616 if (i
!= operands
.length ())
1618 newoperands
.release ();
1624 newvuse
= translate_vuse_through_block (newoperands
,
1625 ref
->set
, ref
->type
,
1626 vuse
, phiblock
, pred
,
1628 if (newvuse
== NULL_TREE
)
1630 newoperands
.release ();
1635 if (changed
|| newvuse
!= vuse
)
1637 unsigned int new_val_id
;
1640 tree result
= vn_reference_lookup_pieces (newvuse
, ref
->set
,
1645 newoperands
.release ();
1647 /* We can always insert constants, so if we have a partial
1648 redundant constant load of another type try to translate it
1649 to a constant of appropriate type. */
1650 if (result
&& is_gimple_min_invariant (result
))
1653 if (!useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1655 tem
= fold_unary (VIEW_CONVERT_EXPR
, ref
->type
, result
);
1656 if (tem
&& !is_gimple_min_invariant (tem
))
1660 return get_or_alloc_expr_for_constant (tem
);
1663 /* If we'd have to convert things we would need to validate
1664 if we can insert the translated expression. So fail
1665 here for now - we cannot insert an alias with a different
1666 type in the VN tables either, as that would assert. */
1668 && !useless_type_conversion_p (ref
->type
, TREE_TYPE (result
)))
1670 else if (!result
&& newref
1671 && !useless_type_conversion_p (ref
->type
, newref
->type
))
1673 newoperands
.release ();
1677 expr
= (pre_expr
) pool_alloc (pre_expr_pool
);
1678 expr
->kind
= REFERENCE
;
1683 PRE_EXPR_REFERENCE (expr
) = newref
;
1684 constant
= fully_constant_expression (expr
);
1685 if (constant
!= expr
)
1688 new_val_id
= newref
->value_id
;
1689 get_or_alloc_expression_id (expr
);
1693 if (changed
|| !same_valid
)
1695 new_val_id
= get_next_value_id ();
1696 value_expressions
.safe_grow_cleared
1697 (get_max_value_id () + 1);
1700 new_val_id
= ref
->value_id
;
1701 newref
= vn_reference_insert_pieces (newvuse
, ref
->set
,
1704 result
, new_val_id
);
1705 newoperands
.create (0);
1706 PRE_EXPR_REFERENCE (expr
) = newref
;
1707 constant
= fully_constant_expression (expr
);
1708 if (constant
!= expr
)
1710 get_or_alloc_expression_id (expr
);
1712 add_to_value (new_val_id
, expr
);
1714 newoperands
.release ();
1721 tree name
= PRE_EXPR_NAME (expr
);
1722 gimple def_stmt
= SSA_NAME_DEF_STMT (name
);
1723 /* If the SSA name is defined by a PHI node in this block,
1725 if (gimple_code (def_stmt
) == GIMPLE_PHI
1726 && gimple_bb (def_stmt
) == phiblock
)
1728 edge e
= find_edge (pred
, gimple_bb (def_stmt
));
1729 tree def
= PHI_ARG_DEF (def_stmt
, e
->dest_idx
);
1731 /* Handle constant. */
1732 if (is_gimple_min_invariant (def
))
1733 return get_or_alloc_expr_for_constant (def
);
1735 return get_or_alloc_expr_for_name (def
);
1737 /* Otherwise return it unchanged - it will get cleaned if its
1738 value is not available in PREDs AVAIL_OUT set of expressions. */
1747 /* Wrapper around phi_translate_1 providing caching functionality. */
1750 phi_translate (pre_expr expr
, bitmap_set_t set1
, bitmap_set_t set2
,
1751 basic_block pred
, basic_block phiblock
)
1753 expr_pred_trans_t slot
= NULL
;
1759 /* Constants contain no values that need translation. */
1760 if (expr
->kind
== CONSTANT
)
1763 if (value_id_constant_p (get_expr_value_id (expr
)))
1766 /* Don't add translations of NAMEs as those are cheap to translate. */
1767 if (expr
->kind
!= NAME
)
1769 if (phi_trans_add (&slot
, expr
, pred
))
1771 /* Store NULL for the value we want to return in the case of
1777 phitrans
= phi_translate_1 (expr
, set1
, set2
, pred
, phiblock
);
1784 /* Remove failed translations again, they cause insert
1785 iteration to not pick up new opportunities reliably. */
1786 phi_translate_table
->remove_elt_with_hash (slot
, slot
->hashcode
);
1793 /* For each expression in SET, translate the values through phi nodes
1794 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1795 expressions in DEST. */
1798 phi_translate_set (bitmap_set_t dest
, bitmap_set_t set
, basic_block pred
,
1799 basic_block phiblock
)
1801 vec
<pre_expr
> exprs
;
1805 if (gimple_seq_empty_p (phi_nodes (phiblock
)))
1807 bitmap_set_copy (dest
, set
);
1811 exprs
= sorted_array_from_bitmap_set (set
);
1812 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
1814 pre_expr translated
;
1815 translated
= phi_translate (expr
, set
, NULL
, pred
, phiblock
);
1819 /* We might end up with multiple expressions from SET being
1820 translated to the same value. In this case we do not want
1821 to retain the NARY or REFERENCE expression but prefer a NAME
1822 which would be the leader. */
1823 if (translated
->kind
== NAME
)
1824 bitmap_value_replace_in_set (dest
, translated
);
1826 bitmap_value_insert_into_set (dest
, translated
);
1831 /* Find the leader for a value (i.e., the name representing that
1832 value) in a given set, and return it. Return NULL if no leader
1836 bitmap_find_leader (bitmap_set_t set
, unsigned int val
)
1838 if (value_id_constant_p (val
))
1842 bitmap exprset
= value_expressions
[val
];
1844 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
1846 pre_expr expr
= expression_for_id (i
);
1847 if (expr
->kind
== CONSTANT
)
1851 if (bitmap_set_contains_value (set
, val
))
1853 /* Rather than walk the entire bitmap of expressions, and see
1854 whether any of them has the value we are looking for, we look
1855 at the reverse mapping, which tells us the set of expressions
1856 that have a given value (IE value->expressions with that
1857 value) and see if any of those expressions are in our set.
1858 The number of expressions per value is usually significantly
1859 less than the number of expressions in the set. In fact, for
1860 large testcases, doing it this way is roughly 5-10x faster
1861 than walking the bitmap.
1862 If this is somehow a significant lose for some cases, we can
1863 choose which set to walk based on which set is smaller. */
1866 bitmap exprset
= value_expressions
[val
];
1868 EXECUTE_IF_AND_IN_BITMAP (exprset
, &set
->expressions
, 0, i
, bi
)
1869 return expression_for_id (i
);
1874 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1875 BLOCK by seeing if it is not killed in the block. Note that we are
1876 only determining whether there is a store that kills it. Because
1877 of the order in which clean iterates over values, we are guaranteed
1878 that altered operands will have caused us to be eliminated from the
1879 ANTIC_IN set already. */
1882 value_dies_in_block_x (pre_expr expr
, basic_block block
)
1884 tree vuse
= PRE_EXPR_REFERENCE (expr
)->vuse
;
1885 vn_reference_t refx
= PRE_EXPR_REFERENCE (expr
);
1887 gimple_stmt_iterator gsi
;
1888 unsigned id
= get_expression_id (expr
);
1895 /* Lookup a previously calculated result. */
1896 if (EXPR_DIES (block
)
1897 && bitmap_bit_p (EXPR_DIES (block
), id
* 2))
1898 return bitmap_bit_p (EXPR_DIES (block
), id
* 2 + 1);
1900 /* A memory expression {e, VUSE} dies in the block if there is a
1901 statement that may clobber e. If, starting statement walk from the
1902 top of the basic block, a statement uses VUSE there can be no kill
1903 inbetween that use and the original statement that loaded {e, VUSE},
1904 so we can stop walking. */
1905 ref
.base
= NULL_TREE
;
1906 for (gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
1908 tree def_vuse
, def_vdef
;
1909 def
= gsi_stmt (gsi
);
1910 def_vuse
= gimple_vuse (def
);
1911 def_vdef
= gimple_vdef (def
);
1913 /* Not a memory statement. */
1917 /* Not a may-def. */
1920 /* A load with the same VUSE, we're done. */
1921 if (def_vuse
== vuse
)
1927 /* Init ref only if we really need it. */
1928 if (ref
.base
== NULL_TREE
1929 && !ao_ref_init_from_vn_reference (&ref
, refx
->set
, refx
->type
,
1935 /* If the statement may clobber expr, it dies. */
1936 if (stmt_may_clobber_ref_p_1 (def
, &ref
))
1943 /* Remember the result. */
1944 if (!EXPR_DIES (block
))
1945 EXPR_DIES (block
) = BITMAP_ALLOC (&grand_bitmap_obstack
);
1946 bitmap_set_bit (EXPR_DIES (block
), id
* 2);
1948 bitmap_set_bit (EXPR_DIES (block
), id
* 2 + 1);
1954 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1955 contains its value-id. */
1958 op_valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, tree op
)
1960 if (op
&& TREE_CODE (op
) == SSA_NAME
)
1962 unsigned int value_id
= VN_INFO (op
)->value_id
;
1963 if (!(bitmap_set_contains_value (set1
, value_id
)
1964 || (set2
&& bitmap_set_contains_value (set2
, value_id
))))
1970 /* Determine if the expression EXPR is valid in SET1 U SET2.
1971 ONLY SET2 CAN BE NULL.
1972 This means that we have a leader for each part of the expression
1973 (if it consists of values), or the expression is an SSA_NAME.
1974 For loads/calls, we also see if the vuse is killed in this block. */
1977 valid_in_sets (bitmap_set_t set1
, bitmap_set_t set2
, pre_expr expr
,
1983 return bitmap_find_leader (AVAIL_OUT (block
),
1984 get_expr_value_id (expr
)) != NULL
;
1988 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
1989 for (i
= 0; i
< nary
->length
; i
++)
1990 if (!op_valid_in_sets (set1
, set2
, nary
->op
[i
]))
1997 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
1998 vn_reference_op_t vro
;
2001 FOR_EACH_VEC_ELT (ref
->operands
, i
, vro
)
2003 if (!op_valid_in_sets (set1
, set2
, vro
->op0
)
2004 || !op_valid_in_sets (set1
, set2
, vro
->op1
)
2005 || !op_valid_in_sets (set1
, set2
, vro
->op2
))
2015 /* Clean the set of expressions that are no longer valid in SET1 or
2016 SET2. This means expressions that are made up of values we have no
2017 leaders for in SET1 or SET2. This version is used for partial
2018 anticipation, which means it is not valid in either ANTIC_IN or
2022 dependent_clean (bitmap_set_t set1
, bitmap_set_t set2
, basic_block block
)
2024 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set1
);
2028 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2030 if (!valid_in_sets (set1
, set2
, expr
, block
))
2031 bitmap_remove_from_set (set1
, expr
);
2036 /* Clean the set of expressions that are no longer valid in SET. This
2037 means expressions that are made up of values we have no leaders for
2041 clean (bitmap_set_t set
, basic_block block
)
2043 vec
<pre_expr
> exprs
= sorted_array_from_bitmap_set (set
);
2047 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
2049 if (!valid_in_sets (set
, NULL
, expr
, block
))
2050 bitmap_remove_from_set (set
, expr
);
2055 /* Clean the set of expressions that are no longer valid in SET because
2056 they are clobbered in BLOCK or because they trap and may not be executed. */
2059 prune_clobbered_mems (bitmap_set_t set
, basic_block block
)
2064 FOR_EACH_EXPR_ID_IN_SET (set
, i
, bi
)
2066 pre_expr expr
= expression_for_id (i
);
2067 if (expr
->kind
== REFERENCE
)
2069 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2072 gimple def_stmt
= SSA_NAME_DEF_STMT (ref
->vuse
);
2073 if (!gimple_nop_p (def_stmt
)
2074 && ((gimple_bb (def_stmt
) != block
2075 && !dominated_by_p (CDI_DOMINATORS
,
2076 block
, gimple_bb (def_stmt
)))
2077 || (gimple_bb (def_stmt
) == block
2078 && value_dies_in_block_x (expr
, block
))))
2079 bitmap_remove_from_set (set
, expr
);
2082 else if (expr
->kind
== NARY
)
2084 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2085 /* If the NARY may trap make sure the block does not contain
2086 a possible exit point.
2087 ??? This is overly conservative if we translate AVAIL_OUT
2088 as the available expression might be after the exit point. */
2089 if (BB_MAY_NOTRETURN (block
)
2090 && vn_nary_may_trap (nary
))
2091 bitmap_remove_from_set (set
, expr
);
2096 static sbitmap has_abnormal_preds
;
2098 /* List of blocks that may have changed during ANTIC computation and
2099 thus need to be iterated over. */
2101 static sbitmap changed_blocks
;
2103 /* Decide whether to defer a block for a later iteration, or PHI
2104 translate SOURCE to DEST using phis in PHIBLOCK. Return false if we
2105 should defer the block, and true if we processed it. */
2108 defer_or_phi_translate_block (bitmap_set_t dest
, bitmap_set_t source
,
2109 basic_block block
, basic_block phiblock
)
2111 if (!BB_VISITED (phiblock
))
2113 bitmap_set_bit (changed_blocks
, block
->index
);
2114 BB_VISITED (block
) = 0;
2115 BB_DEFERRED (block
) = 1;
2119 phi_translate_set (dest
, source
, block
, phiblock
);
2123 /* Compute the ANTIC set for BLOCK.
2125 If succs(BLOCK) > 1 then
2126 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2127 else if succs(BLOCK) == 1 then
2128 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2130 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2134 compute_antic_aux (basic_block block
, bool block_has_abnormal_pred_edge
)
2136 bool changed
= false;
2137 bitmap_set_t S
, old
, ANTIC_OUT
;
2143 old
= ANTIC_OUT
= S
= NULL
;
2144 BB_VISITED (block
) = 1;
2146 /* If any edges from predecessors are abnormal, antic_in is empty,
2148 if (block_has_abnormal_pred_edge
)
2149 goto maybe_dump_sets
;
2151 old
= ANTIC_IN (block
);
2152 ANTIC_OUT
= bitmap_set_new ();
2154 /* If the block has no successors, ANTIC_OUT is empty. */
2155 if (EDGE_COUNT (block
->succs
) == 0)
2157 /* If we have one successor, we could have some phi nodes to
2158 translate through. */
2159 else if (single_succ_p (block
))
2161 basic_block succ_bb
= single_succ (block
);
2163 /* We trade iterations of the dataflow equations for having to
2164 phi translate the maximal set, which is incredibly slow
2165 (since the maximal set often has 300+ members, even when you
2166 have a small number of blocks).
2167 Basically, we defer the computation of ANTIC for this block
2168 until we have processed it's successor, which will inevitably
2169 have a *much* smaller set of values to phi translate once
2170 clean has been run on it.
2171 The cost of doing this is that we technically perform more
2172 iterations, however, they are lower cost iterations.
2174 Timings for PRE on tramp3d-v4:
2175 without maximal set fix: 11 seconds
2176 with maximal set fix/without deferring: 26 seconds
2177 with maximal set fix/with deferring: 11 seconds
2180 if (!defer_or_phi_translate_block (ANTIC_OUT
, ANTIC_IN (succ_bb
),
2184 goto maybe_dump_sets
;
2187 /* If we have multiple successors, we take the intersection of all of
2188 them. Note that in the case of loop exit phi nodes, we may have
2189 phis to translate through. */
2193 basic_block bprime
, first
= NULL
;
2195 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2196 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2199 && BB_VISITED (e
->dest
))
2201 else if (BB_VISITED (e
->dest
))
2202 worklist
.quick_push (e
->dest
);
2205 /* Of multiple successors we have to have visited one already. */
2208 bitmap_set_bit (changed_blocks
, block
->index
);
2209 BB_VISITED (block
) = 0;
2210 BB_DEFERRED (block
) = 1;
2212 goto maybe_dump_sets
;
2215 if (!gimple_seq_empty_p (phi_nodes (first
)))
2216 phi_translate_set (ANTIC_OUT
, ANTIC_IN (first
), block
, first
);
2218 bitmap_set_copy (ANTIC_OUT
, ANTIC_IN (first
));
2220 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2222 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2224 bitmap_set_t tmp
= bitmap_set_new ();
2225 phi_translate_set (tmp
, ANTIC_IN (bprime
), block
, bprime
);
2226 bitmap_set_and (ANTIC_OUT
, tmp
);
2227 bitmap_set_free (tmp
);
2230 bitmap_set_and (ANTIC_OUT
, ANTIC_IN (bprime
));
2234 /* Prune expressions that are clobbered in block and thus become
2235 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2236 prune_clobbered_mems (ANTIC_OUT
, block
);
2238 /* Generate ANTIC_OUT - TMP_GEN. */
2239 S
= bitmap_set_subtract (ANTIC_OUT
, TMP_GEN (block
));
2241 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2242 ANTIC_IN (block
) = bitmap_set_subtract (EXP_GEN (block
),
2245 /* Then union in the ANTIC_OUT - TMP_GEN values,
2246 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2247 FOR_EACH_EXPR_ID_IN_SET (S
, bii
, bi
)
2248 bitmap_value_insert_into_set (ANTIC_IN (block
),
2249 expression_for_id (bii
));
2251 clean (ANTIC_IN (block
), block
);
2253 if (!bitmap_set_equal (old
, ANTIC_IN (block
)))
2256 bitmap_set_bit (changed_blocks
, block
->index
);
2257 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2258 bitmap_set_bit (changed_blocks
, e
->src
->index
);
2261 bitmap_clear_bit (changed_blocks
, block
->index
);
2264 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2266 if (!BB_DEFERRED (block
) || BB_VISITED (block
))
2269 print_bitmap_set (dump_file
, ANTIC_OUT
, "ANTIC_OUT", block
->index
);
2271 print_bitmap_set (dump_file
, ANTIC_IN (block
), "ANTIC_IN",
2275 print_bitmap_set (dump_file
, S
, "S", block
->index
);
2280 "Block %d was deferred for a future iteration.\n",
2285 bitmap_set_free (old
);
2287 bitmap_set_free (S
);
2289 bitmap_set_free (ANTIC_OUT
);
2293 /* Compute PARTIAL_ANTIC for BLOCK.
2295 If succs(BLOCK) > 1 then
2296 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2297 in ANTIC_OUT for all succ(BLOCK)
2298 else if succs(BLOCK) == 1 then
2299 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2301 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2306 compute_partial_antic_aux (basic_block block
,
2307 bool block_has_abnormal_pred_edge
)
2309 bool changed
= false;
2310 bitmap_set_t old_PA_IN
;
2311 bitmap_set_t PA_OUT
;
2314 unsigned long max_pa
= PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH
);
2316 old_PA_IN
= PA_OUT
= NULL
;
2318 /* If any edges from predecessors are abnormal, antic_in is empty,
2320 if (block_has_abnormal_pred_edge
)
2321 goto maybe_dump_sets
;
2323 /* If there are too many partially anticipatable values in the
2324 block, phi_translate_set can take an exponential time: stop
2325 before the translation starts. */
2327 && single_succ_p (block
)
2328 && bitmap_count_bits (&PA_IN (single_succ (block
))->values
) > max_pa
)
2329 goto maybe_dump_sets
;
2331 old_PA_IN
= PA_IN (block
);
2332 PA_OUT
= bitmap_set_new ();
2334 /* If the block has no successors, ANTIC_OUT is empty. */
2335 if (EDGE_COUNT (block
->succs
) == 0)
2337 /* If we have one successor, we could have some phi nodes to
2338 translate through. Note that we can't phi translate across DFS
2339 back edges in partial antic, because it uses a union operation on
2340 the successors. For recurrences like IV's, we will end up
2341 generating a new value in the set on each go around (i + 3 (VH.1)
2342 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2343 else if (single_succ_p (block
))
2345 basic_block succ
= single_succ (block
);
2346 if (!(single_succ_edge (block
)->flags
& EDGE_DFS_BACK
))
2347 phi_translate_set (PA_OUT
, PA_IN (succ
), block
, succ
);
2349 /* If we have multiple successors, we take the union of all of
2356 auto_vec
<basic_block
> worklist (EDGE_COUNT (block
->succs
));
2357 FOR_EACH_EDGE (e
, ei
, block
->succs
)
2359 if (e
->flags
& EDGE_DFS_BACK
)
2361 worklist
.quick_push (e
->dest
);
2363 if (worklist
.length () > 0)
2365 FOR_EACH_VEC_ELT (worklist
, i
, bprime
)
2370 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime
), i
, bi
)
2371 bitmap_value_insert_into_set (PA_OUT
,
2372 expression_for_id (i
));
2373 if (!gimple_seq_empty_p (phi_nodes (bprime
)))
2375 bitmap_set_t pa_in
= bitmap_set_new ();
2376 phi_translate_set (pa_in
, PA_IN (bprime
), block
, bprime
);
2377 FOR_EACH_EXPR_ID_IN_SET (pa_in
, i
, bi
)
2378 bitmap_value_insert_into_set (PA_OUT
,
2379 expression_for_id (i
));
2380 bitmap_set_free (pa_in
);
2383 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime
), i
, bi
)
2384 bitmap_value_insert_into_set (PA_OUT
,
2385 expression_for_id (i
));
2390 /* Prune expressions that are clobbered in block and thus become
2391 invalid if translated from PA_OUT to PA_IN. */
2392 prune_clobbered_mems (PA_OUT
, block
);
2394 /* PA_IN starts with PA_OUT - TMP_GEN.
2395 Then we subtract things from ANTIC_IN. */
2396 PA_IN (block
) = bitmap_set_subtract (PA_OUT
, TMP_GEN (block
));
2398 /* For partial antic, we want to put back in the phi results, since
2399 we will properly avoid making them partially antic over backedges. */
2400 bitmap_ior_into (&PA_IN (block
)->values
, &PHI_GEN (block
)->values
);
2401 bitmap_ior_into (&PA_IN (block
)->expressions
, &PHI_GEN (block
)->expressions
);
2403 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2404 bitmap_set_subtract_values (PA_IN (block
), ANTIC_IN (block
));
2406 dependent_clean (PA_IN (block
), ANTIC_IN (block
), block
);
2408 if (!bitmap_set_equal (old_PA_IN
, PA_IN (block
)))
2411 bitmap_set_bit (changed_blocks
, block
->index
);
2412 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2413 bitmap_set_bit (changed_blocks
, e
->src
->index
);
2416 bitmap_clear_bit (changed_blocks
, block
->index
);
2419 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2422 print_bitmap_set (dump_file
, PA_OUT
, "PA_OUT", block
->index
);
2424 print_bitmap_set (dump_file
, PA_IN (block
), "PA_IN", block
->index
);
2427 bitmap_set_free (old_PA_IN
);
2429 bitmap_set_free (PA_OUT
);
2433 /* Compute ANTIC and partial ANTIC sets. */
2436 compute_antic (void)
2438 bool changed
= true;
2439 int num_iterations
= 0;
2443 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2444 We pre-build the map of blocks with incoming abnormal edges here. */
2445 has_abnormal_preds
= sbitmap_alloc (last_basic_block_for_fn (cfun
));
2446 bitmap_clear (has_abnormal_preds
);
2448 FOR_ALL_BB_FN (block
, cfun
)
2453 FOR_EACH_EDGE (e
, ei
, block
->preds
)
2455 e
->flags
&= ~EDGE_DFS_BACK
;
2456 if (e
->flags
& EDGE_ABNORMAL
)
2458 bitmap_set_bit (has_abnormal_preds
, block
->index
);
2463 BB_VISITED (block
) = 0;
2464 BB_DEFERRED (block
) = 0;
2466 /* While we are here, give empty ANTIC_IN sets to each block. */
2467 ANTIC_IN (block
) = bitmap_set_new ();
2468 PA_IN (block
) = bitmap_set_new ();
2471 /* At the exit block we anticipate nothing. */
2472 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun
)) = 1;
2474 changed_blocks
= sbitmap_alloc (last_basic_block_for_fn (cfun
) + 1);
2475 bitmap_ones (changed_blocks
);
2478 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2479 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2480 /* ??? We need to clear our PHI translation cache here as the
2481 ANTIC sets shrink and we restrict valid translations to
2482 those having operands with leaders in ANTIC. Same below
2483 for PA ANTIC computation. */
2486 for (i
= postorder_num
- 1; i
>= 0; i
--)
2488 if (bitmap_bit_p (changed_blocks
, postorder
[i
]))
2490 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2491 changed
|= compute_antic_aux (block
,
2492 bitmap_bit_p (has_abnormal_preds
,
2496 /* Theoretically possible, but *highly* unlikely. */
2497 gcc_checking_assert (num_iterations
< 500);
2500 statistics_histogram_event (cfun
, "compute_antic iterations",
2503 if (do_partial_partial
)
2505 bitmap_ones (changed_blocks
);
2506 mark_dfs_back_edges ();
2511 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
2512 fprintf (dump_file
, "Starting iteration %d\n", num_iterations
);
2515 for (i
= postorder_num
- 1 ; i
>= 0; i
--)
2517 if (bitmap_bit_p (changed_blocks
, postorder
[i
]))
2519 basic_block block
= BASIC_BLOCK_FOR_FN (cfun
, postorder
[i
]);
2521 |= compute_partial_antic_aux (block
,
2522 bitmap_bit_p (has_abnormal_preds
,
2526 /* Theoretically possible, but *highly* unlikely. */
2527 gcc_checking_assert (num_iterations
< 500);
2529 statistics_histogram_event (cfun
, "compute_partial_antic iterations",
2532 sbitmap_free (has_abnormal_preds
);
2533 sbitmap_free (changed_blocks
);
2537 /* Inserted expressions are placed onto this worklist, which is used
2538 for performing quick dead code elimination of insertions we made
2539 that didn't turn out to be necessary. */
2540 static bitmap inserted_exprs
;
2542 /* The actual worker for create_component_ref_by_pieces. */
2545 create_component_ref_by_pieces_1 (basic_block block
, vn_reference_t ref
,
2546 unsigned int *operand
, gimple_seq
*stmts
)
2548 vn_reference_op_t currop
= &ref
->operands
[*operand
];
2551 switch (currop
->opcode
)
2555 tree folded
, sc
= NULL_TREE
;
2556 unsigned int nargs
= 0;
2558 if (TREE_CODE (currop
->op0
) == FUNCTION_DECL
)
2561 fn
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2566 sc
= find_or_generate_expression (block
, currop
->op1
, stmts
);
2570 args
= XNEWVEC (tree
, ref
->operands
.length () - 1);
2571 while (*operand
< ref
->operands
.length ())
2573 args
[nargs
] = create_component_ref_by_pieces_1 (block
, ref
,
2579 folded
= build_call_array (currop
->type
,
2580 (TREE_CODE (fn
) == FUNCTION_DECL
2581 ? build_fold_addr_expr (fn
) : fn
),
2585 CALL_EXPR_STATIC_CHAIN (folded
) = sc
;
2591 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2595 tree offset
= currop
->op0
;
2596 if (TREE_CODE (baseop
) == ADDR_EXPR
2597 && handled_component_p (TREE_OPERAND (baseop
, 0)))
2601 base
= get_addr_base_and_unit_offset (TREE_OPERAND (baseop
, 0),
2604 offset
= int_const_binop (PLUS_EXPR
, offset
,
2605 build_int_cst (TREE_TYPE (offset
),
2607 baseop
= build_fold_addr_expr (base
);
2609 return fold_build2 (MEM_REF
, currop
->type
, baseop
, offset
);
2612 case TARGET_MEM_REF
:
2614 tree genop0
= NULL_TREE
, genop1
= NULL_TREE
;
2615 vn_reference_op_t nextop
= &ref
->operands
[++*operand
];
2616 tree baseop
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2622 genop0
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2628 genop1
= find_or_generate_expression (block
, nextop
->op0
, stmts
);
2632 return build5 (TARGET_MEM_REF
, currop
->type
,
2633 baseop
, currop
->op2
, genop0
, currop
->op1
, genop1
);
2639 gcc_assert (is_gimple_min_invariant (currop
->op0
));
2645 case VIEW_CONVERT_EXPR
:
2647 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2651 return fold_build1 (currop
->opcode
, currop
->type
, genop0
);
2654 case WITH_SIZE_EXPR
:
2656 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2660 tree genop1
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2663 return fold_build2 (currop
->opcode
, currop
->type
, genop0
, genop1
);
2668 tree genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2672 tree op1
= currop
->op0
;
2673 tree op2
= currop
->op1
;
2674 return fold_build3 (BIT_FIELD_REF
, currop
->type
, genop0
, op1
, op2
);
2677 /* For array ref vn_reference_op's, operand 1 of the array ref
2678 is op0 of the reference op and operand 3 of the array ref is
2680 case ARRAY_RANGE_REF
:
2684 tree genop1
= currop
->op0
;
2685 tree genop2
= currop
->op1
;
2686 tree genop3
= currop
->op2
;
2687 genop0
= create_component_ref_by_pieces_1 (block
, ref
, operand
,
2691 genop1
= find_or_generate_expression (block
, genop1
, stmts
);
2696 tree domain_type
= TYPE_DOMAIN (TREE_TYPE (genop0
));
2697 /* Drop zero minimum index if redundant. */
2698 if (integer_zerop (genop2
)
2700 || integer_zerop (TYPE_MIN_VALUE (domain_type
))))
2704 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2711 tree elmt_type
= TREE_TYPE (TREE_TYPE (genop0
));
2712 /* We can't always put a size in units of the element alignment
2713 here as the element alignment may be not visible. See
2714 PR43783. Simply drop the element size for constant
2716 if (tree_int_cst_equal (genop3
, TYPE_SIZE_UNIT (elmt_type
)))
2720 genop3
= size_binop (EXACT_DIV_EXPR
, genop3
,
2721 size_int (TYPE_ALIGN_UNIT (elmt_type
)));
2722 genop3
= find_or_generate_expression (block
, genop3
, stmts
);
2727 return build4 (currop
->opcode
, currop
->type
, genop0
, genop1
,
2734 tree genop2
= currop
->op1
;
2735 op0
= create_component_ref_by_pieces_1 (block
, ref
, operand
, stmts
);
2738 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2742 genop2
= find_or_generate_expression (block
, genop2
, stmts
);
2746 return fold_build3 (COMPONENT_REF
, TREE_TYPE (op1
), op0
, op1
, genop2
);
2751 genop
= find_or_generate_expression (block
, currop
->op0
, stmts
);
2772 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2773 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2774 trying to rename aggregates into ssa form directly, which is a no no.
2776 Thus, this routine doesn't create temporaries, it just builds a
2777 single access expression for the array, calling
2778 find_or_generate_expression to build the innermost pieces.
2780 This function is a subroutine of create_expression_by_pieces, and
2781 should not be called on it's own unless you really know what you
2785 create_component_ref_by_pieces (basic_block block
, vn_reference_t ref
,
2788 unsigned int op
= 0;
2789 return create_component_ref_by_pieces_1 (block
, ref
, &op
, stmts
);
2792 /* Find a simple leader for an expression, or generate one using
2793 create_expression_by_pieces from a NARY expression for the value.
2794 BLOCK is the basic_block we are looking for leaders in.
2795 OP is the tree expression to find a leader for or generate.
2796 Returns the leader or NULL_TREE on failure. */
2799 find_or_generate_expression (basic_block block
, tree op
, gimple_seq
*stmts
)
2801 pre_expr expr
= get_or_alloc_expr_for (op
);
2802 unsigned int lookfor
= get_expr_value_id (expr
);
2803 pre_expr leader
= bitmap_find_leader (AVAIL_OUT (block
), lookfor
);
2806 if (leader
->kind
== NAME
)
2807 return PRE_EXPR_NAME (leader
);
2808 else if (leader
->kind
== CONSTANT
)
2809 return PRE_EXPR_CONSTANT (leader
);
2815 /* It must be a complex expression, so generate it recursively. Note
2816 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2817 where the insert algorithm fails to insert a required expression. */
2818 bitmap exprset
= value_expressions
[lookfor
];
2821 EXECUTE_IF_SET_IN_BITMAP (exprset
, 0, i
, bi
)
2823 pre_expr temp
= expression_for_id (i
);
2824 /* We cannot insert random REFERENCE expressions at arbitrary
2825 places. We can insert NARYs which eventually re-materializes
2826 its operand values. */
2827 if (temp
->kind
== NARY
)
2828 return create_expression_by_pieces (block
, temp
, stmts
,
2829 get_expr_type (expr
));
2836 #define NECESSARY GF_PLF_1
2838 /* Create an expression in pieces, so that we can handle very complex
2839 expressions that may be ANTIC, but not necessary GIMPLE.
2840 BLOCK is the basic block the expression will be inserted into,
2841 EXPR is the expression to insert (in value form)
2842 STMTS is a statement list to append the necessary insertions into.
2844 This function will die if we hit some value that shouldn't be
2845 ANTIC but is (IE there is no leader for it, or its components).
2846 The function returns NULL_TREE in case a different antic expression
2847 has to be inserted first.
2848 This function may also generate expressions that are themselves
2849 partially or fully redundant. Those that are will be either made
2850 fully redundant during the next iteration of insert (for partially
2851 redundant ones), or eliminated by eliminate (for fully redundant
2855 create_expression_by_pieces (basic_block block
, pre_expr expr
,
2856 gimple_seq
*stmts
, tree type
)
2860 gimple_seq forced_stmts
= NULL
;
2861 unsigned int value_id
;
2862 gimple_stmt_iterator gsi
;
2863 tree exprtype
= type
? type
: get_expr_type (expr
);
2869 /* We may hit the NAME/CONSTANT case if we have to convert types
2870 that value numbering saw through. */
2872 folded
= PRE_EXPR_NAME (expr
);
2875 folded
= PRE_EXPR_CONSTANT (expr
);
2879 vn_reference_t ref
= PRE_EXPR_REFERENCE (expr
);
2880 folded
= create_component_ref_by_pieces (block
, ref
, stmts
);
2887 vn_nary_op_t nary
= PRE_EXPR_NARY (expr
);
2888 tree
*genop
= XALLOCAVEC (tree
, nary
->length
);
2890 for (i
= 0; i
< nary
->length
; ++i
)
2892 genop
[i
] = find_or_generate_expression (block
, nary
->op
[i
], stmts
);
2895 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2896 may have conversions stripped. */
2897 if (nary
->opcode
== POINTER_PLUS_EXPR
)
2900 genop
[i
] = fold_convert (nary
->type
, genop
[i
]);
2902 genop
[i
] = convert_to_ptrofftype (genop
[i
]);
2905 genop
[i
] = fold_convert (TREE_TYPE (nary
->op
[i
]), genop
[i
]);
2907 if (nary
->opcode
== CONSTRUCTOR
)
2909 vec
<constructor_elt
, va_gc
> *elts
= NULL
;
2910 for (i
= 0; i
< nary
->length
; ++i
)
2911 CONSTRUCTOR_APPEND_ELT (elts
, NULL_TREE
, genop
[i
]);
2912 folded
= build_constructor (nary
->type
, elts
);
2916 switch (nary
->length
)
2919 folded
= fold_build1 (nary
->opcode
, nary
->type
,
2923 folded
= fold_build2 (nary
->opcode
, nary
->type
,
2924 genop
[0], genop
[1]);
2927 folded
= fold_build3 (nary
->opcode
, nary
->type
,
2928 genop
[0], genop
[1], genop
[2]);
2940 if (!useless_type_conversion_p (exprtype
, TREE_TYPE (folded
)))
2941 folded
= fold_convert (exprtype
, folded
);
2943 /* Force the generated expression to be a sequence of GIMPLE
2945 We have to call unshare_expr because force_gimple_operand may
2946 modify the tree we pass to it. */
2947 folded
= force_gimple_operand (unshare_expr (folded
), &forced_stmts
,
2950 /* If we have any intermediate expressions to the value sets, add them
2951 to the value sets and chain them in the instruction stream. */
2954 gsi
= gsi_start (forced_stmts
);
2955 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
2957 gimple stmt
= gsi_stmt (gsi
);
2958 tree forcedname
= gimple_get_lhs (stmt
);
2961 if (TREE_CODE (forcedname
) == SSA_NAME
)
2963 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (forcedname
));
2964 VN_INFO_GET (forcedname
)->valnum
= forcedname
;
2965 VN_INFO (forcedname
)->value_id
= get_next_value_id ();
2966 nameexpr
= get_or_alloc_expr_for_name (forcedname
);
2967 add_to_value (VN_INFO (forcedname
)->value_id
, nameexpr
);
2968 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
2969 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
2972 gimple_seq_add_seq (stmts
, forced_stmts
);
2975 name
= make_temp_ssa_name (exprtype
, NULL
, "pretmp");
2976 newstmt
= gimple_build_assign (name
, folded
);
2977 gimple_set_plf (newstmt
, NECESSARY
, false);
2979 gimple_seq_add_stmt (stmts
, newstmt
);
2980 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (name
));
2982 /* Fold the last statement. */
2983 gsi
= gsi_last (*stmts
);
2984 if (fold_stmt_inplace (&gsi
))
2985 update_stmt (gsi_stmt (gsi
));
2987 /* Add a value number to the temporary.
2988 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2989 we are creating the expression by pieces, and this particular piece of
2990 the expression may have been represented. There is no harm in replacing
2992 value_id
= get_expr_value_id (expr
);
2993 VN_INFO_GET (name
)->value_id
= value_id
;
2994 VN_INFO (name
)->valnum
= sccvn_valnum_from_value_id (value_id
);
2995 if (VN_INFO (name
)->valnum
== NULL_TREE
)
2996 VN_INFO (name
)->valnum
= name
;
2997 gcc_assert (VN_INFO (name
)->valnum
!= NULL_TREE
);
2998 nameexpr
= get_or_alloc_expr_for_name (name
);
2999 add_to_value (value_id
, nameexpr
);
3000 if (NEW_SETS (block
))
3001 bitmap_value_replace_in_set (NEW_SETS (block
), nameexpr
);
3002 bitmap_value_replace_in_set (AVAIL_OUT (block
), nameexpr
);
3004 pre_stats
.insertions
++;
3005 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3007 fprintf (dump_file
, "Inserted ");
3008 print_gimple_stmt (dump_file
, newstmt
, 0, 0);
3009 fprintf (dump_file
, " in predecessor %d (%04d)\n",
3010 block
->index
, value_id
);
3017 /* Insert the to-be-made-available values of expression EXPRNUM for each
3018 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3019 merge the result with a phi node, given the same value number as
3020 NODE. Return true if we have inserted new stuff. */
3023 insert_into_preds_of_block (basic_block block
, unsigned int exprnum
,
3024 vec
<pre_expr
> avail
)
3026 pre_expr expr
= expression_for_id (exprnum
);
3028 unsigned int val
= get_expr_value_id (expr
);
3030 bool insertions
= false;
3035 tree type
= get_expr_type (expr
);
3039 /* Make sure we aren't creating an induction variable. */
3040 if (bb_loop_depth (block
) > 0 && EDGE_COUNT (block
->preds
) == 2)
3042 bool firstinsideloop
= false;
3043 bool secondinsideloop
= false;
3044 firstinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3045 EDGE_PRED (block
, 0)->src
);
3046 secondinsideloop
= flow_bb_inside_loop_p (block
->loop_father
,
3047 EDGE_PRED (block
, 1)->src
);
3048 /* Induction variables only have one edge inside the loop. */
3049 if ((firstinsideloop
^ secondinsideloop
)
3050 && expr
->kind
!= REFERENCE
)
3052 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3053 fprintf (dump_file
, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3058 /* Make the necessary insertions. */
3059 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3061 gimple_seq stmts
= NULL
;
3064 eprime
= avail
[pred
->dest_idx
];
3066 if (eprime
->kind
!= NAME
&& eprime
->kind
!= CONSTANT
)
3068 builtexpr
= create_expression_by_pieces (bprime
, eprime
,
3070 gcc_assert (!(pred
->flags
& EDGE_ABNORMAL
));
3071 gsi_insert_seq_on_edge (pred
, stmts
);
3074 /* We cannot insert a PHI node if we failed to insert
3079 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_name (builtexpr
);
3082 else if (eprime
->kind
== CONSTANT
)
3084 /* Constants may not have the right type, fold_convert
3085 should give us back a constant with the right type. */
3086 tree constant
= PRE_EXPR_CONSTANT (eprime
);
3087 if (!useless_type_conversion_p (type
, TREE_TYPE (constant
)))
3089 tree builtexpr
= fold_convert (type
, constant
);
3090 if (!is_gimple_min_invariant (builtexpr
))
3092 tree forcedexpr
= force_gimple_operand (builtexpr
,
3095 if (!is_gimple_min_invariant (forcedexpr
))
3097 if (forcedexpr
!= builtexpr
)
3099 VN_INFO_GET (forcedexpr
)->valnum
= PRE_EXPR_CONSTANT (eprime
);
3100 VN_INFO (forcedexpr
)->value_id
= get_expr_value_id (eprime
);
3104 gimple_stmt_iterator gsi
;
3105 gsi
= gsi_start (stmts
);
3106 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
3108 gimple stmt
= gsi_stmt (gsi
);
3109 tree lhs
= gimple_get_lhs (stmt
);
3110 if (TREE_CODE (lhs
) == SSA_NAME
)
3111 bitmap_set_bit (inserted_exprs
,
3112 SSA_NAME_VERSION (lhs
));
3113 gimple_set_plf (stmt
, NECESSARY
, false);
3115 gsi_insert_seq_on_edge (pred
, stmts
);
3117 avail
[pred
->dest_idx
]
3118 = get_or_alloc_expr_for_name (forcedexpr
);
3122 avail
[pred
->dest_idx
]
3123 = get_or_alloc_expr_for_constant (builtexpr
);
3126 else if (eprime
->kind
== NAME
)
3128 /* We may have to do a conversion because our value
3129 numbering can look through types in certain cases, but
3130 our IL requires all operands of a phi node have the same
3132 tree name
= PRE_EXPR_NAME (eprime
);
3133 if (!useless_type_conversion_p (type
, TREE_TYPE (name
)))
3137 builtexpr
= fold_convert (type
, name
);
3138 forcedexpr
= force_gimple_operand (builtexpr
,
3142 if (forcedexpr
!= name
)
3144 VN_INFO_GET (forcedexpr
)->valnum
= VN_INFO (name
)->valnum
;
3145 VN_INFO (forcedexpr
)->value_id
= VN_INFO (name
)->value_id
;
3150 gimple_stmt_iterator gsi
;
3151 gsi
= gsi_start (stmts
);
3152 for (; !gsi_end_p (gsi
); gsi_next (&gsi
))
3154 gimple stmt
= gsi_stmt (gsi
);
3155 tree lhs
= gimple_get_lhs (stmt
);
3156 if (TREE_CODE (lhs
) == SSA_NAME
)
3157 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (lhs
));
3158 gimple_set_plf (stmt
, NECESSARY
, false);
3160 gsi_insert_seq_on_edge (pred
, stmts
);
3162 avail
[pred
->dest_idx
] = get_or_alloc_expr_for_name (forcedexpr
);
3166 /* If we didn't want a phi node, and we made insertions, we still have
3167 inserted new stuff, and thus return true. If we didn't want a phi node,
3168 and didn't make insertions, we haven't added anything new, so return
3170 if (nophi
&& insertions
)
3172 else if (nophi
&& !insertions
)
3175 /* Now build a phi for the new variable. */
3176 temp
= make_temp_ssa_name (type
, NULL
, "prephitmp");
3177 phi
= create_phi_node (temp
, block
);
3179 gimple_set_plf (phi
, NECESSARY
, false);
3180 VN_INFO_GET (temp
)->value_id
= val
;
3181 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3182 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3183 VN_INFO (temp
)->valnum
= temp
;
3184 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3185 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3187 pre_expr ae
= avail
[pred
->dest_idx
];
3188 gcc_assert (get_expr_type (ae
) == type
3189 || useless_type_conversion_p (type
, get_expr_type (ae
)));
3190 if (ae
->kind
== CONSTANT
)
3191 add_phi_arg (phi
, unshare_expr (PRE_EXPR_CONSTANT (ae
)),
3192 pred
, UNKNOWN_LOCATION
);
3194 add_phi_arg (phi
, PRE_EXPR_NAME (ae
), pred
, UNKNOWN_LOCATION
);
3197 newphi
= get_or_alloc_expr_for_name (temp
);
3198 add_to_value (val
, newphi
);
3200 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3201 this insertion, since we test for the existence of this value in PHI_GEN
3202 before proceeding with the partial redundancy checks in insert_aux.
3204 The value may exist in AVAIL_OUT, in particular, it could be represented
3205 by the expression we are trying to eliminate, in which case we want the
3206 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3209 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3210 this block, because if it did, it would have existed in our dominator's
3211 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3214 bitmap_insert_into_set (PHI_GEN (block
), newphi
);
3215 bitmap_value_replace_in_set (AVAIL_OUT (block
),
3217 bitmap_insert_into_set (NEW_SETS (block
),
3220 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3222 fprintf (dump_file
, "Created phi ");
3223 print_gimple_stmt (dump_file
, phi
, 0, 0);
3224 fprintf (dump_file
, " in block %d (%04d)\n", block
->index
, val
);
3232 /* Perform insertion of partially redundant values.
3233 For BLOCK, do the following:
3234 1. Propagate the NEW_SETS of the dominator into the current block.
3235 If the block has multiple predecessors,
3236 2a. Iterate over the ANTIC expressions for the block to see if
3237 any of them are partially redundant.
3238 2b. If so, insert them into the necessary predecessors to make
3239 the expression fully redundant.
3240 2c. Insert a new PHI merging the values of the predecessors.
3241 2d. Insert the new PHI, and the new expressions, into the
3243 3. Recursively call ourselves on the dominator children of BLOCK.
3245 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
3246 do_regular_insertion and do_partial_insertion.
3251 do_regular_insertion (basic_block block
, basic_block dom
)
3253 bool new_stuff
= false;
3254 vec
<pre_expr
> exprs
;
3256 vec
<pre_expr
> avail
= vNULL
;
3259 exprs
= sorted_array_from_bitmap_set (ANTIC_IN (block
));
3260 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3262 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3264 if (expr
->kind
== NARY
3265 || expr
->kind
== REFERENCE
)
3268 bool by_some
= false;
3269 bool cant_insert
= false;
3270 bool all_same
= true;
3271 pre_expr first_s
= NULL
;
3274 pre_expr eprime
= NULL
;
3276 pre_expr edoubleprime
= NULL
;
3277 bool do_insertion
= false;
3279 val
= get_expr_value_id (expr
);
3280 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3282 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3284 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3286 fprintf (dump_file
, "Found fully redundant value: ");
3287 print_pre_expr (dump_file
, expr
);
3288 fprintf (dump_file
, "\n");
3293 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3295 unsigned int vprime
;
3297 /* We should never run insertion for the exit block
3298 and so not come across fake pred edges. */
3299 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3301 eprime
= phi_translate (expr
, ANTIC_IN (block
), NULL
,
3304 /* eprime will generally only be NULL if the
3305 value of the expression, translated
3306 through the PHI for this predecessor, is
3307 undefined. If that is the case, we can't
3308 make the expression fully redundant,
3309 because its value is undefined along a
3310 predecessor path. We can thus break out
3311 early because it doesn't matter what the
3312 rest of the results are. */
3315 avail
[pred
->dest_idx
] = NULL
;
3320 eprime
= fully_constant_expression (eprime
);
3321 vprime
= get_expr_value_id (eprime
);
3322 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
),
3324 if (edoubleprime
== NULL
)
3326 avail
[pred
->dest_idx
] = eprime
;
3331 avail
[pred
->dest_idx
] = edoubleprime
;
3333 /* We want to perform insertions to remove a redundancy on
3334 a path in the CFG we want to optimize for speed. */
3335 if (optimize_edge_for_speed_p (pred
))
3336 do_insertion
= true;
3337 if (first_s
== NULL
)
3338 first_s
= edoubleprime
;
3339 else if (!pre_expr_d::equal (first_s
, edoubleprime
))
3343 /* If we can insert it, it's not the same value
3344 already existing along every predecessor, and
3345 it's defined by some predecessor, it is
3346 partially redundant. */
3347 if (!cant_insert
&& !all_same
&& by_some
)
3351 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3353 fprintf (dump_file
, "Skipping partial redundancy for "
3355 print_pre_expr (dump_file
, expr
);
3356 fprintf (dump_file
, " (%04d), no redundancy on to be "
3357 "optimized for speed edge\n", val
);
3360 else if (dbg_cnt (treepre_insert
))
3362 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3364 fprintf (dump_file
, "Found partial redundancy for "
3366 print_pre_expr (dump_file
, expr
);
3367 fprintf (dump_file
, " (%04d)\n",
3368 get_expr_value_id (expr
));
3370 if (insert_into_preds_of_block (block
,
3371 get_expression_id (expr
),
3376 /* If all edges produce the same value and that value is
3377 an invariant, then the PHI has the same value on all
3378 edges. Note this. */
3379 else if (!cant_insert
&& all_same
)
3381 gcc_assert (edoubleprime
->kind
== CONSTANT
3382 || edoubleprime
->kind
== NAME
);
3384 tree temp
= make_temp_ssa_name (get_expr_type (expr
),
3386 gimple assign
= gimple_build_assign (temp
,
3387 edoubleprime
->kind
== CONSTANT
? PRE_EXPR_CONSTANT (edoubleprime
) : PRE_EXPR_NAME (edoubleprime
));
3388 gimple_stmt_iterator gsi
= gsi_after_labels (block
);
3389 gsi_insert_before (&gsi
, assign
, GSI_NEW_STMT
);
3391 gimple_set_plf (assign
, NECESSARY
, false);
3392 VN_INFO_GET (temp
)->value_id
= val
;
3393 VN_INFO (temp
)->valnum
= sccvn_valnum_from_value_id (val
);
3394 if (VN_INFO (temp
)->valnum
== NULL_TREE
)
3395 VN_INFO (temp
)->valnum
= temp
;
3396 bitmap_set_bit (inserted_exprs
, SSA_NAME_VERSION (temp
));
3397 pre_expr newe
= get_or_alloc_expr_for_name (temp
);
3398 add_to_value (val
, newe
);
3399 bitmap_value_replace_in_set (AVAIL_OUT (block
), newe
);
3400 bitmap_insert_into_set (NEW_SETS (block
), newe
);
3410 /* Perform insertion for partially anticipatable expressions. There
3411 is only one case we will perform insertion for these. This case is
3412 if the expression is partially anticipatable, and fully available.
3413 In this case, we know that putting it earlier will enable us to
3414 remove the later computation. */
3418 do_partial_partial_insertion (basic_block block
, basic_block dom
)
3420 bool new_stuff
= false;
3421 vec
<pre_expr
> exprs
;
3423 auto_vec
<pre_expr
> avail
;
3426 exprs
= sorted_array_from_bitmap_set (PA_IN (block
));
3427 avail
.safe_grow (EDGE_COUNT (block
->preds
));
3429 FOR_EACH_VEC_ELT (exprs
, i
, expr
)
3431 if (expr
->kind
== NARY
3432 || expr
->kind
== REFERENCE
)
3436 bool cant_insert
= false;
3439 pre_expr eprime
= NULL
;
3442 val
= get_expr_value_id (expr
);
3443 if (bitmap_set_contains_value (PHI_GEN (block
), val
))
3445 if (bitmap_set_contains_value (AVAIL_OUT (dom
), val
))
3448 FOR_EACH_EDGE (pred
, ei
, block
->preds
)
3450 unsigned int vprime
;
3451 pre_expr edoubleprime
;
3453 /* We should never run insertion for the exit block
3454 and so not come across fake pred edges. */
3455 gcc_assert (!(pred
->flags
& EDGE_FAKE
));
3457 eprime
= phi_translate (expr
, ANTIC_IN (block
),
3461 /* eprime will generally only be NULL if the
3462 value of the expression, translated
3463 through the PHI for this predecessor, is
3464 undefined. If that is the case, we can't
3465 make the expression fully redundant,
3466 because its value is undefined along a
3467 predecessor path. We can thus break out
3468 early because it doesn't matter what the
3469 rest of the results are. */
3472 avail
[pred
->dest_idx
] = NULL
;
3477 eprime
= fully_constant_expression (eprime
);
3478 vprime
= get_expr_value_id (eprime
);
3479 edoubleprime
= bitmap_find_leader (AVAIL_OUT (bprime
), vprime
);
3480 avail
[pred
->dest_idx
] = edoubleprime
;
3481 if (edoubleprime
== NULL
)
3488 /* If we can insert it, it's not the same value
3489 already existing along every predecessor, and
3490 it's defined by some predecessor, it is
3491 partially redundant. */
3492 if (!cant_insert
&& by_all
)
3495 bool do_insertion
= false;
3497 /* Insert only if we can remove a later expression on a path
3498 that we want to optimize for speed.
3499 The phi node that we will be inserting in BLOCK is not free,
3500 and inserting it for the sake of !optimize_for_speed successor
3501 may cause regressions on the speed path. */
3502 FOR_EACH_EDGE (succ
, ei
, block
->succs
)
3504 if (bitmap_set_contains_value (PA_IN (succ
->dest
), val
)
3505 || bitmap_set_contains_value (ANTIC_IN (succ
->dest
), val
))
3507 if (optimize_edge_for_speed_p (succ
))
3508 do_insertion
= true;
3514 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3516 fprintf (dump_file
, "Skipping partial partial redundancy "
3518 print_pre_expr (dump_file
, expr
);
3519 fprintf (dump_file
, " (%04d), not (partially) anticipated "
3520 "on any to be optimized for speed edges\n", val
);
3523 else if (dbg_cnt (treepre_insert
))
3525 pre_stats
.pa_insert
++;
3526 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3528 fprintf (dump_file
, "Found partial partial redundancy "
3530 print_pre_expr (dump_file
, expr
);
3531 fprintf (dump_file
, " (%04d)\n",
3532 get_expr_value_id (expr
));
3534 if (insert_into_preds_of_block (block
,
3535 get_expression_id (expr
),
3548 insert_aux (basic_block block
)
3551 bool new_stuff
= false;
3556 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3561 bitmap_set_t newset
= NEW_SETS (dom
);
3564 /* Note that we need to value_replace both NEW_SETS, and
3565 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3566 represented by some non-simple expression here that we want
3567 to replace it with. */
3568 FOR_EACH_EXPR_ID_IN_SET (newset
, i
, bi
)
3570 pre_expr expr
= expression_for_id (i
);
3571 bitmap_value_replace_in_set (NEW_SETS (block
), expr
);
3572 bitmap_value_replace_in_set (AVAIL_OUT (block
), expr
);
3575 if (!single_pred_p (block
))
3577 new_stuff
|= do_regular_insertion (block
, dom
);
3578 if (do_partial_partial
)
3579 new_stuff
|= do_partial_partial_insertion (block
, dom
);
3583 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
3585 son
= next_dom_son (CDI_DOMINATORS
, son
))
3587 new_stuff
|= insert_aux (son
);
3593 /* Perform insertion of partially redundant values. */
3598 bool new_stuff
= true;
3600 int num_iterations
= 0;
3602 FOR_ALL_BB_FN (bb
, cfun
)
3603 NEW_SETS (bb
) = bitmap_set_new ();
3608 if (dump_file
&& dump_flags
& TDF_DETAILS
)
3609 fprintf (dump_file
, "Starting insert iteration %d\n", num_iterations
);
3610 new_stuff
= insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3612 /* Clear the NEW sets before the next iteration. We have already
3613 fully propagated its contents. */
3615 FOR_ALL_BB_FN (bb
, cfun
)
3616 bitmap_set_free (NEW_SETS (bb
));
3618 statistics_histogram_event (cfun
, "insert iterations", num_iterations
);
3622 /* Compute the AVAIL set for all basic blocks.
3624 This function performs value numbering of the statements in each basic
3625 block. The AVAIL sets are built from information we glean while doing
3626 this value numbering, since the AVAIL sets contain only one entry per
3629 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3630 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3633 compute_avail (void)
3636 basic_block block
, son
;
3637 basic_block
*worklist
;
3641 /* We pretend that default definitions are defined in the entry block.
3642 This includes function arguments and the static chain decl. */
3643 for (i
= 1; i
< num_ssa_names
; ++i
)
3645 tree name
= ssa_name (i
);
3648 || !SSA_NAME_IS_DEFAULT_DEF (name
)
3649 || has_zero_uses (name
)
3650 || virtual_operand_p (name
))
3653 e
= get_or_alloc_expr_for_name (name
);
3654 add_to_value (get_expr_value_id (e
), e
);
3655 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)), e
);
3656 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3660 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3662 print_bitmap_set (dump_file
, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3663 "tmp_gen", ENTRY_BLOCK
);
3664 print_bitmap_set (dump_file
, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun
)),
3665 "avail_out", ENTRY_BLOCK
);
3668 /* Allocate the worklist. */
3669 worklist
= XNEWVEC (basic_block
, n_basic_blocks_for_fn (cfun
));
3671 /* Seed the algorithm by putting the dominator children of the entry
3672 block on the worklist. */
3673 for (son
= first_dom_son (CDI_DOMINATORS
, ENTRY_BLOCK_PTR_FOR_FN (cfun
));
3675 son
= next_dom_son (CDI_DOMINATORS
, son
))
3676 worklist
[sp
++] = son
;
3678 /* Loop until the worklist is empty. */
3681 gimple_stmt_iterator gsi
;
3685 /* Pick a block from the worklist. */
3686 block
= worklist
[--sp
];
3688 /* Initially, the set of available values in BLOCK is that of
3689 its immediate dominator. */
3690 dom
= get_immediate_dominator (CDI_DOMINATORS
, block
);
3692 bitmap_set_copy (AVAIL_OUT (block
), AVAIL_OUT (dom
));
3694 /* Generate values for PHI nodes. */
3695 for (gsi
= gsi_start_phis (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3697 tree result
= gimple_phi_result (gsi_stmt (gsi
));
3699 /* We have no need for virtual phis, as they don't represent
3700 actual computations. */
3701 if (virtual_operand_p (result
))
3704 pre_expr e
= get_or_alloc_expr_for_name (result
);
3705 add_to_value (get_expr_value_id (e
), e
);
3706 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3707 bitmap_insert_into_set (PHI_GEN (block
), e
);
3710 BB_MAY_NOTRETURN (block
) = 0;
3712 /* Now compute value numbers and populate value sets with all
3713 the expressions computed in BLOCK. */
3714 for (gsi
= gsi_start_bb (block
); !gsi_end_p (gsi
); gsi_next (&gsi
))
3719 stmt
= gsi_stmt (gsi
);
3721 /* Cache whether the basic-block has any non-visible side-effect
3723 If this isn't a call or it is the last stmt in the
3724 basic-block then the CFG represents things correctly. */
3725 if (is_gimple_call (stmt
) && !stmt_ends_bb_p (stmt
))
3727 /* Non-looping const functions always return normally.
3728 Otherwise the call might not return or have side-effects
3729 that forbids hoisting possibly trapping expressions
3731 int flags
= gimple_call_flags (stmt
);
3732 if (!(flags
& ECF_CONST
)
3733 || (flags
& ECF_LOOPING_CONST_OR_PURE
))
3734 BB_MAY_NOTRETURN (block
) = 1;
3737 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_DEF
)
3739 pre_expr e
= get_or_alloc_expr_for_name (op
);
3741 add_to_value (get_expr_value_id (e
), e
);
3742 bitmap_insert_into_set (TMP_GEN (block
), e
);
3743 bitmap_value_insert_into_set (AVAIL_OUT (block
), e
);
3746 if (gimple_has_side_effects (stmt
)
3747 || stmt_could_throw_p (stmt
)
3748 || is_gimple_debug (stmt
))
3751 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
3753 if (ssa_undefined_value_p (op
))
3755 pre_expr e
= get_or_alloc_expr_for_name (op
);
3756 bitmap_value_insert_into_set (EXP_GEN (block
), e
);
3759 switch (gimple_code (stmt
))
3767 pre_expr result
= NULL
;
3768 auto_vec
<vn_reference_op_s
> ops
;
3770 /* We can value number only calls to real functions. */
3771 if (gimple_call_internal_p (stmt
))
3774 copy_reference_ops_from_call (stmt
, &ops
);
3775 vn_reference_lookup_pieces (gimple_vuse (stmt
), 0,
3776 gimple_expr_type (stmt
),
3777 ops
, &ref
, VN_NOWALK
);
3781 /* If the value of the call is not invalidated in
3782 this block until it is computed, add the expression
3784 if (!gimple_vuse (stmt
)
3786 (SSA_NAME_DEF_STMT (gimple_vuse (stmt
))) == GIMPLE_PHI
3787 || gimple_bb (SSA_NAME_DEF_STMT
3788 (gimple_vuse (stmt
))) != block
)
3790 result
= (pre_expr
) pool_alloc (pre_expr_pool
);
3791 result
->kind
= REFERENCE
;
3793 PRE_EXPR_REFERENCE (result
) = ref
;
3795 get_or_alloc_expression_id (result
);
3796 add_to_value (get_expr_value_id (result
), result
);
3797 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
3804 pre_expr result
= NULL
;
3805 switch (vn_get_stmt_kind (stmt
))
3809 enum tree_code code
= gimple_assign_rhs_code (stmt
);
3812 /* COND_EXPR and VEC_COND_EXPR are awkward in
3813 that they contain an embedded complex expression.
3814 Don't even try to shove those through PRE. */
3815 if (code
== COND_EXPR
3816 || code
== VEC_COND_EXPR
)
3819 vn_nary_op_lookup_stmt (stmt
, &nary
);
3823 /* If the NARY traps and there was a preceding
3824 point in the block that might not return avoid
3825 adding the nary to EXP_GEN. */
3826 if (BB_MAY_NOTRETURN (block
)
3827 && vn_nary_may_trap (nary
))
3830 result
= (pre_expr
) pool_alloc (pre_expr_pool
);
3831 result
->kind
= NARY
;
3833 PRE_EXPR_NARY (result
) = nary
;
3840 vn_reference_lookup (gimple_assign_rhs1 (stmt
),
3846 /* If the value of the reference is not invalidated in
3847 this block until it is computed, add the expression
3849 if (gimple_vuse (stmt
))
3853 def_stmt
= SSA_NAME_DEF_STMT (gimple_vuse (stmt
));
3854 while (!gimple_nop_p (def_stmt
)
3855 && gimple_code (def_stmt
) != GIMPLE_PHI
3856 && gimple_bb (def_stmt
) == block
)
3858 if (stmt_may_clobber_ref_p
3859 (def_stmt
, gimple_assign_rhs1 (stmt
)))
3865 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt
));
3871 result
= (pre_expr
) pool_alloc (pre_expr_pool
);
3872 result
->kind
= REFERENCE
;
3874 PRE_EXPR_REFERENCE (result
) = ref
;
3882 get_or_alloc_expression_id (result
);
3883 add_to_value (get_expr_value_id (result
), result
);
3884 bitmap_value_insert_into_set (EXP_GEN (block
), result
);
3892 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3894 print_bitmap_set (dump_file
, EXP_GEN (block
),
3895 "exp_gen", block
->index
);
3896 print_bitmap_set (dump_file
, PHI_GEN (block
),
3897 "phi_gen", block
->index
);
3898 print_bitmap_set (dump_file
, TMP_GEN (block
),
3899 "tmp_gen", block
->index
);
3900 print_bitmap_set (dump_file
, AVAIL_OUT (block
),
3901 "avail_out", block
->index
);
3904 /* Put the dominator children of BLOCK on the worklist of blocks
3905 to compute available sets for. */
3906 for (son
= first_dom_son (CDI_DOMINATORS
, block
);
3908 son
= next_dom_son (CDI_DOMINATORS
, son
))
3909 worklist
[sp
++] = son
;
3916 /* Local state for the eliminate domwalk. */
3917 static vec
<gimple
> el_to_remove
;
3918 static unsigned int el_todo
;
3919 static vec
<tree
> el_avail
;
3920 static vec
<tree
> el_avail_stack
;
3922 /* Return a leader for OP that is available at the current point of the
3923 eliminate domwalk. */
3926 eliminate_avail (tree op
)
3928 tree valnum
= VN_INFO (op
)->valnum
;
3929 if (TREE_CODE (valnum
) == SSA_NAME
)
3931 if (SSA_NAME_IS_DEFAULT_DEF (valnum
))
3933 if (el_avail
.length () > SSA_NAME_VERSION (valnum
))
3934 return el_avail
[SSA_NAME_VERSION (valnum
)];
3936 else if (is_gimple_min_invariant (valnum
))
3941 /* At the current point of the eliminate domwalk make OP available. */
3944 eliminate_push_avail (tree op
)
3946 tree valnum
= VN_INFO (op
)->valnum
;
3947 if (TREE_CODE (valnum
) == SSA_NAME
)
3949 if (el_avail
.length () <= SSA_NAME_VERSION (valnum
))
3950 el_avail
.safe_grow_cleared (SSA_NAME_VERSION (valnum
) + 1);
3951 el_avail
[SSA_NAME_VERSION (valnum
)] = op
;
3952 el_avail_stack
.safe_push (op
);
3956 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
3957 the leader for the expression if insertion was successful. */
3960 eliminate_insert (gimple_stmt_iterator
*gsi
, tree val
)
3962 tree expr
= vn_get_expr_for (val
);
3963 if (!CONVERT_EXPR_P (expr
)
3964 && TREE_CODE (expr
) != VIEW_CONVERT_EXPR
)
3967 tree op
= TREE_OPERAND (expr
, 0);
3968 tree leader
= TREE_CODE (op
) == SSA_NAME
? eliminate_avail (op
) : op
;
3972 tree res
= make_temp_ssa_name (TREE_TYPE (val
), NULL
, "pretmp");
3973 gimple tem
= gimple_build_assign (res
,
3974 fold_build1 (TREE_CODE (expr
),
3975 TREE_TYPE (expr
), leader
));
3976 gsi_insert_before (gsi
, tem
, GSI_SAME_STMT
);
3977 VN_INFO_GET (res
)->valnum
= val
;
3979 if (TREE_CODE (leader
) == SSA_NAME
)
3980 gimple_set_plf (SSA_NAME_DEF_STMT (leader
), NECESSARY
, true);
3982 pre_stats
.insertions
++;
3983 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
3985 fprintf (dump_file
, "Inserted ");
3986 print_gimple_stmt (dump_file
, tem
, 0, 0);
3992 class eliminate_dom_walker
: public dom_walker
3995 eliminate_dom_walker (cdi_direction direction
, bool do_pre_
)
3996 : dom_walker (direction
), do_pre (do_pre_
) {}
3998 virtual void before_dom_children (basic_block
);
3999 virtual void after_dom_children (basic_block
);
4004 /* Perform elimination for the basic-block B during the domwalk. */
4007 eliminate_dom_walker::before_dom_children (basic_block b
)
4009 gimple_stmt_iterator gsi
;
4013 el_avail_stack
.safe_push (NULL_TREE
);
4015 /* ??? If we do nothing for unreachable blocks then this will confuse
4016 tailmerging. Eventually we can reduce its reliance on SCCVN now
4017 that we fully copy/constant-propagate (most) things. */
4019 for (gsi
= gsi_start_phis (b
); !gsi_end_p (gsi
);)
4021 gimple phi
= gsi_stmt (gsi
);
4022 tree res
= PHI_RESULT (phi
);
4024 if (virtual_operand_p (res
))
4030 tree sprime
= eliminate_avail (res
);
4034 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4036 fprintf (dump_file
, "Replaced redundant PHI node defining ");
4037 print_generic_expr (dump_file
, res
, 0);
4038 fprintf (dump_file
, " with ");
4039 print_generic_expr (dump_file
, sprime
, 0);
4040 fprintf (dump_file
, "\n");
4043 /* If we inserted this PHI node ourself, it's not an elimination. */
4045 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
)))
4048 pre_stats
.eliminations
++;
4050 /* If we will propagate into all uses don't bother to do
4052 if (may_propagate_copy (res
, sprime
))
4054 /* Mark the PHI for removal. */
4055 el_to_remove
.safe_push (phi
);
4060 remove_phi_node (&gsi
, false);
4063 && !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (res
))
4064 && TREE_CODE (sprime
) == SSA_NAME
)
4065 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
4067 if (!useless_type_conversion_p (TREE_TYPE (res
), TREE_TYPE (sprime
)))
4068 sprime
= fold_convert (TREE_TYPE (res
), sprime
);
4069 gimple stmt
= gimple_build_assign (res
, sprime
);
4070 /* ??? It cannot yet be necessary (DOM walk). */
4071 gimple_set_plf (stmt
, NECESSARY
, gimple_plf (phi
, NECESSARY
));
4073 gimple_stmt_iterator gsi2
= gsi_after_labels (b
);
4074 gsi_insert_before (&gsi2
, stmt
, GSI_NEW_STMT
);
4078 eliminate_push_avail (res
);
4082 for (gsi
= gsi_start_bb (b
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4084 tree sprime
= NULL_TREE
;
4085 stmt
= gsi_stmt (gsi
);
4086 tree lhs
= gimple_get_lhs (stmt
);
4087 if (lhs
&& TREE_CODE (lhs
) == SSA_NAME
4088 && !gimple_has_volatile_ops (stmt
)
4089 /* See PR43491. Do not replace a global register variable when
4090 it is a the RHS of an assignment. Do replace local register
4091 variables since gcc does not guarantee a local variable will
4092 be allocated in register.
4093 ??? The fix isn't effective here. This should instead
4094 be ensured by not value-numbering them the same but treating
4095 them like volatiles? */
4096 && !(gimple_assign_single_p (stmt
)
4097 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == VAR_DECL
4098 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt
))
4099 && is_global_var (gimple_assign_rhs1 (stmt
)))))
4101 sprime
= eliminate_avail (lhs
);
4104 /* If there is no existing usable leader but SCCVN thinks
4105 it has an expression it wants to use as replacement,
4107 tree val
= VN_INFO (lhs
)->valnum
;
4109 && TREE_CODE (val
) == SSA_NAME
4110 && VN_INFO (val
)->needs_insertion
4111 && VN_INFO (val
)->expr
!= NULL_TREE
4112 && (sprime
= eliminate_insert (&gsi
, val
)) != NULL_TREE
)
4113 eliminate_push_avail (sprime
);
4116 /* If this now constitutes a copy duplicate points-to
4117 and range info appropriately. This is especially
4118 important for inserted code. See tree-ssa-copy.c
4119 for similar code. */
4121 && TREE_CODE (sprime
) == SSA_NAME
)
4123 basic_block sprime_b
= gimple_bb (SSA_NAME_DEF_STMT (sprime
));
4124 if (POINTER_TYPE_P (TREE_TYPE (lhs
))
4125 && SSA_NAME_PTR_INFO (lhs
)
4126 && !SSA_NAME_PTR_INFO (sprime
))
4128 duplicate_ssa_name_ptr_info (sprime
,
4129 SSA_NAME_PTR_INFO (lhs
));
4131 mark_ptr_info_alignment_unknown
4132 (SSA_NAME_PTR_INFO (sprime
));
4134 else if (!POINTER_TYPE_P (TREE_TYPE (lhs
))
4135 && SSA_NAME_RANGE_INFO (lhs
)
4136 && !SSA_NAME_RANGE_INFO (sprime
)
4138 duplicate_ssa_name_range_info (sprime
,
4139 SSA_NAME_RANGE_TYPE (lhs
),
4140 SSA_NAME_RANGE_INFO (lhs
));
4143 /* Inhibit the use of an inserted PHI on a loop header when
4144 the address of the memory reference is a simple induction
4145 variable. In other cases the vectorizer won't do anything
4146 anyway (either it's loop invariant or a complicated
4149 && TREE_CODE (sprime
) == SSA_NAME
4151 && flag_tree_loop_vectorize
4152 && loop_outer (b
->loop_father
)
4153 && has_zero_uses (sprime
)
4154 && bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))
4155 && gimple_assign_load_p (stmt
))
4157 gimple def_stmt
= SSA_NAME_DEF_STMT (sprime
);
4158 basic_block def_bb
= gimple_bb (def_stmt
);
4159 if (gimple_code (def_stmt
) == GIMPLE_PHI
4160 && b
->loop_father
->header
== def_bb
)
4165 FOR_EACH_SSA_TREE_OPERAND (op
, stmt
, iter
, SSA_OP_USE
)
4168 def_bb
= gimple_bb (SSA_NAME_DEF_STMT (op
));
4170 && flow_bb_inside_loop_p (b
->loop_father
, def_bb
)
4171 && simple_iv (b
->loop_father
,
4172 b
->loop_father
, op
, &iv
, true))
4180 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4182 fprintf (dump_file
, "Not replacing ");
4183 print_gimple_expr (dump_file
, stmt
, 0, 0);
4184 fprintf (dump_file
, " with ");
4185 print_generic_expr (dump_file
, sprime
, 0);
4186 fprintf (dump_file
, " which would add a loop"
4187 " carried dependence to loop %d\n",
4188 b
->loop_father
->num
);
4190 /* Don't keep sprime available. */
4198 /* If we can propagate the value computed for LHS into
4199 all uses don't bother doing anything with this stmt. */
4200 if (may_propagate_copy (lhs
, sprime
))
4202 /* Mark it for removal. */
4203 el_to_remove
.safe_push (stmt
);
4205 /* ??? Don't count copy/constant propagations. */
4206 if (gimple_assign_single_p (stmt
)
4207 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4208 || gimple_assign_rhs1 (stmt
) == sprime
))
4211 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4213 fprintf (dump_file
, "Replaced ");
4214 print_gimple_expr (dump_file
, stmt
, 0, 0);
4215 fprintf (dump_file
, " with ");
4216 print_generic_expr (dump_file
, sprime
, 0);
4217 fprintf (dump_file
, " in all uses of ");
4218 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4221 pre_stats
.eliminations
++;
4225 /* If this is an assignment from our leader (which
4226 happens in the case the value-number is a constant)
4227 then there is nothing to do. */
4228 if (gimple_assign_single_p (stmt
)
4229 && sprime
== gimple_assign_rhs1 (stmt
))
4232 /* Else replace its RHS. */
4233 bool can_make_abnormal_goto
4234 = is_gimple_call (stmt
)
4235 && stmt_can_make_abnormal_goto (stmt
);
4237 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4239 fprintf (dump_file
, "Replaced ");
4240 print_gimple_expr (dump_file
, stmt
, 0, 0);
4241 fprintf (dump_file
, " with ");
4242 print_generic_expr (dump_file
, sprime
, 0);
4243 fprintf (dump_file
, " in ");
4244 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4247 if (TREE_CODE (sprime
) == SSA_NAME
)
4248 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4251 pre_stats
.eliminations
++;
4252 gimple orig_stmt
= stmt
;
4253 if (!useless_type_conversion_p (TREE_TYPE (lhs
),
4254 TREE_TYPE (sprime
)))
4255 sprime
= fold_convert (TREE_TYPE (lhs
), sprime
);
4256 tree vdef
= gimple_vdef (stmt
);
4257 tree vuse
= gimple_vuse (stmt
);
4258 propagate_tree_value_into_stmt (&gsi
, sprime
);
4259 stmt
= gsi_stmt (gsi
);
4261 if (vdef
!= gimple_vdef (stmt
))
4262 VN_INFO (vdef
)->valnum
= vuse
;
4264 /* If we removed EH side-effects from the statement, clean
4265 its EH information. */
4266 if (maybe_clean_or_replace_eh_stmt (orig_stmt
, stmt
))
4268 bitmap_set_bit (need_eh_cleanup
,
4269 gimple_bb (stmt
)->index
);
4270 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4271 fprintf (dump_file
, " Removed EH side-effects.\n");
4274 /* Likewise for AB side-effects. */
4275 if (can_make_abnormal_goto
4276 && !stmt_can_make_abnormal_goto (stmt
))
4278 bitmap_set_bit (need_ab_cleanup
,
4279 gimple_bb (stmt
)->index
);
4280 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4281 fprintf (dump_file
, " Removed AB side-effects.\n");
4288 /* If the statement is a scalar store, see if the expression
4289 has the same value number as its rhs. If so, the store is
4291 if (gimple_assign_single_p (stmt
)
4292 && !gimple_has_volatile_ops (stmt
)
4293 && !is_gimple_reg (gimple_assign_lhs (stmt
))
4294 && (TREE_CODE (gimple_assign_rhs1 (stmt
)) == SSA_NAME
4295 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt
))))
4298 tree rhs
= gimple_assign_rhs1 (stmt
);
4299 val
= vn_reference_lookup (gimple_assign_lhs (stmt
),
4300 gimple_vuse (stmt
), VN_WALK
, NULL
);
4301 if (TREE_CODE (rhs
) == SSA_NAME
)
4302 rhs
= VN_INFO (rhs
)->valnum
;
4304 && operand_equal_p (val
, rhs
, 0))
4306 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4308 fprintf (dump_file
, "Deleted redundant store ");
4309 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4312 /* Queue stmt for removal. */
4313 el_to_remove
.safe_push (stmt
);
4318 bool can_make_abnormal_goto
= stmt_can_make_abnormal_goto (stmt
);
4319 bool was_noreturn
= (is_gimple_call (stmt
)
4320 && gimple_call_noreturn_p (stmt
));
4321 tree vdef
= gimple_vdef (stmt
);
4322 tree vuse
= gimple_vuse (stmt
);
4324 /* If we didn't replace the whole stmt (or propagate the result
4325 into all uses), replace all uses on this stmt with their
4327 use_operand_p use_p
;
4329 FOR_EACH_SSA_USE_OPERAND (use_p
, stmt
, iter
, SSA_OP_USE
)
4331 tree use
= USE_FROM_PTR (use_p
);
4332 /* ??? The call code above leaves stmt operands un-updated. */
4333 if (TREE_CODE (use
) != SSA_NAME
)
4335 tree sprime
= eliminate_avail (use
);
4336 if (sprime
&& sprime
!= use
4337 && may_propagate_copy (use
, sprime
)
4338 /* We substitute into debug stmts to avoid excessive
4339 debug temporaries created by removed stmts, but we need
4340 to avoid doing so for inserted sprimes as we never want
4341 to create debug temporaries for them. */
4343 || TREE_CODE (sprime
) != SSA_NAME
4344 || !is_gimple_debug (stmt
)
4345 || !bitmap_bit_p (inserted_exprs
, SSA_NAME_VERSION (sprime
))))
4347 propagate_value (use_p
, sprime
);
4348 gimple_set_modified (stmt
, true);
4349 if (TREE_CODE (sprime
) == SSA_NAME
4350 && !is_gimple_debug (stmt
))
4351 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
),
4356 /* Visit indirect calls and turn them into direct calls if
4357 possible using the devirtualization machinery. */
4358 if (is_gimple_call (stmt
))
4360 tree fn
= gimple_call_fn (stmt
);
4362 && TREE_CODE (fn
) == OBJ_TYPE_REF
4363 && TREE_CODE (OBJ_TYPE_REF_EXPR (fn
)) == SSA_NAME
)
4365 fn
= ipa_intraprocedural_devirtualization (stmt
);
4366 if (fn
&& dbg_cnt (devirt
))
4368 if (dump_enabled_p ())
4370 location_t loc
= gimple_location_safe (stmt
);
4371 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS
, loc
,
4372 "converting indirect call to "
4374 cgraph_get_node (fn
)->name ());
4376 gimple_call_set_fndecl (stmt
, fn
);
4377 gimple_set_modified (stmt
, true);
4382 if (gimple_modified_p (stmt
))
4384 /* If a formerly non-invariant ADDR_EXPR is turned into an
4385 invariant one it was on a separate stmt. */
4386 if (gimple_assign_single_p (stmt
)
4387 && TREE_CODE (gimple_assign_rhs1 (stmt
)) == ADDR_EXPR
)
4388 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt
));
4389 gimple old_stmt
= stmt
;
4390 if (is_gimple_call (stmt
))
4392 /* ??? Only fold calls inplace for now, this may create new
4393 SSA names which in turn will confuse free_scc_vn SSA name
4395 fold_stmt_inplace (&gsi
);
4396 /* When changing a call into a noreturn call, cfg cleanup
4397 is needed to fix up the noreturn call. */
4398 if (!was_noreturn
&& gimple_call_noreturn_p (stmt
))
4399 el_todo
|= TODO_cleanup_cfg
;
4404 stmt
= gsi_stmt (gsi
);
4405 if ((gimple_code (stmt
) == GIMPLE_COND
4406 && (gimple_cond_true_p (stmt
)
4407 || gimple_cond_false_p (stmt
)))
4408 || (gimple_code (stmt
) == GIMPLE_SWITCH
4409 && TREE_CODE (gimple_switch_index (stmt
)) == INTEGER_CST
))
4410 el_todo
|= TODO_cleanup_cfg
;
4412 /* If we removed EH side-effects from the statement, clean
4413 its EH information. */
4414 if (maybe_clean_or_replace_eh_stmt (old_stmt
, stmt
))
4416 bitmap_set_bit (need_eh_cleanup
,
4417 gimple_bb (stmt
)->index
);
4418 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4419 fprintf (dump_file
, " Removed EH side-effects.\n");
4421 /* Likewise for AB side-effects. */
4422 if (can_make_abnormal_goto
4423 && !stmt_can_make_abnormal_goto (stmt
))
4425 bitmap_set_bit (need_ab_cleanup
,
4426 gimple_bb (stmt
)->index
);
4427 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4428 fprintf (dump_file
, " Removed AB side-effects.\n");
4431 if (vdef
!= gimple_vdef (stmt
))
4432 VN_INFO (vdef
)->valnum
= vuse
;
4435 /* Make new values available - for fully redundant LHS we
4436 continue with the next stmt above and skip this. */
4438 FOR_EACH_SSA_DEF_OPERAND (defp
, stmt
, iter
, SSA_OP_DEF
)
4439 eliminate_push_avail (DEF_FROM_PTR (defp
));
4442 /* Replace destination PHI arguments. */
4445 FOR_EACH_EDGE (e
, ei
, b
->succs
)
4447 for (gsi
= gsi_start_phis (e
->dest
); !gsi_end_p (gsi
); gsi_next (&gsi
))
4449 gimple phi
= gsi_stmt (gsi
);
4450 use_operand_p use_p
= PHI_ARG_DEF_PTR_FROM_EDGE (phi
, e
);
4451 tree arg
= USE_FROM_PTR (use_p
);
4452 if (TREE_CODE (arg
) != SSA_NAME
4453 || virtual_operand_p (arg
))
4455 tree sprime
= eliminate_avail (arg
);
4456 if (sprime
&& may_propagate_copy (arg
, sprime
))
4458 propagate_value (use_p
, sprime
);
4459 if (TREE_CODE (sprime
) == SSA_NAME
)
4460 gimple_set_plf (SSA_NAME_DEF_STMT (sprime
), NECESSARY
, true);
4466 /* Make no longer available leaders no longer available. */
4469 eliminate_dom_walker::after_dom_children (basic_block
)
4472 while ((entry
= el_avail_stack
.pop ()) != NULL_TREE
)
4473 el_avail
[SSA_NAME_VERSION (VN_INFO (entry
)->valnum
)] = NULL_TREE
;
4476 /* Eliminate fully redundant computations. */
4479 eliminate (bool do_pre
)
4481 gimple_stmt_iterator gsi
;
4484 need_eh_cleanup
= BITMAP_ALLOC (NULL
);
4485 need_ab_cleanup
= BITMAP_ALLOC (NULL
);
4487 el_to_remove
.create (0);
4489 el_avail
.create (0);
4490 el_avail_stack
.create (0);
4492 eliminate_dom_walker (CDI_DOMINATORS
,
4493 do_pre
).walk (cfun
->cfg
->x_entry_block_ptr
);
4495 el_avail
.release ();
4496 el_avail_stack
.release ();
4498 /* We cannot remove stmts during BB walk, especially not release SSA
4499 names there as this confuses the VN machinery. The stmts ending
4500 up in el_to_remove are either stores or simple copies.
4501 Remove stmts in reverse order to make debug stmt creation possible. */
4502 while (!el_to_remove
.is_empty ())
4504 stmt
= el_to_remove
.pop ();
4506 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4508 fprintf (dump_file
, "Removing dead stmt ");
4509 print_gimple_stmt (dump_file
, stmt
, 0, 0);
4513 if (gimple_code (stmt
) == GIMPLE_PHI
)
4514 lhs
= gimple_phi_result (stmt
);
4516 lhs
= gimple_get_lhs (stmt
);
4519 && TREE_CODE (lhs
) == SSA_NAME
)
4520 bitmap_clear_bit (inserted_exprs
, SSA_NAME_VERSION (lhs
));
4522 gsi
= gsi_for_stmt (stmt
);
4523 if (gimple_code (stmt
) == GIMPLE_PHI
)
4524 remove_phi_node (&gsi
, true);
4527 basic_block bb
= gimple_bb (stmt
);
4528 unlink_stmt_vdef (stmt
);
4529 if (gsi_remove (&gsi
, true))
4530 bitmap_set_bit (need_eh_cleanup
, bb
->index
);
4531 release_defs (stmt
);
4534 /* Removing a stmt may expose a forwarder block. */
4535 el_todo
|= TODO_cleanup_cfg
;
4537 el_to_remove
.release ();
4542 /* Perform CFG cleanups made necessary by elimination. */
4545 fini_eliminate (void)
4547 bool do_eh_cleanup
= !bitmap_empty_p (need_eh_cleanup
);
4548 bool do_ab_cleanup
= !bitmap_empty_p (need_ab_cleanup
);
4551 gimple_purge_all_dead_eh_edges (need_eh_cleanup
);
4554 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup
);
4556 BITMAP_FREE (need_eh_cleanup
);
4557 BITMAP_FREE (need_ab_cleanup
);
4559 if (do_eh_cleanup
|| do_ab_cleanup
)
4560 return TODO_cleanup_cfg
;
4564 /* Borrow a bit of tree-ssa-dce.c for the moment.
4565 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4566 this may be a bit faster, and we may want critical edges kept split. */
4568 /* If OP's defining statement has not already been determined to be necessary,
4569 mark that statement necessary. Return the stmt, if it is newly
4572 static inline gimple
4573 mark_operand_necessary (tree op
)
4579 if (TREE_CODE (op
) != SSA_NAME
)
4582 stmt
= SSA_NAME_DEF_STMT (op
);
4585 if (gimple_plf (stmt
, NECESSARY
)
4586 || gimple_nop_p (stmt
))
4589 gimple_set_plf (stmt
, NECESSARY
, true);
4593 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4594 to insert PHI nodes sometimes, and because value numbering of casts isn't
4595 perfect, we sometimes end up inserting dead code. This simple DCE-like
4596 pass removes any insertions we made that weren't actually used. */
4599 remove_dead_inserted_code (void)
4606 worklist
= BITMAP_ALLOC (NULL
);
4607 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4609 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4610 if (gimple_plf (t
, NECESSARY
))
4611 bitmap_set_bit (worklist
, i
);
4613 while (!bitmap_empty_p (worklist
))
4615 i
= bitmap_first_set_bit (worklist
);
4616 bitmap_clear_bit (worklist
, i
);
4617 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4619 /* PHI nodes are somewhat special in that each PHI alternative has
4620 data and control dependencies. All the statements feeding the
4621 PHI node's arguments are always necessary. */
4622 if (gimple_code (t
) == GIMPLE_PHI
)
4626 for (k
= 0; k
< gimple_phi_num_args (t
); k
++)
4628 tree arg
= PHI_ARG_DEF (t
, k
);
4629 if (TREE_CODE (arg
) == SSA_NAME
)
4631 gimple n
= mark_operand_necessary (arg
);
4633 bitmap_set_bit (worklist
, SSA_NAME_VERSION (arg
));
4639 /* Propagate through the operands. Examine all the USE, VUSE and
4640 VDEF operands in this statement. Mark all the statements
4641 which feed this statement's uses as necessary. */
4645 /* The operands of VDEF expressions are also needed as they
4646 represent potential definitions that may reach this
4647 statement (VDEF operands allow us to follow def-def
4650 FOR_EACH_SSA_TREE_OPERAND (use
, t
, iter
, SSA_OP_ALL_USES
)
4652 gimple n
= mark_operand_necessary (use
);
4654 bitmap_set_bit (worklist
, SSA_NAME_VERSION (use
));
4659 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs
, 0, i
, bi
)
4661 t
= SSA_NAME_DEF_STMT (ssa_name (i
));
4662 if (!gimple_plf (t
, NECESSARY
))
4664 gimple_stmt_iterator gsi
;
4666 if (dump_file
&& (dump_flags
& TDF_DETAILS
))
4668 fprintf (dump_file
, "Removing unnecessary insertion:");
4669 print_gimple_stmt (dump_file
, t
, 0, 0);
4672 gsi
= gsi_for_stmt (t
);
4673 if (gimple_code (t
) == GIMPLE_PHI
)
4674 remove_phi_node (&gsi
, true);
4677 gsi_remove (&gsi
, true);
4682 BITMAP_FREE (worklist
);
4686 /* Initialize data structures used by PRE. */
4693 next_expression_id
= 1;
4694 expressions
.create (0);
4695 expressions
.safe_push (NULL
);
4696 value_expressions
.create (get_max_value_id () + 1);
4697 value_expressions
.safe_grow_cleared (get_max_value_id () + 1);
4698 name_to_id
.create (0);
4700 inserted_exprs
= BITMAP_ALLOC (NULL
);
4702 connect_infinite_loops_to_exit ();
4703 memset (&pre_stats
, 0, sizeof (pre_stats
));
4705 postorder
= XNEWVEC (int, n_basic_blocks_for_fn (cfun
));
4706 postorder_num
= inverted_post_order_compute (postorder
);
4708 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets
));
4710 calculate_dominance_info (CDI_POST_DOMINATORS
);
4711 calculate_dominance_info (CDI_DOMINATORS
);
4713 bitmap_obstack_initialize (&grand_bitmap_obstack
);
4714 phi_translate_table
= new hash_table
<expr_pred_trans_d
> (5110);
4715 expression_to_id
= new hash_table
<pre_expr_d
> (num_ssa_names
* 3);
4716 bitmap_set_pool
= create_alloc_pool ("Bitmap sets",
4717 sizeof (struct bitmap_set
), 30);
4718 pre_expr_pool
= create_alloc_pool ("pre_expr nodes",
4719 sizeof (struct pre_expr_d
), 30);
4720 FOR_ALL_BB_FN (bb
, cfun
)
4722 EXP_GEN (bb
) = bitmap_set_new ();
4723 PHI_GEN (bb
) = bitmap_set_new ();
4724 TMP_GEN (bb
) = bitmap_set_new ();
4725 AVAIL_OUT (bb
) = bitmap_set_new ();
4730 /* Deallocate data structures used by PRE. */
4736 value_expressions
.release ();
4737 BITMAP_FREE (inserted_exprs
);
4738 bitmap_obstack_release (&grand_bitmap_obstack
);
4739 free_alloc_pool (bitmap_set_pool
);
4740 free_alloc_pool (pre_expr_pool
);
4741 delete phi_translate_table
;
4742 phi_translate_table
= NULL
;
4743 delete expression_to_id
;
4744 expression_to_id
= NULL
;
4745 name_to_id
.release ();
4747 free_aux_for_blocks ();
4749 free_dominance_info (CDI_POST_DOMINATORS
);
4754 const pass_data pass_data_pre
=
4756 GIMPLE_PASS
, /* type */
4758 OPTGROUP_NONE
, /* optinfo_flags */
4759 true, /* has_execute */
4760 TV_TREE_PRE
, /* tv_id */
4761 /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before
4763 ( PROP_no_crit_edges
| PROP_cfg
| PROP_ssa
), /* properties_required */
4764 0, /* properties_provided */
4765 PROP_no_crit_edges
, /* properties_destroyed */
4766 TODO_rebuild_alias
, /* todo_flags_start */
4767 0, /* todo_flags_finish */
4770 class pass_pre
: public gimple_opt_pass
4773 pass_pre (gcc::context
*ctxt
)
4774 : gimple_opt_pass (pass_data_pre
, ctxt
)
4777 /* opt_pass methods: */
4778 virtual bool gate (function
*) { return flag_tree_pre
!= 0; }
4779 virtual unsigned int execute (function
*);
4781 }; // class pass_pre
4784 pass_pre::execute (function
*fun
)
4786 unsigned int todo
= 0;
4788 do_partial_partial
=
4789 flag_tree_partial_pre
&& optimize_function_for_speed_p (fun
);
4791 /* This has to happen before SCCVN runs because
4792 loop_optimizer_init may create new phis, etc. */
4793 loop_optimizer_init (LOOPS_NORMAL
);
4795 if (!run_scc_vn (VN_WALK
))
4797 loop_optimizer_finalize ();
4804 /* Collect and value number expressions computed in each basic block. */
4807 /* Insert can get quite slow on an incredibly large number of basic
4808 blocks due to some quadratic behavior. Until this behavior is
4809 fixed, don't run it when he have an incredibly large number of
4810 bb's. If we aren't going to run insert, there is no point in
4811 computing ANTIC, either, even though it's plenty fast. */
4812 if (n_basic_blocks_for_fn (fun
) < 4000)
4818 /* Make sure to remove fake edges before committing our inserts.
4819 This makes sure we don't end up with extra critical edges that
4820 we would need to split. */
4821 remove_fake_exit_edges ();
4822 gsi_commit_edge_inserts ();
4824 /* Remove all the redundant expressions. */
4825 todo
|= eliminate (true);
4827 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
4828 statistics_counter_event (fun
, "PA inserted", pre_stats
.pa_insert
);
4829 statistics_counter_event (fun
, "New PHIs", pre_stats
.phis
);
4830 statistics_counter_event (fun
, "Eliminated", pre_stats
.eliminations
);
4832 clear_expression_ids ();
4833 remove_dead_inserted_code ();
4837 todo
|= fini_eliminate ();
4838 loop_optimizer_finalize ();
4840 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4841 case we can merge the block with the remaining predecessor of the block.
4843 - call merge_blocks after each tail merge iteration
4844 - call merge_blocks after all tail merge iterations
4845 - mark TODO_cleanup_cfg when necessary
4846 - share the cfg cleanup with fini_pre. */
4847 todo
|= tail_merge_optimize (todo
);
4851 /* Tail merging invalidates the virtual SSA web, together with
4852 cfg-cleanup opportunities exposed by PRE this will wreck the
4853 SSA updating machinery. So make sure to run update-ssa
4854 manually, before eventually scheduling cfg-cleanup as part of
4856 update_ssa (TODO_update_ssa_only_virtuals
);
4864 make_pass_pre (gcc::context
*ctxt
)
4866 return new pass_pre (ctxt
);
4871 const pass_data pass_data_fre
=
4873 GIMPLE_PASS
, /* type */
4875 OPTGROUP_NONE
, /* optinfo_flags */
4876 true, /* has_execute */
4877 TV_TREE_FRE
, /* tv_id */
4878 ( PROP_cfg
| PROP_ssa
), /* properties_required */
4879 0, /* properties_provided */
4880 0, /* properties_destroyed */
4881 0, /* todo_flags_start */
4882 0, /* todo_flags_finish */
4885 class pass_fre
: public gimple_opt_pass
4888 pass_fre (gcc::context
*ctxt
)
4889 : gimple_opt_pass (pass_data_fre
, ctxt
)
4892 /* opt_pass methods: */
4893 opt_pass
* clone () { return new pass_fre (m_ctxt
); }
4894 virtual bool gate (function
*) { return flag_tree_fre
!= 0; }
4895 virtual unsigned int execute (function
*);
4897 }; // class pass_fre
4900 pass_fre::execute (function
*fun
)
4902 unsigned int todo
= 0;
4904 if (!run_scc_vn (VN_WALKREWRITE
))
4907 memset (&pre_stats
, 0, sizeof (pre_stats
));
4909 /* Remove all the redundant expressions. */
4910 todo
|= eliminate (false);
4912 todo
|= fini_eliminate ();
4916 statistics_counter_event (fun
, "Insertions", pre_stats
.insertions
);
4917 statistics_counter_event (fun
, "Eliminated", pre_stats
.eliminations
);
4925 make_pass_fre (gcc::context
*ctxt
)
4927 return new pass_fre (ctxt
);