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