OSDN Git Service

2007-11-05 Nick Clifton <nickc@redhat.com>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-pre.c
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>
6
7 This file is part of GCC.
8
9 GCC is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3, or (at your option)
12 any later version.
13
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.
18
19 You should have received a copy of the GNU General Public License
20 along with GCC; see the file COPYING3.  If not see
21 <http://www.gnu.org/licenses/>.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "ggc.h"
28 #include "tree.h"
29 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-inline.h"
32 #include "tree-flow.h"
33 #include "tree-gimple.h"
34 #include "tree-dump.h"
35 #include "timevar.h"
36 #include "fibheap.h"
37 #include "hashtab.h"
38 #include "tree-iterator.h"
39 #include "real.h"
40 #include "alloc-pool.h"
41 #include "obstack.h"
42 #include "tree-pass.h"
43 #include "flags.h"
44 #include "bitmap.h"
45 #include "langhooks.h"
46 #include "cfgloop.h"
47 #include "tree-ssa-sccvn.h"
48 #include "params.h"
49
50 /* TODO:
51
52    1. Avail sets can be shared by making an avail_find_leader that
53       walks up the dominator tree and looks in those avail sets.
54       This might affect code optimality, it's unclear right now.
55    2. Strength reduction can be performed by anticipating expressions
56       we can repair later on.
57    3. We can do back-substitution or smarter value numbering to catch
58       commutative expressions split up over multiple statements.
59 */
60
61 /* For ease of terminology, "expression node" in the below refers to
62    every expression node but GIMPLE_MODIFY_STMT, because GIMPLE_MODIFY_STMT's
63    represent the actual statement containing the expressions we care about,
64    and we cache the value number by putting it in the expression.  */
65
66 /* Basic algorithm
67
68    First we walk the statements to generate the AVAIL sets, the
69    EXP_GEN sets, and the tmp_gen sets.  EXP_GEN sets represent the
70    generation of values/expressions by a given block.  We use them
71    when computing the ANTIC sets.  The AVAIL sets consist of
72    SSA_NAME's that represent values, so we know what values are
73    available in what blocks.  AVAIL is a forward dataflow problem.  In
74    SSA, values are never killed, so we don't need a kill set, or a
75    fixpoint iteration, in order to calculate the AVAIL sets.  In
76    traditional parlance, AVAIL sets tell us the downsafety of the
77    expressions/values.
78
79    Next, we generate the ANTIC sets.  These sets represent the
80    anticipatable expressions.  ANTIC is a backwards dataflow
81    problem.  An expression is anticipatable in a given block if it could
82    be generated in that block.  This means that if we had to perform
83    an insertion in that block, of the value of that expression, we
84    could.  Calculating the ANTIC sets requires phi translation of
85    expressions, because the flow goes backwards through phis.  We must
86    iterate to a fixpoint of the ANTIC sets, because we have a kill
87    set.  Even in SSA form, values are not live over the entire
88    function, only from their definition point onwards.  So we have to
89    remove values from the ANTIC set once we go past the definition
90    point of the leaders that make them up.
91    compute_antic/compute_antic_aux performs this computation.
92
93    Third, we perform insertions to make partially redundant
94    expressions fully redundant.
95
96    An expression is partially redundant (excluding partial
97    anticipation) if:
98
99    1. It is AVAIL in some, but not all, of the predecessors of a
100       given block.
101    2. It is ANTIC in all the predecessors.
102
103    In order to make it fully redundant, we insert the expression into
104    the predecessors where it is not available, but is ANTIC.
105
106    For the partial anticipation case, we only perform insertion if it
107    is partially anticipated in some block, and fully available in all
108    of the predecessors.
109
110    insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
111    performs these steps.
112
113    Fourth, we eliminate fully redundant expressions.
114    This is a simple statement walk that replaces redundant
115    calculations with the now available values.  */
116
117 /* Representations of value numbers:
118
119    Value numbers are represented using the "value handle" approach.
120    This means that each SSA_NAME (and for other reasons to be
121    disclosed in a moment, expression nodes) has a value handle that
122    can be retrieved through get_value_handle.  This value handle *is*
123    the value number of the SSA_NAME.  You can pointer compare the
124    value handles for equivalence purposes.
125
126    For debugging reasons, the value handle is internally more than
127    just a number, it is a VALUE_HANDLE named "VH.x", where x is a
128    unique number for each value number in use.  This allows
129    expressions with SSA_NAMES replaced by value handles to still be
130    pretty printed in a sane way.  They simply print as "VH.3 *
131    VH.5", etc.
132
133    Expression nodes have value handles associated with them as a
134    cache.  Otherwise, we'd have to look them up again in the hash
135    table This makes significant difference (factor of two or more) on
136    some test cases.  They can be thrown away after the pass is
137    finished.  */
138
139 /* Representation of expressions on value numbers:
140
141    In some portions of this code, you will notice we allocate "fake"
142    analogues to the expression we are value numbering, and replace the
143    operands with the values of the expression.  Since we work on
144    values, and not just names, we canonicalize expressions to value
145    expressions for use in the ANTIC sets, the EXP_GEN set, etc.
146
147    This is theoretically unnecessary, it just saves a bunch of
148    repeated get_value_handle and find_leader calls in the remainder of
149    the code, trading off temporary memory usage for speed.  The tree
150    nodes aren't actually creating more garbage, since they are
151    allocated in a special pools which are thrown away at the end of
152    this pass.
153
154    All of this also means that if you print the EXP_GEN or ANTIC sets,
155    you will see "VH.5 + VH.7" in the set, instead of "a_55 +
156    b_66" or something.  The only thing that actually cares about
157    seeing the value leaders is phi translation, and it needs to be
158    able to find the leader for a value in an arbitrary block, so this
159    "value expression" form is perfect for it (otherwise you'd do
160    get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
161
162
163 /* Representation of sets:
164
165    There are currently two types of sets used, hopefully to be unified soon.
166    The AVAIL sets do not need to be sorted in any particular order,
167    and thus, are simply represented as two bitmaps, one that keeps
168    track of values present in the set, and one that keeps track of
169    expressions present in the set.
170
171    The other sets are represented as doubly linked lists kept in topological
172    order, with an optional supporting bitmap of values present in the
173    set.  The sets represent values, and the elements can be values or
174    expressions.  The elements can appear in different sets, but each
175    element can only appear once in each set.
176
177    Since each node in the set represents a value, we also want to be
178    able to map expression, set pairs to something that tells us
179    whether the value is present is a set.  We use a per-set bitmap for
180    that.  The value handles also point to a linked list of the
181    expressions they represent via a tree annotation.  This is mainly
182    useful only for debugging, since we don't do identity lookups.  */
183
184
185 /* Next global expression id number.  */
186 static unsigned int next_expression_id;
187
188 typedef VEC(tree, gc) *vuse_vec;
189 DEF_VEC_P (vuse_vec);
190 DEF_VEC_ALLOC_P (vuse_vec, heap);
191
192 static VEC(vuse_vec, heap) *expression_vuses;
193
194 /* Mapping from expression to id number we can use in bitmap sets.  */
195 static VEC(tree, heap) *expressions;
196
197 /* Allocate an expression id for EXPR.  */
198
199 static inline unsigned int
200 alloc_expression_id (tree expr)
201 {
202   tree_ann_common_t ann;
203
204   ann = get_tree_common_ann (expr);
205
206   /* Make sure we won't overflow. */
207   gcc_assert (next_expression_id + 1 > next_expression_id);
208
209   ann->aux = XNEW (unsigned int);
210   * ((unsigned int *)ann->aux) = next_expression_id++;
211   VEC_safe_push (tree, heap, expressions, expr);
212   VEC_safe_push (vuse_vec, heap, expression_vuses, NULL);
213   return next_expression_id - 1;
214 }
215
216 /* Return the expression id for tree EXPR.  */
217
218 static inline unsigned int
219 get_expression_id (tree expr)
220 {
221   tree_ann_common_t ann = tree_common_ann (expr);
222   gcc_assert (ann);
223   gcc_assert (ann->aux);
224
225   return  *((unsigned int *)ann->aux);
226 }
227
228 /* Return the existing expression id for EXPR, or create one if one
229    does not exist yet.  */
230
231 static inline unsigned int
232 get_or_alloc_expression_id (tree expr)
233 {
234   tree_ann_common_t ann = tree_common_ann (expr);
235
236   if (ann == NULL || !ann->aux)
237     return alloc_expression_id (expr);
238
239   return get_expression_id (expr);
240 }
241
242 /* Return the expression that has expression id ID */
243
244 static inline tree
245 expression_for_id (unsigned int id)
246 {
247   return VEC_index (tree, expressions, id);
248 }
249
250 /* Return the expression vuses for EXPR, if there are any.  */
251
252 static inline vuse_vec
253 get_expression_vuses (tree expr)
254 {
255   unsigned int expr_id = get_or_alloc_expression_id (expr);
256   return VEC_index (vuse_vec, expression_vuses, expr_id);
257 }
258
259 /* Set the expression vuses for EXPR to VUSES.  */
260
261 static inline void
262 set_expression_vuses (tree expr, vuse_vec vuses)
263 {
264   unsigned int expr_id = get_or_alloc_expression_id (expr);
265   VEC_replace (vuse_vec, expression_vuses, expr_id, vuses);
266 }
267
268
269 /* Free the expression id field in all of our expressions,
270    and then destroy the expressions array.  */
271
272 static void
273 clear_expression_ids (void)
274 {
275   int i;
276   tree expr;
277
278   for (i = 0; VEC_iterate (tree, expressions, i, expr); i++)
279     {
280       free (tree_common_ann (expr)->aux);
281       tree_common_ann (expr)->aux = NULL;
282     }
283   VEC_free (tree, heap, expressions);
284   VEC_free (vuse_vec, heap, expression_vuses);
285 }
286
287 static bool in_fre = false;
288
289 /* An unordered bitmap set.  One bitmap tracks values, the other,
290    expressions.  */
291 typedef struct bitmap_set
292 {
293   bitmap expressions;
294   bitmap values;
295 } *bitmap_set_t;
296
297 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi)            \
298   EXECUTE_IF_SET_IN_BITMAP(set->expressions, 0, id, bi)
299
300 /* Sets that we need to keep track of.  */
301 typedef struct bb_bitmap_sets
302 {
303   /* The EXP_GEN set, which represents expressions/values generated in
304      a basic block.  */
305   bitmap_set_t exp_gen;
306
307   /* The PHI_GEN set, which represents PHI results generated in a
308      basic block.  */
309   bitmap_set_t phi_gen;
310
311   /* The TMP_GEN set, which represents results/temporaries generated
312      in a basic block. IE the LHS of an expression.  */
313   bitmap_set_t tmp_gen;
314
315   /* The AVAIL_OUT set, which represents which values are available in
316      a given basic block.  */
317   bitmap_set_t avail_out;
318
319   /* The ANTIC_IN set, which represents which values are anticipatable
320      in a given basic block.  */
321   bitmap_set_t antic_in;
322
323   /* The PA_IN set, which represents which values are
324      partially anticipatable in a given basic block.  */
325   bitmap_set_t pa_in;
326
327   /* The NEW_SETS set, which is used during insertion to augment the
328      AVAIL_OUT set of blocks with the new insertions performed during
329      the current iteration.  */
330   bitmap_set_t new_sets;
331
332   /* True if we have visited this block during ANTIC calculation.  */
333   unsigned int visited:1;
334
335   /* True we have deferred processing this block during ANTIC
336      calculation until its successor is processed.  */
337   unsigned int deferred : 1;
338 } *bb_value_sets_t;
339
340 #define EXP_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->exp_gen
341 #define PHI_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->phi_gen
342 #define TMP_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->tmp_gen
343 #define AVAIL_OUT(BB)   ((bb_value_sets_t) ((BB)->aux))->avail_out
344 #define ANTIC_IN(BB)    ((bb_value_sets_t) ((BB)->aux))->antic_in
345 #define PA_IN(BB)       ((bb_value_sets_t) ((BB)->aux))->pa_in
346 #define NEW_SETS(BB)    ((bb_value_sets_t) ((BB)->aux))->new_sets
347 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
348 #define BB_DEFERRED(BB) ((bb_value_sets_t) ((BB)->aux))->deferred
349
350 /* Maximal set of values, used to initialize the ANTIC problem, which
351    is an intersection problem.  */
352 static bitmap_set_t maximal_set;
353
354 /* Basic block list in postorder.  */
355 static int *postorder;
356
357 /* This structure is used to keep track of statistics on what
358    optimization PRE was able to perform.  */
359 static struct
360 {
361   /* The number of RHS computations eliminated by PRE.  */
362   int eliminations;
363
364   /* The number of new expressions/temporaries generated by PRE.  */
365   int insertions;
366
367   /* The number of inserts found due to partial anticipation  */
368   int pa_insert;
369
370   /* The number of new PHI nodes added by PRE.  */
371   int phis;
372
373   /* The number of values found constant.  */
374   int constified;
375
376 } pre_stats;
377
378 static bool do_partial_partial;
379 static tree bitmap_find_leader (bitmap_set_t, tree);
380 static void bitmap_value_insert_into_set (bitmap_set_t, tree);
381 static void bitmap_value_replace_in_set (bitmap_set_t, tree);
382 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
383 static bool bitmap_set_contains_value (bitmap_set_t, tree);
384 static void bitmap_insert_into_set (bitmap_set_t, tree);
385 static bitmap_set_t bitmap_set_new (void);
386 static bool is_undefined_value (tree);
387 static tree create_expression_by_pieces (basic_block, tree, tree);
388 static tree find_or_generate_expression (basic_block, tree, tree);
389
390 /* We can add and remove elements and entries to and from sets
391    and hash tables, so we use alloc pools for them.  */
392
393 static alloc_pool bitmap_set_pool;
394 static alloc_pool binary_node_pool;
395 static alloc_pool unary_node_pool;
396 static alloc_pool reference_node_pool;
397 static alloc_pool comparison_node_pool;
398 static alloc_pool modify_expr_node_pool;
399 static bitmap_obstack grand_bitmap_obstack;
400
401 /* We can't use allocation pools to hold temporary CALL_EXPR objects, since
402    they are not of fixed size.  Instead, use an obstack.  */
403
404 static struct obstack temp_call_expr_obstack;
405
406
407 /* To avoid adding 300 temporary variables when we only need one, we
408    only create one temporary variable, on demand, and build ssa names
409    off that.  We do have to change the variable if the types don't
410    match the current variable's type.  */
411 static tree pretemp;
412 static tree storetemp;
413 static tree prephitemp;
414
415 /* Set of blocks with statements that have had its EH information
416    cleaned up.  */
417 static bitmap need_eh_cleanup;
418
419 /* Which expressions have been seen during a given phi translation.  */
420 static bitmap seen_during_translate;
421
422 /* The phi_translate_table caches phi translations for a given
423    expression and predecessor.  */
424
425 static htab_t phi_translate_table;
426
427 /* A three tuple {e, pred, v} used to cache phi translations in the
428    phi_translate_table.  */
429
430 typedef struct expr_pred_trans_d
431 {
432   /* The expression.  */
433   tree e;
434
435   /* The predecessor block along which we translated the expression.  */
436   basic_block pred;
437
438   /* vuses associated with the expression.  */
439   VEC (tree, gc) *vuses;
440
441   /* The value that resulted from the translation.  */
442   tree v;
443
444   /* The hashcode for the expression, pred pair. This is cached for
445      speed reasons.  */
446   hashval_t hashcode;
447 } *expr_pred_trans_t;
448 typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
449
450 /* Return the hash value for a phi translation table entry.  */
451
452 static hashval_t
453 expr_pred_trans_hash (const void *p)
454 {
455   const_expr_pred_trans_t const ve = (const_expr_pred_trans_t) p;
456   return ve->hashcode;
457 }
458
459 /* Return true if two phi translation table entries are the same.
460    P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
461
462 static int
463 expr_pred_trans_eq (const void *p1, const void *p2)
464 {
465   const_expr_pred_trans_t const ve1 = (const_expr_pred_trans_t) p1;
466   const_expr_pred_trans_t const ve2 = (const_expr_pred_trans_t) p2;
467   basic_block b1 = ve1->pred;
468   basic_block b2 = ve2->pred;
469   int i;
470   tree vuse1;
471
472   /* If they are not translations for the same basic block, they can't
473      be equal.  */
474   if (b1 != b2)
475     return false;
476
477
478   /* If they are for the same basic block, determine if the
479      expressions are equal.  */
480   if (!expressions_equal_p (ve1->e, ve2->e))
481     return false;
482
483   /* Make sure the vuses are equivalent.  */
484   if (ve1->vuses == ve2->vuses)
485     return true;
486
487   if (VEC_length (tree, ve1->vuses) != VEC_length (tree, ve2->vuses))
488     return false;
489
490   for (i = 0; VEC_iterate (tree, ve1->vuses, i, vuse1); i++)
491     {
492       if (VEC_index (tree, ve2->vuses, i) != vuse1)
493         return false;
494     }
495
496   return true;
497 }
498
499 /* Search in the phi translation table for the translation of
500    expression E in basic block PRED with vuses VUSES.
501    Return the translated value, if found, NULL otherwise.  */
502
503 static inline tree
504 phi_trans_lookup (tree e, basic_block pred, VEC (tree, gc) *vuses)
505 {
506   void **slot;
507   struct expr_pred_trans_d ept;
508
509   ept.e = e;
510   ept.pred = pred;
511   ept.vuses = vuses;
512   ept.hashcode = iterative_hash_expr (e, (unsigned long) pred);
513   slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
514                                    NO_INSERT);
515   if (!slot)
516     return NULL;
517   else
518     return ((expr_pred_trans_t) *slot)->v;
519 }
520
521
522 /* Add the tuple mapping from {expression E, basic block PRED, vuses VUSES} to
523    value V, to the phi translation table.  */
524
525 static inline void
526 phi_trans_add (tree e, tree v, basic_block pred, VEC (tree, gc) *vuses)
527 {
528   void **slot;
529   expr_pred_trans_t new_pair = XNEW (struct expr_pred_trans_d);
530   new_pair->e = e;
531   new_pair->pred = pred;
532   new_pair->vuses = vuses;
533   new_pair->v = v;
534   new_pair->hashcode = iterative_hash_expr (e, (unsigned long) pred);
535   slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
536                                    new_pair->hashcode, INSERT);
537   if (*slot)
538     free (*slot);
539   *slot = (void *) new_pair;
540 }
541
542
543 /* Return true if V is a value expression that represents itself.
544    In our world, this is *only* non-value handles.  */
545
546 static inline bool
547 constant_expr_p (tree v)
548 {
549   return TREE_CODE (v) != VALUE_HANDLE &&
550     (TREE_CODE (v) == FIELD_DECL || is_gimple_min_invariant (v));
551 }
552
553 /* Add expression E to the expression set of value V.  */
554
555 void
556 add_to_value (tree v, tree e)
557 {
558   /* Constants have no expression sets.  */
559   if (constant_expr_p (v))
560     return;
561
562   if (VALUE_HANDLE_EXPR_SET (v) == NULL)
563     VALUE_HANDLE_EXPR_SET (v) = bitmap_set_new ();
564
565   bitmap_insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
566 }
567
568 /* Create a new bitmap set and return it.  */
569
570 static bitmap_set_t
571 bitmap_set_new (void)
572 {
573   bitmap_set_t ret = (bitmap_set_t) pool_alloc (bitmap_set_pool);
574   ret->expressions = BITMAP_ALLOC (&grand_bitmap_obstack);
575   ret->values = BITMAP_ALLOC (&grand_bitmap_obstack);
576   return ret;
577 }
578
579 /* Remove an expression EXPR from a bitmapped set.  */
580
581 static void
582 bitmap_remove_from_set (bitmap_set_t set, tree expr)
583 {
584   tree val = get_value_handle (expr);
585
586   gcc_assert (val);
587   if (!constant_expr_p (val))
588     {
589       bitmap_clear_bit (set->values, VALUE_HANDLE_ID (val));
590       bitmap_clear_bit (set->expressions, get_expression_id (expr));
591     }
592 }
593
594 /* Insert an expression EXPR into a bitmapped set.  */
595
596 static void
597 bitmap_insert_into_set (bitmap_set_t set, tree expr)
598 {
599   tree val = get_value_handle (expr);
600
601   gcc_assert (val);
602   if (!constant_expr_p (val))
603     {
604       bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
605       bitmap_set_bit (set->expressions, get_or_alloc_expression_id (expr));
606     }
607 }
608
609 /* Copy a bitmapped set ORIG, into bitmapped set DEST.  */
610
611 static void
612 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
613 {
614   bitmap_copy (dest->expressions, orig->expressions);
615   bitmap_copy (dest->values, orig->values);
616 }
617
618
619 /* Free memory used up by SET.  */
620 static void
621 bitmap_set_free (bitmap_set_t set)
622 {
623   BITMAP_FREE (set->expressions);
624   BITMAP_FREE (set->values);
625 }
626
627
628 /* A comparison function for use in qsort to top sort a bitmap set.  Simply
629    subtracts value handle ids, since they are created in topo-order.  */
630
631 static int
632 vh_compare (const void *pa, const void *pb)
633 {
634   const tree vha = get_value_handle (*((const tree *)pa));
635   const tree vhb = get_value_handle (*((const tree *)pb));
636
637   /* This can happen when we constify things.  */
638   if (constant_expr_p (vha))
639     {
640       if (constant_expr_p (vhb))
641         return -1;
642       return -1;
643     }
644   else if (constant_expr_p (vhb))
645     return 1;
646   return VALUE_HANDLE_ID (vha) - VALUE_HANDLE_ID (vhb);
647 }
648
649 /* Generate an topological-ordered array of bitmap set SET.  */
650
651 static VEC(tree, heap) *
652 sorted_array_from_bitmap_set (bitmap_set_t set)
653 {
654   unsigned int i;
655   bitmap_iterator bi;
656   VEC(tree, heap) *result = NULL;
657
658   FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
659     VEC_safe_push (tree, heap, result, expression_for_id (i));
660
661   qsort (VEC_address (tree, result), VEC_length (tree, result),
662          sizeof (tree), vh_compare);
663
664   return result;
665 }
666
667 /* Perform bitmapped set operation DEST &= ORIG.  */
668
669 static void
670 bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig)
671 {
672   bitmap_iterator bi;
673   unsigned int i;
674
675   if (dest != orig)
676     {
677       bitmap temp = BITMAP_ALLOC (&grand_bitmap_obstack);
678
679       bitmap_and_into (dest->values, orig->values);
680       bitmap_copy (temp, dest->expressions);
681       EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
682         {
683           tree expr = expression_for_id (i);
684           tree val = get_value_handle (expr);
685           if (!bitmap_bit_p (dest->values, VALUE_HANDLE_ID (val)))
686             bitmap_clear_bit (dest->expressions, i);
687         }
688       BITMAP_FREE (temp);
689     }
690 }
691
692 /* Subtract all values and expressions contained in ORIG from DEST.  */
693
694 static bitmap_set_t
695 bitmap_set_subtract (bitmap_set_t dest, bitmap_set_t orig)
696 {
697   bitmap_set_t result = bitmap_set_new ();
698   bitmap_iterator bi;
699   unsigned int i;
700
701   bitmap_and_compl (result->expressions, dest->expressions,
702                     orig->expressions);
703
704   FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
705     {
706       tree expr = expression_for_id (i);
707       tree val = get_value_handle (expr);
708       bitmap_set_bit (result->values, VALUE_HANDLE_ID (val));
709     }
710
711   return result;
712 }
713
714 /* Subtract all the values in bitmap set B from bitmap set A.  */
715
716 static void
717 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
718 {
719   unsigned int i;
720   bitmap_iterator bi;
721   bitmap temp = BITMAP_ALLOC (&grand_bitmap_obstack);
722
723   bitmap_copy (temp, a->expressions);
724   EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
725     {
726       tree expr = expression_for_id (i);
727       if (bitmap_set_contains_value (b, get_value_handle (expr)))
728         bitmap_remove_from_set (a, expr);
729     }
730   BITMAP_FREE (temp);
731 }
732
733
734 /* Return true if bitmapped set SET contains the value VAL.  */
735
736 static bool
737 bitmap_set_contains_value (bitmap_set_t set, tree val)
738 {
739   if (constant_expr_p (val))
740     return true;
741
742   if (!set || bitmap_empty_p (set->expressions))
743     return false;
744
745   return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
746 }
747
748 static inline bool
749 bitmap_set_contains_expr (bitmap_set_t set, tree expr)
750 {
751   return bitmap_bit_p (set->expressions, get_expression_id (expr));
752 }
753
754 /* Replace an instance of value LOOKFOR with expression EXPR in SET.  */
755
756 static void
757 bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
758 {
759   bitmap_set_t exprset;
760   unsigned int i;
761   bitmap_iterator bi;
762
763   if (constant_expr_p (lookfor))
764     return;
765
766   if (!bitmap_set_contains_value (set, lookfor))
767     return;
768
769   /* The number of expressions having a given value is usually
770      significantly less than the total number of expressions in SET.
771      Thus, rather than check, for each expression in SET, whether it
772      has the value LOOKFOR, we walk the reverse mapping that tells us
773      what expressions have a given value, and see if any of those
774      expressions are in our set.  For large testcases, this is about
775      5-10x faster than walking the bitmap.  If this is somehow a
776      significant lose for some cases, we can choose which set to walk
777      based on the set size.  */
778   exprset = VALUE_HANDLE_EXPR_SET (lookfor);
779   FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
780     {
781       if (bitmap_bit_p (set->expressions, i))
782         {
783           bitmap_clear_bit (set->expressions, i);
784           bitmap_set_bit (set->expressions, get_expression_id (expr));
785           return;
786         }
787     }
788 }
789
790 /* Return true if two bitmap sets are equal.  */
791
792 static bool
793 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
794 {
795   return bitmap_equal_p (a->values, b->values);
796 }
797
798 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
799    and add it otherwise.  */
800
801 static void
802 bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
803 {
804   tree val = get_value_handle (expr);
805
806   if (bitmap_set_contains_value (set, val))
807     bitmap_set_replace_value (set, val, expr);
808   else
809     bitmap_insert_into_set (set, expr);
810 }
811
812 /* Insert EXPR into SET if EXPR's value is not already present in
813    SET.  */
814
815 static void
816 bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
817 {
818   tree val = get_value_handle (expr);
819
820   if (constant_expr_p (val))
821     return;
822
823   if (!bitmap_set_contains_value (set, val))
824     bitmap_insert_into_set (set, expr);
825 }
826
827 /* Print out SET to OUTFILE.  */
828
829 static void
830 print_bitmap_set (FILE *outfile, bitmap_set_t set,
831                   const char *setname, int blockindex)
832 {
833   fprintf (outfile, "%s[%d] := { ", setname, blockindex);
834   if (set)
835     {
836       bool first = true;
837       unsigned i;
838       bitmap_iterator bi;
839
840       FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
841         {
842           tree expr = expression_for_id (i);
843
844           if (!first)
845             fprintf (outfile, ", ");
846           first = false;
847           print_generic_expr (outfile, expr, 0);
848
849           fprintf (outfile, " (");
850           print_generic_expr (outfile, get_value_handle (expr), 0);
851           fprintf (outfile, ") ");
852         }
853     }
854   fprintf (outfile, " }\n");
855 }
856
857 void debug_bitmap_set (bitmap_set_t);
858
859 void
860 debug_bitmap_set (bitmap_set_t set)
861 {
862   print_bitmap_set (stderr, set, "debug", 0);
863 }
864
865 /* Print out the expressions that have VAL to OUTFILE.  */
866
867 void
868 print_value_expressions (FILE *outfile, tree val)
869 {
870   if (VALUE_HANDLE_EXPR_SET (val))
871     {
872       char s[10];
873       sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
874       print_bitmap_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
875     }
876 }
877
878
879 void
880 debug_value_expressions (tree val)
881 {
882   print_value_expressions (stderr, val);
883 }
884
885 /* Return the folded version of T if T, when folded, is a gimple
886    min_invariant.  Otherwise, return T.  */
887
888 static tree
889 fully_constant_expression (tree t)
890 {
891   tree folded;
892   folded = fold (t);
893   if (folded && is_gimple_min_invariant (folded))
894     return folded;
895   return t;
896 }
897
898 /* Make a temporary copy of a CALL_EXPR object NODE.  */
899
900 static tree
901 temp_copy_call_expr (tree node)
902 {
903   return (tree) obstack_copy (&temp_call_expr_obstack, node, tree_size (node));
904 }
905
906 /* Translate the vuses in the VUSES vector backwards through phi nodes
907    in PHIBLOCK, so that they have the value they would have in
908    BLOCK. */
909
910 static VEC(tree, gc) *
911 translate_vuses_through_block (VEC (tree, gc) *vuses,
912                                basic_block phiblock,
913                                basic_block block)
914 {
915   tree oldvuse;
916   VEC(tree, gc) *result = NULL;
917   int i;
918
919   for (i = 0; VEC_iterate (tree, vuses, i, oldvuse); i++)
920     {
921       tree phi = SSA_NAME_DEF_STMT (oldvuse);
922       if (TREE_CODE (phi) == PHI_NODE
923           && bb_for_stmt (phi) == phiblock)
924         {
925           edge e = find_edge (block, bb_for_stmt (phi));
926           if (e)
927             {
928               tree def = PHI_ARG_DEF (phi, e->dest_idx);
929               if (def != oldvuse)
930                 {
931                   if (!result)
932                     result = VEC_copy (tree, gc, vuses);
933                   VEC_replace (tree, result, i, def);
934                 }
935             }
936         }
937     }
938
939   /* We avoid creating a new copy of the vuses unless something
940      actually changed, so result can be NULL.  */
941   if (result)
942     {
943       sort_vuses (result);
944       return result;
945     }
946   return vuses;
947
948 }
949
950 /* Like find_leader, but checks for the value existing in SET1 *or*
951    SET2.  This is used to avoid making a set consisting of the union
952    of PA_IN and ANTIC_IN during insert.  */
953
954 static inline tree
955 find_leader_in_sets (tree expr, bitmap_set_t set1, bitmap_set_t set2)
956 {
957   tree result;
958
959   result = bitmap_find_leader (set1, expr);
960   if (!result && set2)
961     result = bitmap_find_leader (set2, expr);
962   return result;
963 }
964
965 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
966    the phis in PRED.  SEEN is a bitmap saying which expression we have
967    translated since we started translation of the toplevel expression.
968    Return NULL if we can't find a leader for each part of the
969    translated expression.  */
970
971 static tree
972 phi_translate_1 (tree expr, bitmap_set_t set1, bitmap_set_t set2,
973                  basic_block pred, basic_block phiblock, bitmap seen)
974 {
975   tree phitrans = NULL;
976   tree oldexpr = expr;
977
978   if (expr == NULL)
979     return NULL;
980
981   if (constant_expr_p (expr))
982     return expr;
983
984   /* Phi translations of a given expression don't change.  */
985   if (EXPR_P (expr) || GIMPLE_STMT_P (expr))
986     {
987       phitrans = phi_trans_lookup (expr, pred, get_expression_vuses (expr));
988     }
989   else
990     phitrans = phi_trans_lookup (expr, pred, NULL);
991
992   if (phitrans)
993     return phitrans;
994
995   /* Prevent cycles when we have recursively dependent leaders.  This
996      can only happen when phi translating the maximal set.  */
997   if (seen)
998     {
999       unsigned int expr_id = get_expression_id (expr);
1000       if (bitmap_bit_p (seen, expr_id))
1001         return NULL;
1002       bitmap_set_bit (seen, expr_id);
1003     }
1004
1005   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1006     {
1007     case tcc_expression:
1008       return NULL;
1009
1010     case tcc_vl_exp:
1011       {
1012         if (TREE_CODE (expr) != CALL_EXPR)
1013           return NULL;
1014         else
1015           {
1016             tree oldfn = CALL_EXPR_FN (expr);
1017             tree oldsc = CALL_EXPR_STATIC_CHAIN (expr);
1018             tree newfn, newsc = NULL;
1019             tree newexpr = NULL_TREE;
1020             bool invariantarg = false;
1021             int i, nargs;
1022             VEC (tree, gc) *vuses = get_expression_vuses (expr);
1023             VEC (tree, gc) *tvuses;
1024
1025             newfn = phi_translate_1 (find_leader_in_sets (oldfn, set1, set2),
1026                                      set1, set2, pred, phiblock, seen);
1027             if (newfn == NULL)
1028               return NULL;
1029             if (newfn != oldfn)
1030               {
1031                 newexpr = temp_copy_call_expr (expr);
1032                 CALL_EXPR_FN (newexpr) = get_value_handle (newfn);
1033               }
1034             if (oldsc)
1035               {
1036                 newsc = phi_translate_1 (find_leader_in_sets (oldsc, set1, set2),
1037                                          set1, set2, pred, phiblock, seen);
1038                 if (newsc == NULL)
1039                   return NULL;
1040                 if (newsc != oldsc)
1041                   {
1042                     if (!newexpr)
1043                       newexpr = temp_copy_call_expr (expr);
1044                     CALL_EXPR_STATIC_CHAIN (newexpr) = get_value_handle (newsc);
1045                   }
1046               }
1047
1048             /* phi translate the argument list piece by piece.  */
1049             nargs = call_expr_nargs (expr);
1050             for (i = 0; i < nargs; i++)
1051               {
1052                 tree oldval = CALL_EXPR_ARG (expr, i);
1053                 tree newval;
1054                 if (oldval)
1055                   {
1056                     /* This may seem like a weird place for this
1057                        check, but it's actually the easiest place to
1058                        do it.  We can't do it lower on in the
1059                        recursion because it's valid for pieces of a
1060                        component ref to be of AGGREGATE_TYPE, as long
1061                        as the outermost one is not.
1062                        To avoid *that* case, we have a check for
1063                        AGGREGATE_TYPE_P in insert_aux.  However, that
1064                        check will *not* catch this case because here
1065                        it occurs in the argument list.  */
1066                     if (AGGREGATE_TYPE_P (TREE_TYPE (oldval)))
1067                       return NULL;
1068                     oldval = find_leader_in_sets (oldval, set1, set2);
1069                     newval = phi_translate_1 (oldval, set1, set2, pred,
1070                                             phiblock, seen);
1071                     if (newval == NULL)
1072                       return NULL;
1073                     if (newval != oldval)
1074                       {
1075                         invariantarg |= is_gimple_min_invariant (newval);
1076                         if (!newexpr)
1077                           newexpr = temp_copy_call_expr (expr);
1078                         CALL_EXPR_ARG (newexpr, i) = get_value_handle (newval);
1079                       }
1080                   }
1081               }
1082
1083             /* In case of new invariant args we might try to fold the call
1084                again.  */
1085             if (invariantarg && !newsc)
1086               {
1087                 tree tmp1 = build_call_array (TREE_TYPE (expr),
1088                                               newfn, call_expr_nargs (newexpr),
1089                                               CALL_EXPR_ARGP (newexpr));
1090                 tree tmp2 = fold (tmp1);
1091                 if (tmp2 != tmp1)
1092                   {
1093                     STRIP_TYPE_NOPS (tmp2);
1094                     if (is_gimple_min_invariant (tmp2))
1095                       return tmp2;
1096                   }
1097               }
1098
1099             tvuses = translate_vuses_through_block (vuses, phiblock, pred);
1100             if (vuses != tvuses && ! newexpr)
1101               newexpr = temp_copy_call_expr (expr);
1102
1103             if (newexpr)
1104               {
1105                 newexpr->base.ann = NULL;
1106                 vn_lookup_or_add_with_vuses (newexpr, tvuses);
1107                 expr = newexpr;
1108                 set_expression_vuses (newexpr, tvuses);
1109               }
1110             phi_trans_add (oldexpr, expr, pred, tvuses);
1111           }
1112       }
1113       return expr;
1114
1115     case tcc_declaration:
1116       {
1117         VEC (tree, gc) * oldvuses = NULL;
1118         VEC (tree, gc) * newvuses = NULL;
1119
1120         oldvuses = get_expression_vuses (expr);
1121         if (oldvuses)
1122           newvuses = translate_vuses_through_block (oldvuses, phiblock,
1123                                                     pred);
1124
1125         if (oldvuses != newvuses)
1126           {
1127             vn_lookup_or_add_with_vuses (expr, newvuses);
1128             set_expression_vuses (expr, newvuses);
1129           }
1130         phi_trans_add (oldexpr, expr, pred, newvuses);
1131       }
1132       return expr;
1133
1134     case tcc_reference:
1135       {
1136         tree oldop0 = TREE_OPERAND (expr, 0);
1137         tree oldop1 = NULL;
1138         tree newop0;
1139         tree newop1 = NULL;
1140         tree oldop2 = NULL;
1141         tree newop2 = NULL;
1142         tree oldop3 = NULL;
1143         tree newop3 = NULL;
1144         tree newexpr;
1145         VEC (tree, gc) * oldvuses = NULL;
1146         VEC (tree, gc) * newvuses = NULL;
1147
1148         if (TREE_CODE (expr) != INDIRECT_REF
1149             && TREE_CODE (expr) != COMPONENT_REF
1150             && TREE_CODE (expr) != ARRAY_REF)
1151           return NULL;
1152
1153         oldop0 = find_leader_in_sets (oldop0, set1, set2);
1154         newop0 = phi_translate_1 (oldop0, set1, set2, pred, phiblock, seen);
1155         if (newop0 == NULL)
1156           return NULL;
1157
1158         if (TREE_CODE (expr) == ARRAY_REF)
1159           {
1160             oldop1 = TREE_OPERAND (expr, 1);
1161             oldop1 = find_leader_in_sets (oldop1, set1, set2);
1162             newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
1163
1164             if (newop1 == NULL)
1165               return NULL;
1166
1167             oldop2 = TREE_OPERAND (expr, 2);
1168             if (oldop2)
1169               {
1170                 oldop2 = find_leader_in_sets (oldop2, set1, set2);
1171                 newop2 = phi_translate_1 (oldop2, set1, set2, pred, phiblock, seen);
1172
1173                 if (newop2 == NULL)
1174                   return NULL;
1175               }
1176             oldop3 = TREE_OPERAND (expr, 3);
1177             if (oldop3)
1178               {
1179                 oldop3 = find_leader_in_sets (oldop3, set1, set2);
1180                 newop3 = phi_translate_1 (oldop3, set1, set2, pred, phiblock, seen);
1181
1182                 if (newop3 == NULL)
1183                   return NULL;
1184               }
1185           }
1186
1187         oldvuses = get_expression_vuses (expr);
1188         if (oldvuses)
1189           newvuses = translate_vuses_through_block (oldvuses, phiblock,
1190                                                     pred);
1191
1192         if (newop0 != oldop0 || newvuses != oldvuses
1193             || newop1 != oldop1
1194             || newop2 != oldop2
1195             || newop3 != oldop3)
1196           {
1197             tree t;
1198
1199             newexpr = (tree) pool_alloc (reference_node_pool);
1200             memcpy (newexpr, expr, tree_size (expr));
1201             TREE_OPERAND (newexpr, 0) = get_value_handle (newop0);
1202             if (TREE_CODE (expr) == ARRAY_REF)
1203               {
1204                 TREE_OPERAND (newexpr, 1) = get_value_handle (newop1);
1205                 if (newop2)
1206                   TREE_OPERAND (newexpr, 2) = get_value_handle (newop2);
1207                 if (newop3)
1208                   TREE_OPERAND (newexpr, 3) = get_value_handle (newop3);
1209               }
1210
1211             t = fully_constant_expression (newexpr);
1212
1213             if (t != newexpr)
1214               {
1215                 pool_free (reference_node_pool, newexpr);
1216                 newexpr = t;
1217               }
1218             else
1219               {
1220                 newexpr->base.ann = NULL;
1221                 vn_lookup_or_add_with_vuses (newexpr, newvuses);
1222                 set_expression_vuses (newexpr, newvuses);
1223               }
1224             expr = newexpr;
1225           }
1226         phi_trans_add (oldexpr, expr, pred, newvuses);
1227       }
1228       return expr;
1229       break;
1230
1231     case tcc_binary:
1232     case tcc_comparison:
1233       {
1234         tree oldop1 = TREE_OPERAND (expr, 0);
1235         tree oldval1 = oldop1;
1236         tree oldop2 = TREE_OPERAND (expr, 1);
1237         tree oldval2 = oldop2;
1238         tree newop1;
1239         tree newop2;
1240         tree newexpr;
1241
1242         oldop1 = find_leader_in_sets (oldop1, set1, set2);
1243         newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
1244         if (newop1 == NULL)
1245           return NULL;
1246
1247         oldop2 = find_leader_in_sets (oldop2, set1, set2);
1248         newop2 = phi_translate_1 (oldop2, set1, set2, pred, phiblock, seen);
1249         if (newop2 == NULL)
1250           return NULL;
1251         if (newop1 != oldop1 || newop2 != oldop2)
1252           {
1253             tree t;
1254             newexpr = (tree) pool_alloc (binary_node_pool);
1255             memcpy (newexpr, expr, tree_size (expr));
1256             TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldval1 : get_value_handle (newop1);
1257             TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldval2 : get_value_handle (newop2);
1258             t = fully_constant_expression (newexpr);
1259             if (t != newexpr)
1260               {
1261                 pool_free (binary_node_pool, newexpr);
1262                 newexpr = t;
1263               }
1264             else
1265               {
1266                 newexpr->base.ann = NULL;
1267                 vn_lookup_or_add (newexpr);
1268               }
1269             expr = newexpr;
1270           }
1271         phi_trans_add (oldexpr, expr, pred, NULL);
1272       }
1273       return expr;
1274
1275     case tcc_unary:
1276       {
1277         tree oldop1 = TREE_OPERAND (expr, 0);
1278         tree newop1;
1279         tree newexpr;
1280
1281         oldop1 = find_leader_in_sets (oldop1, set1, set2);
1282         newop1 = phi_translate_1 (oldop1, set1, set2, pred, phiblock, seen);
1283         if (newop1 == NULL)
1284           return NULL;
1285         if (newop1 != oldop1)
1286           {
1287             tree t;
1288             newexpr = (tree) pool_alloc (unary_node_pool);
1289             memcpy (newexpr, expr, tree_size (expr));
1290             TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
1291             t = fully_constant_expression (newexpr);
1292             if (t != newexpr)
1293               {
1294                 pool_free (unary_node_pool, newexpr);
1295                 newexpr = t;
1296               }
1297             else
1298               {
1299                 newexpr->base.ann = NULL;
1300                 vn_lookup_or_add (newexpr);
1301               }
1302             expr = newexpr;
1303           }
1304         phi_trans_add (oldexpr, expr, pred, NULL);
1305       }
1306       return expr;
1307
1308     case tcc_exceptional:
1309       {
1310         tree phi = NULL;
1311         edge e;
1312         tree def_stmt;
1313         gcc_assert (TREE_CODE (expr) == SSA_NAME);
1314
1315         def_stmt = SSA_NAME_DEF_STMT (expr);
1316         if (TREE_CODE (def_stmt) == PHI_NODE
1317             && bb_for_stmt (def_stmt) == phiblock)
1318           phi = def_stmt;
1319         else
1320           return expr;
1321
1322         e = find_edge (pred, bb_for_stmt (phi));
1323         if (e)
1324           {
1325             tree val;
1326             tree def = PHI_ARG_DEF (phi, e->dest_idx);
1327
1328             if (is_gimple_min_invariant (def))
1329               return def;
1330
1331             if (is_undefined_value (def))
1332               return NULL;
1333
1334             val = get_value_handle (def);
1335             gcc_assert (val);
1336             return def;
1337           }
1338       }
1339       return expr;
1340
1341     default:
1342       gcc_unreachable ();
1343     }
1344 }
1345
1346 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1347    the phis in PRED.
1348    Return NULL if we can't find a leader for each part of the
1349    translated expression.  */
1350
1351 static tree
1352 phi_translate (tree expr, bitmap_set_t set1, bitmap_set_t set2,
1353                basic_block pred, basic_block phiblock)
1354 {
1355   bitmap_clear (seen_during_translate);
1356   return phi_translate_1 (expr, set1, set2, pred, phiblock,
1357                           seen_during_translate);
1358 }
1359
1360 /* For each expression in SET, translate the value handles through phi nodes
1361    in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1362    expressions in DEST.  */
1363
1364 static void
1365 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
1366                    basic_block phiblock)
1367 {
1368   VEC (tree, heap) *exprs;
1369   tree expr;
1370   int i;
1371
1372   if (!phi_nodes (phiblock))
1373     {
1374       bitmap_set_copy (dest, set);
1375       return;
1376     }
1377
1378   exprs = sorted_array_from_bitmap_set (set);
1379   for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
1380     {
1381       tree translated;
1382       translated = phi_translate (expr, set, NULL, pred, phiblock);
1383
1384       /* Don't add constants or empty translations to the cache, since
1385          we won't look them up that way, or use the result, anyway.  */
1386       if (translated && !is_gimple_min_invariant (translated))
1387         {
1388           phi_trans_add (expr, translated, pred,
1389                          get_expression_vuses (translated));
1390         }
1391
1392       if (translated != NULL)
1393         bitmap_value_insert_into_set (dest, translated);
1394     }
1395   VEC_free (tree, heap, exprs);
1396 }
1397
1398 /* Find the leader for a value (i.e., the name representing that
1399    value) in a given set, and return it.  Return NULL if no leader is
1400    found.  */
1401
1402 static tree
1403 bitmap_find_leader (bitmap_set_t set, tree val)
1404 {
1405   if (val == NULL)
1406     return NULL;
1407
1408   if (constant_expr_p (val))
1409     return val;
1410
1411   if (bitmap_set_contains_value (set, val))
1412     {
1413       /* Rather than walk the entire bitmap of expressions, and see
1414          whether any of them has the value we are looking for, we look
1415          at the reverse mapping, which tells us the set of expressions
1416          that have a given value (IE value->expressions with that
1417          value) and see if any of those expressions are in our set.
1418          The number of expressions per value is usually significantly
1419          less than the number of expressions in the set.  In fact, for
1420          large testcases, doing it this way is roughly 5-10x faster
1421          than walking the bitmap.
1422          If this is somehow a significant lose for some cases, we can
1423          choose which set to walk based on which set is smaller.  */
1424       unsigned int i;
1425       bitmap_iterator bi;
1426       bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
1427
1428       EXECUTE_IF_AND_IN_BITMAP (exprset->expressions,
1429                                 set->expressions, 0, i, bi)
1430         return expression_for_id (i);
1431     }
1432   return NULL;
1433 }
1434
1435 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1436    BLOCK by seeing if it is not killed in the block.  Note that we are
1437    only determining whether there is a store that kills it.  Because
1438    of the order in which clean iterates over values, we are guaranteed
1439    that altered operands will have caused us to be eliminated from the
1440    ANTIC_IN set already.  */
1441
1442 static bool
1443 value_dies_in_block_x (tree expr, basic_block block)
1444 {
1445   int i;
1446   tree vuse;
1447   VEC (tree, gc) *vuses = get_expression_vuses (expr);
1448
1449   /* Conservatively, a value dies if it's vuses are defined in this
1450      block, unless they come from phi nodes (which are merge operations,
1451      rather than stores.  */
1452   for (i = 0; VEC_iterate (tree, vuses, i, vuse); i++)
1453     {
1454       tree def = SSA_NAME_DEF_STMT (vuse);
1455
1456       if (bb_for_stmt (def) != block)
1457         continue;
1458       if (TREE_CODE (def) == PHI_NODE)
1459         continue;
1460       return true;
1461     }
1462   return false;
1463 }
1464
1465 /* Determine if the expression EXPR is valid in SET1 U SET2.
1466    ONLY SET2 CAN BE NULL.
1467    This means that we have a leader for each part of the expression
1468    (if it consists of values), or the expression is an SSA_NAME.
1469    For loads/calls, we also see if the vuses are killed in this block.
1470
1471    NB: We never should run into a case where we have SSA_NAME +
1472    SSA_NAME or SSA_NAME + value.  The sets valid_in_sets is called on,
1473    the ANTIC sets, will only ever have SSA_NAME's or value expressions
1474    (IE VALUE1 + VALUE2, *VALUE1, VALUE1 < VALUE2)  */
1475
1476 #define union_contains_value(SET1, SET2, VAL)                   \
1477   (bitmap_set_contains_value ((SET1), (VAL))                    \
1478    || ((SET2) && bitmap_set_contains_value ((SET2), (VAL))))
1479
1480 static bool
1481 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree expr,
1482                basic_block block)
1483 {
1484  switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1485     {
1486     case tcc_binary:
1487     case tcc_comparison:
1488       {
1489         tree op1 = TREE_OPERAND (expr, 0);
1490         tree op2 = TREE_OPERAND (expr, 1);
1491
1492         return union_contains_value (set1, set2, op1)
1493           && union_contains_value (set1, set2, op2);
1494       }
1495
1496     case tcc_unary:
1497       {
1498         tree op1 = TREE_OPERAND (expr, 0);
1499         return union_contains_value (set1, set2, op1);
1500       }
1501
1502     case tcc_expression:
1503       return false;
1504
1505     case tcc_vl_exp:
1506       {
1507         if (TREE_CODE (expr) == CALL_EXPR)
1508           {
1509             tree fn = CALL_EXPR_FN (expr);
1510             tree sc = CALL_EXPR_STATIC_CHAIN (expr);
1511             tree arg;
1512             call_expr_arg_iterator iter;
1513
1514             /* Check the non-argument operands first.  */
1515             if (!union_contains_value (set1, set2, fn)
1516                 || (sc && !union_contains_value (set1, set2, sc)))
1517               return false;
1518
1519             /* Now check the operands.  */
1520             FOR_EACH_CALL_EXPR_ARG (arg, iter, expr)
1521               {
1522                 if (!union_contains_value (set1, set2, arg))
1523                   return false;
1524               }
1525             return !value_dies_in_block_x (expr, block);
1526           }
1527         return false;
1528       }
1529
1530     case tcc_reference:
1531       {
1532         if (TREE_CODE (expr) == INDIRECT_REF
1533             || TREE_CODE (expr) == COMPONENT_REF
1534             || TREE_CODE (expr) == ARRAY_REF)
1535           {
1536             tree op0 = TREE_OPERAND (expr, 0);
1537             gcc_assert (is_gimple_min_invariant (op0)
1538                         || TREE_CODE (op0) == VALUE_HANDLE);
1539             if (!union_contains_value (set1, set2, op0))
1540               return false;
1541             if (TREE_CODE (expr) == ARRAY_REF)
1542               {
1543                 tree op1 = TREE_OPERAND (expr, 1);
1544                 tree op2 = TREE_OPERAND (expr, 2);
1545                 tree op3 = TREE_OPERAND (expr, 3);
1546                 gcc_assert (is_gimple_min_invariant (op1)
1547                             || TREE_CODE (op1) == VALUE_HANDLE);
1548                 if (!union_contains_value (set1, set2, op1))
1549                   return false;
1550                 gcc_assert (!op2 || is_gimple_min_invariant (op2)
1551                             || TREE_CODE (op2) == VALUE_HANDLE);
1552                 if (op2
1553                     && !union_contains_value (set1, set2, op2))
1554                   return false;
1555                 gcc_assert (!op3 || is_gimple_min_invariant (op3)
1556                             || TREE_CODE (op3) == VALUE_HANDLE);
1557                 if (op3
1558                     && !union_contains_value (set1, set2, op3))
1559                   return false;
1560             }
1561             return !value_dies_in_block_x (expr, block);
1562           }
1563       }
1564       return false;
1565
1566     case tcc_exceptional:
1567       {
1568         gcc_assert (TREE_CODE (expr) == SSA_NAME);
1569         return bitmap_set_contains_expr (AVAIL_OUT (block), expr);
1570       }
1571
1572     case tcc_declaration:
1573       return !value_dies_in_block_x (expr, block);
1574
1575     default:
1576       /* No other cases should be encountered.  */
1577       gcc_unreachable ();
1578    }
1579 }
1580
1581 /* Clean the set of expressions that are no longer valid in SET1 or
1582    SET2.  This means expressions that are made up of values we have no
1583    leaders for in SET1 or SET2.  This version is used for partial
1584    anticipation, which means it is not valid in either ANTIC_IN or
1585    PA_IN.  */
1586
1587 static void
1588 dependent_clean (bitmap_set_t set1, bitmap_set_t set2, basic_block block)
1589 {
1590   VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set1);
1591   tree expr;
1592   int i;
1593
1594   for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
1595     {
1596       if (!valid_in_sets (set1, set2, expr, block))
1597         bitmap_remove_from_set (set1, expr);
1598     }
1599   VEC_free (tree, heap, exprs);
1600 }
1601
1602 /* Clean the set of expressions that are no longer valid in SET.  This
1603    means expressions that are made up of values we have no leaders for
1604    in SET.  */
1605
1606 static void
1607 clean (bitmap_set_t set, basic_block block)
1608 {
1609   VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (set);
1610   tree expr;
1611   int i;
1612
1613   for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
1614     {
1615       if (!valid_in_sets (set, NULL, expr, block))
1616         bitmap_remove_from_set (set, expr);
1617     }
1618   VEC_free (tree, heap, exprs);
1619 }
1620
1621 static sbitmap has_abnormal_preds;
1622
1623 /* List of blocks that may have changed during ANTIC computation and
1624    thus need to be iterated over.  */
1625
1626 static sbitmap changed_blocks;
1627
1628 /* Decide whether to defer a block for a later iteration, or PHI
1629    translate SOURCE to DEST using phis in PHIBLOCK.  Return false if we
1630    should defer the block, and true if we processed it.  */
1631
1632 static bool
1633 defer_or_phi_translate_block (bitmap_set_t dest, bitmap_set_t source,
1634                               basic_block block, basic_block phiblock)
1635 {
1636   if (!BB_VISITED (phiblock))
1637     {
1638       SET_BIT (changed_blocks, block->index);
1639       BB_VISITED (block) = 0;
1640       BB_DEFERRED (block) = 1;
1641       return false;
1642     }
1643   else
1644     phi_translate_set (dest, source, block, phiblock);
1645   return true;
1646 }
1647
1648 /* Compute the ANTIC set for BLOCK.
1649
1650    If succs(BLOCK) > 1 then
1651      ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
1652    else if succs(BLOCK) == 1 then
1653      ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
1654
1655    ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
1656 */
1657
1658 static bool
1659 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
1660 {
1661   bool changed = false;
1662   bitmap_set_t S, old, ANTIC_OUT;
1663   bitmap_iterator bi;
1664   unsigned int bii;
1665   edge e;
1666   edge_iterator ei;
1667
1668   old = ANTIC_OUT = S = NULL;
1669   BB_VISITED (block) = 1;
1670
1671   /* If any edges from predecessors are abnormal, antic_in is empty,
1672      so do nothing.  */
1673   if (block_has_abnormal_pred_edge)
1674     goto maybe_dump_sets;
1675
1676   old = ANTIC_IN (block);
1677   ANTIC_OUT = bitmap_set_new ();
1678
1679   /* If the block has no successors, ANTIC_OUT is empty.  */
1680   if (EDGE_COUNT (block->succs) == 0)
1681     ;
1682   /* If we have one successor, we could have some phi nodes to
1683      translate through.  */
1684   else if (single_succ_p (block))
1685     {
1686       basic_block succ_bb = single_succ (block);
1687
1688       /* We trade iterations of the dataflow equations for having to
1689          phi translate the maximal set, which is incredibly slow
1690          (since the maximal set often has 300+ members, even when you
1691          have a small number of blocks).
1692          Basically, we defer the computation of ANTIC for this block
1693          until we have processed it's successor, which will inevitably
1694          have a *much* smaller set of values to phi translate once
1695          clean has been run on it.
1696          The cost of doing this is that we technically perform more
1697          iterations, however, they are lower cost iterations.
1698
1699          Timings for PRE on tramp3d-v4:
1700          without maximal set fix: 11 seconds
1701          with maximal set fix/without deferring: 26 seconds
1702          with maximal set fix/with deferring: 11 seconds
1703      */
1704
1705       if (!defer_or_phi_translate_block (ANTIC_OUT, ANTIC_IN (succ_bb),
1706                                         block, succ_bb))
1707         {
1708           changed = true;
1709           goto maybe_dump_sets;
1710         }
1711     }
1712   /* If we have multiple successors, we take the intersection of all of
1713      them.  Note that in the case of loop exit phi nodes, we may have
1714      phis to translate through.  */
1715   else
1716     {
1717       VEC(basic_block, heap) * worklist;
1718       size_t i;
1719       basic_block bprime, first;
1720
1721       worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
1722       FOR_EACH_EDGE (e, ei, block->succs)
1723         VEC_quick_push (basic_block, worklist, e->dest);
1724       first = VEC_index (basic_block, worklist, 0);
1725
1726       if (phi_nodes (first))
1727         {
1728           bitmap_set_t from = ANTIC_IN (first);
1729
1730           if (!BB_VISITED (first))
1731             from = maximal_set;
1732           phi_translate_set (ANTIC_OUT, from, block, first);
1733         }
1734       else
1735         {
1736           if (!BB_VISITED (first))
1737             bitmap_set_copy (ANTIC_OUT, maximal_set);
1738           else
1739             bitmap_set_copy (ANTIC_OUT, ANTIC_IN (first));
1740         }
1741
1742       for (i = 1; VEC_iterate (basic_block, worklist, i, bprime); i++)
1743         {
1744           if (phi_nodes (bprime))
1745             {
1746               bitmap_set_t tmp = bitmap_set_new ();
1747               bitmap_set_t from = ANTIC_IN (bprime);
1748
1749               if (!BB_VISITED (bprime))
1750                 from = maximal_set;
1751               phi_translate_set (tmp, from, block, bprime);
1752               bitmap_set_and (ANTIC_OUT, tmp);
1753               bitmap_set_free (tmp);
1754             }
1755           else
1756             {
1757               if (!BB_VISITED (bprime))
1758                 bitmap_set_and (ANTIC_OUT, maximal_set);
1759               else
1760                 bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime));
1761             }
1762         }
1763       VEC_free (basic_block, heap, worklist);
1764     }
1765
1766   /* Generate ANTIC_OUT - TMP_GEN.  */
1767   S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block));
1768
1769   /* Start ANTIC_IN with EXP_GEN - TMP_GEN.  */
1770   ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block),
1771                                           TMP_GEN (block));
1772
1773   /* Then union in the ANTIC_OUT - TMP_GEN values,
1774      to get ANTIC_OUT U EXP_GEN - TMP_GEN */
1775   FOR_EACH_EXPR_ID_IN_SET (S, bii, bi)
1776     bitmap_value_insert_into_set (ANTIC_IN (block),
1777                                   expression_for_id (bii));
1778
1779   clean (ANTIC_IN (block), block);
1780
1781   /* !old->expressions can happen when we deferred a block.  */
1782   if (!old->expressions || !bitmap_set_equal (old, ANTIC_IN (block)))
1783     {
1784       changed = true;
1785       SET_BIT (changed_blocks, block->index);
1786       FOR_EACH_EDGE (e, ei, block->preds)
1787         SET_BIT (changed_blocks, e->src->index);
1788     }
1789   else
1790     RESET_BIT (changed_blocks, block->index);
1791
1792  maybe_dump_sets:
1793   if (dump_file && (dump_flags & TDF_DETAILS))
1794     {
1795       if (!BB_DEFERRED (block) || BB_VISITED (block))
1796         {
1797           if (ANTIC_OUT)
1798             print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1799
1800           print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
1801                             block->index);
1802
1803           if (S)
1804             print_bitmap_set (dump_file, S, "S", block->index);
1805         }
1806       else
1807         {
1808           fprintf (dump_file,
1809                    "Block %d was deferred for a future iteration.\n",
1810                    block->index);
1811         }
1812     }
1813   if (old)
1814     bitmap_set_free (old);
1815   if (S)
1816     bitmap_set_free (S);
1817   if (ANTIC_OUT)
1818     bitmap_set_free (ANTIC_OUT);
1819   return changed;
1820 }
1821
1822 /* Compute PARTIAL_ANTIC for BLOCK.
1823
1824    If succs(BLOCK) > 1 then
1825      PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
1826      in ANTIC_OUT for all succ(BLOCK)
1827    else if succs(BLOCK) == 1 then
1828      PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
1829
1830    PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
1831                                   - ANTIC_IN[BLOCK])
1832
1833 */
1834 static bool
1835 compute_partial_antic_aux (basic_block block,
1836                            bool block_has_abnormal_pred_edge)
1837 {
1838   bool changed = false;
1839   bitmap_set_t old_PA_IN;
1840   bitmap_set_t PA_OUT;
1841   edge e;
1842   edge_iterator ei;
1843   unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH);
1844
1845   old_PA_IN = PA_OUT = NULL;
1846
1847   /* If any edges from predecessors are abnormal, antic_in is empty,
1848      so do nothing.  */
1849   if (block_has_abnormal_pred_edge)
1850     goto maybe_dump_sets;
1851
1852   /* If there are too many partially anticipatable values in the
1853      block, phi_translate_set can take an exponential time: stop
1854      before the translation starts.  */
1855   if (max_pa
1856       && single_succ_p (block)
1857       && bitmap_count_bits (PA_IN (single_succ (block))->values) > max_pa)
1858     goto maybe_dump_sets;
1859
1860   old_PA_IN = PA_IN (block);
1861   PA_OUT = bitmap_set_new ();
1862
1863   /* If the block has no successors, ANTIC_OUT is empty.  */
1864   if (EDGE_COUNT (block->succs) == 0)
1865     ;
1866   /* If we have one successor, we could have some phi nodes to
1867      translate through.  Note that we can't phi translate across DFS
1868      back edges in partial antic, because it uses a union operation on
1869      the successors.  For recurrences like IV's, we will end up
1870      generating a new value in the set on each go around (i + 3 (VH.1)
1871      VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever.  */
1872   else if (single_succ_p (block))
1873     {
1874       basic_block succ = single_succ (block);
1875       if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK))
1876         phi_translate_set (PA_OUT, PA_IN (succ), block, succ);
1877     }
1878   /* If we have multiple successors, we take the union of all of
1879      them.  */
1880   else
1881     {
1882       VEC(basic_block, heap) * worklist;
1883       size_t i;
1884       basic_block bprime;
1885
1886       worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
1887       FOR_EACH_EDGE (e, ei, block->succs)
1888         {
1889           if (e->flags & EDGE_DFS_BACK)
1890             continue;
1891           VEC_quick_push (basic_block, worklist, e->dest);
1892         }
1893       if (VEC_length (basic_block, worklist) > 0)
1894         {
1895           for (i = 0; VEC_iterate (basic_block, worklist, i, bprime); i++)
1896             {
1897               unsigned int i;
1898               bitmap_iterator bi;
1899
1900               FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi)
1901                 bitmap_value_insert_into_set (PA_OUT,
1902                                               expression_for_id (i));
1903               if (phi_nodes (bprime))
1904                 {
1905                   bitmap_set_t pa_in = bitmap_set_new ();
1906                   phi_translate_set (pa_in, PA_IN (bprime), block, bprime);
1907                   FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
1908                     bitmap_value_insert_into_set (PA_OUT,
1909                                                   expression_for_id (i));
1910                   bitmap_set_free (pa_in);
1911                 }
1912               else
1913                 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi)
1914                   bitmap_value_insert_into_set (PA_OUT,
1915                                                 expression_for_id (i));
1916             }
1917         }
1918       VEC_free (basic_block, heap, worklist);
1919     }
1920
1921   /* PA_IN starts with PA_OUT - TMP_GEN.
1922      Then we subtract things from ANTIC_IN.  */
1923   PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block));
1924
1925   /* For partial antic, we want to put back in the phi results, since
1926      we will properly avoid making them partially antic over backedges.  */
1927   bitmap_ior_into (PA_IN (block)->values, PHI_GEN (block)->values);
1928   bitmap_ior_into (PA_IN (block)->expressions, PHI_GEN (block)->expressions);
1929
1930   /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
1931   bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
1932
1933   dependent_clean (PA_IN (block), ANTIC_IN (block), block);
1934
1935   if (!bitmap_set_equal (old_PA_IN, PA_IN (block)))
1936     {
1937       changed = true;
1938       SET_BIT (changed_blocks, block->index);
1939       FOR_EACH_EDGE (e, ei, block->preds)
1940         SET_BIT (changed_blocks, e->src->index);
1941     }
1942   else
1943     RESET_BIT (changed_blocks, block->index);
1944
1945  maybe_dump_sets:
1946   if (dump_file && (dump_flags & TDF_DETAILS))
1947     {
1948       if (PA_OUT)
1949         print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
1950
1951       print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
1952     }
1953   if (old_PA_IN)
1954     bitmap_set_free (old_PA_IN);
1955   if (PA_OUT)
1956     bitmap_set_free (PA_OUT);
1957   return changed;
1958 }
1959
1960 /* Compute ANTIC and partial ANTIC sets.  */
1961
1962 static void
1963 compute_antic (void)
1964 {
1965   bool changed = true;
1966   int num_iterations = 0;
1967   basic_block block;
1968   int i;
1969
1970   /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
1971      We pre-build the map of blocks with incoming abnormal edges here.  */
1972   has_abnormal_preds = sbitmap_alloc (last_basic_block);
1973   sbitmap_zero (has_abnormal_preds);
1974
1975   FOR_EACH_BB (block)
1976     {
1977       edge_iterator ei;
1978       edge e;
1979
1980       FOR_EACH_EDGE (e, ei, block->preds)
1981         {
1982           e->flags &= ~EDGE_DFS_BACK;
1983           if (e->flags & EDGE_ABNORMAL)
1984             {
1985               SET_BIT (has_abnormal_preds, block->index);
1986               break;
1987             }
1988         }
1989
1990       BB_VISITED (block) = 0;
1991       BB_DEFERRED (block) = 0;
1992       /* While we are here, give empty ANTIC_IN sets to each block.  */
1993       ANTIC_IN (block) = bitmap_set_new ();
1994       PA_IN (block) = bitmap_set_new ();
1995     }
1996
1997   /* At the exit block we anticipate nothing.  */
1998   ANTIC_IN (EXIT_BLOCK_PTR) = bitmap_set_new ();
1999   BB_VISITED (EXIT_BLOCK_PTR) = 1;
2000   PA_IN (EXIT_BLOCK_PTR) = bitmap_set_new ();
2001
2002   changed_blocks = sbitmap_alloc (last_basic_block + 1);
2003   sbitmap_ones (changed_blocks);
2004   while (changed)
2005     {
2006       if (dump_file && (dump_flags & TDF_DETAILS))
2007         fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2008       num_iterations++;
2009       changed = false;
2010       for (i = 0; i < last_basic_block - NUM_FIXED_BLOCKS; i++)
2011         {
2012           if (TEST_BIT (changed_blocks, postorder[i]))
2013             {
2014               basic_block block = BASIC_BLOCK (postorder[i]);
2015               changed |= compute_antic_aux (block,
2016                                             TEST_BIT (has_abnormal_preds,
2017                                                       block->index));
2018             }
2019         }
2020       /* Theoretically possible, but *highly* unlikely.  */
2021       gcc_assert (num_iterations < 50);
2022     }
2023
2024   if (dump_file && (dump_flags & TDF_STATS))
2025     fprintf (dump_file, "compute_antic required %d iterations\n",
2026              num_iterations);
2027
2028   if (do_partial_partial)
2029     {
2030       sbitmap_ones (changed_blocks);
2031       mark_dfs_back_edges ();
2032       num_iterations = 0;
2033       changed = true;
2034       while (changed)
2035         {
2036           if (dump_file && (dump_flags & TDF_DETAILS))
2037             fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2038           num_iterations++;
2039           changed = false;
2040           for (i = 0; i < last_basic_block - NUM_FIXED_BLOCKS; i++)
2041             {
2042               if (TEST_BIT (changed_blocks, postorder[i]))
2043                 {
2044                   basic_block block = BASIC_BLOCK (postorder[i]);
2045                   changed
2046                     |= compute_partial_antic_aux (block,
2047                                                   TEST_BIT (has_abnormal_preds,
2048                                                             block->index));
2049                 }
2050             }
2051           /* Theoretically possible, but *highly* unlikely.  */
2052           gcc_assert (num_iterations < 50);
2053         }
2054       if (dump_file && (dump_flags & TDF_STATS))
2055         fprintf (dump_file, "compute_partial_antic required %d iterations\n",
2056                  num_iterations);
2057     }
2058   sbitmap_free (has_abnormal_preds);
2059   sbitmap_free (changed_blocks);
2060 }
2061
2062 /* Return true if we can value number the call in STMT.  This is true
2063    if we have a pure or constant call.  */
2064
2065 static bool
2066 can_value_number_call (tree stmt)
2067 {
2068   tree call = get_call_expr_in (stmt);
2069
2070   if (call_expr_flags (call)  & (ECF_PURE | ECF_CONST))
2071     return true;
2072   return false;
2073 }
2074
2075 /* Return true if OP is an exception handler related operation, such as
2076    FILTER_EXPR or EXC_PTR_EXPR.  */
2077
2078 static bool
2079 is_exception_related (tree op)
2080 {
2081   return TREE_CODE (op) == FILTER_EXPR || TREE_CODE (op) == EXC_PTR_EXPR;
2082 }
2083
2084 /* Return true if OP is a tree which we can perform value numbering
2085    on.  */
2086
2087 static bool
2088 can_value_number_operation (tree op)
2089 {
2090   return (UNARY_CLASS_P (op)
2091           && !is_exception_related (TREE_OPERAND (op, 0)))
2092     || BINARY_CLASS_P (op)
2093     || COMPARISON_CLASS_P (op)
2094     || REFERENCE_CLASS_P (op)
2095     || (TREE_CODE (op) == CALL_EXPR
2096         && can_value_number_call (op));
2097 }
2098
2099
2100 /* Return true if OP is a tree which we can perform PRE on
2101    on.  This may not match the operations we can value number, but in
2102    a perfect world would.  */
2103
2104 static bool
2105 can_PRE_operation (tree op)
2106 {
2107   return UNARY_CLASS_P (op)
2108     || BINARY_CLASS_P (op)
2109     || COMPARISON_CLASS_P (op)
2110     || TREE_CODE (op) == INDIRECT_REF
2111     || TREE_CODE (op) == COMPONENT_REF
2112     || TREE_CODE (op) == CALL_EXPR
2113     || TREE_CODE (op) == ARRAY_REF;
2114 }
2115
2116
2117 /* Inserted expressions are placed onto this worklist, which is used
2118    for performing quick dead code elimination of insertions we made
2119    that didn't turn out to be necessary.   */
2120 static VEC(tree,heap) *inserted_exprs;
2121
2122 /* Pool allocated fake store expressions are placed onto this
2123    worklist, which, after performing dead code elimination, is walked
2124    to see which expressions need to be put into GC'able memory  */
2125 static VEC(tree, heap) *need_creation;
2126
2127 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2128    COMPONENT_REF or INDIRECT_REF or ARRAY_REF portion, because we'd end up with
2129    trying to rename aggregates into ssa form directly, which is a no
2130    no.
2131
2132    Thus, this routine doesn't create temporaries, it just builds a
2133    single access expression for the array, calling
2134    find_or_generate_expression to build the innermost pieces.
2135
2136    This function is a subroutine of create_expression_by_pieces, and
2137    should not be called on it's own unless you really know what you
2138    are doing.
2139 */
2140 static tree
2141 create_component_ref_by_pieces (basic_block block, tree expr, tree stmts)
2142 {
2143   tree genop = expr;
2144   tree folded;
2145
2146   if (TREE_CODE (genop) == VALUE_HANDLE)
2147     {
2148       tree found = bitmap_find_leader (AVAIL_OUT (block), expr);
2149       if (found)
2150         return found;
2151     }
2152
2153   if (TREE_CODE (genop) == VALUE_HANDLE)
2154     {
2155       bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
2156       unsigned int firstbit = bitmap_first_set_bit (exprset->expressions);
2157       genop = expression_for_id (firstbit);
2158     }
2159
2160   switch TREE_CODE (genop)
2161     {
2162     case ARRAY_REF:
2163       {
2164         tree op0;
2165         tree op1, op2, op3;
2166         op0 = create_component_ref_by_pieces (block,
2167                                               TREE_OPERAND (genop, 0),
2168                                               stmts);
2169         op1 = TREE_OPERAND (genop, 1);
2170         if (TREE_CODE (op1) == VALUE_HANDLE)
2171           op1 = find_or_generate_expression (block, op1, stmts);
2172         op2 = TREE_OPERAND (genop, 2);
2173         if (op2 && TREE_CODE (op2) == VALUE_HANDLE)
2174           op2 = find_or_generate_expression (block, op2, stmts);
2175         op3 = TREE_OPERAND (genop, 3);
2176         if (op3 && TREE_CODE (op3) == VALUE_HANDLE)
2177           op3 = find_or_generate_expression (block, op3, stmts);
2178         folded = build4 (ARRAY_REF, TREE_TYPE (genop), op0, op1,
2179                               op2, op3);
2180         return folded;
2181       }
2182     case COMPONENT_REF:
2183       {
2184         tree op0;
2185         tree op1;
2186         op0 = create_component_ref_by_pieces (block,
2187                                               TREE_OPERAND (genop, 0),
2188                                               stmts);
2189         /* op1 should be a FIELD_DECL, which are represented by
2190            themselves.  */
2191         op1 = TREE_OPERAND (genop, 1);
2192         folded = fold_build3 (COMPONENT_REF, TREE_TYPE (genop), op0, op1,
2193                               NULL_TREE);
2194         return folded;
2195       }
2196       break;
2197     case INDIRECT_REF:
2198       {
2199         tree op1 = TREE_OPERAND (genop, 0);
2200         tree genop1 = find_or_generate_expression (block, op1, stmts);
2201
2202         folded = fold_build1 (TREE_CODE (genop), TREE_TYPE (genop),
2203                               genop1);
2204         return folded;
2205       }
2206       break;
2207     case VAR_DECL:
2208     case PARM_DECL:
2209     case RESULT_DECL:
2210     case SSA_NAME:
2211     case STRING_CST:
2212       return genop;
2213     default:
2214       gcc_unreachable ();
2215     }
2216
2217   return NULL_TREE;
2218 }
2219
2220 /* Find a leader for an expression, or generate one using
2221    create_expression_by_pieces if it's ANTIC but
2222    complex.
2223    BLOCK is the basic_block we are looking for leaders in.
2224    EXPR is the expression to find a leader or generate for.
2225    STMTS is the statement list to put the inserted expressions on.
2226    Returns the SSA_NAME of the LHS of the generated expression or the
2227    leader.  */
2228
2229 static tree
2230 find_or_generate_expression (basic_block block, tree expr, tree stmts)
2231 {
2232   tree genop = bitmap_find_leader (AVAIL_OUT (block), expr);
2233
2234   /* If it's still NULL, it must be a complex expression, so generate
2235      it recursively.  */
2236   if (genop == NULL)
2237     {
2238       bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (expr);
2239       bool handled = false;
2240       bitmap_iterator bi;
2241       unsigned int i;
2242
2243       /* We will hit cases where we have SSA_NAME's in exprset before
2244          other operations, because we may have come up with the SCCVN
2245          value before getting to the RHS of the expression.  */
2246       FOR_EACH_EXPR_ID_IN_SET (exprset, i, bi)
2247         {
2248           genop = expression_for_id (i);
2249           if (can_PRE_operation (genop))
2250             {
2251               handled = true;
2252               genop = create_expression_by_pieces (block, genop, stmts);
2253               break;
2254             }
2255         }
2256       gcc_assert (handled);
2257     }
2258   return genop;
2259 }
2260
2261 #define NECESSARY(stmt)         stmt->base.asm_written_flag
2262 /* Create an expression in pieces, so that we can handle very complex
2263    expressions that may be ANTIC, but not necessary GIMPLE.
2264    BLOCK is the basic block the expression will be inserted into,
2265    EXPR is the expression to insert (in value form)
2266    STMTS is a statement list to append the necessary insertions into.
2267
2268    This function will die if we hit some value that shouldn't be
2269    ANTIC but is (IE there is no leader for it, or its components).
2270    This function may also generate expressions that are themselves
2271    partially or fully redundant.  Those that are will be either made
2272    fully redundant during the next iteration of insert (for partially
2273    redundant ones), or eliminated by eliminate (for fully redundant
2274    ones).  */
2275
2276 static tree
2277 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
2278 {
2279   tree temp, name;
2280   tree folded, forced_stmts, newexpr;
2281   tree v;
2282   tree_stmt_iterator tsi;
2283
2284   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
2285     {
2286     case tcc_vl_exp:
2287       {
2288         tree fn, sc;
2289         tree genfn;
2290         int i, nargs;
2291         tree *buffer;
2292
2293         gcc_assert (TREE_CODE (expr) == CALL_EXPR);
2294
2295         fn = CALL_EXPR_FN (expr);
2296         sc = CALL_EXPR_STATIC_CHAIN (expr);
2297
2298         genfn = find_or_generate_expression (block, fn, stmts);
2299
2300         nargs = call_expr_nargs (expr);
2301         buffer = (tree*) alloca (nargs * sizeof (tree));
2302
2303         for (i = 0; i < nargs; i++)
2304           {
2305             tree arg = CALL_EXPR_ARG (expr, i);
2306             buffer[i] = find_or_generate_expression (block, arg, stmts);
2307           }
2308
2309         folded = build_call_array (TREE_TYPE (expr), genfn, nargs, buffer);
2310         if (sc)
2311           CALL_EXPR_STATIC_CHAIN (folded) =
2312             find_or_generate_expression (block, sc, stmts);
2313         folded = fold (folded);
2314         break;
2315       }
2316       break;
2317     case tcc_reference:
2318       {
2319         if (TREE_CODE (expr) == COMPONENT_REF
2320             || TREE_CODE (expr) == ARRAY_REF)
2321           {
2322             folded = create_component_ref_by_pieces (block, expr, stmts);
2323           }
2324         else
2325           {
2326             tree op1 = TREE_OPERAND (expr, 0);
2327             tree genop1 = find_or_generate_expression (block, op1, stmts);
2328
2329             folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
2330                                   genop1);
2331           }
2332         break;
2333       }
2334
2335     case tcc_binary:
2336     case tcc_comparison:
2337       {
2338         tree op1 = TREE_OPERAND (expr, 0);
2339         tree op2 = TREE_OPERAND (expr, 1);
2340         tree genop1 = find_or_generate_expression (block, op1, stmts);
2341         tree genop2 = find_or_generate_expression (block, op2, stmts);
2342         folded = fold_build2 (TREE_CODE (expr), TREE_TYPE (expr),
2343                               genop1, genop2);
2344         break;
2345       }
2346
2347     case tcc_unary:
2348       {
2349         tree op1 = TREE_OPERAND (expr, 0);
2350         tree genop1 = find_or_generate_expression (block, op1, stmts);
2351         folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
2352                               genop1);
2353         break;
2354       }
2355
2356     default:
2357       gcc_unreachable ();
2358     }
2359
2360   /* Force the generated expression to be a sequence of GIMPLE
2361      statements.
2362      We have to call unshare_expr because force_gimple_operand may
2363      modify the tree we pass to it.  */
2364   newexpr = force_gimple_operand (unshare_expr (folded), &forced_stmts,
2365                                   false, NULL);
2366
2367   /* If we have any intermediate expressions to the value sets, add them
2368      to the value sets and chain them on in the instruction stream.  */
2369   if (forced_stmts)
2370     {
2371       tsi = tsi_start (forced_stmts);
2372       for (; !tsi_end_p (tsi); tsi_next (&tsi))
2373         {
2374           tree stmt = tsi_stmt (tsi);
2375           tree forcedname = GIMPLE_STMT_OPERAND (stmt, 0);
2376           tree forcedexpr = GIMPLE_STMT_OPERAND (stmt, 1);
2377           tree val = vn_lookup_or_add (forcedexpr);
2378
2379           VEC_safe_push (tree, heap, inserted_exprs, stmt);
2380           VN_INFO_GET (forcedname)->valnum = forcedname;
2381           vn_add (forcedname, val);
2382           bitmap_value_replace_in_set (NEW_SETS (block), forcedname);
2383           bitmap_value_replace_in_set (AVAIL_OUT (block), forcedname);
2384           mark_symbols_for_renaming (stmt);
2385         }
2386       tsi = tsi_last (stmts);
2387       tsi_link_after (&tsi, forced_stmts, TSI_CONTINUE_LINKING);
2388     }
2389
2390   /* Build and insert the assignment of the end result to the temporary
2391      that we will return.  */
2392   if (!pretemp || TREE_TYPE (expr) != TREE_TYPE (pretemp))
2393     {
2394       pretemp = create_tmp_var (TREE_TYPE (expr), "pretmp");
2395       get_var_ann (pretemp);
2396     }
2397
2398   temp = pretemp;
2399   add_referenced_var (temp);
2400
2401   if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE
2402       || TREE_CODE (TREE_TYPE (expr)) == VECTOR_TYPE)
2403     DECL_GIMPLE_REG_P (temp) = 1;
2404
2405   newexpr = build_gimple_modify_stmt (temp, newexpr);
2406   name = make_ssa_name (temp, newexpr);
2407   GIMPLE_STMT_OPERAND (newexpr, 0) = name;
2408   NECESSARY (newexpr) = 0;
2409
2410   tsi = tsi_last (stmts);
2411   tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
2412   VEC_safe_push (tree, heap, inserted_exprs, newexpr);
2413
2414   /* All the symbols in NEWEXPR should be put into SSA form.  */
2415   mark_symbols_for_renaming (newexpr);
2416
2417   /* Add a value handle to the temporary.
2418      The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2419      we are creating the expression by pieces, and this particular piece of
2420      the expression may have been represented.  There is no harm in replacing
2421      here.  */
2422   v = get_value_handle (expr);
2423   vn_add (name, v);
2424   VN_INFO_GET (name)->valnum = name;
2425   get_or_alloc_expression_id (name);
2426   bitmap_value_replace_in_set (NEW_SETS (block), name);
2427   bitmap_value_replace_in_set (AVAIL_OUT (block), name);
2428
2429   pre_stats.insertions++;
2430   if (dump_file && (dump_flags & TDF_DETAILS))
2431     {
2432       fprintf (dump_file, "Inserted ");
2433       print_generic_expr (dump_file, newexpr, 0);
2434       fprintf (dump_file, " in predecessor %d\n", block->index);
2435     }
2436
2437   return name;
2438 }
2439
2440 /* Insert the to-be-made-available values of expression EXPRNUM for each
2441    predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2442    merge the result with a phi node, given the same value handle as
2443    NODE.  Return true if we have inserted new stuff.  */
2444
2445 static bool
2446 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
2447                             tree *avail)
2448 {
2449   tree expr = expression_for_id (exprnum);
2450   tree val = get_value_handle (expr);
2451   edge pred;
2452   bool insertions = false;
2453   bool nophi = false;
2454   basic_block bprime;
2455   tree eprime;
2456   edge_iterator ei;
2457   tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
2458   tree temp;
2459
2460   if (dump_file && (dump_flags & TDF_DETAILS))
2461     {
2462       fprintf (dump_file, "Found partial redundancy for expression ");
2463       print_generic_expr (dump_file, expr, 0);
2464       fprintf (dump_file, " (");
2465       print_generic_expr (dump_file, val, 0);
2466       fprintf (dump_file, ")");
2467       fprintf (dump_file, "\n");
2468     }
2469
2470   /* Make sure we aren't creating an induction variable.  */
2471   if (block->loop_depth > 0 && EDGE_COUNT (block->preds) == 2
2472       && TREE_CODE_CLASS (TREE_CODE (expr)) != tcc_reference )
2473     {
2474       bool firstinsideloop = false;
2475       bool secondinsideloop = false;
2476       firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
2477                                                EDGE_PRED (block, 0)->src);
2478       secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
2479                                                 EDGE_PRED (block, 1)->src);
2480       /* Induction variables only have one edge inside the loop.  */
2481       if (firstinsideloop ^ secondinsideloop)
2482         {
2483           if (dump_file && (dump_flags & TDF_DETAILS))
2484             fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
2485           nophi = true;
2486         }
2487     }
2488
2489
2490   /* Make the necessary insertions.  */
2491   FOR_EACH_EDGE (pred, ei, block->preds)
2492     {
2493       tree stmts = alloc_stmt_list ();
2494       tree builtexpr;
2495       bprime = pred->src;
2496       eprime = avail[bprime->index];
2497
2498       if (can_PRE_operation (eprime))
2499         {
2500           builtexpr = create_expression_by_pieces (bprime,
2501                                                    eprime,
2502                                                    stmts);
2503           gcc_assert (!(pred->flags & EDGE_ABNORMAL));
2504           bsi_insert_on_edge (pred, stmts);
2505           avail[bprime->index] = builtexpr;
2506           insertions = true;
2507         }
2508     }
2509   /* If we didn't want a phi node, and we made insertions, we still have
2510      inserted new stuff, and thus return true.  If we didn't want a phi node,
2511      and didn't make insertions, we haven't added anything new, so return
2512      false.  */
2513   if (nophi && insertions)
2514     return true;
2515   else if (nophi && !insertions)
2516     return false;
2517
2518   /* Now build a phi for the new variable.  */
2519   if (!prephitemp || TREE_TYPE (prephitemp) != type)
2520     {
2521       prephitemp = create_tmp_var (type, "prephitmp");
2522       get_var_ann (prephitemp);
2523     }
2524
2525   temp = prephitemp;
2526   add_referenced_var (temp);
2527
2528
2529   if (TREE_CODE (type) == COMPLEX_TYPE
2530       || TREE_CODE (type) == VECTOR_TYPE)
2531     DECL_GIMPLE_REG_P (temp) = 1;
2532   temp = create_phi_node (temp, block);
2533
2534   NECESSARY (temp) = 0;
2535   VN_INFO_GET (PHI_RESULT (temp))->valnum = PHI_RESULT (temp);
2536
2537   VEC_safe_push (tree, heap, inserted_exprs, temp);
2538   FOR_EACH_EDGE (pred, ei, block->preds)
2539     add_phi_arg (temp, avail[pred->src->index], pred);
2540
2541   vn_add (PHI_RESULT (temp), val);
2542
2543   /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
2544      this insertion, since we test for the existence of this value in PHI_GEN
2545      before proceeding with the partial redundancy checks in insert_aux.
2546
2547      The value may exist in AVAIL_OUT, in particular, it could be represented
2548      by the expression we are trying to eliminate, in which case we want the
2549      replacement to occur.  If it's not existing in AVAIL_OUT, we want it
2550      inserted there.
2551
2552      Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
2553      this block, because if it did, it would have existed in our dominator's
2554      AVAIL_OUT, and would have been skipped due to the full redundancy check.
2555   */
2556
2557   bitmap_insert_into_set (PHI_GEN (block),
2558                           PHI_RESULT (temp));
2559   bitmap_value_replace_in_set (AVAIL_OUT (block),
2560                                PHI_RESULT (temp));
2561   bitmap_insert_into_set (NEW_SETS (block),
2562                           PHI_RESULT (temp));
2563
2564   if (dump_file && (dump_flags & TDF_DETAILS))
2565     {
2566       fprintf (dump_file, "Created phi ");
2567       print_generic_expr (dump_file, temp, 0);
2568       fprintf (dump_file, " in block %d\n", block->index);
2569     }
2570   pre_stats.phis++;
2571   return true;
2572 }
2573
2574
2575
2576 /* Perform insertion of partially redundant values.
2577    For BLOCK, do the following:
2578    1.  Propagate the NEW_SETS of the dominator into the current block.
2579    If the block has multiple predecessors,
2580        2a. Iterate over the ANTIC expressions for the block to see if
2581            any of them are partially redundant.
2582        2b. If so, insert them into the necessary predecessors to make
2583            the expression fully redundant.
2584        2c. Insert a new PHI merging the values of the predecessors.
2585        2d. Insert the new PHI, and the new expressions, into the
2586            NEW_SETS set.
2587    3. Recursively call ourselves on the dominator children of BLOCK.
2588
2589    Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
2590    do_regular_insertion and do_partial_insertion.
2591
2592 */
2593
2594 static bool
2595 do_regular_insertion (basic_block block, basic_block dom)
2596 {
2597   bool new_stuff = false;
2598   VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
2599   tree expr;
2600   int i;
2601
2602   for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
2603     {
2604       if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
2605         {
2606           tree *avail;
2607           tree val;
2608           bool by_some = false;
2609           bool cant_insert = false;
2610           bool all_same = true;
2611           tree first_s = NULL;
2612           edge pred;
2613           basic_block bprime;
2614           tree eprime = NULL_TREE;
2615           edge_iterator ei;
2616
2617           val = get_value_handle (expr);
2618           if (bitmap_set_contains_value (PHI_GEN (block), val))
2619             continue;
2620           if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
2621             {
2622               if (dump_file && (dump_flags & TDF_DETAILS))
2623                 fprintf (dump_file, "Found fully redundant value\n");
2624               continue;
2625             }
2626
2627           avail = XCNEWVEC (tree, last_basic_block);
2628           FOR_EACH_EDGE (pred, ei, block->preds)
2629             {
2630               tree vprime;
2631               tree edoubleprime;
2632
2633               /* This can happen in the very weird case
2634                  that our fake infinite loop edges have caused a
2635                  critical edge to appear.  */
2636               if (EDGE_CRITICAL_P (pred))
2637                 {
2638                   cant_insert = true;
2639                   break;
2640                 }
2641               bprime = pred->src;
2642               eprime = phi_translate (expr, ANTIC_IN (block), NULL,
2643                                       bprime, block);
2644
2645               /* eprime will generally only be NULL if the
2646                  value of the expression, translated
2647                  through the PHI for this predecessor, is
2648                  undefined.  If that is the case, we can't
2649                  make the expression fully redundant,
2650                  because its value is undefined along a
2651                  predecessor path.  We can thus break out
2652                  early because it doesn't matter what the
2653                  rest of the results are.  */
2654               if (eprime == NULL)
2655                 {
2656                   cant_insert = true;
2657                   break;
2658                 }
2659
2660               eprime = fully_constant_expression (eprime);
2661               vprime = get_value_handle (eprime);
2662               gcc_assert (vprime);
2663               edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
2664                                                  vprime);
2665               if (edoubleprime == NULL)
2666                 {
2667                   avail[bprime->index] = eprime;
2668                   all_same = false;
2669                 }
2670               else
2671                 {
2672                   avail[bprime->index] = edoubleprime;
2673                   by_some = true;
2674                   if (first_s == NULL)
2675                     first_s = edoubleprime;
2676                   else if (!operand_equal_p (first_s, edoubleprime,
2677                                              0))
2678                     all_same = false;
2679                 }
2680             }
2681           /* If we can insert it, it's not the same value
2682              already existing along every predecessor, and
2683              it's defined by some predecessor, it is
2684              partially redundant.  */
2685           if (!cant_insert && !all_same && by_some)
2686             {
2687               if (insert_into_preds_of_block (block, get_expression_id (expr),
2688                                               avail))
2689                 new_stuff = true;
2690             }
2691           /* If all edges produce the same value and that value is
2692              an invariant, then the PHI has the same value on all
2693              edges.  Note this.  */
2694           else if (!cant_insert && all_same && eprime
2695                    && is_gimple_min_invariant (eprime)
2696                    && !is_gimple_min_invariant (val))
2697             {
2698               unsigned int j;
2699               bitmap_iterator bi;
2700
2701               bitmap_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
2702               FOR_EACH_EXPR_ID_IN_SET (exprset, j, bi)
2703                 {
2704                   tree expr = expression_for_id (j);
2705                   if (TREE_CODE (expr) == SSA_NAME)
2706                     {
2707                       vn_add (expr, eprime);
2708                       pre_stats.constified++;
2709                     }
2710                 }
2711             }
2712           free (avail);
2713         }
2714     }
2715
2716   VEC_free (tree, heap, exprs);
2717   return new_stuff;
2718 }
2719
2720
2721 /* Perform insertion for partially anticipatable expressions.  There
2722    is only one case we will perform insertion for these.  This case is
2723    if the expression is partially anticipatable, and fully available.
2724    In this case, we know that putting it earlier will enable us to
2725    remove the later computation.  */
2726
2727
2728 static bool
2729 do_partial_partial_insertion (basic_block block, basic_block dom)
2730 {
2731   bool new_stuff = false;
2732   VEC (tree, heap) *exprs = sorted_array_from_bitmap_set (PA_IN (block));
2733   tree expr;
2734   int i;
2735
2736   for (i = 0; VEC_iterate (tree, exprs, i, expr); i++)
2737     {
2738       if (can_PRE_operation (expr) && !AGGREGATE_TYPE_P (TREE_TYPE (expr)))
2739         {
2740           tree *avail;
2741           tree val;
2742           bool by_all = true;
2743           bool cant_insert = false;
2744           edge pred;
2745           basic_block bprime;
2746           tree eprime = NULL_TREE;
2747           edge_iterator ei;
2748
2749           val = get_value_handle (expr);
2750           if (bitmap_set_contains_value (PHI_GEN (block), val))
2751             continue;
2752           if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
2753             continue;
2754
2755           avail = XCNEWVEC (tree, last_basic_block);
2756           FOR_EACH_EDGE (pred, ei, block->preds)
2757             {
2758               tree vprime;
2759               tree edoubleprime;
2760
2761               /* This can happen in the very weird case
2762                  that our fake infinite loop edges have caused a
2763                  critical edge to appear.  */
2764               if (EDGE_CRITICAL_P (pred))
2765                 {
2766                   cant_insert = true;
2767                   break;
2768                 }
2769               bprime = pred->src;
2770               eprime = phi_translate (expr, ANTIC_IN (block),
2771                                       PA_IN (block),
2772                                       bprime, block);
2773
2774               /* eprime will generally only be NULL if the
2775                  value of the expression, translated
2776                  through the PHI for this predecessor, is
2777                  undefined.  If that is the case, we can't
2778                  make the expression fully redundant,
2779                  because its value is undefined along a
2780                  predecessor path.  We can thus break out
2781                  early because it doesn't matter what the
2782                  rest of the results are.  */
2783               if (eprime == NULL)
2784                 {
2785                   cant_insert = true;
2786                   break;
2787                 }
2788
2789               eprime = fully_constant_expression (eprime);
2790               vprime = get_value_handle (eprime);
2791               gcc_assert (vprime);
2792               edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
2793                                                  vprime);
2794               if (edoubleprime == NULL)
2795                 {
2796                   by_all = false;
2797                   break;
2798                 }
2799               else
2800                 avail[bprime->index] = edoubleprime;
2801
2802             }
2803
2804           /* If we can insert it, it's not the same value
2805              already existing along every predecessor, and
2806              it's defined by some predecessor, it is
2807              partially redundant.  */
2808           if (!cant_insert && by_all)
2809             {
2810               pre_stats.pa_insert++;
2811               if (insert_into_preds_of_block (block, get_expression_id (expr),
2812                                               avail))
2813                 new_stuff = true;
2814             }
2815           free (avail);
2816         }
2817     }
2818
2819   VEC_free (tree, heap, exprs);
2820   return new_stuff;
2821 }
2822
2823 static bool
2824 insert_aux (basic_block block)
2825 {
2826   basic_block son;
2827   bool new_stuff = false;
2828
2829   if (block)
2830     {
2831       basic_block dom;
2832       dom = get_immediate_dominator (CDI_DOMINATORS, block);
2833       if (dom)
2834         {
2835           unsigned i;
2836           bitmap_iterator bi;
2837           bitmap_set_t newset = NEW_SETS (dom);
2838           if (newset)
2839             {
2840               /* Note that we need to value_replace both NEW_SETS, and
2841                  AVAIL_OUT. For both the case of NEW_SETS, the value may be
2842                  represented by some non-simple expression here that we want
2843                  to replace it with.  */
2844               FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
2845                 {
2846                   tree expr = expression_for_id (i);
2847                   bitmap_value_replace_in_set (NEW_SETS (block), expr);
2848                   bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
2849                 }
2850             }
2851           if (!single_pred_p (block))
2852             {
2853               new_stuff |= do_regular_insertion (block, dom);
2854               if (do_partial_partial)
2855                 new_stuff |= do_partial_partial_insertion (block, dom);
2856             }
2857         }
2858     }
2859   for (son = first_dom_son (CDI_DOMINATORS, block);
2860        son;
2861        son = next_dom_son (CDI_DOMINATORS, son))
2862     {
2863       new_stuff |= insert_aux (son);
2864     }
2865
2866   return new_stuff;
2867 }
2868
2869 /* Perform insertion of partially redundant values.  */
2870
2871 static void
2872 insert (void)
2873 {
2874   bool new_stuff = true;
2875   basic_block bb;
2876   int num_iterations = 0;
2877
2878   FOR_ALL_BB (bb)
2879     NEW_SETS (bb) = bitmap_set_new ();
2880
2881   while (new_stuff)
2882     {
2883       num_iterations++;
2884       new_stuff = false;
2885       new_stuff = insert_aux (ENTRY_BLOCK_PTR);
2886     }
2887   if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
2888     fprintf (dump_file, "insert required %d iterations\n", num_iterations);
2889 }
2890
2891
2892 /* Return true if VAR is an SSA variable with no defining statement in
2893    this procedure, *AND* isn't a live-on-entry parameter.  */
2894
2895 static bool
2896 is_undefined_value (tree expr)
2897 {
2898   return (TREE_CODE (expr) == SSA_NAME
2899           && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
2900           /* PARM_DECLs and hard registers are always defined.  */
2901           && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL);
2902 }
2903
2904 /* Add OP to EXP_GEN (block), and possibly to the maximal set if it is
2905    not defined by a phi node.
2906    PHI nodes can't go in the maximal sets because they are not in
2907    TMP_GEN, so it is possible to get into non-monotonic situations
2908    during ANTIC calculation, because it will *add* bits.  */
2909
2910 static void
2911 add_to_exp_gen (basic_block block, tree op)
2912 {
2913   if (!in_fre)
2914     {
2915       if (TREE_CODE (op) == SSA_NAME && is_undefined_value (op))
2916         return;
2917       bitmap_value_insert_into_set (EXP_GEN (block), op);
2918       if (TREE_CODE (op) != SSA_NAME
2919           || TREE_CODE (SSA_NAME_DEF_STMT (op)) != PHI_NODE)
2920         bitmap_value_insert_into_set (maximal_set, op);
2921     }
2922 }
2923
2924
2925 /* Given an SSA variable VAR and an expression EXPR, compute the value
2926    number for EXPR and create a value handle (VAL) for it.  If VAR and
2927    EXPR are not the same, associate VAL with VAR.  Finally, add VAR to
2928    S1 and its value handle to S2, and to the maximal set if
2929    ADD_TO_MAXIMAL is true.
2930
2931    VUSES represent the virtual use operands associated with EXPR (if
2932    any).  */
2933
2934 static inline void
2935 add_to_sets (tree var, tree expr, VEC(tree, gc) *vuses, bitmap_set_t s1,
2936              bitmap_set_t s2)
2937 {
2938   tree val;
2939   val = vn_lookup_or_add_with_vuses (expr, vuses);
2940
2941   /* VAR and EXPR may be the same when processing statements for which
2942      we are not computing value numbers (e.g., non-assignments, or
2943      statements that make aliased stores).  In those cases, we are
2944      only interested in making VAR available as its own value.  */
2945   if (var != expr)
2946     vn_add (var, val);
2947
2948   if (s1)
2949     bitmap_insert_into_set (s1, var);
2950
2951   bitmap_value_insert_into_set (s2, var);
2952 }
2953
2954 /* Find existing value expression that is the same as T,
2955    and return it if it exists.  */
2956
2957 static inline tree
2958 find_existing_value_expr (tree t, VEC (tree, gc) *vuses)
2959 {
2960   bitmap_iterator bi;
2961   unsigned int bii;
2962   tree vh;
2963   bitmap_set_t exprset;
2964
2965   if (REFERENCE_CLASS_P (t) || TREE_CODE (t) == CALL_EXPR || DECL_P (t))
2966     vh = vn_lookup_with_vuses (t, vuses);
2967   else
2968     vh = vn_lookup (t);
2969
2970   if (!vh)
2971     return NULL;
2972   exprset = VALUE_HANDLE_EXPR_SET (vh);
2973   FOR_EACH_EXPR_ID_IN_SET (exprset, bii, bi)
2974     {
2975       tree efi = expression_for_id (bii);
2976       if (expressions_equal_p (t, efi))
2977         return efi;
2978     }
2979   return NULL;
2980 }
2981
2982 /* Given a unary or binary expression EXPR, create and return a new
2983    expression with the same structure as EXPR but with its operands
2984    replaced with the value handles of each of the operands of EXPR.
2985
2986    VUSES represent the virtual use operands associated with EXPR (if
2987    any). Insert EXPR's operands into the EXP_GEN set for BLOCK. */
2988
2989 static inline tree
2990 create_value_expr_from (tree expr, basic_block block, VEC (tree, gc) *vuses)
2991 {
2992   int i;
2993   enum tree_code code = TREE_CODE (expr);
2994   tree vexpr;
2995   alloc_pool pool = NULL;
2996   tree efi;
2997
2998   gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
2999               || TREE_CODE_CLASS (code) == tcc_binary
3000               || TREE_CODE_CLASS (code) == tcc_comparison
3001               || TREE_CODE_CLASS (code) == tcc_reference
3002               || TREE_CODE_CLASS (code) == tcc_expression
3003               || TREE_CODE_CLASS (code) == tcc_vl_exp
3004               || TREE_CODE_CLASS (code) == tcc_exceptional
3005               || TREE_CODE_CLASS (code) == tcc_declaration);
3006
3007   if (TREE_CODE_CLASS (code) == tcc_unary)
3008     pool = unary_node_pool;
3009   else if (TREE_CODE_CLASS (code) == tcc_reference)
3010     pool = reference_node_pool;
3011   else if (TREE_CODE_CLASS (code) == tcc_binary)
3012     pool = binary_node_pool;
3013   else if (TREE_CODE_CLASS (code) == tcc_comparison)
3014     pool = comparison_node_pool;
3015   else
3016     gcc_assert (code == CALL_EXPR);
3017
3018   if (code == CALL_EXPR)
3019     vexpr = temp_copy_call_expr (expr);
3020   else
3021     {
3022       vexpr = (tree) pool_alloc (pool);
3023       memcpy (vexpr, expr, tree_size (expr));
3024     }
3025
3026   for (i = 0; i < TREE_OPERAND_LENGTH (expr); i++)
3027     {
3028       tree val = NULL_TREE;
3029       tree op;
3030
3031       op = TREE_OPERAND (expr, i);
3032       if (op == NULL_TREE)
3033         continue;
3034
3035       /* Recursively value-numberize reference ops and tree lists.  */
3036       if (REFERENCE_CLASS_P (op))
3037         {
3038           tree tempop = create_value_expr_from (op, block, vuses);
3039           op = tempop ? tempop : op;
3040           val = vn_lookup_or_add_with_vuses (op, vuses);
3041           set_expression_vuses (op, vuses);
3042         }
3043       else
3044         {
3045           val = vn_lookup_or_add (op);
3046         }
3047       if (TREE_CODE (op) != TREE_LIST)
3048         add_to_exp_gen (block, op);
3049
3050       if (TREE_CODE (val) == VALUE_HANDLE)
3051         TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
3052
3053       TREE_OPERAND (vexpr, i) = val;
3054     }
3055   efi = find_existing_value_expr (vexpr, vuses);
3056   if (efi)
3057     return efi;
3058   get_or_alloc_expression_id (vexpr);
3059   return vexpr;
3060 }
3061
3062 /* Return a copy of NODE that is stored in the temporary alloc_pool's.
3063    This is made recursively true, so that the operands are stored in
3064    the pool as well.  */
3065
3066 static tree
3067 poolify_tree (tree node)
3068 {
3069   switch  (TREE_CODE (node))
3070     {
3071     case INDIRECT_REF:
3072       {
3073         tree temp = (tree) pool_alloc (reference_node_pool);
3074         memcpy (temp, node, tree_size (node));
3075         TREE_OPERAND (temp, 0) = poolify_tree (TREE_OPERAND (temp, 0));
3076         return temp;
3077       }
3078       break;
3079     case GIMPLE_MODIFY_STMT:
3080       {
3081         tree temp = (tree) pool_alloc (modify_expr_node_pool);
3082         memcpy (temp, node, tree_size (node));
3083         GIMPLE_STMT_OPERAND (temp, 0) =
3084           poolify_tree (GIMPLE_STMT_OPERAND (temp, 0));
3085         GIMPLE_STMT_OPERAND (temp, 1) =
3086           poolify_tree (GIMPLE_STMT_OPERAND (temp, 1));
3087         return temp;
3088       }
3089       break;
3090     case SSA_NAME:
3091     case INTEGER_CST:
3092     case STRING_CST:
3093     case REAL_CST:
3094     case FIXED_CST:
3095     case PARM_DECL:
3096     case VAR_DECL:
3097     case RESULT_DECL:
3098       return node;
3099     default:
3100       gcc_unreachable ();
3101     }
3102 }
3103
3104 static tree modify_expr_template;
3105
3106 /* Allocate a GIMPLE_MODIFY_STMT with TYPE, and operands OP1, OP2 in the
3107    alloc pools and return it.  */
3108 static tree
3109 poolify_modify_stmt (tree op1, tree op2)
3110 {
3111   if (modify_expr_template == NULL)
3112     modify_expr_template = build_gimple_modify_stmt (op1, op2);
3113
3114   GIMPLE_STMT_OPERAND (modify_expr_template, 0) = op1;
3115   GIMPLE_STMT_OPERAND (modify_expr_template, 1) = op2;
3116
3117   return poolify_tree (modify_expr_template);
3118 }
3119
3120
3121 /* For each real store operation of the form
3122    *a = <value> that we see, create a corresponding fake store of the
3123    form storetmp_<version> = *a.
3124
3125    This enables AVAIL computation to mark the results of stores as
3126    available.  Without this, you'd need to do some computation to
3127    mark the result of stores as ANTIC and AVAIL at all the right
3128    points.
3129    To save memory, we keep the store
3130    statements pool allocated until we decide whether they are
3131    necessary or not.  */
3132
3133 static void
3134 insert_fake_stores (void)
3135 {
3136   basic_block block;
3137
3138   FOR_ALL_BB (block)
3139     {
3140       block_stmt_iterator bsi;
3141       for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
3142         {
3143           tree stmt = bsi_stmt (bsi);
3144
3145           /* We can't generate SSA names for stores that are complex
3146              or aggregate.  We also want to ignore things whose
3147              virtual uses occur in abnormal phis.  */
3148
3149           if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3150               && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == INDIRECT_REF
3151               && !AGGREGATE_TYPE_P (TREE_TYPE (GIMPLE_STMT_OPERAND (stmt, 0)))
3152               && TREE_CODE (TREE_TYPE (GIMPLE_STMT_OPERAND
3153                                         (stmt, 0))) != COMPLEX_TYPE)
3154             {
3155               ssa_op_iter iter;
3156               def_operand_p defp;
3157               tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3158               tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3159               tree new_tree;
3160               bool notokay = false;
3161
3162               FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_VIRTUAL_DEFS)
3163                 {
3164                   tree defvar = DEF_FROM_PTR (defp);
3165                   if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (defvar))
3166                     {
3167                       notokay = true;
3168                       break;
3169                     }
3170                 }
3171
3172               if (notokay)
3173                 continue;
3174
3175               if (!storetemp || TREE_TYPE (rhs) != TREE_TYPE (storetemp))
3176                 {
3177                   storetemp = create_tmp_var (TREE_TYPE (rhs), "storetmp");
3178                   if (TREE_CODE (TREE_TYPE (storetemp)) == VECTOR_TYPE)
3179                     DECL_GIMPLE_REG_P (storetemp) = 1;
3180                   get_var_ann (storetemp);
3181                 }
3182
3183               new_tree = poolify_modify_stmt (storetemp, lhs);
3184
3185               lhs = make_ssa_name (storetemp, new_tree);
3186               GIMPLE_STMT_OPERAND (new_tree, 0) = lhs;
3187               create_ssa_artificial_load_stmt (new_tree, stmt);
3188
3189               NECESSARY (new_tree) = 0;
3190               VEC_safe_push (tree, heap, inserted_exprs, new_tree);
3191               VEC_safe_push (tree, heap, need_creation, new_tree);
3192               bsi_insert_after (&bsi, new_tree, BSI_NEW_STMT);
3193             }
3194         }
3195     }
3196 }
3197
3198 /* Turn the pool allocated fake stores that we created back into real
3199    GC allocated ones if they turned out to be necessary to PRE some
3200    expressions.  */
3201
3202 static void
3203 realify_fake_stores (void)
3204 {
3205   unsigned int i;
3206   tree stmt;
3207
3208   for (i = 0; VEC_iterate (tree, need_creation, i, stmt); i++)
3209     {
3210       if (NECESSARY (stmt))
3211         {
3212           block_stmt_iterator bsi;
3213           tree newstmt, tmp;
3214
3215           /* Mark the temp variable as referenced */
3216           add_referenced_var (SSA_NAME_VAR (GIMPLE_STMT_OPERAND (stmt, 0)));
3217
3218           /* Put the new statement in GC memory, fix up the
3219              SSA_NAME_DEF_STMT on it, and then put it in place of
3220              the old statement before the store in the IR stream
3221              as a plain ssa name copy.  */
3222           bsi = bsi_for_stmt (stmt);
3223           bsi_prev (&bsi);
3224           tmp = GIMPLE_STMT_OPERAND (bsi_stmt (bsi), 1);
3225           newstmt = build_gimple_modify_stmt (GIMPLE_STMT_OPERAND (stmt, 0),
3226                                               tmp);
3227           SSA_NAME_DEF_STMT (GIMPLE_STMT_OPERAND (newstmt, 0)) = newstmt;
3228           bsi_insert_before (&bsi, newstmt, BSI_SAME_STMT);
3229           bsi = bsi_for_stmt (stmt);
3230           bsi_remove (&bsi, true);
3231         }
3232       else
3233         release_defs (stmt);
3234     }
3235 }
3236
3237 /* Given an SSA_NAME, see if SCCVN has a value number for it, and if
3238    so, return the value handle for this value number, creating it if
3239    necessary.
3240    Return NULL if SCCVN has no info for us.  */
3241
3242 static tree
3243 get_sccvn_value (tree name)
3244 {
3245   if (TREE_CODE (name) == SSA_NAME
3246       && VN_INFO (name)->valnum != name
3247       && VN_INFO (name)->valnum != VN_TOP)
3248     {
3249       tree val = VN_INFO (name)->valnum;
3250       bool is_invariant = is_gimple_min_invariant (val);
3251       tree valvh = !is_invariant ? get_value_handle (val) : NULL_TREE;
3252
3253       /* We may end up with situations where SCCVN has chosen a
3254          representative for the equivalence set that we have not
3255          visited yet.  In this case, just create the value handle for
3256          it.  */
3257       if (!valvh && !is_invariant)
3258         {
3259           tree defstmt = SSA_NAME_DEF_STMT (val);
3260
3261           gcc_assert (VN_INFO (val)->valnum == val);
3262           /* PHI nodes can't have vuses and attempts to iterate over
3263              their VUSE operands will crash.  */
3264           if (TREE_CODE (defstmt) == PHI_NODE || IS_EMPTY_STMT (defstmt))
3265             defstmt = NULL;
3266           {
3267             tree defstmt2 = SSA_NAME_DEF_STMT (name);
3268             if (TREE_CODE (defstmt2) != PHI_NODE &&
3269                 !ZERO_SSA_OPERANDS (defstmt2, SSA_OP_ALL_VIRTUALS))
3270               gcc_assert (defstmt);
3271           }
3272           valvh = vn_lookup_or_add_with_stmt (val, defstmt);
3273         }
3274
3275       if (dump_file && (dump_flags & TDF_DETAILS))
3276         {
3277           fprintf (dump_file, "SCCVN says ");
3278           print_generic_expr (dump_file, name, 0);
3279           fprintf (dump_file, " value numbers to ");
3280           if (valvh && !is_invariant)
3281             {
3282               print_generic_expr (dump_file, val, 0);
3283               fprintf (dump_file, " (");
3284               print_generic_expr (dump_file, valvh, 0);
3285               fprintf (dump_file, ")\n");
3286             }
3287           else
3288             print_generic_stmt (dump_file, val, 0);
3289         }
3290       if (valvh)
3291         return valvh;
3292       else
3293         return val;
3294     }
3295   return NULL_TREE;
3296 }
3297
3298 /* Create value handles for PHI in BLOCK.  */
3299
3300 static void
3301 make_values_for_phi (tree phi, basic_block block)
3302 {
3303   tree result = PHI_RESULT (phi);
3304   /* We have no need for virtual phis, as they don't represent
3305      actual computations.  */
3306   if (is_gimple_reg (result))
3307     {
3308       tree sccvnval = get_sccvn_value (result);
3309       if (sccvnval)
3310         {
3311           vn_add (result, sccvnval);
3312           bitmap_insert_into_set (PHI_GEN (block), result);
3313           bitmap_value_insert_into_set (AVAIL_OUT (block), result);
3314         }
3315       else
3316         add_to_sets (result, result, NULL,
3317                      PHI_GEN (block), AVAIL_OUT (block));
3318     }
3319 }
3320
3321 /* Create value handles for STMT in BLOCK.  Return true if we handled
3322    the statement.  */
3323
3324 static bool
3325 make_values_for_stmt (tree stmt, basic_block block)
3326 {
3327
3328   tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3329   tree rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3330   tree valvh = NULL_TREE;
3331   tree lhsval;
3332   VEC (tree, gc) *vuses = NULL;
3333
3334   valvh = get_sccvn_value (lhs);
3335
3336   if (valvh)
3337     {
3338       vn_add (lhs, valvh);
3339       bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
3340       /* Shortcut for FRE. We have no need to create value expressions,
3341          just want to know what values are available where.  */
3342       if (in_fre)
3343         return true;
3344
3345     }
3346   else if (in_fre)
3347     {
3348       /* For FRE, if SCCVN didn't find anything, we aren't going to
3349          either, so just make up a new value number if necessary and
3350          call it a day */
3351       if (get_value_handle (lhs) == NULL)
3352         vn_lookup_or_add (lhs);
3353       bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
3354       return true;
3355     }
3356
3357   lhsval = valvh ? valvh : get_value_handle (lhs);
3358   vuses = copy_vuses_from_stmt (stmt);
3359   STRIP_USELESS_TYPE_CONVERSION (rhs);
3360   if (can_value_number_operation (rhs)
3361       && (!lhsval || !is_gimple_min_invariant (lhsval)))
3362     {
3363       /* For value numberable operation, create a
3364          duplicate expression with the operands replaced
3365          with the value handles of the original RHS.  */
3366       tree newt = create_value_expr_from (rhs, block, vuses);
3367       if (newt)
3368         {
3369           set_expression_vuses (newt, vuses);
3370           /* If we already have a value number for the LHS, reuse
3371              it rather than creating a new one.  */
3372           if (lhsval)
3373             {
3374               set_value_handle (newt, lhsval);
3375               if (!is_gimple_min_invariant (lhsval))
3376                 add_to_value (lhsval, newt);
3377             }
3378           else
3379             {
3380               tree val = vn_lookup_or_add_with_vuses (newt, vuses);
3381               vn_add (lhs, val);
3382             }
3383
3384           add_to_exp_gen (block, newt);
3385         }
3386
3387       bitmap_insert_into_set (TMP_GEN (block), lhs);
3388       bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
3389       return true;
3390     }
3391   else if ((TREE_CODE (rhs) == SSA_NAME
3392             && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
3393            || is_gimple_min_invariant (rhs)
3394            || TREE_CODE (rhs) == ADDR_EXPR
3395            || TREE_INVARIANT (rhs)
3396            || DECL_P (rhs))
3397     {
3398
3399       if (lhsval)
3400         {
3401           set_expression_vuses (rhs, vuses);
3402           set_value_handle (rhs, lhsval);
3403           if (!is_gimple_min_invariant (lhsval))
3404             add_to_value (lhsval, rhs);
3405           bitmap_insert_into_set (TMP_GEN (block), lhs);
3406           bitmap_value_insert_into_set (AVAIL_OUT (block), lhs);
3407         }
3408       else
3409         {
3410           /* Compute a value number for the RHS of the statement
3411              and add its value to the AVAIL_OUT set for the block.
3412              Add the LHS to TMP_GEN.  */
3413           set_expression_vuses (rhs, vuses);
3414           add_to_sets (lhs, rhs, vuses, TMP_GEN (block),
3415                        AVAIL_OUT (block));
3416         }
3417       /* None of the rest of these can be PRE'd.  */
3418       if (TREE_CODE (rhs) == SSA_NAME && !is_undefined_value (rhs))
3419         add_to_exp_gen (block, rhs);
3420       return true;
3421     }
3422   return false;
3423
3424 }
3425
3426 /* Compute the AVAIL set for all basic blocks.
3427
3428    This function performs value numbering of the statements in each basic
3429    block.  The AVAIL sets are built from information we glean while doing
3430    this value numbering, since the AVAIL sets contain only one entry per
3431    value.
3432
3433    AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3434    AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK].  */
3435
3436 static void
3437 compute_avail (void)
3438 {
3439   basic_block block, son;
3440   basic_block *worklist;
3441   size_t sp = 0;
3442   tree param;
3443
3444   /* For arguments with default definitions, we pretend they are
3445      defined in the entry block.  */
3446   for (param = DECL_ARGUMENTS (current_function_decl);
3447        param;
3448        param = TREE_CHAIN (param))
3449     {
3450       if (gimple_default_def (cfun, param) != NULL)
3451         {
3452           tree def = gimple_default_def (cfun, param);
3453
3454           vn_lookup_or_add (def);
3455           if (!in_fre)
3456             {
3457               bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
3458               bitmap_value_insert_into_set (maximal_set, def);
3459             }
3460           bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
3461         }
3462     }
3463
3464   /* Likewise for the static chain decl. */
3465   if (cfun->static_chain_decl)
3466     {
3467       param = cfun->static_chain_decl;
3468       if (gimple_default_def (cfun, param) != NULL)
3469         {
3470           tree def = gimple_default_def (cfun, param);
3471
3472           vn_lookup_or_add (def);
3473           if (!in_fre)
3474             {
3475               bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
3476               bitmap_value_insert_into_set (maximal_set, def);
3477             }
3478           bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
3479         }
3480     }
3481
3482   /* Allocate the worklist.  */
3483   worklist = XNEWVEC (basic_block, n_basic_blocks);
3484
3485   /* Seed the algorithm by putting the dominator children of the entry
3486      block on the worklist.  */
3487   for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR);
3488        son;
3489        son = next_dom_son (CDI_DOMINATORS, son))
3490     worklist[sp++] = son;
3491
3492   /* Loop until the worklist is empty.  */
3493   while (sp)
3494     {
3495       block_stmt_iterator bsi;
3496       tree stmt, phi;
3497       basic_block dom;
3498       unsigned int stmt_uid = 1;
3499
3500       /* Pick a block from the worklist.  */
3501       block = worklist[--sp];
3502
3503       /* Initially, the set of available values in BLOCK is that of
3504          its immediate dominator.  */
3505       dom = get_immediate_dominator (CDI_DOMINATORS, block);
3506       if (dom)
3507         bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3508
3509       /* Generate values for PHI nodes.  */
3510       for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
3511         make_values_for_phi (phi, block);
3512
3513       /* Now compute value numbers and populate value sets with all
3514          the expressions computed in BLOCK.  */
3515       for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
3516         {
3517           stmt_ann_t ann;
3518           ssa_op_iter iter;
3519           tree op;
3520
3521           stmt = bsi_stmt (bsi);
3522           ann = stmt_ann (stmt);
3523
3524           ann->uid = stmt_uid++;
3525
3526           /* For regular value numbering, we are only interested in
3527              assignments of the form X_i = EXPR, where EXPR represents
3528              an "interesting" computation, it has no volatile operands
3529              and X_i doesn't flow through an abnormal edge.  */
3530           if (TREE_CODE (stmt) == RETURN_EXPR
3531               && !ann->has_volatile_ops)
3532             {
3533               tree realstmt = stmt;
3534               tree lhs;
3535               tree rhs;
3536
3537               stmt = TREE_OPERAND (stmt, 0);
3538               if (stmt && TREE_CODE (stmt) == GIMPLE_MODIFY_STMT)
3539                 {
3540                   lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3541                   rhs = GIMPLE_STMT_OPERAND (stmt, 1);
3542                   if (TREE_CODE (lhs) == SSA_NAME
3543                       && is_gimple_min_invariant (VN_INFO (lhs)->valnum))
3544                     {
3545                       if (dump_file && (dump_flags & TDF_DETAILS))
3546                         {
3547                           fprintf (dump_file, "SCCVN says ");
3548                           print_generic_expr (dump_file, lhs, 0);
3549                           fprintf (dump_file, " value numbers to ");
3550                           print_generic_stmt (dump_file, VN_INFO (lhs)->valnum,
3551                                               0);
3552                         }
3553                       vn_add (lhs, VN_INFO (lhs)->valnum);
3554                       continue;
3555                     }
3556
3557                   if (TREE_CODE (rhs) == SSA_NAME)
3558                     add_to_exp_gen (block, rhs);
3559
3560                   FOR_EACH_SSA_TREE_OPERAND (op, realstmt, iter, SSA_OP_DEF)
3561                     add_to_sets (op, op, NULL, TMP_GEN (block),
3562                                  AVAIL_OUT (block));
3563                 }
3564               continue;
3565             }
3566
3567           else if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3568                    && !ann->has_volatile_ops
3569                    && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
3570                    && (!SSA_NAME_OCCURS_IN_ABNORMAL_PHI
3571                        (GIMPLE_STMT_OPERAND (stmt, 0)))
3572                    && !tree_could_throw_p (stmt))
3573             {
3574               if (make_values_for_stmt (stmt, block))
3575                 continue;
3576
3577             }
3578
3579           /* For any other statement that we don't recognize, simply
3580              make the names generated by the statement available in
3581              AVAIL_OUT and TMP_GEN.  */
3582           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3583             add_to_sets (op, op, NULL, TMP_GEN (block), AVAIL_OUT (block));
3584
3585           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3586             {
3587               add_to_sets (op, op, NULL, NULL , AVAIL_OUT (block));
3588               if (TREE_CODE (op) == SSA_NAME || can_PRE_operation (op))
3589                 add_to_exp_gen (block, op);
3590             }
3591         }
3592
3593       /* Put the dominator children of BLOCK on the worklist of blocks
3594          to compute available sets for.  */
3595       for (son = first_dom_son (CDI_DOMINATORS, block);
3596            son;
3597            son = next_dom_son (CDI_DOMINATORS, son))
3598         worklist[sp++] = son;
3599     }
3600
3601   free (worklist);
3602 }
3603
3604
3605 /* Eliminate fully redundant computations.  */
3606
3607 static void
3608 eliminate (void)
3609 {
3610   basic_block b;
3611
3612   FOR_EACH_BB (b)
3613     {
3614       block_stmt_iterator i;
3615
3616       for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
3617         {
3618           tree stmt = bsi_stmt (i);
3619
3620           /* Lookup the RHS of the expression, see if we have an
3621              available computation for it.  If so, replace the RHS with
3622              the available computation.  */
3623           if (TREE_CODE (stmt) == GIMPLE_MODIFY_STMT
3624               && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 0)) == SSA_NAME
3625               && TREE_CODE (GIMPLE_STMT_OPERAND (stmt, 1)) != SSA_NAME
3626               && !is_gimple_min_invariant (GIMPLE_STMT_OPERAND (stmt, 1))
3627               && !stmt_ann (stmt)->has_volatile_ops)
3628             {
3629               tree lhs = GIMPLE_STMT_OPERAND (stmt, 0);
3630               tree *rhs_p = &GIMPLE_STMT_OPERAND (stmt, 1);
3631               tree sprime;
3632
3633               sprime = bitmap_find_leader (AVAIL_OUT (b),
3634                                            get_value_handle (lhs));
3635
3636               if (sprime
3637                   && sprime != lhs
3638                   && (TREE_CODE (*rhs_p) != SSA_NAME
3639                       || may_propagate_copy (*rhs_p, sprime)))
3640                 {
3641                   gcc_assert (sprime != *rhs_p);
3642
3643                   if (dump_file && (dump_flags & TDF_DETAILS))
3644                     {
3645                       fprintf (dump_file, "Replaced ");
3646                       print_generic_expr (dump_file, *rhs_p, 0);
3647                       fprintf (dump_file, " with ");
3648                       print_generic_expr (dump_file, sprime, 0);
3649                       fprintf (dump_file, " in ");
3650                       print_generic_stmt (dump_file, stmt, 0);
3651                     }
3652
3653                   if (TREE_CODE (sprime) == SSA_NAME)
3654                     NECESSARY (SSA_NAME_DEF_STMT (sprime)) = 1;
3655                   /* We need to make sure the new and old types actually match,
3656                      which may require adding a simple cast, which fold_convert
3657                      will do for us.  */
3658                   if (TREE_CODE (*rhs_p) != SSA_NAME
3659                       && !useless_type_conversion_p (TREE_TYPE (*rhs_p),
3660                                                     TREE_TYPE (sprime)))
3661                     sprime = fold_convert (TREE_TYPE (*rhs_p), sprime);
3662
3663                   pre_stats.eliminations++;
3664                   propagate_tree_value (rhs_p, sprime);
3665                   update_stmt (stmt);
3666
3667                   /* If we removed EH side effects from the statement, clean
3668                      its EH information.  */
3669                   if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
3670                     {
3671                       bitmap_set_bit (need_eh_cleanup,
3672                                       bb_for_stmt (stmt)->index);
3673                       if (dump_file && (dump_flags & TDF_DETAILS))
3674                         fprintf (dump_file, "  Removed EH side effects.\n");
3675                     }
3676                 }
3677             }
3678         }
3679     }
3680 }
3681
3682 /* Borrow a bit of tree-ssa-dce.c for the moment.
3683    XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
3684    this may be a bit faster, and we may want critical edges kept split.  */
3685
3686 /* If OP's defining statement has not already been determined to be necessary,
3687    mark that statement necessary. Return the stmt, if it is newly
3688    necessary.  */
3689
3690 static inline tree
3691 mark_operand_necessary (tree op)
3692 {
3693   tree stmt;
3694
3695   gcc_assert (op);
3696
3697   if (TREE_CODE (op) != SSA_NAME)
3698     return NULL;
3699
3700   stmt = SSA_NAME_DEF_STMT (op);
3701   gcc_assert (stmt);
3702
3703   if (NECESSARY (stmt)
3704       || IS_EMPTY_STMT (stmt))
3705     return NULL;
3706
3707   NECESSARY (stmt) = 1;
3708   return stmt;
3709 }
3710
3711 /* Because we don't follow exactly the standard PRE algorithm, and decide not
3712    to insert PHI nodes sometimes, and because value numbering of casts isn't
3713    perfect, we sometimes end up inserting dead code.   This simple DCE-like
3714    pass removes any insertions we made that weren't actually used.  */
3715
3716 static void
3717 remove_dead_inserted_code (void)
3718 {
3719   VEC(tree,heap) *worklist = NULL;
3720   int i;
3721   tree t;
3722
3723   worklist = VEC_alloc (tree, heap, VEC_length (tree, inserted_exprs));
3724   for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
3725     {
3726       if (NECESSARY (t))
3727         VEC_quick_push (tree, worklist, t);
3728     }
3729   while (VEC_length (tree, worklist) > 0)
3730     {
3731       t = VEC_pop (tree, worklist);
3732
3733       /* PHI nodes are somewhat special in that each PHI alternative has
3734          data and control dependencies.  All the statements feeding the
3735          PHI node's arguments are always necessary. */
3736       if (TREE_CODE (t) == PHI_NODE)
3737         {
3738           int k;
3739
3740           VEC_reserve (tree, heap, worklist, PHI_NUM_ARGS (t));
3741           for (k = 0; k < PHI_NUM_ARGS (t); k++)
3742             {
3743               tree arg = PHI_ARG_DEF (t, k);
3744               if (TREE_CODE (arg) == SSA_NAME)
3745                 {
3746                   arg = mark_operand_necessary (arg);
3747                   if (arg)
3748                     VEC_quick_push (tree, worklist, arg);
3749                 }
3750             }
3751         }
3752       else
3753         {
3754           /* Propagate through the operands.  Examine all the USE, VUSE and
3755              VDEF operands in this statement.  Mark all the statements
3756              which feed this statement's uses as necessary.  */
3757           ssa_op_iter iter;
3758           tree use;
3759
3760           /* The operands of VDEF expressions are also needed as they
3761              represent potential definitions that may reach this
3762              statement (VDEF operands allow us to follow def-def
3763              links).  */
3764
3765           FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
3766             {
3767               tree n = mark_operand_necessary (use);
3768               if (n)
3769                 VEC_safe_push (tree, heap, worklist, n);
3770             }
3771         }
3772     }
3773
3774   for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
3775     {
3776       if (!NECESSARY (t))
3777         {
3778           block_stmt_iterator bsi;
3779
3780           if (dump_file && (dump_flags & TDF_DETAILS))
3781             {
3782               fprintf (dump_file, "Removing unnecessary insertion:");
3783               print_generic_stmt (dump_file, t, 0);
3784             }
3785
3786           if (TREE_CODE (t) == PHI_NODE)
3787             {
3788               remove_phi_node (t, NULL, true);
3789             }
3790           else
3791             {
3792               bsi = bsi_for_stmt (t);
3793               bsi_remove (&bsi, true);
3794               release_defs (t);
3795             }
3796         }
3797     }
3798   VEC_free (tree, heap, worklist);
3799 }
3800
3801 /* Initialize data structures used by PRE.  */
3802
3803 static void
3804 init_pre (bool do_fre)
3805 {
3806   basic_block bb;
3807
3808   next_expression_id = 0;
3809   expressions = NULL;
3810   expression_vuses = NULL;
3811   in_fre = do_fre;
3812
3813   inserted_exprs = NULL;
3814   need_creation = NULL;
3815   pretemp = NULL_TREE;
3816   storetemp = NULL_TREE;
3817   prephitemp = NULL_TREE;
3818
3819   if (!do_fre)
3820     loop_optimizer_init (LOOPS_NORMAL);
3821
3822   connect_infinite_loops_to_exit ();
3823   memset (&pre_stats, 0, sizeof (pre_stats));
3824
3825
3826   postorder = XNEWVEC (int, n_basic_blocks - NUM_FIXED_BLOCKS);
3827   post_order_compute (postorder, false, false);
3828
3829   FOR_ALL_BB (bb)
3830     bb->aux = xcalloc (1, sizeof (struct bb_bitmap_sets));
3831
3832   calculate_dominance_info (CDI_POST_DOMINATORS);
3833   calculate_dominance_info (CDI_DOMINATORS);
3834
3835   bitmap_obstack_initialize (&grand_bitmap_obstack);
3836   phi_translate_table = htab_create (5110, expr_pred_trans_hash,
3837                                      expr_pred_trans_eq, free);
3838   seen_during_translate = BITMAP_ALLOC (&grand_bitmap_obstack);
3839   bitmap_set_pool = create_alloc_pool ("Bitmap sets",
3840                                        sizeof (struct bitmap_set), 30);
3841   binary_node_pool = create_alloc_pool ("Binary tree nodes",
3842                                         tree_code_size (PLUS_EXPR), 30);
3843   unary_node_pool = create_alloc_pool ("Unary tree nodes",
3844                                        tree_code_size (NEGATE_EXPR), 30);
3845   reference_node_pool = create_alloc_pool ("Reference tree nodes",
3846                                            tree_code_size (ARRAY_REF), 30);
3847   comparison_node_pool = create_alloc_pool ("Comparison tree nodes",
3848                                             tree_code_size (EQ_EXPR), 30);
3849   modify_expr_node_pool = create_alloc_pool ("GIMPLE_MODIFY_STMT nodes",
3850                                              tree_code_size (GIMPLE_MODIFY_STMT),
3851                                              30);
3852   obstack_init (&temp_call_expr_obstack);
3853   modify_expr_template = NULL;
3854
3855   FOR_ALL_BB (bb)
3856     {
3857       EXP_GEN (bb) = bitmap_set_new ();
3858       PHI_GEN (bb) = bitmap_set_new ();
3859       TMP_GEN (bb) = bitmap_set_new ();
3860       AVAIL_OUT (bb) = bitmap_set_new ();
3861     }
3862   maximal_set = in_fre ? NULL : bitmap_set_new ();
3863
3864   need_eh_cleanup = BITMAP_ALLOC (NULL);
3865 }
3866
3867
3868 /* Deallocate data structures used by PRE.  */
3869
3870 static void
3871 fini_pre (void)
3872 {
3873   basic_block bb;
3874   unsigned int i;
3875
3876   free (postorder);
3877   VEC_free (tree, heap, inserted_exprs);
3878   VEC_free (tree, heap, need_creation);
3879   bitmap_obstack_release (&grand_bitmap_obstack);
3880   free_alloc_pool (bitmap_set_pool);
3881   free_alloc_pool (binary_node_pool);
3882   free_alloc_pool (reference_node_pool);
3883   free_alloc_pool (unary_node_pool);
3884   free_alloc_pool (comparison_node_pool);
3885   free_alloc_pool (modify_expr_node_pool);
3886   htab_delete (phi_translate_table);
3887   remove_fake_exit_edges ();
3888
3889   FOR_ALL_BB (bb)
3890     {
3891       free (bb->aux);
3892       bb->aux = NULL;
3893     }
3894
3895   free_dominance_info (CDI_POST_DOMINATORS);
3896
3897   if (!bitmap_empty_p (need_eh_cleanup))
3898     {
3899       tree_purge_all_dead_eh_edges (need_eh_cleanup);
3900       cleanup_tree_cfg ();
3901     }
3902
3903   BITMAP_FREE (need_eh_cleanup);
3904
3905   /* Wipe out pointers to VALUE_HANDLEs.  In the not terribly distant
3906      future we will want them to be persistent though.  */
3907   for (i = 0; i < num_ssa_names; i++)
3908     {
3909       tree name = ssa_name (i);
3910
3911       if (!name)
3912         continue;
3913
3914       if (SSA_NAME_VALUE (name)
3915           && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
3916         SSA_NAME_VALUE (name) = NULL;
3917     }
3918   if (current_loops != NULL)
3919     loop_optimizer_finalize ();
3920 }
3921
3922 /* Main entry point to the SSA-PRE pass.  DO_FRE is true if the caller
3923    only wants to do full redundancy elimination.  */
3924
3925 static void
3926 execute_pre (bool do_fre)
3927 {
3928
3929   do_partial_partial = optimize > 2;
3930   init_pre (do_fre);
3931
3932   if (!do_fre)
3933     insert_fake_stores ();
3934
3935   /* Collect and value number expressions computed in each basic block.  */
3936   run_scc_vn ();
3937   switch_to_PRE_table ();
3938   compute_avail ();
3939
3940   if (dump_file && (dump_flags & TDF_DETAILS))
3941     {
3942       basic_block bb;
3943
3944       FOR_ALL_BB (bb)
3945         {
3946           print_bitmap_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
3947           print_bitmap_set (dump_file, TMP_GEN (bb), "tmp_gen",
3948                                   bb->index);
3949           print_bitmap_set (dump_file, AVAIL_OUT (bb), "avail_out",
3950                                   bb->index);
3951         }
3952     }
3953
3954   /* Insert can get quite slow on an incredibly large number of basic
3955      blocks due to some quadratic behavior.  Until this behavior is
3956      fixed, don't run it when he have an incredibly large number of
3957      bb's.  If we aren't going to run insert, there is no point in
3958      computing ANTIC, either, even though it's plenty fast.  */
3959   if (!do_fre && n_basic_blocks < 4000)
3960     {
3961       compute_antic ();
3962       insert ();
3963     }
3964
3965   /* Remove all the redundant expressions.  */
3966   eliminate ();
3967
3968   if (dump_file && (dump_flags & TDF_STATS))
3969     {
3970       fprintf (dump_file, "Insertions: %d\n", pre_stats.insertions);
3971       fprintf (dump_file, "PA inserted: %d\n", pre_stats.pa_insert);
3972       fprintf (dump_file, "New PHIs: %d\n", pre_stats.phis);
3973       fprintf (dump_file, "Eliminated: %d\n", pre_stats.eliminations);
3974       fprintf (dump_file, "Constified: %d\n", pre_stats.constified);
3975     }
3976   bsi_commit_edge_inserts ();
3977
3978   free_scc_vn ();
3979   clear_expression_ids ();
3980   if (!do_fre)
3981     {
3982       remove_dead_inserted_code ();
3983       realify_fake_stores ();
3984     }
3985
3986   fini_pre ();
3987 }
3988
3989 /* Gate and execute functions for PRE.  */
3990
3991 static unsigned int
3992 do_pre (void)
3993 {
3994   execute_pre (false);
3995   return TODO_rebuild_alias;
3996 }
3997
3998 static bool
3999 gate_pre (void)
4000 {
4001   return flag_tree_pre != 0;
4002 }
4003
4004 struct tree_opt_pass pass_pre =
4005 {
4006   "pre",                                /* name */
4007   gate_pre,                             /* gate */
4008   do_pre,                               /* execute */
4009   NULL,                                 /* sub */
4010   NULL,                                 /* next */
4011   0,                                    /* static_pass_number */
4012   TV_TREE_PRE,                          /* tv_id */
4013   PROP_no_crit_edges | PROP_cfg
4014     | PROP_ssa | PROP_alias,            /* properties_required */
4015   0,                                    /* properties_provided */
4016   0,                                    /* properties_destroyed */
4017   0,                                    /* todo_flags_start */
4018   TODO_update_ssa_only_virtuals | TODO_dump_func | TODO_ggc_collect
4019   | TODO_verify_ssa, /* todo_flags_finish */
4020   0                                     /* letter */
4021 };
4022
4023
4024 /* Gate and execute functions for FRE.  */
4025
4026 static unsigned int
4027 execute_fre (void)
4028 {
4029   execute_pre (true);
4030   return 0;
4031 }
4032
4033 static bool
4034 gate_fre (void)
4035 {
4036   return flag_tree_fre != 0;
4037 }
4038
4039 struct tree_opt_pass pass_fre =
4040 {
4041   "fre",                                /* name */
4042   gate_fre,                             /* gate */
4043   execute_fre,                          /* execute */
4044   NULL,                                 /* sub */
4045   NULL,                                 /* next */
4046   0,                                    /* static_pass_number */
4047   TV_TREE_FRE,                          /* tv_id */
4048   PROP_cfg | PROP_ssa | PROP_alias,     /* properties_required */
4049   0,                                    /* properties_provided */
4050   0,                                    /* properties_destroyed */
4051   0,                                    /* todo_flags_start */
4052   TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
4053   0                                     /* letter */
4054 };