tree-ssa-pre.c (phi_trans_add): Use is_gimple_min_invariant to check for constants.
[gcc.git] / gcc / tree-ssa-pre.c
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>
5
6 This file is part of GCC.
7
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.
12
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.
17
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. */
22
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"
47
48 /* TODO:
49
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 */
70
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. */
75
76 /* Basic algorithm
77
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.
88
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.
102
103 Third, we perform insertions to make partially redundant
104 expressions fully redundant.
105
106 An expression is partially redundant (excluding partial
107 anticipation) if:
108
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.
112
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.
116
117 Fourth, we eliminate fully redundant expressions.
118 This is a simple statement walk that replaces redundant
119 calculations with the now available values. */
120
121 /* Representations of value numbers:
122
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.
129
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.
136
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. */
142
143 /* Representation of expressions on value numbers:
144
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.
150
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.
157
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).*/
165
166
167 /* Representation of sets:
168
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.
174
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. */
181
182
183 /* A value set element. Basically a single linked list of
184 expressions/values. */
185 typedef struct value_set_node
186 {
187 /* An expression. */
188 tree expr;
189
190 /* A pointer to the next element of the value set. */
191 struct value_set_node *next;
192 } *value_set_node_t;
193
194
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
199 {
200 /* The head of the list. Used for iterating over the list in
201 order. */
202 value_set_node_t head;
203
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;
208
209 /* The length of the list. */
210 size_t length;
211
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;
216
217 /* The bitmap of values that exist in the set. May be NULL in an
218 empty or non-indexed set. */
219 bitmap values;
220
221 } *value_set_t;
222
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. */
226
227 typedef struct bb_value_sets
228 {
229 /* The EXP_GEN set, which represents expressions/values generated in
230 a basic block. */
231 value_set_t exp_gen;
232
233 /* The PHI_GEN set, which represents PHI results generated in a
234 basic block. */
235 value_set_t phi_gen;
236
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;
240
241 /* The AVAIL_OUT set, which represents which values are available in
242 a given basic block. */
243 value_set_t avail_out;
244
245 /* The ANTIC_IN set, which represents which values are anticiptable
246 in a given basic block. */
247 value_set_t antic_in;
248
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;
254
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
261
262 /* This structure is used to keep track of statistics on what
263 optimization PRE was able to perform. */
264 static struct
265 {
266 /* The number of RHS computations eliminated by PRE. */
267 int eliminations;
268
269 /* The number of new expressions/temporaries generated by PRE. */
270 int insertions;
271
272 /* The number of new PHI nodes added by PRE. */
273 int phis;
274 } pre_stats;
275
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);
282
283 /* We can add and remove elements and entries to and from sets
284 and hash tables, so we use alloc pools for them. */
285
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;
290
291
292 /* The phi_translate_table caches phi translations for a given
293 expression and predecessor. */
294
295 static htab_t phi_translate_table;
296
297 /* A three tuple {e, pred, v} used to cache phi translations in the
298 phi_translate_table. */
299
300 typedef struct expr_pred_trans_d
301 {
302 /* The expression. */
303 tree e;
304
305 /* The predecessor block along which we translated the expression. */
306 basic_block pred;
307
308 /* The value that resulted from the translation. */
309 tree v;
310
311 /* The hashcode for the expression, pred pair. This is cached for
312 speed reasons. */
313 hashval_t hashcode;
314 } *expr_pred_trans_t;
315
316 /* Return the hash value for a phi translation table entry. */
317
318 static hashval_t
319 expr_pred_trans_hash (const void *p)
320 {
321 const expr_pred_trans_t ve = (expr_pred_trans_t) p;
322 return ve->hashcode;
323 }
324
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.*/
327
328 static int
329 expr_pred_trans_eq (const void *p1, const void *p2)
330 {
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;
335
336
337 /* If they are not translations for the same basic block, they can't
338 be equal. */
339 if (b1 != b2)
340 return false;
341
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;
346
347 return false;
348 }
349
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. */
353
354 static inline tree
355 phi_trans_lookup (tree e, basic_block pred)
356 {
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;
368 }
369
370
371 /* Add the tuple mapping from {expression E, basic block PRED} to
372 value V, to the phi translation table. */
373
374 static inline void
375 phi_trans_add (tree e, tree v, basic_block pred)
376 {
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;
388 }
389
390
391 /* Add expression E to the expression set of value V. */
392
393 void
394 add_to_value (tree v, tree e)
395 {
396 /* Constants have no expression sets. */
397 if (is_gimple_min_invariant (v))
398 return;
399
400 if (VALUE_HANDLE_EXPR_SET (v) == NULL)
401 VALUE_HANDLE_EXPR_SET (v) = set_new (false);
402
403 insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
404 }
405
406
407 /* Return true if value V exists in the bitmap for SET. */
408
409 static inline bool
410 value_exists_in_set_bitmap (value_set_t set, tree v)
411 {
412 if (!set->values)
413 return false;
414
415 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (v));
416 }
417
418
419 /* Remove value V from the bitmap for SET. */
420
421 static void
422 value_remove_from_set_bitmap (value_set_t set, tree v)
423 {
424 #ifdef ENABLE_CHECKING
425 if (!set->indexed)
426 abort ();
427 #endif
428
429 if (!set->values)
430 return;
431
432 bitmap_clear_bit (set->values, VALUE_HANDLE_ID (v));
433 }
434
435
436 /* Insert the value number V into the bitmap of values existing in
437 SET. */
438
439 static inline void
440 value_insert_into_set_bitmap (value_set_t set, tree v)
441 {
442 #ifdef ENABLE_CHECKING
443 if (!set->indexed)
444 abort ();
445 #endif
446
447 if (set->values == NULL)
448 {
449 set->values = BITMAP_GGC_ALLOC ();
450 bitmap_clear (set->values);
451 }
452
453 bitmap_set_bit (set->values, VALUE_HANDLE_ID (v));
454 }
455
456
457 /* Create a new set. */
458
459 static value_set_t
460 set_new (bool indexed)
461 {
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;
469 }
470
471
472 /* Insert EXPR into SET. */
473
474 static void
475 insert_into_set (value_set_t set, tree expr)
476 {
477 value_set_node_t newnode = pool_alloc (value_set_node_pool);
478 tree val = get_value_handle (expr);
479
480 if (val == NULL)
481 abort ();
482
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);
488
489 newnode->next = NULL;
490 newnode->expr = expr;
491 set->length ++;
492 if (set->head == NULL)
493 {
494 set->head = set->tail = newnode;
495 }
496 else
497 {
498 set->tail->next = newnode;
499 set->tail = newnode;
500 }
501 }
502
503 /* Copy the set ORIG to the set DEST. */
504
505 static void
506 set_copy (value_set_t dest, value_set_t orig)
507 {
508 value_set_node_t node;
509
510 if (!orig || !orig->head)
511 return;
512
513 for (node = orig->head;
514 node;
515 node = node->next)
516 {
517 insert_into_set (dest, node->expr);
518 }
519 }
520
521 /* Remove EXPR from SET. */
522
523 static void
524 set_remove (value_set_t set, tree expr)
525 {
526 value_set_node_t node, prev;
527
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)
536 {
537 if (node->expr == expr)
538 {
539 if (prev == NULL)
540 set->head = node->next;
541 else
542 prev->next= node->next;
543
544 if (node == set->tail)
545 set->tail = prev;
546 pool_free (value_set_node_pool, node);
547 return;
548 }
549 }
550 }
551
552 /* Return true if SET contains the value VAL. */
553
554 static bool
555 set_contains_value (value_set_t set, tree val)
556 {
557 /* All constants are in every set. */
558 if (is_gimple_min_invariant (val))
559 return true;
560
561 if (set->length == 0)
562 return false;
563
564 return value_exists_in_set_bitmap (set, val);
565 }
566
567 /* Replace the leader for the value LOOKFOR in SET with EXPR. */
568
569 static void
570 set_replace_value (value_set_t set, tree lookfor, tree expr)
571 {
572 value_set_node_t node = set->head;
573
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;
578
579 for (node = set->head;
580 node;
581 node = node->next)
582 {
583 if (get_value_handle (node->expr) == lookfor)
584 {
585 node->expr = expr;
586 return;
587 }
588 }
589 }
590
591 /* Return true if the set contains expression (not value) EXPR. */
592
593 static bool
594 set_contains (value_set_t set, tree expr)
595 {
596 value_set_node_t node;
597
598 for (node = set->head;
599 node;
600 node = node->next)
601 {
602 if (operand_equal_p (node->expr, expr, 0))
603 return true;
604 }
605 return false;
606 }
607
608 /* Subtract set B from set A, and return the new set. */
609
610 static value_set_t
611 set_subtract (value_set_t a, value_set_t b, bool indexed)
612 {
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)
618 {
619 if (!set_contains (b, node->expr))
620 insert_into_set (ret, node->expr);
621 }
622 return ret;
623 }
624
625 /* Return true if two sets are equal. */
626
627 static bool
628 set_equal (value_set_t a, value_set_t b)
629 {
630 value_set_node_t node;
631
632 if (a->length != b->length)
633 return false;
634 for (node = a->head;
635 node;
636 node = node->next)
637 {
638 if (!set_contains_value (b, get_value_handle (node->expr)))
639 return false;
640 }
641 return true;
642 }
643
644 /* Replace the value for EXPR in SET with EXPR. */
645 static void
646 value_replace_in_set (value_set_t set, tree expr)
647 {
648 tree val = get_value_handle (expr);
649
650 if (set->length == 0)
651 return;
652
653 set_replace_value (set, val, expr);
654 }
655
656 /* Insert the value for EXPR into SET, if it doesn't exist already. */
657
658 static void
659 value_insert_into_set (value_set_t set, tree expr)
660 {
661 tree val = get_value_handle (expr);
662
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;
667
668 if (!set_contains_value (set, val))
669 insert_into_set (set, expr);
670 }
671
672
673 /* Print out the value_set SET to OUTFILE. */
674
675 static void
676 print_value_set (FILE *outfile, value_set_t set,
677 const char *setname, int blockindex)
678 {
679 value_set_node_t node;
680 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
681 if (set)
682 {
683 for (node = set->head;
684 node;
685 node = node->next)
686 {
687 print_generic_expr (outfile, node->expr, 0);
688
689 fprintf (outfile, " (");
690 print_generic_expr (outfile, get_value_handle (node->expr), 0);
691 fprintf (outfile, ") ");
692
693 if (node->next)
694 fprintf (outfile, ", ");
695 }
696 }
697
698 fprintf (outfile, " }\n");
699 }
700
701 /* Print out the expressions that have VAL to OUTFILE. */
702
703 void
704 print_value_expressions (FILE *outfile, tree val)
705 {
706 if (VALUE_HANDLE_EXPR_SET (val))
707 {
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);
711 }
712 }
713
714
715 void
716 debug_value_expressions (tree val)
717 {
718 print_value_expressions (stderr, val);
719 }
720
721
722 void debug_value_set (value_set_t, const char *, int);
723
724 void
725 debug_value_set (value_set_t set, const char *setname, int blockindex)
726 {
727 print_value_set (stderr, set, setname, blockindex);
728 }
729
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. */
733
734 static tree
735 phi_translate (tree expr, value_set_t set, basic_block pred,
736 basic_block phiblock)
737 {
738 tree phitrans = NULL;
739 tree oldexpr = expr;
740
741 if (expr == NULL)
742 return NULL;
743
744 /* Phi translations of a given expression don't change, */
745 phitrans = phi_trans_lookup (expr, pred);
746 if (phitrans)
747 return phitrans;
748
749
750 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
751 {
752 case '2':
753 {
754 tree oldop1 = TREE_OPERAND (expr, 0);
755 tree oldop2 = TREE_OPERAND (expr, 1);
756 tree newop1;
757 tree newop2;
758 tree newexpr;
759
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)
769 {
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);
778 }
779 }
780 break;
781 /* XXX: Until we have PRE of loads working, none will be ANTIC.
782 */
783 case 'r':
784 return NULL;
785 break;
786 case '1':
787 {
788 tree oldop1 = TREE_OPERAND (expr, 0);
789 tree newop1;
790 tree newexpr;
791
792 newop1 = phi_translate (find_leader (set, oldop1),
793 set, pred, phiblock);
794 if (newop1 == NULL)
795 return NULL;
796 if (newop1 != oldop1)
797 {
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);
805 }
806 }
807 break;
808 case 'd':
809 abort ();
810 case 'x':
811 {
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;
820
821 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
822 if (PHI_ARG_EDGE (phi, i)->src == pred)
823 {
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);
829 }
830 }
831 break;
832 }
833 return expr;
834 }
835
836 static void
837 phi_translate_set (value_set_t dest, value_set_t set, basic_block pred,
838 basic_block phiblock)
839 {
840 value_set_node_t node;
841 for (node = set->head;
842 node;
843 node = node->next)
844 {
845 tree translated;
846 translated = phi_translate (node->expr, set, pred, phiblock);
847 phi_trans_add (node->expr, translated, pred);
848
849 if (translated != NULL)
850 value_insert_into_set (dest, translated);
851 }
852 }
853
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. */
857
858 static tree
859 find_leader (value_set_t set, tree val)
860 {
861 value_set_node_t node;
862
863 if (val == NULL)
864 return NULL;
865
866 /* Constants represent themselves. */
867 if (is_gimple_min_invariant (val))
868 return val;
869
870 if (set->length == 0)
871 return NULL;
872
873 if (value_exists_in_set_bitmap (set, val))
874 {
875 for (node = set->head;
876 node;
877 node = node->next)
878 {
879 if (get_value_handle (node->expr) == val)
880 return node->expr;
881 }
882 }
883
884 return NULL;
885 }
886
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.
890
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) */
895
896 static bool
897 valid_in_set (value_set_t set, tree expr)
898 {
899 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
900 {
901 case '2':
902 {
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);
906 }
907 break;
908 case '1':
909 {
910 tree op1 = TREE_OPERAND (expr, 0);
911 return set_contains_value (set, op1);
912 }
913 break;
914 /* XXX: Until PRE of loads works, no reference nodes are ANTIC.
915 */
916 case 'r':
917 {
918 return false;
919 }
920 case 'x':
921 {
922 if (TREE_CODE (expr) == SSA_NAME)
923 return true;
924 abort ();
925 }
926 case 'c':
927 abort ();
928 }
929 return false;
930 }
931
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. */
935
936 static void
937 clean (value_set_t set)
938 {
939 value_set_node_t node;
940 value_set_node_t next;
941 node = set->head;
942 while (node)
943 {
944 next = node->next;
945 if (!valid_in_set (set, node->expr))
946 set_remove (set, node->expr);
947 node = next;
948 }
949 }
950
951 /* Compute the ANTIC set for BLOCK.
952
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
957
958 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] -
959 TMP_GEN[BLOCK])
960
961 Iterate until fixpointed.
962
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. */
967
968
969 static bool
970 compute_antic_aux (basic_block block)
971 {
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;
977
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))
983 {
984 for (e = block->pred; e; e = e->pred_next)
985 if (e->flags & EDGE_ABNORMAL)
986 {
987 block->flags |= BB_VISITED;
988 break;
989 }
990 }
991 if (block->flags & BB_VISITED)
992 {
993 S = NULL;
994 goto visit_sons;
995 }
996
997
998 old = set_new (false);
999 set_copy (old, ANTIC_IN (block));
1000 ANTIC_OUT = set_new (true);
1001
1002 /* If the block has no successors, ANTIC_OUT is empty, because it is
1003 the exit block. */
1004 if (block->succ == NULL);
1005
1006 /* If we have one successor, we could have some phi nodes to
1007 translate through. */
1008 else if (block->succ->succ_next == NULL)
1009 {
1010 phi_translate_set (ANTIC_OUT, ANTIC_IN(block->succ->dest),
1011 block, block->succ->dest);
1012 }
1013 /* If we have multiple successors, we take the intersection of all of
1014 them. */
1015 else
1016 {
1017 varray_type worklist;
1018 edge e;
1019 size_t i;
1020 basic_block bprime, first;
1021
1022 VARRAY_BB_INIT (worklist, 1, "succ");
1023 e = block->succ;
1024 while (e)
1025 {
1026 VARRAY_PUSH_BB (worklist, e->dest);
1027 e = e->succ_next;
1028 }
1029 first = VARRAY_BB (worklist, 0);
1030 set_copy (ANTIC_OUT, ANTIC_IN (first));
1031
1032 for (i = 1; i < VARRAY_ACTIVE_SIZE (worklist); i++)
1033 {
1034 bprime = VARRAY_BB (worklist, i);
1035 node = ANTIC_OUT->head;
1036 while (node)
1037 {
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;
1044 }
1045 }
1046 VARRAY_CLEAR (worklist);
1047 }
1048
1049 /* Generate ANTIC_OUT - TMP_GEN */
1050 S = set_subtract (ANTIC_OUT, TMP_GEN (block), false);
1051
1052 /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1053 ANTIC_IN (block) = set_subtract (EXP_GEN (block),TMP_GEN (block), true);
1054
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)
1060 {
1061 value_insert_into_set (ANTIC_IN (block), node->expr);
1062 }
1063 clean (ANTIC_IN (block));
1064
1065
1066 if (!set_equal (old, ANTIC_IN (block)))
1067 changed = true;
1068
1069 visit_sons:
1070 if (dump_file && (dump_flags & TDF_DETAILS))
1071 {
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);
1077
1078 }
1079
1080 for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1081 son;
1082 son = next_dom_son (CDI_POST_DOMINATORS, son))
1083 {
1084 changed |= compute_antic_aux (son);
1085 }
1086 return changed;
1087 }
1088
1089 /* Compute ANTIC sets. */
1090
1091 static void
1092 compute_antic (void)
1093 {
1094 bool changed = true;
1095 basic_block bb;
1096 int num_iterations = 0;
1097 FOR_ALL_BB (bb)
1098 {
1099 ANTIC_IN (bb) = set_new (true);
1100 if (bb->flags & BB_VISITED)
1101 abort ();
1102 }
1103
1104 while (changed)
1105 {
1106 num_iterations++;
1107 changed = false;
1108 changed = compute_antic_aux (EXIT_BLOCK_PTR);
1109 }
1110 FOR_ALL_BB (bb)
1111 {
1112 bb->flags &= ~BB_VISITED;
1113 }
1114 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1115 fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1116 }
1117
1118
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. */
1127
1128 static tree
1129 find_or_generate_expression (basic_block block, tree expr, tree stmts)
1130 {
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)
1144 {
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);
1150 }
1151 return genop;
1152 }
1153
1154
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.
1160
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). */
1168
1169 static tree
1170 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
1171 {
1172 tree name = NULL_TREE;
1173 tree newexpr = NULL_TREE;
1174 tree v;
1175
1176 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1177 {
1178 case '2':
1179 {
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;
1199 }
1200 case '1':
1201 {
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++;
1218
1219 break;
1220 }
1221 default:
1222 abort ();
1223
1224 }
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))
1230 {
1231 fprintf (dump_file, "Inserted ");
1232 print_generic_expr (dump_file, newexpr, 0);
1233 fprintf (dump_file, " in predecessor %d\n", block->index);
1234 }
1235 return name;
1236 }
1237
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.
1250
1251 */
1252 static bool
1253 insert_aux (basic_block block)
1254 {
1255 basic_block son;
1256 bool new_stuff = false;
1257
1258 if (block)
1259 {
1260 value_set_node_t e;
1261 basic_block dom;
1262 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1263 if (dom)
1264 {
1265 e = NEW_SETS (dom)->head;
1266 while (e)
1267 {
1268 insert_into_set (NEW_SETS (block), e->expr);
1269 value_replace_in_set (AVAIL_OUT (block), e->expr);
1270 e = e->next;
1271 }
1272 if (block->pred->pred_next)
1273 {
1274 value_set_node_t node;
1275 for (node = ANTIC_IN (block)->head;
1276 node;
1277 node = node->next)
1278 {
1279 if (TREE_CODE_CLASS (TREE_CODE (node->expr)) == '2'
1280 || TREE_CODE_CLASS (TREE_CODE (node->expr)) == '1')
1281 {
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;
1291
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))
1296 {
1297 if (dump_file && (dump_flags & TDF_DETAILS))
1298 fprintf (dump_file, "Found fully redundant value\n");
1299 continue;
1300 }
1301
1302 avail = xcalloc (last_basic_block, sizeof (tree));
1303 for (pred = block->pred;
1304 pred;
1305 pred = pred->pred_next)
1306 {
1307 tree vprime;
1308 tree edoubleprime;
1309 bprime = pred->src;
1310 eprime = phi_translate (node->expr,
1311 ANTIC_IN (block),
1312 bprime, block);
1313
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)
1324 {
1325 cant_insert = true;
1326 break;
1327 }
1328
1329 vprime = get_value_handle (eprime);
1330 if (!vprime)
1331 abort ();
1332 edoubleprime = find_leader (AVAIL_OUT (bprime),
1333 vprime);
1334 if (edoubleprime == NULL)
1335 {
1336 avail[bprime->index] = eprime;
1337 all_same = false;
1338 }
1339 else
1340 {
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 ();
1350 }
1351 }
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)
1357 {
1358 tree type = TREE_TYPE (avail[block->pred->src->index]);
1359 tree temp;
1360 if (dump_file && (dump_flags & TDF_DETAILS))
1361 {
1362 fprintf (dump_file, "Found partial redundancy for expression ");
1363 print_generic_expr (dump_file, node->expr, 0);
1364 fprintf (dump_file, "\n");
1365 }
1366
1367 /* Make the necessary insertions. */
1368 for (pred = block->pred;
1369 pred;
1370 pred = pred->pred_next)
1371 {
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')
1378 {
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;
1385 }
1386 }
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);
1392
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)
1404 {
1405 add_phi_arg (&temp, avail[pred->src->index],
1406 pred);
1407 }
1408 if (dump_file && (dump_flags & TDF_DETAILS))
1409 {
1410 fprintf (dump_file, "Created phi ");
1411 print_generic_expr (dump_file, temp, 0);
1412 fprintf (dump_file, " in block %d\n", block->index);
1413 }
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));
1420 }
1421
1422 free (avail);
1423 }
1424 }
1425 }
1426 }
1427 }
1428 for (son = first_dom_son (CDI_DOMINATORS, block);
1429 son;
1430 son = next_dom_son (CDI_DOMINATORS, son))
1431 {
1432 new_stuff |= insert_aux (son);
1433 }
1434
1435 return new_stuff;
1436 }
1437
1438 /* Perform insertion of partially redundant values. */
1439
1440 static void
1441 insert (void)
1442 {
1443 bool new_stuff = true;
1444 basic_block bb;
1445 int num_iterations = 0;
1446
1447 FOR_ALL_BB (bb)
1448 NEW_SETS (bb) = set_new (true);
1449
1450 while (new_stuff)
1451 {
1452 num_iterations++;
1453 new_stuff = false;
1454 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1455 }
1456 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1457 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1458 }
1459
1460
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. */
1463
1464 static bool
1465 is_undefined_value (tree expr)
1466 {
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)));
1472 }
1473
1474
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.
1479
1480 VUSES represent the virtual use operands associated with EXPR (if
1481 any). They are used when computing the hash value for EXPR. */
1482
1483 static inline void
1484 add_to_sets (tree var, tree expr, vuse_optype vuses, value_set_t s1,
1485 value_set_t s2)
1486 {
1487 tree val = vn_lookup_or_add (expr, vuses);
1488
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);
1495
1496 insert_into_set (s1, var);
1497 value_insert_into_set (s2, var);
1498 }
1499
1500
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.
1505
1506 VUSES represent the virtual use operands associated with EXPR (if
1507 any). They are used when computing the hash value for EXPR. */
1508
1509 static inline tree
1510 create_value_expr_from (tree expr, basic_block block, vuse_optype vuses)
1511 {
1512 int i;
1513 enum tree_code code = TREE_CODE (expr);
1514 tree vexpr;
1515
1516 #if defined ENABLE_CHECKING
1517 if (TREE_CODE_CLASS (code) != '1'
1518 && TREE_CODE_CLASS (code) != '2')
1519 abort ();
1520 #endif
1521
1522 if (TREE_CODE_CLASS (code) == '1')
1523 vexpr = pool_alloc (unary_node_pool);
1524 else
1525 vexpr = pool_alloc (binary_node_pool);
1526
1527 memcpy (vexpr, expr, tree_size (expr));
1528
1529 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
1530 {
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;
1536 }
1537
1538 return vexpr;
1539 }
1540
1541
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.
1547
1548 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
1549 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
1550
1551 static void
1552 compute_avail (basic_block block)
1553 {
1554 basic_block son;
1555
1556 /* For arguments with default definitions, we pretend they are
1557 defined in the entry block. */
1558 if (block == ENTRY_BLOCK_PTR)
1559 {
1560 tree param;
1561 for (param = DECL_ARGUMENTS (current_function_decl);
1562 param;
1563 param = TREE_CHAIN (param))
1564 {
1565 if (default_def (param) != NULL)
1566 {
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);
1572 }
1573 }
1574 }
1575 else if (block)
1576 {
1577 block_stmt_iterator bsi;
1578 tree stmt, phi;
1579 basic_block dom;
1580
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));
1586
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));
1591
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))
1595 {
1596 stmt_ann_t ann;
1597 size_t j;
1598
1599 stmt = bsi_stmt (bsi);
1600 ann = stmt_ann (stmt);
1601 get_stmt_operands (stmt);
1602
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)))
1611 {
1612 tree lhs = TREE_OPERAND (stmt, 0);
1613 tree rhs = TREE_OPERAND (stmt, 1);
1614 vuse_optype vuses = STMT_VUSE_OPS (stmt);
1615
1616 STRIP_USELESS_TYPE_CONVERSION (rhs);
1617
1618 if (TREE_CODE_CLASS (TREE_CODE (rhs)) == '1'
1619 || TREE_CODE_CLASS (TREE_CODE (rhs)) == '2')
1620 {
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;
1629 }
1630 else if (TREE_CODE (rhs) == SSA_NAME
1631 || is_gimple_min_invariant (rhs))
1632 {
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));
1638
1639 if (TREE_CODE (rhs) == SSA_NAME
1640 && !is_undefined_value (rhs))
1641 value_insert_into_set (EXP_GEN (block), rhs);
1642 continue;
1643 }
1644 }
1645
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++)
1650 {
1651 tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1652 add_to_sets (def, def, NULL, TMP_GEN (block),
1653 AVAIL_OUT (block));
1654 }
1655
1656 for (j = 0; j < NUM_USES (STMT_USE_OPS (stmt)); j++)
1657 {
1658 tree use = USE_OP (STMT_USE_OPS (stmt), j);
1659 add_to_sets (use, use, NULL, TMP_GEN (block),
1660 AVAIL_OUT (block));
1661 }
1662 }
1663 }
1664
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);
1670 }
1671
1672
1673 /* Eliminate fully redundant computations. */
1674
1675 static void
1676 eliminate (void)
1677 {
1678 basic_block b;
1679
1680 FOR_EACH_BB (b)
1681 {
1682 block_stmt_iterator i;
1683
1684 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
1685 {
1686 tree stmt = bsi_stmt (i);
1687
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)
1696 {
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);
1701
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)))
1707 {
1708 if (sprime == *rhs_p)
1709 abort ();
1710
1711 if (dump_file && (dump_flags & TDF_DETAILS))
1712 {
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);
1719 }
1720 pre_stats.eliminations++;
1721 propagate_tree_value (rhs_p, sprime);
1722 modify_stmt (stmt);
1723 }
1724 }
1725 }
1726 }
1727 }
1728
1729
1730 /* Initialize data structures used by PRE. */
1731
1732 static void
1733 init_pre (void)
1734 {
1735 size_t tsize;
1736 basic_block bb;
1737
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));
1742
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);
1755
1756 FOR_ALL_BB (bb)
1757 {
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);
1762 }
1763 }
1764
1765
1766 /* Deallocate data structures used by PRE. */
1767
1768 static void
1769 fini_pre (void)
1770 {
1771 basic_block bb;
1772
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);
1778
1779 FOR_ALL_BB (bb)
1780 {
1781 free (bb->aux);
1782 bb->aux = NULL;
1783 }
1784 free_dominance_info (CDI_POST_DOMINATORS);
1785 vn_delete ();
1786 }
1787
1788
1789 /* Main entry point to the SSA-PRE pass. DO_FRE is true if the caller
1790 only wants to do full redundancy elimination. */
1791
1792 static void
1793 execute_pre (bool do_fre)
1794 {
1795 init_pre ();
1796
1797 /* Collect and value number expressions computed in each basic
1798 block. */
1799 compute_avail (ENTRY_BLOCK_PTR);
1800
1801 if (dump_file && (dump_flags & TDF_DETAILS))
1802 {
1803 basic_block bb;
1804
1805 FOR_ALL_BB (bb)
1806 {
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);
1810 }
1811 }
1812
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)
1819 {
1820 compute_antic ();
1821 insert ();
1822 }
1823
1824 /* Remove all the redundant expressions. */
1825 eliminate ();
1826
1827 if (dump_file && (dump_flags & TDF_STATS))
1828 {
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);
1832 }
1833
1834 fini_pre ();
1835 }
1836
1837
1838 /* Gate and execute functions for PRE. */
1839
1840 static void
1841 do_pre (void)
1842 {
1843 execute_pre (false);
1844 }
1845
1846 static bool
1847 gate_pre (void)
1848 {
1849 return flag_tree_pre != 0;
1850 }
1851
1852 struct tree_opt_pass pass_pre =
1853 {
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 */
1866 };
1867
1868
1869 /* Gate and execute functions for FRE. */
1870
1871 static void
1872 do_fre (void)
1873 {
1874 execute_pre (true);
1875 }
1876
1877 static bool
1878 gate_fre (void)
1879 {
1880 return flag_tree_fre != 0;
1881 }
1882
1883 struct tree_opt_pass pass_fre =
1884 {
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 */
1897 };