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 55991232 : pre_expr_d::equal (const pre_expr_d *e1, const pre_expr_d *e2)
277 : {
278 55991232 : if (e1->kind != e2->kind)
279 : return false;
280 :
281 35045496 : switch (e1->kind)
282 : {
283 4706162 : case CONSTANT:
284 4706162 : return vn_constant_eq_with_type (PRE_EXPR_CONSTANT (e1),
285 4706162 : PRE_EXPR_CONSTANT (e2));
286 154838 : case NAME:
287 154838 : return PRE_EXPR_NAME (e1) == PRE_EXPR_NAME (e2);
288 21519865 : case NARY:
289 21519865 : return vn_nary_op_eq (PRE_EXPR_NARY (e1), PRE_EXPR_NARY (e2));
290 8664631 : case REFERENCE:
291 8664631 : return vn_reference_eq (PRE_EXPR_REFERENCE (e1),
292 8664631 : PRE_EXPR_REFERENCE (e2), true);
293 0 : default:
294 0 : gcc_unreachable ();
295 : }
296 : }
297 :
298 : /* Hash E. */
299 :
300 : inline hashval_t
301 89444713 : pre_expr_d::hash (const pre_expr_d *e)
302 : {
303 89444713 : switch (e->kind)
304 : {
305 7200871 : case CONSTANT:
306 7200871 : 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 54356904 : case NARY:
310 54356904 : return PRE_EXPR_NARY (e)->hashcode;
311 27886938 : case REFERENCE:
312 27886938 : 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 42834063 : alloc_expression_id (pre_expr expr)
331 : {
332 42834063 : struct pre_expr_d **slot;
333 : /* Make sure we won't overflow. */
334 42834063 : gcc_assert (next_expression_id + 1 > next_expression_id);
335 42834063 : expr->id = next_expression_id++;
336 42834063 : expressions.safe_push (expr);
337 42834063 : if (expr->kind == NAME)
338 : {
339 23369489 : 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 23369489 : unsigned old_len = name_to_id.length ();
343 46738978 : name_to_id.reserve (num_ssa_names - old_len);
344 46738978 : name_to_id.quick_grow_cleared (num_ssa_names);
345 23369489 : gcc_assert (name_to_id[version] == 0);
346 23369489 : name_to_id[version] = expr->id;
347 : }
348 : else
349 : {
350 19464574 : slot = expression_to_id->find_slot (expr, INSERT);
351 19464574 : gcc_assert (!*slot);
352 19464574 : *slot = expr;
353 : }
354 42834063 : return next_expression_id - 1;
355 : }
356 :
357 : /* Return the expression id for tree EXPR. */
358 :
359 : static inline unsigned int
360 252654175 : get_expression_id (const pre_expr expr)
361 : {
362 252654175 : return expr->id;
363 : }
364 :
365 : static inline unsigned int
366 77170911 : lookup_expression_id (const pre_expr expr)
367 : {
368 77170911 : struct pre_expr_d **slot;
369 :
370 77170911 : if (expr->kind == NAME)
371 : {
372 51881823 : unsigned version = SSA_NAME_VERSION (PRE_EXPR_NAME (expr));
373 71346397 : if (name_to_id.length () <= version)
374 : return 0;
375 48930016 : return name_to_id[version];
376 : }
377 : else
378 : {
379 25289088 : slot = expression_to_id->find_slot (expr, NO_INSERT);
380 25289088 : if (!slot)
381 : return 0;
382 5824514 : return ((pre_expr)*slot)->id;
383 : }
384 : }
385 :
386 : /* Return the expression that has expression id ID */
387 :
388 : static inline pre_expr
389 528778906 : expression_for_id (unsigned int id)
390 : {
391 1057557812 : 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 51881823 : get_or_alloc_expr_for_name (tree name)
400 : {
401 51881823 : struct pre_expr_d expr;
402 51881823 : pre_expr result;
403 51881823 : unsigned int result_id;
404 :
405 51881823 : expr.kind = NAME;
406 51881823 : expr.id = 0;
407 51881823 : PRE_EXPR_NAME (&expr) = name;
408 51881823 : result_id = lookup_expression_id (&expr);
409 51881823 : if (result_id != 0)
410 28512334 : return expression_for_id (result_id);
411 :
412 23369489 : result = pre_expr_pool.allocate ();
413 23369489 : result->kind = NAME;
414 23369489 : result->loc = UNKNOWN_LOCATION;
415 23369489 : result->value_id = VN_INFO (name)->value_id;
416 23369489 : PRE_EXPR_NAME (result) = name;
417 23369489 : alloc_expression_id (result);
418 23369489 : 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 13348771 : get_or_alloc_expr_for_nary (vn_nary_op_t nary, unsigned value_id,
427 : location_t loc = UNKNOWN_LOCATION)
428 : {
429 13348771 : struct pre_expr_d expr;
430 13348771 : pre_expr result;
431 13348771 : unsigned int result_id;
432 :
433 13348771 : gcc_assert (value_id == 0 || !value_id_constant_p (value_id));
434 :
435 13348771 : expr.kind = NARY;
436 13348771 : expr.id = 0;
437 13348771 : nary->hashcode = vn_nary_op_compute_hash (nary);
438 13348771 : PRE_EXPR_NARY (&expr) = nary;
439 13348771 : result_id = lookup_expression_id (&expr);
440 13348771 : if (result_id != 0)
441 962080 : return expression_for_id (result_id);
442 :
443 12386691 : result = pre_expr_pool.allocate ();
444 12386691 : result->kind = NARY;
445 12386691 : result->loc = loc;
446 12386691 : result->value_id = value_id ? value_id : get_next_value_id ();
447 12386691 : PRE_EXPR_NARY (result)
448 12386691 : = alloc_vn_nary_op_noinit (nary->length, &pre_expr_obstack);
449 12386691 : memcpy (PRE_EXPR_NARY (result), nary, sizeof_vn_nary_op (nary->length));
450 12386691 : alloc_expression_id (result);
451 12386691 : return result;
452 : }
453 :
454 : /* Given an REFERENCE, get or create a pre_expr to represent it. Assign
455 : VALUE_ID to it or allocate a new value-id if it is zero. Record
456 : LOC as the original location of the expression. If MOVE_OPERANDS
457 : is true then ownership of REFERENCE->operands is transferred, otherwise
458 : a copy is made if necessary. */
459 :
460 : static pre_expr
461 6971149 : get_or_alloc_expr_for_reference (vn_reference_t reference,
462 : unsigned value_id,
463 : location_t loc = UNKNOWN_LOCATION,
464 : bool move_operands = false)
465 : {
466 6971149 : struct pre_expr_d expr;
467 6971149 : pre_expr result;
468 6971149 : unsigned int result_id;
469 :
470 6971149 : expr.kind = REFERENCE;
471 6971149 : expr.id = 0;
472 6971149 : PRE_EXPR_REFERENCE (&expr) = reference;
473 6971149 : result_id = lookup_expression_id (&expr);
474 6971149 : if (result_id != 0)
475 : {
476 727125 : if (move_operands)
477 716377 : reference->operands.release ();
478 727125 : return expression_for_id (result_id);
479 : }
480 :
481 6244024 : result = pre_expr_pool.allocate ();
482 6244024 : result->kind = REFERENCE;
483 6244024 : result->loc = loc;
484 6244024 : result->value_id = value_id ? value_id : get_next_value_id ();
485 6244024 : vn_reference_t ref = XOBNEW (&pre_expr_obstack, struct vn_reference_s);
486 6244024 : *ref = *reference;
487 6244024 : if (!move_operands)
488 450006 : ref->operands = ref->operands.copy ();
489 6244024 : PRE_EXPR_REFERENCE (result) = ref;
490 6244024 : alloc_expression_id (result);
491 6244024 : return result;
492 : }
493 :
494 :
495 : /* An unordered bitmap set. One bitmap tracks values, the other,
496 : expressions. */
497 142858071 : typedef class bitmap_set
498 : {
499 : public:
500 : bitmap_head expressions;
501 : bitmap_head values;
502 : } *bitmap_set_t;
503 :
504 : #define FOR_EACH_EXPR_ID_IN_SET(set, id, bi) \
505 : EXECUTE_IF_SET_IN_BITMAP (&(set)->expressions, 0, (id), (bi))
506 :
507 : #define FOR_EACH_VALUE_ID_IN_SET(set, id, bi) \
508 : EXECUTE_IF_SET_IN_BITMAP (&(set)->values, 0, (id), (bi))
509 :
510 : /* Mapping from value id to expressions with that value_id. */
511 : static vec<bitmap> value_expressions;
512 : /* We just record a single expression for each constant value,
513 : one of kind CONSTANT. */
514 : static vec<pre_expr> constant_value_expressions;
515 :
516 :
517 : /* This structure is used to keep track of statistics on what
518 : optimization PRE was able to perform. */
519 : static struct
520 : {
521 : /* The number of new expressions/temporaries generated by PRE. */
522 : int insertions;
523 :
524 : /* The number of inserts found due to partial anticipation */
525 : int pa_insert;
526 :
527 : /* The number of inserts made for code hoisting. */
528 : int hoist_insert;
529 :
530 : /* The number of new PHI nodes added by PRE. */
531 : int phis;
532 : } pre_stats;
533 :
534 : static bool do_partial_partial;
535 : static pre_expr bitmap_find_leader (bitmap_set_t, unsigned int);
536 : static void bitmap_value_insert_into_set (bitmap_set_t, pre_expr);
537 : static bool bitmap_value_replace_in_set (bitmap_set_t, pre_expr);
538 : static void bitmap_set_copy (bitmap_set_t, bitmap_set_t);
539 : static bool bitmap_set_contains_value (bitmap_set_t, unsigned int);
540 : static void bitmap_insert_into_set (bitmap_set_t, pre_expr);
541 : static bitmap_set_t bitmap_set_new (void);
542 : static tree create_expression_by_pieces (basic_block, pre_expr, gimple_seq *,
543 : tree);
544 : static tree find_or_generate_expression (basic_block, tree, gimple_seq *);
545 : static unsigned int get_expr_value_id (pre_expr);
546 :
547 : /* We can add and remove elements and entries to and from sets
548 : and hash tables, so we use alloc pools for them. */
549 :
550 : static object_allocator<bitmap_set> bitmap_set_pool ("Bitmap sets");
551 : static bitmap_obstack grand_bitmap_obstack;
552 :
553 : /* A three tuple {e, pred, v} used to cache phi translations in the
554 : phi_translate_table. */
555 :
556 : typedef struct expr_pred_trans_d : public typed_noop_remove <expr_pred_trans_d>
557 : {
558 : typedef expr_pred_trans_d value_type;
559 : typedef expr_pred_trans_d compare_type;
560 :
561 : /* The expression ID. */
562 : unsigned e;
563 :
564 : /* The value expression ID that resulted from the translation. */
565 : unsigned v;
566 :
567 : /* hash_table support. */
568 : static inline void mark_empty (expr_pred_trans_d &);
569 : static inline bool is_empty (const expr_pred_trans_d &);
570 : static inline void mark_deleted (expr_pred_trans_d &);
571 : static inline bool is_deleted (const expr_pred_trans_d &);
572 : static const bool empty_zero_p = true;
573 : static inline hashval_t hash (const expr_pred_trans_d &);
574 : static inline int equal (const expr_pred_trans_d &, const expr_pred_trans_d &);
575 : } *expr_pred_trans_t;
576 : typedef const struct expr_pred_trans_d *const_expr_pred_trans_t;
577 :
578 : inline bool
579 1363126197 : expr_pred_trans_d::is_empty (const expr_pred_trans_d &e)
580 : {
581 1363126197 : return e.e == 0;
582 : }
583 :
584 : inline bool
585 272712730 : expr_pred_trans_d::is_deleted (const expr_pred_trans_d &e)
586 : {
587 272712730 : return e.e == -1u;
588 : }
589 :
590 : inline void
591 2110842 : expr_pred_trans_d::mark_empty (expr_pred_trans_d &e)
592 : {
593 2110842 : e.e = 0;
594 : }
595 :
596 : inline void
597 3618735 : expr_pred_trans_d::mark_deleted (expr_pred_trans_d &e)
598 : {
599 3618735 : e.e = -1u;
600 : }
601 :
602 : inline hashval_t
603 : expr_pred_trans_d::hash (const expr_pred_trans_d &e)
604 : {
605 : return e.e;
606 : }
607 :
608 : inline int
609 214193285 : expr_pred_trans_d::equal (const expr_pred_trans_d &ve1,
610 : const expr_pred_trans_d &ve2)
611 : {
612 214193285 : return ve1.e == ve2.e;
613 : }
614 :
615 : /* Sets that we need to keep track of. */
616 : typedef struct bb_bitmap_sets
617 : {
618 : /* The EXP_GEN set, which represents expressions/values generated in
619 : a basic block. */
620 : bitmap_set_t exp_gen;
621 :
622 : /* The PHI_GEN set, which represents PHI results generated in a
623 : basic block. */
624 : bitmap_set_t phi_gen;
625 :
626 : /* The TMP_GEN set, which represents results/temporaries generated
627 : in a basic block. IE the LHS of an expression. */
628 : bitmap_set_t tmp_gen;
629 :
630 : /* The AVAIL_OUT set, which represents which values are available in
631 : a given basic block. */
632 : bitmap_set_t avail_out;
633 :
634 : /* The ANTIC_IN set, which represents which values are anticipatable
635 : in a given basic block. */
636 : bitmap_set_t antic_in;
637 :
638 : /* The PA_IN set, which represents which values are
639 : partially anticipatable in a given basic block. */
640 : bitmap_set_t pa_in;
641 :
642 : /* The NEW_SETS set, which is used during insertion to augment the
643 : AVAIL_OUT set of blocks with the new insertions performed during
644 : the current iteration. */
645 : bitmap_set_t new_sets;
646 :
647 : /* A cache for value_dies_in_block_x. */
648 : bitmap expr_dies;
649 :
650 : /* The live virtual operand on successor edges. */
651 : tree vop_on_exit;
652 :
653 : /* PHI translate cache for the single successor edge. */
654 : hash_table<expr_pred_trans_d> *phi_translate_table;
655 :
656 : /* True if we have visited this block during ANTIC calculation. */
657 : unsigned int visited : 1;
658 :
659 : /* True when the block contains a call that might not return. */
660 : unsigned int contains_may_not_return_call : 1;
661 : } *bb_value_sets_t;
662 :
663 : #define EXP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->exp_gen
664 : #define PHI_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->phi_gen
665 : #define TMP_GEN(BB) ((bb_value_sets_t) ((BB)->aux))->tmp_gen
666 : #define AVAIL_OUT(BB) ((bb_value_sets_t) ((BB)->aux))->avail_out
667 : #define ANTIC_IN(BB) ((bb_value_sets_t) ((BB)->aux))->antic_in
668 : #define PA_IN(BB) ((bb_value_sets_t) ((BB)->aux))->pa_in
669 : #define NEW_SETS(BB) ((bb_value_sets_t) ((BB)->aux))->new_sets
670 : #define EXPR_DIES(BB) ((bb_value_sets_t) ((BB)->aux))->expr_dies
671 : #define PHI_TRANS_TABLE(BB) ((bb_value_sets_t) ((BB)->aux))->phi_translate_table
672 : #define BB_VISITED(BB) ((bb_value_sets_t) ((BB)->aux))->visited
673 : #define BB_MAY_NOTRETURN(BB) ((bb_value_sets_t) ((BB)->aux))->contains_may_not_return_call
674 : #define BB_LIVE_VOP_ON_EXIT(BB) ((bb_value_sets_t) ((BB)->aux))->vop_on_exit
675 :
676 :
677 : /* Add the tuple mapping from {expression E, basic block PRED} to
678 : the phi translation table and return whether it pre-existed. */
679 :
680 : static inline bool
681 84565984 : phi_trans_add (expr_pred_trans_t *entry, pre_expr e, basic_block pred)
682 : {
683 84565984 : if (!PHI_TRANS_TABLE (pred))
684 185680 : PHI_TRANS_TABLE (pred) = new hash_table<expr_pred_trans_d> (11);
685 :
686 84565984 : expr_pred_trans_t slot;
687 84565984 : expr_pred_trans_d tem;
688 84565984 : unsigned id = get_expression_id (e);
689 84565984 : tem.e = id;
690 84565984 : slot = PHI_TRANS_TABLE (pred)->find_slot_with_hash (tem, id, INSERT);
691 84565984 : if (slot->e)
692 : {
693 61571652 : *entry = slot;
694 61571652 : return true;
695 : }
696 :
697 22994332 : *entry = slot;
698 22994332 : slot->e = id;
699 22994332 : return false;
700 : }
701 :
702 :
703 : /* Add expression E to the expression set of value id V. */
704 :
705 : static void
706 44523268 : add_to_value (unsigned int v, pre_expr e)
707 : {
708 0 : gcc_checking_assert (get_expr_value_id (e) == v);
709 :
710 44523268 : if (value_id_constant_p (v))
711 : {
712 876526 : if (e->kind != CONSTANT)
713 : return;
714 :
715 833859 : if (-v >= constant_value_expressions.length ())
716 498864 : constant_value_expressions.safe_grow_cleared (-v + 1);
717 :
718 833859 : pre_expr leader = constant_value_expressions[-v];
719 833859 : if (!leader)
720 833859 : constant_value_expressions[-v] = e;
721 : }
722 : else
723 : {
724 43646742 : if (v >= value_expressions.length ())
725 6893146 : value_expressions.safe_grow_cleared (v + 1);
726 :
727 43646742 : bitmap set = value_expressions[v];
728 43646742 : if (!set)
729 : {
730 24414145 : set = BITMAP_ALLOC (&grand_bitmap_obstack);
731 24414145 : value_expressions[v] = set;
732 : }
733 43646742 : bitmap_set_bit (set, get_expression_id (e));
734 : }
735 : }
736 :
737 : /* Create a new bitmap set and return it. */
738 :
739 : static bitmap_set_t
740 142858071 : bitmap_set_new (void)
741 : {
742 142858071 : bitmap_set_t ret = bitmap_set_pool.allocate ();
743 142858071 : bitmap_initialize (&ret->expressions, &grand_bitmap_obstack);
744 142858071 : bitmap_initialize (&ret->values, &grand_bitmap_obstack);
745 142858071 : return ret;
746 : }
747 :
748 : /* Return the value id for a PRE expression EXPR. */
749 :
750 : static unsigned int
751 525414168 : get_expr_value_id (pre_expr expr)
752 : {
753 : /* ??? We cannot assert that expr has a value-id (it can be 0), because
754 : we assign value-ids only to expressions that have a result
755 : in set_hashtable_value_ids. */
756 44523268 : return expr->value_id;
757 : }
758 :
759 : /* Return a VN valnum (SSA name or constant) for the PRE value-id VAL. */
760 :
761 : static tree
762 1231335 : vn_valnum_from_value_id (unsigned int val)
763 : {
764 1231335 : if (value_id_constant_p (val))
765 : {
766 0 : pre_expr vexpr = constant_value_expressions[-val];
767 0 : if (vexpr)
768 0 : return PRE_EXPR_CONSTANT (vexpr);
769 : return NULL_TREE;
770 : }
771 :
772 1231335 : bitmap exprset = value_expressions[val];
773 1231335 : bitmap_iterator bi;
774 1231335 : unsigned int i;
775 1829989 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
776 : {
777 1490110 : pre_expr vexpr = expression_for_id (i);
778 1490110 : if (vexpr->kind == NAME)
779 891456 : return VN_INFO (PRE_EXPR_NAME (vexpr))->valnum;
780 : }
781 : return NULL_TREE;
782 : }
783 :
784 : /* Insert an expression EXPR into a bitmapped set. */
785 :
786 : static void
787 73968726 : bitmap_insert_into_set (bitmap_set_t set, pre_expr expr)
788 : {
789 73968726 : unsigned int val = get_expr_value_id (expr);
790 73968726 : if (! value_id_constant_p (val))
791 : {
792 : /* Note this is the only function causing multiple expressions
793 : for the same value to appear in a set. This is needed for
794 : TMP_GEN, PHI_GEN and NEW_SETs. */
795 71072359 : bitmap_set_bit (&set->values, val);
796 71072359 : bitmap_set_bit (&set->expressions, get_expression_id (expr));
797 : }
798 73968726 : }
799 :
800 : /* Copy a bitmapped set ORIG, into bitmapped set DEST. */
801 :
802 : static void
803 26337902 : bitmap_set_copy (bitmap_set_t dest, bitmap_set_t orig)
804 : {
805 26337902 : bitmap_copy (&dest->expressions, &orig->expressions);
806 26337902 : bitmap_copy (&dest->values, &orig->values);
807 26337902 : }
808 :
809 :
810 : /* Free memory used up by SET. */
811 : static void
812 71419658 : bitmap_set_free (bitmap_set_t set)
813 : {
814 0 : bitmap_clear (&set->expressions);
815 19012358 : bitmap_clear (&set->values);
816 48330429 : }
817 :
818 :
819 : /* Sort pre_expr after their value-id. */
820 :
821 : static int
822 427731529 : expr_cmp (const void *a_, const void *b_, void *)
823 : {
824 427731529 : pre_expr a = *(pre_expr const *) a_;
825 427731529 : pre_expr b = *(pre_expr const *) b_;
826 427731529 : return a->value_id - b->value_id;
827 : }
828 :
829 : /* Return an expression that is a valid representation to insert for
830 : both A and B reaching expressions. Return NULL if neither works,
831 : in this case all expressions of the value will be elided. */
832 :
833 : static pre_expr
834 241312 : prefer (pre_expr a, pre_expr b)
835 : {
836 241312 : if (a->kind == REFERENCE && b->kind == REFERENCE)
837 : {
838 56159 : auto refa = PRE_EXPR_REFERENCE (a);
839 56159 : auto refb = PRE_EXPR_REFERENCE (b);
840 56159 : auto &oprsa = refa->operands;
841 56159 : auto &oprsb = refb->operands;
842 56159 : pre_expr palias = NULL;
843 56159 : if (refa->set == refb->set
844 53270 : && refa->base_set == refb->base_set)
845 : ;
846 12434 : else if ((refb->set == refa->set
847 2889 : || alias_set_subset_of (refb->set, refa->set))
848 13542 : && (refb->base_set == refa->base_set
849 10030 : || alias_set_subset_of (refb->base_set, refa->base_set)))
850 : palias = a;
851 4134 : else if ((refa->set == refb->set
852 1802 : || alias_set_subset_of (refa->set, refb->set))
853 5798 : && (refa->base_set == refb->base_set
854 3415 : || alias_set_subset_of (refa->base_set, refb->base_set)))
855 : palias = b;
856 : else
857 : /* We have to chose an expression representation that can stand
858 : in for all others - there can be none, in which case we have
859 : to drop this PRE/hoisting opportunity.
860 : ??? Previously we've arranged for alias-set zero being used
861 : as fallback, but we do not really want to allocate a new expression
862 : here unless it proves to be absolutely necessary. */
863 244 : return NULL;
864 55915 : pre_expr p = palias;
865 111830 : if (oprsa.length () > 1 && oprsb.length () > 1)
866 : {
867 55915 : vn_reference_op_t vroa = &oprsa[oprsa.length () - 2];
868 55915 : vn_reference_op_t vrob = &oprsb[oprsb.length () - 2];
869 55915 : if (vroa->opcode == MEM_REF && vrob->opcode == MEM_REF)
870 : {
871 : /* We have to canonicalize to the more conservative alignment.
872 : gcc.dg/torture/pr65270-?.c.*/
873 55894 : pre_expr palign = NULL;
874 55894 : if (TYPE_ALIGN (vroa->type) < TYPE_ALIGN (vrob->type))
875 : palign = a;
876 55753 : else if (TYPE_ALIGN (vroa->type) > TYPE_ALIGN (vrob->type))
877 : palign = b;
878 327 : if (palign)
879 : {
880 327 : if (p && p != palign)
881 : return NULL;
882 : p = palign;
883 : }
884 : /* We have to canonicalize to the more conservative (smaller)
885 : innermost object access size. gcc.dg/torture/pr110799.c. */
886 55665 : if (TYPE_SIZE (vroa->type) != TYPE_SIZE (vrob->type))
887 : {
888 4243 : pre_expr psize = NULL;
889 4243 : if (!TYPE_SIZE (vroa->type))
890 : psize = a;
891 4243 : else if (!TYPE_SIZE (vrob->type))
892 : psize = b;
893 4243 : else if (TREE_CODE (TYPE_SIZE (vroa->type)) == INTEGER_CST
894 4243 : && TREE_CODE (TYPE_SIZE (vrob->type)) == INTEGER_CST)
895 : {
896 4237 : int cmp = tree_int_cst_compare (TYPE_SIZE (vroa->type),
897 4237 : TYPE_SIZE (vrob->type));
898 4237 : if (cmp < 0)
899 : psize = a;
900 2615 : else if (cmp > 0)
901 : psize = b;
902 : }
903 : /* ??? What about non-constant sizes? */
904 4237 : if (psize)
905 : {
906 4237 : if (p && p != psize)
907 : return NULL;
908 : p = psize;
909 : }
910 : }
911 : }
912 : }
913 : /* Note we cannot leave it undecided because when having
914 : more than two expressions we have to keep doing
915 : pariwise reduction. */
916 95859 : return p ? p : b;
917 : }
918 : /* Always prefer an non-REFERENCE, avoiding the above mess. */
919 185153 : else if (a->kind == REFERENCE)
920 : return b;
921 180937 : else if (b->kind == REFERENCE)
922 : return a;
923 153812 : else if (a->kind == b->kind)
924 : ;
925 : /* And prefer NAME over anything else. */
926 11084 : else if (b->kind == NAME)
927 : return b;
928 8235 : else if (a->kind == NAME)
929 8235 : return a;
930 : return b;
931 : }
932 :
933 : static void
934 : pre_expr_DFS (pre_expr expr, bitmap_set_t set, bitmap exclusions,
935 : bitmap val_visited, vec<pre_expr> &post);
936 :
937 : /* DFS walk leaders of VAL to their operands with leaders in SET, collecting
938 : expressions in SET in postorder into POST. */
939 :
940 : static void
941 84161296 : pre_expr_DFS (unsigned val, bitmap_set_t set, bitmap exclusions,
942 : bitmap val_visited, vec<pre_expr> &post)
943 : {
944 84161296 : unsigned int i;
945 84161296 : bitmap_iterator bi;
946 :
947 : /* Iterate over all leaders and DFS recurse. Borrowed from
948 : bitmap_find_leader. */
949 84161296 : bitmap exprset = value_expressions[val];
950 84161296 : if (!exprset->first->next)
951 : {
952 199986992 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
953 126851560 : if (bitmap_bit_p (&set->expressions, i)
954 126851560 : && !bitmap_bit_p (exclusions, i))
955 73245647 : pre_expr_DFS (expression_for_id (i), set, exclusions,
956 : val_visited, post);
957 73135432 : return;
958 : }
959 :
960 22345977 : EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
961 11320113 : if (!bitmap_bit_p (exclusions, i))
962 11175799 : pre_expr_DFS (expression_for_id (i), set, exclusions,
963 : val_visited, post);
964 : }
965 :
966 : /* DFS walk EXPR to its operands with leaders in SET, collecting
967 : expressions in SET in postorder into POST. */
968 :
969 : static void
970 84421446 : pre_expr_DFS (pre_expr expr, bitmap_set_t set, bitmap exclusions,
971 : bitmap val_visited, vec<pre_expr> &post)
972 : {
973 84421446 : switch (expr->kind)
974 : {
975 38929095 : case NARY:
976 38929095 : {
977 38929095 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
978 104640231 : for (unsigned i = 0; i < nary->length; i++)
979 : {
980 65711136 : if (TREE_CODE (nary->op[i]) != SSA_NAME)
981 20253513 : continue;
982 45457623 : unsigned int op_val_id = VN_INFO (nary->op[i])->value_id;
983 : /* If we already found a leader for the value we've
984 : recursed already. Avoid the costly bitmap_find_leader. */
985 45457623 : if (bitmap_bit_p (&set->values, op_val_id)
986 45457623 : && bitmap_set_bit (val_visited, op_val_id))
987 8082157 : pre_expr_DFS (op_val_id, set, exclusions, val_visited, post);
988 : }
989 : break;
990 : }
991 12832628 : case REFERENCE:
992 12832628 : {
993 12832628 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
994 12832628 : vec<vn_reference_op_s> operands = ref->operands;
995 12832628 : vn_reference_op_t operand;
996 50675884 : for (unsigned i = 0; operands.iterate (i, &operand); i++)
997 : {
998 37843256 : tree op[3];
999 37843256 : op[0] = operand->op0;
1000 37843256 : op[1] = operand->op1;
1001 37843256 : op[2] = operand->op2;
1002 151373024 : for (unsigned n = 0; n < 3; ++n)
1003 : {
1004 113529768 : if (!op[n] || TREE_CODE (op[n]) != SSA_NAME)
1005 104845779 : continue;
1006 8683989 : unsigned op_val_id = VN_INFO (op[n])->value_id;
1007 8683989 : if (bitmap_bit_p (&set->values, op_val_id)
1008 8683989 : && bitmap_set_bit (val_visited, op_val_id))
1009 1446905 : pre_expr_DFS (op_val_id, set, exclusions, val_visited, post);
1010 : }
1011 : }
1012 : break;
1013 : }
1014 84421446 : default:;
1015 : }
1016 84421446 : post.quick_push (expr);
1017 84421446 : }
1018 :
1019 : /* Generate an topological-ordered array of bitmap set SET. If FOR_INSERTION
1020 : is true then perform canonexpr on the set. */
1021 :
1022 : static vec<pre_expr>
1023 17942954 : sorted_array_from_bitmap_set (bitmap_set_t set, bool for_insertion)
1024 : {
1025 17942954 : unsigned int i;
1026 17942954 : bitmap_iterator bi;
1027 17942954 : vec<pre_expr> result;
1028 :
1029 : /* Pre-allocate enough space for the array. */
1030 17942954 : unsigned cnt = bitmap_count_bits (&set->expressions);
1031 17942954 : result.create (cnt);
1032 :
1033 : /* For expressions with the same value from EXPRESSIONS retain only
1034 : expressions that can be inserted in place of all others. */
1035 17942954 : auto_bitmap exclusions;
1036 17942954 : bitmap_tree_view (exclusions);
1037 17942954 : if (for_insertion && cnt > 1)
1038 : {
1039 26067742 : EXECUTE_IF_SET_IN_BITMAP (&set->expressions, 0, i, bi)
1040 23508744 : result.safe_push (expression_for_id (i));
1041 2558998 : result.sort (expr_cmp, NULL);
1042 47011046 : for (unsigned i = 0; i < result.length () - 1; ++i)
1043 20946525 : if (result[i]->value_id == result[i+1]->value_id)
1044 : {
1045 241312 : if (pre_expr p = prefer (result[i], result[i+1]))
1046 : {
1047 : /* We retain and iterate on exprs[i+1], if we want to
1048 : retain exprs[i], swap both. */
1049 237478 : if (p == result[i])
1050 41365 : std::swap (result[i], result[i+1]);
1051 237478 : bitmap_set_bit (exclusions, get_expression_id (result[i]));
1052 : }
1053 : else
1054 : {
1055 : /* If neither works for pairwise choosing a conservative
1056 : alternative, drop all REFERENCE expressions for this value.
1057 : REFERENCE are always toplevel, so no chain should be
1058 : interrupted by pruning them. */
1059 : unsigned j, k;
1060 : for (j = i;; --j)
1061 3844 : if (j == 0
1062 3844 : || result[j - 1]->value_id != result[i]->value_id)
1063 : break;
1064 : for (k = j;; ++k)
1065 : {
1066 7727 : if (result[k]->kind == REFERENCE)
1067 7727 : bitmap_set_bit (exclusions,
1068 7727 : get_expression_id (result[k]));
1069 7727 : if (k == result.length () - 1
1070 7727 : || result[k + 1]->value_id != result[i]->value_id)
1071 : break;
1072 : }
1073 : i = k;
1074 : }
1075 : }
1076 2558998 : result.truncate (0);
1077 : }
1078 :
1079 17942954 : auto_bitmap val_visited (&grand_bitmap_obstack);
1080 17942954 : bitmap_tree_view (val_visited);
1081 102104250 : FOR_EACH_VALUE_ID_IN_SET (set, i, bi)
1082 84161296 : if (bitmap_set_bit (val_visited, i))
1083 74632234 : pre_expr_DFS (i, set, exclusions, val_visited, result);
1084 :
1085 17942954 : return result;
1086 17942954 : }
1087 :
1088 : /* Subtract all expressions contained in ORIG from DEST. */
1089 :
1090 : static bitmap_set_t
1091 31812280 : bitmap_set_subtract_expressions (bitmap_set_t dest, bitmap_set_t orig,
1092 : bool copy_values = false)
1093 : {
1094 31812280 : bitmap_set_t result = bitmap_set_new ();
1095 31812280 : bitmap_iterator bi;
1096 31812280 : unsigned int i;
1097 :
1098 31812280 : bitmap_and_compl (&result->expressions, &dest->expressions,
1099 31812280 : &orig->expressions);
1100 :
1101 31812280 : if (copy_values)
1102 644636 : bitmap_copy (&result->values, &dest->values);
1103 : else
1104 108706982 : FOR_EACH_EXPR_ID_IN_SET (result, i, bi)
1105 : {
1106 77539338 : pre_expr expr = expression_for_id (i);
1107 77539338 : unsigned int value_id = get_expr_value_id (expr);
1108 77539338 : bitmap_set_bit (&result->values, value_id);
1109 : }
1110 :
1111 31812280 : return result;
1112 : }
1113 :
1114 : /* Subtract all values in bitmap set B from bitmap set A. */
1115 :
1116 : static void
1117 1217750 : bitmap_set_subtract_values (bitmap_set_t a, bitmap_set_t b)
1118 : {
1119 1217750 : unsigned int i;
1120 1217750 : bitmap_iterator bi;
1121 1217750 : unsigned to_remove = -1U;
1122 1217750 : bitmap_and_compl_into (&a->values, &b->values);
1123 11451950 : FOR_EACH_EXPR_ID_IN_SET (a, i, bi)
1124 : {
1125 10234200 : if (to_remove != -1U)
1126 : {
1127 1425977 : bitmap_clear_bit (&a->expressions, to_remove);
1128 1425977 : to_remove = -1U;
1129 : }
1130 10234200 : pre_expr expr = expression_for_id (i);
1131 10234200 : if (! bitmap_bit_p (&a->values, get_expr_value_id (expr)))
1132 1484517 : to_remove = i;
1133 : }
1134 1217750 : if (to_remove != -1U)
1135 58540 : bitmap_clear_bit (&a->expressions, to_remove);
1136 1217750 : }
1137 :
1138 :
1139 : /* Return true if bitmapped set SET contains the value VALUE_ID. */
1140 :
1141 : static bool
1142 194314270 : bitmap_set_contains_value (bitmap_set_t set, unsigned int value_id)
1143 : {
1144 0 : if (value_id_constant_p (value_id))
1145 : return true;
1146 :
1147 93792007 : return bitmap_bit_p (&set->values, value_id);
1148 : }
1149 :
1150 : /* Return true if two bitmap sets are equal. */
1151 :
1152 : static bool
1153 15297265 : bitmap_set_equal (bitmap_set_t a, bitmap_set_t b)
1154 : {
1155 0 : return bitmap_equal_p (&a->values, &b->values);
1156 : }
1157 :
1158 : /* Replace an instance of EXPR's VALUE with EXPR in SET if it exists,
1159 : and add it otherwise. Return true if any changes were made. */
1160 :
1161 : static bool
1162 32736644 : bitmap_value_replace_in_set (bitmap_set_t set, pre_expr expr)
1163 : {
1164 32736644 : unsigned int val = get_expr_value_id (expr);
1165 32736644 : if (value_id_constant_p (val))
1166 : return false;
1167 :
1168 32736644 : if (bitmap_set_contains_value (set, val))
1169 : {
1170 : /* The number of expressions having a given value is usually
1171 : significantly less than the total number of expressions in SET.
1172 : Thus, rather than check, for each expression in SET, whether it
1173 : has the value LOOKFOR, we walk the reverse mapping that tells us
1174 : what expressions have a given value, and see if any of those
1175 : expressions are in our set. For large testcases, this is about
1176 : 5-10x faster than walking the bitmap. If this is somehow a
1177 : significant lose for some cases, we can choose which set to walk
1178 : based on the set size. */
1179 13958521 : unsigned int i;
1180 13958521 : bitmap_iterator bi;
1181 13958521 : bitmap exprset = value_expressions[val];
1182 16190926 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1183 : {
1184 16190926 : if (bitmap_clear_bit (&set->expressions, i))
1185 : {
1186 13958521 : bitmap_set_bit (&set->expressions, get_expression_id (expr));
1187 13958521 : return i != get_expression_id (expr);
1188 : }
1189 : }
1190 0 : gcc_unreachable ();
1191 : }
1192 :
1193 18778123 : bitmap_insert_into_set (set, expr);
1194 18778123 : return true;
1195 : }
1196 :
1197 : /* Insert EXPR into SET if EXPR's value is not already present in
1198 : SET. */
1199 :
1200 : static void
1201 62019207 : bitmap_value_insert_into_set (bitmap_set_t set, pre_expr expr)
1202 : {
1203 62019207 : unsigned int val = get_expr_value_id (expr);
1204 :
1205 62019207 : gcc_checking_assert (expr->id == get_expression_id (expr));
1206 :
1207 : /* Constant values are always considered to be part of the set. */
1208 62019207 : if (value_id_constant_p (val))
1209 : return;
1210 :
1211 : /* If the value membership changed, add the expression. */
1212 61963444 : if (bitmap_set_bit (&set->values, val))
1213 47904156 : bitmap_set_bit (&set->expressions, expr->id);
1214 : }
1215 :
1216 : /* Print out EXPR to outfile. */
1217 :
1218 : static void
1219 4573 : print_pre_expr (FILE *outfile, const pre_expr expr)
1220 : {
1221 4573 : if (! expr)
1222 : {
1223 0 : fprintf (outfile, "NULL");
1224 0 : return;
1225 : }
1226 4573 : switch (expr->kind)
1227 : {
1228 0 : case CONSTANT:
1229 0 : print_generic_expr (outfile, PRE_EXPR_CONSTANT (expr));
1230 0 : break;
1231 3214 : case NAME:
1232 3214 : print_generic_expr (outfile, PRE_EXPR_NAME (expr));
1233 3214 : break;
1234 1072 : case NARY:
1235 1072 : {
1236 1072 : unsigned int i;
1237 1072 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1238 1072 : fprintf (outfile, "{%s,", get_tree_code_name (nary->opcode));
1239 4095 : for (i = 0; i < nary->length; i++)
1240 : {
1241 1951 : print_generic_expr (outfile, nary->op[i]);
1242 1951 : if (i != (unsigned) nary->length - 1)
1243 879 : fprintf (outfile, ",");
1244 : }
1245 1072 : fprintf (outfile, "}");
1246 : }
1247 1072 : break;
1248 :
1249 287 : case REFERENCE:
1250 287 : {
1251 287 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1252 287 : print_vn_reference_ops (outfile, ref->operands);
1253 287 : if (ref->vuse)
1254 : {
1255 275 : fprintf (outfile, "@");
1256 275 : print_generic_expr (outfile, ref->vuse);
1257 : }
1258 : }
1259 : break;
1260 : }
1261 : }
1262 : void debug_pre_expr (pre_expr);
1263 :
1264 : /* Like print_pre_expr but always prints to stderr. */
1265 : DEBUG_FUNCTION void
1266 0 : debug_pre_expr (pre_expr e)
1267 : {
1268 0 : print_pre_expr (stderr, e);
1269 0 : fprintf (stderr, "\n");
1270 0 : }
1271 :
1272 : /* Print out SET to OUTFILE. */
1273 :
1274 : static void
1275 913 : print_bitmap_set (FILE *outfile, bitmap_set_t set,
1276 : const char *setname, int blockindex)
1277 : {
1278 913 : fprintf (outfile, "%s[%d] := { ", setname, blockindex);
1279 913 : if (set)
1280 : {
1281 913 : bool first = true;
1282 913 : unsigned i;
1283 913 : bitmap_iterator bi;
1284 :
1285 5345 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1286 : {
1287 4432 : const pre_expr expr = expression_for_id (i);
1288 :
1289 4432 : if (!first)
1290 3806 : fprintf (outfile, ", ");
1291 4432 : first = false;
1292 4432 : print_pre_expr (outfile, expr);
1293 :
1294 4432 : fprintf (outfile, " (%04d)", get_expr_value_id (expr));
1295 : }
1296 : }
1297 913 : fprintf (outfile, " }\n");
1298 913 : }
1299 :
1300 : void debug_bitmap_set (bitmap_set_t);
1301 :
1302 : DEBUG_FUNCTION void
1303 0 : debug_bitmap_set (bitmap_set_t set)
1304 : {
1305 0 : print_bitmap_set (stderr, set, "debug", 0);
1306 0 : }
1307 :
1308 : void debug_bitmap_sets_for (basic_block);
1309 :
1310 : DEBUG_FUNCTION void
1311 0 : debug_bitmap_sets_for (basic_block bb)
1312 : {
1313 0 : print_bitmap_set (stderr, AVAIL_OUT (bb), "avail_out", bb->index);
1314 0 : print_bitmap_set (stderr, EXP_GEN (bb), "exp_gen", bb->index);
1315 0 : print_bitmap_set (stderr, PHI_GEN (bb), "phi_gen", bb->index);
1316 0 : print_bitmap_set (stderr, TMP_GEN (bb), "tmp_gen", bb->index);
1317 0 : print_bitmap_set (stderr, ANTIC_IN (bb), "antic_in", bb->index);
1318 0 : if (do_partial_partial)
1319 0 : print_bitmap_set (stderr, PA_IN (bb), "pa_in", bb->index);
1320 0 : print_bitmap_set (stderr, NEW_SETS (bb), "new_sets", bb->index);
1321 0 : }
1322 :
1323 : /* Print out the expressions that have VAL to OUTFILE. */
1324 :
1325 : static void
1326 0 : print_value_expressions (FILE *outfile, unsigned int val)
1327 : {
1328 0 : bitmap set = value_expressions[val];
1329 0 : if (set)
1330 : {
1331 0 : bitmap_set x;
1332 0 : char s[10];
1333 0 : sprintf (s, "%04d", val);
1334 0 : x.expressions = *set;
1335 0 : print_bitmap_set (outfile, &x, s, 0);
1336 : }
1337 0 : }
1338 :
1339 :
1340 : DEBUG_FUNCTION void
1341 0 : debug_value_expressions (unsigned int val)
1342 : {
1343 0 : print_value_expressions (stderr, val);
1344 0 : }
1345 :
1346 : /* Given a CONSTANT, allocate a new CONSTANT type PRE_EXPR to
1347 : represent it. */
1348 :
1349 : static pre_expr
1350 4969168 : get_or_alloc_expr_for_constant (tree constant)
1351 : {
1352 4969168 : unsigned int result_id;
1353 4969168 : struct pre_expr_d expr;
1354 4969168 : pre_expr newexpr;
1355 :
1356 4969168 : expr.kind = CONSTANT;
1357 4969168 : PRE_EXPR_CONSTANT (&expr) = constant;
1358 4969168 : result_id = lookup_expression_id (&expr);
1359 4969168 : if (result_id != 0)
1360 4135309 : return expression_for_id (result_id);
1361 :
1362 833859 : newexpr = pre_expr_pool.allocate ();
1363 833859 : newexpr->kind = CONSTANT;
1364 833859 : newexpr->loc = UNKNOWN_LOCATION;
1365 833859 : PRE_EXPR_CONSTANT (newexpr) = constant;
1366 833859 : alloc_expression_id (newexpr);
1367 833859 : newexpr->value_id = get_or_alloc_constant_value_id (constant);
1368 833859 : add_to_value (newexpr->value_id, newexpr);
1369 833859 : return newexpr;
1370 : }
1371 :
1372 : /* Translate the VUSE backwards through phi nodes in E->dest, so that
1373 : it has the value it would have in E->src. Set *SAME_VALID to true
1374 : in case the new vuse doesn't change the value id of the OPERANDS. */
1375 :
1376 : static tree
1377 4439591 : translate_vuse_through_block (vec<vn_reference_op_s> operands,
1378 : alias_set_type set, alias_set_type base_set,
1379 : tree type, tree vuse, edge e, bool *same_valid)
1380 : {
1381 4439591 : basic_block phiblock = e->dest;
1382 4439591 : gimple *def = SSA_NAME_DEF_STMT (vuse);
1383 4439591 : ao_ref ref;
1384 :
1385 4439591 : if (same_valid)
1386 3216536 : *same_valid = true;
1387 :
1388 : /* If value-numbering provided a memory state for this
1389 : that dominates PHIBLOCK we can just use that. */
1390 4439591 : if (gimple_nop_p (def)
1391 4439591 : || (gimple_bb (def) != phiblock
1392 1153859 : && dominated_by_p (CDI_DOMINATORS, phiblock, gimple_bb (def))))
1393 1840891 : return vuse;
1394 :
1395 : /* We have pruned expressions that are killed in PHIBLOCK via
1396 : prune_clobbered_mems but we have not rewritten the VUSE to the one
1397 : live at the start of the block. If there is no virtual PHI to translate
1398 : through return the VUSE live at entry. Otherwise the VUSE to translate
1399 : is the def of the virtual PHI node. */
1400 2598700 : gphi *phi = get_virtual_phi (phiblock);
1401 2598700 : if (!phi)
1402 90257 : return BB_LIVE_VOP_ON_EXIT
1403 : (get_immediate_dominator (CDI_DOMINATORS, phiblock));
1404 :
1405 2508443 : if (same_valid
1406 2508443 : && ao_ref_init_from_vn_reference (&ref, set, base_set, type, operands))
1407 : {
1408 1838271 : bitmap visited = NULL;
1409 : /* Try to find a vuse that dominates this phi node by skipping
1410 : non-clobbering statements. */
1411 1838271 : unsigned int cnt = param_sccvn_max_alias_queries_per_access;
1412 1838271 : vuse = get_continuation_for_phi (phi, &ref, true,
1413 : cnt, &visited, false, NULL, NULL);
1414 1838271 : if (visited)
1415 1826449 : BITMAP_FREE (visited);
1416 : }
1417 : else
1418 : vuse = NULL_TREE;
1419 : /* If we didn't find any, the value ID can't stay the same. */
1420 2508443 : if (!vuse && same_valid)
1421 1586187 : *same_valid = false;
1422 :
1423 : /* ??? We would like to return vuse here as this is the canonical
1424 : upmost vdef that this reference is associated with. But during
1425 : insertion of the references into the hash tables we only ever
1426 : directly insert with their direct gimple_vuse, hence returning
1427 : something else would make us not find the other expression. */
1428 2508443 : return PHI_ARG_DEF (phi, e->dest_idx);
1429 : }
1430 :
1431 : /* Like bitmap_find_leader, but checks for the value existing in SET1 *or*
1432 : SET2 *or* SET3. This is used to avoid making a set consisting of the union
1433 : of PA_IN and ANTIC_IN during insert and phi-translation. */
1434 :
1435 : static inline pre_expr
1436 23842333 : find_leader_in_sets (unsigned int val, bitmap_set_t set1, bitmap_set_t set2,
1437 : bitmap_set_t set3 = NULL)
1438 : {
1439 23842333 : pre_expr result = NULL;
1440 :
1441 23842333 : if (set1)
1442 23716791 : result = bitmap_find_leader (set1, val);
1443 23842333 : if (!result && set2)
1444 1517424 : result = bitmap_find_leader (set2, val);
1445 23842333 : if (!result && set3)
1446 0 : result = bitmap_find_leader (set3, val);
1447 23842333 : return result;
1448 : }
1449 :
1450 : /* Get the tree type for our PRE expression e. */
1451 :
1452 : static tree
1453 7288242 : get_expr_type (const pre_expr e)
1454 : {
1455 7288242 : switch (e->kind)
1456 : {
1457 984663 : case NAME:
1458 984663 : return TREE_TYPE (PRE_EXPR_NAME (e));
1459 181774 : case CONSTANT:
1460 181774 : return TREE_TYPE (PRE_EXPR_CONSTANT (e));
1461 1342496 : case REFERENCE:
1462 1342496 : return PRE_EXPR_REFERENCE (e)->type;
1463 4779309 : case NARY:
1464 4779309 : return PRE_EXPR_NARY (e)->type;
1465 : }
1466 0 : gcc_unreachable ();
1467 : }
1468 :
1469 : /* Get a representative SSA_NAME for a given expression that is available in B.
1470 : Since all of our sub-expressions are treated as values, we require
1471 : them to be SSA_NAME's for simplicity.
1472 : Prior versions of GVNPRE used to use "value handles" here, so that
1473 : an expression would be VH.11 + VH.10 instead of d_3 + e_6. In
1474 : either case, the operands are really values (IE we do not expect
1475 : them to be usable without finding leaders). */
1476 :
1477 : static tree
1478 19161194 : get_representative_for (const pre_expr e, basic_block b = NULL)
1479 : {
1480 19161194 : tree name, valnum = NULL_TREE;
1481 19161194 : unsigned int value_id = get_expr_value_id (e);
1482 :
1483 19161194 : switch (e->kind)
1484 : {
1485 8770306 : case NAME:
1486 8770306 : return PRE_EXPR_NAME (e);
1487 1880485 : case CONSTANT:
1488 1880485 : return PRE_EXPR_CONSTANT (e);
1489 8510403 : case NARY:
1490 8510403 : case REFERENCE:
1491 8510403 : {
1492 : /* Go through all of the expressions representing this value
1493 : and pick out an SSA_NAME. */
1494 8510403 : unsigned int i;
1495 8510403 : bitmap_iterator bi;
1496 8510403 : bitmap exprs = value_expressions[value_id];
1497 21935984 : EXECUTE_IF_SET_IN_BITMAP (exprs, 0, i, bi)
1498 : {
1499 17955508 : pre_expr rep = expression_for_id (i);
1500 17955508 : if (rep->kind == NAME)
1501 : {
1502 8168933 : tree name = PRE_EXPR_NAME (rep);
1503 8168933 : valnum = VN_INFO (name)->valnum;
1504 8168933 : gimple *def = SSA_NAME_DEF_STMT (name);
1505 : /* We have to return either a new representative or one
1506 : that can be used for expression simplification and thus
1507 : is available in B. */
1508 8168933 : if (! b
1509 7859518 : || gimple_nop_p (def)
1510 12078386 : || dominated_by_p (CDI_DOMINATORS, b, gimple_bb (def)))
1511 4529927 : return name;
1512 : }
1513 9786575 : else if (rep->kind == CONSTANT)
1514 0 : return PRE_EXPR_CONSTANT (rep);
1515 : }
1516 : }
1517 3980476 : break;
1518 : }
1519 :
1520 : /* If we reached here we couldn't find an SSA_NAME. This can
1521 : happen when we've discovered a value that has never appeared in
1522 : the program as set to an SSA_NAME, as the result of phi translation.
1523 : Create one here.
1524 : ??? We should be able to re-use this when we insert the statement
1525 : to compute it. */
1526 3980476 : name = make_temp_ssa_name (get_expr_type (e), gimple_build_nop (), "pretmp");
1527 3980476 : vn_ssa_aux_t vn_info = VN_INFO (name);
1528 3980476 : vn_info->value_id = value_id;
1529 3980476 : vn_info->valnum = valnum ? valnum : name;
1530 3980476 : vn_info->visited = true;
1531 : /* ??? For now mark this SSA name for release by VN. */
1532 3980476 : vn_info->needs_insertion = true;
1533 3980476 : add_to_value (value_id, get_or_alloc_expr_for_name (name));
1534 3980476 : if (dump_file && (dump_flags & TDF_DETAILS))
1535 : {
1536 47 : fprintf (dump_file, "Created SSA_NAME representative ");
1537 47 : print_generic_expr (dump_file, name);
1538 47 : fprintf (dump_file, " for expression:");
1539 47 : print_pre_expr (dump_file, e);
1540 47 : fprintf (dump_file, " (%04d)\n", value_id);
1541 : }
1542 :
1543 : return name;
1544 : }
1545 :
1546 :
1547 : static pre_expr
1548 : phi_translate (bitmap_set_t, pre_expr, bitmap_set_t, bitmap_set_t, edge);
1549 :
1550 : /* Translate EXPR using phis in PHIBLOCK, so that it has the values of
1551 : the phis in PRED. Return NULL if we can't find a leader for each part
1552 : of the translated expression. */
1553 :
1554 : static pre_expr
1555 47927347 : phi_translate_1 (bitmap_set_t dest,
1556 : pre_expr expr, bitmap_set_t set1, bitmap_set_t set2, edge e)
1557 : {
1558 47927347 : basic_block pred = e->src;
1559 47927347 : basic_block phiblock = e->dest;
1560 47927347 : location_t expr_loc = expr->loc;
1561 47927347 : switch (expr->kind)
1562 : {
1563 18096752 : case NARY:
1564 18096752 : {
1565 18096752 : unsigned int i;
1566 18096752 : bool changed = false;
1567 18096752 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
1568 18096752 : vn_nary_op_t newnary = XALLOCAVAR (struct vn_nary_op_s,
1569 : sizeof_vn_nary_op (nary->length));
1570 18096752 : memcpy (newnary, nary, sizeof_vn_nary_op (nary->length));
1571 :
1572 43059497 : for (i = 0; i < newnary->length; i++)
1573 : {
1574 28227112 : if (TREE_CODE (newnary->op[i]) != SSA_NAME)
1575 8855802 : continue;
1576 : else
1577 : {
1578 19371310 : pre_expr leader, result;
1579 19371310 : unsigned int op_val_id = VN_INFO (newnary->op[i])->value_id;
1580 19371310 : leader = find_leader_in_sets (op_val_id, set1, set2);
1581 19371310 : result = phi_translate (dest, leader, set1, set2, e);
1582 19371310 : if (result)
1583 : /* If op has a leader in the sets we translate make
1584 : sure to use the value of the translated expression.
1585 : We might need a new representative for that. */
1586 16106943 : newnary->op[i] = get_representative_for (result, pred);
1587 : else if (!result)
1588 : return NULL;
1589 :
1590 16106943 : changed |= newnary->op[i] != nary->op[i];
1591 : }
1592 : }
1593 14832385 : if (changed)
1594 : {
1595 7420132 : unsigned int new_val_id;
1596 :
1597 : /* Try to simplify the new NARY. */
1598 7420132 : tree res = vn_nary_simplify (newnary);
1599 7420132 : if (res)
1600 : {
1601 2377949 : if (is_gimple_min_invariant (res))
1602 1232070 : return get_or_alloc_expr_for_constant (res);
1603 :
1604 : /* For non-CONSTANTs we have to make sure we can eventually
1605 : insert the expression. Which means we need to have a
1606 : leader for it. */
1607 1145879 : gcc_assert (TREE_CODE (res) == SSA_NAME);
1608 :
1609 : /* Do not allow simplifications to non-constants over
1610 : backedges as this will likely result in a loop PHI node
1611 : to be inserted and increased register pressure.
1612 : See PR77498 - this avoids doing predcoms work in
1613 : a less efficient way. */
1614 1145879 : if (e->flags & EDGE_DFS_BACK)
1615 : ;
1616 : else
1617 : {
1618 1063404 : unsigned value_id = VN_INFO (res)->value_id;
1619 : /* We want a leader in ANTIC_OUT or AVAIL_OUT here.
1620 : dest has what we computed into ANTIC_OUT sofar
1621 : so pick from that - since topological sorting
1622 : by sorted_array_from_bitmap_set isn't perfect
1623 : we may lose some cases here. */
1624 2126808 : pre_expr constant = find_leader_in_sets (value_id, dest,
1625 1063404 : AVAIL_OUT (pred));
1626 1063404 : if (constant)
1627 : {
1628 328272 : if (dump_file && (dump_flags & TDF_DETAILS))
1629 : {
1630 7 : fprintf (dump_file, "simplifying ");
1631 7 : print_pre_expr (dump_file, expr);
1632 7 : fprintf (dump_file, " translated %d -> %d to ",
1633 : phiblock->index, pred->index);
1634 7 : PRE_EXPR_NARY (expr) = newnary;
1635 7 : print_pre_expr (dump_file, expr);
1636 7 : PRE_EXPR_NARY (expr) = nary;
1637 7 : fprintf (dump_file, " to ");
1638 7 : print_pre_expr (dump_file, constant);
1639 7 : fprintf (dump_file, "\n");
1640 : }
1641 328272 : return constant;
1642 : }
1643 : }
1644 : }
1645 :
1646 11719580 : tree result = vn_nary_op_lookup_pieces (newnary->length,
1647 5859790 : newnary->opcode,
1648 : newnary->type,
1649 : &newnary->op[0],
1650 : &nary);
1651 5859790 : if (result && is_gimple_min_invariant (result))
1652 0 : return get_or_alloc_expr_for_constant (result);
1653 :
1654 5859790 : if (!nary || nary->predicated_values)
1655 : new_val_id = 0;
1656 : else
1657 787117 : new_val_id = nary->value_id;
1658 5859790 : expr = get_or_alloc_expr_for_nary (newnary, new_val_id, expr_loc);
1659 5859790 : add_to_value (get_expr_value_id (expr), expr);
1660 : }
1661 : return expr;
1662 : }
1663 4897580 : break;
1664 :
1665 4897580 : case REFERENCE:
1666 4897580 : {
1667 4897580 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
1668 4897580 : vec<vn_reference_op_s> operands = ref->operands;
1669 4897580 : tree vuse = ref->vuse;
1670 4897580 : tree newvuse = vuse;
1671 4897580 : vec<vn_reference_op_s> newoperands = vNULL;
1672 4897580 : bool changed = false, same_valid = true;
1673 4897580 : unsigned int i, n;
1674 4897580 : vn_reference_op_t operand;
1675 4897580 : vn_reference_t newref;
1676 :
1677 18696249 : for (i = 0; operands.iterate (i, &operand); i++)
1678 : {
1679 14152037 : pre_expr opresult;
1680 14152037 : pre_expr leader;
1681 14152037 : tree op[3];
1682 14152037 : tree type = operand->type;
1683 14152037 : vn_reference_op_s newop = *operand;
1684 14152037 : op[0] = operand->op0;
1685 14152037 : op[1] = operand->op1;
1686 14152037 : op[2] = operand->op2;
1687 55548100 : for (n = 0; n < 3; ++n)
1688 : {
1689 41749431 : unsigned int op_val_id;
1690 41749431 : if (!op[n])
1691 25298195 : continue;
1692 16451236 : if (TREE_CODE (op[n]) != SSA_NAME)
1693 : {
1694 : /* We can't possibly insert these. */
1695 13043617 : if (n != 0
1696 13043617 : && !is_gimple_min_invariant (op[n]))
1697 : break;
1698 13043617 : continue;
1699 : }
1700 3407619 : op_val_id = VN_INFO (op[n])->value_id;
1701 3407619 : leader = find_leader_in_sets (op_val_id, set1, set2);
1702 3407619 : opresult = phi_translate (dest, leader, set1, set2, e);
1703 3407619 : if (opresult)
1704 : {
1705 3054251 : tree name = get_representative_for (opresult);
1706 3054251 : changed |= name != op[n];
1707 3054251 : op[n] = name;
1708 : }
1709 : else if (!opresult)
1710 : break;
1711 : }
1712 14152037 : if (n != 3)
1713 : {
1714 353368 : newoperands.release ();
1715 353368 : return NULL;
1716 : }
1717 : /* When we translate a MEM_REF across a backedge and we have
1718 : restrict info that's not from our functions parameters
1719 : we have to remap it since we now may deal with a different
1720 : instance where the dependence info is no longer valid.
1721 : See PR102970. Note instead of keeping a remapping table
1722 : per backedge we simply throw away restrict info. */
1723 13798669 : if ((newop.opcode == MEM_REF
1724 13798669 : || newop.opcode == TARGET_MEM_REF)
1725 4657943 : && newop.clique > 1
1726 154318 : && (e->flags & EDGE_DFS_BACK))
1727 : {
1728 : newop.clique = 0;
1729 : newop.base = 0;
1730 : changed = true;
1731 : }
1732 13780259 : if (!changed)
1733 11361247 : continue;
1734 2437422 : if (!newoperands.exists ())
1735 1253578 : newoperands = operands.copy ();
1736 : /* We may have changed from an SSA_NAME to a constant */
1737 2437422 : if (newop.opcode == SSA_NAME && TREE_CODE (op[0]) != SSA_NAME)
1738 : newop.opcode = TREE_CODE (op[0]);
1739 2437422 : newop.type = type;
1740 2437422 : newop.op0 = op[0];
1741 2437422 : newop.op1 = op[1];
1742 2437422 : newop.op2 = op[2];
1743 2437422 : newoperands[i] = newop;
1744 : }
1745 9088424 : gcc_checking_assert (i == operands.length ());
1746 :
1747 4544212 : if (vuse)
1748 : {
1749 10872663 : newvuse = translate_vuse_through_block (newoperands.exists ()
1750 4439591 : ? newoperands : operands,
1751 : ref->set, ref->base_set,
1752 : ref->type, vuse, e,
1753 : changed
1754 : ? NULL : &same_valid);
1755 4439591 : if (newvuse == NULL_TREE)
1756 : {
1757 0 : newoperands.release ();
1758 0 : return NULL;
1759 : }
1760 : }
1761 :
1762 4544212 : if (changed || newvuse != vuse)
1763 : {
1764 3193476 : unsigned int new_val_id;
1765 :
1766 5134594 : tree result = vn_reference_lookup_pieces (newvuse, ref->set,
1767 : ref->base_set,
1768 : ref->type,
1769 3193476 : newoperands.exists ()
1770 3193476 : ? newoperands : operands,
1771 : &newref, VN_WALK);
1772 :
1773 : /* We can always insert constants, so if we have a partial
1774 : redundant constant load of another type try to translate it
1775 : to a constant of appropriate type. */
1776 3193476 : if (result && is_gimple_min_invariant (result))
1777 : {
1778 73737 : tree tem = result;
1779 73737 : if (!useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1780 : {
1781 83 : tem = fold_unary (VIEW_CONVERT_EXPR, ref->type, result);
1782 83 : if (tem && !is_gimple_min_invariant (tem))
1783 : tem = NULL_TREE;
1784 : }
1785 73737 : if (tem)
1786 : {
1787 73737 : newoperands.release ();
1788 73737 : return get_or_alloc_expr_for_constant (tem);
1789 : }
1790 : }
1791 :
1792 : /* If we'd have to convert things we would need to validate
1793 : if we can insert the translated expression. So fail
1794 : here for now - we cannot insert an alias with a different
1795 : type in the VN tables either, as that would assert. */
1796 3119739 : if (result
1797 3119739 : && !useless_type_conversion_p (ref->type, TREE_TYPE (result)))
1798 : {
1799 1000 : newoperands.release ();
1800 1000 : return NULL;
1801 : }
1802 2565170 : else if (!result && newref
1803 3118739 : && !useless_type_conversion_p (ref->type, newref->type))
1804 : {
1805 0 : newoperands.release ();
1806 0 : return NULL;
1807 : }
1808 :
1809 3118739 : if (newref)
1810 : {
1811 553569 : new_val_id = newref->value_id;
1812 553569 : newvuse = newref->vuse;
1813 : }
1814 : else
1815 : {
1816 2565170 : if (changed || !same_valid)
1817 : new_val_id = 0;
1818 : else
1819 129415 : new_val_id = ref->value_id;
1820 : }
1821 3118739 : newref = XALLOCAVAR (struct vn_reference_s,
1822 : sizeof (vn_reference_s));
1823 3118739 : memcpy (newref, ref, sizeof (vn_reference_s));
1824 3118739 : newref->next = NULL;
1825 3118739 : newref->value_id = new_val_id;
1826 3118739 : newref->vuse = newvuse;
1827 6237478 : newref->operands
1828 3118739 : = newoperands.exists () ? newoperands : operands.copy ();
1829 3118739 : newoperands = vNULL;
1830 3118739 : newref->type = ref->type;
1831 3118739 : newref->result = result;
1832 3118739 : newref->hashcode = vn_reference_compute_hash (newref);
1833 3118739 : expr = get_or_alloc_expr_for_reference (newref, new_val_id,
1834 : expr_loc, true);
1835 3118739 : add_to_value (get_expr_value_id (expr), expr);
1836 : }
1837 4469475 : newoperands.release ();
1838 4469475 : return expr;
1839 : }
1840 24933015 : break;
1841 :
1842 24933015 : case NAME:
1843 24933015 : {
1844 24933015 : tree name = PRE_EXPR_NAME (expr);
1845 24933015 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
1846 : /* If the SSA name is defined by a PHI node in this block,
1847 : translate it. */
1848 24933015 : if (gimple_code (def_stmt) == GIMPLE_PHI
1849 24933015 : && gimple_bb (def_stmt) == phiblock)
1850 : {
1851 7743726 : tree def = PHI_ARG_DEF (def_stmt, e->dest_idx);
1852 :
1853 : /* Handle constant. */
1854 7743726 : if (is_gimple_min_invariant (def))
1855 2291726 : return get_or_alloc_expr_for_constant (def);
1856 :
1857 5452000 : return get_or_alloc_expr_for_name (def);
1858 : }
1859 : /* Otherwise return it unchanged - it will get removed if its
1860 : value is not available in PREDs AVAIL_OUT set of expressions
1861 : by the subtraction of TMP_GEN. */
1862 : return expr;
1863 : }
1864 :
1865 0 : default:
1866 0 : gcc_unreachable ();
1867 : }
1868 : }
1869 :
1870 : /* Wrapper around phi_translate_1 providing caching functionality. */
1871 :
1872 : static pre_expr
1873 88308807 : phi_translate (bitmap_set_t dest, pre_expr expr,
1874 : bitmap_set_t set1, bitmap_set_t set2, edge e)
1875 : {
1876 88308807 : expr_pred_trans_t slot = NULL;
1877 88308807 : pre_expr phitrans;
1878 :
1879 88308807 : if (!expr)
1880 : return NULL;
1881 :
1882 : /* Constants contain no values that need translation. */
1883 86504887 : if (expr->kind == CONSTANT)
1884 : return expr;
1885 :
1886 86504667 : if (value_id_constant_p (get_expr_value_id (expr)))
1887 : return expr;
1888 :
1889 : /* Don't add translations of NAMEs as those are cheap to translate. */
1890 86504667 : if (expr->kind != NAME)
1891 : {
1892 61571652 : if (phi_trans_add (&slot, expr, e->src))
1893 38577320 : return slot->v == 0 ? NULL : expression_for_id (slot->v);
1894 : /* Store NULL for the value we want to return in the case of
1895 : recursing. */
1896 22994332 : slot->v = 0;
1897 : }
1898 :
1899 : /* Translate. */
1900 47927347 : basic_block saved_valueize_bb = vn_context_bb;
1901 47927347 : vn_context_bb = e->src;
1902 47927347 : phitrans = phi_translate_1 (dest, expr, set1, set2, e);
1903 47927347 : vn_context_bb = saved_valueize_bb;
1904 :
1905 47927347 : if (slot)
1906 : {
1907 : /* We may have reallocated. */
1908 22994332 : phi_trans_add (&slot, expr, e->src);
1909 22994332 : if (phitrans)
1910 19375597 : slot->v = get_expression_id (phitrans);
1911 : else
1912 : /* Remove failed translations again, they cause insert
1913 : iteration to not pick up new opportunities reliably. */
1914 3618735 : PHI_TRANS_TABLE (e->src)->clear_slot (slot);
1915 : }
1916 :
1917 : return phitrans;
1918 : }
1919 :
1920 :
1921 : /* For each expression in SET, translate the values through phi nodes
1922 : in PHIBLOCK using edge PHIBLOCK->PRED, and store the resulting
1923 : expressions in DEST. */
1924 :
1925 : static void
1926 20062014 : phi_translate_set (bitmap_set_t dest, bitmap_set_t set, edge e)
1927 : {
1928 20062014 : bitmap_iterator bi;
1929 20062014 : unsigned int i;
1930 :
1931 20062014 : if (gimple_seq_empty_p (phi_nodes (e->dest)))
1932 : {
1933 13411038 : bitmap_set_copy (dest, set);
1934 13411038 : return;
1935 : }
1936 :
1937 : /* Allocate the phi-translation cache where we have an idea about
1938 : its size. hash-table implementation internals tell us that
1939 : allocating the table to fit twice the number of elements will
1940 : make sure we do not usually re-allocate. */
1941 6650976 : if (!PHI_TRANS_TABLE (e->src))
1942 5798491 : PHI_TRANS_TABLE (e->src) = new hash_table<expr_pred_trans_d>
1943 5798491 : (2 * bitmap_count_bits (&set->expressions));
1944 44523473 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
1945 : {
1946 37872497 : pre_expr expr = expression_for_id (i);
1947 37872497 : pre_expr translated = phi_translate (dest, expr, set, NULL, e);
1948 37872497 : if (!translated)
1949 1804093 : continue;
1950 :
1951 36068404 : bitmap_insert_into_set (dest, translated);
1952 : }
1953 : }
1954 :
1955 : /* Find the leader for a value (i.e., the name representing that
1956 : value) in a given set, and return it. Return NULL if no leader
1957 : is found. */
1958 :
1959 : static pre_expr
1960 55821205 : bitmap_find_leader (bitmap_set_t set, unsigned int val)
1961 : {
1962 55821205 : if (value_id_constant_p (val))
1963 1740032 : return constant_value_expressions[-val];
1964 :
1965 54081173 : if (bitmap_set_contains_value (set, val))
1966 : {
1967 : /* Rather than walk the entire bitmap of expressions, and see
1968 : whether any of them has the value we are looking for, we look
1969 : at the reverse mapping, which tells us the set of expressions
1970 : that have a given value (IE value->expressions with that
1971 : value) and see if any of those expressions are in our set.
1972 : The number of expressions per value is usually significantly
1973 : less than the number of expressions in the set. In fact, for
1974 : large testcases, doing it this way is roughly 5-10x faster
1975 : than walking the bitmap.
1976 : If this is somehow a significant lose for some cases, we can
1977 : choose which set to walk based on which set is smaller. */
1978 25321385 : unsigned int i;
1979 25321385 : bitmap_iterator bi;
1980 25321385 : bitmap exprset = value_expressions[val];
1981 :
1982 25321385 : if (!exprset->first->next)
1983 32158954 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
1984 29766909 : if (bitmap_bit_p (&set->expressions, i))
1985 22841972 : return expression_for_id (i);
1986 :
1987 6335879 : EXECUTE_IF_AND_IN_BITMAP (exprset, &set->expressions, 0, i, bi)
1988 3856466 : return expression_for_id (i);
1989 : }
1990 : return NULL;
1991 : }
1992 :
1993 : /* Determine if EXPR, a memory expression, is ANTIC_IN at the top of
1994 : BLOCK by seeing if it is not killed in the block. Note that we are
1995 : only determining whether there is a store that kills it. Because
1996 : of the order in which clean iterates over values, we are guaranteed
1997 : that altered operands will have caused us to be eliminated from the
1998 : ANTIC_IN set already. */
1999 :
2000 : static bool
2001 1621795 : value_dies_in_block_x (pre_expr expr, basic_block block)
2002 : {
2003 1621795 : tree vuse = PRE_EXPR_REFERENCE (expr)->vuse;
2004 1621795 : vn_reference_t refx = PRE_EXPR_REFERENCE (expr);
2005 1621795 : gimple *def;
2006 1621795 : gimple_stmt_iterator gsi;
2007 1621795 : unsigned id = get_expression_id (expr);
2008 1621795 : bool res = false;
2009 1621795 : ao_ref ref;
2010 :
2011 1621795 : if (!vuse)
2012 : return false;
2013 :
2014 : /* Lookup a previously calculated result. */
2015 1621795 : if (EXPR_DIES (block)
2016 1621795 : && bitmap_bit_p (EXPR_DIES (block), id * 2))
2017 133190 : return bitmap_bit_p (EXPR_DIES (block), id * 2 + 1);
2018 :
2019 : /* A memory expression {e, VUSE} dies in the block if there is a
2020 : statement that may clobber e. If, starting statement walk from the
2021 : top of the basic block, a statement uses VUSE there can be no kill
2022 : in between that use and the original statement that loaded {e, VUSE},
2023 : so we can stop walking. */
2024 1488605 : ref.base = NULL_TREE;
2025 13188851 : for (gsi = gsi_start_bb (block); !gsi_end_p (gsi); gsi_next (&gsi))
2026 : {
2027 11229582 : tree def_vuse, def_vdef;
2028 11229582 : def = gsi_stmt (gsi);
2029 11229582 : def_vuse = gimple_vuse (def);
2030 11229582 : def_vdef = gimple_vdef (def);
2031 :
2032 : /* Not a memory statement. */
2033 11229582 : if (!def_vuse)
2034 7961411 : continue;
2035 :
2036 : /* Not a may-def. */
2037 3268171 : if (!def_vdef)
2038 : {
2039 : /* A load with the same VUSE, we're done. */
2040 942538 : if (def_vuse == vuse)
2041 : break;
2042 :
2043 655459 : continue;
2044 : }
2045 :
2046 : /* Init ref only if we really need it. */
2047 2325633 : if (ref.base == NULL_TREE
2048 3448309 : && !ao_ref_init_from_vn_reference (&ref, refx->set, refx->base_set,
2049 1122676 : refx->type, refx->operands))
2050 : {
2051 : res = true;
2052 : break;
2053 : }
2054 : /* If the statement may clobber expr, it dies. */
2055 2292128 : if (stmt_may_clobber_ref_p_1 (def, &ref))
2056 : {
2057 : res = true;
2058 : break;
2059 : }
2060 : }
2061 :
2062 : /* Remember the result. */
2063 1488605 : if (!EXPR_DIES (block))
2064 691394 : EXPR_DIES (block) = BITMAP_ALLOC (&grand_bitmap_obstack);
2065 1488605 : bitmap_set_bit (EXPR_DIES (block), id * 2);
2066 1488605 : if (res)
2067 730862 : bitmap_set_bit (EXPR_DIES (block), id * 2 + 1);
2068 :
2069 : return res;
2070 : }
2071 :
2072 :
2073 : /* Determine if OP is valid in SET1 U SET2, which it is when the union
2074 : contains its value-id. */
2075 :
2076 : static bool
2077 258527388 : op_valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, tree op)
2078 : {
2079 258527388 : if (op && TREE_CODE (op) == SSA_NAME)
2080 : {
2081 76883277 : unsigned int value_id = VN_INFO (op)->value_id;
2082 153763577 : if (!(bitmap_set_contains_value (set1, value_id)
2083 2233947 : || (set2 && bitmap_set_contains_value (set2, value_id))))
2084 2287463 : return false;
2085 : }
2086 : return true;
2087 : }
2088 :
2089 : /* Determine if the expression EXPR is valid in SET1 U SET2.
2090 : ONLY SET2 CAN BE NULL.
2091 : This means that we have a leader for each part of the expression
2092 : (if it consists of values), or the expression is an SSA_NAME.
2093 : For loads/calls, we also see if the vuse is killed in this block. */
2094 :
2095 : static bool
2096 121856111 : valid_in_sets (bitmap_set_t set1, bitmap_set_t set2, pre_expr expr)
2097 : {
2098 121856111 : switch (expr->kind)
2099 : {
2100 : case NAME:
2101 : /* By construction all NAMEs are available. Non-available
2102 : NAMEs are removed by subtracting TMP_GEN from the sets. */
2103 : return true;
2104 55907481 : case NARY:
2105 55907481 : {
2106 55907481 : unsigned int i;
2107 55907481 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2108 146027318 : for (i = 0; i < nary->length; i++)
2109 92247217 : if (!op_valid_in_sets (set1, set2, nary->op[i]))
2110 : return false;
2111 : return true;
2112 : }
2113 19040995 : break;
2114 19040995 : case REFERENCE:
2115 19040995 : {
2116 19040995 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2117 19040995 : vn_reference_op_t vro;
2118 19040995 : unsigned int i;
2119 :
2120 74414331 : FOR_EACH_VEC_ELT (ref->operands, i, vro)
2121 : {
2122 55533419 : if (!op_valid_in_sets (set1, set2, vro->op0)
2123 55373376 : || !op_valid_in_sets (set1, set2, vro->op1)
2124 110906795 : || !op_valid_in_sets (set1, set2, vro->op2))
2125 160083 : return false;
2126 : }
2127 : return true;
2128 : }
2129 0 : default:
2130 0 : gcc_unreachable ();
2131 : }
2132 : }
2133 :
2134 : /* Clean the set of expressions SET1 that are no longer valid in SET1 or SET2.
2135 : This means expressions that are made up of values we have no leaders for
2136 : in SET1 or SET2. */
2137 :
2138 : static void
2139 14144614 : clean (bitmap_set_t set1, bitmap_set_t set2 = NULL)
2140 : {
2141 14144614 : vec<pre_expr> exprs = sorted_array_from_bitmap_set (set1, false);
2142 14144614 : pre_expr expr;
2143 14144614 : int i;
2144 :
2145 76108413 : FOR_EACH_VEC_ELT (exprs, i, expr)
2146 : {
2147 61963799 : if (!valid_in_sets (set1, set2, expr))
2148 : {
2149 2287454 : unsigned int val = get_expr_value_id (expr);
2150 2287454 : bitmap_clear_bit (&set1->expressions, get_expression_id (expr));
2151 : /* We are entered with possibly multiple expressions for a value
2152 : so before removing a value from the set see if there's an
2153 : expression for it left. */
2154 2287454 : if (! bitmap_find_leader (set1, val))
2155 2277317 : bitmap_clear_bit (&set1->values, val);
2156 : }
2157 : }
2158 14144614 : exprs.release ();
2159 :
2160 14144614 : if (flag_checking)
2161 : {
2162 14144427 : unsigned j;
2163 14144427 : bitmap_iterator bi;
2164 73820573 : FOR_EACH_EXPR_ID_IN_SET (set1, j, bi)
2165 59676146 : gcc_assert (valid_in_sets (set1, set2, expression_for_id (j)));
2166 : }
2167 14144614 : }
2168 :
2169 : /* Clean the set of expressions that are no longer valid in SET because
2170 : they are clobbered in BLOCK or because they trap and may not be executed.
2171 : When CLEAN_TRAPS is true remove all possibly trapping expressions. */
2172 :
2173 : static void
2174 16515015 : prune_clobbered_mems (bitmap_set_t set, basic_block block, bool clean_traps)
2175 : {
2176 16515015 : bitmap_iterator bi;
2177 16515015 : unsigned i;
2178 16515015 : unsigned to_remove = -1U;
2179 16515015 : bool any_removed = false;
2180 :
2181 75735224 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2182 : {
2183 : /* Remove queued expr. */
2184 59220209 : if (to_remove != -1U)
2185 : {
2186 580992 : bitmap_clear_bit (&set->expressions, to_remove);
2187 580992 : any_removed = true;
2188 580992 : to_remove = -1U;
2189 : }
2190 :
2191 59220209 : pre_expr expr = expression_for_id (i);
2192 59220209 : if (expr->kind == REFERENCE)
2193 : {
2194 8009804 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
2195 8009804 : if (ref->vuse)
2196 : {
2197 7266115 : gimple *def_stmt = SSA_NAME_DEF_STMT (ref->vuse);
2198 7266115 : if (!gimple_nop_p (def_stmt)
2199 : /* If value-numbering provided a memory state for this
2200 : that dominates BLOCK we're done, otherwise we have
2201 : to check if the value dies in BLOCK. */
2202 8914037 : && !(gimple_bb (def_stmt) != block
2203 3755270 : && dominated_by_p (CDI_DOMINATORS,
2204 3755270 : block, gimple_bb (def_stmt)))
2205 8887910 : && value_dies_in_block_x (expr, block))
2206 : to_remove = i;
2207 : }
2208 : /* If the REFERENCE may trap make sure the block does not contain
2209 : a possible exit point.
2210 : ??? This is overly conservative if we translate AVAIL_OUT
2211 : as the available expression might be after the exit point. */
2212 7353013 : if ((BB_MAY_NOTRETURN (block) || clean_traps)
2213 8256806 : && vn_reference_may_trap (ref))
2214 : to_remove = i;
2215 : }
2216 51210405 : else if (expr->kind == NARY)
2217 : {
2218 27360447 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
2219 : /* If the NARY may trap make sure the block does not contain
2220 : a possible exit point.
2221 : ??? This is overly conservative if we translate AVAIL_OUT
2222 : as the available expression might be after the exit point. */
2223 23242984 : if ((BB_MAY_NOTRETURN (block) || clean_traps)
2224 28224065 : && vn_nary_may_trap (nary))
2225 : to_remove = i;
2226 : }
2227 : }
2228 :
2229 : /* Remove queued expr. */
2230 16515015 : if (to_remove != -1U)
2231 : {
2232 410235 : bitmap_clear_bit (&set->expressions, to_remove);
2233 410235 : any_removed = true;
2234 : }
2235 :
2236 : /* Above we only removed expressions, now clean the set of values
2237 : which no longer have any corresponding expression. We cannot
2238 : clear the value at the time we remove an expression since there
2239 : may be multiple expressions per value.
2240 : If we'd queue possibly to be removed values we could use
2241 : the bitmap_find_leader way to see if there's still an expression
2242 : for it. For some ratio of to be removed values and number of
2243 : values/expressions in the set this might be faster than rebuilding
2244 : the value-set.
2245 : Note when there's a MAX solution on one edge (clean_traps) do not
2246 : prune values as we need to consider the resulting expression set MAX
2247 : as well. This avoids a later growing ANTIC_IN value-set during
2248 : iteration, when the explicitly represented expression set grows. */
2249 16515015 : if (any_removed && !clean_traps)
2250 : {
2251 472312 : bitmap_clear (&set->values);
2252 2670110 : FOR_EACH_EXPR_ID_IN_SET (set, i, bi)
2253 : {
2254 2197798 : pre_expr expr = expression_for_id (i);
2255 2197798 : unsigned int value_id = get_expr_value_id (expr);
2256 2197798 : bitmap_set_bit (&set->values, value_id);
2257 : }
2258 : }
2259 16515015 : }
2260 :
2261 : /* Compute the ANTIC set for BLOCK.
2262 :
2263 : If succs(BLOCK) > 1 then
2264 : ANTIC_OUT[BLOCK] = intersection of ANTIC_IN[b] for all succ(BLOCK)
2265 : else if succs(BLOCK) == 1 then
2266 : ANTIC_OUT[BLOCK] = phi_translate (ANTIC_IN[succ(BLOCK)])
2267 :
2268 : ANTIC_IN[BLOCK] = clean(ANTIC_OUT[BLOCK] U EXP_GEN[BLOCK] - TMP_GEN[BLOCK])
2269 :
2270 : Note that clean() is deferred until after the iteration. */
2271 :
2272 : static bool
2273 15301671 : compute_antic_aux (basic_block block, bool block_has_abnormal_pred_edge)
2274 : {
2275 15301671 : bitmap_set_t S, old, ANTIC_OUT;
2276 15301671 : edge e;
2277 15301671 : edge_iterator ei;
2278 :
2279 15301671 : bool was_visited = BB_VISITED (block);
2280 15301671 : bool changed = ! BB_VISITED (block);
2281 15301671 : bool any_max_on_edge = false;
2282 :
2283 15301671 : BB_VISITED (block) = 1;
2284 15301671 : old = ANTIC_OUT = S = NULL;
2285 :
2286 : /* If any edges from predecessors are abnormal, antic_in is empty,
2287 : so do nothing. */
2288 15301671 : if (block_has_abnormal_pred_edge)
2289 4406 : goto maybe_dump_sets;
2290 :
2291 15297265 : old = ANTIC_IN (block);
2292 15297265 : ANTIC_OUT = bitmap_set_new ();
2293 :
2294 : /* If the block has no successors, ANTIC_OUT is empty. */
2295 15297265 : if (EDGE_COUNT (block->succs) == 0)
2296 : ;
2297 : /* If we have one successor, we could have some phi nodes to
2298 : translate through. */
2299 15297265 : else if (single_succ_p (block))
2300 : {
2301 9771636 : e = single_succ_edge (block);
2302 9771636 : gcc_assert (BB_VISITED (e->dest));
2303 9771636 : phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
2304 : }
2305 : /* If we have multiple successors, we take the intersection of all of
2306 : them. Note that in the case of loop exit phi nodes, we may have
2307 : phis to translate through. */
2308 : else
2309 : {
2310 5525629 : size_t i;
2311 5525629 : edge first = NULL;
2312 :
2313 5525629 : auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2314 16679900 : FOR_EACH_EDGE (e, ei, block->succs)
2315 : {
2316 11154271 : if (!first
2317 5989217 : && BB_VISITED (e->dest))
2318 : first = e;
2319 5628642 : else if (BB_VISITED (e->dest))
2320 4981393 : worklist.quick_push (e);
2321 : else
2322 : {
2323 : /* Unvisited successors get their ANTIC_IN replaced by the
2324 : maximal set to arrive at a maximum ANTIC_IN solution.
2325 : We can ignore them in the intersection operation and thus
2326 : need not explicitly represent that maximum solution. */
2327 647249 : any_max_on_edge = true;
2328 647249 : if (dump_file && (dump_flags & TDF_DETAILS))
2329 18 : fprintf (dump_file, "ANTIC_IN is MAX on %d->%d\n",
2330 18 : e->src->index, e->dest->index);
2331 : }
2332 : }
2333 :
2334 : /* Of multiple successors we have to have visited one already
2335 : which is guaranteed by iteration order. */
2336 5525629 : gcc_assert (first != NULL);
2337 :
2338 5525629 : phi_translate_set (ANTIC_OUT, ANTIC_IN (first->dest), first);
2339 :
2340 : /* If we have multiple successors we need to intersect the ANTIC_OUT
2341 : sets. For values that's a simple intersection but for
2342 : expressions it is a union. Given we want to have a single
2343 : expression per value in our sets we have to canonicalize.
2344 : Avoid randomness and running into cycles like for PR82129 and
2345 : canonicalize the expression we choose to the one with the
2346 : lowest id. This requires we actually compute the union first. */
2347 10507022 : FOR_EACH_VEC_ELT (worklist, i, e)
2348 : {
2349 4981393 : if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2350 : {
2351 2370 : bitmap_set_t tmp = bitmap_set_new ();
2352 2370 : phi_translate_set (tmp, ANTIC_IN (e->dest), e);
2353 2370 : bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
2354 2370 : bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
2355 2370 : bitmap_set_free (tmp);
2356 : }
2357 : else
2358 : {
2359 4979023 : bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
2360 4979023 : bitmap_ior_into (&ANTIC_OUT->expressions,
2361 4979023 : &ANTIC_IN (e->dest)->expressions);
2362 : }
2363 : }
2364 11051258 : if (! worklist.is_empty ())
2365 : {
2366 : /* Prune expressions not in the value set. */
2367 4881994 : bitmap_iterator bi;
2368 4881994 : unsigned int i;
2369 4881994 : unsigned int to_clear = -1U;
2370 35617586 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_OUT, i, bi)
2371 : {
2372 30735592 : if (to_clear != -1U)
2373 : {
2374 16153044 : bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2375 16153044 : to_clear = -1U;
2376 : }
2377 30735592 : pre_expr expr = expression_for_id (i);
2378 30735592 : unsigned int value_id = get_expr_value_id (expr);
2379 30735592 : if (!bitmap_bit_p (&ANTIC_OUT->values, value_id))
2380 19773703 : to_clear = i;
2381 : }
2382 4881994 : if (to_clear != -1U)
2383 3620659 : bitmap_clear_bit (&ANTIC_OUT->expressions, to_clear);
2384 : }
2385 5525629 : }
2386 :
2387 : /* Dump ANTIC_OUT before it's pruned. */
2388 15297265 : if (dump_file && (dump_flags & TDF_DETAILS))
2389 151 : print_bitmap_set (dump_file, ANTIC_OUT, "ANTIC_OUT", block->index);
2390 :
2391 : /* Prune expressions that are clobbered in block and thus become
2392 : invalid if translated from ANTIC_OUT to ANTIC_IN. */
2393 15297265 : prune_clobbered_mems (ANTIC_OUT, block, any_max_on_edge);
2394 :
2395 : /* Generate ANTIC_OUT - TMP_GEN. Note when there's a MAX solution
2396 : on one edge do not prune values as we need to consider the resulting
2397 : expression set MAX as well. This avoids a later growing ANTIC_IN
2398 : value-set during iteration, when the explicitly represented
2399 : expression set grows. */
2400 15297265 : S = bitmap_set_subtract_expressions (ANTIC_OUT, TMP_GEN (block),
2401 : any_max_on_edge);
2402 :
2403 : /* Start ANTIC_IN with EXP_GEN - TMP_GEN. */
2404 30594530 : ANTIC_IN (block) = bitmap_set_subtract_expressions (EXP_GEN (block),
2405 15297265 : TMP_GEN (block));
2406 :
2407 : /* Then union in the ANTIC_OUT - TMP_GEN values,
2408 : to get ANTIC_OUT U EXP_GEN - TMP_GEN */
2409 15297265 : bitmap_ior_into (&ANTIC_IN (block)->values, &S->values);
2410 15297265 : bitmap_ior_into (&ANTIC_IN (block)->expressions, &S->expressions);
2411 :
2412 : /* clean (ANTIC_IN (block)) is deferred to after the iteration converged
2413 : because it can cause non-convergence, see for example PR81181. */
2414 :
2415 15297265 : if (was_visited
2416 15297265 : && bitmap_and_into (&ANTIC_IN (block)->values, &old->values))
2417 : {
2418 1939 : if (dump_file && (dump_flags & TDF_DETAILS))
2419 0 : fprintf (dump_file, "warning: intersecting with old ANTIC_IN "
2420 : "shrinks the set\n");
2421 : /* Prune expressions not in the value set. */
2422 1939 : bitmap_iterator bi;
2423 1939 : unsigned int i;
2424 1939 : unsigned int to_clear = -1U;
2425 21258 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (block), i, bi)
2426 : {
2427 19319 : if (to_clear != -1U)
2428 : {
2429 1567 : bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2430 1567 : to_clear = -1U;
2431 : }
2432 19319 : pre_expr expr = expression_for_id (i);
2433 19319 : unsigned int value_id = get_expr_value_id (expr);
2434 19319 : if (!bitmap_bit_p (&ANTIC_IN (block)->values, value_id))
2435 2911 : to_clear = i;
2436 : }
2437 1939 : if (to_clear != -1U)
2438 1344 : bitmap_clear_bit (&ANTIC_IN (block)->expressions, to_clear);
2439 : }
2440 :
2441 15297265 : if (!bitmap_set_equal (old, ANTIC_IN (block)))
2442 9937312 : changed = true;
2443 :
2444 5359953 : maybe_dump_sets:
2445 15301671 : if (dump_file && (dump_flags & TDF_DETAILS))
2446 : {
2447 151 : if (changed)
2448 129 : fprintf (dump_file, "[changed] ");
2449 151 : print_bitmap_set (dump_file, ANTIC_IN (block), "ANTIC_IN",
2450 : block->index);
2451 :
2452 151 : if (S)
2453 151 : print_bitmap_set (dump_file, S, "S", block->index);
2454 : }
2455 15301671 : if (old)
2456 15297265 : bitmap_set_free (old);
2457 15301671 : if (S)
2458 15297265 : bitmap_set_free (S);
2459 15301671 : if (ANTIC_OUT)
2460 15297265 : bitmap_set_free (ANTIC_OUT);
2461 15301671 : return changed;
2462 : }
2463 :
2464 : /* Compute PARTIAL_ANTIC for BLOCK.
2465 :
2466 : If succs(BLOCK) > 1 then
2467 : PA_OUT[BLOCK] = value wise union of PA_IN[b] + all ANTIC_IN not
2468 : in ANTIC_OUT for all succ(BLOCK)
2469 : else if succs(BLOCK) == 1 then
2470 : PA_OUT[BLOCK] = phi_translate (PA_IN[succ(BLOCK)])
2471 :
2472 : PA_IN[BLOCK] = clean(PA_OUT[BLOCK] - TMP_GEN[BLOCK] - ANTIC_IN[BLOCK])
2473 :
2474 : */
2475 : static void
2476 1219062 : compute_partial_antic_aux (basic_block block,
2477 : bool block_has_abnormal_pred_edge)
2478 : {
2479 1219062 : bitmap_set_t old_PA_IN;
2480 1219062 : bitmap_set_t PA_OUT;
2481 1219062 : edge e;
2482 1219062 : edge_iterator ei;
2483 1219062 : unsigned long max_pa = param_max_partial_antic_length;
2484 :
2485 1219062 : old_PA_IN = PA_OUT = NULL;
2486 :
2487 : /* If any edges from predecessors are abnormal, antic_in is empty,
2488 : so do nothing. */
2489 1219062 : if (block_has_abnormal_pred_edge)
2490 777 : goto maybe_dump_sets;
2491 :
2492 : /* If there are too many partially anticipatable values in the
2493 : block, phi_translate_set can take an exponential time: stop
2494 : before the translation starts. */
2495 1218285 : if (max_pa
2496 1127682 : && single_succ_p (block)
2497 1981094 : && bitmap_count_bits (&PA_IN (single_succ (block))->values) > max_pa)
2498 535 : goto maybe_dump_sets;
2499 :
2500 1217750 : old_PA_IN = PA_IN (block);
2501 1217750 : PA_OUT = bitmap_set_new ();
2502 :
2503 : /* If the block has no successors, ANTIC_OUT is empty. */
2504 1217750 : if (EDGE_COUNT (block->succs) == 0)
2505 : ;
2506 : /* If we have one successor, we could have some phi nodes to
2507 : translate through. Note that we can't phi translate across DFS
2508 : back edges in partial antic, because it uses a union operation on
2509 : the successors. For recurrences like IV's, we will end up
2510 : generating a new value in the set on each go around (i + 3 (VH.1)
2511 : VH.1 + 1 (VH.2), VH.2 + 1 (VH.3), etc), forever. */
2512 1127149 : else if (single_succ_p (block))
2513 : {
2514 762276 : e = single_succ_edge (block);
2515 762276 : if (!(e->flags & EDGE_DFS_BACK))
2516 683987 : phi_translate_set (PA_OUT, PA_IN (e->dest), e);
2517 : }
2518 : /* If we have multiple successors, we take the union of all of
2519 : them. */
2520 : else
2521 : {
2522 364873 : size_t i;
2523 :
2524 364873 : auto_vec<edge> worklist (EDGE_COUNT (block->succs));
2525 1099965 : FOR_EACH_EDGE (e, ei, block->succs)
2526 : {
2527 735092 : if (e->flags & EDGE_DFS_BACK)
2528 312 : continue;
2529 734780 : worklist.quick_push (e);
2530 : }
2531 364873 : if (worklist.length () > 0)
2532 : {
2533 1099653 : FOR_EACH_VEC_ELT (worklist, i, e)
2534 : {
2535 734780 : unsigned int i;
2536 734780 : bitmap_iterator bi;
2537 :
2538 734780 : if (!gimple_seq_empty_p (phi_nodes (e->dest)))
2539 : {
2540 757 : bitmap_set_t antic_in = bitmap_set_new ();
2541 757 : phi_translate_set (antic_in, ANTIC_IN (e->dest), e);
2542 1548 : FOR_EACH_EXPR_ID_IN_SET (antic_in, i, bi)
2543 791 : bitmap_value_insert_into_set (PA_OUT,
2544 : expression_for_id (i));
2545 757 : bitmap_set_free (antic_in);
2546 757 : bitmap_set_t pa_in = bitmap_set_new ();
2547 757 : phi_translate_set (pa_in, PA_IN (e->dest), e);
2548 757 : FOR_EACH_EXPR_ID_IN_SET (pa_in, i, bi)
2549 0 : bitmap_value_insert_into_set (PA_OUT,
2550 : expression_for_id (i));
2551 757 : bitmap_set_free (pa_in);
2552 : }
2553 : else
2554 : {
2555 4704000 : FOR_EACH_EXPR_ID_IN_SET (ANTIC_IN (e->dest), i, bi)
2556 3969977 : bitmap_value_insert_into_set (PA_OUT,
2557 : expression_for_id (i));
2558 7459093 : FOR_EACH_EXPR_ID_IN_SET (PA_IN (e->dest), i, bi)
2559 6725070 : bitmap_value_insert_into_set (PA_OUT,
2560 : expression_for_id (i));
2561 : }
2562 : }
2563 : }
2564 364873 : }
2565 :
2566 : /* Prune expressions that are clobbered in block and thus become
2567 : invalid if translated from PA_OUT to PA_IN. */
2568 1217750 : prune_clobbered_mems (PA_OUT, block, false);
2569 :
2570 : /* PA_IN starts with PA_OUT - TMP_GEN.
2571 : Then we subtract things from ANTIC_IN. */
2572 1217750 : PA_IN (block) = bitmap_set_subtract_expressions (PA_OUT, TMP_GEN (block));
2573 :
2574 : /* For partial antic, we want to put back in the phi results, since
2575 : we will properly avoid making them partially antic over backedges. */
2576 1217750 : bitmap_ior_into (&PA_IN (block)->values, &PHI_GEN (block)->values);
2577 1217750 : bitmap_ior_into (&PA_IN (block)->expressions, &PHI_GEN (block)->expressions);
2578 :
2579 : /* PA_IN[block] = PA_IN[block] - ANTIC_IN[block] */
2580 1217750 : bitmap_set_subtract_values (PA_IN (block), ANTIC_IN (block));
2581 :
2582 1217750 : clean (PA_IN (block), ANTIC_IN (block));
2583 :
2584 1219062 : maybe_dump_sets:
2585 1219062 : if (dump_file && (dump_flags & TDF_DETAILS))
2586 : {
2587 0 : if (PA_OUT)
2588 0 : print_bitmap_set (dump_file, PA_OUT, "PA_OUT", block->index);
2589 :
2590 0 : print_bitmap_set (dump_file, PA_IN (block), "PA_IN", block->index);
2591 : }
2592 1219062 : if (old_PA_IN)
2593 1217750 : bitmap_set_free (old_PA_IN);
2594 1219062 : if (PA_OUT)
2595 1217750 : bitmap_set_free (PA_OUT);
2596 1219062 : }
2597 :
2598 : /* Compute ANTIC and partial ANTIC sets. */
2599 :
2600 : static void
2601 961524 : compute_antic (void)
2602 : {
2603 961524 : bool changed = true;
2604 961524 : int num_iterations = 0;
2605 961524 : basic_block block;
2606 961524 : int i;
2607 961524 : edge_iterator ei;
2608 961524 : edge e;
2609 :
2610 : /* If any predecessor edges are abnormal, we punt, so antic_in is empty.
2611 : We pre-build the map of blocks with incoming abnormal edges here. */
2612 961524 : auto_sbitmap has_abnormal_preds (last_basic_block_for_fn (cfun));
2613 961524 : bitmap_clear (has_abnormal_preds);
2614 :
2615 15811436 : FOR_ALL_BB_FN (block, cfun)
2616 : {
2617 14849912 : BB_VISITED (block) = 0;
2618 :
2619 33464286 : FOR_EACH_EDGE (e, ei, block->preds)
2620 18617768 : if (e->flags & EDGE_ABNORMAL)
2621 : {
2622 3394 : bitmap_set_bit (has_abnormal_preds, block->index);
2623 3394 : break;
2624 : }
2625 :
2626 : /* While we are here, give empty ANTIC_IN sets to each block. */
2627 14849912 : ANTIC_IN (block) = bitmap_set_new ();
2628 14849912 : if (do_partial_partial)
2629 1219062 : PA_IN (block) = bitmap_set_new ();
2630 : }
2631 :
2632 : /* At the exit block we anticipate nothing. */
2633 961524 : BB_VISITED (EXIT_BLOCK_PTR_FOR_FN (cfun)) = 1;
2634 :
2635 : /* For ANTIC computation we need a postorder that also guarantees that
2636 : a block with a single successor is visited after its successor.
2637 : RPO on the inverted CFG has this property. */
2638 961524 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
2639 961524 : int n = inverted_rev_post_order_compute (cfun, rpo);
2640 :
2641 961524 : auto_sbitmap worklist (last_basic_block_for_fn (cfun) + 1);
2642 961524 : bitmap_clear (worklist);
2643 2767534 : FOR_EACH_EDGE (e, ei, EXIT_BLOCK_PTR_FOR_FN (cfun)->preds)
2644 1806010 : bitmap_set_bit (worklist, e->src->index);
2645 2974307 : while (changed)
2646 : {
2647 2012783 : if (dump_file && (dump_flags & TDF_DETAILS))
2648 35 : fprintf (dump_file, "Starting iteration %d\n", num_iterations);
2649 : /* ??? We need to clear our PHI translation cache here as the
2650 : ANTIC sets shrink and we restrict valid translations to
2651 : those having operands with leaders in ANTIC. Same below
2652 : for PA ANTIC computation. */
2653 2012783 : num_iterations++;
2654 2012783 : changed = false;
2655 37522330 : for (i = 0; i < n; ++i)
2656 : {
2657 35509547 : if (bitmap_bit_p (worklist, rpo[i]))
2658 : {
2659 15301671 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2660 15301671 : bitmap_clear_bit (worklist, block->index);
2661 15301671 : if (compute_antic_aux (block,
2662 15301671 : bitmap_bit_p (has_abnormal_preds,
2663 : block->index)))
2664 : {
2665 32173971 : FOR_EACH_EDGE (e, ei, block->preds)
2666 17691285 : bitmap_set_bit (worklist, e->src->index);
2667 : changed = true;
2668 : }
2669 : }
2670 : }
2671 : /* Theoretically possible, but *highly* unlikely. */
2672 2012783 : gcc_checking_assert (num_iterations < 500);
2673 : }
2674 :
2675 : /* We have to clean after the dataflow problem converged as cleaning
2676 : can cause non-convergence because it is based on expressions
2677 : rather than values. */
2678 13888388 : FOR_EACH_BB_FN (block, cfun)
2679 12926864 : clean (ANTIC_IN (block));
2680 :
2681 961524 : statistics_histogram_event (cfun, "compute_antic iterations",
2682 : num_iterations);
2683 :
2684 961524 : if (do_partial_partial)
2685 : {
2686 : /* For partial antic we ignore backedges and thus we do not need
2687 : to perform any iteration when we process blocks in rpo. */
2688 1309663 : for (i = 0; i < n; ++i)
2689 : {
2690 1219062 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[i]);
2691 1219062 : compute_partial_antic_aux (block,
2692 1219062 : bitmap_bit_p (has_abnormal_preds,
2693 : block->index));
2694 : }
2695 : }
2696 :
2697 961524 : free (rpo);
2698 961524 : }
2699 :
2700 :
2701 : /* Inserted expressions are placed onto this worklist, which is used
2702 : for performing quick dead code elimination of insertions we made
2703 : that didn't turn out to be necessary. */
2704 : static bitmap inserted_exprs;
2705 :
2706 : /* The actual worker for create_component_ref_by_pieces. */
2707 :
2708 : static tree
2709 1113756 : create_component_ref_by_pieces_1 (basic_block block, vn_reference_t ref,
2710 : unsigned int *operand, gimple_seq *stmts)
2711 : {
2712 1113756 : vn_reference_op_t currop = &ref->operands[*operand];
2713 1113756 : tree genop;
2714 1113756 : ++*operand;
2715 1113756 : switch (currop->opcode)
2716 : {
2717 0 : case CALL_EXPR:
2718 0 : gcc_unreachable ();
2719 :
2720 389936 : case MEM_REF:
2721 389936 : {
2722 389936 : tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2723 : stmts);
2724 389936 : if (!baseop)
2725 : return NULL_TREE;
2726 389932 : tree offset = currop->op0;
2727 389932 : if (TREE_CODE (baseop) == ADDR_EXPR
2728 389932 : && handled_component_p (TREE_OPERAND (baseop, 0)))
2729 : {
2730 102 : poly_int64 off;
2731 102 : tree base;
2732 102 : base = get_addr_base_and_unit_offset (TREE_OPERAND (baseop, 0),
2733 : &off);
2734 102 : gcc_assert (base);
2735 102 : offset = int_const_binop (PLUS_EXPR, offset,
2736 102 : build_int_cst (TREE_TYPE (offset),
2737 : off));
2738 102 : baseop = build_fold_addr_expr (base);
2739 : }
2740 389932 : genop = build2 (MEM_REF, currop->type, baseop, offset);
2741 389932 : MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2742 389932 : MR_DEPENDENCE_BASE (genop) = currop->base;
2743 389932 : REF_REVERSE_STORAGE_ORDER (genop) = currop->reverse;
2744 389932 : return genop;
2745 : }
2746 :
2747 0 : case TARGET_MEM_REF:
2748 0 : {
2749 0 : tree genop0 = NULL_TREE, genop1 = NULL_TREE;
2750 0 : vn_reference_op_t nextop = &ref->operands[(*operand)++];
2751 0 : tree baseop = create_component_ref_by_pieces_1 (block, ref, operand,
2752 : stmts);
2753 0 : if (!baseop)
2754 : return NULL_TREE;
2755 0 : if (currop->op0)
2756 : {
2757 0 : genop0 = find_or_generate_expression (block, currop->op0, stmts);
2758 0 : if (!genop0)
2759 : return NULL_TREE;
2760 : }
2761 0 : if (nextop->op0)
2762 : {
2763 0 : genop1 = find_or_generate_expression (block, nextop->op0, stmts);
2764 0 : if (!genop1)
2765 : return NULL_TREE;
2766 : }
2767 0 : genop = build5 (TARGET_MEM_REF, currop->type,
2768 : baseop, currop->op2, genop0, currop->op1, genop1);
2769 :
2770 0 : MR_DEPENDENCE_CLIQUE (genop) = currop->clique;
2771 0 : MR_DEPENDENCE_BASE (genop) = currop->base;
2772 0 : return genop;
2773 : }
2774 :
2775 236932 : case ADDR_EXPR:
2776 236932 : if (currop->op0)
2777 : {
2778 234785 : gcc_assert (is_gimple_min_invariant (currop->op0));
2779 234785 : return currop->op0;
2780 : }
2781 : /* Fallthrough. */
2782 6357 : case REALPART_EXPR:
2783 6357 : case IMAGPART_EXPR:
2784 6357 : case VIEW_CONVERT_EXPR:
2785 6357 : {
2786 6357 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2787 : stmts);
2788 6357 : if (!genop0)
2789 : return NULL_TREE;
2790 6357 : return build1 (currop->opcode, currop->type, genop0);
2791 : }
2792 :
2793 4 : case WITH_SIZE_EXPR:
2794 4 : {
2795 4 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2796 : stmts);
2797 4 : if (!genop0)
2798 : return NULL_TREE;
2799 4 : tree genop1 = find_or_generate_expression (block, currop->op0, stmts);
2800 4 : if (!genop1)
2801 : return NULL_TREE;
2802 4 : return build2 (currop->opcode, currop->type, genop0, genop1);
2803 : }
2804 :
2805 1778 : case BIT_FIELD_REF:
2806 1778 : {
2807 1778 : tree genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2808 : stmts);
2809 1778 : if (!genop0)
2810 : return NULL_TREE;
2811 1778 : tree op1 = currop->op0;
2812 1778 : tree op2 = currop->op1;
2813 1778 : tree t = build3 (BIT_FIELD_REF, currop->type, genop0, op1, op2);
2814 1778 : REF_REVERSE_STORAGE_ORDER (t) = currop->reverse;
2815 1778 : return t;
2816 : }
2817 :
2818 : /* For array ref vn_reference_op's, operand 1 of the array ref
2819 : is op0 of the reference op and operand 3 of the array ref is
2820 : op1. */
2821 58993 : case ARRAY_RANGE_REF:
2822 58993 : case ARRAY_REF:
2823 58993 : {
2824 58993 : tree genop0;
2825 58993 : tree genop1 = currop->op0;
2826 58993 : tree genop2 = currop->op1;
2827 58993 : tree genop3 = currop->op2;
2828 58993 : genop0 = create_component_ref_by_pieces_1 (block, ref, operand,
2829 : stmts);
2830 58993 : if (!genop0)
2831 : return NULL_TREE;
2832 58993 : genop1 = find_or_generate_expression (block, genop1, stmts);
2833 58993 : if (!genop1)
2834 : return NULL_TREE;
2835 58993 : if (genop2)
2836 : {
2837 58993 : tree domain_type = TYPE_DOMAIN (TREE_TYPE (genop0));
2838 : /* Drop zero minimum index if redundant. */
2839 58993 : if (integer_zerop (genop2)
2840 58993 : && (!domain_type
2841 57962 : || integer_zerop (TYPE_MIN_VALUE (domain_type))))
2842 : genop2 = NULL_TREE;
2843 : else
2844 : {
2845 604 : genop2 = find_or_generate_expression (block, genop2, stmts);
2846 604 : if (!genop2)
2847 : return NULL_TREE;
2848 : }
2849 : }
2850 58993 : if (genop3)
2851 : {
2852 58993 : tree elmt_type = TREE_TYPE (TREE_TYPE (genop0));
2853 : /* We can't always put a size in units of the element alignment
2854 : here as the element alignment may be not visible. See
2855 : PR43783. Simply drop the element size for constant
2856 : sizes. */
2857 58993 : if ((TREE_CODE (genop3) == INTEGER_CST
2858 58989 : && TREE_CODE (TYPE_SIZE_UNIT (elmt_type)) == INTEGER_CST
2859 58989 : && wi::eq_p (wi::to_offset (TYPE_SIZE_UNIT (elmt_type)),
2860 58989 : (wi::to_offset (genop3) * vn_ref_op_align_unit (currop))))
2861 58993 : || (TREE_CODE (genop3) == EXACT_DIV_EXPR
2862 0 : && TREE_CODE (TREE_OPERAND (genop3, 1)) == INTEGER_CST
2863 0 : && operand_equal_p (TREE_OPERAND (genop3, 0), TYPE_SIZE_UNIT (elmt_type))
2864 0 : && wi::eq_p (wi::to_offset (TREE_OPERAND (genop3, 1)),
2865 58989 : vn_ref_op_align_unit (currop))))
2866 : genop3 = NULL_TREE;
2867 : else
2868 : {
2869 4 : genop3 = find_or_generate_expression (block, genop3, stmts);
2870 4 : if (!genop3)
2871 : return NULL_TREE;
2872 : }
2873 : }
2874 58993 : return build4 (currop->opcode, currop->type, genop0, genop1,
2875 58993 : genop2, genop3);
2876 : }
2877 262695 : case COMPONENT_REF:
2878 262695 : {
2879 262695 : tree op0;
2880 262695 : tree op1;
2881 262695 : tree genop2 = currop->op1;
2882 262695 : op0 = create_component_ref_by_pieces_1 (block, ref, operand, stmts);
2883 262695 : if (!op0)
2884 : return NULL_TREE;
2885 : /* op1 should be a FIELD_DECL, which are represented by themselves. */
2886 262687 : op1 = currop->op0;
2887 262687 : if (genop2)
2888 : {
2889 0 : genop2 = find_or_generate_expression (block, genop2, stmts);
2890 0 : if (!genop2)
2891 : return NULL_TREE;
2892 : }
2893 262687 : return build3 (COMPONENT_REF, TREE_TYPE (op1), op0, op1, genop2);
2894 : }
2895 :
2896 157370 : case SSA_NAME:
2897 157370 : {
2898 157370 : genop = find_or_generate_expression (block, currop->op0, stmts);
2899 157370 : return genop;
2900 : }
2901 1838 : case STRING_CST:
2902 1838 : case INTEGER_CST:
2903 1838 : case POLY_INT_CST:
2904 1838 : case COMPLEX_CST:
2905 1838 : case VECTOR_CST:
2906 1838 : case REAL_CST:
2907 1838 : case CONSTRUCTOR:
2908 1838 : case VAR_DECL:
2909 1838 : case PARM_DECL:
2910 1838 : case CONST_DECL:
2911 1838 : case RESULT_DECL:
2912 1838 : case FUNCTION_DECL:
2913 1838 : return currop->op0;
2914 :
2915 0 : default:
2916 0 : gcc_unreachable ();
2917 : }
2918 : }
2919 :
2920 : /* For COMPONENT_REF's and ARRAY_REF's, we can't have any intermediates for the
2921 : COMPONENT_REF or MEM_REF or ARRAY_REF portion, because we'd end up with
2922 : trying to rename aggregates into ssa form directly, which is a no no.
2923 :
2924 : Thus, this routine doesn't create temporaries, it just builds a
2925 : single access expression for the array, calling
2926 : find_or_generate_expression to build the innermost pieces.
2927 :
2928 : This function is a subroutine of create_expression_by_pieces, and
2929 : should not be called on it's own unless you really know what you
2930 : are doing. */
2931 :
2932 : static tree
2933 389965 : create_component_ref_by_pieces (basic_block block, vn_reference_t ref,
2934 : gimple_seq *stmts)
2935 : {
2936 389965 : unsigned int op = 0;
2937 389965 : return create_component_ref_by_pieces_1 (block, ref, &op, stmts);
2938 : }
2939 :
2940 : /* Find a simple leader for an expression, or generate one using
2941 : create_expression_by_pieces from a NARY expression for the value.
2942 : BLOCK is the basic_block we are looking for leaders in.
2943 : OP is the tree expression to find a leader for or generate.
2944 : Returns the leader or NULL_TREE on failure. */
2945 :
2946 : static tree
2947 837657 : find_or_generate_expression (basic_block block, tree op, gimple_seq *stmts)
2948 : {
2949 : /* Constants are always leaders. */
2950 837657 : if (is_gimple_min_invariant (op))
2951 : return op;
2952 :
2953 642985 : gcc_assert (TREE_CODE (op) == SSA_NAME);
2954 642985 : vn_ssa_aux_t info = VN_INFO (op);
2955 642985 : unsigned int lookfor = info->value_id;
2956 642985 : if (value_id_constant_p (lookfor))
2957 3 : return info->valnum;
2958 :
2959 642982 : pre_expr leader = bitmap_find_leader (AVAIL_OUT (block), lookfor);
2960 642982 : if (leader)
2961 : {
2962 610411 : if (leader->kind == NAME)
2963 : {
2964 610411 : tree name = PRE_EXPR_NAME (leader);
2965 610411 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (name))
2966 : return NULL_TREE;
2967 : return name;
2968 : }
2969 0 : else if (leader->kind == CONSTANT)
2970 0 : return PRE_EXPR_CONSTANT (leader);
2971 :
2972 : /* Defer. */
2973 : return NULL_TREE;
2974 : }
2975 32571 : gcc_assert (!value_id_constant_p (lookfor));
2976 :
2977 : /* It must be a complex expression, so generate it recursively. Note
2978 : that this is only necessary to handle gcc.dg/tree-ssa/ssa-pre28.c
2979 : where the insert algorithm fails to insert a required expression. */
2980 32571 : bitmap exprset = value_expressions[lookfor];
2981 32571 : bitmap_iterator bi;
2982 32571 : unsigned int i;
2983 32571 : if (exprset)
2984 46718 : EXECUTE_IF_SET_IN_BITMAP (exprset, 0, i, bi)
2985 : {
2986 43765 : pre_expr temp = expression_for_id (i);
2987 : /* We cannot insert random REFERENCE expressions at arbitrary
2988 : places. We can insert NARYs which eventually re-materializes
2989 : its operand values. */
2990 43765 : if (temp->kind == NARY)
2991 29614 : return create_expression_by_pieces (block, temp, stmts,
2992 59228 : TREE_TYPE (op));
2993 : }
2994 :
2995 : /* Defer. */
2996 : return NULL_TREE;
2997 : }
2998 :
2999 : /* Create an expression in pieces, so that we can handle very complex
3000 : expressions that may be ANTIC, but not necessary GIMPLE.
3001 : BLOCK is the basic block the expression will be inserted into,
3002 : EXPR is the expression to insert (in value form)
3003 : STMTS is a statement list to append the necessary insertions into.
3004 :
3005 : This function will die if we hit some value that shouldn't be
3006 : ANTIC but is (IE there is no leader for it, or its components).
3007 : The function returns NULL_TREE in case a different antic expression
3008 : has to be inserted first.
3009 : This function may also generate expressions that are themselves
3010 : partially or fully redundant. Those that are will be either made
3011 : fully redundant during the next iteration of insert (for partially
3012 : redundant ones), or eliminated by eliminate (for fully redundant
3013 : ones). */
3014 :
3015 : static tree
3016 2855935 : create_expression_by_pieces (basic_block block, pre_expr expr,
3017 : gimple_seq *stmts, tree type)
3018 : {
3019 2855935 : tree name;
3020 2855935 : tree folded;
3021 2855935 : gimple_seq forced_stmts = NULL;
3022 2855935 : unsigned int value_id;
3023 2855935 : gimple_stmt_iterator gsi;
3024 2855935 : tree exprtype = type ? type : get_expr_type (expr);
3025 2855935 : pre_expr nameexpr;
3026 2855935 : gassign *newstmt;
3027 :
3028 2855935 : switch (expr->kind)
3029 : {
3030 : /* We may hit the NAME/CONSTANT case if we have to convert types
3031 : that value numbering saw through. */
3032 725454 : case NAME:
3033 725454 : folded = PRE_EXPR_NAME (expr);
3034 725454 : if (SSA_NAME_OCCURS_IN_ABNORMAL_PHI (folded))
3035 : return NULL_TREE;
3036 725449 : if (useless_type_conversion_p (exprtype, TREE_TYPE (folded)))
3037 : return folded;
3038 : break;
3039 1371590 : case CONSTANT:
3040 1371590 : {
3041 1371590 : folded = PRE_EXPR_CONSTANT (expr);
3042 1371590 : tree tem = fold_convert (exprtype, folded);
3043 1371590 : if (is_gimple_min_invariant (tem))
3044 : return tem;
3045 : break;
3046 : }
3047 392658 : case REFERENCE:
3048 392658 : if (PRE_EXPR_REFERENCE (expr)->operands[0].opcode == CALL_EXPR)
3049 : {
3050 2693 : vn_reference_t ref = PRE_EXPR_REFERENCE (expr);
3051 2693 : unsigned int operand = 1;
3052 2693 : vn_reference_op_t currop = &ref->operands[0];
3053 2693 : tree sc = NULL_TREE;
3054 2693 : tree fn = NULL_TREE;
3055 2693 : if (currop->op0)
3056 : {
3057 2551 : fn = find_or_generate_expression (block, currop->op0, stmts);
3058 2551 : if (!fn)
3059 0 : return NULL_TREE;
3060 : }
3061 2693 : if (currop->op1)
3062 : {
3063 0 : sc = find_or_generate_expression (block, currop->op1, stmts);
3064 0 : if (!sc)
3065 : return NULL_TREE;
3066 : }
3067 5386 : auto_vec<tree> args (ref->operands.length () - 1);
3068 6721 : while (operand < ref->operands.length ())
3069 : {
3070 4028 : tree arg = create_component_ref_by_pieces_1 (block, ref,
3071 4028 : &operand, stmts);
3072 4028 : if (!arg)
3073 0 : return NULL_TREE;
3074 4028 : args.quick_push (arg);
3075 : }
3076 2693 : gcall *call;
3077 2693 : if (currop->op0)
3078 : {
3079 2551 : call = gimple_build_call_vec (fn, args);
3080 2551 : gimple_call_set_fntype (call, currop->type);
3081 : }
3082 : else
3083 142 : call = gimple_build_call_internal_vec ((internal_fn)currop->clique,
3084 : args);
3085 2693 : gimple_set_location (call, expr->loc);
3086 2693 : if (sc)
3087 0 : gimple_call_set_chain (call, sc);
3088 2693 : tree forcedname = make_ssa_name (ref->type);
3089 2693 : gimple_call_set_lhs (call, forcedname);
3090 : /* There's no CCP pass after PRE which would re-compute alignment
3091 : information so make sure we re-materialize this here. */
3092 2693 : if (gimple_call_builtin_p (call, BUILT_IN_ASSUME_ALIGNED)
3093 0 : && args.length () - 2 <= 1
3094 0 : && tree_fits_uhwi_p (args[1])
3095 2693 : && (args.length () != 3 || tree_fits_uhwi_p (args[2])))
3096 : {
3097 0 : unsigned HOST_WIDE_INT halign = tree_to_uhwi (args[1]);
3098 0 : unsigned HOST_WIDE_INT hmisalign
3099 0 : = args.length () == 3 ? tree_to_uhwi (args[2]) : 0;
3100 0 : if ((halign & (halign - 1)) == 0
3101 0 : && (hmisalign & ~(halign - 1)) == 0
3102 0 : && (unsigned int)halign != 0)
3103 0 : set_ptr_info_alignment (get_ptr_info (forcedname),
3104 : halign, hmisalign);
3105 : }
3106 2693 : gimple_set_vuse (call, BB_LIVE_VOP_ON_EXIT (block));
3107 2693 : gimple_seq_add_stmt_without_update (&forced_stmts, call);
3108 2693 : folded = forcedname;
3109 2693 : }
3110 : else
3111 : {
3112 389965 : folded = create_component_ref_by_pieces (block,
3113 : PRE_EXPR_REFERENCE (expr),
3114 : stmts);
3115 389965 : if (!folded)
3116 : return NULL_TREE;
3117 389961 : name = make_temp_ssa_name (exprtype, NULL, "pretmp");
3118 389961 : newstmt = gimple_build_assign (name, folded);
3119 389961 : gimple_set_location (newstmt, expr->loc);
3120 389961 : gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
3121 389961 : gimple_set_vuse (newstmt, BB_LIVE_VOP_ON_EXIT (block));
3122 389961 : folded = name;
3123 : }
3124 : break;
3125 366233 : case NARY:
3126 366233 : {
3127 366233 : vn_nary_op_t nary = PRE_EXPR_NARY (expr);
3128 366233 : tree *genop = XALLOCAVEC (tree, nary->length);
3129 366233 : unsigned i;
3130 974370 : for (i = 0; i < nary->length; ++i)
3131 : {
3132 618131 : genop[i] = find_or_generate_expression (block, nary->op[i], stmts);
3133 618131 : if (!genop[i])
3134 : return NULL_TREE;
3135 : /* Ensure genop[] is properly typed for POINTER_PLUS_EXPR. It
3136 : may have conversions stripped. */
3137 608137 : if (nary->opcode == POINTER_PLUS_EXPR)
3138 : {
3139 103632 : if (i == 0)
3140 51835 : genop[i] = gimple_convert (&forced_stmts,
3141 : nary->type, genop[i]);
3142 51797 : else if (i == 1)
3143 51797 : genop[i] = gimple_convert (&forced_stmts,
3144 : sizetype, genop[i]);
3145 : }
3146 : else
3147 504505 : genop[i] = gimple_convert (&forced_stmts,
3148 504505 : TREE_TYPE (nary->op[i]), genop[i]);
3149 : }
3150 356239 : if (nary->opcode == CONSTRUCTOR)
3151 : {
3152 4 : vec<constructor_elt, va_gc> *elts = NULL;
3153 20 : for (i = 0; i < nary->length; ++i)
3154 16 : CONSTRUCTOR_APPEND_ELT (elts, NULL_TREE, genop[i]);
3155 4 : folded = build_constructor (nary->type, elts);
3156 4 : name = make_temp_ssa_name (exprtype, NULL, "pretmp");
3157 4 : newstmt = gimple_build_assign (name, folded);
3158 4 : gimple_set_location (newstmt, expr->loc);
3159 4 : gimple_seq_add_stmt_without_update (&forced_stmts, newstmt);
3160 4 : folded = name;
3161 : }
3162 : else
3163 : {
3164 356235 : switch (nary->length)
3165 : {
3166 105636 : case 1:
3167 105636 : folded = gimple_build (&forced_stmts, expr->loc,
3168 : nary->opcode, nary->type, genop[0]);
3169 105636 : break;
3170 250417 : case 2:
3171 250417 : folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
3172 : nary->type, genop[0], genop[1]);
3173 250417 : break;
3174 182 : case 3:
3175 182 : folded = gimple_build (&forced_stmts, expr->loc, nary->opcode,
3176 : nary->type, genop[0], genop[1],
3177 : genop[2]);
3178 182 : break;
3179 0 : default:
3180 0 : gcc_unreachable ();
3181 : }
3182 : }
3183 : }
3184 : break;
3185 0 : default:
3186 0 : gcc_unreachable ();
3187 : }
3188 :
3189 841501 : folded = gimple_convert (&forced_stmts, exprtype, folded);
3190 :
3191 : /* If there is nothing to insert, return the simplified result. */
3192 841501 : if (gimple_seq_empty_p (forced_stmts))
3193 : return folded;
3194 : /* If we simplified to a constant return it and discard eventually
3195 : built stmts. */
3196 748835 : if (is_gimple_min_invariant (folded))
3197 : {
3198 0 : gimple_seq_discard (forced_stmts);
3199 0 : return folded;
3200 : }
3201 : /* Likewise if we simplified to sth not queued for insertion. */
3202 748835 : bool found = false;
3203 748835 : gsi = gsi_last (forced_stmts);
3204 748835 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
3205 : {
3206 748835 : gimple *stmt = gsi_stmt (gsi);
3207 748835 : tree forcedname = gimple_get_lhs (stmt);
3208 748835 : if (forcedname == folded)
3209 : {
3210 : found = true;
3211 : break;
3212 : }
3213 : }
3214 748835 : if (! found)
3215 : {
3216 0 : gimple_seq_discard (forced_stmts);
3217 0 : return folded;
3218 : }
3219 748835 : gcc_assert (TREE_CODE (folded) == SSA_NAME);
3220 :
3221 : /* If we have any intermediate expressions to the value sets, add them
3222 : to the value sets and chain them in the instruction stream. */
3223 748835 : if (forced_stmts)
3224 : {
3225 748835 : gsi = gsi_start (forced_stmts);
3226 1498149 : for (; !gsi_end_p (gsi); gsi_next (&gsi))
3227 : {
3228 749314 : gimple *stmt = gsi_stmt (gsi);
3229 749314 : tree forcedname = gimple_get_lhs (stmt);
3230 749314 : pre_expr nameexpr;
3231 :
3232 749314 : if (forcedname != folded)
3233 : {
3234 479 : vn_ssa_aux_t vn_info = VN_INFO (forcedname);
3235 479 : vn_info->valnum = forcedname;
3236 479 : vn_info->value_id = get_next_value_id ();
3237 479 : nameexpr = get_or_alloc_expr_for_name (forcedname);
3238 479 : add_to_value (vn_info->value_id, nameexpr);
3239 479 : if (NEW_SETS (block))
3240 479 : bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3241 479 : bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3242 : }
3243 :
3244 749314 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (forcedname));
3245 : }
3246 748835 : gimple_seq_add_seq (stmts, forced_stmts);
3247 : }
3248 :
3249 748835 : name = folded;
3250 :
3251 : /* Fold the last statement. */
3252 748835 : gsi = gsi_last (*stmts);
3253 748835 : if (fold_stmt_inplace (&gsi))
3254 201322 : update_stmt (gsi_stmt (gsi));
3255 :
3256 : /* Add a value number to the temporary.
3257 : The value may already exist in either NEW_SETS, or AVAIL_OUT, because
3258 : we are creating the expression by pieces, and this particular piece of
3259 : the expression may have been represented. There is no harm in replacing
3260 : here. */
3261 748835 : value_id = get_expr_value_id (expr);
3262 748835 : vn_ssa_aux_t vn_info = VN_INFO (name);
3263 748835 : vn_info->value_id = value_id;
3264 748835 : vn_info->valnum = vn_valnum_from_value_id (value_id);
3265 748835 : if (vn_info->valnum == NULL_TREE)
3266 240467 : vn_info->valnum = name;
3267 748835 : gcc_assert (vn_info->valnum != NULL_TREE);
3268 748835 : nameexpr = get_or_alloc_expr_for_name (name);
3269 748835 : add_to_value (value_id, nameexpr);
3270 748835 : if (NEW_SETS (block))
3271 532697 : bitmap_value_replace_in_set (NEW_SETS (block), nameexpr);
3272 748835 : bitmap_value_replace_in_set (AVAIL_OUT (block), nameexpr);
3273 :
3274 748835 : pre_stats.insertions++;
3275 748835 : if (dump_file && (dump_flags & TDF_DETAILS))
3276 : {
3277 18 : fprintf (dump_file, "Inserted ");
3278 36 : print_gimple_stmt (dump_file, gsi_stmt (gsi_last (*stmts)), 0);
3279 18 : fprintf (dump_file, " in predecessor %d (%04d)\n",
3280 : block->index, value_id);
3281 : }
3282 :
3283 : return name;
3284 : }
3285 :
3286 :
3287 : /* Insert the to-be-made-available values of expression EXPRNUM for each
3288 : predecessor, stored in AVAIL, into the predecessors of BLOCK, and
3289 : merge the result with a phi node, given the same value number as
3290 : NODE. Return true if we have inserted new stuff. */
3291 :
3292 : static bool
3293 1921997 : insert_into_preds_of_block (basic_block block, unsigned int exprnum,
3294 : vec<pre_expr> &avail)
3295 : {
3296 1921997 : pre_expr expr = expression_for_id (exprnum);
3297 1921997 : pre_expr newphi;
3298 1921997 : unsigned int val = get_expr_value_id (expr);
3299 1921997 : edge pred;
3300 1921997 : bool insertions = false;
3301 1921997 : bool nophi = false;
3302 1921997 : basic_block bprime;
3303 1921997 : pre_expr eprime;
3304 1921997 : edge_iterator ei;
3305 1921997 : tree type = get_expr_type (expr);
3306 1921997 : tree temp;
3307 1921997 : gphi *phi;
3308 :
3309 : /* Make sure we aren't creating an induction variable. */
3310 1921997 : if (bb_loop_depth (block) > 0 && EDGE_COUNT (block->preds) == 2)
3311 : {
3312 1593601 : bool firstinsideloop = false;
3313 1593601 : bool secondinsideloop = false;
3314 4780803 : firstinsideloop = flow_bb_inside_loop_p (block->loop_father,
3315 1593601 : EDGE_PRED (block, 0)->src);
3316 4780803 : secondinsideloop = flow_bb_inside_loop_p (block->loop_father,
3317 1593601 : EDGE_PRED (block, 1)->src);
3318 : /* Induction variables only have one edge inside the loop. */
3319 1593601 : if ((firstinsideloop ^ secondinsideloop)
3320 1517760 : && expr->kind != REFERENCE)
3321 : {
3322 1439703 : if (dump_file && (dump_flags & TDF_DETAILS))
3323 56 : fprintf (dump_file, "Skipping insertion of phi for partial "
3324 : "redundancy: Looks like an induction variable\n");
3325 : nophi = true;
3326 : }
3327 : }
3328 :
3329 : /* Make the necessary insertions. */
3330 5983073 : FOR_EACH_EDGE (pred, ei, block->preds)
3331 : {
3332 : /* When we are not inserting a PHI node do not bother inserting
3333 : into places that do not dominate the anticipated computations. */
3334 4061076 : if (nophi && !dominated_by_p (CDI_DOMINATORS, block, pred->src))
3335 1453886 : continue;
3336 2610164 : gimple_seq stmts = NULL;
3337 2610164 : tree builtexpr;
3338 2610164 : bprime = pred->src;
3339 2610164 : eprime = avail[pred->dest_idx];
3340 2610164 : builtexpr = create_expression_by_pieces (bprime, eprime,
3341 : &stmts, type);
3342 2610164 : gcc_assert (!(pred->flags & EDGE_ABNORMAL));
3343 2610164 : if (!gimple_seq_empty_p (stmts))
3344 : {
3345 510105 : basic_block new_bb = gsi_insert_seq_on_edge_immediate (pred, stmts);
3346 510105 : gcc_assert (! new_bb);
3347 : insertions = true;
3348 : }
3349 2610164 : if (!builtexpr)
3350 : {
3351 : /* We cannot insert a PHI node if we failed to insert
3352 : on one edge. */
3353 2974 : nophi = true;
3354 2974 : continue;
3355 : }
3356 2607190 : if (is_gimple_min_invariant (builtexpr))
3357 1371635 : avail[pred->dest_idx] = get_or_alloc_expr_for_constant (builtexpr);
3358 : else
3359 1235555 : avail[pred->dest_idx] = get_or_alloc_expr_for_name (builtexpr);
3360 : }
3361 : /* If we didn't want a phi node, and we made insertions, we still have
3362 : inserted new stuff, and thus return true. If we didn't want a phi node,
3363 : and didn't make insertions, we haven't added anything new, so return
3364 : false. */
3365 1921997 : if (nophi && insertions)
3366 : return true;
3367 1913194 : else if (nophi && !insertions)
3368 : return false;
3369 :
3370 : /* Now build a phi for the new variable. */
3371 479325 : temp = make_temp_ssa_name (type, NULL, "prephitmp");
3372 479325 : phi = create_phi_node (temp, block);
3373 :
3374 479325 : vn_ssa_aux_t vn_info = VN_INFO (temp);
3375 479325 : vn_info->value_id = val;
3376 479325 : vn_info->valnum = vn_valnum_from_value_id (val);
3377 479325 : if (vn_info->valnum == NULL_TREE)
3378 98909 : vn_info->valnum = temp;
3379 479325 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3380 1645762 : FOR_EACH_EDGE (pred, ei, block->preds)
3381 : {
3382 1166437 : pre_expr ae = avail[pred->dest_idx];
3383 1166437 : gcc_assert (get_expr_type (ae) == type
3384 : || useless_type_conversion_p (type, get_expr_type (ae)));
3385 1166437 : if (ae->kind == CONSTANT)
3386 181774 : add_phi_arg (phi, unshare_expr (PRE_EXPR_CONSTANT (ae)),
3387 : pred, UNKNOWN_LOCATION);
3388 : else
3389 984663 : add_phi_arg (phi, PRE_EXPR_NAME (ae), pred, UNKNOWN_LOCATION);
3390 : }
3391 :
3392 479325 : newphi = get_or_alloc_expr_for_name (temp);
3393 479325 : add_to_value (val, newphi);
3394 :
3395 : /* The value should *not* exist in PHI_GEN, or else we wouldn't be doing
3396 : this insertion, since we test for the existence of this value in PHI_GEN
3397 : before proceeding with the partial redundancy checks in insert_aux.
3398 :
3399 : The value may exist in AVAIL_OUT, in particular, it could be represented
3400 : by the expression we are trying to eliminate, in which case we want the
3401 : replacement to occur. If it's not existing in AVAIL_OUT, we want it
3402 : inserted there.
3403 :
3404 : Similarly, to the PHI_GEN case, the value should not exist in NEW_SETS of
3405 : this block, because if it did, it would have existed in our dominator's
3406 : AVAIL_OUT, and would have been skipped due to the full redundancy check.
3407 : */
3408 :
3409 479325 : bitmap_insert_into_set (PHI_GEN (block), newphi);
3410 479325 : bitmap_value_replace_in_set (AVAIL_OUT (block),
3411 : newphi);
3412 479325 : if (NEW_SETS (block))
3413 479325 : bitmap_insert_into_set (NEW_SETS (block), newphi);
3414 :
3415 : /* If we insert a PHI node for a conversion of another PHI node
3416 : in the same basic-block try to preserve range information.
3417 : This is important so that followup loop passes receive optimal
3418 : number of iteration analysis results. See PR61743. */
3419 479325 : if (expr->kind == NARY
3420 183733 : && CONVERT_EXPR_CODE_P (expr->u.nary->opcode)
3421 54582 : && TREE_CODE (expr->u.nary->op[0]) == SSA_NAME
3422 54407 : && gimple_bb (SSA_NAME_DEF_STMT (expr->u.nary->op[0])) == block
3423 44841 : && INTEGRAL_TYPE_P (type)
3424 44000 : && INTEGRAL_TYPE_P (TREE_TYPE (expr->u.nary->op[0]))
3425 42910 : && (TYPE_PRECISION (type)
3426 42910 : >= TYPE_PRECISION (TREE_TYPE (expr->u.nary->op[0])))
3427 514825 : && SSA_NAME_RANGE_INFO (expr->u.nary->op[0]))
3428 : {
3429 22417 : int_range_max r;
3430 44834 : if (get_range_query (cfun)->range_of_expr (r, expr->u.nary->op[0])
3431 22417 : && !r.undefined_p ()
3432 22417 : && !r.varying_p ()
3433 44834 : && !wi::neg_p (r.lower_bound (), SIGNED)
3434 61324 : && !wi::neg_p (r.upper_bound (), SIGNED))
3435 : {
3436 : /* Just handle extension and sign-changes of all-positive ranges. */
3437 15790 : range_cast (r, type);
3438 15790 : set_range_info (temp, r);
3439 : }
3440 22417 : }
3441 :
3442 479325 : if (dump_file && (dump_flags & TDF_DETAILS))
3443 : {
3444 8 : fprintf (dump_file, "Created phi ");
3445 8 : print_gimple_stmt (dump_file, phi, 0);
3446 8 : fprintf (dump_file, " in block %d (%04d)\n", block->index, val);
3447 : }
3448 479325 : pre_stats.phis++;
3449 479325 : return true;
3450 : }
3451 :
3452 :
3453 :
3454 : /* Perform insertion of partially redundant or hoistable values.
3455 : For BLOCK, do the following:
3456 : 1. Propagate the NEW_SETS of the dominator into the current block.
3457 : If the block has multiple predecessors,
3458 : 2a. Iterate over the ANTIC expressions for the block to see if
3459 : any of them are partially redundant.
3460 : 2b. If so, insert them into the necessary predecessors to make
3461 : the expression fully redundant.
3462 : 2c. Insert a new PHI merging the values of the predecessors.
3463 : 2d. Insert the new PHI, and the new expressions, into the
3464 : NEW_SETS set.
3465 : If the block has multiple successors,
3466 : 3a. Iterate over the ANTIC values for the block to see if
3467 : any of them are good candidates for hoisting.
3468 : 3b. If so, insert expressions computing the values in BLOCK,
3469 : and add the new expressions into the NEW_SETS set.
3470 : 4. Recursively call ourselves on the dominator children of BLOCK.
3471 :
3472 : Steps 1, 2a, and 4 are done by insert_aux. 2b, 2c and 2d are done by
3473 : do_pre_regular_insertion and do_partial_insertion. 3a and 3b are
3474 : done in do_hoist_insertion.
3475 : */
3476 :
3477 : static bool
3478 3600582 : do_pre_regular_insertion (basic_block block, basic_block dom,
3479 : vec<pre_expr> exprs)
3480 : {
3481 3600582 : bool new_stuff = false;
3482 3600582 : pre_expr expr;
3483 3600582 : auto_vec<pre_expr, 2> avail;
3484 3600582 : int i;
3485 :
3486 3600582 : avail.safe_grow (EDGE_COUNT (block->preds), true);
3487 :
3488 25342069 : FOR_EACH_VEC_ELT (exprs, i, expr)
3489 : {
3490 21741487 : if (expr->kind == NARY
3491 21741487 : || expr->kind == REFERENCE)
3492 : {
3493 12338858 : unsigned int val;
3494 12338858 : bool by_some = false;
3495 12338858 : bool cant_insert = false;
3496 12338858 : bool all_same = true;
3497 12338858 : unsigned num_inserts = 0;
3498 12338858 : unsigned num_const = 0;
3499 12338858 : pre_expr first_s = NULL;
3500 12338858 : edge pred;
3501 12338858 : basic_block bprime;
3502 12338858 : pre_expr eprime = NULL;
3503 12338858 : edge_iterator ei;
3504 12338858 : pre_expr edoubleprime = NULL;
3505 12338858 : bool do_insertion = false;
3506 :
3507 12338858 : val = get_expr_value_id (expr);
3508 24677716 : if (bitmap_set_contains_value (PHI_GEN (block), val))
3509 1046081 : continue;
3510 11556367 : if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3511 : {
3512 263590 : if (dump_file && (dump_flags & TDF_DETAILS))
3513 : {
3514 7 : fprintf (dump_file, "Found fully redundant value: ");
3515 7 : print_pre_expr (dump_file, expr);
3516 7 : fprintf (dump_file, "\n");
3517 : }
3518 263590 : continue;
3519 : }
3520 :
3521 36817601 : FOR_EACH_EDGE (pred, ei, block->preds)
3522 : {
3523 25525631 : unsigned int vprime;
3524 :
3525 : /* We should never run insertion for the exit block
3526 : and so not come across fake pred edges. */
3527 25525631 : gcc_assert (!(pred->flags & EDGE_FAKE));
3528 25525631 : bprime = pred->src;
3529 : /* We are looking at ANTIC_OUT of bprime. */
3530 25525631 : eprime = phi_translate (NULL, expr, ANTIC_IN (block), NULL, pred);
3531 :
3532 : /* eprime will generally only be NULL if the
3533 : value of the expression, translated
3534 : through the PHI for this predecessor, is
3535 : undefined. If that is the case, we can't
3536 : make the expression fully redundant,
3537 : because its value is undefined along a
3538 : predecessor path. We can thus break out
3539 : early because it doesn't matter what the
3540 : rest of the results are. */
3541 25525631 : if (eprime == NULL)
3542 : {
3543 807 : avail[pred->dest_idx] = NULL;
3544 807 : cant_insert = true;
3545 807 : break;
3546 : }
3547 :
3548 25524824 : vprime = get_expr_value_id (eprime);
3549 25524824 : edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime),
3550 : vprime);
3551 25524824 : if (edoubleprime == NULL)
3552 : {
3553 22943396 : avail[pred->dest_idx] = eprime;
3554 22943396 : all_same = false;
3555 22943396 : num_inserts++;
3556 : }
3557 : else
3558 : {
3559 2581428 : avail[pred->dest_idx] = edoubleprime;
3560 2581428 : by_some = true;
3561 2581428 : if (edoubleprime->kind == CONSTANT)
3562 1707194 : num_const++;
3563 : /* We want to perform insertions to remove a redundancy on
3564 : a path in the CFG we want to optimize for speed. */
3565 2581428 : if (optimize_edge_for_speed_p (pred))
3566 2157823 : do_insertion = true;
3567 2581428 : if (first_s == NULL)
3568 : first_s = edoubleprime;
3569 286704 : else if (!pre_expr_d::equal (first_s, edoubleprime))
3570 220415 : all_same = false;
3571 : }
3572 : }
3573 : /* If we can insert it, it's not the same value
3574 : already existing along every predecessor, and
3575 : it's defined by some predecessor, it is
3576 : partially redundant. */
3577 11292777 : if (!cant_insert && !all_same && by_some)
3578 : {
3579 : /* If the expression is redundant on all edges and we need
3580 : to at most insert one copy from a constant do the PHI
3581 : insertion even when not optimizing a path that's to be
3582 : optimized for speed. */
3583 2291523 : if (num_inserts == 0 && num_const <= 1)
3584 : do_insertion = true;
3585 2147456 : if (!do_insertion)
3586 : {
3587 376029 : if (dump_file && (dump_flags & TDF_DETAILS))
3588 : {
3589 0 : fprintf (dump_file, "Skipping partial redundancy for "
3590 : "expression ");
3591 0 : print_pre_expr (dump_file, expr);
3592 0 : fprintf (dump_file, " (%04d), no redundancy on to be "
3593 : "optimized for speed edge\n", val);
3594 : }
3595 : }
3596 1915494 : else if (dbg_cnt (treepre_insert))
3597 : {
3598 1915494 : if (dump_file && (dump_flags & TDF_DETAILS))
3599 : {
3600 64 : fprintf (dump_file, "Found partial redundancy for "
3601 : "expression ");
3602 64 : print_pre_expr (dump_file, expr);
3603 64 : fprintf (dump_file, " (%04d)\n",
3604 : get_expr_value_id (expr));
3605 : }
3606 1915494 : if (insert_into_preds_of_block (block,
3607 : get_expression_id (expr),
3608 : avail))
3609 11292777 : new_stuff = true;
3610 : }
3611 : }
3612 : /* If all edges produce the same value and that value is
3613 : an invariant, then the PHI has the same value on all
3614 : edges. Note this. */
3615 9001254 : else if (!cant_insert
3616 9001254 : && all_same
3617 9001254 : && (edoubleprime->kind != NAME
3618 1722 : || !SSA_NAME_OCCURS_IN_ABNORMAL_PHI
3619 : (PRE_EXPR_NAME (edoubleprime))))
3620 : {
3621 3175 : gcc_assert (edoubleprime->kind == CONSTANT
3622 : || edoubleprime->kind == NAME);
3623 :
3624 3175 : tree temp = make_temp_ssa_name (get_expr_type (expr),
3625 : NULL, "pretmp");
3626 3175 : gassign *assign
3627 3175 : = gimple_build_assign (temp,
3628 3175 : edoubleprime->kind == CONSTANT ?
3629 : PRE_EXPR_CONSTANT (edoubleprime) :
3630 : PRE_EXPR_NAME (edoubleprime));
3631 3175 : gimple_stmt_iterator gsi = gsi_after_labels (block);
3632 3175 : gsi_insert_before (&gsi, assign, GSI_NEW_STMT);
3633 :
3634 3175 : vn_ssa_aux_t vn_info = VN_INFO (temp);
3635 3175 : vn_info->value_id = val;
3636 3175 : vn_info->valnum = vn_valnum_from_value_id (val);
3637 3175 : if (vn_info->valnum == NULL_TREE)
3638 503 : vn_info->valnum = temp;
3639 3175 : bitmap_set_bit (inserted_exprs, SSA_NAME_VERSION (temp));
3640 3175 : pre_expr newe = get_or_alloc_expr_for_name (temp);
3641 3175 : add_to_value (val, newe);
3642 3175 : bitmap_value_replace_in_set (AVAIL_OUT (block), newe);
3643 3175 : bitmap_insert_into_set (NEW_SETS (block), newe);
3644 3175 : bitmap_insert_into_set (PHI_GEN (block), newe);
3645 : }
3646 : }
3647 : }
3648 :
3649 3600582 : return new_stuff;
3650 3600582 : }
3651 :
3652 :
3653 : /* Perform insertion for partially anticipatable expressions. There
3654 : is only one case we will perform insertion for these. This case is
3655 : if the expression is partially anticipatable, and fully available.
3656 : In this case, we know that putting it earlier will enable us to
3657 : remove the later computation. */
3658 :
3659 : static bool
3660 329274 : do_pre_partial_partial_insertion (basic_block block, basic_block dom,
3661 : vec<pre_expr> exprs)
3662 : {
3663 329274 : bool new_stuff = false;
3664 329274 : pre_expr expr;
3665 329274 : auto_vec<pre_expr, 2> avail;
3666 329274 : int i;
3667 :
3668 329274 : avail.safe_grow (EDGE_COUNT (block->preds), true);
3669 :
3670 3146829 : FOR_EACH_VEC_ELT (exprs, i, expr)
3671 : {
3672 2817555 : if (expr->kind == NARY
3673 2817555 : || expr->kind == REFERENCE)
3674 : {
3675 2122736 : unsigned int val;
3676 2122736 : bool by_all = true;
3677 2122736 : bool cant_insert = false;
3678 2122736 : edge pred;
3679 2122736 : basic_block bprime;
3680 2122736 : pre_expr eprime = NULL;
3681 2122736 : edge_iterator ei;
3682 :
3683 2122736 : val = get_expr_value_id (expr);
3684 4245472 : if (bitmap_set_contains_value (PHI_GEN (block), val))
3685 56585 : continue;
3686 2112960 : if (bitmap_set_contains_value (AVAIL_OUT (dom), val))
3687 46809 : continue;
3688 :
3689 2142013 : FOR_EACH_EDGE (pred, ei, block->preds)
3690 : {
3691 2131750 : unsigned int vprime;
3692 2131750 : pre_expr edoubleprime;
3693 :
3694 : /* We should never run insertion for the exit block
3695 : and so not come across fake pred edges. */
3696 2131750 : gcc_assert (!(pred->flags & EDGE_FAKE));
3697 2131750 : bprime = pred->src;
3698 4263500 : eprime = phi_translate (NULL, expr, ANTIC_IN (block),
3699 2131750 : PA_IN (block), pred);
3700 :
3701 : /* eprime will generally only be NULL if the
3702 : value of the expression, translated
3703 : through the PHI for this predecessor, is
3704 : undefined. If that is the case, we can't
3705 : make the expression fully redundant,
3706 : because its value is undefined along a
3707 : predecessor path. We can thus break out
3708 : early because it doesn't matter what the
3709 : rest of the results are. */
3710 2131750 : if (eprime == NULL)
3711 : {
3712 20 : avail[pred->dest_idx] = NULL;
3713 20 : cant_insert = true;
3714 20 : break;
3715 : }
3716 :
3717 2131730 : vprime = get_expr_value_id (eprime);
3718 2131730 : edoubleprime = bitmap_find_leader (AVAIL_OUT (bprime), vprime);
3719 2131730 : avail[pred->dest_idx] = edoubleprime;
3720 2131730 : if (edoubleprime == NULL)
3721 : {
3722 : by_all = false;
3723 : break;
3724 : }
3725 : }
3726 :
3727 : /* If we can insert it, it's not the same value
3728 : already existing along every predecessor, and
3729 : it's defined by some predecessor, it is
3730 : partially redundant. */
3731 2066151 : if (!cant_insert && by_all)
3732 : {
3733 10263 : edge succ;
3734 10263 : bool do_insertion = false;
3735 :
3736 : /* Insert only if we can remove a later expression on a path
3737 : that we want to optimize for speed.
3738 : The phi node that we will be inserting in BLOCK is not free,
3739 : and inserting it for the sake of !optimize_for_speed successor
3740 : may cause regressions on the speed path. */
3741 28483 : FOR_EACH_EDGE (succ, ei, block->succs)
3742 : {
3743 18220 : if (bitmap_set_contains_value (PA_IN (succ->dest), val)
3744 18220 : || bitmap_set_contains_value (ANTIC_IN (succ->dest), val))
3745 : {
3746 9616 : if (optimize_edge_for_speed_p (succ))
3747 18220 : do_insertion = true;
3748 : }
3749 : }
3750 :
3751 10263 : if (!do_insertion)
3752 : {
3753 3760 : if (dump_file && (dump_flags & TDF_DETAILS))
3754 : {
3755 0 : fprintf (dump_file, "Skipping partial partial redundancy "
3756 : "for expression ");
3757 0 : print_pre_expr (dump_file, expr);
3758 0 : fprintf (dump_file, " (%04d), not (partially) anticipated "
3759 : "on any to be optimized for speed edges\n", val);
3760 : }
3761 : }
3762 6503 : else if (dbg_cnt (treepre_insert))
3763 : {
3764 6503 : pre_stats.pa_insert++;
3765 6503 : if (dump_file && (dump_flags & TDF_DETAILS))
3766 : {
3767 0 : fprintf (dump_file, "Found partial partial redundancy "
3768 : "for expression ");
3769 0 : print_pre_expr (dump_file, expr);
3770 0 : fprintf (dump_file, " (%04d)\n",
3771 : get_expr_value_id (expr));
3772 : }
3773 6503 : if (insert_into_preds_of_block (block,
3774 : get_expression_id (expr),
3775 : avail))
3776 10263 : new_stuff = true;
3777 : }
3778 : }
3779 : }
3780 : }
3781 :
3782 329274 : return new_stuff;
3783 329274 : }
3784 :
3785 : /* Insert expressions in BLOCK to compute hoistable values up.
3786 : Return TRUE if something was inserted, otherwise return FALSE.
3787 : The caller has to make sure that BLOCK has at least two successors. */
3788 :
3789 : static bool
3790 4637376 : do_hoist_insertion (basic_block block)
3791 : {
3792 4637376 : edge e;
3793 4637376 : edge_iterator ei;
3794 4637376 : bool new_stuff = false;
3795 4637376 : unsigned i;
3796 4637376 : gimple_stmt_iterator last;
3797 :
3798 : /* At least two successors, or else... */
3799 4637376 : gcc_assert (EDGE_COUNT (block->succs) >= 2);
3800 :
3801 : /* Check that all successors of BLOCK are dominated by block.
3802 : We could use dominated_by_p() for this, but actually there is a much
3803 : quicker check: any successor that is dominated by BLOCK can't have
3804 : more than one predecessor edge. */
3805 13997515 : FOR_EACH_EDGE (e, ei, block->succs)
3806 13860377 : if (! single_pred_p (e->dest))
3807 : return false;
3808 :
3809 : /* Determine the insertion point. If we cannot safely insert before
3810 : the last stmt if we'd have to, bail out. */
3811 4629932 : last = gsi_last_bb (block);
3812 4629932 : if (!gsi_end_p (last)
3813 4629478 : && !is_ctrl_stmt (gsi_stmt (last))
3814 5183578 : && stmt_ends_bb_p (gsi_stmt (last)))
3815 : return false;
3816 :
3817 : /* We have multiple successors, compute ANTIC_OUT by taking the intersection
3818 : of all of ANTIC_IN translating through PHI nodes. Track the union
3819 : of the expression sets so we can pick a representative that is
3820 : fully generatable out of hoistable expressions. */
3821 4076871 : bitmap_set_t ANTIC_OUT = bitmap_set_new ();
3822 4076871 : bool first = true;
3823 12324526 : FOR_EACH_EDGE (e, ei, block->succs)
3824 : {
3825 8247655 : if (first)
3826 : {
3827 4076871 : phi_translate_set (ANTIC_OUT, ANTIC_IN (e->dest), e);
3828 4076871 : first = false;
3829 : }
3830 4170784 : else if (!gimple_seq_empty_p (phi_nodes (e->dest)))
3831 : {
3832 7 : bitmap_set_t tmp = bitmap_set_new ();
3833 7 : phi_translate_set (tmp, ANTIC_IN (e->dest), e);
3834 7 : bitmap_and_into (&ANTIC_OUT->values, &tmp->values);
3835 7 : bitmap_ior_into (&ANTIC_OUT->expressions, &tmp->expressions);
3836 7 : bitmap_set_free (tmp);
3837 : }
3838 : else
3839 : {
3840 4170777 : bitmap_and_into (&ANTIC_OUT->values, &ANTIC_IN (e->dest)->values);
3841 4170777 : bitmap_ior_into (&ANTIC_OUT->expressions,
3842 4170777 : &ANTIC_IN (e->dest)->expressions);
3843 : }
3844 : }
3845 :
3846 : /* Compute the set of hoistable expressions from ANTIC_OUT. First compute
3847 : hoistable values. */
3848 4076871 : bitmap_set hoistable_set;
3849 :
3850 : /* A hoistable value must be in ANTIC_OUT(block)
3851 : but not in AVAIL_OUT(BLOCK). */
3852 4076871 : bitmap_initialize (&hoistable_set.values, &grand_bitmap_obstack);
3853 4076871 : bitmap_and_compl (&hoistable_set.values,
3854 4076871 : &ANTIC_OUT->values, &AVAIL_OUT (block)->values);
3855 :
3856 : /* Short-cut for a common case: hoistable_set is empty. */
3857 4076871 : if (bitmap_empty_p (&hoistable_set.values))
3858 : {
3859 3341547 : bitmap_set_free (ANTIC_OUT);
3860 3341547 : return false;
3861 : }
3862 :
3863 : /* Compute which of the hoistable values is in AVAIL_OUT of
3864 : at least one of the successors of BLOCK. */
3865 735324 : bitmap_head availout_in_some;
3866 735324 : bitmap_initialize (&availout_in_some, &grand_bitmap_obstack);
3867 2219373 : FOR_EACH_EDGE (e, ei, block->succs)
3868 : /* Do not consider expressions solely because their availability
3869 : on loop exits. They'd be ANTIC-IN throughout the whole loop
3870 : and thus effectively hoisted across loops by combination of
3871 : PRE and hoisting. */
3872 1484049 : if (! loop_exit_edge_p (block->loop_father, e))
3873 1320836 : bitmap_ior_and_into (&availout_in_some, &hoistable_set.values,
3874 1320836 : &AVAIL_OUT (e->dest)->values);
3875 735324 : bitmap_clear (&hoistable_set.values);
3876 :
3877 : /* Short-cut for a common case: availout_in_some is empty. */
3878 735324 : if (bitmap_empty_p (&availout_in_some))
3879 : {
3880 598186 : bitmap_set_free (ANTIC_OUT);
3881 598186 : return false;
3882 : }
3883 :
3884 : /* Hack hoistable_set in-place so we can use sorted_array_from_bitmap_set. */
3885 137138 : bitmap_move (&hoistable_set.values, &availout_in_some);
3886 137138 : hoistable_set.expressions = ANTIC_OUT->expressions;
3887 :
3888 : /* Now finally construct the topological-ordered expression set. */
3889 137138 : vec<pre_expr> exprs = sorted_array_from_bitmap_set (&hoistable_set, true);
3890 :
3891 : /* If there are candidate values for hoisting, insert expressions
3892 : strategically to make the hoistable expressions fully redundant. */
3893 137138 : pre_expr expr;
3894 353304 : FOR_EACH_VEC_ELT (exprs, i, expr)
3895 : {
3896 : /* While we try to sort expressions topologically above the
3897 : sorting doesn't work out perfectly. Catch expressions we
3898 : already inserted. */
3899 216166 : unsigned int value_id = get_expr_value_id (expr);
3900 432332 : if (bitmap_set_contains_value (AVAIL_OUT (block), value_id))
3901 : {
3902 0 : if (dump_file && (dump_flags & TDF_DETAILS))
3903 : {
3904 0 : fprintf (dump_file,
3905 : "Already inserted expression for ");
3906 0 : print_pre_expr (dump_file, expr);
3907 0 : fprintf (dump_file, " (%04d)\n", value_id);
3908 : }
3909 28 : continue;
3910 : }
3911 :
3912 : /* If we end up with a punned expression representation and this
3913 : happens to be a float typed one give up - we can't know for
3914 : sure whether all paths perform the floating-point load we are
3915 : about to insert and on some targets this can cause correctness
3916 : issues. See PR88240. */
3917 216166 : if (expr->kind == REFERENCE
3918 95142 : && PRE_EXPR_REFERENCE (expr)->punned
3919 216166 : && FLOAT_TYPE_P (get_expr_type (expr)))
3920 0 : continue;
3921 :
3922 : /* Only hoist if the full expression is available for hoisting.
3923 : This avoids hoisting values that are not common and for
3924 : example evaluate an expression that's not valid to evaluate
3925 : unconditionally (PR112310). */
3926 216166 : if (!valid_in_sets (&hoistable_set, AVAIL_OUT (block), expr))
3927 9 : continue;
3928 :
3929 : /* OK, we should hoist this value. Perform the transformation. */
3930 216157 : pre_stats.hoist_insert++;
3931 216157 : if (dump_file && (dump_flags & TDF_DETAILS))
3932 : {
3933 2 : fprintf (dump_file,
3934 : "Inserting expression in block %d for code hoisting: ",
3935 : block->index);
3936 2 : print_pre_expr (dump_file, expr);
3937 2 : fprintf (dump_file, " (%04d)\n", value_id);
3938 : }
3939 :
3940 216157 : gimple_seq stmts = NULL;
3941 216157 : tree res = create_expression_by_pieces (block, expr, &stmts,
3942 : get_expr_type (expr));
3943 :
3944 : /* Do not return true if expression creation ultimately
3945 : did not insert any statements. */
3946 216157 : if (gimple_seq_empty_p (stmts))
3947 : res = NULL_TREE;
3948 : else
3949 : {
3950 216138 : if (gsi_end_p (last) || is_ctrl_stmt (gsi_stmt (last)))
3951 216138 : gsi_insert_seq_before (&last, stmts, GSI_SAME_STMT);
3952 : else
3953 0 : gsi_insert_seq_after (&last, stmts, GSI_NEW_STMT);
3954 : }
3955 :
3956 : /* Make sure to not return true if expression creation ultimately
3957 : failed but also make sure to insert any stmts produced as they
3958 : are tracked in inserted_exprs. */
3959 216138 : if (! res)
3960 19 : continue;
3961 :
3962 216138 : new_stuff = true;
3963 : }
3964 :
3965 137138 : exprs.release ();
3966 137138 : bitmap_clear (&hoistable_set.values);
3967 137138 : bitmap_set_free (ANTIC_OUT);
3968 :
3969 137138 : return new_stuff;
3970 : }
3971 :
3972 : /* Perform insertion of partially redundant and hoistable values. */
3973 :
3974 : static void
3975 961524 : insert (void)
3976 : {
3977 961524 : basic_block bb;
3978 :
3979 15811436 : FOR_ALL_BB_FN (bb, cfun)
3980 14849912 : NEW_SETS (bb) = bitmap_set_new ();
3981 :
3982 961524 : int *rpo = XNEWVEC (int, n_basic_blocks_for_fn (cfun));
3983 961524 : int *bb_rpo = XNEWVEC (int, last_basic_block_for_fn (cfun) + 1);
3984 961524 : int rpo_num = pre_and_rev_post_order_compute (NULL, rpo, false);
3985 13888388 : for (int i = 0; i < rpo_num; ++i)
3986 12926864 : bb_rpo[rpo[i]] = i;
3987 :
3988 : int num_iterations = 0;
3989 1013131 : bool changed;
3990 1013131 : do
3991 : {
3992 1013131 : num_iterations++;
3993 1013131 : if (dump_file && dump_flags & TDF_DETAILS)
3994 18 : fprintf (dump_file, "Starting insert iteration %d\n", num_iterations);
3995 :
3996 : changed = false;
3997 17998470 : for (int idx = 0; idx < rpo_num; ++idx)
3998 : {
3999 16985339 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
4000 16985339 : basic_block dom = get_immediate_dominator (CDI_DOMINATORS, block);
4001 16985339 : if (dom)
4002 : {
4003 16985339 : unsigned i;
4004 16985339 : bitmap_iterator bi;
4005 16985339 : bitmap_set_t newset;
4006 :
4007 : /* First, update the AVAIL_OUT set with anything we may have
4008 : inserted higher up in the dominator tree. */
4009 16985339 : newset = NEW_SETS (dom);
4010 :
4011 : /* Note that we need to value_replace both NEW_SETS, and
4012 : AVAIL_OUT. For both the case of NEW_SETS, the value may be
4013 : represented by some non-simple expression here that we want
4014 : to replace it with. */
4015 16985339 : bool avail_out_changed = false;
4016 32471166 : FOR_EACH_EXPR_ID_IN_SET (newset, i, bi)
4017 : {
4018 15485827 : pre_expr expr = expression_for_id (i);
4019 15485827 : bitmap_value_replace_in_set (NEW_SETS (block), expr);
4020 15485827 : avail_out_changed
4021 15485827 : |= bitmap_value_replace_in_set (AVAIL_OUT (block), expr);
4022 : }
4023 : /* We need to iterate if AVAIL_OUT of an already processed
4024 : block source changed. */
4025 16985339 : if (avail_out_changed && !changed)
4026 : {
4027 1656205 : edge_iterator ei;
4028 1656205 : edge e;
4029 3938177 : FOR_EACH_EDGE (e, ei, block->succs)
4030 2281972 : if (e->dest->index != EXIT_BLOCK
4031 2179608 : && bb_rpo[e->dest->index] < idx)
4032 2281972 : changed = true;
4033 : }
4034 :
4035 : /* Insert expressions for partial redundancies. */
4036 33969862 : if (flag_tree_pre && !single_pred_p (block))
4037 : {
4038 3335005 : vec<pre_expr> exprs
4039 3335005 : = sorted_array_from_bitmap_set (ANTIC_IN (block), true);
4040 : /* Sorting is not perfect, iterate locally. */
4041 6935587 : while (do_pre_regular_insertion (block, dom, exprs))
4042 : ;
4043 3335005 : exprs.release ();
4044 3335005 : if (do_partial_partial)
4045 : {
4046 326197 : exprs = sorted_array_from_bitmap_set (PA_IN (block),
4047 : true);
4048 655471 : while (do_pre_partial_partial_insertion (block, dom,
4049 : exprs))
4050 : ;
4051 326197 : exprs.release ();
4052 : }
4053 : }
4054 : }
4055 : }
4056 :
4057 : /* Clear the NEW sets before the next iteration. We have already
4058 : fully propagated its contents. */
4059 1013131 : if (changed)
4060 4213296 : FOR_ALL_BB_FN (bb, cfun)
4061 8323378 : bitmap_set_free (NEW_SETS (bb));
4062 : }
4063 : while (changed);
4064 :
4065 961524 : statistics_histogram_event (cfun, "insert iterations", num_iterations);
4066 :
4067 : /* AVAIL_OUT is not needed after insertion so we don't have to
4068 : propagate NEW_SETS from hoist insertion. */
4069 15811436 : FOR_ALL_BB_FN (bb, cfun)
4070 : {
4071 14849912 : bitmap_set_free (NEW_SETS (bb));
4072 14849912 : bitmap_set_pool.remove (NEW_SETS (bb));
4073 14849912 : NEW_SETS (bb) = NULL;
4074 : }
4075 :
4076 : /* Insert expressions for hoisting. Do a backward walk here since
4077 : inserting into BLOCK exposes new opportunities in its predecessors.
4078 : Since PRE and hoist insertions can cause back-to-back iteration
4079 : and we are interested in PRE insertion exposed hoisting opportunities
4080 : but not in hoisting exposed PRE ones do hoist insertion only after
4081 : PRE insertion iteration finished and do not iterate it. */
4082 961524 : if (flag_code_hoisting)
4083 13887839 : for (int idx = rpo_num - 1; idx >= 0; --idx)
4084 : {
4085 12926368 : basic_block block = BASIC_BLOCK_FOR_FN (cfun, rpo[idx]);
4086 17563744 : if (EDGE_COUNT (block->succs) >= 2)
4087 4637376 : changed |= do_hoist_insertion (block);
4088 : }
4089 :
4090 961524 : free (rpo);
4091 961524 : free (bb_rpo);
4092 961524 : }
4093 :
4094 :
4095 : /* Compute the AVAIL set for all basic blocks.
4096 :
4097 : This function performs value numbering of the statements in each basic
4098 : block. The AVAIL sets are built from information we glean while doing
4099 : this value numbering, since the AVAIL sets contain only one entry per
4100 : value.
4101 :
4102 : AVAIL_IN[BLOCK] = AVAIL_OUT[dom(BLOCK)].
4103 : AVAIL_OUT[BLOCK] = AVAIL_IN[BLOCK] U PHI_GEN[BLOCK] U TMP_GEN[BLOCK]. */
4104 :
4105 : static void
4106 961524 : compute_avail (function *fun)
4107 : {
4108 :
4109 961524 : basic_block block, son;
4110 961524 : basic_block *worklist;
4111 961524 : size_t sp = 0;
4112 961524 : unsigned i;
4113 961524 : tree name;
4114 :
4115 : /* We pretend that default definitions are defined in the entry block.
4116 : This includes function arguments and the static chain decl. */
4117 46218203 : FOR_EACH_SSA_NAME (i, name, fun)
4118 : {
4119 33127561 : pre_expr e;
4120 33127561 : if (!SSA_NAME_IS_DEFAULT_DEF (name)
4121 2889926 : || has_zero_uses (name)
4122 35496696 : || virtual_operand_p (name))
4123 31719131 : continue;
4124 :
4125 1408430 : e = get_or_alloc_expr_for_name (name);
4126 1408430 : add_to_value (get_expr_value_id (e), e);
4127 1408430 : bitmap_insert_into_set (TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun)), e);
4128 1408430 : bitmap_value_insert_into_set (AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun)),
4129 : e);
4130 : }
4131 :
4132 961524 : if (dump_file && (dump_flags & TDF_DETAILS))
4133 : {
4134 14 : print_bitmap_set (dump_file, TMP_GEN (ENTRY_BLOCK_PTR_FOR_FN (fun)),
4135 : "tmp_gen", ENTRY_BLOCK);
4136 14 : print_bitmap_set (dump_file, AVAIL_OUT (ENTRY_BLOCK_PTR_FOR_FN (fun)),
4137 : "avail_out", ENTRY_BLOCK);
4138 : }
4139 :
4140 : /* Allocate the worklist. */
4141 961524 : worklist = XNEWVEC (basic_block, n_basic_blocks_for_fn (fun));
4142 :
4143 : /* Seed the algorithm by putting the dominator children of the entry
4144 : block on the worklist. */
4145 961524 : for (son = first_dom_son (CDI_DOMINATORS, ENTRY_BLOCK_PTR_FOR_FN (fun));
4146 1923048 : son;
4147 961524 : son = next_dom_son (CDI_DOMINATORS, son))
4148 961524 : worklist[sp++] = son;
4149 :
4150 1923048 : BB_LIVE_VOP_ON_EXIT (ENTRY_BLOCK_PTR_FOR_FN (fun))
4151 961524 : = ssa_default_def (fun, gimple_vop (fun));
4152 :
4153 : /* Loop until the worklist is empty. */
4154 13888388 : while (sp)
4155 : {
4156 12926864 : gimple *stmt;
4157 12926864 : basic_block dom;
4158 :
4159 : /* Pick a block from the worklist. */
4160 12926864 : block = worklist[--sp];
4161 12926864 : vn_context_bb = block;
4162 :
4163 : /* Initially, the set of available values in BLOCK is that of
4164 : its immediate dominator. */
4165 12926864 : dom = get_immediate_dominator (CDI_DOMINATORS, block);
4166 12926864 : if (dom)
4167 : {
4168 12926864 : bitmap_set_copy (AVAIL_OUT (block), AVAIL_OUT (dom));
4169 12926864 : BB_LIVE_VOP_ON_EXIT (block) = BB_LIVE_VOP_ON_EXIT (dom);
4170 : }
4171 :
4172 : /* Generate values for PHI nodes. */
4173 16668981 : for (gphi_iterator gsi = gsi_start_phis (block); !gsi_end_p (gsi);
4174 3742117 : gsi_next (&gsi))
4175 : {
4176 3742117 : tree result = gimple_phi_result (gsi.phi ());
4177 :
4178 : /* We have no need for virtual phis, as they don't represent
4179 : actual computations. */
4180 7484234 : if (virtual_operand_p (result))
4181 : {
4182 1707778 : BB_LIVE_VOP_ON_EXIT (block) = result;
4183 1707778 : continue;
4184 : }
4185 :
4186 2034339 : pre_expr e = get_or_alloc_expr_for_name (result);
4187 2034339 : add_to_value (get_expr_value_id (e), e);
4188 2034339 : bitmap_value_insert_into_set (AVAIL_OUT (block), e);
4189 2034339 : bitmap_insert_into_set (PHI_GEN (block), e);
4190 : }
4191 :
4192 12926864 : BB_MAY_NOTRETURN (block) = 0;
4193 :
4194 : /* Now compute value numbers and populate value sets with all
4195 : the expressions computed in BLOCK. */
4196 12926864 : bool set_bb_may_notreturn = false;
4197 105106898 : for (gimple_stmt_iterator gsi = gsi_start_bb (block); !gsi_end_p (gsi);
4198 79253170 : gsi_next (&gsi))
4199 : {
4200 79253170 : ssa_op_iter iter;
4201 79253170 : tree op;
4202 :
4203 79253170 : stmt = gsi_stmt (gsi);
4204 :
4205 79253170 : if (set_bb_may_notreturn)
4206 : {
4207 2675874 : BB_MAY_NOTRETURN (block) = 1;
4208 2675874 : set_bb_may_notreturn = false;
4209 : }
4210 :
4211 : /* Cache whether the basic-block has any non-visible side-effect
4212 : or control flow.
4213 : If this isn't a call or it is the last stmt in the
4214 : basic-block then the CFG represents things correctly. */
4215 79253170 : if (is_gimple_call (stmt) && !stmt_ends_bb_p (stmt))
4216 : {
4217 : /* Non-looping const functions always return normally.
4218 : Otherwise the call might not return or have side-effects
4219 : that forbids hoisting possibly trapping expressions
4220 : before it. */
4221 3782494 : int flags = gimple_call_flags (stmt);
4222 3782494 : if (!(flags & (ECF_CONST|ECF_PURE))
4223 579317 : || (flags & ECF_LOOPING_CONST_OR_PURE)
4224 4334952 : || stmt_can_throw_external (fun, stmt))
4225 : /* Defer setting of BB_MAY_NOTRETURN to avoid it
4226 : influencing the processing of the call itself. */
4227 : set_bb_may_notreturn = true;
4228 : }
4229 :
4230 93967600 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_DEF)
4231 : {
4232 14714430 : pre_expr e = get_or_alloc_expr_for_name (op);
4233 14714430 : add_to_value (get_expr_value_id (e), e);
4234 14714430 : bitmap_insert_into_set (TMP_GEN (block), e);
4235 14714430 : bitmap_value_insert_into_set (AVAIL_OUT (block), e);
4236 : }
4237 :
4238 105724982 : if (gimple_vdef (stmt))
4239 11691768 : BB_LIVE_VOP_ON_EXIT (block) = gimple_vdef (stmt);
4240 :
4241 79253170 : if (gimple_has_side_effects (stmt)
4242 73143175 : || stmt_could_throw_p (fun, stmt)
4243 151248780 : || is_gimple_debug (stmt))
4244 74131176 : continue;
4245 :
4246 46347291 : FOR_EACH_SSA_TREE_OPERAND (op, stmt, iter, SSA_OP_USE)
4247 : {
4248 21877656 : if (ssa_undefined_value_p (op))
4249 52877 : continue;
4250 21824779 : pre_expr e = get_or_alloc_expr_for_name (op);
4251 21824779 : bitmap_value_insert_into_set (EXP_GEN (block), e);
4252 : }
4253 :
4254 24469635 : switch (gimple_code (stmt))
4255 : {
4256 936828 : case GIMPLE_RETURN:
4257 936828 : continue;
4258 :
4259 550192 : case GIMPLE_CALL:
4260 550192 : {
4261 550192 : vn_reference_t ref;
4262 550192 : vn_reference_s ref1;
4263 550192 : pre_expr result = NULL;
4264 :
4265 550192 : vn_reference_lookup_call (as_a <gcall *> (stmt), &ref, &ref1);
4266 : /* There is no point to PRE a call without a value. */
4267 550192 : if (!ref || !ref->result)
4268 25160 : continue;
4269 :
4270 : /* If the value of the call is not invalidated in
4271 : this block until it is computed, add the expression
4272 : to EXP_GEN. */
4273 525032 : if ((!gimple_vuse (stmt)
4274 294245 : || gimple_code
4275 294245 : (SSA_NAME_DEF_STMT (gimple_vuse (stmt))) == GIMPLE_PHI
4276 270092 : || gimple_bb (SSA_NAME_DEF_STMT
4277 : (gimple_vuse (stmt))) != block)
4278 : /* If the REFERENCE traps and there was a preceding
4279 : point in the block that might not return avoid
4280 : adding the reference to EXP_GEN. */
4281 765238 : && (!BB_MAY_NOTRETURN (block)
4282 10239 : || !vn_reference_may_trap (ref)))
4283 : {
4284 460754 : result = get_or_alloc_expr_for_reference
4285 460754 : (ref, ref->value_id, gimple_location (stmt));
4286 460754 : add_to_value (get_expr_value_id (result), result);
4287 460754 : bitmap_value_insert_into_set (EXP_GEN (block), result);
4288 : }
4289 525032 : continue;
4290 525032 : }
4291 :
4292 17860621 : case GIMPLE_ASSIGN:
4293 17860621 : {
4294 17860621 : pre_expr result = NULL;
4295 17860621 : switch (vn_get_stmt_kind (stmt))
4296 : {
4297 7462410 : case VN_NARY:
4298 7462410 : {
4299 7462410 : enum tree_code code = gimple_assign_rhs_code (stmt);
4300 7462410 : vn_nary_op_t nary;
4301 :
4302 : /* COND_EXPR is awkward in that it contains an
4303 : embedded complex expression.
4304 : Don't even try to shove it through PRE. */
4305 7462410 : if (code == COND_EXPR)
4306 139165 : continue;
4307 :
4308 7458373 : vn_nary_op_lookup_stmt (stmt, &nary);
4309 7458373 : if (!nary || nary->predicated_values)
4310 106221 : continue;
4311 :
4312 7352152 : unsigned value_id = nary->value_id;
4313 7352152 : if (value_id_constant_p (value_id))
4314 0 : continue;
4315 :
4316 : /* Record the un-valueized expression for EXP_GEN. */
4317 7352152 : nary = XALLOCAVAR (struct vn_nary_op_s,
4318 : sizeof_vn_nary_op
4319 : (vn_nary_length_from_stmt (stmt)));
4320 7352152 : init_vn_nary_op_from_stmt (nary, as_a <gassign *> (stmt));
4321 :
4322 : /* If the NARY traps and there was a preceding
4323 : point in the block that might not return avoid
4324 : adding the nary to EXP_GEN. */
4325 7381059 : if (BB_MAY_NOTRETURN (block)
4326 7352152 : && vn_nary_may_trap (nary))
4327 28907 : continue;
4328 :
4329 7323245 : result = get_or_alloc_expr_for_nary
4330 7323245 : (nary, value_id, gimple_location (stmt));
4331 7323245 : break;
4332 : }
4333 :
4334 5003380 : case VN_REFERENCE:
4335 5003380 : {
4336 5003380 : tree rhs1 = gimple_assign_rhs1 (stmt);
4337 : /* There is no point in trying to handle aggregates,
4338 : even when via punning we might get a value number
4339 : corresponding to a register typed load. */
4340 5003380 : if (!is_gimple_reg_type (TREE_TYPE (rhs1)))
4341 1445988 : continue;
4342 4640189 : ao_ref rhs1_ref;
4343 4640189 : ao_ref_init (&rhs1_ref, rhs1);
4344 4640189 : alias_set_type set = ao_ref_alias_set (&rhs1_ref);
4345 4640189 : alias_set_type base_set
4346 4640189 : = ao_ref_base_alias_set (&rhs1_ref);
4347 4640189 : vec<vn_reference_op_s> operands
4348 4640189 : = vn_reference_operands_for_lookup (rhs1);
4349 4640189 : vn_reference_t ref;
4350 :
4351 : /* We handle &MEM[ptr + 5].b[1].c as
4352 : POINTER_PLUS_EXPR. */
4353 4640189 : if (operands[0].opcode == ADDR_EXPR
4354 4890730 : && operands.last ().opcode == SSA_NAME)
4355 : {
4356 250529 : tree ops[2];
4357 250529 : if (vn_pp_nary_for_addr (operands, ops))
4358 : {
4359 165748 : vn_nary_op_t nary;
4360 165748 : vn_nary_op_lookup_pieces (2, POINTER_PLUS_EXPR,
4361 165748 : TREE_TYPE (rhs1), ops,
4362 : &nary);
4363 165748 : operands.release ();
4364 165748 : if (nary && !nary->predicated_values)
4365 : {
4366 165736 : unsigned value_id = nary->value_id;
4367 165736 : if (value_id_constant_p (value_id))
4368 12 : continue;
4369 165736 : result = get_or_alloc_expr_for_nary
4370 165736 : (nary, value_id, gimple_location (stmt));
4371 165736 : break;
4372 : }
4373 12 : continue;
4374 12 : }
4375 : }
4376 :
4377 8948882 : vn_reference_lookup_pieces (gimple_vuse (stmt), set,
4378 4474441 : base_set, TREE_TYPE (rhs1),
4379 : operands, &ref, VN_WALK);
4380 : /* When there is no value recorded or the value was
4381 : recorded for a different type, fail, similar as
4382 : how we do during PHI translation. */
4383 4477375 : if (!ref
4384 4474441 : || !useless_type_conversion_p (TREE_TYPE (rhs1),
4385 : ref->type))
4386 : {
4387 2934 : operands.release ();
4388 2934 : continue;
4389 : }
4390 4471507 : operands.release ();
4391 :
4392 : /* If the REFERENCE traps and there was a preceding
4393 : point in the block that might not return avoid
4394 : adding the reference to EXP_GEN. */
4395 4624363 : if (BB_MAY_NOTRETURN (block)
4396 4471507 : && gimple_could_trap_p_1 (stmt, true, false))
4397 152856 : continue;
4398 :
4399 : /* If the value of the reference is not invalidated in
4400 : this block until it is computed, add the expression
4401 : to EXP_GEN. */
4402 8637302 : if (gimple_vuse (stmt))
4403 : {
4404 4233875 : gimple *def_stmt;
4405 4233875 : bool ok = true;
4406 4233875 : def_stmt = SSA_NAME_DEF_STMT (gimple_vuse (stmt));
4407 6844086 : while (!gimple_nop_p (def_stmt)
4408 5876975 : && gimple_code (def_stmt) != GIMPLE_PHI
4409 11554089 : && gimple_bb (def_stmt) == block)
4410 : {
4411 3537206 : if (stmt_may_clobber_ref_p
4412 3537206 : (def_stmt, gimple_assign_rhs1 (stmt)))
4413 : {
4414 : ok = false;
4415 : break;
4416 : }
4417 2610211 : def_stmt
4418 2610211 : = SSA_NAME_DEF_STMT (gimple_vuse (def_stmt));
4419 : }
4420 4233875 : if (!ok)
4421 926995 : continue;
4422 : }
4423 :
4424 : /* Record the un-valueized expression for EXP_GEN. */
4425 3391656 : copy_reference_ops_from_ref (rhs1, &operands);
4426 3391656 : vn_reference_t newref
4427 3391656 : = XALLOCAVAR (struct vn_reference_s,
4428 : sizeof (vn_reference_s));
4429 3391656 : memset (newref, 0, sizeof (vn_reference_s));
4430 3391656 : newref->value_id = ref->value_id;
4431 3391656 : newref->vuse = ref->vuse;
4432 3391656 : newref->operands = operands;
4433 3391656 : newref->type = TREE_TYPE (rhs1);
4434 3391656 : newref->set = set;
4435 3391656 : newref->base_set = base_set;
4436 3391656 : newref->offset = 0;
4437 3391656 : newref->max_size = -1;
4438 3391656 : newref->result = ref->result;
4439 3391656 : newref->hashcode = vn_reference_compute_hash (newref);
4440 :
4441 3391656 : result = get_or_alloc_expr_for_reference
4442 3391656 : (newref, newref->value_id,
4443 : gimple_location (stmt), true);
4444 3391656 : break;
4445 : }
4446 :
4447 5394831 : default:
4448 5394831 : continue;
4449 5394831 : }
4450 :
4451 10880637 : add_to_value (get_expr_value_id (result), result);
4452 10880637 : bitmap_value_insert_into_set (EXP_GEN (block), result);
4453 10880637 : continue;
4454 10880637 : }
4455 5121994 : default:
4456 5121994 : break;
4457 936828 : }
4458 : }
4459 12926864 : if (set_bb_may_notreturn)
4460 : {
4461 556426 : BB_MAY_NOTRETURN (block) = 1;
4462 556426 : set_bb_may_notreturn = false;
4463 : }
4464 :
4465 12926864 : if (dump_file && (dump_flags & TDF_DETAILS))
4466 : {
4467 108 : print_bitmap_set (dump_file, EXP_GEN (block),
4468 : "exp_gen", block->index);
4469 108 : print_bitmap_set (dump_file, PHI_GEN (block),
4470 : "phi_gen", block->index);
4471 108 : print_bitmap_set (dump_file, TMP_GEN (block),
4472 : "tmp_gen", block->index);
4473 108 : print_bitmap_set (dump_file, AVAIL_OUT (block),
4474 : "avail_out", block->index);
4475 : }
4476 :
4477 : /* Put the dominator children of BLOCK on the worklist of blocks
4478 : to compute available sets for. */
4479 12926864 : for (son = first_dom_son (CDI_DOMINATORS, block);
4480 24892204 : son;
4481 11965340 : son = next_dom_son (CDI_DOMINATORS, son))
4482 11965340 : worklist[sp++] = son;
4483 : }
4484 961524 : vn_context_bb = NULL;
4485 :
4486 961524 : free (worklist);
4487 961524 : }
4488 :
4489 :
4490 : /* Initialize data structures used by PRE. */
4491 :
4492 : static void
4493 961530 : init_pre (void)
4494 : {
4495 961530 : basic_block bb;
4496 :
4497 961530 : next_expression_id = 1;
4498 961530 : expressions.create (0);
4499 961530 : expressions.safe_push (NULL);
4500 961530 : value_expressions.create (get_max_value_id () + 1);
4501 961530 : value_expressions.quick_grow_cleared (get_max_value_id () + 1);
4502 961530 : constant_value_expressions.create (get_max_constant_value_id () + 1);
4503 961530 : constant_value_expressions.quick_grow_cleared (get_max_constant_value_id () + 1);
4504 961530 : name_to_id.create (0);
4505 961530 : gcc_obstack_init (&pre_expr_obstack);
4506 :
4507 961530 : inserted_exprs = BITMAP_ALLOC (NULL);
4508 :
4509 961530 : connect_infinite_loops_to_exit ();
4510 961530 : memset (&pre_stats, 0, sizeof (pre_stats));
4511 :
4512 961530 : alloc_aux_for_blocks (sizeof (struct bb_bitmap_sets));
4513 :
4514 961530 : calculate_dominance_info (CDI_DOMINATORS);
4515 :
4516 961530 : bitmap_obstack_initialize (&grand_bitmap_obstack);
4517 1923060 : expression_to_id = new hash_table<pre_expr_d> (num_ssa_names * 3);
4518 15844312 : FOR_ALL_BB_FN (bb, cfun)
4519 : {
4520 14882782 : EXP_GEN (bb) = bitmap_set_new ();
4521 14882782 : PHI_GEN (bb) = bitmap_set_new ();
4522 14882782 : TMP_GEN (bb) = bitmap_set_new ();
4523 14882782 : AVAIL_OUT (bb) = bitmap_set_new ();
4524 14882782 : PHI_TRANS_TABLE (bb) = NULL;
4525 : }
4526 961530 : }
4527 :
4528 :
4529 : /* Deallocate data structures used by PRE. */
4530 :
4531 : static void
4532 961530 : fini_pre ()
4533 : {
4534 961530 : value_expressions.release ();
4535 961530 : constant_value_expressions.release ();
4536 43795593 : for (unsigned i = 1; i < expressions.length (); ++i)
4537 42834063 : if (expressions[i]->kind == REFERENCE)
4538 6244024 : PRE_EXPR_REFERENCE (expressions[i])->operands.release ();
4539 961530 : expressions.release ();
4540 961530 : bitmap_obstack_release (&grand_bitmap_obstack);
4541 961530 : bitmap_set_pool.release ();
4542 961530 : pre_expr_pool.release ();
4543 961530 : delete expression_to_id;
4544 961530 : expression_to_id = NULL;
4545 961530 : name_to_id.release ();
4546 961530 : obstack_free (&pre_expr_obstack, NULL);
4547 :
4548 961530 : basic_block bb;
4549 15844018 : FOR_ALL_BB_FN (bb, cfun)
4550 14882488 : if (bb->aux && PHI_TRANS_TABLE (bb))
4551 5983885 : delete PHI_TRANS_TABLE (bb);
4552 961530 : free_aux_for_blocks ();
4553 961530 : }
4554 :
4555 : namespace {
4556 :
4557 : const pass_data pass_data_pre =
4558 : {
4559 : GIMPLE_PASS, /* type */
4560 : "pre", /* name */
4561 : OPTGROUP_NONE, /* optinfo_flags */
4562 : TV_TREE_PRE, /* tv_id */
4563 : ( PROP_cfg | PROP_ssa ), /* properties_required */
4564 : 0, /* properties_provided */
4565 : 0, /* properties_destroyed */
4566 : TODO_rebuild_alias, /* todo_flags_start */
4567 : 0, /* todo_flags_finish */
4568 : };
4569 :
4570 : class pass_pre : public gimple_opt_pass
4571 : {
4572 : public:
4573 298828 : pass_pre (gcc::context *ctxt)
4574 597656 : : gimple_opt_pass (pass_data_pre, ctxt)
4575 : {}
4576 :
4577 : /* opt_pass methods: */
4578 1039819 : bool gate (function *) final override
4579 1039819 : { return flag_tree_pre != 0 || flag_code_hoisting != 0; }
4580 : unsigned int execute (function *) final override;
4581 :
4582 : }; // class pass_pre
4583 :
4584 : /* Valueization hook for RPO VN when we are calling back to it
4585 : at ANTIC compute time. */
4586 :
4587 : static tree
4588 111883847 : pre_valueize (tree name)
4589 : {
4590 111883847 : if (TREE_CODE (name) == SSA_NAME)
4591 : {
4592 111622784 : tree tem = VN_INFO (name)->valnum;
4593 111622784 : if (tem != VN_TOP && tem != name)
4594 : {
4595 15382236 : if (TREE_CODE (tem) != SSA_NAME
4596 15382236 : || SSA_NAME_IS_DEFAULT_DEF (tem))
4597 : return tem;
4598 : /* We create temporary SSA names for representatives that
4599 : do not have a definition (yet) but are not default defs either
4600 : assume they are fine to use. */
4601 15376984 : basic_block def_bb = gimple_bb (SSA_NAME_DEF_STMT (tem));
4602 15376984 : if (! def_bb
4603 15376984 : || dominated_by_p (CDI_DOMINATORS, vn_context_bb, def_bb))
4604 126431 : return tem;
4605 : /* ??? Now we could look for a leader. Ideally we'd somehow
4606 : expose RPO VN leaders and get rid of AVAIL_OUT as well... */
4607 : }
4608 : }
4609 : return name;
4610 : }
4611 :
4612 : unsigned int
4613 961530 : pass_pre::execute (function *fun)
4614 : {
4615 961530 : unsigned int todo = 0;
4616 :
4617 1923060 : do_partial_partial =
4618 961530 : flag_tree_partial_pre && optimize_function_for_speed_p (fun);
4619 :
4620 : /* This has to happen before VN runs because
4621 : loop_optimizer_init may create new phis, etc. */
4622 961530 : loop_optimizer_init (LOOPS_NORMAL);
4623 961530 : split_edges_for_insertion ();
4624 961530 : scev_initialize ();
4625 961530 : calculate_dominance_info (CDI_DOMINATORS);
4626 :
4627 961530 : run_rpo_vn (VN_WALK);
4628 :
4629 961530 : init_pre ();
4630 :
4631 961530 : vn_valueize = pre_valueize;
4632 :
4633 : /* Insert can get quite slow on an incredibly large number of basic
4634 : blocks due to some quadratic behavior. Until this behavior is
4635 : fixed, don't run it when he have an incredibly large number of
4636 : bb's. If we aren't going to run insert, there is no point in
4637 : computing ANTIC, either, even though it's plenty fast nor do
4638 : we require AVAIL. */
4639 961530 : if (n_basic_blocks_for_fn (fun) < 4000)
4640 : {
4641 961524 : compute_avail (fun);
4642 961524 : compute_antic ();
4643 961524 : insert ();
4644 : }
4645 :
4646 : /* Make sure to remove fake edges before committing our inserts.
4647 : This makes sure we don't end up with extra critical edges that
4648 : we would need to split. */
4649 961530 : remove_fake_exit_edges ();
4650 961530 : gsi_commit_edge_inserts ();
4651 :
4652 : /* Eliminate folds statements which might (should not...) end up
4653 : not keeping virtual operands up-to-date. */
4654 961530 : gcc_assert (!need_ssa_update_p (fun));
4655 :
4656 961530 : statistics_counter_event (fun, "Insertions", pre_stats.insertions);
4657 961530 : statistics_counter_event (fun, "PA inserted", pre_stats.pa_insert);
4658 961530 : statistics_counter_event (fun, "HOIST inserted", pre_stats.hoist_insert);
4659 961530 : statistics_counter_event (fun, "New PHIs", pre_stats.phis);
4660 :
4661 961530 : todo |= eliminate_with_rpo_vn (inserted_exprs);
4662 :
4663 961530 : vn_valueize = NULL;
4664 :
4665 961530 : fini_pre ();
4666 :
4667 961530 : scev_finalize ();
4668 961530 : loop_optimizer_finalize ();
4669 :
4670 : /* Perform a CFG cleanup before we run simple_dce_from_worklist since
4671 : unreachable code regions will have not up-to-date SSA form which
4672 : confuses it. */
4673 961530 : bool need_crit_edge_split = false;
4674 961530 : if (todo & TODO_cleanup_cfg)
4675 : {
4676 136880 : cleanup_tree_cfg ();
4677 136880 : need_crit_edge_split = true;
4678 : }
4679 :
4680 : /* Because we don't follow exactly the standard PRE algorithm, and decide not
4681 : to insert PHI nodes sometimes, and because value numbering of casts isn't
4682 : perfect, we sometimes end up inserting dead code. This simple DCE-like
4683 : pass removes any insertions we made that weren't actually used. */
4684 961530 : simple_dce_from_worklist (inserted_exprs);
4685 961530 : BITMAP_FREE (inserted_exprs);
4686 :
4687 : /* TODO: tail_merge_optimize may merge all predecessors of a block, in which
4688 : case we can merge the block with the remaining predecessor of the block.
4689 : It should either:
4690 : - call merge_blocks after each tail merge iteration
4691 : - call merge_blocks after all tail merge iterations
4692 : - mark TODO_cleanup_cfg when necessary. */
4693 961530 : todo |= tail_merge_optimize (need_crit_edge_split);
4694 :
4695 961530 : free_rpo_vn ();
4696 :
4697 : /* Tail merging invalidates the virtual SSA web, together with
4698 : cfg-cleanup opportunities exposed by PRE this will wreck the
4699 : SSA updating machinery. So make sure to run update-ssa
4700 : manually, before eventually scheduling cfg-cleanup as part of
4701 : the todo. */
4702 961530 : update_ssa (TODO_update_ssa_only_virtuals);
4703 :
4704 961530 : return todo;
4705 : }
4706 :
4707 : } // anon namespace
4708 :
4709 : gimple_opt_pass *
4710 298828 : make_pass_pre (gcc::context *ctxt)
4711 : {
4712 298828 : return new pass_pre (ctxt);
4713 : }
|