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