OSDN Git Service

* basic-block.h, config/i386/winnt.c, config/pa/pa.c,
[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. Load motion can be performed by value numbering the loads the
53       same as we do other expressions.  This requires iterative
54       hashing the vuses into the values.  Right now we simply assign
55       a new value every time we see a statement with a vuse.
56    3. Strength reduction can be performed by anticipating expressions
57       we can repair later on.
58    4. We can do back-substitution or smarter value numbering to catch
59       commutative expressions split up over multiple statements.
60 */   
61
62 /* For ease of terminology, "expression node" in the below refers to
63    every expression node but MODIFY_EXPR, because MODIFY_EXPR's represent
64    the actual statement containing the expressions we care about, and
65    we cache the value number by putting it in the expression.  */
66
67 /* Basic algorithm
68    
69    First we walk the statements to generate the AVAIL sets, the
70    EXP_GEN sets, and the tmp_gen sets.  EXP_GEN sets represent the
71    generation of values/expressions by a given block.  We use them
72    when computing the ANTIC sets.  The AVAIL sets consist of
73    SSA_NAME's that represent values, so we know what values are
74    available in what blocks.  AVAIL is a forward dataflow problem.  In
75    SSA, values are never killed, so we don't need a kill set, or a
76    fixpoint iteration, in order to calculate the AVAIL sets.  In
77    traditional parlance, AVAIL sets tell us the downsafety of the
78    expressions/values.
79    
80    Next, we generate the ANTIC sets.  These sets represent the
81    anticipatable expressions.  ANTIC is a backwards dataflow
82    problem.An expression is anticipatable in a given block if it could
83    be generated in that block.  This means that if we had to perform
84    an insertion in that block, of the value of that expression, we
85    could.  Calculating the ANTIC sets requires phi translation of
86    expressions, because the flow goes backwards through phis.  We must
87    iterate to a fixpoint of the ANTIC sets, because we have a kill
88    set.  Even in SSA form, values are not live over the entire
89    function, only from their definition point onwards.  So we have to
90    remove values from the ANTIC set once we go past the definition
91    point of the leaders that make them up.
92    compute_antic/compute_antic_aux performs this computation.
93
94    Third, we perform insertions to make partially redundant
95    expressions fully redundant.
96
97    An expression is partially redundant (excluding partial
98    anticipation) if:
99
100    1. It is AVAIL in some, but not all, of the predecessors of a
101       given block.
102    2. It is ANTIC in all the predecessors.
103
104    In order to make it fully redundant, we insert the expression into
105    the predecessors where it is not available, but is ANTIC.
106    insert/insert_aux performs this insertion.
107
108    Fourth, we eliminate fully redundant expressions.
109    This is a simple statement walk that replaces redundant
110    calculations  with the now available values.  */
111
112 /* Representations of value numbers:
113
114    Value numbers are represented using the "value handle" approach.
115    This means that each SSA_NAME (and for other reasons to be
116    disclosed in a moment, expression nodes) has a value handle that
117    can be retrieved through get_value_handle.  This value handle, *is*
118    the value number of the SSA_NAME.  You can pointer compare the
119    value handles for equivalence purposes.
120
121    For debugging reasons, the value handle is internally more than
122    just a number, it is a VAR_DECL named "value.x", where x is a
123    unique number for each value number in use.  This allows
124    expressions with SSA_NAMES replaced by value handles to still be
125    pretty printed in a sane way.  They simply print as "value.3 *
126    value.5", etc.  
127
128    Expression nodes have value handles associated with them as a
129    cache.  Otherwise, we'd have to look them up again in the hash
130    table This makes significant difference (factor of two or more) on
131    some test cases.  They can be thrown away after the pass is
132    finished.  */
133
134 /* Representation of expressions on value numbers: 
135
136    In some portions of this code, you will notice we allocate "fake"
137    analogues to the expression we are value numbering, and replace the
138    operands with the values of the expression.  Since we work on
139    values, and not just names, we canonicalize expressions to value
140    expressions for use in the ANTIC sets, the EXP_GEN set, etc.  
141
142    This is theoretically unnecessary, it just saves a bunch of
143    repeated get_value_handle and find_leader calls in the remainder of
144    the code, trading off temporary memory usage for speed.  The tree
145    nodes aren't actually creating more garbage, since they are
146    allocated in a special pools which are thrown away at the end of
147    this pass.  
148
149    All of this also means that if you print the EXP_GEN or ANTIC sets,
150    you will see "value.5 + value.7" in the set, instead of "a_55 +
151    b_66" or something.  The only thing that actually cares about
152    seeing the value leaders is phi translation, and it needs to be
153    able to find the leader for a value in an arbitrary block, so this
154    "value expression" form is perfect for it (otherwise you'd do
155    get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
156
157
158 /* Representation of sets:
159
160    There are currently two types of sets used, hopefully to be unified soon.
161    The AVAIL sets do not need to be sorted in any particular order,
162    and thus, are simply represented as two bitmaps, one that keeps
163    track of values present in the set, and one that keeps track of
164    expressions present in the set.
165    
166    The other sets are represented as doubly linked lists kept in topological
167    order, with an optional supporting bitmap of values present in the
168    set.  The sets represent values, and the elements can be values or
169    expressions.  The elements can appear in different sets, but each
170    element can only appear once in each set.
171
172    Since each node in the set represents a value, we also want to be
173    able to map expression, set pairs to something that tells us
174    whether the value is present is a set.  We use a per-set bitmap for
175    that.  The value handles also point to a linked list of the
176    expressions they represent via a tree annotation.  This is mainly
177    useful only for debugging, since we don't do identity lookups.  */
178
179
180 static bool in_fre = false;
181
182 /* A value set element.  Basically a single linked list of
183    expressions/values.  */
184 typedef struct value_set_node
185 {
186   /* An expression.  */
187   tree expr;
188
189   /* A pointer to the next element of the value set.  */
190   struct value_set_node *next;
191 } *value_set_node_t;
192
193
194 /* A value set.  This is a singly linked list of value_set_node
195    elements with a possible bitmap that tells us what values exist in
196    the set.  This set must be kept in topologically sorted order.  */
197 typedef struct value_set
198 {
199   /* The head of the list.  Used for iterating over the list in
200      order.  */
201   value_set_node_t head;
202
203   /* The tail of the list.  Used for tail insertions, which are
204      necessary to keep the set in topologically sorted order because
205      of how the set is built.  */
206   value_set_node_t tail;
207   
208   /* The length of the list.  */
209   size_t length;
210   
211   /* True if the set is indexed, which means it contains a backing
212      bitmap for quick determination of whether certain values exist in the
213      set.  */
214   bool indexed;
215   
216   /* The bitmap of values that exist in the set.  May be NULL in an
217      empty or non-indexed set.  */
218   bitmap values;
219   
220 } *value_set_t;
221
222
223 /* An unordered bitmap set.  One bitmap tracks values, the other,
224    expressions.  */
225 typedef struct bitmap_set
226 {
227   bitmap expressions;
228   bitmap values;
229 } *bitmap_set_t;
230
231 /* Sets that we need to keep track of.  */
232 typedef struct bb_value_sets
233 {
234   /* The EXP_GEN set, which represents expressions/values generated in
235      a basic block.  */
236   value_set_t exp_gen;
237
238   /* The PHI_GEN set, which represents PHI results generated in a
239      basic block.  */
240   bitmap_set_t phi_gen;
241
242   /* The TMP_GEN set, which represents results/temporaries generated
243      in a basic block. IE the LHS of an expression.  */
244   bitmap_set_t tmp_gen;
245
246   /* The AVAIL_OUT set, which represents which values are available in
247      a given basic block.  */
248   bitmap_set_t avail_out;
249
250   /* The ANTIC_IN set, which represents which values are anticiptable
251      in a given basic block.  */
252   value_set_t antic_in;
253
254   /* The NEW_SETS set, which is used during insertion to augment the
255      AVAIL_OUT set of blocks with the new insertions performed during
256      the current iteration.  */
257   bitmap_set_t new_sets;
258 } *bb_value_sets_t;
259
260 #define EXP_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->exp_gen
261 #define PHI_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->phi_gen
262 #define TMP_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->tmp_gen
263 #define AVAIL_OUT(BB)   ((bb_value_sets_t) ((BB)->aux))->avail_out
264 #define ANTIC_IN(BB)    ((bb_value_sets_t) ((BB)->aux))->antic_in
265 #define NEW_SETS(BB)    ((bb_value_sets_t) ((BB)->aux))->new_sets
266
267 /* This structure is used to keep track of statistics on what
268    optimization PRE was able to perform.  */
269 static struct
270 {
271   /* The number of RHS computations eliminated by PRE.  */
272   int eliminations;
273
274   /* The number of new expressions/temporaries generated by PRE.  */
275   int insertions;
276
277   /* The number of new PHI nodes added by PRE.  */
278   int phis;
279   
280   /* The number of values found constant.  */
281   int constified;
282   
283 } pre_stats;
284
285
286 static tree bitmap_find_leader (bitmap_set_t, tree);
287 static tree find_leader (value_set_t, tree);
288 static void value_insert_into_set (value_set_t, tree);
289 static void bitmap_value_insert_into_set (bitmap_set_t, tree);
290 static void bitmap_value_replace_in_set (bitmap_set_t, tree);
291 static void insert_into_set (value_set_t, tree);
292 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
293 static bool bitmap_set_contains_value (bitmap_set_t, tree);
294 static bitmap_set_t bitmap_set_new (void);
295 static value_set_t set_new  (bool);
296 static bool is_undefined_value (tree);
297 static tree create_expression_by_pieces (basic_block, tree, tree);
298
299
300 /* We can add and remove elements and entries to and from sets
301    and hash tables, so we use alloc pools for them.  */
302
303 static alloc_pool value_set_pool;
304 static alloc_pool bitmap_set_pool;
305 static alloc_pool value_set_node_pool;
306 static alloc_pool binary_node_pool;
307 static alloc_pool unary_node_pool;
308 static alloc_pool reference_node_pool;
309 static alloc_pool comparison_node_pool;
310 static alloc_pool expression_node_pool;
311 static alloc_pool list_node_pool;
312 static bitmap_obstack grand_bitmap_obstack;
313
314 /* Set of blocks with statements that have had its EH information
315    cleaned up.  */
316 static bitmap need_eh_cleanup;
317
318 /* The phi_translate_table caches phi translations for a given
319    expression and predecessor.  */
320
321 static htab_t phi_translate_table;
322
323 /* A three tuple {e, pred, v} used to cache phi translations in the
324    phi_translate_table.  */
325
326 typedef struct expr_pred_trans_d
327 {
328   /* The expression.  */
329   tree e;
330
331   /* The predecessor block along which we translated the expression.  */
332   basic_block pred;
333
334   /* The value that resulted from the translation.  */
335   tree v;
336
337   /* The hashcode for the expression, pred pair. This is cached for
338      speed reasons.  */
339   hashval_t hashcode;
340 } *expr_pred_trans_t;
341
342 /* Return the hash value for a phi translation table entry.  */
343
344 static hashval_t
345 expr_pred_trans_hash (const void *p)
346 {
347   const expr_pred_trans_t ve = (expr_pred_trans_t) p;
348   return ve->hashcode;
349 }
350
351 /* Return true if two phi translation table entries are the same.
352    P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
353
354 static int
355 expr_pred_trans_eq (const void *p1, const void *p2)
356 {
357   const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
358   const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
359   basic_block b1 = ve1->pred;
360   basic_block b2 = ve2->pred;
361
362   
363   /* If they are not translations for the same basic block, they can't
364      be equal.  */
365   if (b1 != b2)
366     return false;
367
368   /* If they are for the same basic block, determine if the
369      expressions are equal.  */  
370   if (expressions_equal_p (ve1->e, ve2->e))
371     return true;
372   
373   return false;
374 }
375
376 /* Search in the phi translation table for the translation of
377    expression E in basic block PRED. Return the translated value, if
378    found, NULL otherwise.  */ 
379
380 static inline tree
381 phi_trans_lookup (tree e, basic_block pred)
382 {
383   void **slot;
384   struct expr_pred_trans_d ept;
385   ept.e = e;
386   ept.pred = pred;
387   ept.hashcode = vn_compute (e, (unsigned long) pred, NULL);
388   slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
389                                    NO_INSERT);
390   if (!slot)
391     return NULL;
392   else
393     return ((expr_pred_trans_t) *slot)->v;
394 }
395
396
397 /* Add the tuple mapping from {expression E, basic block PRED} to
398    value V, to the phi translation table.  */
399
400 static inline void
401 phi_trans_add (tree e, tree v, basic_block pred)
402 {
403   void **slot;
404   expr_pred_trans_t new_pair = xmalloc (sizeof (*new_pair));
405   new_pair->e = e;
406   new_pair->pred = pred;
407   new_pair->v = v;
408   new_pair->hashcode = vn_compute (e, (unsigned long) pred, NULL);
409   slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
410                                    new_pair->hashcode, INSERT);
411   if (*slot)
412     free (*slot);
413   *slot = (void *) new_pair;
414 }
415
416
417 /* Add expression E to the expression set of value V.  */
418
419 void
420 add_to_value (tree v, tree e)
421 {
422   /* Constants have no expression sets.  */
423   if (is_gimple_min_invariant (v))
424     return;
425
426   if (VALUE_HANDLE_EXPR_SET (v) == NULL)
427     VALUE_HANDLE_EXPR_SET (v) = set_new (false);
428
429   insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
430 }
431
432
433 /* Return true if value V exists in the bitmap for SET.  */
434
435 static inline bool
436 value_exists_in_set_bitmap (value_set_t set, tree v)
437 {
438   if (!set->values)
439     return false;
440
441   return bitmap_bit_p (set->values, VALUE_HANDLE_ID (v));
442 }
443
444
445 /* Remove value V from the bitmap for SET.  */
446
447 static void
448 value_remove_from_set_bitmap (value_set_t set, tree v)
449 {
450   gcc_assert (set->indexed);
451
452   if (!set->values)
453     return;
454
455   bitmap_clear_bit (set->values, VALUE_HANDLE_ID (v));
456 }
457
458
459 /* Insert the value number V into the bitmap of values existing in
460    SET.  */
461
462 static inline void
463 value_insert_into_set_bitmap (value_set_t set, tree v)
464 {
465   gcc_assert (set->indexed);
466
467   if (set->values == NULL)
468     set->values = BITMAP_ALLOC (&grand_bitmap_obstack);
469
470   bitmap_set_bit (set->values, VALUE_HANDLE_ID (v));
471 }
472
473
474 /* Create a new bitmap set and return it.  */
475
476 static bitmap_set_t 
477 bitmap_set_new (void)
478 {
479   bitmap_set_t ret = pool_alloc (bitmap_set_pool);
480   ret->expressions = BITMAP_ALLOC (&grand_bitmap_obstack);
481   ret->values = BITMAP_ALLOC (&grand_bitmap_obstack);
482   return ret;
483 }
484
485 /* Create a new set.  */
486
487 static value_set_t
488 set_new  (bool indexed)
489 {
490   value_set_t ret;
491   ret = pool_alloc (value_set_pool);
492   ret->head = ret->tail = NULL;
493   ret->length = 0;
494   ret->indexed = indexed;
495   ret->values = NULL;
496   return ret;
497 }
498
499 /* Insert an expression EXPR into a bitmapped set.  */
500
501 static void
502 bitmap_insert_into_set (bitmap_set_t set, tree expr)
503 {
504   tree val;
505   /* XXX: For now, we only let SSA_NAMES into the bitmap sets.  */
506   gcc_assert (TREE_CODE (expr) == SSA_NAME);
507   val = get_value_handle (expr);
508   
509   gcc_assert (val);
510   if (!is_gimple_min_invariant (val))
511   {
512     bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
513     bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
514   }
515 }
516
517 /* Insert EXPR into SET.  */
518
519 static void
520 insert_into_set (value_set_t set, tree expr)
521 {
522   value_set_node_t newnode = pool_alloc (value_set_node_pool);
523   tree val = get_value_handle (expr);
524   gcc_assert (val);
525   
526   if (is_gimple_min_invariant (val))
527     return;
528
529   /* For indexed sets, insert the value into the set value bitmap.
530      For all sets, add it to the linked list and increment the list
531      length.  */
532   if (set->indexed)
533     value_insert_into_set_bitmap (set, val);
534
535   newnode->next = NULL;
536   newnode->expr = expr;
537   set->length ++;
538   if (set->head == NULL)
539     {
540       set->head = set->tail = newnode;
541     }
542   else
543     {
544       set->tail->next = newnode;
545       set->tail = newnode;
546     }
547 }
548
549 /* Copy a bitmapped set ORIG, into bitmapped set DEST.  */
550
551 static void
552 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
553 {
554   bitmap_copy (dest->expressions, orig->expressions);
555   bitmap_copy (dest->values, orig->values);
556 }
557
558 /* Perform bitmapped set operation DEST &= ORIG.  */
559
560 static void
561 bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig)
562 {
563   bitmap_iterator bi;
564   unsigned int i;
565   bitmap temp = BITMAP_ALLOC (&grand_bitmap_obstack);
566
567   bitmap_and_into (dest->values, orig->values);
568   bitmap_copy (temp, dest->expressions);
569   EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
570     {
571       tree name = ssa_name (i);
572       tree val = get_value_handle (name);
573       if (!bitmap_bit_p (dest->values, VALUE_HANDLE_ID (val)))
574         bitmap_clear_bit (dest->expressions, i);
575     }
576
577 }
578
579 /* Perform bitmapped value set operation DEST = DEST & ~ORIG.  */
580
581 static void
582 bitmap_set_and_compl (bitmap_set_t dest, bitmap_set_t orig)
583 {
584   bitmap_iterator bi;
585   unsigned int i;
586   bitmap temp = BITMAP_ALLOC (&grand_bitmap_obstack);
587
588   bitmap_and_compl_into (dest->values, orig->values);
589   bitmap_copy (temp, dest->expressions);
590   EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
591     {
592       tree name = ssa_name (i);
593       tree val = get_value_handle (name);
594       if (!bitmap_bit_p (dest->values, VALUE_HANDLE_ID (val)))
595         bitmap_clear_bit (dest->expressions, i);
596     }
597 }
598
599 /* Return true if the bitmap set SET is empty.  */
600
601 static bool
602 bitmap_set_empty_p (bitmap_set_t set)
603 {
604   return bitmap_empty_p (set->values);
605 }
606
607 /* Copy the set ORIG to the set DEST.  */
608
609 static void
610 set_copy (value_set_t dest, value_set_t orig)
611 {
612   value_set_node_t node;
613  
614   if (!orig || !orig->head)
615     return;
616
617   for (node = orig->head;
618        node;
619        node = node->next)
620     {
621       insert_into_set (dest, node->expr);
622     }
623 }
624
625 /* Remove EXPR from SET.  */
626
627 static void
628 set_remove (value_set_t set, tree expr)
629 {
630   value_set_node_t node, prev;
631
632   /* Remove the value of EXPR from the bitmap, decrement the set
633      length, and remove it from the actual double linked list.  */ 
634   value_remove_from_set_bitmap (set, get_value_handle (expr));
635   set->length--;
636   prev = NULL;
637   for (node = set->head; 
638        node != NULL; 
639        prev = node, node = node->next)
640     {
641       if (node->expr == expr)
642         {
643           if (prev == NULL)
644             set->head = node->next;
645           else
646             prev->next= node->next;
647  
648           if (node == set->tail)
649             set->tail = prev;
650           pool_free (value_set_node_pool, node);
651           return;
652         }
653     }
654 }
655
656 /* Return true if SET contains the value VAL.  */
657
658 static bool
659 set_contains_value (value_set_t set, tree val)
660 {
661   /* All constants are in every set.  */
662   if (is_gimple_min_invariant (val))
663     return true;
664   
665   if (set->length == 0)
666     return false;
667   
668   return value_exists_in_set_bitmap (set, val);
669 }
670
671 /* Return true if bitmapped set SET contains the expression EXPR.  */
672 static bool
673 bitmap_set_contains (bitmap_set_t set, tree expr)
674 {
675   /* All constants are in every set.  */
676   if (is_gimple_min_invariant (get_value_handle (expr)))
677     return true;
678
679   /* XXX: Bitmapped sets only contain SSA_NAME's for now.  */
680   if (TREE_CODE (expr) != SSA_NAME)
681     return false;
682   return bitmap_bit_p (set->expressions, SSA_NAME_VERSION (expr));
683 }
684
685   
686 /* Return true if bitmapped set SET contains the value VAL.  */
687
688 static bool
689 bitmap_set_contains_value (bitmap_set_t set, tree val)
690 {
691   if (is_gimple_min_invariant (val))
692     return true;
693   return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
694 }
695
696 /* Replace an instance of value LOOKFOR with expression EXPR in SET.  */
697
698 static void
699 bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
700 {
701   value_set_t exprset;
702   value_set_node_t node;
703   if (is_gimple_min_invariant (lookfor))
704     return;
705   if (!bitmap_set_contains_value (set, lookfor))
706     return;
707
708   /* The number of expressions having a given value is usually
709      significantly less than the total number of expressions in SET.
710      Thus, rather than check, for each expression in SET, whether it
711      has the value LOOKFOR, we walk the reverse mapping that tells us
712      what expressions have a given value, and see if any of those
713      expressions are in our set.  For large testcases, this is about
714      5-10x faster than walking the bitmap.  If this is somehow a
715      significant lose for some cases, we can choose which set to walk
716      based on the set size.  */
717   exprset = VALUE_HANDLE_EXPR_SET (lookfor);
718   for (node = exprset->head; node; node = node->next)
719     {
720       if (TREE_CODE (node->expr) == SSA_NAME)
721         {
722           if (bitmap_bit_p (set->expressions, SSA_NAME_VERSION (node->expr)))
723             {
724               bitmap_clear_bit (set->expressions, SSA_NAME_VERSION (node->expr));
725               bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
726               return;
727             }
728         }
729     }
730 }
731
732 /* Subtract bitmapped set B from value set A, and return the new set.  */
733
734 static value_set_t
735 bitmap_set_subtract_from_value_set (value_set_t a, bitmap_set_t b,
736                                     bool indexed)
737 {
738   value_set_t ret = set_new (indexed);
739   value_set_node_t node;
740   for (node = a->head;
741        node;
742        node = node->next)
743     {
744       if (!bitmap_set_contains (b, node->expr))
745         insert_into_set (ret, node->expr);
746     }
747   return ret;
748 }
749
750 /* Return true if two sets are equal.  */
751
752 static bool
753 set_equal (value_set_t a, value_set_t b)
754 {
755   value_set_node_t node;
756
757   if (a->length != b->length)
758     return false;
759   for (node = a->head;
760        node;
761        node = node->next)
762     {
763       if (!set_contains_value (b, get_value_handle (node->expr)))
764         return false;
765     }
766   return true;
767 }
768
769 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
770    and add it otherwise.  */
771
772 static void
773 bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
774 {
775   tree val = get_value_handle (expr);
776   if (bitmap_set_contains_value (set, val))
777     bitmap_set_replace_value (set, val, expr);
778   else
779     bitmap_insert_into_set (set, expr);
780 }
781
782 /* Insert EXPR into SET if EXPR's value is not already present in
783    SET.  */
784
785 static void
786 bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
787 {
788   tree val = get_value_handle (expr);
789
790   if (is_gimple_min_invariant (val))
791     return;
792   
793   if (!bitmap_set_contains_value (set, val))
794     bitmap_insert_into_set (set, expr);
795 }
796
797 /* Insert the value for EXPR into SET, if it doesn't exist already.  */
798
799 static void
800 value_insert_into_set (value_set_t set, tree expr)
801 {
802   tree val = get_value_handle (expr);
803
804   /* Constant and invariant values exist everywhere, and thus,
805      actually keeping them in the sets is pointless.  */
806   if (is_gimple_min_invariant (val))
807     return;
808
809   if (!set_contains_value (set, val))
810     insert_into_set (set, expr);
811 }
812
813
814 /* Print out SET to OUTFILE.  */
815
816 static void
817 bitmap_print_value_set (FILE *outfile, bitmap_set_t set,
818                         const char *setname, int blockindex)
819 {
820   fprintf (outfile, "%s[%d] := { ", setname, blockindex);
821   if (set)
822     {
823       bool first = true;
824       unsigned i;
825       bitmap_iterator bi;
826
827       EXECUTE_IF_SET_IN_BITMAP (set->expressions, 0, i, bi)
828         {
829           if (!first)
830             fprintf (outfile, ", ");
831           first = false;
832           print_generic_expr (outfile, ssa_name (i), 0);
833         
834           fprintf (outfile, " (");
835           print_generic_expr (outfile, get_value_handle (ssa_name (i)), 0);
836           fprintf (outfile, ") ");
837         }
838     }
839   fprintf (outfile, " }\n");
840 }
841 /* Print out the value_set SET to OUTFILE.  */
842
843 static void
844 print_value_set (FILE *outfile, value_set_t set,
845                  const char *setname, int blockindex)
846 {
847   value_set_node_t node;
848   fprintf (outfile, "%s[%d] := { ", setname, blockindex);
849   if (set)
850     {
851       for (node = set->head;
852            node;
853            node = node->next)
854         {
855           print_generic_expr (outfile, node->expr, 0);
856           
857           fprintf (outfile, " (");
858           print_generic_expr (outfile, get_value_handle (node->expr), 0);
859           fprintf (outfile, ") ");
860                      
861           if (node->next)
862             fprintf (outfile, ", ");
863         }
864     }
865
866   fprintf (outfile, " }\n");
867 }
868
869 /* Print out the expressions that have VAL to OUTFILE.  */
870
871 void
872 print_value_expressions (FILE *outfile, tree val)
873 {
874   if (VALUE_HANDLE_EXPR_SET (val))
875     {
876       char s[10];
877       sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
878       print_value_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
879     }
880 }
881
882
883 void
884 debug_value_expressions (tree val)
885 {
886   print_value_expressions (stderr, val);
887 }
888
889   
890 void debug_value_set (value_set_t, const char *, int);
891
892 void
893 debug_value_set (value_set_t set, const char *setname, int blockindex)
894 {
895   print_value_set (stderr, set, setname, blockindex);
896 }
897
898 /* Return the folded version of T if T, when folded, is a gimple
899    min_invariant.  Otherwise, return T.  */ 
900
901 static tree
902 fully_constant_expression (tree t)
903 {  
904   tree folded;
905   folded = fold (t);
906   if (folded && is_gimple_min_invariant (folded))
907     return folded;
908   return t;
909 }
910
911 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
912    For example, this can copy a list made of TREE_LIST nodes.  
913    Allocates the nodes in list_node_pool*/
914
915 static tree
916 pool_copy_list (tree list)
917 {
918   tree head;
919   tree prev, next;
920
921   if (list == 0)
922     return 0;
923   head = pool_alloc (list_node_pool);
924   
925   memcpy (head, list, tree_size (list));
926   prev = head;
927   
928   next = TREE_CHAIN (list);
929   while (next)
930     {
931       TREE_CHAIN (prev) = pool_alloc (list_node_pool);
932       memcpy (TREE_CHAIN (prev), next, tree_size (next));
933       prev = TREE_CHAIN (prev);
934       next = TREE_CHAIN (next);
935     }
936   return head;
937 }
938
939
940 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
941    the phis in PRED.  Return NULL if we can't find a leader for each
942    part of the translated expression.  */
943
944 static tree
945 phi_translate (tree expr, value_set_t set, basic_block pred,
946                basic_block phiblock)
947 {
948   tree phitrans = NULL;
949   tree oldexpr = expr;
950   
951   if (expr == NULL)
952     return NULL;
953
954   if (is_gimple_min_invariant (expr))
955     return expr;
956
957   /* Phi translations of a given expression don't change.  */
958   phitrans = phi_trans_lookup (expr, pred);
959   if (phitrans)
960     return phitrans;
961   
962   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
963     {
964     case tcc_expression:
965       {
966         if (TREE_CODE (expr) != CALL_EXPR)
967           return NULL;
968         else
969           {
970             tree oldop0 = TREE_OPERAND (expr, 0);
971             tree oldarglist = TREE_OPERAND (expr, 1);
972             tree oldop2 = TREE_OPERAND (expr, 2);
973             tree newop0;
974             tree newarglist;
975             tree newop2 = NULL;
976             tree oldwalker;
977             tree newwalker;
978             tree newexpr;
979             bool listchanged = false;
980
981             /* Call expressions are kind of weird because they have an
982                argument list.  We don't want to value number the list
983                as one value number, because that doesn't make much
984                sense, and just breaks the support functions we call,
985                which expect TREE_OPERAND (call_expr, 2) to be a
986                TREE_LIST. */          
987             
988             newop0 = phi_translate (find_leader (set, oldop0),
989                                     set, pred, phiblock);
990             if (newop0 == NULL)
991               return NULL;
992             if (oldop2)
993               {
994                 newop2 = phi_translate (find_leader (set, oldop2),
995                                         set, pred, phiblock);
996                 if (newop2 == NULL)
997                   return NULL;
998               }
999
1000             /* phi translate the argument list piece by piece.
1001                
1002               We could actually build the list piece by piece here,
1003               but it's likely to not be worth the memory we will save,
1004               unless you have millions of call arguments.  */
1005
1006             newarglist = pool_copy_list (oldarglist);
1007             for (oldwalker = oldarglist, newwalker = newarglist;
1008                  oldwalker && newwalker;
1009                  oldwalker = TREE_CHAIN (oldwalker), 
1010                    newwalker = TREE_CHAIN (newwalker))
1011               {
1012                 
1013                 tree oldval = TREE_VALUE (oldwalker);
1014                 tree newval;
1015                 if (oldval)
1016                   {
1017                     newval = phi_translate (find_leader (set, oldval),
1018                                             set, pred, phiblock);
1019                     if (newval == NULL)
1020                       return NULL;
1021                     if (newval != oldval)
1022                       {
1023                         listchanged = true;
1024                         TREE_VALUE (newwalker) = get_value_handle (newval);
1025                       }
1026                   }
1027               }
1028             if (listchanged)
1029               vn_lookup_or_add (newarglist, NULL);
1030             
1031             if (listchanged || (newop0 != oldop0) || (oldop2 != newop2))
1032               {
1033                 newexpr = pool_alloc (expression_node_pool);
1034                 memcpy (newexpr, expr, tree_size (expr));
1035                 TREE_OPERAND (newexpr, 0) = newop0 == oldop0 ? oldop0 : get_value_handle (newop0);
1036                 TREE_OPERAND (newexpr, 1) = listchanged ? newarglist : oldarglist;
1037                 TREE_OPERAND (newexpr, 2) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
1038                 create_tree_ann (newexpr);       
1039                 vn_lookup_or_add (newexpr, NULL);
1040                 expr = newexpr;
1041                 phi_trans_add (oldexpr, newexpr, pred);
1042               }
1043           }
1044       }
1045       return expr;
1046
1047     case tcc_reference:
1048       /* XXX: Until we have PRE of loads working, none will be ANTIC.  */
1049       return NULL;
1050
1051     case tcc_binary:
1052     case tcc_comparison:
1053       {
1054         tree oldop1 = TREE_OPERAND (expr, 0);
1055         tree oldop2 = TREE_OPERAND (expr, 1);
1056         tree newop1;
1057         tree newop2;
1058         tree newexpr;
1059         
1060         newop1 = phi_translate (find_leader (set, oldop1),
1061                                 set, pred, phiblock);
1062         if (newop1 == NULL)
1063           return NULL;
1064         newop2 = phi_translate (find_leader (set, oldop2),
1065                                 set, pred, phiblock);
1066         if (newop2 == NULL)
1067           return NULL;
1068         if (newop1 != oldop1 || newop2 != oldop2)
1069           {
1070             tree t;
1071             newexpr = pool_alloc (binary_node_pool);
1072             memcpy (newexpr, expr, tree_size (expr));
1073             TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
1074             TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
1075             t = fully_constant_expression (newexpr);
1076             if (t != newexpr)
1077               {
1078                 pool_free (binary_node_pool, newexpr);
1079                 newexpr = t;
1080               }
1081             else
1082               {
1083                 create_tree_ann (newexpr);       
1084                 vn_lookup_or_add (newexpr, NULL);
1085               }
1086             expr = newexpr;
1087             phi_trans_add (oldexpr, newexpr, pred);         
1088           }
1089       }
1090       return expr;
1091
1092     case tcc_unary:
1093       {
1094         tree oldop1 = TREE_OPERAND (expr, 0);
1095         tree newop1;
1096         tree newexpr;
1097
1098         newop1 = phi_translate (find_leader (set, oldop1),
1099                                 set, pred, phiblock);
1100         if (newop1 == NULL)
1101           return NULL;
1102         if (newop1 != oldop1)
1103           {
1104             tree t;
1105             newexpr = pool_alloc (unary_node_pool);
1106             memcpy (newexpr, expr, tree_size (expr));
1107             TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
1108             t = fully_constant_expression (newexpr);
1109             if (t != newexpr)
1110               {
1111                 pool_free (unary_node_pool, newexpr);
1112                 newexpr = t;
1113               }
1114             else
1115               {
1116                 create_tree_ann (newexpr);       
1117                 vn_lookup_or_add (newexpr, NULL);
1118               }
1119             expr = newexpr;
1120             phi_trans_add (oldexpr, newexpr, pred);
1121           }
1122       }
1123       return expr;
1124
1125     case tcc_exceptional:
1126       {
1127         tree phi = NULL;
1128         edge e;
1129         gcc_assert (TREE_CODE (expr) == SSA_NAME);
1130         if (TREE_CODE (SSA_NAME_DEF_STMT (expr)) == PHI_NODE)
1131           phi = SSA_NAME_DEF_STMT (expr);
1132         else
1133           return expr;
1134         
1135         e = find_edge (pred, bb_for_stmt (phi));
1136         if (e)
1137           {
1138             if (is_undefined_value (PHI_ARG_DEF (phi, e->dest_idx)))
1139               return NULL;
1140             vn_lookup_or_add (PHI_ARG_DEF (phi, e->dest_idx), NULL);
1141             return PHI_ARG_DEF (phi, e->dest_idx);
1142           }
1143       }
1144       return expr;
1145
1146     default:
1147       gcc_unreachable ();
1148     }
1149 }
1150
1151 /* For each expression in SET, translate the value handles through phi nodes
1152    in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1153    expressions in DEST.  */
1154
1155 static void
1156 phi_translate_set (value_set_t dest, value_set_t set, basic_block pred,
1157                    basic_block phiblock)
1158 {
1159   value_set_node_t node;
1160   for (node = set->head;
1161        node;
1162        node = node->next)
1163     {
1164       tree translated;
1165       translated = phi_translate (node->expr, set, pred, phiblock);
1166       phi_trans_add (node->expr, translated, pred);
1167       
1168       if (translated != NULL)
1169         value_insert_into_set (dest, translated);
1170     } 
1171 }
1172
1173 /* Find the leader for a value (i.e., the name representing that
1174    value) in a given set, and return it.  Return NULL if no leader is
1175    found.  */
1176
1177 static tree
1178 bitmap_find_leader (bitmap_set_t set, tree val)
1179 {
1180   if (val == NULL)
1181     return NULL;
1182   
1183   if (is_gimple_min_invariant (val))
1184     return val;
1185   if (bitmap_set_contains_value (set, val))
1186     {
1187       /* Rather than walk the entire bitmap of expressions, and see
1188          whether any of them has the value we are looking for, we look
1189          at the reverse mapping, which tells us the set of expressions
1190          that have a given value (IE value->expressions with that
1191          value) and see if any of those expressions are in our set.
1192          The number of expressions per value is usually significantly
1193          less than the number of expressions in the set.  In fact, for
1194          large testcases, doing it this way is roughly 5-10x faster
1195          than walking the bitmap.
1196          If this is somehow a significant lose for some cases, we can
1197          choose which set to walk based on which set is smaller.  */     
1198       value_set_t exprset;
1199       value_set_node_t node;
1200       exprset = VALUE_HANDLE_EXPR_SET (val);
1201       for (node = exprset->head; node; node = node->next)
1202         {
1203           if (TREE_CODE (node->expr) == SSA_NAME)
1204             {
1205               if (bitmap_bit_p (set->expressions, 
1206                                 SSA_NAME_VERSION (node->expr)))
1207                 return node->expr;
1208             }
1209         }
1210     }
1211   return NULL;
1212 }
1213
1214         
1215 /* Find the leader for a value (i.e., the name representing that
1216    value) in a given set, and return it.  Return NULL if no leader is
1217    found.  */
1218
1219 static tree
1220 find_leader (value_set_t set, tree val)
1221 {
1222   value_set_node_t node;
1223
1224   if (val == NULL)
1225     return NULL;
1226
1227   /* Constants represent themselves.  */
1228   if (is_gimple_min_invariant (val))
1229     return val;
1230
1231   if (set->length == 0)
1232     return NULL;
1233   
1234   if (value_exists_in_set_bitmap (set, val))
1235     {
1236       for (node = set->head;
1237            node;
1238            node = node->next)
1239         {
1240           if (get_value_handle (node->expr) == val)
1241             return node->expr;
1242         }
1243     }
1244
1245   return NULL;
1246 }
1247
1248 /* Determine if the expression EXPR is valid in SET.  This means that
1249    we have a leader for each part of the expression (if it consists of
1250    values), or the expression is an SSA_NAME.  
1251
1252    NB: We never should run into a case where we have SSA_NAME +
1253    SSA_NAME or SSA_NAME + value.  The sets valid_in_set is called on,
1254    the ANTIC sets, will only ever have SSA_NAME's or value expressions
1255    (IE VALUE1 + VALUE2, *VALUE1, VALUE1 < VALUE2)  */
1256
1257 static bool
1258 valid_in_set (value_set_t set, tree expr)
1259 {
1260   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1261     {
1262     case tcc_binary:
1263     case tcc_comparison:
1264       {
1265         tree op1 = TREE_OPERAND (expr, 0);
1266         tree op2 = TREE_OPERAND (expr, 1);
1267         return set_contains_value (set, op1) && set_contains_value (set, op2);
1268       }
1269
1270     case tcc_unary:
1271       {
1272         tree op1 = TREE_OPERAND (expr, 0);
1273         return set_contains_value (set, op1);
1274       }
1275       
1276     case tcc_expression:
1277       {
1278         if (TREE_CODE (expr) == CALL_EXPR)
1279           {
1280             tree op0 = TREE_OPERAND (expr, 0);
1281             tree arglist = TREE_OPERAND (expr, 1);
1282             tree op2 = TREE_OPERAND (expr, 2);
1283
1284             /* Check the non-list operands first.  */
1285             if (!set_contains_value (set, op0)
1286                 || (op2 && !set_contains_value (set, op2)))
1287               return false;
1288
1289             /* Now check the operands.  */
1290             for (; arglist; arglist = TREE_CHAIN (arglist))
1291               {
1292                 if (!set_contains_value (set, TREE_VALUE (arglist)))
1293                   return false;
1294               }
1295             return true;
1296           }
1297         return false;
1298       }
1299       
1300     case tcc_reference:
1301       /* XXX: Until PRE of loads works, no reference nodes are ANTIC.  */
1302       return false;
1303
1304     case tcc_exceptional:
1305       gcc_assert (TREE_CODE (expr) == SSA_NAME);
1306       return true;
1307
1308     case tcc_declaration:
1309       /* VAR_DECL and PARM_DECL are never anticipatable.  */
1310       return false;
1311
1312     default:
1313       /* No other cases should be encountered.  */
1314       gcc_unreachable (); 
1315    }
1316 }
1317
1318 /* Clean the set of expressions that are no longer valid in SET.  This
1319    means expressions that are made up of values we have no leaders for
1320    in SET.  */
1321
1322 static void
1323 clean (value_set_t set)
1324 {
1325   value_set_node_t node;
1326   value_set_node_t next;
1327   node = set->head;
1328   while (node)
1329     {
1330       next = node->next;
1331       if (!valid_in_set (set, node->expr))      
1332         set_remove (set, node->expr);
1333       node = next;
1334     }
1335 }
1336
1337 DEF_VEC_P (basic_block);
1338 DEF_VEC_ALLOC_P (basic_block, heap);
1339 static sbitmap has_abnormal_preds;
1340
1341 /* Compute the ANTIC set for BLOCK.
1342
1343    If succs(BLOCK) > 1 then
1344      ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
1345    else if succs(BLOCK) == 1 then
1346      ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
1347
1348    ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
1349
1350    XXX: It would be nice to either write a set_clear, and use it for
1351    ANTIC_OUT, or to mark the antic_out set as deleted at the end
1352    of this routine, so that the pool can hand the same memory back out
1353    again for the next ANTIC_OUT.  */
1354
1355 static bool
1356 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
1357 {
1358   basic_block son;
1359   bool changed = false;
1360   value_set_t S, old, ANTIC_OUT;
1361   value_set_node_t node;
1362
1363   ANTIC_OUT = S = NULL;
1364
1365   /* If any edges from predecessors are abnormal, antic_in is empty,
1366      so do nothing.  */
1367   if (block_has_abnormal_pred_edge)
1368     goto maybe_dump_sets;
1369
1370   old = set_new (false);
1371   set_copy (old, ANTIC_IN (block));
1372   ANTIC_OUT = set_new (true);
1373
1374   /* If the block has no successors, ANTIC_OUT is empty.  */
1375   if (EDGE_COUNT (block->succs) == 0)
1376     ;
1377   /* If we have one successor, we could have some phi nodes to
1378      translate through.  */
1379   else if (single_succ_p (block))
1380     {
1381       phi_translate_set (ANTIC_OUT, ANTIC_IN (single_succ (block)),
1382                          block, single_succ (block));
1383     }
1384   /* If we have multiple successors, we take the intersection of all of
1385      them.  */
1386   else
1387     {
1388       VEC(basic_block, heap) * worklist;
1389       edge e;
1390       size_t i;
1391       basic_block bprime, first;
1392       edge_iterator ei;
1393
1394       worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
1395       FOR_EACH_EDGE (e, ei, block->succs)
1396         VEC_quick_push (basic_block, worklist, e->dest);
1397       first = VEC_index (basic_block, worklist, 0);
1398       set_copy (ANTIC_OUT, ANTIC_IN (first));
1399
1400       for (i = 1; VEC_iterate (basic_block, worklist, i, bprime); i++)
1401         {
1402           node = ANTIC_OUT->head;
1403           while (node)
1404             {
1405               tree val;
1406               value_set_node_t next = node->next;
1407               val = get_value_handle (node->expr);
1408               if (!set_contains_value (ANTIC_IN (bprime), val))
1409                 set_remove (ANTIC_OUT, node->expr);
1410               node = next;
1411             }
1412         }
1413       VEC_free (basic_block, heap, worklist);
1414     }
1415
1416   /* Generate ANTIC_OUT - TMP_GEN.  */
1417   S = bitmap_set_subtract_from_value_set (ANTIC_OUT, TMP_GEN (block), false);
1418
1419   /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1420   ANTIC_IN (block) = bitmap_set_subtract_from_value_set (EXP_GEN (block), 
1421                                                          TMP_GEN (block),
1422                                                          true);
1423
1424   /* Then union in the ANTIC_OUT - TMP_GEN values,
1425      to get ANTIC_OUT U EXP_GEN - TMP_GEN */
1426   for (node = S->head; node; node = node->next)
1427     value_insert_into_set (ANTIC_IN (block), node->expr);
1428
1429   clean (ANTIC_IN (block));
1430   if (!set_equal (old, ANTIC_IN (block)))
1431     changed = true;
1432
1433  maybe_dump_sets:
1434   if (dump_file && (dump_flags & TDF_DETAILS))
1435     {
1436       if (ANTIC_OUT)
1437         print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1438       print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1439       if (S)
1440         print_value_set (dump_file, S, "S", block->index);
1441     }
1442
1443   for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1444        son;
1445        son = next_dom_son (CDI_POST_DOMINATORS, son))
1446     {
1447       changed |= compute_antic_aux (son,
1448                                     TEST_BIT (has_abnormal_preds, son->index));
1449     }
1450   return changed;
1451 }
1452
1453 /* Compute ANTIC sets.  */
1454
1455 static void
1456 compute_antic (void)
1457 {
1458   bool changed = true;
1459   int num_iterations = 0;
1460   basic_block block;
1461
1462   /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
1463      We pre-build the map of blocks with incoming abnormal edges here.  */
1464   has_abnormal_preds = sbitmap_alloc (last_basic_block);
1465   sbitmap_zero (has_abnormal_preds);
1466   FOR_EACH_BB (block)
1467     {
1468       edge_iterator ei;
1469       edge e;
1470
1471       FOR_EACH_EDGE (e, ei, block->preds)
1472         if (e->flags & EDGE_ABNORMAL)
1473           {
1474             SET_BIT (has_abnormal_preds, block->index);
1475             break;
1476           }
1477
1478       /* While we are here, give empty ANTIC_IN sets to each block.  */
1479       ANTIC_IN (block) = set_new (true);
1480     }
1481   /* At the exit block we anticipate nothing.  */
1482   ANTIC_IN (EXIT_BLOCK_PTR) = set_new (true);
1483
1484   while (changed)
1485     {
1486       num_iterations++;
1487       changed = false;
1488       changed = compute_antic_aux (EXIT_BLOCK_PTR, false);
1489     }
1490
1491   sbitmap_free (has_abnormal_preds);
1492
1493   if (dump_file && (dump_flags & TDF_STATS))
1494     fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1495 }
1496
1497 static VEC(tree,heap) *inserted_exprs;
1498 /* Find a leader for an expression, or generate one using
1499    create_expression_by_pieces if it's ANTIC but
1500    complex.  
1501    BLOCK is the basic_block we are looking for leaders in.
1502    EXPR is the expression to find a leader or generate for. 
1503    STMTS is the statement list to put the inserted expressions on.
1504    Returns the SSA_NAME of the LHS of the generated expression or the
1505    leader.  */
1506
1507 static tree
1508 find_or_generate_expression (basic_block block, tree expr, tree stmts)
1509 {
1510   tree genop = bitmap_find_leader (AVAIL_OUT (block), expr);
1511
1512   /* If it's still NULL, it must be a complex expression, so generate
1513      it recursively.  */
1514   if (genop == NULL)
1515     {
1516       genop = VALUE_HANDLE_EXPR_SET (expr)->head->expr;
1517       gcc_assert (UNARY_CLASS_P (genop)
1518                   || BINARY_CLASS_P (genop)
1519                   || COMPARISON_CLASS_P (genop)
1520                   || REFERENCE_CLASS_P (genop)
1521                   || TREE_CODE (genop) == CALL_EXPR);
1522       genop = create_expression_by_pieces (block, genop, stmts);
1523     }
1524   return genop;
1525 }
1526
1527 #define NECESSARY(stmt)         stmt->common.asm_written_flag  
1528 /* Create an expression in pieces, so that we can handle very complex
1529    expressions that may be ANTIC, but not necessary GIMPLE.  
1530    BLOCK is the basic block the expression will be inserted into,
1531    EXPR is the expression to insert (in value form)
1532    STMTS is a statement list to append the necessary insertions into.
1533
1534    This function will die if we hit some value that shouldn't be
1535    ANTIC but is (IE there is no leader for it, or its components).
1536    This function may also generate expressions that are themselves
1537    partially or fully redundant.  Those that are will be either made
1538    fully redundant during the next iteration of insert (for partially
1539    redundant ones), or eliminated by eliminate (for fully redundant
1540    ones).  */
1541
1542 static tree
1543 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
1544 {
1545   tree temp, name;
1546   tree folded, forced_stmts, newexpr;
1547   tree v;
1548   tree_stmt_iterator tsi;
1549
1550   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1551     {
1552     case tcc_expression:
1553       {
1554         tree op0, op2;
1555         tree arglist;
1556         tree genop0, genop2;
1557         tree genarglist;
1558         tree walker, genwalker;
1559         
1560         gcc_assert (TREE_CODE (expr) == CALL_EXPR);
1561         genop2 = NULL;
1562         
1563         op0 = TREE_OPERAND (expr, 0);
1564         arglist = TREE_OPERAND (expr, 1);
1565         op2 = TREE_OPERAND (expr, 2);
1566         
1567         genop0 = find_or_generate_expression (block, op0, stmts);
1568         genarglist = copy_list (arglist);
1569         for (walker = arglist, genwalker = genarglist;
1570              genwalker && walker;
1571              genwalker = TREE_CHAIN (genwalker), walker = TREE_CHAIN (walker))
1572           {
1573             TREE_VALUE (genwalker) = find_or_generate_expression (block, 
1574                                                                   TREE_VALUE (walker), 
1575                                                                   stmts);
1576           }
1577
1578         if (op2)          
1579           genop2 = find_or_generate_expression (block, op2, stmts);
1580         folded = fold_build3 (TREE_CODE (expr), TREE_TYPE (expr),
1581                               genop0, genarglist, genop2);
1582         break;
1583         
1584         
1585       }
1586       break;
1587       
1588     case tcc_binary:
1589     case tcc_comparison:
1590       {
1591         tree op1 = TREE_OPERAND (expr, 0);
1592         tree op2 = TREE_OPERAND (expr, 1);
1593         tree genop1 = find_or_generate_expression (block, op1, stmts);
1594         tree genop2 = find_or_generate_expression (block, op2, stmts);
1595         folded = fold_build2 (TREE_CODE (expr), TREE_TYPE (expr), 
1596                               genop1, genop2);
1597         break;
1598       }
1599
1600     case tcc_unary:
1601       {
1602         tree op1 = TREE_OPERAND (expr, 0);
1603         tree genop1 = find_or_generate_expression (block, op1, stmts);
1604         folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr), 
1605                               genop1);
1606         break;
1607       }
1608
1609     default:
1610       gcc_unreachable ();
1611     }
1612
1613   /* Force the generated expression to be a sequence of GIMPLE
1614      statements.
1615      We have to call unshare_expr because force_gimple_operand may
1616      modify the tree we pass to it.  */
1617   newexpr = force_gimple_operand (unshare_expr (folded), &forced_stmts, 
1618                                   false, NULL);
1619
1620   /* If we have any intermediate expressions to the value sets, add them
1621      to the value sets and chain them on in the instruction stream.  */
1622   if (forced_stmts)
1623     {
1624       tsi = tsi_start (forced_stmts);
1625       for (; !tsi_end_p (tsi); tsi_next (&tsi))
1626         {
1627           tree stmt = tsi_stmt (tsi);
1628           tree forcedname = TREE_OPERAND (stmt, 0);
1629           tree forcedexpr = TREE_OPERAND (stmt, 1);
1630           tree val = vn_lookup_or_add (forcedexpr, NULL);
1631           
1632           VEC_safe_push (tree, heap, inserted_exprs, stmt);
1633           vn_add (forcedname, val, NULL);
1634           bitmap_value_replace_in_set (NEW_SETS (block), forcedname);
1635           bitmap_value_replace_in_set (AVAIL_OUT (block), forcedname);
1636         }
1637       tsi = tsi_last (stmts);
1638       tsi_link_after (&tsi, forced_stmts, TSI_CONTINUE_LINKING);
1639     }
1640
1641   /* Build and insert the assignment of the end result to the temporary
1642      that we will return.  */
1643   temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1644   add_referenced_tmp_var (temp);
1645   if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
1646     DECL_COMPLEX_GIMPLE_REG_P (temp) = 1;
1647   newexpr = build2 (MODIFY_EXPR, TREE_TYPE (expr), temp, newexpr);
1648   name = make_ssa_name (temp, newexpr);
1649   TREE_OPERAND (newexpr, 0) = name;
1650   NECESSARY (newexpr) = 0;
1651   tsi = tsi_last (stmts);
1652   tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1653   VEC_safe_push (tree, heap, inserted_exprs, newexpr);
1654
1655   /* Add a value handle to the temporary.
1656      The value may already exist in either NEW_SETS, or AVAIL_OUT, because
1657      we are creating the expression by pieces, and this particular piece of
1658      the expression may have been represented.  There is no harm in replacing
1659      here.  */
1660   v = get_value_handle (expr);
1661   vn_add (name, v, NULL);
1662   bitmap_value_replace_in_set (NEW_SETS (block), name); 
1663   bitmap_value_replace_in_set (AVAIL_OUT (block), name);
1664
1665   pre_stats.insertions++;
1666   if (dump_file && (dump_flags & TDF_DETAILS))
1667     {                               
1668       fprintf (dump_file, "Inserted ");
1669       print_generic_expr (dump_file, newexpr, 0);
1670       fprintf (dump_file, " in predecessor %d\n", block->index);
1671     }
1672
1673   return name;
1674 }
1675
1676 /* Insert the to-be-made-available values of NODE for each predecessor, stored
1677    in AVAIL, into the predecessors of BLOCK, and merge the result with a phi
1678    node, given the same value handle as NODE.  The prefix of the phi node is
1679    given with TMPNAME.  Return true if we have inserted new stuff.  */
1680
1681 static bool
1682 insert_into_preds_of_block (basic_block block, value_set_node_t node,
1683                             tree *avail, const char *tmpname)
1684 {
1685   tree val = get_value_handle (node->expr);
1686   edge pred;
1687   bool insertions = false;
1688   bool nophi = false;
1689   basic_block bprime;
1690   tree eprime;
1691   edge_iterator ei;
1692   tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
1693   tree temp;
1694   
1695   if (dump_file && (dump_flags & TDF_DETAILS))
1696     {
1697       fprintf (dump_file, "Found partial redundancy for expression ");
1698       print_generic_expr (dump_file, node->expr, 0);
1699       fprintf (dump_file, "\n");
1700     }
1701
1702   /* Make sure we aren't creating an induction variable.  */
1703   if (block->loop_depth > 0 && EDGE_COUNT (block->preds) == 2)
1704     {
1705       bool firstinsideloop = false;
1706       bool secondinsideloop = false;
1707       firstinsideloop = flow_bb_inside_loop_p (block->loop_father, 
1708                                                EDGE_PRED (block, 0)->src);
1709       secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
1710                                                 EDGE_PRED (block, 1)->src);
1711       /* Induction variables only have one edge inside the loop.  */
1712       if (firstinsideloop ^ secondinsideloop)
1713         {
1714           if (dump_file && (dump_flags & TDF_DETAILS))
1715             fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
1716           nophi = true;
1717         }
1718     }
1719           
1720
1721   /* Make the necessary insertions.  */
1722   FOR_EACH_EDGE (pred, ei, block->preds)
1723     {
1724       tree stmts = alloc_stmt_list ();
1725       tree builtexpr;
1726       bprime = pred->src;
1727       eprime = avail[bprime->index];
1728       if (BINARY_CLASS_P (eprime)
1729           || COMPARISON_CLASS_P (eprime)
1730           || UNARY_CLASS_P (eprime)
1731           || TREE_CODE (eprime) == CALL_EXPR)
1732         {
1733           builtexpr = create_expression_by_pieces (bprime,
1734                                                    eprime,
1735                                                    stmts);
1736           bsi_insert_on_edge (pred, stmts);
1737           avail[bprime->index] = builtexpr;
1738           insertions = true;
1739         }                             
1740     }
1741   /* If we didn't want a phi node, and we made insertions, we still have
1742      inserted new stuff, and thus return true.  If we didn't want a phi node,
1743      and didn't make insertions, we haven't added anything new, so return
1744      false.  */
1745   if (nophi && insertions)
1746     return true;
1747   else if (nophi && !insertions)
1748     return false;
1749
1750   /* Now build a phi for the new variable.  */
1751   temp = create_tmp_var (type, tmpname);
1752   add_referenced_tmp_var (temp);
1753   if (TREE_CODE (type) == COMPLEX_TYPE)
1754     DECL_COMPLEX_GIMPLE_REG_P (temp) = 1;
1755   temp = create_phi_node (temp, block);
1756   NECESSARY (temp) = 0; 
1757   VEC_safe_push (tree, heap, inserted_exprs, temp);
1758   FOR_EACH_EDGE (pred, ei, block->preds)
1759     add_phi_arg (temp, avail[pred->src->index], pred);
1760   
1761   vn_add (PHI_RESULT (temp), val, NULL);
1762   
1763   /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
1764      this insertion, since we test for the existence of this value in PHI_GEN
1765      before proceeding with the partial redundancy checks in insert_aux.
1766      
1767      The value may exist in AVAIL_OUT, in particular, it could be represented
1768      by the expression we are trying to eliminate, in which case we want the
1769      replacement to occur.  If it's not existing in AVAIL_OUT, we want it
1770      inserted there.
1771      
1772      Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
1773      this block, because if it did, it would have existed in our dominator's
1774      AVAIL_OUT, and would have been skipped due to the full redundancy check.
1775   */
1776
1777   bitmap_insert_into_set (PHI_GEN (block),
1778                           PHI_RESULT (temp));
1779   bitmap_value_replace_in_set (AVAIL_OUT (block), 
1780                                PHI_RESULT (temp));
1781   bitmap_insert_into_set (NEW_SETS (block),
1782                           PHI_RESULT (temp));
1783   
1784   if (dump_file && (dump_flags & TDF_DETAILS))
1785     {
1786       fprintf (dump_file, "Created phi ");
1787       print_generic_expr (dump_file, temp, 0);
1788       fprintf (dump_file, " in block %d\n", block->index);
1789     }
1790   pre_stats.phis++;
1791   return true;
1792 }
1793
1794
1795       
1796 /* Perform insertion of partially redundant values.
1797    For BLOCK, do the following:
1798    1.  Propagate the NEW_SETS of the dominator into the current block.
1799    If the block has multiple predecessors, 
1800        2a. Iterate over the ANTIC expressions for the block to see if
1801            any of them are partially redundant.
1802        2b. If so, insert them into the necessary predecessors to make
1803            the expression fully redundant.
1804        2c. Insert a new PHI merging the values of the predecessors.
1805        2d. Insert the new PHI, and the new expressions, into the
1806            NEW_SETS set.  
1807    3. Recursively call ourselves on the dominator children of BLOCK.
1808
1809 */
1810
1811 static bool
1812 insert_aux (basic_block block)
1813 {
1814   basic_block son;
1815   bool new_stuff = false;
1816
1817   if (block)
1818     {
1819       basic_block dom;
1820       dom = get_immediate_dominator (CDI_DOMINATORS, block);
1821       if (dom)
1822         {
1823           unsigned i;
1824           bitmap_iterator bi;
1825           bitmap_set_t newset = NEW_SETS (dom);
1826           if (newset)
1827             {
1828               /* Note that we need to value_replace both NEW_SETS, and
1829                  AVAIL_OUT. For both the case of NEW_SETS, the value may be
1830                  represented by some non-simple expression here that we want
1831                  to replace it with.  */
1832               EXECUTE_IF_SET_IN_BITMAP (newset->expressions, 0, i, bi)
1833                 {
1834                   bitmap_value_replace_in_set (NEW_SETS (block), ssa_name (i));
1835                   bitmap_value_replace_in_set (AVAIL_OUT (block), ssa_name (i));
1836                 }
1837             }
1838           if (!single_pred_p (block))
1839             {
1840               value_set_node_t node;
1841               for (node = ANTIC_IN (block)->head;
1842                    node;
1843                    node = node->next)
1844                 {
1845                   if (BINARY_CLASS_P (node->expr)
1846                       || COMPARISON_CLASS_P (node->expr)
1847                       || UNARY_CLASS_P (node->expr)
1848                       || TREE_CODE (node->expr) == CALL_EXPR)
1849                     {
1850                       tree *avail;
1851                       tree val;
1852                       bool by_some = false;
1853                       bool cant_insert = false;
1854                       bool all_same = true;
1855                       tree first_s = NULL;
1856                       edge pred;
1857                       basic_block bprime;
1858                       tree eprime = NULL_TREE;
1859                       edge_iterator ei;
1860
1861                       val = get_value_handle (node->expr);
1862                       if (bitmap_set_contains_value (PHI_GEN (block), val))
1863                         continue; 
1864                       if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
1865                         {
1866                           if (dump_file && (dump_flags & TDF_DETAILS))
1867                             fprintf (dump_file, "Found fully redundant value\n");
1868                           continue;
1869                         }
1870                                               
1871                       avail = xcalloc (last_basic_block, sizeof (tree));
1872                       FOR_EACH_EDGE (pred, ei, block->preds)
1873                         {
1874                           tree vprime;
1875                           tree edoubleprime;
1876
1877                           /* This can happen in the very weird case
1878                              that our fake infinite loop edges have caused a
1879                              critical edge to appear.  */
1880                           if (EDGE_CRITICAL_P (pred))
1881                             {
1882                               cant_insert = true;
1883                               break;
1884                             }
1885                           bprime = pred->src;
1886                           eprime = phi_translate (node->expr,
1887                                                   ANTIC_IN (block),
1888                                                   bprime, block);
1889
1890                           /* eprime will generally only be NULL if the
1891                              value of the expression, translated
1892                              through the PHI for this predecessor, is
1893                              undefined.  If that is the case, we can't
1894                              make the expression fully redundant,
1895                              because its value is undefined along a
1896                              predecessor path.  We can thus break out
1897                              early because it doesn't matter what the
1898                              rest of the results are.  */
1899                           if (eprime == NULL)
1900                             {
1901                               cant_insert = true;
1902                               break;
1903                             }
1904
1905                           eprime = fully_constant_expression (eprime);
1906                           vprime = get_value_handle (eprime);
1907                           gcc_assert (vprime);
1908                           edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
1909                                                              vprime);
1910                           if (edoubleprime == NULL)
1911                             {
1912                               avail[bprime->index] = eprime;
1913                               all_same = false;
1914                             }
1915                           else
1916                             {
1917                               avail[bprime->index] = edoubleprime;
1918                               by_some = true; 
1919                               if (first_s == NULL)
1920                                 first_s = edoubleprime;
1921                               else if (!operand_equal_p (first_s, edoubleprime,
1922                                                          0))
1923                                 all_same = false;
1924                             }
1925                         }
1926                       /* If we can insert it, it's not the same value
1927                          already existing along every predecessor, and
1928                          it's defined by some predecessor, it is
1929                          partially redundant.  */
1930                       if (!cant_insert && !all_same && by_some)
1931                         {
1932                           if (insert_into_preds_of_block (block, node, avail, 
1933                                                           "prephitmp"))
1934                             new_stuff = true;
1935                         }
1936                       /* If all edges produce the same value and that value is
1937                          an invariant, then the PHI has the same value on all
1938                          edges.  Note this.  */
1939                       else if (!cant_insert && all_same && eprime 
1940                                && is_gimple_min_invariant (eprime)
1941                                && !is_gimple_min_invariant (val))
1942                         {
1943                           value_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
1944                           value_set_node_t node;
1945                           for (node = exprset->head; node; node = node->next)
1946                             {
1947                               if (TREE_CODE (node->expr) == SSA_NAME)
1948                                 {                                 
1949                                   vn_add (node->expr, eprime, NULL);
1950                                   pre_stats.constified++;
1951                                 }
1952                             }
1953                         }
1954                       free (avail);
1955                     }
1956                 }
1957             }
1958         }
1959     }
1960   for (son = first_dom_son (CDI_DOMINATORS, block);
1961        son;
1962        son = next_dom_son (CDI_DOMINATORS, son))
1963     {
1964       new_stuff |= insert_aux (son);
1965     }
1966
1967   return new_stuff;
1968 }
1969
1970 /* Perform insertion of partially redundant values.  */
1971
1972 static void
1973 insert (void)
1974 {
1975   bool new_stuff = true;
1976   basic_block bb;
1977   int num_iterations = 0;
1978   
1979   FOR_ALL_BB (bb)
1980     NEW_SETS (bb) = bitmap_set_new ();
1981   
1982   while (new_stuff)
1983     {
1984       num_iterations++;
1985       new_stuff = false;
1986       new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1987     }
1988   if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1989     fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1990 }
1991
1992
1993 /* Return true if VAR is an SSA variable with no defining statement in
1994    this procedure, *AND* isn't a live-on-entry parameter.  */
1995
1996 static bool
1997 is_undefined_value (tree expr)
1998 {
1999   return (TREE_CODE (expr) == SSA_NAME
2000           && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
2001           /* PARM_DECLs and hard registers are always defined.  */
2002           && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL);
2003 }
2004
2005
2006 /* Given an SSA variable VAR and an expression EXPR, compute the value
2007    number for EXPR and create a value handle (VAL) for it.  If VAR and
2008    EXPR are not the same, associate VAL with VAR.  Finally, add VAR to
2009    S1 and its value handle to S2.
2010
2011    VUSES represent the virtual use operands associated with EXPR (if
2012    any). They are used when computing the hash value for EXPR.  */
2013
2014 static inline void
2015 add_to_sets (tree var, tree expr, tree stmt, bitmap_set_t s1,
2016              bitmap_set_t s2)
2017 {
2018   tree val = vn_lookup_or_add (expr, stmt);
2019
2020   /* VAR and EXPR may be the same when processing statements for which
2021      we are not computing value numbers (e.g., non-assignments, or
2022      statements that make aliased stores).  In those cases, we are
2023      only interested in making VAR available as its own value.  */
2024   if (var != expr)
2025     vn_add (var, val, NULL_TREE);
2026
2027   if (s1)
2028     bitmap_insert_into_set (s1, var);
2029   bitmap_value_insert_into_set (s2, var);
2030 }
2031
2032
2033 /* Given a unary or binary expression EXPR, create and return a new
2034    expression with the same structure as EXPR but with its operands
2035    replaced with the value handles of each of the operands of EXPR.
2036
2037    VUSES represent the virtual use operands associated with EXPR (if
2038    any). They are used when computing the hash value for EXPR.
2039    Insert EXPR's operands into the EXP_GEN set for BLOCK. */
2040
2041 static inline tree
2042 create_value_expr_from (tree expr, basic_block block, tree stmt)
2043 {
2044   int i;
2045   enum tree_code code = TREE_CODE (expr);
2046   tree vexpr;
2047   alloc_pool pool;
2048
2049   gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
2050               || TREE_CODE_CLASS (code) == tcc_binary
2051               || TREE_CODE_CLASS (code) == tcc_comparison
2052               || TREE_CODE_CLASS (code) == tcc_reference
2053               || TREE_CODE_CLASS (code) == tcc_expression
2054               || TREE_CODE_CLASS (code) == tcc_exceptional);
2055
2056   if (TREE_CODE_CLASS (code) == tcc_unary)
2057     pool = unary_node_pool;
2058   else if (TREE_CODE_CLASS (code) == tcc_reference)
2059     pool = reference_node_pool;
2060   else if (TREE_CODE_CLASS (code) == tcc_binary)
2061     pool = binary_node_pool;
2062   else if (TREE_CODE_CLASS (code) == tcc_comparison)
2063     pool = comparison_node_pool;
2064   else if (TREE_CODE_CLASS (code) == tcc_exceptional)
2065     {
2066       gcc_assert (code == TREE_LIST);
2067       pool = list_node_pool;
2068     }
2069   else 
2070     {
2071       gcc_assert (code == CALL_EXPR);
2072       pool = expression_node_pool;
2073     }
2074
2075   vexpr = pool_alloc (pool);
2076   memcpy (vexpr, expr, tree_size (expr));
2077   
2078   /* This case is only for TREE_LIST's that appear as part of
2079      CALL_EXPR's.  Anything else is a bug, but we can't easily verify
2080      this, hence this comment.  TREE_LIST is not handled by the
2081      general case below is because they don't have a fixed length, or
2082      operands, so you can't access purpose/value/chain through
2083      TREE_OPERAND macros.  */
2084
2085   if (code == TREE_LIST)
2086     {
2087       tree op = NULL_TREE;
2088       tree temp = NULL_TREE;
2089       if (TREE_CHAIN (vexpr))
2090         temp = create_value_expr_from (TREE_CHAIN (vexpr), block, stmt);      
2091       TREE_CHAIN (vexpr) = temp ? temp : TREE_CHAIN (vexpr);
2092       
2093
2094       /* Recursively value-numberize reference ops.  */
2095       if (REFERENCE_CLASS_P (TREE_VALUE (vexpr)))
2096         {
2097           tree tempop;
2098           op = TREE_VALUE (vexpr);
2099           tempop = create_value_expr_from (op, block, stmt);
2100           op = tempop ? tempop : op;
2101           
2102           TREE_VALUE (vexpr)  = vn_lookup_or_add (op, stmt);
2103         }
2104       else
2105         {
2106           op = TREE_VALUE (vexpr);
2107           TREE_VALUE (vexpr) = vn_lookup_or_add (TREE_VALUE (vexpr), NULL);
2108         }
2109       /* This is the equivalent of inserting op into EXP_GEN like we
2110          do below */
2111       if (!is_undefined_value (op))
2112         value_insert_into_set (EXP_GEN (block), op);
2113
2114       return vexpr;
2115     }
2116
2117   for (i = 0; i < TREE_CODE_LENGTH (code); i++)
2118     {
2119       tree val, op;
2120       
2121       op = TREE_OPERAND (expr, i);
2122       if (op == NULL_TREE)
2123         continue;
2124
2125       /* If OP is a constant that has overflowed, do not value number
2126          this expression.  */
2127       if (CONSTANT_CLASS_P (op)
2128           && TREE_OVERFLOW (op))
2129         {
2130           pool_free (pool, vexpr);
2131           return NULL;
2132         }
2133
2134       /* Recursively value-numberize reference ops and tree lists.  */
2135       if (REFERENCE_CLASS_P (op))
2136         {
2137           tree tempop = create_value_expr_from (op, block, stmt);
2138           op = tempop ? tempop : op;
2139           val = vn_lookup_or_add (op, stmt);
2140         }
2141       else if (TREE_CODE (op) == TREE_LIST)
2142         {
2143           tree tempop;
2144           
2145           gcc_assert (TREE_CODE (expr) == CALL_EXPR);
2146           tempop = create_value_expr_from (op, block, stmt);
2147           
2148           op = tempop ? tempop : op;
2149           vn_lookup_or_add (op, NULL);
2150           /* Unlike everywhere else, we do *not* want to replace the
2151              TREE_LIST itself with a value number, because support
2152              functions we call will blow up.  */
2153           val = op;
2154         }
2155       else       
2156         /* Create a value handle for OP and add it to VEXPR.  */
2157         val = vn_lookup_or_add (op, NULL);
2158
2159       if (!is_undefined_value (op) && TREE_CODE (op) != TREE_LIST)
2160         value_insert_into_set (EXP_GEN (block), op);
2161
2162       if (TREE_CODE (val) == VALUE_HANDLE)
2163         TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
2164
2165       TREE_OPERAND (vexpr, i) = val;
2166     }
2167
2168   return vexpr;
2169 }
2170
2171
2172 /* Return true if we can value number a call.  This is true if we have
2173    a pure or constant call.  */
2174 static bool
2175 can_value_number_call (tree stmt)
2176 {
2177   tree call = get_call_expr_in (stmt);
2178
2179   /* This is a temporary restriction until we translate vuses through
2180      phi nodes.  This is only needed for PRE, of course.  */
2181   if (!in_fre && !ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
2182     return false;  
2183   if (call_expr_flags (call)  & (ECF_PURE | ECF_CONST))
2184     return true;
2185   return false;
2186 }
2187
2188 /* Insert extra phis to merge values that are fully available from
2189    preds of BLOCK, but have no dominating representative coming from
2190    block DOM.   */
2191
2192 static void
2193 insert_extra_phis (basic_block block, basic_block dom)
2194 {
2195   
2196   if (!single_pred_p (block))
2197     {
2198       edge e;
2199       edge_iterator ei;
2200       bool first = true;
2201       bitmap_set_t tempset = bitmap_set_new ();
2202
2203       FOR_EACH_EDGE (e, ei, block->preds)
2204         {
2205           if (first)
2206             {
2207               bitmap_set_copy (tempset, AVAIL_OUT (e->src));
2208               first = false;
2209             }
2210           else
2211             bitmap_set_and (tempset, AVAIL_OUT (e->src));
2212         }
2213
2214       if (dom)
2215         bitmap_set_and_compl (tempset, AVAIL_OUT (dom));
2216
2217       if (!bitmap_set_empty_p (tempset))
2218         {
2219           unsigned int i;
2220           bitmap_iterator bi;
2221
2222           EXECUTE_IF_SET_IN_BITMAP (tempset->expressions, 0, i, bi)
2223             {
2224               tree name = ssa_name (i);
2225               tree val = get_value_handle (name);
2226               tree temp = create_tmp_var (TREE_TYPE (name), "mergephitmp");
2227                   
2228               if (dump_file && (dump_flags & TDF_DETAILS))
2229                 {
2230                   fprintf (dump_file, "Creating phi ");
2231                   print_generic_expr (dump_file, temp, 0);
2232                   fprintf (dump_file, " to merge available but not dominating values ");
2233                 }
2234
2235               add_referenced_tmp_var (temp);
2236               temp = create_phi_node (temp, block);
2237               NECESSARY (temp) = 0; 
2238               VEC_safe_push (tree, heap, inserted_exprs, temp);
2239
2240               FOR_EACH_EDGE (e, ei, block->preds)
2241                 {
2242                   tree leader = bitmap_find_leader (AVAIL_OUT (e->src), val);
2243                   
2244                   gcc_assert (leader);
2245                   add_phi_arg (temp, leader, e);
2246
2247                   if (dump_file && (dump_flags & TDF_DETAILS))
2248                     {
2249                       print_generic_expr (dump_file, leader, 0);
2250                       fprintf (dump_file, " in block %d,", e->src->index);
2251                     }
2252                 }
2253
2254               vn_add (PHI_RESULT (temp), val, NULL);
2255               
2256               if (dump_file && (dump_flags & TDF_DETAILS))
2257                 fprintf (dump_file, "\n");
2258             }
2259         }
2260     }
2261 }
2262
2263 /* Given a statement STMT and its right hand side which is a load, try
2264    to look for the expression stored in the location for the load, and
2265    return true if a useful equivalence was recorded for LHS.  */
2266
2267 static bool
2268 try_look_through_load (tree lhs, tree mem_ref, tree stmt, basic_block block)
2269 {
2270   tree store_stmt = NULL;
2271   tree rhs;
2272   ssa_op_iter i;
2273   tree vuse;
2274
2275   FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, i, SSA_OP_VIRTUAL_USES)
2276     {
2277       tree def_stmt;
2278
2279       gcc_assert (TREE_CODE (vuse) == SSA_NAME);
2280       def_stmt = SSA_NAME_DEF_STMT (vuse);
2281
2282       /* If there is no useful statement for this VUSE, we'll not find a
2283          useful expression to return either.  Likewise, if there is a
2284          statement but it is not a simple assignment or it has virtual
2285          uses, we can stop right here.  Note that this means we do
2286          not look through PHI nodes, which is intentional.  */
2287       if (!def_stmt
2288           || TREE_CODE (def_stmt) != MODIFY_EXPR
2289           || !ZERO_SSA_OPERANDS (def_stmt, SSA_OP_VIRTUAL_USES))
2290         return false;
2291
2292       /* If this is not the same statement as one we have looked at for
2293          another VUSE of STMT already, we have two statements producing
2294          something that reaches our STMT.  */
2295       if (store_stmt && store_stmt != def_stmt)
2296         return false;
2297       else
2298         {
2299           /* Is this a store to the exact same location as the one we are
2300              loading from in STMT?  */
2301           if (!operand_equal_p (TREE_OPERAND (def_stmt, 0), mem_ref, 0))
2302             return false;
2303
2304           /* Otherwise remember this statement and see if all other VUSEs
2305              come from the same statement.  */
2306           store_stmt = def_stmt;
2307         }
2308     }
2309
2310   /* Alright then, we have visited all VUSEs of STMT and we've determined
2311      that all of them come from the same statement STORE_STMT.  See if there
2312      is a useful expression we can deduce from STORE_STMT.  */
2313   rhs = TREE_OPERAND (store_stmt, 1);
2314   if ((TREE_CODE (rhs) == SSA_NAME
2315        && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2316       || is_gimple_min_invariant (rhs)
2317       || TREE_CODE (rhs) == ADDR_EXPR
2318       || TREE_INVARIANT (rhs))
2319     {
2320       
2321       /* Yay!  Compute a value number for the RHS of the statement and
2322          add its value to the AVAIL_OUT set for the block.  Add the LHS
2323          to TMP_GEN.  */
2324       add_to_sets (lhs, rhs, store_stmt, TMP_GEN (block), AVAIL_OUT (block));
2325       if (TREE_CODE (rhs) == SSA_NAME
2326           && !is_undefined_value (rhs))
2327         value_insert_into_set (EXP_GEN (block), rhs);
2328       return true;
2329     }
2330
2331   return false;
2332 }
2333
2334 /* Compute the AVAIL set for all basic blocks.
2335
2336    This function performs value numbering of the statements in each basic
2337    block.  The AVAIL sets are built from information we glean while doing
2338    this value numbering, since the AVAIL sets contain only one entry per
2339    value.
2340    
2341    AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
2342    AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK].  */
2343
2344 static void
2345 compute_avail (void)
2346 {
2347   basic_block block, son;
2348   basic_block *worklist;
2349   size_t sp = 0;
2350   tree param;
2351
2352   /* For arguments with default definitions, we pretend they are
2353      defined in the entry block.  */
2354   for (param = DECL_ARGUMENTS (current_function_decl);
2355        param;
2356        param = TREE_CHAIN (param))
2357     {
2358       if (default_def (param) != NULL)
2359         {
2360           tree def = default_def (param);
2361           vn_lookup_or_add (def, NULL);
2362           bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
2363           bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
2364         }
2365     }
2366
2367   /* Allocate the worklist.  */
2368   worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
2369
2370   /* Seed the algorithm by putting the dominator children of the entry
2371      block on the worklist.  */
2372   for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR);
2373        son;
2374        son = next_dom_son (CDI_DOMINATORS, son))
2375     worklist[sp++] = son;
2376
2377   /* Loop until the worklist is empty.  */
2378   while (sp)
2379     {
2380       block_stmt_iterator bsi;
2381       tree stmt, phi;
2382       basic_block dom;
2383
2384       /* Pick a block from the worklist.  */
2385       block = worklist[--sp];
2386
2387       /* Initially, the set of available values in BLOCK is that of
2388          its immediate dominator.  */
2389       dom = get_immediate_dominator (CDI_DOMINATORS, block);
2390       if (dom)
2391         bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
2392
2393       if (!in_fre)
2394         insert_extra_phis (block, dom);
2395
2396       /* Generate values for PHI nodes.  */
2397       for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
2398         /* We have no need for virtual phis, as they don't represent
2399            actual computations.  */
2400         if (is_gimple_reg (PHI_RESULT (phi)))
2401           add_to_sets (PHI_RESULT (phi), PHI_RESULT (phi), NULL,
2402                        PHI_GEN (block), AVAIL_OUT (block));
2403
2404       /* Now compute value numbers and populate value sets with all
2405          the expressions computed in BLOCK.  */
2406       for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
2407         {
2408           stmt_ann_t ann;
2409           ssa_op_iter iter;
2410           tree op;
2411
2412           stmt = bsi_stmt (bsi);
2413           ann = stmt_ann (stmt);
2414
2415           /* We are only interested in assignments of the form
2416              X_i = EXPR, where EXPR represents an "interesting"
2417              computation, it has no volatile operands and X_i
2418              doesn't flow through an abnormal edge.  */
2419           if (TREE_CODE (stmt) == MODIFY_EXPR
2420               && !ann->has_volatile_ops
2421               && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
2422               && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (stmt, 0)))
2423             {
2424               tree lhs = TREE_OPERAND (stmt, 0);
2425               tree rhs = TREE_OPERAND (stmt, 1);
2426
2427               /* Try to look through loads.  */
2428               if (TREE_CODE (lhs) == SSA_NAME
2429                   && !ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_USES)
2430                   && try_look_through_load (lhs, rhs, stmt, block))
2431                 continue;
2432
2433               STRIP_USELESS_TYPE_CONVERSION (rhs);
2434               if (UNARY_CLASS_P (rhs)
2435                   || BINARY_CLASS_P (rhs)
2436                   || COMPARISON_CLASS_P (rhs)
2437                   || REFERENCE_CLASS_P (rhs)
2438                   || (TREE_CODE (rhs) == CALL_EXPR
2439                       && can_value_number_call (stmt)))
2440                 {
2441                   /* For binary, unary, and reference expressions,
2442                      create a duplicate expression with the operands
2443                      replaced with the value handles of the original
2444                      RHS.  */
2445                   tree newt = create_value_expr_from (rhs, block, stmt);
2446                   if (newt)
2447                     {
2448                       add_to_sets (lhs, newt, stmt, TMP_GEN (block),
2449                                    AVAIL_OUT (block));
2450                       value_insert_into_set (EXP_GEN (block), newt);
2451                       continue;
2452                     }
2453                 }
2454               else if ((TREE_CODE (rhs) == SSA_NAME
2455                         && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2456                        || is_gimple_min_invariant (rhs)
2457                        || TREE_CODE (rhs) == ADDR_EXPR
2458                        || TREE_INVARIANT (rhs)
2459                        || DECL_P (rhs))
2460                 {
2461                   /* Compute a value number for the RHS of the statement
2462                      and add its value to the AVAIL_OUT set for the block.
2463                      Add the LHS to TMP_GEN.  */
2464                   add_to_sets (lhs, rhs, stmt, TMP_GEN (block), 
2465                                AVAIL_OUT (block));
2466                   
2467                   if (TREE_CODE (rhs) == SSA_NAME
2468                       && !is_undefined_value (rhs))
2469                     value_insert_into_set (EXP_GEN (block), rhs);
2470                   continue;
2471                 }          
2472             }
2473
2474           /* For any other statement that we don't recognize, simply
2475              make the names generated by the statement available in
2476              AVAIL_OUT and TMP_GEN.  */
2477           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
2478             add_to_sets (op, op, NULL, TMP_GEN (block), AVAIL_OUT (block));
2479
2480           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
2481             add_to_sets (op, op, NULL, NULL , AVAIL_OUT (block));
2482         }
2483
2484       /* Put the dominator children of BLOCK on the worklist of blocks
2485          to compute available sets for.  */
2486       for (son = first_dom_son (CDI_DOMINATORS, block);
2487            son;
2488            son = next_dom_son (CDI_DOMINATORS, son))
2489         worklist[sp++] = son;
2490     }
2491
2492   free (worklist);
2493 }
2494
2495
2496 /* Eliminate fully redundant computations.  */
2497
2498 static void
2499 eliminate (void)
2500 {
2501   basic_block b;
2502
2503   FOR_EACH_BB (b)
2504     {
2505       block_stmt_iterator i;
2506       
2507       for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
2508         {
2509           tree stmt = bsi_stmt (i);
2510
2511           /* Lookup the RHS of the expression, see if we have an
2512              available computation for it.  If so, replace the RHS with
2513              the available computation.  */
2514           if (TREE_CODE (stmt) == MODIFY_EXPR
2515               && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
2516               && TREE_CODE (TREE_OPERAND (stmt ,1)) != SSA_NAME
2517               && !is_gimple_min_invariant (TREE_OPERAND (stmt, 1))
2518               && !stmt_ann (stmt)->has_volatile_ops)
2519             {
2520               tree lhs = TREE_OPERAND (stmt, 0);
2521               tree *rhs_p = &TREE_OPERAND (stmt, 1);
2522               tree sprime;
2523
2524               sprime = bitmap_find_leader (AVAIL_OUT (b),
2525                                            vn_lookup (lhs, NULL));
2526               if (sprime 
2527                   && sprime != lhs
2528                   && (TREE_CODE (*rhs_p) != SSA_NAME
2529                       || may_propagate_copy (*rhs_p, sprime)))
2530                 {
2531                   gcc_assert (sprime != *rhs_p);
2532
2533                   if (dump_file && (dump_flags & TDF_DETAILS))
2534                     {
2535                       fprintf (dump_file, "Replaced ");
2536                       print_generic_expr (dump_file, *rhs_p, 0);
2537                       fprintf (dump_file, " with ");
2538                       print_generic_expr (dump_file, sprime, 0);
2539                       fprintf (dump_file, " in ");
2540                       print_generic_stmt (dump_file, stmt, 0);
2541                     }
2542                   
2543                   if (TREE_CODE (sprime) == SSA_NAME) 
2544                     NECESSARY (SSA_NAME_DEF_STMT (sprime)) = 1;
2545                   /* We need to make sure the new and old types actually match,
2546                      which may require adding a simple cast, which fold_convert
2547                      will do for us.  */
2548                   if (TREE_CODE (*rhs_p) != SSA_NAME
2549                       && !tree_ssa_useless_type_conversion_1 (TREE_TYPE (*rhs_p),
2550                                                               TREE_TYPE (sprime)))
2551                     sprime = fold_convert (TREE_TYPE (*rhs_p), sprime);
2552                   
2553                   pre_stats.eliminations++;
2554                   propagate_tree_value (rhs_p, sprime);
2555                   update_stmt (stmt);
2556
2557                   /* If we removed EH side effects from the statement, clean
2558                      its EH information.  */
2559                   if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
2560                     {
2561                       bitmap_set_bit (need_eh_cleanup,
2562                                       bb_for_stmt (stmt)->index);
2563                       if (dump_file && (dump_flags & TDF_DETAILS))
2564                         fprintf (dump_file, "  Removed EH side effects.\n");
2565                     }
2566                 }
2567             }
2568         }
2569     }
2570 }
2571
2572 /* Borrow a bit of tree-ssa-dce.c for the moment.
2573    XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
2574    this may be a bit faster, and we may want critical edges kept split.  */
2575
2576 /* If OP's defining statement has not already been determined to be necessary,
2577    mark that statement necessary. Return the stmt, if it is newly
2578    necessary.  */ 
2579
2580 static inline tree
2581 mark_operand_necessary (tree op)
2582 {
2583   tree stmt;
2584
2585   gcc_assert (op);
2586
2587   stmt = SSA_NAME_DEF_STMT (op);
2588   gcc_assert (stmt);
2589
2590   if (NECESSARY (stmt)
2591       || IS_EMPTY_STMT (stmt))
2592     return NULL;
2593
2594   NECESSARY (stmt) = 1;
2595   return stmt;
2596 }
2597
2598 /* Because we don't follow exactly the standard PRE algorithm, and decide not
2599    to insert PHI nodes sometimes, and because value numbering of casts isn't
2600    perfect, we sometimes end up inserting dead code.   This simple DCE-like
2601    pass removes any insertions we made that weren't actually used.  */
2602
2603 static void
2604 remove_dead_inserted_code (void)
2605 {
2606   VEC(tree,heap) *worklist = NULL;
2607   int i;
2608   tree t;
2609
2610   worklist = VEC_alloc (tree, heap, VEC_length (tree, inserted_exprs));
2611   for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
2612     {
2613       if (NECESSARY (t))
2614         VEC_quick_push (tree, worklist, t);
2615     }
2616   while (VEC_length (tree, worklist) > 0)
2617     {
2618       t = VEC_pop (tree, worklist);
2619       if (TREE_CODE (t) == PHI_NODE)
2620         {
2621           /* PHI nodes are somewhat special in that each PHI alternative has
2622              data and control dependencies.  All the statements feeding the
2623              PHI node's arguments are always necessary.  In aggressive mode,
2624              we also consider the control dependent edges leading to the
2625              predecessor block associated with each PHI alternative as
2626              necessary.  */
2627           int k;
2628
2629           VEC_reserve (tree, heap, worklist, PHI_NUM_ARGS (t));
2630           for (k = 0; k < PHI_NUM_ARGS (t); k++)
2631             {
2632               tree arg = PHI_ARG_DEF (t, k);
2633               if (TREE_CODE (arg) == SSA_NAME)
2634                 {
2635                   arg = mark_operand_necessary (arg);
2636                   if (arg)
2637                     VEC_quick_push (tree, worklist, arg);
2638                 }
2639             }
2640         }
2641       else
2642         {
2643           /* Propagate through the operands.  Examine all the USE, VUSE and
2644              V_MAY_DEF operands in this statement.  Mark all the statements 
2645              which feed this statement's uses as necessary.  */
2646           ssa_op_iter iter;
2647           tree use;
2648
2649           /* The operands of V_MAY_DEF expressions are also needed as they
2650              represent potential definitions that may reach this
2651              statement (V_MAY_DEF operands allow us to follow def-def 
2652              links).  */
2653
2654           FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
2655             {
2656               tree n = mark_operand_necessary (use);
2657               if (n)
2658                 VEC_safe_push (tree, heap, worklist, n);
2659             }
2660         }
2661     }
2662   for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
2663     {
2664       if (!NECESSARY (t))
2665         {
2666           block_stmt_iterator bsi;
2667           if (dump_file && (dump_flags & TDF_DETAILS))
2668             {
2669               fprintf (dump_file, "Removing unnecessary insertion:");
2670               print_generic_stmt (dump_file, t, 0);
2671             }
2672           if (TREE_CODE (t) == PHI_NODE)
2673             {
2674               remove_phi_node (t, NULL);
2675             }
2676           else
2677             {
2678               bsi = bsi_for_stmt (t);
2679               bsi_remove (&bsi);
2680             }
2681         }
2682     }
2683   VEC_free (tree, heap, worklist);
2684 }
2685 /* Initialize data structures used by PRE.  */
2686
2687 static void
2688 init_pre (bool do_fre)
2689 {
2690   basic_block bb;
2691   
2692   in_fre = do_fre;
2693
2694   inserted_exprs = NULL;
2695   vn_init ();
2696   if (!do_fre)
2697     current_loops = loop_optimizer_init (dump_file);
2698   connect_infinite_loops_to_exit ();
2699   memset (&pre_stats, 0, sizeof (pre_stats));
2700
2701   /* If block 0 has more than one predecessor, it means that its PHI
2702      nodes will have arguments coming from block -1.  This creates
2703      problems for several places in PRE that keep local arrays indexed
2704      by block number.  To prevent this, we split the edge coming from
2705      ENTRY_BLOCK_PTR (FIXME, if ENTRY_BLOCK_PTR had an index number
2706      different than -1 we wouldn't have to hack this.  tree-ssa-dce.c
2707      needs a similar change).  */
2708   if (!single_pred_p (single_succ (ENTRY_BLOCK_PTR)))
2709     if (!(single_succ_edge (ENTRY_BLOCK_PTR)->flags & EDGE_ABNORMAL))
2710       split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
2711
2712   FOR_ALL_BB (bb)
2713     bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
2714
2715   bitmap_obstack_initialize (&grand_bitmap_obstack);
2716   phi_translate_table = htab_create (511, expr_pred_trans_hash,
2717                                      expr_pred_trans_eq, free);
2718   value_set_pool = create_alloc_pool ("Value sets",
2719                                       sizeof (struct value_set), 30);
2720   bitmap_set_pool = create_alloc_pool ("Bitmap sets",
2721                                        sizeof (struct bitmap_set), 30);
2722   value_set_node_pool = create_alloc_pool ("Value set nodes",
2723                                            sizeof (struct value_set_node), 30);
2724   calculate_dominance_info (CDI_POST_DOMINATORS);
2725   calculate_dominance_info (CDI_DOMINATORS);
2726   binary_node_pool = create_alloc_pool ("Binary tree nodes",
2727                                         tree_code_size (PLUS_EXPR), 30);
2728   unary_node_pool = create_alloc_pool ("Unary tree nodes",
2729                                        tree_code_size (NEGATE_EXPR), 30);
2730   reference_node_pool = create_alloc_pool ("Reference tree nodes",
2731                                            tree_code_size (ARRAY_REF), 30);
2732   expression_node_pool = create_alloc_pool ("Expression tree nodes",
2733                                             tree_code_size (CALL_EXPR), 30);
2734   list_node_pool = create_alloc_pool ("List tree nodes",
2735                                       tree_code_size (TREE_LIST), 30);  
2736   comparison_node_pool = create_alloc_pool ("Comparison tree nodes",
2737                                             tree_code_size (EQ_EXPR), 30);
2738   FOR_ALL_BB (bb)
2739     {
2740       EXP_GEN (bb) = set_new (true);
2741       PHI_GEN (bb) = bitmap_set_new ();
2742       TMP_GEN (bb) = bitmap_set_new ();
2743       AVAIL_OUT (bb) = bitmap_set_new ();
2744     }
2745
2746   need_eh_cleanup = BITMAP_ALLOC (NULL);
2747 }
2748
2749
2750 /* Deallocate data structures used by PRE.  */
2751
2752 static void
2753 fini_pre (bool do_fre)
2754 {
2755   basic_block bb;
2756   unsigned int i;
2757
2758   VEC_free (tree, heap, inserted_exprs);
2759   bitmap_obstack_release (&grand_bitmap_obstack);
2760   free_alloc_pool (value_set_pool);
2761   free_alloc_pool (bitmap_set_pool);
2762   free_alloc_pool (value_set_node_pool);
2763   free_alloc_pool (binary_node_pool);
2764   free_alloc_pool (reference_node_pool);
2765   free_alloc_pool (unary_node_pool);
2766   free_alloc_pool (list_node_pool);
2767   free_alloc_pool (expression_node_pool);
2768   free_alloc_pool (comparison_node_pool);
2769   htab_delete (phi_translate_table);
2770   remove_fake_exit_edges ();
2771
2772   FOR_ALL_BB (bb)
2773     {
2774       free (bb->aux);
2775       bb->aux = NULL;
2776     }
2777
2778   free_dominance_info (CDI_POST_DOMINATORS);
2779   vn_delete ();
2780
2781   if (!bitmap_empty_p (need_eh_cleanup))
2782     {
2783       tree_purge_all_dead_eh_edges (need_eh_cleanup);
2784       cleanup_tree_cfg ();
2785     }
2786
2787   BITMAP_FREE (need_eh_cleanup);
2788
2789   /* Wipe out pointers to VALUE_HANDLEs.  In the not terribly distant
2790      future we will want them to be persistent though.  */
2791   for (i = 0; i < num_ssa_names; i++)
2792     {
2793       tree name = ssa_name (i);
2794
2795       if (!name)
2796         continue;
2797
2798       if (SSA_NAME_VALUE (name)
2799           && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
2800         SSA_NAME_VALUE (name) = NULL;
2801     }
2802   if (!do_fre && current_loops)
2803     {
2804       loop_optimizer_finalize (current_loops, dump_file);
2805       current_loops = NULL;
2806     }
2807 }
2808
2809
2810 /* Main entry point to the SSA-PRE pass.  DO_FRE is true if the caller
2811    only wants to do full redundancy elimination.  */
2812
2813 static void
2814 execute_pre (bool do_fre)
2815 {
2816   init_pre (do_fre);
2817
2818   /* Collect and value number expressions computed in each basic block.  */
2819   compute_avail ();
2820
2821   if (dump_file && (dump_flags & TDF_DETAILS))
2822     {
2823       basic_block bb;
2824
2825       FOR_ALL_BB (bb)
2826         {
2827           print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
2828           bitmap_print_value_set (dump_file, TMP_GEN (bb), "tmp_gen", 
2829                                   bb->index);
2830           bitmap_print_value_set (dump_file, AVAIL_OUT (bb), "avail_out", 
2831                                   bb->index);
2832         }
2833     }
2834
2835   /* Insert can get quite slow on an incredibly large number of basic
2836      blocks due to some quadratic behavior.  Until this behavior is
2837      fixed, don't run it when he have an incredibly large number of
2838      bb's.  If we aren't going to run insert, there is no point in
2839      computing ANTIC, either, even though it's plenty fast.  */
2840   if (!do_fre && n_basic_blocks < 4000)
2841     {
2842       compute_antic ();
2843       insert ();
2844     }
2845
2846   /* Remove all the redundant expressions.  */
2847   eliminate ();
2848
2849
2850   if (dump_file && (dump_flags & TDF_STATS))
2851     {
2852       fprintf (dump_file, "Insertions: %d\n", pre_stats.insertions);
2853       fprintf (dump_file, "New PHIs: %d\n", pre_stats.phis);
2854       fprintf (dump_file, "Eliminated: %d\n", pre_stats.eliminations);
2855       fprintf (dump_file, "Constified: %d\n", pre_stats.constified);
2856     }
2857   
2858   bsi_commit_edge_inserts ();
2859   if (!do_fre)
2860     remove_dead_inserted_code ();
2861   fini_pre (do_fre);
2862
2863 }
2864
2865
2866 /* Gate and execute functions for PRE.  */
2867
2868 static void
2869 do_pre (void)
2870 {
2871   execute_pre (false);
2872 }
2873
2874 static bool
2875 gate_pre (void)
2876 {
2877   return flag_tree_pre != 0;
2878 }
2879
2880 struct tree_opt_pass pass_pre =
2881 {
2882   "pre",                                /* name */
2883   gate_pre,                             /* gate */
2884   do_pre,                               /* execute */
2885   NULL,                                 /* sub */
2886   NULL,                                 /* next */
2887   0,                                    /* static_pass_number */
2888   TV_TREE_PRE,                          /* tv_id */
2889   PROP_no_crit_edges | PROP_cfg
2890     | PROP_ssa | PROP_alias,            /* properties_required */
2891   0,                                    /* properties_provided */
2892   0,                                    /* properties_destroyed */
2893   0,                                    /* todo_flags_start */
2894   TODO_update_ssa | TODO_dump_func | TODO_ggc_collect 
2895   | TODO_verify_ssa, /* todo_flags_finish */
2896   0                                     /* letter */
2897 };
2898
2899
2900 /* Gate and execute functions for FRE.  */
2901
2902 static void
2903 execute_fre (void)
2904 {
2905   execute_pre (true);
2906 }
2907
2908 static bool
2909 gate_fre (void)
2910 {
2911   return flag_tree_fre != 0;
2912 }
2913
2914 struct tree_opt_pass pass_fre =
2915 {
2916   "fre",                                /* name */
2917   gate_fre,                             /* gate */
2918   execute_fre,                          /* execute */
2919   NULL,                                 /* sub */
2920   NULL,                                 /* next */
2921   0,                                    /* static_pass_number */
2922   TV_TREE_FRE,                          /* tv_id */
2923   PROP_cfg | PROP_ssa | PROP_alias,     /* properties_required */
2924   0,                                    /* properties_provided */
2925   0,                                    /* properties_destroyed */
2926   0,                                    /* todo_flags_start */
2927   TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
2928   0                                     /* letter */
2929 };
2930
2931 /* Return true if T is a copy statement between two ssa names.  */
2932
2933 static bool
2934 is_copy_stmt (tree t)
2935 {  
2936   if (!t || TREE_CODE (t) != MODIFY_EXPR)
2937     return false;
2938   if (TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME 
2939       && TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME)
2940     return true;
2941   return false;
2942 }
2943
2944 /* Starting from START, walk copy statements till we hit a statement with a
2945    VUSE or a non-copy statement.  */
2946
2947 static tree 
2948 follow_copies_till_vuse (tree start)
2949 {
2950   if (is_copy_stmt (start) && ZERO_SSA_OPERANDS (start, SSA_OP_VIRTUAL_USES))
2951     {
2952       tree rhs, defstmt;
2953
2954       rhs = TREE_OPERAND (start, 1);
2955       defstmt = SSA_NAME_DEF_STMT (rhs);
2956       return follow_copies_till_vuse (defstmt);
2957     }
2958   return start;
2959 }
2960
2961 /* Gate and execute functions for eliminate useless stores.     
2962    The goal here is to recognize the pattern *x = ... *x, and eliminate the
2963    store because the value hasn't changed.  Store copy/const prop won't
2964    do this because making *more* loads (IE propagating *x) is not a win, so it
2965    ignores them.  
2966    This pass is currently geared completely towards static variable store
2967    elimination.  */
2968
2969 static void
2970 do_eustores (void)
2971 {
2972   basic_block bb;
2973   /* For each basic block
2974        For each statement (STMT) in the block
2975          if STMT is a stores of the pattern *x = y
2976            follow the chain of definitions for y, until we hit a non-copy
2977            statement or a statement with a vuse. 
2978              if the statement we arrive at is a vuse of the operand we killed,
2979              accessed through the same memory operation, then we have a
2980              useless store (because it is *x = ... = *x).  */
2981           
2982   FOR_EACH_BB (bb)
2983     {
2984       block_stmt_iterator bsi;
2985
2986       for (bsi = bsi_start (bb);
2987            !bsi_end_p (bsi);)
2988         {
2989           tree stmt = bsi_stmt (bsi);
2990           tree startat;
2991           tree kill;      
2992           tree found;
2993                   
2994           if (NUM_SSA_OPERANDS (stmt, SSA_OP_VMUSTDEF) != 1
2995               || TREE_CODE (stmt) != MODIFY_EXPR
2996               || TREE_CODE (TREE_OPERAND (stmt, 1)) != SSA_NAME)
2997             {
2998               bsi_next (&bsi);
2999               continue;
3000             }
3001
3002           kill = MUSTDEF_KILL (MUSTDEF_OPS (stmt)); 
3003           startat = TREE_OPERAND (stmt, 1);
3004           startat = SSA_NAME_DEF_STMT (startat);
3005           found = follow_copies_till_vuse (startat);
3006
3007           if (found && TREE_CODE (found) == MODIFY_EXPR)
3008             {      
3009
3010               /* We want exactly one virtual use, and it should match up with
3011                  the use being killed.  */
3012
3013               if (NUM_SSA_OPERANDS (found, SSA_OP_VUSE) != 1
3014                   || VUSE_OP (VUSE_OPS (found)) != kill
3015                   || !DECL_P (TREE_OPERAND (stmt, 0))
3016                   || !operand_equal_p (TREE_OPERAND (found, 1), 
3017                                        TREE_OPERAND (stmt, 0), 0))
3018                 {
3019                   bsi_next (&bsi);
3020                   continue;
3021                 }
3022
3023               if (dump_file)
3024                 {
3025                   fprintf (dump_file, "Eliminating useless store ");
3026                   print_generic_stmt (dump_file, stmt, 0);
3027                 }
3028               mark_sym_for_renaming (TREE_OPERAND (stmt, 0));
3029               bsi_remove (&bsi);
3030             }
3031           else
3032             {
3033               bsi_next (&bsi);
3034               continue;
3035             }
3036         }
3037     }
3038 }
3039
3040 static bool
3041 gate_eustores(void)
3042 {
3043   return flag_unit_at_a_time != 0;
3044 }
3045
3046 struct tree_opt_pass pass_eliminate_useless_stores =
3047 {
3048   "eustores",                           /* name */
3049   gate_eustores,                                /* gate */
3050   do_eustores,                          /* execute */
3051   NULL,                                 /* sub */
3052   NULL,                                 /* next */
3053   0,                                    /* static_pass_number */
3054   0,                            /* tv_id */
3055   PROP_cfg | PROP_ssa | PROP_alias,     /* properties_required */
3056   0,                                    /* properties_provided */
3057   0,                                    /* properties_destroyed */
3058   0,                                    /* todo_flags_start */
3059   TODO_update_ssa | TODO_dump_func 
3060   | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
3061   0                                     /* letter */
3062 };