PR fortran/14923
[official-gcc.git] / gcc / tree-ssa-pre.c
bloba1f33ad36f374b6cd3022a5a62bb16dc5433f5a2
1 /* SSA-PRE for trees.
2 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
4 <stevenb@suse.de>
6 This file is part of GCC.
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.
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.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "errors.h"
27 #include "ggc.h"
28 #include "tree.h"
30 /* These RTL headers are needed for basic-block.h. */
31 #include "rtl.h"
32 #include "tm_p.h"
33 #include "hard-reg-set.h"
34 #include "basic-block.h"
35 #include "diagnostic.h"
36 #include "tree-inline.h"
37 #include "tree-flow.h"
38 #include "tree-gimple.h"
39 #include "tree-dump.h"
40 #include "timevar.h"
41 #include "fibheap.h"
42 #include "hashtab.h"
43 #include "tree-iterator.h"
44 #include "real.h"
45 #include "alloc-pool.h"
46 #include "tree-pass.h"
47 #include "flags.h"
48 #include "splay-tree.h"
49 #include "bitmap.h"
50 #include "langhooks.h"
51 /* TODO:
53 1. Implement load value numbering.
54 2. Speed up insert_aux so that we can use it all the time. It
55 spends most of it's time in quadratic value replacement.
56 3. Avail sets can be shared by making an avail_find_leader that
57 walks up the dominator tree and looks in those avail sets.
58 This might affect code optimality, it's unclear right now.
59 4. Load motion can be performed by value numbering the loads the
60 same as we do other expressions. This requires iterative
61 hashing the vuses into the values. Right now we simply assign
62 a new value every time we see a statement with a vuse.
63 5. Strength reduction can be performed by anticipating expressions
64 we can repair later on.
65 */
67 /* For ease of terminology, "expression node" in the below refers to
68 every expression node but MODIFY_EXPR, because MODIFY_EXPR's represent
69 the actual statement containing the expressions we care about, and
70 we cache the value number by putting it in the expression. */
72 /* Basic algorithm
74 First we walk the statements to generate the AVAIL sets, the EXP_GEN
75 sets, and the tmp_gen sets. AVAIL is a forward dataflow
76 problem. EXP_GEN sets represent the generation of
77 values/expressions by a given block. We use them when computing
78 the ANTIC sets. The AVAIL sets consist of SSA_NAME's that
79 represent values, so we know what values are available in what
80 blocks. In SSA, values are never killed, so we don't need a kill
81 set, or a fixpoint iteration, in order to calculate the AVAIL sets.
82 In traditional parlance, AVAIL sets tell us the downsafety of the
83 expressions/values.
85 Next, we generate the ANTIC sets. ANTIC is a backwards dataflow
86 problem. These sets represent the anticipatable expressions. An
87 expression is anticipatable in a given block if it could be
88 generated in that block. This means that if we had to perform an
89 insertion in that block, of the value of that expression, we could.
90 Calculating the ANTIC sets requires phi translation of expressions,
91 because the flow goes backwards through phis. We must iterate to a
92 fixpoint of the ANTIC sets, because we have a kill set.
93 Even in SSA form, values are not live over the entire function,
94 only from their definition point onwards. So we have to remove
95 values from the ANTIC set once we go past the definition point of
96 the leaders that make them up. compute_antic/compute_antic_aux
97 performs this computation.
99 Third, we perform insertions to make partially redundant
100 expressions fully redundant.
102 An expression is partially redundant (excluding partial
103 anticipation) if:
105 1. It is AVAIL in some, but not all, of the predecessors of a
106 given block.
107 2. It is ANTIC in all the predecessors.
109 In order to make it fully redundant, we insert the expression into
110 the predecessors where it is not available, but is ANTIC.
111 insert/insert_aux performs this insertion.
113 Fourth, we eliminate fully redundant expressions.
114 This is a simple statement walk that replaces redundant
115 calculations with the now available values. */
117 /* Representations of value numbers:
119 Value numbers are represented using the "value handle" approach.
120 This means that each SSA_NAME (and for other reasons to be
121 disclosed in a moment, expression nodes and constant nodes) has a
122 value handle that can be retrieved through get_value_handle. This
123 value handle, *is* the value number of the SSA_NAME. You can
124 pointer compare the value handles for equivalence purposes.
126 For debugging reasons, the value handle is internally more than
127 just a number, it is a VAR_DECL named "value.x", where x is a
128 unique number for each value number in use. This allows
129 expressions with SSA_NAMES replaced by value handles to still be
130 pretty printed in a sane way. They simply print as "value.3 *
131 value.5", etc.
133 Expression nodes have value handles associated with them as a
134 cache. Otherwise, we'd have to look them up again in the hash
135 table This makes significant difference (factor of two or more) on
136 some test cases. They can be thrown away after the Constants have
137 value handles associated with them so that they aren't special
138 cased everywhere, and for consistency sake. This may be changed
139 depending on memory usage vs code maintenance tradeoff. */
141 /* Representation of expressions on value numbers:
143 In some portions of this code, you will notice we allocate "fake"
144 analogues to the expression we are value numbering, and replace the
145 operands with the values of the expression. Since we work on
146 values, and not just names, we canonicalize expressions to value
147 expressions for use in the ANTIC sets, the EXP_GEN set, etc.
149 This is theoretically unnecessary, it just saves a bunch of
150 repeated get_value_handle and find_leader calls in the remainder of
151 the code, trading off temporary memory usage for speed. The tree
152 nodes aren't actually creating more garbage, since they are
153 allocated in a special pools which are thrown away at the end of
154 this pass.
156 All of this also means that if you print the EXP_GEN or ANTIC sets,
157 you will see "value.5 + value.7" in the set, instead of "a_55 +
158 b_66" or something. The only thing that actually cares about
159 seeing the value leaders is phi translation, and it needs to be
160 able to find the leader for a value in an arbitrary block, so this
161 "value expression" form is perfect for it (otherwise you'd do
162 get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
165 /* Representation of sets:
167 Sets are represented as doubly linked lists kept in topological
168 order, with an optional supporting bitmap of values present in the
169 set. The sets represent values, and the elements can be constants,
170 values, or expressions. The elements can appear in different sets,
171 but each element can only appear once in each set.
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. */
181 /* A value set element. Basically a single linked list of
182 expressions/constants/values. */
183 typedef struct value_set_node
185 tree expr;
186 struct value_set_node *next;
187 } *value_set_node_t;
190 /* A value set, which is the head of the linked list, and we also keep
191 the tail because we have to append for the topolofical sort. */
192 typedef struct value_set
194 value_set_node_t head;
195 value_set_node_t tail;
196 size_t length;
197 bool indexed;
198 bitmap values;
200 } *value_set_t;
202 /* All of the following sets, except for TMP_GEN, are indexed.
203 TMP_GEN is only ever iterated over, we never check what values
204 exist in it. */
205 typedef struct bb_value_sets
207 value_set_t exp_gen;
208 value_set_t phi_gen;
209 value_set_t tmp_gen;
210 value_set_t avail_out;
211 value_set_t antic_in;
212 value_set_t new_sets;
213 } *bb_value_sets_t;
215 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
216 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
217 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
218 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
219 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
220 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
222 static struct
224 int eliminations;
225 int insertions;
226 int phis;
227 } pre_stats;
229 static tree find_leader (value_set_t, tree);
230 static void value_insert_into_set (value_set_t, tree);
231 static void insert_into_set (value_set_t, tree);
232 static void add_to_value (tree, tree);
233 static value_set_t set_new (bool);
234 static bool is_undefined_value (tree);
236 /* We can add and remove elements and entries to and from sets
237 and hash tables, so we use alloc pools for them. */
239 static alloc_pool value_set_pool;
240 static alloc_pool value_set_node_pool;
241 static alloc_pool binary_node_pool;
242 static alloc_pool unary_node_pool;
244 /* The value table that maps expressions to values. */
245 static htab_t value_table;
247 /* The phi_translate_table caches phi translations for a given
248 expression and predecessor. */
249 static htab_t phi_translate_table;
252 /* Map expressions to values. These are simple pairs of expressions
253 and the values they represent. To find the value represented by
254 an expression, we use a hash table where the elements are {e,v}
255 pairs, and the expression is the key. */
257 typedef struct val_expr_pair_d
259 tree v, e;
260 hashval_t hashcode;
261 } *val_expr_pair_t;
264 /* Hash a {v,e} pair. We really only hash the expression. */
266 static hashval_t
267 val_expr_pair_hash (const void *p)
269 const val_expr_pair_t ve = (val_expr_pair_t) p;
270 return ve->hashcode;
274 /* Are {e2,v2} and {e1,v1} the same? Again, only the expression
275 matters. */
277 static int
278 val_expr_pair_expr_eq (const void *p1, const void *p2)
280 const val_expr_pair_t ve1 = (val_expr_pair_t) p1;
281 const val_expr_pair_t ve2 = (val_expr_pair_t) p2;
282 tree e1 = ve1->e;
283 tree e2 = ve2->e;
284 tree te1;
285 tree te2;
286 if (e1 == e2)
287 return true;
289 te1 = TREE_TYPE (e1);
290 te2 = TREE_TYPE (e2);
291 if (TREE_CODE (e1) == TREE_CODE (e2)
292 && (te1 == te2 || lang_hooks.types_compatible_p (te1, te2))
293 && operand_equal_p (e1, e2, 0))
294 return true;
296 return false;
300 /* Get the value handle of EXPR. This is the only correct way to get
301 the value handle for a "thing". */
303 tree
304 get_value_handle (tree expr)
306 /* We should never see these. */
307 if (DECL_P (expr))
308 abort ();
309 else if (TREE_CODE (expr) == SSA_NAME)
311 return SSA_NAME_VALUE (expr);
313 else if (TREE_CODE_CLASS (TREE_CODE (expr)) == 'c')
315 cst_ann_t ann = cst_ann (expr);
316 if (ann)
317 return ann->common.value_handle;
318 return NULL;
320 else if (EXPR_P (expr))
322 expr_ann_t ann = expr_ann (expr);
323 if (ann)
324 return ann->common.value_handle;
325 return NULL;
327 abort ();
331 /* Set the value handle for E to V */
333 void
334 set_value_handle (tree e, tree v)
336 if (DECL_P (e))
337 abort ();
338 else if (TREE_CODE (e) == SSA_NAME)
339 SSA_NAME_VALUE (e) = v;
340 else if (TREE_CODE_CLASS (TREE_CODE (e)) == 'c')
341 get_cst_ann (e)->common.value_handle = v;
342 else if (EXPR_P (e))
343 get_expr_ann (e)->common.value_handle = v;
346 /* A three tuple {e, pred, v} used to cache phi translations in the
347 phi_translate_table. */
349 typedef struct expr_pred_trans_d
351 tree e;
352 basic_block pred;
353 tree v;
354 hashval_t hashcode;
355 } *expr_pred_trans_t;
357 /* Return the hash value for a phi translation table entry. */
359 static hashval_t
360 expr_pred_trans_hash (const void *p)
362 const expr_pred_trans_t ve = (expr_pred_trans_t) p;
363 return ve->hashcode;
366 /* Return true if two phi translation table entries are the same. */
368 static int
369 expr_pred_trans_eq (const void *p1, const void *p2)
371 const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
372 const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
373 tree e1 = ve1->e;
374 tree e2 = ve2->e;
375 basic_block b1 = ve1->pred;
376 basic_block b2 = ve2->pred;
377 tree te1;
378 tree te2;
380 if (b1 != b2)
381 return false;
383 if (e1 == e2)
384 return true;
386 te1 = TREE_TYPE (e1);
387 te2 = TREE_TYPE (e2);
389 if (TREE_CODE (e1) == TREE_CODE (e2)
390 && (te1 == te2 || lang_hooks.types_compatible_p (te1, te2))
391 && operand_equal_p (e1, e2, 0))
392 return true;
394 return false;
397 /* Search in the phi translation table for the translation of E in
398 PRED. Return the translated value, if found, NULL otherwise. */
400 static inline tree
401 phi_trans_lookup (tree e, basic_block pred)
403 void **slot;
404 struct expr_pred_trans_d ugly;
405 ugly.e = e;
406 ugly.pred = pred;
407 ugly.hashcode = iterative_hash_expr (e, (unsigned long) pred);
408 slot = htab_find_slot_with_hash (phi_translate_table, &ugly, ugly.hashcode,
409 NO_INSERT);
410 if (!slot)
411 return NULL;
412 else
413 return ((expr_pred_trans_t) *slot)->v;
417 /* Add the tuple mapping {e, pred}->v to the phi translation table. */
419 static inline void
420 phi_trans_add (tree e, tree v, basic_block pred)
422 void **slot;
423 expr_pred_trans_t new_pair = xmalloc (sizeof (*new_pair));
424 new_pair->e = e;
425 new_pair->pred = pred;
426 new_pair->v = v;
427 new_pair->hashcode = iterative_hash_expr (e, (unsigned long) pred);
428 slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
429 new_pair->hashcode, INSERT);
430 if (*slot)
431 free (*slot);
432 *slot = (void *) new_pair;
435 /* Search in TABLE for an existing instance of expression E,
436 and return its value, or NULL if none has been set. */
438 static inline tree
439 lookup (htab_t table, tree e)
441 void **slot;
442 struct val_expr_pair_d ugly = {NULL, NULL, 0};
443 ugly.e = e;
444 ugly.hashcode = iterative_hash_expr (e,0);
445 slot = htab_find_slot_with_hash (table, &ugly, ugly.hashcode, NO_INSERT);
446 if (!slot)
447 return NULL_TREE;
448 else
449 return ((val_expr_pair_t) *slot)->v;
452 /* Add E to the expression set of V. */
454 static inline void
455 add_to_value (tree v, tree e)
457 #if DEBUG_VALUE_EXPRESSIONS
458 var_ann_t va = var_ann (v);
459 #endif
460 /* For values representing numerical constants, we mark
461 TREE_CONSTANT as true and set the tree chain to the actual
462 constant. This is because unlike values involving expressions,
463 which are only available to use where the expressions are live, a
464 constant can be remade anywhere, and thus, is available
465 everywhere. */
466 if (TREE_CODE_CLASS (TREE_CODE (e)) == 'c')
468 TREE_CONSTANT (v) = true;
469 TREE_CHAIN (v) = e;
471 #if DEBUG_VALUE_EXPRESSIONS
472 if (va->expr_set == NULL)
473 va->expr_set = set_new (false);
474 insert_into_set (va->expr_set, e);
475 #endif
478 /* Insert E into TABLE with value V, and add E to the value set for V. */
480 static inline void
481 add (htab_t table, tree e, tree v)
484 void **slot;
485 val_expr_pair_t new_pair = xmalloc (sizeof (struct val_expr_pair_d));
486 new_pair->e = e;
487 new_pair->v = v;
488 new_pair->hashcode = iterative_hash_expr (e, 0);
489 slot = htab_find_slot_with_hash (table, new_pair, new_pair->hashcode,
490 INSERT);
491 if (*slot)
492 free (*slot);
493 *slot = (void *) new_pair;
494 set_value_handle (e, v);
496 add_to_value (v, e);
500 static int pre_uid;
502 /* Create a new value handle for EXPR. */
503 static tree
504 create_new_value (tree expr)
506 tree a = create_tmp_var_raw (TREE_TYPE (expr), "value");
507 create_var_ann (a);
508 var_ann (a)->uid = pre_uid++;
510 if (dump_file && (dump_flags & TDF_DETAILS))
512 fprintf (dump_file, "Created value ");
513 print_generic_expr (dump_file, a, dump_flags);
514 fprintf (dump_file, " for ");
515 print_generic_expr (dump_file, expr, dump_flags);
516 fprintf (dump_file, "\n");
518 return a;
521 /* Like lookup, but adds V as the value for E if E does not have a value. */
522 static inline tree
523 lookup_or_add (htab_t table, tree e)
525 tree x = lookup (table, e);
526 if (x == NULL_TREE)
528 tree v;
529 v = create_new_value (e);
530 add (table, e, v);
531 x = v;
533 set_value_handle (e, x);
534 return x;
538 /* Search in the bitmap for SET to see if E exists. */
540 static inline bool
541 value_exists_in_set_bitmap (value_set_t set, tree e)
543 if (TREE_CODE (e) != VAR_DECL)
544 abort ();
546 if (!set->values)
547 return false;
548 return bitmap_bit_p (set->values, get_var_ann (e)->uid);
551 /* Remove E from the bitmap for SET. */
553 static void
554 value_remove_from_set_bitmap (value_set_t set, tree e)
556 if (TREE_CODE (e) != VAR_DECL)
557 abort ();
558 #ifdef ENABLE_CHECKING
559 if (!set->indexed)
560 abort ();
561 #endif
562 if (!set->values)
563 return;
564 bitmap_clear_bit (set->values, get_var_ann (e)->uid);
568 /* Insert the value number E into the bitmap of values existing in
569 SET. */
571 static inline void
572 value_insert_into_set_bitmap (value_set_t set, tree e)
574 if (TREE_CODE (e) != VAR_DECL)
575 abort ();
576 #ifdef ENABLE_CHECKING
577 if (!set->indexed)
578 abort ();
579 #endif
580 if (set->values == NULL)
582 set->values = BITMAP_GGC_ALLOC ();
583 bitmap_clear (set->values);
585 bitmap_set_bit (set->values, get_var_ann (e)->uid);
588 /* Create a new set. */
590 static value_set_t
591 set_new (bool indexed)
593 value_set_t ret;
594 ret = pool_alloc (value_set_pool);
595 ret->head = ret->tail = NULL;
596 ret->length = 0;
597 ret->indexed = indexed;
598 ret->values = NULL;
599 return ret;
603 /* Insert EXPR into SET. */
605 static void
606 insert_into_set (value_set_t set, tree expr)
608 value_set_node_t newnode = pool_alloc (value_set_node_pool);
609 tree val = get_value_handle (expr);
610 if (DECL_P (expr))
611 abort ();
613 if (val == NULL)
614 abort ();
616 /* For indexed sets, insert the value into the set value bitmap.
617 For all sets, add it to the linked list and increment the list
618 length. */
619 if (set->indexed)
620 value_insert_into_set_bitmap (set, val);
622 newnode->next = NULL;
623 newnode->expr = expr;
624 set->length ++;
625 if (set->head == NULL)
627 set->head = set->tail = newnode;
629 else
631 set->tail->next = newnode;
632 set->tail = newnode;
636 /* Copy the set ORIG to the set DEST. */
638 static void
639 set_copy (value_set_t dest, value_set_t orig)
641 value_set_node_t node;
643 if (!orig || !orig->head)
644 return;
646 for (node = orig->head;
647 node;
648 node = node->next)
650 insert_into_set (dest, node->expr);
654 /* Remove EXPR from SET. */
656 static void
657 set_remove (value_set_t set, tree expr)
659 value_set_node_t node, prev;
661 /* Remove the value of EXPR from the bitmap, decrement the set
662 length, and remove it from the actual double linked list. */
663 value_remove_from_set_bitmap (set, get_value_handle (expr));
664 set->length--;
665 prev = NULL;
666 for (node = set->head;
667 node != NULL;
668 prev = node, node = node->next)
670 if (node->expr == expr)
672 if (prev == NULL)
673 set->head = node->next;
674 else
675 prev->next= node->next;
677 if (node == set->tail)
678 set->tail = prev;
679 pool_free (value_set_node_pool, node);
680 return;
685 /* Return true if SET contains the value VAL. */
687 static bool
688 set_contains_value (value_set_t set, tree val)
690 /* This is only referring to the flag above that we set on
691 values referring to numerical constants, because we know that we
692 are dealing with one of the value handles we created. */
693 if (TREE_CONSTANT (val))
694 return true;
696 if (set->length == 0)
697 return false;
699 return value_exists_in_set_bitmap (set, val);
702 /* Replace the leader for the value LOOKFOR in SET with EXPR. */
704 static void
705 set_replace_value (value_set_t set, tree lookfor, tree expr)
707 value_set_node_t node = set->head;
709 /* The lookup is probably more expensive than walking the linked
710 list when we have only a small number of nodes. */
711 if (!set_contains_value (set, lookfor))
712 return;
714 for (node = set->head;
715 node;
716 node = node->next)
718 if (get_value_handle (node->expr) == lookfor)
720 node->expr = expr;
721 return;
726 /* Return true if the set contains expression (not value) EXPR. */
728 static bool
729 set_contains (value_set_t set, tree expr)
731 value_set_node_t node;
733 for (node = set->head;
734 node;
735 node = node->next)
737 if (operand_equal_p (node->expr, expr, 0))
738 return true;
740 return false;
743 /* Subtract set B from set A, and return the new set. */
745 static value_set_t
746 set_subtract (value_set_t a, value_set_t b, bool indexed)
748 value_set_t ret = set_new (indexed);
749 value_set_node_t node;
750 for (node = a->head;
751 node;
752 node = node->next)
754 if (!set_contains (b, node->expr))
755 insert_into_set (ret, node->expr);
757 return ret;
760 /* Return true if two sets are equal. */
762 static bool
763 set_equal (value_set_t a, value_set_t b)
765 value_set_node_t node;
767 if (a->length != b->length)
768 return false;
769 for (node = a->head;
770 node;
771 node = node->next)
773 if (!set_contains_value (b, get_value_handle (node->expr)))
774 return false;
776 return true;
779 /* Replace the value for EXPR in SET with EXPR. */
780 static void
781 value_replace_in_set (value_set_t set, tree expr)
783 tree val = get_value_handle (expr);
785 if (set->length == 0)
786 return;
788 set_replace_value (set, val, expr);
791 /* Insert the value for EXPR into SET, if it doesn't exist already. */
793 static void
794 value_insert_into_set (value_set_t set, tree expr)
796 tree val = get_value_handle (expr);
798 /* Constant values exist everywhere. */
799 if (TREE_CONSTANT (val))
800 return;
802 if (!set_contains_value (set, val))
803 insert_into_set (set, expr);
807 /* Print out the value_set SET to OUTFILE. */
809 static void
810 print_value_set (FILE *outfile, value_set_t set,
811 const char *setname, int blockindex)
813 value_set_node_t node;
814 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
815 if (set)
817 for (node = set->head;
818 node;
819 node = node->next)
821 print_generic_expr (outfile, node->expr, 0);
822 if (node->next)
823 fprintf (outfile, ", ");
827 fprintf (outfile, " }\n");
830 /* Print out the expressions that have VAL to OUTFILE. */
831 void
832 print_value_expressions (FILE *outfile, tree val)
834 var_ann_t va = var_ann (val);
835 if (va && va->expr_set)
836 print_value_set (outfile, va->expr_set,
837 IDENTIFIER_POINTER (DECL_NAME (val)), 0);
841 void
842 debug_value_expressions (tree val)
844 print_value_expressions (stderr, val);
848 void debug_value_set (value_set_t, const char *, int);
850 void
851 debug_value_set (value_set_t set, const char *setname, int blockindex)
853 print_value_set (stderr, set, setname, blockindex);
856 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
857 the phis in PRED. Return NULL if we can't find a leader for each
858 part of the translated expression. */
860 static tree
861 phi_translate (tree expr, value_set_t set, basic_block pred,
862 basic_block phiblock)
864 tree phitrans = NULL;
865 tree oldexpr = expr;
867 if (expr == NULL)
868 return NULL;
870 /* Phi translations of a given expression don't change, */
871 phitrans = phi_trans_lookup (expr, pred);
872 if (phitrans)
873 return phitrans;
876 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
878 case '2':
880 tree oldop1 = TREE_OPERAND (expr, 0);
881 tree oldop2 = TREE_OPERAND (expr, 1);
882 tree newop1;
883 tree newop2;
884 tree newexpr;
886 newop1 = phi_translate (find_leader (set, oldop1),
887 set, pred, phiblock);
888 if (newop1 == NULL)
889 return NULL;
890 newop2 = phi_translate (find_leader (set, oldop2),
891 set, pred, phiblock);
892 if (newop2 == NULL)
893 return NULL;
894 if (newop1 != oldop1 || newop2 != oldop2)
896 newexpr = pool_alloc (binary_node_pool);
897 memcpy (newexpr, expr, tree_size (expr));
898 create_expr_ann (newexpr);
899 TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
900 TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
901 lookup_or_add (value_table, newexpr);
902 expr = newexpr;
903 phi_trans_add (oldexpr, newexpr, pred);
906 break;
907 case '1':
909 tree oldop1 = TREE_OPERAND (expr, 0);
910 tree newop1;
911 tree newexpr;
913 newop1 = phi_translate (find_leader (set, oldop1),
914 set, pred, phiblock);
915 if (newop1 == NULL)
916 return NULL;
917 if (newop1 != oldop1)
919 newexpr = pool_alloc (unary_node_pool);
920 memcpy (newexpr, expr, tree_size (expr));
921 create_expr_ann (newexpr);
922 TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
923 lookup_or_add (value_table, newexpr);
924 expr = newexpr;
925 phi_trans_add (oldexpr, newexpr, pred);
928 break;
929 case 'd':
930 abort ();
931 case 'x':
933 tree phi = NULL;
934 int i;
935 if (TREE_CODE (expr) != SSA_NAME)
936 abort ();
937 if (TREE_CODE (SSA_NAME_DEF_STMT (expr)) == PHI_NODE)
938 phi = SSA_NAME_DEF_STMT (expr);
939 else
940 return expr;
942 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
943 if (PHI_ARG_EDGE (phi, i)->src == pred)
945 tree val;
946 if (is_undefined_value (PHI_ARG_DEF (phi, i)))
947 return NULL;
948 val = lookup_or_add (value_table, PHI_ARG_DEF (phi, i));
949 return PHI_ARG_DEF (phi, i);
952 break;
954 return expr;
957 static void
958 phi_translate_set (value_set_t dest, value_set_t set, basic_block pred,
959 basic_block phiblock)
961 value_set_node_t node;
962 for (node = set->head;
963 node;
964 node = node->next)
966 tree translated;
967 translated = phi_translate (node->expr, set, pred, phiblock);
968 phi_trans_add (node->expr, translated, pred);
970 if (translated != NULL)
971 value_insert_into_set (dest, translated);
975 /* Find the leader for a value (IE the name representing that
976 value) in a given set, and return it. Return NULL if no leader is
977 found. */
979 static tree
980 find_leader (value_set_t set, tree val)
982 value_set_node_t node;
984 if (val == NULL)
985 return NULL;
987 if (TREE_CONSTANT (val))
988 return TREE_CHAIN (val);
990 if (set->length == 0)
991 return NULL;
993 if (value_exists_in_set_bitmap (set, val))
995 for (node = set->head;
996 node;
997 node = node->next)
999 if (get_value_handle (node->expr) == val)
1000 return node->expr;
1003 return NULL;
1006 /* Determine if the expression EXPR is valid in SET. This means that
1007 we have a leader for each part of the expression (if it consists of
1008 values), or the expression is an SSA_NAME.
1010 NB: We never should run into a case where we have SSA_NAME +
1011 SSA_NAME or SSA_NAME + value. The sets valid_in_set is called on,
1012 the ANTIC sets, will only ever have SSA_NAME's or binary value
1013 expression (IE VALUE1 + VALUE2) */
1015 static bool
1016 valid_in_set (value_set_t set, tree expr)
1018 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1020 case '2':
1022 tree op1 = TREE_OPERAND (expr, 0);
1023 tree op2 = TREE_OPERAND (expr, 1);
1024 return set_contains_value (set, op1) && set_contains_value (set, op2);
1026 break;
1027 case '1':
1029 tree op1 = TREE_OPERAND (expr, 0);
1030 return set_contains_value (set, op1);
1032 break;
1033 case 'x':
1035 if (TREE_CODE (expr) == SSA_NAME)
1036 return true;
1037 abort ();
1039 case 'c':
1040 abort ();
1042 return false;
1045 /* Clean the set of expressions that are no longer valid in the
1046 specified set. This means expressions that are made up of values
1047 we have no leaders for in the current set, etc. */
1049 static void
1050 clean (value_set_t set)
1052 value_set_node_t node;
1053 value_set_node_t next;
1054 node = set->head;
1055 while (node)
1057 next = node->next;
1058 if (!valid_in_set (set, node->expr))
1059 set_remove (set, node->expr);
1060 node = next;
1064 /* Compute the ANTIC set for BLOCK.
1066 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK), if
1067 succs(BLOCK) > 1
1068 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)]) if
1069 succs(BLOCK) == 1
1071 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] -
1072 TMP_GEN[BLOCK])
1074 Iterate until fixpointed.
1076 XXX: It would be nice to either write a set_clear, and use it for
1077 antic_out, or to mark the antic_out set as deleted at the end
1078 of this routine, so that the pool can hand the same memory back out
1079 again for the next antic_out. */
1082 static bool
1083 compute_antic_aux (basic_block block)
1085 basic_block son;
1086 edge e;
1087 bool changed = false;
1088 value_set_t S, old, ANTIC_OUT;
1089 value_set_node_t node;
1091 ANTIC_OUT = S = NULL;
1092 /* If any edges from predecessors are abnormal, antic_in is empty, so
1093 punt. Remember that the block has an incoming abnormal edge by
1094 setting the BB_VISITED flag. */
1095 if (! (block->flags & BB_VISITED))
1097 for (e = block->pred; e; e = e->pred_next)
1098 if (e->flags & EDGE_ABNORMAL)
1100 block->flags |= BB_VISITED;
1101 break;
1104 if (block->flags & BB_VISITED)
1106 S = NULL;
1107 goto visit_sons;
1111 old = set_new (false);
1112 set_copy (old, ANTIC_IN (block));
1113 ANTIC_OUT = set_new (true);
1115 /* If the block has no successors, ANTIC_OUT is empty, because it is
1116 the exit block. */
1117 if (block->succ == NULL);
1119 /* If we have one successor, we could have some phi nodes to
1120 translate through. */
1121 else if (block->succ->succ_next == NULL)
1123 phi_translate_set (ANTIC_OUT, ANTIC_IN(block->succ->dest),
1124 block, block->succ->dest);
1126 /* If we have multiple successors, we take the intersection of all of
1127 them. */
1128 else
1130 varray_type worklist;
1131 edge e;
1132 size_t i;
1133 basic_block bprime, first;
1135 VARRAY_BB_INIT (worklist, 1, "succ");
1136 e = block->succ;
1137 while (e)
1139 VARRAY_PUSH_BB (worklist, e->dest);
1140 e = e->succ_next;
1142 first = VARRAY_BB (worklist, 0);
1143 set_copy (ANTIC_OUT, ANTIC_IN (first));
1145 for (i = 1; i < VARRAY_ACTIVE_SIZE (worklist); i++)
1147 bprime = VARRAY_BB (worklist, i);
1148 node = ANTIC_OUT->head;
1149 while (node)
1151 tree val;
1152 value_set_node_t next = node->next;
1153 val = get_value_handle (node->expr);
1154 if (!set_contains_value (ANTIC_IN (bprime), val))
1155 set_remove (ANTIC_OUT, node->expr);
1156 node = next;
1159 VARRAY_CLEAR (worklist);
1162 /* Generate ANTIC_OUT - TMP_GEN */
1163 S = set_subtract (ANTIC_OUT, TMP_GEN (block), false);
1165 /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1166 ANTIC_IN (block) = set_subtract (EXP_GEN (block),TMP_GEN (block), true);
1168 /* Then union in the ANTIC_OUT - TMP_GEN values, to get ANTIC_OUT U
1169 EXP_GEN - TMP_GEN */
1170 for (node = S->head;
1171 node;
1172 node = node->next)
1174 value_insert_into_set (ANTIC_IN (block), node->expr);
1176 clean (ANTIC_IN (block));
1178 if (!set_equal (old, ANTIC_IN (block)))
1179 changed = true;
1181 visit_sons:
1182 if (dump_file && (dump_flags & TDF_DETAILS))
1184 if (ANTIC_OUT)
1185 print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1186 print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1187 if (S)
1188 print_value_set (dump_file, S, "S", block->index);
1192 for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1193 son;
1194 son = next_dom_son (CDI_POST_DOMINATORS, son))
1196 changed |= compute_antic_aux (son);
1198 return changed;
1201 /* Compute ANTIC sets. */
1203 static void
1204 compute_antic (void)
1206 bool changed = true;
1207 basic_block bb;
1208 int num_iterations = 0;
1209 FOR_ALL_BB (bb)
1211 ANTIC_IN (bb) = set_new (true);
1212 bb->flags &= ~BB_VISITED;
1215 while (changed)
1217 num_iterations++;
1218 changed = false;
1219 changed = compute_antic_aux (EXIT_BLOCK_PTR);
1221 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1222 fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1225 /* Perform insertion of partially redundant values.
1226 For BLOCK, do the following:
1227 1. Propagate the NEW_SETS of the dominator into the current block.
1228 If the block has multiple predecessors,
1229 2a. Iterate over the ANTIC expressions for the block to see if
1230 any of them are partially redundant.
1231 2b. If so, insert them into the necessary predecessors to make
1232 the expression fully redundant.
1233 2c. Insert a new PHI merging the values of the predecessors.
1234 2d. Insert the new PHI, and the new expressions, into the
1235 NEW_SETS set.
1236 3. Recursively call ourselves on the dominator children of BLOCK.
1239 static bool
1240 insert_aux (basic_block block)
1242 basic_block son;
1243 bool new_stuff = false;
1245 if (block)
1247 value_set_node_t e;
1248 basic_block dom;
1249 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1250 if (dom)
1252 e = NEW_SETS (dom)->head;
1253 while (e)
1255 insert_into_set (NEW_SETS (block), e->expr);
1256 value_replace_in_set (AVAIL_OUT (block), e->expr);
1257 e = e->next;
1259 if (block->pred->pred_next)
1261 value_set_node_t node;
1262 for (node = ANTIC_IN (block)->head;
1263 node;
1264 node = node->next)
1266 if (TREE_CODE_CLASS (TREE_CODE (node->expr)) == '2'
1267 || TREE_CODE_CLASS (TREE_CODE (node->expr)) == '1')
1269 tree *avail;
1270 tree val;
1271 bool by_some = false;
1272 bool all_same = true;
1273 tree first_s = NULL;
1274 edge pred;
1275 basic_block bprime;
1276 tree eprime;
1277 val = get_value_handle (node->expr);
1278 if (set_contains_value (PHI_GEN (block), val))
1279 continue;
1280 if (set_contains_value (AVAIL_OUT (dom), val))
1282 if (dump_file && (dump_flags & TDF_DETAILS))
1283 fprintf (dump_file, "Found fully redundant value\n");
1284 continue;
1288 avail = xcalloc (last_basic_block, sizeof (tree));
1289 for (pred = block->pred;
1290 pred;
1291 pred = pred->pred_next)
1293 tree vprime;
1294 tree edoubleprime;
1295 bprime = pred->src;
1296 eprime = phi_translate (node->expr,
1297 ANTIC_IN (block),
1298 bprime, block);
1299 if (eprime == NULL)
1300 continue;
1302 vprime = get_value_handle (eprime);
1303 if (!vprime)
1304 abort ();
1305 edoubleprime = find_leader (AVAIL_OUT (bprime),
1306 vprime);
1307 if (edoubleprime == NULL)
1309 avail[bprime->index] = eprime;
1310 all_same = false;
1312 else
1314 avail[bprime->index] = edoubleprime;
1315 by_some = true;
1316 if (first_s == NULL)
1317 first_s = edoubleprime;
1318 else if (first_s != edoubleprime)
1319 all_same = false;
1320 if (first_s != edoubleprime
1321 && operand_equal_p (first_s, edoubleprime, 0))
1322 abort ();
1326 if (!all_same && by_some)
1328 tree temp;
1329 tree type = TREE_TYPE (avail[block->pred->src->index]);
1330 tree v;
1332 if (dump_file && (dump_flags & TDF_DETAILS))
1334 fprintf (dump_file, "Found partial redundancy for expression ");
1335 print_generic_expr (dump_file, node->expr, 0);
1336 fprintf (dump_file, "\n");
1339 /* Make the necessary insertions. */
1340 for (pred = block->pred;
1341 pred;
1342 pred = pred->pred_next)
1344 bprime = pred->src;
1345 eprime = avail[bprime->index];
1346 if (TREE_CODE_CLASS (TREE_CODE (eprime)) == '2')
1348 tree s1, s2;
1349 tree newexpr;
1350 s1 = find_leader (AVAIL_OUT (bprime),
1351 TREE_OPERAND (eprime, 0));
1352 /* Depending on the order we process
1353 DOM branches in, the value may
1354 not have propagated to all the
1355 dom children yet during this
1356 iteration. In this case, the
1357 value will always be in the
1358 NEW_SETS for *our* dominator */
1359 if (!s1)
1360 s1 = find_leader (NEW_SETS (dom),
1361 TREE_OPERAND (eprime, 0));
1362 if (!s1)
1363 abort ();
1365 s2 = find_leader (AVAIL_OUT (bprime),
1366 TREE_OPERAND (eprime, 1));
1367 if (!s2)
1368 s2 = find_leader (NEW_SETS (dom),
1369 TREE_OPERAND (eprime, 1));
1370 if (!s2)
1371 abort ();
1373 temp = create_tmp_var (TREE_TYPE (eprime),
1374 "pretmp");
1375 add_referenced_tmp_var (temp);
1376 newexpr = build (TREE_CODE (eprime),
1377 TREE_TYPE (eprime),
1378 s1, s2);
1379 newexpr = build (MODIFY_EXPR,
1380 TREE_TYPE (eprime),
1381 temp, newexpr);
1382 temp = make_ssa_name (temp, newexpr);
1383 TREE_OPERAND (newexpr, 0) = temp;
1384 bsi_insert_on_edge (pred, newexpr);
1385 bsi_commit_edge_inserts (NULL);
1387 if (dump_file && (dump_flags & TDF_DETAILS))
1389 fprintf (dump_file, "Inserted ");
1390 print_generic_expr (dump_file, newexpr, 0);
1391 fprintf (dump_file, " in predecessor %d\n", pred->src->index);
1393 pre_stats.insertions++;
1394 v = lookup_or_add (value_table, eprime);
1395 add (value_table, temp, v);
1396 insert_into_set (NEW_SETS (bprime), temp);
1397 value_insert_into_set (AVAIL_OUT (bprime),
1398 temp);
1399 avail[bprime->index] = temp;
1401 else if (TREE_CODE_CLASS (TREE_CODE (eprime)) == '1')
1403 tree s1;
1404 tree newexpr;
1405 s1 = find_leader (AVAIL_OUT (bprime),
1406 TREE_OPERAND (eprime, 0));
1407 /* Depending on the order we process
1408 DOM branches in, the value may not have
1409 propagated to all the dom
1410 children yet in the current
1411 iteration, but it will be in
1412 NEW_SETS if it is not yet
1413 propagated. */
1415 if (!s1)
1416 s1 = find_leader (NEW_SETS (dom),
1417 TREE_OPERAND (eprime, 0));
1418 if (!s1)
1419 abort ();
1421 temp = create_tmp_var (TREE_TYPE (eprime),
1422 "pretmp");
1423 add_referenced_tmp_var (temp);
1424 newexpr = build (TREE_CODE (eprime),
1425 TREE_TYPE (eprime),
1426 s1);
1427 newexpr = build (MODIFY_EXPR,
1428 TREE_TYPE (eprime),
1429 temp, newexpr);
1430 temp = make_ssa_name (temp, newexpr);
1431 TREE_OPERAND (newexpr, 0) = temp;
1432 bsi_insert_on_edge (pred, newexpr);
1433 bsi_commit_edge_inserts (NULL);
1435 if (dump_file && (dump_flags & TDF_DETAILS))
1437 fprintf (dump_file, "Inserted ");
1438 print_generic_expr (dump_file, newexpr, 0);
1439 fprintf (dump_file, " in predecessor %d\n", pred->src->index);
1441 pre_stats.insertions++;
1442 v = lookup_or_add (value_table, eprime);
1443 add (value_table, temp, v);
1444 insert_into_set (NEW_SETS (bprime), temp);
1445 value_insert_into_set (AVAIL_OUT (bprime),
1446 temp);
1447 avail[bprime->index] = temp;
1450 /* Now build a phi for the new variable. */
1451 temp = create_tmp_var (type, "prephitmp");
1452 add_referenced_tmp_var (temp);
1453 temp = create_phi_node (temp, block);
1454 add (value_table, PHI_RESULT (temp), val);
1456 #if 0
1457 if (!set_contains_value (AVAIL_OUT (block), val))
1458 insert_into_set (AVAIL_OUT (block),
1459 PHI_RESULT (temp));
1460 else
1461 #endif
1462 value_replace_in_set (AVAIL_OUT (block),
1463 PHI_RESULT (temp));
1464 for (pred = block->pred;
1465 pred;
1466 pred = pred->pred_next)
1468 add_phi_arg (&temp, avail[pred->src->index],
1469 pred);
1471 if (dump_file && (dump_flags & TDF_DETAILS))
1473 fprintf (dump_file, "Created phi ");
1474 print_generic_expr (dump_file, temp, 0);
1475 fprintf (dump_file, " in block %d\n", block->index);
1477 pre_stats.phis++;
1478 new_stuff = true;
1479 insert_into_set (NEW_SETS (block),
1480 PHI_RESULT (temp));
1481 insert_into_set (PHI_GEN (block),
1482 PHI_RESULT (temp));
1485 free (avail);
1491 for (son = first_dom_son (CDI_DOMINATORS, block);
1492 son;
1493 son = next_dom_son (CDI_DOMINATORS, son))
1495 new_stuff |= insert_aux (son);
1498 return new_stuff;
1501 /* Perform insertion of partially redundant values. */
1503 static void
1504 insert (void)
1506 bool new_stuff = true;
1507 basic_block bb;
1508 int num_iterations = 0;
1510 FOR_ALL_BB (bb)
1511 NEW_SETS (bb) = set_new (true);
1513 while (new_stuff)
1515 num_iterations++;
1516 new_stuff = false;
1517 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1519 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1520 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1523 /* Return true if EXPR has no defining statement in this procedure,
1524 *AND* isn't a live-on-entry parameter. */
1525 static bool
1526 is_undefined_value (tree expr)
1529 #ifdef ENABLE_CHECKING
1530 /* We should never be handed DECL's */
1531 if (DECL_P (expr))
1532 abort ();
1533 #endif
1534 if (TREE_CODE (expr) == SSA_NAME)
1536 /* XXX: Is this the correct test? */
1537 if (TREE_CODE (SSA_NAME_VAR (expr)) == PARM_DECL)
1538 return false;
1539 if (IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr)))
1540 return true;
1542 return false;
1545 /* Compute the AVAIL set for BLOCK.
1546 This function performs value numbering of the statements in BLOCK.
1547 The AVAIL sets are built from information we glean while doing this
1548 value numbering, since the AVAIL sets contain only entry per
1549 value.
1552 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
1553 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U
1554 TMP_GEN[BLOCK].
1557 static void
1558 compute_avail (basic_block block)
1560 basic_block son;
1562 /* For arguments with default definitions, we pretend they are
1563 defined in the entry block. */
1564 if (block == ENTRY_BLOCK_PTR)
1566 tree param;
1567 for (param = DECL_ARGUMENTS (current_function_decl);
1568 param;
1569 param = TREE_CHAIN (param))
1571 if (default_def (param) != NULL)
1573 tree val;
1574 tree def = default_def (param);
1575 val = lookup_or_add (value_table, def);
1576 insert_into_set (TMP_GEN (block), def);
1577 value_insert_into_set (AVAIL_OUT (block), def);
1581 else if (block)
1583 block_stmt_iterator bsi;
1584 tree stmt, phi;
1585 basic_block dom;
1587 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1588 if (dom)
1589 set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
1590 for (phi = phi_nodes (block); phi; phi = TREE_CHAIN (phi))
1592 /* Ignore virtual PHIs until we can do PRE on expressions
1593 with virtual operands. */
1594 if (!is_gimple_reg (SSA_NAME_VAR (PHI_RESULT (phi))))
1595 continue;
1597 lookup_or_add (value_table, PHI_RESULT (phi));
1598 value_insert_into_set (AVAIL_OUT (block), PHI_RESULT (phi));
1599 insert_into_set (PHI_GEN (block), PHI_RESULT (phi));
1602 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
1604 tree op0, op1;
1605 stmt = bsi_stmt (bsi);
1606 get_stmt_operands (stmt);
1608 if (NUM_VUSES (STMT_VUSE_OPS (stmt))
1609 || NUM_V_MUST_DEFS (STMT_V_MUST_DEF_OPS (stmt))
1610 || NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt))
1611 || stmt_ann (stmt)->has_volatile_ops)
1613 size_t j;
1614 for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1616 tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1617 lookup_or_add (value_table, def);
1618 insert_into_set (TMP_GEN (block), def);
1619 value_insert_into_set (AVAIL_OUT (block), def);
1621 continue;
1623 else if (TREE_CODE (stmt) == RETURN_EXPR
1624 && TREE_OPERAND (stmt, 0)
1625 && TREE_CODE (TREE_OPERAND (stmt, 0)) == MODIFY_EXPR)
1626 stmt = TREE_OPERAND (stmt, 0);
1628 if (TREE_CODE (stmt) == MODIFY_EXPR)
1630 op0 = TREE_OPERAND (stmt, 0);
1631 if (TREE_CODE (op0) != SSA_NAME)
1632 continue;
1633 if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (op0))
1634 continue;
1635 op1 = TREE_OPERAND (stmt, 1);
1636 if (TREE_CODE_CLASS (TREE_CODE (op1)) == 'c')
1638 add (value_table, op0, lookup_or_add (value_table, op1));
1639 insert_into_set (TMP_GEN (block), op0);
1640 value_insert_into_set (AVAIL_OUT (block), op0);
1642 else if (TREE_CODE_CLASS (TREE_CODE (op1)) == '2')
1644 tree bop1, bop2;
1645 tree val, val1, val2;
1646 tree newt;
1647 bop1 = TREE_OPERAND (op1, 0);
1648 bop2 = TREE_OPERAND (op1, 1);
1649 val1 = lookup_or_add (value_table, bop1);
1650 val2 = lookup_or_add (value_table, bop2);
1652 newt = pool_alloc (binary_node_pool);
1653 memcpy (newt, op1, tree_size (op1));
1654 TREE_OPERAND (newt, 0) = val1;
1655 TREE_OPERAND (newt, 1) = val2;
1656 val = lookup_or_add (value_table, newt);
1657 add (value_table, op0, val);
1658 if (!is_undefined_value (bop1))
1659 value_insert_into_set (EXP_GEN (block), bop1);
1660 if (!is_undefined_value (bop2))
1661 value_insert_into_set (EXP_GEN (block), bop2);
1662 value_insert_into_set (EXP_GEN (block), newt);
1663 insert_into_set (TMP_GEN (block), op0);
1664 value_insert_into_set (AVAIL_OUT (block), op0);
1666 else if (TREE_CODE_CLASS (TREE_CODE (op1)) == '1')
1668 tree uop;
1669 tree val, val1;
1670 tree newt;
1671 uop = TREE_OPERAND (op1, 0);
1672 val1 = lookup_or_add (value_table, uop);
1673 newt = pool_alloc (unary_node_pool);
1674 memcpy (newt, op1, tree_size (op1));
1675 TREE_OPERAND (newt, 0) = val1;
1676 val = lookup_or_add (value_table, newt);
1677 add (value_table, op0, val);
1678 if (!is_undefined_value (uop))
1679 value_insert_into_set (EXP_GEN (block), uop);
1680 value_insert_into_set (EXP_GEN (block), newt);
1681 insert_into_set (TMP_GEN (block), op0);
1682 value_insert_into_set (AVAIL_OUT (block), op0);
1684 else if (TREE_CODE (op1) == SSA_NAME)
1686 tree val = lookup_or_add (value_table, op1);
1687 add (value_table, op0, val);
1688 if (!is_undefined_value (op1))
1689 value_insert_into_set (EXP_GEN (block), op1);
1690 insert_into_set (TMP_GEN (block), op0);
1691 value_insert_into_set (AVAIL_OUT (block), op0);
1693 else
1695 size_t j;
1696 for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1698 tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1699 lookup_or_add (value_table, def);
1700 insert_into_set (TMP_GEN (block), def);
1701 value_insert_into_set (AVAIL_OUT (block), def);
1702 value_insert_into_set (AVAIL_OUT (block), op0);
1706 else
1708 size_t j;
1709 for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1711 tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1712 lookup_or_add (value_table, def);
1713 insert_into_set (TMP_GEN (block), def);
1714 value_insert_into_set (AVAIL_OUT (block), def);
1719 for (son = first_dom_son (CDI_DOMINATORS, block);
1720 son;
1721 son = next_dom_son (CDI_DOMINATORS, son))
1722 compute_avail (son);
1726 /* Eliminate fully redundant computations. */
1728 static void
1729 eliminate (void)
1731 basic_block b;
1733 FOR_EACH_BB (b)
1735 block_stmt_iterator i;
1737 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
1739 tree stmt = bsi_stmt (i);
1741 if (NUM_VUSES (STMT_VUSE_OPS (stmt))
1742 || NUM_V_MUST_DEFS (STMT_V_MUST_DEF_OPS (stmt))
1743 || NUM_V_MAY_DEFS (STMT_V_MAY_DEF_OPS (stmt))
1744 || stmt_ann (stmt)->has_volatile_ops)
1745 continue;
1746 /* Lookup the RHS of the expression, see if we have an
1747 available computation for it. If so, replace the RHS with
1748 the available computation. */
1749 if (TREE_CODE (stmt) == MODIFY_EXPR)
1751 tree t = TREE_OPERAND (stmt, 0);
1752 tree expr = TREE_OPERAND (stmt, 1);
1753 tree sprime;
1754 /* There is no point in eliminating NOP_EXPR, it isn't
1755 supposed to generate any code. */
1756 if (TREE_CODE (expr) == NOP_EXPR
1757 || (TREE_CODE_CLASS (TREE_CODE (expr)) != '2'
1758 && TREE_CODE_CLASS (TREE_CODE (expr)) != '1'))
1759 continue;
1760 sprime = find_leader (AVAIL_OUT (b),
1761 lookup (value_table, t));
1762 if (sprime
1763 && sprime != t
1764 && may_propagate_copy (sprime, TREE_OPERAND (stmt, 1)))
1766 if (dump_file && (dump_flags & TDF_DETAILS))
1768 fprintf (dump_file, "Replaced ");
1769 print_generic_expr (dump_file, expr, 0);
1770 fprintf (dump_file, " with ");
1771 print_generic_expr (dump_file, sprime, 0);
1772 fprintf (dump_file, " in ");
1773 print_generic_stmt (dump_file, stmt, 0);
1775 pre_stats.eliminations++;
1776 propagate_value (&TREE_OPERAND (stmt, 1), sprime);
1777 modify_stmt (stmt);
1785 /* Main entry point to the SSA-PRE pass.
1787 PHASE indicates which dump file from the DUMP_FILES array to use when
1788 dumping debugging information. */
1790 static void
1791 execute_pre (void)
1793 size_t tsize;
1794 basic_block bb;
1795 pre_uid = num_referenced_vars;
1796 memset (&pre_stats, 0, sizeof (pre_stats));
1797 FOR_ALL_BB (bb)
1799 bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
1801 phi_translate_table = htab_create (511, expr_pred_trans_hash,
1802 expr_pred_trans_eq,
1803 free);
1804 value_table = htab_create (511, val_expr_pair_hash,
1805 val_expr_pair_expr_eq, free);
1806 value_set_pool = create_alloc_pool ("Value sets",
1807 sizeof (struct value_set), 30);
1808 value_set_node_pool = create_alloc_pool ("Value set nodes",
1809 sizeof (struct value_set_node), 30);
1810 calculate_dominance_info (CDI_POST_DOMINATORS);
1811 calculate_dominance_info (CDI_DOMINATORS);
1812 tsize = tree_size (build (PLUS_EXPR, void_type_node, NULL_TREE,
1813 NULL_TREE));
1814 binary_node_pool = create_alloc_pool ("Binary tree nodes", tsize, 30);
1815 tsize = tree_size (build1 (NEGATE_EXPR, void_type_node, NULL_TREE));
1816 unary_node_pool = create_alloc_pool ("Unary tree nodes", tsize, 30);
1818 FOR_ALL_BB (bb)
1820 EXP_GEN (bb) = set_new (true);
1821 PHI_GEN (bb) = set_new (true);
1822 TMP_GEN (bb) = set_new (false);
1823 AVAIL_OUT (bb) = set_new (true);
1826 compute_avail (ENTRY_BLOCK_PTR);
1828 if (dump_file && (dump_flags & TDF_DETAILS))
1830 FOR_ALL_BB (bb)
1832 print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
1833 print_value_set (dump_file, TMP_GEN (bb), "tmp_gen", bb->index);
1834 print_value_set (dump_file, AVAIL_OUT (bb), "avail_out", bb->index);
1838 /* Insert can get quite slow on an incredibly large number of basic
1839 blocks due to some quadratic behavior. Until this behavior is
1840 fixed, don't run it when he have an incredibly large number of
1841 bb's. If we aren't going to run insert, there is no point in
1842 computing ANTIC, either, even though it's plenty fast. */
1844 if (n_basic_blocks < 4000)
1846 compute_antic ();
1848 insert ();
1850 eliminate ();
1852 if (dump_file && (dump_flags & TDF_STATS))
1854 fprintf (dump_file, "Insertions:%d\n", pre_stats.insertions);
1855 fprintf (dump_file, "New PHIs:%d\n", pre_stats.phis);
1856 fprintf (dump_file, "Eliminated:%d\n", pre_stats.eliminations);
1859 free_alloc_pool (value_set_pool);
1860 free_alloc_pool (value_set_node_pool);
1861 free_alloc_pool (binary_node_pool);
1862 free_alloc_pool (unary_node_pool);
1863 htab_delete (value_table);
1864 htab_delete (phi_translate_table);
1866 FOR_ALL_BB (bb)
1868 free (bb->aux);
1869 bb->aux = NULL;
1871 free_dominance_info (CDI_POST_DOMINATORS);
1874 static bool
1875 gate_pre (void)
1877 return flag_tree_pre != 0;
1880 struct tree_opt_pass pass_pre =
1882 "pre", /* name */
1883 gate_pre, /* gate */
1884 execute_pre, /* execute */
1885 NULL, /* sub */
1886 NULL, /* next */
1887 0, /* static_pass_number */
1888 TV_TREE_PRE, /* tv_id */
1889 PROP_no_crit_edges | PROP_cfg | PROP_ssa,/* properties_required */
1890 0, /* properties_provided */
1891 0, /* properties_destroyed */
1892 0, /* todo_flags_start */
1893 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */