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