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