2007-02-20 Manuel Lopez-Ibanez <manu@gcc.gnu.org>
[official-gcc.git] / gcc / tree-ssa-pre.c
blob546a2b1c9f47f025354012aa5ba6457510cce758
1 /* SSA-PRE for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
5 <stevenb@suse.de>
7 This file is part of GCC.
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2, or (at your option)
12 any later version.
14 GCC is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING. If not, write to
21 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
22 Boston, MA 02110-1301, USA. */
24 #include "config.h"
25 #include "system.h"
26 #include "coretypes.h"
27 #include "tm.h"
28 #include "ggc.h"
29 #include "tree.h"
30 #include "basic-block.h"
31 #include "diagnostic.h"
32 #include "tree-inline.h"
33 #include "tree-flow.h"
34 #include "tree-gimple.h"
35 #include "tree-dump.h"
36 #include "timevar.h"
37 #include "fibheap.h"
38 #include "hashtab.h"
39 #include "tree-iterator.h"
40 #include "real.h"
41 #include "alloc-pool.h"
42 #include "obstack.h"
43 #include "tree-pass.h"
44 #include "flags.h"
45 #include "bitmap.h"
46 #include "langhooks.h"
47 #include "cfgloop.h"
49 /* TODO:
51 1. Avail sets can be shared by making an avail_find_leader that
52 walks up the dominator tree and looks in those avail sets.
53 This might affect code optimality, it's unclear right now.
54 2. Strength reduction can be performed by anticipating expressions
55 we can repair later on.
56 3. We can do back-substitution or smarter value numbering to catch
57 commutative expressions split up over multiple statements.
58 4. ANTIC_SAFE_LOADS could be a lot smarter than it is now.
59 Right now, it is simply calculating loads that occur before
60 any store in a block, instead of loads that occur before
61 stores that affect them. This is relatively more expensive, and
62 it's not clear how much more it will buy us.
65 /* For ease of terminology, "expression node" in the below refers to
66 every expression node but GIMPLE_MODIFY_STMT, because GIMPLE_MODIFY_STMT's
67 represent the actual statement containing the expressions we care about,
68 and we cache the value number by putting it in the expression. */
70 /* Basic algorithm
72 First we walk the statements to generate the AVAIL sets, the
73 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
74 generation of values/expressions by a given block. We use them
75 when computing the ANTIC sets. The AVAIL sets consist of
76 SSA_NAME's that represent values, so we know what values are
77 available in what blocks. AVAIL is a forward dataflow problem. In
78 SSA, values are never killed, so we don't need a kill set, or a
79 fixpoint iteration, in order to calculate the AVAIL sets. In
80 traditional parlance, AVAIL sets tell us the downsafety of the
81 expressions/values.
83 Next, we generate the ANTIC sets. These sets represent the
84 anticipatable expressions. ANTIC is a backwards dataflow
85 problem. An expression is anticipatable in a given block if it could
86 be generated in that block. This means that if we had to perform
87 an insertion in that block, of the value of that expression, we
88 could. Calculating the ANTIC sets requires phi translation of
89 expressions, because the flow goes backwards through phis. We must
90 iterate to a fixpoint of the ANTIC sets, because we have a kill
91 set. Even in SSA form, values are not live over the entire
92 function, only from their definition point onwards. So we have to
93 remove values from the ANTIC set once we go past the definition
94 point of the leaders that make them up.
95 compute_antic/compute_antic_aux performs this computation.
97 Third, we perform insertions to make partially redundant
98 expressions fully redundant.
100 An expression is partially redundant (excluding partial
101 anticipation) if:
103 1. It is AVAIL in some, but not all, of the predecessors of a
104 given block.
105 2. It is ANTIC in all the predecessors.
107 In order to make it fully redundant, we insert the expression into
108 the predecessors where it is not available, but is ANTIC.
110 For the partial anticipation case, we only perform insertion if it
111 is partially anticipated in some block, and fully available in all
112 of the predecessors.
114 insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
115 performs these steps.
117 Fourth, we eliminate fully redundant expressions.
118 This is a simple statement walk that replaces redundant
119 calculations with the now available values. */
121 /* Representations of value numbers:
123 Value numbers are represented using the "value handle" approach.
124 This means that each SSA_NAME (and for other reasons to be
125 disclosed in a moment, expression nodes) has a value handle that
126 can be retrieved through get_value_handle. This value handle *is*
127 the value number of the SSA_NAME. You can pointer compare the
128 value handles for equivalence purposes.
130 For debugging reasons, the value handle is internally more than
131 just a number, it is a VALUE_HANDLE named "VH.x", where x is a
132 unique number for each value number in use. This allows
133 expressions with SSA_NAMES replaced by value handles to still be
134 pretty printed in a sane way. They simply print as "VH.3 *
135 VH.5", etc.
137 Expression nodes have value handles associated with them as a
138 cache. Otherwise, we'd have to look them up again in the hash
139 table This makes significant difference (factor of two or more) on
140 some test cases. They can be thrown away after the pass is
141 finished. */
143 /* Representation of expressions on value numbers:
145 In some portions of this code, you will notice we allocate "fake"
146 analogues to the expression we are value numbering, and replace the
147 operands with the values of the expression. Since we work on
148 values, and not just names, we canonicalize expressions to value
149 expressions for use in the ANTIC sets, the EXP_GEN set, etc.
151 This is theoretically unnecessary, it just saves a bunch of
152 repeated get_value_handle and find_leader calls in the remainder of
153 the code, trading off temporary memory usage for speed. The tree
154 nodes aren't actually creating more garbage, since they are
155 allocated in a special pools which are thrown away at the end of
156 this pass.
158 All of this also means that if you print the EXP_GEN or ANTIC sets,
159 you will see "VH.5 + VH.7" in the set, instead of "a_55 +
160 b_66" or something. The only thing that actually cares about
161 seeing the value leaders is phi translation, and it needs to be
162 able to find the leader for a value in an arbitrary block, so this
163 "value expression" form is perfect for it (otherwise you'd do
164 get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
167 /* Representation of sets:
169 There are currently two types of sets used, hopefully to be unified soon.
170 The AVAIL sets do not need to be sorted in any particular order,
171 and thus, are simply represented as two bitmaps, one that keeps
172 track of values present in the set, and one that keeps track of
173 expressions present in the set.
175 The other sets are represented as doubly linked lists kept in topological
176 order, with an optional supporting bitmap of values present in the
177 set. The sets represent values, and the elements can be values or
178 expressions. The elements can appear in different sets, but each
179 element can only appear once in each set.
181 Since each node in the set represents a value, we also want to be
182 able to map expression, set pairs to something that tells us
183 whether the value is present is a set. We use a per-set bitmap for
184 that. The value handles also point to a linked list of the
185 expressions they represent via a tree annotation. This is mainly
186 useful only for debugging, since we don't do identity lookups. */
189 /* Next global expression id number. */
190 static unsigned int next_expression_id;
192 /* Mapping from expression to id number we can use in bitmap sets. */
193 static VEC(tree, heap) *expressions;
195 /* Allocate an expression id for EXPR. */
197 static inline unsigned int
198 alloc_expression_id (tree expr)
200 tree_ann_common_t ann;
202 ann = get_tree_common_ann (expr);
204 /* Make sure we won't overflow. */
205 gcc_assert (next_expression_id + 1 > next_expression_id);
207 ann->aux = XNEW (unsigned int);
208 * ((unsigned int *)ann->aux) = next_expression_id++;
209 VEC_safe_push (tree, heap, expressions, expr);
210 return next_expression_id - 1;
213 /* Return the expression id for tree EXPR. */
215 static inline unsigned int
216 get_expression_id (tree expr)
218 tree_ann_common_t ann = tree_common_ann (expr);
219 gcc_assert (ann);
220 gcc_assert (ann->aux);
222 return *((unsigned int *)ann->aux);
225 /* Return the existing expression id for EXPR, or create one if one
226 does not exist yet. */
228 static inline unsigned int
229 get_or_alloc_expression_id (tree expr)
231 tree_ann_common_t ann = tree_common_ann (expr);
233 if (ann == NULL || !ann->aux)
234 return alloc_expression_id (expr);
236 return get_expression_id (expr);
239 /* Return the expression that has expression id ID */
241 static inline tree
242 expression_for_id (unsigned int id)
244 return VEC_index (tree, expressions, id);
247 /* Free the expression id field in all of our expressions,
248 and then destroy the expressions array. */
250 static void
251 clear_expression_ids (void)
253 int i;
254 tree expr;
256 for (i = 0; VEC_iterate (tree, expressions, i, expr); i++)
258 free (tree_common_ann (expr)->aux);
259 tree_common_ann (expr)->aux = NULL;
261 VEC_free (tree, heap, expressions);
264 static bool in_fre = false;
266 /* An unordered bitmap set. One bitmap tracks values, the other,
267 expressions. */
268 typedef struct bitmap_set
270 bitmap expressions;
271 bitmap values;
272 } *bitmap_set_t;
274 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
275 EXECUTE_IF_SET_IN_BITMAP(set->expressions, 0, id, bi)
277 /* Sets that we need to keep track of. */
278 typedef struct bb_bitmap_sets
280 /* The EXP_GEN set, which represents expressions/values generated in
281 a basic block. */
282 bitmap_set_t exp_gen;
284 /* The PHI_GEN set, which represents PHI results generated in a
285 basic block. */
286 bitmap_set_t phi_gen;
288 /* The TMP_GEN set, which represents results/temporaries generated
289 in a basic block. IE the LHS of an expression. */
290 bitmap_set_t tmp_gen;
292 /* The AVAIL_OUT set, which represents which values are available in
293 a given basic block. */
294 bitmap_set_t avail_out;
296 /* The ANTIC_IN set, which represents which values are anticipatable
297 in a given basic block. */
298 bitmap_set_t antic_in;
300 /* The PA_IN set, which represents which values are
301 partially anticipatable in a given basic block. */
302 bitmap_set_t pa_in;
304 /* The NEW_SETS set, which is used during insertion to augment the
305 AVAIL_OUT set of blocks with the new insertions performed during
306 the current iteration. */
307 bitmap_set_t new_sets;
309 /* The RVUSE sets, which are used during ANTIC computation to ensure
310 that we don't mark loads ANTIC once they have died. */
311 bitmap rvuse_in;
312 bitmap rvuse_out;
313 bitmap rvuse_gen;
314 bitmap rvuse_kill;
316 /* For actually occurring loads, as long as they occur before all the
317 other stores in the block, we know they are antic at the top of
318 the block, regardless of RVUSE_KILL. */
319 bitmap_set_t antic_safe_loads;
321 /* True if we have visited this block during ANTIC calculation. */
322 unsigned int visited:1;
324 /* True we have deferred processing this block during ANTIC
325 calculation until its successor is processed. */
326 unsigned int deferred : 1;
327 } *bb_value_sets_t;
329 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
330 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
331 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
332 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
333 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
334 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
335 #define RVUSE_IN(BB) ((bb_value_sets_t) ((BB)->aux))->rvuse_in
336 #define RVUSE_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->rvuse_gen
337 #define RVUSE_KILL(BB) ((bb_value_sets_t) ((BB)->aux))->rvuse_kill
338 #define RVUSE_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->rvuse_out
339 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
340 #define ANTIC_SAFE_LOADS(BB) ((bb_value_sets_t) ((BB)->aux))->antic_safe_loads
341 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
342 #define BB_DEFERRED(BB) ((bb_value_sets_t) ((BB)->aux))->deferred
344 /* Maximal set of values, used to initialize the ANTIC problem, which
345 is an intersection problem. */
346 static bitmap_set_t maximal_set;
348 /* Basic block list in postorder. */
349 static int *postorder;
351 /* This structure is used to keep track of statistics on what
352 optimization PRE was able to perform. */
353 static struct
355 /* The number of RHS computations eliminated by PRE. */
356 int eliminations;
358 /* The number of new expressions/temporaries generated by PRE. */
359 int insertions;
361 /* The number of inserts found due to partial anticipation */
362 int pa_insert;
364 /* The number of new PHI nodes added by PRE. */
365 int phis;
367 /* The number of values found constant. */
368 int constified;
370 } pre_stats;
372 static bool do_partial_partial;
373 static tree bitmap_find_leader (bitmap_set_t, tree);
374 static void bitmap_value_insert_into_set (bitmap_set_t, tree);
375 static void bitmap_value_replace_in_set (bitmap_set_t, tree);
376 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
377 static bool bitmap_set_contains_value (bitmap_set_t, tree);
378 static void bitmap_insert_into_set (bitmap_set_t, tree);
379 static bitmap_set_t bitmap_set_new (void);
380 static bool is_undefined_value (tree);
381 static tree create_expression_by_pieces (basic_block, tree, tree);
382 static tree find_or_generate_expression (basic_block, tree, tree);
384 /* We can add and remove elements and entries to and from sets
385 and hash tables, so we use alloc pools for them. */
387 static alloc_pool bitmap_set_pool;
388 static alloc_pool binary_node_pool;
389 static alloc_pool unary_node_pool;
390 static alloc_pool reference_node_pool;
391 static alloc_pool comparison_node_pool;
392 static alloc_pool modify_expr_node_pool;
393 static bitmap_obstack grand_bitmap_obstack;
395 /* We can't use allocation pools to hold temporary CALL_EXPR objects, since
396 they are not of fixed size. Instead, use an obstack. */
398 static struct obstack temp_call_expr_obstack;
401 /* To avoid adding 300 temporary variables when we only need one, we
402 only create one temporary variable, on demand, and build ssa names
403 off that. We do have to change the variable if the types don't
404 match the current variable's type. */
405 static tree pretemp;
406 static tree storetemp;
407 static tree mergephitemp;
408 static tree prephitemp;
410 /* Set of blocks with statements that have had its EH information
411 cleaned up. */
412 static bitmap need_eh_cleanup;
414 /* The phi_translate_table caches phi translations for a given
415 expression and predecessor. */
417 static htab_t phi_translate_table;
419 /* A three tuple {e, pred, v} used to cache phi translations in the
420 phi_translate_table. */
422 typedef struct expr_pred_trans_d
424 /* The expression. */
425 tree e;
427 /* The predecessor block along which we translated the expression. */
428 basic_block pred;
430 /* vuses associated with the expression. */
431 VEC (tree, gc) *vuses;
433 /* The value that resulted from the translation. */
434 tree v;
436 /* The hashcode for the expression, pred pair. This is cached for
437 speed reasons. */
438 hashval_t hashcode;
439 } *expr_pred_trans_t;
441 /* Return the hash value for a phi translation table entry. */
443 static hashval_t
444 expr_pred_trans_hash (const void *p)
446 const expr_pred_trans_t ve = (expr_pred_trans_t) p;
447 return ve->hashcode;
450 /* Return true if two phi translation table entries are the same.
451 P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
453 static int
454 expr_pred_trans_eq (const void *p1, const void *p2)
456 const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
457 const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
458 basic_block b1 = ve1->pred;
459 basic_block b2 = ve2->pred;
460 int i;
461 tree vuse1;
463 /* If they are not translations for the same basic block, they can't
464 be equal. */
465 if (b1 != b2)
466 return false;
469 /* If they are for the same basic block, determine if the
470 expressions are equal. */
471 if (!expressions_equal_p (ve1->e, ve2->e))
472 return false;
474 /* Make sure the vuses are equivalent. */
475 if (ve1->vuses == ve2->vuses)
476 return true;
478 if (VEC_length (tree, ve1->vuses) != VEC_length (tree, ve2->vuses))
479 return false;
481 for (i = 0; VEC_iterate (tree, ve1->vuses, i, vuse1); i++)
483 if (VEC_index (tree, ve2->vuses, i) != vuse1)
484 return false;
487 return true;
490 /* Search in the phi translation table for the translation of
491 expression E in basic block PRED with vuses VUSES.
492 Return the translated value, if found, NULL otherwise. */
494 static inline tree
495 phi_trans_lookup (tree e, basic_block pred, VEC (tree, gc) *vuses)
497 void **slot;
498 struct expr_pred_trans_d ept;
500 ept.e = e;
501 ept.pred = pred;
502 ept.vuses = vuses;
503 ept.hashcode = vn_compute (e, (unsigned long) pred);
504 slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
505 NO_INSERT);
506 if (!slot)
507 return NULL;
508 else
509 return ((expr_pred_trans_t) *slot)->v;
513 /* Add the tuple mapping from {expression E, basic block PRED, vuses VUSES} to
514 value V, to the phi translation table. */
516 static inline void
517 phi_trans_add (tree e, tree v, basic_block pred, VEC (tree, gc) *vuses)
519 void **slot;
520 expr_pred_trans_t new_pair = XNEW (struct expr_pred_trans_d);
521 new_pair->e = e;
522 new_pair->pred = pred;
523 new_pair->vuses = vuses;
524 new_pair->v = v;
525 new_pair->hashcode = vn_compute (e, (unsigned long) pred);
526 slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
527 new_pair->hashcode, INSERT);
528 if (*slot)
529 free (*slot);
530 *slot = (void *) new_pair;
534 /* Return true if V is a value expression that represents itself.
535 In our world, this is *only* non-value handles. */
537 static inline bool
538 constant_expr_p (tree v)
540 return TREE_CODE (v) != VALUE_HANDLE && is_gimple_min_invariant (v);
541 /* return TREE_CODE (v) != VALUE_HANDLE; */
544 /* Add expression E to the expression set of value V. */
546 void
547 add_to_value (tree v, tree e)
549 /* Constants have no expression sets. */
550 if (constant_expr_p (v))
551 return;
553 if (VALUE_HANDLE_EXPR_SET (v) == NULL)
554 VALUE_HANDLE_EXPR_SET (v) = bitmap_set_new ();
556 bitmap_insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
559 /* Create a new bitmap set and return it. */
561 static bitmap_set_t
562 bitmap_set_new (void)
564 bitmap_set_t ret = (bitmap_set_t) pool_alloc (bitmap_set_pool);
565 ret->expressions = BITMAP_ALLOC (&grand_bitmap_obstack);
566 ret->values = BITMAP_ALLOC (&grand_bitmap_obstack);
567 return ret;
570 /* Remove an expression EXPR from a bitmapped set. */
572 static void
573 bitmap_remove_from_set (bitmap_set_t set, tree expr)
575 tree val = get_value_handle (expr);
577 gcc_assert (val);
578 if (!constant_expr_p (val))
580 bitmap_clear_bit (set->values, VALUE_HANDLE_ID (val));
581 bitmap_clear_bit (set->expressions, get_expression_id (expr));
585 /* Insert an expression EXPR into a bitmapped set. */
587 static void
588 bitmap_insert_into_set (bitmap_set_t set, tree expr)
590 tree val = get_value_handle (expr);
592 gcc_assert (val);
593 if (!constant_expr_p (val))
595 bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
596 bitmap_set_bit (set->expressions, get_or_alloc_expression_id (expr));
600 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
602 static void
603 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
605 bitmap_copy (dest->expressions, orig->expressions);
606 bitmap_copy (dest->values, orig->values);
610 /* Free memory used up by SET. */
611 static void
612 bitmap_set_free (bitmap_set_t set)
614 BITMAP_FREE (set->expressions);
615 BITMAP_FREE (set->values);
619 /* A comparison function for use in qsort to top sort a bitmap set. Simply
620 subtracts value handle ids, since they are created in topo-order. */
622 static int
623 vh_compare (const void *pa, const void *pb)
625 const tree vha = get_value_handle (*((const tree *)pa));
626 const tree vhb = get_value_handle (*((const tree *)pb));
628 /* This can happen when we constify things. */
629 if (constant_expr_p (vha))
631 if (constant_expr_p (vhb))
632 return -1;
633 return -1;
635 else if (constant_expr_p (vhb))
636 return 1;
637 return VALUE_HANDLE_ID (vha) - VALUE_HANDLE_ID (vhb);
640 /* Generate an topological-ordered array of bitmap set SET. */
642 static VEC(tree, heap) *
643 sorted_array_from_bitmap_set (bitmap_set_t set)
645 unsigned int i;
646 bitmap_iterator bi;
647 VEC(tree, heap) *result = NULL;
649 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
650 VEC_safe_push (tree, heap, result, expression_for_id (i));
652 qsort (VEC_address (tree, result), VEC_length (tree, result),
653 sizeof (tree), vh_compare);
655 return result;
658 /* Perform bitmapped set operation DEST &= ORIG. */
660 static void
661 bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig)
663 bitmap_iterator bi;
664 unsigned int i;
666 if (dest != orig)
668 bitmap temp = BITMAP_ALLOC (&grand_bitmap_obstack);
670 bitmap_and_into (dest->values, orig->values);
672 bitmap_copy (temp, dest->expressions);
673 EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
675 tree expr = expression_for_id (i);
676 tree val = get_value_handle (expr);
677 if (!bitmap_bit_p (dest->values, VALUE_HANDLE_ID (val)))
678 bitmap_clear_bit (dest->expressions, i);
680 BITMAP_FREE (temp);
684 /* Subtract all values and expressions contained in ORIG from DEST. */
686 static bitmap_set_t
687 bitmap_set_subtract (bitmap_set_t dest, bitmap_set_t orig)
689 bitmap_set_t result = bitmap_set_new ();
690 bitmap_iterator bi;
691 unsigned int i;
693 bitmap_and_compl (result->expressions, dest->expressions,
694 orig->expressions);
696 FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
698 tree expr = expression_for_id (i);
699 tree val = get_value_handle (expr);
700 bitmap_set_bit (result->values, VALUE_HANDLE_ID (val));
703 return result;
706 /* Subtract all the values in bitmap set B from bitmap set A. */
708 static void
709 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
711 unsigned int i;
712 bitmap_iterator bi;
713 bitmap temp = BITMAP_ALLOC (&grand_bitmap_obstack);
715 bitmap_copy (temp, a->expressions);
716 EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
718 tree expr = expression_for_id (i);
719 if (bitmap_set_contains_value (b, get_value_handle (expr)))
720 bitmap_remove_from_set (a, expr);
722 BITMAP_FREE (temp);
726 /* Return true if bitmapped set SET contains the value VAL. */
728 static bool
729 bitmap_set_contains_value (bitmap_set_t set, tree val)
731 if (constant_expr_p (val))
732 return true;
734 if (!set || bitmap_empty_p (set->expressions))
735 return false;
737 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
740 static inline bool
741 bitmap_set_contains_expr (bitmap_set_t set, tree expr)
743 return bitmap_bit_p (set->expressions, get_expression_id (expr));
746 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
748 static void
749 bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
751 bitmap_set_t exprset;
752 unsigned int i;
753 bitmap_iterator bi;
755 if (constant_expr_p (lookfor))
756 return;
758 if (!bitmap_set_contains_value (set, lookfor))
759 return;
761 /* The number of expressions having a given value is usually
762 significantly less than the total number of expressions in SET.
763 Thus, rather than check, for each expression in SET, whether it
764 has the value LOOKFOR, we walk the reverse mapping that tells us
765 what expressions have a given value, and see if any of those
766 expressions are in our set. For large testcases, this is about
767 5-10x faster than walking the bitmap. If this is somehow a
768 significant lose for some cases, we can choose which set to walk
769 based on the set size. */
770 exprset = VALUE_HANDLE_EXPR_SET (lookfor);
771 FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
773 if (bitmap_bit_p (set->expressions, i))
775 bitmap_clear_bit (set->expressions, i);
776 bitmap_set_bit (set->expressions, get_expression_id (expr));
777 return;
782 /* Return true if two bitmap sets are equal. */
784 static bool
785 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
787 return bitmap_equal_p (a->values, b->values);
790 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
791 and add it otherwise. */
793 static void
794 bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
796 tree val = get_value_handle (expr);
798 if (bitmap_set_contains_value (set, val))
799 bitmap_set_replace_value (set, val, expr);
800 else
801 bitmap_insert_into_set (set, expr);
804 /* Insert EXPR into SET if EXPR's value is not already present in
805 SET. */
807 static void
808 bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
810 tree val = get_value_handle (expr);
812 if (constant_expr_p (val))
813 return;
815 if (!bitmap_set_contains_value (set, val))
816 bitmap_insert_into_set (set, expr);
819 /* Print out SET to OUTFILE. */
821 static void
822 print_bitmap_set (FILE *outfile, bitmap_set_t set,
823 const char *setname, int blockindex)
825 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
826 if (set)
828 bool first = true;
829 unsigned i;
830 bitmap_iterator bi;
832 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
834 tree expr = expression_for_id (i);
836 if (!first)
837 fprintf (outfile, ", ");
838 first = false;
839 print_generic_expr (outfile, expr, 0);
841 fprintf (outfile, " (");
842 print_generic_expr (outfile, get_value_handle (expr), 0);
843 fprintf (outfile, ") ");
846 fprintf (outfile, " }\n");
849 void debug_bitmap_set (bitmap_set_t);
851 void
852 debug_bitmap_set (bitmap_set_t set)
854 print_bitmap_set (stderr, set, "debug", 0);
857 /* Print out the expressions that have VAL to OUTFILE. */
859 void
860 print_value_expressions (FILE *outfile, tree val)
862 if (VALUE_HANDLE_EXPR_SET (val))
864 char s[10];
865 sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
866 print_bitmap_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
871 void
872 debug_value_expressions (tree val)
874 print_value_expressions (stderr, val);
877 /* Return the folded version of T if T, when folded, is a gimple
878 min_invariant. Otherwise, return T. */
880 static tree
881 fully_constant_expression (tree t)
883 tree folded;
884 folded = fold (t);
885 if (folded && is_gimple_min_invariant (folded))
886 return folded;
887 return t;
890 /* Make a temporary copy of a CALL_EXPR object NODE. */
892 static tree
893 temp_copy_call_expr (tree node)
895 return (tree) obstack_copy (&temp_call_expr_obstack, node, tree_size (node));
898 /* Translate the vuses in the VUSES vector backwards through phi nodes
899 in PHIBLOCK, so that they have the value they would have in
900 BLOCK. */
902 static VEC(tree, gc) *
903 translate_vuses_through_block (VEC (tree, gc) *vuses,
904 basic_block phiblock,
905 basic_block block)
907 tree oldvuse;
908 VEC(tree, gc) *result = NULL;
909 int i;
911 for (i = 0; VEC_iterate (tree, vuses, i, oldvuse); i++)
913 tree phi = SSA_NAME_DEF_STMT (oldvuse);
914 if (TREE_CODE (phi) == PHI_NODE
915 && bb_for_stmt (phi) == phiblock)
917 edge e = find_edge (block, bb_for_stmt (phi));
918 if (e)
920 tree def = PHI_ARG_DEF (phi, e->dest_idx);
921 if (def != oldvuse)
923 if (!result)
924 result = VEC_copy (tree, gc, vuses);
925 VEC_replace (tree, result, i, def);
931 /* We avoid creating a new copy of the vuses unless something
932 actually changed, so result can be NULL. */
933 if (result)
935 sort_vuses (result);
936 return result;
938 return vuses;
942 /* Like find_leader, but checks for the value existing in SET1 *or*
943 SET2. This is used to avoid making a set consisting of the union
944 of PA_IN and ANTIC_IN during insert. */
946 static inline tree
947 find_leader_in_sets (tree expr, bitmap_set_t set1, bitmap_set_t set2)
949 tree result;
951 result = bitmap_find_leader (set1, expr);
952 if (!result && set2)
953 result = bitmap_find_leader (set2, expr);
954 return result;
957 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
958 the phis in PRED. Return NULL if we can't find a leader for each
959 part of the translated expression. */
961 static tree
962 phi_translate (tree expr, bitmap_set_t set1, bitmap_set_t set2,
963 basic_block pred, basic_block phiblock)
965 tree phitrans = NULL;
966 tree oldexpr = expr;
968 if (expr == NULL)
969 return NULL;
971 if (is_gimple_min_invariant (expr))
972 return expr;
974 /* Phi translations of a given expression don't change. */
975 if (EXPR_P (expr) || GIMPLE_STMT_P (expr))
977 tree vh;
979 vh = get_value_handle (expr);
980 if (vh && TREE_CODE (vh) == VALUE_HANDLE)
981 phitrans = phi_trans_lookup (expr, pred, VALUE_HANDLE_VUSES (vh));
982 else
983 phitrans = phi_trans_lookup (expr, pred, NULL);
985 else
986 phitrans = phi_trans_lookup (expr, pred, NULL);
988 if (phitrans)
989 return phitrans;
991 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
993 case tcc_expression:
994 return NULL;
996 case tcc_vl_exp:
998 if (TREE_CODE (expr) != CALL_EXPR)
999 return NULL;
1000 else
1002 tree oldfn = CALL_EXPR_FN (expr);
1003 tree oldsc = CALL_EXPR_STATIC_CHAIN (expr);
1004 tree newfn, newsc = NULL;
1005 tree newexpr = NULL_TREE;
1006 tree vh = get_value_handle (expr);
1007 bool invariantarg = false;
1008 int i, nargs;
1009 VEC (tree, gc) *vuses = VALUE_HANDLE_VUSES (vh);
1010 VEC (tree, gc) *tvuses;
1012 newfn = phi_translate (find_leader_in_sets (oldfn, set1, set2),
1013 set1, set2, pred, phiblock);
1014 if (newfn == NULL)
1015 return NULL;
1016 if (newfn != oldfn)
1018 newexpr = temp_copy_call_expr (expr);
1019 CALL_EXPR_FN (newexpr) = get_value_handle (newfn);
1021 if (oldsc)
1023 newsc = phi_translate (find_leader_in_sets (oldsc, set1, set2),
1024 set1, set2, pred, phiblock);
1025 if (newsc == NULL)
1026 return NULL;
1027 if (newsc != oldsc)
1029 if (!newexpr)
1030 newexpr = temp_copy_call_expr (expr);
1031 CALL_EXPR_STATIC_CHAIN (newexpr) = get_value_handle (newsc);
1035 /* phi translate the argument list piece by piece. */
1036 nargs = call_expr_nargs (expr);
1037 for (i = 0; i < nargs; i++)
1039 tree oldval = CALL_EXPR_ARG (expr, i);
1040 tree newval;
1041 if (oldval)
1043 /* This may seem like a weird place for this
1044 check, but it's actually the easiest place to
1045 do it. We can't do it lower on in the
1046 recursion because it's valid for pieces of a
1047 component ref to be of AGGREGATE_TYPE, as long
1048 as the outermost one is not.
1049 To avoid *that* case, we have a check for
1050 AGGREGATE_TYPE_P in insert_aux. However, that
1051 check will *not* catch this case because here
1052 it occurs in the argument list. */
1053 if (AGGREGATE_TYPE_P (TREE_TYPE (oldval)))
1054 return NULL;
1055 oldval = find_leader_in_sets (oldval, set1, set2);
1056 newval = phi_translate (oldval, set1, set2, pred,
1057 phiblock);
1058 if (newval == NULL)
1059 return NULL;
1060 if (newval != oldval)
1062 invariantarg |= is_gimple_min_invariant (newval);
1063 if (!newexpr)
1064 newexpr = temp_copy_call_expr (expr);
1065 CALL_EXPR_ARG (newexpr, i) = get_value_handle (newval);
1070 /* In case of new invariant args we might try to fold the call
1071 again. */
1072 if (invariantarg && !newsc)
1074 tree tmp1 = build_call_array (TREE_TYPE (expr),
1075 newfn, call_expr_nargs (newexpr),
1076 CALL_EXPR_ARGP (newexpr));
1077 tree tmp2 = fold (tmp1);
1078 if (tmp2 != tmp1)
1080 STRIP_TYPE_NOPS (tmp2);
1081 if (is_gimple_min_invariant (tmp2))
1082 return tmp2;
1086 tvuses = translate_vuses_through_block (vuses, phiblock, pred);
1087 if (vuses != tvuses && ! newexpr)
1088 newexpr = temp_copy_call_expr (expr);
1090 if (newexpr)
1092 newexpr->base.ann = NULL;
1093 vn_lookup_or_add_with_vuses (newexpr, tvuses);
1094 expr = newexpr;
1095 phi_trans_add (oldexpr, newexpr, pred, tvuses);
1099 return expr;
1101 case tcc_declaration:
1103 VEC (tree, gc) * oldvuses = NULL;
1104 VEC (tree, gc) * newvuses = NULL;
1106 oldvuses = VALUE_HANDLE_VUSES (get_value_handle (expr));
1107 if (oldvuses)
1108 newvuses = translate_vuses_through_block (oldvuses, phiblock,
1109 pred);
1111 if (oldvuses != newvuses)
1112 vn_lookup_or_add_with_vuses (expr, newvuses);
1114 phi_trans_add (oldexpr, expr, pred, newvuses);
1116 return expr;
1118 case tcc_reference:
1120 tree oldop0 = TREE_OPERAND (expr, 0);
1121 tree oldop1 = NULL;
1122 tree newop0;
1123 tree newop1 = NULL;
1124 tree oldop2 = NULL;
1125 tree newop2 = NULL;
1126 tree oldop3 = NULL;
1127 tree newop3 = NULL;
1128 tree newexpr;
1129 VEC (tree, gc) * oldvuses = NULL;
1130 VEC (tree, gc) * newvuses = NULL;
1132 if (TREE_CODE (expr) != INDIRECT_REF
1133 && TREE_CODE (expr) != COMPONENT_REF
1134 && TREE_CODE (expr) != ARRAY_REF)
1135 return NULL;
1137 oldop0 = find_leader_in_sets (oldop0, set1, set2);
1138 newop0 = phi_translate (oldop0, set1, set2, pred, phiblock);
1139 if (newop0 == NULL)
1140 return NULL;
1142 if (TREE_CODE (expr) == ARRAY_REF)
1144 oldop1 = TREE_OPERAND (expr, 1);
1145 oldop1 = find_leader_in_sets (oldop1, set1, set2);
1146 newop1 = phi_translate (oldop1, set1, set2, pred, phiblock);
1148 if (newop1 == NULL)
1149 return NULL;
1151 oldop2 = TREE_OPERAND (expr, 2);
1152 if (oldop2)
1154 oldop2 = find_leader_in_sets (oldop2, set1, set2);
1155 newop2 = phi_translate (oldop2, set1, set2, pred, phiblock);
1157 if (newop2 == NULL)
1158 return NULL;
1160 oldop3 = TREE_OPERAND (expr, 3);
1161 if (oldop3)
1163 oldop3 = find_leader_in_sets (oldop3, set1, set2);
1164 newop3 = phi_translate (oldop3, set1, set2, pred, phiblock);
1166 if (newop3 == NULL)
1167 return NULL;
1171 oldvuses = VALUE_HANDLE_VUSES (get_value_handle (expr));
1172 if (oldvuses)
1173 newvuses = translate_vuses_through_block (oldvuses, phiblock,
1174 pred);
1176 if (newop0 != oldop0 || newvuses != oldvuses
1177 || newop1 != oldop1
1178 || newop2 != oldop2
1179 || newop3 != oldop3)
1181 tree t;
1183 newexpr = (tree) pool_alloc (reference_node_pool);
1184 memcpy (newexpr, expr, tree_size (expr));
1185 TREE_OPERAND (newexpr, 0) = get_value_handle (newop0);
1186 if (TREE_CODE (expr) == ARRAY_REF)
1188 TREE_OPERAND (newexpr, 1) = get_value_handle (newop1);
1189 if (newop2)
1190 TREE_OPERAND (newexpr, 2) = get_value_handle (newop2);
1191 if (newop3)
1192 TREE_OPERAND (newexpr, 3) = get_value_handle (newop3);
1195 t = fully_constant_expression (newexpr);
1197 if (t != newexpr)
1199 pool_free (reference_node_pool, newexpr);
1200 newexpr = t;
1202 else
1204 newexpr->base.ann = NULL;
1205 vn_lookup_or_add_with_vuses (newexpr, newvuses);
1207 expr = newexpr;
1208 phi_trans_add (oldexpr, newexpr, pred, newvuses);
1211 return expr;
1212 break;
1214 case tcc_binary:
1215 case tcc_comparison:
1217 tree oldop1 = TREE_OPERAND (expr, 0);
1218 tree oldval1 = oldop1;
1219 tree oldop2 = TREE_OPERAND (expr, 1);
1220 tree oldval2 = oldop2;
1221 tree newop1;
1222 tree newop2;
1223 tree newexpr;
1225 oldop1 = find_leader_in_sets (oldop1, set1, set2);
1226 newop1 = phi_translate (oldop1, set1, set2, pred, phiblock);
1227 if (newop1 == NULL)
1228 return NULL;
1230 oldop2 = find_leader_in_sets (oldop2, set1, set2);
1231 newop2 = phi_translate (oldop2, set1, set2, pred, phiblock);
1232 if (newop2 == NULL)
1233 return NULL;
1234 if (newop1 != oldop1 || newop2 != oldop2)
1236 tree t;
1237 newexpr = (tree) pool_alloc (binary_node_pool);
1238 memcpy (newexpr, expr, tree_size (expr));
1239 TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldval1 : get_value_handle (newop1);
1240 TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldval2 : get_value_handle (newop2);
1241 t = fully_constant_expression (newexpr);
1242 if (t != newexpr)
1244 pool_free (binary_node_pool, newexpr);
1245 newexpr = t;
1247 else
1249 newexpr->base.ann = NULL;
1250 vn_lookup_or_add (newexpr, NULL);
1252 expr = newexpr;
1253 phi_trans_add (oldexpr, newexpr, pred, NULL);
1256 return expr;
1258 case tcc_unary:
1260 tree oldop1 = TREE_OPERAND (expr, 0);
1261 tree newop1;
1262 tree newexpr;
1264 oldop1 = find_leader_in_sets (oldop1, set1, set2);
1265 newop1 = phi_translate (oldop1, set1, set2, pred, phiblock);
1266 if (newop1 == NULL)
1267 return NULL;
1268 if (newop1 != oldop1)
1270 tree t;
1271 newexpr = (tree) pool_alloc (unary_node_pool);
1272 memcpy (newexpr, expr, tree_size (expr));
1273 TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
1274 t = fully_constant_expression (newexpr);
1275 if (t != newexpr)
1277 pool_free (unary_node_pool, newexpr);
1278 newexpr = t;
1280 else
1282 newexpr->base.ann = NULL;
1283 vn_lookup_or_add (newexpr, NULL);
1285 expr = newexpr;
1286 phi_trans_add (oldexpr, newexpr, pred, NULL);
1289 return expr;
1291 case tcc_exceptional:
1293 tree phi = NULL;
1294 edge e;
1295 tree def_stmt;
1296 gcc_assert (TREE_CODE (expr) == SSA_NAME);
1298 def_stmt = SSA_NAME_DEF_STMT (expr);
1299 if (TREE_CODE (def_stmt) == PHI_NODE
1300 && bb_for_stmt (def_stmt) == phiblock)
1301 phi = def_stmt;
1302 else
1303 return expr;
1305 e = find_edge (pred, bb_for_stmt (phi));
1306 if (e)
1308 if (is_undefined_value (PHI_ARG_DEF (phi, e->dest_idx)))
1309 return NULL;
1310 vn_lookup_or_add (PHI_ARG_DEF (phi, e->dest_idx), NULL);
1311 return PHI_ARG_DEF (phi, e->dest_idx);
1314 return expr;
1316 default:
1317 gcc_unreachable ();
1321 /* For each expression in SET, translate the value handles through phi nodes
1322 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1323 expressions in DEST. */
1325 static void
1326 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
1327 basic_block phiblock)
1329 VEC (tree, heap) *exprs;
1330 tree expr;
1331 int i;
1333 if (!phi_nodes (phiblock))
1335 bitmap_set_copy (dest, set);
1336 return;
1339 exprs = sorted_array_from_bitmap_set (set);
1340 for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
1342 tree translated;
1343 translated = phi_translate (expr, set, NULL, pred, phiblock);
1345 /* Don't add constants or empty translations to the cache, since
1346 we won't look them up that way, or use the result, anyway. */
1347 if (translated && !is_gimple_min_invariant (translated))
1349 tree vh = get_value_handle (translated);
1350 VEC (tree, gc) *vuses;
1352 /* The value handle itself may also be an invariant, in
1353 which case, it has no vuses. */
1354 vuses = !is_gimple_min_invariant (vh)
1355 ? VALUE_HANDLE_VUSES (vh) : NULL;
1356 phi_trans_add (expr, translated, pred, vuses);
1359 if (translated != NULL)
1360 bitmap_value_insert_into_set (dest, translated);
1362 VEC_free (tree, heap, exprs);
1365 /* Find the leader for a value (i.e., the name representing that
1366 value) in a given set, and return it. Return NULL if no leader is
1367 found. */
1369 static tree
1370 bitmap_find_leader (bitmap_set_t set, tree val)
1372 if (val == NULL)
1373 return NULL;
1375 if (constant_expr_p (val))
1376 return val;
1378 if (bitmap_set_contains_value (set, val))
1380 /* Rather than walk the entire bitmap of expressions, and see
1381 whether any of them has the value we are looking for, we look
1382 at the reverse mapping, which tells us the set of expressions
1383 that have a given value (IE value->expressions with that
1384 value) and see if any of those expressions are in our set.
1385 The number of expressions per value is usually significantly
1386 less than the number of expressions in the set. In fact, for
1387 large testcases, doing it this way is roughly 5-10x faster
1388 than walking the bitmap.
1389 If this is somehow a significant lose for some cases, we can
1390 choose which set to walk based on which set is smaller. */
1391 unsigned int i;
1392 bitmap_iterator bi;
1393 bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
1395 EXECUTE_IF_AND_IN_BITMAP (exprset->expressions,
1396 set->expressions, 0, i, bi)
1397 return expression_for_id (i);
1399 return NULL;
1402 /* Given the vuse representative map, MAP, and an SSA version number,
1403 ID, return the bitmap of names ID represents, or NULL, if none
1404 exists. */
1406 static bitmap
1407 get_representative (bitmap *map, int id)
1409 if (map[id] != NULL)
1410 return map[id];
1411 return NULL;
1414 /* A vuse is anticipable at the top of block x, from the bottom of the
1415 block, if it reaches the top of the block, and is not killed in the
1416 block. In effect, we are trying to see if the vuse is transparent
1417 backwards in the block. */
1419 static bool
1420 vuses_dies_in_block_x (VEC (tree, gc) *vuses, basic_block block)
1422 int i;
1423 tree vuse;
1425 for (i = 0; VEC_iterate (tree, vuses, i, vuse); i++)
1427 /* Any places where this is too conservative, are places
1428 where we created a new version and shouldn't have. */
1430 if (!bitmap_bit_p (RVUSE_IN (block), SSA_NAME_VERSION (vuse))
1431 || bitmap_bit_p (RVUSE_KILL (block), SSA_NAME_VERSION (vuse)))
1432 return true;
1434 return false;
1437 /* Determine if the expression EXPR is valid in SET1 U SET2.
1438 ONLY SET2 CAN BE NULL.
1439 This means that we have a leader for each part of the expression
1440 (if it consists of values), or the expression is an SSA_NAME.
1442 NB: We never should run into a case where we have SSA_NAME +
1443 SSA_NAME or SSA_NAME + value. The sets valid_in_sets is called on,
1444 the ANTIC sets, will only ever have SSA_NAME's or value expressions
1445 (IE VALUE1 + VALUE2, *VALUE1, VALUE1 < VALUE2) */
1447 #define union_contains_value(SET1, SET2, VAL) \
1448 (bitmap_set_contains_value ((SET1), (VAL)) \
1449 || ((SET2) && bitmap_set_contains_value ((SET2), (VAL))))
1451 static bool
1452 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree expr,
1453 basic_block block)
1455 tree vh = get_value_handle (expr);
1456 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1458 case tcc_binary:
1459 case tcc_comparison:
1461 tree op1 = TREE_OPERAND (expr, 0);
1462 tree op2 = TREE_OPERAND (expr, 1);
1464 return union_contains_value (set1, set2, op1)
1465 && union_contains_value (set1, set2, op2);
1468 case tcc_unary:
1470 tree op1 = TREE_OPERAND (expr, 0);
1471 return union_contains_value (set1, set2, op1);
1474 case tcc_expression:
1475 return false;
1477 case tcc_vl_exp:
1479 if (TREE_CODE (expr) == CALL_EXPR)
1481 tree fn = CALL_EXPR_FN (expr);
1482 tree sc = CALL_EXPR_STATIC_CHAIN (expr);
1483 tree arg;
1484 call_expr_arg_iterator iter;
1486 /* Check the non-argument operands first. */
1487 if (!union_contains_value (set1, set2, fn)
1488 || (sc && !union_contains_value (set1, set2, sc)))
1489 return false;
1491 /* Now check the operands. */
1492 FOR_EACH_CALL_EXPR_ARG (arg, iter, expr)
1494 if (!union_contains_value (set1, set2, arg))
1495 return false;
1497 return !vuses_dies_in_block_x (VALUE_HANDLE_VUSES (vh), block);
1499 return false;
1502 case tcc_reference:
1504 if (TREE_CODE (expr) == INDIRECT_REF
1505 || TREE_CODE (expr) == COMPONENT_REF
1506 || TREE_CODE (expr) == ARRAY_REF)
1508 tree op0 = TREE_OPERAND (expr, 0);
1509 gcc_assert (is_gimple_min_invariant (op0)
1510 || TREE_CODE (op0) == VALUE_HANDLE);
1511 if (!union_contains_value (set1, set2, op0))
1512 return false;
1513 if (TREE_CODE (expr) == ARRAY_REF)
1515 tree op1 = TREE_OPERAND (expr, 1);
1516 tree op2 = TREE_OPERAND (expr, 2);
1517 tree op3 = TREE_OPERAND (expr, 3);
1518 gcc_assert (is_gimple_min_invariant (op1)
1519 || TREE_CODE (op1) == VALUE_HANDLE);
1520 if (!union_contains_value (set1, set2, op1))
1521 return false;
1522 gcc_assert (!op2 || is_gimple_min_invariant (op2)
1523 || TREE_CODE (op2) == VALUE_HANDLE);
1524 if (op2
1525 && !union_contains_value (set1, set2, op2))
1526 return false;
1527 gcc_assert (!op3 || is_gimple_min_invariant (op3)
1528 || TREE_CODE (op3) == VALUE_HANDLE);
1529 if (op3
1530 && !union_contains_value (set1, set2, op3))
1531 return false;
1533 return bitmap_set_contains_value (ANTIC_SAFE_LOADS (block),
1535 || !vuses_dies_in_block_x (VALUE_HANDLE_VUSES (vh),
1536 block);
1539 return false;
1541 case tcc_exceptional:
1543 gcc_assert (TREE_CODE (expr) == SSA_NAME);
1544 return bitmap_set_contains_expr (AVAIL_OUT (block), expr);
1547 case tcc_declaration:
1548 return !vuses_dies_in_block_x (VALUE_HANDLE_VUSES (vh), block);
1550 default:
1551 /* No other cases should be encountered. */
1552 gcc_unreachable ();
1556 /* Clean the set of expressions that are no longer valid in SET1 or
1557 SET2. This means expressions that are made up of values we have no
1558 leaders for in SET1 or SET2. This version is used for partial
1559 anticipation, which means it is not valid in either ANTIC_IN or
1560 PA_IN. */
1562 static void
1563 dependent_clean (bitmap_set_t set1, bitmap_set_t set2, basic_block block)
1565 VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set1);
1566 tree expr;
1567 int i;
1569 for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
1571 if (!valid_in_sets (set1, set2, expr, block))
1572 bitmap_remove_from_set (set1, expr);
1574 VEC_free (tree, heap, exprs);
1577 /* Clean the set of expressions that are no longer valid in SET. This
1578 means expressions that are made up of values we have no leaders for
1579 in SET. */
1581 static void
1582 clean (bitmap_set_t set, basic_block block)
1584 VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set);
1585 tree expr;
1586 int i;
1588 for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
1590 if (!valid_in_sets (set, NULL, expr, block))
1591 bitmap_remove_from_set (set, expr);
1593 VEC_free (tree, heap, exprs);
1596 static sbitmap has_abnormal_preds;
1599 /* List of blocks that may have changed during ANTIC computation and
1600 thus need to be iterated over. */
1602 static sbitmap changed_blocks;
1603 /* Compute the ANTIC set for BLOCK.
1605 If succs(BLOCK) > 1 then
1606 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
1607 else if succs(BLOCK) == 1 then
1608 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
1610 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
1613 static bool
1614 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
1616 bool changed = false;
1617 bitmap_set_t S, old, ANTIC_OUT;
1618 bitmap_iterator bi;
1619 unsigned int bii;
1620 edge e;
1621 edge_iterator ei;
1623 old = ANTIC_OUT = S = NULL;
1624 BB_VISITED (block) = 1;
1626 /* If any edges from predecessors are abnormal, antic_in is empty,
1627 so do nothing. */
1628 if (block_has_abnormal_pred_edge)
1629 goto maybe_dump_sets;
1631 old = ANTIC_IN (block);
1632 ANTIC_OUT = bitmap_set_new ();
1634 /* If the block has no successors, ANTIC_OUT is empty. */
1635 if (EDGE_COUNT (block->succs) == 0)
1637 /* If we have one successor, we could have some phi nodes to
1638 translate through. */
1639 else if (single_succ_p (block))
1641 basic_block succ_bb = single_succ (block);
1643 /* We trade iterations of the dataflow equations for having to
1644 phi translate the maximal set, which is incredibly slow
1645 (since the maximal set often has 300+ members, even when you
1646 have a small number of blocks).
1647 Basically, we defer the computation of ANTIC for this block
1648 until we have processed it's successor, which will inevitably
1649 have a *much* smaller set of values to phi translate once
1650 clean has been run on it.
1651 The cost of doing this is that we technically perform more
1652 iterations, however, they are lower cost iterations.
1654 Timings for PRE on tramp3d-v4:
1655 without maximal set fix: 11 seconds
1656 with maximal set fix/without deferring: 26 seconds
1657 with maximal set fix/with deferring: 11 seconds
1660 if (!BB_VISITED (succ_bb))
1662 changed = true;
1663 SET_BIT (changed_blocks, block->index);
1664 BB_VISITED (block) = 0;
1665 BB_DEFERRED (block) = 1;
1666 goto maybe_dump_sets;
1668 else
1669 phi_translate_set (ANTIC_OUT, ANTIC_IN (succ_bb),
1670 block, succ_bb);
1674 /* If we have multiple successors, we take the intersection of all of
1675 them. */
1676 else
1678 VEC(basic_block, heap) * worklist;
1679 size_t i;
1680 basic_block bprime, first;
1682 worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
1683 FOR_EACH_EDGE (e, ei, block->succs)
1684 VEC_quick_push (basic_block, worklist, e->dest);
1685 first = VEC_index (basic_block, worklist, 0);
1687 if (!BB_VISITED (first))
1688 bitmap_set_copy (ANTIC_OUT, maximal_set);
1689 else
1690 bitmap_set_copy (ANTIC_OUT, ANTIC_IN (first));
1692 for (i = 1; VEC_iterate (basic_block, worklist, i, bprime); i++)
1694 if (!BB_VISITED (bprime))
1695 bitmap_set_and (ANTIC_OUT, maximal_set);
1696 else
1697 bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime));
1699 VEC_free (basic_block, heap, worklist);
1702 /* Generate ANTIC_OUT - TMP_GEN. */
1703 S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block));
1705 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
1706 ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block),
1707 TMP_GEN (block));
1709 /* Then union in the ANTIC_OUT - TMP_GEN values,
1710 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
1711 FOR_EACH_EXPR_ID_IN_SET (S, bii, bi)
1712 bitmap_value_insert_into_set (ANTIC_IN (block),
1713 expression_for_id (bii));
1715 clean (ANTIC_IN (block), block);
1717 /* !old->expressions can happen when we deferred a block. */
1718 if (!old->expressions || !bitmap_set_equal (old, ANTIC_IN (block)))
1720 changed = true;
1721 SET_BIT (changed_blocks, block->index);
1722 FOR_EACH_EDGE (e, ei, block->preds)
1723 SET_BIT (changed_blocks, e->src->index);
1725 else
1726 RESET_BIT (changed_blocks, block->index);
1728 maybe_dump_sets:
1729 if (dump_file && (dump_flags & TDF_DETAILS))
1731 if (!BB_DEFERRED (block) || BB_VISITED (block))
1733 if (ANTIC_OUT)
1734 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1736 if (ANTIC_SAFE_LOADS (block))
1737 print_bitmap_set (dump_file, ANTIC_SAFE_LOADS (block),
1738 "ANTIC_SAFE_LOADS", block->index);
1739 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
1740 block->index);
1742 if (S)
1743 print_bitmap_set (dump_file, S, "S", block->index);
1745 else
1747 fprintf (dump_file,
1748 "Block %d was deferred for a future iteration.\n",
1749 block->index);
1752 if (old)
1753 bitmap_set_free (old);
1754 if (S)
1755 bitmap_set_free (S);
1756 if (ANTIC_OUT)
1757 bitmap_set_free (ANTIC_OUT);
1758 return changed;
1761 /* Compute PARTIAL_ANTIC for BLOCK.
1763 If succs(BLOCK) > 1 then
1764 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
1765 in ANTIC_OUT for all succ(BLOCK)
1766 else if succs(BLOCK) == 1 then
1767 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
1769 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
1770 - ANTIC_IN[BLOCK])
1773 static bool
1774 compute_partial_antic_aux (basic_block block,
1775 bool block_has_abnormal_pred_edge)
1777 bool changed = false;
1778 bitmap_set_t old_PA_IN;
1779 bitmap_set_t PA_OUT;
1780 edge e;
1781 edge_iterator ei;
1783 old_PA_IN = PA_OUT = NULL;
1785 /* If any edges from predecessors are abnormal, antic_in is empty,
1786 so do nothing. */
1787 if (block_has_abnormal_pred_edge)
1788 goto maybe_dump_sets;
1790 old_PA_IN = PA_IN (block);
1791 PA_OUT = bitmap_set_new ();
1793 /* If the block has no successors, ANTIC_OUT is empty. */
1794 if (EDGE_COUNT (block->succs) == 0)
1796 /* If we have one successor, we could have some phi nodes to
1797 translate through. Note that we can't phi translate across DFS
1798 back edges in partial antic, because it uses a union operation
1799 on the successors. For recurrences like IV's, we will end up generating a
1800 new value in the set on each go around (i + 3 (VH.1) VH.1 + 1
1801 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
1802 else if (single_succ_p (block))
1804 basic_block succ = single_succ (block);
1805 if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK))
1806 phi_translate_set (PA_OUT, PA_IN (succ), block, succ);
1808 /* If we have multiple successors, we take the union of all of
1809 them. */
1810 else
1812 VEC(basic_block, heap) * worklist;
1813 size_t i;
1814 basic_block bprime;
1816 worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
1817 FOR_EACH_EDGE (e, ei, block->succs)
1819 if (e->flags & EDGE_DFS_BACK)
1820 continue;
1821 VEC_quick_push (basic_block, worklist, e->dest);
1823 if (VEC_length (basic_block, worklist) > 0)
1825 for (i = 0; VEC_iterate (basic_block, worklist, i, bprime); i++)
1827 unsigned int i;
1828 bitmap_iterator bi;
1830 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi)
1831 bitmap_value_insert_into_set (PA_OUT,
1832 expression_for_id (i));
1834 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi)
1835 bitmap_value_insert_into_set (PA_OUT,
1836 expression_for_id (i));
1839 VEC_free (basic_block, heap, worklist);
1842 /* PA_IN starts with PA_OUT - TMP_GEN.
1843 Then we subtract things from ANTIC_IN. */
1844 PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block));
1846 /* For partial antic, we want to put back in the phi results, since
1847 we will properly avoid making them partially antic over backedges. */
1848 bitmap_ior_into (PA_IN (block)->values, PHI_GEN (block)->values);
1849 bitmap_ior_into (PA_IN (block)->expressions, PHI_GEN (block)->expressions);
1851 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
1852 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
1854 dependent_clean (PA_IN (block), ANTIC_IN (block), block);
1856 if (!bitmap_set_equal (old_PA_IN, PA_IN (block)))
1858 changed = true;
1859 SET_BIT (changed_blocks, block->index);
1860 FOR_EACH_EDGE (e, ei, block->preds)
1861 SET_BIT (changed_blocks, e->src->index);
1863 else
1864 RESET_BIT (changed_blocks, block->index);
1866 maybe_dump_sets:
1867 if (dump_file && (dump_flags & TDF_DETAILS))
1869 if (PA_OUT)
1870 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
1872 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
1874 if (old_PA_IN)
1875 bitmap_set_free (old_PA_IN);
1876 if (PA_OUT)
1877 bitmap_set_free (PA_OUT);
1878 return changed;
1881 /* Compute ANTIC and partial ANTIC sets. */
1883 static void
1884 compute_antic (void)
1886 bool changed = true;
1887 int num_iterations = 0;
1888 basic_block block;
1889 int i;
1891 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
1892 We pre-build the map of blocks with incoming abnormal edges here. */
1893 has_abnormal_preds = sbitmap_alloc (last_basic_block);
1894 sbitmap_zero (has_abnormal_preds);
1896 FOR_EACH_BB (block)
1898 edge_iterator ei;
1899 edge e;
1901 FOR_EACH_EDGE (e, ei, block->preds)
1903 e->flags &= ~EDGE_DFS_BACK;
1904 if (e->flags & EDGE_ABNORMAL)
1906 SET_BIT (has_abnormal_preds, block->index);
1907 break;
1911 BB_VISITED (block) = 0;
1912 BB_DEFERRED (block) = 0;
1913 /* While we are here, give empty ANTIC_IN sets to each block. */
1914 ANTIC_IN (block) = bitmap_set_new ();
1915 PA_IN (block) = bitmap_set_new ();
1918 /* At the exit block we anticipate nothing. */
1919 ANTIC_IN (EXIT_BLOCK_PTR) = bitmap_set_new ();
1920 BB_VISITED (EXIT_BLOCK_PTR) = 1;
1921 PA_IN (EXIT_BLOCK_PTR) = bitmap_set_new ();
1923 changed_blocks = sbitmap_alloc (last_basic_block + 1);
1924 sbitmap_ones (changed_blocks);
1925 while (changed)
1927 if (dump_file && (dump_flags & TDF_DETAILS))
1928 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
1929 num_iterations++;
1930 changed = false;
1931 for (i = 0; i < last_basic_block - NUM_FIXED_BLOCKS; i++)
1933 if (TEST_BIT (changed_blocks, postorder[i]))
1935 basic_block block = BASIC_BLOCK (postorder[i]);
1936 changed |= compute_antic_aux (block,
1937 TEST_BIT (has_abnormal_preds,
1938 block->index));
1941 /* Theoretically possible, but *highly* unlikely. */
1942 gcc_assert (num_iterations < 50);
1945 if (dump_file && (dump_flags & TDF_STATS))
1946 fprintf (dump_file, "compute_antic required %d iterations\n",
1947 num_iterations);
1949 if (do_partial_partial)
1951 sbitmap_ones (changed_blocks);
1952 mark_dfs_back_edges ();
1953 num_iterations = 0;
1954 changed = true;
1955 while (changed)
1957 if (dump_file && (dump_flags & TDF_DETAILS))
1958 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
1959 num_iterations++;
1960 changed = false;
1961 for (i = 0; i < last_basic_block - NUM_FIXED_BLOCKS; i++)
1963 if (TEST_BIT (changed_blocks, postorder[i]))
1965 basic_block block = BASIC_BLOCK (postorder[i]);
1966 changed
1967 |= compute_partial_antic_aux (block,
1968 TEST_BIT (has_abnormal_preds,
1969 block->index));
1972 /* Theoretically possible, but *highly* unlikely. */
1973 gcc_assert (num_iterations < 50);
1975 if (dump_file && (dump_flags & TDF_STATS))
1976 fprintf (dump_file, "compute_partial_antic required %d iterations\n",
1977 num_iterations);
1979 sbitmap_free (has_abnormal_preds);
1980 sbitmap_free (changed_blocks);
1983 /* Print the names represented by the bitmap NAMES, to the file OUT. */
1985 static void
1986 dump_bitmap_of_names (FILE *out, bitmap names)
1988 bitmap_iterator bi;
1989 unsigned int i;
1991 fprintf (out, " { ");
1992 EXECUTE_IF_SET_IN_BITMAP (names, 0, i, bi)
1994 print_generic_expr (out, ssa_name (i), 0);
1995 fprintf (out, " ");
1997 fprintf (out, "}\n");
2000 /* Compute a set of representative vuse versions for each phi. This
2001 is so we can compute conservative kill sets in terms of all vuses
2002 that are killed, instead of continually walking chains.
2004 We also have to be able kill all names associated with a phi when
2005 the phi dies in order to ensure we don't generate overlapping
2006 live ranges, which are not allowed in virtual SSA. */
2008 static bitmap *vuse_names;
2009 static void
2010 compute_vuse_representatives (void)
2012 tree phi;
2013 basic_block bb;
2014 VEC (tree, heap) *phis = NULL;
2015 bool changed = true;
2016 size_t i;
2018 FOR_EACH_BB (bb)
2020 for (phi = phi_nodes (bb);
2021 phi;
2022 phi = PHI_CHAIN (phi))
2023 if (!is_gimple_reg (PHI_RESULT (phi)))
2024 VEC_safe_push (tree, heap, phis, phi);
2027 while (changed)
2029 changed = false;
2031 for (i = 0; VEC_iterate (tree, phis, i, phi); i++)
2033 size_t ver = SSA_NAME_VERSION (PHI_RESULT (phi));
2034 use_operand_p usep;
2035 ssa_op_iter iter;
2037 if (vuse_names[ver] == NULL)
2039 vuse_names[ver] = BITMAP_ALLOC (&grand_bitmap_obstack);
2040 bitmap_set_bit (vuse_names[ver], ver);
2042 FOR_EACH_PHI_ARG (usep, phi, iter, SSA_OP_ALL_USES)
2044 tree use = USE_FROM_PTR (usep);
2045 bitmap usebitmap = get_representative (vuse_names,
2046 SSA_NAME_VERSION (use));
2047 if (usebitmap != NULL)
2049 changed |= bitmap_ior_into (vuse_names[ver],
2050 usebitmap);
2052 else
2054 changed |= !bitmap_bit_p (vuse_names[ver],
2055 SSA_NAME_VERSION (use));
2056 if (changed)
2057 bitmap_set_bit (vuse_names[ver],
2058 SSA_NAME_VERSION (use));
2064 if (dump_file && (dump_flags & TDF_DETAILS))
2065 for (i = 0; VEC_iterate (tree, phis, i, phi); i++)
2067 bitmap reps = get_representative (vuse_names,
2068 SSA_NAME_VERSION (PHI_RESULT (phi)));
2069 if (reps)
2071 print_generic_expr (dump_file, PHI_RESULT (phi), 0);
2072 fprintf (dump_file, " represents ");
2073 dump_bitmap_of_names (dump_file, reps);
2076 VEC_free (tree, heap, phis);
2079 /* Compute reaching vuses and antic safe loads. RVUSE computation is
2080 is a small bit of iterative dataflow to determine what virtual uses
2081 reach what blocks. Because we can't generate overlapping virtual
2082 uses, and virtual uses *do* actually die, this ends up being faster
2083 in most cases than continually walking the virtual use/def chains
2084 to determine whether we are inside a block where a given virtual is
2085 still available to be used.
2087 ANTIC_SAFE_LOADS are those loads that actually occur before any kill to
2088 their vuses in the block,and thus, are safe at the top of the
2089 block.
2091 An example:
2093 <block begin>
2094 b = *a
2095 *a = 9
2096 <block end>
2098 b = *a is an antic safe load because it still safe to consider it
2099 ANTIC at the top of the block.
2101 We currently compute a conservative approximation to
2102 ANTIC_SAFE_LOADS. We compute those loads that occur before *any*
2103 stores in the block. This is not because it is difficult to
2104 compute the precise answer, but because it is expensive. More
2105 testing is necessary to determine whether it is worth computing the
2106 precise answer. */
2108 static void
2109 compute_rvuse_and_antic_safe (void)
2112 unsigned int i;
2113 tree phi;
2114 basic_block bb;
2115 bool changed = true;
2116 unsigned int *first_store_uid;
2118 first_store_uid = xcalloc (n_basic_blocks, sizeof (unsigned int));
2120 compute_vuse_representatives ();
2122 FOR_ALL_BB (bb)
2124 RVUSE_IN (bb) = BITMAP_ALLOC (&grand_bitmap_obstack);
2125 RVUSE_GEN (bb) = BITMAP_ALLOC (&grand_bitmap_obstack);
2126 RVUSE_KILL (bb) = BITMAP_ALLOC (&grand_bitmap_obstack);
2127 RVUSE_OUT (bb) = BITMAP_ALLOC (&grand_bitmap_obstack);
2128 ANTIC_SAFE_LOADS (bb) = NULL;
2131 /* Mark live on entry */
2132 for (i = 0; i < num_ssa_names; i++)
2134 tree name = ssa_name (i);
2135 if (name && !is_gimple_reg (name)
2136 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (name)))
2137 bitmap_set_bit (RVUSE_OUT (ENTRY_BLOCK_PTR),
2138 SSA_NAME_VERSION (name));
2141 /* Compute local sets for reaching vuses.
2142 GEN(block) = generated in block and not locally killed.
2143 KILL(block) = set of vuses killed in block.
2146 FOR_EACH_BB (bb)
2148 block_stmt_iterator bsi;
2149 ssa_op_iter iter;
2150 def_operand_p defp;
2151 use_operand_p usep;
2153 for (bsi = bsi_start (bb); !bsi_end_p (bsi); bsi_next (&bsi))
2155 tree stmt = bsi_stmt (bsi);
2157 if (first_store_uid[bb->index] == 0
2158 && !ZERO_SSA_OPERANDS (stmt, SSA_OP_VMAYUSE | SSA_OP_VDEF))
2160 first_store_uid[bb->index] = stmt_ann (stmt)->uid;
2163 FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_VMAYUSE)
2165 tree use = USE_FROM_PTR (usep);
2166 bitmap repbit = get_representative (vuse_names,
2167 SSA_NAME_VERSION (use));
2168 if (repbit != NULL)
2170 bitmap_and_compl_into (RVUSE_GEN (bb), repbit);
2171 bitmap_ior_into (RVUSE_KILL (bb), repbit);
2173 else
2175 bitmap_set_bit (RVUSE_KILL (bb), SSA_NAME_VERSION (use));
2176 bitmap_clear_bit (RVUSE_GEN (bb), SSA_NAME_VERSION (use));
2179 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_VIRTUAL_DEFS)
2181 tree def = DEF_FROM_PTR (defp);
2182 bitmap_set_bit (RVUSE_GEN (bb), SSA_NAME_VERSION (def));
2187 FOR_EACH_BB (bb)
2189 for (phi = phi_nodes (bb); phi; phi = PHI_CHAIN (phi))
2191 if (!is_gimple_reg (PHI_RESULT (phi)))
2193 edge e;
2194 edge_iterator ei;
2196 tree def = PHI_RESULT (phi);
2197 /* In reality, the PHI result is generated at the end of
2198 each predecessor block. This will make the value
2199 LVUSE_IN for the bb containing the PHI, which is
2200 correct. */
2201 FOR_EACH_EDGE (e, ei, bb->preds)
2202 bitmap_set_bit (RVUSE_GEN (e->src), SSA_NAME_VERSION (def));
2207 /* Solve reaching vuses.
2209 RVUSE_IN[BB] = Union of RVUSE_OUT of predecessors.
2210 RVUSE_OUT[BB] = RVUSE_GEN[BB] U (RVUSE_IN[BB] - RVUSE_KILL[BB])
2213 changed = true;
2214 while (changed)
2216 int j;
2217 changed = false;
2218 for (j = n_basic_blocks - NUM_FIXED_BLOCKS - 1; j >= 0; j--)
2220 edge e;
2221 edge_iterator ei;
2222 bb = BASIC_BLOCK (postorder[j]);
2224 FOR_EACH_EDGE (e, ei, bb->preds)
2225 bitmap_ior_into (RVUSE_IN (bb), RVUSE_OUT (e->src));
2227 changed |= bitmap_ior_and_compl (RVUSE_OUT (bb),
2228 RVUSE_GEN (bb),
2229 RVUSE_IN (bb),
2230 RVUSE_KILL (bb));
2234 if (dump_file && (dump_flags & TDF_DETAILS))
2236 FOR_ALL_BB (bb)
2238 fprintf (dump_file, "RVUSE_IN (%d) =", bb->index);
2239 dump_bitmap_of_names (dump_file, RVUSE_IN (bb));
2241 fprintf (dump_file, "RVUSE_KILL (%d) =", bb->index);
2242 dump_bitmap_of_names (dump_file, RVUSE_KILL (bb));
2244 fprintf (dump_file, "RVUSE_GEN (%d) =", bb->index);
2245 dump_bitmap_of_names (dump_file, RVUSE_GEN (bb));
2247 fprintf (dump_file, "RVUSE_OUT (%d) =", bb->index);
2248 dump_bitmap_of_names (dump_file, RVUSE_OUT (bb));
2252 FOR_EACH_BB (bb)
2254 bitmap_iterator bi;
2256 if (bitmap_empty_p (RVUSE_KILL (bb)))
2257 continue;
2259 FOR_EACH_EXPR_ID_IN_SET (EXP_GEN (bb), i, bi)
2261 tree expr = expression_for_id (i);
2262 if (REFERENCE_CLASS_P (expr))
2264 tree vh = get_value_handle (expr);
2265 tree maybe = bitmap_find_leader (AVAIL_OUT (bb), vh);
2267 if (maybe)
2269 tree def = SSA_NAME_DEF_STMT (maybe);
2271 if (bb_for_stmt (def) != bb)
2272 continue;
2274 if (TREE_CODE (def) == PHI_NODE
2275 || stmt_ann (def)->uid < first_store_uid[bb->index])
2277 if (ANTIC_SAFE_LOADS (bb) == NULL)
2278 ANTIC_SAFE_LOADS (bb) = bitmap_set_new ();
2279 bitmap_value_insert_into_set (ANTIC_SAFE_LOADS (bb),
2280 expr);
2286 free (first_store_uid);
2289 /* Return true if we can value number the call in STMT. This is true
2290 if we have a pure or constant call. */
2292 static bool
2293 can_value_number_call (tree stmt)
2295 tree call = get_call_expr_in (stmt);
2297 if (call_expr_flags (call) & (ECF_PURE | ECF_CONST))
2298 return true;
2299 return false;
2302 /* Return true if OP is a tree which we can perform value numbering
2303 on. */
2305 static bool
2306 can_value_number_operation (tree op)
2308 return UNARY_CLASS_P (op)
2309 || BINARY_CLASS_P (op)
2310 || COMPARISON_CLASS_P (op)
2311 || REFERENCE_CLASS_P (op)
2312 || (TREE_CODE (op) == CALL_EXPR
2313 && can_value_number_call (op));
2317 /* Return true if OP is a tree which we can perform PRE on
2318 on. This may not match the operations we can value number, but in
2319 a perfect world would. */
2321 static bool
2322 can_PRE_operation (tree op)
2324 return UNARY_CLASS_P (op)
2325 || BINARY_CLASS_P (op)
2326 || COMPARISON_CLASS_P (op)
2327 || TREE_CODE (op) == INDIRECT_REF
2328 || TREE_CODE (op) == COMPONENT_REF
2329 || TREE_CODE (op) == CALL_EXPR
2330 || TREE_CODE (op) == ARRAY_REF;
2334 /* Inserted expressions are placed onto this worklist, which is used
2335 for performing quick dead code elimination of insertions we made
2336 that didn't turn out to be necessary. */
2337 static VEC(tree,heap) *inserted_exprs;
2339 /* Pool allocated fake store expressions are placed onto this
2340 worklist, which, after performing dead code elimination, is walked
2341 to see which expressions need to be put into GC'able memory */
2342 static VEC(tree, heap) *need_creation;
2344 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2345 COMPONENT_REF or INDIRECT_REF or ARRAY_REF portion, because we'd end up with
2346 trying to rename aggregates into ssa form directly, which is a no
2349 Thus, this routine doesn't create temporaries, it just builds a
2350 single access expression for the array, calling
2351 find_or_generate_expression to build the innermost pieces.
2353 This function is a subroutine of create_expression_by_pieces, and
2354 should not be called on it's own unless you really know what you
2355 are doing.
2357 static tree
2358 create_component_ref_by_pieces (basic_block block, tree expr, tree stmts)
2360 tree genop = expr;
2361 tree folded;
2363 if (TREE_CODE (genop) == VALUE_HANDLE)
2365 tree found = bitmap_find_leader (AVAIL_OUT (block), expr);
2366 if (found)
2367 return found;
2370 if (TREE_CODE (genop) == VALUE_HANDLE)
2372 bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
2373 unsigned int firstbit = bitmap_first_set_bit (exprset->expressions);
2374 genop = expression_for_id (firstbit);
2377 switch TREE_CODE (genop)
2379 case ARRAY_REF:
2381 tree op0;
2382 tree op1, op2, op3;
2383 op0 = create_component_ref_by_pieces (block,
2384 TREE_OPERAND (genop, 0),
2385 stmts);
2386 op1 = TREE_OPERAND (genop, 1);
2387 if (TREE_CODE (op1) == VALUE_HANDLE)
2388 op1 = find_or_generate_expression (block, op1, stmts);
2389 op2 = TREE_OPERAND (genop, 2);
2390 if (op2 && TREE_CODE (op2) == VALUE_HANDLE)
2391 op2 = find_or_generate_expression (block, op2, stmts);
2392 op3 = TREE_OPERAND (genop, 3);
2393 if (op3 && TREE_CODE (op3) == VALUE_HANDLE)
2394 op3 = find_or_generate_expression (block, op3, stmts);
2395 folded = build4 (ARRAY_REF, TREE_TYPE (genop), op0, op1,
2396 op2, op3);
2397 return folded;
2399 case COMPONENT_REF:
2401 bitmap_set_t exprset;
2402 unsigned int firstbit;
2403 tree op0;
2404 tree op1;
2405 op0 = create_component_ref_by_pieces (block,
2406 TREE_OPERAND (genop, 0),
2407 stmts);
2408 exprset = VALUE_HANDLE_EXPR_SET (TREE_OPERAND (genop, 1));
2409 firstbit = bitmap_first_set_bit (exprset->expressions);
2410 op1 = expression_for_id (firstbit);
2411 folded = fold_build3 (COMPONENT_REF, TREE_TYPE (genop), op0, op1,
2412 NULL_TREE);
2413 return folded;
2415 break;
2416 case INDIRECT_REF:
2418 tree op1 = TREE_OPERAND (genop, 0);
2419 tree genop1 = find_or_generate_expression (block, op1, stmts);
2421 folded = fold_build1 (TREE_CODE (genop), TREE_TYPE (genop),
2422 genop1);
2423 return folded;
2425 break;
2426 case VAR_DECL:
2427 case PARM_DECL:
2428 case RESULT_DECL:
2429 case SSA_NAME:
2430 case STRING_CST:
2431 return genop;
2432 default:
2433 gcc_unreachable ();
2436 return NULL_TREE;
2439 /* Find a leader for an expression, or generate one using
2440 create_expression_by_pieces if it's ANTIC but
2441 complex.
2442 BLOCK is the basic_block we are looking for leaders in.
2443 EXPR is the expression to find a leader or generate for.
2444 STMTS is the statement list to put the inserted expressions on.
2445 Returns the SSA_NAME of the LHS of the generated expression or the
2446 leader. */
2448 static tree
2449 find_or_generate_expression (basic_block block, tree expr, tree stmts)
2451 tree genop = bitmap_find_leader (AVAIL_OUT (block), expr);
2453 /* If it's still NULL, it must be a complex expression, so generate
2454 it recursively. */
2455 if (genop == NULL)
2457 bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
2458 unsigned int firstbit = bitmap_first_set_bit (exprset->expressions);
2460 genop = expression_for_id (firstbit);
2461 gcc_assert (can_PRE_operation (genop));
2462 genop = create_expression_by_pieces (block, genop, stmts);
2464 return genop;
2467 #define NECESSARY(stmt) stmt->base.asm_written_flag
2468 /* Create an expression in pieces, so that we can handle very complex
2469 expressions that may be ANTIC, but not necessary GIMPLE.
2470 BLOCK is the basic block the expression will be inserted into,
2471 EXPR is the expression to insert (in value form)
2472 STMTS is a statement list to append the necessary insertions into.
2474 This function will die if we hit some value that shouldn't be
2475 ANTIC but is (IE there is no leader for it, or its components).
2476 This function may also generate expressions that are themselves
2477 partially or fully redundant. Those that are will be either made
2478 fully redundant during the next iteration of insert (for partially
2479 redundant ones), or eliminated by eliminate (for fully redundant
2480 ones). */
2482 static tree
2483 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
2485 tree temp, name;
2486 tree folded, forced_stmts, newexpr;
2487 tree v;
2488 tree_stmt_iterator tsi;
2490 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2492 case tcc_vl_exp:
2494 tree fn, sc;
2495 tree genfn;
2496 int i, nargs;
2497 tree *buffer;
2499 gcc_assert (TREE_CODE (expr) == CALL_EXPR);
2501 fn = CALL_EXPR_FN (expr);
2502 sc = CALL_EXPR_STATIC_CHAIN (expr);
2504 genfn = find_or_generate_expression (block, fn, stmts);
2506 nargs = call_expr_nargs (expr);
2507 buffer = alloca (nargs * sizeof (tree));
2509 for (i = 0; i < nargs; i++)
2511 tree arg = CALL_EXPR_ARG (expr, i);
2512 buffer[i] = find_or_generate_expression (block, arg, stmts);
2515 folded = build_call_array (TREE_TYPE (expr), genfn, nargs, buffer);
2516 if (sc)
2517 CALL_EXPR_STATIC_CHAIN (folded) =
2518 find_or_generate_expression (block, sc, stmts);
2519 folded = fold (folded);
2520 break;
2522 break;
2523 case tcc_reference:
2525 if (TREE_CODE (expr) == COMPONENT_REF
2526 || TREE_CODE (expr) == ARRAY_REF)
2528 folded = create_component_ref_by_pieces (block, expr, stmts);
2530 else
2532 tree op1 = TREE_OPERAND (expr, 0);
2533 tree genop1 = find_or_generate_expression (block, op1, stmts);
2535 folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
2536 genop1);
2538 break;
2541 case tcc_binary:
2542 case tcc_comparison:
2544 tree op1 = TREE_OPERAND (expr, 0);
2545 tree op2 = TREE_OPERAND (expr, 1);
2546 tree genop1 = find_or_generate_expression (block, op1, stmts);
2547 tree genop2 = find_or_generate_expression (block, op2, stmts);
2548 folded = fold_build2 (TREE_CODE (expr), TREE_TYPE (expr),
2549 genop1, genop2);
2550 break;
2553 case tcc_unary:
2555 tree op1 = TREE_OPERAND (expr, 0);
2556 tree genop1 = find_or_generate_expression (block, op1, stmts);
2557 folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
2558 genop1);
2559 break;
2562 default:
2563 gcc_unreachable ();
2566 /* Force the generated expression to be a sequence of GIMPLE
2567 statements.
2568 We have to call unshare_expr because force_gimple_operand may
2569 modify the tree we pass to it. */
2570 newexpr = force_gimple_operand (unshare_expr (folded), &forced_stmts,
2571 false, NULL);
2573 /* If we have any intermediate expressions to the value sets, add them
2574 to the value sets and chain them on in the instruction stream. */
2575 if (forced_stmts)
2577 tsi = tsi_start (forced_stmts);
2578 for (; !tsi_end_p (tsi); tsi_next (&tsi))
2580 tree stmt = tsi_stmt (tsi);
2581 tree forcedname = GIMPLE_STMT_OPERAND (stmt, 0);
2582 tree forcedexpr = GIMPLE_STMT_OPERAND (stmt, 1);
2583 tree val = vn_lookup_or_add (forcedexpr, NULL);
2585 VEC_safe_push (tree, heap, inserted_exprs, stmt);
2586 vn_add (forcedname, val);
2587 bitmap_value_replace_in_set (NEW_SETS (block), forcedname);
2588 bitmap_value_replace_in_set (AVAIL_OUT (block), forcedname);
2589 mark_symbols_for_renaming (stmt);
2591 tsi = tsi_last (stmts);
2592 tsi_link_after (&tsi, forced_stmts, TSI_CONTINUE_LINKING);
2595 /* Build and insert the assignment of the end result to the temporary
2596 that we will return. */
2597 if (!pretemp || TREE_TYPE (expr) != TREE_TYPE (pretemp))
2599 pretemp = create_tmp_var (TREE_TYPE (expr), "pretmp");
2600 get_var_ann (pretemp);
2603 temp = pretemp;
2604 add_referenced_var (temp);
2606 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE
2607 || TREE_CODE (TREE_TYPE (expr)) == VECTOR_TYPE)
2608 DECL_GIMPLE_REG_P (temp) = 1;
2610 newexpr = build_gimple_modify_stmt (temp, newexpr);
2611 name = make_ssa_name (temp, newexpr);
2612 GIMPLE_STMT_OPERAND (newexpr, 0) = name;
2613 NECESSARY (newexpr) = 0;
2615 tsi = tsi_last (stmts);
2616 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
2617 VEC_safe_push (tree, heap, inserted_exprs, newexpr);
2619 /* All the symbols in NEWEXPR should be put into SSA form. */
2620 mark_symbols_for_renaming (newexpr);
2622 /* Add a value handle to the temporary.
2623 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2624 we are creating the expression by pieces, and this particular piece of
2625 the expression may have been represented. There is no harm in replacing
2626 here. */
2627 v = get_value_handle (expr);
2628 vn_add (name, v);
2629 get_or_alloc_expression_id (name);
2630 bitmap_value_replace_in_set (NEW_SETS (block), name);
2631 bitmap_value_replace_in_set (AVAIL_OUT (block), name);
2633 pre_stats.insertions++;
2634 if (dump_file && (dump_flags & TDF_DETAILS))
2636 fprintf (dump_file, "Inserted ");
2637 print_generic_expr (dump_file, newexpr, 0);
2638 fprintf (dump_file, " in predecessor %d\n", block->index);
2641 return name;
2644 /* Insert the to-be-made-available values of expression EXPRNUM for each
2645 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2646 merge the result with a phi node, given the same value handle as
2647 NODE. Return true if we have inserted new stuff. */
2649 static bool
2650 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
2651 tree *avail)
2653 tree expr = expression_for_id (exprnum);
2654 tree val = get_value_handle (expr);
2655 edge pred;
2656 bool insertions = false;
2657 bool nophi = false;
2658 basic_block bprime;
2659 tree eprime;
2660 edge_iterator ei;
2661 tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
2662 tree temp;
2664 if (dump_file && (dump_flags & TDF_DETAILS))
2666 fprintf (dump_file, "Found partial redundancy for expression ");
2667 print_generic_expr (dump_file, expr, 0);
2668 fprintf (dump_file, " (");
2669 print_generic_expr (dump_file, val, 0);
2670 fprintf (dump_file, ")");
2671 fprintf (dump_file, "\n");
2674 /* Make sure we aren't creating an induction variable. */
2675 if (block->loop_depth > 0 && EDGE_COUNT (block->preds) == 2
2676 && TREE_CODE_CLASS (TREE_CODE (expr)) != tcc_reference )
2678 bool firstinsideloop = false;
2679 bool secondinsideloop = false;
2680 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
2681 EDGE_PRED (block, 0)->src);
2682 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
2683 EDGE_PRED (block, 1)->src);
2684 /* Induction variables only have one edge inside the loop. */
2685 if (firstinsideloop ^ secondinsideloop)
2687 if (dump_file && (dump_flags & TDF_DETAILS))
2688 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
2689 nophi = true;
2694 /* Make the necessary insertions. */
2695 FOR_EACH_EDGE (pred, ei, block->preds)
2697 tree stmts = alloc_stmt_list ();
2698 tree builtexpr;
2699 bprime = pred->src;
2700 eprime = avail[bprime->index];
2702 if (can_PRE_operation (eprime))
2704 #ifdef ENABLE_CHECKING
2705 tree vh;
2707 /* eprime may be an invariant. */
2708 vh = TREE_CODE (eprime) == VALUE_HANDLE
2709 ? eprime
2710 : get_value_handle (eprime);
2712 /* ensure that the virtual uses we need reach our block. */
2713 if (TREE_CODE (vh) == VALUE_HANDLE)
2715 int i;
2716 tree vuse;
2717 for (i = 0;
2718 VEC_iterate (tree, VALUE_HANDLE_VUSES (vh), i, vuse);
2719 i++)
2721 size_t id = SSA_NAME_VERSION (vuse);
2722 gcc_assert (bitmap_bit_p (RVUSE_OUT (bprime), id)
2723 || IS_EMPTY_STMT (SSA_NAME_DEF_STMT (vuse)));
2726 #endif
2727 builtexpr = create_expression_by_pieces (bprime,
2728 eprime,
2729 stmts);
2730 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
2731 bsi_insert_on_edge (pred, stmts);
2732 avail[bprime->index] = builtexpr;
2733 insertions = true;
2736 /* If we didn't want a phi node, and we made insertions, we still have
2737 inserted new stuff, and thus return true. If we didn't want a phi node,
2738 and didn't make insertions, we haven't added anything new, so return
2739 false. */
2740 if (nophi && insertions)
2741 return true;
2742 else if (nophi && !insertions)
2743 return false;
2745 /* Now build a phi for the new variable. */
2746 if (!prephitemp || TREE_TYPE (prephitemp) != type)
2748 prephitemp = create_tmp_var (type, "prephitmp");
2749 get_var_ann (prephitemp);
2752 temp = prephitemp;
2753 add_referenced_var (temp);
2756 if (TREE_CODE (type) == COMPLEX_TYPE
2757 || TREE_CODE (type) == VECTOR_TYPE)
2758 DECL_GIMPLE_REG_P (temp) = 1;
2759 temp = create_phi_node (temp, block);
2761 NECESSARY (temp) = 0;
2762 VEC_safe_push (tree, heap, inserted_exprs, temp);
2763 FOR_EACH_EDGE (pred, ei, block->preds)
2764 add_phi_arg (temp, avail[pred->src->index], pred);
2766 vn_add (PHI_RESULT (temp), val);
2768 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
2769 this insertion, since we test for the existence of this value in PHI_GEN
2770 before proceeding with the partial redundancy checks in insert_aux.
2772 The value may exist in AVAIL_OUT, in particular, it could be represented
2773 by the expression we are trying to eliminate, in which case we want the
2774 replacement to occur. If it's not existing in AVAIL_OUT, we want it
2775 inserted there.
2777 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
2778 this block, because if it did, it would have existed in our dominator's
2779 AVAIL_OUT, and would have been skipped due to the full redundancy check.
2782 bitmap_insert_into_set (PHI_GEN (block),
2783 PHI_RESULT (temp));
2784 bitmap_value_replace_in_set (AVAIL_OUT (block),
2785 PHI_RESULT (temp));
2786 bitmap_insert_into_set (NEW_SETS (block),
2787 PHI_RESULT (temp));
2789 if (dump_file && (dump_flags & TDF_DETAILS))
2791 fprintf (dump_file, "Created phi ");
2792 print_generic_expr (dump_file, temp, 0);
2793 fprintf (dump_file, " in block %d\n", block->index);
2795 pre_stats.phis++;
2796 return true;
2801 /* Perform insertion of partially redundant values.
2802 For BLOCK, do the following:
2803 1. Propagate the NEW_SETS of the dominator into the current block.
2804 If the block has multiple predecessors,
2805 2a. Iterate over the ANTIC expressions for the block to see if
2806 any of them are partially redundant.
2807 2b. If so, insert them into the necessary predecessors to make
2808 the expression fully redundant.
2809 2c. Insert a new PHI merging the values of the predecessors.
2810 2d. Insert the new PHI, and the new expressions, into the
2811 NEW_SETS set.
2812 3. Recursively call ourselves on the dominator children of BLOCK.
2814 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
2815 do_regular_insertion and do_partial_insertion.
2819 static bool
2820 do_regular_insertion (basic_block block, basic_block dom)
2822 bool new_stuff = false;
2823 VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
2824 tree expr;
2825 int i;
2827 for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
2829 if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
2831 tree *avail;
2832 tree val;
2833 bool by_some = false;
2834 bool cant_insert = false;
2835 bool all_same = true;
2836 tree first_s = NULL;
2837 edge pred;
2838 basic_block bprime;
2839 tree eprime = NULL_TREE;
2840 edge_iterator ei;
2842 val = get_value_handle (expr);
2843 if (bitmap_set_contains_value (PHI_GEN (block), val))
2844 continue;
2845 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
2847 if (dump_file && (dump_flags & TDF_DETAILS))
2848 fprintf (dump_file, "Found fully redundant value\n");
2849 continue;
2852 avail = XCNEWVEC (tree, last_basic_block);
2853 FOR_EACH_EDGE (pred, ei, block->preds)
2855 tree vprime;
2856 tree edoubleprime;
2858 /* This can happen in the very weird case
2859 that our fake infinite loop edges have caused a
2860 critical edge to appear. */
2861 if (EDGE_CRITICAL_P (pred))
2863 cant_insert = true;
2864 break;
2866 bprime = pred->src;
2867 eprime = phi_translate (expr, ANTIC_IN (block), NULL,
2868 bprime, block);
2870 /* eprime will generally only be NULL if the
2871 value of the expression, translated
2872 through the PHI for this predecessor, is
2873 undefined. If that is the case, we can't
2874 make the expression fully redundant,
2875 because its value is undefined along a
2876 predecessor path. We can thus break out
2877 early because it doesn't matter what the
2878 rest of the results are. */
2879 if (eprime == NULL)
2881 cant_insert = true;
2882 break;
2885 eprime = fully_constant_expression (eprime);
2886 vprime = get_value_handle (eprime);
2887 gcc_assert (vprime);
2888 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
2889 vprime);
2890 if (edoubleprime == NULL)
2892 avail[bprime->index] = eprime;
2893 all_same = false;
2895 else
2897 avail[bprime->index] = edoubleprime;
2898 by_some = true;
2899 if (first_s == NULL)
2900 first_s = edoubleprime;
2901 else if (!operand_equal_p (first_s, edoubleprime,
2903 all_same = false;
2906 /* If we can insert it, it's not the same value
2907 already existing along every predecessor, and
2908 it's defined by some predecessor, it is
2909 partially redundant. */
2910 if (!cant_insert && !all_same && by_some)
2912 if (insert_into_preds_of_block (block, get_expression_id (expr),
2913 avail))
2914 new_stuff = true;
2916 /* If all edges produce the same value and that value is
2917 an invariant, then the PHI has the same value on all
2918 edges. Note this. */
2919 else if (!cant_insert && all_same && eprime
2920 && is_gimple_min_invariant (eprime)
2921 && !is_gimple_min_invariant (val))
2923 unsigned int j;
2924 bitmap_iterator bi;
2926 bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
2927 FOR_EACH_EXPR_ID_IN_SET (exprset, j, bi)
2929 tree expr = expression_for_id (j);
2930 if (TREE_CODE (expr) == SSA_NAME)
2932 vn_add (expr, eprime);
2933 pre_stats.constified++;
2937 free (avail);
2941 VEC_free (tree, heap, exprs);
2942 return new_stuff;
2946 /* Perform insertion for partially anticipatable expressions. There
2947 is only one case we will perform insertion for these. This case is
2948 if the expression is partially anticipatable, and fully available.
2949 In this case, we know that putting it earlier will enable us to
2950 remove the later computation. */
2953 static bool
2954 do_partial_partial_insertion (basic_block block, basic_block dom)
2956 bool new_stuff = false;
2957 VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (PA_IN (block));
2958 tree expr;
2959 int i;
2961 for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
2963 if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
2965 tree *avail;
2966 tree val;
2967 bool by_all = true;
2968 bool cant_insert = false;
2969 edge pred;
2970 basic_block bprime;
2971 tree eprime = NULL_TREE;
2972 edge_iterator ei;
2974 val = get_value_handle (expr);
2975 if (bitmap_set_contains_value (PHI_GEN (block), val))
2976 continue;
2977 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
2978 continue;
2980 avail = XCNEWVEC (tree, last_basic_block);
2981 FOR_EACH_EDGE (pred, ei, block->preds)
2983 tree vprime;
2984 tree edoubleprime;
2986 /* This can happen in the very weird case
2987 that our fake infinite loop edges have caused a
2988 critical edge to appear. */
2989 if (EDGE_CRITICAL_P (pred))
2991 cant_insert = true;
2992 break;
2994 bprime = pred->src;
2995 eprime = phi_translate (expr, ANTIC_IN (block),
2996 PA_IN (block),
2997 bprime, block);
2999 /* eprime will generally only be NULL if the
3000 value of the expression, translated
3001 through the PHI for this predecessor, is
3002 undefined. If that is the case, we can't
3003 make the expression fully redundant,
3004 because its value is undefined along a
3005 predecessor path. We can thus break out
3006 early because it doesn't matter what the
3007 rest of the results are. */
3008 if (eprime == NULL)
3010 cant_insert = true;
3011 break;
3014 eprime = fully_constant_expression (eprime);
3015 vprime = get_value_handle (eprime);
3016 gcc_assert (vprime);
3017 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3018 vprime);
3019 if (edoubleprime == NULL)
3021 by_all = false;
3022 break;
3024 else
3025 avail[bprime->index] = edoubleprime;
3029 /* If we can insert it, it's not the same value
3030 already existing along every predecessor, and
3031 it's defined by some predecessor, it is
3032 partially redundant. */
3033 if (!cant_insert && by_all)
3035 pre_stats.pa_insert++;
3036 if (insert_into_preds_of_block (block, get_expression_id (expr),
3037 avail))
3038 new_stuff = true;
3040 free (avail);
3044 VEC_free (tree, heap, exprs);
3045 return new_stuff;
3048 static bool
3049 insert_aux (basic_block block)
3051 basic_block son;
3052 bool new_stuff = false;
3054 if (block)
3056 basic_block dom;
3057 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3058 if (dom)
3060 unsigned i;
3061 bitmap_iterator bi;
3062 bitmap_set_t newset = NEW_SETS (dom);
3063 if (newset)
3065 /* Note that we need to value_replace both NEW_SETS, and
3066 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3067 represented by some non-simple expression here that we want
3068 to replace it with. */
3069 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3071 tree expr = expression_for_id (i);
3072 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3073 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3076 if (!single_pred_p (block))
3078 new_stuff |= do_regular_insertion (block, dom);
3079 if (do_partial_partial)
3080 new_stuff |= do_partial_partial_insertion (block, dom);
3084 for (son = first_dom_son (CDI_DOMINATORS, block);
3085 son;
3086 son = next_dom_son (CDI_DOMINATORS, son))
3088 new_stuff |= insert_aux (son);
3091 return new_stuff;
3094 /* Perform insertion of partially redundant values. */
3096 static void
3097 insert (void)
3099 bool new_stuff = true;
3100 basic_block bb;
3101 int num_iterations = 0;
3103 FOR_ALL_BB (bb)
3104 NEW_SETS (bb) = bitmap_set_new ();
3106 while (new_stuff)
3108 num_iterations++;
3109 new_stuff = false;
3110 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
3112 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
3113 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
3117 /* Return true if VAR is an SSA variable with no defining statement in
3118 this procedure, *AND* isn't a live-on-entry parameter. */
3120 static bool
3121 is_undefined_value (tree expr)
3123 return (TREE_CODE (expr) == SSA_NAME
3124 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
3125 /* PARM_DECLs and hard registers are always defined. */
3126 && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL);
3130 /* Given an SSA variable VAR and an expression EXPR, compute the value
3131 number for EXPR and create a value handle (VAL) for it. If VAR and
3132 EXPR are not the same, associate VAL with VAR. Finally, add VAR to
3133 S1 and its value handle to S2, and to the maximal set if
3134 ADD_TO_MAXIMAL is true.
3136 VUSES represent the virtual use operands associated with EXPR (if
3137 any). */
3139 static inline void
3140 add_to_sets (tree var, tree expr, tree stmt, bitmap_set_t s1,
3141 bitmap_set_t s2)
3143 tree val = vn_lookup_or_add (expr, stmt);
3145 /* VAR and EXPR may be the same when processing statements for which
3146 we are not computing value numbers (e.g., non-assignments, or
3147 statements that make aliased stores). In those cases, we are
3148 only interested in making VAR available as its own value. */
3149 if (var != expr)
3150 vn_add (var, val);
3152 if (s1)
3153 bitmap_insert_into_set (s1, var);
3155 /* PHI nodes can't go in the maximal sets because they are not in
3156 TMP_GEN, so it is possible to get into non-monotonic situations
3157 during ANTIC calculation, because it will *add* bits. */
3158 if (!in_fre && TREE_CODE (SSA_NAME_DEF_STMT (var)) != PHI_NODE)
3159 bitmap_value_insert_into_set (maximal_set, var);
3160 bitmap_value_insert_into_set (s2, var);
3163 /* Find existing value expression that is the same as T,
3164 and return it if it exists. */
3166 static inline tree
3167 find_existing_value_expr (tree t, tree stmt)
3169 bitmap_iterator bi;
3170 unsigned int bii;
3171 tree vh;
3172 bitmap_set_t exprset;
3174 if (REFERENCE_CLASS_P (t))
3175 vh = vn_lookup (t, stmt);
3176 else
3177 vh = vn_lookup (t, NULL);
3179 if (!vh)
3180 return NULL;
3181 exprset = VALUE_HANDLE_EXPR_SET (vh);
3182 FOR_EACH_EXPR_ID_IN_SET (exprset, bii, bi)
3184 tree efi = expression_for_id (bii);
3185 if (expressions_equal_p (t, efi))
3186 return efi;
3188 return NULL;
3191 /* Given a unary or binary expression EXPR, create and return a new
3192 expression with the same structure as EXPR but with its operands
3193 replaced with the value handles of each of the operands of EXPR.
3195 VUSES represent the virtual use operands associated with EXPR (if
3196 any). Insert EXPR's operands into the EXP_GEN set for BLOCK. */
3198 static inline tree
3199 create_value_expr_from (tree expr, basic_block block, tree stmt)
3201 int i;
3202 enum tree_code code = TREE_CODE (expr);
3203 tree vexpr;
3204 alloc_pool pool = NULL;
3205 tree efi;
3207 gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
3208 || TREE_CODE_CLASS (code) == tcc_binary
3209 || TREE_CODE_CLASS (code) == tcc_comparison
3210 || TREE_CODE_CLASS (code) == tcc_reference
3211 || TREE_CODE_CLASS (code) == tcc_expression
3212 || TREE_CODE_CLASS (code) == tcc_vl_exp
3213 || TREE_CODE_CLASS (code) == tcc_exceptional
3214 || TREE_CODE_CLASS (code) == tcc_declaration);
3216 if (TREE_CODE_CLASS (code) == tcc_unary)
3217 pool = unary_node_pool;
3218 else if (TREE_CODE_CLASS (code) == tcc_reference)
3219 pool = reference_node_pool;
3220 else if (TREE_CODE_CLASS (code) == tcc_binary)
3221 pool = binary_node_pool;
3222 else if (TREE_CODE_CLASS (code) == tcc_comparison)
3223 pool = comparison_node_pool;
3224 else
3225 gcc_assert (code == CALL_EXPR);
3227 if (code == CALL_EXPR)
3228 vexpr = temp_copy_call_expr (expr);
3229 else
3231 vexpr = (tree) pool_alloc (pool);
3232 memcpy (vexpr, expr, tree_size (expr));
3235 for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
3237 tree val, op;
3239 op = TREE_OPERAND (expr, i);
3240 if (op == NULL_TREE)
3241 continue;
3243 /* Recursively value-numberize reference ops and tree lists. */
3244 if (REFERENCE_CLASS_P (op))
3246 tree tempop = create_value_expr_from (op, block, stmt);
3247 op = tempop ? tempop : op;
3248 val = vn_lookup_or_add (op, stmt);
3250 else
3251 /* Create a value handle for OP and add it to VEXPR. */
3252 val = vn_lookup_or_add (op, NULL);
3254 if (!is_undefined_value (op) && TREE_CODE (op) != TREE_LIST)
3255 bitmap_value_insert_into_set (EXP_GEN (block), op);
3257 if (TREE_CODE (val) == VALUE_HANDLE)
3258 TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
3260 TREE_OPERAND (vexpr, i) = val;
3262 efi = find_existing_value_expr (vexpr, stmt);
3263 if (efi)
3264 return efi;
3265 get_or_alloc_expression_id (vexpr);
3266 return vexpr;
3269 /* Given a statement STMT and its right hand side which is a load, try
3270 to look for the expression stored in the location for the load, and
3271 return true if a useful equivalence was recorded for LHS. */
3273 static bool
3274 try_look_through_load (tree lhs, tree mem_ref, tree stmt, basic_block block)
3276 tree store_stmt = NULL;
3277 tree rhs;
3278 ssa_op_iter i;
3279 tree vuse;
3281 FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, i, SSA_OP_VIRTUAL_USES)
3283 tree def_stmt;
3285 gcc_assert (TREE_CODE (vuse) == SSA_NAME);
3286 def_stmt = SSA_NAME_DEF_STMT (vuse);
3288 /* If there is no useful statement for this VUSE, we'll not find a
3289 useful expression to return either. Likewise, if there is a
3290 statement but it is not a simple assignment or it has virtual
3291 uses, we can stop right here. Note that this means we do
3292 not look through PHI nodes, which is intentional. */
3293 if (!def_stmt
3294 || TREE_CODE (def_stmt) != GIMPLE_MODIFY_STMT
3295 || !ZERO_SSA_OPERANDS (def_stmt, SSA_OP_VIRTUAL_USES))
3296 return false;
3298 /* If this is not the same statement as one we have looked at for
3299 another VUSE of STMT already, we have two statements producing
3300 something that reaches our STMT. */
3301 if (store_stmt && store_stmt != def_stmt)
3302 return false;
3303 else
3305 /* Is this a store to the exact same location as the one we are
3306 loading from in STMT? */
3307 if (!operand_equal_p (GIMPLE_STMT_OPERAND (def_stmt, 0), mem_ref, 0))
3308 return false;
3310 /* Otherwise remember this statement and see if all other VUSEs
3311 come from the same statement. */
3312 store_stmt = def_stmt;
3316 /* Alright then, we have visited all VUSEs of STMT and we've determined
3317 that all of them come from the same statement STORE_STMT. See if there
3318 is a useful expression we can deduce from STORE_STMT. */
3319 rhs = GIMPLE_STMT_OPERAND (store_stmt, 1);
3320 if ((TREE_CODE (rhs) == SSA_NAME
3321 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
3322 || is_gimple_min_invariant (rhs)
3323 || TREE_CODE (rhs) == ADDR_EXPR
3324 || TREE_INVARIANT (rhs))
3327 /* Yay! Compute a value number for the RHS of the statement and
3328 add its value to the AVAIL_OUT set for the block. Add the LHS
3329 to TMP_GEN. */
3330 add_to_sets (lhs, rhs, store_stmt, TMP_GEN (block), AVAIL_OUT (block));
3331 if (TREE_CODE (rhs) == SSA_NAME
3332 && !is_undefined_value (rhs))
3333 bitmap_value_insert_into_set (EXP_GEN (block), rhs);
3334 return true;
3337 return false;
3340 /* Return a copy of NODE that is stored in the temporary alloc_pool's.
3341 This is made recursively true, so that the operands are stored in
3342 the pool as well. */
3344 static tree
3345 poolify_tree (tree node)
3347 switch (TREE_CODE (node))
3349 case INDIRECT_REF:
3351 tree temp = (tree) pool_alloc (reference_node_pool);
3352 memcpy (temp, node, tree_size (node));
3353 TREE_OPERAND (temp, 0) = poolify_tree (TREE_OPERAND (temp, 0));
3354 return temp;
3356 break;
3357 case GIMPLE_MODIFY_STMT:
3359 tree temp = (tree) pool_alloc (modify_expr_node_pool);
3360 memcpy (temp, node, tree_size (node));
3361 GIMPLE_STMT_OPERAND (temp, 0) =
3362 poolify_tree (GIMPLE_STMT_OPERAND (temp, 0));
3363 GIMPLE_STMT_OPERAND (temp, 1) =
3364 poolify_tree (GIMPLE_STMT_OPERAND (temp, 1));
3365 return temp;
3367 break;
3368 case SSA_NAME:
3369 case INTEGER_CST:
3370 case STRING_CST:
3371 case REAL_CST:
3372 case PARM_DECL:
3373 case VAR_DECL:
3374 case RESULT_DECL:
3375 return node;
3376 default:
3377 gcc_unreachable ();
3381 static tree modify_expr_template;
3383 /* Allocate a GIMPLE_MODIFY_STMT with TYPE, and operands OP1, OP2 in the
3384 alloc pools and return it. */
3385 static tree
3386 poolify_modify_stmt (tree op1, tree op2)
3388 if (modify_expr_template == NULL)
3389 modify_expr_template = build_gimple_modify_stmt (op1, op2);
3391 GIMPLE_STMT_OPERAND (modify_expr_template, 0) = op1;
3392 GIMPLE_STMT_OPERAND (modify_expr_template, 1) = op2;
3394 return poolify_tree (modify_expr_template);
3398 /* For each real store operation of the form
3399 *a = <value> that we see, create a corresponding fake store of the
3400 form storetmp_<version> = *a.
3402 This enables AVAIL computation to mark the results of stores as
3403 available. Without this, you'd need to do some computation to
3404 mark the result of stores as ANTIC and AVAIL at all the right
3405 points.
3406 To save memory, we keep the store
3407 statements pool allocated until we decide whether they are
3408 necessary or not. */
3410 static void
3411 insert_fake_stores (void)
3413 basic_block block;
3415 FOR_ALL_BB (block)
3417 block_stmt_iterator bsi;
3418 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
3420 tree stmt = bsi_stmt (bsi);
3422 /* We can't generate SSA names for stores that are complex
3423 or aggregate. We also want to ignore things whose
3424 virtual uses occur in abnormal phis. */
3426 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3427 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == INDIRECT_REF
3428 && !AGGREGATE_TYPE_P (TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 0)))
3429 && TREE_CODE (TREE_TYPE (GIMPLE_STMT_OPERAND
3430 (stmt, 0))) != COMPLEX_TYPE)
3432 ssa_op_iter iter;
3433 def_operand_p defp;
3434 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3435 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3436 tree new;
3437 bool notokay = false;
3439 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_VIRTUAL_DEFS)
3441 tree defvar = DEF_FROM_PTR (defp);
3442 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (defvar))
3444 notokay = true;
3445 break;
3449 if (notokay)
3450 continue;
3452 if (!storetemp || TREE_TYPE (rhs) != TREE_TYPE (storetemp))
3454 storetemp = create_tmp_var (TREE_TYPE (rhs), "storetmp");
3455 if (TREE_CODE (TREE_TYPE (storetemp)) == VECTOR_TYPE)
3456 DECL_GIMPLE_REG_P (storetemp) = 1;
3457 get_var_ann (storetemp);
3460 new = poolify_modify_stmt (storetemp, lhs);
3462 lhs = make_ssa_name (storetemp, new);
3463 GIMPLE_STMT_OPERAND (new, 0) = lhs;
3464 create_ssa_artificial_load_stmt (new, stmt);
3466 NECESSARY (new) = 0;
3467 VEC_safe_push (tree, heap, inserted_exprs, new);
3468 VEC_safe_push (tree, heap, need_creation, new);
3469 bsi_insert_after (&bsi, new, BSI_NEW_STMT);
3475 /* Turn the pool allocated fake stores that we created back into real
3476 GC allocated ones if they turned out to be necessary to PRE some
3477 expressions. */
3479 static void
3480 realify_fake_stores (void)
3482 unsigned int i;
3483 tree stmt;
3485 for (i = 0; VEC_iterate (tree, need_creation, i, stmt); i++)
3487 if (NECESSARY (stmt))
3489 block_stmt_iterator bsi;
3490 tree newstmt, tmp;
3492 /* Mark the temp variable as referenced */
3493 add_referenced_var (SSA_NAME_VAR (GIMPLE_STMT_OPERAND (stmt, 0)));
3495 /* Put the new statement in GC memory, fix up the
3496 SSA_NAME_DEF_STMT on it, and then put it in place of
3497 the old statement before the store in the IR stream
3498 as a plain ssa name copy. */
3499 bsi = bsi_for_stmt (stmt);
3500 bsi_prev (&bsi);
3501 tmp = GIMPLE_STMT_OPERAND (bsi_stmt (bsi), 1);
3502 newstmt = build_gimple_modify_stmt (GIMPLE_STMT_OPERAND (stmt, 0),
3503 tmp);
3504 SSA_NAME_DEF_STMT (GIMPLE_STMT_OPERAND (newstmt, 0)) = newstmt;
3505 bsi_insert_before (&bsi, newstmt, BSI_SAME_STMT);
3506 bsi = bsi_for_stmt (stmt);
3507 bsi_remove (&bsi, true);
3509 else
3510 release_defs (stmt);
3514 /* Tree-combine a value number expression *EXPR_P that does a type
3515 conversion with the value number expression of its operand.
3516 Returns true, if *EXPR_P simplifies to a value number or
3517 gimple min-invariant expression different from EXPR_P and
3518 sets *EXPR_P to the simplified expression value number.
3519 Otherwise returns false and does not change *EXPR_P. */
3521 static bool
3522 try_combine_conversion (tree *expr_p)
3524 tree expr = *expr_p;
3525 tree t;
3526 bitmap_set_t exprset;
3527 unsigned int firstbit;
3529 if (!((TREE_CODE (expr) == NOP_EXPR
3530 || TREE_CODE (expr) == CONVERT_EXPR
3531 || TREE_CODE (expr) == REALPART_EXPR
3532 || TREE_CODE (expr) == IMAGPART_EXPR)
3533 && TREE_CODE (TREE_OPERAND (expr, 0)) == VALUE_HANDLE
3534 && !VALUE_HANDLE_VUSES (TREE_OPERAND (expr, 0))))
3535 return false;
3537 exprset = VALUE_HANDLE_EXPR_SET (TREE_OPERAND (expr, 0));
3538 firstbit = bitmap_first_set_bit (exprset->expressions);
3539 t = fold_unary (TREE_CODE (expr), TREE_TYPE (expr),
3540 expression_for_id (firstbit));
3541 if (!t)
3542 return false;
3544 /* Strip useless type conversions, which is safe in the optimizers but
3545 not generally in fold. */
3546 STRIP_USELESS_TYPE_CONVERSION (t);
3548 /* Disallow value expressions we have no value number for already, as
3549 we would miss a leader for it here. */
3550 if (!(TREE_CODE (t) == VALUE_HANDLE
3551 || is_gimple_min_invariant (t)))
3552 t = vn_lookup (t, NULL);
3554 if (t && t != expr)
3556 *expr_p = t;
3557 return true;
3559 return false;
3562 /* Compute the AVAIL set for all basic blocks.
3564 This function performs value numbering of the statements in each basic
3565 block. The AVAIL sets are built from information we glean while doing
3566 this value numbering, since the AVAIL sets contain only one entry per
3567 value.
3569 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3570 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3572 static void
3573 compute_avail (void)
3575 basic_block block, son;
3576 basic_block *worklist;
3577 size_t sp = 0;
3578 tree param;
3579 /* For arguments with default definitions, we pretend they are
3580 defined in the entry block. */
3581 for (param = DECL_ARGUMENTS (current_function_decl);
3582 param;
3583 param = TREE_CHAIN (param))
3585 if (gimple_default_def (cfun, param) != NULL)
3587 tree def = gimple_default_def (cfun, param);
3589 vn_lookup_or_add (def, NULL);
3590 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
3591 if (!in_fre)
3592 bitmap_value_insert_into_set (maximal_set, def);
3593 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
3597 /* Likewise for the static chain decl. */
3598 if (cfun->static_chain_decl)
3600 param = cfun->static_chain_decl;
3601 if (gimple_default_def (cfun, param) != NULL)
3603 tree def = gimple_default_def (cfun, param);
3605 vn_lookup_or_add (def, NULL);
3606 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
3607 if (!in_fre)
3608 bitmap_value_insert_into_set (maximal_set, def);
3609 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
3613 /* Allocate the worklist. */
3614 worklist = XNEWVEC (basic_block, n_basic_blocks);
3616 /* Seed the algorithm by putting the dominator children of the entry
3617 block on the worklist. */
3618 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR);
3619 son;
3620 son = next_dom_son (CDI_DOMINATORS, son))
3621 worklist[sp++] = son;
3623 /* Loop until the worklist is empty. */
3624 while (sp)
3626 block_stmt_iterator bsi;
3627 tree stmt, phi;
3628 basic_block dom;
3629 unsigned int stmt_uid = 1;
3631 /* Pick a block from the worklist. */
3632 block = worklist[--sp];
3634 /* Initially, the set of available values in BLOCK is that of
3635 its immediate dominator. */
3636 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3637 if (dom)
3638 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3640 /* Generate values for PHI nodes. */
3641 for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
3643 /* We have no need for virtual phis, as they don't represent
3644 actual computations. */
3645 if (is_gimple_reg (PHI_RESULT (phi)))
3647 add_to_sets (PHI_RESULT (phi), PHI_RESULT (phi), NULL,
3648 PHI_GEN (block), AVAIL_OUT (block));
3652 /* Now compute value numbers and populate value sets with all
3653 the expressions computed in BLOCK. */
3654 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
3656 stmt_ann_t ann;
3657 ssa_op_iter iter;
3658 tree op;
3660 stmt = bsi_stmt (bsi);
3661 ann = stmt_ann (stmt);
3663 ann->uid = stmt_uid++;
3665 /* For regular value numbering, we are only interested in
3666 assignments of the form X_i = EXPR, where EXPR represents
3667 an "interesting" computation, it has no volatile operands
3668 and X_i doesn't flow through an abnormal edge. */
3669 if (TREE_CODE (stmt) == RETURN_EXPR
3670 && !ann->has_volatile_ops)
3672 tree realstmt = stmt;
3673 tree lhs;
3674 tree rhs;
3676 stmt = TREE_OPERAND (stmt, 0);
3677 if (stmt && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
3679 lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3680 rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3681 if (TREE_CODE (rhs) == SSA_NAME
3682 && !is_undefined_value (rhs))
3683 bitmap_value_insert_into_set (EXP_GEN (block), rhs);
3685 FOR_EACH_SSA_TREE_OPERAND (op, realstmt, iter, SSA_OP_DEF)
3686 add_to_sets (op, op, NULL, TMP_GEN (block),
3687 AVAIL_OUT (block));
3689 continue;
3692 else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3693 && !ann->has_volatile_ops
3694 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
3695 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI
3696 (GIMPLE_STMT_OPERAND (stmt, 0)))
3698 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3699 tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3701 /* Try to look through loads. */
3702 if (TREE_CODE (lhs) == SSA_NAME
3703 && !ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_USES)
3704 && try_look_through_load (lhs, rhs, stmt, block))
3705 continue;
3707 STRIP_USELESS_TYPE_CONVERSION (rhs);
3708 if (can_value_number_operation (rhs))
3710 /* For value numberable operation, create a
3711 duplicate expression with the operands replaced
3712 with the value handles of the original RHS. */
3713 tree newt = create_value_expr_from (rhs, block, stmt);
3714 if (newt)
3716 /* If we can combine a conversion expression
3717 with the expression for its operand just
3718 record the value number for it. */
3719 if (try_combine_conversion (&newt))
3720 vn_add (lhs, newt);
3721 else
3723 tree val = vn_lookup_or_add (newt, stmt);
3724 vn_add (lhs, val);
3725 if (!in_fre)
3726 bitmap_value_insert_into_set (maximal_set, newt);
3727 bitmap_value_insert_into_set (EXP_GEN (block), newt);
3729 bitmap_insert_into_set (TMP_GEN (block), lhs);
3730 bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
3731 continue;
3734 else if ((TREE_CODE (rhs) == SSA_NAME
3735 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
3736 || is_gimple_min_invariant (rhs)
3737 || TREE_CODE (rhs) == ADDR_EXPR
3738 || TREE_INVARIANT (rhs)
3739 || DECL_P (rhs))
3741 /* Compute a value number for the RHS of the statement
3742 and add its value to the AVAIL_OUT set for the block.
3743 Add the LHS to TMP_GEN. */
3744 add_to_sets (lhs, rhs, stmt, TMP_GEN (block),
3745 AVAIL_OUT (block));
3747 if (TREE_CODE (rhs) == SSA_NAME
3748 && !is_undefined_value (rhs))
3749 bitmap_value_insert_into_set (EXP_GEN (block), rhs);
3750 continue;
3754 /* For any other statement that we don't recognize, simply
3755 make the names generated by the statement available in
3756 AVAIL_OUT and TMP_GEN. */
3757 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3758 add_to_sets (op, op, NULL, TMP_GEN (block), AVAIL_OUT (block));
3760 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3761 add_to_sets (op, op, NULL, NULL , AVAIL_OUT (block));
3764 /* Put the dominator children of BLOCK on the worklist of blocks
3765 to compute available sets for. */
3766 for (son = first_dom_son (CDI_DOMINATORS, block);
3767 son;
3768 son = next_dom_son (CDI_DOMINATORS, son))
3769 worklist[sp++] = son;
3772 free (worklist);
3776 /* Eliminate fully redundant computations. */
3778 static void
3779 eliminate (void)
3781 basic_block b;
3783 FOR_EACH_BB (b)
3785 block_stmt_iterator i;
3787 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
3789 tree stmt = bsi_stmt (i);
3791 /* Lookup the RHS of the expression, see if we have an
3792 available computation for it. If so, replace the RHS with
3793 the available computation. */
3794 if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3795 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
3796 && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) != SSA_NAME
3797 && !is_gimple_min_invariant (GIMPLE_STMT_OPERAND (stmt, 1))
3798 && !stmt_ann (stmt)->has_volatile_ops)
3800 tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3801 tree *rhs_p = &GIMPLE_STMT_OPERAND (stmt, 1);
3802 tree sprime;
3804 sprime = bitmap_find_leader (AVAIL_OUT (b),
3805 vn_lookup (lhs, NULL));
3806 if (sprime
3807 && sprime != lhs
3808 && (TREE_CODE (*rhs_p) != SSA_NAME
3809 || may_propagate_copy (*rhs_p, sprime)))
3811 gcc_assert (sprime != *rhs_p);
3813 if (dump_file && (dump_flags & TDF_DETAILS))
3815 fprintf (dump_file, "Replaced ");
3816 print_generic_expr (dump_file, *rhs_p, 0);
3817 fprintf (dump_file, " with ");
3818 print_generic_expr (dump_file, sprime, 0);
3819 fprintf (dump_file, " in ");
3820 print_generic_stmt (dump_file, stmt, 0);
3823 if (TREE_CODE (sprime) == SSA_NAME)
3824 NECESSARY (SSA_NAME_DEF_STMT (sprime)) = 1;
3825 /* We need to make sure the new and old types actually match,
3826 which may require adding a simple cast, which fold_convert
3827 will do for us. */
3828 if (TREE_CODE (*rhs_p) != SSA_NAME
3829 && !tree_ssa_useless_type_conversion_1 (TREE_TYPE (*rhs_p),
3830 TREE_TYPE (sprime)))
3831 sprime = fold_convert (TREE_TYPE (*rhs_p), sprime);
3833 pre_stats.eliminations++;
3834 propagate_tree_value (rhs_p, sprime);
3835 update_stmt (stmt);
3837 /* If we removed EH side effects from the statement, clean
3838 its EH information. */
3839 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
3841 bitmap_set_bit (need_eh_cleanup,
3842 bb_for_stmt (stmt)->index);
3843 if (dump_file && (dump_flags & TDF_DETAILS))
3844 fprintf (dump_file, " Removed EH side effects.\n");
3852 /* Borrow a bit of tree-ssa-dce.c for the moment.
3853 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
3854 this may be a bit faster, and we may want critical edges kept split. */
3856 /* If OP's defining statement has not already been determined to be necessary,
3857 mark that statement necessary. Return the stmt, if it is newly
3858 necessary. */
3860 static inline tree
3861 mark_operand_necessary (tree op)
3863 tree stmt;
3865 gcc_assert (op);
3867 if (TREE_CODE (op) != SSA_NAME)
3868 return NULL;
3870 stmt = SSA_NAME_DEF_STMT (op);
3871 gcc_assert (stmt);
3873 if (NECESSARY (stmt)
3874 || IS_EMPTY_STMT (stmt))
3875 return NULL;
3877 NECESSARY (stmt) = 1;
3878 return stmt;
3881 /* Because we don't follow exactly the standard PRE algorithm, and decide not
3882 to insert PHI nodes sometimes, and because value numbering of casts isn't
3883 perfect, we sometimes end up inserting dead code. This simple DCE-like
3884 pass removes any insertions we made that weren't actually used. */
3886 static void
3887 remove_dead_inserted_code (void)
3889 VEC(tree,heap) *worklist = NULL;
3890 int i;
3891 tree t;
3893 worklist = VEC_alloc (tree, heap, VEC_length (tree, inserted_exprs));
3894 for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
3896 if (NECESSARY (t))
3897 VEC_quick_push (tree, worklist, t);
3899 while (VEC_length (tree, worklist) > 0)
3901 t = VEC_pop (tree, worklist);
3903 /* PHI nodes are somewhat special in that each PHI alternative has
3904 data and control dependencies. All the statements feeding the
3905 PHI node's arguments are always necessary. */
3906 if (TREE_CODE (t) == PHI_NODE)
3908 int k;
3910 VEC_reserve (tree, heap, worklist, PHI_NUM_ARGS (t));
3911 for (k = 0; k < PHI_NUM_ARGS (t); k++)
3913 tree arg = PHI_ARG_DEF (t, k);
3914 if (TREE_CODE (arg) == SSA_NAME)
3916 arg = mark_operand_necessary (arg);
3917 if (arg)
3918 VEC_quick_push (tree, worklist, arg);
3922 else
3924 /* Propagate through the operands. Examine all the USE, VUSE and
3925 VDEF operands in this statement. Mark all the statements
3926 which feed this statement's uses as necessary. */
3927 ssa_op_iter iter;
3928 tree use;
3930 /* The operands of VDEF expressions are also needed as they
3931 represent potential definitions that may reach this
3932 statement (VDEF operands allow us to follow def-def
3933 links). */
3935 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
3937 tree n = mark_operand_necessary (use);
3938 if (n)
3939 VEC_safe_push (tree, heap, worklist, n);
3944 for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
3946 if (!NECESSARY (t))
3948 block_stmt_iterator bsi;
3950 if (dump_file && (dump_flags & TDF_DETAILS))
3952 fprintf (dump_file, "Removing unnecessary insertion:");
3953 print_generic_stmt (dump_file, t, 0);
3956 if (TREE_CODE (t) == PHI_NODE)
3958 remove_phi_node (t, NULL, true);
3960 else
3962 bsi = bsi_for_stmt (t);
3963 bsi_remove (&bsi, true);
3964 release_defs (t);
3968 VEC_free (tree, heap, worklist);
3971 /* Initialize data structures used by PRE. */
3973 static void
3974 init_pre (bool do_fre)
3976 basic_block bb;
3978 next_expression_id = 0;
3979 expressions = NULL;
3980 in_fre = do_fre;
3982 inserted_exprs = NULL;
3983 need_creation = NULL;
3984 pretemp = NULL_TREE;
3985 storetemp = NULL_TREE;
3986 mergephitemp = NULL_TREE;
3987 prephitemp = NULL_TREE;
3989 vn_init ();
3990 if (!do_fre)
3991 loop_optimizer_init (LOOPS_NORMAL);
3993 connect_infinite_loops_to_exit ();
3994 memset (&pre_stats, 0, sizeof (pre_stats));
3997 postorder = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
3998 post_order_compute (postorder, false);
4000 FOR_ALL_BB (bb)
4001 bb->aux = xcalloc (1, sizeof (struct bb_bitmap_sets));
4003 calculate_dominance_info (CDI_POST_DOMINATORS);
4004 calculate_dominance_info (CDI_DOMINATORS);
4006 bitmap_obstack_initialize (&grand_bitmap_obstack);
4007 phi_translate_table = htab_create (5110, expr_pred_trans_hash,
4008 expr_pred_trans_eq, free);
4009 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
4010 sizeof (struct bitmap_set), 30);
4011 binary_node_pool = create_alloc_pool ("Binary tree nodes",
4012 tree_code_size (PLUS_EXPR), 30);
4013 unary_node_pool = create_alloc_pool ("Unary tree nodes",
4014 tree_code_size (NEGATE_EXPR), 30);
4015 reference_node_pool = create_alloc_pool ("Reference tree nodes",
4016 tree_code_size (ARRAY_REF), 30);
4017 comparison_node_pool = create_alloc_pool ("Comparison tree nodes",
4018 tree_code_size (EQ_EXPR), 30);
4019 modify_expr_node_pool = create_alloc_pool ("GIMPLE_MODIFY_STMT nodes",
4020 tree_code_size (GIMPLE_MODIFY_STMT),
4021 30);
4022 obstack_init (&temp_call_expr_obstack);
4023 modify_expr_template = NULL;
4025 FOR_ALL_BB (bb)
4027 EXP_GEN (bb) = bitmap_set_new ();
4028 PHI_GEN (bb) = bitmap_set_new ();
4029 TMP_GEN (bb) = bitmap_set_new ();
4030 AVAIL_OUT (bb) = bitmap_set_new ();
4032 maximal_set = in_fre ? NULL : bitmap_set_new ();
4034 need_eh_cleanup = BITMAP_ALLOC (NULL);
4038 /* Deallocate data structures used by PRE. */
4040 static void
4041 fini_pre (bool do_fre)
4043 basic_block bb;
4044 unsigned int i;
4046 free (postorder);
4047 VEC_free (tree, heap, inserted_exprs);
4048 VEC_free (tree, heap, need_creation);
4049 bitmap_obstack_release (&grand_bitmap_obstack);
4050 free_alloc_pool (bitmap_set_pool);
4051 free_alloc_pool (binary_node_pool);
4052 free_alloc_pool (reference_node_pool);
4053 free_alloc_pool (unary_node_pool);
4054 free_alloc_pool (comparison_node_pool);
4055 free_alloc_pool (modify_expr_node_pool);
4056 htab_delete (phi_translate_table);
4057 remove_fake_exit_edges ();
4059 FOR_ALL_BB (bb)
4061 free (bb->aux);
4062 bb->aux = NULL;
4065 free_dominance_info (CDI_POST_DOMINATORS);
4066 vn_delete ();
4068 if (!bitmap_empty_p (need_eh_cleanup))
4070 tree_purge_all_dead_eh_edges (need_eh_cleanup);
4071 cleanup_tree_cfg ();
4074 BITMAP_FREE (need_eh_cleanup);
4076 /* Wipe out pointers to VALUE_HANDLEs. In the not terribly distant
4077 future we will want them to be persistent though. */
4078 for (i = 0; i < num_ssa_names; i++)
4080 tree name = ssa_name (i);
4082 if (!name)
4083 continue;
4085 if (SSA_NAME_VALUE (name)
4086 && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
4087 SSA_NAME_VALUE (name) = NULL;
4089 if (!do_fre && current_loops)
4090 loop_optimizer_finalize ();
4093 /* Main entry point to the SSA-PRE pass. DO_FRE is true if the caller
4094 only wants to do full redundancy elimination. */
4096 static void
4097 execute_pre (bool do_fre)
4100 do_partial_partial = optimize > 2;
4101 init_pre (do_fre);
4103 if (!do_fre)
4104 insert_fake_stores ();
4106 /* Collect and value number expressions computed in each basic block. */
4107 compute_avail ();
4109 if (dump_file && (dump_flags & TDF_DETAILS))
4111 basic_block bb;
4113 FOR_ALL_BB (bb)
4115 print_bitmap_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
4116 print_bitmap_set (dump_file, TMP_GEN (bb), "tmp_gen",
4117 bb->index);
4118 print_bitmap_set (dump_file, AVAIL_OUT (bb), "avail_out",
4119 bb->index);
4123 /* Insert can get quite slow on an incredibly large number of basic
4124 blocks due to some quadratic behavior. Until this behavior is
4125 fixed, don't run it when he have an incredibly large number of
4126 bb's. If we aren't going to run insert, there is no point in
4127 computing ANTIC, either, even though it's plenty fast. */
4128 if (!do_fre && n_basic_blocks < 4000)
4130 vuse_names = XCNEWVEC (bitmap, num_ssa_names);
4131 compute_rvuse_and_antic_safe ();
4132 compute_antic ();
4133 insert ();
4134 free (vuse_names);
4137 /* Remove all the redundant expressions. */
4138 eliminate ();
4140 if (dump_file && (dump_flags & TDF_STATS))
4142 fprintf (dump_file, "Insertions: %d\n", pre_stats.insertions);
4143 fprintf (dump_file, "PA inserted: %d\n", pre_stats.pa_insert);
4144 fprintf (dump_file, "New PHIs: %d\n", pre_stats.phis);
4145 fprintf (dump_file, "Eliminated: %d\n", pre_stats.eliminations);
4146 fprintf (dump_file, "Constified: %d\n", pre_stats.constified);
4148 bsi_commit_edge_inserts ();
4150 clear_expression_ids ();
4151 if (!do_fre)
4153 remove_dead_inserted_code ();
4154 realify_fake_stores ();
4157 fini_pre (do_fre);
4160 /* Gate and execute functions for PRE. */
4162 static unsigned int
4163 do_pre (void)
4165 execute_pre (false);
4166 return 0;
4169 static bool
4170 gate_pre (void)
4172 return flag_tree_pre != 0;
4175 struct tree_opt_pass pass_pre =
4177 "pre", /* name */
4178 gate_pre, /* gate */
4179 do_pre, /* execute */
4180 NULL, /* sub */
4181 NULL, /* next */
4182 0, /* static_pass_number */
4183 TV_TREE_PRE, /* tv_id */
4184 PROP_no_crit_edges | PROP_cfg
4185 | PROP_ssa | PROP_alias, /* properties_required */
4186 0, /* properties_provided */
4187 0, /* properties_destroyed */
4188 0, /* todo_flags_start */
4189 TODO_update_ssa_only_virtuals | TODO_dump_func | TODO_ggc_collect
4190 | TODO_verify_ssa, /* todo_flags_finish */
4191 0 /* letter */
4195 /* Gate and execute functions for FRE. */
4197 static unsigned int
4198 execute_fre (void)
4200 execute_pre (true);
4201 return 0;
4204 static bool
4205 gate_fre (void)
4207 return flag_tree_fre != 0;
4210 struct tree_opt_pass pass_fre =
4212 "fre", /* name */
4213 gate_fre, /* gate */
4214 execute_fre, /* execute */
4215 NULL, /* sub */
4216 NULL, /* next */
4217 0, /* static_pass_number */
4218 TV_TREE_FRE, /* tv_id */
4219 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
4220 0, /* properties_provided */
4221 0, /* properties_destroyed */
4222 0, /* todo_flags_start */
4223 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
4224 0 /* letter */