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