cfganal.c (flow_reverse_top_sort_order_compute): Renamed to post_order_compute and...
[official-gcc.git] / gcc / tree-ssa-pre.c
blob08964004d0aa8fe37c005014817b55dd56bd20fb
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 DEF_VEC_P (basic_block);
1338 DEF_VEC_ALLOC_P (basic_block, heap);
1339 static sbitmap has_abnormal_preds;
1341 /* Compute the ANTIC set for BLOCK.
1343 If succs(BLOCK) > 1 then
1344 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
1345 else if succs(BLOCK) == 1 then
1346 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
1348 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
1350 XXX: It would be nice to either write a set_clear, and use it for
1351 ANTIC_OUT, or to mark the antic_out set as deleted at the end
1352 of this routine, so that the pool can hand the same memory back out
1353 again for the next ANTIC_OUT. */
1355 static bool
1356 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
1358 basic_block son;
1359 bool changed = false;
1360 value_set_t S, old, ANTIC_OUT;
1361 value_set_node_t node;
1363 ANTIC_OUT = S = NULL;
1365 /* If any edges from predecessors are abnormal, antic_in is empty,
1366 so do nothing. */
1367 if (block_has_abnormal_pred_edge)
1368 goto maybe_dump_sets;
1370 old = set_new (false);
1371 set_copy (old, ANTIC_IN (block));
1372 ANTIC_OUT = set_new (true);
1374 /* If the block has no successors, ANTIC_OUT is empty. */
1375 if (EDGE_COUNT (block->succs) == 0)
1377 /* If we have one successor, we could have some phi nodes to
1378 translate through. */
1379 else if (single_succ_p (block))
1381 phi_translate_set (ANTIC_OUT, ANTIC_IN (single_succ (block)),
1382 block, single_succ (block));
1384 /* If we have multiple successors, we take the intersection of all of
1385 them. */
1386 else
1388 VEC(basic_block, heap) * worklist;
1389 edge e;
1390 size_t i;
1391 basic_block bprime, first;
1392 edge_iterator ei;
1394 worklist = VEC_alloc (basic_block, heap, EDGE_COUNT (block->succs));
1395 FOR_EACH_EDGE (e, ei, block->succs)
1396 VEC_quick_push (basic_block, worklist, e->dest);
1397 first = VEC_index (basic_block, worklist, 0);
1398 set_copy (ANTIC_OUT, ANTIC_IN (first));
1400 for (i = 1; VEC_iterate (basic_block, worklist, i, bprime); i++)
1402 node = ANTIC_OUT->head;
1403 while (node)
1405 tree val;
1406 value_set_node_t next = node->next;
1407 val = get_value_handle (node->expr);
1408 if (!set_contains_value (ANTIC_IN (bprime), val))
1409 set_remove (ANTIC_OUT, node->expr);
1410 node = next;
1413 VEC_free (basic_block, heap, worklist);
1416 /* Generate ANTIC_OUT - TMP_GEN. */
1417 S = bitmap_set_subtract_from_value_set (ANTIC_OUT, TMP_GEN (block), false);
1419 /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1420 ANTIC_IN (block) = bitmap_set_subtract_from_value_set (EXP_GEN (block),
1421 TMP_GEN (block),
1422 true);
1424 /* Then union in the ANTIC_OUT - TMP_GEN values,
1425 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
1426 for (node = S->head; node; node = node->next)
1427 value_insert_into_set (ANTIC_IN (block), node->expr);
1429 clean (ANTIC_IN (block));
1430 if (!set_equal (old, ANTIC_IN (block)))
1431 changed = true;
1433 maybe_dump_sets:
1434 if (dump_file && (dump_flags & TDF_DETAILS))
1436 if (ANTIC_OUT)
1437 print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1438 print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1439 if (S)
1440 print_value_set (dump_file, S, "S", block->index);
1443 for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1444 son;
1445 son = next_dom_son (CDI_POST_DOMINATORS, son))
1447 changed |= compute_antic_aux (son,
1448 TEST_BIT (has_abnormal_preds, son->index));
1450 return changed;
1453 /* Compute ANTIC sets. */
1455 static void
1456 compute_antic (void)
1458 bool changed = true;
1459 int num_iterations = 0;
1460 basic_block block;
1462 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
1463 We pre-build the map of blocks with incoming abnormal edges here. */
1464 has_abnormal_preds = sbitmap_alloc (last_basic_block);
1465 sbitmap_zero (has_abnormal_preds);
1466 FOR_EACH_BB (block)
1468 edge_iterator ei;
1469 edge e;
1471 FOR_EACH_EDGE (e, ei, block->preds)
1472 if (e->flags & EDGE_ABNORMAL)
1474 SET_BIT (has_abnormal_preds, block->index);
1475 break;
1478 /* While we are here, give empty ANTIC_IN sets to each block. */
1479 ANTIC_IN (block) = set_new (true);
1481 /* At the exit block we anticipate nothing. */
1482 ANTIC_IN (EXIT_BLOCK_PTR) = set_new (true);
1484 while (changed)
1486 num_iterations++;
1487 changed = false;
1488 changed = compute_antic_aux (EXIT_BLOCK_PTR, false);
1491 sbitmap_free (has_abnormal_preds);
1493 if (dump_file && (dump_flags & TDF_STATS))
1494 fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1497 static VEC(tree,heap) *inserted_exprs;
1498 /* Find a leader for an expression, or generate one using
1499 create_expression_by_pieces if it's ANTIC but
1500 complex.
1501 BLOCK is the basic_block we are looking for leaders in.
1502 EXPR is the expression to find a leader or generate for.
1503 STMTS is the statement list to put the inserted expressions on.
1504 Returns the SSA_NAME of the LHS of the generated expression or the
1505 leader. */
1507 static tree
1508 find_or_generate_expression (basic_block block, tree expr, tree stmts)
1510 tree genop = bitmap_find_leader (AVAIL_OUT (block), expr);
1512 /* If it's still NULL, it must be a complex expression, so generate
1513 it recursively. */
1514 if (genop == NULL)
1516 genop = VALUE_HANDLE_EXPR_SET (expr)->head->expr;
1517 gcc_assert (UNARY_CLASS_P (genop)
1518 || BINARY_CLASS_P (genop)
1519 || COMPARISON_CLASS_P (genop)
1520 || REFERENCE_CLASS_P (genop)
1521 || TREE_CODE (genop) == CALL_EXPR);
1522 genop = create_expression_by_pieces (block, genop, stmts);
1524 return genop;
1527 #define NECESSARY(stmt) stmt->common.asm_written_flag
1528 /* Create an expression in pieces, so that we can handle very complex
1529 expressions that may be ANTIC, but not necessary GIMPLE.
1530 BLOCK is the basic block the expression will be inserted into,
1531 EXPR is the expression to insert (in value form)
1532 STMTS is a statement list to append the necessary insertions into.
1534 This function will die if we hit some value that shouldn't be
1535 ANTIC but is (IE there is no leader for it, or its components).
1536 This function may also generate expressions that are themselves
1537 partially or fully redundant. Those that are will be either made
1538 fully redundant during the next iteration of insert (for partially
1539 redundant ones), or eliminated by eliminate (for fully redundant
1540 ones). */
1542 static tree
1543 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
1545 tree temp, name;
1546 tree folded, forced_stmts, newexpr;
1547 tree v;
1548 tree_stmt_iterator tsi;
1550 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1552 case tcc_expression:
1554 tree op0, op2;
1555 tree arglist;
1556 tree genop0, genop2;
1557 tree genarglist;
1558 tree walker, genwalker;
1560 gcc_assert (TREE_CODE (expr) == CALL_EXPR);
1561 genop2 = NULL;
1563 op0 = TREE_OPERAND (expr, 0);
1564 arglist = TREE_OPERAND (expr, 1);
1565 op2 = TREE_OPERAND (expr, 2);
1567 genop0 = find_or_generate_expression (block, op0, stmts);
1568 genarglist = copy_list (arglist);
1569 for (walker = arglist, genwalker = genarglist;
1570 genwalker && walker;
1571 genwalker = TREE_CHAIN (genwalker), walker = TREE_CHAIN (walker))
1573 TREE_VALUE (genwalker) = find_or_generate_expression (block,
1574 TREE_VALUE (walker),
1575 stmts);
1578 if (op2)
1579 genop2 = find_or_generate_expression (block, op2, stmts);
1580 folded = fold_build3 (TREE_CODE (expr), TREE_TYPE (expr),
1581 genop0, genarglist, genop2);
1582 break;
1586 break;
1588 case tcc_binary:
1589 case tcc_comparison:
1591 tree op1 = TREE_OPERAND (expr, 0);
1592 tree op2 = TREE_OPERAND (expr, 1);
1593 tree genop1 = find_or_generate_expression (block, op1, stmts);
1594 tree genop2 = find_or_generate_expression (block, op2, stmts);
1595 folded = fold_build2 (TREE_CODE (expr), TREE_TYPE (expr),
1596 genop1, genop2);
1597 break;
1600 case tcc_unary:
1602 tree op1 = TREE_OPERAND (expr, 0);
1603 tree genop1 = find_or_generate_expression (block, op1, stmts);
1604 folded = fold_build1 (TREE_CODE (expr), TREE_TYPE (expr),
1605 genop1);
1606 break;
1609 default:
1610 gcc_unreachable ();
1613 /* Force the generated expression to be a sequence of GIMPLE
1614 statements.
1615 We have to call unshare_expr because force_gimple_operand may
1616 modify the tree we pass to it. */
1617 newexpr = force_gimple_operand (unshare_expr (folded), &forced_stmts,
1618 false, NULL);
1620 /* If we have any intermediate expressions to the value sets, add them
1621 to the value sets and chain them on in the instruction stream. */
1622 if (forced_stmts)
1624 tsi = tsi_start (forced_stmts);
1625 for (; !tsi_end_p (tsi); tsi_next (&tsi))
1627 tree stmt = tsi_stmt (tsi);
1628 tree forcedname = TREE_OPERAND (stmt, 0);
1629 tree forcedexpr = TREE_OPERAND (stmt, 1);
1630 tree val = vn_lookup_or_add (forcedexpr, NULL);
1632 VEC_safe_push (tree, heap, inserted_exprs, stmt);
1633 vn_add (forcedname, val, NULL);
1634 bitmap_value_replace_in_set (NEW_SETS (block), forcedname);
1635 bitmap_value_replace_in_set (AVAIL_OUT (block), forcedname);
1637 tsi = tsi_last (stmts);
1638 tsi_link_after (&tsi, forced_stmts, TSI_CONTINUE_LINKING);
1641 /* Build and insert the assignment of the end result to the temporary
1642 that we will return. */
1643 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1644 add_referenced_tmp_var (temp);
1645 if (TREE_CODE (TREE_TYPE (expr)) == COMPLEX_TYPE)
1646 DECL_COMPLEX_GIMPLE_REG_P (temp) = 1;
1647 newexpr = build2 (MODIFY_EXPR, TREE_TYPE (expr), temp, newexpr);
1648 name = make_ssa_name (temp, newexpr);
1649 TREE_OPERAND (newexpr, 0) = name;
1650 NECESSARY (newexpr) = 0;
1651 tsi = tsi_last (stmts);
1652 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1653 VEC_safe_push (tree, heap, inserted_exprs, newexpr);
1655 /* Add a value handle to the temporary.
1656 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
1657 we are creating the expression by pieces, and this particular piece of
1658 the expression may have been represented. There is no harm in replacing
1659 here. */
1660 v = get_value_handle (expr);
1661 vn_add (name, v, NULL);
1662 bitmap_value_replace_in_set (NEW_SETS (block), name);
1663 bitmap_value_replace_in_set (AVAIL_OUT (block), name);
1665 pre_stats.insertions++;
1666 if (dump_file && (dump_flags & TDF_DETAILS))
1668 fprintf (dump_file, "Inserted ");
1669 print_generic_expr (dump_file, newexpr, 0);
1670 fprintf (dump_file, " in predecessor %d\n", block->index);
1673 return name;
1676 /* Insert the to-be-made-available values of NODE for each predecessor, stored
1677 in AVAIL, into the predecessors of BLOCK, and merge the result with a phi
1678 node, given the same value handle as NODE. The prefix of the phi node is
1679 given with TMPNAME. Return true if we have inserted new stuff. */
1681 static bool
1682 insert_into_preds_of_block (basic_block block, value_set_node_t node,
1683 tree *avail, const char *tmpname)
1685 tree val = get_value_handle (node->expr);
1686 edge pred;
1687 bool insertions = false;
1688 bool nophi = false;
1689 basic_block bprime;
1690 tree eprime;
1691 edge_iterator ei;
1692 tree type = TREE_TYPE (avail[EDGE_PRED (block, 0)->src->index]);
1693 tree temp;
1695 if (dump_file && (dump_flags & TDF_DETAILS))
1697 fprintf (dump_file, "Found partial redundancy for expression ");
1698 print_generic_expr (dump_file, node->expr, 0);
1699 fprintf (dump_file, "\n");
1702 /* Make sure we aren't creating an induction variable. */
1703 if (block->loop_depth > 0 && EDGE_COUNT (block->preds) == 2)
1705 bool firstinsideloop = false;
1706 bool secondinsideloop = false;
1707 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
1708 EDGE_PRED (block, 0)->src);
1709 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
1710 EDGE_PRED (block, 1)->src);
1711 /* Induction variables only have one edge inside the loop. */
1712 if (firstinsideloop ^ secondinsideloop)
1714 if (dump_file && (dump_flags & TDF_DETAILS))
1715 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
1716 nophi = true;
1721 /* Make the necessary insertions. */
1722 FOR_EACH_EDGE (pred, ei, block->preds)
1724 tree stmts = alloc_stmt_list ();
1725 tree builtexpr;
1726 bprime = pred->src;
1727 eprime = avail[bprime->index];
1728 if (BINARY_CLASS_P (eprime)
1729 || COMPARISON_CLASS_P (eprime)
1730 || UNARY_CLASS_P (eprime)
1731 || TREE_CODE (eprime) == CALL_EXPR)
1733 builtexpr = create_expression_by_pieces (bprime,
1734 eprime,
1735 stmts);
1736 bsi_insert_on_edge (pred, stmts);
1737 avail[bprime->index] = builtexpr;
1738 insertions = true;
1741 /* If we didn't want a phi node, and we made insertions, we still have
1742 inserted new stuff, and thus return true. If we didn't want a phi node,
1743 and didn't make insertions, we haven't added anything new, so return
1744 false. */
1745 if (nophi && insertions)
1746 return true;
1747 else if (nophi && !insertions)
1748 return false;
1750 /* Now build a phi for the new variable. */
1751 temp = create_tmp_var (type, tmpname);
1752 add_referenced_tmp_var (temp);
1753 if (TREE_CODE (type) == COMPLEX_TYPE)
1754 DECL_COMPLEX_GIMPLE_REG_P (temp) = 1;
1755 temp = create_phi_node (temp, block);
1756 NECESSARY (temp) = 0;
1757 VEC_safe_push (tree, heap, inserted_exprs, temp);
1758 FOR_EACH_EDGE (pred, ei, block->preds)
1759 add_phi_arg (temp, avail[pred->src->index], pred);
1761 vn_add (PHI_RESULT (temp), val, NULL);
1763 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
1764 this insertion, since we test for the existence of this value in PHI_GEN
1765 before proceeding with the partial redundancy checks in insert_aux.
1767 The value may exist in AVAIL_OUT, in particular, it could be represented
1768 by the expression we are trying to eliminate, in which case we want the
1769 replacement to occur. If it's not existing in AVAIL_OUT, we want it
1770 inserted there.
1772 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
1773 this block, because if it did, it would have existed in our dominator's
1774 AVAIL_OUT, and would have been skipped due to the full redundancy check.
1777 bitmap_insert_into_set (PHI_GEN (block),
1778 PHI_RESULT (temp));
1779 bitmap_value_replace_in_set (AVAIL_OUT (block),
1780 PHI_RESULT (temp));
1781 bitmap_insert_into_set (NEW_SETS (block),
1782 PHI_RESULT (temp));
1784 if (dump_file && (dump_flags & TDF_DETAILS))
1786 fprintf (dump_file, "Created phi ");
1787 print_generic_expr (dump_file, temp, 0);
1788 fprintf (dump_file, " in block %d\n", block->index);
1790 pre_stats.phis++;
1791 return true;
1796 /* Perform insertion of partially redundant values.
1797 For BLOCK, do the following:
1798 1. Propagate the NEW_SETS of the dominator into the current block.
1799 If the block has multiple predecessors,
1800 2a. Iterate over the ANTIC expressions for the block to see if
1801 any of them are partially redundant.
1802 2b. If so, insert them into the necessary predecessors to make
1803 the expression fully redundant.
1804 2c. Insert a new PHI merging the values of the predecessors.
1805 2d. Insert the new PHI, and the new expressions, into the
1806 NEW_SETS set.
1807 3. Recursively call ourselves on the dominator children of BLOCK.
1811 static bool
1812 insert_aux (basic_block block)
1814 basic_block son;
1815 bool new_stuff = false;
1817 if (block)
1819 basic_block dom;
1820 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1821 if (dom)
1823 unsigned i;
1824 bitmap_iterator bi;
1825 bitmap_set_t newset = NEW_SETS (dom);
1826 if (newset)
1828 /* Note that we need to value_replace both NEW_SETS, and
1829 AVAIL_OUT. For both the case of NEW_SETS, the value may be
1830 represented by some non-simple expression here that we want
1831 to replace it with. */
1832 EXECUTE_IF_SET_IN_BITMAP (newset->expressions, 0, i, bi)
1834 bitmap_value_replace_in_set (NEW_SETS (block), ssa_name (i));
1835 bitmap_value_replace_in_set (AVAIL_OUT (block), ssa_name (i));
1838 if (!single_pred_p (block))
1840 value_set_node_t node;
1841 for (node = ANTIC_IN (block)->head;
1842 node;
1843 node = node->next)
1845 if (BINARY_CLASS_P (node->expr)
1846 || COMPARISON_CLASS_P (node->expr)
1847 || UNARY_CLASS_P (node->expr)
1848 || TREE_CODE (node->expr) == CALL_EXPR)
1850 tree *avail;
1851 tree val;
1852 bool by_some = false;
1853 bool cant_insert = false;
1854 bool all_same = true;
1855 tree first_s = NULL;
1856 edge pred;
1857 basic_block bprime;
1858 tree eprime = NULL_TREE;
1859 edge_iterator ei;
1861 val = get_value_handle (node->expr);
1862 if (bitmap_set_contains_value (PHI_GEN (block), val))
1863 continue;
1864 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
1866 if (dump_file && (dump_flags & TDF_DETAILS))
1867 fprintf (dump_file, "Found fully redundant value\n");
1868 continue;
1871 avail = XCNEWVEC (tree, last_basic_block);
1872 FOR_EACH_EDGE (pred, ei, block->preds)
1874 tree vprime;
1875 tree edoubleprime;
1877 /* This can happen in the very weird case
1878 that our fake infinite loop edges have caused a
1879 critical edge to appear. */
1880 if (EDGE_CRITICAL_P (pred))
1882 cant_insert = true;
1883 break;
1885 bprime = pred->src;
1886 eprime = phi_translate (node->expr,
1887 ANTIC_IN (block),
1888 bprime, block);
1890 /* eprime will generally only be NULL if the
1891 value of the expression, translated
1892 through the PHI for this predecessor, is
1893 undefined. If that is the case, we can't
1894 make the expression fully redundant,
1895 because its value is undefined along a
1896 predecessor path. We can thus break out
1897 early because it doesn't matter what the
1898 rest of the results are. */
1899 if (eprime == NULL)
1901 cant_insert = true;
1902 break;
1905 eprime = fully_constant_expression (eprime);
1906 vprime = get_value_handle (eprime);
1907 gcc_assert (vprime);
1908 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
1909 vprime);
1910 if (edoubleprime == NULL)
1912 avail[bprime->index] = eprime;
1913 all_same = false;
1915 else
1917 avail[bprime->index] = edoubleprime;
1918 by_some = true;
1919 if (first_s == NULL)
1920 first_s = edoubleprime;
1921 else if (!operand_equal_p (first_s, edoubleprime,
1923 all_same = false;
1926 /* If we can insert it, it's not the same value
1927 already existing along every predecessor, and
1928 it's defined by some predecessor, it is
1929 partially redundant. */
1930 if (!cant_insert && !all_same && by_some)
1932 if (insert_into_preds_of_block (block, node, avail,
1933 "prephitmp"))
1934 new_stuff = true;
1936 /* If all edges produce the same value and that value is
1937 an invariant, then the PHI has the same value on all
1938 edges. Note this. */
1939 else if (!cant_insert && all_same && eprime
1940 && is_gimple_min_invariant (eprime)
1941 && !is_gimple_min_invariant (val))
1943 value_set_t exprset = VALUE_HANDLE_EXPR_SET (val);
1944 value_set_node_t node;
1945 for (node = exprset->head; node; node = node->next)
1947 if (TREE_CODE (node->expr) == SSA_NAME)
1949 vn_add (node->expr, eprime, NULL);
1950 pre_stats.constified++;
1954 free (avail);
1960 for (son = first_dom_son (CDI_DOMINATORS, block);
1961 son;
1962 son = next_dom_son (CDI_DOMINATORS, son))
1964 new_stuff |= insert_aux (son);
1967 return new_stuff;
1970 /* Perform insertion of partially redundant values. */
1972 static void
1973 insert (void)
1975 bool new_stuff = true;
1976 basic_block bb;
1977 int num_iterations = 0;
1979 FOR_ALL_BB (bb)
1980 NEW_SETS (bb) = bitmap_set_new ();
1982 while (new_stuff)
1984 num_iterations++;
1985 new_stuff = false;
1986 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1988 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1989 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1993 /* Return true if VAR is an SSA variable with no defining statement in
1994 this procedure, *AND* isn't a live-on-entry parameter. */
1996 static bool
1997 is_undefined_value (tree expr)
1999 return (TREE_CODE (expr) == SSA_NAME
2000 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
2001 /* PARM_DECLs and hard registers are always defined. */
2002 && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL);
2006 /* Given an SSA variable VAR and an expression EXPR, compute the value
2007 number for EXPR and create a value handle (VAL) for it. If VAR and
2008 EXPR are not the same, associate VAL with VAR. Finally, add VAR to
2009 S1 and its value handle to S2.
2011 VUSES represent the virtual use operands associated with EXPR (if
2012 any). They are used when computing the hash value for EXPR. */
2014 static inline void
2015 add_to_sets (tree var, tree expr, tree stmt, bitmap_set_t s1,
2016 bitmap_set_t s2)
2018 tree val = vn_lookup_or_add (expr, stmt);
2020 /* VAR and EXPR may be the same when processing statements for which
2021 we are not computing value numbers (e.g., non-assignments, or
2022 statements that make aliased stores). In those cases, we are
2023 only interested in making VAR available as its own value. */
2024 if (var != expr)
2025 vn_add (var, val, NULL_TREE);
2027 if (s1)
2028 bitmap_insert_into_set (s1, var);
2029 bitmap_value_insert_into_set (s2, var);
2033 /* Given a unary or binary expression EXPR, create and return a new
2034 expression with the same structure as EXPR but with its operands
2035 replaced with the value handles of each of the operands of EXPR.
2037 VUSES represent the virtual use operands associated with EXPR (if
2038 any). They are used when computing the hash value for EXPR.
2039 Insert EXPR's operands into the EXP_GEN set for BLOCK. */
2041 static inline tree
2042 create_value_expr_from (tree expr, basic_block block, tree stmt)
2044 int i;
2045 enum tree_code code = TREE_CODE (expr);
2046 tree vexpr;
2047 alloc_pool pool;
2049 gcc_assert (TREE_CODE_CLASS (code) == tcc_unary
2050 || TREE_CODE_CLASS (code) == tcc_binary
2051 || TREE_CODE_CLASS (code) == tcc_comparison
2052 || TREE_CODE_CLASS (code) == tcc_reference
2053 || TREE_CODE_CLASS (code) == tcc_expression
2054 || TREE_CODE_CLASS (code) == tcc_exceptional);
2056 if (TREE_CODE_CLASS (code) == tcc_unary)
2057 pool = unary_node_pool;
2058 else if (TREE_CODE_CLASS (code) == tcc_reference)
2059 pool = reference_node_pool;
2060 else if (TREE_CODE_CLASS (code) == tcc_binary)
2061 pool = binary_node_pool;
2062 else if (TREE_CODE_CLASS (code) == tcc_comparison)
2063 pool = comparison_node_pool;
2064 else if (TREE_CODE_CLASS (code) == tcc_exceptional)
2066 gcc_assert (code == TREE_LIST);
2067 pool = list_node_pool;
2069 else
2071 gcc_assert (code == CALL_EXPR);
2072 pool = expression_node_pool;
2075 vexpr = (tree) pool_alloc (pool);
2076 memcpy (vexpr, expr, tree_size (expr));
2078 /* This case is only for TREE_LIST's that appear as part of
2079 CALL_EXPR's. Anything else is a bug, but we can't easily verify
2080 this, hence this comment. TREE_LIST is not handled by the
2081 general case below is because they don't have a fixed length, or
2082 operands, so you can't access purpose/value/chain through
2083 TREE_OPERAND macros. */
2085 if (code == TREE_LIST)
2087 tree op = NULL_TREE;
2088 tree temp = NULL_TREE;
2089 if (TREE_CHAIN (vexpr))
2090 temp = create_value_expr_from (TREE_CHAIN (vexpr), block, stmt);
2091 TREE_CHAIN (vexpr) = temp ? temp : TREE_CHAIN (vexpr);
2094 /* Recursively value-numberize reference ops. */
2095 if (REFERENCE_CLASS_P (TREE_VALUE (vexpr)))
2097 tree tempop;
2098 op = TREE_VALUE (vexpr);
2099 tempop = create_value_expr_from (op, block, stmt);
2100 op = tempop ? tempop : op;
2102 TREE_VALUE (vexpr) = vn_lookup_or_add (op, stmt);
2104 else
2106 op = TREE_VALUE (vexpr);
2107 TREE_VALUE (vexpr) = vn_lookup_or_add (TREE_VALUE (vexpr), NULL);
2109 /* This is the equivalent of inserting op into EXP_GEN like we
2110 do below */
2111 if (!is_undefined_value (op))
2112 value_insert_into_set (EXP_GEN (block), op);
2114 return vexpr;
2117 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
2119 tree val, op;
2121 op = TREE_OPERAND (expr, i);
2122 if (op == NULL_TREE)
2123 continue;
2125 /* If OP is a constant that has overflowed, do not value number
2126 this expression. */
2127 if (CONSTANT_CLASS_P (op)
2128 && TREE_OVERFLOW (op))
2130 pool_free (pool, vexpr);
2131 return NULL;
2134 /* Recursively value-numberize reference ops and tree lists. */
2135 if (REFERENCE_CLASS_P (op))
2137 tree tempop = create_value_expr_from (op, block, stmt);
2138 op = tempop ? tempop : op;
2139 val = vn_lookup_or_add (op, stmt);
2141 else if (TREE_CODE (op) == TREE_LIST)
2143 tree tempop;
2145 gcc_assert (TREE_CODE (expr) == CALL_EXPR);
2146 tempop = create_value_expr_from (op, block, stmt);
2148 op = tempop ? tempop : op;
2149 vn_lookup_or_add (op, NULL);
2150 /* Unlike everywhere else, we do *not* want to replace the
2151 TREE_LIST itself with a value number, because support
2152 functions we call will blow up. */
2153 val = op;
2155 else
2156 /* Create a value handle for OP and add it to VEXPR. */
2157 val = vn_lookup_or_add (op, NULL);
2159 if (!is_undefined_value (op) && TREE_CODE (op) != TREE_LIST)
2160 value_insert_into_set (EXP_GEN (block), op);
2162 if (TREE_CODE (val) == VALUE_HANDLE)
2163 TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
2165 TREE_OPERAND (vexpr, i) = val;
2168 return vexpr;
2172 /* Return true if we can value number a call. This is true if we have
2173 a pure or constant call. */
2174 static bool
2175 can_value_number_call (tree stmt)
2177 tree call = get_call_expr_in (stmt);
2179 /* This is a temporary restriction until we translate vuses through
2180 phi nodes. This is only needed for PRE, of course. */
2181 if (!in_fre && !ZERO_SSA_OPERANDS (stmt, SSA_OP_ALL_VIRTUALS))
2182 return false;
2183 if (call_expr_flags (call) & (ECF_PURE | ECF_CONST))
2184 return true;
2185 return false;
2188 /* Insert extra phis to merge values that are fully available from
2189 preds of BLOCK, but have no dominating representative coming from
2190 block DOM. */
2192 static void
2193 insert_extra_phis (basic_block block, basic_block dom)
2196 if (!single_pred_p (block))
2198 edge e;
2199 edge_iterator ei;
2200 bool first = true;
2201 bitmap_set_t tempset = bitmap_set_new ();
2203 FOR_EACH_EDGE (e, ei, block->preds)
2205 if (first)
2207 bitmap_set_copy (tempset, AVAIL_OUT (e->src));
2208 first = false;
2210 else
2211 bitmap_set_and (tempset, AVAIL_OUT (e->src));
2214 if (dom)
2215 bitmap_set_and_compl (tempset, AVAIL_OUT (dom));
2217 if (!bitmap_set_empty_p (tempset))
2219 unsigned int i;
2220 bitmap_iterator bi;
2222 EXECUTE_IF_SET_IN_BITMAP (tempset->expressions, 0, i, bi)
2224 tree name = ssa_name (i);
2225 tree val = get_value_handle (name);
2226 tree temp = create_tmp_var (TREE_TYPE (name), "mergephitmp");
2228 if (dump_file && (dump_flags & TDF_DETAILS))
2230 fprintf (dump_file, "Creating phi ");
2231 print_generic_expr (dump_file, temp, 0);
2232 fprintf (dump_file, " to merge available but not dominating values ");
2235 add_referenced_tmp_var (temp);
2236 temp = create_phi_node (temp, block);
2237 NECESSARY (temp) = 0;
2238 VEC_safe_push (tree, heap, inserted_exprs, temp);
2240 FOR_EACH_EDGE (e, ei, block->preds)
2242 tree leader = bitmap_find_leader (AVAIL_OUT (e->src), val);
2244 gcc_assert (leader);
2245 add_phi_arg (temp, leader, e);
2247 if (dump_file && (dump_flags & TDF_DETAILS))
2249 print_generic_expr (dump_file, leader, 0);
2250 fprintf (dump_file, " in block %d,", e->src->index);
2254 vn_add (PHI_RESULT (temp), val, NULL);
2256 if (dump_file && (dump_flags & TDF_DETAILS))
2257 fprintf (dump_file, "\n");
2263 /* Given a statement STMT and its right hand side which is a load, try
2264 to look for the expression stored in the location for the load, and
2265 return true if a useful equivalence was recorded for LHS. */
2267 static bool
2268 try_look_through_load (tree lhs, tree mem_ref, tree stmt, basic_block block)
2270 tree store_stmt = NULL;
2271 tree rhs;
2272 ssa_op_iter i;
2273 tree vuse;
2275 FOR_EACH_SSA_TREE_OPERAND (vuse, stmt, i, SSA_OP_VIRTUAL_USES)
2277 tree def_stmt;
2279 gcc_assert (TREE_CODE (vuse) == SSA_NAME);
2280 def_stmt = SSA_NAME_DEF_STMT (vuse);
2282 /* If there is no useful statement for this VUSE, we'll not find a
2283 useful expression to return either. Likewise, if there is a
2284 statement but it is not a simple assignment or it has virtual
2285 uses, we can stop right here. Note that this means we do
2286 not look through PHI nodes, which is intentional. */
2287 if (!def_stmt
2288 || TREE_CODE (def_stmt) != MODIFY_EXPR
2289 || !ZERO_SSA_OPERANDS (def_stmt, SSA_OP_VIRTUAL_USES))
2290 return false;
2292 /* If this is not the same statement as one we have looked at for
2293 another VUSE of STMT already, we have two statements producing
2294 something that reaches our STMT. */
2295 if (store_stmt && store_stmt != def_stmt)
2296 return false;
2297 else
2299 /* Is this a store to the exact same location as the one we are
2300 loading from in STMT? */
2301 if (!operand_equal_p (TREE_OPERAND (def_stmt, 0), mem_ref, 0))
2302 return false;
2304 /* Otherwise remember this statement and see if all other VUSEs
2305 come from the same statement. */
2306 store_stmt = def_stmt;
2310 /* Alright then, we have visited all VUSEs of STMT and we've determined
2311 that all of them come from the same statement STORE_STMT. See if there
2312 is a useful expression we can deduce from STORE_STMT. */
2313 rhs = TREE_OPERAND (store_stmt, 1);
2314 if ((TREE_CODE (rhs) == SSA_NAME
2315 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2316 || is_gimple_min_invariant (rhs)
2317 || TREE_CODE (rhs) == ADDR_EXPR
2318 || TREE_INVARIANT (rhs))
2321 /* Yay! Compute a value number for the RHS of the statement and
2322 add its value to the AVAIL_OUT set for the block. Add the LHS
2323 to TMP_GEN. */
2324 add_to_sets (lhs, rhs, store_stmt, TMP_GEN (block), AVAIL_OUT (block));
2325 if (TREE_CODE (rhs) == SSA_NAME
2326 && !is_undefined_value (rhs))
2327 value_insert_into_set (EXP_GEN (block), rhs);
2328 return true;
2331 return false;
2334 /* Compute the AVAIL set for all basic blocks.
2336 This function performs value numbering of the statements in each basic
2337 block. The AVAIL sets are built from information we glean while doing
2338 this value numbering, since the AVAIL sets contain only one entry per
2339 value.
2341 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
2342 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
2344 static void
2345 compute_avail (void)
2347 basic_block block, son;
2348 basic_block *worklist;
2349 size_t sp = 0;
2350 tree param;
2352 /* For arguments with default definitions, we pretend they are
2353 defined in the entry block. */
2354 for (param = DECL_ARGUMENTS (current_function_decl);
2355 param;
2356 param = TREE_CHAIN (param))
2358 if (default_def (param) != NULL)
2360 tree def = default_def (param);
2361 vn_lookup_or_add (def, NULL);
2362 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), def);
2363 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), def);
2367 /* Allocate the worklist. */
2368 worklist = XNEWVEC (basic_block, n_basic_blocks);
2370 /* Seed the algorithm by putting the dominator children of the entry
2371 block on the worklist. */
2372 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR);
2373 son;
2374 son = next_dom_son (CDI_DOMINATORS, son))
2375 worklist[sp++] = son;
2377 /* Loop until the worklist is empty. */
2378 while (sp)
2380 block_stmt_iterator bsi;
2381 tree stmt, phi;
2382 basic_block dom;
2384 /* Pick a block from the worklist. */
2385 block = worklist[--sp];
2387 /* Initially, the set of available values in BLOCK is that of
2388 its immediate dominator. */
2389 dom = get_immediate_dominator (CDI_DOMINATORS, block);
2390 if (dom)
2391 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
2393 if (!in_fre)
2394 insert_extra_phis (block, dom);
2396 /* Generate values for PHI nodes. */
2397 for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
2398 /* We have no need for virtual phis, as they don't represent
2399 actual computations. */
2400 if (is_gimple_reg (PHI_RESULT (phi)))
2401 add_to_sets (PHI_RESULT (phi), PHI_RESULT (phi), NULL,
2402 PHI_GEN (block), AVAIL_OUT (block));
2404 /* Now compute value numbers and populate value sets with all
2405 the expressions computed in BLOCK. */
2406 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
2408 stmt_ann_t ann;
2409 ssa_op_iter iter;
2410 tree op;
2412 stmt = bsi_stmt (bsi);
2413 ann = stmt_ann (stmt);
2415 /* We are only interested in assignments of the form
2416 X_i = EXPR, where EXPR represents an "interesting"
2417 computation, it has no volatile operands and X_i
2418 doesn't flow through an abnormal edge. */
2419 if (TREE_CODE (stmt) == MODIFY_EXPR
2420 && !ann->has_volatile_ops
2421 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
2422 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (stmt, 0)))
2424 tree lhs = TREE_OPERAND (stmt, 0);
2425 tree rhs = TREE_OPERAND (stmt, 1);
2427 /* Try to look through loads. */
2428 if (TREE_CODE (lhs) == SSA_NAME
2429 && !ZERO_SSA_OPERANDS (stmt, SSA_OP_VIRTUAL_USES)
2430 && try_look_through_load (lhs, rhs, stmt, block))
2431 continue;
2433 STRIP_USELESS_TYPE_CONVERSION (rhs);
2434 if (UNARY_CLASS_P (rhs)
2435 || BINARY_CLASS_P (rhs)
2436 || COMPARISON_CLASS_P (rhs)
2437 || REFERENCE_CLASS_P (rhs)
2438 || (TREE_CODE (rhs) == CALL_EXPR
2439 && can_value_number_call (stmt)))
2441 /* For binary, unary, and reference expressions,
2442 create a duplicate expression with the operands
2443 replaced with the value handles of the original
2444 RHS. */
2445 tree newt = create_value_expr_from (rhs, block, stmt);
2446 if (newt)
2448 add_to_sets (lhs, newt, stmt, TMP_GEN (block),
2449 AVAIL_OUT (block));
2450 value_insert_into_set (EXP_GEN (block), newt);
2451 continue;
2454 else if ((TREE_CODE (rhs) == SSA_NAME
2455 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (rhs))
2456 || is_gimple_min_invariant (rhs)
2457 || TREE_CODE (rhs) == ADDR_EXPR
2458 || TREE_INVARIANT (rhs)
2459 || DECL_P (rhs))
2461 /* Compute a value number for the RHS of the statement
2462 and add its value to the AVAIL_OUT set for the block.
2463 Add the LHS to TMP_GEN. */
2464 add_to_sets (lhs, rhs, stmt, TMP_GEN (block),
2465 AVAIL_OUT (block));
2467 if (TREE_CODE (rhs) == SSA_NAME
2468 && !is_undefined_value (rhs))
2469 value_insert_into_set (EXP_GEN (block), rhs);
2470 continue;
2474 /* For any other statement that we don't recognize, simply
2475 make the names generated by the statement available in
2476 AVAIL_OUT and TMP_GEN. */
2477 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
2478 add_to_sets (op, op, NULL, TMP_GEN (block), AVAIL_OUT (block));
2480 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
2481 add_to_sets (op, op, NULL, NULL , AVAIL_OUT (block));
2484 /* Put the dominator children of BLOCK on the worklist of blocks
2485 to compute available sets for. */
2486 for (son = first_dom_son (CDI_DOMINATORS, block);
2487 son;
2488 son = next_dom_son (CDI_DOMINATORS, son))
2489 worklist[sp++] = son;
2492 free (worklist);
2496 /* Eliminate fully redundant computations. */
2498 static void
2499 eliminate (void)
2501 basic_block b;
2503 FOR_EACH_BB (b)
2505 block_stmt_iterator i;
2507 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
2509 tree stmt = bsi_stmt (i);
2511 /* Lookup the RHS of the expression, see if we have an
2512 available computation for it. If so, replace the RHS with
2513 the available computation. */
2514 if (TREE_CODE (stmt) == MODIFY_EXPR
2515 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
2516 && TREE_CODE (TREE_OPERAND (stmt ,1)) != SSA_NAME
2517 && !is_gimple_min_invariant (TREE_OPERAND (stmt, 1))
2518 && !stmt_ann (stmt)->has_volatile_ops)
2520 tree lhs = TREE_OPERAND (stmt, 0);
2521 tree *rhs_p = &TREE_OPERAND (stmt, 1);
2522 tree sprime;
2524 sprime = bitmap_find_leader (AVAIL_OUT (b),
2525 vn_lookup (lhs, NULL));
2526 if (sprime
2527 && sprime != lhs
2528 && (TREE_CODE (*rhs_p) != SSA_NAME
2529 || may_propagate_copy (*rhs_p, sprime)))
2531 gcc_assert (sprime != *rhs_p);
2533 if (dump_file && (dump_flags & TDF_DETAILS))
2535 fprintf (dump_file, "Replaced ");
2536 print_generic_expr (dump_file, *rhs_p, 0);
2537 fprintf (dump_file, " with ");
2538 print_generic_expr (dump_file, sprime, 0);
2539 fprintf (dump_file, " in ");
2540 print_generic_stmt (dump_file, stmt, 0);
2543 if (TREE_CODE (sprime) == SSA_NAME)
2544 NECESSARY (SSA_NAME_DEF_STMT (sprime)) = 1;
2545 /* We need to make sure the new and old types actually match,
2546 which may require adding a simple cast, which fold_convert
2547 will do for us. */
2548 if (TREE_CODE (*rhs_p) != SSA_NAME
2549 && !tree_ssa_useless_type_conversion_1 (TREE_TYPE (*rhs_p),
2550 TREE_TYPE (sprime)))
2551 sprime = fold_convert (TREE_TYPE (*rhs_p), sprime);
2553 pre_stats.eliminations++;
2554 propagate_tree_value (rhs_p, sprime);
2555 update_stmt (stmt);
2557 /* If we removed EH side effects from the statement, clean
2558 its EH information. */
2559 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
2561 bitmap_set_bit (need_eh_cleanup,
2562 bb_for_stmt (stmt)->index);
2563 if (dump_file && (dump_flags & TDF_DETAILS))
2564 fprintf (dump_file, " Removed EH side effects.\n");
2572 /* Borrow a bit of tree-ssa-dce.c for the moment.
2573 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
2574 this may be a bit faster, and we may want critical edges kept split. */
2576 /* If OP's defining statement has not already been determined to be necessary,
2577 mark that statement necessary. Return the stmt, if it is newly
2578 necessary. */
2580 static inline tree
2581 mark_operand_necessary (tree op)
2583 tree stmt;
2585 gcc_assert (op);
2587 stmt = SSA_NAME_DEF_STMT (op);
2588 gcc_assert (stmt);
2590 if (NECESSARY (stmt)
2591 || IS_EMPTY_STMT (stmt))
2592 return NULL;
2594 NECESSARY (stmt) = 1;
2595 return stmt;
2598 /* Because we don't follow exactly the standard PRE algorithm, and decide not
2599 to insert PHI nodes sometimes, and because value numbering of casts isn't
2600 perfect, we sometimes end up inserting dead code. This simple DCE-like
2601 pass removes any insertions we made that weren't actually used. */
2603 static void
2604 remove_dead_inserted_code (void)
2606 VEC(tree,heap) *worklist = NULL;
2607 int i;
2608 tree t;
2610 worklist = VEC_alloc (tree, heap, VEC_length (tree, inserted_exprs));
2611 for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
2613 if (NECESSARY (t))
2614 VEC_quick_push (tree, worklist, t);
2616 while (VEC_length (tree, worklist) > 0)
2618 t = VEC_pop (tree, worklist);
2619 if (TREE_CODE (t) == PHI_NODE)
2621 /* PHI nodes are somewhat special in that each PHI alternative has
2622 data and control dependencies. All the statements feeding the
2623 PHI node's arguments are always necessary. In aggressive mode,
2624 we also consider the control dependent edges leading to the
2625 predecessor block associated with each PHI alternative as
2626 necessary. */
2627 int k;
2629 VEC_reserve (tree, heap, worklist, PHI_NUM_ARGS (t));
2630 for (k = 0; k < PHI_NUM_ARGS (t); k++)
2632 tree arg = PHI_ARG_DEF (t, k);
2633 if (TREE_CODE (arg) == SSA_NAME)
2635 arg = mark_operand_necessary (arg);
2636 if (arg)
2637 VEC_quick_push (tree, worklist, arg);
2641 else
2643 /* Propagate through the operands. Examine all the USE, VUSE and
2644 V_MAY_DEF operands in this statement. Mark all the statements
2645 which feed this statement's uses as necessary. */
2646 ssa_op_iter iter;
2647 tree use;
2649 /* The operands of V_MAY_DEF expressions are also needed as they
2650 represent potential definitions that may reach this
2651 statement (V_MAY_DEF operands allow us to follow def-def
2652 links). */
2654 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
2656 tree n = mark_operand_necessary (use);
2657 if (n)
2658 VEC_safe_push (tree, heap, worklist, n);
2662 for (i = 0; VEC_iterate (tree, inserted_exprs, i, t); i++)
2664 if (!NECESSARY (t))
2666 block_stmt_iterator bsi;
2667 if (dump_file && (dump_flags & TDF_DETAILS))
2669 fprintf (dump_file, "Removing unnecessary insertion:");
2670 print_generic_stmt (dump_file, t, 0);
2672 if (TREE_CODE (t) == PHI_NODE)
2674 remove_phi_node (t, NULL);
2676 else
2678 bsi = bsi_for_stmt (t);
2679 bsi_remove (&bsi);
2683 VEC_free (tree, heap, worklist);
2685 /* Initialize data structures used by PRE. */
2687 static void
2688 init_pre (bool do_fre)
2690 basic_block bb;
2692 in_fre = do_fre;
2694 inserted_exprs = NULL;
2695 vn_init ();
2696 if (!do_fre)
2697 current_loops = loop_optimizer_init (dump_file);
2698 connect_infinite_loops_to_exit ();
2699 memset (&pre_stats, 0, sizeof (pre_stats));
2701 /* If block 0 has more than one predecessor, it means that its PHI
2702 nodes will have arguments coming from block -1. This creates
2703 problems for several places in PRE that keep local arrays indexed
2704 by block number. To prevent this, we split the edge coming from
2705 ENTRY_BLOCK_PTR (FIXME, if ENTRY_BLOCK_PTR had an index number
2706 different than -1 we wouldn't have to hack this. tree-ssa-dce.c
2707 needs a similar change). */
2708 if (!single_pred_p (single_succ (ENTRY_BLOCK_PTR)))
2709 if (!(single_succ_edge (ENTRY_BLOCK_PTR)->flags & EDGE_ABNORMAL))
2710 split_edge (single_succ_edge (ENTRY_BLOCK_PTR));
2712 FOR_ALL_BB (bb)
2713 bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
2715 bitmap_obstack_initialize (&grand_bitmap_obstack);
2716 phi_translate_table = htab_create (511, expr_pred_trans_hash,
2717 expr_pred_trans_eq, free);
2718 value_set_pool = create_alloc_pool ("Value sets",
2719 sizeof (struct value_set), 30);
2720 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
2721 sizeof (struct bitmap_set), 30);
2722 value_set_node_pool = create_alloc_pool ("Value set nodes",
2723 sizeof (struct value_set_node), 30);
2724 calculate_dominance_info (CDI_POST_DOMINATORS);
2725 calculate_dominance_info (CDI_DOMINATORS);
2726 binary_node_pool = create_alloc_pool ("Binary tree nodes",
2727 tree_code_size (PLUS_EXPR), 30);
2728 unary_node_pool = create_alloc_pool ("Unary tree nodes",
2729 tree_code_size (NEGATE_EXPR), 30);
2730 reference_node_pool = create_alloc_pool ("Reference tree nodes",
2731 tree_code_size (ARRAY_REF), 30);
2732 expression_node_pool = create_alloc_pool ("Expression tree nodes",
2733 tree_code_size (CALL_EXPR), 30);
2734 list_node_pool = create_alloc_pool ("List tree nodes",
2735 tree_code_size (TREE_LIST), 30);
2736 comparison_node_pool = create_alloc_pool ("Comparison tree nodes",
2737 tree_code_size (EQ_EXPR), 30);
2738 FOR_ALL_BB (bb)
2740 EXP_GEN (bb) = set_new (true);
2741 PHI_GEN (bb) = bitmap_set_new ();
2742 TMP_GEN (bb) = bitmap_set_new ();
2743 AVAIL_OUT (bb) = bitmap_set_new ();
2746 need_eh_cleanup = BITMAP_ALLOC (NULL);
2750 /* Deallocate data structures used by PRE. */
2752 static void
2753 fini_pre (bool do_fre)
2755 basic_block bb;
2756 unsigned int i;
2758 VEC_free (tree, heap, inserted_exprs);
2759 bitmap_obstack_release (&grand_bitmap_obstack);
2760 free_alloc_pool (value_set_pool);
2761 free_alloc_pool (bitmap_set_pool);
2762 free_alloc_pool (value_set_node_pool);
2763 free_alloc_pool (binary_node_pool);
2764 free_alloc_pool (reference_node_pool);
2765 free_alloc_pool (unary_node_pool);
2766 free_alloc_pool (list_node_pool);
2767 free_alloc_pool (expression_node_pool);
2768 free_alloc_pool (comparison_node_pool);
2769 htab_delete (phi_translate_table);
2770 remove_fake_exit_edges ();
2772 FOR_ALL_BB (bb)
2774 free (bb->aux);
2775 bb->aux = NULL;
2778 free_dominance_info (CDI_POST_DOMINATORS);
2779 vn_delete ();
2781 if (!bitmap_empty_p (need_eh_cleanup))
2783 tree_purge_all_dead_eh_edges (need_eh_cleanup);
2784 cleanup_tree_cfg ();
2787 BITMAP_FREE (need_eh_cleanup);
2789 /* Wipe out pointers to VALUE_HANDLEs. In the not terribly distant
2790 future we will want them to be persistent though. */
2791 for (i = 0; i < num_ssa_names; i++)
2793 tree name = ssa_name (i);
2795 if (!name)
2796 continue;
2798 if (SSA_NAME_VALUE (name)
2799 && TREE_CODE (SSA_NAME_VALUE (name)) == VALUE_HANDLE)
2800 SSA_NAME_VALUE (name) = NULL;
2802 if (!do_fre && current_loops)
2804 loop_optimizer_finalize (current_loops, dump_file);
2805 current_loops = NULL;
2810 /* Main entry point to the SSA-PRE pass. DO_FRE is true if the caller
2811 only wants to do full redundancy elimination. */
2813 static void
2814 execute_pre (bool do_fre)
2816 init_pre (do_fre);
2818 /* Collect and value number expressions computed in each basic block. */
2819 compute_avail ();
2821 if (dump_file && (dump_flags & TDF_DETAILS))
2823 basic_block bb;
2825 FOR_ALL_BB (bb)
2827 print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
2828 bitmap_print_value_set (dump_file, TMP_GEN (bb), "tmp_gen",
2829 bb->index);
2830 bitmap_print_value_set (dump_file, AVAIL_OUT (bb), "avail_out",
2831 bb->index);
2835 /* Insert can get quite slow on an incredibly large number of basic
2836 blocks due to some quadratic behavior. Until this behavior is
2837 fixed, don't run it when he have an incredibly large number of
2838 bb's. If we aren't going to run insert, there is no point in
2839 computing ANTIC, either, even though it's plenty fast. */
2840 if (!do_fre && n_basic_blocks < 4000)
2842 compute_antic ();
2843 insert ();
2846 /* Remove all the redundant expressions. */
2847 eliminate ();
2850 if (dump_file && (dump_flags & TDF_STATS))
2852 fprintf (dump_file, "Insertions: %d\n", pre_stats.insertions);
2853 fprintf (dump_file, "New PHIs: %d\n", pre_stats.phis);
2854 fprintf (dump_file, "Eliminated: %d\n", pre_stats.eliminations);
2855 fprintf (dump_file, "Constified: %d\n", pre_stats.constified);
2858 bsi_commit_edge_inserts ();
2859 if (!do_fre)
2860 remove_dead_inserted_code ();
2861 fini_pre (do_fre);
2866 /* Gate and execute functions for PRE. */
2868 static void
2869 do_pre (void)
2871 execute_pre (false);
2874 static bool
2875 gate_pre (void)
2877 return flag_tree_pre != 0;
2880 struct tree_opt_pass pass_pre =
2882 "pre", /* name */
2883 gate_pre, /* gate */
2884 do_pre, /* execute */
2885 NULL, /* sub */
2886 NULL, /* next */
2887 0, /* static_pass_number */
2888 TV_TREE_PRE, /* tv_id */
2889 PROP_no_crit_edges | PROP_cfg
2890 | PROP_ssa | PROP_alias, /* properties_required */
2891 0, /* properties_provided */
2892 0, /* properties_destroyed */
2893 0, /* todo_flags_start */
2894 TODO_update_ssa | TODO_dump_func | TODO_ggc_collect
2895 | TODO_verify_ssa, /* todo_flags_finish */
2896 0 /* letter */
2900 /* Gate and execute functions for FRE. */
2902 static void
2903 execute_fre (void)
2905 execute_pre (true);
2908 static bool
2909 gate_fre (void)
2911 return flag_tree_fre != 0;
2914 struct tree_opt_pass pass_fre =
2916 "fre", /* name */
2917 gate_fre, /* gate */
2918 execute_fre, /* execute */
2919 NULL, /* sub */
2920 NULL, /* next */
2921 0, /* static_pass_number */
2922 TV_TREE_FRE, /* tv_id */
2923 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2924 0, /* properties_provided */
2925 0, /* properties_destroyed */
2926 0, /* todo_flags_start */
2927 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
2928 0 /* letter */
2931 /* Return true if T is a copy statement between two ssa names. */
2933 static bool
2934 is_copy_stmt (tree t)
2936 if (!t || TREE_CODE (t) != MODIFY_EXPR)
2937 return false;
2938 if (TREE_CODE (TREE_OPERAND (t, 0)) == SSA_NAME
2939 && TREE_CODE (TREE_OPERAND (t, 1)) == SSA_NAME)
2940 return true;
2941 return false;
2944 /* Starting from START, walk copy statements till we hit a statement with a
2945 VUSE or a non-copy statement. */
2947 static tree
2948 follow_copies_till_vuse (tree start)
2950 if (is_copy_stmt (start) && ZERO_SSA_OPERANDS (start, SSA_OP_VIRTUAL_USES))
2952 tree rhs, defstmt;
2954 rhs = TREE_OPERAND (start, 1);
2955 defstmt = SSA_NAME_DEF_STMT (rhs);
2956 return follow_copies_till_vuse (defstmt);
2958 return start;
2961 /* Gate and execute functions for eliminate useless stores.
2962 The goal here is to recognize the pattern *x = ... *x, and eliminate the
2963 store because the value hasn't changed. Store copy/const prop won't
2964 do this because making *more* loads (IE propagating *x) is not a win, so it
2965 ignores them.
2966 This pass is currently geared completely towards static variable store
2967 elimination. */
2969 static void
2970 do_eustores (void)
2972 basic_block bb;
2973 /* For each basic block
2974 For each statement (STMT) in the block
2975 if STMT is a stores of the pattern *x = y
2976 follow the chain of definitions for y, until we hit a non-copy
2977 statement or a statement with a vuse.
2978 if the statement we arrive at is a vuse of the operand we killed,
2979 accessed through the same memory operation, then we have a
2980 useless store (because it is *x = ... = *x). */
2982 FOR_EACH_BB (bb)
2984 block_stmt_iterator bsi;
2986 for (bsi = bsi_start (bb);
2987 !bsi_end_p (bsi);)
2989 tree stmt = bsi_stmt (bsi);
2990 tree startat;
2991 tree kill;
2992 tree found;
2994 if (NUM_SSA_OPERANDS (stmt, SSA_OP_VMUSTDEF) != 1
2995 || TREE_CODE (stmt) != MODIFY_EXPR
2996 || TREE_CODE (TREE_OPERAND (stmt, 1)) != SSA_NAME)
2998 bsi_next (&bsi);
2999 continue;
3002 kill = MUSTDEF_KILL (MUSTDEF_OPS (stmt));
3003 startat = TREE_OPERAND (stmt, 1);
3004 startat = SSA_NAME_DEF_STMT (startat);
3005 found = follow_copies_till_vuse (startat);
3007 if (found && TREE_CODE (found) == MODIFY_EXPR)
3010 /* We want exactly one virtual use, and it should match up with
3011 the use being killed. */
3013 if (NUM_SSA_OPERANDS (found, SSA_OP_VUSE) != 1
3014 || VUSE_OP (VUSE_OPS (found)) != kill
3015 || !DECL_P (TREE_OPERAND (stmt, 0))
3016 || !operand_equal_p (TREE_OPERAND (found, 1),
3017 TREE_OPERAND (stmt, 0), 0))
3019 bsi_next (&bsi);
3020 continue;
3023 if (dump_file)
3025 fprintf (dump_file, "Eliminating useless store ");
3026 print_generic_stmt (dump_file, stmt, 0);
3028 mark_sym_for_renaming (TREE_OPERAND (stmt, 0));
3029 bsi_remove (&bsi);
3031 else
3033 bsi_next (&bsi);
3034 continue;
3040 static bool
3041 gate_eustores(void)
3043 return flag_unit_at_a_time != 0;
3046 struct tree_opt_pass pass_eliminate_useless_stores =
3048 "eustores", /* name */
3049 gate_eustores, /* gate */
3050 do_eustores, /* execute */
3051 NULL, /* sub */
3052 NULL, /* next */
3053 0, /* static_pass_number */
3054 0, /* tv_id */
3055 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
3056 0, /* properties_provided */
3057 0, /* properties_destroyed */
3058 0, /* todo_flags_start */
3059 TODO_update_ssa | TODO_dump_func
3060 | TODO_ggc_collect | TODO_verify_ssa, /* todo_flags_finish */
3061 0 /* letter */