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