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