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