OSDN Git Service

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