OSDN Git Service

2004-06-18 Daniel Berlin <dberlin@dberlin.org>
[pf3gnuchains/gcc-fork.git] / gcc / tree-ssa-pre.c
1 /* SSA-PRE for trees.
2    Copyright (C) 2001, 2002, 2003, 2004 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, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA.  */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "errors.h"
27 #include "ggc.h"
28 #include "tree.h"
29
30 /* These RTL headers are needed for basic-block.h.  */
31 #include "rtl.h"
32 #include "tm_p.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
35 #include "diagnostic.h"
36 #include "tree-inline.h"
37 #include "tree-flow.h"
38 #include "tree-gimple.h"
39 #include "tree-dump.h"
40 #include "timevar.h"
41 #include "fibheap.h"
42 #include "hashtab.h"
43 #include "tree-iterator.h"
44 #include "real.h"
45 #include "alloc-pool.h"
46 #include "tree-pass.h"
47 #include "flags.h"
48 #include "splay-tree.h"
49 #include "bitmap.h"
50 #include "langhooks.h"
51 /* TODO:
52    
53    1. Implement load value numbering.
54    2. Speed up insert_aux so that we can use it all the time.  It
55       spends most of it's time in quadratic value replacement.
56    3. Avail sets can be shared by making an avail_find_leader that
57       walks up the dominator tree and looks in those avail sets.
58       This might affect code optimality, it's unclear right now.
59    4. Load motion can be performed by value numbering the loads the
60       same as we do other expressions.  This requires iterative
61       hashing the vuses into the values.  Right now we simply assign
62       a new value every time we see a statement with a vuse.
63    5. Strength reduction can be performed by anticipating expressions
64       we can repair later on.
65 */   
66
67 /* For ease of terminology, "expression node" in the below refers to
68    every expression node but MODIFY_EXPR, because MODIFY_EXPR's represent
69    the actual statement containing the expressions we care about, and
70    we cache the value number by putting it in the expression.  */
71
72 /* Basic algorithm
73    
74    First we walk the statements to generate the AVAIL sets, the EXP_GEN
75    sets, and the tmp_gen sets.  AVAIL is a forward dataflow
76    problem. EXP_GEN sets represent the generation of
77    values/expressions by a given block.  We use them when computing
78    the ANTIC sets.  The AVAIL sets consist of SSA_NAME's that
79    represent values, so we know what values are available in what
80    blocks.  In SSA, values are never killed, so we don't need a kill
81    set, or a fixpoint iteration, in order to calculate the AVAIL sets.
82    In traditional parlance, AVAIL sets tell us the downsafety of the
83    expressions/values.
84    
85    Next, we generate the ANTIC sets.  ANTIC is a backwards dataflow
86    problem.  These sets represent the anticipatable expressions.  An
87    expression is anticipatable in a given block if it could be
88    generated in that block.  This means that if we had to perform an
89    insertion in that block, of the value of that expression, we could.
90    Calculating the ANTIC sets requires phi translation of expressions,
91    because the flow goes backwards through phis.  We must iterate to a
92    fixpoint of the ANTIC sets, because we have a kill set.
93    Even in SSA form, values are not live over the entire function,
94    only from their definition point onwards.  So we have to remove
95    values from the ANTIC set once we go past the definition point of
96    the leaders that make them up.  compute_antic/compute_antic_aux
97    performs this computation.
98
99    Third, we perform insertions to make partially redundant
100    expressions fully redundant.
101
102    An expression is partially redundant (excluding partial
103    anticipation) if:
104
105    1. It is AVAIL in some, but not all, of the predecessors of a
106       given block.
107    2. It is ANTIC in all the predecessors.
108
109    In order to make it fully redundant, we insert the expression into
110    the predecessors where it is not available, but is ANTIC.
111    insert/insert_aux performs this insertion.
112
113    Fourth, we eliminate fully redundant expressions.
114    This is a simple statement walk that replaces redundant
115    calculations  with the now available values.  */
116
117 /* Representations of value numbers:
118
119    Value numbers are represented using the "value handle" approach.
120    This means that each SSA_NAME (and for other reasons to be
121    disclosed in a moment, expression nodes and constant nodes) has a
122    value handle that can be retrieved through get_value_handle.  This
123    value handle, *is* the value number of the SSA_NAME.  You can
124    pointer compare the value handles for equivalence purposes.
125
126    For debugging reasons, the value handle is internally more than
127    just a number, it is a VAR_DECL named "value.x", where x is a
128    unique number for each value number in use.  This allows
129    expressions with SSA_NAMES replaced by value handles to still be
130    pretty printed in a sane way.  They simply print as "value.3 *
131    value.5", etc.  
132
133    Expression nodes have value handles associated with them as a
134    cache.  Otherwise, we'd have to look them up again in the hash
135    table This makes significant difference (factor of two or more) on
136    some test cases.  They can be thrown away after the Constants have
137    value handles associated with them so that they aren't special
138    cased everywhere, and for consistency sake. This may be changed
139    depending on memory usage vs code maintenance tradeoff.  */
140
141 /* Representation of expressions on value numbers: 
142
143    In some portions of this code, you will notice we allocate "fake"
144    analogues to the expression we are value numbering, and replace the
145    operands with the values of the expression.  Since we work on
146    values, and not just names, we canonicalize expressions to value
147    expressions for use in the ANTIC sets, the EXP_GEN set, etc.  
148
149    This is theoretically unnecessary, it just saves a bunch of
150    repeated get_value_handle and find_leader calls in the remainder of
151    the code, trading off temporary memory usage for speed.  The tree
152    nodes aren't actually creating more garbage, since they are
153    allocated in a special pools which are thrown away at the end of
154    this pass.  
155
156    All of this also means that if you print the EXP_GEN or ANTIC sets,
157    you will see "value.5 + value.7" in the set, instead of "a_55 +
158    b_66" or something.  The only thing that actually cares about
159    seeing the value leaders is phi translation, and it needs to be
160    able to find the leader for a value in an arbitrary block, so this
161    "value expression" form is perfect for it (otherwise you'd do
162    get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
163
164
165 /* Representation of sets:
166
167    Sets are represented as doubly linked lists kept in topological
168    order, with an optional supporting bitmap of values present in the
169    set.  The sets represent values, and the elements can be constants,
170    values, or expressions.  The elements can appear in different sets,
171    but each element can only appear once in each set.
172
173    Since each node in the set represents a value, we also want to be
174    able to map expression, set pairs to something that tells us
175    whether the value is present is a set.  We use a per-set bitmap for
176    that.  The value handles also point to a linked list of the
177    expressions they represent via a tree annotation.  This is mainly
178    useful only for debugging, since we don't do identity lookups.  */
179
180
181 /* A value set element.  Basically a single linked list of
182    expressions/constants/values.  */
183 typedef struct value_set_node
184 {
185   /* An expression.  */
186   tree expr;
187
188   /* A pointer to the next element of the value set.  */
189   struct value_set_node *next;
190 } *value_set_node_t;
191
192
193 /* A value set.  This is a singly linked list of value_set_node
194    elements with a possible bitmap that tells us what values exist in
195    the set.  This set must be kept in topologically sorted order.  */
196 typedef struct value_set
197 {
198   /* The head of the list.  Used for iterating over the list in
199      order.  */
200   value_set_node_t head;
201
202   /* The tail of the list.  Used for tail insertions, which are
203      necessary to keep the set in topologically sorted order because
204      of how the set is built.  */
205   value_set_node_t tail;
206   
207   /* The length of the list.  */
208   size_t length;
209   
210   /* True if the set is indexed, which means it contains a backing
211      bitmap for quick determination of whether certain values exist in the
212      set.  */
213   bool indexed;
214   
215   /* The bitmap of values that exist in the set.  May be NULL in an
216      empty or non-indexed set.  */
217   bitmap values;
218   
219 } *value_set_t;
220
221 /* All of the following sets, except for TMP_GEN, are indexed.
222    TMP_GEN is only ever iterated over, we never check what values
223    exist in it.  */
224
225 typedef struct bb_value_sets
226 {
227   /* The EXP_GEN set, which represents expressions/values generated in
228      a basic block.  */
229   value_set_t exp_gen;
230
231   /* The PHI_GEN set, which represents PHI results generated in a
232      basic block.  */
233   value_set_t phi_gen;
234
235   /* The TMP_GEN set, which represents results/temporaries genererated
236      in a basic block. IE the LHS of an expression.  */
237   value_set_t tmp_gen;
238
239   /* The AVAIL_OUT set, which represents which values are available in
240      a given basic block.  */
241   value_set_t avail_out;
242
243   /* The ANTIC_IN set, which represents which values are anticiptable
244      in a given basic block.  */
245   value_set_t antic_in;
246
247   /* The NEW_SETS set, which is used during insertion to augment the
248      AVAIL_OUT set of blocks with the new insertions performed during
249      the current iteration.  */
250   value_set_t new_sets;
251 } *bb_value_sets_t;
252
253 #define EXP_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->exp_gen
254 #define PHI_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->phi_gen
255 #define TMP_GEN(BB)     ((bb_value_sets_t) ((BB)->aux))->tmp_gen
256 #define AVAIL_OUT(BB)   ((bb_value_sets_t) ((BB)->aux))->avail_out
257 #define ANTIC_IN(BB)    ((bb_value_sets_t) ((BB)->aux))->antic_in
258 #define NEW_SETS(BB)    ((bb_value_sets_t) ((BB)->aux))->new_sets
259
260 /* This structure is used to keep track of statistics on what
261    optimization PRE was able to perform.  */
262 static struct
263 {
264   /* The number of RHS computations eliminated by PRE.  */
265   int eliminations;
266
267   /* The number of new expressions/temporaries generated by PRE.  */
268   int insertions;
269
270   /* The number of new PHI nodes added by PRE.  */
271   int phis;
272 } pre_stats;
273
274 static tree find_leader (value_set_t, tree);
275 static void value_insert_into_set (value_set_t, tree);
276 static void insert_into_set (value_set_t, tree);
277 static void add_to_value (tree, tree);
278 static value_set_t set_new  (bool);
279 static bool is_undefined_value (tree);
280 static bool expressions_equal_p (tree, tree);
281
282 /* We can add and remove elements and entries to and from sets
283    and hash tables, so we use alloc pools for them.  */
284
285 static alloc_pool value_set_pool;
286 static alloc_pool value_set_node_pool;
287 static alloc_pool binary_node_pool;
288 static alloc_pool unary_node_pool;
289
290 /* The value table that maps expressions to values.  */
291
292 static htab_t value_table;
293
294 /* The phi_translate_table caches phi translations for a given
295    expression and predecessor.  */
296
297 static htab_t phi_translate_table;
298
299 /* Compare two expressions E1 and E2 and return true if they are
300    equal.  */
301
302 static bool
303 expressions_equal_p (tree e1, tree e2)
304 {
305   tree te1, te2;
306   
307   if (e1 == e2)
308     return true;
309   
310   te1 = TREE_TYPE (e1);
311   te2 = TREE_TYPE (e2);
312
313   if (TREE_CODE (e1) == TREE_CODE (e2) 
314       && (te1 == te2 || lang_hooks.types_compatible_p (te1, te2))
315       && operand_equal_p (e1, e2, 0))
316     return true;
317   return false;
318 }
319
320 /* Map expressions to values.  These are simple pairs of expressions
321    and the values they represent.  To find the value represented by
322    an expression, we use a hash table where the elements are {e,v}
323    pairs, and the expression is the key.  */
324
325 typedef struct val_expr_pair_d
326 {
327   tree v, e;
328   hashval_t hashcode;
329 } *val_expr_pair_t;
330
331
332 /* Hash a {v,e} pair that is pointed to by P.
333    The hashcode is cached in the val_expr_pair, so we just return
334    that.  */
335
336 static hashval_t
337 val_expr_pair_hash (const void *p)
338 {
339   const val_expr_pair_t ve = (val_expr_pair_t) p;
340   return ve->hashcode;
341 }
342
343
344 /* Given two val_expr_pair_t's, return true if they represent the same
345    expression, false otherwise.
346    P1 and P2 should point to the val_expr_pair_t's to be compared.  */
347
348 static int
349 val_expr_pair_expr_eq (const void *p1, const void *p2)
350 {
351   const val_expr_pair_t ve1 = (val_expr_pair_t) p1;
352   const val_expr_pair_t ve2 = (val_expr_pair_t) p2;
353
354   if (expressions_equal_p (ve1->e, ve2->e))
355     return true;
356   
357   return false;
358 }
359
360
361 /* Get the value handle of EXPR.  This is the only correct way to get
362    the value handle for a "thing". 
363    Returns NULL if the value handle does not exist.  */
364
365 tree
366 get_value_handle (tree expr)
367 {
368   /* We should never see these.  */
369   if (DECL_P (expr))
370     abort ();
371   else if (TREE_CODE (expr) == SSA_NAME)
372     {
373       return SSA_NAME_VALUE (expr);
374     }
375   else if (TREE_CODE_CLASS (TREE_CODE (expr)) == 'c'
376            || EXPR_P (expr))
377     {
378       tree_ann_t ann = tree_ann (expr);  
379       if (ann)
380         return ann->common.value_handle;
381       return NULL;
382     }
383   abort ();
384 }
385
386
387 /* Set the value handle for expression E to value V */
388    
389 void
390 set_value_handle (tree e, tree v)
391 {
392   if (DECL_P (e))
393     abort ();
394   else if (TREE_CODE (e) == SSA_NAME)
395     SSA_NAME_VALUE (e) = v;
396   else if (TREE_CODE_CLASS (TREE_CODE (e)) == 'c'
397            || EXPR_P (e))
398     get_tree_ann (e)->common.value_handle = v;
399 }
400
401 /* A three tuple {e, pred, v} used to cache phi translations in the
402    phi_translate_table.  */
403
404 typedef struct expr_pred_trans_d
405 {
406   /* The expression. */
407   tree e;
408
409   /* The predecessor block along which we translated the expression.  */
410   basic_block pred;
411
412   /* The value that resulted from the translation.  */
413   tree v;
414
415   /* The hashcode for the expression, pred pair. This is cached for
416      speed reasons.  */
417   hashval_t hashcode;
418 } *expr_pred_trans_t;
419
420 /* Return the hash value for a phi translation table entry.  */
421
422 static hashval_t
423 expr_pred_trans_hash (const void *p)
424 {
425   const expr_pred_trans_t ve = (expr_pred_trans_t) p;
426   return ve->hashcode;
427 }
428
429 /* Return true if two phi translation table entries are the same.
430    P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
431
432 static int
433 expr_pred_trans_eq (const void *p1, const void *p2)
434 {
435   const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
436   const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
437   basic_block b1 = ve1->pred;
438   basic_block b2 = ve2->pred;
439
440   
441   /* If they are not translations for the same basic block, they can't
442      be equal.  */
443   if (b1 != b2)
444     return false;
445
446   /* If they are for the same basic block, determine if the
447      expressions are equal.   */  
448   if (expressions_equal_p (ve1->e, ve2->e))
449     return true;
450   
451   return false;
452 }
453
454 /* Search in the phi translation table for the translation of
455    expression E in basic block PRED. Return the translated value, if
456    found, NULL otherwise. */ 
457
458 static inline tree
459 phi_trans_lookup (tree e, basic_block pred)
460 {
461   void **slot;
462   struct expr_pred_trans_d ept;
463   ept.e = e;
464   ept.pred = pred;
465   ept.hashcode = iterative_hash_expr (e, (unsigned long) pred);
466   slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
467                                    NO_INSERT);
468   if (!slot)
469     return NULL;
470   else
471     return ((expr_pred_trans_t) *slot)->v;
472 }
473
474
475 /* Add the tuple mapping from {expression E, basic block PRED} to
476    value V, to the phi translation table.  */
477
478 static inline void
479 phi_trans_add (tree e, tree v, basic_block pred)
480 {
481   void **slot;
482   expr_pred_trans_t new_pair = xmalloc (sizeof (*new_pair));
483   new_pair->e = e;
484   new_pair->pred = pred;
485   new_pair->v = v;
486   new_pair->hashcode = iterative_hash_expr (e, (unsigned long) pred);
487   slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
488                                    new_pair->hashcode, INSERT);
489   if (*slot)
490     free (*slot);
491   *slot = (void *) new_pair;
492 }
493
494 /* Search in TABLE for an existing instance of expression E,
495    and return its value, or NULL if none has been set.  */
496
497 static inline tree
498 lookup (htab_t table, tree e)
499 {
500   void **slot;
501   struct val_expr_pair_d vep = {NULL, NULL, 0};
502   vep.e = e;
503   vep.hashcode = iterative_hash_expr (e,0); 
504   slot = htab_find_slot_with_hash (table, &vep, vep.hashcode, NO_INSERT);
505   if (!slot)
506     return NULL_TREE;
507   else
508     return ((val_expr_pair_t) *slot)->v;
509 }
510
511 /* Add expression E to the expression set of value V.  */
512
513 static inline void
514 add_to_value (tree v, tree e)
515 {
516 #if DEBUG_VALUE_EXPRESSIONS
517   var_ann_t va = var_ann (v);
518 #endif
519   /* For values representing numerical constants, we mark
520      TREE_CONSTANT as true and set the tree chain to the actual
521      constant.  This is because unlike values involving expressions,
522      which are only available to use where the expressions are live, a
523      constant can be remade anywhere, and thus, is available
524      everywhere.  */
525   if (TREE_CODE_CLASS (TREE_CODE (e)) == 'c')
526     {
527       TREE_CONSTANT (v) = true;
528       TREE_CHAIN (v) = e;
529     }
530   else if (is_gimple_min_invariant (e))
531     {
532       TREE_CONSTANT (v) = true;
533       TREE_CHAIN (v) = e;
534     }
535 #if DEBUG_VALUE_EXPRESSIONS
536   if (va->expr_set == NULL)
537     va->expr_set = set_new (false);
538   insert_into_set (va->expr_set, e);
539 #endif
540 }
541
542 /* Insert E into TABLE with value V, and add expression E to the value
543    set for value V.  */
544
545 static inline void
546 add (htab_t table, tree e, tree v)
547 {
548
549   void **slot;
550   val_expr_pair_t new_pair = xmalloc (sizeof (struct val_expr_pair_d));
551   new_pair->e = e;
552   new_pair->v = v;
553   new_pair->hashcode = iterative_hash_expr (e, 0);
554   slot = htab_find_slot_with_hash (table, new_pair, new_pair->hashcode,
555                                    INSERT);
556   if (*slot)
557     free (*slot);
558   *slot = (void *) new_pair;
559   set_value_handle (e, v);
560
561   add_to_value (v, e);
562  
563 }
564
565 /* A unique counter that is incremented every time we create a new
566    value.  */
567 static int pre_uid;
568
569 /* Create a new value handle for expression EXPR.  */
570
571 static tree
572 create_new_value (tree expr)
573 {
574   tree a = create_tmp_var_raw (TREE_TYPE (expr), "value");
575   create_var_ann (a);
576   var_ann (a)->uid = pre_uid++;
577
578   if (dump_file && (dump_flags & TDF_DETAILS))
579     {     
580       fprintf (dump_file, "Created value ");
581       print_generic_expr (dump_file, a, dump_flags);
582       fprintf (dump_file, " for ");
583       print_generic_expr (dump_file, expr, dump_flags);
584       fprintf (dump_file, "\n");
585     }
586   return a;
587 }
588
589 /* Like lookup, but creates a new value for expression E if E doesn't
590    already have a value.
591    Return the existing/created value for E.  */
592
593 static inline tree
594 lookup_or_add (htab_t table, tree e)
595 {
596   tree x = lookup (table, e);
597   if (x == NULL_TREE)
598     {
599       tree v;
600       v = create_new_value (e);
601       add (table, e, v);
602       x = v;
603     }
604   set_value_handle (e, x);
605   return x;
606 }
607
608   
609 /* Return true if value V exists in the bitmap for SET.  */
610
611 static inline bool
612 value_exists_in_set_bitmap (value_set_t set, tree v)
613 {
614   if (TREE_CODE (v) != VAR_DECL)
615     abort ();
616
617   if (!set->values)
618     return false;
619   return bitmap_bit_p (set->values, get_var_ann (v)->uid);
620 }
621
622 /* Remove value V from the bitmap for SET.  */
623
624 static void
625 value_remove_from_set_bitmap (value_set_t set, tree v)
626 {
627   if (TREE_CODE (v) != VAR_DECL)
628     abort ();
629 #ifdef ENABLE_CHECKING
630   if (!set->indexed)
631     abort ();
632 #endif
633   if (!set->values)
634     return;
635   bitmap_clear_bit (set->values, get_var_ann (v)->uid);
636 }
637
638
639 /* Insert the value number V into the bitmap of values existing in
640    SET.  */
641
642 static inline void
643 value_insert_into_set_bitmap (value_set_t set, tree v)
644 {
645   if (TREE_CODE (v) != VAR_DECL)
646     abort ();  
647 #ifdef ENABLE_CHECKING
648   if (!set->indexed)
649     abort ();
650 #endif
651   if (set->values == NULL)
652     {
653       set->values = BITMAP_GGC_ALLOC ();
654       bitmap_clear (set->values);
655     }
656   bitmap_set_bit (set->values, get_var_ann (v)->uid);
657 }
658
659 /* Create a new set.  */
660
661 static value_set_t
662 set_new  (bool indexed)
663 {
664   value_set_t ret;
665   ret = pool_alloc (value_set_pool);
666   ret->head = ret->tail = NULL;
667   ret->length = 0;
668   ret->indexed = indexed;
669   ret->values = NULL;
670   return ret;
671 }
672
673
674 /* Insert EXPR into SET.  */
675
676 static void
677 insert_into_set (value_set_t set, tree expr)
678 {
679   value_set_node_t newnode = pool_alloc (value_set_node_pool);
680   tree val = get_value_handle (expr);
681   if (DECL_P (expr))
682     abort ();
683   
684   if (val == NULL)
685     abort ();
686
687   /* For indexed sets, insert the value into the set value bitmap.
688      For all sets, add it to the linked list and increment the list
689      length.  */
690   if (set->indexed)
691     value_insert_into_set_bitmap (set, val);
692
693   newnode->next = NULL;
694   newnode->expr = expr;
695   set->length ++;
696   if (set->head == NULL)
697     {
698       set->head = set->tail = newnode;
699     }
700   else
701     {
702       set->tail->next = newnode;
703       set->tail = newnode;
704     }
705 }
706
707 /* Copy the set ORIG to the set DEST.  */
708
709 static void
710 set_copy (value_set_t dest, value_set_t orig)
711 {
712   value_set_node_t node;
713  
714   if (!orig || !orig->head)
715     return;
716
717   for (node = orig->head;
718        node;
719        node = node->next)
720     {
721       insert_into_set (dest, node->expr);
722     }
723 }
724
725 /* Remove EXPR from SET.  */
726
727 static void
728 set_remove (value_set_t set, tree expr)
729 {
730   value_set_node_t node, prev;
731
732   /* Remove the value of EXPR from the bitmap, decrement the set
733      length, and remove it from the actual double linked list.  */ 
734   value_remove_from_set_bitmap (set, get_value_handle (expr));
735   set->length--;
736   prev = NULL;
737   for (node = set->head; 
738        node != NULL; 
739        prev = node, node = node->next)
740     {
741       if (node->expr == expr)
742         {
743           if (prev == NULL)
744             set->head = node->next;
745           else
746             prev->next= node->next;
747  
748           if (node == set->tail)
749             set->tail = prev;
750           pool_free (value_set_node_pool, node);
751           return;
752         }
753     }
754 }
755
756 /* Return true if SET contains the value VAL.  */
757
758 static bool
759 set_contains_value (value_set_t set, tree val)
760 {
761   /* This is only referring to the flag above that we set on
762      values referring to numerical constants, because we know that we
763      are dealing with one of the value handles we created.  */
764   if (TREE_CONSTANT (val))
765     return true;
766   
767   if (set->length == 0)
768     return false;
769   
770   return value_exists_in_set_bitmap (set, val);
771 }
772
773 /* Replace the leader for the value LOOKFOR in SET with EXPR.  */
774
775 static void
776 set_replace_value (value_set_t set, tree lookfor, tree expr)
777 {
778   value_set_node_t node = set->head;
779
780   /* The lookup is probably more expensive than walking the linked
781      list when we have only a small number of nodes.  */
782   if (!set_contains_value (set, lookfor))
783     return;
784
785   for (node = set->head;
786        node;
787        node = node->next)
788     {
789       if (get_value_handle (node->expr) == lookfor)
790         {
791           node->expr = expr;
792           return;
793         }
794     }
795 }
796
797 /* Return true if the set contains expression (not value) EXPR.  */
798
799 static bool
800 set_contains (value_set_t set, tree expr)
801 {
802   value_set_node_t node;
803   
804   for (node = set->head;
805        node;
806        node = node->next)
807     {
808       if (operand_equal_p (node->expr, expr, 0))
809         return true;
810     }
811   return false;
812 }
813
814 /* Subtract set B from set A, and return the new set.  */
815
816 static value_set_t
817 set_subtract (value_set_t a, value_set_t b, bool indexed)
818 {
819   value_set_t ret = set_new (indexed);
820   value_set_node_t node;
821   for (node = a->head;
822        node;
823        node = node->next)
824     {
825       if (!set_contains (b, node->expr))
826         insert_into_set (ret, node->expr);
827     }
828   return ret;
829 }
830
831 /* Return true if two sets are equal. */
832
833 static bool
834 set_equal (value_set_t a, value_set_t b)
835 {
836   value_set_node_t node;
837
838   if (a->length != b->length)
839     return false;
840   for (node = a->head;
841        node;
842        node = node->next)
843     {
844       if (!set_contains_value (b, get_value_handle (node->expr)))
845         return false;
846     }
847   return true;
848 }
849
850 /* Replace the value for EXPR in SET with EXPR.  */
851 static void
852 value_replace_in_set (value_set_t set, tree expr)
853 {
854   tree val = get_value_handle (expr);
855
856   if (set->length == 0)
857     return;
858   
859   set_replace_value (set, val, expr);
860 }
861
862 /* Insert the value for EXPR into SET, if it doesn't exist already.  */
863
864 static void
865 value_insert_into_set (value_set_t set, tree expr)
866 {
867   tree val = get_value_handle (expr);
868
869   /* Constant values exist everywhere.  */
870   if (TREE_CONSTANT (val))
871     return;
872
873   if (!set_contains_value (set, val))
874     insert_into_set (set, expr);
875 }
876
877
878 /* Print out the value_set SET to OUTFILE.  */
879
880 static void
881 print_value_set (FILE *outfile, value_set_t set,
882                  const char *setname, int blockindex)
883 {
884   value_set_node_t node;
885   fprintf (outfile, "%s[%d] := { ", setname, blockindex);
886   if (set)
887     {
888       for (node = set->head;
889            node;
890            node = node->next)
891         {
892           print_generic_expr (outfile, node->expr, 0);
893           
894           fprintf (outfile, " (");
895           print_generic_expr (outfile, get_value_handle (node->expr), 0);
896           fprintf (outfile, ") ");
897                      
898           if (node->next)
899             fprintf (outfile, ", ");
900         }
901     }
902
903   fprintf (outfile, " }\n");
904 }
905
906 /* Print out the expressions that have VAL to OUTFILE.  */
907 void
908 print_value_expressions (FILE *outfile, tree val)
909 {
910   var_ann_t va = var_ann (val);
911   if (va && va->expr_set)
912     print_value_set (outfile, va->expr_set, 
913                      IDENTIFIER_POINTER (DECL_NAME (val)), 0);
914 }
915
916
917 void
918 debug_value_expressions (tree val)
919 {
920   print_value_expressions (stderr, val);
921 }
922
923   
924 void debug_value_set (value_set_t, const char *, int);
925
926 void
927 debug_value_set (value_set_t set, const char *setname, int blockindex)
928 {
929   print_value_set (stderr, set, setname, blockindex);
930 }
931
932 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
933    the phis in PRED.  Return NULL if we can't find a leader for each
934    part of the translated expression.  */
935
936 static tree
937 phi_translate (tree expr, value_set_t set,  basic_block pred,
938                basic_block phiblock)
939 {
940   tree phitrans = NULL;
941   tree oldexpr = expr;
942   
943   if (expr == NULL)
944     return NULL;
945
946   /* Phi translations of a given expression don't change,  */
947   phitrans = phi_trans_lookup (expr, pred);
948   if (phitrans)
949     return phitrans;
950   
951   
952   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
953     {
954     case '2':
955       {
956         tree oldop1 = TREE_OPERAND (expr, 0);
957         tree oldop2 = TREE_OPERAND (expr, 1);
958         tree newop1;
959         tree newop2;
960         tree newexpr;
961         
962         newop1 = phi_translate (find_leader (set, oldop1),
963                                 set, pred, phiblock);
964         if (newop1 == NULL)
965           return NULL;
966         newop2 = phi_translate (find_leader (set, oldop2),
967                                 set, pred, phiblock);
968         if (newop2 == NULL)
969           return NULL;
970         if (newop1 != oldop1 || newop2 != oldop2)
971           {
972             newexpr = pool_alloc (binary_node_pool);
973             memcpy (newexpr, expr, tree_size (expr));
974             create_tree_ann (newexpr);
975             TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
976             TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
977             lookup_or_add (value_table, newexpr);
978             expr = newexpr;
979             phi_trans_add (oldexpr, newexpr, pred);         
980           }
981       }
982       break;
983     case '1':
984       {
985         tree oldop1 = TREE_OPERAND (expr, 0);
986         tree newop1;
987         tree newexpr;
988
989         newop1 = phi_translate (find_leader (set, oldop1),
990                                 set, pred, phiblock);
991         if (newop1 == NULL)
992           return NULL;
993         if (newop1 != oldop1)
994           {
995             newexpr = pool_alloc (unary_node_pool);
996             memcpy (newexpr, expr, tree_size (expr));
997             create_tree_ann (newexpr);   
998             TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
999             lookup_or_add (value_table, newexpr);
1000             expr = newexpr;
1001             phi_trans_add (oldexpr, newexpr, pred);
1002           }
1003       }
1004       break;
1005     case 'd':
1006       abort ();
1007     case 'x':
1008       {
1009         tree phi = NULL;
1010         int i;
1011         if (TREE_CODE (expr) != SSA_NAME)
1012           abort ();
1013         if (TREE_CODE (SSA_NAME_DEF_STMT (expr)) == PHI_NODE)
1014           phi = SSA_NAME_DEF_STMT (expr);
1015         else
1016           return expr;
1017         
1018         for (i = 0; i < PHI_NUM_ARGS (phi); i++)
1019           if (PHI_ARG_EDGE (phi, i)->src == pred)
1020             {
1021               tree val;
1022               if (is_undefined_value (PHI_ARG_DEF (phi, i)))
1023                 return NULL;
1024               val = lookup_or_add (value_table, PHI_ARG_DEF (phi, i));
1025               return PHI_ARG_DEF (phi, i);
1026             }
1027       }
1028       break;
1029     }
1030   return expr;
1031 }
1032
1033 static void
1034 phi_translate_set (value_set_t dest, value_set_t set, basic_block pred,
1035                    basic_block phiblock)
1036 {
1037   value_set_node_t node;
1038   for (node = set->head;
1039        node;
1040        node = node->next)
1041     {
1042       tree translated;
1043       translated = phi_translate (node->expr, set, pred, phiblock);
1044       phi_trans_add (node->expr, translated, pred);
1045       
1046       if (translated != NULL)
1047         value_insert_into_set (dest, translated);
1048     } 
1049 }
1050
1051 /* Find the leader for a value (IE the name representing that
1052    value) in a given set, and return it.  Return NULL if no leader is
1053    found.  */
1054
1055 static tree
1056 find_leader (value_set_t set, tree val)
1057 {
1058   value_set_node_t node;
1059
1060   if (val == NULL)
1061     return NULL;
1062
1063   if (TREE_CONSTANT (val))
1064     return TREE_CHAIN (val);
1065
1066   if (set->length == 0)
1067     return NULL;
1068   
1069   if (value_exists_in_set_bitmap (set, val))
1070     {
1071       for (node = set->head;
1072            node;
1073            node = node->next)
1074         {
1075           if (get_value_handle (node->expr) == val)
1076             return node->expr;
1077         }
1078     }
1079   return NULL;
1080 }
1081
1082 /* Determine if the expression EXPR is valid in SET.  This means that
1083    we have a leader for each part of the expression (if it consists of
1084    values), or the expression is an SSA_NAME.  
1085
1086    NB:  We never should run into a case where we have SSA_NAME +
1087    SSA_NAME or SSA_NAME + value.  The sets valid_in_set is called on,
1088    the ANTIC sets, will only ever have SSA_NAME's or binary value
1089    expression (IE VALUE1 + VALUE2)  */
1090
1091 static bool
1092 valid_in_set (value_set_t set, tree expr)
1093 {
1094   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1095     {
1096     case '2':
1097       {
1098         tree op1 = TREE_OPERAND (expr, 0);
1099         tree op2 = TREE_OPERAND (expr, 1);
1100         return set_contains_value (set, op1) && set_contains_value (set, op2);
1101       }
1102       break;
1103     case '1':
1104       {
1105         tree op1 = TREE_OPERAND (expr, 0);
1106         return set_contains_value (set, op1);
1107       }
1108       break;
1109     case 'x':
1110       {
1111         if (TREE_CODE (expr) == SSA_NAME)
1112           return true;
1113         abort ();
1114       }
1115     case 'c':
1116       abort ();
1117     }
1118   return false;
1119 }
1120
1121 /* Clean the set of expressions that are no longer valid in SET.  This
1122    means expressions that are made up of values we have no leaders for
1123    in SET.  */
1124
1125 static void
1126 clean (value_set_t set)
1127 {
1128   value_set_node_t node;
1129   value_set_node_t next;
1130   node = set->head;
1131   while (node)
1132     {
1133       next = node->next;
1134       if (!valid_in_set (set, node->expr))      
1135         set_remove (set, node->expr);
1136       node = next;
1137     }
1138 }
1139
1140 /* Compute the ANTIC set for BLOCK.
1141
1142 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK), if
1143 succs(BLOCK) > 1
1144 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)]) if
1145 succs(BLOCK) == 1
1146
1147 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] -
1148 TMP_GEN[BLOCK])
1149
1150 Iterate until fixpointed.
1151
1152 XXX: It would be nice to either write a set_clear, and use it for
1153 antic_out, or to mark the antic_out set as deleted at the end
1154 of this routine, so that the pool can hand the same memory back out
1155 again for the next antic_out.  */
1156
1157
1158 static bool
1159 compute_antic_aux (basic_block block)
1160 {
1161   basic_block son;
1162   edge e;
1163   bool changed = false;
1164   value_set_t S, old, ANTIC_OUT;
1165   value_set_node_t node;
1166   
1167   ANTIC_OUT = S = NULL;
1168   /* If any edges from predecessors are abnormal, antic_in is empty, so
1169      punt.  Remember that the block has an incoming abnormal edge by
1170      setting the BB_VISITED flag.  */
1171   if (! (block->flags & BB_VISITED))
1172     {
1173       for (e = block->pred; e; e = e->pred_next)
1174         if (e->flags & EDGE_ABNORMAL)
1175           {
1176             block->flags |= BB_VISITED;
1177             break;
1178           }
1179     }
1180   if (block->flags & BB_VISITED)
1181     {
1182       S = NULL;
1183       goto visit_sons;
1184     }
1185   
1186
1187   old = set_new (false);
1188   set_copy (old, ANTIC_IN (block));
1189   ANTIC_OUT = set_new (true);
1190
1191   /* If the block has no successors, ANTIC_OUT is empty, because it is
1192      the exit block.  */
1193   if (block->succ == NULL);
1194
1195   /* If we have one successor, we could have some phi nodes to
1196      translate through.  */
1197   else if (block->succ->succ_next == NULL)
1198     {
1199       phi_translate_set (ANTIC_OUT, ANTIC_IN(block->succ->dest),
1200                          block, block->succ->dest);
1201     }
1202   /* If we have multiple successors, we take the intersection of all of
1203      them.  */
1204   else
1205     {
1206       varray_type worklist;
1207       edge e;
1208       size_t i;
1209       basic_block bprime, first;
1210
1211       VARRAY_BB_INIT (worklist, 1, "succ");
1212       e = block->succ;
1213       while (e)
1214         {
1215           VARRAY_PUSH_BB (worklist, e->dest);
1216           e = e->succ_next;
1217         }
1218       first = VARRAY_BB (worklist, 0);
1219       set_copy (ANTIC_OUT, ANTIC_IN (first));
1220
1221       for (i = 1; i < VARRAY_ACTIVE_SIZE (worklist); i++)
1222         {
1223           bprime = VARRAY_BB (worklist, i);
1224           node = ANTIC_OUT->head;
1225           while (node)
1226             {
1227               tree val;
1228               value_set_node_t next = node->next;
1229               val = get_value_handle (node->expr);
1230               if (!set_contains_value (ANTIC_IN (bprime), val))
1231                 set_remove (ANTIC_OUT, node->expr);
1232               node = next;
1233             }
1234         }
1235       VARRAY_CLEAR (worklist);
1236     }
1237
1238   /* Generate ANTIC_OUT - TMP_GEN */
1239   S = set_subtract (ANTIC_OUT, TMP_GEN (block), false);
1240
1241   /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1242   ANTIC_IN (block) = set_subtract (EXP_GEN (block),TMP_GEN (block), true);
1243   
1244   /* Then union in the ANTIC_OUT - TMP_GEN values, to get ANTIC_OUT U
1245      EXP_GEN - TMP_GEN */
1246   for (node = S->head;
1247        node;
1248        node = node->next)
1249     {
1250       value_insert_into_set (ANTIC_IN (block), node->expr);
1251     }
1252   clean (ANTIC_IN (block));
1253   
1254
1255   if (!set_equal (old, ANTIC_IN (block)))
1256     changed = true;
1257
1258  visit_sons:
1259   if (dump_file && (dump_flags & TDF_DETAILS))
1260     {
1261       if (ANTIC_OUT)
1262         print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1263       print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1264       if (S)
1265         print_value_set (dump_file, S, "S", block->index);
1266
1267     }
1268
1269   for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1270        son;
1271        son = next_dom_son (CDI_POST_DOMINATORS, son))
1272     {
1273       changed |= compute_antic_aux (son);
1274     }
1275   return changed;
1276 }
1277
1278 /* Compute ANTIC sets.  */
1279
1280 static void
1281 compute_antic (void)
1282 {
1283   bool changed = true;
1284   basic_block bb;
1285   int num_iterations = 0;
1286   FOR_ALL_BB (bb)
1287     {
1288       ANTIC_IN (bb) = set_new (true);
1289       bb->flags &= ~BB_VISITED;
1290     }
1291
1292   while (changed)
1293     {
1294       num_iterations++;
1295       changed = false;
1296       changed = compute_antic_aux (EXIT_BLOCK_PTR);
1297     }
1298   if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1299     fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1300 }
1301
1302 /* Perform insertion of partially redundant values.
1303    For BLOCK, do the following:
1304    1.  Propagate the NEW_SETS of the dominator into the current block.
1305    If the block has multiple predecessors, 
1306        2a. Iterate over the ANTIC expressions for the block to see if
1307            any of them are partially redundant.
1308        2b. If so, insert them into the necessary predecessors to make
1309            the expression fully redundant.
1310        2c. Insert a new PHI merging the values of the predecessors.
1311        2d. Insert the new PHI, and the new expressions, into the
1312            NEW_SETS set.  
1313    3. Recursively call ourselves on the dominator children of BLOCK.
1314
1315 */
1316 static bool
1317 insert_aux (basic_block block)
1318 {
1319   basic_block son;
1320   bool new_stuff = false;
1321
1322   if (block)
1323     {
1324       value_set_node_t e;
1325       basic_block dom;
1326       dom = get_immediate_dominator (CDI_DOMINATORS, block);
1327       if (dom)
1328         {
1329           e = NEW_SETS (dom)->head;
1330           while (e)
1331             {
1332               insert_into_set (NEW_SETS (block), e->expr);
1333               value_replace_in_set (AVAIL_OUT (block), e->expr);
1334               e = e->next;
1335             }
1336           if (block->pred->pred_next)
1337             {
1338               value_set_node_t node;
1339               for (node = ANTIC_IN (block)->head;
1340                    node;
1341                    node = node->next)
1342                 {
1343                   if (TREE_CODE_CLASS (TREE_CODE (node->expr)) == '2'
1344                       || TREE_CODE_CLASS (TREE_CODE (node->expr)) == '1')
1345                     {
1346                       tree *avail;
1347                       tree val;
1348                       bool by_some = false;
1349                       bool cant_insert = false;
1350                       bool all_same = true;
1351                       tree first_s = NULL;
1352                       edge pred;
1353                       basic_block bprime;
1354                       tree eprime;
1355
1356                       val = get_value_handle (node->expr);
1357                       if (set_contains_value (PHI_GEN (block), val))
1358                         continue; 
1359                       if (set_contains_value (AVAIL_OUT (dom), val))
1360                         {
1361                           if (dump_file && (dump_flags & TDF_DETAILS))
1362                             fprintf (dump_file, "Found fully redundant value\n");
1363                           continue;
1364                         }
1365                                     
1366                       avail = xcalloc (last_basic_block, sizeof (tree));
1367                       for (pred = block->pred;
1368                            pred;
1369                            pred = pred->pred_next)
1370                         {
1371                           tree vprime;
1372                           tree edoubleprime;
1373                           bprime = pred->src;
1374                           eprime = phi_translate (node->expr,
1375                                                   ANTIC_IN (block),
1376                                                   bprime, block);
1377
1378                           /* eprime will generally only be NULL if the
1379                              value of the expression, translated
1380                              through the PHI for this predecessor, is
1381                              undefined.  If that is the case, we can't
1382                              make the expression fully redundant,
1383                              because its value is undefined along a
1384                              predecessor path.  We can thus break out
1385                              early because it doesn't matter what the
1386                              rest of the results are.  */
1387                           if (eprime == NULL)
1388                             {
1389                               cant_insert = true;
1390                               break;
1391                             }
1392
1393                           vprime = get_value_handle (eprime);
1394                           if (!vprime)
1395                             abort ();                     
1396                           edoubleprime = find_leader (AVAIL_OUT (bprime),
1397                                                       vprime);
1398                           if (edoubleprime == NULL)
1399                             {
1400                               avail[bprime->index] = eprime;
1401                               all_same = false;
1402                             }
1403                           else
1404                             {
1405                               avail[bprime->index] = edoubleprime;
1406                               by_some = true; 
1407                               if (first_s == NULL)
1408                                 first_s = edoubleprime;
1409                               else if (first_s != edoubleprime)
1410                                 all_same = false;
1411                               if (first_s != edoubleprime 
1412                                   && operand_equal_p (first_s, edoubleprime, 0))
1413                                 abort ();
1414                             }
1415                         }
1416                       /* If we can insert it, it's not the same value
1417                          already existing along every predecessor, and
1418                          it's defined by some predecessor, it is
1419                          partially redundant.  */
1420                       if (!cant_insert && !all_same && by_some)
1421                         {
1422                           tree type = TREE_TYPE (avail[block->pred->src->index]);
1423                           tree temp;
1424                           tree v;
1425
1426                           if (dump_file && (dump_flags & TDF_DETAILS))
1427                             {
1428                               fprintf (dump_file, "Found partial redundancy for expression ");
1429                               print_generic_expr (dump_file, node->expr, 0);
1430                               fprintf (dump_file, "\n");
1431                             }
1432
1433                           /* Make the necessary insertions. */
1434                           for (pred = block->pred;
1435                                pred;
1436                                pred = pred->pred_next)
1437                             {
1438                               bprime = pred->src;
1439                               eprime = avail[bprime->index];
1440                               if (TREE_CODE_CLASS (TREE_CODE (eprime)) == '2')
1441                                 {
1442                                   tree s1, s2;
1443                                   tree newexpr;
1444                                   s1 = find_leader (AVAIL_OUT (bprime),
1445                                                     TREE_OPERAND (eprime, 0));
1446                                   /* Depending on the order we process
1447                                      DOM branches in, the value may
1448                                      not have propagated to all the
1449                                      dom children yet during this
1450                                      iteration.  In this case, the
1451                                      value will always be in the
1452                                      NEW_SETS for *our* dominator */
1453                                   if (!s1)
1454                                     s1 = find_leader (NEW_SETS (dom),
1455                                                       TREE_OPERAND (eprime, 0));
1456                                   if (!s1)
1457                                     abort ();
1458                                   
1459                                   s2 = find_leader (AVAIL_OUT (bprime),
1460                                                     TREE_OPERAND (eprime, 1));
1461                                   if (!s2)
1462                                     s2 = find_leader (NEW_SETS (dom),
1463                                                       TREE_OPERAND (eprime, 1));
1464                                   if (!s2)
1465                                     abort ();
1466                                   
1467                                   temp = create_tmp_var (TREE_TYPE (eprime),
1468                                                          "pretmp");
1469                                   add_referenced_tmp_var (temp);
1470                                   newexpr = build (TREE_CODE (eprime),
1471                                                    TREE_TYPE (eprime),
1472                                                    s1, s2);
1473                                   newexpr = build (MODIFY_EXPR, 
1474                                                    TREE_TYPE (eprime),
1475                                                    temp, newexpr);
1476                                   temp = make_ssa_name (temp, newexpr);
1477                                   TREE_OPERAND (newexpr, 0) = temp;
1478                                   bsi_insert_on_edge (pred, newexpr);
1479                                   bsi_commit_edge_inserts (NULL);
1480                                   
1481                                   if (dump_file && (dump_flags & TDF_DETAILS))
1482                                     {                               
1483                                       fprintf (dump_file, "Inserted ");
1484                                       print_generic_expr (dump_file, newexpr, 0);
1485                                       fprintf (dump_file, " in predecessor %d\n", pred->src->index);
1486                                     }
1487                                   pre_stats.insertions++;
1488                                   v = lookup_or_add (value_table, eprime);
1489                                   add (value_table, temp, v);
1490                                   insert_into_set (NEW_SETS (bprime), temp);
1491                                   value_insert_into_set (AVAIL_OUT (bprime), 
1492                                                          temp);
1493                                   avail[bprime->index] = temp;
1494                                 }
1495                               else if (TREE_CODE_CLASS (TREE_CODE (eprime)) == '1')
1496                                 {
1497                                   tree s1;
1498                                   tree newexpr;
1499                                   s1 = find_leader (AVAIL_OUT (bprime),
1500                                                     TREE_OPERAND (eprime, 0));
1501                                   /* Depending on the order we process
1502                                      DOM branches in, the value may not have
1503                                      propagated to all the dom
1504                                      children yet in the current
1505                                      iteration, but it will be in
1506                                      NEW_SETS if it is not yet
1507                                      propagated.  */
1508                                      
1509                                   if (!s1)
1510                                     s1 = find_leader (NEW_SETS (dom),
1511                                                       TREE_OPERAND (eprime, 0));
1512                                   if (!s1)
1513                                     abort ();
1514                                   
1515                                   temp = create_tmp_var (TREE_TYPE (eprime),
1516                                                          "pretmp");
1517                                   add_referenced_tmp_var (temp);
1518                                   newexpr = build (TREE_CODE (eprime),
1519                                                    TREE_TYPE (eprime),
1520                                                    s1);
1521                                   newexpr = build (MODIFY_EXPR, 
1522                                                    TREE_TYPE (eprime),
1523                                                    temp, newexpr);
1524                                   temp = make_ssa_name (temp, newexpr);
1525                                   TREE_OPERAND (newexpr, 0) = temp;
1526                                   bsi_insert_on_edge (pred, newexpr);
1527                                   bsi_commit_edge_inserts (NULL);
1528                                   
1529                                   if (dump_file && (dump_flags & TDF_DETAILS))
1530                                     {                               
1531                                       fprintf (dump_file, "Inserted ");
1532                                       print_generic_expr (dump_file, newexpr, 0);
1533                                       fprintf (dump_file, " in predecessor %d\n", pred->src->index);
1534                                     }
1535                                   pre_stats.insertions++;
1536                                   v = lookup_or_add (value_table, eprime);
1537                                   add (value_table, temp, v);
1538                                   insert_into_set (NEW_SETS (bprime), temp);
1539                                   value_insert_into_set (AVAIL_OUT (bprime), 
1540                                                          temp);
1541                                   avail[bprime->index] = temp;
1542                                 }
1543                             }           
1544                           /* Now build a phi for the new variable.  */
1545                           temp = create_tmp_var (type, "prephitmp");
1546                           add_referenced_tmp_var (temp);
1547                           temp = create_phi_node (temp, block);
1548                           add (value_table, PHI_RESULT (temp), val);
1549
1550 #if 0
1551                           if (!set_contains_value (AVAIL_OUT (block), val))
1552                             insert_into_set (AVAIL_OUT (block), 
1553                                              PHI_RESULT (temp));
1554                           else
1555 #endif
1556                             value_replace_in_set (AVAIL_OUT (block), 
1557                                                  PHI_RESULT (temp));
1558                           for (pred = block->pred;
1559                                pred;
1560                                pred = pred->pred_next)
1561                             {
1562                               add_phi_arg (&temp, avail[pred->src->index],
1563                                            pred);
1564                             }
1565                           if (dump_file && (dump_flags & TDF_DETAILS))
1566                             {
1567                               fprintf (dump_file, "Created phi ");
1568                               print_generic_expr (dump_file, temp, 0);
1569                               fprintf (dump_file, " in block %d\n", block->index);
1570                             }
1571                           pre_stats.phis++;
1572                           new_stuff = true;
1573                           insert_into_set (NEW_SETS (block),
1574                                            PHI_RESULT (temp));
1575                           insert_into_set (PHI_GEN (block),
1576                                            PHI_RESULT (temp));
1577                         }
1578
1579                       free (avail);
1580                     }
1581                 }
1582             }
1583         }
1584     }
1585   for (son = first_dom_son (CDI_DOMINATORS, block);
1586        son;
1587        son = next_dom_son (CDI_DOMINATORS, son))
1588     {
1589       new_stuff |= insert_aux (son);
1590     }
1591
1592   return new_stuff;
1593 }
1594
1595 /* Perform insertion of partially redundant values.  */
1596
1597 static void
1598 insert (void)
1599 {
1600   bool new_stuff = true;
1601   basic_block bb;
1602   int num_iterations = 0;
1603
1604   FOR_ALL_BB (bb)
1605     NEW_SETS (bb) = set_new (true);
1606   
1607   while (new_stuff)
1608     {
1609       num_iterations++;
1610       new_stuff = false;
1611       new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1612     }
1613   if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1614     fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1615 }
1616
1617 /* Return true if EXPR has no defining statement in this procedure,
1618    *AND* isn't a live-on-entry parameter.  */
1619 static bool
1620 is_undefined_value (tree expr)
1621 {  
1622   
1623 #ifdef ENABLE_CHECKING
1624   /* We should never be handed DECL's  */
1625   if (DECL_P (expr))
1626     abort ();
1627 #endif
1628   if (TREE_CODE (expr) == SSA_NAME)
1629     {
1630       /* XXX: Is this the correct test?  */
1631       if (TREE_CODE (SSA_NAME_VAR (expr)) == PARM_DECL)
1632         return false;
1633       if (IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr)))
1634         return true;
1635     }
1636   return false;
1637 }
1638
1639 /* Compute the AVAIL set for BLOCK.
1640    This function performs value numbering of the statements in BLOCK. 
1641    The AVAIL sets are built from information we glean while doing this
1642    value numbering, since the AVAIL sets contain only entry per
1643    value.
1644
1645    
1646    AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
1647    AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U
1648    TMP_GEN[BLOCK].
1649 */
1650
1651 static void
1652 compute_avail (basic_block block)
1653 {
1654   basic_block son;
1655   
1656   /* For arguments with default definitions, we pretend they are
1657      defined in the entry block.  */
1658   if (block == ENTRY_BLOCK_PTR)
1659     {
1660       tree param;
1661       for (param = DECL_ARGUMENTS (current_function_decl);
1662            param;
1663            param = TREE_CHAIN (param))
1664         {
1665           if (default_def (param) != NULL)
1666             {
1667               tree val;
1668               tree def = default_def (param);
1669               val = lookup_or_add (value_table, def);
1670               insert_into_set (TMP_GEN (block), def);
1671               value_insert_into_set (AVAIL_OUT (block), def);
1672             }
1673         }
1674     }
1675   else if (block)
1676     {
1677       block_stmt_iterator bsi;
1678       tree stmt, phi;
1679       basic_block dom;
1680
1681       dom = get_immediate_dominator (CDI_DOMINATORS, block);
1682       if (dom)
1683         set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
1684       for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
1685         {
1686           /* Ignore virtual PHIs until we can do PRE on expressions
1687              with virtual operands.  */
1688           if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
1689             continue;
1690
1691           lookup_or_add (value_table, PHI_RESULT (phi));
1692           value_insert_into_set (AVAIL_OUT (block), PHI_RESULT (phi));
1693           insert_into_set (PHI_GEN (block), PHI_RESULT (phi));
1694         }
1695
1696       for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
1697         {
1698           tree op0, op1;
1699           stmt = bsi_stmt (bsi);
1700           get_stmt_operands (stmt);
1701           
1702           if (NUM_VUSES (STMT_VUSE_OPS (stmt))
1703               || NUM_V_MUST_DEFS (STMT_V_MUST_DEF_OPS (stmt))
1704               || NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt))
1705               || stmt_ann (stmt)->has_volatile_ops)
1706             {
1707               size_t j;
1708               for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1709                 {
1710                   tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1711                   lookup_or_add (value_table, def);
1712                   insert_into_set (TMP_GEN (block), def);
1713                   value_insert_into_set (AVAIL_OUT (block), def);
1714                 }
1715               for (j = 0; j < NUM_USES (STMT_USE_OPS (stmt)); j++)
1716                 {
1717                   tree use = USE_OP (STMT_USE_OPS (stmt), j);
1718                   if (TREE_CODE (use) == SSA_NAME)
1719                     {
1720                       lookup_or_add (value_table, use);
1721                       insert_into_set (TMP_GEN (block), use);
1722                       value_insert_into_set (AVAIL_OUT (block), use);
1723                     }
1724                 }
1725               continue;
1726             }
1727           else if (TREE_CODE (stmt) == RETURN_EXPR
1728                    && TREE_OPERAND (stmt, 0)
1729                    && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
1730             stmt = TREE_OPERAND (stmt, 0);
1731           
1732           if (TREE_CODE (stmt) == MODIFY_EXPR)
1733             {
1734               op0 = TREE_OPERAND (stmt, 0);
1735               if (TREE_CODE (op0) != SSA_NAME)
1736                 continue;
1737               if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
1738                 continue;
1739               op1 = TREE_OPERAND (stmt, 1);
1740               STRIP_USELESS_TYPE_CONVERSION (op1);
1741               if (TREE_CODE_CLASS (TREE_CODE (op1)) == 'c')
1742                 {
1743                   add (value_table, op0, lookup_or_add (value_table, op1));
1744                   insert_into_set (TMP_GEN (block), op0);
1745                   value_insert_into_set (AVAIL_OUT (block), op0);
1746                 }
1747               else if (TREE_CODE_CLASS (TREE_CODE (op1)) == '2')
1748                 {
1749                   tree bop1, bop2;
1750                   tree val, val1, val2;
1751                   tree newt;
1752                   bop1 = TREE_OPERAND (op1, 0);
1753                   bop2 = TREE_OPERAND (op1, 1);
1754                   val1 = lookup_or_add (value_table, bop1);
1755                   val2 = lookup_or_add (value_table, bop2);
1756  
1757                   newt = pool_alloc (binary_node_pool);
1758                   memcpy (newt, op1, tree_size (op1));
1759                   TREE_OPERAND (newt, 0) = val1;
1760                   TREE_OPERAND (newt, 1) = val2;
1761                   val = lookup_or_add (value_table, newt);
1762                   add (value_table, op0, val);
1763                   if (!is_undefined_value (bop1))
1764                     value_insert_into_set (EXP_GEN (block), bop1);
1765                   if (!is_undefined_value (bop2))
1766                     value_insert_into_set (EXP_GEN (block), bop2);
1767                   value_insert_into_set (EXP_GEN (block), newt);
1768                   insert_into_set (TMP_GEN (block), op0);
1769                   value_insert_into_set (AVAIL_OUT (block), op0);  
1770                 }
1771               else if (TREE_CODE_CLASS (TREE_CODE (op1)) == '1'
1772                        && !is_gimple_cast (op1))
1773                 {
1774                   tree uop;
1775                   tree val, val1;
1776                   tree newt;
1777                   uop = TREE_OPERAND (op1, 0);
1778                   val1 = lookup_or_add (value_table, uop);
1779                   newt = pool_alloc (unary_node_pool);
1780                   memcpy (newt, op1, tree_size (op1));
1781                   TREE_OPERAND (newt, 0) = val1;
1782                   val = lookup_or_add (value_table, newt);
1783                   add (value_table, op0, val);
1784                   if (!is_undefined_value (uop))
1785                     value_insert_into_set (EXP_GEN (block), uop);
1786                   value_insert_into_set (EXP_GEN (block), newt);
1787                   insert_into_set (TMP_GEN (block), op0);
1788                   value_insert_into_set (AVAIL_OUT (block), op0);
1789                 }
1790               else if (TREE_CODE (op1) == SSA_NAME)
1791                 {
1792                   tree val = lookup_or_add (value_table, op1);
1793                   add (value_table, op0, val);
1794                   if (!is_undefined_value (op1))
1795                     value_insert_into_set (EXP_GEN (block), op1);
1796                   insert_into_set (TMP_GEN (block), op0);
1797                   value_insert_into_set (AVAIL_OUT (block), op0);
1798                 }
1799               else
1800                 {
1801                   size_t j;
1802                   for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1803                     {
1804                       tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1805                       lookup_or_add (value_table, def);
1806                       insert_into_set (TMP_GEN (block), def);
1807                       value_insert_into_set (AVAIL_OUT (block), def);
1808                       if (def != op0)
1809                         abort ();
1810                     }
1811                   for (j = 0; j < NUM_USES (STMT_USE_OPS (stmt)); j++)
1812                     {
1813                       tree use = USE_OP (STMT_USE_OPS (stmt), j);
1814                       if (TREE_CODE (use) == SSA_NAME)
1815                         {
1816                           lookup_or_add (value_table, use);
1817                           insert_into_set (TMP_GEN (block), use);
1818                           value_insert_into_set (AVAIL_OUT (block), use);
1819                         }
1820                     }
1821                 }
1822             }
1823           else
1824             {
1825               size_t j;
1826               for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1827                 {
1828                   tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1829                   lookup_or_add (value_table, def);
1830                   insert_into_set (TMP_GEN (block), def);
1831                   value_insert_into_set (AVAIL_OUT (block), def);
1832                 }
1833               for (j = 0; j < NUM_USES (STMT_USE_OPS (stmt)); j++)
1834                 {
1835                   tree use = USE_OP (STMT_USE_OPS (stmt), j);
1836                   if (TREE_CODE (use) == SSA_NAME)
1837                     {
1838                       lookup_or_add (value_table, use);
1839                       insert_into_set (TMP_GEN (block), use);
1840                       value_insert_into_set (AVAIL_OUT (block), use);
1841                     }
1842                 }
1843             }
1844         }
1845     }
1846   for (son = first_dom_son (CDI_DOMINATORS, block);
1847        son;
1848        son = next_dom_son (CDI_DOMINATORS, son))
1849     compute_avail (son);
1850
1851 }
1852
1853 /* Eliminate fully redundant computations.  */
1854
1855 static void
1856 eliminate (void)
1857 {
1858   basic_block b;
1859
1860   FOR_EACH_BB (b)
1861     {
1862       block_stmt_iterator i;
1863       
1864       for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
1865         {
1866           tree stmt = bsi_stmt (i);
1867
1868           if (NUM_VUSES (STMT_VUSE_OPS (stmt))
1869               || NUM_V_MUST_DEFS (STMT_V_MUST_DEF_OPS (stmt))
1870               || NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt))
1871               || stmt_ann (stmt)->has_volatile_ops)
1872             continue;
1873           /* Lookup the RHS of the expression, see if we have an
1874              available computation for it. If so, replace the RHS with
1875              the available computation.  */
1876           if (TREE_CODE (stmt) == MODIFY_EXPR)
1877             {
1878               tree t = TREE_OPERAND (stmt, 0);
1879               tree expr = TREE_OPERAND (stmt, 1);
1880               tree sprime;
1881               /* There is no point in eliminating NOP_EXPR, it isn't
1882                  supposed to generate any code.  */
1883               if (TREE_CODE (expr) == NOP_EXPR
1884                   || (TREE_CODE_CLASS (TREE_CODE (expr)) != '2' 
1885                    && TREE_CODE_CLASS (TREE_CODE (expr)) != '1'))
1886                 continue;
1887               sprime = find_leader (AVAIL_OUT (b),
1888                                     lookup (value_table, t));
1889               if (sprime 
1890                   && sprime != t 
1891                   && may_propagate_copy (sprime, TREE_OPERAND (stmt, 1)))
1892                 {
1893                   if (dump_file && (dump_flags & TDF_DETAILS))
1894                     {
1895                       fprintf (dump_file, "Replaced ");
1896                       print_generic_expr (dump_file, expr, 0);
1897                       fprintf (dump_file, " with ");
1898                       print_generic_expr (dump_file, sprime, 0);
1899                       fprintf (dump_file, " in ");
1900                       print_generic_stmt (dump_file, stmt, 0);
1901                     }
1902                   pre_stats.eliminations++;
1903                   propagate_tree_value (&TREE_OPERAND (stmt, 1), sprime);
1904                   modify_stmt (stmt);
1905                 }
1906             }
1907
1908         }
1909     }
1910 }
1911
1912 /* Main entry point to the SSA-PRE pass.
1913
1914    PHASE indicates which dump file from the DUMP_FILES array to use when
1915    dumping debugging information.  */
1916
1917 static void
1918 execute_pre (void)
1919 {
1920   size_t tsize;
1921   basic_block bb;
1922   pre_uid = num_referenced_vars;
1923   memset (&pre_stats, 0, sizeof (pre_stats));
1924   FOR_ALL_BB (bb)
1925     {
1926       bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
1927     }
1928   phi_translate_table = htab_create (511, expr_pred_trans_hash,
1929                                      expr_pred_trans_eq,
1930                                      free);
1931   value_table = htab_create (511, val_expr_pair_hash,
1932                              val_expr_pair_expr_eq, free);
1933   value_set_pool = create_alloc_pool ("Value sets",
1934                                       sizeof (struct value_set), 30);
1935   value_set_node_pool = create_alloc_pool ("Value set nodes",
1936                                        sizeof (struct value_set_node), 30);
1937   calculate_dominance_info (CDI_POST_DOMINATORS);
1938   calculate_dominance_info (CDI_DOMINATORS);
1939   tsize = tree_size (build (PLUS_EXPR, void_type_node, NULL_TREE,
1940                             NULL_TREE));
1941   binary_node_pool = create_alloc_pool ("Binary tree nodes", tsize, 30);
1942   tsize = tree_size (build1 (NEGATE_EXPR, void_type_node, NULL_TREE));
1943   unary_node_pool = create_alloc_pool ("Unary tree nodes", tsize, 30);
1944
1945   FOR_ALL_BB (bb)
1946     {
1947       EXP_GEN (bb) = set_new (true);
1948       PHI_GEN (bb) = set_new (true);
1949       TMP_GEN (bb) = set_new (false);
1950       AVAIL_OUT (bb) = set_new (true);
1951     }
1952
1953   compute_avail (ENTRY_BLOCK_PTR);
1954
1955   if (dump_file && (dump_flags & TDF_DETAILS))
1956     {
1957       FOR_ALL_BB (bb)
1958         {
1959           print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
1960           print_value_set (dump_file, TMP_GEN (bb), "tmp_gen", bb->index);
1961           print_value_set (dump_file, AVAIL_OUT (bb), "avail_out", bb->index);
1962         }
1963     }
1964
1965   /* Insert can get quite slow on an incredibly large number of basic
1966      blocks due to some quadratic behavior.  Until this behavior is
1967      fixed, don't run it when he have an incredibly large number of
1968      bb's.  If we aren't going to run insert, there is no point in
1969      computing ANTIC, either, even though it's plenty fast.  */
1970  
1971   if (n_basic_blocks < 4000)
1972     {
1973       compute_antic ();
1974       
1975       insert ();
1976     }
1977   eliminate ();
1978   
1979   if (dump_file && (dump_flags & TDF_STATS))
1980     {
1981       fprintf (dump_file, "Insertions:%d\n", pre_stats.insertions);
1982       fprintf (dump_file, "New PHIs:%d\n", pre_stats.phis);
1983       fprintf (dump_file, "Eliminated:%d\n", pre_stats.eliminations);
1984     }
1985
1986   free_alloc_pool (value_set_pool);
1987   free_alloc_pool (value_set_node_pool);
1988   free_alloc_pool (binary_node_pool);
1989   free_alloc_pool (unary_node_pool);
1990   htab_delete (value_table);
1991   htab_delete (phi_translate_table);
1992   
1993   FOR_ALL_BB (bb)
1994     {
1995       free (bb->aux);
1996       bb->aux = NULL;
1997     }
1998   free_dominance_info (CDI_POST_DOMINATORS);
1999 }
2000
2001 static bool
2002 gate_pre (void)
2003 {
2004   return flag_tree_pre != 0;
2005 }
2006
2007 struct tree_opt_pass pass_pre =
2008 {
2009   "pre",                                /* name */
2010   gate_pre,                             /* gate */
2011   execute_pre,                          /* execute */
2012   NULL,                                 /* sub */
2013   NULL,                                 /* next */
2014   0,                                    /* static_pass_number */
2015   TV_TREE_PRE,                          /* tv_id */
2016   PROP_no_crit_edges | PROP_cfg | PROP_ssa,/* properties_required */
2017   0,                                    /* properties_provided */
2018   0,                                    /* properties_destroyed */
2019   0,                                    /* todo_flags_start */
2020   TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
2021 };