Line data Source code
1 : /* Full and partial redundancy elimination and code hoisting on SSA GIMPLE.
2 : Copyright (C) 2001-2026 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 "tree-cfg.h"
41 : #include "tree-into-ssa.h"
42 : #include "tree-dfa.h"
43 : #include "tree-ssa.h"
44 : #include "cfgloop.h"
45 : #include "tree-ssa-sccvn.h"
46 : #include "tree-scalar-evolution.h"
47 : #include "dbgcnt.h"
48 : #include "domwalk.h"
49 : #include "tree-ssa-propagate.h"
50 : #include "tree-ssa-dce.h"
51 : #include "tree-cfgcleanup.h"
52 : #include "alias.h"
53 : #include "gimple-range.h"
54 :
55 : /* Even though this file is called tree-ssa-pre.cc, we actually
56 : implement a bit more than just PRE here. All of them piggy-back
57 : on GVN which is implemented in tree-ssa-sccvn.cc.
58 :
59 : 1. Full Redundancy Elimination (FRE)
60 : This is the elimination phase of GVN.
61 :
62 : 2. Partial Redundancy Elimination (PRE)
63 : This is adds computation of AVAIL_OUT and ANTIC_IN and
64 : doing expression insertion to form GVN-PRE.
65 :
66 : 3. Code hoisting
67 : This optimization uses the ANTIC_IN sets computed for PRE
68 : to move expressions further up than PRE would do, to make
69 : multiple computations of the same value fully redundant.
70 : This pass is explained below (after the explanation of the
71 : basic algorithm for PRE).
72 : */
73 :
74 : /* TODO:
75 :
76 : 1. Avail sets can be shared by making an avail_find_leader that
77 : walks up the dominator tree and looks in those avail sets.
78 : This might affect code optimality, it's unclear right now.
79 : Currently the AVAIL_OUT sets are the remaining quadraticness in
80 : memory of GVN-PRE.
81 : 2. Strength reduction can be performed by anticipating expressions
82 : we can repair later on.
83 : 3. We can do back-substitution or smarter value numbering to catch
84 : commutative expressions split up over multiple statements.
85 : */
86 :
87 : /* For ease of terminology, "expression node" in the below refers to
88 : every expression node but GIMPLE_ASSIGN, because GIMPLE_ASSIGNs
89 : represent the actual statement containing the expressions we care about,
90 : and we cache the value number by putting it in the expression. */
91 :
92 : /* Basic algorithm for Partial Redundancy Elimination:
93 :
94 : First we walk the statements to generate the AVAIL sets, the
95 : EXP_GEN sets, and the tmp_gen sets. EXP_GEN sets represent the
96 : generation of values/expressions by a given block. We use them
97 : when computing the ANTIC sets. The AVAIL sets consist of
98 : SSA_NAME's that represent values, so we know what values are
99 : available in what blocks. AVAIL is a forward dataflow problem. In
100 : SSA, values are never killed, so we don't need a kill set, or a
101 : fixpoint iteration, in order to calculate the AVAIL sets. In
102 : traditional parlance, AVAIL sets tell us the downsafety of the
103 : expressions/values.
104 :
105 : Next, we generate the ANTIC sets. These sets represent the
106 : anticipatable expressions. ANTIC is a backwards dataflow
107 : problem. An expression is anticipatable in a given block if it could
108 : be generated in that block. This means that if we had to perform
109 : an insertion in that block, of the value of that expression, we
110 : could. Calculating the ANTIC sets requires phi translation of
111 : expressions, because the flow goes backwards through phis. We must
112 : iterate to a fixpoint of the ANTIC sets, because we have a kill
113 : set. Even in SSA form, values are not live over the entire
114 : function, only from their definition point onwards. So we have to
115 : remove values from the ANTIC set once we go past the definition
116 : point of the leaders that make them up.
117 : compute_antic/compute_antic_aux performs this computation.
118 :
119 : Third, we perform insertions to make partially redundant
120 : expressions fully redundant.
121 :
122 : An expression is partially redundant (excluding partial
123 : anticipation) if:
124 :
125 : 1. It is AVAIL in some, but not all, of the predecessors of a
126 : given block.
127 : 2. It is ANTIC in all the predecessors.
128 :
129 : In order to make it fully redundant, we insert the expression into
130 : the predecessors where it is not available, but is ANTIC.
131 :
132 : When optimizing for size, we only eliminate the partial redundancy
133 : if we need to insert in only one predecessor. This avoids almost
134 : completely the code size increase that PRE usually causes.
135 :
136 : For the partial anticipation case, we only perform insertion if it
137 : is partially anticipated in some block, and fully available in all
138 : of the predecessors.
139 :
140 : do_pre_regular_insertion/do_pre_partial_partial_insertion
141 : performs these steps, driven by insert/insert_aux.
142 :
143 : Fourth, we eliminate fully redundant expressions.
144 : This is a simple statement walk that replaces redundant
145 : calculations with the now available values. */
146 :
147 : /* Basic algorithm for Code Hoisting:
148 :
149 : Code hoisting is: Moving value computations up in the control flow
150 : graph to make multiple copies redundant. Typically this is a size
151 : optimization, but there are cases where it also is helpful for speed.
152 :
153 : A simple code hoisting algorithm is implemented that piggy-backs on
154 : the PRE infrastructure. For code hoisting, we have to know ANTIC_OUT
155 : which is effectively ANTIC_IN - AVAIL_OUT. The latter two have to be
156 : computed for PRE, and we can use them to perform a limited version of
157 : code hoisting, too.
158 :
159 : For the purpose of this implementation, a value is hoistable to a basic
160 : block B if the following properties are met:
161 :
162 : 1. The value is in ANTIC_IN(B) -- the value will be computed on all
163 : paths from B to function exit and it can be computed in B);
164 :
165 : 2. The value is not in AVAIL_OUT(B) -- there would be no need to
166 : compute the value again and make it available twice;
167 :
168 : 3. All successors of B are dominated by B -- makes sure that inserting
169 : a computation of the value in B will make the remaining
170 : computations fully redundant;
171 :
172 : 4. At least one successor has the value in AVAIL_OUT -- to avoid
173 : hoisting values up too far;
174 :
175 : 5. There are at least two successors of B -- hoisting in straight
176 : line code is pointless.
177 :
178 : The third condition is not strictly necessary, but it would complicate
179 : the hoisting pass a lot. In fact, I don't know of any code hoisting
180 : algorithm that does not have this requirement. Fortunately, experiments
181 : have show that most candidate hoistable values are in regions that meet
182 : this condition (e.g. diamond-shape regions).
183 :
184 : The forth condition is necessary to avoid hoisting things up too far
185 : away from the uses of the value. Nothing else limits the algorithm
186 : from hoisting everything up as far as ANTIC_IN allows. Experiments
187 : with SPEC and CSiBE have shown that hoisting up too far results in more
188 : spilling, less benefits for code size, and worse benchmark scores.
189 : Fortunately, in practice most of the interesting hoisting opportunities
190 : are caught despite this limitation.
191 :
192 : For hoistable values that meet all conditions, expressions are inserted
193 : to make the calculation of the hoistable value fully redundant. We
194 : perform code hoisting insertions after each round of PRE insertions,
195 : because code hoisting never exposes new PRE opportunities, but PRE can
196 : create new code hoisting opportunities.
197 :
198 : The code hoisting algorithm is implemented in do_hoist_insert, driven
199 : by insert/insert_aux. */
200 :
201 : /* Representations of value numbers:
202 :
203 : Value numbers are represented by a representative SSA_NAME. We
204 : will create fake SSA_NAME's in situations where we need a
205 : representative but do not have one (because it is a complex
206 : expression). In order to facilitate storing the value numbers in
207 : bitmaps, and keep the number of wasted SSA_NAME's down, we also
208 : associate a value_id with each value number, and create full blown
209 : ssa_name's only where we actually need them (IE in operands of
210 : existing expressions).
211 :
212 : Theoretically you could replace all the value_id's with
213 : SSA_NAME_VERSION, but this would allocate a large number of
214 : SSA_NAME's (which are each > 30 bytes) just to get a 4 byte number.
215 : It would also require an additional indirection at each point we
216 : use the value id. */
217 :
218 : /* Representation of expressions on value numbers:
219 :
220 : Expressions consisting of value numbers are represented the same
221 : way as our VN internally represents them, with an additional
222 : "pre_expr" wrapping around them in order to facilitate storing all
223 : of the expressions in the same sets. */
224 :
225 : /* Representation of sets:
226 :
227 : The dataflow sets do not need to be sorted in any particular order
228 : for the majority of their lifetime, are simply represented as two
229 : bitmaps, one that keeps track of values present in the set, and one
230 : that keeps track of expressions present in the set.
231 :
232 : When we need them in topological order, we produce it on demand by
233 : transforming the bitmap into an array and sorting it into topo
234 : order. */
235 :
236 : /* Type of expression, used to know which member of the PRE_EXPR union
237 : is valid. */
238 :
239 : enum pre_expr_kind
240 : {
241 : NAME,
242 : NARY,
243 : REFERENCE,
244 : CONSTANT
245 : };
246 :
247 : union pre_expr_union
248 : {
249 : tree name;
250 : tree constant;
251 : vn_nary_op_t nary;
252 : vn_reference_t reference;
253 : };
254 :
255 : typedef struct pre_expr_d : nofree_ptr_hash <pre_expr_d>
256 : {
257 : enum pre_expr_kind kind;
258 : unsigned int id;
259 : unsigned value_id;
260 : location_t loc;
261 : pre_expr_union u;
262 :
263 : /* hash_table support. */
264 : static inline hashval_t hash (const pre_expr_d *);
265 : static inline int equal (const pre_expr_d *, const pre_expr_d *);
266 : } *pre_expr;
267 :
268 : #define PRE_EXPR_NAME(e) (e)->u.name
269 : #define PRE_EXPR_NARY(e) (e)->u.nary
270 : #define PRE_EXPR_REFERENCE(e) (e)->u.reference
271 : #define PRE_EXPR_CONSTANT(e) (e)->u.constant
272 :
273 : /* Compare E1 and E1 for equality. */
274 :
275 : inline int
276 57431852 : pre_expr_d::equal (const pre_expr_d *e1, const pre_expr_d *e2)
277 : {
278 57431852 : if (e1->kind != e2->kind)
279 : return false;
280 :
281 36011482 : switch (e1->kind)
282 : {
283 4758107 : case CONSTANT:
284 4758107 : return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
285 4758107 : PRE_EXPR_CONSTANT (e2));
286 158002 : case NAME:
287 158002 : return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
288 22503388 : case NARY:
289 22503388 : return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
290 8591985 : case REFERENCE:
291 8591985 : return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
292 8591985 : PRE_EXPR_REFERENCE (e2), true);
293 0 : default:
294 0 : gcc_unreachable ();
295 : }
296 : }
297 :
298 : /* Hash E. */
299 :
300 : inline hashval_t
301 92032594 : pre_expr_d::hash (const pre_expr_d *e)
302 : {
303 92032594 : switch (e->kind)
304 : {
305 7259291 : case CONSTANT:
306 7259291 : return vn_hash_constant_with_type (PRE_EXPR_CONSTANT (e));
307 0 : case NAME:
308 0 : return SSA_NAME_VERSION (PRE_EXPR_NAME (e));
309 56630243 : case NARY:
310 56630243 : return PRE_EXPR_NARY (e)->hashcode;
311 28143060 : case REFERENCE:
312 28143060 : return PRE_EXPR_REFERENCE (e)->hashcode;
313 0 : default:
314 0 : gcc_unreachable ();
315 : }
316 : }
317 :
318 : /* Next global expression id number. */
319 : static unsigned int next_expression_id;
320 :
321 : /* Mapping from expression to id number we can use in bitmap sets. */
322 : static vec<pre_expr> expressions;
323 : static hash_table<pre_expr_d> *expression_to_id;
324 : static vec<unsigned> name_to_id;
325 : static obstack pre_expr_obstack;
326 :
327 : /* Allocate an expression id for EXPR. */
328 :
329 : static inline unsigned int
330 44139626 : alloc_expression_id (pre_expr expr)
331 : {
332 44139626 : struct pre_expr_d **slot;
333 : /* Make sure we won't overflow. */
334 44139626 : gcc_assert (next_expression_id + 1 > next_expression_id);
335 44139626 : expr->id = next_expression_id++;
336 44139626 : expressions.safe_push (expr);
337 44139626 : if (expr->kind == NAME)
338 : {
339 24099092 : unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
340 : /* vec::safe_grow_cleared allocates no headroom. Avoid frequent
341 : re-allocations by using vec::reserve upfront. */
342 24099092 : unsigned old_len = name_to_id.length ();
343 48198184 : name_to_id.reserve (num_ssa_names - old_len);
344 48198184 : name_to_id.quick_grow_cleared (num_ssa_names);
345 24099092 : gcc_assert (name_to_id[version] == 0);
346 24099092 : name_to_id[version] = expr->id;
347 : }
348 : else
349 : {
350 20040534 : slot = expression_to_id->find_slot (expr, INSERT);
351 20040534 : gcc_assert (!*slot);
352 20040534 : *slot = expr;
353 : }
354 44139626 : return next_expression_id - 1;
355 : }
356 :
357 : /* Return the expression id for tree EXPR. */
358 :
359 : static inline unsigned int
360 258900293 : get_expression_id (const pre_expr expr)
361 : {
362 258900293 : return expr->id;
363 : }
364 :
365 : static inline unsigned int
366 79641302 : lookup_expression_id (const pre_expr expr)
367 : {
368 79641302 : struct pre_expr_d **slot;
369 :
370 79641302 : if (expr->kind == NAME)
371 : {
372 53664788 : unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
373 53664788 : if (name_to_id.length () <= version)
374 : return 0;
375 50659790 : return name_to_id[version];
376 : }
377 : else
378 : {
379 25976514 : slot = expression_to_id->find_slot (expr, NO_INSERT);
380 25976514 : if (!slot)
381 : return 0;
382 5935980 : return ((pre_expr)*slot)->id;
383 : }
384 : }
385 :
386 : /* Return the expression that has expression id ID */
387 :
388 : static inline pre_expr
389 657205583 : expression_for_id (unsigned int id)
390 : {
391 1314411166 : return expressions[id];
392 : }
393 :
394 : static object_allocator<pre_expr_d> pre_expr_pool ("pre_expr nodes");
395 :
396 : /* Given an SSA_NAME NAME, get or create a pre_expr to represent it. */
397 :
398 : static pre_expr
399 53664788 : get_or_alloc_expr_for_name (tree name)
400 : {
401 53664788 : struct pre_expr_d expr;
402 53664788 : pre_expr result;
403 53664788 : unsigned int result_id;
404 :
405 53664788 : expr.kind = NAME;
406 53664788 : expr.id = 0;
407 53664788 : PRE_EXPR_NAME (&expr) = name;
408 53664788 : result_id = lookup_expression_id (&expr);
409 53664788 : if (result_id != 0)
410 29565696 : return expression_for_id (result_id);
411 :
412 24099092 : result = pre_expr_pool.allocate ();
413 24099092 : result->kind = NAME;
414 24099092 : result->loc = UNKNOWN_LOCATION;
415 24099092 : result->value_id = VN_INFO (name)->value_id;
416 24099092 : PRE_EXPR_NAME (result) = name;
417 24099092 : alloc_expression_id (result);
418 24099092 : return result;
419 : }
420 :
421 : /* Given an NARY, get or create a pre_expr to represent it. Assign
422 : VALUE_ID to it or allocate a new value-id if it is zero. Record
423 : LOC as the original location of the expression. */
424 :
425 : static pre_expr
426 13706634 : get_or_alloc_expr_for_nary (vn_nary_op_t nary, unsigned value_id,
427 : location_t loc = UNKNOWN_LOCATION)
428 : {
429 13706634 : struct pre_expr_d expr;
430 13706634 : pre_expr result;
431 13706634 : unsigned int result_id;
432 :
433 13706634 : gcc_assert (value_id == 0 || !value_id_constant_p (value_id));
434 13706634 : gcc_assert (nary->opcode != SSA_NAME
435 : && TREE_CODE_CLASS (nary->opcode) != tcc_constant);
436 :
437 13706634 : expr.kind = NARY;
438 13706634 : expr.id = 0;
439 13706634 : nary->hashcode = vn_nary_op_compute_hash (nary);
440 13706634 : PRE_EXPR_NARY (&expr) = nary;
441 13706634 : result_id = lookup_expression_id (&expr);
442 13706634 : if (result_id != 0)
443 973684 : return expression_for_id (result_id);
444 :
445 12732950 : result = pre_expr_pool.allocate ();
446 12732950 : result->kind = NARY;
447 12732950 : result->loc = loc;
448 12732950 : result->value_id = value_id ? value_id : get_next_value_id ();
449 12732950 : PRE_EXPR_NARY (result)
450 12732950 : = alloc_vn_nary_op_noinit (nary->length, &pre_expr_obstack);
451 12732950 : memcpy (PRE_EXPR_NARY (result), nary, sizeof_vn_nary_op (nary->length));
452 12732950 : alloc_expression_id (result);
453 12732950 : return result;
454 : }
455 :
456 : /* Given an REFERENCE, get or create a pre_expr to represent it. Assign
457 : VALUE_ID to it or allocate a new value-id if it is zero. Record
458 : LOC as the original location of the expression. If MOVE_OPERANDS
459 : is true then ownership of REFERENCE->operands is transferred, otherwise
460 : a copy is made if necessary. */
461 :
462 : static pre_expr
463 7210939 : get_or_alloc_expr_for_reference (vn_reference_t reference,
464 : unsigned value_id,
465 : location_t loc = UNKNOWN_LOCATION,
466 : bool move_operands = false)
467 : {
468 7210939 : struct pre_expr_d expr;
469 7210939 : pre_expr result;
470 7210939 : unsigned int result_id;
471 :
472 7210939 : expr.kind = REFERENCE;
473 7210939 : expr.id = 0;
474 7210939 : PRE_EXPR_REFERENCE (&expr) = reference;
475 7210939 : result_id = lookup_expression_id (&expr);
476 7210939 : if (result_id != 0)
477 : {
478 752639 : if (move_operands)
479 740275 : reference->operands.release ();
480 752639 : return expression_for_id (result_id);
481 : }
482 :
483 6458300 : result = pre_expr_pool.allocate ();
484 6458300 : result->kind = REFERENCE;
485 6458300 : result->loc = loc;
486 6458300 : result->value_id = value_id ? value_id : get_next_value_id ();
487 6458300 : vn_reference_t ref = XOBNEW (&pre_expr_obstack, struct vn_reference_s);
488 6458300 : *ref = *reference;
489 6458300 : if (!move_operands)
490 453310 : ref->operands = ref->operands.copy ();
491 6458300 : PRE_EXPR_REFERENCE (result) = ref;
492 6458300 : alloc_expression_id (result);
493 6458300 : return result;
494 : }
495 :
496 :
497 : /* An unordered bitmap set. One bitmap tracks values, the other,
498 : expressions. */
499 148038587 : typedef class bitmap_set
500 : {
501 : public:
502 : bitmap_head expressions;
503 : bitmap_head values;
504 : } *bitmap_set_t;
505 :
506 : #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
507 : EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
508 :
509 : #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
510 : EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
511 :
512 : /* Mapping from value id to expressions with that value_id. */
513 : static vec<bitmap> value_expressions;
514 : /* We just record a single expression for each constant value,
515 : one of kind CONSTANT. */
516 : static vec<pre_expr> constant_value_expressions;
517 :
518 :
519 : /* This structure is used to keep track of statistics on what
520 : optimization PRE was able to perform. */
521 : static struct
522 : {
523 : /* The number of new expressions/temporaries generated by PRE. */
524 : int insertions;
525 :
526 : /* The number of inserts found due to partial anticipation */
527 : int pa_insert;
528 :
529 : /* The number of inserts made for code hoisting. */
530 : int hoist_insert;
531 :
532 : /* The number of new PHI nodes added by PRE. */
533 : int phis;
534 : } pre_stats;
535 :
536 : static bool do_partial_partial;
537 : static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
538 : static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
539 : static bool bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
540 : static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
541 : static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
542 : static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
543 : static bitmap_set_t bitmap_set_new (void);
544 : static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
545 : tree);
546 : static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
547 : static unsigned int get_expr_value_id (pre_expr);
548 :
549 : /* We can add and remove elements and entries to and from sets
550 : and hash tables, so we use alloc pools for them. */
551 :
552 : static object_allocator<bitmap_set> bitmap_set_pool ("Bitmap sets");
553 : static bitmap_obstack grand_bitmap_obstack;
554 :
555 : /* A three tuple {e, pred, v} used to cache phi translations in the
556 : phi_translate_table. */
557 :
558 : typedef struct expr_pred_trans_d : public typed_noop_remove <expr_pred_trans_d>
559 : {
560 : typedef expr_pred_trans_d value_type;
561 : typedef expr_pred_trans_d compare_type;
562 :
563 : /* The expression ID. */
564 : unsigned e;
565 :
566 : /* The value expression ID that resulted from the translation. */
567 : unsigned v;
568 :
569 : /* hash_table support. */
570 : static inline void mark_empty (expr_pred_trans_d &);
571 : static inline bool is_empty (const expr_pred_trans_d &);
572 : static inline void mark_deleted (expr_pred_trans_d &);
573 : static inline bool is_deleted (const expr_pred_trans_d &);
574 : static const bool empty_zero_p = true;
575 : static inline hashval_t hash (const expr_pred_trans_d &);
576 : static inline int equal (const expr_pred_trans_d &, const expr_pred_trans_d &);
577 : } *expr_pred_trans_t;
578 : typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
579 :
580 : inline bool
581 1394032406 : expr_pred_trans_d::is_empty (const expr_pred_trans_d &e)
582 : {
583 1394032406 : return e.e == 0;
584 : }
585 :
586 : inline bool
587 279322111 : expr_pred_trans_d::is_deleted (const expr_pred_trans_d &e)
588 : {
589 279322111 : return e.e == -1u;
590 : }
591 :
592 : inline void
593 2258436 : expr_pred_trans_d::mark_empty (expr_pred_trans_d &e)
594 : {
595 2258436 : e.e = 0;
596 : }
597 :
598 : inline void
599 3854282 : expr_pred_trans_d::mark_deleted (expr_pred_trans_d &e)
600 : {
601 3854282 : e.e = -1u;
602 : }
603 :
604 : inline hashval_t
605 : expr_pred_trans_d::hash (const expr_pred_trans_d &e)
606 : {
607 : return e.e;
608 : }
609 :
610 : inline int
611 218723679 : expr_pred_trans_d::equal (const expr_pred_trans_d &ve1,
612 : const expr_pred_trans_d &ve2)
613 : {
614 218723679 : return ve1.e == ve2.e;
615 : }
616 :
617 : /* Sets that we need to keep track of. */
618 : typedef struct bb_bitmap_sets
619 : {
620 : /* The EXP_GEN set, which represents expressions/values generated in
621 : a basic block. */
622 : bitmap_set_t exp_gen;
623 :
624 : /* The PHI_GEN set, which represents PHI results generated in a
625 : basic block. */
626 : bitmap_set_t phi_gen;
627 :
628 : /* The TMP_GEN set, which represents results/temporaries generated
629 : in a basic block. IE the LHS of an expression. */
630 : bitmap_set_t tmp_gen;
631 :
632 : /* The AVAIL_OUT set, which represents which values are available in
633 : a given basic block. */
634 : bitmap_set_t avail_out;
635 :
636 : /* The ANTIC_IN set, which represents which values are anticipatable
637 : in a given basic block. */
638 : bitmap_set_t antic_in;
639 :
640 : /* The PA_IN set, which represents which values are
641 : partially anticipatable in a given basic block. */
642 : bitmap_set_t pa_in;
643 :
644 : /* The NEW_SETS set, which is used during insertion to augment the
645 : AVAIL_OUT set of blocks with the new insertions performed during
646 : the current iteration. */
647 : bitmap_set_t new_sets;
648 :
649 : /* A cache for value_dies_in_block_x. */
650 : bitmap expr_dies;
651 :
652 : /* The live virtual operand on successor edges. */
653 : tree vop_on_exit;
654 :
655 : /* PHI translate cache for the single successor edge. */
656 : hash_table<expr_pred_trans_d> *phi_translate_table;
657 :
658 : /* True if we have visited this block during ANTIC calculation. */
659 : unsigned int visited : 1;
660 :
661 : /* True when the block contains a call that might not return. */
662 : unsigned int contains_may_not_return_call : 1;
663 : } *bb_value_sets_t;
664 :
665 : #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
666 : #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
667 : #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
668 : #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
669 : #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
670 : #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
671 : #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
672 : #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
673 : #define PHI_TRANS_TABLE(BB) ((bb_value_sets_t) ((BB)->aux))->phi_translate_table
674 : #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
675 : #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
676 : #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
677 :
678 :
679 : /* Add the tuple mapping from {expression E, basic block PRED} to
680 : the phi translation table and return whether it pre-existed. */
681 :
682 : static inline bool
683 86370016 : phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
684 : {
685 86370016 : if (!PHI_TRANS_TABLE (pred))
686 197437 : PHI_TRANS_TABLE (pred) = new hash_table<expr_pred_trans_d> (11);
687 :
688 86370016 : expr_pred_trans_t slot;
689 86370016 : expr_pred_trans_d tem;
690 86370016 : unsigned id = get_expression_id (e);
691 86370016 : tem.e = id;
692 86370016 : slot = PHI_TRANS_TABLE (pred)->find_slot_with_hash (tem, id, INSERT);
693 86370016 : if (slot->e)
694 : {
695 62783822 : *entry = slot;
696 62783822 : return true;
697 : }
698 :
699 23586194 : *entry = slot;
700 23586194 : slot->e = id;
701 23586194 : return false;
702 : }
703 :
704 :
705 : /* Add expression E to the expression set of value id V. */
706 :
707 : static void
708 45865949 : add_to_value (unsigned int v, pre_expr e)
709 : {
710 0 : gcc_checking_assert (get_expr_value_id (e) == v);
711 :
712 45865949 : if (value_id_constant_p (v))
713 : {
714 895590 : if (e->kind != CONSTANT)
715 : return;
716 :
717 849284 : if (-v >= constant_value_expressions.length ())
718 506309 : constant_value_expressions.safe_grow_cleared (-v + 1);
719 :
720 849284 : pre_expr leader = constant_value_expressions[-v];
721 849284 : if (!leader)
722 849284 : constant_value_expressions[-v] = e;
723 : }
724 : else
725 : {
726 44970359 : if (v >= value_expressions.length ())
727 7046625 : value_expressions.safe_grow_cleared (v + 1);
728 :
729 44970359 : bitmap set = value_expressions[v];
730 44970359 : if (!set)
731 : {
732 25145969 : set = BITMAP_ALLOC (&grand_bitmap_obstack);
733 25145969 : value_expressions[v] = set;
734 : }
735 44970359 : bitmap_set_bit (set, get_expression_id (e));
736 : }
737 : }
738 :
739 : /* Create a new bitmap set and return it. */
740 :
741 : static bitmap_set_t
742 148038587 : bitmap_set_new (void)
743 : {
744 148038587 : bitmap_set_t ret = bitmap_set_pool.allocate ();
745 148038587 : bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
746 148038587 : bitmap_initialize (&ret->values, &grand_bitmap_obstack);
747 148038587 : return ret;
748 : }
749 :
750 : /* Return the value id for a PRE expression EXPR. */
751 :
752 : static unsigned int
753 539636961 : get_expr_value_id (pre_expr expr)
754 : {
755 : /* ??? We cannot assert that expr has a value-id (it can be 0), because
756 : we assign value-ids only to expressions that have a result
757 : in set_hashtable_value_ids. */
758 45865949 : return expr->value_id;
759 : }
760 :
761 : /* Return a VN valnum (SSA name or constant) for the PRE value-id VAL. */
762 :
763 : static tree
764 1264966 : vn_valnum_from_value_id (unsigned int val)
765 : {
766 1264966 : if (value_id_constant_p (val))
767 : {
768 0 : pre_expr vexpr = constant_value_expressions[-val];
769 0 : if (vexpr)
770 0 : return PRE_EXPR_CONSTANT (vexpr);
771 : return NULL_TREE;
772 : }
773 :
774 1264966 : bitmap exprset = value_expressions[val];
775 1264966 : bitmap_iterator bi;
776 1264966 : unsigned int i;
777 1873713 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
778 : {
779 1529516 : pre_expr vexpr = expression_for_id (i);
780 1529516 : if (vexpr->kind == NAME)
781 920769 : return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
782 : }
783 : return NULL_TREE;
784 : }
785 :
786 : /* Insert an expression EXPR into a bitmapped set. */
787 :
788 : static void
789 76003593 : bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
790 : {
791 76003593 : unsigned int val = get_expr_value_id (expr);
792 76003593 : if (! value_id_constant_p (val))
793 : {
794 : /* Note this is the only function causing multiple expressions
795 : for the same value to appear in a set. This is needed for
796 : TMP_GEN, PHI_GEN and NEW_SETs. */
797 73048078 : bitmap_set_bit (&set->values, val);
798 73048078 : bitmap_set_bit (&set->expressions, get_expression_id (expr));
799 : }
800 76003593 : }
801 :
802 : /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
803 :
804 : static void
805 27327328 : bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
806 : {
807 27327328 : bitmap_copy (&dest->expressions, &orig->expressions);
808 27327328 : bitmap_copy (&dest->values, &orig->values);
809 27327328 : }
810 :
811 :
812 : /* Free memory used up by SET. */
813 : static void
814 74082043 : bitmap_set_free (bitmap_set_t set)
815 : {
816 0 : bitmap_clear (&set->expressions);
817 19798949 : bitmap_clear (&set->values);
818 0 : }
819 :
820 :
821 : /* Sort pre_expr after their value-id. */
822 :
823 : static int
824 435151690 : expr_cmp (const void *a_, const void *b_, void *)
825 : {
826 435151690 : pre_expr a = *(pre_expr const *) a_;
827 435151690 : pre_expr b = *(pre_expr const *) b_;
828 435151690 : return a->value_id - b->value_id;
829 : }
830 :
831 : /* Return an expression that is a valid representation to insert for
832 : both A and B reaching expressions. Return NULL if neither works,
833 : in this case all expressions of the value will be elided. */
834 :
835 : static pre_expr
836 263107 : prefer (pre_expr a, pre_expr b)
837 : {
838 263107 : if (a->kind == REFERENCE && b->kind == REFERENCE)
839 : {
840 66412 : auto refa = PRE_EXPR_REFERENCE (a);
841 66412 : auto refb = PRE_EXPR_REFERENCE (b);
842 66412 : auto &oprsa = refa->operands;
843 66412 : auto &oprsb = refb->operands;
844 66412 : pre_expr palias = NULL;
845 66412 : if (refa->set == refb->set
846 63085 : && refa->base_set == refb->base_set)
847 : ;
848 12595 : else if ((refb->set == refa->set
849 3327 : || alias_set_subset_of (refb->set, refa->set))
850 13966 : && (refb->base_set == refa->base_set
851 9935 : || alias_set_subset_of (refb->base_set, refa->base_set)))
852 : palias = a;
853 4658 : else if ((refa->set == refb->set
854 1977 : || alias_set_subset_of (refa->set, refb->set))
855 6508 : && (refa->base_set == refb->base_set
856 3808 : || alias_set_subset_of (refa->base_set, refb->base_set)))
857 : palias = b;
858 : else
859 : /* We have to chose an expression representation that can stand
860 : in for all others - there can be none, in which case we have
861 : to drop this PRE/hoisting opportunity.
862 : ??? Previously we've arranged for alias-set zero being used
863 : as fallback, but we do not really want to allocate a new expression
864 : here unless it proves to be absolutely necessary. */
865 : return NULL;
866 66179 : pre_expr p = palias;
867 132358 : if (oprsa.length () > 1 && oprsb.length () > 1)
868 : {
869 66179 : vn_reference_op_t vroa = &oprsa[oprsa.length () - 2];
870 66179 : vn_reference_op_t vrob = &oprsb[oprsb.length () - 2];
871 66179 : if (vroa->opcode == MEM_REF && vrob->opcode == MEM_REF)
872 : {
873 : /* We have to canonicalize to the more conservative alignment.
874 : gcc.dg/torture/pr65270-?.c.*/
875 66158 : pre_expr palign = NULL;
876 66158 : if (TYPE_ALIGN (vroa->type) < TYPE_ALIGN (vrob->type))
877 : palign = a;
878 65936 : else if (TYPE_ALIGN (vroa->type) > TYPE_ALIGN (vrob->type))
879 : palign = b;
880 471 : if (palign)
881 : {
882 471 : if (p && p != palign)
883 : return NULL;
884 : p = palign;
885 : }
886 : /* We have to canonicalize to the more conservative (smaller)
887 : innermost object access size. gcc.dg/torture/pr110799.c. */
888 65785 : if (TYPE_SIZE (vroa->type) != TYPE_SIZE (vrob->type))
889 : {
890 4846 : pre_expr psize = NULL;
891 4846 : if (!TYPE_SIZE (vroa->type))
892 : psize = a;
893 4846 : else if (!TYPE_SIZE (vrob->type))
894 : psize = b;
895 4846 : else if (TREE_CODE (TYPE_SIZE (vroa->type)) == INTEGER_CST
896 4846 : && TREE_CODE (TYPE_SIZE (vrob->type)) == INTEGER_CST)
897 : {
898 4840 : int cmp = tree_int_cst_compare (TYPE_SIZE (vroa->type),
899 4840 : TYPE_SIZE (vrob->type));
900 4840 : if (cmp < 0)
901 : psize = a;
902 2687 : else if (cmp > 0)
903 : psize = b;
904 : }
905 : /* ??? What about non-constant sizes? */
906 4840 : if (psize)
907 : {
908 4840 : if (p && p != psize)
909 : return NULL;
910 : p = psize;
911 : }
912 : }
913 : }
914 : }
915 : /* Note we cannot leave it undecided because when having
916 : more than two expressions we have to keep doing
917 : pariwise reduction. */
918 61857 : return p ? p : b;
919 : }
920 : /* Always prefer an non-REFERENCE, avoiding the above mess. */
921 196695 : else if (a->kind == REFERENCE)
922 : return b;
923 192394 : else if (b->kind == REFERENCE)
924 : return a;
925 161625 : else if (a->kind == b->kind)
926 : ;
927 : /* And prefer NAME over anything else. */
928 10677 : else if (b->kind == NAME)
929 : return b;
930 8020 : else if (a->kind == NAME)
931 8020 : return a;
932 : return b;
933 : }
934 :
935 : static void
936 : pre_expr_DFS (pre_expr expr, bitmap_set_t set, bitmap exclusions,
937 : bitmap val_visited, vec<pre_expr> &post);
938 :
939 : /* DFS walk leaders of VAL to their operands with leaders in SET, collecting
940 : expressions in SET in postorder into POST. */
941 :
942 : static void
943 200378305 : pre_expr_DFS (unsigned val, bitmap_set_t set, bitmap exclusions,
944 : bitmap val_visited, vec<pre_expr> &post)
945 : {
946 200378305 : unsigned int i;
947 200378305 : bitmap_iterator bi;
948 :
949 : /* Iterate over all leaders and DFS recurse. Borrowed from
950 : bitmap_find_leader. */
951 200378305 : bitmap exprset = value_expressions[val];
952 200378305 : if (!exprset->first->next)
953 : {
954 476002347 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
955 303632091 : if (bitmap_bit_p (&set->expressions, i)
956 303632091 : && !bitmap_bit_p (exclusions, i))
957 172639417 : pre_expr_DFS (expression_for_id (i), set, exclusions,
958 : val_visited, post);
959 172370256 : return;
960 : }
961 :
962 56797065 : EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
963 28789016 : if (!bitmap_bit_p (exclusions, i))
964 28408944 : pre_expr_DFS (expression_for_id (i), set, exclusions,
965 : val_visited, post);
966 : }
967 :
968 : /* DFS walk EXPR to its operands with leaders in SET, collecting
969 : expressions in SET in postorder into POST. */
970 :
971 : static void
972 201048361 : pre_expr_DFS (pre_expr expr, bitmap_set_t set, bitmap exclusions,
973 : bitmap val_visited, vec<pre_expr> &post)
974 : {
975 201048361 : switch (expr->kind)
976 : {
977 96186098 : case NARY:
978 96186098 : {
979 96186098 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
980 260502992 : for (unsigned i = 0; i < nary->length; i++)
981 : {
982 164316894 : if (TREE_CODE (nary->op[i]) != SSA_NAME)
983 47055398 : continue;
984 117261496 : unsigned int op_val_id = VN_INFO (nary->op[i])->value_id;
985 : /* If we already found a leader for the value we've
986 : recursed already. Avoid the costly bitmap_find_leader. */
987 117261496 : if (bitmap_bit_p (&set->values, op_val_id)
988 117261496 : && bitmap_set_bit (val_visited, op_val_id))
989 69392640 : pre_expr_DFS (op_val_id, set, exclusions, val_visited, post);
990 : }
991 : break;
992 : }
993 30212721 : case REFERENCE:
994 30212721 : {
995 30212721 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
996 30212721 : vec<vn_reference_op_s> operands = ref->operands;
997 30212721 : vn_reference_op_t operand;
998 119651798 : for (unsigned i = 0; operands.iterate (i, &operand); i++)
999 : {
1000 89439077 : tree op[3];
1001 89439077 : op[0] = operand->op0;
1002 89439077 : op[1] = operand->op1;
1003 89439077 : op[2] = operand->op2;
1004 357756308 : for (unsigned n = 0; n < 3; ++n)
1005 : {
1006 268317231 : if (!op[n] || TREE_CODE (op[n]) != SSA_NAME)
1007 246607011 : continue;
1008 21710220 : unsigned op_val_id = VN_INFO (op[n])->value_id;
1009 21710220 : if (bitmap_bit_p (&set->values, op_val_id)
1010 21710220 : && bitmap_set_bit (val_visited, op_val_id))
1011 11280493 : pre_expr_DFS (op_val_id, set, exclusions, val_visited, post);
1012 : }
1013 : }
1014 : break;
1015 : }
1016 201048361 : default:;
1017 : }
1018 201048361 : post.quick_push (expr);
1019 201048361 : }
1020 :
1021 : /* Generate an topological-ordered array of bitmap set SET. If FOR_INSERTION
1022 : is true then perform canonexpr on the set. */
1023 :
1024 : static vec<pre_expr>
1025 18615104 : sorted_array_from_bitmap_set (bitmap_set_t set, bool for_insertion)
1026 : {
1027 18615104 : unsigned int i;
1028 18615104 : bitmap_iterator bi;
1029 18615104 : vec<pre_expr> result;
1030 :
1031 : /* Pre-allocate enough space for the array. */
1032 18615104 : unsigned cnt = bitmap_count_bits (&set->expressions);
1033 18615104 : result.create (cnt);
1034 :
1035 : /* For expressions with the same value from EXPRESSIONS retain only
1036 : expressions that can be inserted in place of all others. */
1037 18615104 : auto_bitmap exclusions;
1038 18615104 : bitmap_tree_view (exclusions);
1039 18615104 : if (for_insertion && cnt > 1)
1040 : {
1041 26844036 : EXECUTE_IF_SET_IN_BITMAP (&set->expressions, 0, i, bi)
1042 24178742 : result.safe_push (expression_for_id (i));
1043 2665294 : result.sort (expr_cmp, NULL);
1044 48349868 : for (unsigned i = 0; i < result.length () - 1; ++i)
1045 21509640 : if (result[i]->value_id == result[i+1]->value_id)
1046 : {
1047 263107 : if (pre_expr p = prefer (result[i], result[i+1]))
1048 : {
1049 : /* We retain and iterate on exprs[i+1], if we want to
1050 : retain exprs[i], swap both. */
1051 258552 : if (p == result[i])
1052 44302 : std::swap (result[i], result[i+1]);
1053 258552 : bitmap_set_bit (exclusions, get_expression_id (result[i]));
1054 : }
1055 : else
1056 : {
1057 : /* If neither works for pairwise choosing a conservative
1058 : alternative, drop all REFERENCE expressions for this value.
1059 : REFERENCE are always toplevel, so no chain should be
1060 : interrupted by pruning them. */
1061 : unsigned j, k;
1062 10 : for (j = i;; --j)
1063 4565 : if (j == 0
1064 4565 : || result[j - 1]->value_id != result[i]->value_id)
1065 : break;
1066 : for (k = j;; ++k)
1067 : {
1068 9171 : if (result[k]->kind == REFERENCE)
1069 9171 : bitmap_set_bit (exclusions,
1070 9171 : get_expression_id (result[k]));
1071 9171 : if (k == result.length () - 1
1072 9171 : || result[k + 1]->value_id != result[i]->value_id)
1073 : break;
1074 : }
1075 : i = k;
1076 : }
1077 : }
1078 2665294 : result.truncate (0);
1079 : }
1080 :
1081 18615104 : bool single_p = true;
1082 18615104 : auto_bitmap val_visited (&grand_bitmap_obstack);
1083 18615104 : bitmap_tree_view (val_visited);
1084 105239192 : FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
1085 86624088 : if (bitmap_set_bit (val_visited, i))
1086 : {
1087 76877212 : if (!result.is_empty ())
1088 : {
1089 63956459 : single_p = false;
1090 63956459 : result.truncate (0);
1091 : }
1092 76877212 : pre_expr_DFS (i, set, exclusions, val_visited, result);
1093 : /* Mark i as entry that is not forward reachable. Note we do
1094 : have cycles in the value graph so eventually i reaches itself. */
1095 76877212 : bitmap_clear_bit (val_visited, i);
1096 : }
1097 : /* If we didn't by luck visit only a single entry to the value
1098 : graph visit now all not forward reachable values. */
1099 18615104 : if (!single_p)
1100 : {
1101 9733449 : result.truncate (0);
1102 9733449 : auto_bitmap val_visited2 (&grand_bitmap_obstack);
1103 9733449 : bitmap_tree_view (val_visited2);
1104 92623992 : FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
1105 82890543 : if (!bitmap_bit_p (val_visited, i))
1106 : {
1107 42827960 : if (bitmap_set_bit (val_visited2, i))
1108 42827960 : pre_expr_DFS (i, set, exclusions, val_visited2, result);
1109 : else
1110 0 : gcc_unreachable ();
1111 : }
1112 9733449 : if (flag_checking)
1113 : {
1114 9733398 : bitmap_list_view (val_visited2);
1115 9733398 : gcc_assert (bitmap_equal_p (&set->values, val_visited2));
1116 : }
1117 9733449 : }
1118 :
1119 18615104 : return result;
1120 18615104 : }
1121 :
1122 : /* Subtract all expressions contained in ORIG from DEST. */
1123 :
1124 : static bitmap_set_t
1125 32955881 : bitmap_set_subtract_expressions (bitmap_set_t dest, bitmap_set_t orig,
1126 : bool copy_values = false)
1127 : {
1128 32955881 : bitmap_set_t result = bitmap_set_new ();
1129 32955881 : bitmap_iterator bi;
1130 32955881 : unsigned int i;
1131 :
1132 32955881 : bitmap_and_compl (&result->expressions, &dest->expressions,
1133 32955881 : &orig->expressions);
1134 :
1135 32955881 : if (copy_values)
1136 656207 : bitmap_copy (&result->values, &dest->values);
1137 : else
1138 111950192 : FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
1139 : {
1140 79650518 : pre_expr expr = expression_for_id (i);
1141 79650518 : unsigned int value_id = get_expr_value_id (expr);
1142 79650518 : bitmap_set_bit (&result->values, value_id);
1143 : }
1144 :
1145 32955881 : return result;
1146 : }
1147 :
1148 : /* Subtract all values in bitmap set B from bitmap set A. */
1149 :
1150 : static void
1151 1233069 : bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
1152 : {
1153 1233069 : unsigned int i;
1154 1233069 : bitmap_iterator bi;
1155 1233069 : unsigned to_remove = -1U;
1156 1233069 : bitmap_and_compl_into (&a->values, &b->values);
1157 11613170 : FOR_EACH_EXPR_ID_IN_SET (a, i, bi)
1158 : {
1159 10380101 : if (to_remove != -1U)
1160 : {
1161 1447538 : bitmap_clear_bit (&a->expressions, to_remove);
1162 1447538 : to_remove = -1U;
1163 : }
1164 10380101 : pre_expr expr = expression_for_id (i);
1165 10380101 : if (! bitmap_bit_p (&a->values, get_expr_value_id (expr)))
1166 1503665 : to_remove = i;
1167 : }
1168 1233069 : if (to_remove != -1U)
1169 56127 : bitmap_clear_bit (&a->expressions, to_remove);
1170 1233069 : }
1171 :
1172 :
1173 : /* Return true if bitmapped set SET contains the value VALUE_ID. */
1174 :
1175 : static bool
1176 202478110 : bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
1177 : {
1178 0 : if (value_id_constant_p (value_id))
1179 : return true;
1180 :
1181 99581172 : return bitmap_bit_p (&set->values, value_id);
1182 : }
1183 :
1184 : /* Return true if two bitmap sets are equal. */
1185 :
1186 : static bool
1187 15861406 : bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
1188 : {
1189 0 : return bitmap_equal_p (&a->values, &b->values);
1190 : }
1191 :
1192 : /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
1193 : and add it otherwise. Return true if any changes were made. */
1194 :
1195 : static bool
1196 33630721 : bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
1197 : {
1198 33630721 : unsigned int val = get_expr_value_id (expr);
1199 33630721 : if (value_id_constant_p (val))
1200 : return false;
1201 :
1202 33630721 : if (bitmap_set_contains_value (set, val))
1203 : {
1204 : /* The number of expressions having a given value is usually
1205 : significantly less than the total number of expressions in SET.
1206 : Thus, rather than check, for each expression in SET, whether it
1207 : has the value LOOKFOR, we walk the reverse mapping that tells us
1208 : what expressions have a given value, and see if any of those
1209 : expressions are in our set. For large testcases, this is about
1210 : 5-10x faster than walking the bitmap. If this is somehow a
1211 : significant lose for some cases, we can choose which set to walk
1212 : based on the set size. */
1213 14340617 : unsigned int i;
1214 14340617 : bitmap_iterator bi;
1215 14340617 : bitmap exprset = value_expressions[val];
1216 16594945 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1217 : {
1218 16594945 : if (bitmap_clear_bit (&set->expressions, i))
1219 : {
1220 14340617 : bitmap_set_bit (&set->expressions, get_expression_id (expr));
1221 14340617 : return i != get_expression_id (expr);
1222 : }
1223 : }
1224 0 : gcc_unreachable ();
1225 : }
1226 :
1227 19290104 : bitmap_insert_into_set (set, expr);
1228 19290104 : return true;
1229 : }
1230 :
1231 : /* Insert EXPR into SET if EXPR's value is not already present in
1232 : SET. */
1233 :
1234 : static void
1235 63965019 : bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
1236 : {
1237 63965019 : unsigned int val = get_expr_value_id (expr);
1238 :
1239 63965019 : gcc_checking_assert (expr->id == get_expression_id (expr));
1240 :
1241 : /* Constant values are always considered to be part of the set. */
1242 63965019 : if (value_id_constant_p (val))
1243 : return;
1244 :
1245 : /* If the value membership changed, add the expression. */
1246 63904929 : if (bitmap_set_bit (&set->values, val))
1247 49421607 : bitmap_set_bit (&set->expressions, expr->id);
1248 : }
1249 :
1250 : /* Print out EXPR to outfile. */
1251 :
1252 : static void
1253 4573 : print_pre_expr (FILE *outfile, const pre_expr expr)
1254 : {
1255 4573 : if (! expr)
1256 : {
1257 0 : fprintf (outfile, "NULL");
1258 0 : return;
1259 : }
1260 4573 : switch (expr->kind)
1261 : {
1262 0 : case CONSTANT:
1263 0 : print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr));
1264 0 : break;
1265 3214 : case NAME:
1266 3214 : print_generic_expr (outfile, PRE_EXPR_NAME (expr));
1267 3214 : break;
1268 1072 : case NARY:
1269 1072 : {
1270 1072 : unsigned int i;
1271 1072 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1272 1072 : fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
1273 4103 : for (i = 0; i < nary->length; i++)
1274 : {
1275 1959 : print_generic_expr (outfile, nary->op[i]);
1276 1959 : if (i != (unsigned) nary->length - 1)
1277 887 : fprintf (outfile, ",");
1278 : }
1279 1072 : fprintf (outfile, "}");
1280 : }
1281 1072 : break;
1282 :
1283 287 : case REFERENCE:
1284 287 : {
1285 287 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1286 287 : print_vn_reference_ops (outfile, ref->operands);
1287 287 : if (ref->vuse)
1288 : {
1289 275 : fprintf (outfile, "@");
1290 275 : print_generic_expr (outfile, ref->vuse);
1291 : }
1292 : }
1293 : break;
1294 : }
1295 : }
1296 : void debug_pre_expr (pre_expr);
1297 :
1298 : /* Like print_pre_expr but always prints to stderr. */
1299 : DEBUG_FUNCTION void
1300 0 : debug_pre_expr (pre_expr e)
1301 : {
1302 0 : print_pre_expr (stderr, e);
1303 0 : fprintf (stderr, "\n");
1304 0 : }
1305 :
1306 : /* Print out SET to OUTFILE. */
1307 :
1308 : static void
1309 913 : print_bitmap_set (FILE *outfile, bitmap_set_t set,
1310 : const char *setname, int blockindex)
1311 : {
1312 913 : fprintf (outfile, "%s[%d] := { ", setname, blockindex);
1313 913 : if (set)
1314 : {
1315 913 : bool first = true;
1316 913 : unsigned i;
1317 913 : bitmap_iterator bi;
1318 :
1319 5345 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1320 : {
1321 4432 : const pre_expr expr = expression_for_id (i);
1322 :
1323 4432 : if (!first)
1324 3806 : fprintf (outfile, ", ");
1325 4432 : first = false;
1326 4432 : print_pre_expr (outfile, expr);
1327 :
1328 4432 : fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1329 : }
1330 : }
1331 913 : fprintf (outfile, " }\n");
1332 913 : }
1333 :
1334 : void debug_bitmap_set (bitmap_set_t);
1335 :
1336 : DEBUG_FUNCTION void
1337 0 : debug_bitmap_set (bitmap_set_t set)
1338 : {
1339 0 : print_bitmap_set (stderr, set, "debug", 0);
1340 0 : }
1341 :
1342 : void debug_bitmap_sets_for (basic_block);
1343 :
1344 : DEBUG_FUNCTION void
1345 0 : debug_bitmap_sets_for (basic_block bb)
1346 : {
1347 0 : print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1348 0 : print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1349 0 : print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1350 0 : print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1351 0 : print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1352 0 : if (do_partial_partial)
1353 0 : print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1354 0 : print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1355 0 : }
1356 :
1357 : /* Print out the expressions that have VAL to OUTFILE. */
1358 :
1359 : static void
1360 0 : print_value_expressions (FILE *outfile, unsigned int val)
1361 : {
1362 0 : bitmap set = value_expressions[val];
1363 0 : if (set)
1364 : {
1365 0 : bitmap_set x;
1366 0 : char s[10];
1367 0 : sprintf (s, "%04d", val);
1368 0 : x.expressions = *set;
1369 0 : print_bitmap_set (outfile, &x, s, 0);
1370 : }
1371 0 : }
1372 :
1373 :
1374 : DEBUG_FUNCTION void
1375 0 : debug_value_expressions (unsigned int val)
1376 : {
1377 0 : print_value_expressions (stderr, val);
1378 0 : }
1379 :
1380 : /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1381 : represent it. */
1382 :
1383 : static pre_expr
1384 5058941 : get_or_alloc_expr_for_constant (tree constant)
1385 : {
1386 5058941 : unsigned int result_id;
1387 5058941 : struct pre_expr_d expr;
1388 5058941 : pre_expr newexpr;
1389 :
1390 5058941 : expr.kind = CONSTANT;
1391 5058941 : PRE_EXPR_CONSTANT (&expr) = constant;
1392 5058941 : result_id = lookup_expression_id (&expr);
1393 5058941 : if (result_id != 0)
1394 4209657 : return expression_for_id (result_id);
1395 :
1396 849284 : newexpr = pre_expr_pool.allocate ();
1397 849284 : newexpr->kind = CONSTANT;
1398 849284 : newexpr->loc = UNKNOWN_LOCATION;
1399 849284 : PRE_EXPR_CONSTANT (newexpr) = constant;
1400 849284 : alloc_expression_id (newexpr);
1401 849284 : newexpr->value_id = get_or_alloc_constant_value_id (constant);
1402 849284 : add_to_value (newexpr->value_id, newexpr);
1403 849284 : return newexpr;
1404 : }
1405 :
1406 : /* Translate the VUSE backwards through phi nodes in E->dest, so that
1407 : it has the value it would have in E->src. Set *SAME_VALID to true
1408 : in case the new vuse doesn't change the value id of the OPERANDS. */
1409 :
1410 : static tree
1411 4542847 : translate_vuse_through_block (vec<vn_reference_op_s> operands,
1412 : alias_set_type set, alias_set_type base_set,
1413 : tree type, tree vuse, edge e, bool *same_valid)
1414 : {
1415 4542847 : basic_block phiblock = e->dest;
1416 4542847 : gimple *def = SSA_NAME_DEF_STMT (vuse);
1417 4542847 : ao_ref ref;
1418 :
1419 4542847 : if (same_valid)
1420 3279508 : *same_valid = true;
1421 :
1422 : /* If value-numbering provided a memory state for this
1423 : that dominates PHIBLOCK we can just use that. */
1424 4542847 : if (gimple_nop_p (def)
1425 4542847 : || (gimple_bb (def) != phiblock
1426 1193016 : && dominated_by_p (CDI_DOMINATORS, phiblock, gimple_bb (def))))
1427 : return vuse;
1428 :
1429 : /* We have pruned expressions that are killed in PHIBLOCK via
1430 : prune_clobbered_mems but we have not rewritten the VUSE to the one
1431 : live at the start of the block. If there is no virtual PHI to translate
1432 : through return the VUSE live at entry. Otherwise the VUSE to translate
1433 : is the def of the virtual PHI node. */
1434 2647704 : gphi *phi = get_virtual_phi (phiblock);
1435 2647704 : if (!phi)
1436 93740 : return BB_LIVE_VOP_ON_EXIT
1437 : (get_immediate_dominator (CDI_DOMINATORS, phiblock));
1438 :
1439 2553964 : if (same_valid
1440 2553964 : && ao_ref_init_from_vn_reference (&ref, set, base_set, type, operands))
1441 : {
1442 1872329 : bitmap visited = NULL;
1443 : /* Try to find a vuse that dominates this phi node by skipping
1444 : non-clobbering statements. */
1445 1872329 : unsigned int cnt = param_sccvn_max_alias_queries_per_access;
1446 1872329 : vuse = get_continuation_for_phi (phi, &ref, true,
1447 : cnt, &visited, false, NULL, NULL);
1448 1872329 : if (visited)
1449 1861167 : BITMAP_FREE (visited);
1450 : }
1451 : else
1452 : vuse = NULL_TREE;
1453 : /* If we didn't find any, the value ID can't stay the same. */
1454 2553964 : if (!vuse && same_valid)
1455 1617643 : *same_valid = false;
1456 :
1457 : /* ??? We would like to return vuse here as this is the canonical
1458 : upmost vdef that this reference is associated with. But during
1459 : insertion of the references into the hash tables we only ever
1460 : directly insert with their direct gimple_vuse, hence returning
1461 : something else would make us not find the other expression. */
1462 2553964 : return PHI_ARG_DEF (phi, e->dest_idx);
1463 : }
1464 :
1465 : /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1466 : SET2 *or* SET3. This is used to avoid making a set consisting of the union
1467 : of PA_IN and ANTIC_IN during insert and phi-translation. */
1468 :
1469 : static inline pre_expr
1470 24486959 : find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
1471 : bitmap_set_t set3 = NULL)
1472 : {
1473 24486959 : pre_expr result = NULL;
1474 :
1475 24486959 : if (set1)
1476 24357784 : result = bitmap_find_leader (set1, val);
1477 24486959 : if (!result && set2)
1478 1597199 : result = bitmap_find_leader (set2, val);
1479 24486959 : if (!result && set3)
1480 0 : result = bitmap_find_leader (set3, val);
1481 24486959 : return result;
1482 : }
1483 :
1484 : /* Get the tree type for our PRE expression e. */
1485 :
1486 : static tree
1487 7438914 : get_expr_type (const pre_expr e)
1488 : {
1489 7438914 : switch (e->kind)
1490 : {
1491 1003613 : case NAME:
1492 1003613 : return TREE_TYPE (PRE_EXPR_NAME (e));
1493 186055 : case CONSTANT:
1494 186055 : return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1495 1358499 : case REFERENCE:
1496 1358499 : return PRE_EXPR_REFERENCE (e)->type;
1497 4890747 : case NARY:
1498 4890747 : return PRE_EXPR_NARY (e)->type;
1499 : }
1500 0 : gcc_unreachable ();
1501 : }
1502 :
1503 : /* Get a representative SSA_NAME for a given expression that is available in B.
1504 : Since all of our sub-expressions are treated as values, we require
1505 : them to be SSA_NAME's for simplicity.
1506 : Prior versions of GVNPRE used to use "value handles" here, so that
1507 : an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1508 : either case, the operands are really values (IE we do not expect
1509 : them to be usable without finding leaders). */
1510 :
1511 : static tree
1512 19538645 : get_representative_for (const pre_expr e, basic_block b = NULL)
1513 : {
1514 19538645 : tree name, valnum = NULL_TREE;
1515 19538645 : unsigned int value_id = get_expr_value_id (e);
1516 :
1517 19538645 : switch (e->kind)
1518 : {
1519 9008978 : case NAME:
1520 9008978 : return PRE_EXPR_NAME (e);
1521 1909536 : case CONSTANT:
1522 1909536 : return PRE_EXPR_CONSTANT (e);
1523 8620131 : case NARY:
1524 8620131 : case REFERENCE:
1525 8620131 : {
1526 : /* Go through all of the expressions representing this value
1527 : and pick out an SSA_NAME. */
1528 8620131 : unsigned int i;
1529 8620131 : bitmap_iterator bi;
1530 8620131 : bitmap exprs = value_expressions[value_id];
1531 22323181 : EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1532 : {
1533 18264781 : pre_expr rep = expression_for_id (i);
1534 18264781 : if (rep->kind == NAME)
1535 : {
1536 8308081 : tree name = PRE_EXPR_NAME (rep);
1537 8308081 : valnum = VN_INFO (name)->valnum;
1538 8308081 : gimple *def = SSA_NAME_DEF_STMT (name);
1539 : /* We have to return either a new representative or one
1540 : that can be used for expression simplification and thus
1541 : is available in B. */
1542 8308081 : if (! b
1543 7984562 : || gimple_nop_p (def)
1544 12310801 : || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
1545 4561731 : return name;
1546 : }
1547 9956700 : else if (rep->kind == CONSTANT)
1548 0 : return PRE_EXPR_CONSTANT (rep);
1549 : }
1550 : }
1551 4058400 : break;
1552 : }
1553 :
1554 : /* If we reached here we couldn't find an SSA_NAME. This can
1555 : happen when we've discovered a value that has never appeared in
1556 : the program as set to an SSA_NAME, as the result of phi translation.
1557 : Create one here.
1558 : ??? We should be able to re-use this when we insert the statement
1559 : to compute it. */
1560 4058400 : name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1561 4058400 : vn_ssa_aux_t vn_info = VN_INFO (name);
1562 4058400 : vn_info->value_id = value_id;
1563 4058400 : vn_info->valnum = valnum ? valnum : name;
1564 4058400 : vn_info->visited = true;
1565 : /* ??? For now mark this SSA name for release by VN. */
1566 4058400 : vn_info->needs_insertion = true;
1567 4058400 : add_to_value (value_id, get_or_alloc_expr_for_name (name));
1568 4058400 : if (dump_file && (dump_flags & TDF_DETAILS))
1569 : {
1570 47 : fprintf (dump_file, "Created SSA_NAME representative ");
1571 47 : print_generic_expr (dump_file, name);
1572 47 : fprintf (dump_file, " for expression:");
1573 47 : print_pre_expr (dump_file, e);
1574 47 : fprintf (dump_file, " (%04d)\n", value_id);
1575 : }
1576 :
1577 : return name;
1578 : }
1579 :
1580 :
1581 : static pre_expr
1582 : phi_translate (bitmap_set_t, pre_expr, bitmap_set_t, bitmap_set_t, edge);
1583 :
1584 : /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1585 : the phis in PRED. Return NULL if we can't find a leader for each part
1586 : of the translated expression. */
1587 :
1588 : static pre_expr
1589 49389263 : phi_translate_1 (bitmap_set_t dest,
1590 : pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
1591 : {
1592 49389263 : basic_block pred = e->src;
1593 49389263 : basic_block phiblock = e->dest;
1594 49389263 : location_t expr_loc = expr->loc;
1595 49389263 : switch (expr->kind)
1596 : {
1597 18550851 : case NARY:
1598 18550851 : {
1599 18550851 : unsigned int i;
1600 18550851 : bool changed = false;
1601 18550851 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1602 18550851 : vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1603 : sizeof_vn_nary_op (nary->length));
1604 18550851 : memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1605 :
1606 43959095 : for (i = 0; i < newnary->length; i++)
1607 : {
1608 28884827 : if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1609 9013541 : continue;
1610 : else
1611 : {
1612 19871286 : pre_expr leader, result;
1613 19871286 : unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1614 19871286 : leader = find_leader_in_sets (op_val_id, set1, set2);
1615 19871286 : result = phi_translate (dest, leader, set1, set2, e);
1616 19871286 : if (result)
1617 : /* If op has a leader in the sets we translate make
1618 : sure to use the value of the translated expression.
1619 : We might need a new representative for that. */
1620 16394703 : newnary->op[i] = get_representative_for (result, pred);
1621 : else if (!result)
1622 : return NULL;
1623 :
1624 16394703 : changed |= newnary->op[i] != nary->op[i];
1625 : }
1626 : }
1627 15074268 : if (changed)
1628 : {
1629 7564131 : unsigned int new_val_id;
1630 :
1631 7564131 : vn_nary_op_t saved_newnary
1632 7564131 : = XALLOCAVAR (struct vn_nary_op_s,
1633 : sizeof_vn_nary_op (newnary->length));
1634 7564131 : memcpy (saved_newnary, newnary,
1635 : sizeof_vn_nary_op (newnary->length));
1636 :
1637 : /* Try to simplify the new NARY. */
1638 7564131 : tree res = vn_nary_simplify (newnary);
1639 7564131 : if (res)
1640 : {
1641 2423599 : if (is_gimple_min_invariant (res))
1642 1246765 : return get_or_alloc_expr_for_constant (res);
1643 :
1644 : /* For non-CONSTANTs we have to make sure we can eventually
1645 : insert the expression. Which means we need to have a
1646 : leader for it. */
1647 1176834 : gcc_assert (TREE_CODE (res) == SSA_NAME);
1648 :
1649 : /* Do not allow simplifications to non-constants over
1650 : backedges as this will likely result in a loop PHI node
1651 : to be inserted and increased register pressure.
1652 : See PR77498 - this avoids doing predcoms work in
1653 : a less efficient way. */
1654 1176834 : if (e->flags & EDGE_DFS_BACK)
1655 : ;
1656 : else
1657 : {
1658 1095030 : unsigned value_id = VN_INFO (res)->value_id;
1659 : /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1660 : dest has what we computed into ANTIC_OUT sofar
1661 : so pick from that - since topological sorting
1662 : by sorted_array_from_bitmap_set isn't perfect
1663 : we may lose some cases here. */
1664 2190060 : pre_expr constant = find_leader_in_sets (value_id, dest,
1665 1095030 : AVAIL_OUT (pred));
1666 1095030 : if (constant)
1667 : {
1668 336109 : if (dump_file && (dump_flags & TDF_DETAILS))
1669 : {
1670 7 : fprintf (dump_file, "simplifying ");
1671 7 : print_pre_expr (dump_file, expr);
1672 7 : fprintf (dump_file, " translated %d -> %d to ",
1673 : phiblock->index, pred->index);
1674 7 : PRE_EXPR_NARY (expr) = newnary;
1675 7 : print_pre_expr (dump_file, expr);
1676 7 : PRE_EXPR_NARY (expr) = nary;
1677 7 : fprintf (dump_file, " to ");
1678 7 : print_pre_expr (dump_file, constant);
1679 7 : fprintf (dump_file, "\n");
1680 : }
1681 : return constant;
1682 : }
1683 : }
1684 : /* Restore the unsimplified newnary, it was simplified
1685 : to a NAME that we do not want (not as NARY anyway). */
1686 840725 : memcpy (newnary, saved_newnary,
1687 840725 : sizeof_vn_nary_op (saved_newnary->length));
1688 : }
1689 :
1690 11962514 : tree result = vn_nary_op_lookup_pieces (newnary->length,
1691 5981257 : newnary->opcode,
1692 : newnary->type,
1693 : &newnary->op[0],
1694 : &nary);
1695 5981257 : if (result && is_gimple_min_invariant (result))
1696 0 : return get_or_alloc_expr_for_constant (result);
1697 :
1698 5981257 : if (!nary || nary->predicated_values)
1699 : new_val_id = 0;
1700 : else
1701 803105 : new_val_id = nary->value_id;
1702 5981257 : expr = get_or_alloc_expr_for_nary (newnary, new_val_id, expr_loc);
1703 5981257 : add_to_value (get_expr_value_id (expr), expr);
1704 : }
1705 : return expr;
1706 : }
1707 5035343 : break;
1708 :
1709 5035343 : case REFERENCE:
1710 5035343 : {
1711 5035343 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1712 5035343 : vec<vn_reference_op_s> operands = ref->operands;
1713 5035343 : tree vuse = ref->vuse;
1714 5035343 : tree newvuse = vuse;
1715 5035343 : vec<vn_reference_op_s> newoperands = vNULL;
1716 5035343 : bool changed = false, same_valid = true;
1717 5035343 : unsigned int i, n;
1718 5035343 : vn_reference_op_t operand;
1719 5035343 : vn_reference_t newref;
1720 :
1721 19175593 : for (i = 0; operands.iterate (i, &operand); i++)
1722 : {
1723 14516951 : pre_expr opresult;
1724 14516951 : pre_expr leader;
1725 14516951 : tree op[3];
1726 14516951 : tree type = operand->type;
1727 14516951 : vn_reference_op_s newop = *operand;
1728 14516951 : op[0] = operand->op0;
1729 14516951 : op[1] = operand->op1;
1730 14516951 : op[2] = operand->op2;
1731 56937773 : for (n = 0; n < 3; ++n)
1732 : {
1733 42797523 : unsigned int op_val_id;
1734 42797523 : if (!op[n])
1735 25954729 : continue;
1736 16842794 : if (TREE_CODE (op[n]) != SSA_NAME)
1737 : {
1738 : /* We can't possibly insert these. */
1739 13322151 : if (n != 0
1740 13322151 : && !is_gimple_min_invariant (op[n]))
1741 : break;
1742 13322151 : continue;
1743 : }
1744 3520643 : op_val_id = VN_INFO (op[n])->value_id;
1745 3520643 : leader = find_leader_in_sets (op_val_id, set1, set2);
1746 3520643 : opresult = phi_translate (dest, leader, set1, set2, e);
1747 3520643 : if (opresult)
1748 : {
1749 3143942 : tree name = get_representative_for (opresult);
1750 3143942 : changed |= name != op[n];
1751 3143942 : op[n] = name;
1752 : }
1753 : else if (!opresult)
1754 : break;
1755 : }
1756 14516951 : if (n != 3)
1757 : {
1758 376701 : newoperands.release ();
1759 376701 : return NULL;
1760 : }
1761 : /* When we translate a MEM_REF across a backedge and we have
1762 : restrict info that's not from our functions parameters
1763 : we have to remap it since we now may deal with a different
1764 : instance where the dependence info is no longer valid.
1765 : See PR102970. Note instead of keeping a remapping table
1766 : per backedge we simply throw away restrict info. */
1767 14140250 : if ((newop.opcode == MEM_REF
1768 14140250 : || newop.opcode == TARGET_MEM_REF)
1769 4767791 : && newop.clique > 1
1770 162207 : && (e->flags & EDGE_DFS_BACK))
1771 : {
1772 : newop.clique = 0;
1773 : newop.base = 0;
1774 : changed = true;
1775 : }
1776 14121205 : if (!changed)
1777 11622319 : continue;
1778 2517931 : if (!newoperands.exists ())
1779 1295812 : newoperands = operands.copy ();
1780 : /* We may have changed from an SSA_NAME to a constant */
1781 2517931 : if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1782 : newop.opcode = TREE_CODE (op[0]);
1783 2517931 : newop.type = type;
1784 2517931 : newop.op0 = op[0];
1785 2517931 : newop.op1 = op[1];
1786 2517931 : newop.op2 = op[2];
1787 2517931 : newoperands[i] = newop;
1788 : }
1789 9317284 : gcc_checking_assert (i == operands.length ());
1790 :
1791 4658642 : if (vuse)
1792 : {
1793 11101863 : newvuse = translate_vuse_through_block (newoperands.exists ()
1794 4542847 : ? newoperands : operands,
1795 : ref->set, ref->base_set,
1796 : ref->type, vuse, e,
1797 : changed
1798 : ? NULL : &same_valid);
1799 4542847 : if (newvuse == NULL_TREE)
1800 : {
1801 0 : newoperands.release ();
1802 0 : return NULL;
1803 : }
1804 : }
1805 :
1806 4658642 : if (changed || newvuse != vuse)
1807 : {
1808 3272697 : unsigned int new_val_id;
1809 :
1810 5251055 : tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1811 : ref->base_set,
1812 : ref->type,
1813 3272697 : newoperands.exists ()
1814 3272697 : ? newoperands : operands,
1815 : &newref, VN_WALK);
1816 :
1817 : /* We can always insert constants, so if we have a partial
1818 : redundant constant load of another type try to translate it
1819 : to a constant of appropriate type. */
1820 3272697 : if (result && is_gimple_min_invariant (result))
1821 : {
1822 78549 : tree tem = result;
1823 78549 : if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1824 : {
1825 85 : tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1826 85 : if (tem && !is_gimple_min_invariant (tem))
1827 : tem = NULL_TREE;
1828 : }
1829 78549 : if (tem)
1830 : {
1831 78549 : newoperands.release ();
1832 78549 : return get_or_alloc_expr_for_constant (tem);
1833 : }
1834 : }
1835 :
1836 : /* If we'd have to convert things we would need to validate
1837 : if we can insert the translated expression. So fail
1838 : here for now - we cannot insert an alias with a different
1839 : type in the VN tables either, as that would assert. */
1840 3194148 : if (result
1841 3194148 : && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1842 : {
1843 998 : newoperands.release ();
1844 998 : return NULL;
1845 : }
1846 2622235 : else if (!result && newref
1847 3193150 : && !useless_type_conversion_p (ref->type, newref->type))
1848 : {
1849 0 : newoperands.release ();
1850 0 : return NULL;
1851 : }
1852 :
1853 3193150 : if (newref)
1854 : {
1855 570915 : new_val_id = newref->value_id;
1856 570915 : newvuse = newref->vuse;
1857 : }
1858 : else
1859 : {
1860 2622235 : if (changed || !same_valid)
1861 : new_val_id = 0;
1862 : else
1863 127366 : new_val_id = ref->value_id;
1864 : }
1865 3193150 : newref = XALLOCAVAR (struct vn_reference_s,
1866 : sizeof (vn_reference_s));
1867 3193150 : memcpy (newref, ref, sizeof (vn_reference_s));
1868 3193150 : newref->next = NULL;
1869 3193150 : newref->value_id = new_val_id;
1870 3193150 : newref->vuse = newvuse;
1871 6386300 : newref->operands
1872 3193150 : = newoperands.exists () ? newoperands : operands.copy ();
1873 3193150 : newoperands = vNULL;
1874 3193150 : newref->type = ref->type;
1875 3193150 : newref->result = result;
1876 3193150 : newref->hashcode = vn_reference_compute_hash (newref);
1877 3193150 : expr = get_or_alloc_expr_for_reference (newref, new_val_id,
1878 : expr_loc, true);
1879 3193150 : add_to_value (get_expr_value_id (expr), expr);
1880 : }
1881 4579095 : newoperands.release ();
1882 4579095 : return expr;
1883 : }
1884 25803069 : break;
1885 :
1886 25803069 : case NAME:
1887 25803069 : {
1888 25803069 : tree name = PRE_EXPR_NAME (expr);
1889 25803069 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1890 : /* If the SSA name is defined by a PHI node in this block,
1891 : translate it. */
1892 25803069 : if (gimple_code (def_stmt) == GIMPLE_PHI
1893 25803069 : && gimple_bb (def_stmt) == phiblock)
1894 : {
1895 8054747 : tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1896 :
1897 : /* Handle constant. */
1898 8054747 : if (is_gimple_min_invariant (def))
1899 2333276 : return get_or_alloc_expr_for_constant (def);
1900 :
1901 5721471 : return get_or_alloc_expr_for_name (def);
1902 : }
1903 : /* Otherwise return it unchanged - it will get removed if its
1904 : value is not available in PREDs AVAIL_OUT set of expressions
1905 : by the subtraction of TMP_GEN. */
1906 : return expr;
1907 : }
1908 :
1909 0 : default:
1910 0 : gcc_unreachable ();
1911 : }
1912 : }
1913 :
1914 : /* Wrapper around phi_translate_1 providing caching functionality. */
1915 :
1916 : static pre_expr
1917 90494955 : phi_translate (bitmap_set_t dest, pre_expr expr,
1918 : bitmap_set_t set1, bitmap_set_t set2, edge e)
1919 : {
1920 90494955 : expr_pred_trans_t slot = NULL;
1921 90494955 : pre_expr phitrans;
1922 :
1923 90494955 : if (!expr)
1924 : return NULL;
1925 :
1926 : /* Constants contain no values that need translation. */
1927 88587205 : if (expr->kind == CONSTANT)
1928 : return expr;
1929 :
1930 88586891 : if (value_id_constant_p (get_expr_value_id (expr)))
1931 : return expr;
1932 :
1933 : /* Don't add translations of NAMEs as those are cheap to translate. */
1934 88586891 : if (expr->kind != NAME)
1935 : {
1936 62783822 : if (phi_trans_add (&slot, expr, e->src))
1937 39197628 : return slot->v == 0 ? NULL : expression_for_id (slot->v);
1938 : /* Store NULL for the value we want to return in the case of
1939 : recursing. */
1940 23586194 : slot->v = 0;
1941 : }
1942 :
1943 : /* Translate. */
1944 49389263 : basic_block saved_valueize_bb = vn_context_bb;
1945 49389263 : vn_context_bb = e->src;
1946 49389263 : phitrans = phi_translate_1 (dest, expr, set1, set2, e);
1947 49389263 : vn_context_bb = saved_valueize_bb;
1948 :
1949 49389263 : if (slot)
1950 : {
1951 : /* We may have reallocated. */
1952 23586194 : phi_trans_add (&slot, expr, e->src);
1953 23586194 : if (phitrans)
1954 19731912 : slot->v = get_expression_id (phitrans);
1955 : else
1956 : /* Remove failed translations again, they cause insert
1957 : iteration to not pick up new opportunities reliably. */
1958 3854282 : PHI_TRANS_TABLE (e->src)->clear_slot (slot);
1959 : }
1960 :
1961 : return phitrans;
1962 : }
1963 :
1964 :
1965 : /* For each expression in SET, translate the values through phi nodes
1966 : in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1967 : expressions in DEST. */
1968 :
1969 : static void
1970 20787583 : phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
1971 : {
1972 20787583 : bitmap_iterator bi;
1973 20787583 : unsigned int i;
1974 :
1975 20787583 : if (gimple_seq_empty_p (phi_nodes (e->dest)))
1976 : {
1977 13895351 : bitmap_set_copy (dest, set);
1978 13895351 : return;
1979 : }
1980 :
1981 : /* Allocate the phi-translation cache where we have an idea about
1982 : its size. hash-table implementation internals tell us that
1983 : allocating the table to fit twice the number of elements will
1984 : make sure we do not usually re-allocate. */
1985 6892232 : if (!PHI_TRANS_TABLE (e->src))
1986 6018986 : PHI_TRANS_TABLE (e->src) = new hash_table<expr_pred_trans_d>
1987 6018986 : (2 * bitmap_count_bits (&set->expressions));
1988 45759866 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1989 : {
1990 38867634 : pre_expr expr = expression_for_id (i);
1991 38867634 : pre_expr translated = phi_translate (dest, expr, set, NULL, e);
1992 38867634 : if (!translated)
1993 1907866 : continue;
1994 :
1995 36959768 : bitmap_insert_into_set (dest, translated);
1996 : }
1997 : }
1998 :
1999 : /* Find the leader for a value (i.e., the name representing that
2000 : value) in a given set, and return it. Return NULL if no leader
2001 : is found. */
2002 :
2003 : static pre_expr
2004 57063855 : bitmap_find_leader (bitmap_set_t set, unsigned int val)
2005 : {
2006 57063855 : if (value_id_constant_p (val))
2007 1776971 : return constant_value_expressions[-val];
2008 :
2009 55286884 : if (bitmap_set_contains_value (set, val))
2010 : {
2011 : /* Rather than walk the entire bitmap of expressions, and see
2012 : whether any of them has the value we are looking for, we look
2013 : at the reverse mapping, which tells us the set of expressions
2014 : that have a given value (IE value->expressions with that
2015 : value) and see if any of those expressions are in our set.
2016 : The number of expressions per value is usually significantly
2017 : less than the number of expressions in the set. In fact, for
2018 : large testcases, doing it this way is roughly 5-10x faster
2019 : than walking the bitmap.
2020 : If this is somehow a significant lose for some cases, we can
2021 : choose which set to walk based on which set is smaller. */
2022 25796053 : unsigned int i;
2023 25796053 : bitmap_iterator bi;
2024 25796053 : bitmap exprset = value_expressions[val];
2025 :
2026 25796053 : if (!exprset->first->next)
2027 32580679 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2028 30269158 : if (bitmap_bit_p (&set->expressions, i))
2029 23393255 : return expression_for_id (i);
2030 :
2031 6331243 : EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
2032 3928445 : return expression_for_id (i);
2033 : }
2034 : return NULL;
2035 : }
2036 :
2037 : /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
2038 : BLOCK by seeing if it is not killed in the block. Note that we are
2039 : only determining whether there is a store that kills it. Because
2040 : of the order in which clean iterates over values, we are guaranteed
2041 : that altered operands will have caused us to be eliminated from the
2042 : ANTIC_IN set already. */
2043 :
2044 : static bool
2045 1663149 : value_dies_in_block_x (pre_expr expr, basic_block block)
2046 : {
2047 1663149 : tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
2048 1663149 : vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
2049 1663149 : gimple *def;
2050 1663149 : gimple_stmt_iterator gsi;
2051 1663149 : unsigned id = get_expression_id (expr);
2052 1663149 : bool res = false;
2053 1663149 : ao_ref ref;
2054 :
2055 1663149 : if (!vuse)
2056 : return false;
2057 :
2058 : /* Lookup a previously calculated result. */
2059 1663149 : if (EXPR_DIES (block)
2060 1663149 : && bitmap_bit_p (EXPR_DIES (block), id * 2))
2061 136994 : return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
2062 :
2063 : /* A memory expression {e, VUSE} dies in the block if there is a
2064 : statement that may clobber e. If, starting statement walk from the
2065 : top of the basic block, a statement uses VUSE there can be no kill
2066 : in between that use and the original statement that loaded {e, VUSE},
2067 : so we can stop walking. */
2068 1526155 : ref.base = NULL_TREE;
2069 13738721 : for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
2070 : {
2071 11733008 : tree def_vuse, def_vdef;
2072 11733008 : def = gsi_stmt (gsi);
2073 11733008 : def_vuse = gimple_vuse (def);
2074 11733008 : def_vdef = gimple_vdef (def);
2075 :
2076 : /* Not a memory statement. */
2077 11733008 : if (!def_vuse)
2078 8407727 : continue;
2079 :
2080 : /* Not a may-def. */
2081 3325281 : if (!def_vdef)
2082 : {
2083 : /* A load with the same VUSE, we're done. */
2084 968162 : if (def_vuse == vuse)
2085 : break;
2086 :
2087 677031 : continue;
2088 : }
2089 :
2090 : /* Init ref only if we really need it. */
2091 2357119 : if (ref.base == NULL_TREE
2092 3507138 : && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->base_set,
2093 1150019 : refx->type, refx->operands))
2094 : {
2095 : res = true;
2096 : break;
2097 : }
2098 : /* If the statement may clobber expr, it dies. */
2099 2322667 : if (stmt_may_clobber_ref_p_1 (def, &ref))
2100 : {
2101 : res = true;
2102 : break;
2103 : }
2104 : }
2105 :
2106 : /* Remember the result. */
2107 1526155 : if (!EXPR_DIES (block))
2108 717054 : EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
2109 1526155 : bitmap_set_bit (EXPR_DIES (block), id * 2);
2110 1526155 : if (res)
2111 755466 : bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
2112 :
2113 : return res;
2114 : }
2115 :
2116 :
2117 : /* Determine if OP is valid in SET1 U SET2, which it is when the union
2118 : contains its value-id. */
2119 :
2120 : static bool
2121 276290518 : op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
2122 : {
2123 276290518 : if (op && TREE_CODE (op) == SSA_NAME)
2124 : {
2125 82256306 : unsigned int value_id = VN_INFO (op)->value_id;
2126 164509988 : if (!(bitmap_set_contains_value (set1, value_id)
2127 2342684 : || (set2 && bitmap_set_contains_value (set2, value_id))))
2128 2210290 : return false;
2129 : }
2130 : return true;
2131 : }
2132 :
2133 : /* Determine if the expression EXPR is valid in SET1 U SET2.
2134 : ONLY SET2 CAN BE NULL.
2135 : This means that we have a leader for each part of the expression
2136 : (if it consists of values), or the expression is an SSA_NAME.
2137 : For loads/calls, we also see if the vuse is killed in this block. */
2138 :
2139 : static bool
2140 131090120 : valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
2141 : {
2142 131090120 : switch (expr->kind)
2143 : {
2144 : case NAME:
2145 : /* By construction all NAMEs are available. Non-available
2146 : NAMEs are removed by subtracting TMP_GEN from the sets. */
2147 : return true;
2148 59440465 : case NARY:
2149 59440465 : {
2150 59440465 : unsigned int i;
2151 59440465 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2152 155840969 : for (i = 0; i < nary->length; i++)
2153 98468495 : if (!op_valid_in_sets (set1, set2, nary->op[i]))
2154 : return false;
2155 : return true;
2156 : }
2157 20289840 : break;
2158 20289840 : case REFERENCE:
2159 20289840 : {
2160 20289840 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2161 20289840 : vn_reference_op_t vro;
2162 20289840 : unsigned int i;
2163 :
2164 79516384 : FOR_EACH_VEC_ELT (ref->operands, i, vro)
2165 : {
2166 59368843 : if (!op_valid_in_sets (set1, set2, vro->op0)
2167 59226590 : || !op_valid_in_sets (set1, set2, vro->op1)
2168 118595433 : || !op_valid_in_sets (set1, set2, vro->op2))
2169 : return false;
2170 : }
2171 : return true;
2172 : }
2173 0 : default:
2174 0 : gcc_unreachable ();
2175 : }
2176 : }
2177 :
2178 : /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
2179 : This means expressions that are made up of values we have no leaders for
2180 : in SET1 or SET2. */
2181 :
2182 : static void
2183 14665046 : clean (bitmap_set_t set1, bitmap_set_t set2 = NULL)
2184 : {
2185 14665046 : vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1, false);
2186 15624254 : bool changed;
2187 :
2188 15624254 : do
2189 : {
2190 15624254 : unsigned j = 0;
2191 15624254 : changed = false;
2192 84876265 : for (unsigned i = 0; i < exprs.length (); ++i)
2193 : {
2194 69252011 : pre_expr expr = exprs[i];
2195 69252011 : if (!valid_in_sets (set1, set2, expr))
2196 : {
2197 2210280 : unsigned int val = get_expr_value_id (expr);
2198 2210280 : bitmap_clear_bit (&set1->expressions, get_expression_id (expr));
2199 : /* We are entered with possibly multiple expressions for a value
2200 : so before removing a value from the set see if there's an
2201 : expression for it left. */
2202 2210280 : if (! bitmap_find_leader (set1, val))
2203 : {
2204 2199914 : bitmap_clear_bit (&set1->values, val);
2205 2199914 : changed = true;
2206 : }
2207 : }
2208 : else
2209 : {
2210 67041731 : exprs[j] = expr;
2211 67041731 : ++j;
2212 : }
2213 : }
2214 15624254 : exprs.truncate (j);
2215 : }
2216 : /* As the value graph can have cycles we have to iterate here. */
2217 : while (changed);
2218 14665046 : exprs.release ();
2219 :
2220 14665046 : if (flag_checking)
2221 : {
2222 14664859 : unsigned j;
2223 14664859 : bitmap_iterator bi;
2224 76272065 : FOR_EACH_EXPR_ID_IN_SET (set1, j, bi)
2225 61607206 : gcc_assert (valid_in_sets (set1, set2, expression_for_id (j)));
2226 : }
2227 14665046 : }
2228 :
2229 : /* Clean the set of expressions that are no longer valid in SET because
2230 : they are clobbered in BLOCK or because they trap and may not be executed.
2231 : When CLEAN_TRAPS is true remove all possibly trapping expressions. */
2232 :
2233 : static void
2234 17094475 : prune_clobbered_mems (bitmap_set_t set, basic_block block, bool clean_traps)
2235 : {
2236 17094475 : bitmap_iterator bi;
2237 17094475 : unsigned i;
2238 17094475 : unsigned to_remove = -1U;
2239 17094475 : bool any_removed = false;
2240 :
2241 77751274 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2242 : {
2243 : /* Remove queued expr. */
2244 60656799 : if (to_remove != -1U)
2245 : {
2246 594693 : bitmap_clear_bit (&set->expressions, to_remove);
2247 594693 : any_removed = true;
2248 594693 : to_remove = -1U;
2249 : }
2250 :
2251 60656799 : pre_expr expr = expression_for_id (i);
2252 60656799 : if (expr->kind == REFERENCE)
2253 : {
2254 8175672 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2255 8175672 : if (ref->vuse)
2256 : {
2257 7419759 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2258 7419759 : if (!gimple_nop_p (def_stmt)
2259 : /* If value-numbering provided a memory state for this
2260 : that dominates BLOCK we're done, otherwise we have
2261 : to check if the value dies in BLOCK. */
2262 9088802 : && !(gimple_bb (def_stmt) != block
2263 3823604 : && dominated_by_p (CDI_DOMINATORS,
2264 3823604 : block, gimple_bb (def_stmt)))
2265 9082908 : && value_dies_in_block_x (expr, block))
2266 : to_remove = i;
2267 : }
2268 : /* If the REFERENCE may trap make sure the block does not contain
2269 : a possible exit point.
2270 : ??? This is overly conservative if we translate AVAIL_OUT
2271 : as the available expression might be after the exit point. */
2272 7497369 : if ((BB_MAY_NOTRETURN (block) || clean_traps)
2273 8430057 : && vn_reference_may_trap (ref))
2274 : to_remove = i;
2275 : }
2276 52481127 : else if (expr->kind == NARY)
2277 : {
2278 27674704 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2279 : /* If the NARY may trap make sure the block does not contain
2280 : a possible exit point.
2281 : ??? This is overly conservative if we translate AVAIL_OUT
2282 : as the available expression might be after the exit point. */
2283 23477504 : if ((BB_MAY_NOTRETURN (block) || clean_traps)
2284 28539184 : && vn_nary_may_trap (nary))
2285 : to_remove = i;
2286 : }
2287 : }
2288 :
2289 : /* Remove queued expr. */
2290 17094475 : if (to_remove != -1U)
2291 : {
2292 431310 : bitmap_clear_bit (&set->expressions, to_remove);
2293 431310 : any_removed = true;
2294 : }
2295 :
2296 : /* Above we only removed expressions, now clean the set of values
2297 : which no longer have any corresponding expression. We cannot
2298 : clear the value at the time we remove an expression since there
2299 : may be multiple expressions per value.
2300 : If we'd queue possibly to be removed values we could use
2301 : the bitmap_find_leader way to see if there's still an expression
2302 : for it. For some ratio of to be removed values and number of
2303 : values/expressions in the set this might be faster than rebuilding
2304 : the value-set.
2305 : Note when there's a MAX solution on one edge (clean_traps) do not
2306 : prune values as we need to consider the resulting expression set MAX
2307 : as well. This avoids a later growing ANTIC_IN value-set during
2308 : iteration, when the explicitly represented expression set grows. */
2309 17094475 : if (any_removed && !clean_traps)
2310 : {
2311 494643 : bitmap_clear (&set->values);
2312 2792572 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2313 : {
2314 2297929 : pre_expr expr = expression_for_id (i);
2315 2297929 : unsigned int value_id = get_expr_value_id (expr);
2316 2297929 : bitmap_set_bit (&set->values, value_id);
2317 : }
2318 : }
2319 17094475 : }
2320 :
2321 : /* Compute the ANTIC set for BLOCK.
2322 :
2323 : If succs(BLOCK) > 1 then
2324 : ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2325 : else if succs(BLOCK) == 1 then
2326 : ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2327 :
2328 : ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2329 :
2330 : Note that clean() is deferred until after the iteration. */
2331 :
2332 : static bool
2333 15865805 : compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2334 : {
2335 15865805 : bitmap_set_t S, old, ANTIC_OUT;
2336 15865805 : edge e;
2337 15865805 : edge_iterator ei;
2338 :
2339 15865805 : bool was_visited = BB_VISITED (block);
2340 15865805 : bool changed = ! BB_VISITED (block);
2341 15865805 : bool any_max_on_edge = false;
2342 :
2343 15865805 : BB_VISITED (block) = 1;
2344 15865805 : old = ANTIC_OUT = S = NULL;
2345 :
2346 : /* If any edges from predecessors are abnormal, antic_in is empty,
2347 : so do nothing. */
2348 15865805 : if (block_has_abnormal_pred_edge)
2349 4399 : goto maybe_dump_sets;
2350 :
2351 15861406 : old = ANTIC_IN (block);
2352 15861406 : ANTIC_OUT = bitmap_set_new ();
2353 :
2354 : /* If the block has no successors, ANTIC_OUT is empty. */
2355 15861406 : if (EDGE_COUNT (block->succs) == 0)
2356 : ;
2357 : /* If we have one successor, we could have some phi nodes to
2358 : translate through. */
2359 15861406 : else if (single_succ_p (block))
2360 : {
2361 10126688 : e = single_succ_edge (block);
2362 10126688 : gcc_assert (BB_VISITED (e->dest));
2363 10126688 : phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
2364 : }
2365 : /* If we have multiple successors, we take the intersection of all of
2366 : them. Note that in the case of loop exit phi nodes, we may have
2367 : phis to translate through. */
2368 : else
2369 : {
2370 5734718 : size_t i;
2371 5734718 : edge first = NULL;
2372 :
2373 5734718 : auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2374 17311974 : FOR_EACH_EDGE (e, ei, block->succs)
2375 : {
2376 11577256 : if (!first
2377 6207754 : && BB_VISITED (e->dest))
2378 : first = e;
2379 5842538 : else if (BB_VISITED (e->dest))
2380 5183723 : worklist.quick_push (e);
2381 : else
2382 : {
2383 : /* Unvisited successors get their ANTIC_IN replaced by the
2384 : maximal set to arrive at a maximum ANTIC_IN solution.
2385 : We can ignore them in the intersection operation and thus
2386 : need not explicitly represent that maximum solution. */
2387 658815 : any_max_on_edge = true;
2388 658815 : if (dump_file && (dump_flags & TDF_DETAILS))
2389 18 : fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n",
2390 18 : e->src->index, e->dest->index);
2391 : }
2392 : }
2393 :
2394 : /* Of multiple successors we have to have visited one already
2395 : which is guaranteed by iteration order. */
2396 5734718 : gcc_assert (first != NULL);
2397 :
2398 5734718 : phi_translate_set (ANTIC_OUT, ANTIC_IN (first->dest), first);
2399 :
2400 : /* If we have multiple successors we need to intersect the ANTIC_OUT
2401 : sets. For values that's a simple intersection but for
2402 : expressions it is a union. Given we want to have a single
2403 : expression per value in our sets we have to canonicalize.
2404 : Avoid randomness and running into cycles like for PR82129 and
2405 : canonicalize the expression we choose to the one with the
2406 : lowest id. This requires we actually compute the union first. */
2407 22387877 : FOR_EACH_VEC_ELT (worklist, i, e)
2408 : {
2409 5183723 : if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2410 : {
2411 2350 : bitmap_set_t tmp = bitmap_set_new ();
2412 2350 : phi_translate_set (tmp, ANTIC_IN (e->dest), e);
2413 2350 : bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
2414 2350 : bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
2415 2350 : bitmap_set_free (tmp);
2416 : }
2417 : else
2418 : {
2419 5181373 : bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
2420 5181373 : bitmap_ior_into (&ANTIC_OUT->expressions,
2421 5181373 : &ANTIC_IN (e->dest)->expressions);
2422 : }
2423 : }
2424 11469436 : if (! worklist.is_empty ())
2425 : {
2426 : /* Prune expressions not in the value set. */
2427 5079511 : bitmap_iterator bi;
2428 5079511 : unsigned int i;
2429 5079511 : unsigned int to_clear = -1U;
2430 36917590 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
2431 : {
2432 31838079 : if (to_clear != -1U)
2433 : {
2434 16813216 : bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2435 16813216 : to_clear = -1U;
2436 : }
2437 31838079 : pre_expr expr = expression_for_id (i);
2438 31838079 : unsigned int value_id = get_expr_value_id (expr);
2439 31838079 : if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
2440 20584728 : to_clear = i;
2441 : }
2442 5079511 : if (to_clear != -1U)
2443 3771512 : bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2444 : }
2445 5734718 : }
2446 :
2447 : /* Dump ANTIC_OUT before it's pruned. */
2448 15861406 : if (dump_file && (dump_flags & TDF_DETAILS))
2449 151 : print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2450 :
2451 : /* Prune expressions that are clobbered in block and thus become
2452 : invalid if translated from ANTIC_OUT to ANTIC_IN. */
2453 15861406 : prune_clobbered_mems (ANTIC_OUT, block, any_max_on_edge);
2454 :
2455 : /* Generate ANTIC_OUT - TMP_GEN. Note when there's a MAX solution
2456 : on one edge do not prune values as we need to consider the resulting
2457 : expression set MAX as well. This avoids a later growing ANTIC_IN
2458 : value-set during iteration, when the explicitly represented
2459 : expression set grows. */
2460 15861406 : S = bitmap_set_subtract_expressions (ANTIC_OUT, TMP_GEN (block),
2461 : any_max_on_edge);
2462 :
2463 : /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2464 31722812 : ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
2465 15861406 : TMP_GEN (block));
2466 :
2467 : /* Then union in the ANTIC_OUT - TMP_GEN values,
2468 : to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2469 15861406 : bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
2470 15861406 : bitmap_ior_into (&ANTIC_IN (block)->expressions, &S->expressions);
2471 :
2472 : /* clean (ANTIC_IN (block)) is deferred to after the iteration converged
2473 : because it can cause non-convergence, see for example PR81181. */
2474 :
2475 15861406 : if (was_visited
2476 15861406 : && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
2477 : {
2478 1957 : if (dump_file && (dump_flags & TDF_DETAILS))
2479 0 : fprintf (dump_file, "warning: intersecting with old ANTIC_IN "
2480 : "shrinks the set\n");
2481 : /* Prune expressions not in the value set. */
2482 1957 : bitmap_iterator bi;
2483 1957 : unsigned int i;
2484 1957 : unsigned int to_clear = -1U;
2485 21317 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
2486 : {
2487 19360 : if (to_clear != -1U)
2488 : {
2489 1565 : bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2490 1565 : to_clear = -1U;
2491 : }
2492 19360 : pre_expr expr = expression_for_id (i);
2493 19360 : unsigned int value_id = get_expr_value_id (expr);
2494 19360 : if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
2495 2930 : to_clear = i;
2496 : }
2497 1957 : if (to_clear != -1U)
2498 1365 : bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2499 : }
2500 :
2501 15861406 : if (!bitmap_set_equal (old, ANTIC_IN (block)))
2502 10352612 : changed = true;
2503 :
2504 5508794 : maybe_dump_sets:
2505 15865805 : if (dump_file && (dump_flags & TDF_DETAILS))
2506 : {
2507 151 : if (changed)
2508 129 : fprintf (dump_file, "[changed] ");
2509 151 : print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2510 : block->index);
2511 :
2512 151 : if (S)
2513 151 : print_bitmap_set (dump_file, S, "S", block->index);
2514 : }
2515 15865805 : if (old)
2516 15861406 : bitmap_set_free (old);
2517 15865805 : if (S)
2518 15861406 : bitmap_set_free (S);
2519 15865805 : if (ANTIC_OUT)
2520 15861406 : bitmap_set_free (ANTIC_OUT);
2521 15865805 : return changed;
2522 : }
2523 :
2524 : /* Compute PARTIAL_ANTIC for BLOCK.
2525 :
2526 : If succs(BLOCK) > 1 then
2527 : PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2528 : in ANTIC_OUT for all succ(BLOCK)
2529 : else if succs(BLOCK) == 1 then
2530 : PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2531 :
2532 : PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
2533 :
2534 : */
2535 : static void
2536 1234390 : compute_partial_antic_aux (basic_block block,
2537 : bool block_has_abnormal_pred_edge)
2538 : {
2539 1234390 : bitmap_set_t old_PA_IN;
2540 1234390 : bitmap_set_t PA_OUT;
2541 1234390 : edge e;
2542 1234390 : edge_iterator ei;
2543 1234390 : unsigned long max_pa = param_max_partial_antic_length;
2544 :
2545 1234390 : old_PA_IN = PA_OUT = NULL;
2546 :
2547 : /* If any edges from predecessors are abnormal, antic_in is empty,
2548 : so do nothing. */
2549 1234390 : if (block_has_abnormal_pred_edge)
2550 770 : goto maybe_dump_sets;
2551 :
2552 : /* If there are too many partially anticipatable values in the
2553 : block, phi_translate_set can take an exponential time: stop
2554 : before the translation starts. */
2555 1233620 : if (max_pa
2556 1142102 : && single_succ_p (block)
2557 2006231 : && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2558 551 : goto maybe_dump_sets;
2559 :
2560 1233069 : old_PA_IN = PA_IN (block);
2561 1233069 : PA_OUT = bitmap_set_new ();
2562 :
2563 : /* If the block has no successors, ANTIC_OUT is empty. */
2564 1233069 : if (EDGE_COUNT (block->succs) == 0)
2565 : ;
2566 : /* If we have one successor, we could have some phi nodes to
2567 : translate through. Note that we can't phi translate across DFS
2568 : back edges in partial antic, because it uses a union operation on
2569 : the successors. For recurrences like IV's, we will end up
2570 : generating a new value in the set on each go around (i + 3 (VH.1)
2571 : VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2572 1141553 : else if (single_succ_p (block))
2573 : {
2574 772062 : e = single_succ_edge (block);
2575 772062 : if (!(e->flags & EDGE_DFS_BACK))
2576 692694 : phi_translate_set (PA_OUT, PA_IN (e->dest), e);
2577 : }
2578 : /* If we have multiple successors, we take the union of all of
2579 : them. */
2580 : else
2581 : {
2582 369491 : size_t i;
2583 :
2584 369491 : auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2585 1113829 : FOR_EACH_EDGE (e, ei, block->succs)
2586 : {
2587 744338 : if (e->flags & EDGE_DFS_BACK)
2588 312 : continue;
2589 744026 : worklist.quick_push (e);
2590 : }
2591 369491 : if (worklist.length () > 0)
2592 : {
2593 1113517 : FOR_EACH_VEC_ELT (worklist, i, e)
2594 : {
2595 744026 : unsigned int i;
2596 744026 : bitmap_iterator bi;
2597 :
2598 744026 : if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2599 : {
2600 745 : bitmap_set_t antic_in = bitmap_set_new ();
2601 745 : phi_translate_set (antic_in, ANTIC_IN (e->dest), e);
2602 1513 : FOR_EACH_EXPR_ID_IN_SET (antic_in, i, bi)
2603 768 : bitmap_value_insert_into_set (PA_OUT,
2604 : expression_for_id (i));
2605 745 : bitmap_set_free (antic_in);
2606 745 : bitmap_set_t pa_in = bitmap_set_new ();
2607 745 : phi_translate_set (pa_in, PA_IN (e->dest), e);
2608 745 : FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2609 0 : bitmap_value_insert_into_set (PA_OUT,
2610 : expression_for_id (i));
2611 745 : bitmap_set_free (pa_in);
2612 : }
2613 : else
2614 : {
2615 4826026 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
2616 4082745 : bitmap_value_insert_into_set (PA_OUT,
2617 : expression_for_id (i));
2618 7520439 : FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
2619 6777158 : bitmap_value_insert_into_set (PA_OUT,
2620 : expression_for_id (i));
2621 : }
2622 : }
2623 : }
2624 369491 : }
2625 :
2626 : /* Prune expressions that are clobbered in block and thus become
2627 : invalid if translated from PA_OUT to PA_IN. */
2628 1233069 : prune_clobbered_mems (PA_OUT, block, false);
2629 :
2630 : /* PA_IN starts with PA_OUT - TMP_GEN.
2631 : Then we subtract things from ANTIC_IN. */
2632 1233069 : PA_IN (block) = bitmap_set_subtract_expressions (PA_OUT, TMP_GEN (block));
2633 :
2634 : /* For partial antic, we want to put back in the phi results, since
2635 : we will properly avoid making them partially antic over backedges. */
2636 1233069 : bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2637 1233069 : bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2638 :
2639 : /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2640 1233069 : bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2641 :
2642 1233069 : clean (PA_IN (block), ANTIC_IN (block));
2643 :
2644 1234390 : maybe_dump_sets:
2645 1234390 : if (dump_file && (dump_flags & TDF_DETAILS))
2646 : {
2647 0 : if (PA_OUT)
2648 0 : print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2649 :
2650 0 : print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2651 : }
2652 1234390 : if (old_PA_IN)
2653 1233069 : bitmap_set_free (old_PA_IN);
2654 1234390 : if (PA_OUT)
2655 1233069 : bitmap_set_free (PA_OUT);
2656 1234390 : }
2657 :
2658 : /* Compute ANTIC and partial ANTIC sets. */
2659 :
2660 : static void
2661 981520 : compute_antic (void)
2662 : {
2663 981520 : bool changed = true;
2664 981520 : int num_iterations = 0;
2665 981520 : basic_block block;
2666 981520 : int i;
2667 981520 : edge_iterator ei;
2668 981520 : edge e;
2669 :
2670 : /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2671 : We pre-build the map of blocks with incoming abnormal edges here. */
2672 981520 : auto_sbitmap has_abnormal_preds (last_basic_block_for_fn (cfun));
2673 981520 : bitmap_clear (has_abnormal_preds);
2674 :
2675 16376537 : FOR_ALL_BB_FN (block, cfun)
2676 : {
2677 15395017 : BB_VISITED (block) = 0;
2678 :
2679 34725784 : FOR_EACH_EDGE (e, ei, block->preds)
2680 19334152 : if (e->flags & EDGE_ABNORMAL)
2681 : {
2682 3385 : bitmap_set_bit (has_abnormal_preds, block->index);
2683 3385 : break;
2684 : }
2685 :
2686 : /* While we are here, give empty ANTIC_IN sets to each block. */
2687 15395017 : ANTIC_IN (block) = bitmap_set_new ();
2688 15395017 : if (do_partial_partial)
2689 1234390 : PA_IN (block) = bitmap_set_new ();
2690 : }
2691 :
2692 : /* At the exit block we anticipate nothing. */
2693 981520 : BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2694 :
2695 : /* For ANTIC computation we need a postorder that also guarantees that
2696 : a block with a single successor is visited after its successor.
2697 : RPO on the inverted CFG has this property. */
2698 981520 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2699 981520 : int n = inverted_rev_post_order_compute (cfun, rpo);
2700 :
2701 981520 : auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
2702 981520 : bitmap_clear (worklist);
2703 2850826 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2704 1869306 : bitmap_set_bit (worklist, e->src->index);
2705 3037045 : while (changed)
2706 : {
2707 2055525 : if (dump_file && (dump_flags & TDF_DETAILS))
2708 35 : fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2709 : /* ??? We need to clear our PHI translation cache here as the
2710 : ANTIC sets shrink and we restrict valid translations to
2711 : those having operands with leaders in ANTIC. Same below
2712 : for PA ANTIC computation. */
2713 2055525 : num_iterations++;
2714 2055525 : changed = false;
2715 38992854 : for (i = 0; i < n; ++i)
2716 : {
2717 36937329 : if (bitmap_bit_p (worklist, rpo[i]))
2718 : {
2719 15865805 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2720 15865805 : bitmap_clear_bit (worklist, block->index);
2721 15865805 : if (compute_antic_aux (block,
2722 15865805 : bitmap_bit_p (has_abnormal_preds,
2723 : block->index)))
2724 : {
2725 33400226 : FOR_EACH_EDGE (e, ei, block->preds)
2726 18373469 : bitmap_set_bit (worklist, e->src->index);
2727 : changed = true;
2728 : }
2729 : }
2730 : }
2731 : /* Theoretically possible, but *highly* unlikely. */
2732 2055525 : gcc_checking_assert (num_iterations < 500);
2733 : }
2734 :
2735 : /* We have to clean after the dataflow problem converged as cleaning
2736 : can cause non-convergence because it is based on expressions
2737 : rather than values. */
2738 14413497 : FOR_EACH_BB_FN (block, cfun)
2739 13431977 : clean (ANTIC_IN (block));
2740 :
2741 981520 : statistics_histogram_event (cfun, "compute_antic iterations",
2742 : num_iterations);
2743 :
2744 981520 : if (do_partial_partial)
2745 : {
2746 : /* For partial antic we ignore backedges and thus we do not need
2747 : to perform any iteration when we process blocks in rpo. */
2748 1325906 : for (i = 0; i < n; ++i)
2749 : {
2750 1234390 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2751 1234390 : compute_partial_antic_aux (block,
2752 1234390 : bitmap_bit_p (has_abnormal_preds,
2753 : block->index));
2754 : }
2755 : }
2756 :
2757 981520 : free (rpo);
2758 981520 : }
2759 :
2760 :
2761 : /* Inserted expressions are placed onto this worklist, which is used
2762 : for performing quick dead code elimination of insertions we made
2763 : that didn't turn out to be necessary. */
2764 : static bitmap inserted_exprs;
2765 :
2766 : /* The actual worker for create_component_ref_by_pieces. */
2767 :
2768 : static tree
2769 1161988 : create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2770 : unsigned int *operand, gimple_seq *stmts)
2771 : {
2772 1161988 : vn_reference_op_t currop = &ref->operands[*operand];
2773 1161988 : tree genop;
2774 1161988 : ++*operand;
2775 1161988 : switch (currop->opcode)
2776 : {
2777 0 : case CALL_EXPR:
2778 0 : gcc_unreachable ();
2779 :
2780 405227 : case MEM_REF:
2781 405227 : {
2782 405227 : tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2783 : stmts);
2784 405227 : if (!baseop)
2785 : return NULL_TREE;
2786 405223 : tree offset = currop->op0;
2787 405223 : if (TREE_CODE (baseop) == ADDR_EXPR
2788 405223 : && handled_component_p (TREE_OPERAND (baseop, 0)))
2789 : {
2790 96 : poly_int64 off;
2791 96 : tree base;
2792 96 : base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2793 : &off);
2794 96 : gcc_assert (base);
2795 96 : offset = int_const_binop (PLUS_EXPR, offset,
2796 96 : build_int_cst (TREE_TYPE (offset),
2797 : off));
2798 96 : baseop = build_fold_addr_expr (base);
2799 : }
2800 405223 : genop = build2 (MEM_REF, currop->type, baseop, offset);
2801 405223 : MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2802 405223 : MR_DEPENDENCE_BASE (genop) = currop->base;
2803 405223 : REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
2804 405223 : return genop;
2805 : }
2806 :
2807 0 : case TARGET_MEM_REF:
2808 0 : {
2809 0 : tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2810 0 : vn_reference_op_t nextop = &ref->operands[(*operand)++];
2811 0 : tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2812 : stmts);
2813 0 : if (!baseop)
2814 : return NULL_TREE;
2815 0 : if (currop->op0)
2816 : {
2817 0 : genop0 = find_or_generate_expression (block, currop->op0, stmts);
2818 0 : if (!genop0)
2819 : return NULL_TREE;
2820 : }
2821 0 : if (nextop->op0)
2822 : {
2823 0 : genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2824 0 : if (!genop1)
2825 : return NULL_TREE;
2826 : }
2827 0 : genop = build5 (TARGET_MEM_REF, currop->type,
2828 : baseop, currop->op2, genop0, currop->op1, genop1);
2829 :
2830 0 : MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2831 0 : MR_DEPENDENCE_BASE (genop) = currop->base;
2832 0 : return genop;
2833 : }
2834 :
2835 245716 : case ADDR_EXPR:
2836 245716 : if (currop->op0)
2837 : {
2838 243520 : gcc_assert (is_gimple_min_invariant (currop->op0));
2839 243520 : return currop->op0;
2840 : }
2841 : /* Fallthrough. */
2842 6496 : case REALPART_EXPR:
2843 6496 : case IMAGPART_EXPR:
2844 6496 : case VIEW_CONVERT_EXPR:
2845 6496 : {
2846 6496 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2847 : stmts);
2848 6496 : if (!genop0)
2849 : return NULL_TREE;
2850 6496 : return build1 (currop->opcode, currop->type, genop0);
2851 : }
2852 :
2853 4 : case WITH_SIZE_EXPR:
2854 4 : {
2855 4 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2856 : stmts);
2857 4 : if (!genop0)
2858 : return NULL_TREE;
2859 4 : tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2860 4 : if (!genop1)
2861 : return NULL_TREE;
2862 4 : return build2 (currop->opcode, currop->type, genop0, genop1);
2863 : }
2864 :
2865 2808 : case BIT_FIELD_REF:
2866 2808 : {
2867 2808 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2868 : stmts);
2869 2808 : if (!genop0)
2870 : return NULL_TREE;
2871 2808 : tree op1 = currop->op0;
2872 2808 : tree op2 = currop->op1;
2873 2808 : tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2874 2808 : REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
2875 2808 : return t;
2876 : }
2877 :
2878 : /* For array ref vn_reference_op's, operand 1 of the array ref
2879 : is op0 of the reference op and operand 3 of the array ref is
2880 : op1. */
2881 63140 : case ARRAY_RANGE_REF:
2882 63140 : case ARRAY_REF:
2883 63140 : {
2884 63140 : tree genop0;
2885 63140 : tree genop1 = currop->op0;
2886 63140 : tree genop2 = currop->op1;
2887 63140 : tree genop3 = currop->op2;
2888 63140 : genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2889 : stmts);
2890 63140 : if (!genop0)
2891 : return NULL_TREE;
2892 63140 : genop1 = find_or_generate_expression (block, genop1, stmts);
2893 63140 : if (!genop1)
2894 : return NULL_TREE;
2895 63140 : if (genop2)
2896 : {
2897 63140 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2898 : /* Drop zero minimum index if redundant. */
2899 63140 : if (integer_zerop (genop2)
2900 63140 : && (!domain_type
2901 62105 : || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2902 : genop2 = NULL_TREE;
2903 : else
2904 : {
2905 608 : genop2 = find_or_generate_expression (block, genop2, stmts);
2906 608 : if (!genop2)
2907 : return NULL_TREE;
2908 : }
2909 : }
2910 63140 : if (genop3)
2911 : {
2912 63140 : tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2913 : /* We can't always put a size in units of the element alignment
2914 : here as the element alignment may be not visible. See
2915 : PR43783. Simply drop the element size for constant
2916 : sizes. */
2917 63140 : if ((TREE_CODE (genop3) == INTEGER_CST
2918 63136 : && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
2919 63136 : && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
2920 63136 : (wi::to_offset (genop3) * vn_ref_op_align_unit (currop))))
2921 63140 : || (TREE_CODE (genop3) == EXACT_DIV_EXPR
2922 0 : && TREE_CODE (TREE_OPERAND (genop3, 1)) == INTEGER_CST
2923 0 : && operand_equal_p (TREE_OPERAND (genop3, 0), TYPE_SIZE_UNIT (elmt_type))
2924 0 : && wi::eq_p (wi::to_offset (TREE_OPERAND (genop3, 1)),
2925 63136 : vn_ref_op_align_unit (currop))))
2926 : genop3 = NULL_TREE;
2927 : else
2928 : {
2929 4 : genop3 = find_or_generate_expression (block, genop3, stmts);
2930 4 : if (!genop3)
2931 : return NULL_TREE;
2932 : }
2933 : }
2934 63140 : return build4 (currop->opcode, currop->type, genop0, genop1,
2935 63140 : genop2, genop3);
2936 : }
2937 274471 : case COMPONENT_REF:
2938 274471 : {
2939 274471 : tree op0;
2940 274471 : tree op1;
2941 274471 : tree genop2 = currop->op1;
2942 274471 : op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2943 274471 : if (!op0)
2944 : return NULL_TREE;
2945 : /* op1 should be a FIELD_DECL, which are represented by themselves. */
2946 274463 : op1 = currop->op0;
2947 274463 : if (genop2)
2948 : {
2949 0 : genop2 = find_or_generate_expression (block, genop2, stmts);
2950 0 : if (!genop2)
2951 : return NULL_TREE;
2952 : }
2953 274463 : return build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2954 : }
2955 :
2956 164342 : case SSA_NAME:
2957 164342 : {
2958 164342 : genop = find_or_generate_expression (block, currop->op0, stmts);
2959 164342 : return genop;
2960 : }
2961 1980 : case STRING_CST:
2962 1980 : case INTEGER_CST:
2963 1980 : case POLY_INT_CST:
2964 1980 : case COMPLEX_CST:
2965 1980 : case VECTOR_CST:
2966 1980 : case REAL_CST:
2967 1980 : case CONSTRUCTOR:
2968 1980 : case VAR_DECL:
2969 1980 : case PARM_DECL:
2970 1980 : case CONST_DECL:
2971 1980 : case RESULT_DECL:
2972 1980 : case FUNCTION_DECL:
2973 1980 : return currop->op0;
2974 :
2975 0 : default:
2976 0 : gcc_unreachable ();
2977 : }
2978 : }
2979 :
2980 : /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2981 : COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2982 : trying to rename aggregates into ssa form directly, which is a no no.
2983 :
2984 : Thus, this routine doesn't create temporaries, it just builds a
2985 : single access expression for the array, calling
2986 : find_or_generate_expression to build the innermost pieces.
2987 :
2988 : This function is a subroutine of create_expression_by_pieces, and
2989 : should not be called on it's own unless you really know what you
2990 : are doing. */
2991 :
2992 : static tree
2993 405256 : create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2994 : gimple_seq *stmts)
2995 : {
2996 405256 : unsigned int op = 0;
2997 405256 : return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2998 : }
2999 :
3000 : /* Find a simple leader for an expression, or generate one using
3001 : create_expression_by_pieces from a NARY expression for the value.
3002 : BLOCK is the basic_block we are looking for leaders in.
3003 : OP is the tree expression to find a leader for or generate.
3004 : Returns the leader or NULL_TREE on failure. */
3005 :
3006 : static tree
3007 868928 : find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
3008 : {
3009 : /* Constants are always leaders. */
3010 868928 : if (is_gimple_min_invariant (op))
3011 : return op;
3012 :
3013 664085 : gcc_assert (TREE_CODE (op) == SSA_NAME);
3014 664085 : vn_ssa_aux_t info = VN_INFO (op);
3015 664085 : unsigned int lookfor = info->value_id;
3016 664085 : if (value_id_constant_p (lookfor))
3017 3 : return info->valnum;
3018 :
3019 664082 : pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
3020 664082 : if (leader)
3021 : {
3022 630592 : if (leader->kind == NAME)
3023 : {
3024 630592 : tree name = PRE_EXPR_NAME (leader);
3025 630592 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
3026 : return NULL_TREE;
3027 630564 : return name;
3028 : }
3029 0 : else if (leader->kind == CONSTANT)
3030 0 : return PRE_EXPR_CONSTANT (leader);
3031 :
3032 : /* Defer. */
3033 : return NULL_TREE;
3034 : }
3035 33490 : gcc_assert (!value_id_constant_p (lookfor));
3036 :
3037 : /* It must be a complex expression, so generate it recursively. Note
3038 : that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre-28.c
3039 : where the insert algorithm fails to insert a required expression. */
3040 33490 : bitmap exprset = value_expressions[lookfor];
3041 33490 : bitmap_iterator bi;
3042 33490 : unsigned int i;
3043 33490 : if (exprset)
3044 44010 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
3045 : {
3046 41396 : pre_expr temp = expression_for_id (i);
3047 : /* We cannot insert random REFERENCE expressions at arbitrary
3048 : places. We can insert NARYs which eventually re-materializes
3049 : its operand values. */
3050 41396 : if (temp->kind == NARY)
3051 : {
3052 30872 : static int depth;
3053 30872 : if (depth > 8)
3054 : return NULL_TREE;
3055 :
3056 30857 : depth++;
3057 30857 : tree res = create_expression_by_pieces (block, temp, stmts,
3058 30857 : TREE_TYPE (op));
3059 30857 : depth--;
3060 30857 : return res;
3061 : }
3062 : }
3063 :
3064 : /* Defer. */
3065 : return NULL_TREE;
3066 : }
3067 :
3068 : /* Create an expression in pieces, so that we can handle very complex
3069 : expressions that may be ANTIC, but not necessary GIMPLE.
3070 : BLOCK is the basic block the expression will be inserted into,
3071 : EXPR is the expression to insert (in value form)
3072 : STMTS is a statement list to append the necessary insertions into.
3073 :
3074 : This function will die if we hit some value that shouldn't be
3075 : ANTIC but is (IE there is no leader for it, or its components).
3076 : The function returns NULL_TREE in case a different antic expression
3077 : has to be inserted first.
3078 : This function may also generate expressions that are themselves
3079 : partially or fully redundant. Those that are will be either made
3080 : fully redundant during the next iteration of insert (for partially
3081 : redundant ones), or eliminated by eliminate (for fully redundant
3082 : ones). */
3083 :
3084 : static tree
3085 2923019 : create_expression_by_pieces (basic_block block, pre_expr expr,
3086 : gimple_seq *stmts, tree type)
3087 : {
3088 2923019 : tree name;
3089 2923019 : tree folded;
3090 2923019 : gimple_seq forced_stmts = NULL;
3091 2923019 : unsigned int value_id;
3092 2923019 : gimple_stmt_iterator gsi;
3093 2923019 : tree exprtype = type ? type : get_expr_type (expr);
3094 2923019 : pre_expr nameexpr;
3095 2923019 : gassign *newstmt;
3096 :
3097 2923019 : switch (expr->kind)
3098 : {
3099 : /* We may hit the NAME/CONSTANT case if we have to convert types
3100 : that value numbering saw through. */
3101 738219 : case NAME:
3102 738219 : folded = PRE_EXPR_NAME (expr);
3103 738219 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
3104 : return NULL_TREE;
3105 738214 : if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
3106 : return folded;
3107 : break;
3108 1400306 : case CONSTANT:
3109 1400306 : {
3110 1400306 : folded = PRE_EXPR_CONSTANT (expr);
3111 1400306 : tree tem = fold_convert (exprtype, folded);
3112 1400306 : if (is_gimple_min_invariant (tem))
3113 : return tem;
3114 : break;
3115 : }
3116 408391 : case REFERENCE:
3117 408391 : if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
3118 : {
3119 3135 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
3120 3135 : unsigned int operand = 1;
3121 3135 : vn_reference_op_t currop = &ref->operands[0];
3122 3135 : tree sc = NULL_TREE;
3123 3135 : tree fn = NULL_TREE;
3124 3135 : if (currop->op0)
3125 : {
3126 2993 : fn = find_or_generate_expression (block, currop->op0, stmts);
3127 2993 : if (!fn)
3128 6 : return NULL_TREE;
3129 : }
3130 3135 : if (currop->op1)
3131 : {
3132 0 : sc = find_or_generate_expression (block, currop->op1, stmts);
3133 0 : if (!sc)
3134 : return NULL_TREE;
3135 : }
3136 6270 : auto_vec<tree> args (ref->operands.length () - 1);
3137 10850 : while (operand < ref->operands.length ())
3138 : {
3139 4586 : tree arg = create_component_ref_by_pieces_1 (block, ref,
3140 4586 : &operand, stmts);
3141 4586 : if (!arg)
3142 6 : return NULL_TREE;
3143 4580 : args.quick_push (arg);
3144 : }
3145 3129 : gcall *call;
3146 3129 : if (currop->op0)
3147 : {
3148 2987 : call = gimple_build_call_vec (fn, args);
3149 2987 : gimple_call_set_fntype (call, currop->type);
3150 : }
3151 : else
3152 142 : call = gimple_build_call_internal_vec ((internal_fn)currop->clique,
3153 : args);
3154 3129 : gimple_set_location (call, expr->loc);
3155 3129 : if (sc)
3156 0 : gimple_call_set_chain (call, sc);
3157 3129 : tree forcedname = make_ssa_name (ref->type);
3158 3129 : gimple_call_set_lhs (call, forcedname);
3159 : /* There's no CCP pass after PRE which would re-compute alignment
3160 : information so make sure we re-materialize this here. */
3161 3129 : if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED)
3162 0 : && args.length () - 2 <= 1
3163 0 : && tree_fits_uhwi_p (args[1])
3164 3129 : && (args.length () != 3 || tree_fits_uhwi_p (args[2])))
3165 : {
3166 0 : unsigned HOST_WIDE_INT halign = tree_to_uhwi (args[1]);
3167 0 : unsigned HOST_WIDE_INT hmisalign
3168 0 : = args.length () == 3 ? tree_to_uhwi (args[2]) : 0;
3169 0 : if ((halign & (halign - 1)) == 0
3170 0 : && (hmisalign & ~(halign - 1)) == 0
3171 0 : && (unsigned int)halign != 0)
3172 0 : set_ptr_info_alignment (get_ptr_info (forcedname),
3173 : halign, hmisalign);
3174 : }
3175 3129 : gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
3176 3129 : gimple_seq_add_stmt_without_update (&forced_stmts, call);
3177 3129 : folded = forcedname;
3178 3135 : }
3179 : else
3180 : {
3181 405256 : folded = create_component_ref_by_pieces (block,
3182 : PRE_EXPR_REFERENCE (expr),
3183 : stmts);
3184 405256 : if (!folded)
3185 : return NULL_TREE;
3186 405252 : name = make_temp_ssa_name (exprtype, NULL, "pretmp");
3187 405252 : newstmt = gimple_build_assign (name, folded);
3188 405252 : gimple_set_location (newstmt, expr->loc);
3189 405252 : gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
3190 405252 : gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
3191 405252 : folded = name;
3192 : }
3193 : break;
3194 376103 : case NARY:
3195 376103 : {
3196 376103 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
3197 376103 : tree *genop = XALLOCAVEC (tree, nary->length);
3198 376103 : unsigned i;
3199 1005257 : for (i = 0; i < nary->length; ++i)
3200 : {
3201 637837 : genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
3202 637837 : if (!genop[i])
3203 : return NULL_TREE;
3204 : /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
3205 : may have conversions stripped. */
3206 629154 : if (nary->opcode == POINTER_PLUS_EXPR)
3207 : {
3208 107002 : if (i == 0)
3209 53520 : genop[i] = gimple_convert (&forced_stmts,
3210 : nary->type, genop[i]);
3211 53482 : else if (i == 1)
3212 53482 : genop[i] = gimple_convert (&forced_stmts,
3213 : sizetype, genop[i]);
3214 : }
3215 : else
3216 522152 : genop[i] = gimple_convert (&forced_stmts,
3217 522152 : TREE_TYPE (nary->op[i]), genop[i]);
3218 : }
3219 367420 : if (nary->opcode == CONSTRUCTOR)
3220 : {
3221 8 : vec<constructor_elt, va_gc> *elts = NULL;
3222 40 : for (i = 0; i < nary->length; ++i)
3223 32 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
3224 8 : folded = build_constructor (nary->type, elts);
3225 8 : name = make_temp_ssa_name (exprtype, NULL, "pretmp");
3226 8 : newstmt = gimple_build_assign (name, folded);
3227 8 : gimple_set_location (newstmt, expr->loc);
3228 8 : gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
3229 8 : folded = name;
3230 : }
3231 : else
3232 : {
3233 367412 : switch (nary->length)
3234 : {
3235 106952 : case 1:
3236 106952 : folded = gimple_build (&forced_stmts, expr->loc,
3237 : nary->opcode, nary->type, genop[0]);
3238 106952 : break;
3239 260247 : case 2:
3240 260247 : folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
3241 : nary->type, genop[0], genop[1]);
3242 260247 : break;
3243 213 : case 3:
3244 213 : folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
3245 : nary->type, genop[0], genop[1],
3246 : genop[2]);
3247 213 : break;
3248 0 : default:
3249 0 : gcc_unreachable ();
3250 : }
3251 : }
3252 : }
3253 : break;
3254 0 : default:
3255 0 : gcc_unreachable ();
3256 : }
3257 :
3258 871139 : folded = gimple_convert (&forced_stmts, exprtype, folded);
3259 :
3260 : /* If there is nothing to insert, return the simplified result. */
3261 871139 : if (gimple_seq_empty_p (forced_stmts))
3262 : return folded;
3263 : /* If we simplified to a constant return it and discard eventually
3264 : built stmts. */
3265 775720 : if (is_gimple_min_invariant (folded))
3266 : {
3267 0 : gimple_seq_discard (forced_stmts);
3268 0 : return folded;
3269 : }
3270 : /* Likewise if we simplified to sth not queued for insertion. */
3271 775720 : bool found = false;
3272 775720 : gsi = gsi_last (forced_stmts);
3273 775720 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
3274 : {
3275 775720 : gimple *stmt = gsi_stmt (gsi);
3276 775720 : tree forcedname = gimple_get_lhs (stmt);
3277 775720 : if (forcedname == folded)
3278 : {
3279 : found = true;
3280 : break;
3281 : }
3282 : }
3283 775720 : if (! found)
3284 : {
3285 0 : gimple_seq_discard (forced_stmts);
3286 0 : return folded;
3287 : }
3288 775720 : gcc_assert (TREE_CODE (folded) == SSA_NAME);
3289 :
3290 : /* If we have any intermediate expressions to the value sets, add them
3291 : to the value sets and chain them in the instruction stream. */
3292 775720 : if (forced_stmts)
3293 : {
3294 775720 : gsi = gsi_start (forced_stmts);
3295 1551937 : for (; !gsi_end_p (gsi); gsi_next (&gsi))
3296 : {
3297 776217 : gimple *stmt = gsi_stmt (gsi);
3298 776217 : tree forcedname = gimple_get_lhs (stmt);
3299 776217 : pre_expr nameexpr;
3300 :
3301 776217 : if (forcedname != folded)
3302 : {
3303 497 : vn_ssa_aux_t vn_info = VN_INFO (forcedname);
3304 497 : vn_info->valnum = forcedname;
3305 497 : vn_info->value_id = get_next_value_id ();
3306 497 : nameexpr = get_or_alloc_expr_for_name (forcedname);
3307 497 : add_to_value (vn_info->value_id, nameexpr);
3308 497 : if (NEW_SETS (block))
3309 497 : bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3310 497 : bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3311 : }
3312 :
3313 776217 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
3314 : }
3315 775720 : gimple_seq_add_seq (stmts, forced_stmts);
3316 : }
3317 :
3318 775720 : name = folded;
3319 :
3320 : /* Fold the last statement. */
3321 775720 : gsi = gsi_last (*stmts);
3322 775720 : if (fold_stmt_inplace (&gsi))
3323 209833 : update_stmt (gsi_stmt (gsi));
3324 :
3325 : /* Add a value number to the temporary.
3326 : The value may already exist in either NEW_SETS, or AVAIL_OUT, because
3327 : we are creating the expression by pieces, and this particular piece of
3328 : the expression may have been represented. There is no harm in replacing
3329 : here. */
3330 775720 : value_id = get_expr_value_id (expr);
3331 775720 : vn_ssa_aux_t vn_info = VN_INFO (name);
3332 775720 : vn_info->value_id = value_id;
3333 775720 : vn_info->valnum = vn_valnum_from_value_id (value_id);
3334 775720 : if (vn_info->valnum == NULL_TREE)
3335 244817 : vn_info->valnum = name;
3336 775720 : gcc_assert (vn_info->valnum != NULL_TREE);
3337 775720 : nameexpr = get_or_alloc_expr_for_name (name);
3338 775720 : add_to_value (value_id, nameexpr);
3339 775720 : if (NEW_SETS (block))
3340 544847 : bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3341 775720 : bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3342 :
3343 775720 : pre_stats.insertions++;
3344 775720 : if (dump_file && (dump_flags & TDF_DETAILS))
3345 : {
3346 18 : fprintf (dump_file, "Inserted ");
3347 36 : print_gimple_stmt (dump_file, gsi_stmt (gsi_last (*stmts)), 0);
3348 18 : fprintf (dump_file, " in predecessor %d (%04d)\n",
3349 : block->index, value_id);
3350 : }
3351 :
3352 : return name;
3353 : }
3354 :
3355 :
3356 : /* Insert the to-be-made-available values of expression EXPRNUM for each
3357 : predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3358 : merge the result with a phi node, given the same value number as
3359 : NODE. Return true if we have inserted new stuff. */
3360 :
3361 : static bool
3362 1957542 : insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3363 : vec<pre_expr> &avail)
3364 : {
3365 1957542 : pre_expr expr = expression_for_id (exprnum);
3366 1957542 : pre_expr newphi;
3367 1957542 : unsigned int val = get_expr_value_id (expr);
3368 1957542 : edge pred;
3369 1957542 : bool insertions = false;
3370 1957542 : bool nophi = false;
3371 1957542 : basic_block bprime;
3372 1957542 : pre_expr eprime;
3373 1957542 : edge_iterator ei;
3374 1957542 : tree type = get_expr_type (expr);
3375 1957542 : tree temp;
3376 1957542 : gphi *phi;
3377 :
3378 : /* Make sure we aren't creating an induction variable. */
3379 1957542 : if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3380 : {
3381 1624807 : bool firstinsideloop = false;
3382 1624807 : bool secondinsideloop = false;
3383 4874421 : firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3384 1624807 : EDGE_PRED (block, 0)->src);
3385 4874421 : secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3386 1624807 : EDGE_PRED (block, 1)->src);
3387 : /* Induction variables only have one edge inside the loop. */
3388 1624807 : if ((firstinsideloop ^ secondinsideloop)
3389 1548453 : && expr->kind != REFERENCE)
3390 : {
3391 1468067 : if (dump_file && (dump_flags & TDF_DETAILS))
3392 56 : fprintf (dump_file, "Skipping insertion of phi for partial "
3393 : "redundancy: Looks like an induction variable\n");
3394 : nophi = true;
3395 : }
3396 : }
3397 :
3398 : /* Make the necessary insertions. */
3399 6096828 : FOR_EACH_EDGE (pred, ei, block->preds)
3400 : {
3401 : /* When we are not inserting a PHI node do not bother inserting
3402 : into places that do not dominate the anticipated computations. */
3403 4139286 : if (nophi && !dominated_by_p (CDI_DOMINATORS, block, pred->src))
3404 1480663 : continue;
3405 2661269 : gimple_seq stmts = NULL;
3406 2661269 : tree builtexpr;
3407 2661269 : bprime = pred->src;
3408 2661269 : eprime = avail[pred->dest_idx];
3409 2661269 : builtexpr = create_expression_by_pieces (bprime, eprime,
3410 : &stmts, type);
3411 2661269 : gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3412 2661269 : if (!gimple_seq_empty_p (stmts))
3413 : {
3414 520055 : basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
3415 520055 : gcc_assert (! new_bb);
3416 : insertions = true;
3417 : }
3418 2661269 : if (!builtexpr)
3419 : {
3420 : /* We cannot insert a PHI node if we failed to insert
3421 : on one edge. */
3422 2646 : nophi = true;
3423 2646 : continue;
3424 : }
3425 2658623 : if (is_gimple_min_invariant (builtexpr))
3426 1400351 : avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
3427 : else
3428 1258272 : avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3429 : }
3430 : /* If we didn't want a phi node, and we made insertions, we still have
3431 : inserted new stuff, and thus return true. If we didn't want a phi node,
3432 : and didn't make insertions, we haven't added anything new, so return
3433 : false. */
3434 1957542 : if (nophi && insertions)
3435 : return true;
3436 1948811 : else if (nophi && !insertions)
3437 : return false;
3438 :
3439 : /* Now build a phi for the new variable. */
3440 486835 : temp = make_temp_ssa_name (type, NULL, "prephitmp");
3441 486835 : phi = create_phi_node (temp, block);
3442 :
3443 486835 : vn_ssa_aux_t vn_info = VN_INFO (temp);
3444 486835 : vn_info->value_id = val;
3445 486835 : vn_info->valnum = vn_valnum_from_value_id (val);
3446 486835 : if (vn_info->valnum == NULL_TREE)
3447 98857 : vn_info->valnum = temp;
3448 486835 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3449 1676503 : FOR_EACH_EDGE (pred, ei, block->preds)
3450 : {
3451 1189668 : pre_expr ae = avail[pred->dest_idx];
3452 1189668 : gcc_assert (get_expr_type (ae) == type
3453 : || useless_type_conversion_p (type, get_expr_type (ae)));
3454 1189668 : if (ae->kind == CONSTANT)
3455 186055 : add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3456 : pred, UNKNOWN_LOCATION);
3457 : else
3458 1003613 : add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3459 : }
3460 :
3461 486835 : newphi = get_or_alloc_expr_for_name (temp);
3462 486835 : add_to_value (val, newphi);
3463 :
3464 : /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3465 : this insertion, since we test for the existence of this value in PHI_GEN
3466 : before proceeding with the partial redundancy checks in insert_aux.
3467 :
3468 : The value may exist in AVAIL_OUT, in particular, it could be represented
3469 : by the expression we are trying to eliminate, in which case we want the
3470 : replacement to occur. If it's not existing in AVAIL_OUT, we want it
3471 : inserted there.
3472 :
3473 : Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3474 : this block, because if it did, it would have existed in our dominator's
3475 : AVAIL_OUT, and would have been skipped due to the full redundancy check.
3476 : */
3477 :
3478 486835 : bitmap_insert_into_set (PHI_GEN (block), newphi);
3479 486835 : bitmap_value_replace_in_set (AVAIL_OUT (block),
3480 : newphi);
3481 486835 : if (NEW_SETS (block))
3482 486835 : bitmap_insert_into_set (NEW_SETS (block), newphi);
3483 :
3484 : /* If we insert a PHI node for a conversion of another PHI node
3485 : in the same basic-block try to preserve range information.
3486 : This is important so that followup loop passes receive optimal
3487 : number of iteration analysis results. See PR61743. */
3488 486835 : if (expr->kind == NARY
3489 184943 : && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3490 55298 : && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3491 55123 : && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3492 45196 : && INTEGRAL_TYPE_P (type)
3493 44354 : && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3494 43295 : && (TYPE_PRECISION (type)
3495 43295 : >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3496 522346 : && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3497 : {
3498 22241 : int_range_max r;
3499 44482 : if (get_range_query (cfun)->range_of_expr (r, expr->u.nary->op[0])
3500 22241 : && !r.undefined_p ()
3501 22241 : && !r.varying_p ()
3502 44482 : && !wi::neg_p (r.lower_bound (), SIGNED)
3503 61011 : && !wi::neg_p (r.upper_bound (), SIGNED))
3504 : {
3505 : /* Just handle extension and sign-changes of all-positive ranges. */
3506 15796 : range_cast (r, type);
3507 15796 : set_range_info (temp, r);
3508 : }
3509 22241 : }
3510 :
3511 486835 : if (dump_file && (dump_flags & TDF_DETAILS))
3512 : {
3513 8 : fprintf (dump_file, "Created phi ");
3514 8 : print_gimple_stmt (dump_file, phi, 0);
3515 8 : fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3516 : }
3517 486835 : pre_stats.phis++;
3518 486835 : return true;
3519 : }
3520 :
3521 :
3522 :
3523 : /* Perform insertion of partially redundant or hoistable values.
3524 : For BLOCK, do the following:
3525 : 1. Propagate the NEW_SETS of the dominator into the current block.
3526 : If the block has multiple predecessors,
3527 : 2a. Iterate over the ANTIC expressions for the block to see if
3528 : any of them are partially redundant.
3529 : 2b. If so, insert them into the necessary predecessors to make
3530 : the expression fully redundant.
3531 : 2c. Insert a new PHI merging the values of the predecessors.
3532 : 2d. Insert the new PHI, and the new expressions, into the
3533 : NEW_SETS set.
3534 : If the block has multiple successors,
3535 : 3a. Iterate over the ANTIC values for the block to see if
3536 : any of them are good candidates for hoisting.
3537 : 3b. If so, insert expressions computing the values in BLOCK,
3538 : and add the new expressions into the NEW_SETS set.
3539 : 4. Recursively call ourselves on the dominator children of BLOCK.
3540 :
3541 : Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3542 : do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3543 : done in do_hoist_insertion.
3544 : */
3545 :
3546 : static bool
3547 3745726 : do_pre_regular_insertion (basic_block block, basic_block dom,
3548 : vec<pre_expr> exprs)
3549 : {
3550 3745726 : bool new_stuff = false;
3551 3745726 : pre_expr expr;
3552 3745726 : auto_vec<pre_expr, 2> avail;
3553 3745726 : int i;
3554 :
3555 3745726 : avail.safe_grow (EDGE_COUNT (block->preds), true);
3556 :
3557 29758498 : FOR_EACH_VEC_ELT (exprs, i, expr)
3558 : {
3559 22267046 : if (expr->kind == NARY
3560 22267046 : || expr->kind == REFERENCE)
3561 : {
3562 12534150 : unsigned int val;
3563 12534150 : bool by_some = false;
3564 12534150 : bool cant_insert = false;
3565 12534150 : bool all_same = true;
3566 12534150 : unsigned num_inserts = 0;
3567 12534150 : unsigned num_const = 0;
3568 12534150 : pre_expr first_s = NULL;
3569 12534150 : edge pred;
3570 12534150 : basic_block bprime;
3571 12534150 : pre_expr eprime = NULL;
3572 12534150 : edge_iterator ei;
3573 12534150 : pre_expr edoubleprime = NULL;
3574 12534150 : bool do_insertion = false;
3575 :
3576 12534150 : val = get_expr_value_id (expr);
3577 25068300 : if (bitmap_set_contains_value (PHI_GEN (block), val))
3578 1050070 : continue;
3579 11735139 : if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3580 : {
3581 251059 : if (dump_file && (dump_flags & TDF_DETAILS))
3582 : {
3583 7 : fprintf (dump_file, "Found fully redundant value: ");
3584 7 : print_pre_expr (dump_file, expr);
3585 7 : fprintf (dump_file, "\n");
3586 : }
3587 251059 : continue;
3588 : }
3589 :
3590 37490693 : FOR_EACH_EDGE (pred, ei, block->preds)
3591 : {
3592 26007473 : unsigned int vprime;
3593 :
3594 : /* We should never run insertion for the exit block
3595 : and so not come across fake pred edges. */
3596 26007473 : gcc_assert (!(pred->flags & EDGE_FAKE));
3597 26007473 : bprime = pred->src;
3598 : /* We are looking at ANTIC_OUT of bprime. */
3599 26007473 : eprime = phi_translate (NULL, expr, ANTIC_IN (block), NULL, pred);
3600 :
3601 : /* eprime will generally only be NULL if the
3602 : value of the expression, translated
3603 : through the PHI for this predecessor, is
3604 : undefined. If that is the case, we can't
3605 : make the expression fully redundant,
3606 : because its value is undefined along a
3607 : predecessor path. We can thus break out
3608 : early because it doesn't matter what the
3609 : rest of the results are. */
3610 26007473 : if (eprime == NULL)
3611 : {
3612 860 : avail[pred->dest_idx] = NULL;
3613 860 : cant_insert = true;
3614 860 : break;
3615 : }
3616 :
3617 26006613 : vprime = get_expr_value_id (eprime);
3618 26006613 : edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3619 : vprime);
3620 26006613 : if (edoubleprime == NULL)
3621 : {
3622 23374249 : avail[pred->dest_idx] = eprime;
3623 23374249 : all_same = false;
3624 23374249 : num_inserts++;
3625 : }
3626 : else
3627 : {
3628 2632364 : avail[pred->dest_idx] = edoubleprime;
3629 2632364 : by_some = true;
3630 2632364 : if (edoubleprime->kind == CONSTANT)
3631 1744426 : num_const++;
3632 : /* We want to perform insertions to remove a redundancy on
3633 : a path in the CFG we want to optimize for speed. */
3634 2632364 : if (optimize_edge_for_speed_p (pred))
3635 2197909 : do_insertion = true;
3636 2632364 : if (first_s == NULL)
3637 : first_s = edoubleprime;
3638 293469 : else if (!pre_expr_d::equal (first_s, edoubleprime))
3639 224709 : all_same = false;
3640 : }
3641 : }
3642 : /* If we can insert it, it's not the same value
3643 : already existing along every predecessor, and
3644 : it's defined by some predecessor, it is
3645 : partially redundant. */
3646 11484080 : if (!cant_insert && !all_same && by_some)
3647 : {
3648 : /* If the expression is redundant on all edges and we need
3649 : to at most insert one copy from a constant do the PHI
3650 : insertion even when not optimizing a path that's to be
3651 : optimized for speed. */
3652 2336458 : if (num_inserts == 0 && num_const <= 1)
3653 : do_insertion = true;
3654 2191081 : if (!do_insertion)
3655 : {
3656 385153 : if (dump_file && (dump_flags & TDF_DETAILS))
3657 : {
3658 0 : fprintf (dump_file, "Skipping partial redundancy for "
3659 : "expression ");
3660 0 : print_pre_expr (dump_file, expr);
3661 0 : fprintf (dump_file, " (%04d), no redundancy on to be "
3662 : "optimized for speed edge\n", val);
3663 : }
3664 : }
3665 1951305 : else if (dbg_cnt (treepre_insert))
3666 : {
3667 1951305 : if (dump_file && (dump_flags & TDF_DETAILS))
3668 : {
3669 64 : fprintf (dump_file, "Found partial redundancy for "
3670 : "expression ");
3671 64 : print_pre_expr (dump_file, expr);
3672 64 : fprintf (dump_file, " (%04d)\n",
3673 : get_expr_value_id (expr));
3674 : }
3675 1951305 : if (insert_into_preds_of_block (block,
3676 : get_expression_id (expr),
3677 : avail))
3678 11484080 : new_stuff = true;
3679 : }
3680 : }
3681 : /* If all edges produce the same value and that value is
3682 : an invariant, then the PHI has the same value on all
3683 : edges. Note this. */
3684 9147622 : else if (!cant_insert
3685 9147622 : && all_same
3686 9147622 : && (edoubleprime->kind != NAME
3687 831 : || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI
3688 : (PRE_EXPR_NAME (edoubleprime))))
3689 : {
3690 2411 : gcc_assert (edoubleprime->kind == CONSTANT
3691 : || edoubleprime->kind == NAME);
3692 :
3693 2411 : tree temp = make_temp_ssa_name (get_expr_type (expr),
3694 : NULL, "pretmp");
3695 2411 : gassign *assign
3696 2411 : = gimple_build_assign (temp,
3697 2411 : edoubleprime->kind == CONSTANT ?
3698 : PRE_EXPR_CONSTANT (edoubleprime) :
3699 : PRE_EXPR_NAME (edoubleprime));
3700 2411 : gimple_stmt_iterator gsi = gsi_after_labels (block);
3701 2411 : gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3702 :
3703 2411 : vn_ssa_aux_t vn_info = VN_INFO (temp);
3704 2411 : vn_info->value_id = val;
3705 2411 : vn_info->valnum = vn_valnum_from_value_id (val);
3706 2411 : if (vn_info->valnum == NULL_TREE)
3707 523 : vn_info->valnum = temp;
3708 2411 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3709 2411 : pre_expr newe = get_or_alloc_expr_for_name (temp);
3710 2411 : add_to_value (val, newe);
3711 2411 : bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3712 2411 : bitmap_insert_into_set (NEW_SETS (block), newe);
3713 2411 : bitmap_insert_into_set (PHI_GEN (block), newe);
3714 : }
3715 : }
3716 : }
3717 :
3718 3745726 : return new_stuff;
3719 3745726 : }
3720 :
3721 :
3722 : /* Perform insertion for partially anticipatable expressions. There
3723 : is only one case we will perform insertion for these. This case is
3724 : if the expression is partially anticipatable, and fully available.
3725 : In this case, we know that putting it earlier will enable us to
3726 : remove the later computation. */
3727 :
3728 : static bool
3729 334228 : do_pre_partial_partial_insertion (basic_block block, basic_block dom,
3730 : vec<pre_expr> exprs)
3731 : {
3732 334228 : bool new_stuff = false;
3733 334228 : pre_expr expr;
3734 334228 : auto_vec<pre_expr, 2> avail;
3735 334228 : int i;
3736 :
3737 334228 : avail.safe_grow (EDGE_COUNT (block->preds), true);
3738 :
3739 3600402 : FOR_EACH_VEC_ELT (exprs, i, expr)
3740 : {
3741 2931946 : if (expr->kind == NARY
3742 2931946 : || expr->kind == REFERENCE)
3743 : {
3744 2219753 : unsigned int val;
3745 2219753 : bool by_all = true;
3746 2219753 : bool cant_insert = false;
3747 2219753 : edge pred;
3748 2219753 : basic_block bprime;
3749 2219753 : pre_expr eprime = NULL;
3750 2219753 : edge_iterator ei;
3751 :
3752 2219753 : val = get_expr_value_id (expr);
3753 4439506 : if (bitmap_set_contains_value (PHI_GEN (block), val))
3754 57409 : continue;
3755 2210458 : if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3756 48114 : continue;
3757 :
3758 2237872 : FOR_EACH_EDGE (pred, ei, block->preds)
3759 : {
3760 2227919 : unsigned int vprime;
3761 2227919 : pre_expr edoubleprime;
3762 :
3763 : /* We should never run insertion for the exit block
3764 : and so not come across fake pred edges. */
3765 2227919 : gcc_assert (!(pred->flags & EDGE_FAKE));
3766 2227919 : bprime = pred->src;
3767 4455838 : eprime = phi_translate (NULL, expr, ANTIC_IN (block),
3768 2227919 : PA_IN (block), pred);
3769 :
3770 : /* eprime will generally only be NULL if the
3771 : value of the expression, translated
3772 : through the PHI for this predecessor, is
3773 : undefined. If that is the case, we can't
3774 : make the expression fully redundant,
3775 : because its value is undefined along a
3776 : predecessor path. We can thus break out
3777 : early because it doesn't matter what the
3778 : rest of the results are. */
3779 2227919 : if (eprime == NULL)
3780 : {
3781 22 : avail[pred->dest_idx] = NULL;
3782 22 : cant_insert = true;
3783 22 : break;
3784 : }
3785 :
3786 2227897 : vprime = get_expr_value_id (eprime);
3787 2227897 : edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3788 2227897 : avail[pred->dest_idx] = edoubleprime;
3789 2227897 : if (edoubleprime == NULL)
3790 : {
3791 : by_all = false;
3792 : break;
3793 : }
3794 : }
3795 :
3796 : /* If we can insert it, it's not the same value
3797 : already existing along every predecessor, and
3798 : it's defined by some predecessor, it is
3799 : partially redundant. */
3800 2162344 : if (!cant_insert && by_all)
3801 : {
3802 9953 : edge succ;
3803 9953 : bool do_insertion = false;
3804 :
3805 : /* Insert only if we can remove a later expression on a path
3806 : that we want to optimize for speed.
3807 : The phi node that we will be inserting in BLOCK is not free,
3808 : and inserting it for the sake of !optimize_for_speed successor
3809 : may cause regressions on the speed path. */
3810 27561 : FOR_EACH_EDGE (succ, ei, block->succs)
3811 : {
3812 17608 : if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3813 17608 : || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3814 : {
3815 9264 : if (optimize_edge_for_speed_p (succ))
3816 17608 : do_insertion = true;
3817 : }
3818 : }
3819 :
3820 9953 : if (!do_insertion)
3821 : {
3822 3716 : if (dump_file && (dump_flags & TDF_DETAILS))
3823 : {
3824 0 : fprintf (dump_file, "Skipping partial partial redundancy "
3825 : "for expression ");
3826 0 : print_pre_expr (dump_file, expr);
3827 0 : fprintf (dump_file, " (%04d), not (partially) anticipated "
3828 : "on any to be optimized for speed edges\n", val);
3829 : }
3830 : }
3831 6237 : else if (dbg_cnt (treepre_insert))
3832 : {
3833 6237 : pre_stats.pa_insert++;
3834 6237 : if (dump_file && (dump_flags & TDF_DETAILS))
3835 : {
3836 0 : fprintf (dump_file, "Found partial partial redundancy "
3837 : "for expression ");
3838 0 : print_pre_expr (dump_file, expr);
3839 0 : fprintf (dump_file, " (%04d)\n",
3840 : get_expr_value_id (expr));
3841 : }
3842 6237 : if (insert_into_preds_of_block (block,
3843 : get_expression_id (expr),
3844 : avail))
3845 9953 : new_stuff = true;
3846 : }
3847 : }
3848 : }
3849 : }
3850 :
3851 334228 : return new_stuff;
3852 334228 : }
3853 :
3854 : /* Insert expressions in BLOCK to compute hoistable values up.
3855 : Return TRUE if something was inserted, otherwise return FALSE.
3856 : The caller has to make sure that BLOCK has at least two successors. */
3857 :
3858 : static bool
3859 4823891 : do_hoist_insertion (basic_block block)
3860 : {
3861 4823891 : edge e;
3862 4823891 : edge_iterator ei;
3863 4823891 : bool new_stuff = false;
3864 4823891 : unsigned i;
3865 4823891 : gimple_stmt_iterator last;
3866 :
3867 : /* At least two successors, or else... */
3868 4823891 : gcc_assert (EDGE_COUNT (block->succs) >= 2);
3869 :
3870 : /* Check that all successors of BLOCK are dominated by block.
3871 : We could use dominated_by_p() for this, but actually there is a much
3872 : quicker check: any successor that is dominated by BLOCK can't have
3873 : more than one predecessor edge. */
3874 14561808 : FOR_EACH_EDGE (e, ei, block->succs)
3875 9745375 : if (! single_pred_p (e->dest))
3876 : return false;
3877 :
3878 : /* Determine the insertion point. If we cannot safely insert before
3879 : the last stmt if we'd have to, bail out. */
3880 4816433 : last = gsi_last_bb (block);
3881 4816433 : if (!gsi_end_p (last)
3882 4815984 : && !is_ctrl_stmt (gsi_stmt (last))
3883 5403809 : && stmt_ends_bb_p (gsi_stmt (last)))
3884 : return false;
3885 :
3886 : /* We have multiple successors, compute ANTIC_OUT by taking the intersection
3887 : of all of ANTIC_IN translating through PHI nodes. Track the union
3888 : of the expression sets so we can pick a representative that is
3889 : fully generatable out of hoistable expressions. */
3890 4229636 : bitmap_set_t ANTIC_OUT = bitmap_set_new ();
3891 4229636 : bool first = true;
3892 12787582 : FOR_EACH_EDGE (e, ei, block->succs)
3893 : {
3894 8557946 : if (first)
3895 : {
3896 4229636 : phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
3897 4229636 : first = false;
3898 : }
3899 4328310 : else if (!gimple_seq_empty_p (phi_nodes (e->dest)))
3900 : {
3901 7 : bitmap_set_t tmp = bitmap_set_new ();
3902 7 : phi_translate_set (tmp, ANTIC_IN (e->dest), e);
3903 7 : bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
3904 7 : bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
3905 7 : bitmap_set_free (tmp);
3906 : }
3907 : else
3908 : {
3909 4328303 : bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
3910 4328303 : bitmap_ior_into (&ANTIC_OUT->expressions,
3911 4328303 : &ANTIC_IN (e->dest)->expressions);
3912 : }
3913 : }
3914 :
3915 : /* Compute the set of hoistable expressions from ANTIC_OUT. First compute
3916 : hoistable values. */
3917 4229636 : bitmap_set hoistable_set;
3918 :
3919 : /* A hoistable value must be in ANTIC_OUT(block)
3920 : but not in AVAIL_OUT(BLOCK). */
3921 4229636 : bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
3922 4229636 : bitmap_and_compl (&hoistable_set.values,
3923 4229636 : &ANTIC_OUT->values, &AVAIL_OUT (block)->values);
3924 :
3925 : /* Short-cut for a common case: hoistable_set is empty. */
3926 4229636 : if (bitmap_empty_p (&hoistable_set.values))
3927 : {
3928 3468346 : bitmap_set_free (ANTIC_OUT);
3929 3468346 : return false;
3930 : }
3931 :
3932 : /* Compute which of the hoistable values is in AVAIL_OUT of
3933 : at least one of the successors of BLOCK. */
3934 761290 : bitmap_head availout_in_some;
3935 761290 : bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
3936 2300132 : FOR_EACH_EDGE (e, ei, block->succs)
3937 : /* Do not consider expressions solely because their availability
3938 : on loop exits. They'd be ANTIC-IN throughout the whole loop
3939 : and thus effectively hoisted across loops by combination of
3940 : PRE and hoisting. */
3941 1538842 : if (! loop_exit_edge_p (block->loop_father, e))
3942 1371879 : bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
3943 1371879 : &AVAIL_OUT (e->dest)->values);
3944 761290 : bitmap_clear (&hoistable_set.values);
3945 :
3946 : /* Short-cut for a common case: availout_in_some is empty. */
3947 761290 : if (bitmap_empty_p (&availout_in_some))
3948 : {
3949 616479 : bitmap_set_free (ANTIC_OUT);
3950 616479 : return false;
3951 : }
3952 :
3953 : /* Hack hoistable_set in-place so we can use sorted_array_from_bitmap_set. */
3954 144811 : bitmap_move (&hoistable_set.values, &availout_in_some);
3955 144811 : hoistable_set.expressions = ANTIC_OUT->expressions;
3956 :
3957 : /* Now finally construct the topological-ordered expression set. */
3958 144811 : vec<pre_expr> exprs = sorted_array_from_bitmap_set (&hoistable_set, true);
3959 :
3960 : /* If there are candidate values for hoisting, insert expressions
3961 : strategically to make the hoistable expressions fully redundant. */
3962 144811 : pre_expr expr;
3963 520525 : FOR_EACH_VEC_ELT (exprs, i, expr)
3964 : {
3965 : /* While we try to sort expressions topologically above the
3966 : sorting doesn't work out perfectly. Catch expressions we
3967 : already inserted. */
3968 230903 : unsigned int value_id = get_expr_value_id (expr);
3969 461806 : if (bitmap_set_contains_value (AVAIL_OUT (block), value_id))
3970 : {
3971 0 : if (dump_file && (dump_flags & TDF_DETAILS))
3972 : {
3973 0 : fprintf (dump_file,
3974 : "Already inserted expression for ");
3975 0 : print_pre_expr (dump_file, expr);
3976 0 : fprintf (dump_file, " (%04d)\n", value_id);
3977 : }
3978 30 : continue;
3979 : }
3980 :
3981 : /* If we end up with a punned expression representation and this
3982 : happens to be a float typed one give up - we can't know for
3983 : sure whether all paths perform the floating-point load we are
3984 : about to insert and on some targets this can cause correctness
3985 : issues. See PR88240. */
3986 230903 : if (expr->kind == REFERENCE
3987 102502 : && PRE_EXPR_REFERENCE (expr)->punned
3988 230903 : && FLOAT_TYPE_P (get_expr_type (expr)))
3989 0 : continue;
3990 :
3991 : /* Only hoist if the full expression is available for hoisting.
3992 : This avoids hoisting values that are not common and for
3993 : example evaluate an expression that's not valid to evaluate
3994 : unconditionally (PR112310). */
3995 230903 : if (!valid_in_sets (&hoistable_set, AVAIL_OUT (block), expr))
3996 10 : continue;
3997 :
3998 : /* OK, we should hoist this value. Perform the transformation. */
3999 230893 : pre_stats.hoist_insert++;
4000 230893 : if (dump_file && (dump_flags & TDF_DETAILS))
4001 : {
4002 2 : fprintf (dump_file,
4003 : "Inserting expression in block %d for code hoisting: ",
4004 : block->index);
4005 2 : print_pre_expr (dump_file, expr);
4006 2 : fprintf (dump_file, " (%04d)\n", value_id);
4007 : }
4008 :
4009 230893 : gimple_seq stmts = NULL;
4010 230893 : tree res = create_expression_by_pieces (block, expr, &stmts,
4011 : get_expr_type (expr));
4012 :
4013 : /* Do not return true if expression creation ultimately
4014 : did not insert any statements. */
4015 230893 : if (gimple_seq_empty_p (stmts))
4016 : res = NULL_TREE;
4017 : else
4018 : {
4019 230873 : if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
4020 230873 : gsi_insert_seq_before (&last, stmts, GSI_SAME_STMT);
4021 : else
4022 0 : gsi_insert_seq_after (&last, stmts, GSI_NEW_STMT);
4023 : }
4024 :
4025 : /* Make sure to not return true if expression creation ultimately
4026 : failed but also make sure to insert any stmts produced as they
4027 : are tracked in inserted_exprs. */
4028 230873 : if (! res)
4029 20 : continue;
4030 :
4031 230873 : new_stuff = true;
4032 : }
4033 :
4034 144811 : exprs.release ();
4035 144811 : bitmap_clear (&hoistable_set.values);
4036 144811 : bitmap_set_free (ANTIC_OUT);
4037 :
4038 144811 : return new_stuff;
4039 : }
4040 :
4041 : /* Perform insertion of partially redundant and hoistable values. */
4042 :
4043 : static void
4044 981520 : insert (void)
4045 : {
4046 981520 : basic_block bb;
4047 :
4048 16376537 : FOR_ALL_BB_FN (bb, cfun)
4049 15395017 : NEW_SETS (bb) = bitmap_set_new ();
4050 :
4051 981520 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
4052 981520 : int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun) + 1);
4053 981520 : int rpo_num = pre_and_rev_post_order_compute (NULL, rpo, false);
4054 15395017 : for (int i = 0; i < rpo_num; ++i)
4055 13431977 : bb_rpo[rpo[i]] = i;
4056 :
4057 : int num_iterations = 0;
4058 1034013 : bool changed;
4059 1034013 : do
4060 : {
4061 1034013 : num_iterations++;
4062 1034013 : if (dump_file && dump_flags & TDF_DETAILS)
4063 18 : fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
4064 :
4065 1034013 : changed = false;
4066 18764191 : for (int idx = 0; idx < rpo_num; ++idx)
4067 : {
4068 17730178 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
4069 17730178 : basic_block dom = get_immediate_dominator (CDI_DOMINATORS, block);
4070 17730178 : if (dom)
4071 : {
4072 17730178 : unsigned i;
4073 17730178 : bitmap_iterator bi;
4074 17730178 : bitmap_set_t newset;
4075 :
4076 : /* First, update the AVAIL_OUT set with anything we may have
4077 : inserted higher up in the dominator tree. */
4078 17730178 : newset = NEW_SETS (dom);
4079 :
4080 : /* Note that we need to value_replace both NEW_SETS, and
4081 : AVAIL_OUT. For both the case of NEW_SETS, the value may be
4082 : represented by some non-simple expression here that we want
4083 : to replace it with. */
4084 17730178 : bool avail_out_changed = false;
4085 33640135 : FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
4086 : {
4087 15909957 : pre_expr expr = expression_for_id (i);
4088 15909957 : bitmap_value_replace_in_set (NEW_SETS (block), expr);
4089 15909957 : avail_out_changed
4090 15909957 : |= bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
4091 : }
4092 : /* We need to iterate if AVAIL_OUT of an already processed
4093 : block source changed. */
4094 17730178 : if (avail_out_changed && !changed)
4095 : {
4096 1728074 : edge_iterator ei;
4097 1728074 : edge e;
4098 4113606 : FOR_EACH_EDGE (e, ei, block->succs)
4099 2385532 : if (e->dest->index != EXIT_BLOCK
4100 2277282 : && bb_rpo[e->dest->index] < idx)
4101 2385532 : changed = true;
4102 : }
4103 :
4104 : /* Insert expressions for partial redundancies. */
4105 35459518 : if (flag_tree_pre && !single_pred_p (block))
4106 : {
4107 3473990 : vec<pre_expr> exprs
4108 3473990 : = sorted_array_from_bitmap_set (ANTIC_IN (block), true);
4109 : /* Sorting is not perfect, iterate locally. */
4110 7219716 : while (do_pre_regular_insertion (block, dom, exprs))
4111 : ;
4112 3473990 : exprs.release ();
4113 3473990 : if (do_partial_partial)
4114 : {
4115 331257 : exprs = sorted_array_from_bitmap_set (PA_IN (block),
4116 : true);
4117 665485 : while (do_pre_partial_partial_insertion (block, dom,
4118 : exprs))
4119 : ;
4120 331257 : exprs.release ();
4121 : }
4122 : }
4123 : }
4124 : }
4125 :
4126 : /* Clear the NEW sets before the next iteration. We have already
4127 : fully propagated its contents. */
4128 1034013 : if (changed)
4129 4455680 : FOR_ALL_BB_FN (bb, cfun)
4130 8806374 : bitmap_set_free (NEW_SETS (bb));
4131 : }
4132 : while (changed);
4133 :
4134 981520 : statistics_histogram_event (cfun, "insert iterations", num_iterations);
4135 :
4136 : /* AVAIL_OUT is not needed after insertion so we don't have to
4137 : propagate NEW_SETS from hoist insertion. */
4138 16376537 : FOR_ALL_BB_FN (bb, cfun)
4139 : {
4140 15395017 : bitmap_set_free (NEW_SETS (bb));
4141 15395017 : bitmap_set_pool.remove (NEW_SETS (bb));
4142 15395017 : NEW_SETS (bb) = NULL;
4143 : }
4144 :
4145 : /* Insert expressions for hoisting. Do a backward walk here since
4146 : inserting into BLOCK exposes new opportunities in its predecessors.
4147 : Since PRE and hoist insertions can cause back-to-back iteration
4148 : and we are interested in PRE insertion exposed hoisting opportunities
4149 : but not in hoisting exposed PRE ones do hoist insertion only after
4150 : PRE insertion iteration finished and do not iterate it. */
4151 981520 : if (flag_code_hoisting)
4152 14412948 : for (int idx = rpo_num - 1; idx >= 0; --idx)
4153 : {
4154 13431481 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
4155 18255372 : if (EDGE_COUNT (block->succs) >= 2)
4156 4823891 : changed |= do_hoist_insertion (block);
4157 : }
4158 :
4159 981520 : free (rpo);
4160 981520 : free (bb_rpo);
4161 981520 : }
4162 :
4163 :
4164 : /* Compute the AVAIL set for all basic blocks.
4165 :
4166 : This function performs value numbering of the statements in each basic
4167 : block. The AVAIL sets are built from information we glean while doing
4168 : this value numbering, since the AVAIL sets contain only one entry per
4169 : value.
4170 :
4171 : AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
4172 : AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
4173 :
4174 : static void
4175 981520 : compute_avail (function *fun)
4176 : {
4177 :
4178 981520 : basic_block block, son;
4179 981520 : basic_block *worklist;
4180 981520 : size_t sp = 0;
4181 981520 : unsigned i;
4182 981520 : tree name;
4183 :
4184 : /* We pretend that default definitions are defined in the entry block.
4185 : This includes function arguments and the static chain decl. */
4186 48159452 : FOR_EACH_SSA_NAME (i, name, fun)
4187 : {
4188 34319396 : pre_expr e;
4189 34319396 : if (!SSA_NAME_IS_DEFAULT_DEF (name)
4190 2972108 : || has_zero_uses (name)
4191 36742523 : || virtual_operand_p (name))
4192 32876983 : continue;
4193 :
4194 1442413 : e = get_or_alloc_expr_for_name (name);
4195 1442413 : add_to_value (get_expr_value_id (e), e);
4196 1442413 : bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun)), e);
4197 1442413 : bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun)),
4198 : e);
4199 : }
4200 :
4201 981520 : if (dump_file && (dump_flags & TDF_DETAILS))
4202 : {
4203 14 : print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun)),
4204 : "tmp_gen", ENTRY_BLOCK);
4205 14 : print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun)),
4206 : "avail_out", ENTRY_BLOCK);
4207 : }
4208 :
4209 : /* Allocate the worklist. */
4210 981520 : worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
4211 :
4212 : /* Seed the algorithm by putting the dominator children of the entry
4213 : block on the worklist. */
4214 981520 : for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (fun));
4215 1963040 : son;
4216 981520 : son = next_dom_son (CDI_DOMINATORS, son))
4217 981520 : worklist[sp++] = son;
4218 :
4219 1963040 : BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (fun))
4220 981520 : = ssa_default_def (fun, gimple_vop (fun));
4221 :
4222 : /* Loop until the worklist is empty. */
4223 14413497 : while (sp)
4224 : {
4225 13431977 : gimple *stmt;
4226 13431977 : basic_block dom;
4227 :
4228 : /* Pick a block from the worklist. */
4229 13431977 : block = worklist[--sp];
4230 13431977 : vn_context_bb = block;
4231 :
4232 : /* Initially, the set of available values in BLOCK is that of
4233 : its immediate dominator. */
4234 13431977 : dom = get_immediate_dominator (CDI_DOMINATORS, block);
4235 13431977 : if (dom)
4236 : {
4237 13431977 : bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
4238 13431977 : BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
4239 : }
4240 :
4241 : /* Generate values for PHI nodes. */
4242 17331523 : for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
4243 3899546 : gsi_next (&gsi))
4244 : {
4245 3899546 : tree result = gimple_phi_result (gsi.phi ());
4246 :
4247 : /* We have no need for virtual phis, as they don't represent
4248 : actual computations. */
4249 7799092 : if (virtual_operand_p (result))
4250 : {
4251 1774032 : BB_LIVE_VOP_ON_EXIT (block) = result;
4252 1774032 : continue;
4253 : }
4254 :
4255 2125514 : pre_expr e = get_or_alloc_expr_for_name (result);
4256 2125514 : add_to_value (get_expr_value_id (e), e);
4257 2125514 : bitmap_value_insert_into_set (AVAIL_OUT (block), e);
4258 2125514 : bitmap_insert_into_set (PHI_GEN (block), e);
4259 : }
4260 :
4261 13431977 : BB_MAY_NOTRETURN (block) = 0;
4262 :
4263 : /* Now compute value numbers and populate value sets with all
4264 : the expressions computed in BLOCK. */
4265 13431977 : bool set_bb_may_notreturn = false;
4266 113509133 : for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
4267 86645179 : gsi_next (&gsi))
4268 : {
4269 86645179 : ssa_op_iter iter;
4270 86645179 : tree op;
4271 :
4272 86645179 : stmt = gsi_stmt (gsi);
4273 :
4274 86645179 : if (set_bb_may_notreturn)
4275 : {
4276 2769586 : BB_MAY_NOTRETURN (block) = 1;
4277 2769586 : set_bb_may_notreturn = false;
4278 : }
4279 :
4280 : /* Cache whether the basic-block has any non-visible side-effect
4281 : or control flow.
4282 : If this isn't a call or it is the last stmt in the
4283 : basic-block then the CFG represents things correctly. */
4284 86645179 : if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
4285 : {
4286 : /* Non-looping const functions always return normally.
4287 : Otherwise the call might not return or have side-effects
4288 : that forbids hoisting possibly trapping expressions
4289 : before it. */
4290 3904973 : int flags = gimple_call_flags (stmt);
4291 3904973 : if (!(flags & (ECF_CONST|ECF_PURE))
4292 594785 : || (flags & ECF_LOOPING_CONST_OR_PURE)
4293 4472809 : || stmt_can_throw_external (fun, stmt))
4294 : /* Defer setting of BB_MAY_NOTRETURN to avoid it
4295 : influencing the processing of the call itself. */
4296 : set_bb_may_notreturn = true;
4297 : }
4298 :
4299 101852481 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
4300 : {
4301 15207302 : pre_expr e = get_or_alloc_expr_for_name (op);
4302 15207302 : add_to_value (get_expr_value_id (e), e);
4303 15207302 : bitmap_insert_into_set (TMP_GEN (block), e);
4304 15207302 : bitmap_value_insert_into_set (AVAIL_OUT (block), e);
4305 : }
4306 :
4307 114072698 : if (gimple_vdef (stmt))
4308 12150227 : BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
4309 :
4310 86645179 : if (gimple_has_side_effects (stmt)
4311 80252850 : || stmt_could_throw_p (fun, stmt)
4312 165734837 : || is_gimple_debug (stmt))
4313 81308574 : continue;
4314 :
4315 48002927 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
4316 : {
4317 22645094 : if (ssa_undefined_value_p (op))
4318 59141 : continue;
4319 22585953 : pre_expr e = get_or_alloc_expr_for_name (op);
4320 22585953 : bitmap_value_insert_into_set (EXP_GEN (block), e);
4321 : }
4322 :
4323 25357833 : switch (gimple_code (stmt))
4324 : {
4325 956508 : case GIMPLE_RETURN:
4326 956508 : continue;
4327 :
4328 565840 : case GIMPLE_CALL:
4329 565840 : {
4330 565840 : vn_reference_t ref;
4331 565840 : vn_reference_s ref1;
4332 565840 : pre_expr result = NULL;
4333 :
4334 565840 : vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
4335 : /* There is no point to PRE a call without a value. */
4336 565840 : if (!ref || !ref->result)
4337 32765 : continue;
4338 :
4339 : /* If the value of the call is not invalidated in
4340 : this block until it is computed, add the expression
4341 : to EXP_GEN. */
4342 533075 : if ((!gimple_vuse (stmt)
4343 305808 : || gimple_code
4344 305808 : (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
4345 278606 : || gimple_bb (SSA_NAME_DEF_STMT
4346 : (gimple_vuse (stmt))) != block)
4347 : /* If the REFERENCE traps and there was a preceding
4348 : point in the block that might not return avoid
4349 : adding the reference to EXP_GEN. */
4350 782153 : && (!BB_MAY_NOTRETURN (block)
4351 10671 : || !vn_reference_may_trap (ref)))
4352 : {
4353 465674 : result = get_or_alloc_expr_for_reference
4354 465674 : (ref, ref->value_id, gimple_location (stmt));
4355 465674 : add_to_value (get_expr_value_id (result), result);
4356 465674 : bitmap_value_insert_into_set (EXP_GEN (block), result);
4357 : }
4358 533075 : continue;
4359 533075 : }
4360 :
4361 18498880 : case GIMPLE_ASSIGN:
4362 18498880 : {
4363 18498880 : pre_expr result = NULL;
4364 18498880 : switch (vn_get_stmt_kind (stmt))
4365 : {
4366 7692555 : case VN_NARY:
4367 7692555 : {
4368 7692555 : enum tree_code code = gimple_assign_rhs_code (stmt);
4369 7692555 : vn_nary_op_t nary;
4370 :
4371 : /* COND_EXPR is awkward in that it contains an
4372 : embedded complex expression.
4373 : Don't even try to shove it through PRE. */
4374 7692555 : if (code == COND_EXPR)
4375 144618 : continue;
4376 :
4377 7688415 : vn_nary_op_lookup_stmt (stmt, &nary);
4378 7688415 : if (!nary || nary->predicated_values)
4379 110622 : continue;
4380 :
4381 7577793 : unsigned value_id = nary->value_id;
4382 7577793 : if (value_id_constant_p (value_id))
4383 0 : continue;
4384 :
4385 : /* Record the un-valueized expression for EXP_GEN. */
4386 7577793 : nary = XALLOCAVAR (struct vn_nary_op_s,
4387 : sizeof_vn_nary_op
4388 : (vn_nary_length_from_stmt (stmt)));
4389 7577793 : init_vn_nary_op_from_stmt (nary, as_a <gassign *> (stmt));
4390 :
4391 : /* If the NARY traps and there was a preceding
4392 : point in the block that might not return avoid
4393 : adding the nary to EXP_GEN. */
4394 7607649 : if (BB_MAY_NOTRETURN (block)
4395 7577793 : && vn_nary_may_trap (nary))
4396 29856 : continue;
4397 :
4398 7547937 : result = get_or_alloc_expr_for_nary
4399 7547937 : (nary, value_id, gimple_location (stmt));
4400 7547937 : break;
4401 : }
4402 :
4403 5221843 : case VN_REFERENCE:
4404 5221843 : {
4405 5221843 : tree rhs1 = gimple_assign_rhs1 (stmt);
4406 : /* There is no point in trying to handle aggregates,
4407 : even when via punning we might get a value number
4408 : corresponding to a register typed load. */
4409 5221843 : if (!is_gimple_reg_type (TREE_TYPE (rhs1)))
4410 1492288 : continue;
4411 4855276 : ao_ref rhs1_ref;
4412 4855276 : ao_ref_init (&rhs1_ref, rhs1);
4413 4855276 : alias_set_type set = ao_ref_alias_set (&rhs1_ref);
4414 4855276 : alias_set_type base_set
4415 4855276 : = ao_ref_base_alias_set (&rhs1_ref);
4416 4855276 : vec<vn_reference_op_s> operands
4417 4855276 : = vn_reference_operands_for_lookup (rhs1);
4418 4855276 : vn_reference_t ref;
4419 :
4420 : /* We handle &MEM[ptr + 5].b[1].c as
4421 : POINTER_PLUS_EXPR. */
4422 4855276 : if (operands[0].opcode == ADDR_EXPR
4423 5120801 : && operands.last ().opcode == SSA_NAME)
4424 : {
4425 265513 : tree ops[2];
4426 265513 : if (vn_pp_nary_for_addr (operands, ops))
4427 : {
4428 177452 : vn_nary_op_t nary;
4429 177452 : vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4430 177452 : TREE_TYPE (rhs1), ops,
4431 : &nary);
4432 177452 : operands.release ();
4433 177452 : if (nary && !nary->predicated_values)
4434 : {
4435 177440 : unsigned value_id = nary->value_id;
4436 177440 : if (value_id_constant_p (value_id))
4437 12 : continue;
4438 177440 : result = get_or_alloc_expr_for_nary
4439 177440 : (nary, value_id, gimple_location (stmt));
4440 177440 : break;
4441 : }
4442 12 : continue;
4443 12 : }
4444 : }
4445 :
4446 9355648 : vn_reference_lookup_pieces (gimple_vuse (stmt), set,
4447 4677824 : base_set, TREE_TYPE (rhs1),
4448 : operands, &ref, VN_WALK);
4449 : /* When there is no value recorded or the value was
4450 : recorded for a different type, fail, similar as
4451 : how we do during PHI translation. */
4452 4680918 : if (!ref
4453 4677824 : || !useless_type_conversion_p (TREE_TYPE (rhs1),
4454 : ref->type))
4455 : {
4456 3094 : operands.release ();
4457 3094 : continue;
4458 : }
4459 4674730 : operands.release ();
4460 :
4461 : /* If the REFERENCE traps and there was a preceding
4462 : point in the block that might not return avoid
4463 : adding the reference to EXP_GEN. */
4464 4839346 : if (BB_MAY_NOTRETURN (block)
4465 4674730 : && gimple_could_trap_p_1 (stmt, true, false))
4466 164616 : continue;
4467 :
4468 : /* If the value of the reference is not invalidated in
4469 : this block until it is computed, add the expression
4470 : to EXP_GEN. */
4471 9020228 : if (gimple_vuse (stmt))
4472 : {
4473 4422058 : gimple *def_stmt;
4474 4422058 : bool ok = true;
4475 4422058 : def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
4476 7105431 : while (!gimple_nop_p (def_stmt)
4477 6117516 : && gimple_code (def_stmt) != GIMPLE_PHI
4478 12005120 : && gimple_bb (def_stmt) == block)
4479 : {
4480 3641372 : if (stmt_may_clobber_ref_p
4481 3641372 : (def_stmt, gimple_assign_rhs1 (stmt)))
4482 : {
4483 : ok = false;
4484 : break;
4485 : }
4486 2683373 : def_stmt
4487 2683373 : = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
4488 : }
4489 4422058 : if (!ok)
4490 957999 : continue;
4491 : }
4492 :
4493 : /* Record the un-valueized expression for EXP_GEN. */
4494 3552115 : copy_reference_ops_from_ref (rhs1, &operands);
4495 3552115 : vn_reference_t newref
4496 3552115 : = XALLOCAVAR (struct vn_reference_s,
4497 : sizeof (vn_reference_s));
4498 3552115 : memset (newref, 0, sizeof (vn_reference_s));
4499 3552115 : newref->value_id = ref->value_id;
4500 3552115 : newref->vuse = ref->vuse;
4501 3552115 : newref->operands = operands;
4502 3552115 : newref->type = TREE_TYPE (rhs1);
4503 3552115 : newref->set = set;
4504 3552115 : newref->base_set = base_set;
4505 3552115 : newref->offset = 0;
4506 3552115 : newref->max_size = -1;
4507 3552115 : newref->result = ref->result;
4508 3552115 : newref->hashcode = vn_reference_compute_hash (newref);
4509 :
4510 3552115 : result = get_or_alloc_expr_for_reference
4511 3552115 : (newref, newref->value_id,
4512 : gimple_location (stmt), true);
4513 3552115 : break;
4514 : }
4515 :
4516 5584482 : default:
4517 5584482 : continue;
4518 5584482 : }
4519 :
4520 11277492 : add_to_value (get_expr_value_id (result), result);
4521 11277492 : bitmap_value_insert_into_set (EXP_GEN (block), result);
4522 11277492 : continue;
4523 11277492 : }
4524 5336605 : default:
4525 5336605 : break;
4526 956508 : }
4527 : }
4528 13431977 : if (set_bb_may_notreturn)
4529 : {
4530 569545 : BB_MAY_NOTRETURN (block) = 1;
4531 569545 : set_bb_may_notreturn = false;
4532 : }
4533 :
4534 13431977 : if (dump_file && (dump_flags & TDF_DETAILS))
4535 : {
4536 108 : print_bitmap_set (dump_file, EXP_GEN (block),
4537 : "exp_gen", block->index);
4538 108 : print_bitmap_set (dump_file, PHI_GEN (block),
4539 : "phi_gen", block->index);
4540 108 : print_bitmap_set (dump_file, TMP_GEN (block),
4541 : "tmp_gen", block->index);
4542 108 : print_bitmap_set (dump_file, AVAIL_OUT (block),
4543 : "avail_out", block->index);
4544 : }
4545 :
4546 : /* Put the dominator children of BLOCK on the worklist of blocks
4547 : to compute available sets for. */
4548 13431977 : for (son = first_dom_son (CDI_DOMINATORS, block);
4549 25882434 : son;
4550 12450457 : son = next_dom_son (CDI_DOMINATORS, son))
4551 12450457 : worklist[sp++] = son;
4552 : }
4553 981520 : vn_context_bb = NULL;
4554 :
4555 981520 : free (worklist);
4556 981520 : }
4557 :
4558 :
4559 : /* Initialize data structures used by PRE. */
4560 :
4561 : static void
4562 981527 : init_pre (void)
4563 : {
4564 981527 : basic_block bb;
4565 :
4566 981527 : next_expression_id = 1;
4567 981527 : expressions.create (0);
4568 981527 : expressions.safe_push (NULL);
4569 981527 : value_expressions.create (get_max_value_id () + 1);
4570 981527 : value_expressions.quick_grow_cleared (get_max_value_id () + 1);
4571 981527 : constant_value_expressions.create (get_max_constant_value_id () + 1);
4572 981527 : constant_value_expressions.quick_grow_cleared (get_max_constant_value_id () + 1);
4573 981527 : name_to_id.create (0);
4574 981527 : gcc_obstack_init (&pre_expr_obstack);
4575 :
4576 981527 : inserted_exprs = BITMAP_ALLOC (NULL);
4577 :
4578 981527 : connect_infinite_loops_to_exit ();
4579 981527 : memset (&pre_stats, 0, sizeof (pre_stats));
4580 :
4581 981527 : alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4582 :
4583 981527 : calculate_dominance_info (CDI_DOMINATORS);
4584 :
4585 981527 : bitmap_obstack_initialize (&grand_bitmap_obstack);
4586 1963054 : expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4587 16414108 : FOR_ALL_BB_FN (bb, cfun)
4588 : {
4589 15432581 : EXP_GEN (bb) = bitmap_set_new ();
4590 15432581 : PHI_GEN (bb) = bitmap_set_new ();
4591 15432581 : TMP_GEN (bb) = bitmap_set_new ();
4592 15432581 : AVAIL_OUT (bb) = bitmap_set_new ();
4593 15432581 : PHI_TRANS_TABLE (bb) = NULL;
4594 : }
4595 981527 : }
4596 :
4597 :
4598 : /* Deallocate data structures used by PRE. */
4599 :
4600 : static void
4601 981527 : fini_pre ()
4602 : {
4603 981527 : value_expressions.release ();
4604 981527 : constant_value_expressions.release ();
4605 46102680 : for (unsigned i = 1; i < expressions.length (); ++i)
4606 44139626 : if (expressions[i]->kind == REFERENCE)
4607 6458300 : PRE_EXPR_REFERENCE (expressions[i])->operands.release ();
4608 981527 : expressions.release ();
4609 981527 : bitmap_obstack_release (&grand_bitmap_obstack);
4610 981527 : bitmap_set_pool.release ();
4611 981527 : pre_expr_pool.release ();
4612 981527 : delete expression_to_id;
4613 981527 : expression_to_id = NULL;
4614 981527 : name_to_id.release ();
4615 981527 : obstack_free (&pre_expr_obstack, NULL);
4616 :
4617 981527 : basic_block bb;
4618 16413806 : FOR_ALL_BB_FN (bb, cfun)
4619 15432279 : if (bb->aux && PHI_TRANS_TABLE (bb))
4620 6216131 : delete PHI_TRANS_TABLE (bb);
4621 981527 : free_aux_for_blocks ();
4622 981527 : }
4623 :
4624 : namespace {
4625 :
4626 : const pass_data pass_data_pre =
4627 : {
4628 : GIMPLE_PASS, /* type */
4629 : "pre", /* name */
4630 : OPTGROUP_NONE, /* optinfo_flags */
4631 : TV_TREE_PRE, /* tv_id */
4632 : ( PROP_cfg | PROP_ssa ), /* properties_required */
4633 : 0, /* properties_provided */
4634 : 0, /* properties_destroyed */
4635 : TODO_rebuild_alias, /* todo_flags_start */
4636 : 0, /* todo_flags_finish */
4637 : };
4638 :
4639 : class pass_pre : public gimple_opt_pass
4640 : {
4641 : public:
4642 294196 : pass_pre (gcc::context *ctxt)
4643 588392 : : gimple_opt_pass (pass_data_pre, ctxt)
4644 : {}
4645 :
4646 : /* opt_pass methods: */
4647 1060389 : bool gate (function *) final override
4648 1060389 : { return flag_tree_pre != 0 || flag_code_hoisting != 0; }
4649 : unsigned int execute (function *) final override;
4650 :
4651 : }; // class pass_pre
4652 :
4653 : /* Valueization hook for RPO VN when we are calling back to it
4654 : at ANTIC compute time. */
4655 :
4656 : static tree
4657 122245973 : pre_valueize (tree name)
4658 : {
4659 122245973 : if (TREE_CODE (name) == SSA_NAME)
4660 : {
4661 121978379 : tree tem = VN_INFO (name)->valnum;
4662 121978379 : if (tem != VN_TOP && tem != name)
4663 : {
4664 16530172 : if (TREE_CODE (tem) != SSA_NAME
4665 16530172 : || SSA_NAME_IS_DEFAULT_DEF (tem))
4666 : return tem;
4667 : /* We create temporary SSA names for representatives that
4668 : do not have a definition (yet) but are not default defs either
4669 : assume they are fine to use. */
4670 16524780 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (tem));
4671 16524780 : if (! def_bb
4672 16524780 : || dominated_by_p (CDI_DOMINATORS, vn_context_bb, def_bb))
4673 127736 : return tem;
4674 : /* ??? Now we could look for a leader. Ideally we'd somehow
4675 : expose RPO VN leaders and get rid of AVAIL_OUT as well... */
4676 : }
4677 : }
4678 : return name;
4679 : }
4680 :
4681 : unsigned int
4682 981527 : pass_pre::execute (function *fun)
4683 : {
4684 981527 : unsigned int todo = 0;
4685 :
4686 1963054 : do_partial_partial =
4687 981527 : flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4688 :
4689 : /* This has to happen before VN runs because
4690 : loop_optimizer_init may create new phis, etc. */
4691 981527 : loop_optimizer_init (LOOPS_NORMAL);
4692 981527 : split_edges_for_insertion ();
4693 981527 : scev_initialize ();
4694 981527 : calculate_dominance_info (CDI_DOMINATORS);
4695 :
4696 981527 : run_rpo_vn (VN_WALK);
4697 :
4698 981527 : init_pre ();
4699 :
4700 981527 : vn_valueize = pre_valueize;
4701 :
4702 : /* Insert can get quite slow on an incredibly large number of basic
4703 : blocks due to some quadratic behavior. Until this behavior is
4704 : fixed, don't run it when he have an incredibly large number of
4705 : bb's. If we aren't going to run insert, there is no point in
4706 : computing ANTIC, either, even though it's plenty fast nor do
4707 : we require AVAIL. */
4708 981527 : if (n_basic_blocks_for_fn (fun) < 4000)
4709 : {
4710 981520 : compute_avail (fun);
4711 981520 : compute_antic ();
4712 981520 : insert ();
4713 : }
4714 :
4715 : /* Make sure to remove fake edges before committing our inserts.
4716 : This makes sure we don't end up with extra critical edges that
4717 : we would need to split. */
4718 981527 : remove_fake_exit_edges ();
4719 981527 : gsi_commit_edge_inserts ();
4720 :
4721 : /* Eliminate folds statements which might (should not...) end up
4722 : not keeping virtual operands up-to-date. */
4723 981527 : gcc_assert (!need_ssa_update_p (fun));
4724 :
4725 981527 : statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4726 981527 : statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4727 981527 : statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
4728 981527 : statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4729 :
4730 981527 : todo |= eliminate_with_rpo_vn (inserted_exprs);
4731 :
4732 981527 : vn_valueize = NULL;
4733 :
4734 981527 : fini_pre ();
4735 :
4736 981527 : scev_finalize ();
4737 981527 : loop_optimizer_finalize ();
4738 :
4739 : /* Perform a CFG cleanup before we run simple_dce_from_worklist since
4740 : unreachable code regions will have not up-to-date SSA form which
4741 : confuses it. */
4742 981527 : bool need_crit_edge_split = false;
4743 981527 : if (todo & TODO_cleanup_cfg)
4744 : {
4745 140433 : cleanup_tree_cfg ();
4746 140433 : need_crit_edge_split = true;
4747 : }
4748 :
4749 : /* Because we don't follow exactly the standard PRE algorithm, and decide not
4750 : to insert PHI nodes sometimes, and because value numbering of casts isn't
4751 : perfect, we sometimes end up inserting dead code. This simple DCE-like
4752 : pass removes any insertions we made that weren't actually used. */
4753 981527 : simple_dce_from_worklist (inserted_exprs);
4754 981527 : BITMAP_FREE (inserted_exprs);
4755 :
4756 : /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4757 : case we can merge the block with the remaining predecessor of the block.
4758 : It should either:
4759 : - call merge_blocks after each tail merge iteration
4760 : - call merge_blocks after all tail merge iterations
4761 : - mark TODO_cleanup_cfg when necessary. */
4762 981527 : todo |= tail_merge_optimize (need_crit_edge_split);
4763 :
4764 981527 : free_rpo_vn ();
4765 :
4766 : /* Tail merging invalidates the virtual SSA web, together with
4767 : cfg-cleanup opportunities exposed by PRE this will wreck the
4768 : SSA updating machinery. So make sure to run update-ssa
4769 : manually, before eventually scheduling cfg-cleanup as part of
4770 : the todo. */
4771 981527 : update_ssa (TODO_update_ssa_only_virtuals);
4772 :
4773 981527 : return todo;
4774 : }
4775 :
4776 : } // anon namespace
4777 :
4778 : gimple_opt_pass *
4779 294196 : make_pass_pre (gcc::context *ctxt)
4780 : {
4781 294196 : return new pass_pre (ctxt);
4782 : }
|