Debian Codegraph @

gcc/tree-ssa-pre.cc

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