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