* gcc/testsuite/ada/acats/run_acats: Missed in last commit.
[official-gcc.git] / gcc / tree-ssa-pre.c
blob38d5d2b86320928dd95b4a5960e193bdcefd9a49
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. */
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 "splay-tree.h"
45 #include "bitmap.h"
46 #include "langhooks.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. Our canonicalization of expressions during lookups don't take
60 constants into account very well. In particular, we don't fold
61 anywhere, so we can get situations where we stupidly think
62 something is a new value (a + 1 + 1 vs a + 2). This is somewhat
63 expensive to fix, but it does expose a lot more eliminations.
64 It may or not be worth it, depending on how critical you
65 consider PRE vs just plain GRE.
66 */
68 /* For ease of terminology, "expression node" in the below refers to
69 every expression node but MODIFY_EXPR, because MODIFY_EXPR's represent
70 the actual statement containing the expressions we care about, and
71 we cache the value number by putting it in the expression. */
73 /* Basic algorithm
75 First we walk the statements to generate the AVAIL sets, the
76 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
77 generation of values/expressions by a given block. We use them
78 when computing the ANTIC sets. The AVAIL sets consist of
79 SSA_NAME's that represent values, so we know what values are
80 available in what blocks. AVAIL is a forward dataflow problem. In
81 SSA, values are never killed, so we don't need a kill set, or a
82 fixpoint iteration, in order to calculate the AVAIL sets. In
83 traditional parlance, AVAIL sets tell us the downsafety of the
84 expressions/values.
86 Next, we generate the ANTIC sets. These sets represent the
87 anticipatable expressions. ANTIC is a backwards dataflow
88 problem.An expression is anticipatable in a given block if it could
89 be generated in that block. This means that if we had to perform
90 an insertion in that block, of the value of that expression, we
91 could. Calculating the ANTIC sets requires phi translation of
92 expressions, because the flow goes backwards through phis. We must
93 iterate to a fixpoint of the ANTIC sets, because we have a kill
94 set. Even in SSA form, values are not live over the entire
95 function, only from their definition point onwards. So we have to
96 remove values from the ANTIC set once we go past the definition
97 point of the leaders that make them up.
98 compute_antic/compute_antic_aux performs this computation.
100 Third, we perform insertions to make partially redundant
101 expressions fully redundant.
103 An expression is partially redundant (excluding partial
104 anticipation) if:
106 1. It is AVAIL in some, but not all, of the predecessors of a
107 given block.
108 2. It is ANTIC in all the predecessors.
110 In order to make it fully redundant, we insert the expression into
111 the predecessors where it is not available, but is ANTIC.
112 insert/insert_aux performs this insertion.
114 Fourth, we eliminate fully redundant expressions.
115 This is a simple statement walk that replaces redundant
116 calculations with the now available values. */
118 /* Representations of value numbers:
120 Value numbers are represented using the "value handle" approach.
121 This means that each SSA_NAME (and for other reasons to be
122 disclosed in a moment, expression nodes) has a value handle that
123 can be retrieved through get_value_handle. This value handle, *is*
124 the value number of the SSA_NAME. You can pointer compare the
125 value handles for equivalence purposes.
127 For debugging reasons, the value handle is internally more than
128 just a number, it is a VAR_DECL named "value.x", where x is a
129 unique number for each value number in use. This allows
130 expressions with SSA_NAMES replaced by value handles to still be
131 pretty printed in a sane way. They simply print as "value.3 *
132 value.5", etc.
134 Expression nodes have value handles associated with them as a
135 cache. Otherwise, we'd have to look them up again in the hash
136 table This makes significant difference (factor of two or more) on
137 some test cases. They can be thrown away after the pass is
138 finished. */
140 /* Representation of expressions on value numbers:
142 In some portions of this code, you will notice we allocate "fake"
143 analogues to the expression we are value numbering, and replace the
144 operands with the values of the expression. Since we work on
145 values, and not just names, we canonicalize expressions to value
146 expressions for use in the ANTIC sets, the EXP_GEN set, etc.
148 This is theoretically unnecessary, it just saves a bunch of
149 repeated get_value_handle and find_leader calls in the remainder of
150 the code, trading off temporary memory usage for speed. The tree
151 nodes aren't actually creating more garbage, since they are
152 allocated in a special pools which are thrown away at the end of
153 this pass.
155 All of this also means that if you print the EXP_GEN or ANTIC sets,
156 you will see "value.5 + value.7" in the set, instead of "a_55 +
157 b_66" or something. The only thing that actually cares about
158 seeing the value leaders is phi translation, and it needs to be
159 able to find the leader for a value in an arbitrary block, so this
160 "value expression" form is perfect for it (otherwise you'd do
161 get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
164 /* Representation of sets:
166 There are currently two types of sets used, hopefully to be unified soon.
167 The AVAIL sets do not need to be sorted in any particular order,
168 and thus, are simply represented as two bitmaps, one that keeps
169 track of values present in the set, and one that keeps track of
170 expressions present in the set.
172 The other sets are represented as doubly linked lists kept in topological
173 order, with an optional supporting bitmap of values present in the
174 set. The sets represent values, and the elements can be values or
175 expressions. The elements can appear in different sets, but each
176 element can only appear once in each set.
178 Since each node in the set represents a value, we also want to be
179 able to map expression, set pairs to something that tells us
180 whether the value is present is a set. We use a per-set bitmap for
181 that. The value handles also point to a linked list of the
182 expressions they represent via a tree annotation. This is mainly
183 useful only for debugging, since we don't do identity lookups. */
186 /* A value set element. Basically a single linked list of
187 expressions/values. */
188 typedef struct value_set_node
190 /* An expression. */
191 tree expr;
193 /* A pointer to the next element of the value set. */
194 struct value_set_node *next;
195 } *value_set_node_t;
198 /* A value set. This is a singly linked list of value_set_node
199 elements with a possible bitmap that tells us what values exist in
200 the set. This set must be kept in topologically sorted order. */
201 typedef struct value_set
203 /* The head of the list. Used for iterating over the list in
204 order. */
205 value_set_node_t head;
207 /* The tail of the list. Used for tail insertions, which are
208 necessary to keep the set in topologically sorted order because
209 of how the set is built. */
210 value_set_node_t tail;
212 /* The length of the list. */
213 size_t length;
215 /* True if the set is indexed, which means it contains a backing
216 bitmap for quick determination of whether certain values exist in the
217 set. */
218 bool indexed;
220 /* The bitmap of values that exist in the set. May be NULL in an
221 empty or non-indexed set. */
222 bitmap values;
224 } *value_set_t;
227 /* An unordered bitmap set. One bitmap tracks values, the other,
228 expressions. */
229 typedef struct bitmap_set
231 bitmap expressions;
232 bitmap values;
233 } *bitmap_set_t;
235 /* Sets that we need to keep track of. */
236 typedef struct bb_value_sets
238 /* The EXP_GEN set, which represents expressions/values generated in
239 a basic block. */
240 value_set_t exp_gen;
242 /* The PHI_GEN set, which represents PHI results generated in a
243 basic block. */
244 bitmap_set_t phi_gen;
246 /* The TMP_GEN set, which represents results/temporaries generated
247 in a basic block. IE the LHS of an expression. */
248 bitmap_set_t tmp_gen;
250 /* The AVAIL_OUT set, which represents which values are available in
251 a given basic block. */
252 bitmap_set_t avail_out;
254 /* The ANTIC_IN set, which represents which values are anticiptable
255 in a given basic block. */
256 value_set_t antic_in;
258 /* The NEW_SETS set, which is used during insertion to augment the
259 AVAIL_OUT set of blocks with the new insertions performed during
260 the current iteration. */
261 bitmap_set_t new_sets;
262 } *bb_value_sets_t;
264 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
265 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
266 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
267 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
268 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
269 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
271 /* This structure is used to keep track of statistics on what
272 optimization PRE was able to perform. */
273 static struct
275 /* The number of RHS computations eliminated by PRE. */
276 int eliminations;
278 /* The number of new expressions/temporaries generated by PRE. */
279 int insertions;
281 /* The number of new PHI nodes added by PRE. */
282 int phis;
283 } pre_stats;
286 static tree bitmap_find_leader (bitmap_set_t, tree);
287 static tree find_leader (value_set_t, tree);
288 static void value_insert_into_set (value_set_t, tree);
289 static void bitmap_value_insert_into_set (bitmap_set_t, tree);
290 static void bitmap_value_replace_in_set (bitmap_set_t, tree);
291 static void insert_into_set (value_set_t, tree);
292 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
293 static bool bitmap_set_contains_value (bitmap_set_t, tree);
294 static bitmap_set_t bitmap_set_new (void);
295 static value_set_t set_new (bool);
296 static bool is_undefined_value (tree);
297 static tree create_expression_by_pieces (basic_block, tree, tree);
300 /* We can add and remove elements and entries to and from sets
301 and hash tables, so we use alloc pools for them. */
303 static alloc_pool value_set_pool;
304 static alloc_pool bitmap_set_pool;
305 static alloc_pool value_set_node_pool;
306 static alloc_pool binary_node_pool;
307 static alloc_pool unary_node_pool;
308 static alloc_pool reference_node_pool;
309 static bitmap_obstack grand_bitmap_obstack;
311 /* Set of blocks with statements that have had its EH information
312 cleaned up. */
313 static bitmap need_eh_cleanup;
315 /* The phi_translate_table caches phi translations for a given
316 expression and predecessor. */
318 static htab_t phi_translate_table;
320 /* A three tuple {e, pred, v} used to cache phi translations in the
321 phi_translate_table. */
323 typedef struct expr_pred_trans_d
325 /* The expression. */
326 tree e;
328 /* The predecessor block along which we translated the expression. */
329 basic_block pred;
331 /* The value that resulted from the translation. */
332 tree v;
334 /* The hashcode for the expression, pred pair. This is cached for
335 speed reasons. */
336 hashval_t hashcode;
337 } *expr_pred_trans_t;
339 /* Return the hash value for a phi translation table entry. */
341 static hashval_t
342 expr_pred_trans_hash (const void *p)
344 const expr_pred_trans_t ve = (expr_pred_trans_t) p;
345 return ve->hashcode;
348 /* Return true if two phi translation table entries are the same.
349 P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
351 static int
352 expr_pred_trans_eq (const void *p1, const void *p2)
354 const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
355 const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
356 basic_block b1 = ve1->pred;
357 basic_block b2 = ve2->pred;
360 /* If they are not translations for the same basic block, they can't
361 be equal. */
362 if (b1 != b2)
363 return false;
365 /* If they are for the same basic block, determine if the
366 expressions are equal. */
367 if (expressions_equal_p (ve1->e, ve2->e))
368 return true;
370 return false;
373 /* Search in the phi translation table for the translation of
374 expression E in basic block PRED. Return the translated value, if
375 found, NULL otherwise. */
377 static inline tree
378 phi_trans_lookup (tree e, basic_block pred)
380 void **slot;
381 struct expr_pred_trans_d ept;
382 ept.e = e;
383 ept.pred = pred;
384 ept.hashcode = vn_compute (e, (unsigned long) pred, NULL);
385 slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
386 NO_INSERT);
387 if (!slot)
388 return NULL;
389 else
390 return ((expr_pred_trans_t) *slot)->v;
394 /* Add the tuple mapping from {expression E, basic block PRED} to
395 value V, to the phi translation table. */
397 static inline void
398 phi_trans_add (tree e, tree v, basic_block pred)
400 void **slot;
401 expr_pred_trans_t new_pair = xmalloc (sizeof (*new_pair));
402 new_pair->e = e;
403 new_pair->pred = pred;
404 new_pair->v = v;
405 new_pair->hashcode = vn_compute (e, (unsigned long) pred, NULL);
406 slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
407 new_pair->hashcode, INSERT);
408 if (*slot)
409 free (*slot);
410 *slot = (void *) new_pair;
414 /* Add expression E to the expression set of value V. */
416 void
417 add_to_value (tree v, tree e)
419 /* Constants have no expression sets. */
420 if (is_gimple_min_invariant (v))
421 return;
423 if (VALUE_HANDLE_EXPR_SET (v) == NULL)
424 VALUE_HANDLE_EXPR_SET (v) = set_new (false);
426 insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
430 /* Return true if value V exists in the bitmap for SET. */
432 static inline bool
433 value_exists_in_set_bitmap (value_set_t set, tree v)
435 if (!set->values)
436 return false;
438 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (v));
442 /* Remove value V from the bitmap for SET. */
444 static void
445 value_remove_from_set_bitmap (value_set_t set, tree v)
447 gcc_assert (set->indexed);
449 if (!set->values)
450 return;
452 bitmap_clear_bit (set->values, VALUE_HANDLE_ID (v));
456 /* Insert the value number V into the bitmap of values existing in
457 SET. */
459 static inline void
460 value_insert_into_set_bitmap (value_set_t set, tree v)
462 gcc_assert (set->indexed);
464 if (set->values == NULL)
465 set->values = BITMAP_OBSTACK_ALLOC (&grand_bitmap_obstack);
467 bitmap_set_bit (set->values, VALUE_HANDLE_ID (v));
471 /* Create a new bitmap set and return it. */
473 static bitmap_set_t
474 bitmap_set_new (void)
476 bitmap_set_t ret = pool_alloc (bitmap_set_pool);
477 ret->expressions = BITMAP_OBSTACK_ALLOC (&grand_bitmap_obstack);
478 ret->values = BITMAP_OBSTACK_ALLOC (&grand_bitmap_obstack);
479 return ret;
482 /* Create a new set. */
484 static value_set_t
485 set_new (bool indexed)
487 value_set_t ret;
488 ret = pool_alloc (value_set_pool);
489 ret->head = ret->tail = NULL;
490 ret->length = 0;
491 ret->indexed = indexed;
492 ret->values = NULL;
493 return ret;
496 /* Insert an expression EXPR into a bitmapped set. */
498 static void
499 bitmap_insert_into_set (bitmap_set_t set, tree expr)
501 tree val;
502 /* XXX: For now, we only let SSA_NAMES into the bitmap sets. */
503 gcc_assert (TREE_CODE (expr) == SSA_NAME);
504 val = get_value_handle (expr);
506 gcc_assert (val);
507 if (!is_gimple_min_invariant (val))
509 bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
510 bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
514 /* Insert EXPR into SET. */
516 static void
517 insert_into_set (value_set_t set, tree expr)
519 value_set_node_t newnode = pool_alloc (value_set_node_pool);
520 tree val = get_value_handle (expr);
521 gcc_assert (val);
523 if (is_gimple_min_invariant (val))
524 return;
526 /* For indexed sets, insert the value into the set value bitmap.
527 For all sets, add it to the linked list and increment the list
528 length. */
529 if (set->indexed)
530 value_insert_into_set_bitmap (set, val);
532 newnode->next = NULL;
533 newnode->expr = expr;
534 set->length ++;
535 if (set->head == NULL)
537 set->head = set->tail = newnode;
539 else
541 set->tail->next = newnode;
542 set->tail = newnode;
546 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
548 static void
549 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
551 bitmap_copy (dest->expressions, orig->expressions);
552 bitmap_copy (dest->values, orig->values);
555 /* Copy the set ORIG to the set DEST. */
557 static void
558 set_copy (value_set_t dest, value_set_t orig)
560 value_set_node_t node;
562 if (!orig || !orig->head)
563 return;
565 for (node = orig->head;
566 node;
567 node = node->next)
569 insert_into_set (dest, node->expr);
573 /* Remove EXPR from SET. */
575 static void
576 set_remove (value_set_t set, tree expr)
578 value_set_node_t node, prev;
580 /* Remove the value of EXPR from the bitmap, decrement the set
581 length, and remove it from the actual double linked list. */
582 value_remove_from_set_bitmap (set, get_value_handle (expr));
583 set->length--;
584 prev = NULL;
585 for (node = set->head;
586 node != NULL;
587 prev = node, node = node->next)
589 if (node->expr == expr)
591 if (prev == NULL)
592 set->head = node->next;
593 else
594 prev->next= node->next;
596 if (node == set->tail)
597 set->tail = prev;
598 pool_free (value_set_node_pool, node);
599 return;
604 /* Return true if SET contains the value VAL. */
606 static bool
607 set_contains_value (value_set_t set, tree val)
609 /* All constants are in every set. */
610 if (is_gimple_min_invariant (val))
611 return true;
613 if (set->length == 0)
614 return false;
616 return value_exists_in_set_bitmap (set, val);
619 /* Return true if bitmapped set SET contains the expression EXPR. */
620 static bool
621 bitmap_set_contains (bitmap_set_t set, tree expr)
623 /* All constants are in every set. */
624 if (is_gimple_min_invariant (get_value_handle (expr)))
625 return true;
627 /* XXX: Bitmapped sets only contain SSA_NAME's for now. */
628 if (TREE_CODE (expr) != SSA_NAME)
629 return false;
630 return bitmap_bit_p (set->expressions, SSA_NAME_VERSION (expr));
634 /* Return true if bitmapped set SET contains the value VAL. */
636 static bool
637 bitmap_set_contains_value (bitmap_set_t set, tree val)
639 if (is_gimple_min_invariant (val))
640 return true;
641 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
644 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
646 static void
647 bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
649 value_set_t exprset;
650 value_set_node_t node;
651 if (is_gimple_min_invariant (lookfor))
652 return;
653 if (!bitmap_set_contains_value (set, lookfor))
654 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. */
718 static void
719 bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
721 tree val = get_value_handle (expr);
722 bitmap_set_replace_value (set, val, expr);
725 /* Insert EXPR into SET if EXPR's value is not already present in
726 SET. */
728 static void
729 bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
731 tree val = get_value_handle (expr);
733 if (is_gimple_min_invariant (val))
734 return;
736 if (!bitmap_set_contains_value (set, val))
737 bitmap_insert_into_set (set, expr);
740 /* Insert the value for EXPR into SET, if it doesn't exist already. */
742 static void
743 value_insert_into_set (value_set_t set, tree expr)
745 tree val = get_value_handle (expr);
747 /* Constant and invariant values exist everywhere, and thus,
748 actually keeping them in the sets is pointless. */
749 if (is_gimple_min_invariant (val))
750 return;
752 if (!set_contains_value (set, val))
753 insert_into_set (set, expr);
757 /* Print out SET to OUTFILE. */
759 static void
760 bitmap_print_value_set (FILE *outfile, bitmap_set_t set,
761 const char *setname, int blockindex)
763 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
764 if (set)
766 bool first = true;
767 unsigned i;
768 bitmap_iterator bi;
770 EXECUTE_IF_SET_IN_BITMAP (set->expressions, 0, i, bi)
772 if (!first)
773 fprintf (outfile, ", ");
774 first = false;
775 print_generic_expr (outfile, ssa_name (i), 0);
777 fprintf (outfile, " (");
778 print_generic_expr (outfile, get_value_handle (ssa_name (i)), 0);
779 fprintf (outfile, ") ");
782 fprintf (outfile, " }\n");
784 /* Print out the value_set SET to OUTFILE. */
786 static void
787 print_value_set (FILE *outfile, value_set_t set,
788 const char *setname, int blockindex)
790 value_set_node_t node;
791 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
792 if (set)
794 for (node = set->head;
795 node;
796 node = node->next)
798 print_generic_expr (outfile, node->expr, 0);
800 fprintf (outfile, " (");
801 print_generic_expr (outfile, get_value_handle (node->expr), 0);
802 fprintf (outfile, ") ");
804 if (node->next)
805 fprintf (outfile, ", ");
809 fprintf (outfile, " }\n");
812 /* Print out the expressions that have VAL to OUTFILE. */
814 void
815 print_value_expressions (FILE *outfile, tree val)
817 if (VALUE_HANDLE_EXPR_SET (val))
819 char s[10];
820 sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
821 print_value_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
826 void
827 debug_value_expressions (tree val)
829 print_value_expressions (stderr, val);
833 void debug_value_set (value_set_t, const char *, int);
835 void
836 debug_value_set (value_set_t set, const char *setname, int blockindex)
838 print_value_set (stderr, set, setname, blockindex);
841 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
842 the phis in PRED. Return NULL if we can't find a leader for each
843 part of the translated expression. */
845 static tree
846 phi_translate (tree expr, value_set_t set, basic_block pred,
847 basic_block phiblock)
849 tree phitrans = NULL;
850 tree oldexpr = expr;
852 if (expr == NULL)
853 return NULL;
855 if (is_gimple_min_invariant (expr))
856 return expr;
858 /* Phi translations of a given expression don't change. */
859 phitrans = phi_trans_lookup (expr, pred);
860 if (phitrans)
861 return phitrans;
863 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
865 case tcc_reference:
866 /* XXX: Until we have PRE of loads working, none will be ANTIC. */
867 return NULL;
869 case tcc_binary:
871 tree oldop1 = TREE_OPERAND (expr, 0);
872 tree oldop2 = TREE_OPERAND (expr, 1);
873 tree newop1;
874 tree newop2;
875 tree newexpr;
877 newop1 = phi_translate (find_leader (set, oldop1),
878 set, pred, phiblock);
879 if (newop1 == NULL)
880 return NULL;
881 newop2 = phi_translate (find_leader (set, oldop2),
882 set, pred, phiblock);
883 if (newop2 == NULL)
884 return NULL;
885 if (newop1 != oldop1 || newop2 != oldop2)
887 newexpr = pool_alloc (binary_node_pool);
888 memcpy (newexpr, expr, tree_size (expr));
889 create_tree_ann (newexpr);
890 TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
891 TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
892 vn_lookup_or_add (newexpr, NULL);
893 expr = newexpr;
894 phi_trans_add (oldexpr, newexpr, pred);
897 return expr;
899 case tcc_unary:
901 tree oldop1 = TREE_OPERAND (expr, 0);
902 tree newop1;
903 tree newexpr;
905 newop1 = phi_translate (find_leader (set, oldop1),
906 set, pred, phiblock);
907 if (newop1 == NULL)
908 return NULL;
909 if (newop1 != oldop1)
911 newexpr = pool_alloc (unary_node_pool);
912 memcpy (newexpr, expr, tree_size (expr));
913 create_tree_ann (newexpr);
914 TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
915 vn_lookup_or_add (newexpr, NULL);
916 expr = newexpr;
917 phi_trans_add (oldexpr, newexpr, pred);
920 return expr;
922 case tcc_exceptional:
924 tree phi = NULL;
925 int i;
926 gcc_assert (TREE_CODE (expr) == SSA_NAME);
927 if (TREE_CODE (SSA_NAME_DEF_STMT (expr)) == PHI_NODE)
928 phi = SSA_NAME_DEF_STMT (expr);
929 else
930 return expr;
932 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
933 if (PHI_ARG_EDGE (phi, i)->src == pred)
935 tree val;
936 if (is_undefined_value (PHI_ARG_DEF (phi, i)))
937 return NULL;
938 val = vn_lookup_or_add (PHI_ARG_DEF (phi, i), NULL);
939 return PHI_ARG_DEF (phi, i);
942 return expr;
944 default:
945 gcc_unreachable ();
949 static void
950 phi_translate_set (value_set_t dest, value_set_t set, basic_block pred,
951 basic_block phiblock)
953 value_set_node_t node;
954 for (node = set->head;
955 node;
956 node = node->next)
958 tree translated;
959 translated = phi_translate (node->expr, set, pred, phiblock);
960 phi_trans_add (node->expr, translated, pred);
962 if (translated != NULL)
963 value_insert_into_set (dest, translated);
967 /* Find the leader for a value (i.e., the name representing that
968 value) in a given set, and return it. Return NULL if no leader is
969 found. */
971 static tree
972 bitmap_find_leader (bitmap_set_t set, tree val)
974 if (val == NULL)
975 return NULL;
977 if (is_gimple_min_invariant (val))
978 return val;
979 if (bitmap_set_contains_value (set, val))
981 /* Rather than walk the entire bitmap of expressions, and see
982 whether any of them has the value we are looking for, we look
983 at the reverse mapping, which tells us the set of expressions
984 that have a given value (IE value->expressions with that
985 value) and see if any of those expressions are in our set.
986 The number of expressions per value is usually significantly
987 less than the number of expressions in the set. In fact, for
988 large testcases, doing it this way is roughly 5-10x faster
989 than walking the bitmap.
990 If this is somehow a significant lose for some cases, we can
991 choose which set to walk based on which set is smaller. */
992 value_set_t exprset;
993 value_set_node_t node;
994 exprset = VALUE_HANDLE_EXPR_SET (val);
995 for (node = exprset->head; node; node = node->next)
997 if (TREE_CODE (node->expr) == SSA_NAME)
999 if (bitmap_bit_p (set->expressions,
1000 SSA_NAME_VERSION (node->expr)))
1001 return node->expr;
1005 return NULL;
1009 /* Find the leader for a value (i.e., the name representing that
1010 value) in a given set, and return it. Return NULL if no leader is
1011 found. */
1013 static tree
1014 find_leader (value_set_t set, tree val)
1016 value_set_node_t node;
1018 if (val == NULL)
1019 return NULL;
1021 /* Constants represent themselves. */
1022 if (is_gimple_min_invariant (val))
1023 return val;
1025 if (set->length == 0)
1026 return NULL;
1028 if (value_exists_in_set_bitmap (set, val))
1030 for (node = set->head;
1031 node;
1032 node = node->next)
1034 if (get_value_handle (node->expr) == val)
1035 return node->expr;
1039 return NULL;
1042 /* Determine if the expression EXPR is valid in SET. This means that
1043 we have a leader for each part of the expression (if it consists of
1044 values), or the expression is an SSA_NAME.
1046 NB: We never should run into a case where we have SSA_NAME +
1047 SSA_NAME or SSA_NAME + value. The sets valid_in_set is called on,
1048 the ANTIC sets, will only ever have SSA_NAME's or binary value
1049 expression (IE VALUE1 + VALUE2) */
1051 static bool
1052 valid_in_set (value_set_t set, tree expr)
1054 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1056 case tcc_binary:
1058 tree op1 = TREE_OPERAND (expr, 0);
1059 tree op2 = TREE_OPERAND (expr, 1);
1060 return set_contains_value (set, op1) && set_contains_value (set, op2);
1063 case tcc_unary:
1065 tree op1 = TREE_OPERAND (expr, 0);
1066 return set_contains_value (set, op1);
1069 case tcc_reference:
1070 /* XXX: Until PRE of loads works, no reference nodes are ANTIC. */
1071 return false;
1073 case tcc_exceptional:
1074 gcc_assert (TREE_CODE (expr) == SSA_NAME);
1075 return true;
1077 default:
1078 /* No other cases should be encountered. */
1079 gcc_unreachable ();
1083 /* Clean the set of expressions that are no longer valid in SET. This
1084 means expressions that are made up of values we have no leaders for
1085 in SET. */
1087 static void
1088 clean (value_set_t set)
1090 value_set_node_t node;
1091 value_set_node_t next;
1092 node = set->head;
1093 while (node)
1095 next = node->next;
1096 if (!valid_in_set (set, node->expr))
1097 set_remove (set, node->expr);
1098 node = next;
1102 DEF_VEC_MALLOC_P (basic_block);
1104 /* Compute the ANTIC set for BLOCK.
1106 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK), if
1107 succs(BLOCK) > 1
1108 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)]) if
1109 succs(BLOCK) == 1
1111 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] -
1112 TMP_GEN[BLOCK])
1114 Iterate until fixpointed.
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. */
1122 static bool
1123 compute_antic_aux (basic_block block)
1125 basic_block son;
1126 edge e;
1127 bool changed = false;
1128 value_set_t S, old, ANTIC_OUT;
1129 value_set_node_t node;
1131 ANTIC_OUT = S = NULL;
1132 /* If any edges from predecessors are abnormal, antic_in is empty, so
1133 punt. Remember that the block has an incoming abnormal edge by
1134 setting the BB_VISITED flag. */
1135 if (! (block->flags & BB_VISITED))
1137 edge_iterator ei;
1138 FOR_EACH_EDGE (e, ei, block->preds)
1139 if (e->flags & EDGE_ABNORMAL)
1141 block->flags |= BB_VISITED;
1142 break;
1145 if (block->flags & BB_VISITED)
1147 S = NULL;
1148 goto visit_sons;
1152 old = set_new (false);
1153 set_copy (old, ANTIC_IN (block));
1154 ANTIC_OUT = set_new (true);
1156 /* If the block has no successors, ANTIC_OUT is empty, because it is
1157 the exit block. */
1158 if (EDGE_COUNT (block->succs) == 0);
1160 /* If we have one successor, we could have some phi nodes to
1161 translate through. */
1162 else if (EDGE_COUNT (block->succs) == 1)
1164 phi_translate_set (ANTIC_OUT, ANTIC_IN(EDGE_SUCC (block, 0)->dest),
1165 block, EDGE_SUCC (block, 0)->dest);
1167 /* If we have multiple successors, we take the intersection of all of
1168 them. */
1169 else
1171 VEC (basic_block) * worklist;
1172 edge e;
1173 size_t i;
1174 basic_block bprime, first;
1175 edge_iterator ei;
1177 worklist = VEC_alloc (basic_block, 2);
1178 FOR_EACH_EDGE (e, ei, block->succs)
1179 VEC_safe_push (basic_block, worklist, e->dest);
1180 first = VEC_index (basic_block, worklist, 0);
1181 set_copy (ANTIC_OUT, ANTIC_IN (first));
1183 for (i = 1; VEC_iterate (basic_block, worklist, i, bprime); i++)
1185 node = ANTIC_OUT->head;
1186 while (node)
1188 tree val;
1189 value_set_node_t next = node->next;
1190 val = get_value_handle (node->expr);
1191 if (!set_contains_value (ANTIC_IN (bprime), val))
1192 set_remove (ANTIC_OUT, node->expr);
1193 node = next;
1196 VEC_free (basic_block, worklist);
1199 /* Generate ANTIC_OUT - TMP_GEN. */
1200 S = bitmap_set_subtract_from_value_set (ANTIC_OUT, TMP_GEN (block), false);
1202 /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1203 ANTIC_IN (block) = bitmap_set_subtract_from_value_set (EXP_GEN (block),
1204 TMP_GEN (block),
1205 true);
1207 /* Then union in the ANTIC_OUT - TMP_GEN values, to get ANTIC_OUT U
1208 EXP_GEN - TMP_GEN */
1209 for (node = S->head;
1210 node;
1211 node = node->next)
1213 value_insert_into_set (ANTIC_IN (block), node->expr);
1215 clean (ANTIC_IN (block));
1218 if (!set_equal (old, ANTIC_IN (block)))
1219 changed = true;
1221 visit_sons:
1222 if (dump_file && (dump_flags & TDF_DETAILS))
1224 if (ANTIC_OUT)
1225 print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1226 print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1227 if (S)
1228 print_value_set (dump_file, S, "S", block->index);
1232 for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1233 son;
1234 son = next_dom_son (CDI_POST_DOMINATORS, son))
1236 changed |= compute_antic_aux (son);
1238 return changed;
1241 /* Compute ANTIC sets. */
1243 static void
1244 compute_antic (void)
1246 bool changed = true;
1247 basic_block bb;
1248 int num_iterations = 0;
1249 FOR_ALL_BB (bb)
1251 ANTIC_IN (bb) = set_new (true);
1252 gcc_assert (!(bb->flags & BB_VISITED));
1255 while (changed)
1257 num_iterations++;
1258 changed = false;
1259 changed = compute_antic_aux (EXIT_BLOCK_PTR);
1261 FOR_ALL_BB (bb)
1263 bb->flags &= ~BB_VISITED;
1265 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1266 fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1270 /* Find a leader for an expression, or generate one using
1271 create_expression_by_pieces if it's ANTIC but
1272 complex.
1273 BLOCK is the basic_block we are looking for leaders in.
1274 EXPR is the expression to find a leader or generate for.
1275 STMTS is the statement list to put the inserted expressions on.
1276 Returns the SSA_NAME of the LHS of the generated expression or the
1277 leader. */
1279 static tree
1280 find_or_generate_expression (basic_block block, tree expr, tree stmts)
1282 tree genop;
1283 genop = bitmap_find_leader (AVAIL_OUT (block), expr);
1284 /* Depending on the order we process DOM branches in, the value
1285 may not have propagated to all the dom children yet during
1286 this iteration. In this case, the value will always be in
1287 the NEW_SETS for us already, having been propagated from our
1288 dominator. */
1289 if (genop == NULL)
1290 genop = bitmap_find_leader (NEW_SETS (block), expr);
1291 /* If it's still NULL, see if it is a complex expression, and if
1292 so, generate it recursively, otherwise, abort, because it's
1293 not really . */
1294 if (genop == NULL)
1296 genop = VALUE_HANDLE_EXPR_SET (expr)->head->expr;
1297 gcc_assert (UNARY_CLASS_P (genop)
1298 || BINARY_CLASS_P (genop)
1299 || REFERENCE_CLASS_P (genop));
1300 genop = create_expression_by_pieces (block, genop, stmts);
1302 return genop;
1306 /* Create an expression in pieces, so that we can handle very complex
1307 expressions that may be ANTIC, but not necessary GIMPLE.
1308 BLOCK is the basic block the expression will be inserted into,
1309 EXPR is the expression to insert (in value form)
1310 STMTS is a statement list to append the necessary insertions into.
1312 This function will abort if we hit some value that shouldn't be
1313 ANTIC but is (IE there is no leader for it, or its components).
1314 This function may also generate expressions that are themselves
1315 partially or fully redundant. Those that are will be either made
1316 fully redundant during the next iteration of insert (for partially
1317 redundant ones), or eliminated by eliminate (for fully redundant
1318 ones). */
1320 static tree
1321 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
1323 tree name = NULL_TREE;
1324 tree newexpr = NULL_TREE;
1325 tree v;
1327 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1329 case tcc_binary:
1331 tree_stmt_iterator tsi;
1332 tree genop1, genop2;
1333 tree temp;
1334 tree op1 = TREE_OPERAND (expr, 0);
1335 tree op2 = TREE_OPERAND (expr, 1);
1336 genop1 = find_or_generate_expression (block, op1, stmts);
1337 genop2 = find_or_generate_expression (block, op2, stmts);
1338 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1339 add_referenced_tmp_var (temp);
1340 newexpr = build (TREE_CODE (expr), TREE_TYPE (expr),
1341 genop1, genop2);
1342 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1343 temp, newexpr);
1344 name = make_ssa_name (temp, newexpr);
1345 TREE_OPERAND (newexpr, 0) = name;
1346 tsi = tsi_last (stmts);
1347 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1348 pre_stats.insertions++;
1349 break;
1351 case tcc_unary:
1353 tree_stmt_iterator tsi;
1354 tree genop1;
1355 tree temp;
1356 tree op1 = TREE_OPERAND (expr, 0);
1357 genop1 = find_or_generate_expression (block, op1, stmts);
1358 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1359 add_referenced_tmp_var (temp);
1360 newexpr = build (TREE_CODE (expr), TREE_TYPE (expr),
1361 genop1);
1362 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1363 temp, newexpr);
1364 name = make_ssa_name (temp, newexpr);
1365 TREE_OPERAND (newexpr, 0) = name;
1366 tsi = tsi_last (stmts);
1367 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1368 pre_stats.insertions++;
1370 break;
1372 default:
1373 gcc_unreachable ();
1376 v = get_value_handle (expr);
1377 vn_add (name, v, NULL);
1378 bitmap_insert_into_set (NEW_SETS (block), name);
1379 bitmap_value_insert_into_set (AVAIL_OUT (block), name);
1380 if (dump_file && (dump_flags & TDF_DETAILS))
1382 fprintf (dump_file, "Inserted ");
1383 print_generic_expr (dump_file, newexpr, 0);
1384 fprintf (dump_file, " in predecessor %d\n", block->index);
1386 return name;
1389 /* Perform insertion of partially redundant values.
1390 For BLOCK, do the following:
1391 1. Propagate the NEW_SETS of the dominator into the current block.
1392 If the block has multiple predecessors,
1393 2a. Iterate over the ANTIC expressions for the block to see if
1394 any of them are partially redundant.
1395 2b. If so, insert them into the necessary predecessors to make
1396 the expression fully redundant.
1397 2c. Insert a new PHI merging the values of the predecessors.
1398 2d. Insert the new PHI, and the new expressions, into the
1399 NEW_SETS set.
1400 3. Recursively call ourselves on the dominator children of BLOCK.
1403 static bool
1404 insert_aux (basic_block block)
1406 basic_block son;
1407 bool new_stuff = false;
1409 if (block)
1411 basic_block dom;
1412 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1413 if (dom)
1415 unsigned i;
1416 bitmap_iterator bi;
1418 bitmap_set_t newset = NEW_SETS (dom);
1419 EXECUTE_IF_SET_IN_BITMAP (newset->expressions, 0, i, bi)
1421 bitmap_insert_into_set (NEW_SETS (block), ssa_name (i));
1422 bitmap_value_replace_in_set (AVAIL_OUT (block), ssa_name (i));
1424 if (EDGE_COUNT (block->preds) > 1)
1426 value_set_node_t node;
1427 for (node = ANTIC_IN (block)->head;
1428 node;
1429 node = node->next)
1431 if (BINARY_CLASS_P (node->expr)
1432 || UNARY_CLASS_P (node->expr))
1434 tree *avail;
1435 tree val;
1436 bool by_some = false;
1437 bool cant_insert = false;
1438 bool all_same = true;
1439 tree first_s = NULL;
1440 edge pred;
1441 basic_block bprime;
1442 tree eprime;
1443 edge_iterator ei;
1445 val = get_value_handle (node->expr);
1446 if (bitmap_set_contains_value (PHI_GEN (block), val))
1447 continue;
1448 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
1450 if (dump_file && (dump_flags & TDF_DETAILS))
1451 fprintf (dump_file, "Found fully redundant value\n");
1452 continue;
1455 avail = xcalloc (last_basic_block, sizeof (tree));
1456 FOR_EACH_EDGE (pred, ei, block->preds)
1458 tree vprime;
1459 tree edoubleprime;
1461 /* This can happen in the very weird case
1462 that our fake infinite loop edges have caused a
1463 critical edge to appear. */
1464 if (EDGE_CRITICAL_P (pred))
1466 cant_insert = true;
1467 break;
1469 bprime = pred->src;
1470 eprime = phi_translate (node->expr,
1471 ANTIC_IN (block),
1472 bprime, block);
1474 /* eprime will generally only be NULL if the
1475 value of the expression, translated
1476 through the PHI for this predecessor, is
1477 undefined. If that is the case, we can't
1478 make the expression fully redundant,
1479 because its value is undefined along a
1480 predecessor path. We can thus break out
1481 early because it doesn't matter what the
1482 rest of the results are. */
1483 if (eprime == NULL)
1485 cant_insert = true;
1486 break;
1489 vprime = get_value_handle (eprime);
1490 gcc_assert (vprime);
1491 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
1492 vprime);
1493 if (edoubleprime == NULL)
1495 avail[bprime->index] = eprime;
1496 all_same = false;
1498 else
1500 avail[bprime->index] = edoubleprime;
1501 by_some = true;
1502 if (first_s == NULL)
1503 first_s = edoubleprime;
1504 else if (first_s != edoubleprime)
1505 all_same = false;
1506 gcc_assert (first_s == edoubleprime
1507 || !operand_equal_p
1508 (first_s, edoubleprime, 0));
1511 /* If we can insert it, it's not the same value
1512 already existing along every predecessor, and
1513 it's defined by some predecessor, it is
1514 partially redundant. */
1515 if (!cant_insert && !all_same && by_some)
1517 tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
1518 tree temp;
1519 if (dump_file && (dump_flags & TDF_DETAILS))
1521 fprintf (dump_file, "Found partial redundancy for expression ");
1522 print_generic_expr (dump_file, node->expr, 0);
1523 fprintf (dump_file, "\n");
1526 /* Make the necessary insertions. */
1527 FOR_EACH_EDGE (pred, ei, block->preds)
1529 tree stmts = alloc_stmt_list ();
1530 tree builtexpr;
1531 bprime = pred->src;
1532 eprime = avail[bprime->index];
1533 if (BINARY_CLASS_P (eprime)
1534 || UNARY_CLASS_P (eprime))
1536 builtexpr = create_expression_by_pieces (bprime,
1537 eprime,
1538 stmts);
1539 bsi_insert_on_edge (pred, stmts);
1540 avail[bprime->index] = builtexpr;
1543 /* Now build a phi for the new variable. */
1544 temp = create_tmp_var (type, "prephitmp");
1545 add_referenced_tmp_var (temp);
1546 temp = create_phi_node (temp, block);
1547 vn_add (PHI_RESULT (temp), val, NULL);
1549 #if 0
1550 if (!set_contains_value (AVAIL_OUT (block), val))
1551 insert_into_set (AVAIL_OUT (block),
1552 PHI_RESULT (temp));
1553 else
1554 #endif
1555 bitmap_value_replace_in_set (AVAIL_OUT (block),
1556 PHI_RESULT (temp));
1557 FOR_EACH_EDGE (pred, ei, block->preds)
1559 add_phi_arg (&temp, avail[pred->src->index],
1560 pred);
1562 if (dump_file && (dump_flags & TDF_DETAILS))
1564 fprintf (dump_file, "Created phi ");
1565 print_generic_expr (dump_file, temp, 0);
1566 fprintf (dump_file, " in block %d\n", block->index);
1568 pre_stats.phis++;
1569 new_stuff = true;
1570 bitmap_insert_into_set (NEW_SETS (block),
1571 PHI_RESULT (temp));
1572 bitmap_insert_into_set (PHI_GEN (block),
1573 PHI_RESULT (temp));
1576 free (avail);
1582 for (son = first_dom_son (CDI_DOMINATORS, block);
1583 son;
1584 son = next_dom_son (CDI_DOMINATORS, son))
1586 new_stuff |= insert_aux (son);
1589 return new_stuff;
1592 /* Perform insertion of partially redundant values. */
1594 static void
1595 insert (void)
1597 bool new_stuff = true;
1598 basic_block bb;
1599 int num_iterations = 0;
1601 FOR_ALL_BB (bb)
1602 NEW_SETS (bb) = bitmap_set_new ();
1604 while (new_stuff)
1606 num_iterations++;
1607 new_stuff = false;
1608 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1610 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1611 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1615 /* Return true if VAR is an SSA variable with no defining statement in
1616 this procedure, *AND* isn't a live-on-entry parameter. */
1618 static bool
1619 is_undefined_value (tree expr)
1621 return (TREE_CODE (expr) == SSA_NAME
1622 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
1623 /* PARM_DECLs and hard registers are always defined. */
1624 && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL);
1628 /* Given an SSA variable VAR and an expression EXPR, compute the value
1629 number for EXPR and create a value handle (VAL) for it. If VAR and
1630 EXPR are not the same, associate VAL with VAR. Finally, add VAR to
1631 S1 and its value handle to S2.
1633 VUSES represent the virtual use operands associated with EXPR (if
1634 any). They are used when computing the hash value for EXPR. */
1636 static inline void
1637 add_to_sets (tree var, tree expr, vuse_optype vuses, bitmap_set_t s1,
1638 bitmap_set_t s2)
1640 tree val = vn_lookup_or_add (expr, vuses);
1642 /* VAR and EXPR may be the same when processing statements for which
1643 we are not computing value numbers (e.g., non-assignments, or
1644 statements that make aliased stores). In those cases, we are
1645 only interested in making VAR available as its own value. */
1646 if (var != expr)
1647 vn_add (var, val, NULL);
1649 bitmap_insert_into_set (s1, var);
1650 bitmap_value_insert_into_set (s2, var);
1654 /* Given a unary or binary expression EXPR, create and return a new
1655 expression with the same structure as EXPR but with its operands
1656 replaced with the value handles of each of the operands of EXPR.
1657 Insert EXPR's operands into the EXP_GEN set for BLOCK.
1659 VUSES represent the virtual use operands associated with EXPR (if
1660 any). They are used when computing the hash value for EXPR. */
1662 static inline tree
1663 create_value_expr_from (tree expr, basic_block block, vuse_optype vuses)
1665 int i;
1666 enum tree_code code = TREE_CODE (expr);
1667 tree vexpr;
1669 gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
1670 || TREE_CODE_CLASS (code) == tcc_binary
1671 || TREE_CODE_CLASS (code) == tcc_reference);
1673 if (TREE_CODE_CLASS (code) == tcc_unary)
1674 vexpr = pool_alloc (unary_node_pool);
1675 else if (TREE_CODE_CLASS (code) == tcc_reference)
1676 vexpr = pool_alloc (reference_node_pool);
1677 else
1678 vexpr = pool_alloc (binary_node_pool);
1680 memcpy (vexpr, expr, tree_size (expr));
1682 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
1684 tree op = TREE_OPERAND (expr, i);
1685 if (op != NULL)
1687 tree val = vn_lookup_or_add (op, vuses);
1688 if (!is_undefined_value (op))
1689 value_insert_into_set (EXP_GEN (block), op);
1690 if (TREE_CODE (val) == VALUE_HANDLE)
1691 TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
1692 TREE_OPERAND (vexpr, i) = val;
1696 return vexpr;
1700 /* Compute the AVAIL set for BLOCK.
1701 This function performs value numbering of the statements in BLOCK.
1702 The AVAIL sets are built from information we glean while doing this
1703 value numbering, since the AVAIL sets contain only one entry per
1704 value.
1706 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
1707 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
1709 static void
1710 compute_avail (basic_block block)
1712 basic_block son;
1714 /* For arguments with default definitions, we pretend they are
1715 defined in the entry block. */
1716 if (block == ENTRY_BLOCK_PTR)
1718 tree param;
1719 for (param = DECL_ARGUMENTS (current_function_decl);
1720 param;
1721 param = TREE_CHAIN (param))
1723 if (default_def (param) != NULL)
1725 tree val;
1726 tree def = default_def (param);
1727 val = vn_lookup_or_add (def, NULL);
1728 bitmap_insert_into_set (TMP_GEN (block), def);
1729 bitmap_value_insert_into_set (AVAIL_OUT (block), def);
1733 else if (block)
1735 block_stmt_iterator bsi;
1736 tree stmt, phi;
1737 basic_block dom;
1739 /* Initially, the set of available values in BLOCK is that of
1740 its immediate dominator. */
1741 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1742 if (dom)
1743 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
1745 /* Generate values for PHI nodes. */
1746 for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
1747 /* We have no need for virtual phis, as they don't represent
1748 actual computations. */
1749 if (is_gimple_reg (PHI_RESULT (phi)))
1750 add_to_sets (PHI_RESULT (phi), PHI_RESULT (phi), NULL,
1751 PHI_GEN (block), AVAIL_OUT (block));
1753 /* Now compute value numbers and populate value sets with all
1754 the expressions computed in BLOCK. */
1755 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
1757 stmt_ann_t ann;
1758 size_t j;
1760 stmt = bsi_stmt (bsi);
1761 ann = stmt_ann (stmt);
1762 get_stmt_operands (stmt);
1764 /* We are only interested in assignments of the form
1765 X_i = EXPR, where EXPR represents an "interesting"
1766 computation, it has no volatile operands and X_i
1767 doesn't flow through an abnormal edge. */
1768 if (TREE_CODE (stmt) == MODIFY_EXPR
1769 && !ann->has_volatile_ops
1770 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
1771 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (stmt, 0)))
1773 tree lhs = TREE_OPERAND (stmt, 0);
1774 tree rhs = TREE_OPERAND (stmt, 1);
1775 vuse_optype vuses = STMT_VUSE_OPS (stmt);
1777 STRIP_USELESS_TYPE_CONVERSION (rhs);
1778 if (TREE_CODE (rhs) == SSA_NAME
1779 || is_gimple_min_invariant (rhs))
1781 /* Compute a value number for the RHS of the statement
1782 and add its value to the AVAIL_OUT set for the block.
1783 Add the LHS to TMP_GEN. */
1784 add_to_sets (lhs, rhs, vuses, TMP_GEN (block),
1785 AVAIL_OUT (block));
1787 if (TREE_CODE (rhs) == SSA_NAME
1788 && !is_undefined_value (rhs))
1789 value_insert_into_set (EXP_GEN (block), rhs);
1790 continue;
1792 else if (UNARY_CLASS_P (rhs) || BINARY_CLASS_P (rhs)
1793 || TREE_CODE (rhs) == INDIRECT_REF)
1795 /* For binary, unary, and reference expressions,
1796 create a duplicate expression with the operands
1797 replaced with the value handles of the original
1798 RHS. */
1799 tree newt = create_value_expr_from (rhs, block, vuses);
1800 add_to_sets (lhs, newt, vuses, TMP_GEN (block),
1801 AVAIL_OUT (block));
1802 value_insert_into_set (EXP_GEN (block), newt);
1803 continue;
1807 /* For any other statement that we don't recognize, simply
1808 make the names generated by the statement available in
1809 AVAIL_OUT and TMP_GEN. */
1810 for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1812 tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1813 add_to_sets (def, def, NULL, TMP_GEN (block),
1814 AVAIL_OUT (block));
1817 for (j = 0; j < NUM_USES (STMT_USE_OPS (stmt)); j++)
1819 tree use = USE_OP (STMT_USE_OPS (stmt), j);
1820 add_to_sets (use, use, NULL, TMP_GEN (block),
1821 AVAIL_OUT (block));
1826 /* Compute available sets for the dominator children of BLOCK. */
1827 for (son = first_dom_son (CDI_DOMINATORS, block);
1828 son;
1829 son = next_dom_son (CDI_DOMINATORS, son))
1830 compute_avail (son);
1834 /* Eliminate fully redundant computations. */
1836 static void
1837 eliminate (void)
1839 basic_block b;
1841 FOR_EACH_BB (b)
1843 block_stmt_iterator i;
1845 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
1847 tree stmt = bsi_stmt (i);
1849 /* Lookup the RHS of the expression, see if we have an
1850 available computation for it. If so, replace the RHS with
1851 the available computation. */
1852 if (TREE_CODE (stmt) == MODIFY_EXPR
1853 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
1854 && TREE_CODE (TREE_OPERAND (stmt ,1)) != SSA_NAME
1855 && !is_gimple_min_invariant (TREE_OPERAND (stmt, 1))
1856 && !stmt_ann (stmt)->has_volatile_ops)
1858 tree lhs = TREE_OPERAND (stmt, 0);
1859 tree *rhs_p = &TREE_OPERAND (stmt, 1);
1860 tree sprime;
1862 sprime = bitmap_find_leader (AVAIL_OUT (b),
1863 vn_lookup (lhs, NULL));
1864 if (sprime
1865 && sprime != lhs
1866 && (TREE_CODE (*rhs_p) != SSA_NAME
1867 || may_propagate_copy (*rhs_p, sprime)))
1869 gcc_assert (sprime != *rhs_p);
1871 if (dump_file && (dump_flags & TDF_DETAILS))
1873 fprintf (dump_file, "Replaced ");
1874 print_generic_expr (dump_file, *rhs_p, 0);
1875 fprintf (dump_file, " with ");
1876 print_generic_expr (dump_file, sprime, 0);
1877 fprintf (dump_file, " in ");
1878 print_generic_stmt (dump_file, stmt, 0);
1880 pre_stats.eliminations++;
1881 propagate_tree_value (rhs_p, sprime);
1882 modify_stmt (stmt);
1884 /* If we removed EH side effects from the statement, clean
1885 its EH information. */
1886 if (maybe_clean_eh_stmt (stmt))
1888 bitmap_set_bit (need_eh_cleanup,
1889 bb_for_stmt (stmt)->index);
1890 if (dump_file && (dump_flags & TDF_DETAILS))
1891 fprintf (dump_file, " Removed EH side effects.\n");
1900 /* Initialize data structures used by PRE. */
1902 static void
1903 init_pre (void)
1905 basic_block bb;
1907 connect_infinite_loops_to_exit ();
1908 vn_init ();
1909 memset (&pre_stats, 0, sizeof (pre_stats));
1911 /* If block 0 has more than one predecessor, it means that its PHI
1912 nodes will have arguments coming from block -1. This creates
1913 problems for several places in PRE that keep local arrays indexed
1914 by block number. To prevent this, we split the edge coming from
1915 ENTRY_BLOCK_PTR (FIXME, if ENTRY_BLOCK_PTR had an index number
1916 different than -1 we wouldn't have to hack this. tree-ssa-dce.c
1917 needs a similar change). */
1918 if (EDGE_COUNT (EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->dest->preds) > 1)
1919 if (!(EDGE_SUCC (ENTRY_BLOCK_PTR, 0)->flags & EDGE_ABNORMAL))
1920 split_edge (EDGE_SUCC (ENTRY_BLOCK_PTR, 0));
1922 FOR_ALL_BB (bb)
1923 bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
1925 bitmap_obstack_initialize (&grand_bitmap_obstack);
1926 phi_translate_table = htab_create (511, expr_pred_trans_hash,
1927 expr_pred_trans_eq, free);
1928 value_set_pool = create_alloc_pool ("Value sets",
1929 sizeof (struct value_set), 30);
1930 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
1931 sizeof (struct bitmap_set), 30);
1932 value_set_node_pool = create_alloc_pool ("Value set nodes",
1933 sizeof (struct value_set_node), 30);
1934 calculate_dominance_info (CDI_POST_DOMINATORS);
1935 calculate_dominance_info (CDI_DOMINATORS);
1936 binary_node_pool = create_alloc_pool ("Binary tree nodes",
1937 tree_code_size (PLUS_EXPR), 30);
1938 unary_node_pool = create_alloc_pool ("Unary tree nodes",
1939 tree_code_size (NEGATE_EXPR), 30);
1940 reference_node_pool = create_alloc_pool ("Reference tree nodes",
1941 tree_code_size (ARRAY_REF), 30);
1942 FOR_ALL_BB (bb)
1944 EXP_GEN (bb) = set_new (true);
1945 PHI_GEN (bb) = bitmap_set_new ();
1946 TMP_GEN (bb) = bitmap_set_new ();
1947 AVAIL_OUT (bb) = bitmap_set_new ();
1950 need_eh_cleanup = BITMAP_XMALLOC ();
1954 /* Deallocate data structures used by PRE. */
1956 static void
1957 fini_pre (void)
1959 basic_block bb;
1960 unsigned int i;
1962 bsi_commit_edge_inserts ();
1964 bitmap_obstack_release (&grand_bitmap_obstack);
1965 free_alloc_pool (value_set_pool);
1966 free_alloc_pool (bitmap_set_pool);
1967 free_alloc_pool (value_set_node_pool);
1968 free_alloc_pool (binary_node_pool);
1969 free_alloc_pool (reference_node_pool);
1970 free_alloc_pool (unary_node_pool);
1971 htab_delete (phi_translate_table);
1972 remove_fake_exit_edges ();
1974 FOR_ALL_BB (bb)
1976 free (bb->aux);
1977 bb->aux = NULL;
1980 free_dominance_info (CDI_POST_DOMINATORS);
1981 vn_delete ();
1983 if (!bitmap_empty_p (need_eh_cleanup))
1985 tree_purge_all_dead_eh_edges (need_eh_cleanup);
1986 cleanup_tree_cfg ();
1989 BITMAP_XFREE (need_eh_cleanup);
1991 /* Wipe out pointers to VALUE_HANDLEs. In the not terribly distant
1992 future we will want them to be persistent though. */
1993 for (i = 0; i < num_ssa_names; i++)
1995 tree name = ssa_name (i);
1997 if (!name)
1998 continue;
2000 if (SSA_NAME_VALUE (name)
2001 && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
2002 SSA_NAME_VALUE (name) = NULL;
2007 /* Main entry point to the SSA-PRE pass. DO_FRE is true if the caller
2008 only wants to do full redundancy elimination. */
2010 static void
2011 execute_pre (bool do_fre)
2013 init_pre ();
2015 /* Collect and value number expressions computed in each basic
2016 block. */
2017 compute_avail (ENTRY_BLOCK_PTR);
2019 if (dump_file && (dump_flags & TDF_DETAILS))
2021 basic_block bb;
2023 FOR_ALL_BB (bb)
2025 print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
2026 bitmap_print_value_set (dump_file, TMP_GEN (bb), "tmp_gen",
2027 bb->index);
2028 bitmap_print_value_set (dump_file, AVAIL_OUT (bb), "avail_out",
2029 bb->index);
2033 /* Insert can get quite slow on an incredibly large number of basic
2034 blocks due to some quadratic behavior. Until this behavior is
2035 fixed, don't run it when he have an incredibly large number of
2036 bb's. If we aren't going to run insert, there is no point in
2037 computing ANTIC, either, even though it's plenty fast. */
2038 if (!do_fre && n_basic_blocks < 4000)
2040 compute_antic ();
2041 insert ();
2044 /* Remove all the redundant expressions. */
2045 eliminate ();
2047 if (dump_file && (dump_flags & TDF_STATS))
2049 fprintf (dump_file, "Insertions:%d\n", pre_stats.insertions);
2050 fprintf (dump_file, "New PHIs:%d\n", pre_stats.phis);
2051 fprintf (dump_file, "Eliminated:%d\n", pre_stats.eliminations);
2054 fini_pre ();
2058 /* Gate and execute functions for PRE. */
2060 static void
2061 do_pre (void)
2063 execute_pre (false);
2066 static bool
2067 gate_pre (void)
2069 return flag_tree_pre != 0;
2072 struct tree_opt_pass pass_pre =
2074 "pre", /* name */
2075 gate_pre, /* gate */
2076 do_pre, /* execute */
2077 NULL, /* sub */
2078 NULL, /* next */
2079 0, /* static_pass_number */
2080 TV_TREE_PRE, /* tv_id */
2081 PROP_no_crit_edges | PROP_cfg
2082 | PROP_ssa | PROP_alias, /* properties_required */
2083 0, /* properties_provided */
2084 0, /* properties_destroyed */
2085 0, /* todo_flags_start */
2086 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
2087 0 /* letter */
2091 /* Gate and execute functions for FRE. */
2093 static void
2094 do_fre (void)
2096 execute_pre (true);
2099 static bool
2100 gate_fre (void)
2102 return flag_tree_fre != 0;
2105 struct tree_opt_pass pass_fre =
2107 "fre", /* name */
2108 gate_fre, /* gate */
2109 do_fre, /* execute */
2110 NULL, /* sub */
2111 NULL, /* next */
2112 0, /* static_pass_number */
2113 TV_TREE_FRE, /* tv_id */
2114 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2115 0, /* properties_provided */
2116 0, /* properties_destroyed */
2117 0, /* todo_flags_start */
2118 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
2119 0 /* letter */