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