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