tree-optimize.c (init_tree_optimization_passes): Schedule pass_may_alias right after...
[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. Avail sets can be shared by making an avail_find_leader that
51 walks up the dominator tree and looks in those avail sets.
52 This might affect code optimality, it's unclear right now.
53 2. Load motion can be performed by value numbering the loads the
54 same as we do other expressions. This requires iterative
55 hashing the vuses into the values. Right now we simply assign
56 a new value every time we see a statement with a vuse.
57 3. Strength reduction can be performed by anticipating expressions
58 we can repair later on.
59 4. Our canonicalization of expressions during lookups don't take
60 constants into account very well. In particular, we don't fold
61 anywhere, so we can get situations where we stupidly think
62 something is a new value (a + 1 + 1 vs a + 2). This is somewhat
63 expensive to fix, but it does expose a lot more eliminations.
64 It may or not be worth it, depending on how critical you
65 consider PRE vs just plain GRE.
66 */
67
68 /* For ease of terminology, "expression node" in the below refers to
69 every expression node but MODIFY_EXPR, because MODIFY_EXPR's represent
70 the actual statement containing the expressions we care about, and
71 we cache the value number by putting it in the expression. */
72
73 /* Basic algorithm
74
75 First we walk the statements to generate the AVAIL sets, the
76 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
77 generation of values/expressions by a given block. We use them
78 when computing the ANTIC sets. The AVAIL sets consist of
79 SSA_NAME's that represent values, so we know what values are
80 available in what blocks. AVAIL is a forward dataflow problem. In
81 SSA, values are never killed, so we don't need a kill set, or a
82 fixpoint iteration, in order to calculate the AVAIL sets. In
83 traditional parlance, AVAIL sets tell us the downsafety of the
84 expressions/values.
85
86 Next, we generate the ANTIC sets. These sets represent the
87 anticipatable expressions. ANTIC is a backwards dataflow
88 problem.An expression is anticipatable in a given block if it could
89 be generated in that block. This means that if we had to perform
90 an insertion in that block, of the value of that expression, we
91 could. Calculating the ANTIC sets requires phi translation of
92 expressions, because the flow goes backwards through phis. We must
93 iterate to a fixpoint of the ANTIC sets, because we have a kill
94 set. Even in SSA form, values are not live over the entire
95 function, only from their definition point onwards. So we have to
96 remove values from the ANTIC set once we go past the definition
97 point of the leaders that make them up.
98 compute_antic/compute_antic_aux performs this computation.
99
100 Third, we perform insertions to make partially redundant
101 expressions fully redundant.
102
103 An expression is partially redundant (excluding partial
104 anticipation) if:
105
106 1. It is AVAIL in some, but not all, of the predecessors of a
107 given block.
108 2. It is ANTIC in all the predecessors.
109
110 In order to make it fully redundant, we insert the expression into
111 the predecessors where it is not available, but is ANTIC.
112 insert/insert_aux performs this insertion.
113
114 Fourth, we eliminate fully redundant expressions.
115 This is a simple statement walk that replaces redundant
116 calculations with the now available values. */
117
118 /* Representations of value numbers:
119
120 Value numbers are represented using the "value handle" approach.
121 This means that each SSA_NAME (and for other reasons to be
122 disclosed in a moment, expression nodes) has a value handle that
123 can be retrieved through get_value_handle. This value handle, *is*
124 the value number of the SSA_NAME. You can pointer compare the
125 value handles for equivalence purposes.
126
127 For debugging reasons, the value handle is internally more than
128 just a number, it is a VAR_DECL named "value.x", where x is a
129 unique number for each value number in use. This allows
130 expressions with SSA_NAMES replaced by value handles to still be
131 pretty printed in a sane way. They simply print as "value.3 *
132 value.5", etc.
133
134 Expression nodes have value handles associated with them as a
135 cache. Otherwise, we'd have to look them up again in the hash
136 table This makes significant difference (factor of two or more) on
137 some test cases. They can be thrown away after the pass is
138 finished. */
139
140 /* Representation of expressions on value numbers:
141
142 In some portions of this code, you will notice we allocate "fake"
143 analogues to the expression we are value numbering, and replace the
144 operands with the values of the expression. Since we work on
145 values, and not just names, we canonicalize expressions to value
146 expressions for use in the ANTIC sets, the EXP_GEN set, etc.
147
148 This is theoretically unnecessary, it just saves a bunch of
149 repeated get_value_handle and find_leader calls in the remainder of
150 the code, trading off temporary memory usage for speed. The tree
151 nodes aren't actually creating more garbage, since they are
152 allocated in a special pools which are thrown away at the end of
153 this pass.
154
155 All of this also means that if you print the EXP_GEN or ANTIC sets,
156 you will see "value.5 + value.7" in the set, instead of "a_55 +
157 b_66" or something. The only thing that actually cares about
158 seeing the value leaders is phi translation, and it needs to be
159 able to find the leader for a value in an arbitrary block, so this
160 "value expression" form is perfect for it (otherwise you'd do
161 get_value_handle->find_leader->translate->get_value_handle->find_leader).*/
162
163
164 /* Representation of sets:
165
166 There are currently two types of sets used, hopefully to be unified soon.
167 The AVAIL sets do not need to be sorted in any particular order,
168 and thus, are simply represented as two bitmaps, one that keeps
169 track of values present in the set, and one that keeps track of
170 expressions present in the set.
171
172 The other sets are represented as doubly linked lists kept in topological
173 order, with an optional supporting bitmap of values present in the
174 set. The sets represent values, and the elements can be values or
175 expressions. The elements can appear in different sets, but each
176 element can only appear once in each set.
177
178 Since each node in the set represents a value, we also want to be
179 able to map expression, set pairs to something that tells us
180 whether the value is present is a set. We use a per-set bitmap for
181 that. The value handles also point to a linked list of the
182 expressions they represent via a tree annotation. This is mainly
183 useful only for debugging, since we don't do identity lookups. */
184
185
186 /* A value set element. Basically a single linked list of
187 expressions/values. */
188 typedef struct value_set_node
189 {
190 /* An expression. */
191 tree expr;
192
193 /* A pointer to the next element of the value set. */
194 struct value_set_node *next;
195 } *value_set_node_t;
196
197
198 /* A value set. This is a singly linked list of value_set_node
199 elements with a possible bitmap that tells us what values exist in
200 the set. This set must be kept in topologically sorted order. */
201 typedef struct value_set
202 {
203 /* The head of the list. Used for iterating over the list in
204 order. */
205 value_set_node_t head;
206
207 /* The tail of the list. Used for tail insertions, which are
208 necessary to keep the set in topologically sorted order because
209 of how the set is built. */
210 value_set_node_t tail;
211
212 /* The length of the list. */
213 size_t length;
214
215 /* True if the set is indexed, which means it contains a backing
216 bitmap for quick determination of whether certain values exist in the
217 set. */
218 bool indexed;
219
220 /* The bitmap of values that exist in the set. May be NULL in an
221 empty or non-indexed set. */
222 bitmap values;
223
224 } *value_set_t;
225
226
227 /* An unordered bitmap set. One bitmap tracks values, the other,
228 expressions. */
229 typedef struct bitmap_set
230 {
231 bitmap expressions;
232 bitmap values;
233 } *bitmap_set_t;
234
235 /* Sets that we need to keep track of. */
236 typedef struct bb_value_sets
237 {
238 /* The EXP_GEN set, which represents expressions/values generated in
239 a basic block. */
240 value_set_t exp_gen;
241
242 /* The PHI_GEN set, which represents PHI results generated in a
243 basic block. */
244 bitmap_set_t phi_gen;
245
246 /* The TMP_GEN set, which represents results/temporaries generated
247 in a basic block. IE the LHS of an expression. */
248 bitmap_set_t tmp_gen;
249
250 /* The AVAIL_OUT set, which represents which values are available in
251 a given basic block. */
252 bitmap_set_t avail_out;
253
254 /* The ANTIC_IN set, which represents which values are anticiptable
255 in a given basic block. */
256 value_set_t antic_in;
257
258 /* The NEW_SETS set, which is used during insertion to augment the
259 AVAIL_OUT set of blocks with the new insertions performed during
260 the current iteration. */
261 bitmap_set_t new_sets;
262 } *bb_value_sets_t;
263
264 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
265 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
266 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
267 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
268 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
269 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
270
271 /* This structure is used to keep track of statistics on what
272 optimization PRE was able to perform. */
273 static struct
274 {
275 /* The number of RHS computations eliminated by PRE. */
276 int eliminations;
277
278 /* The number of new expressions/temporaries generated by PRE. */
279 int insertions;
280
281 /* The number of new PHI nodes added by PRE. */
282 int phis;
283 } pre_stats;
284
285
286 static tree bitmap_find_leader (bitmap_set_t, tree);
287 static tree find_leader (value_set_t, tree);
288 static void value_insert_into_set (value_set_t, tree);
289 static void bitmap_value_insert_into_set (bitmap_set_t, tree);
290 static void bitmap_value_replace_in_set (bitmap_set_t, tree);
291 static void insert_into_set (value_set_t, tree);
292 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
293 static bool bitmap_set_contains_value (bitmap_set_t, tree);
294 static bitmap_set_t bitmap_set_new (void);
295 static value_set_t set_new (bool);
296 static bool is_undefined_value (tree);
297 static tree create_expression_by_pieces (basic_block, tree, tree);
298
299
300 /* We can add and remove elements and entries to and from sets
301 and hash tables, so we use alloc pools for them. */
302
303 static alloc_pool value_set_pool;
304 static alloc_pool bitmap_set_pool;
305 static alloc_pool value_set_node_pool;
306 static alloc_pool binary_node_pool;
307 static alloc_pool unary_node_pool;
308 static alloc_pool reference_node_pool;
309
310 /* Set of blocks with statements that have had its EH information
311 cleaned up. */
312 static bitmap need_eh_cleanup;
313
314 /* The phi_translate_table caches phi translations for a given
315 expression and predecessor. */
316
317 static htab_t phi_translate_table;
318
319 /* A three tuple {e, pred, v} used to cache phi translations in the
320 phi_translate_table. */
321
322 typedef struct expr_pred_trans_d
323 {
324 /* The expression. */
325 tree e;
326
327 /* The predecessor block along which we translated the expression. */
328 basic_block pred;
329
330 /* The value that resulted from the translation. */
331 tree v;
332
333 /* The hashcode for the expression, pred pair. This is cached for
334 speed reasons. */
335 hashval_t hashcode;
336 } *expr_pred_trans_t;
337
338 /* Return the hash value for a phi translation table entry. */
339
340 static hashval_t
341 expr_pred_trans_hash (const void *p)
342 {
343 const expr_pred_trans_t ve = (expr_pred_trans_t) p;
344 return ve->hashcode;
345 }
346
347 /* Return true if two phi translation table entries are the same.
348 P1 and P2 should point to the expr_pred_trans_t's to be compared.*/
349
350 static int
351 expr_pred_trans_eq (const void *p1, const void *p2)
352 {
353 const expr_pred_trans_t ve1 = (expr_pred_trans_t) p1;
354 const expr_pred_trans_t ve2 = (expr_pred_trans_t) p2;
355 basic_block b1 = ve1->pred;
356 basic_block b2 = ve2->pred;
357
358
359 /* If they are not translations for the same basic block, they can't
360 be equal. */
361 if (b1 != b2)
362 return false;
363
364 /* If they are for the same basic block, determine if the
365 expressions are equal. */
366 if (expressions_equal_p (ve1->e, ve2->e))
367 return true;
368
369 return false;
370 }
371
372 /* Search in the phi translation table for the translation of
373 expression E in basic block PRED. Return the translated value, if
374 found, NULL otherwise. */
375
376 static inline tree
377 phi_trans_lookup (tree e, basic_block pred)
378 {
379 void **slot;
380 struct expr_pred_trans_d ept;
381 ept.e = e;
382 ept.pred = pred;
383 ept.hashcode = vn_compute (e, (unsigned long) pred, NULL);
384 slot = htab_find_slot_with_hash (phi_translate_table, &ept, ept.hashcode,
385 NO_INSERT);
386 if (!slot)
387 return NULL;
388 else
389 return ((expr_pred_trans_t) *slot)->v;
390 }
391
392
393 /* Add the tuple mapping from {expression E, basic block PRED} to
394 value V, to the phi translation table. */
395
396 static inline void
397 phi_trans_add (tree e, tree v, basic_block pred)
398 {
399 void **slot;
400 expr_pred_trans_t new_pair = xmalloc (sizeof (*new_pair));
401 new_pair->e = e;
402 new_pair->pred = pred;
403 new_pair->v = v;
404 new_pair->hashcode = vn_compute (e, (unsigned long) pred, NULL);
405 slot = htab_find_slot_with_hash (phi_translate_table, new_pair,
406 new_pair->hashcode, INSERT);
407 if (*slot)
408 free (*slot);
409 *slot = (void *) new_pair;
410 }
411
412
413 /* Add expression E to the expression set of value V. */
414
415 void
416 add_to_value (tree v, tree e)
417 {
418 /* Constants have no expression sets. */
419 if (is_gimple_min_invariant (v))
420 return;
421
422 if (VALUE_HANDLE_EXPR_SET (v) == NULL)
423 VALUE_HANDLE_EXPR_SET (v) = set_new (false);
424
425 insert_into_set (VALUE_HANDLE_EXPR_SET (v), e);
426 }
427
428
429 /* Return true if value V exists in the bitmap for SET. */
430
431 static inline bool
432 value_exists_in_set_bitmap (value_set_t set, tree v)
433 {
434 if (!set->values)
435 return false;
436
437 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (v));
438 }
439
440
441 /* Remove value V from the bitmap for SET. */
442
443 static void
444 value_remove_from_set_bitmap (value_set_t set, tree v)
445 {
446 #ifdef ENABLE_CHECKING
447 if (!set->indexed)
448 abort ();
449 #endif
450
451 if (!set->values)
452 return;
453
454 bitmap_clear_bit (set->values, VALUE_HANDLE_ID (v));
455 }
456
457
458 /* Insert the value number V into the bitmap of values existing in
459 SET. */
460
461 static inline void
462 value_insert_into_set_bitmap (value_set_t set, tree v)
463 {
464 #ifdef ENABLE_CHECKING
465 if (!set->indexed)
466 abort ();
467 #endif
468
469 if (set->values == NULL)
470 {
471 set->values = BITMAP_GGC_ALLOC ();
472 bitmap_clear (set->values);
473 }
474
475 bitmap_set_bit (set->values, VALUE_HANDLE_ID (v));
476 }
477
478
479 /* Create a new bitmap set and return it. */
480
481 static bitmap_set_t
482 bitmap_set_new (void)
483 {
484 bitmap_set_t ret = pool_alloc (bitmap_set_pool);
485 ret->expressions = BITMAP_GGC_ALLOC ();
486 ret->values = BITMAP_GGC_ALLOC ();
487 bitmap_clear (ret->expressions);
488 bitmap_clear (ret->values);
489 return ret;
490 }
491
492 /* Create a new set. */
493
494 static value_set_t
495 set_new (bool indexed)
496 {
497 value_set_t ret;
498 ret = pool_alloc (value_set_pool);
499 ret->head = ret->tail = NULL;
500 ret->length = 0;
501 ret->indexed = indexed;
502 ret->values = NULL;
503 return ret;
504 }
505
506 /* Insert an expression EXPR into a bitmapped set. */
507
508 static void
509 bitmap_insert_into_set (bitmap_set_t set, tree expr)
510 {
511 tree val;
512 /* XXX: For now, we only let SSA_NAMES into the bitmap sets. */
513 if (TREE_CODE (expr) != SSA_NAME)
514 abort ();
515 val = get_value_handle (expr);
516
517 if (val == NULL)
518 abort ();
519 if (!is_gimple_min_invariant (val))
520 bitmap_set_bit (set->values, VALUE_HANDLE_ID (val));
521 bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
522 }
523
524 /* Insert EXPR into SET. */
525
526 static void
527 insert_into_set (value_set_t set, tree expr)
528 {
529 value_set_node_t newnode = pool_alloc (value_set_node_pool);
530 tree val = get_value_handle (expr);
531
532 if (val == NULL)
533 abort ();
534
535 /* For indexed sets, insert the value into the set value bitmap.
536 For all sets, add it to the linked list and increment the list
537 length. */
538 if (set->indexed)
539 value_insert_into_set_bitmap (set, val);
540
541 newnode->next = NULL;
542 newnode->expr = expr;
543 set->length ++;
544 if (set->head == NULL)
545 {
546 set->head = set->tail = newnode;
547 }
548 else
549 {
550 set->tail->next = newnode;
551 set->tail = newnode;
552 }
553 }
554
555 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
556
557 static void
558 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
559 {
560 bitmap_copy (dest->expressions, orig->expressions);
561 bitmap_copy (dest->values, orig->values);
562 }
563
564 /* Copy the set ORIG to the set DEST. */
565
566 static void
567 set_copy (value_set_t dest, value_set_t orig)
568 {
569 value_set_node_t node;
570
571 if (!orig || !orig->head)
572 return;
573
574 for (node = orig->head;
575 node;
576 node = node->next)
577 {
578 insert_into_set (dest, node->expr);
579 }
580 }
581
582 /* Remove EXPR from SET. */
583
584 static void
585 set_remove (value_set_t set, tree expr)
586 {
587 value_set_node_t node, prev;
588
589 /* Remove the value of EXPR from the bitmap, decrement the set
590 length, and remove it from the actual double linked list. */
591 value_remove_from_set_bitmap (set, get_value_handle (expr));
592 set->length--;
593 prev = NULL;
594 for (node = set->head;
595 node != NULL;
596 prev = node, node = node->next)
597 {
598 if (node->expr == expr)
599 {
600 if (prev == NULL)
601 set->head = node->next;
602 else
603 prev->next= node->next;
604
605 if (node == set->tail)
606 set->tail = prev;
607 pool_free (value_set_node_pool, node);
608 return;
609 }
610 }
611 }
612
613 /* Return true if SET contains the value VAL. */
614
615 static bool
616 set_contains_value (value_set_t set, tree val)
617 {
618 /* All constants are in every set. */
619 if (is_gimple_min_invariant (val))
620 return true;
621
622 if (set->length == 0)
623 return false;
624
625 return value_exists_in_set_bitmap (set, val);
626 }
627
628 /* Return true if bitmapped set SET contains the expression EXPR. */
629 static bool
630 bitmap_set_contains (bitmap_set_t set, tree expr)
631 {
632 /* XXX: Bitmapped sets only contain SSA_NAME's for now. */
633 if (TREE_CODE (expr) != SSA_NAME)
634 return false;
635 return bitmap_bit_p (set->expressions, SSA_NAME_VERSION (expr));
636 }
637
638
639 /* Return true if bitmapped set SET contains the value VAL. */
640
641 static bool
642 bitmap_set_contains_value (bitmap_set_t set, tree val)
643 {
644 if (is_gimple_min_invariant (val))
645 return true;
646 return bitmap_bit_p (set->values, VALUE_HANDLE_ID (val));
647 }
648
649 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
650
651 static void
652 bitmap_set_replace_value (bitmap_set_t set, tree lookfor, tree expr)
653 {
654 value_set_t exprset;
655 value_set_node_t node;
656 if (is_gimple_min_invariant (lookfor))
657 return;
658 if (!bitmap_set_contains_value (set, lookfor))
659 return;
660 /* The number of expressions having a given value is usually
661 significantly less than the total number of expressions in SET.
662 Thus, rather than check, for each expression in SET, whether it
663 has the value LOOKFOR, we walk the reverse mapping that tells us
664 what expressions have a given value, and see if any of those
665 expressions are in our set. For large testcases, this is about
666 5-10x faster than walking the bitmap. If this is somehow a
667 significant lose for some cases, we can choose which set to walk
668 based on the set size. */
669 exprset = VALUE_HANDLE_EXPR_SET (lookfor);
670 for (node = exprset->head; node; node = node->next)
671 {
672 if (TREE_CODE (node->expr) == SSA_NAME)
673 {
674 if (bitmap_bit_p (set->expressions, SSA_NAME_VERSION (node->expr)))
675 {
676 bitmap_clear_bit (set->expressions, SSA_NAME_VERSION (node->expr));
677 bitmap_set_bit (set->expressions, SSA_NAME_VERSION (expr));
678 return;
679 }
680 }
681 }
682 }
683
684 /* Subtract bitmapped set B from value set A, and return the new set. */
685
686 static value_set_t
687 bitmap_set_subtract_from_value_set (value_set_t a, bitmap_set_t b,
688 bool indexed)
689 {
690 value_set_t ret = set_new (indexed);
691 value_set_node_t node;
692 for (node = a->head;
693 node;
694 node = node->next)
695 {
696 if (!bitmap_set_contains (b, node->expr))
697 insert_into_set (ret, node->expr);
698 }
699 return ret;
700 }
701
702 /* Return true if two sets are equal. */
703
704 static bool
705 set_equal (value_set_t a, value_set_t b)
706 {
707 value_set_node_t node;
708
709 if (a->length != b->length)
710 return false;
711 for (node = a->head;
712 node;
713 node = node->next)
714 {
715 if (!set_contains_value (b, get_value_handle (node->expr)))
716 return false;
717 }
718 return true;
719 }
720
721 /* Replace an instance of EXPR's VALUE with EXPR in SET. */
722
723 static void
724 bitmap_value_replace_in_set (bitmap_set_t set, tree expr)
725 {
726 tree val = get_value_handle (expr);
727 bitmap_set_replace_value (set, val, expr);
728 }
729
730 /* Insert EXPR into SET if EXPR's value is not already present in
731 SET. */
732
733 static void
734 bitmap_value_insert_into_set (bitmap_set_t set, tree expr)
735 {
736 tree val = get_value_handle (expr);
737 if (is_gimple_min_invariant (val))
738 return;
739
740 if (!bitmap_set_contains_value (set, val))
741 bitmap_insert_into_set (set, expr);
742 }
743
744 /* Insert the value for EXPR into SET, if it doesn't exist already. */
745
746 static void
747 value_insert_into_set (value_set_t set, tree expr)
748 {
749 tree val = get_value_handle (expr);
750
751 /* Constant and invariant values exist everywhere, and thus,
752 actually keeping them in the sets is pointless. */
753 if (is_gimple_min_invariant (val))
754 return;
755
756 if (!set_contains_value (set, val))
757 insert_into_set (set, expr);
758 }
759
760
761 /* Print out SET to OUTFILE. */
762
763 static void
764 bitmap_print_value_set (FILE *outfile, bitmap_set_t set,
765 const char *setname, int blockindex)
766 {
767 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
768 if (set)
769 {
770 int i;
771 EXECUTE_IF_SET_IN_BITMAP (set->expressions, 0, i,
772 {
773 print_generic_expr (outfile, ssa_name (i), 0);
774
775 fprintf (outfile, " (");
776 print_generic_expr (outfile, get_value_handle (ssa_name (i)), 0);
777 fprintf (outfile, ") ");
778 if (bitmap_last_set_bit (set->expressions) != i)
779 fprintf (outfile, ", ");
780 });
781 }
782 fprintf (outfile, " }\n");
783 }
784 /* Print out the value_set SET to OUTFILE. */
785
786 static void
787 print_value_set (FILE *outfile, value_set_t set,
788 const char *setname, int blockindex)
789 {
790 value_set_node_t node;
791 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
792 if (set)
793 {
794 for (node = set->head;
795 node;
796 node = node->next)
797 {
798 print_generic_expr (outfile, node->expr, 0);
799
800 fprintf (outfile, " (");
801 print_generic_expr (outfile, get_value_handle (node->expr), 0);
802 fprintf (outfile, ") ");
803
804 if (node->next)
805 fprintf (outfile, ", ");
806 }
807 }
808
809 fprintf (outfile, " }\n");
810 }
811
812 /* Print out the expressions that have VAL to OUTFILE. */
813
814 void
815 print_value_expressions (FILE *outfile, tree val)
816 {
817 if (VALUE_HANDLE_EXPR_SET (val))
818 {
819 char s[10];
820 sprintf (s, "VH.%04d", VALUE_HANDLE_ID (val));
821 print_value_set (outfile, VALUE_HANDLE_EXPR_SET (val), s, 0);
822 }
823 }
824
825
826 void
827 debug_value_expressions (tree val)
828 {
829 print_value_expressions (stderr, val);
830 }
831
832
833 void debug_value_set (value_set_t, const char *, int);
834
835 void
836 debug_value_set (value_set_t set, const char *setname, int blockindex)
837 {
838 print_value_set (stderr, set, setname, blockindex);
839 }
840
841 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
842 the phis in PRED. Return NULL if we can't find a leader for each
843 part of the translated expression. */
844
845 static tree
846 phi_translate (tree expr, value_set_t set, basic_block pred,
847 basic_block phiblock)
848 {
849 tree phitrans = NULL;
850 tree oldexpr = expr;
851
852 if (expr == NULL)
853 return NULL;
854
855 /* Phi translations of a given expression don't change, */
856 phitrans = phi_trans_lookup (expr, pred);
857 if (phitrans)
858 return phitrans;
859
860
861 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
862 {
863 case '2':
864 {
865 tree oldop1 = TREE_OPERAND (expr, 0);
866 tree oldop2 = TREE_OPERAND (expr, 1);
867 tree newop1;
868 tree newop2;
869 tree newexpr;
870
871 newop1 = phi_translate (find_leader (set, oldop1),
872 set, pred, phiblock);
873 if (newop1 == NULL)
874 return NULL;
875 newop2 = phi_translate (find_leader (set, oldop2),
876 set, pred, phiblock);
877 if (newop2 == NULL)
878 return NULL;
879 if (newop1 != oldop1 || newop2 != oldop2)
880 {
881 newexpr = pool_alloc (binary_node_pool);
882 memcpy (newexpr, expr, tree_size (expr));
883 create_tree_ann (newexpr);
884 TREE_OPERAND (newexpr, 0) = newop1 == oldop1 ? oldop1 : get_value_handle (newop1);
885 TREE_OPERAND (newexpr, 1) = newop2 == oldop2 ? oldop2 : get_value_handle (newop2);
886 vn_lookup_or_add (newexpr, NULL);
887 expr = newexpr;
888 phi_trans_add (oldexpr, newexpr, pred);
889 }
890 }
891 break;
892 /* XXX: Until we have PRE of loads working, none will be ANTIC.
893 */
894 case 'r':
895 return NULL;
896 break;
897 case '1':
898 {
899 tree oldop1 = TREE_OPERAND (expr, 0);
900 tree newop1;
901 tree newexpr;
902
903 newop1 = phi_translate (find_leader (set, oldop1),
904 set, pred, phiblock);
905 if (newop1 == NULL)
906 return NULL;
907 if (newop1 != oldop1)
908 {
909 newexpr = pool_alloc (unary_node_pool);
910 memcpy (newexpr, expr, tree_size (expr));
911 create_tree_ann (newexpr);
912 TREE_OPERAND (newexpr, 0) = get_value_handle (newop1);
913 vn_lookup_or_add (newexpr, NULL);
914 expr = newexpr;
915 phi_trans_add (oldexpr, newexpr, pred);
916 }
917 }
918 break;
919 case 'd':
920 abort ();
921 case 'x':
922 {
923 tree phi = NULL;
924 int i;
925 if (TREE_CODE (expr) != SSA_NAME)
926 abort ();
927 if (TREE_CODE (SSA_NAME_DEF_STMT (expr)) == PHI_NODE)
928 phi = SSA_NAME_DEF_STMT (expr);
929 else
930 return expr;
931
932 for (i = 0; i < PHI_NUM_ARGS (phi); i++)
933 if (PHI_ARG_EDGE (phi, i)->src == pred)
934 {
935 tree val;
936 if (is_undefined_value (PHI_ARG_DEF (phi, i)))
937 return NULL;
938 val = vn_lookup_or_add (PHI_ARG_DEF (phi, i), NULL);
939 return PHI_ARG_DEF (phi, i);
940 }
941 }
942 break;
943 }
944 return expr;
945 }
946
947 static void
948 phi_translate_set (value_set_t dest, value_set_t set, basic_block pred,
949 basic_block phiblock)
950 {
951 value_set_node_t node;
952 for (node = set->head;
953 node;
954 node = node->next)
955 {
956 tree translated;
957 translated = phi_translate (node->expr, set, pred, phiblock);
958 phi_trans_add (node->expr, translated, pred);
959
960 if (translated != NULL)
961 value_insert_into_set (dest, translated);
962 }
963 }
964
965 /* Find the leader for a value (i.e., the name representing that
966 value) in a given set, and return it. Return NULL if no leader is
967 found. */
968
969 static tree
970 bitmap_find_leader (bitmap_set_t set, tree val)
971 {
972 if (val == NULL)
973 return NULL;
974
975 if (is_gimple_min_invariant (val))
976 return val;
977 if (bitmap_set_contains_value (set, val))
978 {
979 /* Rather than walk the entire bitmap of expressions, and see
980 whether any of them has the value we are looking for, we look
981 at the reverse mapping, which tells us the set of expressions
982 that have a given value (IE value->expressions with that
983 value) and see if any of those expressions are in our set.
984 The number of expressions per value is usually significantly
985 less than the number of expressions in the set. In fact, for
986 large testcases, doing it this way is roughly 5-10x faster
987 than walking the bitmap.
988 If this is somehow a significant lose for some cases, we can
989 choose which set to walk based on which set is smaller. */
990 value_set_t exprset;
991 value_set_node_t node;
992 exprset = VALUE_HANDLE_EXPR_SET (val);
993 for (node = exprset->head; node; node = node->next)
994 {
995 if (TREE_CODE (node->expr) == SSA_NAME)
996 {
997 if (bitmap_bit_p (set->expressions,
998 SSA_NAME_VERSION (node->expr)))
999 return node->expr;
1000 }
1001 }
1002 }
1003 return NULL;
1004 }
1005
1006
1007 /* Find the leader for a value (i.e., the name representing that
1008 value) in a given set, and return it. Return NULL if no leader is
1009 found. */
1010
1011 static tree
1012 find_leader (value_set_t set, tree val)
1013 {
1014 value_set_node_t node;
1015
1016 if (val == NULL)
1017 return NULL;
1018
1019 /* Constants represent themselves. */
1020 if (is_gimple_min_invariant (val))
1021 return val;
1022
1023 if (set->length == 0)
1024 return NULL;
1025
1026 if (value_exists_in_set_bitmap (set, val))
1027 {
1028 for (node = set->head;
1029 node;
1030 node = node->next)
1031 {
1032 if (get_value_handle (node->expr) == val)
1033 return node->expr;
1034 }
1035 }
1036
1037 return NULL;
1038 }
1039
1040 /* Determine if the expression EXPR is valid in SET. This means that
1041 we have a leader for each part of the expression (if it consists of
1042 values), or the expression is an SSA_NAME.
1043
1044 NB: We never should run into a case where we have SSA_NAME +
1045 SSA_NAME or SSA_NAME + value. The sets valid_in_set is called on,
1046 the ANTIC sets, will only ever have SSA_NAME's or binary value
1047 expression (IE VALUE1 + VALUE2) */
1048
1049 static bool
1050 valid_in_set (value_set_t set, tree expr)
1051 {
1052 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1053 {
1054 case '2':
1055 {
1056 tree op1 = TREE_OPERAND (expr, 0);
1057 tree op2 = TREE_OPERAND (expr, 1);
1058 return set_contains_value (set, op1) && set_contains_value (set, op2);
1059 }
1060 break;
1061 case '1':
1062 {
1063 tree op1 = TREE_OPERAND (expr, 0);
1064 return set_contains_value (set, op1);
1065 }
1066 break;
1067 /* XXX: Until PRE of loads works, no reference nodes are ANTIC.
1068 */
1069 case 'r':
1070 {
1071 return false;
1072 }
1073 case 'x':
1074 {
1075 if (TREE_CODE (expr) == SSA_NAME)
1076 return true;
1077 abort ();
1078 }
1079 case 'c':
1080 abort ();
1081 }
1082 return false;
1083 }
1084
1085 /* Clean the set of expressions that are no longer valid in SET. This
1086 means expressions that are made up of values we have no leaders for
1087 in SET. */
1088
1089 static void
1090 clean (value_set_t set)
1091 {
1092 value_set_node_t node;
1093 value_set_node_t next;
1094 node = set->head;
1095 while (node)
1096 {
1097 next = node->next;
1098 if (!valid_in_set (set, node->expr))
1099 set_remove (set, node->expr);
1100 node = next;
1101 }
1102 }
1103
1104 /* Compute the ANTIC set for BLOCK.
1105
1106 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK), if
1107 succs(BLOCK) > 1
1108 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)]) if
1109 succs(BLOCK) == 1
1110
1111 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] -
1112 TMP_GEN[BLOCK])
1113
1114 Iterate until fixpointed.
1115
1116 XXX: It would be nice to either write a set_clear, and use it for
1117 antic_out, or to mark the antic_out set as deleted at the end
1118 of this routine, so that the pool can hand the same memory back out
1119 again for the next antic_out. */
1120
1121
1122 static bool
1123 compute_antic_aux (basic_block block)
1124 {
1125 basic_block son;
1126 edge e;
1127 bool changed = false;
1128 value_set_t S, old, ANTIC_OUT;
1129 value_set_node_t node;
1130
1131 ANTIC_OUT = S = NULL;
1132 /* If any edges from predecessors are abnormal, antic_in is empty, so
1133 punt. Remember that the block has an incoming abnormal edge by
1134 setting the BB_VISITED flag. */
1135 if (! (block->flags & BB_VISITED))
1136 {
1137 for (e = block->pred; e; e = e->pred_next)
1138 if (e->flags & EDGE_ABNORMAL)
1139 {
1140 block->flags |= BB_VISITED;
1141 break;
1142 }
1143 }
1144 if (block->flags & BB_VISITED)
1145 {
1146 S = NULL;
1147 goto visit_sons;
1148 }
1149
1150
1151 old = set_new (false);
1152 set_copy (old, ANTIC_IN (block));
1153 ANTIC_OUT = set_new (true);
1154
1155 /* If the block has no successors, ANTIC_OUT is empty, because it is
1156 the exit block. */
1157 if (block->succ == NULL);
1158
1159 /* If we have one successor, we could have some phi nodes to
1160 translate through. */
1161 else if (block->succ->succ_next == NULL)
1162 {
1163 phi_translate_set (ANTIC_OUT, ANTIC_IN(block->succ->dest),
1164 block, block->succ->dest);
1165 }
1166 /* If we have multiple successors, we take the intersection of all of
1167 them. */
1168 else
1169 {
1170 varray_type worklist;
1171 edge e;
1172 size_t i;
1173 basic_block bprime, first;
1174
1175 VARRAY_BB_INIT (worklist, 1, "succ");
1176 e = block->succ;
1177 while (e)
1178 {
1179 VARRAY_PUSH_BB (worklist, e->dest);
1180 e = e->succ_next;
1181 }
1182 first = VARRAY_BB (worklist, 0);
1183 set_copy (ANTIC_OUT, ANTIC_IN (first));
1184
1185 for (i = 1; i < VARRAY_ACTIVE_SIZE (worklist); i++)
1186 {
1187 bprime = VARRAY_BB (worklist, i);
1188 node = ANTIC_OUT->head;
1189 while (node)
1190 {
1191 tree val;
1192 value_set_node_t next = node->next;
1193 val = get_value_handle (node->expr);
1194 if (!set_contains_value (ANTIC_IN (bprime), val))
1195 set_remove (ANTIC_OUT, node->expr);
1196 node = next;
1197 }
1198 }
1199 VARRAY_CLEAR (worklist);
1200 }
1201
1202 /* Generate ANTIC_OUT - TMP_GEN */
1203 S = bitmap_set_subtract_from_value_set (ANTIC_OUT, TMP_GEN (block), false);
1204
1205 /* Start ANTIC_IN with EXP_GEN - TMP_GEN */
1206 ANTIC_IN (block) = bitmap_set_subtract_from_value_set (EXP_GEN (block),
1207 TMP_GEN (block),
1208 true);
1209
1210 /* Then union in the ANTIC_OUT - TMP_GEN values, to get ANTIC_OUT U
1211 EXP_GEN - TMP_GEN */
1212 for (node = S->head;
1213 node;
1214 node = node->next)
1215 {
1216 value_insert_into_set (ANTIC_IN (block), node->expr);
1217 }
1218 clean (ANTIC_IN (block));
1219
1220
1221 if (!set_equal (old, ANTIC_IN (block)))
1222 changed = true;
1223
1224 visit_sons:
1225 if (dump_file && (dump_flags & TDF_DETAILS))
1226 {
1227 if (ANTIC_OUT)
1228 print_value_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
1229 print_value_set (dump_file, ANTIC_IN (block), "ANTIC_IN", block->index);
1230 if (S)
1231 print_value_set (dump_file, S, "S", block->index);
1232
1233 }
1234
1235 for (son = first_dom_son (CDI_POST_DOMINATORS, block);
1236 son;
1237 son = next_dom_son (CDI_POST_DOMINATORS, son))
1238 {
1239 changed |= compute_antic_aux (son);
1240 }
1241 return changed;
1242 }
1243
1244 /* Compute ANTIC sets. */
1245
1246 static void
1247 compute_antic (void)
1248 {
1249 bool changed = true;
1250 basic_block bb;
1251 int num_iterations = 0;
1252 FOR_ALL_BB (bb)
1253 {
1254 ANTIC_IN (bb) = set_new (true);
1255 if (bb->flags & BB_VISITED)
1256 abort ();
1257 }
1258
1259 while (changed)
1260 {
1261 num_iterations++;
1262 changed = false;
1263 changed = compute_antic_aux (EXIT_BLOCK_PTR);
1264 }
1265 FOR_ALL_BB (bb)
1266 {
1267 bb->flags &= ~BB_VISITED;
1268 }
1269 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1270 fprintf (dump_file, "compute_antic required %d iterations\n", num_iterations);
1271 }
1272
1273
1274 /* Find a leader for an expression, or generate one using
1275 create_expression_by_pieces if it's ANTIC but
1276 complex.
1277 BLOCK is the basic_block we are looking for leaders in.
1278 EXPR is the expression to find a leader or generate for.
1279 STMTS is the statement list to put the inserted expressions on.
1280 Returns the SSA_NAME of the LHS of the generated expression or the
1281 leader. */
1282
1283 static tree
1284 find_or_generate_expression (basic_block block, tree expr, tree stmts)
1285 {
1286 tree genop;
1287 genop = bitmap_find_leader (AVAIL_OUT (block), expr);
1288 /* Depending on the order we process DOM branches in, the value
1289 may not have propagated to all the dom children yet during
1290 this iteration. In this case, the value will always be in
1291 the NEW_SETS for us already, having been propagated from our
1292 dominator. */
1293 if (genop == NULL)
1294 genop = bitmap_find_leader (NEW_SETS (block), expr);
1295 /* If it's still NULL, see if it is a complex expression, and if
1296 so, generate it recursively, otherwise, abort, because it's
1297 not really . */
1298 if (genop == NULL)
1299 {
1300 genop = VALUE_HANDLE_EXPR_SET (expr)->head->expr;
1301 if (TREE_CODE_CLASS (TREE_CODE (genop)) != '1'
1302 && TREE_CODE_CLASS (TREE_CODE (genop)) != '2'
1303 && TREE_CODE_CLASS (TREE_CODE (genop)) != 'r')
1304 abort ();
1305 genop = create_expression_by_pieces (block, genop, stmts);
1306 }
1307 return genop;
1308 }
1309
1310
1311 /* Create an expression in pieces, so that we can handle very complex
1312 expressions that may be ANTIC, but not necessary GIMPLE.
1313 BLOCK is the basic block the expression will be inserted into,
1314 EXPR is the expression to insert (in value form)
1315 STMTS is a statement list to append the necessary insertions into.
1316
1317 This function will abort if we hit some value that shouldn't be
1318 ANTIC but is (IE there is no leader for it, or its components).
1319 This function may also generate expressions that are themselves
1320 partially or fully redundant. Those that are will be either made
1321 fully redundant during the next iteration of insert (for partially
1322 redundant ones), or eliminated by eliminate (for fully redundant
1323 ones). */
1324
1325 static tree
1326 create_expression_by_pieces (basic_block block, tree expr, tree stmts)
1327 {
1328 tree name = NULL_TREE;
1329 tree newexpr = NULL_TREE;
1330 tree v;
1331
1332 switch (TREE_CODE_CLASS (TREE_CODE (expr)))
1333 {
1334 case '2':
1335 {
1336 tree_stmt_iterator tsi;
1337 tree genop1, genop2;
1338 tree temp;
1339 tree op1 = TREE_OPERAND (expr, 0);
1340 tree op2 = TREE_OPERAND (expr, 1);
1341 genop1 = find_or_generate_expression (block, op1, stmts);
1342 genop2 = find_or_generate_expression (block, op2, stmts);
1343 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1344 add_referenced_tmp_var (temp);
1345 newexpr = build (TREE_CODE (expr), TREE_TYPE (expr),
1346 genop1, genop2);
1347 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1348 temp, newexpr);
1349 name = make_ssa_name (temp, newexpr);
1350 TREE_OPERAND (newexpr, 0) = name;
1351 tsi = tsi_last (stmts);
1352 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1353 pre_stats.insertions++;
1354 break;
1355 }
1356 case '1':
1357 {
1358 tree_stmt_iterator tsi;
1359 tree genop1;
1360 tree temp;
1361 tree op1 = TREE_OPERAND (expr, 0);
1362 genop1 = find_or_generate_expression (block, op1, stmts);
1363 temp = create_tmp_var (TREE_TYPE (expr), "pretmp");
1364 add_referenced_tmp_var (temp);
1365 newexpr = build (TREE_CODE (expr), TREE_TYPE (expr),
1366 genop1);
1367 newexpr = build (MODIFY_EXPR, TREE_TYPE (expr),
1368 temp, newexpr);
1369 name = make_ssa_name (temp, newexpr);
1370 TREE_OPERAND (newexpr, 0) = name;
1371 tsi = tsi_last (stmts);
1372 tsi_link_after (&tsi, newexpr, TSI_CONTINUE_LINKING);
1373 pre_stats.insertions++;
1374
1375 break;
1376 }
1377 default:
1378 abort ();
1379
1380 }
1381 v = get_value_handle (expr);
1382 vn_add (name, v, NULL);
1383 bitmap_insert_into_set (NEW_SETS (block), name);
1384 bitmap_value_insert_into_set (AVAIL_OUT (block), name);
1385 if (dump_file && (dump_flags & TDF_DETAILS))
1386 {
1387 fprintf (dump_file, "Inserted ");
1388 print_generic_expr (dump_file, newexpr, 0);
1389 fprintf (dump_file, " in predecessor %d\n", block->index);
1390 }
1391 return name;
1392 }
1393
1394 /* Perform insertion of partially redundant values.
1395 For BLOCK, do the following:
1396 1. Propagate the NEW_SETS of the dominator into the current block.
1397 If the block has multiple predecessors,
1398 2a. Iterate over the ANTIC expressions for the block to see if
1399 any of them are partially redundant.
1400 2b. If so, insert them into the necessary predecessors to make
1401 the expression fully redundant.
1402 2c. Insert a new PHI merging the values of the predecessors.
1403 2d. Insert the new PHI, and the new expressions, into the
1404 NEW_SETS set.
1405 3. Recursively call ourselves on the dominator children of BLOCK.
1406
1407 */
1408 static bool
1409 insert_aux (basic_block block)
1410 {
1411 basic_block son;
1412 bool new_stuff = false;
1413
1414 if (block)
1415 {
1416 basic_block dom;
1417 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1418 if (dom)
1419 {
1420 int i;
1421 bitmap_set_t newset = NEW_SETS (dom);
1422 EXECUTE_IF_SET_IN_BITMAP (newset->expressions, 0, i,
1423 {
1424 bitmap_insert_into_set (NEW_SETS (block), ssa_name (i));
1425 bitmap_value_replace_in_set (AVAIL_OUT (block), ssa_name (i));
1426 });
1427 if (block->pred->pred_next)
1428 {
1429 value_set_node_t node;
1430 for (node = ANTIC_IN (block)->head;
1431 node;
1432 node = node->next)
1433 {
1434 if (TREE_CODE_CLASS (TREE_CODE (node->expr)) == '2'
1435 || TREE_CODE_CLASS (TREE_CODE (node->expr)) == '1')
1436 {
1437 tree *avail;
1438 tree val;
1439 bool by_some = false;
1440 bool cant_insert = false;
1441 bool all_same = true;
1442 tree first_s = NULL;
1443 edge pred;
1444 basic_block bprime;
1445 tree eprime;
1446
1447 val = get_value_handle (node->expr);
1448 if (bitmap_set_contains_value (PHI_GEN (block), val))
1449 continue;
1450 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
1451 {
1452 if (dump_file && (dump_flags & TDF_DETAILS))
1453 fprintf (dump_file, "Found fully redundant value\n");
1454 continue;
1455 }
1456
1457 avail = xcalloc (last_basic_block, sizeof (tree));
1458 for (pred = block->pred;
1459 pred;
1460 pred = pred->pred_next)
1461 {
1462 tree vprime;
1463 tree edoubleprime;
1464
1465 /* This can happen in the very weird case
1466 that our fake infinite loop edges have caused a
1467 critical edge to appear. */
1468 if (EDGE_CRITICAL_P (pred))
1469 {
1470 cant_insert = true;
1471 break;
1472 }
1473 bprime = pred->src;
1474 eprime = phi_translate (node->expr,
1475 ANTIC_IN (block),
1476 bprime, block);
1477
1478 /* eprime will generally only be NULL if the
1479 value of the expression, translated
1480 through the PHI for this predecessor, is
1481 undefined. If that is the case, we can't
1482 make the expression fully redundant,
1483 because its value is undefined along a
1484 predecessor path. We can thus break out
1485 early because it doesn't matter what the
1486 rest of the results are. */
1487 if (eprime == NULL)
1488 {
1489 cant_insert = true;
1490 break;
1491 }
1492
1493 vprime = get_value_handle (eprime);
1494 if (!vprime)
1495 abort ();
1496 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
1497 vprime);
1498 if (edoubleprime == NULL)
1499 {
1500 avail[bprime->index] = eprime;
1501 all_same = false;
1502 }
1503 else
1504 {
1505 avail[bprime->index] = edoubleprime;
1506 by_some = true;
1507 if (first_s == NULL)
1508 first_s = edoubleprime;
1509 else if (first_s != edoubleprime)
1510 all_same = false;
1511 if (first_s != edoubleprime
1512 && operand_equal_p (first_s, edoubleprime, 0))
1513 abort ();
1514 }
1515 }
1516 /* If we can insert it, it's not the same value
1517 already existing along every predecessor, and
1518 it's defined by some predecessor, it is
1519 partially redundant. */
1520 if (!cant_insert && !all_same && by_some)
1521 {
1522 tree type = TREE_TYPE (avail[block->pred->src->index]);
1523 tree temp;
1524 if (dump_file && (dump_flags & TDF_DETAILS))
1525 {
1526 fprintf (dump_file, "Found partial redundancy for expression ");
1527 print_generic_expr (dump_file, node->expr, 0);
1528 fprintf (dump_file, "\n");
1529 }
1530
1531 /* Make the necessary insertions. */
1532 for (pred = block->pred;
1533 pred;
1534 pred = pred->pred_next)
1535 {
1536 tree stmts = alloc_stmt_list ();
1537 tree builtexpr;
1538 bprime = pred->src;
1539 eprime = avail[bprime->index];
1540 if (TREE_CODE_CLASS (TREE_CODE (eprime)) == '2'
1541 || TREE_CODE_CLASS (TREE_CODE (eprime)) == '1')
1542 {
1543 builtexpr = create_expression_by_pieces (bprime,
1544 eprime,
1545 stmts);
1546 bsi_insert_on_edge (pred, stmts);
1547 bsi_commit_edge_inserts (NULL);
1548 avail[bprime->index] = builtexpr;
1549 }
1550 }
1551 /* Now build a phi for the new variable. */
1552 temp = create_tmp_var (type, "prephitmp");
1553 add_referenced_tmp_var (temp);
1554 temp = create_phi_node (temp, block);
1555 vn_add (PHI_RESULT (temp), val, NULL);
1556
1557 #if 0
1558 if (!set_contains_value (AVAIL_OUT (block), val))
1559 insert_into_set (AVAIL_OUT (block),
1560 PHI_RESULT (temp));
1561 else
1562 #endif
1563 bitmap_value_replace_in_set (AVAIL_OUT (block),
1564 PHI_RESULT (temp));
1565 for (pred = block->pred;
1566 pred;
1567 pred = pred->pred_next)
1568 {
1569 add_phi_arg (&temp, avail[pred->src->index],
1570 pred);
1571 }
1572 if (dump_file && (dump_flags & TDF_DETAILS))
1573 {
1574 fprintf (dump_file, "Created phi ");
1575 print_generic_expr (dump_file, temp, 0);
1576 fprintf (dump_file, " in block %d\n", block->index);
1577 }
1578 pre_stats.phis++;
1579 new_stuff = true;
1580 bitmap_insert_into_set (NEW_SETS (block),
1581 PHI_RESULT (temp));
1582 bitmap_insert_into_set (PHI_GEN (block),
1583 PHI_RESULT (temp));
1584 }
1585
1586 free (avail);
1587 }
1588 }
1589 }
1590 }
1591 }
1592 for (son = first_dom_son (CDI_DOMINATORS, block);
1593 son;
1594 son = next_dom_son (CDI_DOMINATORS, son))
1595 {
1596 new_stuff |= insert_aux (son);
1597 }
1598
1599 return new_stuff;
1600 }
1601
1602 /* Perform insertion of partially redundant values. */
1603
1604 static void
1605 insert (void)
1606 {
1607 bool new_stuff = true;
1608 basic_block bb;
1609 int num_iterations = 0;
1610
1611 FOR_ALL_BB (bb)
1612 NEW_SETS (bb) = bitmap_set_new ();
1613
1614 while (new_stuff)
1615 {
1616 num_iterations++;
1617 new_stuff = false;
1618 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
1619 }
1620 if (num_iterations > 2 && dump_file && (dump_flags & TDF_STATS))
1621 fprintf (dump_file, "insert required %d iterations\n", num_iterations);
1622 }
1623
1624
1625 /* Return true if VAR is an SSA variable with no defining statement in
1626 this procedure, *AND* isn't a live-on-entry parameter. */
1627
1628 static bool
1629 is_undefined_value (tree expr)
1630 {
1631 return (TREE_CODE (expr) == SSA_NAME
1632 && IS_EMPTY_STMT (SSA_NAME_DEF_STMT (expr))
1633 /* PARM_DECLs and hard registers are always defined. */
1634 && TREE_CODE (SSA_NAME_VAR (expr)) != PARM_DECL
1635 && !DECL_HARD_REGISTER (SSA_NAME_VAR (expr)));
1636 }
1637
1638
1639 /* Given an SSA variable VAR and an expression EXPR, compute the value
1640 number for EXPR and create a value handle (VAL) for it. If VAR and
1641 EXPR are not the same, associate VAL with VAR. Finally, add VAR to
1642 S1 and its value handle to S2.
1643
1644 VUSES represent the virtual use operands associated with EXPR (if
1645 any). They are used when computing the hash value for EXPR. */
1646
1647 static inline void
1648 add_to_sets (tree var, tree expr, vuse_optype vuses, bitmap_set_t s1,
1649 bitmap_set_t s2)
1650 {
1651 tree val = vn_lookup_or_add (expr, vuses);
1652
1653 /* VAR and EXPR may be the same when processing statements for which
1654 we are not computing value numbers (e.g., non-assignments, or
1655 statements that make aliased stores). In those cases, we are
1656 only interested in making VAR available as its own value. */
1657 if (var != expr)
1658 vn_add (var, val, NULL);
1659
1660 bitmap_insert_into_set (s1, var);
1661 bitmap_value_insert_into_set (s2, var);
1662 }
1663
1664
1665 /* Given a unary or binary expression EXPR, create and return a new
1666 expression with the same structure as EXPR but with its operands
1667 replaced with the value handles of each of the operands of EXPR.
1668 Insert EXPR's operands into the EXP_GEN set for BLOCK.
1669
1670 VUSES represent the virtual use operands associated with EXPR (if
1671 any). They are used when computing the hash value for EXPR. */
1672
1673 static inline tree
1674 create_value_expr_from (tree expr, basic_block block, vuse_optype vuses)
1675 {
1676 int i;
1677 enum tree_code code = TREE_CODE (expr);
1678 tree vexpr;
1679
1680 #if defined ENABLE_CHECKING
1681 if (TREE_CODE_CLASS (code) != '1'
1682 && TREE_CODE_CLASS (code) != '2'
1683 && TREE_CODE_CLASS (code) != 'r')
1684 abort ();
1685 #endif
1686
1687 if (TREE_CODE_CLASS (code) == '1')
1688 vexpr = pool_alloc (unary_node_pool);
1689 else if (TREE_CODE_CLASS (code) == 'r')
1690 vexpr = pool_alloc (reference_node_pool);
1691 else
1692 vexpr = pool_alloc (binary_node_pool);
1693
1694 memcpy (vexpr, expr, tree_size (expr));
1695
1696 for (i = 0; i < TREE_CODE_LENGTH (code); i++)
1697 {
1698 tree op = TREE_OPERAND (expr, i);
1699 if (op != NULL)
1700 {
1701 tree val = vn_lookup_or_add (op, vuses);
1702 if (!is_undefined_value (op))
1703 value_insert_into_set (EXP_GEN (block), op);
1704 TREE_TYPE (val) = TREE_TYPE (TREE_OPERAND (vexpr, i));
1705 TREE_OPERAND (vexpr, i) = val;
1706 }
1707 }
1708
1709 return vexpr;
1710 }
1711
1712
1713 /* Compute the AVAIL set for BLOCK.
1714 This function performs value numbering of the statements in BLOCK.
1715 The AVAIL sets are built from information we glean while doing this
1716 value numbering, since the AVAIL sets contain only one entry per
1717 value.
1718
1719 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
1720 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
1721
1722 static void
1723 compute_avail (basic_block block)
1724 {
1725 basic_block son;
1726
1727 /* For arguments with default definitions, we pretend they are
1728 defined in the entry block. */
1729 if (block == ENTRY_BLOCK_PTR)
1730 {
1731 tree param;
1732 for (param = DECL_ARGUMENTS (current_function_decl);
1733 param;
1734 param = TREE_CHAIN (param))
1735 {
1736 if (default_def (param) != NULL)
1737 {
1738 tree val;
1739 tree def = default_def (param);
1740 val = vn_lookup_or_add (def, NULL);
1741 bitmap_insert_into_set (TMP_GEN (block), def);
1742 bitmap_value_insert_into_set (AVAIL_OUT (block), def);
1743 }
1744 }
1745 }
1746 else if (block)
1747 {
1748 block_stmt_iterator bsi;
1749 tree stmt, phi;
1750 basic_block dom;
1751
1752 /* Initially, the set of available values in BLOCK is that of
1753 its immediate dominator. */
1754 dom = get_immediate_dominator (CDI_DOMINATORS, block);
1755 if (dom)
1756 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
1757
1758 /* Generate values for PHI nodes. */
1759 for (phi = phi_nodes (block); phi; phi = PHI_CHAIN (phi))
1760 /* We have no need for virtual phis, as they don't represent
1761 actual computations. */
1762 if (is_gimple_reg (PHI_RESULT (phi)))
1763 add_to_sets (PHI_RESULT (phi), PHI_RESULT (phi), NULL,
1764 PHI_GEN (block), AVAIL_OUT (block));
1765
1766 /* Now compute value numbers and populate value sets with all
1767 the expressions computed in BLOCK. */
1768 for (bsi = bsi_start (block); !bsi_end_p (bsi); bsi_next (&bsi))
1769 {
1770 stmt_ann_t ann;
1771 size_t j;
1772
1773 stmt = bsi_stmt (bsi);
1774 ann = stmt_ann (stmt);
1775 get_stmt_operands (stmt);
1776
1777 /* We are only interested in assignments of the form
1778 X_i = EXPR, where EXPR represents an "interesting"
1779 computation, it has no volatile operands and X_i
1780 doesn't flow through an abnormal edge. */
1781 if (TREE_CODE (stmt) == MODIFY_EXPR
1782 && !ann->has_volatile_ops
1783 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
1784 && !SSA_NAME_OCCURS_IN_ABNORMAL_PHI (TREE_OPERAND (stmt, 0)))
1785 {
1786 tree lhs = TREE_OPERAND (stmt, 0);
1787 tree rhs = TREE_OPERAND (stmt, 1);
1788 vuse_optype vuses = STMT_VUSE_OPS (stmt);
1789
1790 STRIP_USELESS_TYPE_CONVERSION (rhs);
1791 if (TREE_CODE (rhs) == SSA_NAME
1792 || is_gimple_min_invariant (rhs))
1793 {
1794 /* Compute a value number for the RHS of the statement
1795 and add its value to the AVAIL_OUT set for the block.
1796 Add the LHS to TMP_GEN. */
1797 add_to_sets (lhs, rhs, vuses, TMP_GEN (block),
1798 AVAIL_OUT (block));
1799
1800 if (TREE_CODE (rhs) == SSA_NAME
1801 && !is_undefined_value (rhs))
1802 value_insert_into_set (EXP_GEN (block), rhs);
1803 continue;
1804 }
1805 else if (TREE_CODE_CLASS (TREE_CODE (rhs)) == '1'
1806 || TREE_CODE_CLASS (TREE_CODE (rhs)) == '2'
1807 || TREE_CODE (rhs) == INDIRECT_REF)
1808 {
1809 /* For binary, unary, and reference expressions,
1810 create a duplicate expression with the operands
1811 replaced with the value handles of the original
1812 RHS. */
1813 tree newt = create_value_expr_from (rhs, block, vuses);
1814 add_to_sets (lhs, newt, vuses, TMP_GEN (block),
1815 AVAIL_OUT (block));
1816 value_insert_into_set (EXP_GEN (block), newt);
1817 continue;
1818 }
1819 }
1820
1821 /* For any other statement that we don't recognize, simply
1822 make the names generated by the statement available in
1823 AVAIL_OUT and TMP_GEN. */
1824 for (j = 0; j < NUM_DEFS (STMT_DEF_OPS (stmt)); j++)
1825 {
1826 tree def = DEF_OP (STMT_DEF_OPS (stmt), j);
1827 add_to_sets (def, def, NULL, TMP_GEN (block),
1828 AVAIL_OUT (block));
1829 }
1830
1831 for (j = 0; j < NUM_USES (STMT_USE_OPS (stmt)); j++)
1832 {
1833 tree use = USE_OP (STMT_USE_OPS (stmt), j);
1834 add_to_sets (use, use, NULL, TMP_GEN (block),
1835 AVAIL_OUT (block));
1836 }
1837 }
1838 }
1839
1840 /* Compute available sets for the dominator children of BLOCK. */
1841 for (son = first_dom_son (CDI_DOMINATORS, block);
1842 son;
1843 son = next_dom_son (CDI_DOMINATORS, son))
1844 compute_avail (son);
1845 }
1846
1847
1848 /* Eliminate fully redundant computations. */
1849
1850 static void
1851 eliminate (void)
1852 {
1853 basic_block b;
1854
1855 FOR_EACH_BB (b)
1856 {
1857 block_stmt_iterator i;
1858
1859 for (i = bsi_start (b); !bsi_end_p (i); bsi_next (&i))
1860 {
1861 tree stmt = bsi_stmt (i);
1862
1863 /* Lookup the RHS of the expression, see if we have an
1864 available computation for it. If so, replace the RHS with
1865 the available computation. */
1866 if (TREE_CODE (stmt) == MODIFY_EXPR
1867 && TREE_CODE (TREE_OPERAND (stmt, 0)) == SSA_NAME
1868 && TREE_CODE (TREE_OPERAND (stmt ,1)) != SSA_NAME
1869 && !is_gimple_min_invariant (TREE_OPERAND (stmt, 1))
1870 && !stmt_ann (stmt)->has_volatile_ops)
1871 {
1872 tree lhs = TREE_OPERAND (stmt, 0);
1873 tree *rhs_p = &TREE_OPERAND (stmt, 1);
1874 tree sprime;
1875
1876 sprime = bitmap_find_leader (AVAIL_OUT (b),
1877 vn_lookup (lhs, NULL));
1878 if (sprime
1879 && sprime != lhs
1880 && (TREE_CODE (*rhs_p) != SSA_NAME
1881 || may_propagate_copy (*rhs_p, sprime)))
1882 {
1883 if (sprime == *rhs_p)
1884 abort ();
1885
1886 if (dump_file && (dump_flags & TDF_DETAILS))
1887 {
1888 fprintf (dump_file, "Replaced ");
1889 print_generic_expr (dump_file, *rhs_p, 0);
1890 fprintf (dump_file, " with ");
1891 print_generic_expr (dump_file, sprime, 0);
1892 fprintf (dump_file, " in ");
1893 print_generic_stmt (dump_file, stmt, 0);
1894 }
1895 pre_stats.eliminations++;
1896 propagate_tree_value (rhs_p, sprime);
1897 modify_stmt (stmt);
1898
1899 /* If we removed EH side effects from the statement, clean
1900 its EH information. */
1901 if (maybe_clean_eh_stmt (stmt))
1902 {
1903 bitmap_set_bit (need_eh_cleanup,
1904 bb_for_stmt (stmt)->index);
1905 if (dump_file && (dump_flags & TDF_DETAILS))
1906 fprintf (dump_file, " Removed EH side effects.\n");
1907 }
1908 }
1909 }
1910 }
1911 }
1912 }
1913
1914
1915 /* Initialize data structures used by PRE. */
1916
1917 static void
1918 init_pre (void)
1919 {
1920 size_t tsize;
1921 basic_block bb;
1922
1923 connect_infinite_loops_to_exit ();
1924 vn_init ();
1925 memset (&pre_stats, 0, sizeof (pre_stats));
1926
1927 /* If block 0 has more than one predecessor, it means that its PHI
1928 nodes will have arguments coming from block -1. This creates
1929 problems for several places in PRE that keep local arrays indexed
1930 by block number. To prevent this, we split the edge coming from
1931 ENTRY_BLOCK_PTR (FIXME, if ENTRY_BLOCK_PTR had an index number
1932 different than -1 we wouldn't have to hack this. tree-ssa-dce.c
1933 needs a similar change). */
1934 if (ENTRY_BLOCK_PTR->succ->dest->pred->pred_next)
1935 if (!(ENTRY_BLOCK_PTR->succ->flags & EDGE_ABNORMAL))
1936 split_edge (ENTRY_BLOCK_PTR->succ);
1937
1938 FOR_ALL_BB (bb)
1939 bb->aux = xcalloc (1, sizeof (struct bb_value_sets));
1940
1941 phi_translate_table = htab_create (511, expr_pred_trans_hash,
1942 expr_pred_trans_eq, free);
1943 value_set_pool = create_alloc_pool ("Value sets",
1944 sizeof (struct value_set), 30);
1945 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
1946 sizeof (struct bitmap_set), 30);
1947 value_set_node_pool = create_alloc_pool ("Value set nodes",
1948 sizeof (struct value_set_node), 30);
1949 calculate_dominance_info (CDI_POST_DOMINATORS);
1950 calculate_dominance_info (CDI_DOMINATORS);
1951 tsize = tree_size (build (PLUS_EXPR, void_type_node, NULL_TREE, NULL_TREE));
1952 binary_node_pool = create_alloc_pool ("Binary tree nodes", tsize, 30);
1953 tsize = tree_size (build1 (NEGATE_EXPR, void_type_node, NULL_TREE));
1954 unary_node_pool = create_alloc_pool ("Unary tree nodes", tsize, 30);
1955 tsize = tree_size (build (COMPONENT_REF, void_type_node, NULL_TREE,
1956 NULL_TREE, NULL_TREE));
1957 reference_node_pool = create_alloc_pool ("Reference tree nodes", tsize, 30);
1958 FOR_ALL_BB (bb)
1959 {
1960 EXP_GEN (bb) = set_new (true);
1961 PHI_GEN (bb) = bitmap_set_new ();
1962 TMP_GEN (bb) = bitmap_set_new ();
1963 AVAIL_OUT (bb) = bitmap_set_new ();
1964 }
1965
1966 need_eh_cleanup = BITMAP_XMALLOC ();
1967 }
1968
1969
1970 /* Deallocate data structures used by PRE. */
1971
1972 static void
1973 fini_pre (void)
1974 {
1975 basic_block bb;
1976
1977 free_alloc_pool (value_set_pool);
1978 free_alloc_pool (bitmap_set_pool);
1979 free_alloc_pool (value_set_node_pool);
1980 free_alloc_pool (binary_node_pool);
1981 free_alloc_pool (reference_node_pool);
1982 free_alloc_pool (unary_node_pool);
1983 htab_delete (phi_translate_table);
1984 remove_fake_exit_edges ();
1985
1986 FOR_ALL_BB (bb)
1987 {
1988 free (bb->aux);
1989 bb->aux = NULL;
1990 }
1991
1992 free_dominance_info (CDI_POST_DOMINATORS);
1993 vn_delete ();
1994
1995 if (bitmap_first_set_bit (need_eh_cleanup) >= 0)
1996 {
1997 tree_purge_all_dead_eh_edges (need_eh_cleanup);
1998 cleanup_tree_cfg ();
1999 }
2000
2001 BITMAP_XFREE (need_eh_cleanup);
2002 }
2003
2004
2005 /* Main entry point to the SSA-PRE pass. DO_FRE is true if the caller
2006 only wants to do full redundancy elimination. */
2007
2008 static void
2009 execute_pre (bool do_fre)
2010 {
2011 init_pre ();
2012
2013 /* Collect and value number expressions computed in each basic
2014 block. */
2015 compute_avail (ENTRY_BLOCK_PTR);
2016
2017 if (dump_file && (dump_flags & TDF_DETAILS))
2018 {
2019 basic_block bb;
2020
2021 FOR_ALL_BB (bb)
2022 {
2023 print_value_set (dump_file, EXP_GEN (bb), "exp_gen", bb->index);
2024 bitmap_print_value_set (dump_file, TMP_GEN (bb), "tmp_gen",
2025 bb->index);
2026 bitmap_print_value_set (dump_file, AVAIL_OUT (bb), "avail_out",
2027 bb->index);
2028 }
2029 }
2030
2031 /* Insert can get quite slow on an incredibly large number of basic
2032 blocks due to some quadratic behavior. Until this behavior is
2033 fixed, don't run it when he have an incredibly large number of
2034 bb's. If we aren't going to run insert, there is no point in
2035 computing ANTIC, either, even though it's plenty fast. */
2036 if (!do_fre && n_basic_blocks < 4000)
2037 {
2038 compute_antic ();
2039 insert ();
2040 }
2041
2042 /* Remove all the redundant expressions. */
2043 eliminate ();
2044
2045 if (dump_file && (dump_flags & TDF_STATS))
2046 {
2047 fprintf (dump_file, "Insertions:%d\n", pre_stats.insertions);
2048 fprintf (dump_file, "New PHIs:%d\n", pre_stats.phis);
2049 fprintf (dump_file, "Eliminated:%d\n", pre_stats.eliminations);
2050 }
2051
2052 fini_pre ();
2053 }
2054
2055
2056 /* Gate and execute functions for PRE. */
2057
2058 static void
2059 do_pre (void)
2060 {
2061 execute_pre (false);
2062 }
2063
2064 static bool
2065 gate_pre (void)
2066 {
2067 return flag_tree_pre != 0;
2068 }
2069
2070 struct tree_opt_pass pass_pre =
2071 {
2072 "pre", /* name */
2073 gate_pre, /* gate */
2074 do_pre, /* execute */
2075 NULL, /* sub */
2076 NULL, /* next */
2077 0, /* static_pass_number */
2078 TV_TREE_PRE, /* tv_id */
2079 PROP_no_crit_edges | PROP_cfg
2080 | PROP_ssa | PROP_alias, /* properties_required */
2081 0, /* properties_provided */
2082 0, /* properties_destroyed */
2083 0, /* todo_flags_start */
2084 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
2085 };
2086
2087
2088 /* Gate and execute functions for FRE. */
2089
2090 static void
2091 do_fre (void)
2092 {
2093 execute_pre (true);
2094 }
2095
2096 static bool
2097 gate_fre (void)
2098 {
2099 return flag_tree_fre != 0;
2100 }
2101
2102 struct tree_opt_pass pass_fre =
2103 {
2104 "fre", /* name */
2105 gate_fre, /* gate */
2106 do_fre, /* execute */
2107 NULL, /* sub */
2108 NULL, /* next */
2109 0, /* static_pass_number */
2110 TV_TREE_FRE, /* tv_id */
2111 PROP_cfg | PROP_ssa | PROP_alias, /* properties_required */
2112 0, /* properties_provided */
2113 0, /* properties_destroyed */
2114 0, /* todo_flags_start */
2115 TODO_dump_func | TODO_ggc_collect | TODO_verify_ssa /* todo_flags_finish */
2116 };