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