* config/i386/i386.c (init_cumulative_args): Set mmx/sse registers
[official-gcc.git] / gcc / tree-ssa-pre.c
blob848bd6a3941e30d9aaf5c5901c709ab20eceb8de
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 /* The phi_translate_table caches phi translations for a given
311 expression and predecessor. */
313 static htab_t phi_translate_table;
315 /* A three tuple {e, pred, v} used to cache phi translations in the
316 phi_translate_table. */
318 typedef struct expr_pred_trans_d
320 /* The expression. */
321 tree e;
323 /* The predecessor block along which we translated the expression. */
324 basic_block pred;
326 /* The value that resulted from the translation. */
327 tree v;
329 /* The hashcode for the expression, pred pair. This is cached for
330 speed reasons. */
331 hashval_t hashcode;
332 } *expr_pred_trans_t;
334 /* Return the hash value for a phi translation table entry. */
336 static hashval_t
337 expr_pred_trans_hash (const void *p)
339 const expr_pred_trans_t ve = (expr_pred_trans_t) p;
340 return ve->hashcode;
343 /* Return true if two phi translation table entries are the same.
344 P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
346 static int
347 expr_pred_trans_eq (const void *p1, const void *p2)
349 const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
350 const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
351 basic_block b1 = ve1->pred;
352 basic_block b2 = ve2->pred;
355 /* If they are not translations for the same basic block, they can't
356 be equal. */
357 if (b1 != b2)
358 return false;
360 /* If they are for the same basic block, determine if the
361 expressions are equal. */
362 if (expressions_equal_p (ve1->e, ve2->e))
363 return true;
365 return false;
368 /* Search in the phi translation table for the translation of
369 expression E in basic block PRED. Return the translated value, if
370 found, NULL otherwise. */
372 static inline tree
373 phi_trans_lookup (tree e, basic_block pred)
375 void **slot;
376 struct expr_pred_trans_d ept;
377 ept.e = e;
378 ept.pred = pred;
379 ept.hashcode = vn_compute (e, (unsigned long) pred, NULL);
380 slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
381 NO_INSERT);
382 if (!slot)
383 return NULL;
384 else
385 return ((expr_pred_trans_t) *slot)->v;
389 /* Add the tuple mapping from {expression E, basic block PRED} to
390 value V, to the phi translation table. */
392 static inline void
393 phi_trans_add (tree e, tree v, basic_block pred)
395 void **slot;
396 expr_pred_trans_t new_pair = xmalloc (sizeof (*new_pair));
397 new_pair->e = e;
398 new_pair->pred = pred;
399 new_pair->v = v;
400 new_pair->hashcode = vn_compute (e, (unsigned long) pred, NULL);
401 slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
402 new_pair->hashcode, INSERT);
403 if (*slot)
404 free (*slot);
405 *slot = (void *) new_pair;
409 /* Add expression E to the expression set of value V. */
411 void
412 add_to_value (tree v, tree e)
414 /* Constants have no expression sets. */
415 if (is_gimple_min_invariant (v))
416 return;
418 if (VALUE_HANDLE_EXPR_SET (v) == NULL)
419 VALUE_HANDLE_EXPR_SET (v) = set_new (false);
421 insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
425 /* Return true if value V exists in the bitmap for SET. */
427 static inline bool
428 value_exists_in_set_bitmap (value_set_t set, tree v)
430 if (!set->values)
431 return false;
433 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (v));
437 /* Remove value V from the bitmap for SET. */
439 static void
440 value_remove_from_set_bitmap (value_set_t set, tree v)
442 #ifdef ENABLE_CHECKING
443 if (!set->indexed)
444 abort ();
445 #endif
447 if (!set->values)
448 return;
450 bitmap_clear_bit (set->values, VALUE_HANDLE_ID (v));
454 /* Insert the value number V into the bitmap of values existing in
455 SET. */
457 static inline void
458 value_insert_into_set_bitmap (value_set_t set, tree v)
460 #ifdef ENABLE_CHECKING
461 if (!set->indexed)
462 abort ();
463 #endif
465 if (set->values == NULL)
467 set->values = BITMAP_GGC_ALLOC ();
468 bitmap_clear (set->values);
471 bitmap_set_bit (set->values, VALUE_HANDLE_ID (v));
475 /* Create a new bitmap set and return it. */
477 static bitmap_set_t
478 bitmap_set_new (void)
480 bitmap_set_t ret = pool_alloc (bitmap_set_pool);
481 ret->expressions = BITMAP_GGC_ALLOC ();
482 ret->values = BITMAP_GGC_ALLOC ();
483 bitmap_clear (ret->expressions);
484 bitmap_clear (ret->values);
485 return ret;
488 /* Create a new set. */
490 static value_set_t
491 set_new (bool indexed)
493 value_set_t ret;
494 ret = pool_alloc (value_set_pool);
495 ret->head = ret->tail = NULL;
496 ret->length = 0;
497 ret->indexed = indexed;
498 ret->values = NULL;
499 return ret;
502 /* Insert an expression EXPR into a bitmapped set. */
504 static void
505 bitmap_insert_into_set (bitmap_set_t set, tree expr)
507 tree val;
508 /* XXX: For now, we only let SSA_NAMES into the bitmap sets. */
509 if (TREE_CODE (expr) != SSA_NAME)
510 abort ();
511 val = get_value_handle (expr);
513 if (val == NULL)
514 abort ();
515 if (!is_gimple_min_invariant (val))
516 bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
517 bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
520 /* Insert EXPR into SET. */
522 static void
523 insert_into_set (value_set_t set, tree expr)
525 value_set_node_t newnode = pool_alloc (value_set_node_pool);
526 tree val = get_value_handle (expr);
528 if (val == NULL)
529 abort ();
531 /* For indexed sets, insert the value into the set value bitmap.
532 For all sets, add it to the linked list and increment the list
533 length. */
534 if (set->indexed)
535 value_insert_into_set_bitmap (set, val);
537 newnode->next = NULL;
538 newnode->expr = expr;
539 set->length ++;
540 if (set->head == NULL)
542 set->head = set->tail = newnode;
544 else
546 set->tail->next = newnode;
547 set->tail = newnode;
551 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
553 static void
554 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
556 bitmap_copy (dest->expressions, orig->expressions);
557 bitmap_copy (dest->values, orig->values);
560 /* Copy the set ORIG to the set DEST. */
562 static void
563 set_copy (value_set_t dest, value_set_t orig)
565 value_set_node_t node;
567 if (!orig || !orig->head)
568 return;
570 for (node = orig->head;
571 node;
572 node = node->next)
574 insert_into_set (dest, node->expr);
578 /* Remove EXPR from SET. */
580 static void
581 set_remove (value_set_t set, tree expr)
583 value_set_node_t node, prev;
585 /* Remove the value of EXPR from the bitmap, decrement the set
586 length, and remove it from the actual double linked list. */
587 value_remove_from_set_bitmap (set, get_value_handle (expr));
588 set->length--;
589 prev = NULL;
590 for (node = set->head;
591 node != NULL;
592 prev = node, node = node->next)
594 if (node->expr == expr)
596 if (prev == NULL)
597 set->head = node->next;
598 else
599 prev->next= node->next;
601 if (node == set->tail)
602 set->tail = prev;
603 pool_free (value_set_node_pool, node);
604 return;
609 /* Return true if SET contains the value VAL. */
611 static bool
612 set_contains_value (value_set_t set, tree val)
614 /* All constants are in every set. */
615 if (is_gimple_min_invariant (val))
616 return true;
618 if (set->length == 0)
619 return false;
621 return value_exists_in_set_bitmap (set, val);
624 /* Return true if bitmapped set SET contains the expression EXPR. */
625 static bool
626 bitmap_set_contains (bitmap_set_t set, tree expr)
628 /* XXX: Bitmapped sets only contain SSA_NAME's for now. */
629 if (TREE_CODE (expr) != SSA_NAME)
630 return false;
631 return bitmap_bit_p (set->expressions, SSA_NAME_VERSION (expr));
635 /* Return true if bitmapped set SET contains the value VAL. */
637 static bool
638 bitmap_set_contains_value (bitmap_set_t set, tree val)
640 if (is_gimple_min_invariant (val))
641 return true;
642 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
645 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
647 static void
648 bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
650 value_set_t exprset;
651 value_set_node_t node;
652 if (is_gimple_min_invariant (lookfor))
653 return;
654 if (!bitmap_set_contains_value (set, lookfor))
655 return;
656 /* The number of expressions having a given value is usually
657 significantly less than the total number of expressions in SET.
658 Thus, rather than check, for each expression in SET, whether it
659 has the value LOOKFOR, we walk the reverse mapping that tells us
660 what expressions have a given value, and see if any of those
661 expressions are in our set. For large testcases, this is about
662 5-10x faster than walking the bitmap. If this is somehow a
663 significant lose for some cases, we can choose which set to walk
664 based on the set size. */
665 exprset = VALUE_HANDLE_EXPR_SET (lookfor);
666 for (node = exprset->head; node; node = node->next)
668 if (TREE_CODE (node->expr) == SSA_NAME)
670 if (bitmap_bit_p (set->expressions, SSA_NAME_VERSION (node->expr)))
672 bitmap_clear_bit (set->expressions, SSA_NAME_VERSION (node->expr));
673 bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
674 return;
680 /* Subtract bitmapped set B from value set A, and return the new set. */
682 static value_set_t
683 bitmap_set_subtract_from_value_set (value_set_t a, bitmap_set_t b,
684 bool indexed)
686 value_set_t ret = set_new (indexed);
687 value_set_node_t node;
688 for (node = a->head;
689 node;
690 node = node->next)
692 if (!bitmap_set_contains (b, node->expr))
693 insert_into_set (ret, node->expr);
695 return ret;
698 /* Return true if two sets are equal. */
700 static bool
701 set_equal (value_set_t a, value_set_t b)
703 value_set_node_t node;
705 if (a->length != b->length)
706 return false;
707 for (node = a->head;
708 node;
709 node = node->next)
711 if (!set_contains_value (b, get_value_handle (node->expr)))
712 return false;
714 return true;
717 /* Replace an instance of EXPR's VALUE with EXPR in SET. */
719 static void
720 bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
722 tree val = get_value_handle (expr);
723 bitmap_set_replace_value (set, val, expr);
726 /* Insert EXPR into SET if EXPR's value is not already present in
727 SET. */
729 static void
730 bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
732 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 int i;
767 EXECUTE_IF_SET_IN_BITMAP (set->expressions, 0, i,
769 print_generic_expr (outfile, ssa_name (i), 0);
771 fprintf (outfile, " (");
772 print_generic_expr (outfile, get_value_handle (ssa_name (i)), 0);
773 fprintf (outfile, ") ");
774 if (bitmap_last_set_bit (set->expressions) != i)
775 fprintf (outfile, ", ");
778 fprintf (outfile, " }\n");
780 /* Print out the value_set SET to OUTFILE. */
782 static void
783 print_value_set (FILE *outfile, value_set_t set,
784 const char *setname, int blockindex)
786 value_set_node_t node;
787 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
788 if (set)
790 for (node = set->head;
791 node;
792 node = node->next)
794 print_generic_expr (outfile, node->expr, 0);
796 fprintf (outfile, " (");
797 print_generic_expr (outfile, get_value_handle (node->expr), 0);
798 fprintf (outfile, ") ");
800 if (node->next)
801 fprintf (outfile, ", ");
805 fprintf (outfile, " }\n");
808 /* Print out the expressions that have VAL to OUTFILE. */
810 void
811 print_value_expressions (FILE *outfile, tree val)
813 if (VALUE_HANDLE_EXPR_SET (val))
815 char s[10];
816 sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
817 print_value_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
822 void
823 debug_value_expressions (tree val)
825 print_value_expressions (stderr, val);
829 void debug_value_set (value_set_t, const char *, int);
831 void
832 debug_value_set (value_set_t set, const char *setname, int blockindex)
834 print_value_set (stderr, set, setname, blockindex);
837 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
838 the phis in PRED. Return NULL if we can't find a leader for each
839 part of the translated expression. */
841 static tree
842 phi_translate (tree expr, value_set_t set, basic_block pred,
843 basic_block phiblock)
845 tree phitrans = NULL;
846 tree oldexpr = expr;
848 if (expr == NULL)
849 return NULL;
851 /* Phi translations of a given expression don't change, */
852 phitrans = phi_trans_lookup (expr, pred);
853 if (phitrans)
854 return phitrans;
857 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
859 case '2':
861 tree oldop1 = TREE_OPERAND (expr, 0);
862 tree oldop2 = TREE_OPERAND (expr, 1);
863 tree newop1;
864 tree newop2;
865 tree newexpr;
867 newop1 = phi_translate (find_leader (set, oldop1),
868 set, pred, phiblock);
869 if (newop1 == NULL)
870 return NULL;
871 newop2 = phi_translate (find_leader (set, oldop2),
872 set, pred, phiblock);
873 if (newop2 == NULL)
874 return NULL;
875 if (newop1 != oldop1 || newop2 != oldop2)
877 newexpr = pool_alloc (binary_node_pool);
878 memcpy (newexpr, expr, tree_size (expr));
879 create_tree_ann (newexpr);
880 TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
881 TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
882 vn_lookup_or_add (newexpr, NULL);
883 expr = newexpr;
884 phi_trans_add (oldexpr, newexpr, pred);
887 break;
888 /* XXX: Until we have PRE of loads working, none will be ANTIC.
890 case 'r':
891 return NULL;
892 break;
893 case '1':
895 tree oldop1 = TREE_OPERAND (expr, 0);
896 tree newop1;
897 tree newexpr;
899 newop1 = phi_translate (find_leader (set, oldop1),
900 set, pred, phiblock);
901 if (newop1 == NULL)
902 return NULL;
903 if (newop1 != oldop1)
905 newexpr = pool_alloc (unary_node_pool);
906 memcpy (newexpr, expr, tree_size (expr));
907 create_tree_ann (newexpr);
908 TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
909 vn_lookup_or_add (newexpr, NULL);
910 expr = newexpr;
911 phi_trans_add (oldexpr, newexpr, pred);
914 break;
915 case 'd':
916 abort ();
917 case 'x':
919 tree phi = NULL;
920 int i;
921 if (TREE_CODE (expr) != SSA_NAME)
922 abort ();
923 if (TREE_CODE (SSA_NAME_DEF_STMT (expr)) == PHI_NODE)
924 phi = SSA_NAME_DEF_STMT (expr);
925 else
926 return expr;
928 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
929 if (PHI_ARG_EDGE (phi, i)->src == pred)
931 tree val;
932 if (is_undefined_value (PHI_ARG_DEF (phi, i)))
933 return NULL;
934 val = vn_lookup_or_add (PHI_ARG_DEF (phi, i), NULL);
935 return PHI_ARG_DEF (phi, i);
938 break;
940 return expr;
943 static void
944 phi_translate_set (value_set_t dest, value_set_t set, basic_block pred,
945 basic_block phiblock)
947 value_set_node_t node;
948 for (node = set->head;
949 node;
950 node = node->next)
952 tree translated;
953 translated = phi_translate (node->expr, set, pred, phiblock);
954 phi_trans_add (node->expr, translated, pred);
956 if (translated != NULL)
957 value_insert_into_set (dest, translated);
961 /* Find the leader for a value (i.e., the name representing that
962 value) in a given set, and return it. Return NULL if no leader is
963 found. */
965 static tree
966 bitmap_find_leader (bitmap_set_t set, tree val)
968 if (val == NULL)
969 return NULL;
971 if (is_gimple_min_invariant (val))
972 return val;
973 if (bitmap_set_contains_value (set, val))
975 /* Rather than walk the entire bitmap of expressions, and see
976 whether any of them has the value we are looking for, we look
977 at the reverse mapping, which tells us the set of expressions
978 that have a given value (IE value->expressions with that
979 value) and see if any of those expressions are in our set.
980 The number of expressions per value is usually significantly
981 less than the number of expressions in the set. In fact, for
982 large testcases, doing it this way is roughly 5-10x faster
983 than walking the bitmap.
984 If this is somehow a significant lose for some cases, we can
985 choose which set to walk based on which set is smaller. */
986 value_set_t exprset;
987 value_set_node_t node;
988 exprset = VALUE_HANDLE_EXPR_SET (val);
989 for (node = exprset->head; node; node = node->next)
991 if (TREE_CODE (node->expr) == SSA_NAME)
993 if (bitmap_bit_p (set->expressions,
994 SSA_NAME_VERSION (node->expr)))
995 return node->expr;
999 return NULL;
1003 /* Find the leader for a value (i.e., the name representing that
1004 value) in a given set, and return it. Return NULL if no leader is
1005 found. */
1007 static tree
1008 find_leader (value_set_t set, tree val)
1010 value_set_node_t node;
1012 if (val == NULL)
1013 return NULL;
1015 /* Constants represent themselves. */
1016 if (is_gimple_min_invariant (val))
1017 return val;
1019 if (set->length == 0)
1020 return NULL;
1022 if (value_exists_in_set_bitmap (set, val))
1024 for (node = set->head;
1025 node;
1026 node = node->next)
1028 if (get_value_handle (node->expr) == val)
1029 return node->expr;
1033 return NULL;
1036 /* Determine if the expression EXPR is valid in SET. This means that
1037 we have a leader for each part of the expression (if it consists of
1038 values), or the expression is an SSA_NAME.
1040 NB: We never should run into a case where we have SSA_NAME +
1041 SSA_NAME or SSA_NAME + value. The sets valid_in_set is called on,
1042 the ANTIC sets, will only ever have SSA_NAME's or binary value
1043 expression (IE VALUE1 + VALUE2) */
1045 static bool
1046 valid_in_set (value_set_t set, tree expr)
1048 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1050 case '2':
1052 tree op1 = TREE_OPERAND (expr, 0);
1053 tree op2 = TREE_OPERAND (expr, 1);
1054 return set_contains_value (set, op1) && set_contains_value (set, op2);
1056 break;
1057 case '1':
1059 tree op1 = TREE_OPERAND (expr, 0);
1060 return set_contains_value (set, op1);
1062 break;
1063 /* XXX: Until PRE of loads works, no reference nodes are ANTIC.
1065 case 'r':
1067 return false;
1069 case 'x':
1071 if (TREE_CODE (expr) == SSA_NAME)
1072 return true;
1073 abort ();
1075 case 'c':
1076 abort ();
1078 return false;
1081 /* Clean the set of expressions that are no longer valid in SET. This
1082 means expressions that are made up of values we have no leaders for
1083 in SET. */
1085 static void
1086 clean (value_set_t set)
1088 value_set_node_t node;
1089 value_set_node_t next;
1090 node = set->head;
1091 while (node)
1093 next = node->next;
1094 if (!valid_in_set (set, node->expr))
1095 set_remove (set, node->expr);
1096 node = next;
1100 /* Compute the ANTIC set for BLOCK.
1102 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK), if
1103 succs(BLOCK) > 1
1104 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)]) if
1105 succs(BLOCK) == 1
1107 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] -
1108 TMP_GEN[BLOCK])
1110 Iterate until fixpointed.
1112 XXX: It would be nice to either write a set_clear, and use it for
1113 antic_out, or to mark the antic_out set as deleted at the end
1114 of this routine, so that the pool can hand the same memory back out
1115 again for the next antic_out. */
1118 static bool
1119 compute_antic_aux (basic_block block)
1121 basic_block son;
1122 edge e;
1123 bool changed = false;
1124 value_set_t S, old, ANTIC_OUT;
1125 value_set_node_t node;
1127 ANTIC_OUT = S = NULL;
1128 /* If any edges from predecessors are abnormal, antic_in is empty, so
1129 punt. Remember that the block has an incoming abnormal edge by
1130 setting the BB_VISITED flag. */
1131 if (! (block->flags & BB_VISITED))
1133 for (e = block->pred; e; e = e->pred_next)
1134 if (e->flags & EDGE_ABNORMAL)
1136 block->flags |= BB_VISITED;
1137 break;
1140 if (block->flags & BB_VISITED)
1142 S = NULL;
1143 goto visit_sons;
1147 old = set_new (false);
1148 set_copy (old, ANTIC_IN (block));
1149 ANTIC_OUT = set_new (true);
1151 /* If the block has no successors, ANTIC_OUT is empty, because it is
1152 the exit block. */
1153 if (block->succ == NULL);
1155 /* If we have one successor, we could have some phi nodes to
1156 translate through. */
1157 else if (block->succ->succ_next == NULL)
1159 phi_translate_set (ANTIC_OUT, ANTIC_IN(block->succ->dest),
1160 block, block->succ->dest);
1162 /* If we have multiple successors, we take the intersection of all of
1163 them. */
1164 else
1166 varray_type worklist;
1167 edge e;
1168 size_t i;
1169 basic_block bprime, first;
1171 VARRAY_BB_INIT (worklist, 1, "succ");
1172 e = block->succ;
1173 while (e)
1175 VARRAY_PUSH_BB (worklist, e->dest);
1176 e = e->succ_next;
1178 first = VARRAY_BB (worklist, 0);
1179 set_copy (ANTIC_OUT, ANTIC_IN (first));
1181 for (i = 1; i < VARRAY_ACTIVE_SIZE (worklist); i++)
1183 bprime = VARRAY_BB (worklist, i);
1184 node = ANTIC_OUT->head;
1185 while (node)
1187 tree val;
1188 value_set_node_t next = node->next;
1189 val = get_value_handle (node->expr);
1190 if (!set_contains_value (ANTIC_IN (bprime), val))
1191 set_remove (ANTIC_OUT, node->expr);
1192 node = next;
1195 VARRAY_CLEAR (worklist);
1198 /* Generate ANTIC_OUT - TMP_GEN */
1199 S = bitmap_set_subtract_from_value_set (ANTIC_OUT, TMP_GEN (block), false);
1201 /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1202 ANTIC_IN (block) = bitmap_set_subtract_from_value_set (EXP_GEN (block),
1203 TMP_GEN (block),
1204 true);
1206 /* Then union in the ANTIC_OUT - TMP_GEN values, to get ANTIC_OUT U
1207 EXP_GEN - TMP_GEN */
1208 for (node = S->head;
1209 node;
1210 node = node->next)
1212 value_insert_into_set (ANTIC_IN (block), node->expr);
1214 clean (ANTIC_IN (block));
1217 if (!set_equal (old, ANTIC_IN (block)))
1218 changed = true;
1220 visit_sons:
1221 if (dump_file && (dump_flags & TDF_DETAILS))
1223 if (ANTIC_OUT)
1224 print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1225 print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1226 if (S)
1227 print_value_set (dump_file, S, "S", block->index);
1231 for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1232 son;
1233 son = next_dom_son (CDI_POST_DOMINATORS, son))
1235 changed |= compute_antic_aux (son);
1237 return changed;
1240 /* Compute ANTIC sets. */
1242 static void
1243 compute_antic (void)
1245 bool changed = true;
1246 basic_block bb;
1247 int num_iterations = 0;
1248 FOR_ALL_BB (bb)
1250 ANTIC_IN (bb) = set_new (true);
1251 if (bb->flags & BB_VISITED)
1252 abort ();
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 if (TREE_CODE_CLASS (TREE_CODE (genop)) != '1'
1298 && TREE_CODE_CLASS (TREE_CODE (genop)) != '2'
1299 && TREE_CODE_CLASS (TREE_CODE (genop)) != 'r')
1300 abort ();
1301 genop = create_expression_by_pieces (block, genop, stmts);
1303 return genop;
1307 /* Create an expression in pieces, so that we can handle very complex
1308 expressions that may be ANTIC, but not necessary GIMPLE.
1309 BLOCK is the basic block the expression will be inserted into,
1310 EXPR is the expression to insert (in value form)
1311 STMTS is a statement list to append the necessary insertions into.
1313 This function will abort if we hit some value that shouldn't be
1314 ANTIC but is (IE there is no leader for it, or its components).
1315 This function may also generate expressions that are themselves
1316 partially or fully redundant. Those that are will be either made
1317 fully redundant during the next iteration of insert (for partially
1318 redundant ones), or eliminated by eliminate (for fully redundant
1319 ones). */
1321 static tree
1322 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
1324 tree name = NULL_TREE;
1325 tree newexpr = NULL_TREE;
1326 tree v;
1328 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1330 case '2':
1332 tree_stmt_iterator tsi;
1333 tree genop1, genop2;
1334 tree temp;
1335 tree op1 = TREE_OPERAND (expr, 0);
1336 tree op2 = TREE_OPERAND (expr, 1);
1337 genop1 = find_or_generate_expression (block, op1, stmts);
1338 genop2 = find_or_generate_expression (block, op2, stmts);
1339 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1340 add_referenced_tmp_var (temp);
1341 newexpr = build (TREE_CODE (expr), TREE_TYPE (expr),
1342 genop1, genop2);
1343 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1344 temp, newexpr);
1345 name = make_ssa_name (temp, newexpr);
1346 TREE_OPERAND (newexpr, 0) = name;
1347 tsi = tsi_last (stmts);
1348 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1349 pre_stats.insertions++;
1350 break;
1352 case '1':
1354 tree_stmt_iterator tsi;
1355 tree genop1;
1356 tree temp;
1357 tree op1 = TREE_OPERAND (expr, 0);
1358 genop1 = find_or_generate_expression (block, op1, stmts);
1359 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1360 add_referenced_tmp_var (temp);
1361 newexpr = build (TREE_CODE (expr), TREE_TYPE (expr),
1362 genop1);
1363 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1364 temp, newexpr);
1365 name = make_ssa_name (temp, newexpr);
1366 TREE_OPERAND (newexpr, 0) = name;
1367 tsi = tsi_last (stmts);
1368 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1369 pre_stats.insertions++;
1371 break;
1373 default:
1374 abort ();
1377 v = get_value_handle (expr);
1378 vn_add (name, v, NULL);
1379 bitmap_insert_into_set (NEW_SETS (block), name);
1380 bitmap_value_insert_into_set (AVAIL_OUT (block), name);
1381 if (dump_file && (dump_flags & TDF_DETAILS))
1383 fprintf (dump_file, "Inserted ");
1384 print_generic_expr (dump_file, newexpr, 0);
1385 fprintf (dump_file, " in predecessor %d\n", block->index);
1387 return name;
1390 /* Perform insertion of partially redundant values.
1391 For BLOCK, do the following:
1392 1. Propagate the NEW_SETS of the dominator into the current block.
1393 If the block has multiple predecessors,
1394 2a. Iterate over the ANTIC expressions for the block to see if
1395 any of them are partially redundant.
1396 2b. If so, insert them into the necessary predecessors to make
1397 the expression fully redundant.
1398 2c. Insert a new PHI merging the values of the predecessors.
1399 2d. Insert the new PHI, and the new expressions, into the
1400 NEW_SETS set.
1401 3. Recursively call ourselves on the dominator children of BLOCK.
1404 static bool
1405 insert_aux (basic_block block)
1407 basic_block son;
1408 bool new_stuff = false;
1410 if (block)
1412 basic_block dom;
1413 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1414 if (dom)
1416 int i;
1417 bitmap_set_t newset = NEW_SETS (dom);
1418 EXECUTE_IF_SET_IN_BITMAP (newset->expressions, 0, i,
1420 bitmap_insert_into_set (NEW_SETS (block), ssa_name (i));
1421 bitmap_value_replace_in_set (AVAIL_OUT (block), ssa_name (i));
1423 if (block->pred->pred_next)
1425 value_set_node_t node;
1426 for (node = ANTIC_IN (block)->head;
1427 node;
1428 node = node->next)
1430 if (TREE_CODE_CLASS (TREE_CODE (node->expr)) == '2'
1431 || TREE_CODE_CLASS (TREE_CODE (node->expr)) == '1')
1433 tree *avail;
1434 tree val;
1435 bool by_some = false;
1436 bool cant_insert = false;
1437 bool all_same = true;
1438 tree first_s = NULL;
1439 edge pred;
1440 basic_block bprime;
1441 tree eprime;
1443 val = get_value_handle (node->expr);
1444 if (bitmap_set_contains_value (PHI_GEN (block), val))
1445 continue;
1446 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
1448 if (dump_file && (dump_flags & TDF_DETAILS))
1449 fprintf (dump_file, "Found fully redundant value\n");
1450 continue;
1453 avail = xcalloc (last_basic_block, sizeof (tree));
1454 for (pred = block->pred;
1455 pred;
1456 pred = pred->pred_next)
1458 tree vprime;
1459 tree edoubleprime;
1460 bprime = pred->src;
1461 eprime = phi_translate (node->expr,
1462 ANTIC_IN (block),
1463 bprime, block);
1465 /* eprime will generally only be NULL if the
1466 value of the expression, translated
1467 through the PHI for this predecessor, is
1468 undefined. If that is the case, we can't
1469 make the expression fully redundant,
1470 because its value is undefined along a
1471 predecessor path. We can thus break out
1472 early because it doesn't matter what the
1473 rest of the results are. */
1474 if (eprime == NULL)
1476 cant_insert = true;
1477 break;
1480 vprime = get_value_handle (eprime);
1481 if (!vprime)
1482 abort ();
1483 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
1484 vprime);
1485 if (edoubleprime == NULL)
1487 avail[bprime->index] = eprime;
1488 all_same = false;
1490 else
1492 avail[bprime->index] = edoubleprime;
1493 by_some = true;
1494 if (first_s == NULL)
1495 first_s = edoubleprime;
1496 else if (first_s != edoubleprime)
1497 all_same = false;
1498 if (first_s != edoubleprime
1499 && operand_equal_p (first_s, edoubleprime, 0))
1500 abort ();
1503 /* If we can insert it, it's not the same value
1504 already existing along every predecessor, and
1505 it's defined by some predecessor, it is
1506 partially redundant. */
1507 if (!cant_insert && !all_same && by_some)
1509 tree type = TREE_TYPE (avail[block->pred->src->index]);
1510 tree temp;
1511 if (dump_file && (dump_flags & TDF_DETAILS))
1513 fprintf (dump_file, "Found partial redundancy for expression ");
1514 print_generic_expr (dump_file, node->expr, 0);
1515 fprintf (dump_file, "\n");
1518 /* Make the necessary insertions. */
1519 for (pred = block->pred;
1520 pred;
1521 pred = pred->pred_next)
1523 tree stmts = alloc_stmt_list ();
1524 tree builtexpr;
1525 bprime = pred->src;
1526 eprime = avail[bprime->index];
1527 if (TREE_CODE_CLASS (TREE_CODE (eprime)) == '2'
1528 || TREE_CODE_CLASS (TREE_CODE (eprime)) == '1')
1530 builtexpr = create_expression_by_pieces (bprime,
1531 eprime,
1532 stmts);
1533 bsi_insert_on_edge (pred, stmts);
1534 bsi_commit_edge_inserts (NULL);
1535 avail[bprime->index] = builtexpr;
1538 /* Now build a phi for the new variable. */
1539 temp = create_tmp_var (type, "prephitmp");
1540 add_referenced_tmp_var (temp);
1541 temp = create_phi_node (temp, block);
1542 vn_add (PHI_RESULT (temp), val, NULL);
1544 #if 0
1545 if (!set_contains_value (AVAIL_OUT (block), val))
1546 insert_into_set (AVAIL_OUT (block),
1547 PHI_RESULT (temp));
1548 else
1549 #endif
1550 bitmap_value_replace_in_set (AVAIL_OUT (block),
1551 PHI_RESULT (temp));
1552 for (pred = block->pred;
1553 pred;
1554 pred = pred->pred_next)
1556 add_phi_arg (&temp, avail[pred->src->index],
1557 pred);
1559 if (dump_file && (dump_flags & TDF_DETAILS))
1561 fprintf (dump_file, "Created phi ");
1562 print_generic_expr (dump_file, temp, 0);
1563 fprintf (dump_file, " in block %d\n", block->index);
1565 pre_stats.phis++;
1566 new_stuff = true;
1567 bitmap_insert_into_set (NEW_SETS (block),
1568 PHI_RESULT (temp));
1569 bitmap_insert_into_set (PHI_GEN (block),
1570 PHI_RESULT (temp));
1573 free (avail);
1579 for (son = first_dom_son (CDI_DOMINATORS, block);
1580 son;
1581 son = next_dom_son (CDI_DOMINATORS, son))
1583 new_stuff |= insert_aux (son);
1586 return new_stuff;
1589 /* Perform insertion of partially redundant values. */
1591 static void
1592 insert (void)
1594 bool new_stuff = true;
1595 basic_block bb;
1596 int num_iterations = 0;
1598 FOR_ALL_BB (bb)
1599 NEW_SETS (bb) = bitmap_set_new ();
1601 while (new_stuff)
1603 num_iterations++;
1604 new_stuff = false;
1605 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1607 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1608 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1612 /* Return true if VAR is an SSA variable with no defining statement in
1613 this procedure, *AND* isn't a live-on-entry parameter. */
1615 static bool
1616 is_undefined_value (tree expr)
1618 return (TREE_CODE (expr) == SSA_NAME
1619 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
1620 /* PARM_DECLs and hard registers are always defined. */
1621 && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL
1622 && !DECL_HARD_REGISTER (SSA_NAME_VAR (expr)));
1626 /* Given an SSA variable VAR and an expression EXPR, compute the value
1627 number for EXPR and create a value handle (VAL) for it. If VAR and
1628 EXPR are not the same, associate VAL with VAR. Finally, add VAR to
1629 S1 and its value handle to S2.
1631 VUSES represent the virtual use operands associated with EXPR (if
1632 any). They are used when computing the hash value for EXPR. */
1634 static inline void
1635 add_to_sets (tree var, tree expr, vuse_optype vuses, bitmap_set_t s1,
1636 bitmap_set_t s2)
1638 tree val = vn_lookup_or_add (expr, vuses);
1640 /* VAR and EXPR may be the same when processing statements for which
1641 we are not computing value numbers (e.g., non-assignments, or
1642 statements that make aliased stores). In those cases, we are
1643 only interested in making VAR available as its own value. */
1644 if (var != expr)
1645 vn_add (var, val, NULL);
1647 bitmap_insert_into_set (s1, var);
1648 bitmap_value_insert_into_set (s2, var);
1652 /* Given a unary or binary expression EXPR, create and return a new
1653 expression with the same structure as EXPR but with its operands
1654 replaced with the value handles of each of the operands of EXPR.
1655 Insert EXPR's operands into the EXP_GEN set for BLOCK.
1657 VUSES represent the virtual use operands associated with EXPR (if
1658 any). They are used when computing the hash value for EXPR. */
1660 static inline tree
1661 create_value_expr_from (tree expr, basic_block block, vuse_optype vuses)
1663 int i;
1664 enum tree_code code = TREE_CODE (expr);
1665 tree vexpr;
1667 #if defined ENABLE_CHECKING
1668 if (TREE_CODE_CLASS (code) != '1'
1669 && TREE_CODE_CLASS (code) != '2'
1670 && TREE_CODE_CLASS (code) != 'r')
1671 abort ();
1672 #endif
1674 if (TREE_CODE_CLASS (code) == '1')
1675 vexpr = pool_alloc (unary_node_pool);
1676 else if (TREE_CODE_CLASS (code) == 'r')
1677 vexpr = pool_alloc (reference_node_pool);
1678 else
1679 vexpr = pool_alloc (binary_node_pool);
1681 memcpy (vexpr, expr, tree_size (expr));
1683 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
1685 tree op = TREE_OPERAND (expr, i);
1686 if (op != NULL)
1688 tree val = vn_lookup_or_add (op, vuses);
1689 if (!is_undefined_value (op))
1690 value_insert_into_set (EXP_GEN (block), op);
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 (TREE_CODE_CLASS (TREE_CODE (rhs)) == '1'
1793 || TREE_CODE_CLASS (TREE_CODE (rhs)) == '2'
1794 || TREE_CODE (rhs) == INDIRECT_REF)
1796 /* For binary, unary, and reference expressions,
1797 create a duplicate expression with the operands
1798 replaced with the value handles of the original
1799 RHS. */
1800 tree newt = create_value_expr_from (rhs, block, vuses);
1801 add_to_sets (lhs, newt, vuses, TMP_GEN (block),
1802 AVAIL_OUT (block));
1803 value_insert_into_set (EXP_GEN (block), newt);
1804 continue;
1808 /* For any other statement that we don't recognize, simply
1809 make the names generated by the statement available in
1810 AVAIL_OUT and TMP_GEN. */
1811 for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1813 tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1814 add_to_sets (def, def, NULL, TMP_GEN (block),
1815 AVAIL_OUT (block));
1818 for (j = 0; j < NUM_USES (STMT_USE_OPS (stmt)); j++)
1820 tree use = USE_OP (STMT_USE_OPS (stmt), j);
1821 add_to_sets (use, use, NULL, TMP_GEN (block),
1822 AVAIL_OUT (block));
1827 /* Compute available sets for the dominator children of BLOCK. */
1828 for (son = first_dom_son (CDI_DOMINATORS, block);
1829 son;
1830 son = next_dom_son (CDI_DOMINATORS, son))
1831 compute_avail (son);
1835 /* Eliminate fully redundant computations. */
1837 static void
1838 eliminate (void)
1840 basic_block b;
1842 FOR_EACH_BB (b)
1844 block_stmt_iterator i;
1846 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
1848 tree stmt = bsi_stmt (i);
1850 /* Lookup the RHS of the expression, see if we have an
1851 available computation for it. If so, replace the RHS with
1852 the available computation. */
1853 if (TREE_CODE (stmt) == MODIFY_EXPR
1854 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
1855 && TREE_CODE (TREE_OPERAND (stmt ,1)) != SSA_NAME
1856 && !is_gimple_min_invariant (TREE_OPERAND (stmt, 1))
1857 && !stmt_ann (stmt)->has_volatile_ops)
1859 tree lhs = TREE_OPERAND (stmt, 0);
1860 tree *rhs_p = &TREE_OPERAND (stmt, 1);
1861 tree sprime;
1863 sprime = bitmap_find_leader (AVAIL_OUT (b),
1864 vn_lookup (lhs, NULL));
1865 if (sprime
1866 && sprime != lhs
1867 && (TREE_CODE (*rhs_p) != SSA_NAME
1868 || may_propagate_copy (*rhs_p, sprime)))
1870 if (sprime == *rhs_p)
1871 abort ();
1873 if (dump_file && (dump_flags & TDF_DETAILS))
1875 fprintf (dump_file, "Replaced ");
1876 print_generic_expr (dump_file, *rhs_p, 0);
1877 fprintf (dump_file, " with ");
1878 print_generic_expr (dump_file, sprime, 0);
1879 fprintf (dump_file, " in ");
1880 print_generic_stmt (dump_file, stmt, 0);
1882 pre_stats.eliminations++;
1883 propagate_tree_value (rhs_p, sprime);
1884 modify_stmt (stmt);
1892 /* Initialize data structures used by PRE. */
1894 static void
1895 init_pre (void)
1897 size_t tsize;
1898 basic_block bb;
1900 vn_init ();
1901 memset (&pre_stats, 0, sizeof (pre_stats));
1902 FOR_ALL_BB (bb)
1903 bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
1905 phi_translate_table = htab_create (511, expr_pred_trans_hash,
1906 expr_pred_trans_eq, free);
1907 value_set_pool = create_alloc_pool ("Value sets",
1908 sizeof (struct value_set), 30);
1909 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
1910 sizeof (struct bitmap_set), 30);
1911 value_set_node_pool = create_alloc_pool ("Value set nodes",
1912 sizeof (struct value_set_node), 30);
1913 calculate_dominance_info (CDI_POST_DOMINATORS);
1914 calculate_dominance_info (CDI_DOMINATORS);
1915 tsize = tree_size (build (PLUS_EXPR, void_type_node, NULL_TREE, NULL_TREE));
1916 binary_node_pool = create_alloc_pool ("Binary tree nodes", tsize, 30);
1917 tsize = tree_size (build1 (NEGATE_EXPR, void_type_node, NULL_TREE));
1918 unary_node_pool = create_alloc_pool ("Unary tree nodes", tsize, 30);
1919 tsize = tree_size (build (COMPONENT_REF, void_type_node, NULL_TREE, NULL_TREE, NULL_TREE));
1920 reference_node_pool = create_alloc_pool ("Reference tree nodes", tsize, 30);
1921 FOR_ALL_BB (bb)
1923 EXP_GEN (bb) = set_new (true);
1924 PHI_GEN (bb) = bitmap_set_new ();
1925 TMP_GEN (bb) = bitmap_set_new ();
1926 AVAIL_OUT (bb) = bitmap_set_new ();
1931 /* Deallocate data structures used by PRE. */
1933 static void
1934 fini_pre (void)
1936 basic_block bb;
1938 free_alloc_pool (value_set_pool);
1939 free_alloc_pool (bitmap_set_pool);
1940 free_alloc_pool (value_set_node_pool);
1941 free_alloc_pool (binary_node_pool);
1942 free_alloc_pool (reference_node_pool);
1943 free_alloc_pool (unary_node_pool);
1944 htab_delete (phi_translate_table);
1946 FOR_ALL_BB (bb)
1948 free (bb->aux);
1949 bb->aux = NULL;
1951 free_dominance_info (CDI_POST_DOMINATORS);
1952 vn_delete ();
1956 /* Main entry point to the SSA-PRE pass. DO_FRE is true if the caller
1957 only wants to do full redundancy elimination. */
1959 static void
1960 execute_pre (bool do_fre)
1962 init_pre ();
1964 /* Collect and value number expressions computed in each basic
1965 block. */
1966 compute_avail (ENTRY_BLOCK_PTR);
1968 if (dump_file && (dump_flags & TDF_DETAILS))
1970 basic_block bb;
1972 FOR_ALL_BB (bb)
1974 print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
1975 bitmap_print_value_set (dump_file, TMP_GEN (bb), "tmp_gen",
1976 bb->index);
1977 bitmap_print_value_set (dump_file, AVAIL_OUT (bb), "avail_out",
1978 bb->index);
1982 /* Insert can get quite slow on an incredibly large number of basic
1983 blocks due to some quadratic behavior. Until this behavior is
1984 fixed, don't run it when he have an incredibly large number of
1985 bb's. If we aren't going to run insert, there is no point in
1986 computing ANTIC, either, even though it's plenty fast. */
1987 if (!do_fre && n_basic_blocks < 4000)
1989 compute_antic ();
1990 insert ();
1993 /* Remove all the redundant expressions. */
1994 eliminate ();
1996 if (dump_file && (dump_flags & TDF_STATS))
1998 fprintf (dump_file, "Insertions:%d\n", pre_stats.insertions);
1999 fprintf (dump_file, "New PHIs:%d\n", pre_stats.phis);
2000 fprintf (dump_file, "Eliminated:%d\n", pre_stats.eliminations);
2003 fini_pre ();
2007 /* Gate and execute functions for PRE. */
2009 static void
2010 do_pre (void)
2012 execute_pre (false);
2015 static bool
2016 gate_pre (void)
2018 return flag_tree_pre != 0;
2021 struct tree_opt_pass pass_pre =
2023 "pre", /* name */
2024 gate_pre, /* gate */
2025 do_pre, /* execute */
2026 NULL, /* sub */
2027 NULL, /* next */
2028 0, /* static_pass_number */
2029 TV_TREE_PRE, /* tv_id */
2030 PROP_no_crit_edges | PROP_cfg | PROP_ssa,/* properties_required */
2031 0, /* properties_provided */
2032 0, /* properties_destroyed */
2033 0, /* todo_flags_start */
2034 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
2038 /* Gate and execute functions for FRE. */
2040 static void
2041 do_fre (void)
2043 execute_pre (true);
2046 static bool
2047 gate_fre (void)
2049 return flag_tree_fre != 0;
2052 struct tree_opt_pass pass_fre =
2054 "fre", /* name */
2055 gate_fre, /* gate */
2056 do_fre, /* execute */
2057 NULL, /* sub */
2058 NULL, /* next */
2059 0, /* static_pass_number */
2060 TV_TREE_FRE, /* tv_id */
2061 PROP_no_crit_edges | PROP_cfg | PROP_ssa,/* properties_required */
2062 0, /* properties_provided */
2063 0, /* properties_destroyed */
2064 0, /* todo_flags_start */
2065 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */