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