Line data Source code
1 : /* Forward propagation of expressions for single use variables.
2 : Copyright (C) 2004-2026 Free Software Foundation, Inc.
3 :
4 : This file is part of GCC.
5 :
6 : GCC is free software; you can redistribute it and/or modify
7 : it under the terms of the GNU General Public License as published by
8 : the Free Software Foundation; either version 3, or (at your option)
9 : any later version.
10 :
11 : GCC is distributed in the hope that it will be useful,
12 : but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 : GNU General Public License for more details.
15 :
16 : You should have received a copy of the GNU General Public License
17 : along with GCC; see the file COPYING3. If not see
18 : <http://www.gnu.org/licenses/>. */
19 :
20 : #include "config.h"
21 : #include "system.h"
22 : #include "coretypes.h"
23 : #include "backend.h"
24 : #include "rtl.h"
25 : #include "tree.h"
26 : #include "gimple.h"
27 : #include "cfghooks.h"
28 : #include "tree-pass.h"
29 : #include "ssa.h"
30 : #include "expmed.h"
31 : #include "optabs-query.h"
32 : #include "gimple-pretty-print.h"
33 : #include "fold-const.h"
34 : #include "stor-layout.h"
35 : #include "gimple-iterator.h"
36 : #include "gimple-fold.h"
37 : #include "tree-eh.h"
38 : #include "gimplify.h"
39 : #include "gimplify-me.h"
40 : #include "tree-cfg.h"
41 : #include "expr.h"
42 : #include "tree-dfa.h"
43 : #include "tree-ssa-propagate.h"
44 : #include "tree-ssa-dom.h"
45 : #include "tree-ssa-strlen.h"
46 : #include "builtins.h"
47 : #include "tree-cfgcleanup.h"
48 : #include "cfganal.h"
49 : #include "optabs-tree.h"
50 : #include "insn-config.h"
51 : #include "recog.h"
52 : #include "cfgloop.h"
53 : #include "tree-vectorizer.h"
54 : #include "tree-vector-builder.h"
55 : #include "vec-perm-indices.h"
56 : #include "internal-fn.h"
57 : #include "cgraph.h"
58 : #include "tree-ssa.h"
59 : #include "gimple-range.h"
60 : #include "tree-ssa-dce.h"
61 :
62 : /* This pass propagates the RHS of assignment statements into use
63 : sites of the LHS of the assignment. It's basically a specialized
64 : form of tree combination. It is hoped all of this can disappear
65 : when we have a generalized tree combiner.
66 :
67 : One class of common cases we handle is forward propagating a single use
68 : variable into a COND_EXPR.
69 :
70 : bb0:
71 : x = a COND b;
72 : if (x) goto ... else goto ...
73 :
74 : Will be transformed into:
75 :
76 : bb0:
77 : if (a COND b) goto ... else goto ...
78 :
79 : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
80 :
81 : Or (assuming c1 and c2 are constants):
82 :
83 : bb0:
84 : x = a + c1;
85 : if (x EQ/NEQ c2) goto ... else goto ...
86 :
87 : Will be transformed into:
88 :
89 : bb0:
90 : if (a EQ/NEQ (c2 - c1)) goto ... else goto ...
91 :
92 : Similarly for x = a - c1.
93 :
94 : Or
95 :
96 : bb0:
97 : x = !a
98 : if (x) goto ... else goto ...
99 :
100 : Will be transformed into:
101 :
102 : bb0:
103 : if (a == 0) goto ... else goto ...
104 :
105 : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
106 : For these cases, we propagate A into all, possibly more than one,
107 : COND_EXPRs that use X.
108 :
109 : Or
110 :
111 : bb0:
112 : x = (typecast) a
113 : if (x) goto ... else goto ...
114 :
115 : Will be transformed into:
116 :
117 : bb0:
118 : if (a != 0) goto ... else goto ...
119 :
120 : (Assuming a is an integral type and x is a boolean or x is an
121 : integral and a is a boolean.)
122 :
123 : Similarly for the tests (x == 0), (x != 0), (x == 1) and (x != 1).
124 : For these cases, we propagate A into all, possibly more than one,
125 : COND_EXPRs that use X.
126 :
127 : In addition to eliminating the variable and the statement which assigns
128 : a value to the variable, we may be able to later thread the jump without
129 : adding insane complexity in the dominator optimizer.
130 :
131 : Also note these transformations can cascade. We handle this by having
132 : a worklist of COND_EXPR statements to examine. As we make a change to
133 : a statement, we put it back on the worklist to examine on the next
134 : iteration of the main loop.
135 :
136 : A second class of propagation opportunities arises for ADDR_EXPR
137 : nodes.
138 :
139 : ptr = &x->y->z;
140 : res = *ptr;
141 :
142 : Will get turned into
143 :
144 : res = x->y->z;
145 :
146 : Or
147 : ptr = (type1*)&type2var;
148 : res = *ptr
149 :
150 : Will get turned into (if type1 and type2 are the same size
151 : and neither have volatile on them):
152 : res = VIEW_CONVERT_EXPR<type1>(type2var)
153 :
154 : Or
155 :
156 : ptr = &x[0];
157 : ptr2 = ptr + <constant>;
158 :
159 : Will get turned into
160 :
161 : ptr2 = &x[constant/elementsize];
162 :
163 : Or
164 :
165 : ptr = &x[0];
166 : offset = index * element_size;
167 : offset_p = (pointer) offset;
168 : ptr2 = ptr + offset_p
169 :
170 : Will get turned into:
171 :
172 : ptr2 = &x[index];
173 :
174 : Or
175 : ssa = (int) decl
176 : res = ssa & 1
177 :
178 : Provided that decl has known alignment >= 2, will get turned into
179 :
180 : res = 0
181 :
182 : We also propagate casts into SWITCH_EXPR and COND_EXPR conditions to
183 : allow us to remove the cast and {NOT_EXPR,NEG_EXPR} into a subsequent
184 : {NOT_EXPR,NEG_EXPR}.
185 :
186 : This will (of course) be extended as other needs arise. */
187 :
188 : /* Data structure that contains simplifiable vectorized permute sequences.
189 : See recognise_vec_perm_simplify_seq () for a description of the sequence. */
190 :
191 : struct _vec_perm_simplify_seq
192 : {
193 : /* Defining stmts of vectors in the sequence. */
194 : gassign *v_1_stmt;
195 : gassign *v_2_stmt;
196 : gassign *v_x_stmt;
197 : gassign *v_y_stmt;
198 : /* Final permute statement. */
199 : gassign *stmt;
200 : /* New selector indices for stmt. */
201 : tree new_sel;
202 : /* Elements of each vector and selector. */
203 : unsigned int nelts;
204 : };
205 : typedef struct _vec_perm_simplify_seq *vec_perm_simplify_seq;
206 :
207 : static bool forward_propagate_addr_expr (tree, tree, bool);
208 :
209 : /* Set to true if we delete dead edges during the optimization. */
210 : static bool cfg_changed;
211 :
212 : static tree rhs_to_tree (tree type, gimple *stmt);
213 :
214 : static bitmap to_purge;
215 :
216 : /* Const-and-copy lattice. */
217 : static vec<tree> lattice;
218 :
219 : /* Set the lattice entry for NAME to VAL. */
220 : static void
221 31938485 : fwprop_set_lattice_val (tree name, tree val)
222 : {
223 31938485 : if (TREE_CODE (name) == SSA_NAME)
224 : {
225 31938485 : if (SSA_NAME_VERSION (name) >= lattice.length ())
226 : {
227 32175 : lattice.reserve (num_ssa_names - lattice.length ());
228 21450 : lattice.quick_grow_cleared (num_ssa_names);
229 : }
230 31938485 : lattice[SSA_NAME_VERSION (name)] = val;
231 : /* As this now constitutes a copy duplicate points-to
232 : and range info appropriately. */
233 31938485 : if (TREE_CODE (val) == SSA_NAME)
234 31498202 : maybe_duplicate_ssa_info_at_copy (name, val);
235 : }
236 31938485 : }
237 :
238 : /* Invalidate the lattice entry for NAME, done when releasing SSA names. */
239 : static void
240 895712 : fwprop_invalidate_lattice (tree name)
241 : {
242 895712 : if (name
243 893314 : && TREE_CODE (name) == SSA_NAME
244 1788899 : && SSA_NAME_VERSION (name) < lattice.length ())
245 893154 : lattice[SSA_NAME_VERSION (name)] = NULL_TREE;
246 895712 : }
247 :
248 : /* Get the statement we can propagate from into NAME skipping
249 : trivial copies. Returns the statement which defines the
250 : propagation source or NULL_TREE if there is no such one.
251 : If SINGLE_USE_ONLY is set considers only sources which have
252 : a single use chain up to NAME. If SINGLE_USE_P is non-null,
253 : it is set to whether the chain to NAME is a single use chain
254 : or not. SINGLE_USE_P is not written to if SINGLE_USE_ONLY is set. */
255 :
256 : static gimple *
257 27510765 : get_prop_source_stmt (tree name, bool single_use_only, bool *single_use_p)
258 : {
259 27510765 : bool single_use = true;
260 :
261 27511753 : do {
262 27511259 : gimple *def_stmt = SSA_NAME_DEF_STMT (name);
263 :
264 27511259 : if (!has_single_use (name))
265 : {
266 14965707 : single_use = false;
267 14965707 : if (single_use_only)
268 : return NULL;
269 : }
270 :
271 : /* If name is defined by a PHI node or is the default def, bail out. */
272 27509827 : if (!is_gimple_assign (def_stmt))
273 : return NULL;
274 :
275 : /* If def_stmt is a simple copy, continue looking. */
276 19362685 : if (gimple_assign_rhs_code (def_stmt) == SSA_NAME)
277 494 : name = gimple_assign_rhs1 (def_stmt);
278 : else
279 : {
280 19362191 : if (!single_use_only && single_use_p)
281 19059809 : *single_use_p = single_use;
282 :
283 19362191 : return def_stmt;
284 : }
285 494 : } while (1);
286 : }
287 :
288 : /* Checks if the destination ssa name in DEF_STMT can be used as
289 : propagation source. Returns true if so, otherwise false. */
290 :
291 : static bool
292 27277971 : can_propagate_from (gimple *def_stmt)
293 : {
294 27277971 : gcc_assert (is_gimple_assign (def_stmt));
295 :
296 : /* If the rhs has side-effects we cannot propagate from it. */
297 27277971 : if (gimple_has_volatile_ops (def_stmt))
298 : return false;
299 :
300 : /* If the rhs is a load we cannot propagate from it. */
301 26685736 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_reference
302 26685736 : || TREE_CODE_CLASS (gimple_assign_rhs_code (def_stmt)) == tcc_declaration)
303 : return false;
304 :
305 : /* Constants can be always propagated. */
306 13250929 : if (gimple_assign_single_p (def_stmt)
307 13250929 : && is_gimple_min_invariant (gimple_assign_rhs1 (def_stmt)))
308 : return true;
309 :
310 : /* We cannot propagate ssa names that occur in abnormal phi nodes. */
311 13250929 : if (stmt_references_abnormal_ssa_name (def_stmt))
312 : return false;
313 :
314 : /* If the definition is a conversion of a pointer to a function type,
315 : then we cannot apply optimizations as some targets require
316 : function pointers to be canonicalized and in this case this
317 : optimization could eliminate a necessary canonicalization. */
318 13250238 : if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (def_stmt)))
319 : {
320 3192917 : tree rhs = gimple_assign_rhs1 (def_stmt);
321 3192917 : if (FUNCTION_POINTER_TYPE_P (TREE_TYPE (rhs)))
322 : return false;
323 : }
324 :
325 : return true;
326 : }
327 :
328 : /* Remove a chain of dead statements starting at the definition of
329 : NAME. The chain is linked via the first operand of the defining statements.
330 : If NAME was replaced in its only use then this function can be used
331 : to clean up dead stmts. The function handles already released SSA
332 : names gracefully. */
333 :
334 : static void
335 235785 : remove_prop_source_from_use (tree name)
336 : {
337 295948 : gimple_stmt_iterator gsi;
338 295948 : gimple *stmt;
339 :
340 295948 : do {
341 295948 : basic_block bb;
342 :
343 295948 : if (SSA_NAME_IN_FREE_LIST (name)
344 295905 : || SSA_NAME_IS_DEFAULT_DEF (name)
345 589925 : || !has_zero_uses (name))
346 : break;
347 :
348 60622 : stmt = SSA_NAME_DEF_STMT (name);
349 60622 : if (gimple_code (stmt) == GIMPLE_PHI
350 60622 : || gimple_has_side_effects (stmt))
351 : break;
352 :
353 60622 : bb = gimple_bb (stmt);
354 60622 : gsi = gsi_for_stmt (stmt);
355 60622 : unlink_stmt_vdef (stmt);
356 60622 : if (gsi_remove (&gsi, true))
357 6 : bitmap_set_bit (to_purge, bb->index);
358 60622 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
359 60622 : release_defs (stmt);
360 :
361 60622 : name = is_gimple_assign (stmt) ? gimple_assign_rhs1 (stmt) : NULL_TREE;
362 60622 : } while (name && TREE_CODE (name) == SSA_NAME);
363 :
364 235785 : }
365 :
366 : /* Return the rhs of a gassign *STMT in a form of a single tree,
367 : converted to type TYPE.
368 :
369 : This should disappear, but is needed so we can combine expressions and use
370 : the fold() interfaces. Long term, we need to develop folding and combine
371 : routines that deal with gimple exclusively . */
372 :
373 : static tree
374 7209492 : rhs_to_tree (tree type, gimple *stmt)
375 : {
376 7209492 : location_t loc = gimple_location (stmt);
377 7209492 : enum tree_code code = gimple_assign_rhs_code (stmt);
378 7209492 : switch (get_gimple_rhs_class (code))
379 : {
380 12544 : case GIMPLE_TERNARY_RHS:
381 12544 : return fold_build3_loc (loc, code, type, gimple_assign_rhs1 (stmt),
382 : gimple_assign_rhs2 (stmt),
383 12544 : gimple_assign_rhs3 (stmt));
384 4947318 : case GIMPLE_BINARY_RHS:
385 4947318 : return fold_build2_loc (loc, code, type, gimple_assign_rhs1 (stmt),
386 4947318 : gimple_assign_rhs2 (stmt));
387 1995587 : case GIMPLE_UNARY_RHS:
388 1995587 : return build1 (code, type, gimple_assign_rhs1 (stmt));
389 254043 : case GIMPLE_SINGLE_RHS:
390 254043 : return gimple_assign_rhs1 (stmt);
391 0 : default:
392 0 : gcc_unreachable ();
393 : }
394 : }
395 :
396 : /* Combine OP0 CODE OP1 in the context of a COND_EXPR. Returns
397 : the folded result in a form suitable for COND_EXPR_COND or
398 : NULL_TREE, if there is no suitable simplified form. If
399 : INVARIANT_ONLY is true only gimple_min_invariant results are
400 : considered simplified. */
401 :
402 : static tree
403 8124347 : combine_cond_expr_cond (gimple *stmt, enum tree_code code, tree type,
404 : tree op0, tree op1, bool invariant_only)
405 : {
406 8124347 : tree t;
407 :
408 8124347 : gcc_assert (TREE_CODE_CLASS (code) == tcc_comparison);
409 :
410 8124347 : t = fold_binary_loc (gimple_location (stmt), code, type, op0, op1);
411 8124347 : if (!t)
412 : return NULL_TREE;
413 :
414 : /* Require that we got a boolean type out if we put one in. */
415 3524384 : gcc_assert (TREE_CODE (TREE_TYPE (t)) == TREE_CODE (type));
416 :
417 : /* Canonicalize the combined condition for use in a COND_EXPR. */
418 3524384 : t = canonicalize_cond_expr_cond (t);
419 :
420 : /* Bail out if we required an invariant but didn't get one. */
421 3524384 : if (!t || (invariant_only && !is_gimple_min_invariant (t)))
422 3290884 : return NULL_TREE;
423 :
424 : return t;
425 : }
426 :
427 : /* Combine the comparison OP0 CODE OP1 at LOC with the defining statements
428 : of its operand. Return a new comparison tree or NULL_TREE if there
429 : were no simplifying combines. */
430 :
431 : static tree
432 21608490 : forward_propagate_into_comparison_1 (gimple *stmt,
433 : enum tree_code code, tree type,
434 : tree op0, tree op1)
435 : {
436 21608490 : tree tmp = NULL_TREE;
437 21608490 : tree rhs0 = NULL_TREE, rhs1 = NULL_TREE;
438 21608490 : bool single_use0_p = false, single_use1_p = false;
439 :
440 : /* For comparisons use the first operand, that is likely to
441 : simplify comparisons against constants. */
442 21608490 : if (TREE_CODE (op0) == SSA_NAME)
443 : {
444 21572351 : gimple *def_stmt = get_prop_source_stmt (op0, false, &single_use0_p);
445 21572351 : if (def_stmt && can_propagate_from (def_stmt))
446 : {
447 5464216 : enum tree_code def_code = gimple_assign_rhs_code (def_stmt);
448 5464216 : bool invariant_only_p = !single_use0_p;
449 :
450 5464216 : rhs0 = rhs_to_tree (TREE_TYPE (op1), def_stmt);
451 :
452 : /* Always combine comparisons or conversions from booleans. */
453 5464216 : if (TREE_CODE (op1) == INTEGER_CST
454 5464216 : && ((CONVERT_EXPR_CODE_P (def_code)
455 859839 : && TREE_CODE (TREE_TYPE (TREE_OPERAND (rhs0, 0)))
456 : == BOOLEAN_TYPE)
457 3500343 : || TREE_CODE_CLASS (def_code) == tcc_comparison))
458 : invariant_only_p = false;
459 :
460 5464216 : tmp = combine_cond_expr_cond (stmt, code, type,
461 : rhs0, op1, invariant_only_p);
462 5464216 : if (tmp)
463 : return tmp;
464 : }
465 : }
466 :
467 : /* If that wasn't successful, try the second operand. */
468 21383311 : if (TREE_CODE (op1) == SSA_NAME)
469 : {
470 5379862 : gimple *def_stmt = get_prop_source_stmt (op1, false, &single_use1_p);
471 5379862 : if (def_stmt && can_propagate_from (def_stmt))
472 : {
473 1745276 : rhs1 = rhs_to_tree (TREE_TYPE (op0), def_stmt);
474 3490552 : tmp = combine_cond_expr_cond (stmt, code, type,
475 1745276 : op0, rhs1, !single_use1_p);
476 1745276 : if (tmp)
477 : return tmp;
478 : }
479 : }
480 :
481 : /* If that wasn't successful either, try both operands. */
482 21376899 : if (rhs0 != NULL_TREE
483 21376899 : && rhs1 != NULL_TREE)
484 914855 : tmp = combine_cond_expr_cond (stmt, code, type,
485 : rhs0, rhs1,
486 914855 : !(single_use0_p && single_use1_p));
487 :
488 : return tmp;
489 : }
490 :
491 : /* Propagate from the ssa name definition statements of the assignment
492 : from a comparison at *GSI into the conditional if that simplifies it.
493 : Returns true if the stmt was modified. */
494 :
495 : static bool
496 2551194 : forward_propagate_into_comparison (gimple_stmt_iterator *gsi)
497 : {
498 2551194 : gimple *stmt = gsi_stmt (*gsi);
499 2551194 : tree tmp;
500 2551194 : tree type = TREE_TYPE (gimple_assign_lhs (stmt));
501 2551194 : tree rhs1 = gimple_assign_rhs1 (stmt);
502 2551194 : tree rhs2 = gimple_assign_rhs2 (stmt);
503 :
504 : /* Combine the comparison with defining statements. */
505 2551194 : tmp = forward_propagate_into_comparison_1 (stmt,
506 : gimple_assign_rhs_code (stmt),
507 : type, rhs1, rhs2);
508 2551194 : if (tmp && useless_type_conversion_p (type, TREE_TYPE (tmp)))
509 : {
510 7159 : if (dump_file)
511 : {
512 0 : fprintf (dump_file, " Replaced '");
513 0 : print_gimple_expr (dump_file, stmt, 0);
514 0 : fprintf (dump_file, "' with '");
515 0 : print_generic_expr (dump_file, tmp);
516 0 : fprintf (dump_file, "'\n");
517 : }
518 7159 : gimple_assign_set_rhs_from_tree (gsi, tmp);
519 7159 : fold_stmt (gsi);
520 7159 : update_stmt (gsi_stmt (*gsi));
521 :
522 7159 : if (TREE_CODE (rhs1) == SSA_NAME)
523 7159 : remove_prop_source_from_use (rhs1);
524 7159 : if (TREE_CODE (rhs2) == SSA_NAME)
525 2991 : remove_prop_source_from_use (rhs2);
526 7159 : return true;
527 : }
528 :
529 : return false;
530 : }
531 :
532 : /* Propagate from the ssa name definition statements of COND_EXPR
533 : in GIMPLE_COND statement STMT into the conditional if that simplifies it.
534 : Returns zero if no statement was changed, one if there were
535 : changes and two if cfg_cleanup needs to run. */
536 :
537 : static int
538 19057296 : forward_propagate_into_gimple_cond (gcond *stmt)
539 : {
540 19057296 : tree tmp;
541 19057296 : enum tree_code code = gimple_cond_code (stmt);
542 19057296 : tree rhs1 = gimple_cond_lhs (stmt);
543 19057296 : tree rhs2 = gimple_cond_rhs (stmt);
544 :
545 : /* GIMPLE_COND will always be a comparison. */
546 19057296 : gcc_assert (TREE_CODE_CLASS (gimple_cond_code (stmt)) == tcc_comparison);
547 :
548 19057296 : tmp = forward_propagate_into_comparison_1 (stmt, code,
549 : boolean_type_node,
550 : rhs1, rhs2);
551 19057296 : if (tmp
552 19057296 : && is_gimple_condexpr_for_cond (tmp))
553 : {
554 220016 : if (dump_file)
555 : {
556 9 : fprintf (dump_file, " Replaced '");
557 9 : print_gimple_expr (dump_file, stmt, 0);
558 9 : fprintf (dump_file, "' with '");
559 9 : print_generic_expr (dump_file, tmp);
560 9 : fprintf (dump_file, "'\n");
561 : }
562 :
563 220016 : gimple_cond_set_condition_from_tree (stmt, unshare_expr (tmp));
564 220016 : update_stmt (stmt);
565 :
566 220016 : if (TREE_CODE (rhs1) == SSA_NAME)
567 220016 : remove_prop_source_from_use (rhs1);
568 220016 : if (TREE_CODE (rhs2) == SSA_NAME)
569 5618 : remove_prop_source_from_use (rhs2);
570 220016 : return is_gimple_min_invariant (tmp) ? 2 : 1;
571 : }
572 :
573 18837280 : if (canonicalize_bool_cond (stmt, gimple_bb (stmt)))
574 : return 1;
575 :
576 : return 0;
577 : }
578 :
579 : /* We've just substituted an ADDR_EXPR into stmt. Update all the
580 : relevant data structures to match. */
581 :
582 : static void
583 1907879 : tidy_after_forward_propagate_addr (gimple *stmt)
584 : {
585 : /* We may have turned a trapping insn into a non-trapping insn. */
586 1907879 : if (maybe_clean_or_replace_eh_stmt (stmt, stmt))
587 131 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
588 :
589 1907879 : if (TREE_CODE (gimple_assign_rhs1 (stmt)) == ADDR_EXPR)
590 235555 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
591 1907879 : }
592 :
593 : /* NAME is a SSA_NAME representing DEF_RHS which is of the form
594 : ADDR_EXPR <whatever>.
595 :
596 : Try to forward propagate the ADDR_EXPR into the use USE_STMT.
597 : Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
598 : node or for recovery of array indexing from pointer arithmetic.
599 :
600 : Return true if the propagation was successful (the propagation can
601 : be not totally successful, yet things may have been changed). */
602 :
603 : static bool
604 2723184 : forward_propagate_addr_expr_1 (tree name, tree def_rhs,
605 : gimple_stmt_iterator *use_stmt_gsi,
606 : bool single_use_p)
607 : {
608 2723184 : tree lhs, rhs, rhs2, array_ref;
609 2723184 : gimple *use_stmt = gsi_stmt (*use_stmt_gsi);
610 2723184 : enum tree_code rhs_code;
611 2723184 : bool res = true;
612 :
613 2723184 : gcc_assert (TREE_CODE (def_rhs) == ADDR_EXPR);
614 :
615 2723184 : lhs = gimple_assign_lhs (use_stmt);
616 2723184 : rhs_code = gimple_assign_rhs_code (use_stmt);
617 2723184 : rhs = gimple_assign_rhs1 (use_stmt);
618 :
619 : /* Do not perform copy-propagation but recurse through copy chains. */
620 2723184 : if (TREE_CODE (lhs) == SSA_NAME
621 1347418 : && rhs_code == SSA_NAME)
622 6887 : return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
623 :
624 : /* The use statement could be a conversion. Recurse to the uses of the
625 : lhs as copyprop does not copy through pointer to integer to pointer
626 : conversions and FRE does not catch all cases either.
627 : Treat the case of a single-use name and
628 : a conversion to def_rhs type separate, though. */
629 2716297 : if (TREE_CODE (lhs) == SSA_NAME
630 1340531 : && CONVERT_EXPR_CODE_P (rhs_code))
631 : {
632 : /* If there is a point in a conversion chain where the types match
633 : so we can remove a conversion re-materialize the address here
634 : and stop. */
635 23945 : if (single_use_p
636 23945 : && useless_type_conversion_p (TREE_TYPE (lhs), TREE_TYPE (def_rhs)))
637 : {
638 1 : gimple_assign_set_rhs1 (use_stmt, unshare_expr (def_rhs));
639 1 : gimple_assign_set_rhs_code (use_stmt, TREE_CODE (def_rhs));
640 1 : return true;
641 : }
642 :
643 : /* Else recurse if the conversion preserves the address value. */
644 47888 : if ((INTEGRAL_TYPE_P (TREE_TYPE (lhs))
645 2 : || POINTER_TYPE_P (TREE_TYPE (lhs)))
646 47888 : && (TYPE_PRECISION (TREE_TYPE (lhs))
647 23944 : >= TYPE_PRECISION (TREE_TYPE (def_rhs))))
648 23876 : return forward_propagate_addr_expr (lhs, def_rhs, single_use_p);
649 :
650 : return false;
651 : }
652 :
653 : /* If this isn't a conversion chain from this on we only can propagate
654 : into compatible pointer contexts. */
655 2692352 : if (!types_compatible_p (TREE_TYPE (name), TREE_TYPE (def_rhs)))
656 : return false;
657 :
658 : /* Propagate through constant pointer adjustments. */
659 2671710 : if (TREE_CODE (lhs) == SSA_NAME
660 1297117 : && rhs_code == POINTER_PLUS_EXPR
661 1297117 : && rhs == name
662 2834264 : && TREE_CODE (gimple_assign_rhs2 (use_stmt)) == INTEGER_CST)
663 : {
664 117707 : tree new_def_rhs;
665 : /* As we come here with non-invariant addresses in def_rhs we need
666 : to make sure we can build a valid constant offsetted address
667 : for further propagation. Simply rely on fold building that
668 : and check after the fact. */
669 117707 : new_def_rhs = fold_build2 (MEM_REF, TREE_TYPE (TREE_TYPE (rhs)),
670 : def_rhs,
671 : fold_convert (ptr_type_node,
672 : gimple_assign_rhs2 (use_stmt)));
673 117707 : if (TREE_CODE (new_def_rhs) == MEM_REF
674 117707 : && !is_gimple_mem_ref_addr (TREE_OPERAND (new_def_rhs, 0)))
675 : return false;
676 113747 : new_def_rhs = build1 (ADDR_EXPR, TREE_TYPE (rhs), new_def_rhs);
677 :
678 : /* Recurse. If we could propagate into all uses of lhs do not
679 : bother to replace into the current use but just pretend we did. */
680 113747 : if (forward_propagate_addr_expr (lhs, new_def_rhs, single_use_p))
681 : return true;
682 :
683 37638 : if (useless_type_conversion_p (TREE_TYPE (lhs),
684 37638 : TREE_TYPE (new_def_rhs)))
685 37638 : gimple_assign_set_rhs_with_ops (use_stmt_gsi, TREE_CODE (new_def_rhs),
686 : new_def_rhs);
687 0 : else if (is_gimple_min_invariant (new_def_rhs))
688 0 : gimple_assign_set_rhs_with_ops (use_stmt_gsi, NOP_EXPR, new_def_rhs);
689 : else
690 : return false;
691 37638 : gcc_assert (gsi_stmt (*use_stmt_gsi) == use_stmt);
692 37638 : update_stmt (use_stmt);
693 37638 : return true;
694 : }
695 :
696 : /* Now strip away any outer COMPONENT_REF/ARRAY_REF nodes from the LHS.
697 : ADDR_EXPR will not appear on the LHS. */
698 2554003 : tree *lhsp = gimple_assign_lhs_ptr (use_stmt);
699 3866312 : while (handled_component_p (*lhsp))
700 1312309 : lhsp = &TREE_OPERAND (*lhsp, 0);
701 2554003 : lhs = *lhsp;
702 :
703 : /* Now see if the LHS node is a MEM_REF using NAME. If so,
704 : propagate the ADDR_EXPR into the use of NAME and fold the result. */
705 2554003 : if (TREE_CODE (lhs) == MEM_REF
706 2554003 : && TREE_OPERAND (lhs, 0) == name)
707 : {
708 866366 : tree def_rhs_base;
709 866366 : poly_int64 def_rhs_offset;
710 : /* If the address is invariant we can always fold it. */
711 866366 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
712 : &def_rhs_offset)))
713 : {
714 820405 : poly_offset_int off = mem_ref_offset (lhs);
715 820405 : tree new_ptr;
716 820405 : off += def_rhs_offset;
717 820405 : if (TREE_CODE (def_rhs_base) == MEM_REF)
718 : {
719 799810 : off += mem_ref_offset (def_rhs_base);
720 799810 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
721 : }
722 : else
723 20595 : new_ptr = build_fold_addr_expr (def_rhs_base);
724 820405 : TREE_OPERAND (lhs, 0) = new_ptr;
725 820405 : TREE_OPERAND (lhs, 1)
726 820405 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (lhs, 1)), off);
727 820405 : tidy_after_forward_propagate_addr (use_stmt);
728 : /* Continue propagating into the RHS if this was not the only use. */
729 820405 : if (single_use_p)
730 220103 : return true;
731 : }
732 : /* If the LHS is a plain dereference and the value type is the same as
733 : that of the pointed-to type of the address we can put the
734 : dereferenced address on the LHS preserving the original alias-type. */
735 45961 : else if (integer_zerop (TREE_OPERAND (lhs, 1))
736 17733 : && ((gimple_assign_lhs (use_stmt) == lhs
737 14151 : && useless_type_conversion_p
738 14151 : (TREE_TYPE (TREE_OPERAND (def_rhs, 0)),
739 14151 : TREE_TYPE (gimple_assign_rhs1 (use_stmt))))
740 13489 : || types_compatible_p (TREE_TYPE (lhs),
741 13489 : TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
742 : /* Don't forward anything into clobber stmts if it would result
743 : in the lhs no longer being a MEM_REF. */
744 53416 : && (!gimple_clobber_p (use_stmt)
745 161 : || TREE_CODE (TREE_OPERAND (def_rhs, 0)) == MEM_REF))
746 : {
747 7294 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
748 7294 : tree new_offset, new_base, saved, new_lhs;
749 26188 : while (handled_component_p (*def_rhs_basep))
750 11600 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
751 7294 : saved = *def_rhs_basep;
752 7294 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
753 : {
754 3758 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
755 3758 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (lhs, 1)),
756 : TREE_OPERAND (*def_rhs_basep, 1));
757 : }
758 : else
759 : {
760 3536 : new_base = build_fold_addr_expr (*def_rhs_basep);
761 3536 : new_offset = TREE_OPERAND (lhs, 1);
762 : }
763 7294 : tree atype = TREE_TYPE (*def_rhs_basep);
764 7294 : if (TYPE_ALIGN (TREE_TYPE (lhs)) < TYPE_ALIGN (atype))
765 312 : atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (lhs)));
766 7294 : *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
767 7294 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (lhs);
768 7294 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (lhs);
769 7294 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (lhs);
770 7294 : new_lhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
771 7294 : *lhsp = new_lhs;
772 7294 : TREE_THIS_VOLATILE (new_lhs) = TREE_THIS_VOLATILE (lhs);
773 7294 : TREE_SIDE_EFFECTS (new_lhs) = TREE_SIDE_EFFECTS (lhs);
774 7294 : *def_rhs_basep = saved;
775 7294 : tidy_after_forward_propagate_addr (use_stmt);
776 : /* Continue propagating into the RHS if this was not the
777 : only use. */
778 7294 : if (single_use_p)
779 : return true;
780 : }
781 : else
782 : /* We can have a struct assignment dereferencing our name twice.
783 : Note that we didn't propagate into the lhs to not falsely
784 : claim we did when propagating into the rhs. */
785 : res = false;
786 : }
787 :
788 : /* Strip away any outer COMPONENT_REF, ARRAY_REF or ADDR_EXPR
789 : nodes from the RHS. */
790 2330371 : tree *rhsp = gimple_assign_rhs1_ptr (use_stmt);
791 2330371 : if (TREE_CODE (*rhsp) == ADDR_EXPR)
792 224350 : rhsp = &TREE_OPERAND (*rhsp, 0);
793 3281329 : while (handled_component_p (*rhsp))
794 950958 : rhsp = &TREE_OPERAND (*rhsp, 0);
795 2330371 : rhs = *rhsp;
796 :
797 : /* Now see if the RHS node is a MEM_REF using NAME. If so,
798 : propagate the ADDR_EXPR into the use of NAME and fold the result. */
799 2330371 : if (TREE_CODE (rhs) == MEM_REF
800 2330371 : && TREE_OPERAND (rhs, 0) == name)
801 : {
802 1101721 : tree def_rhs_base;
803 1101721 : poly_int64 def_rhs_offset;
804 1101721 : if ((def_rhs_base = get_addr_base_and_unit_offset (TREE_OPERAND (def_rhs, 0),
805 : &def_rhs_offset)))
806 : {
807 1065508 : poly_offset_int off = mem_ref_offset (rhs);
808 1065508 : tree new_ptr;
809 1065508 : off += def_rhs_offset;
810 1065508 : if (TREE_CODE (def_rhs_base) == MEM_REF)
811 : {
812 1040256 : off += mem_ref_offset (def_rhs_base);
813 1040256 : new_ptr = TREE_OPERAND (def_rhs_base, 0);
814 : }
815 : else
816 25252 : new_ptr = build_fold_addr_expr (def_rhs_base);
817 1065508 : TREE_OPERAND (rhs, 0) = new_ptr;
818 1065508 : TREE_OPERAND (rhs, 1)
819 1065508 : = wide_int_to_tree (TREE_TYPE (TREE_OPERAND (rhs, 1)), off);
820 1065508 : fold_stmt_inplace (use_stmt_gsi);
821 1065508 : tidy_after_forward_propagate_addr (use_stmt);
822 1065508 : return res;
823 : }
824 : /* If the RHS is a plain dereference and the value type is the same as
825 : that of the pointed-to type of the address we can put the
826 : dereferenced address on the RHS preserving the original alias-type. */
827 36213 : else if (integer_zerop (TREE_OPERAND (rhs, 1))
828 36213 : && ((gimple_assign_rhs1 (use_stmt) == rhs
829 19714 : && useless_type_conversion_p
830 19714 : (TREE_TYPE (gimple_assign_lhs (use_stmt)),
831 19714 : TREE_TYPE (TREE_OPERAND (def_rhs, 0))))
832 22479 : || types_compatible_p (TREE_TYPE (rhs),
833 22479 : TREE_TYPE (TREE_OPERAND (def_rhs, 0)))))
834 : {
835 14672 : tree *def_rhs_basep = &TREE_OPERAND (def_rhs, 0);
836 14672 : tree new_offset, new_base, saved, new_rhs;
837 51904 : while (handled_component_p (*def_rhs_basep))
838 22560 : def_rhs_basep = &TREE_OPERAND (*def_rhs_basep, 0);
839 14672 : saved = *def_rhs_basep;
840 14672 : if (TREE_CODE (*def_rhs_basep) == MEM_REF)
841 : {
842 7047 : new_base = TREE_OPERAND (*def_rhs_basep, 0);
843 7047 : new_offset = fold_convert (TREE_TYPE (TREE_OPERAND (rhs, 1)),
844 : TREE_OPERAND (*def_rhs_basep, 1));
845 : }
846 : else
847 : {
848 7625 : new_base = build_fold_addr_expr (*def_rhs_basep);
849 7625 : new_offset = TREE_OPERAND (rhs, 1);
850 : }
851 14672 : tree atype = TREE_TYPE (*def_rhs_basep);
852 14672 : if (TYPE_ALIGN (TREE_TYPE (rhs)) < TYPE_ALIGN (atype))
853 526 : atype = build_aligned_type (atype, TYPE_ALIGN (TREE_TYPE (rhs)));
854 14672 : *def_rhs_basep = build2 (MEM_REF, atype, new_base, new_offset);
855 14672 : TREE_THIS_VOLATILE (*def_rhs_basep) = TREE_THIS_VOLATILE (rhs);
856 14672 : TREE_SIDE_EFFECTS (*def_rhs_basep) = TREE_SIDE_EFFECTS (rhs);
857 14672 : TREE_THIS_NOTRAP (*def_rhs_basep) = TREE_THIS_NOTRAP (rhs);
858 14672 : new_rhs = unshare_expr (TREE_OPERAND (def_rhs, 0));
859 14672 : *rhsp = new_rhs;
860 14672 : TREE_THIS_VOLATILE (new_rhs) = TREE_THIS_VOLATILE (rhs);
861 14672 : TREE_SIDE_EFFECTS (new_rhs) = TREE_SIDE_EFFECTS (rhs);
862 14672 : *def_rhs_basep = saved;
863 14672 : fold_stmt_inplace (use_stmt_gsi);
864 14672 : tidy_after_forward_propagate_addr (use_stmt);
865 14672 : return res;
866 : }
867 : }
868 :
869 : /* If the use of the ADDR_EXPR is not a POINTER_PLUS_EXPR, there
870 : is nothing to do. */
871 1250191 : if (gimple_assign_rhs_code (use_stmt) != POINTER_PLUS_EXPR
872 1250191 : || gimple_assign_rhs1 (use_stmt) != name)
873 : return false;
874 :
875 : /* The remaining cases are all for turning pointer arithmetic into
876 : array indexing. They only apply when we have the address of
877 : element zero in an array. If that is not the case then there
878 : is nothing to do. */
879 44847 : array_ref = TREE_OPERAND (def_rhs, 0);
880 44847 : if ((TREE_CODE (array_ref) != ARRAY_REF
881 4546 : || TREE_CODE (TREE_TYPE (TREE_OPERAND (array_ref, 0))) != ARRAY_TYPE
882 4546 : || TREE_CODE (TREE_OPERAND (array_ref, 1)) != INTEGER_CST)
883 46346 : && TREE_CODE (TREE_TYPE (array_ref)) != ARRAY_TYPE)
884 : return false;
885 :
886 22359 : rhs2 = gimple_assign_rhs2 (use_stmt);
887 : /* Optimize &x[C1] p+ C2 to &x p+ C3 with C3 = C1 * element_size + C2. */
888 22359 : if (TREE_CODE (rhs2) == INTEGER_CST)
889 : {
890 0 : tree new_rhs = build1_loc (gimple_location (use_stmt),
891 0 : ADDR_EXPR, TREE_TYPE (def_rhs),
892 0 : fold_build2 (MEM_REF,
893 : TREE_TYPE (TREE_TYPE (def_rhs)),
894 : unshare_expr (def_rhs),
895 : fold_convert (ptr_type_node,
896 : rhs2)));
897 0 : gimple_assign_set_rhs_from_tree (use_stmt_gsi, new_rhs);
898 0 : use_stmt = gsi_stmt (*use_stmt_gsi);
899 0 : update_stmt (use_stmt);
900 0 : tidy_after_forward_propagate_addr (use_stmt);
901 0 : return true;
902 : }
903 :
904 : return false;
905 : }
906 :
907 : /* STMT is a statement of the form SSA_NAME = ADDR_EXPR <whatever>.
908 :
909 : Try to forward propagate the ADDR_EXPR into all uses of the SSA_NAME.
910 : Often this will allow for removal of an ADDR_EXPR and INDIRECT_REF
911 : node or for recovery of array indexing from pointer arithmetic.
912 :
913 : PARENT_SINGLE_USE_P tells if, when in a recursive invocation, NAME was
914 : the single use in the previous invocation. Pass true when calling
915 : this as toplevel.
916 :
917 : Returns true, if all uses have been propagated into. */
918 :
919 : static bool
920 3159455 : forward_propagate_addr_expr (tree name, tree rhs, bool parent_single_use_p)
921 : {
922 3159455 : bool all = true;
923 3159455 : bool single_use_p = parent_single_use_p && has_single_use (name);
924 :
925 16617325 : for (gimple *use_stmt : gather_imm_use_stmts (name))
926 : {
927 7138960 : bool result;
928 7138960 : tree use_rhs;
929 :
930 : /* If the use is not in a simple assignment statement, then
931 : there is nothing we can do. */
932 7138960 : if (!is_gimple_assign (use_stmt))
933 : {
934 4415776 : if (!is_gimple_debug (use_stmt))
935 1839300 : all = false;
936 4415776 : continue;
937 : }
938 :
939 2723184 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
940 2723184 : result = forward_propagate_addr_expr_1 (name, rhs, &gsi,
941 : single_use_p);
942 : /* If the use has moved to a different statement adjust
943 : the update machinery for the old statement too. */
944 2723184 : if (use_stmt != gsi_stmt (gsi))
945 : {
946 0 : update_stmt (use_stmt);
947 0 : use_stmt = gsi_stmt (gsi);
948 : }
949 2723184 : update_stmt (use_stmt);
950 2723184 : all &= result;
951 :
952 : /* Remove intermediate now unused copy and conversion chains. */
953 2723184 : use_rhs = gimple_assign_rhs1 (use_stmt);
954 2723184 : if (result
955 1421267 : && TREE_CODE (gimple_assign_lhs (use_stmt)) == SSA_NAME
956 1185556 : && TREE_CODE (use_rhs) == SSA_NAME
957 2803000 : && has_zero_uses (gimple_assign_lhs (use_stmt)))
958 : {
959 79816 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
960 79816 : fwprop_invalidate_lattice (gimple_get_lhs (use_stmt));
961 79816 : release_defs (use_stmt);
962 79816 : gsi_remove (&gsi, true);
963 : }
964 3159455 : }
965 :
966 3159455 : return all && has_zero_uses (name);
967 : }
968 :
969 :
970 : /* Helper function for simplify_gimple_switch. Remove case labels that
971 : have values outside the range of the new type. */
972 :
973 : static void
974 11511 : simplify_gimple_switch_label_vec (gswitch *stmt, tree index_type,
975 : vec<std::pair<int, int> > &edges_to_remove)
976 : {
977 11511 : unsigned int branch_num = gimple_switch_num_labels (stmt);
978 11511 : auto_vec<tree> labels (branch_num);
979 11511 : unsigned int i, len;
980 :
981 : /* Collect the existing case labels in a VEC, and preprocess it as if
982 : we are gimplifying a GENERIC SWITCH_EXPR. */
983 72422 : for (i = 1; i < branch_num; i++)
984 49400 : labels.quick_push (gimple_switch_label (stmt, i));
985 11511 : preprocess_case_label_vec_for_gimple (labels, index_type, NULL);
986 :
987 : /* If any labels were removed, replace the existing case labels
988 : in the GIMPLE_SWITCH statement with the correct ones.
989 : Note that the type updates were done in-place on the case labels,
990 : so we only have to replace the case labels in the GIMPLE_SWITCH
991 : if the number of labels changed. */
992 11511 : len = labels.length ();
993 11511 : if (len < branch_num - 1)
994 : {
995 0 : bitmap target_blocks;
996 0 : edge_iterator ei;
997 0 : edge e;
998 :
999 : /* Corner case: *all* case labels have been removed as being
1000 : out-of-range for INDEX_TYPE. Push one label and let the
1001 : CFG cleanups deal with this further. */
1002 0 : if (len == 0)
1003 : {
1004 0 : tree label, elt;
1005 :
1006 0 : label = CASE_LABEL (gimple_switch_default_label (stmt));
1007 0 : elt = build_case_label (build_int_cst (index_type, 0), NULL, label);
1008 0 : labels.quick_push (elt);
1009 0 : len = 1;
1010 : }
1011 :
1012 0 : for (i = 0; i < labels.length (); i++)
1013 0 : gimple_switch_set_label (stmt, i + 1, labels[i]);
1014 0 : for (i++ ; i < branch_num; i++)
1015 0 : gimple_switch_set_label (stmt, i, NULL_TREE);
1016 0 : gimple_switch_set_num_labels (stmt, len + 1);
1017 :
1018 : /* Cleanup any edges that are now dead. */
1019 0 : target_blocks = BITMAP_ALLOC (NULL);
1020 0 : for (i = 0; i < gimple_switch_num_labels (stmt); i++)
1021 : {
1022 0 : tree elt = gimple_switch_label (stmt, i);
1023 0 : basic_block target = label_to_block (cfun, CASE_LABEL (elt));
1024 0 : bitmap_set_bit (target_blocks, target->index);
1025 : }
1026 0 : for (ei = ei_start (gimple_bb (stmt)->succs); (e = ei_safe_edge (ei)); )
1027 : {
1028 0 : if (! bitmap_bit_p (target_blocks, e->dest->index))
1029 0 : edges_to_remove.safe_push (std::make_pair (e->src->index,
1030 0 : e->dest->index));
1031 : else
1032 0 : ei_next (&ei);
1033 : }
1034 0 : BITMAP_FREE (target_blocks);
1035 : }
1036 11511 : }
1037 :
1038 : /* STMT is a SWITCH_EXPR for which we attempt to find equivalent forms of
1039 : the condition which we may be able to optimize better. */
1040 :
1041 : static bool
1042 105981 : simplify_gimple_switch (gswitch *stmt,
1043 : vec<std::pair<int, int> > &edges_to_remove,
1044 : bitmap simple_dce_worklist)
1045 : {
1046 : /* The optimization that we really care about is removing unnecessary
1047 : casts. That will let us do much better in propagating the inferred
1048 : constant at the switch target. */
1049 105981 : tree cond = gimple_switch_index (stmt);
1050 105981 : if (TREE_CODE (cond) == SSA_NAME)
1051 : {
1052 105980 : gimple *def_stmt = SSA_NAME_DEF_STMT (cond);
1053 105980 : if (gimple_assign_cast_p (def_stmt))
1054 : {
1055 12397 : tree def = gimple_assign_rhs1 (def_stmt);
1056 12397 : if (TREE_CODE (def) != SSA_NAME)
1057 : return false;
1058 :
1059 : /* If we have an extension or sign-change that preserves the
1060 : values we check against then we can copy the source value into
1061 : the switch. */
1062 12397 : tree ti = TREE_TYPE (def);
1063 12397 : if (INTEGRAL_TYPE_P (ti)
1064 12397 : && TYPE_PRECISION (ti) <= TYPE_PRECISION (TREE_TYPE (cond)))
1065 : {
1066 12152 : size_t n = gimple_switch_num_labels (stmt);
1067 12152 : tree min = NULL_TREE, max = NULL_TREE;
1068 12152 : if (n > 1)
1069 : {
1070 12152 : min = CASE_LOW (gimple_switch_label (stmt, 1));
1071 12152 : if (CASE_HIGH (gimple_switch_label (stmt, n - 1)))
1072 154 : max = CASE_HIGH (gimple_switch_label (stmt, n - 1));
1073 : else
1074 11998 : max = CASE_LOW (gimple_switch_label (stmt, n - 1));
1075 : }
1076 12152 : if ((!min || int_fits_type_p (min, ti))
1077 12148 : && (!max || int_fits_type_p (max, ti)))
1078 : {
1079 11511 : bitmap_set_bit (simple_dce_worklist,
1080 11511 : SSA_NAME_VERSION (cond));
1081 11511 : gimple_switch_set_index (stmt, def);
1082 11511 : simplify_gimple_switch_label_vec (stmt, ti,
1083 : edges_to_remove);
1084 11511 : update_stmt (stmt);
1085 11511 : return true;
1086 : }
1087 : }
1088 : }
1089 : }
1090 :
1091 : return false;
1092 : }
1093 :
1094 : /* For pointers p2 and p1 return p2 - p1 if the
1095 : difference is known and constant, otherwise return NULL. */
1096 :
1097 : static tree
1098 5359 : constant_pointer_difference (tree p1, tree p2)
1099 : {
1100 5359 : int i, j;
1101 : #define CPD_ITERATIONS 5
1102 5359 : tree exps[2][CPD_ITERATIONS];
1103 5359 : tree offs[2][CPD_ITERATIONS];
1104 5359 : int cnt[2];
1105 :
1106 16077 : for (i = 0; i < 2; i++)
1107 : {
1108 10718 : tree p = i ? p1 : p2;
1109 10718 : tree off = size_zero_node;
1110 10718 : gimple *stmt;
1111 10718 : enum tree_code code;
1112 :
1113 : /* For each of p1 and p2 we need to iterate at least
1114 : twice, to handle ADDR_EXPR directly in p1/p2,
1115 : SSA_NAME with ADDR_EXPR or POINTER_PLUS_EXPR etc.
1116 : on definition's stmt RHS. Iterate a few extra times. */
1117 10718 : j = 0;
1118 12657 : do
1119 : {
1120 12657 : if (!POINTER_TYPE_P (TREE_TYPE (p)))
1121 : break;
1122 12651 : if (TREE_CODE (p) == ADDR_EXPR)
1123 : {
1124 9239 : tree q = TREE_OPERAND (p, 0);
1125 9239 : poly_int64 offset;
1126 9239 : tree base = get_addr_base_and_unit_offset (q, &offset);
1127 9239 : if (base)
1128 : {
1129 8423 : q = base;
1130 8423 : if (maybe_ne (offset, 0))
1131 3536 : off = size_binop (PLUS_EXPR, off, size_int (offset));
1132 : }
1133 9239 : if (TREE_CODE (q) == MEM_REF
1134 9239 : && TREE_CODE (TREE_OPERAND (q, 0)) == SSA_NAME)
1135 : {
1136 213 : p = TREE_OPERAND (q, 0);
1137 213 : off = size_binop (PLUS_EXPR, off,
1138 : wide_int_to_tree (sizetype,
1139 : mem_ref_offset (q)));
1140 : }
1141 : else
1142 : {
1143 9026 : exps[i][j] = q;
1144 9026 : offs[i][j++] = off;
1145 9026 : break;
1146 : }
1147 : }
1148 3625 : if (TREE_CODE (p) != SSA_NAME)
1149 : break;
1150 3625 : exps[i][j] = p;
1151 3625 : offs[i][j++] = off;
1152 3625 : if (j == CPD_ITERATIONS)
1153 : break;
1154 3625 : stmt = SSA_NAME_DEF_STMT (p);
1155 3625 : if (!is_gimple_assign (stmt) || gimple_assign_lhs (stmt) != p)
1156 : break;
1157 2843 : code = gimple_assign_rhs_code (stmt);
1158 2843 : if (code == POINTER_PLUS_EXPR)
1159 : {
1160 1485 : if (TREE_CODE (gimple_assign_rhs2 (stmt)) != INTEGER_CST)
1161 : break;
1162 954 : off = size_binop (PLUS_EXPR, off, gimple_assign_rhs2 (stmt));
1163 954 : p = gimple_assign_rhs1 (stmt);
1164 : }
1165 1358 : else if (code == ADDR_EXPR || CONVERT_EXPR_CODE_P (code))
1166 985 : p = gimple_assign_rhs1 (stmt);
1167 : else
1168 : break;
1169 : }
1170 : while (1);
1171 10718 : cnt[i] = j;
1172 : }
1173 :
1174 7440 : for (i = 0; i < cnt[0]; i++)
1175 9876 : for (j = 0; j < cnt[1]; j++)
1176 7795 : if (exps[0][i] == exps[1][j])
1177 4466 : return size_binop (MINUS_EXPR, offs[0][i], offs[1][j]);
1178 :
1179 : return NULL_TREE;
1180 : }
1181 :
1182 : /* Helper function for optimize_aggr_zeroprop.
1183 : Props the zeroing (memset, VAL) that was done in DEST+OFFSET:LEN
1184 : (DEFSTMT) into the STMT. Returns true if the STMT was updated. */
1185 : static void
1186 22074170 : optimize_aggr_zeroprop_1 (gimple *defstmt, gimple *stmt,
1187 : tree dest, poly_int64 offset, tree val,
1188 : poly_offset_int len)
1189 : {
1190 22074170 : tree src2;
1191 22074170 : tree len2 = NULL_TREE;
1192 22074170 : poly_int64 offset2;
1193 :
1194 22074170 : if (gimple_call_builtin_p (stmt, BUILT_IN_MEMCPY)
1195 21329 : && TREE_CODE (gimple_call_arg (stmt, 1)) == ADDR_EXPR
1196 22087948 : && poly_int_tree_p (gimple_call_arg (stmt, 2)))
1197 : {
1198 12759 : src2 = TREE_OPERAND (gimple_call_arg (stmt, 1), 0);
1199 12759 : len2 = gimple_call_arg (stmt, 2);
1200 : }
1201 22061411 : else if (gimple_assign_load_p (stmt) && gimple_store_p (stmt))
1202 : {
1203 1885397 : src2 = gimple_assign_rhs1 (stmt);
1204 1885397 : len2 = (TREE_CODE (src2) == COMPONENT_REF
1205 1885397 : ? DECL_SIZE_UNIT (TREE_OPERAND (src2, 1))
1206 1718096 : : TYPE_SIZE_UNIT (TREE_TYPE (src2)));
1207 : /* Can only handle zero memsets. */
1208 1885397 : if (!integer_zerop (val))
1209 22052986 : return;
1210 : }
1211 : else
1212 20176014 : return;
1213 :
1214 1897150 : if (len2 == NULL_TREE
1215 1897150 : || !poly_int_tree_p (len2))
1216 : return;
1217 :
1218 1897150 : src2 = get_addr_base_and_unit_offset (src2, &offset2);
1219 1897150 : if (src2 == NULL_TREE
1220 1897150 : || maybe_lt (offset2, offset))
1221 : return;
1222 :
1223 861221 : if (!operand_equal_p (dest, src2, 0))
1224 : return;
1225 :
1226 : /* [ dest + offset, dest + offset + len - 1 ] is set to val.
1227 : Make sure that
1228 : [ dest + offset2, dest + offset2 + len2 - 1 ] is a subset of that. */
1229 130387 : if (maybe_gt (wi::to_poly_offset (len2) + (offset2 - offset),
1230 : len))
1231 : return;
1232 :
1233 21184 : if (dump_file && (dump_flags & TDF_DETAILS))
1234 : {
1235 32 : fprintf (dump_file, "Simplified\n ");
1236 32 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1237 32 : fprintf (dump_file, "after previous\n ");
1238 32 : print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
1239 : }
1240 21184 : gimple *orig_stmt = stmt;
1241 : /* For simplicity, don't change the kind of the stmt,
1242 : turn dest = src; into dest = {}; and memcpy (&dest, &src, len);
1243 : into memset (&dest, val, len);
1244 : In theory we could change dest = src into memset if dest
1245 : is addressable (maybe beneficial if val is not 0), or
1246 : memcpy (&dest, &src, len) into dest = {} if len is the size
1247 : of dest, dest isn't volatile. */
1248 21184 : if (is_gimple_assign (stmt))
1249 : {
1250 21179 : tree ctor_type = TREE_TYPE (gimple_assign_lhs (stmt));
1251 21179 : tree ctor = build_constructor (ctor_type, NULL);
1252 21179 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
1253 21179 : gimple_assign_set_rhs_from_tree (&gsi, ctor);
1254 21179 : update_stmt (stmt);
1255 21179 : statistics_counter_event (cfun, "copy zeroing propagation of aggregate", 1);
1256 : }
1257 : else /* If stmt is memcpy, transform it into memset. */
1258 : {
1259 5 : gcall *call = as_a <gcall *> (stmt);
1260 5 : tree fndecl = builtin_decl_implicit (BUILT_IN_MEMSET);
1261 5 : gimple_call_set_fndecl (call, fndecl);
1262 5 : gimple_call_set_fntype (call, TREE_TYPE (fndecl));
1263 5 : gimple_call_set_arg (call, 1, val);
1264 5 : update_stmt (stmt);
1265 5 : statistics_counter_event (cfun, "memcpy to memset changed", 1);
1266 : }
1267 :
1268 21184 : if (dump_file && (dump_flags & TDF_DETAILS))
1269 : {
1270 32 : fprintf (dump_file, "into\n ");
1271 32 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1272 : }
1273 :
1274 : /* Mark the bb for eh cleanup if needed. */
1275 21184 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
1276 6 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
1277 : }
1278 :
1279 : /* Optimize
1280 : a = {}; // DEST = value ;; LEN(nullptr)
1281 : b = a;
1282 : into
1283 : a = {};
1284 : b = {};
1285 : Similarly for memset (&a, ..., sizeof (a)); instead of a = {};
1286 : and/or memcpy (&b, &a, sizeof (a)); instead of b = a; */
1287 :
1288 : static void
1289 30531023 : optimize_aggr_zeroprop (gimple *stmt, bool full_walk)
1290 : {
1291 30531023 : ao_ref read;
1292 61062046 : if (gimple_has_volatile_ops (stmt))
1293 26598297 : return;
1294 :
1295 29605894 : tree dest = NULL_TREE;
1296 29605894 : tree val = integer_zero_node;
1297 29605894 : tree len = NULL_TREE;
1298 29605894 : bool can_use_tbba = true;
1299 :
1300 29605894 : if (gimple_call_builtin_p (stmt, BUILT_IN_MEMSET)
1301 109618 : && TREE_CODE (gimple_call_arg (stmt, 0)) == ADDR_EXPR
1302 55801 : && TREE_CODE (gimple_call_arg (stmt, 1)) == INTEGER_CST
1303 29659403 : && poly_int_tree_p (gimple_call_arg (stmt, 2)))
1304 : {
1305 50740 : dest = TREE_OPERAND (gimple_call_arg (stmt, 0), 0);
1306 50740 : len = gimple_call_arg (stmt, 2);
1307 50740 : val = gimple_call_arg (stmt, 1);
1308 50740 : ao_ref_init_from_ptr_and_size (&read, gimple_call_arg (stmt, 0), len);
1309 50740 : can_use_tbba = false;
1310 : }
1311 29555154 : else if (gimple_store_p (stmt)
1312 29496116 : && gimple_assign_single_p (stmt)
1313 59051270 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == STRING_CST)
1314 : {
1315 25918 : tree str = gimple_assign_rhs1 (stmt);
1316 25918 : dest = gimple_assign_lhs (stmt);
1317 25918 : ao_ref_init (&read, dest);
1318 : /* The string must contain all null char's for now. */
1319 31207 : for (int i = 0; i < TREE_STRING_LENGTH (str); i++)
1320 : {
1321 28669 : if (TREE_STRING_POINTER (str)[i] != 0)
1322 : {
1323 : dest = NULL_TREE;
1324 : break;
1325 : }
1326 : }
1327 : }
1328 : /* A store of integer (scalar, vector or complex) zeros is
1329 : a zero store. */
1330 29529236 : else if (gimple_store_p (stmt)
1331 29470198 : && gimple_assign_single_p (stmt)
1332 58999434 : && integer_zerop (gimple_assign_rhs1 (stmt)))
1333 : {
1334 3470858 : tree rhs = gimple_assign_rhs1 (stmt);
1335 3470858 : tree type = TREE_TYPE (rhs);
1336 3470858 : dest = gimple_assign_lhs (stmt);
1337 3470858 : ao_ref_init (&read, dest);
1338 : /* For integral types, the type precision needs to be a multiply of BITS_PER_UNIT. */
1339 3470858 : if (INTEGRAL_TYPE_P (type)
1340 3470858 : && (TYPE_PRECISION (type) % BITS_PER_UNIT) != 0)
1341 : dest = NULL_TREE;
1342 : }
1343 26058378 : else if (gimple_store_p (stmt)
1344 25999340 : && gimple_assign_single_p (stmt)
1345 25999340 : && TREE_CODE (gimple_assign_rhs1 (stmt)) == CONSTRUCTOR
1346 26755010 : && !gimple_clobber_p (stmt))
1347 : {
1348 696632 : dest = gimple_assign_lhs (stmt);
1349 696632 : ao_ref_init (&read, dest);
1350 : }
1351 :
1352 4041977 : if (dest == NULL_TREE)
1353 25587297 : return;
1354 :
1355 4018597 : if (len == NULL_TREE)
1356 3967857 : len = (TREE_CODE (dest) == COMPONENT_REF
1357 3967857 : ? DECL_SIZE_UNIT (TREE_OPERAND (dest, 1))
1358 1720973 : : TYPE_SIZE_UNIT (TREE_TYPE (dest)));
1359 3967857 : if (len == NULL_TREE
1360 4018597 : || !poly_int_tree_p (len))
1361 : return;
1362 :
1363 : /* Sometimes memset can have no vdef due to invalid declaration of memset (const, etc.). */
1364 34635467 : if (!gimple_vdef (stmt))
1365 : return;
1366 :
1367 : /* This store needs to be on the byte boundary and pointing to an object. */
1368 4018573 : poly_int64 offset;
1369 4018573 : tree dest_base = get_addr_base_and_unit_offset (dest, &offset);
1370 4018573 : if (dest_base == NULL_TREE)
1371 : return;
1372 :
1373 : /* Setup the worklist. */
1374 3932726 : auto_vec<std::pair<tree, unsigned>> worklist;
1375 3932726 : unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 0;
1376 7865452 : worklist.safe_push (std::make_pair (gimple_vdef (stmt), limit));
1377 :
1378 27507824 : while (!worklist.is_empty ())
1379 : {
1380 19642372 : std::pair<tree, unsigned> top = worklist.pop ();
1381 19642372 : tree vdef = top.first;
1382 19642372 : limit = top.second;
1383 19642372 : gimple *use_stmt;
1384 19642372 : imm_use_iterator iter;
1385 63335242 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1386 : {
1387 : /* Handling PHI nodes might not be worth it so don't. */
1388 24050498 : if (is_a <gphi*> (use_stmt))
1389 1976328 : continue;
1390 :
1391 : /* If this statement does not clobber add the vdef stmt to the
1392 : worklist.
1393 : After hitting the limit, allow clobbers to able to pass through. */
1394 1984120 : if ((limit != 0 || gimple_clobber_p (use_stmt))
1395 20124957 : && gimple_vdef (use_stmt)
1396 39175988 : && !stmt_may_clobber_ref_p_1 (use_stmt, &read,
1397 : /* tbaa_p = */ can_use_tbba))
1398 : {
1399 15709646 : unsigned new_limit = limit == 0 ? 0 : limit - 1;
1400 31419292 : worklist.safe_push (std::make_pair (gimple_vdef (use_stmt),
1401 : new_limit));
1402 : }
1403 :
1404 22074170 : optimize_aggr_zeroprop_1 (stmt, use_stmt, dest_base, offset,
1405 22074170 : val, wi::to_poly_offset (len));
1406 19642372 : }
1407 : }
1408 :
1409 3932726 : }
1410 :
1411 : /* Returns the pointer to the base of the object of the
1412 : reference EXPR and extracts the information about
1413 : the offset of the access, storing it to PBYTESIZE,
1414 : PBYTEPOS and PREVERSEP.
1415 : If the access is not a byte sized or position is not
1416 : on the byte, return NULL. */
1417 : static tree
1418 5252806 : split_core_and_offset_size (tree expr,
1419 : poly_int64 *pbytesize, poly_int64 *pbytepos,
1420 : tree *poffset, int *preversep)
1421 : {
1422 5252806 : tree core;
1423 5252806 : machine_mode mode;
1424 5252806 : int unsignedp, volatilep;
1425 5252806 : poly_int64 bitsize;
1426 5252806 : poly_int64 bitpos;
1427 5252806 : location_t loc = EXPR_LOCATION (expr);
1428 :
1429 5252806 : core = get_inner_reference (expr, &bitsize, &bitpos,
1430 : poffset, &mode, &unsignedp, preversep,
1431 : &volatilep);
1432 10505612 : if (!multiple_p (bitsize, BITS_PER_UNIT, pbytesize))
1433 : return NULL_TREE;
1434 5252806 : if (!multiple_p (bitpos, BITS_PER_UNIT, pbytepos))
1435 : return NULL_TREE;
1436 : /* If we are left with MEM[a + CST] strip that and add it to the
1437 : pbytepos and return a. */
1438 5252806 : if (TREE_CODE (core) == MEM_REF)
1439 : {
1440 1216198 : poly_offset_int tem;
1441 1216198 : tem = wi::to_poly_offset (TREE_OPERAND (core, 1));
1442 1216198 : tem += *pbytepos;
1443 1216198 : if (tem.to_shwi (pbytepos))
1444 1214272 : return TREE_OPERAND (core, 0);
1445 : }
1446 4038534 : core = build_fold_addr_expr_loc (loc, core);
1447 4038534 : STRIP_NOPS (core);
1448 4038534 : return core;
1449 : }
1450 :
1451 : /* Returns a new src based on the
1452 : copy `DEST = SRC` and for the old SRC2.
1453 : Returns null if SRC2 is not related to DEST. */
1454 :
1455 : static tree
1456 1222265 : new_src_based_on_copy (tree src2, tree dest, tree src)
1457 : {
1458 : /* If the second src is not exactly the same as dest,
1459 : try to handle it separately; see it is address/size equivalent.
1460 : Handles `a` and `a.b` and `MEM<char[N]>(&a)` which all have
1461 : the same size and offsets as address/size equivalent.
1462 : This allows copying over a memcpy and also one for copying
1463 : where one field is the same size as the whole struct. */
1464 1222265 : if (operand_equal_p (dest, src2))
1465 : return src;
1466 : /* if both dest and src2 are decls, then we know these 2
1467 : accesses can't be the same. */
1468 708414 : if (DECL_P (dest) && DECL_P (src2))
1469 : return NULL_TREE;
1470 : /* A VCE can't be used with imag/real or BFR so reject them early. */
1471 376472 : if (TREE_CODE (src) == IMAGPART_EXPR
1472 376472 : || TREE_CODE (src) == REALPART_EXPR
1473 376472 : || TREE_CODE (src) == BIT_FIELD_REF)
1474 : return NULL_TREE;
1475 376472 : tree core1, core2;
1476 376472 : poly_int64 bytepos1, bytepos2;
1477 376472 : poly_int64 bytesize1, bytesize2;
1478 376472 : tree toffset1, toffset2;
1479 376472 : int reversep1 = 0;
1480 376472 : int reversep2 = 0;
1481 376472 : poly_int64 diff = 0;
1482 376472 : core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
1483 : &toffset1, &reversep1);
1484 376472 : core2 = split_core_and_offset_size (src2, &bytesize2, &bytepos2,
1485 : &toffset2, &reversep2);
1486 376472 : if (!core1 || !core2)
1487 : return NULL_TREE;
1488 376472 : if (reversep1 != reversep2)
1489 : return NULL_TREE;
1490 : /* The sizes of the 2 accesses need to be the same. */
1491 376472 : if (!known_eq (bytesize1, bytesize2))
1492 : return NULL_TREE;
1493 168340 : if (!operand_equal_p (core1, core2, 0))
1494 : return NULL_TREE;
1495 :
1496 22529 : if (toffset1 && toffset2)
1497 : {
1498 2 : tree type = TREE_TYPE (toffset1);
1499 2 : if (type != TREE_TYPE (toffset2))
1500 0 : toffset2 = fold_convert (type, toffset2);
1501 :
1502 2 : tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
1503 2 : if (!cst_and_fits_in_hwi (tdiff))
1504 : return NULL_TREE;
1505 :
1506 0 : diff = int_cst_value (tdiff);
1507 0 : }
1508 22527 : else if (toffset1 || toffset2)
1509 : {
1510 : /* If only one of the offsets is non-constant, the difference cannot
1511 : be a constant. */
1512 : return NULL_TREE;
1513 : }
1514 22495 : diff += bytepos1 - bytepos2;
1515 : /* The offset between the 2 need to be 0. */
1516 22495 : if (!known_eq (diff, 0))
1517 : return NULL_TREE;
1518 21682 : return fold_build1 (VIEW_CONVERT_EXPR,TREE_TYPE (src2), src);
1519 : }
1520 :
1521 : /* Returns true if SRC and DEST are the same address such that
1522 : `SRC == DEST;` is considered a nop. This is more than an
1523 : operand_equal_p check as it needs to be similar to
1524 : new_src_based_on_copy. */
1525 :
1526 : static bool
1527 4357835 : same_for_assignment (tree src, tree dest)
1528 : {
1529 4357835 : if (operand_equal_p (dest, src, 0))
1530 : return true;
1531 : /* if both dest and src2 are decls, then we know these 2
1532 : accesses can't be the same. */
1533 4354935 : if (DECL_P (dest) && DECL_P (src))
1534 : return false;
1535 :
1536 2249931 : tree core1, core2;
1537 2249931 : poly_int64 bytepos1, bytepos2;
1538 2249931 : poly_int64 bytesize1, bytesize2;
1539 2249931 : tree toffset1, toffset2;
1540 2249931 : int reversep1 = 0;
1541 2249931 : int reversep2 = 0;
1542 2249931 : poly_int64 diff = 0;
1543 2249931 : core1 = split_core_and_offset_size (dest, &bytesize1, &bytepos1,
1544 : &toffset1, &reversep1);
1545 2249931 : core2 = split_core_and_offset_size (src, &bytesize2, &bytepos2,
1546 : &toffset2, &reversep2);
1547 2249931 : if (!core1 || !core2)
1548 : return false;
1549 2249931 : if (reversep1 != reversep2)
1550 : return false;
1551 : /* The sizes of the 2 accesses need to be the same. */
1552 2249931 : if (!known_eq (bytesize1, bytesize2))
1553 : return false;
1554 2249010 : if (!operand_equal_p (core1, core2, 0))
1555 : return false;
1556 6027 : if (toffset1 && toffset2)
1557 : {
1558 313 : tree type = TREE_TYPE (toffset1);
1559 313 : if (type != TREE_TYPE (toffset2))
1560 0 : toffset2 = fold_convert (type, toffset2);
1561 :
1562 313 : tree tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
1563 313 : if (!cst_and_fits_in_hwi (tdiff))
1564 : return false;
1565 :
1566 0 : diff = int_cst_value (tdiff);
1567 0 : }
1568 5714 : else if (toffset1 || toffset2)
1569 : {
1570 : /* If only one of the offsets is non-constant, the difference cannot
1571 : be a constant. */
1572 : return false;
1573 : }
1574 5714 : diff += bytepos1 - bytepos2;
1575 : /* The offset between the 2 need to be 0. */
1576 5714 : if (!known_eq (diff, 0))
1577 : return false;
1578 : return true;
1579 : }
1580 :
1581 : /* Helper function for optimize_agr_copyprop.
1582 : For aggregate copies in USE_STMT, see if DEST
1583 : is on the lhs of USE_STMT and replace it with SRC. */
1584 : static void
1585 991881 : optimize_agr_copyprop_1 (gimple *stmt, gimple *use_stmt,
1586 : tree dest, tree src)
1587 : {
1588 991881 : gcc_assert (gimple_assign_load_p (use_stmt)
1589 : && gimple_store_p (use_stmt));
1590 1983762 : if (gimple_has_volatile_ops (use_stmt))
1591 603749 : return;
1592 991880 : tree dest2 = gimple_assign_lhs (use_stmt);
1593 991880 : tree src2 = gimple_assign_rhs1 (use_stmt);
1594 : /* If the new store is `src2 = src2;` skip over it. */
1595 991880 : if (same_for_assignment (src2, dest2))
1596 : return;
1597 991317 : src = new_src_based_on_copy (src2, dest, src);
1598 991317 : if (!src)
1599 : return;
1600 : /* For 2 memory references and using a temporary to do the copy,
1601 : don't remove the temporary as the 2 memory references might overlap.
1602 : Note t does not need to be decl as it could be field.
1603 : See PR 22237 for full details.
1604 : E.g.
1605 : t = *a; #DEST = SRC;
1606 : *b = t; #DEST2 = SRC2;
1607 : Cannot be convert into
1608 : t = *a;
1609 : *b = *a;
1610 : Though the following is allowed to be done:
1611 : t = *a;
1612 : *a = t;
1613 : And convert it into:
1614 : t = *a;
1615 : *a = *a;
1616 : */
1617 416372 : if (!operand_equal_p (dest2, src, 0)
1618 416372 : && !DECL_P (dest2) && !DECL_P (src))
1619 : {
1620 : /* If *a and *b have the same base see if
1621 : the offset between the two is greater than
1622 : or equal to the size of the type. */
1623 31496 : poly_int64 offset1, offset2;
1624 31496 : tree len = TYPE_SIZE_UNIT (TREE_TYPE (src));
1625 31496 : if (len == NULL_TREE
1626 31496 : || !tree_fits_poly_int64_p (len))
1627 28240 : return;
1628 31496 : tree base1 = get_addr_base_and_unit_offset (dest2, &offset1);
1629 31496 : tree base2 = get_addr_base_and_unit_offset (src, &offset2);
1630 31496 : poly_int64 size = tree_to_poly_int64 (len);
1631 : /* If the bases are 2 different decls,
1632 : then there can be no overlapping. */
1633 31496 : if (base1 && base2
1634 30660 : && DECL_P (base1) && DECL_P (base2)
1635 1852 : && base1 != base2)
1636 : ;
1637 : /* If we can't figure out the base or the bases are
1638 : not equal then fall back to an alignment check. */
1639 29868 : else if (!base1
1640 29868 : || !base2
1641 29868 : || !operand_equal_p (base1, base2))
1642 : {
1643 29501 : unsigned int align1 = get_object_alignment (src);
1644 29501 : unsigned int align2 = get_object_alignment (dest2);
1645 29501 : align1 /= BITS_PER_UNIT;
1646 29501 : align2 /= BITS_PER_UNIT;
1647 : /* If the alignment of either object is less
1648 : than the size then there is a possibility
1649 : of overlapping. */
1650 29501 : if (maybe_lt (align1, size)
1651 29501 : || maybe_lt (align2, size))
1652 28240 : return;
1653 : }
1654 : /* Make sure [offset1, offset1 + len - 1] does
1655 : not overlap with [offset2, offset2 + len - 1],
1656 : it is ok if they are at the same location though. */
1657 367 : else if (ranges_maybe_overlap_p (offset1, size, offset2, size)
1658 367 : && !known_eq (offset2, offset1))
1659 : return;
1660 : }
1661 :
1662 388132 : if (dump_file && (dump_flags & TDF_DETAILS))
1663 : {
1664 11 : fprintf (dump_file, "Simplified\n ");
1665 11 : print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
1666 11 : fprintf (dump_file, "after previous\n ");
1667 11 : print_gimple_stmt (dump_file, stmt, 0, dump_flags);
1668 : }
1669 388132 : gimple *orig_stmt = use_stmt;
1670 388132 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
1671 388132 : gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (src));
1672 388132 : update_stmt (use_stmt);
1673 :
1674 388132 : if (dump_file && (dump_flags & TDF_DETAILS))
1675 : {
1676 11 : fprintf (dump_file, "into\n ");
1677 11 : print_gimple_stmt (dump_file, use_stmt, 0, dump_flags);
1678 : }
1679 388132 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, use_stmt))
1680 0 : bitmap_set_bit (to_purge, gimple_bb (stmt)->index);
1681 388132 : statistics_counter_event (cfun, "copy prop for aggregate", 1);
1682 : }
1683 :
1684 : /* Helper function for optimize_agr_copyprop_1, propagate aggregates
1685 : into the arguments of USE_STMT if the argument matches with DEST;
1686 : replacing it with SRC. */
1687 : static void
1688 693513 : optimize_agr_copyprop_arg (gimple *defstmt, gcall *call,
1689 : tree dest, tree src)
1690 : {
1691 693513 : bool changed = false;
1692 2317978 : for (unsigned arg = 0; arg < gimple_call_num_args (call); arg++)
1693 : {
1694 1624465 : tree *argptr = gimple_call_arg_ptr (call, arg);
1695 3065239 : if (TREE_CODE (*argptr) == SSA_NAME
1696 920210 : || is_gimple_min_invariant (*argptr)
1697 1808156 : || TYPE_VOLATILE (TREE_TYPE (*argptr)))
1698 1440774 : continue;
1699 183691 : tree newsrc = new_src_based_on_copy (*argptr, dest, src);
1700 183691 : if (!newsrc)
1701 111705 : continue;
1702 :
1703 71986 : if (dump_file && (dump_flags & TDF_DETAILS))
1704 : {
1705 9 : fprintf (dump_file, "Simplified\n ");
1706 9 : print_gimple_stmt (dump_file, call, 0, dump_flags);
1707 9 : fprintf (dump_file, "after previous\n ");
1708 9 : print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
1709 : }
1710 71986 : *argptr = unshare_expr (newsrc);
1711 71986 : changed = true;
1712 71986 : if (dump_file && (dump_flags & TDF_DETAILS))
1713 : {
1714 9 : fprintf (dump_file, "into\n ");
1715 9 : print_gimple_stmt (dump_file, call, 0, dump_flags);
1716 : }
1717 : }
1718 693513 : if (changed)
1719 71816 : update_stmt (call);
1720 693513 : }
1721 :
1722 : /* Helper function for optimize_agr_copyprop, propagate aggregates
1723 : into the return stmt USE if the operand of the return matches DEST;
1724 : replacing it with SRC. */
1725 : static void
1726 120037 : optimize_agr_copyprop_return (gimple *defstmt, greturn *use,
1727 : tree dest, tree src)
1728 : {
1729 120037 : tree rvalue = gimple_return_retval (use);
1730 120037 : if (!rvalue
1731 77265 : || TREE_CODE (rvalue) == SSA_NAME
1732 68649 : || is_gimple_min_invariant (rvalue)
1733 188287 : || TYPE_VOLATILE (TREE_TYPE (rvalue)))
1734 51788 : return;
1735 :
1736 : /* `return <retval>;` is already the best it could be.
1737 : Likewise `return *<retval>_N(D)`. */
1738 68249 : if (TREE_CODE (rvalue) == RESULT_DECL
1739 68249 : || (TREE_CODE (rvalue) == MEM_REF
1740 0 : && TREE_CODE (TREE_OPERAND (rvalue, 0)) == SSA_NAME
1741 0 : && TREE_CODE (SSA_NAME_VAR (TREE_OPERAND (rvalue, 0)))
1742 : == RESULT_DECL))
1743 : return;
1744 47257 : tree newsrc = new_src_based_on_copy (rvalue, dest, src);
1745 47257 : if (!newsrc)
1746 : return;
1747 : /* Currently only support non-global vars.
1748 : See PR 124099 on enumtls not supporting expanding for GIMPLE_RETURN.
1749 : FIXME: could support VCEs too? */
1750 47175 : if (!VAR_P (newsrc) || is_global_var (newsrc))
1751 : return;
1752 22694 : if (dump_file && (dump_flags & TDF_DETAILS))
1753 : {
1754 1 : fprintf (dump_file, "Simplified\n ");
1755 1 : print_gimple_stmt (dump_file, use, 0, dump_flags);
1756 1 : fprintf (dump_file, "after previous\n ");
1757 1 : print_gimple_stmt (dump_file, defstmt, 0, dump_flags);
1758 : }
1759 22694 : gimple_return_set_retval (use, newsrc);
1760 22694 : if (dump_file && (dump_flags & TDF_DETAILS))
1761 : {
1762 1 : fprintf (dump_file, "into\n ");
1763 1 : print_gimple_stmt (dump_file, use, 0, dump_flags);
1764 : }
1765 22694 : update_stmt (use);
1766 : }
1767 :
1768 : /* Optimizes
1769 : DEST = SRC;
1770 : DEST2 = DEST; # DEST2 = SRC2;
1771 : into
1772 : DEST = SRC;
1773 : DEST2 = SRC;
1774 : STMT is the first statement and SRC is the common
1775 : between the statements.
1776 :
1777 : Also optimizes:
1778 : DEST = SRC;
1779 : call_func(..., DEST, ...);
1780 : into:
1781 : DEST = SRC;
1782 : call_func(..., SRC, ...);
1783 :
1784 : */
1785 : static void
1786 3774809 : optimize_agr_copyprop (gimple *stmt)
1787 : {
1788 7549618 : if (gimple_has_volatile_ops (stmt))
1789 411437 : return;
1790 :
1791 : /* Can't prop if the statement could throw. */
1792 3773598 : if (stmt_could_throw_p (cfun, stmt))
1793 : return;
1794 :
1795 3365955 : tree dest = gimple_assign_lhs (stmt);
1796 3365955 : tree src = gimple_assign_rhs1 (stmt);
1797 : /* If the statement is `src = src;` then ignore it. */
1798 3365955 : if (same_for_assignment (dest, src))
1799 : return;
1800 :
1801 3363372 : tree vdef = gimple_vdef (stmt);
1802 3363372 : imm_use_iterator iter;
1803 3363372 : gimple *use_stmt;
1804 13107681 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, vdef)
1805 : {
1806 6380937 : if (gimple_assign_load_p (use_stmt)
1807 6380937 : && gimple_store_p (use_stmt))
1808 991881 : optimize_agr_copyprop_1 (stmt, use_stmt, dest, src);
1809 5389056 : else if (is_gimple_call (use_stmt))
1810 693513 : optimize_agr_copyprop_arg (stmt, as_a<gcall*>(use_stmt), dest, src);
1811 4695543 : else if (is_a<greturn*> (use_stmt))
1812 120037 : optimize_agr_copyprop_return (stmt, as_a<greturn*>(use_stmt), dest, src);
1813 3363372 : }
1814 : }
1815 :
1816 : /* Simple DSE of the lhs from a clobber STMT.
1817 : This is used mostly to clean up from optimize_agr_copyprop and
1818 : to remove (exactly one) extra copy that might later on confuse SRA.
1819 : An example is:
1820 : ;; write to a and such.
1821 : b = a; // This statement is to be removed
1822 : b = {CLOBBER};
1823 : SRA will totally scalarize b (which means also a) here for the extra copy
1824 : which is not something welcomed. So removing the copy will
1825 : allow SRA to move the scalarization of a further down or not at all.
1826 : */
1827 : static void
1828 6885349 : do_simple_agr_dse (gassign *stmt, bool full_walk)
1829 : {
1830 : /* Don't do this while in -Og as we want to keep around the copy
1831 : for debuggability. */
1832 6885349 : if (optimize_debug)
1833 4768648 : return;
1834 6881956 : ao_ref read;
1835 6881956 : basic_block bb = gimple_bb (stmt);
1836 6881956 : tree lhs = gimple_assign_lhs (stmt);
1837 : /* Only handle clobbers of a full decl. */
1838 6881956 : if (!DECL_P (lhs))
1839 : return;
1840 6199004 : ao_ref_init (&read, lhs);
1841 6199004 : tree vuse = gimple_vuse (stmt);
1842 6199004 : unsigned limit = full_walk ? param_sccvn_max_alias_queries_per_access : 4;
1843 15907401 : while (limit)
1844 : {
1845 15894818 : gimple *ostmt = SSA_NAME_DEF_STMT (vuse);
1846 : /* Don't handle phis, just declare to be done. */
1847 15894818 : if (is_a<gphi*>(ostmt) || gimple_nop_p (ostmt))
1848 : break;
1849 13790700 : basic_block obb = gimple_bb (ostmt);
1850 : /* If the clobber is not fully dominating the statement define,
1851 : then it is not "simple" to detect if the define is fully clobbered. */
1852 13790700 : if (obb != bb && !dominated_by_p (CDI_DOMINATORS, bb, obb))
1853 4082303 : return;
1854 13790700 : gimple *use_stmt;
1855 13790700 : imm_use_iterator iter;
1856 55792959 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, gimple_vdef (ostmt))
1857 : {
1858 16273071 : basic_block ubb = gimple_bb (use_stmt);
1859 16273071 : if (stmt == use_stmt)
1860 4726625 : continue;
1861 : /* If the use is a clobber for lhs,
1862 : then it can be safely skipped; this happens with eh
1863 : and sometimes jump threading. */
1864 11546446 : if (gimple_clobber_p (use_stmt)
1865 11546446 : && lhs == gimple_assign_lhs (use_stmt))
1866 158634 : continue;
1867 : /* If the use is a phi and it is single use then check if that single use
1868 : is a clobber and lhs is the same. */
1869 11387812 : if (gphi *use_phi = dyn_cast<gphi*>(use_stmt))
1870 : {
1871 339211 : use_operand_p ou;
1872 339211 : gimple *ostmt;
1873 339211 : if (single_imm_use (gimple_phi_result (use_phi), &ou, &ostmt)
1874 282490 : && gimple_clobber_p (ostmt)
1875 563518 : && lhs == gimple_assign_lhs (ostmt))
1876 65840 : continue;
1877 : /* A phi node will never be dominating the clobber. */
1878 273371 : return;
1879 : }
1880 : /* The use needs to be dominating the clobber. */
1881 1382089 : if ((ubb != bb && !dominated_by_p (CDI_DOMINATORS, bb, ubb))
1882 11750723 : || ref_maybe_used_by_stmt_p (use_stmt, &read, false))
1883 1137786 : return;
1884 : /* Count the above alias lookup towards the limit. */
1885 9910815 : limit--;
1886 9910815 : if (limit == 0)
1887 : return;
1888 1852212 : }
1889 11938488 : vuse = gimple_vuse (ostmt);
1890 : /* This is a call with an assignment to the clobber decl,
1891 : remove the lhs or the whole stmt if it was pure/const. */
1892 11938488 : if (is_a <gcall*>(ostmt)
1893 11938488 : && lhs == gimple_call_lhs (ostmt))
1894 : {
1895 : /* Don't remove stores/statements that are needed for non-call
1896 : eh to work. */
1897 3934 : if (stmt_unremovable_because_of_non_call_eh_p (cfun, ostmt))
1898 : return;
1899 : /* If we delete a stmt that could throw, mark the block
1900 : in to_purge to cleanup afterwards. */
1901 3928 : if (stmt_could_throw_p (cfun, ostmt))
1902 941 : bitmap_set_bit (to_purge, obb->index);
1903 3928 : int flags = gimple_call_flags (ostmt);
1904 3928 : if ((flags & (ECF_PURE|ECF_CONST|ECF_NOVOPS))
1905 202 : && !(flags & (ECF_LOOPING_CONST_OR_PURE)))
1906 : {
1907 118 : gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
1908 118 : if (dump_file && (dump_flags & TDF_DETAILS))
1909 : {
1910 14 : fprintf (dump_file, "Removing dead call store stmt ");
1911 14 : print_gimple_stmt (dump_file, ostmt, 0);
1912 14 : fprintf (dump_file, "\n");
1913 : }
1914 118 : unlink_stmt_vdef (ostmt);
1915 118 : release_defs (ostmt);
1916 118 : gsi_remove (&gsi, true);
1917 118 : statistics_counter_event (cfun, "delete call dead store", 1);
1918 : /* Only remove the first store previous statement. */
1919 118 : return;
1920 : }
1921 : /* Make sure we do not remove a return slot we cannot reconstruct
1922 : later. */
1923 3810 : if (gimple_call_return_slot_opt_p (as_a <gcall *>(ostmt))
1924 3810 : && (TREE_ADDRESSABLE (TREE_TYPE (gimple_call_fntype (ostmt)))
1925 472 : || !poly_int_tree_p
1926 472 : (TYPE_SIZE (TREE_TYPE (gimple_call_fntype (ostmt))))))
1927 : return;
1928 588 : if (dump_file && (dump_flags & TDF_DETAILS))
1929 : {
1930 6 : fprintf (dump_file, "Removing lhs of call stmt ");
1931 6 : print_gimple_stmt (dump_file, ostmt, 0);
1932 6 : fprintf (dump_file, "\n");
1933 : }
1934 588 : gimple_call_set_lhs (ostmt, NULL_TREE);
1935 588 : update_stmt (ostmt);
1936 588 : statistics_counter_event (cfun, "removed lhs call", 1);
1937 588 : return;
1938 : }
1939 : /* This an assignment store to the clobbered decl,
1940 : then maybe remove it. */
1941 11934554 : if (is_a <gassign*>(ostmt)
1942 10098133 : && gimple_store_p (ostmt)
1943 10098133 : && !gimple_clobber_p (ostmt)
1944 14940982 : && lhs == gimple_assign_lhs (ostmt))
1945 : {
1946 : /* Don't remove stores/statements that are needed for non-call
1947 : eh to work. */
1948 150806 : if (stmt_unremovable_because_of_non_call_eh_p (cfun, ostmt))
1949 : return;
1950 : /* If we delete a stmt that could throw, mark the block
1951 : in to_purge to cleanup afterwards. */
1952 145722 : if (stmt_could_throw_p (cfun, ostmt))
1953 0 : bitmap_set_bit (to_purge, obb->index);
1954 145722 : gimple_stmt_iterator gsi = gsi_for_stmt (ostmt);
1955 145722 : if (dump_file && (dump_flags & TDF_DETAILS))
1956 : {
1957 12 : fprintf (dump_file, "Removing dead store stmt ");
1958 12 : print_gimple_stmt (dump_file, ostmt, 0);
1959 12 : fprintf (dump_file, "\n");
1960 : }
1961 145722 : unlink_stmt_vdef (ostmt);
1962 145722 : release_defs (ostmt);
1963 145722 : gsi_remove (&gsi, true);
1964 145722 : statistics_counter_event (cfun, "delete dead store", 1);
1965 : /* Only remove the first store previous statement. */
1966 145722 : return;
1967 : }
1968 : /* If the statement uses or maybe writes to the decl,
1969 : then nothing is to be removed. Don't know if the write
1970 : to the decl is partial write or a full one so the need
1971 : to stop.
1972 : e.g.
1973 : b.c = a;
1974 : Easier to stop here rather than do a full partial
1975 : dse of this statement.
1976 : b = {CLOBBER}; */
1977 11783748 : if (stmt_may_clobber_ref_p_1 (ostmt, &read, false)
1978 11783748 : || ref_maybe_used_by_stmt_p (ostmt, &read, false))
1979 2075351 : return;
1980 9708397 : limit--;
1981 : }
1982 : }
1983 :
1984 : /* Optimizes builtin memcmps for small constant sizes.
1985 : GSI_P is the GSI for the call. STMT is the call itself.
1986 : */
1987 :
1988 : static bool
1989 462059 : simplify_builtin_memcmp (gimple_stmt_iterator *gsi_p, gcall *stmt)
1990 : {
1991 : /* Make sure memcmp arguments are the correct type. */
1992 462059 : if (gimple_call_num_args (stmt) != 3)
1993 : return false;
1994 462059 : tree arg1 = gimple_call_arg (stmt, 0);
1995 462059 : tree arg2 = gimple_call_arg (stmt, 1);
1996 462059 : tree len = gimple_call_arg (stmt, 2);
1997 :
1998 462059 : if (!POINTER_TYPE_P (TREE_TYPE (arg1)))
1999 : return false;
2000 462059 : if (!POINTER_TYPE_P (TREE_TYPE (arg2)))
2001 : return false;
2002 462059 : if (!INTEGRAL_TYPE_P (TREE_TYPE (len)))
2003 : return false;
2004 :
2005 : /* The return value of the memcmp has to be used
2006 : equality comparison to zero. */
2007 462059 : tree res = gimple_call_lhs (stmt);
2008 :
2009 462059 : if (!res || !use_in_zero_equality (res))
2010 13850 : return false;
2011 :
2012 448209 : unsigned HOST_WIDE_INT leni;
2013 :
2014 448209 : if (tree_fits_uhwi_p (len)
2015 626544 : && (leni = tree_to_uhwi (len)) <= GET_MODE_SIZE (word_mode)
2016 528045 : && pow2p_hwi (leni))
2017 : {
2018 18964 : leni *= CHAR_TYPE_SIZE;
2019 18964 : unsigned align1 = get_pointer_alignment (arg1);
2020 18964 : unsigned align2 = get_pointer_alignment (arg2);
2021 18964 : unsigned align = MIN (align1, align2);
2022 18964 : scalar_int_mode mode;
2023 18964 : if (int_mode_for_size (leni, 1).exists (&mode)
2024 18964 : && (align >= leni || !targetm.slow_unaligned_access (mode, align)))
2025 : {
2026 18964 : location_t loc = gimple_location (stmt);
2027 18964 : tree type, off;
2028 18964 : type = build_nonstandard_integer_type (leni, 1);
2029 37928 : gcc_assert (known_eq (GET_MODE_BITSIZE (TYPE_MODE (type)), leni));
2030 18964 : tree ptrtype = build_pointer_type_for_mode (char_type_node,
2031 : ptr_mode, true);
2032 18964 : off = build_int_cst (ptrtype, 0);
2033 :
2034 : /* Create unaligned types if needed. */
2035 18964 : tree type1 = type, type2 = type;
2036 18964 : if (TYPE_ALIGN (type1) > align1)
2037 7647 : type1 = build_aligned_type (type1, align1);
2038 18964 : if (TYPE_ALIGN (type2) > align2)
2039 8145 : type2 = build_aligned_type (type2, align2);
2040 :
2041 18964 : arg1 = build2_loc (loc, MEM_REF, type1, arg1, off);
2042 18964 : arg2 = build2_loc (loc, MEM_REF, type2, arg2, off);
2043 18964 : tree tem1 = fold_const_aggregate_ref (arg1);
2044 18964 : if (tem1)
2045 219 : arg1 = tem1;
2046 18964 : tree tem2 = fold_const_aggregate_ref (arg2);
2047 18964 : if (tem2)
2048 7317 : arg2 = tem2;
2049 18964 : res = fold_convert_loc (loc, TREE_TYPE (res),
2050 : fold_build2_loc (loc, NE_EXPR,
2051 : boolean_type_node,
2052 : arg1, arg2));
2053 18964 : gimplify_and_update_call_from_tree (gsi_p, res);
2054 18964 : return true;
2055 : }
2056 : }
2057 :
2058 : /* Replace memcmp with memcmp_eq if the above fails. */
2059 429245 : if (DECL_FUNCTION_CODE (gimple_call_fndecl (stmt)) == BUILT_IN_MEMCMP_EQ)
2060 : return false;
2061 340144 : if (!fold_before_rtl_expansion_p ())
2062 : return false;
2063 89101 : gimple_call_set_fndecl (stmt, builtin_decl_explicit (BUILT_IN_MEMCMP_EQ));
2064 89101 : update_stmt (stmt);
2065 89101 : return true;
2066 : }
2067 :
2068 : /* Optimizes builtin memchrs for small constant sizes with a const string.
2069 : GSI_P is the GSI for the call. STMT is the call itself.
2070 : */
2071 :
2072 : static bool
2073 11652 : simplify_builtin_memchr (gimple_stmt_iterator *gsi_p, gcall *stmt)
2074 : {
2075 11652 : if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
2076 : return false;
2077 :
2078 11652 : if (gimple_call_num_args (stmt) != 3)
2079 : return false;
2080 :
2081 11652 : tree res = gimple_call_lhs (stmt);
2082 11652 : if (!res || !use_in_zero_equality (res))
2083 10243 : return false;
2084 :
2085 1409 : tree ptr = gimple_call_arg (stmt, 0);
2086 1409 : if (TREE_CODE (ptr) != ADDR_EXPR
2087 1409 : || TREE_CODE (TREE_OPERAND (ptr, 0)) != STRING_CST)
2088 : return false;
2089 :
2090 402 : unsigned HOST_WIDE_INT slen
2091 402 : = TREE_STRING_LENGTH (TREE_OPERAND (ptr, 0));
2092 : /* It must be a non-empty string constant. */
2093 402 : if (slen < 2)
2094 : return false;
2095 :
2096 : /* For -Os, only simplify strings with a single character. */
2097 398 : if (!optimize_bb_for_speed_p (gimple_bb (stmt))
2098 398 : && slen > 2)
2099 : return false;
2100 :
2101 382 : tree size = gimple_call_arg (stmt, 2);
2102 : /* Size must be a constant which is <= UNITS_PER_WORD and
2103 : <= the string length. */
2104 382 : if (!tree_fits_uhwi_p (size))
2105 : return false;
2106 :
2107 382 : unsigned HOST_WIDE_INT sz = tree_to_uhwi (size);
2108 383 : if (sz == 0 || sz > UNITS_PER_WORD || sz >= slen)
2109 : return false;
2110 :
2111 330 : tree ch = gimple_call_arg (stmt, 1);
2112 330 : location_t loc = gimple_location (stmt);
2113 330 : if (!useless_type_conversion_p (char_type_node,
2114 330 : TREE_TYPE (ch)))
2115 330 : ch = fold_convert_loc (loc, char_type_node, ch);
2116 330 : const char *p = TREE_STRING_POINTER (TREE_OPERAND (ptr, 0));
2117 330 : unsigned int isize = sz;
2118 330 : tree *op = XALLOCAVEC (tree, isize);
2119 1171 : for (unsigned int i = 0; i < isize; i++)
2120 : {
2121 841 : op[i] = build_int_cst (char_type_node, p[i]);
2122 841 : op[i] = fold_build2_loc (loc, EQ_EXPR, boolean_type_node,
2123 : op[i], ch);
2124 : }
2125 841 : for (unsigned int i = isize - 1; i >= 1; i--)
2126 511 : op[i - 1] = fold_convert_loc (loc, boolean_type_node,
2127 : fold_build2_loc (loc,
2128 : BIT_IOR_EXPR,
2129 : boolean_type_node,
2130 511 : op[i - 1],
2131 511 : op[i]));
2132 330 : res = fold_convert_loc (loc, TREE_TYPE (res), op[0]);
2133 330 : gimplify_and_update_call_from_tree (gsi_p, res);
2134 330 : return true;
2135 : }
2136 :
2137 : /* *GSI_P is a GIMPLE_CALL to a builtin function.
2138 : Optimize
2139 : memcpy (p, "abcd", 4); // STMT1
2140 : memset (p + 4, ' ', 3); // STMT2
2141 : into
2142 : memcpy (p, "abcd ", 7);
2143 : call if the latter can be stored by pieces during expansion.
2144 : */
2145 :
2146 : static bool
2147 109778 : simplify_builtin_memcpy_memset (gimple_stmt_iterator *gsi_p, gcall *stmt2)
2148 : {
2149 109778 : if (gimple_call_num_args (stmt2) != 3
2150 109778 : || gimple_call_lhs (stmt2)
2151 : || CHAR_BIT != 8
2152 109778 : || BITS_PER_UNIT != 8)
2153 : return false;
2154 :
2155 210816 : tree vuse = gimple_vuse (stmt2);
2156 103436 : if (vuse == NULL)
2157 : return false;
2158 103420 : gimple *stmt1 = SSA_NAME_DEF_STMT (vuse);
2159 :
2160 103420 : tree callee1;
2161 103420 : tree ptr1, src1, str1, off1, len1, lhs1;
2162 103420 : tree ptr2 = gimple_call_arg (stmt2, 0);
2163 103420 : tree val2 = gimple_call_arg (stmt2, 1);
2164 103420 : tree len2 = gimple_call_arg (stmt2, 2);
2165 103420 : tree diff, vdef, new_str_cst;
2166 103420 : gimple *use_stmt;
2167 103420 : unsigned int ptr1_align;
2168 103420 : unsigned HOST_WIDE_INT src_len;
2169 103420 : char *src_buf;
2170 103420 : use_operand_p use_p;
2171 :
2172 103420 : if (!tree_fits_shwi_p (val2)
2173 99071 : || !tree_fits_uhwi_p (len2)
2174 166564 : || compare_tree_int (len2, 1024) == 1)
2175 45360 : return false;
2176 :
2177 58060 : if (is_gimple_call (stmt1))
2178 : {
2179 : /* If first stmt is a call, it needs to be memcpy
2180 : or mempcpy, with string literal as second argument and
2181 : constant length. */
2182 30068 : callee1 = gimple_call_fndecl (stmt1);
2183 30068 : if (callee1 == NULL_TREE
2184 29952 : || !fndecl_built_in_p (callee1, BUILT_IN_NORMAL)
2185 56512 : || gimple_call_num_args (stmt1) != 3)
2186 : return false;
2187 25152 : if (DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMCPY
2188 25152 : && DECL_FUNCTION_CODE (callee1) != BUILT_IN_MEMPCPY)
2189 : return false;
2190 10984 : ptr1 = gimple_call_arg (stmt1, 0);
2191 10984 : src1 = gimple_call_arg (stmt1, 1);
2192 10984 : len1 = gimple_call_arg (stmt1, 2);
2193 10984 : lhs1 = gimple_call_lhs (stmt1);
2194 10984 : if (!tree_fits_uhwi_p (len1))
2195 : return false;
2196 10897 : str1 = string_constant (src1, &off1, NULL, NULL);
2197 10897 : if (str1 == NULL_TREE)
2198 : return false;
2199 5000 : if (!tree_fits_uhwi_p (off1)
2200 5000 : || compare_tree_int (off1, TREE_STRING_LENGTH (str1) - 1) > 0
2201 5000 : || compare_tree_int (len1, TREE_STRING_LENGTH (str1)
2202 5000 : - tree_to_uhwi (off1)) > 0
2203 5000 : || TREE_CODE (TREE_TYPE (str1)) != ARRAY_TYPE
2204 15000 : || TYPE_MODE (TREE_TYPE (TREE_TYPE (str1)))
2205 5000 : != TYPE_MODE (char_type_node))
2206 0 : return false;
2207 : }
2208 27992 : else if (gimple_assign_single_p (stmt1))
2209 : {
2210 : /* Otherwise look for length 1 memcpy optimized into
2211 : assignment. */
2212 17145 : ptr1 = gimple_assign_lhs (stmt1);
2213 17145 : src1 = gimple_assign_rhs1 (stmt1);
2214 17145 : if (TREE_CODE (ptr1) != MEM_REF
2215 3517 : || TYPE_MODE (TREE_TYPE (ptr1)) != TYPE_MODE (char_type_node)
2216 18179 : || !tree_fits_shwi_p (src1))
2217 16793 : return false;
2218 352 : ptr1 = build_fold_addr_expr (ptr1);
2219 352 : STRIP_USELESS_TYPE_CONVERSION (ptr1);
2220 352 : callee1 = NULL_TREE;
2221 352 : len1 = size_one_node;
2222 352 : lhs1 = NULL_TREE;
2223 352 : off1 = size_zero_node;
2224 352 : str1 = NULL_TREE;
2225 : }
2226 : else
2227 : return false;
2228 :
2229 5352 : diff = constant_pointer_difference (ptr1, ptr2);
2230 5352 : if (diff == NULL && lhs1 != NULL)
2231 : {
2232 7 : diff = constant_pointer_difference (lhs1, ptr2);
2233 7 : if (DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
2234 7 : && diff != NULL)
2235 7 : diff = size_binop (PLUS_EXPR, diff,
2236 : fold_convert (sizetype, len1));
2237 : }
2238 : /* If the difference between the second and first destination pointer
2239 : is not constant, or is bigger than memcpy length, bail out. */
2240 5352 : if (diff == NULL
2241 4466 : || !tree_fits_uhwi_p (diff)
2242 4466 : || tree_int_cst_lt (len1, diff)
2243 9562 : || compare_tree_int (diff, 1024) == 1)
2244 1142 : return false;
2245 :
2246 : /* Use maximum of difference plus memset length and memcpy length
2247 : as the new memcpy length, if it is too big, bail out. */
2248 4210 : src_len = tree_to_uhwi (diff);
2249 4210 : src_len += tree_to_uhwi (len2);
2250 4210 : if (src_len < tree_to_uhwi (len1))
2251 : src_len = tree_to_uhwi (len1);
2252 4210 : if (src_len > 1024)
2253 : return false;
2254 :
2255 : /* If mempcpy value is used elsewhere, bail out, as mempcpy
2256 : with bigger length will return different result. */
2257 4210 : if (lhs1 != NULL_TREE
2258 64 : && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY
2259 4217 : && (TREE_CODE (lhs1) != SSA_NAME
2260 7 : || !single_imm_use (lhs1, &use_p, &use_stmt)
2261 7 : || use_stmt != stmt2))
2262 0 : return false;
2263 :
2264 : /* If anything reads memory in between memcpy and memset
2265 : call, the modified memcpy call might change it. */
2266 4210 : vdef = gimple_vdef (stmt1);
2267 4210 : if (vdef != NULL
2268 4210 : && (!single_imm_use (vdef, &use_p, &use_stmt)
2269 3455 : || use_stmt != stmt2))
2270 : return false;
2271 :
2272 3455 : ptr1_align = get_pointer_alignment (ptr1);
2273 : /* Construct the new source string literal. */
2274 3455 : src_buf = XALLOCAVEC (char, src_len + 1);
2275 3455 : if (callee1)
2276 3294 : memcpy (src_buf,
2277 3294 : TREE_STRING_POINTER (str1) + tree_to_uhwi (off1),
2278 : tree_to_uhwi (len1));
2279 : else
2280 161 : src_buf[0] = tree_to_shwi (src1);
2281 3455 : memset (src_buf + tree_to_uhwi (diff),
2282 3455 : tree_to_shwi (val2), tree_to_uhwi (len2));
2283 3455 : src_buf[src_len] = '\0';
2284 : /* Neither builtin_strncpy_read_str nor builtin_memcpy_read_str
2285 : handle embedded '\0's. */
2286 3455 : if (strlen (src_buf) != src_len)
2287 : return false;
2288 3365 : rtl_profile_for_bb (gimple_bb (stmt2));
2289 : /* If the new memcpy wouldn't be emitted by storing the literal
2290 : by pieces, this optimization might enlarge .rodata too much,
2291 : as commonly used string literals couldn't be shared any
2292 : longer. */
2293 3365 : if (!can_store_by_pieces (src_len,
2294 : builtin_strncpy_read_str,
2295 : src_buf, ptr1_align, false))
2296 : return false;
2297 :
2298 2525 : new_str_cst = build_string_literal (src_len, src_buf);
2299 2525 : if (callee1)
2300 : {
2301 : /* If STMT1 is a mem{,p}cpy call, adjust it and remove
2302 : memset call. */
2303 2398 : if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
2304 7 : gimple_call_set_lhs (stmt1, NULL_TREE);
2305 2398 : gimple_call_set_arg (stmt1, 1, new_str_cst);
2306 2398 : gimple_call_set_arg (stmt1, 2,
2307 2398 : build_int_cst (TREE_TYPE (len1), src_len));
2308 2398 : update_stmt (stmt1);
2309 2398 : unlink_stmt_vdef (stmt2);
2310 2398 : gsi_replace (gsi_p, gimple_build_nop (), false);
2311 2398 : fwprop_invalidate_lattice (gimple_get_lhs (stmt2));
2312 2398 : release_defs (stmt2);
2313 2398 : if (lhs1 && DECL_FUNCTION_CODE (callee1) == BUILT_IN_MEMPCPY)
2314 : {
2315 7 : fwprop_invalidate_lattice (lhs1);
2316 7 : release_ssa_name (lhs1);
2317 : }
2318 2398 : return true;
2319 : }
2320 : else
2321 : {
2322 : /* Otherwise, if STMT1 is length 1 memcpy optimized into
2323 : assignment, remove STMT1 and change memset call into
2324 : memcpy call. */
2325 127 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt1);
2326 :
2327 127 : if (!is_gimple_val (ptr1))
2328 12 : ptr1 = force_gimple_operand_gsi (gsi_p, ptr1, true, NULL_TREE,
2329 : true, GSI_SAME_STMT);
2330 127 : tree fndecl = builtin_decl_explicit (BUILT_IN_MEMCPY);
2331 127 : gimple_call_set_fndecl (stmt2, fndecl);
2332 127 : gimple_call_set_fntype (stmt2,
2333 127 : TREE_TYPE (fndecl));
2334 127 : gimple_call_set_arg (stmt2, 0, ptr1);
2335 127 : gimple_call_set_arg (stmt2, 1, new_str_cst);
2336 127 : gimple_call_set_arg (stmt2, 2,
2337 127 : build_int_cst (TREE_TYPE (len2), src_len));
2338 127 : unlink_stmt_vdef (stmt1);
2339 127 : gsi_remove (&gsi, true);
2340 127 : fwprop_invalidate_lattice (gimple_get_lhs (stmt1));
2341 127 : release_defs (stmt1);
2342 127 : update_stmt (stmt2);
2343 127 : return false;
2344 : }
2345 : }
2346 :
2347 :
2348 : /* Try to optimize out __builtin_stack_restore. Optimize it out
2349 : if there is another __builtin_stack_restore in the same basic
2350 : block and no calls or ASM_EXPRs are in between, or if this block's
2351 : only outgoing edge is to EXIT_BLOCK and there are no calls or
2352 : ASM_EXPRs after this __builtin_stack_restore.
2353 : Note restore right before a noreturn function is not needed.
2354 : And skip some cheap calls that will most likely become an instruction.
2355 : Restoring the stack before a call is important to be able to keep
2356 : stack usage down so that call does not run out of stack. */
2357 :
2358 :
2359 : static bool
2360 10372 : optimize_stack_restore (gimple_stmt_iterator *gsi, gimple *call)
2361 : {
2362 10372 : if (!fold_before_rtl_expansion_p ())
2363 : return false;
2364 2526 : tree callee;
2365 2526 : gimple *stmt;
2366 :
2367 2526 : basic_block bb = gsi_bb (*gsi);
2368 :
2369 2526 : if (gimple_call_num_args (call) != 1
2370 2526 : || TREE_CODE (gimple_call_arg (call, 0)) != SSA_NAME
2371 5052 : || !POINTER_TYPE_P (TREE_TYPE (gimple_call_arg (call, 0))))
2372 : return false;
2373 :
2374 2526 : gimple_stmt_iterator i = *gsi;
2375 6291 : for (gsi_next (&i); !gsi_end_p (i); gsi_next (&i))
2376 : {
2377 4220 : stmt = gsi_stmt (i);
2378 4220 : if (is_a<gasm*> (stmt))
2379 : return false;
2380 4219 : gcall *call = dyn_cast<gcall*>(stmt);
2381 4219 : if (!call)
2382 3556 : continue;
2383 :
2384 : /* We can remove the restore in front of noreturn
2385 : calls. Since the restore will happen either
2386 : via an unwind/longjmp or not at all. */
2387 663 : if (gimple_call_noreturn_p (call))
2388 : break;
2389 :
2390 : /* Internal calls are ok, to bypass
2391 : check first since fndecl will be null. */
2392 647 : if (gimple_call_internal_p (call))
2393 1 : continue;
2394 :
2395 646 : callee = gimple_call_fndecl (call);
2396 : /* Non-builtin calls are not ok. */
2397 646 : if (!callee
2398 646 : || !fndecl_built_in_p (callee))
2399 : return false;
2400 :
2401 : /* Do not remove stack updates before strub leave. */
2402 570 : if (fndecl_built_in_p (callee, BUILT_IN___STRUB_LEAVE)
2403 : /* Alloca calls are not ok either. */
2404 570 : || fndecl_builtin_alloc_p (callee))
2405 : return false;
2406 :
2407 355 : if (fndecl_built_in_p (callee, BUILT_IN_STACK_RESTORE))
2408 52 : goto second_stack_restore;
2409 :
2410 : /* If not a simple or inexpensive builtin, then it is not ok either. */
2411 303 : if (!is_simple_builtin (callee)
2412 303 : && !is_inexpensive_builtin (callee))
2413 : return false;
2414 : }
2415 :
2416 : /* Allow one successor of the exit block, or zero successors. */
2417 2087 : switch (EDGE_COUNT (bb->succs))
2418 : {
2419 : case 0:
2420 : break;
2421 2000 : case 1:
2422 2000 : if (single_succ_edge (bb)->dest != EXIT_BLOCK_PTR_FOR_FN (cfun))
2423 : return false;
2424 : break;
2425 : default:
2426 : return false;
2427 : }
2428 1730 : second_stack_restore:
2429 :
2430 : /* If there's exactly one use, then zap the call to __builtin_stack_save.
2431 : If there are multiple uses, then the last one should remove the call.
2432 : In any case, whether the call to __builtin_stack_save can be removed
2433 : or not is irrelevant to removing the call to __builtin_stack_restore. */
2434 1730 : if (has_single_use (gimple_call_arg (call, 0)))
2435 : {
2436 1560 : gimple *stack_save = SSA_NAME_DEF_STMT (gimple_call_arg (call, 0));
2437 1560 : if (is_gimple_call (stack_save))
2438 : {
2439 1558 : callee = gimple_call_fndecl (stack_save);
2440 1558 : if (callee && fndecl_built_in_p (callee, BUILT_IN_STACK_SAVE))
2441 : {
2442 1558 : gimple_stmt_iterator stack_save_gsi;
2443 1558 : tree rhs;
2444 :
2445 1558 : stack_save_gsi = gsi_for_stmt (stack_save);
2446 1558 : rhs = build_int_cst (TREE_TYPE (gimple_call_arg (call, 0)), 0);
2447 1558 : replace_call_with_value (&stack_save_gsi, rhs);
2448 : }
2449 : }
2450 : }
2451 :
2452 : /* No effect, so the statement will be deleted. */
2453 1730 : replace_call_with_value (gsi, NULL_TREE);
2454 1730 : return true;
2455 : }
2456 :
2457 : /* *GSI_P is a GIMPLE_CALL to a builtin function.
2458 : Optimize
2459 : memcpy (p, "abcd", 4);
2460 : memset (p + 4, ' ', 3);
2461 : into
2462 : memcpy (p, "abcd ", 7);
2463 : call if the latter can be stored by pieces during expansion.
2464 :
2465 : Optimize
2466 : memchr ("abcd", a, 4) == 0;
2467 : or
2468 : memchr ("abcd", a, 4) != 0;
2469 : to
2470 : (a == 'a' || a == 'b' || a == 'c' || a == 'd') == 0
2471 : or
2472 : (a == 'a' || a == 'b' || a == 'c' || a == 'd') != 0
2473 :
2474 : Also canonicalize __atomic_fetch_op (p, x, y) op x
2475 : to __atomic_op_fetch (p, x, y) or
2476 : __atomic_op_fetch (p, x, y) iop x
2477 : to __atomic_fetch_op (p, x, y) when possible (also __sync). */
2478 :
2479 : static bool
2480 6132843 : simplify_builtin_call (gimple_stmt_iterator *gsi_p, tree callee2, bool full_walk)
2481 : {
2482 6132843 : gimple *stmt2 = gsi_stmt (*gsi_p);
2483 6132843 : enum built_in_function other_atomic = END_BUILTINS;
2484 6132843 : enum tree_code atomic_op = ERROR_MARK;
2485 :
2486 6132843 : switch (DECL_FUNCTION_CODE (callee2))
2487 : {
2488 10372 : case BUILT_IN_STACK_RESTORE:
2489 10372 : return optimize_stack_restore (gsi_p, as_a<gcall*>(stmt2));
2490 462059 : case BUILT_IN_MEMCMP:
2491 462059 : case BUILT_IN_MEMCMP_EQ:
2492 462059 : return simplify_builtin_memcmp (gsi_p, as_a<gcall*>(stmt2));
2493 11652 : case BUILT_IN_MEMCHR:
2494 11652 : return simplify_builtin_memchr (gsi_p, as_a<gcall*>(stmt2));
2495 :
2496 109778 : case BUILT_IN_MEMSET:
2497 109778 : if (gimple_call_num_args (stmt2) == 3)
2498 : {
2499 : /* Try to prop the zeroing/value of the memset to memcpy
2500 : if the dest is an address and the value is a constant. */
2501 109778 : optimize_aggr_zeroprop (stmt2, full_walk);
2502 : }
2503 109778 : return simplify_builtin_memcpy_memset (gsi_p, as_a<gcall*>(stmt2));
2504 :
2505 : #define CASE_ATOMIC(NAME, OTHER, OP) \
2506 : case BUILT_IN_##NAME##_1: \
2507 : case BUILT_IN_##NAME##_2: \
2508 : case BUILT_IN_##NAME##_4: \
2509 : case BUILT_IN_##NAME##_8: \
2510 : case BUILT_IN_##NAME##_16: \
2511 : atomic_op = OP; \
2512 : other_atomic \
2513 : = (enum built_in_function) (BUILT_IN_##OTHER##_1 \
2514 : + (DECL_FUNCTION_CODE (callee2) \
2515 : - BUILT_IN_##NAME##_1)); \
2516 : goto handle_atomic_fetch_op;
2517 :
2518 48338 : CASE_ATOMIC (ATOMIC_FETCH_ADD, ATOMIC_ADD_FETCH, PLUS_EXPR)
2519 7125 : CASE_ATOMIC (ATOMIC_FETCH_SUB, ATOMIC_SUB_FETCH, MINUS_EXPR)
2520 2876 : CASE_ATOMIC (ATOMIC_FETCH_AND, ATOMIC_AND_FETCH, BIT_AND_EXPR)
2521 2895 : CASE_ATOMIC (ATOMIC_FETCH_XOR, ATOMIC_XOR_FETCH, BIT_XOR_EXPR)
2522 3823 : CASE_ATOMIC (ATOMIC_FETCH_OR, ATOMIC_OR_FETCH, BIT_IOR_EXPR)
2523 :
2524 2365 : CASE_ATOMIC (SYNC_FETCH_AND_ADD, SYNC_ADD_AND_FETCH, PLUS_EXPR)
2525 2004 : CASE_ATOMIC (SYNC_FETCH_AND_SUB, SYNC_SUB_AND_FETCH, MINUS_EXPR)
2526 1876 : CASE_ATOMIC (SYNC_FETCH_AND_AND, SYNC_AND_AND_FETCH, BIT_AND_EXPR)
2527 2144 : CASE_ATOMIC (SYNC_FETCH_AND_XOR, SYNC_XOR_AND_FETCH, BIT_XOR_EXPR)
2528 1987 : CASE_ATOMIC (SYNC_FETCH_AND_OR, SYNC_OR_AND_FETCH, BIT_IOR_EXPR)
2529 :
2530 14342 : CASE_ATOMIC (ATOMIC_ADD_FETCH, ATOMIC_FETCH_ADD, MINUS_EXPR)
2531 8552 : CASE_ATOMIC (ATOMIC_SUB_FETCH, ATOMIC_FETCH_SUB, PLUS_EXPR)
2532 2380 : CASE_ATOMIC (ATOMIC_XOR_FETCH, ATOMIC_FETCH_XOR, BIT_XOR_EXPR)
2533 :
2534 846 : CASE_ATOMIC (SYNC_ADD_AND_FETCH, SYNC_FETCH_AND_ADD, MINUS_EXPR)
2535 732 : CASE_ATOMIC (SYNC_SUB_AND_FETCH, SYNC_FETCH_AND_SUB, PLUS_EXPR)
2536 800 : CASE_ATOMIC (SYNC_XOR_AND_FETCH, SYNC_FETCH_AND_XOR, BIT_XOR_EXPR)
2537 :
2538 : #undef CASE_ATOMIC
2539 :
2540 103085 : handle_atomic_fetch_op:
2541 103085 : if (gimple_call_num_args (stmt2) >= 2 && gimple_call_lhs (stmt2))
2542 : {
2543 59625 : tree lhs2 = gimple_call_lhs (stmt2), lhsc = lhs2;
2544 59625 : tree arg = gimple_call_arg (stmt2, 1);
2545 59625 : gimple *use_stmt, *cast_stmt = NULL;
2546 59625 : use_operand_p use_p;
2547 59625 : tree ndecl = builtin_decl_explicit (other_atomic);
2548 :
2549 59625 : if (ndecl == NULL_TREE || !single_imm_use (lhs2, &use_p, &use_stmt))
2550 : break;
2551 :
2552 58496 : if (gimple_assign_cast_p (use_stmt))
2553 : {
2554 31011 : cast_stmt = use_stmt;
2555 31011 : lhsc = gimple_assign_lhs (cast_stmt);
2556 31011 : if (lhsc == NULL_TREE
2557 31011 : || !INTEGRAL_TYPE_P (TREE_TYPE (lhsc))
2558 30460 : || (TYPE_PRECISION (TREE_TYPE (lhsc))
2559 30460 : != TYPE_PRECISION (TREE_TYPE (lhs2)))
2560 59945 : || !single_imm_use (lhsc, &use_p, &use_stmt))
2561 : {
2562 2605 : use_stmt = cast_stmt;
2563 2605 : cast_stmt = NULL;
2564 2605 : lhsc = lhs2;
2565 : }
2566 : }
2567 :
2568 58496 : bool ok = false;
2569 58496 : tree oarg = NULL_TREE;
2570 58496 : enum tree_code ccode = ERROR_MARK;
2571 58496 : tree crhs1 = NULL_TREE, crhs2 = NULL_TREE;
2572 58496 : if (is_gimple_assign (use_stmt)
2573 58496 : && gimple_assign_rhs_code (use_stmt) == atomic_op)
2574 : {
2575 1416 : if (gimple_assign_rhs1 (use_stmt) == lhsc)
2576 1016 : oarg = gimple_assign_rhs2 (use_stmt);
2577 400 : else if (atomic_op != MINUS_EXPR)
2578 : oarg = gimple_assign_rhs1 (use_stmt);
2579 : }
2580 57080 : else if (atomic_op == MINUS_EXPR
2581 13188 : && is_gimple_assign (use_stmt)
2582 3612 : && gimple_assign_rhs_code (use_stmt) == PLUS_EXPR
2583 199 : && TREE_CODE (arg) == INTEGER_CST
2584 57279 : && (TREE_CODE (gimple_assign_rhs2 (use_stmt))
2585 : == INTEGER_CST))
2586 : {
2587 183 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
2588 183 : tree o = fold_convert (TREE_TYPE (lhs2),
2589 : gimple_assign_rhs2 (use_stmt));
2590 183 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
2591 : ok = true;
2592 : }
2593 56897 : else if (atomic_op == BIT_AND_EXPR || atomic_op == BIT_IOR_EXPR)
2594 : ;
2595 51655 : else if (gimple_code (use_stmt) == GIMPLE_COND)
2596 : {
2597 19457 : ccode = gimple_cond_code (use_stmt);
2598 19457 : crhs1 = gimple_cond_lhs (use_stmt);
2599 19457 : crhs2 = gimple_cond_rhs (use_stmt);
2600 : }
2601 32198 : else if (is_gimple_assign (use_stmt))
2602 : {
2603 9525 : if (gimple_assign_rhs_class (use_stmt) == GIMPLE_BINARY_RHS)
2604 : {
2605 3941 : ccode = gimple_assign_rhs_code (use_stmt);
2606 3941 : crhs1 = gimple_assign_rhs1 (use_stmt);
2607 3941 : crhs2 = gimple_assign_rhs2 (use_stmt);
2608 : }
2609 5584 : else if (gimple_assign_rhs_code (use_stmt) == COND_EXPR)
2610 : {
2611 0 : tree cond = gimple_assign_rhs1 (use_stmt);
2612 0 : if (COMPARISON_CLASS_P (cond))
2613 : {
2614 0 : ccode = TREE_CODE (cond);
2615 0 : crhs1 = TREE_OPERAND (cond, 0);
2616 0 : crhs2 = TREE_OPERAND (cond, 1);
2617 : }
2618 : }
2619 : }
2620 24414 : if (ccode == EQ_EXPR || ccode == NE_EXPR)
2621 : {
2622 : /* Deal with x - y == 0 or x ^ y == 0
2623 : being optimized into x == y and x + cst == 0
2624 : into x == -cst. */
2625 22214 : tree o = NULL_TREE;
2626 22214 : if (crhs1 == lhsc)
2627 : o = crhs2;
2628 133 : else if (crhs2 == lhsc)
2629 133 : o = crhs1;
2630 22214 : if (o && atomic_op != PLUS_EXPR)
2631 : oarg = o;
2632 10057 : else if (o
2633 10057 : && TREE_CODE (o) == INTEGER_CST
2634 10057 : && TREE_CODE (arg) == INTEGER_CST)
2635 : {
2636 9347 : tree a = fold_convert (TREE_TYPE (lhs2), arg);
2637 9347 : o = fold_convert (TREE_TYPE (lhs2), o);
2638 9347 : if (wi::to_wide (a) == wi::neg (wi::to_wide (o)))
2639 58496 : ok = true;
2640 : }
2641 : }
2642 58496 : if (oarg && !ok)
2643 : {
2644 13573 : if (operand_equal_p (arg, oarg, 0))
2645 : ok = true;
2646 12244 : else if (TREE_CODE (arg) == SSA_NAME
2647 2203 : && TREE_CODE (oarg) == SSA_NAME)
2648 : {
2649 745 : tree oarg2 = oarg;
2650 745 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (oarg)))
2651 : {
2652 104 : gimple *g = SSA_NAME_DEF_STMT (oarg);
2653 104 : oarg2 = gimple_assign_rhs1 (g);
2654 104 : if (TREE_CODE (oarg2) != SSA_NAME
2655 104 : || !INTEGRAL_TYPE_P (TREE_TYPE (oarg2))
2656 208 : || (TYPE_PRECISION (TREE_TYPE (oarg2))
2657 104 : != TYPE_PRECISION (TREE_TYPE (oarg))))
2658 : oarg2 = oarg;
2659 : }
2660 745 : if (gimple_assign_cast_p (SSA_NAME_DEF_STMT (arg)))
2661 : {
2662 544 : gimple *g = SSA_NAME_DEF_STMT (arg);
2663 544 : tree rhs1 = gimple_assign_rhs1 (g);
2664 : /* Handle e.g.
2665 : x.0_1 = (long unsigned int) x_4(D);
2666 : _2 = __atomic_fetch_add_8 (&vlong, x.0_1, 0);
2667 : _3 = (long int) _2;
2668 : _7 = x_4(D) + _3; */
2669 544 : if (rhs1 == oarg || rhs1 == oarg2)
2670 : ok = true;
2671 : /* Handle e.g.
2672 : x.18_1 = (short unsigned int) x_5(D);
2673 : _2 = (int) x.18_1;
2674 : _3 = __atomic_fetch_xor_2 (&vshort, _2, 0);
2675 : _4 = (short int) _3;
2676 : _8 = x_5(D) ^ _4;
2677 : This happens only for char/short. */
2678 160 : else if (TREE_CODE (rhs1) == SSA_NAME
2679 160 : && INTEGRAL_TYPE_P (TREE_TYPE (rhs1))
2680 320 : && (TYPE_PRECISION (TREE_TYPE (rhs1))
2681 160 : == TYPE_PRECISION (TREE_TYPE (lhs2))))
2682 : {
2683 160 : g = SSA_NAME_DEF_STMT (rhs1);
2684 160 : if (gimple_assign_cast_p (g)
2685 160 : && (gimple_assign_rhs1 (g) == oarg
2686 0 : || gimple_assign_rhs1 (g) == oarg2))
2687 : ok = true;
2688 : }
2689 : }
2690 745 : if (!ok && arg == oarg2)
2691 : /* Handle e.g.
2692 : _1 = __sync_fetch_and_add_4 (&v, x_5(D));
2693 : _2 = (int) _1;
2694 : x.0_3 = (int) x_5(D);
2695 : _7 = _2 + x.0_3; */
2696 : ok = true;
2697 : }
2698 : }
2699 :
2700 57167 : if (ok)
2701 : {
2702 2546 : tree new_lhs = make_ssa_name (TREE_TYPE (lhs2));
2703 2546 : gimple_call_set_lhs (stmt2, new_lhs);
2704 2546 : gimple_call_set_fndecl (stmt2, ndecl);
2705 2546 : gimple_stmt_iterator gsi = gsi_for_stmt (use_stmt);
2706 2546 : if (ccode == ERROR_MARK)
2707 2000 : gimple_assign_set_rhs_with_ops (&gsi, cast_stmt
2708 : ? NOP_EXPR : SSA_NAME,
2709 : new_lhs);
2710 : else
2711 : {
2712 1323 : crhs1 = new_lhs;
2713 1323 : crhs2 = build_zero_cst (TREE_TYPE (lhs2));
2714 1323 : if (gimple_code (use_stmt) == GIMPLE_COND)
2715 : {
2716 984 : gcond *cond_stmt = as_a <gcond *> (use_stmt);
2717 984 : gimple_cond_set_lhs (cond_stmt, crhs1);
2718 984 : gimple_cond_set_rhs (cond_stmt, crhs2);
2719 : }
2720 339 : else if (gimple_assign_rhs_class (use_stmt)
2721 : == GIMPLE_BINARY_RHS)
2722 : {
2723 339 : gimple_assign_set_rhs1 (use_stmt, crhs1);
2724 339 : gimple_assign_set_rhs2 (use_stmt, crhs2);
2725 : }
2726 : else
2727 : {
2728 0 : gcc_checking_assert (gimple_assign_rhs_code (use_stmt)
2729 : == COND_EXPR);
2730 0 : tree cond = build2 (ccode, boolean_type_node,
2731 : crhs1, crhs2);
2732 0 : gimple_assign_set_rhs1 (use_stmt, cond);
2733 : }
2734 : }
2735 2546 : update_stmt (use_stmt);
2736 2546 : if (atomic_op != BIT_AND_EXPR
2737 2546 : && atomic_op != BIT_IOR_EXPR
2738 2546 : && !stmt_ends_bb_p (stmt2))
2739 : {
2740 : /* For the benefit of debug stmts, emit stmt(s) to set
2741 : lhs2 to the value it had from the new builtin.
2742 : E.g. if it was previously:
2743 : lhs2 = __atomic_fetch_add_8 (ptr, arg, 0);
2744 : emit:
2745 : new_lhs = __atomic_add_fetch_8 (ptr, arg, 0);
2746 : lhs2 = new_lhs - arg;
2747 : We also keep cast_stmt if any in the IL for
2748 : the same reasons.
2749 : These stmts will be DCEd later and proper debug info
2750 : will be emitted.
2751 : This is only possible for reversible operations
2752 : (+/-/^) and without -fnon-call-exceptions. */
2753 2205 : gsi = gsi_for_stmt (stmt2);
2754 2205 : tree type = TREE_TYPE (lhs2);
2755 2205 : if (TREE_CODE (arg) == INTEGER_CST)
2756 1623 : arg = fold_convert (type, arg);
2757 582 : else if (!useless_type_conversion_p (type, TREE_TYPE (arg)))
2758 : {
2759 0 : tree narg = make_ssa_name (type);
2760 0 : gimple *g = gimple_build_assign (narg, NOP_EXPR, arg);
2761 0 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
2762 0 : arg = narg;
2763 : }
2764 2205 : enum tree_code rcode;
2765 2205 : switch (atomic_op)
2766 : {
2767 : case PLUS_EXPR: rcode = MINUS_EXPR; break;
2768 727 : case MINUS_EXPR: rcode = PLUS_EXPR; break;
2769 492 : case BIT_XOR_EXPR: rcode = atomic_op; break;
2770 0 : default: gcc_unreachable ();
2771 : }
2772 2205 : gimple *g = gimple_build_assign (lhs2, rcode, new_lhs, arg);
2773 2205 : gsi_insert_after (&gsi, g, GSI_NEW_STMT);
2774 2205 : update_stmt (stmt2);
2775 : }
2776 : else
2777 : {
2778 : /* For e.g.
2779 : lhs2 = __atomic_fetch_or_8 (ptr, arg, 0);
2780 : after we change it to
2781 : new_lhs = __atomic_or_fetch_8 (ptr, arg, 0);
2782 : there is no way to find out the lhs2 value (i.e.
2783 : what the atomic memory contained before the operation),
2784 : values of some bits are lost. We have checked earlier
2785 : that we don't have any non-debug users except for what
2786 : we are already changing, so we need to reset the
2787 : debug stmts and remove the cast_stmt if any. */
2788 341 : imm_use_iterator iter;
2789 676 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs2)
2790 335 : if (use_stmt != cast_stmt)
2791 : {
2792 168 : gcc_assert (is_gimple_debug (use_stmt));
2793 168 : gimple_debug_bind_reset_value (use_stmt);
2794 168 : update_stmt (use_stmt);
2795 341 : }
2796 341 : if (cast_stmt)
2797 : {
2798 167 : gsi = gsi_for_stmt (cast_stmt);
2799 167 : gsi_remove (&gsi, true);
2800 : }
2801 341 : update_stmt (stmt2);
2802 341 : release_ssa_name (lhs2);
2803 : }
2804 : }
2805 : }
2806 : break;
2807 :
2808 : default:
2809 : break;
2810 : }
2811 : return false;
2812 : }
2813 :
2814 : /* Given a ssa_name in NAME see if it was defined by an assignment and
2815 : set CODE to be the code and ARG1 to the first operand on the rhs and ARG2
2816 : to the second operand on the rhs. */
2817 :
2818 : static inline void
2819 17202706 : defcodefor_name (tree name, enum tree_code *code, tree *arg1, tree *arg2)
2820 : {
2821 17202706 : gimple *def;
2822 17202706 : enum tree_code code1;
2823 17202706 : tree arg11;
2824 17202706 : tree arg21;
2825 17202706 : tree arg31;
2826 17202706 : enum gimple_rhs_class grhs_class;
2827 :
2828 17202706 : code1 = TREE_CODE (name);
2829 17202706 : arg11 = name;
2830 17202706 : arg21 = NULL_TREE;
2831 17202706 : arg31 = NULL_TREE;
2832 17202706 : grhs_class = get_gimple_rhs_class (code1);
2833 :
2834 17202706 : if (code1 == SSA_NAME)
2835 : {
2836 11486936 : def = SSA_NAME_DEF_STMT (name);
2837 :
2838 11486936 : if (def && is_gimple_assign (def)
2839 18614879 : && can_propagate_from (def))
2840 : {
2841 4926943 : code1 = gimple_assign_rhs_code (def);
2842 4926943 : arg11 = gimple_assign_rhs1 (def);
2843 4926943 : arg21 = gimple_assign_rhs2 (def);
2844 4926943 : arg31 = gimple_assign_rhs3 (def);
2845 : }
2846 : }
2847 5715770 : else if (grhs_class != GIMPLE_SINGLE_RHS)
2848 0 : code1 = ERROR_MARK;
2849 :
2850 17202706 : *code = code1;
2851 17202706 : *arg1 = arg11;
2852 17202706 : if (arg2)
2853 17185470 : *arg2 = arg21;
2854 17202706 : if (arg31)
2855 2333 : *code = ERROR_MARK;
2856 17202706 : }
2857 :
2858 :
2859 : /* Recognize rotation patterns. Return true if a transformation
2860 : applied, otherwise return false.
2861 :
2862 : We are looking for X with unsigned type T with bitsize B, OP being
2863 : +, | or ^, some type T2 wider than T. For:
2864 : (X << CNT1) OP (X >> CNT2) iff CNT1 + CNT2 == B
2865 : ((T) ((T2) X << CNT1)) OP ((T) ((T2) X >> CNT2)) iff CNT1 + CNT2 == B
2866 :
2867 : transform these into:
2868 : X r<< CNT1
2869 :
2870 : Or for:
2871 : (X << Y) OP (X >> (B - Y))
2872 : (X << (int) Y) OP (X >> (int) (B - Y))
2873 : ((T) ((T2) X << Y)) OP ((T) ((T2) X >> (B - Y)))
2874 : ((T) ((T2) X << (int) Y)) OP ((T) ((T2) X >> (int) (B - Y)))
2875 : (X << Y) | (X >> ((-Y) & (B - 1)))
2876 : (X << (int) Y) | (X >> (int) ((-Y) & (B - 1)))
2877 : ((T) ((T2) X << Y)) | ((T) ((T2) X >> ((-Y) & (B - 1))))
2878 : ((T) ((T2) X << (int) Y)) | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
2879 :
2880 : transform these into (last 2 only if ranger can prove Y < B
2881 : or Y = N * B):
2882 : X r<< Y
2883 : or
2884 : X r<< (& & (B - 1))
2885 : The latter for the forms with T2 wider than T if ranger can't prove Y < B.
2886 :
2887 : Or for:
2888 : (X << (Y & (B - 1))) | (X >> ((-Y) & (B - 1)))
2889 : (X << (int) (Y & (B - 1))) | (X >> (int) ((-Y) & (B - 1)))
2890 : ((T) ((T2) X << (Y & (B - 1)))) | ((T) ((T2) X >> ((-Y) & (B - 1))))
2891 : ((T) ((T2) X << (int) (Y & (B - 1)))) \
2892 : | ((T) ((T2) X >> (int) ((-Y) & (B - 1))))
2893 :
2894 : transform these into:
2895 : X r<< (Y & (B - 1))
2896 :
2897 : Note, in the patterns with T2 type, the type of OP operands
2898 : might be even a signed type, but should have precision B.
2899 : Expressions with & (B - 1) should be recognized only if B is
2900 : a power of 2. */
2901 :
2902 : static bool
2903 10111666 : simplify_rotate (gimple_stmt_iterator *gsi)
2904 : {
2905 10111666 : gimple *stmt = gsi_stmt (*gsi);
2906 10111666 : tree arg[2], rtype, rotcnt = NULL_TREE;
2907 10111666 : tree def_arg1[2], def_arg2[2];
2908 10111666 : enum tree_code def_code[2];
2909 10111666 : tree lhs;
2910 10111666 : int i;
2911 10111666 : bool swapped_p = false;
2912 10111666 : gimple *g;
2913 10111666 : gimple *def_arg_stmt[2] = { NULL, NULL };
2914 10111666 : int wider_prec = 0;
2915 10111666 : bool add_masking = false;
2916 :
2917 10111666 : arg[0] = gimple_assign_rhs1 (stmt);
2918 10111666 : arg[1] = gimple_assign_rhs2 (stmt);
2919 10111666 : rtype = TREE_TYPE (arg[0]);
2920 :
2921 : /* Only create rotates in complete modes. Other cases are not
2922 : expanded properly. */
2923 10111666 : if (!INTEGRAL_TYPE_P (rtype)
2924 10111666 : || !type_has_mode_precision_p (rtype))
2925 1560146 : return false;
2926 :
2927 25654560 : for (i = 0; i < 2; i++)
2928 : {
2929 17103040 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2930 17103040 : if (TREE_CODE (arg[i]) == SSA_NAME)
2931 11387270 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2932 : }
2933 :
2934 : /* Look through narrowing (or same precision) conversions. */
2935 7596151 : if (CONVERT_EXPR_CODE_P (def_code[0])
2936 955369 : && CONVERT_EXPR_CODE_P (def_code[1])
2937 140333 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[0]))
2938 116912 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[1]))
2939 109777 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
2940 109777 : == TYPE_PRECISION (TREE_TYPE (def_arg1[1]))
2941 63967 : && TYPE_PRECISION (TREE_TYPE (def_arg1[0])) >= TYPE_PRECISION (rtype)
2942 44155 : && has_single_use (arg[0])
2943 8584297 : && has_single_use (arg[1]))
2944 : {
2945 28564 : wider_prec = TYPE_PRECISION (TREE_TYPE (def_arg1[0]));
2946 85692 : for (i = 0; i < 2; i++)
2947 : {
2948 57128 : arg[i] = def_arg1[i];
2949 57128 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2950 57128 : if (TREE_CODE (arg[i]) == SSA_NAME)
2951 57128 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2952 : }
2953 : }
2954 : else
2955 : {
2956 : /* Handle signed rotate; the RSHIFT_EXPR has to be done
2957 : in unsigned type but LSHIFT_EXPR could be signed. */
2958 8522956 : i = (def_code[0] == LSHIFT_EXPR || def_code[0] == RSHIFT_EXPR);
2959 7579304 : if (CONVERT_EXPR_CODE_P (def_code[i])
2960 943652 : && (def_code[1 - i] == LSHIFT_EXPR || def_code[1 - i] == RSHIFT_EXPR)
2961 29529 : && INTEGRAL_TYPE_P (TREE_TYPE (def_arg1[i]))
2962 28449 : && TYPE_PRECISION (rtype) == TYPE_PRECISION (TREE_TYPE (def_arg1[i]))
2963 8527607 : && has_single_use (arg[i]))
2964 : {
2965 2066 : arg[i] = def_arg1[i];
2966 2066 : defcodefor_name (arg[i], &def_code[i], &def_arg1[i], &def_arg2[i]);
2967 2066 : if (TREE_CODE (arg[i]) == SSA_NAME)
2968 2066 : def_arg_stmt[i] = SSA_NAME_DEF_STMT (arg[i]);
2969 : }
2970 : }
2971 :
2972 : /* One operand has to be LSHIFT_EXPR and one RSHIFT_EXPR. */
2973 8749205 : for (i = 0; i < 2; i++)
2974 8724484 : if (def_code[i] != LSHIFT_EXPR && def_code[i] != RSHIFT_EXPR)
2975 : return false;
2976 237922 : else if (!has_single_use (arg[i]))
2977 : return false;
2978 24721 : if (def_code[0] == def_code[1])
2979 : return false;
2980 :
2981 : /* If we've looked through narrowing conversions before, look through
2982 : widening conversions from unsigned type with the same precision
2983 : as rtype here. */
2984 20356 : if (TYPE_PRECISION (TREE_TYPE (def_arg1[0])) != TYPE_PRECISION (rtype))
2985 19348 : for (i = 0; i < 2; i++)
2986 : {
2987 12900 : tree tem;
2988 12900 : enum tree_code code;
2989 12900 : defcodefor_name (def_arg1[i], &code, &tem, NULL);
2990 4 : if (!CONVERT_EXPR_CODE_P (code)
2991 12896 : || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
2992 25796 : || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
2993 4 : return false;
2994 12896 : def_arg1[i] = tem;
2995 : }
2996 : /* Both shifts have to use the same first operand. */
2997 20352 : if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
2998 32281 : || !types_compatible_p (TREE_TYPE (def_arg1[0]),
2999 11929 : TREE_TYPE (def_arg1[1])))
3000 : {
3001 8423 : if ((TYPE_PRECISION (TREE_TYPE (def_arg1[0]))
3002 8423 : != TYPE_PRECISION (TREE_TYPE (def_arg1[1])))
3003 8423 : || (TYPE_UNSIGNED (TREE_TYPE (def_arg1[0]))
3004 8423 : == TYPE_UNSIGNED (TREE_TYPE (def_arg1[1]))))
3005 8399 : return false;
3006 :
3007 : /* Handle signed rotate; the RSHIFT_EXPR has to be done
3008 : in unsigned type but LSHIFT_EXPR could be signed. */
3009 546 : i = def_code[0] != RSHIFT_EXPR;
3010 546 : if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[i])))
3011 : return false;
3012 :
3013 507 : tree tem;
3014 507 : enum tree_code code;
3015 507 : defcodefor_name (def_arg1[i], &code, &tem, NULL);
3016 304 : if (!CONVERT_EXPR_CODE_P (code)
3017 203 : || !INTEGRAL_TYPE_P (TREE_TYPE (tem))
3018 710 : || TYPE_PRECISION (TREE_TYPE (tem)) != TYPE_PRECISION (rtype))
3019 : return false;
3020 194 : def_arg1[i] = tem;
3021 194 : if (!operand_equal_for_phi_arg_p (def_arg1[0], def_arg1[1])
3022 218 : || !types_compatible_p (TREE_TYPE (def_arg1[0]),
3023 24 : TREE_TYPE (def_arg1[1])))
3024 170 : return false;
3025 : }
3026 11929 : else if (!TYPE_UNSIGNED (TREE_TYPE (def_arg1[0])))
3027 : return false;
3028 :
3029 : /* CNT1 + CNT2 == B case above. */
3030 10698 : if (tree_fits_uhwi_p (def_arg2[0])
3031 1210 : && tree_fits_uhwi_p (def_arg2[1])
3032 10698 : && tree_to_uhwi (def_arg2[0])
3033 1210 : + tree_to_uhwi (def_arg2[1]) == TYPE_PRECISION (rtype))
3034 : rotcnt = def_arg2[0];
3035 9768 : else if (TREE_CODE (def_arg2[0]) != SSA_NAME
3036 9488 : || TREE_CODE (def_arg2[1]) != SSA_NAME)
3037 : return false;
3038 : else
3039 : {
3040 9488 : tree cdef_arg1[2], cdef_arg2[2], def_arg2_alt[2];
3041 9488 : enum tree_code cdef_code[2];
3042 9488 : gimple *def_arg_alt_stmt[2] = { NULL, NULL };
3043 9488 : int check_range = 0;
3044 9488 : gimple *check_range_stmt = NULL;
3045 : /* Look through conversion of the shift count argument.
3046 : The C/C++ FE cast any shift count argument to integer_type_node.
3047 : The only problem might be if the shift count type maximum value
3048 : is equal or smaller than number of bits in rtype. */
3049 28464 : for (i = 0; i < 2; i++)
3050 : {
3051 18976 : def_arg2_alt[i] = def_arg2[i];
3052 18976 : defcodefor_name (def_arg2[i], &cdef_code[i],
3053 : &cdef_arg1[i], &cdef_arg2[i]);
3054 14716 : if (CONVERT_EXPR_CODE_P (cdef_code[i])
3055 4260 : && INTEGRAL_TYPE_P (TREE_TYPE (cdef_arg1[i]))
3056 4260 : && TYPE_PRECISION (TREE_TYPE (cdef_arg1[i]))
3057 8520 : > floor_log2 (TYPE_PRECISION (rtype))
3058 23236 : && type_has_mode_precision_p (TREE_TYPE (cdef_arg1[i])))
3059 : {
3060 4260 : def_arg2_alt[i] = cdef_arg1[i];
3061 4260 : if (TREE_CODE (def_arg2[i]) == SSA_NAME)
3062 4260 : def_arg_alt_stmt[i] = SSA_NAME_DEF_STMT (def_arg2[i]);
3063 4260 : defcodefor_name (def_arg2_alt[i], &cdef_code[i],
3064 : &cdef_arg1[i], &cdef_arg2[i]);
3065 : }
3066 : else
3067 14716 : def_arg_alt_stmt[i] = def_arg_stmt[i];
3068 : }
3069 25796 : for (i = 0; i < 2; i++)
3070 : /* Check for one shift count being Y and the other B - Y,
3071 : with optional casts. */
3072 18625 : if (cdef_code[i] == MINUS_EXPR
3073 862 : && tree_fits_shwi_p (cdef_arg1[i])
3074 862 : && tree_to_shwi (cdef_arg1[i]) == TYPE_PRECISION (rtype)
3075 19447 : && TREE_CODE (cdef_arg2[i]) == SSA_NAME)
3076 : {
3077 822 : tree tem;
3078 822 : enum tree_code code;
3079 :
3080 822 : if (cdef_arg2[i] == def_arg2[1 - i]
3081 472 : || cdef_arg2[i] == def_arg2_alt[1 - i])
3082 : {
3083 350 : rotcnt = cdef_arg2[i];
3084 350 : check_range = -1;
3085 350 : if (cdef_arg2[i] == def_arg2[1 - i])
3086 350 : check_range_stmt = def_arg_stmt[1 - i];
3087 : else
3088 0 : check_range_stmt = def_arg_alt_stmt[1 - i];
3089 806 : break;
3090 : }
3091 472 : defcodefor_name (cdef_arg2[i], &code, &tem, NULL);
3092 16 : if (CONVERT_EXPR_CODE_P (code)
3093 456 : && INTEGRAL_TYPE_P (TREE_TYPE (tem))
3094 456 : && TYPE_PRECISION (TREE_TYPE (tem))
3095 912 : > floor_log2 (TYPE_PRECISION (rtype))
3096 456 : && type_has_mode_precision_p (TREE_TYPE (tem))
3097 928 : && (tem == def_arg2[1 - i]
3098 288 : || tem == def_arg2_alt[1 - i]))
3099 : {
3100 456 : rotcnt = tem;
3101 456 : check_range = -1;
3102 456 : if (tem == def_arg2[1 - i])
3103 168 : check_range_stmt = def_arg_stmt[1 - i];
3104 : else
3105 288 : check_range_stmt = def_arg_alt_stmt[1 - i];
3106 : break;
3107 : }
3108 : }
3109 : /* The above sequence isn't safe for Y being 0,
3110 : because then one of the shifts triggers undefined behavior.
3111 : This alternative is safe even for rotation count of 0.
3112 : One shift count is Y and the other (-Y) & (B - 1).
3113 : Or one shift count is Y & (B - 1) and the other (-Y) & (B - 1). */
3114 17803 : else if (cdef_code[i] == BIT_AND_EXPR
3115 28724 : && pow2p_hwi (TYPE_PRECISION (rtype))
3116 12416 : && tree_fits_shwi_p (cdef_arg2[i])
3117 24832 : && tree_to_shwi (cdef_arg2[i])
3118 12416 : == TYPE_PRECISION (rtype) - 1
3119 12356 : && TREE_CODE (cdef_arg1[i]) == SSA_NAME
3120 30159 : && gimple_assign_rhs_code (stmt) == BIT_IOR_EXPR)
3121 : {
3122 2296 : tree tem;
3123 2296 : enum tree_code code;
3124 :
3125 2296 : defcodefor_name (cdef_arg1[i], &code, &tem, NULL);
3126 2099 : if (CONVERT_EXPR_CODE_P (code)
3127 197 : && INTEGRAL_TYPE_P (TREE_TYPE (tem))
3128 197 : && TYPE_PRECISION (TREE_TYPE (tem))
3129 394 : > floor_log2 (TYPE_PRECISION (rtype))
3130 2493 : && type_has_mode_precision_p (TREE_TYPE (tem)))
3131 197 : defcodefor_name (tem, &code, &tem, NULL);
3132 :
3133 2296 : if (code == NEGATE_EXPR)
3134 : {
3135 1525 : if (tem == def_arg2[1 - i] || tem == def_arg2_alt[1 - i])
3136 : {
3137 854 : rotcnt = tem;
3138 854 : check_range = 1;
3139 854 : if (tem == def_arg2[1 - i])
3140 846 : check_range_stmt = def_arg_stmt[1 - i];
3141 : else
3142 8 : check_range_stmt = def_arg_alt_stmt[1 - i];
3143 1511 : break;
3144 : }
3145 671 : tree tem2;
3146 671 : defcodefor_name (tem, &code, &tem2, NULL);
3147 237 : if (CONVERT_EXPR_CODE_P (code)
3148 434 : && INTEGRAL_TYPE_P (TREE_TYPE (tem2))
3149 434 : && TYPE_PRECISION (TREE_TYPE (tem2))
3150 868 : > floor_log2 (TYPE_PRECISION (rtype))
3151 1105 : && type_has_mode_precision_p (TREE_TYPE (tem2)))
3152 : {
3153 434 : if (tem2 == def_arg2[1 - i]
3154 434 : || tem2 == def_arg2_alt[1 - i])
3155 : {
3156 228 : rotcnt = tem2;
3157 228 : check_range = 1;
3158 228 : if (tem2 == def_arg2[1 - i])
3159 0 : check_range_stmt = def_arg_stmt[1 - i];
3160 : else
3161 228 : check_range_stmt = def_arg_alt_stmt[1 - i];
3162 : break;
3163 : }
3164 : }
3165 : else
3166 237 : tem2 = NULL_TREE;
3167 :
3168 443 : if (cdef_code[1 - i] == BIT_AND_EXPR
3169 430 : && tree_fits_shwi_p (cdef_arg2[1 - i])
3170 860 : && tree_to_shwi (cdef_arg2[1 - i])
3171 430 : == TYPE_PRECISION (rtype) - 1
3172 873 : && TREE_CODE (cdef_arg1[1 - i]) == SSA_NAME)
3173 : {
3174 430 : if (tem == cdef_arg1[1 - i]
3175 205 : || tem2 == cdef_arg1[1 - i])
3176 : {
3177 : rotcnt = def_arg2[1 - i];
3178 429 : break;
3179 : }
3180 193 : tree tem3;
3181 193 : defcodefor_name (cdef_arg1[1 - i], &code, &tem3, NULL);
3182 0 : if (CONVERT_EXPR_CODE_P (code)
3183 193 : && INTEGRAL_TYPE_P (TREE_TYPE (tem3))
3184 193 : && TYPE_PRECISION (TREE_TYPE (tem3))
3185 386 : > floor_log2 (TYPE_PRECISION (rtype))
3186 386 : && type_has_mode_precision_p (TREE_TYPE (tem3)))
3187 : {
3188 193 : if (tem == tem3 || tem2 == tem3)
3189 : {
3190 : rotcnt = def_arg2[1 - i];
3191 : break;
3192 : }
3193 : }
3194 : }
3195 : }
3196 : }
3197 2317 : if (check_range && wider_prec > TYPE_PRECISION (rtype))
3198 : {
3199 1533 : if (TREE_CODE (rotcnt) != SSA_NAME)
3200 573 : return false;
3201 1533 : int_range_max r;
3202 1533 : range_query *q = get_range_query (cfun);
3203 1533 : if (q == get_global_range_query ())
3204 1522 : q = enable_ranger (cfun);
3205 1533 : if (!q->range_of_expr (r, rotcnt, check_range_stmt))
3206 : {
3207 0 : if (check_range > 0)
3208 : return false;
3209 0 : r.set_varying (TREE_TYPE (rotcnt));
3210 : }
3211 1533 : int prec = TYPE_PRECISION (TREE_TYPE (rotcnt));
3212 1533 : signop sign = TYPE_SIGN (TREE_TYPE (rotcnt));
3213 1533 : wide_int min = wide_int::from (TYPE_PRECISION (rtype), prec, sign);
3214 1533 : wide_int max = wide_int::from (wider_prec - 1, prec, sign);
3215 1533 : if (check_range < 0)
3216 616 : max = min;
3217 1533 : int_range<1> r2 (TREE_TYPE (rotcnt), min, max);
3218 1533 : r.intersect (r2);
3219 1533 : if (!r.undefined_p ())
3220 : {
3221 1181 : if (check_range > 0)
3222 : {
3223 589 : int_range_max r3;
3224 1844 : for (int i = TYPE_PRECISION (rtype) + 1; i < wider_prec;
3225 1255 : i += TYPE_PRECISION (rtype))
3226 : {
3227 1255 : int j = i + TYPE_PRECISION (rtype) - 2;
3228 1255 : min = wide_int::from (i, prec, sign);
3229 1255 : max = wide_int::from (MIN (j, wider_prec - 1),
3230 1255 : prec, sign);
3231 1255 : int_range<1> r4 (TREE_TYPE (rotcnt), min, max);
3232 1255 : r3.union_ (r4);
3233 1255 : }
3234 589 : r.intersect (r3);
3235 589 : if (!r.undefined_p ())
3236 573 : return false;
3237 589 : }
3238 : add_masking = true;
3239 : }
3240 1533 : }
3241 8915 : if (rotcnt == NULL_TREE)
3242 : return false;
3243 1744 : swapped_p = i != 1;
3244 : }
3245 :
3246 2674 : if (!useless_type_conversion_p (TREE_TYPE (def_arg2[0]),
3247 2674 : TREE_TYPE (rotcnt)))
3248 : {
3249 496 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (def_arg2[0])),
3250 : NOP_EXPR, rotcnt);
3251 496 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
3252 496 : rotcnt = gimple_assign_lhs (g);
3253 : }
3254 2674 : if (add_masking)
3255 : {
3256 608 : g = gimple_build_assign (make_ssa_name (TREE_TYPE (rotcnt)),
3257 : BIT_AND_EXPR, rotcnt,
3258 608 : build_int_cst (TREE_TYPE (rotcnt),
3259 608 : TYPE_PRECISION (rtype) - 1));
3260 608 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
3261 608 : rotcnt = gimple_assign_lhs (g);
3262 : }
3263 2674 : lhs = gimple_assign_lhs (stmt);
3264 2674 : if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
3265 1010 : lhs = make_ssa_name (TREE_TYPE (def_arg1[0]));
3266 2674 : g = gimple_build_assign (lhs,
3267 2674 : ((def_code[0] == LSHIFT_EXPR) ^ swapped_p)
3268 : ? LROTATE_EXPR : RROTATE_EXPR, def_arg1[0], rotcnt);
3269 2674 : if (!useless_type_conversion_p (rtype, TREE_TYPE (def_arg1[0])))
3270 : {
3271 1010 : gsi_insert_before (gsi, g, GSI_SAME_STMT);
3272 1010 : g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, lhs);
3273 : }
3274 2674 : gsi_replace (gsi, g, false);
3275 2674 : return true;
3276 : }
3277 :
3278 :
3279 : /* Check whether an array contains a valid table according to VALIDATE_FN. */
3280 : template<typename ValidateFn>
3281 : static bool
3282 16 : check_table_array (tree ctor, HOST_WIDE_INT &zero_val, unsigned bits,
3283 : ValidateFn validate_fn)
3284 : {
3285 : tree elt, idx;
3286 16 : unsigned HOST_WIDE_INT i, raw_idx = 0;
3287 16 : unsigned matched = 0;
3288 :
3289 16 : zero_val = 0;
3290 :
3291 670 : FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (ctor), i, idx, elt)
3292 : {
3293 670 : if (!tree_fits_shwi_p (idx))
3294 : return false;
3295 670 : if (!tree_fits_shwi_p (elt) && TREE_CODE (elt) != RAW_DATA_CST)
3296 : return false;
3297 :
3298 670 : unsigned HOST_WIDE_INT index = tree_to_shwi (idx);
3299 : HOST_WIDE_INT val;
3300 :
3301 670 : if (TREE_CODE (elt) == INTEGER_CST)
3302 606 : val = tree_to_shwi (elt);
3303 : else
3304 : {
3305 64 : if (raw_idx == (unsigned) RAW_DATA_LENGTH (elt))
3306 : {
3307 0 : raw_idx = 0;
3308 0 : continue;
3309 : }
3310 64 : if (TYPE_UNSIGNED (TREE_TYPE (elt)))
3311 0 : val = RAW_DATA_UCHAR_ELT (elt, raw_idx);
3312 : else
3313 64 : val = RAW_DATA_SCHAR_ELT (elt, raw_idx);
3314 64 : index += raw_idx;
3315 64 : raw_idx++;
3316 64 : i--;
3317 : }
3318 :
3319 670 : if (index > bits * 2)
3320 : return false;
3321 :
3322 670 : if (index == 0)
3323 : {
3324 16 : zero_val = val;
3325 16 : matched++;
3326 : }
3327 :
3328 670 : if (val >= 0 && val < bits && validate_fn (val, index))
3329 608 : matched++;
3330 :
3331 670 : if (matched > bits)
3332 : return true;
3333 : }
3334 :
3335 : return false;
3336 : }
3337 :
3338 : /* Check whether a string contains a valid table according to VALIDATE_FN. */
3339 : template<typename ValidateFn>
3340 : static bool
3341 4 : check_table_string (tree string, HOST_WIDE_INT &zero_val,unsigned bits,
3342 : ValidateFn validate_fn)
3343 : {
3344 4 : unsigned HOST_WIDE_INT len = TREE_STRING_LENGTH (string);
3345 4 : unsigned matched = 0;
3346 4 : const unsigned char *p = (const unsigned char *) TREE_STRING_POINTER (string);
3347 :
3348 4 : if (len < bits || len > bits * 2)
3349 : return false;
3350 :
3351 4 : zero_val = p[0];
3352 :
3353 164 : for (unsigned i = 0; i < len; i++)
3354 160 : if (p[i] < bits && validate_fn (p[i], i))
3355 160 : matched++;
3356 :
3357 4 : return matched == bits;
3358 : }
3359 :
3360 : /* Check whether CTOR contains a valid table according to VALIDATE_FN. */
3361 : template<typename ValidateFn>
3362 : static bool
3363 28 : check_table (tree ctor, tree type, HOST_WIDE_INT &zero_val, unsigned bits,
3364 : ValidateFn validate_fn)
3365 : {
3366 28 : if (TREE_CODE (ctor) == CONSTRUCTOR)
3367 16 : return check_table_array (ctor, zero_val, bits, validate_fn);
3368 : else if (TREE_CODE (ctor) == STRING_CST
3369 12 : && TYPE_PRECISION (type) == CHAR_TYPE_SIZE)
3370 4 : return check_table_string (ctor, zero_val, bits, validate_fn);
3371 : return false;
3372 : }
3373 :
3374 : /* Match.pd function to match the ctz expression. */
3375 : extern bool gimple_ctz_table_index (tree, tree *, tree (*)(tree));
3376 : extern bool gimple_clz_table_index (tree, tree *, tree (*)(tree));
3377 :
3378 : /* Recognize count leading and trailing zeroes idioms.
3379 : The canonical form is array[((x & -x) * C) >> SHIFT] where C is a magic
3380 : constant which when multiplied by a power of 2 creates a unique value
3381 : in the top 5 or 6 bits. This is then indexed into a table which maps it
3382 : to the number of trailing zeroes. Array[0] is returned so the caller can
3383 : emit an appropriate sequence depending on whether ctz (0) is defined on
3384 : the target. */
3385 :
3386 : static bool
3387 1954317 : simplify_count_zeroes (gimple_stmt_iterator *gsi)
3388 : {
3389 1954317 : gimple *stmt = gsi_stmt (*gsi);
3390 1954317 : tree array_ref = gimple_assign_rhs1 (stmt);
3391 1954317 : tree res_ops[3];
3392 :
3393 1954317 : gcc_checking_assert (TREE_CODE (array_ref) == ARRAY_REF);
3394 :
3395 1954317 : internal_fn fn = IFN_LAST;
3396 : /* For CTZ we recognize ((x & -x) * C) >> SHIFT where the array data
3397 : represents the number of trailing zeros. */
3398 1954317 : if (gimple_ctz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0], NULL))
3399 : fn = IFN_CTZ;
3400 : /* For CLZ we recognize
3401 : x |= x >> 1;
3402 : x |= x >> 2;
3403 : x |= x >> 4;
3404 : x |= x >> 8;
3405 : x |= x >> 16;
3406 : (x * C) >> SHIFT
3407 : where 31 minus the array data represents the number of leading zeros. */
3408 1954294 : else if (gimple_clz_table_index (TREE_OPERAND (array_ref, 1), &res_ops[0],
3409 : NULL))
3410 : fn = IFN_CLZ;
3411 : else
3412 : return false;
3413 :
3414 33 : HOST_WIDE_INT zero_val;
3415 33 : tree type = TREE_TYPE (array_ref);
3416 33 : tree array = TREE_OPERAND (array_ref, 0);
3417 33 : tree input_type = TREE_TYPE (res_ops[0]);
3418 33 : unsigned input_bits = tree_to_shwi (TYPE_SIZE (input_type));
3419 :
3420 : /* Check the array element type is not wider than 32 bits and the input is
3421 : an unsigned 32-bit or 64-bit type. */
3422 33 : if (TYPE_PRECISION (type) > 32 || !TYPE_UNSIGNED (input_type))
3423 : return false;
3424 29 : if (input_bits != 32 && input_bits != 64)
3425 : return false;
3426 :
3427 29 : if (!direct_internal_fn_supported_p (fn, input_type, OPTIMIZE_FOR_BOTH))
3428 : return false;
3429 :
3430 : /* Check the lower bound of the array is zero. */
3431 29 : tree low = array_ref_low_bound (array_ref);
3432 29 : if (!low || !integer_zerop (low))
3433 0 : return false;
3434 :
3435 : /* Check the shift extracts the top 5..7 bits. */
3436 29 : unsigned shiftval = tree_to_shwi (res_ops[2]);
3437 29 : if (shiftval < input_bits - 7 || shiftval > input_bits - 5)
3438 : return false;
3439 :
3440 28 : tree ctor = ctor_for_folding (array);
3441 28 : if (!ctor)
3442 : return false;
3443 28 : unsigned HOST_WIDE_INT mulval = tree_to_uhwi (res_ops[1]);
3444 28 : if (fn == IFN_CTZ)
3445 : {
3446 494 : auto checkfn = [&](unsigned data, unsigned i) -> bool
3447 : {
3448 476 : unsigned HOST_WIDE_INT mask
3449 476 : = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
3450 476 : return (((mulval << data) & mask) >> shiftval) == i;
3451 18 : };
3452 18 : if (!check_table (ctor, type, zero_val, input_bits, checkfn))
3453 8 : return false;
3454 : }
3455 10 : else if (fn == IFN_CLZ)
3456 : {
3457 362 : auto checkfn = [&](unsigned data, unsigned i) -> bool
3458 : {
3459 352 : unsigned HOST_WIDE_INT mask
3460 352 : = ((HOST_WIDE_INT_1U << (input_bits - shiftval)) - 1) << shiftval;
3461 : /* The OR-cascade produces a value with all bits from 0 to the
3462 : original MSB set. Compute (1 << (data + 1)) - 1 to simulate
3463 : that value. When data + 1 equals HOST_BITS_PER_WIDE_INT
3464 : (i.e. data is the MSB position of a 64-bit input) the shift
3465 : is undefined behavior, so handle that case explicitly using
3466 : all-ones. Without this, any well-formed 64-bit DeBruijn CLZ
3467 : table is rejected because its entry for the all-ones input
3468 : correctly maps to the MSB (e.g. table[...] == 63).
3469 : PR tree-optimization/122569. */
3470 703 : unsigned HOST_WIDE_INT all_bits_below
3471 : = (data + 1 == HOST_BITS_PER_WIDE_INT)
3472 352 : ? HOST_WIDE_INT_M1U
3473 351 : : ((HOST_WIDE_INT_1U << (data + 1)) - 1);
3474 352 : return (((all_bits_below * mulval) & mask) >> shiftval) == i;
3475 10 : };
3476 10 : if (!check_table (ctor, type, zero_val, input_bits, checkfn))
3477 0 : return false;
3478 : }
3479 :
3480 20 : HOST_WIDE_INT ctz_val = -1;
3481 20 : bool zero_ok;
3482 20 : if (fn == IFN_CTZ)
3483 : {
3484 10 : ctz_val = 0;
3485 20 : zero_ok = CTZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
3486 : ctz_val) == 2;
3487 : }
3488 10 : else if (fn == IFN_CLZ)
3489 : {
3490 10 : ctz_val = 32;
3491 10 : zero_ok = CLZ_DEFINED_VALUE_AT_ZERO (SCALAR_INT_TYPE_MODE (input_type),
3492 : ctz_val) == 2;
3493 10 : zero_val = input_bits - 1 - zero_val;
3494 : }
3495 20 : int nargs = 2;
3496 :
3497 : /* If the input value can't be zero, don't special case ctz (0). */
3498 20 : range_query *q = get_range_query (cfun);
3499 20 : if (q == get_global_range_query ())
3500 20 : q = enable_ranger (cfun);
3501 20 : int_range_max vr;
3502 20 : if (q->range_of_expr (vr, res_ops[0], stmt)
3503 20 : && !range_includes_zero_p (vr))
3504 : {
3505 4 : zero_ok = true;
3506 4 : zero_val = 0;
3507 4 : ctz_val = 0;
3508 4 : nargs = 1;
3509 : }
3510 :
3511 20 : gimple_seq seq = NULL;
3512 20 : gimple *g;
3513 20 : gcall *call = gimple_build_call_internal (fn, nargs, res_ops[0],
3514 : nargs == 1 ? NULL_TREE
3515 36 : : build_int_cst (integer_type_node,
3516 16 : ctz_val));
3517 20 : gimple_set_location (call, gimple_location (stmt));
3518 20 : gimple_set_lhs (call, make_ssa_name (integer_type_node));
3519 20 : gimple_seq_add_stmt (&seq, call);
3520 :
3521 20 : tree prev_lhs = gimple_call_lhs (call);
3522 :
3523 20 : if (zero_ok && zero_val == ctz_val)
3524 : ;
3525 : /* Emit ctz (x) & 31 if ctz (0) is 32 but we need to return 0. */
3526 6 : else if (zero_ok && zero_val == 0 && ctz_val == input_bits)
3527 : {
3528 5 : g = gimple_build_assign (make_ssa_name (integer_type_node),
3529 : BIT_AND_EXPR, prev_lhs,
3530 : build_int_cst (integer_type_node,
3531 5 : input_bits - 1));
3532 5 : gimple_set_location (g, gimple_location (stmt));
3533 5 : gimple_seq_add_stmt (&seq, g);
3534 5 : prev_lhs = gimple_assign_lhs (g);
3535 : }
3536 : /* As fallback emit a conditional move. */
3537 : else
3538 : {
3539 9 : g = gimple_build_assign (make_ssa_name (boolean_type_node), EQ_EXPR,
3540 : res_ops[0], build_zero_cst (input_type));
3541 9 : gimple_set_location (g, gimple_location (stmt));
3542 9 : gimple_seq_add_stmt (&seq, g);
3543 9 : tree cond = gimple_assign_lhs (g);
3544 9 : g = gimple_build_assign (make_ssa_name (integer_type_node),
3545 : COND_EXPR, cond,
3546 9 : build_int_cst (integer_type_node, zero_val),
3547 : prev_lhs);
3548 9 : gimple_set_location (g, gimple_location (stmt));
3549 9 : gimple_seq_add_stmt (&seq, g);
3550 9 : prev_lhs = gimple_assign_lhs (g);
3551 : }
3552 :
3553 20 : if (fn == IFN_CLZ)
3554 : {
3555 10 : g = gimple_build_assign (make_ssa_name (integer_type_node),
3556 : MINUS_EXPR,
3557 : build_int_cst (integer_type_node,
3558 10 : input_bits - 1),
3559 : prev_lhs);
3560 10 : gimple_set_location (g, gimple_location (stmt));
3561 10 : gimple_seq_add_stmt (&seq, g);
3562 10 : prev_lhs = gimple_assign_lhs (g);
3563 : }
3564 :
3565 20 : g = gimple_build_assign (gimple_assign_lhs (stmt), NOP_EXPR, prev_lhs);
3566 20 : gimple_seq_add_stmt (&seq, g);
3567 20 : gsi_replace_with_seq (gsi, seq, true);
3568 20 : return true;
3569 20 : }
3570 :
3571 :
3572 : /* Determine whether applying the 2 permutations (mask1 then mask2)
3573 : gives back one of the input. */
3574 :
3575 : static int
3576 34 : is_combined_permutation_identity (tree mask1, tree mask2)
3577 : {
3578 34 : tree mask;
3579 34 : unsigned HOST_WIDE_INT nelts, i, j;
3580 34 : bool maybe_identity1 = true;
3581 34 : bool maybe_identity2 = true;
3582 :
3583 34 : gcc_checking_assert (TREE_CODE (mask1) == VECTOR_CST
3584 : && TREE_CODE (mask2) == VECTOR_CST);
3585 :
3586 : /* For VLA masks, check for the following pattern:
3587 : v1 = VEC_PERM_EXPR (v0, ..., mask1)
3588 : v2 = VEC_PERM_EXPR (v1, ..., mask2)
3589 : -->
3590 : v2 = v0
3591 : if mask1 == mask2 == {nelts - 1, nelts - 2, ...}. */
3592 :
3593 34 : if (operand_equal_p (mask1, mask2, 0)
3594 34 : && !VECTOR_CST_NELTS (mask1).is_constant ())
3595 : {
3596 : vec_perm_builder builder;
3597 : if (tree_to_vec_perm_builder (&builder, mask1))
3598 : {
3599 : poly_uint64 nelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (mask1));
3600 : vec_perm_indices sel (builder, 1, nelts);
3601 : if (sel.series_p (0, 1, nelts - 1, -1))
3602 : return 1;
3603 : }
3604 : }
3605 :
3606 34 : mask = fold_ternary (VEC_PERM_EXPR, TREE_TYPE (mask1), mask1, mask1, mask2);
3607 34 : if (mask == NULL_TREE || TREE_CODE (mask) != VECTOR_CST)
3608 : return 0;
3609 :
3610 34 : if (!VECTOR_CST_NELTS (mask).is_constant (&nelts))
3611 : return 0;
3612 60 : for (i = 0; i < nelts; i++)
3613 : {
3614 60 : tree val = VECTOR_CST_ELT (mask, i);
3615 60 : gcc_assert (TREE_CODE (val) == INTEGER_CST);
3616 60 : j = TREE_INT_CST_LOW (val) & (2 * nelts - 1);
3617 60 : if (j == i)
3618 : maybe_identity2 = false;
3619 47 : else if (j == i + nelts)
3620 : maybe_identity1 = false;
3621 : else
3622 : return 0;
3623 : }
3624 0 : return maybe_identity1 ? 1 : maybe_identity2 ? 2 : 0;
3625 : }
3626 :
3627 : /* Combine a shuffle with its arguments. Returns true if there were any
3628 : changes made. */
3629 :
3630 : static bool
3631 184932 : simplify_permutation (gimple_stmt_iterator *gsi)
3632 : {
3633 184932 : gimple *stmt = gsi_stmt (*gsi);
3634 184932 : gimple *def_stmt = NULL;
3635 184932 : tree op0, op1, op2, op3, arg0, arg1;
3636 184932 : enum tree_code code, code2 = ERROR_MARK;
3637 184932 : bool single_use_op0 = false;
3638 :
3639 184932 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
3640 :
3641 184932 : op0 = gimple_assign_rhs1 (stmt);
3642 184932 : op1 = gimple_assign_rhs2 (stmt);
3643 184932 : op2 = gimple_assign_rhs3 (stmt);
3644 :
3645 184932 : if (TREE_CODE (op2) != VECTOR_CST)
3646 : return false;
3647 :
3648 182163 : if (TREE_CODE (op0) == VECTOR_CST)
3649 : {
3650 : code = VECTOR_CST;
3651 : arg0 = op0;
3652 : }
3653 180301 : else if (TREE_CODE (op0) == SSA_NAME)
3654 : {
3655 180301 : def_stmt = get_prop_source_stmt (op0, false, &single_use_op0);
3656 180301 : if (!def_stmt)
3657 : return false;
3658 172219 : code = gimple_assign_rhs_code (def_stmt);
3659 172219 : if (code == VIEW_CONVERT_EXPR)
3660 : {
3661 1563 : tree rhs = gimple_assign_rhs1 (def_stmt);
3662 1563 : tree name = TREE_OPERAND (rhs, 0);
3663 1563 : if (TREE_CODE (name) != SSA_NAME)
3664 : return false;
3665 1563 : if (!has_single_use (name))
3666 240 : single_use_op0 = false;
3667 : /* Here we update the def_stmt through this VIEW_CONVERT_EXPR,
3668 : but still keep the code to indicate it comes from
3669 : VIEW_CONVERT_EXPR. */
3670 1563 : def_stmt = SSA_NAME_DEF_STMT (name);
3671 1563 : if (!def_stmt || !is_gimple_assign (def_stmt))
3672 : return false;
3673 776 : if (gimple_assign_rhs_code (def_stmt) != CONSTRUCTOR)
3674 : return false;
3675 : }
3676 170884 : if (!can_propagate_from (def_stmt))
3677 : return false;
3678 21786 : arg0 = gimple_assign_rhs1 (def_stmt);
3679 : }
3680 : else
3681 : return false;
3682 :
3683 : /* Two consecutive shuffles. */
3684 21786 : if (code == VEC_PERM_EXPR)
3685 : {
3686 6451 : tree orig;
3687 6451 : int ident;
3688 :
3689 6451 : if (op0 != op1)
3690 : return false;
3691 34 : op3 = gimple_assign_rhs3 (def_stmt);
3692 34 : if (TREE_CODE (op3) != VECTOR_CST)
3693 : return false;
3694 34 : ident = is_combined_permutation_identity (op3, op2);
3695 34 : if (!ident)
3696 : return false;
3697 0 : orig = (ident == 1) ? gimple_assign_rhs1 (def_stmt)
3698 0 : : gimple_assign_rhs2 (def_stmt);
3699 0 : gimple_assign_set_rhs1 (stmt, unshare_expr (orig));
3700 0 : gimple_assign_set_rhs_code (stmt, TREE_CODE (orig));
3701 0 : gimple_set_num_ops (stmt, 2);
3702 0 : update_stmt (stmt);
3703 0 : remove_prop_source_from_use (op0);
3704 0 : return true;
3705 : }
3706 17197 : else if (code == CONSTRUCTOR
3707 17197 : || code == VECTOR_CST
3708 : || code == VIEW_CONVERT_EXPR)
3709 : {
3710 3410 : if (op0 != op1)
3711 : {
3712 3244 : if (TREE_CODE (op0) == SSA_NAME && !single_use_op0)
3713 : return false;
3714 :
3715 2745 : if (TREE_CODE (op1) == VECTOR_CST)
3716 : arg1 = op1;
3717 2245 : else if (TREE_CODE (op1) == SSA_NAME)
3718 : {
3719 2245 : gimple *def_stmt2 = get_prop_source_stmt (op1, true, NULL);
3720 2245 : if (!def_stmt2)
3721 : return false;
3722 788 : code2 = gimple_assign_rhs_code (def_stmt2);
3723 788 : if (code2 == VIEW_CONVERT_EXPR)
3724 : {
3725 4 : tree rhs = gimple_assign_rhs1 (def_stmt2);
3726 4 : tree name = TREE_OPERAND (rhs, 0);
3727 4 : if (TREE_CODE (name) != SSA_NAME)
3728 : return false;
3729 4 : if (!has_single_use (name))
3730 : return false;
3731 3 : def_stmt2 = SSA_NAME_DEF_STMT (name);
3732 3 : if (!def_stmt2 || !is_gimple_assign (def_stmt2))
3733 : return false;
3734 0 : if (gimple_assign_rhs_code (def_stmt2) != CONSTRUCTOR)
3735 : return false;
3736 : }
3737 784 : else if (code2 != CONSTRUCTOR && code2 != VECTOR_CST)
3738 : return false;
3739 651 : if (!can_propagate_from (def_stmt2))
3740 : return false;
3741 651 : arg1 = gimple_assign_rhs1 (def_stmt2);
3742 : }
3743 : else
3744 : return false;
3745 : }
3746 : else
3747 : {
3748 : /* Already used twice in this statement. */
3749 166 : if (TREE_CODE (op0) == SSA_NAME && num_imm_uses (op0) > 2)
3750 : return false;
3751 : arg1 = arg0;
3752 : }
3753 :
3754 : /* If there are any VIEW_CONVERT_EXPRs found when finding permutation
3755 : operands source, check whether it's valid to transform and prepare
3756 : the required new operands. */
3757 1249 : if (code == VIEW_CONVERT_EXPR || code2 == VIEW_CONVERT_EXPR)
3758 : {
3759 : /* Figure out the target vector type to which operands should be
3760 : converted. If both are CONSTRUCTOR, the types should be the
3761 : same, otherwise, use the one of CONSTRUCTOR. */
3762 24 : tree tgt_type = NULL_TREE;
3763 24 : if (code == VIEW_CONVERT_EXPR)
3764 : {
3765 24 : gcc_assert (gimple_assign_rhs_code (def_stmt) == CONSTRUCTOR);
3766 24 : code = CONSTRUCTOR;
3767 24 : tgt_type = TREE_TYPE (arg0);
3768 : }
3769 24 : if (code2 == VIEW_CONVERT_EXPR)
3770 : {
3771 0 : tree arg1_type = TREE_TYPE (arg1);
3772 0 : if (tgt_type == NULL_TREE)
3773 : tgt_type = arg1_type;
3774 0 : else if (tgt_type != arg1_type)
3775 23 : return false;
3776 : }
3777 :
3778 24 : if (!VECTOR_TYPE_P (tgt_type))
3779 : return false;
3780 24 : tree op2_type = TREE_TYPE (op2);
3781 :
3782 : /* Figure out the shrunk factor. */
3783 24 : poly_uint64 tgt_units = TYPE_VECTOR_SUBPARTS (tgt_type);
3784 24 : poly_uint64 op2_units = TYPE_VECTOR_SUBPARTS (op2_type);
3785 24 : if (maybe_gt (tgt_units, op2_units))
3786 : return false;
3787 24 : unsigned int factor;
3788 47 : if (!constant_multiple_p (op2_units, tgt_units, &factor))
3789 : return false;
3790 :
3791 : /* Build the new permutation control vector as target vector. */
3792 24 : vec_perm_builder builder;
3793 24 : if (!tree_to_vec_perm_builder (&builder, op2))
3794 : return false;
3795 24 : vec_perm_indices indices (builder, 2, op2_units);
3796 24 : vec_perm_indices new_indices;
3797 24 : if (new_indices.new_shrunk_vector (indices, factor))
3798 : {
3799 1 : tree mask_type = tgt_type;
3800 1 : if (!VECTOR_INTEGER_TYPE_P (mask_type))
3801 : {
3802 0 : tree elem_type = TREE_TYPE (mask_type);
3803 0 : unsigned elem_size = TREE_INT_CST_LOW (TYPE_SIZE (elem_type));
3804 0 : tree int_type = build_nonstandard_integer_type (elem_size, 0);
3805 0 : mask_type = build_vector_type (int_type, tgt_units);
3806 : }
3807 1 : op2 = vec_perm_indices_to_tree (mask_type, new_indices);
3808 : }
3809 : else
3810 23 : return false;
3811 :
3812 : /* Convert the VECTOR_CST to the appropriate vector type. */
3813 1 : if (tgt_type != TREE_TYPE (arg0))
3814 0 : arg0 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg0);
3815 1 : else if (tgt_type != TREE_TYPE (arg1))
3816 0 : arg1 = fold_build1 (VIEW_CONVERT_EXPR, tgt_type, arg1);
3817 47 : }
3818 :
3819 : /* VIEW_CONVERT_EXPR should be updated to CONSTRUCTOR before. */
3820 1226 : gcc_assert (code == CONSTRUCTOR || code == VECTOR_CST);
3821 :
3822 : /* Shuffle of a constructor. */
3823 1226 : tree res_type
3824 1226 : = build_vector_type (TREE_TYPE (TREE_TYPE (arg0)),
3825 1226 : TYPE_VECTOR_SUBPARTS (TREE_TYPE (op2)));
3826 1226 : tree opt = fold_ternary (VEC_PERM_EXPR, res_type, arg0, arg1, op2);
3827 1226 : if (!opt
3828 280 : || (TREE_CODE (opt) != CONSTRUCTOR && TREE_CODE (opt) != VECTOR_CST))
3829 : return false;
3830 : /* Found VIEW_CONVERT_EXPR before, need one explicit conversion. */
3831 280 : if (res_type != TREE_TYPE (op0))
3832 : {
3833 1 : tree name = make_ssa_name (TREE_TYPE (opt));
3834 1 : gimple *ass_stmt = gimple_build_assign (name, opt);
3835 1 : gsi_insert_before (gsi, ass_stmt, GSI_SAME_STMT);
3836 1 : opt = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op0), name);
3837 : }
3838 280 : gimple_assign_set_rhs_from_tree (gsi, opt);
3839 280 : update_stmt (gsi_stmt (*gsi));
3840 280 : if (TREE_CODE (op0) == SSA_NAME)
3841 1 : remove_prop_source_from_use (op0);
3842 280 : if (op0 != op1 && TREE_CODE (op1) == SSA_NAME)
3843 0 : remove_prop_source_from_use (op1);
3844 280 : return true;
3845 : }
3846 :
3847 : return false;
3848 : }
3849 :
3850 : /* Get the BIT_FIELD_REF definition of VAL, if any, looking through
3851 : conversions with code CONV_CODE or update it if still ERROR_MARK.
3852 : Return NULL_TREE if no such matching def was found. */
3853 :
3854 : static tree
3855 402008 : get_bit_field_ref_def (tree val, enum tree_code &conv_code)
3856 : {
3857 402008 : if (TREE_CODE (val) != SSA_NAME)
3858 : return NULL_TREE ;
3859 376006 : gimple *def_stmt = get_prop_source_stmt (val, false, NULL);
3860 376006 : if (!def_stmt)
3861 : return NULL_TREE;
3862 301594 : enum tree_code code = gimple_assign_rhs_code (def_stmt);
3863 301594 : if (code == FLOAT_EXPR
3864 301594 : || code == FIX_TRUNC_EXPR
3865 : || CONVERT_EXPR_CODE_P (code))
3866 : {
3867 181873 : tree op1 = gimple_assign_rhs1 (def_stmt);
3868 181873 : if (conv_code == ERROR_MARK)
3869 87217 : conv_code = code;
3870 94656 : else if (conv_code != code)
3871 : return NULL_TREE;
3872 181848 : if (TREE_CODE (op1) != SSA_NAME)
3873 : return NULL_TREE;
3874 75035 : def_stmt = SSA_NAME_DEF_STMT (op1);
3875 75035 : if (! is_gimple_assign (def_stmt))
3876 : return NULL_TREE;
3877 60095 : code = gimple_assign_rhs_code (def_stmt);
3878 : }
3879 179816 : if (code != BIT_FIELD_REF)
3880 : return NULL_TREE;
3881 22879 : return gimple_assign_rhs1 (def_stmt);
3882 : }
3883 :
3884 : /* Recognize a VEC_PERM_EXPR. Returns true if there were any changes. */
3885 :
3886 : static bool
3887 152851 : simplify_vector_constructor (gimple_stmt_iterator *gsi)
3888 : {
3889 152851 : gimple *stmt = gsi_stmt (*gsi);
3890 152851 : tree op, orig[2], type;
3891 152851 : unsigned i;
3892 152851 : unsigned HOST_WIDE_INT nelts;
3893 152851 : unsigned HOST_WIDE_INT refnelts;
3894 152851 : enum tree_code conv_code;
3895 152851 : constructor_elt *elt;
3896 :
3897 152851 : op = gimple_assign_rhs1 (stmt);
3898 152851 : type = TREE_TYPE (op);
3899 152851 : gcc_checking_assert (TREE_CODE (op) == CONSTRUCTOR
3900 : && TREE_CODE (type) == VECTOR_TYPE);
3901 :
3902 152851 : if (!TYPE_VECTOR_SUBPARTS (type).is_constant (&nelts))
3903 : return false;
3904 :
3905 152851 : orig[0] = NULL;
3906 152851 : orig[1] = NULL;
3907 152851 : tree orig_elem_type[2] = {};
3908 152851 : conv_code = ERROR_MARK;
3909 152851 : bool maybe_ident = true;
3910 152851 : bool maybe_blend[2] = { true, true };
3911 152851 : tree one_constant = NULL_TREE;
3912 152851 : tree one_nonconstant = NULL_TREE;
3913 152851 : tree subelt;
3914 152851 : auto_vec<tree> constants;
3915 152851 : constants.safe_grow_cleared (nelts, true);
3916 152851 : auto_vec<std::pair<unsigned, unsigned>, 64> elts;
3917 152851 : unsigned int tsubelts = 0;
3918 433964 : FOR_EACH_VEC_SAFE_ELT (CONSTRUCTOR_ELTS (op), i, elt)
3919 : {
3920 402008 : tree ref, op1;
3921 402008 : unsigned int elem, src_elem_size;
3922 402008 : unsigned HOST_WIDE_INT nsubelts = 1;
3923 :
3924 402008 : if (i >= nelts)
3925 152851 : return false;
3926 :
3927 : /* Look for elements extracted and possibly converted from
3928 : another vector. */
3929 402008 : op1 = get_bit_field_ref_def (elt->value, conv_code);
3930 402008 : if (op1
3931 22879 : && TREE_CODE ((ref = TREE_OPERAND (op1, 0))) == SSA_NAME
3932 4893 : && VECTOR_TYPE_P (TREE_TYPE (ref))
3933 4886 : && (tree_nop_conversion_p (TREE_TYPE (op1),
3934 4886 : TREE_TYPE (TREE_TYPE (ref)))
3935 680 : || (VECTOR_TYPE_P (TREE_TYPE (op1))
3936 76 : && tree_nop_conversion_p (TREE_TYPE (TREE_TYPE (op1)),
3937 76 : TREE_TYPE (TREE_TYPE (ref)))
3938 76 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (op1))
3939 76 : .is_constant (&nsubelts)))
3940 4282 : && constant_multiple_p (bit_field_size (op1), nsubelts,
3941 : &src_elem_size)
3942 406290 : && constant_multiple_p (bit_field_offset (op1), src_elem_size, &elem)
3943 406290 : && TYPE_VECTOR_SUBPARTS (TREE_TYPE (ref)).is_constant (&refnelts))
3944 : {
3945 : unsigned int j;
3946 4555 : for (j = 0; j < 2; ++j)
3947 : {
3948 4538 : if (!orig[j])
3949 : {
3950 1991 : if (j == 0
3951 2171 : || useless_type_conversion_p (TREE_TYPE (orig[0]),
3952 180 : TREE_TYPE (ref)))
3953 : break;
3954 : }
3955 2547 : else if (ref == orig[j])
3956 : break;
3957 : }
3958 : /* Found a suitable vector element. */
3959 4282 : if (j < 2)
3960 : {
3961 4265 : orig[j] = ref;
3962 : /* Track what element type was actually extracted (which may
3963 : differ in signedness from the vector's element type due to
3964 : tree_nop_conversion_p). */
3965 4265 : if (!orig_elem_type[j])
3966 1987 : orig_elem_type[j] = TREE_TYPE (op1);
3967 4265 : if (elem != i || j != 0)
3968 1850 : maybe_ident = false;
3969 4265 : if (elem != i)
3970 1794 : maybe_blend[j] = false;
3971 8665 : for (unsigned int k = 0; k < nsubelts; ++k)
3972 4400 : elts.safe_push (std::make_pair (j, elem + k));
3973 4265 : tsubelts += nsubelts;
3974 4265 : continue;
3975 4265 : }
3976 : /* Else fallthru. */
3977 : }
3978 : /* Handle elements not extracted from a vector.
3979 : 1. constants by permuting with constant vector
3980 : 2. a unique non-constant element by permuting with a splat vector */
3981 397743 : if (orig[1]
3982 245924 : && orig[1] != error_mark_node)
3983 : return false;
3984 397726 : orig[1] = error_mark_node;
3985 397726 : if (VECTOR_TYPE_P (TREE_TYPE (elt->value))
3986 397726 : && !TYPE_VECTOR_SUBPARTS (TREE_TYPE (elt->value))
3987 3336 : .is_constant (&nsubelts))
3988 : return false;
3989 397726 : if (CONSTANT_CLASS_P (elt->value))
3990 : {
3991 25998 : if (one_nonconstant)
3992 : return false;
3993 17710 : if (!one_constant)
3994 8456 : one_constant = TREE_CODE (elt->value) == VECTOR_CST
3995 8456 : ? VECTOR_CST_ELT (elt->value, 0)
3996 : : elt->value;
3997 17710 : if (TREE_CODE (elt->value) == VECTOR_CST)
3998 : {
3999 347 : for (unsigned int k = 0; k < nsubelts; k++)
4000 255 : constants[tsubelts + k] = VECTOR_CST_ELT (elt->value, k);
4001 : }
4002 : else
4003 17618 : constants[tsubelts] = elt->value;
4004 : }
4005 : else
4006 : {
4007 371728 : if (one_constant)
4008 : return false;
4009 363562 : subelt = VECTOR_TYPE_P (TREE_TYPE (elt->value))
4010 363562 : ? ssa_uniform_vector_p (elt->value)
4011 : : elt->value;
4012 363562 : if (!subelt)
4013 : return false;
4014 360420 : if (!one_nonconstant)
4015 : one_nonconstant = subelt;
4016 220199 : else if (!operand_equal_p (one_nonconstant, subelt, 0))
4017 : return false;
4018 : }
4019 553861 : for (unsigned int k = 0; k < nsubelts; ++k)
4020 277013 : elts.safe_push (std::make_pair (1, tsubelts + k));
4021 276848 : tsubelts += nsubelts;
4022 276848 : maybe_ident = false;
4023 : }
4024 :
4025 63912 : if (elts.length () < nelts)
4026 : return false;
4027 :
4028 30710 : if (! orig[0]
4029 30710 : || ! VECTOR_TYPE_P (TREE_TYPE (orig[0])))
4030 : return false;
4031 1309 : refnelts = TYPE_VECTOR_SUBPARTS (TREE_TYPE (orig[0])).to_constant ();
4032 : /* We currently do not handle larger destination vectors. */
4033 1309 : if (refnelts < nelts)
4034 : return false;
4035 :
4036 : /* Determine the element type for the conversion source.
4037 : As orig_elem_type keeps track of the original type, check
4038 : if we need to perform a sign swap after permuting.
4039 : We need to be able to construct a vector type from the element
4040 : type which is not possible for e.g. BitInt or pointers
4041 : so pun with an integer type if needed. */
4042 1146 : tree perm_eltype = TREE_TYPE (TREE_TYPE (orig[0]));
4043 1146 : bool sign_change_p = false;
4044 1146 : if (conv_code != ERROR_MARK
4045 300 : && orig_elem_type[0]
4046 1446 : && TYPE_SIGN (orig_elem_type[0]) != TYPE_SIGN (perm_eltype))
4047 : {
4048 38 : perm_eltype = signed_or_unsigned_type_for
4049 38 : (TYPE_UNSIGNED (orig_elem_type[0]), perm_eltype);
4050 38 : sign_change_p = true;
4051 : }
4052 1146 : tree conv_src_type = build_vector_type (perm_eltype, nelts);
4053 :
4054 1146 : if (maybe_ident)
4055 : {
4056 : /* When there is no conversion, use the target type directly. */
4057 433 : if (conv_code == ERROR_MARK && nelts != refnelts)
4058 433 : conv_src_type = type;
4059 433 : if (conv_code != ERROR_MARK
4060 433 : && !supportable_convert_operation (conv_code, type, conv_src_type,
4061 : &conv_code))
4062 : {
4063 : /* Only few targets implement direct conversion patterns so try
4064 : some simple special cases via VEC_[UN]PACK[_FLOAT]_LO_EXPR. */
4065 46 : optab optab;
4066 46 : insn_code icode;
4067 46 : tree halfvectype, dblvectype;
4068 46 : enum tree_code unpack_op;
4069 :
4070 46 : if (!BYTES_BIG_ENDIAN)
4071 84 : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
4072 46 : ? VEC_UNPACK_FLOAT_LO_EXPR
4073 : : VEC_UNPACK_LO_EXPR);
4074 : else
4075 : unpack_op = (FLOAT_TYPE_P (TREE_TYPE (type))
4076 : ? VEC_UNPACK_FLOAT_HI_EXPR
4077 : : VEC_UNPACK_HI_EXPR);
4078 :
4079 : /* Conversions between DFP and FP have no special tree code
4080 : but we cannot handle those since all relevant vector conversion
4081 : optabs only have a single mode. */
4082 6 : if (CONVERT_EXPR_CODE_P (conv_code)
4083 40 : && FLOAT_TYPE_P (TREE_TYPE (type))
4084 50 : && (DECIMAL_FLOAT_TYPE_P (TREE_TYPE (type))
4085 2 : != DECIMAL_FLOAT_TYPE_P (TREE_TYPE (conv_src_type))))
4086 : return false;
4087 :
4088 6 : if (CONVERT_EXPR_CODE_P (conv_code)
4089 39 : && (2 * TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
4090 39 : == TYPE_PRECISION (TREE_TYPE (type)))
4091 0 : && orig_elem_type[0]
4092 0 : && useless_type_conversion_p (orig_elem_type[0],
4093 0 : TREE_TYPE (TREE_TYPE (orig[0])))
4094 0 : && mode_for_vector (as_a <scalar_mode>
4095 0 : (TYPE_MODE (TREE_TYPE (TREE_TYPE (orig[0])))),
4096 0 : nelts * 2).exists ()
4097 0 : && (dblvectype
4098 0 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
4099 0 : nelts * 2))
4100 : /* Only use it for vector modes or for vector booleans
4101 : represented as scalar bitmasks. See PR95528. */
4102 0 : && (VECTOR_MODE_P (TYPE_MODE (dblvectype))
4103 0 : || VECTOR_BOOLEAN_TYPE_P (dblvectype))
4104 0 : && (optab = optab_for_tree_code (unpack_op,
4105 : dblvectype,
4106 : optab_default))
4107 0 : && ((icode = optab_handler (optab, TYPE_MODE (dblvectype)))
4108 : != CODE_FOR_nothing)
4109 45 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
4110 : {
4111 0 : gimple_seq stmts = NULL;
4112 0 : tree dbl;
4113 0 : if (refnelts == nelts)
4114 : {
4115 : /* ??? Paradoxical subregs don't exist, so insert into
4116 : the lower half of a wider zero vector. */
4117 0 : dbl = gimple_build (&stmts, BIT_INSERT_EXPR, dblvectype,
4118 : build_zero_cst (dblvectype), orig[0],
4119 0 : bitsize_zero_node);
4120 : }
4121 0 : else if (refnelts == 2 * nelts)
4122 : dbl = orig[0];
4123 : else
4124 0 : dbl = gimple_build (&stmts, BIT_FIELD_REF, dblvectype,
4125 0 : orig[0], TYPE_SIZE (dblvectype),
4126 0 : bitsize_zero_node);
4127 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4128 0 : gimple_assign_set_rhs_with_ops (gsi, unpack_op, dbl);
4129 : }
4130 6 : else if (CONVERT_EXPR_CODE_P (conv_code)
4131 39 : && (TYPE_PRECISION (TREE_TYPE (TREE_TYPE (orig[0])))
4132 39 : == 2 * TYPE_PRECISION (TREE_TYPE (type)))
4133 1 : && orig_elem_type[0]
4134 1 : && useless_type_conversion_p (orig_elem_type[0],
4135 1 : TREE_TYPE (TREE_TYPE (orig[0])))
4136 1 : && mode_for_vector (as_a <scalar_mode>
4137 1 : (TYPE_MODE
4138 : (TREE_TYPE (TREE_TYPE (orig[0])))),
4139 2 : nelts / 2).exists ()
4140 1 : && (halfvectype
4141 1 : = build_vector_type (TREE_TYPE (TREE_TYPE (orig[0])),
4142 1 : nelts / 2))
4143 : /* Only use it for vector modes or for vector booleans
4144 : represented as scalar bitmasks. See PR95528. */
4145 1 : && (VECTOR_MODE_P (TYPE_MODE (halfvectype))
4146 0 : || VECTOR_BOOLEAN_TYPE_P (halfvectype))
4147 1 : && (optab = optab_for_tree_code (VEC_PACK_TRUNC_EXPR,
4148 : halfvectype,
4149 : optab_default))
4150 1 : && ((icode = optab_handler (optab, TYPE_MODE (halfvectype)))
4151 : != CODE_FOR_nothing)
4152 46 : && (insn_data[icode].operand[0].mode == TYPE_MODE (type)))
4153 : {
4154 0 : gimple_seq stmts = NULL;
4155 0 : tree low = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
4156 0 : orig[0], TYPE_SIZE (halfvectype),
4157 0 : bitsize_zero_node);
4158 0 : tree hig = gimple_build (&stmts, BIT_FIELD_REF, halfvectype,
4159 0 : orig[0], TYPE_SIZE (halfvectype),
4160 0 : TYPE_SIZE (halfvectype));
4161 0 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4162 0 : gimple_assign_set_rhs_with_ops (gsi, VEC_PACK_TRUNC_EXPR,
4163 : low, hig);
4164 : }
4165 : else
4166 45 : return false;
4167 0 : update_stmt (gsi_stmt (*gsi));
4168 0 : return true;
4169 : }
4170 387 : if (nelts != refnelts)
4171 : {
4172 14 : gassign *lowpart
4173 14 : = gimple_build_assign (make_ssa_name (conv_src_type),
4174 : build3 (BIT_FIELD_REF, conv_src_type,
4175 14 : orig[0], TYPE_SIZE (conv_src_type),
4176 : bitsize_zero_node));
4177 14 : gsi_insert_before (gsi, lowpart, GSI_SAME_STMT);
4178 14 : orig[0] = gimple_assign_lhs (lowpart);
4179 : }
4180 373 : else if (sign_change_p)
4181 : {
4182 0 : gassign *conv
4183 0 : = gimple_build_assign (make_ssa_name (conv_src_type),
4184 : build1 (VIEW_CONVERT_EXPR, conv_src_type,
4185 : orig[0]));
4186 0 : gsi_insert_before (gsi, conv, GSI_SAME_STMT);
4187 0 : orig[0] = gimple_assign_lhs (conv);
4188 : }
4189 387 : if (conv_code == ERROR_MARK)
4190 : {
4191 370 : tree src_type = TREE_TYPE (orig[0]);
4192 370 : if (!useless_type_conversion_p (type, src_type))
4193 : {
4194 0 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
4195 : TYPE_VECTOR_SUBPARTS (src_type))
4196 : && tree_nop_conversion_p (TREE_TYPE (type),
4197 : TREE_TYPE (src_type)));
4198 0 : tree rhs = build1 (VIEW_CONVERT_EXPR, type, orig[0]);
4199 0 : orig[0] = make_ssa_name (type);
4200 0 : gassign *assign = gimple_build_assign (orig[0], rhs);
4201 0 : gsi_insert_before (gsi, assign, GSI_SAME_STMT);
4202 : }
4203 370 : gimple_assign_set_rhs_from_tree (gsi, orig[0]);
4204 : }
4205 : else
4206 17 : gimple_assign_set_rhs_with_ops (gsi, conv_code, orig[0],
4207 : NULL_TREE, NULL_TREE);
4208 : }
4209 : else
4210 : {
4211 : /* If we combine a vector with a non-vector avoid cases where
4212 : we'll obviously end up with more GIMPLE stmts which is when
4213 : we'll later not fold this to a single insert into the vector
4214 : and we had a single extract originally. See PR92819. */
4215 713 : if (nelts == 2
4216 466 : && refnelts > 2
4217 103 : && orig[1] == error_mark_node
4218 32 : && !maybe_blend[0])
4219 238 : return false;
4220 687 : tree mask_type, perm_type;
4221 687 : perm_type = TREE_TYPE (orig[0]);
4222 687 : if (conv_code != ERROR_MARK
4223 687 : && !supportable_convert_operation (conv_code, type, conv_src_type,
4224 : &conv_code))
4225 : return false;
4226 :
4227 : /* Now that we know the number of elements of the source build the
4228 : permute vector.
4229 : ??? When the second vector has constant values we can shuffle
4230 : it and its source indexes to make the permutation supported.
4231 : For now it mimics a blend. */
4232 516 : vec_perm_builder sel (refnelts, refnelts, 1);
4233 516 : bool all_same_p = true;
4234 5430 : for (i = 0; i < elts.length (); ++i)
4235 : {
4236 2199 : sel.quick_push (elts[i].second + elts[i].first * refnelts);
4237 2199 : all_same_p &= known_eq (sel[i], sel[0]);
4238 : }
4239 : /* And fill the tail with "something". It's really don't care,
4240 : and ideally we'd allow VEC_PERM to have a smaller destination
4241 : vector. As a heuristic:
4242 :
4243 : (a) if what we have so far duplicates a single element, make the
4244 : tail do the same
4245 :
4246 : (b) otherwise preserve a uniform orig[0]. This facilitates
4247 : later pattern-matching of VEC_PERM_EXPR to a BIT_INSERT_EXPR. */
4248 959 : for (; i < refnelts; ++i)
4249 886 : sel.quick_push (all_same_p
4250 1329 : ? sel[0]
4251 92 : : (elts[0].second == 0 && elts[0].first == 0
4252 604 : ? 0 : refnelts) + i);
4253 679 : vec_perm_indices indices (sel, orig[1] ? 2 : 1, refnelts);
4254 516 : machine_mode vmode = TYPE_MODE (perm_type);
4255 516 : if ((cfun->curr_properties & PROP_gimple_lvec)
4256 516 : && !can_vec_perm_const_p (vmode, vmode, indices))
4257 : return false;
4258 475 : mask_type = build_vector_type (ssizetype, refnelts);
4259 475 : tree op2 = vec_perm_indices_to_tree (mask_type, indices);
4260 475 : bool converted_orig1 = false;
4261 475 : gimple_seq stmts = NULL;
4262 475 : if (!orig[1])
4263 150 : orig[1] = orig[0];
4264 325 : else if (orig[1] == error_mark_node
4265 206 : && one_nonconstant)
4266 : {
4267 : /* ??? We can see if we can safely convert to the original
4268 : element type. */
4269 79 : converted_orig1 = conv_code != ERROR_MARK;
4270 79 : tree target_type = converted_orig1 ? type : perm_type;
4271 79 : tree nonconstant_for_splat = one_nonconstant;
4272 : /* If there's a nop conversion between the target element type and
4273 : the nonconstant's type, convert it. */
4274 79 : if (!useless_type_conversion_p (TREE_TYPE (target_type),
4275 79 : TREE_TYPE (one_nonconstant)))
4276 0 : nonconstant_for_splat
4277 0 : = gimple_build (&stmts, NOP_EXPR, TREE_TYPE (target_type),
4278 : one_nonconstant);
4279 79 : orig[1] = gimple_build_vector_from_val (&stmts, UNKNOWN_LOCATION,
4280 : target_type,
4281 : nonconstant_for_splat);
4282 79 : }
4283 246 : else if (orig[1] == error_mark_node)
4284 : {
4285 : /* ??? See if we can convert the vector to the original type. */
4286 127 : converted_orig1 = conv_code != ERROR_MARK;
4287 127 : unsigned n = converted_orig1 ? nelts : refnelts;
4288 110 : tree target_type = converted_orig1 ? type : perm_type;
4289 127 : tree_vector_builder vec (target_type, n, 1);
4290 1011 : for (unsigned i = 0; i < n; ++i)
4291 1692 : if (i < nelts && constants[i])
4292 : {
4293 451 : tree constant = constants[i];
4294 : /* If there's a nop conversion, convert the constant. */
4295 451 : if (!useless_type_conversion_p (TREE_TYPE (target_type),
4296 451 : TREE_TYPE (constant)))
4297 2 : constant = fold_convert (TREE_TYPE (target_type), constant);
4298 451 : vec.quick_push (constant);
4299 : }
4300 : else
4301 : {
4302 : /* ??? Push a don't-care value. */
4303 433 : tree constant = one_constant;
4304 433 : if (!useless_type_conversion_p (TREE_TYPE (target_type),
4305 433 : TREE_TYPE (constant)))
4306 2 : constant = fold_convert (TREE_TYPE (target_type), constant);
4307 433 : vec.quick_push (constant);
4308 : }
4309 127 : orig[1] = vec.build ();
4310 127 : }
4311 356 : tree blend_op2 = NULL_TREE;
4312 356 : if (converted_orig1)
4313 : {
4314 : /* Make sure we can do a blend in the target type. */
4315 19 : vec_perm_builder sel (nelts, nelts, 1);
4316 87 : for (i = 0; i < elts.length (); ++i)
4317 68 : sel.quick_push (elts[i].first
4318 68 : ? elts[i].second + nelts : i);
4319 19 : vec_perm_indices indices (sel, 2, nelts);
4320 19 : machine_mode vmode = TYPE_MODE (type);
4321 19 : if ((cfun->curr_properties & PROP_gimple_lvec)
4322 19 : && !can_vec_perm_const_p (vmode, vmode, indices))
4323 0 : return false;
4324 19 : mask_type = build_vector_type (ssizetype, nelts);
4325 19 : blend_op2 = vec_perm_indices_to_tree (mask_type, indices);
4326 19 : }
4327 :
4328 : /* For a real orig[1] (no splat, constant etc.) we might need to
4329 : nop-convert it. Do so here. */
4330 475 : if (orig[1] && orig[1] != error_mark_node
4331 475 : && !useless_type_conversion_p (perm_type, TREE_TYPE (orig[1]))
4332 494 : && tree_nop_conversion_p (TREE_TYPE (perm_type),
4333 19 : TREE_TYPE (TREE_TYPE (orig[1]))))
4334 0 : orig[1] = gimple_build (&stmts, VIEW_CONVERT_EXPR, perm_type,
4335 : orig[1]);
4336 :
4337 475 : tree orig1_for_perm
4338 475 : = converted_orig1 ? build_zero_cst (perm_type) : orig[1];
4339 475 : tree res = gimple_build (&stmts, VEC_PERM_EXPR, perm_type,
4340 : orig[0], orig1_for_perm, op2);
4341 : /* If we're building a smaller vector, extract the element
4342 : with the proper type. */
4343 475 : if (nelts != refnelts)
4344 190 : res = gimple_build (&stmts, BIT_FIELD_REF,
4345 : conv_code != ERROR_MARK ? conv_src_type : type,
4346 : res,
4347 95 : TYPE_SIZE (conv_code != ERROR_MARK ? conv_src_type
4348 : : type),
4349 95 : bitsize_zero_node);
4350 : /* Otherwise, we can still have an intermediate sign change.
4351 : ??? In that case we have two subsequent conversions.
4352 : We should be able to merge them. */
4353 380 : else if (sign_change_p)
4354 14 : res = gimple_build (&stmts, VIEW_CONVERT_EXPR, conv_src_type, res);
4355 : /* Finally, apply the conversion. */
4356 475 : if (conv_code != ERROR_MARK)
4357 52 : res = gimple_build (&stmts, conv_code, type, res);
4358 423 : else if (!useless_type_conversion_p (type, TREE_TYPE (res)))
4359 : {
4360 3 : gcc_assert (known_eq (TYPE_VECTOR_SUBPARTS (type),
4361 : TYPE_VECTOR_SUBPARTS (perm_type))
4362 : && tree_nop_conversion_p (TREE_TYPE (type),
4363 : TREE_TYPE (perm_type)));
4364 3 : res = gimple_build (&stmts, VIEW_CONVERT_EXPR, type, res);
4365 : }
4366 : /* Blend in the actual constant. */
4367 475 : if (converted_orig1)
4368 19 : res = gimple_build (&stmts, VEC_PERM_EXPR, type,
4369 19 : res, orig[1], blend_op2);
4370 475 : gsi_insert_seq_before (gsi, stmts, GSI_SAME_STMT);
4371 475 : gimple_assign_set_rhs_with_ops (gsi, SSA_NAME, res);
4372 516 : }
4373 862 : update_stmt (gsi_stmt (*gsi));
4374 862 : return true;
4375 152851 : }
4376 :
4377 : /* Prepare a TARGET_MEM_REF ref so that it can be subsetted as
4378 : lvalue. This splits out an address computation stmt before *GSI
4379 : and returns a MEM_REF wrapping the address. */
4380 :
4381 : static tree
4382 1104 : prepare_target_mem_ref_lvalue (tree ref, gimple_stmt_iterator *gsi)
4383 : {
4384 1104 : if (TREE_CODE (TREE_OPERAND (ref, 0)) == ADDR_EXPR)
4385 218 : mark_addressable (TREE_OPERAND (TREE_OPERAND (ref, 0), 0));
4386 1104 : tree ptrtype = build_pointer_type (TREE_TYPE (ref));
4387 1104 : tree tem = make_ssa_name (ptrtype);
4388 1104 : gimple *new_stmt
4389 1104 : = gimple_build_assign (tem, build1 (ADDR_EXPR, TREE_TYPE (tem),
4390 : unshare_expr (ref)));
4391 1104 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4392 2208 : ref = build2_loc (EXPR_LOCATION (ref),
4393 1104 : MEM_REF, TREE_TYPE (ref), tem,
4394 1104 : build_int_cst (TREE_TYPE (TREE_OPERAND (ref, 1)), 0));
4395 1104 : return ref;
4396 : }
4397 :
4398 : /* Rewrite the vector load at *GSI to component-wise loads if the load
4399 : is only used in BIT_FIELD_REF extractions with eventual intermediate
4400 : widening. */
4401 :
4402 : static void
4403 290866 : optimize_vector_load (gimple_stmt_iterator *gsi)
4404 : {
4405 290866 : gimple *stmt = gsi_stmt (*gsi);
4406 290866 : tree lhs = gimple_assign_lhs (stmt);
4407 290866 : tree rhs = gimple_assign_rhs1 (stmt);
4408 290866 : tree vuse = gimple_vuse (stmt);
4409 :
4410 : /* Gather BIT_FIELD_REFs to rewrite, looking through
4411 : VEC_UNPACK_{LO,HI}_EXPR. */
4412 290866 : use_operand_p use_p;
4413 290866 : imm_use_iterator iter;
4414 290866 : bool rewrite = true;
4415 290866 : bool scalar_use = false;
4416 290866 : bool unpack_use = false;
4417 290866 : auto_vec<gimple *, 8> bf_stmts;
4418 290866 : auto_vec<tree, 8> worklist;
4419 290866 : worklist.quick_push (lhs);
4420 292782 : do
4421 : {
4422 292782 : tree def = worklist.pop ();
4423 292782 : unsigned HOST_WIDE_INT def_eltsize
4424 292782 : = TREE_INT_CST_LOW (TYPE_SIZE (TREE_TYPE (TREE_TYPE (def))));
4425 661636 : FOR_EACH_IMM_USE_FAST (use_p, iter, def)
4426 : {
4427 348402 : gimple *use_stmt = USE_STMT (use_p);
4428 348402 : if (is_gimple_debug (use_stmt))
4429 76072 : continue;
4430 347818 : tree use_lhs;
4431 347818 : if (!is_gimple_assign (use_stmt)
4432 : /* For alias reasons we move the use to the place of the
4433 : load. Avoid this when abnormals are involved. */
4434 347818 : || ((TREE_CODE ((use_lhs = gimple_assign_lhs (use_stmt)))
4435 : == SSA_NAME)
4436 237718 : && SSA_NAME_OCCURS_IN_ABNORMAL_PHI (use_lhs)))
4437 : {
4438 : rewrite = false;
4439 272330 : break;
4440 : }
4441 312040 : enum tree_code use_code = gimple_assign_rhs_code (use_stmt);
4442 312040 : tree use_rhs = gimple_assign_rhs1 (use_stmt);
4443 383715 : if (use_code == BIT_FIELD_REF
4444 71676 : && TREE_OPERAND (use_rhs, 0) == def
4445 : /* If its on the VEC_UNPACK_{HI,LO}_EXPR
4446 : def need to verify it is element aligned. */
4447 383716 : && (def == lhs
4448 153 : || (known_eq (bit_field_size (use_rhs), def_eltsize)
4449 153 : && constant_multiple_p (bit_field_offset (use_rhs),
4450 : def_eltsize)
4451 : /* We can simulate the VEC_UNPACK_{HI,LO}_EXPR
4452 : via a NOP_EXPR only for integral types.
4453 : ??? Support VEC_UNPACK_FLOAT_{HI,LO}_EXPR. */
4454 153 : && INTEGRAL_TYPE_P (TREE_TYPE (use_rhs)))))
4455 : {
4456 71675 : if (!VECTOR_TYPE_P (TREE_TYPE (gimple_assign_lhs (use_stmt))))
4457 69569 : scalar_use = true;
4458 71675 : bf_stmts.safe_push (use_stmt);
4459 71675 : continue;
4460 : }
4461 : /* Walk through one level of VEC_UNPACK_{LO,HI}_EXPR. */
4462 240365 : if (def == lhs
4463 238530 : && (use_code == VEC_UNPACK_HI_EXPR
4464 238530 : || use_code == VEC_UNPACK_LO_EXPR)
4465 3813 : && use_rhs == lhs)
4466 : {
4467 3813 : unpack_use = true;
4468 3813 : worklist.safe_push (gimple_assign_lhs (use_stmt));
4469 3813 : continue;
4470 : }
4471 : rewrite = false;
4472 : break;
4473 292782 : }
4474 292782 : if (!rewrite)
4475 : break;
4476 : }
4477 40904 : while (!worklist.is_empty ());
4478 :
4479 290866 : rewrite = rewrite && (scalar_use
4480 18536 : || unpack_use
4481 588 : || !can_implement_p (mov_optab,
4482 588 : TYPE_MODE (TREE_TYPE (lhs))));
4483 290866 : if (!rewrite)
4484 : {
4485 272500 : gsi_next (gsi);
4486 272500 : return;
4487 : }
4488 : /* We now have all ultimate uses of the load to rewrite in bf_stmts. */
4489 :
4490 : /* Prepare the original ref to be wrapped in adjusted BIT_FIELD_REFs.
4491 : For TARGET_MEM_REFs we have to separate the LEA from the reference. */
4492 18366 : tree load_rhs = rhs;
4493 18366 : if (TREE_CODE (load_rhs) == TARGET_MEM_REF)
4494 1103 : load_rhs = prepare_target_mem_ref_lvalue (load_rhs, gsi);
4495 :
4496 : /* Rewrite the BIT_FIELD_REFs to be actual loads, re-emitting them at
4497 : the place of the original load. */
4498 120822 : for (gimple *use_stmt : bf_stmts)
4499 : {
4500 65724 : tree bfr = gimple_assign_rhs1 (use_stmt);
4501 65724 : tree new_rhs = unshare_expr (load_rhs);
4502 65724 : if (TREE_OPERAND (bfr, 0) != lhs)
4503 : {
4504 : /* When the BIT_FIELD_REF is on the promoted vector we have to
4505 : adjust it and emit a conversion afterwards. */
4506 152 : gimple *def_stmt
4507 152 : = SSA_NAME_DEF_STMT (TREE_OPERAND (bfr, 0));
4508 152 : enum tree_code def_code
4509 152 : = gimple_assign_rhs_code (def_stmt);
4510 :
4511 : /* The adjusted BIT_FIELD_REF is of the promotion source
4512 : vector size and at half of the offset... */
4513 152 : new_rhs = fold_build3 (BIT_FIELD_REF,
4514 : TREE_TYPE (TREE_TYPE (lhs)),
4515 : new_rhs,
4516 : TYPE_SIZE (TREE_TYPE (TREE_TYPE (lhs))),
4517 : size_binop (EXACT_DIV_EXPR,
4518 : TREE_OPERAND (bfr, 2),
4519 : bitsize_int (2)));
4520 : /* ... and offsetted by half of the vector if VEC_UNPACK_HI_EXPR. */
4521 152 : if (def_code == (!BYTES_BIG_ENDIAN
4522 : ? VEC_UNPACK_HI_EXPR : VEC_UNPACK_LO_EXPR))
4523 76 : TREE_OPERAND (new_rhs, 2)
4524 152 : = size_binop (PLUS_EXPR, TREE_OPERAND (new_rhs, 2),
4525 : size_binop (EXACT_DIV_EXPR,
4526 : TYPE_SIZE (TREE_TYPE (lhs)),
4527 : bitsize_int (2)));
4528 152 : tree tem = make_ssa_name (TREE_TYPE (TREE_TYPE (lhs)));
4529 152 : gimple *new_stmt = gimple_build_assign (tem, new_rhs);
4530 152 : location_t loc = gimple_location (use_stmt);
4531 152 : gimple_set_location (new_stmt, loc);
4532 152 : gimple_set_vuse (new_stmt, vuse);
4533 152 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4534 : /* Perform scalar promotion. */
4535 152 : new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
4536 : NOP_EXPR, tem);
4537 152 : gimple_set_location (new_stmt, loc);
4538 152 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4539 : }
4540 : else
4541 : {
4542 : /* When the BIT_FIELD_REF is on the original load result
4543 : we can just wrap that. */
4544 65572 : tree new_rhs = fold_build3 (BIT_FIELD_REF, TREE_TYPE (bfr),
4545 : unshare_expr (load_rhs),
4546 : TREE_OPERAND (bfr, 1),
4547 : TREE_OPERAND (bfr, 2));
4548 65572 : gimple *new_stmt = gimple_build_assign (gimple_assign_lhs (use_stmt),
4549 : new_rhs);
4550 65572 : location_t loc = gimple_location (use_stmt);
4551 65572 : gimple_set_location (new_stmt, loc);
4552 65572 : gimple_set_vuse (new_stmt, vuse);
4553 65572 : gsi_insert_before (gsi, new_stmt, GSI_SAME_STMT);
4554 : }
4555 65724 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4556 65724 : unlink_stmt_vdef (use_stmt);
4557 65724 : gsi_remove (&gsi2, true);
4558 : }
4559 :
4560 : /* Finally get rid of the intermediate stmts. */
4561 18366 : gimple *use_stmt;
4562 37060 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
4563 : {
4564 328 : if (is_gimple_debug (use_stmt))
4565 : {
4566 266 : if (gimple_debug_bind_p (use_stmt))
4567 : {
4568 266 : gimple_debug_bind_reset_value (use_stmt);
4569 266 : update_stmt (use_stmt);
4570 : }
4571 266 : continue;
4572 : }
4573 62 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
4574 62 : unlink_stmt_vdef (use_stmt);
4575 62 : release_defs (use_stmt);
4576 62 : gsi_remove (&gsi2, true);
4577 18366 : }
4578 : /* And the original load. */
4579 18366 : release_defs (stmt);
4580 18366 : gsi_remove (gsi, true);
4581 290866 : }
4582 :
4583 :
4584 : /* Primitive "lattice" function for gimple_simplify. */
4585 :
4586 : static tree
4587 1607870015 : fwprop_ssa_val (tree name)
4588 : {
4589 : /* First valueize NAME. */
4590 1607870015 : if (TREE_CODE (name) == SSA_NAME
4591 1607870015 : && SSA_NAME_VERSION (name) < lattice.length ())
4592 : {
4593 1607028059 : tree val = lattice[SSA_NAME_VERSION (name)];
4594 1607028059 : if (val)
4595 1607870015 : name = val;
4596 : }
4597 : /* We continue matching along SSA use-def edges for SSA names
4598 : that are not single-use. Currently there are no patterns
4599 : that would cause any issues with that. */
4600 1607870015 : return name;
4601 : }
4602 :
4603 : /* Search for opportunities to free half of the lanes in the following pattern:
4604 :
4605 : v_in = {e0, e1, e2, e3}
4606 : v_1 = VEC_PERM <v_in, v_in, {0, 2, 0, 2}>
4607 : // v_1 = {e0, e2, e0, e2}
4608 : v_2 = VEC_PERM <v_in, v_in, {1, 3, 1, 3}>
4609 : // v_2 = {e1, e3, e1, e3}
4610 :
4611 : v_x = v_1 + v_2
4612 : // v_x = {e0+e1, e2+e3, e0+e1, e2+e3}
4613 : v_y = v_1 - v_2
4614 : // v_y = {e0-e1, e2-e3, e0-e1, e2-e3}
4615 :
4616 : v_out = VEC_PERM <v_x, v_y, {0, 1, 6, 7}>
4617 : // v_out = {e0+e1, e2+e3, e0-e1, e2-e3}
4618 :
4619 : The last statement could be simplified to:
4620 : v_out' = VEC_PERM <v_x, v_y, {0, 1, 4, 5}>
4621 : // v_out' = {e0+e1, e2+e3, e0-e1, e2-e3}
4622 :
4623 : Characteristic properties:
4624 : - v_1 and v_2 are created from the same input vector v_in and introduce the
4625 : lane duplication (in the selection operand) that we can eliminate.
4626 : - v_x and v_y are results from lane-preserving operations that use v_1 and
4627 : v_2 as inputs.
4628 : - v_out is created by selecting from duplicated lanes. */
4629 :
4630 : static bool
4631 182938 : recognise_vec_perm_simplify_seq (gassign *stmt, vec_perm_simplify_seq *seq)
4632 : {
4633 182938 : unsigned HOST_WIDE_INT nelts;
4634 :
4635 182938 : gcc_checking_assert (stmt);
4636 182938 : gcc_checking_assert (gimple_assign_rhs_code (stmt) == VEC_PERM_EXPR);
4637 182938 : basic_block bb = gimple_bb (stmt);
4638 :
4639 : /* Decompose the final vec permute statement. */
4640 182938 : tree v_x = gimple_assign_rhs1 (stmt);
4641 182938 : tree v_y = gimple_assign_rhs2 (stmt);
4642 182938 : tree sel = gimple_assign_rhs3 (stmt);
4643 :
4644 182938 : if (TREE_CODE (sel) != VECTOR_CST
4645 180169 : || !VECTOR_CST_NELTS (sel).is_constant (&nelts)
4646 180169 : || TREE_CODE (v_x) != SSA_NAME
4647 178317 : || TREE_CODE (v_y) != SSA_NAME
4648 174280 : || !has_single_use (v_x)
4649 292714 : || !has_single_use (v_y))
4650 74888 : return false;
4651 :
4652 : /* Don't analyse sequences with many lanes. */
4653 108050 : if (nelts > 4)
4654 : return false;
4655 :
4656 : /* Lookup the definition of v_x and v_y. */
4657 105339 : gassign *v_x_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x));
4658 105339 : gassign *v_y_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_y));
4659 104962 : if (!v_x_stmt || gimple_bb (v_x_stmt) != bb
4660 210301 : || !v_y_stmt || gimple_bb (v_y_stmt) != bb)
4661 : return false;
4662 :
4663 : /* Check the operations that define v_x and v_y. */
4664 104955 : if (TREE_CODE_CLASS (gimple_assign_rhs_code (v_x_stmt)) != tcc_binary
4665 107007 : || TREE_CODE_CLASS (gimple_assign_rhs_code (v_y_stmt)) != tcc_binary)
4666 : return false;
4667 :
4668 2052 : tree v_x_1 = gimple_assign_rhs1 (v_x_stmt);
4669 2052 : tree v_x_2 = gimple_assign_rhs2 (v_x_stmt);
4670 2052 : tree v_y_1 = gimple_assign_rhs1 (v_y_stmt);
4671 2052 : tree v_y_2 = gimple_assign_rhs2 (v_y_stmt);
4672 :
4673 2052 : if (v_x_stmt == v_y_stmt
4674 2052 : || TREE_CODE (v_x_1) != SSA_NAME
4675 2049 : || TREE_CODE (v_x_2) != SSA_NAME
4676 2037 : || num_imm_uses (v_x_1) != 2
4677 3933 : || num_imm_uses (v_x_2) != 2)
4678 : return false;
4679 :
4680 1845 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
4681 : {
4682 : /* Allow operands of commutative operators to swap. */
4683 637 : if (commutative_tree_code (gimple_assign_rhs_code (v_x_stmt)))
4684 : {
4685 : /* Keep v_x_1 the first operand for non-commutative operators. */
4686 247 : std::swap (v_x_1, v_x_2);
4687 247 : if (v_x_1 != v_y_1 || v_x_2 != v_y_2)
4688 : return false;
4689 : }
4690 390 : else if (commutative_tree_code (gimple_assign_rhs_code (v_y_stmt)))
4691 : {
4692 390 : if (v_x_1 != v_y_2 || v_x_2 != v_y_1)
4693 : return false;
4694 : }
4695 : else
4696 : return false;
4697 : }
4698 1845 : gassign *v_1_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_1));
4699 1845 : gassign *v_2_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (v_x_2));
4700 1781 : if (!v_1_stmt || gimple_bb (v_1_stmt) != bb
4701 3626 : || !v_2_stmt || gimple_bb (v_2_stmt) != bb)
4702 : return false;
4703 :
4704 1777 : if (gimple_assign_rhs_code (v_1_stmt) != VEC_PERM_EXPR
4705 1899 : || gimple_assign_rhs_code (v_2_stmt) != VEC_PERM_EXPR)
4706 : return false;
4707 :
4708 : /* Decompose initial VEC_PERM_EXPRs. */
4709 108 : tree v_in = gimple_assign_rhs1 (v_1_stmt);
4710 108 : tree v_1_sel = gimple_assign_rhs3 (v_1_stmt);
4711 108 : tree v_2_sel = gimple_assign_rhs3 (v_2_stmt);
4712 108 : if (v_in != gimple_assign_rhs2 (v_1_stmt)
4713 103 : || v_in != gimple_assign_rhs1 (v_2_stmt)
4714 209 : || v_in != gimple_assign_rhs2 (v_2_stmt))
4715 : return false;
4716 :
4717 101 : unsigned HOST_WIDE_INT v_1_nelts, v_2_nelts;
4718 101 : if (TREE_CODE (v_1_sel) != VECTOR_CST
4719 101 : || !VECTOR_CST_NELTS (v_1_sel).is_constant (&v_1_nelts)
4720 101 : || TREE_CODE (v_2_sel) != VECTOR_CST
4721 202 : || !VECTOR_CST_NELTS (v_2_sel).is_constant (&v_2_nelts))
4722 0 : return false;
4723 :
4724 101 : if (nelts != v_1_nelts || nelts != v_2_nelts)
4725 : return false;
4726 :
4727 : /* Create the new selector. */
4728 101 : vec_perm_builder new_sel_perm (nelts, nelts, 1);
4729 101 : auto_vec<bool> lanes (nelts);
4730 101 : lanes.quick_grow_cleared (nelts);
4731 505 : for (unsigned int i = 0; i < nelts; i++)
4732 : {
4733 : /* Extract the i-th value from the selector. */
4734 404 : unsigned int sel_cst = TREE_INT_CST_LOW (VECTOR_CST_ELT (sel, i));
4735 404 : unsigned int lane = sel_cst % nelts;
4736 404 : unsigned int offs = sel_cst / nelts;
4737 :
4738 : /* Check what's in the lane. */
4739 404 : unsigned int e_1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, lane));
4740 404 : unsigned int e_2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, lane));
4741 :
4742 : /* Reuse previous lane (if any). */
4743 404 : unsigned int l = 0;
4744 687 : for (; l < lane; l++)
4745 : {
4746 481 : if ((TREE_INT_CST_LOW (VECTOR_CST_ELT (v_1_sel, l)) == e_1)
4747 481 : && (TREE_INT_CST_LOW (VECTOR_CST_ELT (v_2_sel, l)) == e_2))
4748 : break;
4749 : }
4750 :
4751 : /* Add to narrowed selector. */
4752 404 : new_sel_perm.quick_push (l + offs * nelts);
4753 :
4754 : /* Mark lane as used. */
4755 404 : lanes[l] = true;
4756 : }
4757 :
4758 : /* Count how many lanes are need. */
4759 : unsigned int cnt = 0;
4760 505 : for (unsigned int i = 0; i < nelts; i++)
4761 404 : cnt += lanes[i];
4762 :
4763 : /* If more than (nelts/2) lanes are needed, skip the sequence. */
4764 101 : if (cnt > nelts / 2)
4765 : return false;
4766 :
4767 : /* Check if the resulting permutation is cheap. */
4768 101 : vec_perm_indices new_indices (new_sel_perm, 2, nelts);
4769 101 : tree vectype = TREE_TYPE (gimple_assign_lhs (stmt));
4770 101 : machine_mode vmode = TYPE_MODE (vectype);
4771 101 : if (!can_vec_perm_const_p (vmode, vmode, new_indices, false))
4772 : return false;
4773 :
4774 101 : *seq = XNEW (struct _vec_perm_simplify_seq);
4775 101 : (*seq)->stmt = stmt;
4776 101 : (*seq)->v_1_stmt = v_1_stmt;
4777 101 : (*seq)->v_2_stmt = v_2_stmt;
4778 101 : (*seq)->v_x_stmt = v_x_stmt;
4779 101 : (*seq)->v_y_stmt = v_y_stmt;
4780 101 : (*seq)->nelts = nelts;
4781 101 : (*seq)->new_sel = vect_gen_perm_mask_checked (vectype, new_indices);
4782 :
4783 101 : if (dump_file)
4784 : {
4785 28 : fprintf (dump_file, "Found vec perm simplify sequence ending with:\n\t");
4786 28 : print_gimple_stmt (dump_file, stmt, 0);
4787 :
4788 28 : if (dump_flags & TDF_DETAILS)
4789 : {
4790 28 : fprintf (dump_file, "\tNarrowed vec_perm selector: ");
4791 28 : print_generic_expr (dump_file, (*seq)->new_sel);
4792 28 : fprintf (dump_file, "\n");
4793 : }
4794 : }
4795 :
4796 : return true;
4797 202 : }
4798 :
4799 : /* Reduce the lane consumption of a simplifiable vec perm sequence. */
4800 :
4801 : static void
4802 74 : narrow_vec_perm_simplify_seq (const vec_perm_simplify_seq &seq)
4803 : {
4804 74 : gassign *stmt = seq->stmt;
4805 74 : if (dump_file && (dump_flags & TDF_DETAILS))
4806 : {
4807 22 : fprintf (dump_file, "Updating VEC_PERM statement:\n");
4808 22 : fprintf (dump_file, "Old stmt: ");
4809 22 : print_gimple_stmt (dump_file, stmt, 0);
4810 : }
4811 :
4812 : /* Update the last VEC_PERM statement. */
4813 74 : gimple_assign_set_rhs3 (stmt, seq->new_sel);
4814 74 : update_stmt (stmt);
4815 :
4816 74 : if (dump_file && (dump_flags & TDF_DETAILS))
4817 : {
4818 22 : fprintf (dump_file, "New stmt: ");
4819 22 : print_gimple_stmt (dump_file, stmt, 0);
4820 : }
4821 74 : }
4822 :
4823 : /* Test if we can blend two simplifiable vec permute sequences.
4824 : NEED_SWAP will be set, if sequences must be swapped for blending. */
4825 :
4826 : static bool
4827 47 : can_blend_vec_perm_simplify_seqs_p (vec_perm_simplify_seq seq1,
4828 : vec_perm_simplify_seq seq2,
4829 : bool *need_swap)
4830 : {
4831 47 : unsigned int nelts = seq1->nelts;
4832 47 : basic_block bb = gimple_bb (seq1->stmt);
4833 :
4834 47 : gcc_assert (gimple_bb (seq2->stmt) == bb);
4835 :
4836 : /* BBs and number of elements must be equal. */
4837 47 : if (gimple_bb (seq2->stmt) != bb || seq2->nelts != nelts)
4838 : return false;
4839 :
4840 : /* We need vectors of the same type. */
4841 47 : if (TREE_TYPE (gimple_assign_lhs (seq1->stmt))
4842 47 : != TREE_TYPE (gimple_assign_lhs (seq2->stmt)))
4843 : return false;
4844 :
4845 : /* We require isomorphic operators. */
4846 41 : if (((gimple_assign_rhs_code (seq1->v_x_stmt)
4847 41 : != gimple_assign_rhs_code (seq2->v_x_stmt))
4848 41 : || (gimple_assign_rhs_code (seq1->v_y_stmt)
4849 41 : != gimple_assign_rhs_code (seq2->v_y_stmt))))
4850 : return false;
4851 :
4852 : /* We cannot have any dependencies between the sequences.
4853 :
4854 : For merging, we will reuse seq1->v_1_stmt and seq1->v_2_stmt.
4855 : seq1's v_in is defined before these statements, but we need
4856 : to check if seq2's v_in is defined before them as well.
4857 :
4858 : Further, we will reuse seq2->stmt. We need to ensure that
4859 : seq1->v_x_stmt and seq1->v_y_stmt are before it.
4860 :
4861 : Note, that we don't need to check the BBs here, because all
4862 : statements of both sequences have to be in the same BB. */
4863 :
4864 41 : tree seq2_v_in = gimple_assign_rhs1 (seq2->v_1_stmt);
4865 41 : if (TREE_CODE (seq2_v_in) != SSA_NAME)
4866 : return false;
4867 :
4868 41 : gassign *seq2_v_in_stmt = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq2_v_in));
4869 41 : if (!seq2_v_in_stmt || gimple_bb (seq2_v_in_stmt) != bb
4870 41 : || (gimple_uid (seq2_v_in_stmt) > gimple_uid (seq1->v_1_stmt))
4871 37 : || (gimple_uid (seq1->v_x_stmt) > gimple_uid (seq2->stmt))
4872 37 : || (gimple_uid (seq1->v_y_stmt) > gimple_uid (seq2->stmt)))
4873 : {
4874 4 : tree seq1_v_in = gimple_assign_rhs1 (seq1->v_1_stmt);
4875 4 : if (TREE_CODE (seq1_v_in) != SSA_NAME)
4876 : return false;
4877 :
4878 4 : gassign *seq1_v_in_stmt
4879 4 : = dyn_cast<gassign *> (SSA_NAME_DEF_STMT (seq1_v_in));
4880 : /* Let's try to see if we succeed when swapping the sequences. */
4881 4 : if (!seq1_v_in_stmt || gimple_bb (seq1_v_in_stmt)
4882 0 : || (gimple_uid (seq1_v_in_stmt) > gimple_uid (seq2->v_1_stmt))
4883 0 : || (gimple_uid (seq2->v_x_stmt) > gimple_uid (seq1->stmt))
4884 0 : || (gimple_uid (seq2->v_y_stmt) > gimple_uid (seq1->stmt)))
4885 : return false;
4886 0 : *need_swap = true;
4887 : }
4888 : else
4889 37 : *need_swap = false;
4890 :
4891 37 : if (dump_file && (dump_flags & TDF_DETAILS))
4892 11 : fprintf (dump_file, "Found vec perm simplify sequence pair.\n");
4893 :
4894 : return true;
4895 : }
4896 :
4897 : /* Calculate the permutations for blending the two given vec permute
4898 : sequences. This may fail if the resulting permutation is not
4899 : supported. */
4900 :
4901 : static bool
4902 37 : calc_perm_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
4903 : vec_perm_simplify_seq seq2,
4904 : vec_perm_indices *seq2_stmt_indices,
4905 : vec_perm_indices *seq1_v_1_stmt_indices,
4906 : vec_perm_indices *seq1_v_2_stmt_indices)
4907 : {
4908 37 : unsigned int i;
4909 37 : unsigned int nelts = seq1->nelts;
4910 37 : auto_vec<unsigned int> lane_assignment;
4911 37 : lane_assignment.create (nelts);
4912 :
4913 : /* Mark all lanes as free. */
4914 37 : lane_assignment.quick_grow_cleared (nelts);
4915 :
4916 : /* Allocate lanes for seq1. */
4917 185 : for (i = 0; i < nelts; i++)
4918 : {
4919 148 : unsigned int l = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq1->new_sel, i));
4920 148 : l %= nelts;
4921 148 : lane_assignment[l] = 1;
4922 : }
4923 :
4924 : /* Allocate lanes for seq2 and calculate selector for seq2->stmt. */
4925 37 : vec_perm_builder seq2_stmt_sel_perm (nelts, nelts, 1);
4926 185 : for (i = 0; i < nelts; i++)
4927 : {
4928 148 : unsigned int sel = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, i));
4929 148 : unsigned int lane = sel % nelts;
4930 148 : unsigned int offs = sel / nelts;
4931 148 : unsigned int new_sel;
4932 :
4933 : /* Check if we already allocated the lane for seq2. */
4934 148 : unsigned int j = 0;
4935 263 : for (; j < i; j++)
4936 : {
4937 189 : unsigned int sel_old;
4938 189 : sel_old = TREE_INT_CST_LOW (VECTOR_CST_ELT (seq2->new_sel, j));
4939 189 : unsigned int lane_old = sel_old % nelts;
4940 189 : if (lane == lane_old)
4941 : {
4942 74 : new_sel = seq2_stmt_sel_perm[j].to_constant ();
4943 74 : new_sel = (new_sel % nelts) + offs * nelts;
4944 74 : break;
4945 : }
4946 : }
4947 :
4948 : /* If the lane is not allocated, we need to do that now. */
4949 148 : if (j == i)
4950 : {
4951 : unsigned int l_orig = lane;
4952 182 : while (lane_assignment[lane] != 0)
4953 : {
4954 108 : lane = (lane + 1) % nelts;
4955 :
4956 : /* This should not happen if both sequences utilize no more than
4957 : half of the lanes. Test anyway to guarantee termination. */
4958 108 : if (lane == l_orig)
4959 0 : return false;
4960 : }
4961 :
4962 : /* Allocate lane. */
4963 74 : lane_assignment[lane] = 2 + l_orig;
4964 74 : new_sel = lane + offs * nelts;
4965 : }
4966 :
4967 148 : seq2_stmt_sel_perm.quick_push (new_sel);
4968 : }
4969 :
4970 : /* Check if the resulting permutation is cheap. */
4971 37 : seq2_stmt_indices->new_vector (seq2_stmt_sel_perm, 2, nelts);
4972 37 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
4973 37 : machine_mode vmode = TYPE_MODE (vectype);
4974 37 : if (!can_vec_perm_const_p (vmode, vmode, *seq2_stmt_indices, false))
4975 : return false;
4976 :
4977 : /* Calculate selectors for seq1->v_1_stmt and seq1->v_2_stmt. */
4978 37 : vec_perm_builder seq1_v_1_stmt_sel_perm (nelts, nelts, 1);
4979 37 : vec_perm_builder seq1_v_2_stmt_sel_perm (nelts, nelts, 1);
4980 185 : for (i = 0; i < nelts; i++)
4981 : {
4982 148 : bool use_seq1 = lane_assignment[i] < 2;
4983 148 : unsigned int l1, l2;
4984 :
4985 148 : if (use_seq1)
4986 : {
4987 : /* Just reuse the selector indices. */
4988 74 : tree s1 = gimple_assign_rhs3 (seq1->v_1_stmt);
4989 74 : tree s2 = gimple_assign_rhs3 (seq1->v_2_stmt);
4990 74 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, i));
4991 74 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, i));
4992 : }
4993 : else
4994 : {
4995 : /* We moved the lanes for seq2, so we need to adjust for that. */
4996 74 : tree s1 = gimple_assign_rhs3 (seq2->v_1_stmt);
4997 74 : tree s2 = gimple_assign_rhs3 (seq2->v_2_stmt);
4998 74 : l1 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s1, lane_assignment[i] - 2));
4999 74 : l2 = TREE_INT_CST_LOW (VECTOR_CST_ELT (s2, lane_assignment[i] - 2));
5000 : }
5001 :
5002 148 : l1 %= nelts;
5003 148 : l2 %= nelts;
5004 222 : seq1_v_1_stmt_sel_perm.quick_push (l1 + (use_seq1 ? 0 : nelts));
5005 148 : seq1_v_2_stmt_sel_perm.quick_push (l2 + (use_seq1 ? 0 : nelts));
5006 : }
5007 :
5008 37 : seq1_v_1_stmt_indices->new_vector (seq1_v_1_stmt_sel_perm, 2, nelts);
5009 37 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
5010 37 : vmode = TYPE_MODE (vectype);
5011 37 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_1_stmt_indices, false))
5012 : return false;
5013 :
5014 37 : seq1_v_2_stmt_indices->new_vector (seq1_v_2_stmt_sel_perm, 2, nelts);
5015 37 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
5016 37 : vmode = TYPE_MODE (vectype);
5017 37 : if (!can_vec_perm_const_p (vmode, vmode, *seq1_v_2_stmt_indices, false))
5018 : return false;
5019 :
5020 : return true;
5021 74 : }
5022 :
5023 : /* Blend the two given simplifiable vec permute sequences using the
5024 : given permutations. */
5025 :
5026 : static void
5027 37 : blend_vec_perm_simplify_seqs (vec_perm_simplify_seq seq1,
5028 : vec_perm_simplify_seq seq2,
5029 : const vec_perm_indices &seq2_stmt_indices,
5030 : const vec_perm_indices &seq1_v_1_stmt_indices,
5031 : const vec_perm_indices &seq1_v_2_stmt_indices)
5032 : {
5033 : /* We don't need to adjust seq1->stmt because its lanes consumption
5034 : was already narrowed before entering this function. */
5035 :
5036 : /* Adjust seq2->stmt: copy RHS1/RHS2 from seq1->stmt and set new sel. */
5037 37 : if (dump_file && (dump_flags & TDF_DETAILS))
5038 : {
5039 11 : fprintf (dump_file, "Updating VEC_PERM statement:\n");
5040 11 : fprintf (dump_file, "Old stmt: ");
5041 11 : print_gimple_stmt (dump_file, seq2->stmt, 0);
5042 : }
5043 :
5044 37 : gimple_assign_set_rhs1 (seq2->stmt, gimple_assign_rhs1 (seq1->stmt));
5045 74 : gimple_assign_set_rhs2 (seq2->stmt, gimple_assign_rhs2 (seq1->stmt));
5046 37 : tree vectype = TREE_TYPE (gimple_assign_lhs (seq2->stmt));
5047 37 : tree sel = vect_gen_perm_mask_checked (vectype, seq2_stmt_indices);
5048 37 : gimple_assign_set_rhs3 (seq2->stmt, sel);
5049 37 : update_stmt (seq2->stmt);
5050 :
5051 37 : if (dump_file && (dump_flags & TDF_DETAILS))
5052 : {
5053 11 : fprintf (dump_file, "New stmt: ");
5054 11 : print_gimple_stmt (dump_file, seq2->stmt, 0);
5055 : }
5056 :
5057 : /* Adjust seq1->v_1_stmt: copy RHS2 from seq2->v_1_stmt and set new sel. */
5058 37 : if (dump_file && (dump_flags & TDF_DETAILS))
5059 : {
5060 11 : fprintf (dump_file, "Updating VEC_PERM statement:\n");
5061 11 : fprintf (dump_file, "Old stmt: ");
5062 11 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
5063 : }
5064 :
5065 37 : gimple_assign_set_rhs2 (seq1->v_1_stmt, gimple_assign_rhs1 (seq2->v_1_stmt));
5066 37 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_1_stmt));
5067 37 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_1_stmt_indices);
5068 37 : gimple_assign_set_rhs3 (seq1->v_1_stmt, sel);
5069 37 : update_stmt (seq1->v_1_stmt);
5070 :
5071 37 : if (dump_file && (dump_flags & TDF_DETAILS))
5072 : {
5073 11 : fprintf (dump_file, "New stmt: ");
5074 11 : print_gimple_stmt (dump_file, seq1->v_1_stmt, 0);
5075 : }
5076 :
5077 : /* Adjust seq1->v_2_stmt: copy RHS2 from seq2->v_2_stmt and set new sel. */
5078 37 : if (dump_file && (dump_flags & TDF_DETAILS))
5079 : {
5080 11 : fprintf (dump_file, "Updating VEC_PERM statement:\n");
5081 11 : fprintf (dump_file, "Old stmt: ");
5082 11 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
5083 : }
5084 :
5085 37 : gimple_assign_set_rhs2 (seq1->v_2_stmt, gimple_assign_rhs1 (seq2->v_2_stmt));
5086 37 : vectype = TREE_TYPE (gimple_assign_lhs (seq1->v_2_stmt));
5087 37 : sel = vect_gen_perm_mask_checked (vectype, seq1_v_2_stmt_indices);
5088 37 : gimple_assign_set_rhs3 (seq1->v_2_stmt, sel);
5089 37 : update_stmt (seq1->v_2_stmt);
5090 :
5091 37 : if (dump_file && (dump_flags & TDF_DETAILS))
5092 : {
5093 11 : fprintf (dump_file, "New stmt: ");
5094 11 : print_gimple_stmt (dump_file, seq1->v_2_stmt, 0);
5095 : }
5096 :
5097 : /* At this point, we have four unmodified seq2 stmts, which will be
5098 : eliminated by DCE. */
5099 :
5100 37 : if (dump_file)
5101 11 : fprintf (dump_file, "Vec perm simplify sequences have been blended.\n\n");
5102 37 : }
5103 :
5104 : /* Try to blend narrowed vec_perm_simplify_seqs pairwise.
5105 : The provided list will be empty after this call. */
5106 :
5107 : static void
5108 309166714 : process_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l)
5109 : {
5110 309166714 : unsigned int i, j;
5111 309166714 : vec_perm_simplify_seq seq1, seq2;
5112 :
5113 309166714 : if (l->is_empty ())
5114 309166669 : return;
5115 :
5116 45 : if (dump_file && (dump_flags & TDF_DETAILS))
5117 13 : fprintf (dump_file, "\nProcessing %u vec perm simplify sequences.\n",
5118 : l->length ());
5119 :
5120 109 : FOR_EACH_VEC_ELT (*l, i, seq1)
5121 : {
5122 64 : if (i + 1 < l->length ())
5123 : {
5124 51 : FOR_EACH_VEC_ELT_FROM (*l, j, seq2, i + 1)
5125 : {
5126 47 : bool swap = false;
5127 47 : if (can_blend_vec_perm_simplify_seqs_p (seq1, seq2, &swap))
5128 : {
5129 37 : vec_perm_indices seq2_stmt_indices;
5130 37 : vec_perm_indices seq1_v_1_stmt_indices;
5131 37 : vec_perm_indices seq1_v_2_stmt_indices;
5132 111 : if (calc_perm_vec_perm_simplify_seqs (swap ? seq2 : seq1,
5133 : swap ? seq1 : seq2,
5134 : &seq2_stmt_indices,
5135 : &seq1_v_1_stmt_indices,
5136 : &seq1_v_2_stmt_indices))
5137 : {
5138 : /* Narrow lane usage. */
5139 37 : narrow_vec_perm_simplify_seq (seq1);
5140 37 : narrow_vec_perm_simplify_seq (seq2);
5141 :
5142 : /* Blend sequences. */
5143 37 : blend_vec_perm_simplify_seqs (swap ? seq2 : seq1,
5144 : swap ? seq1 : seq2,
5145 : seq2_stmt_indices,
5146 : seq1_v_1_stmt_indices,
5147 : seq1_v_2_stmt_indices);
5148 :
5149 : /* We can use unordered_remove as we break the loop. */
5150 37 : l->unordered_remove (j);
5151 37 : XDELETE (seq2);
5152 37 : break;
5153 : }
5154 37 : }
5155 : }
5156 : }
5157 :
5158 : /* We don't need to call l->remove for seq1. */
5159 64 : XDELETE (seq1);
5160 : }
5161 :
5162 45 : l->truncate (0);
5163 : }
5164 :
5165 : static void
5166 101 : append_vec_perm_simplify_seq_list (vec<vec_perm_simplify_seq> *l,
5167 : const vec_perm_simplify_seq &seq)
5168 : {
5169 : /* If no space on list left, then process the list. */
5170 101 : if (!l->space (1))
5171 0 : process_vec_perm_simplify_seq_list (l);
5172 :
5173 101 : l->quick_push (seq);
5174 101 : }
5175 :
5176 : /* Main entry point for the forward propagation and statement combine
5177 : optimizer. */
5178 :
5179 : namespace {
5180 :
5181 : const pass_data pass_data_forwprop =
5182 : {
5183 : GIMPLE_PASS, /* type */
5184 : "forwprop", /* name */
5185 : OPTGROUP_NONE, /* optinfo_flags */
5186 : TV_TREE_FORWPROP, /* tv_id */
5187 : ( PROP_cfg | PROP_ssa ), /* properties_required */
5188 : 0, /* properties_provided */
5189 : 0, /* properties_destroyed */
5190 : 0, /* todo_flags_start */
5191 : 0, /* todo_flags_finish */
5192 : };
5193 :
5194 : class pass_forwprop : public gimple_opt_pass
5195 : {
5196 : public:
5197 1494140 : pass_forwprop (gcc::context *ctxt)
5198 2988280 : : gimple_opt_pass (pass_data_forwprop, ctxt), last_p (false)
5199 : {}
5200 :
5201 : /* opt_pass methods: */
5202 1195312 : opt_pass * clone () final override { return new pass_forwprop (m_ctxt); }
5203 1792968 : void set_pass_param (unsigned int n, bool param) final override
5204 : {
5205 1792968 : switch (n)
5206 : {
5207 1195312 : case 0:
5208 1195312 : m_full_walk = param;
5209 1195312 : break;
5210 597656 : case 1:
5211 597656 : last_p = param;
5212 597656 : break;
5213 0 : default:
5214 0 : gcc_unreachable();
5215 : }
5216 1792968 : }
5217 5525044 : bool gate (function *) final override { return flag_tree_forwprop; }
5218 : unsigned int execute (function *) final override;
5219 :
5220 : private:
5221 : /* Determines whether the pass instance should set PROP_last_full_fold. */
5222 : bool last_p;
5223 :
5224 : /* True if the aggregate props are doing a full walk or not. */
5225 : bool m_full_walk = false;
5226 : }; // class pass_forwprop
5227 :
5228 : /* Attempt to make the BB block of __builtin_unreachable unreachable by changing
5229 : the incoming jumps. Return true if at least one jump was changed. */
5230 :
5231 : static bool
5232 2286 : optimize_unreachable (basic_block bb)
5233 : {
5234 2286 : gimple_stmt_iterator gsi;
5235 2286 : gimple *stmt;
5236 2286 : edge_iterator ei;
5237 2286 : edge e;
5238 2286 : bool ret;
5239 :
5240 2286 : ret = false;
5241 5253 : FOR_EACH_EDGE (e, ei, bb->preds)
5242 : {
5243 2967 : gsi = gsi_last_bb (e->src);
5244 2967 : if (gsi_end_p (gsi))
5245 326 : continue;
5246 :
5247 2641 : stmt = gsi_stmt (gsi);
5248 2641 : if (gcond *cond_stmt = dyn_cast <gcond *> (stmt))
5249 : {
5250 : /* If the condition is already true/false
5251 : ignore it. This can happen during copy prop of forwprop. */
5252 687 : if (gimple_cond_true_p (cond_stmt)
5253 679 : || gimple_cond_false_p (cond_stmt))
5254 8 : continue;
5255 671 : else if (e->flags & EDGE_TRUE_VALUE)
5256 583 : gimple_cond_make_false (cond_stmt);
5257 88 : else if (e->flags & EDGE_FALSE_VALUE)
5258 88 : gimple_cond_make_true (cond_stmt);
5259 : else
5260 0 : gcc_unreachable ();
5261 671 : update_stmt (cond_stmt);
5262 : }
5263 : else
5264 : {
5265 : /* Todo: handle other cases. e.g. switch. */
5266 1962 : continue;
5267 : }
5268 :
5269 671 : ret = true;
5270 : }
5271 :
5272 2286 : return ret;
5273 : }
5274 :
5275 : unsigned int
5276 5522436 : pass_forwprop::execute (function *fun)
5277 : {
5278 5522436 : unsigned int todoflags = 0;
5279 : /* Handle a full walk only when expensive optimizations are on. */
5280 5522436 : bool full_walk = m_full_walk && flag_expensive_optimizations;
5281 :
5282 5522436 : cfg_changed = false;
5283 5522436 : if (last_p)
5284 1041712 : fun->curr_properties |= PROP_last_full_fold;
5285 :
5286 5522436 : calculate_dominance_info (CDI_DOMINATORS);
5287 :
5288 : /* Combine stmts with the stmts defining their operands. Do that
5289 : in an order that guarantees visiting SSA defs before SSA uses. */
5290 11044872 : lattice.create (num_ssa_names);
5291 11044872 : lattice.quick_grow_cleared (num_ssa_names);
5292 5522436 : int *postorder = XNEWVEC (int, n_basic_blocks_for_fn (fun));
5293 5522436 : int postorder_num = pre_and_rev_post_order_compute_fn (fun, NULL,
5294 : postorder, false);
5295 5522436 : int *bb_to_rpo = XNEWVEC (int, last_basic_block_for_fn (fun));
5296 49642952 : for (int i = 0; i < postorder_num; ++i)
5297 : {
5298 44120516 : bb_to_rpo[postorder[i]] = i;
5299 44120516 : edge_iterator ei;
5300 44120516 : edge e;
5301 106183726 : FOR_EACH_EDGE (e, ei, BASIC_BLOCK_FOR_FN (fun, postorder[i])->succs)
5302 62063210 : e->flags &= ~EDGE_EXECUTABLE;
5303 : }
5304 5522436 : single_succ_edge (BASIC_BLOCK_FOR_FN (fun, ENTRY_BLOCK))->flags
5305 5522436 : |= EDGE_EXECUTABLE;
5306 5522436 : auto_vec<gimple *, 4> to_fixup;
5307 5522436 : auto_vec<gimple *, 32> to_remove;
5308 5522436 : auto_vec<unsigned, 32> to_remove_defs;
5309 5522436 : auto_vec<std::pair<int, int>, 10> edges_to_remove;
5310 5522436 : auto_bitmap simple_dce_worklist;
5311 5522436 : auto_bitmap need_ab_cleanup;
5312 5522436 : to_purge = BITMAP_ALLOC (NULL);
5313 5522436 : auto_vec<vec_perm_simplify_seq, 8> vec_perm_simplify_seq_list;
5314 49642952 : for (int i = 0; i < postorder_num; ++i)
5315 : {
5316 44120516 : gimple_stmt_iterator gsi;
5317 44120516 : basic_block bb = BASIC_BLOCK_FOR_FN (fun, postorder[i]);
5318 44120516 : edge_iterator ei;
5319 44120516 : edge e;
5320 :
5321 : /* Skip processing not executable blocks. We could improve
5322 : single_use tracking by at least unlinking uses from unreachable
5323 : blocks but since blocks with uses are not processed in a
5324 : meaningful order this is probably not worth it. */
5325 44120516 : bool any = false;
5326 45251195 : FOR_EACH_EDGE (e, ei, bb->preds)
5327 : {
5328 45238347 : if ((e->flags & EDGE_EXECUTABLE)
5329 : /* We can handle backedges in natural loops correctly but
5330 : for irreducible regions we have to take all backedges
5331 : conservatively when we did not visit the source yet. */
5332 45238347 : || (bb_to_rpo[e->src->index] > i
5333 661075 : && !dominated_by_p (CDI_DOMINATORS, e->src, e->dest)))
5334 : {
5335 : any = true;
5336 : break;
5337 : }
5338 : }
5339 44120516 : if (!any)
5340 13484 : continue;
5341 :
5342 : /* Remove conditions that go directly to unreachable when this is the last forwprop. */
5343 44107668 : if (last_p
5344 9637702 : && !(flag_sanitize & SANITIZE_UNREACHABLE))
5345 : {
5346 9632717 : gimple_stmt_iterator gsi;
5347 9632717 : gsi = gsi_start_nondebug_after_labels_bb (bb);
5348 9633353 : if (!gsi_end_p (gsi)
5349 8816775 : && gimple_call_builtin_p (*gsi, BUILT_IN_UNREACHABLE)
5350 9635003 : && optimize_unreachable (bb))
5351 : {
5352 636 : cfg_changed = true;
5353 636 : continue;
5354 : }
5355 : }
5356 :
5357 : /* Record degenerate PHIs in the lattice. */
5358 59687628 : for (gphi_iterator si = gsi_start_phis (bb); !gsi_end_p (si);
5359 15580596 : gsi_next (&si))
5360 : {
5361 15580596 : gphi *phi = si.phi ();
5362 15580596 : tree res = gimple_phi_result (phi);
5363 31161192 : if (virtual_operand_p (res))
5364 7133059 : continue;
5365 :
5366 8447537 : tree first = NULL_TREE;
5367 8447537 : bool all_same = true;
5368 8447537 : edge_iterator ei;
5369 8447537 : edge e;
5370 17367345 : FOR_EACH_EDGE (e, ei, bb->preds)
5371 : {
5372 : /* Ignore not executable forward edges. */
5373 17163090 : if (!(e->flags & EDGE_EXECUTABLE))
5374 : {
5375 3999206 : if (bb_to_rpo[e->src->index] < i)
5376 5535 : continue;
5377 : /* Avoid equivalences from backedges - while we might
5378 : be able to make irreducible regions reducible and
5379 : thus turning a back into a forward edge we do not
5380 : want to deal with the intermediate SSA issues that
5381 : exposes. */
5382 : all_same = false;
5383 : }
5384 17157555 : tree use = PHI_ARG_DEF_FROM_EDGE (phi, e);
5385 17157555 : if (use == res)
5386 : /* The PHI result can also appear on a backedge, if so
5387 : we can ignore this case for the purpose of determining
5388 : the singular value. */
5389 : ;
5390 17144988 : else if (! first)
5391 : first = use;
5392 8697451 : else if (! operand_equal_p (first, use, 0))
5393 : {
5394 : all_same = false;
5395 : break;
5396 : }
5397 : }
5398 8447537 : if (all_same)
5399 : {
5400 199663 : if (may_propagate_copy (res, first))
5401 199108 : to_remove_defs.safe_push (SSA_NAME_VERSION (res));
5402 199663 : fwprop_set_lattice_val (res, first);
5403 : }
5404 : }
5405 :
5406 : /* Apply forward propagation to all stmts in the basic-block.
5407 : Note we update GSI within the loop as necessary. */
5408 44107032 : unsigned int uid = 1;
5409 418907634 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); )
5410 : {
5411 330693570 : gimple *stmt = gsi_stmt (gsi);
5412 330693570 : tree lhs, rhs;
5413 330693570 : enum tree_code code;
5414 :
5415 330693570 : gimple_set_uid (stmt, uid++);
5416 :
5417 330693570 : if (!is_gimple_assign (stmt))
5418 : {
5419 227140694 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
5420 227140694 : gsi_next (&gsi);
5421 227140694 : continue;
5422 : }
5423 :
5424 103552876 : lhs = gimple_assign_lhs (stmt);
5425 103552876 : rhs = gimple_assign_rhs1 (stmt);
5426 103552876 : code = gimple_assign_rhs_code (stmt);
5427 :
5428 141471864 : if (TREE_CODE (lhs) != SSA_NAME
5429 103552876 : || has_zero_uses (lhs))
5430 : {
5431 37918988 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
5432 37918988 : gsi_next (&gsi);
5433 37918988 : continue;
5434 : }
5435 :
5436 : /* If this statement sets an SSA_NAME to an address,
5437 : try to propagate the address into the uses of the SSA_NAME. */
5438 65633888 : if ((code == ADDR_EXPR
5439 : /* Handle pointer conversions on invariant addresses
5440 : as well, as this is valid gimple. */
5441 63429524 : || (CONVERT_EXPR_CODE_P (code)
5442 8756835 : && TREE_CODE (rhs) == ADDR_EXPR
5443 351653 : && POINTER_TYPE_P (TREE_TYPE (lhs))))
5444 65634112 : && TREE_CODE (TREE_OPERAND (rhs, 0)) != TARGET_MEM_REF)
5445 : {
5446 2203927 : tree base = get_base_address (TREE_OPERAND (rhs, 0));
5447 2203927 : if ((!base
5448 2203927 : || !DECL_P (base)
5449 128811 : || decl_address_invariant_p (base))
5450 2203927 : && !stmt_references_abnormal_ssa_name (stmt)
5451 4407838 : && forward_propagate_addr_expr (lhs, rhs, true))
5452 : {
5453 444953 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
5454 444953 : release_defs (stmt);
5455 444953 : gsi_remove (&gsi, true);
5456 : }
5457 : else
5458 1758974 : gsi_next (&gsi);
5459 : }
5460 63429961 : else if (code == POINTER_PLUS_EXPR)
5461 : {
5462 3549436 : tree off = gimple_assign_rhs2 (stmt);
5463 3549436 : if (TREE_CODE (off) == INTEGER_CST
5464 1090903 : && can_propagate_from (stmt)
5465 1090550 : && !simple_iv_increment_p (stmt)
5466 : /* ??? Better adjust the interface to that function
5467 : instead of building new trees here. */
5468 4360470 : && forward_propagate_addr_expr
5469 2433102 : (lhs,
5470 : build1_loc (gimple_location (stmt),
5471 811034 : ADDR_EXPR, TREE_TYPE (rhs),
5472 811034 : fold_build2 (MEM_REF,
5473 : TREE_TYPE (TREE_TYPE (rhs)),
5474 : rhs,
5475 : fold_convert (ptr_type_node,
5476 : off))), true))
5477 : {
5478 307789 : fwprop_invalidate_lattice (gimple_get_lhs (stmt));
5479 307789 : release_defs (stmt);
5480 307789 : gsi_remove (&gsi, true);
5481 : }
5482 3241647 : else if (is_gimple_min_invariant (rhs))
5483 : {
5484 : /* Make sure to fold &a[0] + off_1 here. */
5485 399453 : fold_stmt_inplace (&gsi);
5486 399453 : update_stmt (stmt);
5487 399453 : if (gimple_assign_rhs_code (stmt) == POINTER_PLUS_EXPR)
5488 399435 : gsi_next (&gsi);
5489 : }
5490 : else
5491 2842194 : gsi_next (&gsi);
5492 : }
5493 59880525 : else if (TREE_CODE (TREE_TYPE (lhs)) == COMPLEX_TYPE
5494 212422 : && gimple_assign_load_p (stmt)
5495 134601 : && !gimple_has_volatile_ops (stmt)
5496 40526 : && TREE_CODE (rhs) != TARGET_MEM_REF
5497 40497 : && TREE_CODE (rhs) != BIT_FIELD_REF
5498 59921018 : && !stmt_can_throw_internal (fun, stmt))
5499 : {
5500 : /* Rewrite loads used only in real/imagpart extractions to
5501 : component-wise loads. */
5502 40368 : use_operand_p use_p;
5503 40368 : imm_use_iterator iter;
5504 40368 : tree vuse = gimple_vuse (stmt);
5505 40368 : bool rewrite = true;
5506 85495 : FOR_EACH_IMM_USE_FAST (use_p, iter, lhs)
5507 : {
5508 43041 : gimple *use_stmt = USE_STMT (use_p);
5509 43041 : if (is_gimple_debug (use_stmt))
5510 691 : continue;
5511 42350 : if (!is_gimple_assign (use_stmt)
5512 27680 : || (gimple_assign_rhs_code (use_stmt) != REALPART_EXPR
5513 25631 : && gimple_assign_rhs_code (use_stmt) != IMAGPART_EXPR)
5514 46418 : || TREE_OPERAND (gimple_assign_rhs1 (use_stmt), 0) != lhs)
5515 : {
5516 : rewrite = false;
5517 : break;
5518 : }
5519 40368 : }
5520 40368 : if (rewrite)
5521 : {
5522 2086 : gimple *use_stmt;
5523 8665 : FOR_EACH_IMM_USE_STMT (use_stmt, iter, lhs)
5524 : {
5525 4493 : if (is_gimple_debug (use_stmt))
5526 : {
5527 454 : if (gimple_debug_bind_p (use_stmt))
5528 : {
5529 454 : gimple_debug_bind_reset_value (use_stmt);
5530 454 : update_stmt (use_stmt);
5531 : }
5532 454 : continue;
5533 : }
5534 :
5535 8078 : tree new_rhs = build1 (gimple_assign_rhs_code (use_stmt),
5536 4039 : TREE_TYPE (TREE_TYPE (rhs)),
5537 : unshare_expr (rhs));
5538 4039 : gimple *new_stmt
5539 4039 : = gimple_build_assign (gimple_assign_lhs (use_stmt),
5540 : new_rhs);
5541 :
5542 4039 : location_t loc = gimple_location (use_stmt);
5543 4039 : gimple_set_location (new_stmt, loc);
5544 4039 : gimple_set_vuse (new_stmt, vuse);
5545 4039 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5546 4039 : unlink_stmt_vdef (use_stmt);
5547 4039 : gsi_remove (&gsi2, true);
5548 :
5549 4039 : gsi_insert_before (&gsi, new_stmt, GSI_SAME_STMT);
5550 2086 : }
5551 :
5552 2086 : release_defs (stmt);
5553 2086 : gsi_remove (&gsi, true);
5554 : }
5555 : else
5556 38282 : gsi_next (&gsi);
5557 : }
5558 59840157 : else if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE
5559 1699762 : && (TYPE_MODE (TREE_TYPE (lhs)) == BLKmode
5560 : /* After vector lowering rewrite all loads, but
5561 : initially do not since this conflicts with
5562 : vector CONSTRUCTOR to shuffle optimization. */
5563 1677403 : || (fun->curr_properties & PROP_gimple_lvec))
5564 907996 : && gimple_assign_load_p (stmt)
5565 305237 : && !gimple_has_volatile_ops (stmt)
5566 291368 : && !stmt_can_throw_internal (fun, stmt)
5567 60131525 : && (!VAR_P (rhs) || !DECL_HARD_REGISTER (rhs)))
5568 290866 : optimize_vector_load (&gsi);
5569 :
5570 59549291 : else if (code == COMPLEX_EXPR)
5571 : {
5572 : /* Rewrite stores of a single-use complex build expression
5573 : to component-wise stores. */
5574 36972 : use_operand_p use_p;
5575 36972 : gimple *use_stmt, *def1, *def2;
5576 36972 : tree rhs2;
5577 36972 : if (single_imm_use (lhs, &use_p, &use_stmt)
5578 34807 : && gimple_store_p (use_stmt)
5579 41040 : && !gimple_has_volatile_ops (use_stmt)
5580 2604 : && is_gimple_assign (use_stmt)
5581 2600 : && (TREE_CODE (TREE_TYPE (gimple_assign_lhs (use_stmt)))
5582 : == COMPLEX_TYPE)
5583 39567 : && (TREE_CODE (gimple_assign_lhs (use_stmt))
5584 : != TARGET_MEM_REF))
5585 : {
5586 2591 : tree use_lhs = gimple_assign_lhs (use_stmt);
5587 2591 : if (auto_var_p (use_lhs))
5588 601 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
5589 5182 : tree new_lhs = build1 (REALPART_EXPR,
5590 2591 : TREE_TYPE (TREE_TYPE (use_lhs)),
5591 : unshare_expr (use_lhs));
5592 2591 : gimple *new_stmt = gimple_build_assign (new_lhs, rhs);
5593 2591 : location_t loc = gimple_location (use_stmt);
5594 2591 : gimple_set_location (new_stmt, loc);
5595 5182 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
5596 2591 : gimple_set_vdef (new_stmt, make_ssa_name (gimple_vop (fun)));
5597 5182 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
5598 5182 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
5599 2591 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5600 2591 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
5601 :
5602 5182 : new_lhs = build1 (IMAGPART_EXPR,
5603 2591 : TREE_TYPE (TREE_TYPE (use_lhs)),
5604 : unshare_expr (use_lhs));
5605 2591 : gimple_assign_set_lhs (use_stmt, new_lhs);
5606 2591 : gimple_assign_set_rhs1 (use_stmt, gimple_assign_rhs2 (stmt));
5607 2591 : update_stmt (use_stmt);
5608 :
5609 2591 : release_defs (stmt);
5610 2591 : gsi_remove (&gsi, true);
5611 : }
5612 : /* Rewrite a component-wise load of a complex to a complex
5613 : load if the components are not used separately. */
5614 34381 : else if (TREE_CODE (rhs) == SSA_NAME
5615 33940 : && has_single_use (rhs)
5616 30450 : && ((rhs2 = gimple_assign_rhs2 (stmt)), true)
5617 30450 : && TREE_CODE (rhs2) == SSA_NAME
5618 28703 : && has_single_use (rhs2)
5619 28283 : && (def1 = SSA_NAME_DEF_STMT (rhs),
5620 28283 : gimple_assign_load_p (def1))
5621 1085 : && (def2 = SSA_NAME_DEF_STMT (rhs2),
5622 1085 : gimple_assign_load_p (def2))
5623 1582 : && (gimple_vuse (def1) == gimple_vuse (def2))
5624 788 : && !gimple_has_volatile_ops (def1)
5625 788 : && !gimple_has_volatile_ops (def2)
5626 788 : && !stmt_can_throw_internal (fun, def1)
5627 788 : && !stmt_can_throw_internal (fun, def2)
5628 788 : && gimple_assign_rhs_code (def1) == REALPART_EXPR
5629 542 : && gimple_assign_rhs_code (def2) == IMAGPART_EXPR
5630 34923 : && operand_equal_p (TREE_OPERAND (gimple_assign_rhs1
5631 : (def1), 0),
5632 542 : TREE_OPERAND (gimple_assign_rhs1
5633 : (def2), 0)))
5634 : {
5635 542 : tree cl = TREE_OPERAND (gimple_assign_rhs1 (def1), 0);
5636 542 : gimple_assign_set_rhs_from_tree (&gsi, unshare_expr (cl));
5637 542 : gcc_assert (gsi_stmt (gsi) == stmt);
5638 1084 : gimple_set_vuse (stmt, gimple_vuse (def1));
5639 542 : gimple_set_modified (stmt, true);
5640 542 : gimple_stmt_iterator gsi2 = gsi_for_stmt (def1);
5641 542 : gsi_remove (&gsi, false);
5642 542 : gsi_insert_after (&gsi2, stmt, GSI_SAME_STMT);
5643 : }
5644 : else
5645 33839 : gsi_next (&gsi);
5646 : }
5647 59512319 : else if (code == CONSTRUCTOR
5648 155053 : && VECTOR_TYPE_P (TREE_TYPE (rhs))
5649 155053 : && TYPE_MODE (TREE_TYPE (rhs)) == BLKmode
5650 3457 : && CONSTRUCTOR_NELTS (rhs) > 0
5651 59515776 : && (!VECTOR_TYPE_P (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
5652 1050 : || (TYPE_MODE (TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value))
5653 : != BLKmode)))
5654 : {
5655 : /* Rewrite stores of a single-use vector constructors
5656 : to component-wise stores if the mode isn't supported. */
5657 3362 : use_operand_p use_p;
5658 3362 : gimple *use_stmt;
5659 3362 : if (single_imm_use (lhs, &use_p, &use_stmt)
5660 2922 : && gimple_store_p (use_stmt)
5661 2998 : && !gimple_has_volatile_ops (use_stmt)
5662 1493 : && !stmt_can_throw_internal (fun, use_stmt)
5663 4848 : && is_gimple_assign (use_stmt))
5664 : {
5665 1486 : tree elt_t = TREE_TYPE (CONSTRUCTOR_ELT (rhs, 0)->value);
5666 1486 : unsigned HOST_WIDE_INT elt_w
5667 1486 : = tree_to_uhwi (TYPE_SIZE (elt_t));
5668 1486 : unsigned HOST_WIDE_INT n
5669 1486 : = tree_to_uhwi (TYPE_SIZE (TREE_TYPE (rhs)));
5670 1486 : tree use_lhs = gimple_assign_lhs (use_stmt);
5671 1486 : if (auto_var_p (use_lhs))
5672 554 : DECL_NOT_GIMPLE_REG_P (use_lhs) = 1;
5673 932 : else if (TREE_CODE (use_lhs) == TARGET_MEM_REF)
5674 : {
5675 1 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5676 1 : use_lhs = prepare_target_mem_ref_lvalue (use_lhs, &gsi2);
5677 : }
5678 33260 : for (unsigned HOST_WIDE_INT bi = 0; bi < n; bi += elt_w)
5679 : {
5680 31774 : unsigned HOST_WIDE_INT ci = bi / elt_w;
5681 31774 : tree new_rhs;
5682 31774 : if (ci < CONSTRUCTOR_NELTS (rhs))
5683 31156 : new_rhs = CONSTRUCTOR_ELT (rhs, ci)->value;
5684 : else
5685 618 : new_rhs = build_zero_cst (elt_t);
5686 31774 : tree new_lhs = build3 (BIT_FIELD_REF,
5687 : elt_t,
5688 : unshare_expr (use_lhs),
5689 31774 : bitsize_int (elt_w),
5690 31774 : bitsize_int (bi));
5691 31774 : gimple *new_stmt = gimple_build_assign (new_lhs, new_rhs);
5692 31774 : location_t loc = gimple_location (use_stmt);
5693 31774 : gimple_set_location (new_stmt, loc);
5694 63548 : gimple_set_vuse (new_stmt, gimple_vuse (use_stmt));
5695 31774 : gimple_set_vdef (new_stmt,
5696 : make_ssa_name (gimple_vop (fun)));
5697 63548 : SSA_NAME_DEF_STMT (gimple_vdef (new_stmt)) = new_stmt;
5698 63548 : gimple_set_vuse (use_stmt, gimple_vdef (new_stmt));
5699 31774 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5700 31774 : gsi_insert_before (&gsi2, new_stmt, GSI_SAME_STMT);
5701 : }
5702 1486 : gimple_stmt_iterator gsi2 = gsi_for_stmt (use_stmt);
5703 1486 : unlink_stmt_vdef (use_stmt);
5704 1486 : release_defs (use_stmt);
5705 1486 : gsi_remove (&gsi2, true);
5706 1486 : release_defs (stmt);
5707 1486 : gsi_remove (&gsi, true);
5708 : }
5709 : else
5710 1876 : gsi_next (&gsi);
5711 : }
5712 59508957 : else if (code == VEC_PERM_EXPR)
5713 : {
5714 : /* Find vectorized sequences, where we can reduce the lane
5715 : utilization. The narrowing will be donw later and only
5716 : if we find a pair of sequences that can be blended. */
5717 182938 : gassign *assign = dyn_cast <gassign *> (stmt);
5718 182938 : vec_perm_simplify_seq seq;
5719 182938 : if (recognise_vec_perm_simplify_seq (assign, &seq))
5720 101 : append_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list,
5721 : seq);
5722 :
5723 182938 : gsi_next (&gsi);
5724 : }
5725 : else
5726 59326019 : gsi_next (&gsi);
5727 : }
5728 :
5729 44107032 : process_vec_perm_simplify_seq_list (&vec_perm_simplify_seq_list);
5730 :
5731 : /* Combine stmts with the stmts defining their operands.
5732 : Note we update GSI within the loop as necessary. */
5733 418568358 : for (gsi = gsi_start_bb (bb); !gsi_end_p (gsi); gsi_next (&gsi))
5734 : {
5735 330354294 : gimple *stmt = gsi_stmt (gsi);
5736 :
5737 : /* Mark stmt as potentially needing revisiting. */
5738 330354294 : gimple_set_plf (stmt, GF_PLF_1, false);
5739 :
5740 330354294 : bool can_make_abnormal_goto = (is_gimple_call (stmt)
5741 330354294 : && stmt_can_make_abnormal_goto (stmt));
5742 :
5743 : /* Substitute from our lattice. We need to do so only once. */
5744 330354294 : bool substituted_p = false;
5745 330354294 : use_operand_p usep;
5746 330354294 : ssa_op_iter iter;
5747 489124268 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
5748 : {
5749 158769974 : tree use = USE_FROM_PTR (usep);
5750 158769974 : tree val = fwprop_ssa_val (use);
5751 158769974 : if (val && val != use)
5752 : {
5753 1775685 : if (!is_gimple_debug (stmt))
5754 1490143 : bitmap_set_bit (simple_dce_worklist, SSA_NAME_VERSION (use));
5755 1775685 : if (may_propagate_copy (use, val))
5756 : {
5757 1772457 : propagate_value (usep, val);
5758 1772457 : substituted_p = true;
5759 : }
5760 : }
5761 : }
5762 330354294 : if (substituted_p)
5763 1719307 : update_stmt (stmt);
5764 1719307 : if (substituted_p
5765 1719307 : && is_gimple_assign (stmt)
5766 1042323 : && gimple_assign_rhs_code (stmt) == ADDR_EXPR)
5767 19568 : recompute_tree_invariant_for_addr_expr (gimple_assign_rhs1 (stmt));
5768 330354294 : if (substituted_p
5769 330354294 : && can_make_abnormal_goto
5770 330354294 : && !stmt_can_make_abnormal_goto (stmt))
5771 3 : bitmap_set_bit (need_ab_cleanup, bb->index);
5772 :
5773 333120556 : bool changed;
5774 666241112 : do
5775 : {
5776 333120556 : gimple *orig_stmt = stmt = gsi_stmt (gsi);
5777 333120556 : bool was_call = is_gimple_call (stmt);
5778 333120556 : bool was_noreturn = (was_call
5779 333120556 : && gimple_call_noreturn_p (stmt));
5780 333120556 : changed = false;
5781 :
5782 333120556 : auto_vec<tree, 8> uses;
5783 494848150 : FOR_EACH_SSA_USE_OPERAND (usep, stmt, iter, SSA_OP_USE)
5784 161727594 : if (uses.space (1))
5785 161340131 : uses.quick_push (USE_FROM_PTR (usep));
5786 :
5787 333120556 : if (fold_stmt (&gsi, fwprop_ssa_val, simple_dce_worklist))
5788 : {
5789 2452638 : changed = true;
5790 : /* There is no updating of the address
5791 : taken after the last forwprop so update
5792 : the addresses when a folding happened to a call.
5793 : The va_* builtins can remove taking of the address so
5794 : can the sincos->cexpi transformation. See PR 39643 and PR 20983. */
5795 2452638 : if (was_call && last_p)
5796 2452638 : todoflags |= TODO_update_address_taken;
5797 2452638 : stmt = gsi_stmt (gsi);
5798 : /* Cleanup the CFG if we simplified a condition to
5799 : true or false. */
5800 2452638 : if (gcond *cond = dyn_cast <gcond *> (stmt))
5801 973184 : if (gimple_cond_true_p (cond)
5802 973184 : || gimple_cond_false_p (cond))
5803 13888 : cfg_changed = true;
5804 : /* Queue old uses for simple DCE if not debug statement. */
5805 2452638 : if (!is_gimple_debug (stmt))
5806 10382744 : for (tree use : uses)
5807 3045398 : if (TREE_CODE (use) == SSA_NAME
5808 3045398 : && !SSA_NAME_IS_DEFAULT_DEF (use))
5809 2850657 : bitmap_set_bit (simple_dce_worklist,
5810 2850657 : SSA_NAME_VERSION (use));
5811 2452638 : update_stmt (stmt);
5812 : }
5813 :
5814 333120556 : switch (gimple_code (stmt))
5815 : {
5816 104542393 : case GIMPLE_ASSIGN:
5817 104542393 : {
5818 104542393 : tree rhs1 = gimple_assign_rhs1 (stmt);
5819 104542393 : enum tree_code code = gimple_assign_rhs_code (stmt);
5820 104542393 : if (gimple_clobber_p (stmt))
5821 6885349 : do_simple_agr_dse (as_a<gassign*>(stmt), full_walk);
5822 97657044 : else if (gimple_store_p (stmt))
5823 : {
5824 30421245 : optimize_aggr_zeroprop (stmt, full_walk);
5825 30421245 : if (gimple_assign_load_p (stmt))
5826 3774809 : optimize_agr_copyprop (stmt);
5827 : }
5828 67235799 : else if (TREE_CODE_CLASS (code) == tcc_comparison)
5829 2551194 : changed |= forward_propagate_into_comparison (&gsi);
5830 64684605 : else if ((code == PLUS_EXPR
5831 64684605 : || code == BIT_IOR_EXPR
5832 54706641 : || code == BIT_XOR_EXPR)
5833 64818307 : && simplify_rotate (&gsi))
5834 : changed = true;
5835 64681931 : else if (code == VEC_PERM_EXPR)
5836 184932 : changed |= simplify_permutation (&gsi);
5837 64496999 : else if (code == CONSTRUCTOR
5838 64496999 : && TREE_CODE (TREE_TYPE (rhs1)) == VECTOR_TYPE)
5839 152851 : changed |= simplify_vector_constructor (&gsi);
5840 64344148 : else if (code == ARRAY_REF)
5841 1954317 : changed |= simplify_count_zeroes (&gsi);
5842 : break;
5843 : }
5844 :
5845 105981 : case GIMPLE_SWITCH:
5846 105981 : changed |= simplify_gimple_switch (as_a <gswitch *> (stmt),
5847 : edges_to_remove,
5848 : simple_dce_worklist);
5849 105981 : break;
5850 :
5851 19057296 : case GIMPLE_COND:
5852 19057296 : {
5853 19057296 : int did_something = forward_propagate_into_gimple_cond
5854 19057296 : (as_a <gcond *> (stmt));
5855 19057296 : if (did_something == 2)
5856 1648 : cfg_changed = true;
5857 19057296 : changed |= did_something != 0;
5858 19057296 : break;
5859 : }
5860 :
5861 23000510 : case GIMPLE_CALL:
5862 23000510 : {
5863 23000510 : tree callee = gimple_call_fndecl (stmt);
5864 23000510 : if (callee != NULL_TREE
5865 23000510 : && fndecl_built_in_p (callee, BUILT_IN_NORMAL))
5866 6132843 : changed |= simplify_builtin_call (&gsi, callee, full_walk);
5867 : break;
5868 : }
5869 :
5870 333117882 : default:;
5871 : }
5872 :
5873 333117882 : if (changed || substituted_p)
5874 : {
5875 3975943 : substituted_p = false;
5876 3975943 : stmt = gsi_stmt (gsi);
5877 3975943 : if (maybe_clean_or_replace_eh_stmt (orig_stmt, stmt))
5878 70 : bitmap_set_bit (to_purge, bb->index);
5879 3975943 : if (!was_noreturn
5880 3975943 : && is_gimple_call (stmt) && gimple_call_noreturn_p (stmt))
5881 12 : to_fixup.safe_push (stmt);
5882 : }
5883 3975943 : if (changed)
5884 : {
5885 : /* If the stmt changed then re-visit it and the statements
5886 : inserted before it. */
5887 8655242 : for (; !gsi_end_p (gsi); gsi_prev (&gsi))
5888 5492794 : if (gimple_plf (gsi_stmt (gsi), GF_PLF_1))
5889 : break;
5890 2766262 : if (gsi_end_p (gsi))
5891 435916 : gsi = gsi_start_bb (bb);
5892 : else
5893 2548304 : gsi_next (&gsi);
5894 : }
5895 333120556 : }
5896 : while (changed);
5897 :
5898 : /* Stmt no longer needs to be revisited. */
5899 330354294 : stmt = gsi_stmt (gsi);
5900 330354294 : gcc_checking_assert (!gimple_plf (stmt, GF_PLF_1));
5901 330354294 : gimple_set_plf (stmt, GF_PLF_1, true);
5902 :
5903 : /* Fill up the lattice. */
5904 330354294 : if (gimple_assign_single_p (stmt))
5905 : {
5906 69034199 : tree lhs = gimple_assign_lhs (stmt);
5907 69034199 : tree rhs = gimple_assign_rhs1 (stmt);
5908 69034199 : if (TREE_CODE (lhs) == SSA_NAME)
5909 : {
5910 31738822 : tree val = lhs;
5911 31738822 : if (TREE_CODE (rhs) == SSA_NAME)
5912 755669 : val = fwprop_ssa_val (rhs);
5913 30983153 : else if (is_gimple_min_invariant (rhs))
5914 417218 : val = rhs;
5915 : /* If we can propagate the lattice-value mark the
5916 : stmt for removal. */
5917 31738822 : if (val != lhs
5918 31738822 : && may_propagate_copy (lhs, val))
5919 1169457 : to_remove_defs.safe_push (SSA_NAME_VERSION (lhs));
5920 31738822 : fwprop_set_lattice_val (lhs, val);
5921 : }
5922 : }
5923 261320095 : else if (gimple_nop_p (stmt))
5924 88921 : to_remove.safe_push (stmt);
5925 : }
5926 :
5927 : /* Substitute in destination PHI arguments. */
5928 106159313 : FOR_EACH_EDGE (e, ei, bb->succs)
5929 62052281 : for (gphi_iterator gsi = gsi_start_phis (e->dest);
5930 103008144 : !gsi_end_p (gsi); gsi_next (&gsi))
5931 : {
5932 40955863 : gphi *phi = gsi.phi ();
5933 40955863 : use_operand_p use_p = PHI_ARG_DEF_PTR_FROM_EDGE (phi, e);
5934 40955863 : tree arg = USE_FROM_PTR (use_p);
5935 67617367 : if (TREE_CODE (arg) != SSA_NAME
5936 40955863 : || virtual_operand_p (arg))
5937 26661504 : continue;
5938 14294359 : tree val = fwprop_ssa_val (arg);
5939 14294359 : if (val != arg
5940 14294359 : && may_propagate_copy (arg, val, !(e->flags & EDGE_ABNORMAL)))
5941 234093 : propagate_value (use_p, val);
5942 : }
5943 :
5944 : /* Mark outgoing executable edges. */
5945 44107032 : if (edge e = find_taken_edge (bb, NULL))
5946 : {
5947 18732173 : e->flags |= EDGE_EXECUTABLE;
5948 44126784 : if (EDGE_COUNT (bb->succs) > 1)
5949 19752 : cfg_changed = true;
5950 : }
5951 : else
5952 : {
5953 68675214 : FOR_EACH_EDGE (e, ei, bb->succs)
5954 43300355 : e->flags |= EDGE_EXECUTABLE;
5955 : }
5956 : }
5957 5522436 : free (postorder);
5958 5522436 : free (bb_to_rpo);
5959 5522436 : lattice.release ();
5960 :
5961 : /* First remove chains of stmts where we check no uses remain. */
5962 5522436 : simple_dce_from_worklist (simple_dce_worklist, to_purge);
5963 :
5964 5855610 : auto remove = [](gimple *stmt)
5965 : {
5966 333174 : if (dump_file && (dump_flags & TDF_DETAILS))
5967 : {
5968 1 : fprintf (dump_file, "Removing dead stmt ");
5969 1 : print_gimple_stmt (dump_file, stmt, 0);
5970 1 : fprintf (dump_file, "\n");
5971 : }
5972 333174 : gimple_stmt_iterator gsi = gsi_for_stmt (stmt);
5973 333174 : if (gimple_code (stmt) == GIMPLE_PHI)
5974 86966 : remove_phi_node (&gsi, true);
5975 : else
5976 : {
5977 246208 : unlink_stmt_vdef (stmt);
5978 246208 : gsi_remove (&gsi, true);
5979 246208 : release_defs (stmt);
5980 : }
5981 333174 : };
5982 :
5983 : /* Then remove stmts we know we can remove even though we did not
5984 : substitute in dead code regions, so uses can remain. Do so in reverse
5985 : order to make debug stmt creation possible. */
5986 12413437 : while (!to_remove_defs.is_empty())
5987 : {
5988 1368565 : tree def = ssa_name (to_remove_defs.pop ());
5989 : /* For example remove_prop_source_from_use can remove stmts queued
5990 : for removal. Deal with this gracefully. */
5991 1368565 : if (!def)
5992 1124312 : continue;
5993 244253 : gimple *stmt = SSA_NAME_DEF_STMT (def);
5994 244253 : remove (stmt);
5995 : }
5996 :
5997 : /* Wipe other queued stmts that do not have SSA defs. */
5998 5611357 : while (!to_remove.is_empty())
5999 : {
6000 88921 : gimple *stmt = to_remove.pop ();
6001 88921 : remove (stmt);
6002 : }
6003 :
6004 : /* Fixup stmts that became noreturn calls. This may require splitting
6005 : blocks and thus isn't possible during the walk. Do this
6006 : in reverse order so we don't inadvertently remove a stmt we want to
6007 : fixup by visiting a dominating now noreturn call first. */
6008 5522448 : while (!to_fixup.is_empty ())
6009 : {
6010 12 : gimple *stmt = to_fixup.pop ();
6011 12 : if (dump_file && dump_flags & TDF_DETAILS)
6012 : {
6013 0 : fprintf (dump_file, "Fixing up noreturn call ");
6014 0 : print_gimple_stmt (dump_file, stmt, 0);
6015 0 : fprintf (dump_file, "\n");
6016 : }
6017 12 : cfg_changed |= fixup_noreturn_call (stmt);
6018 : }
6019 :
6020 5522436 : cfg_changed |= gimple_purge_all_dead_eh_edges (to_purge);
6021 5522436 : cfg_changed |= gimple_purge_all_dead_abnormal_call_edges (need_ab_cleanup);
6022 5522436 : BITMAP_FREE (to_purge);
6023 :
6024 : /* Remove edges queued from switch stmt simplification. */
6025 16567308 : for (auto ep : edges_to_remove)
6026 : {
6027 0 : basic_block src = BASIC_BLOCK_FOR_FN (fun, ep.first);
6028 0 : basic_block dest = BASIC_BLOCK_FOR_FN (fun, ep.second);
6029 0 : edge e;
6030 0 : if (src && dest && (e = find_edge (src, dest)))
6031 : {
6032 0 : free_dominance_info (CDI_DOMINATORS);
6033 0 : remove_edge (e);
6034 0 : cfg_changed = true;
6035 : }
6036 : }
6037 :
6038 11043330 : if (get_range_query (fun) != get_global_range_query ())
6039 1542 : disable_ranger (fun);
6040 :
6041 5522436 : if (cfg_changed)
6042 9351 : todoflags |= TODO_cleanup_cfg;
6043 :
6044 5522436 : return todoflags;
6045 5522436 : }
6046 :
6047 : } // anon namespace
6048 :
6049 : gimple_opt_pass *
6050 298828 : make_pass_forwprop (gcc::context *ctxt)
6051 : {
6052 298828 : return new pass_forwprop (ctxt);
6053 : }
|