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