re PR tree-optimization/58464 (Crashes with SIGSEGV (infinite recursion in phi_transl...
[gcc.git] / gcc / tree-ssa-pre.c
1 /* SSA-PRE for trees.
2 Copyright (C) 2001-2013 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 3, 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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "tree.h"
27 #include "basic-block.h"
28 #include "gimple-pretty-print.h"
29 #include "tree-inline.h"
30 #include "tree-ssa.h"
31 #include "gimple.h"
32 #include "hash-table.h"
33 #include "tree-iterator.h"
34 #include "alloc-pool.h"
35 #include "obstack.h"
36 #include "tree-pass.h"
37 #include "flags.h"
38 #include "bitmap.h"
39 #include "langhooks.h"
40 #include "cfgloop.h"
41 #include "tree-ssa-sccvn.h"
42 #include "tree-scalar-evolution.h"
43 #include "params.h"
44 #include "dbgcnt.h"
45 #include "domwalk.h"
46 #include "ipa-prop.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. Strength reduction can be performed by anticipating expressions
54 we can repair later on.
55 3. We can do back-substitution or smarter value numbering to catch
56 commutative expressions split up over multiple statements.
57 */
58
59 /* For ease of terminology, "expression node" in the below refers to
60 every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
61 represent the actual statement containing the expressions we care about,
62 and we cache the value number by putting it in the expression. */
63
64 /* Basic algorithm
65
66 First we walk the statements to generate the AVAIL sets, the
67 EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
68 generation of values/expressions by a given block. We use them
69 when computing the ANTIC sets. The AVAIL sets consist of
70 SSA_NAME's that represent values, so we know what values are
71 available in what blocks. AVAIL is a forward dataflow problem. In
72 SSA, values are never killed, so we don't need a kill set, or a
73 fixpoint iteration, in order to calculate the AVAIL sets. In
74 traditional parlance, AVAIL sets tell us the downsafety of the
75 expressions/values.
76
77 Next, we generate the ANTIC sets. These sets represent the
78 anticipatable expressions. ANTIC is a backwards dataflow
79 problem. An expression is anticipatable in a given block if it could
80 be generated in that block. This means that if we had to perform
81 an insertion in that block, of the value of that expression, we
82 could. Calculating the ANTIC sets requires phi translation of
83 expressions, because the flow goes backwards through phis. We must
84 iterate to a fixpoint of the ANTIC sets, because we have a kill
85 set. Even in SSA form, values are not live over the entire
86 function, only from their definition point onwards. So we have to
87 remove values from the ANTIC set once we go past the definition
88 point of the leaders that make them up.
89 compute_antic/compute_antic_aux performs this computation.
90
91 Third, we perform insertions to make partially redundant
92 expressions fully redundant.
93
94 An expression is partially redundant (excluding partial
95 anticipation) if:
96
97 1. It is AVAIL in some, but not all, of the predecessors of a
98 given block.
99 2. It is ANTIC in all the predecessors.
100
101 In order to make it fully redundant, we insert the expression into
102 the predecessors where it is not available, but is ANTIC.
103
104 For the partial anticipation case, we only perform insertion if it
105 is partially anticipated in some block, and fully available in all
106 of the predecessors.
107
108 insert/insert_aux/do_regular_insertion/do_partial_partial_insertion
109 performs these steps.
110
111 Fourth, we eliminate fully redundant expressions.
112 This is a simple statement walk that replaces redundant
113 calculations with the now available values. */
114
115 /* Representations of value numbers:
116
117 Value numbers are represented by a representative SSA_NAME. We
118 will create fake SSA_NAME's in situations where we need a
119 representative but do not have one (because it is a complex
120 expression). In order to facilitate storing the value numbers in
121 bitmaps, and keep the number of wasted SSA_NAME's down, we also
122 associate a value_id with each value number, and create full blown
123 ssa_name's only where we actually need them (IE in operands of
124 existing expressions).
125
126 Theoretically you could replace all the value_id's with
127 SSA_NAME_VERSION, but this would allocate a large number of
128 SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
129 It would also require an additional indirection at each point we
130 use the value id. */
131
132 /* Representation of expressions on value numbers:
133
134 Expressions consisting of value numbers are represented the same
135 way as our VN internally represents them, with an additional
136 "pre_expr" wrapping around them in order to facilitate storing all
137 of the expressions in the same sets. */
138
139 /* Representation of sets:
140
141 The dataflow sets do not need to be sorted in any particular order
142 for the majority of their lifetime, are simply represented as two
143 bitmaps, one that keeps track of values present in the set, and one
144 that keeps track of expressions present in the set.
145
146 When we need them in topological order, we produce it on demand by
147 transforming the bitmap into an array and sorting it into topo
148 order. */
149
150 /* Type of expression, used to know which member of the PRE_EXPR union
151 is valid. */
152
153 enum pre_expr_kind
154 {
155 NAME,
156 NARY,
157 REFERENCE,
158 CONSTANT
159 };
160
161 typedef union pre_expr_union_d
162 {
163 tree name;
164 tree constant;
165 vn_nary_op_t nary;
166 vn_reference_t reference;
167 } pre_expr_union;
168
169 typedef struct pre_expr_d : typed_noop_remove <pre_expr_d>
170 {
171 enum pre_expr_kind kind;
172 unsigned int id;
173 pre_expr_union u;
174
175 /* hash_table support. */
176 typedef pre_expr_d value_type;
177 typedef pre_expr_d compare_type;
178 static inline hashval_t hash (const pre_expr_d *);
179 static inline int equal (const pre_expr_d *, const pre_expr_d *);
180 } *pre_expr;
181
182 #define PRE_EXPR_NAME(e) (e)->u.name
183 #define PRE_EXPR_NARY(e) (e)->u.nary
184 #define PRE_EXPR_REFERENCE(e) (e)->u.reference
185 #define PRE_EXPR_CONSTANT(e) (e)->u.constant
186
187 /* Compare E1 and E1 for equality. */
188
189 inline int
190 pre_expr_d::equal (const value_type *e1, const compare_type *e2)
191 {
192 if (e1->kind != e2->kind)
193 return false;
194
195 switch (e1->kind)
196 {
197 case CONSTANT:
198 return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
199 PRE_EXPR_CONSTANT (e2));
200 case NAME:
201 return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
202 case NARY:
203 return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
204 case REFERENCE:
205 return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
206 PRE_EXPR_REFERENCE (e2));
207 default:
208 gcc_unreachable ();
209 }
210 }
211
212 /* Hash E. */
213
214 inline hashval_t
215 pre_expr_d::hash (const value_type *e)
216 {
217 switch (e->kind)
218 {
219 case CONSTANT:
220 return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
221 case NAME:
222 return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
223 case NARY:
224 return PRE_EXPR_NARY (e)->hashcode;
225 case REFERENCE:
226 return PRE_EXPR_REFERENCE (e)->hashcode;
227 default:
228 gcc_unreachable ();
229 }
230 }
231
232 /* Next global expression id number. */
233 static unsigned int next_expression_id;
234
235 /* Mapping from expression to id number we can use in bitmap sets. */
236 static vec<pre_expr> expressions;
237 static hash_table <pre_expr_d> expression_to_id;
238 static vec<unsigned> name_to_id;
239
240 /* Allocate an expression id for EXPR. */
241
242 static inline unsigned int
243 alloc_expression_id (pre_expr expr)
244 {
245 struct pre_expr_d **slot;
246 /* Make sure we won't overflow. */
247 gcc_assert (next_expression_id + 1 > next_expression_id);
248 expr->id = next_expression_id++;
249 expressions.safe_push (expr);
250 if (expr->kind == NAME)
251 {
252 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
253 /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
254 re-allocations by using vec::reserve upfront. There is no
255 vec::quick_grow_cleared unfortunately. */
256 unsigned old_len = name_to_id.length ();
257 name_to_id.reserve (num_ssa_names - old_len);
258 name_to_id.safe_grow_cleared (num_ssa_names);
259 gcc_assert (name_to_id[version] == 0);
260 name_to_id[version] = expr->id;
261 }
262 else
263 {
264 slot = expression_to_id.find_slot (expr, INSERT);
265 gcc_assert (!*slot);
266 *slot = expr;
267 }
268 return next_expression_id - 1;
269 }
270
271 /* Return the expression id for tree EXPR. */
272
273 static inline unsigned int
274 get_expression_id (const pre_expr expr)
275 {
276 return expr->id;
277 }
278
279 static inline unsigned int
280 lookup_expression_id (const pre_expr expr)
281 {
282 struct pre_expr_d **slot;
283
284 if (expr->kind == NAME)
285 {
286 unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
287 if (name_to_id.length () <= version)
288 return 0;
289 return name_to_id[version];
290 }
291 else
292 {
293 slot = expression_to_id.find_slot (expr, NO_INSERT);
294 if (!slot)
295 return 0;
296 return ((pre_expr)*slot)->id;
297 }
298 }
299
300 /* Return the existing expression id for EXPR, or create one if one
301 does not exist yet. */
302
303 static inline unsigned int
304 get_or_alloc_expression_id (pre_expr expr)
305 {
306 unsigned int id = lookup_expression_id (expr);
307 if (id == 0)
308 return alloc_expression_id (expr);
309 return expr->id = id;
310 }
311
312 /* Return the expression that has expression id ID */
313
314 static inline pre_expr
315 expression_for_id (unsigned int id)
316 {
317 return expressions[id];
318 }
319
320 /* Free the expression id field in all of our expressions,
321 and then destroy the expressions array. */
322
323 static void
324 clear_expression_ids (void)
325 {
326 expressions.release ();
327 }
328
329 static alloc_pool pre_expr_pool;
330
331 /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
332
333 static pre_expr
334 get_or_alloc_expr_for_name (tree name)
335 {
336 struct pre_expr_d expr;
337 pre_expr result;
338 unsigned int result_id;
339
340 expr.kind = NAME;
341 expr.id = 0;
342 PRE_EXPR_NAME (&expr) = name;
343 result_id = lookup_expression_id (&expr);
344 if (result_id != 0)
345 return expression_for_id (result_id);
346
347 result = (pre_expr) pool_alloc (pre_expr_pool);
348 result->kind = NAME;
349 PRE_EXPR_NAME (result) = name;
350 alloc_expression_id (result);
351 return result;
352 }
353
354 /* An unordered bitmap set. One bitmap tracks values, the other,
355 expressions. */
356 typedef struct bitmap_set
357 {
358 bitmap_head expressions;
359 bitmap_head values;
360 } *bitmap_set_t;
361
362 #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
363 EXECUTE_IF_SET_IN_BITMAP(&(set)->expressions, 0, (id), (bi))
364
365 #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
366 EXECUTE_IF_SET_IN_BITMAP(&(set)->values, 0, (id), (bi))
367
368 /* Mapping from value id to expressions with that value_id. */
369 static vec<bitmap> value_expressions;
370
371 /* Sets that we need to keep track of. */
372 typedef struct bb_bitmap_sets
373 {
374 /* The EXP_GEN set, which represents expressions/values generated in
375 a basic block. */
376 bitmap_set_t exp_gen;
377
378 /* The PHI_GEN set, which represents PHI results generated in a
379 basic block. */
380 bitmap_set_t phi_gen;
381
382 /* The TMP_GEN set, which represents results/temporaries generated
383 in a basic block. IE the LHS of an expression. */
384 bitmap_set_t tmp_gen;
385
386 /* The AVAIL_OUT set, which represents which values are available in
387 a given basic block. */
388 bitmap_set_t avail_out;
389
390 /* The ANTIC_IN set, which represents which values are anticipatable
391 in a given basic block. */
392 bitmap_set_t antic_in;
393
394 /* The PA_IN set, which represents which values are
395 partially anticipatable in a given basic block. */
396 bitmap_set_t pa_in;
397
398 /* The NEW_SETS set, which is used during insertion to augment the
399 AVAIL_OUT set of blocks with the new insertions performed during
400 the current iteration. */
401 bitmap_set_t new_sets;
402
403 /* A cache for value_dies_in_block_x. */
404 bitmap expr_dies;
405
406 /* True if we have visited this block during ANTIC calculation. */
407 unsigned int visited : 1;
408
409 /* True we have deferred processing this block during ANTIC
410 calculation until its successor is processed. */
411 unsigned int deferred : 1;
412
413 /* True when the block contains a call that might not return. */
414 unsigned int contains_may_not_return_call : 1;
415 } *bb_value_sets_t;
416
417 #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
418 #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
419 #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
420 #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
421 #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
422 #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
423 #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
424 #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
425 #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
426 #define BB_DEFERRED(BB) ((bb_value_sets_t) ((BB)->aux))->deferred
427 #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
428
429
430 /* Basic block list in postorder. */
431 static int *postorder;
432 static int postorder_num;
433
434 /* This structure is used to keep track of statistics on what
435 optimization PRE was able to perform. */
436 static struct
437 {
438 /* The number of RHS computations eliminated by PRE. */
439 int eliminations;
440
441 /* The number of new expressions/temporaries generated by PRE. */
442 int insertions;
443
444 /* The number of inserts found due to partial anticipation */
445 int pa_insert;
446
447 /* The number of new PHI nodes added by PRE. */
448 int phis;
449 } pre_stats;
450
451 static bool do_partial_partial;
452 static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
453 static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
454 static void bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
455 static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
456 static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
457 static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
458 static void bitmap_insert_into_set_1 (bitmap_set_t, pre_expr,
459 unsigned int, bool);
460 static bitmap_set_t bitmap_set_new (void);
461 static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
462 tree);
463 static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
464 static unsigned int get_expr_value_id (pre_expr);
465
466 /* We can add and remove elements and entries to and from sets
467 and hash tables, so we use alloc pools for them. */
468
469 static alloc_pool bitmap_set_pool;
470 static bitmap_obstack grand_bitmap_obstack;
471
472 /* Set of blocks with statements that have had their EH properties changed. */
473 static bitmap need_eh_cleanup;
474
475 /* Set of blocks with statements that have had their AB properties changed. */
476 static bitmap need_ab_cleanup;
477
478 /* A three tuple {e, pred, v} used to cache phi translations in the
479 phi_translate_table. */
480
481 typedef struct expr_pred_trans_d : typed_free_remove<expr_pred_trans_d>
482 {
483 /* The expression. */
484 pre_expr e;
485
486 /* The predecessor block along which we translated the expression. */
487 basic_block pred;
488
489 /* The value that resulted from the translation. */
490 pre_expr v;
491
492 /* The hashcode for the expression, pred pair. This is cached for
493 speed reasons. */
494 hashval_t hashcode;
495
496 /* hash_table support. */
497 typedef expr_pred_trans_d value_type;
498 typedef expr_pred_trans_d compare_type;
499 static inline hashval_t hash (const value_type *);
500 static inline int equal (const value_type *, const compare_type *);
501 } *expr_pred_trans_t;
502 typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
503
504 inline hashval_t
505 expr_pred_trans_d::hash (const expr_pred_trans_d *e)
506 {
507 return e->hashcode;
508 }
509
510 inline int
511 expr_pred_trans_d::equal (const value_type *ve1,
512 const compare_type *ve2)
513 {
514 basic_block b1 = ve1->pred;
515 basic_block b2 = ve2->pred;
516
517 /* If they are not translations for the same basic block, they can't
518 be equal. */
519 if (b1 != b2)
520 return false;
521 return pre_expr_d::equal (ve1->e, ve2->e);
522 }
523
524 /* The phi_translate_table caches phi translations for a given
525 expression and predecessor. */
526 static hash_table <expr_pred_trans_d> phi_translate_table;
527
528 /* Add the tuple mapping from {expression E, basic block PRED} to
529 the phi translation table and return whether it pre-existed. */
530
531 static inline bool
532 phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
533 {
534 expr_pred_trans_t *slot;
535 expr_pred_trans_d tem;
536 hashval_t hash = iterative_hash_hashval_t (pre_expr_d::hash (e),
537 pred->index);
538 tem.e = e;
539 tem.pred = pred;
540 tem.hashcode = hash;
541 slot = phi_translate_table.find_slot_with_hash (&tem, hash, INSERT);
542 if (*slot)
543 {
544 *entry = *slot;
545 return true;
546 }
547
548 *entry = *slot = XNEW (struct expr_pred_trans_d);
549 (*entry)->e = e;
550 (*entry)->pred = pred;
551 (*entry)->hashcode = hash;
552 return false;
553 }
554
555
556 /* Add expression E to the expression set of value id V. */
557
558 static void
559 add_to_value (unsigned int v, pre_expr e)
560 {
561 bitmap set;
562
563 gcc_checking_assert (get_expr_value_id (e) == v);
564
565 if (v >= value_expressions.length ())
566 {
567 value_expressions.safe_grow_cleared (v + 1);
568 }
569
570 set = value_expressions[v];
571 if (!set)
572 {
573 set = BITMAP_ALLOC (&grand_bitmap_obstack);
574 value_expressions[v] = set;
575 }
576
577 bitmap_set_bit (set, get_or_alloc_expression_id (e));
578 }
579
580 /* Create a new bitmap set and return it. */
581
582 static bitmap_set_t
583 bitmap_set_new (void)
584 {
585 bitmap_set_t ret = (bitmap_set_t) pool_alloc (bitmap_set_pool);
586 bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
587 bitmap_initialize (&ret->values, &grand_bitmap_obstack);
588 return ret;
589 }
590
591 /* Return the value id for a PRE expression EXPR. */
592
593 static unsigned int
594 get_expr_value_id (pre_expr expr)
595 {
596 unsigned int id;
597 switch (expr->kind)
598 {
599 case CONSTANT:
600 id = get_constant_value_id (PRE_EXPR_CONSTANT (expr));
601 break;
602 case NAME:
603 id = VN_INFO (PRE_EXPR_NAME (expr))->value_id;
604 break;
605 case NARY:
606 id = PRE_EXPR_NARY (expr)->value_id;
607 break;
608 case REFERENCE:
609 id = PRE_EXPR_REFERENCE (expr)->value_id;
610 break;
611 default:
612 gcc_unreachable ();
613 }
614 /* ??? We cannot assert that expr has a value-id (it can be 0), because
615 we assign value-ids only to expressions that have a result
616 in set_hashtable_value_ids. */
617 return id;
618 }
619
620 /* Return a SCCVN valnum (SSA name or constant) for the PRE value-id VAL. */
621
622 static tree
623 sccvn_valnum_from_value_id (unsigned int val)
624 {
625 bitmap_iterator bi;
626 unsigned int i;
627 bitmap exprset = value_expressions[val];
628 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
629 {
630 pre_expr vexpr = expression_for_id (i);
631 if (vexpr->kind == NAME)
632 return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
633 else if (vexpr->kind == CONSTANT)
634 return PRE_EXPR_CONSTANT (vexpr);
635 }
636 return NULL_TREE;
637 }
638
639 /* Remove an expression EXPR from a bitmapped set. */
640
641 static void
642 bitmap_remove_from_set (bitmap_set_t set, pre_expr expr)
643 {
644 unsigned int val = get_expr_value_id (expr);
645 if (!value_id_constant_p (val))
646 {
647 bitmap_clear_bit (&set->values, val);
648 bitmap_clear_bit (&set->expressions, get_expression_id (expr));
649 }
650 }
651
652 static void
653 bitmap_insert_into_set_1 (bitmap_set_t set, pre_expr expr,
654 unsigned int val, bool allow_constants)
655 {
656 if (allow_constants || !value_id_constant_p (val))
657 {
658 /* We specifically expect this and only this function to be able to
659 insert constants into a set. */
660 bitmap_set_bit (&set->values, val);
661 bitmap_set_bit (&set->expressions, get_or_alloc_expression_id (expr));
662 }
663 }
664
665 /* Insert an expression EXPR into a bitmapped set. */
666
667 static void
668 bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
669 {
670 bitmap_insert_into_set_1 (set, expr, get_expr_value_id (expr), false);
671 }
672
673 /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
674
675 static void
676 bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
677 {
678 bitmap_copy (&dest->expressions, &orig->expressions);
679 bitmap_copy (&dest->values, &orig->values);
680 }
681
682
683 /* Free memory used up by SET. */
684 static void
685 bitmap_set_free (bitmap_set_t set)
686 {
687 bitmap_clear (&set->expressions);
688 bitmap_clear (&set->values);
689 }
690
691
692 /* Generate an topological-ordered array of bitmap set SET. */
693
694 static vec<pre_expr>
695 sorted_array_from_bitmap_set (bitmap_set_t set)
696 {
697 unsigned int i, j;
698 bitmap_iterator bi, bj;
699 vec<pre_expr> result;
700
701 /* Pre-allocate roughly enough space for the array. */
702 result.create (bitmap_count_bits (&set->values));
703
704 FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
705 {
706 /* The number of expressions having a given value is usually
707 relatively small. Thus, rather than making a vector of all
708 the expressions and sorting it by value-id, we walk the values
709 and check in the reverse mapping that tells us what expressions
710 have a given value, to filter those in our set. As a result,
711 the expressions are inserted in value-id order, which means
712 topological order.
713
714 If this is somehow a significant lose for some cases, we can
715 choose which set to walk based on the set size. */
716 bitmap exprset = value_expressions[i];
717 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, j, bj)
718 {
719 if (bitmap_bit_p (&set->expressions, j))
720 result.safe_push (expression_for_id (j));
721 }
722 }
723
724 return result;
725 }
726
727 /* Perform bitmapped set operation DEST &= ORIG. */
728
729 static void
730 bitmap_set_and (bitmap_set_t dest, bitmap_set_t orig)
731 {
732 bitmap_iterator bi;
733 unsigned int i;
734
735 if (dest != orig)
736 {
737 bitmap_head temp;
738 bitmap_initialize (&temp, &grand_bitmap_obstack);
739
740 bitmap_and_into (&dest->values, &orig->values);
741 bitmap_copy (&temp, &dest->expressions);
742 EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
743 {
744 pre_expr expr = expression_for_id (i);
745 unsigned int value_id = get_expr_value_id (expr);
746 if (!bitmap_bit_p (&dest->values, value_id))
747 bitmap_clear_bit (&dest->expressions, i);
748 }
749 bitmap_clear (&temp);
750 }
751 }
752
753 /* Subtract all values and expressions contained in ORIG from DEST. */
754
755 static bitmap_set_t
756 bitmap_set_subtract (bitmap_set_t dest, bitmap_set_t orig)
757 {
758 bitmap_set_t result = bitmap_set_new ();
759 bitmap_iterator bi;
760 unsigned int i;
761
762 bitmap_and_compl (&result->expressions, &dest->expressions,
763 &orig->expressions);
764
765 FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
766 {
767 pre_expr expr = expression_for_id (i);
768 unsigned int value_id = get_expr_value_id (expr);
769 bitmap_set_bit (&result->values, value_id);
770 }
771
772 return result;
773 }
774
775 /* Subtract all the values in bitmap set B from bitmap set A. */
776
777 static void
778 bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
779 {
780 unsigned int i;
781 bitmap_iterator bi;
782 bitmap_head temp;
783
784 bitmap_initialize (&temp, &grand_bitmap_obstack);
785
786 bitmap_copy (&temp, &a->expressions);
787 EXECUTE_IF_SET_IN_BITMAP (&temp, 0, i, bi)
788 {
789 pre_expr expr = expression_for_id (i);
790 if (bitmap_set_contains_value (b, get_expr_value_id (expr)))
791 bitmap_remove_from_set (a, expr);
792 }
793 bitmap_clear (&temp);
794 }
795
796
797 /* Return true if bitmapped set SET contains the value VALUE_ID. */
798
799 static bool
800 bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
801 {
802 if (value_id_constant_p (value_id))
803 return true;
804
805 if (!set || bitmap_empty_p (&set->expressions))
806 return false;
807
808 return bitmap_bit_p (&set->values, value_id);
809 }
810
811 static inline bool
812 bitmap_set_contains_expr (bitmap_set_t set, const pre_expr expr)
813 {
814 return bitmap_bit_p (&set->expressions, get_expression_id (expr));
815 }
816
817 /* Replace an instance of value LOOKFOR with expression EXPR in SET. */
818
819 static void
820 bitmap_set_replace_value (bitmap_set_t set, unsigned int lookfor,
821 const pre_expr expr)
822 {
823 bitmap exprset;
824 unsigned int i;
825 bitmap_iterator bi;
826
827 if (value_id_constant_p (lookfor))
828 return;
829
830 if (!bitmap_set_contains_value (set, lookfor))
831 return;
832
833 /* The number of expressions having a given value is usually
834 significantly less than the total number of expressions in SET.
835 Thus, rather than check, for each expression in SET, whether it
836 has the value LOOKFOR, we walk the reverse mapping that tells us
837 what expressions have a given value, and see if any of those
838 expressions are in our set. For large testcases, this is about
839 5-10x faster than walking the bitmap. If this is somehow a
840 significant lose for some cases, we can choose which set to walk
841 based on the set size. */
842 exprset = value_expressions[lookfor];
843 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
844 {
845 if (bitmap_clear_bit (&set->expressions, i))
846 {
847 bitmap_set_bit (&set->expressions, get_expression_id (expr));
848 return;
849 }
850 }
851
852 gcc_unreachable ();
853 }
854
855 /* Return true if two bitmap sets are equal. */
856
857 static bool
858 bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
859 {
860 return bitmap_equal_p (&a->values, &b->values);
861 }
862
863 /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
864 and add it otherwise. */
865
866 static void
867 bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
868 {
869 unsigned int val = get_expr_value_id (expr);
870
871 if (bitmap_set_contains_value (set, val))
872 bitmap_set_replace_value (set, val, expr);
873 else
874 bitmap_insert_into_set (set, expr);
875 }
876
877 /* Insert EXPR into SET if EXPR's value is not already present in
878 SET. */
879
880 static void
881 bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
882 {
883 unsigned int val = get_expr_value_id (expr);
884
885 gcc_checking_assert (expr->id == get_or_alloc_expression_id (expr));
886
887 /* Constant values are always considered to be part of the set. */
888 if (value_id_constant_p (val))
889 return;
890
891 /* If the value membership changed, add the expression. */
892 if (bitmap_set_bit (&set->values, val))
893 bitmap_set_bit (&set->expressions, expr->id);
894 }
895
896 /* Print out EXPR to outfile. */
897
898 static void
899 print_pre_expr (FILE *outfile, const pre_expr expr)
900 {
901 switch (expr->kind)
902 {
903 case CONSTANT:
904 print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr), 0);
905 break;
906 case NAME:
907 print_generic_expr (outfile, PRE_EXPR_NAME (expr), 0);
908 break;
909 case NARY:
910 {
911 unsigned int i;
912 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
913 fprintf (outfile, "{%s,", tree_code_name [nary->opcode]);
914 for (i = 0; i < nary->length; i++)
915 {
916 print_generic_expr (outfile, nary->op[i], 0);
917 if (i != (unsigned) nary->length - 1)
918 fprintf (outfile, ",");
919 }
920 fprintf (outfile, "}");
921 }
922 break;
923
924 case REFERENCE:
925 {
926 vn_reference_op_t vro;
927 unsigned int i;
928 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
929 fprintf (outfile, "{");
930 for (i = 0;
931 ref->operands.iterate (i, &vro);
932 i++)
933 {
934 bool closebrace = false;
935 if (vro->opcode != SSA_NAME
936 && TREE_CODE_CLASS (vro->opcode) != tcc_declaration)
937 {
938 fprintf (outfile, "%s", tree_code_name [vro->opcode]);
939 if (vro->op0)
940 {
941 fprintf (outfile, "<");
942 closebrace = true;
943 }
944 }
945 if (vro->op0)
946 {
947 print_generic_expr (outfile, vro->op0, 0);
948 if (vro->op1)
949 {
950 fprintf (outfile, ",");
951 print_generic_expr (outfile, vro->op1, 0);
952 }
953 if (vro->op2)
954 {
955 fprintf (outfile, ",");
956 print_generic_expr (outfile, vro->op2, 0);
957 }
958 }
959 if (closebrace)
960 fprintf (outfile, ">");
961 if (i != ref->operands.length () - 1)
962 fprintf (outfile, ",");
963 }
964 fprintf (outfile, "}");
965 if (ref->vuse)
966 {
967 fprintf (outfile, "@");
968 print_generic_expr (outfile, ref->vuse, 0);
969 }
970 }
971 break;
972 }
973 }
974 void debug_pre_expr (pre_expr);
975
976 /* Like print_pre_expr but always prints to stderr. */
977 DEBUG_FUNCTION void
978 debug_pre_expr (pre_expr e)
979 {
980 print_pre_expr (stderr, e);
981 fprintf (stderr, "\n");
982 }
983
984 /* Print out SET to OUTFILE. */
985
986 static void
987 print_bitmap_set (FILE *outfile, bitmap_set_t set,
988 const char *setname, int blockindex)
989 {
990 fprintf (outfile, "%s[%d] := { ", setname, blockindex);
991 if (set)
992 {
993 bool first = true;
994 unsigned i;
995 bitmap_iterator bi;
996
997 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
998 {
999 const pre_expr expr = expression_for_id (i);
1000
1001 if (!first)
1002 fprintf (outfile, ", ");
1003 first = false;
1004 print_pre_expr (outfile, expr);
1005
1006 fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1007 }
1008 }
1009 fprintf (outfile, " }\n");
1010 }
1011
1012 void debug_bitmap_set (bitmap_set_t);
1013
1014 DEBUG_FUNCTION void
1015 debug_bitmap_set (bitmap_set_t set)
1016 {
1017 print_bitmap_set (stderr, set, "debug", 0);
1018 }
1019
1020 void debug_bitmap_sets_for (basic_block);
1021
1022 DEBUG_FUNCTION void
1023 debug_bitmap_sets_for (basic_block bb)
1024 {
1025 print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1026 print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1027 print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1028 print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1029 print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1030 if (do_partial_partial)
1031 print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1032 print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1033 }
1034
1035 /* Print out the expressions that have VAL to OUTFILE. */
1036
1037 static void
1038 print_value_expressions (FILE *outfile, unsigned int val)
1039 {
1040 bitmap set = value_expressions[val];
1041 if (set)
1042 {
1043 bitmap_set x;
1044 char s[10];
1045 sprintf (s, "%04d", val);
1046 x.expressions = *set;
1047 print_bitmap_set (outfile, &x, s, 0);
1048 }
1049 }
1050
1051
1052 DEBUG_FUNCTION void
1053 debug_value_expressions (unsigned int val)
1054 {
1055 print_value_expressions (stderr, val);
1056 }
1057
1058 /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1059 represent it. */
1060
1061 static pre_expr
1062 get_or_alloc_expr_for_constant (tree constant)
1063 {
1064 unsigned int result_id;
1065 unsigned int value_id;
1066 struct pre_expr_d expr;
1067 pre_expr newexpr;
1068
1069 expr.kind = CONSTANT;
1070 PRE_EXPR_CONSTANT (&expr) = constant;
1071 result_id = lookup_expression_id (&expr);
1072 if (result_id != 0)
1073 return expression_for_id (result_id);
1074
1075 newexpr = (pre_expr) pool_alloc (pre_expr_pool);
1076 newexpr->kind = CONSTANT;
1077 PRE_EXPR_CONSTANT (newexpr) = constant;
1078 alloc_expression_id (newexpr);
1079 value_id = get_or_alloc_constant_value_id (constant);
1080 add_to_value (value_id, newexpr);
1081 return newexpr;
1082 }
1083
1084 /* Given a value id V, find the actual tree representing the constant
1085 value if there is one, and return it. Return NULL if we can't find
1086 a constant. */
1087
1088 static tree
1089 get_constant_for_value_id (unsigned int v)
1090 {
1091 if (value_id_constant_p (v))
1092 {
1093 unsigned int i;
1094 bitmap_iterator bi;
1095 bitmap exprset = value_expressions[v];
1096
1097 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1098 {
1099 pre_expr expr = expression_for_id (i);
1100 if (expr->kind == CONSTANT)
1101 return PRE_EXPR_CONSTANT (expr);
1102 }
1103 }
1104 return NULL;
1105 }
1106
1107 /* Get or allocate a pre_expr for a piece of GIMPLE, and return it.
1108 Currently only supports constants and SSA_NAMES. */
1109 static pre_expr
1110 get_or_alloc_expr_for (tree t)
1111 {
1112 if (TREE_CODE (t) == SSA_NAME)
1113 return get_or_alloc_expr_for_name (t);
1114 else if (is_gimple_min_invariant (t))
1115 return get_or_alloc_expr_for_constant (t);
1116 else
1117 {
1118 /* More complex expressions can result from SCCVN expression
1119 simplification that inserts values for them. As they all
1120 do not have VOPs the get handled by the nary ops struct. */
1121 vn_nary_op_t result;
1122 unsigned int result_id;
1123 vn_nary_op_lookup (t, &result);
1124 if (result != NULL)
1125 {
1126 pre_expr e = (pre_expr) pool_alloc (pre_expr_pool);
1127 e->kind = NARY;
1128 PRE_EXPR_NARY (e) = result;
1129 result_id = lookup_expression_id (e);
1130 if (result_id != 0)
1131 {
1132 pool_free (pre_expr_pool, e);
1133 e = expression_for_id (result_id);
1134 return e;
1135 }
1136 alloc_expression_id (e);
1137 return e;
1138 }
1139 }
1140 return NULL;
1141 }
1142
1143 /* Return the folded version of T if T, when folded, is a gimple
1144 min_invariant. Otherwise, return T. */
1145
1146 static pre_expr
1147 fully_constant_expression (pre_expr e)
1148 {
1149 switch (e->kind)
1150 {
1151 case CONSTANT:
1152 return e;
1153 case NARY:
1154 {
1155 vn_nary_op_t nary = PRE_EXPR_NARY (e);
1156 switch (TREE_CODE_CLASS (nary->opcode))
1157 {
1158 case tcc_binary:
1159 case tcc_comparison:
1160 {
1161 /* We have to go from trees to pre exprs to value ids to
1162 constants. */
1163 tree naryop0 = nary->op[0];
1164 tree naryop1 = nary->op[1];
1165 tree result;
1166 if (!is_gimple_min_invariant (naryop0))
1167 {
1168 pre_expr rep0 = get_or_alloc_expr_for (naryop0);
1169 unsigned int vrep0 = get_expr_value_id (rep0);
1170 tree const0 = get_constant_for_value_id (vrep0);
1171 if (const0)
1172 naryop0 = fold_convert (TREE_TYPE (naryop0), const0);
1173 }
1174 if (!is_gimple_min_invariant (naryop1))
1175 {
1176 pre_expr rep1 = get_or_alloc_expr_for (naryop1);
1177 unsigned int vrep1 = get_expr_value_id (rep1);
1178 tree const1 = get_constant_for_value_id (vrep1);
1179 if (const1)
1180 naryop1 = fold_convert (TREE_TYPE (naryop1), const1);
1181 }
1182 result = fold_binary (nary->opcode, nary->type,
1183 naryop0, naryop1);
1184 if (result && is_gimple_min_invariant (result))
1185 return get_or_alloc_expr_for_constant (result);
1186 /* We might have simplified the expression to a
1187 SSA_NAME for example from x_1 * 1. But we cannot
1188 insert a PHI for x_1 unconditionally as x_1 might
1189 not be available readily. */
1190 return e;
1191 }
1192 case tcc_reference:
1193 if (nary->opcode != REALPART_EXPR
1194 && nary->opcode != IMAGPART_EXPR
1195 && nary->opcode != VIEW_CONVERT_EXPR)
1196 return e;
1197 /* Fallthrough. */
1198 case tcc_unary:
1199 {
1200 /* We have to go from trees to pre exprs to value ids to
1201 constants. */
1202 tree naryop0 = nary->op[0];
1203 tree const0, result;
1204 if (is_gimple_min_invariant (naryop0))
1205 const0 = naryop0;
1206 else
1207 {
1208 pre_expr rep0 = get_or_alloc_expr_for (naryop0);
1209 unsigned int vrep0 = get_expr_value_id (rep0);
1210 const0 = get_constant_for_value_id (vrep0);
1211 }
1212 result = NULL;
1213 if (const0)
1214 {
1215 tree type1 = TREE_TYPE (nary->op[0]);
1216 const0 = fold_convert (type1, const0);
1217 result = fold_unary (nary->opcode, nary->type, const0);
1218 }
1219 if (result && is_gimple_min_invariant (result))
1220 return get_or_alloc_expr_for_constant (result);
1221 return e;
1222 }
1223 default:
1224 return e;
1225 }
1226 }
1227 case REFERENCE:
1228 {
1229 vn_reference_t ref = PRE_EXPR_REFERENCE (e);
1230 tree folded;
1231 if ((folded = fully_constant_vn_reference_p (ref)))
1232 return get_or_alloc_expr_for_constant (folded);
1233 return e;
1234 }
1235 default:
1236 return e;
1237 }
1238 return e;
1239 }
1240
1241 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1242 it has the value it would have in BLOCK. Set *SAME_VALID to true
1243 in case the new vuse doesn't change the value id of the OPERANDS. */
1244
1245 static tree
1246 translate_vuse_through_block (vec<vn_reference_op_s> operands,
1247 alias_set_type set, tree type, tree vuse,
1248 basic_block phiblock,
1249 basic_block block, bool *same_valid)
1250 {
1251 gimple phi = SSA_NAME_DEF_STMT (vuse);
1252 ao_ref ref;
1253 edge e = NULL;
1254 bool use_oracle;
1255
1256 *same_valid = true;
1257
1258 if (gimple_bb (phi) != phiblock)
1259 return vuse;
1260
1261 use_oracle = ao_ref_init_from_vn_reference (&ref, set, type, operands);
1262
1263 /* Use the alias-oracle to find either the PHI node in this block,
1264 the first VUSE used in this block that is equivalent to vuse or
1265 the first VUSE which definition in this block kills the value. */
1266 if (gimple_code (phi) == GIMPLE_PHI)
1267 e = find_edge (block, phiblock);
1268 else if (use_oracle)
1269 while (!stmt_may_clobber_ref_p_1 (phi, &ref))
1270 {
1271 vuse = gimple_vuse (phi);
1272 phi = SSA_NAME_DEF_STMT (vuse);
1273 if (gimple_bb (phi) != phiblock)
1274 return vuse;
1275 if (gimple_code (phi) == GIMPLE_PHI)
1276 {
1277 e = find_edge (block, phiblock);
1278 break;
1279 }
1280 }
1281 else
1282 return NULL_TREE;
1283
1284 if (e)
1285 {
1286 if (use_oracle)
1287 {
1288 bitmap visited = NULL;
1289 unsigned int cnt;
1290 /* Try to find a vuse that dominates this phi node by skipping
1291 non-clobbering statements. */
1292 vuse = get_continuation_for_phi (phi, &ref, &cnt, &visited, false);
1293 if (visited)
1294 BITMAP_FREE (visited);
1295 }
1296 else
1297 vuse = NULL_TREE;
1298 if (!vuse)
1299 {
1300 /* If we didn't find any, the value ID can't stay the same,
1301 but return the translated vuse. */
1302 *same_valid = false;
1303 vuse = PHI_ARG_DEF (phi, e->dest_idx);
1304 }
1305 /* ??? We would like to return vuse here as this is the canonical
1306 upmost vdef that this reference is associated with. But during
1307 insertion of the references into the hash tables we only ever
1308 directly insert with their direct gimple_vuse, hence returning
1309 something else would make us not find the other expression. */
1310 return PHI_ARG_DEF (phi, e->dest_idx);
1311 }
1312
1313 return NULL_TREE;
1314 }
1315
1316 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1317 SET2. This is used to avoid making a set consisting of the union
1318 of PA_IN and ANTIC_IN during insert. */
1319
1320 static inline pre_expr
1321 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2)
1322 {
1323 pre_expr result;
1324
1325 result = bitmap_find_leader (set1, val);
1326 if (!result && set2)
1327 result = bitmap_find_leader (set2, val);
1328 return result;
1329 }
1330
1331 /* Get the tree type for our PRE expression e. */
1332
1333 static tree
1334 get_expr_type (const pre_expr e)
1335 {
1336 switch (e->kind)
1337 {
1338 case NAME:
1339 return TREE_TYPE (PRE_EXPR_NAME (e));
1340 case CONSTANT:
1341 return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1342 case REFERENCE:
1343 return PRE_EXPR_REFERENCE (e)->type;
1344 case NARY:
1345 return PRE_EXPR_NARY (e)->type;
1346 }
1347 gcc_unreachable();
1348 }
1349
1350 /* Get a representative SSA_NAME for a given expression.
1351 Since all of our sub-expressions are treated as values, we require
1352 them to be SSA_NAME's for simplicity.
1353 Prior versions of GVNPRE used to use "value handles" here, so that
1354 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1355 either case, the operands are really values (IE we do not expect
1356 them to be usable without finding leaders). */
1357
1358 static tree
1359 get_representative_for (const pre_expr e)
1360 {
1361 tree name;
1362 unsigned int value_id = get_expr_value_id (e);
1363
1364 switch (e->kind)
1365 {
1366 case NAME:
1367 return PRE_EXPR_NAME (e);
1368 case CONSTANT:
1369 return PRE_EXPR_CONSTANT (e);
1370 case NARY:
1371 case REFERENCE:
1372 {
1373 /* Go through all of the expressions representing this value
1374 and pick out an SSA_NAME. */
1375 unsigned int i;
1376 bitmap_iterator bi;
1377 bitmap exprs = value_expressions[value_id];
1378 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1379 {
1380 pre_expr rep = expression_for_id (i);
1381 if (rep->kind == NAME)
1382 return PRE_EXPR_NAME (rep);
1383 else if (rep->kind == CONSTANT)
1384 return PRE_EXPR_CONSTANT (rep);
1385 }
1386 }
1387 break;
1388 }
1389
1390 /* If we reached here we couldn't find an SSA_NAME. This can
1391 happen when we've discovered a value that has never appeared in
1392 the program as set to an SSA_NAME, as the result of phi translation.
1393 Create one here.
1394 ??? We should be able to re-use this when we insert the statement
1395 to compute it. */
1396 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1397 VN_INFO_GET (name)->value_id = value_id;
1398 VN_INFO (name)->valnum = name;
1399 /* ??? For now mark this SSA name for release by SCCVN. */
1400 VN_INFO (name)->needs_insertion = true;
1401 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1402 if (dump_file && (dump_flags & TDF_DETAILS))
1403 {
1404 fprintf (dump_file, "Created SSA_NAME representative ");
1405 print_generic_expr (dump_file, name, 0);
1406 fprintf (dump_file, " for expression:");
1407 print_pre_expr (dump_file, e);
1408 fprintf (dump_file, " (%04d)\n", value_id);
1409 }
1410
1411 return name;
1412 }
1413
1414
1415
1416 static pre_expr
1417 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1418 basic_block pred, basic_block phiblock);
1419
1420 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1421 the phis in PRED. Return NULL if we can't find a leader for each part
1422 of the translated expression. */
1423
1424 static pre_expr
1425 phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1426 basic_block pred, basic_block phiblock)
1427 {
1428 switch (expr->kind)
1429 {
1430 case NARY:
1431 {
1432 unsigned int i;
1433 bool changed = false;
1434 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1435 vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1436 sizeof_vn_nary_op (nary->length));
1437 memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1438
1439 for (i = 0; i < newnary->length; i++)
1440 {
1441 if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1442 continue;
1443 else
1444 {
1445 pre_expr leader, result;
1446 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1447 leader = find_leader_in_sets (op_val_id, set1, set2);
1448 result = phi_translate (leader, set1, set2, pred, phiblock);
1449 if (result && result != leader)
1450 {
1451 tree name = get_representative_for (result);
1452 if (!name)
1453 return NULL;
1454 newnary->op[i] = name;
1455 }
1456 else if (!result)
1457 return NULL;
1458
1459 changed |= newnary->op[i] != nary->op[i];
1460 }
1461 }
1462 if (changed)
1463 {
1464 pre_expr constant;
1465 unsigned int new_val_id;
1466
1467 tree result = vn_nary_op_lookup_pieces (newnary->length,
1468 newnary->opcode,
1469 newnary->type,
1470 &newnary->op[0],
1471 &nary);
1472 if (result && is_gimple_min_invariant (result))
1473 return get_or_alloc_expr_for_constant (result);
1474
1475 expr = (pre_expr) pool_alloc (pre_expr_pool);
1476 expr->kind = NARY;
1477 expr->id = 0;
1478 if (nary)
1479 {
1480 PRE_EXPR_NARY (expr) = nary;
1481 constant = fully_constant_expression (expr);
1482 if (constant != expr)
1483 return constant;
1484
1485 new_val_id = nary->value_id;
1486 get_or_alloc_expression_id (expr);
1487 }
1488 else
1489 {
1490 new_val_id = get_next_value_id ();
1491 value_expressions.safe_grow_cleared (get_max_value_id() + 1);
1492 nary = vn_nary_op_insert_pieces (newnary->length,
1493 newnary->opcode,
1494 newnary->type,
1495 &newnary->op[0],
1496 result, new_val_id);
1497 PRE_EXPR_NARY (expr) = nary;
1498 constant = fully_constant_expression (expr);
1499 if (constant != expr)
1500 return constant;
1501 get_or_alloc_expression_id (expr);
1502 }
1503 add_to_value (new_val_id, expr);
1504 }
1505 return expr;
1506 }
1507 break;
1508
1509 case REFERENCE:
1510 {
1511 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1512 vec<vn_reference_op_s> operands = ref->operands;
1513 tree vuse = ref->vuse;
1514 tree newvuse = vuse;
1515 vec<vn_reference_op_s> newoperands = vNULL;
1516 bool changed = false, same_valid = true;
1517 unsigned int i, j, n;
1518 vn_reference_op_t operand;
1519 vn_reference_t newref;
1520
1521 for (i = 0, j = 0;
1522 operands.iterate (i, &operand); i++, j++)
1523 {
1524 pre_expr opresult;
1525 pre_expr leader;
1526 tree op[3];
1527 tree type = operand->type;
1528 vn_reference_op_s newop = *operand;
1529 op[0] = operand->op0;
1530 op[1] = operand->op1;
1531 op[2] = operand->op2;
1532 for (n = 0; n < 3; ++n)
1533 {
1534 unsigned int op_val_id;
1535 if (!op[n])
1536 continue;
1537 if (TREE_CODE (op[n]) != SSA_NAME)
1538 {
1539 /* We can't possibly insert these. */
1540 if (n != 0
1541 && !is_gimple_min_invariant (op[n]))
1542 break;
1543 continue;
1544 }
1545 op_val_id = VN_INFO (op[n])->value_id;
1546 leader = find_leader_in_sets (op_val_id, set1, set2);
1547 if (!leader)
1548 break;
1549 opresult = phi_translate (leader, set1, set2, pred, phiblock);
1550 if (!opresult)
1551 break;
1552 if (opresult != leader)
1553 {
1554 tree name = get_representative_for (opresult);
1555 if (!name)
1556 break;
1557 changed |= name != op[n];
1558 op[n] = name;
1559 }
1560 }
1561 if (n != 3)
1562 {
1563 newoperands.release ();
1564 return NULL;
1565 }
1566 if (!newoperands.exists ())
1567 newoperands = operands.copy ();
1568 /* We may have changed from an SSA_NAME to a constant */
1569 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1570 newop.opcode = TREE_CODE (op[0]);
1571 newop.type = type;
1572 newop.op0 = op[0];
1573 newop.op1 = op[1];
1574 newop.op2 = op[2];
1575 /* If it transforms a non-constant ARRAY_REF into a constant
1576 one, adjust the constant offset. */
1577 if (newop.opcode == ARRAY_REF
1578 && newop.off == -1
1579 && TREE_CODE (op[0]) == INTEGER_CST
1580 && TREE_CODE (op[1]) == INTEGER_CST
1581 && TREE_CODE (op[2]) == INTEGER_CST)
1582 {
1583 double_int off = tree_to_double_int (op[0]);
1584 off += -tree_to_double_int (op[1]);
1585 off *= tree_to_double_int (op[2]);
1586 if (off.fits_shwi ())
1587 newop.off = off.low;
1588 }
1589 newoperands[j] = newop;
1590 /* If it transforms from an SSA_NAME to an address, fold with
1591 a preceding indirect reference. */
1592 if (j > 0 && op[0] && TREE_CODE (op[0]) == ADDR_EXPR
1593 && newoperands[j - 1].opcode == MEM_REF)
1594 vn_reference_fold_indirect (&newoperands, &j);
1595 }
1596 if (i != operands.length ())
1597 {
1598 newoperands.release ();
1599 return NULL;
1600 }
1601
1602 if (vuse)
1603 {
1604 newvuse = translate_vuse_through_block (newoperands,
1605 ref->set, ref->type,
1606 vuse, phiblock, pred,
1607 &same_valid);
1608 if (newvuse == NULL_TREE)
1609 {
1610 newoperands.release ();
1611 return NULL;
1612 }
1613 }
1614
1615 if (changed || newvuse != vuse)
1616 {
1617 unsigned int new_val_id;
1618 pre_expr constant;
1619
1620 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1621 ref->type,
1622 newoperands,
1623 &newref, VN_WALK);
1624 if (result)
1625 newoperands.release ();
1626
1627 /* We can always insert constants, so if we have a partial
1628 redundant constant load of another type try to translate it
1629 to a constant of appropriate type. */
1630 if (result && is_gimple_min_invariant (result))
1631 {
1632 tree tem = result;
1633 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1634 {
1635 tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1636 if (tem && !is_gimple_min_invariant (tem))
1637 tem = NULL_TREE;
1638 }
1639 if (tem)
1640 return get_or_alloc_expr_for_constant (tem);
1641 }
1642
1643 /* If we'd have to convert things we would need to validate
1644 if we can insert the translated expression. So fail
1645 here for now - we cannot insert an alias with a different
1646 type in the VN tables either, as that would assert. */
1647 if (result
1648 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1649 return NULL;
1650 else if (!result && newref
1651 && !useless_type_conversion_p (ref->type, newref->type))
1652 {
1653 newoperands.release ();
1654 return NULL;
1655 }
1656
1657 expr = (pre_expr) pool_alloc (pre_expr_pool);
1658 expr->kind = REFERENCE;
1659 expr->id = 0;
1660
1661 if (newref)
1662 {
1663 PRE_EXPR_REFERENCE (expr) = newref;
1664 constant = fully_constant_expression (expr);
1665 if (constant != expr)
1666 return constant;
1667
1668 new_val_id = newref->value_id;
1669 get_or_alloc_expression_id (expr);
1670 }
1671 else
1672 {
1673 if (changed || !same_valid)
1674 {
1675 new_val_id = get_next_value_id ();
1676 value_expressions.safe_grow_cleared(get_max_value_id() + 1);
1677 }
1678 else
1679 new_val_id = ref->value_id;
1680 newref = vn_reference_insert_pieces (newvuse, ref->set,
1681 ref->type,
1682 newoperands,
1683 result, new_val_id);
1684 newoperands.create (0);
1685 PRE_EXPR_REFERENCE (expr) = newref;
1686 constant = fully_constant_expression (expr);
1687 if (constant != expr)
1688 return constant;
1689 get_or_alloc_expression_id (expr);
1690 }
1691 add_to_value (new_val_id, expr);
1692 }
1693 newoperands.release ();
1694 return expr;
1695 }
1696 break;
1697
1698 case NAME:
1699 {
1700 tree name = PRE_EXPR_NAME (expr);
1701 gimple def_stmt = SSA_NAME_DEF_STMT (name);
1702 /* If the SSA name is defined by a PHI node in this block,
1703 translate it. */
1704 if (gimple_code (def_stmt) == GIMPLE_PHI
1705 && gimple_bb (def_stmt) == phiblock)
1706 {
1707 edge e = find_edge (pred, gimple_bb (def_stmt));
1708 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1709
1710 /* Handle constant. */
1711 if (is_gimple_min_invariant (def))
1712 return get_or_alloc_expr_for_constant (def);
1713
1714 return get_or_alloc_expr_for_name (def);
1715 }
1716 /* Otherwise return it unchanged - it will get cleaned if its
1717 value is not available in PREDs AVAIL_OUT set of expressions. */
1718 return expr;
1719 }
1720
1721 default:
1722 gcc_unreachable ();
1723 }
1724 }
1725
1726 /* Wrapper around phi_translate_1 providing caching functionality. */
1727
1728 static pre_expr
1729 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1730 basic_block pred, basic_block phiblock)
1731 {
1732 expr_pred_trans_t slot = NULL;
1733 pre_expr phitrans;
1734
1735 if (!expr)
1736 return NULL;
1737
1738 /* Constants contain no values that need translation. */
1739 if (expr->kind == CONSTANT)
1740 return expr;
1741
1742 if (value_id_constant_p (get_expr_value_id (expr)))
1743 return expr;
1744
1745 /* Don't add translations of NAMEs as those are cheap to translate. */
1746 if (expr->kind != NAME)
1747 {
1748 if (phi_trans_add (&slot, expr, pred))
1749 return slot->v;
1750 /* Store NULL for the value we want to return in the case of
1751 recursing. */
1752 slot->v = NULL;
1753 }
1754
1755 /* Translate. */
1756 phitrans = phi_translate_1 (expr, set1, set2, pred, phiblock);
1757
1758 if (slot)
1759 slot->v = phitrans;
1760
1761 return phitrans;
1762 }
1763
1764
1765 /* For each expression in SET, translate the values through phi nodes
1766 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1767 expressions in DEST. */
1768
1769 static void
1770 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
1771 basic_block phiblock)
1772 {
1773 vec<pre_expr> exprs;
1774 pre_expr expr;
1775 int i;
1776
1777 if (gimple_seq_empty_p (phi_nodes (phiblock)))
1778 {
1779 bitmap_set_copy (dest, set);
1780 return;
1781 }
1782
1783 exprs = sorted_array_from_bitmap_set (set);
1784 FOR_EACH_VEC_ELT (exprs, i, expr)
1785 {
1786 pre_expr translated;
1787 translated = phi_translate (expr, set, NULL, pred, phiblock);
1788 if (!translated)
1789 continue;
1790
1791 /* We might end up with multiple expressions from SET being
1792 translated to the same value. In this case we do not want
1793 to retain the NARY or REFERENCE expression but prefer a NAME
1794 which would be the leader. */
1795 if (translated->kind == NAME)
1796 bitmap_value_replace_in_set (dest, translated);
1797 else
1798 bitmap_value_insert_into_set (dest, translated);
1799 }
1800 exprs.release ();
1801 }
1802
1803 /* Find the leader for a value (i.e., the name representing that
1804 value) in a given set, and return it. Return NULL if no leader
1805 is found. */
1806
1807 static pre_expr
1808 bitmap_find_leader (bitmap_set_t set, unsigned int val)
1809 {
1810 if (value_id_constant_p (val))
1811 {
1812 unsigned int i;
1813 bitmap_iterator bi;
1814 bitmap exprset = value_expressions[val];
1815
1816 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1817 {
1818 pre_expr expr = expression_for_id (i);
1819 if (expr->kind == CONSTANT)
1820 return expr;
1821 }
1822 }
1823 if (bitmap_set_contains_value (set, val))
1824 {
1825 /* Rather than walk the entire bitmap of expressions, and see
1826 whether any of them has the value we are looking for, we look
1827 at the reverse mapping, which tells us the set of expressions
1828 that have a given value (IE value->expressions with that
1829 value) and see if any of those expressions are in our set.
1830 The number of expressions per value is usually significantly
1831 less than the number of expressions in the set. In fact, for
1832 large testcases, doing it this way is roughly 5-10x faster
1833 than walking the bitmap.
1834 If this is somehow a significant lose for some cases, we can
1835 choose which set to walk based on which set is smaller. */
1836 unsigned int i;
1837 bitmap_iterator bi;
1838 bitmap exprset = value_expressions[val];
1839
1840 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1841 return expression_for_id (i);
1842 }
1843 return NULL;
1844 }
1845
1846 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1847 BLOCK by seeing if it is not killed in the block. Note that we are
1848 only determining whether there is a store that kills it. Because
1849 of the order in which clean iterates over values, we are guaranteed
1850 that altered operands will have caused us to be eliminated from the
1851 ANTIC_IN set already. */
1852
1853 static bool
1854 value_dies_in_block_x (pre_expr expr, basic_block block)
1855 {
1856 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1857 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1858 gimple def;
1859 gimple_stmt_iterator gsi;
1860 unsigned id = get_expression_id (expr);
1861 bool res = false;
1862 ao_ref ref;
1863
1864 if (!vuse)
1865 return false;
1866
1867 /* Lookup a previously calculated result. */
1868 if (EXPR_DIES (block)
1869 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1870 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1871
1872 /* A memory expression {e, VUSE} dies in the block if there is a
1873 statement that may clobber e. If, starting statement walk from the
1874 top of the basic block, a statement uses VUSE there can be no kill
1875 inbetween that use and the original statement that loaded {e, VUSE},
1876 so we can stop walking. */
1877 ref.base = NULL_TREE;
1878 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1879 {
1880 tree def_vuse, def_vdef;
1881 def = gsi_stmt (gsi);
1882 def_vuse = gimple_vuse (def);
1883 def_vdef = gimple_vdef (def);
1884
1885 /* Not a memory statement. */
1886 if (!def_vuse)
1887 continue;
1888
1889 /* Not a may-def. */
1890 if (!def_vdef)
1891 {
1892 /* A load with the same VUSE, we're done. */
1893 if (def_vuse == vuse)
1894 break;
1895
1896 continue;
1897 }
1898
1899 /* Init ref only if we really need it. */
1900 if (ref.base == NULL_TREE
1901 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type,
1902 refx->operands))
1903 {
1904 res = true;
1905 break;
1906 }
1907 /* If the statement may clobber expr, it dies. */
1908 if (stmt_may_clobber_ref_p_1 (def, &ref))
1909 {
1910 res = true;
1911 break;
1912 }
1913 }
1914
1915 /* Remember the result. */
1916 if (!EXPR_DIES (block))
1917 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1918 bitmap_set_bit (EXPR_DIES (block), id * 2);
1919 if (res)
1920 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1921
1922 return res;
1923 }
1924
1925
1926 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1927 contains its value-id. */
1928
1929 static bool
1930 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1931 {
1932 if (op && TREE_CODE (op) == SSA_NAME)
1933 {
1934 unsigned int value_id = VN_INFO (op)->value_id;
1935 if (!(bitmap_set_contains_value (set1, value_id)
1936 || (set2 && bitmap_set_contains_value (set2, value_id))))
1937 return false;
1938 }
1939 return true;
1940 }
1941
1942 /* Determine if the expression EXPR is valid in SET1 U SET2.
1943 ONLY SET2 CAN BE NULL.
1944 This means that we have a leader for each part of the expression
1945 (if it consists of values), or the expression is an SSA_NAME.
1946 For loads/calls, we also see if the vuse is killed in this block. */
1947
1948 static bool
1949 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr,
1950 basic_block block)
1951 {
1952 switch (expr->kind)
1953 {
1954 case NAME:
1955 return bitmap_find_leader (AVAIL_OUT (block),
1956 get_expr_value_id (expr)) != NULL;
1957 case NARY:
1958 {
1959 unsigned int i;
1960 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1961 for (i = 0; i < nary->length; i++)
1962 if (!op_valid_in_sets (set1, set2, nary->op[i]))
1963 return false;
1964 return true;
1965 }
1966 break;
1967 case REFERENCE:
1968 {
1969 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1970 vn_reference_op_t vro;
1971 unsigned int i;
1972
1973 FOR_EACH_VEC_ELT (ref->operands, i, vro)
1974 {
1975 if (!op_valid_in_sets (set1, set2, vro->op0)
1976 || !op_valid_in_sets (set1, set2, vro->op1)
1977 || !op_valid_in_sets (set1, set2, vro->op2))
1978 return false;
1979 }
1980 return true;
1981 }
1982 default:
1983 gcc_unreachable ();
1984 }
1985 }
1986
1987 /* Clean the set of expressions that are no longer valid in SET1 or
1988 SET2. This means expressions that are made up of values we have no
1989 leaders for in SET1 or SET2. This version is used for partial
1990 anticipation, which means it is not valid in either ANTIC_IN or
1991 PA_IN. */
1992
1993 static void
1994 dependent_clean (bitmap_set_t set1, bitmap_set_t set2, basic_block block)
1995 {
1996 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
1997 pre_expr expr;
1998 int i;
1999
2000 FOR_EACH_VEC_ELT (exprs, i, expr)
2001 {
2002 if (!valid_in_sets (set1, set2, expr, block))
2003 bitmap_remove_from_set (set1, expr);
2004 }
2005 exprs.release ();
2006 }
2007
2008 /* Clean the set of expressions that are no longer valid in SET. This
2009 means expressions that are made up of values we have no leaders for
2010 in SET. */
2011
2012 static void
2013 clean (bitmap_set_t set, basic_block block)
2014 {
2015 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set);
2016 pre_expr expr;
2017 int i;
2018
2019 FOR_EACH_VEC_ELT (exprs, i, expr)
2020 {
2021 if (!valid_in_sets (set, NULL, expr, block))
2022 bitmap_remove_from_set (set, expr);
2023 }
2024 exprs.release ();
2025 }
2026
2027 /* Clean the set of expressions that are no longer valid in SET because
2028 they are clobbered in BLOCK or because they trap and may not be executed. */
2029
2030 static void
2031 prune_clobbered_mems (bitmap_set_t set, basic_block block)
2032 {
2033 bitmap_iterator bi;
2034 unsigned i;
2035
2036 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2037 {
2038 pre_expr expr = expression_for_id (i);
2039 if (expr->kind == REFERENCE)
2040 {
2041 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2042 if (ref->vuse)
2043 {
2044 gimple def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2045 if (!gimple_nop_p (def_stmt)
2046 && ((gimple_bb (def_stmt) != block
2047 && !dominated_by_p (CDI_DOMINATORS,
2048 block, gimple_bb (def_stmt)))
2049 || (gimple_bb (def_stmt) == block
2050 && value_dies_in_block_x (expr, block))))
2051 bitmap_remove_from_set (set, expr);
2052 }
2053 }
2054 else if (expr->kind == NARY)
2055 {
2056 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2057 /* If the NARY may trap make sure the block does not contain
2058 a possible exit point.
2059 ??? This is overly conservative if we translate AVAIL_OUT
2060 as the available expression might be after the exit point. */
2061 if (BB_MAY_NOTRETURN (block)
2062 && vn_nary_may_trap (nary))
2063 bitmap_remove_from_set (set, expr);
2064 }
2065 }
2066 }
2067
2068 static sbitmap has_abnormal_preds;
2069
2070 /* List of blocks that may have changed during ANTIC computation and
2071 thus need to be iterated over. */
2072
2073 static sbitmap changed_blocks;
2074
2075 /* Decide whether to defer a block for a later iteration, or PHI
2076 translate SOURCE to DEST using phis in PHIBLOCK. Return false if we
2077 should defer the block, and true if we processed it. */
2078
2079 static bool
2080 defer_or_phi_translate_block (bitmap_set_t dest, bitmap_set_t source,
2081 basic_block block, basic_block phiblock)
2082 {
2083 if (!BB_VISITED (phiblock))
2084 {
2085 bitmap_set_bit (changed_blocks, block->index);
2086 BB_VISITED (block) = 0;
2087 BB_DEFERRED (block) = 1;
2088 return false;
2089 }
2090 else
2091 phi_translate_set (dest, source, block, phiblock);
2092 return true;
2093 }
2094
2095 /* Compute the ANTIC set for BLOCK.
2096
2097 If succs(BLOCK) > 1 then
2098 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2099 else if succs(BLOCK) == 1 then
2100 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2101
2102 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2103 */
2104
2105 static bool
2106 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2107 {
2108 bool changed = false;
2109 bitmap_set_t S, old, ANTIC_OUT;
2110 bitmap_iterator bi;
2111 unsigned int bii;
2112 edge e;
2113 edge_iterator ei;
2114
2115 old = ANTIC_OUT = S = NULL;
2116 BB_VISITED (block) = 1;
2117
2118 /* If any edges from predecessors are abnormal, antic_in is empty,
2119 so do nothing. */
2120 if (block_has_abnormal_pred_edge)
2121 goto maybe_dump_sets;
2122
2123 old = ANTIC_IN (block);
2124 ANTIC_OUT = bitmap_set_new ();
2125
2126 /* If the block has no successors, ANTIC_OUT is empty. */
2127 if (EDGE_COUNT (block->succs) == 0)
2128 ;
2129 /* If we have one successor, we could have some phi nodes to
2130 translate through. */
2131 else if (single_succ_p (block))
2132 {
2133 basic_block succ_bb = single_succ (block);
2134
2135 /* We trade iterations of the dataflow equations for having to
2136 phi translate the maximal set, which is incredibly slow
2137 (since the maximal set often has 300+ members, even when you
2138 have a small number of blocks).
2139 Basically, we defer the computation of ANTIC for this block
2140 until we have processed it's successor, which will inevitably
2141 have a *much* smaller set of values to phi translate once
2142 clean has been run on it.
2143 The cost of doing this is that we technically perform more
2144 iterations, however, they are lower cost iterations.
2145
2146 Timings for PRE on tramp3d-v4:
2147 without maximal set fix: 11 seconds
2148 with maximal set fix/without deferring: 26 seconds
2149 with maximal set fix/with deferring: 11 seconds
2150 */
2151
2152 if (!defer_or_phi_translate_block (ANTIC_OUT, ANTIC_IN (succ_bb),
2153 block, succ_bb))
2154 {
2155 changed = true;
2156 goto maybe_dump_sets;
2157 }
2158 }
2159 /* If we have multiple successors, we take the intersection of all of
2160 them. Note that in the case of loop exit phi nodes, we may have
2161 phis to translate through. */
2162 else
2163 {
2164 vec<basic_block> worklist;
2165 size_t i;
2166 basic_block bprime, first = NULL;
2167
2168 worklist.create (EDGE_COUNT (block->succs));
2169 FOR_EACH_EDGE (e, ei, block->succs)
2170 {
2171 if (!first
2172 && BB_VISITED (e->dest))
2173 first = e->dest;
2174 else if (BB_VISITED (e->dest))
2175 worklist.quick_push (e->dest);
2176 }
2177
2178 /* Of multiple successors we have to have visited one already. */
2179 if (!first)
2180 {
2181 bitmap_set_bit (changed_blocks, block->index);
2182 BB_VISITED (block) = 0;
2183 BB_DEFERRED (block) = 1;
2184 changed = true;
2185 worklist.release ();
2186 goto maybe_dump_sets;
2187 }
2188
2189 if (!gimple_seq_empty_p (phi_nodes (first)))
2190 phi_translate_set (ANTIC_OUT, ANTIC_IN (first), block, first);
2191 else
2192 bitmap_set_copy (ANTIC_OUT, ANTIC_IN (first));
2193
2194 FOR_EACH_VEC_ELT (worklist, i, bprime)
2195 {
2196 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2197 {
2198 bitmap_set_t tmp = bitmap_set_new ();
2199 phi_translate_set (tmp, ANTIC_IN (bprime), block, bprime);
2200 bitmap_set_and (ANTIC_OUT, tmp);
2201 bitmap_set_free (tmp);
2202 }
2203 else
2204 bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime));
2205 }
2206 worklist.release ();
2207 }
2208
2209 /* Prune expressions that are clobbered in block and thus become
2210 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2211 prune_clobbered_mems (ANTIC_OUT, block);
2212
2213 /* Generate ANTIC_OUT - TMP_GEN. */
2214 S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block));
2215
2216 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2217 ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block),
2218 TMP_GEN (block));
2219
2220 /* Then union in the ANTIC_OUT - TMP_GEN values,
2221 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2222 FOR_EACH_EXPR_ID_IN_SET (S, bii, bi)
2223 bitmap_value_insert_into_set (ANTIC_IN (block),
2224 expression_for_id (bii));
2225
2226 clean (ANTIC_IN (block), block);
2227
2228 if (!bitmap_set_equal (old, ANTIC_IN (block)))
2229 {
2230 changed = true;
2231 bitmap_set_bit (changed_blocks, block->index);
2232 FOR_EACH_EDGE (e, ei, block->preds)
2233 bitmap_set_bit (changed_blocks, e->src->index);
2234 }
2235 else
2236 bitmap_clear_bit (changed_blocks, block->index);
2237
2238 maybe_dump_sets:
2239 if (dump_file && (dump_flags & TDF_DETAILS))
2240 {
2241 if (!BB_DEFERRED (block) || BB_VISITED (block))
2242 {
2243 if (ANTIC_OUT)
2244 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2245
2246 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2247 block->index);
2248
2249 if (S)
2250 print_bitmap_set (dump_file, S, "S", block->index);
2251 }
2252 else
2253 {
2254 fprintf (dump_file,
2255 "Block %d was deferred for a future iteration.\n",
2256 block->index);
2257 }
2258 }
2259 if (old)
2260 bitmap_set_free (old);
2261 if (S)
2262 bitmap_set_free (S);
2263 if (ANTIC_OUT)
2264 bitmap_set_free (ANTIC_OUT);
2265 return changed;
2266 }
2267
2268 /* Compute PARTIAL_ANTIC for BLOCK.
2269
2270 If succs(BLOCK) > 1 then
2271 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2272 in ANTIC_OUT for all succ(BLOCK)
2273 else if succs(BLOCK) == 1 then
2274 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2275
2276 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2277 - ANTIC_IN[BLOCK])
2278
2279 */
2280 static bool
2281 compute_partial_antic_aux (basic_block block,
2282 bool block_has_abnormal_pred_edge)
2283 {
2284 bool changed = false;
2285 bitmap_set_t old_PA_IN;
2286 bitmap_set_t PA_OUT;
2287 edge e;
2288 edge_iterator ei;
2289 unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH);
2290
2291 old_PA_IN = PA_OUT = NULL;
2292
2293 /* If any edges from predecessors are abnormal, antic_in is empty,
2294 so do nothing. */
2295 if (block_has_abnormal_pred_edge)
2296 goto maybe_dump_sets;
2297
2298 /* If there are too many partially anticipatable values in the
2299 block, phi_translate_set can take an exponential time: stop
2300 before the translation starts. */
2301 if (max_pa
2302 && single_succ_p (block)
2303 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2304 goto maybe_dump_sets;
2305
2306 old_PA_IN = PA_IN (block);
2307 PA_OUT = bitmap_set_new ();
2308
2309 /* If the block has no successors, ANTIC_OUT is empty. */
2310 if (EDGE_COUNT (block->succs) == 0)
2311 ;
2312 /* If we have one successor, we could have some phi nodes to
2313 translate through. Note that we can't phi translate across DFS
2314 back edges in partial antic, because it uses a union operation on
2315 the successors. For recurrences like IV's, we will end up
2316 generating a new value in the set on each go around (i + 3 (VH.1)
2317 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2318 else if (single_succ_p (block))
2319 {
2320 basic_block succ = single_succ (block);
2321 if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK))
2322 phi_translate_set (PA_OUT, PA_IN (succ), block, succ);
2323 }
2324 /* If we have multiple successors, we take the union of all of
2325 them. */
2326 else
2327 {
2328 vec<basic_block> worklist;
2329 size_t i;
2330 basic_block bprime;
2331
2332 worklist.create (EDGE_COUNT (block->succs));
2333 FOR_EACH_EDGE (e, ei, block->succs)
2334 {
2335 if (e->flags & EDGE_DFS_BACK)
2336 continue;
2337 worklist.quick_push (e->dest);
2338 }
2339 if (worklist.length () > 0)
2340 {
2341 FOR_EACH_VEC_ELT (worklist, i, bprime)
2342 {
2343 unsigned int i;
2344 bitmap_iterator bi;
2345
2346 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi)
2347 bitmap_value_insert_into_set (PA_OUT,
2348 expression_for_id (i));
2349 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2350 {
2351 bitmap_set_t pa_in = bitmap_set_new ();
2352 phi_translate_set (pa_in, PA_IN (bprime), block, bprime);
2353 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2354 bitmap_value_insert_into_set (PA_OUT,
2355 expression_for_id (i));
2356 bitmap_set_free (pa_in);
2357 }
2358 else
2359 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi)
2360 bitmap_value_insert_into_set (PA_OUT,
2361 expression_for_id (i));
2362 }
2363 }
2364 worklist.release ();
2365 }
2366
2367 /* Prune expressions that are clobbered in block and thus become
2368 invalid if translated from PA_OUT to PA_IN. */
2369 prune_clobbered_mems (PA_OUT, block);
2370
2371 /* PA_IN starts with PA_OUT - TMP_GEN.
2372 Then we subtract things from ANTIC_IN. */
2373 PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block));
2374
2375 /* For partial antic, we want to put back in the phi results, since
2376 we will properly avoid making them partially antic over backedges. */
2377 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2378 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2379
2380 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2381 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2382
2383 dependent_clean (PA_IN (block), ANTIC_IN (block), block);
2384
2385 if (!bitmap_set_equal (old_PA_IN, PA_IN (block)))
2386 {
2387 changed = true;
2388 bitmap_set_bit (changed_blocks, block->index);
2389 FOR_EACH_EDGE (e, ei, block->preds)
2390 bitmap_set_bit (changed_blocks, e->src->index);
2391 }
2392 else
2393 bitmap_clear_bit (changed_blocks, block->index);
2394
2395 maybe_dump_sets:
2396 if (dump_file && (dump_flags & TDF_DETAILS))
2397 {
2398 if (PA_OUT)
2399 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2400
2401 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2402 }
2403 if (old_PA_IN)
2404 bitmap_set_free (old_PA_IN);
2405 if (PA_OUT)
2406 bitmap_set_free (PA_OUT);
2407 return changed;
2408 }
2409
2410 /* Compute ANTIC and partial ANTIC sets. */
2411
2412 static void
2413 compute_antic (void)
2414 {
2415 bool changed = true;
2416 int num_iterations = 0;
2417 basic_block block;
2418 int i;
2419
2420 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2421 We pre-build the map of blocks with incoming abnormal edges here. */
2422 has_abnormal_preds = sbitmap_alloc (last_basic_block);
2423 bitmap_clear (has_abnormal_preds);
2424
2425 FOR_ALL_BB (block)
2426 {
2427 edge_iterator ei;
2428 edge e;
2429
2430 FOR_EACH_EDGE (e, ei, block->preds)
2431 {
2432 e->flags &= ~EDGE_DFS_BACK;
2433 if (e->flags & EDGE_ABNORMAL)
2434 {
2435 bitmap_set_bit (has_abnormal_preds, block->index);
2436 break;
2437 }
2438 }
2439
2440 BB_VISITED (block) = 0;
2441 BB_DEFERRED (block) = 0;
2442
2443 /* While we are here, give empty ANTIC_IN sets to each block. */
2444 ANTIC_IN (block) = bitmap_set_new ();
2445 PA_IN (block) = bitmap_set_new ();
2446 }
2447
2448 /* At the exit block we anticipate nothing. */
2449 BB_VISITED (EXIT_BLOCK_PTR) = 1;
2450
2451 changed_blocks = sbitmap_alloc (last_basic_block + 1);
2452 bitmap_ones (changed_blocks);
2453 while (changed)
2454 {
2455 if (dump_file && (dump_flags & TDF_DETAILS))
2456 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2457 /* ??? We need to clear our PHI translation cache here as the
2458 ANTIC sets shrink and we restrict valid translations to
2459 those having operands with leaders in ANTIC. Same below
2460 for PA ANTIC computation. */
2461 num_iterations++;
2462 changed = false;
2463 for (i = postorder_num - 1; i >= 0; i--)
2464 {
2465 if (bitmap_bit_p (changed_blocks, postorder[i]))
2466 {
2467 basic_block block = BASIC_BLOCK (postorder[i]);
2468 changed |= compute_antic_aux (block,
2469 bitmap_bit_p (has_abnormal_preds,
2470 block->index));
2471 }
2472 }
2473 /* Theoretically possible, but *highly* unlikely. */
2474 gcc_checking_assert (num_iterations < 500);
2475 }
2476
2477 statistics_histogram_event (cfun, "compute_antic iterations",
2478 num_iterations);
2479
2480 if (do_partial_partial)
2481 {
2482 bitmap_ones (changed_blocks);
2483 mark_dfs_back_edges ();
2484 num_iterations = 0;
2485 changed = true;
2486 while (changed)
2487 {
2488 if (dump_file && (dump_flags & TDF_DETAILS))
2489 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2490 num_iterations++;
2491 changed = false;
2492 for (i = postorder_num - 1 ; i >= 0; i--)
2493 {
2494 if (bitmap_bit_p (changed_blocks, postorder[i]))
2495 {
2496 basic_block block = BASIC_BLOCK (postorder[i]);
2497 changed
2498 |= compute_partial_antic_aux (block,
2499 bitmap_bit_p (has_abnormal_preds,
2500 block->index));
2501 }
2502 }
2503 /* Theoretically possible, but *highly* unlikely. */
2504 gcc_checking_assert (num_iterations < 500);
2505 }
2506 statistics_histogram_event (cfun, "compute_partial_antic iterations",
2507 num_iterations);
2508 }
2509 sbitmap_free (has_abnormal_preds);
2510 sbitmap_free (changed_blocks);
2511 }
2512
2513
2514 /* Inserted expressions are placed onto this worklist, which is used
2515 for performing quick dead code elimination of insertions we made
2516 that didn't turn out to be necessary. */
2517 static bitmap inserted_exprs;
2518
2519 /* The actual worker for create_component_ref_by_pieces. */
2520
2521 static tree
2522 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2523 unsigned int *operand, gimple_seq *stmts)
2524 {
2525 vn_reference_op_t currop = &ref->operands[*operand];
2526 tree genop;
2527 ++*operand;
2528 switch (currop->opcode)
2529 {
2530 case CALL_EXPR:
2531 {
2532 tree folded, sc = NULL_TREE;
2533 unsigned int nargs = 0;
2534 tree fn, *args;
2535 if (TREE_CODE (currop->op0) == FUNCTION_DECL)
2536 fn = currop->op0;
2537 else
2538 fn = find_or_generate_expression (block, currop->op0, stmts);
2539 if (!fn)
2540 return NULL_TREE;
2541 if (currop->op1)
2542 {
2543 sc = find_or_generate_expression (block, currop->op1, stmts);
2544 if (!sc)
2545 return NULL_TREE;
2546 }
2547 args = XNEWVEC (tree, ref->operands.length () - 1);
2548 while (*operand < ref->operands.length ())
2549 {
2550 args[nargs] = create_component_ref_by_pieces_1 (block, ref,
2551 operand, stmts);
2552 if (!args[nargs])
2553 return NULL_TREE;
2554 nargs++;
2555 }
2556 folded = build_call_array (currop->type,
2557 (TREE_CODE (fn) == FUNCTION_DECL
2558 ? build_fold_addr_expr (fn) : fn),
2559 nargs, args);
2560 free (args);
2561 if (sc)
2562 CALL_EXPR_STATIC_CHAIN (folded) = sc;
2563 return folded;
2564 }
2565
2566 case MEM_REF:
2567 {
2568 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2569 stmts);
2570 if (!baseop)
2571 return NULL_TREE;
2572 tree offset = currop->op0;
2573 if (TREE_CODE (baseop) == ADDR_EXPR
2574 && handled_component_p (TREE_OPERAND (baseop, 0)))
2575 {
2576 HOST_WIDE_INT off;
2577 tree base;
2578 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2579 &off);
2580 gcc_assert (base);
2581 offset = int_const_binop (PLUS_EXPR, offset,
2582 build_int_cst (TREE_TYPE (offset),
2583 off));
2584 baseop = build_fold_addr_expr (base);
2585 }
2586 return fold_build2 (MEM_REF, currop->type, baseop, offset);
2587 }
2588
2589 case TARGET_MEM_REF:
2590 {
2591 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2592 vn_reference_op_t nextop = &ref->operands[++*operand];
2593 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2594 stmts);
2595 if (!baseop)
2596 return NULL_TREE;
2597 if (currop->op0)
2598 {
2599 genop0 = find_or_generate_expression (block, currop->op0, stmts);
2600 if (!genop0)
2601 return NULL_TREE;
2602 }
2603 if (nextop->op0)
2604 {
2605 genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2606 if (!genop1)
2607 return NULL_TREE;
2608 }
2609 return build5 (TARGET_MEM_REF, currop->type,
2610 baseop, currop->op2, genop0, currop->op1, genop1);
2611 }
2612
2613 case ADDR_EXPR:
2614 if (currop->op0)
2615 {
2616 gcc_assert (is_gimple_min_invariant (currop->op0));
2617 return currop->op0;
2618 }
2619 /* Fallthrough. */
2620 case REALPART_EXPR:
2621 case IMAGPART_EXPR:
2622 case VIEW_CONVERT_EXPR:
2623 {
2624 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2625 stmts);
2626 if (!genop0)
2627 return NULL_TREE;
2628 return fold_build1 (currop->opcode, currop->type, genop0);
2629 }
2630
2631 case WITH_SIZE_EXPR:
2632 {
2633 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2634 stmts);
2635 if (!genop0)
2636 return NULL_TREE;
2637 tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2638 if (!genop1)
2639 return NULL_TREE;
2640 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2641 }
2642
2643 case BIT_FIELD_REF:
2644 {
2645 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2646 stmts);
2647 if (!genop0)
2648 return NULL_TREE;
2649 tree op1 = currop->op0;
2650 tree op2 = currop->op1;
2651 return fold_build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2652 }
2653
2654 /* For array ref vn_reference_op's, operand 1 of the array ref
2655 is op0 of the reference op and operand 3 of the array ref is
2656 op1. */
2657 case ARRAY_RANGE_REF:
2658 case ARRAY_REF:
2659 {
2660 tree genop0;
2661 tree genop1 = currop->op0;
2662 tree genop2 = currop->op1;
2663 tree genop3 = currop->op2;
2664 genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2665 stmts);
2666 if (!genop0)
2667 return NULL_TREE;
2668 genop1 = find_or_generate_expression (block, genop1, stmts);
2669 if (!genop1)
2670 return NULL_TREE;
2671 if (genop2)
2672 {
2673 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2674 /* Drop zero minimum index if redundant. */
2675 if (integer_zerop (genop2)
2676 && (!domain_type
2677 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2678 genop2 = NULL_TREE;
2679 else
2680 {
2681 genop2 = find_or_generate_expression (block, genop2, stmts);
2682 if (!genop2)
2683 return NULL_TREE;
2684 }
2685 }
2686 if (genop3)
2687 {
2688 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2689 /* We can't always put a size in units of the element alignment
2690 here as the element alignment may be not visible. See
2691 PR43783. Simply drop the element size for constant
2692 sizes. */
2693 if (tree_int_cst_equal (genop3, TYPE_SIZE_UNIT (elmt_type)))
2694 genop3 = NULL_TREE;
2695 else
2696 {
2697 genop3 = size_binop (EXACT_DIV_EXPR, genop3,
2698 size_int (TYPE_ALIGN_UNIT (elmt_type)));
2699 genop3 = find_or_generate_expression (block, genop3, stmts);
2700 if (!genop3)
2701 return NULL_TREE;
2702 }
2703 }
2704 return build4 (currop->opcode, currop->type, genop0, genop1,
2705 genop2, genop3);
2706 }
2707 case COMPONENT_REF:
2708 {
2709 tree op0;
2710 tree op1;
2711 tree genop2 = currop->op1;
2712 op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2713 if (!op0)
2714 return NULL_TREE;
2715 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2716 op1 = currop->op0;
2717 if (genop2)
2718 {
2719 genop2 = find_or_generate_expression (block, genop2, stmts);
2720 if (!genop2)
2721 return NULL_TREE;
2722 }
2723 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2724 }
2725
2726 case SSA_NAME:
2727 {
2728 genop = find_or_generate_expression (block, currop->op0, stmts);
2729 return genop;
2730 }
2731 case STRING_CST:
2732 case INTEGER_CST:
2733 case COMPLEX_CST:
2734 case VECTOR_CST:
2735 case REAL_CST:
2736 case CONSTRUCTOR:
2737 case VAR_DECL:
2738 case PARM_DECL:
2739 case CONST_DECL:
2740 case RESULT_DECL:
2741 case FUNCTION_DECL:
2742 return currop->op0;
2743
2744 default:
2745 gcc_unreachable ();
2746 }
2747 }
2748
2749 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2750 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2751 trying to rename aggregates into ssa form directly, which is a no no.
2752
2753 Thus, this routine doesn't create temporaries, it just builds a
2754 single access expression for the array, calling
2755 find_or_generate_expression to build the innermost pieces.
2756
2757 This function is a subroutine of create_expression_by_pieces, and
2758 should not be called on it's own unless you really know what you
2759 are doing. */
2760
2761 static tree
2762 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2763 gimple_seq *stmts)
2764 {
2765 unsigned int op = 0;
2766 return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2767 }
2768
2769 /* Find a simple leader for an expression, or generate one using
2770 create_expression_by_pieces from a NARY expression for the value.
2771 BLOCK is the basic_block we are looking for leaders in.
2772 OP is the tree expression to find a leader for or generate.
2773 Returns the leader or NULL_TREE on failure. */
2774
2775 static tree
2776 find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2777 {
2778 pre_expr expr = get_or_alloc_expr_for (op);
2779 unsigned int lookfor = get_expr_value_id (expr);
2780 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2781 if (leader)
2782 {
2783 if (leader->kind == NAME)
2784 return PRE_EXPR_NAME (leader);
2785 else if (leader->kind == CONSTANT)
2786 return PRE_EXPR_CONSTANT (leader);
2787
2788 /* Defer. */
2789 return NULL_TREE;
2790 }
2791
2792 /* It must be a complex expression, so generate it recursively. Note
2793 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2794 where the insert algorithm fails to insert a required expression. */
2795 bitmap exprset = value_expressions[lookfor];
2796 bitmap_iterator bi;
2797 unsigned int i;
2798 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2799 {
2800 pre_expr temp = expression_for_id (i);
2801 /* We cannot insert random REFERENCE expressions at arbitrary
2802 places. We can insert NARYs which eventually re-materializes
2803 its operand values. */
2804 if (temp->kind == NARY)
2805 return create_expression_by_pieces (block, temp, stmts,
2806 get_expr_type (expr));
2807 }
2808
2809 /* Defer. */
2810 return NULL_TREE;
2811 }
2812
2813 #define NECESSARY GF_PLF_1
2814
2815 /* Create an expression in pieces, so that we can handle very complex
2816 expressions that may be ANTIC, but not necessary GIMPLE.
2817 BLOCK is the basic block the expression will be inserted into,
2818 EXPR is the expression to insert (in value form)
2819 STMTS is a statement list to append the necessary insertions into.
2820
2821 This function will die if we hit some value that shouldn't be
2822 ANTIC but is (IE there is no leader for it, or its components).
2823 The function returns NULL_TREE in case a different antic expression
2824 has to be inserted first.
2825 This function may also generate expressions that are themselves
2826 partially or fully redundant. Those that are will be either made
2827 fully redundant during the next iteration of insert (for partially
2828 redundant ones), or eliminated by eliminate (for fully redundant
2829 ones). */
2830
2831 static tree
2832 create_expression_by_pieces (basic_block block, pre_expr expr,
2833 gimple_seq *stmts, tree type)
2834 {
2835 tree name;
2836 tree folded;
2837 gimple_seq forced_stmts = NULL;
2838 unsigned int value_id;
2839 gimple_stmt_iterator gsi;
2840 tree exprtype = type ? type : get_expr_type (expr);
2841 pre_expr nameexpr;
2842 gimple newstmt;
2843
2844 switch (expr->kind)
2845 {
2846 /* We may hit the NAME/CONSTANT case if we have to convert types
2847 that value numbering saw through. */
2848 case NAME:
2849 folded = PRE_EXPR_NAME (expr);
2850 break;
2851 case CONSTANT:
2852 folded = PRE_EXPR_CONSTANT (expr);
2853 break;
2854 case REFERENCE:
2855 {
2856 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2857 folded = create_component_ref_by_pieces (block, ref, stmts);
2858 if (!folded)
2859 return NULL_TREE;
2860 }
2861 break;
2862 case NARY:
2863 {
2864 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2865 tree *genop = XALLOCAVEC (tree, nary->length);
2866 unsigned i;
2867 for (i = 0; i < nary->length; ++i)
2868 {
2869 genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2870 if (!genop[i])
2871 return NULL_TREE;
2872 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2873 may have conversions stripped. */
2874 if (nary->opcode == POINTER_PLUS_EXPR)
2875 {
2876 if (i == 0)
2877 genop[i] = fold_convert (nary->type, genop[i]);
2878 else if (i == 1)
2879 genop[i] = convert_to_ptrofftype (genop[i]);
2880 }
2881 else
2882 genop[i] = fold_convert (TREE_TYPE (nary->op[i]), genop[i]);
2883 }
2884 if (nary->opcode == CONSTRUCTOR)
2885 {
2886 vec<constructor_elt, va_gc> *elts = NULL;
2887 for (i = 0; i < nary->length; ++i)
2888 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2889 folded = build_constructor (nary->type, elts);
2890 }
2891 else
2892 {
2893 switch (nary->length)
2894 {
2895 case 1:
2896 folded = fold_build1 (nary->opcode, nary->type,
2897 genop[0]);
2898 break;
2899 case 2:
2900 folded = fold_build2 (nary->opcode, nary->type,
2901 genop[0], genop[1]);
2902 break;
2903 case 3:
2904 folded = fold_build3 (nary->opcode, nary->type,
2905 genop[0], genop[1], genop[2]);
2906 break;
2907 default:
2908 gcc_unreachable ();
2909 }
2910 }
2911 }
2912 break;
2913 default:
2914 gcc_unreachable ();
2915 }
2916
2917 if (!useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2918 folded = fold_convert (exprtype, folded);
2919
2920 /* Force the generated expression to be a sequence of GIMPLE
2921 statements.
2922 We have to call unshare_expr because force_gimple_operand may
2923 modify the tree we pass to it. */
2924 folded = force_gimple_operand (unshare_expr (folded), &forced_stmts,
2925 false, NULL);
2926
2927 /* If we have any intermediate expressions to the value sets, add them
2928 to the value sets and chain them in the instruction stream. */
2929 if (forced_stmts)
2930 {
2931 gsi = gsi_start (forced_stmts);
2932 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2933 {
2934 gimple stmt = gsi_stmt (gsi);
2935 tree forcedname = gimple_get_lhs (stmt);
2936 pre_expr nameexpr;
2937
2938 if (TREE_CODE (forcedname) == SSA_NAME)
2939 {
2940 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
2941 VN_INFO_GET (forcedname)->valnum = forcedname;
2942 VN_INFO (forcedname)->value_id = get_next_value_id ();
2943 nameexpr = get_or_alloc_expr_for_name (forcedname);
2944 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
2945 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2946 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2947 }
2948 }
2949 gimple_seq_add_seq (stmts, forced_stmts);
2950 }
2951
2952 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2953 newstmt = gimple_build_assign (name, folded);
2954 gimple_set_plf (newstmt, NECESSARY, false);
2955
2956 gimple_seq_add_stmt (stmts, newstmt);
2957 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (name));
2958
2959 /* Fold the last statement. */
2960 gsi = gsi_last (*stmts);
2961 if (fold_stmt_inplace (&gsi))
2962 update_stmt (gsi_stmt (gsi));
2963
2964 /* Add a value number to the temporary.
2965 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2966 we are creating the expression by pieces, and this particular piece of
2967 the expression may have been represented. There is no harm in replacing
2968 here. */
2969 value_id = get_expr_value_id (expr);
2970 VN_INFO_GET (name)->value_id = value_id;
2971 VN_INFO (name)->valnum = sccvn_valnum_from_value_id (value_id);
2972 if (VN_INFO (name)->valnum == NULL_TREE)
2973 VN_INFO (name)->valnum = name;
2974 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
2975 nameexpr = get_or_alloc_expr_for_name (name);
2976 add_to_value (value_id, nameexpr);
2977 if (NEW_SETS (block))
2978 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2979 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2980
2981 pre_stats.insertions++;
2982 if (dump_file && (dump_flags & TDF_DETAILS))
2983 {
2984 fprintf (dump_file, "Inserted ");
2985 print_gimple_stmt (dump_file, newstmt, 0, 0);
2986 fprintf (dump_file, " in predecessor %d (%04d)\n",
2987 block->index, value_id);
2988 }
2989
2990 return name;
2991 }
2992
2993
2994 /* Returns true if we want to inhibit the insertions of PHI nodes
2995 for the given EXPR for basic block BB (a member of a loop).
2996 We want to do this, when we fear that the induction variable we
2997 create might inhibit vectorization. */
2998
2999 static bool
3000 inhibit_phi_insertion (basic_block bb, pre_expr expr)
3001 {
3002 vn_reference_t vr = PRE_EXPR_REFERENCE (expr);
3003 vec<vn_reference_op_s> ops = vr->operands;
3004 vn_reference_op_t op;
3005 unsigned i;
3006
3007 /* If we aren't going to vectorize we don't inhibit anything. */
3008 if (!flag_tree_loop_vectorize)
3009 return false;
3010
3011 /* Otherwise we inhibit the insertion when the address of the
3012 memory reference is a simple induction variable. In other
3013 cases the vectorizer won't do anything anyway (either it's
3014 loop invariant or a complicated expression). */
3015 FOR_EACH_VEC_ELT (ops, i, op)
3016 {
3017 switch (op->opcode)
3018 {
3019 case CALL_EXPR:
3020 /* Calls are not a problem. */
3021 return false;
3022
3023 case ARRAY_REF:
3024 case ARRAY_RANGE_REF:
3025 if (TREE_CODE (op->op0) != SSA_NAME)
3026 break;
3027 /* Fallthru. */
3028 case SSA_NAME:
3029 {
3030 basic_block defbb = gimple_bb (SSA_NAME_DEF_STMT (op->op0));
3031 affine_iv iv;
3032 /* Default defs are loop invariant. */
3033 if (!defbb)
3034 break;
3035 /* Defined outside this loop, also loop invariant. */
3036 if (!flow_bb_inside_loop_p (bb->loop_father, defbb))
3037 break;
3038 /* If it's a simple induction variable inhibit insertion,
3039 the vectorizer might be interested in this one. */
3040 if (simple_iv (bb->loop_father, bb->loop_father,
3041 op->op0, &iv, true))
3042 return true;
3043 /* No simple IV, vectorizer can't do anything, hence no
3044 reason to inhibit the transformation for this operand. */
3045 break;
3046 }
3047 default:
3048 break;
3049 }
3050 }
3051 return false;
3052 }
3053
3054 /* Insert the to-be-made-available values of expression EXPRNUM for each
3055 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3056 merge the result with a phi node, given the same value number as
3057 NODE. Return true if we have inserted new stuff. */
3058
3059 static bool
3060 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3061 vec<pre_expr> avail)
3062 {
3063 pre_expr expr = expression_for_id (exprnum);
3064 pre_expr newphi;
3065 unsigned int val = get_expr_value_id (expr);
3066 edge pred;
3067 bool insertions = false;
3068 bool nophi = false;
3069 basic_block bprime;
3070 pre_expr eprime;
3071 edge_iterator ei;
3072 tree type = get_expr_type (expr);
3073 tree temp;
3074 gimple phi;
3075
3076 /* Make sure we aren't creating an induction variable. */
3077 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3078 {
3079 bool firstinsideloop = false;
3080 bool secondinsideloop = false;
3081 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3082 EDGE_PRED (block, 0)->src);
3083 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3084 EDGE_PRED (block, 1)->src);
3085 /* Induction variables only have one edge inside the loop. */
3086 if ((firstinsideloop ^ secondinsideloop)
3087 && (expr->kind != REFERENCE
3088 || inhibit_phi_insertion (block, expr)))
3089 {
3090 if (dump_file && (dump_flags & TDF_DETAILS))
3091 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3092 nophi = true;
3093 }
3094 }
3095
3096 /* Make the necessary insertions. */
3097 FOR_EACH_EDGE (pred, ei, block->preds)
3098 {
3099 gimple_seq stmts = NULL;
3100 tree builtexpr;
3101 bprime = pred->src;
3102 eprime = avail[pred->dest_idx];
3103
3104 if (eprime->kind != NAME && eprime->kind != CONSTANT)
3105 {
3106 builtexpr = create_expression_by_pieces (bprime, eprime,
3107 &stmts, type);
3108 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3109 gsi_insert_seq_on_edge (pred, stmts);
3110 if (!builtexpr)
3111 {
3112 /* We cannot insert a PHI node if we failed to insert
3113 on one edge. */
3114 nophi = true;
3115 continue;
3116 }
3117 avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3118 insertions = true;
3119 }
3120 else if (eprime->kind == CONSTANT)
3121 {
3122 /* Constants may not have the right type, fold_convert
3123 should give us back a constant with the right type. */
3124 tree constant = PRE_EXPR_CONSTANT (eprime);
3125 if (!useless_type_conversion_p (type, TREE_TYPE (constant)))
3126 {
3127 tree builtexpr = fold_convert (type, constant);
3128 if (!is_gimple_min_invariant (builtexpr))
3129 {
3130 tree forcedexpr = force_gimple_operand (builtexpr,
3131 &stmts, true,
3132 NULL);
3133 if (!is_gimple_min_invariant (forcedexpr))
3134 {
3135 if (forcedexpr != builtexpr)
3136 {
3137 VN_INFO_GET (forcedexpr)->valnum = PRE_EXPR_CONSTANT (eprime);
3138 VN_INFO (forcedexpr)->value_id = get_expr_value_id (eprime);
3139 }
3140 if (stmts)
3141 {
3142 gimple_stmt_iterator gsi;
3143 gsi = gsi_start (stmts);
3144 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3145 {
3146 gimple stmt = gsi_stmt (gsi);
3147 tree lhs = gimple_get_lhs (stmt);
3148 if (TREE_CODE (lhs) == SSA_NAME)
3149 bitmap_set_bit (inserted_exprs,
3150 SSA_NAME_VERSION (lhs));
3151 gimple_set_plf (stmt, NECESSARY, false);
3152 }
3153 gsi_insert_seq_on_edge (pred, stmts);
3154 }
3155 avail[pred->dest_idx]
3156 = get_or_alloc_expr_for_name (forcedexpr);
3157 }
3158 }
3159 else
3160 avail[pred->dest_idx]
3161 = get_or_alloc_expr_for_constant (builtexpr);
3162 }
3163 }
3164 else if (eprime->kind == NAME)
3165 {
3166 /* We may have to do a conversion because our value
3167 numbering can look through types in certain cases, but
3168 our IL requires all operands of a phi node have the same
3169 type. */
3170 tree name = PRE_EXPR_NAME (eprime);
3171 if (!useless_type_conversion_p (type, TREE_TYPE (name)))
3172 {
3173 tree builtexpr;
3174 tree forcedexpr;
3175 builtexpr = fold_convert (type, name);
3176 forcedexpr = force_gimple_operand (builtexpr,
3177 &stmts, true,
3178 NULL);
3179
3180 if (forcedexpr != name)
3181 {
3182 VN_INFO_GET (forcedexpr)->valnum = VN_INFO (name)->valnum;
3183 VN_INFO (forcedexpr)->value_id = VN_INFO (name)->value_id;
3184 }
3185
3186 if (stmts)
3187 {
3188 gimple_stmt_iterator gsi;
3189 gsi = gsi_start (stmts);
3190 for (; !gsi_end_p (gsi); gsi_next (&gsi))
3191 {
3192 gimple stmt = gsi_stmt (gsi);
3193 tree lhs = gimple_get_lhs (stmt);
3194 if (TREE_CODE (lhs) == SSA_NAME)
3195 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
3196 gimple_set_plf (stmt, NECESSARY, false);
3197 }
3198 gsi_insert_seq_on_edge (pred, stmts);
3199 }
3200 avail[pred->dest_idx] = get_or_alloc_expr_for_name (forcedexpr);
3201 }
3202 }
3203 }
3204 /* If we didn't want a phi node, and we made insertions, we still have
3205 inserted new stuff, and thus return true. If we didn't want a phi node,
3206 and didn't make insertions, we haven't added anything new, so return
3207 false. */
3208 if (nophi && insertions)
3209 return true;
3210 else if (nophi && !insertions)
3211 return false;
3212
3213 /* Now build a phi for the new variable. */
3214 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3215 phi = create_phi_node (temp, block);
3216
3217 gimple_set_plf (phi, NECESSARY, false);
3218 VN_INFO_GET (temp)->value_id = val;
3219 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3220 if (VN_INFO (temp)->valnum == NULL_TREE)
3221 VN_INFO (temp)->valnum = temp;
3222 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3223 FOR_EACH_EDGE (pred, ei, block->preds)
3224 {
3225 pre_expr ae = avail[pred->dest_idx];
3226 gcc_assert (get_expr_type (ae) == type
3227 || useless_type_conversion_p (type, get_expr_type (ae)));
3228 if (ae->kind == CONSTANT)
3229 add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3230 pred, UNKNOWN_LOCATION);
3231 else
3232 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3233 }
3234
3235 newphi = get_or_alloc_expr_for_name (temp);
3236 add_to_value (val, newphi);
3237
3238 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3239 this insertion, since we test for the existence of this value in PHI_GEN
3240 before proceeding with the partial redundancy checks in insert_aux.
3241
3242 The value may exist in AVAIL_OUT, in particular, it could be represented
3243 by the expression we are trying to eliminate, in which case we want the
3244 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3245 inserted there.
3246
3247 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3248 this block, because if it did, it would have existed in our dominator's
3249 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3250 */
3251
3252 bitmap_insert_into_set (PHI_GEN (block), newphi);
3253 bitmap_value_replace_in_set (AVAIL_OUT (block),
3254 newphi);
3255 bitmap_insert_into_set (NEW_SETS (block),
3256 newphi);
3257
3258 if (dump_file && (dump_flags & TDF_DETAILS))
3259 {
3260 fprintf (dump_file, "Created phi ");
3261 print_gimple_stmt (dump_file, phi, 0, 0);
3262 fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3263 }
3264 pre_stats.phis++;
3265 return true;
3266 }
3267
3268
3269
3270 /* Perform insertion of partially redundant values.
3271 For BLOCK, do the following:
3272 1. Propagate the NEW_SETS of the dominator into the current block.
3273 If the block has multiple predecessors,
3274 2a. Iterate over the ANTIC expressions for the block to see if
3275 any of them are partially redundant.
3276 2b. If so, insert them into the necessary predecessors to make
3277 the expression fully redundant.
3278 2c. Insert a new PHI merging the values of the predecessors.
3279 2d. Insert the new PHI, and the new expressions, into the
3280 NEW_SETS set.
3281 3. Recursively call ourselves on the dominator children of BLOCK.
3282
3283 Steps 1, 2a, and 3 are done by insert_aux. 2b, 2c and 2d are done by
3284 do_regular_insertion and do_partial_insertion.
3285
3286 */
3287
3288 static bool
3289 do_regular_insertion (basic_block block, basic_block dom)
3290 {
3291 bool new_stuff = false;
3292 vec<pre_expr> exprs;
3293 pre_expr expr;
3294 vec<pre_expr> avail = vNULL;
3295 int i;
3296
3297 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3298 avail.safe_grow (EDGE_COUNT (block->preds));
3299
3300 FOR_EACH_VEC_ELT (exprs, i, expr)
3301 {
3302 if (expr->kind == NARY
3303 || expr->kind == REFERENCE)
3304 {
3305 unsigned int val;
3306 bool by_some = false;
3307 bool cant_insert = false;
3308 bool all_same = true;
3309 pre_expr first_s = NULL;
3310 edge pred;
3311 basic_block bprime;
3312 pre_expr eprime = NULL;
3313 edge_iterator ei;
3314 pre_expr edoubleprime = NULL;
3315 bool do_insertion = false;
3316
3317 val = get_expr_value_id (expr);
3318 if (bitmap_set_contains_value (PHI_GEN (block), val))
3319 continue;
3320 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3321 {
3322 if (dump_file && (dump_flags & TDF_DETAILS))
3323 {
3324 fprintf (dump_file, "Found fully redundant value: ");
3325 print_pre_expr (dump_file, expr);
3326 fprintf (dump_file, "\n");
3327 }
3328 continue;
3329 }
3330
3331 FOR_EACH_EDGE (pred, ei, block->preds)
3332 {
3333 unsigned int vprime;
3334
3335 /* We should never run insertion for the exit block
3336 and so not come across fake pred edges. */
3337 gcc_assert (!(pred->flags & EDGE_FAKE));
3338 bprime = pred->src;
3339 eprime = phi_translate (expr, ANTIC_IN (block), NULL,
3340 bprime, block);
3341
3342 /* eprime will generally only be NULL if the
3343 value of the expression, translated
3344 through the PHI for this predecessor, is
3345 undefined. If that is the case, we can't
3346 make the expression fully redundant,
3347 because its value is undefined along a
3348 predecessor path. We can thus break out
3349 early because it doesn't matter what the
3350 rest of the results are. */
3351 if (eprime == NULL)
3352 {
3353 avail[pred->dest_idx] = NULL;
3354 cant_insert = true;
3355 break;
3356 }
3357
3358 eprime = fully_constant_expression (eprime);
3359 vprime = get_expr_value_id (eprime);
3360 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3361 vprime);
3362 if (edoubleprime == NULL)
3363 {
3364 avail[pred->dest_idx] = eprime;
3365 all_same = false;
3366 }
3367 else
3368 {
3369 avail[pred->dest_idx] = edoubleprime;
3370 by_some = true;
3371 /* We want to perform insertions to remove a redundancy on
3372 a path in the CFG we want to optimize for speed. */
3373 if (optimize_edge_for_speed_p (pred))
3374 do_insertion = true;
3375 if (first_s == NULL)
3376 first_s = edoubleprime;
3377 else if (!pre_expr_d::equal (first_s, edoubleprime))
3378 all_same = false;
3379 }
3380 }
3381 /* If we can insert it, it's not the same value
3382 already existing along every predecessor, and
3383 it's defined by some predecessor, it is
3384 partially redundant. */
3385 if (!cant_insert && !all_same && by_some)
3386 {
3387 if (!do_insertion)
3388 {
3389 if (dump_file && (dump_flags & TDF_DETAILS))
3390 {
3391 fprintf (dump_file, "Skipping partial redundancy for "
3392 "expression ");
3393 print_pre_expr (dump_file, expr);
3394 fprintf (dump_file, " (%04d), no redundancy on to be "
3395 "optimized for speed edge\n", val);
3396 }
3397 }
3398 else if (dbg_cnt (treepre_insert))
3399 {
3400 if (dump_file && (dump_flags & TDF_DETAILS))
3401 {
3402 fprintf (dump_file, "Found partial redundancy for "
3403 "expression ");
3404 print_pre_expr (dump_file, expr);
3405 fprintf (dump_file, " (%04d)\n",
3406 get_expr_value_id (expr));
3407 }
3408 if (insert_into_preds_of_block (block,
3409 get_expression_id (expr),
3410 avail))
3411 new_stuff = true;
3412 }
3413 }
3414 /* If all edges produce the same value and that value is
3415 an invariant, then the PHI has the same value on all
3416 edges. Note this. */
3417 else if (!cant_insert && all_same)
3418 {
3419 gcc_assert (edoubleprime->kind == CONSTANT
3420 || edoubleprime->kind == NAME);
3421
3422 tree temp = make_temp_ssa_name (get_expr_type (expr),
3423 NULL, "pretmp");
3424 gimple assign = gimple_build_assign (temp,
3425 edoubleprime->kind == CONSTANT ? PRE_EXPR_CONSTANT (edoubleprime) : PRE_EXPR_NAME (edoubleprime));
3426 gimple_stmt_iterator gsi = gsi_after_labels (block);
3427 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3428
3429 gimple_set_plf (assign, NECESSARY, false);
3430 VN_INFO_GET (temp)->value_id = val;
3431 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3432 if (VN_INFO (temp)->valnum == NULL_TREE)
3433 VN_INFO (temp)->valnum = temp;
3434 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3435 pre_expr newe = get_or_alloc_expr_for_name (temp);
3436 add_to_value (val, newe);
3437 bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3438 bitmap_insert_into_set (NEW_SETS (block), newe);
3439 }
3440 }
3441 }
3442
3443 exprs.release ();
3444 avail.release ();
3445 return new_stuff;
3446 }
3447
3448
3449 /* Perform insertion for partially anticipatable expressions. There
3450 is only one case we will perform insertion for these. This case is
3451 if the expression is partially anticipatable, and fully available.
3452 In this case, we know that putting it earlier will enable us to
3453 remove the later computation. */
3454
3455
3456 static bool
3457 do_partial_partial_insertion (basic_block block, basic_block dom)
3458 {
3459 bool new_stuff = false;
3460 vec<pre_expr> exprs;
3461 pre_expr expr;
3462 vec<pre_expr> avail = vNULL;
3463 int i;
3464
3465 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3466 avail.safe_grow (EDGE_COUNT (block->preds));
3467
3468 FOR_EACH_VEC_ELT (exprs, i, expr)
3469 {
3470 if (expr->kind == NARY
3471 || expr->kind == REFERENCE)
3472 {
3473 unsigned int val;
3474 bool by_all = true;
3475 bool cant_insert = false;
3476 edge pred;
3477 basic_block bprime;
3478 pre_expr eprime = NULL;
3479 edge_iterator ei;
3480
3481 val = get_expr_value_id (expr);
3482 if (bitmap_set_contains_value (PHI_GEN (block), val))
3483 continue;
3484 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3485 continue;
3486
3487 FOR_EACH_EDGE (pred, ei, block->preds)
3488 {
3489 unsigned int vprime;
3490 pre_expr edoubleprime;
3491
3492 /* We should never run insertion for the exit block
3493 and so not come across fake pred edges. */
3494 gcc_assert (!(pred->flags & EDGE_FAKE));
3495 bprime = pred->src;
3496 eprime = phi_translate (expr, ANTIC_IN (block),
3497 PA_IN (block),
3498 bprime, block);
3499
3500 /* eprime will generally only be NULL if the
3501 value of the expression, translated
3502 through the PHI for this predecessor, is
3503 undefined. If that is the case, we can't
3504 make the expression fully redundant,
3505 because its value is undefined along a
3506 predecessor path. We can thus break out
3507 early because it doesn't matter what the
3508 rest of the results are. */
3509 if (eprime == NULL)
3510 {
3511 avail[pred->dest_idx] = NULL;
3512 cant_insert = true;
3513 break;
3514 }
3515
3516 eprime = fully_constant_expression (eprime);
3517 vprime = get_expr_value_id (eprime);
3518 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3519 avail[pred->dest_idx] = edoubleprime;
3520 if (edoubleprime == NULL)
3521 {
3522 by_all = false;
3523 break;
3524 }
3525 }
3526
3527 /* If we can insert it, it's not the same value
3528 already existing along every predecessor, and
3529 it's defined by some predecessor, it is
3530 partially redundant. */
3531 if (!cant_insert && by_all)
3532 {
3533 edge succ;
3534 bool do_insertion = false;
3535
3536 /* Insert only if we can remove a later expression on a path
3537 that we want to optimize for speed.
3538 The phi node that we will be inserting in BLOCK is not free,
3539 and inserting it for the sake of !optimize_for_speed successor
3540 may cause regressions on the speed path. */
3541 FOR_EACH_EDGE (succ, ei, block->succs)
3542 {
3543 if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3544 || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3545 {
3546 if (optimize_edge_for_speed_p (succ))
3547 do_insertion = true;
3548 }
3549 }
3550
3551 if (!do_insertion)
3552 {
3553 if (dump_file && (dump_flags & TDF_DETAILS))
3554 {
3555 fprintf (dump_file, "Skipping partial partial redundancy "
3556 "for expression ");
3557 print_pre_expr (dump_file, expr);
3558 fprintf (dump_file, " (%04d), not (partially) anticipated "
3559 "on any to be optimized for speed edges\n", val);
3560 }
3561 }
3562 else if (dbg_cnt (treepre_insert))
3563 {
3564 pre_stats.pa_insert++;
3565 if (dump_file && (dump_flags & TDF_DETAILS))
3566 {
3567 fprintf (dump_file, "Found partial partial redundancy "
3568 "for expression ");
3569 print_pre_expr (dump_file, expr);
3570 fprintf (dump_file, " (%04d)\n",
3571 get_expr_value_id (expr));
3572 }
3573 if (insert_into_preds_of_block (block,
3574 get_expression_id (expr),
3575 avail))
3576 new_stuff = true;
3577 }
3578 }
3579 }
3580 }
3581
3582 exprs.release ();
3583 avail.release ();
3584 return new_stuff;
3585 }
3586
3587 static bool
3588 insert_aux (basic_block block)
3589 {
3590 basic_block son;
3591 bool new_stuff = false;
3592
3593 if (block)
3594 {
3595 basic_block dom;
3596 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3597 if (dom)
3598 {
3599 unsigned i;
3600 bitmap_iterator bi;
3601 bitmap_set_t newset = NEW_SETS (dom);
3602 if (newset)
3603 {
3604 /* Note that we need to value_replace both NEW_SETS, and
3605 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3606 represented by some non-simple expression here that we want
3607 to replace it with. */
3608 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3609 {
3610 pre_expr expr = expression_for_id (i);
3611 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3612 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3613 }
3614 }
3615 if (!single_pred_p (block))
3616 {
3617 new_stuff |= do_regular_insertion (block, dom);
3618 if (do_partial_partial)
3619 new_stuff |= do_partial_partial_insertion (block, dom);
3620 }
3621 }
3622 }
3623 for (son = first_dom_son (CDI_DOMINATORS, block);
3624 son;
3625 son = next_dom_son (CDI_DOMINATORS, son))
3626 {
3627 new_stuff |= insert_aux (son);
3628 }
3629
3630 return new_stuff;
3631 }
3632
3633 /* Perform insertion of partially redundant values. */
3634
3635 static void
3636 insert (void)
3637 {
3638 bool new_stuff = true;
3639 basic_block bb;
3640 int num_iterations = 0;
3641
3642 FOR_ALL_BB (bb)
3643 NEW_SETS (bb) = bitmap_set_new ();
3644
3645 while (new_stuff)
3646 {
3647 num_iterations++;
3648 if (dump_file && dump_flags & TDF_DETAILS)
3649 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3650 new_stuff = insert_aux (ENTRY_BLOCK_PTR);
3651
3652 /* Clear the NEW sets before the next iteration. We have already
3653 fully propagated its contents. */
3654 if (new_stuff)
3655 FOR_ALL_BB (bb)
3656 bitmap_set_free (NEW_SETS (bb));
3657 }
3658 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3659 }
3660
3661
3662 /* Compute the AVAIL set for all basic blocks.
3663
3664 This function performs value numbering of the statements in each basic
3665 block. The AVAIL sets are built from information we glean while doing
3666 this value numbering, since the AVAIL sets contain only one entry per
3667 value.
3668
3669 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3670 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3671
3672 static void
3673 compute_avail (void)
3674 {
3675
3676 basic_block block, son;
3677 basic_block *worklist;
3678 size_t sp = 0;
3679 unsigned i;
3680
3681 /* We pretend that default definitions are defined in the entry block.
3682 This includes function arguments and the static chain decl. */
3683 for (i = 1; i < num_ssa_names; ++i)
3684 {
3685 tree name = ssa_name (i);
3686 pre_expr e;
3687 if (!name
3688 || !SSA_NAME_IS_DEFAULT_DEF (name)
3689 || has_zero_uses (name)
3690 || virtual_operand_p (name))
3691 continue;
3692
3693 e = get_or_alloc_expr_for_name (name);
3694 add_to_value (get_expr_value_id (e), e);
3695 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR), e);
3696 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR), e);
3697 }
3698
3699 if (dump_file && (dump_flags & TDF_DETAILS))
3700 {
3701 print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR),
3702 "tmp_gen", ENTRY_BLOCK);
3703 print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR),
3704 "avail_out", ENTRY_BLOCK);
3705 }
3706
3707 /* Allocate the worklist. */
3708 worklist = XNEWVEC (basic_block, n_basic_blocks);
3709
3710 /* Seed the algorithm by putting the dominator children of the entry
3711 block on the worklist. */
3712 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR);
3713 son;
3714 son = next_dom_son (CDI_DOMINATORS, son))
3715 worklist[sp++] = son;
3716
3717 /* Loop until the worklist is empty. */
3718 while (sp)
3719 {
3720 gimple_stmt_iterator gsi;
3721 gimple stmt;
3722 basic_block dom;
3723
3724 /* Pick a block from the worklist. */
3725 block = worklist[--sp];
3726
3727 /* Initially, the set of available values in BLOCK is that of
3728 its immediate dominator. */
3729 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3730 if (dom)
3731 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3732
3733 /* Generate values for PHI nodes. */
3734 for (gsi = gsi_start_phis (block); !gsi_end_p (gsi); gsi_next (&gsi))
3735 {
3736 tree result = gimple_phi_result (gsi_stmt (gsi));
3737
3738 /* We have no need for virtual phis, as they don't represent
3739 actual computations. */
3740 if (virtual_operand_p (result))
3741 continue;
3742
3743 pre_expr e = get_or_alloc_expr_for_name (result);
3744 add_to_value (get_expr_value_id (e), e);
3745 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3746 bitmap_insert_into_set (PHI_GEN (block), e);
3747 }
3748
3749 BB_MAY_NOTRETURN (block) = 0;
3750
3751 /* Now compute value numbers and populate value sets with all
3752 the expressions computed in BLOCK. */
3753 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
3754 {
3755 ssa_op_iter iter;
3756 tree op;
3757
3758 stmt = gsi_stmt (gsi);
3759
3760 /* Cache whether the basic-block has any non-visible side-effect
3761 or control flow.
3762 If this isn't a call or it is the last stmt in the
3763 basic-block then the CFG represents things correctly. */
3764 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3765 {
3766 /* Non-looping const functions always return normally.
3767 Otherwise the call might not return or have side-effects
3768 that forbids hoisting possibly trapping expressions
3769 before it. */
3770 int flags = gimple_call_flags (stmt);
3771 if (!(flags & ECF_CONST)
3772 || (flags & ECF_LOOPING_CONST_OR_PURE))
3773 BB_MAY_NOTRETURN (block) = 1;
3774 }
3775
3776 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3777 {
3778 pre_expr e = get_or_alloc_expr_for_name (op);
3779
3780 add_to_value (get_expr_value_id (e), e);
3781 bitmap_insert_into_set (TMP_GEN (block), e);
3782 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3783 }
3784
3785 if (gimple_has_side_effects (stmt)
3786 || stmt_could_throw_p (stmt)
3787 || is_gimple_debug (stmt))
3788 continue;
3789
3790 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3791 {
3792 if (ssa_undefined_value_p (op))
3793 continue;
3794 pre_expr e = get_or_alloc_expr_for_name (op);
3795 bitmap_value_insert_into_set (EXP_GEN (block), e);
3796 }
3797
3798 switch (gimple_code (stmt))
3799 {
3800 case GIMPLE_RETURN:
3801 continue;
3802
3803 case GIMPLE_CALL:
3804 {
3805 vn_reference_t ref;
3806 pre_expr result = NULL;
3807 vec<vn_reference_op_s> ops = vNULL;
3808
3809 /* We can value number only calls to real functions. */
3810 if (gimple_call_internal_p (stmt))
3811 continue;
3812
3813 copy_reference_ops_from_call (stmt, &ops);
3814 vn_reference_lookup_pieces (gimple_vuse (stmt), 0,
3815 gimple_expr_type (stmt),
3816 ops, &ref, VN_NOWALK);
3817 ops.release ();
3818 if (!ref)
3819 continue;
3820
3821 /* If the value of the call is not invalidated in
3822 this block until it is computed, add the expression
3823 to EXP_GEN. */
3824 if (!gimple_vuse (stmt)
3825 || gimple_code
3826 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3827 || gimple_bb (SSA_NAME_DEF_STMT
3828 (gimple_vuse (stmt))) != block)
3829 {
3830 result = (pre_expr) pool_alloc (pre_expr_pool);
3831 result->kind = REFERENCE;
3832 result->id = 0;
3833 PRE_EXPR_REFERENCE (result) = ref;
3834
3835 get_or_alloc_expression_id (result);
3836 add_to_value (get_expr_value_id (result), result);
3837 bitmap_value_insert_into_set (EXP_GEN (block), result);
3838 }
3839 continue;
3840 }
3841
3842 case GIMPLE_ASSIGN:
3843 {
3844 pre_expr result = NULL;
3845 switch (vn_get_stmt_kind (stmt))
3846 {
3847 case VN_NARY:
3848 {
3849 enum tree_code code = gimple_assign_rhs_code (stmt);
3850 vn_nary_op_t nary;
3851
3852 /* COND_EXPR and VEC_COND_EXPR are awkward in
3853 that they contain an embedded complex expression.
3854 Don't even try to shove those through PRE. */
3855 if (code == COND_EXPR
3856 || code == VEC_COND_EXPR)
3857 continue;
3858
3859 vn_nary_op_lookup_stmt (stmt, &nary);
3860 if (!nary)
3861 continue;
3862
3863 /* If the NARY traps and there was a preceding
3864 point in the block that might not return avoid
3865 adding the nary to EXP_GEN. */
3866 if (BB_MAY_NOTRETURN (block)
3867 && vn_nary_may_trap (nary))
3868 continue;
3869
3870 result = (pre_expr) pool_alloc (pre_expr_pool);
3871 result->kind = NARY;
3872 result->id = 0;
3873 PRE_EXPR_NARY (result) = nary;
3874 break;
3875 }
3876
3877 case VN_REFERENCE:
3878 {
3879 vn_reference_t ref;
3880 vn_reference_lookup (gimple_assign_rhs1 (stmt),
3881 gimple_vuse (stmt),
3882 VN_WALK, &ref);
3883 if (!ref)
3884 continue;
3885
3886 /* If the value of the reference is not invalidated in
3887 this block until it is computed, add the expression
3888 to EXP_GEN. */
3889 if (gimple_vuse (stmt))
3890 {
3891 gimple def_stmt;
3892 bool ok = true;
3893 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
3894 while (!gimple_nop_p (def_stmt)
3895 && gimple_code (def_stmt) != GIMPLE_PHI
3896 && gimple_bb (def_stmt) == block)
3897 {
3898 if (stmt_may_clobber_ref_p
3899 (def_stmt, gimple_assign_rhs1 (stmt)))
3900 {
3901 ok = false;
3902 break;
3903 }
3904 def_stmt
3905 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
3906 }
3907 if (!ok)
3908 continue;
3909 }
3910
3911 result = (pre_expr) pool_alloc (pre_expr_pool);
3912 result->kind = REFERENCE;
3913 result->id = 0;
3914 PRE_EXPR_REFERENCE (result) = ref;
3915 break;
3916 }
3917
3918 default:
3919 continue;
3920 }
3921
3922 get_or_alloc_expression_id (result);
3923 add_to_value (get_expr_value_id (result), result);
3924 bitmap_value_insert_into_set (EXP_GEN (block), result);
3925 continue;
3926 }
3927 default:
3928 break;
3929 }
3930 }
3931
3932 if (dump_file && (dump_flags & TDF_DETAILS))
3933 {
3934 print_bitmap_set (dump_file, EXP_GEN (block),
3935 "exp_gen", block->index);
3936 print_bitmap_set (dump_file, PHI_GEN (block),
3937 "phi_gen", block->index);
3938 print_bitmap_set (dump_file, TMP_GEN (block),
3939 "tmp_gen", block->index);
3940 print_bitmap_set (dump_file, AVAIL_OUT (block),
3941 "avail_out", block->index);
3942 }
3943
3944 /* Put the dominator children of BLOCK on the worklist of blocks
3945 to compute available sets for. */
3946 for (son = first_dom_son (CDI_DOMINATORS, block);
3947 son;
3948 son = next_dom_son (CDI_DOMINATORS, son))
3949 worklist[sp++] = son;
3950 }
3951
3952 free (worklist);
3953 }
3954
3955
3956 /* Local state for the eliminate domwalk. */
3957 static vec<gimple> el_to_remove;
3958 static vec<gimple> el_to_update;
3959 static unsigned int el_todo;
3960 static vec<tree> el_avail;
3961 static vec<tree> el_avail_stack;
3962
3963 /* Return a leader for OP that is available at the current point of the
3964 eliminate domwalk. */
3965
3966 static tree
3967 eliminate_avail (tree op)
3968 {
3969 tree valnum = VN_INFO (op)->valnum;
3970 if (TREE_CODE (valnum) == SSA_NAME)
3971 {
3972 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
3973 return valnum;
3974 if (el_avail.length () > SSA_NAME_VERSION (valnum))
3975 return el_avail[SSA_NAME_VERSION (valnum)];
3976 }
3977 else if (is_gimple_min_invariant (valnum))
3978 return valnum;
3979 return NULL_TREE;
3980 }
3981
3982 /* At the current point of the eliminate domwalk make OP available. */
3983
3984 static void
3985 eliminate_push_avail (tree op)
3986 {
3987 tree valnum = VN_INFO (op)->valnum;
3988 if (TREE_CODE (valnum) == SSA_NAME)
3989 {
3990 if (el_avail.length () <= SSA_NAME_VERSION (valnum))
3991 el_avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1);
3992 el_avail[SSA_NAME_VERSION (valnum)] = op;
3993 el_avail_stack.safe_push (op);
3994 }
3995 }
3996
3997 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
3998 the leader for the expression if insertion was successful. */
3999
4000 static tree
4001 eliminate_insert (gimple_stmt_iterator *gsi, tree val)
4002 {
4003 tree expr = vn_get_expr_for (val);
4004 if (!CONVERT_EXPR_P (expr)
4005 && TREE_CODE (expr) != VIEW_CONVERT_EXPR)
4006 return NULL_TREE;
4007
4008 tree op = TREE_OPERAND (expr, 0);
4009 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (op) : op;
4010 if (!leader)
4011 return NULL_TREE;
4012
4013 tree res = make_temp_ssa_name (TREE_TYPE (val), NULL, "pretmp");
4014 gimple tem = gimple_build_assign (res,
4015 fold_build1 (TREE_CODE (expr),
4016 TREE_TYPE (expr), leader));
4017 gsi_insert_before (gsi, tem, GSI_SAME_STMT);
4018 VN_INFO_GET (res)->valnum = val;
4019
4020 if (TREE_CODE (leader) == SSA_NAME)
4021 gimple_set_plf (SSA_NAME_DEF_STMT (leader), NECESSARY, true);
4022
4023 pre_stats.insertions++;
4024 if (dump_file && (dump_flags & TDF_DETAILS))
4025 {
4026 fprintf (dump_file, "Inserted ");
4027 print_gimple_stmt (dump_file, tem, 0, 0);
4028 }
4029
4030 return res;
4031 }
4032
4033 class eliminate_dom_walker : public dom_walker
4034 {
4035 public:
4036 eliminate_dom_walker (cdi_direction direction) : dom_walker (direction) {}
4037
4038 virtual void before_dom_children (basic_block);
4039 virtual void after_dom_children (basic_block);
4040 };
4041
4042 /* Perform elimination for the basic-block B during the domwalk. */
4043
4044 void
4045 eliminate_dom_walker::before_dom_children (basic_block b)
4046 {
4047 gimple_stmt_iterator gsi;
4048 gimple stmt;
4049
4050 /* Mark new bb. */
4051 el_avail_stack.safe_push (NULL_TREE);
4052
4053 for (gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
4054 {
4055 gimple stmt, phi = gsi_stmt (gsi);
4056 tree sprime = NULL_TREE, res = PHI_RESULT (phi);
4057 gimple_stmt_iterator gsi2;
4058
4059 /* We want to perform redundant PHI elimination. Do so by
4060 replacing the PHI with a single copy if possible.
4061 Do not touch inserted, single-argument or virtual PHIs. */
4062 if (gimple_phi_num_args (phi) == 1
4063 || virtual_operand_p (res))
4064 {
4065 gsi_next (&gsi);
4066 continue;
4067 }
4068
4069 sprime = eliminate_avail (res);
4070 if (!sprime
4071 || sprime == res)
4072 {
4073 eliminate_push_avail (res);
4074 gsi_next (&gsi);
4075 continue;
4076 }
4077 else if (is_gimple_min_invariant (sprime))
4078 {
4079 if (!useless_type_conversion_p (TREE_TYPE (res),
4080 TREE_TYPE (sprime)))
4081 sprime = fold_convert (TREE_TYPE (res), sprime);
4082 }
4083
4084 if (dump_file && (dump_flags & TDF_DETAILS))
4085 {
4086 fprintf (dump_file, "Replaced redundant PHI node defining ");
4087 print_generic_expr (dump_file, res, 0);
4088 fprintf (dump_file, " with ");
4089 print_generic_expr (dump_file, sprime, 0);
4090 fprintf (dump_file, "\n");
4091 }
4092
4093 remove_phi_node (&gsi, false);
4094
4095 if (inserted_exprs
4096 && !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res))
4097 && TREE_CODE (sprime) == SSA_NAME)
4098 gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
4099
4100 if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
4101 sprime = fold_convert (TREE_TYPE (res), sprime);
4102 stmt = gimple_build_assign (res, sprime);
4103 SSA_NAME_DEF_STMT (res) = stmt;
4104 gimple_set_plf (stmt, NECESSARY, gimple_plf (phi, NECESSARY));
4105
4106 gsi2 = gsi_after_labels (b);
4107 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
4108 /* Queue the copy for eventual removal. */
4109 el_to_remove.safe_push (stmt);
4110 /* If we inserted this PHI node ourself, it's not an elimination. */
4111 if (inserted_exprs
4112 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
4113 pre_stats.phis--;
4114 else
4115 pre_stats.eliminations++;
4116 }
4117
4118 for (gsi = gsi_start_bb (b); !gsi_end_p (gsi); gsi_next (&gsi))
4119 {
4120 tree lhs = NULL_TREE;
4121 tree rhs = NULL_TREE;
4122
4123 stmt = gsi_stmt (gsi);
4124
4125 if (gimple_has_lhs (stmt))
4126 lhs = gimple_get_lhs (stmt);
4127
4128 if (gimple_assign_single_p (stmt))
4129 rhs = gimple_assign_rhs1 (stmt);
4130
4131 /* Lookup the RHS of the expression, see if we have an
4132 available computation for it. If so, replace the RHS with
4133 the available computation. */
4134 if (gimple_has_lhs (stmt)
4135 && TREE_CODE (lhs) == SSA_NAME
4136 && !gimple_has_volatile_ops (stmt))
4137 {
4138 tree sprime;
4139 gimple orig_stmt = stmt;
4140
4141 sprime = eliminate_avail (lhs);
4142 /* If there is no usable leader mark lhs as leader for its value. */
4143 if (!sprime)
4144 eliminate_push_avail (lhs);
4145
4146 /* See PR43491. Do not replace a global register variable when
4147 it is a the RHS of an assignment. Do replace local register
4148 variables since gcc does not guarantee a local variable will
4149 be allocated in register.
4150 Do not perform copy propagation or undo constant propagation. */
4151 if (gimple_assign_single_p (stmt)
4152 && (TREE_CODE (rhs) == SSA_NAME
4153 || is_gimple_min_invariant (rhs)
4154 || (TREE_CODE (rhs) == VAR_DECL
4155 && is_global_var (rhs)
4156 && DECL_HARD_REGISTER (rhs))))
4157 continue;
4158
4159 if (!sprime)
4160 {
4161 /* If there is no existing usable leader but SCCVN thinks
4162 it has an expression it wants to use as replacement,
4163 insert that. */
4164 tree val = VN_INFO (lhs)->valnum;
4165 if (val != VN_TOP
4166 && TREE_CODE (val) == SSA_NAME
4167 && VN_INFO (val)->needs_insertion
4168 && VN_INFO (val)->expr != NULL_TREE
4169 && (sprime = eliminate_insert (&gsi, val)) != NULL_TREE)
4170 eliminate_push_avail (sprime);
4171 }
4172 else if (is_gimple_min_invariant (sprime))
4173 {
4174 /* If there is no existing leader but SCCVN knows this
4175 value is constant, use that constant. */
4176 if (!useless_type_conversion_p (TREE_TYPE (lhs),
4177 TREE_TYPE (sprime)))
4178 sprime = fold_convert (TREE_TYPE (lhs), sprime);
4179
4180 if (dump_file && (dump_flags & TDF_DETAILS))
4181 {
4182 fprintf (dump_file, "Replaced ");
4183 print_gimple_expr (dump_file, stmt, 0, 0);
4184 fprintf (dump_file, " with ");
4185 print_generic_expr (dump_file, sprime, 0);
4186 fprintf (dump_file, " in ");
4187 print_gimple_stmt (dump_file, stmt, 0, 0);
4188 }
4189 pre_stats.eliminations++;
4190 propagate_tree_value_into_stmt (&gsi, sprime);
4191 stmt = gsi_stmt (gsi);
4192 update_stmt (stmt);
4193
4194 /* If we removed EH side-effects from the statement, clean
4195 its EH information. */
4196 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4197 {
4198 bitmap_set_bit (need_eh_cleanup,
4199 gimple_bb (stmt)->index);
4200 if (dump_file && (dump_flags & TDF_DETAILS))
4201 fprintf (dump_file, " Removed EH side-effects.\n");
4202 }
4203 continue;
4204 }
4205
4206 if (sprime
4207 && sprime != lhs
4208 && (rhs == NULL_TREE
4209 || TREE_CODE (rhs) != SSA_NAME
4210 || may_propagate_copy (rhs, sprime)))
4211 {
4212 bool can_make_abnormal_goto
4213 = is_gimple_call (stmt)
4214 && stmt_can_make_abnormal_goto (stmt);
4215
4216 gcc_assert (sprime != rhs);
4217
4218 if (dump_file && (dump_flags & TDF_DETAILS))
4219 {
4220 fprintf (dump_file, "Replaced ");
4221 print_gimple_expr (dump_file, stmt, 0, 0);
4222 fprintf (dump_file, " with ");
4223 print_generic_expr (dump_file, sprime, 0);
4224 fprintf (dump_file, " in ");
4225 print_gimple_stmt (dump_file, stmt, 0, 0);
4226 }
4227
4228 if (TREE_CODE (sprime) == SSA_NAME)
4229 gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
4230 NECESSARY, true);
4231 /* We need to make sure the new and old types actually match,
4232 which may require adding a simple cast, which fold_convert
4233 will do for us. */
4234 if ((!rhs || TREE_CODE (rhs) != SSA_NAME)
4235 && !useless_type_conversion_p (gimple_expr_type (stmt),
4236 TREE_TYPE (sprime)))
4237 sprime = fold_convert (gimple_expr_type (stmt), sprime);
4238
4239 pre_stats.eliminations++;
4240 propagate_tree_value_into_stmt (&gsi, sprime);
4241 stmt = gsi_stmt (gsi);
4242 update_stmt (stmt);
4243
4244 /* If we removed EH side-effects from the statement, clean
4245 its EH information. */
4246 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4247 {
4248 bitmap_set_bit (need_eh_cleanup,
4249 gimple_bb (stmt)->index);
4250 if (dump_file && (dump_flags & TDF_DETAILS))
4251 fprintf (dump_file, " Removed EH side-effects.\n");
4252 }
4253
4254 /* Likewise for AB side-effects. */
4255 if (can_make_abnormal_goto
4256 && !stmt_can_make_abnormal_goto (stmt))
4257 {
4258 bitmap_set_bit (need_ab_cleanup,
4259 gimple_bb (stmt)->index);
4260 if (dump_file && (dump_flags & TDF_DETAILS))
4261 fprintf (dump_file, " Removed AB side-effects.\n");
4262 }
4263 }
4264 }
4265 /* If the statement is a scalar store, see if the expression
4266 has the same value number as its rhs. If so, the store is
4267 dead. */
4268 else if (gimple_assign_single_p (stmt)
4269 && !gimple_has_volatile_ops (stmt)
4270 && !is_gimple_reg (gimple_assign_lhs (stmt))
4271 && (TREE_CODE (rhs) == SSA_NAME
4272 || is_gimple_min_invariant (rhs)))
4273 {
4274 tree val;
4275 val = vn_reference_lookup (gimple_assign_lhs (stmt),
4276 gimple_vuse (stmt), VN_WALK, NULL);
4277 if (TREE_CODE (rhs) == SSA_NAME)
4278 rhs = VN_INFO (rhs)->valnum;
4279 if (val
4280 && operand_equal_p (val, rhs, 0))
4281 {
4282 if (dump_file && (dump_flags & TDF_DETAILS))
4283 {
4284 fprintf (dump_file, "Deleted redundant store ");
4285 print_gimple_stmt (dump_file, stmt, 0, 0);
4286 }
4287
4288 /* Queue stmt for removal. */
4289 el_to_remove.safe_push (stmt);
4290 }
4291 }
4292 /* Visit COND_EXPRs and fold the comparison with the
4293 available value-numbers. */
4294 else if (gimple_code (stmt) == GIMPLE_COND)
4295 {
4296 tree op0 = gimple_cond_lhs (stmt);
4297 tree op1 = gimple_cond_rhs (stmt);
4298 tree result;
4299
4300 if (TREE_CODE (op0) == SSA_NAME)
4301 op0 = VN_INFO (op0)->valnum;
4302 if (TREE_CODE (op1) == SSA_NAME)
4303 op1 = VN_INFO (op1)->valnum;
4304 result = fold_binary (gimple_cond_code (stmt), boolean_type_node,
4305 op0, op1);
4306 if (result && TREE_CODE (result) == INTEGER_CST)
4307 {
4308 if (integer_zerop (result))
4309 gimple_cond_make_false (stmt);
4310 else
4311 gimple_cond_make_true (stmt);
4312 update_stmt (stmt);
4313 el_todo = TODO_cleanup_cfg;
4314 }
4315 }
4316 /* Visit indirect calls and turn them into direct calls if
4317 possible. */
4318 if (is_gimple_call (stmt))
4319 {
4320 tree orig_fn = gimple_call_fn (stmt);
4321 tree fn;
4322 if (!orig_fn)
4323 continue;
4324 if (TREE_CODE (orig_fn) == SSA_NAME)
4325 fn = VN_INFO (orig_fn)->valnum;
4326 else if (TREE_CODE (orig_fn) == OBJ_TYPE_REF
4327 && TREE_CODE (OBJ_TYPE_REF_EXPR (orig_fn)) == SSA_NAME)
4328 {
4329 fn = VN_INFO (OBJ_TYPE_REF_EXPR (orig_fn))->valnum;
4330 if (!gimple_call_addr_fndecl (fn))
4331 {
4332 fn = ipa_intraprocedural_devirtualization (stmt);
4333 if (fn)
4334 fn = build_fold_addr_expr (fn);
4335 }
4336 }
4337 else
4338 continue;
4339 if (gimple_call_addr_fndecl (fn) != NULL_TREE
4340 && useless_type_conversion_p (TREE_TYPE (orig_fn),
4341 TREE_TYPE (fn)))
4342 {
4343 bool can_make_abnormal_goto
4344 = stmt_can_make_abnormal_goto (stmt);
4345 bool was_noreturn = gimple_call_noreturn_p (stmt);
4346
4347 if (dump_file && (dump_flags & TDF_DETAILS))
4348 {
4349 fprintf (dump_file, "Replacing call target with ");
4350 print_generic_expr (dump_file, fn, 0);
4351 fprintf (dump_file, " in ");
4352 print_gimple_stmt (dump_file, stmt, 0, 0);
4353 }
4354
4355 gimple_call_set_fn (stmt, fn);
4356 el_to_update.safe_push (stmt);
4357
4358 /* When changing a call into a noreturn call, cfg cleanup
4359 is needed to fix up the noreturn call. */
4360 if (!was_noreturn && gimple_call_noreturn_p (stmt))
4361 el_todo |= TODO_cleanup_cfg;
4362
4363 /* If we removed EH side-effects from the statement, clean
4364 its EH information. */
4365 if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
4366 {
4367 bitmap_set_bit (need_eh_cleanup,
4368 gimple_bb (stmt)->index);
4369 if (dump_file && (dump_flags & TDF_DETAILS))
4370 fprintf (dump_file, " Removed EH side-effects.\n");
4371 }
4372
4373 /* Likewise for AB side-effects. */
4374 if (can_make_abnormal_goto
4375 && !stmt_can_make_abnormal_goto (stmt))
4376 {
4377 bitmap_set_bit (need_ab_cleanup,
4378 gimple_bb (stmt)->index);
4379 if (dump_file && (dump_flags & TDF_DETAILS))
4380 fprintf (dump_file, " Removed AB side-effects.\n");
4381 }
4382
4383 /* Changing an indirect call to a direct call may
4384 have exposed different semantics. This may
4385 require an SSA update. */
4386 el_todo |= TODO_update_ssa_only_virtuals;
4387 }
4388 }
4389 }
4390 }
4391
4392 /* Make no longer available leaders no longer available. */
4393
4394 void
4395 eliminate_dom_walker::after_dom_children (basic_block)
4396 {
4397 tree entry;
4398 while ((entry = el_avail_stack.pop ()) != NULL_TREE)
4399 el_avail[SSA_NAME_VERSION (VN_INFO (entry)->valnum)] = NULL_TREE;
4400 }
4401
4402 /* Eliminate fully redundant computations. */
4403
4404 static unsigned int
4405 eliminate (void)
4406 {
4407 gimple_stmt_iterator gsi;
4408 gimple stmt;
4409 unsigned i;
4410
4411 need_eh_cleanup = BITMAP_ALLOC (NULL);
4412 need_ab_cleanup = BITMAP_ALLOC (NULL);
4413
4414 el_to_remove.create (0);
4415 el_to_update.create (0);
4416 el_todo = 0;
4417 el_avail.create (0);
4418 el_avail_stack.create (0);
4419
4420 eliminate_dom_walker (CDI_DOMINATORS).walk (cfun->cfg->x_entry_block_ptr);
4421
4422 el_avail.release ();
4423 el_avail_stack.release ();
4424
4425 /* We cannot remove stmts during BB walk, especially not release SSA
4426 names there as this confuses the VN machinery. The stmts ending
4427 up in el_to_remove are either stores or simple copies. */
4428 FOR_EACH_VEC_ELT (el_to_remove, i, stmt)
4429 {
4430 tree lhs = gimple_assign_lhs (stmt);
4431 tree rhs = gimple_assign_rhs1 (stmt);
4432 use_operand_p use_p;
4433 gimple use_stmt;
4434
4435 /* If there is a single use only, propagate the equivalency
4436 instead of keeping the copy. */
4437 if (TREE_CODE (lhs) == SSA_NAME
4438 && TREE_CODE (rhs) == SSA_NAME
4439 && single_imm_use (lhs, &use_p, &use_stmt)
4440 && may_propagate_copy (USE_FROM_PTR (use_p), rhs))
4441 {
4442 SET_USE (use_p, rhs);
4443 update_stmt (use_stmt);
4444 if (inserted_exprs
4445 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (lhs))
4446 && TREE_CODE (rhs) == SSA_NAME)
4447 gimple_set_plf (SSA_NAME_DEF_STMT (rhs), NECESSARY, true);
4448 }
4449
4450 /* If this is a store or a now unused copy, remove it. */
4451 if (TREE_CODE (lhs) != SSA_NAME
4452 || has_zero_uses (lhs))
4453 {
4454 basic_block bb = gimple_bb (stmt);
4455 gsi = gsi_for_stmt (stmt);
4456 unlink_stmt_vdef (stmt);
4457 if (gsi_remove (&gsi, true))
4458 bitmap_set_bit (need_eh_cleanup, bb->index);
4459 if (inserted_exprs
4460 && TREE_CODE (lhs) == SSA_NAME)
4461 bitmap_clear_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
4462 release_defs (stmt);
4463 }
4464 }
4465 el_to_remove.release ();
4466
4467 /* We cannot update call statements with virtual operands during
4468 SSA walk. This might remove them which in turn makes our
4469 VN lattice invalid. */
4470 FOR_EACH_VEC_ELT (el_to_update, i, stmt)
4471 update_stmt (stmt);
4472 el_to_update.release ();
4473
4474 return el_todo;
4475 }
4476
4477 /* Perform CFG cleanups made necessary by elimination. */
4478
4479 static unsigned
4480 fini_eliminate (void)
4481 {
4482 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
4483 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
4484
4485 if (do_eh_cleanup)
4486 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
4487
4488 if (do_ab_cleanup)
4489 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
4490
4491 BITMAP_FREE (need_eh_cleanup);
4492 BITMAP_FREE (need_ab_cleanup);
4493
4494 if (do_eh_cleanup || do_ab_cleanup)
4495 return TODO_cleanup_cfg;
4496 return 0;
4497 }
4498
4499 /* Borrow a bit of tree-ssa-dce.c for the moment.
4500 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4501 this may be a bit faster, and we may want critical edges kept split. */
4502
4503 /* If OP's defining statement has not already been determined to be necessary,
4504 mark that statement necessary. Return the stmt, if it is newly
4505 necessary. */
4506
4507 static inline gimple
4508 mark_operand_necessary (tree op)
4509 {
4510 gimple stmt;
4511
4512 gcc_assert (op);
4513
4514 if (TREE_CODE (op) != SSA_NAME)
4515 return NULL;
4516
4517 stmt = SSA_NAME_DEF_STMT (op);
4518 gcc_assert (stmt);
4519
4520 if (gimple_plf (stmt, NECESSARY)
4521 || gimple_nop_p (stmt))
4522 return NULL;
4523
4524 gimple_set_plf (stmt, NECESSARY, true);
4525 return stmt;
4526 }
4527
4528 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4529 to insert PHI nodes sometimes, and because value numbering of casts isn't
4530 perfect, we sometimes end up inserting dead code. This simple DCE-like
4531 pass removes any insertions we made that weren't actually used. */
4532
4533 static void
4534 remove_dead_inserted_code (void)
4535 {
4536 bitmap worklist;
4537 unsigned i;
4538 bitmap_iterator bi;
4539 gimple t;
4540
4541 worklist = BITMAP_ALLOC (NULL);
4542 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4543 {
4544 t = SSA_NAME_DEF_STMT (ssa_name (i));
4545 if (gimple_plf (t, NECESSARY))
4546 bitmap_set_bit (worklist, i);
4547 }
4548 while (!bitmap_empty_p (worklist))
4549 {
4550 i = bitmap_first_set_bit (worklist);
4551 bitmap_clear_bit (worklist, i);
4552 t = SSA_NAME_DEF_STMT (ssa_name (i));
4553
4554 /* PHI nodes are somewhat special in that each PHI alternative has
4555 data and control dependencies. All the statements feeding the
4556 PHI node's arguments are always necessary. */
4557 if (gimple_code (t) == GIMPLE_PHI)
4558 {
4559 unsigned k;
4560
4561 for (k = 0; k < gimple_phi_num_args (t); k++)
4562 {
4563 tree arg = PHI_ARG_DEF (t, k);
4564 if (TREE_CODE (arg) == SSA_NAME)
4565 {
4566 gimple n = mark_operand_necessary (arg);
4567 if (n)
4568 bitmap_set_bit (worklist, SSA_NAME_VERSION (arg));
4569 }
4570 }
4571 }
4572 else
4573 {
4574 /* Propagate through the operands. Examine all the USE, VUSE and
4575 VDEF operands in this statement. Mark all the statements
4576 which feed this statement's uses as necessary. */
4577 ssa_op_iter iter;
4578 tree use;
4579
4580 /* The operands of VDEF expressions are also needed as they
4581 represent potential definitions that may reach this
4582 statement (VDEF operands allow us to follow def-def
4583 links). */
4584
4585 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
4586 {
4587 gimple n = mark_operand_necessary (use);
4588 if (n)
4589 bitmap_set_bit (worklist, SSA_NAME_VERSION (use));
4590 }
4591 }
4592 }
4593
4594 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4595 {
4596 t = SSA_NAME_DEF_STMT (ssa_name (i));
4597 if (!gimple_plf (t, NECESSARY))
4598 {
4599 gimple_stmt_iterator gsi;
4600
4601 if (dump_file && (dump_flags & TDF_DETAILS))
4602 {
4603 fprintf (dump_file, "Removing unnecessary insertion:");
4604 print_gimple_stmt (dump_file, t, 0, 0);
4605 }
4606
4607 gsi = gsi_for_stmt (t);
4608 if (gimple_code (t) == GIMPLE_PHI)
4609 remove_phi_node (&gsi, true);
4610 else
4611 {
4612 gsi_remove (&gsi, true);
4613 release_defs (t);
4614 }
4615 }
4616 }
4617 BITMAP_FREE (worklist);
4618 }
4619
4620
4621 /* Initialize data structures used by PRE. */
4622
4623 static void
4624 init_pre (void)
4625 {
4626 basic_block bb;
4627
4628 next_expression_id = 1;
4629 expressions.create (0);
4630 expressions.safe_push (NULL);
4631 value_expressions.create (get_max_value_id () + 1);
4632 value_expressions.safe_grow_cleared (get_max_value_id() + 1);
4633 name_to_id.create (0);
4634
4635 inserted_exprs = BITMAP_ALLOC (NULL);
4636
4637 connect_infinite_loops_to_exit ();
4638 memset (&pre_stats, 0, sizeof (pre_stats));
4639
4640 postorder = XNEWVEC (int, n_basic_blocks);
4641 postorder_num = inverted_post_order_compute (postorder);
4642
4643 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4644
4645 calculate_dominance_info (CDI_POST_DOMINATORS);
4646 calculate_dominance_info (CDI_DOMINATORS);
4647
4648 bitmap_obstack_initialize (&grand_bitmap_obstack);
4649 phi_translate_table.create (5110);
4650 expression_to_id.create (num_ssa_names * 3);
4651 bitmap_set_pool = create_alloc_pool ("Bitmap sets",
4652 sizeof (struct bitmap_set), 30);
4653 pre_expr_pool = create_alloc_pool ("pre_expr nodes",
4654 sizeof (struct pre_expr_d), 30);
4655 FOR_ALL_BB (bb)
4656 {
4657 EXP_GEN (bb) = bitmap_set_new ();
4658 PHI_GEN (bb) = bitmap_set_new ();
4659 TMP_GEN (bb) = bitmap_set_new ();
4660 AVAIL_OUT (bb) = bitmap_set_new ();
4661 }
4662 }
4663
4664
4665 /* Deallocate data structures used by PRE. */
4666
4667 static void
4668 fini_pre ()
4669 {
4670 free (postorder);
4671 value_expressions.release ();
4672 BITMAP_FREE (inserted_exprs);
4673 bitmap_obstack_release (&grand_bitmap_obstack);
4674 free_alloc_pool (bitmap_set_pool);
4675 free_alloc_pool (pre_expr_pool);
4676 phi_translate_table.dispose ();
4677 expression_to_id.dispose ();
4678 name_to_id.release ();
4679
4680 free_aux_for_blocks ();
4681
4682 free_dominance_info (CDI_POST_DOMINATORS);
4683 }
4684
4685 /* Gate and execute functions for PRE. */
4686
4687 static unsigned int
4688 do_pre (void)
4689 {
4690 unsigned int todo = 0;
4691
4692 do_partial_partial =
4693 flag_tree_partial_pre && optimize_function_for_speed_p (cfun);
4694
4695 /* This has to happen before SCCVN runs because
4696 loop_optimizer_init may create new phis, etc. */
4697 loop_optimizer_init (LOOPS_NORMAL);
4698
4699 if (!run_scc_vn (VN_WALK))
4700 {
4701 loop_optimizer_finalize ();
4702 return 0;
4703 }
4704
4705 init_pre ();
4706 scev_initialize ();
4707
4708 /* Collect and value number expressions computed in each basic block. */
4709 compute_avail ();
4710
4711 /* Insert can get quite slow on an incredibly large number of basic
4712 blocks due to some quadratic behavior. Until this behavior is
4713 fixed, don't run it when he have an incredibly large number of
4714 bb's. If we aren't going to run insert, there is no point in
4715 computing ANTIC, either, even though it's plenty fast. */
4716 if (n_basic_blocks < 4000)
4717 {
4718 compute_antic ();
4719 insert ();
4720 }
4721
4722 /* Make sure to remove fake edges before committing our inserts.
4723 This makes sure we don't end up with extra critical edges that
4724 we would need to split. */
4725 remove_fake_exit_edges ();
4726 gsi_commit_edge_inserts ();
4727
4728 /* Remove all the redundant expressions. */
4729 todo |= eliminate ();
4730
4731 statistics_counter_event (cfun, "Insertions", pre_stats.insertions);
4732 statistics_counter_event (cfun, "PA inserted", pre_stats.pa_insert);
4733 statistics_counter_event (cfun, "New PHIs", pre_stats.phis);
4734 statistics_counter_event (cfun, "Eliminated", pre_stats.eliminations);
4735
4736 clear_expression_ids ();
4737 remove_dead_inserted_code ();
4738 todo |= TODO_verify_flow;
4739
4740 scev_finalize ();
4741 fini_pre ();
4742 todo |= fini_eliminate ();
4743 loop_optimizer_finalize ();
4744
4745 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4746 case we can merge the block with the remaining predecessor of the block.
4747 It should either:
4748 - call merge_blocks after each tail merge iteration
4749 - call merge_blocks after all tail merge iterations
4750 - mark TODO_cleanup_cfg when necessary
4751 - share the cfg cleanup with fini_pre. */
4752 todo |= tail_merge_optimize (todo);
4753
4754 free_scc_vn ();
4755
4756 /* Tail merging invalidates the virtual SSA web, together with
4757 cfg-cleanup opportunities exposed by PRE this will wreck the
4758 SSA updating machinery. So make sure to run update-ssa
4759 manually, before eventually scheduling cfg-cleanup as part of
4760 the todo. */
4761 update_ssa (TODO_update_ssa_only_virtuals);
4762
4763 return todo;
4764 }
4765
4766 static bool
4767 gate_pre (void)
4768 {
4769 return flag_tree_pre != 0;
4770 }
4771
4772 namespace {
4773
4774 const pass_data pass_data_pre =
4775 {
4776 GIMPLE_PASS, /* type */
4777 "pre", /* name */
4778 OPTGROUP_NONE, /* optinfo_flags */
4779 true, /* has_gate */
4780 true, /* has_execute */
4781 TV_TREE_PRE, /* tv_id */
4782 ( PROP_no_crit_edges | PROP_cfg | PROP_ssa ), /* properties_required */
4783 0, /* properties_provided */
4784 0, /* properties_destroyed */
4785 TODO_rebuild_alias, /* todo_flags_start */
4786 TODO_verify_ssa, /* todo_flags_finish */
4787 };
4788
4789 class pass_pre : public gimple_opt_pass
4790 {
4791 public:
4792 pass_pre(gcc::context *ctxt)
4793 : gimple_opt_pass(pass_data_pre, ctxt)
4794 {}
4795
4796 /* opt_pass methods: */
4797 bool gate () { return gate_pre (); }
4798 unsigned int execute () { return do_pre (); }
4799
4800 }; // class pass_pre
4801
4802 } // anon namespace
4803
4804 gimple_opt_pass *
4805 make_pass_pre (gcc::context *ctxt)
4806 {
4807 return new pass_pre (ctxt);
4808 }
4809
4810
4811 /* Gate and execute functions for FRE. */
4812
4813 static unsigned int
4814 execute_fre (void)
4815 {
4816 unsigned int todo = 0;
4817
4818 if (!run_scc_vn (VN_WALKREWRITE))
4819 return 0;
4820
4821 memset (&pre_stats, 0, sizeof (pre_stats));
4822
4823 /* Remove all the redundant expressions. */
4824 todo |= eliminate ();
4825
4826 todo |= fini_eliminate ();
4827
4828 free_scc_vn ();
4829
4830 statistics_counter_event (cfun, "Insertions", pre_stats.insertions);
4831 statistics_counter_event (cfun, "Eliminated", pre_stats.eliminations);
4832
4833 return todo;
4834 }
4835
4836 static bool
4837 gate_fre (void)
4838 {
4839 return flag_tree_fre != 0;
4840 }
4841
4842 namespace {
4843
4844 const pass_data pass_data_fre =
4845 {
4846 GIMPLE_PASS, /* type */
4847 "fre", /* name */
4848 OPTGROUP_NONE, /* optinfo_flags */
4849 true, /* has_gate */
4850 true, /* has_execute */
4851 TV_TREE_FRE, /* tv_id */
4852 ( PROP_cfg | PROP_ssa ), /* properties_required */
4853 0, /* properties_provided */
4854 0, /* properties_destroyed */
4855 0, /* todo_flags_start */
4856 TODO_verify_ssa, /* todo_flags_finish */
4857 };
4858
4859 class pass_fre : public gimple_opt_pass
4860 {
4861 public:
4862 pass_fre(gcc::context *ctxt)
4863 : gimple_opt_pass(pass_data_fre, ctxt)
4864 {}
4865
4866 /* opt_pass methods: */
4867 opt_pass * clone () { return new pass_fre (ctxt_); }
4868 bool gate () { return gate_fre (); }
4869 unsigned int execute () { return execute_fre (); }
4870
4871 }; // class pass_fre
4872
4873 } // anon namespace
4874
4875 gimple_opt_pass *
4876 make_pass_fre (gcc::context *ctxt)
4877 {
4878 return new pass_fre (ctxt);
4879 }