OSDN Git Service

* de.po, zh_CN.po: Update.
[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 alloc_pool comparison_node_pool;
309 static alloc_pool expression_node_pool;
310 static alloc_pool list_node_pool;
311 static bitmap_obstack grand_bitmap_obstack;
312
313 /* Set of blocks with statements that have had its EH information
314    cleaned up.  */
315 static bitmap need_eh_cleanup;
316
317 /* The phi_translate_table caches phi translations for a given
318    expression and predecessor.  */
319
320 static htab_t phi_translate_table;
321
322 /* A three tuple {e, pred, v} used to cache phi translations in the
323    phi_translate_table.  */
324
325 typedef struct expr_pred_trans_d
326 {
327   /* The expression.  */
328   tree e;
329
330   /* The predecessor block along which we translated the expression.  */
331   basic_block pred;
332
333   /* The value that resulted from the translation.  */
334   tree v;
335
336   /* The hashcode for the expression, pred pair. This is cached for
337      speed reasons.  */
338   hashval_t hashcode;
339 } *expr_pred_trans_t;
340
341 /* Return the hash value for a phi translation table entry.  */
342
343 static hashval_t
344 expr_pred_trans_hash (const void *p)
345 {
346   const expr_pred_trans_t ve = (expr_pred_trans_t) p;
347   return ve->hashcode;
348 }
349
350 /* Return true if two phi translation table entries are the same.
351    P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
352
353 static int
354 expr_pred_trans_eq (const void *p1, const void *p2)
355 {
356   const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
357   const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
358   basic_block b1 = ve1->pred;
359   basic_block b2 = ve2->pred;
360
361   
362   /* If they are not translations for the same basic block, they can't
363      be equal.  */
364   if (b1 != b2)
365     return false;
366
367   /* If they are for the same basic block, determine if the
368      expressions are equal.  */  
369   if (expressions_equal_p (ve1->e, ve2->e))
370     return true;
371   
372   return false;
373 }
374
375 /* Search in the phi translation table for the translation of
376    expression E in basic block PRED. Return the translated value, if
377    found, NULL otherwise.  */ 
378
379 static inline tree
380 phi_trans_lookup (tree e, basic_block pred)
381 {
382   void **slot;
383   struct expr_pred_trans_d ept;
384   ept.e = e;
385   ept.pred = pred;
386   ept.hashcode = vn_compute (e, (unsigned long) pred, NULL);
387   slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
388                                    NO_INSERT);
389   if (!slot)
390     return NULL;
391   else
392     return ((expr_pred_trans_t) *slot)->v;
393 }
394
395
396 /* Add the tuple mapping from {expression E, basic block PRED} to
397    value V, to the phi translation table.  */
398
399 static inline void
400 phi_trans_add (tree e, tree v, basic_block pred)
401 {
402   void **slot;
403   expr_pred_trans_t new_pair = xmalloc (sizeof (*new_pair));
404   new_pair->e = e;
405   new_pair->pred = pred;
406   new_pair->v = v;
407   new_pair->hashcode = vn_compute (e, (unsigned long) pred, NULL);
408   slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
409                                    new_pair->hashcode, INSERT);
410   if (*slot)
411     free (*slot);
412   *slot = (void *) new_pair;
413 }
414
415
416 /* Add expression E to the expression set of value V.  */
417
418 void
419 add_to_value (tree v, tree e)
420 {
421   /* Constants have no expression sets.  */
422   if (is_gimple_min_invariant (v))
423     return;
424
425   if (VALUE_HANDLE_EXPR_SET (v) == NULL)
426     VALUE_HANDLE_EXPR_SET (v) = set_new (false);
427
428   insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
429 }
430
431
432 /* Return true if value V exists in the bitmap for SET.  */
433
434 static inline bool
435 value_exists_in_set_bitmap (value_set_t set, tree v)
436 {
437   if (!set->values)
438     return false;
439
440   return bitmap_bit_p (set->values, VALUE_HANDLE_ID (v));
441 }
442
443
444 /* Remove value V from the bitmap for SET.  */
445
446 static void
447 value_remove_from_set_bitmap (value_set_t set, tree v)
448 {
449   gcc_assert (set->indexed);
450
451   if (!set->values)
452     return;
453
454   bitmap_clear_bit (set->values, VALUE_HANDLE_ID (v));
455 }
456
457
458 /* Insert the value number V into the bitmap of values existing in
459    SET.  */
460
461 static inline void
462 value_insert_into_set_bitmap (value_set_t set, tree v)
463 {
464   gcc_assert (set->indexed);
465
466   if (set->values == NULL)
467     set->values = BITMAP_ALLOC (&grand_bitmap_obstack);
468
469   bitmap_set_bit (set->values, VALUE_HANDLE_ID (v));
470 }
471
472
473 /* Create a new bitmap set and return it.  */
474
475 static bitmap_set_t 
476 bitmap_set_new (void)
477 {
478   bitmap_set_t ret = pool_alloc (bitmap_set_pool);
479   ret->expressions = BITMAP_ALLOC (&grand_bitmap_obstack);
480   ret->values = BITMAP_ALLOC (&grand_bitmap_obstack);
481   return ret;
482 }
483
484 /* Create a new set.  */
485
486 static value_set_t
487 set_new  (bool indexed)
488 {
489   value_set_t ret;
490   ret = pool_alloc (value_set_pool);
491   ret->head = ret->tail = NULL;
492   ret->length = 0;
493   ret->indexed = indexed;
494   ret->values = NULL;
495   return ret;
496 }
497
498 /* Insert an expression EXPR into a bitmapped set.  */
499
500 static void
501 bitmap_insert_into_set (bitmap_set_t set, tree expr)
502 {
503   tree val;
504   /* XXX: For now, we only let SSA_NAMES into the bitmap sets.  */
505   gcc_assert (TREE_CODE (expr) == SSA_NAME);
506   val = get_value_handle (expr);
507   
508   gcc_assert (val);
509   if (!is_gimple_min_invariant (val))
510   {
511     bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
512     bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
513   }
514 }
515
516 /* Insert EXPR into SET.  */
517
518 static void
519 insert_into_set (value_set_t set, tree expr)
520 {
521   value_set_node_t newnode = pool_alloc (value_set_node_pool);
522   tree val = get_value_handle (expr);
523   gcc_assert (val);
524   
525   if (is_gimple_min_invariant (val))
526     return;
527
528   /* For indexed sets, insert the value into the set value bitmap.
529      For all sets, add it to the linked list and increment the list
530      length.  */
531   if (set->indexed)
532     value_insert_into_set_bitmap (set, val);
533
534   newnode->next = NULL;
535   newnode->expr = expr;
536   set->length ++;
537   if (set->head == NULL)
538     {
539       set->head = set->tail = newnode;
540     }
541   else
542     {
543       set->tail->next = newnode;
544       set->tail = newnode;
545     }
546 }
547
548 /* Copy a bitmapped set ORIG, into bitmapped set DEST.  */
549
550 static void
551 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
552 {
553   bitmap_copy (dest->expressions, orig->expressions);
554   bitmap_copy (dest->values, orig->values);
555 }
556
557 /* Copy the set ORIG to the set DEST.  */
558
559 static void
560 set_copy (value_set_t dest, value_set_t orig)
561 {
562   value_set_node_t node;
563  
564   if (!orig || !orig->head)
565     return;
566
567   for (node = orig->head;
568        node;
569        node = node->next)
570     {
571       insert_into_set (dest, node->expr);
572     }
573 }
574
575 /* Remove EXPR from SET.  */
576
577 static void
578 set_remove (value_set_t set, tree expr)
579 {
580   value_set_node_t node, prev;
581
582   /* Remove the value of EXPR from the bitmap, decrement the set
583      length, and remove it from the actual double linked list.  */ 
584   value_remove_from_set_bitmap (set, get_value_handle (expr));
585   set->length--;
586   prev = NULL;
587   for (node = set->head; 
588        node != NULL; 
589        prev = node, node = node->next)
590     {
591       if (node->expr == expr)
592         {
593           if (prev == NULL)
594             set->head = node->next;
595           else
596             prev->next= node->next;
597  
598           if (node == set->tail)
599             set->tail = prev;
600           pool_free (value_set_node_pool, node);
601           return;
602         }
603     }
604 }
605
606 /* Return true if SET contains the value VAL.  */
607
608 static bool
609 set_contains_value (value_set_t set, tree val)
610 {
611   /* All constants are in every set.  */
612   if (is_gimple_min_invariant (val))
613     return true;
614   
615   if (set->length == 0)
616     return false;
617   
618   return value_exists_in_set_bitmap (set, val);
619 }
620
621 /* Return true if bitmapped set SET contains the expression EXPR.  */
622 static bool
623 bitmap_set_contains (bitmap_set_t set, tree expr)
624 {
625   /* All constants are in every set.  */
626   if (is_gimple_min_invariant (get_value_handle (expr)))
627     return true;
628
629   /* XXX: Bitmapped sets only contain SSA_NAME's for now.  */
630   if (TREE_CODE (expr) != SSA_NAME)
631     return false;
632   return bitmap_bit_p (set->expressions, SSA_NAME_VERSION (expr));
633 }
634
635   
636 /* Return true if bitmapped set SET contains the value VAL.  */
637
638 static bool
639 bitmap_set_contains_value (bitmap_set_t set, tree val)
640 {
641   if (is_gimple_min_invariant (val))
642     return true;
643   return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
644 }
645
646 /* Replace an instance of value LOOKFOR with expression EXPR in SET.  */
647
648 static void
649 bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
650 {
651   value_set_t exprset;
652   value_set_node_t node;
653   if (is_gimple_min_invariant (lookfor))
654     return;
655   if (!bitmap_set_contains_value (set, lookfor))
656     return;
657
658   /* The number of expressions having a given value is usually
659      significantly less than the total number of expressions in SET.
660      Thus, rather than check, for each expression in SET, whether it
661      has the value LOOKFOR, we walk the reverse mapping that tells us
662      what expressions have a given value, and see if any of those
663      expressions are in our set.  For large testcases, this is about
664      5-10x faster than walking the bitmap.  If this is somehow a
665      significant lose for some cases, we can choose which set to walk
666      based on the set size.  */
667   exprset = VALUE_HANDLE_EXPR_SET (lookfor);
668   for (node = exprset->head; node; node = node->next)
669     {
670       if (TREE_CODE (node->expr) == SSA_NAME)
671         {
672           if (bitmap_bit_p (set->expressions, SSA_NAME_VERSION (node->expr)))
673             {
674               bitmap_clear_bit (set->expressions, SSA_NAME_VERSION (node->expr));
675               bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
676               return;
677             }
678         }
679     }
680 }
681
682 /* Subtract bitmapped set B from value set A, and return the new set.  */
683
684 static value_set_t
685 bitmap_set_subtract_from_value_set (value_set_t a, bitmap_set_t b,
686                                     bool indexed)
687 {
688   value_set_t ret = set_new (indexed);
689   value_set_node_t node;
690   for (node = a->head;
691        node;
692        node = node->next)
693     {
694       if (!bitmap_set_contains (b, node->expr))
695         insert_into_set (ret, node->expr);
696     }
697   return ret;
698 }
699
700 /* Return true if two sets are equal.  */
701
702 static bool
703 set_equal (value_set_t a, value_set_t b)
704 {
705   value_set_node_t node;
706
707   if (a->length != b->length)
708     return false;
709   for (node = a->head;
710        node;
711        node = node->next)
712     {
713       if (!set_contains_value (b, get_value_handle (node->expr)))
714         return false;
715     }
716   return true;
717 }
718
719 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
720    and add it otherwise.  */
721
722 static void
723 bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
724 {
725   tree val = get_value_handle (expr);
726   if (bitmap_set_contains_value (set, val))
727     bitmap_set_replace_value (set, val, expr);
728   else
729     bitmap_insert_into_set (set, expr);
730 }
731
732 /* Insert EXPR into SET if EXPR's value is not already present in
733    SET.  */
734
735 static void
736 bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
737 {
738   tree val = get_value_handle (expr);
739
740   if (is_gimple_min_invariant (val))
741     return;
742   
743   if (!bitmap_set_contains_value (set, val))
744     bitmap_insert_into_set (set, expr);
745 }
746
747 /* Insert the value for EXPR into SET, if it doesn't exist already.  */
748
749 static void
750 value_insert_into_set (value_set_t set, tree expr)
751 {
752   tree val = get_value_handle (expr);
753
754   /* Constant and invariant values exist everywhere, and thus,
755      actually keeping them in the sets is pointless.  */
756   if (is_gimple_min_invariant (val))
757     return;
758
759   if (!set_contains_value (set, val))
760     insert_into_set (set, expr);
761 }
762
763
764 /* Print out SET to OUTFILE.  */
765
766 static void
767 bitmap_print_value_set (FILE *outfile, bitmap_set_t set,
768                         const char *setname, int blockindex)
769 {
770   fprintf (outfile, "%s[%d] := { ", setname, blockindex);
771   if (set)
772     {
773       bool first = true;
774       unsigned i;
775       bitmap_iterator bi;
776
777       EXECUTE_IF_SET_IN_BITMAP (set->expressions, 0, i, bi)
778         {
779           if (!first)
780             fprintf (outfile, ", ");
781           first = false;
782           print_generic_expr (outfile, ssa_name (i), 0);
783         
784           fprintf (outfile, " (");
785           print_generic_expr (outfile, get_value_handle (ssa_name (i)), 0);
786           fprintf (outfile, ") ");
787         }
788     }
789   fprintf (outfile, " }\n");
790 }
791 /* Print out the value_set SET to OUTFILE.  */
792
793 static void
794 print_value_set (FILE *outfile, value_set_t set,
795                  const char *setname, int blockindex)
796 {
797   value_set_node_t node;
798   fprintf (outfile, "%s[%d] := { ", setname, blockindex);
799   if (set)
800     {
801       for (node = set->head;
802            node;
803            node = node->next)
804         {
805           print_generic_expr (outfile, node->expr, 0);
806           
807           fprintf (outfile, " (");
808           print_generic_expr (outfile, get_value_handle (node->expr), 0);
809           fprintf (outfile, ") ");
810                      
811           if (node->next)
812             fprintf (outfile, ", ");
813         }
814     }
815
816   fprintf (outfile, " }\n");
817 }
818
819 /* Print out the expressions that have VAL to OUTFILE.  */
820
821 void
822 print_value_expressions (FILE *outfile, tree val)
823 {
824   if (VALUE_HANDLE_EXPR_SET (val))
825     {
826       char s[10];
827       sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
828       print_value_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
829     }
830 }
831
832
833 void
834 debug_value_expressions (tree val)
835 {
836   print_value_expressions (stderr, val);
837 }
838
839   
840 void debug_value_set (value_set_t, const char *, int);
841
842 void
843 debug_value_set (value_set_t set, const char *setname, int blockindex)
844 {
845   print_value_set (stderr, set, setname, blockindex);
846 }
847
848 /* Return the folded version of T if T, when folded, is a gimple
849    min_invariant.  Otherwise, return T.  */ 
850
851 static tree
852 fully_constant_expression (tree t)
853 {  
854   tree folded;
855   folded = fold (t);
856   if (folded && is_gimple_min_invariant (folded))
857     return folded;
858   return t;
859 }
860
861 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
862    For example, this can copy a list made of TREE_LIST nodes.  
863    Allocates the nodes in list_node_pool*/
864
865 static tree
866 pool_copy_list (tree list)
867 {
868   tree head;
869   tree prev, next;
870
871   if (list == 0)
872     return 0;
873   head = pool_alloc (list_node_pool);
874   
875   memcpy (head, list, tree_size (list));
876   prev = head;
877   
878   next = TREE_CHAIN (list);
879   while (next)
880     {
881       TREE_CHAIN (prev) = pool_alloc (list_node_pool);
882       memcpy (TREE_CHAIN (prev), next, tree_size (next));
883       prev = TREE_CHAIN (prev);
884       next = TREE_CHAIN (next);
885     }
886   return head;
887 }
888
889
890 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
891    the phis in PRED.  Return NULL if we can't find a leader for each
892    part of the translated expression.  */
893
894 static tree
895 phi_translate (tree expr, value_set_t set, basic_block pred,
896                basic_block phiblock)
897 {
898   tree phitrans = NULL;
899   tree oldexpr = expr;
900   
901   if (expr == NULL)
902     return NULL;
903
904   if (is_gimple_min_invariant (expr))
905     return expr;
906
907   /* Phi translations of a given expression don't change.  */
908   phitrans = phi_trans_lookup (expr, pred);
909   if (phitrans)
910     return phitrans;
911   
912   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
913     {
914     case tcc_expression:
915       {
916         if (TREE_CODE (expr) != CALL_EXPR)
917           return NULL;
918         else
919           {
920             tree oldop0 = TREE_OPERAND (expr, 0);
921             tree oldarglist = TREE_OPERAND (expr, 1);
922             tree oldop2 = TREE_OPERAND (expr, 2);
923             tree newop0;
924             tree newarglist;
925             tree newop2 = NULL;
926             tree oldwalker;
927             tree newwalker;
928             tree newexpr;
929             bool listchanged = false;
930
931             /* Call expressions are kind of weird because they have an
932                argument list.  We don't want to value number the list
933                as one value number, because that doesn't make much
934                sense, and just breaks the support functions we call,
935                which expect TREE_OPERAND (call_expr, 2) to be a
936                TREE_LIST. */          
937             
938             newop0 = phi_translate (find_leader (set, oldop0),
939                                     set, pred, phiblock);
940             if (newop0 == NULL)
941               return NULL;
942             if (oldop2)
943               {
944                 newop2 = phi_translate (find_leader (set, oldop2),
945                                         set, pred, phiblock);
946                 if (newop2 == NULL)
947                   return NULL;
948               }
949
950             /* phi translate the argument list piece by piece.
951                
952               We could actually build the list piece by piece here,
953               but it's likely to not be worth the memory we will save,
954               unless you have millions of call arguments.  */
955
956             newarglist = pool_copy_list (oldarglist);
957             for (oldwalker = oldarglist, newwalker = newarglist;
958                  oldwalker && newwalker;
959                  oldwalker = TREE_CHAIN (oldwalker), 
960                    newwalker = TREE_CHAIN (newwalker))
961               {
962                 
963                 tree oldval = TREE_VALUE (oldwalker);
964                 tree newval;
965                 if (oldval)
966                   {
967                     newval = phi_translate (find_leader (set, oldval),
968                                             set, pred, phiblock);
969                     if (newval == NULL)
970                       return NULL;
971                     if (newval != oldval)
972                       {
973                         listchanged = true;
974                         TREE_VALUE (newwalker) = get_value_handle (newval);
975                       }
976                   }
977               }
978             if (listchanged)
979               vn_lookup_or_add (newarglist, NULL);
980             
981             if (listchanged || (newop0 != oldop0) || (oldop2 != newop2))
982               {
983                 newexpr = pool_alloc (expression_node_pool);
984                 memcpy (newexpr, expr, tree_size (expr));
985                 TREE_OPERAND (newexpr, 0) = newop0 == oldop0 ? oldop0 : get_value_handle (newop0);
986                 TREE_OPERAND (newexpr, 1) = listchanged ? newarglist : oldarglist;
987                 TREE_OPERAND (newexpr, 2) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
988                 create_tree_ann (newexpr);       
989                 vn_lookup_or_add (newexpr, NULL);
990                 expr = newexpr;
991                 phi_trans_add (oldexpr, newexpr, pred);
992               }
993           }
994       }
995       return expr;
996
997     case tcc_reference:
998       /* XXX: Until we have PRE of loads working, none will be ANTIC.  */
999       return NULL;
1000
1001     case tcc_binary:
1002     case tcc_comparison:
1003       {
1004         tree oldop1 = TREE_OPERAND (expr, 0);
1005         tree oldop2 = TREE_OPERAND (expr, 1);
1006         tree newop1;
1007         tree newop2;
1008         tree newexpr;
1009         
1010         newop1 = phi_translate (find_leader (set, oldop1),
1011                                 set, pred, phiblock);
1012         if (newop1 == NULL)
1013           return NULL;
1014         newop2 = phi_translate (find_leader (set, oldop2),
1015                                 set, pred, phiblock);
1016         if (newop2 == NULL)
1017           return NULL;
1018         if (newop1 != oldop1 || newop2 != oldop2)
1019           {
1020             tree t;
1021             newexpr = pool_alloc (binary_node_pool);
1022             memcpy (newexpr, expr, tree_size (expr));
1023             TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
1024             TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
1025             t = fully_constant_expression (newexpr);
1026             if (t != newexpr)
1027               {
1028                 pool_free (binary_node_pool, newexpr);
1029                 newexpr = t;
1030               }
1031             else
1032               {
1033                 create_tree_ann (newexpr);       
1034                 vn_lookup_or_add (newexpr, NULL);
1035               }
1036             expr = newexpr;
1037             phi_trans_add (oldexpr, newexpr, pred);         
1038           }
1039       }
1040       return expr;
1041
1042     case tcc_unary:
1043       {
1044         tree oldop1 = TREE_OPERAND (expr, 0);
1045         tree newop1;
1046         tree newexpr;
1047
1048         newop1 = phi_translate (find_leader (set, oldop1),
1049                                 set, pred, phiblock);
1050         if (newop1 == NULL)
1051           return NULL;
1052         if (newop1 != oldop1)
1053           {
1054             tree t;
1055             newexpr = pool_alloc (unary_node_pool);
1056             memcpy (newexpr, expr, tree_size (expr));
1057             TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
1058             t = fully_constant_expression (newexpr);
1059             if (t != newexpr)
1060               {
1061                 pool_free (unary_node_pool, newexpr);
1062                 newexpr = t;
1063               }
1064             else
1065               {
1066                 create_tree_ann (newexpr);       
1067                 vn_lookup_or_add (newexpr, NULL);
1068               }
1069             expr = newexpr;
1070             phi_trans_add (oldexpr, newexpr, pred);
1071           }
1072       }
1073       return expr;
1074
1075     case tcc_exceptional:
1076       {
1077         tree phi = NULL;
1078         edge e;
1079         gcc_assert (TREE_CODE (expr) == SSA_NAME);
1080         if (TREE_CODE (SSA_NAME_DEF_STMT (expr)) == PHI_NODE)
1081           phi = SSA_NAME_DEF_STMT (expr);
1082         else
1083           return expr;
1084         
1085         e = find_edge (pred, bb_for_stmt (phi));
1086         if (e)
1087           {
1088             if (is_undefined_value (PHI_ARG_DEF (phi, e->dest_idx)))
1089               return NULL;
1090             vn_lookup_or_add (PHI_ARG_DEF (phi, e->dest_idx), NULL);
1091             return PHI_ARG_DEF (phi, e->dest_idx);
1092           }
1093       }
1094       return expr;
1095
1096     default:
1097       gcc_unreachable ();
1098     }
1099 }
1100
1101 /* For each expression in SET, translate the value handles through phi nodes
1102    in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1103    expressions in DEST.  */
1104
1105 static void
1106 phi_translate_set (value_set_t dest, value_set_t set, basic_block pred,
1107                    basic_block phiblock)
1108 {
1109   value_set_node_t node;
1110   for (node = set->head;
1111        node;
1112        node = node->next)
1113     {
1114       tree translated;
1115       translated = phi_translate (node->expr, set, pred, phiblock);
1116       phi_trans_add (node->expr, translated, pred);
1117       
1118       if (translated != NULL)
1119         value_insert_into_set (dest, translated);
1120     } 
1121 }
1122
1123 /* Find the leader for a value (i.e., the name representing that
1124    value) in a given set, and return it.  Return NULL if no leader is
1125    found.  */
1126
1127 static tree
1128 bitmap_find_leader (bitmap_set_t set, tree val)
1129 {
1130   if (val == NULL)
1131     return NULL;
1132   
1133   if (is_gimple_min_invariant (val))
1134     return val;
1135   if (bitmap_set_contains_value (set, val))
1136     {
1137       /* Rather than walk the entire bitmap of expressions, and see
1138          whether any of them has the value we are looking for, we look
1139          at the reverse mapping, which tells us the set of expressions
1140          that have a given value (IE value->expressions with that
1141          value) and see if any of those expressions are in our set.
1142          The number of expressions per value is usually significantly
1143          less than the number of expressions in the set.  In fact, for
1144          large testcases, doing it this way is roughly 5-10x faster
1145          than walking the bitmap.
1146          If this is somehow a significant lose for some cases, we can
1147          choose which set to walk based on which set is smaller.  */     
1148       value_set_t exprset;
1149       value_set_node_t node;
1150       exprset = VALUE_HANDLE_EXPR_SET (val);
1151       for (node = exprset->head; node; node = node->next)
1152         {
1153           if (TREE_CODE (node->expr) == SSA_NAME)
1154             {
1155               if (bitmap_bit_p (set->expressions, 
1156                                 SSA_NAME_VERSION (node->expr)))
1157                 return node->expr;
1158             }
1159         }
1160     }
1161   return NULL;
1162 }
1163
1164         
1165 /* Find the leader for a value (i.e., the name representing that
1166    value) in a given set, and return it.  Return NULL if no leader is
1167    found.  */
1168
1169 static tree
1170 find_leader (value_set_t set, tree val)
1171 {
1172   value_set_node_t node;
1173
1174   if (val == NULL)
1175     return NULL;
1176
1177   /* Constants represent themselves.  */
1178   if (is_gimple_min_invariant (val))
1179     return val;
1180
1181   if (set->length == 0)
1182     return NULL;
1183   
1184   if (value_exists_in_set_bitmap (set, val))
1185     {
1186       for (node = set->head;
1187            node;
1188            node = node->next)
1189         {
1190           if (get_value_handle (node->expr) == val)
1191             return node->expr;
1192         }
1193     }
1194
1195   return NULL;
1196 }
1197
1198 /* Determine if the expression EXPR is valid in SET.  This means that
1199    we have a leader for each part of the expression (if it consists of
1200    values), or the expression is an SSA_NAME.  
1201
1202    NB: We never should run into a case where we have SSA_NAME +
1203    SSA_NAME or SSA_NAME + value.  The sets valid_in_set is called on,
1204    the ANTIC sets, will only ever have SSA_NAME's or value expressions
1205    (IE VALUE1 + VALUE2, *VALUE1, VALUE1 < VALUE2)  */
1206
1207 static bool
1208 valid_in_set (value_set_t set, tree expr)
1209 {
1210   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1211     {
1212     case tcc_binary:
1213     case tcc_comparison:
1214       {
1215         tree op1 = TREE_OPERAND (expr, 0);
1216         tree op2 = TREE_OPERAND (expr, 1);
1217         return set_contains_value (set, op1) && set_contains_value (set, op2);
1218       }
1219
1220     case tcc_unary:
1221       {
1222         tree op1 = TREE_OPERAND (expr, 0);
1223         return set_contains_value (set, op1);
1224       }
1225       
1226     case tcc_expression:
1227       {
1228         if (TREE_CODE (expr) == CALL_EXPR)
1229           {
1230             tree op0 = TREE_OPERAND (expr, 0);
1231             tree arglist = TREE_OPERAND (expr, 1);
1232             tree op2 = TREE_OPERAND (expr, 2);
1233
1234             /* Check the non-list operands first.  */
1235             if (!set_contains_value (set, op0)
1236                 || (op2 && !set_contains_value (set, op2)))
1237               return false;
1238
1239             /* Now check the operands.  */
1240             for (; arglist; arglist = TREE_CHAIN (arglist))
1241               {
1242                 if (!set_contains_value (set, TREE_VALUE (arglist)))
1243                   return false;
1244               }
1245             return true;
1246           }
1247         return false;
1248       }
1249       
1250     case tcc_reference:
1251       /* XXX: Until PRE of loads works, no reference nodes are ANTIC.  */
1252       return false;
1253
1254     case tcc_exceptional:
1255       gcc_assert (TREE_CODE (expr) == SSA_NAME);
1256       return true;
1257
1258     case tcc_declaration:
1259       /* VAR_DECL and PARM_DECL are never anticipatable.  */
1260       return false;
1261
1262     default:
1263       /* No other cases should be encountered.  */
1264       gcc_unreachable (); 
1265    }
1266 }
1267
1268 /* Clean the set of expressions that are no longer valid in SET.  This
1269    means expressions that are made up of values we have no leaders for
1270    in SET.  */
1271
1272 static void
1273 clean (value_set_t set)
1274 {
1275   value_set_node_t node;
1276   value_set_node_t next;
1277   node = set->head;
1278   while (node)
1279     {
1280       next = node->next;
1281       if (!valid_in_set (set, node->expr))      
1282         set_remove (set, node->expr);
1283       node = next;
1284     }
1285 }
1286
1287 DEF_VEC_P (basic_block);
1288 DEF_VEC_ALLOC_P (basic_block, heap);
1289 static sbitmap has_abnormal_preds;
1290
1291 /* Compute the ANTIC set for BLOCK.
1292
1293    If succs(BLOCK) > 1 then
1294      ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
1295    else if succs(BLOCK) == 1 then
1296      ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
1297
1298    ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
1299
1300    XXX: It would be nice to either write a set_clear, and use it for
1301    ANTIC_OUT, or to mark the antic_out set as deleted at the end
1302    of this routine, so that the pool can hand the same memory back out
1303    again for the next ANTIC_OUT.  */
1304
1305 static bool
1306 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
1307 {
1308   basic_block son;
1309   bool changed = false;
1310   value_set_t S, old, ANTIC_OUT;
1311   value_set_node_t node;
1312
1313   ANTIC_OUT = S = NULL;
1314
1315   /* If any edges from predecessors are abnormal, antic_in is empty,
1316      so do nothing.  */
1317   if (block_has_abnormal_pred_edge)
1318     goto maybe_dump_sets;
1319
1320   old = set_new (false);
1321   set_copy (old, ANTIC_IN (block));
1322   ANTIC_OUT = set_new (true);
1323
1324   /* If the block has no successors, ANTIC_OUT is empty.  */
1325   if (EDGE_COUNT (block->succs) == 0)
1326     ;
1327   /* If we have one successor, we could have some phi nodes to
1328      translate through.  */
1329   else if (single_succ_p (block))
1330     {
1331       phi_translate_set (ANTIC_OUT, ANTIC_IN (single_succ (block)),
1332                          block, single_succ (block));
1333     }
1334   /* If we have multiple successors, we take the intersection of all of
1335      them.  */
1336   else
1337     {
1338       VEC(basic_block, heap) * worklist;
1339       edge e;
1340       size_t i;
1341       basic_block bprime, first;
1342       edge_iterator ei;
1343
1344       worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
1345       FOR_EACH_EDGE (e, ei, block->succs)
1346         VEC_quick_push (basic_block, worklist, e->dest);
1347       first = VEC_index (basic_block, worklist, 0);
1348       set_copy (ANTIC_OUT, ANTIC_IN (first));
1349
1350       for (i = 1; VEC_iterate (basic_block, worklist, i, bprime); i++)
1351         {
1352           node = ANTIC_OUT->head;
1353           while (node)
1354             {
1355               tree val;
1356               value_set_node_t next = node->next;
1357               val = get_value_handle (node->expr);
1358               if (!set_contains_value (ANTIC_IN (bprime), val))
1359                 set_remove (ANTIC_OUT, node->expr);
1360               node = next;
1361             }
1362         }
1363       VEC_free (basic_block, heap, worklist);
1364     }
1365
1366   /* Generate ANTIC_OUT - TMP_GEN.  */
1367   S = bitmap_set_subtract_from_value_set (ANTIC_OUT, TMP_GEN (block), false);
1368
1369   /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1370   ANTIC_IN (block) = bitmap_set_subtract_from_value_set (EXP_GEN (block), 
1371                                                          TMP_GEN (block),
1372                                                          true);
1373
1374   /* Then union in the ANTIC_OUT - TMP_GEN values,
1375      to get ANTIC_OUT U EXP_GEN - TMP_GEN */
1376   for (node = S->head; node; node = node->next)
1377     value_insert_into_set (ANTIC_IN (block), node->expr);
1378
1379   clean (ANTIC_IN (block));
1380   if (!set_equal (old, ANTIC_IN (block)))
1381     changed = true;
1382
1383  maybe_dump_sets:
1384   if (dump_file && (dump_flags & TDF_DETAILS))
1385     {
1386       if (ANTIC_OUT)
1387         print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1388       print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1389       if (S)
1390         print_value_set (dump_file, S, "S", block->index);
1391     }
1392
1393   for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1394        son;
1395        son = next_dom_son (CDI_POST_DOMINATORS, son))
1396     {
1397       changed |= compute_antic_aux (son,
1398                                     TEST_BIT (has_abnormal_preds, son->index));
1399     }
1400   return changed;
1401 }
1402
1403 /* Compute ANTIC sets.  */
1404
1405 static void
1406 compute_antic (void)
1407 {
1408   bool changed = true;
1409   int num_iterations = 0;
1410   basic_block block;
1411
1412   /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
1413      We pre-build the map of blocks with incoming abnormal edges here.  */
1414   has_abnormal_preds = sbitmap_alloc (last_basic_block);
1415   sbitmap_zero (has_abnormal_preds);
1416   FOR_EACH_BB (block)
1417     {
1418       edge_iterator ei;
1419       edge e;
1420
1421       FOR_EACH_EDGE (e, ei, block->preds)
1422         if (e->flags & EDGE_ABNORMAL)
1423           {
1424             SET_BIT (has_abnormal_preds, block->index);
1425             break;
1426           }
1427
1428       /* While we are here, give empty ANTIC_IN sets to each block.  */
1429       ANTIC_IN (block) = set_new (true);
1430     }
1431   /* At the exit block we anticipate nothing.  */
1432   ANTIC_IN (EXIT_BLOCK_PTR) = set_new (true);
1433
1434   while (changed)
1435     {
1436       num_iterations++;
1437       changed = false;
1438       changed = compute_antic_aux (EXIT_BLOCK_PTR, false);
1439     }
1440
1441   sbitmap_free (has_abnormal_preds);
1442
1443   if (dump_file && (dump_flags & TDF_STATS))
1444     fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1445 }
1446
1447 static VEC(tree,heap) *inserted_exprs;
1448 /* Find a leader for an expression, or generate one using
1449    create_expression_by_pieces if it's ANTIC but
1450    complex.  
1451    BLOCK is the basic_block we are looking for leaders in.
1452    EXPR is the expression to find a leader or generate for. 
1453    STMTS is the statement list to put the inserted expressions on.
1454    Returns the SSA_NAME of the LHS of the generated expression or the
1455    leader.  */
1456
1457 static tree
1458 find_or_generate_expression (basic_block block, tree expr, tree stmts)
1459 {
1460   tree genop = bitmap_find_leader (AVAIL_OUT (block), expr);
1461
1462   /* If it's still NULL, it must be a complex expression, so generate
1463      it recursively.  */
1464   if (genop == NULL)
1465     {
1466       genop = VALUE_HANDLE_EXPR_SET (expr)->head->expr;
1467       gcc_assert (UNARY_CLASS_P (genop)
1468                   || BINARY_CLASS_P (genop)
1469                   || COMPARISON_CLASS_P (genop)
1470                   || REFERENCE_CLASS_P (genop)
1471                   || TREE_CODE (genop) == CALL_EXPR);
1472       genop = create_expression_by_pieces (block, genop, stmts);
1473     }
1474   return genop;
1475 }
1476
1477 #define NECESSARY(stmt)         stmt->common.asm_written_flag  
1478 /* Create an expression in pieces, so that we can handle very complex
1479    expressions that may be ANTIC, but not necessary GIMPLE.  
1480    BLOCK is the basic block the expression will be inserted into,
1481    EXPR is the expression to insert (in value form)
1482    STMTS is a statement list to append the necessary insertions into.
1483
1484    This function will die if we hit some value that shouldn't be
1485    ANTIC but is (IE there is no leader for it, or its components).
1486    This function may also generate expressions that are themselves
1487    partially or fully redundant.  Those that are will be either made
1488    fully redundant during the next iteration of insert (for partially
1489    redundant ones), or eliminated by eliminate (for fully redundant
1490    ones).  */
1491
1492 static tree
1493 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
1494 {
1495   tree temp, name;
1496   tree folded, forced_stmts, newexpr;
1497   tree v;
1498   tree_stmt_iterator tsi;
1499
1500   switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1501     {
1502     case tcc_expression:
1503       {
1504         tree op0, op2;
1505         tree arglist;
1506         tree genop0, genop2;
1507         tree genarglist;
1508         tree walker, genwalker;
1509         
1510         gcc_assert (TREE_CODE (expr) == CALL_EXPR);
1511         genop2 = NULL;
1512         
1513         op0 = TREE_OPERAND (expr, 0);
1514         arglist = TREE_OPERAND (expr, 1);
1515         op2 = TREE_OPERAND (expr, 2);
1516         
1517         genop0 = find_or_generate_expression (block, op0, stmts);
1518         genarglist = copy_list (arglist);
1519         for (walker = arglist, genwalker = genarglist;
1520              genwalker && walker;
1521              genwalker = TREE_CHAIN (genwalker), walker = TREE_CHAIN (walker))
1522           {
1523             TREE_VALUE (genwalker) = find_or_generate_expression (block, 
1524                                                                   TREE_VALUE (walker), 
1525                                                                   stmts);
1526           }
1527
1528         if (op2)          
1529           genop2 = find_or_generate_expression (block, op2, stmts);
1530         folded = fold (build (TREE_CODE (expr), TREE_TYPE (expr),
1531                               genop0, genarglist, genop2));
1532         break;
1533         
1534         
1535       }
1536       break;
1537       
1538     case tcc_binary:
1539     case tcc_comparison:
1540       {
1541         tree op1 = TREE_OPERAND (expr, 0);
1542         tree op2 = TREE_OPERAND (expr, 1);
1543         tree genop1 = find_or_generate_expression (block, op1, stmts);
1544         tree genop2 = find_or_generate_expression (block, op2, stmts);
1545         folded = fold (build (TREE_CODE (expr), TREE_TYPE (expr), 
1546                               genop1, genop2));
1547         break;
1548       }
1549
1550     case tcc_unary:
1551       {
1552         tree op1 = TREE_OPERAND (expr, 0);
1553         tree genop1 = find_or_generate_expression (block, op1, stmts);
1554         folded = fold (build (TREE_CODE (expr), TREE_TYPE (expr), 
1555                               genop1));
1556         break;
1557       }
1558
1559     default:
1560       gcc_unreachable ();
1561     }
1562
1563   /* Force the generated expression to be a sequence of GIMPLE
1564      statements.
1565      We have to call unshare_expr because force_gimple_operand may
1566      modify the tree we pass to it.  */
1567   newexpr = force_gimple_operand (unshare_expr (folded), &forced_stmts, 
1568                                   false, NULL);
1569
1570   /* If we have any intermediate expressions to the value sets, add them
1571      to the value sets and chain them on in the instruction stream.  */
1572   if (forced_stmts)
1573     {
1574       tsi = tsi_start (forced_stmts);
1575       for (; !tsi_end_p (tsi); tsi_next (&tsi))
1576         {
1577           tree stmt = tsi_stmt (tsi);
1578           tree forcedname = TREE_OPERAND (stmt, 0);
1579           tree forcedexpr = TREE_OPERAND (stmt, 1);
1580           tree val = vn_lookup_or_add (forcedexpr, NULL);
1581           
1582           VEC_safe_push (tree, heap, inserted_exprs, stmt);
1583           vn_add (forcedname, val, NULL);
1584           bitmap_value_replace_in_set (NEW_SETS (block), forcedname);
1585           bitmap_value_replace_in_set (AVAIL_OUT (block), forcedname);
1586         }
1587       tsi = tsi_last (stmts);
1588       tsi_link_after (&tsi, forced_stmts, TSI_CONTINUE_LINKING);
1589     }
1590
1591   /* Build and insert the assignment of the end result to the temporary
1592      that we will return.  */
1593   temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1594   add_referenced_tmp_var (temp);
1595   newexpr = build (MODIFY_EXPR, TREE_TYPE (expr), temp, newexpr);
1596   name = make_ssa_name (temp, newexpr);
1597   TREE_OPERAND (newexpr, 0) = name;
1598   NECESSARY (newexpr) = 0;
1599   tsi = tsi_last (stmts);
1600   tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1601   VEC_safe_push (tree, heap, inserted_exprs, newexpr);
1602
1603   /* Add a value handle to the temporary.
1604      The value may already exist in either NEW_SETS, or AVAIL_OUT, because
1605      we are creating the expression by pieces, and this particular piece of
1606      the expression may have been represented.  There is no harm in replacing
1607      here.  */
1608   v = get_value_handle (expr);
1609   vn_add (name, v, NULL);
1610   bitmap_value_replace_in_set (NEW_SETS (block), name); 
1611   bitmap_value_replace_in_set (AVAIL_OUT (block), name);
1612
1613   pre_stats.insertions++;
1614   if (dump_file && (dump_flags & TDF_DETAILS))
1615     {                               
1616       fprintf (dump_file, "Inserted ");
1617       print_generic_expr (dump_file, newexpr, 0);
1618       fprintf (dump_file, " in predecessor %d\n", block->index);
1619     }
1620
1621   return name;
1622 }
1623
1624 /* Insert the to-be-made-available values of NODE for each predecessor, stored
1625    in AVAIL, into the predecessors of BLOCK, and merge the result with a phi
1626    node, given the same value handle as NODE.  The prefix of the phi node is
1627    given with TMPNAME.  Return true if we have inserted new stuff.  */
1628
1629 static bool
1630 insert_into_preds_of_block (basic_block block, value_set_node_t node,
1631                             tree *avail, const char *tmpname)
1632 {
1633   tree val = get_value_handle (node->expr);
1634   edge pred;
1635   bool insertions = false;
1636   bool nophi = false;
1637   basic_block bprime;
1638   tree eprime;
1639   edge_iterator ei;
1640   tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
1641   tree temp;
1642   
1643   if (dump_file && (dump_flags & TDF_DETAILS))
1644     {
1645       fprintf (dump_file, "Found partial redundancy for expression ");
1646       print_generic_expr (dump_file, node->expr, 0);
1647       fprintf (dump_file, "\n");
1648     }
1649
1650   /* Make sure we aren't creating an induction variable.  */
1651   if (block->loop_depth > 0 && EDGE_COUNT (block->preds) == 2)
1652     {
1653       bool firstinsideloop = false;
1654       bool secondinsideloop = false;
1655       firstinsideloop = flow_bb_inside_loop_p (block->loop_father, 
1656                                                EDGE_PRED (block, 0)->src);
1657       secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
1658                                                 EDGE_PRED (block, 1)->src);
1659       /* Induction variables only have one edge inside the loop.  */
1660       if (firstinsideloop ^ secondinsideloop)
1661         {
1662           if (dump_file && (dump_flags & TDF_DETAILS))
1663             fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
1664           nophi = true;
1665         }
1666     }
1667           
1668
1669   /* Make the necessary insertions.  */
1670   FOR_EACH_EDGE (pred, ei, block->preds)
1671     {
1672       tree stmts = alloc_stmt_list ();
1673       tree builtexpr;
1674       bprime = pred->src;
1675       eprime = avail[bprime->index];
1676       if (BINARY_CLASS_P (eprime)
1677           || COMPARISON_CLASS_P (eprime)
1678           || UNARY_CLASS_P (eprime)
1679           || TREE_CODE (eprime) == CALL_EXPR)
1680         {
1681           builtexpr = create_expression_by_pieces (bprime,
1682                                                    eprime,
1683                                                    stmts);
1684           bsi_insert_on_edge (pred, stmts);
1685           avail[bprime->index] = builtexpr;
1686           insertions = true;
1687         }                             
1688     }
1689   /* If we didn't want a phi node, and we made insertions, we still have
1690      inserted new stuff, and thus return true.  If we didn't want a phi node,
1691      and didn't make insertions, we haven't added anything new, so return
1692      false.  */
1693   if (nophi && insertions)
1694     return true;
1695   else if (nophi && !insertions)
1696     return false;
1697
1698   /* Now build a phi for the new variable.  */
1699   temp = create_tmp_var (type, tmpname);
1700   add_referenced_tmp_var (temp);
1701   temp = create_phi_node (temp, block);
1702   NECESSARY (temp) = 0; 
1703   VEC_safe_push (tree, heap, inserted_exprs, temp);
1704   FOR_EACH_EDGE (pred, ei, block->preds)
1705     add_phi_arg (temp, avail[pred->src->index], pred);
1706   
1707   vn_add (PHI_RESULT (temp), val, NULL);
1708   
1709   /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
1710      this insertion, since we test for the existence of this value in PHI_GEN
1711      before proceeding with the partial redundancy checks in insert_aux.
1712      
1713      The value may exist in AVAIL_OUT, in particular, it could be represented
1714      by the expression we are trying to eliminate, in which case we want the
1715      replacement to occur.  If it's not existing in AVAIL_OUT, we want it
1716      inserted there.
1717      
1718      Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
1719      this block, because if it did, it would have existed in our dominator's
1720      AVAIL_OUT, and would have been skipped due to the full redundancy check.
1721   */
1722
1723   bitmap_insert_into_set (PHI_GEN (block),
1724                           PHI_RESULT (temp));
1725   bitmap_value_replace_in_set (AVAIL_OUT (block), 
1726                                PHI_RESULT (temp));
1727   bitmap_insert_into_set (NEW_SETS (block),
1728                           PHI_RESULT (temp));
1729   
1730   if (dump_file && (dump_flags & TDF_DETAILS))
1731     {
1732       fprintf (dump_file, "Created phi ");
1733       print_generic_expr (dump_file, temp, 0);
1734       fprintf (dump_file, " in block %d\n", block->index);
1735     }
1736   pre_stats.phis++;
1737   return true;
1738 }
1739
1740
1741       
1742 /* Perform insertion of partially redundant values.
1743    For BLOCK, do the following:
1744    1.  Propagate the NEW_SETS of the dominator into the current block.
1745    If the block has multiple predecessors, 
1746        2a. Iterate over the ANTIC expressions for the block to see if
1747            any of them are partially redundant.
1748        2b. If so, insert them into the necessary predecessors to make
1749            the expression fully redundant.
1750        2c. Insert a new PHI merging the values of the predecessors.
1751        2d. Insert the new PHI, and the new expressions, into the
1752            NEW_SETS set.  
1753    3. Recursively call ourselves on the dominator children of BLOCK.
1754
1755 */
1756
1757 static bool
1758 insert_aux (basic_block block)
1759 {
1760   basic_block son;
1761   bool new_stuff = false;
1762
1763   if (block)
1764     {
1765       basic_block dom;
1766       dom = get_immediate_dominator (CDI_DOMINATORS, block);
1767       if (dom)
1768         {
1769           unsigned i;
1770           bitmap_iterator bi;
1771           bitmap_set_t newset = NEW_SETS (dom);
1772           if (newset)
1773             {
1774               /* Note that we need to value_replace both NEW_SETS, and
1775                  AVAIL_OUT. For both the case of NEW_SETS, the value may be
1776                  represented by some non-simple expression here that we want
1777                  to replace it with.  */
1778               EXECUTE_IF_SET_IN_BITMAP (newset->expressions, 0, i, bi)
1779                 {
1780                   bitmap_value_replace_in_set (NEW_SETS (block), ssa_name (i));
1781                   bitmap_value_replace_in_set (AVAIL_OUT (block), ssa_name (i));
1782                 }
1783             }
1784           if (!single_pred_p (block))
1785             {
1786               value_set_node_t node;
1787               for (node = ANTIC_IN (block)->head;
1788                    node;
1789                    node = node->next)
1790                 {
1791                   if (BINARY_CLASS_P (node->expr)
1792                       || COMPARISON_CLASS_P (node->expr)
1793                       || UNARY_CLASS_P (node->expr)
1794                       || TREE_CODE (node->expr) == CALL_EXPR)
1795                     {
1796                       tree *avail;
1797                       tree val;
1798                       bool by_some = false;
1799                       bool cant_insert = false;
1800                       bool all_same = true;
1801                       tree first_s = NULL;
1802                       edge pred;
1803                       basic_block bprime;
1804                       tree eprime = NULL_TREE;
1805                       edge_iterator ei;
1806
1807                       val = get_value_handle (node->expr);
1808                       if (bitmap_set_contains_value (PHI_GEN (block), val))
1809                         continue; 
1810                       if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
1811                         {
1812                           if (dump_file && (dump_flags & TDF_DETAILS))
1813                             fprintf (dump_file, "Found fully redundant value\n");
1814                           continue;
1815                         }
1816                                               
1817                       avail = xcalloc (last_basic_block, sizeof (tree));
1818                       FOR_EACH_EDGE (pred, ei, block->preds)
1819                         {
1820                           tree vprime;
1821                           tree edoubleprime;
1822
1823                           /* This can happen in the very weird case
1824                              that our fake infinite loop edges have caused a
1825                              critical edge to appear.  */
1826                           if (EDGE_CRITICAL_P (pred))
1827                             {
1828                               cant_insert = true;
1829                               break;
1830                             }
1831                           bprime = pred->src;
1832                           eprime = phi_translate (node->expr,
1833                                                   ANTIC_IN (block),
1834                                                   bprime, block);
1835
1836                           /* eprime will generally only be NULL if the
1837                              value of the expression, translated
1838                              through the PHI for this predecessor, is
1839                              undefined.  If that is the case, we can't
1840                              make the expression fully redundant,
1841                              because its value is undefined along a
1842                              predecessor path.  We can thus break out
1843                              early because it doesn't matter what the
1844                              rest of the results are.  */
1845                           if (eprime == NULL)
1846                             {
1847                               cant_insert = true;
1848                               break;
1849                             }
1850
1851                           eprime = fully_constant_expression (eprime);
1852                           vprime = get_value_handle (eprime);
1853                           gcc_assert (vprime);
1854                           edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
1855                                                              vprime);
1856                           if (edoubleprime == NULL)
1857                             {
1858                               avail[bprime->index] = eprime;
1859                               all_same = false;
1860                             }
1861                           else
1862                             {
1863                               avail[bprime->index] = edoubleprime;
1864                               by_some = true; 
1865                               if (first_s == NULL)
1866                                 first_s = edoubleprime;
1867                               else if (!operand_equal_p (first_s, edoubleprime,
1868                                                          0))
1869                                 all_same = false;
1870                             }
1871                         }
1872                       /* If we can insert it, it's not the same value
1873                          already existing along every predecessor, and
1874                          it's defined by some predecessor, it is
1875                          partially redundant.  */
1876                       if (!cant_insert && !all_same && by_some)
1877                         {
1878                           if (insert_into_preds_of_block (block, node, avail, 
1879                                                           "prephitmp"))
1880                             new_stuff = true;
1881                         }
1882                       /* If all edges produce the same value and that value is
1883                          an invariant, then the PHI has the same value on all
1884                          edges.  Note this.  */
1885                       else if (!cant_insert && all_same && eprime 
1886                                && is_gimple_min_invariant (eprime)
1887                                && !is_gimple_min_invariant (val))
1888                         {
1889                           value_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
1890                           value_set_node_t node;
1891                           for (node = exprset->head; node; node = node->next)
1892                             {
1893                               if (TREE_CODE (node->expr) == SSA_NAME)
1894                                 {                                 
1895                                   vn_add (node->expr, eprime, NULL);
1896                                   pre_stats.constified++;
1897                                 }
1898                             }
1899                         }
1900                       free (avail);
1901                     }
1902                 }
1903             }
1904         }
1905     }
1906   for (son = first_dom_son (CDI_DOMINATORS, block);
1907        son;
1908        son = next_dom_son (CDI_DOMINATORS, son))
1909     {
1910       new_stuff |= insert_aux (son);
1911     }
1912
1913   return new_stuff;
1914 }
1915
1916 /* Perform insertion of partially redundant values.  */
1917
1918 static void
1919 insert (void)
1920 {
1921   bool new_stuff = true;
1922   basic_block bb;
1923   int num_iterations = 0;
1924   
1925   FOR_ALL_BB (bb)
1926     NEW_SETS (bb) = bitmap_set_new ();
1927   
1928   while (new_stuff)
1929     {
1930       num_iterations++;
1931       new_stuff = false;
1932       new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1933     }
1934   if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1935     fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1936 }
1937
1938
1939 /* Return true if VAR is an SSA variable with no defining statement in
1940    this procedure, *AND* isn't a live-on-entry parameter.  */
1941
1942 static bool
1943 is_undefined_value (tree expr)
1944 {
1945   return (TREE_CODE (expr) == SSA_NAME
1946           && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
1947           /* PARM_DECLs and hard registers are always defined.  */
1948           && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL);
1949 }
1950
1951
1952 /* Given an SSA variable VAR and an expression EXPR, compute the value
1953    number for EXPR and create a value handle (VAL) for it.  If VAR and
1954    EXPR are not the same, associate VAL with VAR.  Finally, add VAR to
1955    S1 and its value handle to S2.
1956
1957    VUSES represent the virtual use operands associated with EXPR (if
1958    any). They are used when computing the hash value for EXPR.  */
1959
1960 static inline void
1961 add_to_sets (tree var, tree expr, tree stmt, bitmap_set_t s1,
1962              bitmap_set_t s2)
1963 {
1964   tree val = vn_lookup_or_add (expr, stmt);
1965
1966   /* VAR and EXPR may be the same when processing statements for which
1967      we are not computing value numbers (e.g., non-assignments, or
1968      statements that make aliased stores).  In those cases, we are
1969      only interested in making VAR available as its own value.  */
1970   if (var != expr)
1971     vn_add (var, val, NULL_TREE);
1972
1973   if (s1)
1974     bitmap_insert_into_set (s1, var);
1975   bitmap_value_insert_into_set (s2, var);
1976 }
1977
1978
1979 /* Given a unary or binary expression EXPR, create and return a new
1980    expression with the same structure as EXPR but with its operands
1981    replaced with the value handles of each of the operands of EXPR.
1982
1983    VUSES represent the virtual use operands associated with EXPR (if
1984    any). They are used when computing the hash value for EXPR.
1985    Insert EXPR's operands into the EXP_GEN set for BLOCK. */
1986
1987 static inline tree
1988 create_value_expr_from (tree expr, basic_block block, tree stmt)
1989 {
1990   int i;
1991   enum tree_code code = TREE_CODE (expr);
1992   tree vexpr;
1993   alloc_pool pool;
1994
1995   gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
1996               || TREE_CODE_CLASS (code) == tcc_binary
1997               || TREE_CODE_CLASS (code) == tcc_comparison
1998               || TREE_CODE_CLASS (code) == tcc_reference
1999               || TREE_CODE_CLASS (code) == tcc_expression
2000               || TREE_CODE_CLASS (code) == tcc_exceptional);
2001
2002   if (TREE_CODE_CLASS (code) == tcc_unary)
2003     pool = unary_node_pool;
2004   else if (TREE_CODE_CLASS (code) == tcc_reference)
2005     pool = reference_node_pool;
2006   else if (TREE_CODE_CLASS (code) == tcc_binary)
2007     pool = binary_node_pool;
2008   else if (TREE_CODE_CLASS (code) == tcc_comparison)
2009     pool = comparison_node_pool;
2010   else if (TREE_CODE_CLASS (code) == tcc_exceptional)
2011     {
2012       gcc_assert (code == TREE_LIST);
2013       pool = list_node_pool;
2014     }
2015   else 
2016     {
2017       gcc_assert (code == CALL_EXPR);
2018       pool = expression_node_pool;
2019     }
2020
2021   vexpr = pool_alloc (pool);
2022   memcpy (vexpr, expr, tree_size (expr));
2023   
2024   /* This case is only for TREE_LIST's that appear as part of
2025      CALL_EXPR's.  Anything else is a bug, but we can't easily verify
2026      this, hence this comment.  TREE_LIST is not handled by the
2027      general case below is because they don't have a fixed length, or
2028      operands, so you can't access purpose/value/chain through
2029      TREE_OPERAND macros.  */
2030
2031   if (code == TREE_LIST)
2032     {
2033       tree temp = NULL_TREE;
2034       if (TREE_CHAIN (vexpr))
2035         temp = create_value_expr_from (TREE_CHAIN (vexpr), block, stmt);      
2036       TREE_CHAIN (vexpr) = temp ? temp : TREE_CHAIN (vexpr);
2037       
2038       /* This is the equivalent of inserting op into EXP_GEN like we
2039          do below */
2040       if (!is_undefined_value (TREE_VALUE (vexpr)))
2041         value_insert_into_set (EXP_GEN (block), TREE_VALUE (vexpr));      
2042           
2043       TREE_VALUE (vexpr) = vn_lookup_or_add (TREE_VALUE (vexpr), NULL);
2044
2045       return vexpr;
2046     }
2047
2048   for (i = 0; i < TREE_CODE_LENGTH (code); i++)
2049     {
2050       tree val, op;
2051       
2052       op = TREE_OPERAND (expr, i);
2053       if (op == NULL_TREE)
2054         continue;
2055
2056       /* If OP is a constant that has overflowed, do not value number
2057          this expression.  */
2058       if (CONSTANT_CLASS_P (op)
2059           && TREE_OVERFLOW (op))
2060         {
2061           pool_free (pool, vexpr);
2062           return NULL;
2063         }
2064
2065       /* Recursively value-numberize reference ops and tree lists.  */
2066       if (REFERENCE_CLASS_P (op))
2067         {
2068           tree tempop = create_value_expr_from (op, block, stmt);
2069           op = tempop ? tempop : op;
2070           val = vn_lookup_or_add (op, stmt);
2071         }
2072       else if (TREE_CODE (op) == TREE_LIST)
2073         {
2074           tree tempop;
2075           
2076           gcc_assert (TREE_CODE (expr) == CALL_EXPR);
2077           tempop = create_value_expr_from (op, block, stmt);
2078           
2079           op = tempop ? tempop : op;
2080           vn_lookup_or_add (op, NULL);
2081           /* Unlike everywhere else, we do *not* want to replace the
2082              TREE_LIST itself with a value number, because support
2083              functions we call will blow up.  */
2084           val = op;
2085         }
2086       else       
2087         /* Create a value handle for OP and add it to VEXPR.  */
2088         val = vn_lookup_or_add (op, NULL);
2089
2090       if (!is_undefined_value (op) && TREE_CODE (op) != TREE_LIST)
2091         value_insert_into_set (EXP_GEN (block), op);
2092
2093       if (TREE_CODE (val) == VALUE_HANDLE)
2094         TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
2095
2096       TREE_OPERAND (vexpr, i) = val;
2097     }
2098
2099   return vexpr;
2100 }
2101
2102
2103 /* Return true if we can value number a call.  This is true if we have
2104    a pure or constant call.  */
2105 static bool
2106 can_value_number_call (tree stmt)
2107 {
2108   tree call = get_call_expr_in (stmt);
2109
2110   /* This is a temporary restriction until we translate vuses through
2111      phi nodes.  */
2112   if (!ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
2113     return false;  
2114   if (call_expr_flags (call)  & (ECF_PURE | ECF_CONST))
2115     return true;
2116   return false;
2117 }
2118
2119 /* Compute the AVAIL set for all basic blocks.
2120
2121    This function performs value numbering of the statements in each basic
2122    block.  The AVAIL sets are built from information we glean while doing
2123    this value numbering, since the AVAIL sets contain only one entry per
2124    value.
2125    
2126    AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
2127    AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK].  */
2128
2129 static void
2130 compute_avail (void)
2131 {
2132   basic_block block, son;
2133   basic_block *worklist;
2134   size_t sp = 0;
2135   tree param;
2136
2137   /* For arguments with default definitions, we pretend they are
2138      defined in the entry block.  */
2139   for (param = DECL_ARGUMENTS (current_function_decl);
2140        param;
2141        param = TREE_CHAIN (param))
2142     {
2143       if (default_def (param) != NULL)
2144         {
2145           tree def = default_def (param);
2146           vn_lookup_or_add (def, NULL);
2147           bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
2148           bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
2149         }
2150     }
2151
2152   /* Allocate the worklist.  */
2153   worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
2154
2155   /* Seed the algorithm by putting the dominator children of the entry
2156      block on the worklist.  */
2157   for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR);
2158        son;
2159        son = next_dom_son (CDI_DOMINATORS, son))
2160     worklist[sp++] = son;
2161
2162   /* Loop until the worklist is empty.  */
2163   while (sp)
2164     {
2165       block_stmt_iterator bsi;
2166       tree stmt, phi;
2167       basic_block dom;
2168
2169       /* Pick a block from the worklist.  */
2170       block = worklist[--sp];
2171
2172       /* Initially, the set of available values in BLOCK is that of
2173          its immediate dominator.  */
2174       dom = get_immediate_dominator (CDI_DOMINATORS, block);
2175       if (dom)
2176         bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
2177
2178       /* Generate values for PHI nodes.  */
2179       for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
2180         /* We have no need for virtual phis, as they don't represent
2181            actual computations.  */
2182         if (is_gimple_reg (PHI_RESULT (phi)))
2183           add_to_sets (PHI_RESULT (phi), PHI_RESULT (phi), NULL,
2184                        PHI_GEN (block), AVAIL_OUT (block));
2185
2186       /* Now compute value numbers and populate value sets with all
2187          the expressions computed in BLOCK.  */
2188       for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
2189         {
2190           stmt_ann_t ann;
2191           ssa_op_iter iter;
2192           tree op;
2193
2194           stmt = bsi_stmt (bsi);
2195           ann = stmt_ann (stmt);
2196
2197           /* We are only interested in assignments of the form
2198              X_i = EXPR, where EXPR represents an "interesting"
2199              computation, it has no volatile operands and X_i
2200              doesn't flow through an abnormal edge.  */
2201           if (TREE_CODE (stmt) == MODIFY_EXPR
2202               && !ann->has_volatile_ops
2203               && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
2204               && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (stmt, 0)))
2205             {
2206               tree lhs = TREE_OPERAND (stmt, 0);
2207               tree rhs = TREE_OPERAND (stmt, 1);
2208
2209               STRIP_USELESS_TYPE_CONVERSION (rhs);
2210               if (UNARY_CLASS_P (rhs)
2211                   || BINARY_CLASS_P (rhs)
2212                   || COMPARISON_CLASS_P (rhs)
2213                   || REFERENCE_CLASS_P (rhs)
2214                   || (TREE_CODE (rhs) == CALL_EXPR
2215                       && can_value_number_call (stmt)))
2216                 {
2217                   /* For binary, unary, and reference expressions,
2218                      create a duplicate expression with the operands
2219                      replaced with the value handles of the original
2220                      RHS.  */
2221                   tree newt = create_value_expr_from (rhs, block, stmt);
2222                   if (newt)
2223                     {
2224                       add_to_sets (lhs, newt, stmt, TMP_GEN (block),
2225                                    AVAIL_OUT (block));
2226                       value_insert_into_set (EXP_GEN (block), newt);
2227                       continue;
2228                     }
2229                 }
2230               else if (TREE_CODE (rhs) == SSA_NAME
2231                        || is_gimple_min_invariant (rhs)
2232                        || TREE_CODE (rhs) == ADDR_EXPR
2233                        || TREE_INVARIANT (rhs)
2234                        || DECL_P (rhs))
2235                 {
2236                   /* Compute a value number for the RHS of the statement
2237                      and add its value to the AVAIL_OUT set for the block.
2238                      Add the LHS to TMP_GEN.  */
2239                   add_to_sets (lhs, rhs, stmt, TMP_GEN (block), 
2240                                AVAIL_OUT (block));
2241                   
2242                   if (TREE_CODE (rhs) == SSA_NAME
2243                       && !is_undefined_value (rhs))
2244                     value_insert_into_set (EXP_GEN (block), rhs);
2245                   continue;
2246                 }          
2247             }
2248
2249           /* For any other statement that we don't recognize, simply
2250              make the names generated by the statement available in
2251              AVAIL_OUT and TMP_GEN.  */
2252           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
2253             add_to_sets (op, op, NULL, TMP_GEN (block), AVAIL_OUT (block));
2254
2255           FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
2256             add_to_sets (op, op, NULL, NULL , AVAIL_OUT (block));
2257         }
2258
2259       /* Put the dominator children of BLOCK on the worklist of blocks
2260          to compute available sets for.  */
2261       for (son = first_dom_son (CDI_DOMINATORS, block);
2262            son;
2263            son = next_dom_son (CDI_DOMINATORS, son))
2264         worklist[sp++] = son;
2265     }
2266
2267   free (worklist);
2268 }
2269
2270
2271 /* Eliminate fully redundant computations.  */
2272
2273 static void
2274 eliminate (void)
2275 {
2276   basic_block b;
2277
2278   FOR_EACH_BB (b)
2279     {
2280       block_stmt_iterator i;
2281       
2282       for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
2283         {
2284           tree stmt = bsi_stmt (i);
2285
2286           /* Lookup the RHS of the expression, see if we have an
2287              available computation for it.  If so, replace the RHS with
2288              the available computation.  */
2289           if (TREE_CODE (stmt) == MODIFY_EXPR
2290               && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
2291               && TREE_CODE (TREE_OPERAND (stmt ,1)) != SSA_NAME
2292               && !is_gimple_min_invariant (TREE_OPERAND (stmt, 1))
2293               && !stmt_ann (stmt)->has_volatile_ops)
2294             {
2295               tree lhs = TREE_OPERAND (stmt, 0);
2296               tree *rhs_p = &TREE_OPERAND (stmt, 1);
2297               tree sprime;
2298
2299               sprime = bitmap_find_leader (AVAIL_OUT (b),
2300                                            vn_lookup (lhs, NULL));
2301               if (sprime 
2302                   && sprime != lhs
2303                   && (TREE_CODE (*rhs_p) != SSA_NAME
2304                       || may_propagate_copy (*rhs_p, sprime)))
2305                 {
2306                   gcc_assert (sprime != *rhs_p);
2307
2308                   if (dump_file && (dump_flags & TDF_DETAILS))
2309                     {
2310                       fprintf (dump_file, "Replaced ");
2311                       print_generic_expr (dump_file, *rhs_p, 0);
2312                       fprintf (dump_file, " with ");
2313                       print_generic_expr (dump_file, sprime, 0);
2314                       fprintf (dump_file, " in ");
2315                       print_generic_stmt (dump_file, stmt, 0);
2316                     }
2317                   if (TREE_CODE (sprime) == SSA_NAME) 
2318                     NECESSARY (SSA_NAME_DEF_STMT (sprime)) = 1;
2319                   pre_stats.eliminations++;
2320                   propagate_tree_value (rhs_p, sprime);
2321                   update_stmt (stmt);
2322
2323                   /* If we removed EH side effects from the statement, clean
2324                      its EH information.  */
2325                   if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
2326                     {
2327                       bitmap_set_bit (need_eh_cleanup,
2328                                       bb_for_stmt (stmt)->index);
2329                       if (dump_file && (dump_flags & TDF_DETAILS))
2330                         fprintf (dump_file, "  Removed EH side effects.\n");
2331                     }
2332                 }
2333             }
2334         }
2335     }
2336 }
2337
2338 /* Borrow a bit of tree-ssa-dce.c for the moment.
2339    XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
2340    this may be a bit faster, and we may want critical edges kept split.  */
2341
2342 /* If OP's defining statement has not already been determined to be necessary,
2343    mark that statement necessary. Return the stmt, if it is newly
2344    necessary.  */ 
2345
2346 static inline tree
2347 mark_operand_necessary (tree op)
2348 {
2349   tree stmt;
2350
2351   gcc_assert (op);
2352
2353   stmt = SSA_NAME_DEF_STMT (op);
2354   gcc_assert (stmt);
2355
2356   if (NECESSARY (stmt)
2357       || IS_EMPTY_STMT (stmt))
2358     return NULL;
2359
2360   NECESSARY (stmt) = 1;
2361   return stmt;
2362 }
2363
2364 /* Because we don't follow exactly the standard PRE algorithm, and decide not
2365    to insert PHI nodes sometimes, and because value numbering of casts isn't
2366    perfect, we sometimes end up inserting dead code.   This simple DCE-like
2367    pass removes any insertions we made that weren't actually used.  */
2368
2369 static void
2370 remove_dead_inserted_code (void)
2371 {
2372   VEC(tree,heap) *worklist = NULL;
2373   int i;
2374   tree t;
2375
2376   worklist = VEC_alloc (tree, heap, VEC_length (tree, inserted_exprs));
2377   for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
2378     {
2379       if (NECESSARY (t))
2380         VEC_quick_push (tree, worklist, t);
2381     }
2382   while (VEC_length (tree, worklist) > 0)
2383     {
2384       t = VEC_pop (tree, worklist);
2385       if (TREE_CODE (t) == PHI_NODE)
2386         {
2387           /* PHI nodes are somewhat special in that each PHI alternative has
2388              data and control dependencies.  All the statements feeding the
2389              PHI node's arguments are always necessary.  In aggressive mode,
2390              we also consider the control dependent edges leading to the
2391              predecessor block associated with each PHI alternative as
2392              necessary.  */
2393           int k;
2394
2395           VEC_reserve (tree, heap, worklist, PHI_NUM_ARGS (t));
2396           for (k = 0; k < PHI_NUM_ARGS (t); k++)
2397             {
2398               tree arg = PHI_ARG_DEF (t, k);
2399               if (TREE_CODE (arg) == SSA_NAME)
2400                 {
2401                   arg = mark_operand_necessary (arg);
2402                   if (arg)
2403                     VEC_quick_push (tree, worklist, arg);
2404                 }
2405             }
2406         }
2407       else
2408         {
2409           /* Propagate through the operands.  Examine all the USE, VUSE and
2410              V_MAY_DEF operands in this statement.  Mark all the statements 
2411              which feed this statement's uses as necessary.  */
2412           ssa_op_iter iter;
2413           tree use;
2414
2415           /* The operands of V_MAY_DEF expressions are also needed as they
2416              represent potential definitions that may reach this
2417              statement (V_MAY_DEF operands allow us to follow def-def 
2418              links).  */
2419
2420           FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
2421             {
2422               tree n = mark_operand_necessary (use);
2423               if (n)
2424                 VEC_safe_push (tree, heap, worklist, n);
2425             }
2426         }
2427     }
2428   for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
2429     {
2430       if (!NECESSARY (t))
2431         {
2432           block_stmt_iterator bsi;
2433           if (dump_file && (dump_flags & TDF_DETAILS))
2434             {
2435               fprintf (dump_file, "Removing unnecessary insertion:");
2436               print_generic_stmt (dump_file, t, 0);
2437             }
2438           if (TREE_CODE (t) == PHI_NODE)
2439             {
2440               remove_phi_node (t, NULL);
2441             }
2442           else
2443             {
2444               bsi = bsi_for_stmt (t);
2445               bsi_remove (&bsi);
2446             }
2447         }
2448     }
2449   VEC_free (tree, heap, worklist);
2450 }
2451 /* Initialize data structures used by PRE.  */
2452
2453 static void
2454 init_pre (bool do_fre)
2455 {
2456   basic_block bb;
2457
2458   inserted_exprs = NULL;
2459   vn_init ();
2460   if (!do_fre)
2461     current_loops = loop_optimizer_init (dump_file);
2462   connect_infinite_loops_to_exit ();
2463   memset (&pre_stats, 0, sizeof (pre_stats));
2464
2465   /* If block 0 has more than one predecessor, it means that its PHI
2466      nodes will have arguments coming from block -1.  This creates
2467      problems for several places in PRE that keep local arrays indexed
2468      by block number.  To prevent this, we split the edge coming from
2469      ENTRY_BLOCK_PTR (FIXME, if ENTRY_BLOCK_PTR had an index number
2470      different than -1 we wouldn't have to hack this.  tree-ssa-dce.c
2471      needs a similar change).  */
2472   if (!single_pred_p (single_succ (ENTRY_BLOCK_PTR)))
2473     if (!(single_succ_edge (ENTRY_BLOCK_PTR)->flags & EDGE_ABNORMAL))
2474       split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
2475
2476   FOR_ALL_BB (bb)
2477     bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
2478
2479   bitmap_obstack_initialize (&grand_bitmap_obstack);
2480   phi_translate_table = htab_create (511, expr_pred_trans_hash,
2481                                      expr_pred_trans_eq, free);
2482   value_set_pool = create_alloc_pool ("Value sets",
2483                                       sizeof (struct value_set), 30);
2484   bitmap_set_pool = create_alloc_pool ("Bitmap sets",
2485                                        sizeof (struct bitmap_set), 30);
2486   value_set_node_pool = create_alloc_pool ("Value set nodes",
2487                                            sizeof (struct value_set_node), 30);
2488   calculate_dominance_info (CDI_POST_DOMINATORS);
2489   calculate_dominance_info (CDI_DOMINATORS);
2490   binary_node_pool = create_alloc_pool ("Binary tree nodes",
2491                                         tree_code_size (PLUS_EXPR), 30);
2492   unary_node_pool = create_alloc_pool ("Unary tree nodes",
2493                                        tree_code_size (NEGATE_EXPR), 30);
2494   reference_node_pool = create_alloc_pool ("Reference tree nodes",
2495                                            tree_code_size (ARRAY_REF), 30);
2496   expression_node_pool = create_alloc_pool ("Expression tree nodes",
2497                                             tree_code_size (CALL_EXPR), 30);
2498   list_node_pool = create_alloc_pool ("List tree nodes",
2499                                       tree_code_size (TREE_LIST), 30);  
2500   comparison_node_pool = create_alloc_pool ("Comparison tree nodes",
2501                                             tree_code_size (EQ_EXPR), 30);
2502   FOR_ALL_BB (bb)
2503     {
2504       EXP_GEN (bb) = set_new (true);
2505       PHI_GEN (bb) = bitmap_set_new ();
2506       TMP_GEN (bb) = bitmap_set_new ();
2507       AVAIL_OUT (bb) = bitmap_set_new ();
2508     }
2509
2510   need_eh_cleanup = BITMAP_ALLOC (NULL);
2511 }
2512
2513
2514 /* Deallocate data structures used by PRE.  */
2515
2516 static void
2517 fini_pre (bool do_fre)
2518 {
2519   basic_block bb;
2520   unsigned int i;
2521
2522   VEC_free (tree, heap, inserted_exprs);
2523   bitmap_obstack_release (&grand_bitmap_obstack);
2524   free_alloc_pool (value_set_pool);
2525   free_alloc_pool (bitmap_set_pool);
2526   free_alloc_pool (value_set_node_pool);
2527   free_alloc_pool (binary_node_pool);
2528   free_alloc_pool (reference_node_pool);
2529   free_alloc_pool (unary_node_pool);
2530   free_alloc_pool (list_node_pool);
2531   free_alloc_pool (expression_node_pool);
2532   free_alloc_pool (comparison_node_pool);
2533   htab_delete (phi_translate_table);
2534   remove_fake_exit_edges ();
2535
2536   FOR_ALL_BB (bb)
2537     {
2538       free (bb->aux);
2539       bb->aux = NULL;
2540     }
2541
2542   free_dominance_info (CDI_POST_DOMINATORS);
2543   vn_delete ();
2544
2545   if (!bitmap_empty_p (need_eh_cleanup))
2546     {
2547       tree_purge_all_dead_eh_edges (need_eh_cleanup);
2548       cleanup_tree_cfg ();
2549     }
2550
2551   BITMAP_FREE (need_eh_cleanup);
2552
2553   /* Wipe out pointers to VALUE_HANDLEs.  In the not terribly distant
2554      future we will want them to be persistent though.  */
2555   for (i = 0; i < num_ssa_names; i++)
2556     {
2557       tree name = ssa_name (i);
2558
2559       if (!name)
2560         continue;
2561
2562       if (SSA_NAME_VALUE (name)
2563           && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
2564         SSA_NAME_VALUE (name) = NULL;
2565     }
2566   if (!do_fre && current_loops)
2567     {
2568       loop_optimizer_finalize (current_loops, dump_file);
2569       current_loops = NULL;
2570     }
2571 }
2572
2573
2574 /* Main entry point to the SSA-PRE pass.  DO_FRE is true if the caller
2575    only wants to do full redundancy elimination.  */
2576
2577 static void
2578 execute_pre (bool do_fre)
2579 {
2580   init_pre (do_fre);
2581
2582   /* Collect and value number expressions computed in each basic block.  */
2583   compute_avail ();
2584
2585   if (dump_file && (dump_flags & TDF_DETAILS))
2586     {
2587       basic_block bb;
2588
2589       FOR_ALL_BB (bb)
2590         {
2591           print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
2592           bitmap_print_value_set (dump_file, TMP_GEN (bb), "tmp_gen", 
2593                                   bb->index);
2594           bitmap_print_value_set (dump_file, AVAIL_OUT (bb), "avail_out", 
2595                                   bb->index);
2596         }
2597     }
2598
2599   /* Insert can get quite slow on an incredibly large number of basic
2600      blocks due to some quadratic behavior.  Until this behavior is
2601      fixed, don't run it when he have an incredibly large number of
2602      bb's.  If we aren't going to run insert, there is no point in
2603      computing ANTIC, either, even though it's plenty fast.  */
2604   if (!do_fre && n_basic_blocks < 4000)
2605     {
2606       compute_antic ();
2607       insert ();
2608     }
2609
2610   /* Remove all the redundant expressions.  */
2611   eliminate ();
2612
2613
2614   if (dump_file && (dump_flags & TDF_STATS))
2615     {
2616       fprintf (dump_file, "Insertions: %d\n", pre_stats.insertions);
2617       fprintf (dump_file, "New PHIs: %d\n", pre_stats.phis);
2618       fprintf (dump_file, "Eliminated: %d\n", pre_stats.eliminations);
2619       fprintf (dump_file, "Constified: %d\n", pre_stats.constified);
2620     }
2621   
2622   bsi_commit_edge_inserts ();
2623   if (!do_fre)
2624     remove_dead_inserted_code ();
2625   fini_pre (do_fre);
2626
2627 }
2628
2629
2630 /* Gate and execute functions for PRE.  */
2631
2632 static void
2633 do_pre (void)
2634 {
2635   execute_pre (false);
2636 }
2637
2638 static bool
2639 gate_pre (void)
2640 {
2641   return flag_tree_pre != 0;
2642 }
2643
2644 struct tree_opt_pass pass_pre =
2645 {
2646   "pre",                                /* name */
2647   gate_pre,                             /* gate */
2648   do_pre,                               /* execute */
2649   NULL,                                 /* sub */
2650   NULL,                                 /* next */
2651   0,                                    /* static_pass_number */
2652   TV_TREE_PRE,                          /* tv_id */
2653   PROP_no_crit_edges | PROP_cfg
2654     | PROP_ssa | PROP_alias,            /* properties_required */
2655   0,                                    /* properties_provided */
2656   0,                                    /* properties_destroyed */
2657   0,                                    /* todo_flags_start */
2658   TODO_update_ssa | TODO_dump_func | TODO_ggc_collect 
2659   | TODO_verify_ssa, /* todo_flags_finish */
2660   0                                     /* letter */
2661 };
2662
2663
2664 /* Gate and execute functions for FRE.  */
2665
2666 static void
2667 execute_fre (void)
2668 {
2669   execute_pre (true);
2670 }
2671
2672 static bool
2673 gate_fre (void)
2674 {
2675   return flag_tree_fre != 0;
2676 }
2677
2678 struct tree_opt_pass pass_fre =
2679 {
2680   "fre",                                /* name */
2681   gate_fre,                             /* gate */
2682   execute_fre,                          /* execute */
2683   NULL,                                 /* sub */
2684   NULL,                                 /* next */
2685   0,                                    /* static_pass_number */
2686   TV_TREE_FRE,                          /* tv_id */
2687   PROP_cfg | PROP_ssa | PROP_alias,     /* properties_required */
2688   0,                                    /* properties_provided */
2689   0,                                    /* properties_destroyed */
2690   0,                                    /* todo_flags_start */
2691   TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
2692   0                                     /* letter */
2693 };