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 : 51194598 : pre_expr_d::equal (const pre_expr_d *e1, const pre_expr_d *e2)
278 : : {
279 : 51194598 : if (e1->kind != e2->kind)
280 : : return false;
281 : :
282 : 32677556 : switch (e1->kind)
283 : : {
284 : 4360075 : case CONSTANT:
285 : 4360075 : return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
286 : 4360075 : PRE_EXPR_CONSTANT (e2));
287 : 125193 : case NAME:
288 : 125193 : return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
289 : 20150448 : case NARY:
290 : 20150448 : return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
291 : 8041840 : case REFERENCE:
292 : 8041840 : return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
293 : 8041840 : PRE_EXPR_REFERENCE (e2));
294 : 0 : default:
295 : 0 : gcc_unreachable ();
296 : : }
297 : : }
298 : :
299 : : /* Hash E. */
300 : :
301 : : inline hashval_t
302 : 82274711 : pre_expr_d::hash (const pre_expr_d *e)
303 : : {
304 : 82274711 : switch (e->kind)
305 : : {
306 : 6631256 : case CONSTANT:
307 : 6631256 : 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 : 50208350 : case NARY:
311 : 50208350 : return PRE_EXPR_NARY (e)->hashcode;
312 : 25435105 : case REFERENCE:
313 : 25435105 : 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 : 40351039 : alloc_expression_id (pre_expr expr)
332 : : {
333 : 40351039 : struct pre_expr_d **slot;
334 : : /* Make sure we won't overflow. */
335 : 40351039 : gcc_assert (next_expression_id + 1 > next_expression_id);
336 : 40351039 : expr->id = next_expression_id++;
337 : 40351039 : expressions.safe_push (expr);
338 : 40351039 : if (expr->kind == NAME)
339 : : {
340 : 22295397 : 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 : 22295397 : unsigned old_len = name_to_id.length ();
344 : 44590794 : name_to_id.reserve (num_ssa_names - old_len);
345 : 44590794 : name_to_id.quick_grow_cleared (num_ssa_names);
346 : 22295397 : gcc_assert (name_to_id[version] == 0);
347 : 22295397 : name_to_id[version] = expr->id;
348 : : }
349 : : else
350 : : {
351 : 18055642 : slot = expression_to_id->find_slot (expr, INSERT);
352 : 18055642 : gcc_assert (!*slot);
353 : 18055642 : *slot = expr;
354 : : }
355 : 40351039 : return next_expression_id - 1;
356 : : }
357 : :
358 : : /* Return the expression id for tree EXPR. */
359 : :
360 : : static inline unsigned int
361 : 232969911 : get_expression_id (const pre_expr expr)
362 : : {
363 : 232969911 : return expr->id;
364 : : }
365 : :
366 : : static inline unsigned int
367 : 73361284 : lookup_expression_id (const pre_expr expr)
368 : : {
369 : 73361284 : struct pre_expr_d **slot;
370 : :
371 : 73361284 : if (expr->kind == NAME)
372 : : {
373 : 49948675 : unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
374 : 68004317 : if (name_to_id.length () <= version)
375 : : return 0;
376 : 47119172 : return name_to_id[version];
377 : : }
378 : : else
379 : : {
380 : 23412609 : slot = expression_to_id->find_slot (expr, NO_INSERT);
381 : 23412609 : if (!slot)
382 : : return 0;
383 : 5356967 : return ((pre_expr)*slot)->id;
384 : : }
385 : : }
386 : :
387 : : /* Return the expression that has expression id ID */
388 : :
389 : : static inline pre_expr
390 : 465052696 : expression_for_id (unsigned int id)
391 : : {
392 : 930105392 : 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 : 49948675 : get_or_alloc_expr_for_name (tree name)
401 : : {
402 : 49948675 : struct pre_expr_d expr;
403 : 49948675 : pre_expr result;
404 : 49948675 : unsigned int result_id;
405 : :
406 : 49948675 : expr.kind = NAME;
407 : 49948675 : expr.id = 0;
408 : 49948675 : PRE_EXPR_NAME (&expr) = name;
409 : 49948675 : result_id = lookup_expression_id (&expr);
410 : 49948675 : if (result_id != 0)
411 : 27653278 : return expression_for_id (result_id);
412 : :
413 : 22295397 : result = pre_expr_pool.allocate ();
414 : 22295397 : result->kind = NAME;
415 : 22295397 : result->loc = UNKNOWN_LOCATION;
416 : 22295397 : result->value_id = VN_INFO (name)->value_id;
417 : 22295397 : PRE_EXPR_NAME (result) = name;
418 : 22295397 : alloc_expression_id (result);
419 : 22295397 : 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 : 12441312 : get_or_alloc_expr_for_nary (vn_nary_op_t nary, unsigned value_id,
428 : : location_t loc = UNKNOWN_LOCATION)
429 : : {
430 : 12441312 : struct pre_expr_d expr;
431 : 12441312 : pre_expr result;
432 : 12441312 : unsigned int result_id;
433 : :
434 : 12441312 : gcc_assert (value_id == 0 || !value_id_constant_p (value_id));
435 : :
436 : 12441312 : expr.kind = NARY;
437 : 12441312 : expr.id = 0;
438 : 12441312 : nary->hashcode = vn_nary_op_compute_hash (nary);
439 : 12441312 : PRE_EXPR_NARY (&expr) = nary;
440 : 12441312 : result_id = lookup_expression_id (&expr);
441 : 12441312 : if (result_id != 0)
442 : 858118 : return expression_for_id (result_id);
443 : :
444 : 11583194 : result = pre_expr_pool.allocate ();
445 : 11583194 : result->kind = NARY;
446 : 11583194 : result->loc = loc;
447 : 11583194 : result->value_id = value_id ? value_id : get_next_value_id ();
448 : 11583194 : PRE_EXPR_NARY (result)
449 : 11583194 : = alloc_vn_nary_op_noinit (nary->length, &pre_expr_obstack);
450 : 11583194 : memcpy (PRE_EXPR_NARY (result), nary, sizeof_vn_nary_op (nary->length));
451 : 11583194 : alloc_expression_id (result);
452 : 11583194 : return result;
453 : : }
454 : :
455 : : /* Given an REFERENCE, get or create a pre_expr to represent it. */
456 : :
457 : : static pre_expr
458 : 6413549 : get_or_alloc_expr_for_reference (vn_reference_t reference,
459 : : location_t loc = UNKNOWN_LOCATION)
460 : : {
461 : 6413549 : struct pre_expr_d expr;
462 : 6413549 : pre_expr result;
463 : 6413549 : unsigned int result_id;
464 : :
465 : 6413549 : expr.kind = REFERENCE;
466 : 6413549 : expr.id = 0;
467 : 6413549 : PRE_EXPR_REFERENCE (&expr) = reference;
468 : 6413549 : result_id = lookup_expression_id (&expr);
469 : 6413549 : if (result_id != 0)
470 : 718126 : return expression_for_id (result_id);
471 : :
472 : 5695423 : result = pre_expr_pool.allocate ();
473 : 5695423 : result->kind = REFERENCE;
474 : 5695423 : result->loc = loc;
475 : 5695423 : result->value_id = reference->value_id;
476 : 5695423 : PRE_EXPR_REFERENCE (result) = reference;
477 : 5695423 : alloc_expression_id (result);
478 : 5695423 : return result;
479 : : }
480 : :
481 : :
482 : : /* An unordered bitmap set. One bitmap tracks values, the other,
483 : : expressions. */
484 : 135984723 : 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 : 1261791299 : expr_pred_trans_d::is_empty (const expr_pred_trans_d &e)
567 : : {
568 : 1261791299 : return e.e == 0;
569 : : }
570 : :
571 : : inline bool
572 : 249332005 : expr_pred_trans_d::is_deleted (const expr_pred_trans_d &e)
573 : : {
574 : 249332005 : return e.e == -1u;
575 : : }
576 : :
577 : : inline void
578 : 2085478 : expr_pred_trans_d::mark_empty (expr_pred_trans_d &e)
579 : : {
580 : 2085478 : e.e = 0;
581 : : }
582 : :
583 : : inline void
584 : 3605197 : expr_pred_trans_d::mark_deleted (expr_pred_trans_d &e)
585 : : {
586 : 3605197 : 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 : 194479680 : expr_pred_trans_d::equal (const expr_pred_trans_d &ve1,
597 : : const expr_pred_trans_d &ve2)
598 : : {
599 : 194479680 : 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 : 78567574 : phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
669 : : {
670 : 78567574 : if (!PHI_TRANS_TABLE (pred))
671 : 159940 : PHI_TRANS_TABLE (pred) = new hash_table<expr_pred_trans_d> (11);
672 : :
673 : 78567574 : expr_pred_trans_t slot;
674 : 78567574 : expr_pred_trans_d tem;
675 : 78567574 : unsigned id = get_expression_id (e);
676 : 78567574 : tem.e = id;
677 : 78567574 : slot = PHI_TRANS_TABLE (pred)->find_slot_with_hash (tem, id, INSERT);
678 : 78567574 : if (slot->e)
679 : : {
680 : 57089932 : *entry = slot;
681 : 57089932 : return true;
682 : : }
683 : :
684 : 21477642 : *entry = slot;
685 : 21477642 : slot->e = id;
686 : 21477642 : return false;
687 : : }
688 : :
689 : :
690 : : /* Add expression E to the expression set of value id V. */
691 : :
692 : : static void
693 : 41925030 : add_to_value (unsigned int v, pre_expr e)
694 : : {
695 : 0 : gcc_checking_assert (get_expr_value_id (e) == v);
696 : :
697 : 41925030 : if (value_id_constant_p (v))
698 : : {
699 : 819507 : if (e->kind != CONSTANT)
700 : : return;
701 : :
702 : 777025 : if (-v >= constant_value_expressions.length ())
703 : 465016 : constant_value_expressions.safe_grow_cleared (-v + 1);
704 : :
705 : 777025 : pre_expr leader = constant_value_expressions[-v];
706 : 777025 : if (!leader)
707 : 777025 : constant_value_expressions[-v] = e;
708 : : }
709 : : else
710 : : {
711 : 41105523 : if (v >= value_expressions.length ())
712 : 6310745 : value_expressions.safe_grow_cleared (v + 1);
713 : :
714 : 41105523 : bitmap set = value_expressions[v];
715 : 41105523 : if (!set)
716 : : {
717 : 23122853 : set = BITMAP_ALLOC (&grand_bitmap_obstack);
718 : 23122853 : value_expressions[v] = set;
719 : : }
720 : 41105523 : 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 : 135984723 : bitmap_set_new (void)
728 : : {
729 : 135984723 : bitmap_set_t ret = bitmap_set_pool.allocate ();
730 : 135984723 : bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
731 : 135984723 : bitmap_initialize (&ret->values, &grand_bitmap_obstack);
732 : 135984723 : return ret;
733 : : }
734 : :
735 : : /* Return the value id for a PRE expression EXPR. */
736 : :
737 : : static unsigned int
738 : 484532173 : 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 : 41925030 : 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 : 1143777 : vn_valnum_from_value_id (unsigned int val)
750 : : {
751 : 1143777 : 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 : 1143777 : bitmap exprset = value_expressions[val];
760 : 1143777 : bitmap_iterator bi;
761 : 1143777 : unsigned int i;
762 : 1705368 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
763 : : {
764 : 1385522 : pre_expr vexpr = expression_for_id (i);
765 : 1385522 : if (vexpr->kind == NAME)
766 : 823931 : 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 : 67478143 : bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
775 : : {
776 : 67478143 : unsigned int val = get_expr_value_id (expr);
777 : 67478143 : 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 : 64981972 : bitmap_set_bit (&set->values, val);
783 : 64981972 : bitmap_set_bit (&set->expressions, get_expression_id (expr));
784 : : }
785 : 67478143 : }
786 : :
787 : : /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
788 : :
789 : : static void
790 : 25156991 : bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
791 : : {
792 : 25156991 : bitmap_copy (&dest->expressions, &orig->expressions);
793 : 25156991 : bitmap_copy (&dest->values, &orig->values);
794 : 25156991 : }
795 : :
796 : :
797 : : /* Free memory used up by SET. */
798 : : static void
799 : 67437080 : bitmap_set_free (bitmap_set_t set)
800 : : {
801 : 0 : bitmap_clear (&set->expressions);
802 : 18001575 : bitmap_clear (&set->values);
803 : 45624746 : }
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 : 76856297 : pre_expr_DFS (unsigned val, bitmap_set_t set, bitmap val_visited,
814 : : vec<pre_expr> &post)
815 : : {
816 : 76856297 : unsigned int i;
817 : 76856297 : bitmap_iterator bi;
818 : :
819 : : /* Iterate over all leaders and DFS recurse. Borrowed from
820 : : bitmap_find_leader. */
821 : 76856297 : bitmap exprset = value_expressions[val];
822 : 76856297 : if (!exprset->first->next)
823 : : {
824 : 181887484 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
825 : 115151512 : if (bitmap_bit_p (&set->expressions, i))
826 : 66899361 : pre_expr_DFS (expression_for_id (i), set, val_visited, post);
827 : 66735972 : return;
828 : : }
829 : :
830 : 20443338 : EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
831 : 10323013 : 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 : 77222374 : pre_expr_DFS (pre_expr expr, bitmap_set_t set, bitmap val_visited,
839 : : vec<pre_expr> &post)
840 : : {
841 : 77222374 : switch (expr->kind)
842 : : {
843 : 36495073 : case NARY:
844 : 36495073 : {
845 : 36495073 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
846 : 97742989 : for (unsigned i = 0; i < nary->length; i++)
847 : : {
848 : 61247916 : if (TREE_CODE (nary->op[i]) != SSA_NAME)
849 : 18692509 : continue;
850 : 42555407 : 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 : 42555407 : if (bitmap_bit_p (&set->values, op_val_id)
854 : 42555407 : && bitmap_set_bit (val_visited, op_val_id))
855 : 7417626 : pre_expr_DFS (op_val_id, set, val_visited, post);
856 : : }
857 : : break;
858 : : }
859 : 11318273 : case REFERENCE:
860 : 11318273 : {
861 : 11318273 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
862 : 11318273 : vec<vn_reference_op_s> operands = ref->operands;
863 : 11318273 : vn_reference_op_t operand;
864 : 44630813 : for (unsigned i = 0; operands.iterate (i, &operand); i++)
865 : : {
866 : 33312540 : tree op[3];
867 : 33312540 : op[0] = operand->op0;
868 : 33312540 : op[1] = operand->op1;
869 : 33312540 : op[2] = operand->op2;
870 : 133250160 : for (unsigned n = 0; n < 3; ++n)
871 : : {
872 : 99937620 : if (!op[n] || TREE_CODE (op[n]) != SSA_NAME)
873 : 92167844 : continue;
874 : 7769776 : unsigned op_val_id = VN_INFO (op[n])->value_id;
875 : 7769776 : if (bitmap_bit_p (&set->values, op_val_id)
876 : 7769776 : && bitmap_set_bit (val_visited, op_val_id))
877 : 1290695 : pre_expr_DFS (op_val_id, set, val_visited, post);
878 : : }
879 : : }
880 : : break;
881 : : }
882 : 77222374 : default:;
883 : : }
884 : 77222374 : post.quick_push (expr);
885 : 77222374 : }
886 : :
887 : : /* Generate an topological-ordered array of bitmap set SET. */
888 : :
889 : : static vec<pre_expr>
890 : 16997331 : sorted_array_from_bitmap_set (bitmap_set_t set)
891 : : {
892 : 16997331 : unsigned int i;
893 : 16997331 : bitmap_iterator bi;
894 : 16997331 : vec<pre_expr> result;
895 : :
896 : : /* Pre-allocate enough space for the array. */
897 : 16997331 : result.create (bitmap_count_bits (&set->expressions));
898 : :
899 : 16997331 : auto_bitmap val_visited (&grand_bitmap_obstack);
900 : 16997331 : bitmap_tree_view (val_visited);
901 : 93853628 : FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
902 : 76856297 : if (bitmap_set_bit (val_visited, i))
903 : 68147976 : pre_expr_DFS (i, set, val_visited, result);
904 : :
905 : 16997331 : return result;
906 : 16997331 : }
907 : :
908 : : /* Subtract all expressions contained in ORIG from DEST. */
909 : :
910 : : static bitmap_set_t
911 : 30052998 : bitmap_set_subtract_expressions (bitmap_set_t dest, bitmap_set_t orig)
912 : : {
913 : 30052998 : bitmap_set_t result = bitmap_set_new ();
914 : 30052998 : bitmap_iterator bi;
915 : 30052998 : unsigned int i;
916 : :
917 : 30052998 : bitmap_and_compl (&result->expressions, &dest->expressions,
918 : 30052998 : &orig->expressions);
919 : :
920 : 102107746 : FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
921 : : {
922 : 72054748 : pre_expr expr = expression_for_id (i);
923 : 72054748 : unsigned int value_id = get_expr_value_id (expr);
924 : 72054748 : bitmap_set_bit (&result->values, value_id);
925 : : }
926 : :
927 : 30052998 : return result;
928 : : }
929 : :
930 : : /* Subtract all values in bitmap set B from bitmap set A. */
931 : :
932 : : static void
933 : 1084652 : bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
934 : : {
935 : 1084652 : unsigned int i;
936 : 1084652 : bitmap_iterator bi;
937 : 1084652 : unsigned to_remove = -1U;
938 : 1084652 : bitmap_and_compl_into (&a->values, &b->values);
939 : 10216761 : FOR_EACH_EXPR_ID_IN_SET (a, i, bi)
940 : : {
941 : 9132109 : if (to_remove != -1U)
942 : : {
943 : 1317425 : bitmap_clear_bit (&a->expressions, to_remove);
944 : 1317425 : to_remove = -1U;
945 : : }
946 : 9132109 : pre_expr expr = expression_for_id (i);
947 : 9132109 : if (! bitmap_bit_p (&a->values, get_expr_value_id (expr)))
948 : 1359332 : to_remove = i;
949 : : }
950 : 1084652 : if (to_remove != -1U)
951 : 41907 : bitmap_clear_bit (&a->expressions, to_remove);
952 : 1084652 : }
953 : :
954 : :
955 : : /* Return true if bitmapped set SET contains the value VALUE_ID. */
956 : :
957 : : static bool
958 : 181055476 : 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 : 88162867 : return bitmap_bit_p (&set->values, value_id);
964 : : }
965 : :
966 : : /* Return true if two bitmap sets are equal. */
967 : :
968 : : static bool
969 : 14484173 : 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 : 30091333 : bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
979 : : {
980 : 30091333 : unsigned int val = get_expr_value_id (expr);
981 : 30091333 : if (value_id_constant_p (val))
982 : : return false;
983 : :
984 : 30091333 : 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 : 12836152 : unsigned int i;
996 : 12836152 : bitmap_iterator bi;
997 : 12836152 : bitmap exprset = value_expressions[val];
998 : 14389435 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
999 : : {
1000 : 14389435 : if (bitmap_clear_bit (&set->expressions, i))
1001 : : {
1002 : 12836152 : bitmap_set_bit (&set->expressions, get_expression_id (expr));
1003 : 12836152 : return i != get_expression_id (expr);
1004 : : }
1005 : : }
1006 : 0 : gcc_unreachable ();
1007 : : }
1008 : :
1009 : 17255181 : bitmap_insert_into_set (set, expr);
1010 : 17255181 : 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 : 58243135 : bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
1018 : : {
1019 : 58243135 : unsigned int val = get_expr_value_id (expr);
1020 : :
1021 : 58243135 : gcc_checking_assert (expr->id == get_expression_id (expr));
1022 : :
1023 : : /* Constant values are always considered to be part of the set. */
1024 : 58243135 : if (value_id_constant_p (val))
1025 : : return;
1026 : :
1027 : : /* If the value membership changed, add the expression. */
1028 : 58189385 : if (bitmap_set_bit (&set->values, val))
1029 : 44987598 : bitmap_set_bit (&set->expressions, expr->id);
1030 : : }
1031 : :
1032 : : /* Print out EXPR to outfile. */
1033 : :
1034 : : static void
1035 : 5281 : print_pre_expr (FILE *outfile, const pre_expr expr)
1036 : : {
1037 : 5281 : if (! expr)
1038 : : {
1039 : 0 : fprintf (outfile, "NULL");
1040 : 0 : return;
1041 : : }
1042 : 5281 : switch (expr->kind)
1043 : : {
1044 : 0 : case CONSTANT:
1045 : 0 : print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr));
1046 : 0 : break;
1047 : 3503 : case NAME:
1048 : 3503 : print_generic_expr (outfile, PRE_EXPR_NAME (expr));
1049 : 3503 : break;
1050 : 1377 : case NARY:
1051 : 1377 : {
1052 : 1377 : unsigned int i;
1053 : 1377 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1054 : 1377 : fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
1055 : 5234 : for (i = 0; i < nary->length; i++)
1056 : : {
1057 : 2480 : print_generic_expr (outfile, nary->op[i]);
1058 : 2480 : if (i != (unsigned) nary->length - 1)
1059 : 1103 : fprintf (outfile, ",");
1060 : : }
1061 : 1377 : fprintf (outfile, "}");
1062 : : }
1063 : 1377 : break;
1064 : :
1065 : 401 : case REFERENCE:
1066 : 401 : {
1067 : 401 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1068 : 401 : print_vn_reference_ops (outfile, ref->operands);
1069 : 401 : if (ref->vuse)
1070 : : {
1071 : 389 : fprintf (outfile, "@");
1072 : 389 : 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 : 988 : print_bitmap_set (FILE *outfile, bitmap_set_t set,
1092 : : const char *setname, int blockindex)
1093 : : {
1094 : 988 : fprintf (outfile, "%s[%d] := { ", setname, blockindex);
1095 : 988 : if (set)
1096 : : {
1097 : 988 : bool first = true;
1098 : 988 : unsigned i;
1099 : 988 : bitmap_iterator bi;
1100 : :
1101 : 6105 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1102 : : {
1103 : 5117 : const pre_expr expr = expression_for_id (i);
1104 : :
1105 : 5117 : if (!first)
1106 : 4408 : fprintf (outfile, ", ");
1107 : 5117 : first = false;
1108 : 5117 : print_pre_expr (outfile, expr);
1109 : :
1110 : 5117 : fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1111 : : }
1112 : : }
1113 : 988 : fprintf (outfile, " }\n");
1114 : 988 : }
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 : 4557748 : get_or_alloc_expr_for_constant (tree constant)
1167 : : {
1168 : 4557748 : unsigned int result_id;
1169 : 4557748 : struct pre_expr_d expr;
1170 : 4557748 : pre_expr newexpr;
1171 : :
1172 : 4557748 : expr.kind = CONSTANT;
1173 : 4557748 : PRE_EXPR_CONSTANT (&expr) = constant;
1174 : 4557748 : result_id = lookup_expression_id (&expr);
1175 : 4557748 : if (result_id != 0)
1176 : 3780723 : return expression_for_id (result_id);
1177 : :
1178 : 777025 : newexpr = pre_expr_pool.allocate ();
1179 : 777025 : newexpr->kind = CONSTANT;
1180 : 777025 : newexpr->loc = UNKNOWN_LOCATION;
1181 : 777025 : PRE_EXPR_CONSTANT (newexpr) = constant;
1182 : 777025 : alloc_expression_id (newexpr);
1183 : 777025 : newexpr->value_id = get_or_alloc_constant_value_id (constant);
1184 : 777025 : add_to_value (newexpr->value_id, newexpr);
1185 : 777025 : return newexpr;
1186 : : }
1187 : :
1188 : : /* Return the folded version of T if T, when folded, is a gimple
1189 : : min_invariant or an SSA name. Otherwise, return T. */
1190 : :
1191 : : static pre_expr
1192 : 6918146 : fully_constant_expression (pre_expr e)
1193 : : {
1194 : 6918146 : switch (e->kind)
1195 : : {
1196 : : case CONSTANT:
1197 : : return e;
1198 : 6918146 : case NARY:
1199 : 6918146 : {
1200 : 6918146 : vn_nary_op_t nary = PRE_EXPR_NARY (e);
1201 : 6918146 : tree res = vn_nary_simplify (nary);
1202 : 6918146 : if (!res)
1203 : : return e;
1204 : 2203690 : if (is_gimple_min_invariant (res))
1205 : 1151120 : return get_or_alloc_expr_for_constant (res);
1206 : 1052570 : if (TREE_CODE (res) == SSA_NAME)
1207 : 1052570 : return get_or_alloc_expr_for_name (res);
1208 : : return e;
1209 : : }
1210 : 0 : case REFERENCE:
1211 : 0 : {
1212 : 0 : vn_reference_t ref = PRE_EXPR_REFERENCE (e);
1213 : 0 : tree folded;
1214 : 0 : if ((folded = fully_constant_vn_reference_p (ref)))
1215 : 0 : return get_or_alloc_expr_for_constant (folded);
1216 : : return e;
1217 : : }
1218 : : default:
1219 : : return e;
1220 : : }
1221 : : }
1222 : :
1223 : : /* Translate the VUSE backwards through phi nodes in E->dest, so that
1224 : : it has the value it would have in E->src. Set *SAME_VALID to true
1225 : : in case the new vuse doesn't change the value id of the OPERANDS. */
1226 : :
1227 : : static tree
1228 : 4035406 : translate_vuse_through_block (vec<vn_reference_op_s> operands,
1229 : : alias_set_type set, alias_set_type base_set,
1230 : : tree type, tree vuse, edge e, bool *same_valid)
1231 : : {
1232 : 4035406 : basic_block phiblock = e->dest;
1233 : 4035406 : gimple *phi = SSA_NAME_DEF_STMT (vuse);
1234 : 4035406 : ao_ref ref;
1235 : :
1236 : 4035406 : if (same_valid)
1237 : 2917583 : *same_valid = true;
1238 : :
1239 : : /* If value-numbering provided a memory state for this
1240 : : that dominates PHIBLOCK we can just use that. */
1241 : 4035406 : if (gimple_nop_p (phi)
1242 : 4035406 : || (gimple_bb (phi) != phiblock
1243 : 1029444 : && dominated_by_p (CDI_DOMINATORS, phiblock, gimple_bb (phi))))
1244 : 1697621 : return vuse;
1245 : :
1246 : : /* We have pruned expressions that are killed in PHIBLOCK via
1247 : : prune_clobbered_mems but we have not rewritten the VUSE to the one
1248 : : live at the start of the block. If there is no virtual PHI to translate
1249 : : through return the VUSE live at entry. Otherwise the VUSE to translate
1250 : : is the def of the virtual PHI node. */
1251 : 2337785 : phi = get_virtual_phi (phiblock);
1252 : 2337785 : if (!phi)
1253 : 83616 : return BB_LIVE_VOP_ON_EXIT
1254 : : (get_immediate_dominator (CDI_DOMINATORS, phiblock));
1255 : :
1256 : 2254169 : if (same_valid
1257 : 2254169 : && ao_ref_init_from_vn_reference (&ref, set, base_set, type, operands))
1258 : : {
1259 : 1644594 : bitmap visited = NULL;
1260 : : /* Try to find a vuse that dominates this phi node by skipping
1261 : : non-clobbering statements. */
1262 : 1644594 : unsigned int cnt = param_sccvn_max_alias_queries_per_access;
1263 : 1644594 : vuse = get_continuation_for_phi (phi, &ref, true,
1264 : : cnt, &visited, false, NULL, NULL);
1265 : 1644594 : if (visited)
1266 : 1637812 : BITMAP_FREE (visited);
1267 : : }
1268 : : else
1269 : : vuse = NULL_TREE;
1270 : : /* If we didn't find any, the value ID can't stay the same. */
1271 : 2254169 : if (!vuse && same_valid)
1272 : 1443718 : *same_valid = false;
1273 : :
1274 : : /* ??? We would like to return vuse here as this is the canonical
1275 : : upmost vdef that this reference is associated with. But during
1276 : : insertion of the references into the hash tables we only ever
1277 : : directly insert with their direct gimple_vuse, hence returning
1278 : : something else would make us not find the other expression. */
1279 : 2254169 : return PHI_ARG_DEF (phi, e->dest_idx);
1280 : : }
1281 : :
1282 : : /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1283 : : SET2 *or* SET3. This is used to avoid making a set consisting of the union
1284 : : of PA_IN and ANTIC_IN during insert and phi-translation. */
1285 : :
1286 : : static inline pre_expr
1287 : 22159893 : find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
1288 : : bitmap_set_t set3 = NULL)
1289 : : {
1290 : 22159893 : pre_expr result = NULL;
1291 : :
1292 : 22159893 : if (set1)
1293 : 22037066 : result = bitmap_find_leader (set1, val);
1294 : 22159893 : if (!result && set2)
1295 : 1427366 : result = bitmap_find_leader (set2, val);
1296 : 22159893 : if (!result && set3)
1297 : 0 : result = bitmap_find_leader (set3, val);
1298 : 22159893 : return result;
1299 : : }
1300 : :
1301 : : /* Get the tree type for our PRE expression e. */
1302 : :
1303 : : static tree
1304 : 6758599 : get_expr_type (const pre_expr e)
1305 : : {
1306 : 6758599 : switch (e->kind)
1307 : : {
1308 : 877517 : case NAME:
1309 : 877517 : return TREE_TYPE (PRE_EXPR_NAME (e));
1310 : 166464 : case CONSTANT:
1311 : 166464 : return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1312 : 1213898 : case REFERENCE:
1313 : 1213898 : return PRE_EXPR_REFERENCE (e)->type;
1314 : 4500720 : case NARY:
1315 : 4500720 : return PRE_EXPR_NARY (e)->type;
1316 : : }
1317 : 0 : gcc_unreachable ();
1318 : : }
1319 : :
1320 : : /* Get a representative SSA_NAME for a given expression that is available in B.
1321 : : Since all of our sub-expressions are treated as values, we require
1322 : : them to be SSA_NAME's for simplicity.
1323 : : Prior versions of GVNPRE used to use "value handles" here, so that
1324 : : an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1325 : : either case, the operands are really values (IE we do not expect
1326 : : them to be usable without finding leaders). */
1327 : :
1328 : : static tree
1329 : 17576038 : get_representative_for (const pre_expr e, basic_block b = NULL)
1330 : : {
1331 : 17576038 : tree name, valnum = NULL_TREE;
1332 : 17576038 : unsigned int value_id = get_expr_value_id (e);
1333 : :
1334 : 17576038 : switch (e->kind)
1335 : : {
1336 : 7729134 : case NAME:
1337 : 7729134 : return PRE_EXPR_NAME (e);
1338 : 1759899 : case CONSTANT:
1339 : 1759899 : return PRE_EXPR_CONSTANT (e);
1340 : 8087005 : case NARY:
1341 : 8087005 : case REFERENCE:
1342 : 8087005 : {
1343 : : /* Go through all of the expressions representing this value
1344 : : and pick out an SSA_NAME. */
1345 : 8087005 : unsigned int i;
1346 : 8087005 : bitmap_iterator bi;
1347 : 8087005 : bitmap exprs = value_expressions[value_id];
1348 : 20839851 : EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1349 : : {
1350 : 17115458 : pre_expr rep = expression_for_id (i);
1351 : 17115458 : if (rep->kind == NAME)
1352 : : {
1353 : 7840936 : tree name = PRE_EXPR_NAME (rep);
1354 : 7840936 : valnum = VN_INFO (name)->valnum;
1355 : 7840936 : gimple *def = SSA_NAME_DEF_STMT (name);
1356 : : /* We have to return either a new representative or one
1357 : : that can be used for expression simplification and thus
1358 : : is available in B. */
1359 : 7840936 : if (! b
1360 : 7596626 : || gimple_nop_p (def)
1361 : 11591393 : || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
1362 : 4362612 : return name;
1363 : : }
1364 : 9274522 : else if (rep->kind == CONSTANT)
1365 : 0 : return PRE_EXPR_CONSTANT (rep);
1366 : : }
1367 : : }
1368 : 3724393 : break;
1369 : : }
1370 : :
1371 : : /* If we reached here we couldn't find an SSA_NAME. This can
1372 : : happen when we've discovered a value that has never appeared in
1373 : : the program as set to an SSA_NAME, as the result of phi translation.
1374 : : Create one here.
1375 : : ??? We should be able to re-use this when we insert the statement
1376 : : to compute it. */
1377 : 3724393 : name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1378 : 3724393 : vn_ssa_aux_t vn_info = VN_INFO (name);
1379 : 3724393 : vn_info->value_id = value_id;
1380 : 3724393 : vn_info->valnum = valnum ? valnum : name;
1381 : 3724393 : vn_info->visited = true;
1382 : : /* ??? For now mark this SSA name for release by VN. */
1383 : 3724393 : vn_info->needs_insertion = true;
1384 : 3724393 : add_to_value (value_id, get_or_alloc_expr_for_name (name));
1385 : 3724393 : if (dump_file && (dump_flags & TDF_DETAILS))
1386 : : {
1387 : 65 : fprintf (dump_file, "Created SSA_NAME representative ");
1388 : 65 : print_generic_expr (dump_file, name);
1389 : 65 : fprintf (dump_file, " for expression:");
1390 : 65 : print_pre_expr (dump_file, e);
1391 : 65 : fprintf (dump_file, " (%04d)\n", value_id);
1392 : : }
1393 : :
1394 : : return name;
1395 : : }
1396 : :
1397 : :
1398 : : static pre_expr
1399 : : phi_translate (bitmap_set_t, pre_expr, bitmap_set_t, bitmap_set_t, edge);
1400 : :
1401 : : /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1402 : : the phis in PRED. Return NULL if we can't find a leader for each part
1403 : : of the translated expression. */
1404 : :
1405 : : static pre_expr
1406 : 43377512 : phi_translate_1 (bitmap_set_t dest,
1407 : : pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
1408 : : {
1409 : 43377512 : basic_block pred = e->src;
1410 : 43377512 : basic_block phiblock = e->dest;
1411 : 43377512 : location_t expr_loc = expr->loc;
1412 : 43377512 : switch (expr->kind)
1413 : : {
1414 : 17122546 : case NARY:
1415 : 17122546 : {
1416 : 17122546 : unsigned int i;
1417 : 17122546 : bool changed = false;
1418 : 17122546 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1419 : 17122546 : vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1420 : : sizeof_vn_nary_op (nary->length));
1421 : 17122546 : memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1422 : :
1423 : 40179019 : for (i = 0; i < newnary->length; i++)
1424 : : {
1425 : 26372686 : if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1426 : 8170627 : continue;
1427 : : else
1428 : : {
1429 : 18202059 : pre_expr leader, result;
1430 : 18202059 : unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1431 : 18202059 : leader = find_leader_in_sets (op_val_id, set1, set2);
1432 : 18202059 : result = phi_translate (dest, leader, set1, set2, e);
1433 : 18202059 : if (result)
1434 : : /* If op has a leader in the sets we translate make
1435 : : sure to use the value of the translated expression.
1436 : : We might need a new representative for that. */
1437 : 14885846 : newnary->op[i] = get_representative_for (result, pred);
1438 : : else if (!result)
1439 : : return NULL;
1440 : :
1441 : 14885846 : changed |= newnary->op[i] != nary->op[i];
1442 : : }
1443 : : }
1444 : 13806333 : if (changed)
1445 : : {
1446 : 6918146 : pre_expr constant;
1447 : 6918146 : unsigned int new_val_id;
1448 : :
1449 : 6918146 : PRE_EXPR_NARY (expr) = newnary;
1450 : 6918146 : constant = fully_constant_expression (expr);
1451 : 6918146 : PRE_EXPR_NARY (expr) = nary;
1452 : 6918146 : if (constant != expr)
1453 : : {
1454 : : /* For non-CONSTANTs we have to make sure we can eventually
1455 : : insert the expression. Which means we need to have a
1456 : : leader for it. */
1457 : 2203690 : if (constant->kind != CONSTANT)
1458 : : {
1459 : : /* Do not allow simplifications to non-constants over
1460 : : backedges as this will likely result in a loop PHI node
1461 : : to be inserted and increased register pressure.
1462 : : See PR77498 - this avoids doing predcoms work in
1463 : : a less efficient way. */
1464 : 1052570 : if (e->flags & EDGE_DFS_BACK)
1465 : : ;
1466 : : else
1467 : : {
1468 : 979901 : unsigned value_id = get_expr_value_id (constant);
1469 : : /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1470 : : dest has what we computed into ANTIC_OUT sofar
1471 : : so pick from that - since topological sorting
1472 : : by sorted_array_from_bitmap_set isn't perfect
1473 : : we may lose some cases here. */
1474 : 1959802 : constant = find_leader_in_sets (value_id, dest,
1475 : 979901 : AVAIL_OUT (pred));
1476 : 979901 : if (constant)
1477 : : {
1478 : 282117 : if (dump_file && (dump_flags & TDF_DETAILS))
1479 : : {
1480 : 7 : fprintf (dump_file, "simplifying ");
1481 : 7 : print_pre_expr (dump_file, expr);
1482 : 7 : fprintf (dump_file, " translated %d -> %d to ",
1483 : : phiblock->index, pred->index);
1484 : 7 : PRE_EXPR_NARY (expr) = newnary;
1485 : 7 : print_pre_expr (dump_file, expr);
1486 : 7 : PRE_EXPR_NARY (expr) = nary;
1487 : 7 : fprintf (dump_file, " to ");
1488 : 7 : print_pre_expr (dump_file, constant);
1489 : 7 : fprintf (dump_file, "\n");
1490 : : }
1491 : 282117 : return constant;
1492 : : }
1493 : : }
1494 : : }
1495 : : else
1496 : : return constant;
1497 : : }
1498 : :
1499 : 10969818 : tree result = vn_nary_op_lookup_pieces (newnary->length,
1500 : 5484909 : newnary->opcode,
1501 : : newnary->type,
1502 : : &newnary->op[0],
1503 : : &nary);
1504 : 5484909 : if (result && is_gimple_min_invariant (result))
1505 : 0 : return get_or_alloc_expr_for_constant (result);
1506 : :
1507 : 5484909 : if (!nary || nary->predicated_values)
1508 : : new_val_id = 0;
1509 : : else
1510 : 737549 : new_val_id = nary->value_id;
1511 : 5484909 : expr = get_or_alloc_expr_for_nary (newnary, new_val_id, expr_loc);
1512 : 5484909 : add_to_value (get_expr_value_id (expr), expr);
1513 : : }
1514 : : return expr;
1515 : : }
1516 : 4355096 : break;
1517 : :
1518 : 4355096 : case REFERENCE:
1519 : 4355096 : {
1520 : 4355096 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1521 : 4355096 : vec<vn_reference_op_s> operands = ref->operands;
1522 : 4355096 : tree vuse = ref->vuse;
1523 : 4355096 : tree newvuse = vuse;
1524 : 4355096 : vec<vn_reference_op_s> newoperands = vNULL;
1525 : 4355096 : bool changed = false, same_valid = true;
1526 : 4355096 : unsigned int i, n;
1527 : 4355096 : vn_reference_op_t operand;
1528 : 4355096 : vn_reference_t newref;
1529 : :
1530 : 16549309 : for (i = 0; operands.iterate (i, &operand); i++)
1531 : : {
1532 : 12481954 : pre_expr opresult;
1533 : 12481954 : pre_expr leader;
1534 : 12481954 : tree op[3];
1535 : 12481954 : tree type = operand->type;
1536 : 12481954 : vn_reference_op_s newop = *operand;
1537 : 12481954 : op[0] = operand->op0;
1538 : 12481954 : op[1] = operand->op1;
1539 : 12481954 : op[2] = operand->op2;
1540 : 49064653 : for (n = 0; n < 3; ++n)
1541 : : {
1542 : 36870440 : unsigned int op_val_id;
1543 : 36870440 : if (!op[n])
1544 : 22118292 : continue;
1545 : 14752148 : if (TREE_CODE (op[n]) != SSA_NAME)
1546 : : {
1547 : : /* We can't possibly insert these. */
1548 : 11774215 : if (n != 0
1549 : 11774215 : && !is_gimple_min_invariant (op[n]))
1550 : : break;
1551 : 11774215 : continue;
1552 : : }
1553 : 2977933 : op_val_id = VN_INFO (op[n])->value_id;
1554 : 2977933 : leader = find_leader_in_sets (op_val_id, set1, set2);
1555 : 2977933 : opresult = phi_translate (dest, leader, set1, set2, e);
1556 : 2977933 : if (opresult)
1557 : : {
1558 : 2690192 : tree name = get_representative_for (opresult);
1559 : 2690192 : changed |= name != op[n];
1560 : 2690192 : op[n] = name;
1561 : : }
1562 : : else if (!opresult)
1563 : : break;
1564 : : }
1565 : 12481954 : if (n != 3)
1566 : : {
1567 : 287741 : newoperands.release ();
1568 : 287741 : return NULL;
1569 : : }
1570 : : /* When we translate a MEM_REF across a backedge and we have
1571 : : restrict info that's not from our functions parameters
1572 : : we have to remap it since we now may deal with a different
1573 : : instance where the dependence info is no longer valid.
1574 : : See PR102970. Note instead of keeping a remapping table
1575 : : per backedge we simply throw away restrict info. */
1576 : 12194213 : if ((newop.opcode == MEM_REF
1577 : 12194213 : || newop.opcode == TARGET_MEM_REF)
1578 : 4116127 : && newop.clique > 1
1579 : 123456 : && (e->flags & EDGE_DFS_BACK))
1580 : : {
1581 : : newop.clique = 0;
1582 : : newop.base = 0;
1583 : : changed = true;
1584 : : }
1585 : 12180730 : if (!changed)
1586 : 9916315 : continue;
1587 : 2277898 : if (!newoperands.exists ())
1588 : 1133767 : newoperands = operands.copy ();
1589 : : /* We may have changed from an SSA_NAME to a constant */
1590 : 2277898 : if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1591 : : newop.opcode = TREE_CODE (op[0]);
1592 : 2277898 : newop.type = type;
1593 : 2277898 : newop.op0 = op[0];
1594 : 2277898 : newop.op1 = op[1];
1595 : 2277898 : newop.op2 = op[2];
1596 : 2277898 : newoperands[i] = newop;
1597 : : }
1598 : 8134710 : gcc_checking_assert (i == operands.length ());
1599 : :
1600 : 4067355 : if (vuse)
1601 : : {
1602 : 9870572 : newvuse = translate_vuse_through_block (newoperands.exists ()
1603 : 4035406 : ? newoperands : operands,
1604 : : ref->set, ref->base_set,
1605 : : ref->type, vuse, e,
1606 : : changed
1607 : : ? NULL : &same_valid);
1608 : 4035406 : if (newvuse == NULL_TREE)
1609 : : {
1610 : 0 : newoperands.release ();
1611 : 0 : return NULL;
1612 : : }
1613 : : }
1614 : :
1615 : 4067355 : if (changed || newvuse != vuse)
1616 : : {
1617 : 2880668 : unsigned int new_val_id;
1618 : :
1619 : 4628416 : tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1620 : : ref->base_set,
1621 : : ref->type,
1622 : 2880668 : newoperands.exists ()
1623 : 2880668 : ? newoperands : operands,
1624 : : &newref, VN_WALK);
1625 : 2880668 : if (result)
1626 : 562556 : newoperands.release ();
1627 : :
1628 : : /* We can always insert constants, so if we have a partial
1629 : : redundant constant load of another type try to translate it
1630 : : to a constant of appropriate type. */
1631 : 562556 : if (result && is_gimple_min_invariant (result))
1632 : : {
1633 : 64144 : tree tem = result;
1634 : 64144 : if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1635 : : {
1636 : 47 : tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1637 : 47 : if (tem && !is_gimple_min_invariant (tem))
1638 : : tem = NULL_TREE;
1639 : : }
1640 : 64144 : if (tem)
1641 : 64144 : return get_or_alloc_expr_for_constant (tem);
1642 : : }
1643 : :
1644 : : /* If we'd have to convert things we would need to validate
1645 : : if we can insert the translated expression. So fail
1646 : : here for now - we cannot insert an alias with a different
1647 : : type in the VN tables either, as that would assert. */
1648 : 2816524 : if (result
1649 : 2816524 : && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1650 : : return NULL;
1651 : 2318112 : else if (!result && newref
1652 : 2993167 : && !useless_type_conversion_p (ref->type, newref->type))
1653 : : {
1654 : 224 : newoperands.release ();
1655 : 224 : return NULL;
1656 : : }
1657 : :
1658 : 2815281 : if (newref)
1659 : 674831 : new_val_id = newref->value_id;
1660 : : else
1661 : : {
1662 : 2140450 : if (changed || !same_valid)
1663 : 2082329 : new_val_id = get_next_value_id ();
1664 : : else
1665 : 58121 : new_val_id = ref->value_id;
1666 : 2140450 : if (!newoperands.exists ())
1667 : 1190494 : newoperands = operands.copy ();
1668 : 2140450 : newref = vn_reference_insert_pieces (newvuse, ref->set,
1669 : : ref->base_set,
1670 : : ref->offset, ref->max_size,
1671 : : ref->type, newoperands,
1672 : : result, new_val_id);
1673 : 2140450 : newoperands = vNULL;
1674 : : }
1675 : 2815281 : expr = get_or_alloc_expr_for_reference (newref, expr_loc);
1676 : 2815281 : add_to_value (new_val_id, expr);
1677 : : }
1678 : 4001968 : newoperands.release ();
1679 : 4001968 : return expr;
1680 : : }
1681 : 21899870 : break;
1682 : :
1683 : 21899870 : case NAME:
1684 : 21899870 : {
1685 : 21899870 : tree name = PRE_EXPR_NAME (expr);
1686 : 21899870 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1687 : : /* If the SSA name is defined by a PHI node in this block,
1688 : : translate it. */
1689 : 21899870 : if (gimple_code (def_stmt) == GIMPLE_PHI
1690 : 21899870 : && gimple_bb (def_stmt) == phiblock)
1691 : : {
1692 : 6929517 : tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1693 : :
1694 : : /* Handle constant. */
1695 : 6929517 : if (is_gimple_min_invariant (def))
1696 : 2080055 : return get_or_alloc_expr_for_constant (def);
1697 : :
1698 : 4849462 : return get_or_alloc_expr_for_name (def);
1699 : : }
1700 : : /* Otherwise return it unchanged - it will get removed if its
1701 : : value is not available in PREDs AVAIL_OUT set of expressions
1702 : : by the subtraction of TMP_GEN. */
1703 : : return expr;
1704 : : }
1705 : :
1706 : 0 : default:
1707 : 0 : gcc_unreachable ();
1708 : : }
1709 : : }
1710 : :
1711 : : /* Wrapper around phi_translate_1 providing caching functionality. */
1712 : :
1713 : : static pre_expr
1714 : 80796049 : phi_translate (bitmap_set_t dest, pre_expr expr,
1715 : : bitmap_set_t set1, bitmap_set_t set2, edge e)
1716 : : {
1717 : 80796049 : expr_pred_trans_t slot = NULL;
1718 : 80796049 : pre_expr phitrans;
1719 : :
1720 : 80796049 : if (!expr)
1721 : : return NULL;
1722 : :
1723 : : /* Constants contain no values that need translation. */
1724 : 78990386 : if (expr->kind == CONSTANT)
1725 : : return expr;
1726 : :
1727 : 78989802 : if (value_id_constant_p (get_expr_value_id (expr)))
1728 : : return expr;
1729 : :
1730 : : /* Don't add translations of NAMEs as those are cheap to translate. */
1731 : 78989802 : if (expr->kind != NAME)
1732 : : {
1733 : 57089932 : if (phi_trans_add (&slot, expr, e->src))
1734 : 35612290 : return slot->v == 0 ? NULL : expression_for_id (slot->v);
1735 : : /* Store NULL for the value we want to return in the case of
1736 : : recursing. */
1737 : 21477642 : slot->v = 0;
1738 : : }
1739 : :
1740 : : /* Translate. */
1741 : 43377512 : basic_block saved_valueize_bb = vn_context_bb;
1742 : 43377512 : vn_context_bb = e->src;
1743 : 43377512 : phitrans = phi_translate_1 (dest, expr, set1, set2, e);
1744 : 43377512 : vn_context_bb = saved_valueize_bb;
1745 : :
1746 : 43377512 : if (slot)
1747 : : {
1748 : : /* We may have reallocated. */
1749 : 21477642 : phi_trans_add (&slot, expr, e->src);
1750 : 21477642 : if (phitrans)
1751 : 17872445 : slot->v = get_expression_id (phitrans);
1752 : : else
1753 : : /* Remove failed translations again, they cause insert
1754 : : iteration to not pick up new opportunities reliably. */
1755 : 3605197 : PHI_TRANS_TABLE (e->src)->clear_slot (slot);
1756 : : }
1757 : :
1758 : : return phitrans;
1759 : : }
1760 : :
1761 : :
1762 : : /* For each expression in SET, translate the values through phi nodes
1763 : : in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1764 : : expressions in DEST. */
1765 : :
1766 : : static void
1767 : 18907035 : phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
1768 : : {
1769 : 18907035 : bitmap_iterator bi;
1770 : 18907035 : unsigned int i;
1771 : :
1772 : 18907035 : if (gimple_seq_empty_p (phi_nodes (e->dest)))
1773 : : {
1774 : 12775511 : bitmap_set_copy (dest, set);
1775 : 12775511 : return;
1776 : : }
1777 : :
1778 : : /* Allocate the phi-translation cache where we have an idea about
1779 : : its size. hash-table implementation internals tell us that
1780 : : allocating the table to fit twice the number of elements will
1781 : : make sure we do not usually re-allocate. */
1782 : 6131524 : if (!PHI_TRANS_TABLE (e->src))
1783 : 5492060 : PHI_TRANS_TABLE (e->src) = new hash_table<expr_pred_trans_d>
1784 : 5492060 : (2 * bitmap_count_bits (&set->expressions));
1785 : 39864368 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1786 : : {
1787 : 33732844 : pre_expr expr = expression_for_id (i);
1788 : 33732844 : pre_expr translated = phi_translate (dest, expr, set, NULL, e);
1789 : 33732844 : if (!translated)
1790 : 1805855 : continue;
1791 : :
1792 : 31926989 : bitmap_insert_into_set (dest, translated);
1793 : : }
1794 : : }
1795 : :
1796 : : /* Find the leader for a value (i.e., the name representing that
1797 : : value) in a given set, and return it. Return NULL if no leader
1798 : : is found. */
1799 : :
1800 : : static pre_expr
1801 : 51471580 : bitmap_find_leader (bitmap_set_t set, unsigned int val)
1802 : : {
1803 : 51471580 : if (value_id_constant_p (val))
1804 : 1625345 : return constant_value_expressions[-val];
1805 : :
1806 : 49846235 : if (bitmap_set_contains_value (set, val))
1807 : : {
1808 : : /* Rather than walk the entire bitmap of expressions, and see
1809 : : whether any of them has the value we are looking for, we look
1810 : : at the reverse mapping, which tells us the set of expressions
1811 : : that have a given value (IE value->expressions with that
1812 : : value) and see if any of those expressions are in our set.
1813 : : The number of expressions per value is usually significantly
1814 : : less than the number of expressions in the set. In fact, for
1815 : : large testcases, doing it this way is roughly 5-10x faster
1816 : : than walking the bitmap.
1817 : : If this is somehow a significant lose for some cases, we can
1818 : : choose which set to walk based on which set is smaller. */
1819 : 22591159 : unsigned int i;
1820 : 22591159 : bitmap_iterator bi;
1821 : 22591159 : bitmap exprset = value_expressions[val];
1822 : :
1823 : 22591159 : if (!exprset->first->next)
1824 : 27924160 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1825 : 26448031 : if (bitmap_bit_p (&set->expressions, i))
1826 : 21067990 : return expression_for_id (i);
1827 : :
1828 : 5130507 : EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1829 : 3607338 : return expression_for_id (i);
1830 : : }
1831 : : return NULL;
1832 : : }
1833 : :
1834 : : /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1835 : : BLOCK by seeing if it is not killed in the block. Note that we are
1836 : : only determining whether there is a store that kills it. Because
1837 : : of the order in which clean iterates over values, we are guaranteed
1838 : : that altered operands will have caused us to be eliminated from the
1839 : : ANTIC_IN set already. */
1840 : :
1841 : : static bool
1842 : 1451061 : value_dies_in_block_x (pre_expr expr, basic_block block)
1843 : : {
1844 : 1451061 : tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
1845 : 1451061 : vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
1846 : 1451061 : gimple *def;
1847 : 1451061 : gimple_stmt_iterator gsi;
1848 : 1451061 : unsigned id = get_expression_id (expr);
1849 : 1451061 : bool res = false;
1850 : 1451061 : ao_ref ref;
1851 : :
1852 : 1451061 : if (!vuse)
1853 : : return false;
1854 : :
1855 : : /* Lookup a previously calculated result. */
1856 : 1451061 : if (EXPR_DIES (block)
1857 : 1451061 : && bitmap_bit_p (EXPR_DIES (block), id * 2))
1858 : 128871 : return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
1859 : :
1860 : : /* A memory expression {e, VUSE} dies in the block if there is a
1861 : : statement that may clobber e. If, starting statement walk from the
1862 : : top of the basic block, a statement uses VUSE there can be no kill
1863 : : inbetween that use and the original statement that loaded {e, VUSE},
1864 : : so we can stop walking. */
1865 : 1322190 : ref.base = NULL_TREE;
1866 : 11098509 : for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
1867 : : {
1868 : 9362200 : tree def_vuse, def_vdef;
1869 : 9362200 : def = gsi_stmt (gsi);
1870 : 9362200 : def_vuse = gimple_vuse (def);
1871 : 9362200 : def_vdef = gimple_vdef (def);
1872 : :
1873 : : /* Not a memory statement. */
1874 : 9362200 : if (!def_vuse)
1875 : 6477342 : continue;
1876 : :
1877 : : /* Not a may-def. */
1878 : 2884858 : if (!def_vdef)
1879 : : {
1880 : : /* A load with the same VUSE, we're done. */
1881 : 872261 : if (def_vuse == vuse)
1882 : : break;
1883 : :
1884 : 603832 : continue;
1885 : : }
1886 : :
1887 : : /* Init ref only if we really need it. */
1888 : 2012597 : if (ref.base == NULL_TREE
1889 : 3014769 : && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->base_set,
1890 : 1002172 : refx->type, refx->operands))
1891 : : {
1892 : : res = true;
1893 : : break;
1894 : : }
1895 : : /* If the statement may clobber expr, it dies. */
1896 : 1980764 : if (stmt_may_clobber_ref_p_1 (def, &ref))
1897 : : {
1898 : : res = true;
1899 : : break;
1900 : : }
1901 : : }
1902 : :
1903 : : /* Remember the result. */
1904 : 1322190 : if (!EXPR_DIES (block))
1905 : 630254 : EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
1906 : 1322190 : bitmap_set_bit (EXPR_DIES (block), id * 2);
1907 : 1322190 : if (res)
1908 : 639642 : bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
1909 : :
1910 : : return res;
1911 : : }
1912 : :
1913 : :
1914 : : /* Determine if OP is valid in SET1 U SET2, which it is when the union
1915 : : contains its value-id. */
1916 : :
1917 : : static bool
1918 : 232089849 : op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
1919 : : {
1920 : 232089849 : if (op && TREE_CODE (op) == SSA_NAME)
1921 : : {
1922 : 72106302 : unsigned int value_id = VN_INFO (op)->value_id;
1923 : 144209169 : if (!(bitmap_set_contains_value (set1, value_id)
1924 : 2178675 : || (set2 && bitmap_set_contains_value (set2, value_id))))
1925 : 1532653 : return false;
1926 : : }
1927 : : return true;
1928 : : }
1929 : :
1930 : : /* Determine if the expression EXPR is valid in SET1 U SET2.
1931 : : ONLY SET2 CAN BE NULL.
1932 : : This means that we have a leader for each part of the expression
1933 : : (if it consists of values), or the expression is an SSA_NAME.
1934 : : For loads/calls, we also see if the vuse is killed in this block. */
1935 : :
1936 : : static bool
1937 : 111841990 : valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
1938 : : {
1939 : 111841990 : switch (expr->kind)
1940 : : {
1941 : : case NAME:
1942 : : /* By construction all NAMEs are available. Non-available
1943 : : NAMEs are removed by subtracting TMP_GEN from the sets. */
1944 : : return true;
1945 : 52864490 : case NARY:
1946 : 52864490 : {
1947 : 52864490 : unsigned int i;
1948 : 52864490 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1949 : 138840251 : for (i = 0; i < nary->length; i++)
1950 : 87361334 : if (!op_valid_in_sets (set1, set2, nary->op[i]))
1951 : : return false;
1952 : : return true;
1953 : : }
1954 : 16600163 : break;
1955 : 16600163 : case REFERENCE:
1956 : 16600163 : {
1957 : 16600163 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1958 : 16600163 : vn_reference_op_t vro;
1959 : 16600163 : unsigned int i;
1960 : :
1961 : 64793948 : FOR_EACH_VEC_ELT (ref->operands, i, vro)
1962 : : {
1963 : 48340865 : if (!op_valid_in_sets (set1, set2, vro->op0)
1964 : 48193825 : || !op_valid_in_sets (set1, set2, vro->op1)
1965 : 96534690 : || !op_valid_in_sets (set1, set2, vro->op2))
1966 : 147080 : return false;
1967 : : }
1968 : : return true;
1969 : : }
1970 : 0 : default:
1971 : 0 : gcc_unreachable ();
1972 : : }
1973 : : }
1974 : :
1975 : : /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
1976 : : This means expressions that are made up of values we have no leaders for
1977 : : in SET1 or SET2. */
1978 : :
1979 : : static void
1980 : 13466132 : clean (bitmap_set_t set1, bitmap_set_t set2 = NULL)
1981 : : {
1982 : 13466132 : vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1);
1983 : 13466132 : pre_expr expr;
1984 : 13466132 : int i;
1985 : :
1986 : 70053155 : FOR_EACH_VEC_ELT (exprs, i, expr)
1987 : : {
1988 : 56587023 : if (!valid_in_sets (set1, set2, expr))
1989 : : {
1990 : 1532635 : unsigned int val = get_expr_value_id (expr);
1991 : 1532635 : bitmap_clear_bit (&set1->expressions, get_expression_id (expr));
1992 : : /* We are entered with possibly multiple expressions for a value
1993 : : so before removing a value from the set see if there's an
1994 : : expression for it left. */
1995 : 1532635 : if (! bitmap_find_leader (set1, val))
1996 : 1523169 : bitmap_clear_bit (&set1->values, val);
1997 : : }
1998 : : }
1999 : 13466132 : exprs.release ();
2000 : :
2001 : 13466132 : if (flag_checking)
2002 : : {
2003 : 13465945 : unsigned j;
2004 : 13465945 : bitmap_iterator bi;
2005 : 68520128 : FOR_EACH_EXPR_ID_IN_SET (set1, j, bi)
2006 : 55054183 : gcc_assert (valid_in_sets (set1, set2, expression_for_id (j)));
2007 : : }
2008 : 13466132 : }
2009 : :
2010 : : /* Clean the set of expressions that are no longer valid in SET because
2011 : : they are clobbered in BLOCK or because they trap and may not be executed.
2012 : : When CLEAN_TRAPS is true remove all possibly trapping expressions. */
2013 : :
2014 : : static void
2015 : 15568825 : prune_clobbered_mems (bitmap_set_t set, basic_block block, bool clean_traps)
2016 : : {
2017 : 15568825 : bitmap_iterator bi;
2018 : 15568825 : unsigned i;
2019 : 15568825 : unsigned to_remove = -1U;
2020 : 15568825 : bool any_removed = false;
2021 : :
2022 : 68735175 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2023 : : {
2024 : : /* Remove queued expr. */
2025 : 53166350 : if (to_remove != -1U)
2026 : : {
2027 : 510744 : bitmap_clear_bit (&set->expressions, to_remove);
2028 : 510744 : any_removed = true;
2029 : 510744 : to_remove = -1U;
2030 : : }
2031 : :
2032 : 53166350 : pre_expr expr = expression_for_id (i);
2033 : 53166350 : if (expr->kind == REFERENCE)
2034 : : {
2035 : 6708898 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2036 : 6708898 : if (ref->vuse)
2037 : : {
2038 : 6518396 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2039 : 6518396 : if (!gimple_nop_p (def_stmt)
2040 : : /* If value-numbering provided a memory state for this
2041 : : that dominates BLOCK we're done, otherwise we have
2042 : : to check if the value dies in BLOCK. */
2043 : 8022730 : && !(gimple_bb (def_stmt) != block
2044 : 3395544 : && dominated_by_p (CDI_DOMINATORS,
2045 : 3395544 : block, gimple_bb (def_stmt)))
2046 : 7969457 : && value_dies_in_block_x (expr, block))
2047 : : to_remove = i;
2048 : : }
2049 : : /* If the REFERENCE may trap make sure the block does not contain
2050 : : a possible exit point.
2051 : : ??? This is overly conservative if we translate AVAIL_OUT
2052 : : as the available expression might be after the exit point. */
2053 : 6156108 : if ((BB_MAY_NOTRETURN (block) || clean_traps)
2054 : 6952298 : && vn_reference_may_trap (ref))
2055 : : to_remove = i;
2056 : : }
2057 : 46457452 : else if (expr->kind == NARY)
2058 : : {
2059 : 25429249 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2060 : : /* If the NARY may trap make sure the block does not contain
2061 : : a possible exit point.
2062 : : ??? This is overly conservative if we translate AVAIL_OUT
2063 : : as the available expression might be after the exit point. */
2064 : 21658966 : if ((BB_MAY_NOTRETURN (block) || clean_traps)
2065 : 26242892 : && vn_nary_may_trap (nary))
2066 : : to_remove = i;
2067 : : }
2068 : : }
2069 : :
2070 : : /* Remove queued expr. */
2071 : 15568825 : if (to_remove != -1U)
2072 : : {
2073 : 360696 : bitmap_clear_bit (&set->expressions, to_remove);
2074 : 360696 : any_removed = true;
2075 : : }
2076 : :
2077 : : /* Above we only removed expressions, now clean the set of values
2078 : : which no longer have any corresponding expression. We cannot
2079 : : clear the value at the time we remove an expression since there
2080 : : may be multiple expressions per value.
2081 : : If we'd queue possibly to be removed values we could use
2082 : : the bitmap_find_leader way to see if there's still an expression
2083 : : for it. For some ratio of to be removed values and number of
2084 : : values/expressions in the set this might be faster than rebuilding
2085 : : the value-set. */
2086 : 15568825 : if (any_removed)
2087 : : {
2088 : 525263 : bitmap_clear (&set->values);
2089 : 3008838 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2090 : : {
2091 : 2483575 : pre_expr expr = expression_for_id (i);
2092 : 2483575 : unsigned int value_id = get_expr_value_id (expr);
2093 : 2483575 : bitmap_set_bit (&set->values, value_id);
2094 : : }
2095 : : }
2096 : 15568825 : }
2097 : :
2098 : : /* Compute the ANTIC set for BLOCK.
2099 : :
2100 : : If succs(BLOCK) > 1 then
2101 : : ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2102 : : else if succs(BLOCK) == 1 then
2103 : : ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2104 : :
2105 : : ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2106 : :
2107 : : Note that clean() is deferred until after the iteration. */
2108 : :
2109 : : static bool
2110 : 14488350 : compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2111 : : {
2112 : 14488350 : bitmap_set_t S, old, ANTIC_OUT;
2113 : 14488350 : edge e;
2114 : 14488350 : edge_iterator ei;
2115 : :
2116 : 14488350 : bool was_visited = BB_VISITED (block);
2117 : 14488350 : bool changed = ! BB_VISITED (block);
2118 : 14488350 : bool any_max_on_edge = false;
2119 : :
2120 : 14488350 : BB_VISITED (block) = 1;
2121 : 14488350 : old = ANTIC_OUT = S = NULL;
2122 : :
2123 : : /* If any edges from predecessors are abnormal, antic_in is empty,
2124 : : so do nothing. */
2125 : 14488350 : if (block_has_abnormal_pred_edge)
2126 : 4177 : goto maybe_dump_sets;
2127 : :
2128 : 14484173 : old = ANTIC_IN (block);
2129 : 14484173 : ANTIC_OUT = bitmap_set_new ();
2130 : :
2131 : : /* If the block has no successors, ANTIC_OUT is empty. */
2132 : 14484173 : if (EDGE_COUNT (block->succs) == 0)
2133 : : ;
2134 : : /* If we have one successor, we could have some phi nodes to
2135 : : translate through. */
2136 : 14484173 : else if (single_succ_p (block))
2137 : : {
2138 : 9223024 : e = single_succ_edge (block);
2139 : 9223024 : gcc_assert (BB_VISITED (e->dest));
2140 : 9223024 : phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
2141 : : }
2142 : : /* If we have multiple successors, we take the intersection of all of
2143 : : them. Note that in the case of loop exit phi nodes, we may have
2144 : : phis to translate through. */
2145 : : else
2146 : : {
2147 : 5261149 : size_t i;
2148 : 5261149 : edge first = NULL;
2149 : :
2150 : 5261149 : auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2151 : 15897652 : FOR_EACH_EDGE (e, ei, block->succs)
2152 : : {
2153 : 10636503 : if (!first
2154 : 5687858 : && BB_VISITED (e->dest))
2155 : : first = e;
2156 : 5375354 : else if (BB_VISITED (e->dest))
2157 : 4774932 : worklist.quick_push (e);
2158 : : else
2159 : : {
2160 : : /* Unvisited successors get their ANTIC_IN replaced by the
2161 : : maximal set to arrive at a maximum ANTIC_IN solution.
2162 : : We can ignore them in the intersection operation and thus
2163 : : need not explicitely represent that maximum solution. */
2164 : 600422 : any_max_on_edge = true;
2165 : 600422 : if (dump_file && (dump_flags & TDF_DETAILS))
2166 : 18 : fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n",
2167 : 18 : e->src->index, e->dest->index);
2168 : : }
2169 : : }
2170 : :
2171 : : /* Of multiple successors we have to have visited one already
2172 : : which is guaranteed by iteration order. */
2173 : 5261149 : gcc_assert (first != NULL);
2174 : :
2175 : 5261149 : phi_translate_set (ANTIC_OUT, ANTIC_IN (first->dest), first);
2176 : :
2177 : : /* If we have multiple successors we need to intersect the ANTIC_OUT
2178 : : sets. For values that's a simple intersection but for
2179 : : expressions it is a union. Given we want to have a single
2180 : : expression per value in our sets we have to canonicalize.
2181 : : Avoid randomness and running into cycles like for PR82129 and
2182 : : canonicalize the expression we choose to the one with the
2183 : : lowest id. This requires we actually compute the union first. */
2184 : 10036081 : FOR_EACH_VEC_ELT (worklist, i, e)
2185 : : {
2186 : 4774932 : if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2187 : : {
2188 : 2230 : bitmap_set_t tmp = bitmap_set_new ();
2189 : 2230 : phi_translate_set (tmp, ANTIC_IN (e->dest), e);
2190 : 2230 : bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
2191 : 2230 : bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
2192 : 2230 : bitmap_set_free (tmp);
2193 : : }
2194 : : else
2195 : : {
2196 : 4772702 : bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
2197 : 4772702 : bitmap_ior_into (&ANTIC_OUT->expressions,
2198 : 4772702 : &ANTIC_IN (e->dest)->expressions);
2199 : : }
2200 : : }
2201 : 10522298 : if (! worklist.is_empty ())
2202 : : {
2203 : : /* Prune expressions not in the value set. */
2204 : 4664833 : bitmap_iterator bi;
2205 : 4664833 : unsigned int i;
2206 : 4664833 : unsigned int to_clear = -1U;
2207 : 32952056 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
2208 : : {
2209 : 28287223 : if (to_clear != -1U)
2210 : : {
2211 : 14936512 : bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2212 : 14936512 : to_clear = -1U;
2213 : : }
2214 : 28287223 : pre_expr expr = expression_for_id (i);
2215 : 28287223 : unsigned int value_id = get_expr_value_id (expr);
2216 : 28287223 : if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
2217 : 18404515 : to_clear = i;
2218 : : }
2219 : 4664833 : if (to_clear != -1U)
2220 : 3468003 : bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2221 : : }
2222 : 5261149 : }
2223 : :
2224 : : /* Dump ANTIC_OUT before it's pruned. */
2225 : 14484173 : if (dump_file && (dump_flags & TDF_DETAILS))
2226 : 176 : print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2227 : :
2228 : : /* Prune expressions that are clobbered in block and thus become
2229 : : invalid if translated from ANTIC_OUT to ANTIC_IN. */
2230 : 14484173 : prune_clobbered_mems (ANTIC_OUT, block, any_max_on_edge);
2231 : :
2232 : : /* Generate ANTIC_OUT - TMP_GEN. */
2233 : 14484173 : S = bitmap_set_subtract_expressions (ANTIC_OUT, TMP_GEN (block));
2234 : :
2235 : : /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2236 : 28968346 : ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
2237 : 14484173 : TMP_GEN (block));
2238 : :
2239 : : /* Then union in the ANTIC_OUT - TMP_GEN values,
2240 : : to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2241 : 14484173 : bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
2242 : 14484173 : bitmap_ior_into (&ANTIC_IN (block)->expressions, &S->expressions);
2243 : :
2244 : : /* clean (ANTIC_IN (block)) is defered to after the iteration converged
2245 : : because it can cause non-convergence, see for example PR81181. */
2246 : :
2247 : : /* Intersect ANTIC_IN with the old ANTIC_IN. This is required until
2248 : : we properly represent the maximum expression set, thus not prune
2249 : : values without expressions during the iteration. */
2250 : 14484173 : if (was_visited
2251 : 14484173 : && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
2252 : : {
2253 : 3426 : if (dump_file && (dump_flags & TDF_DETAILS))
2254 : 0 : fprintf (dump_file, "warning: intersecting with old ANTIC_IN "
2255 : : "shrinks the set\n");
2256 : : /* Prune expressions not in the value set. */
2257 : 3426 : bitmap_iterator bi;
2258 : 3426 : unsigned int i;
2259 : 3426 : unsigned int to_clear = -1U;
2260 : 34693 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
2261 : : {
2262 : 31267 : if (to_clear != -1U)
2263 : : {
2264 : 1786 : bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2265 : 1786 : to_clear = -1U;
2266 : : }
2267 : 31267 : pre_expr expr = expression_for_id (i);
2268 : 31267 : unsigned int value_id = get_expr_value_id (expr);
2269 : 31267 : if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
2270 : 4208 : to_clear = i;
2271 : : }
2272 : 3426 : if (to_clear != -1U)
2273 : 2422 : bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2274 : : }
2275 : :
2276 : 14484173 : if (!bitmap_set_equal (old, ANTIC_IN (block)))
2277 : 9258780 : changed = true;
2278 : :
2279 : 5225393 : maybe_dump_sets:
2280 : 14488350 : if (dump_file && (dump_flags & TDF_DETAILS))
2281 : : {
2282 : 176 : if (changed)
2283 : 158 : fprintf (dump_file, "[changed] ");
2284 : 176 : print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2285 : : block->index);
2286 : :
2287 : 176 : if (S)
2288 : 176 : print_bitmap_set (dump_file, S, "S", block->index);
2289 : : }
2290 : 14488350 : if (old)
2291 : 14484173 : bitmap_set_free (old);
2292 : 14488350 : if (S)
2293 : 14484173 : bitmap_set_free (S);
2294 : 14488350 : if (ANTIC_OUT)
2295 : 14484173 : bitmap_set_free (ANTIC_OUT);
2296 : 14488350 : return changed;
2297 : : }
2298 : :
2299 : : /* Compute PARTIAL_ANTIC for BLOCK.
2300 : :
2301 : : If succs(BLOCK) > 1 then
2302 : : PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2303 : : in ANTIC_OUT for all succ(BLOCK)
2304 : : else if succs(BLOCK) == 1 then
2305 : : PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2306 : :
2307 : : PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
2308 : :
2309 : : */
2310 : : static void
2311 : 1085878 : compute_partial_antic_aux (basic_block block,
2312 : : bool block_has_abnormal_pred_edge)
2313 : : {
2314 : 1085878 : bitmap_set_t old_PA_IN;
2315 : 1085878 : bitmap_set_t PA_OUT;
2316 : 1085878 : edge e;
2317 : 1085878 : edge_iterator ei;
2318 : 1085878 : unsigned long max_pa = param_max_partial_antic_length;
2319 : :
2320 : 1085878 : old_PA_IN = PA_OUT = NULL;
2321 : :
2322 : : /* If any edges from predecessors are abnormal, antic_in is empty,
2323 : : so do nothing. */
2324 : 1085878 : if (block_has_abnormal_pred_edge)
2325 : 739 : goto maybe_dump_sets;
2326 : :
2327 : : /* If there are too many partially anticipatable values in the
2328 : : block, phi_translate_set can take an exponential time: stop
2329 : : before the translation starts. */
2330 : 1085139 : if (max_pa
2331 : 1002962 : && single_succ_p (block)
2332 : 1764263 : && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2333 : 487 : goto maybe_dump_sets;
2334 : :
2335 : 1084652 : old_PA_IN = PA_IN (block);
2336 : 1084652 : PA_OUT = bitmap_set_new ();
2337 : :
2338 : : /* If the block has no successors, ANTIC_OUT is empty. */
2339 : 1084652 : if (EDGE_COUNT (block->succs) == 0)
2340 : : ;
2341 : : /* If we have one successor, we could have some phi nodes to
2342 : : translate through. Note that we can't phi translate across DFS
2343 : : back edges in partial antic, because it uses a union operation on
2344 : : the successors. For recurrences like IV's, we will end up
2345 : : generating a new value in the set on each go around (i + 3 (VH.1)
2346 : : VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2347 : 1002477 : else if (single_succ_p (block))
2348 : : {
2349 : 678639 : e = single_succ_edge (block);
2350 : 678639 : if (!(e->flags & EDGE_DFS_BACK))
2351 : 608488 : phi_translate_set (PA_OUT, PA_IN (e->dest), e);
2352 : : }
2353 : : /* If we have multiple successors, we take the union of all of
2354 : : them. */
2355 : : else
2356 : : {
2357 : 323838 : size_t i;
2358 : :
2359 : 323838 : auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2360 : 976577 : FOR_EACH_EDGE (e, ei, block->succs)
2361 : : {
2362 : 652739 : if (e->flags & EDGE_DFS_BACK)
2363 : 290 : continue;
2364 : 652449 : worklist.quick_push (e);
2365 : : }
2366 : 647676 : if (worklist.length () > 0)
2367 : : {
2368 : 976287 : FOR_EACH_VEC_ELT (worklist, i, e)
2369 : : {
2370 : 652449 : unsigned int i;
2371 : 652449 : bitmap_iterator bi;
2372 : :
2373 : 652449 : if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2374 : : {
2375 : 692 : bitmap_set_t antic_in = bitmap_set_new ();
2376 : 692 : phi_translate_set (antic_in, ANTIC_IN (e->dest), e);
2377 : 1457 : FOR_EACH_EXPR_ID_IN_SET (antic_in, i, bi)
2378 : 765 : bitmap_value_insert_into_set (PA_OUT,
2379 : : expression_for_id (i));
2380 : 692 : bitmap_set_free (antic_in);
2381 : 692 : bitmap_set_t pa_in = bitmap_set_new ();
2382 : 692 : phi_translate_set (pa_in, PA_IN (e->dest), e);
2383 : 692 : FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2384 : 0 : bitmap_value_insert_into_set (PA_OUT,
2385 : : expression_for_id (i));
2386 : 692 : bitmap_set_free (pa_in);
2387 : : }
2388 : : else
2389 : : {
2390 : 4379335 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
2391 : 3727578 : bitmap_value_insert_into_set (PA_OUT,
2392 : : expression_for_id (i));
2393 : 6566359 : FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
2394 : 5914602 : bitmap_value_insert_into_set (PA_OUT,
2395 : : expression_for_id (i));
2396 : : }
2397 : : }
2398 : : }
2399 : 323838 : }
2400 : :
2401 : : /* Prune expressions that are clobbered in block and thus become
2402 : : invalid if translated from PA_OUT to PA_IN. */
2403 : 1084652 : prune_clobbered_mems (PA_OUT, block, false);
2404 : :
2405 : : /* PA_IN starts with PA_OUT - TMP_GEN.
2406 : : Then we subtract things from ANTIC_IN. */
2407 : 1084652 : PA_IN (block) = bitmap_set_subtract_expressions (PA_OUT, TMP_GEN (block));
2408 : :
2409 : : /* For partial antic, we want to put back in the phi results, since
2410 : : we will properly avoid making them partially antic over backedges. */
2411 : 1084652 : bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2412 : 1084652 : bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2413 : :
2414 : : /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2415 : 1084652 : bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2416 : :
2417 : 1084652 : clean (PA_IN (block), ANTIC_IN (block));
2418 : :
2419 : 1085878 : maybe_dump_sets:
2420 : 1085878 : if (dump_file && (dump_flags & TDF_DETAILS))
2421 : : {
2422 : 0 : if (PA_OUT)
2423 : 0 : print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2424 : :
2425 : 0 : print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2426 : : }
2427 : 1085878 : if (old_PA_IN)
2428 : 1084652 : bitmap_set_free (old_PA_IN);
2429 : 1085878 : if (PA_OUT)
2430 : 1084652 : bitmap_set_free (PA_OUT);
2431 : 1085878 : }
2432 : :
2433 : : /* Compute ANTIC and partial ANTIC sets. */
2434 : :
2435 : : static void
2436 : 928096 : compute_antic (void)
2437 : : {
2438 : 928096 : bool changed = true;
2439 : 928096 : int num_iterations = 0;
2440 : 928096 : basic_block block;
2441 : 928096 : int i;
2442 : 928096 : edge_iterator ei;
2443 : 928096 : edge e;
2444 : :
2445 : : /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2446 : : We pre-build the map of blocks with incoming abnormal edges here. */
2447 : 928096 : auto_sbitmap has_abnormal_preds (last_basic_block_for_fn (cfun));
2448 : 928096 : bitmap_clear (has_abnormal_preds);
2449 : :
2450 : 15165768 : FOR_ALL_BB_FN (block, cfun)
2451 : : {
2452 : 14237672 : BB_VISITED (block) = 0;
2453 : :
2454 : 32075987 : FOR_EACH_EDGE (e, ei, block->preds)
2455 : 17841553 : if (e->flags & EDGE_ABNORMAL)
2456 : : {
2457 : 3238 : bitmap_set_bit (has_abnormal_preds, block->index);
2458 : 3238 : break;
2459 : : }
2460 : :
2461 : : /* While we are here, give empty ANTIC_IN sets to each block. */
2462 : 14237672 : ANTIC_IN (block) = bitmap_set_new ();
2463 : 14237672 : if (do_partial_partial)
2464 : 1085878 : PA_IN (block) = bitmap_set_new ();
2465 : : }
2466 : :
2467 : : /* At the exit block we anticipate nothing. */
2468 : 928096 : BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2469 : :
2470 : : /* For ANTIC computation we need a postorder that also guarantees that
2471 : : a block with a single successor is visited after its successor.
2472 : : RPO on the inverted CFG has this property. */
2473 : 928096 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2474 : 928096 : int n = inverted_rev_post_order_compute (cfun, rpo);
2475 : :
2476 : 928096 : auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
2477 : 928096 : bitmap_clear (worklist);
2478 : 2634274 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2479 : 1706178 : bitmap_set_bit (worklist, e->src->index);
2480 : 2852866 : while (changed)
2481 : : {
2482 : 1924770 : if (dump_file && (dump_flags & TDF_DETAILS))
2483 : 31 : fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2484 : : /* ??? We need to clear our PHI translation cache here as the
2485 : : ANTIC sets shrink and we restrict valid translations to
2486 : : those having operands with leaders in ANTIC. Same below
2487 : : for PA ANTIC computation. */
2488 : 1924770 : num_iterations++;
2489 : 1924770 : changed = false;
2490 : 35280900 : for (i = 0; i < n; ++i)
2491 : : {
2492 : 33356130 : if (bitmap_bit_p (worklist, rpo[i]))
2493 : : {
2494 : 14488350 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2495 : 14488350 : bitmap_clear_bit (worklist, block->index);
2496 : 14488350 : if (compute_antic_aux (block,
2497 : 14488350 : bitmap_bit_p (has_abnormal_preds,
2498 : : block->index)))
2499 : : {
2500 : 30604501 : FOR_EACH_EDGE (e, ei, block->preds)
2501 : 16812898 : bitmap_set_bit (worklist, e->src->index);
2502 : : changed = true;
2503 : : }
2504 : : }
2505 : : }
2506 : : /* Theoretically possible, but *highly* unlikely. */
2507 : 1924770 : gcc_checking_assert (num_iterations < 500);
2508 : : }
2509 : :
2510 : : /* We have to clean after the dataflow problem converged as cleaning
2511 : : can cause non-convergence because it is based on expressions
2512 : : rather than values. */
2513 : 13309576 : FOR_EACH_BB_FN (block, cfun)
2514 : 12381480 : clean (ANTIC_IN (block));
2515 : :
2516 : 928096 : statistics_histogram_event (cfun, "compute_antic iterations",
2517 : : num_iterations);
2518 : :
2519 : 928096 : if (do_partial_partial)
2520 : : {
2521 : : /* For partial antic we ignore backedges and thus we do not need
2522 : : to perform any iteration when we process blocks in rpo. */
2523 : 1168053 : for (i = 0; i < n; ++i)
2524 : : {
2525 : 1085878 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2526 : 1085878 : compute_partial_antic_aux (block,
2527 : 1085878 : bitmap_bit_p (has_abnormal_preds,
2528 : : block->index));
2529 : : }
2530 : : }
2531 : :
2532 : 928096 : free (rpo);
2533 : 928096 : }
2534 : :
2535 : :
2536 : : /* Inserted expressions are placed onto this worklist, which is used
2537 : : for performing quick dead code elimination of insertions we made
2538 : : that didn't turn out to be necessary. */
2539 : : static bitmap inserted_exprs;
2540 : :
2541 : : /* The actual worker for create_component_ref_by_pieces. */
2542 : :
2543 : : static tree
2544 : 1030271 : create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2545 : : unsigned int *operand, gimple_seq *stmts)
2546 : : {
2547 : 1030271 : vn_reference_op_t currop = &ref->operands[*operand];
2548 : 1030271 : tree genop;
2549 : 1030271 : ++*operand;
2550 : 1030271 : switch (currop->opcode)
2551 : : {
2552 : 0 : case CALL_EXPR:
2553 : 0 : gcc_unreachable ();
2554 : :
2555 : 360315 : case MEM_REF:
2556 : 360315 : {
2557 : 360315 : tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2558 : : stmts);
2559 : 360315 : if (!baseop)
2560 : : return NULL_TREE;
2561 : 360315 : tree offset = currop->op0;
2562 : 360315 : if (TREE_CODE (baseop) == ADDR_EXPR
2563 : 360315 : && handled_component_p (TREE_OPERAND (baseop, 0)))
2564 : : {
2565 : 0 : poly_int64 off;
2566 : 0 : tree base;
2567 : 0 : base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2568 : : &off);
2569 : 0 : gcc_assert (base);
2570 : 0 : offset = int_const_binop (PLUS_EXPR, offset,
2571 : 0 : build_int_cst (TREE_TYPE (offset),
2572 : : off));
2573 : 0 : baseop = build_fold_addr_expr (base);
2574 : : }
2575 : 360315 : genop = build2 (MEM_REF, currop->type, baseop, offset);
2576 : 360315 : MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2577 : 360315 : MR_DEPENDENCE_BASE (genop) = currop->base;
2578 : 360315 : REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
2579 : 360315 : return genop;
2580 : : }
2581 : :
2582 : 0 : case TARGET_MEM_REF:
2583 : 0 : {
2584 : 0 : tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2585 : 0 : vn_reference_op_t nextop = &ref->operands[(*operand)++];
2586 : 0 : tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2587 : : stmts);
2588 : 0 : if (!baseop)
2589 : : return NULL_TREE;
2590 : 0 : if (currop->op0)
2591 : : {
2592 : 0 : genop0 = find_or_generate_expression (block, currop->op0, stmts);
2593 : 0 : if (!genop0)
2594 : : return NULL_TREE;
2595 : : }
2596 : 0 : if (nextop->op0)
2597 : : {
2598 : 0 : genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2599 : 0 : if (!genop1)
2600 : : return NULL_TREE;
2601 : : }
2602 : 0 : genop = build5 (TARGET_MEM_REF, currop->type,
2603 : : baseop, currop->op2, genop0, currop->op1, genop1);
2604 : :
2605 : 0 : MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2606 : 0 : MR_DEPENDENCE_BASE (genop) = currop->base;
2607 : 0 : return genop;
2608 : : }
2609 : :
2610 : 239314 : case ADDR_EXPR:
2611 : 239314 : if (currop->op0)
2612 : : {
2613 : 239314 : gcc_assert (is_gimple_min_invariant (currop->op0));
2614 : 239314 : return currop->op0;
2615 : : }
2616 : : /* Fallthrough. */
2617 : 4027 : case REALPART_EXPR:
2618 : 4027 : case IMAGPART_EXPR:
2619 : 4027 : case VIEW_CONVERT_EXPR:
2620 : 4027 : {
2621 : 4027 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2622 : : stmts);
2623 : 4027 : if (!genop0)
2624 : : return NULL_TREE;
2625 : 4027 : return build1 (currop->opcode, currop->type, genop0);
2626 : : }
2627 : :
2628 : 4 : case WITH_SIZE_EXPR:
2629 : 4 : {
2630 : 4 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2631 : : stmts);
2632 : 4 : if (!genop0)
2633 : : return NULL_TREE;
2634 : 4 : tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2635 : 4 : if (!genop1)
2636 : : return NULL_TREE;
2637 : 4 : return build2 (currop->opcode, currop->type, genop0, genop1);
2638 : : }
2639 : :
2640 : 2509 : case BIT_FIELD_REF:
2641 : 2509 : {
2642 : 2509 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2643 : : stmts);
2644 : 2509 : if (!genop0)
2645 : : return NULL_TREE;
2646 : 2509 : tree op1 = currop->op0;
2647 : 2509 : tree op2 = currop->op1;
2648 : 2509 : tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2649 : 2509 : REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
2650 : 2509 : return t;
2651 : : }
2652 : :
2653 : : /* For array ref vn_reference_op's, operand 1 of the array ref
2654 : : is op0 of the reference op and operand 3 of the array ref is
2655 : : op1. */
2656 : 57585 : case ARRAY_RANGE_REF:
2657 : 57585 : case ARRAY_REF:
2658 : 57585 : {
2659 : 57585 : tree genop0;
2660 : 57585 : tree genop1 = currop->op0;
2661 : 57585 : tree genop2 = currop->op1;
2662 : 57585 : tree genop3 = currop->op2;
2663 : 57585 : genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2664 : : stmts);
2665 : 57585 : if (!genop0)
2666 : : return NULL_TREE;
2667 : 57585 : genop1 = find_or_generate_expression (block, genop1, stmts);
2668 : 57585 : if (!genop1)
2669 : : return NULL_TREE;
2670 : 57585 : if (genop2)
2671 : : {
2672 : 57585 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2673 : : /* Drop zero minimum index if redundant. */
2674 : 57585 : if (integer_zerop (genop2)
2675 : 57585 : && (!domain_type
2676 : 56560 : || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2677 : : genop2 = NULL_TREE;
2678 : : else
2679 : : {
2680 : 598 : genop2 = find_or_generate_expression (block, genop2, stmts);
2681 : 598 : if (!genop2)
2682 : : return NULL_TREE;
2683 : : }
2684 : : }
2685 : 57585 : if (genop3)
2686 : : {
2687 : 57585 : tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2688 : : /* We can't always put a size in units of the element alignment
2689 : : here as the element alignment may be not visible. See
2690 : : PR43783. Simply drop the element size for constant
2691 : : sizes. */
2692 : 57585 : if ((TREE_CODE (genop3) == INTEGER_CST
2693 : 57581 : && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
2694 : 57581 : && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
2695 : 57581 : (wi::to_offset (genop3) * vn_ref_op_align_unit (currop))))
2696 : 57585 : || (TREE_CODE (genop3) == EXACT_DIV_EXPR
2697 : 0 : && TREE_CODE (TREE_OPERAND (genop3, 1)) == INTEGER_CST
2698 : 0 : && operand_equal_p (TREE_OPERAND (genop3, 0), TYPE_SIZE_UNIT (elmt_type))
2699 : 0 : && wi::eq_p (wi::to_offset (TREE_OPERAND (genop3, 1)),
2700 : 57581 : vn_ref_op_align_unit (currop))))
2701 : : genop3 = NULL_TREE;
2702 : : else
2703 : : {
2704 : 4 : genop3 = find_or_generate_expression (block, genop3, stmts);
2705 : 4 : if (!genop3)
2706 : : return NULL_TREE;
2707 : : }
2708 : : }
2709 : 57585 : return build4 (currop->opcode, currop->type, genop0, genop1,
2710 : 57585 : genop2, genop3);
2711 : : }
2712 : 241954 : case COMPONENT_REF:
2713 : 241954 : {
2714 : 241954 : tree op0;
2715 : 241954 : tree op1;
2716 : 241954 : tree genop2 = currop->op1;
2717 : 241954 : op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2718 : 241954 : if (!op0)
2719 : : return NULL_TREE;
2720 : : /* op1 should be a FIELD_DECL, which are represented by themselves. */
2721 : 241954 : op1 = currop->op0;
2722 : 241954 : if (genop2)
2723 : : {
2724 : 0 : genop2 = find_or_generate_expression (block, genop2, stmts);
2725 : 0 : if (!genop2)
2726 : : return NULL_TREE;
2727 : : }
2728 : 241954 : return build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2729 : : }
2730 : :
2731 : 122886 : case SSA_NAME:
2732 : 122886 : {
2733 : 122886 : genop = find_or_generate_expression (block, currop->op0, stmts);
2734 : 122886 : return genop;
2735 : : }
2736 : 1677 : case STRING_CST:
2737 : 1677 : case INTEGER_CST:
2738 : 1677 : case POLY_INT_CST:
2739 : 1677 : case COMPLEX_CST:
2740 : 1677 : case VECTOR_CST:
2741 : 1677 : case REAL_CST:
2742 : 1677 : case CONSTRUCTOR:
2743 : 1677 : case VAR_DECL:
2744 : 1677 : case PARM_DECL:
2745 : 1677 : case CONST_DECL:
2746 : 1677 : case RESULT_DECL:
2747 : 1677 : case FUNCTION_DECL:
2748 : 1677 : return currop->op0;
2749 : :
2750 : 0 : default:
2751 : 0 : gcc_unreachable ();
2752 : : }
2753 : : }
2754 : :
2755 : : /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2756 : : COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2757 : : trying to rename aggregates into ssa form directly, which is a no no.
2758 : :
2759 : : Thus, this routine doesn't create temporaries, it just builds a
2760 : : single access expression for the array, calling
2761 : : find_or_generate_expression to build the innermost pieces.
2762 : :
2763 : : This function is a subroutine of create_expression_by_pieces, and
2764 : : should not be called on it's own unless you really know what you
2765 : : are doing. */
2766 : :
2767 : : static tree
2768 : 360342 : create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2769 : : gimple_seq *stmts)
2770 : : {
2771 : 360342 : unsigned int op = 0;
2772 : 360342 : return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2773 : : }
2774 : :
2775 : : /* Find a simple leader for an expression, or generate one using
2776 : : create_expression_by_pieces from a NARY expression for the value.
2777 : : BLOCK is the basic_block we are looking for leaders in.
2778 : : OP is the tree expression to find a leader for or generate.
2779 : : Returns the leader or NULL_TREE on failure. */
2780 : :
2781 : : static tree
2782 : 768969 : find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2783 : : {
2784 : : /* Constants are always leaders. */
2785 : 768969 : if (is_gimple_min_invariant (op))
2786 : : return op;
2787 : :
2788 : 592361 : gcc_assert (TREE_CODE (op) == SSA_NAME);
2789 : 592361 : vn_ssa_aux_t info = VN_INFO (op);
2790 : 592361 : unsigned int lookfor = info->value_id;
2791 : 592361 : if (value_id_constant_p (lookfor))
2792 : 10 : return info->valnum;
2793 : :
2794 : 592351 : pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2795 : 592351 : if (leader)
2796 : : {
2797 : 562548 : if (leader->kind == NAME)
2798 : 562548 : return PRE_EXPR_NAME (leader);
2799 : 0 : else if (leader->kind == CONSTANT)
2800 : 0 : return PRE_EXPR_CONSTANT (leader);
2801 : :
2802 : : /* Defer. */
2803 : : return NULL_TREE;
2804 : : }
2805 : 29803 : gcc_assert (!value_id_constant_p (lookfor));
2806 : :
2807 : : /* It must be a complex expression, so generate it recursively. Note
2808 : : that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2809 : : where the insert algorithm fails to insert a required expression. */
2810 : 29803 : bitmap exprset = value_expressions[lookfor];
2811 : 29803 : bitmap_iterator bi;
2812 : 29803 : unsigned int i;
2813 : 45675 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2814 : : {
2815 : 42369 : pre_expr temp = expression_for_id (i);
2816 : : /* We cannot insert random REFERENCE expressions at arbitrary
2817 : : places. We can insert NARYs which eventually re-materializes
2818 : : its operand values. */
2819 : 42369 : if (temp->kind == NARY)
2820 : 26497 : return create_expression_by_pieces (block, temp, stmts,
2821 : 52994 : TREE_TYPE (op));
2822 : : }
2823 : :
2824 : : /* Defer. */
2825 : : return NULL_TREE;
2826 : : }
2827 : :
2828 : : /* Create an expression in pieces, so that we can handle very complex
2829 : : expressions that may be ANTIC, but not necessary GIMPLE.
2830 : : BLOCK is the basic block the expression will be inserted into,
2831 : : EXPR is the expression to insert (in value form)
2832 : : STMTS is a statement list to append the necessary insertions into.
2833 : :
2834 : : This function will die if we hit some value that shouldn't be
2835 : : ANTIC but is (IE there is no leader for it, or its components).
2836 : : The function returns NULL_TREE in case a different antic expression
2837 : : has to be inserted first.
2838 : : This function may also generate expressions that are themselves
2839 : : partially or fully redundant. Those that are will be either made
2840 : : fully redundant during the next iteration of insert (for partially
2841 : : redundant ones), or eliminated by eliminate (for fully redundant
2842 : : ones). */
2843 : :
2844 : : static tree
2845 : 2625523 : create_expression_by_pieces (basic_block block, pre_expr expr,
2846 : : gimple_seq *stmts, tree type)
2847 : : {
2848 : 2625523 : tree name;
2849 : 2625523 : tree folded;
2850 : 2625523 : gimple_seq forced_stmts = NULL;
2851 : 2625523 : unsigned int value_id;
2852 : 2625523 : gimple_stmt_iterator gsi;
2853 : 2625523 : tree exprtype = type ? type : get_expr_type (expr);
2854 : 2625523 : pre_expr nameexpr;
2855 : 2625523 : gassign *newstmt;
2856 : :
2857 : 2625523 : switch (expr->kind)
2858 : : {
2859 : : /* We may hit the NAME/CONSTANT case if we have to convert types
2860 : : that value numbering saw through. */
2861 : 645323 : case NAME:
2862 : 645323 : folded = PRE_EXPR_NAME (expr);
2863 : 645323 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
2864 : : return NULL_TREE;
2865 : 645309 : if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
2866 : : return folded;
2867 : : break;
2868 : 1262399 : case CONSTANT:
2869 : 1262399 : {
2870 : 1262399 : folded = PRE_EXPR_CONSTANT (expr);
2871 : 1262399 : tree tem = fold_convert (exprtype, folded);
2872 : 1262399 : if (is_gimple_min_invariant (tem))
2873 : : return tem;
2874 : : break;
2875 : : }
2876 : 362586 : case REFERENCE:
2877 : 362586 : if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
2878 : : {
2879 : 2244 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2880 : 2244 : unsigned int operand = 1;
2881 : 2244 : vn_reference_op_t currop = &ref->operands[0];
2882 : 2244 : tree sc = NULL_TREE;
2883 : 2244 : tree fn = NULL_TREE;
2884 : 2244 : if (currop->op0)
2885 : : {
2886 : 2177 : fn = find_or_generate_expression (block, currop->op0, stmts);
2887 : 2177 : if (!fn)
2888 : 0 : return NULL_TREE;
2889 : : }
2890 : 2244 : if (currop->op1)
2891 : : {
2892 : 0 : sc = find_or_generate_expression (block, currop->op1, stmts);
2893 : 0 : if (!sc)
2894 : : return NULL_TREE;
2895 : : }
2896 : 4488 : auto_vec<tree> args (ref->operands.length () - 1);
2897 : 5779 : while (operand < ref->operands.length ())
2898 : : {
2899 : 3535 : tree arg = create_component_ref_by_pieces_1 (block, ref,
2900 : 3535 : &operand, stmts);
2901 : 3535 : if (!arg)
2902 : 0 : return NULL_TREE;
2903 : 3535 : args.quick_push (arg);
2904 : : }
2905 : 2244 : gcall *call;
2906 : 2244 : if (currop->op0)
2907 : : {
2908 : 2177 : call = gimple_build_call_vec (fn, args);
2909 : 2177 : gimple_call_set_fntype (call, currop->type);
2910 : : }
2911 : : else
2912 : 67 : call = gimple_build_call_internal_vec ((internal_fn)currop->clique,
2913 : : args);
2914 : 2244 : gimple_set_location (call, expr->loc);
2915 : 2244 : if (sc)
2916 : 0 : gimple_call_set_chain (call, sc);
2917 : 2244 : tree forcedname = make_ssa_name (ref->type);
2918 : 2244 : gimple_call_set_lhs (call, forcedname);
2919 : : /* There's no CCP pass after PRE which would re-compute alignment
2920 : : information so make sure we re-materialize this here. */
2921 : 2244 : if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED)
2922 : 0 : && args.length () - 2 <= 1
2923 : 0 : && tree_fits_uhwi_p (args[1])
2924 : 2244 : && (args.length () != 3 || tree_fits_uhwi_p (args[2])))
2925 : : {
2926 : 0 : unsigned HOST_WIDE_INT halign = tree_to_uhwi (args[1]);
2927 : 0 : unsigned HOST_WIDE_INT hmisalign
2928 : 0 : = args.length () == 3 ? tree_to_uhwi (args[2]) : 0;
2929 : 0 : if ((halign & (halign - 1)) == 0
2930 : 0 : && (hmisalign & ~(halign - 1)) == 0
2931 : 0 : && (unsigned int)halign != 0)
2932 : 0 : set_ptr_info_alignment (get_ptr_info (forcedname),
2933 : : halign, hmisalign);
2934 : : }
2935 : 2244 : gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
2936 : 2244 : gimple_seq_add_stmt_without_update (&forced_stmts, call);
2937 : 2244 : folded = forcedname;
2938 : 2244 : }
2939 : : else
2940 : : {
2941 : 360342 : folded = create_component_ref_by_pieces (block,
2942 : : PRE_EXPR_REFERENCE (expr),
2943 : : stmts);
2944 : 360342 : if (!folded)
2945 : : return NULL_TREE;
2946 : 360342 : name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2947 : 360342 : newstmt = gimple_build_assign (name, folded);
2948 : 360342 : gimple_set_location (newstmt, expr->loc);
2949 : 360342 : gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2950 : 360342 : gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
2951 : 360342 : folded = name;
2952 : : }
2953 : : break;
2954 : 355215 : case NARY:
2955 : 355215 : {
2956 : 355215 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2957 : 355215 : tree *genop = XALLOCAVEC (tree, nary->length);
2958 : 355215 : unsigned i;
2959 : 931281 : for (i = 0; i < nary->length; ++i)
2960 : : {
2961 : 585715 : genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
2962 : 585715 : if (!genop[i])
2963 : : return NULL_TREE;
2964 : : /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
2965 : : may have conversions stripped. */
2966 : 576066 : if (nary->opcode == POINTER_PLUS_EXPR)
2967 : : {
2968 : 74761 : if (i == 0)
2969 : 37426 : genop[i] = gimple_convert (&forced_stmts,
2970 : : nary->type, genop[i]);
2971 : 37335 : else if (i == 1)
2972 : 37335 : genop[i] = gimple_convert (&forced_stmts,
2973 : : sizetype, genop[i]);
2974 : : }
2975 : : else
2976 : 501305 : genop[i] = gimple_convert (&forced_stmts,
2977 : 501305 : TREE_TYPE (nary->op[i]), genop[i]);
2978 : : }
2979 : 345566 : if (nary->opcode == CONSTRUCTOR)
2980 : : {
2981 : 8 : vec<constructor_elt, va_gc> *elts = NULL;
2982 : 24 : for (i = 0; i < nary->length; ++i)
2983 : 16 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
2984 : 8 : folded = build_constructor (nary->type, elts);
2985 : 8 : name = make_temp_ssa_name (exprtype, NULL, "pretmp");
2986 : 8 : newstmt = gimple_build_assign (name, folded);
2987 : 8 : gimple_set_location (newstmt, expr->loc);
2988 : 8 : gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
2989 : 8 : folded = name;
2990 : : }
2991 : : else
2992 : : {
2993 : 345558 : switch (nary->length)
2994 : : {
2995 : 116384 : case 1:
2996 : 116384 : folded = gimple_build (&forced_stmts, expr->loc,
2997 : : nary->opcode, nary->type, genop[0]);
2998 : 116384 : break;
2999 : 229029 : case 2:
3000 : 229029 : folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
3001 : : nary->type, genop[0], genop[1]);
3002 : 229029 : break;
3003 : 145 : case 3:
3004 : 145 : folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
3005 : : nary->type, genop[0], genop[1],
3006 : : genop[2]);
3007 : 145 : break;
3008 : 0 : default:
3009 : 0 : gcc_unreachable ();
3010 : : }
3011 : : }
3012 : : }
3013 : : break;
3014 : 0 : default:
3015 : 0 : gcc_unreachable ();
3016 : : }
3017 : :
3018 : 790789 : folded = gimple_convert (&forced_stmts, exprtype, folded);
3019 : :
3020 : : /* If there is nothing to insert, return the simplified result. */
3021 : 790789 : if (gimple_seq_empty_p (forced_stmts))
3022 : : return folded;
3023 : : /* If we simplified to a constant return it and discard eventually
3024 : : built stmts. */
3025 : 708056 : if (is_gimple_min_invariant (folded))
3026 : : {
3027 : 0 : gimple_seq_discard (forced_stmts);
3028 : 0 : return folded;
3029 : : }
3030 : : /* Likewise if we simplified to sth not queued for insertion. */
3031 : 708056 : bool found = false;
3032 : 708056 : gsi = gsi_last (forced_stmts);
3033 : 708056 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
3034 : : {
3035 : 708056 : gimple *stmt = gsi_stmt (gsi);
3036 : 708056 : tree forcedname = gimple_get_lhs (stmt);
3037 : 708056 : if (forcedname == folded)
3038 : : {
3039 : : found = true;
3040 : : break;
3041 : : }
3042 : : }
3043 : 708056 : if (! found)
3044 : : {
3045 : 0 : gimple_seq_discard (forced_stmts);
3046 : 0 : return folded;
3047 : : }
3048 : 708056 : gcc_assert (TREE_CODE (folded) == SSA_NAME);
3049 : :
3050 : : /* If we have any intermediate expressions to the value sets, add them
3051 : : to the value sets and chain them in the instruction stream. */
3052 : 708056 : if (forced_stmts)
3053 : : {
3054 : 708056 : gsi = gsi_start (forced_stmts);
3055 : 1416555 : for (; !gsi_end_p (gsi); gsi_next (&gsi))
3056 : : {
3057 : 708499 : gimple *stmt = gsi_stmt (gsi);
3058 : 708499 : tree forcedname = gimple_get_lhs (stmt);
3059 : 708499 : pre_expr nameexpr;
3060 : :
3061 : 708499 : if (forcedname != folded)
3062 : : {
3063 : 443 : vn_ssa_aux_t vn_info = VN_INFO (forcedname);
3064 : 443 : vn_info->valnum = forcedname;
3065 : 443 : vn_info->value_id = get_next_value_id ();
3066 : 443 : nameexpr = get_or_alloc_expr_for_name (forcedname);
3067 : 443 : add_to_value (vn_info->value_id, nameexpr);
3068 : 443 : if (NEW_SETS (block))
3069 : 443 : bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3070 : 443 : bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3071 : : }
3072 : :
3073 : 708499 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
3074 : : }
3075 : 708056 : gimple_seq_add_seq (stmts, forced_stmts);
3076 : : }
3077 : :
3078 : 708056 : name = folded;
3079 : :
3080 : : /* Fold the last statement. */
3081 : 708056 : gsi = gsi_last (*stmts);
3082 : 708056 : if (fold_stmt_inplace (&gsi))
3083 : 205031 : update_stmt (gsi_stmt (gsi));
3084 : :
3085 : : /* Add a value number to the temporary.
3086 : : The value may already exist in either NEW_SETS, or AVAIL_OUT, because
3087 : : we are creating the expression by pieces, and this particular piece of
3088 : : the expression may have been represented. There is no harm in replacing
3089 : : here. */
3090 : 708056 : value_id = get_expr_value_id (expr);
3091 : 708056 : vn_ssa_aux_t vn_info = VN_INFO (name);
3092 : 708056 : vn_info->value_id = value_id;
3093 : 708056 : vn_info->valnum = vn_valnum_from_value_id (value_id);
3094 : 708056 : if (vn_info->valnum == NULL_TREE)
3095 : 222699 : vn_info->valnum = name;
3096 : 708056 : gcc_assert (vn_info->valnum != NULL_TREE);
3097 : 708056 : nameexpr = get_or_alloc_expr_for_name (name);
3098 : 708056 : add_to_value (value_id, nameexpr);
3099 : 708056 : if (NEW_SETS (block))
3100 : 507290 : bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3101 : 708056 : bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3102 : :
3103 : 708056 : pre_stats.insertions++;
3104 : 708056 : if (dump_file && (dump_flags & TDF_DETAILS))
3105 : : {
3106 : 22 : fprintf (dump_file, "Inserted ");
3107 : 44 : print_gimple_stmt (dump_file, gsi_stmt (gsi_last (*stmts)), 0);
3108 : 22 : fprintf (dump_file, " in predecessor %d (%04d)\n",
3109 : : block->index, value_id);
3110 : : }
3111 : :
3112 : : return name;
3113 : : }
3114 : :
3115 : :
3116 : : /* Insert the to-be-made-available values of expression EXPRNUM for each
3117 : : predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3118 : : merge the result with a phi node, given the same value number as
3119 : : NODE. Return true if we have inserted new stuff. */
3120 : :
3121 : : static bool
3122 : 1786397 : insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3123 : : vec<pre_expr> &avail)
3124 : : {
3125 : 1786397 : pre_expr expr = expression_for_id (exprnum);
3126 : 1786397 : pre_expr newphi;
3127 : 1786397 : unsigned int val = get_expr_value_id (expr);
3128 : 1786397 : edge pred;
3129 : 1786397 : bool insertions = false;
3130 : 1786397 : bool nophi = false;
3131 : 1786397 : basic_block bprime;
3132 : 1786397 : pre_expr eprime;
3133 : 1786397 : edge_iterator ei;
3134 : 1786397 : tree type = get_expr_type (expr);
3135 : 1786397 : tree temp;
3136 : 1786397 : gphi *phi;
3137 : :
3138 : : /* Make sure we aren't creating an induction variable. */
3139 : 1786397 : if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3140 : : {
3141 : 1480829 : bool firstinsideloop = false;
3142 : 1480829 : bool secondinsideloop = false;
3143 : 4442487 : firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3144 : 1480829 : EDGE_PRED (block, 0)->src);
3145 : 4442487 : secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3146 : 1480829 : EDGE_PRED (block, 1)->src);
3147 : : /* Induction variables only have one edge inside the loop. */
3148 : 1480829 : if ((firstinsideloop ^ secondinsideloop)
3149 : 1420341 : && expr->kind != REFERENCE)
3150 : : {
3151 : 1350185 : if (dump_file && (dump_flags & TDF_DETAILS))
3152 : 56 : fprintf (dump_file, "Skipping insertion of phi for partial "
3153 : : "redundancy: Looks like an induction variable\n");
3154 : : nophi = true;
3155 : : }
3156 : : }
3157 : :
3158 : : /* Make the necessary insertions. */
3159 : 5546345 : FOR_EACH_EDGE (pred, ei, block->preds)
3160 : : {
3161 : : /* When we are not inserting a PHI node do not bother inserting
3162 : : into places that do not dominate the anticipated computations. */
3163 : 3759948 : if (nophi && !dominated_by_p (CDI_DOMINATORS, block, pred->src))
3164 : 1365008 : continue;
3165 : 2398260 : gimple_seq stmts = NULL;
3166 : 2398260 : tree builtexpr;
3167 : 2398260 : bprime = pred->src;
3168 : 2398260 : eprime = avail[pred->dest_idx];
3169 : 2398260 : builtexpr = create_expression_by_pieces (bprime, eprime,
3170 : : &stmts, type);
3171 : 2398260 : gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3172 : 2398260 : if (!gimple_seq_empty_p (stmts))
3173 : : {
3174 : 487202 : basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
3175 : 487202 : gcc_assert (! new_bb);
3176 : : insertions = true;
3177 : : }
3178 : 2398260 : if (!builtexpr)
3179 : : {
3180 : : /* We cannot insert a PHI node if we failed to insert
3181 : : on one edge. */
3182 : 3320 : nophi = true;
3183 : 3320 : continue;
3184 : : }
3185 : 2394940 : if (is_gimple_min_invariant (builtexpr))
3186 : 1262429 : avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
3187 : : else
3188 : 1132511 : avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3189 : : }
3190 : : /* If we didn't want a phi node, and we made insertions, we still have
3191 : : inserted new stuff, and thus return true. If we didn't want a phi node,
3192 : : and didn't make insertions, we haven't added anything new, so return
3193 : : false. */
3194 : 1786397 : if (nophi && insertions)
3195 : : return true;
3196 : 1753636 : else if (nophi && !insertions)
3197 : : return false;
3198 : :
3199 : : /* Now build a phi for the new variable. */
3200 : 432897 : temp = make_temp_ssa_name (type, NULL, "prephitmp");
3201 : 432897 : phi = create_phi_node (temp, block);
3202 : :
3203 : 432897 : vn_ssa_aux_t vn_info = VN_INFO (temp);
3204 : 432897 : vn_info->value_id = val;
3205 : 432897 : vn_info->valnum = vn_valnum_from_value_id (val);
3206 : 432897 : if (vn_info->valnum == NULL_TREE)
3207 : 96382 : vn_info->valnum = temp;
3208 : 432897 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3209 : 1476878 : FOR_EACH_EDGE (pred, ei, block->preds)
3210 : : {
3211 : 1043981 : pre_expr ae = avail[pred->dest_idx];
3212 : 1043981 : gcc_assert (get_expr_type (ae) == type
3213 : : || useless_type_conversion_p (type, get_expr_type (ae)));
3214 : 1043981 : if (ae->kind == CONSTANT)
3215 : 166464 : add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3216 : : pred, UNKNOWN_LOCATION);
3217 : : else
3218 : 877517 : add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3219 : : }
3220 : :
3221 : 432897 : newphi = get_or_alloc_expr_for_name (temp);
3222 : 432897 : add_to_value (val, newphi);
3223 : :
3224 : : /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3225 : : this insertion, since we test for the existence of this value in PHI_GEN
3226 : : before proceeding with the partial redundancy checks in insert_aux.
3227 : :
3228 : : The value may exist in AVAIL_OUT, in particular, it could be represented
3229 : : by the expression we are trying to eliminate, in which case we want the
3230 : : replacement to occur. If it's not existing in AVAIL_OUT, we want it
3231 : : inserted there.
3232 : :
3233 : : Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3234 : : this block, because if it did, it would have existed in our dominator's
3235 : : AVAIL_OUT, and would have been skipped due to the full redundancy check.
3236 : : */
3237 : :
3238 : 432897 : bitmap_insert_into_set (PHI_GEN (block), newphi);
3239 : 432897 : bitmap_value_replace_in_set (AVAIL_OUT (block),
3240 : : newphi);
3241 : 432897 : if (NEW_SETS (block))
3242 : 432897 : bitmap_insert_into_set (NEW_SETS (block), newphi);
3243 : :
3244 : : /* If we insert a PHI node for a conversion of another PHI node
3245 : : in the same basic-block try to preserve range information.
3246 : : This is important so that followup loop passes receive optimal
3247 : : number of iteration analysis results. See PR61743. */
3248 : 432897 : if (expr->kind == NARY
3249 : 159824 : && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3250 : 57430 : && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3251 : 57285 : && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3252 : 46871 : && INTEGRAL_TYPE_P (type)
3253 : 45850 : && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3254 : 44873 : && (TYPE_PRECISION (type)
3255 : 44873 : >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3256 : 470464 : && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3257 : : {
3258 : 19444 : int_range_max r;
3259 : 38888 : if (get_range_query (cfun)->range_of_expr (r, expr->u.nary->op[0])
3260 : 19444 : && !r.undefined_p ()
3261 : 19444 : && !r.varying_p ()
3262 : 38888 : && !wi::neg_p (r.lower_bound (), SIGNED)
3263 : 53685 : && !wi::neg_p (r.upper_bound (), SIGNED))
3264 : : {
3265 : : /* Just handle extension and sign-changes of all-positive ranges. */
3266 : 14146 : range_cast (r, type);
3267 : 14146 : set_range_info (temp, r);
3268 : : }
3269 : 19444 : }
3270 : :
3271 : 432897 : if (dump_file && (dump_flags & TDF_DETAILS))
3272 : : {
3273 : 10 : fprintf (dump_file, "Created phi ");
3274 : 10 : print_gimple_stmt (dump_file, phi, 0);
3275 : 10 : fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3276 : : }
3277 : 432897 : pre_stats.phis++;
3278 : 432897 : return true;
3279 : : }
3280 : :
3281 : :
3282 : :
3283 : : /* Perform insertion of partially redundant or hoistable values.
3284 : : For BLOCK, do the following:
3285 : : 1. Propagate the NEW_SETS of the dominator into the current block.
3286 : : If the block has multiple predecessors,
3287 : : 2a. Iterate over the ANTIC expressions for the block to see if
3288 : : any of them are partially redundant.
3289 : : 2b. If so, insert them into the necessary predecessors to make
3290 : : the expression fully redundant.
3291 : : 2c. Insert a new PHI merging the values of the predecessors.
3292 : : 2d. Insert the new PHI, and the new expressions, into the
3293 : : NEW_SETS set.
3294 : : If the block has multiple successors,
3295 : : 3a. Iterate over the ANTIC values for the block to see if
3296 : : any of them are good candidates for hoisting.
3297 : : 3b. If so, insert expressions computing the values in BLOCK,
3298 : : and add the new expressions into the NEW_SETS set.
3299 : : 4. Recursively call ourselves on the dominator children of BLOCK.
3300 : :
3301 : : Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3302 : : do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3303 : : done in do_hoist_insertion.
3304 : : */
3305 : :
3306 : : static bool
3307 : 3372398 : do_pre_regular_insertion (basic_block block, basic_block dom,
3308 : : vec<pre_expr> exprs)
3309 : : {
3310 : 3372398 : bool new_stuff = false;
3311 : 3372398 : pre_expr expr;
3312 : 3372398 : auto_vec<pre_expr, 2> avail;
3313 : 3372398 : int i;
3314 : :
3315 : 3372398 : avail.safe_grow (EDGE_COUNT (block->preds), true);
3316 : :
3317 : 23469682 : FOR_EACH_VEC_ELT (exprs, i, expr)
3318 : : {
3319 : 20097284 : if (expr->kind == NARY
3320 : 20097284 : || expr->kind == REFERENCE)
3321 : : {
3322 : 11607318 : unsigned int val;
3323 : 11607318 : bool by_some = false;
3324 : 11607318 : bool cant_insert = false;
3325 : 11607318 : bool all_same = true;
3326 : 11607318 : unsigned num_inserts = 0;
3327 : 11607318 : unsigned num_const = 0;
3328 : 11607318 : pre_expr first_s = NULL;
3329 : 11607318 : edge pred;
3330 : 11607318 : basic_block bprime;
3331 : 11607318 : pre_expr eprime = NULL;
3332 : 11607318 : edge_iterator ei;
3333 : 11607318 : pre_expr edoubleprime = NULL;
3334 : 11607318 : bool do_insertion = false;
3335 : :
3336 : 11607318 : val = get_expr_value_id (expr);
3337 : 23214636 : if (bitmap_set_contains_value (PHI_GEN (block), val))
3338 : 995739 : continue;
3339 : 10914388 : if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3340 : : {
3341 : 302809 : if (dump_file && (dump_flags & TDF_DETAILS))
3342 : : {
3343 : 7 : fprintf (dump_file, "Found fully redundant value: ");
3344 : 7 : print_pre_expr (dump_file, expr);
3345 : 7 : fprintf (dump_file, "\n");
3346 : : }
3347 : 302809 : continue;
3348 : : }
3349 : :
3350 : 34469096 : FOR_EACH_EDGE (pred, ei, block->preds)
3351 : : {
3352 : 23858414 : unsigned int vprime;
3353 : :
3354 : : /* We should never run insertion for the exit block
3355 : : and so not come across fake pred edges. */
3356 : 23858414 : gcc_assert (!(pred->flags & EDGE_FAKE));
3357 : 23858414 : bprime = pred->src;
3358 : : /* We are looking at ANTIC_OUT of bprime. */
3359 : 23858414 : eprime = phi_translate (NULL, expr, ANTIC_IN (block), NULL, pred);
3360 : :
3361 : : /* eprime will generally only be NULL if the
3362 : : value of the expression, translated
3363 : : through the PHI for this predecessor, is
3364 : : undefined. If that is the case, we can't
3365 : : make the expression fully redundant,
3366 : : because its value is undefined along a
3367 : : predecessor path. We can thus break out
3368 : : early because it doesn't matter what the
3369 : : rest of the results are. */
3370 : 23858414 : if (eprime == NULL)
3371 : : {
3372 : 897 : avail[pred->dest_idx] = NULL;
3373 : 897 : cant_insert = true;
3374 : 897 : break;
3375 : : }
3376 : :
3377 : 23857517 : vprime = get_expr_value_id (eprime);
3378 : 23857517 : edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3379 : : vprime);
3380 : 23857517 : if (edoubleprime == NULL)
3381 : : {
3382 : 21475243 : avail[pred->dest_idx] = eprime;
3383 : 21475243 : all_same = false;
3384 : 21475243 : num_inserts++;
3385 : : }
3386 : : else
3387 : : {
3388 : 2382274 : avail[pred->dest_idx] = edoubleprime;
3389 : 2382274 : by_some = true;
3390 : 2382274 : if (edoubleprime->kind == CONSTANT)
3391 : 1585096 : num_const++;
3392 : : /* We want to perform insertions to remove a redundancy on
3393 : : a path in the CFG we want to optimize for speed. */
3394 : 2382274 : if (optimize_edge_for_speed_p (pred))
3395 : 1981150 : do_insertion = true;
3396 : 2382274 : if (first_s == NULL)
3397 : : first_s = edoubleprime;
3398 : 243307 : else if (!pre_expr_d::equal (first_s, edoubleprime))
3399 : 187045 : all_same = false;
3400 : : }
3401 : : }
3402 : : /* If we can insert it, it's not the same value
3403 : : already existing along every predecessor, and
3404 : : it's defined by some predecessor, it is
3405 : : partially redundant. */
3406 : 10611579 : if (!cant_insert && !all_same && by_some)
3407 : : {
3408 : : /* If the expression is redundant on all edges and we need
3409 : : to at most insert one copy from a constant do the PHI
3410 : : insertion even when not optimizing a path that's to be
3411 : : optimized for speed. */
3412 : 2136126 : if (num_inserts == 0 && num_const <= 1)
3413 : : do_insertion = true;
3414 : 2020689 : if (!do_insertion)
3415 : : {
3416 : 355787 : if (dump_file && (dump_flags & TDF_DETAILS))
3417 : : {
3418 : 0 : fprintf (dump_file, "Skipping partial redundancy for "
3419 : : "expression ");
3420 : 0 : print_pre_expr (dump_file, expr);
3421 : 0 : fprintf (dump_file, " (%04d), no redundancy on to be "
3422 : : "optimized for speed edge\n", val);
3423 : : }
3424 : : }
3425 : 1780339 : else if (dbg_cnt (treepre_insert))
3426 : : {
3427 : 1780339 : if (dump_file && (dump_flags & TDF_DETAILS))
3428 : : {
3429 : 66 : fprintf (dump_file, "Found partial redundancy for "
3430 : : "expression ");
3431 : 66 : print_pre_expr (dump_file, expr);
3432 : 66 : fprintf (dump_file, " (%04d)\n",
3433 : : get_expr_value_id (expr));
3434 : : }
3435 : 1780339 : if (insert_into_preds_of_block (block,
3436 : : get_expression_id (expr),
3437 : : avail))
3438 : 10611579 : new_stuff = true;
3439 : : }
3440 : : }
3441 : : /* If all edges produce the same value and that value is
3442 : : an invariant, then the PHI has the same value on all
3443 : : edges. Note this. */
3444 : 8475453 : else if (!cant_insert
3445 : 8475453 : && all_same
3446 : 8475453 : && (edoubleprime->kind != NAME
3447 : 1158 : || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI
3448 : : (PRE_EXPR_NAME (edoubleprime))))
3449 : : {
3450 : 2824 : gcc_assert (edoubleprime->kind == CONSTANT
3451 : : || edoubleprime->kind == NAME);
3452 : :
3453 : 2824 : tree temp = make_temp_ssa_name (get_expr_type (expr),
3454 : : NULL, "pretmp");
3455 : 2824 : gassign *assign
3456 : 2824 : = gimple_build_assign (temp,
3457 : 2824 : edoubleprime->kind == CONSTANT ?
3458 : : PRE_EXPR_CONSTANT (edoubleprime) :
3459 : : PRE_EXPR_NAME (edoubleprime));
3460 : 2824 : gimple_stmt_iterator gsi = gsi_after_labels (block);
3461 : 2824 : gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3462 : :
3463 : 2824 : vn_ssa_aux_t vn_info = VN_INFO (temp);
3464 : 2824 : vn_info->value_id = val;
3465 : 2824 : vn_info->valnum = vn_valnum_from_value_id (val);
3466 : 2824 : if (vn_info->valnum == NULL_TREE)
3467 : 765 : vn_info->valnum = temp;
3468 : 2824 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3469 : 2824 : pre_expr newe = get_or_alloc_expr_for_name (temp);
3470 : 2824 : add_to_value (val, newe);
3471 : 2824 : bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3472 : 2824 : bitmap_insert_into_set (NEW_SETS (block), newe);
3473 : 2824 : bitmap_insert_into_set (PHI_GEN (block), newe);
3474 : : }
3475 : : }
3476 : : }
3477 : :
3478 : 3372398 : return new_stuff;
3479 : 3372398 : }
3480 : :
3481 : :
3482 : : /* Perform insertion for partially anticipatable expressions. There
3483 : : is only one case we will perform insertion for these. This case is
3484 : : if the expression is partially anticipatable, and fully available.
3485 : : In this case, we know that putting it earlier will enable us to
3486 : : remove the later computation. */
3487 : :
3488 : : static bool
3489 : 286698 : do_pre_partial_partial_insertion (basic_block block, basic_block dom,
3490 : : vec<pre_expr> exprs)
3491 : : {
3492 : 286698 : bool new_stuff = false;
3493 : 286698 : pre_expr expr;
3494 : 286698 : auto_vec<pre_expr, 2> avail;
3495 : 286698 : int i;
3496 : :
3497 : 286698 : avail.safe_grow (EDGE_COUNT (block->preds), true);
3498 : :
3499 : 2904994 : FOR_EACH_VEC_ELT (exprs, i, expr)
3500 : : {
3501 : 2618296 : if (expr->kind == NARY
3502 : 2618296 : || expr->kind == REFERENCE)
3503 : : {
3504 : 2017163 : unsigned int val;
3505 : 2017163 : bool by_all = true;
3506 : 2017163 : bool cant_insert = false;
3507 : 2017163 : edge pred;
3508 : 2017163 : basic_block bprime;
3509 : 2017163 : pre_expr eprime = NULL;
3510 : 2017163 : edge_iterator ei;
3511 : :
3512 : 2017163 : val = get_expr_value_id (expr);
3513 : 4034326 : if (bitmap_set_contains_value (PHI_GEN (block), val))
3514 : 64184 : continue;
3515 : 2007854 : if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3516 : 54875 : continue;
3517 : :
3518 : 2034555 : FOR_EACH_EDGE (pred, ei, block->preds)
3519 : : {
3520 : 2024799 : unsigned int vprime;
3521 : 2024799 : pre_expr edoubleprime;
3522 : :
3523 : : /* We should never run insertion for the exit block
3524 : : and so not come across fake pred edges. */
3525 : 2024799 : gcc_assert (!(pred->flags & EDGE_FAKE));
3526 : 2024799 : bprime = pred->src;
3527 : 4049598 : eprime = phi_translate (NULL, expr, ANTIC_IN (block),
3528 : 2024799 : PA_IN (block), pred);
3529 : :
3530 : : /* eprime will generally only be NULL if the
3531 : : value of the expression, translated
3532 : : through the PHI for this predecessor, is
3533 : : undefined. If that is the case, we can't
3534 : : make the expression fully redundant,
3535 : : because its value is undefined along a
3536 : : predecessor path. We can thus break out
3537 : : early because it doesn't matter what the
3538 : : rest of the results are. */
3539 : 2024799 : if (eprime == NULL)
3540 : : {
3541 : 154 : avail[pred->dest_idx] = NULL;
3542 : 154 : cant_insert = true;
3543 : 154 : break;
3544 : : }
3545 : :
3546 : 2024645 : vprime = get_expr_value_id (eprime);
3547 : 2024645 : edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3548 : 2024645 : avail[pred->dest_idx] = edoubleprime;
3549 : 2024645 : if (edoubleprime == NULL)
3550 : : {
3551 : : by_all = false;
3552 : : break;
3553 : : }
3554 : : }
3555 : :
3556 : : /* If we can insert it, it's not the same value
3557 : : already existing along every predecessor, and
3558 : : it's defined by some predecessor, it is
3559 : : partially redundant. */
3560 : 1952979 : if (!cant_insert && by_all)
3561 : : {
3562 : 9756 : edge succ;
3563 : 9756 : bool do_insertion = false;
3564 : :
3565 : : /* Insert only if we can remove a later expression on a path
3566 : : that we want to optimize for speed.
3567 : : The phi node that we will be inserting in BLOCK is not free,
3568 : : and inserting it for the sake of !optimize_for_speed successor
3569 : : may cause regressions on the speed path. */
3570 : 26333 : FOR_EACH_EDGE (succ, ei, block->succs)
3571 : : {
3572 : 16577 : if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3573 : 16577 : || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3574 : : {
3575 : 8503 : if (optimize_edge_for_speed_p (succ))
3576 : 16577 : do_insertion = true;
3577 : : }
3578 : : }
3579 : :
3580 : 9756 : if (!do_insertion)
3581 : : {
3582 : 3698 : if (dump_file && (dump_flags & TDF_DETAILS))
3583 : : {
3584 : 0 : fprintf (dump_file, "Skipping partial partial redundancy "
3585 : : "for expression ");
3586 : 0 : print_pre_expr (dump_file, expr);
3587 : 0 : fprintf (dump_file, " (%04d), not (partially) anticipated "
3588 : : "on any to be optimized for speed edges\n", val);
3589 : : }
3590 : : }
3591 : 6058 : else if (dbg_cnt (treepre_insert))
3592 : : {
3593 : 6058 : pre_stats.pa_insert++;
3594 : 6058 : if (dump_file && (dump_flags & TDF_DETAILS))
3595 : : {
3596 : 0 : fprintf (dump_file, "Found partial partial redundancy "
3597 : : "for expression ");
3598 : 0 : print_pre_expr (dump_file, expr);
3599 : 0 : fprintf (dump_file, " (%04d)\n",
3600 : : get_expr_value_id (expr));
3601 : : }
3602 : 6058 : if (insert_into_preds_of_block (block,
3603 : : get_expression_id (expr),
3604 : : avail))
3605 : 9756 : new_stuff = true;
3606 : : }
3607 : : }
3608 : : }
3609 : : }
3610 : :
3611 : 286698 : return new_stuff;
3612 : 286698 : }
3613 : :
3614 : : /* Insert expressions in BLOCK to compute hoistable values up.
3615 : : Return TRUE if something was inserted, otherwise return FALSE.
3616 : : The caller has to make sure that BLOCK has at least two successors. */
3617 : :
3618 : : static bool
3619 : 4439190 : do_hoist_insertion (basic_block block)
3620 : : {
3621 : 4439190 : edge e;
3622 : 4439190 : edge_iterator ei;
3623 : 4439190 : bool new_stuff = false;
3624 : 4439190 : unsigned i;
3625 : 4439190 : gimple_stmt_iterator last;
3626 : :
3627 : : /* At least two successors, or else... */
3628 : 4439190 : gcc_assert (EDGE_COUNT (block->succs) >= 2);
3629 : :
3630 : : /* Check that all successors of BLOCK are dominated by block.
3631 : : We could use dominated_by_p() for this, but actually there is a much
3632 : : quicker check: any successor that is dominated by BLOCK can't have
3633 : : more than one predecessor edge. */
3634 : 13404120 : FOR_EACH_EDGE (e, ei, block->succs)
3635 : 13274269 : if (! single_pred_p (e->dest))
3636 : : return false;
3637 : :
3638 : : /* Determine the insertion point. If we cannot safely insert before
3639 : : the last stmt if we'd have to, bail out. */
3640 : 4432134 : last = gsi_last_bb (block);
3641 : 4432134 : if (!gsi_end_p (last)
3642 : 4431721 : && !is_ctrl_stmt (gsi_stmt (last))
3643 : 5054086 : && stmt_ends_bb_p (gsi_stmt (last)))
3644 : : return false;
3645 : :
3646 : : /* We have multiple successors, compute ANTIC_OUT by taking the intersection
3647 : : of all of ANTIC_IN translating through PHI nodes. Track the union
3648 : : of the expression sets so we can pick a representative that is
3649 : : fully generatable out of hoistable expressions. */
3650 : 3810759 : bitmap_set_t ANTIC_OUT = bitmap_set_new ();
3651 : 3810759 : bool first = true;
3652 : 11526875 : FOR_EACH_EDGE (e, ei, block->succs)
3653 : : {
3654 : 7716116 : if (first)
3655 : : {
3656 : 3810759 : phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
3657 : 3810759 : first = false;
3658 : : }
3659 : 3905357 : else if (!gimple_seq_empty_p (phi_nodes (e->dest)))
3660 : : {
3661 : 1 : bitmap_set_t tmp = bitmap_set_new ();
3662 : 1 : phi_translate_set (tmp, ANTIC_IN (e->dest), e);
3663 : 1 : bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
3664 : 1 : bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
3665 : 1 : bitmap_set_free (tmp);
3666 : : }
3667 : : else
3668 : : {
3669 : 3905356 : bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
3670 : 3905356 : bitmap_ior_into (&ANTIC_OUT->expressions,
3671 : 3905356 : &ANTIC_IN (e->dest)->expressions);
3672 : : }
3673 : : }
3674 : :
3675 : : /* Compute the set of hoistable expressions from ANTIC_OUT. First compute
3676 : : hoistable values. */
3677 : 3810759 : bitmap_set hoistable_set;
3678 : :
3679 : : /* A hoistable value must be in ANTIC_OUT(block)
3680 : : but not in AVAIL_OUT(BLOCK). */
3681 : 3810759 : bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
3682 : 3810759 : bitmap_and_compl (&hoistable_set.values,
3683 : 3810759 : &ANTIC_OUT->values, &AVAIL_OUT (block)->values);
3684 : :
3685 : : /* Short-cut for a common case: hoistable_set is empty. */
3686 : 3810759 : if (bitmap_empty_p (&hoistable_set.values))
3687 : : {
3688 : 3177644 : bitmap_set_free (ANTIC_OUT);
3689 : 3177644 : return false;
3690 : : }
3691 : :
3692 : : /* Compute which of the hoistable values is in AVAIL_OUT of
3693 : : at least one of the successors of BLOCK. */
3694 : 633115 : bitmap_head availout_in_some;
3695 : 633115 : bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
3696 : 1905456 : FOR_EACH_EDGE (e, ei, block->succs)
3697 : : /* Do not consider expressions solely because their availability
3698 : : on loop exits. They'd be ANTIC-IN throughout the whole loop
3699 : : and thus effectively hoisted across loops by combination of
3700 : : PRE and hoisting. */
3701 : 1272341 : if (! loop_exit_edge_p (block->loop_father, e))
3702 : 1127193 : bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
3703 : 1127193 : &AVAIL_OUT (e->dest)->values);
3704 : 633115 : bitmap_clear (&hoistable_set.values);
3705 : :
3706 : : /* Short-cut for a common case: availout_in_some is empty. */
3707 : 633115 : if (bitmap_empty_p (&availout_in_some))
3708 : : {
3709 : 503264 : bitmap_set_free (ANTIC_OUT);
3710 : 503264 : return false;
3711 : : }
3712 : :
3713 : : /* Hack hoistable_set in-place so we can use sorted_array_from_bitmap_set. */
3714 : 129851 : bitmap_move (&hoistable_set.values, &availout_in_some);
3715 : 129851 : hoistable_set.expressions = ANTIC_OUT->expressions;
3716 : :
3717 : : /* Now finally construct the topological-ordered expression set. */
3718 : 129851 : vec<pre_expr> exprs = sorted_array_from_bitmap_set (&hoistable_set);
3719 : :
3720 : : /* If there are candidate values for hoisting, insert expressions
3721 : : strategically to make the hoistable expressions fully redundant. */
3722 : 129851 : pre_expr expr;
3723 : 386695 : FOR_EACH_VEC_ELT (exprs, i, expr)
3724 : : {
3725 : : /* While we try to sort expressions topologically above the
3726 : : sorting doesn't work out perfectly. Catch expressions we
3727 : : already inserted. */
3728 : 256844 : unsigned int value_id = get_expr_value_id (expr);
3729 : 513688 : if (bitmap_set_contains_value (AVAIL_OUT (block), value_id))
3730 : : {
3731 : 56060 : if (dump_file && (dump_flags & TDF_DETAILS))
3732 : : {
3733 : 1 : fprintf (dump_file,
3734 : : "Already inserted expression for ");
3735 : 1 : print_pre_expr (dump_file, expr);
3736 : 1 : fprintf (dump_file, " (%04d)\n", value_id);
3737 : : }
3738 : 56078 : continue;
3739 : : }
3740 : :
3741 : : /* If we end up with a punned expression representation and this
3742 : : happens to be a float typed one give up - we can't know for
3743 : : sure whether all paths perform the floating-point load we are
3744 : : about to insert and on some targets this can cause correctness
3745 : : issues. See PR88240. */
3746 : 200784 : if (expr->kind == REFERENCE
3747 : 93487 : && PRE_EXPR_REFERENCE (expr)->punned
3748 : 201022 : && FLOAT_TYPE_P (get_expr_type (expr)))
3749 : 0 : continue;
3750 : :
3751 : : /* Only hoist if the full expression is available for hoisting.
3752 : : This avoids hoisting values that are not common and for
3753 : : example evaluate an expression that's not valid to evaluate
3754 : : unconditionally (PR112310). */
3755 : 200784 : if (!valid_in_sets (&hoistable_set, AVAIL_OUT (block), expr))
3756 : 18 : continue;
3757 : :
3758 : : /* OK, we should hoist this value. Perform the transformation. */
3759 : 200766 : pre_stats.hoist_insert++;
3760 : 200766 : if (dump_file && (dump_flags & TDF_DETAILS))
3761 : : {
3762 : 4 : fprintf (dump_file,
3763 : : "Inserting expression in block %d for code hoisting: ",
3764 : : block->index);
3765 : 4 : print_pre_expr (dump_file, expr);
3766 : 4 : fprintf (dump_file, " (%04d)\n", value_id);
3767 : : }
3768 : :
3769 : 200766 : gimple_seq stmts = NULL;
3770 : 200766 : tree res = create_expression_by_pieces (block, expr, &stmts,
3771 : : get_expr_type (expr));
3772 : :
3773 : : /* Do not return true if expression creation ultimately
3774 : : did not insert any statements. */
3775 : 200766 : if (gimple_seq_empty_p (stmts))
3776 : : res = NULL_TREE;
3777 : : else
3778 : : {
3779 : 200766 : if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
3780 : 200766 : gsi_insert_seq_before (&last, stmts, GSI_SAME_STMT);
3781 : : else
3782 : 0 : gsi_insert_seq_after (&last, stmts, GSI_NEW_STMT);
3783 : : }
3784 : :
3785 : : /* Make sure to not return true if expression creation ultimately
3786 : : failed but also make sure to insert any stmts produced as they
3787 : : are tracked in inserted_exprs. */
3788 : 200766 : if (! res)
3789 : 0 : continue;
3790 : :
3791 : 200766 : new_stuff = true;
3792 : : }
3793 : :
3794 : 129851 : exprs.release ();
3795 : 129851 : bitmap_clear (&hoistable_set.values);
3796 : 129851 : bitmap_set_free (ANTIC_OUT);
3797 : :
3798 : 129851 : return new_stuff;
3799 : : }
3800 : :
3801 : : /* Perform insertion of partially redundant and hoistable values. */
3802 : :
3803 : : static void
3804 : 928096 : insert (void)
3805 : : {
3806 : 928096 : basic_block bb;
3807 : :
3808 : 15165768 : FOR_ALL_BB_FN (bb, cfun)
3809 : 14237672 : NEW_SETS (bb) = bitmap_set_new ();
3810 : :
3811 : 928096 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3812 : 928096 : int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun) + 1);
3813 : 928096 : int rpo_num = pre_and_rev_post_order_compute (NULL, rpo, false);
3814 : 13309576 : for (int i = 0; i < rpo_num; ++i)
3815 : 12381480 : bb_rpo[rpo[i]] = i;
3816 : :
3817 : : int num_iterations = 0;
3818 : 974106 : bool changed;
3819 : 974106 : do
3820 : : {
3821 : 974106 : num_iterations++;
3822 : 974106 : if (dump_file && dump_flags & TDF_DETAILS)
3823 : 18 : fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3824 : :
3825 : : changed = false;
3826 : 17026777 : for (int idx = 0; idx < rpo_num; ++idx)
3827 : : {
3828 : 16052671 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
3829 : 16052671 : basic_block dom = get_immediate_dominator (CDI_DOMINATORS, block);
3830 : 16052671 : if (dom)
3831 : : {
3832 : 16052671 : unsigned i;
3833 : 16052671 : bitmap_iterator bi;
3834 : 16052671 : bitmap_set_t newset;
3835 : :
3836 : : /* First, update the AVAIL_OUT set with anything we may have
3837 : : inserted higher up in the dominator tree. */
3838 : 16052671 : newset = NEW_SETS (dom);
3839 : :
3840 : : /* Note that we need to value_replace both NEW_SETS, and
3841 : : AVAIL_OUT. For both the case of NEW_SETS, the value may be
3842 : : represented by some non-simple expression here that we want
3843 : : to replace it with. */
3844 : 16052671 : bool avail_out_changed = false;
3845 : 30272361 : FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
3846 : : {
3847 : 14219690 : pre_expr expr = expression_for_id (i);
3848 : 14219690 : bitmap_value_replace_in_set (NEW_SETS (block), expr);
3849 : 14219690 : avail_out_changed
3850 : 14219690 : |= bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
3851 : : }
3852 : : /* We need to iterate if AVAIL_OUT of an already processed
3853 : : block source changed. */
3854 : 16052671 : if (avail_out_changed && !changed)
3855 : : {
3856 : 1552131 : edge_iterator ei;
3857 : 1552131 : edge e;
3858 : 3698261 : FOR_EACH_EDGE (e, ei, block->succs)
3859 : 2146130 : if (e->dest->index != EXIT_BLOCK
3860 : 2054841 : && bb_rpo[e->dest->index] < idx)
3861 : 2146130 : changed = true;
3862 : : }
3863 : :
3864 : : /* Insert expressions for partial redundancies. */
3865 : 32104779 : if (flag_tree_pre && !single_pred_p (block))
3866 : : {
3867 : 3117632 : vec<pre_expr> exprs
3868 : 3117632 : = sorted_array_from_bitmap_set (ANTIC_IN (block));
3869 : : /* Sorting is not perfect, iterate locally. */
3870 : 6490030 : while (do_pre_regular_insertion (block, dom, exprs))
3871 : : ;
3872 : 3117632 : exprs.release ();
3873 : 3117632 : if (do_partial_partial)
3874 : : {
3875 : 283716 : exprs = sorted_array_from_bitmap_set (PA_IN (block));
3876 : 570414 : while (do_pre_partial_partial_insertion (block, dom,
3877 : : exprs))
3878 : : ;
3879 : 283716 : exprs.release ();
3880 : : }
3881 : : }
3882 : : }
3883 : : }
3884 : :
3885 : : /* Clear the NEW sets before the next iteration. We have already
3886 : : fully propagated its contents. */
3887 : 974106 : if (changed)
3888 : 3809221 : FOR_ALL_BB_FN (bb, cfun)
3889 : 7526422 : bitmap_set_free (NEW_SETS (bb));
3890 : : }
3891 : : while (changed);
3892 : :
3893 : 928096 : statistics_histogram_event (cfun, "insert iterations", num_iterations);
3894 : :
3895 : : /* AVAIL_OUT is not needed after insertion so we don't have to
3896 : : propagate NEW_SETS from hoist insertion. */
3897 : 15165768 : FOR_ALL_BB_FN (bb, cfun)
3898 : : {
3899 : 14237672 : bitmap_set_free (NEW_SETS (bb));
3900 : 14237672 : bitmap_set_pool.remove (NEW_SETS (bb));
3901 : 14237672 : NEW_SETS (bb) = NULL;
3902 : : }
3903 : :
3904 : : /* Insert expressions for hoisting. Do a backward walk here since
3905 : : inserting into BLOCK exposes new opportunities in its predecessors.
3906 : : Since PRE and hoist insertions can cause back-to-back iteration
3907 : : and we are interested in PRE insertion exposed hoisting opportunities
3908 : : but not in hoisting exposed PRE ones do hoist insertion only after
3909 : : PRE insertion iteration finished and do not iterate it. */
3910 : 928096 : if (flag_code_hoisting)
3911 : 13309056 : for (int idx = rpo_num - 1; idx >= 0; --idx)
3912 : : {
3913 : 12381012 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
3914 : 16820202 : if (EDGE_COUNT (block->succs) >= 2)
3915 : 4439190 : changed |= do_hoist_insertion (block);
3916 : : }
3917 : :
3918 : 928096 : free (rpo);
3919 : 928096 : free (bb_rpo);
3920 : 928096 : }
3921 : :
3922 : :
3923 : : /* Compute the AVAIL set for all basic blocks.
3924 : :
3925 : : This function performs value numbering of the statements in each basic
3926 : : block. The AVAIL sets are built from information we glean while doing
3927 : : this value numbering, since the AVAIL sets contain only one entry per
3928 : : value.
3929 : :
3930 : : AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
3931 : : AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
3932 : :
3933 : : static void
3934 : 928096 : compute_avail (function *fun)
3935 : : {
3936 : :
3937 : 928096 : basic_block block, son;
3938 : 928096 : basic_block *worklist;
3939 : 928096 : size_t sp = 0;
3940 : 928096 : unsigned i;
3941 : 928096 : tree name;
3942 : :
3943 : : /* We pretend that default definitions are defined in the entry block.
3944 : : This includes function arguments and the static chain decl. */
3945 : 43333776 : FOR_EACH_SSA_NAME (i, name, fun)
3946 : : {
3947 : 31849854 : pre_expr e;
3948 : 31849854 : if (!SSA_NAME_IS_DEFAULT_DEF (name)
3949 : 2798905 : || has_zero_uses (name)
3950 : 34153528 : || virtual_operand_p (name))
3951 : 30473508 : continue;
3952 : :
3953 : 1376346 : e = get_or_alloc_expr_for_name (name);
3954 : 1376346 : add_to_value (get_expr_value_id (e), e);
3955 : 1376346 : bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun)), e);
3956 : 1376346 : bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun)),
3957 : : e);
3958 : : }
3959 : :
3960 : 928096 : if (dump_file && (dump_flags & TDF_DETAILS))
3961 : : {
3962 : 14 : print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun)),
3963 : : "tmp_gen", ENTRY_BLOCK);
3964 : 14 : print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun)),
3965 : : "avail_out", ENTRY_BLOCK);
3966 : : }
3967 : :
3968 : : /* Allocate the worklist. */
3969 : 928096 : worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
3970 : :
3971 : : /* Seed the algorithm by putting the dominator children of the entry
3972 : : block on the worklist. */
3973 : 928096 : for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (fun));
3974 : 1856192 : son;
3975 : 928096 : son = next_dom_son (CDI_DOMINATORS, son))
3976 : 928096 : worklist[sp++] = son;
3977 : :
3978 : 1856192 : BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (fun))
3979 : 928096 : = ssa_default_def (fun, gimple_vop (fun));
3980 : :
3981 : : /* Loop until the worklist is empty. */
3982 : 13309576 : while (sp)
3983 : : {
3984 : 12381480 : gimple *stmt;
3985 : 12381480 : basic_block dom;
3986 : :
3987 : : /* Pick a block from the worklist. */
3988 : 12381480 : block = worklist[--sp];
3989 : 12381480 : vn_context_bb = block;
3990 : :
3991 : : /* Initially, the set of available values in BLOCK is that of
3992 : : its immediate dominator. */
3993 : 12381480 : dom = get_immediate_dominator (CDI_DOMINATORS, block);
3994 : 12381480 : if (dom)
3995 : : {
3996 : 12381480 : bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
3997 : 12381480 : BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
3998 : : }
3999 : :
4000 : : /* Generate values for PHI nodes. */
4001 : 15923169 : for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
4002 : 3541689 : gsi_next (&gsi))
4003 : : {
4004 : 3541689 : tree result = gimple_phi_result (gsi.phi ());
4005 : :
4006 : : /* We have no need for virtual phis, as they don't represent
4007 : : actual computations. */
4008 : 7083378 : if (virtual_operand_p (result))
4009 : : {
4010 : 1620055 : BB_LIVE_VOP_ON_EXIT (block) = result;
4011 : 1620055 : continue;
4012 : : }
4013 : :
4014 : 1921634 : pre_expr e = get_or_alloc_expr_for_name (result);
4015 : 1921634 : add_to_value (get_expr_value_id (e), e);
4016 : 1921634 : bitmap_value_insert_into_set (AVAIL_OUT (block), e);
4017 : 1921634 : bitmap_insert_into_set (PHI_GEN (block), e);
4018 : : }
4019 : :
4020 : 12381480 : BB_MAY_NOTRETURN (block) = 0;
4021 : :
4022 : : /* Now compute value numbers and populate value sets with all
4023 : : the expressions computed in BLOCK. */
4024 : 12381480 : bool set_bb_may_notreturn = false;
4025 : 95731630 : for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
4026 : 70968670 : gsi_next (&gsi))
4027 : : {
4028 : 70968670 : ssa_op_iter iter;
4029 : 70968670 : tree op;
4030 : :
4031 : 70968670 : stmt = gsi_stmt (gsi);
4032 : :
4033 : 70968670 : if (set_bb_may_notreturn)
4034 : : {
4035 : 2569778 : BB_MAY_NOTRETURN (block) = 1;
4036 : 2569778 : set_bb_may_notreturn = false;
4037 : : }
4038 : :
4039 : : /* Cache whether the basic-block has any non-visible side-effect
4040 : : or control flow.
4041 : : If this isn't a call or it is the last stmt in the
4042 : : basic-block then the CFG represents things correctly. */
4043 : 70968670 : if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
4044 : : {
4045 : : /* Non-looping const functions always return normally.
4046 : : Otherwise the call might not return or have side-effects
4047 : : that forbids hoisting possibly trapping expressions
4048 : : before it. */
4049 : 3582573 : int flags = gimple_call_flags (stmt);
4050 : 3582573 : if (!(flags & (ECF_CONST|ECF_PURE))
4051 : 546260 : || (flags & ECF_LOOPING_CONST_OR_PURE)
4052 : 4100641 : || stmt_can_throw_external (fun, stmt))
4053 : : /* Defer setting of BB_MAY_NOTRETURN to avoid it
4054 : : influencing the processing of the call itself. */
4055 : : set_bb_may_notreturn = true;
4056 : : }
4057 : :
4058 : 85095221 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
4059 : : {
4060 : 14126551 : pre_expr e = get_or_alloc_expr_for_name (op);
4061 : 14126551 : add_to_value (get_expr_value_id (e), e);
4062 : 14126551 : bitmap_insert_into_set (TMP_GEN (block), e);
4063 : 14126551 : bitmap_value_insert_into_set (AVAIL_OUT (block), e);
4064 : : }
4065 : :
4066 : 96471781 : if (gimple_vdef (stmt))
4067 : 11295832 : BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
4068 : :
4069 : 70968670 : if (gimple_has_side_effects (stmt)
4070 : 64929188 : || stmt_could_throw_p (fun, stmt)
4071 : 134787109 : || is_gimple_debug (stmt))
4072 : 66042216 : continue;
4073 : :
4074 : 44084206 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
4075 : : {
4076 : 20666160 : if (ssa_undefined_value_p (op))
4077 : 45172 : continue;
4078 : 20620988 : pre_expr e = get_or_alloc_expr_for_name (op);
4079 : 20620988 : bitmap_value_insert_into_set (EXP_GEN (block), e);
4080 : : }
4081 : :
4082 : 23418046 : switch (gimple_code (stmt))
4083 : : {
4084 : 906525 : case GIMPLE_RETURN:
4085 : 906525 : continue;
4086 : :
4087 : 514927 : case GIMPLE_CALL:
4088 : 514927 : {
4089 : 514927 : vn_reference_t ref;
4090 : 514927 : vn_reference_s ref1;
4091 : 514927 : pre_expr result = NULL;
4092 : :
4093 : 514927 : vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
4094 : : /* There is no point to PRE a call without a value. */
4095 : 514927 : if (!ref || !ref->result)
4096 : 8902 : continue;
4097 : :
4098 : : /* If the value of the call is not invalidated in
4099 : : this block until it is computed, add the expression
4100 : : to EXP_GEN. */
4101 : 506025 : if ((!gimple_vuse (stmt)
4102 : 288190 : || gimple_code
4103 : 288190 : (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
4104 : 263700 : || gimple_bb (SSA_NAME_DEF_STMT
4105 : : (gimple_vuse (stmt))) != block)
4106 : : /* If the REFERENCE traps and there was a preceding
4107 : : point in the block that might not return avoid
4108 : : adding the reference to EXP_GEN. */
4109 : 747420 : && (!BB_MAY_NOTRETURN (block)
4110 : 9934 : || !vn_reference_may_trap (ref)))
4111 : : {
4112 : 449296 : result = get_or_alloc_expr_for_reference
4113 : 449296 : (ref, gimple_location (stmt));
4114 : 449296 : add_to_value (get_expr_value_id (result), result);
4115 : 449296 : bitmap_value_insert_into_set (EXP_GEN (block), result);
4116 : : }
4117 : 506025 : continue;
4118 : 506025 : }
4119 : :
4120 : 17070140 : case GIMPLE_ASSIGN:
4121 : 17070140 : {
4122 : 17070140 : pre_expr result = NULL;
4123 : 17070140 : switch (vn_get_stmt_kind (stmt))
4124 : : {
4125 : 7086420 : case VN_NARY:
4126 : 7086420 : {
4127 : 7086420 : enum tree_code code = gimple_assign_rhs_code (stmt);
4128 : 7086420 : vn_nary_op_t nary;
4129 : :
4130 : : /* COND_EXPR is awkward in that it contains an
4131 : : embedded complex expression.
4132 : : Don't even try to shove it through PRE. */
4133 : 7086420 : if (code == COND_EXPR)
4134 : 130017 : continue;
4135 : :
4136 : 7082923 : vn_nary_op_lookup_stmt (stmt, &nary);
4137 : 7082923 : if (!nary || nary->predicated_values)
4138 : 101506 : continue;
4139 : :
4140 : 6981417 : unsigned value_id = nary->value_id;
4141 : 6981417 : if (value_id_constant_p (value_id))
4142 : 0 : continue;
4143 : :
4144 : : /* Record the un-valueized expression for EXP_GEN. */
4145 : 6981417 : nary = XALLOCAVAR (struct vn_nary_op_s,
4146 : : sizeof_vn_nary_op
4147 : : (vn_nary_length_from_stmt (stmt)));
4148 : 6981417 : init_vn_nary_op_from_stmt (nary, as_a <gassign *> (stmt));
4149 : :
4150 : : /* If the NARY traps and there was a preceding
4151 : : point in the block that might not return avoid
4152 : : adding the nary to EXP_GEN. */
4153 : 7006431 : if (BB_MAY_NOTRETURN (block)
4154 : 6981417 : && vn_nary_may_trap (nary))
4155 : 25014 : continue;
4156 : :
4157 : 6956403 : result = get_or_alloc_expr_for_nary
4158 : 6956403 : (nary, value_id, gimple_location (stmt));
4159 : 6956403 : break;
4160 : : }
4161 : :
4162 : 4873618 : case VN_REFERENCE:
4163 : 4873618 : {
4164 : 4873618 : tree rhs1 = gimple_assign_rhs1 (stmt);
4165 : 4873618 : ao_ref rhs1_ref;
4166 : 4873618 : ao_ref_init (&rhs1_ref, rhs1);
4167 : 4873618 : alias_set_type set = ao_ref_alias_set (&rhs1_ref);
4168 : 4873618 : alias_set_type base_set
4169 : 4873618 : = ao_ref_base_alias_set (&rhs1_ref);
4170 : 4873618 : vec<vn_reference_op_s> operands
4171 : 4873618 : = vn_reference_operands_for_lookup (rhs1);
4172 : 4873618 : vn_reference_t ref;
4173 : 9747236 : vn_reference_lookup_pieces (gimple_vuse (stmt), set,
4174 : 4873618 : base_set, TREE_TYPE (rhs1),
4175 : : operands, &ref, VN_WALK);
4176 : 4873618 : if (!ref)
4177 : : {
4178 : 606122 : operands.release ();
4179 : 1724646 : continue;
4180 : : }
4181 : :
4182 : : /* If the REFERENCE traps and there was a preceding
4183 : : point in the block that might not return avoid
4184 : : adding the reference to EXP_GEN. */
4185 : 4474230 : if (BB_MAY_NOTRETURN (block)
4186 : 4267496 : && vn_reference_may_trap (ref))
4187 : : {
4188 : 206734 : operands.release ();
4189 : 206734 : continue;
4190 : : }
4191 : :
4192 : : /* If the value of the reference is not invalidated in
4193 : : this block until it is computed, add the expression
4194 : : to EXP_GEN. */
4195 : 8121524 : if (gimple_vuse (stmt))
4196 : : {
4197 : 4060097 : gimple *def_stmt;
4198 : 4060097 : bool ok = true;
4199 : 4060097 : def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
4200 : 6757884 : while (!gimple_nop_p (def_stmt)
4201 : 5847229 : && gimple_code (def_stmt) != GIMPLE_PHI
4202 : 11525804 : && gimple_bb (def_stmt) == block)
4203 : : {
4204 : 3601834 : if (stmt_may_clobber_ref_p
4205 : 3601834 : (def_stmt, gimple_assign_rhs1 (stmt)))
4206 : : {
4207 : : ok = false;
4208 : : break;
4209 : : }
4210 : 2697787 : def_stmt
4211 : 2697787 : = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
4212 : : }
4213 : 4060097 : if (!ok)
4214 : : {
4215 : 904047 : operands.release ();
4216 : 904047 : continue;
4217 : : }
4218 : : }
4219 : :
4220 : : /* If the load was value-numbered to another
4221 : : load make sure we do not use its expression
4222 : : for insertion if it wouldn't be a valid
4223 : : replacement. */
4224 : : /* At the momemt we have a testcase
4225 : : for hoist insertion of aligned vs. misaligned
4226 : : variants in gcc.dg/torture/pr65270-1.c thus
4227 : : with just alignment to be considered we can
4228 : : simply replace the expression in the hashtable
4229 : : with the most conservative one. */
4230 : 3156715 : vn_reference_op_t ref1 = &ref->operands.last ();
4231 : 3156715 : while (ref1->opcode != TARGET_MEM_REF
4232 : 6313340 : && ref1->opcode != MEM_REF
4233 : 6313340 : && ref1 != &ref->operands[0])
4234 : 3156625 : --ref1;
4235 : 3156715 : vn_reference_op_t ref2 = &operands.last ();
4236 : 3156715 : while (ref2->opcode != TARGET_MEM_REF
4237 : 6313345 : && ref2->opcode != MEM_REF
4238 : 9470309 : && ref2 != &operands[0])
4239 : 3156630 : --ref2;
4240 : 3156715 : if ((ref1->opcode == TARGET_MEM_REF
4241 : : || ref1->opcode == MEM_REF)
4242 : 6313091 : && (TYPE_ALIGN (ref1->type)
4243 : 3156376 : > TYPE_ALIGN (ref2->type)))
4244 : 162 : ref1->type
4245 : 162 : = build_aligned_type (ref1->type,
4246 : 162 : TYPE_ALIGN (ref2->type));
4247 : : /* TBAA behavior is an obvious part so make sure
4248 : : that the hashtable one covers this as well
4249 : : by adjusting the ref alias set and its base. */
4250 : 3156715 : if ((ref->set == set
4251 : 8876 : || alias_set_subset_of (set, ref->set))
4252 : 3158939 : && (ref->base_set == base_set
4253 : 8253 : || alias_set_subset_of (base_set, ref->base_set)))
4254 : : ;
4255 : 11461 : else if (ref1->opcode != ref2->opcode
4256 : 11456 : || (ref1->opcode != MEM_REF
4257 : 11456 : && ref1->opcode != TARGET_MEM_REF))
4258 : : {
4259 : : /* With mismatching base opcodes or bases
4260 : : other than MEM_REF or TARGET_MEM_REF we
4261 : : can't do any easy TBAA adjustment. */
4262 : 5 : operands.release ();
4263 : 5 : continue;
4264 : : }
4265 : 11456 : else if (ref->set == set
4266 : 11456 : || alias_set_subset_of (ref->set, set))
4267 : : {
4268 : 11085 : tree reft = reference_alias_ptr_type (rhs1);
4269 : 11085 : ref->set = set;
4270 : 11085 : ref->base_set = set;
4271 : 11085 : if (ref1->opcode == MEM_REF)
4272 : 11085 : ref1->op0
4273 : 11085 : = wide_int_to_tree (reft,
4274 : 22170 : wi::to_wide (ref1->op0));
4275 : : else
4276 : 0 : ref1->op2
4277 : 0 : = wide_int_to_tree (reft,
4278 : 0 : wi::to_wide (ref1->op2));
4279 : : }
4280 : : else
4281 : : {
4282 : 371 : ref->set = 0;
4283 : 371 : ref->base_set = 0;
4284 : 371 : if (ref1->opcode == MEM_REF)
4285 : 371 : ref1->op0
4286 : 371 : = wide_int_to_tree (ptr_type_node,
4287 : 742 : wi::to_wide (ref1->op0));
4288 : : else
4289 : 0 : ref1->op2
4290 : 0 : = wide_int_to_tree (ptr_type_node,
4291 : 0 : wi::to_wide (ref1->op2));
4292 : : }
4293 : : /* We also need to make sure that the access path
4294 : : ends in an access of the same size as otherwise
4295 : : we might assume an access may not trap while in
4296 : : fact it might. That's independent of whether
4297 : : TBAA is in effect. */
4298 : 3156710 : if (TYPE_SIZE (ref1->type) != TYPE_SIZE (ref2->type)
4299 : 3156710 : && (! TYPE_SIZE (ref1->type)
4300 : 7730 : || ! TYPE_SIZE (ref2->type)
4301 : 7721 : || ! operand_equal_p (TYPE_SIZE (ref1->type),
4302 : 7721 : TYPE_SIZE (ref2->type))))
4303 : : {
4304 : 7738 : operands.release ();
4305 : 7738 : continue;
4306 : : }
4307 : 3148972 : operands.release ();
4308 : :
4309 : 3148972 : result = get_or_alloc_expr_for_reference
4310 : 3148972 : (ref, gimple_location (stmt));
4311 : 3148972 : break;
4312 : : }
4313 : :
4314 : 5110102 : default:
4315 : 5110102 : continue;
4316 : 5110102 : }
4317 : :
4318 : 10105375 : add_to_value (get_expr_value_id (result), result);
4319 : 10105375 : bitmap_value_insert_into_set (EXP_GEN (block), result);
4320 : 10105375 : continue;
4321 : 10105375 : }
4322 : 4926454 : default:
4323 : 4926454 : break;
4324 : 906525 : }
4325 : : }
4326 : 12381480 : if (set_bb_may_notreturn)
4327 : : {
4328 : 497868 : BB_MAY_NOTRETURN (block) = 1;
4329 : 497868 : set_bb_may_notreturn = false;
4330 : : }
4331 : :
4332 : 12381480 : if (dump_file && (dump_flags & TDF_DETAILS))
4333 : : {
4334 : 108 : print_bitmap_set (dump_file, EXP_GEN (block),
4335 : : "exp_gen", block->index);
4336 : 108 : print_bitmap_set (dump_file, PHI_GEN (block),
4337 : : "phi_gen", block->index);
4338 : 108 : print_bitmap_set (dump_file, TMP_GEN (block),
4339 : : "tmp_gen", block->index);
4340 : 108 : print_bitmap_set (dump_file, AVAIL_OUT (block),
4341 : : "avail_out", block->index);
4342 : : }
4343 : :
4344 : : /* Put the dominator children of BLOCK on the worklist of blocks
4345 : : to compute available sets for. */
4346 : 12381480 : for (son = first_dom_son (CDI_DOMINATORS, block);
4347 : 23834864 : son;
4348 : 11453384 : son = next_dom_son (CDI_DOMINATORS, son))
4349 : 11453384 : worklist[sp++] = son;
4350 : : }
4351 : 928096 : vn_context_bb = NULL;
4352 : :
4353 : 928096 : free (worklist);
4354 : 928096 : }
4355 : :
4356 : :
4357 : : /* Initialize data structures used by PRE. */
4358 : :
4359 : : static void
4360 : 928098 : init_pre (void)
4361 : : {
4362 : 928098 : basic_block bb;
4363 : :
4364 : 928098 : next_expression_id = 1;
4365 : 928098 : expressions.create (0);
4366 : 928098 : expressions.safe_push (NULL);
4367 : 928098 : value_expressions.create (get_max_value_id () + 1);
4368 : 928098 : value_expressions.quick_grow_cleared (get_max_value_id () + 1);
4369 : 928098 : constant_value_expressions.create (get_max_constant_value_id () + 1);
4370 : 928098 : constant_value_expressions.quick_grow_cleared (get_max_constant_value_id () + 1);
4371 : 928098 : name_to_id.create (0);
4372 : 928098 : gcc_obstack_init (&pre_expr_obstack);
4373 : :
4374 : 928098 : inserted_exprs = BITMAP_ALLOC (NULL);
4375 : :
4376 : 928098 : connect_infinite_loops_to_exit ();
4377 : 928098 : memset (&pre_stats, 0, sizeof (pre_stats));
4378 : :
4379 : 928098 : alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4380 : :
4381 : 928098 : calculate_dominance_info (CDI_DOMINATORS);
4382 : :
4383 : 928098 : bitmap_obstack_initialize (&grand_bitmap_obstack);
4384 : 1856196 : expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4385 : 15174924 : FOR_ALL_BB_FN (bb, cfun)
4386 : : {
4387 : 14246826 : EXP_GEN (bb) = bitmap_set_new ();
4388 : 14246826 : PHI_GEN (bb) = bitmap_set_new ();
4389 : 14246826 : TMP_GEN (bb) = bitmap_set_new ();
4390 : 14246826 : AVAIL_OUT (bb) = bitmap_set_new ();
4391 : 14246826 : PHI_TRANS_TABLE (bb) = NULL;
4392 : : }
4393 : 928098 : }
4394 : :
4395 : :
4396 : : /* Deallocate data structures used by PRE. */
4397 : :
4398 : : static void
4399 : 928098 : fini_pre ()
4400 : : {
4401 : 928098 : value_expressions.release ();
4402 : 928098 : constant_value_expressions.release ();
4403 : 928098 : expressions.release ();
4404 : 928098 : bitmap_obstack_release (&grand_bitmap_obstack);
4405 : 928098 : bitmap_set_pool.release ();
4406 : 928098 : pre_expr_pool.release ();
4407 : 928098 : delete expression_to_id;
4408 : 928098 : expression_to_id = NULL;
4409 : 928098 : name_to_id.release ();
4410 : 928098 : obstack_free (&pre_expr_obstack, NULL);
4411 : :
4412 : 928098 : basic_block bb;
4413 : 15174607 : FOR_ALL_BB_FN (bb, cfun)
4414 : 14246509 : if (bb->aux && PHI_TRANS_TABLE (bb))
4415 : 5651722 : delete PHI_TRANS_TABLE (bb);
4416 : 928098 : free_aux_for_blocks ();
4417 : 928098 : }
4418 : :
4419 : : namespace {
4420 : :
4421 : : const pass_data pass_data_pre =
4422 : : {
4423 : : GIMPLE_PASS, /* type */
4424 : : "pre", /* name */
4425 : : OPTGROUP_NONE, /* optinfo_flags */
4426 : : TV_TREE_PRE, /* tv_id */
4427 : : ( PROP_cfg | PROP_ssa ), /* properties_required */
4428 : : 0, /* properties_provided */
4429 : : 0, /* properties_destroyed */
4430 : : TODO_rebuild_alias, /* todo_flags_start */
4431 : : 0, /* todo_flags_finish */
4432 : : };
4433 : :
4434 : : class pass_pre : public gimple_opt_pass
4435 : : {
4436 : : public:
4437 : 280831 : pass_pre (gcc::context *ctxt)
4438 : 561662 : : gimple_opt_pass (pass_data_pre, ctxt)
4439 : : {}
4440 : :
4441 : : /* opt_pass methods: */
4442 : 1000779 : bool gate (function *) final override
4443 : 1000779 : { return flag_tree_pre != 0 || flag_code_hoisting != 0; }
4444 : : unsigned int execute (function *) final override;
4445 : :
4446 : : }; // class pass_pre
4447 : :
4448 : : /* Valueization hook for RPO VN when we are calling back to it
4449 : : at ANTIC compute time. */
4450 : :
4451 : : static tree
4452 : 95657359 : pre_valueize (tree name)
4453 : : {
4454 : 95657359 : if (TREE_CODE (name) == SSA_NAME)
4455 : : {
4456 : 95386914 : tree tem = VN_INFO (name)->valnum;
4457 : 95386914 : if (tem != VN_TOP && tem != name)
4458 : : {
4459 : 13842130 : if (TREE_CODE (tem) != SSA_NAME
4460 : 13842130 : || SSA_NAME_IS_DEFAULT_DEF (tem))
4461 : : return tem;
4462 : : /* We create temporary SSA names for representatives that
4463 : : do not have a definition (yet) but are not default defs either
4464 : : assume they are fine to use. */
4465 : 13838521 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (tem));
4466 : 13838521 : if (! def_bb
4467 : 13838521 : || dominated_by_p (CDI_DOMINATORS, vn_context_bb, def_bb))
4468 : 102179 : return tem;
4469 : : /* ??? Now we could look for a leader. Ideally we'd somehow
4470 : : expose RPO VN leaders and get rid of AVAIL_OUT as well... */
4471 : : }
4472 : : }
4473 : : return name;
4474 : : }
4475 : :
4476 : : unsigned int
4477 : 928098 : pass_pre::execute (function *fun)
4478 : : {
4479 : 928098 : unsigned int todo = 0;
4480 : :
4481 : 1856196 : do_partial_partial =
4482 : 928098 : flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4483 : :
4484 : : /* This has to happen before VN runs because
4485 : : loop_optimizer_init may create new phis, etc. */
4486 : 928098 : loop_optimizer_init (LOOPS_NORMAL);
4487 : 928098 : split_edges_for_insertion ();
4488 : 928098 : scev_initialize ();
4489 : 928098 : calculate_dominance_info (CDI_DOMINATORS);
4490 : :
4491 : 928098 : run_rpo_vn (VN_WALK);
4492 : :
4493 : 928098 : init_pre ();
4494 : :
4495 : 928098 : vn_valueize = pre_valueize;
4496 : :
4497 : : /* Insert can get quite slow on an incredibly large number of basic
4498 : : blocks due to some quadratic behavior. Until this behavior is
4499 : : fixed, don't run it when he have an incredibly large number of
4500 : : bb's. If we aren't going to run insert, there is no point in
4501 : : computing ANTIC, either, even though it's plenty fast nor do
4502 : : we require AVAIL. */
4503 : 928098 : if (n_basic_blocks_for_fn (fun) < 4000)
4504 : : {
4505 : 928096 : compute_avail (fun);
4506 : 928096 : compute_antic ();
4507 : 928096 : insert ();
4508 : : }
4509 : :
4510 : : /* Make sure to remove fake edges before committing our inserts.
4511 : : This makes sure we don't end up with extra critical edges that
4512 : : we would need to split. */
4513 : 928098 : remove_fake_exit_edges ();
4514 : 928098 : gsi_commit_edge_inserts ();
4515 : :
4516 : : /* Eliminate folds statements which might (should not...) end up
4517 : : not keeping virtual operands up-to-date. */
4518 : 928098 : gcc_assert (!need_ssa_update_p (fun));
4519 : :
4520 : 928098 : statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4521 : 928098 : statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4522 : 928098 : statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
4523 : 928098 : statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4524 : :
4525 : 928098 : todo |= eliminate_with_rpo_vn (inserted_exprs);
4526 : :
4527 : 928098 : vn_valueize = NULL;
4528 : :
4529 : 928098 : fini_pre ();
4530 : :
4531 : 928098 : scev_finalize ();
4532 : 928098 : loop_optimizer_finalize ();
4533 : :
4534 : : /* Perform a CFG cleanup before we run simple_dce_from_worklist since
4535 : : unreachable code regions will have not up-to-date SSA form which
4536 : : confuses it. */
4537 : 928098 : bool need_crit_edge_split = false;
4538 : 928098 : if (todo & TODO_cleanup_cfg)
4539 : : {
4540 : 131898 : cleanup_tree_cfg ();
4541 : 131898 : need_crit_edge_split = true;
4542 : : }
4543 : :
4544 : : /* Because we don't follow exactly the standard PRE algorithm, and decide not
4545 : : to insert PHI nodes sometimes, and because value numbering of casts isn't
4546 : : perfect, we sometimes end up inserting dead code. This simple DCE-like
4547 : : pass removes any insertions we made that weren't actually used. */
4548 : 928098 : simple_dce_from_worklist (inserted_exprs);
4549 : 928098 : BITMAP_FREE (inserted_exprs);
4550 : :
4551 : : /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4552 : : case we can merge the block with the remaining predecessor of the block.
4553 : : It should either:
4554 : : - call merge_blocks after each tail merge iteration
4555 : : - call merge_blocks after all tail merge iterations
4556 : : - mark TODO_cleanup_cfg when necessary. */
4557 : 928098 : todo |= tail_merge_optimize (need_crit_edge_split);
4558 : :
4559 : 928098 : free_rpo_vn ();
4560 : :
4561 : : /* Tail merging invalidates the virtual SSA web, together with
4562 : : cfg-cleanup opportunities exposed by PRE this will wreck the
4563 : : SSA updating machinery. So make sure to run update-ssa
4564 : : manually, before eventually scheduling cfg-cleanup as part of
4565 : : the todo. */
4566 : 928098 : update_ssa (TODO_update_ssa_only_virtuals);
4567 : :
4568 : 928098 : return todo;
4569 : : }
4570 : :
4571 : : } // anon namespace
4572 : :
4573 : : gimple_opt_pass *
4574 : 280831 : make_pass_pre (gcc::context *ctxt)
4575 : : {
4576 : 280831 : return new pass_pre (ctxt);
4577 : : }
|