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