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 57789994 : pre_expr_d::equal (const pre_expr_d *e1, const pre_expr_d *e2)
277 : {
278 57789994 : if (e1->kind != e2->kind)
279 : return false;
280 :
281 36348331 : switch (e1->kind)
282 : {
283 4759643 : case CONSTANT:
284 4759643 : return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
285 4759643 : PRE_EXPR_CONSTANT (e2));
286 159617 : case NAME:
287 159617 : return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
288 22797918 : case NARY:
289 22797918 : return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
290 8631153 : case REFERENCE:
291 8631153 : return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
292 8631153 : PRE_EXPR_REFERENCE (e2), true);
293 0 : default:
294 0 : gcc_unreachable ();
295 : }
296 : }
297 :
298 : /* Hash E. */
299 :
300 : inline hashval_t
301 92406412 : pre_expr_d::hash (const pre_expr_d *e)
302 : {
303 92406412 : switch (e->kind)
304 : {
305 7261018 : case CONSTANT:
306 7261018 : 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 57069689 : case NARY:
310 57069689 : return PRE_EXPR_NARY (e)->hashcode;
311 28075705 : case REFERENCE:
312 28075705 : 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 44169455 : alloc_expression_id (pre_expr expr)
331 : {
332 44169455 : struct pre_expr_d **slot;
333 : /* Make sure we won't overflow. */
334 44169455 : gcc_assert (next_expression_id + 1 > next_expression_id);
335 44169455 : expr->id = next_expression_id++;
336 44169455 : expressions.safe_push (expr);
337 44169455 : if (expr->kind == NAME)
338 : {
339 24110105 : 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 24110105 : unsigned old_len = name_to_id.length ();
343 48220210 : name_to_id.reserve (num_ssa_names - old_len);
344 48220210 : name_to_id.quick_grow_cleared (num_ssa_names);
345 24110105 : gcc_assert (name_to_id[version] == 0);
346 24110105 : name_to_id[version] = expr->id;
347 : }
348 : else
349 : {
350 20059350 : slot = expression_to_id->find_slot (expr, INSERT);
351 20059350 : gcc_assert (!*slot);
352 20059350 : *slot = expr;
353 : }
354 44169455 : return next_expression_id - 1;
355 : }
356 :
357 : /* Return the expression id for tree EXPR. */
358 :
359 : static inline unsigned int
360 259258457 : get_expression_id (const pre_expr expr)
361 : {
362 259258457 : return expr->id;
363 : }
364 :
365 : static inline unsigned int
366 79659867 : lookup_expression_id (const pre_expr expr)
367 : {
368 79659867 : struct pre_expr_d **slot;
369 :
370 79659867 : if (expr->kind == NAME)
371 : {
372 53646264 : unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
373 53646264 : if (name_to_id.length () <= version)
374 : return 0;
375 50629705 : return name_to_id[version];
376 : }
377 : else
378 : {
379 26013603 : slot = expression_to_id->find_slot (expr, NO_INSERT);
380 26013603 : if (!slot)
381 : return 0;
382 5954253 : return ((pre_expr)*slot)->id;
383 : }
384 : }
385 :
386 : /* Return the expression that has expression id ID */
387 :
388 : static inline pre_expr
389 657228814 : expression_for_id (unsigned int id)
390 : {
391 1314457628 : 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 53646264 : get_or_alloc_expr_for_name (tree name)
400 : {
401 53646264 : struct pre_expr_d expr;
402 53646264 : pre_expr result;
403 53646264 : unsigned int result_id;
404 :
405 53646264 : expr.kind = NAME;
406 53646264 : expr.id = 0;
407 53646264 : PRE_EXPR_NAME (&expr) = name;
408 53646264 : result_id = lookup_expression_id (&expr);
409 53646264 : if (result_id != 0)
410 29536159 : return expression_for_id (result_id);
411 :
412 24110105 : result = pre_expr_pool.allocate ();
413 24110105 : result->kind = NAME;
414 24110105 : result->loc = UNKNOWN_LOCATION;
415 24110105 : result->value_id = VN_INFO (name)->value_id;
416 24110105 : PRE_EXPR_NAME (result) = name;
417 24110105 : alloc_expression_id (result);
418 24110105 : 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 13728052 : get_or_alloc_expr_for_nary (vn_nary_op_t nary, unsigned value_id,
427 : location_t loc = UNKNOWN_LOCATION)
428 : {
429 13728052 : struct pre_expr_d expr;
430 13728052 : pre_expr result;
431 13728052 : unsigned int result_id;
432 :
433 13728052 : gcc_assert (value_id == 0 || !value_id_constant_p (value_id));
434 13728052 : gcc_assert (nary->opcode != SSA_NAME
435 : && TREE_CODE_CLASS (nary->opcode) != tcc_constant);
436 :
437 13728052 : expr.kind = NARY;
438 13728052 : expr.id = 0;
439 13728052 : nary->hashcode = vn_nary_op_compute_hash (nary);
440 13728052 : PRE_EXPR_NARY (&expr) = nary;
441 13728052 : result_id = lookup_expression_id (&expr);
442 13728052 : if (result_id != 0)
443 986120 : return expression_for_id (result_id);
444 :
445 12741932 : result = pre_expr_pool.allocate ();
446 12741932 : result->kind = NARY;
447 12741932 : result->loc = loc;
448 12741932 : result->value_id = value_id ? value_id : get_next_value_id ();
449 12741932 : PRE_EXPR_NARY (result)
450 12741932 : = alloc_vn_nary_op_noinit (nary->length, &pre_expr_obstack);
451 12741932 : memcpy (PRE_EXPR_NARY (result), nary, sizeof_vn_nary_op (nary->length));
452 12741932 : alloc_expression_id (result);
453 12741932 : 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 7222051 : 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 7222051 : struct pre_expr_d expr;
469 7222051 : pre_expr result;
470 7222051 : unsigned int result_id;
471 :
472 7222051 : expr.kind = REFERENCE;
473 7222051 : expr.id = 0;
474 7222051 : PRE_EXPR_REFERENCE (&expr) = reference;
475 7222051 : result_id = lookup_expression_id (&expr);
476 7222051 : if (result_id != 0)
477 : {
478 756494 : if (move_operands)
479 744123 : reference->operands.release ();
480 756494 : return expression_for_id (result_id);
481 : }
482 :
483 6465557 : result = pre_expr_pool.allocate ();
484 6465557 : result->kind = REFERENCE;
485 6465557 : result->loc = loc;
486 6465557 : result->value_id = value_id ? value_id : get_next_value_id ();
487 6465557 : vn_reference_t ref = XOBNEW (&pre_expr_obstack, struct vn_reference_s);
488 6465557 : *ref = *reference;
489 6465557 : if (!move_operands)
490 453679 : ref->operands = ref->operands.copy ();
491 6465557 : PRE_EXPR_REFERENCE (result) = ref;
492 6465557 : alloc_expression_id (result);
493 6465557 : return result;
494 : }
495 :
496 :
497 : /* An unordered bitmap set. One bitmap tracks values, the other,
498 : expressions. */
499 147875552 : 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 1396993607 : expr_pred_trans_d::is_empty (const expr_pred_trans_d &e)
582 : {
583 1396993607 : return e.e == 0;
584 : }
585 :
586 : inline bool
587 280251608 : expr_pred_trans_d::is_deleted (const expr_pred_trans_d &e)
588 : {
589 280251608 : return e.e == -1u;
590 : }
591 :
592 : inline void
593 2261378 : expr_pred_trans_d::mark_empty (expr_pred_trans_d &e)
594 : {
595 2261378 : e.e = 0;
596 : }
597 :
598 : inline void
599 3871032 : expr_pred_trans_d::mark_deleted (expr_pred_trans_d &e)
600 : {
601 3871032 : 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 219241265 : expr_pred_trans_d::equal (const expr_pred_trans_d &ve1,
612 : const expr_pred_trans_d &ve2)
613 : {
614 219241265 : 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 86531219 : phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
684 : {
685 86531219 : if (!PHI_TRANS_TABLE (pred))
686 197660 : PHI_TRANS_TABLE (pred) = new hash_table<expr_pred_trans_d> (11);
687 :
688 86531219 : expr_pred_trans_t slot;
689 86531219 : expr_pred_trans_d tem;
690 86531219 : unsigned id = get_expression_id (e);
691 86531219 : tem.e = id;
692 86531219 : slot = PHI_TRANS_TABLE (pred)->find_slot_with_hash (tem, id, INSERT);
693 86531219 : if (slot->e)
694 : {
695 62900664 : *entry = slot;
696 62900664 : return true;
697 : }
698 :
699 23630555 : *entry = slot;
700 23630555 : slot->e = id;
701 23630555 : return false;
702 : }
703 :
704 :
705 : /* Add expression E to the expression set of value id V. */
706 :
707 : static void
708 45912069 : add_to_value (unsigned int v, pre_expr e)
709 : {
710 0 : gcc_checking_assert (get_expr_value_id (e) == v);
711 :
712 45912069 : if (value_id_constant_p (v))
713 : {
714 897717 : if (e->kind != CONSTANT)
715 : return;
716 :
717 851861 : if (-v >= constant_value_expressions.length ())
718 508258 : constant_value_expressions.safe_grow_cleared (-v + 1);
719 :
720 851861 : pre_expr leader = constant_value_expressions[-v];
721 851861 : if (!leader)
722 851861 : constant_value_expressions[-v] = e;
723 : }
724 : else
725 : {
726 45014352 : if (v >= value_expressions.length ())
727 7058353 : value_expressions.safe_grow_cleared (v + 1);
728 :
729 45014352 : bitmap set = value_expressions[v];
730 45014352 : if (!set)
731 : {
732 25162434 : set = BITMAP_ALLOC (&grand_bitmap_obstack);
733 25162434 : value_expressions[v] = set;
734 : }
735 45014352 : 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 147875552 : bitmap_set_new (void)
743 : {
744 147875552 : bitmap_set_t ret = bitmap_set_pool.allocate ();
745 147875552 : bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
746 147875552 : bitmap_initialize (&ret->values, &grand_bitmap_obstack);
747 147875552 : return ret;
748 : }
749 :
750 : /* Return the value id for a PRE expression EXPR. */
751 :
752 : static unsigned int
753 540071396 : 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 45912069 : 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 1270782 : vn_valnum_from_value_id (unsigned int val)
765 : {
766 1270782 : 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 1270782 : bitmap exprset = value_expressions[val];
775 1270782 : bitmap_iterator bi;
776 1270782 : unsigned int i;
777 1884044 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
778 : {
779 1539617 : pre_expr vexpr = expression_for_id (i);
780 1539617 : if (vexpr->kind == NAME)
781 926355 : 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 76039714 : bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
790 : {
791 76039714 : unsigned int val = get_expr_value_id (expr);
792 76039714 : 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 73086924 : bitmap_set_bit (&set->values, val);
798 73086924 : bitmap_set_bit (&set->expressions, get_expression_id (expr));
799 : }
800 76039714 : }
801 :
802 : /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
803 :
804 : static void
805 27288552 : bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
806 : {
807 27288552 : bitmap_copy (&dest->expressions, &orig->expressions);
808 27288552 : bitmap_copy (&dest->values, &orig->values);
809 27288552 : }
810 :
811 :
812 : /* Free memory used up by SET. */
813 : static void
814 73973403 : bitmap_set_free (bitmap_set_t set)
815 : {
816 0 : bitmap_clear (&set->expressions);
817 19762262 : bitmap_clear (&set->values);
818 0 : }
819 :
820 :
821 : /* Sort pre_expr after their value-id. */
822 :
823 : static int
824 434385667 : expr_cmp (const void *a_, const void *b_, void *)
825 : {
826 434385667 : pre_expr a = *(pre_expr const *) a_;
827 434385667 : pre_expr b = *(pre_expr const *) b_;
828 434385667 : 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 264416 : prefer (pre_expr a, pre_expr b)
837 : {
838 264416 : if (a->kind == REFERENCE && b->kind == REFERENCE)
839 : {
840 66715 : auto refa = PRE_EXPR_REFERENCE (a);
841 66715 : auto refb = PRE_EXPR_REFERENCE (b);
842 66715 : auto &oprsa = refa->operands;
843 66715 : auto &oprsb = refb->operands;
844 66715 : pre_expr palias = NULL;
845 66715 : if (refa->set == refb->set
846 63292 : && refa->base_set == refb->base_set)
847 : ;
848 12754 : else if ((refb->set == refa->set
849 3423 : || alias_set_subset_of (refb->set, refa->set))
850 14165 : && (refb->base_set == refa->base_set
851 9990 : || alias_set_subset_of (refb->base_set, refa->base_set)))
852 : palias = a;
853 4750 : else if ((refa->set == refb->set
854 2076 : || alias_set_subset_of (refa->set, refb->set))
855 6640 : && (refa->base_set == refb->base_set
856 3790 : || 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 66444 : pre_expr p = palias;
867 132888 : if (oprsa.length () > 1 && oprsb.length () > 1)
868 : {
869 66444 : vn_reference_op_t vroa = &oprsa[oprsa.length () - 2];
870 66444 : vn_reference_op_t vrob = &oprsb[oprsb.length () - 2];
871 66444 : 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 66419 : pre_expr palign = NULL;
876 66419 : if (TYPE_ALIGN (vroa->type) < TYPE_ALIGN (vrob->type))
877 : palign = a;
878 66191 : else if (TYPE_ALIGN (vroa->type) > TYPE_ALIGN (vrob->type))
879 : palign = b;
880 483 : if (palign)
881 : {
882 483 : 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 66034 : if (TYPE_SIZE (vroa->type) != TYPE_SIZE (vrob->type))
889 : {
890 4950 : pre_expr psize = NULL;
891 4950 : if (!TYPE_SIZE (vroa->type))
892 : psize = a;
893 4950 : else if (!TYPE_SIZE (vrob->type))
894 : psize = b;
895 4950 : else if (TREE_CODE (TYPE_SIZE (vroa->type)) == INTEGER_CST
896 4950 : && TREE_CODE (TYPE_SIZE (vrob->type)) == INTEGER_CST)
897 : {
898 4944 : int cmp = tree_int_cst_compare (TYPE_SIZE (vroa->type),
899 4944 : TYPE_SIZE (vrob->type));
900 4944 : if (cmp < 0)
901 : psize = a;
902 2805 : else if (cmp > 0)
903 : psize = b;
904 : }
905 : /* ??? What about non-constant sizes? */
906 4944 : if (psize)
907 : {
908 4944 : 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 62029 : return p ? p : b;
919 : }
920 : /* Always prefer an non-REFERENCE, avoiding the above mess. */
921 197701 : else if (a->kind == REFERENCE)
922 : return b;
923 194474 : else if (b->kind == REFERENCE)
924 : return a;
925 162843 : else if (a->kind == b->kind)
926 : ;
927 : /* And prefer NAME over anything else. */
928 10897 : else if (b->kind == NAME)
929 : return b;
930 8505 : else if (a->kind == NAME)
931 8505 : 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 200171789 : pre_expr_DFS (unsigned val, bitmap_set_t set, bitmap exclusions,
944 : bitmap val_visited, vec<pre_expr> &post)
945 : {
946 200171789 : unsigned int i;
947 200171789 : bitmap_iterator bi;
948 :
949 : /* Iterate over all leaders and DFS recurse. Borrowed from
950 : bitmap_find_leader. */
951 200171789 : bitmap exprset = value_expressions[val];
952 200171789 : if (!exprset->first->next)
953 : {
954 477326182 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
955 304806001 : if (bitmap_bit_p (&set->expressions, i)
956 304806001 : && !bitmap_bit_p (exclusions, i))
957 172799268 : pre_expr_DFS (expression_for_id (i), set, exclusions,
958 : val_visited, post);
959 172520181 : return;
960 : }
961 :
962 56078391 : EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
963 28426783 : if (!bitmap_bit_p (exclusions, i))
964 28050462 : 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 200849730 : pre_expr_DFS (pre_expr expr, bitmap_set_t set, bitmap exclusions,
973 : bitmap val_visited, vec<pre_expr> &post)
974 : {
975 200849730 : switch (expr->kind)
976 : {
977 96268976 : case NARY:
978 96268976 : {
979 96268976 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
980 260738069 : for (unsigned i = 0; i < nary->length; i++)
981 : {
982 164469093 : if (TREE_CODE (nary->op[i]) != SSA_NAME)
983 47145780 : continue;
984 117323313 : 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 117323313 : if (bitmap_bit_p (&set->values, op_val_id)
988 117323313 : && bitmap_set_bit (val_visited, op_val_id))
989 69432936 : pre_expr_DFS (op_val_id, set, exclusions, val_visited, post);
990 : }
991 : break;
992 : }
993 30237429 : case REFERENCE:
994 30237429 : {
995 30237429 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
996 30237429 : vec<vn_reference_op_s> operands = ref->operands;
997 30237429 : vn_reference_op_t operand;
998 119792983 : for (unsigned i = 0; operands.iterate (i, &operand); i++)
999 : {
1000 89555554 : tree op[3];
1001 89555554 : op[0] = operand->op0;
1002 89555554 : op[1] = operand->op1;
1003 89555554 : op[2] = operand->op2;
1004 358222216 : for (unsigned n = 0; n < 3; ++n)
1005 : {
1006 268666662 : if (!op[n] || TREE_CODE (op[n]) != SSA_NAME)
1007 246932595 : continue;
1008 21734067 : unsigned op_val_id = VN_INFO (op[n])->value_id;
1009 21734067 : if (bitmap_bit_p (&set->values, op_val_id)
1010 21734067 : && bitmap_set_bit (val_visited, op_val_id))
1011 11286519 : pre_expr_DFS (op_val_id, set, exclusions, val_visited, post);
1012 : }
1013 : }
1014 : break;
1015 : }
1016 200849730 : default:;
1017 : }
1018 200849730 : post.quick_push (expr);
1019 200849730 : }
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 18589805 : sorted_array_from_bitmap_set (bitmap_set_t set, bool for_insertion)
1026 : {
1027 18589805 : unsigned int i;
1028 18589805 : bitmap_iterator bi;
1029 18589805 : vec<pre_expr> result;
1030 :
1031 : /* Pre-allocate enough space for the array. */
1032 18589805 : unsigned cnt = bitmap_count_bits (&set->expressions);
1033 18589805 : 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 18589805 : auto_bitmap exclusions;
1038 18589805 : bitmap_tree_view (exclusions);
1039 18589805 : if (for_insertion && cnt > 1)
1040 : {
1041 26751976 : EXECUTE_IF_SET_IN_BITMAP (&set->expressions, 0, i, bi)
1042 24099484 : result.safe_push (expression_for_id (i));
1043 2652492 : result.sort (expr_cmp, NULL);
1044 48191098 : for (unsigned i = 0; i < result.length () - 1; ++i)
1045 21443057 : if (result[i]->value_id == result[i+1]->value_id)
1046 : {
1047 264416 : 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 259730 : if (p == result[i])
1052 45627 : std::swap (result[i], result[i+1]);
1053 259730 : 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 4696 : if (j == 0
1064 4696 : || result[j - 1]->value_id != result[i]->value_id)
1065 : break;
1066 : for (k = j;; ++k)
1067 : {
1068 9433 : if (result[k]->kind == REFERENCE)
1069 9433 : bitmap_set_bit (exclusions,
1070 9433 : get_expression_id (result[k]));
1071 9433 : if (k == result.length () - 1
1072 9433 : || result[k + 1]->value_id != result[i]->value_id)
1073 : break;
1074 : }
1075 : i = k;
1076 : }
1077 : }
1078 2652492 : result.truncate (0);
1079 : }
1080 :
1081 18589805 : bool single_p = true;
1082 18589805 : auto_bitmap val_visited (&grand_bitmap_obstack);
1083 18589805 : bitmap_tree_view (val_visited);
1084 105111617 : FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
1085 86521812 : if (bitmap_set_bit (val_visited, i))
1086 : {
1087 76754608 : if (!result.is_empty ())
1088 : {
1089 63858597 : single_p = false;
1090 63858597 : result.truncate (0);
1091 : }
1092 76754608 : 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 76754608 : 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 18589805 : if (!single_p)
1100 : {
1101 9704335 : result.truncate (0);
1102 9704335 : auto_bitmap val_visited2 (&grand_bitmap_obstack);
1103 9704335 : bitmap_tree_view (val_visited2);
1104 92487324 : FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
1105 82782989 : if (!bitmap_bit_p (val_visited, i))
1106 : {
1107 42697726 : if (bitmap_set_bit (val_visited2, i))
1108 42697726 : pre_expr_DFS (i, set, exclusions, val_visited2, result);
1109 : else
1110 0 : gcc_unreachable ();
1111 : }
1112 9704335 : if (flag_checking)
1113 : {
1114 9704284 : bitmap_list_view (val_visited2);
1115 9704284 : gcc_assert (bitmap_equal_p (&set->values, val_visited2));
1116 : }
1117 9704335 : }
1118 :
1119 18589805 : return result;
1120 18589805 : }
1121 :
1122 : /* Subtract all expressions contained in ORIG from DEST. */
1123 :
1124 : static bitmap_set_t
1125 32910753 : bitmap_set_subtract_expressions (bitmap_set_t dest, bitmap_set_t orig,
1126 : bool copy_values = false)
1127 : {
1128 32910753 : bitmap_set_t result = bitmap_set_new ();
1129 32910753 : bitmap_iterator bi;
1130 32910753 : unsigned int i;
1131 :
1132 32910753 : bitmap_and_compl (&result->expressions, &dest->expressions,
1133 32910753 : &orig->expressions);
1134 :
1135 32910753 : if (copy_values)
1136 657055 : bitmap_copy (&result->values, &dest->values);
1137 : else
1138 111936271 : FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
1139 : {
1140 79682573 : pre_expr expr = expression_for_id (i);
1141 79682573 : unsigned int value_id = get_expr_value_id (expr);
1142 79682573 : bitmap_set_bit (&result->values, value_id);
1143 : }
1144 :
1145 32910753 : return result;
1146 : }
1147 :
1148 : /* Subtract all values in bitmap set B from bitmap set A. */
1149 :
1150 : static void
1151 1239489 : bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
1152 : {
1153 1239489 : unsigned int i;
1154 1239489 : bitmap_iterator bi;
1155 1239489 : unsigned to_remove = -1U;
1156 1239489 : bitmap_and_compl_into (&a->values, &b->values);
1157 11650131 : FOR_EACH_EXPR_ID_IN_SET (a, i, bi)
1158 : {
1159 10410642 : if (to_remove != -1U)
1160 : {
1161 1460969 : bitmap_clear_bit (&a->expressions, to_remove);
1162 1460969 : to_remove = -1U;
1163 : }
1164 10410642 : pre_expr expr = expression_for_id (i);
1165 10410642 : if (! bitmap_bit_p (&a->values, get_expr_value_id (expr)))
1166 1517849 : to_remove = i;
1167 : }
1168 1239489 : if (to_remove != -1U)
1169 56880 : bitmap_clear_bit (&a->expressions, to_remove);
1170 1239489 : }
1171 :
1172 :
1173 : /* Return true if bitmapped set SET contains the value VALUE_ID. */
1174 :
1175 : static bool
1176 202774609 : 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 99692729 : return bitmap_bit_p (&set->values, value_id);
1182 : }
1183 :
1184 : /* Return true if two bitmap sets are equal. */
1185 :
1186 : static bool
1187 15835632 : 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 33711732 : bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
1197 : {
1198 33711732 : unsigned int val = get_expr_value_id (expr);
1199 33711732 : if (value_id_constant_p (val))
1200 : return false;
1201 :
1202 33711732 : 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 14373742 : unsigned int i;
1214 14373742 : bitmap_iterator bi;
1215 14373742 : bitmap exprset = value_expressions[val];
1216 16792421 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1217 : {
1218 16792421 : if (bitmap_clear_bit (&set->expressions, i))
1219 : {
1220 14373742 : bitmap_set_bit (&set->expressions, get_expression_id (expr));
1221 14373742 : return i != get_expression_id (expr);
1222 : }
1223 : }
1224 0 : gcc_unreachable ();
1225 : }
1226 :
1227 19337990 : bitmap_insert_into_set (set, expr);
1228 19337990 : 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 64016240 : bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
1236 : {
1237 64016240 : unsigned int val = get_expr_value_id (expr);
1238 :
1239 64016240 : gcc_checking_assert (expr->id == get_expression_id (expr));
1240 :
1241 : /* Constant values are always considered to be part of the set. */
1242 64016240 : if (value_id_constant_p (val))
1243 : return;
1244 :
1245 : /* If the value membership changed, add the expression. */
1246 63956774 : if (bitmap_set_bit (&set->values, val))
1247 49438048 : 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 5063500 : get_or_alloc_expr_for_constant (tree constant)
1385 : {
1386 5063500 : unsigned int result_id;
1387 5063500 : struct pre_expr_d expr;
1388 5063500 : pre_expr newexpr;
1389 :
1390 5063500 : expr.kind = CONSTANT;
1391 5063500 : PRE_EXPR_CONSTANT (&expr) = constant;
1392 5063500 : result_id = lookup_expression_id (&expr);
1393 5063500 : if (result_id != 0)
1394 4211639 : return expression_for_id (result_id);
1395 :
1396 851861 : newexpr = pre_expr_pool.allocate ();
1397 851861 : newexpr->kind = CONSTANT;
1398 851861 : newexpr->loc = UNKNOWN_LOCATION;
1399 851861 : PRE_EXPR_CONSTANT (newexpr) = constant;
1400 851861 : alloc_expression_id (newexpr);
1401 851861 : newexpr->value_id = get_or_alloc_constant_value_id (constant);
1402 851861 : add_to_value (newexpr->value_id, newexpr);
1403 851861 : 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 4549184 : 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 4549184 : basic_block phiblock = e->dest;
1416 4549184 : gimple *def = SSA_NAME_DEF_STMT (vuse);
1417 4549184 : ao_ref ref;
1418 :
1419 4549184 : if (same_valid)
1420 3282269 : *same_valid = true;
1421 :
1422 : /* If value-numbering provided a memory state for this
1423 : that dominates PHIBLOCK we can just use that. */
1424 4549184 : if (gimple_nop_p (def)
1425 4549184 : || (gimple_bb (def) != phiblock
1426 1190260 : && 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 2654302 : gphi *phi = get_virtual_phi (phiblock);
1435 2654302 : if (!phi)
1436 94079 : return BB_LIVE_VOP_ON_EXIT
1437 : (get_immediate_dominator (CDI_DOMINATORS, phiblock));
1438 :
1439 2560223 : if (same_valid
1440 2560223 : && ao_ref_init_from_vn_reference (&ref, set, base_set, type, operands))
1441 : {
1442 1876927 : bitmap visited = NULL;
1443 : /* Try to find a vuse that dominates this phi node by skipping
1444 : non-clobbering statements. */
1445 1876927 : unsigned int cnt = param_sccvn_max_alias_queries_per_access;
1446 1876927 : vuse = get_continuation_for_phi (phi, &ref, true,
1447 : cnt, &visited, false, NULL, NULL);
1448 1876927 : if (visited)
1449 1865512 : 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 2560223 : if (!vuse && same_valid)
1455 1621613 : *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 2560223 : 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 24546957 : find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
1471 : bitmap_set_t set3 = NULL)
1472 : {
1473 24546957 : pre_expr result = NULL;
1474 :
1475 24546957 : if (set1)
1476 24418669 : result = bitmap_find_leader (set1, val);
1477 24546957 : if (!result && set2)
1478 1602963 : result = bitmap_find_leader (set2, val);
1479 24546957 : if (!result && set3)
1480 0 : result = bitmap_find_leader (set3, val);
1481 24546957 : return result;
1482 : }
1483 :
1484 : /* Get the tree type for our PRE expression e. */
1485 :
1486 : static tree
1487 7459837 : get_expr_type (const pre_expr e)
1488 : {
1489 7459837 : switch (e->kind)
1490 : {
1491 1008844 : case NAME:
1492 1008844 : return TREE_TYPE (PRE_EXPR_NAME (e));
1493 186528 : case CONSTANT:
1494 186528 : return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1495 1358088 : case REFERENCE:
1496 1358088 : return PRE_EXPR_REFERENCE (e)->type;
1497 4906377 : case NARY:
1498 4906377 : 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 19572524 : get_representative_for (const pre_expr e, basic_block b = NULL)
1513 : {
1514 19572524 : tree name, valnum = NULL_TREE;
1515 19572524 : unsigned int value_id = get_expr_value_id (e);
1516 :
1517 19572524 : switch (e->kind)
1518 : {
1519 9030679 : case NAME:
1520 9030679 : return PRE_EXPR_NAME (e);
1521 1913650 : case CONSTANT:
1522 1913650 : return PRE_EXPR_CONSTANT (e);
1523 8628195 : case NARY:
1524 8628195 : case REFERENCE:
1525 8628195 : {
1526 : /* Go through all of the expressions representing this value
1527 : and pick out an SSA_NAME. */
1528 8628195 : unsigned int i;
1529 8628195 : bitmap_iterator bi;
1530 8628195 : bitmap exprs = value_expressions[value_id];
1531 22342942 : EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1532 : {
1533 18276711 : pre_expr rep = expression_for_id (i);
1534 18276711 : if (rep->kind == NAME)
1535 : {
1536 8302985 : tree name = PRE_EXPR_NAME (rep);
1537 8302985 : valnum = VN_INFO (name)->valnum;
1538 8302985 : 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 8302985 : if (! b
1543 7983388 : || gimple_nop_p (def)
1544 12295826 : || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
1545 4561964 : return name;
1546 : }
1547 9973726 : else if (rep->kind == CONSTANT)
1548 0 : return PRE_EXPR_CONSTANT (rep);
1549 : }
1550 : }
1551 4066231 : 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 4066231 : name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1561 4066231 : vn_ssa_aux_t vn_info = VN_INFO (name);
1562 4066231 : vn_info->value_id = value_id;
1563 4066231 : vn_info->valnum = valnum ? valnum : name;
1564 4066231 : vn_info->visited = true;
1565 : /* ??? For now mark this SSA name for release by VN. */
1566 4066231 : vn_info->needs_insertion = true;
1567 4066231 : add_to_value (value_id, get_or_alloc_expr_for_name (name));
1568 4066231 : 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 49390530 : phi_translate_1 (bitmap_set_t dest,
1590 : pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
1591 : {
1592 49390530 : basic_block pred = e->src;
1593 49390530 : basic_block phiblock = e->dest;
1594 49390530 : location_t expr_loc = expr->loc;
1595 49390530 : switch (expr->kind)
1596 : {
1597 18590810 : case NARY:
1598 18590810 : {
1599 18590810 : unsigned int i;
1600 18590810 : bool changed = false;
1601 18590810 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1602 18590810 : vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1603 : sizeof_vn_nary_op (nary->length));
1604 18590810 : memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1605 :
1606 44037619 : for (i = 0; i < newnary->length; i++)
1607 : {
1608 28942209 : if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1609 9024422 : continue;
1610 : else
1611 : {
1612 19917787 : pre_expr leader, result;
1613 19917787 : unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1614 19917787 : leader = find_leader_in_sets (op_val_id, set1, set2);
1615 19917787 : result = phi_translate (dest, leader, set1, set2, e);
1616 19917787 : 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 16422387 : newnary->op[i] = get_representative_for (result, pred);
1621 : else if (!result)
1622 : return NULL;
1623 :
1624 16422387 : changed |= newnary->op[i] != nary->op[i];
1625 : }
1626 : }
1627 15095410 : if (changed)
1628 : {
1629 7586986 : unsigned int new_val_id;
1630 :
1631 7586986 : vn_nary_op_t saved_newnary
1632 7586986 : = XALLOCAVAR (struct vn_nary_op_s,
1633 : sizeof_vn_nary_op (newnary->length));
1634 7586986 : memcpy (saved_newnary, newnary,
1635 : sizeof_vn_nary_op (newnary->length));
1636 :
1637 : /* Try to simplify the new NARY. */
1638 7586986 : tree res = vn_nary_simplify (newnary);
1639 7586986 : if (res)
1640 : {
1641 2435796 : if (is_gimple_min_invariant (res))
1642 1249436 : 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 1186360 : 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 1186360 : if (e->flags & EDGE_DFS_BACK)
1655 : ;
1656 : else
1657 : {
1658 1104399 : 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 2208798 : pre_expr constant = find_leader_in_sets (value_id, dest,
1665 1104399 : AVAIL_OUT (pred));
1666 1104399 : if (constant)
1667 : {
1668 336628 : 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 849732 : memcpy (newnary, saved_newnary,
1687 849732 : sizeof_vn_nary_op (saved_newnary->length));
1688 : }
1689 :
1690 12001844 : tree result = vn_nary_op_lookup_pieces (newnary->length,
1691 6000922 : newnary->opcode,
1692 : newnary->type,
1693 : &newnary->op[0],
1694 : &nary);
1695 6000922 : if (result && is_gimple_min_invariant (result))
1696 0 : return get_or_alloc_expr_for_constant (result);
1697 :
1698 6000922 : if (!nary || nary->predicated_values)
1699 : new_val_id = 0;
1700 : else
1701 812074 : new_val_id = nary->value_id;
1702 6000922 : expr = get_or_alloc_expr_for_nary (newnary, new_val_id, expr_loc);
1703 6000922 : add_to_value (get_expr_value_id (expr), expr);
1704 : }
1705 : return expr;
1706 : }
1707 5039745 : break;
1708 :
1709 5039745 : case REFERENCE:
1710 5039745 : {
1711 5039745 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1712 5039745 : vec<vn_reference_op_s> operands = ref->operands;
1713 5039745 : tree vuse = ref->vuse;
1714 5039745 : tree newvuse = vuse;
1715 5039745 : vec<vn_reference_op_s> newoperands = vNULL;
1716 5039745 : bool changed = false, same_valid = true;
1717 5039745 : unsigned int i, n;
1718 5039745 : vn_reference_op_t operand;
1719 5039745 : vn_reference_t newref;
1720 :
1721 19201054 : for (i = 0; operands.iterate (i, &operand); i++)
1722 : {
1723 14535943 : pre_expr opresult;
1724 14535943 : pre_expr leader;
1725 14535943 : tree op[3];
1726 14535943 : tree type = operand->type;
1727 14535943 : vn_reference_op_s newop = *operand;
1728 14535943 : op[0] = operand->op0;
1729 14535943 : op[1] = operand->op1;
1730 14535943 : op[2] = operand->op2;
1731 57019942 : for (n = 0; n < 3; ++n)
1732 : {
1733 42858633 : unsigned int op_val_id;
1734 42858633 : if (!op[n])
1735 25990517 : continue;
1736 16868116 : if (TREE_CODE (op[n]) != SSA_NAME)
1737 : {
1738 : /* We can't possibly insert these. */
1739 13343345 : if (n != 0
1740 13343345 : && !is_gimple_min_invariant (op[n]))
1741 : break;
1742 13343345 : continue;
1743 : }
1744 3524771 : op_val_id = VN_INFO (op[n])->value_id;
1745 3524771 : leader = find_leader_in_sets (op_val_id, set1, set2);
1746 3524771 : opresult = phi_translate (dest, leader, set1, set2, e);
1747 3524771 : if (opresult)
1748 : {
1749 3150137 : tree name = get_representative_for (opresult);
1750 3150137 : changed |= name != op[n];
1751 3150137 : op[n] = name;
1752 : }
1753 : else if (!opresult)
1754 : break;
1755 : }
1756 14535943 : if (n != 3)
1757 : {
1758 374634 : newoperands.release ();
1759 374634 : 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 14161309 : if ((newop.opcode == MEM_REF
1768 14161309 : || newop.opcode == TARGET_MEM_REF)
1769 4771641 : && newop.clique > 1
1770 164009 : && (e->flags & EDGE_DFS_BACK))
1771 : {
1772 : newop.clique = 0;
1773 : newop.base = 0;
1774 : changed = true;
1775 : }
1776 14141922 : if (!changed)
1777 11636774 : continue;
1778 2524535 : if (!newoperands.exists ())
1779 1299531 : newoperands = operands.copy ();
1780 : /* We may have changed from an SSA_NAME to a constant */
1781 2524535 : if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1782 : newop.opcode = TREE_CODE (op[0]);
1783 2524535 : newop.type = type;
1784 2524535 : newop.op0 = op[0];
1785 2524535 : newop.op1 = op[1];
1786 2524535 : newop.op2 = op[2];
1787 2524535 : newoperands[i] = newop;
1788 : }
1789 9330222 : gcc_checking_assert (i == operands.length ());
1790 :
1791 4665111 : if (vuse)
1792 : {
1793 11113722 : newvuse = translate_vuse_through_block (newoperands.exists ()
1794 4549184 : ? newoperands : operands,
1795 : ref->set, ref->base_set,
1796 : ref->type, vuse, e,
1797 : changed
1798 : ? NULL : &same_valid);
1799 4549184 : if (newvuse == NULL_TREE)
1800 : {
1801 0 : newoperands.release ();
1802 0 : return NULL;
1803 : }
1804 : }
1805 :
1806 4665111 : if (changed || newvuse != vuse)
1807 : {
1808 3281225 : unsigned int new_val_id;
1809 :
1810 5264384 : tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1811 : ref->base_set,
1812 : ref->type,
1813 3281225 : newoperands.exists ()
1814 3281225 : ? 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 3281225 : if (result && is_gimple_min_invariant (result))
1821 : {
1822 79036 : tree tem = result;
1823 79036 : 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 79036 : if (tem)
1830 : {
1831 79036 : newoperands.release ();
1832 79036 : 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 3202189 : if (result
1841 3202189 : && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1842 : {
1843 998 : newoperands.release ();
1844 998 : return NULL;
1845 : }
1846 2628489 : else if (!result && newref
1847 3201191 : && !useless_type_conversion_p (ref->type, newref->type))
1848 : {
1849 0 : newoperands.release ();
1850 0 : return NULL;
1851 : }
1852 :
1853 3201191 : if (newref)
1854 : {
1855 572702 : new_val_id = newref->value_id;
1856 572702 : newvuse = newref->vuse;
1857 : }
1858 : else
1859 : {
1860 2628489 : if (changed || !same_valid)
1861 : new_val_id = 0;
1862 : else
1863 127719 : new_val_id = ref->value_id;
1864 : }
1865 3201191 : newref = XALLOCAVAR (struct vn_reference_s,
1866 : sizeof (vn_reference_s));
1867 3201191 : memcpy (newref, ref, sizeof (vn_reference_s));
1868 3201191 : newref->next = NULL;
1869 3201191 : newref->value_id = new_val_id;
1870 3201191 : newref->vuse = newvuse;
1871 6402382 : newref->operands
1872 3201191 : = newoperands.exists () ? newoperands : operands.copy ();
1873 3201191 : newoperands = vNULL;
1874 3201191 : newref->type = ref->type;
1875 3201191 : newref->result = result;
1876 3201191 : newref->hashcode = vn_reference_compute_hash (newref);
1877 3201191 : expr = get_or_alloc_expr_for_reference (newref, new_val_id,
1878 : expr_loc, true);
1879 3201191 : add_to_value (get_expr_value_id (expr), expr);
1880 : }
1881 4585077 : newoperands.release ();
1882 4585077 : return expr;
1883 : }
1884 25759975 : break;
1885 :
1886 25759975 : case NAME:
1887 25759975 : {
1888 25759975 : tree name = PRE_EXPR_NAME (expr);
1889 25759975 : 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 25759975 : if (gimple_code (def_stmt) == GIMPLE_PHI
1893 25759975 : && gimple_bb (def_stmt) == phiblock)
1894 : {
1895 8037143 : tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1896 :
1897 : /* Handle constant. */
1898 8037143 : if (is_gimple_min_invariant (def))
1899 2329467 : return get_or_alloc_expr_for_constant (def);
1900 :
1901 5707676 : 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 90577955 : phi_translate (bitmap_set_t dest, pre_expr expr,
1918 : bitmap_set_t set1, bitmap_set_t set2, edge e)
1919 : {
1920 90577955 : expr_pred_trans_t slot = NULL;
1921 90577955 : pre_expr phitrans;
1922 :
1923 90577955 : if (!expr)
1924 : return NULL;
1925 :
1926 : /* Constants contain no values that need translation. */
1927 88660813 : if (expr->kind == CONSTANT)
1928 : return expr;
1929 :
1930 88660639 : 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 88660639 : if (expr->kind != NAME)
1935 : {
1936 62900664 : if (phi_trans_add (&slot, expr, e->src))
1937 39270109 : 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 23630555 : slot->v = 0;
1941 : }
1942 :
1943 : /* Translate. */
1944 49390530 : basic_block saved_valueize_bb = vn_context_bb;
1945 49390530 : vn_context_bb = e->src;
1946 49390530 : phitrans = phi_translate_1 (dest, expr, set1, set2, e);
1947 49390530 : vn_context_bb = saved_valueize_bb;
1948 :
1949 49390530 : if (slot)
1950 : {
1951 : /* We may have reallocated. */
1952 23630555 : phi_trans_add (&slot, expr, e->src);
1953 23630555 : if (phitrans)
1954 19759523 : 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 3871032 : 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 20757472 : phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
1971 : {
1972 20757472 : bitmap_iterator bi;
1973 20757472 : unsigned int i;
1974 :
1975 20757472 : if (gimple_seq_empty_p (phi_nodes (e->dest)))
1976 : {
1977 13876396 : bitmap_set_copy (dest, set);
1978 13876396 : 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 6881076 : if (!PHI_TRANS_TABLE (e->src))
1986 6010595 : PHI_TRANS_TABLE (e->src) = new hash_table<expr_pred_trans_d>
1987 6010595 : (2 * bitmap_count_bits (&set->expressions));
1988 45747740 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1989 : {
1990 38866664 : pre_expr expr = expression_for_id (i);
1991 38866664 : pre_expr translated = phi_translate (dest, expr, set, NULL, e);
1992 38866664 : if (!translated)
1993 1917261 : continue;
1994 :
1995 36949403 : 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 57177290 : bitmap_find_leader (bitmap_set_t set, unsigned int val)
2005 : {
2006 57177290 : if (value_id_constant_p (val))
2007 1782892 : return constant_value_expressions[-val];
2008 :
2009 55394398 : 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 25860513 : unsigned int i;
2023 25860513 : bitmap_iterator bi;
2024 25860513 : bitmap exprset = value_expressions[val];
2025 :
2026 25860513 : if (!exprset->first->next)
2027 32805875 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2028 30481314 : if (bitmap_bit_p (&set->expressions, i))
2029 23446230 : return expression_for_id (i);
2030 :
2031 6292405 : EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
2032 3878122 : 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 1668974 : value_dies_in_block_x (pre_expr expr, basic_block block)
2046 : {
2047 1668974 : tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
2048 1668974 : vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
2049 1668974 : gimple *def;
2050 1668974 : gimple_stmt_iterator gsi;
2051 1668974 : unsigned id = get_expression_id (expr);
2052 1668974 : bool res = false;
2053 1668974 : ao_ref ref;
2054 :
2055 1668974 : if (!vuse)
2056 : return false;
2057 :
2058 : /* Lookup a previously calculated result. */
2059 1668974 : if (EXPR_DIES (block)
2060 1668974 : && bitmap_bit_p (EXPR_DIES (block), id * 2))
2061 137501 : 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 1531473 : ref.base = NULL_TREE;
2069 13781190 : for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
2070 : {
2071 11766544 : tree def_vuse, def_vdef;
2072 11766544 : def = gsi_stmt (gsi);
2073 11766544 : def_vuse = gimple_vuse (def);
2074 11766544 : def_vdef = gimple_vdef (def);
2075 :
2076 : /* Not a memory statement. */
2077 11766544 : if (!def_vuse)
2078 8431963 : continue;
2079 :
2080 : /* Not a may-def. */
2081 3334581 : if (!def_vdef)
2082 : {
2083 : /* A load with the same VUSE, we're done. */
2084 971588 : if (def_vuse == vuse)
2085 : break;
2086 :
2087 680093 : continue;
2088 : }
2089 :
2090 : /* Init ref only if we really need it. */
2091 2362993 : if (ref.base == NULL_TREE
2092 3515126 : && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->base_set,
2093 1152133 : refx->type, refx->operands))
2094 : {
2095 : res = true;
2096 : break;
2097 : }
2098 : /* If the statement may clobber expr, it dies. */
2099 2328433 : if (stmt_may_clobber_ref_p_1 (def, &ref))
2100 : {
2101 : res = true;
2102 : break;
2103 : }
2104 : }
2105 :
2106 : /* Remember the result. */
2107 1531473 : if (!EXPR_DIES (block))
2108 718566 : EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
2109 1531473 : bitmap_set_bit (EXPR_DIES (block), id * 2);
2110 1531473 : if (res)
2111 756805 : 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 276710686 : op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
2122 : {
2123 276710686 : if (op && TREE_CODE (op) == SSA_NAME)
2124 : {
2125 82357921 : unsigned int value_id = VN_INFO (op)->value_id;
2126 164713320 : if (!(bitmap_set_contains_value (set1, value_id)
2127 2353830 : || (set2 && bitmap_set_contains_value (set2, value_id))))
2128 2216863 : 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 131081702 : valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
2141 : {
2142 131081702 : 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 59534659 : case NARY:
2149 59534659 : {
2150 59534659 : unsigned int i;
2151 59534659 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2152 156088214 : for (i = 0; i < nary->length; i++)
2153 98627847 : if (!op_valid_in_sets (set1, set2, nary->op[i]))
2154 : return false;
2155 : return true;
2156 : }
2157 20314451 : break;
2158 20314451 : case REFERENCE:
2159 20314451 : {
2160 20314451 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2161 20314451 : vn_reference_op_t vro;
2162 20314451 : unsigned int i;
2163 :
2164 79627851 : FOR_EACH_VEC_ELT (ref->operands, i, vro)
2165 : {
2166 59455971 : if (!op_valid_in_sets (set1, set2, vro->op0)
2167 59313434 : || !op_valid_in_sets (set1, set2, vro->op1)
2168 118769405 : || !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 14651645 : clean (bitmap_set_t set1, bitmap_set_t set2 = NULL)
2184 : {
2185 14651645 : vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1, false);
2186 15611675 : bool changed;
2187 :
2188 15611675 : do
2189 : {
2190 15611675 : unsigned j = 0;
2191 15611675 : changed = false;
2192 84875145 : for (unsigned i = 0; i < exprs.length (); ++i)
2193 : {
2194 69263470 : pre_expr expr = exprs[i];
2195 69263470 : if (!valid_in_sets (set1, set2, expr))
2196 : {
2197 2216853 : unsigned int val = get_expr_value_id (expr);
2198 2216853 : 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 2216853 : if (! bitmap_find_leader (set1, val))
2203 : {
2204 2206335 : bitmap_clear_bit (&set1->values, val);
2205 2206335 : changed = true;
2206 : }
2207 : }
2208 : else
2209 : {
2210 67046617 : exprs[j] = expr;
2211 67046617 : ++j;
2212 : }
2213 : }
2214 15611675 : exprs.truncate (j);
2215 : }
2216 : /* As the value graph can have cycles we have to iterate here. */
2217 : while (changed);
2218 14651645 : exprs.release ();
2219 :
2220 14651645 : if (flag_checking)
2221 : {
2222 14651458 : unsigned j;
2223 14651458 : bitmap_iterator bi;
2224 76237850 : FOR_EACH_EXPR_ID_IN_SET (set1, j, bi)
2225 61586392 : gcc_assert (valid_in_sets (set1, set2, expression_for_id (j)));
2226 : }
2227 14651645 : }
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 17075121 : prune_clobbered_mems (bitmap_set_t set, basic_block block, bool clean_traps)
2235 : {
2236 17075121 : bitmap_iterator bi;
2237 17075121 : unsigned i;
2238 17075121 : unsigned to_remove = -1U;
2239 17075121 : bool any_removed = false;
2240 :
2241 77772597 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2242 : {
2243 : /* Remove queued expr. */
2244 60697476 : if (to_remove != -1U)
2245 : {
2246 596711 : bitmap_clear_bit (&set->expressions, to_remove);
2247 596711 : any_removed = true;
2248 596711 : to_remove = -1U;
2249 : }
2250 :
2251 60697476 : pre_expr expr = expression_for_id (i);
2252 60697476 : if (expr->kind == REFERENCE)
2253 : {
2254 8198175 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2255 8198175 : if (ref->vuse)
2256 : {
2257 7442281 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2258 7442281 : 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 9116933 : && !(gimple_bb (def_stmt) != block
2263 3836153 : && dominated_by_p (CDI_DOMINATORS,
2264 3836153 : block, gimple_bb (def_stmt)))
2265 9111255 : && 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 7518307 : if ((BB_MAY_NOTRETURN (block) || clean_traps)
2273 8452451 : && vn_reference_may_trap (ref))
2274 : to_remove = i;
2275 : }
2276 52499301 : else if (expr->kind == NARY)
2277 : {
2278 27746250 : 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 23546117 : if ((BB_MAY_NOTRETURN (block) || clean_traps)
2284 28610330 : && vn_nary_may_trap (nary))
2285 : to_remove = i;
2286 : }
2287 : }
2288 :
2289 : /* Remove queued expr. */
2290 17075121 : if (to_remove != -1U)
2291 : {
2292 431532 : bitmap_clear_bit (&set->expressions, to_remove);
2293 431532 : 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 17075121 : if (any_removed && !clean_traps)
2310 : {
2311 494774 : bitmap_clear (&set->values);
2312 2795486 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2313 : {
2314 2300712 : pre_expr expr = expression_for_id (i);
2315 2300712 : unsigned int value_id = get_expr_value_id (expr);
2316 2300712 : bitmap_set_bit (&set->values, value_id);
2317 : }
2318 : }
2319 17075121 : }
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 15840031 : compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2334 : {
2335 15840031 : bitmap_set_t S, old, ANTIC_OUT;
2336 15840031 : edge e;
2337 15840031 : edge_iterator ei;
2338 :
2339 15840031 : bool was_visited = BB_VISITED (block);
2340 15840031 : bool changed = ! BB_VISITED (block);
2341 15840031 : bool any_max_on_edge = false;
2342 :
2343 15840031 : BB_VISITED (block) = 1;
2344 15840031 : old = ANTIC_OUT = S = NULL;
2345 :
2346 : /* If any edges from predecessors are abnormal, antic_in is empty,
2347 : so do nothing. */
2348 15840031 : if (block_has_abnormal_pred_edge)
2349 4399 : goto maybe_dump_sets;
2350 :
2351 15835632 : old = ANTIC_IN (block);
2352 15835632 : ANTIC_OUT = bitmap_set_new ();
2353 :
2354 : /* If the block has no successors, ANTIC_OUT is empty. */
2355 15835632 : if (EDGE_COUNT (block->succs) == 0)
2356 : ;
2357 : /* If we have one successor, we could have some phi nodes to
2358 : translate through. */
2359 15835632 : else if (single_succ_p (block))
2360 : {
2361 10111511 : e = single_succ_edge (block);
2362 10111511 : gcc_assert (BB_VISITED (e->dest));
2363 10111511 : 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 5724121 : size_t i;
2371 5724121 : edge first = NULL;
2372 :
2373 5724121 : auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2374 17280680 : FOR_EACH_EDGE (e, ei, block->succs)
2375 : {
2376 11556559 : if (!first
2377 6197911 : && BB_VISITED (e->dest))
2378 : first = e;
2379 5832438 : else if (BB_VISITED (e->dest))
2380 5172668 : 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 659770 : any_max_on_edge = true;
2388 659770 : 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 5724121 : gcc_assert (first != NULL);
2397 :
2398 5724121 : 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 22345031 : FOR_EACH_VEC_ELT (worklist, i, e)
2408 : {
2409 5172668 : 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 5170318 : bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
2420 5170318 : bitmap_ior_into (&ANTIC_OUT->expressions,
2421 5170318 : &ANTIC_IN (e->dest)->expressions);
2422 : }
2423 : }
2424 11448242 : if (! worklist.is_empty ())
2425 : {
2426 : /* Prune expressions not in the value set. */
2427 5068139 : bitmap_iterator bi;
2428 5068139 : unsigned int i;
2429 5068139 : unsigned int to_clear = -1U;
2430 36873500 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
2431 : {
2432 31805361 : if (to_clear != -1U)
2433 : {
2434 16790675 : bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2435 16790675 : to_clear = -1U;
2436 : }
2437 31805361 : pre_expr expr = expression_for_id (i);
2438 31805361 : unsigned int value_id = get_expr_value_id (expr);
2439 31805361 : if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
2440 20546398 : to_clear = i;
2441 : }
2442 5068139 : if (to_clear != -1U)
2443 3755723 : bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2444 : }
2445 5724121 : }
2446 :
2447 : /* Dump ANTIC_OUT before it's pruned. */
2448 15835632 : 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 15835632 : 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 15835632 : 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 31671264 : ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
2465 15835632 : 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 15835632 : bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
2470 15835632 : 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 15835632 : if (was_visited
2476 15835632 : && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
2477 : {
2478 1926 : 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 1926 : bitmap_iterator bi;
2483 1926 : unsigned int i;
2484 1926 : unsigned int to_clear = -1U;
2485 20831 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
2486 : {
2487 18905 : if (to_clear != -1U)
2488 : {
2489 1502 : bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2490 1502 : to_clear = -1U;
2491 : }
2492 18905 : pre_expr expr = expression_for_id (i);
2493 18905 : unsigned int value_id = get_expr_value_id (expr);
2494 18905 : if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
2495 2859 : to_clear = i;
2496 : }
2497 1926 : if (to_clear != -1U)
2498 1357 : bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2499 : }
2500 :
2501 15835632 : if (!bitmap_set_equal (old, ANTIC_IN (block)))
2502 10332412 : changed = true;
2503 :
2504 5503220 : maybe_dump_sets:
2505 15840031 : 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 15840031 : if (old)
2516 15835632 : bitmap_set_free (old);
2517 15840031 : if (S)
2518 15835632 : bitmap_set_free (S);
2519 15840031 : if (ANTIC_OUT)
2520 15835632 : bitmap_set_free (ANTIC_OUT);
2521 15840031 : 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 1240814 : compute_partial_antic_aux (basic_block block,
2537 : bool block_has_abnormal_pred_edge)
2538 : {
2539 1240814 : bitmap_set_t old_PA_IN;
2540 1240814 : bitmap_set_t PA_OUT;
2541 1240814 : edge e;
2542 1240814 : edge_iterator ei;
2543 1240814 : unsigned long max_pa = param_max_partial_antic_length;
2544 :
2545 1240814 : old_PA_IN = PA_OUT = NULL;
2546 :
2547 : /* If any edges from predecessors are abnormal, antic_in is empty,
2548 : so do nothing. */
2549 1240814 : 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 1240044 : if (max_pa
2556 1148143 : && single_succ_p (block)
2557 2016105 : && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2558 555 : goto maybe_dump_sets;
2559 :
2560 1239489 : old_PA_IN = PA_IN (block);
2561 1239489 : PA_OUT = bitmap_set_new ();
2562 :
2563 : /* If the block has no successors, ANTIC_OUT is empty. */
2564 1239489 : 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 1147590 : else if (single_succ_p (block))
2573 : {
2574 775508 : e = single_succ_edge (block);
2575 775508 : if (!(e->flags & EDGE_DFS_BACK))
2576 695828 : 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 372082 : size_t i;
2583 :
2584 372082 : auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2585 1121610 : FOR_EACH_EDGE (e, ei, block->succs)
2586 : {
2587 749528 : if (e->flags & EDGE_DFS_BACK)
2588 310 : continue;
2589 749218 : worklist.quick_push (e);
2590 : }
2591 372082 : if (worklist.length () > 0)
2592 : {
2593 1121300 : FOR_EACH_VEC_ELT (worklist, i, e)
2594 : {
2595 749218 : unsigned int i;
2596 749218 : bitmap_iterator bi;
2597 :
2598 749218 : 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 4872104 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
2616 4123631 : bitmap_value_insert_into_set (PA_OUT,
2617 : expression_for_id (i));
2618 7555822 : FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
2619 6807349 : bitmap_value_insert_into_set (PA_OUT,
2620 : expression_for_id (i));
2621 : }
2622 : }
2623 : }
2624 372082 : }
2625 :
2626 : /* Prune expressions that are clobbered in block and thus become
2627 : invalid if translated from PA_OUT to PA_IN. */
2628 1239489 : 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 1239489 : 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 1239489 : bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2637 1239489 : bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2638 :
2639 : /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2640 1239489 : bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2641 :
2642 1239489 : clean (PA_IN (block), ANTIC_IN (block));
2643 :
2644 1240814 : maybe_dump_sets:
2645 1240814 : 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 1240814 : if (old_PA_IN)
2653 1239489 : bitmap_set_free (old_PA_IN);
2654 1240814 : if (PA_OUT)
2655 1239489 : bitmap_set_free (PA_OUT);
2656 1240814 : }
2657 :
2658 : /* Compute ANTIC and partial ANTIC sets. */
2659 :
2660 : static void
2661 983305 : compute_antic (void)
2662 : {
2663 983305 : bool changed = true;
2664 983305 : int num_iterations = 0;
2665 983305 : basic_block block;
2666 983305 : int i;
2667 983305 : edge_iterator ei;
2668 983305 : 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 983305 : auto_sbitmap has_abnormal_preds (last_basic_block_for_fn (cfun));
2673 983305 : bitmap_clear (has_abnormal_preds);
2674 :
2675 16362071 : FOR_ALL_BB_FN (block, cfun)
2676 : {
2677 15378766 : BB_VISITED (block) = 0;
2678 :
2679 34684386 : FOR_EACH_EDGE (e, ei, block->preds)
2680 19309005 : 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 15378766 : ANTIC_IN (block) = bitmap_set_new ();
2688 15378766 : if (do_partial_partial)
2689 1240814 : PA_IN (block) = bitmap_set_new ();
2690 : }
2691 :
2692 : /* At the exit block we anticipate nothing. */
2693 983305 : 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 983305 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2699 983305 : int n = inverted_rev_post_order_compute (cfun, rpo);
2700 :
2701 983305 : auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
2702 983305 : bitmap_clear (worklist);
2703 2849666 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2704 1866361 : bitmap_set_bit (worklist, e->src->index);
2705 3041970 : while (changed)
2706 : {
2707 2058665 : 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 2058665 : num_iterations++;
2714 2058665 : changed = false;
2715 38894714 : for (i = 0; i < n; ++i)
2716 : {
2717 36836049 : if (bitmap_bit_p (worklist, rpo[i]))
2718 : {
2719 15840031 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2720 15840031 : bitmap_clear_bit (worklist, block->index);
2721 15840031 : if (compute_antic_aux (block,
2722 15840031 : bitmap_bit_p (has_abnormal_preds,
2723 : block->index)))
2724 : {
2725 33345508 : FOR_EACH_EDGE (e, ei, block->preds)
2726 18343142 : bitmap_set_bit (worklist, e->src->index);
2727 : changed = true;
2728 : }
2729 : }
2730 : }
2731 : /* Theoretically possible, but *highly* unlikely. */
2732 2058665 : 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 14395461 : FOR_EACH_BB_FN (block, cfun)
2739 13412156 : clean (ANTIC_IN (block));
2740 :
2741 983305 : statistics_histogram_event (cfun, "compute_antic iterations",
2742 : num_iterations);
2743 :
2744 983305 : 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 1332713 : for (i = 0; i < n; ++i)
2749 : {
2750 1240814 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2751 1240814 : compute_partial_antic_aux (block,
2752 1240814 : bitmap_bit_p (has_abnormal_preds,
2753 : block->index));
2754 : }
2755 : }
2756 :
2757 983305 : free (rpo);
2758 983305 : }
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 1164232 : create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2770 : unsigned int *operand, gimple_seq *stmts)
2771 : {
2772 1164232 : vn_reference_op_t currop = &ref->operands[*operand];
2773 1164232 : tree genop;
2774 1164232 : ++*operand;
2775 1164232 : switch (currop->opcode)
2776 : {
2777 0 : case CALL_EXPR:
2778 0 : gcc_unreachable ();
2779 :
2780 406042 : case MEM_REF:
2781 406042 : {
2782 406042 : tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2783 : stmts);
2784 406042 : if (!baseop)
2785 : return NULL_TREE;
2786 406038 : tree offset = currop->op0;
2787 406038 : if (TREE_CODE (baseop) == ADDR_EXPR
2788 406038 : && 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 406038 : genop = build2 (MEM_REF, currop->type, baseop, offset);
2801 406038 : MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2802 406038 : MR_DEPENDENCE_BASE (genop) = currop->base;
2803 406038 : REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
2804 406038 : 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 245421 : case ADDR_EXPR:
2836 245421 : if (currop->op0)
2837 : {
2838 243194 : gcc_assert (is_gimple_min_invariant (currop->op0));
2839 243194 : return currop->op0;
2840 : }
2841 : /* Fallthrough. */
2842 6543 : case REALPART_EXPR:
2843 6543 : case IMAGPART_EXPR:
2844 6543 : case VIEW_CONVERT_EXPR:
2845 6543 : {
2846 6543 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2847 : stmts);
2848 6543 : if (!genop0)
2849 : return NULL_TREE;
2850 6543 : 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 2821 : case BIT_FIELD_REF:
2866 2821 : {
2867 2821 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2868 : stmts);
2869 2821 : if (!genop0)
2870 : return NULL_TREE;
2871 2821 : tree op1 = currop->op0;
2872 2821 : tree op2 = currop->op1;
2873 2821 : tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2874 2821 : REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
2875 2821 : 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 63353 : case ARRAY_RANGE_REF:
2882 63353 : case ARRAY_REF:
2883 63353 : {
2884 63353 : tree genop0;
2885 63353 : tree genop1 = currop->op0;
2886 63353 : tree genop2 = currop->op1;
2887 63353 : tree genop3 = currop->op2;
2888 63353 : genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2889 : stmts);
2890 63353 : if (!genop0)
2891 : return NULL_TREE;
2892 63353 : genop1 = find_or_generate_expression (block, genop1, stmts);
2893 63353 : if (!genop1)
2894 : return NULL_TREE;
2895 63353 : if (genop2)
2896 : {
2897 63353 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2898 : /* Drop zero minimum index if redundant. */
2899 63353 : if (integer_zerop (genop2)
2900 63353 : && (!domain_type
2901 62318 : || 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 63353 : if (genop3)
2911 : {
2912 63353 : 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 63353 : if ((TREE_CODE (genop3) == INTEGER_CST
2918 63349 : && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
2919 63349 : && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
2920 63349 : (wi::to_offset (genop3) * vn_ref_op_align_unit (currop))))
2921 63353 : || (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 63349 : 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 63353 : return build4 (currop->opcode, currop->type, genop0, genop1,
2935 63353 : genop2, genop3);
2936 : }
2937 274811 : case COMPONENT_REF:
2938 274811 : {
2939 274811 : tree op0;
2940 274811 : tree op1;
2941 274811 : tree genop2 = currop->op1;
2942 274811 : op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2943 274811 : if (!op0)
2944 : return NULL_TREE;
2945 : /* op1 should be a FIELD_DECL, which are represented by themselves. */
2946 274803 : op1 = currop->op0;
2947 274803 : if (genop2)
2948 : {
2949 0 : genop2 = find_or_generate_expression (block, genop2, stmts);
2950 0 : if (!genop2)
2951 : return NULL_TREE;
2952 : }
2953 274803 : return build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2954 : }
2955 :
2956 165493 : case SSA_NAME:
2957 165493 : {
2958 165493 : genop = find_or_generate_expression (block, currop->op0, stmts);
2959 165493 : return genop;
2960 : }
2961 1971 : case STRING_CST:
2962 1971 : case INTEGER_CST:
2963 1971 : case POLY_INT_CST:
2964 1971 : case COMPLEX_CST:
2965 1971 : case VECTOR_CST:
2966 1971 : case REAL_CST:
2967 1971 : case CONSTRUCTOR:
2968 1971 : case VAR_DECL:
2969 1971 : case PARM_DECL:
2970 1971 : case CONST_DECL:
2971 1971 : case RESULT_DECL:
2972 1971 : case FUNCTION_DECL:
2973 1971 : 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 406069 : create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2994 : gimple_seq *stmts)
2995 : {
2996 406069 : unsigned int op = 0;
2997 406069 : 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 877577 : find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
3008 : {
3009 : /* Constants are always leaders. */
3010 877577 : if (is_gimple_min_invariant (op))
3011 : return op;
3012 :
3013 670954 : gcc_assert (TREE_CODE (op) == SSA_NAME);
3014 670954 : vn_ssa_aux_t info = VN_INFO (op);
3015 670954 : unsigned int lookfor = info->value_id;
3016 670954 : if (value_id_constant_p (lookfor))
3017 3 : return info->valnum;
3018 :
3019 670951 : pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
3020 670951 : if (leader)
3021 : {
3022 636596 : if (leader->kind == NAME)
3023 : {
3024 636596 : tree name = PRE_EXPR_NAME (leader);
3025 636596 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
3026 : return NULL_TREE;
3027 636568 : 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 34355 : 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 34355 : bitmap exprset = value_expressions[lookfor];
3041 34355 : bitmap_iterator bi;
3042 34355 : unsigned int i;
3043 34355 : if (exprset)
3044 44816 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
3045 : {
3046 42231 : 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 42231 : if (temp->kind == NARY)
3051 : {
3052 31766 : static int depth;
3053 31766 : if (depth > 8)
3054 : return NULL_TREE;
3055 :
3056 31751 : depth++;
3057 31751 : tree res = create_expression_by_pieces (block, temp, stmts,
3058 31751 : TREE_TYPE (op));
3059 31751 : depth--;
3060 31751 : 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 2936328 : create_expression_by_pieces (basic_block block, pre_expr expr,
3086 : gimple_seq *stmts, tree type)
3087 : {
3088 2936328 : tree name;
3089 2936328 : tree folded;
3090 2936328 : gimple_seq forced_stmts = NULL;
3091 2936328 : unsigned int value_id;
3092 2936328 : gimple_stmt_iterator gsi;
3093 2936328 : tree exprtype = type ? type : get_expr_type (expr);
3094 2936328 : pre_expr nameexpr;
3095 2936328 : gassign *newstmt;
3096 :
3097 2936328 : 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 741166 : case NAME:
3102 741166 : folded = PRE_EXPR_NAME (expr);
3103 741166 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
3104 : return NULL_TREE;
3105 741161 : if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
3106 : return folded;
3107 : break;
3108 1405516 : case CONSTANT:
3109 1405516 : {
3110 1405516 : folded = PRE_EXPR_CONSTANT (expr);
3111 1405516 : tree tem = fold_convert (exprtype, folded);
3112 1405516 : if (is_gimple_min_invariant (tem))
3113 : return tem;
3114 : break;
3115 : }
3116 409220 : case REFERENCE:
3117 409220 : if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
3118 : {
3119 3151 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
3120 3151 : unsigned int operand = 1;
3121 3151 : vn_reference_op_t currop = &ref->operands[0];
3122 3151 : tree sc = NULL_TREE;
3123 3151 : tree fn = NULL_TREE;
3124 3151 : if (currop->op0)
3125 : {
3126 3009 : fn = find_or_generate_expression (block, currop->op0, stmts);
3127 3009 : if (!fn)
3128 6 : return NULL_TREE;
3129 : }
3130 3151 : 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 6302 : auto_vec<tree> args (ref->operands.length () - 1);
3137 10885 : while (operand < ref->operands.length ())
3138 : {
3139 4589 : tree arg = create_component_ref_by_pieces_1 (block, ref,
3140 4589 : &operand, stmts);
3141 4589 : if (!arg)
3142 6 : return NULL_TREE;
3143 4583 : args.quick_push (arg);
3144 : }
3145 3145 : gcall *call;
3146 3145 : if (currop->op0)
3147 : {
3148 3003 : call = gimple_build_call_vec (fn, args);
3149 3003 : 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 3145 : gimple_set_location (call, expr->loc);
3155 3145 : if (sc)
3156 0 : gimple_call_set_chain (call, sc);
3157 3145 : tree forcedname = make_ssa_name (ref->type);
3158 3145 : 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 3145 : 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 3145 : && (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 3145 : gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
3176 3145 : gimple_seq_add_stmt_without_update (&forced_stmts, call);
3177 3145 : folded = forcedname;
3178 3151 : }
3179 : else
3180 : {
3181 406069 : folded = create_component_ref_by_pieces (block,
3182 : PRE_EXPR_REFERENCE (expr),
3183 : stmts);
3184 406069 : if (!folded)
3185 : return NULL_TREE;
3186 406065 : name = make_temp_ssa_name (exprtype, NULL, "pretmp");
3187 406065 : newstmt = gimple_build_assign (name, folded);
3188 406065 : gimple_set_location (newstmt, expr->loc);
3189 406065 : gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
3190 406065 : gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
3191 406065 : folded = name;
3192 : }
3193 : break;
3194 380426 : case NARY:
3195 380426 : {
3196 380426 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
3197 380426 : tree *genop = XALLOCAVEC (tree, nary->length);
3198 380426 : unsigned i;
3199 1016896 : for (i = 0; i < nary->length; ++i)
3200 : {
3201 645106 : genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
3202 645106 : if (!genop[i])
3203 : return NULL_TREE;
3204 : /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
3205 : may have conversions stripped. */
3206 636470 : if (nary->opcode == POINTER_PLUS_EXPR)
3207 : {
3208 110912 : if (i == 0)
3209 55475 : genop[i] = gimple_convert (&forced_stmts,
3210 : nary->type, genop[i]);
3211 55437 : else if (i == 1)
3212 55437 : genop[i] = gimple_convert (&forced_stmts,
3213 : sizetype, genop[i]);
3214 : }
3215 : else
3216 525558 : genop[i] = gimple_convert (&forced_stmts,
3217 525558 : TREE_TYPE (nary->op[i]), genop[i]);
3218 : }
3219 371790 : 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 371782 : switch (nary->length)
3234 : {
3235 108320 : case 1:
3236 108320 : folded = gimple_build (&forced_stmts, expr->loc,
3237 : nary->opcode, nary->type, genop[0]);
3238 108320 : break;
3239 263249 : case 2:
3240 263249 : folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
3241 : nary->type, genop[0], genop[1]);
3242 263249 : 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 876786 : folded = gimple_convert (&forced_stmts, exprtype, folded);
3259 :
3260 : /* If there is nothing to insert, return the simplified result. */
3261 876786 : 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 780919 : 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 780919 : bool found = false;
3272 780919 : gsi = gsi_last (forced_stmts);
3273 780919 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
3274 : {
3275 780919 : gimple *stmt = gsi_stmt (gsi);
3276 780919 : tree forcedname = gimple_get_lhs (stmt);
3277 780919 : if (forcedname == folded)
3278 : {
3279 : found = true;
3280 : break;
3281 : }
3282 : }
3283 780919 : if (! found)
3284 : {
3285 0 : gimple_seq_discard (forced_stmts);
3286 0 : return folded;
3287 : }
3288 780919 : 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 780919 : if (forced_stmts)
3293 : {
3294 780919 : gsi = gsi_start (forced_stmts);
3295 1562335 : for (; !gsi_end_p (gsi); gsi_next (&gsi))
3296 : {
3297 781416 : gimple *stmt = gsi_stmt (gsi);
3298 781416 : tree forcedname = gimple_get_lhs (stmt);
3299 781416 : pre_expr nameexpr;
3300 :
3301 781416 : 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 781416 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
3314 : }
3315 780919 : gimple_seq_add_seq (stmts, forced_stmts);
3316 : }
3317 :
3318 780919 : name = folded;
3319 :
3320 : /* Fold the last statement. */
3321 780919 : gsi = gsi_last (*stmts);
3322 780919 : if (fold_stmt_inplace (&gsi))
3323 209609 : 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 780919 : value_id = get_expr_value_id (expr);
3331 780919 : vn_ssa_aux_t vn_info = VN_INFO (name);
3332 780919 : vn_info->value_id = value_id;
3333 780919 : vn_info->valnum = vn_valnum_from_value_id (value_id);
3334 780919 : if (vn_info->valnum == NULL_TREE)
3335 245060 : vn_info->valnum = name;
3336 780919 : gcc_assert (vn_info->valnum != NULL_TREE);
3337 780919 : nameexpr = get_or_alloc_expr_for_name (name);
3338 780919 : add_to_value (value_id, nameexpr);
3339 780919 : if (NEW_SETS (block))
3340 549116 : bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3341 780919 : bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3342 :
3343 780919 : pre_stats.insertions++;
3344 780919 : 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 1963965 : insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3363 : vec<pre_expr> &avail)
3364 : {
3365 1963965 : pre_expr expr = expression_for_id (exprnum);
3366 1963965 : pre_expr newphi;
3367 1963965 : unsigned int val = get_expr_value_id (expr);
3368 1963965 : edge pred;
3369 1963965 : bool insertions = false;
3370 1963965 : bool nophi = false;
3371 1963965 : basic_block bprime;
3372 1963965 : pre_expr eprime;
3373 1963965 : edge_iterator ei;
3374 1963965 : tree type = get_expr_type (expr);
3375 1963965 : tree temp;
3376 1963965 : gphi *phi;
3377 :
3378 : /* Make sure we aren't creating an induction variable. */
3379 1963965 : if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3380 : {
3381 1630498 : bool firstinsideloop = false;
3382 1630498 : bool secondinsideloop = false;
3383 4891494 : firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3384 1630498 : EDGE_PRED (block, 0)->src);
3385 4891494 : secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3386 1630498 : EDGE_PRED (block, 1)->src);
3387 : /* Induction variables only have one edge inside the loop. */
3388 1630498 : if ((firstinsideloop ^ secondinsideloop)
3389 1554827 : && expr->kind != REFERENCE)
3390 : {
3391 1473937 : 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 6120563 : 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 4156598 : if (nophi && !dominated_by_p (CDI_DOMINATORS, block, pred->src))
3404 1486461 : continue;
3405 2672747 : gimple_seq stmts = NULL;
3406 2672747 : tree builtexpr;
3407 2672747 : bprime = pred->src;
3408 2672747 : eprime = avail[pred->dest_idx];
3409 2672747 : builtexpr = create_expression_by_pieces (bprime, eprime,
3410 : &stmts, type);
3411 2672747 : gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3412 2672747 : if (!gimple_seq_empty_p (stmts))
3413 : {
3414 523412 : basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
3415 523412 : gcc_assert (! new_bb);
3416 : insertions = true;
3417 : }
3418 2672747 : if (!builtexpr)
3419 : {
3420 : /* We cannot insert a PHI node if we failed to insert
3421 : on one edge. */
3422 2610 : nophi = true;
3423 2610 : continue;
3424 : }
3425 2670137 : if (is_gimple_min_invariant (builtexpr))
3426 1405561 : avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
3427 : else
3428 1264576 : 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 1963965 : if (nophi && insertions)
3435 : return true;
3436 1955387 : else if (nophi && !insertions)
3437 : return false;
3438 :
3439 : /* Now build a phi for the new variable. */
3440 487424 : temp = make_temp_ssa_name (type, NULL, "prephitmp");
3441 487424 : phi = create_phi_node (temp, block);
3442 :
3443 487424 : vn_ssa_aux_t vn_info = VN_INFO (temp);
3444 487424 : vn_info->value_id = val;
3445 487424 : vn_info->valnum = vn_valnum_from_value_id (val);
3446 487424 : if (vn_info->valnum == NULL_TREE)
3447 98844 : vn_info->valnum = temp;
3448 487424 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3449 1682796 : FOR_EACH_EDGE (pred, ei, block->preds)
3450 : {
3451 1195372 : pre_expr ae = avail[pred->dest_idx];
3452 1195372 : gcc_assert (get_expr_type (ae) == type
3453 : || useless_type_conversion_p (type, get_expr_type (ae)));
3454 1195372 : if (ae->kind == CONSTANT)
3455 186528 : add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3456 : pred, UNKNOWN_LOCATION);
3457 : else
3458 1008844 : add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3459 : }
3460 :
3461 487424 : newphi = get_or_alloc_expr_for_name (temp);
3462 487424 : 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 487424 : bitmap_insert_into_set (PHI_GEN (block), newphi);
3479 487424 : bitmap_value_replace_in_set (AVAIL_OUT (block),
3480 : newphi);
3481 487424 : if (NEW_SETS (block))
3482 487424 : 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 487424 : if (expr->kind == NARY
3489 185064 : && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3490 55268 : && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3491 55093 : && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3492 45142 : && INTEGRAL_TYPE_P (type)
3493 44308 : && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3494 43262 : && (TYPE_PRECISION (type)
3495 43262 : >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3496 522920 : && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3497 : {
3498 22253 : int_range_max r;
3499 44506 : if (get_range_query (cfun)->range_of_expr (r, expr->u.nary->op[0])
3500 22253 : && !r.undefined_p ()
3501 22253 : && !r.varying_p ()
3502 44506 : && !wi::neg_p (r.lower_bound (), SIGNED)
3503 61032 : && !wi::neg_p (r.upper_bound (), SIGNED))
3504 : {
3505 : /* Just handle extension and sign-changes of all-positive ranges. */
3506 15797 : range_cast (r, type);
3507 15797 : set_range_info (temp, r);
3508 : }
3509 22253 : }
3510 :
3511 487424 : 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 487424 : pre_stats.phis++;
3518 487424 : 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 3733542 : do_pre_regular_insertion (basic_block block, basic_block dom,
3548 : vec<pre_expr> exprs)
3549 : {
3550 3733542 : bool new_stuff = false;
3551 3733542 : pre_expr expr;
3552 3733542 : auto_vec<pre_expr, 2> avail;
3553 3733542 : int i;
3554 :
3555 3733542 : avail.safe_grow (EDGE_COUNT (block->preds), true);
3556 :
3557 29679823 : FOR_EACH_VEC_ELT (exprs, i, expr)
3558 : {
3559 22212739 : if (expr->kind == NARY
3560 22212739 : || expr->kind == REFERENCE)
3561 : {
3562 12548154 : unsigned int val;
3563 12548154 : bool by_some = false;
3564 12548154 : bool cant_insert = false;
3565 12548154 : bool all_same = true;
3566 12548154 : unsigned num_inserts = 0;
3567 12548154 : unsigned num_const = 0;
3568 12548154 : pre_expr first_s = NULL;
3569 12548154 : edge pred;
3570 12548154 : basic_block bprime;
3571 12548154 : pre_expr eprime = NULL;
3572 12548154 : edge_iterator ei;
3573 12548154 : pre_expr edoubleprime = NULL;
3574 12548154 : bool do_insertion = false;
3575 :
3576 12548154 : val = get_expr_value_id (expr);
3577 25096308 : if (bitmap_set_contains_value (PHI_GEN (block), val))
3578 1050631 : continue;
3579 11748048 : if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3580 : {
3581 250525 : 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 250525 : continue;
3588 : }
3589 :
3590 37553527 : FOR_EACH_EDGE (pred, ei, block->preds)
3591 : {
3592 26056861 : unsigned int vprime;
3593 :
3594 : /* We should never run insertion for the exit block
3595 : and so not come across fake pred edges. */
3596 26056861 : gcc_assert (!(pred->flags & EDGE_FAKE));
3597 26056861 : bprime = pred->src;
3598 : /* We are looking at ANTIC_OUT of bprime. */
3599 26056861 : 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 26056861 : if (eprime == NULL)
3611 : {
3612 857 : avail[pred->dest_idx] = NULL;
3613 857 : cant_insert = true;
3614 857 : break;
3615 : }
3616 :
3617 26056004 : vprime = get_expr_value_id (eprime);
3618 26056004 : edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3619 : vprime);
3620 26056004 : if (edoubleprime == NULL)
3621 : {
3622 23412704 : avail[pred->dest_idx] = eprime;
3623 23412704 : all_same = false;
3624 23412704 : num_inserts++;
3625 : }
3626 : else
3627 : {
3628 2643300 : avail[pred->dest_idx] = edoubleprime;
3629 2643300 : by_some = true;
3630 2643300 : if (edoubleprime->kind == CONSTANT)
3631 1750345 : 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 2643300 : if (optimize_edge_for_speed_p (pred))
3635 2207428 : do_insertion = true;
3636 2643300 : if (first_s == NULL)
3637 : first_s = edoubleprime;
3638 296937 : else if (!pre_expr_d::equal (first_s, edoubleprime))
3639 226972 : 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 11497523 : 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 2343898 : if (num_inserts == 0 && num_const <= 1)
3653 : do_insertion = true;
3654 2196521 : if (!do_insertion)
3655 : {
3656 386239 : 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 1957659 : else if (dbg_cnt (treepre_insert))
3666 : {
3667 1957659 : 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 1957659 : if (insert_into_preds_of_block (block,
3676 : get_expression_id (expr),
3677 : avail))
3678 11497523 : 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 9153625 : else if (!cant_insert
3685 9153625 : && all_same
3686 9153625 : && (edoubleprime->kind != NAME
3687 835 : || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI
3688 : (PRE_EXPR_NAME (edoubleprime))))
3689 : {
3690 2439 : gcc_assert (edoubleprime->kind == CONSTANT
3691 : || edoubleprime->kind == NAME);
3692 :
3693 2439 : tree temp = make_temp_ssa_name (get_expr_type (expr),
3694 : NULL, "pretmp");
3695 2439 : gassign *assign
3696 2439 : = gimple_build_assign (temp,
3697 2439 : edoubleprime->kind == CONSTANT ?
3698 : PRE_EXPR_CONSTANT (edoubleprime) :
3699 : PRE_EXPR_NAME (edoubleprime));
3700 2439 : gimple_stmt_iterator gsi = gsi_after_labels (block);
3701 2439 : gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3702 :
3703 2439 : vn_ssa_aux_t vn_info = VN_INFO (temp);
3704 2439 : vn_info->value_id = val;
3705 2439 : vn_info->valnum = vn_valnum_from_value_id (val);
3706 2439 : if (vn_info->valnum == NULL_TREE)
3707 523 : vn_info->valnum = temp;
3708 2439 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3709 2439 : pre_expr newe = get_or_alloc_expr_for_name (temp);
3710 2439 : add_to_value (val, newe);
3711 2439 : bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3712 2439 : bitmap_insert_into_set (NEW_SETS (block), newe);
3713 2439 : bitmap_insert_into_set (PHI_GEN (block), newe);
3714 : }
3715 : }
3716 : }
3717 :
3718 3733542 : return new_stuff;
3719 3733542 : }
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 334133 : do_pre_partial_partial_insertion (basic_block block, basic_block dom,
3730 : vec<pre_expr> exprs)
3731 : {
3732 334133 : bool new_stuff = false;
3733 334133 : pre_expr expr;
3734 334133 : auto_vec<pre_expr, 2> avail;
3735 334133 : int i;
3736 :
3737 334133 : avail.safe_grow (EDGE_COUNT (block->preds), true);
3738 :
3739 3568438 : FOR_EACH_VEC_ELT (exprs, i, expr)
3740 : {
3741 2900172 : if (expr->kind == NARY
3742 2900172 : || expr->kind == REFERENCE)
3743 : {
3744 2203506 : unsigned int val;
3745 2203506 : bool by_all = true;
3746 2203506 : bool cant_insert = false;
3747 2203506 : edge pred;
3748 2203506 : basic_block bprime;
3749 2203506 : pre_expr eprime = NULL;
3750 2203506 : edge_iterator ei;
3751 :
3752 2203506 : val = get_expr_value_id (expr);
3753 4407012 : if (bitmap_set_contains_value (PHI_GEN (block), val))
3754 57225 : continue;
3755 2194088 : if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3756 47807 : continue;
3757 :
3758 2221874 : FOR_EACH_EDGE (pred, ei, block->preds)
3759 : {
3760 2211872 : unsigned int vprime;
3761 2211872 : pre_expr edoubleprime;
3762 :
3763 : /* We should never run insertion for the exit block
3764 : and so not come across fake pred edges. */
3765 2211872 : gcc_assert (!(pred->flags & EDGE_FAKE));
3766 2211872 : bprime = pred->src;
3767 4423744 : eprime = phi_translate (NULL, expr, ANTIC_IN (block),
3768 2211872 : 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 2211872 : if (eprime == NULL)
3780 : {
3781 22 : avail[pred->dest_idx] = NULL;
3782 22 : cant_insert = true;
3783 22 : break;
3784 : }
3785 :
3786 2211850 : vprime = get_expr_value_id (eprime);
3787 2211850 : edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3788 2211850 : avail[pred->dest_idx] = edoubleprime;
3789 2211850 : 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 2146281 : if (!cant_insert && by_all)
3801 : {
3802 10002 : edge succ;
3803 10002 : 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 27648 : FOR_EACH_EDGE (succ, ei, block->succs)
3811 : {
3812 17646 : if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3813 17646 : || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3814 : {
3815 9264 : if (optimize_edge_for_speed_p (succ))
3816 17646 : do_insertion = true;
3817 : }
3818 : }
3819 :
3820 10002 : if (!do_insertion)
3821 : {
3822 3696 : 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 6306 : else if (dbg_cnt (treepre_insert))
3832 : {
3833 6306 : pre_stats.pa_insert++;
3834 6306 : 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 6306 : if (insert_into_preds_of_block (block,
3843 : get_expression_id (expr),
3844 : avail))
3845 10002 : new_stuff = true;
3846 : }
3847 : }
3848 : }
3849 : }
3850 :
3851 334133 : return new_stuff;
3852 334133 : }
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 4816360 : do_hoist_insertion (basic_block block)
3860 : {
3861 4816360 : edge e;
3862 4816360 : edge_iterator ei;
3863 4816360 : bool new_stuff = false;
3864 4816360 : unsigned i;
3865 4816360 : gimple_stmt_iterator last;
3866 :
3867 : /* At least two successors, or else... */
3868 4816360 : 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 14539627 : FOR_EACH_EDGE (e, ei, block->succs)
3875 9730731 : 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 4808896 : last = gsi_last_bb (block);
3881 4808896 : if (!gsi_end_p (last)
3882 4808444 : && !is_ctrl_stmt (gsi_stmt (last))
3883 5396207 : && 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 4222165 : bitmap_set_t ANTIC_OUT = bitmap_set_new ();
3891 4222165 : bool first = true;
3892 12765585 : FOR_EACH_EDGE (e, ei, block->succs)
3893 : {
3894 8543420 : if (first)
3895 : {
3896 4222165 : phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
3897 4222165 : first = false;
3898 : }
3899 4321255 : 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 4321248 : bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
3910 4321248 : bitmap_ior_into (&ANTIC_OUT->expressions,
3911 4321248 : &ANTIC_IN (e->dest)->expressions);
3912 : }
3913 : }
3914 :
3915 : /* Compute the set of hoistable expressions from ANTIC_OUT. First compute
3916 : hoistable values. */
3917 4222165 : bitmap_set hoistable_set;
3918 :
3919 : /* A hoistable value must be in ANTIC_OUT(block)
3920 : but not in AVAIL_OUT(BLOCK). */
3921 4222165 : bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
3922 4222165 : bitmap_and_compl (&hoistable_set.values,
3923 4222165 : &ANTIC_OUT->values, &AVAIL_OUT (block)->values);
3924 :
3925 : /* Short-cut for a common case: hoistable_set is empty. */
3926 4222165 : if (bitmap_empty_p (&hoistable_set.values))
3927 : {
3928 3460901 : bitmap_set_free (ANTIC_OUT);
3929 3460901 : 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 761264 : bitmap_head availout_in_some;
3935 761264 : bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
3936 2300074 : 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 1538810 : if (! loop_exit_edge_p (block->loop_father, e))
3942 1370419 : bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
3943 1370419 : &AVAIL_OUT (e->dest)->values);
3944 761264 : bitmap_clear (&hoistable_set.values);
3945 :
3946 : /* Short-cut for a common case: availout_in_some is empty. */
3947 761264 : if (bitmap_empty_p (&availout_in_some))
3948 : {
3949 615900 : bitmap_set_free (ANTIC_OUT);
3950 615900 : return false;
3951 : }
3952 :
3953 : /* Hack hoistable_set in-place so we can use sorted_array_from_bitmap_set. */
3954 145364 : bitmap_move (&hoistable_set.values, &availout_in_some);
3955 145364 : hoistable_set.expressions = ANTIC_OUT->expressions;
3956 :
3957 : /* Now finally construct the topological-ordered expression set. */
3958 145364 : 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 145364 : pre_expr expr;
3963 522568 : 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 231840 : unsigned int value_id = get_expr_value_id (expr);
3969 463680 : 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 37 : 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 231840 : if (expr->kind == REFERENCE
3987 102992 : && PRE_EXPR_REFERENCE (expr)->punned
3988 231840 : && 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 231840 : 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 231830 : pre_stats.hoist_insert++;
4000 231830 : 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 231830 : gimple_seq stmts = NULL;
4010 231830 : 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 231830 : if (gimple_seq_empty_p (stmts))
4016 : res = NULL_TREE;
4017 : else
4018 : {
4019 231803 : if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
4020 231803 : 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 231803 : if (! res)
4029 27 : continue;
4030 :
4031 231803 : new_stuff = true;
4032 : }
4033 :
4034 145364 : exprs.release ();
4035 145364 : bitmap_clear (&hoistable_set.values);
4036 145364 : bitmap_set_free (ANTIC_OUT);
4037 :
4038 145364 : return new_stuff;
4039 : }
4040 :
4041 : /* Perform insertion of partially redundant and hoistable values. */
4042 :
4043 : static void
4044 983305 : insert (void)
4045 : {
4046 983305 : basic_block bb;
4047 :
4048 16362071 : FOR_ALL_BB_FN (bb, cfun)
4049 15378766 : NEW_SETS (bb) = bitmap_set_new ();
4050 :
4051 983305 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
4052 983305 : int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun) + 1);
4053 983305 : int rpo_num = pre_and_rev_post_order_compute (NULL, rpo, false);
4054 15378766 : for (int i = 0; i < rpo_num; ++i)
4055 13412156 : bb_rpo[rpo[i]] = i;
4056 :
4057 : int num_iterations = 0;
4058 1036025 : bool changed;
4059 1036025 : do
4060 : {
4061 1036025 : num_iterations++;
4062 1036025 : if (dump_file && dump_flags & TDF_DETAILS)
4063 18 : fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
4064 :
4065 1036025 : changed = false;
4066 18725492 : for (int idx = 0; idx < rpo_num; ++idx)
4067 : {
4068 17689467 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
4069 17689467 : basic_block dom = get_immediate_dominator (CDI_DOMINATORS, block);
4070 17689467 : if (dom)
4071 : {
4072 17689467 : unsigned i;
4073 17689467 : bitmap_iterator bi;
4074 17689467 : 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 17689467 : 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 17689467 : bool avail_out_changed = false;
4085 33634887 : FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
4086 : {
4087 15945420 : pre_expr expr = expression_for_id (i);
4088 15945420 : bitmap_value_replace_in_set (NEW_SETS (block), expr);
4089 15945420 : avail_out_changed
4090 15945420 : |= 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 17689467 : if (avail_out_changed && !changed)
4095 : {
4096 1728819 : edge_iterator ei;
4097 1728819 : edge e;
4098 4115996 : FOR_EACH_EDGE (e, ei, block->succs)
4099 2387177 : if (e->dest->index != EXIT_BLOCK
4100 2278813 : && bb_rpo[e->dest->index] < idx)
4101 2387177 : changed = true;
4102 : }
4103 :
4104 : /* Insert expressions for partial redundancies. */
4105 35378076 : if (flag_tree_pre && !single_pred_p (block))
4106 : {
4107 3461675 : vec<pre_expr> exprs
4108 3461675 : = sorted_array_from_bitmap_set (ANTIC_IN (block), true);
4109 : /* Sorting is not perfect, iterate locally. */
4110 7195217 : while (do_pre_regular_insertion (block, dom, exprs))
4111 : ;
4112 3461675 : exprs.release ();
4113 3461675 : if (do_partial_partial)
4114 : {
4115 331121 : exprs = sorted_array_from_bitmap_set (PA_IN (block),
4116 : true);
4117 665254 : while (do_pre_partial_partial_insertion (block, dom,
4118 : exprs))
4119 : ;
4120 331121 : 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 1036025 : if (changed)
4129 4435471 : FOR_ALL_BB_FN (bb, cfun)
4130 8765502 : bitmap_set_free (NEW_SETS (bb));
4131 : }
4132 : while (changed);
4133 :
4134 983305 : 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 16362071 : FOR_ALL_BB_FN (bb, cfun)
4139 : {
4140 15378766 : bitmap_set_free (NEW_SETS (bb));
4141 15378766 : bitmap_set_pool.remove (NEW_SETS (bb));
4142 15378766 : 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 983305 : if (flag_code_hoisting)
4152 14394912 : for (int idx = rpo_num - 1; idx >= 0; --idx)
4153 : {
4154 13411660 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
4155 18228020 : if (EDGE_COUNT (block->succs) >= 2)
4156 4816360 : changed |= do_hoist_insertion (block);
4157 : }
4158 :
4159 983305 : free (rpo);
4160 983305 : free (bb_rpo);
4161 983305 : }
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 983305 : compute_avail (function *fun)
4176 : {
4177 :
4178 983305 : basic_block block, son;
4179 983305 : basic_block *worklist;
4180 983305 : size_t sp = 0;
4181 983305 : unsigned i;
4182 983305 : 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 48162729 : FOR_EACH_SSA_NAME (i, name, fun)
4187 : {
4188 34317179 : pre_expr e;
4189 34317179 : if (!SSA_NAME_IS_DEFAULT_DEF (name)
4190 2977332 : || has_zero_uses (name)
4191 36744548 : || virtual_operand_p (name))
4192 32872305 : continue;
4193 :
4194 1444874 : e = get_or_alloc_expr_for_name (name);
4195 1444874 : add_to_value (get_expr_value_id (e), e);
4196 1444874 : bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun)), e);
4197 1444874 : bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun)),
4198 : e);
4199 : }
4200 :
4201 983305 : 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 983305 : 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 983305 : for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (fun));
4215 1966610 : son;
4216 983305 : son = next_dom_son (CDI_DOMINATORS, son))
4217 983305 : worklist[sp++] = son;
4218 :
4219 1966610 : BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (fun))
4220 983305 : = ssa_default_def (fun, gimple_vop (fun));
4221 :
4222 : /* Loop until the worklist is empty. */
4223 14395461 : while (sp)
4224 : {
4225 13412156 : gimple *stmt;
4226 13412156 : basic_block dom;
4227 :
4228 : /* Pick a block from the worklist. */
4229 13412156 : block = worklist[--sp];
4230 13412156 : vn_context_bb = block;
4231 :
4232 : /* Initially, the set of available values in BLOCK is that of
4233 : its immediate dominator. */
4234 13412156 : dom = get_immediate_dominator (CDI_DOMINATORS, block);
4235 13412156 : if (dom)
4236 : {
4237 13412156 : bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
4238 13412156 : BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
4239 : }
4240 :
4241 : /* Generate values for PHI nodes. */
4242 17306733 : for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
4243 3894577 : gsi_next (&gsi))
4244 : {
4245 3894577 : 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 7789154 : if (virtual_operand_p (result))
4250 : {
4251 1774345 : BB_LIVE_VOP_ON_EXIT (block) = result;
4252 1774345 : continue;
4253 : }
4254 :
4255 2120232 : pre_expr e = get_or_alloc_expr_for_name (result);
4256 2120232 : add_to_value (get_expr_value_id (e), e);
4257 2120232 : bitmap_value_insert_into_set (AVAIL_OUT (block), e);
4258 2120232 : bitmap_insert_into_set (PHI_GEN (block), e);
4259 : }
4260 :
4261 13412156 : BB_MAY_NOTRETURN (block) = 0;
4262 :
4263 : /* Now compute value numbers and populate value sets with all
4264 : the expressions computed in BLOCK. */
4265 13412156 : bool set_bb_may_notreturn = false;
4266 113446959 : for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
4267 86622647 : gsi_next (&gsi))
4268 : {
4269 86622647 : ssa_op_iter iter;
4270 86622647 : tree op;
4271 :
4272 86622647 : stmt = gsi_stmt (gsi);
4273 :
4274 86622647 : if (set_bb_may_notreturn)
4275 : {
4276 2771530 : BB_MAY_NOTRETURN (block) = 1;
4277 2771530 : 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 86622647 : 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 3907243 : int flags = gimple_call_flags (stmt);
4291 3907243 : if (!(flags & (ECF_CONST|ECF_PURE))
4292 594956 : || (flags & ECF_LOOPING_CONST_OR_PURE)
4293 4475231 : || 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 101830136 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
4300 : {
4301 15207489 : pre_expr e = get_or_alloc_expr_for_name (op);
4302 15207489 : add_to_value (get_expr_value_id (e), e);
4303 15207489 : bitmap_insert_into_set (TMP_GEN (block), e);
4304 15207489 : bitmap_value_insert_into_set (AVAIL_OUT (block), e);
4305 : }
4306 :
4307 114048209 : if (gimple_vdef (stmt))
4308 12147592 : BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
4309 :
4310 86622647 : if (gimple_has_side_effects (stmt)
4311 80234390 : || stmt_could_throw_p (fun, stmt)
4312 165693985 : || is_gimple_debug (stmt))
4313 81294459 : continue;
4314 :
4315 47974824 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
4316 : {
4317 22623237 : if (ssa_undefined_value_p (op))
4318 59330 : continue;
4319 22563907 : pre_expr e = get_or_alloc_expr_for_name (op);
4320 22563907 : bitmap_value_insert_into_set (EXP_GEN (block), e);
4321 : }
4322 :
4323 25351587 : switch (gimple_code (stmt))
4324 : {
4325 958267 : case GIMPLE_RETURN:
4326 958267 : continue;
4327 :
4328 565991 : case GIMPLE_CALL:
4329 565991 : {
4330 565991 : vn_reference_t ref;
4331 565991 : vn_reference_s ref1;
4332 565991 : pre_expr result = NULL;
4333 :
4334 565991 : vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
4335 : /* There is no point to PRE a call without a value. */
4336 565991 : if (!ref || !ref->result)
4337 32412 : 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 533579 : if ((!gimple_vuse (stmt)
4343 306046 : || gimple_code
4344 306046 : (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
4345 278946 : || 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 782782 : && (!BB_MAY_NOTRETURN (block)
4351 10686 : || !vn_reference_may_trap (ref)))
4352 : {
4353 466050 : result = get_or_alloc_expr_for_reference
4354 466050 : (ref, ref->value_id, gimple_location (stmt));
4355 466050 : add_to_value (get_expr_value_id (result), result);
4356 466050 : bitmap_value_insert_into_set (EXP_GEN (block), result);
4357 : }
4358 533579 : continue;
4359 533579 : }
4360 :
4361 18499141 : case GIMPLE_ASSIGN:
4362 18499141 : {
4363 18499141 : pre_expr result = NULL;
4364 18499141 : switch (vn_get_stmt_kind (stmt))
4365 : {
4366 7695292 : case VN_NARY:
4367 7695292 : {
4368 7695292 : enum tree_code code = gimple_assign_rhs_code (stmt);
4369 7695292 : 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 7695292 : if (code == COND_EXPR)
4375 145628 : continue;
4376 :
4377 7691071 : vn_nary_op_lookup_stmt (stmt, &nary);
4378 7691071 : if (!nary || nary->predicated_values)
4379 111346 : continue;
4380 :
4381 7579725 : unsigned value_id = nary->value_id;
4382 7579725 : if (value_id_constant_p (value_id))
4383 0 : continue;
4384 :
4385 : /* Record the un-valueized expression for EXP_GEN. */
4386 7579725 : nary = XALLOCAVAR (struct vn_nary_op_s,
4387 : sizeof_vn_nary_op
4388 : (vn_nary_length_from_stmt (stmt)));
4389 7579725 : 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 7609786 : if (BB_MAY_NOTRETURN (block)
4395 7579725 : && vn_nary_may_trap (nary))
4396 30061 : continue;
4397 :
4398 7549664 : result = get_or_alloc_expr_for_nary
4399 7549664 : (nary, value_id, gimple_location (stmt));
4400 7549664 : break;
4401 : }
4402 :
4403 5219693 : case VN_REFERENCE:
4404 5219693 : {
4405 5219693 : 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 5219693 : if (!is_gimple_reg_type (TREE_TYPE (rhs1)))
4410 1487417 : continue;
4411 4852821 : ao_ref rhs1_ref;
4412 4852821 : ao_ref_init (&rhs1_ref, rhs1);
4413 4852821 : alias_set_type set = ao_ref_alias_set (&rhs1_ref);
4414 4852821 : alias_set_type base_set
4415 4852821 : = ao_ref_base_alias_set (&rhs1_ref);
4416 4852821 : vec<vn_reference_op_s> operands
4417 4852821 : = vn_reference_operands_for_lookup (rhs1);
4418 4852821 : vn_reference_t ref;
4419 :
4420 : /* We handle &MEM[ptr + 5].b[1].c as
4421 : POINTER_PLUS_EXPR. */
4422 4852821 : if (operands[0].opcode == ADDR_EXPR
4423 5118148 : && operands.last ().opcode == SSA_NAME)
4424 : {
4425 265315 : tree ops[2];
4426 265315 : if (vn_pp_nary_for_addr (operands, ops))
4427 : {
4428 177468 : vn_nary_op_t nary;
4429 177468 : vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4430 177468 : TREE_TYPE (rhs1), ops,
4431 : &nary);
4432 177468 : operands.release ();
4433 177468 : if (nary && !nary->predicated_values)
4434 : {
4435 177466 : unsigned value_id = nary->value_id;
4436 177466 : if (value_id_constant_p (value_id))
4437 2 : continue;
4438 177466 : result = get_or_alloc_expr_for_nary
4439 177466 : (nary, value_id, gimple_location (stmt));
4440 177466 : break;
4441 : }
4442 2 : continue;
4443 2 : }
4444 : }
4445 :
4446 9350706 : vn_reference_lookup_pieces (gimple_vuse (stmt), set,
4447 4675353 : 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 4678362 : if (!ref
4453 4675353 : || !useless_type_conversion_p (TREE_TYPE (rhs1),
4454 : ref->type))
4455 : {
4456 3009 : operands.release ();
4457 3009 : continue;
4458 : }
4459 4672344 : 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 4812293 : if (BB_MAY_NOTRETURN (block)
4465 4672344 : && gimple_could_trap_p_1 (stmt, true, false))
4466 139949 : 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 9064790 : if (gimple_vuse (stmt))
4472 : {
4473 4444553 : gimple *def_stmt;
4474 4444553 : bool ok = true;
4475 4444553 : def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
4476 7177601 : while (!gimple_nop_p (def_stmt)
4477 6184747 : && gimple_code (def_stmt) != GIMPLE_PHI
4478 12143480 : && gimple_bb (def_stmt) == block)
4479 : {
4480 3710633 : if (stmt_may_clobber_ref_p
4481 3710633 : (def_stmt, gimple_assign_rhs1 (stmt)))
4482 : {
4483 : ok = false;
4484 : break;
4485 : }
4486 2733048 : def_stmt
4487 2733048 : = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
4488 : }
4489 4444553 : if (!ok)
4490 977585 : continue;
4491 : }
4492 :
4493 : /* Record the un-valueized expression for EXP_GEN. */
4494 3554810 : copy_reference_ops_from_ref (rhs1, &operands);
4495 3554810 : vn_reference_t newref
4496 3554810 : = XALLOCAVAR (struct vn_reference_s,
4497 : sizeof (vn_reference_s));
4498 3554810 : memset (newref, 0, sizeof (vn_reference_s));
4499 3554810 : newref->value_id = ref->value_id;
4500 3554810 : newref->vuse = ref->vuse;
4501 3554810 : newref->operands = operands;
4502 3554810 : newref->type = TREE_TYPE (rhs1);
4503 3554810 : newref->set = set;
4504 3554810 : newref->base_set = base_set;
4505 3554810 : newref->offset = 0;
4506 3554810 : newref->max_size = -1;
4507 3554810 : newref->result = ref->result;
4508 3554810 : newref->hashcode = vn_reference_compute_hash (newref);
4509 :
4510 3554810 : result = get_or_alloc_expr_for_reference
4511 3554810 : (newref, newref->value_id,
4512 : gimple_location (stmt), true);
4513 3554810 : break;
4514 : }
4515 :
4516 5584156 : default:
4517 5584156 : continue;
4518 5584156 : }
4519 :
4520 11281940 : add_to_value (get_expr_value_id (result), result);
4521 11281940 : bitmap_value_insert_into_set (EXP_GEN (block), result);
4522 11281940 : continue;
4523 11281940 : }
4524 5328188 : default:
4525 5328188 : break;
4526 958267 : }
4527 : }
4528 13412156 : if (set_bb_may_notreturn)
4529 : {
4530 569720 : BB_MAY_NOTRETURN (block) = 1;
4531 569720 : set_bb_may_notreturn = false;
4532 : }
4533 :
4534 13412156 : 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 13412156 : for (son = first_dom_son (CDI_DOMINATORS, block);
4549 25841007 : son;
4550 12428851 : son = next_dom_son (CDI_DOMINATORS, son))
4551 12428851 : worklist[sp++] = son;
4552 : }
4553 983305 : vn_context_bb = NULL;
4554 :
4555 983305 : free (worklist);
4556 983305 : }
4557 :
4558 :
4559 : /* Initialize data structures used by PRE. */
4560 :
4561 : static void
4562 983312 : init_pre (void)
4563 : {
4564 983312 : basic_block bb;
4565 :
4566 983312 : next_expression_id = 1;
4567 983312 : expressions.create (0);
4568 983312 : expressions.safe_push (NULL);
4569 983312 : value_expressions.create (get_max_value_id () + 1);
4570 983312 : value_expressions.quick_grow_cleared (get_max_value_id () + 1);
4571 983312 : constant_value_expressions.create (get_max_constant_value_id () + 1);
4572 983312 : constant_value_expressions.quick_grow_cleared (get_max_constant_value_id () + 1);
4573 983312 : name_to_id.create (0);
4574 983312 : gcc_obstack_init (&pre_expr_obstack);
4575 :
4576 983312 : inserted_exprs = BITMAP_ALLOC (NULL);
4577 :
4578 983312 : connect_infinite_loops_to_exit ();
4579 983312 : memset (&pre_stats, 0, sizeof (pre_stats));
4580 :
4581 983312 : alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4582 :
4583 983312 : calculate_dominance_info (CDI_DOMINATORS);
4584 :
4585 983312 : bitmap_obstack_initialize (&grand_bitmap_obstack);
4586 1966624 : expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4587 16399642 : FOR_ALL_BB_FN (bb, cfun)
4588 : {
4589 15416330 : EXP_GEN (bb) = bitmap_set_new ();
4590 15416330 : PHI_GEN (bb) = bitmap_set_new ();
4591 15416330 : TMP_GEN (bb) = bitmap_set_new ();
4592 15416330 : AVAIL_OUT (bb) = bitmap_set_new ();
4593 15416330 : PHI_TRANS_TABLE (bb) = NULL;
4594 : }
4595 983312 : }
4596 :
4597 :
4598 : /* Deallocate data structures used by PRE. */
4599 :
4600 : static void
4601 983312 : fini_pre ()
4602 : {
4603 983312 : value_expressions.release ();
4604 983312 : constant_value_expressions.release ();
4605 46136079 : for (unsigned i = 1; i < expressions.length (); ++i)
4606 44169455 : if (expressions[i]->kind == REFERENCE)
4607 6465557 : PRE_EXPR_REFERENCE (expressions[i])->operands.release ();
4608 983312 : expressions.release ();
4609 983312 : bitmap_obstack_release (&grand_bitmap_obstack);
4610 983312 : bitmap_set_pool.release ();
4611 983312 : pre_expr_pool.release ();
4612 983312 : delete expression_to_id;
4613 983312 : expression_to_id = NULL;
4614 983312 : name_to_id.release ();
4615 983312 : obstack_free (&pre_expr_obstack, NULL);
4616 :
4617 983312 : basic_block bb;
4618 16399328 : FOR_ALL_BB_FN (bb, cfun)
4619 15416016 : if (bb->aux && PHI_TRANS_TABLE (bb))
4620 6207945 : delete PHI_TRANS_TABLE (bb);
4621 983312 : free_aux_for_blocks ();
4622 983312 : }
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 294587 : pass_pre (gcc::context *ctxt)
4643 589174 : : gimple_opt_pass (pass_data_pre, ctxt)
4644 : {}
4645 :
4646 : /* opt_pass methods: */
4647 1062413 : bool gate (function *) final override
4648 1062413 : { 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 128443960 : pre_valueize (tree name)
4658 : {
4659 128443960 : if (TREE_CODE (name) == SSA_NAME)
4660 : {
4661 128175739 : tree tem = VN_INFO (name)->valnum;
4662 128175739 : if (tem != VN_TOP && tem != name)
4663 : {
4664 17322431 : if (TREE_CODE (tem) != SSA_NAME
4665 17322431 : || 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 17317557 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (tem));
4671 17317557 : if (! def_bb
4672 17317557 : || dominated_by_p (CDI_DOMINATORS, vn_context_bb, def_bb))
4673 128879 : 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 983312 : pass_pre::execute (function *fun)
4683 : {
4684 983312 : unsigned int todo = 0;
4685 :
4686 1966624 : do_partial_partial =
4687 983312 : 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 983312 : loop_optimizer_init (LOOPS_NORMAL);
4692 983312 : split_edges_for_insertion ();
4693 983312 : scev_initialize ();
4694 983312 : calculate_dominance_info (CDI_DOMINATORS);
4695 :
4696 983312 : run_rpo_vn (VN_WALK);
4697 :
4698 983312 : init_pre ();
4699 :
4700 983312 : 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 983312 : if (n_basic_blocks_for_fn (fun) < 4000)
4709 : {
4710 983305 : compute_avail (fun);
4711 983305 : compute_antic ();
4712 983305 : 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 983312 : remove_fake_exit_edges ();
4719 983312 : gsi_commit_edge_inserts ();
4720 :
4721 : /* Eliminate folds statements which might (should not...) end up
4722 : not keeping virtual operands up-to-date. */
4723 983312 : gcc_assert (!need_ssa_update_p (fun));
4724 :
4725 983312 : statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4726 983312 : statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4727 983312 : statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
4728 983312 : statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4729 :
4730 983312 : todo |= eliminate_with_rpo_vn (inserted_exprs);
4731 :
4732 983312 : vn_valueize = NULL;
4733 :
4734 983312 : fini_pre ();
4735 :
4736 983312 : scev_finalize ();
4737 983312 : 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 983312 : bool need_crit_edge_split = false;
4743 983312 : if (todo & TODO_cleanup_cfg)
4744 : {
4745 139912 : cleanup_tree_cfg ();
4746 139912 : 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 983312 : simple_dce_from_worklist (inserted_exprs);
4754 983312 : 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 983312 : todo |= tail_merge_optimize (need_crit_edge_split);
4763 :
4764 983312 : 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 983312 : update_ssa (TODO_update_ssa_only_virtuals);
4772 :
4773 983312 : return todo;
4774 : }
4775 :
4776 : } // anon namespace
4777 :
4778 : gimple_opt_pass *
4779 294587 : make_pass_pre (gcc::context *ctxt)
4780 : {
4781 294587 : return new pass_pre (ctxt);
4782 : }
|