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