OSDN Git Service

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