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