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