2005-12-29 Paul Brook <paul@codesourcery.com>
[official-gcc.git] / gcc / tree-ssa-pre.c
blobe8ef1220b19538ab7e981a7f8c7e95bd807295ad
1 /* SSA-PRE for trees.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
4 <stevenb@suse.de>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "ggc.h"
28 #include "tree.h"
29 #include "basic-block.h"
30 #include "diagnostic.h"
31 #include "tree-inline.h"
32 #include "tree-flow.h"
33 #include "tree-gimple.h"
34 #include "tree-dump.h"
35 #include "timevar.h"
36 #include "fibheap.h"
37 #include "hashtab.h"
38 #include "tree-iterator.h"
39 #include "real.h"
40 #include "alloc-pool.h"
41 #include "tree-pass.h"
42 #include "flags.h"
43 #include "bitmap.h"
44 #include "langhooks.h"
45 #include "cfgloop.h"
47 /* TODO:
49 1. Avail sets can be shared by making an avail_find_leader that
50 walks up the dominator tree and looks in those avail sets.
51 This might affect code optimality, it's unclear right now.
52 2. Load motion can be performed by value numbering the loads the
53 same as we do other expressions. This requires iterative
54 hashing the vuses into the values. Right now we simply assign
55 a new value every time we see a statement with a vuse.
56 3. Strength reduction can be performed by anticipating expressions
57 we can repair later on.
58 4. We can do back-substitution or smarter value numbering to catch
59 commutative expressions split up over multiple statements.
60 */
62 /* For ease of terminology, "expression node" in the below refers to
63 every expression node but MODIFY_EXPR, because MODIFY_EXPR's represent
64 the actual statement containing the expressions we care about, and
65 we cache the value number by putting it in the expression. */
67 /* Basic algorithm
69 First we walk the statements to generate the AVAIL sets, the
70 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
71 generation of values/expressions by a given block. We use them
72 when computing the ANTIC sets. The AVAIL sets consist of
73 SSA_NAME's that represent values, so we know what values are
74 available in what blocks. AVAIL is a forward dataflow problem. In
75 SSA, values are never killed, so we don't need a kill set, or a
76 fixpoint iteration, in order to calculate the AVAIL sets. In
77 traditional parlance, AVAIL sets tell us the downsafety of the
78 expressions/values.
80 Next, we generate the ANTIC sets. These sets represent the
81 anticipatable expressions. ANTIC is a backwards dataflow
82 problem.An expression is anticipatable in a given block if it could
83 be generated in that block. This means that if we had to perform
84 an insertion in that block, of the value of that expression, we
85 could. Calculating the ANTIC sets requires phi translation of
86 expressions, because the flow goes backwards through phis. We must
87 iterate to a fixpoint of the ANTIC sets, because we have a kill
88 set. Even in SSA form, values are not live over the entire
89 function, only from their definition point onwards. So we have to
90 remove values from the ANTIC set once we go past the definition
91 point of the leaders that make them up.
92 compute_antic/compute_antic_aux performs this computation.
94 Third, we perform insertions to make partially redundant
95 expressions fully redundant.
97 An expression is partially redundant (excluding partial
98 anticipation) if:
100 1. It is AVAIL in some, but not all, of the predecessors of a
101 given block.
102 2. It is ANTIC in all the predecessors.
104 In order to make it fully redundant, we insert the expression into
105 the predecessors where it is not available, but is ANTIC.
106 insert/insert_aux performs this insertion.
108 Fourth, we eliminate fully redundant expressions.
109 This is a simple statement walk that replaces redundant
110 calculations with the now available values. */
112 /* Representations of value numbers:
114 Value numbers are represented using the "value handle" approach.
115 This means that each SSA_NAME (and for other reasons to be
116 disclosed in a moment, expression nodes) has a value handle that
117 can be retrieved through get_value_handle. This value handle, *is*
118 the value number of the SSA_NAME. You can pointer compare the
119 value handles for equivalence purposes.
121 For debugging reasons, the value handle is internally more than
122 just a number, it is a VAR_DECL named "value.x", where x is a
123 unique number for each value number in use. This allows
124 expressions with SSA_NAMES replaced by value handles to still be
125 pretty printed in a sane way. They simply print as "value.3 *
126 value.5", etc.
128 Expression nodes have value handles associated with them as a
129 cache. Otherwise, we'd have to look them up again in the hash
130 table This makes significant difference (factor of two or more) on
131 some test cases. They can be thrown away after the pass is
132 finished. */
134 /* Representation of expressions on value numbers:
136 In some portions of this code, you will notice we allocate "fake"
137 analogues to the expression we are value numbering, and replace the
138 operands with the values of the expression. Since we work on
139 values, and not just names, we canonicalize expressions to value
140 expressions for use in the ANTIC sets, the EXP_GEN set, etc.
142 This is theoretically unnecessary, it just saves a bunch of
143 repeated get_value_handle and find_leader calls in the remainder of
144 the code, trading off temporary memory usage for speed. The tree
145 nodes aren't actually creating more garbage, since they are
146 allocated in a special pools which are thrown away at the end of
147 this pass.
149 All of this also means that if you print the EXP_GEN or ANTIC sets,
150 you will see "value.5 + value.7" in the set, instead of "a_55 +
151 b_66" or something. The only thing that actually cares about
152 seeing the value leaders is phi translation, and it needs to be
153 able to find the leader for a value in an arbitrary block, so this
154 "value expression" form is perfect for it (otherwise you'd do
155 get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
158 /* Representation of sets:
160 There are currently two types of sets used, hopefully to be unified soon.
161 The AVAIL sets do not need to be sorted in any particular order,
162 and thus, are simply represented as two bitmaps, one that keeps
163 track of values present in the set, and one that keeps track of
164 expressions present in the set.
166 The other sets are represented as doubly linked lists kept in topological
167 order, with an optional supporting bitmap of values present in the
168 set. The sets represent values, and the elements can be values or
169 expressions. The elements can appear in different sets, but each
170 element can only appear once in each set.
172 Since each node in the set represents a value, we also want to be
173 able to map expression, set pairs to something that tells us
174 whether the value is present is a set. We use a per-set bitmap for
175 that. The value handles also point to a linked list of the
176 expressions they represent via a tree annotation. This is mainly
177 useful only for debugging, since we don't do identity lookups. */
180 static bool in_fre = false;
182 /* A value set element. Basically a single linked list of
183 expressions/values. */
184 typedef struct value_set_node
186 /* An expression. */
187 tree expr;
189 /* A pointer to the next element of the value set. */
190 struct value_set_node *next;
191 } *value_set_node_t;
194 /* A value set. This is a singly linked list of value_set_node
195 elements with a possible bitmap that tells us what values exist in
196 the set. This set must be kept in topologically sorted order. */
197 typedef struct value_set
199 /* The head of the list. Used for iterating over the list in
200 order. */
201 value_set_node_t head;
203 /* The tail of the list. Used for tail insertions, which are
204 necessary to keep the set in topologically sorted order because
205 of how the set is built. */
206 value_set_node_t tail;
208 /* The length of the list. */
209 size_t length;
211 /* True if the set is indexed, which means it contains a backing
212 bitmap for quick determination of whether certain values exist in the
213 set. */
214 bool indexed;
216 /* The bitmap of values that exist in the set. May be NULL in an
217 empty or non-indexed set. */
218 bitmap values;
220 } *value_set_t;
223 /* An unordered bitmap set. One bitmap tracks values, the other,
224 expressions. */
225 typedef struct bitmap_set
227 bitmap expressions;
228 bitmap values;
229 } *bitmap_set_t;
231 /* Sets that we need to keep track of. */
232 typedef struct bb_value_sets
234 /* The EXP_GEN set, which represents expressions/values generated in
235 a basic block. */
236 value_set_t exp_gen;
238 /* The PHI_GEN set, which represents PHI results generated in a
239 basic block. */
240 bitmap_set_t phi_gen;
242 /* The TMP_GEN set, which represents results/temporaries generated
243 in a basic block. IE the LHS of an expression. */
244 bitmap_set_t tmp_gen;
246 /* The AVAIL_OUT set, which represents which values are available in
247 a given basic block. */
248 bitmap_set_t avail_out;
250 /* The ANTIC_IN set, which represents which values are anticiptable
251 in a given basic block. */
252 value_set_t antic_in;
254 /* The NEW_SETS set, which is used during insertion to augment the
255 AVAIL_OUT set of blocks with the new insertions performed during
256 the current iteration. */
257 bitmap_set_t new_sets;
258 } *bb_value_sets_t;
260 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
261 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
262 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
263 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
264 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
265 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
267 /* This structure is used to keep track of statistics on what
268 optimization PRE was able to perform. */
269 static struct
271 /* The number of RHS computations eliminated by PRE. */
272 int eliminations;
274 /* The number of new expressions/temporaries generated by PRE. */
275 int insertions;
277 /* The number of new PHI nodes added by PRE. */
278 int phis;
280 /* The number of values found constant. */
281 int constified;
283 } pre_stats;
286 static tree bitmap_find_leader (bitmap_set_t, tree);
287 static tree find_leader (value_set_t, tree);
288 static void value_insert_into_set (value_set_t, tree);
289 static void bitmap_value_insert_into_set (bitmap_set_t, tree);
290 static void bitmap_value_replace_in_set (bitmap_set_t, tree);
291 static void insert_into_set (value_set_t, tree);
292 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
293 static bool bitmap_set_contains_value (bitmap_set_t, tree);
294 static bitmap_set_t bitmap_set_new (void);
295 static value_set_t set_new (bool);
296 static bool is_undefined_value (tree);
297 static tree create_expression_by_pieces (basic_block, tree, tree);
300 /* We can add and remove elements and entries to and from sets
301 and hash tables, so we use alloc pools for them. */
303 static alloc_pool value_set_pool;
304 static alloc_pool bitmap_set_pool;
305 static alloc_pool value_set_node_pool;
306 static alloc_pool binary_node_pool;
307 static alloc_pool unary_node_pool;
308 static alloc_pool reference_node_pool;
309 static alloc_pool comparison_node_pool;
310 static alloc_pool expression_node_pool;
311 static alloc_pool list_node_pool;
312 static bitmap_obstack grand_bitmap_obstack;
314 /* Set of blocks with statements that have had its EH information
315 cleaned up. */
316 static bitmap need_eh_cleanup;
318 /* The phi_translate_table caches phi translations for a given
319 expression and predecessor. */
321 static htab_t phi_translate_table;
323 /* A three tuple {e, pred, v} used to cache phi translations in the
324 phi_translate_table. */
326 typedef struct expr_pred_trans_d
328 /* The expression. */
329 tree e;
331 /* The predecessor block along which we translated the expression. */
332 basic_block pred;
334 /* The value that resulted from the translation. */
335 tree v;
337 /* The hashcode for the expression, pred pair. This is cached for
338 speed reasons. */
339 hashval_t hashcode;
340 } *expr_pred_trans_t;
342 /* Return the hash value for a phi translation table entry. */
344 static hashval_t
345 expr_pred_trans_hash (const void *p)
347 const expr_pred_trans_t ve = (expr_pred_trans_t) p;
348 return ve->hashcode;
351 /* Return true if two phi translation table entries are the same.
352 P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
354 static int
355 expr_pred_trans_eq (const void *p1, const void *p2)
357 const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
358 const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
359 basic_block b1 = ve1->pred;
360 basic_block b2 = ve2->pred;
363 /* If they are not translations for the same basic block, they can't
364 be equal. */
365 if (b1 != b2)
366 return false;
368 /* If they are for the same basic block, determine if the
369 expressions are equal. */
370 if (expressions_equal_p (ve1->e, ve2->e))
371 return true;
373 return false;
376 /* Search in the phi translation table for the translation of
377 expression E in basic block PRED. Return the translated value, if
378 found, NULL otherwise. */
380 static inline tree
381 phi_trans_lookup (tree e, basic_block pred)
383 void **slot;
384 struct expr_pred_trans_d ept;
385 ept.e = e;
386 ept.pred = pred;
387 ept.hashcode = vn_compute (e, (unsigned long) pred, NULL);
388 slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
389 NO_INSERT);
390 if (!slot)
391 return NULL;
392 else
393 return ((expr_pred_trans_t) *slot)->v;
397 /* Add the tuple mapping from {expression E, basic block PRED} to
398 value V, to the phi translation table. */
400 static inline void
401 phi_trans_add (tree e, tree v, basic_block pred)
403 void **slot;
404 expr_pred_trans_t new_pair = XNEW (struct expr_pred_trans_d);
405 new_pair->e = e;
406 new_pair->pred = pred;
407 new_pair->v = v;
408 new_pair->hashcode = vn_compute (e, (unsigned long) pred, NULL);
409 slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
410 new_pair->hashcode, INSERT);
411 if (*slot)
412 free (*slot);
413 *slot = (void *) new_pair;
417 /* Add expression E to the expression set of value V. */
419 void
420 add_to_value (tree v, tree e)
422 /* Constants have no expression sets. */
423 if (is_gimple_min_invariant (v))
424 return;
426 if (VALUE_HANDLE_EXPR_SET (v) == NULL)
427 VALUE_HANDLE_EXPR_SET (v) = set_new (false);
429 insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
433 /* Return true if value V exists in the bitmap for SET. */
435 static inline bool
436 value_exists_in_set_bitmap (value_set_t set, tree v)
438 if (!set->values)
439 return false;
441 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (v));
445 /* Remove value V from the bitmap for SET. */
447 static void
448 value_remove_from_set_bitmap (value_set_t set, tree v)
450 gcc_assert (set->indexed);
452 if (!set->values)
453 return;
455 bitmap_clear_bit (set->values, VALUE_HANDLE_ID (v));
459 /* Insert the value number V into the bitmap of values existing in
460 SET. */
462 static inline void
463 value_insert_into_set_bitmap (value_set_t set, tree v)
465 gcc_assert (set->indexed);
467 if (set->values == NULL)
468 set->values = BITMAP_ALLOC (&grand_bitmap_obstack);
470 bitmap_set_bit (set->values, VALUE_HANDLE_ID (v));
474 /* Create a new bitmap set and return it. */
476 static bitmap_set_t
477 bitmap_set_new (void)
479 bitmap_set_t ret = (bitmap_set_t) pool_alloc (bitmap_set_pool);
480 ret->expressions = BITMAP_ALLOC (&grand_bitmap_obstack);
481 ret->values = BITMAP_ALLOC (&grand_bitmap_obstack);
482 return ret;
485 /* Create a new set. */
487 static value_set_t
488 set_new (bool indexed)
490 value_set_t ret;
491 ret = (value_set_t) pool_alloc (value_set_pool);
492 ret->head = ret->tail = NULL;
493 ret->length = 0;
494 ret->indexed = indexed;
495 ret->values = NULL;
496 return ret;
499 /* Insert an expression EXPR into a bitmapped set. */
501 static void
502 bitmap_insert_into_set (bitmap_set_t set, tree expr)
504 tree val;
505 /* XXX: For now, we only let SSA_NAMES into the bitmap sets. */
506 gcc_assert (TREE_CODE (expr) == SSA_NAME);
507 val = get_value_handle (expr);
509 gcc_assert (val);
510 if (!is_gimple_min_invariant (val))
512 bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
513 bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
517 /* Insert EXPR into SET. */
519 static void
520 insert_into_set (value_set_t set, tree expr)
522 value_set_node_t newnode = (value_set_node_t) pool_alloc (value_set_node_pool);
523 tree val = get_value_handle (expr);
524 gcc_assert (val);
526 if (is_gimple_min_invariant (val))
527 return;
529 /* For indexed sets, insert the value into the set value bitmap.
530 For all sets, add it to the linked list and increment the list
531 length. */
532 if (set->indexed)
533 value_insert_into_set_bitmap (set, val);
535 newnode->next = NULL;
536 newnode->expr = expr;
537 set->length ++;
538 if (set->head == NULL)
540 set->head = set->tail = newnode;
542 else
544 set->tail->next = newnode;
545 set->tail = newnode;
549 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
551 static void
552 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
554 bitmap_copy (dest->expressions, orig->expressions);
555 bitmap_copy (dest->values, orig->values);
558 /* Perform bitmapped set operation DEST &= ORIG. */
560 static void
561 bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig)
563 bitmap_iterator bi;
564 unsigned int i;
565 bitmap temp = BITMAP_ALLOC (&grand_bitmap_obstack);
567 bitmap_and_into (dest->values, orig->values);
568 bitmap_copy (temp, dest->expressions);
569 EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
571 tree name = ssa_name (i);
572 tree val = get_value_handle (name);
573 if (!bitmap_bit_p (dest->values, VALUE_HANDLE_ID (val)))
574 bitmap_clear_bit (dest->expressions, i);
579 /* Perform bitmapped value set operation DEST = DEST & ~ORIG. */
581 static void
582 bitmap_set_and_compl (bitmap_set_t dest, bitmap_set_t orig)
584 bitmap_iterator bi;
585 unsigned int i;
586 bitmap temp = BITMAP_ALLOC (&grand_bitmap_obstack);
588 bitmap_and_compl_into (dest->values, orig->values);
589 bitmap_copy (temp, dest->expressions);
590 EXECUTE_IF_SET_IN_BITMAP (temp, 0, i, bi)
592 tree name = ssa_name (i);
593 tree val = get_value_handle (name);
594 if (!bitmap_bit_p (dest->values, VALUE_HANDLE_ID (val)))
595 bitmap_clear_bit (dest->expressions, i);
599 /* Return true if the bitmap set SET is empty. */
601 static bool
602 bitmap_set_empty_p (bitmap_set_t set)
604 return bitmap_empty_p (set->values);
607 /* Copy the set ORIG to the set DEST. */
609 static void
610 set_copy (value_set_t dest, value_set_t orig)
612 value_set_node_t node;
614 if (!orig || !orig->head)
615 return;
617 for (node = orig->head;
618 node;
619 node = node->next)
621 insert_into_set (dest, node->expr);
625 /* Remove EXPR from SET. */
627 static void
628 set_remove (value_set_t set, tree expr)
630 value_set_node_t node, prev;
632 /* Remove the value of EXPR from the bitmap, decrement the set
633 length, and remove it from the actual double linked list. */
634 value_remove_from_set_bitmap (set, get_value_handle (expr));
635 set->length--;
636 prev = NULL;
637 for (node = set->head;
638 node != NULL;
639 prev = node, node = node->next)
641 if (node->expr == expr)
643 if (prev == NULL)
644 set->head = node->next;
645 else
646 prev->next= node->next;
648 if (node == set->tail)
649 set->tail = prev;
650 pool_free (value_set_node_pool, node);
651 return;
656 /* Return true if SET contains the value VAL. */
658 static bool
659 set_contains_value (value_set_t set, tree val)
661 /* All constants are in every set. */
662 if (is_gimple_min_invariant (val))
663 return true;
665 if (set->length == 0)
666 return false;
668 return value_exists_in_set_bitmap (set, val);
671 /* Return true if bitmapped set SET contains the expression EXPR. */
672 static bool
673 bitmap_set_contains (bitmap_set_t set, tree expr)
675 /* All constants are in every set. */
676 if (is_gimple_min_invariant (get_value_handle (expr)))
677 return true;
679 /* XXX: Bitmapped sets only contain SSA_NAME's for now. */
680 if (TREE_CODE (expr) != SSA_NAME)
681 return false;
682 return bitmap_bit_p (set->expressions, SSA_NAME_VERSION (expr));
686 /* Return true if bitmapped set SET contains the value VAL. */
688 static bool
689 bitmap_set_contains_value (bitmap_set_t set, tree val)
691 if (is_gimple_min_invariant (val))
692 return true;
693 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
696 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
698 static void
699 bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
701 value_set_t exprset;
702 value_set_node_t node;
703 if (is_gimple_min_invariant (lookfor))
704 return;
705 if (!bitmap_set_contains_value (set, lookfor))
706 return;
708 /* The number of expressions having a given value is usually
709 significantly less than the total number of expressions in SET.
710 Thus, rather than check, for each expression in SET, whether it
711 has the value LOOKFOR, we walk the reverse mapping that tells us
712 what expressions have a given value, and see if any of those
713 expressions are in our set. For large testcases, this is about
714 5-10x faster than walking the bitmap. If this is somehow a
715 significant lose for some cases, we can choose which set to walk
716 based on the set size. */
717 exprset = VALUE_HANDLE_EXPR_SET (lookfor);
718 for (node = exprset->head; node; node = node->next)
720 if (TREE_CODE (node->expr) == SSA_NAME)
722 if (bitmap_bit_p (set->expressions, SSA_NAME_VERSION (node->expr)))
724 bitmap_clear_bit (set->expressions, SSA_NAME_VERSION (node->expr));
725 bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
726 return;
732 /* Subtract bitmapped set B from value set A, and return the new set. */
734 static value_set_t
735 bitmap_set_subtract_from_value_set (value_set_t a, bitmap_set_t b,
736 bool indexed)
738 value_set_t ret = set_new (indexed);
739 value_set_node_t node;
740 for (node = a->head;
741 node;
742 node = node->next)
744 if (!bitmap_set_contains (b, node->expr))
745 insert_into_set (ret, node->expr);
747 return ret;
750 /* Return true if two sets are equal. */
752 static bool
753 set_equal (value_set_t a, value_set_t b)
755 value_set_node_t node;
757 if (a->length != b->length)
758 return false;
759 for (node = a->head;
760 node;
761 node = node->next)
763 if (!set_contains_value (b, get_value_handle (node->expr)))
764 return false;
766 return true;
769 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
770 and add it otherwise. */
772 static void
773 bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
775 tree val = get_value_handle (expr);
776 if (bitmap_set_contains_value (set, val))
777 bitmap_set_replace_value (set, val, expr);
778 else
779 bitmap_insert_into_set (set, expr);
782 /* Insert EXPR into SET if EXPR's value is not already present in
783 SET. */
785 static void
786 bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
788 tree val = get_value_handle (expr);
790 if (is_gimple_min_invariant (val))
791 return;
793 if (!bitmap_set_contains_value (set, val))
794 bitmap_insert_into_set (set, expr);
797 /* Insert the value for EXPR into SET, if it doesn't exist already. */
799 static void
800 value_insert_into_set (value_set_t set, tree expr)
802 tree val = get_value_handle (expr);
804 /* Constant and invariant values exist everywhere, and thus,
805 actually keeping them in the sets is pointless. */
806 if (is_gimple_min_invariant (val))
807 return;
809 if (!set_contains_value (set, val))
810 insert_into_set (set, expr);
814 /* Print out SET to OUTFILE. */
816 static void
817 bitmap_print_value_set (FILE *outfile, bitmap_set_t set,
818 const char *setname, int blockindex)
820 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
821 if (set)
823 bool first = true;
824 unsigned i;
825 bitmap_iterator bi;
827 EXECUTE_IF_SET_IN_BITMAP (set->expressions, 0, i, bi)
829 if (!first)
830 fprintf (outfile, ", ");
831 first = false;
832 print_generic_expr (outfile, ssa_name (i), 0);
834 fprintf (outfile, " (");
835 print_generic_expr (outfile, get_value_handle (ssa_name (i)), 0);
836 fprintf (outfile, ") ");
839 fprintf (outfile, " }\n");
841 /* Print out the value_set SET to OUTFILE. */
843 static void
844 print_value_set (FILE *outfile, value_set_t set,
845 const char *setname, int blockindex)
847 value_set_node_t node;
848 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
849 if (set)
851 for (node = set->head;
852 node;
853 node = node->next)
855 print_generic_expr (outfile, node->expr, 0);
857 fprintf (outfile, " (");
858 print_generic_expr (outfile, get_value_handle (node->expr), 0);
859 fprintf (outfile, ") ");
861 if (node->next)
862 fprintf (outfile, ", ");
866 fprintf (outfile, " }\n");
869 /* Print out the expressions that have VAL to OUTFILE. */
871 void
872 print_value_expressions (FILE *outfile, tree val)
874 if (VALUE_HANDLE_EXPR_SET (val))
876 char s[10];
877 sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
878 print_value_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
883 void
884 debug_value_expressions (tree val)
886 print_value_expressions (stderr, val);
890 void debug_value_set (value_set_t, const char *, int);
892 void
893 debug_value_set (value_set_t set, const char *setname, int blockindex)
895 print_value_set (stderr, set, setname, blockindex);
898 /* Return the folded version of T if T, when folded, is a gimple
899 min_invariant. Otherwise, return T. */
901 static tree
902 fully_constant_expression (tree t)
904 tree folded;
905 folded = fold (t);
906 if (folded && is_gimple_min_invariant (folded))
907 return folded;
908 return t;
911 /* Return a copy of a chain of nodes, chained through the TREE_CHAIN field.
912 For example, this can copy a list made of TREE_LIST nodes.
913 Allocates the nodes in list_node_pool*/
915 static tree
916 pool_copy_list (tree list)
918 tree head;
919 tree prev, next;
921 if (list == 0)
922 return 0;
923 head = (tree) pool_alloc (list_node_pool);
925 memcpy (head, list, tree_size (list));
926 prev = head;
928 next = TREE_CHAIN (list);
929 while (next)
931 TREE_CHAIN (prev) = (tree) pool_alloc (list_node_pool);
932 memcpy (TREE_CHAIN (prev), next, tree_size (next));
933 prev = TREE_CHAIN (prev);
934 next = TREE_CHAIN (next);
936 return head;
940 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
941 the phis in PRED. Return NULL if we can't find a leader for each
942 part of the translated expression. */
944 static tree
945 phi_translate (tree expr, value_set_t set, basic_block pred,
946 basic_block phiblock)
948 tree phitrans = NULL;
949 tree oldexpr = expr;
951 if (expr == NULL)
952 return NULL;
954 if (is_gimple_min_invariant (expr))
955 return expr;
957 /* Phi translations of a given expression don't change. */
958 phitrans = phi_trans_lookup (expr, pred);
959 if (phitrans)
960 return phitrans;
962 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
964 case tcc_expression:
966 if (TREE_CODE (expr) != CALL_EXPR)
967 return NULL;
968 else
970 tree oldop0 = TREE_OPERAND (expr, 0);
971 tree oldarglist = TREE_OPERAND (expr, 1);
972 tree oldop2 = TREE_OPERAND (expr, 2);
973 tree newop0;
974 tree newarglist;
975 tree newop2 = NULL;
976 tree oldwalker;
977 tree newwalker;
978 tree newexpr;
979 bool listchanged = false;
981 /* Call expressions are kind of weird because they have an
982 argument list. We don't want to value number the list
983 as one value number, because that doesn't make much
984 sense, and just breaks the support functions we call,
985 which expect TREE_OPERAND (call_expr, 2) to be a
986 TREE_LIST. */
988 newop0 = phi_translate (find_leader (set, oldop0),
989 set, pred, phiblock);
990 if (newop0 == NULL)
991 return NULL;
992 if (oldop2)
994 newop2 = phi_translate (find_leader (set, oldop2),
995 set, pred, phiblock);
996 if (newop2 == NULL)
997 return NULL;
1000 /* phi translate the argument list piece by piece.
1002 We could actually build the list piece by piece here,
1003 but it's likely to not be worth the memory we will save,
1004 unless you have millions of call arguments. */
1006 newarglist = pool_copy_list (oldarglist);
1007 for (oldwalker = oldarglist, newwalker = newarglist;
1008 oldwalker && newwalker;
1009 oldwalker = TREE_CHAIN (oldwalker),
1010 newwalker = TREE_CHAIN (newwalker))
1013 tree oldval = TREE_VALUE (oldwalker);
1014 tree newval;
1015 if (oldval)
1017 newval = phi_translate (find_leader (set, oldval),
1018 set, pred, phiblock);
1019 if (newval == NULL)
1020 return NULL;
1021 if (newval != oldval)
1023 listchanged = true;
1024 TREE_VALUE (newwalker) = get_value_handle (newval);
1028 if (listchanged)
1029 vn_lookup_or_add (newarglist, NULL);
1031 if (listchanged || (newop0 != oldop0) || (oldop2 != newop2))
1033 newexpr = (tree) pool_alloc (expression_node_pool);
1034 memcpy (newexpr, expr, tree_size (expr));
1035 TREE_OPERAND (newexpr, 0) = newop0 == oldop0 ? oldop0 : get_value_handle (newop0);
1036 TREE_OPERAND (newexpr, 1) = listchanged ? newarglist : oldarglist;
1037 TREE_OPERAND (newexpr, 2) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
1038 create_tree_ann (newexpr);
1039 vn_lookup_or_add (newexpr, NULL);
1040 expr = newexpr;
1041 phi_trans_add (oldexpr, newexpr, pred);
1045 return expr;
1047 case tcc_reference:
1048 /* XXX: Until we have PRE of loads working, none will be ANTIC. */
1049 return NULL;
1051 case tcc_binary:
1052 case tcc_comparison:
1054 tree oldop1 = TREE_OPERAND (expr, 0);
1055 tree oldop2 = TREE_OPERAND (expr, 1);
1056 tree newop1;
1057 tree newop2;
1058 tree newexpr;
1060 newop1 = phi_translate (find_leader (set, oldop1),
1061 set, pred, phiblock);
1062 if (newop1 == NULL)
1063 return NULL;
1064 newop2 = phi_translate (find_leader (set, oldop2),
1065 set, pred, phiblock);
1066 if (newop2 == NULL)
1067 return NULL;
1068 if (newop1 != oldop1 || newop2 != oldop2)
1070 tree t;
1071 newexpr = (tree) pool_alloc (binary_node_pool);
1072 memcpy (newexpr, expr, tree_size (expr));
1073 TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
1074 TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
1075 t = fully_constant_expression (newexpr);
1076 if (t != newexpr)
1078 pool_free (binary_node_pool, newexpr);
1079 newexpr = t;
1081 else
1083 create_tree_ann (newexpr);
1084 vn_lookup_or_add (newexpr, NULL);
1086 expr = newexpr;
1087 phi_trans_add (oldexpr, newexpr, pred);
1090 return expr;
1092 case tcc_unary:
1094 tree oldop1 = TREE_OPERAND (expr, 0);
1095 tree newop1;
1096 tree newexpr;
1098 newop1 = phi_translate (find_leader (set, oldop1),
1099 set, pred, phiblock);
1100 if (newop1 == NULL)
1101 return NULL;
1102 if (newop1 != oldop1)
1104 tree t;
1105 newexpr = (tree) pool_alloc (unary_node_pool);
1106 memcpy (newexpr, expr, tree_size (expr));
1107 TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
1108 t = fully_constant_expression (newexpr);
1109 if (t != newexpr)
1111 pool_free (unary_node_pool, newexpr);
1112 newexpr = t;
1114 else
1116 create_tree_ann (newexpr);
1117 vn_lookup_or_add (newexpr, NULL);
1119 expr = newexpr;
1120 phi_trans_add (oldexpr, newexpr, pred);
1123 return expr;
1125 case tcc_exceptional:
1127 tree phi = NULL;
1128 edge e;
1129 gcc_assert (TREE_CODE (expr) == SSA_NAME);
1130 if (TREE_CODE (SSA_NAME_DEF_STMT (expr)) == PHI_NODE)
1131 phi = SSA_NAME_DEF_STMT (expr);
1132 else
1133 return expr;
1135 e = find_edge (pred, bb_for_stmt (phi));
1136 if (e)
1138 if (is_undefined_value (PHI_ARG_DEF (phi, e->dest_idx)))
1139 return NULL;
1140 vn_lookup_or_add (PHI_ARG_DEF (phi, e->dest_idx), NULL);
1141 return PHI_ARG_DEF (phi, e->dest_idx);
1144 return expr;
1146 default:
1147 gcc_unreachable ();
1151 /* For each expression in SET, translate the value handles through phi nodes
1152 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1153 expressions in DEST. */
1155 static void
1156 phi_translate_set (value_set_t dest, value_set_t set, basic_block pred,
1157 basic_block phiblock)
1159 value_set_node_t node;
1160 for (node = set->head;
1161 node;
1162 node = node->next)
1164 tree translated;
1165 translated = phi_translate (node->expr, set, pred, phiblock);
1166 phi_trans_add (node->expr, translated, pred);
1168 if (translated != NULL)
1169 value_insert_into_set (dest, translated);
1173 /* Find the leader for a value (i.e., the name representing that
1174 value) in a given set, and return it. Return NULL if no leader is
1175 found. */
1177 static tree
1178 bitmap_find_leader (bitmap_set_t set, tree val)
1180 if (val == NULL)
1181 return NULL;
1183 if (is_gimple_min_invariant (val))
1184 return val;
1185 if (bitmap_set_contains_value (set, val))
1187 /* Rather than walk the entire bitmap of expressions, and see
1188 whether any of them has the value we are looking for, we look
1189 at the reverse mapping, which tells us the set of expressions
1190 that have a given value (IE value->expressions with that
1191 value) and see if any of those expressions are in our set.
1192 The number of expressions per value is usually significantly
1193 less than the number of expressions in the set. In fact, for
1194 large testcases, doing it this way is roughly 5-10x faster
1195 than walking the bitmap.
1196 If this is somehow a significant lose for some cases, we can
1197 choose which set to walk based on which set is smaller. */
1198 value_set_t exprset;
1199 value_set_node_t node;
1200 exprset = VALUE_HANDLE_EXPR_SET (val);
1201 for (node = exprset->head; node; node = node->next)
1203 if (TREE_CODE (node->expr) == SSA_NAME)
1205 if (bitmap_bit_p (set->expressions,
1206 SSA_NAME_VERSION (node->expr)))
1207 return node->expr;
1211 return NULL;
1215 /* Find the leader for a value (i.e., the name representing that
1216 value) in a given set, and return it. Return NULL if no leader is
1217 found. */
1219 static tree
1220 find_leader (value_set_t set, tree val)
1222 value_set_node_t node;
1224 if (val == NULL)
1225 return NULL;
1227 /* Constants represent themselves. */
1228 if (is_gimple_min_invariant (val))
1229 return val;
1231 if (set->length == 0)
1232 return NULL;
1234 if (value_exists_in_set_bitmap (set, val))
1236 for (node = set->head;
1237 node;
1238 node = node->next)
1240 if (get_value_handle (node->expr) == val)
1241 return node->expr;
1245 return NULL;
1248 /* Determine if the expression EXPR is valid in SET. This means that
1249 we have a leader for each part of the expression (if it consists of
1250 values), or the expression is an SSA_NAME.
1252 NB: We never should run into a case where we have SSA_NAME +
1253 SSA_NAME or SSA_NAME + value. The sets valid_in_set is called on,
1254 the ANTIC sets, will only ever have SSA_NAME's or value expressions
1255 (IE VALUE1 + VALUE2, *VALUE1, VALUE1 < VALUE2) */
1257 static bool
1258 valid_in_set (value_set_t set, tree expr)
1260 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1262 case tcc_binary:
1263 case tcc_comparison:
1265 tree op1 = TREE_OPERAND (expr, 0);
1266 tree op2 = TREE_OPERAND (expr, 1);
1267 return set_contains_value (set, op1) && set_contains_value (set, op2);
1270 case tcc_unary:
1272 tree op1 = TREE_OPERAND (expr, 0);
1273 return set_contains_value (set, op1);
1276 case tcc_expression:
1278 if (TREE_CODE (expr) == CALL_EXPR)
1280 tree op0 = TREE_OPERAND (expr, 0);
1281 tree arglist = TREE_OPERAND (expr, 1);
1282 tree op2 = TREE_OPERAND (expr, 2);
1284 /* Check the non-list operands first. */
1285 if (!set_contains_value (set, op0)
1286 || (op2 && !set_contains_value (set, op2)))
1287 return false;
1289 /* Now check the operands. */
1290 for (; arglist; arglist = TREE_CHAIN (arglist))
1292 if (!set_contains_value (set, TREE_VALUE (arglist)))
1293 return false;
1295 return true;
1297 return false;
1300 case tcc_reference:
1301 /* XXX: Until PRE of loads works, no reference nodes are ANTIC. */
1302 return false;
1304 case tcc_exceptional:
1305 gcc_assert (TREE_CODE (expr) == SSA_NAME);
1306 return true;
1308 case tcc_declaration:
1309 /* VAR_DECL and PARM_DECL are never anticipatable. */
1310 return false;
1312 default:
1313 /* No other cases should be encountered. */
1314 gcc_unreachable ();
1318 /* Clean the set of expressions that are no longer valid in SET. This
1319 means expressions that are made up of values we have no leaders for
1320 in SET. */
1322 static void
1323 clean (value_set_t set)
1325 value_set_node_t node;
1326 value_set_node_t next;
1327 node = set->head;
1328 while (node)
1330 next = node->next;
1331 if (!valid_in_set (set, node->expr))
1332 set_remove (set, node->expr);
1333 node = next;
1337 static sbitmap has_abnormal_preds;
1339 /* Compute the ANTIC set for BLOCK.
1341 If succs(BLOCK) > 1 then
1342 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
1343 else if succs(BLOCK) == 1 then
1344 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
1346 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
1348 XXX: It would be nice to either write a set_clear, and use it for
1349 ANTIC_OUT, or to mark the antic_out set as deleted at the end
1350 of this routine, so that the pool can hand the same memory back out
1351 again for the next ANTIC_OUT. */
1353 static bool
1354 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
1356 basic_block son;
1357 bool changed = false;
1358 value_set_t S, old, ANTIC_OUT;
1359 value_set_node_t node;
1361 ANTIC_OUT = S = NULL;
1363 /* If any edges from predecessors are abnormal, antic_in is empty,
1364 so do nothing. */
1365 if (block_has_abnormal_pred_edge)
1366 goto maybe_dump_sets;
1368 old = set_new (false);
1369 set_copy (old, ANTIC_IN (block));
1370 ANTIC_OUT = set_new (true);
1372 /* If the block has no successors, ANTIC_OUT is empty. */
1373 if (EDGE_COUNT (block->succs) == 0)
1375 /* If we have one successor, we could have some phi nodes to
1376 translate through. */
1377 else if (single_succ_p (block))
1379 phi_translate_set (ANTIC_OUT, ANTIC_IN (single_succ (block)),
1380 block, single_succ (block));
1382 /* If we have multiple successors, we take the intersection of all of
1383 them. */
1384 else
1386 VEC(basic_block, heap) * worklist;
1387 edge e;
1388 size_t i;
1389 basic_block bprime, first;
1390 edge_iterator ei;
1392 worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
1393 FOR_EACH_EDGE (e, ei, block->succs)
1394 VEC_quick_push (basic_block, worklist, e->dest);
1395 first = VEC_index (basic_block, worklist, 0);
1396 set_copy (ANTIC_OUT, ANTIC_IN (first));
1398 for (i = 1; VEC_iterate (basic_block, worklist, i, bprime); i++)
1400 node = ANTIC_OUT->head;
1401 while (node)
1403 tree val;
1404 value_set_node_t next = node->next;
1405 val = get_value_handle (node->expr);
1406 if (!set_contains_value (ANTIC_IN (bprime), val))
1407 set_remove (ANTIC_OUT, node->expr);
1408 node = next;
1411 VEC_free (basic_block, heap, worklist);
1414 /* Generate ANTIC_OUT - TMP_GEN. */
1415 S = bitmap_set_subtract_from_value_set (ANTIC_OUT, TMP_GEN (block), false);
1417 /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1418 ANTIC_IN (block) = bitmap_set_subtract_from_value_set (EXP_GEN (block),
1419 TMP_GEN (block),
1420 true);
1422 /* Then union in the ANTIC_OUT - TMP_GEN values,
1423 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
1424 for (node = S->head; node; node = node->next)
1425 value_insert_into_set (ANTIC_IN (block), node->expr);
1427 clean (ANTIC_IN (block));
1428 if (!set_equal (old, ANTIC_IN (block)))
1429 changed = true;
1431 maybe_dump_sets:
1432 if (dump_file && (dump_flags & TDF_DETAILS))
1434 if (ANTIC_OUT)
1435 print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1436 print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1437 if (S)
1438 print_value_set (dump_file, S, "S", block->index);
1441 for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1442 son;
1443 son = next_dom_son (CDI_POST_DOMINATORS, son))
1445 changed |= compute_antic_aux (son,
1446 TEST_BIT (has_abnormal_preds, son->index));
1448 return changed;
1451 /* Compute ANTIC sets. */
1453 static void
1454 compute_antic (void)
1456 bool changed = true;
1457 int num_iterations = 0;
1458 basic_block block;
1460 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
1461 We pre-build the map of blocks with incoming abnormal edges here. */
1462 has_abnormal_preds = sbitmap_alloc (last_basic_block);
1463 sbitmap_zero (has_abnormal_preds);
1464 FOR_EACH_BB (block)
1466 edge_iterator ei;
1467 edge e;
1469 FOR_EACH_EDGE (e, ei, block->preds)
1470 if (e->flags & EDGE_ABNORMAL)
1472 SET_BIT (has_abnormal_preds, block->index);
1473 break;
1476 /* While we are here, give empty ANTIC_IN sets to each block. */
1477 ANTIC_IN (block) = set_new (true);
1479 /* At the exit block we anticipate nothing. */
1480 ANTIC_IN (EXIT_BLOCK_PTR) = set_new (true);
1482 while (changed)
1484 num_iterations++;
1485 changed = false;
1486 changed = compute_antic_aux (EXIT_BLOCK_PTR, false);
1489 sbitmap_free (has_abnormal_preds);
1491 if (dump_file && (dump_flags & TDF_STATS))
1492 fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1495 static VEC(tree,heap) *inserted_exprs;
1496 /* Find a leader for an expression, or generate one using
1497 create_expression_by_pieces if it's ANTIC but
1498 complex.
1499 BLOCK is the basic_block we are looking for leaders in.
1500 EXPR is the expression to find a leader or generate for.
1501 STMTS is the statement list to put the inserted expressions on.
1502 Returns the SSA_NAME of the LHS of the generated expression or the
1503 leader. */
1505 static tree
1506 find_or_generate_expression (basic_block block, tree expr, tree stmts)
1508 tree genop = bitmap_find_leader (AVAIL_OUT (block), expr);
1510 /* If it's still NULL, it must be a complex expression, so generate
1511 it recursively. */
1512 if (genop == NULL)
1514 genop = VALUE_HANDLE_EXPR_SET (expr)->head->expr;
1515 gcc_assert (UNARY_CLASS_P (genop)
1516 || BINARY_CLASS_P (genop)
1517 || COMPARISON_CLASS_P (genop)
1518 || REFERENCE_CLASS_P (genop)
1519 || TREE_CODE (genop) == CALL_EXPR);
1520 genop = create_expression_by_pieces (block, genop, stmts);
1522 return genop;
1525 #define NECESSARY(stmt) stmt->common.asm_written_flag
1526 /* Create an expression in pieces, so that we can handle very complex
1527 expressions that may be ANTIC, but not necessary GIMPLE.
1528 BLOCK is the basic block the expression will be inserted into,
1529 EXPR is the expression to insert (in value form)
1530 STMTS is a statement list to append the necessary insertions into.
1532 This function will die if we hit some value that shouldn't be
1533 ANTIC but is (IE there is no leader for it, or its components).
1534 This function may also generate expressions that are themselves
1535 partially or fully redundant. Those that are will be either made
1536 fully redundant during the next iteration of insert (for partially
1537 redundant ones), or eliminated by eliminate (for fully redundant
1538 ones). */
1540 static tree
1541 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
1543 tree temp, name;
1544 tree folded, forced_stmts, newexpr;
1545 tree v;
1546 tree_stmt_iterator tsi;
1548 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1550 case tcc_expression:
1552 tree op0, op2;
1553 tree arglist;
1554 tree genop0, genop2;
1555 tree genarglist;
1556 tree walker, genwalker;
1558 gcc_assert (TREE_CODE (expr) == CALL_EXPR);
1559 genop2 = NULL;
1561 op0 = TREE_OPERAND (expr, 0);
1562 arglist = TREE_OPERAND (expr, 1);
1563 op2 = TREE_OPERAND (expr, 2);
1565 genop0 = find_or_generate_expression (block, op0, stmts);
1566 genarglist = copy_list (arglist);
1567 for (walker = arglist, genwalker = genarglist;
1568 genwalker && walker;
1569 genwalker = TREE_CHAIN (genwalker), walker = TREE_CHAIN (walker))
1571 TREE_VALUE (genwalker) = find_or_generate_expression (block,
1572 TREE_VALUE (walker),
1573 stmts);
1576 if (op2)
1577 genop2 = find_or_generate_expression (block, op2, stmts);
1578 folded = fold_build3 (TREE_CODE (expr), TREE_TYPE (expr),
1579 genop0, genarglist, genop2);
1580 break;
1584 break;
1586 case tcc_binary:
1587 case tcc_comparison:
1589 tree op1 = TREE_OPERAND (expr, 0);
1590 tree op2 = TREE_OPERAND (expr, 1);
1591 tree genop1 = find_or_generate_expression (block, op1, stmts);
1592 tree genop2 = find_or_generate_expression (block, op2, stmts);
1593 folded = fold_build2 (TREE_CODE (expr), TREE_TYPE (expr),
1594 genop1, genop2);
1595 break;
1598 case tcc_unary:
1600 tree op1 = TREE_OPERAND (expr, 0);
1601 tree genop1 = find_or_generate_expression (block, op1, stmts);
1602 folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
1603 genop1);
1604 break;
1607 default:
1608 gcc_unreachable ();
1611 /* Force the generated expression to be a sequence of GIMPLE
1612 statements.
1613 We have to call unshare_expr because force_gimple_operand may
1614 modify the tree we pass to it. */
1615 newexpr = force_gimple_operand (unshare_expr (folded), &forced_stmts,
1616 false, NULL);
1618 /* If we have any intermediate expressions to the value sets, add them
1619 to the value sets and chain them on in the instruction stream. */
1620 if (forced_stmts)
1622 tsi = tsi_start (forced_stmts);
1623 for (; !tsi_end_p (tsi); tsi_next (&tsi))
1625 tree stmt = tsi_stmt (tsi);
1626 tree forcedname = TREE_OPERAND (stmt, 0);
1627 tree forcedexpr = TREE_OPERAND (stmt, 1);
1628 tree val = vn_lookup_or_add (forcedexpr, NULL);
1630 VEC_safe_push (tree, heap, inserted_exprs, stmt);
1631 vn_add (forcedname, val, NULL);
1632 bitmap_value_replace_in_set (NEW_SETS (block), forcedname);
1633 bitmap_value_replace_in_set (AVAIL_OUT (block), forcedname);
1635 tsi = tsi_last (stmts);
1636 tsi_link_after (&tsi, forced_stmts, TSI_CONTINUE_LINKING);
1639 /* Build and insert the assignment of the end result to the temporary
1640 that we will return. */
1641 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1642 add_referenced_tmp_var (temp);
1643 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
1644 DECL_COMPLEX_GIMPLE_REG_P (temp) = 1;
1645 newexpr = build2 (MODIFY_EXPR, TREE_TYPE (expr), temp, newexpr);
1646 name = make_ssa_name (temp, newexpr);
1647 TREE_OPERAND (newexpr, 0) = name;
1648 NECESSARY (newexpr) = 0;
1649 tsi = tsi_last (stmts);
1650 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1651 VEC_safe_push (tree, heap, inserted_exprs, newexpr);
1653 /* Add a value handle to the temporary.
1654 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
1655 we are creating the expression by pieces, and this particular piece of
1656 the expression may have been represented. There is no harm in replacing
1657 here. */
1658 v = get_value_handle (expr);
1659 vn_add (name, v, NULL);
1660 bitmap_value_replace_in_set (NEW_SETS (block), name);
1661 bitmap_value_replace_in_set (AVAIL_OUT (block), name);
1663 pre_stats.insertions++;
1664 if (dump_file && (dump_flags & TDF_DETAILS))
1666 fprintf (dump_file, "Inserted ");
1667 print_generic_expr (dump_file, newexpr, 0);
1668 fprintf (dump_file, " in predecessor %d\n", block->index);
1671 return name;
1674 /* Insert the to-be-made-available values of NODE for each predecessor, stored
1675 in AVAIL, into the predecessors of BLOCK, and merge the result with a phi
1676 node, given the same value handle as NODE. The prefix of the phi node is
1677 given with TMPNAME. Return true if we have inserted new stuff. */
1679 static bool
1680 insert_into_preds_of_block (basic_block block, value_set_node_t node,
1681 tree *avail, const char *tmpname)
1683 tree val = get_value_handle (node->expr);
1684 edge pred;
1685 bool insertions = false;
1686 bool nophi = false;
1687 basic_block bprime;
1688 tree eprime;
1689 edge_iterator ei;
1690 tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
1691 tree temp;
1693 if (dump_file && (dump_flags & TDF_DETAILS))
1695 fprintf (dump_file, "Found partial redundancy for expression ");
1696 print_generic_expr (dump_file, node->expr, 0);
1697 fprintf (dump_file, "\n");
1700 /* Make sure we aren't creating an induction variable. */
1701 if (block->loop_depth > 0 && EDGE_COUNT (block->preds) == 2)
1703 bool firstinsideloop = false;
1704 bool secondinsideloop = false;
1705 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
1706 EDGE_PRED (block, 0)->src);
1707 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
1708 EDGE_PRED (block, 1)->src);
1709 /* Induction variables only have one edge inside the loop. */
1710 if (firstinsideloop ^ secondinsideloop)
1712 if (dump_file && (dump_flags & TDF_DETAILS))
1713 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
1714 nophi = true;
1719 /* Make the necessary insertions. */
1720 FOR_EACH_EDGE (pred, ei, block->preds)
1722 tree stmts = alloc_stmt_list ();
1723 tree builtexpr;
1724 bprime = pred->src;
1725 eprime = avail[bprime->index];
1726 if (BINARY_CLASS_P (eprime)
1727 || COMPARISON_CLASS_P (eprime)
1728 || UNARY_CLASS_P (eprime)
1729 || TREE_CODE (eprime) == CALL_EXPR)
1731 builtexpr = create_expression_by_pieces (bprime,
1732 eprime,
1733 stmts);
1734 bsi_insert_on_edge (pred, stmts);
1735 avail[bprime->index] = builtexpr;
1736 insertions = true;
1739 /* If we didn't want a phi node, and we made insertions, we still have
1740 inserted new stuff, and thus return true. If we didn't want a phi node,
1741 and didn't make insertions, we haven't added anything new, so return
1742 false. */
1743 if (nophi && insertions)
1744 return true;
1745 else if (nophi && !insertions)
1746 return false;
1748 /* Now build a phi for the new variable. */
1749 temp = create_tmp_var (type, tmpname);
1750 add_referenced_tmp_var (temp);
1751 if (TREE_CODE (type) == COMPLEX_TYPE)
1752 DECL_COMPLEX_GIMPLE_REG_P (temp) = 1;
1753 temp = create_phi_node (temp, block);
1754 NECESSARY (temp) = 0;
1755 VEC_safe_push (tree, heap, inserted_exprs, temp);
1756 FOR_EACH_EDGE (pred, ei, block->preds)
1757 add_phi_arg (temp, avail[pred->src->index], pred);
1759 vn_add (PHI_RESULT (temp), val, NULL);
1761 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
1762 this insertion, since we test for the existence of this value in PHI_GEN
1763 before proceeding with the partial redundancy checks in insert_aux.
1765 The value may exist in AVAIL_OUT, in particular, it could be represented
1766 by the expression we are trying to eliminate, in which case we want the
1767 replacement to occur. If it's not existing in AVAIL_OUT, we want it
1768 inserted there.
1770 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
1771 this block, because if it did, it would have existed in our dominator's
1772 AVAIL_OUT, and would have been skipped due to the full redundancy check.
1775 bitmap_insert_into_set (PHI_GEN (block),
1776 PHI_RESULT (temp));
1777 bitmap_value_replace_in_set (AVAIL_OUT (block),
1778 PHI_RESULT (temp));
1779 bitmap_insert_into_set (NEW_SETS (block),
1780 PHI_RESULT (temp));
1782 if (dump_file && (dump_flags & TDF_DETAILS))
1784 fprintf (dump_file, "Created phi ");
1785 print_generic_expr (dump_file, temp, 0);
1786 fprintf (dump_file, " in block %d\n", block->index);
1788 pre_stats.phis++;
1789 return true;
1794 /* Perform insertion of partially redundant values.
1795 For BLOCK, do the following:
1796 1. Propagate the NEW_SETS of the dominator into the current block.
1797 If the block has multiple predecessors,
1798 2a. Iterate over the ANTIC expressions for the block to see if
1799 any of them are partially redundant.
1800 2b. If so, insert them into the necessary predecessors to make
1801 the expression fully redundant.
1802 2c. Insert a new PHI merging the values of the predecessors.
1803 2d. Insert the new PHI, and the new expressions, into the
1804 NEW_SETS set.
1805 3. Recursively call ourselves on the dominator children of BLOCK.
1809 static bool
1810 insert_aux (basic_block block)
1812 basic_block son;
1813 bool new_stuff = false;
1815 if (block)
1817 basic_block dom;
1818 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1819 if (dom)
1821 unsigned i;
1822 bitmap_iterator bi;
1823 bitmap_set_t newset = NEW_SETS (dom);
1824 if (newset)
1826 /* Note that we need to value_replace both NEW_SETS, and
1827 AVAIL_OUT. For both the case of NEW_SETS, the value may be
1828 represented by some non-simple expression here that we want
1829 to replace it with. */
1830 EXECUTE_IF_SET_IN_BITMAP (newset->expressions, 0, i, bi)
1832 bitmap_value_replace_in_set (NEW_SETS (block), ssa_name (i));
1833 bitmap_value_replace_in_set (AVAIL_OUT (block), ssa_name (i));
1836 if (!single_pred_p (block))
1838 value_set_node_t node;
1839 for (node = ANTIC_IN (block)->head;
1840 node;
1841 node = node->next)
1843 if (BINARY_CLASS_P (node->expr)
1844 || COMPARISON_CLASS_P (node->expr)
1845 || UNARY_CLASS_P (node->expr)
1846 || TREE_CODE (node->expr) == CALL_EXPR)
1848 tree *avail;
1849 tree val;
1850 bool by_some = false;
1851 bool cant_insert = false;
1852 bool all_same = true;
1853 tree first_s = NULL;
1854 edge pred;
1855 basic_block bprime;
1856 tree eprime = NULL_TREE;
1857 edge_iterator ei;
1859 val = get_value_handle (node->expr);
1860 if (bitmap_set_contains_value (PHI_GEN (block), val))
1861 continue;
1862 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
1864 if (dump_file && (dump_flags & TDF_DETAILS))
1865 fprintf (dump_file, "Found fully redundant value\n");
1866 continue;
1869 avail = XCNEWVEC (tree, last_basic_block);
1870 FOR_EACH_EDGE (pred, ei, block->preds)
1872 tree vprime;
1873 tree edoubleprime;
1875 /* This can happen in the very weird case
1876 that our fake infinite loop edges have caused a
1877 critical edge to appear. */
1878 if (EDGE_CRITICAL_P (pred))
1880 cant_insert = true;
1881 break;
1883 bprime = pred->src;
1884 eprime = phi_translate (node->expr,
1885 ANTIC_IN (block),
1886 bprime, block);
1888 /* eprime will generally only be NULL if the
1889 value of the expression, translated
1890 through the PHI for this predecessor, is
1891 undefined. If that is the case, we can't
1892 make the expression fully redundant,
1893 because its value is undefined along a
1894 predecessor path. We can thus break out
1895 early because it doesn't matter what the
1896 rest of the results are. */
1897 if (eprime == NULL)
1899 cant_insert = true;
1900 break;
1903 eprime = fully_constant_expression (eprime);
1904 vprime = get_value_handle (eprime);
1905 gcc_assert (vprime);
1906 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
1907 vprime);
1908 if (edoubleprime == NULL)
1910 avail[bprime->index] = eprime;
1911 all_same = false;
1913 else
1915 avail[bprime->index] = edoubleprime;
1916 by_some = true;
1917 if (first_s == NULL)
1918 first_s = edoubleprime;
1919 else if (!operand_equal_p (first_s, edoubleprime,
1921 all_same = false;
1924 /* If we can insert it, it's not the same value
1925 already existing along every predecessor, and
1926 it's defined by some predecessor, it is
1927 partially redundant. */
1928 if (!cant_insert && !all_same && by_some)
1930 if (insert_into_preds_of_block (block, node, avail,
1931 "prephitmp"))
1932 new_stuff = true;
1934 /* If all edges produce the same value and that value is
1935 an invariant, then the PHI has the same value on all
1936 edges. Note this. */
1937 else if (!cant_insert && all_same && eprime
1938 && is_gimple_min_invariant (eprime)
1939 && !is_gimple_min_invariant (val))
1941 value_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
1942 value_set_node_t node;
1943 for (node = exprset->head; node; node = node->next)
1945 if (TREE_CODE (node->expr) == SSA_NAME)
1947 vn_add (node->expr, eprime, NULL);
1948 pre_stats.constified++;
1952 free (avail);
1958 for (son = first_dom_son (CDI_DOMINATORS, block);
1959 son;
1960 son = next_dom_son (CDI_DOMINATORS, son))
1962 new_stuff |= insert_aux (son);
1965 return new_stuff;
1968 /* Perform insertion of partially redundant values. */
1970 static void
1971 insert (void)
1973 bool new_stuff = true;
1974 basic_block bb;
1975 int num_iterations = 0;
1977 FOR_ALL_BB (bb)
1978 NEW_SETS (bb) = bitmap_set_new ();
1980 while (new_stuff)
1982 num_iterations++;
1983 new_stuff = false;
1984 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1986 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1987 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1991 /* Return true if VAR is an SSA variable with no defining statement in
1992 this procedure, *AND* isn't a live-on-entry parameter. */
1994 static bool
1995 is_undefined_value (tree expr)
1997 return (TREE_CODE (expr) == SSA_NAME
1998 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
1999 /* PARM_DECLs and hard registers are always defined. */
2000 && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL);
2004 /* Given an SSA variable VAR and an expression EXPR, compute the value
2005 number for EXPR and create a value handle (VAL) for it. If VAR and
2006 EXPR are not the same, associate VAL with VAR. Finally, add VAR to
2007 S1 and its value handle to S2.
2009 VUSES represent the virtual use operands associated with EXPR (if
2010 any). They are used when computing the hash value for EXPR. */
2012 static inline void
2013 add_to_sets (tree var, tree expr, tree stmt, bitmap_set_t s1,
2014 bitmap_set_t s2)
2016 tree val = vn_lookup_or_add (expr, stmt);
2018 /* VAR and EXPR may be the same when processing statements for which
2019 we are not computing value numbers (e.g., non-assignments, or
2020 statements that make aliased stores). In those cases, we are
2021 only interested in making VAR available as its own value. */
2022 if (var != expr)
2023 vn_add (var, val, NULL_TREE);
2025 if (s1)
2026 bitmap_insert_into_set (s1, var);
2027 bitmap_value_insert_into_set (s2, var);
2031 /* Given a unary or binary expression EXPR, create and return a new
2032 expression with the same structure as EXPR but with its operands
2033 replaced with the value handles of each of the operands of EXPR.
2035 VUSES represent the virtual use operands associated with EXPR (if
2036 any). They are used when computing the hash value for EXPR.
2037 Insert EXPR's operands into the EXP_GEN set for BLOCK. */
2039 static inline tree
2040 create_value_expr_from (tree expr, basic_block block, tree stmt)
2042 int i;
2043 enum tree_code code = TREE_CODE (expr);
2044 tree vexpr;
2045 alloc_pool pool;
2047 gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
2048 || TREE_CODE_CLASS (code) == tcc_binary
2049 || TREE_CODE_CLASS (code) == tcc_comparison
2050 || TREE_CODE_CLASS (code) == tcc_reference
2051 || TREE_CODE_CLASS (code) == tcc_expression
2052 || TREE_CODE_CLASS (code) == tcc_exceptional);
2054 if (TREE_CODE_CLASS (code) == tcc_unary)
2055 pool = unary_node_pool;
2056 else if (TREE_CODE_CLASS (code) == tcc_reference)
2057 pool = reference_node_pool;
2058 else if (TREE_CODE_CLASS (code) == tcc_binary)
2059 pool = binary_node_pool;
2060 else if (TREE_CODE_CLASS (code) == tcc_comparison)
2061 pool = comparison_node_pool;
2062 else if (TREE_CODE_CLASS (code) == tcc_exceptional)
2064 gcc_assert (code == TREE_LIST);
2065 pool = list_node_pool;
2067 else
2069 gcc_assert (code == CALL_EXPR);
2070 pool = expression_node_pool;
2073 vexpr = (tree) pool_alloc (pool);
2074 memcpy (vexpr, expr, tree_size (expr));
2076 /* This case is only for TREE_LIST's that appear as part of
2077 CALL_EXPR's. Anything else is a bug, but we can't easily verify
2078 this, hence this comment. TREE_LIST is not handled by the
2079 general case below is because they don't have a fixed length, or
2080 operands, so you can't access purpose/value/chain through
2081 TREE_OPERAND macros. */
2083 if (code == TREE_LIST)
2085 tree op = NULL_TREE;
2086 tree temp = NULL_TREE;
2087 if (TREE_CHAIN (vexpr))
2088 temp = create_value_expr_from (TREE_CHAIN (vexpr), block, stmt);
2089 TREE_CHAIN (vexpr) = temp ? temp : TREE_CHAIN (vexpr);
2092 /* Recursively value-numberize reference ops. */
2093 if (REFERENCE_CLASS_P (TREE_VALUE (vexpr)))
2095 tree tempop;
2096 op = TREE_VALUE (vexpr);
2097 tempop = create_value_expr_from (op, block, stmt);
2098 op = tempop ? tempop : op;
2100 TREE_VALUE (vexpr) = vn_lookup_or_add (op, stmt);
2102 else
2104 op = TREE_VALUE (vexpr);
2105 TREE_VALUE (vexpr) = vn_lookup_or_add (TREE_VALUE (vexpr), NULL);
2107 /* This is the equivalent of inserting op into EXP_GEN like we
2108 do below */
2109 if (!is_undefined_value (op))
2110 value_insert_into_set (EXP_GEN (block), op);
2112 return vexpr;
2115 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
2117 tree val, op;
2119 op = TREE_OPERAND (expr, i);
2120 if (op == NULL_TREE)
2121 continue;
2123 /* If OP is a constant that has overflowed, do not value number
2124 this expression. */
2125 if (CONSTANT_CLASS_P (op)
2126 && TREE_OVERFLOW (op))
2128 pool_free (pool, vexpr);
2129 return NULL;
2132 /* Recursively value-numberize reference ops and tree lists. */
2133 if (REFERENCE_CLASS_P (op))
2135 tree tempop = create_value_expr_from (op, block, stmt);
2136 op = tempop ? tempop : op;
2137 val = vn_lookup_or_add (op, stmt);
2139 else if (TREE_CODE (op) == TREE_LIST)
2141 tree tempop;
2143 gcc_assert (TREE_CODE (expr) == CALL_EXPR);
2144 tempop = create_value_expr_from (op, block, stmt);
2146 op = tempop ? tempop : op;
2147 vn_lookup_or_add (op, NULL);
2148 /* Unlike everywhere else, we do *not* want to replace the
2149 TREE_LIST itself with a value number, because support
2150 functions we call will blow up. */
2151 val = op;
2153 else
2154 /* Create a value handle for OP and add it to VEXPR. */
2155 val = vn_lookup_or_add (op, NULL);
2157 if (!is_undefined_value (op) && TREE_CODE (op) != TREE_LIST)
2158 value_insert_into_set (EXP_GEN (block), op);
2160 if (TREE_CODE (val) == VALUE_HANDLE)
2161 TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
2163 TREE_OPERAND (vexpr, i) = val;
2166 return vexpr;
2170 /* Return true if we can value number a call. This is true if we have
2171 a pure or constant call. */
2172 static bool
2173 can_value_number_call (tree stmt)
2175 tree call = get_call_expr_in (stmt);
2177 /* This is a temporary restriction until we translate vuses through
2178 phi nodes. This is only needed for PRE, of course. */
2179 if (!in_fre && !ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
2180 return false;
2181 if (call_expr_flags (call) & (ECF_PURE | ECF_CONST))
2182 return true;
2183 return false;
2186 /* Insert extra phis to merge values that are fully available from
2187 preds of BLOCK, but have no dominating representative coming from
2188 block DOM. */
2190 static void
2191 insert_extra_phis (basic_block block, basic_block dom)
2194 if (!single_pred_p (block))
2196 edge e;
2197 edge_iterator ei;
2198 bool first = true;
2199 bitmap_set_t tempset = bitmap_set_new ();
2201 FOR_EACH_EDGE (e, ei, block->preds)
2203 if (first)
2205 bitmap_set_copy (tempset, AVAIL_OUT (e->src));
2206 first = false;
2208 else
2209 bitmap_set_and (tempset, AVAIL_OUT (e->src));
2212 if (dom)
2213 bitmap_set_and_compl (tempset, AVAIL_OUT (dom));
2215 if (!bitmap_set_empty_p (tempset))
2217 unsigned int i;
2218 bitmap_iterator bi;
2220 EXECUTE_IF_SET_IN_BITMAP (tempset->expressions, 0, i, bi)
2222 tree name = ssa_name (i);
2223 tree val = get_value_handle (name);
2224 tree temp = create_tmp_var (TREE_TYPE (name), "mergephitmp");
2226 if (dump_file && (dump_flags & TDF_DETAILS))
2228 fprintf (dump_file, "Creating phi ");
2229 print_generic_expr (dump_file, temp, 0);
2230 fprintf (dump_file, " to merge available but not dominating values ");
2233 add_referenced_tmp_var (temp);
2234 temp = create_phi_node (temp, block);
2235 NECESSARY (temp) = 0;
2236 VEC_safe_push (tree, heap, inserted_exprs, temp);
2238 FOR_EACH_EDGE (e, ei, block->preds)
2240 tree leader = bitmap_find_leader (AVAIL_OUT (e->src), val);
2242 gcc_assert (leader);
2243 add_phi_arg (temp, leader, e);
2245 if (dump_file && (dump_flags & TDF_DETAILS))
2247 print_generic_expr (dump_file, leader, 0);
2248 fprintf (dump_file, " in block %d,", e->src->index);
2252 vn_add (PHI_RESULT (temp), val, NULL);
2254 if (dump_file && (dump_flags & TDF_DETAILS))
2255 fprintf (dump_file, "\n");
2261 /* Given a statement STMT and its right hand side which is a load, try
2262 to look for the expression stored in the location for the load, and
2263 return true if a useful equivalence was recorded for LHS. */
2265 static bool
2266 try_look_through_load (tree lhs, tree mem_ref, tree stmt, basic_block block)
2268 tree store_stmt = NULL;
2269 tree rhs;
2270 ssa_op_iter i;
2271 tree vuse;
2273 FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, i, SSA_OP_VIRTUAL_USES)
2275 tree def_stmt;
2277 gcc_assert (TREE_CODE (vuse) == SSA_NAME);
2278 def_stmt = SSA_NAME_DEF_STMT (vuse);
2280 /* If there is no useful statement for this VUSE, we'll not find a
2281 useful expression to return either. Likewise, if there is a
2282 statement but it is not a simple assignment or it has virtual
2283 uses, we can stop right here. Note that this means we do
2284 not look through PHI nodes, which is intentional. */
2285 if (!def_stmt
2286 || TREE_CODE (def_stmt) != MODIFY_EXPR
2287 || !ZERO_SSA_OPERANDS (def_stmt, SSA_OP_VIRTUAL_USES))
2288 return false;
2290 /* If this is not the same statement as one we have looked at for
2291 another VUSE of STMT already, we have two statements producing
2292 something that reaches our STMT. */
2293 if (store_stmt && store_stmt != def_stmt)
2294 return false;
2295 else
2297 /* Is this a store to the exact same location as the one we are
2298 loading from in STMT? */
2299 if (!operand_equal_p (TREE_OPERAND (def_stmt, 0), mem_ref, 0))
2300 return false;
2302 /* Otherwise remember this statement and see if all other VUSEs
2303 come from the same statement. */
2304 store_stmt = def_stmt;
2308 /* Alright then, we have visited all VUSEs of STMT and we've determined
2309 that all of them come from the same statement STORE_STMT. See if there
2310 is a useful expression we can deduce from STORE_STMT. */
2311 rhs = TREE_OPERAND (store_stmt, 1);
2312 if ((TREE_CODE (rhs) == SSA_NAME
2313 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2314 || is_gimple_min_invariant (rhs)
2315 || TREE_CODE (rhs) == ADDR_EXPR
2316 || TREE_INVARIANT (rhs))
2319 /* Yay! Compute a value number for the RHS of the statement and
2320 add its value to the AVAIL_OUT set for the block. Add the LHS
2321 to TMP_GEN. */
2322 add_to_sets (lhs, rhs, store_stmt, TMP_GEN (block), AVAIL_OUT (block));
2323 if (TREE_CODE (rhs) == SSA_NAME
2324 && !is_undefined_value (rhs))
2325 value_insert_into_set (EXP_GEN (block), rhs);
2326 return true;
2329 return false;
2332 /* Compute the AVAIL set for all basic blocks.
2334 This function performs value numbering of the statements in each basic
2335 block. The AVAIL sets are built from information we glean while doing
2336 this value numbering, since the AVAIL sets contain only one entry per
2337 value.
2339 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
2340 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
2342 static void
2343 compute_avail (void)
2345 basic_block block, son;
2346 basic_block *worklist;
2347 size_t sp = 0;
2348 tree param;
2350 /* For arguments with default definitions, we pretend they are
2351 defined in the entry block. */
2352 for (param = DECL_ARGUMENTS (current_function_decl);
2353 param;
2354 param = TREE_CHAIN (param))
2356 if (default_def (param) != NULL)
2358 tree def = default_def (param);
2359 vn_lookup_or_add (def, NULL);
2360 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
2361 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
2365 /* Allocate the worklist. */
2366 worklist = XNEWVEC (basic_block, n_basic_blocks);
2368 /* Seed the algorithm by putting the dominator children of the entry
2369 block on the worklist. */
2370 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR);
2371 son;
2372 son = next_dom_son (CDI_DOMINATORS, son))
2373 worklist[sp++] = son;
2375 /* Loop until the worklist is empty. */
2376 while (sp)
2378 block_stmt_iterator bsi;
2379 tree stmt, phi;
2380 basic_block dom;
2382 /* Pick a block from the worklist. */
2383 block = worklist[--sp];
2385 /* Initially, the set of available values in BLOCK is that of
2386 its immediate dominator. */
2387 dom = get_immediate_dominator (CDI_DOMINATORS, block);
2388 if (dom)
2389 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
2391 if (!in_fre)
2392 insert_extra_phis (block, dom);
2394 /* Generate values for PHI nodes. */
2395 for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
2396 /* We have no need for virtual phis, as they don't represent
2397 actual computations. */
2398 if (is_gimple_reg (PHI_RESULT (phi)))
2399 add_to_sets (PHI_RESULT (phi), PHI_RESULT (phi), NULL,
2400 PHI_GEN (block), AVAIL_OUT (block));
2402 /* Now compute value numbers and populate value sets with all
2403 the expressions computed in BLOCK. */
2404 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
2406 stmt_ann_t ann;
2407 ssa_op_iter iter;
2408 tree op;
2410 stmt = bsi_stmt (bsi);
2411 ann = stmt_ann (stmt);
2413 /* We are only interested in assignments of the form
2414 X_i = EXPR, where EXPR represents an "interesting"
2415 computation, it has no volatile operands and X_i
2416 doesn't flow through an abnormal edge. */
2417 if (TREE_CODE (stmt) == MODIFY_EXPR
2418 && !ann->has_volatile_ops
2419 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
2420 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (stmt, 0)))
2422 tree lhs = TREE_OPERAND (stmt, 0);
2423 tree rhs = TREE_OPERAND (stmt, 1);
2425 /* Try to look through loads. */
2426 if (TREE_CODE (lhs) == SSA_NAME
2427 && !ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_USES)
2428 && try_look_through_load (lhs, rhs, stmt, block))
2429 continue;
2431 STRIP_USELESS_TYPE_CONVERSION (rhs);
2432 if (UNARY_CLASS_P (rhs)
2433 || BINARY_CLASS_P (rhs)
2434 || COMPARISON_CLASS_P (rhs)
2435 || REFERENCE_CLASS_P (rhs)
2436 || (TREE_CODE (rhs) == CALL_EXPR
2437 && can_value_number_call (stmt)))
2439 /* For binary, unary, and reference expressions,
2440 create a duplicate expression with the operands
2441 replaced with the value handles of the original
2442 RHS. */
2443 tree newt = create_value_expr_from (rhs, block, stmt);
2444 if (newt)
2446 add_to_sets (lhs, newt, stmt, TMP_GEN (block),
2447 AVAIL_OUT (block));
2448 value_insert_into_set (EXP_GEN (block), newt);
2449 continue;
2452 else if ((TREE_CODE (rhs) == SSA_NAME
2453 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2454 || is_gimple_min_invariant (rhs)
2455 || TREE_CODE (rhs) == ADDR_EXPR
2456 || TREE_INVARIANT (rhs)
2457 || DECL_P (rhs))
2459 /* Compute a value number for the RHS of the statement
2460 and add its value to the AVAIL_OUT set for the block.
2461 Add the LHS to TMP_GEN. */
2462 add_to_sets (lhs, rhs, stmt, TMP_GEN (block),
2463 AVAIL_OUT (block));
2465 if (TREE_CODE (rhs) == SSA_NAME
2466 && !is_undefined_value (rhs))
2467 value_insert_into_set (EXP_GEN (block), rhs);
2468 continue;
2472 /* For any other statement that we don't recognize, simply
2473 make the names generated by the statement available in
2474 AVAIL_OUT and TMP_GEN. */
2475 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
2476 add_to_sets (op, op, NULL, TMP_GEN (block), AVAIL_OUT (block));
2478 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
2479 add_to_sets (op, op, NULL, NULL , AVAIL_OUT (block));
2482 /* Put the dominator children of BLOCK on the worklist of blocks
2483 to compute available sets for. */
2484 for (son = first_dom_son (CDI_DOMINATORS, block);
2485 son;
2486 son = next_dom_son (CDI_DOMINATORS, son))
2487 worklist[sp++] = son;
2490 free (worklist);
2494 /* Eliminate fully redundant computations. */
2496 static void
2497 eliminate (void)
2499 basic_block b;
2501 FOR_EACH_BB (b)
2503 block_stmt_iterator i;
2505 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
2507 tree stmt = bsi_stmt (i);
2509 /* Lookup the RHS of the expression, see if we have an
2510 available computation for it. If so, replace the RHS with
2511 the available computation. */
2512 if (TREE_CODE (stmt) == MODIFY_EXPR
2513 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
2514 && TREE_CODE (TREE_OPERAND (stmt ,1)) != SSA_NAME
2515 && !is_gimple_min_invariant (TREE_OPERAND (stmt, 1))
2516 && !stmt_ann (stmt)->has_volatile_ops)
2518 tree lhs = TREE_OPERAND (stmt, 0);
2519 tree *rhs_p = &TREE_OPERAND (stmt, 1);
2520 tree sprime;
2522 sprime = bitmap_find_leader (AVAIL_OUT (b),
2523 vn_lookup (lhs, NULL));
2524 if (sprime
2525 && sprime != lhs
2526 && (TREE_CODE (*rhs_p) != SSA_NAME
2527 || may_propagate_copy (*rhs_p, sprime)))
2529 gcc_assert (sprime != *rhs_p);
2531 if (dump_file && (dump_flags & TDF_DETAILS))
2533 fprintf (dump_file, "Replaced ");
2534 print_generic_expr (dump_file, *rhs_p, 0);
2535 fprintf (dump_file, " with ");
2536 print_generic_expr (dump_file, sprime, 0);
2537 fprintf (dump_file, " in ");
2538 print_generic_stmt (dump_file, stmt, 0);
2541 if (TREE_CODE (sprime) == SSA_NAME)
2542 NECESSARY (SSA_NAME_DEF_STMT (sprime)) = 1;
2543 /* We need to make sure the new and old types actually match,
2544 which may require adding a simple cast, which fold_convert
2545 will do for us. */
2546 if (TREE_CODE (*rhs_p) != SSA_NAME
2547 && !tree_ssa_useless_type_conversion_1 (TREE_TYPE (*rhs_p),
2548 TREE_TYPE (sprime)))
2549 sprime = fold_convert (TREE_TYPE (*rhs_p), sprime);
2551 pre_stats.eliminations++;
2552 propagate_tree_value (rhs_p, sprime);
2553 update_stmt (stmt);
2555 /* If we removed EH side effects from the statement, clean
2556 its EH information. */
2557 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
2559 bitmap_set_bit (need_eh_cleanup,
2560 bb_for_stmt (stmt)->index);
2561 if (dump_file && (dump_flags & TDF_DETAILS))
2562 fprintf (dump_file, " Removed EH side effects.\n");
2570 /* Borrow a bit of tree-ssa-dce.c for the moment.
2571 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
2572 this may be a bit faster, and we may want critical edges kept split. */
2574 /* If OP's defining statement has not already been determined to be necessary,
2575 mark that statement necessary. Return the stmt, if it is newly
2576 necessary. */
2578 static inline tree
2579 mark_operand_necessary (tree op)
2581 tree stmt;
2583 gcc_assert (op);
2585 stmt = SSA_NAME_DEF_STMT (op);
2586 gcc_assert (stmt);
2588 if (NECESSARY (stmt)
2589 || IS_EMPTY_STMT (stmt))
2590 return NULL;
2592 NECESSARY (stmt) = 1;
2593 return stmt;
2596 /* Because we don't follow exactly the standard PRE algorithm, and decide not
2597 to insert PHI nodes sometimes, and because value numbering of casts isn't
2598 perfect, we sometimes end up inserting dead code. This simple DCE-like
2599 pass removes any insertions we made that weren't actually used. */
2601 static void
2602 remove_dead_inserted_code (void)
2604 VEC(tree,heap) *worklist = NULL;
2605 int i;
2606 tree t;
2608 worklist = VEC_alloc (tree, heap, VEC_length (tree, inserted_exprs));
2609 for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
2611 if (NECESSARY (t))
2612 VEC_quick_push (tree, worklist, t);
2614 while (VEC_length (tree, worklist) > 0)
2616 t = VEC_pop (tree, worklist);
2617 if (TREE_CODE (t) == PHI_NODE)
2619 /* PHI nodes are somewhat special in that each PHI alternative has
2620 data and control dependencies. All the statements feeding the
2621 PHI node's arguments are always necessary. In aggressive mode,
2622 we also consider the control dependent edges leading to the
2623 predecessor block associated with each PHI alternative as
2624 necessary. */
2625 int k;
2627 VEC_reserve (tree, heap, worklist, PHI_NUM_ARGS (t));
2628 for (k = 0; k < PHI_NUM_ARGS (t); k++)
2630 tree arg = PHI_ARG_DEF (t, k);
2631 if (TREE_CODE (arg) == SSA_NAME)
2633 arg = mark_operand_necessary (arg);
2634 if (arg)
2635 VEC_quick_push (tree, worklist, arg);
2639 else
2641 /* Propagate through the operands. Examine all the USE, VUSE and
2642 V_MAY_DEF operands in this statement. Mark all the statements
2643 which feed this statement's uses as necessary. */
2644 ssa_op_iter iter;
2645 tree use;
2647 /* The operands of V_MAY_DEF expressions are also needed as they
2648 represent potential definitions that may reach this
2649 statement (V_MAY_DEF operands allow us to follow def-def
2650 links). */
2652 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
2654 tree n = mark_operand_necessary (use);
2655 if (n)
2656 VEC_safe_push (tree, heap, worklist, n);
2660 for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
2662 if (!NECESSARY (t))
2664 block_stmt_iterator bsi;
2665 if (dump_file && (dump_flags & TDF_DETAILS))
2667 fprintf (dump_file, "Removing unnecessary insertion:");
2668 print_generic_stmt (dump_file, t, 0);
2670 if (TREE_CODE (t) == PHI_NODE)
2672 remove_phi_node (t, NULL);
2674 else
2676 bsi = bsi_for_stmt (t);
2677 bsi_remove (&bsi);
2681 VEC_free (tree, heap, worklist);
2683 /* Initialize data structures used by PRE. */
2685 static void
2686 init_pre (bool do_fre)
2688 basic_block bb;
2690 in_fre = do_fre;
2692 inserted_exprs = NULL;
2693 vn_init ();
2694 if (!do_fre)
2695 current_loops = loop_optimizer_init (dump_file);
2696 connect_infinite_loops_to_exit ();
2697 memset (&pre_stats, 0, sizeof (pre_stats));
2699 /* If block 0 has more than one predecessor, it means that its PHI
2700 nodes will have arguments coming from block -1. This creates
2701 problems for several places in PRE that keep local arrays indexed
2702 by block number. To prevent this, we split the edge coming from
2703 ENTRY_BLOCK_PTR (FIXME, if ENTRY_BLOCK_PTR had an index number
2704 different than -1 we wouldn't have to hack this. tree-ssa-dce.c
2705 needs a similar change). */
2706 if (!single_pred_p (single_succ (ENTRY_BLOCK_PTR)))
2707 if (!(single_succ_edge (ENTRY_BLOCK_PTR)->flags & EDGE_ABNORMAL))
2708 split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
2710 FOR_ALL_BB (bb)
2711 bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
2713 bitmap_obstack_initialize (&grand_bitmap_obstack);
2714 phi_translate_table = htab_create (511, expr_pred_trans_hash,
2715 expr_pred_trans_eq, free);
2716 value_set_pool = create_alloc_pool ("Value sets",
2717 sizeof (struct value_set), 30);
2718 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
2719 sizeof (struct bitmap_set), 30);
2720 value_set_node_pool = create_alloc_pool ("Value set nodes",
2721 sizeof (struct value_set_node), 30);
2722 calculate_dominance_info (CDI_POST_DOMINATORS);
2723 calculate_dominance_info (CDI_DOMINATORS);
2724 binary_node_pool = create_alloc_pool ("Binary tree nodes",
2725 tree_code_size (PLUS_EXPR), 30);
2726 unary_node_pool = create_alloc_pool ("Unary tree nodes",
2727 tree_code_size (NEGATE_EXPR), 30);
2728 reference_node_pool = create_alloc_pool ("Reference tree nodes",
2729 tree_code_size (ARRAY_REF), 30);
2730 expression_node_pool = create_alloc_pool ("Expression tree nodes",
2731 tree_code_size (CALL_EXPR), 30);
2732 list_node_pool = create_alloc_pool ("List tree nodes",
2733 tree_code_size (TREE_LIST), 30);
2734 comparison_node_pool = create_alloc_pool ("Comparison tree nodes",
2735 tree_code_size (EQ_EXPR), 30);
2736 FOR_ALL_BB (bb)
2738 EXP_GEN (bb) = set_new (true);
2739 PHI_GEN (bb) = bitmap_set_new ();
2740 TMP_GEN (bb) = bitmap_set_new ();
2741 AVAIL_OUT (bb) = bitmap_set_new ();
2744 need_eh_cleanup = BITMAP_ALLOC (NULL);
2748 /* Deallocate data structures used by PRE. */
2750 static void
2751 fini_pre (bool do_fre)
2753 basic_block bb;
2754 unsigned int i;
2756 VEC_free (tree, heap, inserted_exprs);
2757 bitmap_obstack_release (&grand_bitmap_obstack);
2758 free_alloc_pool (value_set_pool);
2759 free_alloc_pool (bitmap_set_pool);
2760 free_alloc_pool (value_set_node_pool);
2761 free_alloc_pool (binary_node_pool);
2762 free_alloc_pool (reference_node_pool);
2763 free_alloc_pool (unary_node_pool);
2764 free_alloc_pool (list_node_pool);
2765 free_alloc_pool (expression_node_pool);
2766 free_alloc_pool (comparison_node_pool);
2767 htab_delete (phi_translate_table);
2768 remove_fake_exit_edges ();
2770 FOR_ALL_BB (bb)
2772 free (bb->aux);
2773 bb->aux = NULL;
2776 free_dominance_info (CDI_POST_DOMINATORS);
2777 vn_delete ();
2779 if (!bitmap_empty_p (need_eh_cleanup))
2781 tree_purge_all_dead_eh_edges (need_eh_cleanup);
2782 cleanup_tree_cfg ();
2785 BITMAP_FREE (need_eh_cleanup);
2787 /* Wipe out pointers to VALUE_HANDLEs. In the not terribly distant
2788 future we will want them to be persistent though. */
2789 for (i = 0; i < num_ssa_names; i++)
2791 tree name = ssa_name (i);
2793 if (!name)
2794 continue;
2796 if (SSA_NAME_VALUE (name)
2797 && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
2798 SSA_NAME_VALUE (name) = NULL;
2800 if (!do_fre && current_loops)
2802 loop_optimizer_finalize (current_loops, dump_file);
2803 current_loops = NULL;
2808 /* Main entry point to the SSA-PRE pass. DO_FRE is true if the caller
2809 only wants to do full redundancy elimination. */
2811 static void
2812 execute_pre (bool do_fre)
2814 init_pre (do_fre);
2816 /* Collect and value number expressions computed in each basic block. */
2817 compute_avail ();
2819 if (dump_file && (dump_flags & TDF_DETAILS))
2821 basic_block bb;
2823 FOR_ALL_BB (bb)
2825 print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
2826 bitmap_print_value_set (dump_file, TMP_GEN (bb), "tmp_gen",
2827 bb->index);
2828 bitmap_print_value_set (dump_file, AVAIL_OUT (bb), "avail_out",
2829 bb->index);
2833 /* Insert can get quite slow on an incredibly large number of basic
2834 blocks due to some quadratic behavior. Until this behavior is
2835 fixed, don't run it when he have an incredibly large number of
2836 bb's. If we aren't going to run insert, there is no point in
2837 computing ANTIC, either, even though it's plenty fast. */
2838 if (!do_fre && n_basic_blocks < 4000)
2840 compute_antic ();
2841 insert ();
2844 /* Remove all the redundant expressions. */
2845 eliminate ();
2848 if (dump_file && (dump_flags & TDF_STATS))
2850 fprintf (dump_file, "Insertions: %d\n", pre_stats.insertions);
2851 fprintf (dump_file, "New PHIs: %d\n", pre_stats.phis);
2852 fprintf (dump_file, "Eliminated: %d\n", pre_stats.eliminations);
2853 fprintf (dump_file, "Constified: %d\n", pre_stats.constified);
2856 bsi_commit_edge_inserts ();
2857 if (!do_fre)
2858 remove_dead_inserted_code ();
2859 fini_pre (do_fre);
2864 /* Gate and execute functions for PRE. */
2866 static void
2867 do_pre (void)
2869 execute_pre (false);
2872 static bool
2873 gate_pre (void)
2875 return flag_tree_pre != 0;
2878 struct tree_opt_pass pass_pre =
2880 "pre", /* name */
2881 gate_pre, /* gate */
2882 do_pre, /* execute */
2883 NULL, /* sub */
2884 NULL, /* next */
2885 0, /* static_pass_number */
2886 TV_TREE_PRE, /* tv_id */
2887 PROP_no_crit_edges | PROP_cfg
2888 | PROP_ssa | PROP_alias, /* properties_required */
2889 0, /* properties_provided */
2890 0, /* properties_destroyed */
2891 0, /* todo_flags_start */
2892 TODO_update_ssa | TODO_dump_func | TODO_ggc_collect
2893 | TODO_verify_ssa, /* todo_flags_finish */
2894 0 /* letter */
2898 /* Gate and execute functions for FRE. */
2900 static void
2901 execute_fre (void)
2903 execute_pre (true);
2906 static bool
2907 gate_fre (void)
2909 return flag_tree_fre != 0;
2912 struct tree_opt_pass pass_fre =
2914 "fre", /* name */
2915 gate_fre, /* gate */
2916 execute_fre, /* execute */
2917 NULL, /* sub */
2918 NULL, /* next */
2919 0, /* static_pass_number */
2920 TV_TREE_FRE, /* tv_id */
2921 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2922 0, /* properties_provided */
2923 0, /* properties_destroyed */
2924 0, /* todo_flags_start */
2925 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
2926 0 /* letter */