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