OSDN Git Service

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