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