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