2004-08-20 Daniel Berlin <dberlin@dberlin.org>
[official-gcc.git] / gcc / tree-ssa-pre.c
blob1267a546b0b7499d20e24ee572f6243cb8c315ed
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;
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 #ifdef ENABLE_CHECKING
447 if (!set->indexed)
448 abort ();
449 #endif
451 if (!set->values)
452 return;
454 bitmap_clear_bit (set->values, VALUE_HANDLE_ID (v));
458 /* Insert the value number V into the bitmap of values existing in
459 SET. */
461 static inline void
462 value_insert_into_set_bitmap (value_set_t set, tree v)
464 #ifdef ENABLE_CHECKING
465 if (!set->indexed)
466 abort ();
467 #endif
469 if (set->values == NULL)
471 set->values = BITMAP_GGC_ALLOC ();
472 bitmap_clear (set->values);
475 bitmap_set_bit (set->values, VALUE_HANDLE_ID (v));
479 /* Create a new bitmap set and return it. */
481 static bitmap_set_t
482 bitmap_set_new (void)
484 bitmap_set_t ret = pool_alloc (bitmap_set_pool);
485 ret->expressions = BITMAP_GGC_ALLOC ();
486 ret->values = BITMAP_GGC_ALLOC ();
487 bitmap_clear (ret->expressions);
488 bitmap_clear (ret->values);
489 return ret;
492 /* Create a new set. */
494 static value_set_t
495 set_new (bool indexed)
497 value_set_t ret;
498 ret = pool_alloc (value_set_pool);
499 ret->head = ret->tail = NULL;
500 ret->length = 0;
501 ret->indexed = indexed;
502 ret->values = NULL;
503 return ret;
506 /* Insert an expression EXPR into a bitmapped set. */
508 static void
509 bitmap_insert_into_set (bitmap_set_t set, tree expr)
511 tree val;
512 /* XXX: For now, we only let SSA_NAMES into the bitmap sets. */
513 if (TREE_CODE (expr) != SSA_NAME)
514 abort ();
515 val = get_value_handle (expr);
517 if (val == NULL)
518 abort ();
519 if (!is_gimple_min_invariant (val))
520 bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
521 bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
524 /* Insert EXPR into SET. */
526 static void
527 insert_into_set (value_set_t set, tree expr)
529 value_set_node_t newnode = pool_alloc (value_set_node_pool);
530 tree val = get_value_handle (expr);
532 if (val == NULL)
533 abort ();
535 /* For indexed sets, insert the value into the set value bitmap.
536 For all sets, add it to the linked list and increment the list
537 length. */
538 if (set->indexed)
539 value_insert_into_set_bitmap (set, val);
541 newnode->next = NULL;
542 newnode->expr = expr;
543 set->length ++;
544 if (set->head == NULL)
546 set->head = set->tail = newnode;
548 else
550 set->tail->next = newnode;
551 set->tail = newnode;
555 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
557 static void
558 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
560 bitmap_copy (dest->expressions, orig->expressions);
561 bitmap_copy (dest->values, orig->values);
564 /* Copy the set ORIG to the set DEST. */
566 static void
567 set_copy (value_set_t dest, value_set_t orig)
569 value_set_node_t node;
571 if (!orig || !orig->head)
572 return;
574 for (node = orig->head;
575 node;
576 node = node->next)
578 insert_into_set (dest, node->expr);
582 /* Remove EXPR from SET. */
584 static void
585 set_remove (value_set_t set, tree expr)
587 value_set_node_t node, prev;
589 /* Remove the value of EXPR from the bitmap, decrement the set
590 length, and remove it from the actual double linked list. */
591 value_remove_from_set_bitmap (set, get_value_handle (expr));
592 set->length--;
593 prev = NULL;
594 for (node = set->head;
595 node != NULL;
596 prev = node, node = node->next)
598 if (node->expr == expr)
600 if (prev == NULL)
601 set->head = node->next;
602 else
603 prev->next= node->next;
605 if (node == set->tail)
606 set->tail = prev;
607 pool_free (value_set_node_pool, node);
608 return;
613 /* Return true if SET contains the value VAL. */
615 static bool
616 set_contains_value (value_set_t set, tree val)
618 /* All constants are in every set. */
619 if (is_gimple_min_invariant (val))
620 return true;
622 if (set->length == 0)
623 return false;
625 return value_exists_in_set_bitmap (set, val);
628 /* Return true if bitmapped set SET contains the expression EXPR. */
629 static bool
630 bitmap_set_contains (bitmap_set_t set, tree expr)
632 /* All constants are in every set. */
633 if (is_gimple_min_invariant (get_value_handle (expr)))
634 return true;
636 /* XXX: Bitmapped sets only contain SSA_NAME's for now. */
637 if (TREE_CODE (expr) != SSA_NAME)
638 return false;
639 return bitmap_bit_p (set->expressions, SSA_NAME_VERSION (expr));
643 /* Return true if bitmapped set SET contains the value VAL. */
645 static bool
646 bitmap_set_contains_value (bitmap_set_t set, tree val)
648 if (is_gimple_min_invariant (val))
649 return true;
650 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
653 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
655 static void
656 bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
658 value_set_t exprset;
659 value_set_node_t node;
660 if (is_gimple_min_invariant (lookfor))
661 return;
662 if (!bitmap_set_contains_value (set, lookfor))
663 return;
664 /* The number of expressions having a given value is usually
665 significantly less than the total number of expressions in SET.
666 Thus, rather than check, for each expression in SET, whether it
667 has the value LOOKFOR, we walk the reverse mapping that tells us
668 what expressions have a given value, and see if any of those
669 expressions are in our set. For large testcases, this is about
670 5-10x faster than walking the bitmap. If this is somehow a
671 significant lose for some cases, we can choose which set to walk
672 based on the set size. */
673 exprset = VALUE_HANDLE_EXPR_SET (lookfor);
674 for (node = exprset->head; node; node = node->next)
676 if (TREE_CODE (node->expr) == SSA_NAME)
678 if (bitmap_bit_p (set->expressions, SSA_NAME_VERSION (node->expr)))
680 bitmap_clear_bit (set->expressions, SSA_NAME_VERSION (node->expr));
681 bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
682 return;
688 /* Subtract bitmapped set B from value set A, and return the new set. */
690 static value_set_t
691 bitmap_set_subtract_from_value_set (value_set_t a, bitmap_set_t b,
692 bool indexed)
694 value_set_t ret = set_new (indexed);
695 value_set_node_t node;
696 for (node = a->head;
697 node;
698 node = node->next)
700 if (!bitmap_set_contains (b, node->expr))
701 insert_into_set (ret, node->expr);
703 return ret;
706 /* Return true if two sets are equal. */
708 static bool
709 set_equal (value_set_t a, value_set_t b)
711 value_set_node_t node;
713 if (a->length != b->length)
714 return false;
715 for (node = a->head;
716 node;
717 node = node->next)
719 if (!set_contains_value (b, get_value_handle (node->expr)))
720 return false;
722 return true;
725 /* Replace an instance of EXPR's VALUE with EXPR in SET. */
727 static void
728 bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
730 tree val = get_value_handle (expr);
731 bitmap_set_replace_value (set, val, expr);
734 /* Insert EXPR into SET if EXPR's value is not already present in
735 SET. */
737 static void
738 bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
740 tree val = get_value_handle (expr);
742 if (is_gimple_min_invariant (val))
743 return;
745 if (!bitmap_set_contains_value (set, val))
746 bitmap_insert_into_set (set, expr);
749 /* Insert the value for EXPR into SET, if it doesn't exist already. */
751 static void
752 value_insert_into_set (value_set_t set, tree expr)
754 tree val = get_value_handle (expr);
756 /* Constant and invariant values exist everywhere, and thus,
757 actually keeping them in the sets is pointless. */
758 if (is_gimple_min_invariant (val))
759 return;
761 if (!set_contains_value (set, val))
762 insert_into_set (set, expr);
766 /* Print out SET to OUTFILE. */
768 static void
769 bitmap_print_value_set (FILE *outfile, bitmap_set_t set,
770 const char *setname, int blockindex)
772 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
773 if (set)
775 int i;
776 EXECUTE_IF_SET_IN_BITMAP (set->expressions, 0, i,
778 print_generic_expr (outfile, ssa_name (i), 0);
780 fprintf (outfile, " (");
781 print_generic_expr (outfile, get_value_handle (ssa_name (i)), 0);
782 fprintf (outfile, ") ");
783 if (bitmap_last_set_bit (set->expressions) != i)
784 fprintf (outfile, ", ");
787 fprintf (outfile, " }\n");
789 /* Print out the value_set SET to OUTFILE. */
791 static void
792 print_value_set (FILE *outfile, value_set_t set,
793 const char *setname, int blockindex)
795 value_set_node_t node;
796 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
797 if (set)
799 for (node = set->head;
800 node;
801 node = node->next)
803 print_generic_expr (outfile, node->expr, 0);
805 fprintf (outfile, " (");
806 print_generic_expr (outfile, get_value_handle (node->expr), 0);
807 fprintf (outfile, ") ");
809 if (node->next)
810 fprintf (outfile, ", ");
814 fprintf (outfile, " }\n");
817 /* Print out the expressions that have VAL to OUTFILE. */
819 void
820 print_value_expressions (FILE *outfile, tree val)
822 if (VALUE_HANDLE_EXPR_SET (val))
824 char s[10];
825 sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
826 print_value_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
831 void
832 debug_value_expressions (tree val)
834 print_value_expressions (stderr, val);
838 void debug_value_set (value_set_t, const char *, int);
840 void
841 debug_value_set (value_set_t set, const char *setname, int blockindex)
843 print_value_set (stderr, set, setname, blockindex);
846 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
847 the phis in PRED. Return NULL if we can't find a leader for each
848 part of the translated expression. */
850 static tree
851 phi_translate (tree expr, value_set_t set, basic_block pred,
852 basic_block phiblock)
854 tree phitrans = NULL;
855 tree oldexpr = expr;
857 if (expr == NULL)
858 return NULL;
860 /* Phi translations of a given expression don't change, */
861 phitrans = phi_trans_lookup (expr, pred);
862 if (phitrans)
863 return phitrans;
866 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
868 case '2':
870 tree oldop1 = TREE_OPERAND (expr, 0);
871 tree oldop2 = TREE_OPERAND (expr, 1);
872 tree newop1;
873 tree newop2;
874 tree newexpr;
876 newop1 = phi_translate (find_leader (set, oldop1),
877 set, pred, phiblock);
878 if (newop1 == NULL)
879 return NULL;
880 newop2 = phi_translate (find_leader (set, oldop2),
881 set, pred, phiblock);
882 if (newop2 == NULL)
883 return NULL;
884 if (newop1 != oldop1 || newop2 != oldop2)
886 newexpr = pool_alloc (binary_node_pool);
887 memcpy (newexpr, expr, tree_size (expr));
888 create_tree_ann (newexpr);
889 TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
890 TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
891 vn_lookup_or_add (newexpr, NULL);
892 expr = newexpr;
893 phi_trans_add (oldexpr, newexpr, pred);
896 break;
897 /* XXX: Until we have PRE of loads working, none will be ANTIC.
899 case 'r':
900 return NULL;
901 break;
902 case '1':
904 tree oldop1 = TREE_OPERAND (expr, 0);
905 tree newop1;
906 tree newexpr;
908 newop1 = phi_translate (find_leader (set, oldop1),
909 set, pred, phiblock);
910 if (newop1 == NULL)
911 return NULL;
912 if (newop1 != oldop1)
914 newexpr = pool_alloc (unary_node_pool);
915 memcpy (newexpr, expr, tree_size (expr));
916 create_tree_ann (newexpr);
917 TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
918 vn_lookup_or_add (newexpr, NULL);
919 expr = newexpr;
920 phi_trans_add (oldexpr, newexpr, pred);
923 break;
924 case 'd':
925 abort ();
926 case 'x':
928 tree phi = NULL;
929 int i;
930 if (TREE_CODE (expr) != SSA_NAME)
931 abort ();
932 if (TREE_CODE (SSA_NAME_DEF_STMT (expr)) == PHI_NODE)
933 phi = SSA_NAME_DEF_STMT (expr);
934 else
935 return expr;
937 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
938 if (PHI_ARG_EDGE (phi, i)->src == pred)
940 tree val;
941 if (is_undefined_value (PHI_ARG_DEF (phi, i)))
942 return NULL;
943 val = vn_lookup_or_add (PHI_ARG_DEF (phi, i), NULL);
944 return PHI_ARG_DEF (phi, i);
947 break;
949 return expr;
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 '2':
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);
1065 break;
1066 case '1':
1068 tree op1 = TREE_OPERAND (expr, 0);
1069 return set_contains_value (set, op1);
1071 break;
1072 /* XXX: Until PRE of loads works, no reference nodes are ANTIC.
1074 case 'r':
1076 return false;
1078 case 'x':
1080 if (TREE_CODE (expr) == SSA_NAME)
1081 return true;
1082 abort ();
1084 case 'c':
1085 abort ();
1087 return false;
1090 /* Clean the set of expressions that are no longer valid in SET. This
1091 means expressions that are made up of values we have no leaders for
1092 in SET. */
1094 static void
1095 clean (value_set_t set)
1097 value_set_node_t node;
1098 value_set_node_t next;
1099 node = set->head;
1100 while (node)
1102 next = node->next;
1103 if (!valid_in_set (set, node->expr))
1104 set_remove (set, node->expr);
1105 node = next;
1109 /* Compute the ANTIC set for BLOCK.
1111 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK), if
1112 succs(BLOCK) > 1
1113 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)]) if
1114 succs(BLOCK) == 1
1116 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] -
1117 TMP_GEN[BLOCK])
1119 Iterate until fixpointed.
1121 XXX: It would be nice to either write a set_clear, and use it for
1122 antic_out, or to mark the antic_out set as deleted at the end
1123 of this routine, so that the pool can hand the same memory back out
1124 again for the next antic_out. */
1127 static bool
1128 compute_antic_aux (basic_block block)
1130 basic_block son;
1131 edge e;
1132 bool changed = false;
1133 value_set_t S, old, ANTIC_OUT;
1134 value_set_node_t node;
1136 ANTIC_OUT = S = NULL;
1137 /* If any edges from predecessors are abnormal, antic_in is empty, so
1138 punt. Remember that the block has an incoming abnormal edge by
1139 setting the BB_VISITED flag. */
1140 if (! (block->flags & BB_VISITED))
1142 for (e = block->pred; e; e = e->pred_next)
1143 if (e->flags & EDGE_ABNORMAL)
1145 block->flags |= BB_VISITED;
1146 break;
1149 if (block->flags & BB_VISITED)
1151 S = NULL;
1152 goto visit_sons;
1156 old = set_new (false);
1157 set_copy (old, ANTIC_IN (block));
1158 ANTIC_OUT = set_new (true);
1160 /* If the block has no successors, ANTIC_OUT is empty, because it is
1161 the exit block. */
1162 if (block->succ == NULL);
1164 /* If we have one successor, we could have some phi nodes to
1165 translate through. */
1166 else if (block->succ->succ_next == NULL)
1168 phi_translate_set (ANTIC_OUT, ANTIC_IN(block->succ->dest),
1169 block, block->succ->dest);
1171 /* If we have multiple successors, we take the intersection of all of
1172 them. */
1173 else
1175 varray_type worklist;
1176 edge e;
1177 size_t i;
1178 basic_block bprime, first;
1180 VARRAY_BB_INIT (worklist, 1, "succ");
1181 e = block->succ;
1182 while (e)
1184 VARRAY_PUSH_BB (worklist, e->dest);
1185 e = e->succ_next;
1187 first = VARRAY_BB (worklist, 0);
1188 set_copy (ANTIC_OUT, ANTIC_IN (first));
1190 for (i = 1; i < VARRAY_ACTIVE_SIZE (worklist); i++)
1192 bprime = VARRAY_BB (worklist, i);
1193 node = ANTIC_OUT->head;
1194 while (node)
1196 tree val;
1197 value_set_node_t next = node->next;
1198 val = get_value_handle (node->expr);
1199 if (!set_contains_value (ANTIC_IN (bprime), val))
1200 set_remove (ANTIC_OUT, node->expr);
1201 node = next;
1204 VARRAY_CLEAR (worklist);
1207 /* Generate ANTIC_OUT - TMP_GEN */
1208 S = bitmap_set_subtract_from_value_set (ANTIC_OUT, TMP_GEN (block), false);
1210 /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1211 ANTIC_IN (block) = bitmap_set_subtract_from_value_set (EXP_GEN (block),
1212 TMP_GEN (block),
1213 true);
1215 /* Then union in the ANTIC_OUT - TMP_GEN values, to get ANTIC_OUT U
1216 EXP_GEN - TMP_GEN */
1217 for (node = S->head;
1218 node;
1219 node = node->next)
1221 value_insert_into_set (ANTIC_IN (block), node->expr);
1223 clean (ANTIC_IN (block));
1226 if (!set_equal (old, ANTIC_IN (block)))
1227 changed = true;
1229 visit_sons:
1230 if (dump_file && (dump_flags & TDF_DETAILS))
1232 if (ANTIC_OUT)
1233 print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1234 print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1235 if (S)
1236 print_value_set (dump_file, S, "S", block->index);
1240 for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1241 son;
1242 son = next_dom_son (CDI_POST_DOMINATORS, son))
1244 changed |= compute_antic_aux (son);
1246 return changed;
1249 /* Compute ANTIC sets. */
1251 static void
1252 compute_antic (void)
1254 bool changed = true;
1255 basic_block bb;
1256 int num_iterations = 0;
1257 FOR_ALL_BB (bb)
1259 ANTIC_IN (bb) = set_new (true);
1260 if (bb->flags & BB_VISITED)
1261 abort ();
1264 while (changed)
1266 num_iterations++;
1267 changed = false;
1268 changed = compute_antic_aux (EXIT_BLOCK_PTR);
1270 FOR_ALL_BB (bb)
1272 bb->flags &= ~BB_VISITED;
1274 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1275 fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1279 /* Find a leader for an expression, or generate one using
1280 create_expression_by_pieces if it's ANTIC but
1281 complex.
1282 BLOCK is the basic_block we are looking for leaders in.
1283 EXPR is the expression to find a leader or generate for.
1284 STMTS is the statement list to put the inserted expressions on.
1285 Returns the SSA_NAME of the LHS of the generated expression or the
1286 leader. */
1288 static tree
1289 find_or_generate_expression (basic_block block, tree expr, tree stmts)
1291 tree genop;
1292 genop = bitmap_find_leader (AVAIL_OUT (block), expr);
1293 /* Depending on the order we process DOM branches in, the value
1294 may not have propagated to all the dom children yet during
1295 this iteration. In this case, the value will always be in
1296 the NEW_SETS for us already, having been propagated from our
1297 dominator. */
1298 if (genop == NULL)
1299 genop = bitmap_find_leader (NEW_SETS (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 if (TREE_CODE_CLASS (TREE_CODE (genop)) != '1'
1307 && TREE_CODE_CLASS (TREE_CODE (genop)) != '2'
1308 && TREE_CODE_CLASS (TREE_CODE (genop)) != 'r')
1309 abort ();
1310 genop = create_expression_by_pieces (block, genop, stmts);
1312 return genop;
1316 /* Create an expression in pieces, so that we can handle very complex
1317 expressions that may be ANTIC, but not necessary GIMPLE.
1318 BLOCK is the basic block the expression will be inserted into,
1319 EXPR is the expression to insert (in value form)
1320 STMTS is a statement list to append the necessary insertions into.
1322 This function will abort if we hit some value that shouldn't be
1323 ANTIC but is (IE there is no leader for it, or its components).
1324 This function may also generate expressions that are themselves
1325 partially or fully redundant. Those that are will be either made
1326 fully redundant during the next iteration of insert (for partially
1327 redundant ones), or eliminated by eliminate (for fully redundant
1328 ones). */
1330 static tree
1331 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
1333 tree name = NULL_TREE;
1334 tree newexpr = NULL_TREE;
1335 tree v;
1337 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1339 case '2':
1341 tree_stmt_iterator tsi;
1342 tree genop1, genop2;
1343 tree temp;
1344 tree op1 = TREE_OPERAND (expr, 0);
1345 tree op2 = TREE_OPERAND (expr, 1);
1346 genop1 = find_or_generate_expression (block, op1, stmts);
1347 genop2 = find_or_generate_expression (block, op2, stmts);
1348 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1349 add_referenced_tmp_var (temp);
1350 newexpr = build (TREE_CODE (expr), TREE_TYPE (expr),
1351 genop1, genop2);
1352 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1353 temp, newexpr);
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 pre_stats.insertions++;
1359 break;
1361 case '1':
1363 tree_stmt_iterator tsi;
1364 tree genop1;
1365 tree temp;
1366 tree op1 = TREE_OPERAND (expr, 0);
1367 genop1 = find_or_generate_expression (block, op1, stmts);
1368 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1369 add_referenced_tmp_var (temp);
1370 newexpr = build (TREE_CODE (expr), TREE_TYPE (expr),
1371 genop1);
1372 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1373 temp, newexpr);
1374 name = make_ssa_name (temp, newexpr);
1375 TREE_OPERAND (newexpr, 0) = name;
1376 tsi = tsi_last (stmts);
1377 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1378 pre_stats.insertions++;
1380 break;
1382 default:
1383 abort ();
1386 v = get_value_handle (expr);
1387 vn_add (name, v, NULL);
1388 bitmap_insert_into_set (NEW_SETS (block), name);
1389 bitmap_value_insert_into_set (AVAIL_OUT (block), name);
1390 if (dump_file && (dump_flags & TDF_DETAILS))
1392 fprintf (dump_file, "Inserted ");
1393 print_generic_expr (dump_file, newexpr, 0);
1394 fprintf (dump_file, " in predecessor %d\n", block->index);
1396 return name;
1399 /* Perform insertion of partially redundant values.
1400 For BLOCK, do the following:
1401 1. Propagate the NEW_SETS of the dominator into the current block.
1402 If the block has multiple predecessors,
1403 2a. Iterate over the ANTIC expressions for the block to see if
1404 any of them are partially redundant.
1405 2b. If so, insert them into the necessary predecessors to make
1406 the expression fully redundant.
1407 2c. Insert a new PHI merging the values of the predecessors.
1408 2d. Insert the new PHI, and the new expressions, into the
1409 NEW_SETS set.
1410 3. Recursively call ourselves on the dominator children of BLOCK.
1413 static bool
1414 insert_aux (basic_block block)
1416 basic_block son;
1417 bool new_stuff = false;
1419 if (block)
1421 basic_block dom;
1422 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1423 if (dom)
1425 int i;
1426 bitmap_set_t newset = NEW_SETS (dom);
1427 EXECUTE_IF_SET_IN_BITMAP (newset->expressions, 0, i,
1429 bitmap_insert_into_set (NEW_SETS (block), ssa_name (i));
1430 bitmap_value_replace_in_set (AVAIL_OUT (block), ssa_name (i));
1432 if (block->pred->pred_next)
1434 value_set_node_t node;
1435 for (node = ANTIC_IN (block)->head;
1436 node;
1437 node = node->next)
1439 if (TREE_CODE_CLASS (TREE_CODE (node->expr)) == '2'
1440 || TREE_CODE_CLASS (TREE_CODE (node->expr)) == '1')
1442 tree *avail;
1443 tree val;
1444 bool by_some = false;
1445 bool cant_insert = false;
1446 bool all_same = true;
1447 tree first_s = NULL;
1448 edge pred;
1449 basic_block bprime;
1450 tree eprime;
1452 val = get_value_handle (node->expr);
1453 if (bitmap_set_contains_value (PHI_GEN (block), val))
1454 continue;
1455 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
1457 if (dump_file && (dump_flags & TDF_DETAILS))
1458 fprintf (dump_file, "Found fully redundant value\n");
1459 continue;
1462 avail = xcalloc (last_basic_block, sizeof (tree));
1463 for (pred = block->pred;
1464 pred;
1465 pred = pred->pred_next)
1467 tree vprime;
1468 tree edoubleprime;
1470 /* This can happen in the very weird case
1471 that our fake infinite loop edges have caused a
1472 critical edge to appear. */
1473 if (EDGE_CRITICAL_P (pred))
1475 cant_insert = true;
1476 break;
1478 bprime = pred->src;
1479 eprime = phi_translate (node->expr,
1480 ANTIC_IN (block),
1481 bprime, block);
1483 /* eprime will generally only be NULL if the
1484 value of the expression, translated
1485 through the PHI for this predecessor, is
1486 undefined. If that is the case, we can't
1487 make the expression fully redundant,
1488 because its value is undefined along a
1489 predecessor path. We can thus break out
1490 early because it doesn't matter what the
1491 rest of the results are. */
1492 if (eprime == NULL)
1494 cant_insert = true;
1495 break;
1498 vprime = get_value_handle (eprime);
1499 if (!vprime)
1500 abort ();
1501 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
1502 vprime);
1503 if (edoubleprime == NULL)
1505 avail[bprime->index] = eprime;
1506 all_same = false;
1508 else
1510 avail[bprime->index] = edoubleprime;
1511 by_some = true;
1512 if (first_s == NULL)
1513 first_s = edoubleprime;
1514 else if (first_s != edoubleprime)
1515 all_same = false;
1516 if (first_s != edoubleprime
1517 && operand_equal_p (first_s, edoubleprime, 0))
1518 abort ();
1521 /* If we can insert it, it's not the same value
1522 already existing along every predecessor, and
1523 it's defined by some predecessor, it is
1524 partially redundant. */
1525 if (!cant_insert && !all_same && by_some)
1527 tree type = TREE_TYPE (avail[block->pred->src->index]);
1528 tree temp;
1529 if (dump_file && (dump_flags & TDF_DETAILS))
1531 fprintf (dump_file, "Found partial redundancy for expression ");
1532 print_generic_expr (dump_file, node->expr, 0);
1533 fprintf (dump_file, "\n");
1536 /* Make the necessary insertions. */
1537 for (pred = block->pred;
1538 pred;
1539 pred = pred->pred_next)
1541 tree stmts = alloc_stmt_list ();
1542 tree builtexpr;
1543 bprime = pred->src;
1544 eprime = avail[bprime->index];
1545 if (TREE_CODE_CLASS (TREE_CODE (eprime)) == '2'
1546 || TREE_CODE_CLASS (TREE_CODE (eprime)) == '1')
1548 builtexpr = create_expression_by_pieces (bprime,
1549 eprime,
1550 stmts);
1551 bsi_insert_on_edge (pred, stmts);
1552 bsi_commit_edge_inserts (NULL);
1553 avail[bprime->index] = builtexpr;
1556 /* Now build a phi for the new variable. */
1557 temp = create_tmp_var (type, "prephitmp");
1558 add_referenced_tmp_var (temp);
1559 temp = create_phi_node (temp, block);
1560 vn_add (PHI_RESULT (temp), val, NULL);
1562 #if 0
1563 if (!set_contains_value (AVAIL_OUT (block), val))
1564 insert_into_set (AVAIL_OUT (block),
1565 PHI_RESULT (temp));
1566 else
1567 #endif
1568 bitmap_value_replace_in_set (AVAIL_OUT (block),
1569 PHI_RESULT (temp));
1570 for (pred = block->pred;
1571 pred;
1572 pred = pred->pred_next)
1574 add_phi_arg (&temp, avail[pred->src->index],
1575 pred);
1577 if (dump_file && (dump_flags & TDF_DETAILS))
1579 fprintf (dump_file, "Created phi ");
1580 print_generic_expr (dump_file, temp, 0);
1581 fprintf (dump_file, " in block %d\n", block->index);
1583 pre_stats.phis++;
1584 new_stuff = true;
1585 bitmap_insert_into_set (NEW_SETS (block),
1586 PHI_RESULT (temp));
1587 bitmap_insert_into_set (PHI_GEN (block),
1588 PHI_RESULT (temp));
1591 free (avail);
1597 for (son = first_dom_son (CDI_DOMINATORS, block);
1598 son;
1599 son = next_dom_son (CDI_DOMINATORS, son))
1601 new_stuff |= insert_aux (son);
1604 return new_stuff;
1607 /* Perform insertion of partially redundant values. */
1609 static void
1610 insert (void)
1612 bool new_stuff = true;
1613 basic_block bb;
1614 int num_iterations = 0;
1616 FOR_ALL_BB (bb)
1617 NEW_SETS (bb) = bitmap_set_new ();
1619 while (new_stuff)
1621 num_iterations++;
1622 new_stuff = false;
1623 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1625 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1626 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1630 /* Return true if VAR is an SSA variable with no defining statement in
1631 this procedure, *AND* isn't a live-on-entry parameter. */
1633 static bool
1634 is_undefined_value (tree expr)
1636 return (TREE_CODE (expr) == SSA_NAME
1637 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
1638 /* PARM_DECLs and hard registers are always defined. */
1639 && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL
1640 && !DECL_HARD_REGISTER (SSA_NAME_VAR (expr)));
1644 /* Given an SSA variable VAR and an expression EXPR, compute the value
1645 number for EXPR and create a value handle (VAL) for it. If VAR and
1646 EXPR are not the same, associate VAL with VAR. Finally, add VAR to
1647 S1 and its value handle to S2.
1649 VUSES represent the virtual use operands associated with EXPR (if
1650 any). They are used when computing the hash value for EXPR. */
1652 static inline void
1653 add_to_sets (tree var, tree expr, vuse_optype vuses, bitmap_set_t s1,
1654 bitmap_set_t s2)
1656 tree val = vn_lookup_or_add (expr, vuses);
1658 /* VAR and EXPR may be the same when processing statements for which
1659 we are not computing value numbers (e.g., non-assignments, or
1660 statements that make aliased stores). In those cases, we are
1661 only interested in making VAR available as its own value. */
1662 if (var != expr)
1663 vn_add (var, val, NULL);
1665 bitmap_insert_into_set (s1, var);
1666 bitmap_value_insert_into_set (s2, var);
1670 /* Given a unary or binary expression EXPR, create and return a new
1671 expression with the same structure as EXPR but with its operands
1672 replaced with the value handles of each of the operands of EXPR.
1673 Insert EXPR's operands into the EXP_GEN set for BLOCK.
1675 VUSES represent the virtual use operands associated with EXPR (if
1676 any). They are used when computing the hash value for EXPR. */
1678 static inline tree
1679 create_value_expr_from (tree expr, basic_block block, vuse_optype vuses)
1681 int i;
1682 enum tree_code code = TREE_CODE (expr);
1683 tree vexpr;
1685 #if defined ENABLE_CHECKING
1686 if (TREE_CODE_CLASS (code) != '1'
1687 && TREE_CODE_CLASS (code) != '2'
1688 && TREE_CODE_CLASS (code) != 'r')
1689 abort ();
1690 #endif
1692 if (TREE_CODE_CLASS (code) == '1')
1693 vexpr = pool_alloc (unary_node_pool);
1694 else if (TREE_CODE_CLASS (code) == 'r')
1695 vexpr = pool_alloc (reference_node_pool);
1696 else
1697 vexpr = pool_alloc (binary_node_pool);
1699 memcpy (vexpr, expr, tree_size (expr));
1701 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
1703 tree op = TREE_OPERAND (expr, i);
1704 if (op != NULL)
1706 tree val = vn_lookup_or_add (op, vuses);
1707 if (!is_undefined_value (op))
1708 value_insert_into_set (EXP_GEN (block), op);
1709 if (TREE_CODE (val) == VALUE_HANDLE)
1710 TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
1711 TREE_OPERAND (vexpr, i) = val;
1715 return vexpr;
1719 /* Compute the AVAIL set for BLOCK.
1720 This function performs value numbering of the statements in BLOCK.
1721 The AVAIL sets are built from information we glean while doing this
1722 value numbering, since the AVAIL sets contain only one entry per
1723 value.
1725 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
1726 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
1728 static void
1729 compute_avail (basic_block block)
1731 basic_block son;
1733 /* For arguments with default definitions, we pretend they are
1734 defined in the entry block. */
1735 if (block == ENTRY_BLOCK_PTR)
1737 tree param;
1738 for (param = DECL_ARGUMENTS (current_function_decl);
1739 param;
1740 param = TREE_CHAIN (param))
1742 if (default_def (param) != NULL)
1744 tree val;
1745 tree def = default_def (param);
1746 val = vn_lookup_or_add (def, NULL);
1747 bitmap_insert_into_set (TMP_GEN (block), def);
1748 bitmap_value_insert_into_set (AVAIL_OUT (block), def);
1752 else if (block)
1754 block_stmt_iterator bsi;
1755 tree stmt, phi;
1756 basic_block dom;
1758 /* Initially, the set of available values in BLOCK is that of
1759 its immediate dominator. */
1760 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1761 if (dom)
1762 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
1764 /* Generate values for PHI nodes. */
1765 for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
1766 /* We have no need for virtual phis, as they don't represent
1767 actual computations. */
1768 if (is_gimple_reg (PHI_RESULT (phi)))
1769 add_to_sets (PHI_RESULT (phi), PHI_RESULT (phi), NULL,
1770 PHI_GEN (block), AVAIL_OUT (block));
1772 /* Now compute value numbers and populate value sets with all
1773 the expressions computed in BLOCK. */
1774 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
1776 stmt_ann_t ann;
1777 size_t j;
1779 stmt = bsi_stmt (bsi);
1780 ann = stmt_ann (stmt);
1781 get_stmt_operands (stmt);
1783 /* We are only interested in assignments of the form
1784 X_i = EXPR, where EXPR represents an "interesting"
1785 computation, it has no volatile operands and X_i
1786 doesn't flow through an abnormal edge. */
1787 if (TREE_CODE (stmt) == MODIFY_EXPR
1788 && !ann->has_volatile_ops
1789 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
1790 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (stmt, 0)))
1792 tree lhs = TREE_OPERAND (stmt, 0);
1793 tree rhs = TREE_OPERAND (stmt, 1);
1794 vuse_optype vuses = STMT_VUSE_OPS (stmt);
1796 STRIP_USELESS_TYPE_CONVERSION (rhs);
1797 if (TREE_CODE (rhs) == SSA_NAME
1798 || is_gimple_min_invariant (rhs))
1800 /* Compute a value number for the RHS of the statement
1801 and add its value to the AVAIL_OUT set for the block.
1802 Add the LHS to TMP_GEN. */
1803 add_to_sets (lhs, rhs, vuses, TMP_GEN (block),
1804 AVAIL_OUT (block));
1806 if (TREE_CODE (rhs) == SSA_NAME
1807 && !is_undefined_value (rhs))
1808 value_insert_into_set (EXP_GEN (block), rhs);
1809 continue;
1811 else if (TREE_CODE_CLASS (TREE_CODE (rhs)) == '1'
1812 || TREE_CODE_CLASS (TREE_CODE (rhs)) == '2'
1813 || TREE_CODE (rhs) == INDIRECT_REF)
1815 /* For binary, unary, and reference expressions,
1816 create a duplicate expression with the operands
1817 replaced with the value handles of the original
1818 RHS. */
1819 tree newt = create_value_expr_from (rhs, block, vuses);
1820 add_to_sets (lhs, newt, vuses, TMP_GEN (block),
1821 AVAIL_OUT (block));
1822 value_insert_into_set (EXP_GEN (block), newt);
1823 continue;
1827 /* For any other statement that we don't recognize, simply
1828 make the names generated by the statement available in
1829 AVAIL_OUT and TMP_GEN. */
1830 for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1832 tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1833 add_to_sets (def, def, NULL, TMP_GEN (block),
1834 AVAIL_OUT (block));
1837 for (j = 0; j < NUM_USES (STMT_USE_OPS (stmt)); j++)
1839 tree use = USE_OP (STMT_USE_OPS (stmt), j);
1840 add_to_sets (use, use, NULL, TMP_GEN (block),
1841 AVAIL_OUT (block));
1846 /* Compute available sets for the dominator children of BLOCK. */
1847 for (son = first_dom_son (CDI_DOMINATORS, block);
1848 son;
1849 son = next_dom_son (CDI_DOMINATORS, son))
1850 compute_avail (son);
1854 /* Eliminate fully redundant computations. */
1856 static void
1857 eliminate (void)
1859 basic_block b;
1861 FOR_EACH_BB (b)
1863 block_stmt_iterator i;
1865 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
1867 tree stmt = bsi_stmt (i);
1869 /* Lookup the RHS of the expression, see if we have an
1870 available computation for it. If so, replace the RHS with
1871 the available computation. */
1872 if (TREE_CODE (stmt) == MODIFY_EXPR
1873 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
1874 && TREE_CODE (TREE_OPERAND (stmt ,1)) != SSA_NAME
1875 && !is_gimple_min_invariant (TREE_OPERAND (stmt, 1))
1876 && !stmt_ann (stmt)->has_volatile_ops)
1878 tree lhs = TREE_OPERAND (stmt, 0);
1879 tree *rhs_p = &TREE_OPERAND (stmt, 1);
1880 tree sprime;
1882 sprime = bitmap_find_leader (AVAIL_OUT (b),
1883 vn_lookup (lhs, NULL));
1884 if (sprime
1885 && sprime != lhs
1886 && (TREE_CODE (*rhs_p) != SSA_NAME
1887 || may_propagate_copy (*rhs_p, sprime)))
1889 if (sprime == *rhs_p)
1890 abort ();
1892 if (dump_file && (dump_flags & TDF_DETAILS))
1894 fprintf (dump_file, "Replaced ");
1895 print_generic_expr (dump_file, *rhs_p, 0);
1896 fprintf (dump_file, " with ");
1897 print_generic_expr (dump_file, sprime, 0);
1898 fprintf (dump_file, " in ");
1899 print_generic_stmt (dump_file, stmt, 0);
1901 pre_stats.eliminations++;
1902 propagate_tree_value (rhs_p, sprime);
1903 modify_stmt (stmt);
1905 /* If we removed EH side effects from the statement, clean
1906 its EH information. */
1907 if (maybe_clean_eh_stmt (stmt))
1909 bitmap_set_bit (need_eh_cleanup,
1910 bb_for_stmt (stmt)->index);
1911 if (dump_file && (dump_flags & TDF_DETAILS))
1912 fprintf (dump_file, " Removed EH side effects.\n");
1921 /* Initialize data structures used by PRE. */
1923 static void
1924 init_pre (void)
1926 size_t tsize;
1927 basic_block bb;
1929 connect_infinite_loops_to_exit ();
1930 vn_init ();
1931 memset (&pre_stats, 0, sizeof (pre_stats));
1933 /* If block 0 has more than one predecessor, it means that its PHI
1934 nodes will have arguments coming from block -1. This creates
1935 problems for several places in PRE that keep local arrays indexed
1936 by block number. To prevent this, we split the edge coming from
1937 ENTRY_BLOCK_PTR (FIXME, if ENTRY_BLOCK_PTR had an index number
1938 different than -1 we wouldn't have to hack this. tree-ssa-dce.c
1939 needs a similar change). */
1940 if (ENTRY_BLOCK_PTR->succ->dest->pred->pred_next)
1941 if (!(ENTRY_BLOCK_PTR->succ->flags & EDGE_ABNORMAL))
1942 split_edge (ENTRY_BLOCK_PTR->succ);
1944 FOR_ALL_BB (bb)
1945 bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
1947 phi_translate_table = htab_create (511, expr_pred_trans_hash,
1948 expr_pred_trans_eq, free);
1949 value_set_pool = create_alloc_pool ("Value sets",
1950 sizeof (struct value_set), 30);
1951 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
1952 sizeof (struct bitmap_set), 30);
1953 value_set_node_pool = create_alloc_pool ("Value set nodes",
1954 sizeof (struct value_set_node), 30);
1955 calculate_dominance_info (CDI_POST_DOMINATORS);
1956 calculate_dominance_info (CDI_DOMINATORS);
1957 tsize = tree_size (build (PLUS_EXPR, void_type_node, NULL_TREE, NULL_TREE));
1958 binary_node_pool = create_alloc_pool ("Binary tree nodes", tsize, 30);
1959 tsize = tree_size (build1 (NEGATE_EXPR, void_type_node, NULL_TREE));
1960 unary_node_pool = create_alloc_pool ("Unary tree nodes", tsize, 30);
1961 tsize = tree_size (build (COMPONENT_REF, void_type_node, NULL_TREE,
1962 NULL_TREE, NULL_TREE));
1963 reference_node_pool = create_alloc_pool ("Reference tree nodes", tsize, 30);
1964 FOR_ALL_BB (bb)
1966 EXP_GEN (bb) = set_new (true);
1967 PHI_GEN (bb) = bitmap_set_new ();
1968 TMP_GEN (bb) = bitmap_set_new ();
1969 AVAIL_OUT (bb) = bitmap_set_new ();
1972 need_eh_cleanup = BITMAP_XMALLOC ();
1976 /* Deallocate data structures used by PRE. */
1978 static void
1979 fini_pre (void)
1981 basic_block bb;
1983 free_alloc_pool (value_set_pool);
1984 free_alloc_pool (bitmap_set_pool);
1985 free_alloc_pool (value_set_node_pool);
1986 free_alloc_pool (binary_node_pool);
1987 free_alloc_pool (reference_node_pool);
1988 free_alloc_pool (unary_node_pool);
1989 htab_delete (phi_translate_table);
1990 remove_fake_exit_edges ();
1992 FOR_ALL_BB (bb)
1994 free (bb->aux);
1995 bb->aux = NULL;
1998 free_dominance_info (CDI_POST_DOMINATORS);
1999 vn_delete ();
2001 if (bitmap_first_set_bit (need_eh_cleanup) >= 0)
2003 tree_purge_all_dead_eh_edges (need_eh_cleanup);
2004 cleanup_tree_cfg ();
2007 BITMAP_XFREE (need_eh_cleanup);
2011 /* Main entry point to the SSA-PRE pass. DO_FRE is true if the caller
2012 only wants to do full redundancy elimination. */
2014 static void
2015 execute_pre (bool do_fre)
2017 init_pre ();
2019 /* Collect and value number expressions computed in each basic
2020 block. */
2021 compute_avail (ENTRY_BLOCK_PTR);
2023 if (dump_file && (dump_flags & TDF_DETAILS))
2025 basic_block bb;
2027 FOR_ALL_BB (bb)
2029 print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
2030 bitmap_print_value_set (dump_file, TMP_GEN (bb), "tmp_gen",
2031 bb->index);
2032 bitmap_print_value_set (dump_file, AVAIL_OUT (bb), "avail_out",
2033 bb->index);
2037 /* Insert can get quite slow on an incredibly large number of basic
2038 blocks due to some quadratic behavior. Until this behavior is
2039 fixed, don't run it when he have an incredibly large number of
2040 bb's. If we aren't going to run insert, there is no point in
2041 computing ANTIC, either, even though it's plenty fast. */
2042 if (!do_fre && n_basic_blocks < 4000)
2044 compute_antic ();
2045 insert ();
2048 /* Remove all the redundant expressions. */
2049 eliminate ();
2051 if (dump_file && (dump_flags & TDF_STATS))
2053 fprintf (dump_file, "Insertions:%d\n", pre_stats.insertions);
2054 fprintf (dump_file, "New PHIs:%d\n", pre_stats.phis);
2055 fprintf (dump_file, "Eliminated:%d\n", pre_stats.eliminations);
2058 fini_pre ();
2062 /* Gate and execute functions for PRE. */
2064 static void
2065 do_pre (void)
2067 execute_pre (false);
2070 static bool
2071 gate_pre (void)
2073 return flag_tree_pre != 0;
2076 struct tree_opt_pass pass_pre =
2078 "pre", /* name */
2079 gate_pre, /* gate */
2080 do_pre, /* execute */
2081 NULL, /* sub */
2082 NULL, /* next */
2083 0, /* static_pass_number */
2084 TV_TREE_PRE, /* tv_id */
2085 PROP_no_crit_edges | PROP_cfg
2086 | PROP_ssa | PROP_alias, /* properties_required */
2087 0, /* properties_provided */
2088 0, /* properties_destroyed */
2089 0, /* todo_flags_start */
2090 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
2094 /* Gate and execute functions for FRE. */
2096 static void
2097 do_fre (void)
2099 execute_pre (true);
2102 static bool
2103 gate_fre (void)
2105 return flag_tree_fre != 0;
2108 struct tree_opt_pass pass_fre =
2110 "fre", /* name */
2111 gate_fre, /* gate */
2112 do_fre, /* execute */
2113 NULL, /* sub */
2114 NULL, /* next */
2115 0, /* static_pass_number */
2116 TV_TREE_FRE, /* tv_id */
2117 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2118 0, /* properties_provided */
2119 0, /* properties_destroyed */
2120 0, /* todo_flags_start */
2121 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */