OSDN Git Service

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