re PR tree-optimization/77514 (ICE in VN_INFO_GET, at tree-ssa-sccvn.c:406 w/ -O2...
[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 or an SSA name. 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 if (TREE_CODE (res) == SSA_NAME)
1222 return get_or_alloc_expr_for_name (res);
1223 return e;
1224 }
1225 case REFERENCE:
1226 {
1227 vn_reference_t ref = PRE_EXPR_REFERENCE (e);
1228 tree folded;
1229 if ((folded = fully_constant_vn_reference_p (ref)))
1230 return get_or_alloc_expr_for_constant (folded);
1231 return e;
1232 }
1233 default:
1234 return e;
1235 }
1236 return e;
1237 }
1238
1239 /* Translate the VUSE backwards through phi nodes in PHIBLOCK, so that
1240 it has the value it would have in BLOCK. Set *SAME_VALID to true
1241 in case the new vuse doesn't change the value id of the OPERANDS. */
1242
1243 static tree
1244 translate_vuse_through_block (vec<vn_reference_op_s> operands,
1245 alias_set_type set, tree type, tree vuse,
1246 basic_block phiblock,
1247 basic_block block, bool *same_valid)
1248 {
1249 gimple *phi = SSA_NAME_DEF_STMT (vuse);
1250 ao_ref ref;
1251 edge e = NULL;
1252 bool use_oracle;
1253
1254 *same_valid = true;
1255
1256 if (gimple_bb (phi) != phiblock)
1257 return vuse;
1258
1259 use_oracle = ao_ref_init_from_vn_reference (&ref, set, type, operands);
1260
1261 /* Use the alias-oracle to find either the PHI node in this block,
1262 the first VUSE used in this block that is equivalent to vuse or
1263 the first VUSE which definition in this block kills the value. */
1264 if (gimple_code (phi) == GIMPLE_PHI)
1265 e = find_edge (block, phiblock);
1266 else if (use_oracle)
1267 while (!stmt_may_clobber_ref_p_1 (phi, &ref))
1268 {
1269 vuse = gimple_vuse (phi);
1270 phi = SSA_NAME_DEF_STMT (vuse);
1271 if (gimple_bb (phi) != phiblock)
1272 return vuse;
1273 if (gimple_code (phi) == GIMPLE_PHI)
1274 {
1275 e = find_edge (block, phiblock);
1276 break;
1277 }
1278 }
1279 else
1280 return NULL_TREE;
1281
1282 if (e)
1283 {
1284 if (use_oracle)
1285 {
1286 bitmap visited = NULL;
1287 unsigned int cnt;
1288 /* Try to find a vuse that dominates this phi node by skipping
1289 non-clobbering statements. */
1290 vuse = get_continuation_for_phi (phi, &ref, &cnt, &visited, false,
1291 NULL, NULL);
1292 if (visited)
1293 BITMAP_FREE (visited);
1294 }
1295 else
1296 vuse = NULL_TREE;
1297 if (!vuse)
1298 {
1299 /* If we didn't find any, the value ID can't stay the same,
1300 but return the translated vuse. */
1301 *same_valid = false;
1302 vuse = PHI_ARG_DEF (phi, e->dest_idx);
1303 }
1304 /* ??? We would like to return vuse here as this is the canonical
1305 upmost vdef that this reference is associated with. But during
1306 insertion of the references into the hash tables we only ever
1307 directly insert with their direct gimple_vuse, hence returning
1308 something else would make us not find the other expression. */
1309 return PHI_ARG_DEF (phi, e->dest_idx);
1310 }
1311
1312 return NULL_TREE;
1313 }
1314
1315 /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1316 SET2. This is used to avoid making a set consisting of the union
1317 of PA_IN and ANTIC_IN during insert. */
1318
1319 static inline pre_expr
1320 find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2)
1321 {
1322 pre_expr result;
1323
1324 result = bitmap_find_leader (set1, val);
1325 if (!result && set2)
1326 result = bitmap_find_leader (set2, val);
1327 return result;
1328 }
1329
1330 /* Get the tree type for our PRE expression e. */
1331
1332 static tree
1333 get_expr_type (const pre_expr e)
1334 {
1335 switch (e->kind)
1336 {
1337 case NAME:
1338 return TREE_TYPE (PRE_EXPR_NAME (e));
1339 case CONSTANT:
1340 return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1341 case REFERENCE:
1342 return PRE_EXPR_REFERENCE (e)->type;
1343 case NARY:
1344 return PRE_EXPR_NARY (e)->type;
1345 }
1346 gcc_unreachable ();
1347 }
1348
1349 /* Get a representative SSA_NAME for a given expression.
1350 Since all of our sub-expressions are treated as values, we require
1351 them to be SSA_NAME's for simplicity.
1352 Prior versions of GVNPRE used to use "value handles" here, so that
1353 an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1354 either case, the operands are really values (IE we do not expect
1355 them to be usable without finding leaders). */
1356
1357 static tree
1358 get_representative_for (const pre_expr e)
1359 {
1360 tree name;
1361 unsigned int value_id = get_expr_value_id (e);
1362
1363 switch (e->kind)
1364 {
1365 case NAME:
1366 return VN_INFO (PRE_EXPR_NAME (e))->valnum;
1367 case CONSTANT:
1368 return PRE_EXPR_CONSTANT (e);
1369 case NARY:
1370 case REFERENCE:
1371 {
1372 /* Go through all of the expressions representing this value
1373 and pick out an SSA_NAME. */
1374 unsigned int i;
1375 bitmap_iterator bi;
1376 bitmap exprs = value_expressions[value_id];
1377 EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1378 {
1379 pre_expr rep = expression_for_id (i);
1380 if (rep->kind == NAME)
1381 return VN_INFO (PRE_EXPR_NAME (rep))->valnum;
1382 else if (rep->kind == CONSTANT)
1383 return PRE_EXPR_CONSTANT (rep);
1384 }
1385 }
1386 break;
1387 }
1388
1389 /* If we reached here we couldn't find an SSA_NAME. This can
1390 happen when we've discovered a value that has never appeared in
1391 the program as set to an SSA_NAME, as the result of phi translation.
1392 Create one here.
1393 ??? We should be able to re-use this when we insert the statement
1394 to compute it. */
1395 name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1396 VN_INFO_GET (name)->value_id = value_id;
1397 VN_INFO (name)->valnum = name;
1398 /* ??? For now mark this SSA name for release by SCCVN. */
1399 VN_INFO (name)->needs_insertion = true;
1400 add_to_value (value_id, get_or_alloc_expr_for_name (name));
1401 if (dump_file && (dump_flags & TDF_DETAILS))
1402 {
1403 fprintf (dump_file, "Created SSA_NAME representative ");
1404 print_generic_expr (dump_file, name, 0);
1405 fprintf (dump_file, " for expression:");
1406 print_pre_expr (dump_file, e);
1407 fprintf (dump_file, " (%04d)\n", value_id);
1408 }
1409
1410 return name;
1411 }
1412
1413
1414
1415 static pre_expr
1416 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1417 basic_block pred, basic_block phiblock);
1418
1419 /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1420 the phis in PRED. Return NULL if we can't find a leader for each part
1421 of the translated expression. */
1422
1423 static pre_expr
1424 phi_translate_1 (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1425 basic_block pred, basic_block phiblock)
1426 {
1427 switch (expr->kind)
1428 {
1429 case NARY:
1430 {
1431 unsigned int i;
1432 bool changed = false;
1433 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1434 vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1435 sizeof_vn_nary_op (nary->length));
1436 memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1437
1438 for (i = 0; i < newnary->length; i++)
1439 {
1440 if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1441 continue;
1442 else
1443 {
1444 pre_expr leader, result;
1445 unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1446 leader = find_leader_in_sets (op_val_id, set1, set2);
1447 result = phi_translate (leader, set1, set2, pred, phiblock);
1448 if (result && result != leader)
1449 newnary->op[i] = get_representative_for (result);
1450 else if (!result)
1451 return NULL;
1452
1453 changed |= newnary->op[i] != nary->op[i];
1454 }
1455 }
1456 if (changed)
1457 {
1458 pre_expr constant;
1459 unsigned int new_val_id;
1460
1461 PRE_EXPR_NARY (expr) = newnary;
1462 constant = fully_constant_expression (expr);
1463 PRE_EXPR_NARY (expr) = nary;
1464 if (constant != expr)
1465 {
1466 /* For non-CONSTANTs we have to make sure we can eventually
1467 insert the expression. Which means we need to have a
1468 leader for it. */
1469 if (constant->kind != CONSTANT)
1470 {
1471 unsigned value_id = get_expr_value_id (constant);
1472 constant = find_leader_in_sets (value_id, set1, set2);
1473 if (constant)
1474 return constant;
1475 }
1476 else
1477 return constant;
1478 }
1479
1480 tree result = vn_nary_op_lookup_pieces (newnary->length,
1481 newnary->opcode,
1482 newnary->type,
1483 &newnary->op[0],
1484 &nary);
1485 if (result && is_gimple_min_invariant (result))
1486 return get_or_alloc_expr_for_constant (result);
1487
1488 expr = pre_expr_pool.allocate ();
1489 expr->kind = NARY;
1490 expr->id = 0;
1491 if (nary)
1492 {
1493 PRE_EXPR_NARY (expr) = nary;
1494 new_val_id = nary->value_id;
1495 get_or_alloc_expression_id (expr);
1496 }
1497 else
1498 {
1499 new_val_id = get_next_value_id ();
1500 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
1501 nary = vn_nary_op_insert_pieces (newnary->length,
1502 newnary->opcode,
1503 newnary->type,
1504 &newnary->op[0],
1505 result, new_val_id);
1506 PRE_EXPR_NARY (expr) = nary;
1507 get_or_alloc_expression_id (expr);
1508 }
1509 add_to_value (new_val_id, expr);
1510 }
1511 return expr;
1512 }
1513 break;
1514
1515 case REFERENCE:
1516 {
1517 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1518 vec<vn_reference_op_s> operands = ref->operands;
1519 tree vuse = ref->vuse;
1520 tree newvuse = vuse;
1521 vec<vn_reference_op_s> newoperands = vNULL;
1522 bool changed = false, same_valid = true;
1523 unsigned int i, n;
1524 vn_reference_op_t operand;
1525 vn_reference_t newref;
1526
1527 for (i = 0; operands.iterate (i, &operand); i++)
1528 {
1529 pre_expr opresult;
1530 pre_expr leader;
1531 tree op[3];
1532 tree type = operand->type;
1533 vn_reference_op_s newop = *operand;
1534 op[0] = operand->op0;
1535 op[1] = operand->op1;
1536 op[2] = operand->op2;
1537 for (n = 0; n < 3; ++n)
1538 {
1539 unsigned int op_val_id;
1540 if (!op[n])
1541 continue;
1542 if (TREE_CODE (op[n]) != SSA_NAME)
1543 {
1544 /* We can't possibly insert these. */
1545 if (n != 0
1546 && !is_gimple_min_invariant (op[n]))
1547 break;
1548 continue;
1549 }
1550 op_val_id = VN_INFO (op[n])->value_id;
1551 leader = find_leader_in_sets (op_val_id, set1, set2);
1552 opresult = phi_translate (leader, set1, set2, pred, phiblock);
1553 if (opresult && opresult != leader)
1554 {
1555 tree name = get_representative_for (opresult);
1556 changed |= name != op[n];
1557 op[n] = name;
1558 }
1559 else if (!opresult)
1560 break;
1561 }
1562 if (n != 3)
1563 {
1564 newoperands.release ();
1565 return NULL;
1566 }
1567 if (!changed)
1568 continue;
1569 if (!newoperands.exists ())
1570 newoperands = operands.copy ();
1571 /* We may have changed from an SSA_NAME to a constant */
1572 if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1573 newop.opcode = TREE_CODE (op[0]);
1574 newop.type = type;
1575 newop.op0 = op[0];
1576 newop.op1 = op[1];
1577 newop.op2 = op[2];
1578 newoperands[i] = newop;
1579 }
1580 gcc_checking_assert (i == operands.length ());
1581
1582 if (vuse)
1583 {
1584 newvuse = translate_vuse_through_block (newoperands.exists ()
1585 ? newoperands : operands,
1586 ref->set, ref->type,
1587 vuse, phiblock, pred,
1588 &same_valid);
1589 if (newvuse == NULL_TREE)
1590 {
1591 newoperands.release ();
1592 return NULL;
1593 }
1594 }
1595
1596 if (changed || newvuse != vuse)
1597 {
1598 unsigned int new_val_id;
1599 pre_expr constant;
1600
1601 tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1602 ref->type,
1603 newoperands.exists ()
1604 ? newoperands : operands,
1605 &newref, VN_WALK);
1606 if (result)
1607 newoperands.release ();
1608
1609 /* We can always insert constants, so if we have a partial
1610 redundant constant load of another type try to translate it
1611 to a constant of appropriate type. */
1612 if (result && is_gimple_min_invariant (result))
1613 {
1614 tree tem = result;
1615 if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1616 {
1617 tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1618 if (tem && !is_gimple_min_invariant (tem))
1619 tem = NULL_TREE;
1620 }
1621 if (tem)
1622 return get_or_alloc_expr_for_constant (tem);
1623 }
1624
1625 /* If we'd have to convert things we would need to validate
1626 if we can insert the translated expression. So fail
1627 here for now - we cannot insert an alias with a different
1628 type in the VN tables either, as that would assert. */
1629 if (result
1630 && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1631 return NULL;
1632 else if (!result && newref
1633 && !useless_type_conversion_p (ref->type, newref->type))
1634 {
1635 newoperands.release ();
1636 return NULL;
1637 }
1638
1639 expr = pre_expr_pool.allocate ();
1640 expr->kind = REFERENCE;
1641 expr->id = 0;
1642
1643 if (newref)
1644 {
1645 PRE_EXPR_REFERENCE (expr) = newref;
1646 constant = fully_constant_expression (expr);
1647 if (constant != expr)
1648 return constant;
1649
1650 new_val_id = newref->value_id;
1651 get_or_alloc_expression_id (expr);
1652 }
1653 else
1654 {
1655 if (changed || !same_valid)
1656 {
1657 new_val_id = get_next_value_id ();
1658 value_expressions.safe_grow_cleared
1659 (get_max_value_id () + 1);
1660 }
1661 else
1662 new_val_id = ref->value_id;
1663 if (!newoperands.exists ())
1664 newoperands = operands.copy ();
1665 newref = vn_reference_insert_pieces (newvuse, ref->set,
1666 ref->type,
1667 newoperands,
1668 result, new_val_id);
1669 newoperands = vNULL;
1670 PRE_EXPR_REFERENCE (expr) = newref;
1671 constant = fully_constant_expression (expr);
1672 if (constant != expr)
1673 return constant;
1674 get_or_alloc_expression_id (expr);
1675 }
1676 add_to_value (new_val_id, expr);
1677 }
1678 newoperands.release ();
1679 return expr;
1680 }
1681 break;
1682
1683 case NAME:
1684 {
1685 tree name = PRE_EXPR_NAME (expr);
1686 gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1687 /* If the SSA name is defined by a PHI node in this block,
1688 translate it. */
1689 if (gimple_code (def_stmt) == GIMPLE_PHI
1690 && gimple_bb (def_stmt) == phiblock)
1691 {
1692 edge e = find_edge (pred, gimple_bb (def_stmt));
1693 tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1694
1695 /* Handle constant. */
1696 if (is_gimple_min_invariant (def))
1697 return get_or_alloc_expr_for_constant (def);
1698
1699 return get_or_alloc_expr_for_name (def);
1700 }
1701 /* Otherwise return it unchanged - it will get removed if its
1702 value is not available in PREDs AVAIL_OUT set of expressions
1703 by the subtraction of TMP_GEN. */
1704 return expr;
1705 }
1706
1707 default:
1708 gcc_unreachable ();
1709 }
1710 }
1711
1712 /* Wrapper around phi_translate_1 providing caching functionality. */
1713
1714 static pre_expr
1715 phi_translate (pre_expr expr, bitmap_set_t set1, bitmap_set_t set2,
1716 basic_block pred, basic_block phiblock)
1717 {
1718 expr_pred_trans_t slot = NULL;
1719 pre_expr phitrans;
1720
1721 if (!expr)
1722 return NULL;
1723
1724 /* Constants contain no values that need translation. */
1725 if (expr->kind == CONSTANT)
1726 return expr;
1727
1728 if (value_id_constant_p (get_expr_value_id (expr)))
1729 return expr;
1730
1731 /* Don't add translations of NAMEs as those are cheap to translate. */
1732 if (expr->kind != NAME)
1733 {
1734 if (phi_trans_add (&slot, expr, pred))
1735 return slot->v;
1736 /* Store NULL for the value we want to return in the case of
1737 recursing. */
1738 slot->v = NULL;
1739 }
1740
1741 /* Translate. */
1742 phitrans = phi_translate_1 (expr, set1, set2, pred, phiblock);
1743
1744 if (slot)
1745 {
1746 if (phitrans)
1747 slot->v = phitrans;
1748 else
1749 /* Remove failed translations again, they cause insert
1750 iteration to not pick up new opportunities reliably. */
1751 phi_translate_table->remove_elt_with_hash (slot, slot->hashcode);
1752 }
1753
1754 return phitrans;
1755 }
1756
1757
1758 /* For each expression in SET, translate the values through phi nodes
1759 in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1760 expressions in DEST. */
1761
1762 static void
1763 phi_translate_set (bitmap_set_t dest, bitmap_set_t set, basic_block pred,
1764 basic_block phiblock)
1765 {
1766 vec<pre_expr> exprs;
1767 pre_expr expr;
1768 int i;
1769
1770 if (gimple_seq_empty_p (phi_nodes (phiblock)))
1771 {
1772 bitmap_set_copy (dest, set);
1773 return;
1774 }
1775
1776 exprs = sorted_array_from_bitmap_set (set);
1777 FOR_EACH_VEC_ELT (exprs, i, expr)
1778 {
1779 pre_expr translated;
1780 translated = phi_translate (expr, set, NULL, pred, phiblock);
1781 if (!translated)
1782 continue;
1783
1784 /* We might end up with multiple expressions from SET being
1785 translated to the same value. In this case we do not want
1786 to retain the NARY or REFERENCE expression but prefer a NAME
1787 which would be the leader. */
1788 if (translated->kind == NAME)
1789 bitmap_value_replace_in_set (dest, translated);
1790 else
1791 bitmap_value_insert_into_set (dest, translated);
1792 }
1793 exprs.release ();
1794 }
1795
1796 /* Find the leader for a value (i.e., the name representing that
1797 value) in a given set, and return it. Return NULL if no leader
1798 is found. */
1799
1800 static pre_expr
1801 bitmap_find_leader (bitmap_set_t set, unsigned int val)
1802 {
1803 if (value_id_constant_p (val))
1804 {
1805 unsigned int i;
1806 bitmap_iterator bi;
1807 bitmap exprset = value_expressions[val];
1808
1809 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1810 {
1811 pre_expr expr = expression_for_id (i);
1812 if (expr->kind == CONSTANT)
1813 return expr;
1814 }
1815 }
1816 if (bitmap_set_contains_value (set, val))
1817 {
1818 /* Rather than walk the entire bitmap of expressions, and see
1819 whether any of them has the value we are looking for, we look
1820 at the reverse mapping, which tells us the set of expressions
1821 that have a given value (IE value->expressions with that
1822 value) and see if any of those expressions are in our set.
1823 The number of expressions per value is usually significantly
1824 less than the number of expressions in the set. In fact, for
1825 large testcases, doing it this way is roughly 5-10x faster
1826 than walking the bitmap.
1827 If this is somehow a significant lose for some cases, we can
1828 choose which set to walk based on which set is smaller. */
1829 unsigned int i;
1830 bitmap_iterator bi;
1831 bitmap exprset = value_expressions[val];
1832
1833 EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1834 return expression_for_id (i);
1835 }
1836 return NULL;
1837 }
1838
1839 /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1840 BLOCK by seeing if it is not killed in the block. Note that we are
1841 only determining whether there is a store that kills it. Because
1842 of the order in which clean iterates over values, we are guaranteed
1843 that altered operands will have caused us to be eliminated from the
1844 ANTIC_IN set already. */
1845
1846 static bool
1847 value_dies_in_block_x (pre_expr expr, basic_block block)
1848 {
1849 tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1850 vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1851 gimple *def;
1852 gimple_stmt_iterator gsi;
1853 unsigned id = get_expression_id (expr);
1854 bool res = false;
1855 ao_ref ref;
1856
1857 if (!vuse)
1858 return false;
1859
1860 /* Lookup a previously calculated result. */
1861 if (EXPR_DIES (block)
1862 && bitmap_bit_p (EXPR_DIES (block), id * 2))
1863 return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1864
1865 /* A memory expression {e, VUSE} dies in the block if there is a
1866 statement that may clobber e. If, starting statement walk from the
1867 top of the basic block, a statement uses VUSE there can be no kill
1868 inbetween that use and the original statement that loaded {e, VUSE},
1869 so we can stop walking. */
1870 ref.base = NULL_TREE;
1871 for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1872 {
1873 tree def_vuse, def_vdef;
1874 def = gsi_stmt (gsi);
1875 def_vuse = gimple_vuse (def);
1876 def_vdef = gimple_vdef (def);
1877
1878 /* Not a memory statement. */
1879 if (!def_vuse)
1880 continue;
1881
1882 /* Not a may-def. */
1883 if (!def_vdef)
1884 {
1885 /* A load with the same VUSE, we're done. */
1886 if (def_vuse == vuse)
1887 break;
1888
1889 continue;
1890 }
1891
1892 /* Init ref only if we really need it. */
1893 if (ref.base == NULL_TREE
1894 && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->type,
1895 refx->operands))
1896 {
1897 res = true;
1898 break;
1899 }
1900 /* If the statement may clobber expr, it dies. */
1901 if (stmt_may_clobber_ref_p_1 (def, &ref))
1902 {
1903 res = true;
1904 break;
1905 }
1906 }
1907
1908 /* Remember the result. */
1909 if (!EXPR_DIES (block))
1910 EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1911 bitmap_set_bit (EXPR_DIES (block), id * 2);
1912 if (res)
1913 bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1914
1915 return res;
1916 }
1917
1918
1919 /* Determine if OP is valid in SET1 U SET2, which it is when the union
1920 contains its value-id. */
1921
1922 static bool
1923 op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1924 {
1925 if (op && TREE_CODE (op) == SSA_NAME)
1926 {
1927 unsigned int value_id = VN_INFO (op)->value_id;
1928 if (!(bitmap_set_contains_value (set1, value_id)
1929 || (set2 && bitmap_set_contains_value (set2, value_id))))
1930 return false;
1931 }
1932 return true;
1933 }
1934
1935 /* Determine if the expression EXPR is valid in SET1 U SET2.
1936 ONLY SET2 CAN BE NULL.
1937 This means that we have a leader for each part of the expression
1938 (if it consists of values), or the expression is an SSA_NAME.
1939 For loads/calls, we also see if the vuse is killed in this block. */
1940
1941 static bool
1942 valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
1943 {
1944 switch (expr->kind)
1945 {
1946 case NAME:
1947 /* By construction all NAMEs are available. Non-available
1948 NAMEs are removed by subtracting TMP_GEN from the sets. */
1949 return true;
1950 case NARY:
1951 {
1952 unsigned int i;
1953 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1954 for (i = 0; i < nary->length; i++)
1955 if (!op_valid_in_sets (set1, set2, nary->op[i]))
1956 return false;
1957 return true;
1958 }
1959 break;
1960 case REFERENCE:
1961 {
1962 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1963 vn_reference_op_t vro;
1964 unsigned int i;
1965
1966 FOR_EACH_VEC_ELT (ref->operands, i, vro)
1967 {
1968 if (!op_valid_in_sets (set1, set2, vro->op0)
1969 || !op_valid_in_sets (set1, set2, vro->op1)
1970 || !op_valid_in_sets (set1, set2, vro->op2))
1971 return false;
1972 }
1973 return true;
1974 }
1975 default:
1976 gcc_unreachable ();
1977 }
1978 }
1979
1980 /* Clean the set of expressions that are no longer valid in SET1 or
1981 SET2. This means expressions that are made up of values we have no
1982 leaders for in SET1 or SET2. This version is used for partial
1983 anticipation, which means it is not valid in either ANTIC_IN or
1984 PA_IN. */
1985
1986 static void
1987 dependent_clean (bitmap_set_t set1, bitmap_set_t set2)
1988 {
1989 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
1990 pre_expr expr;
1991 int i;
1992
1993 FOR_EACH_VEC_ELT (exprs, i, expr)
1994 {
1995 if (!valid_in_sets (set1, set2, expr))
1996 bitmap_remove_from_set (set1, expr);
1997 }
1998 exprs.release ();
1999 }
2000
2001 /* Clean the set of expressions that are no longer valid in SET. This
2002 means expressions that are made up of values we have no leaders for
2003 in SET. */
2004
2005 static void
2006 clean (bitmap_set_t set)
2007 {
2008 vec<pre_expr> exprs = sorted_array_from_bitmap_set (set);
2009 pre_expr expr;
2010 int i;
2011
2012 FOR_EACH_VEC_ELT (exprs, i, expr)
2013 {
2014 if (!valid_in_sets (set, NULL, expr))
2015 bitmap_remove_from_set (set, expr);
2016 }
2017 exprs.release ();
2018 }
2019
2020 /* Clean the set of expressions that are no longer valid in SET because
2021 they are clobbered in BLOCK or because they trap and may not be executed. */
2022
2023 static void
2024 prune_clobbered_mems (bitmap_set_t set, basic_block block)
2025 {
2026 bitmap_iterator bi;
2027 unsigned i;
2028
2029 FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2030 {
2031 pre_expr expr = expression_for_id (i);
2032 if (expr->kind == REFERENCE)
2033 {
2034 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2035 if (ref->vuse)
2036 {
2037 gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2038 if (!gimple_nop_p (def_stmt)
2039 && ((gimple_bb (def_stmt) != block
2040 && !dominated_by_p (CDI_DOMINATORS,
2041 block, gimple_bb (def_stmt)))
2042 || (gimple_bb (def_stmt) == block
2043 && value_dies_in_block_x (expr, block))))
2044 bitmap_remove_from_set (set, expr);
2045 }
2046 }
2047 else if (expr->kind == NARY)
2048 {
2049 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2050 /* If the NARY may trap make sure the block does not contain
2051 a possible exit point.
2052 ??? This is overly conservative if we translate AVAIL_OUT
2053 as the available expression might be after the exit point. */
2054 if (BB_MAY_NOTRETURN (block)
2055 && vn_nary_may_trap (nary))
2056 bitmap_remove_from_set (set, expr);
2057 }
2058 }
2059 }
2060
2061 static sbitmap has_abnormal_preds;
2062
2063 /* Compute the ANTIC set for BLOCK.
2064
2065 If succs(BLOCK) > 1 then
2066 ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2067 else if succs(BLOCK) == 1 then
2068 ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2069
2070 ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2071 */
2072
2073 static bool
2074 compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2075 {
2076 bool changed = false;
2077 bitmap_set_t S, old, ANTIC_OUT;
2078 bitmap_iterator bi;
2079 unsigned int bii;
2080 edge e;
2081 edge_iterator ei;
2082 bool was_visited = BB_VISITED (block);
2083
2084 old = ANTIC_OUT = S = NULL;
2085 BB_VISITED (block) = 1;
2086
2087 /* If any edges from predecessors are abnormal, antic_in is empty,
2088 so do nothing. */
2089 if (block_has_abnormal_pred_edge)
2090 goto maybe_dump_sets;
2091
2092 old = ANTIC_IN (block);
2093 ANTIC_OUT = bitmap_set_new ();
2094
2095 /* If the block has no successors, ANTIC_OUT is empty. */
2096 if (EDGE_COUNT (block->succs) == 0)
2097 ;
2098 /* If we have one successor, we could have some phi nodes to
2099 translate through. */
2100 else if (single_succ_p (block))
2101 {
2102 basic_block succ_bb = single_succ (block);
2103 gcc_assert (BB_VISITED (succ_bb));
2104 phi_translate_set (ANTIC_OUT, ANTIC_IN (succ_bb), block, succ_bb);
2105 }
2106 /* If we have multiple successors, we take the intersection of all of
2107 them. Note that in the case of loop exit phi nodes, we may have
2108 phis to translate through. */
2109 else
2110 {
2111 size_t i;
2112 basic_block bprime, first = NULL;
2113
2114 auto_vec<basic_block> worklist (EDGE_COUNT (block->succs));
2115 FOR_EACH_EDGE (e, ei, block->succs)
2116 {
2117 if (!first
2118 && BB_VISITED (e->dest))
2119 first = e->dest;
2120 else if (BB_VISITED (e->dest))
2121 worklist.quick_push (e->dest);
2122 else
2123 {
2124 /* Unvisited successors get their ANTIC_IN replaced by the
2125 maximal set to arrive at a maximum ANTIC_IN solution.
2126 We can ignore them in the intersection operation and thus
2127 need not explicitely represent that maximum solution. */
2128 if (dump_file && (dump_flags & TDF_DETAILS))
2129 fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n",
2130 e->src->index, e->dest->index);
2131 }
2132 }
2133
2134 /* Of multiple successors we have to have visited one already
2135 which is guaranteed by iteration order. */
2136 gcc_assert (first != NULL);
2137
2138 phi_translate_set (ANTIC_OUT, ANTIC_IN (first), block, first);
2139
2140 FOR_EACH_VEC_ELT (worklist, i, bprime)
2141 {
2142 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2143 {
2144 bitmap_set_t tmp = bitmap_set_new ();
2145 phi_translate_set (tmp, ANTIC_IN (bprime), block, bprime);
2146 bitmap_set_and (ANTIC_OUT, tmp);
2147 bitmap_set_free (tmp);
2148 }
2149 else
2150 bitmap_set_and (ANTIC_OUT, ANTIC_IN (bprime));
2151 }
2152 }
2153
2154 /* Prune expressions that are clobbered in block and thus become
2155 invalid if translated from ANTIC_OUT to ANTIC_IN. */
2156 prune_clobbered_mems (ANTIC_OUT, block);
2157
2158 /* Generate ANTIC_OUT - TMP_GEN. */
2159 S = bitmap_set_subtract (ANTIC_OUT, TMP_GEN (block));
2160
2161 /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2162 ANTIC_IN (block) = bitmap_set_subtract (EXP_GEN (block),
2163 TMP_GEN (block));
2164
2165 /* Then union in the ANTIC_OUT - TMP_GEN values,
2166 to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2167 FOR_EACH_EXPR_ID_IN_SET (S, bii, bi)
2168 bitmap_value_insert_into_set (ANTIC_IN (block),
2169 expression_for_id (bii));
2170
2171 clean (ANTIC_IN (block));
2172
2173 if (!was_visited || !bitmap_set_equal (old, ANTIC_IN (block)))
2174 changed = true;
2175
2176 maybe_dump_sets:
2177 if (dump_file && (dump_flags & TDF_DETAILS))
2178 {
2179 if (ANTIC_OUT)
2180 print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2181
2182 if (changed)
2183 fprintf (dump_file, "[changed] ");
2184 print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2185 block->index);
2186
2187 if (S)
2188 print_bitmap_set (dump_file, S, "S", block->index);
2189 }
2190 if (old)
2191 bitmap_set_free (old);
2192 if (S)
2193 bitmap_set_free (S);
2194 if (ANTIC_OUT)
2195 bitmap_set_free (ANTIC_OUT);
2196 return changed;
2197 }
2198
2199 /* Compute PARTIAL_ANTIC for BLOCK.
2200
2201 If succs(BLOCK) > 1 then
2202 PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2203 in ANTIC_OUT for all succ(BLOCK)
2204 else if succs(BLOCK) == 1 then
2205 PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2206
2207 PA_IN[BLOCK] = dependent_clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK]
2208 - ANTIC_IN[BLOCK])
2209
2210 */
2211 static void
2212 compute_partial_antic_aux (basic_block block,
2213 bool block_has_abnormal_pred_edge)
2214 {
2215 bitmap_set_t old_PA_IN;
2216 bitmap_set_t PA_OUT;
2217 edge e;
2218 edge_iterator ei;
2219 unsigned long max_pa = PARAM_VALUE (PARAM_MAX_PARTIAL_ANTIC_LENGTH);
2220
2221 old_PA_IN = PA_OUT = NULL;
2222
2223 /* If any edges from predecessors are abnormal, antic_in is empty,
2224 so do nothing. */
2225 if (block_has_abnormal_pred_edge)
2226 goto maybe_dump_sets;
2227
2228 /* If there are too many partially anticipatable values in the
2229 block, phi_translate_set can take an exponential time: stop
2230 before the translation starts. */
2231 if (max_pa
2232 && single_succ_p (block)
2233 && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2234 goto maybe_dump_sets;
2235
2236 old_PA_IN = PA_IN (block);
2237 PA_OUT = bitmap_set_new ();
2238
2239 /* If the block has no successors, ANTIC_OUT is empty. */
2240 if (EDGE_COUNT (block->succs) == 0)
2241 ;
2242 /* If we have one successor, we could have some phi nodes to
2243 translate through. Note that we can't phi translate across DFS
2244 back edges in partial antic, because it uses a union operation on
2245 the successors. For recurrences like IV's, we will end up
2246 generating a new value in the set on each go around (i + 3 (VH.1)
2247 VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2248 else if (single_succ_p (block))
2249 {
2250 basic_block succ = single_succ (block);
2251 if (!(single_succ_edge (block)->flags & EDGE_DFS_BACK))
2252 phi_translate_set (PA_OUT, PA_IN (succ), block, succ);
2253 }
2254 /* If we have multiple successors, we take the union of all of
2255 them. */
2256 else
2257 {
2258 size_t i;
2259 basic_block bprime;
2260
2261 auto_vec<basic_block> worklist (EDGE_COUNT (block->succs));
2262 FOR_EACH_EDGE (e, ei, block->succs)
2263 {
2264 if (e->flags & EDGE_DFS_BACK)
2265 continue;
2266 worklist.quick_push (e->dest);
2267 }
2268 if (worklist.length () > 0)
2269 {
2270 FOR_EACH_VEC_ELT (worklist, i, bprime)
2271 {
2272 unsigned int i;
2273 bitmap_iterator bi;
2274
2275 FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (bprime), i, bi)
2276 bitmap_value_insert_into_set (PA_OUT,
2277 expression_for_id (i));
2278 if (!gimple_seq_empty_p (phi_nodes (bprime)))
2279 {
2280 bitmap_set_t pa_in = bitmap_set_new ();
2281 phi_translate_set (pa_in, PA_IN (bprime), block, bprime);
2282 FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2283 bitmap_value_insert_into_set (PA_OUT,
2284 expression_for_id (i));
2285 bitmap_set_free (pa_in);
2286 }
2287 else
2288 FOR_EACH_EXPR_ID_IN_SET (PA_IN (bprime), i, bi)
2289 bitmap_value_insert_into_set (PA_OUT,
2290 expression_for_id (i));
2291 }
2292 }
2293 }
2294
2295 /* Prune expressions that are clobbered in block and thus become
2296 invalid if translated from PA_OUT to PA_IN. */
2297 prune_clobbered_mems (PA_OUT, block);
2298
2299 /* PA_IN starts with PA_OUT - TMP_GEN.
2300 Then we subtract things from ANTIC_IN. */
2301 PA_IN (block) = bitmap_set_subtract (PA_OUT, TMP_GEN (block));
2302
2303 /* For partial antic, we want to put back in the phi results, since
2304 we will properly avoid making them partially antic over backedges. */
2305 bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2306 bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2307
2308 /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2309 bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2310
2311 dependent_clean (PA_IN (block), ANTIC_IN (block));
2312
2313 maybe_dump_sets:
2314 if (dump_file && (dump_flags & TDF_DETAILS))
2315 {
2316 if (PA_OUT)
2317 print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2318
2319 print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2320 }
2321 if (old_PA_IN)
2322 bitmap_set_free (old_PA_IN);
2323 if (PA_OUT)
2324 bitmap_set_free (PA_OUT);
2325 }
2326
2327 /* Compute ANTIC and partial ANTIC sets. */
2328
2329 static void
2330 compute_antic (void)
2331 {
2332 bool changed = true;
2333 int num_iterations = 0;
2334 basic_block block;
2335 int i;
2336 edge_iterator ei;
2337 edge e;
2338
2339 /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2340 We pre-build the map of blocks with incoming abnormal edges here. */
2341 has_abnormal_preds = sbitmap_alloc (last_basic_block_for_fn (cfun));
2342 bitmap_clear (has_abnormal_preds);
2343
2344 FOR_ALL_BB_FN (block, cfun)
2345 {
2346 BB_VISITED (block) = 0;
2347
2348 FOR_EACH_EDGE (e, ei, block->preds)
2349 if (e->flags & EDGE_ABNORMAL)
2350 {
2351 bitmap_set_bit (has_abnormal_preds, block->index);
2352
2353 /* We also anticipate nothing. */
2354 BB_VISITED (block) = 1;
2355 break;
2356 }
2357
2358 /* While we are here, give empty ANTIC_IN sets to each block. */
2359 ANTIC_IN (block) = bitmap_set_new ();
2360 if (do_partial_partial)
2361 PA_IN (block) = bitmap_set_new ();
2362 }
2363
2364 /* At the exit block we anticipate nothing. */
2365 BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2366
2367 /* For ANTIC computation we need a postorder that also guarantees that
2368 a block with a single successor is visited after its successor.
2369 RPO on the inverted CFG has this property. */
2370 int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2371 int postorder_num = inverted_post_order_compute (postorder);
2372
2373 auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
2374 bitmap_ones (worklist);
2375 while (changed)
2376 {
2377 if (dump_file && (dump_flags & TDF_DETAILS))
2378 fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2379 /* ??? We need to clear our PHI translation cache here as the
2380 ANTIC sets shrink and we restrict valid translations to
2381 those having operands with leaders in ANTIC. Same below
2382 for PA ANTIC computation. */
2383 num_iterations++;
2384 changed = false;
2385 for (i = postorder_num - 1; i >= 0; i--)
2386 {
2387 if (bitmap_bit_p (worklist, postorder[i]))
2388 {
2389 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2390 bitmap_clear_bit (worklist, block->index);
2391 if (compute_antic_aux (block,
2392 bitmap_bit_p (has_abnormal_preds,
2393 block->index)))
2394 {
2395 FOR_EACH_EDGE (e, ei, block->preds)
2396 bitmap_set_bit (worklist, e->src->index);
2397 changed = true;
2398 }
2399 }
2400 }
2401 /* Theoretically possible, but *highly* unlikely. */
2402 gcc_checking_assert (num_iterations < 500);
2403 }
2404
2405 statistics_histogram_event (cfun, "compute_antic iterations",
2406 num_iterations);
2407
2408 if (do_partial_partial)
2409 {
2410 /* For partial antic we ignore backedges and thus we do not need
2411 to perform any iteration when we process blocks in postorder. */
2412 postorder_num = pre_and_rev_post_order_compute (NULL, postorder, false);
2413 for (i = postorder_num - 1 ; i >= 0; i--)
2414 {
2415 basic_block block = BASIC_BLOCK_FOR_FN (cfun, postorder[i]);
2416 compute_partial_antic_aux (block,
2417 bitmap_bit_p (has_abnormal_preds,
2418 block->index));
2419 }
2420 }
2421
2422 sbitmap_free (has_abnormal_preds);
2423 free (postorder);
2424 }
2425
2426
2427 /* Inserted expressions are placed onto this worklist, which is used
2428 for performing quick dead code elimination of insertions we made
2429 that didn't turn out to be necessary. */
2430 static bitmap inserted_exprs;
2431
2432 /* The actual worker for create_component_ref_by_pieces. */
2433
2434 static tree
2435 create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2436 unsigned int *operand, gimple_seq *stmts)
2437 {
2438 vn_reference_op_t currop = &ref->operands[*operand];
2439 tree genop;
2440 ++*operand;
2441 switch (currop->opcode)
2442 {
2443 case CALL_EXPR:
2444 gcc_unreachable ();
2445
2446 case MEM_REF:
2447 {
2448 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2449 stmts);
2450 if (!baseop)
2451 return NULL_TREE;
2452 tree offset = currop->op0;
2453 if (TREE_CODE (baseop) == ADDR_EXPR
2454 && handled_component_p (TREE_OPERAND (baseop, 0)))
2455 {
2456 HOST_WIDE_INT off;
2457 tree base;
2458 base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2459 &off);
2460 gcc_assert (base);
2461 offset = int_const_binop (PLUS_EXPR, offset,
2462 build_int_cst (TREE_TYPE (offset),
2463 off));
2464 baseop = build_fold_addr_expr (base);
2465 }
2466 genop = build2 (MEM_REF, currop->type, baseop, offset);
2467 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2468 MR_DEPENDENCE_BASE (genop) = currop->base;
2469 REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
2470 return genop;
2471 }
2472
2473 case TARGET_MEM_REF:
2474 {
2475 tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2476 vn_reference_op_t nextop = &ref->operands[++*operand];
2477 tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2478 stmts);
2479 if (!baseop)
2480 return NULL_TREE;
2481 if (currop->op0)
2482 {
2483 genop0 = find_or_generate_expression (block, currop->op0, stmts);
2484 if (!genop0)
2485 return NULL_TREE;
2486 }
2487 if (nextop->op0)
2488 {
2489 genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2490 if (!genop1)
2491 return NULL_TREE;
2492 }
2493 genop = build5 (TARGET_MEM_REF, currop->type,
2494 baseop, currop->op2, genop0, currop->op1, genop1);
2495
2496 MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2497 MR_DEPENDENCE_BASE (genop) = currop->base;
2498 return genop;
2499 }
2500
2501 case ADDR_EXPR:
2502 if (currop->op0)
2503 {
2504 gcc_assert (is_gimple_min_invariant (currop->op0));
2505 return currop->op0;
2506 }
2507 /* Fallthrough. */
2508 case REALPART_EXPR:
2509 case IMAGPART_EXPR:
2510 case VIEW_CONVERT_EXPR:
2511 {
2512 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2513 stmts);
2514 if (!genop0)
2515 return NULL_TREE;
2516 return fold_build1 (currop->opcode, currop->type, genop0);
2517 }
2518
2519 case WITH_SIZE_EXPR:
2520 {
2521 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2522 stmts);
2523 if (!genop0)
2524 return NULL_TREE;
2525 tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2526 if (!genop1)
2527 return NULL_TREE;
2528 return fold_build2 (currop->opcode, currop->type, genop0, genop1);
2529 }
2530
2531 case BIT_FIELD_REF:
2532 {
2533 tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2534 stmts);
2535 if (!genop0)
2536 return NULL_TREE;
2537 tree op1 = currop->op0;
2538 tree op2 = currop->op1;
2539 tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2540 REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
2541 return fold (t);
2542 }
2543
2544 /* For array ref vn_reference_op's, operand 1 of the array ref
2545 is op0 of the reference op and operand 3 of the array ref is
2546 op1. */
2547 case ARRAY_RANGE_REF:
2548 case ARRAY_REF:
2549 {
2550 tree genop0;
2551 tree genop1 = currop->op0;
2552 tree genop2 = currop->op1;
2553 tree genop3 = currop->op2;
2554 genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2555 stmts);
2556 if (!genop0)
2557 return NULL_TREE;
2558 genop1 = find_or_generate_expression (block, genop1, stmts);
2559 if (!genop1)
2560 return NULL_TREE;
2561 if (genop2)
2562 {
2563 tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2564 /* Drop zero minimum index if redundant. */
2565 if (integer_zerop (genop2)
2566 && (!domain_type
2567 || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2568 genop2 = NULL_TREE;
2569 else
2570 {
2571 genop2 = find_or_generate_expression (block, genop2, stmts);
2572 if (!genop2)
2573 return NULL_TREE;
2574 }
2575 }
2576 if (genop3)
2577 {
2578 tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2579 /* We can't always put a size in units of the element alignment
2580 here as the element alignment may be not visible. See
2581 PR43783. Simply drop the element size for constant
2582 sizes. */
2583 if (TREE_CODE (genop3) == INTEGER_CST
2584 && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
2585 && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
2586 (wi::to_offset (genop3)
2587 * vn_ref_op_align_unit (currop))))
2588 genop3 = NULL_TREE;
2589 else
2590 {
2591 genop3 = find_or_generate_expression (block, genop3, stmts);
2592 if (!genop3)
2593 return NULL_TREE;
2594 }
2595 }
2596 return build4 (currop->opcode, currop->type, genop0, genop1,
2597 genop2, genop3);
2598 }
2599 case COMPONENT_REF:
2600 {
2601 tree op0;
2602 tree op1;
2603 tree genop2 = currop->op1;
2604 op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2605 if (!op0)
2606 return NULL_TREE;
2607 /* op1 should be a FIELD_DECL, which are represented by themselves. */
2608 op1 = currop->op0;
2609 if (genop2)
2610 {
2611 genop2 = find_or_generate_expression (block, genop2, stmts);
2612 if (!genop2)
2613 return NULL_TREE;
2614 }
2615 return fold_build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2616 }
2617
2618 case SSA_NAME:
2619 {
2620 genop = find_or_generate_expression (block, currop->op0, stmts);
2621 return genop;
2622 }
2623 case STRING_CST:
2624 case INTEGER_CST:
2625 case COMPLEX_CST:
2626 case VECTOR_CST:
2627 case REAL_CST:
2628 case CONSTRUCTOR:
2629 case VAR_DECL:
2630 case PARM_DECL:
2631 case CONST_DECL:
2632 case RESULT_DECL:
2633 case FUNCTION_DECL:
2634 return currop->op0;
2635
2636 default:
2637 gcc_unreachable ();
2638 }
2639 }
2640
2641 /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2642 COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2643 trying to rename aggregates into ssa form directly, which is a no no.
2644
2645 Thus, this routine doesn't create temporaries, it just builds a
2646 single access expression for the array, calling
2647 find_or_generate_expression to build the innermost pieces.
2648
2649 This function is a subroutine of create_expression_by_pieces, and
2650 should not be called on it's own unless you really know what you
2651 are doing. */
2652
2653 static tree
2654 create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2655 gimple_seq *stmts)
2656 {
2657 unsigned int op = 0;
2658 return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2659 }
2660
2661 /* Find a simple leader for an expression, or generate one using
2662 create_expression_by_pieces from a NARY expression for the value.
2663 BLOCK is the basic_block we are looking for leaders in.
2664 OP is the tree expression to find a leader for or generate.
2665 Returns the leader or NULL_TREE on failure. */
2666
2667 static tree
2668 find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2669 {
2670 pre_expr expr = get_or_alloc_expr_for (op);
2671 unsigned int lookfor = get_expr_value_id (expr);
2672 pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2673 if (leader)
2674 {
2675 if (leader->kind == NAME)
2676 return PRE_EXPR_NAME (leader);
2677 else if (leader->kind == CONSTANT)
2678 return PRE_EXPR_CONSTANT (leader);
2679
2680 /* Defer. */
2681 return NULL_TREE;
2682 }
2683
2684 /* It must be a complex expression, so generate it recursively. Note
2685 that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2686 where the insert algorithm fails to insert a required expression. */
2687 bitmap exprset = value_expressions[lookfor];
2688 bitmap_iterator bi;
2689 unsigned int i;
2690 EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2691 {
2692 pre_expr temp = expression_for_id (i);
2693 /* We cannot insert random REFERENCE expressions at arbitrary
2694 places. We can insert NARYs which eventually re-materializes
2695 its operand values. */
2696 if (temp->kind == NARY)
2697 return create_expression_by_pieces (block, temp, stmts,
2698 get_expr_type (expr));
2699 }
2700
2701 /* Defer. */
2702 return NULL_TREE;
2703 }
2704
2705 #define NECESSARY GF_PLF_1
2706
2707 /* Create an expression in pieces, so that we can handle very complex
2708 expressions that may be ANTIC, but not necessary GIMPLE.
2709 BLOCK is the basic block the expression will be inserted into,
2710 EXPR is the expression to insert (in value form)
2711 STMTS is a statement list to append the necessary insertions into.
2712
2713 This function will die if we hit some value that shouldn't be
2714 ANTIC but is (IE there is no leader for it, or its components).
2715 The function returns NULL_TREE in case a different antic expression
2716 has to be inserted first.
2717 This function may also generate expressions that are themselves
2718 partially or fully redundant. Those that are will be either made
2719 fully redundant during the next iteration of insert (for partially
2720 redundant ones), or eliminated by eliminate (for fully redundant
2721 ones). */
2722
2723 static tree
2724 create_expression_by_pieces (basic_block block, pre_expr expr,
2725 gimple_seq *stmts, tree type)
2726 {
2727 tree name;
2728 tree folded;
2729 gimple_seq forced_stmts = NULL;
2730 unsigned int value_id;
2731 gimple_stmt_iterator gsi;
2732 tree exprtype = type ? type : get_expr_type (expr);
2733 pre_expr nameexpr;
2734 gassign *newstmt;
2735
2736 switch (expr->kind)
2737 {
2738 /* We may hit the NAME/CONSTANT case if we have to convert types
2739 that value numbering saw through. */
2740 case NAME:
2741 folded = PRE_EXPR_NAME (expr);
2742 if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2743 return folded;
2744 break;
2745 case CONSTANT:
2746 {
2747 folded = PRE_EXPR_CONSTANT (expr);
2748 tree tem = fold_convert (exprtype, folded);
2749 if (is_gimple_min_invariant (tem))
2750 return tem;
2751 break;
2752 }
2753 case REFERENCE:
2754 if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
2755 {
2756 vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2757 unsigned int operand = 1;
2758 vn_reference_op_t currop = &ref->operands[0];
2759 tree sc = NULL_TREE;
2760 tree fn;
2761 if (TREE_CODE (currop->op0) == FUNCTION_DECL)
2762 fn = currop->op0;
2763 else
2764 fn = find_or_generate_expression (block, currop->op0, stmts);
2765 if (!fn)
2766 return NULL_TREE;
2767 if (currop->op1)
2768 {
2769 sc = find_or_generate_expression (block, currop->op1, stmts);
2770 if (!sc)
2771 return NULL_TREE;
2772 }
2773 auto_vec<tree> args (ref->operands.length () - 1);
2774 while (operand < ref->operands.length ())
2775 {
2776 tree arg = create_component_ref_by_pieces_1 (block, ref,
2777 &operand, stmts);
2778 if (!arg)
2779 return NULL_TREE;
2780 args.quick_push (arg);
2781 }
2782 gcall *call
2783 = gimple_build_call_vec ((TREE_CODE (fn) == FUNCTION_DECL
2784 ? build_fold_addr_expr (fn) : fn), args);
2785 gimple_call_set_with_bounds (call, currop->with_bounds);
2786 if (sc)
2787 gimple_call_set_chain (call, sc);
2788 tree forcedname = make_ssa_name (currop->type);
2789 gimple_call_set_lhs (call, forcedname);
2790 gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
2791 gimple_seq_add_stmt_without_update (&forced_stmts, call);
2792 folded = forcedname;
2793 }
2794 else
2795 {
2796 folded = create_component_ref_by_pieces (block,
2797 PRE_EXPR_REFERENCE (expr),
2798 stmts);
2799 if (!folded)
2800 return NULL_TREE;
2801 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2802 newstmt = gimple_build_assign (name, folded);
2803 gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2804 gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
2805 folded = name;
2806 }
2807 break;
2808 case NARY:
2809 {
2810 vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2811 tree *genop = XALLOCAVEC (tree, nary->length);
2812 unsigned i;
2813 for (i = 0; i < nary->length; ++i)
2814 {
2815 genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2816 if (!genop[i])
2817 return NULL_TREE;
2818 /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2819 may have conversions stripped. */
2820 if (nary->opcode == POINTER_PLUS_EXPR)
2821 {
2822 if (i == 0)
2823 genop[i] = gimple_convert (&forced_stmts,
2824 nary->type, genop[i]);
2825 else if (i == 1)
2826 genop[i] = gimple_convert (&forced_stmts,
2827 sizetype, genop[i]);
2828 }
2829 else
2830 genop[i] = gimple_convert (&forced_stmts,
2831 TREE_TYPE (nary->op[i]), genop[i]);
2832 }
2833 if (nary->opcode == CONSTRUCTOR)
2834 {
2835 vec<constructor_elt, va_gc> *elts = NULL;
2836 for (i = 0; i < nary->length; ++i)
2837 CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2838 folded = build_constructor (nary->type, elts);
2839 name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2840 newstmt = gimple_build_assign (name, folded);
2841 gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2842 folded = name;
2843 }
2844 else
2845 {
2846 switch (nary->length)
2847 {
2848 case 1:
2849 folded = gimple_build (&forced_stmts, nary->opcode, nary->type,
2850 genop[0]);
2851 break;
2852 case 2:
2853 folded = gimple_build (&forced_stmts, nary->opcode, nary->type,
2854 genop[0], genop[1]);
2855 break;
2856 case 3:
2857 folded = gimple_build (&forced_stmts, nary->opcode, nary->type,
2858 genop[0], genop[1], genop[2]);
2859 break;
2860 default:
2861 gcc_unreachable ();
2862 }
2863 }
2864 }
2865 break;
2866 default:
2867 gcc_unreachable ();
2868 }
2869
2870 folded = gimple_convert (&forced_stmts, exprtype, folded);
2871
2872 /* If there is nothing to insert, return the simplified result. */
2873 if (gimple_seq_empty_p (forced_stmts))
2874 return folded;
2875 /* If we simplified to a constant return it and discard eventually
2876 built stmts. */
2877 if (is_gimple_min_invariant (folded))
2878 {
2879 gimple_seq_discard (forced_stmts);
2880 return folded;
2881 }
2882 /* Likewise if we simplified to sth not queued for insertion. */
2883 bool found = false;
2884 gsi = gsi_start (forced_stmts);
2885 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2886 {
2887 gimple *stmt = gsi_stmt (gsi);
2888 tree forcedname = gimple_get_lhs (stmt);
2889 if (forcedname == folded)
2890 found = true;
2891 }
2892 if (! found)
2893 {
2894 gimple_seq_discard (forced_stmts);
2895 return folded;
2896 }
2897 gcc_assert (TREE_CODE (folded) == SSA_NAME);
2898
2899 /* If we have any intermediate expressions to the value sets, add them
2900 to the value sets and chain them in the instruction stream. */
2901 if (forced_stmts)
2902 {
2903 gsi = gsi_start (forced_stmts);
2904 for (; !gsi_end_p (gsi); gsi_next (&gsi))
2905 {
2906 gimple *stmt = gsi_stmt (gsi);
2907 tree forcedname = gimple_get_lhs (stmt);
2908 pre_expr nameexpr;
2909
2910 if (forcedname != folded)
2911 {
2912 VN_INFO_GET (forcedname)->valnum = forcedname;
2913 VN_INFO (forcedname)->value_id = get_next_value_id ();
2914 nameexpr = get_or_alloc_expr_for_name (forcedname);
2915 add_to_value (VN_INFO (forcedname)->value_id, nameexpr);
2916 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2917 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2918 }
2919
2920 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
2921 gimple_set_plf (stmt, NECESSARY, false);
2922 }
2923 gimple_seq_add_seq (stmts, forced_stmts);
2924 }
2925
2926 name = folded;
2927
2928 /* Fold the last statement. */
2929 gsi = gsi_last (*stmts);
2930 if (fold_stmt_inplace (&gsi))
2931 update_stmt (gsi_stmt (gsi));
2932
2933 /* Add a value number to the temporary.
2934 The value may already exist in either NEW_SETS, or AVAIL_OUT, because
2935 we are creating the expression by pieces, and this particular piece of
2936 the expression may have been represented. There is no harm in replacing
2937 here. */
2938 value_id = get_expr_value_id (expr);
2939 VN_INFO_GET (name)->value_id = value_id;
2940 VN_INFO (name)->valnum = sccvn_valnum_from_value_id (value_id);
2941 if (VN_INFO (name)->valnum == NULL_TREE)
2942 VN_INFO (name)->valnum = name;
2943 gcc_assert (VN_INFO (name)->valnum != NULL_TREE);
2944 nameexpr = get_or_alloc_expr_for_name (name);
2945 add_to_value (value_id, nameexpr);
2946 if (NEW_SETS (block))
2947 bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
2948 bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
2949
2950 pre_stats.insertions++;
2951 if (dump_file && (dump_flags & TDF_DETAILS))
2952 {
2953 fprintf (dump_file, "Inserted ");
2954 print_gimple_stmt (dump_file, gsi_stmt (gsi_last (*stmts)), 0, 0);
2955 fprintf (dump_file, " in predecessor %d (%04d)\n",
2956 block->index, value_id);
2957 }
2958
2959 return name;
2960 }
2961
2962
2963 /* Insert the to-be-made-available values of expression EXPRNUM for each
2964 predecessor, stored in AVAIL, into the predecessors of BLOCK, and
2965 merge the result with a phi node, given the same value number as
2966 NODE. Return true if we have inserted new stuff. */
2967
2968 static bool
2969 insert_into_preds_of_block (basic_block block, unsigned int exprnum,
2970 vec<pre_expr> avail)
2971 {
2972 pre_expr expr = expression_for_id (exprnum);
2973 pre_expr newphi;
2974 unsigned int val = get_expr_value_id (expr);
2975 edge pred;
2976 bool insertions = false;
2977 bool nophi = false;
2978 basic_block bprime;
2979 pre_expr eprime;
2980 edge_iterator ei;
2981 tree type = get_expr_type (expr);
2982 tree temp;
2983 gphi *phi;
2984
2985 /* Make sure we aren't creating an induction variable. */
2986 if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
2987 {
2988 bool firstinsideloop = false;
2989 bool secondinsideloop = false;
2990 firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
2991 EDGE_PRED (block, 0)->src);
2992 secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
2993 EDGE_PRED (block, 1)->src);
2994 /* Induction variables only have one edge inside the loop. */
2995 if ((firstinsideloop ^ secondinsideloop)
2996 && expr->kind != REFERENCE)
2997 {
2998 if (dump_file && (dump_flags & TDF_DETAILS))
2999 fprintf (dump_file, "Skipping insertion of phi for partial redundancy: Looks like an induction variable\n");
3000 nophi = true;
3001 }
3002 }
3003
3004 /* Make the necessary insertions. */
3005 FOR_EACH_EDGE (pred, ei, block->preds)
3006 {
3007 gimple_seq stmts = NULL;
3008 tree builtexpr;
3009 bprime = pred->src;
3010 eprime = avail[pred->dest_idx];
3011 builtexpr = create_expression_by_pieces (bprime, eprime,
3012 &stmts, type);
3013 gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3014 if (!gimple_seq_empty_p (stmts))
3015 {
3016 gsi_insert_seq_on_edge (pred, stmts);
3017 insertions = true;
3018 }
3019 if (!builtexpr)
3020 {
3021 /* We cannot insert a PHI node if we failed to insert
3022 on one edge. */
3023 nophi = true;
3024 continue;
3025 }
3026 if (is_gimple_min_invariant (builtexpr))
3027 avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
3028 else
3029 avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3030 }
3031 /* If we didn't want a phi node, and we made insertions, we still have
3032 inserted new stuff, and thus return true. If we didn't want a phi node,
3033 and didn't make insertions, we haven't added anything new, so return
3034 false. */
3035 if (nophi && insertions)
3036 return true;
3037 else if (nophi && !insertions)
3038 return false;
3039
3040 /* Now build a phi for the new variable. */
3041 temp = make_temp_ssa_name (type, NULL, "prephitmp");
3042 phi = create_phi_node (temp, block);
3043
3044 gimple_set_plf (phi, NECESSARY, false);
3045 VN_INFO_GET (temp)->value_id = val;
3046 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3047 if (VN_INFO (temp)->valnum == NULL_TREE)
3048 VN_INFO (temp)->valnum = temp;
3049 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3050 FOR_EACH_EDGE (pred, ei, block->preds)
3051 {
3052 pre_expr ae = avail[pred->dest_idx];
3053 gcc_assert (get_expr_type (ae) == type
3054 || useless_type_conversion_p (type, get_expr_type (ae)));
3055 if (ae->kind == CONSTANT)
3056 add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3057 pred, UNKNOWN_LOCATION);
3058 else
3059 add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3060 }
3061
3062 newphi = get_or_alloc_expr_for_name (temp);
3063 add_to_value (val, newphi);
3064
3065 /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3066 this insertion, since we test for the existence of this value in PHI_GEN
3067 before proceeding with the partial redundancy checks in insert_aux.
3068
3069 The value may exist in AVAIL_OUT, in particular, it could be represented
3070 by the expression we are trying to eliminate, in which case we want the
3071 replacement to occur. If it's not existing in AVAIL_OUT, we want it
3072 inserted there.
3073
3074 Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3075 this block, because if it did, it would have existed in our dominator's
3076 AVAIL_OUT, and would have been skipped due to the full redundancy check.
3077 */
3078
3079 bitmap_insert_into_set (PHI_GEN (block), newphi);
3080 bitmap_value_replace_in_set (AVAIL_OUT (block),
3081 newphi);
3082 bitmap_insert_into_set (NEW_SETS (block),
3083 newphi);
3084
3085 /* If we insert a PHI node for a conversion of another PHI node
3086 in the same basic-block try to preserve range information.
3087 This is important so that followup loop passes receive optimal
3088 number of iteration analysis results. See PR61743. */
3089 if (expr->kind == NARY
3090 && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3091 && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3092 && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3093 && INTEGRAL_TYPE_P (type)
3094 && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3095 && (TYPE_PRECISION (type)
3096 >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3097 && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3098 {
3099 wide_int min, max;
3100 if (get_range_info (expr->u.nary->op[0], &min, &max) == VR_RANGE
3101 && !wi::neg_p (min, SIGNED)
3102 && !wi::neg_p (max, SIGNED))
3103 /* Just handle extension and sign-changes of all-positive ranges. */
3104 set_range_info (temp,
3105 SSA_NAME_RANGE_TYPE (expr->u.nary->op[0]),
3106 wide_int_storage::from (min, TYPE_PRECISION (type),
3107 TYPE_SIGN (type)),
3108 wide_int_storage::from (max, TYPE_PRECISION (type),
3109 TYPE_SIGN (type)));
3110 }
3111
3112 if (dump_file && (dump_flags & TDF_DETAILS))
3113 {
3114 fprintf (dump_file, "Created phi ");
3115 print_gimple_stmt (dump_file, phi, 0, 0);
3116 fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3117 }
3118 pre_stats.phis++;
3119 return true;
3120 }
3121
3122
3123
3124 /* Perform insertion of partially redundant or hoistable values.
3125 For BLOCK, do the following:
3126 1. Propagate the NEW_SETS of the dominator into the current block.
3127 If the block has multiple predecessors,
3128 2a. Iterate over the ANTIC expressions for the block to see if
3129 any of them are partially redundant.
3130 2b. If so, insert them into the necessary predecessors to make
3131 the expression fully redundant.
3132 2c. Insert a new PHI merging the values of the predecessors.
3133 2d. Insert the new PHI, and the new expressions, into the
3134 NEW_SETS set.
3135 If the block has multiple successors,
3136 3a. Iterate over the ANTIC values for the block to see if
3137 any of them are good candidates for hoisting.
3138 3b. If so, insert expressions computing the values in BLOCK,
3139 and add the new expressions into the NEW_SETS set.
3140 4. Recursively call ourselves on the dominator children of BLOCK.
3141
3142 Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3143 do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3144 done in do_hoist_insertion.
3145 */
3146
3147 static bool
3148 do_pre_regular_insertion (basic_block block, basic_block dom)
3149 {
3150 bool new_stuff = false;
3151 vec<pre_expr> exprs;
3152 pre_expr expr;
3153 auto_vec<pre_expr> avail;
3154 int i;
3155
3156 exprs = sorted_array_from_bitmap_set (ANTIC_IN (block));
3157 avail.safe_grow (EDGE_COUNT (block->preds));
3158
3159 FOR_EACH_VEC_ELT (exprs, i, expr)
3160 {
3161 if (expr->kind == NARY
3162 || expr->kind == REFERENCE)
3163 {
3164 unsigned int val;
3165 bool by_some = false;
3166 bool cant_insert = false;
3167 bool all_same = true;
3168 pre_expr first_s = NULL;
3169 edge pred;
3170 basic_block bprime;
3171 pre_expr eprime = NULL;
3172 edge_iterator ei;
3173 pre_expr edoubleprime = NULL;
3174 bool do_insertion = false;
3175
3176 val = get_expr_value_id (expr);
3177 if (bitmap_set_contains_value (PHI_GEN (block), val))
3178 continue;
3179 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3180 {
3181 if (dump_file && (dump_flags & TDF_DETAILS))
3182 {
3183 fprintf (dump_file, "Found fully redundant value: ");
3184 print_pre_expr (dump_file, expr);
3185 fprintf (dump_file, "\n");
3186 }
3187 continue;
3188 }
3189
3190 FOR_EACH_EDGE (pred, ei, block->preds)
3191 {
3192 unsigned int vprime;
3193
3194 /* We should never run insertion for the exit block
3195 and so not come across fake pred edges. */
3196 gcc_assert (!(pred->flags & EDGE_FAKE));
3197 bprime = pred->src;
3198 /* We are looking at ANTIC_OUT of bprime. */
3199 eprime = phi_translate (expr, ANTIC_IN (block), NULL,
3200 bprime, block);
3201
3202 /* eprime will generally only be NULL if the
3203 value of the expression, translated
3204 through the PHI for this predecessor, is
3205 undefined. If that is the case, we can't
3206 make the expression fully redundant,
3207 because its value is undefined along a
3208 predecessor path. We can thus break out
3209 early because it doesn't matter what the
3210 rest of the results are. */
3211 if (eprime == NULL)
3212 {
3213 avail[pred->dest_idx] = NULL;
3214 cant_insert = true;
3215 break;
3216 }
3217
3218 vprime = get_expr_value_id (eprime);
3219 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3220 vprime);
3221 if (edoubleprime == NULL)
3222 {
3223 avail[pred->dest_idx] = eprime;
3224 all_same = false;
3225 }
3226 else
3227 {
3228 avail[pred->dest_idx] = edoubleprime;
3229 by_some = true;
3230 /* We want to perform insertions to remove a redundancy on
3231 a path in the CFG we want to optimize for speed. */
3232 if (optimize_edge_for_speed_p (pred))
3233 do_insertion = true;
3234 if (first_s == NULL)
3235 first_s = edoubleprime;
3236 else if (!pre_expr_d::equal (first_s, edoubleprime))
3237 all_same = false;
3238 }
3239 }
3240 /* If we can insert it, it's not the same value
3241 already existing along every predecessor, and
3242 it's defined by some predecessor, it is
3243 partially redundant. */
3244 if (!cant_insert && !all_same && by_some)
3245 {
3246 if (!do_insertion)
3247 {
3248 if (dump_file && (dump_flags & TDF_DETAILS))
3249 {
3250 fprintf (dump_file, "Skipping partial redundancy for "
3251 "expression ");
3252 print_pre_expr (dump_file, expr);
3253 fprintf (dump_file, " (%04d), no redundancy on to be "
3254 "optimized for speed edge\n", val);
3255 }
3256 }
3257 else if (dbg_cnt (treepre_insert))
3258 {
3259 if (dump_file && (dump_flags & TDF_DETAILS))
3260 {
3261 fprintf (dump_file, "Found partial redundancy for "
3262 "expression ");
3263 print_pre_expr (dump_file, expr);
3264 fprintf (dump_file, " (%04d)\n",
3265 get_expr_value_id (expr));
3266 }
3267 if (insert_into_preds_of_block (block,
3268 get_expression_id (expr),
3269 avail))
3270 new_stuff = true;
3271 }
3272 }
3273 /* If all edges produce the same value and that value is
3274 an invariant, then the PHI has the same value on all
3275 edges. Note this. */
3276 else if (!cant_insert && all_same)
3277 {
3278 gcc_assert (edoubleprime->kind == CONSTANT
3279 || edoubleprime->kind == NAME);
3280
3281 tree temp = make_temp_ssa_name (get_expr_type (expr),
3282 NULL, "pretmp");
3283 gassign *assign
3284 = gimple_build_assign (temp,
3285 edoubleprime->kind == CONSTANT ?
3286 PRE_EXPR_CONSTANT (edoubleprime) :
3287 PRE_EXPR_NAME (edoubleprime));
3288 gimple_stmt_iterator gsi = gsi_after_labels (block);
3289 gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3290
3291 gimple_set_plf (assign, NECESSARY, false);
3292 VN_INFO_GET (temp)->value_id = val;
3293 VN_INFO (temp)->valnum = sccvn_valnum_from_value_id (val);
3294 if (VN_INFO (temp)->valnum == NULL_TREE)
3295 VN_INFO (temp)->valnum = temp;
3296 bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3297 pre_expr newe = get_or_alloc_expr_for_name (temp);
3298 add_to_value (val, newe);
3299 bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3300 bitmap_insert_into_set (NEW_SETS (block), newe);
3301 }
3302 }
3303 }
3304
3305 exprs.release ();
3306 return new_stuff;
3307 }
3308
3309
3310 /* Perform insertion for partially anticipatable expressions. There
3311 is only one case we will perform insertion for these. This case is
3312 if the expression is partially anticipatable, and fully available.
3313 In this case, we know that putting it earlier will enable us to
3314 remove the later computation. */
3315
3316 static bool
3317 do_pre_partial_partial_insertion (basic_block block, basic_block dom)
3318 {
3319 bool new_stuff = false;
3320 vec<pre_expr> exprs;
3321 pre_expr expr;
3322 auto_vec<pre_expr> avail;
3323 int i;
3324
3325 exprs = sorted_array_from_bitmap_set (PA_IN (block));
3326 avail.safe_grow (EDGE_COUNT (block->preds));
3327
3328 FOR_EACH_VEC_ELT (exprs, i, expr)
3329 {
3330 if (expr->kind == NARY
3331 || expr->kind == REFERENCE)
3332 {
3333 unsigned int val;
3334 bool by_all = true;
3335 bool cant_insert = false;
3336 edge pred;
3337 basic_block bprime;
3338 pre_expr eprime = NULL;
3339 edge_iterator ei;
3340
3341 val = get_expr_value_id (expr);
3342 if (bitmap_set_contains_value (PHI_GEN (block), val))
3343 continue;
3344 if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3345 continue;
3346
3347 FOR_EACH_EDGE (pred, ei, block->preds)
3348 {
3349 unsigned int vprime;
3350 pre_expr edoubleprime;
3351
3352 /* We should never run insertion for the exit block
3353 and so not come across fake pred edges. */
3354 gcc_assert (!(pred->flags & EDGE_FAKE));
3355 bprime = pred->src;
3356 eprime = phi_translate (expr, ANTIC_IN (block),
3357 PA_IN (block),
3358 bprime, block);
3359
3360 /* eprime will generally only be NULL if the
3361 value of the expression, translated
3362 through the PHI for this predecessor, is
3363 undefined. If that is the case, we can't
3364 make the expression fully redundant,
3365 because its value is undefined along a
3366 predecessor path. We can thus break out
3367 early because it doesn't matter what the
3368 rest of the results are. */
3369 if (eprime == NULL)
3370 {
3371 avail[pred->dest_idx] = NULL;
3372 cant_insert = true;
3373 break;
3374 }
3375
3376 vprime = get_expr_value_id (eprime);
3377 edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3378 avail[pred->dest_idx] = edoubleprime;
3379 if (edoubleprime == NULL)
3380 {
3381 by_all = false;
3382 break;
3383 }
3384 }
3385
3386 /* If we can insert it, it's not the same value
3387 already existing along every predecessor, and
3388 it's defined by some predecessor, it is
3389 partially redundant. */
3390 if (!cant_insert && by_all)
3391 {
3392 edge succ;
3393 bool do_insertion = false;
3394
3395 /* Insert only if we can remove a later expression on a path
3396 that we want to optimize for speed.
3397 The phi node that we will be inserting in BLOCK is not free,
3398 and inserting it for the sake of !optimize_for_speed successor
3399 may cause regressions on the speed path. */
3400 FOR_EACH_EDGE (succ, ei, block->succs)
3401 {
3402 if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3403 || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3404 {
3405 if (optimize_edge_for_speed_p (succ))
3406 do_insertion = true;
3407 }
3408 }
3409
3410 if (!do_insertion)
3411 {
3412 if (dump_file && (dump_flags & TDF_DETAILS))
3413 {
3414 fprintf (dump_file, "Skipping partial partial redundancy "
3415 "for expression ");
3416 print_pre_expr (dump_file, expr);
3417 fprintf (dump_file, " (%04d), not (partially) anticipated "
3418 "on any to be optimized for speed edges\n", val);
3419 }
3420 }
3421 else if (dbg_cnt (treepre_insert))
3422 {
3423 pre_stats.pa_insert++;
3424 if (dump_file && (dump_flags & TDF_DETAILS))
3425 {
3426 fprintf (dump_file, "Found partial partial redundancy "
3427 "for expression ");
3428 print_pre_expr (dump_file, expr);
3429 fprintf (dump_file, " (%04d)\n",
3430 get_expr_value_id (expr));
3431 }
3432 if (insert_into_preds_of_block (block,
3433 get_expression_id (expr),
3434 avail))
3435 new_stuff = true;
3436 }
3437 }
3438 }
3439 }
3440
3441 exprs.release ();
3442 return new_stuff;
3443 }
3444
3445 /* Insert expressions in BLOCK to compute hoistable values up.
3446 Return TRUE if something was inserted, otherwise return FALSE.
3447 The caller has to make sure that BLOCK has at least two successors. */
3448
3449 static bool
3450 do_hoist_insertion (basic_block block)
3451 {
3452 edge e;
3453 edge_iterator ei;
3454 bool new_stuff = false;
3455 unsigned i;
3456 gimple_stmt_iterator last;
3457
3458 /* At least two successors, or else... */
3459 gcc_assert (EDGE_COUNT (block->succs) >= 2);
3460
3461 /* Check that all successors of BLOCK are dominated by block.
3462 We could use dominated_by_p() for this, but actually there is a much
3463 quicker check: any successor that is dominated by BLOCK can't have
3464 more than one predecessor edge. */
3465 FOR_EACH_EDGE (e, ei, block->succs)
3466 if (! single_pred_p (e->dest))
3467 return false;
3468
3469 /* Determine the insertion point. If we cannot safely insert before
3470 the last stmt if we'd have to, bail out. */
3471 last = gsi_last_bb (block);
3472 if (!gsi_end_p (last)
3473 && !is_ctrl_stmt (gsi_stmt (last))
3474 && stmt_ends_bb_p (gsi_stmt (last)))
3475 return false;
3476
3477 /* Compute the set of hoistable expressions from ANTIC_IN. First compute
3478 hoistable values. */
3479 bitmap_set hoistable_set;
3480
3481 /* A hoistable value must be in ANTIC_IN(block)
3482 but not in AVAIL_OUT(BLOCK). */
3483 bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
3484 bitmap_and_compl (&hoistable_set.values,
3485 &ANTIC_IN (block)->values, &AVAIL_OUT (block)->values);
3486
3487 /* Short-cut for a common case: hoistable_set is empty. */
3488 if (bitmap_empty_p (&hoistable_set.values))
3489 return false;
3490
3491 /* Compute which of the hoistable values is in AVAIL_OUT of
3492 at least one of the successors of BLOCK. */
3493 bitmap_head availout_in_some;
3494 bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
3495 FOR_EACH_EDGE (e, ei, block->succs)
3496 /* Do not consider expressions solely because their availability
3497 on loop exits. They'd be ANTIC-IN throughout the whole loop
3498 and thus effectively hoisted across loops by combination of
3499 PRE and hoisting. */
3500 if (! loop_exit_edge_p (block->loop_father, e))
3501 bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
3502 &AVAIL_OUT (e->dest)->values);
3503 bitmap_clear (&hoistable_set.values);
3504
3505 /* Short-cut for a common case: availout_in_some is empty. */
3506 if (bitmap_empty_p (&availout_in_some))
3507 return false;
3508
3509 /* Hack hoitable_set in-place so we can use sorted_array_from_bitmap_set. */
3510 hoistable_set.values = availout_in_some;
3511 hoistable_set.expressions = ANTIC_IN (block)->expressions;
3512
3513 /* Now finally construct the topological-ordered expression set. */
3514 vec<pre_expr> exprs = sorted_array_from_bitmap_set (&hoistable_set);
3515
3516 bitmap_clear (&hoistable_set.values);
3517
3518 /* If there are candidate values for hoisting, insert expressions
3519 strategically to make the hoistable expressions fully redundant. */
3520 pre_expr expr;
3521 FOR_EACH_VEC_ELT (exprs, i, expr)
3522 {
3523 /* While we try to sort expressions topologically above the
3524 sorting doesn't work out perfectly. Catch expressions we
3525 already inserted. */
3526 unsigned int value_id = get_expr_value_id (expr);
3527 if (bitmap_set_contains_value (AVAIL_OUT (block), value_id))
3528 {
3529 if (dump_file && (dump_flags & TDF_DETAILS))
3530 {
3531 fprintf (dump_file,
3532 "Already inserted expression for ");
3533 print_pre_expr (dump_file, expr);
3534 fprintf (dump_file, " (%04d)\n", value_id);
3535 }
3536 continue;
3537 }
3538
3539 /* OK, we should hoist this value. Perform the transformation. */
3540 pre_stats.hoist_insert++;
3541 if (dump_file && (dump_flags & TDF_DETAILS))
3542 {
3543 fprintf (dump_file,
3544 "Inserting expression in block %d for code hoisting: ",
3545 block->index);
3546 print_pre_expr (dump_file, expr);
3547 fprintf (dump_file, " (%04d)\n", value_id);
3548 }
3549
3550 gimple_seq stmts = NULL;
3551 tree res = create_expression_by_pieces (block, expr, &stmts,
3552 get_expr_type (expr));
3553
3554 /* Do not return true if expression creation ultimately
3555 did not insert any statements. */
3556 if (gimple_seq_empty_p (stmts))
3557 res = NULL_TREE;
3558 else
3559 {
3560 if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
3561 gsi_insert_seq_before (&last, stmts, GSI_SAME_STMT);
3562 else
3563 gsi_insert_seq_after (&last, stmts, GSI_NEW_STMT);
3564 }
3565
3566 /* Make sure to not return true if expression creation ultimately
3567 failed but also make sure to insert any stmts produced as they
3568 are tracked in inserted_exprs. */
3569 if (! res)
3570 continue;
3571
3572 new_stuff = true;
3573 }
3574
3575 exprs.release ();
3576
3577 return new_stuff;
3578 }
3579
3580 /* Do a dominator walk on the control flow graph, and insert computations
3581 of values as necessary for PRE and hoisting. */
3582
3583 static bool
3584 insert_aux (basic_block block, bool do_pre, bool do_hoist)
3585 {
3586 basic_block son;
3587 bool new_stuff = false;
3588
3589 if (block)
3590 {
3591 basic_block dom;
3592 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3593 if (dom)
3594 {
3595 unsigned i;
3596 bitmap_iterator bi;
3597 bitmap_set_t newset;
3598
3599 /* First, update the AVAIL_OUT set with anything we may have
3600 inserted higher up in the dominator tree. */
3601 newset = NEW_SETS (dom);
3602 if (newset)
3603 {
3604 /* Note that we need to value_replace both NEW_SETS, and
3605 AVAIL_OUT. For both the case of NEW_SETS, the value may be
3606 represented by some non-simple expression here that we want
3607 to replace it with. */
3608 FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3609 {
3610 pre_expr expr = expression_for_id (i);
3611 bitmap_value_replace_in_set (NEW_SETS (block), expr);
3612 bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3613 }
3614 }
3615
3616 /* Insert expressions for partial redundancies. */
3617 if (do_pre && !single_pred_p (block))
3618 {
3619 new_stuff |= do_pre_regular_insertion (block, dom);
3620 if (do_partial_partial)
3621 new_stuff |= do_pre_partial_partial_insertion (block, dom);
3622 }
3623
3624 /* Insert expressions for hoisting. */
3625 if (do_hoist && EDGE_COUNT (block->succs) >= 2)
3626 new_stuff |= do_hoist_insertion (block);
3627 }
3628 }
3629 for (son = first_dom_son (CDI_DOMINATORS, block);
3630 son;
3631 son = next_dom_son (CDI_DOMINATORS, son))
3632 {
3633 new_stuff |= insert_aux (son, do_pre, do_hoist);
3634 }
3635
3636 return new_stuff;
3637 }
3638
3639 /* Perform insertion of partially redundant and hoistable values. */
3640
3641 static void
3642 insert (void)
3643 {
3644 bool new_stuff = true;
3645 basic_block bb;
3646 int num_iterations = 0;
3647
3648 FOR_ALL_BB_FN (bb, cfun)
3649 NEW_SETS (bb) = bitmap_set_new ();
3650
3651 while (new_stuff)
3652 {
3653 num_iterations++;
3654 if (dump_file && dump_flags & TDF_DETAILS)
3655 fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3656 new_stuff = insert_aux (ENTRY_BLOCK_PTR_FOR_FN (cfun), flag_tree_pre,
3657 flag_code_hoisting);
3658
3659 /* Clear the NEW sets before the next iteration. We have already
3660 fully propagated its contents. */
3661 if (new_stuff)
3662 FOR_ALL_BB_FN (bb, cfun)
3663 bitmap_set_free (NEW_SETS (bb));
3664 }
3665 statistics_histogram_event (cfun, "insert iterations", num_iterations);
3666 }
3667
3668
3669 /* Compute the AVAIL set for all basic blocks.
3670
3671 This function performs value numbering of the statements in each basic
3672 block. The AVAIL sets are built from information we glean while doing
3673 this value numbering, since the AVAIL sets contain only one entry per
3674 value.
3675
3676 AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3677 AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3678
3679 static void
3680 compute_avail (void)
3681 {
3682
3683 basic_block block, son;
3684 basic_block *worklist;
3685 size_t sp = 0;
3686 unsigned i;
3687 tree name;
3688
3689 /* We pretend that default definitions are defined in the entry block.
3690 This includes function arguments and the static chain decl. */
3691 FOR_EACH_SSA_NAME (i, name, cfun)
3692 {
3693 pre_expr e;
3694 if (!SSA_NAME_IS_DEFAULT_DEF (name)
3695 || has_zero_uses (name)
3696 || virtual_operand_p (name))
3697 continue;
3698
3699 e = get_or_alloc_expr_for_name (name);
3700 add_to_value (get_expr_value_id (e), e);
3701 bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)), e);
3702 bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3703 e);
3704 }
3705
3706 if (dump_file && (dump_flags & TDF_DETAILS))
3707 {
3708 print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3709 "tmp_gen", ENTRY_BLOCK);
3710 print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (cfun)),
3711 "avail_out", ENTRY_BLOCK);
3712 }
3713
3714 /* Allocate the worklist. */
3715 worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (cfun));
3716
3717 /* Seed the algorithm by putting the dominator children of the entry
3718 block on the worklist. */
3719 for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (cfun));
3720 son;
3721 son = next_dom_son (CDI_DOMINATORS, son))
3722 worklist[sp++] = son;
3723
3724 BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (cfun))
3725 = ssa_default_def (cfun, gimple_vop (cfun));
3726
3727 /* Loop until the worklist is empty. */
3728 while (sp)
3729 {
3730 gimple *stmt;
3731 basic_block dom;
3732
3733 /* Pick a block from the worklist. */
3734 block = worklist[--sp];
3735
3736 /* Initially, the set of available values in BLOCK is that of
3737 its immediate dominator. */
3738 dom = get_immediate_dominator (CDI_DOMINATORS, block);
3739 if (dom)
3740 {
3741 bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3742 BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
3743 }
3744
3745 /* Generate values for PHI nodes. */
3746 for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
3747 gsi_next (&gsi))
3748 {
3749 tree result = gimple_phi_result (gsi.phi ());
3750
3751 /* We have no need for virtual phis, as they don't represent
3752 actual computations. */
3753 if (virtual_operand_p (result))
3754 {
3755 BB_LIVE_VOP_ON_EXIT (block) = result;
3756 continue;
3757 }
3758
3759 pre_expr e = get_or_alloc_expr_for_name (result);
3760 add_to_value (get_expr_value_id (e), e);
3761 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3762 bitmap_insert_into_set (PHI_GEN (block), e);
3763 }
3764
3765 BB_MAY_NOTRETURN (block) = 0;
3766
3767 /* Now compute value numbers and populate value sets with all
3768 the expressions computed in BLOCK. */
3769 for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
3770 gsi_next (&gsi))
3771 {
3772 ssa_op_iter iter;
3773 tree op;
3774
3775 stmt = gsi_stmt (gsi);
3776
3777 /* Cache whether the basic-block has any non-visible side-effect
3778 or control flow.
3779 If this isn't a call or it is the last stmt in the
3780 basic-block then the CFG represents things correctly. */
3781 if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
3782 {
3783 /* Non-looping const functions always return normally.
3784 Otherwise the call might not return or have side-effects
3785 that forbids hoisting possibly trapping expressions
3786 before it. */
3787 int flags = gimple_call_flags (stmt);
3788 if (!(flags & ECF_CONST)
3789 || (flags & ECF_LOOPING_CONST_OR_PURE))
3790 BB_MAY_NOTRETURN (block) = 1;
3791 }
3792
3793 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
3794 {
3795 pre_expr e = get_or_alloc_expr_for_name (op);
3796
3797 add_to_value (get_expr_value_id (e), e);
3798 bitmap_insert_into_set (TMP_GEN (block), e);
3799 bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3800 }
3801
3802 if (gimple_vdef (stmt))
3803 BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
3804
3805 if (gimple_has_side_effects (stmt)
3806 || stmt_could_throw_p (stmt)
3807 || is_gimple_debug (stmt))
3808 continue;
3809
3810 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
3811 {
3812 if (ssa_undefined_value_p (op))
3813 continue;
3814 pre_expr e = get_or_alloc_expr_for_name (op);
3815 bitmap_value_insert_into_set (EXP_GEN (block), e);
3816 }
3817
3818 switch (gimple_code (stmt))
3819 {
3820 case GIMPLE_RETURN:
3821 continue;
3822
3823 case GIMPLE_CALL:
3824 {
3825 vn_reference_t ref;
3826 vn_reference_s ref1;
3827 pre_expr result = NULL;
3828
3829 /* We can value number only calls to real functions. */
3830 if (gimple_call_internal_p (stmt))
3831 continue;
3832
3833 vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
3834 if (!ref)
3835 continue;
3836
3837 /* If the value of the call is not invalidated in
3838 this block until it is computed, add the expression
3839 to EXP_GEN. */
3840 if (!gimple_vuse (stmt)
3841 || gimple_code
3842 (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
3843 || gimple_bb (SSA_NAME_DEF_STMT
3844 (gimple_vuse (stmt))) != block)
3845 {
3846 result = pre_expr_pool.allocate ();
3847 result->kind = REFERENCE;
3848 result->id = 0;
3849 PRE_EXPR_REFERENCE (result) = ref;
3850
3851 get_or_alloc_expression_id (result);
3852 add_to_value (get_expr_value_id (result), result);
3853 bitmap_value_insert_into_set (EXP_GEN (block), result);
3854 }
3855 continue;
3856 }
3857
3858 case GIMPLE_ASSIGN:
3859 {
3860 pre_expr result = NULL;
3861 switch (vn_get_stmt_kind (stmt))
3862 {
3863 case VN_NARY:
3864 {
3865 enum tree_code code = gimple_assign_rhs_code (stmt);
3866 vn_nary_op_t nary;
3867
3868 /* COND_EXPR and VEC_COND_EXPR are awkward in
3869 that they contain an embedded complex expression.
3870 Don't even try to shove those through PRE. */
3871 if (code == COND_EXPR
3872 || code == VEC_COND_EXPR)
3873 continue;
3874
3875 vn_nary_op_lookup_stmt (stmt, &nary);
3876 if (!nary)
3877 continue;
3878
3879 /* If the NARY traps and there was a preceding
3880 point in the block that might not return avoid
3881 adding the nary to EXP_GEN. */
3882 if (BB_MAY_NOTRETURN (block)
3883 && vn_nary_may_trap (nary))
3884 continue;
3885
3886 result = pre_expr_pool.allocate ();
3887 result->kind = NARY;
3888 result->id = 0;
3889 PRE_EXPR_NARY (result) = nary;
3890 break;
3891 }
3892
3893 case VN_REFERENCE:
3894 {
3895 tree rhs1 = gimple_assign_rhs1 (stmt);
3896 alias_set_type set = get_alias_set (rhs1);
3897 vec<vn_reference_op_s> operands
3898 = vn_reference_operands_for_lookup (rhs1);
3899 vn_reference_t ref;
3900 vn_reference_lookup_pieces (gimple_vuse (stmt), set,
3901 TREE_TYPE (rhs1),
3902 operands, &ref, VN_WALK);
3903 if (!ref)
3904 {
3905 operands.release ();
3906 continue;
3907 }
3908
3909 /* If the value of the reference is not invalidated in
3910 this block until it is computed, add the expression
3911 to EXP_GEN. */
3912 if (gimple_vuse (stmt))
3913 {
3914 gimple *def_stmt;
3915 bool ok = true;
3916 def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
3917 while (!gimple_nop_p (def_stmt)
3918 && gimple_code (def_stmt) != GIMPLE_PHI
3919 && gimple_bb (def_stmt) == block)
3920 {
3921 if (stmt_may_clobber_ref_p
3922 (def_stmt, gimple_assign_rhs1 (stmt)))
3923 {
3924 ok = false;
3925 break;
3926 }
3927 def_stmt
3928 = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
3929 }
3930 if (!ok)
3931 {
3932 operands.release ();
3933 continue;
3934 }
3935 }
3936
3937 /* If the load was value-numbered to another
3938 load make sure we do not use its expression
3939 for insertion if it wouldn't be a valid
3940 replacement. */
3941 /* At the momemt we have a testcase
3942 for hoist insertion of aligned vs. misaligned
3943 variants in gcc.dg/torture/pr65270-1.c thus
3944 with just alignment to be considered we can
3945 simply replace the expression in the hashtable
3946 with the most conservative one. */
3947 vn_reference_op_t ref1 = &ref->operands.last ();
3948 while (ref1->opcode != TARGET_MEM_REF
3949 && ref1->opcode != MEM_REF
3950 && ref1 != &ref->operands[0])
3951 --ref1;
3952 vn_reference_op_t ref2 = &operands.last ();
3953 while (ref2->opcode != TARGET_MEM_REF
3954 && ref2->opcode != MEM_REF
3955 && ref2 != &operands[0])
3956 --ref2;
3957 if ((ref1->opcode == TARGET_MEM_REF
3958 || ref1->opcode == MEM_REF)
3959 && (TYPE_ALIGN (ref1->type)
3960 > TYPE_ALIGN (ref2->type)))
3961 ref1->type
3962 = build_aligned_type (ref1->type,
3963 TYPE_ALIGN (ref2->type));
3964 /* TBAA behavior is an obvious part so make sure
3965 that the hashtable one covers this as well
3966 by adjusting the ref alias set and its base. */
3967 if (ref->set == set
3968 || alias_set_subset_of (set, ref->set))
3969 ;
3970 else if (alias_set_subset_of (ref->set, set))
3971 {
3972 ref->set = set;
3973 if (ref1->opcode == MEM_REF)
3974 ref1->op0 = fold_convert (TREE_TYPE (ref2->op0),
3975 ref1->op0);
3976 else
3977 ref1->op2 = fold_convert (TREE_TYPE (ref2->op2),
3978 ref1->op2);
3979 }
3980 else
3981 {
3982 ref->set = 0;
3983 if (ref1->opcode == MEM_REF)
3984 ref1->op0 = fold_convert (ptr_type_node,
3985 ref1->op0);
3986 else
3987 ref1->op2 = fold_convert (ptr_type_node,
3988 ref1->op2);
3989 }
3990 operands.release ();
3991
3992 result = pre_expr_pool.allocate ();
3993 result->kind = REFERENCE;
3994 result->id = 0;
3995 PRE_EXPR_REFERENCE (result) = ref;
3996 break;
3997 }
3998
3999 default:
4000 continue;
4001 }
4002
4003 get_or_alloc_expression_id (result);
4004 add_to_value (get_expr_value_id (result), result);
4005 bitmap_value_insert_into_set (EXP_GEN (block), result);
4006 continue;
4007 }
4008 default:
4009 break;
4010 }
4011 }
4012
4013 if (dump_file && (dump_flags & TDF_DETAILS))
4014 {
4015 print_bitmap_set (dump_file, EXP_GEN (block),
4016 "exp_gen", block->index);
4017 print_bitmap_set (dump_file, PHI_GEN (block),
4018 "phi_gen", block->index);
4019 print_bitmap_set (dump_file, TMP_GEN (block),
4020 "tmp_gen", block->index);
4021 print_bitmap_set (dump_file, AVAIL_OUT (block),
4022 "avail_out", block->index);
4023 }
4024
4025 /* Put the dominator children of BLOCK on the worklist of blocks
4026 to compute available sets for. */
4027 for (son = first_dom_son (CDI_DOMINATORS, block);
4028 son;
4029 son = next_dom_son (CDI_DOMINATORS, son))
4030 worklist[sp++] = son;
4031 }
4032
4033 free (worklist);
4034 }
4035
4036
4037 /* Local state for the eliminate domwalk. */
4038 static vec<gimple *> el_to_remove;
4039 static vec<gimple *> el_to_fixup;
4040 static unsigned int el_todo;
4041 static vec<tree> el_avail;
4042 static vec<tree> el_avail_stack;
4043
4044 /* Return a leader for OP that is available at the current point of the
4045 eliminate domwalk. */
4046
4047 static tree
4048 eliminate_avail (tree op)
4049 {
4050 tree valnum = VN_INFO (op)->valnum;
4051 if (TREE_CODE (valnum) == SSA_NAME)
4052 {
4053 if (SSA_NAME_IS_DEFAULT_DEF (valnum))
4054 return valnum;
4055 if (el_avail.length () > SSA_NAME_VERSION (valnum))
4056 return el_avail[SSA_NAME_VERSION (valnum)];
4057 }
4058 else if (is_gimple_min_invariant (valnum))
4059 return valnum;
4060 return NULL_TREE;
4061 }
4062
4063 /* At the current point of the eliminate domwalk make OP available. */
4064
4065 static void
4066 eliminate_push_avail (tree op)
4067 {
4068 tree valnum = VN_INFO (op)->valnum;
4069 if (TREE_CODE (valnum) == SSA_NAME)
4070 {
4071 if (el_avail.length () <= SSA_NAME_VERSION (valnum))
4072 el_avail.safe_grow_cleared (SSA_NAME_VERSION (valnum) + 1);
4073 tree pushop = op;
4074 if (el_avail[SSA_NAME_VERSION (valnum)])
4075 pushop = el_avail[SSA_NAME_VERSION (valnum)];
4076 el_avail_stack.safe_push (pushop);
4077 el_avail[SSA_NAME_VERSION (valnum)] = op;
4078 }
4079 }
4080
4081 /* Insert the expression recorded by SCCVN for VAL at *GSI. Returns
4082 the leader for the expression if insertion was successful. */
4083
4084 static tree
4085 eliminate_insert (gimple_stmt_iterator *gsi, tree val)
4086 {
4087 gimple *stmt = gimple_seq_first_stmt (VN_INFO (val)->expr);
4088 if (!is_gimple_assign (stmt)
4089 || (!CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stmt))
4090 && gimple_assign_rhs_code (stmt) != VIEW_CONVERT_EXPR
4091 && gimple_assign_rhs_code (stmt) != BIT_FIELD_REF))
4092 return NULL_TREE;
4093
4094 tree op = gimple_assign_rhs1 (stmt);
4095 if (gimple_assign_rhs_code (stmt) == VIEW_CONVERT_EXPR
4096 || gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
4097 op = TREE_OPERAND (op, 0);
4098 tree leader = TREE_CODE (op) == SSA_NAME ? eliminate_avail (op) : op;
4099 if (!leader)
4100 return NULL_TREE;
4101
4102 gimple_seq stmts = NULL;
4103 tree res;
4104 if (gimple_assign_rhs_code (stmt) == BIT_FIELD_REF)
4105 res = gimple_build (&stmts, BIT_FIELD_REF,
4106 TREE_TYPE (val), leader,
4107 TREE_OPERAND (gimple_assign_rhs1 (stmt), 1),
4108 TREE_OPERAND (gimple_assign_rhs1 (stmt), 2));
4109 else
4110 res = gimple_build (&stmts, gimple_assign_rhs_code (stmt),
4111 TREE_TYPE (val), leader);
4112 gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4113 VN_INFO_GET (res)->valnum = val;
4114
4115 if (TREE_CODE (leader) == SSA_NAME)
4116 gimple_set_plf (SSA_NAME_DEF_STMT (leader), NECESSARY, true);
4117
4118 pre_stats.insertions++;
4119 if (dump_file && (dump_flags & TDF_DETAILS))
4120 {
4121 fprintf (dump_file, "Inserted ");
4122 print_gimple_stmt (dump_file, SSA_NAME_DEF_STMT (res), 0, 0);
4123 }
4124
4125 return res;
4126 }
4127
4128 class eliminate_dom_walker : public dom_walker
4129 {
4130 public:
4131 eliminate_dom_walker (cdi_direction direction, bool do_pre_)
4132 : dom_walker (direction), do_pre (do_pre_) {}
4133
4134 virtual edge before_dom_children (basic_block);
4135 virtual void after_dom_children (basic_block);
4136
4137 bool do_pre;
4138 };
4139
4140 /* Perform elimination for the basic-block B during the domwalk. */
4141
4142 edge
4143 eliminate_dom_walker::before_dom_children (basic_block b)
4144 {
4145 /* Mark new bb. */
4146 el_avail_stack.safe_push (NULL_TREE);
4147
4148 /* ??? If we do nothing for unreachable blocks then this will confuse
4149 tailmerging. Eventually we can reduce its reliance on SCCVN now
4150 that we fully copy/constant-propagate (most) things. */
4151
4152 for (gphi_iterator gsi = gsi_start_phis (b); !gsi_end_p (gsi);)
4153 {
4154 gphi *phi = gsi.phi ();
4155 tree res = PHI_RESULT (phi);
4156
4157 if (virtual_operand_p (res))
4158 {
4159 gsi_next (&gsi);
4160 continue;
4161 }
4162
4163 tree sprime = eliminate_avail (res);
4164 if (sprime
4165 && sprime != res)
4166 {
4167 if (dump_file && (dump_flags & TDF_DETAILS))
4168 {
4169 fprintf (dump_file, "Replaced redundant PHI node defining ");
4170 print_generic_expr (dump_file, res, 0);
4171 fprintf (dump_file, " with ");
4172 print_generic_expr (dump_file, sprime, 0);
4173 fprintf (dump_file, "\n");
4174 }
4175
4176 /* If we inserted this PHI node ourself, it's not an elimination. */
4177 if (inserted_exprs
4178 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res)))
4179 pre_stats.phis--;
4180 else
4181 pre_stats.eliminations++;
4182
4183 /* If we will propagate into all uses don't bother to do
4184 anything. */
4185 if (may_propagate_copy (res, sprime))
4186 {
4187 /* Mark the PHI for removal. */
4188 el_to_remove.safe_push (phi);
4189 gsi_next (&gsi);
4190 continue;
4191 }
4192
4193 remove_phi_node (&gsi, false);
4194
4195 if (inserted_exprs
4196 && !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (res))
4197 && TREE_CODE (sprime) == SSA_NAME)
4198 gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
4199
4200 if (!useless_type_conversion_p (TREE_TYPE (res), TREE_TYPE (sprime)))
4201 sprime = fold_convert (TREE_TYPE (res), sprime);
4202 gimple *stmt = gimple_build_assign (res, sprime);
4203 /* ??? It cannot yet be necessary (DOM walk). */
4204 gimple_set_plf (stmt, NECESSARY, gimple_plf (phi, NECESSARY));
4205
4206 gimple_stmt_iterator gsi2 = gsi_after_labels (b);
4207 gsi_insert_before (&gsi2, stmt, GSI_NEW_STMT);
4208 continue;
4209 }
4210
4211 eliminate_push_avail (res);
4212 gsi_next (&gsi);
4213 }
4214
4215 for (gimple_stmt_iterator gsi = gsi_start_bb (b);
4216 !gsi_end_p (gsi);
4217 gsi_next (&gsi))
4218 {
4219 tree sprime = NULL_TREE;
4220 gimple *stmt = gsi_stmt (gsi);
4221 tree lhs = gimple_get_lhs (stmt);
4222 if (lhs && TREE_CODE (lhs) == SSA_NAME
4223 && !gimple_has_volatile_ops (stmt)
4224 /* See PR43491. Do not replace a global register variable when
4225 it is a the RHS of an assignment. Do replace local register
4226 variables since gcc does not guarantee a local variable will
4227 be allocated in register.
4228 ??? The fix isn't effective here. This should instead
4229 be ensured by not value-numbering them the same but treating
4230 them like volatiles? */
4231 && !(gimple_assign_single_p (stmt)
4232 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == VAR_DECL
4233 && DECL_HARD_REGISTER (gimple_assign_rhs1 (stmt))
4234 && is_global_var (gimple_assign_rhs1 (stmt)))))
4235 {
4236 sprime = eliminate_avail (lhs);
4237 if (!sprime)
4238 {
4239 /* If there is no existing usable leader but SCCVN thinks
4240 it has an expression it wants to use as replacement,
4241 insert that. */
4242 tree val = VN_INFO (lhs)->valnum;
4243 if (val != VN_TOP
4244 && TREE_CODE (val) == SSA_NAME
4245 && VN_INFO (val)->needs_insertion
4246 && VN_INFO (val)->expr != NULL
4247 && (sprime = eliminate_insert (&gsi, val)) != NULL_TREE)
4248 eliminate_push_avail (sprime);
4249 }
4250
4251 /* If this now constitutes a copy duplicate points-to
4252 and range info appropriately. This is especially
4253 important for inserted code. See tree-ssa-copy.c
4254 for similar code. */
4255 if (sprime
4256 && TREE_CODE (sprime) == SSA_NAME)
4257 {
4258 basic_block sprime_b = gimple_bb (SSA_NAME_DEF_STMT (sprime));
4259 if (POINTER_TYPE_P (TREE_TYPE (lhs))
4260 && VN_INFO_PTR_INFO (lhs)
4261 && ! VN_INFO_PTR_INFO (sprime))
4262 {
4263 duplicate_ssa_name_ptr_info (sprime,
4264 VN_INFO_PTR_INFO (lhs));
4265 if (b != sprime_b)
4266 mark_ptr_info_alignment_unknown
4267 (SSA_NAME_PTR_INFO (sprime));
4268 }
4269 else if (INTEGRAL_TYPE_P (TREE_TYPE (lhs))
4270 && VN_INFO_RANGE_INFO (lhs)
4271 && ! VN_INFO_RANGE_INFO (sprime)
4272 && b == sprime_b)
4273 duplicate_ssa_name_range_info (sprime,
4274 VN_INFO_RANGE_TYPE (lhs),
4275 VN_INFO_RANGE_INFO (lhs));
4276 }
4277
4278 /* Inhibit the use of an inserted PHI on a loop header when
4279 the address of the memory reference is a simple induction
4280 variable. In other cases the vectorizer won't do anything
4281 anyway (either it's loop invariant or a complicated
4282 expression). */
4283 if (sprime
4284 && TREE_CODE (sprime) == SSA_NAME
4285 && do_pre
4286 && (flag_tree_loop_vectorize || flag_tree_parallelize_loops > 1)
4287 && loop_outer (b->loop_father)
4288 && has_zero_uses (sprime)
4289 && bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))
4290 && gimple_assign_load_p (stmt))
4291 {
4292 gimple *def_stmt = SSA_NAME_DEF_STMT (sprime);
4293 basic_block def_bb = gimple_bb (def_stmt);
4294 if (gimple_code (def_stmt) == GIMPLE_PHI
4295 && def_bb->loop_father->header == def_bb)
4296 {
4297 loop_p loop = def_bb->loop_father;
4298 ssa_op_iter iter;
4299 tree op;
4300 bool found = false;
4301 FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
4302 {
4303 affine_iv iv;
4304 def_bb = gimple_bb (SSA_NAME_DEF_STMT (op));
4305 if (def_bb
4306 && flow_bb_inside_loop_p (loop, def_bb)
4307 && simple_iv (loop, loop, op, &iv, true))
4308 {
4309 found = true;
4310 break;
4311 }
4312 }
4313 if (found)
4314 {
4315 if (dump_file && (dump_flags & TDF_DETAILS))
4316 {
4317 fprintf (dump_file, "Not replacing ");
4318 print_gimple_expr (dump_file, stmt, 0, 0);
4319 fprintf (dump_file, " with ");
4320 print_generic_expr (dump_file, sprime, 0);
4321 fprintf (dump_file, " which would add a loop"
4322 " carried dependence to loop %d\n",
4323 loop->num);
4324 }
4325 /* Don't keep sprime available. */
4326 sprime = NULL_TREE;
4327 }
4328 }
4329 }
4330
4331 if (sprime)
4332 {
4333 /* If we can propagate the value computed for LHS into
4334 all uses don't bother doing anything with this stmt. */
4335 if (may_propagate_copy (lhs, sprime))
4336 {
4337 /* Mark it for removal. */
4338 el_to_remove.safe_push (stmt);
4339
4340 /* ??? Don't count copy/constant propagations. */
4341 if (gimple_assign_single_p (stmt)
4342 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
4343 || gimple_assign_rhs1 (stmt) == sprime))
4344 continue;
4345
4346 if (dump_file && (dump_flags & TDF_DETAILS))
4347 {
4348 fprintf (dump_file, "Replaced ");
4349 print_gimple_expr (dump_file, stmt, 0, 0);
4350 fprintf (dump_file, " with ");
4351 print_generic_expr (dump_file, sprime, 0);
4352 fprintf (dump_file, " in all uses of ");
4353 print_gimple_stmt (dump_file, stmt, 0, 0);
4354 }
4355
4356 pre_stats.eliminations++;
4357 continue;
4358 }
4359
4360 /* If this is an assignment from our leader (which
4361 happens in the case the value-number is a constant)
4362 then there is nothing to do. */
4363 if (gimple_assign_single_p (stmt)
4364 && sprime == gimple_assign_rhs1 (stmt))
4365 continue;
4366
4367 /* Else replace its RHS. */
4368 bool can_make_abnormal_goto
4369 = is_gimple_call (stmt)
4370 && stmt_can_make_abnormal_goto (stmt);
4371
4372 if (dump_file && (dump_flags & TDF_DETAILS))
4373 {
4374 fprintf (dump_file, "Replaced ");
4375 print_gimple_expr (dump_file, stmt, 0, 0);
4376 fprintf (dump_file, " with ");
4377 print_generic_expr (dump_file, sprime, 0);
4378 fprintf (dump_file, " in ");
4379 print_gimple_stmt (dump_file, stmt, 0, 0);
4380 }
4381
4382 if (TREE_CODE (sprime) == SSA_NAME)
4383 gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
4384 NECESSARY, true);
4385
4386 pre_stats.eliminations++;
4387 gimple *orig_stmt = stmt;
4388 if (!useless_type_conversion_p (TREE_TYPE (lhs),
4389 TREE_TYPE (sprime)))
4390 sprime = fold_convert (TREE_TYPE (lhs), sprime);
4391 tree vdef = gimple_vdef (stmt);
4392 tree vuse = gimple_vuse (stmt);
4393 propagate_tree_value_into_stmt (&gsi, sprime);
4394 stmt = gsi_stmt (gsi);
4395 update_stmt (stmt);
4396 if (vdef != gimple_vdef (stmt))
4397 VN_INFO (vdef)->valnum = vuse;
4398
4399 /* If we removed EH side-effects from the statement, clean
4400 its EH information. */
4401 if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
4402 {
4403 bitmap_set_bit (need_eh_cleanup,
4404 gimple_bb (stmt)->index);
4405 if (dump_file && (dump_flags & TDF_DETAILS))
4406 fprintf (dump_file, " Removed EH side-effects.\n");
4407 }
4408
4409 /* Likewise for AB side-effects. */
4410 if (can_make_abnormal_goto
4411 && !stmt_can_make_abnormal_goto (stmt))
4412 {
4413 bitmap_set_bit (need_ab_cleanup,
4414 gimple_bb (stmt)->index);
4415 if (dump_file && (dump_flags & TDF_DETAILS))
4416 fprintf (dump_file, " Removed AB side-effects.\n");
4417 }
4418
4419 continue;
4420 }
4421 }
4422
4423 /* If the statement is a scalar store, see if the expression
4424 has the same value number as its rhs. If so, the store is
4425 dead. */
4426 if (gimple_assign_single_p (stmt)
4427 && !gimple_has_volatile_ops (stmt)
4428 && !is_gimple_reg (gimple_assign_lhs (stmt))
4429 && (TREE_CODE (gimple_assign_rhs1 (stmt)) == SSA_NAME
4430 || is_gimple_min_invariant (gimple_assign_rhs1 (stmt))))
4431 {
4432 tree val;
4433 tree rhs = gimple_assign_rhs1 (stmt);
4434 val = vn_reference_lookup (gimple_assign_lhs (stmt),
4435 gimple_vuse (stmt), VN_WALK, NULL, false);
4436 if (TREE_CODE (rhs) == SSA_NAME)
4437 rhs = VN_INFO (rhs)->valnum;
4438 if (val
4439 && operand_equal_p (val, rhs, 0))
4440 {
4441 if (dump_file && (dump_flags & TDF_DETAILS))
4442 {
4443 fprintf (dump_file, "Deleted redundant store ");
4444 print_gimple_stmt (dump_file, stmt, 0, 0);
4445 }
4446
4447 /* Queue stmt for removal. */
4448 el_to_remove.safe_push (stmt);
4449 continue;
4450 }
4451 }
4452
4453 /* If this is a control statement value numbering left edges
4454 unexecuted on force the condition in a way consistent with
4455 that. */
4456 if (gcond *cond = dyn_cast <gcond *> (stmt))
4457 {
4458 if ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE)
4459 ^ (EDGE_SUCC (b, 1)->flags & EDGE_EXECUTABLE))
4460 {
4461 if (dump_file && (dump_flags & TDF_DETAILS))
4462 {
4463 fprintf (dump_file, "Removing unexecutable edge from ");
4464 print_gimple_stmt (dump_file, stmt, 0, 0);
4465 }
4466 if (((EDGE_SUCC (b, 0)->flags & EDGE_TRUE_VALUE) != 0)
4467 == ((EDGE_SUCC (b, 0)->flags & EDGE_EXECUTABLE) != 0))
4468 gimple_cond_make_true (cond);
4469 else
4470 gimple_cond_make_false (cond);
4471 update_stmt (cond);
4472 el_todo |= TODO_cleanup_cfg;
4473 continue;
4474 }
4475 }
4476
4477 bool can_make_abnormal_goto = stmt_can_make_abnormal_goto (stmt);
4478 bool was_noreturn = (is_gimple_call (stmt)
4479 && gimple_call_noreturn_p (stmt));
4480 tree vdef = gimple_vdef (stmt);
4481 tree vuse = gimple_vuse (stmt);
4482
4483 /* If we didn't replace the whole stmt (or propagate the result
4484 into all uses), replace all uses on this stmt with their
4485 leaders. */
4486 use_operand_p use_p;
4487 ssa_op_iter iter;
4488 FOR_EACH_SSA_USE_OPERAND (use_p, stmt, iter, SSA_OP_USE)
4489 {
4490 tree use = USE_FROM_PTR (use_p);
4491 /* ??? The call code above leaves stmt operands un-updated. */
4492 if (TREE_CODE (use) != SSA_NAME)
4493 continue;
4494 tree sprime = eliminate_avail (use);
4495 if (sprime && sprime != use
4496 && may_propagate_copy (use, sprime)
4497 /* We substitute into debug stmts to avoid excessive
4498 debug temporaries created by removed stmts, but we need
4499 to avoid doing so for inserted sprimes as we never want
4500 to create debug temporaries for them. */
4501 && (!inserted_exprs
4502 || TREE_CODE (sprime) != SSA_NAME
4503 || !is_gimple_debug (stmt)
4504 || !bitmap_bit_p (inserted_exprs, SSA_NAME_VERSION (sprime))))
4505 {
4506 propagate_value (use_p, sprime);
4507 gimple_set_modified (stmt, true);
4508 if (TREE_CODE (sprime) == SSA_NAME
4509 && !is_gimple_debug (stmt))
4510 gimple_set_plf (SSA_NAME_DEF_STMT (sprime),
4511 NECESSARY, true);
4512 }
4513 }
4514
4515 /* Visit indirect calls and turn them into direct calls if
4516 possible using the devirtualization machinery. */
4517 if (gcall *call_stmt = dyn_cast <gcall *> (stmt))
4518 {
4519 tree fn = gimple_call_fn (call_stmt);
4520 if (fn
4521 && flag_devirtualize
4522 && virtual_method_call_p (fn))
4523 {
4524 tree otr_type = obj_type_ref_class (fn);
4525 tree instance;
4526 ipa_polymorphic_call_context context (current_function_decl, fn, stmt, &instance);
4527 bool final;
4528
4529 context.get_dynamic_type (instance, OBJ_TYPE_REF_OBJECT (fn), otr_type, stmt);
4530
4531 vec <cgraph_node *>targets
4532 = possible_polymorphic_call_targets (obj_type_ref_class (fn),
4533 tree_to_uhwi
4534 (OBJ_TYPE_REF_TOKEN (fn)),
4535 context,
4536 &final);
4537 if (dump_file)
4538 dump_possible_polymorphic_call_targets (dump_file,
4539 obj_type_ref_class (fn),
4540 tree_to_uhwi
4541 (OBJ_TYPE_REF_TOKEN (fn)),
4542 context);
4543 if (final && targets.length () <= 1 && dbg_cnt (devirt))
4544 {
4545 tree fn;
4546 if (targets.length () == 1)
4547 fn = targets[0]->decl;
4548 else
4549 fn = builtin_decl_implicit (BUILT_IN_UNREACHABLE);
4550 if (dump_enabled_p ())
4551 {
4552 location_t loc = gimple_location_safe (stmt);
4553 dump_printf_loc (MSG_OPTIMIZED_LOCATIONS, loc,
4554 "converting indirect call to "
4555 "function %s\n",
4556 lang_hooks.decl_printable_name (fn, 2));
4557 }
4558 gimple_call_set_fndecl (call_stmt, fn);
4559 /* If changing the call to __builtin_unreachable
4560 or similar noreturn function, adjust gimple_call_fntype
4561 too. */
4562 if (gimple_call_noreturn_p (call_stmt)
4563 && VOID_TYPE_P (TREE_TYPE (TREE_TYPE (fn)))
4564 && TYPE_ARG_TYPES (TREE_TYPE (fn))
4565 && (TREE_VALUE (TYPE_ARG_TYPES (TREE_TYPE (fn)))
4566 == void_type_node))
4567 gimple_call_set_fntype (call_stmt, TREE_TYPE (fn));
4568 maybe_remove_unused_call_args (cfun, call_stmt);
4569 gimple_set_modified (stmt, true);
4570 }
4571 }
4572 }
4573
4574 if (gimple_modified_p (stmt))
4575 {
4576 /* If a formerly non-invariant ADDR_EXPR is turned into an
4577 invariant one it was on a separate stmt. */
4578 if (gimple_assign_single_p (stmt)
4579 && TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
4580 recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
4581 gimple *old_stmt = stmt;
4582 if (is_gimple_call (stmt))
4583 {
4584 /* ??? Only fold calls inplace for now, this may create new
4585 SSA names which in turn will confuse free_scc_vn SSA name
4586 release code. */
4587 fold_stmt_inplace (&gsi);
4588 /* When changing a call into a noreturn call, cfg cleanup
4589 is needed to fix up the noreturn call. */
4590 if (!was_noreturn && gimple_call_noreturn_p (stmt))
4591 el_to_fixup.safe_push (stmt);
4592 }
4593 else
4594 {
4595 fold_stmt (&gsi);
4596 stmt = gsi_stmt (gsi);
4597 if ((gimple_code (stmt) == GIMPLE_COND
4598 && (gimple_cond_true_p (as_a <gcond *> (stmt))
4599 || gimple_cond_false_p (as_a <gcond *> (stmt))))
4600 || (gimple_code (stmt) == GIMPLE_SWITCH
4601 && TREE_CODE (gimple_switch_index (
4602 as_a <gswitch *> (stmt)))
4603 == INTEGER_CST))
4604 el_todo |= TODO_cleanup_cfg;
4605 }
4606 /* If we removed EH side-effects from the statement, clean
4607 its EH information. */
4608 if (maybe_clean_or_replace_eh_stmt (old_stmt, stmt))
4609 {
4610 bitmap_set_bit (need_eh_cleanup,
4611 gimple_bb (stmt)->index);
4612 if (dump_file && (dump_flags & TDF_DETAILS))
4613 fprintf (dump_file, " Removed EH side-effects.\n");
4614 }
4615 /* Likewise for AB side-effects. */
4616 if (can_make_abnormal_goto
4617 && !stmt_can_make_abnormal_goto (stmt))
4618 {
4619 bitmap_set_bit (need_ab_cleanup,
4620 gimple_bb (stmt)->index);
4621 if (dump_file && (dump_flags & TDF_DETAILS))
4622 fprintf (dump_file, " Removed AB side-effects.\n");
4623 }
4624 update_stmt (stmt);
4625 if (vdef != gimple_vdef (stmt))
4626 VN_INFO (vdef)->valnum = vuse;
4627 }
4628
4629 /* Make new values available - for fully redundant LHS we
4630 continue with the next stmt above and skip this. */
4631 def_operand_p defp;
4632 FOR_EACH_SSA_DEF_OPERAND (defp, stmt, iter, SSA_OP_DEF)
4633 eliminate_push_avail (DEF_FROM_PTR (defp));
4634 }
4635
4636 /* Replace destination PHI arguments. */
4637 edge_iterator ei;
4638 edge e;
4639 FOR_EACH_EDGE (e, ei, b->succs)
4640 {
4641 for (gphi_iterator gsi = gsi_start_phis (e->dest);
4642 !gsi_end_p (gsi);
4643 gsi_next (&gsi))
4644 {
4645 gphi *phi = gsi.phi ();
4646 use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
4647 tree arg = USE_FROM_PTR (use_p);
4648 if (TREE_CODE (arg) != SSA_NAME
4649 || virtual_operand_p (arg))
4650 continue;
4651 tree sprime = eliminate_avail (arg);
4652 if (sprime && may_propagate_copy (arg, sprime))
4653 {
4654 propagate_value (use_p, sprime);
4655 if (TREE_CODE (sprime) == SSA_NAME)
4656 gimple_set_plf (SSA_NAME_DEF_STMT (sprime), NECESSARY, true);
4657 }
4658 }
4659 }
4660 return NULL;
4661 }
4662
4663 /* Make no longer available leaders no longer available. */
4664
4665 void
4666 eliminate_dom_walker::after_dom_children (basic_block)
4667 {
4668 tree entry;
4669 while ((entry = el_avail_stack.pop ()) != NULL_TREE)
4670 {
4671 tree valnum = VN_INFO (entry)->valnum;
4672 tree old = el_avail[SSA_NAME_VERSION (valnum)];
4673 if (old == entry)
4674 el_avail[SSA_NAME_VERSION (valnum)] = NULL_TREE;
4675 else
4676 el_avail[SSA_NAME_VERSION (valnum)] = entry;
4677 }
4678 }
4679
4680 /* Eliminate fully redundant computations. */
4681
4682 static unsigned int
4683 eliminate (bool do_pre)
4684 {
4685 gimple_stmt_iterator gsi;
4686 gimple *stmt;
4687
4688 need_eh_cleanup = BITMAP_ALLOC (NULL);
4689 need_ab_cleanup = BITMAP_ALLOC (NULL);
4690
4691 el_to_remove.create (0);
4692 el_to_fixup.create (0);
4693 el_todo = 0;
4694 el_avail.create (num_ssa_names);
4695 el_avail_stack.create (0);
4696
4697 eliminate_dom_walker (CDI_DOMINATORS,
4698 do_pre).walk (cfun->cfg->x_entry_block_ptr);
4699
4700 el_avail.release ();
4701 el_avail_stack.release ();
4702
4703 /* We cannot remove stmts during BB walk, especially not release SSA
4704 names there as this confuses the VN machinery. The stmts ending
4705 up in el_to_remove are either stores or simple copies.
4706 Remove stmts in reverse order to make debug stmt creation possible. */
4707 while (!el_to_remove.is_empty ())
4708 {
4709 stmt = el_to_remove.pop ();
4710
4711 if (dump_file && (dump_flags & TDF_DETAILS))
4712 {
4713 fprintf (dump_file, "Removing dead stmt ");
4714 print_gimple_stmt (dump_file, stmt, 0, 0);
4715 }
4716
4717 tree lhs;
4718 if (gimple_code (stmt) == GIMPLE_PHI)
4719 lhs = gimple_phi_result (stmt);
4720 else
4721 lhs = gimple_get_lhs (stmt);
4722
4723 if (inserted_exprs
4724 && TREE_CODE (lhs) == SSA_NAME)
4725 bitmap_clear_bit (inserted_exprs, SSA_NAME_VERSION (lhs));
4726
4727 gsi = gsi_for_stmt (stmt);
4728 if (gimple_code (stmt) == GIMPLE_PHI)
4729 remove_phi_node (&gsi, true);
4730 else
4731 {
4732 basic_block bb = gimple_bb (stmt);
4733 unlink_stmt_vdef (stmt);
4734 if (gsi_remove (&gsi, true))
4735 bitmap_set_bit (need_eh_cleanup, bb->index);
4736 if (is_gimple_call (stmt) && stmt_can_make_abnormal_goto (stmt))
4737 bitmap_set_bit (need_ab_cleanup, bb->index);
4738 release_defs (stmt);
4739 }
4740
4741 /* Removing a stmt may expose a forwarder block. */
4742 el_todo |= TODO_cleanup_cfg;
4743 }
4744 el_to_remove.release ();
4745
4746 /* Fixup stmts that became noreturn calls. This may require splitting
4747 blocks and thus isn't possible during the dominator walk. Do this
4748 in reverse order so we don't inadvertedly remove a stmt we want to
4749 fixup by visiting a dominating now noreturn call first. */
4750 while (!el_to_fixup.is_empty ())
4751 {
4752 stmt = el_to_fixup.pop ();
4753
4754 if (dump_file && (dump_flags & TDF_DETAILS))
4755 {
4756 fprintf (dump_file, "Fixing up noreturn call ");
4757 print_gimple_stmt (dump_file, stmt, 0, 0);
4758 }
4759
4760 if (fixup_noreturn_call (stmt))
4761 el_todo |= TODO_cleanup_cfg;
4762 }
4763 el_to_fixup.release ();
4764
4765 return el_todo;
4766 }
4767
4768 /* Perform CFG cleanups made necessary by elimination. */
4769
4770 static unsigned
4771 fini_eliminate (void)
4772 {
4773 bool do_eh_cleanup = !bitmap_empty_p (need_eh_cleanup);
4774 bool do_ab_cleanup = !bitmap_empty_p (need_ab_cleanup);
4775
4776 if (do_eh_cleanup)
4777 gimple_purge_all_dead_eh_edges (need_eh_cleanup);
4778
4779 if (do_ab_cleanup)
4780 gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
4781
4782 BITMAP_FREE (need_eh_cleanup);
4783 BITMAP_FREE (need_ab_cleanup);
4784
4785 if (do_eh_cleanup || do_ab_cleanup)
4786 return TODO_cleanup_cfg;
4787 return 0;
4788 }
4789
4790 /* Borrow a bit of tree-ssa-dce.c for the moment.
4791 XXX: In 4.1, we should be able to just run a DCE pass after PRE, though
4792 this may be a bit faster, and we may want critical edges kept split. */
4793
4794 /* If OP's defining statement has not already been determined to be necessary,
4795 mark that statement necessary. Return the stmt, if it is newly
4796 necessary. */
4797
4798 static inline gimple *
4799 mark_operand_necessary (tree op)
4800 {
4801 gimple *stmt;
4802
4803 gcc_assert (op);
4804
4805 if (TREE_CODE (op) != SSA_NAME)
4806 return NULL;
4807
4808 stmt = SSA_NAME_DEF_STMT (op);
4809 gcc_assert (stmt);
4810
4811 if (gimple_plf (stmt, NECESSARY)
4812 || gimple_nop_p (stmt))
4813 return NULL;
4814
4815 gimple_set_plf (stmt, NECESSARY, true);
4816 return stmt;
4817 }
4818
4819 /* Because we don't follow exactly the standard PRE algorithm, and decide not
4820 to insert PHI nodes sometimes, and because value numbering of casts isn't
4821 perfect, we sometimes end up inserting dead code. This simple DCE-like
4822 pass removes any insertions we made that weren't actually used. */
4823
4824 static void
4825 remove_dead_inserted_code (void)
4826 {
4827 bitmap worklist;
4828 unsigned i;
4829 bitmap_iterator bi;
4830 gimple *t;
4831
4832 worklist = BITMAP_ALLOC (NULL);
4833 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4834 {
4835 t = SSA_NAME_DEF_STMT (ssa_name (i));
4836 if (gimple_plf (t, NECESSARY))
4837 bitmap_set_bit (worklist, i);
4838 }
4839 while (!bitmap_empty_p (worklist))
4840 {
4841 i = bitmap_first_set_bit (worklist);
4842 bitmap_clear_bit (worklist, i);
4843 t = SSA_NAME_DEF_STMT (ssa_name (i));
4844
4845 /* PHI nodes are somewhat special in that each PHI alternative has
4846 data and control dependencies. All the statements feeding the
4847 PHI node's arguments are always necessary. */
4848 if (gimple_code (t) == GIMPLE_PHI)
4849 {
4850 unsigned k;
4851
4852 for (k = 0; k < gimple_phi_num_args (t); k++)
4853 {
4854 tree arg = PHI_ARG_DEF (t, k);
4855 if (TREE_CODE (arg) == SSA_NAME)
4856 {
4857 gimple *n = mark_operand_necessary (arg);
4858 if (n)
4859 bitmap_set_bit (worklist, SSA_NAME_VERSION (arg));
4860 }
4861 }
4862 }
4863 else
4864 {
4865 /* Propagate through the operands. Examine all the USE, VUSE and
4866 VDEF operands in this statement. Mark all the statements
4867 which feed this statement's uses as necessary. */
4868 ssa_op_iter iter;
4869 tree use;
4870
4871 /* The operands of VDEF expressions are also needed as they
4872 represent potential definitions that may reach this
4873 statement (VDEF operands allow us to follow def-def
4874 links). */
4875
4876 FOR_EACH_SSA_TREE_OPERAND (use, t, iter, SSA_OP_ALL_USES)
4877 {
4878 gimple *n = mark_operand_necessary (use);
4879 if (n)
4880 bitmap_set_bit (worklist, SSA_NAME_VERSION (use));
4881 }
4882 }
4883 }
4884
4885 EXECUTE_IF_SET_IN_BITMAP (inserted_exprs, 0, i, bi)
4886 {
4887 t = SSA_NAME_DEF_STMT (ssa_name (i));
4888 if (!gimple_plf (t, NECESSARY))
4889 {
4890 gimple_stmt_iterator gsi;
4891
4892 if (dump_file && (dump_flags & TDF_DETAILS))
4893 {
4894 fprintf (dump_file, "Removing unnecessary insertion:");
4895 print_gimple_stmt (dump_file, t, 0, 0);
4896 }
4897
4898 gsi = gsi_for_stmt (t);
4899 if (gimple_code (t) == GIMPLE_PHI)
4900 remove_phi_node (&gsi, true);
4901 else
4902 {
4903 gsi_remove (&gsi, true);
4904 release_defs (t);
4905 }
4906 }
4907 }
4908 BITMAP_FREE (worklist);
4909 }
4910
4911
4912 /* Initialize data structures used by PRE. */
4913
4914 static void
4915 init_pre (void)
4916 {
4917 basic_block bb;
4918
4919 next_expression_id = 1;
4920 expressions.create (0);
4921 expressions.safe_push (NULL);
4922 value_expressions.create (get_max_value_id () + 1);
4923 value_expressions.safe_grow_cleared (get_max_value_id () + 1);
4924 name_to_id.create (0);
4925
4926 inserted_exprs = BITMAP_ALLOC (NULL);
4927
4928 connect_infinite_loops_to_exit ();
4929 memset (&pre_stats, 0, sizeof (pre_stats));
4930
4931 alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4932
4933 calculate_dominance_info (CDI_DOMINATORS);
4934
4935 bitmap_obstack_initialize (&grand_bitmap_obstack);
4936 phi_translate_table = new hash_table<expr_pred_trans_d> (5110);
4937 expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4938 FOR_ALL_BB_FN (bb, cfun)
4939 {
4940 EXP_GEN (bb) = bitmap_set_new ();
4941 PHI_GEN (bb) = bitmap_set_new ();
4942 TMP_GEN (bb) = bitmap_set_new ();
4943 AVAIL_OUT (bb) = bitmap_set_new ();
4944 }
4945 }
4946
4947
4948 /* Deallocate data structures used by PRE. */
4949
4950 static void
4951 fini_pre ()
4952 {
4953 value_expressions.release ();
4954 BITMAP_FREE (inserted_exprs);
4955 bitmap_obstack_release (&grand_bitmap_obstack);
4956 bitmap_set_pool.release ();
4957 pre_expr_pool.release ();
4958 delete phi_translate_table;
4959 phi_translate_table = NULL;
4960 delete expression_to_id;
4961 expression_to_id = NULL;
4962 name_to_id.release ();
4963
4964 free_aux_for_blocks ();
4965 }
4966
4967 namespace {
4968
4969 const pass_data pass_data_pre =
4970 {
4971 GIMPLE_PASS, /* type */
4972 "pre", /* name */
4973 OPTGROUP_NONE, /* optinfo_flags */
4974 TV_TREE_PRE, /* tv_id */
4975 /* PROP_no_crit_edges is ensured by placing pass_split_crit_edges before
4976 pass_pre. */
4977 ( PROP_no_crit_edges | PROP_cfg | PROP_ssa ), /* properties_required */
4978 0, /* properties_provided */
4979 PROP_no_crit_edges, /* properties_destroyed */
4980 TODO_rebuild_alias, /* todo_flags_start */
4981 0, /* todo_flags_finish */
4982 };
4983
4984 class pass_pre : public gimple_opt_pass
4985 {
4986 public:
4987 pass_pre (gcc::context *ctxt)
4988 : gimple_opt_pass (pass_data_pre, ctxt)
4989 {}
4990
4991 /* opt_pass methods: */
4992 virtual bool gate (function *)
4993 { return flag_tree_pre != 0 || flag_code_hoisting != 0; }
4994 virtual unsigned int execute (function *);
4995
4996 }; // class pass_pre
4997
4998 unsigned int
4999 pass_pre::execute (function *fun)
5000 {
5001 unsigned int todo = 0;
5002
5003 do_partial_partial =
5004 flag_tree_partial_pre && optimize_function_for_speed_p (fun);
5005
5006 /* This has to happen before SCCVN runs because
5007 loop_optimizer_init may create new phis, etc. */
5008 loop_optimizer_init (LOOPS_NORMAL);
5009
5010 if (!run_scc_vn (VN_WALK))
5011 {
5012 loop_optimizer_finalize ();
5013 return 0;
5014 }
5015
5016 init_pre ();
5017 scev_initialize ();
5018
5019 /* Collect and value number expressions computed in each basic block. */
5020 compute_avail ();
5021
5022 /* Insert can get quite slow on an incredibly large number of basic
5023 blocks due to some quadratic behavior. Until this behavior is
5024 fixed, don't run it when he have an incredibly large number of
5025 bb's. If we aren't going to run insert, there is no point in
5026 computing ANTIC, either, even though it's plenty fast. */
5027 if (n_basic_blocks_for_fn (fun) < 4000)
5028 {
5029 compute_antic ();
5030 insert ();
5031 }
5032
5033 /* Make sure to remove fake edges before committing our inserts.
5034 This makes sure we don't end up with extra critical edges that
5035 we would need to split. */
5036 remove_fake_exit_edges ();
5037 gsi_commit_edge_inserts ();
5038
5039 /* Eliminate folds statements which might (should not...) end up
5040 not keeping virtual operands up-to-date. */
5041 gcc_assert (!need_ssa_update_p (fun));
5042
5043 /* Remove all the redundant expressions. */
5044 todo |= eliminate (true);
5045
5046 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
5047 statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
5048 statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
5049 statistics_counter_event (fun, "New PHIs", pre_stats.phis);
5050 statistics_counter_event (fun, "Eliminated", pre_stats.eliminations);
5051
5052 clear_expression_ids ();
5053 remove_dead_inserted_code ();
5054
5055 scev_finalize ();
5056 fini_pre ();
5057 todo |= fini_eliminate ();
5058 loop_optimizer_finalize ();
5059
5060 /* Restore SSA info before tail-merging as that resets it as well. */
5061 scc_vn_restore_ssa_info ();
5062
5063 /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
5064 case we can merge the block with the remaining predecessor of the block.
5065 It should either:
5066 - call merge_blocks after each tail merge iteration
5067 - call merge_blocks after all tail merge iterations
5068 - mark TODO_cleanup_cfg when necessary
5069 - share the cfg cleanup with fini_pre. */
5070 todo |= tail_merge_optimize (todo);
5071
5072 free_scc_vn ();
5073
5074 /* Tail merging invalidates the virtual SSA web, together with
5075 cfg-cleanup opportunities exposed by PRE this will wreck the
5076 SSA updating machinery. So make sure to run update-ssa
5077 manually, before eventually scheduling cfg-cleanup as part of
5078 the todo. */
5079 update_ssa (TODO_update_ssa_only_virtuals);
5080
5081 return todo;
5082 }
5083
5084 } // anon namespace
5085
5086 gimple_opt_pass *
5087 make_pass_pre (gcc::context *ctxt)
5088 {
5089 return new pass_pre (ctxt);
5090 }
5091
5092 namespace {
5093
5094 const pass_data pass_data_fre =
5095 {
5096 GIMPLE_PASS, /* type */
5097 "fre", /* name */
5098 OPTGROUP_NONE, /* optinfo_flags */
5099 TV_TREE_FRE, /* tv_id */
5100 ( PROP_cfg | PROP_ssa ), /* properties_required */
5101 0, /* properties_provided */
5102 0, /* properties_destroyed */
5103 0, /* todo_flags_start */
5104 0, /* todo_flags_finish */
5105 };
5106
5107 class pass_fre : public gimple_opt_pass
5108 {
5109 public:
5110 pass_fre (gcc::context *ctxt)
5111 : gimple_opt_pass (pass_data_fre, ctxt)
5112 {}
5113
5114 /* opt_pass methods: */
5115 opt_pass * clone () { return new pass_fre (m_ctxt); }
5116 virtual bool gate (function *) { return flag_tree_fre != 0; }
5117 virtual unsigned int execute (function *);
5118
5119 }; // class pass_fre
5120
5121 unsigned int
5122 pass_fre::execute (function *fun)
5123 {
5124 unsigned int todo = 0;
5125
5126 if (!run_scc_vn (VN_WALKREWRITE))
5127 return 0;
5128
5129 memset (&pre_stats, 0, sizeof (pre_stats));
5130
5131 /* Remove all the redundant expressions. */
5132 todo |= eliminate (false);
5133
5134 todo |= fini_eliminate ();
5135
5136 scc_vn_restore_ssa_info ();
5137 free_scc_vn ();
5138
5139 statistics_counter_event (fun, "Insertions", pre_stats.insertions);
5140 statistics_counter_event (fun, "Eliminated", pre_stats.eliminations);
5141
5142 return todo;
5143 }
5144
5145 } // anon namespace
5146
5147 gimple_opt_pass *
5148 make_pass_fre (gcc::context *ctxt)
5149 {
5150 return new pass_fre (ctxt);
5151 }