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