Branch data Line data Source code
1 : : /* Full and partial redundancy elimination and code hoisting on SSA GIMPLE.
2 : : Copyright (C) 2001-2025 Free Software Foundation, Inc.
3 : : Contributed by Daniel Berlin <dan@dberlin.org> and Steven Bosscher
4 : : <stevenb@suse.de>
5 : :
6 : : This file is part of GCC.
7 : :
8 : : GCC is free software; you can redistribute it and/or modify
9 : : it under the terms of the GNU General Public License as published by
10 : : the Free Software Foundation; either version 3, or (at your option)
11 : : any later version.
12 : :
13 : : GCC is distributed in the hope that it will be useful,
14 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : : GNU General Public License for more details.
17 : :
18 : : You should have received a copy of the GNU General Public License
19 : : along with GCC; see the file COPYING3. If not see
20 : : <http://www.gnu.org/licenses/>. */
21 : :
22 : : #include "config.h"
23 : : #include "system.h"
24 : : #include "coretypes.h"
25 : : #include "backend.h"
26 : : #include "rtl.h"
27 : : #include "tree.h"
28 : : #include "gimple.h"
29 : : #include "predict.h"
30 : : #include "alloc-pool.h"
31 : : #include "tree-pass.h"
32 : : #include "ssa.h"
33 : : #include "cgraph.h"
34 : : #include "gimple-pretty-print.h"
35 : : #include "fold-const.h"
36 : : #include "cfganal.h"
37 : : #include "gimple-iterator.h"
38 : : #include "gimple-fold.h"
39 : : #include "tree-eh.h"
40 : : #include "gimplify.h"
41 : : #include "tree-cfg.h"
42 : : #include "tree-into-ssa.h"
43 : : #include "tree-dfa.h"
44 : : #include "tree-ssa.h"
45 : : #include "cfgloop.h"
46 : : #include "tree-ssa-sccvn.h"
47 : : #include "tree-scalar-evolution.h"
48 : : #include "dbgcnt.h"
49 : : #include "domwalk.h"
50 : : #include "tree-ssa-propagate.h"
51 : : #include "tree-ssa-dce.h"
52 : : #include "tree-cfgcleanup.h"
53 : : #include "alias.h"
54 : : #include "gimple-range.h"
55 : :
56 : : /* Even though this file is called tree-ssa-pre.cc, we actually
57 : : implement a bit more than just PRE here. All of them piggy-back
58 : : on GVN which is implemented in tree-ssa-sccvn.cc.
59 : :
60 : : 1. Full Redundancy Elimination (FRE)
61 : : This is the elimination phase of GVN.
62 : :
63 : : 2. Partial Redundancy Elimination (PRE)
64 : : This is adds computation of AVAIL_OUT and ANTIC_IN and
65 : : doing expression insertion to form GVN-PRE.
66 : :
67 : : 3. Code hoisting
68 : : This optimization uses the ANTIC_IN sets computed for PRE
69 : : to move expressions further up than PRE would do, to make
70 : : multiple computations of the same value fully redundant.
71 : : This pass is explained below (after the explanation of the
72 : : basic algorithm for PRE).
73 : : */
74 : :
75 : : /* TODO:
76 : :
77 : : 1. Avail sets can be shared by making an avail_find_leader that
78 : : walks up the dominator tree and looks in those avail sets.
79 : : This might affect code optimality, it's unclear right now.
80 : : Currently the AVAIL_OUT sets are the remaining quadraticness in
81 : : memory of GVN-PRE.
82 : : 2. Strength reduction can be performed by anticipating expressions
83 : : we can repair later on.
84 : : 3. We can do back-substitution or smarter value numbering to catch
85 : : commutative expressions split up over multiple statements.
86 : : */
87 : :
88 : : /* For ease of terminology, "expression node" in the below refers to
89 : : every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
90 : : represent the actual statement containing the expressions we care about,
91 : : and we cache the value number by putting it in the expression. */
92 : :
93 : : /* Basic algorithm for Partial Redundancy Elimination:
94 : :
95 : : First we walk the statements to generate the AVAIL sets, the
96 : : EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
97 : : generation of values/expressions by a given block. We use them
98 : : when computing the ANTIC sets. The AVAIL sets consist of
99 : : SSA_NAME's that represent values, so we know what values are
100 : : available in what blocks. AVAIL is a forward dataflow problem. In
101 : : SSA, values are never killed, so we don't need a kill set, or a
102 : : fixpoint iteration, in order to calculate the AVAIL sets. In
103 : : traditional parlance, AVAIL sets tell us the downsafety of the
104 : : expressions/values.
105 : :
106 : : Next, we generate the ANTIC sets. These sets represent the
107 : : anticipatable expressions. ANTIC is a backwards dataflow
108 : : problem. An expression is anticipatable in a given block if it could
109 : : be generated in that block. This means that if we had to perform
110 : : an insertion in that block, of the value of that expression, we
111 : : could. Calculating the ANTIC sets requires phi translation of
112 : : expressions, because the flow goes backwards through phis. We must
113 : : iterate to a fixpoint of the ANTIC sets, because we have a kill
114 : : set. Even in SSA form, values are not live over the entire
115 : : function, only from their definition point onwards. So we have to
116 : : remove values from the ANTIC set once we go past the definition
117 : : point of the leaders that make them up.
118 : : compute_antic/compute_antic_aux performs this computation.
119 : :
120 : : Third, we perform insertions to make partially redundant
121 : : expressions fully redundant.
122 : :
123 : : An expression is partially redundant (excluding partial
124 : : anticipation) if:
125 : :
126 : : 1. It is AVAIL in some, but not all, of the predecessors of a
127 : : given block.
128 : : 2. It is ANTIC in all the predecessors.
129 : :
130 : : In order to make it fully redundant, we insert the expression into
131 : : the predecessors where it is not available, but is ANTIC.
132 : :
133 : : When optimizing for size, we only eliminate the partial redundancy
134 : : if we need to insert in only one predecessor. This avoids almost
135 : : completely the code size increase that PRE usually causes.
136 : :
137 : : For the partial anticipation case, we only perform insertion if it
138 : : is partially anticipated in some block, and fully available in all
139 : : of the predecessors.
140 : :
141 : : do_pre_regular_insertion/do_pre_partial_partial_insertion
142 : : performs these steps, driven by insert/insert_aux.
143 : :
144 : : Fourth, we eliminate fully redundant expressions.
145 : : This is a simple statement walk that replaces redundant
146 : : calculations with the now available values. */
147 : :
148 : : /* Basic algorithm for Code Hoisting:
149 : :
150 : : Code hoisting is: Moving value computations up in the control flow
151 : : graph to make multiple copies redundant. Typically this is a size
152 : : optimization, but there are cases where it also is helpful for speed.
153 : :
154 : : A simple code hoisting algorithm is implemented that piggy-backs on
155 : : the PRE infrastructure. For code hoisting, we have to know ANTIC_OUT
156 : : which is effectively ANTIC_IN - AVAIL_OUT. The latter two have to be
157 : : computed for PRE, and we can use them to perform a limited version of
158 : : code hoisting, too.
159 : :
160 : : For the purpose of this implementation, a value is hoistable to a basic
161 : : block B if the following properties are met:
162 : :
163 : : 1. The value is in ANTIC_IN(B) -- the value will be computed on all
164 : : paths from B to function exit and it can be computed in B);
165 : :
166 : : 2. The value is not in AVAIL_OUT(B) -- there would be no need to
167 : : compute the value again and make it available twice;
168 : :
169 : : 3. All successors of B are dominated by B -- makes sure that inserting
170 : : a computation of the value in B will make the remaining
171 : : computations fully redundant;
172 : :
173 : : 4. At least one successor has the value in AVAIL_OUT -- to avoid
174 : : hoisting values up too far;
175 : :
176 : : 5. There are at least two successors of B -- hoisting in straight
177 : : line code is pointless.
178 : :
179 : : The third condition is not strictly necessary, but it would complicate
180 : : the hoisting pass a lot. In fact, I don't know of any code hoisting
181 : : algorithm that does not have this requirement. Fortunately, experiments
182 : : have show that most candidate hoistable values are in regions that meet
183 : : this condition (e.g. diamond-shape regions).
184 : :
185 : : The forth condition is necessary to avoid hoisting things up too far
186 : : away from the uses of the value. Nothing else limits the algorithm
187 : : from hoisting everything up as far as ANTIC_IN allows. Experiments
188 : : with SPEC and CSiBE have shown that hoisting up too far results in more
189 : : spilling, less benefits for code size, and worse benchmark scores.
190 : : Fortunately, in practice most of the interesting hoisting opportunities
191 : : are caught despite this limitation.
192 : :
193 : : For hoistable values that meet all conditions, expressions are inserted
194 : : to make the calculation of the hoistable value fully redundant. We
195 : : perform code hoisting insertions after each round of PRE insertions,
196 : : because code hoisting never exposes new PRE opportunities, but PRE can
197 : : create new code hoisting opportunities.
198 : :
199 : : The code hoisting algorithm is implemented in do_hoist_insert, driven
200 : : by insert/insert_aux. */
201 : :
202 : : /* Representations of value numbers:
203 : :
204 : : Value numbers are represented by a representative SSA_NAME. We
205 : : will create fake SSA_NAME's in situations where we need a
206 : : representative but do not have one (because it is a complex
207 : : expression). In order to facilitate storing the value numbers in
208 : : bitmaps, and keep the number of wasted SSA_NAME's down, we also
209 : : associate a value_id with each value number, and create full blown
210 : : ssa_name's only where we actually need them (IE in operands of
211 : : existing expressions).
212 : :
213 : : Theoretically you could replace all the value_id's with
214 : : SSA_NAME_VERSION, but this would allocate a large number of
215 : : SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
216 : : It would also require an additional indirection at each point we
217 : : use the value id. */
218 : :
219 : : /* Representation of expressions on value numbers:
220 : :
221 : : Expressions consisting of value numbers are represented the same
222 : : way as our VN internally represents them, with an additional
223 : : "pre_expr" wrapping around them in order to facilitate storing all
224 : : of the expressions in the same sets. */
225 : :
226 : : /* Representation of sets:
227 : :
228 : : The dataflow sets do not need to be sorted in any particular order
229 : : for the majority of their lifetime, are simply represented as two
230 : : bitmaps, one that keeps track of values present in the set, and one
231 : : that keeps track of expressions present in the set.
232 : :
233 : : When we need them in topological order, we produce it on demand by
234 : : transforming the bitmap into an array and sorting it into topo
235 : : order. */
236 : :
237 : : /* Type of expression, used to know which member of the PRE_EXPR union
238 : : is valid. */
239 : :
240 : : enum pre_expr_kind
241 : : {
242 : : NAME,
243 : : NARY,
244 : : REFERENCE,
245 : : CONSTANT
246 : : };
247 : :
248 : : union pre_expr_union
249 : : {
250 : : tree name;
251 : : tree constant;
252 : : vn_nary_op_t nary;
253 : : vn_reference_t reference;
254 : : };
255 : :
256 : : typedef struct pre_expr_d : nofree_ptr_hash <pre_expr_d>
257 : : {
258 : : enum pre_expr_kind kind;
259 : : unsigned int id;
260 : : unsigned value_id;
261 : : location_t loc;
262 : : pre_expr_union u;
263 : :
264 : : /* hash_table support. */
265 : : static inline hashval_t hash (const pre_expr_d *);
266 : : static inline int equal (const pre_expr_d *, const pre_expr_d *);
267 : : } *pre_expr;
268 : :
269 : : #define PRE_EXPR_NAME(e) (e)->u.name
270 : : #define PRE_EXPR_NARY(e) (e)->u.nary
271 : : #define PRE_EXPR_REFERENCE(e) (e)->u.reference
272 : : #define PRE_EXPR_CONSTANT(e) (e)->u.constant
273 : :
274 : : /* Compare E1 and E1 for equality. */
275 : :
276 : : inline int
277 : 56794809 : pre_expr_d::equal (const pre_expr_d *e1, const pre_expr_d *e2)
278 : : {
279 : 56794809 : if (e1->kind != e2->kind)
280 : : return false;
281 : :
282 : 35815430 : switch (e1->kind)
283 : : {
284 : 4484132 : case CONSTANT:
285 : 4484132 : return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
286 : 4484132 : PRE_EXPR_CONSTANT (e2));
287 : 151968 : case NAME:
288 : 151968 : return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
289 : 22522574 : case NARY:
290 : 22522574 : return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
291 : 8656756 : case REFERENCE:
292 : 8656756 : return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
293 : 8656756 : PRE_EXPR_REFERENCE (e2));
294 : 0 : default:
295 : 0 : gcc_unreachable ();
296 : : }
297 : : }
298 : :
299 : : /* Hash E. */
300 : :
301 : : inline hashval_t
302 : 90578006 : pre_expr_d::hash (const pre_expr_d *e)
303 : : {
304 : 90578006 : switch (e->kind)
305 : : {
306 : 6860812 : case CONSTANT:
307 : 6860812 : return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
308 : 0 : case NAME:
309 : 0 : return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
310 : 56050126 : case NARY:
311 : 56050126 : return PRE_EXPR_NARY (e)->hashcode;
312 : 27667068 : case REFERENCE:
313 : 27667068 : return PRE_EXPR_REFERENCE (e)->hashcode;
314 : 0 : default:
315 : 0 : gcc_unreachable ();
316 : : }
317 : : }
318 : :
319 : : /* Next global expression id number. */
320 : : static unsigned int next_expression_id;
321 : :
322 : : /* Mapping from expression to id number we can use in bitmap sets. */
323 : : static vec<pre_expr> expressions;
324 : : static hash_table<pre_expr_d> *expression_to_id;
325 : : static vec<unsigned> name_to_id;
326 : : static obstack pre_expr_obstack;
327 : :
328 : : /* Allocate an expression id for EXPR. */
329 : :
330 : : static inline unsigned int
331 : 43229491 : alloc_expression_id (pre_expr expr)
332 : : {
333 : 43229491 : struct pre_expr_d **slot;
334 : : /* Make sure we won't overflow. */
335 : 43229491 : gcc_assert (next_expression_id + 1 > next_expression_id);
336 : 43229491 : expr->id = next_expression_id++;
337 : 43229491 : expressions.safe_push (expr);
338 : 43229491 : if (expr->kind == NAME)
339 : : {
340 : 23691945 : unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
341 : : /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
342 : : re-allocations by using vec::reserve upfront. */
343 : 23691945 : unsigned old_len = name_to_id.length ();
344 : 47383890 : name_to_id.reserve (num_ssa_names - old_len);
345 : 47383890 : name_to_id.quick_grow_cleared (num_ssa_names);
346 : 23691945 : gcc_assert (name_to_id[version] == 0);
347 : 23691945 : name_to_id[version] = expr->id;
348 : : }
349 : : else
350 : : {
351 : 19537546 : slot = expression_to_id->find_slot (expr, INSERT);
352 : 19537546 : gcc_assert (!*slot);
353 : 19537546 : *slot = expr;
354 : : }
355 : 43229491 : return next_expression_id - 1;
356 : : }
357 : :
358 : : /* Return the expression id for tree EXPR. */
359 : :
360 : : static inline unsigned int
361 : 248819137 : get_expression_id (const pre_expr expr)
362 : : {
363 : 248819137 : return expr->id;
364 : : }
365 : :
366 : : static inline unsigned int
367 : 77600543 : lookup_expression_id (const pre_expr expr)
368 : : {
369 : 77600543 : struct pre_expr_d **slot;
370 : :
371 : 77600543 : if (expr->kind == NAME)
372 : : {
373 : 52289384 : unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
374 : 71826930 : if (name_to_id.length () <= version)
375 : : return 0;
376 : 49468493 : return name_to_id[version];
377 : : }
378 : : else
379 : : {
380 : 25311159 : slot = expression_to_id->find_slot (expr, NO_INSERT);
381 : 25311159 : if (!slot)
382 : : return 0;
383 : 5773613 : return ((pre_expr)*slot)->id;
384 : : }
385 : : }
386 : :
387 : : /* Return the expression that has expression id ID */
388 : :
389 : : static inline pre_expr
390 : 497787865 : expression_for_id (unsigned int id)
391 : : {
392 : 995575730 : return expressions[id];
393 : : }
394 : :
395 : : static object_allocator<pre_expr_d> pre_expr_pool ("pre_expr nodes");
396 : :
397 : : /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
398 : :
399 : : static pre_expr
400 : 52289384 : get_or_alloc_expr_for_name (tree name)
401 : : {
402 : 52289384 : struct pre_expr_d expr;
403 : 52289384 : pre_expr result;
404 : 52289384 : unsigned int result_id;
405 : :
406 : 52289384 : expr.kind = NAME;
407 : 52289384 : expr.id = 0;
408 : 52289384 : PRE_EXPR_NAME (&expr) = name;
409 : 52289384 : result_id = lookup_expression_id (&expr);
410 : 52289384 : if (result_id != 0)
411 : 28597439 : return expression_for_id (result_id);
412 : :
413 : 23691945 : result = pre_expr_pool.allocate ();
414 : 23691945 : result->kind = NAME;
415 : 23691945 : result->loc = UNKNOWN_LOCATION;
416 : 23691945 : result->value_id = VN_INFO (name)->value_id;
417 : 23691945 : PRE_EXPR_NAME (result) = name;
418 : 23691945 : alloc_expression_id (result);
419 : 23691945 : return result;
420 : : }
421 : :
422 : : /* Given an NARY, get or create a pre_expr to represent it. Assign
423 : : VALUE_ID to it or allocate a new value-id if it is zero. Record
424 : : LOC as the original location of the expression. */
425 : :
426 : : static pre_expr
427 : 13496955 : get_or_alloc_expr_for_nary (vn_nary_op_t nary, unsigned value_id,
428 : : location_t loc = UNKNOWN_LOCATION)
429 : : {
430 : 13496955 : struct pre_expr_d expr;
431 : 13496955 : pre_expr result;
432 : 13496955 : unsigned int result_id;
433 : :
434 : 13496955 : gcc_assert (value_id == 0 || !value_id_constant_p (value_id));
435 : :
436 : 13496955 : expr.kind = NARY;
437 : 13496955 : expr.id = 0;
438 : 13496955 : nary->hashcode = vn_nary_op_compute_hash (nary);
439 : 13496955 : PRE_EXPR_NARY (&expr) = nary;
440 : 13496955 : result_id = lookup_expression_id (&expr);
441 : 13496955 : if (result_id != 0)
442 : 996122 : return expression_for_id (result_id);
443 : :
444 : 12500833 : result = pre_expr_pool.allocate ();
445 : 12500833 : result->kind = NARY;
446 : 12500833 : result->loc = loc;
447 : 12500833 : result->value_id = value_id ? value_id : get_next_value_id ();
448 : 12500833 : PRE_EXPR_NARY (result)
449 : 12500833 : = alloc_vn_nary_op_noinit (nary->length, &pre_expr_obstack);
450 : 12500833 : memcpy (PRE_EXPR_NARY (result), nary, sizeof_vn_nary_op (nary->length));
451 : 12500833 : alloc_expression_id (result);
452 : 12500833 : return result;
453 : : }
454 : :
455 : : /* Given an REFERENCE, get or create a pre_expr to represent it. */
456 : :
457 : : static pre_expr
458 : 7038501 : get_or_alloc_expr_for_reference (vn_reference_t reference,
459 : : location_t loc = UNKNOWN_LOCATION)
460 : : {
461 : 7038501 : struct pre_expr_d expr;
462 : 7038501 : pre_expr result;
463 : 7038501 : unsigned int result_id;
464 : :
465 : 7038501 : expr.kind = REFERENCE;
466 : 7038501 : expr.id = 0;
467 : 7038501 : PRE_EXPR_REFERENCE (&expr) = reference;
468 : 7038501 : result_id = lookup_expression_id (&expr);
469 : 7038501 : if (result_id != 0)
470 : 816441 : return expression_for_id (result_id);
471 : :
472 : 6222060 : result = pre_expr_pool.allocate ();
473 : 6222060 : result->kind = REFERENCE;
474 : 6222060 : result->loc = loc;
475 : 6222060 : result->value_id = reference->value_id;
476 : 6222060 : PRE_EXPR_REFERENCE (result) = reference;
477 : 6222060 : alloc_expression_id (result);
478 : 6222060 : return result;
479 : : }
480 : :
481 : :
482 : : /* An unordered bitmap set. One bitmap tracks values, the other,
483 : : expressions. */
484 : 145422268 : typedef class bitmap_set
485 : : {
486 : : public:
487 : : bitmap_head expressions;
488 : : bitmap_head values;
489 : : } *bitmap_set_t;
490 : :
491 : : #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
492 : : EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
493 : :
494 : : #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
495 : : EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
496 : :
497 : : /* Mapping from value id to expressions with that value_id. */
498 : : static vec<bitmap> value_expressions;
499 : : /* We just record a single expression for each constant value,
500 : : one of kind CONSTANT. */
501 : : static vec<pre_expr> constant_value_expressions;
502 : :
503 : :
504 : : /* This structure is used to keep track of statistics on what
505 : : optimization PRE was able to perform. */
506 : : static struct
507 : : {
508 : : /* The number of new expressions/temporaries generated by PRE. */
509 : : int insertions;
510 : :
511 : : /* The number of inserts found due to partial anticipation */
512 : : int pa_insert;
513 : :
514 : : /* The number of inserts made for code hoisting. */
515 : : int hoist_insert;
516 : :
517 : : /* The number of new PHI nodes added by PRE. */
518 : : int phis;
519 : : } pre_stats;
520 : :
521 : : static bool do_partial_partial;
522 : : static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
523 : : static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
524 : : static bool bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
525 : : static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
526 : : static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
527 : : static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
528 : : static bitmap_set_t bitmap_set_new (void);
529 : : static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
530 : : tree);
531 : : static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
532 : : static unsigned int get_expr_value_id (pre_expr);
533 : :
534 : : /* We can add and remove elements and entries to and from sets
535 : : and hash tables, so we use alloc pools for them. */
536 : :
537 : : static object_allocator<bitmap_set> bitmap_set_pool ("Bitmap sets");
538 : : static bitmap_obstack grand_bitmap_obstack;
539 : :
540 : : /* A three tuple {e, pred, v} used to cache phi translations in the
541 : : phi_translate_table. */
542 : :
543 : : typedef struct expr_pred_trans_d : public typed_noop_remove <expr_pred_trans_d>
544 : : {
545 : : typedef expr_pred_trans_d value_type;
546 : : typedef expr_pred_trans_d compare_type;
547 : :
548 : : /* The expression ID. */
549 : : unsigned e;
550 : :
551 : : /* The value expression ID that resulted from the translation. */
552 : : unsigned v;
553 : :
554 : : /* hash_table support. */
555 : : static inline void mark_empty (expr_pred_trans_d &);
556 : : static inline bool is_empty (const expr_pred_trans_d &);
557 : : static inline void mark_deleted (expr_pred_trans_d &);
558 : : static inline bool is_deleted (const expr_pred_trans_d &);
559 : : static const bool empty_zero_p = true;
560 : : static inline hashval_t hash (const expr_pred_trans_d &);
561 : : static inline int equal (const expr_pred_trans_d &, const expr_pred_trans_d &);
562 : : } *expr_pred_trans_t;
563 : : typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
564 : :
565 : : inline bool
566 : 1324055590 : expr_pred_trans_d::is_empty (const expr_pred_trans_d &e)
567 : : {
568 : 1324055590 : return e.e == 0;
569 : : }
570 : :
571 : : inline bool
572 : 257914702 : expr_pred_trans_d::is_deleted (const expr_pred_trans_d &e)
573 : : {
574 : 257914702 : return e.e == -1u;
575 : : }
576 : :
577 : : inline void
578 : 1942164 : expr_pred_trans_d::mark_empty (expr_pred_trans_d &e)
579 : : {
580 : 1942164 : e.e = 0;
581 : : }
582 : :
583 : : inline void
584 : 3417049 : expr_pred_trans_d::mark_deleted (expr_pred_trans_d &e)
585 : : {
586 : 3417049 : e.e = -1u;
587 : : }
588 : :
589 : : inline hashval_t
590 : : expr_pred_trans_d::hash (const expr_pred_trans_d &e)
591 : : {
592 : : return e.e;
593 : : }
594 : :
595 : : inline int
596 : 201880971 : expr_pred_trans_d::equal (const expr_pred_trans_d &ve1,
597 : : const expr_pred_trans_d &ve2)
598 : : {
599 : 201880971 : return ve1.e == ve2.e;
600 : : }
601 : :
602 : : /* Sets that we need to keep track of. */
603 : : typedef struct bb_bitmap_sets
604 : : {
605 : : /* The EXP_GEN set, which represents expressions/values generated in
606 : : a basic block. */
607 : : bitmap_set_t exp_gen;
608 : :
609 : : /* The PHI_GEN set, which represents PHI results generated in a
610 : : basic block. */
611 : : bitmap_set_t phi_gen;
612 : :
613 : : /* The TMP_GEN set, which represents results/temporaries generated
614 : : in a basic block. IE the LHS of an expression. */
615 : : bitmap_set_t tmp_gen;
616 : :
617 : : /* The AVAIL_OUT set, which represents which values are available in
618 : : a given basic block. */
619 : : bitmap_set_t avail_out;
620 : :
621 : : /* The ANTIC_IN set, which represents which values are anticipatable
622 : : in a given basic block. */
623 : : bitmap_set_t antic_in;
624 : :
625 : : /* The PA_IN set, which represents which values are
626 : : partially anticipatable in a given basic block. */
627 : : bitmap_set_t pa_in;
628 : :
629 : : /* The NEW_SETS set, which is used during insertion to augment the
630 : : AVAIL_OUT set of blocks with the new insertions performed during
631 : : the current iteration. */
632 : : bitmap_set_t new_sets;
633 : :
634 : : /* A cache for value_dies_in_block_x. */
635 : : bitmap expr_dies;
636 : :
637 : : /* The live virtual operand on successor edges. */
638 : : tree vop_on_exit;
639 : :
640 : : /* PHI translate cache for the single successor edge. */
641 : : hash_table<expr_pred_trans_d> *phi_translate_table;
642 : :
643 : : /* True if we have visited this block during ANTIC calculation. */
644 : : unsigned int visited : 1;
645 : :
646 : : /* True when the block contains a call that might not return. */
647 : : unsigned int contains_may_not_return_call : 1;
648 : : } *bb_value_sets_t;
649 : :
650 : : #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
651 : : #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
652 : : #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
653 : : #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
654 : : #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
655 : : #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
656 : : #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
657 : : #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
658 : : #define PHI_TRANS_TABLE(BB) ((bb_value_sets_t) ((BB)->aux))->phi_translate_table
659 : : #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
660 : : #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
661 : : #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
662 : :
663 : :
664 : : /* Add the tuple mapping from {expression E, basic block PRED} to
665 : : the phi translation table and return whether it pre-existed. */
666 : :
667 : : static inline bool
668 : 82702168 : phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
669 : : {
670 : 82702168 : if (!PHI_TRANS_TABLE (pred))
671 : 177474 : PHI_TRANS_TABLE (pred) = new hash_table<expr_pred_trans_d> (11);
672 : :
673 : 82702168 : expr_pred_trans_t slot;
674 : 82702168 : expr_pred_trans_d tem;
675 : 82702168 : unsigned id = get_expression_id (e);
676 : 82702168 : tem.e = id;
677 : 82702168 : slot = PHI_TRANS_TABLE (pred)->find_slot_with_hash (tem, id, INSERT);
678 : 82702168 : if (slot->e)
679 : : {
680 : 60227419 : *entry = slot;
681 : 60227419 : return true;
682 : : }
683 : :
684 : 22474749 : *entry = slot;
685 : 22474749 : slot->e = id;
686 : 22474749 : return false;
687 : : }
688 : :
689 : :
690 : : /* Add expression E to the expression set of value id V. */
691 : :
692 : : static void
693 : 45042054 : add_to_value (unsigned int v, pre_expr e)
694 : : {
695 : 0 : gcc_checking_assert (get_expr_value_id (e) == v);
696 : :
697 : 45042054 : if (value_id_constant_p (v))
698 : : {
699 : 862750 : if (e->kind != CONSTANT)
700 : : return;
701 : :
702 : 814653 : if (-v >= constant_value_expressions.length ())
703 : 484612 : constant_value_expressions.safe_grow_cleared (-v + 1);
704 : :
705 : 814653 : pre_expr leader = constant_value_expressions[-v];
706 : 814653 : if (!leader)
707 : 814653 : constant_value_expressions[-v] = e;
708 : : }
709 : : else
710 : : {
711 : 44179304 : if (v >= value_expressions.length ())
712 : 6736644 : value_expressions.safe_grow_cleared (v + 1);
713 : :
714 : 44179304 : bitmap set = value_expressions[v];
715 : 44179304 : if (!set)
716 : : {
717 : 24528465 : set = BITMAP_ALLOC (&grand_bitmap_obstack);
718 : 24528465 : value_expressions[v] = set;
719 : : }
720 : 44179304 : bitmap_set_bit (set, get_expression_id (e));
721 : : }
722 : : }
723 : :
724 : : /* Create a new bitmap set and return it. */
725 : :
726 : : static bitmap_set_t
727 : 145422268 : bitmap_set_new (void)
728 : : {
729 : 145422268 : bitmap_set_t ret = bitmap_set_pool.allocate ();
730 : 145422268 : bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
731 : 145422268 : bitmap_initialize (&ret->values, &grand_bitmap_obstack);
732 : 145422268 : return ret;
733 : : }
734 : :
735 : : /* Return the value id for a PRE expression EXPR. */
736 : :
737 : : static unsigned int
738 : 518585833 : get_expr_value_id (pre_expr expr)
739 : : {
740 : : /* ??? We cannot assert that expr has a value-id (it can be 0), because
741 : : we assign value-ids only to expressions that have a result
742 : : in set_hashtable_value_ids. */
743 : 45042054 : return expr->value_id;
744 : : }
745 : :
746 : : /* Return a VN valnum (SSA name or constant) for the PRE value-id VAL. */
747 : :
748 : : static tree
749 : 1244942 : vn_valnum_from_value_id (unsigned int val)
750 : : {
751 : 1244942 : if (value_id_constant_p (val))
752 : : {
753 : 0 : pre_expr vexpr = constant_value_expressions[-val];
754 : 0 : if (vexpr)
755 : 0 : return PRE_EXPR_CONSTANT (vexpr);
756 : : return NULL_TREE;
757 : : }
758 : :
759 : 1244942 : bitmap exprset = value_expressions[val];
760 : 1244942 : bitmap_iterator bi;
761 : 1244942 : unsigned int i;
762 : 1846109 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
763 : : {
764 : 1512027 : pre_expr vexpr = expression_for_id (i);
765 : 1512027 : if (vexpr->kind == NAME)
766 : 910860 : return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
767 : : }
768 : : return NULL_TREE;
769 : : }
770 : :
771 : : /* Insert an expression EXPR into a bitmapped set. */
772 : :
773 : : static void
774 : 72405906 : bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
775 : : {
776 : 72405906 : unsigned int val = get_expr_value_id (expr);
777 : 72405906 : if (! value_id_constant_p (val))
778 : : {
779 : : /* Note this is the only function causing multiple expressions
780 : : for the same value to appear in a set. This is needed for
781 : : TMP_GEN, PHI_GEN and NEW_SETs. */
782 : 69765418 : bitmap_set_bit (&set->values, val);
783 : 69765418 : bitmap_set_bit (&set->expressions, get_expression_id (expr));
784 : : }
785 : 72405906 : }
786 : :
787 : : /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
788 : :
789 : : static void
790 : 26942310 : bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
791 : : {
792 : 26942310 : bitmap_copy (&dest->expressions, &orig->expressions);
793 : 26942310 : bitmap_copy (&dest->values, &orig->values);
794 : 26942310 : }
795 : :
796 : :
797 : : /* Free memory used up by SET. */
798 : : static void
799 : 72363839 : bitmap_set_free (bitmap_set_t set)
800 : : {
801 : 0 : bitmap_clear (&set->expressions);
802 : 19375909 : bitmap_clear (&set->values);
803 : 48873597 : }
804 : :
805 : : static void
806 : : pre_expr_DFS (pre_expr expr, bitmap_set_t set, bitmap val_visited,
807 : : vec<pre_expr> &post);
808 : :
809 : : /* DFS walk leaders of VAL to their operands with leaders in SET, collecting
810 : : expressions in SET in postorder into POST. */
811 : :
812 : : static void
813 : 82877716 : pre_expr_DFS (unsigned val, bitmap_set_t set, bitmap val_visited,
814 : : vec<pre_expr> &post)
815 : : {
816 : 82877716 : unsigned int i;
817 : 82877716 : bitmap_iterator bi;
818 : :
819 : : /* Iterate over all leaders and DFS recurse. Borrowed from
820 : : bitmap_find_leader. */
821 : 82877716 : bitmap exprset = value_expressions[val];
822 : 82877716 : if (!exprset->first->next)
823 : : {
824 : 197249472 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
825 : 125171610 : if (bitmap_bit_p (&set->expressions, i))
826 : 72253354 : pre_expr_DFS (expression_for_id (i), set, val_visited, post);
827 : 72077862 : return;
828 : : }
829 : :
830 : 21830340 : EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
831 : 11030486 : pre_expr_DFS (expression_for_id (i), set, val_visited, post);
832 : : }
833 : :
834 : : /* DFS walk EXPR to its operands with leaders in SET, collecting
835 : : expressions in SET in postorder into POST. */
836 : :
837 : : static void
838 : 83283840 : pre_expr_DFS (pre_expr expr, bitmap_set_t set, bitmap val_visited,
839 : : vec<pre_expr> &post)
840 : : {
841 : 83283840 : switch (expr->kind)
842 : : {
843 : 38477667 : case NARY:
844 : 38477667 : {
845 : 38477667 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
846 : 103423880 : for (unsigned i = 0; i < nary->length; i++)
847 : : {
848 : 64946213 : if (TREE_CODE (nary->op[i]) != SSA_NAME)
849 : 19913698 : continue;
850 : 45032515 : unsigned int op_val_id = VN_INFO (nary->op[i])->value_id;
851 : : /* If we already found a leader for the value we've
852 : : recursed already. Avoid the costly bitmap_find_leader. */
853 : 45032515 : if (bitmap_bit_p (&set->values, op_val_id)
854 : 45032515 : && bitmap_set_bit (val_visited, op_val_id))
855 : 8011858 : pre_expr_DFS (op_val_id, set, val_visited, post);
856 : : }
857 : : break;
858 : : }
859 : 12722924 : case REFERENCE:
860 : 12722924 : {
861 : 12722924 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
862 : 12722924 : vec<vn_reference_op_s> operands = ref->operands;
863 : 12722924 : vn_reference_op_t operand;
864 : 50305984 : for (unsigned i = 0; operands.iterate (i, &operand); i++)
865 : : {
866 : 37583060 : tree op[3];
867 : 37583060 : op[0] = operand->op0;
868 : 37583060 : op[1] = operand->op1;
869 : 37583060 : op[2] = operand->op2;
870 : 150332240 : for (unsigned n = 0; n < 3; ++n)
871 : : {
872 : 112749180 : if (!op[n] || TREE_CODE (op[n]) != SSA_NAME)
873 : 104287396 : continue;
874 : 8461784 : unsigned op_val_id = VN_INFO (op[n])->value_id;
875 : 8461784 : if (bitmap_bit_p (&set->values, op_val_id)
876 : 8461784 : && bitmap_set_bit (val_visited, op_val_id))
877 : 1446321 : pre_expr_DFS (op_val_id, set, val_visited, post);
878 : : }
879 : : }
880 : : break;
881 : : }
882 : 83283840 : default:;
883 : : }
884 : 83283840 : post.quick_push (expr);
885 : 83283840 : }
886 : :
887 : : /* Generate an topological-ordered array of bitmap set SET. */
888 : :
889 : : static vec<pre_expr>
890 : 18274942 : sorted_array_from_bitmap_set (bitmap_set_t set)
891 : : {
892 : 18274942 : unsigned int i;
893 : 18274942 : bitmap_iterator bi;
894 : 18274942 : vec<pre_expr> result;
895 : :
896 : : /* Pre-allocate enough space for the array. */
897 : 18274942 : result.create (bitmap_count_bits (&set->expressions));
898 : :
899 : 18274942 : auto_bitmap val_visited (&grand_bitmap_obstack);
900 : 18274942 : bitmap_tree_view (val_visited);
901 : 101152658 : FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
902 : 82877716 : if (bitmap_set_bit (val_visited, i))
903 : 73419537 : pre_expr_DFS (i, set, val_visited, result);
904 : :
905 : 18274942 : return result;
906 : 18274942 : }
907 : :
908 : : /* Subtract all expressions contained in ORIG from DEST. */
909 : :
910 : : static bitmap_set_t
911 : 32205997 : bitmap_set_subtract_expressions (bitmap_set_t dest, bitmap_set_t orig)
912 : : {
913 : 32205997 : bitmap_set_t result = bitmap_set_new ();
914 : 32205997 : bitmap_iterator bi;
915 : 32205997 : unsigned int i;
916 : :
917 : 32205997 : bitmap_and_compl (&result->expressions, &dest->expressions,
918 : 32205997 : &orig->expressions);
919 : :
920 : 110045625 : FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
921 : : {
922 : 77839628 : pre_expr expr = expression_for_id (i);
923 : 77839628 : unsigned int value_id = get_expr_value_id (expr);
924 : 77839628 : bitmap_set_bit (&result->values, value_id);
925 : : }
926 : :
927 : 32205997 : return result;
928 : : }
929 : :
930 : : /* Subtract all values in bitmap set B from bitmap set A. */
931 : :
932 : : static void
933 : 1123295 : bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
934 : : {
935 : 1123295 : unsigned int i;
936 : 1123295 : bitmap_iterator bi;
937 : 1123295 : unsigned to_remove = -1U;
938 : 1123295 : bitmap_and_compl_into (&a->values, &b->values);
939 : 10674890 : FOR_EACH_EXPR_ID_IN_SET (a, i, bi)
940 : : {
941 : 9551595 : if (to_remove != -1U)
942 : : {
943 : 1329046 : bitmap_clear_bit (&a->expressions, to_remove);
944 : 1329046 : to_remove = -1U;
945 : : }
946 : 9551595 : pre_expr expr = expression_for_id (i);
947 : 9551595 : if (! bitmap_bit_p (&a->values, get_expr_value_id (expr)))
948 : 1380183 : to_remove = i;
949 : : }
950 : 1123295 : if (to_remove != -1U)
951 : 51137 : bitmap_clear_bit (&a->expressions, to_remove);
952 : 1123295 : }
953 : :
954 : :
955 : : /* Return true if bitmapped set SET contains the value VALUE_ID. */
956 : :
957 : : static bool
958 : 191473176 : bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
959 : : {
960 : 0 : if (value_id_constant_p (value_id))
961 : : return true;
962 : :
963 : 92621642 : return bitmap_bit_p (&set->values, value_id);
964 : : }
965 : :
966 : : /* Return true if two bitmap sets are equal. */
967 : :
968 : : static bool
969 : 15541351 : bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
970 : : {
971 : 0 : return bitmap_equal_p (&a->values, &b->values);
972 : : }
973 : :
974 : : /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
975 : : and add it otherwise. Return true if any changes were made. */
976 : :
977 : : static bool
978 : 31983224 : bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
979 : : {
980 : 31983224 : unsigned int val = get_expr_value_id (expr);
981 : 31983224 : if (value_id_constant_p (val))
982 : : return false;
983 : :
984 : 31983224 : if (bitmap_set_contains_value (set, val))
985 : : {
986 : : /* The number of expressions having a given value is usually
987 : : significantly less than the total number of expressions in SET.
988 : : Thus, rather than check, for each expression in SET, whether it
989 : : has the value LOOKFOR, we walk the reverse mapping that tells us
990 : : what expressions have a given value, and see if any of those
991 : : expressions are in our set. For large testcases, this is about
992 : : 5-10x faster than walking the bitmap. If this is somehow a
993 : : significant lose for some cases, we can choose which set to walk
994 : : based on the set size. */
995 : 13668548 : unsigned int i;
996 : 13668548 : bitmap_iterator bi;
997 : 13668548 : bitmap exprset = value_expressions[val];
998 : 15503865 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
999 : : {
1000 : 15503865 : if (bitmap_clear_bit (&set->expressions, i))
1001 : : {
1002 : 13668548 : bitmap_set_bit (&set->expressions, get_expression_id (expr));
1003 : 13668548 : return i != get_expression_id (expr);
1004 : : }
1005 : : }
1006 : 0 : gcc_unreachable ();
1007 : : }
1008 : :
1009 : 18314676 : bitmap_insert_into_set (set, expr);
1010 : 18314676 : return true;
1011 : : }
1012 : :
1013 : : /* Insert EXPR into SET if EXPR's value is not already present in
1014 : : SET. */
1015 : :
1016 : : static void
1017 : 62143603 : bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
1018 : : {
1019 : 62143603 : unsigned int val = get_expr_value_id (expr);
1020 : :
1021 : 62143603 : gcc_checking_assert (expr->id == get_expression_id (expr));
1022 : :
1023 : : /* Constant values are always considered to be part of the set. */
1024 : 62143603 : if (value_id_constant_p (val))
1025 : : return;
1026 : :
1027 : : /* If the value membership changed, add the expression. */
1028 : 62082741 : if (bitmap_set_bit (&set->values, val))
1029 : 47977624 : bitmap_set_bit (&set->expressions, expr->id);
1030 : : }
1031 : :
1032 : : /* Print out EXPR to outfile. */
1033 : :
1034 : : static void
1035 : 4355 : print_pre_expr (FILE *outfile, const pre_expr expr)
1036 : : {
1037 : 4355 : if (! expr)
1038 : : {
1039 : 0 : fprintf (outfile, "NULL");
1040 : 0 : return;
1041 : : }
1042 : 4355 : switch (expr->kind)
1043 : : {
1044 : 0 : case CONSTANT:
1045 : 0 : print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr));
1046 : 0 : break;
1047 : 3085 : case NAME:
1048 : 3085 : print_generic_expr (outfile, PRE_EXPR_NAME (expr));
1049 : 3085 : break;
1050 : 965 : case NARY:
1051 : 965 : {
1052 : 965 : unsigned int i;
1053 : 965 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1054 : 965 : fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
1055 : 3688 : for (i = 0; i < nary->length; i++)
1056 : : {
1057 : 1758 : print_generic_expr (outfile, nary->op[i]);
1058 : 1758 : if (i != (unsigned) nary->length - 1)
1059 : 793 : fprintf (outfile, ",");
1060 : : }
1061 : 965 : fprintf (outfile, "}");
1062 : : }
1063 : 965 : break;
1064 : :
1065 : 305 : case REFERENCE:
1066 : 305 : {
1067 : 305 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1068 : 305 : print_vn_reference_ops (outfile, ref->operands);
1069 : 305 : if (ref->vuse)
1070 : : {
1071 : 293 : fprintf (outfile, "@");
1072 : 293 : print_generic_expr (outfile, ref->vuse);
1073 : : }
1074 : : }
1075 : : break;
1076 : : }
1077 : : }
1078 : : void debug_pre_expr (pre_expr);
1079 : :
1080 : : /* Like print_pre_expr but always prints to stderr. */
1081 : : DEBUG_FUNCTION void
1082 : 0 : debug_pre_expr (pre_expr e)
1083 : : {
1084 : 0 : print_pre_expr (stderr, e);
1085 : 0 : fprintf (stderr, "\n");
1086 : 0 : }
1087 : :
1088 : : /* Print out SET to OUTFILE. */
1089 : :
1090 : : static void
1091 : 880 : print_bitmap_set (FILE *outfile, bitmap_set_t set,
1092 : : const char *setname, int blockindex)
1093 : : {
1094 : 880 : fprintf (outfile, "%s[%d] := { ", setname, blockindex);
1095 : 880 : if (set)
1096 : : {
1097 : 880 : bool first = true;
1098 : 880 : unsigned i;
1099 : 880 : bitmap_iterator bi;
1100 : :
1101 : 5089 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1102 : : {
1103 : 4209 : const pre_expr expr = expression_for_id (i);
1104 : :
1105 : 4209 : if (!first)
1106 : 3606 : fprintf (outfile, ", ");
1107 : 4209 : first = false;
1108 : 4209 : print_pre_expr (outfile, expr);
1109 : :
1110 : 4209 : fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1111 : : }
1112 : : }
1113 : 880 : fprintf (outfile, " }\n");
1114 : 880 : }
1115 : :
1116 : : void debug_bitmap_set (bitmap_set_t);
1117 : :
1118 : : DEBUG_FUNCTION void
1119 : 0 : debug_bitmap_set (bitmap_set_t set)
1120 : : {
1121 : 0 : print_bitmap_set (stderr, set, "debug", 0);
1122 : 0 : }
1123 : :
1124 : : void debug_bitmap_sets_for (basic_block);
1125 : :
1126 : : DEBUG_FUNCTION void
1127 : 0 : debug_bitmap_sets_for (basic_block bb)
1128 : : {
1129 : 0 : print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1130 : 0 : print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1131 : 0 : print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1132 : 0 : print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1133 : 0 : print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1134 : 0 : if (do_partial_partial)
1135 : 0 : print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1136 : 0 : print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1137 : 0 : }
1138 : :
1139 : : /* Print out the expressions that have VAL to OUTFILE. */
1140 : :
1141 : : static void
1142 : 0 : print_value_expressions (FILE *outfile, unsigned int val)
1143 : : {
1144 : 0 : bitmap set = value_expressions[val];
1145 : 0 : if (set)
1146 : : {
1147 : 0 : bitmap_set x;
1148 : 0 : char s[10];
1149 : 0 : sprintf (s, "%04d", val);
1150 : 0 : x.expressions = *set;
1151 : 0 : print_bitmap_set (outfile, &x, s, 0);
1152 : : }
1153 : 0 : }
1154 : :
1155 : :
1156 : : DEBUG_FUNCTION void
1157 : 0 : debug_value_expressions (unsigned int val)
1158 : : {
1159 : 0 : print_value_expressions (stderr, val);
1160 : 0 : }
1161 : :
1162 : : /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1163 : : represent it. */
1164 : :
1165 : : static pre_expr
1166 : 4775703 : get_or_alloc_expr_for_constant (tree constant)
1167 : : {
1168 : 4775703 : unsigned int result_id;
1169 : 4775703 : struct pre_expr_d expr;
1170 : 4775703 : pre_expr newexpr;
1171 : :
1172 : 4775703 : expr.kind = CONSTANT;
1173 : 4775703 : PRE_EXPR_CONSTANT (&expr) = constant;
1174 : 4775703 : result_id = lookup_expression_id (&expr);
1175 : 4775703 : if (result_id != 0)
1176 : 3961050 : return expression_for_id (result_id);
1177 : :
1178 : 814653 : newexpr = pre_expr_pool.allocate ();
1179 : 814653 : newexpr->kind = CONSTANT;
1180 : 814653 : newexpr->loc = UNKNOWN_LOCATION;
1181 : 814653 : PRE_EXPR_CONSTANT (newexpr) = constant;
1182 : 814653 : alloc_expression_id (newexpr);
1183 : 814653 : newexpr->value_id = get_or_alloc_constant_value_id (constant);
1184 : 814653 : add_to_value (newexpr->value_id, newexpr);
1185 : 814653 : return newexpr;
1186 : : }
1187 : :
1188 : : /* Translate the VUSE backwards through phi nodes in E->dest, so that
1189 : : it has the value it would have in E->src. Set *SAME_VALID to true
1190 : : in case the new vuse doesn't change the value id of the OPERANDS. */
1191 : :
1192 : : static tree
1193 : 4334439 : translate_vuse_through_block (vec<vn_reference_op_s> operands,
1194 : : alias_set_type set, alias_set_type base_set,
1195 : : tree type, tree vuse, edge e, bool *same_valid)
1196 : : {
1197 : 4334439 : basic_block phiblock = e->dest;
1198 : 4334439 : gimple *phi = SSA_NAME_DEF_STMT (vuse);
1199 : 4334439 : ao_ref ref;
1200 : :
1201 : 4334439 : if (same_valid)
1202 : 3138392 : *same_valid = true;
1203 : :
1204 : : /* If value-numbering provided a memory state for this
1205 : : that dominates PHIBLOCK we can just use that. */
1206 : 4334439 : if (gimple_nop_p (phi)
1207 : 4334439 : || (gimple_bb (phi) != phiblock
1208 : 1101968 : && dominated_by_p (CDI_DOMINATORS, phiblock, gimple_bb (phi))))
1209 : 1791601 : return vuse;
1210 : :
1211 : : /* We have pruned expressions that are killed in PHIBLOCK via
1212 : : prune_clobbered_mems but we have not rewritten the VUSE to the one
1213 : : live at the start of the block. If there is no virtual PHI to translate
1214 : : through return the VUSE live at entry. Otherwise the VUSE to translate
1215 : : is the def of the virtual PHI node. */
1216 : 2542838 : phi = get_virtual_phi (phiblock);
1217 : 2542838 : if (!phi)
1218 : 89385 : return BB_LIVE_VOP_ON_EXIT
1219 : : (get_immediate_dominator (CDI_DOMINATORS, phiblock));
1220 : :
1221 : 2453453 : if (same_valid
1222 : 2453453 : && ao_ref_init_from_vn_reference (&ref, set, base_set, type, operands))
1223 : : {
1224 : 1793104 : bitmap visited = NULL;
1225 : : /* Try to find a vuse that dominates this phi node by skipping
1226 : : non-clobbering statements. */
1227 : 1793104 : unsigned int cnt = param_sccvn_max_alias_queries_per_access;
1228 : 1793104 : vuse = get_continuation_for_phi (phi, &ref, true,
1229 : : cnt, &visited, false, NULL, NULL);
1230 : 1793104 : if (visited)
1231 : 1785747 : BITMAP_FREE (visited);
1232 : : }
1233 : : else
1234 : : vuse = NULL_TREE;
1235 : : /* If we didn't find any, the value ID can't stay the same. */
1236 : 2453453 : if (!vuse && same_valid)
1237 : 1560670 : *same_valid = false;
1238 : :
1239 : : /* ??? We would like to return vuse here as this is the canonical
1240 : : upmost vdef that this reference is associated with. But during
1241 : : insertion of the references into the hash tables we only ever
1242 : : directly insert with their direct gimple_vuse, hence returning
1243 : : something else would make us not find the other expression. */
1244 : 2453453 : return PHI_ARG_DEF (phi, e->dest_idx);
1245 : : }
1246 : :
1247 : : /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1248 : : SET2 *or* SET3. This is used to avoid making a set consisting of the union
1249 : : of PA_IN and ANTIC_IN during insert and phi-translation. */
1250 : :
1251 : : static inline pre_expr
1252 : 23386129 : find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
1253 : : bitmap_set_t set3 = NULL)
1254 : : {
1255 : 23386129 : pre_expr result = NULL;
1256 : :
1257 : 23386129 : if (set1)
1258 : 23259778 : result = bitmap_find_leader (set1, val);
1259 : 23386129 : if (!result && set2)
1260 : 1471281 : result = bitmap_find_leader (set2, val);
1261 : 23386129 : if (!result && set3)
1262 : 0 : result = bitmap_find_leader (set3, val);
1263 : 23386129 : return result;
1264 : : }
1265 : :
1266 : : /* Get the tree type for our PRE expression e. */
1267 : :
1268 : : static tree
1269 : 7202822 : get_expr_type (const pre_expr e)
1270 : : {
1271 : 7202822 : switch (e->kind)
1272 : : {
1273 : 985981 : case NAME:
1274 : 985981 : return TREE_TYPE (PRE_EXPR_NAME (e));
1275 : 179793 : case CONSTANT:
1276 : 179793 : return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1277 : 1307020 : case REFERENCE:
1278 : 1307020 : return PRE_EXPR_REFERENCE (e)->type;
1279 : 4730028 : case NARY:
1280 : 4730028 : return PRE_EXPR_NARY (e)->type;
1281 : : }
1282 : 0 : gcc_unreachable ();
1283 : : }
1284 : :
1285 : : /* Get a representative SSA_NAME for a given expression that is available in B.
1286 : : Since all of our sub-expressions are treated as values, we require
1287 : : them to be SSA_NAME's for simplicity.
1288 : : Prior versions of GVNPRE used to use "value handles" here, so that
1289 : : an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1290 : : either case, the operands are really values (IE we do not expect
1291 : : them to be usable without finding leaders). */
1292 : :
1293 : : static tree
1294 : 18887086 : get_representative_for (const pre_expr e, basic_block b = NULL)
1295 : : {
1296 : 18887086 : tree name, valnum = NULL_TREE;
1297 : 18887086 : unsigned int value_id = get_expr_value_id (e);
1298 : :
1299 : 18887086 : switch (e->kind)
1300 : : {
1301 : 8546605 : case NAME:
1302 : 8546605 : return PRE_EXPR_NAME (e);
1303 : 1839953 : case CONSTANT:
1304 : 1839953 : return PRE_EXPR_CONSTANT (e);
1305 : 8500528 : case NARY:
1306 : 8500528 : case REFERENCE:
1307 : 8500528 : {
1308 : : /* Go through all of the expressions representing this value
1309 : : and pick out an SSA_NAME. */
1310 : 8500528 : unsigned int i;
1311 : 8500528 : bitmap_iterator bi;
1312 : 8500528 : bitmap exprs = value_expressions[value_id];
1313 : 21975195 : EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1314 : : {
1315 : 18025674 : pre_expr rep = expression_for_id (i);
1316 : 18025674 : if (rep->kind == NAME)
1317 : : {
1318 : 8234623 : tree name = PRE_EXPR_NAME (rep);
1319 : 8234623 : valnum = VN_INFO (name)->valnum;
1320 : 8234623 : gimple *def = SSA_NAME_DEF_STMT (name);
1321 : : /* We have to return either a new representative or one
1322 : : that can be used for expression simplification and thus
1323 : : is available in B. */
1324 : 8234623 : if (! b
1325 : 7964254 : || gimple_nop_p (def)
1326 : 12190166 : || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
1327 : 4551007 : return name;
1328 : : }
1329 : 9791051 : else if (rep->kind == CONSTANT)
1330 : 0 : return PRE_EXPR_CONSTANT (rep);
1331 : : }
1332 : : }
1333 : 3949521 : break;
1334 : : }
1335 : :
1336 : : /* If we reached here we couldn't find an SSA_NAME. This can
1337 : : happen when we've discovered a value that has never appeared in
1338 : : the program as set to an SSA_NAME, as the result of phi translation.
1339 : : Create one here.
1340 : : ??? We should be able to re-use this when we insert the statement
1341 : : to compute it. */
1342 : 3949521 : name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1343 : 3949521 : vn_ssa_aux_t vn_info = VN_INFO (name);
1344 : 3949521 : vn_info->value_id = value_id;
1345 : 3949521 : vn_info->valnum = valnum ? valnum : name;
1346 : 3949521 : vn_info->visited = true;
1347 : : /* ??? For now mark this SSA name for release by VN. */
1348 : 3949521 : vn_info->needs_insertion = true;
1349 : 3949521 : add_to_value (value_id, get_or_alloc_expr_for_name (name));
1350 : 3949521 : if (dump_file && (dump_flags & TDF_DETAILS))
1351 : : {
1352 : 47 : fprintf (dump_file, "Created SSA_NAME representative ");
1353 : 47 : print_generic_expr (dump_file, name);
1354 : 47 : fprintf (dump_file, " for expression:");
1355 : 47 : print_pre_expr (dump_file, e);
1356 : 47 : fprintf (dump_file, " (%04d)\n", value_id);
1357 : : }
1358 : :
1359 : : return name;
1360 : : }
1361 : :
1362 : :
1363 : : static pre_expr
1364 : : phi_translate (bitmap_set_t, pre_expr, bitmap_set_t, bitmap_set_t, edge);
1365 : :
1366 : : /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1367 : : the phis in PRED. Return NULL if we can't find a leader for each part
1368 : : of the translated expression. */
1369 : :
1370 : : static pre_expr
1371 : 46516136 : phi_translate_1 (bitmap_set_t dest,
1372 : : pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
1373 : : {
1374 : 46516136 : basic_block pred = e->src;
1375 : 46516136 : basic_block phiblock = e->dest;
1376 : 46516136 : location_t expr_loc = expr->loc;
1377 : 46516136 : switch (expr->kind)
1378 : : {
1379 : 17767070 : case NARY:
1380 : 17767070 : {
1381 : 17767070 : unsigned int i;
1382 : 17767070 : bool changed = false;
1383 : 17767070 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1384 : 17767070 : vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1385 : : sizeof_vn_nary_op (nary->length));
1386 : 17767070 : memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1387 : :
1388 : 42362120 : for (i = 0; i < newnary->length; i++)
1389 : : {
1390 : 27732089 : if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1391 : 8640815 : continue;
1392 : : else
1393 : : {
1394 : 19091274 : pre_expr leader, result;
1395 : 19091274 : unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1396 : 19091274 : leader = find_leader_in_sets (op_val_id, set1, set2);
1397 : 19091274 : result = phi_translate (dest, leader, set1, set2, e);
1398 : 19091274 : if (result)
1399 : : /* If op has a leader in the sets we translate make
1400 : : sure to use the value of the translated expression.
1401 : : We might need a new representative for that. */
1402 : 15954235 : newnary->op[i] = get_representative_for (result, pred);
1403 : : else if (!result)
1404 : : return NULL;
1405 : :
1406 : 15954235 : changed |= newnary->op[i] != nary->op[i];
1407 : : }
1408 : : }
1409 : 14630031 : if (changed)
1410 : : {
1411 : 7407330 : unsigned int new_val_id;
1412 : :
1413 : : /* Try to simplify the new NARY. */
1414 : 7407330 : tree res = vn_nary_simplify (newnary);
1415 : 7407330 : if (res)
1416 : : {
1417 : 2373443 : if (is_gimple_min_invariant (res))
1418 : 1206625 : return get_or_alloc_expr_for_constant (res);
1419 : :
1420 : : /* For non-CONSTANTs we have to make sure we can eventually
1421 : : insert the expression. Which means we need to have a
1422 : : leader for it. */
1423 : 1166818 : gcc_assert (TREE_CODE (res) == SSA_NAME);
1424 : :
1425 : : /* Do not allow simplifications to non-constants over
1426 : : backedges as this will likely result in a loop PHI node
1427 : : to be inserted and increased register pressure.
1428 : : See PR77498 - this avoids doing predcoms work in
1429 : : a less efficient way. */
1430 : 1166818 : if (e->flags & EDGE_DFS_BACK)
1431 : : ;
1432 : : else
1433 : : {
1434 : 1083206 : unsigned value_id = VN_INFO (res)->value_id;
1435 : : /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1436 : : dest has what we computed into ANTIC_OUT sofar
1437 : : so pick from that - since topological sorting
1438 : : by sorted_array_from_bitmap_set isn't perfect
1439 : : we may lose some cases here. */
1440 : 2166412 : pre_expr constant = find_leader_in_sets (value_id, dest,
1441 : 1083206 : AVAIL_OUT (pred));
1442 : 1083206 : if (constant)
1443 : : {
1444 : 310630 : if (dump_file && (dump_flags & TDF_DETAILS))
1445 : : {
1446 : 7 : fprintf (dump_file, "simplifying ");
1447 : 7 : print_pre_expr (dump_file, expr);
1448 : 7 : fprintf (dump_file, " translated %d -> %d to ",
1449 : : phiblock->index, pred->index);
1450 : 7 : PRE_EXPR_NARY (expr) = newnary;
1451 : 7 : print_pre_expr (dump_file, expr);
1452 : 7 : PRE_EXPR_NARY (expr) = nary;
1453 : 7 : fprintf (dump_file, " to ");
1454 : 7 : print_pre_expr (dump_file, constant);
1455 : 7 : fprintf (dump_file, "\n");
1456 : : }
1457 : 310630 : return constant;
1458 : : }
1459 : : }
1460 : : }
1461 : :
1462 : 11780150 : tree result = vn_nary_op_lookup_pieces (newnary->length,
1463 : 5890075 : newnary->opcode,
1464 : : newnary->type,
1465 : : &newnary->op[0],
1466 : : &nary);
1467 : 5890075 : if (result && is_gimple_min_invariant (result))
1468 : 0 : return get_or_alloc_expr_for_constant (result);
1469 : :
1470 : 5890075 : if (!nary || nary->predicated_values)
1471 : : new_val_id = 0;
1472 : : else
1473 : 826656 : new_val_id = nary->value_id;
1474 : 5890075 : expr = get_or_alloc_expr_for_nary (newnary, new_val_id, expr_loc);
1475 : 5890075 : add_to_value (get_expr_value_id (expr), expr);
1476 : : }
1477 : : return expr;
1478 : : }
1479 : 4707679 : break;
1480 : :
1481 : 4707679 : case REFERENCE:
1482 : 4707679 : {
1483 : 4707679 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1484 : 4707679 : vec<vn_reference_op_s> operands = ref->operands;
1485 : 4707679 : tree vuse = ref->vuse;
1486 : 4707679 : tree newvuse = vuse;
1487 : 4707679 : vec<vn_reference_op_s> newoperands = vNULL;
1488 : 4707679 : bool changed = false, same_valid = true;
1489 : 4707679 : unsigned int i, n;
1490 : 4707679 : vn_reference_op_t operand;
1491 : 4707679 : vn_reference_t newref;
1492 : :
1493 : 18027699 : for (i = 0; operands.iterate (i, &operand); i++)
1494 : : {
1495 : 13598818 : pre_expr opresult;
1496 : 13598818 : pre_expr leader;
1497 : 13598818 : tree op[3];
1498 : 13598818 : tree type = operand->type;
1499 : 13598818 : vn_reference_op_s newop = *operand;
1500 : 13598818 : op[0] = operand->op0;
1501 : 13598818 : op[1] = operand->op1;
1502 : 13598818 : op[2] = operand->op2;
1503 : 53558938 : for (n = 0; n < 3; ++n)
1504 : : {
1505 : 40238918 : unsigned int op_val_id;
1506 : 40238918 : if (!op[n])
1507 : 24379729 : continue;
1508 : 15859189 : if (TREE_CODE (op[n]) != SSA_NAME)
1509 : : {
1510 : : /* We can't possibly insert these. */
1511 : 12647540 : if (n != 0
1512 : 12647540 : && !is_gimple_min_invariant (op[n]))
1513 : : break;
1514 : 12647540 : continue;
1515 : : }
1516 : 3211649 : op_val_id = VN_INFO (op[n])->value_id;
1517 : 3211649 : leader = find_leader_in_sets (op_val_id, set1, set2);
1518 : 3211649 : opresult = phi_translate (dest, leader, set1, set2, e);
1519 : 3211649 : if (opresult)
1520 : : {
1521 : 2932851 : tree name = get_representative_for (opresult);
1522 : 2932851 : changed |= name != op[n];
1523 : 2932851 : op[n] = name;
1524 : : }
1525 : : else if (!opresult)
1526 : : break;
1527 : : }
1528 : 13598818 : if (n != 3)
1529 : : {
1530 : 278798 : newoperands.release ();
1531 : 278798 : return NULL;
1532 : : }
1533 : : /* When we translate a MEM_REF across a backedge and we have
1534 : : restrict info that's not from our functions parameters
1535 : : we have to remap it since we now may deal with a different
1536 : : instance where the dependence info is no longer valid.
1537 : : See PR102970. Note instead of keeping a remapping table
1538 : : per backedge we simply throw away restrict info. */
1539 : 13320020 : if ((newop.opcode == MEM_REF
1540 : 13320020 : || newop.opcode == TARGET_MEM_REF)
1541 : 4479889 : && newop.clique > 1
1542 : 146932 : && (e->flags & EDGE_DFS_BACK))
1543 : : {
1544 : : newop.clique = 0;
1545 : : newop.base = 0;
1546 : : changed = true;
1547 : : }
1548 : 13302408 : if (!changed)
1549 : 10942351 : continue;
1550 : 2377669 : if (!newoperands.exists ())
1551 : 1221569 : newoperands = operands.copy ();
1552 : : /* We may have changed from an SSA_NAME to a constant */
1553 : 2377669 : if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1554 : : newop.opcode = TREE_CODE (op[0]);
1555 : 2377669 : newop.type = type;
1556 : 2377669 : newop.op0 = op[0];
1557 : 2377669 : newop.op1 = op[1];
1558 : 2377669 : newop.op2 = op[2];
1559 : 2377669 : newoperands[i] = newop;
1560 : : }
1561 : 8857762 : gcc_checking_assert (i == operands.length ());
1562 : :
1563 : 4428881 : if (vuse)
1564 : : {
1565 : 10611223 : newvuse = translate_vuse_through_block (newoperands.exists ()
1566 : 4334439 : ? newoperands : operands,
1567 : : ref->set, ref->base_set,
1568 : : ref->type, vuse, e,
1569 : : changed
1570 : : ? NULL : &same_valid);
1571 : 4334439 : if (newvuse == NULL_TREE)
1572 : : {
1573 : 0 : newoperands.release ();
1574 : 0 : return NULL;
1575 : : }
1576 : : }
1577 : :
1578 : 4428881 : if (changed || newvuse != vuse)
1579 : : {
1580 : 3122824 : unsigned int new_val_id;
1581 : :
1582 : 5024908 : tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1583 : : ref->base_set,
1584 : : ref->type,
1585 : 3122824 : newoperands.exists ()
1586 : 3122824 : ? newoperands : operands,
1587 : : &newref, VN_WALK);
1588 : 3122824 : if (result)
1589 : 641547 : newoperands.release ();
1590 : :
1591 : : /* We can always insert constants, so if we have a partial
1592 : : redundant constant load of another type try to translate it
1593 : : to a constant of appropriate type. */
1594 : 641547 : if (result && is_gimple_min_invariant (result))
1595 : : {
1596 : 67367 : tree tem = result;
1597 : 67367 : if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1598 : : {
1599 : 68 : tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1600 : 68 : if (tem && !is_gimple_min_invariant (tem))
1601 : : tem = NULL_TREE;
1602 : : }
1603 : 67367 : if (tem)
1604 : 67367 : return get_or_alloc_expr_for_constant (tem);
1605 : : }
1606 : :
1607 : : /* If we'd have to convert things we would need to validate
1608 : : if we can insert the translated expression. So fail
1609 : : here for now - we cannot insert an alias with a different
1610 : : type in the VN tables either, as that would assert. */
1611 : 3055457 : if (result
1612 : 3055457 : && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1613 : : return NULL;
1614 : 2481277 : else if (!result && newref
1615 : 3239455 : && !useless_type_conversion_p (ref->type, newref->type))
1616 : : {
1617 : 224 : newoperands.release ();
1618 : 224 : return NULL;
1619 : : }
1620 : :
1621 : 3054245 : if (newref)
1622 : 757954 : new_val_id = newref->value_id;
1623 : : else
1624 : : {
1625 : 2296291 : if (changed || !same_valid)
1626 : 2234642 : new_val_id = get_next_value_id ();
1627 : : else
1628 : 61649 : new_val_id = ref->value_id;
1629 : 2296291 : if (!newoperands.exists ())
1630 : 1275610 : newoperands = operands.copy ();
1631 : 2296291 : newref = vn_reference_insert_pieces (newvuse, ref->set,
1632 : : ref->base_set,
1633 : : ref->offset, ref->max_size,
1634 : : ref->type, newoperands,
1635 : : result, new_val_id);
1636 : 2296291 : newoperands = vNULL;
1637 : : }
1638 : 3054245 : expr = get_or_alloc_expr_for_reference (newref, expr_loc);
1639 : 3054245 : add_to_value (new_val_id, expr);
1640 : : }
1641 : 4360302 : newoperands.release ();
1642 : 4360302 : return expr;
1643 : : }
1644 : 24041387 : break;
1645 : :
1646 : 24041387 : case NAME:
1647 : 24041387 : {
1648 : 24041387 : tree name = PRE_EXPR_NAME (expr);
1649 : 24041387 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1650 : : /* If the SSA name is defined by a PHI node in this block,
1651 : : translate it. */
1652 : 24041387 : if (gimple_code (def_stmt) == GIMPLE_PHI
1653 : 24041387 : && gimple_bb (def_stmt) == phiblock)
1654 : : {
1655 : 7551391 : tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1656 : :
1657 : : /* Handle constant. */
1658 : 7551391 : if (is_gimple_min_invariant (def))
1659 : 2192289 : return get_or_alloc_expr_for_constant (def);
1660 : :
1661 : 5359102 : return get_or_alloc_expr_for_name (def);
1662 : : }
1663 : : /* Otherwise return it unchanged - it will get removed if its
1664 : : value is not available in PREDs AVAIL_OUT set of expressions
1665 : : by the subtraction of TMP_GEN. */
1666 : : return expr;
1667 : : }
1668 : :
1669 : 0 : default:
1670 : 0 : gcc_unreachable ();
1671 : : }
1672 : : }
1673 : :
1674 : : /* Wrapper around phi_translate_1 providing caching functionality. */
1675 : :
1676 : : static pre_expr
1677 : 85991094 : phi_translate (bitmap_set_t dest, pre_expr expr,
1678 : : bitmap_set_t set1, bitmap_set_t set2, edge e)
1679 : : {
1680 : 85991094 : expr_pred_trans_t slot = NULL;
1681 : 85991094 : pre_expr phitrans;
1682 : :
1683 : 85991094 : if (!expr)
1684 : : return NULL;
1685 : :
1686 : : /* Constants contain no values that need translation. */
1687 : 84268896 : if (expr->kind == CONSTANT)
1688 : : return expr;
1689 : :
1690 : 84268806 : if (value_id_constant_p (get_expr_value_id (expr)))
1691 : : return expr;
1692 : :
1693 : : /* Don't add translations of NAMEs as those are cheap to translate. */
1694 : 84268806 : if (expr->kind != NAME)
1695 : : {
1696 : 60227419 : if (phi_trans_add (&slot, expr, e->src))
1697 : 37752670 : return slot->v == 0 ? NULL : expression_for_id (slot->v);
1698 : : /* Store NULL for the value we want to return in the case of
1699 : : recursing. */
1700 : 22474749 : slot->v = 0;
1701 : : }
1702 : :
1703 : : /* Translate. */
1704 : 46516136 : basic_block saved_valueize_bb = vn_context_bb;
1705 : 46516136 : vn_context_bb = e->src;
1706 : 46516136 : phitrans = phi_translate_1 (dest, expr, set1, set2, e);
1707 : 46516136 : vn_context_bb = saved_valueize_bb;
1708 : :
1709 : 46516136 : if (slot)
1710 : : {
1711 : : /* We may have reallocated. */
1712 : 22474749 : phi_trans_add (&slot, expr, e->src);
1713 : 22474749 : if (phitrans)
1714 : 19057700 : slot->v = get_expression_id (phitrans);
1715 : : else
1716 : : /* Remove failed translations again, they cause insert
1717 : : iteration to not pick up new opportunities reliably. */
1718 : 3417049 : PHI_TRANS_TABLE (e->src)->clear_slot (slot);
1719 : : }
1720 : :
1721 : : return phitrans;
1722 : : }
1723 : :
1724 : :
1725 : : /* For each expression in SET, translate the values through phi nodes
1726 : : in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1727 : : expressions in DEST. */
1728 : :
1729 : : static void
1730 : 20288126 : phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
1731 : : {
1732 : 20288126 : bitmap_iterator bi;
1733 : 20288126 : unsigned int i;
1734 : :
1735 : 20288126 : if (gimple_seq_empty_p (phi_nodes (e->dest)))
1736 : : {
1737 : 13642429 : bitmap_set_copy (dest, set);
1738 : 13642429 : return;
1739 : : }
1740 : :
1741 : : /* Allocate the phi-translation cache where we have an idea about
1742 : : its size. hash-table implementation internals tell us that
1743 : : allocating the table to fit twice the number of elements will
1744 : : make sure we do not usually re-allocate. */
1745 : 6645697 : if (!PHI_TRANS_TABLE (e->src))
1746 : 5946281 : PHI_TRANS_TABLE (e->src) = new hash_table<expr_pred_trans_d>
1747 : 5946281 : (2 * bitmap_count_bits (&set->expressions));
1748 : 42993693 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1749 : : {
1750 : 36347996 : pre_expr expr = expression_for_id (i);
1751 : 36347996 : pre_expr translated = phi_translate (dest, expr, set, NULL, e);
1752 : 36347996 : if (!translated)
1753 : 1722563 : continue;
1754 : :
1755 : 34625433 : bitmap_insert_into_set (dest, translated);
1756 : : }
1757 : : }
1758 : :
1759 : : /* Find the leader for a value (i.e., the name representing that
1760 : : value) in a given set, and return it. Return NULL if no leader
1761 : : is found. */
1762 : :
1763 : : static pre_expr
1764 : 55065186 : bitmap_find_leader (bitmap_set_t set, unsigned int val)
1765 : : {
1766 : 55065186 : if (value_id_constant_p (val))
1767 : 1682665 : return constant_value_expressions[-val];
1768 : :
1769 : 53382521 : if (bitmap_set_contains_value (set, val))
1770 : : {
1771 : : /* Rather than walk the entire bitmap of expressions, and see
1772 : : whether any of them has the value we are looking for, we look
1773 : : at the reverse mapping, which tells us the set of expressions
1774 : : that have a given value (IE value->expressions with that
1775 : : value) and see if any of those expressions are in our set.
1776 : : The number of expressions per value is usually significantly
1777 : : less than the number of expressions in the set. In fact, for
1778 : : large testcases, doing it this way is roughly 5-10x faster
1779 : : than walking the bitmap.
1780 : : If this is somehow a significant lose for some cases, we can
1781 : : choose which set to walk based on which set is smaller. */
1782 : 24754271 : unsigned int i;
1783 : 24754271 : bitmap_iterator bi;
1784 : 24754271 : bitmap exprset = value_expressions[val];
1785 : :
1786 : 24754271 : if (!exprset->first->next)
1787 : 31575147 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1788 : 29276536 : if (bitmap_bit_p (&set->expressions, i))
1789 : 22404540 : return expression_for_id (i);
1790 : :
1791 : 6069042 : EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1792 : 3719311 : return expression_for_id (i);
1793 : : }
1794 : : return NULL;
1795 : : }
1796 : :
1797 : : /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1798 : : BLOCK by seeing if it is not killed in the block. Note that we are
1799 : : only determining whether there is a store that kills it. Because
1800 : : of the order in which clean iterates over values, we are guaranteed
1801 : : that altered operands will have caused us to be eliminated from the
1802 : : ANTIC_IN set already. */
1803 : :
1804 : : static bool
1805 : 1562352 : value_dies_in_block_x (pre_expr expr, basic_block block)
1806 : : {
1807 : 1562352 : tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1808 : 1562352 : vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1809 : 1562352 : gimple *def;
1810 : 1562352 : gimple_stmt_iterator gsi;
1811 : 1562352 : unsigned id = get_expression_id (expr);
1812 : 1562352 : bool res = false;
1813 : 1562352 : ao_ref ref;
1814 : :
1815 : 1562352 : if (!vuse)
1816 : : return false;
1817 : :
1818 : : /* Lookup a previously calculated result. */
1819 : 1562352 : if (EXPR_DIES (block)
1820 : 1562352 : && bitmap_bit_p (EXPR_DIES (block), id * 2))
1821 : 137797 : return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1822 : :
1823 : : /* A memory expression {e, VUSE} dies in the block if there is a
1824 : : statement that may clobber e. If, starting statement walk from the
1825 : : top of the basic block, a statement uses VUSE there can be no kill
1826 : : inbetween that use and the original statement that loaded {e, VUSE},
1827 : : so we can stop walking. */
1828 : 1424555 : ref.base = NULL_TREE;
1829 : 12523971 : for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1830 : : {
1831 : 10659937 : tree def_vuse, def_vdef;
1832 : 10659937 : def = gsi_stmt (gsi);
1833 : 10659937 : def_vuse = gimple_vuse (def);
1834 : 10659937 : def_vdef = gimple_vdef (def);
1835 : :
1836 : : /* Not a memory statement. */
1837 : 10659937 : if (!def_vuse)
1838 : 7534801 : continue;
1839 : :
1840 : : /* Not a may-def. */
1841 : 3125136 : if (!def_vdef)
1842 : : {
1843 : : /* A load with the same VUSE, we're done. */
1844 : 904657 : if (def_vuse == vuse)
1845 : : break;
1846 : :
1847 : 630111 : continue;
1848 : : }
1849 : :
1850 : : /* Init ref only if we really need it. */
1851 : 2220479 : if (ref.base == NULL_TREE
1852 : 3305033 : && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->base_set,
1853 : 1084554 : refx->type, refx->operands))
1854 : : {
1855 : : res = true;
1856 : : break;
1857 : : }
1858 : : /* If the statement may clobber expr, it dies. */
1859 : 2186121 : if (stmt_may_clobber_ref_p_1 (def, &ref))
1860 : : {
1861 : : res = true;
1862 : : break;
1863 : : }
1864 : : }
1865 : :
1866 : : /* Remember the result. */
1867 : 1424555 : if (!EXPR_DIES (block))
1868 : 691379 : EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1869 : 1424555 : bitmap_set_bit (EXPR_DIES (block), id * 2);
1870 : 1424555 : if (res)
1871 : 710530 : bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1872 : :
1873 : : return res;
1874 : : }
1875 : :
1876 : :
1877 : : /* Determine if OP is valid in SET1 U SET2, which it is when the union
1878 : : contains its value-id. */
1879 : :
1880 : : static bool
1881 : 256224837 : op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1882 : : {
1883 : 256224837 : if (op && TREE_CODE (op) == SSA_NAME)
1884 : : {
1885 : 76025659 : unsigned int value_id = VN_INFO (op)->value_id;
1886 : 152049227 : if (!(bitmap_set_contains_value (set1, value_id)
1887 : 2067329 : || (set2 && bitmap_set_contains_value (set2, value_id))))
1888 : 2359572 : return false;
1889 : : }
1890 : : return true;
1891 : : }
1892 : :
1893 : : /* Determine if the expression EXPR is valid in SET1 U SET2.
1894 : : ONLY SET2 CAN BE NULL.
1895 : : This means that we have a leader for each part of the expression
1896 : : (if it consists of values), or the expression is an SSA_NAME.
1897 : : For loads/calls, we also see if the vuse is killed in this block. */
1898 : :
1899 : : static bool
1900 : 120230542 : valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
1901 : : {
1902 : 120230542 : switch (expr->kind)
1903 : : {
1904 : : case NAME:
1905 : : /* By construction all NAMEs are available. Non-available
1906 : : NAMEs are removed by subtracting TMP_GEN from the sets. */
1907 : : return true;
1908 : 55206274 : case NARY:
1909 : 55206274 : {
1910 : 55206274 : unsigned int i;
1911 : 55206274 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1912 : 144227129 : for (i = 0; i < nary->length; i++)
1913 : 91165887 : if (!op_valid_in_sets (set1, set2, nary->op[i]))
1914 : : return false;
1915 : : return true;
1916 : : }
1917 : 18876807 : break;
1918 : 18876807 : case REFERENCE:
1919 : 18876807 : {
1920 : 18876807 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1921 : 18876807 : vn_reference_op_t vro;
1922 : 18876807 : unsigned int i;
1923 : :
1924 : 73824917 : FOR_EACH_VEC_ELT (ref->operands, i, vro)
1925 : : {
1926 : 55162650 : if (!op_valid_in_sets (set1, set2, vro->op0)
1927 : 54948150 : || !op_valid_in_sets (set1, set2, vro->op1)
1928 : 110110800 : || !op_valid_in_sets (set1, set2, vro->op2))
1929 : 214540 : return false;
1930 : : }
1931 : : return true;
1932 : : }
1933 : 0 : default:
1934 : 0 : gcc_unreachable ();
1935 : : }
1936 : : }
1937 : :
1938 : : /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
1939 : : This means expressions that are made up of values we have no leaders for
1940 : : in SET1 or SET2. */
1941 : :
1942 : : static void
1943 : 14423176 : clean (bitmap_set_t set1, bitmap_set_t set2 = NULL)
1944 : : {
1945 : 14423176 : vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
1946 : 14423176 : pre_expr expr;
1947 : 14423176 : int i;
1948 : :
1949 : 75603687 : FOR_EACH_VEC_ELT (exprs, i, expr)
1950 : : {
1951 : 61180511 : if (!valid_in_sets (set1, set2, expr))
1952 : : {
1953 : 2359554 : unsigned int val = get_expr_value_id (expr);
1954 : 2359554 : bitmap_clear_bit (&set1->expressions, get_expression_id (expr));
1955 : : /* We are entered with possibly multiple expressions for a value
1956 : : so before removing a value from the set see if there's an
1957 : : expression for it left. */
1958 : 2359554 : if (! bitmap_find_leader (set1, val))
1959 : 2349731 : bitmap_clear_bit (&set1->values, val);
1960 : : }
1961 : : }
1962 : 14423176 : exprs.release ();
1963 : :
1964 : 14423176 : if (flag_checking)
1965 : : {
1966 : 14422989 : unsigned j;
1967 : 14422989 : bitmap_iterator bi;
1968 : 73243747 : FOR_EACH_EXPR_ID_IN_SET (set1, j, bi)
1969 : 58820758 : gcc_assert (valid_in_sets (set1, set2, expression_for_id (j)));
1970 : : }
1971 : 14423176 : }
1972 : :
1973 : : /* Clean the set of expressions that are no longer valid in SET because
1974 : : they are clobbered in BLOCK or because they trap and may not be executed.
1975 : : When CLEAN_TRAPS is true remove all possibly trapping expressions. */
1976 : :
1977 : : static void
1978 : 16664646 : prune_clobbered_mems (bitmap_set_t set, basic_block block, bool clean_traps)
1979 : : {
1980 : 16664646 : bitmap_iterator bi;
1981 : 16664646 : unsigned i;
1982 : 16664646 : unsigned to_remove = -1U;
1983 : 16664646 : bool any_removed = false;
1984 : :
1985 : 73854617 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1986 : : {
1987 : : /* Remove queued expr. */
1988 : 57189971 : if (to_remove != -1U)
1989 : : {
1990 : 561912 : bitmap_clear_bit (&set->expressions, to_remove);
1991 : 561912 : any_removed = true;
1992 : 561912 : to_remove = -1U;
1993 : : }
1994 : :
1995 : 57189971 : pre_expr expr = expression_for_id (i);
1996 : 57189971 : if (expr->kind == REFERENCE)
1997 : : {
1998 : 7682022 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1999 : 7682022 : if (ref->vuse)
2000 : : {
2001 : 6956085 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2002 : 6956085 : if (!gimple_nop_p (def_stmt)
2003 : : /* If value-numbering provided a memory state for this
2004 : : that dominates BLOCK we're done, otherwise we have
2005 : : to check if the value dies in BLOCK. */
2006 : 8592782 : && !(gimple_bb (def_stmt) != block
2007 : 3633455 : && dominated_by_p (CDI_DOMINATORS,
2008 : 3633455 : block, gimple_bb (def_stmt)))
2009 : 8518437 : && value_dies_in_block_x (expr, block))
2010 : : to_remove = i;
2011 : : }
2012 : : /* If the REFERENCE may trap make sure the block does not contain
2013 : : a possible exit point.
2014 : : ??? This is overly conservative if we translate AVAIL_OUT
2015 : : as the available expression might be after the exit point. */
2016 : 7052709 : if ((BB_MAY_NOTRETURN (block) || clean_traps)
2017 : 7942862 : && vn_reference_may_trap (ref))
2018 : : to_remove = i;
2019 : : }
2020 : 49507949 : else if (expr->kind == NARY)
2021 : : {
2022 : 26595682 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2023 : : /* If the NARY may trap make sure the block does not contain
2024 : : a possible exit point.
2025 : : ??? This is overly conservative if we translate AVAIL_OUT
2026 : : as the available expression might be after the exit point. */
2027 : 22697012 : if ((BB_MAY_NOTRETURN (block) || clean_traps)
2028 : 27456913 : && vn_nary_may_trap (nary))
2029 : : to_remove = i;
2030 : : }
2031 : : }
2032 : :
2033 : : /* Remove queued expr. */
2034 : 16664646 : if (to_remove != -1U)
2035 : : {
2036 : 405953 : bitmap_clear_bit (&set->expressions, to_remove);
2037 : 405953 : any_removed = true;
2038 : : }
2039 : :
2040 : : /* Above we only removed expressions, now clean the set of values
2041 : : which no longer have any corresponding expression. We cannot
2042 : : clear the value at the time we remove an expression since there
2043 : : may be multiple expressions per value.
2044 : : If we'd queue possibly to be removed values we could use
2045 : : the bitmap_find_leader way to see if there's still an expression
2046 : : for it. For some ratio of to be removed values and number of
2047 : : values/expressions in the set this might be faster than rebuilding
2048 : : the value-set. */
2049 : 16664646 : if (any_removed)
2050 : : {
2051 : 587962 : bitmap_clear (&set->values);
2052 : 3304568 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2053 : : {
2054 : 2716606 : pre_expr expr = expression_for_id (i);
2055 : 2716606 : unsigned int value_id = get_expr_value_id (expr);
2056 : 2716606 : bitmap_set_bit (&set->values, value_id);
2057 : : }
2058 : : }
2059 : 16664646 : }
2060 : :
2061 : : /* Compute the ANTIC set for BLOCK.
2062 : :
2063 : : If succs(BLOCK) > 1 then
2064 : : ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2065 : : else if succs(BLOCK) == 1 then
2066 : : ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2067 : :
2068 : : ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2069 : :
2070 : : Note that clean() is deferred until after the iteration. */
2071 : :
2072 : : static bool
2073 : 15545570 : compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2074 : : {
2075 : 15545570 : bitmap_set_t S, old, ANTIC_OUT;
2076 : 15545570 : edge e;
2077 : 15545570 : edge_iterator ei;
2078 : :
2079 : 15545570 : bool was_visited = BB_VISITED (block);
2080 : 15545570 : bool changed = ! BB_VISITED (block);
2081 : 15545570 : bool any_max_on_edge = false;
2082 : :
2083 : 15545570 : BB_VISITED (block) = 1;
2084 : 15545570 : old = ANTIC_OUT = S = NULL;
2085 : :
2086 : : /* If any edges from predecessors are abnormal, antic_in is empty,
2087 : : so do nothing. */
2088 : 15545570 : if (block_has_abnormal_pred_edge)
2089 : 4219 : goto maybe_dump_sets;
2090 : :
2091 : 15541351 : old = ANTIC_IN (block);
2092 : 15541351 : ANTIC_OUT = bitmap_set_new ();
2093 : :
2094 : : /* If the block has no successors, ANTIC_OUT is empty. */
2095 : 15541351 : if (EDGE_COUNT (block->succs) == 0)
2096 : : ;
2097 : : /* If we have one successor, we could have some phi nodes to
2098 : : translate through. */
2099 : 15541351 : else if (single_succ_p (block))
2100 : : {
2101 : 9876989 : e = single_succ_edge (block);
2102 : 9876989 : gcc_assert (BB_VISITED (e->dest));
2103 : 9876989 : phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
2104 : : }
2105 : : /* If we have multiple successors, we take the intersection of all of
2106 : : them. Note that in the case of loop exit phi nodes, we may have
2107 : : phis to translate through. */
2108 : : else
2109 : : {
2110 : 5664362 : size_t i;
2111 : 5664362 : edge first = NULL;
2112 : :
2113 : 5664362 : auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2114 : 17136766 : FOR_EACH_EDGE (e, ei, block->succs)
2115 : : {
2116 : 11472404 : if (!first
2117 : 6120505 : && BB_VISITED (e->dest))
2118 : : first = e;
2119 : 5808042 : else if (BB_VISITED (e->dest))
2120 : 5166158 : worklist.quick_push (e);
2121 : : else
2122 : : {
2123 : : /* Unvisited successors get their ANTIC_IN replaced by the
2124 : : maximal set to arrive at a maximum ANTIC_IN solution.
2125 : : We can ignore them in the intersection operation and thus
2126 : : need not explicitely represent that maximum solution. */
2127 : 641884 : any_max_on_edge = true;
2128 : 641884 : if (dump_file && (dump_flags & TDF_DETAILS))
2129 : 18 : fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n",
2130 : 18 : e->src->index, e->dest->index);
2131 : : }
2132 : : }
2133 : :
2134 : : /* Of multiple successors we have to have visited one already
2135 : : which is guaranteed by iteration order. */
2136 : 5664362 : gcc_assert (first != NULL);
2137 : :
2138 : 5664362 : phi_translate_set (ANTIC_OUT, ANTIC_IN (first->dest), first);
2139 : :
2140 : : /* If we have multiple successors we need to intersect the ANTIC_OUT
2141 : : sets. For values that's a simple intersection but for
2142 : : expressions it is a union. Given we want to have a single
2143 : : expression per value in our sets we have to canonicalize.
2144 : : Avoid randomness and running into cycles like for PR82129 and
2145 : : canonicalize the expression we choose to the one with the
2146 : : lowest id. This requires we actually compute the union first. */
2147 : 10830520 : FOR_EACH_VEC_ELT (worklist, i, e)
2148 : : {
2149 : 5166158 : if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2150 : : {
2151 : 2254 : bitmap_set_t tmp = bitmap_set_new ();
2152 : 2254 : phi_translate_set (tmp, ANTIC_IN (e->dest), e);
2153 : 2254 : bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
2154 : 2254 : bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
2155 : 2254 : bitmap_set_free (tmp);
2156 : : }
2157 : : else
2158 : : {
2159 : 5163904 : bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
2160 : 5163904 : bitmap_ior_into (&ANTIC_OUT->expressions,
2161 : 5163904 : &ANTIC_IN (e->dest)->expressions);
2162 : : }
2163 : : }
2164 : 11328724 : if (! worklist.is_empty ())
2165 : : {
2166 : : /* Prune expressions not in the value set. */
2167 : 5026564 : bitmap_iterator bi;
2168 : 5026564 : unsigned int i;
2169 : 5026564 : unsigned int to_clear = -1U;
2170 : 35912217 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
2171 : : {
2172 : 30885653 : if (to_clear != -1U)
2173 : : {
2174 : 16483289 : bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2175 : 16483289 : to_clear = -1U;
2176 : : }
2177 : 30885653 : pre_expr expr = expression_for_id (i);
2178 : 30885653 : unsigned int value_id = get_expr_value_id (expr);
2179 : 30885653 : if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
2180 : 20225254 : to_clear = i;
2181 : : }
2182 : 5026564 : if (to_clear != -1U)
2183 : 3741965 : bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2184 : : }
2185 : 5664362 : }
2186 : :
2187 : : /* Dump ANTIC_OUT before it's pruned. */
2188 : 15541351 : if (dump_file && (dump_flags & TDF_DETAILS))
2189 : 140 : print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2190 : :
2191 : : /* Prune expressions that are clobbered in block and thus become
2192 : : invalid if translated from ANTIC_OUT to ANTIC_IN. */
2193 : 15541351 : prune_clobbered_mems (ANTIC_OUT, block, any_max_on_edge);
2194 : :
2195 : : /* Generate ANTIC_OUT - TMP_GEN. */
2196 : 15541351 : S = bitmap_set_subtract_expressions (ANTIC_OUT, TMP_GEN (block));
2197 : :
2198 : : /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2199 : 31082702 : ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
2200 : 15541351 : TMP_GEN (block));
2201 : :
2202 : : /* Then union in the ANTIC_OUT - TMP_GEN values,
2203 : : to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2204 : 15541351 : bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
2205 : 15541351 : bitmap_ior_into (&ANTIC_IN (block)->expressions, &S->expressions);
2206 : :
2207 : : /* clean (ANTIC_IN (block)) is defered to after the iteration converged
2208 : : because it can cause non-convergence, see for example PR81181. */
2209 : :
2210 : : /* Intersect ANTIC_IN with the old ANTIC_IN. This is required until
2211 : : we properly represent the maximum expression set, thus not prune
2212 : : values without expressions during the iteration. */
2213 : 15541351 : if (was_visited
2214 : 15541351 : && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
2215 : : {
2216 : 3631 : if (dump_file && (dump_flags & TDF_DETAILS))
2217 : 0 : fprintf (dump_file, "warning: intersecting with old ANTIC_IN "
2218 : : "shrinks the set\n");
2219 : : /* Prune expressions not in the value set. */
2220 : 3631 : bitmap_iterator bi;
2221 : 3631 : unsigned int i;
2222 : 3631 : unsigned int to_clear = -1U;
2223 : 37127 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
2224 : : {
2225 : 33496 : if (to_clear != -1U)
2226 : : {
2227 : 1943 : bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2228 : 1943 : to_clear = -1U;
2229 : : }
2230 : 33496 : pre_expr expr = expression_for_id (i);
2231 : 33496 : unsigned int value_id = get_expr_value_id (expr);
2232 : 33496 : if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
2233 : 4397 : to_clear = i;
2234 : : }
2235 : 3631 : if (to_clear != -1U)
2236 : 2454 : bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2237 : : }
2238 : :
2239 : 15541351 : if (!bitmap_set_equal (old, ANTIC_IN (block)))
2240 : 10051703 : changed = true;
2241 : :
2242 : 5489648 : maybe_dump_sets:
2243 : 15545570 : if (dump_file && (dump_flags & TDF_DETAILS))
2244 : : {
2245 : 140 : if (changed)
2246 : 122 : fprintf (dump_file, "[changed] ");
2247 : 140 : print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2248 : : block->index);
2249 : :
2250 : 140 : if (S)
2251 : 140 : print_bitmap_set (dump_file, S, "S", block->index);
2252 : : }
2253 : 15545570 : if (old)
2254 : 15541351 : bitmap_set_free (old);
2255 : 15545570 : if (S)
2256 : 15541351 : bitmap_set_free (S);
2257 : 15545570 : if (ANTIC_OUT)
2258 : 15541351 : bitmap_set_free (ANTIC_OUT);
2259 : 15545570 : return changed;
2260 : : }
2261 : :
2262 : : /* Compute PARTIAL_ANTIC for BLOCK.
2263 : :
2264 : : If succs(BLOCK) > 1 then
2265 : : PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2266 : : in ANTIC_OUT for all succ(BLOCK)
2267 : : else if succs(BLOCK) == 1 then
2268 : : PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2269 : :
2270 : : PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
2271 : :
2272 : : */
2273 : : static void
2274 : 1124489 : compute_partial_antic_aux (basic_block block,
2275 : : bool block_has_abnormal_pred_edge)
2276 : : {
2277 : 1124489 : bitmap_set_t old_PA_IN;
2278 : 1124489 : bitmap_set_t PA_OUT;
2279 : 1124489 : edge e;
2280 : 1124489 : edge_iterator ei;
2281 : 1124489 : unsigned long max_pa = param_max_partial_antic_length;
2282 : :
2283 : 1124489 : old_PA_IN = PA_OUT = NULL;
2284 : :
2285 : : /* If any edges from predecessors are abnormal, antic_in is empty,
2286 : : so do nothing. */
2287 : 1124489 : if (block_has_abnormal_pred_edge)
2288 : 745 : goto maybe_dump_sets;
2289 : :
2290 : : /* If there are too many partially anticipatable values in the
2291 : : block, phi_translate_set can take an exponential time: stop
2292 : : before the translation starts. */
2293 : 1123744 : if (max_pa
2294 : 1040006 : && single_succ_p (block)
2295 : 1825992 : && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2296 : 449 : goto maybe_dump_sets;
2297 : :
2298 : 1123295 : old_PA_IN = PA_IN (block);
2299 : 1123295 : PA_OUT = bitmap_set_new ();
2300 : :
2301 : : /* If the block has no successors, ANTIC_OUT is empty. */
2302 : 1123295 : if (EDGE_COUNT (block->succs) == 0)
2303 : : ;
2304 : : /* If we have one successor, we could have some phi nodes to
2305 : : translate through. Note that we can't phi translate across DFS
2306 : : back edges in partial antic, because it uses a union operation on
2307 : : the successors. For recurrences like IV's, we will end up
2308 : : generating a new value in the set on each go around (i + 3 (VH.1)
2309 : : VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2310 : 1039559 : else if (single_succ_p (block))
2311 : : {
2312 : 701801 : e = single_succ_edge (block);
2313 : 701801 : if (!(e->flags & EDGE_DFS_BACK))
2314 : 628789 : phi_translate_set (PA_OUT, PA_IN (e->dest), e);
2315 : : }
2316 : : /* If we have multiple successors, we take the union of all of
2317 : : them. */
2318 : : else
2319 : : {
2320 : 337758 : size_t i;
2321 : :
2322 : 337758 : auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2323 : 1018390 : FOR_EACH_EDGE (e, ei, block->succs)
2324 : : {
2325 : 680632 : if (e->flags & EDGE_DFS_BACK)
2326 : 296 : continue;
2327 : 680336 : worklist.quick_push (e);
2328 : : }
2329 : 337758 : if (worklist.length () > 0)
2330 : : {
2331 : 1018094 : FOR_EACH_VEC_ELT (worklist, i, e)
2332 : : {
2333 : 680336 : unsigned int i;
2334 : 680336 : bitmap_iterator bi;
2335 : :
2336 : 680336 : if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2337 : : {
2338 : 699 : bitmap_set_t antic_in = bitmap_set_new ();
2339 : 699 : phi_translate_set (antic_in, ANTIC_IN (e->dest), e);
2340 : 1462 : FOR_EACH_EXPR_ID_IN_SET (antic_in, i, bi)
2341 : 763 : bitmap_value_insert_into_set (PA_OUT,
2342 : : expression_for_id (i));
2343 : 699 : bitmap_set_free (antic_in);
2344 : 699 : bitmap_set_t pa_in = bitmap_set_new ();
2345 : 699 : phi_translate_set (pa_in, PA_IN (e->dest), e);
2346 : 699 : FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2347 : 0 : bitmap_value_insert_into_set (PA_OUT,
2348 : : expression_for_id (i));
2349 : 699 : bitmap_set_free (pa_in);
2350 : : }
2351 : : else
2352 : : {
2353 : 4344516 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
2354 : 3664879 : bitmap_value_insert_into_set (PA_OUT,
2355 : : expression_for_id (i));
2356 : 7058602 : FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
2357 : 6378965 : bitmap_value_insert_into_set (PA_OUT,
2358 : : expression_for_id (i));
2359 : : }
2360 : : }
2361 : : }
2362 : 337758 : }
2363 : :
2364 : : /* Prune expressions that are clobbered in block and thus become
2365 : : invalid if translated from PA_OUT to PA_IN. */
2366 : 1123295 : prune_clobbered_mems (PA_OUT, block, false);
2367 : :
2368 : : /* PA_IN starts with PA_OUT - TMP_GEN.
2369 : : Then we subtract things from ANTIC_IN. */
2370 : 1123295 : PA_IN (block) = bitmap_set_subtract_expressions (PA_OUT, TMP_GEN (block));
2371 : :
2372 : : /* For partial antic, we want to put back in the phi results, since
2373 : : we will properly avoid making them partially antic over backedges. */
2374 : 1123295 : bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2375 : 1123295 : bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2376 : :
2377 : : /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2378 : 1123295 : bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2379 : :
2380 : 1123295 : clean (PA_IN (block), ANTIC_IN (block));
2381 : :
2382 : 1124489 : maybe_dump_sets:
2383 : 1124489 : if (dump_file && (dump_flags & TDF_DETAILS))
2384 : : {
2385 : 0 : if (PA_OUT)
2386 : 0 : print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2387 : :
2388 : 0 : print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2389 : : }
2390 : 1124489 : if (old_PA_IN)
2391 : 1123295 : bitmap_set_free (old_PA_IN);
2392 : 1124489 : if (PA_OUT)
2393 : 1123295 : bitmap_set_free (PA_OUT);
2394 : 1124489 : }
2395 : :
2396 : : /* Compute ANTIC and partial ANTIC sets. */
2397 : :
2398 : : static void
2399 : 948152 : compute_antic (void)
2400 : : {
2401 : 948152 : bool changed = true;
2402 : 948152 : int num_iterations = 0;
2403 : 948152 : basic_block block;
2404 : 948152 : int i;
2405 : 948152 : edge_iterator ei;
2406 : 948152 : edge e;
2407 : :
2408 : : /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2409 : : We pre-build the map of blocks with incoming abnormal edges here. */
2410 : 948152 : auto_sbitmap has_abnormal_preds (last_basic_block_for_fn (cfun));
2411 : 948152 : bitmap_clear (has_abnormal_preds);
2412 : :
2413 : 16144337 : FOR_ALL_BB_FN (block, cfun)
2414 : : {
2415 : 15196185 : BB_VISITED (block) = 0;
2416 : :
2417 : 34330954 : FOR_EACH_EDGE (e, ei, block->preds)
2418 : 19138035 : if (e->flags & EDGE_ABNORMAL)
2419 : : {
2420 : 3266 : bitmap_set_bit (has_abnormal_preds, block->index);
2421 : 3266 : break;
2422 : : }
2423 : :
2424 : : /* While we are here, give empty ANTIC_IN sets to each block. */
2425 : 15196185 : ANTIC_IN (block) = bitmap_set_new ();
2426 : 15196185 : if (do_partial_partial)
2427 : 1124489 : PA_IN (block) = bitmap_set_new ();
2428 : : }
2429 : :
2430 : : /* At the exit block we anticipate nothing. */
2431 : 948152 : BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2432 : :
2433 : : /* For ANTIC computation we need a postorder that also guarantees that
2434 : : a block with a single successor is visited after its successor.
2435 : : RPO on the inverted CFG has this property. */
2436 : 948152 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2437 : 948152 : int n = inverted_rev_post_order_compute (cfun, rpo);
2438 : :
2439 : 948152 : auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
2440 : 948152 : bitmap_clear (worklist);
2441 : 2756944 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2442 : 1808792 : bitmap_set_bit (worklist, e->src->index);
2443 : 2918313 : while (changed)
2444 : : {
2445 : 1970161 : if (dump_file && (dump_flags & TDF_DETAILS))
2446 : 28 : fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2447 : : /* ??? We need to clear our PHI translation cache here as the
2448 : : ANTIC sets shrink and we restrict valid translations to
2449 : : those having operands with leaders in ANTIC. Same below
2450 : : for PA ANTIC computation. */
2451 : 1970161 : num_iterations++;
2452 : 1970161 : changed = false;
2453 : 37657320 : for (i = 0; i < n; ++i)
2454 : : {
2455 : 35687159 : if (bitmap_bit_p (worklist, rpo[i]))
2456 : : {
2457 : 15545570 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2458 : 15545570 : bitmap_clear_bit (worklist, block->index);
2459 : 15545570 : if (compute_antic_aux (block,
2460 : 15545570 : bitmap_bit_p (has_abnormal_preds,
2461 : : block->index)))
2462 : : {
2463 : 32881863 : FOR_EACH_EDGE (e, ei, block->preds)
2464 : 18095277 : bitmap_set_bit (worklist, e->src->index);
2465 : : changed = true;
2466 : : }
2467 : : }
2468 : : }
2469 : : /* Theoretically possible, but *highly* unlikely. */
2470 : 1970161 : gcc_checking_assert (num_iterations < 500);
2471 : : }
2472 : :
2473 : : /* We have to clean after the dataflow problem converged as cleaning
2474 : : can cause non-convergence because it is based on expressions
2475 : : rather than values. */
2476 : 14248033 : FOR_EACH_BB_FN (block, cfun)
2477 : 13299881 : clean (ANTIC_IN (block));
2478 : :
2479 : 948152 : statistics_histogram_event (cfun, "compute_antic iterations",
2480 : : num_iterations);
2481 : :
2482 : 948152 : if (do_partial_partial)
2483 : : {
2484 : : /* For partial antic we ignore backedges and thus we do not need
2485 : : to perform any iteration when we process blocks in rpo. */
2486 : 1208225 : for (i = 0; i < n; ++i)
2487 : : {
2488 : 1124489 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2489 : 1124489 : compute_partial_antic_aux (block,
2490 : 1124489 : bitmap_bit_p (has_abnormal_preds,
2491 : : block->index));
2492 : : }
2493 : : }
2494 : :
2495 : 948152 : free (rpo);
2496 : 948152 : }
2497 : :
2498 : :
2499 : : /* Inserted expressions are placed onto this worklist, which is used
2500 : : for performing quick dead code elimination of insertions we made
2501 : : that didn't turn out to be necessary. */
2502 : : static bitmap inserted_exprs;
2503 : :
2504 : : /* The actual worker for create_component_ref_by_pieces. */
2505 : :
2506 : : static tree
2507 : 1124656 : create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2508 : : unsigned int *operand, gimple_seq *stmts)
2509 : : {
2510 : 1124656 : vn_reference_op_t currop = &ref->operands[*operand];
2511 : 1124656 : tree genop;
2512 : 1124656 : ++*operand;
2513 : 1124656 : switch (currop->opcode)
2514 : : {
2515 : 0 : case CALL_EXPR:
2516 : 0 : gcc_unreachable ();
2517 : :
2518 : 394483 : case MEM_REF:
2519 : 394483 : {
2520 : 394483 : tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2521 : : stmts);
2522 : 394483 : if (!baseop)
2523 : : return NULL_TREE;
2524 : 394483 : tree offset = currop->op0;
2525 : 394483 : if (TREE_CODE (baseop) == ADDR_EXPR
2526 : 394483 : && handled_component_p (TREE_OPERAND (baseop, 0)))
2527 : : {
2528 : 0 : poly_int64 off;
2529 : 0 : tree base;
2530 : 0 : base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2531 : : &off);
2532 : 0 : gcc_assert (base);
2533 : 0 : offset = int_const_binop (PLUS_EXPR, offset,
2534 : 0 : build_int_cst (TREE_TYPE (offset),
2535 : : off));
2536 : 0 : baseop = build_fold_addr_expr (base);
2537 : : }
2538 : 394483 : genop = build2 (MEM_REF, currop->type, baseop, offset);
2539 : 394483 : MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2540 : 394483 : MR_DEPENDENCE_BASE (genop) = currop->base;
2541 : 394483 : REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
2542 : 394483 : return genop;
2543 : : }
2544 : :
2545 : 0 : case TARGET_MEM_REF:
2546 : 0 : {
2547 : 0 : tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2548 : 0 : vn_reference_op_t nextop = &ref->operands[(*operand)++];
2549 : 0 : tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2550 : : stmts);
2551 : 0 : if (!baseop)
2552 : : return NULL_TREE;
2553 : 0 : if (currop->op0)
2554 : : {
2555 : 0 : genop0 = find_or_generate_expression (block, currop->op0, stmts);
2556 : 0 : if (!genop0)
2557 : : return NULL_TREE;
2558 : : }
2559 : 0 : if (nextop->op0)
2560 : : {
2561 : 0 : genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2562 : 0 : if (!genop1)
2563 : : return NULL_TREE;
2564 : : }
2565 : 0 : genop = build5 (TARGET_MEM_REF, currop->type,
2566 : : baseop, currop->op2, genop0, currop->op1, genop1);
2567 : :
2568 : 0 : MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2569 : 0 : MR_DEPENDENCE_BASE (genop) = currop->base;
2570 : 0 : return genop;
2571 : : }
2572 : :
2573 : 255962 : case ADDR_EXPR:
2574 : 255962 : if (currop->op0)
2575 : : {
2576 : 253961 : gcc_assert (is_gimple_min_invariant (currop->op0));
2577 : 253961 : return currop->op0;
2578 : : }
2579 : : /* Fallthrough. */
2580 : 6101 : case REALPART_EXPR:
2581 : 6101 : case IMAGPART_EXPR:
2582 : 6101 : case VIEW_CONVERT_EXPR:
2583 : 6101 : {
2584 : 6101 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2585 : : stmts);
2586 : 6101 : if (!genop0)
2587 : : return NULL_TREE;
2588 : 6101 : return build1 (currop->opcode, currop->type, genop0);
2589 : : }
2590 : :
2591 : 4 : case WITH_SIZE_EXPR:
2592 : 4 : {
2593 : 4 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2594 : : stmts);
2595 : 4 : if (!genop0)
2596 : : return NULL_TREE;
2597 : 4 : tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2598 : 4 : if (!genop1)
2599 : : return NULL_TREE;
2600 : 4 : return build2 (currop->opcode, currop->type, genop0, genop1);
2601 : : }
2602 : :
2603 : 3795 : case BIT_FIELD_REF:
2604 : 3795 : {
2605 : 3795 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2606 : : stmts);
2607 : 3795 : if (!genop0)
2608 : : return NULL_TREE;
2609 : 3795 : tree op1 = currop->op0;
2610 : 3795 : tree op2 = currop->op1;
2611 : 3795 : tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2612 : 3795 : REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
2613 : 3795 : return t;
2614 : : }
2615 : :
2616 : : /* For array ref vn_reference_op's, operand 1 of the array ref
2617 : : is op0 of the reference op and operand 3 of the array ref is
2618 : : op1. */
2619 : 58347 : case ARRAY_RANGE_REF:
2620 : 58347 : case ARRAY_REF:
2621 : 58347 : {
2622 : 58347 : tree genop0;
2623 : 58347 : tree genop1 = currop->op0;
2624 : 58347 : tree genop2 = currop->op1;
2625 : 58347 : tree genop3 = currop->op2;
2626 : 58347 : genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2627 : : stmts);
2628 : 58347 : if (!genop0)
2629 : : return NULL_TREE;
2630 : 58347 : genop1 = find_or_generate_expression (block, genop1, stmts);
2631 : 58347 : if (!genop1)
2632 : : return NULL_TREE;
2633 : 58347 : if (genop2)
2634 : : {
2635 : 58347 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2636 : : /* Drop zero minimum index if redundant. */
2637 : 58347 : if (integer_zerop (genop2)
2638 : 58347 : && (!domain_type
2639 : 57282 : || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2640 : : genop2 = NULL_TREE;
2641 : : else
2642 : : {
2643 : 638 : genop2 = find_or_generate_expression (block, genop2, stmts);
2644 : 638 : if (!genop2)
2645 : : return NULL_TREE;
2646 : : }
2647 : : }
2648 : 58347 : if (genop3)
2649 : : {
2650 : 58347 : tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2651 : : /* We can't always put a size in units of the element alignment
2652 : : here as the element alignment may be not visible. See
2653 : : PR43783. Simply drop the element size for constant
2654 : : sizes. */
2655 : 58347 : if ((TREE_CODE (genop3) == INTEGER_CST
2656 : 58343 : && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
2657 : 58343 : && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
2658 : 58343 : (wi::to_offset (genop3) * vn_ref_op_align_unit (currop))))
2659 : 58347 : || (TREE_CODE (genop3) == EXACT_DIV_EXPR
2660 : 0 : && TREE_CODE (TREE_OPERAND (genop3, 1)) == INTEGER_CST
2661 : 0 : && operand_equal_p (TREE_OPERAND (genop3, 0), TYPE_SIZE_UNIT (elmt_type))
2662 : 0 : && wi::eq_p (wi::to_offset (TREE_OPERAND (genop3, 1)),
2663 : 58343 : vn_ref_op_align_unit (currop))))
2664 : : genop3 = NULL_TREE;
2665 : : else
2666 : : {
2667 : 4 : genop3 = find_or_generate_expression (block, genop3, stmts);
2668 : 4 : if (!genop3)
2669 : : return NULL_TREE;
2670 : : }
2671 : : }
2672 : 58347 : return build4 (currop->opcode, currop->type, genop0, genop1,
2673 : 58347 : genop2, genop3);
2674 : : }
2675 : 263467 : case COMPONENT_REF:
2676 : 263467 : {
2677 : 263467 : tree op0;
2678 : 263467 : tree op1;
2679 : 263467 : tree genop2 = currop->op1;
2680 : 263467 : op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2681 : 263467 : if (!op0)
2682 : : return NULL_TREE;
2683 : : /* op1 should be a FIELD_DECL, which are represented by themselves. */
2684 : 263467 : op1 = currop->op0;
2685 : 263467 : if (genop2)
2686 : : {
2687 : 0 : genop2 = find_or_generate_expression (block, genop2, stmts);
2688 : 0 : if (!genop2)
2689 : : return NULL_TREE;
2690 : : }
2691 : 263467 : return build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2692 : : }
2693 : :
2694 : 142786 : case SSA_NAME:
2695 : 142786 : {
2696 : 142786 : genop = find_or_generate_expression (block, currop->op0, stmts);
2697 : 142786 : return genop;
2698 : : }
2699 : 1712 : case STRING_CST:
2700 : 1712 : case INTEGER_CST:
2701 : 1712 : case POLY_INT_CST:
2702 : 1712 : case COMPLEX_CST:
2703 : 1712 : case VECTOR_CST:
2704 : 1712 : case REAL_CST:
2705 : 1712 : case CONSTRUCTOR:
2706 : 1712 : case VAR_DECL:
2707 : 1712 : case PARM_DECL:
2708 : 1712 : case CONST_DECL:
2709 : 1712 : case RESULT_DECL:
2710 : 1712 : case FUNCTION_DECL:
2711 : 1712 : return currop->op0;
2712 : :
2713 : 0 : default:
2714 : 0 : gcc_unreachable ();
2715 : : }
2716 : : }
2717 : :
2718 : : /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2719 : : COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2720 : : trying to rename aggregates into ssa form directly, which is a no no.
2721 : :
2722 : : Thus, this routine doesn't create temporaries, it just builds a
2723 : : single access expression for the array, calling
2724 : : find_or_generate_expression to build the innermost pieces.
2725 : :
2726 : : This function is a subroutine of create_expression_by_pieces, and
2727 : : should not be called on it's own unless you really know what you
2728 : : are doing. */
2729 : :
2730 : : static tree
2731 : 394510 : create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2732 : : gimple_seq *stmts)
2733 : : {
2734 : 394510 : unsigned int op = 0;
2735 : 394510 : return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2736 : : }
2737 : :
2738 : : /* Find a simple leader for an expression, or generate one using
2739 : : create_expression_by_pieces from a NARY expression for the value.
2740 : : BLOCK is the basic_block we are looking for leaders in.
2741 : : OP is the tree expression to find a leader for or generate.
2742 : : Returns the leader or NULL_TREE on failure. */
2743 : :
2744 : : static tree
2745 : 829213 : find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2746 : : {
2747 : : /* Constants are always leaders. */
2748 : 829213 : if (is_gimple_min_invariant (op))
2749 : : return op;
2750 : :
2751 : 635248 : gcc_assert (TREE_CODE (op) == SSA_NAME);
2752 : 635248 : vn_ssa_aux_t info = VN_INFO (op);
2753 : 635248 : unsigned int lookfor = info->value_id;
2754 : 635248 : if (value_id_constant_p (lookfor))
2755 : 3 : return info->valnum;
2756 : :
2757 : 635245 : pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2758 : 635245 : if (leader)
2759 : : {
2760 : 602918 : if (leader->kind == NAME)
2761 : 602918 : return PRE_EXPR_NAME (leader);
2762 : 0 : else if (leader->kind == CONSTANT)
2763 : 0 : return PRE_EXPR_CONSTANT (leader);
2764 : :
2765 : : /* Defer. */
2766 : : return NULL_TREE;
2767 : : }
2768 : 32327 : gcc_assert (!value_id_constant_p (lookfor));
2769 : :
2770 : : /* It must be a complex expression, so generate it recursively. Note
2771 : : that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2772 : : where the insert algorithm fails to insert a required expression. */
2773 : 32327 : bitmap exprset = value_expressions[lookfor];
2774 : 32327 : bitmap_iterator bi;
2775 : 32327 : unsigned int i;
2776 : 48318 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2777 : : {
2778 : 44996 : pre_expr temp = expression_for_id (i);
2779 : : /* We cannot insert random REFERENCE expressions at arbitrary
2780 : : places. We can insert NARYs which eventually re-materializes
2781 : : its operand values. */
2782 : 44996 : if (temp->kind == NARY)
2783 : 29005 : return create_expression_by_pieces (block, temp, stmts,
2784 : 58010 : TREE_TYPE (op));
2785 : : }
2786 : :
2787 : : /* Defer. */
2788 : : return NULL_TREE;
2789 : : }
2790 : :
2791 : : /* Create an expression in pieces, so that we can handle very complex
2792 : : expressions that may be ANTIC, but not necessary GIMPLE.
2793 : : BLOCK is the basic block the expression will be inserted into,
2794 : : EXPR is the expression to insert (in value form)
2795 : : STMTS is a statement list to append the necessary insertions into.
2796 : :
2797 : : This function will die if we hit some value that shouldn't be
2798 : : ANTIC but is (IE there is no leader for it, or its components).
2799 : : The function returns NULL_TREE in case a different antic expression
2800 : : has to be inserted first.
2801 : : This function may also generate expressions that are themselves
2802 : : partially or fully redundant. Those that are will be either made
2803 : : fully redundant during the next iteration of insert (for partially
2804 : : redundant ones), or eliminated by eliminate (for fully redundant
2805 : : ones). */
2806 : :
2807 : : static tree
2808 : 2798478 : create_expression_by_pieces (basic_block block, pre_expr expr,
2809 : : gimple_seq *stmts, tree type)
2810 : : {
2811 : 2798478 : tree name;
2812 : 2798478 : tree folded;
2813 : 2798478 : gimple_seq forced_stmts = NULL;
2814 : 2798478 : unsigned int value_id;
2815 : 2798478 : gimple_stmt_iterator gsi;
2816 : 2798478 : tree exprtype = type ? type : get_expr_type (expr);
2817 : 2798478 : pre_expr nameexpr;
2818 : 2798478 : gassign *newstmt;
2819 : :
2820 : 2798478 : switch (expr->kind)
2821 : : {
2822 : : /* We may hit the NAME/CONSTANT case if we have to convert types
2823 : : that value numbering saw through. */
2824 : 718678 : case NAME:
2825 : 718678 : folded = PRE_EXPR_NAME (expr);
2826 : 718678 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
2827 : : return NULL_TREE;
2828 : 718664 : if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2829 : : return folded;
2830 : : break;
2831 : 1309396 : case CONSTANT:
2832 : 1309396 : {
2833 : 1309396 : folded = PRE_EXPR_CONSTANT (expr);
2834 : 1309396 : tree tem = fold_convert (exprtype, folded);
2835 : 1309396 : if (is_gimple_min_invariant (tem))
2836 : : return tem;
2837 : : break;
2838 : : }
2839 : 396960 : case REFERENCE:
2840 : 396960 : if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
2841 : : {
2842 : 2450 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2843 : 2450 : unsigned int operand = 1;
2844 : 2450 : vn_reference_op_t currop = &ref->operands[0];
2845 : 2450 : tree sc = NULL_TREE;
2846 : 2450 : tree fn = NULL_TREE;
2847 : 2450 : if (currop->op0)
2848 : : {
2849 : 2301 : fn = find_or_generate_expression (block, currop->op0, stmts);
2850 : 2301 : if (!fn)
2851 : 0 : return NULL_TREE;
2852 : : }
2853 : 2450 : if (currop->op1)
2854 : : {
2855 : 0 : sc = find_or_generate_expression (block, currop->op1, stmts);
2856 : 0 : if (!sc)
2857 : : return NULL_TREE;
2858 : : }
2859 : 4900 : auto_vec<tree> args (ref->operands.length () - 1);
2860 : 6399 : while (operand < ref->operands.length ())
2861 : : {
2862 : 3949 : tree arg = create_component_ref_by_pieces_1 (block, ref,
2863 : 3949 : &operand, stmts);
2864 : 3949 : if (!arg)
2865 : 0 : return NULL_TREE;
2866 : 3949 : args.quick_push (arg);
2867 : : }
2868 : 2450 : gcall *call;
2869 : 2450 : if (currop->op0)
2870 : : {
2871 : 2301 : call = gimple_build_call_vec (fn, args);
2872 : 2301 : gimple_call_set_fntype (call, currop->type);
2873 : : }
2874 : : else
2875 : 149 : call = gimple_build_call_internal_vec ((internal_fn)currop->clique,
2876 : : args);
2877 : 2450 : gimple_set_location (call, expr->loc);
2878 : 2450 : if (sc)
2879 : 0 : gimple_call_set_chain (call, sc);
2880 : 2450 : tree forcedname = make_ssa_name (ref->type);
2881 : 2450 : gimple_call_set_lhs (call, forcedname);
2882 : : /* There's no CCP pass after PRE which would re-compute alignment
2883 : : information so make sure we re-materialize this here. */
2884 : 2450 : if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED)
2885 : 0 : && args.length () - 2 <= 1
2886 : 0 : && tree_fits_uhwi_p (args[1])
2887 : 2450 : && (args.length () != 3 || tree_fits_uhwi_p (args[2])))
2888 : : {
2889 : 0 : unsigned HOST_WIDE_INT halign = tree_to_uhwi (args[1]);
2890 : 0 : unsigned HOST_WIDE_INT hmisalign
2891 : 0 : = args.length () == 3 ? tree_to_uhwi (args[2]) : 0;
2892 : 0 : if ((halign & (halign - 1)) == 0
2893 : 0 : && (hmisalign & ~(halign - 1)) == 0
2894 : 0 : && (unsigned int)halign != 0)
2895 : 0 : set_ptr_info_alignment (get_ptr_info (forcedname),
2896 : : halign, hmisalign);
2897 : : }
2898 : 2450 : gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
2899 : 2450 : gimple_seq_add_stmt_without_update (&forced_stmts, call);
2900 : 2450 : folded = forcedname;
2901 : 2450 : }
2902 : : else
2903 : : {
2904 : 394510 : folded = create_component_ref_by_pieces (block,
2905 : : PRE_EXPR_REFERENCE (expr),
2906 : : stmts);
2907 : 394510 : if (!folded)
2908 : : return NULL_TREE;
2909 : 394510 : name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2910 : 394510 : newstmt = gimple_build_assign (name, folded);
2911 : 394510 : gimple_set_location (newstmt, expr->loc);
2912 : 394510 : gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2913 : 394510 : gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
2914 : 394510 : folded = name;
2915 : : }
2916 : : break;
2917 : 373444 : case NARY:
2918 : 373444 : {
2919 : 373444 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2920 : 373444 : tree *genop = XALLOCAVEC (tree, nary->length);
2921 : 373444 : unsigned i;
2922 : 988746 : for (i = 0; i < nary->length; ++i)
2923 : : {
2924 : 625133 : genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2925 : 625133 : if (!genop[i])
2926 : : return NULL_TREE;
2927 : : /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2928 : : may have conversions stripped. */
2929 : 615302 : if (nary->opcode == POINTER_PLUS_EXPR)
2930 : : {
2931 : 106169 : if (i == 0)
2932 : 53130 : genop[i] = gimple_convert (&forced_stmts,
2933 : : nary->type, genop[i]);
2934 : 53039 : else if (i == 1)
2935 : 53039 : genop[i] = gimple_convert (&forced_stmts,
2936 : : sizetype, genop[i]);
2937 : : }
2938 : : else
2939 : 509133 : genop[i] = gimple_convert (&forced_stmts,
2940 : 509133 : TREE_TYPE (nary->op[i]), genop[i]);
2941 : : }
2942 : 363613 : if (nary->opcode == CONSTRUCTOR)
2943 : : {
2944 : 4 : vec<constructor_elt, va_gc> *elts = NULL;
2945 : 8 : for (i = 0; i < nary->length; ++i)
2946 : 4 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2947 : 4 : folded = build_constructor (nary->type, elts);
2948 : 4 : name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2949 : 4 : newstmt = gimple_build_assign (name, folded);
2950 : 4 : gimple_set_location (newstmt, expr->loc);
2951 : 4 : gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2952 : 4 : folded = name;
2953 : : }
2954 : : else
2955 : : {
2956 : 363609 : switch (nary->length)
2957 : : {
2958 : 113299 : case 1:
2959 : 113299 : folded = gimple_build (&forced_stmts, expr->loc,
2960 : : nary->opcode, nary->type, genop[0]);
2961 : 113299 : break;
2962 : 250167 : case 2:
2963 : 250167 : folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
2964 : : nary->type, genop[0], genop[1]);
2965 : 250167 : break;
2966 : 143 : case 3:
2967 : 143 : folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
2968 : : nary->type, genop[0], genop[1],
2969 : : genop[2]);
2970 : 143 : break;
2971 : 0 : default:
2972 : 0 : gcc_unreachable ();
2973 : : }
2974 : : }
2975 : : }
2976 : : break;
2977 : 0 : default:
2978 : 0 : gcc_unreachable ();
2979 : : }
2980 : :
2981 : 849323 : folded = gimple_convert (&forced_stmts, exprtype, folded);
2982 : :
2983 : : /* If there is nothing to insert, return the simplified result. */
2984 : 849323 : if (gimple_seq_empty_p (forced_stmts))
2985 : : return folded;
2986 : : /* If we simplified to a constant return it and discard eventually
2987 : : built stmts. */
2988 : 760535 : if (is_gimple_min_invariant (folded))
2989 : : {
2990 : 0 : gimple_seq_discard (forced_stmts);
2991 : 0 : return folded;
2992 : : }
2993 : : /* Likewise if we simplified to sth not queued for insertion. */
2994 : 760535 : bool found = false;
2995 : 760535 : gsi = gsi_last (forced_stmts);
2996 : 760535 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
2997 : : {
2998 : 760535 : gimple *stmt = gsi_stmt (gsi);
2999 : 760535 : tree forcedname = gimple_get_lhs (stmt);
3000 : 760535 : if (forcedname == folded)
3001 : : {
3002 : : found = true;
3003 : : break;
3004 : : }
3005 : : }
3006 : 760535 : if (! found)
3007 : : {
3008 : 0 : gimple_seq_discard (forced_stmts);
3009 : 0 : return folded;
3010 : : }
3011 : 760535 : gcc_assert (TREE_CODE (folded) == SSA_NAME);
3012 : :
3013 : : /* If we have any intermediate expressions to the value sets, add them
3014 : : to the value sets and chain them in the instruction stream. */
3015 : 760535 : if (forced_stmts)
3016 : : {
3017 : 760535 : gsi = gsi_start (forced_stmts);
3018 : 1521569 : for (; !gsi_end_p (gsi); gsi_next (&gsi))
3019 : : {
3020 : 761034 : gimple *stmt = gsi_stmt (gsi);
3021 : 761034 : tree forcedname = gimple_get_lhs (stmt);
3022 : 761034 : pre_expr nameexpr;
3023 : :
3024 : 761034 : if (forcedname != folded)
3025 : : {
3026 : 499 : vn_ssa_aux_t vn_info = VN_INFO (forcedname);
3027 : 499 : vn_info->valnum = forcedname;
3028 : 499 : vn_info->value_id = get_next_value_id ();
3029 : 499 : nameexpr = get_or_alloc_expr_for_name (forcedname);
3030 : 499 : add_to_value (vn_info->value_id, nameexpr);
3031 : 499 : if (NEW_SETS (block))
3032 : 499 : bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3033 : 499 : bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3034 : : }
3035 : :
3036 : 761034 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
3037 : : }
3038 : 760535 : gimple_seq_add_seq (stmts, forced_stmts);
3039 : : }
3040 : :
3041 : 760535 : name = folded;
3042 : :
3043 : : /* Fold the last statement. */
3044 : 760535 : gsi = gsi_last (*stmts);
3045 : 760535 : if (fold_stmt_inplace (&gsi))
3046 : 216958 : update_stmt (gsi_stmt (gsi));
3047 : :
3048 : : /* Add a value number to the temporary.
3049 : : The value may already exist in either NEW_SETS, or AVAIL_OUT, because
3050 : : we are creating the expression by pieces, and this particular piece of
3051 : : the expression may have been represented. There is no harm in replacing
3052 : : here. */
3053 : 760535 : value_id = get_expr_value_id (expr);
3054 : 760535 : vn_ssa_aux_t vn_info = VN_INFO (name);
3055 : 760535 : vn_info->value_id = value_id;
3056 : 760535 : vn_info->valnum = vn_valnum_from_value_id (value_id);
3057 : 760535 : if (vn_info->valnum == NULL_TREE)
3058 : 234214 : vn_info->valnum = name;
3059 : 760535 : gcc_assert (vn_info->valnum != NULL_TREE);
3060 : 760535 : nameexpr = get_or_alloc_expr_for_name (name);
3061 : 760535 : add_to_value (value_id, nameexpr);
3062 : 760535 : if (NEW_SETS (block))
3063 : 531280 : bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3064 : 760535 : bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3065 : :
3066 : 760535 : pre_stats.insertions++;
3067 : 760535 : if (dump_file && (dump_flags & TDF_DETAILS))
3068 : : {
3069 : 22 : fprintf (dump_file, "Inserted ");
3070 : 44 : print_gimple_stmt (dump_file, gsi_stmt (gsi_last (*stmts)), 0);
3071 : 22 : fprintf (dump_file, " in predecessor %d (%04d)\n",
3072 : : block->index, value_id);
3073 : : }
3074 : :
3075 : : return name;
3076 : : }
3077 : :
3078 : :
3079 : : /* Insert the to-be-made-available values of expression EXPRNUM for each
3080 : : predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3081 : : merge the result with a phi node, given the same value number as
3082 : : NODE. Return true if we have inserted new stuff. */
3083 : :
3084 : : static bool
3085 : 1855545 : insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3086 : : vec<pre_expr> &avail)
3087 : : {
3088 : 1855545 : pre_expr expr = expression_for_id (exprnum);
3089 : 1855545 : pre_expr newphi;
3090 : 1855545 : unsigned int val = get_expr_value_id (expr);
3091 : 1855545 : edge pred;
3092 : 1855545 : bool insertions = false;
3093 : 1855545 : bool nophi = false;
3094 : 1855545 : basic_block bprime;
3095 : 1855545 : pre_expr eprime;
3096 : 1855545 : edge_iterator ei;
3097 : 1855545 : tree type = get_expr_type (expr);
3098 : 1855545 : tree temp;
3099 : 1855545 : gphi *phi;
3100 : :
3101 : : /* Make sure we aren't creating an induction variable. */
3102 : 1855545 : if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3103 : : {
3104 : 1516560 : bool firstinsideloop = false;
3105 : 1516560 : bool secondinsideloop = false;
3106 : 4549680 : firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3107 : 1516560 : EDGE_PRED (block, 0)->src);
3108 : 4549680 : secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3109 : 1516560 : EDGE_PRED (block, 1)->src);
3110 : : /* Induction variables only have one edge inside the loop. */
3111 : 1516560 : if ((firstinsideloop ^ secondinsideloop)
3112 : 1444735 : && expr->kind != REFERENCE)
3113 : : {
3114 : 1370344 : if (dump_file && (dump_flags & TDF_DETAILS))
3115 : 56 : fprintf (dump_file, "Skipping insertion of phi for partial "
3116 : : "redundancy: Looks like an induction variable\n");
3117 : : nophi = true;
3118 : : }
3119 : : }
3120 : :
3121 : : /* Make the necessary insertions. */
3122 : 5777698 : FOR_EACH_EDGE (pred, ei, block->preds)
3123 : : {
3124 : : /* When we are not inserting a PHI node do not bother inserting
3125 : : into places that do not dominate the anticipated computations. */
3126 : 3922153 : if (nophi && !dominated_by_p (CDI_DOMINATORS, block, pred->src))
3127 : 1385271 : continue;
3128 : 2540218 : gimple_seq stmts = NULL;
3129 : 2540218 : tree builtexpr;
3130 : 2540218 : bprime = pred->src;
3131 : 2540218 : eprime = avail[pred->dest_idx];
3132 : 2540218 : builtexpr = create_expression_by_pieces (bprime, eprime,
3133 : : &stmts, type);
3134 : 2540218 : gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3135 : 2540218 : if (!gimple_seq_empty_p (stmts))
3136 : : {
3137 : 508793 : basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
3138 : 508793 : gcc_assert (! new_bb);
3139 : : insertions = true;
3140 : : }
3141 : 2540218 : if (!builtexpr)
3142 : : {
3143 : : /* We cannot insert a PHI node if we failed to insert
3144 : : on one edge. */
3145 : 3336 : nophi = true;
3146 : 3336 : continue;
3147 : : }
3148 : 2536882 : if (is_gimple_min_invariant (builtexpr))
3149 : 1309422 : avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
3150 : : else
3151 : 1227460 : avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3152 : : }
3153 : : /* If we didn't want a phi node, and we made insertions, we still have
3154 : : inserted new stuff, and thus return true. If we didn't want a phi node,
3155 : : and didn't make insertions, we haven't added anything new, so return
3156 : : false. */
3157 : 1855545 : if (nophi && insertions)
3158 : : return true;
3159 : 1845533 : else if (nophi && !insertions)
3160 : : return false;
3161 : :
3162 : : /* Now build a phi for the new variable. */
3163 : 481870 : temp = make_temp_ssa_name (type, NULL, "prephitmp");
3164 : 481870 : phi = create_phi_node (temp, block);
3165 : :
3166 : 481870 : vn_ssa_aux_t vn_info = VN_INFO (temp);
3167 : 481870 : vn_info->value_id = val;
3168 : 481870 : vn_info->valnum = vn_valnum_from_value_id (val);
3169 : 481870 : if (vn_info->valnum == NULL_TREE)
3170 : 99385 : vn_info->valnum = temp;
3171 : 481870 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3172 : 1647644 : FOR_EACH_EDGE (pred, ei, block->preds)
3173 : : {
3174 : 1165774 : pre_expr ae = avail[pred->dest_idx];
3175 : 1165774 : gcc_assert (get_expr_type (ae) == type
3176 : : || useless_type_conversion_p (type, get_expr_type (ae)));
3177 : 1165774 : if (ae->kind == CONSTANT)
3178 : 179793 : add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3179 : : pred, UNKNOWN_LOCATION);
3180 : : else
3181 : 985981 : add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3182 : : }
3183 : :
3184 : 481870 : newphi = get_or_alloc_expr_for_name (temp);
3185 : 481870 : add_to_value (val, newphi);
3186 : :
3187 : : /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3188 : : this insertion, since we test for the existence of this value in PHI_GEN
3189 : : before proceeding with the partial redundancy checks in insert_aux.
3190 : :
3191 : : The value may exist in AVAIL_OUT, in particular, it could be represented
3192 : : by the expression we are trying to eliminate, in which case we want the
3193 : : replacement to occur. If it's not existing in AVAIL_OUT, we want it
3194 : : inserted there.
3195 : :
3196 : : Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3197 : : this block, because if it did, it would have existed in our dominator's
3198 : : AVAIL_OUT, and would have been skipped due to the full redundancy check.
3199 : : */
3200 : :
3201 : 481870 : bitmap_insert_into_set (PHI_GEN (block), newphi);
3202 : 481870 : bitmap_value_replace_in_set (AVAIL_OUT (block),
3203 : : newphi);
3204 : 481870 : if (NEW_SETS (block))
3205 : 481870 : bitmap_insert_into_set (NEW_SETS (block), newphi);
3206 : :
3207 : : /* If we insert a PHI node for a conversion of another PHI node
3208 : : in the same basic-block try to preserve range information.
3209 : : This is important so that followup loop passes receive optimal
3210 : : number of iteration analysis results. See PR61743. */
3211 : 481870 : if (expr->kind == NARY
3212 : 183107 : && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3213 : 58713 : && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3214 : 58503 : && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3215 : 47300 : && INTEGRAL_TYPE_P (type)
3216 : 46373 : && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3217 : 45361 : && (TYPE_PRECISION (type)
3218 : 45361 : >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3219 : 518531 : && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3220 : : {
3221 : 23155 : int_range_max r;
3222 : 46310 : if (get_range_query (cfun)->range_of_expr (r, expr->u.nary->op[0])
3223 : 23155 : && !r.undefined_p ()
3224 : 23155 : && !r.varying_p ()
3225 : 46310 : && !wi::neg_p (r.lower_bound (), SIGNED)
3226 : 63911 : && !wi::neg_p (r.upper_bound (), SIGNED))
3227 : : {
3228 : : /* Just handle extension and sign-changes of all-positive ranges. */
3229 : 16952 : range_cast (r, type);
3230 : 16952 : set_range_info (temp, r);
3231 : : }
3232 : 23155 : }
3233 : :
3234 : 481870 : if (dump_file && (dump_flags & TDF_DETAILS))
3235 : : {
3236 : 10 : fprintf (dump_file, "Created phi ");
3237 : 10 : print_gimple_stmt (dump_file, phi, 0);
3238 : 10 : fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3239 : : }
3240 : 481870 : pre_stats.phis++;
3241 : 481870 : return true;
3242 : : }
3243 : :
3244 : :
3245 : :
3246 : : /* Perform insertion of partially redundant or hoistable values.
3247 : : For BLOCK, do the following:
3248 : : 1. Propagate the NEW_SETS of the dominator into the current block.
3249 : : If the block has multiple predecessors,
3250 : : 2a. Iterate over the ANTIC expressions for the block to see if
3251 : : any of them are partially redundant.
3252 : : 2b. If so, insert them into the necessary predecessors to make
3253 : : the expression fully redundant.
3254 : : 2c. Insert a new PHI merging the values of the predecessors.
3255 : : 2d. Insert the new PHI, and the new expressions, into the
3256 : : NEW_SETS set.
3257 : : If the block has multiple successors,
3258 : : 3a. Iterate over the ANTIC values for the block to see if
3259 : : any of them are good candidates for hoisting.
3260 : : 3b. If so, insert expressions computing the values in BLOCK,
3261 : : and add the new expressions into the NEW_SETS set.
3262 : : 4. Recursively call ourselves on the dominator children of BLOCK.
3263 : :
3264 : : Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3265 : : do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3266 : : done in do_hoist_insertion.
3267 : : */
3268 : :
3269 : : static bool
3270 : 3685117 : do_pre_regular_insertion (basic_block block, basic_block dom,
3271 : : vec<pre_expr> exprs)
3272 : : {
3273 : 3685117 : bool new_stuff = false;
3274 : 3685117 : pre_expr expr;
3275 : 3685117 : auto_vec<pre_expr, 2> avail;
3276 : 3685117 : int i;
3277 : :
3278 : 3685117 : avail.safe_grow (EDGE_COUNT (block->preds), true);
3279 : :
3280 : 25318440 : FOR_EACH_VEC_ELT (exprs, i, expr)
3281 : : {
3282 : 21633323 : if (expr->kind == NARY
3283 : 21633323 : || expr->kind == REFERENCE)
3284 : : {
3285 : 12379275 : unsigned int val;
3286 : 12379275 : bool by_some = false;
3287 : 12379275 : bool cant_insert = false;
3288 : 12379275 : bool all_same = true;
3289 : 12379275 : unsigned num_inserts = 0;
3290 : 12379275 : unsigned num_const = 0;
3291 : 12379275 : pre_expr first_s = NULL;
3292 : 12379275 : edge pred;
3293 : 12379275 : basic_block bprime;
3294 : 12379275 : pre_expr eprime = NULL;
3295 : 12379275 : edge_iterator ei;
3296 : 12379275 : pre_expr edoubleprime = NULL;
3297 : 12379275 : bool do_insertion = false;
3298 : :
3299 : 12379275 : val = get_expr_value_id (expr);
3300 : 24758550 : if (bitmap_set_contains_value (PHI_GEN (block), val))
3301 : 1068108 : continue;
3302 : 11602955 : if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3303 : : {
3304 : 291788 : if (dump_file && (dump_flags & TDF_DETAILS))
3305 : : {
3306 : 7 : fprintf (dump_file, "Found fully redundant value: ");
3307 : 7 : print_pre_expr (dump_file, expr);
3308 : 7 : fprintf (dump_file, "\n");
3309 : : }
3310 : 291788 : continue;
3311 : : }
3312 : :
3313 : 36778027 : FOR_EACH_EDGE (pred, ei, block->preds)
3314 : : {
3315 : 25467663 : unsigned int vprime;
3316 : :
3317 : : /* We should never run insertion for the exit block
3318 : : and so not come across fake pred edges. */
3319 : 25467663 : gcc_assert (!(pred->flags & EDGE_FAKE));
3320 : 25467663 : bprime = pred->src;
3321 : : /* We are looking at ANTIC_OUT of bprime. */
3322 : 25467663 : eprime = phi_translate (NULL, expr, ANTIC_IN (block), NULL, pred);
3323 : :
3324 : : /* eprime will generally only be NULL if the
3325 : : value of the expression, translated
3326 : : through the PHI for this predecessor, is
3327 : : undefined. If that is the case, we can't
3328 : : make the expression fully redundant,
3329 : : because its value is undefined along a
3330 : : predecessor path. We can thus break out
3331 : : early because it doesn't matter what the
3332 : : rest of the results are. */
3333 : 25467663 : if (eprime == NULL)
3334 : : {
3335 : 803 : avail[pred->dest_idx] = NULL;
3336 : 803 : cant_insert = true;
3337 : 803 : break;
3338 : : }
3339 : :
3340 : 25466860 : vprime = get_expr_value_id (eprime);
3341 : 25466860 : edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3342 : : vprime);
3343 : 25466860 : if (edoubleprime == NULL)
3344 : : {
3345 : 22964755 : avail[pred->dest_idx] = eprime;
3346 : 22964755 : all_same = false;
3347 : 22964755 : num_inserts++;
3348 : : }
3349 : : else
3350 : : {
3351 : 2502105 : avail[pred->dest_idx] = edoubleprime;
3352 : 2502105 : by_some = true;
3353 : 2502105 : if (edoubleprime->kind == CONSTANT)
3354 : 1646180 : num_const++;
3355 : : /* We want to perform insertions to remove a redundancy on
3356 : : a path in the CFG we want to optimize for speed. */
3357 : 2502105 : if (optimize_edge_for_speed_p (pred))
3358 : 2087196 : do_insertion = true;
3359 : 2502105 : if (first_s == NULL)
3360 : : first_s = edoubleprime;
3361 : 277555 : else if (!pre_expr_d::equal (first_s, edoubleprime))
3362 : 215929 : all_same = false;
3363 : : }
3364 : : }
3365 : : /* If we can insert it, it's not the same value
3366 : : already existing along every predecessor, and
3367 : : it's defined by some predecessor, it is
3368 : : partially redundant. */
3369 : 11311167 : if (!cant_insert && !all_same && by_some)
3370 : : {
3371 : : /* If the expression is redundant on all edges and we need
3372 : : to at most insert one copy from a constant do the PHI
3373 : : insertion even when not optimizing a path that's to be
3374 : : optimized for speed. */
3375 : 2221990 : if (num_inserts == 0 && num_const <= 1)
3376 : : do_insertion = true;
3377 : 2082508 : if (!do_insertion)
3378 : : {
3379 : 372651 : if (dump_file && (dump_flags & TDF_DETAILS))
3380 : : {
3381 : 0 : fprintf (dump_file, "Skipping partial redundancy for "
3382 : : "expression ");
3383 : 0 : print_pre_expr (dump_file, expr);
3384 : 0 : fprintf (dump_file, " (%04d), no redundancy on to be "
3385 : : "optimized for speed edge\n", val);
3386 : : }
3387 : : }
3388 : 1849339 : else if (dbg_cnt (treepre_insert))
3389 : : {
3390 : 1849339 : if (dump_file && (dump_flags & TDF_DETAILS))
3391 : : {
3392 : 66 : fprintf (dump_file, "Found partial redundancy for "
3393 : : "expression ");
3394 : 66 : print_pre_expr (dump_file, expr);
3395 : 66 : fprintf (dump_file, " (%04d)\n",
3396 : : get_expr_value_id (expr));
3397 : : }
3398 : 1849339 : if (insert_into_preds_of_block (block,
3399 : : get_expression_id (expr),
3400 : : avail))
3401 : 11311167 : new_stuff = true;
3402 : : }
3403 : : }
3404 : : /* If all edges produce the same value and that value is
3405 : : an invariant, then the PHI has the same value on all
3406 : : edges. Note this. */
3407 : 9089177 : else if (!cant_insert
3408 : 9089177 : && all_same
3409 : 9089177 : && (edoubleprime->kind != NAME
3410 : 1182 : || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI
3411 : : (PRE_EXPR_NAME (edoubleprime))))
3412 : : {
3413 : 2537 : gcc_assert (edoubleprime->kind == CONSTANT
3414 : : || edoubleprime->kind == NAME);
3415 : :
3416 : 2537 : tree temp = make_temp_ssa_name (get_expr_type (expr),
3417 : : NULL, "pretmp");
3418 : 2537 : gassign *assign
3419 : 2537 : = gimple_build_assign (temp,
3420 : 2537 : edoubleprime->kind == CONSTANT ?
3421 : : PRE_EXPR_CONSTANT (edoubleprime) :
3422 : : PRE_EXPR_NAME (edoubleprime));
3423 : 2537 : gimple_stmt_iterator gsi = gsi_after_labels (block);
3424 : 2537 : gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3425 : :
3426 : 2537 : vn_ssa_aux_t vn_info = VN_INFO (temp);
3427 : 2537 : vn_info->value_id = val;
3428 : 2537 : vn_info->valnum = vn_valnum_from_value_id (val);
3429 : 2537 : if (vn_info->valnum == NULL_TREE)
3430 : 483 : vn_info->valnum = temp;
3431 : 2537 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3432 : 2537 : pre_expr newe = get_or_alloc_expr_for_name (temp);
3433 : 2537 : add_to_value (val, newe);
3434 : 2537 : bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3435 : 2537 : bitmap_insert_into_set (NEW_SETS (block), newe);
3436 : 2537 : bitmap_insert_into_set (PHI_GEN (block), newe);
3437 : : }
3438 : : }
3439 : : }
3440 : :
3441 : 3685117 : return new_stuff;
3442 : 3685117 : }
3443 : :
3444 : :
3445 : : /* Perform insertion for partially anticipatable expressions. There
3446 : : is only one case we will perform insertion for these. This case is
3447 : : if the expression is partially anticipatable, and fully available.
3448 : : In this case, we know that putting it earlier will enable us to
3449 : : remove the later computation. */
3450 : :
3451 : : static bool
3452 : 295811 : do_pre_partial_partial_insertion (basic_block block, basic_block dom,
3453 : : vec<pre_expr> exprs)
3454 : : {
3455 : 295811 : bool new_stuff = false;
3456 : 295811 : pre_expr expr;
3457 : 295811 : auto_vec<pre_expr, 2> avail;
3458 : 295811 : int i;
3459 : :
3460 : 295811 : avail.safe_grow (EDGE_COUNT (block->preds), true);
3461 : :
3462 : 2778423 : FOR_EACH_VEC_ELT (exprs, i, expr)
3463 : : {
3464 : 2482612 : if (expr->kind == NARY
3465 : 2482612 : || expr->kind == REFERENCE)
3466 : : {
3467 : 1859288 : unsigned int val;
3468 : 1859288 : bool by_all = true;
3469 : 1859288 : bool cant_insert = false;
3470 : 1859288 : edge pred;
3471 : 1859288 : basic_block bprime;
3472 : 1859288 : pre_expr eprime = NULL;
3473 : 1859288 : edge_iterator ei;
3474 : :
3475 : 1859288 : val = get_expr_value_id (expr);
3476 : 3718576 : if (bitmap_set_contains_value (PHI_GEN (block), val))
3477 : 56850 : continue;
3478 : 1849767 : if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3479 : 47329 : continue;
3480 : :
3481 : 1882673 : FOR_EACH_EDGE (pred, ei, block->preds)
3482 : : {
3483 : 1872512 : unsigned int vprime;
3484 : 1872512 : pre_expr edoubleprime;
3485 : :
3486 : : /* We should never run insertion for the exit block
3487 : : and so not come across fake pred edges. */
3488 : 1872512 : gcc_assert (!(pred->flags & EDGE_FAKE));
3489 : 1872512 : bprime = pred->src;
3490 : 3745024 : eprime = phi_translate (NULL, expr, ANTIC_IN (block),
3491 : 1872512 : PA_IN (block), pred);
3492 : :
3493 : : /* eprime will generally only be NULL if the
3494 : : value of the expression, translated
3495 : : through the PHI for this predecessor, is
3496 : : undefined. If that is the case, we can't
3497 : : make the expression fully redundant,
3498 : : because its value is undefined along a
3499 : : predecessor path. We can thus break out
3500 : : early because it doesn't matter what the
3501 : : rest of the results are. */
3502 : 1872512 : if (eprime == NULL)
3503 : : {
3504 : 44 : avail[pred->dest_idx] = NULL;
3505 : 44 : cant_insert = true;
3506 : 44 : break;
3507 : : }
3508 : :
3509 : 1872468 : vprime = get_expr_value_id (eprime);
3510 : 1872468 : edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3511 : 1872468 : avail[pred->dest_idx] = edoubleprime;
3512 : 1872468 : if (edoubleprime == NULL)
3513 : : {
3514 : : by_all = false;
3515 : : break;
3516 : : }
3517 : : }
3518 : :
3519 : : /* If we can insert it, it's not the same value
3520 : : already existing along every predecessor, and
3521 : : it's defined by some predecessor, it is
3522 : : partially redundant. */
3523 : 1802438 : if (!cant_insert && by_all)
3524 : : {
3525 : 10161 : edge succ;
3526 : 10161 : bool do_insertion = false;
3527 : :
3528 : : /* Insert only if we can remove a later expression on a path
3529 : : that we want to optimize for speed.
3530 : : The phi node that we will be inserting in BLOCK is not free,
3531 : : and inserting it for the sake of !optimize_for_speed successor
3532 : : may cause regressions on the speed path. */
3533 : 27584 : FOR_EACH_EDGE (succ, ei, block->succs)
3534 : : {
3535 : 17423 : if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3536 : 17423 : || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3537 : : {
3538 : 8976 : if (optimize_edge_for_speed_p (succ))
3539 : 17423 : do_insertion = true;
3540 : : }
3541 : : }
3542 : :
3543 : 10161 : if (!do_insertion)
3544 : : {
3545 : 3955 : if (dump_file && (dump_flags & TDF_DETAILS))
3546 : : {
3547 : 0 : fprintf (dump_file, "Skipping partial partial redundancy "
3548 : : "for expression ");
3549 : 0 : print_pre_expr (dump_file, expr);
3550 : 0 : fprintf (dump_file, " (%04d), not (partially) anticipated "
3551 : : "on any to be optimized for speed edges\n", val);
3552 : : }
3553 : : }
3554 : 6206 : else if (dbg_cnt (treepre_insert))
3555 : : {
3556 : 6206 : pre_stats.pa_insert++;
3557 : 6206 : if (dump_file && (dump_flags & TDF_DETAILS))
3558 : : {
3559 : 0 : fprintf (dump_file, "Found partial partial redundancy "
3560 : : "for expression ");
3561 : 0 : print_pre_expr (dump_file, expr);
3562 : 0 : fprintf (dump_file, " (%04d)\n",
3563 : : get_expr_value_id (expr));
3564 : : }
3565 : 6206 : if (insert_into_preds_of_block (block,
3566 : : get_expression_id (expr),
3567 : : avail))
3568 : 10161 : new_stuff = true;
3569 : : }
3570 : : }
3571 : : }
3572 : : }
3573 : :
3574 : 295811 : return new_stuff;
3575 : 295811 : }
3576 : :
3577 : : /* Insert expressions in BLOCK to compute hoistable values up.
3578 : : Return TRUE if something was inserted, otherwise return FALSE.
3579 : : The caller has to make sure that BLOCK has at least two successors. */
3580 : :
3581 : : static bool
3582 : 4774631 : do_hoist_insertion (basic_block block)
3583 : : {
3584 : 4774631 : edge e;
3585 : 4774631 : edge_iterator ei;
3586 : 4774631 : bool new_stuff = false;
3587 : 4774631 : unsigned i;
3588 : 4774631 : gimple_stmt_iterator last;
3589 : :
3590 : : /* At least two successors, or else... */
3591 : 4774631 : gcc_assert (EDGE_COUNT (block->succs) >= 2);
3592 : :
3593 : : /* Check that all successors of BLOCK are dominated by block.
3594 : : We could use dominated_by_p() for this, but actually there is a much
3595 : : quicker check: any successor that is dominated by BLOCK can't have
3596 : : more than one predecessor edge. */
3597 : 14432934 : FOR_EACH_EDGE (e, ei, block->succs)
3598 : 14284689 : if (! single_pred_p (e->dest))
3599 : : return false;
3600 : :
3601 : : /* Determine the insertion point. If we cannot safely insert before
3602 : : the last stmt if we'd have to, bail out. */
3603 : 4767488 : last = gsi_last_bb (block);
3604 : 4767488 : if (!gsi_end_p (last)
3605 : 4767064 : && !is_ctrl_stmt (gsi_stmt (last))
3606 : 5421232 : && stmt_ends_bb_p (gsi_stmt (last)))
3607 : : return false;
3608 : :
3609 : : /* We have multiple successors, compute ANTIC_OUT by taking the intersection
3610 : : of all of ANTIC_IN translating through PHI nodes. Track the union
3611 : : of the expression sets so we can pick a representative that is
3612 : : fully generatable out of hoistable expressions. */
3613 : 4114333 : bitmap_set_t ANTIC_OUT = bitmap_set_new ();
3614 : 4114333 : bool first = true;
3615 : 12460208 : FOR_EACH_EDGE (e, ei, block->succs)
3616 : : {
3617 : 8345875 : if (first)
3618 : : {
3619 : 4114333 : phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
3620 : 4114333 : first = false;
3621 : : }
3622 : 4231542 : else if (!gimple_seq_empty_p (phi_nodes (e->dest)))
3623 : : {
3624 : 1 : bitmap_set_t tmp = bitmap_set_new ();
3625 : 1 : phi_translate_set (tmp, ANTIC_IN (e->dest), e);
3626 : 1 : bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
3627 : 1 : bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
3628 : 1 : bitmap_set_free (tmp);
3629 : : }
3630 : : else
3631 : : {
3632 : 4231541 : bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
3633 : 4231541 : bitmap_ior_into (&ANTIC_OUT->expressions,
3634 : 4231541 : &ANTIC_IN (e->dest)->expressions);
3635 : : }
3636 : : }
3637 : :
3638 : : /* Compute the set of hoistable expressions from ANTIC_OUT. First compute
3639 : : hoistable values. */
3640 : 4114333 : bitmap_set hoistable_set;
3641 : :
3642 : : /* A hoistable value must be in ANTIC_OUT(block)
3643 : : but not in AVAIL_OUT(BLOCK). */
3644 : 4114333 : bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
3645 : 4114333 : bitmap_and_compl (&hoistable_set.values,
3646 : 4114333 : &ANTIC_OUT->values, &AVAIL_OUT (block)->values);
3647 : :
3648 : : /* Short-cut for a common case: hoistable_set is empty. */
3649 : 4114333 : if (bitmap_empty_p (&hoistable_set.values))
3650 : : {
3651 : 3381798 : bitmap_set_free (ANTIC_OUT);
3652 : 3381798 : return false;
3653 : : }
3654 : :
3655 : : /* Compute which of the hoistable values is in AVAIL_OUT of
3656 : : at least one of the successors of BLOCK. */
3657 : 732535 : bitmap_head availout_in_some;
3658 : 732535 : bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
3659 : 2203750 : FOR_EACH_EDGE (e, ei, block->succs)
3660 : : /* Do not consider expressions solely because their availability
3661 : : on loop exits. They'd be ANTIC-IN throughout the whole loop
3662 : : and thus effectively hoisted across loops by combination of
3663 : : PRE and hoisting. */
3664 : 1471215 : if (! loop_exit_edge_p (block->loop_father, e))
3665 : 1308499 : bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
3666 : 1308499 : &AVAIL_OUT (e->dest)->values);
3667 : 732535 : bitmap_clear (&hoistable_set.values);
3668 : :
3669 : : /* Short-cut for a common case: availout_in_some is empty. */
3670 : 732535 : if (bitmap_empty_p (&availout_in_some))
3671 : : {
3672 : 584290 : bitmap_set_free (ANTIC_OUT);
3673 : 584290 : return false;
3674 : : }
3675 : :
3676 : : /* Hack hoistable_set in-place so we can use sorted_array_from_bitmap_set. */
3677 : 148245 : bitmap_move (&hoistable_set.values, &availout_in_some);
3678 : 148245 : hoistable_set.expressions = ANTIC_OUT->expressions;
3679 : :
3680 : : /* Now finally construct the topological-ordered expression set. */
3681 : 148245 : vec<pre_expr> exprs = sorted_array_from_bitmap_set (&hoistable_set);
3682 : :
3683 : : /* If there are candidate values for hoisting, insert expressions
3684 : : strategically to make the hoistable expressions fully redundant. */
3685 : 148245 : pre_expr expr;
3686 : 440427 : FOR_EACH_VEC_ELT (exprs, i, expr)
3687 : : {
3688 : : /* While we try to sort expressions topologically above the
3689 : : sorting doesn't work out perfectly. Catch expressions we
3690 : : already inserted. */
3691 : 292182 : unsigned int value_id = get_expr_value_id (expr);
3692 : 584364 : if (bitmap_set_contains_value (AVAIL_OUT (block), value_id))
3693 : : {
3694 : 62909 : if (dump_file && (dump_flags & TDF_DETAILS))
3695 : : {
3696 : 1 : fprintf (dump_file,
3697 : : "Already inserted expression for ");
3698 : 1 : print_pre_expr (dump_file, expr);
3699 : 1 : fprintf (dump_file, " (%04d)\n", value_id);
3700 : : }
3701 : 62927 : continue;
3702 : : }
3703 : :
3704 : : /* If we end up with a punned expression representation and this
3705 : : happens to be a float typed one give up - we can't know for
3706 : : sure whether all paths perform the floating-point load we are
3707 : : about to insert and on some targets this can cause correctness
3708 : : issues. See PR88240. */
3709 : 229273 : if (expr->kind == REFERENCE
3710 : 102951 : && PRE_EXPR_REFERENCE (expr)->punned
3711 : 229463 : && FLOAT_TYPE_P (get_expr_type (expr)))
3712 : 0 : continue;
3713 : :
3714 : : /* Only hoist if the full expression is available for hoisting.
3715 : : This avoids hoisting values that are not common and for
3716 : : example evaluate an expression that's not valid to evaluate
3717 : : unconditionally (PR112310). */
3718 : 229273 : if (!valid_in_sets (&hoistable_set, AVAIL_OUT (block), expr))
3719 : 18 : continue;
3720 : :
3721 : : /* OK, we should hoist this value. Perform the transformation. */
3722 : 229255 : pre_stats.hoist_insert++;
3723 : 229255 : if (dump_file && (dump_flags & TDF_DETAILS))
3724 : : {
3725 : 4 : fprintf (dump_file,
3726 : : "Inserting expression in block %d for code hoisting: ",
3727 : : block->index);
3728 : 4 : print_pre_expr (dump_file, expr);
3729 : 4 : fprintf (dump_file, " (%04d)\n", value_id);
3730 : : }
3731 : :
3732 : 229255 : gimple_seq stmts = NULL;
3733 : 229255 : tree res = create_expression_by_pieces (block, expr, &stmts,
3734 : : get_expr_type (expr));
3735 : :
3736 : : /* Do not return true if expression creation ultimately
3737 : : did not insert any statements. */
3738 : 229255 : if (gimple_seq_empty_p (stmts))
3739 : : res = NULL_TREE;
3740 : : else
3741 : : {
3742 : 229255 : if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
3743 : 229255 : gsi_insert_seq_before (&last, stmts, GSI_SAME_STMT);
3744 : : else
3745 : 0 : gsi_insert_seq_after (&last, stmts, GSI_NEW_STMT);
3746 : : }
3747 : :
3748 : : /* Make sure to not return true if expression creation ultimately
3749 : : failed but also make sure to insert any stmts produced as they
3750 : : are tracked in inserted_exprs. */
3751 : 229255 : if (! res)
3752 : 0 : continue;
3753 : :
3754 : 229255 : new_stuff = true;
3755 : : }
3756 : :
3757 : 148245 : exprs.release ();
3758 : 148245 : bitmap_clear (&hoistable_set.values);
3759 : 148245 : bitmap_set_free (ANTIC_OUT);
3760 : :
3761 : 148245 : return new_stuff;
3762 : : }
3763 : :
3764 : : /* Perform insertion of partially redundant and hoistable values. */
3765 : :
3766 : : static void
3767 : 948152 : insert (void)
3768 : : {
3769 : 948152 : basic_block bb;
3770 : :
3771 : 16144337 : FOR_ALL_BB_FN (bb, cfun)
3772 : 15196185 : NEW_SETS (bb) = bitmap_set_new ();
3773 : :
3774 : 948152 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3775 : 948152 : int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun) + 1);
3776 : 948152 : int rpo_num = pre_and_rev_post_order_compute (NULL, rpo, false);
3777 : 14248033 : for (int i = 0; i < rpo_num; ++i)
3778 : 13299881 : bb_rpo[rpo[i]] = i;
3779 : :
3780 : : int num_iterations = 0;
3781 : 999352 : bool changed;
3782 : 999352 : do
3783 : : {
3784 : 999352 : num_iterations++;
3785 : 999352 : if (dump_file && dump_flags & TDF_DETAILS)
3786 : 18 : fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3787 : :
3788 : : changed = false;
3789 : 18375858 : for (int idx = 0; idx < rpo_num; ++idx)
3790 : : {
3791 : 17376506 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
3792 : 17376506 : basic_block dom = get_immediate_dominator (CDI_DOMINATORS, block);
3793 : 17376506 : if (dom)
3794 : : {
3795 : 17376506 : unsigned i;
3796 : 17376506 : bitmap_iterator bi;
3797 : 17376506 : bitmap_set_t newset;
3798 : :
3799 : : /* First, update the AVAIL_OUT set with anything we may have
3800 : : inserted higher up in the dominator tree. */
3801 : 17376506 : newset = NEW_SETS (dom);
3802 : :
3803 : : /* Note that we need to value_replace both NEW_SETS, and
3804 : : AVAIL_OUT. For both the case of NEW_SETS, the value may be
3805 : : represented by some non-simple expression here that we want
3806 : : to replace it with. */
3807 : 17376506 : bool avail_out_changed = false;
3808 : 32479508 : FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3809 : : {
3810 : 15103002 : pre_expr expr = expression_for_id (i);
3811 : 15103002 : bitmap_value_replace_in_set (NEW_SETS (block), expr);
3812 : 15103002 : avail_out_changed
3813 : 15103002 : |= bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3814 : : }
3815 : : /* We need to iterate if AVAIL_OUT of an already processed
3816 : : block source changed. */
3817 : 17376506 : if (avail_out_changed && !changed)
3818 : : {
3819 : 1713260 : edge_iterator ei;
3820 : 1713260 : edge e;
3821 : 4080201 : FOR_EACH_EDGE (e, ei, block->succs)
3822 : 2366941 : if (e->dest->index != EXIT_BLOCK
3823 : 2265177 : && bb_rpo[e->dest->index] < idx)
3824 : 2366941 : changed = true;
3825 : : }
3826 : :
3827 : : /* Insert expressions for partial redundancies. */
3828 : 34752449 : if (flag_tree_pre && !single_pred_p (block))
3829 : : {
3830 : 3410753 : vec<pre_expr> exprs
3831 : 3410753 : = sorted_array_from_bitmap_set (ANTIC_IN (block));
3832 : : /* Sorting is not perfect, iterate locally. */
3833 : 7095870 : while (do_pre_regular_insertion (block, dom, exprs))
3834 : : ;
3835 : 3410753 : exprs.release ();
3836 : 3410753 : if (do_partial_partial)
3837 : : {
3838 : 292768 : exprs = sorted_array_from_bitmap_set (PA_IN (block));
3839 : 588579 : while (do_pre_partial_partial_insertion (block, dom,
3840 : : exprs))
3841 : : ;
3842 : 292768 : exprs.release ();
3843 : : }
3844 : : }
3845 : : }
3846 : : }
3847 : :
3848 : : /* Clear the NEW sets before the next iteration. We have already
3849 : : fully propagated its contents. */
3850 : 999352 : if (changed)
3851 : 4230225 : FOR_ALL_BB_FN (bb, cfun)
3852 : 8358050 : bitmap_set_free (NEW_SETS (bb));
3853 : : }
3854 : : while (changed);
3855 : :
3856 : 948152 : statistics_histogram_event (cfun, "insert iterations", num_iterations);
3857 : :
3858 : : /* AVAIL_OUT is not needed after insertion so we don't have to
3859 : : propagate NEW_SETS from hoist insertion. */
3860 : 16144337 : FOR_ALL_BB_FN (bb, cfun)
3861 : : {
3862 : 15196185 : bitmap_set_free (NEW_SETS (bb));
3863 : 15196185 : bitmap_set_pool.remove (NEW_SETS (bb));
3864 : 15196185 : NEW_SETS (bb) = NULL;
3865 : : }
3866 : :
3867 : : /* Insert expressions for hoisting. Do a backward walk here since
3868 : : inserting into BLOCK exposes new opportunities in its predecessors.
3869 : : Since PRE and hoist insertions can cause back-to-back iteration
3870 : : and we are interested in PRE insertion exposed hoisting opportunities
3871 : : but not in hoisting exposed PRE ones do hoist insertion only after
3872 : : PRE insertion iteration finished and do not iterate it. */
3873 : 948152 : if (flag_code_hoisting)
3874 : 14247506 : for (int idx = rpo_num - 1; idx >= 0; --idx)
3875 : : {
3876 : 13299406 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
3877 : 18074037 : if (EDGE_COUNT (block->succs) >= 2)
3878 : 4774631 : changed |= do_hoist_insertion (block);
3879 : : }
3880 : :
3881 : 948152 : free (rpo);
3882 : 948152 : free (bb_rpo);
3883 : 948152 : }
3884 : :
3885 : :
3886 : : /* Compute the AVAIL set for all basic blocks.
3887 : :
3888 : : This function performs value numbering of the statements in each basic
3889 : : block. The AVAIL sets are built from information we glean while doing
3890 : : this value numbering, since the AVAIL sets contain only one entry per
3891 : : value.
3892 : :
3893 : : AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3894 : : AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3895 : :
3896 : : static void
3897 : 948152 : compute_avail (function *fun)
3898 : : {
3899 : :
3900 : 948152 : basic_block block, son;
3901 : 948152 : basic_block *worklist;
3902 : 948152 : size_t sp = 0;
3903 : 948152 : unsigned i;
3904 : 948152 : tree name;
3905 : :
3906 : : /* We pretend that default definitions are defined in the entry block.
3907 : : This includes function arguments and the static chain decl. */
3908 : 46830214 : FOR_EACH_SSA_NAME (i, name, fun)
3909 : : {
3910 : 34018807 : pre_expr e;
3911 : 34018807 : if (!SSA_NAME_IS_DEFAULT_DEF (name)
3912 : 2921692 : || has_zero_uses (name)
3913 : 36371059 : || virtual_operand_p (name))
3914 : 32613923 : continue;
3915 : :
3916 : 1404884 : e = get_or_alloc_expr_for_name (name);
3917 : 1404884 : add_to_value (get_expr_value_id (e), e);
3918 : 1404884 : bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun)), e);
3919 : 1404884 : bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun)),
3920 : : e);
3921 : : }
3922 : :
3923 : 948152 : if (dump_file && (dump_flags & TDF_DETAILS))
3924 : : {
3925 : 14 : print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun)),
3926 : : "tmp_gen", ENTRY_BLOCK);
3927 : 14 : print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun)),
3928 : : "avail_out", ENTRY_BLOCK);
3929 : : }
3930 : :
3931 : : /* Allocate the worklist. */
3932 : 948152 : worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
3933 : :
3934 : : /* Seed the algorithm by putting the dominator children of the entry
3935 : : block on the worklist. */
3936 : 948152 : for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (fun));
3937 : 1896304 : son;
3938 : 948152 : son = next_dom_son (CDI_DOMINATORS, son))
3939 : 948152 : worklist[sp++] = son;
3940 : :
3941 : 1896304 : BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (fun))
3942 : 948152 : = ssa_default_def (fun, gimple_vop (fun));
3943 : :
3944 : : /* Loop until the worklist is empty. */
3945 : 14248033 : while (sp)
3946 : : {
3947 : 13299881 : gimple *stmt;
3948 : 13299881 : basic_block dom;
3949 : :
3950 : : /* Pick a block from the worklist. */
3951 : 13299881 : block = worklist[--sp];
3952 : 13299881 : vn_context_bb = block;
3953 : :
3954 : : /* Initially, the set of available values in BLOCK is that of
3955 : : its immediate dominator. */
3956 : 13299881 : dom = get_immediate_dominator (CDI_DOMINATORS, block);
3957 : 13299881 : if (dom)
3958 : : {
3959 : 13299881 : bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3960 : 13299881 : BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
3961 : : }
3962 : :
3963 : : /* Generate values for PHI nodes. */
3964 : 17188238 : for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
3965 : 3888357 : gsi_next (&gsi))
3966 : : {
3967 : 3888357 : tree result = gimple_phi_result (gsi.phi ());
3968 : :
3969 : : /* We have no need for virtual phis, as they don't represent
3970 : : actual computations. */
3971 : 7776714 : if (virtual_operand_p (result))
3972 : : {
3973 : 1767875 : BB_LIVE_VOP_ON_EXIT (block) = result;
3974 : 1767875 : continue;
3975 : : }
3976 : :
3977 : 2120482 : pre_expr e = get_or_alloc_expr_for_name (result);
3978 : 2120482 : add_to_value (get_expr_value_id (e), e);
3979 : 2120482 : bitmap_value_insert_into_set (AVAIL_OUT (block), e);
3980 : 2120482 : bitmap_insert_into_set (PHI_GEN (block), e);
3981 : : }
3982 : :
3983 : 13299881 : BB_MAY_NOTRETURN (block) = 0;
3984 : :
3985 : : /* Now compute value numbers and populate value sets with all
3986 : : the expressions computed in BLOCK. */
3987 : 13299881 : bool set_bb_may_notreturn = false;
3988 : 107505910 : for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
3989 : 80906148 : gsi_next (&gsi))
3990 : : {
3991 : 80906148 : ssa_op_iter iter;
3992 : 80906148 : tree op;
3993 : :
3994 : 80906148 : stmt = gsi_stmt (gsi);
3995 : :
3996 : 80906148 : if (set_bb_may_notreturn)
3997 : : {
3998 : 2665233 : BB_MAY_NOTRETURN (block) = 1;
3999 : 2665233 : set_bb_may_notreturn = false;
4000 : : }
4001 : :
4002 : : /* Cache whether the basic-block has any non-visible side-effect
4003 : : or control flow.
4004 : : If this isn't a call or it is the last stmt in the
4005 : : basic-block then the CFG represents things correctly. */
4006 : 80906148 : if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
4007 : : {
4008 : : /* Non-looping const functions always return normally.
4009 : : Otherwise the call might not return or have side-effects
4010 : : that forbids hoisting possibly trapping expressions
4011 : : before it. */
4012 : 3730595 : int flags = gimple_call_flags (stmt);
4013 : 3730595 : if (!(flags & (ECF_CONST|ECF_PURE))
4014 : 573159 : || (flags & ECF_LOOPING_CONST_OR_PURE)
4015 : 4277284 : || stmt_can_throw_external (fun, stmt))
4016 : : /* Defer setting of BB_MAY_NOTRETURN to avoid it
4017 : : influencing the processing of the call itself. */
4018 : : set_bb_may_notreturn = true;
4019 : : }
4020 : :
4021 : 95877765 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
4022 : : {
4023 : 14971617 : pre_expr e = get_or_alloc_expr_for_name (op);
4024 : 14971617 : add_to_value (get_expr_value_id (e), e);
4025 : 14971617 : bitmap_insert_into_set (TMP_GEN (block), e);
4026 : 14971617 : bitmap_value_insert_into_set (AVAIL_OUT (block), e);
4027 : : }
4028 : :
4029 : 108110156 : if (gimple_vdef (stmt))
4030 : 12146779 : BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
4031 : :
4032 : 80906148 : if (gimple_has_side_effects (stmt)
4033 : 74437385 : || stmt_could_throw_p (fun, stmt)
4034 : 154147304 : || is_gimple_debug (stmt))
4035 : 75592318 : continue;
4036 : :
4037 : 47064689 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
4038 : : {
4039 : 22059467 : if (ssa_undefined_value_p (op))
4040 : 48590 : continue;
4041 : 22010877 : pre_expr e = get_or_alloc_expr_for_name (op);
4042 : 22010877 : bitmap_value_insert_into_set (EXP_GEN (block), e);
4043 : : }
4044 : :
4045 : 25005222 : switch (gimple_code (stmt))
4046 : : {
4047 : 923594 : case GIMPLE_RETURN:
4048 : 923594 : continue;
4049 : :
4050 : 544393 : case GIMPLE_CALL:
4051 : 544393 : {
4052 : 544393 : vn_reference_t ref;
4053 : 544393 : vn_reference_s ref1;
4054 : 544393 : pre_expr result = NULL;
4055 : :
4056 : 544393 : vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
4057 : : /* There is no point to PRE a call without a value. */
4058 : 544393 : if (!ref || !ref->result)
4059 : 9815 : continue;
4060 : :
4061 : : /* If the value of the call is not invalidated in
4062 : : this block until it is computed, add the expression
4063 : : to EXP_GEN. */
4064 : 534578 : if ((!gimple_vuse (stmt)
4065 : 305008 : || gimple_code
4066 : 305008 : (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
4067 : 277393 : || gimple_bb (SSA_NAME_DEF_STMT
4068 : : (gimple_vuse (stmt))) != block)
4069 : : /* If the REFERENCE traps and there was a preceding
4070 : : point in the block that might not return avoid
4071 : : adding the reference to EXP_GEN. */
4072 : 784377 : && (!BB_MAY_NOTRETURN (block)
4073 : 9817 : || !vn_reference_may_trap (ref)))
4074 : : {
4075 : 469552 : result = get_or_alloc_expr_for_reference
4076 : 469552 : (ref, gimple_location (stmt));
4077 : 469552 : add_to_value (get_expr_value_id (result), result);
4078 : 469552 : bitmap_value_insert_into_set (EXP_GEN (block), result);
4079 : : }
4080 : 534578 : continue;
4081 : 534578 : }
4082 : :
4083 : 18223405 : case GIMPLE_ASSIGN:
4084 : 18223405 : {
4085 : 18223405 : pre_expr result = NULL;
4086 : 18223405 : switch (vn_get_stmt_kind (stmt))
4087 : : {
4088 : 7564204 : case VN_NARY:
4089 : 7564204 : {
4090 : 7564204 : enum tree_code code = gimple_assign_rhs_code (stmt);
4091 : 7564204 : vn_nary_op_t nary;
4092 : :
4093 : : /* COND_EXPR is awkward in that it contains an
4094 : : embedded complex expression.
4095 : : Don't even try to shove it through PRE. */
4096 : 7564204 : if (code == COND_EXPR)
4097 : 140792 : continue;
4098 : :
4099 : 7560653 : vn_nary_op_lookup_stmt (stmt, &nary);
4100 : 7560653 : if (!nary || nary->predicated_values)
4101 : 109150 : continue;
4102 : :
4103 : 7451503 : unsigned value_id = nary->value_id;
4104 : 7451503 : if (value_id_constant_p (value_id))
4105 : 0 : continue;
4106 : :
4107 : : /* Record the un-valueized expression for EXP_GEN. */
4108 : 7451503 : nary = XALLOCAVAR (struct vn_nary_op_s,
4109 : : sizeof_vn_nary_op
4110 : : (vn_nary_length_from_stmt (stmt)));
4111 : 7451503 : init_vn_nary_op_from_stmt (nary, as_a <gassign *> (stmt));
4112 : :
4113 : : /* If the NARY traps and there was a preceding
4114 : : point in the block that might not return avoid
4115 : : adding the nary to EXP_GEN. */
4116 : 7479594 : if (BB_MAY_NOTRETURN (block)
4117 : 7451503 : && vn_nary_may_trap (nary))
4118 : 28091 : continue;
4119 : :
4120 : 7423412 : result = get_or_alloc_expr_for_nary
4121 : 7423412 : (nary, value_id, gimple_location (stmt));
4122 : 7423412 : break;
4123 : : }
4124 : :
4125 : 5216424 : case VN_REFERENCE:
4126 : 5216424 : {
4127 : 5216424 : tree rhs1 = gimple_assign_rhs1 (stmt);
4128 : 5216424 : ao_ref rhs1_ref;
4129 : 5216424 : ao_ref_init (&rhs1_ref, rhs1);
4130 : 5216424 : alias_set_type set = ao_ref_alias_set (&rhs1_ref);
4131 : 5216424 : alias_set_type base_set
4132 : 5216424 : = ao_ref_base_alias_set (&rhs1_ref);
4133 : 5216424 : vec<vn_reference_op_s> operands
4134 : 5216424 : = vn_reference_operands_for_lookup (rhs1);
4135 : 5216424 : vn_reference_t ref;
4136 : :
4137 : : /* We handle &MEM[ptr + 5].b[1].c as
4138 : : POINTER_PLUS_EXPR. */
4139 : 5216424 : if (operands[0].opcode == ADDR_EXPR
4140 : 5483614 : && operands.last ().opcode == SSA_NAME)
4141 : : {
4142 : 267182 : tree ops[2];
4143 : 267182 : if (vn_pp_nary_for_addr (operands, ops))
4144 : : {
4145 : 183477 : vn_nary_op_t nary;
4146 : 183477 : vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4147 : 183477 : TREE_TYPE (rhs1), ops,
4148 : : &nary);
4149 : 183477 : operands.release ();
4150 : 183477 : if (nary && !nary->predicated_values)
4151 : : {
4152 : 183468 : unsigned value_id = nary->value_id;
4153 : 183468 : if (value_id_constant_p (value_id))
4154 : 9 : continue;
4155 : 183468 : result = get_or_alloc_expr_for_nary
4156 : 183468 : (nary, value_id, gimple_location (stmt));
4157 : 183468 : break;
4158 : : }
4159 : 9 : continue;
4160 : 9 : }
4161 : : }
4162 : :
4163 : 10065894 : vn_reference_lookup_pieces (gimple_vuse (stmt), set,
4164 : 5032947 : base_set, TREE_TYPE (rhs1),
4165 : : operands, &ref, VN_WALK);
4166 : 5032947 : if (!ref)
4167 : : {
4168 : 383224 : operands.release ();
4169 : 383224 : continue;
4170 : : }
4171 : :
4172 : : /* If the REFERENCE traps and there was a preceding
4173 : : point in the block that might not return avoid
4174 : : adding the reference to EXP_GEN. */
4175 : 4878407 : if (BB_MAY_NOTRETURN (block)
4176 : 4649723 : && vn_reference_may_trap (ref))
4177 : : {
4178 : 228684 : operands.release ();
4179 : 228684 : continue;
4180 : : }
4181 : :
4182 : : /* If the value of the reference is not invalidated in
4183 : : this block until it is computed, add the expression
4184 : : to EXP_GEN. */
4185 : 8842078 : if (gimple_vuse (stmt))
4186 : : {
4187 : 4337339 : gimple *def_stmt;
4188 : 4337339 : bool ok = true;
4189 : 4337339 : def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
4190 : 7187542 : while (!gimple_nop_p (def_stmt)
4191 : 6222853 : && gimple_code (def_stmt) != GIMPLE_PHI
4192 : 12208241 : && gimple_bb (def_stmt) == block)
4193 : : {
4194 : 3745718 : if (stmt_may_clobber_ref_p
4195 : 3745718 : (def_stmt, gimple_assign_rhs1 (stmt)))
4196 : : {
4197 : : ok = false;
4198 : : break;
4199 : : }
4200 : 2850203 : def_stmt
4201 : 2850203 : = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
4202 : : }
4203 : 4337339 : if (!ok)
4204 : : {
4205 : 895515 : operands.release ();
4206 : 895515 : continue;
4207 : : }
4208 : : }
4209 : :
4210 : : /* If the load was value-numbered to another
4211 : : load make sure we do not use its expression
4212 : : for insertion if it wouldn't be a valid
4213 : : replacement. */
4214 : : /* At the momemt we have a testcase
4215 : : for hoist insertion of aligned vs. misaligned
4216 : : variants in gcc.dg/torture/pr65270-1.c thus
4217 : : with just alignment to be considered we can
4218 : : simply replace the expression in the hashtable
4219 : : with the most conservative one. */
4220 : 3525524 : vn_reference_op_t ref1 = &ref->operands.last ();
4221 : 3525524 : while (ref1->opcode != TARGET_MEM_REF
4222 : 7050955 : && ref1->opcode != MEM_REF
4223 : 7050955 : && ref1 != &ref->operands[0])
4224 : 3525431 : --ref1;
4225 : 3525524 : vn_reference_op_t ref2 = &operands.last ();
4226 : 3525524 : while (ref2->opcode != TARGET_MEM_REF
4227 : 7050960 : && ref2->opcode != MEM_REF
4228 : 10576863 : && ref2 != &operands[0])
4229 : 3525436 : --ref2;
4230 : 3525524 : if ((ref1->opcode == TARGET_MEM_REF
4231 : : || ref1->opcode == MEM_REF)
4232 : 7050576 : && (TYPE_ALIGN (ref1->type)
4233 : 3525052 : > TYPE_ALIGN (ref2->type)))
4234 : 496 : ref1->type
4235 : 496 : = build_aligned_type (ref1->type,
4236 : 496 : TYPE_ALIGN (ref2->type));
4237 : : /* TBAA behavior is an obvious part so make sure
4238 : : that the hashtable one covers this as well
4239 : : by adjusting the ref alias set and its base. */
4240 : 3525524 : if ((ref->set == set
4241 : 9906 : || alias_set_subset_of (set, ref->set))
4242 : 3528417 : && (ref->base_set == base_set
4243 : 11484 : || alias_set_subset_of (base_set, ref->base_set)))
4244 : : ;
4245 : 14504 : else if (ref1->opcode != ref2->opcode
4246 : 14499 : || (ref1->opcode != MEM_REF
4247 : 14499 : && ref1->opcode != TARGET_MEM_REF))
4248 : : {
4249 : : /* With mismatching base opcodes or bases
4250 : : other than MEM_REF or TARGET_MEM_REF we
4251 : : can't do any easy TBAA adjustment. */
4252 : 5 : operands.release ();
4253 : 5 : continue;
4254 : : }
4255 : 14499 : else if (ref->set == set
4256 : 14499 : || alias_set_subset_of (ref->set, set))
4257 : : {
4258 : 13993 : tree reft = reference_alias_ptr_type (rhs1);
4259 : 13993 : ref->set = set;
4260 : 13993 : ref->base_set = set;
4261 : 13993 : if (ref1->opcode == MEM_REF)
4262 : 13993 : ref1->op0
4263 : 27986 : = wide_int_to_tree (reft,
4264 : 13993 : wi::to_wide (ref1->op0));
4265 : : else
4266 : 0 : ref1->op2
4267 : 0 : = wide_int_to_tree (reft,
4268 : 0 : wi::to_wide (ref1->op2));
4269 : : }
4270 : : else
4271 : : {
4272 : 506 : ref->set = 0;
4273 : 506 : ref->base_set = 0;
4274 : 506 : if (ref1->opcode == MEM_REF)
4275 : 506 : ref1->op0
4276 : 1012 : = wide_int_to_tree (ptr_type_node,
4277 : 506 : wi::to_wide (ref1->op0));
4278 : : else
4279 : 0 : ref1->op2
4280 : 0 : = wide_int_to_tree (ptr_type_node,
4281 : 0 : wi::to_wide (ref1->op2));
4282 : : }
4283 : : /* We also need to make sure that the access path
4284 : : ends in an access of the same size as otherwise
4285 : : we might assume an access may not trap while in
4286 : : fact it might. That's independent of whether
4287 : : TBAA is in effect. */
4288 : 3525519 : if (TYPE_SIZE (ref1->type) != TYPE_SIZE (ref2->type)
4289 : 3525519 : && (! TYPE_SIZE (ref1->type)
4290 : 10807 : || ! TYPE_SIZE (ref2->type)
4291 : 10795 : || ! operand_equal_p (TYPE_SIZE (ref1->type),
4292 : 10795 : TYPE_SIZE (ref2->type))))
4293 : : {
4294 : 10815 : operands.release ();
4295 : 10815 : continue;
4296 : : }
4297 : 3514704 : operands.release ();
4298 : :
4299 : 3514704 : result = get_or_alloc_expr_for_reference
4300 : 3514704 : (ref, gimple_location (stmt));
4301 : 3514704 : break;
4302 : : }
4303 : :
4304 : 5442777 : default:
4305 : 5442777 : continue;
4306 : 5442777 : }
4307 : :
4308 : 11121584 : add_to_value (get_expr_value_id (result), result);
4309 : 11121584 : bitmap_value_insert_into_set (EXP_GEN (block), result);
4310 : 11121584 : continue;
4311 : 11121584 : }
4312 : 5313830 : default:
4313 : 5313830 : break;
4314 : 923594 : }
4315 : : }
4316 : 13299881 : if (set_bb_may_notreturn)
4317 : : {
4318 : 520969 : BB_MAY_NOTRETURN (block) = 1;
4319 : 520969 : set_bb_may_notreturn = false;
4320 : : }
4321 : :
4322 : 13299881 : if (dump_file && (dump_flags & TDF_DETAILS))
4323 : : {
4324 : 108 : print_bitmap_set (dump_file, EXP_GEN (block),
4325 : : "exp_gen", block->index);
4326 : 108 : print_bitmap_set (dump_file, PHI_GEN (block),
4327 : : "phi_gen", block->index);
4328 : 108 : print_bitmap_set (dump_file, TMP_GEN (block),
4329 : : "tmp_gen", block->index);
4330 : 108 : print_bitmap_set (dump_file, AVAIL_OUT (block),
4331 : : "avail_out", block->index);
4332 : : }
4333 : :
4334 : : /* Put the dominator children of BLOCK on the worklist of blocks
4335 : : to compute available sets for. */
4336 : 13299881 : for (son = first_dom_son (CDI_DOMINATORS, block);
4337 : 25651610 : son;
4338 : 12351729 : son = next_dom_son (CDI_DOMINATORS, son))
4339 : 12351729 : worklist[sp++] = son;
4340 : : }
4341 : 948152 : vn_context_bb = NULL;
4342 : :
4343 : 948152 : free (worklist);
4344 : 948152 : }
4345 : :
4346 : :
4347 : : /* Initialize data structures used by PRE. */
4348 : :
4349 : : static void
4350 : 948158 : init_pre (void)
4351 : : {
4352 : 948158 : basic_block bb;
4353 : :
4354 : 948158 : next_expression_id = 1;
4355 : 948158 : expressions.create (0);
4356 : 948158 : expressions.safe_push (NULL);
4357 : 948158 : value_expressions.create (get_max_value_id () + 1);
4358 : 948158 : value_expressions.quick_grow_cleared (get_max_value_id () + 1);
4359 : 948158 : constant_value_expressions.create (get_max_constant_value_id () + 1);
4360 : 948158 : constant_value_expressions.quick_grow_cleared (get_max_constant_value_id () + 1);
4361 : 948158 : name_to_id.create (0);
4362 : 948158 : gcc_obstack_init (&pre_expr_obstack);
4363 : :
4364 : 948158 : inserted_exprs = BITMAP_ALLOC (NULL);
4365 : :
4366 : 948158 : connect_infinite_loops_to_exit ();
4367 : 948158 : memset (&pre_stats, 0, sizeof (pre_stats));
4368 : :
4369 : 948158 : alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4370 : :
4371 : 948158 : calculate_dominance_info (CDI_DOMINATORS);
4372 : :
4373 : 948158 : bitmap_obstack_initialize (&grand_bitmap_obstack);
4374 : 1896316 : expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4375 : 16177353 : FOR_ALL_BB_FN (bb, cfun)
4376 : : {
4377 : 15229195 : EXP_GEN (bb) = bitmap_set_new ();
4378 : 15229195 : PHI_GEN (bb) = bitmap_set_new ();
4379 : 15229195 : TMP_GEN (bb) = bitmap_set_new ();
4380 : 15229195 : AVAIL_OUT (bb) = bitmap_set_new ();
4381 : 15229195 : PHI_TRANS_TABLE (bb) = NULL;
4382 : : }
4383 : 948158 : }
4384 : :
4385 : :
4386 : : /* Deallocate data structures used by PRE. */
4387 : :
4388 : : static void
4389 : 948158 : fini_pre ()
4390 : : {
4391 : 948158 : value_expressions.release ();
4392 : 948158 : constant_value_expressions.release ();
4393 : 948158 : expressions.release ();
4394 : 948158 : bitmap_obstack_release (&grand_bitmap_obstack);
4395 : 948158 : bitmap_set_pool.release ();
4396 : 948158 : pre_expr_pool.release ();
4397 : 948158 : delete expression_to_id;
4398 : 948158 : expression_to_id = NULL;
4399 : 948158 : name_to_id.release ();
4400 : 948158 : obstack_free (&pre_expr_obstack, NULL);
4401 : :
4402 : 948158 : basic_block bb;
4403 : 16177029 : FOR_ALL_BB_FN (bb, cfun)
4404 : 15228871 : if (bb->aux && PHI_TRANS_TABLE (bb))
4405 : 6123460 : delete PHI_TRANS_TABLE (bb);
4406 : 948158 : free_aux_for_blocks ();
4407 : 948158 : }
4408 : :
4409 : : namespace {
4410 : :
4411 : : const pass_data pass_data_pre =
4412 : : {
4413 : : GIMPLE_PASS, /* type */
4414 : : "pre", /* name */
4415 : : OPTGROUP_NONE, /* optinfo_flags */
4416 : : TV_TREE_PRE, /* tv_id */
4417 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
4418 : : 0, /* properties_provided */
4419 : : 0, /* properties_destroyed */
4420 : : TODO_rebuild_alias, /* todo_flags_start */
4421 : : 0, /* todo_flags_finish */
4422 : : };
4423 : :
4424 : : class pass_pre : public gimple_opt_pass
4425 : : {
4426 : : public:
4427 : 285081 : pass_pre (gcc::context *ctxt)
4428 : 570162 : : gimple_opt_pass (pass_data_pre, ctxt)
4429 : : {}
4430 : :
4431 : : /* opt_pass methods: */
4432 : 1021582 : bool gate (function *) final override
4433 : 1021582 : { return flag_tree_pre != 0 || flag_code_hoisting != 0; }
4434 : : unsigned int execute (function *) final override;
4435 : :
4436 : : }; // class pass_pre
4437 : :
4438 : : /* Valueization hook for RPO VN when we are calling back to it
4439 : : at ANTIC compute time. */
4440 : :
4441 : : static tree
4442 : 103002843 : pre_valueize (tree name)
4443 : : {
4444 : 103002843 : if (TREE_CODE (name) == SSA_NAME)
4445 : : {
4446 : 102712424 : tree tem = VN_INFO (name)->valnum;
4447 : 102712424 : if (tem != VN_TOP && tem != name)
4448 : : {
4449 : 14705605 : if (TREE_CODE (tem) != SSA_NAME
4450 : 14705605 : || SSA_NAME_IS_DEFAULT_DEF (tem))
4451 : : return tem;
4452 : : /* We create temporary SSA names for representatives that
4453 : : do not have a definition (yet) but are not default defs either
4454 : : assume they are fine to use. */
4455 : 14701273 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (tem));
4456 : 14701273 : if (! def_bb
4457 : 14701273 : || dominated_by_p (CDI_DOMINATORS, vn_context_bb, def_bb))
4458 : 120119 : return tem;
4459 : : /* ??? Now we could look for a leader. Ideally we'd somehow
4460 : : expose RPO VN leaders and get rid of AVAIL_OUT as well... */
4461 : : }
4462 : : }
4463 : : return name;
4464 : : }
4465 : :
4466 : : unsigned int
4467 : 948158 : pass_pre::execute (function *fun)
4468 : : {
4469 : 948158 : unsigned int todo = 0;
4470 : :
4471 : 1896316 : do_partial_partial =
4472 : 948158 : flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4473 : :
4474 : : /* This has to happen before VN runs because
4475 : : loop_optimizer_init may create new phis, etc. */
4476 : 948158 : loop_optimizer_init (LOOPS_NORMAL);
4477 : 948158 : split_edges_for_insertion ();
4478 : 948158 : scev_initialize ();
4479 : 948158 : calculate_dominance_info (CDI_DOMINATORS);
4480 : :
4481 : 948158 : run_rpo_vn (VN_WALK);
4482 : :
4483 : 948158 : init_pre ();
4484 : :
4485 : 948158 : vn_valueize = pre_valueize;
4486 : :
4487 : : /* Insert can get quite slow on an incredibly large number of basic
4488 : : blocks due to some quadratic behavior. Until this behavior is
4489 : : fixed, don't run it when he have an incredibly large number of
4490 : : bb's. If we aren't going to run insert, there is no point in
4491 : : computing ANTIC, either, even though it's plenty fast nor do
4492 : : we require AVAIL. */
4493 : 948158 : if (n_basic_blocks_for_fn (fun) < 4000)
4494 : : {
4495 : 948152 : compute_avail (fun);
4496 : 948152 : compute_antic ();
4497 : 948152 : insert ();
4498 : : }
4499 : :
4500 : : /* Make sure to remove fake edges before committing our inserts.
4501 : : This makes sure we don't end up with extra critical edges that
4502 : : we would need to split. */
4503 : 948158 : remove_fake_exit_edges ();
4504 : 948158 : gsi_commit_edge_inserts ();
4505 : :
4506 : : /* Eliminate folds statements which might (should not...) end up
4507 : : not keeping virtual operands up-to-date. */
4508 : 948158 : gcc_assert (!need_ssa_update_p (fun));
4509 : :
4510 : 948158 : statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4511 : 948158 : statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4512 : 948158 : statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
4513 : 948158 : statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4514 : :
4515 : 948158 : todo |= eliminate_with_rpo_vn (inserted_exprs);
4516 : :
4517 : 948158 : vn_valueize = NULL;
4518 : :
4519 : 948158 : fini_pre ();
4520 : :
4521 : 948158 : scev_finalize ();
4522 : 948158 : loop_optimizer_finalize ();
4523 : :
4524 : : /* Perform a CFG cleanup before we run simple_dce_from_worklist since
4525 : : unreachable code regions will have not up-to-date SSA form which
4526 : : confuses it. */
4527 : 948158 : bool need_crit_edge_split = false;
4528 : 948158 : if (todo & TODO_cleanup_cfg)
4529 : : {
4530 : 138177 : cleanup_tree_cfg ();
4531 : 138177 : need_crit_edge_split = true;
4532 : : }
4533 : :
4534 : : /* Because we don't follow exactly the standard PRE algorithm, and decide not
4535 : : to insert PHI nodes sometimes, and because value numbering of casts isn't
4536 : : perfect, we sometimes end up inserting dead code. This simple DCE-like
4537 : : pass removes any insertions we made that weren't actually used. */
4538 : 948158 : simple_dce_from_worklist (inserted_exprs);
4539 : 948158 : BITMAP_FREE (inserted_exprs);
4540 : :
4541 : : /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4542 : : case we can merge the block with the remaining predecessor of the block.
4543 : : It should either:
4544 : : - call merge_blocks after each tail merge iteration
4545 : : - call merge_blocks after all tail merge iterations
4546 : : - mark TODO_cleanup_cfg when necessary. */
4547 : 948158 : todo |= tail_merge_optimize (need_crit_edge_split);
4548 : :
4549 : 948158 : free_rpo_vn ();
4550 : :
4551 : : /* Tail merging invalidates the virtual SSA web, together with
4552 : : cfg-cleanup opportunities exposed by PRE this will wreck the
4553 : : SSA updating machinery. So make sure to run update-ssa
4554 : : manually, before eventually scheduling cfg-cleanup as part of
4555 : : the todo. */
4556 : 948158 : update_ssa (TODO_update_ssa_only_virtuals);
4557 : :
4558 : 948158 : return todo;
4559 : : }
4560 : :
4561 : : } // anon namespace
4562 : :
4563 : : gimple_opt_pass *
4564 : 285081 : make_pass_pre (gcc::context *ctxt)
4565 : : {
4566 : 285081 : return new pass_pre (ctxt);
4567 : : }
|