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