PR target/16201
[official-gcc.git] / gcc / tree-ssa-pre.c
blobb9e1510e05df3a6cd257758c5fb545271585f995
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>
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. */
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"
48 /* TODO:
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 */
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. */
68 /* Basic algorithm
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.
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.
95 Third, we perform insertions to make partially redundant
96 expressions fully redundant.
98 An expression is partially redundant (excluding partial
99 anticipation) if:
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.
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.
109 Fourth, we eliminate fully redundant expressions.
110 This is a simple statement walk that replaces redundant
111 calculations with the now available values. */
113 /* Representations of value numbers:
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.
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.
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. */
135 /* Representation of expressions on value numbers:
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.
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.
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).*/
159 /* Representation of sets:
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.
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.
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/values. */
183 typedef struct value_set_node
185 /* An expression. */
186 tree expr;
188 /* A pointer to the next element of the value set. */
189 struct value_set_node *next;
190 } *value_set_node_t;
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
198 /* The head of the list. Used for iterating over the list in
199 order. */
200 value_set_node_t head;
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;
207 /* The length of the list. */
208 size_t length;
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;
215 /* The bitmap of values that exist in the set. May be NULL in an
216 empty or non-indexed set. */
217 bitmap values;
219 } *value_set_t;
222 /* An unordered bitmap set. One bitmap tracks values, the other,
223 expressions. */
224 typedef struct bitmap_set
226 bitmap expressions;
227 bitmap values;
228 } *bitmap_set_t;
230 /* Sets that we need to keep track of. */
231 typedef struct bb_value_sets
233 /* The EXP_GEN set, which represents expressions/values generated in
234 a basic block. */
235 value_set_t exp_gen;
237 /* The PHI_GEN set, which represents PHI results generated in a
238 basic block. */
239 bitmap_set_t phi_gen;
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;
245 /* The AVAIL_OUT set, which represents which values are available in
246 a given basic block. */
247 bitmap_set_t avail_out;
249 /* The ANTIC_IN set, which represents which values are anticiptable
250 in a given basic block. */
251 value_set_t antic_in;
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;
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
266 /* This structure is used to keep track of statistics on what
267 optimization PRE was able to perform. */
268 static struct
270 /* The number of RHS computations eliminated by PRE. */
271 int eliminations;
273 /* The number of new expressions/temporaries generated by PRE. */
274 int insertions;
276 /* The number of new PHI nodes added by PRE. */
277 int phis;
279 /* The number of values found constant. */
280 int constified;
282 } pre_stats;
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);
299 /* We can add and remove elements and entries to and from sets
300 and hash tables, so we use alloc pools for them. */
302 static alloc_pool value_set_pool;
303 static alloc_pool bitmap_set_pool;
304 static alloc_pool value_set_node_pool;
305 static alloc_pool binary_node_pool;
306 static alloc_pool unary_node_pool;
307 static alloc_pool reference_node_pool;
308 static bitmap_obstack grand_bitmap_obstack;
310 /* Set of blocks with statements that have had its EH information
311 cleaned up. */
312 static bitmap need_eh_cleanup;
314 /* The phi_translate_table caches phi translations for a given
315 expression and predecessor. */
317 static htab_t phi_translate_table;
319 /* A three tuple {e, pred, v} used to cache phi translations in the
320 phi_translate_table. */
322 typedef struct expr_pred_trans_d
324 /* The expression. */
325 tree e;
327 /* The predecessor block along which we translated the expression. */
328 basic_block pred;
330 /* The value that resulted from the translation. */
331 tree v;
333 /* The hashcode for the expression, pred pair. This is cached for
334 speed reasons. */
335 hashval_t hashcode;
336 } *expr_pred_trans_t;
338 /* Return the hash value for a phi translation table entry. */
340 static hashval_t
341 expr_pred_trans_hash (const void *p)
343 const expr_pred_trans_t ve = (expr_pred_trans_t) p;
344 return ve->hashcode;
347 /* Return true if two phi translation table entries are the same.
348 P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
350 static int
351 expr_pred_trans_eq (const void *p1, const void *p2)
353 const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
354 const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
355 basic_block b1 = ve1->pred;
356 basic_block b2 = ve2->pred;
359 /* If they are not translations for the same basic block, they can't
360 be equal. */
361 if (b1 != b2)
362 return false;
364 /* If they are for the same basic block, determine if the
365 expressions are equal. */
366 if (expressions_equal_p (ve1->e, ve2->e))
367 return true;
369 return false;
372 /* Search in the phi translation table for the translation of
373 expression E in basic block PRED. Return the translated value, if
374 found, NULL otherwise. */
376 static inline tree
377 phi_trans_lookup (tree e, basic_block pred)
379 void **slot;
380 struct expr_pred_trans_d ept;
381 ept.e = e;
382 ept.pred = pred;
383 ept.hashcode = vn_compute (e, (unsigned long) pred, NULL);
384 slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
385 NO_INSERT);
386 if (!slot)
387 return NULL;
388 else
389 return ((expr_pred_trans_t) *slot)->v;
393 /* Add the tuple mapping from {expression E, basic block PRED} to
394 value V, to the phi translation table. */
396 static inline void
397 phi_trans_add (tree e, tree v, basic_block pred)
399 void **slot;
400 expr_pred_trans_t new_pair = xmalloc (sizeof (*new_pair));
401 new_pair->e = e;
402 new_pair->pred = pred;
403 new_pair->v = v;
404 new_pair->hashcode = vn_compute (e, (unsigned long) pred, NULL);
405 slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
406 new_pair->hashcode, INSERT);
407 if (*slot)
408 free (*slot);
409 *slot = (void *) new_pair;
413 /* Add expression E to the expression set of value V. */
415 void
416 add_to_value (tree v, tree e)
418 /* Constants have no expression sets. */
419 if (is_gimple_min_invariant (v))
420 return;
422 if (VALUE_HANDLE_EXPR_SET (v) == NULL)
423 VALUE_HANDLE_EXPR_SET (v) = set_new (false);
425 insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
429 /* Return true if value V exists in the bitmap for SET. */
431 static inline bool
432 value_exists_in_set_bitmap (value_set_t set, tree v)
434 if (!set->values)
435 return false;
437 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (v));
441 /* Remove value V from the bitmap for SET. */
443 static void
444 value_remove_from_set_bitmap (value_set_t set, tree v)
446 gcc_assert (set->indexed);
448 if (!set->values)
449 return;
451 bitmap_clear_bit (set->values, VALUE_HANDLE_ID (v));
455 /* Insert the value number V into the bitmap of values existing in
456 SET. */
458 static inline void
459 value_insert_into_set_bitmap (value_set_t set, tree v)
461 gcc_assert (set->indexed);
463 if (set->values == NULL)
464 set->values = BITMAP_ALLOC (&grand_bitmap_obstack);
466 bitmap_set_bit (set->values, VALUE_HANDLE_ID (v));
470 /* Create a new bitmap set and return it. */
472 static bitmap_set_t
473 bitmap_set_new (void)
475 bitmap_set_t ret = pool_alloc (bitmap_set_pool);
476 ret->expressions = BITMAP_ALLOC (&grand_bitmap_obstack);
477 ret->values = BITMAP_ALLOC (&grand_bitmap_obstack);
478 return ret;
481 /* Create a new set. */
483 static value_set_t
484 set_new (bool indexed)
486 value_set_t ret;
487 ret = pool_alloc (value_set_pool);
488 ret->head = ret->tail = NULL;
489 ret->length = 0;
490 ret->indexed = indexed;
491 ret->values = NULL;
492 return ret;
495 /* Insert an expression EXPR into a bitmapped set. */
497 static void
498 bitmap_insert_into_set (bitmap_set_t set, tree expr)
500 tree val;
501 /* XXX: For now, we only let SSA_NAMES into the bitmap sets. */
502 gcc_assert (TREE_CODE (expr) == SSA_NAME);
503 val = get_value_handle (expr);
505 gcc_assert (val);
506 if (!is_gimple_min_invariant (val))
508 bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
509 bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
513 /* Insert EXPR into SET. */
515 static void
516 insert_into_set (value_set_t set, tree expr)
518 value_set_node_t newnode = pool_alloc (value_set_node_pool);
519 tree val = get_value_handle (expr);
520 gcc_assert (val);
522 if (is_gimple_min_invariant (val))
523 return;
525 /* For indexed sets, insert the value into the set value bitmap.
526 For all sets, add it to the linked list and increment the list
527 length. */
528 if (set->indexed)
529 value_insert_into_set_bitmap (set, val);
531 newnode->next = NULL;
532 newnode->expr = expr;
533 set->length ++;
534 if (set->head == NULL)
536 set->head = set->tail = newnode;
538 else
540 set->tail->next = newnode;
541 set->tail = newnode;
545 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
547 static void
548 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
550 bitmap_copy (dest->expressions, orig->expressions);
551 bitmap_copy (dest->values, orig->values);
554 /* Copy the set ORIG to the set DEST. */
556 static void
557 set_copy (value_set_t dest, value_set_t orig)
559 value_set_node_t node;
561 if (!orig || !orig->head)
562 return;
564 for (node = orig->head;
565 node;
566 node = node->next)
568 insert_into_set (dest, node->expr);
572 /* Remove EXPR from SET. */
574 static void
575 set_remove (value_set_t set, tree expr)
577 value_set_node_t node, prev;
579 /* Remove the value of EXPR from the bitmap, decrement the set
580 length, and remove it from the actual double linked list. */
581 value_remove_from_set_bitmap (set, get_value_handle (expr));
582 set->length--;
583 prev = NULL;
584 for (node = set->head;
585 node != NULL;
586 prev = node, node = node->next)
588 if (node->expr == expr)
590 if (prev == NULL)
591 set->head = node->next;
592 else
593 prev->next= node->next;
595 if (node == set->tail)
596 set->tail = prev;
597 pool_free (value_set_node_pool, node);
598 return;
603 /* Return true if SET contains the value VAL. */
605 static bool
606 set_contains_value (value_set_t set, tree val)
608 /* All constants are in every set. */
609 if (is_gimple_min_invariant (val))
610 return true;
612 if (set->length == 0)
613 return false;
615 return value_exists_in_set_bitmap (set, val);
618 /* Return true if bitmapped set SET contains the expression EXPR. */
619 static bool
620 bitmap_set_contains (bitmap_set_t set, tree expr)
622 /* All constants are in every set. */
623 if (is_gimple_min_invariant (get_value_handle (expr)))
624 return true;
626 /* XXX: Bitmapped sets only contain SSA_NAME's for now. */
627 if (TREE_CODE (expr) != SSA_NAME)
628 return false;
629 return bitmap_bit_p (set->expressions, SSA_NAME_VERSION (expr));
633 /* Return true if bitmapped set SET contains the value VAL. */
635 static bool
636 bitmap_set_contains_value (bitmap_set_t set, tree val)
638 if (is_gimple_min_invariant (val))
639 return true;
640 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
643 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
645 static void
646 bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
648 value_set_t exprset;
649 value_set_node_t node;
650 if (is_gimple_min_invariant (lookfor))
651 return;
652 if (!bitmap_set_contains_value (set, lookfor))
653 return;
655 /* The number of expressions having a given value is usually
656 significantly less than the total number of expressions in SET.
657 Thus, rather than check, for each expression in SET, whether it
658 has the value LOOKFOR, we walk the reverse mapping that tells us
659 what expressions have a given value, and see if any of those
660 expressions are in our set. For large testcases, this is about
661 5-10x faster than walking the bitmap. If this is somehow a
662 significant lose for some cases, we can choose which set to walk
663 based on the set size. */
664 exprset = VALUE_HANDLE_EXPR_SET (lookfor);
665 for (node = exprset->head; node; node = node->next)
667 if (TREE_CODE (node->expr) == SSA_NAME)
669 if (bitmap_bit_p (set->expressions, SSA_NAME_VERSION (node->expr)))
671 bitmap_clear_bit (set->expressions, SSA_NAME_VERSION (node->expr));
672 bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
673 return;
679 /* Subtract bitmapped set B from value set A, and return the new set. */
681 static value_set_t
682 bitmap_set_subtract_from_value_set (value_set_t a, bitmap_set_t b,
683 bool indexed)
685 value_set_t ret = set_new (indexed);
686 value_set_node_t node;
687 for (node = a->head;
688 node;
689 node = node->next)
691 if (!bitmap_set_contains (b, node->expr))
692 insert_into_set (ret, node->expr);
694 return ret;
697 /* Return true if two sets are equal. */
699 static bool
700 set_equal (value_set_t a, value_set_t b)
702 value_set_node_t node;
704 if (a->length != b->length)
705 return false;
706 for (node = a->head;
707 node;
708 node = node->next)
710 if (!set_contains_value (b, get_value_handle (node->expr)))
711 return false;
713 return true;
716 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
717 and add it otherwise. */
719 static void
720 bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
722 tree val = get_value_handle (expr);
723 if (bitmap_set_contains_value (set, val))
724 bitmap_set_replace_value (set, val, expr);
725 else
726 bitmap_insert_into_set (set, expr);
729 /* Insert EXPR into SET if EXPR's value is not already present in
730 SET. */
732 static void
733 bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
735 tree val = get_value_handle (expr);
737 if (is_gimple_min_invariant (val))
738 return;
740 if (!bitmap_set_contains_value (set, val))
741 bitmap_insert_into_set (set, expr);
744 /* Insert the value for EXPR into SET, if it doesn't exist already. */
746 static void
747 value_insert_into_set (value_set_t set, tree expr)
749 tree val = get_value_handle (expr);
751 /* Constant and invariant values exist everywhere, and thus,
752 actually keeping them in the sets is pointless. */
753 if (is_gimple_min_invariant (val))
754 return;
756 if (!set_contains_value (set, val))
757 insert_into_set (set, expr);
761 /* Print out SET to OUTFILE. */
763 static void
764 bitmap_print_value_set (FILE *outfile, bitmap_set_t set,
765 const char *setname, int blockindex)
767 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
768 if (set)
770 bool first = true;
771 unsigned i;
772 bitmap_iterator bi;
774 EXECUTE_IF_SET_IN_BITMAP (set->expressions, 0, i, bi)
776 if (!first)
777 fprintf (outfile, ", ");
778 first = false;
779 print_generic_expr (outfile, ssa_name (i), 0);
781 fprintf (outfile, " (");
782 print_generic_expr (outfile, get_value_handle (ssa_name (i)), 0);
783 fprintf (outfile, ") ");
786 fprintf (outfile, " }\n");
788 /* Print out the value_set SET to OUTFILE. */
790 static void
791 print_value_set (FILE *outfile, value_set_t set,
792 const char *setname, int blockindex)
794 value_set_node_t node;
795 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
796 if (set)
798 for (node = set->head;
799 node;
800 node = node->next)
802 print_generic_expr (outfile, node->expr, 0);
804 fprintf (outfile, " (");
805 print_generic_expr (outfile, get_value_handle (node->expr), 0);
806 fprintf (outfile, ") ");
808 if (node->next)
809 fprintf (outfile, ", ");
813 fprintf (outfile, " }\n");
816 /* Print out the expressions that have VAL to OUTFILE. */
818 void
819 print_value_expressions (FILE *outfile, tree val)
821 if (VALUE_HANDLE_EXPR_SET (val))
823 char s[10];
824 sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
825 print_value_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
830 void
831 debug_value_expressions (tree val)
833 print_value_expressions (stderr, val);
837 void debug_value_set (value_set_t, const char *, int);
839 void
840 debug_value_set (value_set_t set, const char *setname, int blockindex)
842 print_value_set (stderr, set, setname, blockindex);
845 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
846 the phis in PRED. Return NULL if we can't find a leader for each
847 part of the translated expression. */
849 static tree
850 phi_translate (tree expr, value_set_t set, basic_block pred,
851 basic_block phiblock)
853 tree phitrans = NULL;
854 tree oldexpr = expr;
856 if (expr == NULL)
857 return NULL;
859 if (is_gimple_min_invariant (expr))
860 return expr;
862 /* Phi translations of a given expression don't change. */
863 phitrans = phi_trans_lookup (expr, pred);
864 if (phitrans)
865 return phitrans;
867 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
869 case tcc_reference:
870 /* XXX: Until we have PRE of loads working, none will be ANTIC. */
871 return NULL;
873 case tcc_binary:
875 tree oldop1 = TREE_OPERAND (expr, 0);
876 tree oldop2 = TREE_OPERAND (expr, 1);
877 tree newop1;
878 tree newop2;
879 tree newexpr;
881 newop1 = phi_translate (find_leader (set, oldop1),
882 set, pred, phiblock);
883 if (newop1 == NULL)
884 return NULL;
885 newop2 = phi_translate (find_leader (set, oldop2),
886 set, pred, phiblock);
887 if (newop2 == NULL)
888 return NULL;
889 if (newop1 != oldop1 || newop2 != oldop2)
891 newexpr = pool_alloc (binary_node_pool);
892 memcpy (newexpr, expr, tree_size (expr));
893 create_tree_ann (newexpr);
894 TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
895 TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
896 vn_lookup_or_add (newexpr, NULL);
897 expr = newexpr;
898 phi_trans_add (oldexpr, newexpr, pred);
901 return expr;
903 case tcc_unary:
905 tree oldop1 = TREE_OPERAND (expr, 0);
906 tree newop1;
907 tree newexpr;
909 newop1 = phi_translate (find_leader (set, oldop1),
910 set, pred, phiblock);
911 if (newop1 == NULL)
912 return NULL;
913 if (newop1 != oldop1)
915 newexpr = pool_alloc (unary_node_pool);
916 memcpy (newexpr, expr, tree_size (expr));
917 create_tree_ann (newexpr);
918 TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
919 vn_lookup_or_add (newexpr, NULL);
920 expr = newexpr;
921 phi_trans_add (oldexpr, newexpr, pred);
924 return expr;
926 case tcc_exceptional:
928 tree phi = NULL;
929 edge e;
930 gcc_assert (TREE_CODE (expr) == SSA_NAME);
931 if (TREE_CODE (SSA_NAME_DEF_STMT (expr)) == PHI_NODE)
932 phi = SSA_NAME_DEF_STMT (expr);
933 else
934 return expr;
936 e = find_edge (pred, bb_for_stmt (phi));
937 if (e)
939 if (is_undefined_value (PHI_ARG_DEF (phi, e->dest_idx)))
940 return NULL;
941 vn_lookup_or_add (PHI_ARG_DEF (phi, e->dest_idx), NULL);
942 return PHI_ARG_DEF (phi, e->dest_idx);
945 return expr;
947 default:
948 gcc_unreachable ();
952 static void
953 phi_translate_set (value_set_t dest, value_set_t set, basic_block pred,
954 basic_block phiblock)
956 value_set_node_t node;
957 for (node = set->head;
958 node;
959 node = node->next)
961 tree translated;
962 translated = phi_translate (node->expr, set, pred, phiblock);
963 phi_trans_add (node->expr, translated, pred);
965 if (translated != NULL)
966 value_insert_into_set (dest, translated);
970 /* Find the leader for a value (i.e., the name representing that
971 value) in a given set, and return it. Return NULL if no leader is
972 found. */
974 static tree
975 bitmap_find_leader (bitmap_set_t set, tree val)
977 if (val == NULL)
978 return NULL;
980 if (is_gimple_min_invariant (val))
981 return val;
982 if (bitmap_set_contains_value (set, val))
984 /* Rather than walk the entire bitmap of expressions, and see
985 whether any of them has the value we are looking for, we look
986 at the reverse mapping, which tells us the set of expressions
987 that have a given value (IE value->expressions with that
988 value) and see if any of those expressions are in our set.
989 The number of expressions per value is usually significantly
990 less than the number of expressions in the set. In fact, for
991 large testcases, doing it this way is roughly 5-10x faster
992 than walking the bitmap.
993 If this is somehow a significant lose for some cases, we can
994 choose which set to walk based on which set is smaller. */
995 value_set_t exprset;
996 value_set_node_t node;
997 exprset = VALUE_HANDLE_EXPR_SET (val);
998 for (node = exprset->head; node; node = node->next)
1000 if (TREE_CODE (node->expr) == SSA_NAME)
1002 if (bitmap_bit_p (set->expressions,
1003 SSA_NAME_VERSION (node->expr)))
1004 return node->expr;
1008 return NULL;
1012 /* Find the leader for a value (i.e., the name representing that
1013 value) in a given set, and return it. Return NULL if no leader is
1014 found. */
1016 static tree
1017 find_leader (value_set_t set, tree val)
1019 value_set_node_t node;
1021 if (val == NULL)
1022 return NULL;
1024 /* Constants represent themselves. */
1025 if (is_gimple_min_invariant (val))
1026 return val;
1028 if (set->length == 0)
1029 return NULL;
1031 if (value_exists_in_set_bitmap (set, val))
1033 for (node = set->head;
1034 node;
1035 node = node->next)
1037 if (get_value_handle (node->expr) == val)
1038 return node->expr;
1042 return NULL;
1045 /* Determine if the expression EXPR is valid in SET. This means that
1046 we have a leader for each part of the expression (if it consists of
1047 values), or the expression is an SSA_NAME.
1049 NB: We never should run into a case where we have SSA_NAME +
1050 SSA_NAME or SSA_NAME + value. The sets valid_in_set is called on,
1051 the ANTIC sets, will only ever have SSA_NAME's or binary value
1052 expression (IE VALUE1 + VALUE2) */
1054 static bool
1055 valid_in_set (value_set_t set, tree expr)
1057 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1059 case tcc_binary:
1061 tree op1 = TREE_OPERAND (expr, 0);
1062 tree op2 = TREE_OPERAND (expr, 1);
1063 return set_contains_value (set, op1) && set_contains_value (set, op2);
1066 case tcc_unary:
1068 tree op1 = TREE_OPERAND (expr, 0);
1069 return set_contains_value (set, op1);
1072 case tcc_reference:
1073 /* XXX: Until PRE of loads works, no reference nodes are ANTIC. */
1074 return false;
1076 case tcc_exceptional:
1077 gcc_assert (TREE_CODE (expr) == SSA_NAME);
1078 return true;
1080 default:
1081 /* No other cases should be encountered. */
1082 gcc_unreachable ();
1086 /* Clean the set of expressions that are no longer valid in SET. This
1087 means expressions that are made up of values we have no leaders for
1088 in SET. */
1090 static void
1091 clean (value_set_t set)
1093 value_set_node_t node;
1094 value_set_node_t next;
1095 node = set->head;
1096 while (node)
1098 next = node->next;
1099 if (!valid_in_set (set, node->expr))
1100 set_remove (set, node->expr);
1101 node = next;
1105 DEF_VEC_MALLOC_P (basic_block);
1107 /* Compute the ANTIC set for BLOCK.
1109 If succs(BLOCK) > 1 then
1110 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
1111 else if succs(BLOCK) == 1 then
1112 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
1114 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
1116 XXX: It would be nice to either write a set_clear, and use it for
1117 ANTIC_OUT, or to mark the antic_out set as deleted at the end
1118 of this routine, so that the pool can hand the same memory back out
1119 again for the next ANTIC_OUT. */
1121 static bool
1122 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
1124 bool changed = false;
1125 value_set_t S, old, ANTIC_OUT;
1126 value_set_node_t node;
1128 ANTIC_OUT = S = NULL;
1130 /* If any edges from predecessors are abnormal, antic_in is empty,
1131 so do nothing. */
1132 if (block_has_abnormal_pred_edge)
1133 goto maybe_dump_sets;
1135 old = set_new (false);
1136 set_copy (old, ANTIC_IN (block));
1137 ANTIC_OUT = set_new (true);
1139 /* If the block has no successors, ANTIC_OUT is empty. */
1140 if (EDGE_COUNT (block->succs) == 0)
1142 /* If we have one successor, we could have some phi nodes to
1143 translate through. */
1144 else if (EDGE_COUNT (block->succs) == 1)
1146 phi_translate_set (ANTIC_OUT, ANTIC_IN(EDGE_SUCC (block, 0)->dest),
1147 block, EDGE_SUCC (block, 0)->dest);
1149 /* If we have multiple successors, we take the intersection of all of
1150 them. */
1151 else
1153 VEC (basic_block) * worklist;
1154 edge e;
1155 size_t i;
1156 basic_block bprime, first;
1157 edge_iterator ei;
1159 worklist = VEC_alloc (basic_block, 2);
1160 FOR_EACH_EDGE (e, ei, block->succs)
1161 VEC_safe_push (basic_block, worklist, e->dest);
1162 first = VEC_index (basic_block, worklist, 0);
1163 set_copy (ANTIC_OUT, ANTIC_IN (first));
1165 for (i = 1; VEC_iterate (basic_block, worklist, i, bprime); i++)
1167 node = ANTIC_OUT->head;
1168 while (node)
1170 tree val;
1171 value_set_node_t next = node->next;
1172 val = get_value_handle (node->expr);
1173 if (!set_contains_value (ANTIC_IN (bprime), val))
1174 set_remove (ANTIC_OUT, node->expr);
1175 node = next;
1178 VEC_free (basic_block, worklist);
1181 /* Generate ANTIC_OUT - TMP_GEN. */
1182 S = bitmap_set_subtract_from_value_set (ANTIC_OUT, TMP_GEN (block), false);
1184 /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1185 ANTIC_IN (block) = bitmap_set_subtract_from_value_set (EXP_GEN (block),
1186 TMP_GEN (block),
1187 true);
1189 /* Then union in the ANTIC_OUT - TMP_GEN values,
1190 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
1191 for (node = S->head; node; node = node->next)
1192 value_insert_into_set (ANTIC_IN (block), node->expr);
1194 clean (ANTIC_IN (block));
1195 if (!set_equal (old, ANTIC_IN (block)))
1196 changed = true;
1198 maybe_dump_sets:
1199 if (dump_file && (dump_flags & TDF_DETAILS))
1201 if (ANTIC_OUT)
1202 print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1203 print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1204 if (S)
1205 print_value_set (dump_file, S, "S", block->index);
1208 return changed;
1211 /* Compute ANTIC sets. Iterates until fixpointed. */
1213 static void
1214 compute_antic (void)
1216 bool changed= true;
1217 int num_iterations = 0;
1218 basic_block block, *worklist;
1219 size_t sp = 0;
1220 sbitmap has_abnormal_preds;
1222 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
1223 We pre-build the map of blocks with incoming abnormal edges here. */
1224 has_abnormal_preds = sbitmap_alloc (last_basic_block);
1225 sbitmap_zero (has_abnormal_preds);
1226 FOR_EACH_BB (block)
1228 edge_iterator ei;
1229 edge e;
1231 FOR_EACH_EDGE (e, ei, block->preds)
1232 if (e->flags & EDGE_ABNORMAL)
1234 SET_BIT (has_abnormal_preds, block->index);
1235 break;
1238 /* While we are here, give empty ANTIC_IN sets to each block. */
1239 ANTIC_IN (block) = set_new (true);
1241 /* At the exit block we anticipate nothing. */
1242 ANTIC_IN (EXIT_BLOCK_PTR) = set_new (true);
1244 /* Allocate the worklist. */
1245 worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
1247 /* Loop until fixpointed. */
1248 while (changed)
1250 basic_block son, bb;
1252 changed = false;
1253 num_iterations++;
1255 /* Seed the algorithm by putting post-dominator children of
1256 the exit block in the worklist. */
1257 for (son = first_dom_son (CDI_POST_DOMINATORS, EXIT_BLOCK_PTR);
1258 son;
1259 son = next_dom_son (CDI_POST_DOMINATORS, son))
1260 worklist[sp++] = son;
1262 /* Now visit all blocks in a DFS of the post dominator tree. */
1263 while (sp)
1265 bool bb_has_abnormal_pred;
1267 bb = worklist[--sp];
1268 bb_has_abnormal_pred = TEST_BIT (has_abnormal_preds, bb->index);
1269 changed |= compute_antic_aux (bb, bb_has_abnormal_pred);
1271 for (son = first_dom_son (CDI_POST_DOMINATORS, bb);
1272 son;
1273 son = next_dom_son (CDI_POST_DOMINATORS, son))
1274 worklist[sp++] = son;
1278 free (worklist);
1279 sbitmap_free (has_abnormal_preds);
1281 if (dump_file && (dump_flags & TDF_STATS))
1282 fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1285 static VEC(tree_on_heap) *inserted_exprs;
1286 /* Find a leader for an expression, or generate one using
1287 create_expression_by_pieces if it's ANTIC but
1288 complex.
1289 BLOCK is the basic_block we are looking for leaders in.
1290 EXPR is the expression to find a leader or generate for.
1291 STMTS is the statement list to put the inserted expressions on.
1292 Returns the SSA_NAME of the LHS of the generated expression or the
1293 leader. */
1295 static tree
1296 find_or_generate_expression (basic_block block, tree expr, tree stmts)
1298 tree genop = bitmap_find_leader (AVAIL_OUT (block), expr);
1300 /* If it's still NULL, see if it is a complex expression, and if
1301 so, generate it recursively, otherwise, abort, because it's
1302 not really . */
1303 if (genop == NULL)
1305 genop = VALUE_HANDLE_EXPR_SET (expr)->head->expr;
1306 gcc_assert (UNARY_CLASS_P (genop)
1307 || BINARY_CLASS_P (genop)
1308 || REFERENCE_CLASS_P (genop));
1309 genop = create_expression_by_pieces (block, genop, stmts);
1311 return genop;
1314 #define NECESSARY(stmt) stmt->common.asm_written_flag
1315 /* Create an expression in pieces, so that we can handle very complex
1316 expressions that may be ANTIC, but not necessary GIMPLE.
1317 BLOCK is the basic block the expression will be inserted into,
1318 EXPR is the expression to insert (in value form)
1319 STMTS is a statement list to append the necessary insertions into.
1321 This function will abort if we hit some value that shouldn't be
1322 ANTIC but is (IE there is no leader for it, or its components).
1323 This function may also generate expressions that are themselves
1324 partially or fully redundant. Those that are will be either made
1325 fully redundant during the next iteration of insert (for partially
1326 redundant ones), or eliminated by eliminate (for fully redundant
1327 ones). */
1329 static tree
1330 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
1332 tree name = NULL_TREE;
1333 tree newexpr = NULL_TREE;
1334 tree v;
1336 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1338 case tcc_binary:
1340 tree_stmt_iterator tsi;
1341 tree genop1, genop2;
1342 tree temp;
1343 tree op1 = TREE_OPERAND (expr, 0);
1344 tree op2 = TREE_OPERAND (expr, 1);
1345 genop1 = find_or_generate_expression (block, op1, stmts);
1346 genop2 = find_or_generate_expression (block, op2, stmts);
1347 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1348 add_referenced_tmp_var (temp);
1349 newexpr = fold (build (TREE_CODE (expr), TREE_TYPE (expr),
1350 genop1, genop2));
1351 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1352 temp, newexpr);
1353 NECESSARY (newexpr) = 0;
1354 name = make_ssa_name (temp, newexpr);
1355 TREE_OPERAND (newexpr, 0) = name;
1356 tsi = tsi_last (stmts);
1357 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1358 VEC_safe_push (tree_on_heap, inserted_exprs, newexpr);
1359 pre_stats.insertions++;
1360 break;
1362 case tcc_unary:
1364 tree_stmt_iterator tsi;
1365 tree genop1;
1366 tree temp;
1367 tree op1 = TREE_OPERAND (expr, 0);
1368 genop1 = find_or_generate_expression (block, op1, stmts);
1369 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1370 add_referenced_tmp_var (temp);
1371 newexpr = fold (build (TREE_CODE (expr), TREE_TYPE (expr),
1372 genop1));
1373 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1374 temp, newexpr);
1375 name = make_ssa_name (temp, newexpr);
1376 TREE_OPERAND (newexpr, 0) = name;
1377 NECESSARY (newexpr) = 0;
1378 tsi = tsi_last (stmts);
1379 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1380 VEC_safe_push (tree_on_heap, inserted_exprs, newexpr);
1381 pre_stats.insertions++;
1383 break;
1385 default:
1386 gcc_unreachable ();
1389 v = get_value_handle (expr);
1390 vn_add (name, v, NULL);
1392 /* The value may already exist in either NEW_SETS, or AVAIL_OUT, because
1393 we are creating the expression by pieces, and this particular piece of
1394 the expression may have been represented. There is no harm in replacing
1395 here. */
1396 bitmap_value_replace_in_set (NEW_SETS (block), name);
1397 bitmap_value_replace_in_set (AVAIL_OUT (block), name);
1398 if (dump_file && (dump_flags & TDF_DETAILS))
1400 fprintf (dump_file, "Inserted ");
1401 print_generic_expr (dump_file, newexpr, 0);
1402 fprintf (dump_file, " in predecessor %d\n", block->index);
1404 return name;
1407 /* Return the folded version of T if T, when folded, is a gimple
1408 min_invariant. Otherwise, return T. */
1410 static tree
1411 fully_constant_expression (tree t)
1413 tree folded;
1414 folded = fold (t);
1415 if (folded && is_gimple_min_invariant (folded))
1416 return folded;
1417 return t;
1420 /* Insert the to-be-made-available values of NODE for each predecessor, stored
1421 in AVAIL, into the predecessors of BLOCK, and merge the result with a phi
1422 node, given the same value handle as NODE. The prefix of the phi node is
1423 given with TMPNAME. Return true if we have inserted new stuff. */
1425 static bool
1426 insert_into_preds_of_block (basic_block block, value_set_node_t node,
1427 tree *avail, const char *tmpname)
1429 tree val = get_value_handle (node->expr);
1430 edge pred;
1431 bool insertions = false;
1432 bool nophi = false;
1433 basic_block bprime;
1434 tree eprime;
1435 edge_iterator ei;
1436 tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
1437 tree temp;
1439 if (dump_file && (dump_flags & TDF_DETAILS))
1441 fprintf (dump_file, "Found partial redundancy for expression ");
1442 print_generic_expr (dump_file, node->expr, 0);
1443 fprintf (dump_file, "\n");
1446 /* Make sure we aren't creating an induction variable. */
1447 if (block->loop_depth > 0 && EDGE_COUNT (block->preds) == 2)
1449 bool firstinsideloop = false;
1450 bool secondinsideloop = false;
1451 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
1452 EDGE_PRED (block, 0)->src);
1453 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
1454 EDGE_PRED (block, 1)->src);
1455 /* Induction variables only have one edge inside the loop. */
1456 if (firstinsideloop ^ secondinsideloop)
1458 if (dump_file && (dump_flags & TDF_DETAILS))
1459 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
1460 nophi = true;
1465 /* Make the necessary insertions. */
1466 FOR_EACH_EDGE (pred, ei, block->preds)
1468 tree stmts = alloc_stmt_list ();
1469 tree builtexpr;
1470 bprime = pred->src;
1471 eprime = avail[bprime->index];
1472 if (BINARY_CLASS_P (eprime)
1473 || UNARY_CLASS_P (eprime))
1475 builtexpr = create_expression_by_pieces (bprime,
1476 eprime,
1477 stmts);
1478 bsi_insert_on_edge (pred, stmts);
1479 avail[bprime->index] = builtexpr;
1480 insertions = true;
1483 /* If we didn't want a phi node, and we made insertions, we still have
1484 inserted new stuff, and thus return true. If we didn't want a phi node,
1485 and didn't make insertions, we haven't added anything new, so return
1486 false. */
1487 if (nophi && insertions)
1488 return true;
1489 else if (nophi && !insertions)
1490 return false;
1492 /* Now build a phi for the new variable. */
1493 temp = create_tmp_var (type, tmpname);
1494 add_referenced_tmp_var (temp);
1495 temp = create_phi_node (temp, block);
1496 NECESSARY (temp) = 0;
1497 VEC_safe_push (tree_on_heap, inserted_exprs, temp);
1498 FOR_EACH_EDGE (pred, ei, block->preds)
1499 add_phi_arg (temp, avail[pred->src->index], pred);
1501 vn_add (PHI_RESULT (temp), val, NULL);
1503 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
1504 this insertion, since we test for the existence of this value in PHI_GEN
1505 before proceeding with the partial redundancy checks in insert_aux.
1507 The value may exist in AVAIL_OUT, in particular, it could be represented
1508 by the expression we are trying to eliminate, in which case we want the
1509 replacement to occur. If it's not existing in AVAIL_OUT, we want it
1510 inserted there.
1512 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
1513 this block, because if it did, it would have existed in our dominator's
1514 AVAIL_OUT, and would have been skipped due to the full redundancy check.
1517 bitmap_insert_into_set (PHI_GEN (block),
1518 PHI_RESULT (temp));
1519 bitmap_value_replace_in_set (AVAIL_OUT (block),
1520 PHI_RESULT (temp));
1521 bitmap_insert_into_set (NEW_SETS (block),
1522 PHI_RESULT (temp));
1524 if (dump_file && (dump_flags & TDF_DETAILS))
1526 fprintf (dump_file, "Created phi ");
1527 print_generic_expr (dump_file, temp, 0);
1528 fprintf (dump_file, " in block %d\n", block->index);
1530 pre_stats.phis++;
1531 return true;
1536 /* Perform insertion of partially redundant values.
1537 For BLOCK, do the following:
1538 1. Propagate the NEW_SETS of the dominator into the current block.
1539 If the block has multiple predecessors,
1540 2a. Iterate over the ANTIC expressions for the block to see if
1541 any of them are partially redundant.
1542 2b. If so, insert them into the necessary predecessors to make
1543 the expression fully redundant.
1544 2c. Insert a new PHI merging the values of the predecessors.
1545 2d. Insert the new PHI, and the new expressions, into the
1546 NEW_SETS set.
1547 3. Recursively call ourselves on the dominator children of BLOCK.
1551 static bool
1552 insert_aux (basic_block block)
1554 basic_block son;
1555 bool new_stuff = false;
1557 if (block)
1559 basic_block dom;
1560 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1561 if (dom)
1563 unsigned i;
1564 bitmap_iterator bi;
1565 bitmap_set_t newset = NEW_SETS (dom);
1566 if (newset)
1568 /* Note that we need to value_replace both NEW_SETS, and
1569 AVAIL_OUT. For both the case of NEW_SETS, the value may be
1570 represented by some non-simple expression here that we want
1571 to replace it with. */
1572 EXECUTE_IF_SET_IN_BITMAP (newset->expressions, 0, i, bi)
1574 bitmap_value_replace_in_set (NEW_SETS (block), ssa_name (i));
1575 bitmap_value_replace_in_set (AVAIL_OUT (block), ssa_name (i));
1578 if (EDGE_COUNT (block->preds) > 1)
1580 value_set_node_t node;
1581 for (node = ANTIC_IN (block)->head;
1582 node;
1583 node = node->next)
1585 if (BINARY_CLASS_P (node->expr)
1586 || UNARY_CLASS_P (node->expr))
1588 tree *avail;
1589 tree val;
1590 bool by_some = false;
1591 bool cant_insert = false;
1592 bool all_same = true;
1593 tree first_s = NULL;
1594 edge pred;
1595 basic_block bprime;
1596 tree eprime = NULL_TREE;
1597 edge_iterator ei;
1599 val = get_value_handle (node->expr);
1600 if (bitmap_set_contains_value (PHI_GEN (block), val))
1601 continue;
1602 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
1604 if (dump_file && (dump_flags & TDF_DETAILS))
1605 fprintf (dump_file, "Found fully redundant value\n");
1606 continue;
1609 avail = xcalloc (last_basic_block, sizeof (tree));
1610 FOR_EACH_EDGE (pred, ei, block->preds)
1612 tree vprime;
1613 tree edoubleprime;
1615 /* This can happen in the very weird case
1616 that our fake infinite loop edges have caused a
1617 critical edge to appear. */
1618 if (EDGE_CRITICAL_P (pred))
1620 cant_insert = true;
1621 break;
1623 bprime = pred->src;
1624 eprime = phi_translate (node->expr,
1625 ANTIC_IN (block),
1626 bprime, block);
1628 /* eprime will generally only be NULL if the
1629 value of the expression, translated
1630 through the PHI for this predecessor, is
1631 undefined. If that is the case, we can't
1632 make the expression fully redundant,
1633 because its value is undefined along a
1634 predecessor path. We can thus break out
1635 early because it doesn't matter what the
1636 rest of the results are. */
1637 if (eprime == NULL)
1639 cant_insert = true;
1640 break;
1643 eprime = fully_constant_expression (eprime);
1644 vprime = get_value_handle (eprime);
1645 gcc_assert (vprime);
1646 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
1647 vprime);
1648 if (edoubleprime == NULL)
1650 avail[bprime->index] = eprime;
1651 all_same = false;
1653 else
1655 avail[bprime->index] = edoubleprime;
1656 by_some = true;
1657 if (first_s == NULL)
1658 first_s = edoubleprime;
1659 else if (!operand_equal_p (first_s, edoubleprime,
1661 all_same = false;
1664 /* If we can insert it, it's not the same value
1665 already existing along every predecessor, and
1666 it's defined by some predecessor, it is
1667 partially redundant. */
1668 if (!cant_insert && !all_same && by_some)
1670 if (insert_into_preds_of_block (block, node, avail,
1671 "prephitmp"))
1672 new_stuff = true;
1674 /* If all edges produce the same value and that value is
1675 an invariant, then the PHI has the same value on all
1676 edges. Note this. */
1677 else if (all_same && eprime
1678 && is_gimple_min_invariant (eprime)
1679 && !is_gimple_min_invariant (val))
1681 value_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
1682 value_set_node_t node;
1683 for (node = exprset->head; node; node = node->next)
1685 if (TREE_CODE (node->expr) == SSA_NAME)
1687 vn_add (node->expr, eprime, NULL);
1688 pre_stats.constified++;
1692 free (avail);
1698 for (son = first_dom_son (CDI_DOMINATORS, block);
1699 son;
1700 son = next_dom_son (CDI_DOMINATORS, son))
1702 new_stuff |= insert_aux (son);
1705 return new_stuff;
1708 /* Perform insertion of partially redundant values. */
1710 static void
1711 insert (void)
1713 bool new_stuff = true;
1714 basic_block bb;
1715 int num_iterations = 0;
1717 FOR_ALL_BB (bb)
1718 NEW_SETS (bb) = bitmap_set_new ();
1720 while (new_stuff)
1722 num_iterations++;
1723 new_stuff = false;
1724 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1726 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1727 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1731 /* Return true if VAR is an SSA variable with no defining statement in
1732 this procedure, *AND* isn't a live-on-entry parameter. */
1734 static bool
1735 is_undefined_value (tree expr)
1737 return (TREE_CODE (expr) == SSA_NAME
1738 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
1739 /* PARM_DECLs and hard registers are always defined. */
1740 && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL);
1744 /* Given an SSA variable VAR and an expression EXPR, compute the value
1745 number for EXPR and create a value handle (VAL) for it. If VAR and
1746 EXPR are not the same, associate VAL with VAR. Finally, add VAR to
1747 S1 and its value handle to S2.
1749 VUSES represent the virtual use operands associated with EXPR (if
1750 any). They are used when computing the hash value for EXPR. */
1752 static inline void
1753 add_to_sets (tree var, tree expr, vuse_optype vuses, bitmap_set_t s1,
1754 bitmap_set_t s2)
1756 tree val = vn_lookup_or_add (expr, vuses);
1758 /* VAR and EXPR may be the same when processing statements for which
1759 we are not computing value numbers (e.g., non-assignments, or
1760 statements that make aliased stores). In those cases, we are
1761 only interested in making VAR available as its own value. */
1762 if (var != expr)
1763 vn_add (var, val, NULL);
1765 if (s1)
1766 bitmap_insert_into_set (s1, var);
1767 bitmap_value_insert_into_set (s2, var);
1771 /* Given a unary or binary expression EXPR, create and return a new
1772 expression with the same structure as EXPR but with its operands
1773 replaced with the value handles of each of the operands of EXPR.
1774 Insert EXPR's operands into the EXP_GEN set for BLOCK.
1776 VUSES represent the virtual use operands associated with EXPR (if
1777 any). They are used when computing the hash value for EXPR. */
1779 static inline tree
1780 create_value_expr_from (tree expr, basic_block block, vuse_optype vuses)
1782 int i;
1783 enum tree_code code = TREE_CODE (expr);
1784 tree vexpr;
1786 gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
1787 || TREE_CODE_CLASS (code) == tcc_binary
1788 || TREE_CODE_CLASS (code) == tcc_reference);
1790 if (TREE_CODE_CLASS (code) == tcc_unary)
1791 vexpr = pool_alloc (unary_node_pool);
1792 else if (TREE_CODE_CLASS (code) == tcc_reference)
1793 vexpr = pool_alloc (reference_node_pool);
1794 else
1795 vexpr = pool_alloc (binary_node_pool);
1797 memcpy (vexpr, expr, tree_size (expr));
1799 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
1801 tree op = TREE_OPERAND (expr, i);
1802 if (op != NULL)
1804 tree val = vn_lookup_or_add (op, vuses);
1805 if (!is_undefined_value (op))
1806 value_insert_into_set (EXP_GEN (block), op);
1807 if (TREE_CODE (val) == VALUE_HANDLE)
1808 TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
1809 TREE_OPERAND (vexpr, i) = val;
1813 return vexpr;
1817 /* Compute the AVAIL set for all basic blocks.
1819 This function performs value numbering of the statements in each basic
1820 block. The AVAIL sets are built from information we glean while doing
1821 this value numbering, since the AVAIL sets contain only one entry per
1822 value.
1824 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
1825 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
1827 static void
1828 compute_avail (void)
1830 basic_block block, son;
1831 basic_block *worklist;
1832 size_t sp = 0;
1833 tree param;
1835 /* For arguments with default definitions, we pretend they are
1836 defined in the entry block. */
1837 for (param = DECL_ARGUMENTS (current_function_decl);
1838 param;
1839 param = TREE_CHAIN (param))
1841 if (default_def (param) != NULL)
1843 tree val;
1844 tree def = default_def (param);
1845 val = vn_lookup_or_add (def, NULL);
1846 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
1847 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
1851 /* Allocate the worklist. */
1852 worklist = xmalloc (sizeof (basic_block) * n_basic_blocks);
1854 /* Seed the algorithm by putting the dominator children of the entry
1855 block on the worklist. */
1856 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR);
1857 son;
1858 son = next_dom_son (CDI_DOMINATORS, son))
1859 worklist[sp++] = son;
1861 /* Loop until the worklist is empty. */
1862 while (sp)
1864 block_stmt_iterator bsi;
1865 tree stmt, phi;
1866 basic_block dom;
1868 /* Pick a block from the worklist. */
1869 block = worklist[--sp];
1871 /* Initially, the set of available values in BLOCK is that of
1872 its immediate dominator. */
1873 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1874 if (dom)
1875 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
1877 /* Generate values for PHI nodes. */
1878 for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
1879 /* We have no need for virtual phis, as they don't represent
1880 actual computations. */
1881 if (is_gimple_reg (PHI_RESULT (phi)))
1882 add_to_sets (PHI_RESULT (phi), PHI_RESULT (phi), NULL,
1883 PHI_GEN (block), AVAIL_OUT (block));
1885 /* Now compute value numbers and populate value sets with all
1886 the expressions computed in BLOCK. */
1887 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
1889 stmt_ann_t ann;
1890 size_t j;
1892 stmt = bsi_stmt (bsi);
1893 ann = stmt_ann (stmt);
1894 get_stmt_operands (stmt);
1896 /* We are only interested in assignments of the form
1897 X_i = EXPR, where EXPR represents an "interesting"
1898 computation, it has no volatile operands and X_i
1899 doesn't flow through an abnormal edge. */
1900 if (TREE_CODE (stmt) == MODIFY_EXPR
1901 && !ann->has_volatile_ops
1902 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
1903 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (stmt, 0)))
1905 tree lhs = TREE_OPERAND (stmt, 0);
1906 tree rhs = TREE_OPERAND (stmt, 1);
1907 vuse_optype vuses = STMT_VUSE_OPS (stmt);
1909 STRIP_USELESS_TYPE_CONVERSION (rhs);
1910 if (TREE_CODE (rhs) == SSA_NAME
1911 || is_gimple_min_invariant (rhs))
1913 /* Compute a value number for the RHS of the statement
1914 and add its value to the AVAIL_OUT set for the block.
1915 Add the LHS to TMP_GEN. */
1916 add_to_sets (lhs, rhs, vuses, TMP_GEN (block),
1917 AVAIL_OUT (block));
1919 if (TREE_CODE (rhs) == SSA_NAME
1920 && !is_undefined_value (rhs))
1921 value_insert_into_set (EXP_GEN (block), rhs);
1922 continue;
1924 else if (UNARY_CLASS_P (rhs) || BINARY_CLASS_P (rhs)
1925 || TREE_CODE (rhs) == INDIRECT_REF)
1927 /* For binary, unary, and reference expressions,
1928 create a duplicate expression with the operands
1929 replaced with the value handles of the original
1930 RHS. */
1931 tree newt = create_value_expr_from (rhs, block, vuses);
1932 add_to_sets (lhs, newt, vuses, TMP_GEN (block),
1933 AVAIL_OUT (block));
1934 value_insert_into_set (EXP_GEN (block), newt);
1935 continue;
1939 /* For any other statement that we don't recognize, simply
1940 make the names generated by the statement available in
1941 AVAIL_OUT and TMP_GEN. */
1942 for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1944 tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1945 add_to_sets (def, def, NULL, TMP_GEN (block),
1946 AVAIL_OUT (block));
1949 for (j = 0; j < NUM_USES (STMT_USE_OPS (stmt)); j++)
1951 tree use = USE_OP (STMT_USE_OPS (stmt), j);
1952 add_to_sets (use, use, NULL, NULL, AVAIL_OUT (block));
1956 /* Put the dominator children of BLOCK on the worklist of blocks
1957 to compute available sets for. */
1958 for (son = first_dom_son (CDI_DOMINATORS, block);
1959 son;
1960 son = next_dom_son (CDI_DOMINATORS, son))
1961 worklist[sp++] = son;
1964 free (worklist);
1968 /* Eliminate fully redundant computations. */
1970 static void
1971 eliminate (void)
1973 basic_block b;
1975 FOR_EACH_BB (b)
1977 block_stmt_iterator i;
1979 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
1981 tree stmt = bsi_stmt (i);
1983 /* Lookup the RHS of the expression, see if we have an
1984 available computation for it. If so, replace the RHS with
1985 the available computation. */
1986 if (TREE_CODE (stmt) == MODIFY_EXPR
1987 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
1988 && TREE_CODE (TREE_OPERAND (stmt ,1)) != SSA_NAME
1989 && !is_gimple_min_invariant (TREE_OPERAND (stmt, 1))
1990 && !stmt_ann (stmt)->has_volatile_ops)
1992 tree lhs = TREE_OPERAND (stmt, 0);
1993 tree *rhs_p = &TREE_OPERAND (stmt, 1);
1994 tree sprime;
1996 sprime = bitmap_find_leader (AVAIL_OUT (b),
1997 vn_lookup (lhs, NULL));
1998 if (sprime
1999 && sprime != lhs
2000 && (TREE_CODE (*rhs_p) != SSA_NAME
2001 || may_propagate_copy (*rhs_p, sprime)))
2003 gcc_assert (sprime != *rhs_p);
2005 if (dump_file && (dump_flags & TDF_DETAILS))
2007 fprintf (dump_file, "Replaced ");
2008 print_generic_expr (dump_file, *rhs_p, 0);
2009 fprintf (dump_file, " with ");
2010 print_generic_expr (dump_file, sprime, 0);
2011 fprintf (dump_file, " in ");
2012 print_generic_stmt (dump_file, stmt, 0);
2014 if (TREE_CODE (sprime) == SSA_NAME)
2015 NECESSARY (SSA_NAME_DEF_STMT (sprime)) = 1;
2016 pre_stats.eliminations++;
2017 propagate_tree_value (rhs_p, sprime);
2018 modify_stmt (stmt);
2020 /* If we removed EH side effects from the statement, clean
2021 its EH information. */
2022 if (maybe_clean_eh_stmt (stmt))
2024 bitmap_set_bit (need_eh_cleanup,
2025 bb_for_stmt (stmt)->index);
2026 if (dump_file && (dump_flags & TDF_DETAILS))
2027 fprintf (dump_file, " Removed EH side effects.\n");
2035 /* Borrow a bit of tree-ssa-dce.c for the moment.
2036 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
2037 this may be a bit faster, and we may want critical edges kept split. */
2039 /* If OP's defining statement has not already been determined to be necessary,
2040 mark that statement necessary. and place it on the WORKLIST. */
2042 static inline void
2043 mark_operand_necessary (tree op, VEC(tree_on_heap) **worklist)
2045 tree stmt;
2046 int ver;
2048 gcc_assert (op);
2050 ver = SSA_NAME_VERSION (op);
2052 stmt = SSA_NAME_DEF_STMT (op);
2053 gcc_assert (stmt);
2055 if (NECESSARY (stmt)
2056 || IS_EMPTY_STMT (stmt))
2057 return;
2059 NECESSARY (stmt) = 1;
2060 VEC_safe_push (tree_on_heap, *worklist, stmt);
2063 /* Because we don't follow exactly the standard PRE algorithm, and decide not
2064 to insert PHI nodes sometimes, and because value numbering of casts isn't
2065 perfect, we sometimes end up inserting dead code. This simple DCE-like
2066 pass removes any insertions we made that weren't actually used. */
2068 static void
2069 remove_dead_inserted_code (void)
2071 VEC (tree_on_heap) *worklist = NULL;
2072 int i;
2073 tree t;
2075 for (i = 0; VEC_iterate (tree_on_heap, inserted_exprs, i, t); i++)
2077 if (NECESSARY (t))
2078 VEC_safe_push (tree_on_heap, worklist, t);
2080 while (VEC_length (tree_on_heap, worklist) > 0)
2082 t = VEC_pop (tree_on_heap, worklist);
2083 if (TREE_CODE (t) == PHI_NODE)
2085 /* PHI nodes are somewhat special in that each PHI alternative has
2086 data and control dependencies. All the statements feeding the
2087 PHI node's arguments are always necessary. In aggressive mode,
2088 we also consider the control dependent edges leading to the
2089 predecessor block associated with each PHI alternative as
2090 necessary. */
2091 int k;
2092 for (k = 0; k < PHI_NUM_ARGS (t); k++)
2094 tree arg = PHI_ARG_DEF (t, k);
2095 if (TREE_CODE (arg) == SSA_NAME)
2096 mark_operand_necessary (arg, &worklist);
2099 else
2101 /* Propagate through the operands. Examine all the USE, VUSE and
2102 V_MAY_DEF operands in this statement. Mark all the statements
2103 which feed this statement's uses as necessary. */
2104 ssa_op_iter iter;
2105 tree use;
2107 get_stmt_operands (t);
2109 /* The operands of V_MAY_DEF expressions are also needed as they
2110 represent potential definitions that may reach this
2111 statement (V_MAY_DEF operands allow us to follow def-def
2112 links). */
2114 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
2115 mark_operand_necessary (use, &worklist);
2118 for (i = 0; VEC_iterate (tree_on_heap, inserted_exprs, i, t); i++)
2120 if (!NECESSARY (t))
2122 block_stmt_iterator bsi;
2123 if (dump_file && (dump_flags & TDF_DETAILS))
2125 fprintf (dump_file, "Removing unnecessary insertion:");
2126 print_generic_stmt (dump_file, t, 0);
2128 if (TREE_CODE (t) == PHI_NODE)
2130 remove_phi_node (t, NULL, bb_for_stmt (t));
2132 else
2134 bsi = bsi_for_stmt (t);
2135 bsi_remove (&bsi);
2139 VEC_free (tree_on_heap, worklist);
2141 /* Initialize data structures used by PRE. */
2143 static void
2144 init_pre (bool do_fre)
2146 basic_block bb;
2148 inserted_exprs = NULL;
2149 vn_init ();
2150 if (!do_fre)
2151 current_loops = loop_optimizer_init (dump_file);
2152 connect_infinite_loops_to_exit ();
2153 memset (&pre_stats, 0, sizeof (pre_stats));
2155 /* If block 0 has more than one predecessor, it means that its PHI
2156 nodes will have arguments coming from block -1. This creates
2157 problems for several places in PRE that keep local arrays indexed
2158 by block number. To prevent this, we split the edge coming from
2159 ENTRY_BLOCK_PTR (FIXME, if ENTRY_BLOCK_PTR had an index number
2160 different than -1 we wouldn't have to hack this. tree-ssa-dce.c
2161 needs a similar change). */
2162 if (EDGE_COUNT (EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest->preds) > 1)
2163 if (!(EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->flags & EDGE_ABNORMAL))
2164 split_edge (EDGE_SUCC (ENTRY_BLOCK_PTR, 0));
2166 FOR_ALL_BB (bb)
2167 bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
2169 bitmap_obstack_initialize (&grand_bitmap_obstack);
2170 phi_translate_table = htab_create (511, expr_pred_trans_hash,
2171 expr_pred_trans_eq, free);
2172 value_set_pool = create_alloc_pool ("Value sets",
2173 sizeof (struct value_set), 30);
2174 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
2175 sizeof (struct bitmap_set), 30);
2176 value_set_node_pool = create_alloc_pool ("Value set nodes",
2177 sizeof (struct value_set_node), 30);
2178 calculate_dominance_info (CDI_POST_DOMINATORS);
2179 calculate_dominance_info (CDI_DOMINATORS);
2180 binary_node_pool = create_alloc_pool ("Binary tree nodes",
2181 tree_code_size (PLUS_EXPR), 30);
2182 unary_node_pool = create_alloc_pool ("Unary tree nodes",
2183 tree_code_size (NEGATE_EXPR), 30);
2184 reference_node_pool = create_alloc_pool ("Reference tree nodes",
2185 tree_code_size (ARRAY_REF), 30);
2186 FOR_ALL_BB (bb)
2188 EXP_GEN (bb) = set_new (true);
2189 PHI_GEN (bb) = bitmap_set_new ();
2190 TMP_GEN (bb) = bitmap_set_new ();
2191 AVAIL_OUT (bb) = bitmap_set_new ();
2194 need_eh_cleanup = BITMAP_XMALLOC ();
2198 /* Deallocate data structures used by PRE. */
2200 static void
2201 fini_pre (bool do_fre)
2203 basic_block bb;
2204 unsigned int i;
2206 VEC_free (tree_on_heap, inserted_exprs);
2207 bitmap_obstack_release (&grand_bitmap_obstack);
2208 free_alloc_pool (value_set_pool);
2209 free_alloc_pool (bitmap_set_pool);
2210 free_alloc_pool (value_set_node_pool);
2211 free_alloc_pool (binary_node_pool);
2212 free_alloc_pool (reference_node_pool);
2213 free_alloc_pool (unary_node_pool);
2214 htab_delete (phi_translate_table);
2215 remove_fake_exit_edges ();
2217 FOR_ALL_BB (bb)
2219 free (bb->aux);
2220 bb->aux = NULL;
2223 free_dominance_info (CDI_POST_DOMINATORS);
2224 vn_delete ();
2226 if (!bitmap_empty_p (need_eh_cleanup))
2228 tree_purge_all_dead_eh_edges (need_eh_cleanup);
2229 cleanup_tree_cfg ();
2232 BITMAP_XFREE (need_eh_cleanup);
2234 /* Wipe out pointers to VALUE_HANDLEs. In the not terribly distant
2235 future we will want them to be persistent though. */
2236 for (i = 0; i < num_ssa_names; i++)
2238 tree name = ssa_name (i);
2240 if (!name)
2241 continue;
2243 if (SSA_NAME_VALUE (name)
2244 && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
2245 SSA_NAME_VALUE (name) = NULL;
2247 if (!do_fre && current_loops)
2249 loop_optimizer_finalize (current_loops, dump_file);
2250 current_loops = NULL;
2255 /* Main entry point to the SSA-PRE pass. DO_FRE is true if the caller
2256 only wants to do full redundancy elimination. */
2258 static void
2259 execute_pre (bool do_fre)
2261 init_pre (do_fre);
2263 /* Collect and value number expressions computed in each basic block. */
2264 compute_avail ();
2266 if (dump_file && (dump_flags & TDF_DETAILS))
2268 basic_block bb;
2270 FOR_ALL_BB (bb)
2272 print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
2273 bitmap_print_value_set (dump_file, TMP_GEN (bb), "tmp_gen",
2274 bb->index);
2275 bitmap_print_value_set (dump_file, AVAIL_OUT (bb), "avail_out",
2276 bb->index);
2280 /* Insert can get quite slow on an incredibly large number of basic
2281 blocks due to some quadratic behavior. Until this behavior is
2282 fixed, don't run it when he have an incredibly large number of
2283 bb's. If we aren't going to run insert, there is no point in
2284 computing ANTIC, either, even though it's plenty fast. */
2285 if (!do_fre && n_basic_blocks < 4000)
2287 compute_antic ();
2288 insert ();
2291 /* Remove all the redundant expressions. */
2292 eliminate ();
2295 if (dump_file && (dump_flags & TDF_STATS))
2297 fprintf (dump_file, "Insertions:%d\n", pre_stats.insertions);
2298 fprintf (dump_file, "New PHIs:%d\n", pre_stats.phis);
2299 fprintf (dump_file, "Eliminated:%d\n", pre_stats.eliminations);
2300 fprintf (dump_file, "Constified:%d\n", pre_stats.constified);
2303 bsi_commit_edge_inserts ();
2304 if (!do_fre)
2305 remove_dead_inserted_code ();
2306 fini_pre (do_fre);
2311 /* Gate and execute functions for PRE. */
2313 static void
2314 do_pre (void)
2316 execute_pre (false);
2319 static bool
2320 gate_pre (void)
2322 return flag_tree_pre != 0;
2325 struct tree_opt_pass pass_pre =
2327 "pre", /* name */
2328 gate_pre, /* gate */
2329 do_pre, /* execute */
2330 NULL, /* sub */
2331 NULL, /* next */
2332 0, /* static_pass_number */
2333 TV_TREE_PRE, /* tv_id */
2334 PROP_no_crit_edges | PROP_cfg
2335 | PROP_ssa | PROP_alias, /* properties_required */
2336 0, /* properties_provided */
2337 0, /* properties_destroyed */
2338 0, /* todo_flags_start */
2339 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
2340 0 /* letter */
2344 /* Gate and execute functions for FRE. */
2346 static void
2347 do_fre (void)
2349 execute_pre (true);
2352 static bool
2353 gate_fre (void)
2355 return flag_tree_fre != 0;
2358 struct tree_opt_pass pass_fre =
2360 "fre", /* name */
2361 gate_fre, /* gate */
2362 do_fre, /* execute */
2363 NULL, /* sub */
2364 NULL, /* next */
2365 0, /* static_pass_number */
2366 TV_TREE_FRE, /* tv_id */
2367 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2368 0, /* properties_provided */
2369 0, /* properties_destroyed */
2370 0, /* todo_flags_start */
2371 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
2372 0 /* letter */