tree.h (enum tree_code_class): Add tcc_vl_exp.
[official-gcc.git] / gcc / tree-ssa-pre.c
blobb39c9e8e9f30c85336d13370e5e8e21bc8889c28
1 /* SSA-PRE for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
4 <stevenb@suse.de>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "ggc.h"
28 #include "tree.h"
29 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-inline.h"
32 #include "tree-flow.h"
33 #include "tree-gimple.h"
34 #include "tree-dump.h"
35 #include "timevar.h"
36 #include "fibheap.h"
37 #include "hashtab.h"
38 #include "tree-iterator.h"
39 #include "real.h"
40 #include "alloc-pool.h"
41 #include "obstack.h"
42 #include "tree-pass.h"
43 #include "flags.h"
44 #include "bitmap.h"
45 #include "langhooks.h"
46 #include "cfgloop.h"
48 /* TODO:
50 1. Avail sets can be shared by making an avail_find_leader that
51 walks up the dominator tree and looks in those avail sets.
52 This might affect code optimality, it's unclear right now.
53 2. Strength reduction can be performed by anticipating expressions
54 we can repair later on.
55 3. We can do back-substitution or smarter value numbering to catch
56 commutative expressions split up over multiple statements.
57 4. ANTIC_SAFE_LOADS could be a lot smarter than it is now.
58 Right now, it is simply calculating loads that occur before
59 any store in a block, instead of loads that occur before
60 stores that affect them. This is relatively more expensive, and
61 it's not clear how much more it will buy us.
64 /* For ease of terminology, "expression node" in the below refers to
65 every expression node but GIMPLE_MODIFY_STMT, because GIMPLE_MODIFY_STMT's
66 represent the actual statement containing the expressions we care about,
67 and we cache the value number by putting it in the expression. */
69 /* Basic algorithm
71 First we walk the statements to generate the AVAIL sets, the
72 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
73 generation of values/expressions by a given block. We use them
74 when computing the ANTIC sets. The AVAIL sets consist of
75 SSA_NAME's that represent values, so we know what values are
76 available in what blocks. AVAIL is a forward dataflow problem. In
77 SSA, values are never killed, so we don't need a kill set, or a
78 fixpoint iteration, in order to calculate the AVAIL sets. In
79 traditional parlance, AVAIL sets tell us the downsafety of the
80 expressions/values.
82 Next, we generate the ANTIC sets. These sets represent the
83 anticipatable expressions. ANTIC is a backwards dataflow
84 problem. An expression is anticipatable in a given block if it could
85 be generated in that block. This means that if we had to perform
86 an insertion in that block, of the value of that expression, we
87 could. Calculating the ANTIC sets requires phi translation of
88 expressions, because the flow goes backwards through phis. We must
89 iterate to a fixpoint of the ANTIC sets, because we have a kill
90 set. Even in SSA form, values are not live over the entire
91 function, only from their definition point onwards. So we have to
92 remove values from the ANTIC set once we go past the definition
93 point of the leaders that make them up.
94 compute_antic/compute_antic_aux performs this computation.
96 Third, we perform insertions to make partially redundant
97 expressions fully redundant.
99 An expression is partially redundant (excluding partial
100 anticipation) if:
102 1. It is AVAIL in some, but not all, of the predecessors of a
103 given block.
104 2. It is ANTIC in all the predecessors.
106 In order to make it fully redundant, we insert the expression into
107 the predecessors where it is not available, but is ANTIC.
109 For the partial anticipation case, we only perform insertion if it
110 is partially anticipated in some block, and fully available in all
111 of the predecessors.
113 insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
114 performs these steps.
116 Fourth, we eliminate fully redundant expressions.
117 This is a simple statement walk that replaces redundant
118 calculations with the now available values. */
120 /* Representations of value numbers:
122 Value numbers are represented using the "value handle" approach.
123 This means that each SSA_NAME (and for other reasons to be
124 disclosed in a moment, expression nodes) has a value handle that
125 can be retrieved through get_value_handle. This value handle *is*
126 the value number of the SSA_NAME. You can pointer compare the
127 value handles for equivalence purposes.
129 For debugging reasons, the value handle is internally more than
130 just a number, it is a VALUE_HANDLE named "VH.x", where x is a
131 unique number for each value number in use. This allows
132 expressions with SSA_NAMES replaced by value handles to still be
133 pretty printed in a sane way. They simply print as "VH.3 *
134 VH.5", etc.
136 Expression nodes have value handles associated with them as a
137 cache. Otherwise, we'd have to look them up again in the hash
138 table This makes significant difference (factor of two or more) on
139 some test cases. They can be thrown away after the pass is
140 finished. */
142 /* Representation of expressions on value numbers:
144 In some portions of this code, you will notice we allocate "fake"
145 analogues to the expression we are value numbering, and replace the
146 operands with the values of the expression. Since we work on
147 values, and not just names, we canonicalize expressions to value
148 expressions for use in the ANTIC sets, the EXP_GEN set, etc.
150 This is theoretically unnecessary, it just saves a bunch of
151 repeated get_value_handle and find_leader calls in the remainder of
152 the code, trading off temporary memory usage for speed. The tree
153 nodes aren't actually creating more garbage, since they are
154 allocated in a special pools which are thrown away at the end of
155 this pass.
157 All of this also means that if you print the EXP_GEN or ANTIC sets,
158 you will see "VH.5 + VH.7" in the set, instead of "a_55 +
159 b_66" or something. The only thing that actually cares about
160 seeing the value leaders is phi translation, and it needs to be
161 able to find the leader for a value in an arbitrary block, so this
162 "value expression" form is perfect for it (otherwise you'd do
163 get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
166 /* Representation of sets:
168 There are currently two types of sets used, hopefully to be unified soon.
169 The AVAIL sets do not need to be sorted in any particular order,
170 and thus, are simply represented as two bitmaps, one that keeps
171 track of values present in the set, and one that keeps track of
172 expressions present in the set.
174 The other sets are represented as doubly linked lists kept in topological
175 order, with an optional supporting bitmap of values present in the
176 set. The sets represent values, and the elements can be values or
177 expressions. The elements can appear in different sets, but each
178 element can only appear once in each set.
180 Since each node in the set represents a value, we also want to be
181 able to map expression, set pairs to something that tells us
182 whether the value is present is a set. We use a per-set bitmap for
183 that. The value handles also point to a linked list of the
184 expressions they represent via a tree annotation. This is mainly
185 useful only for debugging, since we don't do identity lookups. */
188 /* Next global expression id number. */
189 static unsigned int next_expression_id;
191 /* Mapping from expression to id number we can use in bitmap sets. */
192 static VEC(tree, heap) *expressions;
194 /* Allocate an expression id for EXPR. */
196 static inline unsigned int
197 alloc_expression_id (tree expr)
199 tree_ann_common_t ann;
201 ann = get_tree_common_ann (expr);
203 /* Make sure we won't overflow. */
204 gcc_assert (next_expression_id + 1 > next_expression_id);
206 ann->aux = XNEW (unsigned int);
207 * ((unsigned int *)ann->aux) = next_expression_id++;
208 VEC_safe_push (tree, heap, expressions, expr);
209 return next_expression_id - 1;
212 /* Return the expression id for tree EXPR. */
214 static inline unsigned int
215 get_expression_id (tree expr)
217 tree_ann_common_t ann = tree_common_ann (expr);
218 gcc_assert (ann);
219 gcc_assert (ann->aux);
221 return *((unsigned int *)ann->aux);
224 /* Return the existing expression id for EXPR, or create one if one
225 does not exist yet. */
227 static inline unsigned int
228 get_or_alloc_expression_id (tree expr)
230 tree_ann_common_t ann = tree_common_ann (expr);
232 if (ann == NULL || !ann->aux)
233 return alloc_expression_id (expr);
235 return get_expression_id (expr);
238 /* Return the expression that has expression id ID */
240 static inline tree
241 expression_for_id (unsigned int id)
243 return VEC_index (tree, expressions, id);
246 /* Free the expression id field in all of our expressions,
247 and then destroy the expressions array. */
249 static void
250 clear_expression_ids (void)
252 int i;
253 tree expr;
255 for (i = 0; VEC_iterate (tree, expressions, i, expr); i++)
257 free (tree_common_ann (expr)->aux);
258 tree_common_ann (expr)->aux = NULL;
260 VEC_free (tree, heap, expressions);
263 static bool in_fre = false;
265 /* An unordered bitmap set. One bitmap tracks values, the other,
266 expressions. */
267 typedef struct bitmap_set
269 bitmap expressions;
270 bitmap values;
271 } *bitmap_set_t;
273 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
274 EXECUTE_IF_SET_IN_BITMAP(set->expressions, 0, id, bi)
276 /* Sets that we need to keep track of. */
277 typedef struct bb_bitmap_sets
279 /* The EXP_GEN set, which represents expressions/values generated in
280 a basic block. */
281 bitmap_set_t exp_gen;
283 /* The PHI_GEN set, which represents PHI results generated in a
284 basic block. */
285 bitmap_set_t phi_gen;
287 /* The TMP_GEN set, which represents results/temporaries generated
288 in a basic block. IE the LHS of an expression. */
289 bitmap_set_t tmp_gen;
291 /* The AVAIL_OUT set, which represents which values are available in
292 a given basic block. */
293 bitmap_set_t avail_out;
295 /* The ANTIC_IN set, which represents which values are anticipatable
296 in a given basic block. */
297 bitmap_set_t antic_in;
299 /* The PA_IN set, which represents which values are
300 partially anticipatable in a given basic block. */
301 bitmap_set_t pa_in;
303 /* The NEW_SETS set, which is used during insertion to augment the
304 AVAIL_OUT set of blocks with the new insertions performed during
305 the current iteration. */
306 bitmap_set_t new_sets;
308 /* The RVUSE sets, which are used during ANTIC computation to ensure
309 that we don't mark loads ANTIC once they have died. */
310 bitmap rvuse_in;
311 bitmap rvuse_out;
312 bitmap rvuse_gen;
313 bitmap rvuse_kill;
315 /* For actually occurring loads, as long as they occur before all the
316 other stores in the block, we know they are antic at the top of
317 the block, regardless of RVUSE_KILL. */
318 bitmap_set_t antic_safe_loads;
320 /* True if we have visited this block during ANTIC calculation. */
321 unsigned int visited:1;
323 /* True we have deferred processing this block during ANTIC
324 calculation until its successor is processed. */
325 unsigned int deferred : 1;
326 } *bb_value_sets_t;
328 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
329 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
330 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
331 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
332 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
333 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
334 #define RVUSE_IN(BB) ((bb_value_sets_t) ((BB)->aux))->rvuse_in
335 #define RVUSE_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->rvuse_gen
336 #define RVUSE_KILL(BB) ((bb_value_sets_t) ((BB)->aux))->rvuse_kill
337 #define RVUSE_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->rvuse_out
338 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
339 #define ANTIC_SAFE_LOADS(BB) ((bb_value_sets_t) ((BB)->aux))->antic_safe_loads
340 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
341 #define BB_DEFERRED(BB) ((bb_value_sets_t) ((BB)->aux))->deferred
343 /* Maximal set of values, used to initialize the ANTIC problem, which
344 is an intersection problem. */
345 static bitmap_set_t maximal_set;
347 /* Basic block list in postorder. */
348 static int *postorder;
350 /* This structure is used to keep track of statistics on what
351 optimization PRE was able to perform. */
352 static struct
354 /* The number of RHS computations eliminated by PRE. */
355 int eliminations;
357 /* The number of new expressions/temporaries generated by PRE. */
358 int insertions;
360 /* The number of inserts found due to partial anticipation */
361 int pa_insert;
363 /* The number of new PHI nodes added by PRE. */
364 int phis;
366 /* The number of values found constant. */
367 int constified;
369 } pre_stats;
371 static bool do_partial_partial;
372 static tree bitmap_find_leader (bitmap_set_t, tree);
373 static void bitmap_value_insert_into_set (bitmap_set_t, tree);
374 static void bitmap_value_replace_in_set (bitmap_set_t, tree);
375 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
376 static bool bitmap_set_contains_value (bitmap_set_t, tree);
377 static void bitmap_insert_into_set (bitmap_set_t, tree);
378 static bitmap_set_t bitmap_set_new (void);
379 static bool is_undefined_value (tree);
380 static tree create_expression_by_pieces (basic_block, tree, tree);
381 static tree find_or_generate_expression (basic_block, tree, tree);
383 /* We can add and remove elements and entries to and from sets
384 and hash tables, so we use alloc pools for them. */
386 static alloc_pool bitmap_set_pool;
387 static alloc_pool binary_node_pool;
388 static alloc_pool unary_node_pool;
389 static alloc_pool reference_node_pool;
390 static alloc_pool comparison_node_pool;
391 static alloc_pool modify_expr_node_pool;
392 static bitmap_obstack grand_bitmap_obstack;
394 /* We can't use allocation pools to hold temporary CALL_EXPR objects, since
395 they are not of fixed size. Instead, use an obstack. */
397 static struct obstack temp_call_expr_obstack;
400 /* To avoid adding 300 temporary variables when we only need one, we
401 only create one temporary variable, on demand, and build ssa names
402 off that. We do have to change the variable if the types don't
403 match the current variable's type. */
404 static tree pretemp;
405 static tree storetemp;
406 static tree mergephitemp;
407 static tree prephitemp;
409 /* Set of blocks with statements that have had its EH information
410 cleaned up. */
411 static bitmap need_eh_cleanup;
413 /* The phi_translate_table caches phi translations for a given
414 expression and predecessor. */
416 static htab_t phi_translate_table;
418 /* A three tuple {e, pred, v} used to cache phi translations in the
419 phi_translate_table. */
421 typedef struct expr_pred_trans_d
423 /* The expression. */
424 tree e;
426 /* The predecessor block along which we translated the expression. */
427 basic_block pred;
429 /* vuses associated with the expression. */
430 VEC (tree, gc) *vuses;
432 /* The value that resulted from the translation. */
433 tree v;
435 /* The hashcode for the expression, pred pair. This is cached for
436 speed reasons. */
437 hashval_t hashcode;
438 } *expr_pred_trans_t;
440 /* Return the hash value for a phi translation table entry. */
442 static hashval_t
443 expr_pred_trans_hash (const void *p)
445 const expr_pred_trans_t ve = (expr_pred_trans_t) p;
446 return ve->hashcode;
449 /* Return true if two phi translation table entries are the same.
450 P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
452 static int
453 expr_pred_trans_eq (const void *p1, const void *p2)
455 const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
456 const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
457 basic_block b1 = ve1->pred;
458 basic_block b2 = ve2->pred;
459 int i;
460 tree vuse1;
462 /* If they are not translations for the same basic block, they can't
463 be equal. */
464 if (b1 != b2)
465 return false;
468 /* If they are for the same basic block, determine if the
469 expressions are equal. */
470 if (!expressions_equal_p (ve1->e, ve2->e))
471 return false;
473 /* Make sure the vuses are equivalent. */
474 if (ve1->vuses == ve2->vuses)
475 return true;
477 if (VEC_length (tree, ve1->vuses) != VEC_length (tree, ve2->vuses))
478 return false;
480 for (i = 0; VEC_iterate (tree, ve1->vuses, i, vuse1); i++)
482 if (VEC_index (tree, ve2->vuses, i) != vuse1)
483 return false;
486 return true;
489 /* Search in the phi translation table for the translation of
490 expression E in basic block PRED with vuses VUSES.
491 Return the translated value, if found, NULL otherwise. */
493 static inline tree
494 phi_trans_lookup (tree e, basic_block pred, VEC (tree, gc) *vuses)
496 void **slot;
497 struct expr_pred_trans_d ept;
499 ept.e = e;
500 ept.pred = pred;
501 ept.vuses = vuses;
502 ept.hashcode = vn_compute (e, (unsigned long) pred);
503 slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
504 NO_INSERT);
505 if (!slot)
506 return NULL;
507 else
508 return ((expr_pred_trans_t) *slot)->v;
512 /* Add the tuple mapping from {expression E, basic block PRED, vuses VUSES} to
513 value V, to the phi translation table. */
515 static inline void
516 phi_trans_add (tree e, tree v, basic_block pred, VEC (tree, gc) *vuses)
518 void **slot;
519 expr_pred_trans_t new_pair = XNEW (struct expr_pred_trans_d);
520 new_pair->e = e;
521 new_pair->pred = pred;
522 new_pair->vuses = vuses;
523 new_pair->v = v;
524 new_pair->hashcode = vn_compute (e, (unsigned long) pred);
525 slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
526 new_pair->hashcode, INSERT);
527 if (*slot)
528 free (*slot);
529 *slot = (void *) new_pair;
533 /* Return true if V is a value expression that represents itself.
534 In our world, this is *only* non-value handles. */
536 static inline bool
537 constant_expr_p (tree v)
539 return TREE_CODE (v) != VALUE_HANDLE && is_gimple_min_invariant (v);
540 /* return TREE_CODE (v) != VALUE_HANDLE; */
543 /* Add expression E to the expression set of value V. */
545 void
546 add_to_value (tree v, tree e)
548 /* Constants have no expression sets. */
549 if (constant_expr_p (v))
550 return;
552 if (VALUE_HANDLE_EXPR_SET (v) == NULL)
553 VALUE_HANDLE_EXPR_SET (v) = bitmap_set_new ();
555 bitmap_insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
558 /* Create a new bitmap set and return it. */
560 static bitmap_set_t
561 bitmap_set_new (void)
563 bitmap_set_t ret = (bitmap_set_t) pool_alloc (bitmap_set_pool);
564 ret->expressions = BITMAP_ALLOC (&grand_bitmap_obstack);
565 ret->values = BITMAP_ALLOC (&grand_bitmap_obstack);
566 return ret;
569 /* Remove an expression EXPR from a bitmapped set. */
571 static void
572 bitmap_remove_from_set (bitmap_set_t set, tree expr)
574 tree val = get_value_handle (expr);
576 gcc_assert (val);
577 if (!constant_expr_p (val))
579 bitmap_clear_bit (set->values, VALUE_HANDLE_ID (val));
580 bitmap_clear_bit (set->expressions, get_expression_id (expr));
584 /* Insert an expression EXPR into a bitmapped set. */
586 static void
587 bitmap_insert_into_set (bitmap_set_t set, tree expr)
589 tree val = get_value_handle (expr);
591 gcc_assert (val);
592 if (!constant_expr_p (val))
594 bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
595 bitmap_set_bit (set->expressions, get_or_alloc_expression_id (expr));
599 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
601 static void
602 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
604 bitmap_copy (dest->expressions, orig->expressions);
605 bitmap_copy (dest->values, orig->values);
609 /* Free memory used up by SET. */
610 static void
611 bitmap_set_free (bitmap_set_t set)
613 BITMAP_FREE (set->expressions);
614 BITMAP_FREE (set->values);
618 /* A comparison function for use in qsort to top sort a bitmap set. Simply
619 subtracts value handle ids, since they are created in topo-order. */
621 static int
622 vh_compare (const void *pa, const void *pb)
624 const tree vha = get_value_handle (*((const tree *)pa));
625 const tree vhb = get_value_handle (*((const tree *)pb));
627 /* This can happen when we constify things. */
628 if (constant_expr_p (vha))
630 if (constant_expr_p (vhb))
631 return -1;
632 return -1;
634 else if (constant_expr_p (vhb))
635 return 1;
636 return VALUE_HANDLE_ID (vha) - VALUE_HANDLE_ID (vhb);
639 /* Generate an topological-ordered array of bitmap set SET. */
641 static VEC(tree, heap) *
642 sorted_array_from_bitmap_set (bitmap_set_t set)
644 unsigned int i;
645 bitmap_iterator bi;
646 VEC(tree, heap) *result = NULL;
648 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
649 VEC_safe_push (tree, heap, result, expression_for_id (i));
651 qsort (VEC_address (tree, result), VEC_length (tree, result),
652 sizeof (tree), vh_compare);
654 return result;
657 /* Perform bitmapped set operation DEST &= ORIG. */
659 static void
660 bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig)
662 bitmap_iterator bi;
663 unsigned int i;
665 if (dest != orig)
667 bitmap temp = BITMAP_ALLOC (&grand_bitmap_obstack);
669 bitmap_and_into (dest->values, orig->values);
671 bitmap_copy (temp, dest->expressions);
672 EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
674 tree expr = expression_for_id (i);
675 tree val = get_value_handle (expr);
676 if (!bitmap_bit_p (dest->values, VALUE_HANDLE_ID (val)))
677 bitmap_clear_bit (dest->expressions, i);
679 BITMAP_FREE (temp);
683 /* Subtract all values and expressions contained in ORIG from DEST. */
685 static bitmap_set_t
686 bitmap_set_subtract (bitmap_set_t dest, bitmap_set_t orig)
688 bitmap_set_t result = bitmap_set_new ();
689 bitmap_iterator bi;
690 unsigned int i;
692 bitmap_and_compl (result->expressions, dest->expressions,
693 orig->expressions);
695 FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
697 tree expr = expression_for_id (i);
698 tree val = get_value_handle (expr);
699 bitmap_set_bit (result->values, VALUE_HANDLE_ID (val));
702 return result;
705 /* Subtract all the values in bitmap set B from bitmap set A. */
707 static void
708 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
710 unsigned int i;
711 bitmap_iterator bi;
712 bitmap temp = BITMAP_ALLOC (&grand_bitmap_obstack);
714 bitmap_copy (temp, a->expressions);
715 EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
717 tree expr = expression_for_id (i);
718 if (bitmap_set_contains_value (b, get_value_handle (expr)))
719 bitmap_remove_from_set (a, expr);
721 BITMAP_FREE (temp);
725 /* Return true if bitmapped set SET contains the value VAL. */
727 static bool
728 bitmap_set_contains_value (bitmap_set_t set, tree val)
730 if (constant_expr_p (val))
731 return true;
733 if (!set || bitmap_empty_p (set->expressions))
734 return false;
736 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
739 static inline bool
740 bitmap_set_contains_expr (bitmap_set_t set, tree expr)
742 return bitmap_bit_p (set->expressions, get_expression_id (expr));
745 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
747 static void
748 bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
750 bitmap_set_t exprset;
751 unsigned int i;
752 bitmap_iterator bi;
754 if (constant_expr_p (lookfor))
755 return;
757 if (!bitmap_set_contains_value (set, lookfor))
758 return;
760 /* The number of expressions having a given value is usually
761 significantly less than the total number of expressions in SET.
762 Thus, rather than check, for each expression in SET, whether it
763 has the value LOOKFOR, we walk the reverse mapping that tells us
764 what expressions have a given value, and see if any of those
765 expressions are in our set. For large testcases, this is about
766 5-10x faster than walking the bitmap. If this is somehow a
767 significant lose for some cases, we can choose which set to walk
768 based on the set size. */
769 exprset = VALUE_HANDLE_EXPR_SET (lookfor);
770 FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
772 if (bitmap_bit_p (set->expressions, i))
774 bitmap_clear_bit (set->expressions, i);
775 bitmap_set_bit (set->expressions, get_expression_id (expr));
776 return;
781 /* Return true if two bitmap sets are equal. */
783 static bool
784 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
786 return bitmap_equal_p (a->values, b->values);
789 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
790 and add it otherwise. */
792 static void
793 bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
795 tree val = get_value_handle (expr);
797 if (bitmap_set_contains_value (set, val))
798 bitmap_set_replace_value (set, val, expr);
799 else
800 bitmap_insert_into_set (set, expr);
803 /* Insert EXPR into SET if EXPR's value is not already present in
804 SET. */
806 static void
807 bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
809 tree val = get_value_handle (expr);
811 if (constant_expr_p (val))
812 return;
814 if (!bitmap_set_contains_value (set, val))
815 bitmap_insert_into_set (set, expr);
818 /* Print out SET to OUTFILE. */
820 static void
821 print_bitmap_set (FILE *outfile, bitmap_set_t set,
822 const char *setname, int blockindex)
824 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
825 if (set)
827 bool first = true;
828 unsigned i;
829 bitmap_iterator bi;
831 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
833 tree expr = expression_for_id (i);
835 if (!first)
836 fprintf (outfile, ", ");
837 first = false;
838 print_generic_expr (outfile, expr, 0);
840 fprintf (outfile, " (");
841 print_generic_expr (outfile, get_value_handle (expr), 0);
842 fprintf (outfile, ") ");
845 fprintf (outfile, " }\n");
848 void debug_bitmap_set (bitmap_set_t);
850 void
851 debug_bitmap_set (bitmap_set_t set)
853 print_bitmap_set (stderr, set, "debug", 0);
856 /* Print out the expressions that have VAL to OUTFILE. */
858 void
859 print_value_expressions (FILE *outfile, tree val)
861 if (VALUE_HANDLE_EXPR_SET (val))
863 char s[10];
864 sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
865 print_bitmap_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
870 void
871 debug_value_expressions (tree val)
873 print_value_expressions (stderr, val);
876 /* Return the folded version of T if T, when folded, is a gimple
877 min_invariant. Otherwise, return T. */
879 static tree
880 fully_constant_expression (tree t)
882 tree folded;
883 folded = fold (t);
884 if (folded && is_gimple_min_invariant (folded))
885 return folded;
886 return t;
889 /* Make a temporary copy of a CALL_EXPR object NODE. */
891 static tree
892 temp_copy_call_expr (tree node)
894 return (tree) obstack_copy (&temp_call_expr_obstack, node, tree_size (node));
897 /* Translate the vuses in the VUSES vector backwards through phi nodes
898 in PHIBLOCK, so that they have the value they would have in
899 BLOCK. */
901 static VEC(tree, gc) *
902 translate_vuses_through_block (VEC (tree, gc) *vuses,
903 basic_block phiblock,
904 basic_block block)
906 tree oldvuse;
907 VEC(tree, gc) *result = NULL;
908 int i;
910 for (i = 0; VEC_iterate (tree, vuses, i, oldvuse); i++)
912 tree phi = SSA_NAME_DEF_STMT (oldvuse);
913 if (TREE_CODE (phi) == PHI_NODE
914 && bb_for_stmt (phi) == phiblock)
916 edge e = find_edge (block, bb_for_stmt (phi));
917 if (e)
919 tree def = PHI_ARG_DEF (phi, e->dest_idx);
920 if (def != oldvuse)
922 if (!result)
923 result = VEC_copy (tree, gc, vuses);
924 VEC_replace (tree, result, i, def);
930 /* We avoid creating a new copy of the vuses unless something
931 actually changed, so result can be NULL. */
932 if (result)
934 sort_vuses (result);
935 return result;
937 return vuses;
941 /* Like find_leader, but checks for the value existing in SET1 *or*
942 SET2. This is used to avoid making a set consisting of the union
943 of PA_IN and ANTIC_IN during insert. */
945 static inline tree
946 find_leader_in_sets (tree expr, bitmap_set_t set1, bitmap_set_t set2)
948 tree result;
950 result = bitmap_find_leader (set1, expr);
951 if (!result && set2)
952 result = bitmap_find_leader (set2, expr);
953 return result;
956 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
957 the phis in PRED. Return NULL if we can't find a leader for each
958 part of the translated expression. */
960 static tree
961 phi_translate (tree expr, bitmap_set_t set1, bitmap_set_t set2,
962 basic_block pred, basic_block phiblock)
964 tree phitrans = NULL;
965 tree oldexpr = expr;
967 if (expr == NULL)
968 return NULL;
970 if (is_gimple_min_invariant (expr))
971 return expr;
973 /* Phi translations of a given expression don't change. */
974 if (EXPR_P (expr) || GIMPLE_STMT_P (expr))
976 tree vh;
978 vh = get_value_handle (expr);
979 if (vh && TREE_CODE (vh) == VALUE_HANDLE)
980 phitrans = phi_trans_lookup (expr, pred, VALUE_HANDLE_VUSES (vh));
981 else
982 phitrans = phi_trans_lookup (expr, pred, NULL);
984 else
985 phitrans = phi_trans_lookup (expr, pred, NULL);
987 if (phitrans)
988 return phitrans;
990 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
992 case tcc_expression:
993 return NULL;
995 case tcc_vl_exp:
997 if (TREE_CODE (expr) != CALL_EXPR)
998 return NULL;
999 else
1001 tree oldfn = CALL_EXPR_FN (expr);
1002 tree oldsc = CALL_EXPR_STATIC_CHAIN (expr);
1003 tree newfn, newsc = NULL;
1004 tree newexpr = NULL_TREE;
1005 tree vh = get_value_handle (expr);
1006 bool invariantarg = false;
1007 int i, nargs;
1008 VEC (tree, gc) *vuses = VALUE_HANDLE_VUSES (vh);
1009 VEC (tree, gc) *tvuses;
1011 newfn = phi_translate (find_leader_in_sets (oldfn, set1, set2),
1012 set1, set2, pred, phiblock);
1013 if (newfn == NULL)
1014 return NULL;
1015 if (newfn != oldfn)
1017 newexpr = temp_copy_call_expr (expr);
1018 CALL_EXPR_FN (newexpr) = get_value_handle (newfn);
1020 if (oldsc)
1022 newsc = phi_translate (find_leader_in_sets (oldsc, set1, set2),
1023 set1, set2, pred, phiblock);
1024 if (newsc == NULL)
1025 return NULL;
1026 if (newsc != oldsc)
1028 if (!newexpr)
1029 newexpr = temp_copy_call_expr (expr);
1030 CALL_EXPR_STATIC_CHAIN (newexpr) = get_value_handle (newsc);
1034 /* phi translate the argument list piece by piece. */
1035 nargs = call_expr_nargs (expr);
1036 for (i = 0; i < nargs; i++)
1038 tree oldval = CALL_EXPR_ARG (expr, i);
1039 tree newval;
1040 if (oldval)
1042 /* This may seem like a weird place for this
1043 check, but it's actually the easiest place to
1044 do it. We can't do it lower on in the
1045 recursion because it's valid for pieces of a
1046 component ref to be of AGGREGATE_TYPE, as long
1047 as the outermost one is not.
1048 To avoid *that* case, we have a check for
1049 AGGREGATE_TYPE_P in insert_aux. However, that
1050 check will *not* catch this case because here
1051 it occurs in the argument list. */
1052 if (AGGREGATE_TYPE_P (TREE_TYPE (oldval)))
1053 return NULL;
1054 oldval = find_leader_in_sets (oldval, set1, set2);
1055 newval = phi_translate (oldval, set1, set2, pred,
1056 phiblock);
1057 if (newval == NULL)
1058 return NULL;
1059 if (newval != oldval)
1061 invariantarg |= is_gimple_min_invariant (newval);
1062 if (!newexpr)
1063 newexpr = temp_copy_call_expr (expr);
1064 CALL_EXPR_ARG (newexpr, i) = get_value_handle (newval);
1069 /* In case of new invariant args we might try to fold the call
1070 again. */
1071 if (invariantarg && !newsc)
1073 tree tmp1 = build_call_array (TREE_TYPE (expr),
1074 newfn, call_expr_nargs (newexpr),
1075 CALL_EXPR_ARGP (newexpr));
1076 tree tmp2 = fold (tmp1);
1077 if (tmp2 != tmp1)
1079 STRIP_TYPE_NOPS (tmp2);
1080 if (is_gimple_min_invariant (tmp2))
1081 return tmp2;
1085 tvuses = translate_vuses_through_block (vuses, phiblock, pred);
1086 if (vuses != tvuses && ! newexpr)
1087 newexpr = temp_copy_call_expr (expr);
1089 if (newexpr)
1091 newexpr->base.ann = NULL;
1092 vn_lookup_or_add_with_vuses (newexpr, tvuses);
1093 expr = newexpr;
1094 phi_trans_add (oldexpr, newexpr, pred, tvuses);
1098 return expr;
1100 case tcc_declaration:
1102 VEC (tree, gc) * oldvuses = NULL;
1103 VEC (tree, gc) * newvuses = NULL;
1105 oldvuses = VALUE_HANDLE_VUSES (get_value_handle (expr));
1106 if (oldvuses)
1107 newvuses = translate_vuses_through_block (oldvuses, phiblock,
1108 pred);
1110 if (oldvuses != newvuses)
1111 vn_lookup_or_add_with_vuses (expr, newvuses);
1113 phi_trans_add (oldexpr, expr, pred, newvuses);
1115 return expr;
1117 case tcc_reference:
1119 tree oldop0 = TREE_OPERAND (expr, 0);
1120 tree oldop1 = NULL;
1121 tree newop0;
1122 tree newop1 = NULL;
1123 tree oldop2 = NULL;
1124 tree newop2 = NULL;
1125 tree oldop3 = NULL;
1126 tree newop3 = NULL;
1127 tree newexpr;
1128 VEC (tree, gc) * oldvuses = NULL;
1129 VEC (tree, gc) * newvuses = NULL;
1131 if (TREE_CODE (expr) != INDIRECT_REF
1132 && TREE_CODE (expr) != COMPONENT_REF
1133 && TREE_CODE (expr) != ARRAY_REF)
1134 return NULL;
1136 oldop0 = find_leader_in_sets (oldop0, set1, set2);
1137 newop0 = phi_translate (oldop0, set1, set2, pred, phiblock);
1138 if (newop0 == NULL)
1139 return NULL;
1141 if (TREE_CODE (expr) == ARRAY_REF)
1143 oldop1 = TREE_OPERAND (expr, 1);
1144 oldop1 = find_leader_in_sets (oldop1, set1, set2);
1145 newop1 = phi_translate (oldop1, set1, set2, pred, phiblock);
1147 if (newop1 == NULL)
1148 return NULL;
1150 oldop2 = TREE_OPERAND (expr, 2);
1151 if (oldop2)
1153 oldop2 = find_leader_in_sets (oldop2, set1, set2);
1154 newop2 = phi_translate (oldop2, set1, set2, pred, phiblock);
1156 if (newop2 == NULL)
1157 return NULL;
1159 oldop3 = TREE_OPERAND (expr, 3);
1160 if (oldop3)
1162 oldop3 = find_leader_in_sets (oldop3, set1, set2);
1163 newop3 = phi_translate (oldop3, set1, set2, pred, phiblock);
1165 if (newop3 == NULL)
1166 return NULL;
1170 oldvuses = VALUE_HANDLE_VUSES (get_value_handle (expr));
1171 if (oldvuses)
1172 newvuses = translate_vuses_through_block (oldvuses, phiblock,
1173 pred);
1175 if (newop0 != oldop0 || newvuses != oldvuses
1176 || newop1 != oldop1
1177 || newop2 != oldop2
1178 || newop3 != oldop3)
1180 tree t;
1182 newexpr = (tree) pool_alloc (reference_node_pool);
1183 memcpy (newexpr, expr, tree_size (expr));
1184 TREE_OPERAND (newexpr, 0) = get_value_handle (newop0);
1185 if (TREE_CODE (expr) == ARRAY_REF)
1187 TREE_OPERAND (newexpr, 1) = get_value_handle (newop1);
1188 if (newop2)
1189 TREE_OPERAND (newexpr, 2) = get_value_handle (newop2);
1190 if (newop3)
1191 TREE_OPERAND (newexpr, 3) = get_value_handle (newop3);
1194 t = fully_constant_expression (newexpr);
1196 if (t != newexpr)
1198 pool_free (reference_node_pool, newexpr);
1199 newexpr = t;
1201 else
1203 newexpr->base.ann = NULL;
1204 vn_lookup_or_add_with_vuses (newexpr, newvuses);
1206 expr = newexpr;
1207 phi_trans_add (oldexpr, newexpr, pred, newvuses);
1210 return expr;
1211 break;
1213 case tcc_binary:
1214 case tcc_comparison:
1216 tree oldop1 = TREE_OPERAND (expr, 0);
1217 tree oldval1 = oldop1;
1218 tree oldop2 = TREE_OPERAND (expr, 1);
1219 tree oldval2 = oldop2;
1220 tree newop1;
1221 tree newop2;
1222 tree newexpr;
1224 oldop1 = find_leader_in_sets (oldop1, set1, set2);
1225 newop1 = phi_translate (oldop1, set1, set2, pred, phiblock);
1226 if (newop1 == NULL)
1227 return NULL;
1229 oldop2 = find_leader_in_sets (oldop2, set1, set2);
1230 newop2 = phi_translate (oldop2, set1, set2, pred, phiblock);
1231 if (newop2 == NULL)
1232 return NULL;
1233 if (newop1 != oldop1 || newop2 != oldop2)
1235 tree t;
1236 newexpr = (tree) pool_alloc (binary_node_pool);
1237 memcpy (newexpr, expr, tree_size (expr));
1238 TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldval1 : get_value_handle (newop1);
1239 TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldval2 : get_value_handle (newop2);
1240 t = fully_constant_expression (newexpr);
1241 if (t != newexpr)
1243 pool_free (binary_node_pool, newexpr);
1244 newexpr = t;
1246 else
1248 newexpr->base.ann = NULL;
1249 vn_lookup_or_add (newexpr, NULL);
1251 expr = newexpr;
1252 phi_trans_add (oldexpr, newexpr, pred, NULL);
1255 return expr;
1257 case tcc_unary:
1259 tree oldop1 = TREE_OPERAND (expr, 0);
1260 tree newop1;
1261 tree newexpr;
1263 oldop1 = find_leader_in_sets (oldop1, set1, set2);
1264 newop1 = phi_translate (oldop1, set1, set2, pred, phiblock);
1265 if (newop1 == NULL)
1266 return NULL;
1267 if (newop1 != oldop1)
1269 tree t;
1270 newexpr = (tree) pool_alloc (unary_node_pool);
1271 memcpy (newexpr, expr, tree_size (expr));
1272 TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
1273 t = fully_constant_expression (newexpr);
1274 if (t != newexpr)
1276 pool_free (unary_node_pool, newexpr);
1277 newexpr = t;
1279 else
1281 newexpr->base.ann = NULL;
1282 vn_lookup_or_add (newexpr, NULL);
1284 expr = newexpr;
1285 phi_trans_add (oldexpr, newexpr, pred, NULL);
1288 return expr;
1290 case tcc_exceptional:
1292 tree phi = NULL;
1293 edge e;
1294 tree def_stmt;
1295 gcc_assert (TREE_CODE (expr) == SSA_NAME);
1297 def_stmt = SSA_NAME_DEF_STMT (expr);
1298 if (TREE_CODE (def_stmt) == PHI_NODE
1299 && bb_for_stmt (def_stmt) == phiblock)
1300 phi = def_stmt;
1301 else
1302 return expr;
1304 e = find_edge (pred, bb_for_stmt (phi));
1305 if (e)
1307 if (is_undefined_value (PHI_ARG_DEF (phi, e->dest_idx)))
1308 return NULL;
1309 vn_lookup_or_add (PHI_ARG_DEF (phi, e->dest_idx), NULL);
1310 return PHI_ARG_DEF (phi, e->dest_idx);
1313 return expr;
1315 default:
1316 gcc_unreachable ();
1320 /* For each expression in SET, translate the value handles through phi nodes
1321 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1322 expressions in DEST. */
1324 static void
1325 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
1326 basic_block phiblock)
1328 VEC (tree, heap) *exprs;
1329 tree expr;
1330 int i;
1332 if (!phi_nodes (phiblock))
1334 bitmap_set_copy (dest, set);
1335 return;
1338 exprs = sorted_array_from_bitmap_set (set);
1339 for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
1341 tree translated;
1342 translated = phi_translate (expr, set, NULL, pred, phiblock);
1344 /* Don't add constants or empty translations to the cache, since
1345 we won't look them up that way, or use the result, anyway. */
1346 if (translated && !is_gimple_min_invariant (translated))
1348 tree vh = get_value_handle (translated);
1349 VEC (tree, gc) *vuses;
1351 /* The value handle itself may also be an invariant, in
1352 which case, it has no vuses. */
1353 vuses = !is_gimple_min_invariant (vh)
1354 ? VALUE_HANDLE_VUSES (vh) : NULL;
1355 phi_trans_add (expr, translated, pred, vuses);
1358 if (translated != NULL)
1359 bitmap_value_insert_into_set (dest, translated);
1361 VEC_free (tree, heap, exprs);
1364 /* Find the leader for a value (i.e., the name representing that
1365 value) in a given set, and return it. Return NULL if no leader is
1366 found. */
1368 static tree
1369 bitmap_find_leader (bitmap_set_t set, tree val)
1371 if (val == NULL)
1372 return NULL;
1374 if (constant_expr_p (val))
1375 return val;
1377 if (bitmap_set_contains_value (set, val))
1379 /* Rather than walk the entire bitmap of expressions, and see
1380 whether any of them has the value we are looking for, we look
1381 at the reverse mapping, which tells us the set of expressions
1382 that have a given value (IE value->expressions with that
1383 value) and see if any of those expressions are in our set.
1384 The number of expressions per value is usually significantly
1385 less than the number of expressions in the set. In fact, for
1386 large testcases, doing it this way is roughly 5-10x faster
1387 than walking the bitmap.
1388 If this is somehow a significant lose for some cases, we can
1389 choose which set to walk based on which set is smaller. */
1390 unsigned int i;
1391 bitmap_iterator bi;
1392 bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
1394 EXECUTE_IF_AND_IN_BITMAP (exprset->expressions,
1395 set->expressions, 0, i, bi)
1396 return expression_for_id (i);
1398 return NULL;
1401 /* Given the vuse representative map, MAP, and an SSA version number,
1402 ID, return the bitmap of names ID represents, or NULL, if none
1403 exists. */
1405 static bitmap
1406 get_representative (bitmap *map, int id)
1408 if (map[id] != NULL)
1409 return map[id];
1410 return NULL;
1413 /* A vuse is anticipable at the top of block x, from the bottom of the
1414 block, if it reaches the top of the block, and is not killed in the
1415 block. In effect, we are trying to see if the vuse is transparent
1416 backwards in the block. */
1418 static bool
1419 vuses_dies_in_block_x (VEC (tree, gc) *vuses, basic_block block)
1421 int i;
1422 tree vuse;
1424 for (i = 0; VEC_iterate (tree, vuses, i, vuse); i++)
1426 /* Any places where this is too conservative, are places
1427 where we created a new version and shouldn't have. */
1429 if (!bitmap_bit_p (RVUSE_IN (block), SSA_NAME_VERSION (vuse))
1430 || bitmap_bit_p (RVUSE_KILL (block), SSA_NAME_VERSION (vuse)))
1431 return true;
1433 return false;
1436 /* Determine if the expression EXPR is valid in SET1 U SET2.
1437 ONLY SET2 CAN BE NULL.
1438 This means that we have a leader for each part of the expression
1439 (if it consists of values), or the expression is an SSA_NAME.
1441 NB: We never should run into a case where we have SSA_NAME +
1442 SSA_NAME or SSA_NAME + value. The sets valid_in_sets is called on,
1443 the ANTIC sets, will only ever have SSA_NAME's or value expressions
1444 (IE VALUE1 + VALUE2, *VALUE1, VALUE1 < VALUE2) */
1446 #define union_contains_value(SET1, SET2, VAL) \
1447 (bitmap_set_contains_value ((SET1), (VAL)) \
1448 || ((SET2) && bitmap_set_contains_value ((SET2), (VAL))))
1450 static bool
1451 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree expr,
1452 basic_block block)
1454 tree vh = get_value_handle (expr);
1455 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1457 case tcc_binary:
1458 case tcc_comparison:
1460 tree op1 = TREE_OPERAND (expr, 0);
1461 tree op2 = TREE_OPERAND (expr, 1);
1463 return union_contains_value (set1, set2, op1)
1464 && union_contains_value (set1, set2, op2);
1467 case tcc_unary:
1469 tree op1 = TREE_OPERAND (expr, 0);
1470 return union_contains_value (set1, set2, op1);
1473 case tcc_expression:
1474 return false;
1476 case tcc_vl_exp:
1478 if (TREE_CODE (expr) == CALL_EXPR)
1480 tree fn = CALL_EXPR_FN (expr);
1481 tree sc = CALL_EXPR_STATIC_CHAIN (expr);
1482 tree arg;
1483 call_expr_arg_iterator iter;
1485 /* Check the non-argument operands first. */
1486 if (!union_contains_value (set1, set2, fn)
1487 || (sc && !union_contains_value (set1, set2, sc)))
1488 return false;
1490 /* Now check the operands. */
1491 FOR_EACH_CALL_EXPR_ARG (arg, iter, expr)
1493 if (!union_contains_value (set1, set2, arg))
1494 return false;
1496 return !vuses_dies_in_block_x (VALUE_HANDLE_VUSES (vh), block);
1498 return false;
1501 case tcc_reference:
1503 if (TREE_CODE (expr) == INDIRECT_REF
1504 || TREE_CODE (expr) == COMPONENT_REF
1505 || TREE_CODE (expr) == ARRAY_REF)
1507 tree op0 = TREE_OPERAND (expr, 0);
1508 gcc_assert (is_gimple_min_invariant (op0)
1509 || TREE_CODE (op0) == VALUE_HANDLE);
1510 if (!union_contains_value (set1, set2, op0))
1511 return false;
1512 if (TREE_CODE (expr) == ARRAY_REF)
1514 tree op1 = TREE_OPERAND (expr, 1);
1515 tree op2 = TREE_OPERAND (expr, 2);
1516 tree op3 = TREE_OPERAND (expr, 3);
1517 gcc_assert (is_gimple_min_invariant (op1)
1518 || TREE_CODE (op1) == VALUE_HANDLE);
1519 if (!union_contains_value (set1, set2, op1))
1520 return false;
1521 gcc_assert (!op2 || is_gimple_min_invariant (op2)
1522 || TREE_CODE (op2) == VALUE_HANDLE);
1523 if (op2
1524 && !union_contains_value (set1, set2, op2))
1525 return false;
1526 gcc_assert (!op3 || is_gimple_min_invariant (op3)
1527 || TREE_CODE (op3) == VALUE_HANDLE);
1528 if (op3
1529 && !union_contains_value (set1, set2, op3))
1530 return false;
1532 return bitmap_set_contains_value (ANTIC_SAFE_LOADS (block),
1534 || !vuses_dies_in_block_x (VALUE_HANDLE_VUSES (vh),
1535 block);
1538 return false;
1540 case tcc_exceptional:
1542 gcc_assert (TREE_CODE (expr) == SSA_NAME);
1543 return bitmap_set_contains_expr (AVAIL_OUT (block), expr);
1546 case tcc_declaration:
1547 return !vuses_dies_in_block_x (VALUE_HANDLE_VUSES (vh), block);
1549 default:
1550 /* No other cases should be encountered. */
1551 gcc_unreachable ();
1555 /* Clean the set of expressions that are no longer valid in SET1 or
1556 SET2. This means expressions that are made up of values we have no
1557 leaders for in SET1 or SET2. This version is used for partial
1558 anticipation, which means it is not valid in either ANTIC_IN or
1559 PA_IN. */
1561 static void
1562 dependent_clean (bitmap_set_t set1, bitmap_set_t set2, basic_block block)
1564 VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set1);
1565 tree expr;
1566 int i;
1568 for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
1570 if (!valid_in_sets (set1, set2, expr, block))
1571 bitmap_remove_from_set (set1, expr);
1573 VEC_free (tree, heap, exprs);
1576 /* Clean the set of expressions that are no longer valid in SET. This
1577 means expressions that are made up of values we have no leaders for
1578 in SET. */
1580 static void
1581 clean (bitmap_set_t set, basic_block block)
1583 VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set);
1584 tree expr;
1585 int i;
1587 for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
1589 if (!valid_in_sets (set, NULL, expr, block))
1590 bitmap_remove_from_set (set, expr);
1592 VEC_free (tree, heap, exprs);
1595 static sbitmap has_abnormal_preds;
1598 /* List of blocks that may have changed during ANTIC computation and
1599 thus need to be iterated over. */
1601 static sbitmap changed_blocks;
1602 /* Compute the ANTIC set for BLOCK.
1604 If succs(BLOCK) > 1 then
1605 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
1606 else if succs(BLOCK) == 1 then
1607 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
1609 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
1612 static bool
1613 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
1615 bool changed = false;
1616 bitmap_set_t S, old, ANTIC_OUT;
1617 bitmap_iterator bi;
1618 unsigned int bii;
1619 edge e;
1620 edge_iterator ei;
1622 old = ANTIC_OUT = S = NULL;
1623 BB_VISITED (block) = 1;
1625 /* If any edges from predecessors are abnormal, antic_in is empty,
1626 so do nothing. */
1627 if (block_has_abnormal_pred_edge)
1628 goto maybe_dump_sets;
1630 old = ANTIC_IN (block);
1631 ANTIC_OUT = bitmap_set_new ();
1633 /* If the block has no successors, ANTIC_OUT is empty. */
1634 if (EDGE_COUNT (block->succs) == 0)
1636 /* If we have one successor, we could have some phi nodes to
1637 translate through. */
1638 else if (single_succ_p (block))
1640 basic_block succ_bb = single_succ (block);
1642 /* We trade iterations of the dataflow equations for having to
1643 phi translate the maximal set, which is incredibly slow
1644 (since the maximal set often has 300+ members, even when you
1645 have a small number of blocks).
1646 Basically, we defer the computation of ANTIC for this block
1647 until we have processed it's successor, which will inevitably
1648 have a *much* smaller set of values to phi translate once
1649 clean has been run on it.
1650 The cost of doing this is that we technically perform more
1651 iterations, however, they are lower cost iterations.
1653 Timings for PRE on tramp3d-v4:
1654 without maximal set fix: 11 seconds
1655 with maximal set fix/without deferring: 26 seconds
1656 with maximal set fix/with deferring: 11 seconds
1659 if (!BB_VISITED (succ_bb))
1661 changed = true;
1662 SET_BIT (changed_blocks, block->index);
1663 BB_VISITED (block) = 0;
1664 BB_DEFERRED (block) = 1;
1665 goto maybe_dump_sets;
1667 else
1668 phi_translate_set (ANTIC_OUT, ANTIC_IN (succ_bb),
1669 block, succ_bb);
1673 /* If we have multiple successors, we take the intersection of all of
1674 them. */
1675 else
1677 VEC(basic_block, heap) * worklist;
1678 size_t i;
1679 basic_block bprime, first;
1681 worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
1682 FOR_EACH_EDGE (e, ei, block->succs)
1683 VEC_quick_push (basic_block, worklist, e->dest);
1684 first = VEC_index (basic_block, worklist, 0);
1686 if (!BB_VISITED (first))
1687 bitmap_set_copy (ANTIC_OUT, maximal_set);
1688 else
1689 bitmap_set_copy (ANTIC_OUT, ANTIC_IN (first));
1691 for (i = 1; VEC_iterate (basic_block, worklist, i, bprime); i++)
1693 if (!BB_VISITED (bprime))
1694 bitmap_set_and (ANTIC_OUT, maximal_set);
1695 else
1696 bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime));
1698 VEC_free (basic_block, heap, worklist);
1701 /* Generate ANTIC_OUT - TMP_GEN. */
1702 S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block));
1704 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
1705 ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block),
1706 TMP_GEN (block));
1708 /* Then union in the ANTIC_OUT - TMP_GEN values,
1709 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
1710 FOR_EACH_EXPR_ID_IN_SET (S, bii, bi)
1711 bitmap_value_insert_into_set (ANTIC_IN (block),
1712 expression_for_id (bii));
1714 clean (ANTIC_IN (block), block);
1716 /* !old->expressions can happen when we deferred a block. */
1717 if (!old->expressions || !bitmap_set_equal (old, ANTIC_IN (block)))
1719 changed = true;
1720 SET_BIT (changed_blocks, block->index);
1721 FOR_EACH_EDGE (e, ei, block->preds)
1722 SET_BIT (changed_blocks, e->src->index);
1724 else
1725 RESET_BIT (changed_blocks, block->index);
1727 maybe_dump_sets:
1728 if (dump_file && (dump_flags & TDF_DETAILS))
1730 if (!BB_DEFERRED (block) || BB_VISITED (block))
1732 if (ANTIC_OUT)
1733 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1735 if (ANTIC_SAFE_LOADS (block))
1736 print_bitmap_set (dump_file, ANTIC_SAFE_LOADS (block),
1737 "ANTIC_SAFE_LOADS", block->index);
1738 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
1739 block->index);
1741 if (S)
1742 print_bitmap_set (dump_file, S, "S", block->index);
1744 else
1746 fprintf (dump_file,
1747 "Block %d was deferred for a future iteration.\n",
1748 block->index);
1751 if (old)
1752 bitmap_set_free (old);
1753 if (S)
1754 bitmap_set_free (S);
1755 if (ANTIC_OUT)
1756 bitmap_set_free (ANTIC_OUT);
1757 return changed;
1760 /* Compute PARTIAL_ANTIC for BLOCK.
1762 If succs(BLOCK) > 1 then
1763 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
1764 in ANTIC_OUT for all succ(BLOCK)
1765 else if succs(BLOCK) == 1 then
1766 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
1768 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
1769 - ANTIC_IN[BLOCK])
1772 static bool
1773 compute_partial_antic_aux (basic_block block,
1774 bool block_has_abnormal_pred_edge)
1776 bool changed = false;
1777 bitmap_set_t old_PA_IN;
1778 bitmap_set_t PA_OUT;
1779 edge e;
1780 edge_iterator ei;
1782 old_PA_IN = PA_OUT = NULL;
1784 /* If any edges from predecessors are abnormal, antic_in is empty,
1785 so do nothing. */
1786 if (block_has_abnormal_pred_edge)
1787 goto maybe_dump_sets;
1789 old_PA_IN = PA_IN (block);
1790 PA_OUT = bitmap_set_new ();
1792 /* If the block has no successors, ANTIC_OUT is empty. */
1793 if (EDGE_COUNT (block->succs) == 0)
1795 /* If we have one successor, we could have some phi nodes to
1796 translate through. Note that we can't phi translate across DFS
1797 back edges in partial antic, because it uses a union operation
1798 on the successors. For recurrences like IV's, we will end up generating a
1799 new value in the set on each go around (i + 3 (VH.1) VH.1 + 1
1800 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
1801 else if (single_succ_p (block))
1803 basic_block succ = single_succ (block);
1804 if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK))
1805 phi_translate_set (PA_OUT, PA_IN (succ), block, succ);
1807 /* If we have multiple successors, we take the union of all of
1808 them. */
1809 else
1811 VEC(basic_block, heap) * worklist;
1812 size_t i;
1813 basic_block bprime;
1815 worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
1816 FOR_EACH_EDGE (e, ei, block->succs)
1818 if (e->flags & EDGE_DFS_BACK)
1819 continue;
1820 VEC_quick_push (basic_block, worklist, e->dest);
1822 if (VEC_length (basic_block, worklist) > 0)
1824 for (i = 0; VEC_iterate (basic_block, worklist, i, bprime); i++)
1826 unsigned int i;
1827 bitmap_iterator bi;
1829 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi)
1830 bitmap_value_insert_into_set (PA_OUT,
1831 expression_for_id (i));
1833 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi)
1834 bitmap_value_insert_into_set (PA_OUT,
1835 expression_for_id (i));
1838 VEC_free (basic_block, heap, worklist);
1841 /* PA_IN starts with PA_OUT - TMP_GEN.
1842 Then we subtract things from ANTIC_IN. */
1843 PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block));
1845 /* For partial antic, we want to put back in the phi results, since
1846 we will properly avoid making them partially antic over backedges. */
1847 bitmap_ior_into (PA_IN (block)->values, PHI_GEN (block)->values);
1848 bitmap_ior_into (PA_IN (block)->expressions, PHI_GEN (block)->expressions);
1850 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
1851 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
1853 dependent_clean (PA_IN (block), ANTIC_IN (block), block);
1855 if (!bitmap_set_equal (old_PA_IN, PA_IN (block)))
1857 changed = true;
1858 SET_BIT (changed_blocks, block->index);
1859 FOR_EACH_EDGE (e, ei, block->preds)
1860 SET_BIT (changed_blocks, e->src->index);
1862 else
1863 RESET_BIT (changed_blocks, block->index);
1865 maybe_dump_sets:
1866 if (dump_file && (dump_flags & TDF_DETAILS))
1868 if (PA_OUT)
1869 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
1871 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
1873 if (old_PA_IN)
1874 bitmap_set_free (old_PA_IN);
1875 if (PA_OUT)
1876 bitmap_set_free (PA_OUT);
1877 return changed;
1880 /* Compute ANTIC and partial ANTIC sets. */
1882 static void
1883 compute_antic (void)
1885 bool changed = true;
1886 int num_iterations = 0;
1887 basic_block block;
1888 int i;
1890 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
1891 We pre-build the map of blocks with incoming abnormal edges here. */
1892 has_abnormal_preds = sbitmap_alloc (last_basic_block);
1893 sbitmap_zero (has_abnormal_preds);
1895 FOR_EACH_BB (block)
1897 edge_iterator ei;
1898 edge e;
1900 FOR_EACH_EDGE (e, ei, block->preds)
1902 e->flags &= ~EDGE_DFS_BACK;
1903 if (e->flags & EDGE_ABNORMAL)
1905 SET_BIT (has_abnormal_preds, block->index);
1906 break;
1910 BB_VISITED (block) = 0;
1911 BB_DEFERRED (block) = 0;
1912 /* While we are here, give empty ANTIC_IN sets to each block. */
1913 ANTIC_IN (block) = bitmap_set_new ();
1914 PA_IN (block) = bitmap_set_new ();
1917 /* At the exit block we anticipate nothing. */
1918 ANTIC_IN (EXIT_BLOCK_PTR) = bitmap_set_new ();
1919 BB_VISITED (EXIT_BLOCK_PTR) = 1;
1920 PA_IN (EXIT_BLOCK_PTR) = bitmap_set_new ();
1922 changed_blocks = sbitmap_alloc (last_basic_block + 1);
1923 sbitmap_ones (changed_blocks);
1924 while (changed)
1926 if (dump_file && (dump_flags & TDF_DETAILS))
1927 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
1928 num_iterations++;
1929 changed = false;
1930 for (i = 0; i < last_basic_block - NUM_FIXED_BLOCKS; i++)
1932 if (TEST_BIT (changed_blocks, postorder[i]))
1934 basic_block block = BASIC_BLOCK (postorder[i]);
1935 changed |= compute_antic_aux (block,
1936 TEST_BIT (has_abnormal_preds,
1937 block->index));
1940 /* Theoretically possible, but *highly* unlikely. */
1941 gcc_assert (num_iterations < 50);
1944 if (dump_file && (dump_flags & TDF_STATS))
1945 fprintf (dump_file, "compute_antic required %d iterations\n",
1946 num_iterations);
1948 if (do_partial_partial)
1950 sbitmap_ones (changed_blocks);
1951 mark_dfs_back_edges ();
1952 num_iterations = 0;
1953 changed = true;
1954 while (changed)
1956 if (dump_file && (dump_flags & TDF_DETAILS))
1957 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
1958 num_iterations++;
1959 changed = false;
1960 for (i = 0; i < last_basic_block - NUM_FIXED_BLOCKS; i++)
1962 if (TEST_BIT (changed_blocks, postorder[i]))
1964 basic_block block = BASIC_BLOCK (postorder[i]);
1965 changed
1966 |= compute_partial_antic_aux (block,
1967 TEST_BIT (has_abnormal_preds,
1968 block->index));
1971 /* Theoretically possible, but *highly* unlikely. */
1972 gcc_assert (num_iterations < 50);
1974 if (dump_file && (dump_flags & TDF_STATS))
1975 fprintf (dump_file, "compute_partial_antic required %d iterations\n",
1976 num_iterations);
1978 sbitmap_free (has_abnormal_preds);
1979 sbitmap_free (changed_blocks);
1982 /* Print the names represented by the bitmap NAMES, to the file OUT. */
1984 static void
1985 dump_bitmap_of_names (FILE *out, bitmap names)
1987 bitmap_iterator bi;
1988 unsigned int i;
1990 fprintf (out, " { ");
1991 EXECUTE_IF_SET_IN_BITMAP (names, 0, i, bi)
1993 print_generic_expr (out, ssa_name (i), 0);
1994 fprintf (out, " ");
1996 fprintf (out, "}\n");
1999 /* Compute a set of representative vuse versions for each phi. This
2000 is so we can compute conservative kill sets in terms of all vuses
2001 that are killed, instead of continually walking chains.
2003 We also have to be able kill all names associated with a phi when
2004 the phi dies in order to ensure we don't generate overlapping
2005 live ranges, which are not allowed in virtual SSA. */
2007 static bitmap *vuse_names;
2008 static void
2009 compute_vuse_representatives (void)
2011 tree phi;
2012 basic_block bb;
2013 VEC (tree, heap) *phis = NULL;
2014 bool changed = true;
2015 size_t i;
2017 FOR_EACH_BB (bb)
2019 for (phi = phi_nodes (bb);
2020 phi;
2021 phi = PHI_CHAIN (phi))
2022 if (!is_gimple_reg (PHI_RESULT (phi)))
2023 VEC_safe_push (tree, heap, phis, phi);
2026 while (changed)
2028 changed = false;
2030 for (i = 0; VEC_iterate (tree, phis, i, phi); i++)
2032 size_t ver = SSA_NAME_VERSION (PHI_RESULT (phi));
2033 use_operand_p usep;
2034 ssa_op_iter iter;
2036 if (vuse_names[ver] == NULL)
2038 vuse_names[ver] = BITMAP_ALLOC (&grand_bitmap_obstack);
2039 bitmap_set_bit (vuse_names[ver], ver);
2041 FOR_EACH_PHI_ARG (usep, phi, iter, SSA_OP_ALL_USES)
2043 tree use = USE_FROM_PTR (usep);
2044 bitmap usebitmap = get_representative (vuse_names,
2045 SSA_NAME_VERSION (use));
2046 if (usebitmap != NULL)
2048 changed |= bitmap_ior_into (vuse_names[ver],
2049 usebitmap);
2051 else
2053 changed |= !bitmap_bit_p (vuse_names[ver],
2054 SSA_NAME_VERSION (use));
2055 if (changed)
2056 bitmap_set_bit (vuse_names[ver],
2057 SSA_NAME_VERSION (use));
2063 if (dump_file && (dump_flags & TDF_DETAILS))
2064 for (i = 0; VEC_iterate (tree, phis, i, phi); i++)
2066 bitmap reps = get_representative (vuse_names,
2067 SSA_NAME_VERSION (PHI_RESULT (phi)));
2068 if (reps)
2070 print_generic_expr (dump_file, PHI_RESULT (phi), 0);
2071 fprintf (dump_file, " represents ");
2072 dump_bitmap_of_names (dump_file, reps);
2075 VEC_free (tree, heap, phis);
2078 /* Compute reaching vuses and antic safe loads. RVUSE computation is
2079 is a small bit of iterative dataflow to determine what virtual uses
2080 reach what blocks. Because we can't generate overlapping virtual
2081 uses, and virtual uses *do* actually die, this ends up being faster
2082 in most cases than continually walking the virtual use/def chains
2083 to determine whether we are inside a block where a given virtual is
2084 still available to be used.
2086 ANTIC_SAFE_LOADS are those loads that actually occur before any kill to
2087 their vuses in the block,and thus, are safe at the top of the
2088 block.
2090 An example:
2092 <block begin>
2093 b = *a
2094 *a = 9
2095 <block end>
2097 b = *a is an antic safe load because it still safe to consider it
2098 ANTIC at the top of the block.
2100 We currently compute a conservative approximation to
2101 ANTIC_SAFE_LOADS. We compute those loads that occur before *any*
2102 stores in the block. This is not because it is difficult to
2103 compute the precise answer, but because it is expensive. More
2104 testing is necessary to determine whether it is worth computing the
2105 precise answer. */
2107 static void
2108 compute_rvuse_and_antic_safe (void)
2111 unsigned int i;
2112 tree phi;
2113 basic_block bb;
2114 bool changed = true;
2115 unsigned int *first_store_uid;
2117 first_store_uid = xcalloc (n_basic_blocks, sizeof (unsigned int));
2119 compute_vuse_representatives ();
2121 FOR_ALL_BB (bb)
2123 RVUSE_IN (bb) = BITMAP_ALLOC (&grand_bitmap_obstack);
2124 RVUSE_GEN (bb) = BITMAP_ALLOC (&grand_bitmap_obstack);
2125 RVUSE_KILL (bb) = BITMAP_ALLOC (&grand_bitmap_obstack);
2126 RVUSE_OUT (bb) = BITMAP_ALLOC (&grand_bitmap_obstack);
2127 ANTIC_SAFE_LOADS (bb) = NULL;
2130 /* Mark live on entry */
2131 for (i = 0; i < num_ssa_names; i++)
2133 tree name = ssa_name (i);
2134 if (name && !is_gimple_reg (name)
2135 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (name)))
2136 bitmap_set_bit (RVUSE_OUT (ENTRY_BLOCK_PTR),
2137 SSA_NAME_VERSION (name));
2140 /* Compute local sets for reaching vuses.
2141 GEN(block) = generated in block and not locally killed.
2142 KILL(block) = set of vuses killed in block.
2145 FOR_EACH_BB (bb)
2147 block_stmt_iterator bsi;
2148 ssa_op_iter iter;
2149 def_operand_p defp;
2150 use_operand_p usep;
2152 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2154 tree stmt = bsi_stmt (bsi);
2156 if (first_store_uid[bb->index] == 0
2157 && !ZERO_SSA_OPERANDS (stmt, SSA_OP_VMAYUSE | SSA_OP_VDEF))
2159 first_store_uid[bb->index] = stmt_ann (stmt)->uid;
2162 FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_VMAYUSE)
2164 tree use = USE_FROM_PTR (usep);
2165 bitmap repbit = get_representative (vuse_names,
2166 SSA_NAME_VERSION (use));
2167 if (repbit != NULL)
2169 bitmap_and_compl_into (RVUSE_GEN (bb), repbit);
2170 bitmap_ior_into (RVUSE_KILL (bb), repbit);
2172 else
2174 bitmap_set_bit (RVUSE_KILL (bb), SSA_NAME_VERSION (use));
2175 bitmap_clear_bit (RVUSE_GEN (bb), SSA_NAME_VERSION (use));
2178 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_VIRTUAL_DEFS)
2180 tree def = DEF_FROM_PTR (defp);
2181 bitmap_set_bit (RVUSE_GEN (bb), SSA_NAME_VERSION (def));
2186 FOR_EACH_BB (bb)
2188 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2190 if (!is_gimple_reg (PHI_RESULT (phi)))
2192 edge e;
2193 edge_iterator ei;
2195 tree def = PHI_RESULT (phi);
2196 /* In reality, the PHI result is generated at the end of
2197 each predecessor block. This will make the value
2198 LVUSE_IN for the bb containing the PHI, which is
2199 correct. */
2200 FOR_EACH_EDGE (e, ei, bb->preds)
2201 bitmap_set_bit (RVUSE_GEN (e->src), SSA_NAME_VERSION (def));
2206 /* Solve reaching vuses.
2208 RVUSE_IN[BB] = Union of RVUSE_OUT of predecessors.
2209 RVUSE_OUT[BB] = RVUSE_GEN[BB] U (RVUSE_IN[BB] - RVUSE_KILL[BB])
2212 changed = true;
2213 while (changed)
2215 int j;
2216 changed = false;
2217 for (j = n_basic_blocks - NUM_FIXED_BLOCKS - 1; j >= 0; j--)
2219 edge e;
2220 edge_iterator ei;
2221 bb = BASIC_BLOCK (postorder[j]);
2223 FOR_EACH_EDGE (e, ei, bb->preds)
2224 bitmap_ior_into (RVUSE_IN (bb), RVUSE_OUT (e->src));
2226 changed |= bitmap_ior_and_compl (RVUSE_OUT (bb),
2227 RVUSE_GEN (bb),
2228 RVUSE_IN (bb),
2229 RVUSE_KILL (bb));
2233 if (dump_file && (dump_flags & TDF_DETAILS))
2235 FOR_ALL_BB (bb)
2237 fprintf (dump_file, "RVUSE_IN (%d) =", bb->index);
2238 dump_bitmap_of_names (dump_file, RVUSE_IN (bb));
2240 fprintf (dump_file, "RVUSE_KILL (%d) =", bb->index);
2241 dump_bitmap_of_names (dump_file, RVUSE_KILL (bb));
2243 fprintf (dump_file, "RVUSE_GEN (%d) =", bb->index);
2244 dump_bitmap_of_names (dump_file, RVUSE_GEN (bb));
2246 fprintf (dump_file, "RVUSE_OUT (%d) =", bb->index);
2247 dump_bitmap_of_names (dump_file, RVUSE_OUT (bb));
2251 FOR_EACH_BB (bb)
2253 bitmap_iterator bi;
2255 if (bitmap_empty_p (RVUSE_KILL (bb)))
2256 continue;
2258 FOR_EACH_EXPR_ID_IN_SET (EXP_GEN (bb), i, bi)
2260 tree expr = expression_for_id (i);
2261 if (REFERENCE_CLASS_P (expr))
2263 tree vh = get_value_handle (expr);
2264 tree maybe = bitmap_find_leader (AVAIL_OUT (bb), vh);
2266 if (maybe)
2268 tree def = SSA_NAME_DEF_STMT (maybe);
2270 if (bb_for_stmt (def) != bb)
2271 continue;
2273 if (TREE_CODE (def) == PHI_NODE
2274 || stmt_ann (def)->uid < first_store_uid[bb->index])
2276 if (ANTIC_SAFE_LOADS (bb) == NULL)
2277 ANTIC_SAFE_LOADS (bb) = bitmap_set_new ();
2278 bitmap_value_insert_into_set (ANTIC_SAFE_LOADS (bb),
2279 expr);
2285 free (first_store_uid);
2288 /* Return true if we can value number the call in STMT. This is true
2289 if we have a pure or constant call. */
2291 static bool
2292 can_value_number_call (tree stmt)
2294 tree call = get_call_expr_in (stmt);
2296 if (call_expr_flags (call) & (ECF_PURE | ECF_CONST))
2297 return true;
2298 return false;
2301 /* Return true if OP is a tree which we can perform value numbering
2302 on. */
2304 static bool
2305 can_value_number_operation (tree op)
2307 return UNARY_CLASS_P (op)
2308 || BINARY_CLASS_P (op)
2309 || COMPARISON_CLASS_P (op)
2310 || REFERENCE_CLASS_P (op)
2311 || (TREE_CODE (op) == CALL_EXPR
2312 && can_value_number_call (op));
2316 /* Return true if OP is a tree which we can perform PRE on
2317 on. This may not match the operations we can value number, but in
2318 a perfect world would. */
2320 static bool
2321 can_PRE_operation (tree op)
2323 return UNARY_CLASS_P (op)
2324 || BINARY_CLASS_P (op)
2325 || COMPARISON_CLASS_P (op)
2326 || TREE_CODE (op) == INDIRECT_REF
2327 || TREE_CODE (op) == COMPONENT_REF
2328 || TREE_CODE (op) == CALL_EXPR
2329 || TREE_CODE (op) == ARRAY_REF;
2333 /* Inserted expressions are placed onto this worklist, which is used
2334 for performing quick dead code elimination of insertions we made
2335 that didn't turn out to be necessary. */
2336 static VEC(tree,heap) *inserted_exprs;
2338 /* Pool allocated fake store expressions are placed onto this
2339 worklist, which, after performing dead code elimination, is walked
2340 to see which expressions need to be put into GC'able memory */
2341 static VEC(tree, heap) *need_creation;
2343 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2344 COMPONENT_REF or INDIRECT_REF or ARRAY_REF portion, because we'd end up with
2345 trying to rename aggregates into ssa form directly, which is a no
2348 Thus, this routine doesn't create temporaries, it just builds a
2349 single access expression for the array, calling
2350 find_or_generate_expression to build the innermost pieces.
2352 This function is a subroutine of create_expression_by_pieces, and
2353 should not be called on it's own unless you really know what you
2354 are doing.
2356 static tree
2357 create_component_ref_by_pieces (basic_block block, tree expr, tree stmts)
2359 tree genop = expr;
2360 tree folded;
2362 if (TREE_CODE (genop) == VALUE_HANDLE)
2364 tree found = bitmap_find_leader (AVAIL_OUT (block), expr);
2365 if (found)
2366 return found;
2369 if (TREE_CODE (genop) == VALUE_HANDLE)
2371 bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
2372 unsigned int firstbit = bitmap_first_set_bit (exprset->expressions);
2373 genop = expression_for_id (firstbit);
2376 switch TREE_CODE (genop)
2378 case ARRAY_REF:
2380 tree op0;
2381 tree op1, op2, op3;
2382 op0 = create_component_ref_by_pieces (block,
2383 TREE_OPERAND (genop, 0),
2384 stmts);
2385 op1 = TREE_OPERAND (genop, 1);
2386 if (TREE_CODE (op1) == VALUE_HANDLE)
2387 op1 = find_or_generate_expression (block, op1, stmts);
2388 op2 = TREE_OPERAND (genop, 2);
2389 if (op2 && TREE_CODE (op2) == VALUE_HANDLE)
2390 op2 = find_or_generate_expression (block, op2, stmts);
2391 op3 = TREE_OPERAND (genop, 3);
2392 if (op3 && TREE_CODE (op3) == VALUE_HANDLE)
2393 op3 = find_or_generate_expression (block, op3, stmts);
2394 folded = build4 (ARRAY_REF, TREE_TYPE (genop), op0, op1,
2395 op2, op3);
2396 return folded;
2398 case COMPONENT_REF:
2400 bitmap_set_t exprset;
2401 unsigned int firstbit;
2402 tree op0;
2403 tree op1;
2404 op0 = create_component_ref_by_pieces (block,
2405 TREE_OPERAND (genop, 0),
2406 stmts);
2407 exprset = VALUE_HANDLE_EXPR_SET (TREE_OPERAND (genop, 1));
2408 firstbit = bitmap_first_set_bit (exprset->expressions);
2409 op1 = expression_for_id (firstbit);
2410 folded = fold_build3 (COMPONENT_REF, TREE_TYPE (genop), op0, op1,
2411 NULL_TREE);
2412 return folded;
2414 break;
2415 case INDIRECT_REF:
2417 tree op1 = TREE_OPERAND (genop, 0);
2418 tree genop1 = find_or_generate_expression (block, op1, stmts);
2420 folded = fold_build1 (TREE_CODE (genop), TREE_TYPE (genop),
2421 genop1);
2422 return folded;
2424 break;
2425 case VAR_DECL:
2426 case PARM_DECL:
2427 case RESULT_DECL:
2428 case SSA_NAME:
2429 case STRING_CST:
2430 return genop;
2431 default:
2432 gcc_unreachable ();
2435 return NULL_TREE;
2438 /* Find a leader for an expression, or generate one using
2439 create_expression_by_pieces if it's ANTIC but
2440 complex.
2441 BLOCK is the basic_block we are looking for leaders in.
2442 EXPR is the expression to find a leader or generate for.
2443 STMTS is the statement list to put the inserted expressions on.
2444 Returns the SSA_NAME of the LHS of the generated expression or the
2445 leader. */
2447 static tree
2448 find_or_generate_expression (basic_block block, tree expr, tree stmts)
2450 tree genop = bitmap_find_leader (AVAIL_OUT (block), expr);
2452 /* If it's still NULL, it must be a complex expression, so generate
2453 it recursively. */
2454 if (genop == NULL)
2456 bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
2457 unsigned int firstbit = bitmap_first_set_bit (exprset->expressions);
2459 genop = expression_for_id (firstbit);
2460 gcc_assert (can_PRE_operation (genop));
2461 genop = create_expression_by_pieces (block, genop, stmts);
2463 return genop;
2466 #define NECESSARY(stmt) stmt->base.asm_written_flag
2467 /* Create an expression in pieces, so that we can handle very complex
2468 expressions that may be ANTIC, but not necessary GIMPLE.
2469 BLOCK is the basic block the expression will be inserted into,
2470 EXPR is the expression to insert (in value form)
2471 STMTS is a statement list to append the necessary insertions into.
2473 This function will die if we hit some value that shouldn't be
2474 ANTIC but is (IE there is no leader for it, or its components).
2475 This function may also generate expressions that are themselves
2476 partially or fully redundant. Those that are will be either made
2477 fully redundant during the next iteration of insert (for partially
2478 redundant ones), or eliminated by eliminate (for fully redundant
2479 ones). */
2481 static tree
2482 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
2484 tree temp, name;
2485 tree folded, forced_stmts, newexpr;
2486 tree v;
2487 tree_stmt_iterator tsi;
2489 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2491 case tcc_vl_exp:
2493 tree fn, sc;
2494 tree genfn;
2495 int i, nargs;
2496 tree *buffer;
2498 gcc_assert (TREE_CODE (expr) == CALL_EXPR);
2500 fn = CALL_EXPR_FN (expr);
2501 sc = CALL_EXPR_STATIC_CHAIN (expr);
2503 genfn = find_or_generate_expression (block, fn, stmts);
2505 nargs = call_expr_nargs (expr);
2506 buffer = alloca (nargs * sizeof (tree));
2508 for (i = 0; i < nargs; i++)
2510 tree arg = CALL_EXPR_ARG (expr, i);
2511 buffer[i] = find_or_generate_expression (block, arg, stmts);
2514 folded = build_call_array (TREE_TYPE (expr), genfn, nargs, buffer);
2515 if (sc)
2516 CALL_EXPR_STATIC_CHAIN (folded) =
2517 find_or_generate_expression (block, sc, stmts);
2518 folded = fold (folded);
2519 break;
2521 break;
2522 case tcc_reference:
2524 if (TREE_CODE (expr) == COMPONENT_REF
2525 || TREE_CODE (expr) == ARRAY_REF)
2527 folded = create_component_ref_by_pieces (block, expr, stmts);
2529 else
2531 tree op1 = TREE_OPERAND (expr, 0);
2532 tree genop1 = find_or_generate_expression (block, op1, stmts);
2534 folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
2535 genop1);
2537 break;
2540 case tcc_binary:
2541 case tcc_comparison:
2543 tree op1 = TREE_OPERAND (expr, 0);
2544 tree op2 = TREE_OPERAND (expr, 1);
2545 tree genop1 = find_or_generate_expression (block, op1, stmts);
2546 tree genop2 = find_or_generate_expression (block, op2, stmts);
2547 folded = fold_build2 (TREE_CODE (expr), TREE_TYPE (expr),
2548 genop1, genop2);
2549 break;
2552 case tcc_unary:
2554 tree op1 = TREE_OPERAND (expr, 0);
2555 tree genop1 = find_or_generate_expression (block, op1, stmts);
2556 folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
2557 genop1);
2558 break;
2561 default:
2562 gcc_unreachable ();
2565 /* Force the generated expression to be a sequence of GIMPLE
2566 statements.
2567 We have to call unshare_expr because force_gimple_operand may
2568 modify the tree we pass to it. */
2569 newexpr = force_gimple_operand (unshare_expr (folded), &forced_stmts,
2570 false, NULL);
2572 /* If we have any intermediate expressions to the value sets, add them
2573 to the value sets and chain them on in the instruction stream. */
2574 if (forced_stmts)
2576 tsi = tsi_start (forced_stmts);
2577 for (; !tsi_end_p (tsi); tsi_next (&tsi))
2579 tree stmt = tsi_stmt (tsi);
2580 tree forcedname = GIMPLE_STMT_OPERAND (stmt, 0);
2581 tree forcedexpr = GIMPLE_STMT_OPERAND (stmt, 1);
2582 tree val = vn_lookup_or_add (forcedexpr, NULL);
2584 VEC_safe_push (tree, heap, inserted_exprs, stmt);
2585 vn_add (forcedname, val);
2586 bitmap_value_replace_in_set (NEW_SETS (block), forcedname);
2587 bitmap_value_replace_in_set (AVAIL_OUT (block), forcedname);
2588 mark_symbols_for_renaming (stmt);
2590 tsi = tsi_last (stmts);
2591 tsi_link_after (&tsi, forced_stmts, TSI_CONTINUE_LINKING);
2594 /* Build and insert the assignment of the end result to the temporary
2595 that we will return. */
2596 if (!pretemp || TREE_TYPE (expr) != TREE_TYPE (pretemp))
2598 pretemp = create_tmp_var (TREE_TYPE (expr), "pretmp");
2599 get_var_ann (pretemp);
2602 temp = pretemp;
2603 add_referenced_var (temp);
2605 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE
2606 || TREE_CODE (TREE_TYPE (expr)) == VECTOR_TYPE)
2607 DECL_GIMPLE_REG_P (temp) = 1;
2609 newexpr = build2_gimple (GIMPLE_MODIFY_STMT, temp, newexpr);
2610 name = make_ssa_name (temp, newexpr);
2611 GIMPLE_STMT_OPERAND (newexpr, 0) = name;
2612 NECESSARY (newexpr) = 0;
2614 tsi = tsi_last (stmts);
2615 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
2616 VEC_safe_push (tree, heap, inserted_exprs, newexpr);
2618 /* All the symbols in NEWEXPR should be put into SSA form. */
2619 mark_symbols_for_renaming (newexpr);
2621 /* Add a value handle to the temporary.
2622 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2623 we are creating the expression by pieces, and this particular piece of
2624 the expression may have been represented. There is no harm in replacing
2625 here. */
2626 v = get_value_handle (expr);
2627 vn_add (name, v);
2628 get_or_alloc_expression_id (name);
2629 bitmap_value_replace_in_set (NEW_SETS (block), name);
2630 bitmap_value_replace_in_set (AVAIL_OUT (block), name);
2632 pre_stats.insertions++;
2633 if (dump_file && (dump_flags & TDF_DETAILS))
2635 fprintf (dump_file, "Inserted ");
2636 print_generic_expr (dump_file, newexpr, 0);
2637 fprintf (dump_file, " in predecessor %d\n", block->index);
2640 return name;
2643 /* Insert the to-be-made-available values of expression EXPRNUM for each
2644 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2645 merge the result with a phi node, given the same value handle as
2646 NODE. Return true if we have inserted new stuff. */
2648 static bool
2649 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
2650 tree *avail)
2652 tree expr = expression_for_id (exprnum);
2653 tree val = get_value_handle (expr);
2654 edge pred;
2655 bool insertions = false;
2656 bool nophi = false;
2657 basic_block bprime;
2658 tree eprime;
2659 edge_iterator ei;
2660 tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
2661 tree temp;
2663 if (dump_file && (dump_flags & TDF_DETAILS))
2665 fprintf (dump_file, "Found partial redundancy for expression ");
2666 print_generic_expr (dump_file, expr, 0);
2667 fprintf (dump_file, " (");
2668 print_generic_expr (dump_file, val, 0);
2669 fprintf (dump_file, ")");
2670 fprintf (dump_file, "\n");
2673 /* Make sure we aren't creating an induction variable. */
2674 if (block->loop_depth > 0 && EDGE_COUNT (block->preds) == 2
2675 && TREE_CODE_CLASS (TREE_CODE (expr)) != tcc_reference )
2677 bool firstinsideloop = false;
2678 bool secondinsideloop = false;
2679 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
2680 EDGE_PRED (block, 0)->src);
2681 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
2682 EDGE_PRED (block, 1)->src);
2683 /* Induction variables only have one edge inside the loop. */
2684 if (firstinsideloop ^ secondinsideloop)
2686 if (dump_file && (dump_flags & TDF_DETAILS))
2687 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
2688 nophi = true;
2693 /* Make the necessary insertions. */
2694 FOR_EACH_EDGE (pred, ei, block->preds)
2696 tree stmts = alloc_stmt_list ();
2697 tree builtexpr;
2698 bprime = pred->src;
2699 eprime = avail[bprime->index];
2701 if (can_PRE_operation (eprime))
2703 #ifdef ENABLE_CHECKING
2704 tree vh;
2706 /* eprime may be an invariant. */
2707 vh = TREE_CODE (eprime) == VALUE_HANDLE
2708 ? eprime
2709 : get_value_handle (eprime);
2711 /* ensure that the virtual uses we need reach our block. */
2712 if (TREE_CODE (vh) == VALUE_HANDLE)
2714 int i;
2715 tree vuse;
2716 for (i = 0;
2717 VEC_iterate (tree, VALUE_HANDLE_VUSES (vh), i, vuse);
2718 i++)
2720 size_t id = SSA_NAME_VERSION (vuse);
2721 gcc_assert (bitmap_bit_p (RVUSE_OUT (bprime), id)
2722 || IS_EMPTY_STMT (SSA_NAME_DEF_STMT (vuse)));
2725 #endif
2726 builtexpr = create_expression_by_pieces (bprime,
2727 eprime,
2728 stmts);
2729 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
2730 bsi_insert_on_edge (pred, stmts);
2731 avail[bprime->index] = builtexpr;
2732 insertions = true;
2735 /* If we didn't want a phi node, and we made insertions, we still have
2736 inserted new stuff, and thus return true. If we didn't want a phi node,
2737 and didn't make insertions, we haven't added anything new, so return
2738 false. */
2739 if (nophi && insertions)
2740 return true;
2741 else if (nophi && !insertions)
2742 return false;
2744 /* Now build a phi for the new variable. */
2745 if (!prephitemp || TREE_TYPE (prephitemp) != type)
2747 prephitemp = create_tmp_var (type, "prephitmp");
2748 get_var_ann (prephitemp);
2751 temp = prephitemp;
2752 add_referenced_var (temp);
2755 if (TREE_CODE (type) == COMPLEX_TYPE
2756 || TREE_CODE (type) == VECTOR_TYPE)
2757 DECL_GIMPLE_REG_P (temp) = 1;
2758 temp = create_phi_node (temp, block);
2760 NECESSARY (temp) = 0;
2761 VEC_safe_push (tree, heap, inserted_exprs, temp);
2762 FOR_EACH_EDGE (pred, ei, block->preds)
2763 add_phi_arg (temp, avail[pred->src->index], pred);
2765 vn_add (PHI_RESULT (temp), val);
2767 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
2768 this insertion, since we test for the existence of this value in PHI_GEN
2769 before proceeding with the partial redundancy checks in insert_aux.
2771 The value may exist in AVAIL_OUT, in particular, it could be represented
2772 by the expression we are trying to eliminate, in which case we want the
2773 replacement to occur. If it's not existing in AVAIL_OUT, we want it
2774 inserted there.
2776 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
2777 this block, because if it did, it would have existed in our dominator's
2778 AVAIL_OUT, and would have been skipped due to the full redundancy check.
2781 bitmap_insert_into_set (PHI_GEN (block),
2782 PHI_RESULT (temp));
2783 bitmap_value_replace_in_set (AVAIL_OUT (block),
2784 PHI_RESULT (temp));
2785 bitmap_insert_into_set (NEW_SETS (block),
2786 PHI_RESULT (temp));
2788 if (dump_file && (dump_flags & TDF_DETAILS))
2790 fprintf (dump_file, "Created phi ");
2791 print_generic_expr (dump_file, temp, 0);
2792 fprintf (dump_file, " in block %d\n", block->index);
2794 pre_stats.phis++;
2795 return true;
2800 /* Perform insertion of partially redundant values.
2801 For BLOCK, do the following:
2802 1. Propagate the NEW_SETS of the dominator into the current block.
2803 If the block has multiple predecessors,
2804 2a. Iterate over the ANTIC expressions for the block to see if
2805 any of them are partially redundant.
2806 2b. If so, insert them into the necessary predecessors to make
2807 the expression fully redundant.
2808 2c. Insert a new PHI merging the values of the predecessors.
2809 2d. Insert the new PHI, and the new expressions, into the
2810 NEW_SETS set.
2811 3. Recursively call ourselves on the dominator children of BLOCK.
2813 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
2814 do_regular_insertion and do_partial_insertion.
2818 static bool
2819 do_regular_insertion (basic_block block, basic_block dom)
2821 bool new_stuff = false;
2822 VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
2823 tree expr;
2824 int i;
2826 for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
2828 if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
2830 tree *avail;
2831 tree val;
2832 bool by_some = false;
2833 bool cant_insert = false;
2834 bool all_same = true;
2835 tree first_s = NULL;
2836 edge pred;
2837 basic_block bprime;
2838 tree eprime = NULL_TREE;
2839 edge_iterator ei;
2841 val = get_value_handle (expr);
2842 if (bitmap_set_contains_value (PHI_GEN (block), val))
2843 continue;
2844 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
2846 if (dump_file && (dump_flags & TDF_DETAILS))
2847 fprintf (dump_file, "Found fully redundant value\n");
2848 continue;
2851 avail = XCNEWVEC (tree, last_basic_block);
2852 FOR_EACH_EDGE (pred, ei, block->preds)
2854 tree vprime;
2855 tree edoubleprime;
2857 /* This can happen in the very weird case
2858 that our fake infinite loop edges have caused a
2859 critical edge to appear. */
2860 if (EDGE_CRITICAL_P (pred))
2862 cant_insert = true;
2863 break;
2865 bprime = pred->src;
2866 eprime = phi_translate (expr, ANTIC_IN (block), NULL,
2867 bprime, block);
2869 /* eprime will generally only be NULL if the
2870 value of the expression, translated
2871 through the PHI for this predecessor, is
2872 undefined. If that is the case, we can't
2873 make the expression fully redundant,
2874 because its value is undefined along a
2875 predecessor path. We can thus break out
2876 early because it doesn't matter what the
2877 rest of the results are. */
2878 if (eprime == NULL)
2880 cant_insert = true;
2881 break;
2884 eprime = fully_constant_expression (eprime);
2885 vprime = get_value_handle (eprime);
2886 gcc_assert (vprime);
2887 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
2888 vprime);
2889 if (edoubleprime == NULL)
2891 avail[bprime->index] = eprime;
2892 all_same = false;
2894 else
2896 avail[bprime->index] = edoubleprime;
2897 by_some = true;
2898 if (first_s == NULL)
2899 first_s = edoubleprime;
2900 else if (!operand_equal_p (first_s, edoubleprime,
2902 all_same = false;
2905 /* If we can insert it, it's not the same value
2906 already existing along every predecessor, and
2907 it's defined by some predecessor, it is
2908 partially redundant. */
2909 if (!cant_insert && !all_same && by_some)
2911 if (insert_into_preds_of_block (block, get_expression_id (expr),
2912 avail))
2913 new_stuff = true;
2915 /* If all edges produce the same value and that value is
2916 an invariant, then the PHI has the same value on all
2917 edges. Note this. */
2918 else if (!cant_insert && all_same && eprime
2919 && is_gimple_min_invariant (eprime)
2920 && !is_gimple_min_invariant (val))
2922 unsigned int j;
2923 bitmap_iterator bi;
2925 bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
2926 FOR_EACH_EXPR_ID_IN_SET (exprset, j, bi)
2928 tree expr = expression_for_id (j);
2929 if (TREE_CODE (expr) == SSA_NAME)
2931 vn_add (expr, eprime);
2932 pre_stats.constified++;
2936 free (avail);
2940 VEC_free (tree, heap, exprs);
2941 return new_stuff;
2945 /* Perform insertion for partially anticipatable expressions. There
2946 is only one case we will perform insertion for these. This case is
2947 if the expression is partially anticipatable, and fully available.
2948 In this case, we know that putting it earlier will enable us to
2949 remove the later computation. */
2952 static bool
2953 do_partial_partial_insertion (basic_block block, basic_block dom)
2955 bool new_stuff = false;
2956 VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (PA_IN (block));
2957 tree expr;
2958 int i;
2960 for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
2962 if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
2964 tree *avail;
2965 tree val;
2966 bool by_all = true;
2967 bool cant_insert = false;
2968 edge pred;
2969 basic_block bprime;
2970 tree eprime = NULL_TREE;
2971 edge_iterator ei;
2973 val = get_value_handle (expr);
2974 if (bitmap_set_contains_value (PHI_GEN (block), val))
2975 continue;
2976 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
2977 continue;
2979 avail = XCNEWVEC (tree, last_basic_block);
2980 FOR_EACH_EDGE (pred, ei, block->preds)
2982 tree vprime;
2983 tree edoubleprime;
2985 /* This can happen in the very weird case
2986 that our fake infinite loop edges have caused a
2987 critical edge to appear. */
2988 if (EDGE_CRITICAL_P (pred))
2990 cant_insert = true;
2991 break;
2993 bprime = pred->src;
2994 eprime = phi_translate (expr, ANTIC_IN (block),
2995 PA_IN (block),
2996 bprime, block);
2998 /* eprime will generally only be NULL if the
2999 value of the expression, translated
3000 through the PHI for this predecessor, is
3001 undefined. If that is the case, we can't
3002 make the expression fully redundant,
3003 because its value is undefined along a
3004 predecessor path. We can thus break out
3005 early because it doesn't matter what the
3006 rest of the results are. */
3007 if (eprime == NULL)
3009 cant_insert = true;
3010 break;
3013 eprime = fully_constant_expression (eprime);
3014 vprime = get_value_handle (eprime);
3015 gcc_assert (vprime);
3016 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3017 vprime);
3018 if (edoubleprime == NULL)
3020 by_all = false;
3021 break;
3023 else
3024 avail[bprime->index] = edoubleprime;
3028 /* If we can insert it, it's not the same value
3029 already existing along every predecessor, and
3030 it's defined by some predecessor, it is
3031 partially redundant. */
3032 if (!cant_insert && by_all)
3034 pre_stats.pa_insert++;
3035 if (insert_into_preds_of_block (block, get_expression_id (expr),
3036 avail))
3037 new_stuff = true;
3039 free (avail);
3043 VEC_free (tree, heap, exprs);
3044 return new_stuff;
3047 static bool
3048 insert_aux (basic_block block)
3050 basic_block son;
3051 bool new_stuff = false;
3053 if (block)
3055 basic_block dom;
3056 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3057 if (dom)
3059 unsigned i;
3060 bitmap_iterator bi;
3061 bitmap_set_t newset = NEW_SETS (dom);
3062 if (newset)
3064 /* Note that we need to value_replace both NEW_SETS, and
3065 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3066 represented by some non-simple expression here that we want
3067 to replace it with. */
3068 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3070 tree expr = expression_for_id (i);
3071 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3072 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3075 if (!single_pred_p (block))
3077 new_stuff |= do_regular_insertion (block, dom);
3078 if (do_partial_partial)
3079 new_stuff |= do_partial_partial_insertion (block, dom);
3083 for (son = first_dom_son (CDI_DOMINATORS, block);
3084 son;
3085 son = next_dom_son (CDI_DOMINATORS, son))
3087 new_stuff |= insert_aux (son);
3090 return new_stuff;
3093 /* Perform insertion of partially redundant values. */
3095 static void
3096 insert (void)
3098 bool new_stuff = true;
3099 basic_block bb;
3100 int num_iterations = 0;
3102 FOR_ALL_BB (bb)
3103 NEW_SETS (bb) = bitmap_set_new ();
3105 while (new_stuff)
3107 num_iterations++;
3108 new_stuff = false;
3109 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
3111 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
3112 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
3116 /* Return true if VAR is an SSA variable with no defining statement in
3117 this procedure, *AND* isn't a live-on-entry parameter. */
3119 static bool
3120 is_undefined_value (tree expr)
3122 return (TREE_CODE (expr) == SSA_NAME
3123 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
3124 /* PARM_DECLs and hard registers are always defined. */
3125 && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL);
3129 /* Given an SSA variable VAR and an expression EXPR, compute the value
3130 number for EXPR and create a value handle (VAL) for it. If VAR and
3131 EXPR are not the same, associate VAL with VAR. Finally, add VAR to
3132 S1 and its value handle to S2, and to the maximal set if
3133 ADD_TO_MAXIMAL is true.
3135 VUSES represent the virtual use operands associated with EXPR (if
3136 any). */
3138 static inline void
3139 add_to_sets (tree var, tree expr, tree stmt, bitmap_set_t s1,
3140 bitmap_set_t s2)
3142 tree val = vn_lookup_or_add (expr, stmt);
3144 /* VAR and EXPR may be the same when processing statements for which
3145 we are not computing value numbers (e.g., non-assignments, or
3146 statements that make aliased stores). In those cases, we are
3147 only interested in making VAR available as its own value. */
3148 if (var != expr)
3149 vn_add (var, val);
3151 if (s1)
3152 bitmap_insert_into_set (s1, var);
3154 /* PHI nodes can't go in the maximal sets because they are not in
3155 TMP_GEN, so it is possible to get into non-monotonic situations
3156 during ANTIC calculation, because it will *add* bits. */
3157 if (!in_fre && TREE_CODE (SSA_NAME_DEF_STMT (var)) != PHI_NODE)
3158 bitmap_value_insert_into_set (maximal_set, var);
3159 bitmap_value_insert_into_set (s2, var);
3162 /* Find existing value expression that is the same as T,
3163 and return it if it exists. */
3165 static inline tree
3166 find_existing_value_expr (tree t, tree stmt)
3168 bitmap_iterator bi;
3169 unsigned int bii;
3170 tree vh;
3171 bitmap_set_t exprset;
3173 if (REFERENCE_CLASS_P (t))
3174 vh = vn_lookup (t, stmt);
3175 else
3176 vh = vn_lookup (t, NULL);
3178 if (!vh)
3179 return NULL;
3180 exprset = VALUE_HANDLE_EXPR_SET (vh);
3181 FOR_EACH_EXPR_ID_IN_SET (exprset, bii, bi)
3183 tree efi = expression_for_id (bii);
3184 if (expressions_equal_p (t, efi))
3185 return efi;
3187 return NULL;
3190 /* Given a unary or binary expression EXPR, create and return a new
3191 expression with the same structure as EXPR but with its operands
3192 replaced with the value handles of each of the operands of EXPR.
3194 VUSES represent the virtual use operands associated with EXPR (if
3195 any). Insert EXPR's operands into the EXP_GEN set for BLOCK. */
3197 static inline tree
3198 create_value_expr_from (tree expr, basic_block block, tree stmt)
3200 int i;
3201 enum tree_code code = TREE_CODE (expr);
3202 tree vexpr;
3203 alloc_pool pool;
3204 tree efi;
3206 gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
3207 || TREE_CODE_CLASS (code) == tcc_binary
3208 || TREE_CODE_CLASS (code) == tcc_comparison
3209 || TREE_CODE_CLASS (code) == tcc_reference
3210 || TREE_CODE_CLASS (code) == tcc_expression
3211 || TREE_CODE_CLASS (code) == tcc_vl_exp
3212 || TREE_CODE_CLASS (code) == tcc_exceptional
3213 || TREE_CODE_CLASS (code) == tcc_declaration);
3215 if (TREE_CODE_CLASS (code) == tcc_unary)
3216 pool = unary_node_pool;
3217 else if (TREE_CODE_CLASS (code) == tcc_reference)
3218 pool = reference_node_pool;
3219 else if (TREE_CODE_CLASS (code) == tcc_binary)
3220 pool = binary_node_pool;
3221 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3222 pool = comparison_node_pool;
3223 else
3224 gcc_assert (code == CALL_EXPR);
3226 if (code == CALL_EXPR)
3227 vexpr = temp_copy_call_expr (expr);
3228 else
3230 vexpr = (tree) pool_alloc (pool);
3231 memcpy (vexpr, expr, tree_size (expr));
3234 for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
3236 tree val, op;
3238 op = TREE_OPERAND (expr, i);
3239 if (op == NULL_TREE)
3240 continue;
3242 /* Recursively value-numberize reference ops and tree lists. */
3243 if (REFERENCE_CLASS_P (op))
3245 tree tempop = create_value_expr_from (op, block, stmt);
3246 op = tempop ? tempop : op;
3247 val = vn_lookup_or_add (op, stmt);
3249 else
3250 /* Create a value handle for OP and add it to VEXPR. */
3251 val = vn_lookup_or_add (op, NULL);
3253 if (!is_undefined_value (op) && TREE_CODE (op) != TREE_LIST)
3254 bitmap_value_insert_into_set (EXP_GEN (block), op);
3256 if (TREE_CODE (val) == VALUE_HANDLE)
3257 TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
3259 TREE_OPERAND (vexpr, i) = val;
3261 efi = find_existing_value_expr (vexpr, stmt);
3262 if (efi)
3263 return efi;
3264 get_or_alloc_expression_id (vexpr);
3265 return vexpr;
3268 /* Given a statement STMT and its right hand side which is a load, try
3269 to look for the expression stored in the location for the load, and
3270 return true if a useful equivalence was recorded for LHS. */
3272 static bool
3273 try_look_through_load (tree lhs, tree mem_ref, tree stmt, basic_block block)
3275 tree store_stmt = NULL;
3276 tree rhs;
3277 ssa_op_iter i;
3278 tree vuse;
3280 FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, i, SSA_OP_VIRTUAL_USES)
3282 tree def_stmt;
3284 gcc_assert (TREE_CODE (vuse) == SSA_NAME);
3285 def_stmt = SSA_NAME_DEF_STMT (vuse);
3287 /* If there is no useful statement for this VUSE, we'll not find a
3288 useful expression to return either. Likewise, if there is a
3289 statement but it is not a simple assignment or it has virtual
3290 uses, we can stop right here. Note that this means we do
3291 not look through PHI nodes, which is intentional. */
3292 if (!def_stmt
3293 || TREE_CODE (def_stmt) != GIMPLE_MODIFY_STMT
3294 || !ZERO_SSA_OPERANDS (def_stmt, SSA_OP_VIRTUAL_USES))
3295 return false;
3297 /* If this is not the same statement as one we have looked at for
3298 another VUSE of STMT already, we have two statements producing
3299 something that reaches our STMT. */
3300 if (store_stmt && store_stmt != def_stmt)
3301 return false;
3302 else
3304 /* Is this a store to the exact same location as the one we are
3305 loading from in STMT? */
3306 if (!operand_equal_p (GIMPLE_STMT_OPERAND (def_stmt, 0), mem_ref, 0))
3307 return false;
3309 /* Otherwise remember this statement and see if all other VUSEs
3310 come from the same statement. */
3311 store_stmt = def_stmt;
3315 /* Alright then, we have visited all VUSEs of STMT and we've determined
3316 that all of them come from the same statement STORE_STMT. See if there
3317 is a useful expression we can deduce from STORE_STMT. */
3318 rhs = GIMPLE_STMT_OPERAND (store_stmt, 1);
3319 if ((TREE_CODE (rhs) == SSA_NAME
3320 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
3321 || is_gimple_min_invariant (rhs)
3322 || TREE_CODE (rhs) == ADDR_EXPR
3323 || TREE_INVARIANT (rhs))
3326 /* Yay! Compute a value number for the RHS of the statement and
3327 add its value to the AVAIL_OUT set for the block. Add the LHS
3328 to TMP_GEN. */
3329 add_to_sets (lhs, rhs, store_stmt, TMP_GEN (block), AVAIL_OUT (block));
3330 if (TREE_CODE (rhs) == SSA_NAME
3331 && !is_undefined_value (rhs))
3332 bitmap_value_insert_into_set (EXP_GEN (block), rhs);
3333 return true;
3336 return false;
3339 /* Return a copy of NODE that is stored in the temporary alloc_pool's.
3340 This is made recursively true, so that the operands are stored in
3341 the pool as well. */
3343 static tree
3344 poolify_tree (tree node)
3346 switch (TREE_CODE (node))
3348 case INDIRECT_REF:
3350 tree temp = (tree) pool_alloc (reference_node_pool);
3351 memcpy (temp, node, tree_size (node));
3352 TREE_OPERAND (temp, 0) = poolify_tree (TREE_OPERAND (temp, 0));
3353 return temp;
3355 break;
3356 case GIMPLE_MODIFY_STMT:
3358 tree temp = (tree) pool_alloc (modify_expr_node_pool);
3359 memcpy (temp, node, tree_size (node));
3360 GIMPLE_STMT_OPERAND (temp, 0) =
3361 poolify_tree (GIMPLE_STMT_OPERAND (temp, 0));
3362 GIMPLE_STMT_OPERAND (temp, 1) =
3363 poolify_tree (GIMPLE_STMT_OPERAND (temp, 1));
3364 return temp;
3366 break;
3367 case SSA_NAME:
3368 case INTEGER_CST:
3369 case STRING_CST:
3370 case REAL_CST:
3371 case PARM_DECL:
3372 case VAR_DECL:
3373 case RESULT_DECL:
3374 return node;
3375 default:
3376 gcc_unreachable ();
3380 static tree modify_expr_template;
3382 /* Allocate a GIMPLE_MODIFY_STMT with TYPE, and operands OP1, OP2 in the
3383 alloc pools and return it. */
3384 static tree
3385 poolify_modify_stmt (tree op1, tree op2)
3387 if (modify_expr_template == NULL)
3388 modify_expr_template = build2_gimple (GIMPLE_MODIFY_STMT, op1, op2);
3390 GIMPLE_STMT_OPERAND (modify_expr_template, 0) = op1;
3391 GIMPLE_STMT_OPERAND (modify_expr_template, 1) = op2;
3393 return poolify_tree (modify_expr_template);
3397 /* For each real store operation of the form
3398 *a = <value> that we see, create a corresponding fake store of the
3399 form storetmp_<version> = *a.
3401 This enables AVAIL computation to mark the results of stores as
3402 available. Without this, you'd need to do some computation to
3403 mark the result of stores as ANTIC and AVAIL at all the right
3404 points.
3405 To save memory, we keep the store
3406 statements pool allocated until we decide whether they are
3407 necessary or not. */
3409 static void
3410 insert_fake_stores (void)
3412 basic_block block;
3414 FOR_ALL_BB (block)
3416 block_stmt_iterator bsi;
3417 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
3419 tree stmt = bsi_stmt (bsi);
3421 /* We can't generate SSA names for stores that are complex
3422 or aggregate. We also want to ignore things whose
3423 virtual uses occur in abnormal phis. */
3425 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3426 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == INDIRECT_REF
3427 && !AGGREGATE_TYPE_P (TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 0)))
3428 && TREE_CODE (TREE_TYPE (GIMPLE_STMT_OPERAND
3429 (stmt, 0))) != COMPLEX_TYPE)
3431 ssa_op_iter iter;
3432 def_operand_p defp;
3433 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3434 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3435 tree new;
3436 bool notokay = false;
3438 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_VIRTUAL_DEFS)
3440 tree defvar = DEF_FROM_PTR (defp);
3441 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (defvar))
3443 notokay = true;
3444 break;
3448 if (notokay)
3449 continue;
3451 if (!storetemp || TREE_TYPE (rhs) != TREE_TYPE (storetemp))
3453 storetemp = create_tmp_var (TREE_TYPE (rhs), "storetmp");
3454 if (TREE_CODE (TREE_TYPE (storetemp)) == VECTOR_TYPE)
3455 DECL_GIMPLE_REG_P (storetemp) = 1;
3456 get_var_ann (storetemp);
3459 new = poolify_modify_stmt (storetemp, lhs);
3461 lhs = make_ssa_name (storetemp, new);
3462 GIMPLE_STMT_OPERAND (new, 0) = lhs;
3463 create_ssa_artificial_load_stmt (new, stmt);
3465 NECESSARY (new) = 0;
3466 VEC_safe_push (tree, heap, inserted_exprs, new);
3467 VEC_safe_push (tree, heap, need_creation, new);
3468 bsi_insert_after (&bsi, new, BSI_NEW_STMT);
3474 /* Turn the pool allocated fake stores that we created back into real
3475 GC allocated ones if they turned out to be necessary to PRE some
3476 expressions. */
3478 static void
3479 realify_fake_stores (void)
3481 unsigned int i;
3482 tree stmt;
3484 for (i = 0; VEC_iterate (tree, need_creation, i, stmt); i++)
3486 if (NECESSARY (stmt))
3488 block_stmt_iterator bsi;
3489 tree newstmt;
3491 /* Mark the temp variable as referenced */
3492 add_referenced_var (SSA_NAME_VAR (GIMPLE_STMT_OPERAND (stmt, 0)));
3494 /* Put the new statement in GC memory, fix up the
3495 SSA_NAME_DEF_STMT on it, and then put it in place of
3496 the old statement before the store in the IR stream
3497 as a plain ssa name copy. */
3498 bsi = bsi_for_stmt (stmt);
3499 bsi_prev (&bsi);
3500 newstmt = build2_gimple (GIMPLE_MODIFY_STMT,
3501 GIMPLE_STMT_OPERAND (stmt, 0),
3502 GIMPLE_STMT_OPERAND (bsi_stmt (bsi), 1));
3503 SSA_NAME_DEF_STMT (GIMPLE_STMT_OPERAND (newstmt, 0)) = newstmt;
3504 bsi_insert_before (&bsi, newstmt, BSI_SAME_STMT);
3505 bsi = bsi_for_stmt (stmt);
3506 bsi_remove (&bsi, true);
3508 else
3509 release_defs (stmt);
3513 /* Tree-combine a value number expression *EXPR_P that does a type
3514 conversion with the value number expression of its operand.
3515 Returns true, if *EXPR_P simplifies to a value number or
3516 gimple min-invariant expression different from EXPR_P and
3517 sets *EXPR_P to the simplified expression value number.
3518 Otherwise returns false and does not change *EXPR_P. */
3520 static bool
3521 try_combine_conversion (tree *expr_p)
3523 tree expr = *expr_p;
3524 tree t;
3525 bitmap_set_t exprset;
3526 unsigned int firstbit;
3528 if (!((TREE_CODE (expr) == NOP_EXPR
3529 || TREE_CODE (expr) == CONVERT_EXPR
3530 || TREE_CODE (expr) == REALPART_EXPR
3531 || TREE_CODE (expr) == IMAGPART_EXPR)
3532 && TREE_CODE (TREE_OPERAND (expr, 0)) == VALUE_HANDLE
3533 && !VALUE_HANDLE_VUSES (TREE_OPERAND (expr, 0))))
3534 return false;
3536 exprset = VALUE_HANDLE_EXPR_SET (TREE_OPERAND (expr, 0));
3537 firstbit = bitmap_first_set_bit (exprset->expressions);
3538 t = fold_unary (TREE_CODE (expr), TREE_TYPE (expr),
3539 expression_for_id (firstbit));
3540 if (!t)
3541 return false;
3543 /* Strip useless type conversions, which is safe in the optimizers but
3544 not generally in fold. */
3545 STRIP_USELESS_TYPE_CONVERSION (t);
3547 /* Disallow value expressions we have no value number for already, as
3548 we would miss a leader for it here. */
3549 if (!(TREE_CODE (t) == VALUE_HANDLE
3550 || is_gimple_min_invariant (t)))
3551 t = vn_lookup (t, NULL);
3553 if (t && t != expr)
3555 *expr_p = t;
3556 return true;
3558 return false;
3561 /* Compute the AVAIL set for all basic blocks.
3563 This function performs value numbering of the statements in each basic
3564 block. The AVAIL sets are built from information we glean while doing
3565 this value numbering, since the AVAIL sets contain only one entry per
3566 value.
3568 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3569 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3571 static void
3572 compute_avail (void)
3574 basic_block block, son;
3575 basic_block *worklist;
3576 size_t sp = 0;
3577 tree param;
3578 /* For arguments with default definitions, we pretend they are
3579 defined in the entry block. */
3580 for (param = DECL_ARGUMENTS (current_function_decl);
3581 param;
3582 param = TREE_CHAIN (param))
3584 if (gimple_default_def (cfun, param) != NULL)
3586 tree def = gimple_default_def (cfun, param);
3588 vn_lookup_or_add (def, NULL);
3589 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
3590 if (!in_fre)
3591 bitmap_value_insert_into_set (maximal_set, def);
3592 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
3596 /* Likewise for the static chain decl. */
3597 if (cfun->static_chain_decl)
3599 param = cfun->static_chain_decl;
3600 if (gimple_default_def (cfun, param) != NULL)
3602 tree def = gimple_default_def (cfun, param);
3604 vn_lookup_or_add (def, NULL);
3605 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
3606 if (!in_fre)
3607 bitmap_value_insert_into_set (maximal_set, def);
3608 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
3612 /* Allocate the worklist. */
3613 worklist = XNEWVEC (basic_block, n_basic_blocks);
3615 /* Seed the algorithm by putting the dominator children of the entry
3616 block on the worklist. */
3617 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR);
3618 son;
3619 son = next_dom_son (CDI_DOMINATORS, son))
3620 worklist[sp++] = son;
3622 /* Loop until the worklist is empty. */
3623 while (sp)
3625 block_stmt_iterator bsi;
3626 tree stmt, phi;
3627 basic_block dom;
3628 unsigned int stmt_uid = 1;
3630 /* Pick a block from the worklist. */
3631 block = worklist[--sp];
3633 /* Initially, the set of available values in BLOCK is that of
3634 its immediate dominator. */
3635 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3636 if (dom)
3637 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3639 /* Generate values for PHI nodes. */
3640 for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
3642 /* We have no need for virtual phis, as they don't represent
3643 actual computations. */
3644 if (is_gimple_reg (PHI_RESULT (phi)))
3646 add_to_sets (PHI_RESULT (phi), PHI_RESULT (phi), NULL,
3647 PHI_GEN (block), AVAIL_OUT (block));
3651 /* Now compute value numbers and populate value sets with all
3652 the expressions computed in BLOCK. */
3653 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
3655 stmt_ann_t ann;
3656 ssa_op_iter iter;
3657 tree op;
3659 stmt = bsi_stmt (bsi);
3660 ann = stmt_ann (stmt);
3662 ann->uid = stmt_uid++;
3664 /* For regular value numbering, we are only interested in
3665 assignments of the form X_i = EXPR, where EXPR represents
3666 an "interesting" computation, it has no volatile operands
3667 and X_i doesn't flow through an abnormal edge. */
3668 if (TREE_CODE (stmt) == RETURN_EXPR
3669 && !ann->has_volatile_ops)
3671 tree realstmt = stmt;
3672 tree lhs;
3673 tree rhs;
3675 stmt = TREE_OPERAND (stmt, 0);
3676 if (stmt && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
3678 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3679 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3680 if (TREE_CODE (rhs) == SSA_NAME
3681 && !is_undefined_value (rhs))
3682 bitmap_value_insert_into_set (EXP_GEN (block), rhs);
3684 FOR_EACH_SSA_TREE_OPERAND (op, realstmt, iter, SSA_OP_DEF)
3685 add_to_sets (op, op, NULL, TMP_GEN (block),
3686 AVAIL_OUT (block));
3688 continue;
3691 else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3692 && !ann->has_volatile_ops
3693 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
3694 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI
3695 (GIMPLE_STMT_OPERAND (stmt, 0)))
3697 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3698 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3700 /* Try to look through loads. */
3701 if (TREE_CODE (lhs) == SSA_NAME
3702 && !ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_USES)
3703 && try_look_through_load (lhs, rhs, stmt, block))
3704 continue;
3706 STRIP_USELESS_TYPE_CONVERSION (rhs);
3707 if (can_value_number_operation (rhs))
3709 /* For value numberable operation, create a
3710 duplicate expression with the operands replaced
3711 with the value handles of the original RHS. */
3712 tree newt = create_value_expr_from (rhs, block, stmt);
3713 if (newt)
3715 /* If we can combine a conversion expression
3716 with the expression for its operand just
3717 record the value number for it. */
3718 if (try_combine_conversion (&newt))
3719 vn_add (lhs, newt);
3720 else
3722 tree val = vn_lookup_or_add (newt, stmt);
3723 vn_add (lhs, val);
3724 if (!in_fre)
3725 bitmap_value_insert_into_set (maximal_set, newt);
3726 bitmap_value_insert_into_set (EXP_GEN (block), newt);
3728 bitmap_insert_into_set (TMP_GEN (block), lhs);
3729 bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
3730 continue;
3733 else if ((TREE_CODE (rhs) == SSA_NAME
3734 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
3735 || is_gimple_min_invariant (rhs)
3736 || TREE_CODE (rhs) == ADDR_EXPR
3737 || TREE_INVARIANT (rhs)
3738 || DECL_P (rhs))
3740 /* Compute a value number for the RHS of the statement
3741 and add its value to the AVAIL_OUT set for the block.
3742 Add the LHS to TMP_GEN. */
3743 add_to_sets (lhs, rhs, stmt, TMP_GEN (block),
3744 AVAIL_OUT (block));
3746 if (TREE_CODE (rhs) == SSA_NAME
3747 && !is_undefined_value (rhs))
3748 bitmap_value_insert_into_set (EXP_GEN (block), rhs);
3749 continue;
3753 /* For any other statement that we don't recognize, simply
3754 make the names generated by the statement available in
3755 AVAIL_OUT and TMP_GEN. */
3756 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3757 add_to_sets (op, op, NULL, TMP_GEN (block), AVAIL_OUT (block));
3759 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3760 add_to_sets (op, op, NULL, NULL , AVAIL_OUT (block));
3763 /* Put the dominator children of BLOCK on the worklist of blocks
3764 to compute available sets for. */
3765 for (son = first_dom_son (CDI_DOMINATORS, block);
3766 son;
3767 son = next_dom_son (CDI_DOMINATORS, son))
3768 worklist[sp++] = son;
3771 free (worklist);
3775 /* Eliminate fully redundant computations. */
3777 static void
3778 eliminate (void)
3780 basic_block b;
3782 FOR_EACH_BB (b)
3784 block_stmt_iterator i;
3786 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
3788 tree stmt = bsi_stmt (i);
3790 /* Lookup the RHS of the expression, see if we have an
3791 available computation for it. If so, replace the RHS with
3792 the available computation. */
3793 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3794 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
3795 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) != SSA_NAME
3796 && !is_gimple_min_invariant (GIMPLE_STMT_OPERAND (stmt, 1))
3797 && !stmt_ann (stmt)->has_volatile_ops)
3799 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3800 tree *rhs_p = &GIMPLE_STMT_OPERAND (stmt, 1);
3801 tree sprime;
3803 sprime = bitmap_find_leader (AVAIL_OUT (b),
3804 vn_lookup (lhs, NULL));
3805 if (sprime
3806 && sprime != lhs
3807 && (TREE_CODE (*rhs_p) != SSA_NAME
3808 || may_propagate_copy (*rhs_p, sprime)))
3810 gcc_assert (sprime != *rhs_p);
3812 if (dump_file && (dump_flags & TDF_DETAILS))
3814 fprintf (dump_file, "Replaced ");
3815 print_generic_expr (dump_file, *rhs_p, 0);
3816 fprintf (dump_file, " with ");
3817 print_generic_expr (dump_file, sprime, 0);
3818 fprintf (dump_file, " in ");
3819 print_generic_stmt (dump_file, stmt, 0);
3822 if (TREE_CODE (sprime) == SSA_NAME)
3823 NECESSARY (SSA_NAME_DEF_STMT (sprime)) = 1;
3824 /* We need to make sure the new and old types actually match,
3825 which may require adding a simple cast, which fold_convert
3826 will do for us. */
3827 if (TREE_CODE (*rhs_p) != SSA_NAME
3828 && !tree_ssa_useless_type_conversion_1 (TREE_TYPE (*rhs_p),
3829 TREE_TYPE (sprime)))
3830 sprime = fold_convert (TREE_TYPE (*rhs_p), sprime);
3832 pre_stats.eliminations++;
3833 propagate_tree_value (rhs_p, sprime);
3834 update_stmt (stmt);
3836 /* If we removed EH side effects from the statement, clean
3837 its EH information. */
3838 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
3840 bitmap_set_bit (need_eh_cleanup,
3841 bb_for_stmt (stmt)->index);
3842 if (dump_file && (dump_flags & TDF_DETAILS))
3843 fprintf (dump_file, " Removed EH side effects.\n");
3851 /* Borrow a bit of tree-ssa-dce.c for the moment.
3852 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
3853 this may be a bit faster, and we may want critical edges kept split. */
3855 /* If OP's defining statement has not already been determined to be necessary,
3856 mark that statement necessary. Return the stmt, if it is newly
3857 necessary. */
3859 static inline tree
3860 mark_operand_necessary (tree op)
3862 tree stmt;
3864 gcc_assert (op);
3866 if (TREE_CODE (op) != SSA_NAME)
3867 return NULL;
3869 stmt = SSA_NAME_DEF_STMT (op);
3870 gcc_assert (stmt);
3872 if (NECESSARY (stmt)
3873 || IS_EMPTY_STMT (stmt))
3874 return NULL;
3876 NECESSARY (stmt) = 1;
3877 return stmt;
3880 /* Because we don't follow exactly the standard PRE algorithm, and decide not
3881 to insert PHI nodes sometimes, and because value numbering of casts isn't
3882 perfect, we sometimes end up inserting dead code. This simple DCE-like
3883 pass removes any insertions we made that weren't actually used. */
3885 static void
3886 remove_dead_inserted_code (void)
3888 VEC(tree,heap) *worklist = NULL;
3889 int i;
3890 tree t;
3892 worklist = VEC_alloc (tree, heap, VEC_length (tree, inserted_exprs));
3893 for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
3895 if (NECESSARY (t))
3896 VEC_quick_push (tree, worklist, t);
3898 while (VEC_length (tree, worklist) > 0)
3900 t = VEC_pop (tree, worklist);
3902 /* PHI nodes are somewhat special in that each PHI alternative has
3903 data and control dependencies. All the statements feeding the
3904 PHI node's arguments are always necessary. */
3905 if (TREE_CODE (t) == PHI_NODE)
3907 int k;
3909 VEC_reserve (tree, heap, worklist, PHI_NUM_ARGS (t));
3910 for (k = 0; k < PHI_NUM_ARGS (t); k++)
3912 tree arg = PHI_ARG_DEF (t, k);
3913 if (TREE_CODE (arg) == SSA_NAME)
3915 arg = mark_operand_necessary (arg);
3916 if (arg)
3917 VEC_quick_push (tree, worklist, arg);
3921 else
3923 /* Propagate through the operands. Examine all the USE, VUSE and
3924 VDEF operands in this statement. Mark all the statements
3925 which feed this statement's uses as necessary. */
3926 ssa_op_iter iter;
3927 tree use;
3929 /* The operands of VDEF expressions are also needed as they
3930 represent potential definitions that may reach this
3931 statement (VDEF operands allow us to follow def-def
3932 links). */
3934 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
3936 tree n = mark_operand_necessary (use);
3937 if (n)
3938 VEC_safe_push (tree, heap, worklist, n);
3943 for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
3945 if (!NECESSARY (t))
3947 block_stmt_iterator bsi;
3949 if (dump_file && (dump_flags & TDF_DETAILS))
3951 fprintf (dump_file, "Removing unnecessary insertion:");
3952 print_generic_stmt (dump_file, t, 0);
3955 if (TREE_CODE (t) == PHI_NODE)
3957 remove_phi_node (t, NULL, true);
3959 else
3961 bsi = bsi_for_stmt (t);
3962 bsi_remove (&bsi, true);
3963 release_defs (t);
3967 VEC_free (tree, heap, worklist);
3970 /* Initialize data structures used by PRE. */
3972 static void
3973 init_pre (bool do_fre)
3975 basic_block bb;
3977 next_expression_id = 0;
3978 expressions = NULL;
3979 in_fre = do_fre;
3981 inserted_exprs = NULL;
3982 need_creation = NULL;
3983 pretemp = NULL_TREE;
3984 storetemp = NULL_TREE;
3985 mergephitemp = NULL_TREE;
3986 prephitemp = NULL_TREE;
3988 vn_init ();
3989 if (!do_fre)
3990 loop_optimizer_init (LOOPS_NORMAL);
3992 connect_infinite_loops_to_exit ();
3993 memset (&pre_stats, 0, sizeof (pre_stats));
3996 postorder = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
3997 post_order_compute (postorder, false);
3999 FOR_ALL_BB (bb)
4000 bb->aux = xcalloc (1, sizeof (struct bb_bitmap_sets));
4002 calculate_dominance_info (CDI_POST_DOMINATORS);
4003 calculate_dominance_info (CDI_DOMINATORS);
4005 bitmap_obstack_initialize (&grand_bitmap_obstack);
4006 phi_translate_table = htab_create (5110, expr_pred_trans_hash,
4007 expr_pred_trans_eq, free);
4008 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
4009 sizeof (struct bitmap_set), 30);
4010 binary_node_pool = create_alloc_pool ("Binary tree nodes",
4011 tree_code_size (PLUS_EXPR), 30);
4012 unary_node_pool = create_alloc_pool ("Unary tree nodes",
4013 tree_code_size (NEGATE_EXPR), 30);
4014 reference_node_pool = create_alloc_pool ("Reference tree nodes",
4015 tree_code_size (ARRAY_REF), 30);
4016 comparison_node_pool = create_alloc_pool ("Comparison tree nodes",
4017 tree_code_size (EQ_EXPR), 30);
4018 modify_expr_node_pool = create_alloc_pool ("GIMPLE_MODIFY_STMT nodes",
4019 tree_code_size (GIMPLE_MODIFY_STMT),
4020 30);
4021 obstack_init (&temp_call_expr_obstack);
4022 modify_expr_template = NULL;
4024 FOR_ALL_BB (bb)
4026 EXP_GEN (bb) = bitmap_set_new ();
4027 PHI_GEN (bb) = bitmap_set_new ();
4028 TMP_GEN (bb) = bitmap_set_new ();
4029 AVAIL_OUT (bb) = bitmap_set_new ();
4031 maximal_set = in_fre ? NULL : bitmap_set_new ();
4033 need_eh_cleanup = BITMAP_ALLOC (NULL);
4037 /* Deallocate data structures used by PRE. */
4039 static void
4040 fini_pre (bool do_fre)
4042 basic_block bb;
4043 unsigned int i;
4045 free (postorder);
4046 VEC_free (tree, heap, inserted_exprs);
4047 VEC_free (tree, heap, need_creation);
4048 bitmap_obstack_release (&grand_bitmap_obstack);
4049 free_alloc_pool (bitmap_set_pool);
4050 free_alloc_pool (binary_node_pool);
4051 free_alloc_pool (reference_node_pool);
4052 free_alloc_pool (unary_node_pool);
4053 free_alloc_pool (comparison_node_pool);
4054 free_alloc_pool (modify_expr_node_pool);
4055 htab_delete (phi_translate_table);
4056 remove_fake_exit_edges ();
4058 FOR_ALL_BB (bb)
4060 free (bb->aux);
4061 bb->aux = NULL;
4064 free_dominance_info (CDI_POST_DOMINATORS);
4065 vn_delete ();
4067 if (!bitmap_empty_p (need_eh_cleanup))
4069 tree_purge_all_dead_eh_edges (need_eh_cleanup);
4070 cleanup_tree_cfg ();
4073 BITMAP_FREE (need_eh_cleanup);
4075 /* Wipe out pointers to VALUE_HANDLEs. In the not terribly distant
4076 future we will want them to be persistent though. */
4077 for (i = 0; i < num_ssa_names; i++)
4079 tree name = ssa_name (i);
4081 if (!name)
4082 continue;
4084 if (SSA_NAME_VALUE (name)
4085 && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
4086 SSA_NAME_VALUE (name) = NULL;
4088 if (!do_fre && current_loops)
4089 loop_optimizer_finalize ();
4092 /* Main entry point to the SSA-PRE pass. DO_FRE is true if the caller
4093 only wants to do full redundancy elimination. */
4095 static void
4096 execute_pre (bool do_fre)
4099 do_partial_partial = optimize > 2;
4100 init_pre (do_fre);
4102 if (!do_fre)
4103 insert_fake_stores ();
4105 /* Collect and value number expressions computed in each basic block. */
4106 compute_avail ();
4108 if (dump_file && (dump_flags & TDF_DETAILS))
4110 basic_block bb;
4112 FOR_ALL_BB (bb)
4114 print_bitmap_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
4115 print_bitmap_set (dump_file, TMP_GEN (bb), "tmp_gen",
4116 bb->index);
4117 print_bitmap_set (dump_file, AVAIL_OUT (bb), "avail_out",
4118 bb->index);
4122 /* Insert can get quite slow on an incredibly large number of basic
4123 blocks due to some quadratic behavior. Until this behavior is
4124 fixed, don't run it when he have an incredibly large number of
4125 bb's. If we aren't going to run insert, there is no point in
4126 computing ANTIC, either, even though it's plenty fast. */
4127 if (!do_fre && n_basic_blocks < 4000)
4129 vuse_names = XCNEWVEC (bitmap, num_ssa_names);
4130 compute_rvuse_and_antic_safe ();
4131 compute_antic ();
4132 insert ();
4133 free (vuse_names);
4136 /* Remove all the redundant expressions. */
4137 eliminate ();
4139 if (dump_file && (dump_flags & TDF_STATS))
4141 fprintf (dump_file, "Insertions: %d\n", pre_stats.insertions);
4142 fprintf (dump_file, "PA inserted: %d\n", pre_stats.pa_insert);
4143 fprintf (dump_file, "New PHIs: %d\n", pre_stats.phis);
4144 fprintf (dump_file, "Eliminated: %d\n", pre_stats.eliminations);
4145 fprintf (dump_file, "Constified: %d\n", pre_stats.constified);
4147 bsi_commit_edge_inserts ();
4149 clear_expression_ids ();
4150 if (!do_fre)
4152 remove_dead_inserted_code ();
4153 realify_fake_stores ();
4156 fini_pre (do_fre);
4159 /* Gate and execute functions for PRE. */
4161 static unsigned int
4162 do_pre (void)
4164 execute_pre (false);
4165 return 0;
4168 static bool
4169 gate_pre (void)
4171 return flag_tree_pre != 0;
4174 struct tree_opt_pass pass_pre =
4176 "pre", /* name */
4177 gate_pre, /* gate */
4178 do_pre, /* execute */
4179 NULL, /* sub */
4180 NULL, /* next */
4181 0, /* static_pass_number */
4182 TV_TREE_PRE, /* tv_id */
4183 PROP_no_crit_edges | PROP_cfg
4184 | PROP_ssa | PROP_alias, /* properties_required */
4185 0, /* properties_provided */
4186 0, /* properties_destroyed */
4187 0, /* todo_flags_start */
4188 TODO_update_ssa_only_virtuals | TODO_dump_func | TODO_ggc_collect
4189 | TODO_verify_ssa, /* todo_flags_finish */
4190 0 /* letter */
4194 /* Gate and execute functions for FRE. */
4196 static unsigned int
4197 execute_fre (void)
4199 execute_pre (true);
4200 return 0;
4203 static bool
4204 gate_fre (void)
4206 return flag_tree_fre != 0;
4209 struct tree_opt_pass pass_fre =
4211 "fre", /* name */
4212 gate_fre, /* gate */
4213 execute_fre, /* execute */
4214 NULL, /* sub */
4215 NULL, /* next */
4216 0, /* static_pass_number */
4217 TV_TREE_FRE, /* tv_id */
4218 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
4219 0, /* properties_provided */
4220 0, /* properties_destroyed */
4221 0, /* todo_flags_start */
4222 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
4223 0 /* letter */