* target.h (struct gcc_target): Add new field to struct cxx: import_export_class.
[official-gcc.git] / gcc / tree-ssa-pre.c
blob712c3464078f6c080388b7b700c3c9e4c54aa2db
1 /* SSA-PRE for trees.
2 Copyright (C) 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3 Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
4 <stevenb@suse.de>
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GCC is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to
20 the Free Software Foundation, 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "errors.h"
28 #include "ggc.h"
29 #include "tree.h"
30 #include "basic-block.h"
31 #include "diagnostic.h"
32 #include "tree-inline.h"
33 #include "tree-flow.h"
34 #include "tree-gimple.h"
35 #include "tree-dump.h"
36 #include "timevar.h"
37 #include "fibheap.h"
38 #include "hashtab.h"
39 #include "tree-iterator.h"
40 #include "real.h"
41 #include "alloc-pool.h"
42 #include "tree-pass.h"
43 #include "flags.h"
44 #include "splay-tree.h"
45 #include "bitmap.h"
46 #include "langhooks.h"
48 /* TODO:
50 1. Implement load value numbering.
51 2. Speed up insert_aux so that we can use it all the time. It
52 spends most of it's time in quadratic value replacement.
53 3. Avail sets can be shared by making an avail_find_leader that
54 walks up the dominator tree and looks in those avail sets.
55 This might affect code optimality, it's unclear right now.
56 4. Load motion can be performed by value numbering the loads the
57 same as we do other expressions. This requires iterative
58 hashing the vuses into the values. Right now we simply assign
59 a new value every time we see a statement with a vuse.
60 5. Strength reduction can be performed by anticipating expressions
61 we can repair later on.
62 6. Our canonicalization of expressions during lookups don't take
63 constants into account very well. In particular, we don't fold
64 anywhere, so we can get situations where we stupidly think
65 something is a new value (a + 1 + 1 vs a + 2). This is somewhat
66 expensive to fix, but it does expose a lot more eliminations.
67 It may or not be worth it, depending on how critical you
68 consider PRE vs just plain GRE.
69 */
71 /* For ease of terminology, "expression node" in the below refers to
72 every expression node but MODIFY_EXPR, because MODIFY_EXPR's represent
73 the actual statement containing the expressions we care about, and
74 we cache the value number by putting it in the expression. */
76 /* Basic algorithm
78 First we walk the statements to generate the AVAIL sets, the
79 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
80 generation of values/expressions by a given block. We use them
81 when computing the ANTIC sets. The AVAIL sets consist of
82 SSA_NAME's that represent values, so we know what values are
83 available in what blocks. AVAIL is a forward dataflow problem. In
84 SSA, values are never killed, so we don't need a kill set, or a
85 fixpoint iteration, in order to calculate the AVAIL sets. In
86 traditional parlance, AVAIL sets tell us the downsafety of the
87 expressions/values.
89 Next, we generate the ANTIC sets. These sets represent the
90 anticipatable expressions. ANTIC is a backwards dataflow
91 problem.An expression is anticipatable in a given block if it could
92 be generated in that block. This means that if we had to perform
93 an insertion in that block, of the value of that expression, we
94 could. Calculating the ANTIC sets requires phi translation of
95 expressions, because the flow goes backwards through phis. We must
96 iterate to a fixpoint of the ANTIC sets, because we have a kill
97 set. Even in SSA form, values are not live over the entire
98 function, only from their definition point onwards. So we have to
99 remove values from the ANTIC set once we go past the definition
100 point of the leaders that make them up.
101 compute_antic/compute_antic_aux performs this computation.
103 Third, we perform insertions to make partially redundant
104 expressions fully redundant.
106 An expression is partially redundant (excluding partial
107 anticipation) if:
109 1. It is AVAIL in some, but not all, of the predecessors of a
110 given block.
111 2. It is ANTIC in all the predecessors.
113 In order to make it fully redundant, we insert the expression into
114 the predecessors where it is not available, but is ANTIC.
115 insert/insert_aux performs this insertion.
117 Fourth, we eliminate fully redundant expressions.
118 This is a simple statement walk that replaces redundant
119 calculations with the now available values. */
121 /* Representations of value numbers:
123 Value numbers are represented using the "value handle" approach.
124 This means that each SSA_NAME (and for other reasons to be
125 disclosed in a moment, expression nodes) has a value handle that
126 can be retrieved through get_value_handle. This value handle, *is*
127 the value number of the SSA_NAME. You can pointer compare the
128 value handles for equivalence purposes.
130 For debugging reasons, the value handle is internally more than
131 just a number, it is a VAR_DECL named "value.x", where x is a
132 unique number for each value number in use. This allows
133 expressions with SSA_NAMES replaced by value handles to still be
134 pretty printed in a sane way. They simply print as "value.3 *
135 value.5", etc.
137 Expression nodes have value handles associated with them as a
138 cache. Otherwise, we'd have to look them up again in the hash
139 table This makes significant difference (factor of two or more) on
140 some test cases. They can be thrown away after the pass is
141 finished. */
143 /* Representation of expressions on value numbers:
145 In some portions of this code, you will notice we allocate "fake"
146 analogues to the expression we are value numbering, and replace the
147 operands with the values of the expression. Since we work on
148 values, and not just names, we canonicalize expressions to value
149 expressions for use in the ANTIC sets, the EXP_GEN set, etc.
151 This is theoretically unnecessary, it just saves a bunch of
152 repeated get_value_handle and find_leader calls in the remainder of
153 the code, trading off temporary memory usage for speed. The tree
154 nodes aren't actually creating more garbage, since they are
155 allocated in a special pools which are thrown away at the end of
156 this pass.
158 All of this also means that if you print the EXP_GEN or ANTIC sets,
159 you will see "value.5 + value.7" in the set, instead of "a_55 +
160 b_66" or something. The only thing that actually cares about
161 seeing the value leaders is phi translation, and it needs to be
162 able to find the leader for a value in an arbitrary block, so this
163 "value expression" form is perfect for it (otherwise you'd do
164 get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
167 /* Representation of sets:
169 Sets are represented as doubly linked lists kept in topological
170 order, with an optional supporting bitmap of values present in the
171 set. The sets represent values, and the elements can be values or
172 expressions. The elements can appear in different sets, but each
173 element can only appear once in each set.
175 Since each node in the set represents a value, we also want to be
176 able to map expression, set pairs to something that tells us
177 whether the value is present is a set. We use a per-set bitmap for
178 that. The value handles also point to a linked list of the
179 expressions they represent via a tree annotation. This is mainly
180 useful only for debugging, since we don't do identity lookups. */
183 /* A value set element. Basically a single linked list of
184 expressions/values. */
185 typedef struct value_set_node
187 /* An expression. */
188 tree expr;
190 /* A pointer to the next element of the value set. */
191 struct value_set_node *next;
192 } *value_set_node_t;
195 /* A value set. This is a singly linked list of value_set_node
196 elements with a possible bitmap that tells us what values exist in
197 the set. This set must be kept in topologically sorted order. */
198 typedef struct value_set
200 /* The head of the list. Used for iterating over the list in
201 order. */
202 value_set_node_t head;
204 /* The tail of the list. Used for tail insertions, which are
205 necessary to keep the set in topologically sorted order because
206 of how the set is built. */
207 value_set_node_t tail;
209 /* The length of the list. */
210 size_t length;
212 /* True if the set is indexed, which means it contains a backing
213 bitmap for quick determination of whether certain values exist in the
214 set. */
215 bool indexed;
217 /* The bitmap of values that exist in the set. May be NULL in an
218 empty or non-indexed set. */
219 bitmap values;
221 } *value_set_t;
223 /* All of the following sets, except for TMP_GEN, are indexed.
224 TMP_GEN is only ever iterated over, we never check what values
225 exist in it. */
227 typedef struct bb_value_sets
229 /* The EXP_GEN set, which represents expressions/values generated in
230 a basic block. */
231 value_set_t exp_gen;
233 /* The PHI_GEN set, which represents PHI results generated in a
234 basic block. */
235 value_set_t phi_gen;
237 /* The TMP_GEN set, which represents results/temporaries genererated
238 in a basic block. IE the LHS of an expression. */
239 value_set_t tmp_gen;
241 /* The AVAIL_OUT set, which represents which values are available in
242 a given basic block. */
243 value_set_t avail_out;
245 /* The ANTIC_IN set, which represents which values are anticiptable
246 in a given basic block. */
247 value_set_t antic_in;
249 /* The NEW_SETS set, which is used during insertion to augment the
250 AVAIL_OUT set of blocks with the new insertions performed during
251 the current iteration. */
252 value_set_t new_sets;
253 } *bb_value_sets_t;
255 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
256 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
257 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
258 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
259 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
260 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
262 /* This structure is used to keep track of statistics on what
263 optimization PRE was able to perform. */
264 static struct
266 /* The number of RHS computations eliminated by PRE. */
267 int eliminations;
269 /* The number of new expressions/temporaries generated by PRE. */
270 int insertions;
272 /* The number of new PHI nodes added by PRE. */
273 int phis;
274 } pre_stats;
276 static tree find_leader (value_set_t, tree);
277 static void value_insert_into_set (value_set_t, tree);
278 static void insert_into_set (value_set_t, tree);
279 static value_set_t set_new (bool);
280 static bool is_undefined_value (tree);
281 static tree create_expression_by_pieces (basic_block, tree, tree);
283 /* We can add and remove elements and entries to and from sets
284 and hash tables, so we use alloc pools for them. */
286 static alloc_pool value_set_pool;
287 static alloc_pool value_set_node_pool;
288 static alloc_pool binary_node_pool;
289 static alloc_pool unary_node_pool;
292 /* The phi_translate_table caches phi translations for a given
293 expression and predecessor. */
295 static htab_t phi_translate_table;
297 /* A three tuple {e, pred, v} used to cache phi translations in the
298 phi_translate_table. */
300 typedef struct expr_pred_trans_d
302 /* The expression. */
303 tree e;
305 /* The predecessor block along which we translated the expression. */
306 basic_block pred;
308 /* The value that resulted from the translation. */
309 tree v;
311 /* The hashcode for the expression, pred pair. This is cached for
312 speed reasons. */
313 hashval_t hashcode;
314 } *expr_pred_trans_t;
316 /* Return the hash value for a phi translation table entry. */
318 static hashval_t
319 expr_pred_trans_hash (const void *p)
321 const expr_pred_trans_t ve = (expr_pred_trans_t) p;
322 return ve->hashcode;
325 /* Return true if two phi translation table entries are the same.
326 P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
328 static int
329 expr_pred_trans_eq (const void *p1, const void *p2)
331 const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
332 const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
333 basic_block b1 = ve1->pred;
334 basic_block b2 = ve2->pred;
337 /* If they are not translations for the same basic block, they can't
338 be equal. */
339 if (b1 != b2)
340 return false;
342 /* If they are for the same basic block, determine if the
343 expressions are equal. */
344 if (expressions_equal_p (ve1->e, ve2->e))
345 return true;
347 return false;
350 /* Search in the phi translation table for the translation of
351 expression E in basic block PRED. Return the translated value, if
352 found, NULL otherwise. */
354 static inline tree
355 phi_trans_lookup (tree e, basic_block pred)
357 void **slot;
358 struct expr_pred_trans_d ept;
359 ept.e = e;
360 ept.pred = pred;
361 ept.hashcode = vn_compute (e, (unsigned long) pred, NULL);
362 slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
363 NO_INSERT);
364 if (!slot)
365 return NULL;
366 else
367 return ((expr_pred_trans_t) *slot)->v;
371 /* Add the tuple mapping from {expression E, basic block PRED} to
372 value V, to the phi translation table. */
374 static inline void
375 phi_trans_add (tree e, tree v, basic_block pred)
377 void **slot;
378 expr_pred_trans_t new_pair = xmalloc (sizeof (*new_pair));
379 new_pair->e = e;
380 new_pair->pred = pred;
381 new_pair->v = v;
382 new_pair->hashcode = vn_compute (e, (unsigned long) pred, NULL);
383 slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
384 new_pair->hashcode, INSERT);
385 if (*slot)
386 free (*slot);
387 *slot = (void *) new_pair;
391 /* Add expression E to the expression set of value V. */
393 void
394 add_to_value (tree v, tree e)
396 /* Constants have no expression sets. */
397 if (is_gimple_min_invariant (v))
398 return;
400 if (VALUE_HANDLE_EXPR_SET (v) == NULL)
401 VALUE_HANDLE_EXPR_SET (v) = set_new (false);
403 insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
407 /* Return true if value V exists in the bitmap for SET. */
409 static inline bool
410 value_exists_in_set_bitmap (value_set_t set, tree v)
412 if (!set->values)
413 return false;
415 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (v));
419 /* Remove value V from the bitmap for SET. */
421 static void
422 value_remove_from_set_bitmap (value_set_t set, tree v)
424 #ifdef ENABLE_CHECKING
425 if (!set->indexed)
426 abort ();
427 #endif
429 if (!set->values)
430 return;
432 bitmap_clear_bit (set->values, VALUE_HANDLE_ID (v));
436 /* Insert the value number V into the bitmap of values existing in
437 SET. */
439 static inline void
440 value_insert_into_set_bitmap (value_set_t set, tree v)
442 #ifdef ENABLE_CHECKING
443 if (!set->indexed)
444 abort ();
445 #endif
447 if (set->values == NULL)
449 set->values = BITMAP_GGC_ALLOC ();
450 bitmap_clear (set->values);
453 bitmap_set_bit (set->values, VALUE_HANDLE_ID (v));
457 /* Create a new set. */
459 static value_set_t
460 set_new (bool indexed)
462 value_set_t ret;
463 ret = pool_alloc (value_set_pool);
464 ret->head = ret->tail = NULL;
465 ret->length = 0;
466 ret->indexed = indexed;
467 ret->values = NULL;
468 return ret;
472 /* Insert EXPR into SET. */
474 static void
475 insert_into_set (value_set_t set, tree expr)
477 value_set_node_t newnode = pool_alloc (value_set_node_pool);
478 tree val = get_value_handle (expr);
480 if (val == NULL)
481 abort ();
483 /* For indexed sets, insert the value into the set value bitmap.
484 For all sets, add it to the linked list and increment the list
485 length. */
486 if (set->indexed)
487 value_insert_into_set_bitmap (set, val);
489 newnode->next = NULL;
490 newnode->expr = expr;
491 set->length ++;
492 if (set->head == NULL)
494 set->head = set->tail = newnode;
496 else
498 set->tail->next = newnode;
499 set->tail = newnode;
503 /* Copy the set ORIG to the set DEST. */
505 static void
506 set_copy (value_set_t dest, value_set_t orig)
508 value_set_node_t node;
510 if (!orig || !orig->head)
511 return;
513 for (node = orig->head;
514 node;
515 node = node->next)
517 insert_into_set (dest, node->expr);
521 /* Remove EXPR from SET. */
523 static void
524 set_remove (value_set_t set, tree expr)
526 value_set_node_t node, prev;
528 /* Remove the value of EXPR from the bitmap, decrement the set
529 length, and remove it from the actual double linked list. */
530 value_remove_from_set_bitmap (set, get_value_handle (expr));
531 set->length--;
532 prev = NULL;
533 for (node = set->head;
534 node != NULL;
535 prev = node, node = node->next)
537 if (node->expr == expr)
539 if (prev == NULL)
540 set->head = node->next;
541 else
542 prev->next= node->next;
544 if (node == set->tail)
545 set->tail = prev;
546 pool_free (value_set_node_pool, node);
547 return;
552 /* Return true if SET contains the value VAL. */
554 static bool
555 set_contains_value (value_set_t set, tree val)
557 /* All constants are in every set. */
558 if (is_gimple_min_invariant (val))
559 return true;
561 if (set->length == 0)
562 return false;
564 return value_exists_in_set_bitmap (set, val);
567 /* Replace the leader for the value LOOKFOR in SET with EXPR. */
569 static void
570 set_replace_value (value_set_t set, tree lookfor, tree expr)
572 value_set_node_t node = set->head;
574 /* The lookup is probably more expensive than walking the linked
575 list when we have only a small number of nodes. */
576 if (!set_contains_value (set, lookfor))
577 return;
579 for (node = set->head;
580 node;
581 node = node->next)
583 if (get_value_handle (node->expr) == lookfor)
585 node->expr = expr;
586 return;
591 /* Return true if the set contains expression (not value) EXPR. */
593 static bool
594 set_contains (value_set_t set, tree expr)
596 value_set_node_t node;
598 for (node = set->head;
599 node;
600 node = node->next)
602 if (operand_equal_p (node->expr, expr, 0))
603 return true;
605 return false;
608 /* Subtract set B from set A, and return the new set. */
610 static value_set_t
611 set_subtract (value_set_t a, value_set_t b, bool indexed)
613 value_set_t ret = set_new (indexed);
614 value_set_node_t node;
615 for (node = a->head;
616 node;
617 node = node->next)
619 if (!set_contains (b, node->expr))
620 insert_into_set (ret, node->expr);
622 return ret;
625 /* Return true if two sets are equal. */
627 static bool
628 set_equal (value_set_t a, value_set_t b)
630 value_set_node_t node;
632 if (a->length != b->length)
633 return false;
634 for (node = a->head;
635 node;
636 node = node->next)
638 if (!set_contains_value (b, get_value_handle (node->expr)))
639 return false;
641 return true;
644 /* Replace the value for EXPR in SET with EXPR. */
645 static void
646 value_replace_in_set (value_set_t set, tree expr)
648 tree val = get_value_handle (expr);
650 if (set->length == 0)
651 return;
653 set_replace_value (set, val, expr);
656 /* Insert the value for EXPR into SET, if it doesn't exist already. */
658 static void
659 value_insert_into_set (value_set_t set, tree expr)
661 tree val = get_value_handle (expr);
663 /* Constant and invariant values exist everywhere, and thus,
664 actually keeping them in the sets is pointless. */
665 if (is_gimple_min_invariant (val))
666 return;
668 if (!set_contains_value (set, val))
669 insert_into_set (set, expr);
673 /* Print out the value_set SET to OUTFILE. */
675 static void
676 print_value_set (FILE *outfile, value_set_t set,
677 const char *setname, int blockindex)
679 value_set_node_t node;
680 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
681 if (set)
683 for (node = set->head;
684 node;
685 node = node->next)
687 print_generic_expr (outfile, node->expr, 0);
689 fprintf (outfile, " (");
690 print_generic_expr (outfile, get_value_handle (node->expr), 0);
691 fprintf (outfile, ") ");
693 if (node->next)
694 fprintf (outfile, ", ");
698 fprintf (outfile, " }\n");
701 /* Print out the expressions that have VAL to OUTFILE. */
703 void
704 print_value_expressions (FILE *outfile, tree val)
706 if (VALUE_HANDLE_EXPR_SET (val))
708 char s[10];
709 sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
710 print_value_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
715 void
716 debug_value_expressions (tree val)
718 print_value_expressions (stderr, val);
722 void debug_value_set (value_set_t, const char *, int);
724 void
725 debug_value_set (value_set_t set, const char *setname, int blockindex)
727 print_value_set (stderr, set, setname, blockindex);
730 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
731 the phis in PRED. Return NULL if we can't find a leader for each
732 part of the translated expression. */
734 static tree
735 phi_translate (tree expr, value_set_t set, basic_block pred,
736 basic_block phiblock)
738 tree phitrans = NULL;
739 tree oldexpr = expr;
741 if (expr == NULL)
742 return NULL;
744 /* Phi translations of a given expression don't change, */
745 phitrans = phi_trans_lookup (expr, pred);
746 if (phitrans)
747 return phitrans;
750 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
752 case '2':
754 tree oldop1 = TREE_OPERAND (expr, 0);
755 tree oldop2 = TREE_OPERAND (expr, 1);
756 tree newop1;
757 tree newop2;
758 tree newexpr;
760 newop1 = phi_translate (find_leader (set, oldop1),
761 set, pred, phiblock);
762 if (newop1 == NULL)
763 return NULL;
764 newop2 = phi_translate (find_leader (set, oldop2),
765 set, pred, phiblock);
766 if (newop2 == NULL)
767 return NULL;
768 if (newop1 != oldop1 || newop2 != oldop2)
770 newexpr = pool_alloc (binary_node_pool);
771 memcpy (newexpr, expr, tree_size (expr));
772 create_tree_ann (newexpr);
773 TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
774 TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
775 vn_lookup_or_add (newexpr, NULL);
776 expr = newexpr;
777 phi_trans_add (oldexpr, newexpr, pred);
780 break;
781 /* XXX: Until we have PRE of loads working, none will be ANTIC.
783 case 'r':
784 return NULL;
785 break;
786 case '1':
788 tree oldop1 = TREE_OPERAND (expr, 0);
789 tree newop1;
790 tree newexpr;
792 newop1 = phi_translate (find_leader (set, oldop1),
793 set, pred, phiblock);
794 if (newop1 == NULL)
795 return NULL;
796 if (newop1 != oldop1)
798 newexpr = pool_alloc (unary_node_pool);
799 memcpy (newexpr, expr, tree_size (expr));
800 create_tree_ann (newexpr);
801 TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
802 vn_lookup_or_add (newexpr, NULL);
803 expr = newexpr;
804 phi_trans_add (oldexpr, newexpr, pred);
807 break;
808 case 'd':
809 abort ();
810 case 'x':
812 tree phi = NULL;
813 int i;
814 if (TREE_CODE (expr) != SSA_NAME)
815 abort ();
816 if (TREE_CODE (SSA_NAME_DEF_STMT (expr)) == PHI_NODE)
817 phi = SSA_NAME_DEF_STMT (expr);
818 else
819 return expr;
821 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
822 if (PHI_ARG_EDGE (phi, i)->src == pred)
824 tree val;
825 if (is_undefined_value (PHI_ARG_DEF (phi, i)))
826 return NULL;
827 val = vn_lookup_or_add (PHI_ARG_DEF (phi, i), NULL);
828 return PHI_ARG_DEF (phi, i);
831 break;
833 return expr;
836 static void
837 phi_translate_set (value_set_t dest, value_set_t set, basic_block pred,
838 basic_block phiblock)
840 value_set_node_t node;
841 for (node = set->head;
842 node;
843 node = node->next)
845 tree translated;
846 translated = phi_translate (node->expr, set, pred, phiblock);
847 phi_trans_add (node->expr, translated, pred);
849 if (translated != NULL)
850 value_insert_into_set (dest, translated);
854 /* Find the leader for a value (i.e., the name representing that
855 value) in a given set, and return it. Return NULL if no leader is
856 found. */
858 static tree
859 find_leader (value_set_t set, tree val)
861 value_set_node_t node;
863 if (val == NULL)
864 return NULL;
866 /* Constants represent themselves. */
867 if (is_gimple_min_invariant (val))
868 return val;
870 if (set->length == 0)
871 return NULL;
873 if (value_exists_in_set_bitmap (set, val))
875 for (node = set->head;
876 node;
877 node = node->next)
879 if (get_value_handle (node->expr) == val)
880 return node->expr;
884 return NULL;
887 /* Determine if the expression EXPR is valid in SET. This means that
888 we have a leader for each part of the expression (if it consists of
889 values), or the expression is an SSA_NAME.
891 NB: We never should run into a case where we have SSA_NAME +
892 SSA_NAME or SSA_NAME + value. The sets valid_in_set is called on,
893 the ANTIC sets, will only ever have SSA_NAME's or binary value
894 expression (IE VALUE1 + VALUE2) */
896 static bool
897 valid_in_set (value_set_t set, tree expr)
899 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
901 case '2':
903 tree op1 = TREE_OPERAND (expr, 0);
904 tree op2 = TREE_OPERAND (expr, 1);
905 return set_contains_value (set, op1) && set_contains_value (set, op2);
907 break;
908 case '1':
910 tree op1 = TREE_OPERAND (expr, 0);
911 return set_contains_value (set, op1);
913 break;
914 /* XXX: Until PRE of loads works, no reference nodes are ANTIC.
916 case 'r':
918 return false;
920 case 'x':
922 if (TREE_CODE (expr) == SSA_NAME)
923 return true;
924 abort ();
926 case 'c':
927 abort ();
929 return false;
932 /* Clean the set of expressions that are no longer valid in SET. This
933 means expressions that are made up of values we have no leaders for
934 in SET. */
936 static void
937 clean (value_set_t set)
939 value_set_node_t node;
940 value_set_node_t next;
941 node = set->head;
942 while (node)
944 next = node->next;
945 if (!valid_in_set (set, node->expr))
946 set_remove (set, node->expr);
947 node = next;
951 /* Compute the ANTIC set for BLOCK.
953 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK), if
954 succs(BLOCK) > 1
955 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)]) if
956 succs(BLOCK) == 1
958 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] -
959 TMP_GEN[BLOCK])
961 Iterate until fixpointed.
963 XXX: It would be nice to either write a set_clear, and use it for
964 antic_out, or to mark the antic_out set as deleted at the end
965 of this routine, so that the pool can hand the same memory back out
966 again for the next antic_out. */
969 static bool
970 compute_antic_aux (basic_block block)
972 basic_block son;
973 edge e;
974 bool changed = false;
975 value_set_t S, old, ANTIC_OUT;
976 value_set_node_t node;
978 ANTIC_OUT = S = NULL;
979 /* If any edges from predecessors are abnormal, antic_in is empty, so
980 punt. Remember that the block has an incoming abnormal edge by
981 setting the BB_VISITED flag. */
982 if (! (block->flags & BB_VISITED))
984 for (e = block->pred; e; e = e->pred_next)
985 if (e->flags & EDGE_ABNORMAL)
987 block->flags |= BB_VISITED;
988 break;
991 if (block->flags & BB_VISITED)
993 S = NULL;
994 goto visit_sons;
998 old = set_new (false);
999 set_copy (old, ANTIC_IN (block));
1000 ANTIC_OUT = set_new (true);
1002 /* If the block has no successors, ANTIC_OUT is empty, because it is
1003 the exit block. */
1004 if (block->succ == NULL);
1006 /* If we have one successor, we could have some phi nodes to
1007 translate through. */
1008 else if (block->succ->succ_next == NULL)
1010 phi_translate_set (ANTIC_OUT, ANTIC_IN(block->succ->dest),
1011 block, block->succ->dest);
1013 /* If we have multiple successors, we take the intersection of all of
1014 them. */
1015 else
1017 varray_type worklist;
1018 edge e;
1019 size_t i;
1020 basic_block bprime, first;
1022 VARRAY_BB_INIT (worklist, 1, "succ");
1023 e = block->succ;
1024 while (e)
1026 VARRAY_PUSH_BB (worklist, e->dest);
1027 e = e->succ_next;
1029 first = VARRAY_BB (worklist, 0);
1030 set_copy (ANTIC_OUT, ANTIC_IN (first));
1032 for (i = 1; i < VARRAY_ACTIVE_SIZE (worklist); i++)
1034 bprime = VARRAY_BB (worklist, i);
1035 node = ANTIC_OUT->head;
1036 while (node)
1038 tree val;
1039 value_set_node_t next = node->next;
1040 val = get_value_handle (node->expr);
1041 if (!set_contains_value (ANTIC_IN (bprime), val))
1042 set_remove (ANTIC_OUT, node->expr);
1043 node = next;
1046 VARRAY_CLEAR (worklist);
1049 /* Generate ANTIC_OUT - TMP_GEN */
1050 S = set_subtract (ANTIC_OUT, TMP_GEN (block), false);
1052 /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1053 ANTIC_IN (block) = set_subtract (EXP_GEN (block),TMP_GEN (block), true);
1055 /* Then union in the ANTIC_OUT - TMP_GEN values, to get ANTIC_OUT U
1056 EXP_GEN - TMP_GEN */
1057 for (node = S->head;
1058 node;
1059 node = node->next)
1061 value_insert_into_set (ANTIC_IN (block), node->expr);
1063 clean (ANTIC_IN (block));
1066 if (!set_equal (old, ANTIC_IN (block)))
1067 changed = true;
1069 visit_sons:
1070 if (dump_file && (dump_flags & TDF_DETAILS))
1072 if (ANTIC_OUT)
1073 print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1074 print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1075 if (S)
1076 print_value_set (dump_file, S, "S", block->index);
1080 for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1081 son;
1082 son = next_dom_son (CDI_POST_DOMINATORS, son))
1084 changed |= compute_antic_aux (son);
1086 return changed;
1089 /* Compute ANTIC sets. */
1091 static void
1092 compute_antic (void)
1094 bool changed = true;
1095 basic_block bb;
1096 int num_iterations = 0;
1097 FOR_ALL_BB (bb)
1099 ANTIC_IN (bb) = set_new (true);
1100 if (bb->flags & BB_VISITED)
1101 abort ();
1104 while (changed)
1106 num_iterations++;
1107 changed = false;
1108 changed = compute_antic_aux (EXIT_BLOCK_PTR);
1110 FOR_ALL_BB (bb)
1112 bb->flags &= ~BB_VISITED;
1114 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1115 fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1119 /* Find a leader for an expression, or generate one using
1120 create_expression_by_pieces if it's ANTIC but
1121 complex.
1122 BLOCK is the basic_block we are looking for leaders in.
1123 EXPR is the expression to find a leader or generate for.
1124 STMTS is the statement list to put the inserted expressions on.
1125 Returns the SSA_NAME of the LHS of the generated expression or the
1126 leader. */
1128 static tree
1129 find_or_generate_expression (basic_block block, tree expr, tree stmts)
1131 tree genop;
1132 genop = find_leader (AVAIL_OUT (block), expr);
1133 /* Depending on the order we process DOM branches in, the value
1134 may not have propagated to all the dom children yet during
1135 this iteration. In this case, the value will always be in
1136 the NEW_SETS for us already, having been propagated from our
1137 dominator. */
1138 if (genop == NULL)
1139 genop = find_leader (NEW_SETS (block), expr);
1140 /* If it's still NULL, see if it is a complex expression, and if
1141 so, generate it recursively, otherwise, abort, because it's
1142 not really . */
1143 if (genop == NULL)
1145 genop = VALUE_HANDLE_EXPR_SET (expr)->head->expr;
1146 if (TREE_CODE_CLASS (TREE_CODE (genop)) != '1'
1147 && TREE_CODE_CLASS (TREE_CODE (genop)) != '2')
1148 abort ();
1149 genop = create_expression_by_pieces (block, genop, stmts);
1151 return genop;
1155 /* Create an expression in pieces, so that we can handle very complex
1156 expressions that may be ANTIC, but not necessary GIMPLE.
1157 BLOCK is the basic block the expression will be inserted into,
1158 EXPR is the expression to insert (in value form)
1159 STMTS is a statement list to append the necessary insertions into.
1161 This function will abort if we hit some value that shouldn't be
1162 ANTIC but is (IE there is no leader for it, or its components).
1163 This function may also generate expressions that are themselves
1164 partially or fully redundant. Those that are will be either made
1165 fully redundant during the next iteration of insert (for partially
1166 redundant ones), or eliminated by eliminate (for fully redundant
1167 ones). */
1169 static tree
1170 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
1172 tree name = NULL_TREE;
1173 tree newexpr = NULL_TREE;
1174 tree v;
1176 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1178 case '2':
1180 tree_stmt_iterator tsi;
1181 tree genop1, genop2;
1182 tree temp;
1183 tree op1 = TREE_OPERAND (expr, 0);
1184 tree op2 = TREE_OPERAND (expr, 1);
1185 genop1 = find_or_generate_expression (block, op1, stmts);
1186 genop2 = find_or_generate_expression (block, op2, stmts);
1187 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1188 add_referenced_tmp_var (temp);
1189 newexpr = build (TREE_CODE (expr), TREE_TYPE (expr),
1190 genop1, genop2);
1191 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1192 temp, newexpr);
1193 name = make_ssa_name (temp, newexpr);
1194 TREE_OPERAND (newexpr, 0) = name;
1195 tsi = tsi_last (stmts);
1196 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1197 pre_stats.insertions++;
1198 break;
1200 case '1':
1202 tree_stmt_iterator tsi;
1203 tree genop1;
1204 tree temp;
1205 tree op1 = TREE_OPERAND (expr, 0);
1206 genop1 = find_or_generate_expression (block, op1, stmts);
1207 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1208 add_referenced_tmp_var (temp);
1209 newexpr = build (TREE_CODE (expr), TREE_TYPE (expr),
1210 genop1);
1211 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1212 temp, newexpr);
1213 name = make_ssa_name (temp, newexpr);
1214 TREE_OPERAND (newexpr, 0) = name;
1215 tsi = tsi_last (stmts);
1216 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1217 pre_stats.insertions++;
1219 break;
1221 default:
1222 abort ();
1225 v = get_value_handle (expr);
1226 vn_add (name, v, NULL);
1227 insert_into_set (NEW_SETS (block), name);
1228 value_insert_into_set (AVAIL_OUT (block), name);
1229 if (dump_file && (dump_flags & TDF_DETAILS))
1231 fprintf (dump_file, "Inserted ");
1232 print_generic_expr (dump_file, newexpr, 0);
1233 fprintf (dump_file, " in predecessor %d\n", block->index);
1235 return name;
1238 /* Perform insertion of partially redundant values.
1239 For BLOCK, do the following:
1240 1. Propagate the NEW_SETS of the dominator into the current block.
1241 If the block has multiple predecessors,
1242 2a. Iterate over the ANTIC expressions for the block to see if
1243 any of them are partially redundant.
1244 2b. If so, insert them into the necessary predecessors to make
1245 the expression fully redundant.
1246 2c. Insert a new PHI merging the values of the predecessors.
1247 2d. Insert the new PHI, and the new expressions, into the
1248 NEW_SETS set.
1249 3. Recursively call ourselves on the dominator children of BLOCK.
1252 static bool
1253 insert_aux (basic_block block)
1255 basic_block son;
1256 bool new_stuff = false;
1258 if (block)
1260 value_set_node_t e;
1261 basic_block dom;
1262 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1263 if (dom)
1265 e = NEW_SETS (dom)->head;
1266 while (e)
1268 insert_into_set (NEW_SETS (block), e->expr);
1269 value_replace_in_set (AVAIL_OUT (block), e->expr);
1270 e = e->next;
1272 if (block->pred->pred_next)
1274 value_set_node_t node;
1275 for (node = ANTIC_IN (block)->head;
1276 node;
1277 node = node->next)
1279 if (TREE_CODE_CLASS (TREE_CODE (node->expr)) == '2'
1280 || TREE_CODE_CLASS (TREE_CODE (node->expr)) == '1')
1282 tree *avail;
1283 tree val;
1284 bool by_some = false;
1285 bool cant_insert = false;
1286 bool all_same = true;
1287 tree first_s = NULL;
1288 edge pred;
1289 basic_block bprime;
1290 tree eprime;
1292 val = get_value_handle (node->expr);
1293 if (set_contains_value (PHI_GEN (block), val))
1294 continue;
1295 if (set_contains_value (AVAIL_OUT (dom), val))
1297 if (dump_file && (dump_flags & TDF_DETAILS))
1298 fprintf (dump_file, "Found fully redundant value\n");
1299 continue;
1302 avail = xcalloc (last_basic_block, sizeof (tree));
1303 for (pred = block->pred;
1304 pred;
1305 pred = pred->pred_next)
1307 tree vprime;
1308 tree edoubleprime;
1309 bprime = pred->src;
1310 eprime = phi_translate (node->expr,
1311 ANTIC_IN (block),
1312 bprime, block);
1314 /* eprime will generally only be NULL if the
1315 value of the expression, translated
1316 through the PHI for this predecessor, is
1317 undefined. If that is the case, we can't
1318 make the expression fully redundant,
1319 because its value is undefined along a
1320 predecessor path. We can thus break out
1321 early because it doesn't matter what the
1322 rest of the results are. */
1323 if (eprime == NULL)
1325 cant_insert = true;
1326 break;
1329 vprime = get_value_handle (eprime);
1330 if (!vprime)
1331 abort ();
1332 edoubleprime = find_leader (AVAIL_OUT (bprime),
1333 vprime);
1334 if (edoubleprime == NULL)
1336 avail[bprime->index] = eprime;
1337 all_same = false;
1339 else
1341 avail[bprime->index] = edoubleprime;
1342 by_some = true;
1343 if (first_s == NULL)
1344 first_s = edoubleprime;
1345 else if (first_s != edoubleprime)
1346 all_same = false;
1347 if (first_s != edoubleprime
1348 && operand_equal_p (first_s, edoubleprime, 0))
1349 abort ();
1352 /* If we can insert it, it's not the same value
1353 already existing along every predecessor, and
1354 it's defined by some predecessor, it is
1355 partially redundant. */
1356 if (!cant_insert && !all_same && by_some)
1358 tree type = TREE_TYPE (avail[block->pred->src->index]);
1359 tree temp;
1360 if (dump_file && (dump_flags & TDF_DETAILS))
1362 fprintf (dump_file, "Found partial redundancy for expression ");
1363 print_generic_expr (dump_file, node->expr, 0);
1364 fprintf (dump_file, "\n");
1367 /* Make the necessary insertions. */
1368 for (pred = block->pred;
1369 pred;
1370 pred = pred->pred_next)
1372 tree stmts = alloc_stmt_list ();
1373 tree builtexpr;
1374 bprime = pred->src;
1375 eprime = avail[bprime->index];
1376 if (TREE_CODE_CLASS (TREE_CODE (eprime)) == '2'
1377 || TREE_CODE_CLASS (TREE_CODE (eprime)) == '1')
1379 builtexpr = create_expression_by_pieces (bprime,
1380 eprime,
1381 stmts);
1382 bsi_insert_on_edge (pred, stmts);
1383 bsi_commit_edge_inserts (NULL);
1384 avail[bprime->index] = builtexpr;
1387 /* Now build a phi for the new variable. */
1388 temp = create_tmp_var (type, "prephitmp");
1389 add_referenced_tmp_var (temp);
1390 temp = create_phi_node (temp, block);
1391 vn_add (PHI_RESULT (temp), val, NULL);
1393 #if 0
1394 if (!set_contains_value (AVAIL_OUT (block), val))
1395 insert_into_set (AVAIL_OUT (block),
1396 PHI_RESULT (temp));
1397 else
1398 #endif
1399 value_replace_in_set (AVAIL_OUT (block),
1400 PHI_RESULT (temp));
1401 for (pred = block->pred;
1402 pred;
1403 pred = pred->pred_next)
1405 add_phi_arg (&temp, avail[pred->src->index],
1406 pred);
1408 if (dump_file && (dump_flags & TDF_DETAILS))
1410 fprintf (dump_file, "Created phi ");
1411 print_generic_expr (dump_file, temp, 0);
1412 fprintf (dump_file, " in block %d\n", block->index);
1414 pre_stats.phis++;
1415 new_stuff = true;
1416 insert_into_set (NEW_SETS (block),
1417 PHI_RESULT (temp));
1418 insert_into_set (PHI_GEN (block),
1419 PHI_RESULT (temp));
1422 free (avail);
1428 for (son = first_dom_son (CDI_DOMINATORS, block);
1429 son;
1430 son = next_dom_son (CDI_DOMINATORS, son))
1432 new_stuff |= insert_aux (son);
1435 return new_stuff;
1438 /* Perform insertion of partially redundant values. */
1440 static void
1441 insert (void)
1443 bool new_stuff = true;
1444 basic_block bb;
1445 int num_iterations = 0;
1447 FOR_ALL_BB (bb)
1448 NEW_SETS (bb) = set_new (true);
1450 while (new_stuff)
1452 num_iterations++;
1453 new_stuff = false;
1454 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1456 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1457 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1461 /* Return true if VAR is an SSA variable with no defining statement in
1462 this procedure, *AND* isn't a live-on-entry parameter. */
1464 static bool
1465 is_undefined_value (tree expr)
1467 return (TREE_CODE (expr) == SSA_NAME
1468 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
1469 /* PARM_DECLs and hard registers are always defined. */
1470 && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL
1471 && !DECL_HARD_REGISTER (SSA_NAME_VAR (expr)));
1475 /* Given an SSA variable VAR and an expression EXPR, compute the value
1476 number for EXPR and create a value handle (VAL) for it. If VAR and
1477 EXPR are not the same, associate VAL with VAR. Finally, add VAR to
1478 S1 and its value handle to S2.
1480 VUSES represent the virtual use operands associated with EXPR (if
1481 any). They are used when computing the hash value for EXPR. */
1483 static inline void
1484 add_to_sets (tree var, tree expr, vuse_optype vuses, value_set_t s1,
1485 value_set_t s2)
1487 tree val = vn_lookup_or_add (expr, vuses);
1489 /* VAR and EXPR may be the same when processing statements for which
1490 we are not computing value numbers (e.g., non-assignments, or
1491 statements that make aliased stores). In those cases, we are
1492 only interested in making VAR available as its own value. */
1493 if (var != expr)
1494 vn_add (var, val, vuses);
1496 insert_into_set (s1, var);
1497 value_insert_into_set (s2, var);
1501 /* Given a unary or binary expression EXPR, create and return a new
1502 expresion with the same structure as EXPR but with its operands
1503 replaced with the value handles of each of the operands of EXPR.
1504 Insert EXPR's operands into the EXP_GEN set for BLOCK.
1506 VUSES represent the virtual use operands associated with EXPR (if
1507 any). They are used when computing the hash value for EXPR. */
1509 static inline tree
1510 create_value_expr_from (tree expr, basic_block block, vuse_optype vuses)
1512 int i;
1513 enum tree_code code = TREE_CODE (expr);
1514 tree vexpr;
1516 #if defined ENABLE_CHECKING
1517 if (TREE_CODE_CLASS (code) != '1'
1518 && TREE_CODE_CLASS (code) != '2')
1519 abort ();
1520 #endif
1522 if (TREE_CODE_CLASS (code) == '1')
1523 vexpr = pool_alloc (unary_node_pool);
1524 else
1525 vexpr = pool_alloc (binary_node_pool);
1527 memcpy (vexpr, expr, tree_size (expr));
1529 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
1531 tree op = TREE_OPERAND (expr, i);
1532 tree val = vn_lookup_or_add (op, vuses);
1533 if (!is_undefined_value (op))
1534 value_insert_into_set (EXP_GEN (block), op);
1535 TREE_OPERAND (vexpr, i) = val;
1538 return vexpr;
1542 /* Compute the AVAIL set for BLOCK.
1543 This function performs value numbering of the statements in BLOCK.
1544 The AVAIL sets are built from information we glean while doing this
1545 value numbering, since the AVAIL sets contain only one entry per
1546 value.
1548 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
1549 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
1551 static void
1552 compute_avail (basic_block block)
1554 basic_block son;
1556 /* For arguments with default definitions, we pretend they are
1557 defined in the entry block. */
1558 if (block == ENTRY_BLOCK_PTR)
1560 tree param;
1561 for (param = DECL_ARGUMENTS (current_function_decl);
1562 param;
1563 param = TREE_CHAIN (param))
1565 if (default_def (param) != NULL)
1567 tree val;
1568 tree def = default_def (param);
1569 val = vn_lookup_or_add (def, NULL);
1570 insert_into_set (TMP_GEN (block), def);
1571 value_insert_into_set (AVAIL_OUT (block), def);
1575 else if (block)
1577 block_stmt_iterator bsi;
1578 tree stmt, phi;
1579 basic_block dom;
1581 /* Initially, the set of available values in BLOCK is that of
1582 its immediate dominator. */
1583 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1584 if (dom)
1585 set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
1587 /* Generate values for PHI nodes. */
1588 for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
1589 add_to_sets (PHI_RESULT (phi), PHI_RESULT (phi), NULL,
1590 PHI_GEN (block), AVAIL_OUT (block));
1592 /* Now compute value numbers and populate value sets with all
1593 the expressions computed in BLOCK. */
1594 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
1596 stmt_ann_t ann;
1597 size_t j;
1599 stmt = bsi_stmt (bsi);
1600 ann = stmt_ann (stmt);
1601 get_stmt_operands (stmt);
1603 /* We are only interested in assignments of the form
1604 X_i = EXPR, where EXPR represents an "interesting"
1605 computation, it has no volatile operands and X_i
1606 doesn't flow through an abnormal edge. */
1607 if (TREE_CODE (stmt) == MODIFY_EXPR
1608 && !ann->has_volatile_ops
1609 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
1610 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (stmt, 0)))
1612 tree lhs = TREE_OPERAND (stmt, 0);
1613 tree rhs = TREE_OPERAND (stmt, 1);
1614 vuse_optype vuses = STMT_VUSE_OPS (stmt);
1616 STRIP_USELESS_TYPE_CONVERSION (rhs);
1618 if (TREE_CODE_CLASS (TREE_CODE (rhs)) == '1'
1619 || TREE_CODE_CLASS (TREE_CODE (rhs)) == '2')
1621 /* For binary and unary expressions, create a duplicate
1622 expression with the operands replaced with the value
1623 handles of the original RHS. */
1624 tree newt = create_value_expr_from (rhs, block, vuses);
1625 add_to_sets (lhs, newt, vuses, TMP_GEN (block),
1626 AVAIL_OUT (block));
1627 value_insert_into_set (EXP_GEN (block), newt);
1628 continue;
1630 else if (TREE_CODE (rhs) == SSA_NAME
1631 || is_gimple_min_invariant (rhs))
1633 /* Compute a value number for the RHS of the statement
1634 and add its value to the AVAIL_OUT set for the block.
1635 Add the LHS to TMP_GEN. */
1636 add_to_sets (lhs, rhs, vuses, TMP_GEN (block),
1637 AVAIL_OUT (block));
1639 if (TREE_CODE (rhs) == SSA_NAME
1640 && !is_undefined_value (rhs))
1641 value_insert_into_set (EXP_GEN (block), rhs);
1642 continue;
1646 /* For any other statement that we don't recognize, simply
1647 make the names generated by the statement available in
1648 AVAIL_OUT and TMP_GEN. */
1649 for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1651 tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1652 add_to_sets (def, def, NULL, TMP_GEN (block),
1653 AVAIL_OUT (block));
1656 for (j = 0; j < NUM_USES (STMT_USE_OPS (stmt)); j++)
1658 tree use = USE_OP (STMT_USE_OPS (stmt), j);
1659 add_to_sets (use, use, NULL, TMP_GEN (block),
1660 AVAIL_OUT (block));
1665 /* Compute available sets for the dominator children of BLOCK. */
1666 for (son = first_dom_son (CDI_DOMINATORS, block);
1667 son;
1668 son = next_dom_son (CDI_DOMINATORS, son))
1669 compute_avail (son);
1673 /* Eliminate fully redundant computations. */
1675 static void
1676 eliminate (void)
1678 basic_block b;
1680 FOR_EACH_BB (b)
1682 block_stmt_iterator i;
1684 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
1686 tree stmt = bsi_stmt (i);
1688 /* Lookup the RHS of the expression, see if we have an
1689 available computation for it. If so, replace the RHS with
1690 the available computation. */
1691 if (TREE_CODE (stmt) == MODIFY_EXPR
1692 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
1693 && TREE_CODE (TREE_OPERAND (stmt ,1)) != SSA_NAME
1694 && !is_gimple_min_invariant (TREE_OPERAND (stmt, 1))
1695 && !stmt_ann (stmt)->has_volatile_ops)
1697 tree lhs = TREE_OPERAND (stmt, 0);
1698 tree *rhs_p = &TREE_OPERAND (stmt, 1);
1699 tree sprime;
1700 vuse_optype vuses = STMT_VUSE_OPS (stmt);
1702 sprime = find_leader (AVAIL_OUT (b), vn_lookup (lhs, vuses));
1703 if (sprime
1704 && sprime != lhs
1705 && (TREE_CODE (*rhs_p) != SSA_NAME
1706 || may_propagate_copy (*rhs_p, sprime)))
1708 if (sprime == *rhs_p)
1709 abort ();
1711 if (dump_file && (dump_flags & TDF_DETAILS))
1713 fprintf (dump_file, "Replaced ");
1714 print_generic_expr (dump_file, *rhs_p, 0);
1715 fprintf (dump_file, " with ");
1716 print_generic_expr (dump_file, sprime, 0);
1717 fprintf (dump_file, " in ");
1718 print_generic_stmt (dump_file, stmt, 0);
1720 pre_stats.eliminations++;
1721 propagate_tree_value (rhs_p, sprime);
1722 modify_stmt (stmt);
1730 /* Initialize data structures used by PRE. */
1732 static void
1733 init_pre (void)
1735 size_t tsize;
1736 basic_block bb;
1738 vn_init ();
1739 memset (&pre_stats, 0, sizeof (pre_stats));
1740 FOR_ALL_BB (bb)
1741 bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
1743 phi_translate_table = htab_create (511, expr_pred_trans_hash,
1744 expr_pred_trans_eq, free);
1745 value_set_pool = create_alloc_pool ("Value sets",
1746 sizeof (struct value_set), 30);
1747 value_set_node_pool = create_alloc_pool ("Value set nodes",
1748 sizeof (struct value_set_node), 30);
1749 calculate_dominance_info (CDI_POST_DOMINATORS);
1750 calculate_dominance_info (CDI_DOMINATORS);
1751 tsize = tree_size (build (PLUS_EXPR, void_type_node, NULL_TREE, NULL_TREE));
1752 binary_node_pool = create_alloc_pool ("Binary tree nodes", tsize, 30);
1753 tsize = tree_size (build1 (NEGATE_EXPR, void_type_node, NULL_TREE));
1754 unary_node_pool = create_alloc_pool ("Unary tree nodes", tsize, 30);
1756 FOR_ALL_BB (bb)
1758 EXP_GEN (bb) = set_new (true);
1759 PHI_GEN (bb) = set_new (true);
1760 TMP_GEN (bb) = set_new (false);
1761 AVAIL_OUT (bb) = set_new (true);
1766 /* Deallocate data structures used by PRE. */
1768 static void
1769 fini_pre (void)
1771 basic_block bb;
1773 free_alloc_pool (value_set_pool);
1774 free_alloc_pool (value_set_node_pool);
1775 free_alloc_pool (binary_node_pool);
1776 free_alloc_pool (unary_node_pool);
1777 htab_delete (phi_translate_table);
1779 FOR_ALL_BB (bb)
1781 free (bb->aux);
1782 bb->aux = NULL;
1784 free_dominance_info (CDI_POST_DOMINATORS);
1785 vn_delete ();
1789 /* Main entry point to the SSA-PRE pass. DO_FRE is true if the caller
1790 only wants to do full redundancy elimination. */
1792 static void
1793 execute_pre (bool do_fre)
1795 init_pre ();
1797 /* Collect and value number expressions computed in each basic
1798 block. */
1799 compute_avail (ENTRY_BLOCK_PTR);
1801 if (dump_file && (dump_flags & TDF_DETAILS))
1803 basic_block bb;
1805 FOR_ALL_BB (bb)
1807 print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
1808 print_value_set (dump_file, TMP_GEN (bb), "tmp_gen", bb->index);
1809 print_value_set (dump_file, AVAIL_OUT (bb), "avail_out", bb->index);
1813 /* Insert can get quite slow on an incredibly large number of basic
1814 blocks due to some quadratic behavior. Until this behavior is
1815 fixed, don't run it when he have an incredibly large number of
1816 bb's. If we aren't going to run insert, there is no point in
1817 computing ANTIC, either, even though it's plenty fast. */
1818 if (!do_fre && n_basic_blocks < 4000)
1820 compute_antic ();
1821 insert ();
1824 /* Remove all the redundant expressions. */
1825 eliminate ();
1827 if (dump_file && (dump_flags & TDF_STATS))
1829 fprintf (dump_file, "Insertions:%d\n", pre_stats.insertions);
1830 fprintf (dump_file, "New PHIs:%d\n", pre_stats.phis);
1831 fprintf (dump_file, "Eliminated:%d\n", pre_stats.eliminations);
1834 fini_pre ();
1838 /* Gate and execute functions for PRE. */
1840 static void
1841 do_pre (void)
1843 execute_pre (false);
1846 static bool
1847 gate_pre (void)
1849 return flag_tree_pre != 0;
1852 struct tree_opt_pass pass_pre =
1854 "pre", /* name */
1855 gate_pre, /* gate */
1856 do_pre, /* execute */
1857 NULL, /* sub */
1858 NULL, /* next */
1859 0, /* static_pass_number */
1860 TV_TREE_PRE, /* tv_id */
1861 PROP_no_crit_edges | PROP_cfg | PROP_ssa,/* properties_required */
1862 0, /* properties_provided */
1863 0, /* properties_destroyed */
1864 0, /* todo_flags_start */
1865 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
1869 /* Gate and execute functions for FRE. */
1871 static void
1872 do_fre (void)
1874 execute_pre (true);
1877 static bool
1878 gate_fre (void)
1880 return flag_tree_fre != 0;
1883 struct tree_opt_pass pass_fre =
1885 "fre", /* name */
1886 gate_fre, /* gate */
1887 do_fre, /* execute */
1888 NULL, /* sub */
1889 NULL, /* next */
1890 0, /* static_pass_number */
1891 TV_TREE_FRE, /* tv_id */
1892 PROP_no_crit_edges | PROP_cfg | PROP_ssa,/* properties_required */
1893 0, /* properties_provided */
1894 0, /* properties_destroyed */
1895 0, /* todo_flags_start */
1896 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */